xref: /freebsd/sys/dev/ath/if_ath_tx.c (revision a3fd3b1429ae6bc1374155b903410f1c0b7582b4)
1b8e788a5SAdrian Chadd /*-
2b8e788a5SAdrian Chadd  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3c6e9cee2SAdrian Chadd  * Copyright (c) 2010-2012 Adrian Chadd, Xenion Pty Ltd
4b8e788a5SAdrian Chadd  * All rights reserved.
5b8e788a5SAdrian Chadd  *
6b8e788a5SAdrian Chadd  * Redistribution and use in source and binary forms, with or without
7b8e788a5SAdrian Chadd  * modification, are permitted provided that the following conditions
8b8e788a5SAdrian Chadd  * are met:
9b8e788a5SAdrian Chadd  * 1. Redistributions of source code must retain the above copyright
10b8e788a5SAdrian Chadd  *    notice, this list of conditions and the following disclaimer,
11b8e788a5SAdrian Chadd  *    without modification.
12b8e788a5SAdrian Chadd  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13b8e788a5SAdrian Chadd  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
14b8e788a5SAdrian Chadd  *    redistribution must be conditioned upon including a substantially
15b8e788a5SAdrian Chadd  *    similar Disclaimer requirement for further binary redistribution.
16b8e788a5SAdrian Chadd  *
17b8e788a5SAdrian Chadd  * NO WARRANTY
18b8e788a5SAdrian Chadd  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19b8e788a5SAdrian Chadd  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20b8e788a5SAdrian Chadd  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
21b8e788a5SAdrian Chadd  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22b8e788a5SAdrian Chadd  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
23b8e788a5SAdrian Chadd  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24b8e788a5SAdrian Chadd  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25b8e788a5SAdrian Chadd  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
26b8e788a5SAdrian Chadd  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27b8e788a5SAdrian Chadd  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28b8e788a5SAdrian Chadd  * THE POSSIBILITY OF SUCH DAMAGES.
29b8e788a5SAdrian Chadd  */
30b8e788a5SAdrian Chadd 
31b8e788a5SAdrian Chadd #include <sys/cdefs.h>
32b8e788a5SAdrian Chadd __FBSDID("$FreeBSD$");
33b8e788a5SAdrian Chadd 
34b8e788a5SAdrian Chadd /*
35b8e788a5SAdrian Chadd  * Driver for the Atheros Wireless LAN controller.
36b8e788a5SAdrian Chadd  *
37b8e788a5SAdrian Chadd  * This software is derived from work of Atsushi Onoe; his contribution
38b8e788a5SAdrian Chadd  * is greatly appreciated.
39b8e788a5SAdrian Chadd  */
40b8e788a5SAdrian Chadd 
41b8e788a5SAdrian Chadd #include "opt_inet.h"
42b8e788a5SAdrian Chadd #include "opt_ath.h"
43b8e788a5SAdrian Chadd #include "opt_wlan.h"
44b8e788a5SAdrian Chadd 
45b8e788a5SAdrian Chadd #include <sys/param.h>
46b8e788a5SAdrian Chadd #include <sys/systm.h>
47b8e788a5SAdrian Chadd #include <sys/sysctl.h>
48b8e788a5SAdrian Chadd #include <sys/mbuf.h>
49b8e788a5SAdrian Chadd #include <sys/malloc.h>
50b8e788a5SAdrian Chadd #include <sys/lock.h>
51b8e788a5SAdrian Chadd #include <sys/mutex.h>
52b8e788a5SAdrian Chadd #include <sys/kernel.h>
53b8e788a5SAdrian Chadd #include <sys/socket.h>
54b8e788a5SAdrian Chadd #include <sys/sockio.h>
55b8e788a5SAdrian Chadd #include <sys/errno.h>
56b8e788a5SAdrian Chadd #include <sys/callout.h>
57b8e788a5SAdrian Chadd #include <sys/bus.h>
58b8e788a5SAdrian Chadd #include <sys/endian.h>
59b8e788a5SAdrian Chadd #include <sys/kthread.h>
60b8e788a5SAdrian Chadd #include <sys/taskqueue.h>
61b8e788a5SAdrian Chadd #include <sys/priv.h>
62f431664cSOlivier Houchard #include <sys/ktr.h>
63b8e788a5SAdrian Chadd 
64b8e788a5SAdrian Chadd #include <machine/bus.h>
65b8e788a5SAdrian Chadd 
66b8e788a5SAdrian Chadd #include <net/if.h>
6776039bc8SGleb Smirnoff #include <net/if_var.h>
68b8e788a5SAdrian Chadd #include <net/if_dl.h>
69b8e788a5SAdrian Chadd #include <net/if_media.h>
70b8e788a5SAdrian Chadd #include <net/if_types.h>
71b8e788a5SAdrian Chadd #include <net/if_arp.h>
72b8e788a5SAdrian Chadd #include <net/ethernet.h>
73b8e788a5SAdrian Chadd #include <net/if_llc.h>
74b8e788a5SAdrian Chadd 
75b8e788a5SAdrian Chadd #include <net80211/ieee80211_var.h>
76b8e788a5SAdrian Chadd #include <net80211/ieee80211_regdomain.h>
77b8e788a5SAdrian Chadd #ifdef IEEE80211_SUPPORT_SUPERG
78b8e788a5SAdrian Chadd #include <net80211/ieee80211_superg.h>
79b8e788a5SAdrian Chadd #endif
80b8e788a5SAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
81b8e788a5SAdrian Chadd #include <net80211/ieee80211_tdma.h>
82b8e788a5SAdrian Chadd #endif
83eb6f0de0SAdrian Chadd #include <net80211/ieee80211_ht.h>
84b8e788a5SAdrian Chadd 
85b8e788a5SAdrian Chadd #include <net/bpf.h>
86b8e788a5SAdrian Chadd 
87b8e788a5SAdrian Chadd #ifdef INET
88b8e788a5SAdrian Chadd #include <netinet/in.h>
89b8e788a5SAdrian Chadd #include <netinet/if_ether.h>
90b8e788a5SAdrian Chadd #endif
91b8e788a5SAdrian Chadd 
92b8e788a5SAdrian Chadd #include <dev/ath/if_athvar.h>
93b8e788a5SAdrian Chadd #include <dev/ath/ath_hal/ah_devid.h>		/* XXX for softled */
94b8e788a5SAdrian Chadd #include <dev/ath/ath_hal/ah_diagcodes.h>
95b8e788a5SAdrian Chadd 
96b8e788a5SAdrian Chadd #include <dev/ath/if_ath_debug.h>
97b8e788a5SAdrian Chadd 
98b8e788a5SAdrian Chadd #ifdef ATH_TX99_DIAG
99b8e788a5SAdrian Chadd #include <dev/ath/ath_tx99/ath_tx99.h>
100b8e788a5SAdrian Chadd #endif
101b8e788a5SAdrian Chadd 
102b8e788a5SAdrian Chadd #include <dev/ath/if_ath_misc.h>
103b8e788a5SAdrian Chadd #include <dev/ath/if_ath_tx.h>
104c1782ce0SAdrian Chadd #include <dev/ath/if_ath_tx_ht.h>
105b8e788a5SAdrian Chadd 
106b69b0dccSAdrian Chadd #ifdef	ATH_DEBUG_ALQ
107b69b0dccSAdrian Chadd #include <dev/ath/if_ath_alq.h>
108b69b0dccSAdrian Chadd #endif
109b69b0dccSAdrian Chadd 
11081a82688SAdrian Chadd /*
111eb6f0de0SAdrian Chadd  * How many retries to perform in software
112eb6f0de0SAdrian Chadd  */
113eb6f0de0SAdrian Chadd #define	SWMAX_RETRIES		10
114eb6f0de0SAdrian Chadd 
1157403d1b9SAdrian Chadd /*
1167403d1b9SAdrian Chadd  * What queue to throw the non-QoS TID traffic into
1177403d1b9SAdrian Chadd  */
1187403d1b9SAdrian Chadd #define	ATH_NONQOS_TID_AC	WME_AC_VO
1197403d1b9SAdrian Chadd 
1200eb81626SAdrian Chadd #if 0
1210eb81626SAdrian Chadd static int ath_tx_node_is_asleep(struct ath_softc *sc, struct ath_node *an);
1220eb81626SAdrian Chadd #endif
123eb6f0de0SAdrian Chadd static int ath_tx_ampdu_pending(struct ath_softc *sc, struct ath_node *an,
124eb6f0de0SAdrian Chadd     int tid);
125eb6f0de0SAdrian Chadd static int ath_tx_ampdu_running(struct ath_softc *sc, struct ath_node *an,
126eb6f0de0SAdrian Chadd     int tid);
127a108d2d6SAdrian Chadd static ieee80211_seq ath_tx_tid_seqno_assign(struct ath_softc *sc,
128a108d2d6SAdrian Chadd     struct ieee80211_node *ni, struct ath_buf *bf, struct mbuf *m0);
129eb6f0de0SAdrian Chadd static int ath_tx_action_frame_override_queue(struct ath_softc *sc,
130eb6f0de0SAdrian Chadd     struct ieee80211_node *ni, struct mbuf *m0, int *tid);
131f1bc738eSAdrian Chadd static struct ath_buf *
132f1bc738eSAdrian Chadd ath_tx_retry_clone(struct ath_softc *sc, struct ath_node *an,
133f1bc738eSAdrian Chadd     struct ath_tid *tid, struct ath_buf *bf);
134eb6f0de0SAdrian Chadd 
135bb327d28SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
136bb327d28SAdrian Chadd void
137bb327d28SAdrian Chadd ath_tx_alq_post(struct ath_softc *sc, struct ath_buf *bf_first)
138bb327d28SAdrian Chadd {
139bb327d28SAdrian Chadd 	struct ath_buf *bf;
140bb327d28SAdrian Chadd 	int i, n;
141bb327d28SAdrian Chadd 	const char *ds;
142bb327d28SAdrian Chadd 
143bb327d28SAdrian Chadd 	/* XXX we should skip out early if debugging isn't enabled! */
144bb327d28SAdrian Chadd 	bf = bf_first;
145bb327d28SAdrian Chadd 
146bb327d28SAdrian Chadd 	while (bf != NULL) {
147bb327d28SAdrian Chadd 		/* XXX should ensure bf_nseg > 0! */
148bb327d28SAdrian Chadd 		if (bf->bf_nseg == 0)
149bb327d28SAdrian Chadd 			break;
150bb327d28SAdrian Chadd 		n = ((bf->bf_nseg - 1) / sc->sc_tx_nmaps) + 1;
151bb327d28SAdrian Chadd 		for (i = 0, ds = (const char *) bf->bf_desc;
152bb327d28SAdrian Chadd 		    i < n;
153bb327d28SAdrian Chadd 		    i++, ds += sc->sc_tx_desclen) {
154bb327d28SAdrian Chadd 			if_ath_alq_post(&sc->sc_alq,
155bb327d28SAdrian Chadd 			    ATH_ALQ_EDMA_TXDESC,
156bb327d28SAdrian Chadd 			    sc->sc_tx_desclen,
157bb327d28SAdrian Chadd 			    ds);
158bb327d28SAdrian Chadd 		}
159bb327d28SAdrian Chadd 		bf = bf->bf_next;
160bb327d28SAdrian Chadd 	}
161bb327d28SAdrian Chadd }
162bb327d28SAdrian Chadd #endif /* ATH_DEBUG_ALQ */
163bb327d28SAdrian Chadd 
164eb6f0de0SAdrian Chadd /*
16581a82688SAdrian Chadd  * Whether to use the 11n rate scenario functions or not
16681a82688SAdrian Chadd  */
16781a82688SAdrian Chadd static inline int
16881a82688SAdrian Chadd ath_tx_is_11n(struct ath_softc *sc)
16981a82688SAdrian Chadd {
1704ddf2cc3SAdrian Chadd 	return ((sc->sc_ah->ah_magic == 0x20065416) ||
1714ddf2cc3SAdrian Chadd 		    (sc->sc_ah->ah_magic == 0x19741014));
17281a82688SAdrian Chadd }
17381a82688SAdrian Chadd 
174eb6f0de0SAdrian Chadd /*
175eb6f0de0SAdrian Chadd  * Obtain the current TID from the given frame.
176eb6f0de0SAdrian Chadd  *
177eb6f0de0SAdrian Chadd  * Non-QoS frames need to go into TID 16 (IEEE80211_NONQOS_TID.)
178eb6f0de0SAdrian Chadd  * This has implications for which AC/priority the packet is placed
179eb6f0de0SAdrian Chadd  * in.
180eb6f0de0SAdrian Chadd  */
181eb6f0de0SAdrian Chadd static int
182eb6f0de0SAdrian Chadd ath_tx_gettid(struct ath_softc *sc, const struct mbuf *m0)
183eb6f0de0SAdrian Chadd {
184eb6f0de0SAdrian Chadd 	const struct ieee80211_frame *wh;
185eb6f0de0SAdrian Chadd 	int pri = M_WME_GETAC(m0);
186eb6f0de0SAdrian Chadd 
187eb6f0de0SAdrian Chadd 	wh = mtod(m0, const struct ieee80211_frame *);
188eb6f0de0SAdrian Chadd 	if (! IEEE80211_QOS_HAS_SEQ(wh))
189eb6f0de0SAdrian Chadd 		return IEEE80211_NONQOS_TID;
190eb6f0de0SAdrian Chadd 	else
191eb6f0de0SAdrian Chadd 		return WME_AC_TO_TID(pri);
192eb6f0de0SAdrian Chadd }
193eb6f0de0SAdrian Chadd 
194f1bc738eSAdrian Chadd static void
195f1bc738eSAdrian Chadd ath_tx_set_retry(struct ath_softc *sc, struct ath_buf *bf)
196f1bc738eSAdrian Chadd {
197f1bc738eSAdrian Chadd 	struct ieee80211_frame *wh;
198f1bc738eSAdrian Chadd 
199f1bc738eSAdrian Chadd 	wh = mtod(bf->bf_m, struct ieee80211_frame *);
200f1bc738eSAdrian Chadd 	/* Only update/resync if needed */
201f1bc738eSAdrian Chadd 	if (bf->bf_state.bfs_isretried == 0) {
202f1bc738eSAdrian Chadd 		wh->i_fc[1] |= IEEE80211_FC1_RETRY;
203f1bc738eSAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
204f1bc738eSAdrian Chadd 		    BUS_DMASYNC_PREWRITE);
205f1bc738eSAdrian Chadd 	}
206f1bc738eSAdrian Chadd 	bf->bf_state.bfs_isretried = 1;
207f1bc738eSAdrian Chadd 	bf->bf_state.bfs_retries ++;
208f1bc738eSAdrian Chadd }
209f1bc738eSAdrian Chadd 
210eb6f0de0SAdrian Chadd /*
211eb6f0de0SAdrian Chadd  * Determine what the correct AC queue for the given frame
212eb6f0de0SAdrian Chadd  * should be.
213eb6f0de0SAdrian Chadd  *
214eb6f0de0SAdrian Chadd  * This code assumes that the TIDs map consistently to
215eb6f0de0SAdrian Chadd  * the underlying hardware (or software) ath_txq.
216eb6f0de0SAdrian Chadd  * Since the sender may try to set an AC which is
217eb6f0de0SAdrian Chadd  * arbitrary, non-QoS TIDs may end up being put on
218eb6f0de0SAdrian Chadd  * completely different ACs. There's no way to put a
219eb6f0de0SAdrian Chadd  * TID into multiple ath_txq's for scheduling, so
220eb6f0de0SAdrian Chadd  * for now we override the AC/TXQ selection and set
221eb6f0de0SAdrian Chadd  * non-QOS TID frames into the BE queue.
222eb6f0de0SAdrian Chadd  *
223eb6f0de0SAdrian Chadd  * This may be completely incorrect - specifically,
224eb6f0de0SAdrian Chadd  * some management frames may end up out of order
225eb6f0de0SAdrian Chadd  * compared to the QoS traffic they're controlling.
226eb6f0de0SAdrian Chadd  * I'll look into this later.
227eb6f0de0SAdrian Chadd  */
228eb6f0de0SAdrian Chadd static int
229eb6f0de0SAdrian Chadd ath_tx_getac(struct ath_softc *sc, const struct mbuf *m0)
230eb6f0de0SAdrian Chadd {
231eb6f0de0SAdrian Chadd 	const struct ieee80211_frame *wh;
232eb6f0de0SAdrian Chadd 	int pri = M_WME_GETAC(m0);
233eb6f0de0SAdrian Chadd 	wh = mtod(m0, const struct ieee80211_frame *);
234eb6f0de0SAdrian Chadd 	if (IEEE80211_QOS_HAS_SEQ(wh))
235eb6f0de0SAdrian Chadd 		return pri;
236eb6f0de0SAdrian Chadd 
2377403d1b9SAdrian Chadd 	return ATH_NONQOS_TID_AC;
238eb6f0de0SAdrian Chadd }
239eb6f0de0SAdrian Chadd 
240b8e788a5SAdrian Chadd void
241b8e788a5SAdrian Chadd ath_txfrag_cleanup(struct ath_softc *sc,
242b8e788a5SAdrian Chadd 	ath_bufhead *frags, struct ieee80211_node *ni)
243b8e788a5SAdrian Chadd {
244b8e788a5SAdrian Chadd 	struct ath_buf *bf, *next;
245b8e788a5SAdrian Chadd 
246b8e788a5SAdrian Chadd 	ATH_TXBUF_LOCK_ASSERT(sc);
247b8e788a5SAdrian Chadd 
2486b349e5aSAdrian Chadd 	TAILQ_FOREACH_SAFE(bf, frags, bf_list, next) {
249b8e788a5SAdrian Chadd 		/* NB: bf assumed clean */
2506b349e5aSAdrian Chadd 		TAILQ_REMOVE(frags, bf, bf_list);
251e1a50456SAdrian Chadd 		ath_returnbuf_head(sc, bf);
252b8e788a5SAdrian Chadd 		ieee80211_node_decref(ni);
253b8e788a5SAdrian Chadd 	}
254b8e788a5SAdrian Chadd }
255b8e788a5SAdrian Chadd 
256b8e788a5SAdrian Chadd /*
257b8e788a5SAdrian Chadd  * Setup xmit of a fragmented frame.  Allocate a buffer
258b8e788a5SAdrian Chadd  * for each frag and bump the node reference count to
259b8e788a5SAdrian Chadd  * reflect the held reference to be setup by ath_tx_start.
260b8e788a5SAdrian Chadd  */
261b8e788a5SAdrian Chadd int
262b8e788a5SAdrian Chadd ath_txfrag_setup(struct ath_softc *sc, ath_bufhead *frags,
263b8e788a5SAdrian Chadd 	struct mbuf *m0, struct ieee80211_node *ni)
264b8e788a5SAdrian Chadd {
265b8e788a5SAdrian Chadd 	struct mbuf *m;
266b8e788a5SAdrian Chadd 	struct ath_buf *bf;
267b8e788a5SAdrian Chadd 
268b8e788a5SAdrian Chadd 	ATH_TXBUF_LOCK(sc);
269b8e788a5SAdrian Chadd 	for (m = m0->m_nextpkt; m != NULL; m = m->m_nextpkt) {
270af33d486SAdrian Chadd 		/* XXX non-management? */
271af33d486SAdrian Chadd 		bf = _ath_getbuf_locked(sc, ATH_BUFTYPE_NORMAL);
272b8e788a5SAdrian Chadd 		if (bf == NULL) {	/* out of buffers, cleanup */
27383bbd5ebSRui Paulo 			DPRINTF(sc, ATH_DEBUG_XMIT, "%s: no buffer?\n",
274b43facbfSAdrian Chadd 			    __func__);
275b8e788a5SAdrian Chadd 			ath_txfrag_cleanup(sc, frags, ni);
276b8e788a5SAdrian Chadd 			break;
277b8e788a5SAdrian Chadd 		}
278b8e788a5SAdrian Chadd 		ieee80211_node_incref(ni);
2796b349e5aSAdrian Chadd 		TAILQ_INSERT_TAIL(frags, bf, bf_list);
280b8e788a5SAdrian Chadd 	}
281b8e788a5SAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
282b8e788a5SAdrian Chadd 
2836b349e5aSAdrian Chadd 	return !TAILQ_EMPTY(frags);
284b8e788a5SAdrian Chadd }
285b8e788a5SAdrian Chadd 
286b8e788a5SAdrian Chadd /*
287b8e788a5SAdrian Chadd  * Reclaim mbuf resources.  For fragmented frames we
288b8e788a5SAdrian Chadd  * need to claim each frag chained with m_nextpkt.
289b8e788a5SAdrian Chadd  */
290b8e788a5SAdrian Chadd void
291b8e788a5SAdrian Chadd ath_freetx(struct mbuf *m)
292b8e788a5SAdrian Chadd {
293b8e788a5SAdrian Chadd 	struct mbuf *next;
294b8e788a5SAdrian Chadd 
295b8e788a5SAdrian Chadd 	do {
296b8e788a5SAdrian Chadd 		next = m->m_nextpkt;
297b8e788a5SAdrian Chadd 		m->m_nextpkt = NULL;
298b8e788a5SAdrian Chadd 		m_freem(m);
299b8e788a5SAdrian Chadd 	} while ((m = next) != NULL);
300b8e788a5SAdrian Chadd }
301b8e788a5SAdrian Chadd 
302b8e788a5SAdrian Chadd static int
303b8e788a5SAdrian Chadd ath_tx_dmasetup(struct ath_softc *sc, struct ath_buf *bf, struct mbuf *m0)
304b8e788a5SAdrian Chadd {
305b8e788a5SAdrian Chadd 	struct mbuf *m;
306b8e788a5SAdrian Chadd 	int error;
307b8e788a5SAdrian Chadd 
308b8e788a5SAdrian Chadd 	/*
309b8e788a5SAdrian Chadd 	 * Load the DMA map so any coalescing is done.  This
310b8e788a5SAdrian Chadd 	 * also calculates the number of descriptors we need.
311b8e788a5SAdrian Chadd 	 */
312b8e788a5SAdrian Chadd 	error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0,
313b8e788a5SAdrian Chadd 				     bf->bf_segs, &bf->bf_nseg,
314b8e788a5SAdrian Chadd 				     BUS_DMA_NOWAIT);
315b8e788a5SAdrian Chadd 	if (error == EFBIG) {
316b8e788a5SAdrian Chadd 		/* XXX packet requires too many descriptors */
31709067b6eSAdrian Chadd 		bf->bf_nseg = ATH_MAX_SCATTER + 1;
318b8e788a5SAdrian Chadd 	} else if (error != 0) {
319b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_busdma++;
320b8e788a5SAdrian Chadd 		ath_freetx(m0);
321b8e788a5SAdrian Chadd 		return error;
322b8e788a5SAdrian Chadd 	}
323b8e788a5SAdrian Chadd 	/*
324b8e788a5SAdrian Chadd 	 * Discard null packets and check for packets that
325b8e788a5SAdrian Chadd 	 * require too many TX descriptors.  We try to convert
326b8e788a5SAdrian Chadd 	 * the latter to a cluster.
327b8e788a5SAdrian Chadd 	 */
32809067b6eSAdrian Chadd 	if (bf->bf_nseg > ATH_MAX_SCATTER) {		/* too many desc's, linearize */
329b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_linear++;
33009067b6eSAdrian Chadd 		m = m_collapse(m0, M_NOWAIT, ATH_MAX_SCATTER);
331b8e788a5SAdrian Chadd 		if (m == NULL) {
332b8e788a5SAdrian Chadd 			ath_freetx(m0);
333b8e788a5SAdrian Chadd 			sc->sc_stats.ast_tx_nombuf++;
334b8e788a5SAdrian Chadd 			return ENOMEM;
335b8e788a5SAdrian Chadd 		}
336b8e788a5SAdrian Chadd 		m0 = m;
337b8e788a5SAdrian Chadd 		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0,
338b8e788a5SAdrian Chadd 					     bf->bf_segs, &bf->bf_nseg,
339b8e788a5SAdrian Chadd 					     BUS_DMA_NOWAIT);
340b8e788a5SAdrian Chadd 		if (error != 0) {
341b8e788a5SAdrian Chadd 			sc->sc_stats.ast_tx_busdma++;
342b8e788a5SAdrian Chadd 			ath_freetx(m0);
343b8e788a5SAdrian Chadd 			return error;
344b8e788a5SAdrian Chadd 		}
34509067b6eSAdrian Chadd 		KASSERT(bf->bf_nseg <= ATH_MAX_SCATTER,
346b8e788a5SAdrian Chadd 		    ("too many segments after defrag; nseg %u", bf->bf_nseg));
347b8e788a5SAdrian Chadd 	} else if (bf->bf_nseg == 0) {		/* null packet, discard */
348b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_nodata++;
349b8e788a5SAdrian Chadd 		ath_freetx(m0);
350b8e788a5SAdrian Chadd 		return EIO;
351b8e788a5SAdrian Chadd 	}
352b8e788a5SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_XMIT, "%s: m %p len %u\n",
353b8e788a5SAdrian Chadd 		__func__, m0, m0->m_pkthdr.len);
354b8e788a5SAdrian Chadd 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
355b8e788a5SAdrian Chadd 	bf->bf_m = m0;
356b8e788a5SAdrian Chadd 
357b8e788a5SAdrian Chadd 	return 0;
358b8e788a5SAdrian Chadd }
359b8e788a5SAdrian Chadd 
3606edf1dc7SAdrian Chadd /*
3616e84772fSAdrian Chadd  * Chain together segments+descriptors for a frame - 11n or otherwise.
3626e84772fSAdrian Chadd  *
3636e84772fSAdrian Chadd  * For aggregates, this is called on each frame in the aggregate.
3646edf1dc7SAdrian Chadd  */
365b8e788a5SAdrian Chadd static void
3666e84772fSAdrian Chadd ath_tx_chaindesclist(struct ath_softc *sc, struct ath_desc *ds0,
3676e84772fSAdrian Chadd     struct ath_buf *bf, int is_aggr, int is_first_subframe,
3686e84772fSAdrian Chadd     int is_last_subframe)
369b8e788a5SAdrian Chadd {
370b8e788a5SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
3716e84772fSAdrian Chadd 	char *ds;
3722b200bb4SAdrian Chadd 	int i, bp, dsp;
37346634305SAdrian Chadd 	HAL_DMA_ADDR bufAddrList[4];
37446634305SAdrian Chadd 	uint32_t segLenList[4];
3752b200bb4SAdrian Chadd 	int numTxMaps = 1;
376e2137b86SAdrian Chadd 	int isFirstDesc = 1;
37746634305SAdrian Chadd 
3783d9b1596SAdrian Chadd 	/*
3793d9b1596SAdrian Chadd 	 * XXX There's txdma and txdma_mgmt; the descriptor
3803d9b1596SAdrian Chadd 	 * sizes must match.
3813d9b1596SAdrian Chadd 	 */
3823d9b1596SAdrian Chadd 	struct ath_descdma *dd = &sc->sc_txdma;
383b8e788a5SAdrian Chadd 
384b8e788a5SAdrian Chadd 	/*
385b8e788a5SAdrian Chadd 	 * Fillin the remainder of the descriptor info.
386b8e788a5SAdrian Chadd 	 */
38746634305SAdrian Chadd 
3882b200bb4SAdrian Chadd 	/*
389378a752fSAdrian Chadd 	 * We need the number of TX data pointers in each descriptor.
390378a752fSAdrian Chadd 	 * EDMA and later chips support 4 TX buffers per descriptor;
391378a752fSAdrian Chadd 	 * previous chips just support one.
3922b200bb4SAdrian Chadd 	 */
393378a752fSAdrian Chadd 	numTxMaps = sc->sc_tx_nmaps;
3942b200bb4SAdrian Chadd 
3952b200bb4SAdrian Chadd 	/*
3962b200bb4SAdrian Chadd 	 * For EDMA and later chips ensure the TX map is fully populated
3972b200bb4SAdrian Chadd 	 * before advancing to the next descriptor.
3982b200bb4SAdrian Chadd 	 */
3996e84772fSAdrian Chadd 	ds = (char *) bf->bf_desc;
4002b200bb4SAdrian Chadd 	bp = dsp = 0;
4012b200bb4SAdrian Chadd 	bzero(bufAddrList, sizeof(bufAddrList));
4022b200bb4SAdrian Chadd 	bzero(segLenList, sizeof(segLenList));
4032b200bb4SAdrian Chadd 	for (i = 0; i < bf->bf_nseg; i++) {
4042b200bb4SAdrian Chadd 		bufAddrList[bp] = bf->bf_segs[i].ds_addr;
4052b200bb4SAdrian Chadd 		segLenList[bp] = bf->bf_segs[i].ds_len;
4062b200bb4SAdrian Chadd 		bp++;
4072b200bb4SAdrian Chadd 
4082b200bb4SAdrian Chadd 		/*
4092b200bb4SAdrian Chadd 		 * Go to the next segment if this isn't the last segment
4102b200bb4SAdrian Chadd 		 * and there's space in the current TX map.
4112b200bb4SAdrian Chadd 		 */
4122b200bb4SAdrian Chadd 		if ((i != bf->bf_nseg - 1) && (bp < numTxMaps))
4132b200bb4SAdrian Chadd 			continue;
4142b200bb4SAdrian Chadd 
4152b200bb4SAdrian Chadd 		/*
4162b200bb4SAdrian Chadd 		 * Last segment or we're out of buffer pointers.
4172b200bb4SAdrian Chadd 		 */
4182b200bb4SAdrian Chadd 		bp = 0;
41946634305SAdrian Chadd 
420b8e788a5SAdrian Chadd 		if (i == bf->bf_nseg - 1)
42142083b3dSAdrian Chadd 			ath_hal_settxdesclink(ah, (struct ath_desc *) ds, 0);
422b8e788a5SAdrian Chadd 		else
42342083b3dSAdrian Chadd 			ath_hal_settxdesclink(ah, (struct ath_desc *) ds,
4242b200bb4SAdrian Chadd 			    bf->bf_daddr + dd->dd_descsize * (dsp + 1));
42546634305SAdrian Chadd 
42646634305SAdrian Chadd 		/*
427fc56c9c5SAdrian Chadd 		 * XXX This assumes that bfs_txq is the actual destination
428fc56c9c5SAdrian Chadd 		 * hardware queue at this point.  It may not have been
429fc56c9c5SAdrian Chadd 		 * assigned, it may actually be pointing to the multicast
430fc56c9c5SAdrian Chadd 		 * software TXQ id.  These must be fixed!
43146634305SAdrian Chadd 		 */
43242083b3dSAdrian Chadd 		ath_hal_filltxdesc(ah, (struct ath_desc *) ds
43346634305SAdrian Chadd 			, bufAddrList
43446634305SAdrian Chadd 			, segLenList
4352b200bb4SAdrian Chadd 			, bf->bf_descid		/* XXX desc id */
436fc56c9c5SAdrian Chadd 			, bf->bf_state.bfs_tx_queue
437e2137b86SAdrian Chadd 			, isFirstDesc		/* first segment */
438b8e788a5SAdrian Chadd 			, i == bf->bf_nseg - 1	/* last segment */
43942083b3dSAdrian Chadd 			, (struct ath_desc *) ds0	/* first descriptor */
440b8e788a5SAdrian Chadd 		);
44121840808SAdrian Chadd 
4426e84772fSAdrian Chadd 		/*
4436e84772fSAdrian Chadd 		 * Make sure the 11n aggregate fields are cleared.
4446e84772fSAdrian Chadd 		 *
4456e84772fSAdrian Chadd 		 * XXX TODO: this doesn't need to be called for
4466e84772fSAdrian Chadd 		 * aggregate frames; as it'll be called on all
4476e84772fSAdrian Chadd 		 * sub-frames.  Since the descriptors are in
4486e84772fSAdrian Chadd 		 * non-cacheable memory, this leads to some
4496e84772fSAdrian Chadd 		 * rather slow writes on MIPS/ARM platforms.
4506e84772fSAdrian Chadd 		 */
45121840808SAdrian Chadd 		if (ath_tx_is_11n(sc))
4525d9b19f7SAdrian Chadd 			ath_hal_clr11n_aggr(sc->sc_ah, (struct ath_desc *) ds);
45321840808SAdrian Chadd 
4546e84772fSAdrian Chadd 		/*
4556e84772fSAdrian Chadd 		 * If 11n is enabled, set it up as if it's an aggregate
4566e84772fSAdrian Chadd 		 * frame.
4576e84772fSAdrian Chadd 		 */
4586e84772fSAdrian Chadd 		if (is_last_subframe) {
4596e84772fSAdrian Chadd 			ath_hal_set11n_aggr_last(sc->sc_ah,
4606e84772fSAdrian Chadd 			    (struct ath_desc *) ds);
4616e84772fSAdrian Chadd 		} else if (is_aggr) {
4626e84772fSAdrian Chadd 			/*
4636e84772fSAdrian Chadd 			 * This clears the aggrlen field; so
4646e84772fSAdrian Chadd 			 * the caller needs to call set_aggr_first()!
4656e84772fSAdrian Chadd 			 *
4666e84772fSAdrian Chadd 			 * XXX TODO: don't call this for the first
4676e84772fSAdrian Chadd 			 * descriptor in the first frame in an
4686e84772fSAdrian Chadd 			 * aggregate!
4696e84772fSAdrian Chadd 			 */
4706e84772fSAdrian Chadd 			ath_hal_set11n_aggr_middle(sc->sc_ah,
4716e84772fSAdrian Chadd 			    (struct ath_desc *) ds,
4726e84772fSAdrian Chadd 			    bf->bf_state.bfs_ndelim);
4736e84772fSAdrian Chadd 		}
474e2137b86SAdrian Chadd 		isFirstDesc = 0;
47542083b3dSAdrian Chadd 		bf->bf_lastds = (struct ath_desc *) ds;
4762b200bb4SAdrian Chadd 
4772b200bb4SAdrian Chadd 		/*
4782b200bb4SAdrian Chadd 		 * Don't forget to skip to the next descriptor.
4792b200bb4SAdrian Chadd 		 */
48042083b3dSAdrian Chadd 		ds += sc->sc_tx_desclen;
4812b200bb4SAdrian Chadd 		dsp++;
4822b200bb4SAdrian Chadd 
4832b200bb4SAdrian Chadd 		/*
4842b200bb4SAdrian Chadd 		 * .. and don't forget to blank these out!
4852b200bb4SAdrian Chadd 		 */
4862b200bb4SAdrian Chadd 		bzero(bufAddrList, sizeof(bufAddrList));
4872b200bb4SAdrian Chadd 		bzero(segLenList, sizeof(segLenList));
488b8e788a5SAdrian Chadd 	}
4894d7f8837SAdrian Chadd 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
49081a82688SAdrian Chadd }
49181a82688SAdrian Chadd 
492eb6f0de0SAdrian Chadd /*
493d34a7347SAdrian Chadd  * Set the rate control fields in the given descriptor based on
494d34a7347SAdrian Chadd  * the bf_state fields and node state.
495d34a7347SAdrian Chadd  *
496d34a7347SAdrian Chadd  * The bfs fields should already be set with the relevant rate
497d34a7347SAdrian Chadd  * control information, including whether MRR is to be enabled.
498d34a7347SAdrian Chadd  *
499d34a7347SAdrian Chadd  * Since the FreeBSD HAL currently sets up the first TX rate
500d34a7347SAdrian Chadd  * in ath_hal_setuptxdesc(), this will setup the MRR
501d34a7347SAdrian Chadd  * conditionally for the pre-11n chips, and call ath_buf_set_rate
502d34a7347SAdrian Chadd  * unconditionally for 11n chips. These require the 11n rate
503d34a7347SAdrian Chadd  * scenario to be set if MCS rates are enabled, so it's easier
504d34a7347SAdrian Chadd  * to just always call it. The caller can then only set rates 2, 3
505d34a7347SAdrian Chadd  * and 4 if multi-rate retry is needed.
506d34a7347SAdrian Chadd  */
507d34a7347SAdrian Chadd static void
508d34a7347SAdrian Chadd ath_tx_set_ratectrl(struct ath_softc *sc, struct ieee80211_node *ni,
509d34a7347SAdrian Chadd     struct ath_buf *bf)
510d34a7347SAdrian Chadd {
511d34a7347SAdrian Chadd 	struct ath_rc_series *rc = bf->bf_state.bfs_rc;
512d34a7347SAdrian Chadd 
513d34a7347SAdrian Chadd 	/* If mrr is disabled, blank tries 1, 2, 3 */
514d34a7347SAdrian Chadd 	if (! bf->bf_state.bfs_ismrr)
515d34a7347SAdrian Chadd 		rc[1].tries = rc[2].tries = rc[3].tries = 0;
516d34a7347SAdrian Chadd 
517491e1248SAdrian Chadd #if 0
518491e1248SAdrian Chadd 	/*
519491e1248SAdrian Chadd 	 * If NOACK is set, just set ntries=1.
520491e1248SAdrian Chadd 	 */
521491e1248SAdrian Chadd 	else if (bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) {
522491e1248SAdrian Chadd 		rc[1].tries = rc[2].tries = rc[3].tries = 0;
523491e1248SAdrian Chadd 		rc[0].tries = 1;
524491e1248SAdrian Chadd 	}
525491e1248SAdrian Chadd #endif
526491e1248SAdrian Chadd 
527d34a7347SAdrian Chadd 	/*
528d34a7347SAdrian Chadd 	 * Always call - that way a retried descriptor will
529d34a7347SAdrian Chadd 	 * have the MRR fields overwritten.
530d34a7347SAdrian Chadd 	 *
531d34a7347SAdrian Chadd 	 * XXX TODO: see if this is really needed - setting up
532d34a7347SAdrian Chadd 	 * the first descriptor should set the MRR fields to 0
533d34a7347SAdrian Chadd 	 * for us anyway.
534d34a7347SAdrian Chadd 	 */
535d34a7347SAdrian Chadd 	if (ath_tx_is_11n(sc)) {
536d34a7347SAdrian Chadd 		ath_buf_set_rate(sc, ni, bf);
537d34a7347SAdrian Chadd 	} else {
538d34a7347SAdrian Chadd 		ath_hal_setupxtxdesc(sc->sc_ah, bf->bf_desc
539d34a7347SAdrian Chadd 			, rc[1].ratecode, rc[1].tries
540d34a7347SAdrian Chadd 			, rc[2].ratecode, rc[2].tries
541d34a7347SAdrian Chadd 			, rc[3].ratecode, rc[3].tries
542d34a7347SAdrian Chadd 		);
543d34a7347SAdrian Chadd 	}
544d34a7347SAdrian Chadd }
545d34a7347SAdrian Chadd 
546d34a7347SAdrian Chadd /*
547eb6f0de0SAdrian Chadd  * Setup segments+descriptors for an 11n aggregate.
548eb6f0de0SAdrian Chadd  * bf_first is the first buffer in the aggregate.
549eb6f0de0SAdrian Chadd  * The descriptor list must already been linked together using
550eb6f0de0SAdrian Chadd  * bf->bf_next.
551eb6f0de0SAdrian Chadd  */
552eb6f0de0SAdrian Chadd static void
553eb6f0de0SAdrian Chadd ath_tx_setds_11n(struct ath_softc *sc, struct ath_buf *bf_first)
554eb6f0de0SAdrian Chadd {
555eb6f0de0SAdrian Chadd 	struct ath_buf *bf, *bf_prev = NULL;
5566e84772fSAdrian Chadd 	struct ath_desc *ds0 = bf_first->bf_desc;
557eb6f0de0SAdrian Chadd 
558eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: nframes=%d, al=%d\n",
559eb6f0de0SAdrian Chadd 	    __func__, bf_first->bf_state.bfs_nframes,
560eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_al);
561eb6f0de0SAdrian Chadd 
5627d9dd2acSAdrian Chadd 	bf = bf_first;
5637d9dd2acSAdrian Chadd 
5647d9dd2acSAdrian Chadd 	if (bf->bf_state.bfs_txrate0 == 0)
56583bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: bf=%p, txrate0=%d\n",
5667d9dd2acSAdrian Chadd 		    __func__, bf, 0);
5677d9dd2acSAdrian Chadd 	if (bf->bf_state.bfs_rc[0].ratecode == 0)
56883bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: bf=%p, rix0=%d\n",
5697d9dd2acSAdrian Chadd 		    __func__, bf, 0);
5707d9dd2acSAdrian Chadd 
571eb6f0de0SAdrian Chadd 	/*
5726e84772fSAdrian Chadd 	 * Setup all descriptors of all subframes - this will
5736e84772fSAdrian Chadd 	 * call ath_hal_set11naggrmiddle() on every frame.
574eb6f0de0SAdrian Chadd 	 */
575eb6f0de0SAdrian Chadd 	while (bf != NULL) {
576eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
577eb6f0de0SAdrian Chadd 		    "%s: bf=%p, nseg=%d, pktlen=%d, seqno=%d\n",
578eb6f0de0SAdrian Chadd 		    __func__, bf, bf->bf_nseg, bf->bf_state.bfs_pktlen,
579eb6f0de0SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
580eb6f0de0SAdrian Chadd 
5816e84772fSAdrian Chadd 		/*
5826e84772fSAdrian Chadd 		 * Setup the initial fields for the first descriptor - all
5836e84772fSAdrian Chadd 		 * the non-11n specific stuff.
5846e84772fSAdrian Chadd 		 */
5856e84772fSAdrian Chadd 		ath_hal_setuptxdesc(sc->sc_ah, bf->bf_desc
5866e84772fSAdrian Chadd 			, bf->bf_state.bfs_pktlen	/* packet length */
5876e84772fSAdrian Chadd 			, bf->bf_state.bfs_hdrlen	/* header length */
5886e84772fSAdrian Chadd 			, bf->bf_state.bfs_atype	/* Atheros packet type */
5896e84772fSAdrian Chadd 			, bf->bf_state.bfs_txpower	/* txpower */
5906e84772fSAdrian Chadd 			, bf->bf_state.bfs_txrate0
5916e84772fSAdrian Chadd 			, bf->bf_state.bfs_try0		/* series 0 rate/tries */
5926e84772fSAdrian Chadd 			, bf->bf_state.bfs_keyix	/* key cache index */
5936e84772fSAdrian Chadd 			, bf->bf_state.bfs_txantenna	/* antenna mode */
5946e84772fSAdrian Chadd 			, bf->bf_state.bfs_txflags | HAL_TXDESC_INTREQ	/* flags */
5956e84772fSAdrian Chadd 			, bf->bf_state.bfs_ctsrate	/* rts/cts rate */
5966e84772fSAdrian Chadd 			, bf->bf_state.bfs_ctsduration	/* rts/cts duration */
5976e84772fSAdrian Chadd 		);
5986e84772fSAdrian Chadd 
5996e84772fSAdrian Chadd 		/*
6006e84772fSAdrian Chadd 		 * First descriptor? Setup the rate control and initial
6016e84772fSAdrian Chadd 		 * aggregate header information.
6026e84772fSAdrian Chadd 		 */
6036e84772fSAdrian Chadd 		if (bf == bf_first) {
6046e84772fSAdrian Chadd 			/*
6056e84772fSAdrian Chadd 			 * setup first desc with rate and aggr info
6066e84772fSAdrian Chadd 			 */
6076e84772fSAdrian Chadd 			ath_tx_set_ratectrl(sc, bf->bf_node, bf);
6086e84772fSAdrian Chadd 		}
6096e84772fSAdrian Chadd 
6106e84772fSAdrian Chadd 		/*
6116e84772fSAdrian Chadd 		 * Setup the descriptors for a multi-descriptor frame.
6126e84772fSAdrian Chadd 		 * This is both aggregate and non-aggregate aware.
6136e84772fSAdrian Chadd 		 */
6146e84772fSAdrian Chadd 		ath_tx_chaindesclist(sc, ds0, bf,
6156e84772fSAdrian Chadd 		    1, /* is_aggr */
6166e84772fSAdrian Chadd 		    !! (bf == bf_first), /* is_first_subframe */
6176e84772fSAdrian Chadd 		    !! (bf->bf_next == NULL) /* is_last_subframe */
6186e84772fSAdrian Chadd 		    );
6196e84772fSAdrian Chadd 
6206e84772fSAdrian Chadd 		if (bf == bf_first) {
6216e84772fSAdrian Chadd 			/*
6226e84772fSAdrian Chadd 			 * Initialise the first 11n aggregate with the
6236e84772fSAdrian Chadd 			 * aggregate length and aggregate enable bits.
6246e84772fSAdrian Chadd 			 */
6256e84772fSAdrian Chadd 			ath_hal_set11n_aggr_first(sc->sc_ah,
6266e84772fSAdrian Chadd 			    ds0,
6276e84772fSAdrian Chadd 			    bf->bf_state.bfs_al,
6286e84772fSAdrian Chadd 			    bf->bf_state.bfs_ndelim);
6296e84772fSAdrian Chadd 		}
630eb6f0de0SAdrian Chadd 
631eb6f0de0SAdrian Chadd 		/*
632eb6f0de0SAdrian Chadd 		 * Link the last descriptor of the previous frame
633eb6f0de0SAdrian Chadd 		 * to the beginning descriptor of this frame.
634eb6f0de0SAdrian Chadd 		 */
635eb6f0de0SAdrian Chadd 		if (bf_prev != NULL)
636bb069955SAdrian Chadd 			ath_hal_settxdesclink(sc->sc_ah, bf_prev->bf_lastds,
637bb069955SAdrian Chadd 			    bf->bf_daddr);
638eb6f0de0SAdrian Chadd 
639eb6f0de0SAdrian Chadd 		/* Save a copy so we can link the next descriptor in */
640eb6f0de0SAdrian Chadd 		bf_prev = bf;
641eb6f0de0SAdrian Chadd 		bf = bf->bf_next;
642eb6f0de0SAdrian Chadd 	}
643eb6f0de0SAdrian Chadd 
644eb6f0de0SAdrian Chadd 	/*
645eb6f0de0SAdrian Chadd 	 * Set the first descriptor bf_lastds field to point to
646eb6f0de0SAdrian Chadd 	 * the last descriptor in the last subframe, that's where
647eb6f0de0SAdrian Chadd 	 * the status update will occur.
648eb6f0de0SAdrian Chadd 	 */
649eb6f0de0SAdrian Chadd 	bf_first->bf_lastds = bf_prev->bf_lastds;
650eb6f0de0SAdrian Chadd 
651eb6f0de0SAdrian Chadd 	/*
652eb6f0de0SAdrian Chadd 	 * And bf_last in the first descriptor points to the end of
653eb6f0de0SAdrian Chadd 	 * the aggregate list.
654eb6f0de0SAdrian Chadd 	 */
655eb6f0de0SAdrian Chadd 	bf_first->bf_last = bf_prev;
656eb6f0de0SAdrian Chadd 
657bbdf3df1SAdrian Chadd 	/*
658bbdf3df1SAdrian Chadd 	 * For non-AR9300 NICs, which require the rate control
659bbdf3df1SAdrian Chadd 	 * in the final descriptor - let's set that up now.
660bbdf3df1SAdrian Chadd 	 *
661bbdf3df1SAdrian Chadd 	 * This is because the filltxdesc() HAL call doesn't
662bbdf3df1SAdrian Chadd 	 * populate the last segment with rate control information
663bbdf3df1SAdrian Chadd 	 * if firstSeg is also true.  For non-aggregate frames
664bbdf3df1SAdrian Chadd 	 * that is fine, as the first frame already has rate control
665bbdf3df1SAdrian Chadd 	 * info.  But if the last frame in an aggregate has one
666bbdf3df1SAdrian Chadd 	 * descriptor, both firstseg and lastseg will be true and
667bbdf3df1SAdrian Chadd 	 * the rate info isn't copied.
668bbdf3df1SAdrian Chadd 	 *
669bbdf3df1SAdrian Chadd 	 * This is inefficient on MIPS/ARM platforms that have
670bbdf3df1SAdrian Chadd 	 * non-cachable memory for TX descriptors, but we'll just
671bbdf3df1SAdrian Chadd 	 * make do for now.
672bbdf3df1SAdrian Chadd 	 *
673bbdf3df1SAdrian Chadd 	 * As to why the rate table is stashed in the last descriptor
674bbdf3df1SAdrian Chadd 	 * rather than the first descriptor?  Because proctxdesc()
675bbdf3df1SAdrian Chadd 	 * is called on the final descriptor in an MPDU or A-MPDU -
676bbdf3df1SAdrian Chadd 	 * ie, the one that gets updated by the hardware upon
677bbdf3df1SAdrian Chadd 	 * completion.  That way proctxdesc() doesn't need to know
678bbdf3df1SAdrian Chadd 	 * about the first _and_ last TX descriptor.
679bbdf3df1SAdrian Chadd 	 */
680bbdf3df1SAdrian Chadd 	ath_hal_setuplasttxdesc(sc->sc_ah, bf_prev->bf_lastds, ds0);
681bbdf3df1SAdrian Chadd 
682eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: end\n", __func__);
683eb6f0de0SAdrian Chadd }
684eb6f0de0SAdrian Chadd 
68546634305SAdrian Chadd /*
68646634305SAdrian Chadd  * Hand-off a frame to the multicast TX queue.
68746634305SAdrian Chadd  *
68846634305SAdrian Chadd  * This is a software TXQ which will be appended to the CAB queue
68946634305SAdrian Chadd  * during the beacon setup code.
69046634305SAdrian Chadd  *
69146634305SAdrian Chadd  * XXX TODO: since the AR9300 EDMA TX queue support wants the QCU ID
692fc56c9c5SAdrian Chadd  * as part of the TX descriptor, bf_state.bfs_tx_queue must be updated
69346634305SAdrian Chadd  * with the actual hardware txq, or all of this will fall apart.
69446634305SAdrian Chadd  *
69546634305SAdrian Chadd  * XXX It may not be a bad idea to just stuff the QCU ID into bf_state
696fc56c9c5SAdrian Chadd  * and retire bfs_tx_queue; then make sure the CABQ QCU ID is populated
69746634305SAdrian Chadd  * correctly.
69846634305SAdrian Chadd  */
699eb6f0de0SAdrian Chadd static void
700eb6f0de0SAdrian Chadd ath_tx_handoff_mcast(struct ath_softc *sc, struct ath_txq *txq,
701eb6f0de0SAdrian Chadd     struct ath_buf *bf)
702eb6f0de0SAdrian Chadd {
703375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
704375307d4SAdrian Chadd 
705eb6f0de0SAdrian Chadd 	KASSERT((bf->bf_flags & ATH_BUF_BUSY) == 0,
706eb6f0de0SAdrian Chadd 	     ("%s: busy status 0x%x", __func__, bf->bf_flags));
70756a85978SAdrian Chadd 
70897c9a8e8SAdrian Chadd 	/*
70997c9a8e8SAdrian Chadd 	 * Ensure that the tx queue is the cabq, so things get
71097c9a8e8SAdrian Chadd 	 * mapped correctly.
71197c9a8e8SAdrian Chadd 	 */
71297c9a8e8SAdrian Chadd 	if (bf->bf_state.bfs_tx_queue != sc->sc_cabq->axq_qnum) {
71383bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_XMIT,
71497c9a8e8SAdrian Chadd 		    "%s: bf=%p, bfs_tx_queue=%d, axq_qnum=%d\n",
71583bbd5ebSRui Paulo 		    __func__, bf, bf->bf_state.bfs_tx_queue,
71697c9a8e8SAdrian Chadd 		    txq->axq_qnum);
71797c9a8e8SAdrian Chadd 	}
71897c9a8e8SAdrian Chadd 
71956a85978SAdrian Chadd 	ATH_TXQ_LOCK(txq);
7200891354cSAdrian Chadd 	if (ATH_TXQ_LAST(txq, axq_q_s) != NULL) {
7210891354cSAdrian Chadd 		struct ath_buf *bf_last = ATH_TXQ_LAST(txq, axq_q_s);
722eb6f0de0SAdrian Chadd 		struct ieee80211_frame *wh;
723eb6f0de0SAdrian Chadd 
724eb6f0de0SAdrian Chadd 		/* mark previous frame */
7250891354cSAdrian Chadd 		wh = mtod(bf_last->bf_m, struct ieee80211_frame *);
726eb6f0de0SAdrian Chadd 		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
7270891354cSAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf_last->bf_dmamap,
728eb6f0de0SAdrian Chadd 		    BUS_DMASYNC_PREWRITE);
729eb6f0de0SAdrian Chadd 
730eb6f0de0SAdrian Chadd 		/* link descriptor */
7310891354cSAdrian Chadd 		ath_hal_settxdesclink(sc->sc_ah,
7320891354cSAdrian Chadd 		    bf_last->bf_lastds,
7330891354cSAdrian Chadd 		    bf->bf_daddr);
734eb6f0de0SAdrian Chadd 	}
735eb6f0de0SAdrian Chadd 	ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
736b837332dSAdrian Chadd 	ATH_TXQ_UNLOCK(txq);
737eb6f0de0SAdrian Chadd }
738eb6f0de0SAdrian Chadd 
739eb6f0de0SAdrian Chadd /*
740eb6f0de0SAdrian Chadd  * Hand-off packet to a hardware queue.
741eb6f0de0SAdrian Chadd  */
742eb6f0de0SAdrian Chadd static void
743d4365d16SAdrian Chadd ath_tx_handoff_hw(struct ath_softc *sc, struct ath_txq *txq,
744d4365d16SAdrian Chadd     struct ath_buf *bf)
745eb6f0de0SAdrian Chadd {
746eb6f0de0SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
7479be82a42SAdrian Chadd 	struct ath_buf *bf_first;
74881a82688SAdrian Chadd 
749b8e788a5SAdrian Chadd 	/*
750b8e788a5SAdrian Chadd 	 * Insert the frame on the outbound list and pass it on
751b8e788a5SAdrian Chadd 	 * to the hardware.  Multicast frames buffered for power
752b8e788a5SAdrian Chadd 	 * save stations and transmit from the CAB queue are stored
753b8e788a5SAdrian Chadd 	 * on a s/w only queue and loaded on to the CAB queue in
754b8e788a5SAdrian Chadd 	 * the SWBA handler since frames only go out on DTIM and
755b8e788a5SAdrian Chadd 	 * to avoid possible races.
756b8e788a5SAdrian Chadd 	 */
757375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
758b8e788a5SAdrian Chadd 	KASSERT((bf->bf_flags & ATH_BUF_BUSY) == 0,
759eb6f0de0SAdrian Chadd 	     ("%s: busy status 0x%x", __func__, bf->bf_flags));
760eb6f0de0SAdrian Chadd 	KASSERT(txq->axq_qnum != ATH_TXQ_SWQ,
761eb6f0de0SAdrian Chadd 	     ("ath_tx_handoff_hw called for mcast queue"));
762eb6f0de0SAdrian Chadd 
7639be82a42SAdrian Chadd 	/*
7649be82a42SAdrian Chadd 	 * XXX racy, should hold the PCU lock when checking this,
7659be82a42SAdrian Chadd 	 * and also should ensure that the TX counter is >0!
7669be82a42SAdrian Chadd 	 */
7679be82a42SAdrian Chadd 	KASSERT((sc->sc_inreset_cnt == 0),
7689be82a42SAdrian Chadd 	    ("%s: TX during reset?\n", __func__));
7699be82a42SAdrian Chadd 
770ef27340cSAdrian Chadd #if 0
771ef27340cSAdrian Chadd 	/*
772ef27340cSAdrian Chadd 	 * This causes a LOR. Find out where the PCU lock is being
773ef27340cSAdrian Chadd 	 * held whilst the TXQ lock is grabbed - that shouldn't
774ef27340cSAdrian Chadd 	 * be occuring.
775ef27340cSAdrian Chadd 	 */
776ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
777ef27340cSAdrian Chadd 	if (sc->sc_inreset_cnt) {
778ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
779ef27340cSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_RESET,
780ef27340cSAdrian Chadd 		    "%s: called with sc_in_reset != 0\n",
781ef27340cSAdrian Chadd 		    __func__);
782ef27340cSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_XMIT,
783ef27340cSAdrian Chadd 		    "%s: queued: TXDP[%u] = %p (%p) depth %d\n",
784ef27340cSAdrian Chadd 		    __func__, txq->axq_qnum,
785ef27340cSAdrian Chadd 		    (caddr_t)bf->bf_daddr, bf->bf_desc,
786ef27340cSAdrian Chadd 		    txq->axq_depth);
7879be82a42SAdrian Chadd 		/* XXX axq_link needs to be set and updated! */
788ef27340cSAdrian Chadd 		ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
789ef27340cSAdrian Chadd 		if (bf->bf_state.bfs_aggr)
790ef27340cSAdrian Chadd 			txq->axq_aggr_depth++;
791ef27340cSAdrian Chadd 		return;
792ef27340cSAdrian Chadd 		}
793ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
794ef27340cSAdrian Chadd #endif
795ef27340cSAdrian Chadd 
796b837332dSAdrian Chadd 	ATH_TXQ_LOCK(txq);
797b8e788a5SAdrian Chadd 
798b8e788a5SAdrian Chadd 	/*
7999be82a42SAdrian Chadd 	 * XXX TODO: if there's a holdingbf, then
8009be82a42SAdrian Chadd 	 * ATH_TXQ_PUTRUNNING should be clear.
8019be82a42SAdrian Chadd 	 *
8029be82a42SAdrian Chadd 	 * If there is a holdingbf and the list is empty,
8039be82a42SAdrian Chadd 	 * then axq_link should be pointing to the holdingbf.
8049be82a42SAdrian Chadd 	 *
8059be82a42SAdrian Chadd 	 * Otherwise it should point to the last descriptor
8069be82a42SAdrian Chadd 	 * in the last ath_buf.
8079be82a42SAdrian Chadd 	 *
8089be82a42SAdrian Chadd 	 * In any case, we should really ensure that we
8099be82a42SAdrian Chadd 	 * update the previous descriptor link pointer to
8109be82a42SAdrian Chadd 	 * this descriptor, regardless of all of the above state.
8119be82a42SAdrian Chadd 	 *
8129be82a42SAdrian Chadd 	 * For now this is captured by having axq_link point
8139be82a42SAdrian Chadd 	 * to either the holdingbf (if the TXQ list is empty)
8149be82a42SAdrian Chadd 	 * or the end of the list (if the TXQ list isn't empty.)
8159be82a42SAdrian Chadd 	 * I'd rather just kill axq_link here and do it as above.
816b8e788a5SAdrian Chadd 	 */
81703682514SAdrian Chadd 
818b8e788a5SAdrian Chadd 	/*
8199be82a42SAdrian Chadd 	 * Append the frame to the TX queue.
820b8e788a5SAdrian Chadd 	 */
821b8e788a5SAdrian Chadd 	ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
822b1dddc28SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 3,
823b1dddc28SAdrian Chadd 	    "ath_tx_handoff: non-tdma: txq=%u, add bf=%p "
82403682514SAdrian Chadd 	    "depth=%d",
82503682514SAdrian Chadd 	    txq->axq_qnum,
82603682514SAdrian Chadd 	    bf,
82703682514SAdrian Chadd 	    txq->axq_depth);
82803682514SAdrian Chadd 
8299be82a42SAdrian Chadd 	/*
8309be82a42SAdrian Chadd 	 * If there's a link pointer, update it.
8319be82a42SAdrian Chadd 	 *
8329be82a42SAdrian Chadd 	 * XXX we should replace this with the above logic, just
8339be82a42SAdrian Chadd 	 * to kill axq_link with fire.
8349be82a42SAdrian Chadd 	 */
8359be82a42SAdrian Chadd 	if (txq->axq_link != NULL) {
836b8e788a5SAdrian Chadd 		*txq->axq_link = bf->bf_daddr;
837b8e788a5SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_XMIT,
838b8e788a5SAdrian Chadd 		    "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
839b8e788a5SAdrian Chadd 		    txq->axq_qnum, txq->axq_link,
840d4365d16SAdrian Chadd 		    (caddr_t)bf->bf_daddr, bf->bf_desc,
841d4365d16SAdrian Chadd 		    txq->axq_depth);
84203682514SAdrian Chadd 		ATH_KTR(sc, ATH_KTR_TX, 5,
84303682514SAdrian Chadd 		    "ath_tx_handoff: non-tdma: link[%u](%p)=%p (%p) "
84403682514SAdrian Chadd 		    "lastds=%d",
84503682514SAdrian Chadd 		    txq->axq_qnum, txq->axq_link,
84603682514SAdrian Chadd 		    (caddr_t)bf->bf_daddr, bf->bf_desc,
84703682514SAdrian Chadd 		    bf->bf_lastds);
848b8e788a5SAdrian Chadd 	}
84997c9a8e8SAdrian Chadd 
8509be82a42SAdrian Chadd 	/*
8519be82a42SAdrian Chadd 	 * If we've not pushed anything into the hardware yet,
8529be82a42SAdrian Chadd 	 * push the head of the queue into the TxDP.
8539be82a42SAdrian Chadd 	 *
8549be82a42SAdrian Chadd 	 * Once we've started DMA, there's no guarantee that
8559be82a42SAdrian Chadd 	 * updating the TxDP with a new value will actually work.
8569be82a42SAdrian Chadd 	 * So we just don't do that - if we hit the end of the list,
8579be82a42SAdrian Chadd 	 * we keep that buffer around (the "holding buffer") and
8589be82a42SAdrian Chadd 	 * re-start DMA by updating the link pointer of _that_
8599be82a42SAdrian Chadd 	 * descriptor and then restart DMA.
8609be82a42SAdrian Chadd 	 */
8619be82a42SAdrian Chadd 	if (! (txq->axq_flags & ATH_TXQ_PUTRUNNING)) {
8629be82a42SAdrian Chadd 		bf_first = TAILQ_FIRST(&txq->axq_q);
8639be82a42SAdrian Chadd 		txq->axq_flags |= ATH_TXQ_PUTRUNNING;
8649be82a42SAdrian Chadd 		ath_hal_puttxbuf(ah, txq->axq_qnum, bf_first->bf_daddr);
8659be82a42SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_XMIT,
8669be82a42SAdrian Chadd 		    "%s: TXDP[%u] = %p (%p) depth %d\n",
8679be82a42SAdrian Chadd 		    __func__, txq->axq_qnum,
8689be82a42SAdrian Chadd 		    (caddr_t)bf_first->bf_daddr, bf_first->bf_desc,
8699be82a42SAdrian Chadd 		    txq->axq_depth);
8709be82a42SAdrian Chadd 		ATH_KTR(sc, ATH_KTR_TX, 5,
8719be82a42SAdrian Chadd 		    "ath_tx_handoff: TXDP[%u] = %p (%p) "
8729be82a42SAdrian Chadd 		    "lastds=%p depth %d",
8739be82a42SAdrian Chadd 		    txq->axq_qnum,
8749be82a42SAdrian Chadd 		    (caddr_t)bf_first->bf_daddr, bf_first->bf_desc,
8759be82a42SAdrian Chadd 		    bf_first->bf_lastds,
8769be82a42SAdrian Chadd 		    txq->axq_depth);
8779be82a42SAdrian Chadd 	}
8789be82a42SAdrian Chadd 
8799be82a42SAdrian Chadd 	/*
8809be82a42SAdrian Chadd 	 * Ensure that the bf TXQ matches this TXQ, so later
8819be82a42SAdrian Chadd 	 * checking and holding buffer manipulation is sane.
8829be82a42SAdrian Chadd 	 */
88397c9a8e8SAdrian Chadd 	if (bf->bf_state.bfs_tx_queue != txq->axq_qnum) {
88483bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_XMIT,
88597c9a8e8SAdrian Chadd 		    "%s: bf=%p, bfs_tx_queue=%d, axq_qnum=%d\n",
88683bbd5ebSRui Paulo 		    __func__, bf, bf->bf_state.bfs_tx_queue,
88797c9a8e8SAdrian Chadd 		    txq->axq_qnum);
88897c9a8e8SAdrian Chadd 	}
88997c9a8e8SAdrian Chadd 
8909be82a42SAdrian Chadd 	/*
8919be82a42SAdrian Chadd 	 * Track aggregate queue depth.
8929be82a42SAdrian Chadd 	 */
8936edf1dc7SAdrian Chadd 	if (bf->bf_state.bfs_aggr)
8946edf1dc7SAdrian Chadd 		txq->axq_aggr_depth++;
8959be82a42SAdrian Chadd 
8969be82a42SAdrian Chadd 	/*
8979be82a42SAdrian Chadd 	 * Update the link pointer.
8989be82a42SAdrian Chadd 	 */
899bb069955SAdrian Chadd 	ath_hal_gettxdesclinkptr(ah, bf->bf_lastds, &txq->axq_link);
9009be82a42SAdrian Chadd 
9019be82a42SAdrian Chadd 	/*
9029be82a42SAdrian Chadd 	 * Start DMA.
9039be82a42SAdrian Chadd 	 *
9049be82a42SAdrian Chadd 	 * If we wrote a TxDP above, DMA will start from here.
9059be82a42SAdrian Chadd 	 *
9069be82a42SAdrian Chadd 	 * If DMA is running, it'll do nothing.
9079be82a42SAdrian Chadd 	 *
9089be82a42SAdrian Chadd 	 * If the DMA engine hit the end of the QCU list (ie LINK=NULL,
9099be82a42SAdrian Chadd 	 * or VEOL) then it stops at the last transmitted write.
9109be82a42SAdrian Chadd 	 * We then append a new frame by updating the link pointer
9119be82a42SAdrian Chadd 	 * in that descriptor and then kick TxE here; it will re-read
9129be82a42SAdrian Chadd 	 * that last descriptor and find the new descriptor to transmit.
9139be82a42SAdrian Chadd 	 *
9149be82a42SAdrian Chadd 	 * This is why we keep the holding descriptor around.
9159be82a42SAdrian Chadd 	 */
916b8e788a5SAdrian Chadd 	ath_hal_txstart(ah, txq->axq_qnum);
917b837332dSAdrian Chadd 	ATH_TXQ_UNLOCK(txq);
91803682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 1,
91903682514SAdrian Chadd 	    "ath_tx_handoff: txq=%u, txstart", txq->axq_qnum);
920b8e788a5SAdrian Chadd }
921eb6f0de0SAdrian Chadd 
922eb6f0de0SAdrian Chadd /*
923eb6f0de0SAdrian Chadd  * Restart TX DMA for the given TXQ.
924eb6f0de0SAdrian Chadd  *
925eb6f0de0SAdrian Chadd  * This must be called whether the queue is empty or not.
926eb6f0de0SAdrian Chadd  */
927746bab5bSAdrian Chadd static void
928746bab5bSAdrian Chadd ath_legacy_tx_dma_restart(struct ath_softc *sc, struct ath_txq *txq)
929eb6f0de0SAdrian Chadd {
930b1f3262cSAdrian Chadd 	struct ath_buf *bf, *bf_last;
931eb6f0de0SAdrian Chadd 
932b837332dSAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
933eb6f0de0SAdrian Chadd 
934b1f3262cSAdrian Chadd 	/* XXX make this ATH_TXQ_FIRST */
935eb6f0de0SAdrian Chadd 	bf = TAILQ_FIRST(&txq->axq_q);
936b1f3262cSAdrian Chadd 	bf_last = ATH_TXQ_LAST(txq, axq_q_s);
937b1f3262cSAdrian Chadd 
938eb6f0de0SAdrian Chadd 	if (bf == NULL)
939eb6f0de0SAdrian Chadd 		return;
940eb6f0de0SAdrian Chadd 
9419be82a42SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET,
9429be82a42SAdrian Chadd 	    "%s: Q%d: bf=%p, bf_last=%p, daddr=0x%08x\n",
9439be82a42SAdrian Chadd 	    __func__,
9449be82a42SAdrian Chadd 	    txq->axq_qnum,
9459be82a42SAdrian Chadd 	    bf,
9469be82a42SAdrian Chadd 	    bf_last,
9479be82a42SAdrian Chadd 	    (uint32_t) bf->bf_daddr);
9489be82a42SAdrian Chadd 
9496112d22cSAdrian Chadd #ifdef	ATH_DEBUG
9509be82a42SAdrian Chadd 	if (sc->sc_debug & ATH_DEBUG_RESET)
9519be82a42SAdrian Chadd 		ath_tx_dump(sc, txq);
9526112d22cSAdrian Chadd #endif
9539be82a42SAdrian Chadd 
9549be82a42SAdrian Chadd 	/*
9559be82a42SAdrian Chadd 	 * This is called from a restart, so DMA is known to be
9569be82a42SAdrian Chadd 	 * completely stopped.
9579be82a42SAdrian Chadd 	 */
9589be82a42SAdrian Chadd 	KASSERT((!(txq->axq_flags & ATH_TXQ_PUTRUNNING)),
9599be82a42SAdrian Chadd 	    ("%s: Q%d: called with PUTRUNNING=1\n",
9609be82a42SAdrian Chadd 	    __func__,
9619be82a42SAdrian Chadd 	    txq->axq_qnum));
9629be82a42SAdrian Chadd 
9639be82a42SAdrian Chadd 	ath_hal_puttxbuf(sc->sc_ah, txq->axq_qnum, bf->bf_daddr);
9649be82a42SAdrian Chadd 	txq->axq_flags |= ATH_TXQ_PUTRUNNING;
9659be82a42SAdrian Chadd 
9666112d22cSAdrian Chadd 	ath_hal_gettxdesclinkptr(sc->sc_ah, bf_last->bf_lastds,
9676112d22cSAdrian Chadd 	    &txq->axq_link);
9686112d22cSAdrian Chadd 	ath_hal_txstart(sc->sc_ah, txq->axq_qnum);
969eb6f0de0SAdrian Chadd }
970eb6f0de0SAdrian Chadd 
971eb6f0de0SAdrian Chadd /*
972eb6f0de0SAdrian Chadd  * Hand off a packet to the hardware (or mcast queue.)
973eb6f0de0SAdrian Chadd  *
974eb6f0de0SAdrian Chadd  * The relevant hardware txq should be locked.
975eb6f0de0SAdrian Chadd  */
976eb6f0de0SAdrian Chadd static void
977746bab5bSAdrian Chadd ath_legacy_xmit_handoff(struct ath_softc *sc, struct ath_txq *txq,
978746bab5bSAdrian Chadd     struct ath_buf *bf)
979eb6f0de0SAdrian Chadd {
980375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
981eb6f0de0SAdrian Chadd 
982bb327d28SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
983bb327d28SAdrian Chadd 	if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_EDMA_TXDESC))
984bb327d28SAdrian Chadd 		ath_tx_alq_post(sc, bf);
985bb327d28SAdrian Chadd #endif
986bb327d28SAdrian Chadd 
987eb6f0de0SAdrian Chadd 	if (txq->axq_qnum == ATH_TXQ_SWQ)
988eb6f0de0SAdrian Chadd 		ath_tx_handoff_mcast(sc, txq, bf);
989eb6f0de0SAdrian Chadd 	else
990eb6f0de0SAdrian Chadd 		ath_tx_handoff_hw(sc, txq, bf);
991b8e788a5SAdrian Chadd }
992b8e788a5SAdrian Chadd 
99381a82688SAdrian Chadd static int
99481a82688SAdrian Chadd ath_tx_tag_crypto(struct ath_softc *sc, struct ieee80211_node *ni,
995d4365d16SAdrian Chadd     struct mbuf *m0, int iswep, int isfrag, int *hdrlen, int *pktlen,
996d4365d16SAdrian Chadd     int *keyix)
99781a82688SAdrian Chadd {
99812be5b9cSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_XMIT,
99912be5b9cSAdrian Chadd 	    "%s: hdrlen=%d, pktlen=%d, isfrag=%d, iswep=%d, m0=%p\n",
100012be5b9cSAdrian Chadd 	    __func__,
100112be5b9cSAdrian Chadd 	    *hdrlen,
100212be5b9cSAdrian Chadd 	    *pktlen,
100312be5b9cSAdrian Chadd 	    isfrag,
100412be5b9cSAdrian Chadd 	    iswep,
100512be5b9cSAdrian Chadd 	    m0);
100612be5b9cSAdrian Chadd 
100781a82688SAdrian Chadd 	if (iswep) {
100881a82688SAdrian Chadd 		const struct ieee80211_cipher *cip;
100981a82688SAdrian Chadd 		struct ieee80211_key *k;
101081a82688SAdrian Chadd 
101181a82688SAdrian Chadd 		/*
101281a82688SAdrian Chadd 		 * Construct the 802.11 header+trailer for an encrypted
101381a82688SAdrian Chadd 		 * frame. The only reason this can fail is because of an
101481a82688SAdrian Chadd 		 * unknown or unsupported cipher/key type.
101581a82688SAdrian Chadd 		 */
101681a82688SAdrian Chadd 		k = ieee80211_crypto_encap(ni, m0);
101781a82688SAdrian Chadd 		if (k == NULL) {
101881a82688SAdrian Chadd 			/*
101981a82688SAdrian Chadd 			 * This can happen when the key is yanked after the
102081a82688SAdrian Chadd 			 * frame was queued.  Just discard the frame; the
102181a82688SAdrian Chadd 			 * 802.11 layer counts failures and provides
102281a82688SAdrian Chadd 			 * debugging/diagnostics.
102381a82688SAdrian Chadd 			 */
1024d4365d16SAdrian Chadd 			return (0);
102581a82688SAdrian Chadd 		}
102681a82688SAdrian Chadd 		/*
102781a82688SAdrian Chadd 		 * Adjust the packet + header lengths for the crypto
102881a82688SAdrian Chadd 		 * additions and calculate the h/w key index.  When
102981a82688SAdrian Chadd 		 * a s/w mic is done the frame will have had any mic
103081a82688SAdrian Chadd 		 * added to it prior to entry so m0->m_pkthdr.len will
103181a82688SAdrian Chadd 		 * account for it. Otherwise we need to add it to the
103281a82688SAdrian Chadd 		 * packet length.
103381a82688SAdrian Chadd 		 */
103481a82688SAdrian Chadd 		cip = k->wk_cipher;
103581a82688SAdrian Chadd 		(*hdrlen) += cip->ic_header;
103681a82688SAdrian Chadd 		(*pktlen) += cip->ic_header + cip->ic_trailer;
103781a82688SAdrian Chadd 		/* NB: frags always have any TKIP MIC done in s/w */
103881a82688SAdrian Chadd 		if ((k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && !isfrag)
103981a82688SAdrian Chadd 			(*pktlen) += cip->ic_miclen;
104081a82688SAdrian Chadd 		(*keyix) = k->wk_keyix;
104181a82688SAdrian Chadd 	} else if (ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) {
104281a82688SAdrian Chadd 		/*
104381a82688SAdrian Chadd 		 * Use station key cache slot, if assigned.
104481a82688SAdrian Chadd 		 */
104581a82688SAdrian Chadd 		(*keyix) = ni->ni_ucastkey.wk_keyix;
104681a82688SAdrian Chadd 		if ((*keyix) == IEEE80211_KEYIX_NONE)
104781a82688SAdrian Chadd 			(*keyix) = HAL_TXKEYIX_INVALID;
104881a82688SAdrian Chadd 	} else
104981a82688SAdrian Chadd 		(*keyix) = HAL_TXKEYIX_INVALID;
105081a82688SAdrian Chadd 
1051d4365d16SAdrian Chadd 	return (1);
105281a82688SAdrian Chadd }
105381a82688SAdrian Chadd 
1054e2e4a2c2SAdrian Chadd /*
1055e2e4a2c2SAdrian Chadd  * Calculate whether interoperability protection is required for
1056e2e4a2c2SAdrian Chadd  * this frame.
1057e2e4a2c2SAdrian Chadd  *
1058e2e4a2c2SAdrian Chadd  * This requires the rate control information be filled in,
1059e2e4a2c2SAdrian Chadd  * as the protection requirement depends upon the current
1060e2e4a2c2SAdrian Chadd  * operating mode / PHY.
1061e2e4a2c2SAdrian Chadd  */
1062e2e4a2c2SAdrian Chadd static void
1063e2e4a2c2SAdrian Chadd ath_tx_calc_protection(struct ath_softc *sc, struct ath_buf *bf)
1064e2e4a2c2SAdrian Chadd {
1065e2e4a2c2SAdrian Chadd 	struct ieee80211_frame *wh;
1066e2e4a2c2SAdrian Chadd 	uint8_t rix;
1067e2e4a2c2SAdrian Chadd 	uint16_t flags;
1068e2e4a2c2SAdrian Chadd 	int shortPreamble;
1069e2e4a2c2SAdrian Chadd 	const HAL_RATE_TABLE *rt = sc->sc_currates;
1070e2e4a2c2SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
1071e2e4a2c2SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
1072e2e4a2c2SAdrian Chadd 
1073e2e4a2c2SAdrian Chadd 	flags = bf->bf_state.bfs_txflags;
1074e2e4a2c2SAdrian Chadd 	rix = bf->bf_state.bfs_rc[0].rix;
1075e2e4a2c2SAdrian Chadd 	shortPreamble = bf->bf_state.bfs_shpream;
1076e2e4a2c2SAdrian Chadd 	wh = mtod(bf->bf_m, struct ieee80211_frame *);
1077e2e4a2c2SAdrian Chadd 
1078e2e4a2c2SAdrian Chadd 	/*
1079e2e4a2c2SAdrian Chadd 	 * If 802.11g protection is enabled, determine whether
1080e2e4a2c2SAdrian Chadd 	 * to use RTS/CTS or just CTS.  Note that this is only
1081e2e4a2c2SAdrian Chadd 	 * done for OFDM unicast frames.
1082e2e4a2c2SAdrian Chadd 	 */
1083e2e4a2c2SAdrian Chadd 	if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1084e2e4a2c2SAdrian Chadd 	    rt->info[rix].phy == IEEE80211_T_OFDM &&
1085e2e4a2c2SAdrian Chadd 	    (flags & HAL_TXDESC_NOACK) == 0) {
1086e2e4a2c2SAdrian Chadd 		bf->bf_state.bfs_doprot = 1;
1087e2e4a2c2SAdrian Chadd 		/* XXX fragments must use CCK rates w/ protection */
1088e2e4a2c2SAdrian Chadd 		if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) {
1089e2e4a2c2SAdrian Chadd 			flags |= HAL_TXDESC_RTSENA;
1090e2e4a2c2SAdrian Chadd 		} else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) {
1091e2e4a2c2SAdrian Chadd 			flags |= HAL_TXDESC_CTSENA;
1092e2e4a2c2SAdrian Chadd 		}
1093e2e4a2c2SAdrian Chadd 		/*
1094e2e4a2c2SAdrian Chadd 		 * For frags it would be desirable to use the
1095e2e4a2c2SAdrian Chadd 		 * highest CCK rate for RTS/CTS.  But stations
1096e2e4a2c2SAdrian Chadd 		 * farther away may detect it at a lower CCK rate
1097e2e4a2c2SAdrian Chadd 		 * so use the configured protection rate instead
1098e2e4a2c2SAdrian Chadd 		 * (for now).
1099e2e4a2c2SAdrian Chadd 		 */
1100e2e4a2c2SAdrian Chadd 		sc->sc_stats.ast_tx_protect++;
1101e2e4a2c2SAdrian Chadd 	}
1102e2e4a2c2SAdrian Chadd 
1103e2e4a2c2SAdrian Chadd 	/*
1104e2e4a2c2SAdrian Chadd 	 * If 11n protection is enabled and it's a HT frame,
1105e2e4a2c2SAdrian Chadd 	 * enable RTS.
1106e2e4a2c2SAdrian Chadd 	 *
1107e2e4a2c2SAdrian Chadd 	 * XXX ic_htprotmode or ic_curhtprotmode?
1108e2e4a2c2SAdrian Chadd 	 * XXX should it_htprotmode only matter if ic_curhtprotmode
1109e2e4a2c2SAdrian Chadd 	 * XXX indicates it's not a HT pure environment?
1110e2e4a2c2SAdrian Chadd 	 */
1111e2e4a2c2SAdrian Chadd 	if ((ic->ic_htprotmode == IEEE80211_PROT_RTSCTS) &&
1112e2e4a2c2SAdrian Chadd 	    rt->info[rix].phy == IEEE80211_T_HT &&
1113e2e4a2c2SAdrian Chadd 	    (flags & HAL_TXDESC_NOACK) == 0) {
1114e2e4a2c2SAdrian Chadd 		flags |= HAL_TXDESC_RTSENA;
1115e2e4a2c2SAdrian Chadd 		sc->sc_stats.ast_tx_htprotect++;
1116e2e4a2c2SAdrian Chadd 	}
1117e2e4a2c2SAdrian Chadd 	bf->bf_state.bfs_txflags = flags;
1118e2e4a2c2SAdrian Chadd }
1119e2e4a2c2SAdrian Chadd 
1120e2e4a2c2SAdrian Chadd /*
1121e2e4a2c2SAdrian Chadd  * Update the frame duration given the currently selected rate.
1122e2e4a2c2SAdrian Chadd  *
1123e2e4a2c2SAdrian Chadd  * This also updates the frame duration value, so it will require
1124e2e4a2c2SAdrian Chadd  * a DMA flush.
1125e2e4a2c2SAdrian Chadd  */
1126e2e4a2c2SAdrian Chadd static void
1127e2e4a2c2SAdrian Chadd ath_tx_calc_duration(struct ath_softc *sc, struct ath_buf *bf)
1128e2e4a2c2SAdrian Chadd {
1129e2e4a2c2SAdrian Chadd 	struct ieee80211_frame *wh;
1130e2e4a2c2SAdrian Chadd 	uint8_t rix;
1131e2e4a2c2SAdrian Chadd 	uint16_t flags;
1132e2e4a2c2SAdrian Chadd 	int shortPreamble;
1133e2e4a2c2SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
1134e2e4a2c2SAdrian Chadd 	const HAL_RATE_TABLE *rt = sc->sc_currates;
1135e2e4a2c2SAdrian Chadd 	int isfrag = bf->bf_m->m_flags & M_FRAG;
1136e2e4a2c2SAdrian Chadd 
1137e2e4a2c2SAdrian Chadd 	flags = bf->bf_state.bfs_txflags;
1138e2e4a2c2SAdrian Chadd 	rix = bf->bf_state.bfs_rc[0].rix;
1139e2e4a2c2SAdrian Chadd 	shortPreamble = bf->bf_state.bfs_shpream;
1140e2e4a2c2SAdrian Chadd 	wh = mtod(bf->bf_m, struct ieee80211_frame *);
1141e2e4a2c2SAdrian Chadd 
1142e2e4a2c2SAdrian Chadd 	/*
1143e2e4a2c2SAdrian Chadd 	 * Calculate duration.  This logically belongs in the 802.11
1144e2e4a2c2SAdrian Chadd 	 * layer but it lacks sufficient information to calculate it.
1145e2e4a2c2SAdrian Chadd 	 */
1146e2e4a2c2SAdrian Chadd 	if ((flags & HAL_TXDESC_NOACK) == 0 &&
1147e2e4a2c2SAdrian Chadd 	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL) {
1148e2e4a2c2SAdrian Chadd 		u_int16_t dur;
1149e2e4a2c2SAdrian Chadd 		if (shortPreamble)
1150e2e4a2c2SAdrian Chadd 			dur = rt->info[rix].spAckDuration;
1151e2e4a2c2SAdrian Chadd 		else
1152e2e4a2c2SAdrian Chadd 			dur = rt->info[rix].lpAckDuration;
1153e2e4a2c2SAdrian Chadd 		if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) {
1154e2e4a2c2SAdrian Chadd 			dur += dur;		/* additional SIFS+ACK */
1155e2e4a2c2SAdrian Chadd 			/*
1156e2e4a2c2SAdrian Chadd 			 * Include the size of next fragment so NAV is
1157e2e4a2c2SAdrian Chadd 			 * updated properly.  The last fragment uses only
1158e2e4a2c2SAdrian Chadd 			 * the ACK duration
11599572684aSAdrian Chadd 			 *
11609572684aSAdrian Chadd 			 * XXX TODO: ensure that the rate lookup for each
11619572684aSAdrian Chadd 			 * fragment is the same as the rate used by the
11629572684aSAdrian Chadd 			 * first fragment!
1163e2e4a2c2SAdrian Chadd 			 */
1164cd7dffd0SAdrian Chadd 			dur += ath_hal_computetxtime(ah,
1165cd7dffd0SAdrian Chadd 			    rt,
1166cd7dffd0SAdrian Chadd 			    bf->bf_nextfraglen,
1167e2e4a2c2SAdrian Chadd 			    rix, shortPreamble);
1168e2e4a2c2SAdrian Chadd 		}
1169e2e4a2c2SAdrian Chadd 		if (isfrag) {
1170e2e4a2c2SAdrian Chadd 			/*
1171e2e4a2c2SAdrian Chadd 			 * Force hardware to use computed duration for next
1172e2e4a2c2SAdrian Chadd 			 * fragment by disabling multi-rate retry which updates
1173e2e4a2c2SAdrian Chadd 			 * duration based on the multi-rate duration table.
1174e2e4a2c2SAdrian Chadd 			 */
1175e2e4a2c2SAdrian Chadd 			bf->bf_state.bfs_ismrr = 0;
1176e2e4a2c2SAdrian Chadd 			bf->bf_state.bfs_try0 = ATH_TXMGTTRY;
1177e2e4a2c2SAdrian Chadd 			/* XXX update bfs_rc[0].try? */
1178e2e4a2c2SAdrian Chadd 		}
1179e2e4a2c2SAdrian Chadd 
1180e2e4a2c2SAdrian Chadd 		/* Update the duration field itself */
1181e2e4a2c2SAdrian Chadd 		*(u_int16_t *)wh->i_dur = htole16(dur);
1182e2e4a2c2SAdrian Chadd 	}
1183e2e4a2c2SAdrian Chadd }
1184e2e4a2c2SAdrian Chadd 
1185e42b5dbaSAdrian Chadd static uint8_t
1186e42b5dbaSAdrian Chadd ath_tx_get_rtscts_rate(struct ath_hal *ah, const HAL_RATE_TABLE *rt,
1187eb6f0de0SAdrian Chadd     int cix, int shortPreamble)
118879f02dbfSAdrian Chadd {
1189e42b5dbaSAdrian Chadd 	uint8_t ctsrate;
1190e42b5dbaSAdrian Chadd 
119179f02dbfSAdrian Chadd 	/*
119279f02dbfSAdrian Chadd 	 * CTS transmit rate is derived from the transmit rate
119379f02dbfSAdrian Chadd 	 * by looking in the h/w rate table.  We must also factor
119479f02dbfSAdrian Chadd 	 * in whether or not a short preamble is to be used.
119579f02dbfSAdrian Chadd 	 */
119679f02dbfSAdrian Chadd 	/* NB: cix is set above where RTS/CTS is enabled */
119779f02dbfSAdrian Chadd 	KASSERT(cix != 0xff, ("cix not setup"));
1198e42b5dbaSAdrian Chadd 	ctsrate = rt->info[cix].rateCode;
1199e42b5dbaSAdrian Chadd 
1200e42b5dbaSAdrian Chadd 	/* XXX this should only matter for legacy rates */
1201e42b5dbaSAdrian Chadd 	if (shortPreamble)
1202e42b5dbaSAdrian Chadd 		ctsrate |= rt->info[cix].shortPreamble;
1203e42b5dbaSAdrian Chadd 
1204d4365d16SAdrian Chadd 	return (ctsrate);
1205e42b5dbaSAdrian Chadd }
1206e42b5dbaSAdrian Chadd 
1207e42b5dbaSAdrian Chadd /*
1208e42b5dbaSAdrian Chadd  * Calculate the RTS/CTS duration for legacy frames.
1209e42b5dbaSAdrian Chadd  */
1210e42b5dbaSAdrian Chadd static int
1211e42b5dbaSAdrian Chadd ath_tx_calc_ctsduration(struct ath_hal *ah, int rix, int cix,
1212e42b5dbaSAdrian Chadd     int shortPreamble, int pktlen, const HAL_RATE_TABLE *rt,
1213e42b5dbaSAdrian Chadd     int flags)
1214e42b5dbaSAdrian Chadd {
1215e42b5dbaSAdrian Chadd 	int ctsduration = 0;
1216e42b5dbaSAdrian Chadd 
1217e42b5dbaSAdrian Chadd 	/* This mustn't be called for HT modes */
1218e42b5dbaSAdrian Chadd 	if (rt->info[cix].phy == IEEE80211_T_HT) {
1219e42b5dbaSAdrian Chadd 		printf("%s: HT rate where it shouldn't be (0x%x)\n",
1220e42b5dbaSAdrian Chadd 		    __func__, rt->info[cix].rateCode);
1221d4365d16SAdrian Chadd 		return (-1);
1222e42b5dbaSAdrian Chadd 	}
1223e42b5dbaSAdrian Chadd 
122479f02dbfSAdrian Chadd 	/*
122579f02dbfSAdrian Chadd 	 * Compute the transmit duration based on the frame
122679f02dbfSAdrian Chadd 	 * size and the size of an ACK frame.  We call into the
122779f02dbfSAdrian Chadd 	 * HAL to do the computation since it depends on the
122879f02dbfSAdrian Chadd 	 * characteristics of the actual PHY being used.
122979f02dbfSAdrian Chadd 	 *
123079f02dbfSAdrian Chadd 	 * NB: CTS is assumed the same size as an ACK so we can
123179f02dbfSAdrian Chadd 	 *     use the precalculated ACK durations.
123279f02dbfSAdrian Chadd 	 */
123379f02dbfSAdrian Chadd 	if (shortPreamble) {
123479f02dbfSAdrian Chadd 		if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
1235e42b5dbaSAdrian Chadd 			ctsduration += rt->info[cix].spAckDuration;
1236e42b5dbaSAdrian Chadd 		ctsduration += ath_hal_computetxtime(ah,
123779f02dbfSAdrian Chadd 			rt, pktlen, rix, AH_TRUE);
123879f02dbfSAdrian Chadd 		if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
1239e42b5dbaSAdrian Chadd 			ctsduration += rt->info[rix].spAckDuration;
124079f02dbfSAdrian Chadd 	} else {
124179f02dbfSAdrian Chadd 		if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
1242e42b5dbaSAdrian Chadd 			ctsduration += rt->info[cix].lpAckDuration;
1243e42b5dbaSAdrian Chadd 		ctsduration += ath_hal_computetxtime(ah,
124479f02dbfSAdrian Chadd 			rt, pktlen, rix, AH_FALSE);
124579f02dbfSAdrian Chadd 		if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
1246e42b5dbaSAdrian Chadd 			ctsduration += rt->info[rix].lpAckDuration;
124779f02dbfSAdrian Chadd 	}
1248e42b5dbaSAdrian Chadd 
1249d4365d16SAdrian Chadd 	return (ctsduration);
125079f02dbfSAdrian Chadd }
125179f02dbfSAdrian Chadd 
1252eb6f0de0SAdrian Chadd /*
1253eb6f0de0SAdrian Chadd  * Update the given ath_buf with updated rts/cts setup and duration
1254eb6f0de0SAdrian Chadd  * values.
1255eb6f0de0SAdrian Chadd  *
1256eb6f0de0SAdrian Chadd  * To support rate lookups for each software retry, the rts/cts rate
1257eb6f0de0SAdrian Chadd  * and cts duration must be re-calculated.
1258eb6f0de0SAdrian Chadd  *
1259eb6f0de0SAdrian Chadd  * This function assumes the RTS/CTS flags have been set as needed;
1260eb6f0de0SAdrian Chadd  * mrr has been disabled; and the rate control lookup has been done.
1261eb6f0de0SAdrian Chadd  *
1262eb6f0de0SAdrian Chadd  * XXX TODO: MRR need only be disabled for the pre-11n NICs.
1263eb6f0de0SAdrian Chadd  * XXX The 11n NICs support per-rate RTS/CTS configuration.
1264eb6f0de0SAdrian Chadd  */
1265eb6f0de0SAdrian Chadd static void
1266eb6f0de0SAdrian Chadd ath_tx_set_rtscts(struct ath_softc *sc, struct ath_buf *bf)
1267eb6f0de0SAdrian Chadd {
1268eb6f0de0SAdrian Chadd 	uint16_t ctsduration = 0;
1269eb6f0de0SAdrian Chadd 	uint8_t ctsrate = 0;
1270eb6f0de0SAdrian Chadd 	uint8_t rix = bf->bf_state.bfs_rc[0].rix;
1271eb6f0de0SAdrian Chadd 	uint8_t cix = 0;
1272eb6f0de0SAdrian Chadd 	const HAL_RATE_TABLE *rt = sc->sc_currates;
1273eb6f0de0SAdrian Chadd 
1274eb6f0de0SAdrian Chadd 	/*
1275eb6f0de0SAdrian Chadd 	 * No RTS/CTS enabled? Don't bother.
1276eb6f0de0SAdrian Chadd 	 */
1277875a9451SAdrian Chadd 	if ((bf->bf_state.bfs_txflags &
1278eb6f0de0SAdrian Chadd 	    (HAL_TXDESC_RTSENA | HAL_TXDESC_CTSENA)) == 0) {
1279eb6f0de0SAdrian Chadd 		/* XXX is this really needed? */
1280eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_ctsrate = 0;
1281eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_ctsduration = 0;
1282eb6f0de0SAdrian Chadd 		return;
1283eb6f0de0SAdrian Chadd 	}
1284eb6f0de0SAdrian Chadd 
1285eb6f0de0SAdrian Chadd 	/*
1286eb6f0de0SAdrian Chadd 	 * If protection is enabled, use the protection rix control
1287eb6f0de0SAdrian Chadd 	 * rate. Otherwise use the rate0 control rate.
1288eb6f0de0SAdrian Chadd 	 */
1289eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_doprot)
1290eb6f0de0SAdrian Chadd 		rix = sc->sc_protrix;
1291eb6f0de0SAdrian Chadd 	else
1292eb6f0de0SAdrian Chadd 		rix = bf->bf_state.bfs_rc[0].rix;
1293eb6f0de0SAdrian Chadd 
1294eb6f0de0SAdrian Chadd 	/*
1295eb6f0de0SAdrian Chadd 	 * If the raw path has hard-coded ctsrate0 to something,
1296eb6f0de0SAdrian Chadd 	 * use it.
1297eb6f0de0SAdrian Chadd 	 */
1298eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_ctsrate0 != 0)
1299eb6f0de0SAdrian Chadd 		cix = ath_tx_findrix(sc, bf->bf_state.bfs_ctsrate0);
1300eb6f0de0SAdrian Chadd 	else
1301eb6f0de0SAdrian Chadd 		/* Control rate from above */
1302eb6f0de0SAdrian Chadd 		cix = rt->info[rix].controlRate;
1303eb6f0de0SAdrian Chadd 
1304eb6f0de0SAdrian Chadd 	/* Calculate the rtscts rate for the given cix */
1305eb6f0de0SAdrian Chadd 	ctsrate = ath_tx_get_rtscts_rate(sc->sc_ah, rt, cix,
1306eb6f0de0SAdrian Chadd 	    bf->bf_state.bfs_shpream);
1307eb6f0de0SAdrian Chadd 
1308eb6f0de0SAdrian Chadd 	/* The 11n chipsets do ctsduration calculations for you */
1309eb6f0de0SAdrian Chadd 	if (! ath_tx_is_11n(sc))
1310eb6f0de0SAdrian Chadd 		ctsduration = ath_tx_calc_ctsduration(sc->sc_ah, rix, cix,
1311eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_shpream, bf->bf_state.bfs_pktlen,
1312875a9451SAdrian Chadd 		    rt, bf->bf_state.bfs_txflags);
1313eb6f0de0SAdrian Chadd 
1314eb6f0de0SAdrian Chadd 	/* Squirrel away in ath_buf */
1315eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsrate = ctsrate;
1316eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsduration = ctsduration;
1317eb6f0de0SAdrian Chadd 
1318eb6f0de0SAdrian Chadd 	/*
1319eb6f0de0SAdrian Chadd 	 * Must disable multi-rate retry when using RTS/CTS.
1320eb6f0de0SAdrian Chadd 	 */
1321af017101SAdrian Chadd 	if (!sc->sc_mrrprot) {
1322eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_ismrr = 0;
1323eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_try0 =
1324eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_rc[0].tries = ATH_TXMGTTRY; /* XXX ew */
1325eb6f0de0SAdrian Chadd 	}
1326af017101SAdrian Chadd }
1327eb6f0de0SAdrian Chadd 
1328eb6f0de0SAdrian Chadd /*
1329eb6f0de0SAdrian Chadd  * Setup the descriptor chain for a normal or fast-frame
1330eb6f0de0SAdrian Chadd  * frame.
133146634305SAdrian Chadd  *
133246634305SAdrian Chadd  * XXX TODO: extend to include the destination hardware QCU ID.
133346634305SAdrian Chadd  * Make sure that is correct.  Make sure that when being added
133446634305SAdrian Chadd  * to the mcastq, the CABQ QCUID is set or things will get a bit
133546634305SAdrian Chadd  * odd.
1336eb6f0de0SAdrian Chadd  */
1337eb6f0de0SAdrian Chadd static void
1338eb6f0de0SAdrian Chadd ath_tx_setds(struct ath_softc *sc, struct ath_buf *bf)
1339eb6f0de0SAdrian Chadd {
1340eb6f0de0SAdrian Chadd 	struct ath_desc *ds = bf->bf_desc;
1341eb6f0de0SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
1342eb6f0de0SAdrian Chadd 
13437d9dd2acSAdrian Chadd 	if (bf->bf_state.bfs_txrate0 == 0)
134483bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_XMIT,
134583bbd5ebSRui Paulo 		    "%s: bf=%p, txrate0=%d\n", __func__, bf, 0);
13467d9dd2acSAdrian Chadd 
1347eb6f0de0SAdrian Chadd 	ath_hal_setuptxdesc(ah, ds
1348eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_pktlen	/* packet length */
1349eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_hdrlen	/* header length */
1350eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_atype	/* Atheros packet type */
1351eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_txpower	/* txpower */
1352eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_txrate0
1353eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_try0		/* series 0 rate/tries */
1354eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_keyix	/* key cache index */
1355eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_txantenna	/* antenna mode */
1356875a9451SAdrian Chadd 		, bf->bf_state.bfs_txflags	/* flags */
1357eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_ctsrate	/* rts/cts rate */
1358eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_ctsduration	/* rts/cts duration */
1359eb6f0de0SAdrian Chadd 	);
1360eb6f0de0SAdrian Chadd 
1361eb6f0de0SAdrian Chadd 	/*
1362eb6f0de0SAdrian Chadd 	 * This will be overriden when the descriptor chain is written.
1363eb6f0de0SAdrian Chadd 	 */
1364eb6f0de0SAdrian Chadd 	bf->bf_lastds = ds;
1365eb6f0de0SAdrian Chadd 	bf->bf_last = bf;
1366eb6f0de0SAdrian Chadd 
1367d34a7347SAdrian Chadd 	/* Set rate control and descriptor chain for this frame */
1368d34a7347SAdrian Chadd 	ath_tx_set_ratectrl(sc, bf->bf_node, bf);
13696e84772fSAdrian Chadd 	ath_tx_chaindesclist(sc, ds, bf, 0, 0, 0);
1370eb6f0de0SAdrian Chadd }
1371eb6f0de0SAdrian Chadd 
1372eb6f0de0SAdrian Chadd /*
1373eb6f0de0SAdrian Chadd  * Do a rate lookup.
1374eb6f0de0SAdrian Chadd  *
1375eb6f0de0SAdrian Chadd  * This performs a rate lookup for the given ath_buf only if it's required.
1376eb6f0de0SAdrian Chadd  * Non-data frames and raw frames don't require it.
1377eb6f0de0SAdrian Chadd  *
1378eb6f0de0SAdrian Chadd  * This populates the primary and MRR entries; MRR values are
1379eb6f0de0SAdrian Chadd  * then disabled later on if something requires it (eg RTS/CTS on
1380eb6f0de0SAdrian Chadd  * pre-11n chipsets.
1381eb6f0de0SAdrian Chadd  *
1382eb6f0de0SAdrian Chadd  * This needs to be done before the RTS/CTS fields are calculated
1383eb6f0de0SAdrian Chadd  * as they may depend upon the rate chosen.
1384eb6f0de0SAdrian Chadd  */
1385eb6f0de0SAdrian Chadd static void
1386eb6f0de0SAdrian Chadd ath_tx_do_ratelookup(struct ath_softc *sc, struct ath_buf *bf)
1387eb6f0de0SAdrian Chadd {
1388eb6f0de0SAdrian Chadd 	uint8_t rate, rix;
1389eb6f0de0SAdrian Chadd 	int try0;
1390eb6f0de0SAdrian Chadd 
1391eb6f0de0SAdrian Chadd 	if (! bf->bf_state.bfs_doratelookup)
1392eb6f0de0SAdrian Chadd 		return;
1393eb6f0de0SAdrian Chadd 
1394eb6f0de0SAdrian Chadd 	/* Get rid of any previous state */
1395eb6f0de0SAdrian Chadd 	bzero(bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
1396eb6f0de0SAdrian Chadd 
1397eb6f0de0SAdrian Chadd 	ATH_NODE_LOCK(ATH_NODE(bf->bf_node));
1398eb6f0de0SAdrian Chadd 	ath_rate_findrate(sc, ATH_NODE(bf->bf_node), bf->bf_state.bfs_shpream,
1399eb6f0de0SAdrian Chadd 	    bf->bf_state.bfs_pktlen, &rix, &try0, &rate);
1400eb6f0de0SAdrian Chadd 
1401eb6f0de0SAdrian Chadd 	/* In case MRR is disabled, make sure rc[0] is setup correctly */
1402eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].rix = rix;
1403eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].ratecode = rate;
1404eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].tries = try0;
1405eb6f0de0SAdrian Chadd 
1406eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_ismrr && try0 != ATH_TXMAXTRY)
1407eb6f0de0SAdrian Chadd 		ath_rate_getxtxrates(sc, ATH_NODE(bf->bf_node), rix,
1408eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_rc);
1409eb6f0de0SAdrian Chadd 	ATH_NODE_UNLOCK(ATH_NODE(bf->bf_node));
1410eb6f0de0SAdrian Chadd 
1411eb6f0de0SAdrian Chadd 	sc->sc_txrix = rix;	/* for LED blinking */
1412eb6f0de0SAdrian Chadd 	sc->sc_lastdatarix = rix;	/* for fast frames */
1413eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_try0 = try0;
1414eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txrate0 = rate;
1415eb6f0de0SAdrian Chadd }
1416eb6f0de0SAdrian Chadd 
1417eb6f0de0SAdrian Chadd /*
14180c54de88SAdrian Chadd  * Update the CLRDMASK bit in the ath_buf if it needs to be set.
14190c54de88SAdrian Chadd  */
14200c54de88SAdrian Chadd static void
14210c54de88SAdrian Chadd ath_tx_update_clrdmask(struct ath_softc *sc, struct ath_tid *tid,
14220c54de88SAdrian Chadd     struct ath_buf *bf)
14230c54de88SAdrian Chadd {
14244f25ddbbSAdrian Chadd 	struct ath_node *an = ATH_NODE(bf->bf_node);
14250c54de88SAdrian Chadd 
1426375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
14270c54de88SAdrian Chadd 
14284f25ddbbSAdrian Chadd 	if (an->clrdmask == 1) {
14290c54de88SAdrian Chadd 		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
14304f25ddbbSAdrian Chadd 		an->clrdmask = 0;
14310c54de88SAdrian Chadd 	}
14320c54de88SAdrian Chadd }
14330c54de88SAdrian Chadd 
14340c54de88SAdrian Chadd /*
143522a3aee6SAdrian Chadd  * Return whether this frame should be software queued or
143622a3aee6SAdrian Chadd  * direct dispatched.
143722a3aee6SAdrian Chadd  *
143822a3aee6SAdrian Chadd  * When doing powersave, BAR frames should be queued but other management
143922a3aee6SAdrian Chadd  * frames should be directly sent.
144022a3aee6SAdrian Chadd  *
144122a3aee6SAdrian Chadd  * When not doing powersave, stick BAR frames into the hardware queue
144222a3aee6SAdrian Chadd  * so it goes out even though the queue is paused.
144322a3aee6SAdrian Chadd  *
144422a3aee6SAdrian Chadd  * For now, management frames are also software queued by default.
144522a3aee6SAdrian Chadd  */
144622a3aee6SAdrian Chadd static int
144722a3aee6SAdrian Chadd ath_tx_should_swq_frame(struct ath_softc *sc, struct ath_node *an,
144822a3aee6SAdrian Chadd     struct mbuf *m0, int *queue_to_head)
144922a3aee6SAdrian Chadd {
145022a3aee6SAdrian Chadd 	struct ieee80211_node *ni = &an->an_node;
145122a3aee6SAdrian Chadd 	struct ieee80211_frame *wh;
145222a3aee6SAdrian Chadd 	uint8_t type, subtype;
145322a3aee6SAdrian Chadd 
145422a3aee6SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
145522a3aee6SAdrian Chadd 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
145622a3aee6SAdrian Chadd 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
145722a3aee6SAdrian Chadd 
145822a3aee6SAdrian Chadd 	(*queue_to_head) = 0;
145922a3aee6SAdrian Chadd 
146022a3aee6SAdrian Chadd 	/* If it's not in powersave - direct-dispatch BAR */
146122a3aee6SAdrian Chadd 	if ((ATH_NODE(ni)->an_is_powersave == 0)
146222a3aee6SAdrian Chadd 	    && type == IEEE80211_FC0_TYPE_CTL &&
146322a3aee6SAdrian Chadd 	    subtype == IEEE80211_FC0_SUBTYPE_BAR) {
146422a3aee6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX,
146522a3aee6SAdrian Chadd 		    "%s: BAR: TX'ing direct\n", __func__);
146622a3aee6SAdrian Chadd 		return (0);
146722a3aee6SAdrian Chadd 	} else if ((ATH_NODE(ni)->an_is_powersave == 1)
146822a3aee6SAdrian Chadd 	    && type == IEEE80211_FC0_TYPE_CTL &&
146922a3aee6SAdrian Chadd 	    subtype == IEEE80211_FC0_SUBTYPE_BAR) {
147022a3aee6SAdrian Chadd 		/* BAR TX whilst asleep; queue */
147122a3aee6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX,
147222a3aee6SAdrian Chadd 		    "%s: swq: TX'ing\n", __func__);
147322a3aee6SAdrian Chadd 		(*queue_to_head) = 1;
147422a3aee6SAdrian Chadd 		return (1);
147522a3aee6SAdrian Chadd 	} else if ((ATH_NODE(ni)->an_is_powersave == 1)
147622a3aee6SAdrian Chadd 	    && (type == IEEE80211_FC0_TYPE_MGT ||
147722a3aee6SAdrian Chadd 	        type == IEEE80211_FC0_TYPE_CTL)) {
147822a3aee6SAdrian Chadd 		/*
147922a3aee6SAdrian Chadd 		 * Other control/mgmt frame; bypass software queuing
148022a3aee6SAdrian Chadd 		 * for now!
148122a3aee6SAdrian Chadd 		 */
148283bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_XMIT,
148322a3aee6SAdrian Chadd 		    "%s: %6D: Node is asleep; sending mgmt "
148422a3aee6SAdrian Chadd 		    "(type=%d, subtype=%d)\n",
148583bbd5ebSRui Paulo 		    __func__, ni->ni_macaddr, ":", type, subtype);
148622a3aee6SAdrian Chadd 		return (0);
148722a3aee6SAdrian Chadd 	} else {
148822a3aee6SAdrian Chadd 		return (1);
148922a3aee6SAdrian Chadd 	}
149022a3aee6SAdrian Chadd }
149122a3aee6SAdrian Chadd 
149222a3aee6SAdrian Chadd 
149322a3aee6SAdrian Chadd /*
1494eb6f0de0SAdrian Chadd  * Transmit the given frame to the hardware.
1495eb6f0de0SAdrian Chadd  *
1496eb6f0de0SAdrian Chadd  * The frame must already be setup; rate control must already have
1497eb6f0de0SAdrian Chadd  * been done.
1498eb6f0de0SAdrian Chadd  *
1499eb6f0de0SAdrian Chadd  * XXX since the TXQ lock is being held here (and I dislike holding
1500eb6f0de0SAdrian Chadd  * it for this long when not doing software aggregation), later on
1501eb6f0de0SAdrian Chadd  * break this function into "setup_normal" and "xmit_normal". The
1502eb6f0de0SAdrian Chadd  * lock only needs to be held for the ath_tx_handoff call.
150322a3aee6SAdrian Chadd  *
150422a3aee6SAdrian Chadd  * XXX we don't update the leak count here - if we're doing
150522a3aee6SAdrian Chadd  * direct frame dispatch, we need to be able to do it without
150622a3aee6SAdrian Chadd  * decrementing the leak count (eg multicast queue frames.)
1507eb6f0de0SAdrian Chadd  */
1508eb6f0de0SAdrian Chadd static void
1509eb6f0de0SAdrian Chadd ath_tx_xmit_normal(struct ath_softc *sc, struct ath_txq *txq,
1510eb6f0de0SAdrian Chadd     struct ath_buf *bf)
1511eb6f0de0SAdrian Chadd {
15120c54de88SAdrian Chadd 	struct ath_node *an = ATH_NODE(bf->bf_node);
15130c54de88SAdrian Chadd 	struct ath_tid *tid = &an->an_tid[bf->bf_state.bfs_tid];
1514eb6f0de0SAdrian Chadd 
1515375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
1516eb6f0de0SAdrian Chadd 
15170c54de88SAdrian Chadd 	/*
15180c54de88SAdrian Chadd 	 * For now, just enable CLRDMASK. ath_tx_xmit_normal() does
15190c54de88SAdrian Chadd 	 * set a completion handler however it doesn't (yet) properly
15200c54de88SAdrian Chadd 	 * handle the strict ordering requirements needed for normal,
15210c54de88SAdrian Chadd 	 * non-aggregate session frames.
15220c54de88SAdrian Chadd 	 *
15230c54de88SAdrian Chadd 	 * Once this is implemented, only set CLRDMASK like this for
15240c54de88SAdrian Chadd 	 * frames that must go out - eg management/raw frames.
15250c54de88SAdrian Chadd 	 */
15260c54de88SAdrian Chadd 	bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
15270c54de88SAdrian Chadd 
1528eb6f0de0SAdrian Chadd 	/* Setup the descriptor before handoff */
1529eb6f0de0SAdrian Chadd 	ath_tx_do_ratelookup(sc, bf);
1530e2e4a2c2SAdrian Chadd 	ath_tx_calc_duration(sc, bf);
1531e2e4a2c2SAdrian Chadd 	ath_tx_calc_protection(sc, bf);
1532eb6f0de0SAdrian Chadd 	ath_tx_set_rtscts(sc, bf);
1533e2e4a2c2SAdrian Chadd 	ath_tx_rate_fill_rcflags(sc, bf);
1534eb6f0de0SAdrian Chadd 	ath_tx_setds(sc, bf);
1535eb6f0de0SAdrian Chadd 
15360c54de88SAdrian Chadd 	/* Track per-TID hardware queue depth correctly */
15370c54de88SAdrian Chadd 	tid->hwq_depth++;
15380c54de88SAdrian Chadd 
15390c54de88SAdrian Chadd 	/* Assign the completion handler */
15400c54de88SAdrian Chadd 	bf->bf_comp = ath_tx_normal_comp;
15414e81f27cSAdrian Chadd 
1542eb6f0de0SAdrian Chadd 	/* Hand off to hardware */
1543eb6f0de0SAdrian Chadd 	ath_tx_handoff(sc, txq, bf);
1544eb6f0de0SAdrian Chadd }
1545eb6f0de0SAdrian Chadd 
1546d05b576dSAdrian Chadd /*
1547d05b576dSAdrian Chadd  * Do the basic frame setup stuff that's required before the frame
1548d05b576dSAdrian Chadd  * is added to a software queue.
1549d05b576dSAdrian Chadd  *
1550d05b576dSAdrian Chadd  * All frames get mostly the same treatment and it's done once.
1551d05b576dSAdrian Chadd  * Retransmits fiddle with things like the rate control setup,
1552d05b576dSAdrian Chadd  * setting the retransmit bit in the packet; doing relevant DMA/bus
1553d05b576dSAdrian Chadd  * syncing and relinking it (back) into the hardware TX queue.
1554d05b576dSAdrian Chadd  *
1555d05b576dSAdrian Chadd  * Note that this may cause the mbuf to be reallocated, so
1556d05b576dSAdrian Chadd  * m0 may not be valid.
1557d05b576dSAdrian Chadd  */
1558eb6f0de0SAdrian Chadd static int
1559eb6f0de0SAdrian Chadd ath_tx_normal_setup(struct ath_softc *sc, struct ieee80211_node *ni,
1560b43facbfSAdrian Chadd     struct ath_buf *bf, struct mbuf *m0, struct ath_txq *txq)
1561b8e788a5SAdrian Chadd {
1562b8e788a5SAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
1563b8e788a5SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
1564b8e788a5SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
1565b8e788a5SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
1566b8e788a5SAdrian Chadd 	const struct chanAccParams *cap = &ic->ic_wme.wme_chanParams;
1567b8e788a5SAdrian Chadd 	int error, iswep, ismcast, isfrag, ismrr;
1568eb6f0de0SAdrian Chadd 	int keyix, hdrlen, pktlen, try0 = 0;
1569eb6f0de0SAdrian Chadd 	u_int8_t rix = 0, txrate = 0;
1570b8e788a5SAdrian Chadd 	struct ath_desc *ds;
1571b8e788a5SAdrian Chadd 	struct ieee80211_frame *wh;
1572eb6f0de0SAdrian Chadd 	u_int subtype, flags;
1573b8e788a5SAdrian Chadd 	HAL_PKT_TYPE atype;
1574b8e788a5SAdrian Chadd 	const HAL_RATE_TABLE *rt;
1575b8e788a5SAdrian Chadd 	HAL_BOOL shortPreamble;
1576b8e788a5SAdrian Chadd 	struct ath_node *an;
1577b8e788a5SAdrian Chadd 	u_int pri;
1578b8e788a5SAdrian Chadd 
15797561cb5cSAdrian Chadd 	/*
15807561cb5cSAdrian Chadd 	 * To ensure that both sequence numbers and the CCMP PN handling
15817561cb5cSAdrian Chadd 	 * is "correct", make sure that the relevant TID queue is locked.
15827561cb5cSAdrian Chadd 	 * Otherwise the CCMP PN and seqno may appear out of order, causing
15837561cb5cSAdrian Chadd 	 * re-ordered frames to have out of order CCMP PN's, resulting
15847561cb5cSAdrian Chadd 	 * in many, many frame drops.
15857561cb5cSAdrian Chadd 	 */
1586375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
15877561cb5cSAdrian Chadd 
1588b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
15895945b5f5SKevin Lo 	iswep = wh->i_fc[1] & IEEE80211_FC1_PROTECTED;
1590b8e788a5SAdrian Chadd 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1591b8e788a5SAdrian Chadd 	isfrag = m0->m_flags & M_FRAG;
1592b8e788a5SAdrian Chadd 	hdrlen = ieee80211_anyhdrsize(wh);
1593b8e788a5SAdrian Chadd 	/*
1594b8e788a5SAdrian Chadd 	 * Packet length must not include any
1595b8e788a5SAdrian Chadd 	 * pad bytes; deduct them here.
1596b8e788a5SAdrian Chadd 	 */
1597b8e788a5SAdrian Chadd 	pktlen = m0->m_pkthdr.len - (hdrlen & 3);
1598b8e788a5SAdrian Chadd 
159981a82688SAdrian Chadd 	/* Handle encryption twiddling if needed */
1600eb6f0de0SAdrian Chadd 	if (! ath_tx_tag_crypto(sc, ni, m0, iswep, isfrag, &hdrlen,
1601eb6f0de0SAdrian Chadd 	    &pktlen, &keyix)) {
1602b8e788a5SAdrian Chadd 		ath_freetx(m0);
1603b8e788a5SAdrian Chadd 		return EIO;
1604b8e788a5SAdrian Chadd 	}
1605b8e788a5SAdrian Chadd 
1606b8e788a5SAdrian Chadd 	/* packet header may have moved, reset our local pointer */
1607b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1608b8e788a5SAdrian Chadd 
1609b8e788a5SAdrian Chadd 	pktlen += IEEE80211_CRC_LEN;
1610b8e788a5SAdrian Chadd 
1611b8e788a5SAdrian Chadd 	/*
1612b8e788a5SAdrian Chadd 	 * Load the DMA map so any coalescing is done.  This
1613b8e788a5SAdrian Chadd 	 * also calculates the number of descriptors we need.
1614b8e788a5SAdrian Chadd 	 */
1615b8e788a5SAdrian Chadd 	error = ath_tx_dmasetup(sc, bf, m0);
1616b8e788a5SAdrian Chadd 	if (error != 0)
1617b8e788a5SAdrian Chadd 		return error;
1618b8e788a5SAdrian Chadd 	bf->bf_node = ni;			/* NB: held reference */
1619b8e788a5SAdrian Chadd 	m0 = bf->bf_m;				/* NB: may have changed */
1620b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1621b8e788a5SAdrian Chadd 
1622b8e788a5SAdrian Chadd 	/* setup descriptors */
1623b8e788a5SAdrian Chadd 	ds = bf->bf_desc;
1624b8e788a5SAdrian Chadd 	rt = sc->sc_currates;
1625b8e788a5SAdrian Chadd 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
1626b8e788a5SAdrian Chadd 
1627b8e788a5SAdrian Chadd 	/*
1628b8e788a5SAdrian Chadd 	 * NB: the 802.11 layer marks whether or not we should
1629b8e788a5SAdrian Chadd 	 * use short preamble based on the current mode and
1630b8e788a5SAdrian Chadd 	 * negotiated parameters.
1631b8e788a5SAdrian Chadd 	 */
1632b8e788a5SAdrian Chadd 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1633b8e788a5SAdrian Chadd 	    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
1634b8e788a5SAdrian Chadd 		shortPreamble = AH_TRUE;
1635b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_shortpre++;
1636b8e788a5SAdrian Chadd 	} else {
1637b8e788a5SAdrian Chadd 		shortPreamble = AH_FALSE;
1638b8e788a5SAdrian Chadd 	}
1639b8e788a5SAdrian Chadd 
1640b8e788a5SAdrian Chadd 	an = ATH_NODE(ni);
16414e81f27cSAdrian Chadd 	//flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for crypto errs */
16424e81f27cSAdrian Chadd 	flags = 0;
1643b8e788a5SAdrian Chadd 	ismrr = 0;				/* default no multi-rate retry*/
1644b8e788a5SAdrian Chadd 	pri = M_WME_GETAC(m0);			/* honor classification */
1645b8e788a5SAdrian Chadd 	/* XXX use txparams instead of fixed values */
1646b8e788a5SAdrian Chadd 	/*
1647b8e788a5SAdrian Chadd 	 * Calculate Atheros packet type from IEEE80211 packet header,
1648b8e788a5SAdrian Chadd 	 * setup for rate calculations, and select h/w transmit queue.
1649b8e788a5SAdrian Chadd 	 */
1650b8e788a5SAdrian Chadd 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1651b8e788a5SAdrian Chadd 	case IEEE80211_FC0_TYPE_MGT:
1652b8e788a5SAdrian Chadd 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1653b8e788a5SAdrian Chadd 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON)
1654b8e788a5SAdrian Chadd 			atype = HAL_PKT_TYPE_BEACON;
1655b8e788a5SAdrian Chadd 		else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1656b8e788a5SAdrian Chadd 			atype = HAL_PKT_TYPE_PROBE_RESP;
1657b8e788a5SAdrian Chadd 		else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM)
1658b8e788a5SAdrian Chadd 			atype = HAL_PKT_TYPE_ATIM;
1659b8e788a5SAdrian Chadd 		else
1660b8e788a5SAdrian Chadd 			atype = HAL_PKT_TYPE_NORMAL;	/* XXX */
1661b8e788a5SAdrian Chadd 		rix = an->an_mgmtrix;
1662b8e788a5SAdrian Chadd 		txrate = rt->info[rix].rateCode;
1663b8e788a5SAdrian Chadd 		if (shortPreamble)
1664b8e788a5SAdrian Chadd 			txrate |= rt->info[rix].shortPreamble;
1665b8e788a5SAdrian Chadd 		try0 = ATH_TXMGTTRY;
1666b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_INTREQ;	/* force interrupt */
1667b8e788a5SAdrian Chadd 		break;
1668b8e788a5SAdrian Chadd 	case IEEE80211_FC0_TYPE_CTL:
1669b8e788a5SAdrian Chadd 		atype = HAL_PKT_TYPE_PSPOLL;	/* stop setting of duration */
1670b8e788a5SAdrian Chadd 		rix = an->an_mgmtrix;
1671b8e788a5SAdrian Chadd 		txrate = rt->info[rix].rateCode;
1672b8e788a5SAdrian Chadd 		if (shortPreamble)
1673b8e788a5SAdrian Chadd 			txrate |= rt->info[rix].shortPreamble;
1674b8e788a5SAdrian Chadd 		try0 = ATH_TXMGTTRY;
1675b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_INTREQ;	/* force interrupt */
1676b8e788a5SAdrian Chadd 		break;
1677b8e788a5SAdrian Chadd 	case IEEE80211_FC0_TYPE_DATA:
1678b8e788a5SAdrian Chadd 		atype = HAL_PKT_TYPE_NORMAL;		/* default */
1679b8e788a5SAdrian Chadd 		/*
1680b8e788a5SAdrian Chadd 		 * Data frames: multicast frames go out at a fixed rate,
1681b8e788a5SAdrian Chadd 		 * EAPOL frames use the mgmt frame rate; otherwise consult
1682b8e788a5SAdrian Chadd 		 * the rate control module for the rate to use.
1683b8e788a5SAdrian Chadd 		 */
1684b8e788a5SAdrian Chadd 		if (ismcast) {
1685b8e788a5SAdrian Chadd 			rix = an->an_mcastrix;
1686b8e788a5SAdrian Chadd 			txrate = rt->info[rix].rateCode;
1687b8e788a5SAdrian Chadd 			if (shortPreamble)
1688b8e788a5SAdrian Chadd 				txrate |= rt->info[rix].shortPreamble;
1689b8e788a5SAdrian Chadd 			try0 = 1;
1690b8e788a5SAdrian Chadd 		} else if (m0->m_flags & M_EAPOL) {
1691b8e788a5SAdrian Chadd 			/* XXX? maybe always use long preamble? */
1692b8e788a5SAdrian Chadd 			rix = an->an_mgmtrix;
1693b8e788a5SAdrian Chadd 			txrate = rt->info[rix].rateCode;
1694b8e788a5SAdrian Chadd 			if (shortPreamble)
1695b8e788a5SAdrian Chadd 				txrate |= rt->info[rix].shortPreamble;
1696b8e788a5SAdrian Chadd 			try0 = ATH_TXMAXTRY;	/* XXX?too many? */
1697b8e788a5SAdrian Chadd 		} else {
1698eb6f0de0SAdrian Chadd 			/*
1699eb6f0de0SAdrian Chadd 			 * Do rate lookup on each TX, rather than using
1700eb6f0de0SAdrian Chadd 			 * the hard-coded TX information decided here.
1701eb6f0de0SAdrian Chadd 			 */
1702b8e788a5SAdrian Chadd 			ismrr = 1;
1703eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_doratelookup = 1;
1704b8e788a5SAdrian Chadd 		}
1705b8e788a5SAdrian Chadd 		if (cap->cap_wmeParams[pri].wmep_noackPolicy)
1706b8e788a5SAdrian Chadd 			flags |= HAL_TXDESC_NOACK;
1707b8e788a5SAdrian Chadd 		break;
1708b8e788a5SAdrian Chadd 	default:
1709b8e788a5SAdrian Chadd 		if_printf(ifp, "bogus frame type 0x%x (%s)\n",
1710b8e788a5SAdrian Chadd 			wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__);
1711b8e788a5SAdrian Chadd 		/* XXX statistic */
1712c23a9d98SAdrian Chadd 		/* XXX free tx dmamap */
1713b8e788a5SAdrian Chadd 		ath_freetx(m0);
1714b8e788a5SAdrian Chadd 		return EIO;
1715b8e788a5SAdrian Chadd 	}
1716b8e788a5SAdrian Chadd 
1717447fd44aSAdrian Chadd 	/*
1718447fd44aSAdrian Chadd 	 * There are two known scenarios where the frame AC doesn't match
1719447fd44aSAdrian Chadd 	 * what the destination TXQ is.
1720447fd44aSAdrian Chadd 	 *
1721447fd44aSAdrian Chadd 	 * + non-QoS frames (eg management?) that the net80211 stack has
1722447fd44aSAdrian Chadd 	 *   assigned a higher AC to, but since it's a non-QoS TID, it's
1723447fd44aSAdrian Chadd 	 *   being thrown into TID 16.  TID 16 gets the AC_BE queue.
1724447fd44aSAdrian Chadd 	 *   It's quite possible that management frames should just be
1725447fd44aSAdrian Chadd 	 *   direct dispatched to hardware rather than go via the software
1726447fd44aSAdrian Chadd 	 *   queue; that should be investigated in the future.  There are
1727447fd44aSAdrian Chadd 	 *   some specific scenarios where this doesn't make sense, mostly
1728447fd44aSAdrian Chadd 	 *   surrounding ADDBA request/response - hence why that is special
1729447fd44aSAdrian Chadd 	 *   cased.
1730447fd44aSAdrian Chadd 	 *
1731447fd44aSAdrian Chadd 	 * + Multicast frames going into the VAP mcast queue.  That shows up
1732447fd44aSAdrian Chadd 	 *   as "TXQ 11".
1733447fd44aSAdrian Chadd 	 *
1734447fd44aSAdrian Chadd 	 * This driver should eventually support separate TID and TXQ locking,
1735447fd44aSAdrian Chadd 	 * allowing for arbitrary AC frames to appear on arbitrary software
1736447fd44aSAdrian Chadd 	 * queues, being queued to the "correct" hardware queue when needed.
1737447fd44aSAdrian Chadd 	 */
1738447fd44aSAdrian Chadd #if 0
17396deb7f32SAdrian Chadd 	if (txq != sc->sc_ac2q[pri]) {
174083bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_XMIT,
17416deb7f32SAdrian Chadd 		    "%s: txq=%p (%d), pri=%d, pri txq=%p (%d)\n",
17426deb7f32SAdrian Chadd 		    __func__,
17436deb7f32SAdrian Chadd 		    txq,
17446deb7f32SAdrian Chadd 		    txq->axq_qnum,
17456deb7f32SAdrian Chadd 		    pri,
17466deb7f32SAdrian Chadd 		    sc->sc_ac2q[pri],
17476deb7f32SAdrian Chadd 		    sc->sc_ac2q[pri]->axq_qnum);
17486deb7f32SAdrian Chadd 	}
1749447fd44aSAdrian Chadd #endif
17506deb7f32SAdrian Chadd 
1751b8e788a5SAdrian Chadd 	/*
1752b8e788a5SAdrian Chadd 	 * Calculate miscellaneous flags.
1753b8e788a5SAdrian Chadd 	 */
1754b8e788a5SAdrian Chadd 	if (ismcast) {
1755b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_NOACK;	/* no ack on broad/multicast */
1756b8e788a5SAdrian Chadd 	} else if (pktlen > vap->iv_rtsthreshold &&
1757b8e788a5SAdrian Chadd 	    (ni->ni_ath_flags & IEEE80211_NODE_FF) == 0) {
1758b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_RTSENA;	/* RTS based on frame length */
1759b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_rts++;
1760b8e788a5SAdrian Chadd 	}
1761b8e788a5SAdrian Chadd 	if (flags & HAL_TXDESC_NOACK)		/* NB: avoid double counting */
1762b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_noack++;
1763b8e788a5SAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
1764b8e788a5SAdrian Chadd 	if (sc->sc_tdma && (flags & HAL_TXDESC_NOACK) == 0) {
1765b8e788a5SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_TDMA,
1766b8e788a5SAdrian Chadd 		    "%s: discard frame, ACK required w/ TDMA\n", __func__);
1767b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tdma_ack++;
1768c23a9d98SAdrian Chadd 		/* XXX free tx dmamap */
1769b8e788a5SAdrian Chadd 		ath_freetx(m0);
1770b8e788a5SAdrian Chadd 		return EIO;
1771b8e788a5SAdrian Chadd 	}
1772b8e788a5SAdrian Chadd #endif
1773b8e788a5SAdrian Chadd 
1774b8e788a5SAdrian Chadd 	/*
1775eb6f0de0SAdrian Chadd 	 * Determine if a tx interrupt should be generated for
1776eb6f0de0SAdrian Chadd 	 * this descriptor.  We take a tx interrupt to reap
1777eb6f0de0SAdrian Chadd 	 * descriptors when the h/w hits an EOL condition or
1778eb6f0de0SAdrian Chadd 	 * when the descriptor is specifically marked to generate
1779eb6f0de0SAdrian Chadd 	 * an interrupt.  We periodically mark descriptors in this
1780eb6f0de0SAdrian Chadd 	 * way to insure timely replenishing of the supply needed
1781eb6f0de0SAdrian Chadd 	 * for sending frames.  Defering interrupts reduces system
1782eb6f0de0SAdrian Chadd 	 * load and potentially allows more concurrent work to be
1783eb6f0de0SAdrian Chadd 	 * done but if done to aggressively can cause senders to
1784eb6f0de0SAdrian Chadd 	 * backup.
1785eb6f0de0SAdrian Chadd 	 *
1786eb6f0de0SAdrian Chadd 	 * NB: use >= to deal with sc_txintrperiod changing
1787eb6f0de0SAdrian Chadd 	 *     dynamically through sysctl.
1788b8e788a5SAdrian Chadd 	 */
1789eb6f0de0SAdrian Chadd 	if (flags & HAL_TXDESC_INTREQ) {
1790eb6f0de0SAdrian Chadd 		txq->axq_intrcnt = 0;
1791eb6f0de0SAdrian Chadd 	} else if (++txq->axq_intrcnt >= sc->sc_txintrperiod) {
1792eb6f0de0SAdrian Chadd 		flags |= HAL_TXDESC_INTREQ;
1793eb6f0de0SAdrian Chadd 		txq->axq_intrcnt = 0;
1794eb6f0de0SAdrian Chadd 	}
1795e42b5dbaSAdrian Chadd 
1796eb6f0de0SAdrian Chadd 	/* This point forward is actual TX bits */
1797b8e788a5SAdrian Chadd 
1798b8e788a5SAdrian Chadd 	/*
1799b8e788a5SAdrian Chadd 	 * At this point we are committed to sending the frame
1800b8e788a5SAdrian Chadd 	 * and we don't need to look at m_nextpkt; clear it in
1801b8e788a5SAdrian Chadd 	 * case this frame is part of frag chain.
1802b8e788a5SAdrian Chadd 	 */
1803b8e788a5SAdrian Chadd 	m0->m_nextpkt = NULL;
1804b8e788a5SAdrian Chadd 
1805b8e788a5SAdrian Chadd 	if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
1806b8e788a5SAdrian Chadd 		ieee80211_dump_pkt(ic, mtod(m0, const uint8_t *), m0->m_len,
1807b8e788a5SAdrian Chadd 		    sc->sc_hwmap[rix].ieeerate, -1);
1808b8e788a5SAdrian Chadd 
1809b8e788a5SAdrian Chadd 	if (ieee80211_radiotap_active_vap(vap)) {
1810b8e788a5SAdrian Chadd 		u_int64_t tsf = ath_hal_gettsf64(ah);
1811b8e788a5SAdrian Chadd 
1812b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_tsf = htole64(tsf);
1813b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
1814b8e788a5SAdrian Chadd 		if (iswep)
1815b8e788a5SAdrian Chadd 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
1816b8e788a5SAdrian Chadd 		if (isfrag)
1817b8e788a5SAdrian Chadd 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
1818b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
181912087a07SAdrian Chadd 		sc->sc_tx_th.wt_txpower = ieee80211_get_node_txpower(ni);
1820b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
1821b8e788a5SAdrian Chadd 
1822b8e788a5SAdrian Chadd 		ieee80211_radiotap_tx(vap, m0);
1823b8e788a5SAdrian Chadd 	}
1824b8e788a5SAdrian Chadd 
1825eb6f0de0SAdrian Chadd 	/* Blank the legacy rate array */
1826eb6f0de0SAdrian Chadd 	bzero(&bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
1827c1782ce0SAdrian Chadd 
1828b8e788a5SAdrian Chadd 	/*
1829eb6f0de0SAdrian Chadd 	 * ath_buf_set_rate needs at least one rate/try to setup
1830eb6f0de0SAdrian Chadd 	 * the rate scenario.
1831b8e788a5SAdrian Chadd 	 */
1832eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].rix = rix;
1833eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].tries = try0;
1834eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].ratecode = txrate;
1835eb6f0de0SAdrian Chadd 
1836eb6f0de0SAdrian Chadd 	/* Store the decided rate index values away */
1837eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_pktlen = pktlen;
1838eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_hdrlen = hdrlen;
1839eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_atype = atype;
184012087a07SAdrian Chadd 	bf->bf_state.bfs_txpower = ieee80211_get_node_txpower(ni);
1841eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txrate0 = txrate;
1842eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_try0 = try0;
1843eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_keyix = keyix;
1844eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txantenna = sc->sc_txantenna;
1845875a9451SAdrian Chadd 	bf->bf_state.bfs_txflags = flags;
1846eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_shpream = shortPreamble;
1847eb6f0de0SAdrian Chadd 
1848eb6f0de0SAdrian Chadd 	/* XXX this should be done in ath_tx_setrate() */
1849eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsrate0 = 0;	/* ie, no hard-coded ctsrate */
1850eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsrate = 0;	/* calculated later */
1851eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsduration = 0;
1852eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ismrr = ismrr;
1853eb6f0de0SAdrian Chadd 
1854eb6f0de0SAdrian Chadd 	return 0;
1855eb6f0de0SAdrian Chadd }
1856eb6f0de0SAdrian Chadd 
1857b8e788a5SAdrian Chadd /*
18584e81f27cSAdrian Chadd  * Queue a frame to the hardware or software queue.
1859eb6f0de0SAdrian Chadd  *
1860eb6f0de0SAdrian Chadd  * This can be called by the net80211 code.
1861eb6f0de0SAdrian Chadd  *
1862eb6f0de0SAdrian Chadd  * XXX what about locking? Or, push the seqno assign into the
1863eb6f0de0SAdrian Chadd  * XXX aggregate scheduler so its serialised?
18644e81f27cSAdrian Chadd  *
18654e81f27cSAdrian Chadd  * XXX When sending management frames via ath_raw_xmit(),
18664e81f27cSAdrian Chadd  *     should CLRDMASK be set unconditionally?
1867b8e788a5SAdrian Chadd  */
1868eb6f0de0SAdrian Chadd int
1869eb6f0de0SAdrian Chadd ath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni,
1870eb6f0de0SAdrian Chadd     struct ath_buf *bf, struct mbuf *m0)
1871eb6f0de0SAdrian Chadd {
1872eb6f0de0SAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
1873eb6f0de0SAdrian Chadd 	struct ath_vap *avp = ATH_VAP(vap);
18749c85ff91SAdrian Chadd 	int r = 0;
1875eb6f0de0SAdrian Chadd 	u_int pri;
1876eb6f0de0SAdrian Chadd 	int tid;
1877eb6f0de0SAdrian Chadd 	struct ath_txq *txq;
1878eb6f0de0SAdrian Chadd 	int ismcast;
1879eb6f0de0SAdrian Chadd 	const struct ieee80211_frame *wh;
1880eb6f0de0SAdrian Chadd 	int is_ampdu, is_ampdu_tx, is_ampdu_pending;
1881a108d2d6SAdrian Chadd 	ieee80211_seq seqno;
1882eb6f0de0SAdrian Chadd 	uint8_t type, subtype;
188322a3aee6SAdrian Chadd 	int queue_to_head;
1884eb6f0de0SAdrian Chadd 
1885375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
1886375307d4SAdrian Chadd 
1887eb6f0de0SAdrian Chadd 	/*
1888eb6f0de0SAdrian Chadd 	 * Determine the target hardware queue.
1889eb6f0de0SAdrian Chadd 	 *
1890b43facbfSAdrian Chadd 	 * For multicast frames, the txq gets overridden appropriately
1891b43facbfSAdrian Chadd 	 * depending upon the state of PS.
1892eb6f0de0SAdrian Chadd 	 *
1893eb6f0de0SAdrian Chadd 	 * For any other frame, we do a TID/QoS lookup inside the frame
1894eb6f0de0SAdrian Chadd 	 * to see what the TID should be. If it's a non-QoS frame, the
1895eb6f0de0SAdrian Chadd 	 * AC and TID are overridden. The TID/TXQ code assumes the
1896eb6f0de0SAdrian Chadd 	 * TID is on a predictable hardware TXQ, so we don't support
1897eb6f0de0SAdrian Chadd 	 * having a node TID queued to multiple hardware TXQs.
1898eb6f0de0SAdrian Chadd 	 * This may change in the future but would require some locking
1899eb6f0de0SAdrian Chadd 	 * fudgery.
1900eb6f0de0SAdrian Chadd 	 */
1901eb6f0de0SAdrian Chadd 	pri = ath_tx_getac(sc, m0);
1902eb6f0de0SAdrian Chadd 	tid = ath_tx_gettid(sc, m0);
1903eb6f0de0SAdrian Chadd 
1904eb6f0de0SAdrian Chadd 	txq = sc->sc_ac2q[pri];
1905eb6f0de0SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1906eb6f0de0SAdrian Chadd 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1907eb6f0de0SAdrian Chadd 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1908eb6f0de0SAdrian Chadd 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1909eb6f0de0SAdrian Chadd 
19109c85ff91SAdrian Chadd 	/*
19119c85ff91SAdrian Chadd 	 * Enforce how deep the multicast queue can grow.
19129c85ff91SAdrian Chadd 	 *
19139c85ff91SAdrian Chadd 	 * XXX duplicated in ath_raw_xmit().
19149c85ff91SAdrian Chadd 	 */
19159c85ff91SAdrian Chadd 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
191692e84e43SAdrian Chadd 		if (sc->sc_cabq->axq_depth + sc->sc_cabq->fifo.axq_depth
191792e84e43SAdrian Chadd 		    > sc->sc_txq_mcastq_maxdepth) {
19189c85ff91SAdrian Chadd 			sc->sc_stats.ast_tx_mcastq_overflow++;
19199c85ff91SAdrian Chadd 			m_freem(m0);
192055cf0326SAdrian Chadd 			return (ENOBUFS);
19219c85ff91SAdrian Chadd 		}
19229c85ff91SAdrian Chadd 	}
19239c85ff91SAdrian Chadd 
192422a3aee6SAdrian Chadd 	/*
192522a3aee6SAdrian Chadd 	 * Enforce how deep the unicast queue can grow.
192622a3aee6SAdrian Chadd 	 *
192722a3aee6SAdrian Chadd 	 * If the node is in power save then we don't want
192822a3aee6SAdrian Chadd 	 * the software queue to grow too deep, or a node may
192922a3aee6SAdrian Chadd 	 * end up consuming all of the ath_buf entries.
193022a3aee6SAdrian Chadd 	 *
193122a3aee6SAdrian Chadd 	 * For now, only do this for DATA frames.
193222a3aee6SAdrian Chadd 	 *
193322a3aee6SAdrian Chadd 	 * We will want to cap how many management/control
193422a3aee6SAdrian Chadd 	 * frames get punted to the software queue so it doesn't
193522a3aee6SAdrian Chadd 	 * fill up.  But the correct solution isn't yet obvious.
193622a3aee6SAdrian Chadd 	 * In any case, this check should at least let frames pass
193722a3aee6SAdrian Chadd 	 * that we are direct-dispatching.
193822a3aee6SAdrian Chadd 	 *
193922a3aee6SAdrian Chadd 	 * XXX TODO: duplicate this to the raw xmit path!
194022a3aee6SAdrian Chadd 	 */
194122a3aee6SAdrian Chadd 	if (type == IEEE80211_FC0_TYPE_DATA &&
194222a3aee6SAdrian Chadd 	    ATH_NODE(ni)->an_is_powersave &&
194322a3aee6SAdrian Chadd 	    ATH_NODE(ni)->an_swq_depth >
194422a3aee6SAdrian Chadd 	     sc->sc_txq_node_psq_maxdepth) {
194522a3aee6SAdrian Chadd 		sc->sc_stats.ast_tx_node_psq_overflow++;
194622a3aee6SAdrian Chadd 		m_freem(m0);
194722a3aee6SAdrian Chadd 		return (ENOBUFS);
194822a3aee6SAdrian Chadd 	}
194922a3aee6SAdrian Chadd 
1950eb6f0de0SAdrian Chadd 	/* A-MPDU TX */
1951eb6f0de0SAdrian Chadd 	is_ampdu_tx = ath_tx_ampdu_running(sc, ATH_NODE(ni), tid);
1952eb6f0de0SAdrian Chadd 	is_ampdu_pending = ath_tx_ampdu_pending(sc, ATH_NODE(ni), tid);
1953eb6f0de0SAdrian Chadd 	is_ampdu = is_ampdu_tx | is_ampdu_pending;
1954eb6f0de0SAdrian Chadd 
1955a108d2d6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d, ac=%d, is_ampdu=%d\n",
1956a108d2d6SAdrian Chadd 	    __func__, tid, pri, is_ampdu);
1957eb6f0de0SAdrian Chadd 
195846634305SAdrian Chadd 	/* Set local packet state, used to queue packets to hardware */
195946634305SAdrian Chadd 	bf->bf_state.bfs_tid = tid;
1960fc56c9c5SAdrian Chadd 	bf->bf_state.bfs_tx_queue = txq->axq_qnum;
196146634305SAdrian Chadd 	bf->bf_state.bfs_pri = pri;
196246634305SAdrian Chadd 
1963b837332dSAdrian Chadd #if 1
1964c5940c30SAdrian Chadd 	/*
1965b43facbfSAdrian Chadd 	 * When servicing one or more stations in power-save mode
1966b43facbfSAdrian Chadd 	 * (or) if there is some mcast data waiting on the mcast
1967b43facbfSAdrian Chadd 	 * queue (to prevent out of order delivery) multicast frames
1968b43facbfSAdrian Chadd 	 * must be bufferd until after the beacon.
1969b43facbfSAdrian Chadd 	 *
1970b43facbfSAdrian Chadd 	 * TODO: we should lock the mcastq before we check the length.
1971c5940c30SAdrian Chadd 	 */
1972b837332dSAdrian Chadd 	if (sc->sc_cabq_enable && ismcast && (vap->iv_ps_sta || avp->av_mcastq.axq_depth)) {
1973eb6f0de0SAdrian Chadd 		txq = &avp->av_mcastq;
197446634305SAdrian Chadd 		/*
197546634305SAdrian Chadd 		 * Mark the frame as eventually belonging on the CAB
197646634305SAdrian Chadd 		 * queue, so the descriptor setup functions will
197746634305SAdrian Chadd 		 * correctly initialise the descriptor 'qcuId' field.
197846634305SAdrian Chadd 		 */
1979fc56c9c5SAdrian Chadd 		bf->bf_state.bfs_tx_queue = sc->sc_cabq->axq_qnum;
198046634305SAdrian Chadd 	}
1981b837332dSAdrian Chadd #endif
1982eb6f0de0SAdrian Chadd 
1983eb6f0de0SAdrian Chadd 	/* Do the generic frame setup */
1984eb6f0de0SAdrian Chadd 	/* XXX should just bzero the bf_state? */
1985eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_dobaw = 0;
1986eb6f0de0SAdrian Chadd 
19877561cb5cSAdrian Chadd 	/* A-MPDU TX? Manually set sequence number */
19887561cb5cSAdrian Chadd 	/*
19897561cb5cSAdrian Chadd 	 * Don't do it whilst pending; the net80211 layer still
19907561cb5cSAdrian Chadd 	 * assigns them.
19917561cb5cSAdrian Chadd 	 */
19927561cb5cSAdrian Chadd 	if (is_ampdu_tx) {
1993eb6f0de0SAdrian Chadd 		/*
1994eb6f0de0SAdrian Chadd 		 * Always call; this function will
1995eb6f0de0SAdrian Chadd 		 * handle making sure that null data frames
1996eb6f0de0SAdrian Chadd 		 * don't get a sequence number from the current
1997eb6f0de0SAdrian Chadd 		 * TID and thus mess with the BAW.
1998eb6f0de0SAdrian Chadd 		 */
1999a108d2d6SAdrian Chadd 		seqno = ath_tx_tid_seqno_assign(sc, ni, bf, m0);
200042f4d061SAdrian Chadd 
200142f4d061SAdrian Chadd 		/*
200242f4d061SAdrian Chadd 		 * Don't add QoS NULL frames to the BAW.
200342f4d061SAdrian Chadd 		 */
2004a108d2d6SAdrian Chadd 		if (IEEE80211_QOS_HAS_SEQ(wh) &&
2005a108d2d6SAdrian Chadd 		    subtype != IEEE80211_FC0_SUBTYPE_QOS_NULL) {
2006eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_dobaw = 1;
2007eb6f0de0SAdrian Chadd 		}
2008c1782ce0SAdrian Chadd 	}
2009c1782ce0SAdrian Chadd 
2010eb6f0de0SAdrian Chadd 	/*
2011eb6f0de0SAdrian Chadd 	 * If needed, the sequence number has been assigned.
2012eb6f0de0SAdrian Chadd 	 * Squirrel it away somewhere easy to get to.
2013eb6f0de0SAdrian Chadd 	 */
2014a108d2d6SAdrian Chadd 	bf->bf_state.bfs_seqno = M_SEQNO_GET(m0) << IEEE80211_SEQ_SEQ_SHIFT;
2015b8e788a5SAdrian Chadd 
2016eb6f0de0SAdrian Chadd 	/* Is ampdu pending? fetch the seqno and print it out */
2017eb6f0de0SAdrian Chadd 	if (is_ampdu_pending)
2018eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX,
2019eb6f0de0SAdrian Chadd 		    "%s: tid %d: ampdu pending, seqno %d\n",
2020eb6f0de0SAdrian Chadd 		    __func__, tid, M_SEQNO_GET(m0));
2021eb6f0de0SAdrian Chadd 
2022eb6f0de0SAdrian Chadd 	/* This also sets up the DMA map */
2023b43facbfSAdrian Chadd 	r = ath_tx_normal_setup(sc, ni, bf, m0, txq);
2024eb6f0de0SAdrian Chadd 
2025eb6f0de0SAdrian Chadd 	if (r != 0)
20267561cb5cSAdrian Chadd 		goto done;
2027eb6f0de0SAdrian Chadd 
2028eb6f0de0SAdrian Chadd 	/* At this point m0 could have changed! */
2029eb6f0de0SAdrian Chadd 	m0 = bf->bf_m;
2030eb6f0de0SAdrian Chadd 
2031eb6f0de0SAdrian Chadd #if 1
2032eb6f0de0SAdrian Chadd 	/*
2033eb6f0de0SAdrian Chadd 	 * If it's a multicast frame, do a direct-dispatch to the
2034eb6f0de0SAdrian Chadd 	 * destination hardware queue. Don't bother software
2035eb6f0de0SAdrian Chadd 	 * queuing it.
2036eb6f0de0SAdrian Chadd 	 */
2037eb6f0de0SAdrian Chadd 	/*
2038eb6f0de0SAdrian Chadd 	 * If it's a BAR frame, do a direct dispatch to the
2039eb6f0de0SAdrian Chadd 	 * destination hardware queue. Don't bother software
2040eb6f0de0SAdrian Chadd 	 * queuing it, as the TID will now be paused.
2041eb6f0de0SAdrian Chadd 	 * Sending a BAR frame can occur from the net80211 txa timer
2042eb6f0de0SAdrian Chadd 	 * (ie, retries) or from the ath txtask (completion call.)
2043eb6f0de0SAdrian Chadd 	 * It queues directly to hardware because the TID is paused
2044eb6f0de0SAdrian Chadd 	 * at this point (and won't be unpaused until the BAR has
2045eb6f0de0SAdrian Chadd 	 * either been TXed successfully or max retries has been
2046eb6f0de0SAdrian Chadd 	 * reached.)
2047eb6f0de0SAdrian Chadd 	 */
204822a3aee6SAdrian Chadd 	/*
204922a3aee6SAdrian Chadd 	 * Until things are better debugged - if this node is asleep
205022a3aee6SAdrian Chadd 	 * and we're sending it a non-BAR frame, direct dispatch it.
205122a3aee6SAdrian Chadd 	 * Why? Because we need to figure out what's actually being
205222a3aee6SAdrian Chadd 	 * sent - eg, during reassociation/reauthentication after
205322a3aee6SAdrian Chadd 	 * the node (last) disappeared whilst asleep, the driver should
205422a3aee6SAdrian Chadd 	 * have unpaused/unsleep'ed the node.  So until that is
205522a3aee6SAdrian Chadd 	 * sorted out, use this workaround.
205622a3aee6SAdrian Chadd 	 */
2057eb6f0de0SAdrian Chadd 	if (txq == &avp->av_mcastq) {
2058d3a6425bSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX,
20590b96ef63SAdrian Chadd 		    "%s: bf=%p: mcastq: TX'ing\n", __func__, bf);
20604e81f27cSAdrian Chadd 		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
2061eb6f0de0SAdrian Chadd 		ath_tx_xmit_normal(sc, txq, bf);
206222a3aee6SAdrian Chadd 	} else if (ath_tx_should_swq_frame(sc, ATH_NODE(ni), m0,
206322a3aee6SAdrian Chadd 	    &queue_to_head)) {
206422a3aee6SAdrian Chadd 		ath_tx_swq(sc, ni, txq, queue_to_head, bf);
206522a3aee6SAdrian Chadd 	} else {
20664e81f27cSAdrian Chadd 		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
2067eb6f0de0SAdrian Chadd 		ath_tx_xmit_normal(sc, txq, bf);
2068eb6f0de0SAdrian Chadd 	}
2069eb6f0de0SAdrian Chadd #else
2070eb6f0de0SAdrian Chadd 	/*
2071eb6f0de0SAdrian Chadd 	 * For now, since there's no software queue,
2072eb6f0de0SAdrian Chadd 	 * direct-dispatch to the hardware.
2073eb6f0de0SAdrian Chadd 	 */
20744e81f27cSAdrian Chadd 	bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
207522a3aee6SAdrian Chadd 	/*
207622a3aee6SAdrian Chadd 	 * Update the current leak count if
207722a3aee6SAdrian Chadd 	 * we're leaking frames; and set the
207822a3aee6SAdrian Chadd 	 * MORE flag as appropriate.
207922a3aee6SAdrian Chadd 	 */
208022a3aee6SAdrian Chadd 	ath_tx_leak_count_update(sc, tid, bf);
2081eb6f0de0SAdrian Chadd 	ath_tx_xmit_normal(sc, txq, bf);
2082eb6f0de0SAdrian Chadd #endif
20837561cb5cSAdrian Chadd done:
2084b8e788a5SAdrian Chadd 	return 0;
2085b8e788a5SAdrian Chadd }
2086b8e788a5SAdrian Chadd 
2087b8e788a5SAdrian Chadd static int
2088b8e788a5SAdrian Chadd ath_tx_raw_start(struct ath_softc *sc, struct ieee80211_node *ni,
2089b8e788a5SAdrian Chadd 	struct ath_buf *bf, struct mbuf *m0,
2090b8e788a5SAdrian Chadd 	const struct ieee80211_bpf_params *params)
2091b8e788a5SAdrian Chadd {
2092b8e788a5SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
2093b8e788a5SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
2094b8e788a5SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
2095b8e788a5SAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
2096b8e788a5SAdrian Chadd 	int error, ismcast, ismrr;
2097b8e788a5SAdrian Chadd 	int keyix, hdrlen, pktlen, try0, txantenna;
2098eb6f0de0SAdrian Chadd 	u_int8_t rix, txrate;
2099b8e788a5SAdrian Chadd 	struct ieee80211_frame *wh;
2100eb6f0de0SAdrian Chadd 	u_int flags;
2101b8e788a5SAdrian Chadd 	HAL_PKT_TYPE atype;
2102b8e788a5SAdrian Chadd 	const HAL_RATE_TABLE *rt;
2103b8e788a5SAdrian Chadd 	struct ath_desc *ds;
2104b8e788a5SAdrian Chadd 	u_int pri;
2105eb6f0de0SAdrian Chadd 	int o_tid = -1;
2106eb6f0de0SAdrian Chadd 	int do_override;
210722a3aee6SAdrian Chadd 	uint8_t type, subtype;
210822a3aee6SAdrian Chadd 	int queue_to_head;
2109b8e788a5SAdrian Chadd 
2110375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
2111375307d4SAdrian Chadd 
2112b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
2113b8e788a5SAdrian Chadd 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
2114b8e788a5SAdrian Chadd 	hdrlen = ieee80211_anyhdrsize(wh);
2115b8e788a5SAdrian Chadd 	/*
2116b8e788a5SAdrian Chadd 	 * Packet length must not include any
2117b8e788a5SAdrian Chadd 	 * pad bytes; deduct them here.
2118b8e788a5SAdrian Chadd 	 */
2119b8e788a5SAdrian Chadd 	/* XXX honor IEEE80211_BPF_DATAPAD */
2120b8e788a5SAdrian Chadd 	pktlen = m0->m_pkthdr.len - (hdrlen & 3) + IEEE80211_CRC_LEN;
2121b8e788a5SAdrian Chadd 
212222a3aee6SAdrian Chadd 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
212322a3aee6SAdrian Chadd 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
212422a3aee6SAdrian Chadd 
212503682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 2,
212603682514SAdrian Chadd 	     "ath_tx_raw_start: ni=%p, bf=%p, raw", ni, bf);
212703682514SAdrian Chadd 
2128eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: ismcast=%d\n",
2129eb6f0de0SAdrian Chadd 	    __func__, ismcast);
2130eb6f0de0SAdrian Chadd 
21317561cb5cSAdrian Chadd 	pri = params->ibp_pri & 3;
21327561cb5cSAdrian Chadd 	/* Override pri if the frame isn't a QoS one */
21337561cb5cSAdrian Chadd 	if (! IEEE80211_QOS_HAS_SEQ(wh))
21347561cb5cSAdrian Chadd 		pri = ath_tx_getac(sc, m0);
21357561cb5cSAdrian Chadd 
21367561cb5cSAdrian Chadd 	/* XXX If it's an ADDBA, override the correct queue */
21377561cb5cSAdrian Chadd 	do_override = ath_tx_action_frame_override_queue(sc, ni, m0, &o_tid);
21387561cb5cSAdrian Chadd 
21397561cb5cSAdrian Chadd 	/* Map ADDBA to the correct priority */
21407561cb5cSAdrian Chadd 	if (do_override) {
21417561cb5cSAdrian Chadd #if 0
214283bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_XMIT,
21437561cb5cSAdrian Chadd 		    "%s: overriding tid %d pri %d -> %d\n",
21447561cb5cSAdrian Chadd 		    __func__, o_tid, pri, TID_TO_WME_AC(o_tid));
21457561cb5cSAdrian Chadd #endif
21467561cb5cSAdrian Chadd 		pri = TID_TO_WME_AC(o_tid);
21477561cb5cSAdrian Chadd 	}
21487561cb5cSAdrian Chadd 
214981a82688SAdrian Chadd 	/* Handle encryption twiddling if needed */
2150eb6f0de0SAdrian Chadd 	if (! ath_tx_tag_crypto(sc, ni,
2151eb6f0de0SAdrian Chadd 	    m0, params->ibp_flags & IEEE80211_BPF_CRYPTO, 0,
2152eb6f0de0SAdrian Chadd 	    &hdrlen, &pktlen, &keyix)) {
2153b8e788a5SAdrian Chadd 		ath_freetx(m0);
2154b8e788a5SAdrian Chadd 		return EIO;
2155b8e788a5SAdrian Chadd 	}
2156b8e788a5SAdrian Chadd 	/* packet header may have moved, reset our local pointer */
2157b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
2158b8e788a5SAdrian Chadd 
2159eb6f0de0SAdrian Chadd 	/* Do the generic frame setup */
2160eb6f0de0SAdrian Chadd 	/* XXX should just bzero the bf_state? */
2161eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_dobaw = 0;
2162eb6f0de0SAdrian Chadd 
2163b8e788a5SAdrian Chadd 	error = ath_tx_dmasetup(sc, bf, m0);
2164b8e788a5SAdrian Chadd 	if (error != 0)
2165b8e788a5SAdrian Chadd 		return error;
2166b8e788a5SAdrian Chadd 	m0 = bf->bf_m;				/* NB: may have changed */
2167b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
2168b8e788a5SAdrian Chadd 	bf->bf_node = ni;			/* NB: held reference */
2169b8e788a5SAdrian Chadd 
21704e81f27cSAdrian Chadd 	/* Always enable CLRDMASK for raw frames for now.. */
2171b8e788a5SAdrian Chadd 	flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for crypto errs */
2172b8e788a5SAdrian Chadd 	flags |= HAL_TXDESC_INTREQ;		/* force interrupt */
2173b8e788a5SAdrian Chadd 	if (params->ibp_flags & IEEE80211_BPF_RTS)
2174b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_RTSENA;
2175eb6f0de0SAdrian Chadd 	else if (params->ibp_flags & IEEE80211_BPF_CTS) {
2176eb6f0de0SAdrian Chadd 		/* XXX assume 11g/11n protection? */
2177eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_doprot = 1;
2178b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_CTSENA;
2179eb6f0de0SAdrian Chadd 	}
2180b8e788a5SAdrian Chadd 	/* XXX leave ismcast to injector? */
2181b8e788a5SAdrian Chadd 	if ((params->ibp_flags & IEEE80211_BPF_NOACK) || ismcast)
2182b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_NOACK;
2183b8e788a5SAdrian Chadd 
2184b8e788a5SAdrian Chadd 	rt = sc->sc_currates;
2185b8e788a5SAdrian Chadd 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
2186b8e788a5SAdrian Chadd 	rix = ath_tx_findrix(sc, params->ibp_rate0);
2187b8e788a5SAdrian Chadd 	txrate = rt->info[rix].rateCode;
2188b8e788a5SAdrian Chadd 	if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
2189b8e788a5SAdrian Chadd 		txrate |= rt->info[rix].shortPreamble;
2190b8e788a5SAdrian Chadd 	sc->sc_txrix = rix;
2191b8e788a5SAdrian Chadd 	try0 = params->ibp_try0;
2192b8e788a5SAdrian Chadd 	ismrr = (params->ibp_try1 != 0);
2193b8e788a5SAdrian Chadd 	txantenna = params->ibp_pri >> 2;
2194b8e788a5SAdrian Chadd 	if (txantenna == 0)			/* XXX? */
2195b8e788a5SAdrian Chadd 		txantenna = sc->sc_txantenna;
219679f02dbfSAdrian Chadd 
219779f02dbfSAdrian Chadd 	/*
2198eb6f0de0SAdrian Chadd 	 * Since ctsrate is fixed, store it away for later
2199eb6f0de0SAdrian Chadd 	 * use when the descriptor fields are being set.
220079f02dbfSAdrian Chadd 	 */
2201eb6f0de0SAdrian Chadd 	if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA))
2202eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_ctsrate0 = params->ibp_ctsrate;
220379f02dbfSAdrian Chadd 
2204b8e788a5SAdrian Chadd 	/*
2205b8e788a5SAdrian Chadd 	 * NB: we mark all packets as type PSPOLL so the h/w won't
2206b8e788a5SAdrian Chadd 	 * set the sequence number, duration, etc.
2207b8e788a5SAdrian Chadd 	 */
2208b8e788a5SAdrian Chadd 	atype = HAL_PKT_TYPE_PSPOLL;
2209b8e788a5SAdrian Chadd 
2210b8e788a5SAdrian Chadd 	if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
2211b8e788a5SAdrian Chadd 		ieee80211_dump_pkt(ic, mtod(m0, caddr_t), m0->m_len,
2212b8e788a5SAdrian Chadd 		    sc->sc_hwmap[rix].ieeerate, -1);
2213b8e788a5SAdrian Chadd 
2214b8e788a5SAdrian Chadd 	if (ieee80211_radiotap_active_vap(vap)) {
2215b8e788a5SAdrian Chadd 		u_int64_t tsf = ath_hal_gettsf64(ah);
2216b8e788a5SAdrian Chadd 
2217b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_tsf = htole64(tsf);
2218b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
22195945b5f5SKevin Lo 		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED)
2220b8e788a5SAdrian Chadd 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2221b8e788a5SAdrian Chadd 		if (m0->m_flags & M_FRAG)
2222b8e788a5SAdrian Chadd 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
2223b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
222412087a07SAdrian Chadd 		sc->sc_tx_th.wt_txpower = MIN(params->ibp_power,
222512087a07SAdrian Chadd 		    ieee80211_get_node_txpower(ni));
2226b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
2227b8e788a5SAdrian Chadd 
2228b8e788a5SAdrian Chadd 		ieee80211_radiotap_tx(vap, m0);
2229b8e788a5SAdrian Chadd 	}
2230b8e788a5SAdrian Chadd 
2231b8e788a5SAdrian Chadd 	/*
2232b8e788a5SAdrian Chadd 	 * Formulate first tx descriptor with tx controls.
2233b8e788a5SAdrian Chadd 	 */
2234b8e788a5SAdrian Chadd 	ds = bf->bf_desc;
2235b8e788a5SAdrian Chadd 	/* XXX check return value? */
2236eb6f0de0SAdrian Chadd 
2237eb6f0de0SAdrian Chadd 	/* Store the decided rate index values away */
2238eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_pktlen = pktlen;
2239eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_hdrlen = hdrlen;
2240eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_atype = atype;
224112087a07SAdrian Chadd 	bf->bf_state.bfs_txpower = MIN(params->ibp_power,
224212087a07SAdrian Chadd 	    ieee80211_get_node_txpower(ni));
2243eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txrate0 = txrate;
2244eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_try0 = try0;
2245eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_keyix = keyix;
2246eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txantenna = txantenna;
2247875a9451SAdrian Chadd 	bf->bf_state.bfs_txflags = flags;
2248eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_shpream =
2249eb6f0de0SAdrian Chadd 	    !! (params->ibp_flags & IEEE80211_BPF_SHORTPRE);
2250b8e788a5SAdrian Chadd 
225146634305SAdrian Chadd 	/* Set local packet state, used to queue packets to hardware */
225246634305SAdrian Chadd 	bf->bf_state.bfs_tid = WME_AC_TO_TID(pri);
2253fc56c9c5SAdrian Chadd 	bf->bf_state.bfs_tx_queue = sc->sc_ac2q[pri]->axq_qnum;
225446634305SAdrian Chadd 	bf->bf_state.bfs_pri = pri;
225546634305SAdrian Chadd 
2256eb6f0de0SAdrian Chadd 	/* XXX this should be done in ath_tx_setrate() */
2257eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsrate = 0;
2258eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsduration = 0;
2259eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ismrr = ismrr;
2260eb6f0de0SAdrian Chadd 
2261eb6f0de0SAdrian Chadd 	/* Blank the legacy rate array */
2262eb6f0de0SAdrian Chadd 	bzero(&bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
2263eb6f0de0SAdrian Chadd 
2264eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].rix =
2265eb6f0de0SAdrian Chadd 	    ath_tx_findrix(sc, params->ibp_rate0);
2266eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].tries = try0;
2267eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].ratecode = txrate;
2268c1782ce0SAdrian Chadd 
2269c1782ce0SAdrian Chadd 	if (ismrr) {
2270eb6f0de0SAdrian Chadd 		int rix;
2271c1782ce0SAdrian Chadd 
2272b8e788a5SAdrian Chadd 		rix = ath_tx_findrix(sc, params->ibp_rate1);
2273eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[1].rix = rix;
2274eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[1].tries = params->ibp_try1;
2275c1782ce0SAdrian Chadd 
2276eb6f0de0SAdrian Chadd 		rix = ath_tx_findrix(sc, params->ibp_rate2);
2277eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[2].rix = rix;
2278eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[2].tries = params->ibp_try2;
2279eb6f0de0SAdrian Chadd 
2280eb6f0de0SAdrian Chadd 		rix = ath_tx_findrix(sc, params->ibp_rate3);
2281eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[3].rix = rix;
2282eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[3].tries = params->ibp_try3;
2283c1782ce0SAdrian Chadd 	}
2284eb6f0de0SAdrian Chadd 	/*
2285eb6f0de0SAdrian Chadd 	 * All the required rate control decisions have been made;
2286eb6f0de0SAdrian Chadd 	 * fill in the rc flags.
2287eb6f0de0SAdrian Chadd 	 */
2288eb6f0de0SAdrian Chadd 	ath_tx_rate_fill_rcflags(sc, bf);
2289b8e788a5SAdrian Chadd 
2290b8e788a5SAdrian Chadd 	/* NB: no buffered multicast in power save support */
2291eb6f0de0SAdrian Chadd 
2292eb6f0de0SAdrian Chadd 	/*
2293eb6f0de0SAdrian Chadd 	 * If we're overiding the ADDBA destination, dump directly
2294eb6f0de0SAdrian Chadd 	 * into the hardware queue, right after any pending
2295eb6f0de0SAdrian Chadd 	 * frames to that node are.
2296eb6f0de0SAdrian Chadd 	 */
2297eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: dooverride=%d\n",
2298eb6f0de0SAdrian Chadd 	    __func__, do_override);
2299eb6f0de0SAdrian Chadd 
230094eefcf1SAdrian Chadd #if 1
230122a3aee6SAdrian Chadd 	/*
230222a3aee6SAdrian Chadd 	 * Put addba frames in the right place in the right TID/HWQ.
230322a3aee6SAdrian Chadd 	 */
2304eb6f0de0SAdrian Chadd 	if (do_override) {
23054e81f27cSAdrian Chadd 		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
230622a3aee6SAdrian Chadd 		/*
230722a3aee6SAdrian Chadd 		 * XXX if it's addba frames, should we be leaking
230822a3aee6SAdrian Chadd 		 * them out via the frame leak method?
230922a3aee6SAdrian Chadd 		 * XXX for now let's not risk it; but we may wish
231022a3aee6SAdrian Chadd 		 * to investigate this later.
231122a3aee6SAdrian Chadd 		 */
2312eb6f0de0SAdrian Chadd 		ath_tx_xmit_normal(sc, sc->sc_ac2q[pri], bf);
231322a3aee6SAdrian Chadd 	} else if (ath_tx_should_swq_frame(sc, ATH_NODE(ni), m0,
231422a3aee6SAdrian Chadd 	    &queue_to_head)) {
2315eb6f0de0SAdrian Chadd 		/* Queue to software queue */
231622a3aee6SAdrian Chadd 		ath_tx_swq(sc, ni, sc->sc_ac2q[pri], queue_to_head, bf);
231722a3aee6SAdrian Chadd 	} else {
231822a3aee6SAdrian Chadd 		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
231922a3aee6SAdrian Chadd 		ath_tx_xmit_normal(sc, sc->sc_ac2q[pri], bf);
2320eb6f0de0SAdrian Chadd 	}
232194eefcf1SAdrian Chadd #else
232294eefcf1SAdrian Chadd 	/* Direct-dispatch to the hardware */
232394eefcf1SAdrian Chadd 	bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
232422a3aee6SAdrian Chadd 	/*
232522a3aee6SAdrian Chadd 	 * Update the current leak count if
232622a3aee6SAdrian Chadd 	 * we're leaking frames; and set the
232722a3aee6SAdrian Chadd 	 * MORE flag as appropriate.
232822a3aee6SAdrian Chadd 	 */
232922a3aee6SAdrian Chadd 	ath_tx_leak_count_update(sc, tid, bf);
233094eefcf1SAdrian Chadd 	ath_tx_xmit_normal(sc, sc->sc_ac2q[pri], bf);
233194eefcf1SAdrian Chadd #endif
2332b8e788a5SAdrian Chadd 	return 0;
2333b8e788a5SAdrian Chadd }
2334b8e788a5SAdrian Chadd 
2335eb6f0de0SAdrian Chadd /*
2336eb6f0de0SAdrian Chadd  * Send a raw frame.
2337eb6f0de0SAdrian Chadd  *
2338eb6f0de0SAdrian Chadd  * This can be called by net80211.
2339eb6f0de0SAdrian Chadd  */
2340b8e788a5SAdrian Chadd int
2341b8e788a5SAdrian Chadd ath_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2342b8e788a5SAdrian Chadd 	const struct ieee80211_bpf_params *params)
2343b8e788a5SAdrian Chadd {
2344b8e788a5SAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
2345b8e788a5SAdrian Chadd 	struct ifnet *ifp = ic->ic_ifp;
2346b8e788a5SAdrian Chadd 	struct ath_softc *sc = ifp->if_softc;
2347b8e788a5SAdrian Chadd 	struct ath_buf *bf;
23489c85ff91SAdrian Chadd 	struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
23499c85ff91SAdrian Chadd 	int error = 0;
2350b8e788a5SAdrian Chadd 
2351ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2352ef27340cSAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
235383bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_XMIT,
235483bbd5ebSRui Paulo 		    "%s: sc_inreset_cnt > 0; bailing\n", __func__);
2355ef27340cSAdrian Chadd 		error = EIO;
2356ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2357ef27340cSAdrian Chadd 		goto bad0;
2358ef27340cSAdrian Chadd 	}
2359ef27340cSAdrian Chadd 	sc->sc_txstart_cnt++;
2360ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2361ef27340cSAdrian Chadd 
23621b5c5f5aSAdrian Chadd 	ATH_TX_LOCK(sc);
23631b5c5f5aSAdrian Chadd 
2364b8e788a5SAdrian Chadd 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->sc_invalid) {
2365b8e788a5SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: discard frame, %s", __func__,
2366b8e788a5SAdrian Chadd 		    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ?
2367b8e788a5SAdrian Chadd 			"!running" : "invalid");
2368b8e788a5SAdrian Chadd 		m_freem(m);
2369b8e788a5SAdrian Chadd 		error = ENETDOWN;
2370b8e788a5SAdrian Chadd 		goto bad;
2371b8e788a5SAdrian Chadd 	}
23729c85ff91SAdrian Chadd 
23739c85ff91SAdrian Chadd 	/*
23749c85ff91SAdrian Chadd 	 * Enforce how deep the multicast queue can grow.
23759c85ff91SAdrian Chadd 	 *
23769c85ff91SAdrian Chadd 	 * XXX duplicated in ath_tx_start().
23779c85ff91SAdrian Chadd 	 */
23789c85ff91SAdrian Chadd 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
237992e84e43SAdrian Chadd 		if (sc->sc_cabq->axq_depth + sc->sc_cabq->fifo.axq_depth
238092e84e43SAdrian Chadd 		    > sc->sc_txq_mcastq_maxdepth) {
23819c85ff91SAdrian Chadd 			sc->sc_stats.ast_tx_mcastq_overflow++;
23829c85ff91SAdrian Chadd 			error = ENOBUFS;
23839c85ff91SAdrian Chadd 		}
23849c85ff91SAdrian Chadd 
23859c85ff91SAdrian Chadd 		if (error != 0) {
23869c85ff91SAdrian Chadd 			m_freem(m);
23879c85ff91SAdrian Chadd 			goto bad;
23889c85ff91SAdrian Chadd 		}
23899c85ff91SAdrian Chadd 	}
23909c85ff91SAdrian Chadd 
2391b8e788a5SAdrian Chadd 	/*
2392b8e788a5SAdrian Chadd 	 * Grab a TX buffer and associated resources.
2393b8e788a5SAdrian Chadd 	 */
2394af33d486SAdrian Chadd 	bf = ath_getbuf(sc, ATH_BUFTYPE_MGMT);
2395b8e788a5SAdrian Chadd 	if (bf == NULL) {
2396b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_nobuf++;
2397b8e788a5SAdrian Chadd 		m_freem(m);
2398b8e788a5SAdrian Chadd 		error = ENOBUFS;
2399b8e788a5SAdrian Chadd 		goto bad;
2400b8e788a5SAdrian Chadd 	}
240103682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 3, "ath_raw_xmit: m=%p, params=%p, bf=%p\n",
240203682514SAdrian Chadd 	    m, params,  bf);
2403b8e788a5SAdrian Chadd 
2404b8e788a5SAdrian Chadd 	if (params == NULL) {
2405b8e788a5SAdrian Chadd 		/*
2406b8e788a5SAdrian Chadd 		 * Legacy path; interpret frame contents to decide
2407b8e788a5SAdrian Chadd 		 * precisely how to send the frame.
2408b8e788a5SAdrian Chadd 		 */
2409b8e788a5SAdrian Chadd 		if (ath_tx_start(sc, ni, bf, m)) {
2410b8e788a5SAdrian Chadd 			error = EIO;		/* XXX */
2411b8e788a5SAdrian Chadd 			goto bad2;
2412b8e788a5SAdrian Chadd 		}
2413b8e788a5SAdrian Chadd 	} else {
2414b8e788a5SAdrian Chadd 		/*
2415b8e788a5SAdrian Chadd 		 * Caller supplied explicit parameters to use in
2416b8e788a5SAdrian Chadd 		 * sending the frame.
2417b8e788a5SAdrian Chadd 		 */
2418b8e788a5SAdrian Chadd 		if (ath_tx_raw_start(sc, ni, bf, m, params)) {
2419b8e788a5SAdrian Chadd 			error = EIO;		/* XXX */
2420b8e788a5SAdrian Chadd 			goto bad2;
2421b8e788a5SAdrian Chadd 		}
2422b8e788a5SAdrian Chadd 	}
2423b8e788a5SAdrian Chadd 	sc->sc_wd_timer = 5;
2424b8e788a5SAdrian Chadd 	ifp->if_opackets++;
2425b8e788a5SAdrian Chadd 	sc->sc_stats.ast_tx_raw++;
2426b8e788a5SAdrian Chadd 
2427548a605dSAdrian Chadd 	/*
2428548a605dSAdrian Chadd 	 * Update the TIM - if there's anything queued to the
2429548a605dSAdrian Chadd 	 * software queue and power save is enabled, we should
2430548a605dSAdrian Chadd 	 * set the TIM.
2431548a605dSAdrian Chadd 	 */
2432548a605dSAdrian Chadd 	ath_tx_update_tim(sc, ni, 1);
2433548a605dSAdrian Chadd 
2434974185bbSAdrian Chadd 	ATH_TX_UNLOCK(sc);
2435974185bbSAdrian Chadd 
2436ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2437ef27340cSAdrian Chadd 	sc->sc_txstart_cnt--;
2438ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2439ef27340cSAdrian Chadd 
2440b8e788a5SAdrian Chadd 	return 0;
2441b8e788a5SAdrian Chadd bad2:
244203682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 3, "ath_raw_xmit: bad2: m=%p, params=%p, "
244303682514SAdrian Chadd 	    "bf=%p",
244403682514SAdrian Chadd 	    m,
244503682514SAdrian Chadd 	    params,
244603682514SAdrian Chadd 	    bf);
2447b8e788a5SAdrian Chadd 	ATH_TXBUF_LOCK(sc);
2448e1a50456SAdrian Chadd 	ath_returnbuf_head(sc, bf);
2449b8e788a5SAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
2450b8e788a5SAdrian Chadd bad:
24511b5c5f5aSAdrian Chadd 
24521b5c5f5aSAdrian Chadd 	ATH_TX_UNLOCK(sc);
24531b5c5f5aSAdrian Chadd 
2454ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2455ef27340cSAdrian Chadd 	sc->sc_txstart_cnt--;
2456ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2457ef27340cSAdrian Chadd bad0:
245803682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 2, "ath_raw_xmit: bad0: m=%p, params=%p",
245903682514SAdrian Chadd 	    m, params);
2460b8e788a5SAdrian Chadd 	ifp->if_oerrors++;
2461b8e788a5SAdrian Chadd 	sc->sc_stats.ast_tx_raw_fail++;
2462b8e788a5SAdrian Chadd 	ieee80211_free_node(ni);
2463ef27340cSAdrian Chadd 
2464b8e788a5SAdrian Chadd 	return error;
2465b8e788a5SAdrian Chadd }
2466eb6f0de0SAdrian Chadd 
2467eb6f0de0SAdrian Chadd /* Some helper functions */
2468eb6f0de0SAdrian Chadd 
2469eb6f0de0SAdrian Chadd /*
2470eb6f0de0SAdrian Chadd  * ADDBA (and potentially others) need to be placed in the same
2471eb6f0de0SAdrian Chadd  * hardware queue as the TID/node it's relating to. This is so
2472eb6f0de0SAdrian Chadd  * it goes out after any pending non-aggregate frames to the
2473eb6f0de0SAdrian Chadd  * same node/TID.
2474eb6f0de0SAdrian Chadd  *
2475eb6f0de0SAdrian Chadd  * If this isn't done, the ADDBA can go out before the frames
2476eb6f0de0SAdrian Chadd  * queued in hardware. Even though these frames have a sequence
2477eb6f0de0SAdrian Chadd  * number -earlier- than the ADDBA can be transmitted (but
2478eb6f0de0SAdrian Chadd  * no frames whose sequence numbers are after the ADDBA should
2479eb6f0de0SAdrian Chadd  * be!) they'll arrive after the ADDBA - and the receiving end
2480eb6f0de0SAdrian Chadd  * will simply drop them as being out of the BAW.
2481eb6f0de0SAdrian Chadd  *
2482eb6f0de0SAdrian Chadd  * The frames can't be appended to the TID software queue - it'll
2483eb6f0de0SAdrian Chadd  * never be sent out. So these frames have to be directly
2484eb6f0de0SAdrian Chadd  * dispatched to the hardware, rather than queued in software.
2485eb6f0de0SAdrian Chadd  * So if this function returns true, the TXQ has to be
2486eb6f0de0SAdrian Chadd  * overridden and it has to be directly dispatched.
2487eb6f0de0SAdrian Chadd  *
2488eb6f0de0SAdrian Chadd  * It's a dirty hack, but someone's gotta do it.
2489eb6f0de0SAdrian Chadd  */
2490eb6f0de0SAdrian Chadd 
2491eb6f0de0SAdrian Chadd /*
2492eb6f0de0SAdrian Chadd  * XXX doesn't belong here!
2493eb6f0de0SAdrian Chadd  */
2494eb6f0de0SAdrian Chadd static int
2495eb6f0de0SAdrian Chadd ieee80211_is_action(struct ieee80211_frame *wh)
2496eb6f0de0SAdrian Chadd {
2497eb6f0de0SAdrian Chadd 	/* Type: Management frame? */
2498eb6f0de0SAdrian Chadd 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) !=
2499eb6f0de0SAdrian Chadd 	    IEEE80211_FC0_TYPE_MGT)
2500eb6f0de0SAdrian Chadd 		return 0;
2501eb6f0de0SAdrian Chadd 
2502eb6f0de0SAdrian Chadd 	/* Subtype: Action frame? */
2503eb6f0de0SAdrian Chadd 	if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) !=
2504eb6f0de0SAdrian Chadd 	    IEEE80211_FC0_SUBTYPE_ACTION)
2505eb6f0de0SAdrian Chadd 		return 0;
2506eb6f0de0SAdrian Chadd 
2507eb6f0de0SAdrian Chadd 	return 1;
2508eb6f0de0SAdrian Chadd }
2509eb6f0de0SAdrian Chadd 
2510eb6f0de0SAdrian Chadd #define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
2511eb6f0de0SAdrian Chadd /*
2512eb6f0de0SAdrian Chadd  * Return an alternate TID for ADDBA request frames.
2513eb6f0de0SAdrian Chadd  *
2514eb6f0de0SAdrian Chadd  * Yes, this likely should be done in the net80211 layer.
2515eb6f0de0SAdrian Chadd  */
2516eb6f0de0SAdrian Chadd static int
2517eb6f0de0SAdrian Chadd ath_tx_action_frame_override_queue(struct ath_softc *sc,
2518eb6f0de0SAdrian Chadd     struct ieee80211_node *ni,
2519eb6f0de0SAdrian Chadd     struct mbuf *m0, int *tid)
2520eb6f0de0SAdrian Chadd {
2521eb6f0de0SAdrian Chadd 	struct ieee80211_frame *wh = mtod(m0, struct ieee80211_frame *);
2522eb6f0de0SAdrian Chadd 	struct ieee80211_action_ba_addbarequest *ia;
2523eb6f0de0SAdrian Chadd 	uint8_t *frm;
2524eb6f0de0SAdrian Chadd 	uint16_t baparamset;
2525eb6f0de0SAdrian Chadd 
2526eb6f0de0SAdrian Chadd 	/* Not action frame? Bail */
2527eb6f0de0SAdrian Chadd 	if (! ieee80211_is_action(wh))
2528eb6f0de0SAdrian Chadd 		return 0;
2529eb6f0de0SAdrian Chadd 
2530eb6f0de0SAdrian Chadd 	/* XXX Not needed for frames we send? */
2531eb6f0de0SAdrian Chadd #if 0
2532eb6f0de0SAdrian Chadd 	/* Correct length? */
2533eb6f0de0SAdrian Chadd 	if (! ieee80211_parse_action(ni, m))
2534eb6f0de0SAdrian Chadd 		return 0;
2535eb6f0de0SAdrian Chadd #endif
2536eb6f0de0SAdrian Chadd 
2537eb6f0de0SAdrian Chadd 	/* Extract out action frame */
2538eb6f0de0SAdrian Chadd 	frm = (u_int8_t *)&wh[1];
2539eb6f0de0SAdrian Chadd 	ia = (struct ieee80211_action_ba_addbarequest *) frm;
2540eb6f0de0SAdrian Chadd 
2541eb6f0de0SAdrian Chadd 	/* Not ADDBA? Bail */
2542eb6f0de0SAdrian Chadd 	if (ia->rq_header.ia_category != IEEE80211_ACTION_CAT_BA)
2543eb6f0de0SAdrian Chadd 		return 0;
2544eb6f0de0SAdrian Chadd 	if (ia->rq_header.ia_action != IEEE80211_ACTION_BA_ADDBA_REQUEST)
2545eb6f0de0SAdrian Chadd 		return 0;
2546eb6f0de0SAdrian Chadd 
2547eb6f0de0SAdrian Chadd 	/* Extract TID, return it */
2548eb6f0de0SAdrian Chadd 	baparamset = le16toh(ia->rq_baparamset);
2549eb6f0de0SAdrian Chadd 	*tid = (int) MS(baparamset, IEEE80211_BAPS_TID);
2550eb6f0de0SAdrian Chadd 
2551eb6f0de0SAdrian Chadd 	return 1;
2552eb6f0de0SAdrian Chadd }
2553eb6f0de0SAdrian Chadd #undef	MS
2554eb6f0de0SAdrian Chadd 
2555eb6f0de0SAdrian Chadd /* Per-node software queue operations */
2556eb6f0de0SAdrian Chadd 
2557eb6f0de0SAdrian Chadd /*
2558eb6f0de0SAdrian Chadd  * Add the current packet to the given BAW.
2559eb6f0de0SAdrian Chadd  * It is assumed that the current packet
2560eb6f0de0SAdrian Chadd  *
2561eb6f0de0SAdrian Chadd  * + fits inside the BAW;
2562eb6f0de0SAdrian Chadd  * + already has had a sequence number allocated.
2563eb6f0de0SAdrian Chadd  *
2564eb6f0de0SAdrian Chadd  * Since the BAW status may be modified by both the ath task and
2565eb6f0de0SAdrian Chadd  * the net80211/ifnet contexts, the TID must be locked.
2566eb6f0de0SAdrian Chadd  */
2567eb6f0de0SAdrian Chadd void
2568eb6f0de0SAdrian Chadd ath_tx_addto_baw(struct ath_softc *sc, struct ath_node *an,
2569eb6f0de0SAdrian Chadd     struct ath_tid *tid, struct ath_buf *bf)
2570eb6f0de0SAdrian Chadd {
2571eb6f0de0SAdrian Chadd 	int index, cindex;
2572eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
2573eb6f0de0SAdrian Chadd 
2574375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
2575eb6f0de0SAdrian Chadd 
2576eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_isretried)
2577eb6f0de0SAdrian Chadd 		return;
2578eb6f0de0SAdrian Chadd 
2579c7c07341SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
2580c7c07341SAdrian Chadd 
25817561cb5cSAdrian Chadd 	if (! bf->bf_state.bfs_dobaw) {
258283bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
25837561cb5cSAdrian Chadd 		    "%s: dobaw=0, seqno=%d, window %d:%d\n",
258483bbd5ebSRui Paulo 		    __func__, SEQNO(bf->bf_state.bfs_seqno),
258583bbd5ebSRui Paulo 		    tap->txa_start, tap->txa_wnd);
25867561cb5cSAdrian Chadd 	}
25877561cb5cSAdrian Chadd 
2588eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_addedbaw)
258983bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2590a108d2d6SAdrian Chadd 		    "%s: re-added? tid=%d, seqno %d; window %d:%d; "
2591d4365d16SAdrian Chadd 		    "baw head=%d tail=%d\n",
2592a108d2d6SAdrian Chadd 		    __func__, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
2593d4365d16SAdrian Chadd 		    tap->txa_start, tap->txa_wnd, tid->baw_head,
2594d4365d16SAdrian Chadd 		    tid->baw_tail);
2595eb6f0de0SAdrian Chadd 
2596eb6f0de0SAdrian Chadd 	/*
25977561cb5cSAdrian Chadd 	 * Verify that the given sequence number is not outside of the
25987561cb5cSAdrian Chadd 	 * BAW.  Complain loudly if that's the case.
25997561cb5cSAdrian Chadd 	 */
26007561cb5cSAdrian Chadd 	if (! BAW_WITHIN(tap->txa_start, tap->txa_wnd,
26017561cb5cSAdrian Chadd 	    SEQNO(bf->bf_state.bfs_seqno))) {
260283bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
26037561cb5cSAdrian Chadd 		    "%s: bf=%p: outside of BAW?? tid=%d, seqno %d; window %d:%d; "
26047561cb5cSAdrian Chadd 		    "baw head=%d tail=%d\n",
26057561cb5cSAdrian Chadd 		    __func__, bf, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
26067561cb5cSAdrian Chadd 		    tap->txa_start, tap->txa_wnd, tid->baw_head,
26077561cb5cSAdrian Chadd 		    tid->baw_tail);
26087561cb5cSAdrian Chadd 	}
26097561cb5cSAdrian Chadd 
26107561cb5cSAdrian Chadd 	/*
2611eb6f0de0SAdrian Chadd 	 * ni->ni_txseqs[] is the currently allocated seqno.
2612eb6f0de0SAdrian Chadd 	 * the txa state contains the current baw start.
2613eb6f0de0SAdrian Chadd 	 */
2614eb6f0de0SAdrian Chadd 	index  = ATH_BA_INDEX(tap->txa_start, SEQNO(bf->bf_state.bfs_seqno));
2615eb6f0de0SAdrian Chadd 	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
2616eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2617a108d2d6SAdrian Chadd 	    "%s: tid=%d, seqno %d; window %d:%d; index=%d cindex=%d "
2618d4365d16SAdrian Chadd 	    "baw head=%d tail=%d\n",
2619a108d2d6SAdrian Chadd 	    __func__, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
2620d4365d16SAdrian Chadd 	    tap->txa_start, tap->txa_wnd, index, cindex, tid->baw_head,
2621d4365d16SAdrian Chadd 	    tid->baw_tail);
2622eb6f0de0SAdrian Chadd 
2623eb6f0de0SAdrian Chadd 
2624eb6f0de0SAdrian Chadd #if 0
2625eb6f0de0SAdrian Chadd 	assert(tid->tx_buf[cindex] == NULL);
2626eb6f0de0SAdrian Chadd #endif
2627eb6f0de0SAdrian Chadd 	if (tid->tx_buf[cindex] != NULL) {
262883bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2629eb6f0de0SAdrian Chadd 		    "%s: ba packet dup (index=%d, cindex=%d, "
2630eb6f0de0SAdrian Chadd 		    "head=%d, tail=%d)\n",
2631eb6f0de0SAdrian Chadd 		    __func__, index, cindex, tid->baw_head, tid->baw_tail);
263283bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2633eb6f0de0SAdrian Chadd 		    "%s: BA bf: %p; seqno=%d ; new bf: %p; seqno=%d\n",
2634eb6f0de0SAdrian Chadd 		    __func__,
2635eb6f0de0SAdrian Chadd 		    tid->tx_buf[cindex],
2636eb6f0de0SAdrian Chadd 		    SEQNO(tid->tx_buf[cindex]->bf_state.bfs_seqno),
2637eb6f0de0SAdrian Chadd 		    bf,
2638eb6f0de0SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno)
2639eb6f0de0SAdrian Chadd 		);
2640eb6f0de0SAdrian Chadd 	}
2641eb6f0de0SAdrian Chadd 	tid->tx_buf[cindex] = bf;
2642eb6f0de0SAdrian Chadd 
2643d4365d16SAdrian Chadd 	if (index >= ((tid->baw_tail - tid->baw_head) &
2644d4365d16SAdrian Chadd 	    (ATH_TID_MAX_BUFS - 1))) {
2645eb6f0de0SAdrian Chadd 		tid->baw_tail = cindex;
2646eb6f0de0SAdrian Chadd 		INCR(tid->baw_tail, ATH_TID_MAX_BUFS);
2647eb6f0de0SAdrian Chadd 	}
2648eb6f0de0SAdrian Chadd }
2649eb6f0de0SAdrian Chadd 
2650eb6f0de0SAdrian Chadd /*
265138962489SAdrian Chadd  * Flip the BAW buffer entry over from the existing one to the new one.
265238962489SAdrian Chadd  *
265338962489SAdrian Chadd  * When software retransmitting a (sub-)frame, it is entirely possible that
265438962489SAdrian Chadd  * the frame ath_buf is marked as BUSY and can't be immediately reused.
265538962489SAdrian Chadd  * In that instance the buffer is cloned and the new buffer is used for
265638962489SAdrian Chadd  * retransmit. We thus need to update the ath_buf slot in the BAW buf
265738962489SAdrian Chadd  * tracking array to maintain consistency.
265838962489SAdrian Chadd  */
265938962489SAdrian Chadd static void
266038962489SAdrian Chadd ath_tx_switch_baw_buf(struct ath_softc *sc, struct ath_node *an,
266138962489SAdrian Chadd     struct ath_tid *tid, struct ath_buf *old_bf, struct ath_buf *new_bf)
266238962489SAdrian Chadd {
266338962489SAdrian Chadd 	int index, cindex;
266438962489SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
266538962489SAdrian Chadd 	int seqno = SEQNO(old_bf->bf_state.bfs_seqno);
266638962489SAdrian Chadd 
2667375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
266838962489SAdrian Chadd 
266938962489SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
267038962489SAdrian Chadd 	index  = ATH_BA_INDEX(tap->txa_start, seqno);
267138962489SAdrian Chadd 	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
267238962489SAdrian Chadd 
267338962489SAdrian Chadd 	/*
267438962489SAdrian Chadd 	 * Just warn for now; if it happens then we should find out
267538962489SAdrian Chadd 	 * about it. It's highly likely the aggregation session will
267638962489SAdrian Chadd 	 * soon hang.
267738962489SAdrian Chadd 	 */
267838962489SAdrian Chadd 	if (old_bf->bf_state.bfs_seqno != new_bf->bf_state.bfs_seqno) {
267983bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
268083bbd5ebSRui Paulo 		    "%s: retransmitted buffer"
268138962489SAdrian Chadd 		    " has mismatching seqno's, BA session may hang.\n",
268238962489SAdrian Chadd 		    __func__);
268383bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
268483bbd5ebSRui Paulo 		    "%s: old seqno=%d, new_seqno=%d\n", __func__,
268583bbd5ebSRui Paulo 		    old_bf->bf_state.bfs_seqno, new_bf->bf_state.bfs_seqno);
268638962489SAdrian Chadd 	}
268738962489SAdrian Chadd 
268838962489SAdrian Chadd 	if (tid->tx_buf[cindex] != old_bf) {
268983bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
269083bbd5ebSRui Paulo 		    "%s: ath_buf pointer incorrect; "
269183bbd5ebSRui Paulo 		    " has m BA session may hang.\n", __func__);
269283bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
269383bbd5ebSRui Paulo 		    "%s: old bf=%p, new bf=%p\n", __func__, old_bf, new_bf);
269438962489SAdrian Chadd 	}
269538962489SAdrian Chadd 
269638962489SAdrian Chadd 	tid->tx_buf[cindex] = new_bf;
269738962489SAdrian Chadd }
269838962489SAdrian Chadd 
269938962489SAdrian Chadd /*
2700eb6f0de0SAdrian Chadd  * seq_start - left edge of BAW
2701eb6f0de0SAdrian Chadd  * seq_next - current/next sequence number to allocate
2702eb6f0de0SAdrian Chadd  *
2703eb6f0de0SAdrian Chadd  * Since the BAW status may be modified by both the ath task and
2704eb6f0de0SAdrian Chadd  * the net80211/ifnet contexts, the TID must be locked.
2705eb6f0de0SAdrian Chadd  */
2706eb6f0de0SAdrian Chadd static void
2707eb6f0de0SAdrian Chadd ath_tx_update_baw(struct ath_softc *sc, struct ath_node *an,
2708eb6f0de0SAdrian Chadd     struct ath_tid *tid, const struct ath_buf *bf)
2709eb6f0de0SAdrian Chadd {
2710eb6f0de0SAdrian Chadd 	int index, cindex;
2711eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
2712eb6f0de0SAdrian Chadd 	int seqno = SEQNO(bf->bf_state.bfs_seqno);
2713eb6f0de0SAdrian Chadd 
2714375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
2715eb6f0de0SAdrian Chadd 
2716eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
2717eb6f0de0SAdrian Chadd 	index  = ATH_BA_INDEX(tap->txa_start, seqno);
2718eb6f0de0SAdrian Chadd 	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
2719eb6f0de0SAdrian Chadd 
2720eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2721a108d2d6SAdrian Chadd 	    "%s: tid=%d, baw=%d:%d, seqno=%d, index=%d, cindex=%d, "
2722d4365d16SAdrian Chadd 	    "baw head=%d, tail=%d\n",
2723a108d2d6SAdrian Chadd 	    __func__, tid->tid, tap->txa_start, tap->txa_wnd, seqno, index,
2724eb6f0de0SAdrian Chadd 	    cindex, tid->baw_head, tid->baw_tail);
2725eb6f0de0SAdrian Chadd 
2726eb6f0de0SAdrian Chadd 	/*
2727eb6f0de0SAdrian Chadd 	 * If this occurs then we have a big problem - something else
2728eb6f0de0SAdrian Chadd 	 * has slid tap->txa_start along without updating the BAW
2729eb6f0de0SAdrian Chadd 	 * tracking start/end pointers. Thus the TX BAW state is now
2730eb6f0de0SAdrian Chadd 	 * completely busted.
2731eb6f0de0SAdrian Chadd 	 *
2732eb6f0de0SAdrian Chadd 	 * But for now, since I haven't yet fixed TDMA and buffer cloning,
2733eb6f0de0SAdrian Chadd 	 * it's quite possible that a cloned buffer is making its way
2734eb6f0de0SAdrian Chadd 	 * here and causing it to fire off. Disable TDMA for now.
2735eb6f0de0SAdrian Chadd 	 */
2736eb6f0de0SAdrian Chadd 	if (tid->tx_buf[cindex] != bf) {
273783bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2738eb6f0de0SAdrian Chadd 		    "%s: comp bf=%p, seq=%d; slot bf=%p, seqno=%d\n",
273983bbd5ebSRui Paulo 		    __func__, bf, SEQNO(bf->bf_state.bfs_seqno),
2740eb6f0de0SAdrian Chadd 		    tid->tx_buf[cindex],
27413527f6a9SAdrian Chadd 		    (tid->tx_buf[cindex] != NULL) ?
27423527f6a9SAdrian Chadd 		      SEQNO(tid->tx_buf[cindex]->bf_state.bfs_seqno) : -1);
2743eb6f0de0SAdrian Chadd 	}
2744eb6f0de0SAdrian Chadd 
2745eb6f0de0SAdrian Chadd 	tid->tx_buf[cindex] = NULL;
2746eb6f0de0SAdrian Chadd 
2747d4365d16SAdrian Chadd 	while (tid->baw_head != tid->baw_tail &&
2748d4365d16SAdrian Chadd 	    !tid->tx_buf[tid->baw_head]) {
2749eb6f0de0SAdrian Chadd 		INCR(tap->txa_start, IEEE80211_SEQ_RANGE);
2750eb6f0de0SAdrian Chadd 		INCR(tid->baw_head, ATH_TID_MAX_BUFS);
2751eb6f0de0SAdrian Chadd 	}
2752d4365d16SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2753d4365d16SAdrian Chadd 	    "%s: baw is now %d:%d, baw head=%d\n",
2754eb6f0de0SAdrian Chadd 	    __func__, tap->txa_start, tap->txa_wnd, tid->baw_head);
2755eb6f0de0SAdrian Chadd }
2756eb6f0de0SAdrian Chadd 
275722a3aee6SAdrian Chadd static void
275822a3aee6SAdrian Chadd ath_tx_leak_count_update(struct ath_softc *sc, struct ath_tid *tid,
275922a3aee6SAdrian Chadd     struct ath_buf *bf)
276022a3aee6SAdrian Chadd {
276122a3aee6SAdrian Chadd 	struct ieee80211_frame *wh;
276222a3aee6SAdrian Chadd 
276322a3aee6SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
276422a3aee6SAdrian Chadd 
276522a3aee6SAdrian Chadd 	if (tid->an->an_leak_count > 0) {
276622a3aee6SAdrian Chadd 		wh = mtod(bf->bf_m, struct ieee80211_frame *);
276722a3aee6SAdrian Chadd 
276822a3aee6SAdrian Chadd 		/*
276922a3aee6SAdrian Chadd 		 * Update MORE based on the software/net80211 queue states.
277022a3aee6SAdrian Chadd 		 */
277122a3aee6SAdrian Chadd 		if ((tid->an->an_stack_psq > 0)
277222a3aee6SAdrian Chadd 		    || (tid->an->an_swq_depth > 0))
277322a3aee6SAdrian Chadd 			wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
277422a3aee6SAdrian Chadd 		else
277522a3aee6SAdrian Chadd 			wh->i_fc[1] &= ~IEEE80211_FC1_MORE_DATA;
277622a3aee6SAdrian Chadd 
277722a3aee6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
277822a3aee6SAdrian Chadd 		    "%s: %6D: leak count = %d, psq=%d, swq=%d, MORE=%d\n",
277922a3aee6SAdrian Chadd 		    __func__,
278022a3aee6SAdrian Chadd 		    tid->an->an_node.ni_macaddr,
278122a3aee6SAdrian Chadd 		    ":",
278222a3aee6SAdrian Chadd 		    tid->an->an_leak_count,
278322a3aee6SAdrian Chadd 		    tid->an->an_stack_psq,
278422a3aee6SAdrian Chadd 		    tid->an->an_swq_depth,
278522a3aee6SAdrian Chadd 		    !! (wh->i_fc[1] & IEEE80211_FC1_MORE_DATA));
278622a3aee6SAdrian Chadd 
278722a3aee6SAdrian Chadd 		/*
278822a3aee6SAdrian Chadd 		 * Re-sync the underlying buffer.
278922a3aee6SAdrian Chadd 		 */
279022a3aee6SAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
279122a3aee6SAdrian Chadd 		    BUS_DMASYNC_PREWRITE);
279222a3aee6SAdrian Chadd 
279322a3aee6SAdrian Chadd 		tid->an->an_leak_count --;
279422a3aee6SAdrian Chadd 	}
279522a3aee6SAdrian Chadd }
279622a3aee6SAdrian Chadd 
279722a3aee6SAdrian Chadd static int
279822a3aee6SAdrian Chadd ath_tx_tid_can_tx_or_sched(struct ath_softc *sc, struct ath_tid *tid)
279922a3aee6SAdrian Chadd {
280022a3aee6SAdrian Chadd 
280122a3aee6SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
280222a3aee6SAdrian Chadd 
280322a3aee6SAdrian Chadd 	if (tid->an->an_leak_count > 0) {
280422a3aee6SAdrian Chadd 		return (1);
280522a3aee6SAdrian Chadd 	}
280622a3aee6SAdrian Chadd 	if (tid->paused)
280722a3aee6SAdrian Chadd 		return (0);
280822a3aee6SAdrian Chadd 	return (1);
280922a3aee6SAdrian Chadd }
281022a3aee6SAdrian Chadd 
2811eb6f0de0SAdrian Chadd /*
2812eb6f0de0SAdrian Chadd  * Mark the current node/TID as ready to TX.
2813eb6f0de0SAdrian Chadd  *
2814eb6f0de0SAdrian Chadd  * This is done to make it easy for the software scheduler to
2815eb6f0de0SAdrian Chadd  * find which nodes have data to send.
2816eb6f0de0SAdrian Chadd  *
2817eb6f0de0SAdrian Chadd  * The TXQ lock must be held.
2818eb6f0de0SAdrian Chadd  */
281922a3aee6SAdrian Chadd void
2820eb6f0de0SAdrian Chadd ath_tx_tid_sched(struct ath_softc *sc, struct ath_tid *tid)
2821eb6f0de0SAdrian Chadd {
2822eb6f0de0SAdrian Chadd 	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
2823eb6f0de0SAdrian Chadd 
2824375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
2825eb6f0de0SAdrian Chadd 
282622a3aee6SAdrian Chadd 	/*
282722a3aee6SAdrian Chadd 	 * If we are leaking out a frame to this destination
282822a3aee6SAdrian Chadd 	 * for PS-POLL, ensure that we allow scheduling to
282922a3aee6SAdrian Chadd 	 * occur.
283022a3aee6SAdrian Chadd 	 */
283122a3aee6SAdrian Chadd 	if (! ath_tx_tid_can_tx_or_sched(sc, tid))
2832eb6f0de0SAdrian Chadd 		return;		/* paused, can't schedule yet */
2833eb6f0de0SAdrian Chadd 
2834eb6f0de0SAdrian Chadd 	if (tid->sched)
2835eb6f0de0SAdrian Chadd 		return;		/* already scheduled */
2836eb6f0de0SAdrian Chadd 
2837eb6f0de0SAdrian Chadd 	tid->sched = 1;
2838eb6f0de0SAdrian Chadd 
283922a3aee6SAdrian Chadd #if 0
284022a3aee6SAdrian Chadd 	/*
284122a3aee6SAdrian Chadd 	 * If this is a sleeping node we're leaking to, given
284222a3aee6SAdrian Chadd 	 * it a higher priority.  This is so bad for QoS it hurts.
284322a3aee6SAdrian Chadd 	 */
284422a3aee6SAdrian Chadd 	if (tid->an->an_leak_count) {
284522a3aee6SAdrian Chadd 		TAILQ_INSERT_HEAD(&txq->axq_tidq, tid, axq_qelem);
284622a3aee6SAdrian Chadd 	} else {
284722a3aee6SAdrian Chadd 		TAILQ_INSERT_TAIL(&txq->axq_tidq, tid, axq_qelem);
284822a3aee6SAdrian Chadd 	}
284922a3aee6SAdrian Chadd #endif
285022a3aee6SAdrian Chadd 
285122a3aee6SAdrian Chadd 	/*
285222a3aee6SAdrian Chadd 	 * We can't do the above - it'll confuse the TXQ software
285322a3aee6SAdrian Chadd 	 * scheduler which will keep checking the _head_ TID
285422a3aee6SAdrian Chadd 	 * in the list to see if it has traffic.  If we queue
285522a3aee6SAdrian Chadd 	 * a TID to the head of the list and it doesn't transmit,
285622a3aee6SAdrian Chadd 	 * we'll check it again.
285722a3aee6SAdrian Chadd 	 *
285822a3aee6SAdrian Chadd 	 * So, get the rest of this leaking frames support working
285922a3aee6SAdrian Chadd 	 * and reliable first and _then_ optimise it so they're
286022a3aee6SAdrian Chadd 	 * pushed out in front of any other pending software
286122a3aee6SAdrian Chadd 	 * queued nodes.
286222a3aee6SAdrian Chadd 	 */
2863eb6f0de0SAdrian Chadd 	TAILQ_INSERT_TAIL(&txq->axq_tidq, tid, axq_qelem);
2864eb6f0de0SAdrian Chadd }
2865eb6f0de0SAdrian Chadd 
2866eb6f0de0SAdrian Chadd /*
2867eb6f0de0SAdrian Chadd  * Mark the current node as no longer needing to be polled for
2868eb6f0de0SAdrian Chadd  * TX packets.
2869eb6f0de0SAdrian Chadd  *
2870eb6f0de0SAdrian Chadd  * The TXQ lock must be held.
2871eb6f0de0SAdrian Chadd  */
2872eb6f0de0SAdrian Chadd static void
2873eb6f0de0SAdrian Chadd ath_tx_tid_unsched(struct ath_softc *sc, struct ath_tid *tid)
2874eb6f0de0SAdrian Chadd {
2875eb6f0de0SAdrian Chadd 	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
2876eb6f0de0SAdrian Chadd 
2877375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
2878eb6f0de0SAdrian Chadd 
2879eb6f0de0SAdrian Chadd 	if (tid->sched == 0)
2880eb6f0de0SAdrian Chadd 		return;
2881eb6f0de0SAdrian Chadd 
2882eb6f0de0SAdrian Chadd 	tid->sched = 0;
2883eb6f0de0SAdrian Chadd 	TAILQ_REMOVE(&txq->axq_tidq, tid, axq_qelem);
2884eb6f0de0SAdrian Chadd }
2885eb6f0de0SAdrian Chadd 
2886eb6f0de0SAdrian Chadd /*
2887eb6f0de0SAdrian Chadd  * Assign a sequence number manually to the given frame.
2888eb6f0de0SAdrian Chadd  *
2889eb6f0de0SAdrian Chadd  * This should only be called for A-MPDU TX frames.
2890eb6f0de0SAdrian Chadd  */
2891a108d2d6SAdrian Chadd static ieee80211_seq
2892eb6f0de0SAdrian Chadd ath_tx_tid_seqno_assign(struct ath_softc *sc, struct ieee80211_node *ni,
2893eb6f0de0SAdrian Chadd     struct ath_buf *bf, struct mbuf *m0)
2894eb6f0de0SAdrian Chadd {
2895eb6f0de0SAdrian Chadd 	struct ieee80211_frame *wh;
2896eb6f0de0SAdrian Chadd 	int tid, pri;
2897eb6f0de0SAdrian Chadd 	ieee80211_seq seqno;
2898eb6f0de0SAdrian Chadd 	uint8_t subtype;
2899eb6f0de0SAdrian Chadd 
2900eb6f0de0SAdrian Chadd 	/* TID lookup */
2901eb6f0de0SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
2902eb6f0de0SAdrian Chadd 	pri = M_WME_GETAC(m0);			/* honor classification */
2903eb6f0de0SAdrian Chadd 	tid = WME_AC_TO_TID(pri);
2904a108d2d6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: pri=%d, tid=%d, qos has seq=%d\n",
2905a108d2d6SAdrian Chadd 	    __func__, pri, tid, IEEE80211_QOS_HAS_SEQ(wh));
2906eb6f0de0SAdrian Chadd 
2907eb6f0de0SAdrian Chadd 	/* XXX Is it a control frame? Ignore */
2908eb6f0de0SAdrian Chadd 
2909eb6f0de0SAdrian Chadd 	/* Does the packet require a sequence number? */
2910eb6f0de0SAdrian Chadd 	if (! IEEE80211_QOS_HAS_SEQ(wh))
2911eb6f0de0SAdrian Chadd 		return -1;
2912eb6f0de0SAdrian Chadd 
2913375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
29147561cb5cSAdrian Chadd 
2915eb6f0de0SAdrian Chadd 	/*
2916eb6f0de0SAdrian Chadd 	 * Is it a QOS NULL Data frame? Give it a sequence number from
2917eb6f0de0SAdrian Chadd 	 * the default TID (IEEE80211_NONQOS_TID.)
2918eb6f0de0SAdrian Chadd 	 *
2919eb6f0de0SAdrian Chadd 	 * The RX path of everything I've looked at doesn't include the NULL
2920eb6f0de0SAdrian Chadd 	 * data frame sequence number in the aggregation state updates, so
2921eb6f0de0SAdrian Chadd 	 * assigning it a sequence number there will cause a BAW hole on the
2922eb6f0de0SAdrian Chadd 	 * RX side.
2923eb6f0de0SAdrian Chadd 	 */
2924eb6f0de0SAdrian Chadd 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
2925eb6f0de0SAdrian Chadd 	if (subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL) {
29267561cb5cSAdrian Chadd 		/* XXX no locking for this TID? This is a bit of a problem. */
2927eb6f0de0SAdrian Chadd 		seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID];
2928eb6f0de0SAdrian Chadd 		INCR(ni->ni_txseqs[IEEE80211_NONQOS_TID], IEEE80211_SEQ_RANGE);
2929eb6f0de0SAdrian Chadd 	} else {
2930eb6f0de0SAdrian Chadd 		/* Manually assign sequence number */
2931eb6f0de0SAdrian Chadd 		seqno = ni->ni_txseqs[tid];
2932eb6f0de0SAdrian Chadd 		INCR(ni->ni_txseqs[tid], IEEE80211_SEQ_RANGE);
2933eb6f0de0SAdrian Chadd 	}
2934eb6f0de0SAdrian Chadd 	*(uint16_t *)&wh->i_seq[0] = htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
2935eb6f0de0SAdrian Chadd 	M_SEQNO_SET(m0, seqno);
2936eb6f0de0SAdrian Chadd 
2937eb6f0de0SAdrian Chadd 	/* Return so caller can do something with it if needed */
2938a108d2d6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s:  -> seqno=%d\n", __func__, seqno);
2939eb6f0de0SAdrian Chadd 	return seqno;
2940eb6f0de0SAdrian Chadd }
2941eb6f0de0SAdrian Chadd 
2942eb6f0de0SAdrian Chadd /*
2943eb6f0de0SAdrian Chadd  * Attempt to direct dispatch an aggregate frame to hardware.
2944eb6f0de0SAdrian Chadd  * If the frame is out of BAW, queue.
2945eb6f0de0SAdrian Chadd  * Otherwise, schedule it as a single frame.
2946eb6f0de0SAdrian Chadd  */
2947eb6f0de0SAdrian Chadd static void
294846634305SAdrian Chadd ath_tx_xmit_aggr(struct ath_softc *sc, struct ath_node *an,
294946634305SAdrian Chadd     struct ath_txq *txq, struct ath_buf *bf)
2950eb6f0de0SAdrian Chadd {
2951eb6f0de0SAdrian Chadd 	struct ath_tid *tid = &an->an_tid[bf->bf_state.bfs_tid];
2952eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
2953eb6f0de0SAdrian Chadd 
2954375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
2955eb6f0de0SAdrian Chadd 
2956eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
2957eb6f0de0SAdrian Chadd 
2958eb6f0de0SAdrian Chadd 	/* paused? queue */
295922a3aee6SAdrian Chadd 	if (! ath_tx_tid_can_tx_or_sched(sc, tid)) {
29603e6cc97fSAdrian Chadd 		ATH_TID_INSERT_HEAD(tid, bf, bf_list);
29610f04c5a2SAdrian Chadd 		/* XXX don't sched - we're paused! */
2962eb6f0de0SAdrian Chadd 		return;
2963eb6f0de0SAdrian Chadd 	}
2964eb6f0de0SAdrian Chadd 
2965eb6f0de0SAdrian Chadd 	/* outside baw? queue */
2966eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_dobaw &&
2967eb6f0de0SAdrian Chadd 	    (! BAW_WITHIN(tap->txa_start, tap->txa_wnd,
2968eb6f0de0SAdrian Chadd 	    SEQNO(bf->bf_state.bfs_seqno)))) {
29693e6cc97fSAdrian Chadd 		ATH_TID_INSERT_HEAD(tid, bf, bf_list);
2970eb6f0de0SAdrian Chadd 		ath_tx_tid_sched(sc, tid);
2971eb6f0de0SAdrian Chadd 		return;
2972eb6f0de0SAdrian Chadd 	}
2973eb6f0de0SAdrian Chadd 
29742a9f83afSAdrian Chadd 	/*
29752a9f83afSAdrian Chadd 	 * This is a temporary check and should be removed once
29762a9f83afSAdrian Chadd 	 * all the relevant code paths have been fixed.
29772a9f83afSAdrian Chadd 	 *
29782a9f83afSAdrian Chadd 	 * During aggregate retries, it's possible that the head
29792a9f83afSAdrian Chadd 	 * frame will fail (which has the bfs_aggr and bfs_nframes
29802a9f83afSAdrian Chadd 	 * fields set for said aggregate) and will be retried as
29812a9f83afSAdrian Chadd 	 * a single frame.  In this instance, the values should
29822a9f83afSAdrian Chadd 	 * be reset or the completion code will get upset with you.
29832a9f83afSAdrian Chadd 	 */
29842a9f83afSAdrian Chadd 	if (bf->bf_state.bfs_aggr != 0 || bf->bf_state.bfs_nframes > 1) {
2985b372f122SRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
298683bbd5ebSRui Paulo 		    "%s: bfs_aggr=%d, bfs_nframes=%d\n", __func__,
298783bbd5ebSRui Paulo 		    bf->bf_state.bfs_aggr, bf->bf_state.bfs_nframes);
29882a9f83afSAdrian Chadd 		bf->bf_state.bfs_aggr = 0;
29892a9f83afSAdrian Chadd 		bf->bf_state.bfs_nframes = 1;
29902a9f83afSAdrian Chadd 	}
29912a9f83afSAdrian Chadd 
29924e81f27cSAdrian Chadd 	/* Update CLRDMASK just before this frame is queued */
29934e81f27cSAdrian Chadd 	ath_tx_update_clrdmask(sc, tid, bf);
29944e81f27cSAdrian Chadd 
2995eb6f0de0SAdrian Chadd 	/* Direct dispatch to hardware */
2996eb6f0de0SAdrian Chadd 	ath_tx_do_ratelookup(sc, bf);
2997e2e4a2c2SAdrian Chadd 	ath_tx_calc_duration(sc, bf);
2998e2e4a2c2SAdrian Chadd 	ath_tx_calc_protection(sc, bf);
2999eb6f0de0SAdrian Chadd 	ath_tx_set_rtscts(sc, bf);
3000e2e4a2c2SAdrian Chadd 	ath_tx_rate_fill_rcflags(sc, bf);
3001eb6f0de0SAdrian Chadd 	ath_tx_setds(sc, bf);
3002eb6f0de0SAdrian Chadd 
3003eb6f0de0SAdrian Chadd 	/* Statistics */
3004eb6f0de0SAdrian Chadd 	sc->sc_aggr_stats.aggr_low_hwq_single_pkt++;
3005eb6f0de0SAdrian Chadd 
3006eb6f0de0SAdrian Chadd 	/* Track per-TID hardware queue depth correctly */
3007eb6f0de0SAdrian Chadd 	tid->hwq_depth++;
3008eb6f0de0SAdrian Chadd 
3009eb6f0de0SAdrian Chadd 	/* Add to BAW */
3010eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_dobaw) {
3011eb6f0de0SAdrian Chadd 		ath_tx_addto_baw(sc, an, tid, bf);
3012eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_addedbaw = 1;
3013eb6f0de0SAdrian Chadd 	}
3014eb6f0de0SAdrian Chadd 
3015eb6f0de0SAdrian Chadd 	/* Set completion handler, multi-frame aggregate or not */
3016eb6f0de0SAdrian Chadd 	bf->bf_comp = ath_tx_aggr_comp;
3017eb6f0de0SAdrian Chadd 
301822a3aee6SAdrian Chadd 	/*
301922a3aee6SAdrian Chadd 	 * Update the current leak count if
302022a3aee6SAdrian Chadd 	 * we're leaking frames; and set the
302122a3aee6SAdrian Chadd 	 * MORE flag as appropriate.
302222a3aee6SAdrian Chadd 	 */
302322a3aee6SAdrian Chadd 	ath_tx_leak_count_update(sc, tid, bf);
302422a3aee6SAdrian Chadd 
3025eb6f0de0SAdrian Chadd 	/* Hand off to hardware */
3026eb6f0de0SAdrian Chadd 	ath_tx_handoff(sc, txq, bf);
3027eb6f0de0SAdrian Chadd }
3028eb6f0de0SAdrian Chadd 
3029eb6f0de0SAdrian Chadd /*
3030eb6f0de0SAdrian Chadd  * Attempt to send the packet.
3031eb6f0de0SAdrian Chadd  * If the queue isn't busy, direct-dispatch.
3032eb6f0de0SAdrian Chadd  * If the queue is busy enough, queue the given packet on the
3033eb6f0de0SAdrian Chadd  *  relevant software queue.
3034eb6f0de0SAdrian Chadd  */
3035eb6f0de0SAdrian Chadd void
303622a3aee6SAdrian Chadd ath_tx_swq(struct ath_softc *sc, struct ieee80211_node *ni,
303722a3aee6SAdrian Chadd     struct ath_txq *txq, int queue_to_head, struct ath_buf *bf)
3038eb6f0de0SAdrian Chadd {
3039eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
3040eb6f0de0SAdrian Chadd 	struct ieee80211_frame *wh;
3041eb6f0de0SAdrian Chadd 	struct ath_tid *atid;
3042eb6f0de0SAdrian Chadd 	int pri, tid;
3043eb6f0de0SAdrian Chadd 	struct mbuf *m0 = bf->bf_m;
3044eb6f0de0SAdrian Chadd 
3045375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
30467561cb5cSAdrian Chadd 
3047eb6f0de0SAdrian Chadd 	/* Fetch the TID - non-QoS frames get assigned to TID 16 */
3048eb6f0de0SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
3049eb6f0de0SAdrian Chadd 	pri = ath_tx_getac(sc, m0);
3050eb6f0de0SAdrian Chadd 	tid = ath_tx_gettid(sc, m0);
3051eb6f0de0SAdrian Chadd 	atid = &an->an_tid[tid];
3052eb6f0de0SAdrian Chadd 
3053a108d2d6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bf=%p, pri=%d, tid=%d, qos=%d\n",
3054a108d2d6SAdrian Chadd 	    __func__, bf, pri, tid, IEEE80211_QOS_HAS_SEQ(wh));
3055eb6f0de0SAdrian Chadd 
3056eb6f0de0SAdrian Chadd 	/* Set local packet state, used to queue packets to hardware */
305746634305SAdrian Chadd 	/* XXX potentially duplicate info, re-check */
3058eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_tid = tid;
3059fc56c9c5SAdrian Chadd 	bf->bf_state.bfs_tx_queue = txq->axq_qnum;
3060eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_pri = pri;
3061eb6f0de0SAdrian Chadd 
3062eb6f0de0SAdrian Chadd 	/*
3063eb6f0de0SAdrian Chadd 	 * If the hardware queue isn't busy, queue it directly.
3064eb6f0de0SAdrian Chadd 	 * If the hardware queue is busy, queue it.
3065eb6f0de0SAdrian Chadd 	 * If the TID is paused or the traffic it outside BAW, software
3066eb6f0de0SAdrian Chadd 	 * queue it.
306722a3aee6SAdrian Chadd 	 *
306822a3aee6SAdrian Chadd 	 * If the node is in power-save and we're leaking a frame,
306922a3aee6SAdrian Chadd 	 * leak a single frame.
3070eb6f0de0SAdrian Chadd 	 */
307122a3aee6SAdrian Chadd 	if (! ath_tx_tid_can_tx_or_sched(sc, atid)) {
3072eb6f0de0SAdrian Chadd 		/* TID is paused, queue */
3073a108d2d6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: paused\n", __func__);
307422a3aee6SAdrian Chadd 		/*
307522a3aee6SAdrian Chadd 		 * If the caller requested that it be sent at a high
307622a3aee6SAdrian Chadd 		 * priority, queue it at the head of the list.
307722a3aee6SAdrian Chadd 		 */
307822a3aee6SAdrian Chadd 		if (queue_to_head)
307922a3aee6SAdrian Chadd 			ATH_TID_INSERT_HEAD(atid, bf, bf_list);
308022a3aee6SAdrian Chadd 		else
30813e6cc97fSAdrian Chadd 			ATH_TID_INSERT_TAIL(atid, bf, bf_list);
3082eb6f0de0SAdrian Chadd 	} else if (ath_tx_ampdu_pending(sc, an, tid)) {
3083eb6f0de0SAdrian Chadd 		/* AMPDU pending; queue */
3084a108d2d6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: pending\n", __func__);
30853e6cc97fSAdrian Chadd 		ATH_TID_INSERT_TAIL(atid, bf, bf_list);
3086eb6f0de0SAdrian Chadd 		/* XXX sched? */
3087eb6f0de0SAdrian Chadd 	} else if (ath_tx_ampdu_running(sc, an, tid)) {
3088eb6f0de0SAdrian Chadd 		/* AMPDU running, attempt direct dispatch if possible */
308939f24578SAdrian Chadd 
309039f24578SAdrian Chadd 		/*
309139f24578SAdrian Chadd 		 * Always queue the frame to the tail of the list.
309239f24578SAdrian Chadd 		 */
30933e6cc97fSAdrian Chadd 		ATH_TID_INSERT_TAIL(atid, bf, bf_list);
309439f24578SAdrian Chadd 
309539f24578SAdrian Chadd 		/*
309639f24578SAdrian Chadd 		 * If the hardware queue isn't busy, direct dispatch
309739f24578SAdrian Chadd 		 * the head frame in the list.  Don't schedule the
309839f24578SAdrian Chadd 		 * TID - let it build some more frames first?
309939f24578SAdrian Chadd 		 *
310072910f03SAdrian Chadd 		 * When running A-MPDU, always just check the hardware
310172910f03SAdrian Chadd 		 * queue depth against the aggregate frame limit.
310272910f03SAdrian Chadd 		 * We don't want to burst a large number of single frames
310372910f03SAdrian Chadd 		 * out to the hardware; we want to aggressively hold back.
310472910f03SAdrian Chadd 		 *
310539f24578SAdrian Chadd 		 * Otherwise, schedule the TID.
310639f24578SAdrian Chadd 		 */
310772910f03SAdrian Chadd 		/* XXX TXQ locking */
310872910f03SAdrian Chadd 		if (txq->axq_depth + txq->fifo.axq_depth < sc->sc_hwq_limit_aggr) {
31093e6cc97fSAdrian Chadd 			bf = ATH_TID_FIRST(atid);
31103e6cc97fSAdrian Chadd 			ATH_TID_REMOVE(atid, bf, bf_list);
31112a9f83afSAdrian Chadd 
31122a9f83afSAdrian Chadd 			/*
31132a9f83afSAdrian Chadd 			 * Ensure it's definitely treated as a non-AMPDU
31142a9f83afSAdrian Chadd 			 * frame - this information may have been left
31152a9f83afSAdrian Chadd 			 * over from a previous attempt.
31162a9f83afSAdrian Chadd 			 */
31172a9f83afSAdrian Chadd 			bf->bf_state.bfs_aggr = 0;
31182a9f83afSAdrian Chadd 			bf->bf_state.bfs_nframes = 1;
31192a9f83afSAdrian Chadd 
31202a9f83afSAdrian Chadd 			/* Queue to the hardware */
312146634305SAdrian Chadd 			ath_tx_xmit_aggr(sc, an, txq, bf);
3122a108d2d6SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX,
3123a108d2d6SAdrian Chadd 			    "%s: xmit_aggr\n",
3124a108d2d6SAdrian Chadd 			    __func__);
3125d4365d16SAdrian Chadd 		} else {
3126d4365d16SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX,
3127a108d2d6SAdrian Chadd 			    "%s: ampdu; swq'ing\n",
3128a108d2d6SAdrian Chadd 			    __func__);
312903682514SAdrian Chadd 
3130eb6f0de0SAdrian Chadd 			ath_tx_tid_sched(sc, atid);
3131eb6f0de0SAdrian Chadd 		}
313272910f03SAdrian Chadd 	/*
313372910f03SAdrian Chadd 	 * If we're not doing A-MPDU, be prepared to direct dispatch
313472910f03SAdrian Chadd 	 * up to both limits if possible.  This particular corner
313572910f03SAdrian Chadd 	 * case may end up with packet starvation between aggregate
313672910f03SAdrian Chadd 	 * traffic and non-aggregate traffic: we wnat to ensure
313772910f03SAdrian Chadd 	 * that non-aggregate stations get a few frames queued to the
313872910f03SAdrian Chadd 	 * hardware before the aggregate station(s) get their chance.
313972910f03SAdrian Chadd 	 *
314072910f03SAdrian Chadd 	 * So if you only ever see a couple of frames direct dispatched
314172910f03SAdrian Chadd 	 * to the hardware from a non-AMPDU client, check both here
314272910f03SAdrian Chadd 	 * and in the software queue dispatcher to ensure that those
314372910f03SAdrian Chadd 	 * non-AMPDU stations get a fair chance to transmit.
314472910f03SAdrian Chadd 	 */
314572910f03SAdrian Chadd 	/* XXX TXQ locking */
314672910f03SAdrian Chadd 	} else if ((txq->axq_depth + txq->fifo.axq_depth < sc->sc_hwq_limit_nonaggr) &&
314772910f03SAdrian Chadd 		    (txq->axq_aggr_depth < sc->sc_hwq_limit_aggr)) {
3148eb6f0de0SAdrian Chadd 		/* AMPDU not running, attempt direct dispatch */
3149a108d2d6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: xmit_normal\n", __func__);
31500a544719SAdrian Chadd 		/* See if clrdmask needs to be set */
31510a544719SAdrian Chadd 		ath_tx_update_clrdmask(sc, atid, bf);
315222a3aee6SAdrian Chadd 
315322a3aee6SAdrian Chadd 		/*
315422a3aee6SAdrian Chadd 		 * Update the current leak count if
315522a3aee6SAdrian Chadd 		 * we're leaking frames; and set the
315622a3aee6SAdrian Chadd 		 * MORE flag as appropriate.
315722a3aee6SAdrian Chadd 		 */
315822a3aee6SAdrian Chadd 		ath_tx_leak_count_update(sc, atid, bf);
315922a3aee6SAdrian Chadd 
316022a3aee6SAdrian Chadd 		/*
316122a3aee6SAdrian Chadd 		 * Dispatch the frame.
316222a3aee6SAdrian Chadd 		 */
3163eb6f0de0SAdrian Chadd 		ath_tx_xmit_normal(sc, txq, bf);
3164eb6f0de0SAdrian Chadd 	} else {
3165eb6f0de0SAdrian Chadd 		/* Busy; queue */
3166a108d2d6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: swq'ing\n", __func__);
31673e6cc97fSAdrian Chadd 		ATH_TID_INSERT_TAIL(atid, bf, bf_list);
3168eb6f0de0SAdrian Chadd 		ath_tx_tid_sched(sc, atid);
3169eb6f0de0SAdrian Chadd 	}
3170eb6f0de0SAdrian Chadd }
3171eb6f0de0SAdrian Chadd 
3172eb6f0de0SAdrian Chadd /*
31734f25ddbbSAdrian Chadd  * Only set the clrdmask bit if none of the nodes are currently
31744f25ddbbSAdrian Chadd  * filtered.
31754f25ddbbSAdrian Chadd  *
31764f25ddbbSAdrian Chadd  * XXX TODO: go through all the callers and check to see
31774f25ddbbSAdrian Chadd  * which are being called in the context of looping over all
31784f25ddbbSAdrian Chadd  * TIDs (eg, if all tids are being paused, resumed, etc.)
31794f25ddbbSAdrian Chadd  * That'll avoid O(n^2) complexity here.
31804f25ddbbSAdrian Chadd  */
31814f25ddbbSAdrian Chadd static void
31824f25ddbbSAdrian Chadd ath_tx_set_clrdmask(struct ath_softc *sc, struct ath_node *an)
31834f25ddbbSAdrian Chadd {
31844f25ddbbSAdrian Chadd 	int i;
31854f25ddbbSAdrian Chadd 
31864f25ddbbSAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
31874f25ddbbSAdrian Chadd 
31884f25ddbbSAdrian Chadd 	for (i = 0; i < IEEE80211_TID_SIZE; i++) {
31894f25ddbbSAdrian Chadd 		if (an->an_tid[i].isfiltered == 1)
3190f74d878fSAdrian Chadd 			return;
31914f25ddbbSAdrian Chadd 	}
31924f25ddbbSAdrian Chadd 	an->clrdmask = 1;
31934f25ddbbSAdrian Chadd }
31944f25ddbbSAdrian Chadd 
31954f25ddbbSAdrian Chadd /*
3196eb6f0de0SAdrian Chadd  * Configure the per-TID node state.
3197eb6f0de0SAdrian Chadd  *
3198eb6f0de0SAdrian Chadd  * This likely belongs in if_ath_node.c but I can't think of anywhere
3199eb6f0de0SAdrian Chadd  * else to put it just yet.
3200eb6f0de0SAdrian Chadd  *
3201eb6f0de0SAdrian Chadd  * This sets up the SLISTs and the mutex as appropriate.
3202eb6f0de0SAdrian Chadd  */
3203eb6f0de0SAdrian Chadd void
3204eb6f0de0SAdrian Chadd ath_tx_tid_init(struct ath_softc *sc, struct ath_node *an)
3205eb6f0de0SAdrian Chadd {
3206eb6f0de0SAdrian Chadd 	int i, j;
3207eb6f0de0SAdrian Chadd 	struct ath_tid *atid;
3208eb6f0de0SAdrian Chadd 
3209eb6f0de0SAdrian Chadd 	for (i = 0; i < IEEE80211_TID_SIZE; i++) {
3210eb6f0de0SAdrian Chadd 		atid = &an->an_tid[i];
3211f1bc738eSAdrian Chadd 
3212f1bc738eSAdrian Chadd 		/* XXX now with this bzer(), is the field 0'ing needed? */
3213f1bc738eSAdrian Chadd 		bzero(atid, sizeof(*atid));
3214f1bc738eSAdrian Chadd 
32153e6cc97fSAdrian Chadd 		TAILQ_INIT(&atid->tid_q);
32163e6cc97fSAdrian Chadd 		TAILQ_INIT(&atid->filtq.tid_q);
3217eb6f0de0SAdrian Chadd 		atid->tid = i;
3218eb6f0de0SAdrian Chadd 		atid->an = an;
3219eb6f0de0SAdrian Chadd 		for (j = 0; j < ATH_TID_MAX_BUFS; j++)
3220eb6f0de0SAdrian Chadd 			atid->tx_buf[j] = NULL;
3221eb6f0de0SAdrian Chadd 		atid->baw_head = atid->baw_tail = 0;
3222eb6f0de0SAdrian Chadd 		atid->paused = 0;
3223eb6f0de0SAdrian Chadd 		atid->sched = 0;
3224eb6f0de0SAdrian Chadd 		atid->hwq_depth = 0;
3225eb6f0de0SAdrian Chadd 		atid->cleanup_inprogress = 0;
3226eb6f0de0SAdrian Chadd 		if (i == IEEE80211_NONQOS_TID)
32277403d1b9SAdrian Chadd 			atid->ac = ATH_NONQOS_TID_AC;
3228eb6f0de0SAdrian Chadd 		else
3229eb6f0de0SAdrian Chadd 			atid->ac = TID_TO_WME_AC(i);
3230eb6f0de0SAdrian Chadd 	}
32314f25ddbbSAdrian Chadd 	an->clrdmask = 1;	/* Always start by setting this bit */
3232eb6f0de0SAdrian Chadd }
3233eb6f0de0SAdrian Chadd 
3234eb6f0de0SAdrian Chadd /*
3235eb6f0de0SAdrian Chadd  * Pause the current TID. This stops packets from being transmitted
3236eb6f0de0SAdrian Chadd  * on it.
3237eb6f0de0SAdrian Chadd  *
3238eb6f0de0SAdrian Chadd  * Since this is also called from upper layers as well as the driver,
3239eb6f0de0SAdrian Chadd  * it will get the TID lock.
3240eb6f0de0SAdrian Chadd  */
3241eb6f0de0SAdrian Chadd static void
3242eb6f0de0SAdrian Chadd ath_tx_tid_pause(struct ath_softc *sc, struct ath_tid *tid)
3243eb6f0de0SAdrian Chadd {
324488b3d483SAdrian Chadd 
3245375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
3246eb6f0de0SAdrian Chadd 	tid->paused++;
3247eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: paused = %d\n",
3248eb6f0de0SAdrian Chadd 	    __func__, tid->paused);
3249eb6f0de0SAdrian Chadd }
3250eb6f0de0SAdrian Chadd 
3251eb6f0de0SAdrian Chadd /*
3252eb6f0de0SAdrian Chadd  * Unpause the current TID, and schedule it if needed.
3253eb6f0de0SAdrian Chadd  */
3254eb6f0de0SAdrian Chadd static void
3255eb6f0de0SAdrian Chadd ath_tx_tid_resume(struct ath_softc *sc, struct ath_tid *tid)
3256eb6f0de0SAdrian Chadd {
3257375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
3258eb6f0de0SAdrian Chadd 
3259dff5bdf4SAdrian Chadd 	/*
3260dff5bdf4SAdrian Chadd 	 * There's some odd places where ath_tx_tid_resume() is called
3261dff5bdf4SAdrian Chadd 	 * when it shouldn't be; this works around that particular issue
3262dff5bdf4SAdrian Chadd 	 * until it's actually resolved.
3263dff5bdf4SAdrian Chadd 	 */
3264dff5bdf4SAdrian Chadd 	if (tid->paused == 0) {
326583bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
326683bbd5ebSRui Paulo 		    "%s: %6D: paused=0?\n", __func__,
326783bbd5ebSRui Paulo 		    tid->an->an_node.ni_macaddr, ":");
3268dff5bdf4SAdrian Chadd 	} else {
3269eb6f0de0SAdrian Chadd 		tid->paused--;
3270dff5bdf4SAdrian Chadd 	}
3271eb6f0de0SAdrian Chadd 
3272eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: unpaused = %d\n",
3273eb6f0de0SAdrian Chadd 	    __func__, tid->paused);
3274eb6f0de0SAdrian Chadd 
32750eb81626SAdrian Chadd 	if (tid->paused)
3276eb6f0de0SAdrian Chadd 		return;
32770eb81626SAdrian Chadd 
32780eb81626SAdrian Chadd 	/*
32790eb81626SAdrian Chadd 	 * Override the clrdmask configuration for the next frame
32800eb81626SAdrian Chadd 	 * from this TID, just to get the ball rolling.
32810eb81626SAdrian Chadd 	 */
32824f25ddbbSAdrian Chadd 	ath_tx_set_clrdmask(sc, tid->an);
32830eb81626SAdrian Chadd 
32840eb81626SAdrian Chadd 	if (tid->axq_depth == 0)
32850eb81626SAdrian Chadd 		return;
3286eb6f0de0SAdrian Chadd 
3287f1bc738eSAdrian Chadd 	/* XXX isfiltered shouldn't ever be 0 at this point */
3288f1bc738eSAdrian Chadd 	if (tid->isfiltered == 1) {
328983bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: filtered?!\n",
329083bbd5ebSRui Paulo 		    __func__);
3291f1bc738eSAdrian Chadd 		return;
3292f1bc738eSAdrian Chadd 	}
3293f1bc738eSAdrian Chadd 
3294eb6f0de0SAdrian Chadd 	ath_tx_tid_sched(sc, tid);
329521bca442SAdrian Chadd 
329621bca442SAdrian Chadd 	/*
329721bca442SAdrian Chadd 	 * Queue the software TX scheduler.
329821bca442SAdrian Chadd 	 */
329921bca442SAdrian Chadd 	ath_tx_swq_kick(sc);
3300eb6f0de0SAdrian Chadd }
3301eb6f0de0SAdrian Chadd 
3302eb6f0de0SAdrian Chadd /*
3303f1bc738eSAdrian Chadd  * Add the given ath_buf to the TID filtered frame list.
3304f1bc738eSAdrian Chadd  * This requires the TID be filtered.
3305f1bc738eSAdrian Chadd  */
3306f1bc738eSAdrian Chadd static void
3307f1bc738eSAdrian Chadd ath_tx_tid_filt_addbuf(struct ath_softc *sc, struct ath_tid *tid,
3308f1bc738eSAdrian Chadd     struct ath_buf *bf)
3309f1bc738eSAdrian Chadd {
3310f1bc738eSAdrian Chadd 
3311375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
3312375307d4SAdrian Chadd 
3313f1bc738eSAdrian Chadd 	if (!tid->isfiltered)
331483bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT, "%s: not filtered?!\n",
331583bbd5ebSRui Paulo 		    __func__);
3316f1bc738eSAdrian Chadd 
3317f1bc738eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_FILT, "%s: bf=%p\n", __func__, bf);
3318f1bc738eSAdrian Chadd 
3319f1bc738eSAdrian Chadd 	/* Set the retry bit and bump the retry counter */
3320f1bc738eSAdrian Chadd 	ath_tx_set_retry(sc, bf);
3321f1bc738eSAdrian Chadd 	sc->sc_stats.ast_tx_swfiltered++;
3322f1bc738eSAdrian Chadd 
332313aa9ee5SAdrian Chadd 	ATH_TID_FILT_INSERT_TAIL(tid, bf, bf_list);
3324f1bc738eSAdrian Chadd }
3325f1bc738eSAdrian Chadd 
3326f1bc738eSAdrian Chadd /*
3327f1bc738eSAdrian Chadd  * Handle a completed filtered frame from the given TID.
3328f1bc738eSAdrian Chadd  * This just enables/pauses the filtered frame state if required
3329f1bc738eSAdrian Chadd  * and appends the filtered frame to the filtered queue.
3330f1bc738eSAdrian Chadd  */
3331f1bc738eSAdrian Chadd static void
3332f1bc738eSAdrian Chadd ath_tx_tid_filt_comp_buf(struct ath_softc *sc, struct ath_tid *tid,
3333f1bc738eSAdrian Chadd     struct ath_buf *bf)
3334f1bc738eSAdrian Chadd {
3335f1bc738eSAdrian Chadd 
3336375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
3337f1bc738eSAdrian Chadd 
3338f1bc738eSAdrian Chadd 	if (! tid->isfiltered) {
3339f1bc738eSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT, "%s: filter transition\n",
3340f1bc738eSAdrian Chadd 		    __func__);
3341f1bc738eSAdrian Chadd 		tid->isfiltered = 1;
3342f1bc738eSAdrian Chadd 		ath_tx_tid_pause(sc, tid);
3343f1bc738eSAdrian Chadd 	}
3344f1bc738eSAdrian Chadd 
3345f1bc738eSAdrian Chadd 	/* Add the frame to the filter queue */
3346f1bc738eSAdrian Chadd 	ath_tx_tid_filt_addbuf(sc, tid, bf);
3347f1bc738eSAdrian Chadd }
3348f1bc738eSAdrian Chadd 
3349f1bc738eSAdrian Chadd /*
3350f1bc738eSAdrian Chadd  * Complete the filtered frame TX completion.
3351f1bc738eSAdrian Chadd  *
3352f1bc738eSAdrian Chadd  * If there are no more frames in the hardware queue, unpause/unfilter
3353f1bc738eSAdrian Chadd  * the TID if applicable.  Otherwise we will wait for a node PS transition
3354f1bc738eSAdrian Chadd  * to unfilter.
3355f1bc738eSAdrian Chadd  */
3356f1bc738eSAdrian Chadd static void
3357f1bc738eSAdrian Chadd ath_tx_tid_filt_comp_complete(struct ath_softc *sc, struct ath_tid *tid)
3358f1bc738eSAdrian Chadd {
3359f1bc738eSAdrian Chadd 	struct ath_buf *bf;
3360*a3fd3b14SAdrian Chadd 	int do_resume = 0;
3361f1bc738eSAdrian Chadd 
3362375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
3363f1bc738eSAdrian Chadd 
3364f1bc738eSAdrian Chadd 	if (tid->hwq_depth != 0)
3365f1bc738eSAdrian Chadd 		return;
3366f1bc738eSAdrian Chadd 
3367f1bc738eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_FILT, "%s: hwq=0, transition back\n",
3368f1bc738eSAdrian Chadd 	    __func__);
3369*a3fd3b14SAdrian Chadd 	if (tid->isfiltered == 1) {
3370f1bc738eSAdrian Chadd 		tid->isfiltered = 0;
3371*a3fd3b14SAdrian Chadd 		do_resume = 1;
3372*a3fd3b14SAdrian Chadd 	}
3373*a3fd3b14SAdrian Chadd 
33744f25ddbbSAdrian Chadd 	/* XXX ath_tx_tid_resume() also calls ath_tx_set_clrdmask()! */
33754f25ddbbSAdrian Chadd 	ath_tx_set_clrdmask(sc, tid->an);
3376f1bc738eSAdrian Chadd 
3377f1bc738eSAdrian Chadd 	/* XXX this is really quite inefficient */
337813aa9ee5SAdrian Chadd 	while ((bf = ATH_TID_FILT_LAST(tid, ath_bufhead_s)) != NULL) {
337913aa9ee5SAdrian Chadd 		ATH_TID_FILT_REMOVE(tid, bf, bf_list);
33803e6cc97fSAdrian Chadd 		ATH_TID_INSERT_HEAD(tid, bf, bf_list);
3381f1bc738eSAdrian Chadd 	}
3382f1bc738eSAdrian Chadd 
3383*a3fd3b14SAdrian Chadd 	if (do_resume)
3384f1bc738eSAdrian Chadd 		ath_tx_tid_resume(sc, tid);
3385f1bc738eSAdrian Chadd }
3386f1bc738eSAdrian Chadd 
3387f1bc738eSAdrian Chadd /*
3388f1bc738eSAdrian Chadd  * Called when a single (aggregate or otherwise) frame is completed.
3389f1bc738eSAdrian Chadd  *
3390f1bc738eSAdrian Chadd  * Returns 1 if the buffer could be added to the filtered list
3391f1bc738eSAdrian Chadd  * (cloned or otherwise), 0 if the buffer couldn't be added to the
3392f1bc738eSAdrian Chadd  * filtered list (failed clone; expired retry) and the caller should
3393f1bc738eSAdrian Chadd  * free it and handle it like a failure (eg by sending a BAR.)
3394f1bc738eSAdrian Chadd  */
3395f1bc738eSAdrian Chadd static int
3396f1bc738eSAdrian Chadd ath_tx_tid_filt_comp_single(struct ath_softc *sc, struct ath_tid *tid,
3397f1bc738eSAdrian Chadd     struct ath_buf *bf)
3398f1bc738eSAdrian Chadd {
3399f1bc738eSAdrian Chadd 	struct ath_buf *nbf;
3400f1bc738eSAdrian Chadd 	int retval;
3401f1bc738eSAdrian Chadd 
3402375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
3403f1bc738eSAdrian Chadd 
3404f1bc738eSAdrian Chadd 	/*
3405f1bc738eSAdrian Chadd 	 * Don't allow a filtered frame to live forever.
3406f1bc738eSAdrian Chadd 	 */
3407f1bc738eSAdrian Chadd 	if (bf->bf_state.bfs_retries > SWMAX_RETRIES) {
34080eb81626SAdrian Chadd 		sc->sc_stats.ast_tx_swretrymax++;
3409f1bc738eSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3410f1bc738eSAdrian Chadd 		    "%s: bf=%p, seqno=%d, exceeded retries\n",
3411f1bc738eSAdrian Chadd 		    __func__,
3412f1bc738eSAdrian Chadd 		    bf,
3413f1bc738eSAdrian Chadd 		    bf->bf_state.bfs_seqno);
3414f1bc738eSAdrian Chadd 		return (0);
3415f1bc738eSAdrian Chadd 	}
3416f1bc738eSAdrian Chadd 
3417f1bc738eSAdrian Chadd 	/*
3418f1bc738eSAdrian Chadd 	 * A busy buffer can't be added to the retry list.
3419f1bc738eSAdrian Chadd 	 * It needs to be cloned.
3420f1bc738eSAdrian Chadd 	 */
3421f1bc738eSAdrian Chadd 	if (bf->bf_flags & ATH_BUF_BUSY) {
3422f1bc738eSAdrian Chadd 		nbf = ath_tx_retry_clone(sc, tid->an, tid, bf);
3423f1bc738eSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3424f1bc738eSAdrian Chadd 		    "%s: busy buffer clone: %p -> %p\n",
3425f1bc738eSAdrian Chadd 		    __func__, bf, nbf);
3426f1bc738eSAdrian Chadd 	} else {
3427f1bc738eSAdrian Chadd 		nbf = bf;
3428f1bc738eSAdrian Chadd 	}
3429f1bc738eSAdrian Chadd 
3430f1bc738eSAdrian Chadd 	if (nbf == NULL) {
3431f1bc738eSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3432f1bc738eSAdrian Chadd 		    "%s: busy buffer couldn't be cloned (%p)!\n",
3433f1bc738eSAdrian Chadd 		    __func__, bf);
3434f1bc738eSAdrian Chadd 		retval = 1;
3435f1bc738eSAdrian Chadd 	} else {
3436f1bc738eSAdrian Chadd 		ath_tx_tid_filt_comp_buf(sc, tid, nbf);
3437f1bc738eSAdrian Chadd 		retval = 0;
3438f1bc738eSAdrian Chadd 	}
3439f1bc738eSAdrian Chadd 	ath_tx_tid_filt_comp_complete(sc, tid);
3440f1bc738eSAdrian Chadd 
3441f1bc738eSAdrian Chadd 	return (retval);
3442f1bc738eSAdrian Chadd }
3443f1bc738eSAdrian Chadd 
3444f1bc738eSAdrian Chadd static void
3445f1bc738eSAdrian Chadd ath_tx_tid_filt_comp_aggr(struct ath_softc *sc, struct ath_tid *tid,
3446f1bc738eSAdrian Chadd     struct ath_buf *bf_first, ath_bufhead *bf_q)
3447f1bc738eSAdrian Chadd {
3448f1bc738eSAdrian Chadd 	struct ath_buf *bf, *bf_next, *nbf;
3449f1bc738eSAdrian Chadd 
3450375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
3451f1bc738eSAdrian Chadd 
3452f1bc738eSAdrian Chadd 	bf = bf_first;
3453f1bc738eSAdrian Chadd 	while (bf) {
3454f1bc738eSAdrian Chadd 		bf_next = bf->bf_next;
3455f1bc738eSAdrian Chadd 		bf->bf_next = NULL;	/* Remove it from the aggr list */
3456f1bc738eSAdrian Chadd 
3457f1bc738eSAdrian Chadd 		/*
3458f1bc738eSAdrian Chadd 		 * Don't allow a filtered frame to live forever.
3459f1bc738eSAdrian Chadd 		 */
3460f1bc738eSAdrian Chadd 		if (bf->bf_state.bfs_retries > SWMAX_RETRIES) {
34610eb81626SAdrian Chadd 			sc->sc_stats.ast_tx_swretrymax++;
3462f1bc738eSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3463f1bc738eSAdrian Chadd 			    "%s: bf=%p, seqno=%d, exceeded retries\n",
3464f1bc738eSAdrian Chadd 			    __func__,
3465f1bc738eSAdrian Chadd 			    bf,
3466f1bc738eSAdrian Chadd 			    bf->bf_state.bfs_seqno);
3467f1bc738eSAdrian Chadd 			TAILQ_INSERT_TAIL(bf_q, bf, bf_list);
3468f1bc738eSAdrian Chadd 			goto next;
3469f1bc738eSAdrian Chadd 		}
3470f1bc738eSAdrian Chadd 
3471f1bc738eSAdrian Chadd 		if (bf->bf_flags & ATH_BUF_BUSY) {
3472f1bc738eSAdrian Chadd 			nbf = ath_tx_retry_clone(sc, tid->an, tid, bf);
3473f1bc738eSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3474f1bc738eSAdrian Chadd 			    "%s: busy buffer cloned: %p -> %p",
3475f1bc738eSAdrian Chadd 			    __func__, bf, nbf);
3476f1bc738eSAdrian Chadd 		} else {
3477f1bc738eSAdrian Chadd 			nbf = bf;
3478f1bc738eSAdrian Chadd 		}
3479f1bc738eSAdrian Chadd 
3480f1bc738eSAdrian Chadd 		/*
3481f1bc738eSAdrian Chadd 		 * If the buffer couldn't be cloned, add it to bf_q;
3482f1bc738eSAdrian Chadd 		 * the caller will free the buffer(s) as required.
3483f1bc738eSAdrian Chadd 		 */
3484f1bc738eSAdrian Chadd 		if (nbf == NULL) {
3485f1bc738eSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3486f1bc738eSAdrian Chadd 			    "%s: buffer couldn't be cloned! (%p)\n",
3487f1bc738eSAdrian Chadd 			    __func__, bf);
3488f1bc738eSAdrian Chadd 			TAILQ_INSERT_TAIL(bf_q, bf, bf_list);
3489f1bc738eSAdrian Chadd 		} else {
3490f1bc738eSAdrian Chadd 			ath_tx_tid_filt_comp_buf(sc, tid, nbf);
3491f1bc738eSAdrian Chadd 		}
3492f1bc738eSAdrian Chadd next:
3493f1bc738eSAdrian Chadd 		bf = bf_next;
3494f1bc738eSAdrian Chadd 	}
3495f1bc738eSAdrian Chadd 
3496f1bc738eSAdrian Chadd 	ath_tx_tid_filt_comp_complete(sc, tid);
3497f1bc738eSAdrian Chadd }
3498f1bc738eSAdrian Chadd 
3499f1bc738eSAdrian Chadd /*
350088b3d483SAdrian Chadd  * Suspend the queue because we need to TX a BAR.
350188b3d483SAdrian Chadd  */
350288b3d483SAdrian Chadd static void
350388b3d483SAdrian Chadd ath_tx_tid_bar_suspend(struct ath_softc *sc, struct ath_tid *tid)
350488b3d483SAdrian Chadd {
3505375307d4SAdrian Chadd 
3506375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
350788b3d483SAdrian Chadd 
35080e22ed0eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
35096d07d3e0SAdrian Chadd 	    "%s: tid=%d, bar_wait=%d, bar_tx=%d, called\n",
351088b3d483SAdrian Chadd 	    __func__,
35116d07d3e0SAdrian Chadd 	    tid->tid,
3512e60c4fc2SAdrian Chadd 	    tid->bar_wait,
3513e60c4fc2SAdrian Chadd 	    tid->bar_tx);
351488b3d483SAdrian Chadd 
351588b3d483SAdrian Chadd 	/* We shouldn't be called when bar_tx is 1 */
351688b3d483SAdrian Chadd 	if (tid->bar_tx) {
351783bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
351883bbd5ebSRui Paulo 		    "%s: bar_tx is 1?!\n", __func__);
351988b3d483SAdrian Chadd 	}
352088b3d483SAdrian Chadd 
352188b3d483SAdrian Chadd 	/* If we've already been called, just be patient. */
352288b3d483SAdrian Chadd 	if (tid->bar_wait)
352388b3d483SAdrian Chadd 		return;
352488b3d483SAdrian Chadd 
352588b3d483SAdrian Chadd 	/* Wait! */
352688b3d483SAdrian Chadd 	tid->bar_wait = 1;
352788b3d483SAdrian Chadd 
352888b3d483SAdrian Chadd 	/* Only one pause, no matter how many frames fail */
352988b3d483SAdrian Chadd 	ath_tx_tid_pause(sc, tid);
353088b3d483SAdrian Chadd }
353188b3d483SAdrian Chadd 
353288b3d483SAdrian Chadd /*
353388b3d483SAdrian Chadd  * We've finished with BAR handling - either we succeeded or
353488b3d483SAdrian Chadd  * failed. Either way, unsuspend TX.
353588b3d483SAdrian Chadd  */
353688b3d483SAdrian Chadd static void
353788b3d483SAdrian Chadd ath_tx_tid_bar_unsuspend(struct ath_softc *sc, struct ath_tid *tid)
353888b3d483SAdrian Chadd {
3539375307d4SAdrian Chadd 
3540375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
354188b3d483SAdrian Chadd 
35420e22ed0eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
35436d07d3e0SAdrian Chadd 	    "%s: %6D: TID=%d, called\n",
354488b3d483SAdrian Chadd 	    __func__,
35459b48fb4bSAdrian Chadd 	    tid->an->an_node.ni_macaddr,
35469b48fb4bSAdrian Chadd 	    ":",
35476d07d3e0SAdrian Chadd 	    tid->tid);
354888b3d483SAdrian Chadd 
354988b3d483SAdrian Chadd 	if (tid->bar_tx == 0 || tid->bar_wait == 0) {
355083bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
35516d07d3e0SAdrian Chadd 		    "%s: %6D: TID=%d, bar_tx=%d, bar_wait=%d: ?\n",
355283bbd5ebSRui Paulo 		    __func__, tid->an->an_node.ni_macaddr, ":",
355383bbd5ebSRui Paulo 		    tid->tid, tid->bar_tx, tid->bar_wait);
355488b3d483SAdrian Chadd 	}
355588b3d483SAdrian Chadd 
355688b3d483SAdrian Chadd 	tid->bar_tx = tid->bar_wait = 0;
355788b3d483SAdrian Chadd 	ath_tx_tid_resume(sc, tid);
355888b3d483SAdrian Chadd }
355988b3d483SAdrian Chadd 
356088b3d483SAdrian Chadd /*
356188b3d483SAdrian Chadd  * Return whether we're ready to TX a BAR frame.
356288b3d483SAdrian Chadd  *
356388b3d483SAdrian Chadd  * Requires the TID lock be held.
356488b3d483SAdrian Chadd  */
356588b3d483SAdrian Chadd static int
356688b3d483SAdrian Chadd ath_tx_tid_bar_tx_ready(struct ath_softc *sc, struct ath_tid *tid)
356788b3d483SAdrian Chadd {
356888b3d483SAdrian Chadd 
3569375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
357088b3d483SAdrian Chadd 
357188b3d483SAdrian Chadd 	if (tid->bar_wait == 0 || tid->hwq_depth > 0)
357288b3d483SAdrian Chadd 		return (0);
357388b3d483SAdrian Chadd 
35749b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
35756d07d3e0SAdrian Chadd 	    "%s: %6D: TID=%d, bar ready\n",
35769b48fb4bSAdrian Chadd 	    __func__,
35779b48fb4bSAdrian Chadd 	    tid->an->an_node.ni_macaddr,
35789b48fb4bSAdrian Chadd 	    ":",
35796d07d3e0SAdrian Chadd 	    tid->tid);
35800e22ed0eSAdrian Chadd 
358188b3d483SAdrian Chadd 	return (1);
358288b3d483SAdrian Chadd }
358388b3d483SAdrian Chadd 
358488b3d483SAdrian Chadd /*
358588b3d483SAdrian Chadd  * Check whether the current TID is ready to have a BAR
358688b3d483SAdrian Chadd  * TXed and if so, do the TX.
358788b3d483SAdrian Chadd  *
358888b3d483SAdrian Chadd  * Since the TID/TXQ lock can't be held during a call to
358988b3d483SAdrian Chadd  * ieee80211_send_bar(), we have to do the dirty thing of unlocking it,
359088b3d483SAdrian Chadd  * sending the BAR and locking it again.
359188b3d483SAdrian Chadd  *
359288b3d483SAdrian Chadd  * Eventually, the code to send the BAR should be broken out
359388b3d483SAdrian Chadd  * from this routine so the lock doesn't have to be reacquired
359488b3d483SAdrian Chadd  * just to be immediately dropped by the caller.
359588b3d483SAdrian Chadd  */
359688b3d483SAdrian Chadd static void
359788b3d483SAdrian Chadd ath_tx_tid_bar_tx(struct ath_softc *sc, struct ath_tid *tid)
359888b3d483SAdrian Chadd {
359988b3d483SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
360088b3d483SAdrian Chadd 
3601375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
360288b3d483SAdrian Chadd 
36030e22ed0eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
36046d07d3e0SAdrian Chadd 	    "%s: %6D: TID=%d, called\n",
360588b3d483SAdrian Chadd 	    __func__,
36069b48fb4bSAdrian Chadd 	    tid->an->an_node.ni_macaddr,
36079b48fb4bSAdrian Chadd 	    ":",
36086d07d3e0SAdrian Chadd 	    tid->tid);
360988b3d483SAdrian Chadd 
361088b3d483SAdrian Chadd 	tap = ath_tx_get_tx_tid(tid->an, tid->tid);
361188b3d483SAdrian Chadd 
361288b3d483SAdrian Chadd 	/*
361388b3d483SAdrian Chadd 	 * This is an error condition!
361488b3d483SAdrian Chadd 	 */
361588b3d483SAdrian Chadd 	if (tid->bar_wait == 0 || tid->bar_tx == 1) {
361683bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
36176d07d3e0SAdrian Chadd 		    "%s: %6D: TID=%d, bar_tx=%d, bar_wait=%d: ?\n",
361883bbd5ebSRui Paulo 		    __func__, tid->an->an_node.ni_macaddr, ":",
361983bbd5ebSRui Paulo 		    tid->tid, tid->bar_tx, tid->bar_wait);
362088b3d483SAdrian Chadd 		return;
362188b3d483SAdrian Chadd 	}
362288b3d483SAdrian Chadd 
362388b3d483SAdrian Chadd 	/* Don't do anything if we still have pending frames */
362488b3d483SAdrian Chadd 	if (tid->hwq_depth > 0) {
36250e22ed0eSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
36266d07d3e0SAdrian Chadd 		    "%s: %6D: TID=%d, hwq_depth=%d, waiting\n",
362788b3d483SAdrian Chadd 		    __func__,
36289b48fb4bSAdrian Chadd 		    tid->an->an_node.ni_macaddr,
36299b48fb4bSAdrian Chadd 		    ":",
36306d07d3e0SAdrian Chadd 		    tid->tid,
363188b3d483SAdrian Chadd 		    tid->hwq_depth);
363288b3d483SAdrian Chadd 		return;
363388b3d483SAdrian Chadd 	}
363488b3d483SAdrian Chadd 
363588b3d483SAdrian Chadd 	/* We're now about to TX */
363688b3d483SAdrian Chadd 	tid->bar_tx = 1;
363788b3d483SAdrian Chadd 
363888b3d483SAdrian Chadd 	/*
36394e81f27cSAdrian Chadd 	 * Override the clrdmask configuration for the next frame,
36404e81f27cSAdrian Chadd 	 * just to get the ball rolling.
36414e81f27cSAdrian Chadd 	 */
36424f25ddbbSAdrian Chadd 	ath_tx_set_clrdmask(sc, tid->an);
36434e81f27cSAdrian Chadd 
36444e81f27cSAdrian Chadd 	/*
364588b3d483SAdrian Chadd 	 * Calculate new BAW left edge, now that all frames have either
364688b3d483SAdrian Chadd 	 * succeeded or failed.
364788b3d483SAdrian Chadd 	 *
364888b3d483SAdrian Chadd 	 * XXX verify this is _actually_ the valid value to begin at!
364988b3d483SAdrian Chadd 	 */
36500e22ed0eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
36516d07d3e0SAdrian Chadd 	    "%s: %6D: TID=%d, new BAW left edge=%d\n",
365288b3d483SAdrian Chadd 	    __func__,
36539b48fb4bSAdrian Chadd 	    tid->an->an_node.ni_macaddr,
36549b48fb4bSAdrian Chadd 	    ":",
36556d07d3e0SAdrian Chadd 	    tid->tid,
365688b3d483SAdrian Chadd 	    tap->txa_start);
365788b3d483SAdrian Chadd 
365888b3d483SAdrian Chadd 	/* Try sending the BAR frame */
365988b3d483SAdrian Chadd 	/* We can't hold the lock here! */
366088b3d483SAdrian Chadd 
3661375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
366288b3d483SAdrian Chadd 	if (ieee80211_send_bar(&tid->an->an_node, tap, tap->txa_start) == 0) {
366388b3d483SAdrian Chadd 		/* Success? Now we wait for notification that it's done */
3664375307d4SAdrian Chadd 		ATH_TX_LOCK(sc);
366588b3d483SAdrian Chadd 		return;
366688b3d483SAdrian Chadd 	}
366788b3d483SAdrian Chadd 
366888b3d483SAdrian Chadd 	/* Failure? For now, warn loudly and continue */
3669375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
367083bbd5ebSRui Paulo 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
36716d07d3e0SAdrian Chadd 	    "%s: %6D: TID=%d, failed to TX BAR, continue!\n",
367283bbd5ebSRui Paulo 	    __func__, tid->an->an_node.ni_macaddr, ":",
36736d07d3e0SAdrian Chadd 	    tid->tid);
367488b3d483SAdrian Chadd 	ath_tx_tid_bar_unsuspend(sc, tid);
367588b3d483SAdrian Chadd }
367688b3d483SAdrian Chadd 
3677eb6f0de0SAdrian Chadd static void
3678f1bc738eSAdrian Chadd ath_tx_tid_drain_pkt(struct ath_softc *sc, struct ath_node *an,
3679f1bc738eSAdrian Chadd     struct ath_tid *tid, ath_bufhead *bf_cq, struct ath_buf *bf)
3680eb6f0de0SAdrian Chadd {
3681eb6f0de0SAdrian Chadd 
3682375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
3683eb6f0de0SAdrian Chadd 
3684eb6f0de0SAdrian Chadd 	/*
3685eb6f0de0SAdrian Chadd 	 * If the current TID is running AMPDU, update
3686eb6f0de0SAdrian Chadd 	 * the BAW.
3687eb6f0de0SAdrian Chadd 	 */
3688eb6f0de0SAdrian Chadd 	if (ath_tx_ampdu_running(sc, an, tid->tid) &&
3689eb6f0de0SAdrian Chadd 	    bf->bf_state.bfs_dobaw) {
3690eb6f0de0SAdrian Chadd 		/*
3691eb6f0de0SAdrian Chadd 		 * Only remove the frame from the BAW if it's
3692eb6f0de0SAdrian Chadd 		 * been transmitted at least once; this means
3693eb6f0de0SAdrian Chadd 		 * the frame was in the BAW to begin with.
3694eb6f0de0SAdrian Chadd 		 */
3695eb6f0de0SAdrian Chadd 		if (bf->bf_state.bfs_retries > 0) {
3696eb6f0de0SAdrian Chadd 			ath_tx_update_baw(sc, an, tid, bf);
3697eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_dobaw = 0;
3698eb6f0de0SAdrian Chadd 		}
3699ce597531SAdrian Chadd #if 0
3700eb6f0de0SAdrian Chadd 		/*
3701eb6f0de0SAdrian Chadd 		 * This has become a non-fatal error now
3702eb6f0de0SAdrian Chadd 		 */
3703eb6f0de0SAdrian Chadd 		if (! bf->bf_state.bfs_addedbaw)
370483bbd5ebSRui Paulo 			DPRINTF(sc, ATH_DEBUG_SW_TX_BAW
3705eb6f0de0SAdrian Chadd 			    "%s: wasn't added: seqno %d\n",
3706eb6f0de0SAdrian Chadd 			    __func__, SEQNO(bf->bf_state.bfs_seqno));
3707ce597531SAdrian Chadd #endif
3708eb6f0de0SAdrian Chadd 	}
3709b837332dSAdrian Chadd 
3710b837332dSAdrian Chadd 	/* Strip it out of an aggregate list if it was in one */
3711b837332dSAdrian Chadd 	bf->bf_next = NULL;
3712b837332dSAdrian Chadd 
3713b837332dSAdrian Chadd 	/* Insert on the free queue to be freed by the caller */
3714eb6f0de0SAdrian Chadd 	TAILQ_INSERT_TAIL(bf_cq, bf, bf_list);
3715eb6f0de0SAdrian Chadd }
3716eb6f0de0SAdrian Chadd 
3717f1bc738eSAdrian Chadd static void
3718f1bc738eSAdrian Chadd ath_tx_tid_drain_print(struct ath_softc *sc, struct ath_node *an,
371903682514SAdrian Chadd     const char *pfx, struct ath_tid *tid, struct ath_buf *bf)
3720f1bc738eSAdrian Chadd {
3721f1bc738eSAdrian Chadd 	struct ieee80211_node *ni = &an->an_node;
372283bbd5ebSRui Paulo 	struct ath_txq *txq;
3723f1bc738eSAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
3724f1bc738eSAdrian Chadd 
372583bbd5ebSRui Paulo 	txq = sc->sc_ac2q[tid->ac];
3726f1bc738eSAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
3727f1bc738eSAdrian Chadd 
37286fc621c2SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX | ATH_DEBUG_RESET,
3729272a8ab6SAdrian Chadd 	    "%s: %s: %6D: bf=%p: addbaw=%d, dobaw=%d, "
3730f1bc738eSAdrian Chadd 	    "seqno=%d, retry=%d\n",
3731272a8ab6SAdrian Chadd 	    __func__,
3732272a8ab6SAdrian Chadd 	    pfx,
3733272a8ab6SAdrian Chadd 	    ni->ni_macaddr,
3734272a8ab6SAdrian Chadd 	    ":",
3735272a8ab6SAdrian Chadd 	    bf,
3736f1bc738eSAdrian Chadd 	    bf->bf_state.bfs_addedbaw,
3737f1bc738eSAdrian Chadd 	    bf->bf_state.bfs_dobaw,
3738f1bc738eSAdrian Chadd 	    SEQNO(bf->bf_state.bfs_seqno),
3739f1bc738eSAdrian Chadd 	    bf->bf_state.bfs_retries);
37406fc621c2SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX | ATH_DEBUG_RESET,
3741272a8ab6SAdrian Chadd 	    "%s: %s: %6D: bf=%p: txq[%d] axq_depth=%d, axq_aggr_depth=%d\n",
3742272a8ab6SAdrian Chadd 	    __func__,
3743272a8ab6SAdrian Chadd 	    pfx,
3744272a8ab6SAdrian Chadd 	    ni->ni_macaddr,
3745272a8ab6SAdrian Chadd 	    ":",
3746272a8ab6SAdrian Chadd 	    bf,
374703682514SAdrian Chadd 	    txq->axq_qnum,
37484e81f27cSAdrian Chadd 	    txq->axq_depth,
37494e81f27cSAdrian Chadd 	    txq->axq_aggr_depth);
37506fc621c2SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX | ATH_DEBUG_RESET,
3751272a8ab6SAdrian Chadd 	    "%s: %s: %6D: bf=%p: tid txq_depth=%d hwq_depth=%d, bar_wait=%d, "
3752272a8ab6SAdrian Chadd 	      "isfiltered=%d\n",
3753272a8ab6SAdrian Chadd 	    __func__,
3754272a8ab6SAdrian Chadd 	    pfx,
3755272a8ab6SAdrian Chadd 	    ni->ni_macaddr,
3756272a8ab6SAdrian Chadd 	    ":",
3757272a8ab6SAdrian Chadd 	    bf,
3758f1bc738eSAdrian Chadd 	    tid->axq_depth,
3759f1bc738eSAdrian Chadd 	    tid->hwq_depth,
3760f1bc738eSAdrian Chadd 	    tid->bar_wait,
3761f1bc738eSAdrian Chadd 	    tid->isfiltered);
37626fc621c2SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX | ATH_DEBUG_RESET,
3763272a8ab6SAdrian Chadd 	    "%s: %s: %6D: tid %d: "
37644e81f27cSAdrian Chadd 	    "sched=%d, paused=%d, "
37654e81f27cSAdrian Chadd 	    "incomp=%d, baw_head=%d, "
3766f1bc738eSAdrian Chadd 	    "baw_tail=%d txa_start=%d, ni_txseqs=%d\n",
3767272a8ab6SAdrian Chadd 	     __func__,
3768272a8ab6SAdrian Chadd 	     pfx,
3769272a8ab6SAdrian Chadd 	     ni->ni_macaddr,
3770272a8ab6SAdrian Chadd 	     ":",
3771272a8ab6SAdrian Chadd 	     tid->tid,
37724e81f27cSAdrian Chadd 	     tid->sched, tid->paused,
37734e81f27cSAdrian Chadd 	     tid->incomp, tid->baw_head,
3774f1bc738eSAdrian Chadd 	     tid->baw_tail, tap == NULL ? -1 : tap->txa_start,
3775f1bc738eSAdrian Chadd 	     ni->ni_txseqs[tid->tid]);
3776f1bc738eSAdrian Chadd 
3777f1bc738eSAdrian Chadd 	/* XXX Dump the frame, see what it is? */
3778a2be2710SRui Paulo 	if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
3779f1bc738eSAdrian Chadd 		ieee80211_dump_pkt(ni->ni_ic,
3780f1bc738eSAdrian Chadd 		    mtod(bf->bf_m, const uint8_t *),
3781f1bc738eSAdrian Chadd 		    bf->bf_m->m_len, 0, -1);
3782f1bc738eSAdrian Chadd }
3783f1bc738eSAdrian Chadd 
3784f1bc738eSAdrian Chadd /*
3785f1bc738eSAdrian Chadd  * Free any packets currently pending in the software TX queue.
3786f1bc738eSAdrian Chadd  *
3787f1bc738eSAdrian Chadd  * This will be called when a node is being deleted.
3788f1bc738eSAdrian Chadd  *
3789f1bc738eSAdrian Chadd  * It can also be called on an active node during an interface
3790f1bc738eSAdrian Chadd  * reset or state transition.
3791f1bc738eSAdrian Chadd  *
3792f1bc738eSAdrian Chadd  * (From Linux/reference):
3793f1bc738eSAdrian Chadd  *
3794f1bc738eSAdrian Chadd  * TODO: For frame(s) that are in the retry state, we will reuse the
3795f1bc738eSAdrian Chadd  * sequence number(s) without setting the retry bit. The
3796f1bc738eSAdrian Chadd  * alternative is to give up on these and BAR the receiver's window
3797f1bc738eSAdrian Chadd  * forward.
3798f1bc738eSAdrian Chadd  */
3799f1bc738eSAdrian Chadd static void
3800f1bc738eSAdrian Chadd ath_tx_tid_drain(struct ath_softc *sc, struct ath_node *an,
3801f1bc738eSAdrian Chadd     struct ath_tid *tid, ath_bufhead *bf_cq)
3802f1bc738eSAdrian Chadd {
3803f1bc738eSAdrian Chadd 	struct ath_buf *bf;
3804f1bc738eSAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
3805f1bc738eSAdrian Chadd 	struct ieee80211_node *ni = &an->an_node;
3806f1bc738eSAdrian Chadd 	int t;
3807f1bc738eSAdrian Chadd 
3808f1bc738eSAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
3809f1bc738eSAdrian Chadd 
3810375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
3811f1bc738eSAdrian Chadd 
3812f1bc738eSAdrian Chadd 	/* Walk the queue, free frames */
3813f1bc738eSAdrian Chadd 	t = 0;
3814f1bc738eSAdrian Chadd 	for (;;) {
38153e6cc97fSAdrian Chadd 		bf = ATH_TID_FIRST(tid);
3816f1bc738eSAdrian Chadd 		if (bf == NULL) {
3817f1bc738eSAdrian Chadd 			break;
3818f1bc738eSAdrian Chadd 		}
3819f1bc738eSAdrian Chadd 
3820f1bc738eSAdrian Chadd 		if (t == 0) {
382103682514SAdrian Chadd 			ath_tx_tid_drain_print(sc, an, "norm", tid, bf);
38226fc621c2SAdrian Chadd //			t = 1;
3823f1bc738eSAdrian Chadd 		}
3824f1bc738eSAdrian Chadd 
38253e6cc97fSAdrian Chadd 		ATH_TID_REMOVE(tid, bf, bf_list);
3826f1bc738eSAdrian Chadd 		ath_tx_tid_drain_pkt(sc, an, tid, bf_cq, bf);
3827f1bc738eSAdrian Chadd 	}
3828f1bc738eSAdrian Chadd 
3829f1bc738eSAdrian Chadd 	/* And now, drain the filtered frame queue */
3830f1bc738eSAdrian Chadd 	t = 0;
3831f1bc738eSAdrian Chadd 	for (;;) {
383213aa9ee5SAdrian Chadd 		bf = ATH_TID_FILT_FIRST(tid);
3833f1bc738eSAdrian Chadd 		if (bf == NULL)
3834f1bc738eSAdrian Chadd 			break;
3835f1bc738eSAdrian Chadd 
3836f1bc738eSAdrian Chadd 		if (t == 0) {
383703682514SAdrian Chadd 			ath_tx_tid_drain_print(sc, an, "filt", tid, bf);
38386fc621c2SAdrian Chadd //			t = 1;
3839f1bc738eSAdrian Chadd 		}
3840f1bc738eSAdrian Chadd 
384113aa9ee5SAdrian Chadd 		ATH_TID_FILT_REMOVE(tid, bf, bf_list);
3842f1bc738eSAdrian Chadd 		ath_tx_tid_drain_pkt(sc, an, tid, bf_cq, bf);
3843f1bc738eSAdrian Chadd 	}
3844f1bc738eSAdrian Chadd 
3845eb6f0de0SAdrian Chadd 	/*
38464e81f27cSAdrian Chadd 	 * Override the clrdmask configuration for the next frame
38474e81f27cSAdrian Chadd 	 * in case there is some future transmission, just to get
38484e81f27cSAdrian Chadd 	 * the ball rolling.
38494e81f27cSAdrian Chadd 	 *
38504e81f27cSAdrian Chadd 	 * This won't hurt things if the TID is about to be freed.
38514e81f27cSAdrian Chadd 	 */
38524f25ddbbSAdrian Chadd 	ath_tx_set_clrdmask(sc, tid->an);
38534e81f27cSAdrian Chadd 
38544e81f27cSAdrian Chadd 	/*
3855eb6f0de0SAdrian Chadd 	 * Now that it's completed, grab the TID lock and update
3856eb6f0de0SAdrian Chadd 	 * the sequence number and BAW window.
3857eb6f0de0SAdrian Chadd 	 * Because sequence numbers have been assigned to frames
3858eb6f0de0SAdrian Chadd 	 * that haven't been sent yet, it's entirely possible
3859eb6f0de0SAdrian Chadd 	 * we'll be called with some pending frames that have not
3860eb6f0de0SAdrian Chadd 	 * been transmitted.
3861eb6f0de0SAdrian Chadd 	 *
3862eb6f0de0SAdrian Chadd 	 * The cleaner solution is to do the sequence number allocation
3863eb6f0de0SAdrian Chadd 	 * when the packet is first transmitted - and thus the "retries"
3864eb6f0de0SAdrian Chadd 	 * check above would be enough to update the BAW/seqno.
3865eb6f0de0SAdrian Chadd 	 */
3866eb6f0de0SAdrian Chadd 
3867eb6f0de0SAdrian Chadd 	/* But don't do it for non-QoS TIDs */
3868eb6f0de0SAdrian Chadd 	if (tap) {
38699b48fb4bSAdrian Chadd #if 1
3870eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
38719b48fb4bSAdrian Chadd 		    "%s: %6D: node %p: TID %d: sliding BAW left edge to %d\n",
38729b48fb4bSAdrian Chadd 		    __func__,
38739b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
38749b48fb4bSAdrian Chadd 		    ":",
38759b48fb4bSAdrian Chadd 		    an,
38769b48fb4bSAdrian Chadd 		    tid->tid,
38779b48fb4bSAdrian Chadd 		    tap->txa_start);
3878eb6f0de0SAdrian Chadd #endif
3879eb6f0de0SAdrian Chadd 		ni->ni_txseqs[tid->tid] = tap->txa_start;
3880eb6f0de0SAdrian Chadd 		tid->baw_tail = tid->baw_head;
3881eb6f0de0SAdrian Chadd 	}
3882eb6f0de0SAdrian Chadd }
3883eb6f0de0SAdrian Chadd 
3884eb6f0de0SAdrian Chadd /*
388522780332SAdrian Chadd  * Reset the TID state.  This must be only called once the node has
388622780332SAdrian Chadd  * had its frames flushed from this TID, to ensure that no other
388722780332SAdrian Chadd  * pause / unpause logic can kick in.
388822780332SAdrian Chadd  */
388922780332SAdrian Chadd static void
389022780332SAdrian Chadd ath_tx_tid_reset(struct ath_softc *sc, struct ath_tid *tid)
389122780332SAdrian Chadd {
389222780332SAdrian Chadd 
389322780332SAdrian Chadd #if 0
389422780332SAdrian Chadd 	tid->bar_wait = tid->bar_tx = tid->isfiltered = 0;
389522780332SAdrian Chadd 	tid->paused = tid->sched = tid->addba_tx_pending = 0;
389622780332SAdrian Chadd 	tid->incomp = tid->cleanup_inprogress = 0;
389722780332SAdrian Chadd #endif
389822780332SAdrian Chadd 
389922780332SAdrian Chadd 	/*
390022780332SAdrian Chadd 	 * If we have a bar_wait set, we need to unpause the TID
390122780332SAdrian Chadd 	 * here.  Otherwise once cleanup has finished, the TID won't
390222780332SAdrian Chadd 	 * have the right paused counter.
390322780332SAdrian Chadd 	 *
390422780332SAdrian Chadd 	 * XXX I'm not going through resume here - I don't want the
390522780332SAdrian Chadd 	 * node to be rescheuled just yet.  This however should be
390622780332SAdrian Chadd 	 * methodized!
390722780332SAdrian Chadd 	 */
390822780332SAdrian Chadd 	if (tid->bar_wait) {
390922780332SAdrian Chadd 		if (tid->paused > 0) {
391022780332SAdrian Chadd 			tid->paused --;
391122780332SAdrian Chadd 		}
391222780332SAdrian Chadd 	}
391322780332SAdrian Chadd 
391422780332SAdrian Chadd 	/*
391522780332SAdrian Chadd 	 * XXX same with a currently filtered TID.
391622780332SAdrian Chadd 	 *
391722780332SAdrian Chadd 	 * Since this is being called during a flush, we assume that
391822780332SAdrian Chadd 	 * the filtered frame list is actually empty.
391922780332SAdrian Chadd 	 *
392022780332SAdrian Chadd 	 * XXX TODO: add in a check to ensure that the filtered queue
392122780332SAdrian Chadd 	 * depth is actually 0!
392222780332SAdrian Chadd 	 */
392322780332SAdrian Chadd 	if (tid->isfiltered) {
392422780332SAdrian Chadd 		if (tid->paused > 0) {
392522780332SAdrian Chadd 			tid->paused --;
392622780332SAdrian Chadd 		}
392722780332SAdrian Chadd 	}
392822780332SAdrian Chadd 
392922780332SAdrian Chadd 	/*
393022780332SAdrian Chadd 	 * Clear BAR, filtered frames, scheduled and ADDBA pending.
393122780332SAdrian Chadd 	 * The TID may be going through cleanup from the last association
393222780332SAdrian Chadd 	 * where things in the BAW are still in the hardware queue.
393322780332SAdrian Chadd 	 */
393422780332SAdrian Chadd 	tid->bar_wait = 0;
393522780332SAdrian Chadd 	tid->bar_tx = 0;
393622780332SAdrian Chadd 	tid->isfiltered = 0;
393722780332SAdrian Chadd 	tid->sched = 0;
393822780332SAdrian Chadd 	tid->addba_tx_pending = 0;
393922780332SAdrian Chadd 
394022780332SAdrian Chadd 	/*
394122780332SAdrian Chadd 	 * XXX TODO: it may just be enough to walk the HWQs and mark
394222780332SAdrian Chadd 	 * frames for that node as non-aggregate; or mark the ath_node
394322780332SAdrian Chadd 	 * with something that indicates that aggregation is no longer
394422780332SAdrian Chadd 	 * occuring.  Then we can just toss the BAW complaints and
394522780332SAdrian Chadd 	 * do a complete hard reset of state here - no pause, no
394622780332SAdrian Chadd 	 * complete counter, etc.
394722780332SAdrian Chadd 	 */
394822a3aee6SAdrian Chadd 
394922780332SAdrian Chadd }
395022780332SAdrian Chadd 
395122780332SAdrian Chadd /*
3952eb6f0de0SAdrian Chadd  * Flush all software queued packets for the given node.
3953eb6f0de0SAdrian Chadd  *
3954eb6f0de0SAdrian Chadd  * This occurs when a completion handler frees the last buffer
3955eb6f0de0SAdrian Chadd  * for a node, and the node is thus freed. This causes the node
3956eb6f0de0SAdrian Chadd  * to be cleaned up, which ends up calling ath_tx_node_flush.
3957eb6f0de0SAdrian Chadd  */
3958eb6f0de0SAdrian Chadd void
3959eb6f0de0SAdrian Chadd ath_tx_node_flush(struct ath_softc *sc, struct ath_node *an)
3960eb6f0de0SAdrian Chadd {
3961eb6f0de0SAdrian Chadd 	int tid;
3962eb6f0de0SAdrian Chadd 	ath_bufhead bf_cq;
3963eb6f0de0SAdrian Chadd 	struct ath_buf *bf;
3964eb6f0de0SAdrian Chadd 
3965eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_cq);
3966eb6f0de0SAdrian Chadd 
396703682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_NODE, 1, "ath_tx_node_flush: flush node; ni=%p",
396803682514SAdrian Chadd 	    &an->an_node);
396903682514SAdrian Chadd 
3970375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
39719b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE,
39729b48fb4bSAdrian Chadd 	    "%s: %6D: flush; is_powersave=%d, stack_psq=%d, tim=%d, "
397322a3aee6SAdrian Chadd 	    "swq_depth=%d, clrdmask=%d, leak_count=%d\n",
39749b48fb4bSAdrian Chadd 	    __func__,
39759b48fb4bSAdrian Chadd 	    an->an_node.ni_macaddr,
39769b48fb4bSAdrian Chadd 	    ":",
39779b48fb4bSAdrian Chadd 	    an->an_is_powersave,
39789b48fb4bSAdrian Chadd 	    an->an_stack_psq,
39799b48fb4bSAdrian Chadd 	    an->an_tim_set,
39809b48fb4bSAdrian Chadd 	    an->an_swq_depth,
398122a3aee6SAdrian Chadd 	    an->clrdmask,
398222a3aee6SAdrian Chadd 	    an->an_leak_count);
39839b48fb4bSAdrian Chadd 
3984eb6f0de0SAdrian Chadd 	for (tid = 0; tid < IEEE80211_TID_SIZE; tid++) {
3985eb6f0de0SAdrian Chadd 		struct ath_tid *atid = &an->an_tid[tid];
3986eb6f0de0SAdrian Chadd 
3987eb6f0de0SAdrian Chadd 		/* Free packets */
3988eb6f0de0SAdrian Chadd 		ath_tx_tid_drain(sc, an, atid, &bf_cq);
398922a3aee6SAdrian Chadd 
399023f44d2bSAdrian Chadd 		/* Remove this tid from the list of active tids */
399123f44d2bSAdrian Chadd 		ath_tx_tid_unsched(sc, atid);
399222a3aee6SAdrian Chadd 
399322780332SAdrian Chadd 		/* Reset the per-TID pause, BAR, etc state */
399422780332SAdrian Chadd 		ath_tx_tid_reset(sc, atid);
3995eb6f0de0SAdrian Chadd 	}
399622a3aee6SAdrian Chadd 
399722a3aee6SAdrian Chadd 	/*
399822a3aee6SAdrian Chadd 	 * Clear global leak count
399922a3aee6SAdrian Chadd 	 */
400022a3aee6SAdrian Chadd 	an->an_leak_count = 0;
4001375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
4002eb6f0de0SAdrian Chadd 
4003eb6f0de0SAdrian Chadd 	/* Handle completed frames */
4004eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
4005eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
4006eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
4007eb6f0de0SAdrian Chadd 	}
4008eb6f0de0SAdrian Chadd }
4009eb6f0de0SAdrian Chadd 
4010eb6f0de0SAdrian Chadd /*
4011eb6f0de0SAdrian Chadd  * Drain all the software TXQs currently with traffic queued.
4012eb6f0de0SAdrian Chadd  */
4013eb6f0de0SAdrian Chadd void
4014eb6f0de0SAdrian Chadd ath_tx_txq_drain(struct ath_softc *sc, struct ath_txq *txq)
4015eb6f0de0SAdrian Chadd {
4016eb6f0de0SAdrian Chadd 	struct ath_tid *tid;
4017eb6f0de0SAdrian Chadd 	ath_bufhead bf_cq;
4018eb6f0de0SAdrian Chadd 	struct ath_buf *bf;
4019eb6f0de0SAdrian Chadd 
4020eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_cq);
4021375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
4022eb6f0de0SAdrian Chadd 
4023eb6f0de0SAdrian Chadd 	/*
4024eb6f0de0SAdrian Chadd 	 * Iterate over all active tids for the given txq,
4025eb6f0de0SAdrian Chadd 	 * flushing and unsched'ing them
4026eb6f0de0SAdrian Chadd 	 */
4027eb6f0de0SAdrian Chadd 	while (! TAILQ_EMPTY(&txq->axq_tidq)) {
4028eb6f0de0SAdrian Chadd 		tid = TAILQ_FIRST(&txq->axq_tidq);
4029eb6f0de0SAdrian Chadd 		ath_tx_tid_drain(sc, tid->an, tid, &bf_cq);
4030eb6f0de0SAdrian Chadd 		ath_tx_tid_unsched(sc, tid);
4031eb6f0de0SAdrian Chadd 	}
4032eb6f0de0SAdrian Chadd 
4033375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
4034eb6f0de0SAdrian Chadd 
4035eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
4036eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
4037eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
4038eb6f0de0SAdrian Chadd 	}
4039eb6f0de0SAdrian Chadd }
4040eb6f0de0SAdrian Chadd 
4041eb6f0de0SAdrian Chadd /*
4042eb6f0de0SAdrian Chadd  * Handle completion of non-aggregate session frames.
40430c54de88SAdrian Chadd  *
40440c54de88SAdrian Chadd  * This (currently) doesn't implement software retransmission of
40450c54de88SAdrian Chadd  * non-aggregate frames!
40460c54de88SAdrian Chadd  *
40470c54de88SAdrian Chadd  * Software retransmission of non-aggregate frames needs to obey
40480c54de88SAdrian Chadd  * the strict sequence number ordering, and drop any frames that
40490c54de88SAdrian Chadd  * will fail this.
40500c54de88SAdrian Chadd  *
40510c54de88SAdrian Chadd  * For now, filtered frames and frame transmission will cause
40520c54de88SAdrian Chadd  * all kinds of issues.  So we don't support them.
40530c54de88SAdrian Chadd  *
40540c54de88SAdrian Chadd  * So anyone queuing frames via ath_tx_normal_xmit() or
40550c54de88SAdrian Chadd  * ath_tx_hw_queue_norm() must override and set CLRDMASK.
4056eb6f0de0SAdrian Chadd  */
4057eb6f0de0SAdrian Chadd void
4058eb6f0de0SAdrian Chadd ath_tx_normal_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
4059eb6f0de0SAdrian Chadd {
4060eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
4061eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4062eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
4063eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4064eb6f0de0SAdrian Chadd 	struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
4065eb6f0de0SAdrian Chadd 
4066eb6f0de0SAdrian Chadd 	/* The TID state is protected behind the TXQ lock */
4067375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
4068eb6f0de0SAdrian Chadd 
4069eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bf=%p: fail=%d, hwq_depth now %d\n",
4070eb6f0de0SAdrian Chadd 	    __func__, bf, fail, atid->hwq_depth - 1);
4071eb6f0de0SAdrian Chadd 
4072eb6f0de0SAdrian Chadd 	atid->hwq_depth--;
4073f1bc738eSAdrian Chadd 
40740c54de88SAdrian Chadd #if 0
40750c54de88SAdrian Chadd 	/*
40760c54de88SAdrian Chadd 	 * If the frame was filtered, stick it on the filter frame
40770c54de88SAdrian Chadd 	 * queue and complain about it.  It shouldn't happen!
40780c54de88SAdrian Chadd 	 */
40790c54de88SAdrian Chadd 	if ((ts->ts_status & HAL_TXERR_FILT) ||
40800c54de88SAdrian Chadd 	    (ts->ts_status != 0 && atid->isfiltered)) {
408183bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX,
40820c54de88SAdrian Chadd 		    "%s: isfiltered=%d, ts_status=%d: huh?\n",
40830c54de88SAdrian Chadd 		    __func__,
40840c54de88SAdrian Chadd 		    atid->isfiltered,
40850c54de88SAdrian Chadd 		    ts->ts_status);
40860c54de88SAdrian Chadd 		ath_tx_tid_filt_comp_buf(sc, atid, bf);
40870c54de88SAdrian Chadd 	}
40880c54de88SAdrian Chadd #endif
4089f1bc738eSAdrian Chadd 	if (atid->isfiltered)
409083bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: filtered?!\n", __func__);
4091eb6f0de0SAdrian Chadd 	if (atid->hwq_depth < 0)
409283bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: hwq_depth < 0: %d\n",
4093eb6f0de0SAdrian Chadd 		    __func__, atid->hwq_depth);
4094f1bc738eSAdrian Chadd 
4095f1bc738eSAdrian Chadd 	/*
4096f1bc738eSAdrian Chadd 	 * If the queue is filtered, potentially mark it as complete
4097f1bc738eSAdrian Chadd 	 * and reschedule it as needed.
4098f1bc738eSAdrian Chadd 	 *
4099f1bc738eSAdrian Chadd 	 * This is required as there may be a subsequent TX descriptor
4100f1bc738eSAdrian Chadd 	 * for this end-node that has CLRDMASK set, so it's quite possible
4101f1bc738eSAdrian Chadd 	 * that a filtered frame will be followed by a non-filtered
4102f1bc738eSAdrian Chadd 	 * (complete or otherwise) frame.
4103f1bc738eSAdrian Chadd 	 *
4104f1bc738eSAdrian Chadd 	 * XXX should we do this before we complete the frame?
4105f1bc738eSAdrian Chadd 	 */
4106f1bc738eSAdrian Chadd 	if (atid->isfiltered)
4107f1bc738eSAdrian Chadd 		ath_tx_tid_filt_comp_complete(sc, atid);
4108375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
4109eb6f0de0SAdrian Chadd 
4110eb6f0de0SAdrian Chadd 	/*
4111eb6f0de0SAdrian Chadd 	 * punt to rate control if we're not being cleaned up
4112eb6f0de0SAdrian Chadd 	 * during a hw queue drain and the frame wanted an ACK.
4113eb6f0de0SAdrian Chadd 	 */
4114875a9451SAdrian Chadd 	if (fail == 0 && ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0))
4115eb6f0de0SAdrian Chadd 		ath_tx_update_ratectrl(sc, ni, bf->bf_state.bfs_rc,
4116eb6f0de0SAdrian Chadd 		    ts, bf->bf_state.bfs_pktlen,
4117eb6f0de0SAdrian Chadd 		    1, (ts->ts_status == 0) ? 0 : 1);
4118eb6f0de0SAdrian Chadd 
4119eb6f0de0SAdrian Chadd 	ath_tx_default_comp(sc, bf, fail);
4120eb6f0de0SAdrian Chadd }
4121eb6f0de0SAdrian Chadd 
4122eb6f0de0SAdrian Chadd /*
4123eb6f0de0SAdrian Chadd  * Handle cleanup of aggregate session packets that aren't
4124eb6f0de0SAdrian Chadd  * an A-MPDU.
4125eb6f0de0SAdrian Chadd  *
4126eb6f0de0SAdrian Chadd  * There's no need to update the BAW here - the session is being
4127eb6f0de0SAdrian Chadd  * torn down.
4128eb6f0de0SAdrian Chadd  */
4129eb6f0de0SAdrian Chadd static void
4130eb6f0de0SAdrian Chadd ath_tx_comp_cleanup_unaggr(struct ath_softc *sc, struct ath_buf *bf)
4131eb6f0de0SAdrian Chadd {
4132eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
4133eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4134eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
4135eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4136eb6f0de0SAdrian Chadd 
4137eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: TID %d: incomp=%d\n",
4138eb6f0de0SAdrian Chadd 	    __func__, tid, atid->incomp);
4139eb6f0de0SAdrian Chadd 
4140375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
4141eb6f0de0SAdrian Chadd 	atid->incomp--;
4142eb6f0de0SAdrian Chadd 	if (atid->incomp == 0) {
4143eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4144eb6f0de0SAdrian Chadd 		    "%s: TID %d: cleaned up! resume!\n",
4145eb6f0de0SAdrian Chadd 		    __func__, tid);
4146eb6f0de0SAdrian Chadd 		atid->cleanup_inprogress = 0;
4147eb6f0de0SAdrian Chadd 		ath_tx_tid_resume(sc, atid);
4148eb6f0de0SAdrian Chadd 	}
4149375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
4150eb6f0de0SAdrian Chadd 
4151eb6f0de0SAdrian Chadd 	ath_tx_default_comp(sc, bf, 0);
4152eb6f0de0SAdrian Chadd }
4153eb6f0de0SAdrian Chadd 
4154eb6f0de0SAdrian Chadd /*
4155eb6f0de0SAdrian Chadd  * Performs transmit side cleanup when TID changes from aggregated to
4156eb6f0de0SAdrian Chadd  * unaggregated.
4157eb6f0de0SAdrian Chadd  *
4158eb6f0de0SAdrian Chadd  * - Discard all retry frames from the s/w queue.
4159eb6f0de0SAdrian Chadd  * - Fix the tx completion function for all buffers in s/w queue.
4160eb6f0de0SAdrian Chadd  * - Count the number of unacked frames, and let transmit completion
4161eb6f0de0SAdrian Chadd  *   handle it later.
4162eb6f0de0SAdrian Chadd  *
41635da3fc10SAdrian Chadd  * The caller is responsible for pausing the TID and unpausing the
41645da3fc10SAdrian Chadd  * TID if no cleanup was required. Otherwise the cleanup path will
41655da3fc10SAdrian Chadd  * unpause the TID once the last hardware queued frame is completed.
4166eb6f0de0SAdrian Chadd  */
4167eb6f0de0SAdrian Chadd static void
416822780332SAdrian Chadd ath_tx_tid_cleanup(struct ath_softc *sc, struct ath_node *an, int tid,
416922780332SAdrian Chadd     ath_bufhead *bf_cq)
4170eb6f0de0SAdrian Chadd {
4171eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4172eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
4173eb6f0de0SAdrian Chadd 	struct ath_buf *bf, *bf_next;
417422780332SAdrian Chadd 
417522780332SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
4176eb6f0de0SAdrian Chadd 
4177d3a6425bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
4178eb6f0de0SAdrian Chadd 	    "%s: TID %d: called\n", __func__, tid);
4179eb6f0de0SAdrian Chadd 
4180eb6f0de0SAdrian Chadd 	/*
4181f1bc738eSAdrian Chadd 	 * Move the filtered frames to the TX queue, before
4182f1bc738eSAdrian Chadd 	 * we run off and discard/process things.
4183f1bc738eSAdrian Chadd 	 */
4184f1bc738eSAdrian Chadd 	/* XXX this is really quite inefficient */
418513aa9ee5SAdrian Chadd 	while ((bf = ATH_TID_FILT_LAST(atid, ath_bufhead_s)) != NULL) {
418613aa9ee5SAdrian Chadd 		ATH_TID_FILT_REMOVE(atid, bf, bf_list);
41873e6cc97fSAdrian Chadd 		ATH_TID_INSERT_HEAD(atid, bf, bf_list);
4188f1bc738eSAdrian Chadd 	}
4189f1bc738eSAdrian Chadd 
4190f1bc738eSAdrian Chadd 	/*
4191eb6f0de0SAdrian Chadd 	 * Update the frames in the software TX queue:
4192eb6f0de0SAdrian Chadd 	 *
4193eb6f0de0SAdrian Chadd 	 * + Discard retry frames in the queue
4194eb6f0de0SAdrian Chadd 	 * + Fix the completion function to be non-aggregate
4195eb6f0de0SAdrian Chadd 	 */
41963e6cc97fSAdrian Chadd 	bf = ATH_TID_FIRST(atid);
4197eb6f0de0SAdrian Chadd 	while (bf) {
4198eb6f0de0SAdrian Chadd 		if (bf->bf_state.bfs_isretried) {
4199eb6f0de0SAdrian Chadd 			bf_next = TAILQ_NEXT(bf, bf_list);
42003e6cc97fSAdrian Chadd 			ATH_TID_REMOVE(atid, bf, bf_list);
4201eb6f0de0SAdrian Chadd 			if (bf->bf_state.bfs_dobaw) {
4202eb6f0de0SAdrian Chadd 				ath_tx_update_baw(sc, an, atid, bf);
4203eb6f0de0SAdrian Chadd 				if (!bf->bf_state.bfs_addedbaw)
420483bbd5ebSRui Paulo 					DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
4205eb6f0de0SAdrian Chadd 					    "%s: wasn't added: seqno %d\n",
4206d4365d16SAdrian Chadd 					    __func__,
4207d4365d16SAdrian Chadd 					    SEQNO(bf->bf_state.bfs_seqno));
4208eb6f0de0SAdrian Chadd 			}
4209eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_dobaw = 0;
4210eb6f0de0SAdrian Chadd 			/*
4211eb6f0de0SAdrian Chadd 			 * Call the default completion handler with "fail" just
4212eb6f0de0SAdrian Chadd 			 * so upper levels are suitably notified about this.
4213eb6f0de0SAdrian Chadd 			 */
421422780332SAdrian Chadd 			TAILQ_INSERT_TAIL(bf_cq, bf, bf_list);
4215eb6f0de0SAdrian Chadd 			bf = bf_next;
4216eb6f0de0SAdrian Chadd 			continue;
4217eb6f0de0SAdrian Chadd 		}
4218eb6f0de0SAdrian Chadd 		/* Give these the default completion handler */
4219eb6f0de0SAdrian Chadd 		bf->bf_comp = ath_tx_normal_comp;
4220eb6f0de0SAdrian Chadd 		bf = TAILQ_NEXT(bf, bf_list);
4221eb6f0de0SAdrian Chadd 	}
4222eb6f0de0SAdrian Chadd 
4223eb6f0de0SAdrian Chadd 	/*
4224eb6f0de0SAdrian Chadd 	 * Calculate what hardware-queued frames exist based
4225eb6f0de0SAdrian Chadd 	 * on the current BAW size. Ie, what frames have been
4226eb6f0de0SAdrian Chadd 	 * added to the TX hardware queue for this TID but
4227eb6f0de0SAdrian Chadd 	 * not yet ACKed.
4228eb6f0de0SAdrian Chadd 	 */
4229eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
4230eb6f0de0SAdrian Chadd 	/* Need the lock - fiddling with BAW */
4231eb6f0de0SAdrian Chadd 	while (atid->baw_head != atid->baw_tail) {
4232eb6f0de0SAdrian Chadd 		if (atid->tx_buf[atid->baw_head]) {
4233eb6f0de0SAdrian Chadd 			atid->incomp++;
4234eb6f0de0SAdrian Chadd 			atid->cleanup_inprogress = 1;
4235eb6f0de0SAdrian Chadd 			atid->tx_buf[atid->baw_head] = NULL;
4236eb6f0de0SAdrian Chadd 		}
4237eb6f0de0SAdrian Chadd 		INCR(atid->baw_head, ATH_TID_MAX_BUFS);
4238eb6f0de0SAdrian Chadd 		INCR(tap->txa_start, IEEE80211_SEQ_RANGE);
4239eb6f0de0SAdrian Chadd 	}
4240eb6f0de0SAdrian Chadd 
4241eb6f0de0SAdrian Chadd 	if (atid->cleanup_inprogress)
4242eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4243eb6f0de0SAdrian Chadd 		    "%s: TID %d: cleanup needed: %d packets\n",
4244eb6f0de0SAdrian Chadd 		    __func__, tid, atid->incomp);
4245eb6f0de0SAdrian Chadd 
424622780332SAdrian Chadd 	/* Owner now must free completed frames */
4247eb6f0de0SAdrian Chadd }
4248eb6f0de0SAdrian Chadd 
4249eb6f0de0SAdrian Chadd static struct ath_buf *
425038962489SAdrian Chadd ath_tx_retry_clone(struct ath_softc *sc, struct ath_node *an,
425138962489SAdrian Chadd     struct ath_tid *tid, struct ath_buf *bf)
4252eb6f0de0SAdrian Chadd {
4253eb6f0de0SAdrian Chadd 	struct ath_buf *nbf;
4254eb6f0de0SAdrian Chadd 	int error;
4255eb6f0de0SAdrian Chadd 
42563f3a5dbdSAdrian Chadd 	/*
42573f3a5dbdSAdrian Chadd 	 * Clone the buffer.  This will handle the dma unmap and
42583f3a5dbdSAdrian Chadd 	 * copy the node reference to the new buffer.  If this
42593f3a5dbdSAdrian Chadd 	 * works out, 'bf' will have no DMA mapping, no mbuf
42603f3a5dbdSAdrian Chadd 	 * pointer and no node reference.
42613f3a5dbdSAdrian Chadd 	 */
4262eb6f0de0SAdrian Chadd 	nbf = ath_buf_clone(sc, bf);
4263eb6f0de0SAdrian Chadd 
4264eb6f0de0SAdrian Chadd #if 0
426583bbd5ebSRui Paulo 	DPRINTF(sc, ATH_DEBUG_XMIT, "%s: ATH_BUF_BUSY; cloning\n",
4266eb6f0de0SAdrian Chadd 	    __func__);
4267eb6f0de0SAdrian Chadd #endif
4268eb6f0de0SAdrian Chadd 
4269eb6f0de0SAdrian Chadd 	if (nbf == NULL) {
4270eb6f0de0SAdrian Chadd 		/* Failed to clone */
427183bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_XMIT,
4272eb6f0de0SAdrian Chadd 		    "%s: failed to clone a busy buffer\n",
4273eb6f0de0SAdrian Chadd 		    __func__);
4274eb6f0de0SAdrian Chadd 		return NULL;
4275eb6f0de0SAdrian Chadd 	}
4276eb6f0de0SAdrian Chadd 
4277eb6f0de0SAdrian Chadd 	/* Setup the dma for the new buffer */
4278eb6f0de0SAdrian Chadd 	error = ath_tx_dmasetup(sc, nbf, nbf->bf_m);
4279eb6f0de0SAdrian Chadd 	if (error != 0) {
428083bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_XMIT,
4281eb6f0de0SAdrian Chadd 		    "%s: failed to setup dma for clone\n",
4282eb6f0de0SAdrian Chadd 		    __func__);
4283eb6f0de0SAdrian Chadd 		/*
4284eb6f0de0SAdrian Chadd 		 * Put this at the head of the list, not tail;
4285eb6f0de0SAdrian Chadd 		 * that way it doesn't interfere with the
4286eb6f0de0SAdrian Chadd 		 * busy buffer logic (which uses the tail of
4287eb6f0de0SAdrian Chadd 		 * the list.)
4288eb6f0de0SAdrian Chadd 		 */
4289eb6f0de0SAdrian Chadd 		ATH_TXBUF_LOCK(sc);
429032c387f7SAdrian Chadd 		ath_returnbuf_head(sc, nbf);
4291eb6f0de0SAdrian Chadd 		ATH_TXBUF_UNLOCK(sc);
4292eb6f0de0SAdrian Chadd 		return NULL;
4293eb6f0de0SAdrian Chadd 	}
4294eb6f0de0SAdrian Chadd 
429538962489SAdrian Chadd 	/* Update BAW if required, before we free the original buf */
429638962489SAdrian Chadd 	if (bf->bf_state.bfs_dobaw)
429738962489SAdrian Chadd 		ath_tx_switch_baw_buf(sc, an, tid, bf, nbf);
429838962489SAdrian Chadd 
42993f3a5dbdSAdrian Chadd 	/* Free original buffer; return new buffer */
4300eb6f0de0SAdrian Chadd 	ath_freebuf(sc, bf);
4301f1bc738eSAdrian Chadd 
4302eb6f0de0SAdrian Chadd 	return nbf;
4303eb6f0de0SAdrian Chadd }
4304eb6f0de0SAdrian Chadd 
4305eb6f0de0SAdrian Chadd /*
4306eb6f0de0SAdrian Chadd  * Handle retrying an unaggregate frame in an aggregate
4307eb6f0de0SAdrian Chadd  * session.
4308eb6f0de0SAdrian Chadd  *
4309eb6f0de0SAdrian Chadd  * If too many retries occur, pause the TID, wait for
4310eb6f0de0SAdrian Chadd  * any further retransmits (as there's no reason why
4311eb6f0de0SAdrian Chadd  * non-aggregate frames in an aggregate session are
4312eb6f0de0SAdrian Chadd  * transmitted in-order; they just have to be in-BAW)
4313eb6f0de0SAdrian Chadd  * and then queue a BAR.
4314eb6f0de0SAdrian Chadd  */
4315eb6f0de0SAdrian Chadd static void
4316eb6f0de0SAdrian Chadd ath_tx_aggr_retry_unaggr(struct ath_softc *sc, struct ath_buf *bf)
4317eb6f0de0SAdrian Chadd {
4318eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
4319eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4320eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
4321eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4322eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
4323eb6f0de0SAdrian Chadd 
4324375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
4325eb6f0de0SAdrian Chadd 
4326eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
4327eb6f0de0SAdrian Chadd 
4328eb6f0de0SAdrian Chadd 	/*
4329eb6f0de0SAdrian Chadd 	 * If the buffer is marked as busy, we can't directly
4330eb6f0de0SAdrian Chadd 	 * reuse it. Instead, try to clone the buffer.
4331eb6f0de0SAdrian Chadd 	 * If the clone is successful, recycle the old buffer.
4332eb6f0de0SAdrian Chadd 	 * If the clone is unsuccessful, set bfs_retries to max
4333eb6f0de0SAdrian Chadd 	 * to force the next bit of code to free the buffer
4334eb6f0de0SAdrian Chadd 	 * for us.
4335eb6f0de0SAdrian Chadd 	 */
4336eb6f0de0SAdrian Chadd 	if ((bf->bf_state.bfs_retries < SWMAX_RETRIES) &&
4337eb6f0de0SAdrian Chadd 	    (bf->bf_flags & ATH_BUF_BUSY)) {
4338eb6f0de0SAdrian Chadd 		struct ath_buf *nbf;
433938962489SAdrian Chadd 		nbf = ath_tx_retry_clone(sc, an, atid, bf);
4340eb6f0de0SAdrian Chadd 		if (nbf)
4341eb6f0de0SAdrian Chadd 			/* bf has been freed at this point */
4342eb6f0de0SAdrian Chadd 			bf = nbf;
4343eb6f0de0SAdrian Chadd 		else
4344eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_retries = SWMAX_RETRIES + 1;
4345eb6f0de0SAdrian Chadd 	}
4346eb6f0de0SAdrian Chadd 
4347eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_retries >= SWMAX_RETRIES) {
4348eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_RETRIES,
4349eb6f0de0SAdrian Chadd 		    "%s: exceeded retries; seqno %d\n",
4350eb6f0de0SAdrian Chadd 		    __func__, SEQNO(bf->bf_state.bfs_seqno));
4351eb6f0de0SAdrian Chadd 		sc->sc_stats.ast_tx_swretrymax++;
4352eb6f0de0SAdrian Chadd 
4353eb6f0de0SAdrian Chadd 		/* Update BAW anyway */
4354eb6f0de0SAdrian Chadd 		if (bf->bf_state.bfs_dobaw) {
4355eb6f0de0SAdrian Chadd 			ath_tx_update_baw(sc, an, atid, bf);
4356eb6f0de0SAdrian Chadd 			if (! bf->bf_state.bfs_addedbaw)
435783bbd5ebSRui Paulo 				DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
4358eb6f0de0SAdrian Chadd 				    "%s: wasn't added: seqno %d\n",
4359eb6f0de0SAdrian Chadd 				    __func__, SEQNO(bf->bf_state.bfs_seqno));
4360eb6f0de0SAdrian Chadd 		}
4361eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_dobaw = 0;
4362eb6f0de0SAdrian Chadd 
436388b3d483SAdrian Chadd 		/* Suspend the TX queue and get ready to send the BAR */
436488b3d483SAdrian Chadd 		ath_tx_tid_bar_suspend(sc, atid);
436588b3d483SAdrian Chadd 
436688b3d483SAdrian Chadd 		/* Send the BAR if there are no other frames waiting */
436788b3d483SAdrian Chadd 		if (ath_tx_tid_bar_tx_ready(sc, atid))
436888b3d483SAdrian Chadd 			ath_tx_tid_bar_tx(sc, atid);
436988b3d483SAdrian Chadd 
4370375307d4SAdrian Chadd 		ATH_TX_UNLOCK(sc);
4371eb6f0de0SAdrian Chadd 
4372eb6f0de0SAdrian Chadd 		/* Free buffer, bf is free after this call */
4373eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
4374eb6f0de0SAdrian Chadd 		return;
4375eb6f0de0SAdrian Chadd 	}
4376eb6f0de0SAdrian Chadd 
4377eb6f0de0SAdrian Chadd 	/*
4378eb6f0de0SAdrian Chadd 	 * This increments the retry counter as well as
4379eb6f0de0SAdrian Chadd 	 * sets the retry flag in the ath_buf and packet
4380eb6f0de0SAdrian Chadd 	 * body.
4381eb6f0de0SAdrian Chadd 	 */
4382eb6f0de0SAdrian Chadd 	ath_tx_set_retry(sc, bf);
4383f1bc738eSAdrian Chadd 	sc->sc_stats.ast_tx_swretries++;
4384eb6f0de0SAdrian Chadd 
4385eb6f0de0SAdrian Chadd 	/*
4386eb6f0de0SAdrian Chadd 	 * Insert this at the head of the queue, so it's
4387eb6f0de0SAdrian Chadd 	 * retried before any current/subsequent frames.
4388eb6f0de0SAdrian Chadd 	 */
43893e6cc97fSAdrian Chadd 	ATH_TID_INSERT_HEAD(atid, bf, bf_list);
4390eb6f0de0SAdrian Chadd 	ath_tx_tid_sched(sc, atid);
439188b3d483SAdrian Chadd 	/* Send the BAR if there are no other frames waiting */
439288b3d483SAdrian Chadd 	if (ath_tx_tid_bar_tx_ready(sc, atid))
439388b3d483SAdrian Chadd 		ath_tx_tid_bar_tx(sc, atid);
4394eb6f0de0SAdrian Chadd 
4395375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
4396eb6f0de0SAdrian Chadd }
4397eb6f0de0SAdrian Chadd 
4398eb6f0de0SAdrian Chadd /*
4399eb6f0de0SAdrian Chadd  * Common code for aggregate excessive retry/subframe retry.
4400eb6f0de0SAdrian Chadd  * If retrying, queues buffers to bf_q. If not, frees the
4401eb6f0de0SAdrian Chadd  * buffers.
4402eb6f0de0SAdrian Chadd  *
4403eb6f0de0SAdrian Chadd  * XXX should unify this with ath_tx_aggr_retry_unaggr()
4404eb6f0de0SAdrian Chadd  */
4405eb6f0de0SAdrian Chadd static int
4406eb6f0de0SAdrian Chadd ath_tx_retry_subframe(struct ath_softc *sc, struct ath_buf *bf,
4407eb6f0de0SAdrian Chadd     ath_bufhead *bf_q)
4408eb6f0de0SAdrian Chadd {
4409eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
4410eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4411eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
4412eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4413eb6f0de0SAdrian Chadd 
4414375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
4415eb6f0de0SAdrian Chadd 
441621840808SAdrian Chadd 	/* XXX clr11naggr should be done for all subframes */
4417eb6f0de0SAdrian Chadd 	ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
4418eb6f0de0SAdrian Chadd 	ath_hal_set11nburstduration(sc->sc_ah, bf->bf_desc, 0);
4419f1bc738eSAdrian Chadd 
4420eb6f0de0SAdrian Chadd 	/* ath_hal_set11n_virtualmorefrag(sc->sc_ah, bf->bf_desc, 0); */
4421eb6f0de0SAdrian Chadd 
4422eb6f0de0SAdrian Chadd 	/*
4423eb6f0de0SAdrian Chadd 	 * If the buffer is marked as busy, we can't directly
4424eb6f0de0SAdrian Chadd 	 * reuse it. Instead, try to clone the buffer.
4425eb6f0de0SAdrian Chadd 	 * If the clone is successful, recycle the old buffer.
4426eb6f0de0SAdrian Chadd 	 * If the clone is unsuccessful, set bfs_retries to max
4427eb6f0de0SAdrian Chadd 	 * to force the next bit of code to free the buffer
4428eb6f0de0SAdrian Chadd 	 * for us.
4429eb6f0de0SAdrian Chadd 	 */
4430eb6f0de0SAdrian Chadd 	if ((bf->bf_state.bfs_retries < SWMAX_RETRIES) &&
4431eb6f0de0SAdrian Chadd 	    (bf->bf_flags & ATH_BUF_BUSY)) {
4432eb6f0de0SAdrian Chadd 		struct ath_buf *nbf;
443338962489SAdrian Chadd 		nbf = ath_tx_retry_clone(sc, an, atid, bf);
4434eb6f0de0SAdrian Chadd 		if (nbf)
4435eb6f0de0SAdrian Chadd 			/* bf has been freed at this point */
4436eb6f0de0SAdrian Chadd 			bf = nbf;
4437eb6f0de0SAdrian Chadd 		else
4438eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_retries = SWMAX_RETRIES + 1;
4439eb6f0de0SAdrian Chadd 	}
4440eb6f0de0SAdrian Chadd 
4441eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_retries >= SWMAX_RETRIES) {
4442eb6f0de0SAdrian Chadd 		sc->sc_stats.ast_tx_swretrymax++;
4443eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_RETRIES,
4444eb6f0de0SAdrian Chadd 		    "%s: max retries: seqno %d\n",
4445eb6f0de0SAdrian Chadd 		    __func__, SEQNO(bf->bf_state.bfs_seqno));
4446eb6f0de0SAdrian Chadd 		ath_tx_update_baw(sc, an, atid, bf);
4447eb6f0de0SAdrian Chadd 		if (!bf->bf_state.bfs_addedbaw)
444883bbd5ebSRui Paulo 			DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
4449eb6f0de0SAdrian Chadd 			    "%s: wasn't added: seqno %d\n",
4450eb6f0de0SAdrian Chadd 			    __func__, SEQNO(bf->bf_state.bfs_seqno));
4451eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_dobaw = 0;
4452eb6f0de0SAdrian Chadd 		return 1;
4453eb6f0de0SAdrian Chadd 	}
4454eb6f0de0SAdrian Chadd 
4455eb6f0de0SAdrian Chadd 	ath_tx_set_retry(sc, bf);
4456f1bc738eSAdrian Chadd 	sc->sc_stats.ast_tx_swretries++;
4457eb6f0de0SAdrian Chadd 	bf->bf_next = NULL;		/* Just to make sure */
4458eb6f0de0SAdrian Chadd 
445921840808SAdrian Chadd 	/* Clear the aggregate state */
446021840808SAdrian Chadd 	bf->bf_state.bfs_aggr = 0;
446121840808SAdrian Chadd 	bf->bf_state.bfs_ndelim = 0;	/* ??? needed? */
446221840808SAdrian Chadd 	bf->bf_state.bfs_nframes = 1;
446321840808SAdrian Chadd 
4464eb6f0de0SAdrian Chadd 	TAILQ_INSERT_TAIL(bf_q, bf, bf_list);
4465eb6f0de0SAdrian Chadd 	return 0;
4466eb6f0de0SAdrian Chadd }
4467eb6f0de0SAdrian Chadd 
4468eb6f0de0SAdrian Chadd /*
4469eb6f0de0SAdrian Chadd  * error pkt completion for an aggregate destination
4470eb6f0de0SAdrian Chadd  */
4471eb6f0de0SAdrian Chadd static void
4472eb6f0de0SAdrian Chadd ath_tx_comp_aggr_error(struct ath_softc *sc, struct ath_buf *bf_first,
4473eb6f0de0SAdrian Chadd     struct ath_tid *tid)
4474eb6f0de0SAdrian Chadd {
4475eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf_first->bf_node;
4476eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4477eb6f0de0SAdrian Chadd 	struct ath_buf *bf_next, *bf;
4478eb6f0de0SAdrian Chadd 	ath_bufhead bf_q;
4479eb6f0de0SAdrian Chadd 	int drops = 0;
4480eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
4481eb6f0de0SAdrian Chadd 	ath_bufhead bf_cq;
4482eb6f0de0SAdrian Chadd 
4483eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_q);
4484eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_cq);
4485eb6f0de0SAdrian Chadd 
4486eb6f0de0SAdrian Chadd 	/*
4487eb6f0de0SAdrian Chadd 	 * Update rate control - all frames have failed.
4488eb6f0de0SAdrian Chadd 	 *
4489eb6f0de0SAdrian Chadd 	 * XXX use the length in the first frame in the series;
4490eb6f0de0SAdrian Chadd 	 * XXX just so things are consistent for now.
4491eb6f0de0SAdrian Chadd 	 */
4492eb6f0de0SAdrian Chadd 	ath_tx_update_ratectrl(sc, ni, bf_first->bf_state.bfs_rc,
4493eb6f0de0SAdrian Chadd 	    &bf_first->bf_status.ds_txstat,
4494eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_pktlen,
4495eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_nframes, bf_first->bf_state.bfs_nframes);
4496eb6f0de0SAdrian Chadd 
4497375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
4498eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
44992d3d4776SAdrian Chadd 	sc->sc_stats.ast_tx_aggr_failall++;
4500eb6f0de0SAdrian Chadd 
4501eb6f0de0SAdrian Chadd 	/* Retry all subframes */
4502eb6f0de0SAdrian Chadd 	bf = bf_first;
4503eb6f0de0SAdrian Chadd 	while (bf) {
4504eb6f0de0SAdrian Chadd 		bf_next = bf->bf_next;
4505eb6f0de0SAdrian Chadd 		bf->bf_next = NULL;	/* Remove it from the aggr list */
45062d3d4776SAdrian Chadd 		sc->sc_stats.ast_tx_aggr_fail++;
4507eb6f0de0SAdrian Chadd 		if (ath_tx_retry_subframe(sc, bf, &bf_q)) {
4508eb6f0de0SAdrian Chadd 			drops++;
4509eb6f0de0SAdrian Chadd 			bf->bf_next = NULL;
4510eb6f0de0SAdrian Chadd 			TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
4511eb6f0de0SAdrian Chadd 		}
4512eb6f0de0SAdrian Chadd 		bf = bf_next;
4513eb6f0de0SAdrian Chadd 	}
4514eb6f0de0SAdrian Chadd 
4515eb6f0de0SAdrian Chadd 	/* Prepend all frames to the beginning of the queue */
4516eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_LAST(&bf_q, ath_bufhead_s)) != NULL) {
4517eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_q, bf, bf_list);
45183e6cc97fSAdrian Chadd 		ATH_TID_INSERT_HEAD(tid, bf, bf_list);
4519eb6f0de0SAdrian Chadd 	}
4520eb6f0de0SAdrian Chadd 
452139da9d42SAdrian Chadd 	/*
452239da9d42SAdrian Chadd 	 * Schedule the TID to be re-tried.
452339da9d42SAdrian Chadd 	 */
4524eb6f0de0SAdrian Chadd 	ath_tx_tid_sched(sc, tid);
4525eb6f0de0SAdrian Chadd 
4526eb6f0de0SAdrian Chadd 	/*
4527eb6f0de0SAdrian Chadd 	 * send bar if we dropped any frames
4528eb6f0de0SAdrian Chadd 	 *
4529eb6f0de0SAdrian Chadd 	 * Keep the txq lock held for now, as we need to ensure
4530eb6f0de0SAdrian Chadd 	 * that ni_txseqs[] is consistent (as it's being updated
4531eb6f0de0SAdrian Chadd 	 * in the ifnet TX context or raw TX context.)
4532eb6f0de0SAdrian Chadd 	 */
4533eb6f0de0SAdrian Chadd 	if (drops) {
453488b3d483SAdrian Chadd 		/* Suspend the TX queue and get ready to send the BAR */
453588b3d483SAdrian Chadd 		ath_tx_tid_bar_suspend(sc, tid);
4536eb6f0de0SAdrian Chadd 	}
4537eb6f0de0SAdrian Chadd 
453888b3d483SAdrian Chadd 	/*
453988b3d483SAdrian Chadd 	 * Send BAR if required
454088b3d483SAdrian Chadd 	 */
454188b3d483SAdrian Chadd 	if (ath_tx_tid_bar_tx_ready(sc, tid))
454288b3d483SAdrian Chadd 		ath_tx_tid_bar_tx(sc, tid);
4543f1bc738eSAdrian Chadd 
4544375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
454588b3d483SAdrian Chadd 
4546eb6f0de0SAdrian Chadd 	/* Complete frames which errored out */
4547eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
4548eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
4549eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
4550eb6f0de0SAdrian Chadd 	}
4551eb6f0de0SAdrian Chadd }
4552eb6f0de0SAdrian Chadd 
4553eb6f0de0SAdrian Chadd /*
4554eb6f0de0SAdrian Chadd  * Handle clean-up of packets from an aggregate list.
4555eb6f0de0SAdrian Chadd  *
4556eb6f0de0SAdrian Chadd  * There's no need to update the BAW here - the session is being
4557eb6f0de0SAdrian Chadd  * torn down.
4558eb6f0de0SAdrian Chadd  */
4559eb6f0de0SAdrian Chadd static void
4560eb6f0de0SAdrian Chadd ath_tx_comp_cleanup_aggr(struct ath_softc *sc, struct ath_buf *bf_first)
4561eb6f0de0SAdrian Chadd {
4562eb6f0de0SAdrian Chadd 	struct ath_buf *bf, *bf_next;
4563eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf_first->bf_node;
4564eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4565eb6f0de0SAdrian Chadd 	int tid = bf_first->bf_state.bfs_tid;
4566eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4567eb6f0de0SAdrian Chadd 
4568375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
4569eb6f0de0SAdrian Chadd 
4570eb6f0de0SAdrian Chadd 	/* update incomp */
4571302868d9SAdrian Chadd 	bf = bf_first;
4572eb6f0de0SAdrian Chadd 	while (bf) {
4573eb6f0de0SAdrian Chadd 		atid->incomp--;
4574eb6f0de0SAdrian Chadd 		bf = bf->bf_next;
4575eb6f0de0SAdrian Chadd 	}
4576eb6f0de0SAdrian Chadd 
4577eb6f0de0SAdrian Chadd 	if (atid->incomp == 0) {
4578eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4579eb6f0de0SAdrian Chadd 		    "%s: TID %d: cleaned up! resume!\n",
4580eb6f0de0SAdrian Chadd 		    __func__, tid);
4581eb6f0de0SAdrian Chadd 		atid->cleanup_inprogress = 0;
4582eb6f0de0SAdrian Chadd 		ath_tx_tid_resume(sc, atid);
4583eb6f0de0SAdrian Chadd 	}
458488b3d483SAdrian Chadd 
458588b3d483SAdrian Chadd 	/* Send BAR if required */
4586f1bc738eSAdrian Chadd 	/* XXX why would we send a BAR when transitioning to non-aggregation? */
4587302868d9SAdrian Chadd 	/*
4588302868d9SAdrian Chadd 	 * XXX TODO: we should likely just tear down the BAR state here,
4589302868d9SAdrian Chadd 	 * rather than sending a BAR.
4590302868d9SAdrian Chadd 	 */
459188b3d483SAdrian Chadd 	if (ath_tx_tid_bar_tx_ready(sc, atid))
459288b3d483SAdrian Chadd 		ath_tx_tid_bar_tx(sc, atid);
4593f1bc738eSAdrian Chadd 
4594375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
4595eb6f0de0SAdrian Chadd 
4596eb6f0de0SAdrian Chadd 	/* Handle frame completion */
4597302868d9SAdrian Chadd 	bf = bf_first;
4598eb6f0de0SAdrian Chadd 	while (bf) {
4599eb6f0de0SAdrian Chadd 		bf_next = bf->bf_next;
4600eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 1);
4601eb6f0de0SAdrian Chadd 		bf = bf_next;
4602eb6f0de0SAdrian Chadd 	}
4603eb6f0de0SAdrian Chadd }
4604eb6f0de0SAdrian Chadd 
4605eb6f0de0SAdrian Chadd /*
4606eb6f0de0SAdrian Chadd  * Handle completion of an set of aggregate frames.
4607eb6f0de0SAdrian Chadd  *
4608eb6f0de0SAdrian Chadd  * Note: the completion handler is the last descriptor in the aggregate,
4609eb6f0de0SAdrian Chadd  * not the last descriptor in the first frame.
4610eb6f0de0SAdrian Chadd  */
4611eb6f0de0SAdrian Chadd static void
4612d4365d16SAdrian Chadd ath_tx_aggr_comp_aggr(struct ath_softc *sc, struct ath_buf *bf_first,
4613d4365d16SAdrian Chadd     int fail)
4614eb6f0de0SAdrian Chadd {
4615eb6f0de0SAdrian Chadd 	//struct ath_desc *ds = bf->bf_lastds;
4616eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf_first->bf_node;
4617eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4618eb6f0de0SAdrian Chadd 	int tid = bf_first->bf_state.bfs_tid;
4619eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4620eb6f0de0SAdrian Chadd 	struct ath_tx_status ts;
4621eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
4622eb6f0de0SAdrian Chadd 	ath_bufhead bf_q;
4623eb6f0de0SAdrian Chadd 	ath_bufhead bf_cq;
4624eb6f0de0SAdrian Chadd 	int seq_st, tx_ok;
4625eb6f0de0SAdrian Chadd 	int hasba, isaggr;
4626eb6f0de0SAdrian Chadd 	uint32_t ba[2];
4627eb6f0de0SAdrian Chadd 	struct ath_buf *bf, *bf_next;
4628eb6f0de0SAdrian Chadd 	int ba_index;
4629eb6f0de0SAdrian Chadd 	int drops = 0;
4630eb6f0de0SAdrian Chadd 	int nframes = 0, nbad = 0, nf;
4631eb6f0de0SAdrian Chadd 	int pktlen;
4632eb6f0de0SAdrian Chadd 	/* XXX there's too much on the stack? */
4633b815538dSAdrian Chadd 	struct ath_rc_series rc[ATH_RC_NUM];
4634eb6f0de0SAdrian Chadd 	int txseq;
4635eb6f0de0SAdrian Chadd 
4636eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: called; hwq_depth=%d\n",
4637eb6f0de0SAdrian Chadd 	    __func__, atid->hwq_depth);
4638eb6f0de0SAdrian Chadd 
46390aa5c1bbSAdrian Chadd 	/*
46400aa5c1bbSAdrian Chadd 	 * Take a copy; this may be needed -after- bf_first
46410aa5c1bbSAdrian Chadd 	 * has been completed and freed.
46420aa5c1bbSAdrian Chadd 	 */
46430aa5c1bbSAdrian Chadd 	ts = bf_first->bf_status.ds_txstat;
46440aa5c1bbSAdrian Chadd 
4645f1bc738eSAdrian Chadd 	TAILQ_INIT(&bf_q);
4646f1bc738eSAdrian Chadd 	TAILQ_INIT(&bf_cq);
4647f1bc738eSAdrian Chadd 
4648eb6f0de0SAdrian Chadd 	/* The TID state is kept behind the TXQ lock */
4649375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
4650eb6f0de0SAdrian Chadd 
4651eb6f0de0SAdrian Chadd 	atid->hwq_depth--;
4652eb6f0de0SAdrian Chadd 	if (atid->hwq_depth < 0)
465383bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: hwq_depth < 0: %d\n",
4654eb6f0de0SAdrian Chadd 		    __func__, atid->hwq_depth);
4655eb6f0de0SAdrian Chadd 
4656eb6f0de0SAdrian Chadd 	/*
4657f1bc738eSAdrian Chadd 	 * If the TID is filtered, handle completing the filter
4658f1bc738eSAdrian Chadd 	 * transition before potentially kicking it to the cleanup
4659f1bc738eSAdrian Chadd 	 * function.
46600aa5c1bbSAdrian Chadd 	 *
46610aa5c1bbSAdrian Chadd 	 * XXX this is duplicate work, ew.
4662f1bc738eSAdrian Chadd 	 */
4663f1bc738eSAdrian Chadd 	if (atid->isfiltered)
4664f1bc738eSAdrian Chadd 		ath_tx_tid_filt_comp_complete(sc, atid);
4665f1bc738eSAdrian Chadd 
4666f1bc738eSAdrian Chadd 	/*
4667eb6f0de0SAdrian Chadd 	 * Punt cleanup to the relevant function, not our problem now
4668eb6f0de0SAdrian Chadd 	 */
4669eb6f0de0SAdrian Chadd 	if (atid->cleanup_inprogress) {
4670f1bc738eSAdrian Chadd 		if (atid->isfiltered)
467183bbd5ebSRui Paulo 			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4672f1bc738eSAdrian Chadd 			    "%s: isfiltered=1, normal_comp?\n",
4673f1bc738eSAdrian Chadd 			    __func__);
4674375307d4SAdrian Chadd 		ATH_TX_UNLOCK(sc);
4675eb6f0de0SAdrian Chadd 		ath_tx_comp_cleanup_aggr(sc, bf_first);
4676eb6f0de0SAdrian Chadd 		return;
4677eb6f0de0SAdrian Chadd 	}
4678eb6f0de0SAdrian Chadd 
4679eb6f0de0SAdrian Chadd 	/*
4680f1bc738eSAdrian Chadd 	 * If the frame is filtered, transition to filtered frame
4681f1bc738eSAdrian Chadd 	 * mode and add this to the filtered frame list.
4682f1bc738eSAdrian Chadd 	 *
4683f1bc738eSAdrian Chadd 	 * XXX TODO: figure out how this interoperates with
4684f1bc738eSAdrian Chadd 	 * BAR, pause and cleanup states.
4685f1bc738eSAdrian Chadd 	 */
4686f1bc738eSAdrian Chadd 	if ((ts.ts_status & HAL_TXERR_FILT) ||
4687f1bc738eSAdrian Chadd 	    (ts.ts_status != 0 && atid->isfiltered)) {
4688f1bc738eSAdrian Chadd 		if (fail != 0)
468983bbd5ebSRui Paulo 			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4690f1bc738eSAdrian Chadd 			    "%s: isfiltered=1, fail=%d\n", __func__, fail);
4691f1bc738eSAdrian Chadd 		ath_tx_tid_filt_comp_aggr(sc, atid, bf_first, &bf_cq);
4692f1bc738eSAdrian Chadd 
4693f1bc738eSAdrian Chadd 		/* Remove from BAW */
4694f1bc738eSAdrian Chadd 		TAILQ_FOREACH_SAFE(bf, &bf_cq, bf_list, bf_next) {
4695f1bc738eSAdrian Chadd 			if (bf->bf_state.bfs_addedbaw)
4696f1bc738eSAdrian Chadd 				drops++;
4697f1bc738eSAdrian Chadd 			if (bf->bf_state.bfs_dobaw) {
4698f1bc738eSAdrian Chadd 				ath_tx_update_baw(sc, an, atid, bf);
4699f1bc738eSAdrian Chadd 				if (!bf->bf_state.bfs_addedbaw)
470083bbd5ebSRui Paulo 					DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4701f1bc738eSAdrian Chadd 					    "%s: wasn't added: seqno %d\n",
4702f1bc738eSAdrian Chadd 					    __func__,
4703f1bc738eSAdrian Chadd 					    SEQNO(bf->bf_state.bfs_seqno));
4704f1bc738eSAdrian Chadd 			}
4705f1bc738eSAdrian Chadd 			bf->bf_state.bfs_dobaw = 0;
4706f1bc738eSAdrian Chadd 		}
4707f1bc738eSAdrian Chadd 		/*
4708f1bc738eSAdrian Chadd 		 * If any intermediate frames in the BAW were dropped when
4709f1bc738eSAdrian Chadd 		 * handling filtering things, send a BAR.
4710f1bc738eSAdrian Chadd 		 */
4711f1bc738eSAdrian Chadd 		if (drops)
4712f1bc738eSAdrian Chadd 			ath_tx_tid_bar_suspend(sc, atid);
4713f1bc738eSAdrian Chadd 
4714f1bc738eSAdrian Chadd 		/*
4715f1bc738eSAdrian Chadd 		 * Finish up by sending a BAR if required and freeing
4716f1bc738eSAdrian Chadd 		 * the frames outside of the TX lock.
4717f1bc738eSAdrian Chadd 		 */
4718f1bc738eSAdrian Chadd 		goto finish_send_bar;
4719f1bc738eSAdrian Chadd 	}
4720f1bc738eSAdrian Chadd 
4721f1bc738eSAdrian Chadd 	/*
4722eb6f0de0SAdrian Chadd 	 * XXX for now, use the first frame in the aggregate for
4723eb6f0de0SAdrian Chadd 	 * XXX rate control completion; it's at least consistent.
4724eb6f0de0SAdrian Chadd 	 */
4725eb6f0de0SAdrian Chadd 	pktlen = bf_first->bf_state.bfs_pktlen;
4726eb6f0de0SAdrian Chadd 
4727eb6f0de0SAdrian Chadd 	/*
4728e9a6408eSAdrian Chadd 	 * Handle errors first!
4729e9a6408eSAdrian Chadd 	 *
4730e9a6408eSAdrian Chadd 	 * Here, handle _any_ error as a "exceeded retries" error.
4731e9a6408eSAdrian Chadd 	 * Later on (when filtered frames are to be specially handled)
4732e9a6408eSAdrian Chadd 	 * it'll have to be expanded.
4733eb6f0de0SAdrian Chadd 	 */
4734e9a6408eSAdrian Chadd #if 0
4735eb6f0de0SAdrian Chadd 	if (ts.ts_status & HAL_TXERR_XRETRY) {
4736e9a6408eSAdrian Chadd #endif
4737e9a6408eSAdrian Chadd 	if (ts.ts_status != 0) {
4738375307d4SAdrian Chadd 		ATH_TX_UNLOCK(sc);
4739eb6f0de0SAdrian Chadd 		ath_tx_comp_aggr_error(sc, bf_first, atid);
4740eb6f0de0SAdrian Chadd 		return;
4741eb6f0de0SAdrian Chadd 	}
4742eb6f0de0SAdrian Chadd 
4743eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
4744eb6f0de0SAdrian Chadd 
4745eb6f0de0SAdrian Chadd 	/*
4746eb6f0de0SAdrian Chadd 	 * extract starting sequence and block-ack bitmap
4747eb6f0de0SAdrian Chadd 	 */
4748eb6f0de0SAdrian Chadd 	/* XXX endian-ness of seq_st, ba? */
4749eb6f0de0SAdrian Chadd 	seq_st = ts.ts_seqnum;
4750eb6f0de0SAdrian Chadd 	hasba = !! (ts.ts_flags & HAL_TX_BA);
4751eb6f0de0SAdrian Chadd 	tx_ok = (ts.ts_status == 0);
4752eb6f0de0SAdrian Chadd 	isaggr = bf_first->bf_state.bfs_aggr;
4753eb6f0de0SAdrian Chadd 	ba[0] = ts.ts_ba_low;
4754eb6f0de0SAdrian Chadd 	ba[1] = ts.ts_ba_high;
4755eb6f0de0SAdrian Chadd 
4756eb6f0de0SAdrian Chadd 	/*
4757eb6f0de0SAdrian Chadd 	 * Copy the TX completion status and the rate control
4758eb6f0de0SAdrian Chadd 	 * series from the first descriptor, as it may be freed
4759eb6f0de0SAdrian Chadd 	 * before the rate control code can get its grubby fingers
4760eb6f0de0SAdrian Chadd 	 * into things.
4761eb6f0de0SAdrian Chadd 	 */
4762eb6f0de0SAdrian Chadd 	memcpy(rc, bf_first->bf_state.bfs_rc, sizeof(rc));
4763eb6f0de0SAdrian Chadd 
4764eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4765d4365d16SAdrian Chadd 	    "%s: txa_start=%d, tx_ok=%d, status=%.8x, flags=%.8x, "
4766d4365d16SAdrian Chadd 	    "isaggr=%d, seq_st=%d, hasba=%d, ba=%.8x, %.8x\n",
4767eb6f0de0SAdrian Chadd 	    __func__, tap->txa_start, tx_ok, ts.ts_status, ts.ts_flags,
4768eb6f0de0SAdrian Chadd 	    isaggr, seq_st, hasba, ba[0], ba[1]);
4769eb6f0de0SAdrian Chadd 
4770b3420862SAdrian Chadd 	/*
4771b3420862SAdrian Chadd 	 * The reference driver doesn't do this; it simply ignores
4772b3420862SAdrian Chadd 	 * this check in its entirety.
4773b3420862SAdrian Chadd 	 *
4774b3420862SAdrian Chadd 	 * I've seen this occur when using iperf to send traffic
4775b3420862SAdrian Chadd 	 * out tid 1 - the aggregate frames are all marked as TID 1,
4776b3420862SAdrian Chadd 	 * but the TXSTATUS has TID=0.  So, let's just ignore this
4777b3420862SAdrian Chadd 	 * check.
4778b3420862SAdrian Chadd 	 */
4779b3420862SAdrian Chadd #if 0
4780eb6f0de0SAdrian Chadd 	/* Occasionally, the MAC sends a tx status for the wrong TID. */
4781eb6f0de0SAdrian Chadd 	if (tid != ts.ts_tid) {
478283bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: tid %d != hw tid %d\n",
4783eb6f0de0SAdrian Chadd 		    __func__, tid, ts.ts_tid);
4784eb6f0de0SAdrian Chadd 		tx_ok = 0;
4785eb6f0de0SAdrian Chadd 	}
4786b3420862SAdrian Chadd #endif
4787eb6f0de0SAdrian Chadd 
4788eb6f0de0SAdrian Chadd 	/* AR5416 BA bug; this requires an interface reset */
4789eb6f0de0SAdrian Chadd 	if (isaggr && tx_ok && (! hasba)) {
479083bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4791d4365d16SAdrian Chadd 		    "%s: AR5416 bug: hasba=%d; txok=%d, isaggr=%d, "
4792d4365d16SAdrian Chadd 		    "seq_st=%d\n",
4793eb6f0de0SAdrian Chadd 		    __func__, hasba, tx_ok, isaggr, seq_st);
4794eb6f0de0SAdrian Chadd 		/* XXX TODO: schedule an interface reset */
47950f078d63SJohn Baldwin #ifdef ATH_DEBUG
47966abbbae5SAdrian Chadd 		ath_printtxbuf(sc, bf_first,
47976abbbae5SAdrian Chadd 		    sc->sc_ac2q[atid->ac]->axq_qnum, 0, 0);
47980f078d63SJohn Baldwin #endif
4799eb6f0de0SAdrian Chadd 	}
4800eb6f0de0SAdrian Chadd 
4801eb6f0de0SAdrian Chadd 	/*
4802eb6f0de0SAdrian Chadd 	 * Walk the list of frames, figure out which ones were correctly
4803eb6f0de0SAdrian Chadd 	 * sent and which weren't.
4804eb6f0de0SAdrian Chadd 	 */
4805eb6f0de0SAdrian Chadd 	bf = bf_first;
4806eb6f0de0SAdrian Chadd 	nf = bf_first->bf_state.bfs_nframes;
4807eb6f0de0SAdrian Chadd 
4808eb6f0de0SAdrian Chadd 	/* bf_first is going to be invalid once this list is walked */
4809eb6f0de0SAdrian Chadd 	bf_first = NULL;
4810eb6f0de0SAdrian Chadd 
4811eb6f0de0SAdrian Chadd 	/*
4812eb6f0de0SAdrian Chadd 	 * Walk the list of completed frames and determine
4813eb6f0de0SAdrian Chadd 	 * which need to be completed and which need to be
4814eb6f0de0SAdrian Chadd 	 * retransmitted.
4815eb6f0de0SAdrian Chadd 	 *
4816eb6f0de0SAdrian Chadd 	 * For completed frames, the completion functions need
4817eb6f0de0SAdrian Chadd 	 * to be called at the end of this function as the last
4818eb6f0de0SAdrian Chadd 	 * node reference may free the node.
4819eb6f0de0SAdrian Chadd 	 *
4820eb6f0de0SAdrian Chadd 	 * Finally, since the TXQ lock can't be held during the
4821eb6f0de0SAdrian Chadd 	 * completion callback (to avoid lock recursion),
4822eb6f0de0SAdrian Chadd 	 * the completion calls have to be done outside of the
4823eb6f0de0SAdrian Chadd 	 * lock.
4824eb6f0de0SAdrian Chadd 	 */
4825eb6f0de0SAdrian Chadd 	while (bf) {
4826eb6f0de0SAdrian Chadd 		nframes++;
4827d4365d16SAdrian Chadd 		ba_index = ATH_BA_INDEX(seq_st,
4828d4365d16SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
4829eb6f0de0SAdrian Chadd 		bf_next = bf->bf_next;
4830eb6f0de0SAdrian Chadd 		bf->bf_next = NULL;	/* Remove it from the aggr list */
4831eb6f0de0SAdrian Chadd 
4832eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4833eb6f0de0SAdrian Chadd 		    "%s: checking bf=%p seqno=%d; ack=%d\n",
4834eb6f0de0SAdrian Chadd 		    __func__, bf, SEQNO(bf->bf_state.bfs_seqno),
4835eb6f0de0SAdrian Chadd 		    ATH_BA_ISSET(ba, ba_index));
4836eb6f0de0SAdrian Chadd 
4837eb6f0de0SAdrian Chadd 		if (tx_ok && ATH_BA_ISSET(ba, ba_index)) {
48382d3d4776SAdrian Chadd 			sc->sc_stats.ast_tx_aggr_ok++;
4839eb6f0de0SAdrian Chadd 			ath_tx_update_baw(sc, an, atid, bf);
4840eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_dobaw = 0;
4841eb6f0de0SAdrian Chadd 			if (!bf->bf_state.bfs_addedbaw)
484283bbd5ebSRui Paulo 				DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4843eb6f0de0SAdrian Chadd 				    "%s: wasn't added: seqno %d\n",
4844eb6f0de0SAdrian Chadd 				    __func__, SEQNO(bf->bf_state.bfs_seqno));
4845eb6f0de0SAdrian Chadd 			bf->bf_next = NULL;
4846eb6f0de0SAdrian Chadd 			TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
4847eb6f0de0SAdrian Chadd 		} else {
48482d3d4776SAdrian Chadd 			sc->sc_stats.ast_tx_aggr_fail++;
4849eb6f0de0SAdrian Chadd 			if (ath_tx_retry_subframe(sc, bf, &bf_q)) {
4850eb6f0de0SAdrian Chadd 				drops++;
4851eb6f0de0SAdrian Chadd 				bf->bf_next = NULL;
4852eb6f0de0SAdrian Chadd 				TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
4853eb6f0de0SAdrian Chadd 			}
4854eb6f0de0SAdrian Chadd 			nbad++;
4855eb6f0de0SAdrian Chadd 		}
4856eb6f0de0SAdrian Chadd 		bf = bf_next;
4857eb6f0de0SAdrian Chadd 	}
4858eb6f0de0SAdrian Chadd 
4859eb6f0de0SAdrian Chadd 	/*
4860eb6f0de0SAdrian Chadd 	 * Now that the BAW updates have been done, unlock
4861eb6f0de0SAdrian Chadd 	 *
4862eb6f0de0SAdrian Chadd 	 * txseq is grabbed before the lock is released so we
4863eb6f0de0SAdrian Chadd 	 * have a consistent view of what -was- in the BAW.
4864eb6f0de0SAdrian Chadd 	 * Anything after this point will not yet have been
4865eb6f0de0SAdrian Chadd 	 * TXed.
4866eb6f0de0SAdrian Chadd 	 */
4867eb6f0de0SAdrian Chadd 	txseq = tap->txa_start;
4868375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
4869eb6f0de0SAdrian Chadd 
4870eb6f0de0SAdrian Chadd 	if (nframes != nf)
487183bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4872eb6f0de0SAdrian Chadd 		    "%s: num frames seen=%d; bf nframes=%d\n",
4873eb6f0de0SAdrian Chadd 		    __func__, nframes, nf);
4874eb6f0de0SAdrian Chadd 
4875eb6f0de0SAdrian Chadd 	/*
4876eb6f0de0SAdrian Chadd 	 * Now we know how many frames were bad, call the rate
4877eb6f0de0SAdrian Chadd 	 * control code.
4878eb6f0de0SAdrian Chadd 	 */
4879eb6f0de0SAdrian Chadd 	if (fail == 0)
4880d4365d16SAdrian Chadd 		ath_tx_update_ratectrl(sc, ni, rc, &ts, pktlen, nframes,
4881d4365d16SAdrian Chadd 		    nbad);
4882eb6f0de0SAdrian Chadd 
4883eb6f0de0SAdrian Chadd 	/*
4884eb6f0de0SAdrian Chadd 	 * send bar if we dropped any frames
4885eb6f0de0SAdrian Chadd 	 */
4886eb6f0de0SAdrian Chadd 	if (drops) {
488788b3d483SAdrian Chadd 		/* Suspend the TX queue and get ready to send the BAR */
4888375307d4SAdrian Chadd 		ATH_TX_LOCK(sc);
488988b3d483SAdrian Chadd 		ath_tx_tid_bar_suspend(sc, atid);
4890375307d4SAdrian Chadd 		ATH_TX_UNLOCK(sc);
4891eb6f0de0SAdrian Chadd 	}
4892eb6f0de0SAdrian Chadd 
489339da9d42SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
489439da9d42SAdrian Chadd 	    "%s: txa_start now %d\n", __func__, tap->txa_start);
489539da9d42SAdrian Chadd 
4896375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
489739da9d42SAdrian Chadd 
489839da9d42SAdrian Chadd 	/* Prepend all frames to the beginning of the queue */
4899eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_LAST(&bf_q, ath_bufhead_s)) != NULL) {
4900eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_q, bf, bf_list);
49013e6cc97fSAdrian Chadd 		ATH_TID_INSERT_HEAD(atid, bf, bf_list);
4902eb6f0de0SAdrian Chadd 	}
4903eb6f0de0SAdrian Chadd 
490439da9d42SAdrian Chadd 	/*
490539da9d42SAdrian Chadd 	 * Reschedule to grab some further frames.
490639da9d42SAdrian Chadd 	 */
490739da9d42SAdrian Chadd 	ath_tx_tid_sched(sc, atid);
4908eb6f0de0SAdrian Chadd 
490988b3d483SAdrian Chadd 	/*
4910f1bc738eSAdrian Chadd 	 * If the queue is filtered, re-schedule as required.
4911f1bc738eSAdrian Chadd 	 *
4912f1bc738eSAdrian Chadd 	 * This is required as there may be a subsequent TX descriptor
4913f1bc738eSAdrian Chadd 	 * for this end-node that has CLRDMASK set, so it's quite possible
4914f1bc738eSAdrian Chadd 	 * that a filtered frame will be followed by a non-filtered
4915f1bc738eSAdrian Chadd 	 * (complete or otherwise) frame.
4916f1bc738eSAdrian Chadd 	 *
4917f1bc738eSAdrian Chadd 	 * XXX should we do this before we complete the frame?
4918f1bc738eSAdrian Chadd 	 */
4919f1bc738eSAdrian Chadd 	if (atid->isfiltered)
4920f1bc738eSAdrian Chadd 		ath_tx_tid_filt_comp_complete(sc, atid);
4921f1bc738eSAdrian Chadd 
4922f1bc738eSAdrian Chadd finish_send_bar:
4923f1bc738eSAdrian Chadd 
4924f1bc738eSAdrian Chadd 	/*
492588b3d483SAdrian Chadd 	 * Send BAR if required
492688b3d483SAdrian Chadd 	 */
492788b3d483SAdrian Chadd 	if (ath_tx_tid_bar_tx_ready(sc, atid))
492888b3d483SAdrian Chadd 		ath_tx_tid_bar_tx(sc, atid);
492939da9d42SAdrian Chadd 
4930375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
493188b3d483SAdrian Chadd 
4932eb6f0de0SAdrian Chadd 	/* Do deferred completion */
4933eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
4934eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
4935eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
4936eb6f0de0SAdrian Chadd 	}
4937eb6f0de0SAdrian Chadd }
4938eb6f0de0SAdrian Chadd 
4939eb6f0de0SAdrian Chadd /*
4940eb6f0de0SAdrian Chadd  * Handle completion of unaggregated frames in an ADDBA
4941eb6f0de0SAdrian Chadd  * session.
4942eb6f0de0SAdrian Chadd  *
4943eb6f0de0SAdrian Chadd  * Fail is set to 1 if the entry is being freed via a call to
4944eb6f0de0SAdrian Chadd  * ath_tx_draintxq().
4945eb6f0de0SAdrian Chadd  */
4946eb6f0de0SAdrian Chadd static void
4947eb6f0de0SAdrian Chadd ath_tx_aggr_comp_unaggr(struct ath_softc *sc, struct ath_buf *bf, int fail)
4948eb6f0de0SAdrian Chadd {
4949eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
4950eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4951eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
4952eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
49530aa5c1bbSAdrian Chadd 	struct ath_tx_status ts;
4954f1bc738eSAdrian Chadd 	int drops = 0;
4955eb6f0de0SAdrian Chadd 
4956eb6f0de0SAdrian Chadd 	/*
49570aa5c1bbSAdrian Chadd 	 * Take a copy of this; filtering/cloning the frame may free the
49580aa5c1bbSAdrian Chadd 	 * bf pointer.
49590aa5c1bbSAdrian Chadd 	 */
49600aa5c1bbSAdrian Chadd 	ts = bf->bf_status.ds_txstat;
49610aa5c1bbSAdrian Chadd 
49620aa5c1bbSAdrian Chadd 	/*
4963eb6f0de0SAdrian Chadd 	 * Update rate control status here, before we possibly
4964eb6f0de0SAdrian Chadd 	 * punt to retry or cleanup.
4965eb6f0de0SAdrian Chadd 	 *
4966eb6f0de0SAdrian Chadd 	 * Do it outside of the TXQ lock.
4967eb6f0de0SAdrian Chadd 	 */
4968875a9451SAdrian Chadd 	if (fail == 0 && ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0))
4969eb6f0de0SAdrian Chadd 		ath_tx_update_ratectrl(sc, ni, bf->bf_state.bfs_rc,
4970eb6f0de0SAdrian Chadd 		    &bf->bf_status.ds_txstat,
4971eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_pktlen,
49720aa5c1bbSAdrian Chadd 		    1, (ts.ts_status == 0) ? 0 : 1);
4973eb6f0de0SAdrian Chadd 
4974eb6f0de0SAdrian Chadd 	/*
4975eb6f0de0SAdrian Chadd 	 * This is called early so atid->hwq_depth can be tracked.
4976eb6f0de0SAdrian Chadd 	 * This unfortunately means that it's released and regrabbed
4977eb6f0de0SAdrian Chadd 	 * during retry and cleanup. That's rather inefficient.
4978eb6f0de0SAdrian Chadd 	 */
4979375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
4980eb6f0de0SAdrian Chadd 
4981eb6f0de0SAdrian Chadd 	if (tid == IEEE80211_NONQOS_TID)
498283bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: TID=16!\n", __func__);
4983eb6f0de0SAdrian Chadd 
4984d4365d16SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX,
4985d4365d16SAdrian Chadd 	    "%s: bf=%p: tid=%d, hwq_depth=%d, seqno=%d\n",
4986d4365d16SAdrian Chadd 	    __func__, bf, bf->bf_state.bfs_tid, atid->hwq_depth,
4987d4365d16SAdrian Chadd 	    SEQNO(bf->bf_state.bfs_seqno));
4988eb6f0de0SAdrian Chadd 
4989eb6f0de0SAdrian Chadd 	atid->hwq_depth--;
4990eb6f0de0SAdrian Chadd 	if (atid->hwq_depth < 0)
499183bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: hwq_depth < 0: %d\n",
4992eb6f0de0SAdrian Chadd 		    __func__, atid->hwq_depth);
4993eb6f0de0SAdrian Chadd 
4994eb6f0de0SAdrian Chadd 	/*
4995f1bc738eSAdrian Chadd 	 * If the TID is filtered, handle completing the filter
4996f1bc738eSAdrian Chadd 	 * transition before potentially kicking it to the cleanup
4997f1bc738eSAdrian Chadd 	 * function.
4998f1bc738eSAdrian Chadd 	 */
4999f1bc738eSAdrian Chadd 	if (atid->isfiltered)
5000f1bc738eSAdrian Chadd 		ath_tx_tid_filt_comp_complete(sc, atid);
5001f1bc738eSAdrian Chadd 
5002f1bc738eSAdrian Chadd 	/*
5003eb6f0de0SAdrian Chadd 	 * If a cleanup is in progress, punt to comp_cleanup;
5004eb6f0de0SAdrian Chadd 	 * rather than handling it here. It's thus their
5005eb6f0de0SAdrian Chadd 	 * responsibility to clean up, call the completion
5006eb6f0de0SAdrian Chadd 	 * function in net80211, etc.
5007eb6f0de0SAdrian Chadd 	 */
5008eb6f0de0SAdrian Chadd 	if (atid->cleanup_inprogress) {
5009f1bc738eSAdrian Chadd 		if (atid->isfiltered)
501083bbd5ebSRui Paulo 			DPRINTF(sc, ATH_DEBUG_SW_TX,
5011f1bc738eSAdrian Chadd 			    "%s: isfiltered=1, normal_comp?\n",
5012f1bc738eSAdrian Chadd 			    __func__);
5013375307d4SAdrian Chadd 		ATH_TX_UNLOCK(sc);
5014d4365d16SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: cleanup_unaggr\n",
5015d4365d16SAdrian Chadd 		    __func__);
5016eb6f0de0SAdrian Chadd 		ath_tx_comp_cleanup_unaggr(sc, bf);
5017eb6f0de0SAdrian Chadd 		return;
5018eb6f0de0SAdrian Chadd 	}
5019eb6f0de0SAdrian Chadd 
5020eb6f0de0SAdrian Chadd 	/*
5021f1bc738eSAdrian Chadd 	 * XXX TODO: how does cleanup, BAR and filtered frame handling
5022f1bc738eSAdrian Chadd 	 * overlap?
5023f1bc738eSAdrian Chadd 	 *
5024f1bc738eSAdrian Chadd 	 * If the frame is filtered OR if it's any failure but
5025f1bc738eSAdrian Chadd 	 * the TID is filtered, the frame must be added to the
5026f1bc738eSAdrian Chadd 	 * filtered frame list.
5027f1bc738eSAdrian Chadd 	 *
5028f1bc738eSAdrian Chadd 	 * However - a busy buffer can't be added to the filtered
5029f1bc738eSAdrian Chadd 	 * list as it will end up being recycled without having
5030f1bc738eSAdrian Chadd 	 * been made available for the hardware.
5031f1bc738eSAdrian Chadd 	 */
50320aa5c1bbSAdrian Chadd 	if ((ts.ts_status & HAL_TXERR_FILT) ||
50330aa5c1bbSAdrian Chadd 	    (ts.ts_status != 0 && atid->isfiltered)) {
5034f1bc738eSAdrian Chadd 		int freeframe;
5035f1bc738eSAdrian Chadd 
5036f1bc738eSAdrian Chadd 		if (fail != 0)
503783bbd5ebSRui Paulo 			DPRINTF(sc, ATH_DEBUG_SW_TX,
5038f1bc738eSAdrian Chadd 			    "%s: isfiltered=1, fail=%d\n",
503983bbd5ebSRui Paulo 			    __func__, fail);
5040f1bc738eSAdrian Chadd 		freeframe = ath_tx_tid_filt_comp_single(sc, atid, bf);
5041f1bc738eSAdrian Chadd 		if (freeframe) {
5042f1bc738eSAdrian Chadd 			/* Remove from BAW */
5043f1bc738eSAdrian Chadd 			if (bf->bf_state.bfs_addedbaw)
5044f1bc738eSAdrian Chadd 				drops++;
5045f1bc738eSAdrian Chadd 			if (bf->bf_state.bfs_dobaw) {
5046f1bc738eSAdrian Chadd 				ath_tx_update_baw(sc, an, atid, bf);
5047f1bc738eSAdrian Chadd 				if (!bf->bf_state.bfs_addedbaw)
504883bbd5ebSRui Paulo 					DPRINTF(sc, ATH_DEBUG_SW_TX,
5049f1bc738eSAdrian Chadd 					    "%s: wasn't added: seqno %d\n",
5050f1bc738eSAdrian Chadd 					    __func__, SEQNO(bf->bf_state.bfs_seqno));
5051f1bc738eSAdrian Chadd 			}
5052f1bc738eSAdrian Chadd 			bf->bf_state.bfs_dobaw = 0;
5053f1bc738eSAdrian Chadd 		}
5054f1bc738eSAdrian Chadd 
5055f1bc738eSAdrian Chadd 		/*
5056f1bc738eSAdrian Chadd 		 * If the frame couldn't be filtered, treat it as a drop and
5057f1bc738eSAdrian Chadd 		 * prepare to send a BAR.
5058f1bc738eSAdrian Chadd 		 */
5059f1bc738eSAdrian Chadd 		if (freeframe && drops)
5060f1bc738eSAdrian Chadd 			ath_tx_tid_bar_suspend(sc, atid);
5061f1bc738eSAdrian Chadd 
5062f1bc738eSAdrian Chadd 		/*
5063f1bc738eSAdrian Chadd 		 * Send BAR if required
5064f1bc738eSAdrian Chadd 		 */
5065f1bc738eSAdrian Chadd 		if (ath_tx_tid_bar_tx_ready(sc, atid))
5066f1bc738eSAdrian Chadd 			ath_tx_tid_bar_tx(sc, atid);
5067f1bc738eSAdrian Chadd 
5068375307d4SAdrian Chadd 		ATH_TX_UNLOCK(sc);
5069f1bc738eSAdrian Chadd 		/*
5070f1bc738eSAdrian Chadd 		 * If freeframe is set, then the frame couldn't be
5071f1bc738eSAdrian Chadd 		 * cloned and bf is still valid.  Just complete/free it.
5072f1bc738eSAdrian Chadd 		 */
5073f1bc738eSAdrian Chadd 		if (freeframe)
5074f1bc738eSAdrian Chadd 			ath_tx_default_comp(sc, bf, fail);
5075f1bc738eSAdrian Chadd 
5076f1bc738eSAdrian Chadd 
5077f1bc738eSAdrian Chadd 		return;
5078f1bc738eSAdrian Chadd 	}
5079f1bc738eSAdrian Chadd 	/*
5080eb6f0de0SAdrian Chadd 	 * Don't bother with the retry check if all frames
5081eb6f0de0SAdrian Chadd 	 * are being failed (eg during queue deletion.)
5082eb6f0de0SAdrian Chadd 	 */
5083e9a6408eSAdrian Chadd #if 0
5084eb6f0de0SAdrian Chadd 	if (fail == 0 && ts->ts_status & HAL_TXERR_XRETRY) {
5085e9a6408eSAdrian Chadd #endif
50860aa5c1bbSAdrian Chadd 	if (fail == 0 && ts.ts_status != 0) {
5087375307d4SAdrian Chadd 		ATH_TX_UNLOCK(sc);
5088d4365d16SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: retry_unaggr\n",
5089d4365d16SAdrian Chadd 		    __func__);
5090eb6f0de0SAdrian Chadd 		ath_tx_aggr_retry_unaggr(sc, bf);
5091eb6f0de0SAdrian Chadd 		return;
5092eb6f0de0SAdrian Chadd 	}
5093eb6f0de0SAdrian Chadd 
5094eb6f0de0SAdrian Chadd 	/* Success? Complete */
5095eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: TID=%d, seqno %d\n",
5096eb6f0de0SAdrian Chadd 	    __func__, tid, SEQNO(bf->bf_state.bfs_seqno));
5097eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_dobaw) {
5098eb6f0de0SAdrian Chadd 		ath_tx_update_baw(sc, an, atid, bf);
5099eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_dobaw = 0;
5100eb6f0de0SAdrian Chadd 		if (!bf->bf_state.bfs_addedbaw)
510183bbd5ebSRui Paulo 			DPRINTF(sc, ATH_DEBUG_SW_TX,
5102eb6f0de0SAdrian Chadd 			    "%s: wasn't added: seqno %d\n",
5103eb6f0de0SAdrian Chadd 			    __func__, SEQNO(bf->bf_state.bfs_seqno));
5104eb6f0de0SAdrian Chadd 	}
5105eb6f0de0SAdrian Chadd 
510688b3d483SAdrian Chadd 	/*
5107f1bc738eSAdrian Chadd 	 * If the queue is filtered, re-schedule as required.
5108f1bc738eSAdrian Chadd 	 *
5109f1bc738eSAdrian Chadd 	 * This is required as there may be a subsequent TX descriptor
5110f1bc738eSAdrian Chadd 	 * for this end-node that has CLRDMASK set, so it's quite possible
5111f1bc738eSAdrian Chadd 	 * that a filtered frame will be followed by a non-filtered
5112f1bc738eSAdrian Chadd 	 * (complete or otherwise) frame.
5113f1bc738eSAdrian Chadd 	 *
5114f1bc738eSAdrian Chadd 	 * XXX should we do this before we complete the frame?
5115f1bc738eSAdrian Chadd 	 */
5116f1bc738eSAdrian Chadd 	if (atid->isfiltered)
5117f1bc738eSAdrian Chadd 		ath_tx_tid_filt_comp_complete(sc, atid);
5118f1bc738eSAdrian Chadd 
5119f1bc738eSAdrian Chadd 	/*
512088b3d483SAdrian Chadd 	 * Send BAR if required
512188b3d483SAdrian Chadd 	 */
512288b3d483SAdrian Chadd 	if (ath_tx_tid_bar_tx_ready(sc, atid))
512388b3d483SAdrian Chadd 		ath_tx_tid_bar_tx(sc, atid);
512488b3d483SAdrian Chadd 
5125375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
5126eb6f0de0SAdrian Chadd 
5127eb6f0de0SAdrian Chadd 	ath_tx_default_comp(sc, bf, fail);
5128eb6f0de0SAdrian Chadd 	/* bf is freed at this point */
5129eb6f0de0SAdrian Chadd }
5130eb6f0de0SAdrian Chadd 
5131eb6f0de0SAdrian Chadd void
5132eb6f0de0SAdrian Chadd ath_tx_aggr_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
5133eb6f0de0SAdrian Chadd {
5134eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_aggr)
5135eb6f0de0SAdrian Chadd 		ath_tx_aggr_comp_aggr(sc, bf, fail);
5136eb6f0de0SAdrian Chadd 	else
5137eb6f0de0SAdrian Chadd 		ath_tx_aggr_comp_unaggr(sc, bf, fail);
5138eb6f0de0SAdrian Chadd }
5139eb6f0de0SAdrian Chadd 
5140eb6f0de0SAdrian Chadd /*
5141eb6f0de0SAdrian Chadd  * Schedule some packets from the given node/TID to the hardware.
5142eb6f0de0SAdrian Chadd  *
5143eb6f0de0SAdrian Chadd  * This is the aggregate version.
5144eb6f0de0SAdrian Chadd  */
5145eb6f0de0SAdrian Chadd void
5146eb6f0de0SAdrian Chadd ath_tx_tid_hw_queue_aggr(struct ath_softc *sc, struct ath_node *an,
5147eb6f0de0SAdrian Chadd     struct ath_tid *tid)
5148eb6f0de0SAdrian Chadd {
5149eb6f0de0SAdrian Chadd 	struct ath_buf *bf;
5150eb6f0de0SAdrian Chadd 	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
5151eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
5152eb6f0de0SAdrian Chadd 	ATH_AGGR_STATUS status;
5153eb6f0de0SAdrian Chadd 	ath_bufhead bf_q;
5154eb6f0de0SAdrian Chadd 
5155eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d\n", __func__, tid->tid);
5156375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
5157eb6f0de0SAdrian Chadd 
515822a3aee6SAdrian Chadd 	/*
515922a3aee6SAdrian Chadd 	 * XXX TODO: If we're called for a queue that we're leaking frames to,
516022a3aee6SAdrian Chadd 	 * ensure we only leak one.
516122a3aee6SAdrian Chadd 	 */
516222a3aee6SAdrian Chadd 
5163eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
5164eb6f0de0SAdrian Chadd 
5165eb6f0de0SAdrian Chadd 	if (tid->tid == IEEE80211_NONQOS_TID)
516683bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX,
516783bbd5ebSRui Paulo 		    "%s: called for TID=NONQOS_TID?\n", __func__);
5168eb6f0de0SAdrian Chadd 
5169eb6f0de0SAdrian Chadd 	for (;;) {
5170eb6f0de0SAdrian Chadd 		status = ATH_AGGR_DONE;
5171eb6f0de0SAdrian Chadd 
5172eb6f0de0SAdrian Chadd 		/*
5173eb6f0de0SAdrian Chadd 		 * If the upper layer has paused the TID, don't
5174eb6f0de0SAdrian Chadd 		 * queue any further packets.
5175eb6f0de0SAdrian Chadd 		 *
5176eb6f0de0SAdrian Chadd 		 * This can also occur from the completion task because
5177eb6f0de0SAdrian Chadd 		 * of packet loss; but as its serialised with this code,
5178eb6f0de0SAdrian Chadd 		 * it won't "appear" half way through queuing packets.
5179eb6f0de0SAdrian Chadd 		 */
518022a3aee6SAdrian Chadd 		if (! ath_tx_tid_can_tx_or_sched(sc, tid))
5181eb6f0de0SAdrian Chadd 			break;
5182eb6f0de0SAdrian Chadd 
51833e6cc97fSAdrian Chadd 		bf = ATH_TID_FIRST(tid);
5184eb6f0de0SAdrian Chadd 		if (bf == NULL) {
5185eb6f0de0SAdrian Chadd 			break;
5186eb6f0de0SAdrian Chadd 		}
5187eb6f0de0SAdrian Chadd 
5188eb6f0de0SAdrian Chadd 		/*
5189eb6f0de0SAdrian Chadd 		 * If the packet doesn't fall within the BAW (eg a NULL
5190eb6f0de0SAdrian Chadd 		 * data frame), schedule it directly; continue.
5191eb6f0de0SAdrian Chadd 		 */
5192eb6f0de0SAdrian Chadd 		if (! bf->bf_state.bfs_dobaw) {
5193d4365d16SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
5194d4365d16SAdrian Chadd 			    "%s: non-baw packet\n",
5195eb6f0de0SAdrian Chadd 			    __func__);
51963e6cc97fSAdrian Chadd 			ATH_TID_REMOVE(tid, bf, bf_list);
51972a9f83afSAdrian Chadd 
51982a9f83afSAdrian Chadd 			if (bf->bf_state.bfs_nframes > 1)
519983bbd5ebSRui Paulo 				DPRINTF(sc, ATH_DEBUG_SW_TX,
52002a9f83afSAdrian Chadd 				    "%s: aggr=%d, nframes=%d\n",
52012a9f83afSAdrian Chadd 				    __func__,
52022a9f83afSAdrian Chadd 				    bf->bf_state.bfs_aggr,
52032a9f83afSAdrian Chadd 				    bf->bf_state.bfs_nframes);
52042a9f83afSAdrian Chadd 
52052a9f83afSAdrian Chadd 			/*
52062a9f83afSAdrian Chadd 			 * This shouldn't happen - such frames shouldn't
52072a9f83afSAdrian Chadd 			 * ever have been queued as an aggregate in the
52082a9f83afSAdrian Chadd 			 * first place.  However, make sure the fields
52092a9f83afSAdrian Chadd 			 * are correctly setup just to be totally sure.
52102a9f83afSAdrian Chadd 			 */
5211eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_aggr = 0;
52122a9f83afSAdrian Chadd 			bf->bf_state.bfs_nframes = 1;
52132a9f83afSAdrian Chadd 
52144e81f27cSAdrian Chadd 			/* Update CLRDMASK just before this frame is queued */
52154e81f27cSAdrian Chadd 			ath_tx_update_clrdmask(sc, tid, bf);
52164e81f27cSAdrian Chadd 
5217eb6f0de0SAdrian Chadd 			ath_tx_do_ratelookup(sc, bf);
5218e2e4a2c2SAdrian Chadd 			ath_tx_calc_duration(sc, bf);
5219e2e4a2c2SAdrian Chadd 			ath_tx_calc_protection(sc, bf);
5220eb6f0de0SAdrian Chadd 			ath_tx_set_rtscts(sc, bf);
5221e2e4a2c2SAdrian Chadd 			ath_tx_rate_fill_rcflags(sc, bf);
5222eb6f0de0SAdrian Chadd 			ath_tx_setds(sc, bf);
5223eb6f0de0SAdrian Chadd 			ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
5224eb6f0de0SAdrian Chadd 
5225eb6f0de0SAdrian Chadd 			sc->sc_aggr_stats.aggr_nonbaw_pkt++;
5226eb6f0de0SAdrian Chadd 
5227eb6f0de0SAdrian Chadd 			/* Queue the packet; continue */
5228eb6f0de0SAdrian Chadd 			goto queuepkt;
5229eb6f0de0SAdrian Chadd 		}
5230eb6f0de0SAdrian Chadd 
5231eb6f0de0SAdrian Chadd 		TAILQ_INIT(&bf_q);
5232eb6f0de0SAdrian Chadd 
5233eb6f0de0SAdrian Chadd 		/*
5234eb6f0de0SAdrian Chadd 		 * Do a rate control lookup on the first frame in the
5235eb6f0de0SAdrian Chadd 		 * list. The rate control code needs that to occur
5236eb6f0de0SAdrian Chadd 		 * before it can determine whether to TX.
5237eb6f0de0SAdrian Chadd 		 * It's inaccurate because the rate control code doesn't
5238eb6f0de0SAdrian Chadd 		 * really "do" aggregate lookups, so it only considers
5239eb6f0de0SAdrian Chadd 		 * the size of the first frame.
5240eb6f0de0SAdrian Chadd 		 */
5241eb6f0de0SAdrian Chadd 		ath_tx_do_ratelookup(sc, bf);
5242eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[3].rix = 0;
5243eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[3].tries = 0;
5244e2e4a2c2SAdrian Chadd 
5245e2e4a2c2SAdrian Chadd 		ath_tx_calc_duration(sc, bf);
5246e2e4a2c2SAdrian Chadd 		ath_tx_calc_protection(sc, bf);
5247e2e4a2c2SAdrian Chadd 
5248e2e4a2c2SAdrian Chadd 		ath_tx_set_rtscts(sc, bf);
5249eb6f0de0SAdrian Chadd 		ath_tx_rate_fill_rcflags(sc, bf);
5250eb6f0de0SAdrian Chadd 
5251eb6f0de0SAdrian Chadd 		status = ath_tx_form_aggr(sc, an, tid, &bf_q);
5252eb6f0de0SAdrian Chadd 
5253eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
5254eb6f0de0SAdrian Chadd 		    "%s: ath_tx_form_aggr() status=%d\n", __func__, status);
5255eb6f0de0SAdrian Chadd 
5256eb6f0de0SAdrian Chadd 		/*
5257eb6f0de0SAdrian Chadd 		 * No frames to be picked up - out of BAW
5258eb6f0de0SAdrian Chadd 		 */
5259eb6f0de0SAdrian Chadd 		if (TAILQ_EMPTY(&bf_q))
5260eb6f0de0SAdrian Chadd 			break;
5261eb6f0de0SAdrian Chadd 
5262eb6f0de0SAdrian Chadd 		/*
5263eb6f0de0SAdrian Chadd 		 * This assumes that the descriptor list in the ath_bufhead
5264eb6f0de0SAdrian Chadd 		 * are already linked together via bf_next pointers.
5265eb6f0de0SAdrian Chadd 		 */
5266eb6f0de0SAdrian Chadd 		bf = TAILQ_FIRST(&bf_q);
5267eb6f0de0SAdrian Chadd 
5268e2e4a2c2SAdrian Chadd 		if (status == ATH_AGGR_8K_LIMITED)
5269e2e4a2c2SAdrian Chadd 			sc->sc_aggr_stats.aggr_rts_aggr_limited++;
5270e2e4a2c2SAdrian Chadd 
5271eb6f0de0SAdrian Chadd 		/*
5272eb6f0de0SAdrian Chadd 		 * If it's the only frame send as non-aggregate
5273eb6f0de0SAdrian Chadd 		 * assume that ath_tx_form_aggr() has checked
5274eb6f0de0SAdrian Chadd 		 * whether it's in the BAW and added it appropriately.
5275eb6f0de0SAdrian Chadd 		 */
5276eb6f0de0SAdrian Chadd 		if (bf->bf_state.bfs_nframes == 1) {
5277eb6f0de0SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
5278eb6f0de0SAdrian Chadd 			    "%s: single-frame aggregate\n", __func__);
52794e81f27cSAdrian Chadd 
52804e81f27cSAdrian Chadd 			/* Update CLRDMASK just before this frame is queued */
52814e81f27cSAdrian Chadd 			ath_tx_update_clrdmask(sc, tid, bf);
52824e81f27cSAdrian Chadd 
5283eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_aggr = 0;
528421840808SAdrian Chadd 			bf->bf_state.bfs_ndelim = 0;
5285eb6f0de0SAdrian Chadd 			ath_tx_setds(sc, bf);
5286eb6f0de0SAdrian Chadd 			ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
5287eb6f0de0SAdrian Chadd 			if (status == ATH_AGGR_BAW_CLOSED)
5288eb6f0de0SAdrian Chadd 				sc->sc_aggr_stats.aggr_baw_closed_single_pkt++;
5289eb6f0de0SAdrian Chadd 			else
5290eb6f0de0SAdrian Chadd 				sc->sc_aggr_stats.aggr_single_pkt++;
5291eb6f0de0SAdrian Chadd 		} else {
5292eb6f0de0SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
5293d4365d16SAdrian Chadd 			    "%s: multi-frame aggregate: %d frames, "
5294d4365d16SAdrian Chadd 			    "length %d\n",
5295eb6f0de0SAdrian Chadd 			     __func__, bf->bf_state.bfs_nframes,
5296eb6f0de0SAdrian Chadd 			    bf->bf_state.bfs_al);
5297eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_aggr = 1;
5298eb6f0de0SAdrian Chadd 			sc->sc_aggr_stats.aggr_pkts[bf->bf_state.bfs_nframes]++;
5299eb6f0de0SAdrian Chadd 			sc->sc_aggr_stats.aggr_aggr_pkt++;
5300eb6f0de0SAdrian Chadd 
53014e81f27cSAdrian Chadd 			/* Update CLRDMASK just before this frame is queued */
53024e81f27cSAdrian Chadd 			ath_tx_update_clrdmask(sc, tid, bf);
53034e81f27cSAdrian Chadd 
5304eb6f0de0SAdrian Chadd 			/*
5305e2e4a2c2SAdrian Chadd 			 * Calculate the duration/protection as required.
5306e2e4a2c2SAdrian Chadd 			 */
5307e2e4a2c2SAdrian Chadd 			ath_tx_calc_duration(sc, bf);
5308e2e4a2c2SAdrian Chadd 			ath_tx_calc_protection(sc, bf);
5309e2e4a2c2SAdrian Chadd 
5310e2e4a2c2SAdrian Chadd 			/*
5311eb6f0de0SAdrian Chadd 			 * Update the rate and rtscts information based on the
5312eb6f0de0SAdrian Chadd 			 * rate decision made by the rate control code;
5313eb6f0de0SAdrian Chadd 			 * the first frame in the aggregate needs it.
5314eb6f0de0SAdrian Chadd 			 */
5315eb6f0de0SAdrian Chadd 			ath_tx_set_rtscts(sc, bf);
5316eb6f0de0SAdrian Chadd 
5317eb6f0de0SAdrian Chadd 			/*
5318eb6f0de0SAdrian Chadd 			 * Setup the relevant descriptor fields
5319eb6f0de0SAdrian Chadd 			 * for aggregation. The first descriptor
5320eb6f0de0SAdrian Chadd 			 * already points to the rest in the chain.
5321eb6f0de0SAdrian Chadd 			 */
5322eb6f0de0SAdrian Chadd 			ath_tx_setds_11n(sc, bf);
5323eb6f0de0SAdrian Chadd 
5324eb6f0de0SAdrian Chadd 		}
5325eb6f0de0SAdrian Chadd 	queuepkt:
5326eb6f0de0SAdrian Chadd 		/* Set completion handler, multi-frame aggregate or not */
5327eb6f0de0SAdrian Chadd 		bf->bf_comp = ath_tx_aggr_comp;
5328eb6f0de0SAdrian Chadd 
5329eb6f0de0SAdrian Chadd 		if (bf->bf_state.bfs_tid == IEEE80211_NONQOS_TID)
533083bbd5ebSRui Paulo 			DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: TID=16?\n", __func__);
5331eb6f0de0SAdrian Chadd 
533222a3aee6SAdrian Chadd 		/*
533322a3aee6SAdrian Chadd 		 * Update leak count and frame config if were leaking frames.
533422a3aee6SAdrian Chadd 		 *
533522a3aee6SAdrian Chadd 		 * XXX TODO: it should update all frames in an aggregate
533622a3aee6SAdrian Chadd 		 * correctly!
533722a3aee6SAdrian Chadd 		 */
533822a3aee6SAdrian Chadd 		ath_tx_leak_count_update(sc, tid, bf);
533922a3aee6SAdrian Chadd 
5340eb6f0de0SAdrian Chadd 		/* Punt to txq */
5341eb6f0de0SAdrian Chadd 		ath_tx_handoff(sc, txq, bf);
5342eb6f0de0SAdrian Chadd 
5343eb6f0de0SAdrian Chadd 		/* Track outstanding buffer count to hardware */
5344eb6f0de0SAdrian Chadd 		/* aggregates are "one" buffer */
5345eb6f0de0SAdrian Chadd 		tid->hwq_depth++;
5346eb6f0de0SAdrian Chadd 
5347eb6f0de0SAdrian Chadd 		/*
5348eb6f0de0SAdrian Chadd 		 * Break out if ath_tx_form_aggr() indicated
5349eb6f0de0SAdrian Chadd 		 * there can't be any further progress (eg BAW is full.)
5350eb6f0de0SAdrian Chadd 		 * Checking for an empty txq is done above.
5351eb6f0de0SAdrian Chadd 		 *
5352eb6f0de0SAdrian Chadd 		 * XXX locking on txq here?
5353eb6f0de0SAdrian Chadd 		 */
535472910f03SAdrian Chadd 		/* XXX TXQ locking */
535572910f03SAdrian Chadd 		if (txq->axq_aggr_depth >= sc->sc_hwq_limit_aggr ||
535622a3aee6SAdrian Chadd 		    (status == ATH_AGGR_BAW_CLOSED ||
535722a3aee6SAdrian Chadd 		     status == ATH_AGGR_LEAK_CLOSED))
5358eb6f0de0SAdrian Chadd 			break;
5359eb6f0de0SAdrian Chadd 	}
5360eb6f0de0SAdrian Chadd }
5361eb6f0de0SAdrian Chadd 
5362eb6f0de0SAdrian Chadd /*
5363eb6f0de0SAdrian Chadd  * Schedule some packets from the given node/TID to the hardware.
536472910f03SAdrian Chadd  *
536572910f03SAdrian Chadd  * XXX TODO: this routine doesn't enforce the maximum TXQ depth.
536672910f03SAdrian Chadd  * It just dumps frames into the TXQ.  We should limit how deep
536772910f03SAdrian Chadd  * the transmit queue can grow for frames dispatched to the given
536872910f03SAdrian Chadd  * TXQ.
536972910f03SAdrian Chadd  *
537072910f03SAdrian Chadd  * To avoid locking issues, either we need to own the TXQ lock
537172910f03SAdrian Chadd  * at this point, or we need to pass in the maximum frame count
537272910f03SAdrian Chadd  * from the caller.
5373eb6f0de0SAdrian Chadd  */
5374eb6f0de0SAdrian Chadd void
5375eb6f0de0SAdrian Chadd ath_tx_tid_hw_queue_norm(struct ath_softc *sc, struct ath_node *an,
5376eb6f0de0SAdrian Chadd     struct ath_tid *tid)
5377eb6f0de0SAdrian Chadd {
5378eb6f0de0SAdrian Chadd 	struct ath_buf *bf;
5379eb6f0de0SAdrian Chadd 	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
5380eb6f0de0SAdrian Chadd 
5381eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: node %p: TID %d: called\n",
5382eb6f0de0SAdrian Chadd 	    __func__, an, tid->tid);
5383eb6f0de0SAdrian Chadd 
5384375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
5385eb6f0de0SAdrian Chadd 
5386eb6f0de0SAdrian Chadd 	/* Check - is AMPDU pending or running? then print out something */
5387eb6f0de0SAdrian Chadd 	if (ath_tx_ampdu_pending(sc, an, tid->tid))
538883bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d, ampdu pending?\n",
5389eb6f0de0SAdrian Chadd 		    __func__, tid->tid);
5390eb6f0de0SAdrian Chadd 	if (ath_tx_ampdu_running(sc, an, tid->tid))
539183bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d, ampdu running?\n",
5392eb6f0de0SAdrian Chadd 		    __func__, tid->tid);
5393eb6f0de0SAdrian Chadd 
5394eb6f0de0SAdrian Chadd 	for (;;) {
5395eb6f0de0SAdrian Chadd 
5396eb6f0de0SAdrian Chadd 		/*
5397eb6f0de0SAdrian Chadd 		 * If the upper layers have paused the TID, don't
5398eb6f0de0SAdrian Chadd 		 * queue any further packets.
539922a3aee6SAdrian Chadd 		 *
540022a3aee6SAdrian Chadd 		 * XXX if we are leaking frames, make sure we decrement
540122a3aee6SAdrian Chadd 		 * that counter _and_ we continue here.
5402eb6f0de0SAdrian Chadd 		 */
540322a3aee6SAdrian Chadd 		if (! ath_tx_tid_can_tx_or_sched(sc, tid))
5404eb6f0de0SAdrian Chadd 			break;
5405eb6f0de0SAdrian Chadd 
54063e6cc97fSAdrian Chadd 		bf = ATH_TID_FIRST(tid);
5407eb6f0de0SAdrian Chadd 		if (bf == NULL) {
5408eb6f0de0SAdrian Chadd 			break;
5409eb6f0de0SAdrian Chadd 		}
5410eb6f0de0SAdrian Chadd 
54113e6cc97fSAdrian Chadd 		ATH_TID_REMOVE(tid, bf, bf_list);
5412eb6f0de0SAdrian Chadd 
5413eb6f0de0SAdrian Chadd 		/* Sanity check! */
5414eb6f0de0SAdrian Chadd 		if (tid->tid != bf->bf_state.bfs_tid) {
541583bbd5ebSRui Paulo 			DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bfs_tid %d !="
541683bbd5ebSRui Paulo 			    " tid %d\n", __func__, bf->bf_state.bfs_tid,
541783bbd5ebSRui Paulo 			    tid->tid);
5418eb6f0de0SAdrian Chadd 		}
5419eb6f0de0SAdrian Chadd 		/* Normal completion handler */
5420eb6f0de0SAdrian Chadd 		bf->bf_comp = ath_tx_normal_comp;
5421eb6f0de0SAdrian Chadd 
54220c54de88SAdrian Chadd 		/*
54230c54de88SAdrian Chadd 		 * Override this for now, until the non-aggregate
54240c54de88SAdrian Chadd 		 * completion handler correctly handles software retransmits.
54250c54de88SAdrian Chadd 		 */
54260c54de88SAdrian Chadd 		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
54270c54de88SAdrian Chadd 
54284e81f27cSAdrian Chadd 		/* Update CLRDMASK just before this frame is queued */
54294e81f27cSAdrian Chadd 		ath_tx_update_clrdmask(sc, tid, bf);
54304e81f27cSAdrian Chadd 
5431eb6f0de0SAdrian Chadd 		/* Program descriptors + rate control */
5432eb6f0de0SAdrian Chadd 		ath_tx_do_ratelookup(sc, bf);
5433e2e4a2c2SAdrian Chadd 		ath_tx_calc_duration(sc, bf);
5434e2e4a2c2SAdrian Chadd 		ath_tx_calc_protection(sc, bf);
5435eb6f0de0SAdrian Chadd 		ath_tx_set_rtscts(sc, bf);
5436e2e4a2c2SAdrian Chadd 		ath_tx_rate_fill_rcflags(sc, bf);
5437eb6f0de0SAdrian Chadd 		ath_tx_setds(sc, bf);
5438eb6f0de0SAdrian Chadd 
543922a3aee6SAdrian Chadd 		/*
544022a3aee6SAdrian Chadd 		 * Update the current leak count if
544122a3aee6SAdrian Chadd 		 * we're leaking frames; and set the
544222a3aee6SAdrian Chadd 		 * MORE flag as appropriate.
544322a3aee6SAdrian Chadd 		 */
544422a3aee6SAdrian Chadd 		ath_tx_leak_count_update(sc, tid, bf);
544522a3aee6SAdrian Chadd 
5446eb6f0de0SAdrian Chadd 		/* Track outstanding buffer count to hardware */
5447eb6f0de0SAdrian Chadd 		/* aggregates are "one" buffer */
5448eb6f0de0SAdrian Chadd 		tid->hwq_depth++;
5449eb6f0de0SAdrian Chadd 
5450eb6f0de0SAdrian Chadd 		/* Punt to hardware or software txq */
5451eb6f0de0SAdrian Chadd 		ath_tx_handoff(sc, txq, bf);
5452eb6f0de0SAdrian Chadd 	}
5453eb6f0de0SAdrian Chadd }
5454eb6f0de0SAdrian Chadd 
5455eb6f0de0SAdrian Chadd /*
5456eb6f0de0SAdrian Chadd  * Schedule some packets to the given hardware queue.
5457eb6f0de0SAdrian Chadd  *
5458eb6f0de0SAdrian Chadd  * This function walks the list of TIDs (ie, ath_node TIDs
5459eb6f0de0SAdrian Chadd  * with queued traffic) and attempts to schedule traffic
5460eb6f0de0SAdrian Chadd  * from them.
5461eb6f0de0SAdrian Chadd  *
5462eb6f0de0SAdrian Chadd  * TID scheduling is implemented as a FIFO, with TIDs being
5463eb6f0de0SAdrian Chadd  * added to the end of the queue after some frames have been
5464eb6f0de0SAdrian Chadd  * scheduled.
5465eb6f0de0SAdrian Chadd  */
5466eb6f0de0SAdrian Chadd void
5467eb6f0de0SAdrian Chadd ath_txq_sched(struct ath_softc *sc, struct ath_txq *txq)
5468eb6f0de0SAdrian Chadd {
5469eb6f0de0SAdrian Chadd 	struct ath_tid *tid, *next, *last;
5470eb6f0de0SAdrian Chadd 
5471375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
5472eb6f0de0SAdrian Chadd 
5473eb6f0de0SAdrian Chadd 	/*
5474eb6f0de0SAdrian Chadd 	 * Don't schedule if the hardware queue is busy.
5475eb6f0de0SAdrian Chadd 	 * This (hopefully) gives some more time to aggregate
5476eb6f0de0SAdrian Chadd 	 * some packets in the aggregation queue.
547772910f03SAdrian Chadd 	 *
547872910f03SAdrian Chadd 	 * XXX It doesn't stop a parallel sender from sneaking
547972910f03SAdrian Chadd 	 * in transmitting a frame!
5480eb6f0de0SAdrian Chadd 	 */
548172910f03SAdrian Chadd 	/* XXX TXQ locking */
548272910f03SAdrian Chadd 	if (txq->axq_aggr_depth + txq->fifo.axq_depth >= sc->sc_hwq_limit_aggr) {
548372910f03SAdrian Chadd 		sc->sc_aggr_stats.aggr_sched_nopkt++;
548472910f03SAdrian Chadd 		return;
548572910f03SAdrian Chadd 	}
548672910f03SAdrian Chadd 	if (txq->axq_depth >= sc->sc_hwq_limit_nonaggr) {
5487eb6f0de0SAdrian Chadd 		sc->sc_aggr_stats.aggr_sched_nopkt++;
5488eb6f0de0SAdrian Chadd 		return;
5489eb6f0de0SAdrian Chadd 	}
5490eb6f0de0SAdrian Chadd 
5491eb6f0de0SAdrian Chadd 	last = TAILQ_LAST(&txq->axq_tidq, axq_t_s);
5492eb6f0de0SAdrian Chadd 
5493eb6f0de0SAdrian Chadd 	TAILQ_FOREACH_SAFE(tid, &txq->axq_tidq, axq_qelem, next) {
5494eb6f0de0SAdrian Chadd 		/*
5495eb6f0de0SAdrian Chadd 		 * Suspend paused queues here; they'll be resumed
5496eb6f0de0SAdrian Chadd 		 * once the addba completes or times out.
5497eb6f0de0SAdrian Chadd 		 */
5498eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d, paused=%d\n",
5499eb6f0de0SAdrian Chadd 		    __func__, tid->tid, tid->paused);
5500eb6f0de0SAdrian Chadd 		ath_tx_tid_unsched(sc, tid);
550122a3aee6SAdrian Chadd 		/*
550222a3aee6SAdrian Chadd 		 * This node may be in power-save and we're leaking
550322a3aee6SAdrian Chadd 		 * a frame; be careful.
550422a3aee6SAdrian Chadd 		 */
550522a3aee6SAdrian Chadd 		if (! ath_tx_tid_can_tx_or_sched(sc, tid)) {
5506eb6f0de0SAdrian Chadd 			continue;
5507eb6f0de0SAdrian Chadd 		}
5508eb6f0de0SAdrian Chadd 		if (ath_tx_ampdu_running(sc, tid->an, tid->tid))
5509eb6f0de0SAdrian Chadd 			ath_tx_tid_hw_queue_aggr(sc, tid->an, tid);
5510eb6f0de0SAdrian Chadd 		else
5511eb6f0de0SAdrian Chadd 			ath_tx_tid_hw_queue_norm(sc, tid->an, tid);
5512eb6f0de0SAdrian Chadd 
5513eb6f0de0SAdrian Chadd 		/* Not empty? Re-schedule */
5514eb6f0de0SAdrian Chadd 		if (tid->axq_depth != 0)
5515eb6f0de0SAdrian Chadd 			ath_tx_tid_sched(sc, tid);
5516eb6f0de0SAdrian Chadd 
5517b45a991eSAdrian Chadd 		/*
5518b45a991eSAdrian Chadd 		 * Give the software queue time to aggregate more
5519b45a991eSAdrian Chadd 		 * packets.  If we aren't running aggregation then
5520b45a991eSAdrian Chadd 		 * we should still limit the hardware queue depth.
5521b45a991eSAdrian Chadd 		 */
552272910f03SAdrian Chadd 		/* XXX TXQ locking */
552372910f03SAdrian Chadd 		if (txq->axq_aggr_depth + txq->fifo.axq_depth >= sc->sc_hwq_limit_aggr) {
552472910f03SAdrian Chadd 			break;
552572910f03SAdrian Chadd 		}
552672910f03SAdrian Chadd 		if (txq->axq_depth >= sc->sc_hwq_limit_nonaggr) {
5527eb6f0de0SAdrian Chadd 			break;
5528eb6f0de0SAdrian Chadd 		}
5529eb6f0de0SAdrian Chadd 
5530eb6f0de0SAdrian Chadd 		/*
5531eb6f0de0SAdrian Chadd 		 * If this was the last entry on the original list, stop.
5532eb6f0de0SAdrian Chadd 		 * Otherwise nodes that have been rescheduled onto the end
5533eb6f0de0SAdrian Chadd 		 * of the TID FIFO list will just keep being rescheduled.
553422a3aee6SAdrian Chadd 		 *
553522a3aee6SAdrian Chadd 		 * XXX What should we do about nodes that were paused
553622a3aee6SAdrian Chadd 		 * but are pending a leaking frame in response to a ps-poll?
553722a3aee6SAdrian Chadd 		 * They'll be put at the front of the list; so they'll
553822a3aee6SAdrian Chadd 		 * prematurely trigger this condition! Ew.
5539eb6f0de0SAdrian Chadd 		 */
5540eb6f0de0SAdrian Chadd 		if (tid == last)
5541eb6f0de0SAdrian Chadd 			break;
5542eb6f0de0SAdrian Chadd 	}
5543eb6f0de0SAdrian Chadd }
5544eb6f0de0SAdrian Chadd 
5545eb6f0de0SAdrian Chadd /*
5546eb6f0de0SAdrian Chadd  * TX addba handling
5547eb6f0de0SAdrian Chadd  */
5548eb6f0de0SAdrian Chadd 
5549eb6f0de0SAdrian Chadd /*
5550eb6f0de0SAdrian Chadd  * Return net80211 TID struct pointer, or NULL for none
5551eb6f0de0SAdrian Chadd  */
5552eb6f0de0SAdrian Chadd struct ieee80211_tx_ampdu *
5553eb6f0de0SAdrian Chadd ath_tx_get_tx_tid(struct ath_node *an, int tid)
5554eb6f0de0SAdrian Chadd {
5555eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = &an->an_node;
5556eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
5557eb6f0de0SAdrian Chadd 
5558eb6f0de0SAdrian Chadd 	if (tid == IEEE80211_NONQOS_TID)
5559eb6f0de0SAdrian Chadd 		return NULL;
5560eb6f0de0SAdrian Chadd 
55612aa563dfSAdrian Chadd 	tap = &ni->ni_tx_ampdu[tid];
5562eb6f0de0SAdrian Chadd 	return tap;
5563eb6f0de0SAdrian Chadd }
5564eb6f0de0SAdrian Chadd 
5565eb6f0de0SAdrian Chadd /*
5566eb6f0de0SAdrian Chadd  * Is AMPDU-TX running?
5567eb6f0de0SAdrian Chadd  */
5568eb6f0de0SAdrian Chadd static int
5569eb6f0de0SAdrian Chadd ath_tx_ampdu_running(struct ath_softc *sc, struct ath_node *an, int tid)
5570eb6f0de0SAdrian Chadd {
5571eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
5572eb6f0de0SAdrian Chadd 
5573eb6f0de0SAdrian Chadd 	if (tid == IEEE80211_NONQOS_TID)
5574eb6f0de0SAdrian Chadd 		return 0;
5575eb6f0de0SAdrian Chadd 
5576eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
5577eb6f0de0SAdrian Chadd 	if (tap == NULL)
5578eb6f0de0SAdrian Chadd 		return 0;	/* Not valid; default to not running */
5579eb6f0de0SAdrian Chadd 
5580eb6f0de0SAdrian Chadd 	return !! (tap->txa_flags & IEEE80211_AGGR_RUNNING);
5581eb6f0de0SAdrian Chadd }
5582eb6f0de0SAdrian Chadd 
5583eb6f0de0SAdrian Chadd /*
5584eb6f0de0SAdrian Chadd  * Is AMPDU-TX negotiation pending?
5585eb6f0de0SAdrian Chadd  */
5586eb6f0de0SAdrian Chadd static int
5587eb6f0de0SAdrian Chadd ath_tx_ampdu_pending(struct ath_softc *sc, struct ath_node *an, int tid)
5588eb6f0de0SAdrian Chadd {
5589eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
5590eb6f0de0SAdrian Chadd 
5591eb6f0de0SAdrian Chadd 	if (tid == IEEE80211_NONQOS_TID)
5592eb6f0de0SAdrian Chadd 		return 0;
5593eb6f0de0SAdrian Chadd 
5594eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
5595eb6f0de0SAdrian Chadd 	if (tap == NULL)
5596eb6f0de0SAdrian Chadd 		return 0;	/* Not valid; default to not pending */
5597eb6f0de0SAdrian Chadd 
5598eb6f0de0SAdrian Chadd 	return !! (tap->txa_flags & IEEE80211_AGGR_XCHGPEND);
5599eb6f0de0SAdrian Chadd }
5600eb6f0de0SAdrian Chadd 
5601eb6f0de0SAdrian Chadd /*
5602eb6f0de0SAdrian Chadd  * Is AMPDU-TX pending for the given TID?
5603eb6f0de0SAdrian Chadd  */
5604eb6f0de0SAdrian Chadd 
5605eb6f0de0SAdrian Chadd 
5606eb6f0de0SAdrian Chadd /*
5607eb6f0de0SAdrian Chadd  * Method to handle sending an ADDBA request.
5608eb6f0de0SAdrian Chadd  *
5609eb6f0de0SAdrian Chadd  * We tap this so the relevant flags can be set to pause the TID
5610eb6f0de0SAdrian Chadd  * whilst waiting for the response.
5611eb6f0de0SAdrian Chadd  *
5612eb6f0de0SAdrian Chadd  * XXX there's no timeout handler we can override?
5613eb6f0de0SAdrian Chadd  */
5614eb6f0de0SAdrian Chadd int
5615eb6f0de0SAdrian Chadd ath_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
5616eb6f0de0SAdrian Chadd     int dialogtoken, int baparamset, int batimeout)
5617eb6f0de0SAdrian Chadd {
5618eb6f0de0SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
56192aa563dfSAdrian Chadd 	int tid = tap->txa_tid;
5620eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
5621eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
5622eb6f0de0SAdrian Chadd 
5623eb6f0de0SAdrian Chadd 	/*
5624eb6f0de0SAdrian Chadd 	 * XXX danger Will Robinson!
5625eb6f0de0SAdrian Chadd 	 *
5626eb6f0de0SAdrian Chadd 	 * Although the taskqueue may be running and scheduling some more
5627eb6f0de0SAdrian Chadd 	 * packets, these should all be _before_ the addba sequence number.
5628eb6f0de0SAdrian Chadd 	 * However, net80211 will keep self-assigning sequence numbers
5629eb6f0de0SAdrian Chadd 	 * until addba has been negotiated.
5630eb6f0de0SAdrian Chadd 	 *
5631eb6f0de0SAdrian Chadd 	 * In the past, these packets would be "paused" (which still works
5632eb6f0de0SAdrian Chadd 	 * fine, as they're being scheduled to the driver in the same
5633eb6f0de0SAdrian Chadd 	 * serialised method which is calling the addba request routine)
5634eb6f0de0SAdrian Chadd 	 * and when the aggregation session begins, they'll be dequeued
5635eb6f0de0SAdrian Chadd 	 * as aggregate packets and added to the BAW. However, now there's
5636eb6f0de0SAdrian Chadd 	 * a "bf->bf_state.bfs_dobaw" flag, and this isn't set for these
5637eb6f0de0SAdrian Chadd 	 * packets. Thus they never get included in the BAW tracking and
5638eb6f0de0SAdrian Chadd 	 * this can cause the initial burst of packets after the addba
5639eb6f0de0SAdrian Chadd 	 * negotiation to "hang", as they quickly fall outside the BAW.
5640eb6f0de0SAdrian Chadd 	 *
5641eb6f0de0SAdrian Chadd 	 * The "eventual" solution should be to tag these packets with
5642eb6f0de0SAdrian Chadd 	 * dobaw. Although net80211 has given us a sequence number,
5643eb6f0de0SAdrian Chadd 	 * it'll be "after" the left edge of the BAW and thus it'll
5644eb6f0de0SAdrian Chadd 	 * fall within it.
5645eb6f0de0SAdrian Chadd 	 */
5646375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
5647d3a6425bSAdrian Chadd 	/*
5648d3a6425bSAdrian Chadd 	 * This is a bit annoying.  Until net80211 HT code inherits some
5649d3a6425bSAdrian Chadd 	 * (any) locking, we may have this called in parallel BUT only
5650d3a6425bSAdrian Chadd 	 * one response/timeout will be called.  Grr.
5651d3a6425bSAdrian Chadd 	 */
5652d3a6425bSAdrian Chadd 	if (atid->addba_tx_pending == 0) {
5653eb6f0de0SAdrian Chadd 		ath_tx_tid_pause(sc, atid);
5654d3a6425bSAdrian Chadd 		atid->addba_tx_pending = 1;
5655d3a6425bSAdrian Chadd 	}
5656375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
5657eb6f0de0SAdrian Chadd 
5658eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
56599b48fb4bSAdrian Chadd 	    "%s: %6D: called; dialogtoken=%d, baparamset=%d, batimeout=%d\n",
56609b48fb4bSAdrian Chadd 	    __func__,
56619b48fb4bSAdrian Chadd 	    ni->ni_macaddr,
56629b48fb4bSAdrian Chadd 	    ":",
56639b48fb4bSAdrian Chadd 	    dialogtoken, baparamset, batimeout);
5664eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5665eb6f0de0SAdrian Chadd 	    "%s: txa_start=%d, ni_txseqs=%d\n",
5666eb6f0de0SAdrian Chadd 	    __func__, tap->txa_start, ni->ni_txseqs[tid]);
5667eb6f0de0SAdrian Chadd 
5668eb6f0de0SAdrian Chadd 	return sc->sc_addba_request(ni, tap, dialogtoken, baparamset,
5669eb6f0de0SAdrian Chadd 	    batimeout);
5670eb6f0de0SAdrian Chadd }
5671eb6f0de0SAdrian Chadd 
5672eb6f0de0SAdrian Chadd /*
5673eb6f0de0SAdrian Chadd  * Handle an ADDBA response.
5674eb6f0de0SAdrian Chadd  *
5675eb6f0de0SAdrian Chadd  * We unpause the queue so TX'ing can resume.
5676eb6f0de0SAdrian Chadd  *
5677eb6f0de0SAdrian Chadd  * Any packets TX'ed from this point should be "aggregate" (whether
5678eb6f0de0SAdrian Chadd  * aggregate or not) so the BAW is updated.
5679eb6f0de0SAdrian Chadd  *
5680eb6f0de0SAdrian Chadd  * Note! net80211 keeps self-assigning sequence numbers until
5681eb6f0de0SAdrian Chadd  * ampdu is negotiated. This means the initially-negotiated BAW left
5682eb6f0de0SAdrian Chadd  * edge won't match the ni->ni_txseq.
5683eb6f0de0SAdrian Chadd  *
5684eb6f0de0SAdrian Chadd  * So, being very dirty, the BAW left edge is "slid" here to match
5685eb6f0de0SAdrian Chadd  * ni->ni_txseq.
5686eb6f0de0SAdrian Chadd  *
5687eb6f0de0SAdrian Chadd  * What likely SHOULD happen is that all packets subsequent to the
5688eb6f0de0SAdrian Chadd  * addba request should be tagged as aggregate and queued as non-aggregate
5689eb6f0de0SAdrian Chadd  * frames; thus updating the BAW. For now though, I'll just slide the
5690eb6f0de0SAdrian Chadd  * window.
5691eb6f0de0SAdrian Chadd  */
5692eb6f0de0SAdrian Chadd int
5693eb6f0de0SAdrian Chadd ath_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
5694eb6f0de0SAdrian Chadd     int status, int code, int batimeout)
5695eb6f0de0SAdrian Chadd {
5696eb6f0de0SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
56972aa563dfSAdrian Chadd 	int tid = tap->txa_tid;
5698eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
5699eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
5700eb6f0de0SAdrian Chadd 	int r;
5701eb6f0de0SAdrian Chadd 
5702eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
57039b48fb4bSAdrian Chadd 	    "%s: %6D: called; status=%d, code=%d, batimeout=%d\n", __func__,
57049b48fb4bSAdrian Chadd 	    ni->ni_macaddr,
57059b48fb4bSAdrian Chadd 	    ":",
5706eb6f0de0SAdrian Chadd 	    status, code, batimeout);
5707eb6f0de0SAdrian Chadd 
5708eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5709eb6f0de0SAdrian Chadd 	    "%s: txa_start=%d, ni_txseqs=%d\n",
5710eb6f0de0SAdrian Chadd 	    __func__, tap->txa_start, ni->ni_txseqs[tid]);
5711eb6f0de0SAdrian Chadd 
5712eb6f0de0SAdrian Chadd 	/*
5713eb6f0de0SAdrian Chadd 	 * Call this first, so the interface flags get updated
5714eb6f0de0SAdrian Chadd 	 * before the TID is unpaused. Otherwise a race condition
5715eb6f0de0SAdrian Chadd 	 * exists where the unpaused TID still doesn't yet have
5716eb6f0de0SAdrian Chadd 	 * IEEE80211_AGGR_RUNNING set.
5717eb6f0de0SAdrian Chadd 	 */
5718eb6f0de0SAdrian Chadd 	r = sc->sc_addba_response(ni, tap, status, code, batimeout);
5719eb6f0de0SAdrian Chadd 
5720375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
5721d3a6425bSAdrian Chadd 	atid->addba_tx_pending = 0;
5722eb6f0de0SAdrian Chadd 	/*
5723eb6f0de0SAdrian Chadd 	 * XXX dirty!
5724eb6f0de0SAdrian Chadd 	 * Slide the BAW left edge to wherever net80211 left it for us.
5725eb6f0de0SAdrian Chadd 	 * Read above for more information.
5726eb6f0de0SAdrian Chadd 	 */
5727eb6f0de0SAdrian Chadd 	tap->txa_start = ni->ni_txseqs[tid];
5728eb6f0de0SAdrian Chadd 	ath_tx_tid_resume(sc, atid);
5729375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
5730eb6f0de0SAdrian Chadd 	return r;
5731eb6f0de0SAdrian Chadd }
5732eb6f0de0SAdrian Chadd 
5733eb6f0de0SAdrian Chadd 
5734eb6f0de0SAdrian Chadd /*
5735eb6f0de0SAdrian Chadd  * Stop ADDBA on a queue.
57368405fe86SAdrian Chadd  *
57378405fe86SAdrian Chadd  * This can be called whilst BAR TX is currently active on the queue,
57388405fe86SAdrian Chadd  * so make sure this is unblocked before continuing.
5739eb6f0de0SAdrian Chadd  */
5740eb6f0de0SAdrian Chadd void
5741eb6f0de0SAdrian Chadd ath_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
5742eb6f0de0SAdrian Chadd {
5743eb6f0de0SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
57442aa563dfSAdrian Chadd 	int tid = tap->txa_tid;
5745eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
5746eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
574722780332SAdrian Chadd 	ath_bufhead bf_cq;
574822780332SAdrian Chadd 	struct ath_buf *bf;
5749eb6f0de0SAdrian Chadd 
57509b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: %6D: called\n",
57519b48fb4bSAdrian Chadd 	    __func__,
57529b48fb4bSAdrian Chadd 	    ni->ni_macaddr,
57539b48fb4bSAdrian Chadd 	    ":");
5754eb6f0de0SAdrian Chadd 
57558405fe86SAdrian Chadd 	/*
57568405fe86SAdrian Chadd 	 * Pause TID traffic early, so there aren't any races
57578405fe86SAdrian Chadd 	 * Unblock the pending BAR held traffic, if it's currently paused.
57588405fe86SAdrian Chadd 	 */
5759375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
5760eb6f0de0SAdrian Chadd 	ath_tx_tid_pause(sc, atid);
57618405fe86SAdrian Chadd 	if (atid->bar_wait) {
57628405fe86SAdrian Chadd 		/*
57638405fe86SAdrian Chadd 		 * bar_unsuspend() expects bar_tx == 1, as it should be
57648405fe86SAdrian Chadd 		 * called from the TX completion path.  This quietens
57658405fe86SAdrian Chadd 		 * the warning.  It's cleared for us anyway.
57668405fe86SAdrian Chadd 		 */
57678405fe86SAdrian Chadd 		atid->bar_tx = 1;
57688405fe86SAdrian Chadd 		ath_tx_tid_bar_unsuspend(sc, atid);
57698405fe86SAdrian Chadd 	}
5770375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
5771eb6f0de0SAdrian Chadd 
5772eb6f0de0SAdrian Chadd 	/* There's no need to hold the TXQ lock here */
5773eb6f0de0SAdrian Chadd 	sc->sc_addba_stop(ni, tap);
5774eb6f0de0SAdrian Chadd 
5775eb6f0de0SAdrian Chadd 	/*
57764dfd4507SAdrian Chadd 	 * ath_tx_tid_cleanup will resume the TID if possible, otherwise
5777eb6f0de0SAdrian Chadd 	 * it'll set the cleanup flag, and it'll be unpaused once
5778eb6f0de0SAdrian Chadd 	 * things have been cleaned up.
5779eb6f0de0SAdrian Chadd 	 */
578022780332SAdrian Chadd 	TAILQ_INIT(&bf_cq);
578122780332SAdrian Chadd 	ATH_TX_LOCK(sc);
578222780332SAdrian Chadd 	ath_tx_tid_cleanup(sc, an, tid, &bf_cq);
57835da3fc10SAdrian Chadd 	/*
57845da3fc10SAdrian Chadd 	 * Unpause the TID if no cleanup is required.
57855da3fc10SAdrian Chadd 	 */
57865da3fc10SAdrian Chadd 	if (! atid->cleanup_inprogress)
57875da3fc10SAdrian Chadd 		ath_tx_tid_resume(sc, atid);
578822780332SAdrian Chadd 	ATH_TX_UNLOCK(sc);
578922780332SAdrian Chadd 
579022780332SAdrian Chadd 	/* Handle completing frames and fail them */
579122780332SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
579222780332SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
579322780332SAdrian Chadd 		ath_tx_default_comp(sc, bf, 1);
579422780332SAdrian Chadd 	}
579522a3aee6SAdrian Chadd 
579622780332SAdrian Chadd }
579722780332SAdrian Chadd 
579822780332SAdrian Chadd /*
579922780332SAdrian Chadd  * Handle a node reassociation.
580022780332SAdrian Chadd  *
580122780332SAdrian Chadd  * We may have a bunch of frames queued to the hardware; those need
580222780332SAdrian Chadd  * to be marked as cleanup.
580322780332SAdrian Chadd  */
580422780332SAdrian Chadd void
580522780332SAdrian Chadd ath_tx_node_reassoc(struct ath_softc *sc, struct ath_node *an)
580622780332SAdrian Chadd {
580722780332SAdrian Chadd 	struct ath_tid *tid;
580822780332SAdrian Chadd 	int i;
580922780332SAdrian Chadd 	ath_bufhead bf_cq;
581022780332SAdrian Chadd 	struct ath_buf *bf;
581122780332SAdrian Chadd 
581222780332SAdrian Chadd 	TAILQ_INIT(&bf_cq);
581322780332SAdrian Chadd 
581422780332SAdrian Chadd 	ATH_TX_UNLOCK_ASSERT(sc);
581522780332SAdrian Chadd 
581622780332SAdrian Chadd 	ATH_TX_LOCK(sc);
581722780332SAdrian Chadd 	for (i = 0; i < IEEE80211_TID_SIZE; i++) {
581822780332SAdrian Chadd 		tid = &an->an_tid[i];
581922780332SAdrian Chadd 		if (tid->hwq_depth == 0)
582022780332SAdrian Chadd 			continue;
582122780332SAdrian Chadd 		ath_tx_tid_pause(sc, tid);
582222780332SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE,
582322780332SAdrian Chadd 		    "%s: %6D: TID %d: cleaning up TID\n",
582422780332SAdrian Chadd 		    __func__,
582522780332SAdrian Chadd 		    an->an_node.ni_macaddr,
582622780332SAdrian Chadd 		    ":",
582722780332SAdrian Chadd 		    i);
582822780332SAdrian Chadd 		ath_tx_tid_cleanup(sc, an, i, &bf_cq);
58295da3fc10SAdrian Chadd 		/*
58305da3fc10SAdrian Chadd 		 * Unpause the TID if no cleanup is required.
58315da3fc10SAdrian Chadd 		 */
58325da3fc10SAdrian Chadd 		if (! tid->cleanup_inprogress)
58335da3fc10SAdrian Chadd 			ath_tx_tid_resume(sc, tid);
583422780332SAdrian Chadd 	}
583522780332SAdrian Chadd 	ATH_TX_UNLOCK(sc);
583622780332SAdrian Chadd 
583722780332SAdrian Chadd 	/* Handle completing frames and fail them */
583822780332SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
583922780332SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
584022780332SAdrian Chadd 		ath_tx_default_comp(sc, bf, 1);
584122780332SAdrian Chadd 	}
5842eb6f0de0SAdrian Chadd }
5843eb6f0de0SAdrian Chadd 
5844eb6f0de0SAdrian Chadd /*
5845eb6f0de0SAdrian Chadd  * Note: net80211 bar_timeout() doesn't call this function on BAR failure;
5846eb6f0de0SAdrian Chadd  * it simply tears down the aggregation session. Ew.
5847eb6f0de0SAdrian Chadd  *
5848eb6f0de0SAdrian Chadd  * It however will call ieee80211_ampdu_stop() which will call
5849eb6f0de0SAdrian Chadd  * ic->ic_addba_stop().
5850eb6f0de0SAdrian Chadd  *
5851eb6f0de0SAdrian Chadd  * XXX This uses a hard-coded max BAR count value; the whole
5852eb6f0de0SAdrian Chadd  * XXX BAR TX success or failure should be better handled!
5853eb6f0de0SAdrian Chadd  */
5854eb6f0de0SAdrian Chadd void
5855eb6f0de0SAdrian Chadd ath_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
5856eb6f0de0SAdrian Chadd     int status)
5857eb6f0de0SAdrian Chadd {
5858eb6f0de0SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
58592aa563dfSAdrian Chadd 	int tid = tap->txa_tid;
5860eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
5861eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
5862eb6f0de0SAdrian Chadd 	int attempts = tap->txa_attempts;
5863eb6f0de0SAdrian Chadd 
58640e22ed0eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
58656d07d3e0SAdrian Chadd 	    "%s: %6D: called; txa_tid=%d, atid->tid=%d, status=%d, attempts=%d\n",
58660e22ed0eSAdrian Chadd 	    __func__,
58679b48fb4bSAdrian Chadd 	    ni->ni_macaddr,
58689b48fb4bSAdrian Chadd 	    ":",
5869e60c4fc2SAdrian Chadd 	    tap->txa_tid,
5870e60c4fc2SAdrian Chadd 	    atid->tid,
58710e22ed0eSAdrian Chadd 	    status,
58720e22ed0eSAdrian Chadd 	    attempts);
5873eb6f0de0SAdrian Chadd 
5874eb6f0de0SAdrian Chadd 	/* Note: This may update the BAW details */
5875eb6f0de0SAdrian Chadd 	sc->sc_bar_response(ni, tap, status);
5876eb6f0de0SAdrian Chadd 
5877eb6f0de0SAdrian Chadd 	/* Unpause the TID */
5878eb6f0de0SAdrian Chadd 	/*
5879eb6f0de0SAdrian Chadd 	 * XXX if this is attempt=50, the TID will be downgraded
5880eb6f0de0SAdrian Chadd 	 * XXX to a non-aggregate session. So we must unpause the
5881eb6f0de0SAdrian Chadd 	 * XXX TID here or it'll never be done.
5882088d8b81SAdrian Chadd 	 *
5883088d8b81SAdrian Chadd 	 * Also, don't call it if bar_tx/bar_wait are 0; something
5884088d8b81SAdrian Chadd 	 * has beaten us to the punch? (XXX figure out what?)
5885eb6f0de0SAdrian Chadd 	 */
5886eb6f0de0SAdrian Chadd 	if (status == 0 || attempts == 50) {
5887375307d4SAdrian Chadd 		ATH_TX_LOCK(sc);
5888088d8b81SAdrian Chadd 		if (atid->bar_tx == 0 || atid->bar_wait == 0)
588983bbd5ebSRui Paulo 			DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
5890088d8b81SAdrian Chadd 			    "%s: huh? bar_tx=%d, bar_wait=%d\n",
5891088d8b81SAdrian Chadd 			    __func__,
5892088d8b81SAdrian Chadd 			    atid->bar_tx, atid->bar_wait);
5893088d8b81SAdrian Chadd 		else
589488b3d483SAdrian Chadd 			ath_tx_tid_bar_unsuspend(sc, atid);
5895375307d4SAdrian Chadd 		ATH_TX_UNLOCK(sc);
5896eb6f0de0SAdrian Chadd 	}
5897eb6f0de0SAdrian Chadd }
5898eb6f0de0SAdrian Chadd 
5899eb6f0de0SAdrian Chadd /*
5900eb6f0de0SAdrian Chadd  * This is called whenever the pending ADDBA request times out.
5901eb6f0de0SAdrian Chadd  * Unpause and reschedule the TID.
5902eb6f0de0SAdrian Chadd  */
5903eb6f0de0SAdrian Chadd void
5904eb6f0de0SAdrian Chadd ath_addba_response_timeout(struct ieee80211_node *ni,
5905eb6f0de0SAdrian Chadd     struct ieee80211_tx_ampdu *tap)
5906eb6f0de0SAdrian Chadd {
5907eb6f0de0SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
59082aa563dfSAdrian Chadd 	int tid = tap->txa_tid;
5909eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
5910eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
5911eb6f0de0SAdrian Chadd 
5912eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
59136d07d3e0SAdrian Chadd 	    "%s: %6D: TID=%d, called; resuming\n",
59149b48fb4bSAdrian Chadd 	    __func__,
59159b48fb4bSAdrian Chadd 	    ni->ni_macaddr,
59166d07d3e0SAdrian Chadd 	    ":",
59176d07d3e0SAdrian Chadd 	    tid);
5918eb6f0de0SAdrian Chadd 
5919375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
5920d3a6425bSAdrian Chadd 	atid->addba_tx_pending = 0;
5921375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
5922d3a6425bSAdrian Chadd 
5923eb6f0de0SAdrian Chadd 	/* Note: This updates the aggregate state to (again) pending */
5924eb6f0de0SAdrian Chadd 	sc->sc_addba_response_timeout(ni, tap);
5925eb6f0de0SAdrian Chadd 
5926eb6f0de0SAdrian Chadd 	/* Unpause the TID; which reschedules it */
5927375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
5928eb6f0de0SAdrian Chadd 	ath_tx_tid_resume(sc, atid);
5929375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
5930eb6f0de0SAdrian Chadd }
59313fdfc330SAdrian Chadd 
59320eb81626SAdrian Chadd /*
59330eb81626SAdrian Chadd  * Check if a node is asleep or not.
59340eb81626SAdrian Chadd  */
5935548a605dSAdrian Chadd int
59360eb81626SAdrian Chadd ath_tx_node_is_asleep(struct ath_softc *sc, struct ath_node *an)
59370eb81626SAdrian Chadd {
59380eb81626SAdrian Chadd 
593922780332SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
59400eb81626SAdrian Chadd 
59410eb81626SAdrian Chadd 	return (an->an_is_powersave);
59420eb81626SAdrian Chadd }
59430eb81626SAdrian Chadd 
59440eb81626SAdrian Chadd /*
59450eb81626SAdrian Chadd  * Mark a node as currently "in powersaving."
59460eb81626SAdrian Chadd  * This suspends all traffic on the node.
59470eb81626SAdrian Chadd  *
59480eb81626SAdrian Chadd  * This must be called with the node/tx locks free.
59490eb81626SAdrian Chadd  *
59500eb81626SAdrian Chadd  * XXX TODO: the locking silliness below is due to how the node
59510eb81626SAdrian Chadd  * locking currently works.  Right now, the node lock is grabbed
59520eb81626SAdrian Chadd  * to do rate control lookups and these are done with the TX
59530eb81626SAdrian Chadd  * queue lock held.  This means the node lock can't be grabbed
59540eb81626SAdrian Chadd  * first here or a LOR will occur.
59550eb81626SAdrian Chadd  *
59560eb81626SAdrian Chadd  * Eventually (hopefully!) the TX path code will only grab
59570eb81626SAdrian Chadd  * the TXQ lock when transmitting and the ath_node lock when
59580eb81626SAdrian Chadd  * doing node/TID operations.  There are other complications -
59590eb81626SAdrian Chadd  * the sched/unsched operations involve walking the per-txq
59600eb81626SAdrian Chadd  * 'active tid' list and this requires both locks to be held.
59610eb81626SAdrian Chadd  */
59620eb81626SAdrian Chadd void
59630eb81626SAdrian Chadd ath_tx_node_sleep(struct ath_softc *sc, struct ath_node *an)
59640eb81626SAdrian Chadd {
59650eb81626SAdrian Chadd 	struct ath_tid *atid;
59660eb81626SAdrian Chadd 	struct ath_txq *txq;
59670eb81626SAdrian Chadd 	int tid;
59680eb81626SAdrian Chadd 
596922780332SAdrian Chadd 	ATH_TX_UNLOCK_ASSERT(sc);
59700eb81626SAdrian Chadd 
59710eb81626SAdrian Chadd 	/* Suspend all traffic on the node */
5972375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
597322a3aee6SAdrian Chadd 
597422a3aee6SAdrian Chadd 	if (an->an_is_powersave) {
597583bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_XMIT,
597622a3aee6SAdrian Chadd 		    "%s: %6D: node was already asleep!\n",
597783bbd5ebSRui Paulo 		    __func__, an->an_node.ni_macaddr, ":");
597822a3aee6SAdrian Chadd 		ATH_TX_UNLOCK(sc);
597922a3aee6SAdrian Chadd 		return;
598022a3aee6SAdrian Chadd 	}
598122a3aee6SAdrian Chadd 
59820eb81626SAdrian Chadd 	for (tid = 0; tid < IEEE80211_TID_SIZE; tid++) {
59830eb81626SAdrian Chadd 		atid = &an->an_tid[tid];
59840eb81626SAdrian Chadd 		txq = sc->sc_ac2q[atid->ac];
59850eb81626SAdrian Chadd 
59860eb81626SAdrian Chadd 		ath_tx_tid_pause(sc, atid);
59870eb81626SAdrian Chadd 	}
59880eb81626SAdrian Chadd 
59890eb81626SAdrian Chadd 	/* Mark node as in powersaving */
59900eb81626SAdrian Chadd 	an->an_is_powersave = 1;
59910eb81626SAdrian Chadd 
599222780332SAdrian Chadd 	ATH_TX_UNLOCK(sc);
59930eb81626SAdrian Chadd }
59940eb81626SAdrian Chadd 
59950eb81626SAdrian Chadd /*
59960eb81626SAdrian Chadd  * Mark a node as currently "awake."
59970eb81626SAdrian Chadd  * This resumes all traffic to the node.
59980eb81626SAdrian Chadd  */
59990eb81626SAdrian Chadd void
60000eb81626SAdrian Chadd ath_tx_node_wakeup(struct ath_softc *sc, struct ath_node *an)
60010eb81626SAdrian Chadd {
60020eb81626SAdrian Chadd 	struct ath_tid *atid;
60030eb81626SAdrian Chadd 	struct ath_txq *txq;
60040eb81626SAdrian Chadd 	int tid;
60050eb81626SAdrian Chadd 
600622780332SAdrian Chadd 	ATH_TX_UNLOCK_ASSERT(sc);
600722780332SAdrian Chadd 
600822780332SAdrian Chadd 	ATH_TX_LOCK(sc);
60090eb81626SAdrian Chadd 
601022a3aee6SAdrian Chadd 	/* !? */
60110eb81626SAdrian Chadd 	if (an->an_is_powersave == 0) {
601222780332SAdrian Chadd 		ATH_TX_UNLOCK(sc);
601383bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_XMIT,
60140eb81626SAdrian Chadd 		    "%s: an=%p: node was already awake\n",
60150eb81626SAdrian Chadd 		    __func__, an);
60160eb81626SAdrian Chadd 		return;
60170eb81626SAdrian Chadd 	}
60180eb81626SAdrian Chadd 
60190eb81626SAdrian Chadd 	/* Mark node as awake */
60200eb81626SAdrian Chadd 	an->an_is_powersave = 0;
602122a3aee6SAdrian Chadd 	/*
602222a3aee6SAdrian Chadd 	 * Clear any pending leaked frame requests
602322a3aee6SAdrian Chadd 	 */
602422a3aee6SAdrian Chadd 	an->an_leak_count = 0;
60250eb81626SAdrian Chadd 
60260eb81626SAdrian Chadd 	for (tid = 0; tid < IEEE80211_TID_SIZE; tid++) {
60270eb81626SAdrian Chadd 		atid = &an->an_tid[tid];
60280eb81626SAdrian Chadd 		txq = sc->sc_ac2q[atid->ac];
60290eb81626SAdrian Chadd 
60300eb81626SAdrian Chadd 		ath_tx_tid_resume(sc, atid);
60310eb81626SAdrian Chadd 	}
6032375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
60330eb81626SAdrian Chadd }
60340eb81626SAdrian Chadd 
60353fdfc330SAdrian Chadd static int
60363fdfc330SAdrian Chadd ath_legacy_dma_txsetup(struct ath_softc *sc)
60373fdfc330SAdrian Chadd {
60383fdfc330SAdrian Chadd 
60393fdfc330SAdrian Chadd 	/* nothing new needed */
60403fdfc330SAdrian Chadd 	return (0);
60413fdfc330SAdrian Chadd }
60423fdfc330SAdrian Chadd 
60433fdfc330SAdrian Chadd static int
60443fdfc330SAdrian Chadd ath_legacy_dma_txteardown(struct ath_softc *sc)
60453fdfc330SAdrian Chadd {
60463fdfc330SAdrian Chadd 
60473fdfc330SAdrian Chadd 	/* nothing new needed */
60483fdfc330SAdrian Chadd 	return (0);
60493fdfc330SAdrian Chadd }
60503fdfc330SAdrian Chadd 
60513fdfc330SAdrian Chadd void
60523fdfc330SAdrian Chadd ath_xmit_setup_legacy(struct ath_softc *sc)
60533fdfc330SAdrian Chadd {
60541006fc0cSAdrian Chadd 	/*
60551006fc0cSAdrian Chadd 	 * For now, just set the descriptor length to sizeof(ath_desc);
60561006fc0cSAdrian Chadd 	 * worry about extracting the real length out of the HAL later.
60571006fc0cSAdrian Chadd 	 */
60581006fc0cSAdrian Chadd 	sc->sc_tx_desclen = sizeof(struct ath_desc);
6059bb327d28SAdrian Chadd 	sc->sc_tx_statuslen = sizeof(struct ath_desc);
60601006fc0cSAdrian Chadd 	sc->sc_tx_nmaps = 1;	/* only one buffer per TX desc */
60613fdfc330SAdrian Chadd 
60623fdfc330SAdrian Chadd 	sc->sc_tx.xmit_setup = ath_legacy_dma_txsetup;
60633fdfc330SAdrian Chadd 	sc->sc_tx.xmit_teardown = ath_legacy_dma_txteardown;
6064f8418db5SAdrian Chadd 	sc->sc_tx.xmit_attach_comp_func = ath_legacy_attach_comp_func;
6065746bab5bSAdrian Chadd 
6066746bab5bSAdrian Chadd 	sc->sc_tx.xmit_dma_restart = ath_legacy_tx_dma_restart;
6067746bab5bSAdrian Chadd 	sc->sc_tx.xmit_handoff = ath_legacy_xmit_handoff;
6068788e6aa9SAdrian Chadd 
6069788e6aa9SAdrian Chadd 	sc->sc_tx.xmit_drain = ath_legacy_tx_drain;
60703fdfc330SAdrian Chadd }
6071