xref: /freebsd/sys/dev/ath/if_ath_tx.c (revision cce634440284f96a81443c77edc4893decda1f0c)
1b8e788a5SAdrian Chadd /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3718cf2ccSPedro F. Giffuni  *
4b8e788a5SAdrian Chadd  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
5c6e9cee2SAdrian Chadd  * Copyright (c) 2010-2012 Adrian Chadd, Xenion Pty Ltd
6b8e788a5SAdrian Chadd  * All rights reserved.
7b8e788a5SAdrian Chadd  *
8b8e788a5SAdrian Chadd  * Redistribution and use in source and binary forms, with or without
9b8e788a5SAdrian Chadd  * modification, are permitted provided that the following conditions
10b8e788a5SAdrian Chadd  * are met:
11b8e788a5SAdrian Chadd  * 1. Redistributions of source code must retain the above copyright
12b8e788a5SAdrian Chadd  *    notice, this list of conditions and the following disclaimer,
13b8e788a5SAdrian Chadd  *    without modification.
14b8e788a5SAdrian Chadd  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
15b8e788a5SAdrian Chadd  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
16b8e788a5SAdrian Chadd  *    redistribution must be conditioned upon including a substantially
17b8e788a5SAdrian Chadd  *    similar Disclaimer requirement for further binary redistribution.
18b8e788a5SAdrian Chadd  *
19b8e788a5SAdrian Chadd  * NO WARRANTY
20b8e788a5SAdrian Chadd  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21b8e788a5SAdrian Chadd  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22b8e788a5SAdrian Chadd  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
23b8e788a5SAdrian Chadd  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
24b8e788a5SAdrian Chadd  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
25b8e788a5SAdrian Chadd  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26b8e788a5SAdrian Chadd  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27b8e788a5SAdrian Chadd  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
28b8e788a5SAdrian Chadd  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29b8e788a5SAdrian Chadd  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30b8e788a5SAdrian Chadd  * THE POSSIBILITY OF SUCH DAMAGES.
31b8e788a5SAdrian Chadd  */
32b8e788a5SAdrian Chadd 
33b8e788a5SAdrian Chadd #include <sys/cdefs.h>
34b8e788a5SAdrian Chadd __FBSDID("$FreeBSD$");
35b8e788a5SAdrian Chadd 
36b8e788a5SAdrian Chadd /*
37b8e788a5SAdrian Chadd  * Driver for the Atheros Wireless LAN controller.
38b8e788a5SAdrian Chadd  *
39b8e788a5SAdrian Chadd  * This software is derived from work of Atsushi Onoe; his contribution
40b8e788a5SAdrian Chadd  * is greatly appreciated.
41b8e788a5SAdrian Chadd  */
42b8e788a5SAdrian Chadd 
43b8e788a5SAdrian Chadd #include "opt_inet.h"
44b8e788a5SAdrian Chadd #include "opt_ath.h"
45b8e788a5SAdrian Chadd #include "opt_wlan.h"
46b8e788a5SAdrian Chadd 
47b8e788a5SAdrian Chadd #include <sys/param.h>
48b8e788a5SAdrian Chadd #include <sys/systm.h>
49b8e788a5SAdrian Chadd #include <sys/sysctl.h>
50b8e788a5SAdrian Chadd #include <sys/mbuf.h>
51b8e788a5SAdrian Chadd #include <sys/malloc.h>
52b8e788a5SAdrian Chadd #include <sys/lock.h>
53b8e788a5SAdrian Chadd #include <sys/mutex.h>
54b8e788a5SAdrian Chadd #include <sys/kernel.h>
55b8e788a5SAdrian Chadd #include <sys/socket.h>
56b8e788a5SAdrian Chadd #include <sys/sockio.h>
57b8e788a5SAdrian Chadd #include <sys/errno.h>
58b8e788a5SAdrian Chadd #include <sys/callout.h>
59b8e788a5SAdrian Chadd #include <sys/bus.h>
60b8e788a5SAdrian Chadd #include <sys/endian.h>
61b8e788a5SAdrian Chadd #include <sys/kthread.h>
62b8e788a5SAdrian Chadd #include <sys/taskqueue.h>
63b8e788a5SAdrian Chadd #include <sys/priv.h>
64f431664cSOlivier Houchard #include <sys/ktr.h>
65b8e788a5SAdrian Chadd 
66b8e788a5SAdrian Chadd #include <machine/bus.h>
67b8e788a5SAdrian Chadd 
68b8e788a5SAdrian Chadd #include <net/if.h>
6976039bc8SGleb Smirnoff #include <net/if_var.h>
70b8e788a5SAdrian Chadd #include <net/if_dl.h>
71b8e788a5SAdrian Chadd #include <net/if_media.h>
72b8e788a5SAdrian Chadd #include <net/if_types.h>
73b8e788a5SAdrian Chadd #include <net/if_arp.h>
74b8e788a5SAdrian Chadd #include <net/ethernet.h>
75b8e788a5SAdrian Chadd #include <net/if_llc.h>
76b8e788a5SAdrian Chadd 
77b8e788a5SAdrian Chadd #include <net80211/ieee80211_var.h>
78b8e788a5SAdrian Chadd #include <net80211/ieee80211_regdomain.h>
79b8e788a5SAdrian Chadd #ifdef IEEE80211_SUPPORT_SUPERG
80b8e788a5SAdrian Chadd #include <net80211/ieee80211_superg.h>
81b8e788a5SAdrian Chadd #endif
82b8e788a5SAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
83b8e788a5SAdrian Chadd #include <net80211/ieee80211_tdma.h>
84b8e788a5SAdrian Chadd #endif
85eb6f0de0SAdrian Chadd #include <net80211/ieee80211_ht.h>
86b8e788a5SAdrian Chadd 
87b8e788a5SAdrian Chadd #include <net/bpf.h>
88b8e788a5SAdrian Chadd 
89b8e788a5SAdrian Chadd #ifdef INET
90b8e788a5SAdrian Chadd #include <netinet/in.h>
91b8e788a5SAdrian Chadd #include <netinet/if_ether.h>
92b8e788a5SAdrian Chadd #endif
93b8e788a5SAdrian Chadd 
94b8e788a5SAdrian Chadd #include <dev/ath/if_athvar.h>
95b8e788a5SAdrian Chadd #include <dev/ath/ath_hal/ah_devid.h>		/* XXX for softled */
96b8e788a5SAdrian Chadd #include <dev/ath/ath_hal/ah_diagcodes.h>
97b8e788a5SAdrian Chadd 
98b8e788a5SAdrian Chadd #include <dev/ath/if_ath_debug.h>
99b8e788a5SAdrian Chadd 
100b8e788a5SAdrian Chadd #ifdef ATH_TX99_DIAG
101b8e788a5SAdrian Chadd #include <dev/ath/ath_tx99/ath_tx99.h>
102b8e788a5SAdrian Chadd #endif
103b8e788a5SAdrian Chadd 
104b8e788a5SAdrian Chadd #include <dev/ath/if_ath_misc.h>
105b8e788a5SAdrian Chadd #include <dev/ath/if_ath_tx.h>
106c1782ce0SAdrian Chadd #include <dev/ath/if_ath_tx_ht.h>
107b8e788a5SAdrian Chadd 
108b69b0dccSAdrian Chadd #ifdef	ATH_DEBUG_ALQ
109b69b0dccSAdrian Chadd #include <dev/ath/if_ath_alq.h>
110b69b0dccSAdrian Chadd #endif
111b69b0dccSAdrian Chadd 
11281a82688SAdrian Chadd /*
113eb6f0de0SAdrian Chadd  * How many retries to perform in software
114eb6f0de0SAdrian Chadd  */
115eb6f0de0SAdrian Chadd #define	SWMAX_RETRIES		10
116eb6f0de0SAdrian Chadd 
1177403d1b9SAdrian Chadd /*
1187403d1b9SAdrian Chadd  * What queue to throw the non-QoS TID traffic into
1197403d1b9SAdrian Chadd  */
1207403d1b9SAdrian Chadd #define	ATH_NONQOS_TID_AC	WME_AC_VO
1217403d1b9SAdrian Chadd 
1220eb81626SAdrian Chadd #if 0
1230eb81626SAdrian Chadd static int ath_tx_node_is_asleep(struct ath_softc *sc, struct ath_node *an);
1240eb81626SAdrian Chadd #endif
125eb6f0de0SAdrian Chadd static int ath_tx_ampdu_pending(struct ath_softc *sc, struct ath_node *an,
126eb6f0de0SAdrian Chadd     int tid);
127eb6f0de0SAdrian Chadd static int ath_tx_ampdu_running(struct ath_softc *sc, struct ath_node *an,
128eb6f0de0SAdrian Chadd     int tid);
129a108d2d6SAdrian Chadd static ieee80211_seq ath_tx_tid_seqno_assign(struct ath_softc *sc,
130a108d2d6SAdrian Chadd     struct ieee80211_node *ni, struct ath_buf *bf, struct mbuf *m0);
131eb6f0de0SAdrian Chadd static int ath_tx_action_frame_override_queue(struct ath_softc *sc,
132eb6f0de0SAdrian Chadd     struct ieee80211_node *ni, struct mbuf *m0, int *tid);
133f1bc738eSAdrian Chadd static struct ath_buf *
134f1bc738eSAdrian Chadd ath_tx_retry_clone(struct ath_softc *sc, struct ath_node *an,
135f1bc738eSAdrian Chadd     struct ath_tid *tid, struct ath_buf *bf);
136eb6f0de0SAdrian Chadd 
137bb327d28SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
138bb327d28SAdrian Chadd void
139bb327d28SAdrian Chadd ath_tx_alq_post(struct ath_softc *sc, struct ath_buf *bf_first)
140bb327d28SAdrian Chadd {
141bb327d28SAdrian Chadd 	struct ath_buf *bf;
142bb327d28SAdrian Chadd 	int i, n;
143bb327d28SAdrian Chadd 	const char *ds;
144bb327d28SAdrian Chadd 
145bb327d28SAdrian Chadd 	/* XXX we should skip out early if debugging isn't enabled! */
146bb327d28SAdrian Chadd 	bf = bf_first;
147bb327d28SAdrian Chadd 
148bb327d28SAdrian Chadd 	while (bf != NULL) {
149bb327d28SAdrian Chadd 		/* XXX should ensure bf_nseg > 0! */
150bb327d28SAdrian Chadd 		if (bf->bf_nseg == 0)
151bb327d28SAdrian Chadd 			break;
152bb327d28SAdrian Chadd 		n = ((bf->bf_nseg - 1) / sc->sc_tx_nmaps) + 1;
153bb327d28SAdrian Chadd 		for (i = 0, ds = (const char *) bf->bf_desc;
154bb327d28SAdrian Chadd 		    i < n;
155bb327d28SAdrian Chadd 		    i++, ds += sc->sc_tx_desclen) {
156bb327d28SAdrian Chadd 			if_ath_alq_post(&sc->sc_alq,
157bb327d28SAdrian Chadd 			    ATH_ALQ_EDMA_TXDESC,
158bb327d28SAdrian Chadd 			    sc->sc_tx_desclen,
159bb327d28SAdrian Chadd 			    ds);
160bb327d28SAdrian Chadd 		}
161bb327d28SAdrian Chadd 		bf = bf->bf_next;
162bb327d28SAdrian Chadd 	}
163bb327d28SAdrian Chadd }
164bb327d28SAdrian Chadd #endif /* ATH_DEBUG_ALQ */
165bb327d28SAdrian Chadd 
166eb6f0de0SAdrian Chadd /*
16781a82688SAdrian Chadd  * Whether to use the 11n rate scenario functions or not
16881a82688SAdrian Chadd  */
16981a82688SAdrian Chadd static inline int
17081a82688SAdrian Chadd ath_tx_is_11n(struct ath_softc *sc)
17181a82688SAdrian Chadd {
1724ddf2cc3SAdrian Chadd 	return ((sc->sc_ah->ah_magic == 0x20065416) ||
1734ddf2cc3SAdrian Chadd 		    (sc->sc_ah->ah_magic == 0x19741014));
17481a82688SAdrian Chadd }
17581a82688SAdrian Chadd 
176eb6f0de0SAdrian Chadd /*
177eb6f0de0SAdrian Chadd  * Obtain the current TID from the given frame.
178eb6f0de0SAdrian Chadd  *
17915e58d4dSAdrian Chadd  * Non-QoS frames get mapped to a TID so frames consistently
18015e58d4dSAdrian Chadd  * go on a sensible queue.
181eb6f0de0SAdrian Chadd  */
182eb6f0de0SAdrian Chadd static int
183eb6f0de0SAdrian Chadd ath_tx_gettid(struct ath_softc *sc, const struct mbuf *m0)
184eb6f0de0SAdrian Chadd {
185eb6f0de0SAdrian Chadd 	const struct ieee80211_frame *wh;
186eb6f0de0SAdrian Chadd 
187eb6f0de0SAdrian Chadd 	wh = mtod(m0, const struct ieee80211_frame *);
18815e58d4dSAdrian Chadd 
18915e58d4dSAdrian Chadd 	/* Non-QoS: map frame to a TID queue for software queueing */
190eb6f0de0SAdrian Chadd 	if (! IEEE80211_QOS_HAS_SEQ(wh))
19115e58d4dSAdrian Chadd 		return (WME_AC_TO_TID(M_WME_GETAC(m0)));
19215e58d4dSAdrian Chadd 
19315e58d4dSAdrian Chadd 	/* QoS - fetch the TID from the header, ignore mbuf WME */
19415e58d4dSAdrian Chadd 	return (ieee80211_gettid(wh));
195eb6f0de0SAdrian Chadd }
196eb6f0de0SAdrian Chadd 
197f1bc738eSAdrian Chadd static void
198f1bc738eSAdrian Chadd ath_tx_set_retry(struct ath_softc *sc, struct ath_buf *bf)
199f1bc738eSAdrian Chadd {
200f1bc738eSAdrian Chadd 	struct ieee80211_frame *wh;
201f1bc738eSAdrian Chadd 
202f1bc738eSAdrian Chadd 	wh = mtod(bf->bf_m, struct ieee80211_frame *);
203f1bc738eSAdrian Chadd 	/* Only update/resync if needed */
204f1bc738eSAdrian Chadd 	if (bf->bf_state.bfs_isretried == 0) {
205f1bc738eSAdrian Chadd 		wh->i_fc[1] |= IEEE80211_FC1_RETRY;
206f1bc738eSAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
207f1bc738eSAdrian Chadd 		    BUS_DMASYNC_PREWRITE);
208f1bc738eSAdrian Chadd 	}
209f1bc738eSAdrian Chadd 	bf->bf_state.bfs_isretried = 1;
210f1bc738eSAdrian Chadd 	bf->bf_state.bfs_retries ++;
211f1bc738eSAdrian Chadd }
212f1bc738eSAdrian Chadd 
213eb6f0de0SAdrian Chadd /*
214eb6f0de0SAdrian Chadd  * Determine what the correct AC queue for the given frame
215eb6f0de0SAdrian Chadd  * should be.
216eb6f0de0SAdrian Chadd  *
21715e58d4dSAdrian Chadd  * For QoS frames, obey the TID.  That way things like
21815e58d4dSAdrian Chadd  * management frames that are related to a given TID
21915e58d4dSAdrian Chadd  * are thus serialised with the rest of the TID traffic,
22015e58d4dSAdrian Chadd  * regardless of net80211 overriding priority.
221eb6f0de0SAdrian Chadd  *
22215e58d4dSAdrian Chadd  * For non-QoS frames, return the mbuf WMI priority.
22315e58d4dSAdrian Chadd  *
22415e58d4dSAdrian Chadd  * This has implications that higher priority non-QoS traffic
22515e58d4dSAdrian Chadd  * may end up being scheduled before other non-QoS traffic,
22615e58d4dSAdrian Chadd  * leading to out-of-sequence packets being emitted.
22715e58d4dSAdrian Chadd  *
22815e58d4dSAdrian Chadd  * (It'd be nice to log/count this so we can see if it
22915e58d4dSAdrian Chadd  * really is a problem.)
23015e58d4dSAdrian Chadd  *
23115e58d4dSAdrian Chadd  * TODO: maybe we should throw multicast traffic, QoS or
23215e58d4dSAdrian Chadd  * otherwise, into a separate TX queue?
233eb6f0de0SAdrian Chadd  */
234eb6f0de0SAdrian Chadd static int
235eb6f0de0SAdrian Chadd ath_tx_getac(struct ath_softc *sc, const struct mbuf *m0)
236eb6f0de0SAdrian Chadd {
237eb6f0de0SAdrian Chadd 	const struct ieee80211_frame *wh;
238eb6f0de0SAdrian Chadd 
23915e58d4dSAdrian Chadd 	wh = mtod(m0, const struct ieee80211_frame *);
24015e58d4dSAdrian Chadd 
24115e58d4dSAdrian Chadd 	/*
24215e58d4dSAdrian Chadd 	 * QoS data frame (sequence number or otherwise) -
24315e58d4dSAdrian Chadd 	 * return hardware queue mapping for the underlying
24415e58d4dSAdrian Chadd 	 * TID.
24515e58d4dSAdrian Chadd 	 */
24615e58d4dSAdrian Chadd 	if (IEEE80211_QOS_HAS_SEQ(wh))
24715e58d4dSAdrian Chadd 		return TID_TO_WME_AC(ieee80211_gettid(wh));
24815e58d4dSAdrian Chadd 
24915e58d4dSAdrian Chadd 	/*
25015e58d4dSAdrian Chadd 	 * Otherwise - return mbuf QoS pri.
25115e58d4dSAdrian Chadd 	 */
25215e58d4dSAdrian Chadd 	return (M_WME_GETAC(m0));
253eb6f0de0SAdrian Chadd }
254eb6f0de0SAdrian Chadd 
255b8e788a5SAdrian Chadd void
256b8e788a5SAdrian Chadd ath_txfrag_cleanup(struct ath_softc *sc,
257b8e788a5SAdrian Chadd 	ath_bufhead *frags, struct ieee80211_node *ni)
258b8e788a5SAdrian Chadd {
259b8e788a5SAdrian Chadd 	struct ath_buf *bf, *next;
260b8e788a5SAdrian Chadd 
261b8e788a5SAdrian Chadd 	ATH_TXBUF_LOCK_ASSERT(sc);
262b8e788a5SAdrian Chadd 
2636b349e5aSAdrian Chadd 	TAILQ_FOREACH_SAFE(bf, frags, bf_list, next) {
264b8e788a5SAdrian Chadd 		/* NB: bf assumed clean */
2656b349e5aSAdrian Chadd 		TAILQ_REMOVE(frags, bf, bf_list);
266e1a50456SAdrian Chadd 		ath_returnbuf_head(sc, bf);
267b8e788a5SAdrian Chadd 		ieee80211_node_decref(ni);
268b8e788a5SAdrian Chadd 	}
269b8e788a5SAdrian Chadd }
270b8e788a5SAdrian Chadd 
271b8e788a5SAdrian Chadd /*
272b8e788a5SAdrian Chadd  * Setup xmit of a fragmented frame.  Allocate a buffer
273b8e788a5SAdrian Chadd  * for each frag and bump the node reference count to
274b8e788a5SAdrian Chadd  * reflect the held reference to be setup by ath_tx_start.
275b8e788a5SAdrian Chadd  */
276b8e788a5SAdrian Chadd int
277b8e788a5SAdrian Chadd ath_txfrag_setup(struct ath_softc *sc, ath_bufhead *frags,
278b8e788a5SAdrian Chadd 	struct mbuf *m0, struct ieee80211_node *ni)
279b8e788a5SAdrian Chadd {
280b8e788a5SAdrian Chadd 	struct mbuf *m;
281b8e788a5SAdrian Chadd 	struct ath_buf *bf;
282b8e788a5SAdrian Chadd 
283b8e788a5SAdrian Chadd 	ATH_TXBUF_LOCK(sc);
284b8e788a5SAdrian Chadd 	for (m = m0->m_nextpkt; m != NULL; m = m->m_nextpkt) {
285af33d486SAdrian Chadd 		/* XXX non-management? */
286af33d486SAdrian Chadd 		bf = _ath_getbuf_locked(sc, ATH_BUFTYPE_NORMAL);
287b8e788a5SAdrian Chadd 		if (bf == NULL) {	/* out of buffers, cleanup */
28883bbd5ebSRui Paulo 			DPRINTF(sc, ATH_DEBUG_XMIT, "%s: no buffer?\n",
289b43facbfSAdrian Chadd 			    __func__);
290b8e788a5SAdrian Chadd 			ath_txfrag_cleanup(sc, frags, ni);
291b8e788a5SAdrian Chadd 			break;
292b8e788a5SAdrian Chadd 		}
293b8e788a5SAdrian Chadd 		ieee80211_node_incref(ni);
2946b349e5aSAdrian Chadd 		TAILQ_INSERT_TAIL(frags, bf, bf_list);
295b8e788a5SAdrian Chadd 	}
296b8e788a5SAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
297b8e788a5SAdrian Chadd 
2986b349e5aSAdrian Chadd 	return !TAILQ_EMPTY(frags);
299b8e788a5SAdrian Chadd }
300b8e788a5SAdrian Chadd 
301b8e788a5SAdrian Chadd static int
302b8e788a5SAdrian Chadd ath_tx_dmasetup(struct ath_softc *sc, struct ath_buf *bf, struct mbuf *m0)
303b8e788a5SAdrian Chadd {
304b8e788a5SAdrian Chadd 	struct mbuf *m;
305b8e788a5SAdrian Chadd 	int error;
306b8e788a5SAdrian Chadd 
307b8e788a5SAdrian Chadd 	/*
308b8e788a5SAdrian Chadd 	 * Load the DMA map so any coalescing is done.  This
309b8e788a5SAdrian Chadd 	 * also calculates the number of descriptors we need.
310b8e788a5SAdrian Chadd 	 */
311b8e788a5SAdrian Chadd 	error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0,
312b8e788a5SAdrian Chadd 				     bf->bf_segs, &bf->bf_nseg,
313b8e788a5SAdrian Chadd 				     BUS_DMA_NOWAIT);
314b8e788a5SAdrian Chadd 	if (error == EFBIG) {
315b8e788a5SAdrian Chadd 		/* XXX packet requires too many descriptors */
31609067b6eSAdrian Chadd 		bf->bf_nseg = ATH_MAX_SCATTER + 1;
317b8e788a5SAdrian Chadd 	} else if (error != 0) {
318b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_busdma++;
319d07be335SAdrian Chadd 		ieee80211_free_mbuf(m0);
320b8e788a5SAdrian Chadd 		return error;
321b8e788a5SAdrian Chadd 	}
322b8e788a5SAdrian Chadd 	/*
323b8e788a5SAdrian Chadd 	 * Discard null packets and check for packets that
324b8e788a5SAdrian Chadd 	 * require too many TX descriptors.  We try to convert
325b8e788a5SAdrian Chadd 	 * the latter to a cluster.
326b8e788a5SAdrian Chadd 	 */
32709067b6eSAdrian Chadd 	if (bf->bf_nseg > ATH_MAX_SCATTER) {		/* too many desc's, linearize */
328b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_linear++;
32909067b6eSAdrian Chadd 		m = m_collapse(m0, M_NOWAIT, ATH_MAX_SCATTER);
330b8e788a5SAdrian Chadd 		if (m == NULL) {
331d07be335SAdrian Chadd 			ieee80211_free_mbuf(m0);
332b8e788a5SAdrian Chadd 			sc->sc_stats.ast_tx_nombuf++;
333b8e788a5SAdrian Chadd 			return ENOMEM;
334b8e788a5SAdrian Chadd 		}
335b8e788a5SAdrian Chadd 		m0 = m;
336b8e788a5SAdrian Chadd 		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0,
337b8e788a5SAdrian Chadd 					     bf->bf_segs, &bf->bf_nseg,
338b8e788a5SAdrian Chadd 					     BUS_DMA_NOWAIT);
339b8e788a5SAdrian Chadd 		if (error != 0) {
340b8e788a5SAdrian Chadd 			sc->sc_stats.ast_tx_busdma++;
341d07be335SAdrian Chadd 			ieee80211_free_mbuf(m0);
342b8e788a5SAdrian Chadd 			return error;
343b8e788a5SAdrian Chadd 		}
34409067b6eSAdrian Chadd 		KASSERT(bf->bf_nseg <= ATH_MAX_SCATTER,
345b8e788a5SAdrian Chadd 		    ("too many segments after defrag; nseg %u", bf->bf_nseg));
346b8e788a5SAdrian Chadd 	} else if (bf->bf_nseg == 0) {		/* null packet, discard */
347b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_nodata++;
348d07be335SAdrian Chadd 		ieee80211_free_mbuf(m0);
349b8e788a5SAdrian Chadd 		return EIO;
350b8e788a5SAdrian Chadd 	}
351b8e788a5SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_XMIT, "%s: m %p len %u\n",
352b8e788a5SAdrian Chadd 		__func__, m0, m0->m_pkthdr.len);
353b8e788a5SAdrian Chadd 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
354b8e788a5SAdrian Chadd 	bf->bf_m = m0;
355b8e788a5SAdrian Chadd 
356b8e788a5SAdrian Chadd 	return 0;
357b8e788a5SAdrian Chadd }
358b8e788a5SAdrian Chadd 
3596edf1dc7SAdrian Chadd /*
3606e84772fSAdrian Chadd  * Chain together segments+descriptors for a frame - 11n or otherwise.
3616e84772fSAdrian Chadd  *
3626e84772fSAdrian Chadd  * For aggregates, this is called on each frame in the aggregate.
3636edf1dc7SAdrian Chadd  */
364b8e788a5SAdrian Chadd static void
3656e84772fSAdrian Chadd ath_tx_chaindesclist(struct ath_softc *sc, struct ath_desc *ds0,
366*cce63444SAdrian Chadd     struct ath_buf *bf, bool is_aggr, int is_first_subframe,
3676e84772fSAdrian Chadd     int is_last_subframe)
368b8e788a5SAdrian Chadd {
369b8e788a5SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
3706e84772fSAdrian Chadd 	char *ds;
3712b200bb4SAdrian Chadd 	int i, bp, dsp;
37246634305SAdrian Chadd 	HAL_DMA_ADDR bufAddrList[4];
37346634305SAdrian Chadd 	uint32_t segLenList[4];
3742b200bb4SAdrian Chadd 	int numTxMaps = 1;
375e2137b86SAdrian Chadd 	int isFirstDesc = 1;
37646634305SAdrian Chadd 
3773d9b1596SAdrian Chadd 	/*
3783d9b1596SAdrian Chadd 	 * XXX There's txdma and txdma_mgmt; the descriptor
3793d9b1596SAdrian Chadd 	 * sizes must match.
3803d9b1596SAdrian Chadd 	 */
3813d9b1596SAdrian Chadd 	struct ath_descdma *dd = &sc->sc_txdma;
382b8e788a5SAdrian Chadd 
383b8e788a5SAdrian Chadd 	/*
384b8e788a5SAdrian Chadd 	 * Fillin the remainder of the descriptor info.
385b8e788a5SAdrian Chadd 	 */
38646634305SAdrian Chadd 
3872b200bb4SAdrian Chadd 	/*
388378a752fSAdrian Chadd 	 * We need the number of TX data pointers in each descriptor.
389378a752fSAdrian Chadd 	 * EDMA and later chips support 4 TX buffers per descriptor;
390378a752fSAdrian Chadd 	 * previous chips just support one.
3912b200bb4SAdrian Chadd 	 */
392378a752fSAdrian Chadd 	numTxMaps = sc->sc_tx_nmaps;
3932b200bb4SAdrian Chadd 
3942b200bb4SAdrian Chadd 	/*
3952b200bb4SAdrian Chadd 	 * For EDMA and later chips ensure the TX map is fully populated
3962b200bb4SAdrian Chadd 	 * before advancing to the next descriptor.
3972b200bb4SAdrian Chadd 	 */
3986e84772fSAdrian Chadd 	ds = (char *) bf->bf_desc;
3992b200bb4SAdrian Chadd 	bp = dsp = 0;
4002b200bb4SAdrian Chadd 	bzero(bufAddrList, sizeof(bufAddrList));
4012b200bb4SAdrian Chadd 	bzero(segLenList, sizeof(segLenList));
4022b200bb4SAdrian Chadd 	for (i = 0; i < bf->bf_nseg; i++) {
4032b200bb4SAdrian Chadd 		bufAddrList[bp] = bf->bf_segs[i].ds_addr;
4042b200bb4SAdrian Chadd 		segLenList[bp] = bf->bf_segs[i].ds_len;
4052b200bb4SAdrian Chadd 		bp++;
4062b200bb4SAdrian Chadd 
4072b200bb4SAdrian Chadd 		/*
4082b200bb4SAdrian Chadd 		 * Go to the next segment if this isn't the last segment
4092b200bb4SAdrian Chadd 		 * and there's space in the current TX map.
4102b200bb4SAdrian Chadd 		 */
4112b200bb4SAdrian Chadd 		if ((i != bf->bf_nseg - 1) && (bp < numTxMaps))
4122b200bb4SAdrian Chadd 			continue;
4132b200bb4SAdrian Chadd 
4142b200bb4SAdrian Chadd 		/*
4152b200bb4SAdrian Chadd 		 * Last segment or we're out of buffer pointers.
4162b200bb4SAdrian Chadd 		 */
4172b200bb4SAdrian Chadd 		bp = 0;
41846634305SAdrian Chadd 
419b8e788a5SAdrian Chadd 		if (i == bf->bf_nseg - 1)
42042083b3dSAdrian Chadd 			ath_hal_settxdesclink(ah, (struct ath_desc *) ds, 0);
421b8e788a5SAdrian Chadd 		else
42242083b3dSAdrian Chadd 			ath_hal_settxdesclink(ah, (struct ath_desc *) ds,
4232b200bb4SAdrian Chadd 			    bf->bf_daddr + dd->dd_descsize * (dsp + 1));
42446634305SAdrian Chadd 
42546634305SAdrian Chadd 		/*
426fc56c9c5SAdrian Chadd 		 * XXX This assumes that bfs_txq is the actual destination
427fc56c9c5SAdrian Chadd 		 * hardware queue at this point.  It may not have been
428fc56c9c5SAdrian Chadd 		 * assigned, it may actually be pointing to the multicast
429fc56c9c5SAdrian Chadd 		 * software TXQ id.  These must be fixed!
43046634305SAdrian Chadd 		 */
43142083b3dSAdrian Chadd 		ath_hal_filltxdesc(ah, (struct ath_desc *) ds
43246634305SAdrian Chadd 			, bufAddrList
43346634305SAdrian Chadd 			, segLenList
4342b200bb4SAdrian Chadd 			, bf->bf_descid		/* XXX desc id */
435fc56c9c5SAdrian Chadd 			, bf->bf_state.bfs_tx_queue
436e2137b86SAdrian Chadd 			, isFirstDesc		/* first segment */
437b8e788a5SAdrian Chadd 			, i == bf->bf_nseg - 1	/* last segment */
43842083b3dSAdrian Chadd 			, (struct ath_desc *) ds0	/* first descriptor */
439b8e788a5SAdrian Chadd 		);
44021840808SAdrian Chadd 
4416e84772fSAdrian Chadd 		/*
4426e84772fSAdrian Chadd 		 * Make sure the 11n aggregate fields are cleared.
4436e84772fSAdrian Chadd 		 *
4446e84772fSAdrian Chadd 		 * XXX TODO: this doesn't need to be called for
4456e84772fSAdrian Chadd 		 * aggregate frames; as it'll be called on all
4466e84772fSAdrian Chadd 		 * sub-frames.  Since the descriptors are in
4476e84772fSAdrian Chadd 		 * non-cacheable memory, this leads to some
4486e84772fSAdrian Chadd 		 * rather slow writes on MIPS/ARM platforms.
4496e84772fSAdrian Chadd 		 */
45021840808SAdrian Chadd 		if (ath_tx_is_11n(sc))
4515d9b19f7SAdrian Chadd 			ath_hal_clr11n_aggr(sc->sc_ah, (struct ath_desc *) ds);
45221840808SAdrian Chadd 
4536e84772fSAdrian Chadd 		/*
4546e84772fSAdrian Chadd 		 * If 11n is enabled, set it up as if it's an aggregate
4556e84772fSAdrian Chadd 		 * frame.
4566e84772fSAdrian Chadd 		 */
4576e84772fSAdrian Chadd 		if (is_last_subframe) {
4586e84772fSAdrian Chadd 			ath_hal_set11n_aggr_last(sc->sc_ah,
4596e84772fSAdrian Chadd 			    (struct ath_desc *) ds);
4606e84772fSAdrian Chadd 		} else if (is_aggr) {
4616e84772fSAdrian Chadd 			/*
4626e84772fSAdrian Chadd 			 * This clears the aggrlen field; so
4636e84772fSAdrian Chadd 			 * the caller needs to call set_aggr_first()!
4646e84772fSAdrian Chadd 			 *
4656e84772fSAdrian Chadd 			 * XXX TODO: don't call this for the first
4666e84772fSAdrian Chadd 			 * descriptor in the first frame in an
4676e84772fSAdrian Chadd 			 * aggregate!
4686e84772fSAdrian Chadd 			 */
4696e84772fSAdrian Chadd 			ath_hal_set11n_aggr_middle(sc->sc_ah,
4706e84772fSAdrian Chadd 			    (struct ath_desc *) ds,
4716e84772fSAdrian Chadd 			    bf->bf_state.bfs_ndelim);
4726e84772fSAdrian Chadd 		}
473e2137b86SAdrian Chadd 		isFirstDesc = 0;
47442083b3dSAdrian Chadd 		bf->bf_lastds = (struct ath_desc *) ds;
4752b200bb4SAdrian Chadd 
4762b200bb4SAdrian Chadd 		/*
4772b200bb4SAdrian Chadd 		 * Don't forget to skip to the next descriptor.
4782b200bb4SAdrian Chadd 		 */
47942083b3dSAdrian Chadd 		ds += sc->sc_tx_desclen;
4802b200bb4SAdrian Chadd 		dsp++;
4812b200bb4SAdrian Chadd 
4822b200bb4SAdrian Chadd 		/*
4832b200bb4SAdrian Chadd 		 * .. and don't forget to blank these out!
4842b200bb4SAdrian Chadd 		 */
4852b200bb4SAdrian Chadd 		bzero(bufAddrList, sizeof(bufAddrList));
4862b200bb4SAdrian Chadd 		bzero(segLenList, sizeof(segLenList));
487b8e788a5SAdrian Chadd 	}
4884d7f8837SAdrian Chadd 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
48981a82688SAdrian Chadd }
49081a82688SAdrian Chadd 
491eb6f0de0SAdrian Chadd /*
492d34a7347SAdrian Chadd  * Set the rate control fields in the given descriptor based on
493d34a7347SAdrian Chadd  * the bf_state fields and node state.
494d34a7347SAdrian Chadd  *
495d34a7347SAdrian Chadd  * The bfs fields should already be set with the relevant rate
496d34a7347SAdrian Chadd  * control information, including whether MRR is to be enabled.
497d34a7347SAdrian Chadd  *
498d34a7347SAdrian Chadd  * Since the FreeBSD HAL currently sets up the first TX rate
499d34a7347SAdrian Chadd  * in ath_hal_setuptxdesc(), this will setup the MRR
500d34a7347SAdrian Chadd  * conditionally for the pre-11n chips, and call ath_buf_set_rate
501d34a7347SAdrian Chadd  * unconditionally for 11n chips. These require the 11n rate
502d34a7347SAdrian Chadd  * scenario to be set if MCS rates are enabled, so it's easier
503d34a7347SAdrian Chadd  * to just always call it. The caller can then only set rates 2, 3
504d34a7347SAdrian Chadd  * and 4 if multi-rate retry is needed.
505d34a7347SAdrian Chadd  */
506d34a7347SAdrian Chadd static void
507d34a7347SAdrian Chadd ath_tx_set_ratectrl(struct ath_softc *sc, struct ieee80211_node *ni,
508d34a7347SAdrian Chadd     struct ath_buf *bf)
509d34a7347SAdrian Chadd {
510d34a7347SAdrian Chadd 	struct ath_rc_series *rc = bf->bf_state.bfs_rc;
511d34a7347SAdrian Chadd 
512d34a7347SAdrian Chadd 	/* If mrr is disabled, blank tries 1, 2, 3 */
513d34a7347SAdrian Chadd 	if (! bf->bf_state.bfs_ismrr)
514d34a7347SAdrian Chadd 		rc[1].tries = rc[2].tries = rc[3].tries = 0;
515d34a7347SAdrian Chadd 
516491e1248SAdrian Chadd #if 0
517491e1248SAdrian Chadd 	/*
518491e1248SAdrian Chadd 	 * If NOACK is set, just set ntries=1.
519491e1248SAdrian Chadd 	 */
520491e1248SAdrian Chadd 	else if (bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) {
521491e1248SAdrian Chadd 		rc[1].tries = rc[2].tries = rc[3].tries = 0;
522491e1248SAdrian Chadd 		rc[0].tries = 1;
523491e1248SAdrian Chadd 	}
524491e1248SAdrian Chadd #endif
525491e1248SAdrian Chadd 
526d34a7347SAdrian Chadd 	/*
527d34a7347SAdrian Chadd 	 * Always call - that way a retried descriptor will
528d34a7347SAdrian Chadd 	 * have the MRR fields overwritten.
529d34a7347SAdrian Chadd 	 *
530d34a7347SAdrian Chadd 	 * XXX TODO: see if this is really needed - setting up
531d34a7347SAdrian Chadd 	 * the first descriptor should set the MRR fields to 0
532d34a7347SAdrian Chadd 	 * for us anyway.
533d34a7347SAdrian Chadd 	 */
534d34a7347SAdrian Chadd 	if (ath_tx_is_11n(sc)) {
535d34a7347SAdrian Chadd 		ath_buf_set_rate(sc, ni, bf);
536d34a7347SAdrian Chadd 	} else {
537d34a7347SAdrian Chadd 		ath_hal_setupxtxdesc(sc->sc_ah, bf->bf_desc
538d34a7347SAdrian Chadd 			, rc[1].ratecode, rc[1].tries
539d34a7347SAdrian Chadd 			, rc[2].ratecode, rc[2].tries
540d34a7347SAdrian Chadd 			, rc[3].ratecode, rc[3].tries
541d34a7347SAdrian Chadd 		);
542d34a7347SAdrian Chadd 	}
543d34a7347SAdrian Chadd }
544d34a7347SAdrian Chadd 
545d34a7347SAdrian Chadd /*
546eb6f0de0SAdrian Chadd  * Setup segments+descriptors for an 11n aggregate.
547eb6f0de0SAdrian Chadd  * bf_first is the first buffer in the aggregate.
548eb6f0de0SAdrian Chadd  * The descriptor list must already been linked together using
549eb6f0de0SAdrian Chadd  * bf->bf_next.
550eb6f0de0SAdrian Chadd  */
551eb6f0de0SAdrian Chadd static void
552eb6f0de0SAdrian Chadd ath_tx_setds_11n(struct ath_softc *sc, struct ath_buf *bf_first)
553eb6f0de0SAdrian Chadd {
554eb6f0de0SAdrian Chadd 	struct ath_buf *bf, *bf_prev = NULL;
5556e84772fSAdrian Chadd 	struct ath_desc *ds0 = bf_first->bf_desc;
556eb6f0de0SAdrian Chadd 
557eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: nframes=%d, al=%d\n",
558eb6f0de0SAdrian Chadd 	    __func__, bf_first->bf_state.bfs_nframes,
559eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_al);
560eb6f0de0SAdrian Chadd 
5617d9dd2acSAdrian Chadd 	bf = bf_first;
5627d9dd2acSAdrian Chadd 
5637d9dd2acSAdrian Chadd 	if (bf->bf_state.bfs_txrate0 == 0)
56483bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: bf=%p, txrate0=%d\n",
5657d9dd2acSAdrian Chadd 		    __func__, bf, 0);
5667d9dd2acSAdrian Chadd 	if (bf->bf_state.bfs_rc[0].ratecode == 0)
56783bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: bf=%p, rix0=%d\n",
5687d9dd2acSAdrian Chadd 		    __func__, bf, 0);
5697d9dd2acSAdrian Chadd 
570eb6f0de0SAdrian Chadd 	/*
5716e84772fSAdrian Chadd 	 * Setup all descriptors of all subframes - this will
5726e84772fSAdrian Chadd 	 * call ath_hal_set11naggrmiddle() on every frame.
573eb6f0de0SAdrian Chadd 	 */
574eb6f0de0SAdrian Chadd 	while (bf != NULL) {
575eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
576eb6f0de0SAdrian Chadd 		    "%s: bf=%p, nseg=%d, pktlen=%d, seqno=%d\n",
577eb6f0de0SAdrian Chadd 		    __func__, bf, bf->bf_nseg, bf->bf_state.bfs_pktlen,
578eb6f0de0SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
579eb6f0de0SAdrian Chadd 
5806e84772fSAdrian Chadd 		/*
5816e84772fSAdrian Chadd 		 * Setup the initial fields for the first descriptor - all
5826e84772fSAdrian Chadd 		 * the non-11n specific stuff.
5836e84772fSAdrian Chadd 		 */
5846e84772fSAdrian Chadd 		ath_hal_setuptxdesc(sc->sc_ah, bf->bf_desc
5856e84772fSAdrian Chadd 			, bf->bf_state.bfs_pktlen	/* packet length */
5866e84772fSAdrian Chadd 			, bf->bf_state.bfs_hdrlen	/* header length */
5876e84772fSAdrian Chadd 			, bf->bf_state.bfs_atype	/* Atheros packet type */
5886e84772fSAdrian Chadd 			, bf->bf_state.bfs_txpower	/* txpower */
5896e84772fSAdrian Chadd 			, bf->bf_state.bfs_txrate0
5906e84772fSAdrian Chadd 			, bf->bf_state.bfs_try0		/* series 0 rate/tries */
5916e84772fSAdrian Chadd 			, bf->bf_state.bfs_keyix	/* key cache index */
5926e84772fSAdrian Chadd 			, bf->bf_state.bfs_txantenna	/* antenna mode */
5936e84772fSAdrian Chadd 			, bf->bf_state.bfs_txflags | HAL_TXDESC_INTREQ	/* flags */
5946e84772fSAdrian Chadd 			, bf->bf_state.bfs_ctsrate	/* rts/cts rate */
5956e84772fSAdrian Chadd 			, bf->bf_state.bfs_ctsduration	/* rts/cts duration */
5966e84772fSAdrian Chadd 		);
5976e84772fSAdrian Chadd 
5986e84772fSAdrian Chadd 		/*
5996e84772fSAdrian Chadd 		 * First descriptor? Setup the rate control and initial
6006e84772fSAdrian Chadd 		 * aggregate header information.
6016e84772fSAdrian Chadd 		 */
6026e84772fSAdrian Chadd 		if (bf == bf_first) {
6036e84772fSAdrian Chadd 			/*
6046e84772fSAdrian Chadd 			 * setup first desc with rate and aggr info
6056e84772fSAdrian Chadd 			 */
6066e84772fSAdrian Chadd 			ath_tx_set_ratectrl(sc, bf->bf_node, bf);
6076e84772fSAdrian Chadd 		}
6086e84772fSAdrian Chadd 
6096e84772fSAdrian Chadd 		/*
6106e84772fSAdrian Chadd 		 * Setup the descriptors for a multi-descriptor frame.
6116e84772fSAdrian Chadd 		 * This is both aggregate and non-aggregate aware.
6126e84772fSAdrian Chadd 		 */
6136e84772fSAdrian Chadd 		ath_tx_chaindesclist(sc, ds0, bf,
6146e84772fSAdrian Chadd 		    1, /* is_aggr */
6156e84772fSAdrian Chadd 		    !! (bf == bf_first), /* is_first_subframe */
6166e84772fSAdrian Chadd 		    !! (bf->bf_next == NULL) /* is_last_subframe */
6176e84772fSAdrian Chadd 		    );
6186e84772fSAdrian Chadd 
6196e84772fSAdrian Chadd 		if (bf == bf_first) {
6206e84772fSAdrian Chadd 			/*
6216e84772fSAdrian Chadd 			 * Initialise the first 11n aggregate with the
6226e84772fSAdrian Chadd 			 * aggregate length and aggregate enable bits.
6236e84772fSAdrian Chadd 			 */
6246e84772fSAdrian Chadd 			ath_hal_set11n_aggr_first(sc->sc_ah,
6256e84772fSAdrian Chadd 			    ds0,
6266e84772fSAdrian Chadd 			    bf->bf_state.bfs_al,
6276e84772fSAdrian Chadd 			    bf->bf_state.bfs_ndelim);
6286e84772fSAdrian Chadd 		}
629eb6f0de0SAdrian Chadd 
630eb6f0de0SAdrian Chadd 		/*
631eb6f0de0SAdrian Chadd 		 * Link the last descriptor of the previous frame
632eb6f0de0SAdrian Chadd 		 * to the beginning descriptor of this frame.
633eb6f0de0SAdrian Chadd 		 */
634eb6f0de0SAdrian Chadd 		if (bf_prev != NULL)
635bb069955SAdrian Chadd 			ath_hal_settxdesclink(sc->sc_ah, bf_prev->bf_lastds,
636bb069955SAdrian Chadd 			    bf->bf_daddr);
637eb6f0de0SAdrian Chadd 
638eb6f0de0SAdrian Chadd 		/* Save a copy so we can link the next descriptor in */
639eb6f0de0SAdrian Chadd 		bf_prev = bf;
640eb6f0de0SAdrian Chadd 		bf = bf->bf_next;
641eb6f0de0SAdrian Chadd 	}
642eb6f0de0SAdrian Chadd 
643eb6f0de0SAdrian Chadd 	/*
644eb6f0de0SAdrian Chadd 	 * Set the first descriptor bf_lastds field to point to
645eb6f0de0SAdrian Chadd 	 * the last descriptor in the last subframe, that's where
646eb6f0de0SAdrian Chadd 	 * the status update will occur.
647eb6f0de0SAdrian Chadd 	 */
648eb6f0de0SAdrian Chadd 	bf_first->bf_lastds = bf_prev->bf_lastds;
649eb6f0de0SAdrian Chadd 
650eb6f0de0SAdrian Chadd 	/*
651eb6f0de0SAdrian Chadd 	 * And bf_last in the first descriptor points to the end of
652eb6f0de0SAdrian Chadd 	 * the aggregate list.
653eb6f0de0SAdrian Chadd 	 */
654eb6f0de0SAdrian Chadd 	bf_first->bf_last = bf_prev;
655eb6f0de0SAdrian Chadd 
656bbdf3df1SAdrian Chadd 	/*
657bbdf3df1SAdrian Chadd 	 * For non-AR9300 NICs, which require the rate control
658bbdf3df1SAdrian Chadd 	 * in the final descriptor - let's set that up now.
659bbdf3df1SAdrian Chadd 	 *
660bbdf3df1SAdrian Chadd 	 * This is because the filltxdesc() HAL call doesn't
661bbdf3df1SAdrian Chadd 	 * populate the last segment with rate control information
662bbdf3df1SAdrian Chadd 	 * if firstSeg is also true.  For non-aggregate frames
663bbdf3df1SAdrian Chadd 	 * that is fine, as the first frame already has rate control
664bbdf3df1SAdrian Chadd 	 * info.  But if the last frame in an aggregate has one
665bbdf3df1SAdrian Chadd 	 * descriptor, both firstseg and lastseg will be true and
666bbdf3df1SAdrian Chadd 	 * the rate info isn't copied.
667bbdf3df1SAdrian Chadd 	 *
668bbdf3df1SAdrian Chadd 	 * This is inefficient on MIPS/ARM platforms that have
669bbdf3df1SAdrian Chadd 	 * non-cachable memory for TX descriptors, but we'll just
670bbdf3df1SAdrian Chadd 	 * make do for now.
671bbdf3df1SAdrian Chadd 	 *
672bbdf3df1SAdrian Chadd 	 * As to why the rate table is stashed in the last descriptor
673bbdf3df1SAdrian Chadd 	 * rather than the first descriptor?  Because proctxdesc()
674bbdf3df1SAdrian Chadd 	 * is called on the final descriptor in an MPDU or A-MPDU -
675bbdf3df1SAdrian Chadd 	 * ie, the one that gets updated by the hardware upon
676bbdf3df1SAdrian Chadd 	 * completion.  That way proctxdesc() doesn't need to know
677bbdf3df1SAdrian Chadd 	 * about the first _and_ last TX descriptor.
678bbdf3df1SAdrian Chadd 	 */
679bbdf3df1SAdrian Chadd 	ath_hal_setuplasttxdesc(sc->sc_ah, bf_prev->bf_lastds, ds0);
680bbdf3df1SAdrian Chadd 
681eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: end\n", __func__);
682eb6f0de0SAdrian Chadd }
683eb6f0de0SAdrian Chadd 
68446634305SAdrian Chadd /*
68546634305SAdrian Chadd  * Hand-off a frame to the multicast TX queue.
68646634305SAdrian Chadd  *
68746634305SAdrian Chadd  * This is a software TXQ which will be appended to the CAB queue
68846634305SAdrian Chadd  * during the beacon setup code.
68946634305SAdrian Chadd  *
69046634305SAdrian Chadd  * XXX TODO: since the AR9300 EDMA TX queue support wants the QCU ID
691fc56c9c5SAdrian Chadd  * as part of the TX descriptor, bf_state.bfs_tx_queue must be updated
69246634305SAdrian Chadd  * with the actual hardware txq, or all of this will fall apart.
69346634305SAdrian Chadd  *
69446634305SAdrian Chadd  * XXX It may not be a bad idea to just stuff the QCU ID into bf_state
695fc56c9c5SAdrian Chadd  * and retire bfs_tx_queue; then make sure the CABQ QCU ID is populated
69646634305SAdrian Chadd  * correctly.
69746634305SAdrian Chadd  */
698eb6f0de0SAdrian Chadd static void
699eb6f0de0SAdrian Chadd ath_tx_handoff_mcast(struct ath_softc *sc, struct ath_txq *txq,
700eb6f0de0SAdrian Chadd     struct ath_buf *bf)
701eb6f0de0SAdrian Chadd {
702375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
703375307d4SAdrian Chadd 
704eb6f0de0SAdrian Chadd 	KASSERT((bf->bf_flags & ATH_BUF_BUSY) == 0,
705eb6f0de0SAdrian Chadd 	     ("%s: busy status 0x%x", __func__, bf->bf_flags));
70656a85978SAdrian Chadd 
70797c9a8e8SAdrian Chadd 	/*
70897c9a8e8SAdrian Chadd 	 * Ensure that the tx queue is the cabq, so things get
70997c9a8e8SAdrian Chadd 	 * mapped correctly.
71097c9a8e8SAdrian Chadd 	 */
71197c9a8e8SAdrian Chadd 	if (bf->bf_state.bfs_tx_queue != sc->sc_cabq->axq_qnum) {
71283bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_XMIT,
71397c9a8e8SAdrian Chadd 		    "%s: bf=%p, bfs_tx_queue=%d, axq_qnum=%d\n",
71483bbd5ebSRui Paulo 		    __func__, bf, bf->bf_state.bfs_tx_queue,
71597c9a8e8SAdrian Chadd 		    txq->axq_qnum);
71697c9a8e8SAdrian Chadd 	}
71797c9a8e8SAdrian Chadd 
71856a85978SAdrian Chadd 	ATH_TXQ_LOCK(txq);
7190891354cSAdrian Chadd 	if (ATH_TXQ_LAST(txq, axq_q_s) != NULL) {
7200891354cSAdrian Chadd 		struct ath_buf *bf_last = ATH_TXQ_LAST(txq, axq_q_s);
721eb6f0de0SAdrian Chadd 		struct ieee80211_frame *wh;
722eb6f0de0SAdrian Chadd 
723eb6f0de0SAdrian Chadd 		/* mark previous frame */
7240891354cSAdrian Chadd 		wh = mtod(bf_last->bf_m, struct ieee80211_frame *);
725eb6f0de0SAdrian Chadd 		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
7260891354cSAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf_last->bf_dmamap,
727eb6f0de0SAdrian Chadd 		    BUS_DMASYNC_PREWRITE);
728eb6f0de0SAdrian Chadd 
729eb6f0de0SAdrian Chadd 		/* link descriptor */
7300891354cSAdrian Chadd 		ath_hal_settxdesclink(sc->sc_ah,
7310891354cSAdrian Chadd 		    bf_last->bf_lastds,
7320891354cSAdrian Chadd 		    bf->bf_daddr);
733eb6f0de0SAdrian Chadd 	}
734eb6f0de0SAdrian Chadd 	ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
735b837332dSAdrian Chadd 	ATH_TXQ_UNLOCK(txq);
736eb6f0de0SAdrian Chadd }
737eb6f0de0SAdrian Chadd 
738eb6f0de0SAdrian Chadd /*
739eb6f0de0SAdrian Chadd  * Hand-off packet to a hardware queue.
740eb6f0de0SAdrian Chadd  */
741eb6f0de0SAdrian Chadd static void
742d4365d16SAdrian Chadd ath_tx_handoff_hw(struct ath_softc *sc, struct ath_txq *txq,
743d4365d16SAdrian Chadd     struct ath_buf *bf)
744eb6f0de0SAdrian Chadd {
745eb6f0de0SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
7469be82a42SAdrian Chadd 	struct ath_buf *bf_first;
74781a82688SAdrian Chadd 
748b8e788a5SAdrian Chadd 	/*
749b8e788a5SAdrian Chadd 	 * Insert the frame on the outbound list and pass it on
750b8e788a5SAdrian Chadd 	 * to the hardware.  Multicast frames buffered for power
751b8e788a5SAdrian Chadd 	 * save stations and transmit from the CAB queue are stored
752b8e788a5SAdrian Chadd 	 * on a s/w only queue and loaded on to the CAB queue in
753b8e788a5SAdrian Chadd 	 * the SWBA handler since frames only go out on DTIM and
754b8e788a5SAdrian Chadd 	 * to avoid possible races.
755b8e788a5SAdrian Chadd 	 */
756375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
757b8e788a5SAdrian Chadd 	KASSERT((bf->bf_flags & ATH_BUF_BUSY) == 0,
758eb6f0de0SAdrian Chadd 	     ("%s: busy status 0x%x", __func__, bf->bf_flags));
759eb6f0de0SAdrian Chadd 	KASSERT(txq->axq_qnum != ATH_TXQ_SWQ,
760eb6f0de0SAdrian Chadd 	     ("ath_tx_handoff_hw called for mcast queue"));
761eb6f0de0SAdrian Chadd 
7629be82a42SAdrian Chadd 	/*
763f5c30c4eSAdrian Chadd 	 * XXX We should instead just verify that sc_txstart_cnt
764f5c30c4eSAdrian Chadd 	 * or ath_txproc_cnt > 0.  That would mean that
765f5c30c4eSAdrian Chadd 	 * the reset is going to be waiting for us to complete.
7669be82a42SAdrian Chadd 	 */
767f5c30c4eSAdrian Chadd 	if (sc->sc_txproc_cnt == 0 && sc->sc_txstart_cnt == 0) {
768f5c30c4eSAdrian Chadd 		device_printf(sc->sc_dev,
769f5c30c4eSAdrian Chadd 		    "%s: TX dispatch without holding txcount/txstart refcnt!\n",
770ef27340cSAdrian Chadd 		    __func__);
771ef27340cSAdrian Chadd 	}
772f5c30c4eSAdrian Chadd 
773f5c30c4eSAdrian Chadd 	/*
774f5c30c4eSAdrian Chadd 	 * XXX .. this is going to cause the hardware to get upset;
775f5c30c4eSAdrian Chadd 	 * so we really should find some way to drop or queue
776f5c30c4eSAdrian Chadd 	 * things.
777f5c30c4eSAdrian Chadd 	 */
778ef27340cSAdrian Chadd 
779b837332dSAdrian Chadd 	ATH_TXQ_LOCK(txq);
780b8e788a5SAdrian Chadd 
781b8e788a5SAdrian Chadd 	/*
7829be82a42SAdrian Chadd 	 * XXX TODO: if there's a holdingbf, then
7839be82a42SAdrian Chadd 	 * ATH_TXQ_PUTRUNNING should be clear.
7849be82a42SAdrian Chadd 	 *
7859be82a42SAdrian Chadd 	 * If there is a holdingbf and the list is empty,
7869be82a42SAdrian Chadd 	 * then axq_link should be pointing to the holdingbf.
7879be82a42SAdrian Chadd 	 *
7889be82a42SAdrian Chadd 	 * Otherwise it should point to the last descriptor
7899be82a42SAdrian Chadd 	 * in the last ath_buf.
7909be82a42SAdrian Chadd 	 *
7919be82a42SAdrian Chadd 	 * In any case, we should really ensure that we
7929be82a42SAdrian Chadd 	 * update the previous descriptor link pointer to
7939be82a42SAdrian Chadd 	 * this descriptor, regardless of all of the above state.
7949be82a42SAdrian Chadd 	 *
7959be82a42SAdrian Chadd 	 * For now this is captured by having axq_link point
7969be82a42SAdrian Chadd 	 * to either the holdingbf (if the TXQ list is empty)
7979be82a42SAdrian Chadd 	 * or the end of the list (if the TXQ list isn't empty.)
7989be82a42SAdrian Chadd 	 * I'd rather just kill axq_link here and do it as above.
799b8e788a5SAdrian Chadd 	 */
80003682514SAdrian Chadd 
801b8e788a5SAdrian Chadd 	/*
8029be82a42SAdrian Chadd 	 * Append the frame to the TX queue.
803b8e788a5SAdrian Chadd 	 */
804b8e788a5SAdrian Chadd 	ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
805b1dddc28SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 3,
806b1dddc28SAdrian Chadd 	    "ath_tx_handoff: non-tdma: txq=%u, add bf=%p "
80703682514SAdrian Chadd 	    "depth=%d",
80803682514SAdrian Chadd 	    txq->axq_qnum,
80903682514SAdrian Chadd 	    bf,
81003682514SAdrian Chadd 	    txq->axq_depth);
81103682514SAdrian Chadd 
8129be82a42SAdrian Chadd 	/*
8139be82a42SAdrian Chadd 	 * If there's a link pointer, update it.
8149be82a42SAdrian Chadd 	 *
8159be82a42SAdrian Chadd 	 * XXX we should replace this with the above logic, just
8169be82a42SAdrian Chadd 	 * to kill axq_link with fire.
8179be82a42SAdrian Chadd 	 */
8189be82a42SAdrian Chadd 	if (txq->axq_link != NULL) {
819b8e788a5SAdrian Chadd 		*txq->axq_link = bf->bf_daddr;
820b8e788a5SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_XMIT,
821b8e788a5SAdrian Chadd 		    "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
822b8e788a5SAdrian Chadd 		    txq->axq_qnum, txq->axq_link,
823d4365d16SAdrian Chadd 		    (caddr_t)bf->bf_daddr, bf->bf_desc,
824d4365d16SAdrian Chadd 		    txq->axq_depth);
82503682514SAdrian Chadd 		ATH_KTR(sc, ATH_KTR_TX, 5,
82603682514SAdrian Chadd 		    "ath_tx_handoff: non-tdma: link[%u](%p)=%p (%p) "
82703682514SAdrian Chadd 		    "lastds=%d",
82803682514SAdrian Chadd 		    txq->axq_qnum, txq->axq_link,
82903682514SAdrian Chadd 		    (caddr_t)bf->bf_daddr, bf->bf_desc,
83003682514SAdrian Chadd 		    bf->bf_lastds);
831b8e788a5SAdrian Chadd 	}
83297c9a8e8SAdrian Chadd 
8339be82a42SAdrian Chadd 	/*
8349be82a42SAdrian Chadd 	 * If we've not pushed anything into the hardware yet,
8359be82a42SAdrian Chadd 	 * push the head of the queue into the TxDP.
8369be82a42SAdrian Chadd 	 *
8379be82a42SAdrian Chadd 	 * Once we've started DMA, there's no guarantee that
8389be82a42SAdrian Chadd 	 * updating the TxDP with a new value will actually work.
8399be82a42SAdrian Chadd 	 * So we just don't do that - if we hit the end of the list,
8409be82a42SAdrian Chadd 	 * we keep that buffer around (the "holding buffer") and
8419be82a42SAdrian Chadd 	 * re-start DMA by updating the link pointer of _that_
8429be82a42SAdrian Chadd 	 * descriptor and then restart DMA.
8439be82a42SAdrian Chadd 	 */
8449be82a42SAdrian Chadd 	if (! (txq->axq_flags & ATH_TXQ_PUTRUNNING)) {
8459be82a42SAdrian Chadd 		bf_first = TAILQ_FIRST(&txq->axq_q);
8469be82a42SAdrian Chadd 		txq->axq_flags |= ATH_TXQ_PUTRUNNING;
8479be82a42SAdrian Chadd 		ath_hal_puttxbuf(ah, txq->axq_qnum, bf_first->bf_daddr);
8489be82a42SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_XMIT,
8499be82a42SAdrian Chadd 		    "%s: TXDP[%u] = %p (%p) depth %d\n",
8509be82a42SAdrian Chadd 		    __func__, txq->axq_qnum,
8519be82a42SAdrian Chadd 		    (caddr_t)bf_first->bf_daddr, bf_first->bf_desc,
8529be82a42SAdrian Chadd 		    txq->axq_depth);
8539be82a42SAdrian Chadd 		ATH_KTR(sc, ATH_KTR_TX, 5,
8549be82a42SAdrian Chadd 		    "ath_tx_handoff: TXDP[%u] = %p (%p) "
8559be82a42SAdrian Chadd 		    "lastds=%p depth %d",
8569be82a42SAdrian Chadd 		    txq->axq_qnum,
8579be82a42SAdrian Chadd 		    (caddr_t)bf_first->bf_daddr, bf_first->bf_desc,
8589be82a42SAdrian Chadd 		    bf_first->bf_lastds,
8599be82a42SAdrian Chadd 		    txq->axq_depth);
8609be82a42SAdrian Chadd 	}
8619be82a42SAdrian Chadd 
8629be82a42SAdrian Chadd 	/*
8639be82a42SAdrian Chadd 	 * Ensure that the bf TXQ matches this TXQ, so later
8649be82a42SAdrian Chadd 	 * checking and holding buffer manipulation is sane.
8659be82a42SAdrian Chadd 	 */
86697c9a8e8SAdrian Chadd 	if (bf->bf_state.bfs_tx_queue != txq->axq_qnum) {
86783bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_XMIT,
86897c9a8e8SAdrian Chadd 		    "%s: bf=%p, bfs_tx_queue=%d, axq_qnum=%d\n",
86983bbd5ebSRui Paulo 		    __func__, bf, bf->bf_state.bfs_tx_queue,
87097c9a8e8SAdrian Chadd 		    txq->axq_qnum);
87197c9a8e8SAdrian Chadd 	}
87297c9a8e8SAdrian Chadd 
8739be82a42SAdrian Chadd 	/*
8749be82a42SAdrian Chadd 	 * Track aggregate queue depth.
8759be82a42SAdrian Chadd 	 */
8766edf1dc7SAdrian Chadd 	if (bf->bf_state.bfs_aggr)
8776edf1dc7SAdrian Chadd 		txq->axq_aggr_depth++;
8789be82a42SAdrian Chadd 
8799be82a42SAdrian Chadd 	/*
8809be82a42SAdrian Chadd 	 * Update the link pointer.
8819be82a42SAdrian Chadd 	 */
882bb069955SAdrian Chadd 	ath_hal_gettxdesclinkptr(ah, bf->bf_lastds, &txq->axq_link);
8839be82a42SAdrian Chadd 
8849be82a42SAdrian Chadd 	/*
8859be82a42SAdrian Chadd 	 * Start DMA.
8869be82a42SAdrian Chadd 	 *
8879be82a42SAdrian Chadd 	 * If we wrote a TxDP above, DMA will start from here.
8889be82a42SAdrian Chadd 	 *
8899be82a42SAdrian Chadd 	 * If DMA is running, it'll do nothing.
8909be82a42SAdrian Chadd 	 *
8919be82a42SAdrian Chadd 	 * If the DMA engine hit the end of the QCU list (ie LINK=NULL,
8929be82a42SAdrian Chadd 	 * or VEOL) then it stops at the last transmitted write.
8939be82a42SAdrian Chadd 	 * We then append a new frame by updating the link pointer
8949be82a42SAdrian Chadd 	 * in that descriptor and then kick TxE here; it will re-read
8959be82a42SAdrian Chadd 	 * that last descriptor and find the new descriptor to transmit.
8969be82a42SAdrian Chadd 	 *
8979be82a42SAdrian Chadd 	 * This is why we keep the holding descriptor around.
8989be82a42SAdrian Chadd 	 */
899b8e788a5SAdrian Chadd 	ath_hal_txstart(ah, txq->axq_qnum);
900b837332dSAdrian Chadd 	ATH_TXQ_UNLOCK(txq);
90103682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 1,
90203682514SAdrian Chadd 	    "ath_tx_handoff: txq=%u, txstart", txq->axq_qnum);
903b8e788a5SAdrian Chadd }
904eb6f0de0SAdrian Chadd 
905eb6f0de0SAdrian Chadd /*
906eb6f0de0SAdrian Chadd  * Restart TX DMA for the given TXQ.
907eb6f0de0SAdrian Chadd  *
908eb6f0de0SAdrian Chadd  * This must be called whether the queue is empty or not.
909eb6f0de0SAdrian Chadd  */
910746bab5bSAdrian Chadd static void
911746bab5bSAdrian Chadd ath_legacy_tx_dma_restart(struct ath_softc *sc, struct ath_txq *txq)
912eb6f0de0SAdrian Chadd {
913b1f3262cSAdrian Chadd 	struct ath_buf *bf, *bf_last;
914eb6f0de0SAdrian Chadd 
915b837332dSAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
916eb6f0de0SAdrian Chadd 
917b1f3262cSAdrian Chadd 	/* XXX make this ATH_TXQ_FIRST */
918eb6f0de0SAdrian Chadd 	bf = TAILQ_FIRST(&txq->axq_q);
919b1f3262cSAdrian Chadd 	bf_last = ATH_TXQ_LAST(txq, axq_q_s);
920b1f3262cSAdrian Chadd 
921eb6f0de0SAdrian Chadd 	if (bf == NULL)
922eb6f0de0SAdrian Chadd 		return;
923eb6f0de0SAdrian Chadd 
9249be82a42SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET,
9259be82a42SAdrian Chadd 	    "%s: Q%d: bf=%p, bf_last=%p, daddr=0x%08x\n",
9269be82a42SAdrian Chadd 	    __func__,
9279be82a42SAdrian Chadd 	    txq->axq_qnum,
9289be82a42SAdrian Chadd 	    bf,
9299be82a42SAdrian Chadd 	    bf_last,
9309be82a42SAdrian Chadd 	    (uint32_t) bf->bf_daddr);
9319be82a42SAdrian Chadd 
9326112d22cSAdrian Chadd #ifdef	ATH_DEBUG
9339be82a42SAdrian Chadd 	if (sc->sc_debug & ATH_DEBUG_RESET)
9349be82a42SAdrian Chadd 		ath_tx_dump(sc, txq);
9356112d22cSAdrian Chadd #endif
9369be82a42SAdrian Chadd 
9379be82a42SAdrian Chadd 	/*
9389be82a42SAdrian Chadd 	 * This is called from a restart, so DMA is known to be
9399be82a42SAdrian Chadd 	 * completely stopped.
9409be82a42SAdrian Chadd 	 */
9419be82a42SAdrian Chadd 	KASSERT((!(txq->axq_flags & ATH_TXQ_PUTRUNNING)),
9429be82a42SAdrian Chadd 	    ("%s: Q%d: called with PUTRUNNING=1\n",
9439be82a42SAdrian Chadd 	    __func__,
9449be82a42SAdrian Chadd 	    txq->axq_qnum));
9459be82a42SAdrian Chadd 
9469be82a42SAdrian Chadd 	ath_hal_puttxbuf(sc->sc_ah, txq->axq_qnum, bf->bf_daddr);
9479be82a42SAdrian Chadd 	txq->axq_flags |= ATH_TXQ_PUTRUNNING;
9489be82a42SAdrian Chadd 
9496112d22cSAdrian Chadd 	ath_hal_gettxdesclinkptr(sc->sc_ah, bf_last->bf_lastds,
9506112d22cSAdrian Chadd 	    &txq->axq_link);
9516112d22cSAdrian Chadd 	ath_hal_txstart(sc->sc_ah, txq->axq_qnum);
952eb6f0de0SAdrian Chadd }
953eb6f0de0SAdrian Chadd 
954eb6f0de0SAdrian Chadd /*
955eb6f0de0SAdrian Chadd  * Hand off a packet to the hardware (or mcast queue.)
956eb6f0de0SAdrian Chadd  *
957eb6f0de0SAdrian Chadd  * The relevant hardware txq should be locked.
958eb6f0de0SAdrian Chadd  */
959eb6f0de0SAdrian Chadd static void
960746bab5bSAdrian Chadd ath_legacy_xmit_handoff(struct ath_softc *sc, struct ath_txq *txq,
961746bab5bSAdrian Chadd     struct ath_buf *bf)
962eb6f0de0SAdrian Chadd {
963375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
964eb6f0de0SAdrian Chadd 
965bb327d28SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
966bb327d28SAdrian Chadd 	if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_EDMA_TXDESC))
967bb327d28SAdrian Chadd 		ath_tx_alq_post(sc, bf);
968bb327d28SAdrian Chadd #endif
969bb327d28SAdrian Chadd 
970eb6f0de0SAdrian Chadd 	if (txq->axq_qnum == ATH_TXQ_SWQ)
971eb6f0de0SAdrian Chadd 		ath_tx_handoff_mcast(sc, txq, bf);
972eb6f0de0SAdrian Chadd 	else
973eb6f0de0SAdrian Chadd 		ath_tx_handoff_hw(sc, txq, bf);
974b8e788a5SAdrian Chadd }
975b8e788a5SAdrian Chadd 
97681a82688SAdrian Chadd static int
97781a82688SAdrian Chadd ath_tx_tag_crypto(struct ath_softc *sc, struct ieee80211_node *ni,
978d4365d16SAdrian Chadd     struct mbuf *m0, int iswep, int isfrag, int *hdrlen, int *pktlen,
979d4365d16SAdrian Chadd     int *keyix)
98081a82688SAdrian Chadd {
98112be5b9cSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_XMIT,
98212be5b9cSAdrian Chadd 	    "%s: hdrlen=%d, pktlen=%d, isfrag=%d, iswep=%d, m0=%p\n",
98312be5b9cSAdrian Chadd 	    __func__,
98412be5b9cSAdrian Chadd 	    *hdrlen,
98512be5b9cSAdrian Chadd 	    *pktlen,
98612be5b9cSAdrian Chadd 	    isfrag,
98712be5b9cSAdrian Chadd 	    iswep,
98812be5b9cSAdrian Chadd 	    m0);
98912be5b9cSAdrian Chadd 
99081a82688SAdrian Chadd 	if (iswep) {
99181a82688SAdrian Chadd 		const struct ieee80211_cipher *cip;
99281a82688SAdrian Chadd 		struct ieee80211_key *k;
99381a82688SAdrian Chadd 
99481a82688SAdrian Chadd 		/*
99581a82688SAdrian Chadd 		 * Construct the 802.11 header+trailer for an encrypted
99681a82688SAdrian Chadd 		 * frame. The only reason this can fail is because of an
99781a82688SAdrian Chadd 		 * unknown or unsupported cipher/key type.
99881a82688SAdrian Chadd 		 */
99981a82688SAdrian Chadd 		k = ieee80211_crypto_encap(ni, m0);
100081a82688SAdrian Chadd 		if (k == NULL) {
100181a82688SAdrian Chadd 			/*
100281a82688SAdrian Chadd 			 * This can happen when the key is yanked after the
100381a82688SAdrian Chadd 			 * frame was queued.  Just discard the frame; the
100481a82688SAdrian Chadd 			 * 802.11 layer counts failures and provides
100581a82688SAdrian Chadd 			 * debugging/diagnostics.
100681a82688SAdrian Chadd 			 */
1007d4365d16SAdrian Chadd 			return (0);
100881a82688SAdrian Chadd 		}
100981a82688SAdrian Chadd 		/*
101081a82688SAdrian Chadd 		 * Adjust the packet + header lengths for the crypto
101181a82688SAdrian Chadd 		 * additions and calculate the h/w key index.  When
101281a82688SAdrian Chadd 		 * a s/w mic is done the frame will have had any mic
101381a82688SAdrian Chadd 		 * added to it prior to entry so m0->m_pkthdr.len will
101481a82688SAdrian Chadd 		 * account for it. Otherwise we need to add it to the
101581a82688SAdrian Chadd 		 * packet length.
101681a82688SAdrian Chadd 		 */
101781a82688SAdrian Chadd 		cip = k->wk_cipher;
101881a82688SAdrian Chadd 		(*hdrlen) += cip->ic_header;
101981a82688SAdrian Chadd 		(*pktlen) += cip->ic_header + cip->ic_trailer;
102081a82688SAdrian Chadd 		/* NB: frags always have any TKIP MIC done in s/w */
102181a82688SAdrian Chadd 		if ((k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && !isfrag)
102281a82688SAdrian Chadd 			(*pktlen) += cip->ic_miclen;
102381a82688SAdrian Chadd 		(*keyix) = k->wk_keyix;
102481a82688SAdrian Chadd 	} else if (ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) {
102581a82688SAdrian Chadd 		/*
102681a82688SAdrian Chadd 		 * Use station key cache slot, if assigned.
102781a82688SAdrian Chadd 		 */
102881a82688SAdrian Chadd 		(*keyix) = ni->ni_ucastkey.wk_keyix;
102981a82688SAdrian Chadd 		if ((*keyix) == IEEE80211_KEYIX_NONE)
103081a82688SAdrian Chadd 			(*keyix) = HAL_TXKEYIX_INVALID;
103181a82688SAdrian Chadd 	} else
103281a82688SAdrian Chadd 		(*keyix) = HAL_TXKEYIX_INVALID;
103381a82688SAdrian Chadd 
1034d4365d16SAdrian Chadd 	return (1);
103581a82688SAdrian Chadd }
103681a82688SAdrian Chadd 
1037e2e4a2c2SAdrian Chadd /*
1038e2e4a2c2SAdrian Chadd  * Calculate whether interoperability protection is required for
1039e2e4a2c2SAdrian Chadd  * this frame.
1040e2e4a2c2SAdrian Chadd  *
1041e2e4a2c2SAdrian Chadd  * This requires the rate control information be filled in,
1042e2e4a2c2SAdrian Chadd  * as the protection requirement depends upon the current
1043e2e4a2c2SAdrian Chadd  * operating mode / PHY.
1044e2e4a2c2SAdrian Chadd  */
1045e2e4a2c2SAdrian Chadd static void
1046e2e4a2c2SAdrian Chadd ath_tx_calc_protection(struct ath_softc *sc, struct ath_buf *bf)
1047e2e4a2c2SAdrian Chadd {
1048e2e4a2c2SAdrian Chadd 	struct ieee80211_frame *wh;
1049e2e4a2c2SAdrian Chadd 	uint8_t rix;
1050e2e4a2c2SAdrian Chadd 	uint16_t flags;
1051e2e4a2c2SAdrian Chadd 	int shortPreamble;
1052e2e4a2c2SAdrian Chadd 	const HAL_RATE_TABLE *rt = sc->sc_currates;
10537a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
1054e2e4a2c2SAdrian Chadd 
1055e2e4a2c2SAdrian Chadd 	flags = bf->bf_state.bfs_txflags;
1056e2e4a2c2SAdrian Chadd 	rix = bf->bf_state.bfs_rc[0].rix;
1057e2e4a2c2SAdrian Chadd 	shortPreamble = bf->bf_state.bfs_shpream;
1058e2e4a2c2SAdrian Chadd 	wh = mtod(bf->bf_m, struct ieee80211_frame *);
1059e2e4a2c2SAdrian Chadd 
10605abc0b25SAdrian Chadd 	/* Disable frame protection for TOA probe frames */
10615abc0b25SAdrian Chadd 	if (bf->bf_flags & ATH_BUF_TOA_PROBE) {
10625abc0b25SAdrian Chadd 		/* XXX count */
10635abc0b25SAdrian Chadd 		flags &= ~(HAL_TXDESC_CTSENA | HAL_TXDESC_RTSENA);
10645abc0b25SAdrian Chadd 		bf->bf_state.bfs_doprot = 0;
10655abc0b25SAdrian Chadd 		goto finish;
10665abc0b25SAdrian Chadd 	}
10675abc0b25SAdrian Chadd 
1068e2e4a2c2SAdrian Chadd 	/*
1069e2e4a2c2SAdrian Chadd 	 * If 802.11g protection is enabled, determine whether
1070e2e4a2c2SAdrian Chadd 	 * to use RTS/CTS or just CTS.  Note that this is only
1071e2e4a2c2SAdrian Chadd 	 * done for OFDM unicast frames.
1072e2e4a2c2SAdrian Chadd 	 */
1073e2e4a2c2SAdrian Chadd 	if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1074e2e4a2c2SAdrian Chadd 	    rt->info[rix].phy == IEEE80211_T_OFDM &&
1075e2e4a2c2SAdrian Chadd 	    (flags & HAL_TXDESC_NOACK) == 0) {
1076e2e4a2c2SAdrian Chadd 		bf->bf_state.bfs_doprot = 1;
1077e2e4a2c2SAdrian Chadd 		/* XXX fragments must use CCK rates w/ protection */
1078e2e4a2c2SAdrian Chadd 		if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) {
1079e2e4a2c2SAdrian Chadd 			flags |= HAL_TXDESC_RTSENA;
1080e2e4a2c2SAdrian Chadd 		} else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) {
1081e2e4a2c2SAdrian Chadd 			flags |= HAL_TXDESC_CTSENA;
1082e2e4a2c2SAdrian Chadd 		}
1083e2e4a2c2SAdrian Chadd 		/*
1084e2e4a2c2SAdrian Chadd 		 * For frags it would be desirable to use the
1085e2e4a2c2SAdrian Chadd 		 * highest CCK rate for RTS/CTS.  But stations
1086e2e4a2c2SAdrian Chadd 		 * farther away may detect it at a lower CCK rate
1087e2e4a2c2SAdrian Chadd 		 * so use the configured protection rate instead
1088e2e4a2c2SAdrian Chadd 		 * (for now).
1089e2e4a2c2SAdrian Chadd 		 */
1090e2e4a2c2SAdrian Chadd 		sc->sc_stats.ast_tx_protect++;
1091e2e4a2c2SAdrian Chadd 	}
1092e2e4a2c2SAdrian Chadd 
1093e2e4a2c2SAdrian Chadd 	/*
1094e2e4a2c2SAdrian Chadd 	 * If 11n protection is enabled and it's a HT frame,
1095e2e4a2c2SAdrian Chadd 	 * enable RTS.
1096e2e4a2c2SAdrian Chadd 	 *
1097e2e4a2c2SAdrian Chadd 	 * XXX ic_htprotmode or ic_curhtprotmode?
1098e2e4a2c2SAdrian Chadd 	 * XXX should it_htprotmode only matter if ic_curhtprotmode
1099e2e4a2c2SAdrian Chadd 	 * XXX indicates it's not a HT pure environment?
1100e2e4a2c2SAdrian Chadd 	 */
1101e2e4a2c2SAdrian Chadd 	if ((ic->ic_htprotmode == IEEE80211_PROT_RTSCTS) &&
1102e2e4a2c2SAdrian Chadd 	    rt->info[rix].phy == IEEE80211_T_HT &&
1103e2e4a2c2SAdrian Chadd 	    (flags & HAL_TXDESC_NOACK) == 0) {
1104e2e4a2c2SAdrian Chadd 		flags |= HAL_TXDESC_RTSENA;
1105e2e4a2c2SAdrian Chadd 		sc->sc_stats.ast_tx_htprotect++;
1106e2e4a2c2SAdrian Chadd 	}
11075abc0b25SAdrian Chadd 
11085abc0b25SAdrian Chadd finish:
1109e2e4a2c2SAdrian Chadd 	bf->bf_state.bfs_txflags = flags;
1110e2e4a2c2SAdrian Chadd }
1111e2e4a2c2SAdrian Chadd 
1112e2e4a2c2SAdrian Chadd /*
1113e2e4a2c2SAdrian Chadd  * Update the frame duration given the currently selected rate.
1114e2e4a2c2SAdrian Chadd  *
1115e2e4a2c2SAdrian Chadd  * This also updates the frame duration value, so it will require
1116e2e4a2c2SAdrian Chadd  * a DMA flush.
1117e2e4a2c2SAdrian Chadd  */
1118e2e4a2c2SAdrian Chadd static void
1119e2e4a2c2SAdrian Chadd ath_tx_calc_duration(struct ath_softc *sc, struct ath_buf *bf)
1120e2e4a2c2SAdrian Chadd {
1121e2e4a2c2SAdrian Chadd 	struct ieee80211_frame *wh;
1122e2e4a2c2SAdrian Chadd 	uint8_t rix;
1123e2e4a2c2SAdrian Chadd 	uint16_t flags;
1124e2e4a2c2SAdrian Chadd 	int shortPreamble;
1125e2e4a2c2SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
1126e2e4a2c2SAdrian Chadd 	const HAL_RATE_TABLE *rt = sc->sc_currates;
1127e2e4a2c2SAdrian Chadd 	int isfrag = bf->bf_m->m_flags & M_FRAG;
1128e2e4a2c2SAdrian Chadd 
1129e2e4a2c2SAdrian Chadd 	flags = bf->bf_state.bfs_txflags;
1130e2e4a2c2SAdrian Chadd 	rix = bf->bf_state.bfs_rc[0].rix;
1131e2e4a2c2SAdrian Chadd 	shortPreamble = bf->bf_state.bfs_shpream;
1132e2e4a2c2SAdrian Chadd 	wh = mtod(bf->bf_m, struct ieee80211_frame *);
1133e2e4a2c2SAdrian Chadd 
1134e2e4a2c2SAdrian Chadd 	/*
1135e2e4a2c2SAdrian Chadd 	 * Calculate duration.  This logically belongs in the 802.11
1136e2e4a2c2SAdrian Chadd 	 * layer but it lacks sufficient information to calculate it.
1137e2e4a2c2SAdrian Chadd 	 */
1138e2e4a2c2SAdrian Chadd 	if ((flags & HAL_TXDESC_NOACK) == 0 &&
1139e2e4a2c2SAdrian Chadd 	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL) {
1140e2e4a2c2SAdrian Chadd 		u_int16_t dur;
1141e2e4a2c2SAdrian Chadd 		if (shortPreamble)
1142e2e4a2c2SAdrian Chadd 			dur = rt->info[rix].spAckDuration;
1143e2e4a2c2SAdrian Chadd 		else
1144e2e4a2c2SAdrian Chadd 			dur = rt->info[rix].lpAckDuration;
1145e2e4a2c2SAdrian Chadd 		if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) {
1146e2e4a2c2SAdrian Chadd 			dur += dur;		/* additional SIFS+ACK */
1147e2e4a2c2SAdrian Chadd 			/*
1148e2e4a2c2SAdrian Chadd 			 * Include the size of next fragment so NAV is
1149e2e4a2c2SAdrian Chadd 			 * updated properly.  The last fragment uses only
1150e2e4a2c2SAdrian Chadd 			 * the ACK duration
11519572684aSAdrian Chadd 			 *
11529572684aSAdrian Chadd 			 * XXX TODO: ensure that the rate lookup for each
11539572684aSAdrian Chadd 			 * fragment is the same as the rate used by the
11549572684aSAdrian Chadd 			 * first fragment!
1155e2e4a2c2SAdrian Chadd 			 */
1156cd7dffd0SAdrian Chadd 			dur += ath_hal_computetxtime(ah,
1157cd7dffd0SAdrian Chadd 			    rt,
1158cd7dffd0SAdrian Chadd 			    bf->bf_nextfraglen,
11597ff1939dSAdrian Chadd 			    rix, shortPreamble,
11607ff1939dSAdrian Chadd 			    AH_TRUE);
1161e2e4a2c2SAdrian Chadd 		}
1162e2e4a2c2SAdrian Chadd 		if (isfrag) {
1163e2e4a2c2SAdrian Chadd 			/*
1164e2e4a2c2SAdrian Chadd 			 * Force hardware to use computed duration for next
1165e2e4a2c2SAdrian Chadd 			 * fragment by disabling multi-rate retry which updates
1166e2e4a2c2SAdrian Chadd 			 * duration based on the multi-rate duration table.
1167e2e4a2c2SAdrian Chadd 			 */
1168e2e4a2c2SAdrian Chadd 			bf->bf_state.bfs_ismrr = 0;
1169e2e4a2c2SAdrian Chadd 			bf->bf_state.bfs_try0 = ATH_TXMGTTRY;
1170e2e4a2c2SAdrian Chadd 			/* XXX update bfs_rc[0].try? */
1171e2e4a2c2SAdrian Chadd 		}
1172e2e4a2c2SAdrian Chadd 
1173e2e4a2c2SAdrian Chadd 		/* Update the duration field itself */
1174e2e4a2c2SAdrian Chadd 		*(u_int16_t *)wh->i_dur = htole16(dur);
1175e2e4a2c2SAdrian Chadd 	}
1176e2e4a2c2SAdrian Chadd }
1177e2e4a2c2SAdrian Chadd 
1178e42b5dbaSAdrian Chadd static uint8_t
1179e42b5dbaSAdrian Chadd ath_tx_get_rtscts_rate(struct ath_hal *ah, const HAL_RATE_TABLE *rt,
1180eb6f0de0SAdrian Chadd     int cix, int shortPreamble)
118179f02dbfSAdrian Chadd {
1182e42b5dbaSAdrian Chadd 	uint8_t ctsrate;
1183e42b5dbaSAdrian Chadd 
118479f02dbfSAdrian Chadd 	/*
118579f02dbfSAdrian Chadd 	 * CTS transmit rate is derived from the transmit rate
118679f02dbfSAdrian Chadd 	 * by looking in the h/w rate table.  We must also factor
118779f02dbfSAdrian Chadd 	 * in whether or not a short preamble is to be used.
118879f02dbfSAdrian Chadd 	 */
118979f02dbfSAdrian Chadd 	/* NB: cix is set above where RTS/CTS is enabled */
119079f02dbfSAdrian Chadd 	KASSERT(cix != 0xff, ("cix not setup"));
1191e42b5dbaSAdrian Chadd 	ctsrate = rt->info[cix].rateCode;
1192e42b5dbaSAdrian Chadd 
1193e42b5dbaSAdrian Chadd 	/* XXX this should only matter for legacy rates */
1194e42b5dbaSAdrian Chadd 	if (shortPreamble)
1195e42b5dbaSAdrian Chadd 		ctsrate |= rt->info[cix].shortPreamble;
1196e42b5dbaSAdrian Chadd 
1197d4365d16SAdrian Chadd 	return (ctsrate);
1198e42b5dbaSAdrian Chadd }
1199e42b5dbaSAdrian Chadd 
1200e42b5dbaSAdrian Chadd /*
1201e42b5dbaSAdrian Chadd  * Calculate the RTS/CTS duration for legacy frames.
1202e42b5dbaSAdrian Chadd  */
1203e42b5dbaSAdrian Chadd static int
1204e42b5dbaSAdrian Chadd ath_tx_calc_ctsduration(struct ath_hal *ah, int rix, int cix,
1205e42b5dbaSAdrian Chadd     int shortPreamble, int pktlen, const HAL_RATE_TABLE *rt,
1206e42b5dbaSAdrian Chadd     int flags)
1207e42b5dbaSAdrian Chadd {
1208e42b5dbaSAdrian Chadd 	int ctsduration = 0;
1209e42b5dbaSAdrian Chadd 
1210e42b5dbaSAdrian Chadd 	/* This mustn't be called for HT modes */
1211e42b5dbaSAdrian Chadd 	if (rt->info[cix].phy == IEEE80211_T_HT) {
1212e42b5dbaSAdrian Chadd 		printf("%s: HT rate where it shouldn't be (0x%x)\n",
1213e42b5dbaSAdrian Chadd 		    __func__, rt->info[cix].rateCode);
1214d4365d16SAdrian Chadd 		return (-1);
1215e42b5dbaSAdrian Chadd 	}
1216e42b5dbaSAdrian Chadd 
121779f02dbfSAdrian Chadd 	/*
121879f02dbfSAdrian Chadd 	 * Compute the transmit duration based on the frame
121979f02dbfSAdrian Chadd 	 * size and the size of an ACK frame.  We call into the
122079f02dbfSAdrian Chadd 	 * HAL to do the computation since it depends on the
122179f02dbfSAdrian Chadd 	 * characteristics of the actual PHY being used.
122279f02dbfSAdrian Chadd 	 *
122379f02dbfSAdrian Chadd 	 * NB: CTS is assumed the same size as an ACK so we can
122479f02dbfSAdrian Chadd 	 *     use the precalculated ACK durations.
122579f02dbfSAdrian Chadd 	 */
122679f02dbfSAdrian Chadd 	if (shortPreamble) {
122779f02dbfSAdrian Chadd 		if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
1228e42b5dbaSAdrian Chadd 			ctsduration += rt->info[cix].spAckDuration;
1229e42b5dbaSAdrian Chadd 		ctsduration += ath_hal_computetxtime(ah,
12307ff1939dSAdrian Chadd 			rt, pktlen, rix, AH_TRUE, AH_TRUE);
123179f02dbfSAdrian Chadd 		if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
1232e42b5dbaSAdrian Chadd 			ctsduration += rt->info[rix].spAckDuration;
123379f02dbfSAdrian Chadd 	} else {
123479f02dbfSAdrian Chadd 		if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
1235e42b5dbaSAdrian Chadd 			ctsduration += rt->info[cix].lpAckDuration;
1236e42b5dbaSAdrian Chadd 		ctsduration += ath_hal_computetxtime(ah,
12377ff1939dSAdrian Chadd 			rt, pktlen, rix, AH_FALSE, AH_TRUE);
123879f02dbfSAdrian Chadd 		if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
1239e42b5dbaSAdrian Chadd 			ctsduration += rt->info[rix].lpAckDuration;
124079f02dbfSAdrian Chadd 	}
1241e42b5dbaSAdrian Chadd 
1242d4365d16SAdrian Chadd 	return (ctsduration);
124379f02dbfSAdrian Chadd }
124479f02dbfSAdrian Chadd 
1245eb6f0de0SAdrian Chadd /*
1246eb6f0de0SAdrian Chadd  * Update the given ath_buf with updated rts/cts setup and duration
1247eb6f0de0SAdrian Chadd  * values.
1248eb6f0de0SAdrian Chadd  *
1249eb6f0de0SAdrian Chadd  * To support rate lookups for each software retry, the rts/cts rate
1250eb6f0de0SAdrian Chadd  * and cts duration must be re-calculated.
1251eb6f0de0SAdrian Chadd  *
1252eb6f0de0SAdrian Chadd  * This function assumes the RTS/CTS flags have been set as needed;
1253eb6f0de0SAdrian Chadd  * mrr has been disabled; and the rate control lookup has been done.
1254eb6f0de0SAdrian Chadd  *
1255eb6f0de0SAdrian Chadd  * XXX TODO: MRR need only be disabled for the pre-11n NICs.
1256eb6f0de0SAdrian Chadd  * XXX The 11n NICs support per-rate RTS/CTS configuration.
1257eb6f0de0SAdrian Chadd  */
1258eb6f0de0SAdrian Chadd static void
1259eb6f0de0SAdrian Chadd ath_tx_set_rtscts(struct ath_softc *sc, struct ath_buf *bf)
1260eb6f0de0SAdrian Chadd {
1261eb6f0de0SAdrian Chadd 	uint16_t ctsduration = 0;
1262eb6f0de0SAdrian Chadd 	uint8_t ctsrate = 0;
1263eb6f0de0SAdrian Chadd 	uint8_t rix = bf->bf_state.bfs_rc[0].rix;
1264eb6f0de0SAdrian Chadd 	uint8_t cix = 0;
1265eb6f0de0SAdrian Chadd 	const HAL_RATE_TABLE *rt = sc->sc_currates;
1266eb6f0de0SAdrian Chadd 
1267eb6f0de0SAdrian Chadd 	/*
1268eb6f0de0SAdrian Chadd 	 * No RTS/CTS enabled? Don't bother.
1269eb6f0de0SAdrian Chadd 	 */
1270875a9451SAdrian Chadd 	if ((bf->bf_state.bfs_txflags &
1271eb6f0de0SAdrian Chadd 	    (HAL_TXDESC_RTSENA | HAL_TXDESC_CTSENA)) == 0) {
1272eb6f0de0SAdrian Chadd 		/* XXX is this really needed? */
1273eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_ctsrate = 0;
1274eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_ctsduration = 0;
1275eb6f0de0SAdrian Chadd 		return;
1276eb6f0de0SAdrian Chadd 	}
1277eb6f0de0SAdrian Chadd 
1278eb6f0de0SAdrian Chadd 	/*
1279eb6f0de0SAdrian Chadd 	 * If protection is enabled, use the protection rix control
1280eb6f0de0SAdrian Chadd 	 * rate. Otherwise use the rate0 control rate.
1281eb6f0de0SAdrian Chadd 	 */
1282eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_doprot)
1283eb6f0de0SAdrian Chadd 		rix = sc->sc_protrix;
1284eb6f0de0SAdrian Chadd 	else
1285eb6f0de0SAdrian Chadd 		rix = bf->bf_state.bfs_rc[0].rix;
1286eb6f0de0SAdrian Chadd 
1287eb6f0de0SAdrian Chadd 	/*
1288eb6f0de0SAdrian Chadd 	 * If the raw path has hard-coded ctsrate0 to something,
1289eb6f0de0SAdrian Chadd 	 * use it.
1290eb6f0de0SAdrian Chadd 	 */
1291eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_ctsrate0 != 0)
1292eb6f0de0SAdrian Chadd 		cix = ath_tx_findrix(sc, bf->bf_state.bfs_ctsrate0);
1293eb6f0de0SAdrian Chadd 	else
1294eb6f0de0SAdrian Chadd 		/* Control rate from above */
1295eb6f0de0SAdrian Chadd 		cix = rt->info[rix].controlRate;
1296eb6f0de0SAdrian Chadd 
1297eb6f0de0SAdrian Chadd 	/* Calculate the rtscts rate for the given cix */
1298eb6f0de0SAdrian Chadd 	ctsrate = ath_tx_get_rtscts_rate(sc->sc_ah, rt, cix,
1299eb6f0de0SAdrian Chadd 	    bf->bf_state.bfs_shpream);
1300eb6f0de0SAdrian Chadd 
1301eb6f0de0SAdrian Chadd 	/* The 11n chipsets do ctsduration calculations for you */
1302eb6f0de0SAdrian Chadd 	if (! ath_tx_is_11n(sc))
1303eb6f0de0SAdrian Chadd 		ctsduration = ath_tx_calc_ctsduration(sc->sc_ah, rix, cix,
1304eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_shpream, bf->bf_state.bfs_pktlen,
1305875a9451SAdrian Chadd 		    rt, bf->bf_state.bfs_txflags);
1306eb6f0de0SAdrian Chadd 
1307eb6f0de0SAdrian Chadd 	/* Squirrel away in ath_buf */
1308eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsrate = ctsrate;
1309eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsduration = ctsduration;
1310eb6f0de0SAdrian Chadd 
1311eb6f0de0SAdrian Chadd 	/*
1312eb6f0de0SAdrian Chadd 	 * Must disable multi-rate retry when using RTS/CTS.
1313eb6f0de0SAdrian Chadd 	 */
1314af017101SAdrian Chadd 	if (!sc->sc_mrrprot) {
1315eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_ismrr = 0;
1316eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_try0 =
1317eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_rc[0].tries = ATH_TXMGTTRY; /* XXX ew */
1318eb6f0de0SAdrian Chadd 	}
1319af017101SAdrian Chadd }
1320eb6f0de0SAdrian Chadd 
1321eb6f0de0SAdrian Chadd /*
1322eb6f0de0SAdrian Chadd  * Setup the descriptor chain for a normal or fast-frame
1323eb6f0de0SAdrian Chadd  * frame.
132446634305SAdrian Chadd  *
132546634305SAdrian Chadd  * XXX TODO: extend to include the destination hardware QCU ID.
132646634305SAdrian Chadd  * Make sure that is correct.  Make sure that when being added
132746634305SAdrian Chadd  * to the mcastq, the CABQ QCUID is set or things will get a bit
132846634305SAdrian Chadd  * odd.
1329eb6f0de0SAdrian Chadd  */
1330eb6f0de0SAdrian Chadd static void
1331eb6f0de0SAdrian Chadd ath_tx_setds(struct ath_softc *sc, struct ath_buf *bf)
1332eb6f0de0SAdrian Chadd {
1333eb6f0de0SAdrian Chadd 	struct ath_desc *ds = bf->bf_desc;
1334eb6f0de0SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
1335eb6f0de0SAdrian Chadd 
13367d9dd2acSAdrian Chadd 	if (bf->bf_state.bfs_txrate0 == 0)
133783bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_XMIT,
133883bbd5ebSRui Paulo 		    "%s: bf=%p, txrate0=%d\n", __func__, bf, 0);
13397d9dd2acSAdrian Chadd 
1340eb6f0de0SAdrian Chadd 	ath_hal_setuptxdesc(ah, ds
1341eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_pktlen	/* packet length */
1342eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_hdrlen	/* header length */
1343eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_atype	/* Atheros packet type */
1344eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_txpower	/* txpower */
1345eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_txrate0
1346eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_try0		/* series 0 rate/tries */
1347eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_keyix	/* key cache index */
1348eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_txantenna	/* antenna mode */
1349875a9451SAdrian Chadd 		, bf->bf_state.bfs_txflags	/* flags */
1350eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_ctsrate	/* rts/cts rate */
1351eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_ctsduration	/* rts/cts duration */
1352eb6f0de0SAdrian Chadd 	);
1353eb6f0de0SAdrian Chadd 
1354eb6f0de0SAdrian Chadd 	/*
1355eb6f0de0SAdrian Chadd 	 * This will be overriden when the descriptor chain is written.
1356eb6f0de0SAdrian Chadd 	 */
1357eb6f0de0SAdrian Chadd 	bf->bf_lastds = ds;
1358eb6f0de0SAdrian Chadd 	bf->bf_last = bf;
1359eb6f0de0SAdrian Chadd 
1360d34a7347SAdrian Chadd 	/* Set rate control and descriptor chain for this frame */
1361d34a7347SAdrian Chadd 	ath_tx_set_ratectrl(sc, bf->bf_node, bf);
13626e84772fSAdrian Chadd 	ath_tx_chaindesclist(sc, ds, bf, 0, 0, 0);
1363eb6f0de0SAdrian Chadd }
1364eb6f0de0SAdrian Chadd 
1365eb6f0de0SAdrian Chadd /*
1366eb6f0de0SAdrian Chadd  * Do a rate lookup.
1367eb6f0de0SAdrian Chadd  *
1368eb6f0de0SAdrian Chadd  * This performs a rate lookup for the given ath_buf only if it's required.
1369eb6f0de0SAdrian Chadd  * Non-data frames and raw frames don't require it.
1370eb6f0de0SAdrian Chadd  *
1371eb6f0de0SAdrian Chadd  * This populates the primary and MRR entries; MRR values are
1372eb6f0de0SAdrian Chadd  * then disabled later on if something requires it (eg RTS/CTS on
1373eb6f0de0SAdrian Chadd  * pre-11n chipsets.
1374eb6f0de0SAdrian Chadd  *
1375eb6f0de0SAdrian Chadd  * This needs to be done before the RTS/CTS fields are calculated
1376eb6f0de0SAdrian Chadd  * as they may depend upon the rate chosen.
1377eb6f0de0SAdrian Chadd  */
1378eb6f0de0SAdrian Chadd static void
137984f950a5SAdrian Chadd ath_tx_do_ratelookup(struct ath_softc *sc, struct ath_buf *bf, int tid,
1380*cce63444SAdrian Chadd     int pktlen, int is_aggr)
1381eb6f0de0SAdrian Chadd {
1382eb6f0de0SAdrian Chadd 	uint8_t rate, rix;
1383eb6f0de0SAdrian Chadd 	int try0;
138484f950a5SAdrian Chadd 	int maxdur; // Note: Unused for now
1385*cce63444SAdrian Chadd 	int maxpktlen;
1386eb6f0de0SAdrian Chadd 
1387eb6f0de0SAdrian Chadd 	if (! bf->bf_state.bfs_doratelookup)
1388eb6f0de0SAdrian Chadd 		return;
1389eb6f0de0SAdrian Chadd 
1390eb6f0de0SAdrian Chadd 	/* Get rid of any previous state */
1391eb6f0de0SAdrian Chadd 	bzero(bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
1392eb6f0de0SAdrian Chadd 
1393eb6f0de0SAdrian Chadd 	ATH_NODE_LOCK(ATH_NODE(bf->bf_node));
1394eb6f0de0SAdrian Chadd 	ath_rate_findrate(sc, ATH_NODE(bf->bf_node), bf->bf_state.bfs_shpream,
1395*cce63444SAdrian Chadd 	    pktlen, tid, is_aggr, &rix, &try0, &rate, &maxdur, &maxpktlen);
1396eb6f0de0SAdrian Chadd 
1397eb6f0de0SAdrian Chadd 	/* In case MRR is disabled, make sure rc[0] is setup correctly */
1398eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].rix = rix;
1399eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].ratecode = rate;
1400eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].tries = try0;
1401eb6f0de0SAdrian Chadd 
1402eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_ismrr && try0 != ATH_TXMAXTRY)
1403eb6f0de0SAdrian Chadd 		ath_rate_getxtxrates(sc, ATH_NODE(bf->bf_node), rix,
1404eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_rc);
1405eb6f0de0SAdrian Chadd 	ATH_NODE_UNLOCK(ATH_NODE(bf->bf_node));
1406eb6f0de0SAdrian Chadd 
1407eb6f0de0SAdrian Chadd 	sc->sc_txrix = rix;	/* for LED blinking */
1408eb6f0de0SAdrian Chadd 	sc->sc_lastdatarix = rix;	/* for fast frames */
1409eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_try0 = try0;
1410eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txrate0 = rate;
1411*cce63444SAdrian Chadd 	bf->bf_state.bfs_rc_maxpktlen = maxpktlen;
1412eb6f0de0SAdrian Chadd }
1413eb6f0de0SAdrian Chadd 
1414eb6f0de0SAdrian Chadd /*
14150c54de88SAdrian Chadd  * Update the CLRDMASK bit in the ath_buf if it needs to be set.
14160c54de88SAdrian Chadd  */
14170c54de88SAdrian Chadd static void
14180c54de88SAdrian Chadd ath_tx_update_clrdmask(struct ath_softc *sc, struct ath_tid *tid,
14190c54de88SAdrian Chadd     struct ath_buf *bf)
14200c54de88SAdrian Chadd {
14214f25ddbbSAdrian Chadd 	struct ath_node *an = ATH_NODE(bf->bf_node);
14220c54de88SAdrian Chadd 
1423375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
14240c54de88SAdrian Chadd 
14254f25ddbbSAdrian Chadd 	if (an->clrdmask == 1) {
14260c54de88SAdrian Chadd 		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
14274f25ddbbSAdrian Chadd 		an->clrdmask = 0;
14280c54de88SAdrian Chadd 	}
14290c54de88SAdrian Chadd }
14300c54de88SAdrian Chadd 
14310c54de88SAdrian Chadd /*
143222a3aee6SAdrian Chadd  * Return whether this frame should be software queued or
143322a3aee6SAdrian Chadd  * direct dispatched.
143422a3aee6SAdrian Chadd  *
143522a3aee6SAdrian Chadd  * When doing powersave, BAR frames should be queued but other management
143622a3aee6SAdrian Chadd  * frames should be directly sent.
143722a3aee6SAdrian Chadd  *
143822a3aee6SAdrian Chadd  * When not doing powersave, stick BAR frames into the hardware queue
143922a3aee6SAdrian Chadd  * so it goes out even though the queue is paused.
144022a3aee6SAdrian Chadd  *
144122a3aee6SAdrian Chadd  * For now, management frames are also software queued by default.
144222a3aee6SAdrian Chadd  */
144322a3aee6SAdrian Chadd static int
144422a3aee6SAdrian Chadd ath_tx_should_swq_frame(struct ath_softc *sc, struct ath_node *an,
144522a3aee6SAdrian Chadd     struct mbuf *m0, int *queue_to_head)
144622a3aee6SAdrian Chadd {
144722a3aee6SAdrian Chadd 	struct ieee80211_node *ni = &an->an_node;
144822a3aee6SAdrian Chadd 	struct ieee80211_frame *wh;
144922a3aee6SAdrian Chadd 	uint8_t type, subtype;
145022a3aee6SAdrian Chadd 
145122a3aee6SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
145222a3aee6SAdrian Chadd 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
145322a3aee6SAdrian Chadd 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
145422a3aee6SAdrian Chadd 
145522a3aee6SAdrian Chadd 	(*queue_to_head) = 0;
145622a3aee6SAdrian Chadd 
145722a3aee6SAdrian Chadd 	/* If it's not in powersave - direct-dispatch BAR */
145822a3aee6SAdrian Chadd 	if ((ATH_NODE(ni)->an_is_powersave == 0)
145922a3aee6SAdrian Chadd 	    && type == IEEE80211_FC0_TYPE_CTL &&
146022a3aee6SAdrian Chadd 	    subtype == IEEE80211_FC0_SUBTYPE_BAR) {
146122a3aee6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX,
146222a3aee6SAdrian Chadd 		    "%s: BAR: TX'ing direct\n", __func__);
146322a3aee6SAdrian Chadd 		return (0);
146422a3aee6SAdrian Chadd 	} else if ((ATH_NODE(ni)->an_is_powersave == 1)
146522a3aee6SAdrian Chadd 	    && type == IEEE80211_FC0_TYPE_CTL &&
146622a3aee6SAdrian Chadd 	    subtype == IEEE80211_FC0_SUBTYPE_BAR) {
146722a3aee6SAdrian Chadd 		/* BAR TX whilst asleep; queue */
146822a3aee6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX,
146922a3aee6SAdrian Chadd 		    "%s: swq: TX'ing\n", __func__);
147022a3aee6SAdrian Chadd 		(*queue_to_head) = 1;
147122a3aee6SAdrian Chadd 		return (1);
147222a3aee6SAdrian Chadd 	} else if ((ATH_NODE(ni)->an_is_powersave == 1)
147322a3aee6SAdrian Chadd 	    && (type == IEEE80211_FC0_TYPE_MGT ||
147422a3aee6SAdrian Chadd 	        type == IEEE80211_FC0_TYPE_CTL)) {
147522a3aee6SAdrian Chadd 		/*
147622a3aee6SAdrian Chadd 		 * Other control/mgmt frame; bypass software queuing
147722a3aee6SAdrian Chadd 		 * for now!
147822a3aee6SAdrian Chadd 		 */
147983bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_XMIT,
148022a3aee6SAdrian Chadd 		    "%s: %6D: Node is asleep; sending mgmt "
148122a3aee6SAdrian Chadd 		    "(type=%d, subtype=%d)\n",
148283bbd5ebSRui Paulo 		    __func__, ni->ni_macaddr, ":", type, subtype);
148322a3aee6SAdrian Chadd 		return (0);
148422a3aee6SAdrian Chadd 	} else {
148522a3aee6SAdrian Chadd 		return (1);
148622a3aee6SAdrian Chadd 	}
148722a3aee6SAdrian Chadd }
148822a3aee6SAdrian Chadd 
148922a3aee6SAdrian Chadd 
149022a3aee6SAdrian Chadd /*
1491eb6f0de0SAdrian Chadd  * Transmit the given frame to the hardware.
1492eb6f0de0SAdrian Chadd  *
1493eb6f0de0SAdrian Chadd  * The frame must already be setup; rate control must already have
1494eb6f0de0SAdrian Chadd  * been done.
1495eb6f0de0SAdrian Chadd  *
1496eb6f0de0SAdrian Chadd  * XXX since the TXQ lock is being held here (and I dislike holding
1497eb6f0de0SAdrian Chadd  * it for this long when not doing software aggregation), later on
1498eb6f0de0SAdrian Chadd  * break this function into "setup_normal" and "xmit_normal". The
1499eb6f0de0SAdrian Chadd  * lock only needs to be held for the ath_tx_handoff call.
150022a3aee6SAdrian Chadd  *
150122a3aee6SAdrian Chadd  * XXX we don't update the leak count here - if we're doing
150222a3aee6SAdrian Chadd  * direct frame dispatch, we need to be able to do it without
150322a3aee6SAdrian Chadd  * decrementing the leak count (eg multicast queue frames.)
1504eb6f0de0SAdrian Chadd  */
1505eb6f0de0SAdrian Chadd static void
1506eb6f0de0SAdrian Chadd ath_tx_xmit_normal(struct ath_softc *sc, struct ath_txq *txq,
1507eb6f0de0SAdrian Chadd     struct ath_buf *bf)
1508eb6f0de0SAdrian Chadd {
15090c54de88SAdrian Chadd 	struct ath_node *an = ATH_NODE(bf->bf_node);
15100c54de88SAdrian Chadd 	struct ath_tid *tid = &an->an_tid[bf->bf_state.bfs_tid];
1511eb6f0de0SAdrian Chadd 
1512375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
1513eb6f0de0SAdrian Chadd 
15140c54de88SAdrian Chadd 	/*
15150c54de88SAdrian Chadd 	 * For now, just enable CLRDMASK. ath_tx_xmit_normal() does
15160c54de88SAdrian Chadd 	 * set a completion handler however it doesn't (yet) properly
15170c54de88SAdrian Chadd 	 * handle the strict ordering requirements needed for normal,
15180c54de88SAdrian Chadd 	 * non-aggregate session frames.
15190c54de88SAdrian Chadd 	 *
15200c54de88SAdrian Chadd 	 * Once this is implemented, only set CLRDMASK like this for
15210c54de88SAdrian Chadd 	 * frames that must go out - eg management/raw frames.
15220c54de88SAdrian Chadd 	 */
15230c54de88SAdrian Chadd 	bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
15240c54de88SAdrian Chadd 
1525eb6f0de0SAdrian Chadd 	/* Setup the descriptor before handoff */
1526*cce63444SAdrian Chadd 	ath_tx_do_ratelookup(sc, bf, tid->tid, bf->bf_state.bfs_pktlen, false);
1527e2e4a2c2SAdrian Chadd 	ath_tx_calc_duration(sc, bf);
1528e2e4a2c2SAdrian Chadd 	ath_tx_calc_protection(sc, bf);
1529eb6f0de0SAdrian Chadd 	ath_tx_set_rtscts(sc, bf);
1530e2e4a2c2SAdrian Chadd 	ath_tx_rate_fill_rcflags(sc, bf);
1531eb6f0de0SAdrian Chadd 	ath_tx_setds(sc, bf);
1532eb6f0de0SAdrian Chadd 
15330c54de88SAdrian Chadd 	/* Track per-TID hardware queue depth correctly */
15340c54de88SAdrian Chadd 	tid->hwq_depth++;
15350c54de88SAdrian Chadd 
15360c54de88SAdrian Chadd 	/* Assign the completion handler */
15370c54de88SAdrian Chadd 	bf->bf_comp = ath_tx_normal_comp;
15384e81f27cSAdrian Chadd 
1539eb6f0de0SAdrian Chadd 	/* Hand off to hardware */
1540eb6f0de0SAdrian Chadd 	ath_tx_handoff(sc, txq, bf);
1541eb6f0de0SAdrian Chadd }
1542eb6f0de0SAdrian Chadd 
1543d05b576dSAdrian Chadd /*
1544d05b576dSAdrian Chadd  * Do the basic frame setup stuff that's required before the frame
1545d05b576dSAdrian Chadd  * is added to a software queue.
1546d05b576dSAdrian Chadd  *
1547d05b576dSAdrian Chadd  * All frames get mostly the same treatment and it's done once.
1548d05b576dSAdrian Chadd  * Retransmits fiddle with things like the rate control setup,
1549d05b576dSAdrian Chadd  * setting the retransmit bit in the packet; doing relevant DMA/bus
1550d05b576dSAdrian Chadd  * syncing and relinking it (back) into the hardware TX queue.
1551d05b576dSAdrian Chadd  *
1552d05b576dSAdrian Chadd  * Note that this may cause the mbuf to be reallocated, so
1553d05b576dSAdrian Chadd  * m0 may not be valid.
1554d05b576dSAdrian Chadd  */
1555eb6f0de0SAdrian Chadd static int
1556eb6f0de0SAdrian Chadd ath_tx_normal_setup(struct ath_softc *sc, struct ieee80211_node *ni,
1557b43facbfSAdrian Chadd     struct ath_buf *bf, struct mbuf *m0, struct ath_txq *txq)
1558b8e788a5SAdrian Chadd {
1559b8e788a5SAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
15607a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
1561b8e788a5SAdrian Chadd 	int error, iswep, ismcast, isfrag, ismrr;
1562eb6f0de0SAdrian Chadd 	int keyix, hdrlen, pktlen, try0 = 0;
1563eb6f0de0SAdrian Chadd 	u_int8_t rix = 0, txrate = 0;
1564b8e788a5SAdrian Chadd 	struct ath_desc *ds;
1565b8e788a5SAdrian Chadd 	struct ieee80211_frame *wh;
1566eb6f0de0SAdrian Chadd 	u_int subtype, flags;
1567b8e788a5SAdrian Chadd 	HAL_PKT_TYPE atype;
1568b8e788a5SAdrian Chadd 	const HAL_RATE_TABLE *rt;
1569b8e788a5SAdrian Chadd 	HAL_BOOL shortPreamble;
1570b8e788a5SAdrian Chadd 	struct ath_node *an;
157115e58d4dSAdrian Chadd 
157215e58d4dSAdrian Chadd 	/* XXX TODO: this pri is only used for non-QoS check, right? */
1573b8e788a5SAdrian Chadd 	u_int pri;
1574b8e788a5SAdrian Chadd 
15757561cb5cSAdrian Chadd 	/*
15767561cb5cSAdrian Chadd 	 * To ensure that both sequence numbers and the CCMP PN handling
15777561cb5cSAdrian Chadd 	 * is "correct", make sure that the relevant TID queue is locked.
15787561cb5cSAdrian Chadd 	 * Otherwise the CCMP PN and seqno may appear out of order, causing
15797561cb5cSAdrian Chadd 	 * re-ordered frames to have out of order CCMP PN's, resulting
15807561cb5cSAdrian Chadd 	 * in many, many frame drops.
15817561cb5cSAdrian Chadd 	 */
1582375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
15837561cb5cSAdrian Chadd 
1584b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
15855945b5f5SKevin Lo 	iswep = wh->i_fc[1] & IEEE80211_FC1_PROTECTED;
1586b8e788a5SAdrian Chadd 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1587b8e788a5SAdrian Chadd 	isfrag = m0->m_flags & M_FRAG;
1588b8e788a5SAdrian Chadd 	hdrlen = ieee80211_anyhdrsize(wh);
1589b8e788a5SAdrian Chadd 	/*
1590b8e788a5SAdrian Chadd 	 * Packet length must not include any
1591b8e788a5SAdrian Chadd 	 * pad bytes; deduct them here.
1592b8e788a5SAdrian Chadd 	 */
1593b8e788a5SAdrian Chadd 	pktlen = m0->m_pkthdr.len - (hdrlen & 3);
1594b8e788a5SAdrian Chadd 
159581a82688SAdrian Chadd 	/* Handle encryption twiddling if needed */
1596eb6f0de0SAdrian Chadd 	if (! ath_tx_tag_crypto(sc, ni, m0, iswep, isfrag, &hdrlen,
1597eb6f0de0SAdrian Chadd 	    &pktlen, &keyix)) {
1598d07be335SAdrian Chadd 		ieee80211_free_mbuf(m0);
1599b8e788a5SAdrian Chadd 		return EIO;
1600b8e788a5SAdrian Chadd 	}
1601b8e788a5SAdrian Chadd 
1602b8e788a5SAdrian Chadd 	/* packet header may have moved, reset our local pointer */
1603b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1604b8e788a5SAdrian Chadd 
1605b8e788a5SAdrian Chadd 	pktlen += IEEE80211_CRC_LEN;
1606b8e788a5SAdrian Chadd 
1607b8e788a5SAdrian Chadd 	/*
1608b8e788a5SAdrian Chadd 	 * Load the DMA map so any coalescing is done.  This
1609b8e788a5SAdrian Chadd 	 * also calculates the number of descriptors we need.
1610b8e788a5SAdrian Chadd 	 */
1611b8e788a5SAdrian Chadd 	error = ath_tx_dmasetup(sc, bf, m0);
1612b8e788a5SAdrian Chadd 	if (error != 0)
1613b8e788a5SAdrian Chadd 		return error;
1614f5c30c4eSAdrian Chadd 	KASSERT((ni != NULL), ("%s: ni=NULL!", __func__));
1615b8e788a5SAdrian Chadd 	bf->bf_node = ni;			/* NB: held reference */
1616b8e788a5SAdrian Chadd 	m0 = bf->bf_m;				/* NB: may have changed */
1617b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1618b8e788a5SAdrian Chadd 
1619b8e788a5SAdrian Chadd 	/* setup descriptors */
1620b8e788a5SAdrian Chadd 	ds = bf->bf_desc;
1621b8e788a5SAdrian Chadd 	rt = sc->sc_currates;
1622b8e788a5SAdrian Chadd 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
1623b8e788a5SAdrian Chadd 
1624b8e788a5SAdrian Chadd 	/*
1625b8e788a5SAdrian Chadd 	 * NB: the 802.11 layer marks whether or not we should
1626b8e788a5SAdrian Chadd 	 * use short preamble based on the current mode and
1627b8e788a5SAdrian Chadd 	 * negotiated parameters.
1628b8e788a5SAdrian Chadd 	 */
1629b8e788a5SAdrian Chadd 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1630b8e788a5SAdrian Chadd 	    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
1631b8e788a5SAdrian Chadd 		shortPreamble = AH_TRUE;
1632b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_shortpre++;
1633b8e788a5SAdrian Chadd 	} else {
1634b8e788a5SAdrian Chadd 		shortPreamble = AH_FALSE;
1635b8e788a5SAdrian Chadd 	}
1636b8e788a5SAdrian Chadd 
1637b8e788a5SAdrian Chadd 	an = ATH_NODE(ni);
16384e81f27cSAdrian Chadd 	//flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for crypto errs */
16394e81f27cSAdrian Chadd 	flags = 0;
1640b8e788a5SAdrian Chadd 	ismrr = 0;				/* default no multi-rate retry*/
164115e58d4dSAdrian Chadd 
164215e58d4dSAdrian Chadd 	pri = ath_tx_getac(sc, m0);			/* honor classification */
1643b8e788a5SAdrian Chadd 	/* XXX use txparams instead of fixed values */
1644b8e788a5SAdrian Chadd 	/*
1645b8e788a5SAdrian Chadd 	 * Calculate Atheros packet type from IEEE80211 packet header,
1646b8e788a5SAdrian Chadd 	 * setup for rate calculations, and select h/w transmit queue.
1647b8e788a5SAdrian Chadd 	 */
1648b8e788a5SAdrian Chadd 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1649b8e788a5SAdrian Chadd 	case IEEE80211_FC0_TYPE_MGT:
1650b8e788a5SAdrian Chadd 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1651b8e788a5SAdrian Chadd 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON)
1652b8e788a5SAdrian Chadd 			atype = HAL_PKT_TYPE_BEACON;
1653b8e788a5SAdrian Chadd 		else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1654b8e788a5SAdrian Chadd 			atype = HAL_PKT_TYPE_PROBE_RESP;
1655b8e788a5SAdrian Chadd 		else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM)
1656b8e788a5SAdrian Chadd 			atype = HAL_PKT_TYPE_ATIM;
1657b8e788a5SAdrian Chadd 		else
1658b8e788a5SAdrian Chadd 			atype = HAL_PKT_TYPE_NORMAL;	/* XXX */
1659b8e788a5SAdrian Chadd 		rix = an->an_mgmtrix;
1660b8e788a5SAdrian Chadd 		txrate = rt->info[rix].rateCode;
1661b8e788a5SAdrian Chadd 		if (shortPreamble)
1662b8e788a5SAdrian Chadd 			txrate |= rt->info[rix].shortPreamble;
1663b8e788a5SAdrian Chadd 		try0 = ATH_TXMGTTRY;
1664b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_INTREQ;	/* force interrupt */
1665b8e788a5SAdrian Chadd 		break;
1666b8e788a5SAdrian Chadd 	case IEEE80211_FC0_TYPE_CTL:
1667b8e788a5SAdrian Chadd 		atype = HAL_PKT_TYPE_PSPOLL;	/* stop setting of duration */
1668b8e788a5SAdrian Chadd 		rix = an->an_mgmtrix;
1669b8e788a5SAdrian Chadd 		txrate = rt->info[rix].rateCode;
1670b8e788a5SAdrian Chadd 		if (shortPreamble)
1671b8e788a5SAdrian Chadd 			txrate |= rt->info[rix].shortPreamble;
1672b8e788a5SAdrian Chadd 		try0 = ATH_TXMGTTRY;
1673b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_INTREQ;	/* force interrupt */
1674b8e788a5SAdrian Chadd 		break;
1675b8e788a5SAdrian Chadd 	case IEEE80211_FC0_TYPE_DATA:
1676b8e788a5SAdrian Chadd 		atype = HAL_PKT_TYPE_NORMAL;		/* default */
1677b8e788a5SAdrian Chadd 		/*
1678b8e788a5SAdrian Chadd 		 * Data frames: multicast frames go out at a fixed rate,
1679b8e788a5SAdrian Chadd 		 * EAPOL frames use the mgmt frame rate; otherwise consult
1680b8e788a5SAdrian Chadd 		 * the rate control module for the rate to use.
1681b8e788a5SAdrian Chadd 		 */
1682b8e788a5SAdrian Chadd 		if (ismcast) {
1683b8e788a5SAdrian Chadd 			rix = an->an_mcastrix;
1684b8e788a5SAdrian Chadd 			txrate = rt->info[rix].rateCode;
1685b8e788a5SAdrian Chadd 			if (shortPreamble)
1686b8e788a5SAdrian Chadd 				txrate |= rt->info[rix].shortPreamble;
1687b8e788a5SAdrian Chadd 			try0 = 1;
1688b8e788a5SAdrian Chadd 		} else if (m0->m_flags & M_EAPOL) {
1689b8e788a5SAdrian Chadd 			/* XXX? maybe always use long preamble? */
1690b8e788a5SAdrian Chadd 			rix = an->an_mgmtrix;
1691b8e788a5SAdrian Chadd 			txrate = rt->info[rix].rateCode;
1692b8e788a5SAdrian Chadd 			if (shortPreamble)
1693b8e788a5SAdrian Chadd 				txrate |= rt->info[rix].shortPreamble;
1694b8e788a5SAdrian Chadd 			try0 = ATH_TXMAXTRY;	/* XXX?too many? */
1695b8e788a5SAdrian Chadd 		} else {
1696eb6f0de0SAdrian Chadd 			/*
1697eb6f0de0SAdrian Chadd 			 * Do rate lookup on each TX, rather than using
1698eb6f0de0SAdrian Chadd 			 * the hard-coded TX information decided here.
1699eb6f0de0SAdrian Chadd 			 */
1700b8e788a5SAdrian Chadd 			ismrr = 1;
1701eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_doratelookup = 1;
1702b8e788a5SAdrian Chadd 		}
17039fbe631aSAdrian Chadd 
17049fbe631aSAdrian Chadd 		/*
17059fbe631aSAdrian Chadd 		 * Check whether to set NOACK for this WME category or not.
17069fbe631aSAdrian Chadd 		 */
17079fbe631aSAdrian Chadd 		if (ieee80211_wme_vap_ac_is_noack(vap, pri))
1708b8e788a5SAdrian Chadd 			flags |= HAL_TXDESC_NOACK;
1709b8e788a5SAdrian Chadd 		break;
1710b8e788a5SAdrian Chadd 	default:
171176e6fd5dSGleb Smirnoff 		device_printf(sc->sc_dev, "bogus frame type 0x%x (%s)\n",
1712b8e788a5SAdrian Chadd 		    wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__);
1713b8e788a5SAdrian Chadd 		/* XXX statistic */
1714c23a9d98SAdrian Chadd 		/* XXX free tx dmamap */
1715d07be335SAdrian Chadd 		ieee80211_free_mbuf(m0);
1716b8e788a5SAdrian Chadd 		return EIO;
1717b8e788a5SAdrian Chadd 	}
1718b8e788a5SAdrian Chadd 
1719447fd44aSAdrian Chadd 	/*
1720447fd44aSAdrian Chadd 	 * There are two known scenarios where the frame AC doesn't match
1721447fd44aSAdrian Chadd 	 * what the destination TXQ is.
1722447fd44aSAdrian Chadd 	 *
1723447fd44aSAdrian Chadd 	 * + non-QoS frames (eg management?) that the net80211 stack has
1724447fd44aSAdrian Chadd 	 *   assigned a higher AC to, but since it's a non-QoS TID, it's
1725447fd44aSAdrian Chadd 	 *   being thrown into TID 16.  TID 16 gets the AC_BE queue.
1726447fd44aSAdrian Chadd 	 *   It's quite possible that management frames should just be
1727447fd44aSAdrian Chadd 	 *   direct dispatched to hardware rather than go via the software
1728447fd44aSAdrian Chadd 	 *   queue; that should be investigated in the future.  There are
1729447fd44aSAdrian Chadd 	 *   some specific scenarios where this doesn't make sense, mostly
1730447fd44aSAdrian Chadd 	 *   surrounding ADDBA request/response - hence why that is special
1731447fd44aSAdrian Chadd 	 *   cased.
1732447fd44aSAdrian Chadd 	 *
1733447fd44aSAdrian Chadd 	 * + Multicast frames going into the VAP mcast queue.  That shows up
1734447fd44aSAdrian Chadd 	 *   as "TXQ 11".
1735447fd44aSAdrian Chadd 	 *
1736447fd44aSAdrian Chadd 	 * This driver should eventually support separate TID and TXQ locking,
1737447fd44aSAdrian Chadd 	 * allowing for arbitrary AC frames to appear on arbitrary software
1738447fd44aSAdrian Chadd 	 * queues, being queued to the "correct" hardware queue when needed.
1739447fd44aSAdrian Chadd 	 */
1740447fd44aSAdrian Chadd #if 0
17416deb7f32SAdrian Chadd 	if (txq != sc->sc_ac2q[pri]) {
174283bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_XMIT,
17436deb7f32SAdrian Chadd 		    "%s: txq=%p (%d), pri=%d, pri txq=%p (%d)\n",
17446deb7f32SAdrian Chadd 		    __func__,
17456deb7f32SAdrian Chadd 		    txq,
17466deb7f32SAdrian Chadd 		    txq->axq_qnum,
17476deb7f32SAdrian Chadd 		    pri,
17486deb7f32SAdrian Chadd 		    sc->sc_ac2q[pri],
17496deb7f32SAdrian Chadd 		    sc->sc_ac2q[pri]->axq_qnum);
17506deb7f32SAdrian Chadd 	}
1751447fd44aSAdrian Chadd #endif
17526deb7f32SAdrian Chadd 
1753b8e788a5SAdrian Chadd 	/*
1754b8e788a5SAdrian Chadd 	 * Calculate miscellaneous flags.
1755b8e788a5SAdrian Chadd 	 */
1756b8e788a5SAdrian Chadd 	if (ismcast) {
1757b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_NOACK;	/* no ack on broad/multicast */
1758b8e788a5SAdrian Chadd 	} else if (pktlen > vap->iv_rtsthreshold &&
1759b8e788a5SAdrian Chadd 	    (ni->ni_ath_flags & IEEE80211_NODE_FF) == 0) {
1760b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_RTSENA;	/* RTS based on frame length */
1761b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_rts++;
1762b8e788a5SAdrian Chadd 	}
1763b8e788a5SAdrian Chadd 	if (flags & HAL_TXDESC_NOACK)		/* NB: avoid double counting */
1764b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_noack++;
1765b8e788a5SAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
1766b8e788a5SAdrian Chadd 	if (sc->sc_tdma && (flags & HAL_TXDESC_NOACK) == 0) {
1767b8e788a5SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_TDMA,
1768b8e788a5SAdrian Chadd 		    "%s: discard frame, ACK required w/ TDMA\n", __func__);
1769b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tdma_ack++;
1770c23a9d98SAdrian Chadd 		/* XXX free tx dmamap */
1771d07be335SAdrian Chadd 		ieee80211_free_mbuf(m0);
1772b8e788a5SAdrian Chadd 		return EIO;
1773b8e788a5SAdrian Chadd 	}
1774b8e788a5SAdrian Chadd #endif
1775b8e788a5SAdrian Chadd 
17765abc0b25SAdrian Chadd 	/*
17775abc0b25SAdrian Chadd 	 * If it's a frame to do location reporting on,
17785abc0b25SAdrian Chadd 	 * communicate it to the HAL.
17795abc0b25SAdrian Chadd 	 */
17805abc0b25SAdrian Chadd 	if (ieee80211_get_toa_params(m0, NULL)) {
17815abc0b25SAdrian Chadd 		device_printf(sc->sc_dev,
17825abc0b25SAdrian Chadd 		    "%s: setting TX positioning bit\n", __func__);
17835abc0b25SAdrian Chadd 		flags |= HAL_TXDESC_POS;
17845abc0b25SAdrian Chadd 
17855abc0b25SAdrian Chadd 		/*
17865abc0b25SAdrian Chadd 		 * Note: The hardware reports timestamps for
17875abc0b25SAdrian Chadd 		 * each of the RX'ed packets as part of the packet
17885abc0b25SAdrian Chadd 		 * exchange.  So this means things like RTS/CTS
17895abc0b25SAdrian Chadd 		 * exchanges, as well as the final ACK.
17905abc0b25SAdrian Chadd 		 *
17915abc0b25SAdrian Chadd 		 * So, if you send a RTS-protected NULL data frame,
17925abc0b25SAdrian Chadd 		 * you'll get an RX report for the RTS response, then
17935abc0b25SAdrian Chadd 		 * an RX report for the NULL frame, and then the TX
17945abc0b25SAdrian Chadd 		 * completion at the end.
17955abc0b25SAdrian Chadd 		 *
17965abc0b25SAdrian Chadd 		 * NOTE: it doesn't work right for CCK frames;
17975abc0b25SAdrian Chadd 		 * there's no channel info data provided unless
17985abc0b25SAdrian Chadd 		 * it's OFDM or HT.  Will have to dig into it.
17995abc0b25SAdrian Chadd 		 */
18005abc0b25SAdrian Chadd 		flags &= ~(HAL_TXDESC_RTSENA | HAL_TXDESC_CTSENA);
18015abc0b25SAdrian Chadd 		bf->bf_flags |= ATH_BUF_TOA_PROBE;
18025abc0b25SAdrian Chadd 	}
18035abc0b25SAdrian Chadd 
1804bcf5fc49SAdrian Chadd #if 0
1805bcf5fc49SAdrian Chadd 	/*
1806bcf5fc49SAdrian Chadd 	 * Placeholder: if you want to transmit with the azimuth
1807bcf5fc49SAdrian Chadd 	 * timestamp in the end of the payload, here's where you
1808bcf5fc49SAdrian Chadd 	 * should set the TXDESC field.
1809bcf5fc49SAdrian Chadd 	 */
1810bcf5fc49SAdrian Chadd 	flags |= HAL_TXDESC_HWTS;
1811bcf5fc49SAdrian Chadd #endif
1812bcf5fc49SAdrian Chadd 
1813b8e788a5SAdrian Chadd 	/*
1814eb6f0de0SAdrian Chadd 	 * Determine if a tx interrupt should be generated for
1815eb6f0de0SAdrian Chadd 	 * this descriptor.  We take a tx interrupt to reap
1816eb6f0de0SAdrian Chadd 	 * descriptors when the h/w hits an EOL condition or
1817eb6f0de0SAdrian Chadd 	 * when the descriptor is specifically marked to generate
1818eb6f0de0SAdrian Chadd 	 * an interrupt.  We periodically mark descriptors in this
1819eb6f0de0SAdrian Chadd 	 * way to insure timely replenishing of the supply needed
1820eb6f0de0SAdrian Chadd 	 * for sending frames.  Defering interrupts reduces system
1821eb6f0de0SAdrian Chadd 	 * load and potentially allows more concurrent work to be
1822eb6f0de0SAdrian Chadd 	 * done but if done to aggressively can cause senders to
1823eb6f0de0SAdrian Chadd 	 * backup.
1824eb6f0de0SAdrian Chadd 	 *
1825eb6f0de0SAdrian Chadd 	 * NB: use >= to deal with sc_txintrperiod changing
1826eb6f0de0SAdrian Chadd 	 *     dynamically through sysctl.
1827b8e788a5SAdrian Chadd 	 */
1828eb6f0de0SAdrian Chadd 	if (flags & HAL_TXDESC_INTREQ) {
1829eb6f0de0SAdrian Chadd 		txq->axq_intrcnt = 0;
1830eb6f0de0SAdrian Chadd 	} else if (++txq->axq_intrcnt >= sc->sc_txintrperiod) {
1831eb6f0de0SAdrian Chadd 		flags |= HAL_TXDESC_INTREQ;
1832eb6f0de0SAdrian Chadd 		txq->axq_intrcnt = 0;
1833eb6f0de0SAdrian Chadd 	}
1834e42b5dbaSAdrian Chadd 
1835eb6f0de0SAdrian Chadd 	/* This point forward is actual TX bits */
1836b8e788a5SAdrian Chadd 
1837b8e788a5SAdrian Chadd 	/*
1838b8e788a5SAdrian Chadd 	 * At this point we are committed to sending the frame
1839b8e788a5SAdrian Chadd 	 * and we don't need to look at m_nextpkt; clear it in
1840b8e788a5SAdrian Chadd 	 * case this frame is part of frag chain.
1841b8e788a5SAdrian Chadd 	 */
1842b8e788a5SAdrian Chadd 	m0->m_nextpkt = NULL;
1843b8e788a5SAdrian Chadd 
1844b8e788a5SAdrian Chadd 	if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
1845b8e788a5SAdrian Chadd 		ieee80211_dump_pkt(ic, mtod(m0, const uint8_t *), m0->m_len,
1846b8e788a5SAdrian Chadd 		    sc->sc_hwmap[rix].ieeerate, -1);
1847b8e788a5SAdrian Chadd 
1848b8e788a5SAdrian Chadd 	if (ieee80211_radiotap_active_vap(vap)) {
1849b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
1850b8e788a5SAdrian Chadd 		if (iswep)
1851b8e788a5SAdrian Chadd 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
1852b8e788a5SAdrian Chadd 		if (isfrag)
1853b8e788a5SAdrian Chadd 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
1854b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
185512087a07SAdrian Chadd 		sc->sc_tx_th.wt_txpower = ieee80211_get_node_txpower(ni);
1856b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
1857b8e788a5SAdrian Chadd 
1858b8e788a5SAdrian Chadd 		ieee80211_radiotap_tx(vap, m0);
1859b8e788a5SAdrian Chadd 	}
1860b8e788a5SAdrian Chadd 
1861eb6f0de0SAdrian Chadd 	/* Blank the legacy rate array */
1862eb6f0de0SAdrian Chadd 	bzero(&bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
1863c1782ce0SAdrian Chadd 
1864b8e788a5SAdrian Chadd 	/*
1865eb6f0de0SAdrian Chadd 	 * ath_buf_set_rate needs at least one rate/try to setup
1866eb6f0de0SAdrian Chadd 	 * the rate scenario.
1867b8e788a5SAdrian Chadd 	 */
1868eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].rix = rix;
1869eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].tries = try0;
1870eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].ratecode = txrate;
1871eb6f0de0SAdrian Chadd 
1872eb6f0de0SAdrian Chadd 	/* Store the decided rate index values away */
1873eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_pktlen = pktlen;
1874eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_hdrlen = hdrlen;
1875eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_atype = atype;
187612087a07SAdrian Chadd 	bf->bf_state.bfs_txpower = ieee80211_get_node_txpower(ni);
1877eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txrate0 = txrate;
1878eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_try0 = try0;
1879eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_keyix = keyix;
1880eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txantenna = sc->sc_txantenna;
1881875a9451SAdrian Chadd 	bf->bf_state.bfs_txflags = flags;
1882eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_shpream = shortPreamble;
1883eb6f0de0SAdrian Chadd 
1884eb6f0de0SAdrian Chadd 	/* XXX this should be done in ath_tx_setrate() */
1885eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsrate0 = 0;	/* ie, no hard-coded ctsrate */
1886eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsrate = 0;	/* calculated later */
1887eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsduration = 0;
1888eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ismrr = ismrr;
1889eb6f0de0SAdrian Chadd 
1890eb6f0de0SAdrian Chadd 	return 0;
1891eb6f0de0SAdrian Chadd }
1892eb6f0de0SAdrian Chadd 
1893b8e788a5SAdrian Chadd /*
18944e81f27cSAdrian Chadd  * Queue a frame to the hardware or software queue.
1895eb6f0de0SAdrian Chadd  *
1896eb6f0de0SAdrian Chadd  * This can be called by the net80211 code.
1897eb6f0de0SAdrian Chadd  *
1898eb6f0de0SAdrian Chadd  * XXX what about locking? Or, push the seqno assign into the
1899eb6f0de0SAdrian Chadd  * XXX aggregate scheduler so its serialised?
19004e81f27cSAdrian Chadd  *
19014e81f27cSAdrian Chadd  * XXX When sending management frames via ath_raw_xmit(),
19024e81f27cSAdrian Chadd  *     should CLRDMASK be set unconditionally?
1903b8e788a5SAdrian Chadd  */
1904eb6f0de0SAdrian Chadd int
1905eb6f0de0SAdrian Chadd ath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni,
1906eb6f0de0SAdrian Chadd     struct ath_buf *bf, struct mbuf *m0)
1907eb6f0de0SAdrian Chadd {
1908eb6f0de0SAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
1909eb6f0de0SAdrian Chadd 	struct ath_vap *avp = ATH_VAP(vap);
19109c85ff91SAdrian Chadd 	int r = 0;
1911eb6f0de0SAdrian Chadd 	u_int pri;
1912eb6f0de0SAdrian Chadd 	int tid;
1913eb6f0de0SAdrian Chadd 	struct ath_txq *txq;
1914eb6f0de0SAdrian Chadd 	int ismcast;
1915eb6f0de0SAdrian Chadd 	const struct ieee80211_frame *wh;
1916eb6f0de0SAdrian Chadd 	int is_ampdu, is_ampdu_tx, is_ampdu_pending;
1917a108d2d6SAdrian Chadd 	ieee80211_seq seqno;
1918eb6f0de0SAdrian Chadd 	uint8_t type, subtype;
191922a3aee6SAdrian Chadd 	int queue_to_head;
1920eb6f0de0SAdrian Chadd 
1921375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
1922375307d4SAdrian Chadd 
1923eb6f0de0SAdrian Chadd 	/*
1924eb6f0de0SAdrian Chadd 	 * Determine the target hardware queue.
1925eb6f0de0SAdrian Chadd 	 *
1926b43facbfSAdrian Chadd 	 * For multicast frames, the txq gets overridden appropriately
192715e58d4dSAdrian Chadd 	 * depending upon the state of PS.  If powersave is enabled
192815e58d4dSAdrian Chadd 	 * then they get added to the cabq for later transmit.
192915e58d4dSAdrian Chadd 	 *
193015e58d4dSAdrian Chadd 	 * The "fun" issue here is that group addressed frames should
193115e58d4dSAdrian Chadd 	 * have the sequence number from a different pool, rather than
193215e58d4dSAdrian Chadd 	 * the per-TID pool.  That means that even QoS group addressed
193315e58d4dSAdrian Chadd 	 * frames will have a sequence number from that global value,
193415e58d4dSAdrian Chadd 	 * which means if we transmit different group addressed frames
193515e58d4dSAdrian Chadd 	 * at different traffic priorities, the sequence numbers will
193615e58d4dSAdrian Chadd 	 * all be out of whack.  So - chances are, the right thing
193715e58d4dSAdrian Chadd 	 * to do here is to always put group addressed frames into the BE
193815e58d4dSAdrian Chadd 	 * queue, and ignore the TID for queue selection.
1939eb6f0de0SAdrian Chadd 	 *
1940eb6f0de0SAdrian Chadd 	 * For any other frame, we do a TID/QoS lookup inside the frame
1941eb6f0de0SAdrian Chadd 	 * to see what the TID should be. If it's a non-QoS frame, the
1942eb6f0de0SAdrian Chadd 	 * AC and TID are overridden. The TID/TXQ code assumes the
1943eb6f0de0SAdrian Chadd 	 * TID is on a predictable hardware TXQ, so we don't support
1944eb6f0de0SAdrian Chadd 	 * having a node TID queued to multiple hardware TXQs.
1945eb6f0de0SAdrian Chadd 	 * This may change in the future but would require some locking
1946eb6f0de0SAdrian Chadd 	 * fudgery.
1947eb6f0de0SAdrian Chadd 	 */
1948eb6f0de0SAdrian Chadd 	pri = ath_tx_getac(sc, m0);
1949eb6f0de0SAdrian Chadd 	tid = ath_tx_gettid(sc, m0);
1950eb6f0de0SAdrian Chadd 
1951eb6f0de0SAdrian Chadd 	txq = sc->sc_ac2q[pri];
1952eb6f0de0SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1953eb6f0de0SAdrian Chadd 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1954eb6f0de0SAdrian Chadd 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1955eb6f0de0SAdrian Chadd 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1956eb6f0de0SAdrian Chadd 
19579c85ff91SAdrian Chadd 	/*
19589c85ff91SAdrian Chadd 	 * Enforce how deep the multicast queue can grow.
19599c85ff91SAdrian Chadd 	 *
19609c85ff91SAdrian Chadd 	 * XXX duplicated in ath_raw_xmit().
19619c85ff91SAdrian Chadd 	 */
19629c85ff91SAdrian Chadd 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
196392e84e43SAdrian Chadd 		if (sc->sc_cabq->axq_depth + sc->sc_cabq->fifo.axq_depth
196492e84e43SAdrian Chadd 		    > sc->sc_txq_mcastq_maxdepth) {
19659c85ff91SAdrian Chadd 			sc->sc_stats.ast_tx_mcastq_overflow++;
19669c85ff91SAdrian Chadd 			m_freem(m0);
196755cf0326SAdrian Chadd 			return (ENOBUFS);
19689c85ff91SAdrian Chadd 		}
19699c85ff91SAdrian Chadd 	}
19709c85ff91SAdrian Chadd 
197122a3aee6SAdrian Chadd 	/*
197222a3aee6SAdrian Chadd 	 * Enforce how deep the unicast queue can grow.
197322a3aee6SAdrian Chadd 	 *
197422a3aee6SAdrian Chadd 	 * If the node is in power save then we don't want
197522a3aee6SAdrian Chadd 	 * the software queue to grow too deep, or a node may
197622a3aee6SAdrian Chadd 	 * end up consuming all of the ath_buf entries.
197722a3aee6SAdrian Chadd 	 *
197822a3aee6SAdrian Chadd 	 * For now, only do this for DATA frames.
197922a3aee6SAdrian Chadd 	 *
198022a3aee6SAdrian Chadd 	 * We will want to cap how many management/control
198122a3aee6SAdrian Chadd 	 * frames get punted to the software queue so it doesn't
198222a3aee6SAdrian Chadd 	 * fill up.  But the correct solution isn't yet obvious.
198322a3aee6SAdrian Chadd 	 * In any case, this check should at least let frames pass
198422a3aee6SAdrian Chadd 	 * that we are direct-dispatching.
198522a3aee6SAdrian Chadd 	 *
198622a3aee6SAdrian Chadd 	 * XXX TODO: duplicate this to the raw xmit path!
198722a3aee6SAdrian Chadd 	 */
198822a3aee6SAdrian Chadd 	if (type == IEEE80211_FC0_TYPE_DATA &&
198922a3aee6SAdrian Chadd 	    ATH_NODE(ni)->an_is_powersave &&
199022a3aee6SAdrian Chadd 	    ATH_NODE(ni)->an_swq_depth >
199122a3aee6SAdrian Chadd 	     sc->sc_txq_node_psq_maxdepth) {
199222a3aee6SAdrian Chadd 		sc->sc_stats.ast_tx_node_psq_overflow++;
199322a3aee6SAdrian Chadd 		m_freem(m0);
199422a3aee6SAdrian Chadd 		return (ENOBUFS);
199522a3aee6SAdrian Chadd 	}
199622a3aee6SAdrian Chadd 
1997eb6f0de0SAdrian Chadd 	/* A-MPDU TX */
1998eb6f0de0SAdrian Chadd 	is_ampdu_tx = ath_tx_ampdu_running(sc, ATH_NODE(ni), tid);
1999eb6f0de0SAdrian Chadd 	is_ampdu_pending = ath_tx_ampdu_pending(sc, ATH_NODE(ni), tid);
2000eb6f0de0SAdrian Chadd 	is_ampdu = is_ampdu_tx | is_ampdu_pending;
2001eb6f0de0SAdrian Chadd 
2002a108d2d6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d, ac=%d, is_ampdu=%d\n",
2003a108d2d6SAdrian Chadd 	    __func__, tid, pri, is_ampdu);
2004eb6f0de0SAdrian Chadd 
200546634305SAdrian Chadd 	/* Set local packet state, used to queue packets to hardware */
200646634305SAdrian Chadd 	bf->bf_state.bfs_tid = tid;
2007fc56c9c5SAdrian Chadd 	bf->bf_state.bfs_tx_queue = txq->axq_qnum;
200846634305SAdrian Chadd 	bf->bf_state.bfs_pri = pri;
200946634305SAdrian Chadd 
2010b837332dSAdrian Chadd #if 1
2011c5940c30SAdrian Chadd 	/*
2012b43facbfSAdrian Chadd 	 * When servicing one or more stations in power-save mode
2013b43facbfSAdrian Chadd 	 * (or) if there is some mcast data waiting on the mcast
2014b43facbfSAdrian Chadd 	 * queue (to prevent out of order delivery) multicast frames
2015b43facbfSAdrian Chadd 	 * must be bufferd until after the beacon.
2016b43facbfSAdrian Chadd 	 *
2017b43facbfSAdrian Chadd 	 * TODO: we should lock the mcastq before we check the length.
2018c5940c30SAdrian Chadd 	 */
2019b837332dSAdrian Chadd 	if (sc->sc_cabq_enable && ismcast && (vap->iv_ps_sta || avp->av_mcastq.axq_depth)) {
2020eb6f0de0SAdrian Chadd 		txq = &avp->av_mcastq;
202146634305SAdrian Chadd 		/*
202246634305SAdrian Chadd 		 * Mark the frame as eventually belonging on the CAB
202346634305SAdrian Chadd 		 * queue, so the descriptor setup functions will
202446634305SAdrian Chadd 		 * correctly initialise the descriptor 'qcuId' field.
202546634305SAdrian Chadd 		 */
2026fc56c9c5SAdrian Chadd 		bf->bf_state.bfs_tx_queue = sc->sc_cabq->axq_qnum;
202746634305SAdrian Chadd 	}
2028b837332dSAdrian Chadd #endif
2029eb6f0de0SAdrian Chadd 
2030eb6f0de0SAdrian Chadd 	/* Do the generic frame setup */
2031eb6f0de0SAdrian Chadd 	/* XXX should just bzero the bf_state? */
2032eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_dobaw = 0;
2033eb6f0de0SAdrian Chadd 
20347561cb5cSAdrian Chadd 	/* A-MPDU TX? Manually set sequence number */
20357561cb5cSAdrian Chadd 	/*
20367561cb5cSAdrian Chadd 	 * Don't do it whilst pending; the net80211 layer still
20377561cb5cSAdrian Chadd 	 * assigns them.
203815e58d4dSAdrian Chadd 	 *
203915e58d4dSAdrian Chadd 	 * Don't assign A-MPDU sequence numbers to group address
204015e58d4dSAdrian Chadd 	 * frames; they come from a different sequence number space.
20417561cb5cSAdrian Chadd 	 */
204215e58d4dSAdrian Chadd 	if (is_ampdu_tx && (! IEEE80211_IS_MULTICAST(wh->i_addr1))) {
2043eb6f0de0SAdrian Chadd 		/*
2044eb6f0de0SAdrian Chadd 		 * Always call; this function will
2045eb6f0de0SAdrian Chadd 		 * handle making sure that null data frames
204615e58d4dSAdrian Chadd 		 * and group-addressed frames don't get a sequence number
204715e58d4dSAdrian Chadd 		 * from the current TID and thus mess with the BAW.
2048eb6f0de0SAdrian Chadd 		 */
2049a108d2d6SAdrian Chadd 		seqno = ath_tx_tid_seqno_assign(sc, ni, bf, m0);
205042f4d061SAdrian Chadd 
205142f4d061SAdrian Chadd 		/*
205215e58d4dSAdrian Chadd 		 * Don't add QoS NULL frames and group-addressed frames
205315e58d4dSAdrian Chadd 		 * to the BAW.
205442f4d061SAdrian Chadd 		 */
2055a108d2d6SAdrian Chadd 		if (IEEE80211_QOS_HAS_SEQ(wh) &&
205615e58d4dSAdrian Chadd 		    (! IEEE80211_IS_MULTICAST(wh->i_addr1)) &&
205715e58d4dSAdrian Chadd 		    (subtype != IEEE80211_FC0_SUBTYPE_QOS_NULL)) {
2058eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_dobaw = 1;
2059eb6f0de0SAdrian Chadd 		}
2060c1782ce0SAdrian Chadd 	}
2061c1782ce0SAdrian Chadd 
2062eb6f0de0SAdrian Chadd 	/*
2063eb6f0de0SAdrian Chadd 	 * If needed, the sequence number has been assigned.
2064eb6f0de0SAdrian Chadd 	 * Squirrel it away somewhere easy to get to.
2065eb6f0de0SAdrian Chadd 	 */
2066a108d2d6SAdrian Chadd 	bf->bf_state.bfs_seqno = M_SEQNO_GET(m0) << IEEE80211_SEQ_SEQ_SHIFT;
2067b8e788a5SAdrian Chadd 
2068eb6f0de0SAdrian Chadd 	/* Is ampdu pending? fetch the seqno and print it out */
2069eb6f0de0SAdrian Chadd 	if (is_ampdu_pending)
2070eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX,
2071eb6f0de0SAdrian Chadd 		    "%s: tid %d: ampdu pending, seqno %d\n",
2072eb6f0de0SAdrian Chadd 		    __func__, tid, M_SEQNO_GET(m0));
2073eb6f0de0SAdrian Chadd 
207415e58d4dSAdrian Chadd 	/* This also sets up the DMA map; crypto; frame parameters, etc */
2075b43facbfSAdrian Chadd 	r = ath_tx_normal_setup(sc, ni, bf, m0, txq);
2076eb6f0de0SAdrian Chadd 
2077eb6f0de0SAdrian Chadd 	if (r != 0)
20787561cb5cSAdrian Chadd 		goto done;
2079eb6f0de0SAdrian Chadd 
2080eb6f0de0SAdrian Chadd 	/* At this point m0 could have changed! */
2081eb6f0de0SAdrian Chadd 	m0 = bf->bf_m;
2082eb6f0de0SAdrian Chadd 
2083eb6f0de0SAdrian Chadd #if 1
2084eb6f0de0SAdrian Chadd 	/*
2085eb6f0de0SAdrian Chadd 	 * If it's a multicast frame, do a direct-dispatch to the
2086eb6f0de0SAdrian Chadd 	 * destination hardware queue. Don't bother software
2087eb6f0de0SAdrian Chadd 	 * queuing it.
2088eb6f0de0SAdrian Chadd 	 */
2089eb6f0de0SAdrian Chadd 	/*
2090eb6f0de0SAdrian Chadd 	 * If it's a BAR frame, do a direct dispatch to the
2091eb6f0de0SAdrian Chadd 	 * destination hardware queue. Don't bother software
2092eb6f0de0SAdrian Chadd 	 * queuing it, as the TID will now be paused.
2093eb6f0de0SAdrian Chadd 	 * Sending a BAR frame can occur from the net80211 txa timer
2094eb6f0de0SAdrian Chadd 	 * (ie, retries) or from the ath txtask (completion call.)
2095eb6f0de0SAdrian Chadd 	 * It queues directly to hardware because the TID is paused
2096eb6f0de0SAdrian Chadd 	 * at this point (and won't be unpaused until the BAR has
2097eb6f0de0SAdrian Chadd 	 * either been TXed successfully or max retries has been
2098eb6f0de0SAdrian Chadd 	 * reached.)
2099eb6f0de0SAdrian Chadd 	 */
210022a3aee6SAdrian Chadd 	/*
210122a3aee6SAdrian Chadd 	 * Until things are better debugged - if this node is asleep
210222a3aee6SAdrian Chadd 	 * and we're sending it a non-BAR frame, direct dispatch it.
210322a3aee6SAdrian Chadd 	 * Why? Because we need to figure out what's actually being
210422a3aee6SAdrian Chadd 	 * sent - eg, during reassociation/reauthentication after
210522a3aee6SAdrian Chadd 	 * the node (last) disappeared whilst asleep, the driver should
210622a3aee6SAdrian Chadd 	 * have unpaused/unsleep'ed the node.  So until that is
210722a3aee6SAdrian Chadd 	 * sorted out, use this workaround.
210822a3aee6SAdrian Chadd 	 */
2109eb6f0de0SAdrian Chadd 	if (txq == &avp->av_mcastq) {
2110d3a6425bSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX,
21110b96ef63SAdrian Chadd 		    "%s: bf=%p: mcastq: TX'ing\n", __func__, bf);
21124e81f27cSAdrian Chadd 		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
2113eb6f0de0SAdrian Chadd 		ath_tx_xmit_normal(sc, txq, bf);
211422a3aee6SAdrian Chadd 	} else if (ath_tx_should_swq_frame(sc, ATH_NODE(ni), m0,
211522a3aee6SAdrian Chadd 	    &queue_to_head)) {
211622a3aee6SAdrian Chadd 		ath_tx_swq(sc, ni, txq, queue_to_head, bf);
211722a3aee6SAdrian Chadd 	} else {
21184e81f27cSAdrian Chadd 		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
2119eb6f0de0SAdrian Chadd 		ath_tx_xmit_normal(sc, txq, bf);
2120eb6f0de0SAdrian Chadd 	}
2121eb6f0de0SAdrian Chadd #else
2122eb6f0de0SAdrian Chadd 	/*
2123eb6f0de0SAdrian Chadd 	 * For now, since there's no software queue,
2124eb6f0de0SAdrian Chadd 	 * direct-dispatch to the hardware.
2125eb6f0de0SAdrian Chadd 	 */
21264e81f27cSAdrian Chadd 	bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
212722a3aee6SAdrian Chadd 	/*
212822a3aee6SAdrian Chadd 	 * Update the current leak count if
212922a3aee6SAdrian Chadd 	 * we're leaking frames; and set the
213022a3aee6SAdrian Chadd 	 * MORE flag as appropriate.
213122a3aee6SAdrian Chadd 	 */
213222a3aee6SAdrian Chadd 	ath_tx_leak_count_update(sc, tid, bf);
2133eb6f0de0SAdrian Chadd 	ath_tx_xmit_normal(sc, txq, bf);
2134eb6f0de0SAdrian Chadd #endif
21357561cb5cSAdrian Chadd done:
2136b8e788a5SAdrian Chadd 	return 0;
2137b8e788a5SAdrian Chadd }
2138b8e788a5SAdrian Chadd 
2139b8e788a5SAdrian Chadd static int
2140b8e788a5SAdrian Chadd ath_tx_raw_start(struct ath_softc *sc, struct ieee80211_node *ni,
2141b8e788a5SAdrian Chadd 	struct ath_buf *bf, struct mbuf *m0,
2142b8e788a5SAdrian Chadd 	const struct ieee80211_bpf_params *params)
2143b8e788a5SAdrian Chadd {
21447a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
2145b8e788a5SAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
2146b8e788a5SAdrian Chadd 	int error, ismcast, ismrr;
2147b8e788a5SAdrian Chadd 	int keyix, hdrlen, pktlen, try0, txantenna;
2148eb6f0de0SAdrian Chadd 	u_int8_t rix, txrate;
2149b8e788a5SAdrian Chadd 	struct ieee80211_frame *wh;
2150eb6f0de0SAdrian Chadd 	u_int flags;
2151b8e788a5SAdrian Chadd 	HAL_PKT_TYPE atype;
2152b8e788a5SAdrian Chadd 	const HAL_RATE_TABLE *rt;
2153b8e788a5SAdrian Chadd 	struct ath_desc *ds;
2154b8e788a5SAdrian Chadd 	u_int pri;
2155eb6f0de0SAdrian Chadd 	int o_tid = -1;
2156eb6f0de0SAdrian Chadd 	int do_override;
215722a3aee6SAdrian Chadd 	uint8_t type, subtype;
215822a3aee6SAdrian Chadd 	int queue_to_head;
2159f5c30c4eSAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
2160b8e788a5SAdrian Chadd 
2161375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
2162375307d4SAdrian Chadd 
2163b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
2164b8e788a5SAdrian Chadd 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
2165b8e788a5SAdrian Chadd 	hdrlen = ieee80211_anyhdrsize(wh);
2166b8e788a5SAdrian Chadd 	/*
2167b8e788a5SAdrian Chadd 	 * Packet length must not include any
2168b8e788a5SAdrian Chadd 	 * pad bytes; deduct them here.
2169b8e788a5SAdrian Chadd 	 */
2170b8e788a5SAdrian Chadd 	/* XXX honor IEEE80211_BPF_DATAPAD */
2171b8e788a5SAdrian Chadd 	pktlen = m0->m_pkthdr.len - (hdrlen & 3) + IEEE80211_CRC_LEN;
2172b8e788a5SAdrian Chadd 
217322a3aee6SAdrian Chadd 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
217422a3aee6SAdrian Chadd 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
217522a3aee6SAdrian Chadd 
217603682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 2,
217703682514SAdrian Chadd 	     "ath_tx_raw_start: ni=%p, bf=%p, raw", ni, bf);
217803682514SAdrian Chadd 
2179eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: ismcast=%d\n",
2180eb6f0de0SAdrian Chadd 	    __func__, ismcast);
2181eb6f0de0SAdrian Chadd 
21827561cb5cSAdrian Chadd 	pri = params->ibp_pri & 3;
21837561cb5cSAdrian Chadd 	/* Override pri if the frame isn't a QoS one */
21847561cb5cSAdrian Chadd 	if (! IEEE80211_QOS_HAS_SEQ(wh))
21857561cb5cSAdrian Chadd 		pri = ath_tx_getac(sc, m0);
21867561cb5cSAdrian Chadd 
21877561cb5cSAdrian Chadd 	/* XXX If it's an ADDBA, override the correct queue */
21887561cb5cSAdrian Chadd 	do_override = ath_tx_action_frame_override_queue(sc, ni, m0, &o_tid);
21897561cb5cSAdrian Chadd 
21907561cb5cSAdrian Chadd 	/* Map ADDBA to the correct priority */
21917561cb5cSAdrian Chadd 	if (do_override) {
219215e58d4dSAdrian Chadd #if 1
219383bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_XMIT,
21947561cb5cSAdrian Chadd 		    "%s: overriding tid %d pri %d -> %d\n",
21957561cb5cSAdrian Chadd 		    __func__, o_tid, pri, TID_TO_WME_AC(o_tid));
21967561cb5cSAdrian Chadd #endif
21977561cb5cSAdrian Chadd 		pri = TID_TO_WME_AC(o_tid);
21987561cb5cSAdrian Chadd 	}
21997561cb5cSAdrian Chadd 
220015e58d4dSAdrian Chadd 	/*
220115e58d4dSAdrian Chadd 	 * "pri" is the hardware queue to transmit on.
220215e58d4dSAdrian Chadd 	 *
220315e58d4dSAdrian Chadd 	 * Look at the description in ath_tx_start() to understand
220415e58d4dSAdrian Chadd 	 * what needs to be "fixed" here so we just use the TID
220515e58d4dSAdrian Chadd 	 * for QoS frames.
220615e58d4dSAdrian Chadd 	 */
220715e58d4dSAdrian Chadd 
220881a82688SAdrian Chadd 	/* Handle encryption twiddling if needed */
2209eb6f0de0SAdrian Chadd 	if (! ath_tx_tag_crypto(sc, ni,
2210eb6f0de0SAdrian Chadd 	    m0, params->ibp_flags & IEEE80211_BPF_CRYPTO, 0,
2211eb6f0de0SAdrian Chadd 	    &hdrlen, &pktlen, &keyix)) {
2212d07be335SAdrian Chadd 		ieee80211_free_mbuf(m0);
2213b8e788a5SAdrian Chadd 		return EIO;
2214b8e788a5SAdrian Chadd 	}
2215b8e788a5SAdrian Chadd 	/* packet header may have moved, reset our local pointer */
2216b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
2217b8e788a5SAdrian Chadd 
2218eb6f0de0SAdrian Chadd 	/* Do the generic frame setup */
2219eb6f0de0SAdrian Chadd 	/* XXX should just bzero the bf_state? */
2220eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_dobaw = 0;
2221eb6f0de0SAdrian Chadd 
2222b8e788a5SAdrian Chadd 	error = ath_tx_dmasetup(sc, bf, m0);
2223b8e788a5SAdrian Chadd 	if (error != 0)
2224b8e788a5SAdrian Chadd 		return error;
2225b8e788a5SAdrian Chadd 	m0 = bf->bf_m;				/* NB: may have changed */
2226b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
2227f5c30c4eSAdrian Chadd 	KASSERT((ni != NULL), ("%s: ni=NULL!", __func__));
2228b8e788a5SAdrian Chadd 	bf->bf_node = ni;			/* NB: held reference */
2229b8e788a5SAdrian Chadd 
22304e81f27cSAdrian Chadd 	/* Always enable CLRDMASK for raw frames for now.. */
2231b8e788a5SAdrian Chadd 	flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for crypto errs */
2232b8e788a5SAdrian Chadd 	flags |= HAL_TXDESC_INTREQ;		/* force interrupt */
2233b8e788a5SAdrian Chadd 	if (params->ibp_flags & IEEE80211_BPF_RTS)
2234b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_RTSENA;
2235eb6f0de0SAdrian Chadd 	else if (params->ibp_flags & IEEE80211_BPF_CTS) {
2236eb6f0de0SAdrian Chadd 		/* XXX assume 11g/11n protection? */
2237eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_doprot = 1;
2238b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_CTSENA;
2239eb6f0de0SAdrian Chadd 	}
2240b8e788a5SAdrian Chadd 	/* XXX leave ismcast to injector? */
2241b8e788a5SAdrian Chadd 	if ((params->ibp_flags & IEEE80211_BPF_NOACK) || ismcast)
2242b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_NOACK;
2243b8e788a5SAdrian Chadd 
2244b8e788a5SAdrian Chadd 	rt = sc->sc_currates;
2245b8e788a5SAdrian Chadd 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
2246f5c30c4eSAdrian Chadd 
2247f5c30c4eSAdrian Chadd 	/* Fetch first rate information */
2248b8e788a5SAdrian Chadd 	rix = ath_tx_findrix(sc, params->ibp_rate0);
2249f5c30c4eSAdrian Chadd 	try0 = params->ibp_try0;
2250f5c30c4eSAdrian Chadd 
2251f5c30c4eSAdrian Chadd 	/*
2252f5c30c4eSAdrian Chadd 	 * Override EAPOL rate as appropriate.
2253f5c30c4eSAdrian Chadd 	 */
2254f5c30c4eSAdrian Chadd 	if (m0->m_flags & M_EAPOL) {
2255f5c30c4eSAdrian Chadd 		/* XXX? maybe always use long preamble? */
2256f5c30c4eSAdrian Chadd 		rix = an->an_mgmtrix;
2257f5c30c4eSAdrian Chadd 		try0 = ATH_TXMAXTRY;	/* XXX?too many? */
2258f5c30c4eSAdrian Chadd 	}
2259f5c30c4eSAdrian Chadd 
22605abc0b25SAdrian Chadd 	/*
22615abc0b25SAdrian Chadd 	 * If it's a frame to do location reporting on,
22625abc0b25SAdrian Chadd 	 * communicate it to the HAL.
22635abc0b25SAdrian Chadd 	 */
22645abc0b25SAdrian Chadd 	if (ieee80211_get_toa_params(m0, NULL)) {
22655abc0b25SAdrian Chadd 		device_printf(sc->sc_dev,
22665abc0b25SAdrian Chadd 		    "%s: setting TX positioning bit\n", __func__);
22675abc0b25SAdrian Chadd 		flags |= HAL_TXDESC_POS;
22685abc0b25SAdrian Chadd 		flags &= ~(HAL_TXDESC_RTSENA | HAL_TXDESC_CTSENA);
22695abc0b25SAdrian Chadd 		bf->bf_flags |= ATH_BUF_TOA_PROBE;
22705abc0b25SAdrian Chadd 	}
22715abc0b25SAdrian Chadd 
2272b8e788a5SAdrian Chadd 	txrate = rt->info[rix].rateCode;
2273b8e788a5SAdrian Chadd 	if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
2274b8e788a5SAdrian Chadd 		txrate |= rt->info[rix].shortPreamble;
2275b8e788a5SAdrian Chadd 	sc->sc_txrix = rix;
2276b8e788a5SAdrian Chadd 	ismrr = (params->ibp_try1 != 0);
2277b8e788a5SAdrian Chadd 	txantenna = params->ibp_pri >> 2;
2278b8e788a5SAdrian Chadd 	if (txantenna == 0)			/* XXX? */
2279b8e788a5SAdrian Chadd 		txantenna = sc->sc_txantenna;
228079f02dbfSAdrian Chadd 
228179f02dbfSAdrian Chadd 	/*
2282eb6f0de0SAdrian Chadd 	 * Since ctsrate is fixed, store it away for later
2283eb6f0de0SAdrian Chadd 	 * use when the descriptor fields are being set.
228479f02dbfSAdrian Chadd 	 */
2285eb6f0de0SAdrian Chadd 	if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA))
2286eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_ctsrate0 = params->ibp_ctsrate;
228779f02dbfSAdrian Chadd 
2288b8e788a5SAdrian Chadd 	/*
2289b8e788a5SAdrian Chadd 	 * NB: we mark all packets as type PSPOLL so the h/w won't
2290b8e788a5SAdrian Chadd 	 * set the sequence number, duration, etc.
2291b8e788a5SAdrian Chadd 	 */
2292b8e788a5SAdrian Chadd 	atype = HAL_PKT_TYPE_PSPOLL;
2293b8e788a5SAdrian Chadd 
2294b8e788a5SAdrian Chadd 	if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
2295b8e788a5SAdrian Chadd 		ieee80211_dump_pkt(ic, mtod(m0, caddr_t), m0->m_len,
2296b8e788a5SAdrian Chadd 		    sc->sc_hwmap[rix].ieeerate, -1);
2297b8e788a5SAdrian Chadd 
2298b8e788a5SAdrian Chadd 	if (ieee80211_radiotap_active_vap(vap)) {
2299b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
23005945b5f5SKevin Lo 		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED)
2301b8e788a5SAdrian Chadd 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2302b8e788a5SAdrian Chadd 		if (m0->m_flags & M_FRAG)
2303b8e788a5SAdrian Chadd 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
2304b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
230512087a07SAdrian Chadd 		sc->sc_tx_th.wt_txpower = MIN(params->ibp_power,
230612087a07SAdrian Chadd 		    ieee80211_get_node_txpower(ni));
2307b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
2308b8e788a5SAdrian Chadd 
2309b8e788a5SAdrian Chadd 		ieee80211_radiotap_tx(vap, m0);
2310b8e788a5SAdrian Chadd 	}
2311b8e788a5SAdrian Chadd 
2312b8e788a5SAdrian Chadd 	/*
2313b8e788a5SAdrian Chadd 	 * Formulate first tx descriptor with tx controls.
2314b8e788a5SAdrian Chadd 	 */
2315b8e788a5SAdrian Chadd 	ds = bf->bf_desc;
2316b8e788a5SAdrian Chadd 	/* XXX check return value? */
2317eb6f0de0SAdrian Chadd 
2318eb6f0de0SAdrian Chadd 	/* Store the decided rate index values away */
2319eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_pktlen = pktlen;
2320eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_hdrlen = hdrlen;
2321eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_atype = atype;
232212087a07SAdrian Chadd 	bf->bf_state.bfs_txpower = MIN(params->ibp_power,
232312087a07SAdrian Chadd 	    ieee80211_get_node_txpower(ni));
2324eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txrate0 = txrate;
2325eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_try0 = try0;
2326eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_keyix = keyix;
2327eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txantenna = txantenna;
2328875a9451SAdrian Chadd 	bf->bf_state.bfs_txflags = flags;
2329eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_shpream =
2330eb6f0de0SAdrian Chadd 	    !! (params->ibp_flags & IEEE80211_BPF_SHORTPRE);
2331b8e788a5SAdrian Chadd 
233246634305SAdrian Chadd 	/* Set local packet state, used to queue packets to hardware */
233346634305SAdrian Chadd 	bf->bf_state.bfs_tid = WME_AC_TO_TID(pri);
2334fc56c9c5SAdrian Chadd 	bf->bf_state.bfs_tx_queue = sc->sc_ac2q[pri]->axq_qnum;
233546634305SAdrian Chadd 	bf->bf_state.bfs_pri = pri;
233646634305SAdrian Chadd 
2337eb6f0de0SAdrian Chadd 	/* XXX this should be done in ath_tx_setrate() */
2338eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsrate = 0;
2339eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsduration = 0;
2340eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ismrr = ismrr;
2341eb6f0de0SAdrian Chadd 
2342eb6f0de0SAdrian Chadd 	/* Blank the legacy rate array */
2343eb6f0de0SAdrian Chadd 	bzero(&bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
2344eb6f0de0SAdrian Chadd 
2345f5c30c4eSAdrian Chadd 	bf->bf_state.bfs_rc[0].rix = rix;
2346eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].tries = try0;
2347eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].ratecode = txrate;
2348c1782ce0SAdrian Chadd 
2349c1782ce0SAdrian Chadd 	if (ismrr) {
2350eb6f0de0SAdrian Chadd 		int rix;
2351c1782ce0SAdrian Chadd 
2352b8e788a5SAdrian Chadd 		rix = ath_tx_findrix(sc, params->ibp_rate1);
2353eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[1].rix = rix;
2354eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[1].tries = params->ibp_try1;
2355c1782ce0SAdrian Chadd 
2356eb6f0de0SAdrian Chadd 		rix = ath_tx_findrix(sc, params->ibp_rate2);
2357eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[2].rix = rix;
2358eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[2].tries = params->ibp_try2;
2359eb6f0de0SAdrian Chadd 
2360eb6f0de0SAdrian Chadd 		rix = ath_tx_findrix(sc, params->ibp_rate3);
2361eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[3].rix = rix;
2362eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[3].tries = params->ibp_try3;
2363c1782ce0SAdrian Chadd 	}
2364eb6f0de0SAdrian Chadd 	/*
2365eb6f0de0SAdrian Chadd 	 * All the required rate control decisions have been made;
2366eb6f0de0SAdrian Chadd 	 * fill in the rc flags.
2367eb6f0de0SAdrian Chadd 	 */
2368eb6f0de0SAdrian Chadd 	ath_tx_rate_fill_rcflags(sc, bf);
2369b8e788a5SAdrian Chadd 
2370b8e788a5SAdrian Chadd 	/* NB: no buffered multicast in power save support */
2371eb6f0de0SAdrian Chadd 
2372eb6f0de0SAdrian Chadd 	/*
2373eb6f0de0SAdrian Chadd 	 * If we're overiding the ADDBA destination, dump directly
2374eb6f0de0SAdrian Chadd 	 * into the hardware queue, right after any pending
2375eb6f0de0SAdrian Chadd 	 * frames to that node are.
2376eb6f0de0SAdrian Chadd 	 */
2377eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: dooverride=%d\n",
2378eb6f0de0SAdrian Chadd 	    __func__, do_override);
2379eb6f0de0SAdrian Chadd 
238094eefcf1SAdrian Chadd #if 1
238122a3aee6SAdrian Chadd 	/*
238222a3aee6SAdrian Chadd 	 * Put addba frames in the right place in the right TID/HWQ.
238322a3aee6SAdrian Chadd 	 */
2384eb6f0de0SAdrian Chadd 	if (do_override) {
23854e81f27cSAdrian Chadd 		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
238622a3aee6SAdrian Chadd 		/*
238722a3aee6SAdrian Chadd 		 * XXX if it's addba frames, should we be leaking
238822a3aee6SAdrian Chadd 		 * them out via the frame leak method?
238922a3aee6SAdrian Chadd 		 * XXX for now let's not risk it; but we may wish
239022a3aee6SAdrian Chadd 		 * to investigate this later.
239122a3aee6SAdrian Chadd 		 */
2392eb6f0de0SAdrian Chadd 		ath_tx_xmit_normal(sc, sc->sc_ac2q[pri], bf);
239322a3aee6SAdrian Chadd 	} else if (ath_tx_should_swq_frame(sc, ATH_NODE(ni), m0,
239422a3aee6SAdrian Chadd 	    &queue_to_head)) {
2395eb6f0de0SAdrian Chadd 		/* Queue to software queue */
239622a3aee6SAdrian Chadd 		ath_tx_swq(sc, ni, sc->sc_ac2q[pri], queue_to_head, bf);
239722a3aee6SAdrian Chadd 	} else {
239822a3aee6SAdrian Chadd 		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
239922a3aee6SAdrian Chadd 		ath_tx_xmit_normal(sc, sc->sc_ac2q[pri], bf);
2400eb6f0de0SAdrian Chadd 	}
240194eefcf1SAdrian Chadd #else
240294eefcf1SAdrian Chadd 	/* Direct-dispatch to the hardware */
240394eefcf1SAdrian Chadd 	bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
240422a3aee6SAdrian Chadd 	/*
240522a3aee6SAdrian Chadd 	 * Update the current leak count if
240622a3aee6SAdrian Chadd 	 * we're leaking frames; and set the
240722a3aee6SAdrian Chadd 	 * MORE flag as appropriate.
240822a3aee6SAdrian Chadd 	 */
240922a3aee6SAdrian Chadd 	ath_tx_leak_count_update(sc, tid, bf);
241094eefcf1SAdrian Chadd 	ath_tx_xmit_normal(sc, sc->sc_ac2q[pri], bf);
241194eefcf1SAdrian Chadd #endif
2412b8e788a5SAdrian Chadd 	return 0;
2413b8e788a5SAdrian Chadd }
2414b8e788a5SAdrian Chadd 
2415eb6f0de0SAdrian Chadd /*
2416eb6f0de0SAdrian Chadd  * Send a raw frame.
2417eb6f0de0SAdrian Chadd  *
2418eb6f0de0SAdrian Chadd  * This can be called by net80211.
2419eb6f0de0SAdrian Chadd  */
2420b8e788a5SAdrian Chadd int
2421b8e788a5SAdrian Chadd ath_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2422b8e788a5SAdrian Chadd 	const struct ieee80211_bpf_params *params)
2423b8e788a5SAdrian Chadd {
2424b8e788a5SAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
24253797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
2426b8e788a5SAdrian Chadd 	struct ath_buf *bf;
24279c85ff91SAdrian Chadd 	struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
24289c85ff91SAdrian Chadd 	int error = 0;
2429b8e788a5SAdrian Chadd 
2430ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2431ef27340cSAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
243283bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_XMIT,
243383bbd5ebSRui Paulo 		    "%s: sc_inreset_cnt > 0; bailing\n", __func__);
2434ef27340cSAdrian Chadd 		error = EIO;
2435ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2436f5c30c4eSAdrian Chadd 		goto badbad;
2437ef27340cSAdrian Chadd 	}
2438ef27340cSAdrian Chadd 	sc->sc_txstart_cnt++;
2439ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2440ef27340cSAdrian Chadd 
2441f5c30c4eSAdrian Chadd 	/* Wake the hardware up already */
2442f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2443f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2444f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2445f5c30c4eSAdrian Chadd 
24461b5c5f5aSAdrian Chadd 	ATH_TX_LOCK(sc);
24471b5c5f5aSAdrian Chadd 
24487a79cebfSGleb Smirnoff 	if (!sc->sc_running || sc->sc_invalid) {
24497a79cebfSGleb Smirnoff 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: discard frame, r/i: %d/%d",
24507a79cebfSGleb Smirnoff 		    __func__, sc->sc_running, sc->sc_invalid);
2451b8e788a5SAdrian Chadd 		m_freem(m);
2452b8e788a5SAdrian Chadd 		error = ENETDOWN;
2453b8e788a5SAdrian Chadd 		goto bad;
2454b8e788a5SAdrian Chadd 	}
24559c85ff91SAdrian Chadd 
24569c85ff91SAdrian Chadd 	/*
24579c85ff91SAdrian Chadd 	 * Enforce how deep the multicast queue can grow.
24589c85ff91SAdrian Chadd 	 *
24599c85ff91SAdrian Chadd 	 * XXX duplicated in ath_tx_start().
24609c85ff91SAdrian Chadd 	 */
24619c85ff91SAdrian Chadd 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
246292e84e43SAdrian Chadd 		if (sc->sc_cabq->axq_depth + sc->sc_cabq->fifo.axq_depth
246392e84e43SAdrian Chadd 		    > sc->sc_txq_mcastq_maxdepth) {
24649c85ff91SAdrian Chadd 			sc->sc_stats.ast_tx_mcastq_overflow++;
24659c85ff91SAdrian Chadd 			error = ENOBUFS;
24669c85ff91SAdrian Chadd 		}
24679c85ff91SAdrian Chadd 
24689c85ff91SAdrian Chadd 		if (error != 0) {
24699c85ff91SAdrian Chadd 			m_freem(m);
24709c85ff91SAdrian Chadd 			goto bad;
24719c85ff91SAdrian Chadd 		}
24729c85ff91SAdrian Chadd 	}
24739c85ff91SAdrian Chadd 
2474b8e788a5SAdrian Chadd 	/*
2475b8e788a5SAdrian Chadd 	 * Grab a TX buffer and associated resources.
2476b8e788a5SAdrian Chadd 	 */
2477af33d486SAdrian Chadd 	bf = ath_getbuf(sc, ATH_BUFTYPE_MGMT);
2478b8e788a5SAdrian Chadd 	if (bf == NULL) {
2479b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_nobuf++;
2480b8e788a5SAdrian Chadd 		m_freem(m);
2481b8e788a5SAdrian Chadd 		error = ENOBUFS;
2482b8e788a5SAdrian Chadd 		goto bad;
2483b8e788a5SAdrian Chadd 	}
248403682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 3, "ath_raw_xmit: m=%p, params=%p, bf=%p\n",
248503682514SAdrian Chadd 	    m, params,  bf);
2486b8e788a5SAdrian Chadd 
2487b8e788a5SAdrian Chadd 	if (params == NULL) {
2488b8e788a5SAdrian Chadd 		/*
2489b8e788a5SAdrian Chadd 		 * Legacy path; interpret frame contents to decide
2490b8e788a5SAdrian Chadd 		 * precisely how to send the frame.
2491b8e788a5SAdrian Chadd 		 */
2492b8e788a5SAdrian Chadd 		if (ath_tx_start(sc, ni, bf, m)) {
2493b8e788a5SAdrian Chadd 			error = EIO;		/* XXX */
2494b8e788a5SAdrian Chadd 			goto bad2;
2495b8e788a5SAdrian Chadd 		}
2496b8e788a5SAdrian Chadd 	} else {
2497b8e788a5SAdrian Chadd 		/*
2498b8e788a5SAdrian Chadd 		 * Caller supplied explicit parameters to use in
2499b8e788a5SAdrian Chadd 		 * sending the frame.
2500b8e788a5SAdrian Chadd 		 */
2501b8e788a5SAdrian Chadd 		if (ath_tx_raw_start(sc, ni, bf, m, params)) {
2502b8e788a5SAdrian Chadd 			error = EIO;		/* XXX */
2503b8e788a5SAdrian Chadd 			goto bad2;
2504b8e788a5SAdrian Chadd 		}
2505b8e788a5SAdrian Chadd 	}
2506b8e788a5SAdrian Chadd 	sc->sc_wd_timer = 5;
2507b8e788a5SAdrian Chadd 	sc->sc_stats.ast_tx_raw++;
2508b8e788a5SAdrian Chadd 
2509548a605dSAdrian Chadd 	/*
2510548a605dSAdrian Chadd 	 * Update the TIM - if there's anything queued to the
2511548a605dSAdrian Chadd 	 * software queue and power save is enabled, we should
2512548a605dSAdrian Chadd 	 * set the TIM.
2513548a605dSAdrian Chadd 	 */
2514548a605dSAdrian Chadd 	ath_tx_update_tim(sc, ni, 1);
2515548a605dSAdrian Chadd 
2516974185bbSAdrian Chadd 	ATH_TX_UNLOCK(sc);
2517974185bbSAdrian Chadd 
2518ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2519ef27340cSAdrian Chadd 	sc->sc_txstart_cnt--;
2520ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2521ef27340cSAdrian Chadd 
2522f5c30c4eSAdrian Chadd 
2523f5c30c4eSAdrian Chadd 	/* Put the hardware back to sleep if required */
2524f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2525f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2526f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2527f5c30c4eSAdrian Chadd 
2528b8e788a5SAdrian Chadd 	return 0;
2529f5c30c4eSAdrian Chadd 
2530b8e788a5SAdrian Chadd bad2:
253103682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 3, "ath_raw_xmit: bad2: m=%p, params=%p, "
253203682514SAdrian Chadd 	    "bf=%p",
253303682514SAdrian Chadd 	    m,
253403682514SAdrian Chadd 	    params,
253503682514SAdrian Chadd 	    bf);
2536b8e788a5SAdrian Chadd 	ATH_TXBUF_LOCK(sc);
2537e1a50456SAdrian Chadd 	ath_returnbuf_head(sc, bf);
2538b8e788a5SAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
25391b5c5f5aSAdrian Chadd 
2540f5c30c4eSAdrian Chadd bad:
25411b5c5f5aSAdrian Chadd 	ATH_TX_UNLOCK(sc);
25421b5c5f5aSAdrian Chadd 
2543ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2544ef27340cSAdrian Chadd 	sc->sc_txstart_cnt--;
2545ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2546f5c30c4eSAdrian Chadd 
2547f5c30c4eSAdrian Chadd 	/* Put the hardware back to sleep if required */
2548f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
2549f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
2550f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
2551f5c30c4eSAdrian Chadd 
2552f5c30c4eSAdrian Chadd badbad:
255303682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 2, "ath_raw_xmit: bad0: m=%p, params=%p",
255403682514SAdrian Chadd 	    m, params);
2555b8e788a5SAdrian Chadd 	sc->sc_stats.ast_tx_raw_fail++;
2556ef27340cSAdrian Chadd 
2557b8e788a5SAdrian Chadd 	return error;
2558b8e788a5SAdrian Chadd }
2559eb6f0de0SAdrian Chadd 
2560eb6f0de0SAdrian Chadd /* Some helper functions */
2561eb6f0de0SAdrian Chadd 
2562eb6f0de0SAdrian Chadd /*
2563eb6f0de0SAdrian Chadd  * ADDBA (and potentially others) need to be placed in the same
2564eb6f0de0SAdrian Chadd  * hardware queue as the TID/node it's relating to. This is so
2565eb6f0de0SAdrian Chadd  * it goes out after any pending non-aggregate frames to the
2566eb6f0de0SAdrian Chadd  * same node/TID.
2567eb6f0de0SAdrian Chadd  *
2568eb6f0de0SAdrian Chadd  * If this isn't done, the ADDBA can go out before the frames
2569eb6f0de0SAdrian Chadd  * queued in hardware. Even though these frames have a sequence
2570eb6f0de0SAdrian Chadd  * number -earlier- than the ADDBA can be transmitted (but
2571eb6f0de0SAdrian Chadd  * no frames whose sequence numbers are after the ADDBA should
2572eb6f0de0SAdrian Chadd  * be!) they'll arrive after the ADDBA - and the receiving end
2573eb6f0de0SAdrian Chadd  * will simply drop them as being out of the BAW.
2574eb6f0de0SAdrian Chadd  *
2575eb6f0de0SAdrian Chadd  * The frames can't be appended to the TID software queue - it'll
2576eb6f0de0SAdrian Chadd  * never be sent out. So these frames have to be directly
2577eb6f0de0SAdrian Chadd  * dispatched to the hardware, rather than queued in software.
2578eb6f0de0SAdrian Chadd  * So if this function returns true, the TXQ has to be
2579eb6f0de0SAdrian Chadd  * overridden and it has to be directly dispatched.
2580eb6f0de0SAdrian Chadd  *
2581eb6f0de0SAdrian Chadd  * It's a dirty hack, but someone's gotta do it.
2582eb6f0de0SAdrian Chadd  */
2583eb6f0de0SAdrian Chadd 
2584eb6f0de0SAdrian Chadd /*
2585eb6f0de0SAdrian Chadd  * XXX doesn't belong here!
2586eb6f0de0SAdrian Chadd  */
2587eb6f0de0SAdrian Chadd static int
2588eb6f0de0SAdrian Chadd ieee80211_is_action(struct ieee80211_frame *wh)
2589eb6f0de0SAdrian Chadd {
2590eb6f0de0SAdrian Chadd 	/* Type: Management frame? */
2591eb6f0de0SAdrian Chadd 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) !=
2592eb6f0de0SAdrian Chadd 	    IEEE80211_FC0_TYPE_MGT)
2593eb6f0de0SAdrian Chadd 		return 0;
2594eb6f0de0SAdrian Chadd 
2595eb6f0de0SAdrian Chadd 	/* Subtype: Action frame? */
2596eb6f0de0SAdrian Chadd 	if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) !=
2597eb6f0de0SAdrian Chadd 	    IEEE80211_FC0_SUBTYPE_ACTION)
2598eb6f0de0SAdrian Chadd 		return 0;
2599eb6f0de0SAdrian Chadd 
2600eb6f0de0SAdrian Chadd 	return 1;
2601eb6f0de0SAdrian Chadd }
2602eb6f0de0SAdrian Chadd 
2603eb6f0de0SAdrian Chadd #define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
2604eb6f0de0SAdrian Chadd /*
2605eb6f0de0SAdrian Chadd  * Return an alternate TID for ADDBA request frames.
2606eb6f0de0SAdrian Chadd  *
2607eb6f0de0SAdrian Chadd  * Yes, this likely should be done in the net80211 layer.
2608eb6f0de0SAdrian Chadd  */
2609eb6f0de0SAdrian Chadd static int
2610eb6f0de0SAdrian Chadd ath_tx_action_frame_override_queue(struct ath_softc *sc,
2611eb6f0de0SAdrian Chadd     struct ieee80211_node *ni,
2612eb6f0de0SAdrian Chadd     struct mbuf *m0, int *tid)
2613eb6f0de0SAdrian Chadd {
2614eb6f0de0SAdrian Chadd 	struct ieee80211_frame *wh = mtod(m0, struct ieee80211_frame *);
2615eb6f0de0SAdrian Chadd 	struct ieee80211_action_ba_addbarequest *ia;
2616eb6f0de0SAdrian Chadd 	uint8_t *frm;
2617eb6f0de0SAdrian Chadd 	uint16_t baparamset;
2618eb6f0de0SAdrian Chadd 
2619eb6f0de0SAdrian Chadd 	/* Not action frame? Bail */
2620eb6f0de0SAdrian Chadd 	if (! ieee80211_is_action(wh))
2621eb6f0de0SAdrian Chadd 		return 0;
2622eb6f0de0SAdrian Chadd 
2623eb6f0de0SAdrian Chadd 	/* XXX Not needed for frames we send? */
2624eb6f0de0SAdrian Chadd #if 0
2625eb6f0de0SAdrian Chadd 	/* Correct length? */
2626eb6f0de0SAdrian Chadd 	if (! ieee80211_parse_action(ni, m))
2627eb6f0de0SAdrian Chadd 		return 0;
2628eb6f0de0SAdrian Chadd #endif
2629eb6f0de0SAdrian Chadd 
2630eb6f0de0SAdrian Chadd 	/* Extract out action frame */
2631eb6f0de0SAdrian Chadd 	frm = (u_int8_t *)&wh[1];
2632eb6f0de0SAdrian Chadd 	ia = (struct ieee80211_action_ba_addbarequest *) frm;
2633eb6f0de0SAdrian Chadd 
2634eb6f0de0SAdrian Chadd 	/* Not ADDBA? Bail */
2635eb6f0de0SAdrian Chadd 	if (ia->rq_header.ia_category != IEEE80211_ACTION_CAT_BA)
2636eb6f0de0SAdrian Chadd 		return 0;
2637eb6f0de0SAdrian Chadd 	if (ia->rq_header.ia_action != IEEE80211_ACTION_BA_ADDBA_REQUEST)
2638eb6f0de0SAdrian Chadd 		return 0;
2639eb6f0de0SAdrian Chadd 
2640eb6f0de0SAdrian Chadd 	/* Extract TID, return it */
2641eb6f0de0SAdrian Chadd 	baparamset = le16toh(ia->rq_baparamset);
2642eb6f0de0SAdrian Chadd 	*tid = (int) MS(baparamset, IEEE80211_BAPS_TID);
2643eb6f0de0SAdrian Chadd 
2644eb6f0de0SAdrian Chadd 	return 1;
2645eb6f0de0SAdrian Chadd }
2646eb6f0de0SAdrian Chadd #undef	MS
2647eb6f0de0SAdrian Chadd 
2648eb6f0de0SAdrian Chadd /* Per-node software queue operations */
2649eb6f0de0SAdrian Chadd 
2650eb6f0de0SAdrian Chadd /*
2651eb6f0de0SAdrian Chadd  * Add the current packet to the given BAW.
2652eb6f0de0SAdrian Chadd  * It is assumed that the current packet
2653eb6f0de0SAdrian Chadd  *
2654eb6f0de0SAdrian Chadd  * + fits inside the BAW;
2655eb6f0de0SAdrian Chadd  * + already has had a sequence number allocated.
2656eb6f0de0SAdrian Chadd  *
2657eb6f0de0SAdrian Chadd  * Since the BAW status may be modified by both the ath task and
2658eb6f0de0SAdrian Chadd  * the net80211/ifnet contexts, the TID must be locked.
2659eb6f0de0SAdrian Chadd  */
2660eb6f0de0SAdrian Chadd void
2661eb6f0de0SAdrian Chadd ath_tx_addto_baw(struct ath_softc *sc, struct ath_node *an,
2662eb6f0de0SAdrian Chadd     struct ath_tid *tid, struct ath_buf *bf)
2663eb6f0de0SAdrian Chadd {
2664eb6f0de0SAdrian Chadd 	int index, cindex;
2665eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
2666eb6f0de0SAdrian Chadd 
2667375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
2668eb6f0de0SAdrian Chadd 
2669eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_isretried)
2670eb6f0de0SAdrian Chadd 		return;
2671eb6f0de0SAdrian Chadd 
2672c7c07341SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
2673c7c07341SAdrian Chadd 
26747561cb5cSAdrian Chadd 	if (! bf->bf_state.bfs_dobaw) {
267583bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
26767561cb5cSAdrian Chadd 		    "%s: dobaw=0, seqno=%d, window %d:%d\n",
267783bbd5ebSRui Paulo 		    __func__, SEQNO(bf->bf_state.bfs_seqno),
267883bbd5ebSRui Paulo 		    tap->txa_start, tap->txa_wnd);
26797561cb5cSAdrian Chadd 	}
26807561cb5cSAdrian Chadd 
2681eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_addedbaw)
268283bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2683a108d2d6SAdrian Chadd 		    "%s: re-added? tid=%d, seqno %d; window %d:%d; "
2684d4365d16SAdrian Chadd 		    "baw head=%d tail=%d\n",
2685a108d2d6SAdrian Chadd 		    __func__, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
2686d4365d16SAdrian Chadd 		    tap->txa_start, tap->txa_wnd, tid->baw_head,
2687d4365d16SAdrian Chadd 		    tid->baw_tail);
2688eb6f0de0SAdrian Chadd 
2689eb6f0de0SAdrian Chadd 	/*
26907561cb5cSAdrian Chadd 	 * Verify that the given sequence number is not outside of the
26917561cb5cSAdrian Chadd 	 * BAW.  Complain loudly if that's the case.
26927561cb5cSAdrian Chadd 	 */
26937561cb5cSAdrian Chadd 	if (! BAW_WITHIN(tap->txa_start, tap->txa_wnd,
26947561cb5cSAdrian Chadd 	    SEQNO(bf->bf_state.bfs_seqno))) {
269583bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
26967561cb5cSAdrian Chadd 		    "%s: bf=%p: outside of BAW?? tid=%d, seqno %d; window %d:%d; "
26977561cb5cSAdrian Chadd 		    "baw head=%d tail=%d\n",
26987561cb5cSAdrian Chadd 		    __func__, bf, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
26997561cb5cSAdrian Chadd 		    tap->txa_start, tap->txa_wnd, tid->baw_head,
27007561cb5cSAdrian Chadd 		    tid->baw_tail);
27017561cb5cSAdrian Chadd 	}
27027561cb5cSAdrian Chadd 
27037561cb5cSAdrian Chadd 	/*
2704eb6f0de0SAdrian Chadd 	 * ni->ni_txseqs[] is the currently allocated seqno.
2705eb6f0de0SAdrian Chadd 	 * the txa state contains the current baw start.
2706eb6f0de0SAdrian Chadd 	 */
2707eb6f0de0SAdrian Chadd 	index  = ATH_BA_INDEX(tap->txa_start, SEQNO(bf->bf_state.bfs_seqno));
2708eb6f0de0SAdrian Chadd 	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
2709eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2710a108d2d6SAdrian Chadd 	    "%s: tid=%d, seqno %d; window %d:%d; index=%d cindex=%d "
2711d4365d16SAdrian Chadd 	    "baw head=%d tail=%d\n",
2712a108d2d6SAdrian Chadd 	    __func__, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
2713d4365d16SAdrian Chadd 	    tap->txa_start, tap->txa_wnd, index, cindex, tid->baw_head,
2714d4365d16SAdrian Chadd 	    tid->baw_tail);
2715eb6f0de0SAdrian Chadd 
2716eb6f0de0SAdrian Chadd 
2717eb6f0de0SAdrian Chadd #if 0
2718eb6f0de0SAdrian Chadd 	assert(tid->tx_buf[cindex] == NULL);
2719eb6f0de0SAdrian Chadd #endif
2720eb6f0de0SAdrian Chadd 	if (tid->tx_buf[cindex] != NULL) {
272183bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2722eb6f0de0SAdrian Chadd 		    "%s: ba packet dup (index=%d, cindex=%d, "
2723eb6f0de0SAdrian Chadd 		    "head=%d, tail=%d)\n",
2724eb6f0de0SAdrian Chadd 		    __func__, index, cindex, tid->baw_head, tid->baw_tail);
272583bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2726eb6f0de0SAdrian Chadd 		    "%s: BA bf: %p; seqno=%d ; new bf: %p; seqno=%d\n",
2727eb6f0de0SAdrian Chadd 		    __func__,
2728eb6f0de0SAdrian Chadd 		    tid->tx_buf[cindex],
2729eb6f0de0SAdrian Chadd 		    SEQNO(tid->tx_buf[cindex]->bf_state.bfs_seqno),
2730eb6f0de0SAdrian Chadd 		    bf,
2731eb6f0de0SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno)
2732eb6f0de0SAdrian Chadd 		);
2733eb6f0de0SAdrian Chadd 	}
2734eb6f0de0SAdrian Chadd 	tid->tx_buf[cindex] = bf;
2735eb6f0de0SAdrian Chadd 
2736d4365d16SAdrian Chadd 	if (index >= ((tid->baw_tail - tid->baw_head) &
2737d4365d16SAdrian Chadd 	    (ATH_TID_MAX_BUFS - 1))) {
2738eb6f0de0SAdrian Chadd 		tid->baw_tail = cindex;
2739eb6f0de0SAdrian Chadd 		INCR(tid->baw_tail, ATH_TID_MAX_BUFS);
2740eb6f0de0SAdrian Chadd 	}
2741eb6f0de0SAdrian Chadd }
2742eb6f0de0SAdrian Chadd 
2743eb6f0de0SAdrian Chadd /*
274438962489SAdrian Chadd  * Flip the BAW buffer entry over from the existing one to the new one.
274538962489SAdrian Chadd  *
274638962489SAdrian Chadd  * When software retransmitting a (sub-)frame, it is entirely possible that
274738962489SAdrian Chadd  * the frame ath_buf is marked as BUSY and can't be immediately reused.
274838962489SAdrian Chadd  * In that instance the buffer is cloned and the new buffer is used for
274938962489SAdrian Chadd  * retransmit. We thus need to update the ath_buf slot in the BAW buf
275038962489SAdrian Chadd  * tracking array to maintain consistency.
275138962489SAdrian Chadd  */
275238962489SAdrian Chadd static void
275338962489SAdrian Chadd ath_tx_switch_baw_buf(struct ath_softc *sc, struct ath_node *an,
275438962489SAdrian Chadd     struct ath_tid *tid, struct ath_buf *old_bf, struct ath_buf *new_bf)
275538962489SAdrian Chadd {
275638962489SAdrian Chadd 	int index, cindex;
275738962489SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
275838962489SAdrian Chadd 	int seqno = SEQNO(old_bf->bf_state.bfs_seqno);
275938962489SAdrian Chadd 
2760375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
276138962489SAdrian Chadd 
276238962489SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
276338962489SAdrian Chadd 	index  = ATH_BA_INDEX(tap->txa_start, seqno);
276438962489SAdrian Chadd 	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
276538962489SAdrian Chadd 
276638962489SAdrian Chadd 	/*
276738962489SAdrian Chadd 	 * Just warn for now; if it happens then we should find out
276838962489SAdrian Chadd 	 * about it. It's highly likely the aggregation session will
276938962489SAdrian Chadd 	 * soon hang.
277038962489SAdrian Chadd 	 */
277138962489SAdrian Chadd 	if (old_bf->bf_state.bfs_seqno != new_bf->bf_state.bfs_seqno) {
277283bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
277383bbd5ebSRui Paulo 		    "%s: retransmitted buffer"
277438962489SAdrian Chadd 		    " has mismatching seqno's, BA session may hang.\n",
277538962489SAdrian Chadd 		    __func__);
277683bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
277783bbd5ebSRui Paulo 		    "%s: old seqno=%d, new_seqno=%d\n", __func__,
277883bbd5ebSRui Paulo 		    old_bf->bf_state.bfs_seqno, new_bf->bf_state.bfs_seqno);
277938962489SAdrian Chadd 	}
278038962489SAdrian Chadd 
278138962489SAdrian Chadd 	if (tid->tx_buf[cindex] != old_bf) {
278283bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
278383bbd5ebSRui Paulo 		    "%s: ath_buf pointer incorrect; "
278483bbd5ebSRui Paulo 		    " has m BA session may hang.\n", __func__);
278583bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
278683bbd5ebSRui Paulo 		    "%s: old bf=%p, new bf=%p\n", __func__, old_bf, new_bf);
278738962489SAdrian Chadd 	}
278838962489SAdrian Chadd 
278938962489SAdrian Chadd 	tid->tx_buf[cindex] = new_bf;
279038962489SAdrian Chadd }
279138962489SAdrian Chadd 
279238962489SAdrian Chadd /*
2793eb6f0de0SAdrian Chadd  * seq_start - left edge of BAW
2794eb6f0de0SAdrian Chadd  * seq_next - current/next sequence number to allocate
2795eb6f0de0SAdrian Chadd  *
2796eb6f0de0SAdrian Chadd  * Since the BAW status may be modified by both the ath task and
2797eb6f0de0SAdrian Chadd  * the net80211/ifnet contexts, the TID must be locked.
2798eb6f0de0SAdrian Chadd  */
2799eb6f0de0SAdrian Chadd static void
2800eb6f0de0SAdrian Chadd ath_tx_update_baw(struct ath_softc *sc, struct ath_node *an,
2801eb6f0de0SAdrian Chadd     struct ath_tid *tid, const struct ath_buf *bf)
2802eb6f0de0SAdrian Chadd {
2803eb6f0de0SAdrian Chadd 	int index, cindex;
2804eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
2805eb6f0de0SAdrian Chadd 	int seqno = SEQNO(bf->bf_state.bfs_seqno);
2806eb6f0de0SAdrian Chadd 
2807375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
2808eb6f0de0SAdrian Chadd 
2809eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
2810eb6f0de0SAdrian Chadd 	index  = ATH_BA_INDEX(tap->txa_start, seqno);
2811eb6f0de0SAdrian Chadd 	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
2812eb6f0de0SAdrian Chadd 
2813eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2814a108d2d6SAdrian Chadd 	    "%s: tid=%d, baw=%d:%d, seqno=%d, index=%d, cindex=%d, "
2815d4365d16SAdrian Chadd 	    "baw head=%d, tail=%d\n",
2816a108d2d6SAdrian Chadd 	    __func__, tid->tid, tap->txa_start, tap->txa_wnd, seqno, index,
2817eb6f0de0SAdrian Chadd 	    cindex, tid->baw_head, tid->baw_tail);
2818eb6f0de0SAdrian Chadd 
2819eb6f0de0SAdrian Chadd 	/*
2820eb6f0de0SAdrian Chadd 	 * If this occurs then we have a big problem - something else
2821eb6f0de0SAdrian Chadd 	 * has slid tap->txa_start along without updating the BAW
2822eb6f0de0SAdrian Chadd 	 * tracking start/end pointers. Thus the TX BAW state is now
2823eb6f0de0SAdrian Chadd 	 * completely busted.
2824eb6f0de0SAdrian Chadd 	 *
2825eb6f0de0SAdrian Chadd 	 * But for now, since I haven't yet fixed TDMA and buffer cloning,
2826eb6f0de0SAdrian Chadd 	 * it's quite possible that a cloned buffer is making its way
2827eb6f0de0SAdrian Chadd 	 * here and causing it to fire off. Disable TDMA for now.
2828eb6f0de0SAdrian Chadd 	 */
2829eb6f0de0SAdrian Chadd 	if (tid->tx_buf[cindex] != bf) {
283083bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2831eb6f0de0SAdrian Chadd 		    "%s: comp bf=%p, seq=%d; slot bf=%p, seqno=%d\n",
283283bbd5ebSRui Paulo 		    __func__, bf, SEQNO(bf->bf_state.bfs_seqno),
2833eb6f0de0SAdrian Chadd 		    tid->tx_buf[cindex],
28343527f6a9SAdrian Chadd 		    (tid->tx_buf[cindex] != NULL) ?
28353527f6a9SAdrian Chadd 		      SEQNO(tid->tx_buf[cindex]->bf_state.bfs_seqno) : -1);
2836eb6f0de0SAdrian Chadd 	}
2837eb6f0de0SAdrian Chadd 
2838eb6f0de0SAdrian Chadd 	tid->tx_buf[cindex] = NULL;
2839eb6f0de0SAdrian Chadd 
2840d4365d16SAdrian Chadd 	while (tid->baw_head != tid->baw_tail &&
2841d4365d16SAdrian Chadd 	    !tid->tx_buf[tid->baw_head]) {
2842eb6f0de0SAdrian Chadd 		INCR(tap->txa_start, IEEE80211_SEQ_RANGE);
2843eb6f0de0SAdrian Chadd 		INCR(tid->baw_head, ATH_TID_MAX_BUFS);
2844eb6f0de0SAdrian Chadd 	}
2845d4365d16SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
284642fdd8e7SAdrian Chadd 	    "%s: tid=%d: baw is now %d:%d, baw head=%d\n",
284742fdd8e7SAdrian Chadd 	    __func__, tid->tid, tap->txa_start, tap->txa_wnd, tid->baw_head);
2848eb6f0de0SAdrian Chadd }
2849eb6f0de0SAdrian Chadd 
285022a3aee6SAdrian Chadd static void
285122a3aee6SAdrian Chadd ath_tx_leak_count_update(struct ath_softc *sc, struct ath_tid *tid,
285222a3aee6SAdrian Chadd     struct ath_buf *bf)
285322a3aee6SAdrian Chadd {
285422a3aee6SAdrian Chadd 	struct ieee80211_frame *wh;
285522a3aee6SAdrian Chadd 
285622a3aee6SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
285722a3aee6SAdrian Chadd 
285822a3aee6SAdrian Chadd 	if (tid->an->an_leak_count > 0) {
285922a3aee6SAdrian Chadd 		wh = mtod(bf->bf_m, struct ieee80211_frame *);
286022a3aee6SAdrian Chadd 
286122a3aee6SAdrian Chadd 		/*
286222a3aee6SAdrian Chadd 		 * Update MORE based on the software/net80211 queue states.
286322a3aee6SAdrian Chadd 		 */
286422a3aee6SAdrian Chadd 		if ((tid->an->an_stack_psq > 0)
286522a3aee6SAdrian Chadd 		    || (tid->an->an_swq_depth > 0))
286622a3aee6SAdrian Chadd 			wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
286722a3aee6SAdrian Chadd 		else
286822a3aee6SAdrian Chadd 			wh->i_fc[1] &= ~IEEE80211_FC1_MORE_DATA;
286922a3aee6SAdrian Chadd 
287022a3aee6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
287122a3aee6SAdrian Chadd 		    "%s: %6D: leak count = %d, psq=%d, swq=%d, MORE=%d\n",
287222a3aee6SAdrian Chadd 		    __func__,
287322a3aee6SAdrian Chadd 		    tid->an->an_node.ni_macaddr,
287422a3aee6SAdrian Chadd 		    ":",
287522a3aee6SAdrian Chadd 		    tid->an->an_leak_count,
287622a3aee6SAdrian Chadd 		    tid->an->an_stack_psq,
287722a3aee6SAdrian Chadd 		    tid->an->an_swq_depth,
287822a3aee6SAdrian Chadd 		    !! (wh->i_fc[1] & IEEE80211_FC1_MORE_DATA));
287922a3aee6SAdrian Chadd 
288022a3aee6SAdrian Chadd 		/*
288122a3aee6SAdrian Chadd 		 * Re-sync the underlying buffer.
288222a3aee6SAdrian Chadd 		 */
288322a3aee6SAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
288422a3aee6SAdrian Chadd 		    BUS_DMASYNC_PREWRITE);
288522a3aee6SAdrian Chadd 
288622a3aee6SAdrian Chadd 		tid->an->an_leak_count --;
288722a3aee6SAdrian Chadd 	}
288822a3aee6SAdrian Chadd }
288922a3aee6SAdrian Chadd 
289022a3aee6SAdrian Chadd static int
289122a3aee6SAdrian Chadd ath_tx_tid_can_tx_or_sched(struct ath_softc *sc, struct ath_tid *tid)
289222a3aee6SAdrian Chadd {
289322a3aee6SAdrian Chadd 
289422a3aee6SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
289522a3aee6SAdrian Chadd 
289622a3aee6SAdrian Chadd 	if (tid->an->an_leak_count > 0) {
289722a3aee6SAdrian Chadd 		return (1);
289822a3aee6SAdrian Chadd 	}
289922a3aee6SAdrian Chadd 	if (tid->paused)
290022a3aee6SAdrian Chadd 		return (0);
290122a3aee6SAdrian Chadd 	return (1);
290222a3aee6SAdrian Chadd }
290322a3aee6SAdrian Chadd 
2904eb6f0de0SAdrian Chadd /*
2905eb6f0de0SAdrian Chadd  * Mark the current node/TID as ready to TX.
2906eb6f0de0SAdrian Chadd  *
2907eb6f0de0SAdrian Chadd  * This is done to make it easy for the software scheduler to
2908eb6f0de0SAdrian Chadd  * find which nodes have data to send.
2909eb6f0de0SAdrian Chadd  *
2910eb6f0de0SAdrian Chadd  * The TXQ lock must be held.
2911eb6f0de0SAdrian Chadd  */
291222a3aee6SAdrian Chadd void
2913eb6f0de0SAdrian Chadd ath_tx_tid_sched(struct ath_softc *sc, struct ath_tid *tid)
2914eb6f0de0SAdrian Chadd {
2915eb6f0de0SAdrian Chadd 	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
2916eb6f0de0SAdrian Chadd 
2917375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
2918eb6f0de0SAdrian Chadd 
291922a3aee6SAdrian Chadd 	/*
292022a3aee6SAdrian Chadd 	 * If we are leaking out a frame to this destination
292122a3aee6SAdrian Chadd 	 * for PS-POLL, ensure that we allow scheduling to
292222a3aee6SAdrian Chadd 	 * occur.
292322a3aee6SAdrian Chadd 	 */
292422a3aee6SAdrian Chadd 	if (! ath_tx_tid_can_tx_or_sched(sc, tid))
2925eb6f0de0SAdrian Chadd 		return;		/* paused, can't schedule yet */
2926eb6f0de0SAdrian Chadd 
2927eb6f0de0SAdrian Chadd 	if (tid->sched)
2928eb6f0de0SAdrian Chadd 		return;		/* already scheduled */
2929eb6f0de0SAdrian Chadd 
2930eb6f0de0SAdrian Chadd 	tid->sched = 1;
2931eb6f0de0SAdrian Chadd 
293222a3aee6SAdrian Chadd #if 0
293322a3aee6SAdrian Chadd 	/*
293422a3aee6SAdrian Chadd 	 * If this is a sleeping node we're leaking to, given
293522a3aee6SAdrian Chadd 	 * it a higher priority.  This is so bad for QoS it hurts.
293622a3aee6SAdrian Chadd 	 */
293722a3aee6SAdrian Chadd 	if (tid->an->an_leak_count) {
293822a3aee6SAdrian Chadd 		TAILQ_INSERT_HEAD(&txq->axq_tidq, tid, axq_qelem);
293922a3aee6SAdrian Chadd 	} else {
294022a3aee6SAdrian Chadd 		TAILQ_INSERT_TAIL(&txq->axq_tidq, tid, axq_qelem);
294122a3aee6SAdrian Chadd 	}
294222a3aee6SAdrian Chadd #endif
294322a3aee6SAdrian Chadd 
294422a3aee6SAdrian Chadd 	/*
294522a3aee6SAdrian Chadd 	 * We can't do the above - it'll confuse the TXQ software
294622a3aee6SAdrian Chadd 	 * scheduler which will keep checking the _head_ TID
294722a3aee6SAdrian Chadd 	 * in the list to see if it has traffic.  If we queue
294822a3aee6SAdrian Chadd 	 * a TID to the head of the list and it doesn't transmit,
294922a3aee6SAdrian Chadd 	 * we'll check it again.
295022a3aee6SAdrian Chadd 	 *
295122a3aee6SAdrian Chadd 	 * So, get the rest of this leaking frames support working
295222a3aee6SAdrian Chadd 	 * and reliable first and _then_ optimise it so they're
295322a3aee6SAdrian Chadd 	 * pushed out in front of any other pending software
295422a3aee6SAdrian Chadd 	 * queued nodes.
295522a3aee6SAdrian Chadd 	 */
2956eb6f0de0SAdrian Chadd 	TAILQ_INSERT_TAIL(&txq->axq_tidq, tid, axq_qelem);
2957eb6f0de0SAdrian Chadd }
2958eb6f0de0SAdrian Chadd 
2959eb6f0de0SAdrian Chadd /*
2960eb6f0de0SAdrian Chadd  * Mark the current node as no longer needing to be polled for
2961eb6f0de0SAdrian Chadd  * TX packets.
2962eb6f0de0SAdrian Chadd  *
2963eb6f0de0SAdrian Chadd  * The TXQ lock must be held.
2964eb6f0de0SAdrian Chadd  */
2965eb6f0de0SAdrian Chadd static void
2966eb6f0de0SAdrian Chadd ath_tx_tid_unsched(struct ath_softc *sc, struct ath_tid *tid)
2967eb6f0de0SAdrian Chadd {
2968eb6f0de0SAdrian Chadd 	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
2969eb6f0de0SAdrian Chadd 
2970375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
2971eb6f0de0SAdrian Chadd 
2972eb6f0de0SAdrian Chadd 	if (tid->sched == 0)
2973eb6f0de0SAdrian Chadd 		return;
2974eb6f0de0SAdrian Chadd 
2975eb6f0de0SAdrian Chadd 	tid->sched = 0;
2976eb6f0de0SAdrian Chadd 	TAILQ_REMOVE(&txq->axq_tidq, tid, axq_qelem);
2977eb6f0de0SAdrian Chadd }
2978eb6f0de0SAdrian Chadd 
2979eb6f0de0SAdrian Chadd /*
2980eb6f0de0SAdrian Chadd  * Assign a sequence number manually to the given frame.
2981eb6f0de0SAdrian Chadd  *
2982eb6f0de0SAdrian Chadd  * This should only be called for A-MPDU TX frames.
298315e58d4dSAdrian Chadd  *
298415e58d4dSAdrian Chadd  * Note: for group addressed frames, the sequence number
298515e58d4dSAdrian Chadd  * should be from NONQOS_TID, and net80211 should have
298615e58d4dSAdrian Chadd  * already assigned it for us.
2987eb6f0de0SAdrian Chadd  */
2988a108d2d6SAdrian Chadd static ieee80211_seq
2989eb6f0de0SAdrian Chadd ath_tx_tid_seqno_assign(struct ath_softc *sc, struct ieee80211_node *ni,
2990eb6f0de0SAdrian Chadd     struct ath_buf *bf, struct mbuf *m0)
2991eb6f0de0SAdrian Chadd {
2992eb6f0de0SAdrian Chadd 	struct ieee80211_frame *wh;
299315e58d4dSAdrian Chadd 	int tid;
2994eb6f0de0SAdrian Chadd 	ieee80211_seq seqno;
2995eb6f0de0SAdrian Chadd 	uint8_t subtype;
2996eb6f0de0SAdrian Chadd 
2997eb6f0de0SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
299815e58d4dSAdrian Chadd 	tid = ieee80211_gettid(wh);
299915e58d4dSAdrian Chadd 
300015e58d4dSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d, qos has seq=%d\n",
300115e58d4dSAdrian Chadd 	    __func__, tid, IEEE80211_QOS_HAS_SEQ(wh));
3002eb6f0de0SAdrian Chadd 
3003eb6f0de0SAdrian Chadd 	/* XXX Is it a control frame? Ignore */
3004eb6f0de0SAdrian Chadd 
3005eb6f0de0SAdrian Chadd 	/* Does the packet require a sequence number? */
3006eb6f0de0SAdrian Chadd 	if (! IEEE80211_QOS_HAS_SEQ(wh))
3007eb6f0de0SAdrian Chadd 		return -1;
3008eb6f0de0SAdrian Chadd 
3009375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
30107561cb5cSAdrian Chadd 
3011eb6f0de0SAdrian Chadd 	/*
3012eb6f0de0SAdrian Chadd 	 * Is it a QOS NULL Data frame? Give it a sequence number from
3013eb6f0de0SAdrian Chadd 	 * the default TID (IEEE80211_NONQOS_TID.)
3014eb6f0de0SAdrian Chadd 	 *
3015eb6f0de0SAdrian Chadd 	 * The RX path of everything I've looked at doesn't include the NULL
3016eb6f0de0SAdrian Chadd 	 * data frame sequence number in the aggregation state updates, so
3017eb6f0de0SAdrian Chadd 	 * assigning it a sequence number there will cause a BAW hole on the
3018eb6f0de0SAdrian Chadd 	 * RX side.
3019eb6f0de0SAdrian Chadd 	 */
3020eb6f0de0SAdrian Chadd 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
3021eb6f0de0SAdrian Chadd 	if (subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL) {
30227561cb5cSAdrian Chadd 		/* XXX no locking for this TID? This is a bit of a problem. */
3023eb6f0de0SAdrian Chadd 		seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID];
3024eb6f0de0SAdrian Chadd 		INCR(ni->ni_txseqs[IEEE80211_NONQOS_TID], IEEE80211_SEQ_RANGE);
302515e58d4dSAdrian Chadd 	} else if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
302615e58d4dSAdrian Chadd 		/*
302715e58d4dSAdrian Chadd 		 * group addressed frames get a sequence number from
302815e58d4dSAdrian Chadd 		 * a different sequence number space.
302915e58d4dSAdrian Chadd 		 */
303015e58d4dSAdrian Chadd 		seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID];
303115e58d4dSAdrian Chadd 		INCR(ni->ni_txseqs[IEEE80211_NONQOS_TID], IEEE80211_SEQ_RANGE);
3032eb6f0de0SAdrian Chadd 	} else {
3033eb6f0de0SAdrian Chadd 		/* Manually assign sequence number */
3034eb6f0de0SAdrian Chadd 		seqno = ni->ni_txseqs[tid];
3035eb6f0de0SAdrian Chadd 		INCR(ni->ni_txseqs[tid], IEEE80211_SEQ_RANGE);
3036eb6f0de0SAdrian Chadd 	}
3037eb6f0de0SAdrian Chadd 	*(uint16_t *)&wh->i_seq[0] = htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
3038eb6f0de0SAdrian Chadd 	M_SEQNO_SET(m0, seqno);
3039eb6f0de0SAdrian Chadd 
3040eb6f0de0SAdrian Chadd 	/* Return so caller can do something with it if needed */
304139d54676SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX,
304239d54676SAdrian Chadd 	    "%s:  -> subtype=0x%x, tid=%d, seqno=%d\n",
304339d54676SAdrian Chadd 	    __func__, subtype, tid, seqno);
3044eb6f0de0SAdrian Chadd 	return seqno;
3045eb6f0de0SAdrian Chadd }
3046eb6f0de0SAdrian Chadd 
3047eb6f0de0SAdrian Chadd /*
3048eb6f0de0SAdrian Chadd  * Attempt to direct dispatch an aggregate frame to hardware.
3049eb6f0de0SAdrian Chadd  * If the frame is out of BAW, queue.
3050eb6f0de0SAdrian Chadd  * Otherwise, schedule it as a single frame.
3051eb6f0de0SAdrian Chadd  */
3052eb6f0de0SAdrian Chadd static void
305346634305SAdrian Chadd ath_tx_xmit_aggr(struct ath_softc *sc, struct ath_node *an,
305446634305SAdrian Chadd     struct ath_txq *txq, struct ath_buf *bf)
3055eb6f0de0SAdrian Chadd {
3056eb6f0de0SAdrian Chadd 	struct ath_tid *tid = &an->an_tid[bf->bf_state.bfs_tid];
3057eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
3058eb6f0de0SAdrian Chadd 
3059375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
3060eb6f0de0SAdrian Chadd 
3061eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
3062eb6f0de0SAdrian Chadd 
3063eb6f0de0SAdrian Chadd 	/* paused? queue */
306422a3aee6SAdrian Chadd 	if (! ath_tx_tid_can_tx_or_sched(sc, tid)) {
30653e6cc97fSAdrian Chadd 		ATH_TID_INSERT_HEAD(tid, bf, bf_list);
30660f04c5a2SAdrian Chadd 		/* XXX don't sched - we're paused! */
3067eb6f0de0SAdrian Chadd 		return;
3068eb6f0de0SAdrian Chadd 	}
3069eb6f0de0SAdrian Chadd 
3070eb6f0de0SAdrian Chadd 	/* outside baw? queue */
3071eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_dobaw &&
3072eb6f0de0SAdrian Chadd 	    (! BAW_WITHIN(tap->txa_start, tap->txa_wnd,
3073eb6f0de0SAdrian Chadd 	    SEQNO(bf->bf_state.bfs_seqno)))) {
30743e6cc97fSAdrian Chadd 		ATH_TID_INSERT_HEAD(tid, bf, bf_list);
3075eb6f0de0SAdrian Chadd 		ath_tx_tid_sched(sc, tid);
3076eb6f0de0SAdrian Chadd 		return;
3077eb6f0de0SAdrian Chadd 	}
3078eb6f0de0SAdrian Chadd 
30792a9f83afSAdrian Chadd 	/*
30802a9f83afSAdrian Chadd 	 * This is a temporary check and should be removed once
30812a9f83afSAdrian Chadd 	 * all the relevant code paths have been fixed.
30822a9f83afSAdrian Chadd 	 *
30832a9f83afSAdrian Chadd 	 * During aggregate retries, it's possible that the head
30842a9f83afSAdrian Chadd 	 * frame will fail (which has the bfs_aggr and bfs_nframes
30852a9f83afSAdrian Chadd 	 * fields set for said aggregate) and will be retried as
30862a9f83afSAdrian Chadd 	 * a single frame.  In this instance, the values should
30872a9f83afSAdrian Chadd 	 * be reset or the completion code will get upset with you.
30882a9f83afSAdrian Chadd 	 */
30892a9f83afSAdrian Chadd 	if (bf->bf_state.bfs_aggr != 0 || bf->bf_state.bfs_nframes > 1) {
3090b372f122SRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
309183bbd5ebSRui Paulo 		    "%s: bfs_aggr=%d, bfs_nframes=%d\n", __func__,
309283bbd5ebSRui Paulo 		    bf->bf_state.bfs_aggr, bf->bf_state.bfs_nframes);
30932a9f83afSAdrian Chadd 		bf->bf_state.bfs_aggr = 0;
30942a9f83afSAdrian Chadd 		bf->bf_state.bfs_nframes = 1;
30952a9f83afSAdrian Chadd 	}
30962a9f83afSAdrian Chadd 
30974e81f27cSAdrian Chadd 	/* Update CLRDMASK just before this frame is queued */
30984e81f27cSAdrian Chadd 	ath_tx_update_clrdmask(sc, tid, bf);
30994e81f27cSAdrian Chadd 
3100eb6f0de0SAdrian Chadd 	/* Direct dispatch to hardware */
3101*cce63444SAdrian Chadd 	ath_tx_do_ratelookup(sc, bf, tid->tid, bf->bf_state.bfs_pktlen,
3102*cce63444SAdrian Chadd 	    false);
3103e2e4a2c2SAdrian Chadd 	ath_tx_calc_duration(sc, bf);
3104e2e4a2c2SAdrian Chadd 	ath_tx_calc_protection(sc, bf);
3105eb6f0de0SAdrian Chadd 	ath_tx_set_rtscts(sc, bf);
3106e2e4a2c2SAdrian Chadd 	ath_tx_rate_fill_rcflags(sc, bf);
3107eb6f0de0SAdrian Chadd 	ath_tx_setds(sc, bf);
3108eb6f0de0SAdrian Chadd 
3109eb6f0de0SAdrian Chadd 	/* Statistics */
3110eb6f0de0SAdrian Chadd 	sc->sc_aggr_stats.aggr_low_hwq_single_pkt++;
3111eb6f0de0SAdrian Chadd 
3112eb6f0de0SAdrian Chadd 	/* Track per-TID hardware queue depth correctly */
3113eb6f0de0SAdrian Chadd 	tid->hwq_depth++;
3114eb6f0de0SAdrian Chadd 
3115eb6f0de0SAdrian Chadd 	/* Add to BAW */
3116eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_dobaw) {
3117eb6f0de0SAdrian Chadd 		ath_tx_addto_baw(sc, an, tid, bf);
3118eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_addedbaw = 1;
3119eb6f0de0SAdrian Chadd 	}
3120eb6f0de0SAdrian Chadd 
3121eb6f0de0SAdrian Chadd 	/* Set completion handler, multi-frame aggregate or not */
3122eb6f0de0SAdrian Chadd 	bf->bf_comp = ath_tx_aggr_comp;
3123eb6f0de0SAdrian Chadd 
312422a3aee6SAdrian Chadd 	/*
312522a3aee6SAdrian Chadd 	 * Update the current leak count if
312622a3aee6SAdrian Chadd 	 * we're leaking frames; and set the
312722a3aee6SAdrian Chadd 	 * MORE flag as appropriate.
312822a3aee6SAdrian Chadd 	 */
312922a3aee6SAdrian Chadd 	ath_tx_leak_count_update(sc, tid, bf);
313022a3aee6SAdrian Chadd 
3131eb6f0de0SAdrian Chadd 	/* Hand off to hardware */
3132eb6f0de0SAdrian Chadd 	ath_tx_handoff(sc, txq, bf);
3133eb6f0de0SAdrian Chadd }
3134eb6f0de0SAdrian Chadd 
3135eb6f0de0SAdrian Chadd /*
3136eb6f0de0SAdrian Chadd  * Attempt to send the packet.
3137eb6f0de0SAdrian Chadd  * If the queue isn't busy, direct-dispatch.
3138eb6f0de0SAdrian Chadd  * If the queue is busy enough, queue the given packet on the
3139eb6f0de0SAdrian Chadd  *  relevant software queue.
3140eb6f0de0SAdrian Chadd  */
3141eb6f0de0SAdrian Chadd void
314222a3aee6SAdrian Chadd ath_tx_swq(struct ath_softc *sc, struct ieee80211_node *ni,
314322a3aee6SAdrian Chadd     struct ath_txq *txq, int queue_to_head, struct ath_buf *bf)
3144eb6f0de0SAdrian Chadd {
3145eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
3146eb6f0de0SAdrian Chadd 	struct ieee80211_frame *wh;
3147eb6f0de0SAdrian Chadd 	struct ath_tid *atid;
3148eb6f0de0SAdrian Chadd 	int pri, tid;
3149eb6f0de0SAdrian Chadd 	struct mbuf *m0 = bf->bf_m;
3150eb6f0de0SAdrian Chadd 
3151375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
31527561cb5cSAdrian Chadd 
3153eb6f0de0SAdrian Chadd 	/* Fetch the TID - non-QoS frames get assigned to TID 16 */
3154eb6f0de0SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
3155eb6f0de0SAdrian Chadd 	pri = ath_tx_getac(sc, m0);
3156eb6f0de0SAdrian Chadd 	tid = ath_tx_gettid(sc, m0);
3157eb6f0de0SAdrian Chadd 	atid = &an->an_tid[tid];
3158eb6f0de0SAdrian Chadd 
3159a108d2d6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bf=%p, pri=%d, tid=%d, qos=%d\n",
3160a108d2d6SAdrian Chadd 	    __func__, bf, pri, tid, IEEE80211_QOS_HAS_SEQ(wh));
3161eb6f0de0SAdrian Chadd 
3162eb6f0de0SAdrian Chadd 	/* Set local packet state, used to queue packets to hardware */
316346634305SAdrian Chadd 	/* XXX potentially duplicate info, re-check */
3164eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_tid = tid;
3165fc56c9c5SAdrian Chadd 	bf->bf_state.bfs_tx_queue = txq->axq_qnum;
3166eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_pri = pri;
3167eb6f0de0SAdrian Chadd 
3168eb6f0de0SAdrian Chadd 	/*
3169eb6f0de0SAdrian Chadd 	 * If the hardware queue isn't busy, queue it directly.
3170eb6f0de0SAdrian Chadd 	 * If the hardware queue is busy, queue it.
3171eb6f0de0SAdrian Chadd 	 * If the TID is paused or the traffic it outside BAW, software
3172eb6f0de0SAdrian Chadd 	 * queue it.
317322a3aee6SAdrian Chadd 	 *
317422a3aee6SAdrian Chadd 	 * If the node is in power-save and we're leaking a frame,
317522a3aee6SAdrian Chadd 	 * leak a single frame.
3176eb6f0de0SAdrian Chadd 	 */
317722a3aee6SAdrian Chadd 	if (! ath_tx_tid_can_tx_or_sched(sc, atid)) {
3178eb6f0de0SAdrian Chadd 		/* TID is paused, queue */
3179a108d2d6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: paused\n", __func__);
318022a3aee6SAdrian Chadd 		/*
318122a3aee6SAdrian Chadd 		 * If the caller requested that it be sent at a high
318222a3aee6SAdrian Chadd 		 * priority, queue it at the head of the list.
318322a3aee6SAdrian Chadd 		 */
318422a3aee6SAdrian Chadd 		if (queue_to_head)
318522a3aee6SAdrian Chadd 			ATH_TID_INSERT_HEAD(atid, bf, bf_list);
318622a3aee6SAdrian Chadd 		else
31873e6cc97fSAdrian Chadd 			ATH_TID_INSERT_TAIL(atid, bf, bf_list);
3188eb6f0de0SAdrian Chadd 	} else if (ath_tx_ampdu_pending(sc, an, tid)) {
3189eb6f0de0SAdrian Chadd 		/* AMPDU pending; queue */
3190a108d2d6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: pending\n", __func__);
31913e6cc97fSAdrian Chadd 		ATH_TID_INSERT_TAIL(atid, bf, bf_list);
3192eb6f0de0SAdrian Chadd 		/* XXX sched? */
3193eb6f0de0SAdrian Chadd 	} else if (ath_tx_ampdu_running(sc, an, tid)) {
319457af292dSAdrian Chadd 		/*
319557af292dSAdrian Chadd 		 * AMPDU running, queue single-frame if the hardware queue
319657af292dSAdrian Chadd 		 * isn't busy.
319757af292dSAdrian Chadd 		 *
319857af292dSAdrian Chadd 		 * If the hardware queue is busy, sending an aggregate frame
319957af292dSAdrian Chadd 		 * then just hold off so we can queue more aggregate frames.
320057af292dSAdrian Chadd 		 *
320157af292dSAdrian Chadd 		 * Otherwise we may end up with single frames leaking through
320257af292dSAdrian Chadd 		 * because we are dispatching them too quickly.
320357af292dSAdrian Chadd 		 *
320457af292dSAdrian Chadd 		 * TODO: maybe we should treat this as two policies - minimise
320557af292dSAdrian Chadd 		 * latency, or maximise throughput.  Then for BE/BK we can
320657af292dSAdrian Chadd 		 * maximise throughput, and VO/VI (if AMPDU is enabled!)
320757af292dSAdrian Chadd 		 * minimise latency.
320857af292dSAdrian Chadd 		 */
320939f24578SAdrian Chadd 
321039f24578SAdrian Chadd 		/*
321139f24578SAdrian Chadd 		 * Always queue the frame to the tail of the list.
321239f24578SAdrian Chadd 		 */
32133e6cc97fSAdrian Chadd 		ATH_TID_INSERT_TAIL(atid, bf, bf_list);
321439f24578SAdrian Chadd 
321539f24578SAdrian Chadd 		/*
321639f24578SAdrian Chadd 		 * If the hardware queue isn't busy, direct dispatch
321757af292dSAdrian Chadd 		 * the head frame in the list.
321839f24578SAdrian Chadd 		 *
321957af292dSAdrian Chadd 		 * Note: if we're say, configured to do ADDBA but not A-MPDU
322057af292dSAdrian Chadd 		 * then maybe we want to still queue two non-aggregate frames
322157af292dSAdrian Chadd 		 * to the hardware.  Again with the per-TID policy
322257af292dSAdrian Chadd 		 * configuration..)
322372910f03SAdrian Chadd 		 *
322439f24578SAdrian Chadd 		 * Otherwise, schedule the TID.
322539f24578SAdrian Chadd 		 */
322672910f03SAdrian Chadd 		/* XXX TXQ locking */
322757af292dSAdrian Chadd 		if (txq->axq_depth + txq->fifo.axq_depth == 0) {
322857af292dSAdrian Chadd 
32293e6cc97fSAdrian Chadd 			bf = ATH_TID_FIRST(atid);
32303e6cc97fSAdrian Chadd 			ATH_TID_REMOVE(atid, bf, bf_list);
32312a9f83afSAdrian Chadd 
32322a9f83afSAdrian Chadd 			/*
32332a9f83afSAdrian Chadd 			 * Ensure it's definitely treated as a non-AMPDU
32342a9f83afSAdrian Chadd 			 * frame - this information may have been left
32352a9f83afSAdrian Chadd 			 * over from a previous attempt.
32362a9f83afSAdrian Chadd 			 */
32372a9f83afSAdrian Chadd 			bf->bf_state.bfs_aggr = 0;
32382a9f83afSAdrian Chadd 			bf->bf_state.bfs_nframes = 1;
32392a9f83afSAdrian Chadd 
32402a9f83afSAdrian Chadd 			/* Queue to the hardware */
324146634305SAdrian Chadd 			ath_tx_xmit_aggr(sc, an, txq, bf);
3242a108d2d6SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX,
3243a108d2d6SAdrian Chadd 			    "%s: xmit_aggr\n",
3244a108d2d6SAdrian Chadd 			    __func__);
3245d4365d16SAdrian Chadd 		} else {
3246d4365d16SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX,
3247a108d2d6SAdrian Chadd 			    "%s: ampdu; swq'ing\n",
3248a108d2d6SAdrian Chadd 			    __func__);
324903682514SAdrian Chadd 
3250eb6f0de0SAdrian Chadd 			ath_tx_tid_sched(sc, atid);
3251eb6f0de0SAdrian Chadd 		}
325272910f03SAdrian Chadd 	/*
325372910f03SAdrian Chadd 	 * If we're not doing A-MPDU, be prepared to direct dispatch
325472910f03SAdrian Chadd 	 * up to both limits if possible.  This particular corner
325572910f03SAdrian Chadd 	 * case may end up with packet starvation between aggregate
3256f6b6084bSPedro F. Giffuni 	 * traffic and non-aggregate traffic: we want to ensure
325772910f03SAdrian Chadd 	 * that non-aggregate stations get a few frames queued to the
325872910f03SAdrian Chadd 	 * hardware before the aggregate station(s) get their chance.
325972910f03SAdrian Chadd 	 *
326072910f03SAdrian Chadd 	 * So if you only ever see a couple of frames direct dispatched
326172910f03SAdrian Chadd 	 * to the hardware from a non-AMPDU client, check both here
326272910f03SAdrian Chadd 	 * and in the software queue dispatcher to ensure that those
326372910f03SAdrian Chadd 	 * non-AMPDU stations get a fair chance to transmit.
326472910f03SAdrian Chadd 	 */
326572910f03SAdrian Chadd 	/* XXX TXQ locking */
326672910f03SAdrian Chadd 	} else if ((txq->axq_depth + txq->fifo.axq_depth < sc->sc_hwq_limit_nonaggr) &&
326772910f03SAdrian Chadd 		    (txq->axq_aggr_depth < sc->sc_hwq_limit_aggr)) {
3268eb6f0de0SAdrian Chadd 		/* AMPDU not running, attempt direct dispatch */
3269a108d2d6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: xmit_normal\n", __func__);
32700a544719SAdrian Chadd 		/* See if clrdmask needs to be set */
32710a544719SAdrian Chadd 		ath_tx_update_clrdmask(sc, atid, bf);
327222a3aee6SAdrian Chadd 
327322a3aee6SAdrian Chadd 		/*
327422a3aee6SAdrian Chadd 		 * Update the current leak count if
327522a3aee6SAdrian Chadd 		 * we're leaking frames; and set the
327622a3aee6SAdrian Chadd 		 * MORE flag as appropriate.
327722a3aee6SAdrian Chadd 		 */
327822a3aee6SAdrian Chadd 		ath_tx_leak_count_update(sc, atid, bf);
327922a3aee6SAdrian Chadd 
328022a3aee6SAdrian Chadd 		/*
328122a3aee6SAdrian Chadd 		 * Dispatch the frame.
328222a3aee6SAdrian Chadd 		 */
3283eb6f0de0SAdrian Chadd 		ath_tx_xmit_normal(sc, txq, bf);
3284eb6f0de0SAdrian Chadd 	} else {
3285eb6f0de0SAdrian Chadd 		/* Busy; queue */
3286a108d2d6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: swq'ing\n", __func__);
32873e6cc97fSAdrian Chadd 		ATH_TID_INSERT_TAIL(atid, bf, bf_list);
3288eb6f0de0SAdrian Chadd 		ath_tx_tid_sched(sc, atid);
3289eb6f0de0SAdrian Chadd 	}
3290eb6f0de0SAdrian Chadd }
3291eb6f0de0SAdrian Chadd 
3292eb6f0de0SAdrian Chadd /*
32934f25ddbbSAdrian Chadd  * Only set the clrdmask bit if none of the nodes are currently
32944f25ddbbSAdrian Chadd  * filtered.
32954f25ddbbSAdrian Chadd  *
32964f25ddbbSAdrian Chadd  * XXX TODO: go through all the callers and check to see
32974f25ddbbSAdrian Chadd  * which are being called in the context of looping over all
32984f25ddbbSAdrian Chadd  * TIDs (eg, if all tids are being paused, resumed, etc.)
32994f25ddbbSAdrian Chadd  * That'll avoid O(n^2) complexity here.
33004f25ddbbSAdrian Chadd  */
33014f25ddbbSAdrian Chadd static void
33024f25ddbbSAdrian Chadd ath_tx_set_clrdmask(struct ath_softc *sc, struct ath_node *an)
33034f25ddbbSAdrian Chadd {
33044f25ddbbSAdrian Chadd 	int i;
33054f25ddbbSAdrian Chadd 
33064f25ddbbSAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
33074f25ddbbSAdrian Chadd 
33084f25ddbbSAdrian Chadd 	for (i = 0; i < IEEE80211_TID_SIZE; i++) {
33094f25ddbbSAdrian Chadd 		if (an->an_tid[i].isfiltered == 1)
3310f74d878fSAdrian Chadd 			return;
33114f25ddbbSAdrian Chadd 	}
33124f25ddbbSAdrian Chadd 	an->clrdmask = 1;
33134f25ddbbSAdrian Chadd }
33144f25ddbbSAdrian Chadd 
33154f25ddbbSAdrian Chadd /*
3316eb6f0de0SAdrian Chadd  * Configure the per-TID node state.
3317eb6f0de0SAdrian Chadd  *
3318eb6f0de0SAdrian Chadd  * This likely belongs in if_ath_node.c but I can't think of anywhere
3319eb6f0de0SAdrian Chadd  * else to put it just yet.
3320eb6f0de0SAdrian Chadd  *
3321eb6f0de0SAdrian Chadd  * This sets up the SLISTs and the mutex as appropriate.
3322eb6f0de0SAdrian Chadd  */
3323eb6f0de0SAdrian Chadd void
3324eb6f0de0SAdrian Chadd ath_tx_tid_init(struct ath_softc *sc, struct ath_node *an)
3325eb6f0de0SAdrian Chadd {
3326eb6f0de0SAdrian Chadd 	int i, j;
3327eb6f0de0SAdrian Chadd 	struct ath_tid *atid;
3328eb6f0de0SAdrian Chadd 
3329eb6f0de0SAdrian Chadd 	for (i = 0; i < IEEE80211_TID_SIZE; i++) {
3330eb6f0de0SAdrian Chadd 		atid = &an->an_tid[i];
3331f1bc738eSAdrian Chadd 
3332f1bc738eSAdrian Chadd 		/* XXX now with this bzer(), is the field 0'ing needed? */
3333f1bc738eSAdrian Chadd 		bzero(atid, sizeof(*atid));
3334f1bc738eSAdrian Chadd 
33353e6cc97fSAdrian Chadd 		TAILQ_INIT(&atid->tid_q);
33363e6cc97fSAdrian Chadd 		TAILQ_INIT(&atid->filtq.tid_q);
3337eb6f0de0SAdrian Chadd 		atid->tid = i;
3338eb6f0de0SAdrian Chadd 		atid->an = an;
3339eb6f0de0SAdrian Chadd 		for (j = 0; j < ATH_TID_MAX_BUFS; j++)
3340eb6f0de0SAdrian Chadd 			atid->tx_buf[j] = NULL;
3341eb6f0de0SAdrian Chadd 		atid->baw_head = atid->baw_tail = 0;
3342eb6f0de0SAdrian Chadd 		atid->paused = 0;
3343eb6f0de0SAdrian Chadd 		atid->sched = 0;
3344eb6f0de0SAdrian Chadd 		atid->hwq_depth = 0;
3345eb6f0de0SAdrian Chadd 		atid->cleanup_inprogress = 0;
3346eb6f0de0SAdrian Chadd 		if (i == IEEE80211_NONQOS_TID)
33477403d1b9SAdrian Chadd 			atid->ac = ATH_NONQOS_TID_AC;
3348eb6f0de0SAdrian Chadd 		else
3349eb6f0de0SAdrian Chadd 			atid->ac = TID_TO_WME_AC(i);
3350eb6f0de0SAdrian Chadd 	}
33514f25ddbbSAdrian Chadd 	an->clrdmask = 1;	/* Always start by setting this bit */
3352eb6f0de0SAdrian Chadd }
3353eb6f0de0SAdrian Chadd 
3354eb6f0de0SAdrian Chadd /*
3355eb6f0de0SAdrian Chadd  * Pause the current TID. This stops packets from being transmitted
3356eb6f0de0SAdrian Chadd  * on it.
3357eb6f0de0SAdrian Chadd  *
3358eb6f0de0SAdrian Chadd  * Since this is also called from upper layers as well as the driver,
3359eb6f0de0SAdrian Chadd  * it will get the TID lock.
3360eb6f0de0SAdrian Chadd  */
3361eb6f0de0SAdrian Chadd static void
3362eb6f0de0SAdrian Chadd ath_tx_tid_pause(struct ath_softc *sc, struct ath_tid *tid)
3363eb6f0de0SAdrian Chadd {
336488b3d483SAdrian Chadd 
3365375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
3366eb6f0de0SAdrian Chadd 	tid->paused++;
33671771c649SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: [%6D]: tid=%d, paused = %d\n",
33681771c649SAdrian Chadd 	    __func__,
33691771c649SAdrian Chadd 	    tid->an->an_node.ni_macaddr, ":",
33701771c649SAdrian Chadd 	    tid->tid,
33711771c649SAdrian Chadd 	    tid->paused);
3372eb6f0de0SAdrian Chadd }
3373eb6f0de0SAdrian Chadd 
3374eb6f0de0SAdrian Chadd /*
3375eb6f0de0SAdrian Chadd  * Unpause the current TID, and schedule it if needed.
3376eb6f0de0SAdrian Chadd  */
3377eb6f0de0SAdrian Chadd static void
3378eb6f0de0SAdrian Chadd ath_tx_tid_resume(struct ath_softc *sc, struct ath_tid *tid)
3379eb6f0de0SAdrian Chadd {
3380375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
3381eb6f0de0SAdrian Chadd 
3382dff5bdf4SAdrian Chadd 	/*
3383dff5bdf4SAdrian Chadd 	 * There's some odd places where ath_tx_tid_resume() is called
3384dff5bdf4SAdrian Chadd 	 * when it shouldn't be; this works around that particular issue
3385dff5bdf4SAdrian Chadd 	 * until it's actually resolved.
3386dff5bdf4SAdrian Chadd 	 */
3387dff5bdf4SAdrian Chadd 	if (tid->paused == 0) {
33881771c649SAdrian Chadd 		device_printf(sc->sc_dev,
33891771c649SAdrian Chadd 		    "%s: [%6D]: tid=%d, paused=0?\n",
33901771c649SAdrian Chadd 		    __func__,
33911771c649SAdrian Chadd 		    tid->an->an_node.ni_macaddr, ":",
33921771c649SAdrian Chadd 		    tid->tid);
3393dff5bdf4SAdrian Chadd 	} else {
3394eb6f0de0SAdrian Chadd 		tid->paused--;
3395dff5bdf4SAdrian Chadd 	}
3396eb6f0de0SAdrian Chadd 
33971771c649SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
33981771c649SAdrian Chadd 	    "%s: [%6D]: tid=%d, unpaused = %d\n",
33991771c649SAdrian Chadd 	    __func__,
34001771c649SAdrian Chadd 	    tid->an->an_node.ni_macaddr, ":",
34011771c649SAdrian Chadd 	    tid->tid,
34021771c649SAdrian Chadd 	    tid->paused);
3403eb6f0de0SAdrian Chadd 
34040eb81626SAdrian Chadd 	if (tid->paused)
3405eb6f0de0SAdrian Chadd 		return;
34060eb81626SAdrian Chadd 
34070eb81626SAdrian Chadd 	/*
34080eb81626SAdrian Chadd 	 * Override the clrdmask configuration for the next frame
34090eb81626SAdrian Chadd 	 * from this TID, just to get the ball rolling.
34100eb81626SAdrian Chadd 	 */
34114f25ddbbSAdrian Chadd 	ath_tx_set_clrdmask(sc, tid->an);
34120eb81626SAdrian Chadd 
34130eb81626SAdrian Chadd 	if (tid->axq_depth == 0)
34140eb81626SAdrian Chadd 		return;
3415eb6f0de0SAdrian Chadd 
3416f1bc738eSAdrian Chadd 	/* XXX isfiltered shouldn't ever be 0 at this point */
3417f1bc738eSAdrian Chadd 	if (tid->isfiltered == 1) {
341883bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: filtered?!\n",
341983bbd5ebSRui Paulo 		    __func__);
3420f1bc738eSAdrian Chadd 		return;
3421f1bc738eSAdrian Chadd 	}
3422f1bc738eSAdrian Chadd 
3423eb6f0de0SAdrian Chadd 	ath_tx_tid_sched(sc, tid);
342421bca442SAdrian Chadd 
342521bca442SAdrian Chadd 	/*
342621bca442SAdrian Chadd 	 * Queue the software TX scheduler.
342721bca442SAdrian Chadd 	 */
342821bca442SAdrian Chadd 	ath_tx_swq_kick(sc);
3429eb6f0de0SAdrian Chadd }
3430eb6f0de0SAdrian Chadd 
3431eb6f0de0SAdrian Chadd /*
3432f1bc738eSAdrian Chadd  * Add the given ath_buf to the TID filtered frame list.
3433f1bc738eSAdrian Chadd  * This requires the TID be filtered.
3434f1bc738eSAdrian Chadd  */
3435f1bc738eSAdrian Chadd static void
3436f1bc738eSAdrian Chadd ath_tx_tid_filt_addbuf(struct ath_softc *sc, struct ath_tid *tid,
3437f1bc738eSAdrian Chadd     struct ath_buf *bf)
3438f1bc738eSAdrian Chadd {
3439f1bc738eSAdrian Chadd 
3440375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
3441375307d4SAdrian Chadd 
3442f1bc738eSAdrian Chadd 	if (!tid->isfiltered)
344383bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT, "%s: not filtered?!\n",
344483bbd5ebSRui Paulo 		    __func__);
3445f1bc738eSAdrian Chadd 
3446f1bc738eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_FILT, "%s: bf=%p\n", __func__, bf);
3447f1bc738eSAdrian Chadd 
3448f1bc738eSAdrian Chadd 	/* Set the retry bit and bump the retry counter */
3449f1bc738eSAdrian Chadd 	ath_tx_set_retry(sc, bf);
3450f1bc738eSAdrian Chadd 	sc->sc_stats.ast_tx_swfiltered++;
3451f1bc738eSAdrian Chadd 
345213aa9ee5SAdrian Chadd 	ATH_TID_FILT_INSERT_TAIL(tid, bf, bf_list);
3453f1bc738eSAdrian Chadd }
3454f1bc738eSAdrian Chadd 
3455f1bc738eSAdrian Chadd /*
3456f1bc738eSAdrian Chadd  * Handle a completed filtered frame from the given TID.
3457f1bc738eSAdrian Chadd  * This just enables/pauses the filtered frame state if required
3458f1bc738eSAdrian Chadd  * and appends the filtered frame to the filtered queue.
3459f1bc738eSAdrian Chadd  */
3460f1bc738eSAdrian Chadd static void
3461f1bc738eSAdrian Chadd ath_tx_tid_filt_comp_buf(struct ath_softc *sc, struct ath_tid *tid,
3462f1bc738eSAdrian Chadd     struct ath_buf *bf)
3463f1bc738eSAdrian Chadd {
3464f1bc738eSAdrian Chadd 
3465375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
3466f1bc738eSAdrian Chadd 
3467f1bc738eSAdrian Chadd 	if (! tid->isfiltered) {
346842fdd8e7SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT, "%s: tid=%d; filter transition\n",
346942fdd8e7SAdrian Chadd 		    __func__, tid->tid);
3470f1bc738eSAdrian Chadd 		tid->isfiltered = 1;
3471f1bc738eSAdrian Chadd 		ath_tx_tid_pause(sc, tid);
3472f1bc738eSAdrian Chadd 	}
3473f1bc738eSAdrian Chadd 
3474f1bc738eSAdrian Chadd 	/* Add the frame to the filter queue */
3475f1bc738eSAdrian Chadd 	ath_tx_tid_filt_addbuf(sc, tid, bf);
3476f1bc738eSAdrian Chadd }
3477f1bc738eSAdrian Chadd 
3478f1bc738eSAdrian Chadd /*
3479f1bc738eSAdrian Chadd  * Complete the filtered frame TX completion.
3480f1bc738eSAdrian Chadd  *
3481f1bc738eSAdrian Chadd  * If there are no more frames in the hardware queue, unpause/unfilter
3482f1bc738eSAdrian Chadd  * the TID if applicable.  Otherwise we will wait for a node PS transition
3483f1bc738eSAdrian Chadd  * to unfilter.
3484f1bc738eSAdrian Chadd  */
3485f1bc738eSAdrian Chadd static void
3486f1bc738eSAdrian Chadd ath_tx_tid_filt_comp_complete(struct ath_softc *sc, struct ath_tid *tid)
3487f1bc738eSAdrian Chadd {
3488f1bc738eSAdrian Chadd 	struct ath_buf *bf;
3489a3fd3b14SAdrian Chadd 	int do_resume = 0;
3490f1bc738eSAdrian Chadd 
3491375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
3492f1bc738eSAdrian Chadd 
3493f1bc738eSAdrian Chadd 	if (tid->hwq_depth != 0)
3494f1bc738eSAdrian Chadd 		return;
3495f1bc738eSAdrian Chadd 
349642fdd8e7SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_FILT, "%s: tid=%d, hwq=0, transition back\n",
349742fdd8e7SAdrian Chadd 	    __func__, tid->tid);
3498a3fd3b14SAdrian Chadd 	if (tid->isfiltered == 1) {
3499f1bc738eSAdrian Chadd 		tid->isfiltered = 0;
3500a3fd3b14SAdrian Chadd 		do_resume = 1;
3501a3fd3b14SAdrian Chadd 	}
3502a3fd3b14SAdrian Chadd 
35034f25ddbbSAdrian Chadd 	/* XXX ath_tx_tid_resume() also calls ath_tx_set_clrdmask()! */
35044f25ddbbSAdrian Chadd 	ath_tx_set_clrdmask(sc, tid->an);
3505f1bc738eSAdrian Chadd 
3506f1bc738eSAdrian Chadd 	/* XXX this is really quite inefficient */
350713aa9ee5SAdrian Chadd 	while ((bf = ATH_TID_FILT_LAST(tid, ath_bufhead_s)) != NULL) {
350813aa9ee5SAdrian Chadd 		ATH_TID_FILT_REMOVE(tid, bf, bf_list);
35093e6cc97fSAdrian Chadd 		ATH_TID_INSERT_HEAD(tid, bf, bf_list);
3510f1bc738eSAdrian Chadd 	}
3511f1bc738eSAdrian Chadd 
3512c5d230abSAdrian Chadd 	/* And only resume if we had paused before */
3513a3fd3b14SAdrian Chadd 	if (do_resume)
3514f1bc738eSAdrian Chadd 		ath_tx_tid_resume(sc, tid);
3515f1bc738eSAdrian Chadd }
3516f1bc738eSAdrian Chadd 
3517f1bc738eSAdrian Chadd /*
3518f1bc738eSAdrian Chadd  * Called when a single (aggregate or otherwise) frame is completed.
3519f1bc738eSAdrian Chadd  *
35201f737306SAdrian Chadd  * Returns 0 if the buffer could be added to the filtered list
35211f737306SAdrian Chadd  * (cloned or otherwise), 1 if the buffer couldn't be added to the
3522f1bc738eSAdrian Chadd  * filtered list (failed clone; expired retry) and the caller should
3523f1bc738eSAdrian Chadd  * free it and handle it like a failure (eg by sending a BAR.)
35241f737306SAdrian Chadd  *
35251f737306SAdrian Chadd  * since the buffer may be cloned, bf must be not touched after this
35261f737306SAdrian Chadd  * if the return value is 0.
3527f1bc738eSAdrian Chadd  */
3528f1bc738eSAdrian Chadd static int
3529f1bc738eSAdrian Chadd ath_tx_tid_filt_comp_single(struct ath_softc *sc, struct ath_tid *tid,
3530f1bc738eSAdrian Chadd     struct ath_buf *bf)
3531f1bc738eSAdrian Chadd {
3532f1bc738eSAdrian Chadd 	struct ath_buf *nbf;
3533f1bc738eSAdrian Chadd 	int retval;
3534f1bc738eSAdrian Chadd 
3535375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
3536f1bc738eSAdrian Chadd 
3537f1bc738eSAdrian Chadd 	/*
3538f1bc738eSAdrian Chadd 	 * Don't allow a filtered frame to live forever.
3539f1bc738eSAdrian Chadd 	 */
3540f1bc738eSAdrian Chadd 	if (bf->bf_state.bfs_retries > SWMAX_RETRIES) {
35410eb81626SAdrian Chadd 		sc->sc_stats.ast_tx_swretrymax++;
3542f1bc738eSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3543f1bc738eSAdrian Chadd 		    "%s: bf=%p, seqno=%d, exceeded retries\n",
3544f1bc738eSAdrian Chadd 		    __func__,
3545f1bc738eSAdrian Chadd 		    bf,
354642fdd8e7SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
35471f737306SAdrian Chadd 		retval = 1; /* error */
35481f737306SAdrian Chadd 		goto finish;
3549f1bc738eSAdrian Chadd 	}
3550f1bc738eSAdrian Chadd 
3551f1bc738eSAdrian Chadd 	/*
3552f1bc738eSAdrian Chadd 	 * A busy buffer can't be added to the retry list.
3553f1bc738eSAdrian Chadd 	 * It needs to be cloned.
3554f1bc738eSAdrian Chadd 	 */
3555f1bc738eSAdrian Chadd 	if (bf->bf_flags & ATH_BUF_BUSY) {
3556f1bc738eSAdrian Chadd 		nbf = ath_tx_retry_clone(sc, tid->an, tid, bf);
3557f1bc738eSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3558f1bc738eSAdrian Chadd 		    "%s: busy buffer clone: %p -> %p\n",
3559f1bc738eSAdrian Chadd 		    __func__, bf, nbf);
3560f1bc738eSAdrian Chadd 	} else {
3561f1bc738eSAdrian Chadd 		nbf = bf;
3562f1bc738eSAdrian Chadd 	}
3563f1bc738eSAdrian Chadd 
3564f1bc738eSAdrian Chadd 	if (nbf == NULL) {
3565f1bc738eSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3566f1bc738eSAdrian Chadd 		    "%s: busy buffer couldn't be cloned (%p)!\n",
3567f1bc738eSAdrian Chadd 		    __func__, bf);
35681f737306SAdrian Chadd 		retval = 1; /* error */
3569f1bc738eSAdrian Chadd 	} else {
3570f1bc738eSAdrian Chadd 		ath_tx_tid_filt_comp_buf(sc, tid, nbf);
35711f737306SAdrian Chadd 		retval = 0; /* ok */
3572f1bc738eSAdrian Chadd 	}
35731f737306SAdrian Chadd finish:
3574f1bc738eSAdrian Chadd 	ath_tx_tid_filt_comp_complete(sc, tid);
3575f1bc738eSAdrian Chadd 
3576f1bc738eSAdrian Chadd 	return (retval);
3577f1bc738eSAdrian Chadd }
3578f1bc738eSAdrian Chadd 
3579f1bc738eSAdrian Chadd static void
3580f1bc738eSAdrian Chadd ath_tx_tid_filt_comp_aggr(struct ath_softc *sc, struct ath_tid *tid,
3581f1bc738eSAdrian Chadd     struct ath_buf *bf_first, ath_bufhead *bf_q)
3582f1bc738eSAdrian Chadd {
3583f1bc738eSAdrian Chadd 	struct ath_buf *bf, *bf_next, *nbf;
3584f1bc738eSAdrian Chadd 
3585375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
3586f1bc738eSAdrian Chadd 
3587f1bc738eSAdrian Chadd 	bf = bf_first;
3588f1bc738eSAdrian Chadd 	while (bf) {
3589f1bc738eSAdrian Chadd 		bf_next = bf->bf_next;
3590f1bc738eSAdrian Chadd 		bf->bf_next = NULL;	/* Remove it from the aggr list */
3591f1bc738eSAdrian Chadd 
3592f1bc738eSAdrian Chadd 		/*
3593f1bc738eSAdrian Chadd 		 * Don't allow a filtered frame to live forever.
3594f1bc738eSAdrian Chadd 		 */
3595f1bc738eSAdrian Chadd 		if (bf->bf_state.bfs_retries > SWMAX_RETRIES) {
35960eb81626SAdrian Chadd 			sc->sc_stats.ast_tx_swretrymax++;
3597f1bc738eSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
359842fdd8e7SAdrian Chadd 			    "%s: tid=%d, bf=%p, seqno=%d, exceeded retries\n",
3599f1bc738eSAdrian Chadd 			    __func__,
360042fdd8e7SAdrian Chadd 			    tid->tid,
3601f1bc738eSAdrian Chadd 			    bf,
360242fdd8e7SAdrian Chadd 			    SEQNO(bf->bf_state.bfs_seqno));
3603f1bc738eSAdrian Chadd 			TAILQ_INSERT_TAIL(bf_q, bf, bf_list);
3604f1bc738eSAdrian Chadd 			goto next;
3605f1bc738eSAdrian Chadd 		}
3606f1bc738eSAdrian Chadd 
3607f1bc738eSAdrian Chadd 		if (bf->bf_flags & ATH_BUF_BUSY) {
3608f1bc738eSAdrian Chadd 			nbf = ath_tx_retry_clone(sc, tid->an, tid, bf);
3609f1bc738eSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
361042fdd8e7SAdrian Chadd 			    "%s: tid=%d, busy buffer cloned: %p -> %p, seqno=%d\n",
361142fdd8e7SAdrian Chadd 			    __func__, tid->tid, bf, nbf, SEQNO(bf->bf_state.bfs_seqno));
3612f1bc738eSAdrian Chadd 		} else {
3613f1bc738eSAdrian Chadd 			nbf = bf;
3614f1bc738eSAdrian Chadd 		}
3615f1bc738eSAdrian Chadd 
3616f1bc738eSAdrian Chadd 		/*
3617f1bc738eSAdrian Chadd 		 * If the buffer couldn't be cloned, add it to bf_q;
3618f1bc738eSAdrian Chadd 		 * the caller will free the buffer(s) as required.
3619f1bc738eSAdrian Chadd 		 */
3620f1bc738eSAdrian Chadd 		if (nbf == NULL) {
3621f1bc738eSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
362242fdd8e7SAdrian Chadd 			    "%s: tid=%d, buffer couldn't be cloned! (%p) seqno=%d\n",
362342fdd8e7SAdrian Chadd 			    __func__, tid->tid, bf, SEQNO(bf->bf_state.bfs_seqno));
3624f1bc738eSAdrian Chadd 			TAILQ_INSERT_TAIL(bf_q, bf, bf_list);
3625f1bc738eSAdrian Chadd 		} else {
3626f1bc738eSAdrian Chadd 			ath_tx_tid_filt_comp_buf(sc, tid, nbf);
3627f1bc738eSAdrian Chadd 		}
3628f1bc738eSAdrian Chadd next:
3629f1bc738eSAdrian Chadd 		bf = bf_next;
3630f1bc738eSAdrian Chadd 	}
3631f1bc738eSAdrian Chadd 
3632f1bc738eSAdrian Chadd 	ath_tx_tid_filt_comp_complete(sc, tid);
3633f1bc738eSAdrian Chadd }
3634f1bc738eSAdrian Chadd 
3635f1bc738eSAdrian Chadd /*
363688b3d483SAdrian Chadd  * Suspend the queue because we need to TX a BAR.
363788b3d483SAdrian Chadd  */
363888b3d483SAdrian Chadd static void
363988b3d483SAdrian Chadd ath_tx_tid_bar_suspend(struct ath_softc *sc, struct ath_tid *tid)
364088b3d483SAdrian Chadd {
3641375307d4SAdrian Chadd 
3642375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
364388b3d483SAdrian Chadd 
36440e22ed0eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
36456d07d3e0SAdrian Chadd 	    "%s: tid=%d, bar_wait=%d, bar_tx=%d, called\n",
364688b3d483SAdrian Chadd 	    __func__,
36476d07d3e0SAdrian Chadd 	    tid->tid,
3648e60c4fc2SAdrian Chadd 	    tid->bar_wait,
3649e60c4fc2SAdrian Chadd 	    tid->bar_tx);
365088b3d483SAdrian Chadd 
365188b3d483SAdrian Chadd 	/* We shouldn't be called when bar_tx is 1 */
365288b3d483SAdrian Chadd 	if (tid->bar_tx) {
365383bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
365483bbd5ebSRui Paulo 		    "%s: bar_tx is 1?!\n", __func__);
365588b3d483SAdrian Chadd 	}
365688b3d483SAdrian Chadd 
365788b3d483SAdrian Chadd 	/* If we've already been called, just be patient. */
365888b3d483SAdrian Chadd 	if (tid->bar_wait)
365988b3d483SAdrian Chadd 		return;
366088b3d483SAdrian Chadd 
366188b3d483SAdrian Chadd 	/* Wait! */
366288b3d483SAdrian Chadd 	tid->bar_wait = 1;
366388b3d483SAdrian Chadd 
366488b3d483SAdrian Chadd 	/* Only one pause, no matter how many frames fail */
366588b3d483SAdrian Chadd 	ath_tx_tid_pause(sc, tid);
366688b3d483SAdrian Chadd }
366788b3d483SAdrian Chadd 
366888b3d483SAdrian Chadd /*
366988b3d483SAdrian Chadd  * We've finished with BAR handling - either we succeeded or
367088b3d483SAdrian Chadd  * failed. Either way, unsuspend TX.
367188b3d483SAdrian Chadd  */
367288b3d483SAdrian Chadd static void
367388b3d483SAdrian Chadd ath_tx_tid_bar_unsuspend(struct ath_softc *sc, struct ath_tid *tid)
367488b3d483SAdrian Chadd {
3675375307d4SAdrian Chadd 
3676375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
367788b3d483SAdrian Chadd 
36780e22ed0eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
36796d07d3e0SAdrian Chadd 	    "%s: %6D: TID=%d, called\n",
368088b3d483SAdrian Chadd 	    __func__,
36819b48fb4bSAdrian Chadd 	    tid->an->an_node.ni_macaddr,
36829b48fb4bSAdrian Chadd 	    ":",
36836d07d3e0SAdrian Chadd 	    tid->tid);
368488b3d483SAdrian Chadd 
368588b3d483SAdrian Chadd 	if (tid->bar_tx == 0 || tid->bar_wait == 0) {
368683bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
36876d07d3e0SAdrian Chadd 		    "%s: %6D: TID=%d, bar_tx=%d, bar_wait=%d: ?\n",
368883bbd5ebSRui Paulo 		    __func__, tid->an->an_node.ni_macaddr, ":",
368983bbd5ebSRui Paulo 		    tid->tid, tid->bar_tx, tid->bar_wait);
369088b3d483SAdrian Chadd 	}
369188b3d483SAdrian Chadd 
369288b3d483SAdrian Chadd 	tid->bar_tx = tid->bar_wait = 0;
369388b3d483SAdrian Chadd 	ath_tx_tid_resume(sc, tid);
369488b3d483SAdrian Chadd }
369588b3d483SAdrian Chadd 
369688b3d483SAdrian Chadd /*
369788b3d483SAdrian Chadd  * Return whether we're ready to TX a BAR frame.
369888b3d483SAdrian Chadd  *
369988b3d483SAdrian Chadd  * Requires the TID lock be held.
370088b3d483SAdrian Chadd  */
370188b3d483SAdrian Chadd static int
370288b3d483SAdrian Chadd ath_tx_tid_bar_tx_ready(struct ath_softc *sc, struct ath_tid *tid)
370388b3d483SAdrian Chadd {
370488b3d483SAdrian Chadd 
3705375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
370688b3d483SAdrian Chadd 
370788b3d483SAdrian Chadd 	if (tid->bar_wait == 0 || tid->hwq_depth > 0)
370888b3d483SAdrian Chadd 		return (0);
370988b3d483SAdrian Chadd 
37109b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
37116d07d3e0SAdrian Chadd 	    "%s: %6D: TID=%d, bar ready\n",
37129b48fb4bSAdrian Chadd 	    __func__,
37139b48fb4bSAdrian Chadd 	    tid->an->an_node.ni_macaddr,
37149b48fb4bSAdrian Chadd 	    ":",
37156d07d3e0SAdrian Chadd 	    tid->tid);
37160e22ed0eSAdrian Chadd 
371788b3d483SAdrian Chadd 	return (1);
371888b3d483SAdrian Chadd }
371988b3d483SAdrian Chadd 
372088b3d483SAdrian Chadd /*
372188b3d483SAdrian Chadd  * Check whether the current TID is ready to have a BAR
372288b3d483SAdrian Chadd  * TXed and if so, do the TX.
372388b3d483SAdrian Chadd  *
372488b3d483SAdrian Chadd  * Since the TID/TXQ lock can't be held during a call to
372588b3d483SAdrian Chadd  * ieee80211_send_bar(), we have to do the dirty thing of unlocking it,
372688b3d483SAdrian Chadd  * sending the BAR and locking it again.
372788b3d483SAdrian Chadd  *
372888b3d483SAdrian Chadd  * Eventually, the code to send the BAR should be broken out
372988b3d483SAdrian Chadd  * from this routine so the lock doesn't have to be reacquired
373088b3d483SAdrian Chadd  * just to be immediately dropped by the caller.
373188b3d483SAdrian Chadd  */
373288b3d483SAdrian Chadd static void
373388b3d483SAdrian Chadd ath_tx_tid_bar_tx(struct ath_softc *sc, struct ath_tid *tid)
373488b3d483SAdrian Chadd {
373588b3d483SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
373688b3d483SAdrian Chadd 
3737375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
373888b3d483SAdrian Chadd 
37390e22ed0eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
37406d07d3e0SAdrian Chadd 	    "%s: %6D: TID=%d, called\n",
374188b3d483SAdrian Chadd 	    __func__,
37429b48fb4bSAdrian Chadd 	    tid->an->an_node.ni_macaddr,
37439b48fb4bSAdrian Chadd 	    ":",
37446d07d3e0SAdrian Chadd 	    tid->tid);
374588b3d483SAdrian Chadd 
374688b3d483SAdrian Chadd 	tap = ath_tx_get_tx_tid(tid->an, tid->tid);
374788b3d483SAdrian Chadd 
374888b3d483SAdrian Chadd 	/*
374988b3d483SAdrian Chadd 	 * This is an error condition!
375088b3d483SAdrian Chadd 	 */
375188b3d483SAdrian Chadd 	if (tid->bar_wait == 0 || tid->bar_tx == 1) {
375283bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
37536d07d3e0SAdrian Chadd 		    "%s: %6D: TID=%d, bar_tx=%d, bar_wait=%d: ?\n",
375483bbd5ebSRui Paulo 		    __func__, tid->an->an_node.ni_macaddr, ":",
375583bbd5ebSRui Paulo 		    tid->tid, tid->bar_tx, tid->bar_wait);
375688b3d483SAdrian Chadd 		return;
375788b3d483SAdrian Chadd 	}
375888b3d483SAdrian Chadd 
375988b3d483SAdrian Chadd 	/* Don't do anything if we still have pending frames */
376088b3d483SAdrian Chadd 	if (tid->hwq_depth > 0) {
37610e22ed0eSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
37626d07d3e0SAdrian Chadd 		    "%s: %6D: TID=%d, hwq_depth=%d, waiting\n",
376388b3d483SAdrian Chadd 		    __func__,
37649b48fb4bSAdrian Chadd 		    tid->an->an_node.ni_macaddr,
37659b48fb4bSAdrian Chadd 		    ":",
37666d07d3e0SAdrian Chadd 		    tid->tid,
376788b3d483SAdrian Chadd 		    tid->hwq_depth);
376888b3d483SAdrian Chadd 		return;
376988b3d483SAdrian Chadd 	}
377088b3d483SAdrian Chadd 
377188b3d483SAdrian Chadd 	/* We're now about to TX */
377288b3d483SAdrian Chadd 	tid->bar_tx = 1;
377388b3d483SAdrian Chadd 
377488b3d483SAdrian Chadd 	/*
37754e81f27cSAdrian Chadd 	 * Override the clrdmask configuration for the next frame,
37764e81f27cSAdrian Chadd 	 * just to get the ball rolling.
37774e81f27cSAdrian Chadd 	 */
37784f25ddbbSAdrian Chadd 	ath_tx_set_clrdmask(sc, tid->an);
37794e81f27cSAdrian Chadd 
37804e81f27cSAdrian Chadd 	/*
378188b3d483SAdrian Chadd 	 * Calculate new BAW left edge, now that all frames have either
378288b3d483SAdrian Chadd 	 * succeeded or failed.
378388b3d483SAdrian Chadd 	 *
378488b3d483SAdrian Chadd 	 * XXX verify this is _actually_ the valid value to begin at!
378588b3d483SAdrian Chadd 	 */
37860e22ed0eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
37876d07d3e0SAdrian Chadd 	    "%s: %6D: TID=%d, new BAW left edge=%d\n",
378888b3d483SAdrian Chadd 	    __func__,
37899b48fb4bSAdrian Chadd 	    tid->an->an_node.ni_macaddr,
37909b48fb4bSAdrian Chadd 	    ":",
37916d07d3e0SAdrian Chadd 	    tid->tid,
379288b3d483SAdrian Chadd 	    tap->txa_start);
379388b3d483SAdrian Chadd 
379488b3d483SAdrian Chadd 	/* Try sending the BAR frame */
379588b3d483SAdrian Chadd 	/* We can't hold the lock here! */
379688b3d483SAdrian Chadd 
3797375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
379888b3d483SAdrian Chadd 	if (ieee80211_send_bar(&tid->an->an_node, tap, tap->txa_start) == 0) {
379988b3d483SAdrian Chadd 		/* Success? Now we wait for notification that it's done */
3800375307d4SAdrian Chadd 		ATH_TX_LOCK(sc);
380188b3d483SAdrian Chadd 		return;
380288b3d483SAdrian Chadd 	}
380388b3d483SAdrian Chadd 
380488b3d483SAdrian Chadd 	/* Failure? For now, warn loudly and continue */
3805375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
380683bbd5ebSRui Paulo 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
38076d07d3e0SAdrian Chadd 	    "%s: %6D: TID=%d, failed to TX BAR, continue!\n",
380883bbd5ebSRui Paulo 	    __func__, tid->an->an_node.ni_macaddr, ":",
38096d07d3e0SAdrian Chadd 	    tid->tid);
381088b3d483SAdrian Chadd 	ath_tx_tid_bar_unsuspend(sc, tid);
381188b3d483SAdrian Chadd }
381288b3d483SAdrian Chadd 
3813eb6f0de0SAdrian Chadd static void
3814f1bc738eSAdrian Chadd ath_tx_tid_drain_pkt(struct ath_softc *sc, struct ath_node *an,
3815f1bc738eSAdrian Chadd     struct ath_tid *tid, ath_bufhead *bf_cq, struct ath_buf *bf)
3816eb6f0de0SAdrian Chadd {
3817eb6f0de0SAdrian Chadd 
3818375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
3819eb6f0de0SAdrian Chadd 
3820eb6f0de0SAdrian Chadd 	/*
3821eb6f0de0SAdrian Chadd 	 * If the current TID is running AMPDU, update
3822eb6f0de0SAdrian Chadd 	 * the BAW.
3823eb6f0de0SAdrian Chadd 	 */
3824eb6f0de0SAdrian Chadd 	if (ath_tx_ampdu_running(sc, an, tid->tid) &&
3825eb6f0de0SAdrian Chadd 	    bf->bf_state.bfs_dobaw) {
3826eb6f0de0SAdrian Chadd 		/*
3827eb6f0de0SAdrian Chadd 		 * Only remove the frame from the BAW if it's
3828eb6f0de0SAdrian Chadd 		 * been transmitted at least once; this means
3829eb6f0de0SAdrian Chadd 		 * the frame was in the BAW to begin with.
3830eb6f0de0SAdrian Chadd 		 */
3831eb6f0de0SAdrian Chadd 		if (bf->bf_state.bfs_retries > 0) {
3832eb6f0de0SAdrian Chadd 			ath_tx_update_baw(sc, an, tid, bf);
3833eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_dobaw = 0;
3834eb6f0de0SAdrian Chadd 		}
3835ce597531SAdrian Chadd #if 0
3836eb6f0de0SAdrian Chadd 		/*
3837eb6f0de0SAdrian Chadd 		 * This has become a non-fatal error now
3838eb6f0de0SAdrian Chadd 		 */
3839eb6f0de0SAdrian Chadd 		if (! bf->bf_state.bfs_addedbaw)
384083bbd5ebSRui Paulo 			DPRINTF(sc, ATH_DEBUG_SW_TX_BAW
3841eb6f0de0SAdrian Chadd 			    "%s: wasn't added: seqno %d\n",
3842eb6f0de0SAdrian Chadd 			    __func__, SEQNO(bf->bf_state.bfs_seqno));
3843ce597531SAdrian Chadd #endif
3844eb6f0de0SAdrian Chadd 	}
3845b837332dSAdrian Chadd 
3846b837332dSAdrian Chadd 	/* Strip it out of an aggregate list if it was in one */
3847b837332dSAdrian Chadd 	bf->bf_next = NULL;
3848b837332dSAdrian Chadd 
3849b837332dSAdrian Chadd 	/* Insert on the free queue to be freed by the caller */
3850eb6f0de0SAdrian Chadd 	TAILQ_INSERT_TAIL(bf_cq, bf, bf_list);
3851eb6f0de0SAdrian Chadd }
3852eb6f0de0SAdrian Chadd 
3853f1bc738eSAdrian Chadd static void
3854f1bc738eSAdrian Chadd ath_tx_tid_drain_print(struct ath_softc *sc, struct ath_node *an,
385503682514SAdrian Chadd     const char *pfx, struct ath_tid *tid, struct ath_buf *bf)
3856f1bc738eSAdrian Chadd {
3857f1bc738eSAdrian Chadd 	struct ieee80211_node *ni = &an->an_node;
385883bbd5ebSRui Paulo 	struct ath_txq *txq;
3859f1bc738eSAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
3860f1bc738eSAdrian Chadd 
386183bbd5ebSRui Paulo 	txq = sc->sc_ac2q[tid->ac];
3862f1bc738eSAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
3863f1bc738eSAdrian Chadd 
38646fc621c2SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX | ATH_DEBUG_RESET,
3865272a8ab6SAdrian Chadd 	    "%s: %s: %6D: bf=%p: addbaw=%d, dobaw=%d, "
3866f1bc738eSAdrian Chadd 	    "seqno=%d, retry=%d\n",
3867272a8ab6SAdrian Chadd 	    __func__,
3868272a8ab6SAdrian Chadd 	    pfx,
3869272a8ab6SAdrian Chadd 	    ni->ni_macaddr,
3870272a8ab6SAdrian Chadd 	    ":",
3871272a8ab6SAdrian Chadd 	    bf,
3872f1bc738eSAdrian Chadd 	    bf->bf_state.bfs_addedbaw,
3873f1bc738eSAdrian Chadd 	    bf->bf_state.bfs_dobaw,
3874f1bc738eSAdrian Chadd 	    SEQNO(bf->bf_state.bfs_seqno),
3875f1bc738eSAdrian Chadd 	    bf->bf_state.bfs_retries);
38766fc621c2SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX | ATH_DEBUG_RESET,
3877272a8ab6SAdrian Chadd 	    "%s: %s: %6D: bf=%p: txq[%d] axq_depth=%d, axq_aggr_depth=%d\n",
3878272a8ab6SAdrian Chadd 	    __func__,
3879272a8ab6SAdrian Chadd 	    pfx,
3880272a8ab6SAdrian Chadd 	    ni->ni_macaddr,
3881272a8ab6SAdrian Chadd 	    ":",
3882272a8ab6SAdrian Chadd 	    bf,
388303682514SAdrian Chadd 	    txq->axq_qnum,
38844e81f27cSAdrian Chadd 	    txq->axq_depth,
38854e81f27cSAdrian Chadd 	    txq->axq_aggr_depth);
38866fc621c2SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX | ATH_DEBUG_RESET,
3887272a8ab6SAdrian Chadd 	    "%s: %s: %6D: bf=%p: tid txq_depth=%d hwq_depth=%d, bar_wait=%d, "
3888272a8ab6SAdrian Chadd 	      "isfiltered=%d\n",
3889272a8ab6SAdrian Chadd 	    __func__,
3890272a8ab6SAdrian Chadd 	    pfx,
3891272a8ab6SAdrian Chadd 	    ni->ni_macaddr,
3892272a8ab6SAdrian Chadd 	    ":",
3893272a8ab6SAdrian Chadd 	    bf,
3894f1bc738eSAdrian Chadd 	    tid->axq_depth,
3895f1bc738eSAdrian Chadd 	    tid->hwq_depth,
3896f1bc738eSAdrian Chadd 	    tid->bar_wait,
3897f1bc738eSAdrian Chadd 	    tid->isfiltered);
38986fc621c2SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX | ATH_DEBUG_RESET,
3899272a8ab6SAdrian Chadd 	    "%s: %s: %6D: tid %d: "
39004e81f27cSAdrian Chadd 	    "sched=%d, paused=%d, "
39014e81f27cSAdrian Chadd 	    "incomp=%d, baw_head=%d, "
3902f1bc738eSAdrian Chadd 	    "baw_tail=%d txa_start=%d, ni_txseqs=%d\n",
3903272a8ab6SAdrian Chadd 	     __func__,
3904272a8ab6SAdrian Chadd 	     pfx,
3905272a8ab6SAdrian Chadd 	     ni->ni_macaddr,
3906272a8ab6SAdrian Chadd 	     ":",
3907272a8ab6SAdrian Chadd 	     tid->tid,
39084e81f27cSAdrian Chadd 	     tid->sched, tid->paused,
39094e81f27cSAdrian Chadd 	     tid->incomp, tid->baw_head,
3910f1bc738eSAdrian Chadd 	     tid->baw_tail, tap == NULL ? -1 : tap->txa_start,
3911f1bc738eSAdrian Chadd 	     ni->ni_txseqs[tid->tid]);
3912f1bc738eSAdrian Chadd 
3913f1bc738eSAdrian Chadd 	/* XXX Dump the frame, see what it is? */
3914a2be2710SRui Paulo 	if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
3915f1bc738eSAdrian Chadd 		ieee80211_dump_pkt(ni->ni_ic,
3916f1bc738eSAdrian Chadd 		    mtod(bf->bf_m, const uint8_t *),
3917f1bc738eSAdrian Chadd 		    bf->bf_m->m_len, 0, -1);
3918f1bc738eSAdrian Chadd }
3919f1bc738eSAdrian Chadd 
3920f1bc738eSAdrian Chadd /*
3921f1bc738eSAdrian Chadd  * Free any packets currently pending in the software TX queue.
3922f1bc738eSAdrian Chadd  *
3923f1bc738eSAdrian Chadd  * This will be called when a node is being deleted.
3924f1bc738eSAdrian Chadd  *
3925f1bc738eSAdrian Chadd  * It can also be called on an active node during an interface
3926f1bc738eSAdrian Chadd  * reset or state transition.
3927f1bc738eSAdrian Chadd  *
3928f1bc738eSAdrian Chadd  * (From Linux/reference):
3929f1bc738eSAdrian Chadd  *
3930f1bc738eSAdrian Chadd  * TODO: For frame(s) that are in the retry state, we will reuse the
3931f1bc738eSAdrian Chadd  * sequence number(s) without setting the retry bit. The
3932f1bc738eSAdrian Chadd  * alternative is to give up on these and BAR the receiver's window
3933f1bc738eSAdrian Chadd  * forward.
3934f1bc738eSAdrian Chadd  */
3935f1bc738eSAdrian Chadd static void
3936f1bc738eSAdrian Chadd ath_tx_tid_drain(struct ath_softc *sc, struct ath_node *an,
3937f1bc738eSAdrian Chadd     struct ath_tid *tid, ath_bufhead *bf_cq)
3938f1bc738eSAdrian Chadd {
3939f1bc738eSAdrian Chadd 	struct ath_buf *bf;
3940f1bc738eSAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
3941f1bc738eSAdrian Chadd 	struct ieee80211_node *ni = &an->an_node;
3942f1bc738eSAdrian Chadd 	int t;
3943f1bc738eSAdrian Chadd 
3944f1bc738eSAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
3945f1bc738eSAdrian Chadd 
3946375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
3947f1bc738eSAdrian Chadd 
3948f1bc738eSAdrian Chadd 	/* Walk the queue, free frames */
3949f1bc738eSAdrian Chadd 	t = 0;
3950f1bc738eSAdrian Chadd 	for (;;) {
39513e6cc97fSAdrian Chadd 		bf = ATH_TID_FIRST(tid);
3952f1bc738eSAdrian Chadd 		if (bf == NULL) {
3953f1bc738eSAdrian Chadd 			break;
3954f1bc738eSAdrian Chadd 		}
3955f1bc738eSAdrian Chadd 
3956f1bc738eSAdrian Chadd 		if (t == 0) {
395703682514SAdrian Chadd 			ath_tx_tid_drain_print(sc, an, "norm", tid, bf);
39586fc621c2SAdrian Chadd //			t = 1;
3959f1bc738eSAdrian Chadd 		}
3960f1bc738eSAdrian Chadd 
39613e6cc97fSAdrian Chadd 		ATH_TID_REMOVE(tid, bf, bf_list);
3962f1bc738eSAdrian Chadd 		ath_tx_tid_drain_pkt(sc, an, tid, bf_cq, bf);
3963f1bc738eSAdrian Chadd 	}
3964f1bc738eSAdrian Chadd 
3965f1bc738eSAdrian Chadd 	/* And now, drain the filtered frame queue */
3966f1bc738eSAdrian Chadd 	t = 0;
3967f1bc738eSAdrian Chadd 	for (;;) {
396813aa9ee5SAdrian Chadd 		bf = ATH_TID_FILT_FIRST(tid);
3969f1bc738eSAdrian Chadd 		if (bf == NULL)
3970f1bc738eSAdrian Chadd 			break;
3971f1bc738eSAdrian Chadd 
3972f1bc738eSAdrian Chadd 		if (t == 0) {
397303682514SAdrian Chadd 			ath_tx_tid_drain_print(sc, an, "filt", tid, bf);
39746fc621c2SAdrian Chadd //			t = 1;
3975f1bc738eSAdrian Chadd 		}
3976f1bc738eSAdrian Chadd 
397713aa9ee5SAdrian Chadd 		ATH_TID_FILT_REMOVE(tid, bf, bf_list);
3978f1bc738eSAdrian Chadd 		ath_tx_tid_drain_pkt(sc, an, tid, bf_cq, bf);
3979f1bc738eSAdrian Chadd 	}
3980f1bc738eSAdrian Chadd 
3981eb6f0de0SAdrian Chadd 	/*
39824e81f27cSAdrian Chadd 	 * Override the clrdmask configuration for the next frame
39834e81f27cSAdrian Chadd 	 * in case there is some future transmission, just to get
39844e81f27cSAdrian Chadd 	 * the ball rolling.
39854e81f27cSAdrian Chadd 	 *
39864e81f27cSAdrian Chadd 	 * This won't hurt things if the TID is about to be freed.
39874e81f27cSAdrian Chadd 	 */
39884f25ddbbSAdrian Chadd 	ath_tx_set_clrdmask(sc, tid->an);
39894e81f27cSAdrian Chadd 
39904e81f27cSAdrian Chadd 	/*
3991eb6f0de0SAdrian Chadd 	 * Now that it's completed, grab the TID lock and update
3992eb6f0de0SAdrian Chadd 	 * the sequence number and BAW window.
3993eb6f0de0SAdrian Chadd 	 * Because sequence numbers have been assigned to frames
3994eb6f0de0SAdrian Chadd 	 * that haven't been sent yet, it's entirely possible
3995eb6f0de0SAdrian Chadd 	 * we'll be called with some pending frames that have not
3996eb6f0de0SAdrian Chadd 	 * been transmitted.
3997eb6f0de0SAdrian Chadd 	 *
3998eb6f0de0SAdrian Chadd 	 * The cleaner solution is to do the sequence number allocation
3999eb6f0de0SAdrian Chadd 	 * when the packet is first transmitted - and thus the "retries"
4000eb6f0de0SAdrian Chadd 	 * check above would be enough to update the BAW/seqno.
4001eb6f0de0SAdrian Chadd 	 */
4002eb6f0de0SAdrian Chadd 
4003eb6f0de0SAdrian Chadd 	/* But don't do it for non-QoS TIDs */
4004eb6f0de0SAdrian Chadd 	if (tap) {
40059b48fb4bSAdrian Chadd #if 1
4006eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
40079b48fb4bSAdrian Chadd 		    "%s: %6D: node %p: TID %d: sliding BAW left edge to %d\n",
40089b48fb4bSAdrian Chadd 		    __func__,
40099b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
40109b48fb4bSAdrian Chadd 		    ":",
40119b48fb4bSAdrian Chadd 		    an,
40129b48fb4bSAdrian Chadd 		    tid->tid,
40139b48fb4bSAdrian Chadd 		    tap->txa_start);
4014eb6f0de0SAdrian Chadd #endif
4015eb6f0de0SAdrian Chadd 		ni->ni_txseqs[tid->tid] = tap->txa_start;
4016eb6f0de0SAdrian Chadd 		tid->baw_tail = tid->baw_head;
4017eb6f0de0SAdrian Chadd 	}
4018eb6f0de0SAdrian Chadd }
4019eb6f0de0SAdrian Chadd 
4020eb6f0de0SAdrian Chadd /*
402122780332SAdrian Chadd  * Reset the TID state.  This must be only called once the node has
402222780332SAdrian Chadd  * had its frames flushed from this TID, to ensure that no other
402322780332SAdrian Chadd  * pause / unpause logic can kick in.
402422780332SAdrian Chadd  */
402522780332SAdrian Chadd static void
402622780332SAdrian Chadd ath_tx_tid_reset(struct ath_softc *sc, struct ath_tid *tid)
402722780332SAdrian Chadd {
402822780332SAdrian Chadd 
402922780332SAdrian Chadd #if 0
403022780332SAdrian Chadd 	tid->bar_wait = tid->bar_tx = tid->isfiltered = 0;
403122780332SAdrian Chadd 	tid->paused = tid->sched = tid->addba_tx_pending = 0;
403222780332SAdrian Chadd 	tid->incomp = tid->cleanup_inprogress = 0;
403322780332SAdrian Chadd #endif
403422780332SAdrian Chadd 
403522780332SAdrian Chadd 	/*
403622780332SAdrian Chadd 	 * If we have a bar_wait set, we need to unpause the TID
403722780332SAdrian Chadd 	 * here.  Otherwise once cleanup has finished, the TID won't
403822780332SAdrian Chadd 	 * have the right paused counter.
403922780332SAdrian Chadd 	 *
404022780332SAdrian Chadd 	 * XXX I'm not going through resume here - I don't want the
404122780332SAdrian Chadd 	 * node to be rescheuled just yet.  This however should be
404222780332SAdrian Chadd 	 * methodized!
404322780332SAdrian Chadd 	 */
404422780332SAdrian Chadd 	if (tid->bar_wait) {
404522780332SAdrian Chadd 		if (tid->paused > 0) {
404622780332SAdrian Chadd 			tid->paused --;
404722780332SAdrian Chadd 		}
404822780332SAdrian Chadd 	}
404922780332SAdrian Chadd 
405022780332SAdrian Chadd 	/*
405122780332SAdrian Chadd 	 * XXX same with a currently filtered TID.
405222780332SAdrian Chadd 	 *
405322780332SAdrian Chadd 	 * Since this is being called during a flush, we assume that
405422780332SAdrian Chadd 	 * the filtered frame list is actually empty.
405522780332SAdrian Chadd 	 *
405622780332SAdrian Chadd 	 * XXX TODO: add in a check to ensure that the filtered queue
405722780332SAdrian Chadd 	 * depth is actually 0!
405822780332SAdrian Chadd 	 */
405922780332SAdrian Chadd 	if (tid->isfiltered) {
406022780332SAdrian Chadd 		if (tid->paused > 0) {
406122780332SAdrian Chadd 			tid->paused --;
406222780332SAdrian Chadd 		}
406322780332SAdrian Chadd 	}
406422780332SAdrian Chadd 
406522780332SAdrian Chadd 	/*
406622780332SAdrian Chadd 	 * Clear BAR, filtered frames, scheduled and ADDBA pending.
406722780332SAdrian Chadd 	 * The TID may be going through cleanup from the last association
406822780332SAdrian Chadd 	 * where things in the BAW are still in the hardware queue.
406922780332SAdrian Chadd 	 */
407022780332SAdrian Chadd 	tid->bar_wait = 0;
407122780332SAdrian Chadd 	tid->bar_tx = 0;
407222780332SAdrian Chadd 	tid->isfiltered = 0;
407322780332SAdrian Chadd 	tid->sched = 0;
407422780332SAdrian Chadd 	tid->addba_tx_pending = 0;
407522780332SAdrian Chadd 
407622780332SAdrian Chadd 	/*
407722780332SAdrian Chadd 	 * XXX TODO: it may just be enough to walk the HWQs and mark
407822780332SAdrian Chadd 	 * frames for that node as non-aggregate; or mark the ath_node
407922780332SAdrian Chadd 	 * with something that indicates that aggregation is no longer
4080f6b6084bSPedro F. Giffuni 	 * occurring.  Then we can just toss the BAW complaints and
408122780332SAdrian Chadd 	 * do a complete hard reset of state here - no pause, no
408222780332SAdrian Chadd 	 * complete counter, etc.
408322780332SAdrian Chadd 	 */
408422a3aee6SAdrian Chadd 
408522780332SAdrian Chadd }
408622780332SAdrian Chadd 
408722780332SAdrian Chadd /*
4088eb6f0de0SAdrian Chadd  * Flush all software queued packets for the given node.
4089eb6f0de0SAdrian Chadd  *
4090eb6f0de0SAdrian Chadd  * This occurs when a completion handler frees the last buffer
4091eb6f0de0SAdrian Chadd  * for a node, and the node is thus freed. This causes the node
4092eb6f0de0SAdrian Chadd  * to be cleaned up, which ends up calling ath_tx_node_flush.
4093eb6f0de0SAdrian Chadd  */
4094eb6f0de0SAdrian Chadd void
4095eb6f0de0SAdrian Chadd ath_tx_node_flush(struct ath_softc *sc, struct ath_node *an)
4096eb6f0de0SAdrian Chadd {
4097eb6f0de0SAdrian Chadd 	int tid;
4098eb6f0de0SAdrian Chadd 	ath_bufhead bf_cq;
4099eb6f0de0SAdrian Chadd 	struct ath_buf *bf;
4100eb6f0de0SAdrian Chadd 
4101eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_cq);
4102eb6f0de0SAdrian Chadd 
410303682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_NODE, 1, "ath_tx_node_flush: flush node; ni=%p",
410403682514SAdrian Chadd 	    &an->an_node);
410503682514SAdrian Chadd 
4106375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
41079b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE,
41089b48fb4bSAdrian Chadd 	    "%s: %6D: flush; is_powersave=%d, stack_psq=%d, tim=%d, "
410922a3aee6SAdrian Chadd 	    "swq_depth=%d, clrdmask=%d, leak_count=%d\n",
41109b48fb4bSAdrian Chadd 	    __func__,
41119b48fb4bSAdrian Chadd 	    an->an_node.ni_macaddr,
41129b48fb4bSAdrian Chadd 	    ":",
41139b48fb4bSAdrian Chadd 	    an->an_is_powersave,
41149b48fb4bSAdrian Chadd 	    an->an_stack_psq,
41159b48fb4bSAdrian Chadd 	    an->an_tim_set,
41169b48fb4bSAdrian Chadd 	    an->an_swq_depth,
411722a3aee6SAdrian Chadd 	    an->clrdmask,
411822a3aee6SAdrian Chadd 	    an->an_leak_count);
41199b48fb4bSAdrian Chadd 
4120eb6f0de0SAdrian Chadd 	for (tid = 0; tid < IEEE80211_TID_SIZE; tid++) {
4121eb6f0de0SAdrian Chadd 		struct ath_tid *atid = &an->an_tid[tid];
4122eb6f0de0SAdrian Chadd 
4123eb6f0de0SAdrian Chadd 		/* Free packets */
4124eb6f0de0SAdrian Chadd 		ath_tx_tid_drain(sc, an, atid, &bf_cq);
412522a3aee6SAdrian Chadd 
412623f44d2bSAdrian Chadd 		/* Remove this tid from the list of active tids */
412723f44d2bSAdrian Chadd 		ath_tx_tid_unsched(sc, atid);
412822a3aee6SAdrian Chadd 
412922780332SAdrian Chadd 		/* Reset the per-TID pause, BAR, etc state */
413022780332SAdrian Chadd 		ath_tx_tid_reset(sc, atid);
4131eb6f0de0SAdrian Chadd 	}
413222a3aee6SAdrian Chadd 
413322a3aee6SAdrian Chadd 	/*
413422a3aee6SAdrian Chadd 	 * Clear global leak count
413522a3aee6SAdrian Chadd 	 */
413622a3aee6SAdrian Chadd 	an->an_leak_count = 0;
4137375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
4138eb6f0de0SAdrian Chadd 
4139eb6f0de0SAdrian Chadd 	/* Handle completed frames */
4140eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
4141eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
4142eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
4143eb6f0de0SAdrian Chadd 	}
4144eb6f0de0SAdrian Chadd }
4145eb6f0de0SAdrian Chadd 
4146eb6f0de0SAdrian Chadd /*
4147eb6f0de0SAdrian Chadd  * Drain all the software TXQs currently with traffic queued.
4148eb6f0de0SAdrian Chadd  */
4149eb6f0de0SAdrian Chadd void
4150eb6f0de0SAdrian Chadd ath_tx_txq_drain(struct ath_softc *sc, struct ath_txq *txq)
4151eb6f0de0SAdrian Chadd {
4152eb6f0de0SAdrian Chadd 	struct ath_tid *tid;
4153eb6f0de0SAdrian Chadd 	ath_bufhead bf_cq;
4154eb6f0de0SAdrian Chadd 	struct ath_buf *bf;
4155eb6f0de0SAdrian Chadd 
4156eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_cq);
4157375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
4158eb6f0de0SAdrian Chadd 
4159eb6f0de0SAdrian Chadd 	/*
4160eb6f0de0SAdrian Chadd 	 * Iterate over all active tids for the given txq,
4161eb6f0de0SAdrian Chadd 	 * flushing and unsched'ing them
4162eb6f0de0SAdrian Chadd 	 */
4163eb6f0de0SAdrian Chadd 	while (! TAILQ_EMPTY(&txq->axq_tidq)) {
4164eb6f0de0SAdrian Chadd 		tid = TAILQ_FIRST(&txq->axq_tidq);
4165eb6f0de0SAdrian Chadd 		ath_tx_tid_drain(sc, tid->an, tid, &bf_cq);
4166eb6f0de0SAdrian Chadd 		ath_tx_tid_unsched(sc, tid);
4167eb6f0de0SAdrian Chadd 	}
4168eb6f0de0SAdrian Chadd 
4169375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
4170eb6f0de0SAdrian Chadd 
4171eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
4172eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
4173eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
4174eb6f0de0SAdrian Chadd 	}
4175eb6f0de0SAdrian Chadd }
4176eb6f0de0SAdrian Chadd 
4177eb6f0de0SAdrian Chadd /*
4178eb6f0de0SAdrian Chadd  * Handle completion of non-aggregate session frames.
41790c54de88SAdrian Chadd  *
41800c54de88SAdrian Chadd  * This (currently) doesn't implement software retransmission of
41810c54de88SAdrian Chadd  * non-aggregate frames!
41820c54de88SAdrian Chadd  *
41830c54de88SAdrian Chadd  * Software retransmission of non-aggregate frames needs to obey
41840c54de88SAdrian Chadd  * the strict sequence number ordering, and drop any frames that
41850c54de88SAdrian Chadd  * will fail this.
41860c54de88SAdrian Chadd  *
41870c54de88SAdrian Chadd  * For now, filtered frames and frame transmission will cause
41880c54de88SAdrian Chadd  * all kinds of issues.  So we don't support them.
41890c54de88SAdrian Chadd  *
41900c54de88SAdrian Chadd  * So anyone queuing frames via ath_tx_normal_xmit() or
41910c54de88SAdrian Chadd  * ath_tx_hw_queue_norm() must override and set CLRDMASK.
4192eb6f0de0SAdrian Chadd  */
4193eb6f0de0SAdrian Chadd void
4194eb6f0de0SAdrian Chadd ath_tx_normal_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
4195eb6f0de0SAdrian Chadd {
4196eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
4197eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4198eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
4199eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4200eb6f0de0SAdrian Chadd 	struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
4201eb6f0de0SAdrian Chadd 
4202eb6f0de0SAdrian Chadd 	/* The TID state is protected behind the TXQ lock */
4203375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
4204eb6f0de0SAdrian Chadd 
4205eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bf=%p: fail=%d, hwq_depth now %d\n",
4206eb6f0de0SAdrian Chadd 	    __func__, bf, fail, atid->hwq_depth - 1);
4207eb6f0de0SAdrian Chadd 
4208eb6f0de0SAdrian Chadd 	atid->hwq_depth--;
4209f1bc738eSAdrian Chadd 
42100c54de88SAdrian Chadd #if 0
42110c54de88SAdrian Chadd 	/*
42120c54de88SAdrian Chadd 	 * If the frame was filtered, stick it on the filter frame
42130c54de88SAdrian Chadd 	 * queue and complain about it.  It shouldn't happen!
42140c54de88SAdrian Chadd 	 */
42150c54de88SAdrian Chadd 	if ((ts->ts_status & HAL_TXERR_FILT) ||
42160c54de88SAdrian Chadd 	    (ts->ts_status != 0 && atid->isfiltered)) {
421783bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX,
42180c54de88SAdrian Chadd 		    "%s: isfiltered=%d, ts_status=%d: huh?\n",
42190c54de88SAdrian Chadd 		    __func__,
42200c54de88SAdrian Chadd 		    atid->isfiltered,
42210c54de88SAdrian Chadd 		    ts->ts_status);
42220c54de88SAdrian Chadd 		ath_tx_tid_filt_comp_buf(sc, atid, bf);
42230c54de88SAdrian Chadd 	}
42240c54de88SAdrian Chadd #endif
4225f1bc738eSAdrian Chadd 	if (atid->isfiltered)
422683bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: filtered?!\n", __func__);
4227eb6f0de0SAdrian Chadd 	if (atid->hwq_depth < 0)
422883bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: hwq_depth < 0: %d\n",
4229eb6f0de0SAdrian Chadd 		    __func__, atid->hwq_depth);
4230f1bc738eSAdrian Chadd 
4231f172ef75SAdrian Chadd 	/* If the TID is being cleaned up, track things */
4232f172ef75SAdrian Chadd 	/* XXX refactor! */
4233f172ef75SAdrian Chadd 	if (atid->cleanup_inprogress) {
4234f172ef75SAdrian Chadd 		atid->incomp--;
4235f172ef75SAdrian Chadd 		if (atid->incomp == 0) {
4236f172ef75SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4237f172ef75SAdrian Chadd 			    "%s: TID %d: cleaned up! resume!\n",
4238f172ef75SAdrian Chadd 			    __func__, tid);
4239f172ef75SAdrian Chadd 			atid->cleanup_inprogress = 0;
4240f172ef75SAdrian Chadd 			ath_tx_tid_resume(sc, atid);
4241f172ef75SAdrian Chadd 		}
4242f172ef75SAdrian Chadd 	}
4243f172ef75SAdrian Chadd 
4244f1bc738eSAdrian Chadd 	/*
4245f1bc738eSAdrian Chadd 	 * If the queue is filtered, potentially mark it as complete
4246f1bc738eSAdrian Chadd 	 * and reschedule it as needed.
4247f1bc738eSAdrian Chadd 	 *
4248f1bc738eSAdrian Chadd 	 * This is required as there may be a subsequent TX descriptor
4249f1bc738eSAdrian Chadd 	 * for this end-node that has CLRDMASK set, so it's quite possible
4250f1bc738eSAdrian Chadd 	 * that a filtered frame will be followed by a non-filtered
4251f1bc738eSAdrian Chadd 	 * (complete or otherwise) frame.
4252f1bc738eSAdrian Chadd 	 *
4253f1bc738eSAdrian Chadd 	 * XXX should we do this before we complete the frame?
4254f1bc738eSAdrian Chadd 	 */
4255f1bc738eSAdrian Chadd 	if (atid->isfiltered)
4256f1bc738eSAdrian Chadd 		ath_tx_tid_filt_comp_complete(sc, atid);
4257375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
4258eb6f0de0SAdrian Chadd 
4259eb6f0de0SAdrian Chadd 	/*
4260eb6f0de0SAdrian Chadd 	 * punt to rate control if we're not being cleaned up
4261eb6f0de0SAdrian Chadd 	 * during a hw queue drain and the frame wanted an ACK.
4262eb6f0de0SAdrian Chadd 	 */
4263875a9451SAdrian Chadd 	if (fail == 0 && ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0))
4264eb6f0de0SAdrian Chadd 		ath_tx_update_ratectrl(sc, ni, bf->bf_state.bfs_rc,
4265*cce63444SAdrian Chadd 		    ts,
4266*cce63444SAdrian Chadd 		    bf->bf_state.bfs_pktlen,
4267*cce63444SAdrian Chadd 		    bf->bf_state.bfs_pktlen,
4268eb6f0de0SAdrian Chadd 		    1, (ts->ts_status == 0) ? 0 : 1);
4269eb6f0de0SAdrian Chadd 
4270eb6f0de0SAdrian Chadd 	ath_tx_default_comp(sc, bf, fail);
4271eb6f0de0SAdrian Chadd }
4272eb6f0de0SAdrian Chadd 
4273eb6f0de0SAdrian Chadd /*
4274eb6f0de0SAdrian Chadd  * Handle cleanup of aggregate session packets that aren't
4275eb6f0de0SAdrian Chadd  * an A-MPDU.
4276eb6f0de0SAdrian Chadd  *
4277eb6f0de0SAdrian Chadd  * There's no need to update the BAW here - the session is being
4278eb6f0de0SAdrian Chadd  * torn down.
4279eb6f0de0SAdrian Chadd  */
4280eb6f0de0SAdrian Chadd static void
4281eb6f0de0SAdrian Chadd ath_tx_comp_cleanup_unaggr(struct ath_softc *sc, struct ath_buf *bf)
4282eb6f0de0SAdrian Chadd {
4283eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
4284eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4285eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
4286eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4287eb6f0de0SAdrian Chadd 
4288eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: TID %d: incomp=%d\n",
4289eb6f0de0SAdrian Chadd 	    __func__, tid, atid->incomp);
4290eb6f0de0SAdrian Chadd 
4291375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
4292eb6f0de0SAdrian Chadd 	atid->incomp--;
4293f172ef75SAdrian Chadd 
4294f172ef75SAdrian Chadd 	/* XXX refactor! */
4295f172ef75SAdrian Chadd 	if (bf->bf_state.bfs_dobaw) {
4296f172ef75SAdrian Chadd 		ath_tx_update_baw(sc, an, atid, bf);
4297f172ef75SAdrian Chadd 		if (!bf->bf_state.bfs_addedbaw)
4298f172ef75SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX,
4299f172ef75SAdrian Chadd 			    "%s: wasn't added: seqno %d\n",
4300f172ef75SAdrian Chadd 			    __func__, SEQNO(bf->bf_state.bfs_seqno));
4301f172ef75SAdrian Chadd 	}
4302f172ef75SAdrian Chadd 
4303eb6f0de0SAdrian Chadd 	if (atid->incomp == 0) {
4304eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4305eb6f0de0SAdrian Chadd 		    "%s: TID %d: cleaned up! resume!\n",
4306eb6f0de0SAdrian Chadd 		    __func__, tid);
4307eb6f0de0SAdrian Chadd 		atid->cleanup_inprogress = 0;
4308eb6f0de0SAdrian Chadd 		ath_tx_tid_resume(sc, atid);
4309eb6f0de0SAdrian Chadd 	}
4310375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
4311eb6f0de0SAdrian Chadd 
4312eb6f0de0SAdrian Chadd 	ath_tx_default_comp(sc, bf, 0);
4313eb6f0de0SAdrian Chadd }
4314eb6f0de0SAdrian Chadd 
4315f172ef75SAdrian Chadd 
4316f172ef75SAdrian Chadd /*
4317f172ef75SAdrian Chadd  * This as it currently stands is a bit dumb.  Ideally we'd just
4318f172ef75SAdrian Chadd  * fail the frame the normal way and have it permanently fail
4319f172ef75SAdrian Chadd  * via the normal aggregate completion path.
4320f172ef75SAdrian Chadd  */
4321f172ef75SAdrian Chadd static void
4322f172ef75SAdrian Chadd ath_tx_tid_cleanup_frame(struct ath_softc *sc, struct ath_node *an,
4323f172ef75SAdrian Chadd     int tid, struct ath_buf *bf_head, ath_bufhead *bf_cq)
4324f172ef75SAdrian Chadd {
4325f172ef75SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4326f172ef75SAdrian Chadd 	struct ath_buf *bf, *bf_next;
4327f172ef75SAdrian Chadd 
4328f172ef75SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
4329f172ef75SAdrian Chadd 
4330f172ef75SAdrian Chadd 	/*
4331f172ef75SAdrian Chadd 	 * Remove this frame from the queue.
4332f172ef75SAdrian Chadd 	 */
4333f172ef75SAdrian Chadd 	ATH_TID_REMOVE(atid, bf_head, bf_list);
4334f172ef75SAdrian Chadd 
4335f172ef75SAdrian Chadd 	/*
4336f172ef75SAdrian Chadd 	 * Loop over all the frames in the aggregate.
4337f172ef75SAdrian Chadd 	 */
4338f172ef75SAdrian Chadd 	bf = bf_head;
4339f172ef75SAdrian Chadd 	while (bf != NULL) {
4340f172ef75SAdrian Chadd 		bf_next = bf->bf_next;	/* next aggregate frame, or NULL */
4341f172ef75SAdrian Chadd 
4342f172ef75SAdrian Chadd 		/*
4343f172ef75SAdrian Chadd 		 * If it's been added to the BAW we need to kick
4344f172ef75SAdrian Chadd 		 * it out of the BAW before we continue.
4345f172ef75SAdrian Chadd 		 *
4346f172ef75SAdrian Chadd 		 * XXX if it's an aggregate, assert that it's in the
4347f172ef75SAdrian Chadd 		 * BAW - we shouldn't have it be in an aggregate
4348f172ef75SAdrian Chadd 		 * otherwise!
4349f172ef75SAdrian Chadd 		 */
4350f172ef75SAdrian Chadd 		if (bf->bf_state.bfs_addedbaw) {
4351f172ef75SAdrian Chadd 			ath_tx_update_baw(sc, an, atid, bf);
4352f172ef75SAdrian Chadd 			bf->bf_state.bfs_dobaw = 0;
4353f172ef75SAdrian Chadd 		}
4354f172ef75SAdrian Chadd 
4355f172ef75SAdrian Chadd 		/*
4356f172ef75SAdrian Chadd 		 * Give it the default completion handler.
4357f172ef75SAdrian Chadd 		 */
4358f172ef75SAdrian Chadd 		bf->bf_comp = ath_tx_normal_comp;
4359f172ef75SAdrian Chadd 		bf->bf_next = NULL;
4360f172ef75SAdrian Chadd 
4361f172ef75SAdrian Chadd 		/*
4362f172ef75SAdrian Chadd 		 * Add it to the list to free.
4363f172ef75SAdrian Chadd 		 */
4364f172ef75SAdrian Chadd 		TAILQ_INSERT_TAIL(bf_cq, bf, bf_list);
4365f172ef75SAdrian Chadd 
4366f172ef75SAdrian Chadd 		/*
4367f172ef75SAdrian Chadd 		 * Now advance to the next frame in the aggregate.
4368f172ef75SAdrian Chadd 		 */
4369f172ef75SAdrian Chadd 		bf = bf_next;
4370f172ef75SAdrian Chadd 	}
4371f172ef75SAdrian Chadd }
4372f172ef75SAdrian Chadd 
4373eb6f0de0SAdrian Chadd /*
4374eb6f0de0SAdrian Chadd  * Performs transmit side cleanup when TID changes from aggregated to
4375f172ef75SAdrian Chadd  * unaggregated and during reassociation.
4376eb6f0de0SAdrian Chadd  *
4377f172ef75SAdrian Chadd  * For now, this just tosses everything from the TID software queue
4378f172ef75SAdrian Chadd  * whether or not it has been retried and marks the TID as
4379f172ef75SAdrian Chadd  * pending completion if there's anything for this TID queued to
4380f172ef75SAdrian Chadd  * the hardware.
4381eb6f0de0SAdrian Chadd  *
43825da3fc10SAdrian Chadd  * The caller is responsible for pausing the TID and unpausing the
43835da3fc10SAdrian Chadd  * TID if no cleanup was required. Otherwise the cleanup path will
43845da3fc10SAdrian Chadd  * unpause the TID once the last hardware queued frame is completed.
4385eb6f0de0SAdrian Chadd  */
4386eb6f0de0SAdrian Chadd static void
438722780332SAdrian Chadd ath_tx_tid_cleanup(struct ath_softc *sc, struct ath_node *an, int tid,
438822780332SAdrian Chadd     ath_bufhead *bf_cq)
4389eb6f0de0SAdrian Chadd {
4390eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4391eb6f0de0SAdrian Chadd 	struct ath_buf *bf, *bf_next;
439222780332SAdrian Chadd 
439322780332SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
4394eb6f0de0SAdrian Chadd 
4395d3a6425bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
4396f172ef75SAdrian Chadd 	    "%s: TID %d: called; inprogress=%d\n", __func__, tid,
4397f172ef75SAdrian Chadd 	    atid->cleanup_inprogress);
4398eb6f0de0SAdrian Chadd 
4399eb6f0de0SAdrian Chadd 	/*
4400f1bc738eSAdrian Chadd 	 * Move the filtered frames to the TX queue, before
4401f1bc738eSAdrian Chadd 	 * we run off and discard/process things.
4402f1bc738eSAdrian Chadd 	 */
4403f172ef75SAdrian Chadd 
4404f1bc738eSAdrian Chadd 	/* XXX this is really quite inefficient */
440513aa9ee5SAdrian Chadd 	while ((bf = ATH_TID_FILT_LAST(atid, ath_bufhead_s)) != NULL) {
440613aa9ee5SAdrian Chadd 		ATH_TID_FILT_REMOVE(atid, bf, bf_list);
44073e6cc97fSAdrian Chadd 		ATH_TID_INSERT_HEAD(atid, bf, bf_list);
4408f1bc738eSAdrian Chadd 	}
4409f1bc738eSAdrian Chadd 
4410f1bc738eSAdrian Chadd 	/*
4411eb6f0de0SAdrian Chadd 	 * Update the frames in the software TX queue:
4412eb6f0de0SAdrian Chadd 	 *
4413eb6f0de0SAdrian Chadd 	 * + Discard retry frames in the queue
4414eb6f0de0SAdrian Chadd 	 * + Fix the completion function to be non-aggregate
4415eb6f0de0SAdrian Chadd 	 */
44163e6cc97fSAdrian Chadd 	bf = ATH_TID_FIRST(atid);
4417eb6f0de0SAdrian Chadd 	while (bf) {
4418eb6f0de0SAdrian Chadd 		/*
4419f172ef75SAdrian Chadd 		 * Grab the next frame in the list, we may
4420f172ef75SAdrian Chadd 		 * be fiddling with the list.
4421eb6f0de0SAdrian Chadd 		 */
4422f172ef75SAdrian Chadd 		bf_next = TAILQ_NEXT(bf, bf_list);
4423f172ef75SAdrian Chadd 
4424f172ef75SAdrian Chadd 		/*
4425f172ef75SAdrian Chadd 		 * Free the frame and all subframes.
4426f172ef75SAdrian Chadd 		 */
4427f172ef75SAdrian Chadd 		ath_tx_tid_cleanup_frame(sc, an, tid, bf, bf_cq);
4428f172ef75SAdrian Chadd 
4429f172ef75SAdrian Chadd 		/*
4430f172ef75SAdrian Chadd 		 * Next frame!
4431f172ef75SAdrian Chadd 		 */
4432eb6f0de0SAdrian Chadd 		bf = bf_next;
4433eb6f0de0SAdrian Chadd 	}
4434eb6f0de0SAdrian Chadd 
4435eb6f0de0SAdrian Chadd 	/*
4436f172ef75SAdrian Chadd 	 * If there's anything in the hardware queue we wait
4437f172ef75SAdrian Chadd 	 * for the TID HWQ to empty.
4438eb6f0de0SAdrian Chadd 	 */
4439f172ef75SAdrian Chadd 	if (atid->hwq_depth > 0) {
4440f172ef75SAdrian Chadd 		/*
4441f172ef75SAdrian Chadd 		 * XXX how about we kill atid->incomp, and instead
4442f172ef75SAdrian Chadd 		 * replace it with a macro that checks that atid->hwq_depth
4443f172ef75SAdrian Chadd 		 * is 0?
4444f172ef75SAdrian Chadd 		 */
4445f172ef75SAdrian Chadd 		atid->incomp = atid->hwq_depth;
4446eb6f0de0SAdrian Chadd 		atid->cleanup_inprogress = 1;
4447eb6f0de0SAdrian Chadd 	}
4448eb6f0de0SAdrian Chadd 
4449eb6f0de0SAdrian Chadd 	if (atid->cleanup_inprogress)
4450eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4451eb6f0de0SAdrian Chadd 		    "%s: TID %d: cleanup needed: %d packets\n",
4452eb6f0de0SAdrian Chadd 		    __func__, tid, atid->incomp);
4453eb6f0de0SAdrian Chadd 
445422780332SAdrian Chadd 	/* Owner now must free completed frames */
4455eb6f0de0SAdrian Chadd }
4456eb6f0de0SAdrian Chadd 
4457eb6f0de0SAdrian Chadd static struct ath_buf *
445838962489SAdrian Chadd ath_tx_retry_clone(struct ath_softc *sc, struct ath_node *an,
445938962489SAdrian Chadd     struct ath_tid *tid, struct ath_buf *bf)
4460eb6f0de0SAdrian Chadd {
4461eb6f0de0SAdrian Chadd 	struct ath_buf *nbf;
4462eb6f0de0SAdrian Chadd 	int error;
4463eb6f0de0SAdrian Chadd 
44643f3a5dbdSAdrian Chadd 	/*
44653f3a5dbdSAdrian Chadd 	 * Clone the buffer.  This will handle the dma unmap and
44663f3a5dbdSAdrian Chadd 	 * copy the node reference to the new buffer.  If this
44673f3a5dbdSAdrian Chadd 	 * works out, 'bf' will have no DMA mapping, no mbuf
44683f3a5dbdSAdrian Chadd 	 * pointer and no node reference.
44693f3a5dbdSAdrian Chadd 	 */
4470eb6f0de0SAdrian Chadd 	nbf = ath_buf_clone(sc, bf);
4471eb6f0de0SAdrian Chadd 
4472eb6f0de0SAdrian Chadd #if 0
447383bbd5ebSRui Paulo 	DPRINTF(sc, ATH_DEBUG_XMIT, "%s: ATH_BUF_BUSY; cloning\n",
4474eb6f0de0SAdrian Chadd 	    __func__);
4475eb6f0de0SAdrian Chadd #endif
4476eb6f0de0SAdrian Chadd 
4477eb6f0de0SAdrian Chadd 	if (nbf == NULL) {
4478eb6f0de0SAdrian Chadd 		/* Failed to clone */
447983bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_XMIT,
4480eb6f0de0SAdrian Chadd 		    "%s: failed to clone a busy buffer\n",
4481eb6f0de0SAdrian Chadd 		    __func__);
4482eb6f0de0SAdrian Chadd 		return NULL;
4483eb6f0de0SAdrian Chadd 	}
4484eb6f0de0SAdrian Chadd 
4485eb6f0de0SAdrian Chadd 	/* Setup the dma for the new buffer */
4486eb6f0de0SAdrian Chadd 	error = ath_tx_dmasetup(sc, nbf, nbf->bf_m);
4487eb6f0de0SAdrian Chadd 	if (error != 0) {
448883bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_XMIT,
4489eb6f0de0SAdrian Chadd 		    "%s: failed to setup dma for clone\n",
4490eb6f0de0SAdrian Chadd 		    __func__);
4491eb6f0de0SAdrian Chadd 		/*
4492eb6f0de0SAdrian Chadd 		 * Put this at the head of the list, not tail;
4493eb6f0de0SAdrian Chadd 		 * that way it doesn't interfere with the
4494eb6f0de0SAdrian Chadd 		 * busy buffer logic (which uses the tail of
4495eb6f0de0SAdrian Chadd 		 * the list.)
4496eb6f0de0SAdrian Chadd 		 */
4497eb6f0de0SAdrian Chadd 		ATH_TXBUF_LOCK(sc);
449832c387f7SAdrian Chadd 		ath_returnbuf_head(sc, nbf);
4499eb6f0de0SAdrian Chadd 		ATH_TXBUF_UNLOCK(sc);
4500eb6f0de0SAdrian Chadd 		return NULL;
4501eb6f0de0SAdrian Chadd 	}
4502eb6f0de0SAdrian Chadd 
450338962489SAdrian Chadd 	/* Update BAW if required, before we free the original buf */
450438962489SAdrian Chadd 	if (bf->bf_state.bfs_dobaw)
450538962489SAdrian Chadd 		ath_tx_switch_baw_buf(sc, an, tid, bf, nbf);
450638962489SAdrian Chadd 
45073f3a5dbdSAdrian Chadd 	/* Free original buffer; return new buffer */
4508eb6f0de0SAdrian Chadd 	ath_freebuf(sc, bf);
4509f1bc738eSAdrian Chadd 
4510eb6f0de0SAdrian Chadd 	return nbf;
4511eb6f0de0SAdrian Chadd }
4512eb6f0de0SAdrian Chadd 
4513eb6f0de0SAdrian Chadd /*
4514eb6f0de0SAdrian Chadd  * Handle retrying an unaggregate frame in an aggregate
4515eb6f0de0SAdrian Chadd  * session.
4516eb6f0de0SAdrian Chadd  *
4517eb6f0de0SAdrian Chadd  * If too many retries occur, pause the TID, wait for
4518eb6f0de0SAdrian Chadd  * any further retransmits (as there's no reason why
4519eb6f0de0SAdrian Chadd  * non-aggregate frames in an aggregate session are
4520eb6f0de0SAdrian Chadd  * transmitted in-order; they just have to be in-BAW)
4521eb6f0de0SAdrian Chadd  * and then queue a BAR.
4522eb6f0de0SAdrian Chadd  */
4523eb6f0de0SAdrian Chadd static void
4524eb6f0de0SAdrian Chadd ath_tx_aggr_retry_unaggr(struct ath_softc *sc, struct ath_buf *bf)
4525eb6f0de0SAdrian Chadd {
4526eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
4527eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4528eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
4529eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4530eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
4531eb6f0de0SAdrian Chadd 
4532375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
4533eb6f0de0SAdrian Chadd 
4534eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
4535eb6f0de0SAdrian Chadd 
4536eb6f0de0SAdrian Chadd 	/*
4537eb6f0de0SAdrian Chadd 	 * If the buffer is marked as busy, we can't directly
4538eb6f0de0SAdrian Chadd 	 * reuse it. Instead, try to clone the buffer.
4539eb6f0de0SAdrian Chadd 	 * If the clone is successful, recycle the old buffer.
4540eb6f0de0SAdrian Chadd 	 * If the clone is unsuccessful, set bfs_retries to max
4541eb6f0de0SAdrian Chadd 	 * to force the next bit of code to free the buffer
4542eb6f0de0SAdrian Chadd 	 * for us.
4543eb6f0de0SAdrian Chadd 	 */
4544eb6f0de0SAdrian Chadd 	if ((bf->bf_state.bfs_retries < SWMAX_RETRIES) &&
4545eb6f0de0SAdrian Chadd 	    (bf->bf_flags & ATH_BUF_BUSY)) {
4546eb6f0de0SAdrian Chadd 		struct ath_buf *nbf;
454738962489SAdrian Chadd 		nbf = ath_tx_retry_clone(sc, an, atid, bf);
4548eb6f0de0SAdrian Chadd 		if (nbf)
4549eb6f0de0SAdrian Chadd 			/* bf has been freed at this point */
4550eb6f0de0SAdrian Chadd 			bf = nbf;
4551eb6f0de0SAdrian Chadd 		else
4552eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_retries = SWMAX_RETRIES + 1;
4553eb6f0de0SAdrian Chadd 	}
4554eb6f0de0SAdrian Chadd 
4555eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_retries >= SWMAX_RETRIES) {
4556eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_RETRIES,
4557eb6f0de0SAdrian Chadd 		    "%s: exceeded retries; seqno %d\n",
4558eb6f0de0SAdrian Chadd 		    __func__, SEQNO(bf->bf_state.bfs_seqno));
4559eb6f0de0SAdrian Chadd 		sc->sc_stats.ast_tx_swretrymax++;
4560eb6f0de0SAdrian Chadd 
4561eb6f0de0SAdrian Chadd 		/* Update BAW anyway */
4562eb6f0de0SAdrian Chadd 		if (bf->bf_state.bfs_dobaw) {
4563eb6f0de0SAdrian Chadd 			ath_tx_update_baw(sc, an, atid, bf);
4564eb6f0de0SAdrian Chadd 			if (! bf->bf_state.bfs_addedbaw)
456583bbd5ebSRui Paulo 				DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
4566eb6f0de0SAdrian Chadd 				    "%s: wasn't added: seqno %d\n",
4567eb6f0de0SAdrian Chadd 				    __func__, SEQNO(bf->bf_state.bfs_seqno));
4568eb6f0de0SAdrian Chadd 		}
4569eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_dobaw = 0;
4570eb6f0de0SAdrian Chadd 
457188b3d483SAdrian Chadd 		/* Suspend the TX queue and get ready to send the BAR */
457288b3d483SAdrian Chadd 		ath_tx_tid_bar_suspend(sc, atid);
457388b3d483SAdrian Chadd 
457488b3d483SAdrian Chadd 		/* Send the BAR if there are no other frames waiting */
457588b3d483SAdrian Chadd 		if (ath_tx_tid_bar_tx_ready(sc, atid))
457688b3d483SAdrian Chadd 			ath_tx_tid_bar_tx(sc, atid);
457788b3d483SAdrian Chadd 
4578375307d4SAdrian Chadd 		ATH_TX_UNLOCK(sc);
4579eb6f0de0SAdrian Chadd 
4580eb6f0de0SAdrian Chadd 		/* Free buffer, bf is free after this call */
4581eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
4582eb6f0de0SAdrian Chadd 		return;
4583eb6f0de0SAdrian Chadd 	}
4584eb6f0de0SAdrian Chadd 
4585eb6f0de0SAdrian Chadd 	/*
4586eb6f0de0SAdrian Chadd 	 * This increments the retry counter as well as
4587eb6f0de0SAdrian Chadd 	 * sets the retry flag in the ath_buf and packet
4588eb6f0de0SAdrian Chadd 	 * body.
4589eb6f0de0SAdrian Chadd 	 */
4590eb6f0de0SAdrian Chadd 	ath_tx_set_retry(sc, bf);
4591f1bc738eSAdrian Chadd 	sc->sc_stats.ast_tx_swretries++;
4592eb6f0de0SAdrian Chadd 
4593eb6f0de0SAdrian Chadd 	/*
4594eb6f0de0SAdrian Chadd 	 * Insert this at the head of the queue, so it's
4595eb6f0de0SAdrian Chadd 	 * retried before any current/subsequent frames.
4596eb6f0de0SAdrian Chadd 	 */
45973e6cc97fSAdrian Chadd 	ATH_TID_INSERT_HEAD(atid, bf, bf_list);
4598eb6f0de0SAdrian Chadd 	ath_tx_tid_sched(sc, atid);
459988b3d483SAdrian Chadd 	/* Send the BAR if there are no other frames waiting */
460088b3d483SAdrian Chadd 	if (ath_tx_tid_bar_tx_ready(sc, atid))
460188b3d483SAdrian Chadd 		ath_tx_tid_bar_tx(sc, atid);
4602eb6f0de0SAdrian Chadd 
4603375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
4604eb6f0de0SAdrian Chadd }
4605eb6f0de0SAdrian Chadd 
4606eb6f0de0SAdrian Chadd /*
4607eb6f0de0SAdrian Chadd  * Common code for aggregate excessive retry/subframe retry.
4608eb6f0de0SAdrian Chadd  * If retrying, queues buffers to bf_q. If not, frees the
4609eb6f0de0SAdrian Chadd  * buffers.
4610eb6f0de0SAdrian Chadd  *
4611eb6f0de0SAdrian Chadd  * XXX should unify this with ath_tx_aggr_retry_unaggr()
4612eb6f0de0SAdrian Chadd  */
4613eb6f0de0SAdrian Chadd static int
4614eb6f0de0SAdrian Chadd ath_tx_retry_subframe(struct ath_softc *sc, struct ath_buf *bf,
4615eb6f0de0SAdrian Chadd     ath_bufhead *bf_q)
4616eb6f0de0SAdrian Chadd {
4617eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
4618eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4619eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
4620eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4621eb6f0de0SAdrian Chadd 
4622375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
4623eb6f0de0SAdrian Chadd 
462421840808SAdrian Chadd 	/* XXX clr11naggr should be done for all subframes */
4625eb6f0de0SAdrian Chadd 	ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
4626eb6f0de0SAdrian Chadd 	ath_hal_set11nburstduration(sc->sc_ah, bf->bf_desc, 0);
4627f1bc738eSAdrian Chadd 
4628eb6f0de0SAdrian Chadd 	/* ath_hal_set11n_virtualmorefrag(sc->sc_ah, bf->bf_desc, 0); */
4629eb6f0de0SAdrian Chadd 
4630eb6f0de0SAdrian Chadd 	/*
4631eb6f0de0SAdrian Chadd 	 * If the buffer is marked as busy, we can't directly
4632eb6f0de0SAdrian Chadd 	 * reuse it. Instead, try to clone the buffer.
4633eb6f0de0SAdrian Chadd 	 * If the clone is successful, recycle the old buffer.
4634eb6f0de0SAdrian Chadd 	 * If the clone is unsuccessful, set bfs_retries to max
4635eb6f0de0SAdrian Chadd 	 * to force the next bit of code to free the buffer
4636eb6f0de0SAdrian Chadd 	 * for us.
4637eb6f0de0SAdrian Chadd 	 */
4638eb6f0de0SAdrian Chadd 	if ((bf->bf_state.bfs_retries < SWMAX_RETRIES) &&
4639eb6f0de0SAdrian Chadd 	    (bf->bf_flags & ATH_BUF_BUSY)) {
4640eb6f0de0SAdrian Chadd 		struct ath_buf *nbf;
464138962489SAdrian Chadd 		nbf = ath_tx_retry_clone(sc, an, atid, bf);
4642eb6f0de0SAdrian Chadd 		if (nbf)
4643eb6f0de0SAdrian Chadd 			/* bf has been freed at this point */
4644eb6f0de0SAdrian Chadd 			bf = nbf;
4645eb6f0de0SAdrian Chadd 		else
4646eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_retries = SWMAX_RETRIES + 1;
4647eb6f0de0SAdrian Chadd 	}
4648eb6f0de0SAdrian Chadd 
4649eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_retries >= SWMAX_RETRIES) {
4650eb6f0de0SAdrian Chadd 		sc->sc_stats.ast_tx_swretrymax++;
4651eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_RETRIES,
4652eb6f0de0SAdrian Chadd 		    "%s: max retries: seqno %d\n",
4653eb6f0de0SAdrian Chadd 		    __func__, SEQNO(bf->bf_state.bfs_seqno));
4654eb6f0de0SAdrian Chadd 		ath_tx_update_baw(sc, an, atid, bf);
4655eb6f0de0SAdrian Chadd 		if (!bf->bf_state.bfs_addedbaw)
465683bbd5ebSRui Paulo 			DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
4657eb6f0de0SAdrian Chadd 			    "%s: wasn't added: seqno %d\n",
4658eb6f0de0SAdrian Chadd 			    __func__, SEQNO(bf->bf_state.bfs_seqno));
4659eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_dobaw = 0;
4660eb6f0de0SAdrian Chadd 		return 1;
4661eb6f0de0SAdrian Chadd 	}
4662eb6f0de0SAdrian Chadd 
4663eb6f0de0SAdrian Chadd 	ath_tx_set_retry(sc, bf);
4664f1bc738eSAdrian Chadd 	sc->sc_stats.ast_tx_swretries++;
4665eb6f0de0SAdrian Chadd 	bf->bf_next = NULL;		/* Just to make sure */
4666eb6f0de0SAdrian Chadd 
466721840808SAdrian Chadd 	/* Clear the aggregate state */
466821840808SAdrian Chadd 	bf->bf_state.bfs_aggr = 0;
466921840808SAdrian Chadd 	bf->bf_state.bfs_ndelim = 0;	/* ??? needed? */
467021840808SAdrian Chadd 	bf->bf_state.bfs_nframes = 1;
467121840808SAdrian Chadd 
4672eb6f0de0SAdrian Chadd 	TAILQ_INSERT_TAIL(bf_q, bf, bf_list);
4673eb6f0de0SAdrian Chadd 	return 0;
4674eb6f0de0SAdrian Chadd }
4675eb6f0de0SAdrian Chadd 
4676eb6f0de0SAdrian Chadd /*
4677eb6f0de0SAdrian Chadd  * error pkt completion for an aggregate destination
4678eb6f0de0SAdrian Chadd  */
4679eb6f0de0SAdrian Chadd static void
4680eb6f0de0SAdrian Chadd ath_tx_comp_aggr_error(struct ath_softc *sc, struct ath_buf *bf_first,
4681eb6f0de0SAdrian Chadd     struct ath_tid *tid)
4682eb6f0de0SAdrian Chadd {
4683eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf_first->bf_node;
4684eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4685eb6f0de0SAdrian Chadd 	struct ath_buf *bf_next, *bf;
4686eb6f0de0SAdrian Chadd 	ath_bufhead bf_q;
4687eb6f0de0SAdrian Chadd 	int drops = 0;
4688eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
4689eb6f0de0SAdrian Chadd 	ath_bufhead bf_cq;
4690eb6f0de0SAdrian Chadd 
4691eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_q);
4692eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_cq);
4693eb6f0de0SAdrian Chadd 
4694eb6f0de0SAdrian Chadd 	/*
4695eb6f0de0SAdrian Chadd 	 * Update rate control - all frames have failed.
4696eb6f0de0SAdrian Chadd 	 */
4697eb6f0de0SAdrian Chadd 	ath_tx_update_ratectrl(sc, ni, bf_first->bf_state.bfs_rc,
4698eb6f0de0SAdrian Chadd 	    &bf_first->bf_status.ds_txstat,
4699*cce63444SAdrian Chadd 	    bf_first->bf_state.bfs_al,
4700*cce63444SAdrian Chadd 	    bf_first->bf_state.bfs_rc_maxpktlen,
4701eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_nframes, bf_first->bf_state.bfs_nframes);
4702eb6f0de0SAdrian Chadd 
4703375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
4704eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
47052d3d4776SAdrian Chadd 	sc->sc_stats.ast_tx_aggr_failall++;
4706eb6f0de0SAdrian Chadd 
4707eb6f0de0SAdrian Chadd 	/* Retry all subframes */
4708eb6f0de0SAdrian Chadd 	bf = bf_first;
4709eb6f0de0SAdrian Chadd 	while (bf) {
4710eb6f0de0SAdrian Chadd 		bf_next = bf->bf_next;
4711eb6f0de0SAdrian Chadd 		bf->bf_next = NULL;	/* Remove it from the aggr list */
47122d3d4776SAdrian Chadd 		sc->sc_stats.ast_tx_aggr_fail++;
4713eb6f0de0SAdrian Chadd 		if (ath_tx_retry_subframe(sc, bf, &bf_q)) {
4714eb6f0de0SAdrian Chadd 			drops++;
4715eb6f0de0SAdrian Chadd 			bf->bf_next = NULL;
4716eb6f0de0SAdrian Chadd 			TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
4717eb6f0de0SAdrian Chadd 		}
4718eb6f0de0SAdrian Chadd 		bf = bf_next;
4719eb6f0de0SAdrian Chadd 	}
4720eb6f0de0SAdrian Chadd 
4721eb6f0de0SAdrian Chadd 	/* Prepend all frames to the beginning of the queue */
4722eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_LAST(&bf_q, ath_bufhead_s)) != NULL) {
4723eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_q, bf, bf_list);
47243e6cc97fSAdrian Chadd 		ATH_TID_INSERT_HEAD(tid, bf, bf_list);
4725eb6f0de0SAdrian Chadd 	}
4726eb6f0de0SAdrian Chadd 
472739da9d42SAdrian Chadd 	/*
472839da9d42SAdrian Chadd 	 * Schedule the TID to be re-tried.
472939da9d42SAdrian Chadd 	 */
4730eb6f0de0SAdrian Chadd 	ath_tx_tid_sched(sc, tid);
4731eb6f0de0SAdrian Chadd 
4732eb6f0de0SAdrian Chadd 	/*
4733eb6f0de0SAdrian Chadd 	 * send bar if we dropped any frames
4734eb6f0de0SAdrian Chadd 	 *
4735eb6f0de0SAdrian Chadd 	 * Keep the txq lock held for now, as we need to ensure
4736eb6f0de0SAdrian Chadd 	 * that ni_txseqs[] is consistent (as it's being updated
4737eb6f0de0SAdrian Chadd 	 * in the ifnet TX context or raw TX context.)
4738eb6f0de0SAdrian Chadd 	 */
4739eb6f0de0SAdrian Chadd 	if (drops) {
474088b3d483SAdrian Chadd 		/* Suspend the TX queue and get ready to send the BAR */
474188b3d483SAdrian Chadd 		ath_tx_tid_bar_suspend(sc, tid);
4742eb6f0de0SAdrian Chadd 	}
4743eb6f0de0SAdrian Chadd 
474488b3d483SAdrian Chadd 	/*
474588b3d483SAdrian Chadd 	 * Send BAR if required
474688b3d483SAdrian Chadd 	 */
474788b3d483SAdrian Chadd 	if (ath_tx_tid_bar_tx_ready(sc, tid))
474888b3d483SAdrian Chadd 		ath_tx_tid_bar_tx(sc, tid);
4749f1bc738eSAdrian Chadd 
4750375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
475188b3d483SAdrian Chadd 
4752eb6f0de0SAdrian Chadd 	/* Complete frames which errored out */
4753eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
4754eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
4755eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
4756eb6f0de0SAdrian Chadd 	}
4757eb6f0de0SAdrian Chadd }
4758eb6f0de0SAdrian Chadd 
4759eb6f0de0SAdrian Chadd /*
4760eb6f0de0SAdrian Chadd  * Handle clean-up of packets from an aggregate list.
4761eb6f0de0SAdrian Chadd  *
4762eb6f0de0SAdrian Chadd  * There's no need to update the BAW here - the session is being
4763eb6f0de0SAdrian Chadd  * torn down.
4764eb6f0de0SAdrian Chadd  */
4765eb6f0de0SAdrian Chadd static void
4766eb6f0de0SAdrian Chadd ath_tx_comp_cleanup_aggr(struct ath_softc *sc, struct ath_buf *bf_first)
4767eb6f0de0SAdrian Chadd {
4768eb6f0de0SAdrian Chadd 	struct ath_buf *bf, *bf_next;
4769eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf_first->bf_node;
4770eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4771eb6f0de0SAdrian Chadd 	int tid = bf_first->bf_state.bfs_tid;
4772eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4773eb6f0de0SAdrian Chadd 
4774375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
4775eb6f0de0SAdrian Chadd 
4776eb6f0de0SAdrian Chadd 	/* update incomp */
4777f172ef75SAdrian Chadd 	atid->incomp--;
4778f172ef75SAdrian Chadd 
4779f172ef75SAdrian Chadd 	/* Update the BAW */
4780302868d9SAdrian Chadd 	bf = bf_first;
4781eb6f0de0SAdrian Chadd 	while (bf) {
4782f172ef75SAdrian Chadd 		/* XXX refactor! */
4783f172ef75SAdrian Chadd 		if (bf->bf_state.bfs_dobaw) {
4784f172ef75SAdrian Chadd 			ath_tx_update_baw(sc, an, atid, bf);
4785f172ef75SAdrian Chadd 			if (!bf->bf_state.bfs_addedbaw)
4786f172ef75SAdrian Chadd 				DPRINTF(sc, ATH_DEBUG_SW_TX,
4787f172ef75SAdrian Chadd 				    "%s: wasn't added: seqno %d\n",
4788f172ef75SAdrian Chadd 				    __func__, SEQNO(bf->bf_state.bfs_seqno));
4789f172ef75SAdrian Chadd 		}
4790eb6f0de0SAdrian Chadd 		bf = bf->bf_next;
4791eb6f0de0SAdrian Chadd 	}
4792eb6f0de0SAdrian Chadd 
4793eb6f0de0SAdrian Chadd 	if (atid->incomp == 0) {
4794eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4795eb6f0de0SAdrian Chadd 		    "%s: TID %d: cleaned up! resume!\n",
4796eb6f0de0SAdrian Chadd 		    __func__, tid);
4797eb6f0de0SAdrian Chadd 		atid->cleanup_inprogress = 0;
4798eb6f0de0SAdrian Chadd 		ath_tx_tid_resume(sc, atid);
4799eb6f0de0SAdrian Chadd 	}
480088b3d483SAdrian Chadd 
480188b3d483SAdrian Chadd 	/* Send BAR if required */
4802f1bc738eSAdrian Chadd 	/* XXX why would we send a BAR when transitioning to non-aggregation? */
4803302868d9SAdrian Chadd 	/*
4804302868d9SAdrian Chadd 	 * XXX TODO: we should likely just tear down the BAR state here,
4805302868d9SAdrian Chadd 	 * rather than sending a BAR.
4806302868d9SAdrian Chadd 	 */
480788b3d483SAdrian Chadd 	if (ath_tx_tid_bar_tx_ready(sc, atid))
480888b3d483SAdrian Chadd 		ath_tx_tid_bar_tx(sc, atid);
4809f1bc738eSAdrian Chadd 
4810375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
4811eb6f0de0SAdrian Chadd 
4812706bb444SAdrian Chadd 	/* Handle frame completion as individual frames */
4813302868d9SAdrian Chadd 	bf = bf_first;
4814eb6f0de0SAdrian Chadd 	while (bf) {
4815eb6f0de0SAdrian Chadd 		bf_next = bf->bf_next;
4816706bb444SAdrian Chadd 		bf->bf_next = NULL;
4817eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 1);
4818eb6f0de0SAdrian Chadd 		bf = bf_next;
4819eb6f0de0SAdrian Chadd 	}
4820eb6f0de0SAdrian Chadd }
4821eb6f0de0SAdrian Chadd 
4822eb6f0de0SAdrian Chadd /*
4823eb6f0de0SAdrian Chadd  * Handle completion of an set of aggregate frames.
4824eb6f0de0SAdrian Chadd  *
4825eb6f0de0SAdrian Chadd  * Note: the completion handler is the last descriptor in the aggregate,
4826eb6f0de0SAdrian Chadd  * not the last descriptor in the first frame.
4827eb6f0de0SAdrian Chadd  */
4828eb6f0de0SAdrian Chadd static void
4829d4365d16SAdrian Chadd ath_tx_aggr_comp_aggr(struct ath_softc *sc, struct ath_buf *bf_first,
4830d4365d16SAdrian Chadd     int fail)
4831eb6f0de0SAdrian Chadd {
4832eb6f0de0SAdrian Chadd 	//struct ath_desc *ds = bf->bf_lastds;
4833eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf_first->bf_node;
4834eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4835eb6f0de0SAdrian Chadd 	int tid = bf_first->bf_state.bfs_tid;
4836eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4837eb6f0de0SAdrian Chadd 	struct ath_tx_status ts;
4838eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
4839eb6f0de0SAdrian Chadd 	ath_bufhead bf_q;
4840eb6f0de0SAdrian Chadd 	ath_bufhead bf_cq;
4841eb6f0de0SAdrian Chadd 	int seq_st, tx_ok;
4842eb6f0de0SAdrian Chadd 	int hasba, isaggr;
4843eb6f0de0SAdrian Chadd 	uint32_t ba[2];
4844eb6f0de0SAdrian Chadd 	struct ath_buf *bf, *bf_next;
4845eb6f0de0SAdrian Chadd 	int ba_index;
4846eb6f0de0SAdrian Chadd 	int drops = 0;
4847eb6f0de0SAdrian Chadd 	int nframes = 0, nbad = 0, nf;
4848eb6f0de0SAdrian Chadd 	int pktlen;
4849*cce63444SAdrian Chadd 	int agglen, rc_agglen;
4850eb6f0de0SAdrian Chadd 	/* XXX there's too much on the stack? */
4851b815538dSAdrian Chadd 	struct ath_rc_series rc[ATH_RC_NUM];
4852eb6f0de0SAdrian Chadd 	int txseq;
4853eb6f0de0SAdrian Chadd 
4854eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: called; hwq_depth=%d\n",
4855eb6f0de0SAdrian Chadd 	    __func__, atid->hwq_depth);
4856eb6f0de0SAdrian Chadd 
48570aa5c1bbSAdrian Chadd 	/*
48580aa5c1bbSAdrian Chadd 	 * Take a copy; this may be needed -after- bf_first
48590aa5c1bbSAdrian Chadd 	 * has been completed and freed.
48600aa5c1bbSAdrian Chadd 	 */
48610aa5c1bbSAdrian Chadd 	ts = bf_first->bf_status.ds_txstat;
4862*cce63444SAdrian Chadd 	agglen = bf_first->bf_state.bfs_al;
4863*cce63444SAdrian Chadd 	rc_agglen = bf_first->bf_state.bfs_rc_maxpktlen;
48640aa5c1bbSAdrian Chadd 
4865f1bc738eSAdrian Chadd 	TAILQ_INIT(&bf_q);
4866f1bc738eSAdrian Chadd 	TAILQ_INIT(&bf_cq);
4867f1bc738eSAdrian Chadd 
4868eb6f0de0SAdrian Chadd 	/* The TID state is kept behind the TXQ lock */
4869375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
4870eb6f0de0SAdrian Chadd 
4871eb6f0de0SAdrian Chadd 	atid->hwq_depth--;
4872eb6f0de0SAdrian Chadd 	if (atid->hwq_depth < 0)
487383bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: hwq_depth < 0: %d\n",
4874eb6f0de0SAdrian Chadd 		    __func__, atid->hwq_depth);
4875eb6f0de0SAdrian Chadd 
4876eb6f0de0SAdrian Chadd 	/*
4877f1bc738eSAdrian Chadd 	 * If the TID is filtered, handle completing the filter
4878f1bc738eSAdrian Chadd 	 * transition before potentially kicking it to the cleanup
4879f1bc738eSAdrian Chadd 	 * function.
48800aa5c1bbSAdrian Chadd 	 *
48810aa5c1bbSAdrian Chadd 	 * XXX this is duplicate work, ew.
4882f1bc738eSAdrian Chadd 	 */
4883f1bc738eSAdrian Chadd 	if (atid->isfiltered)
4884f1bc738eSAdrian Chadd 		ath_tx_tid_filt_comp_complete(sc, atid);
4885f1bc738eSAdrian Chadd 
4886f1bc738eSAdrian Chadd 	/*
4887eb6f0de0SAdrian Chadd 	 * Punt cleanup to the relevant function, not our problem now
4888eb6f0de0SAdrian Chadd 	 */
4889eb6f0de0SAdrian Chadd 	if (atid->cleanup_inprogress) {
4890f1bc738eSAdrian Chadd 		if (atid->isfiltered)
489183bbd5ebSRui Paulo 			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4892f1bc738eSAdrian Chadd 			    "%s: isfiltered=1, normal_comp?\n",
4893f1bc738eSAdrian Chadd 			    __func__);
4894375307d4SAdrian Chadd 		ATH_TX_UNLOCK(sc);
4895eb6f0de0SAdrian Chadd 		ath_tx_comp_cleanup_aggr(sc, bf_first);
4896eb6f0de0SAdrian Chadd 		return;
4897eb6f0de0SAdrian Chadd 	}
4898eb6f0de0SAdrian Chadd 
4899eb6f0de0SAdrian Chadd 	/*
4900f1bc738eSAdrian Chadd 	 * If the frame is filtered, transition to filtered frame
4901f1bc738eSAdrian Chadd 	 * mode and add this to the filtered frame list.
4902f1bc738eSAdrian Chadd 	 *
4903f1bc738eSAdrian Chadd 	 * XXX TODO: figure out how this interoperates with
4904f1bc738eSAdrian Chadd 	 * BAR, pause and cleanup states.
4905f1bc738eSAdrian Chadd 	 */
4906f1bc738eSAdrian Chadd 	if ((ts.ts_status & HAL_TXERR_FILT) ||
4907f1bc738eSAdrian Chadd 	    (ts.ts_status != 0 && atid->isfiltered)) {
4908f1bc738eSAdrian Chadd 		if (fail != 0)
490983bbd5ebSRui Paulo 			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4910f1bc738eSAdrian Chadd 			    "%s: isfiltered=1, fail=%d\n", __func__, fail);
4911f1bc738eSAdrian Chadd 		ath_tx_tid_filt_comp_aggr(sc, atid, bf_first, &bf_cq);
4912f1bc738eSAdrian Chadd 
4913f1bc738eSAdrian Chadd 		/* Remove from BAW */
4914f1bc738eSAdrian Chadd 		TAILQ_FOREACH_SAFE(bf, &bf_cq, bf_list, bf_next) {
4915f1bc738eSAdrian Chadd 			if (bf->bf_state.bfs_addedbaw)
4916f1bc738eSAdrian Chadd 				drops++;
4917f1bc738eSAdrian Chadd 			if (bf->bf_state.bfs_dobaw) {
4918f1bc738eSAdrian Chadd 				ath_tx_update_baw(sc, an, atid, bf);
4919f1bc738eSAdrian Chadd 				if (!bf->bf_state.bfs_addedbaw)
492083bbd5ebSRui Paulo 					DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4921f1bc738eSAdrian Chadd 					    "%s: wasn't added: seqno %d\n",
4922f1bc738eSAdrian Chadd 					    __func__,
4923f1bc738eSAdrian Chadd 					    SEQNO(bf->bf_state.bfs_seqno));
4924f1bc738eSAdrian Chadd 			}
4925f1bc738eSAdrian Chadd 			bf->bf_state.bfs_dobaw = 0;
4926f1bc738eSAdrian Chadd 		}
4927f1bc738eSAdrian Chadd 		/*
4928f1bc738eSAdrian Chadd 		 * If any intermediate frames in the BAW were dropped when
4929f1bc738eSAdrian Chadd 		 * handling filtering things, send a BAR.
4930f1bc738eSAdrian Chadd 		 */
4931f1bc738eSAdrian Chadd 		if (drops)
4932f1bc738eSAdrian Chadd 			ath_tx_tid_bar_suspend(sc, atid);
4933f1bc738eSAdrian Chadd 
4934f1bc738eSAdrian Chadd 		/*
4935f1bc738eSAdrian Chadd 		 * Finish up by sending a BAR if required and freeing
4936f1bc738eSAdrian Chadd 		 * the frames outside of the TX lock.
4937f1bc738eSAdrian Chadd 		 */
4938f1bc738eSAdrian Chadd 		goto finish_send_bar;
4939f1bc738eSAdrian Chadd 	}
4940f1bc738eSAdrian Chadd 
4941f1bc738eSAdrian Chadd 	/*
4942eb6f0de0SAdrian Chadd 	 * XXX for now, use the first frame in the aggregate for
4943eb6f0de0SAdrian Chadd 	 * XXX rate control completion; it's at least consistent.
4944eb6f0de0SAdrian Chadd 	 */
4945eb6f0de0SAdrian Chadd 	pktlen = bf_first->bf_state.bfs_pktlen;
4946eb6f0de0SAdrian Chadd 
4947eb6f0de0SAdrian Chadd 	/*
4948e9a6408eSAdrian Chadd 	 * Handle errors first!
4949e9a6408eSAdrian Chadd 	 *
4950e9a6408eSAdrian Chadd 	 * Here, handle _any_ error as a "exceeded retries" error.
4951e9a6408eSAdrian Chadd 	 * Later on (when filtered frames are to be specially handled)
4952e9a6408eSAdrian Chadd 	 * it'll have to be expanded.
4953eb6f0de0SAdrian Chadd 	 */
4954e9a6408eSAdrian Chadd #if 0
4955eb6f0de0SAdrian Chadd 	if (ts.ts_status & HAL_TXERR_XRETRY) {
4956e9a6408eSAdrian Chadd #endif
4957e9a6408eSAdrian Chadd 	if (ts.ts_status != 0) {
4958375307d4SAdrian Chadd 		ATH_TX_UNLOCK(sc);
4959eb6f0de0SAdrian Chadd 		ath_tx_comp_aggr_error(sc, bf_first, atid);
4960eb6f0de0SAdrian Chadd 		return;
4961eb6f0de0SAdrian Chadd 	}
4962eb6f0de0SAdrian Chadd 
4963eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
4964eb6f0de0SAdrian Chadd 
4965eb6f0de0SAdrian Chadd 	/*
4966eb6f0de0SAdrian Chadd 	 * extract starting sequence and block-ack bitmap
4967eb6f0de0SAdrian Chadd 	 */
4968eb6f0de0SAdrian Chadd 	/* XXX endian-ness of seq_st, ba? */
4969eb6f0de0SAdrian Chadd 	seq_st = ts.ts_seqnum;
4970eb6f0de0SAdrian Chadd 	hasba = !! (ts.ts_flags & HAL_TX_BA);
4971eb6f0de0SAdrian Chadd 	tx_ok = (ts.ts_status == 0);
4972eb6f0de0SAdrian Chadd 	isaggr = bf_first->bf_state.bfs_aggr;
4973eb6f0de0SAdrian Chadd 	ba[0] = ts.ts_ba_low;
4974eb6f0de0SAdrian Chadd 	ba[1] = ts.ts_ba_high;
4975eb6f0de0SAdrian Chadd 
4976eb6f0de0SAdrian Chadd 	/*
4977eb6f0de0SAdrian Chadd 	 * Copy the TX completion status and the rate control
4978eb6f0de0SAdrian Chadd 	 * series from the first descriptor, as it may be freed
4979eb6f0de0SAdrian Chadd 	 * before the rate control code can get its grubby fingers
4980eb6f0de0SAdrian Chadd 	 * into things.
4981eb6f0de0SAdrian Chadd 	 */
4982eb6f0de0SAdrian Chadd 	memcpy(rc, bf_first->bf_state.bfs_rc, sizeof(rc));
4983eb6f0de0SAdrian Chadd 
4984eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4985d4365d16SAdrian Chadd 	    "%s: txa_start=%d, tx_ok=%d, status=%.8x, flags=%.8x, "
4986d4365d16SAdrian Chadd 	    "isaggr=%d, seq_st=%d, hasba=%d, ba=%.8x, %.8x\n",
4987eb6f0de0SAdrian Chadd 	    __func__, tap->txa_start, tx_ok, ts.ts_status, ts.ts_flags,
4988eb6f0de0SAdrian Chadd 	    isaggr, seq_st, hasba, ba[0], ba[1]);
4989eb6f0de0SAdrian Chadd 
4990b3420862SAdrian Chadd 	/*
4991b3420862SAdrian Chadd 	 * The reference driver doesn't do this; it simply ignores
4992b3420862SAdrian Chadd 	 * this check in its entirety.
4993b3420862SAdrian Chadd 	 *
4994b3420862SAdrian Chadd 	 * I've seen this occur when using iperf to send traffic
4995b3420862SAdrian Chadd 	 * out tid 1 - the aggregate frames are all marked as TID 1,
4996b3420862SAdrian Chadd 	 * but the TXSTATUS has TID=0.  So, let's just ignore this
4997b3420862SAdrian Chadd 	 * check.
4998b3420862SAdrian Chadd 	 */
4999b3420862SAdrian Chadd #if 0
5000eb6f0de0SAdrian Chadd 	/* Occasionally, the MAC sends a tx status for the wrong TID. */
5001eb6f0de0SAdrian Chadd 	if (tid != ts.ts_tid) {
500283bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: tid %d != hw tid %d\n",
5003eb6f0de0SAdrian Chadd 		    __func__, tid, ts.ts_tid);
5004eb6f0de0SAdrian Chadd 		tx_ok = 0;
5005eb6f0de0SAdrian Chadd 	}
5006b3420862SAdrian Chadd #endif
5007eb6f0de0SAdrian Chadd 
5008eb6f0de0SAdrian Chadd 	/* AR5416 BA bug; this requires an interface reset */
5009eb6f0de0SAdrian Chadd 	if (isaggr && tx_ok && (! hasba)) {
501082525db1SAdrian Chadd 		device_printf(sc->sc_dev,
5011d4365d16SAdrian Chadd 		    "%s: AR5416 bug: hasba=%d; txok=%d, isaggr=%d, "
5012d4365d16SAdrian Chadd 		    "seq_st=%d\n",
5013eb6f0de0SAdrian Chadd 		    __func__, hasba, tx_ok, isaggr, seq_st);
5014eb6f0de0SAdrian Chadd 		/* XXX TODO: schedule an interface reset */
50150f078d63SJohn Baldwin #ifdef ATH_DEBUG
50166abbbae5SAdrian Chadd 		ath_printtxbuf(sc, bf_first,
50176abbbae5SAdrian Chadd 		    sc->sc_ac2q[atid->ac]->axq_qnum, 0, 0);
50180f078d63SJohn Baldwin #endif
5019eb6f0de0SAdrian Chadd 	}
5020eb6f0de0SAdrian Chadd 
5021eb6f0de0SAdrian Chadd 	/*
5022eb6f0de0SAdrian Chadd 	 * Walk the list of frames, figure out which ones were correctly
5023eb6f0de0SAdrian Chadd 	 * sent and which weren't.
5024eb6f0de0SAdrian Chadd 	 */
5025eb6f0de0SAdrian Chadd 	bf = bf_first;
5026eb6f0de0SAdrian Chadd 	nf = bf_first->bf_state.bfs_nframes;
5027eb6f0de0SAdrian Chadd 
5028eb6f0de0SAdrian Chadd 	/* bf_first is going to be invalid once this list is walked */
5029eb6f0de0SAdrian Chadd 	bf_first = NULL;
5030eb6f0de0SAdrian Chadd 
5031eb6f0de0SAdrian Chadd 	/*
5032eb6f0de0SAdrian Chadd 	 * Walk the list of completed frames and determine
5033eb6f0de0SAdrian Chadd 	 * which need to be completed and which need to be
5034eb6f0de0SAdrian Chadd 	 * retransmitted.
5035eb6f0de0SAdrian Chadd 	 *
5036eb6f0de0SAdrian Chadd 	 * For completed frames, the completion functions need
5037eb6f0de0SAdrian Chadd 	 * to be called at the end of this function as the last
5038eb6f0de0SAdrian Chadd 	 * node reference may free the node.
5039eb6f0de0SAdrian Chadd 	 *
5040eb6f0de0SAdrian Chadd 	 * Finally, since the TXQ lock can't be held during the
5041eb6f0de0SAdrian Chadd 	 * completion callback (to avoid lock recursion),
5042eb6f0de0SAdrian Chadd 	 * the completion calls have to be done outside of the
5043eb6f0de0SAdrian Chadd 	 * lock.
5044eb6f0de0SAdrian Chadd 	 */
5045eb6f0de0SAdrian Chadd 	while (bf) {
5046eb6f0de0SAdrian Chadd 		nframes++;
5047d4365d16SAdrian Chadd 		ba_index = ATH_BA_INDEX(seq_st,
5048d4365d16SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
5049eb6f0de0SAdrian Chadd 		bf_next = bf->bf_next;
5050eb6f0de0SAdrian Chadd 		bf->bf_next = NULL;	/* Remove it from the aggr list */
5051eb6f0de0SAdrian Chadd 
5052eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
5053eb6f0de0SAdrian Chadd 		    "%s: checking bf=%p seqno=%d; ack=%d\n",
5054eb6f0de0SAdrian Chadd 		    __func__, bf, SEQNO(bf->bf_state.bfs_seqno),
5055eb6f0de0SAdrian Chadd 		    ATH_BA_ISSET(ba, ba_index));
5056eb6f0de0SAdrian Chadd 
5057eb6f0de0SAdrian Chadd 		if (tx_ok && ATH_BA_ISSET(ba, ba_index)) {
50582d3d4776SAdrian Chadd 			sc->sc_stats.ast_tx_aggr_ok++;
5059eb6f0de0SAdrian Chadd 			ath_tx_update_baw(sc, an, atid, bf);
5060eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_dobaw = 0;
5061eb6f0de0SAdrian Chadd 			if (!bf->bf_state.bfs_addedbaw)
506283bbd5ebSRui Paulo 				DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
5063eb6f0de0SAdrian Chadd 				    "%s: wasn't added: seqno %d\n",
5064eb6f0de0SAdrian Chadd 				    __func__, SEQNO(bf->bf_state.bfs_seqno));
5065eb6f0de0SAdrian Chadd 			bf->bf_next = NULL;
5066eb6f0de0SAdrian Chadd 			TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
5067eb6f0de0SAdrian Chadd 		} else {
50682d3d4776SAdrian Chadd 			sc->sc_stats.ast_tx_aggr_fail++;
5069eb6f0de0SAdrian Chadd 			if (ath_tx_retry_subframe(sc, bf, &bf_q)) {
5070eb6f0de0SAdrian Chadd 				drops++;
5071eb6f0de0SAdrian Chadd 				bf->bf_next = NULL;
5072eb6f0de0SAdrian Chadd 				TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
5073eb6f0de0SAdrian Chadd 			}
5074eb6f0de0SAdrian Chadd 			nbad++;
5075eb6f0de0SAdrian Chadd 		}
5076eb6f0de0SAdrian Chadd 		bf = bf_next;
5077eb6f0de0SAdrian Chadd 	}
5078eb6f0de0SAdrian Chadd 
5079eb6f0de0SAdrian Chadd 	/*
5080eb6f0de0SAdrian Chadd 	 * Now that the BAW updates have been done, unlock
5081eb6f0de0SAdrian Chadd 	 *
5082eb6f0de0SAdrian Chadd 	 * txseq is grabbed before the lock is released so we
5083eb6f0de0SAdrian Chadd 	 * have a consistent view of what -was- in the BAW.
5084eb6f0de0SAdrian Chadd 	 * Anything after this point will not yet have been
5085eb6f0de0SAdrian Chadd 	 * TXed.
5086eb6f0de0SAdrian Chadd 	 */
5087eb6f0de0SAdrian Chadd 	txseq = tap->txa_start;
5088375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
5089eb6f0de0SAdrian Chadd 
5090eb6f0de0SAdrian Chadd 	if (nframes != nf)
509183bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
5092eb6f0de0SAdrian Chadd 		    "%s: num frames seen=%d; bf nframes=%d\n",
5093eb6f0de0SAdrian Chadd 		    __func__, nframes, nf);
5094eb6f0de0SAdrian Chadd 
5095eb6f0de0SAdrian Chadd 	/*
5096eb6f0de0SAdrian Chadd 	 * Now we know how many frames were bad, call the rate
5097eb6f0de0SAdrian Chadd 	 * control code.
5098eb6f0de0SAdrian Chadd 	 */
509984f950a5SAdrian Chadd 	if (fail == 0) {
5100*cce63444SAdrian Chadd 		ath_tx_update_ratectrl(sc, ni, rc, &ts, agglen, rc_agglen,
5101*cce63444SAdrian Chadd 		    nframes, nbad);
510284f950a5SAdrian Chadd 	}
5103eb6f0de0SAdrian Chadd 
5104eb6f0de0SAdrian Chadd 	/*
5105eb6f0de0SAdrian Chadd 	 * send bar if we dropped any frames
5106eb6f0de0SAdrian Chadd 	 */
5107eb6f0de0SAdrian Chadd 	if (drops) {
510888b3d483SAdrian Chadd 		/* Suspend the TX queue and get ready to send the BAR */
5109375307d4SAdrian Chadd 		ATH_TX_LOCK(sc);
511088b3d483SAdrian Chadd 		ath_tx_tid_bar_suspend(sc, atid);
5111375307d4SAdrian Chadd 		ATH_TX_UNLOCK(sc);
5112eb6f0de0SAdrian Chadd 	}
5113eb6f0de0SAdrian Chadd 
511439da9d42SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
511539da9d42SAdrian Chadd 	    "%s: txa_start now %d\n", __func__, tap->txa_start);
511639da9d42SAdrian Chadd 
5117375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
511839da9d42SAdrian Chadd 
511939da9d42SAdrian Chadd 	/* Prepend all frames to the beginning of the queue */
5120eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_LAST(&bf_q, ath_bufhead_s)) != NULL) {
5121eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_q, bf, bf_list);
51223e6cc97fSAdrian Chadd 		ATH_TID_INSERT_HEAD(atid, bf, bf_list);
5123eb6f0de0SAdrian Chadd 	}
5124eb6f0de0SAdrian Chadd 
512539da9d42SAdrian Chadd 	/*
512639da9d42SAdrian Chadd 	 * Reschedule to grab some further frames.
512739da9d42SAdrian Chadd 	 */
512839da9d42SAdrian Chadd 	ath_tx_tid_sched(sc, atid);
5129eb6f0de0SAdrian Chadd 
513088b3d483SAdrian Chadd 	/*
5131f1bc738eSAdrian Chadd 	 * If the queue is filtered, re-schedule as required.
5132f1bc738eSAdrian Chadd 	 *
5133f1bc738eSAdrian Chadd 	 * This is required as there may be a subsequent TX descriptor
5134f1bc738eSAdrian Chadd 	 * for this end-node that has CLRDMASK set, so it's quite possible
5135f1bc738eSAdrian Chadd 	 * that a filtered frame will be followed by a non-filtered
5136f1bc738eSAdrian Chadd 	 * (complete or otherwise) frame.
5137f1bc738eSAdrian Chadd 	 *
5138f1bc738eSAdrian Chadd 	 * XXX should we do this before we complete the frame?
5139f1bc738eSAdrian Chadd 	 */
5140f1bc738eSAdrian Chadd 	if (atid->isfiltered)
5141f1bc738eSAdrian Chadd 		ath_tx_tid_filt_comp_complete(sc, atid);
5142f1bc738eSAdrian Chadd 
5143f1bc738eSAdrian Chadd finish_send_bar:
5144f1bc738eSAdrian Chadd 
5145f1bc738eSAdrian Chadd 	/*
514688b3d483SAdrian Chadd 	 * Send BAR if required
514788b3d483SAdrian Chadd 	 */
514888b3d483SAdrian Chadd 	if (ath_tx_tid_bar_tx_ready(sc, atid))
514988b3d483SAdrian Chadd 		ath_tx_tid_bar_tx(sc, atid);
515039da9d42SAdrian Chadd 
5151375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
515288b3d483SAdrian Chadd 
5153eb6f0de0SAdrian Chadd 	/* Do deferred completion */
5154eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
5155eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
5156eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
5157eb6f0de0SAdrian Chadd 	}
5158eb6f0de0SAdrian Chadd }
5159eb6f0de0SAdrian Chadd 
5160eb6f0de0SAdrian Chadd /*
5161eb6f0de0SAdrian Chadd  * Handle completion of unaggregated frames in an ADDBA
5162eb6f0de0SAdrian Chadd  * session.
5163eb6f0de0SAdrian Chadd  *
5164eb6f0de0SAdrian Chadd  * Fail is set to 1 if the entry is being freed via a call to
5165eb6f0de0SAdrian Chadd  * ath_tx_draintxq().
5166eb6f0de0SAdrian Chadd  */
5167eb6f0de0SAdrian Chadd static void
5168eb6f0de0SAdrian Chadd ath_tx_aggr_comp_unaggr(struct ath_softc *sc, struct ath_buf *bf, int fail)
5169eb6f0de0SAdrian Chadd {
5170eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
5171eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
5172eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
5173eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
51740aa5c1bbSAdrian Chadd 	struct ath_tx_status ts;
5175f1bc738eSAdrian Chadd 	int drops = 0;
5176eb6f0de0SAdrian Chadd 
5177eb6f0de0SAdrian Chadd 	/*
51780aa5c1bbSAdrian Chadd 	 * Take a copy of this; filtering/cloning the frame may free the
51790aa5c1bbSAdrian Chadd 	 * bf pointer.
51800aa5c1bbSAdrian Chadd 	 */
51810aa5c1bbSAdrian Chadd 	ts = bf->bf_status.ds_txstat;
51820aa5c1bbSAdrian Chadd 
51830aa5c1bbSAdrian Chadd 	/*
5184eb6f0de0SAdrian Chadd 	 * Update rate control status here, before we possibly
5185eb6f0de0SAdrian Chadd 	 * punt to retry or cleanup.
5186eb6f0de0SAdrian Chadd 	 *
5187eb6f0de0SAdrian Chadd 	 * Do it outside of the TXQ lock.
5188eb6f0de0SAdrian Chadd 	 */
5189875a9451SAdrian Chadd 	if (fail == 0 && ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0))
5190eb6f0de0SAdrian Chadd 		ath_tx_update_ratectrl(sc, ni, bf->bf_state.bfs_rc,
5191eb6f0de0SAdrian Chadd 		    &bf->bf_status.ds_txstat,
5192eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_pktlen,
5193*cce63444SAdrian Chadd 		    bf->bf_state.bfs_pktlen,
51940aa5c1bbSAdrian Chadd 		    1, (ts.ts_status == 0) ? 0 : 1);
5195eb6f0de0SAdrian Chadd 
5196eb6f0de0SAdrian Chadd 	/*
5197eb6f0de0SAdrian Chadd 	 * This is called early so atid->hwq_depth can be tracked.
5198eb6f0de0SAdrian Chadd 	 * This unfortunately means that it's released and regrabbed
5199eb6f0de0SAdrian Chadd 	 * during retry and cleanup. That's rather inefficient.
5200eb6f0de0SAdrian Chadd 	 */
5201375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
5202eb6f0de0SAdrian Chadd 
5203eb6f0de0SAdrian Chadd 	if (tid == IEEE80211_NONQOS_TID)
520483bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: TID=16!\n", __func__);
5205eb6f0de0SAdrian Chadd 
5206d4365d16SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX,
5207d4365d16SAdrian Chadd 	    "%s: bf=%p: tid=%d, hwq_depth=%d, seqno=%d\n",
5208d4365d16SAdrian Chadd 	    __func__, bf, bf->bf_state.bfs_tid, atid->hwq_depth,
5209d4365d16SAdrian Chadd 	    SEQNO(bf->bf_state.bfs_seqno));
5210eb6f0de0SAdrian Chadd 
5211eb6f0de0SAdrian Chadd 	atid->hwq_depth--;
5212eb6f0de0SAdrian Chadd 	if (atid->hwq_depth < 0)
521383bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: hwq_depth < 0: %d\n",
5214eb6f0de0SAdrian Chadd 		    __func__, atid->hwq_depth);
5215eb6f0de0SAdrian Chadd 
5216eb6f0de0SAdrian Chadd 	/*
5217f1bc738eSAdrian Chadd 	 * If the TID is filtered, handle completing the filter
5218f1bc738eSAdrian Chadd 	 * transition before potentially kicking it to the cleanup
5219f1bc738eSAdrian Chadd 	 * function.
5220f1bc738eSAdrian Chadd 	 */
5221f1bc738eSAdrian Chadd 	if (atid->isfiltered)
5222f1bc738eSAdrian Chadd 		ath_tx_tid_filt_comp_complete(sc, atid);
5223f1bc738eSAdrian Chadd 
5224f1bc738eSAdrian Chadd 	/*
5225eb6f0de0SAdrian Chadd 	 * If a cleanup is in progress, punt to comp_cleanup;
5226eb6f0de0SAdrian Chadd 	 * rather than handling it here. It's thus their
5227eb6f0de0SAdrian Chadd 	 * responsibility to clean up, call the completion
5228eb6f0de0SAdrian Chadd 	 * function in net80211, etc.
5229eb6f0de0SAdrian Chadd 	 */
5230eb6f0de0SAdrian Chadd 	if (atid->cleanup_inprogress) {
5231f1bc738eSAdrian Chadd 		if (atid->isfiltered)
523283bbd5ebSRui Paulo 			DPRINTF(sc, ATH_DEBUG_SW_TX,
5233f1bc738eSAdrian Chadd 			    "%s: isfiltered=1, normal_comp?\n",
5234f1bc738eSAdrian Chadd 			    __func__);
5235375307d4SAdrian Chadd 		ATH_TX_UNLOCK(sc);
5236d4365d16SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: cleanup_unaggr\n",
5237d4365d16SAdrian Chadd 		    __func__);
5238eb6f0de0SAdrian Chadd 		ath_tx_comp_cleanup_unaggr(sc, bf);
5239eb6f0de0SAdrian Chadd 		return;
5240eb6f0de0SAdrian Chadd 	}
5241eb6f0de0SAdrian Chadd 
5242eb6f0de0SAdrian Chadd 	/*
5243f1bc738eSAdrian Chadd 	 * XXX TODO: how does cleanup, BAR and filtered frame handling
5244f1bc738eSAdrian Chadd 	 * overlap?
5245f1bc738eSAdrian Chadd 	 *
5246f1bc738eSAdrian Chadd 	 * If the frame is filtered OR if it's any failure but
5247f1bc738eSAdrian Chadd 	 * the TID is filtered, the frame must be added to the
5248f1bc738eSAdrian Chadd 	 * filtered frame list.
5249f1bc738eSAdrian Chadd 	 *
5250f1bc738eSAdrian Chadd 	 * However - a busy buffer can't be added to the filtered
5251f1bc738eSAdrian Chadd 	 * list as it will end up being recycled without having
5252f1bc738eSAdrian Chadd 	 * been made available for the hardware.
5253f1bc738eSAdrian Chadd 	 */
52540aa5c1bbSAdrian Chadd 	if ((ts.ts_status & HAL_TXERR_FILT) ||
52550aa5c1bbSAdrian Chadd 	    (ts.ts_status != 0 && atid->isfiltered)) {
5256f1bc738eSAdrian Chadd 		int freeframe;
5257f1bc738eSAdrian Chadd 
5258f1bc738eSAdrian Chadd 		if (fail != 0)
525983bbd5ebSRui Paulo 			DPRINTF(sc, ATH_DEBUG_SW_TX,
5260f1bc738eSAdrian Chadd 			    "%s: isfiltered=1, fail=%d\n",
526183bbd5ebSRui Paulo 			    __func__, fail);
5262f1bc738eSAdrian Chadd 		freeframe = ath_tx_tid_filt_comp_single(sc, atid, bf);
526342fdd8e7SAdrian Chadd 		/*
526442fdd8e7SAdrian Chadd 		 * If freeframe=0 then bf is no longer ours; don't
526542fdd8e7SAdrian Chadd 		 * touch it.
526642fdd8e7SAdrian Chadd 		 */
5267f1bc738eSAdrian Chadd 		if (freeframe) {
5268f1bc738eSAdrian Chadd 			/* Remove from BAW */
5269f1bc738eSAdrian Chadd 			if (bf->bf_state.bfs_addedbaw)
5270f1bc738eSAdrian Chadd 				drops++;
5271f1bc738eSAdrian Chadd 			if (bf->bf_state.bfs_dobaw) {
5272f1bc738eSAdrian Chadd 				ath_tx_update_baw(sc, an, atid, bf);
5273f1bc738eSAdrian Chadd 				if (!bf->bf_state.bfs_addedbaw)
527483bbd5ebSRui Paulo 					DPRINTF(sc, ATH_DEBUG_SW_TX,
5275f1bc738eSAdrian Chadd 					    "%s: wasn't added: seqno %d\n",
5276f1bc738eSAdrian Chadd 					    __func__, SEQNO(bf->bf_state.bfs_seqno));
5277f1bc738eSAdrian Chadd 			}
5278f1bc738eSAdrian Chadd 			bf->bf_state.bfs_dobaw = 0;
5279f1bc738eSAdrian Chadd 		}
5280f1bc738eSAdrian Chadd 
5281f1bc738eSAdrian Chadd 		/*
5282f1bc738eSAdrian Chadd 		 * If the frame couldn't be filtered, treat it as a drop and
5283f1bc738eSAdrian Chadd 		 * prepare to send a BAR.
5284f1bc738eSAdrian Chadd 		 */
5285f1bc738eSAdrian Chadd 		if (freeframe && drops)
5286f1bc738eSAdrian Chadd 			ath_tx_tid_bar_suspend(sc, atid);
5287f1bc738eSAdrian Chadd 
5288f1bc738eSAdrian Chadd 		/*
5289f1bc738eSAdrian Chadd 		 * Send BAR if required
5290f1bc738eSAdrian Chadd 		 */
5291f1bc738eSAdrian Chadd 		if (ath_tx_tid_bar_tx_ready(sc, atid))
5292f1bc738eSAdrian Chadd 			ath_tx_tid_bar_tx(sc, atid);
5293f1bc738eSAdrian Chadd 
5294375307d4SAdrian Chadd 		ATH_TX_UNLOCK(sc);
5295f1bc738eSAdrian Chadd 		/*
5296f1bc738eSAdrian Chadd 		 * If freeframe is set, then the frame couldn't be
5297f1bc738eSAdrian Chadd 		 * cloned and bf is still valid.  Just complete/free it.
5298f1bc738eSAdrian Chadd 		 */
5299f1bc738eSAdrian Chadd 		if (freeframe)
5300f1bc738eSAdrian Chadd 			ath_tx_default_comp(sc, bf, fail);
5301f1bc738eSAdrian Chadd 
5302f1bc738eSAdrian Chadd 		return;
5303f1bc738eSAdrian Chadd 	}
5304f1bc738eSAdrian Chadd 	/*
5305eb6f0de0SAdrian Chadd 	 * Don't bother with the retry check if all frames
5306eb6f0de0SAdrian Chadd 	 * are being failed (eg during queue deletion.)
5307eb6f0de0SAdrian Chadd 	 */
5308e9a6408eSAdrian Chadd #if 0
5309eb6f0de0SAdrian Chadd 	if (fail == 0 && ts->ts_status & HAL_TXERR_XRETRY) {
5310e9a6408eSAdrian Chadd #endif
53110aa5c1bbSAdrian Chadd 	if (fail == 0 && ts.ts_status != 0) {
5312375307d4SAdrian Chadd 		ATH_TX_UNLOCK(sc);
5313d4365d16SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: retry_unaggr\n",
5314d4365d16SAdrian Chadd 		    __func__);
5315eb6f0de0SAdrian Chadd 		ath_tx_aggr_retry_unaggr(sc, bf);
5316eb6f0de0SAdrian Chadd 		return;
5317eb6f0de0SAdrian Chadd 	}
5318eb6f0de0SAdrian Chadd 
5319eb6f0de0SAdrian Chadd 	/* Success? Complete */
5320eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: TID=%d, seqno %d\n",
5321eb6f0de0SAdrian Chadd 	    __func__, tid, SEQNO(bf->bf_state.bfs_seqno));
5322eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_dobaw) {
5323eb6f0de0SAdrian Chadd 		ath_tx_update_baw(sc, an, atid, bf);
5324eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_dobaw = 0;
5325eb6f0de0SAdrian Chadd 		if (!bf->bf_state.bfs_addedbaw)
532683bbd5ebSRui Paulo 			DPRINTF(sc, ATH_DEBUG_SW_TX,
5327eb6f0de0SAdrian Chadd 			    "%s: wasn't added: seqno %d\n",
5328eb6f0de0SAdrian Chadd 			    __func__, SEQNO(bf->bf_state.bfs_seqno));
5329eb6f0de0SAdrian Chadd 	}
5330eb6f0de0SAdrian Chadd 
533188b3d483SAdrian Chadd 	/*
5332f1bc738eSAdrian Chadd 	 * If the queue is filtered, re-schedule as required.
5333f1bc738eSAdrian Chadd 	 *
5334f1bc738eSAdrian Chadd 	 * This is required as there may be a subsequent TX descriptor
5335f1bc738eSAdrian Chadd 	 * for this end-node that has CLRDMASK set, so it's quite possible
5336f1bc738eSAdrian Chadd 	 * that a filtered frame will be followed by a non-filtered
5337f1bc738eSAdrian Chadd 	 * (complete or otherwise) frame.
5338f1bc738eSAdrian Chadd 	 *
5339f1bc738eSAdrian Chadd 	 * XXX should we do this before we complete the frame?
5340f1bc738eSAdrian Chadd 	 */
5341f1bc738eSAdrian Chadd 	if (atid->isfiltered)
5342f1bc738eSAdrian Chadd 		ath_tx_tid_filt_comp_complete(sc, atid);
5343f1bc738eSAdrian Chadd 
5344f1bc738eSAdrian Chadd 	/*
534588b3d483SAdrian Chadd 	 * Send BAR if required
534688b3d483SAdrian Chadd 	 */
534788b3d483SAdrian Chadd 	if (ath_tx_tid_bar_tx_ready(sc, atid))
534888b3d483SAdrian Chadd 		ath_tx_tid_bar_tx(sc, atid);
534988b3d483SAdrian Chadd 
5350375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
5351eb6f0de0SAdrian Chadd 
5352eb6f0de0SAdrian Chadd 	ath_tx_default_comp(sc, bf, fail);
5353eb6f0de0SAdrian Chadd 	/* bf is freed at this point */
5354eb6f0de0SAdrian Chadd }
5355eb6f0de0SAdrian Chadd 
5356eb6f0de0SAdrian Chadd void
5357eb6f0de0SAdrian Chadd ath_tx_aggr_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
5358eb6f0de0SAdrian Chadd {
5359eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_aggr)
5360eb6f0de0SAdrian Chadd 		ath_tx_aggr_comp_aggr(sc, bf, fail);
5361eb6f0de0SAdrian Chadd 	else
5362eb6f0de0SAdrian Chadd 		ath_tx_aggr_comp_unaggr(sc, bf, fail);
5363eb6f0de0SAdrian Chadd }
5364eb6f0de0SAdrian Chadd 
5365eb6f0de0SAdrian Chadd /*
5366*cce63444SAdrian Chadd  * Grab the software queue depth that we COULD transmit.
5367*cce63444SAdrian Chadd  *
5368*cce63444SAdrian Chadd  * This includes checks if it's in the BAW, whether it's a frame
5369*cce63444SAdrian Chadd  * that is supposed to be in the BAW.  Other checks could be done;
5370*cce63444SAdrian Chadd  * but for now let's try and avoid doing the whole of ath_tx_form_aggr()
5371*cce63444SAdrian Chadd  * here.
5372*cce63444SAdrian Chadd  */
5373*cce63444SAdrian Chadd static int
5374*cce63444SAdrian Chadd ath_tx_tid_swq_depth_bytes(struct ath_softc *sc, struct ath_node *an,
5375*cce63444SAdrian Chadd     struct ath_tid *tid)
5376*cce63444SAdrian Chadd {
5377*cce63444SAdrian Chadd 	struct ath_buf *bf;
5378*cce63444SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
5379*cce63444SAdrian Chadd 	int nbytes = 0;
5380*cce63444SAdrian Chadd 
5381*cce63444SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
5382*cce63444SAdrian Chadd 
5383*cce63444SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
5384*cce63444SAdrian Chadd 
5385*cce63444SAdrian Chadd 	/*
5386*cce63444SAdrian Chadd 	 * Iterate over each buffer and sum the pkt_len.
5387*cce63444SAdrian Chadd 	 * Bail if we exceed ATH_AGGR_MAXSIZE bytes; we won't
5388*cce63444SAdrian Chadd 	 * ever queue more than that in a single frame.
5389*cce63444SAdrian Chadd 	 */
5390*cce63444SAdrian Chadd 	TAILQ_FOREACH(bf, &tid->tid_q, bf_list) {
5391*cce63444SAdrian Chadd 
5392*cce63444SAdrian Chadd 		/*
5393*cce63444SAdrian Chadd 		 * TODO: I'm not sure if we're going to hit cases where
5394*cce63444SAdrian Chadd 		 * no frames get sent because the list is empty.
5395*cce63444SAdrian Chadd 		 */
5396*cce63444SAdrian Chadd 
5397*cce63444SAdrian Chadd 		/* Check if it's in the BAW */
5398*cce63444SAdrian Chadd 		if (tap != NULL && (! BAW_WITHIN(tap->txa_start, tap->txa_wnd,
5399*cce63444SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno)))) {
5400*cce63444SAdrian Chadd 			break;
5401*cce63444SAdrian Chadd 		}
5402*cce63444SAdrian Chadd 
5403*cce63444SAdrian Chadd 		/* Check if it's even supposed to be in the BAW */
5404*cce63444SAdrian Chadd 		if (! bf->bf_state.bfs_dobaw) {
5405*cce63444SAdrian Chadd 			break;
5406*cce63444SAdrian Chadd 		}
5407*cce63444SAdrian Chadd 
5408*cce63444SAdrian Chadd 		nbytes += bf->bf_state.bfs_pktlen;
5409*cce63444SAdrian Chadd 		if (nbytes >= ATH_AGGR_MAXSIZE)
5410*cce63444SAdrian Chadd 			break;
5411*cce63444SAdrian Chadd 
5412*cce63444SAdrian Chadd 		/*
5413*cce63444SAdrian Chadd 		 * Check if we're likely going to leak a frame
5414*cce63444SAdrian Chadd 		 * as part of a PSPOLL.  Break out at this point;
5415*cce63444SAdrian Chadd 		 * we're only going to send a single frame anyway.
5416*cce63444SAdrian Chadd 		 */
5417*cce63444SAdrian Chadd 		if (an->an_leak_count) {
5418*cce63444SAdrian Chadd 			break;
5419*cce63444SAdrian Chadd 		}
5420*cce63444SAdrian Chadd 	}
5421*cce63444SAdrian Chadd 
5422*cce63444SAdrian Chadd 	return MIN(nbytes, ATH_AGGR_MAXSIZE);
5423*cce63444SAdrian Chadd }
5424*cce63444SAdrian Chadd 
5425*cce63444SAdrian Chadd /*
5426eb6f0de0SAdrian Chadd  * Schedule some packets from the given node/TID to the hardware.
5427eb6f0de0SAdrian Chadd  *
5428eb6f0de0SAdrian Chadd  * This is the aggregate version.
5429eb6f0de0SAdrian Chadd  */
5430eb6f0de0SAdrian Chadd void
5431eb6f0de0SAdrian Chadd ath_tx_tid_hw_queue_aggr(struct ath_softc *sc, struct ath_node *an,
5432eb6f0de0SAdrian Chadd     struct ath_tid *tid)
5433eb6f0de0SAdrian Chadd {
5434eb6f0de0SAdrian Chadd 	struct ath_buf *bf;
5435eb6f0de0SAdrian Chadd 	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
5436eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
5437eb6f0de0SAdrian Chadd 	ATH_AGGR_STATUS status;
5438eb6f0de0SAdrian Chadd 	ath_bufhead bf_q;
5439*cce63444SAdrian Chadd 	int swq_pktbytes;
5440eb6f0de0SAdrian Chadd 
5441eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d\n", __func__, tid->tid);
5442375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
5443eb6f0de0SAdrian Chadd 
544422a3aee6SAdrian Chadd 	/*
544522a3aee6SAdrian Chadd 	 * XXX TODO: If we're called for a queue that we're leaking frames to,
544622a3aee6SAdrian Chadd 	 * ensure we only leak one.
544722a3aee6SAdrian Chadd 	 */
544822a3aee6SAdrian Chadd 
5449eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
5450eb6f0de0SAdrian Chadd 
5451eb6f0de0SAdrian Chadd 	if (tid->tid == IEEE80211_NONQOS_TID)
545283bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX,
545383bbd5ebSRui Paulo 		    "%s: called for TID=NONQOS_TID?\n", __func__);
5454eb6f0de0SAdrian Chadd 
5455eb6f0de0SAdrian Chadd 	for (;;) {
5456eb6f0de0SAdrian Chadd 		status = ATH_AGGR_DONE;
5457eb6f0de0SAdrian Chadd 
5458eb6f0de0SAdrian Chadd 		/*
5459eb6f0de0SAdrian Chadd 		 * If the upper layer has paused the TID, don't
5460eb6f0de0SAdrian Chadd 		 * queue any further packets.
5461eb6f0de0SAdrian Chadd 		 *
5462eb6f0de0SAdrian Chadd 		 * This can also occur from the completion task because
5463eb6f0de0SAdrian Chadd 		 * of packet loss; but as its serialised with this code,
5464eb6f0de0SAdrian Chadd 		 * it won't "appear" half way through queuing packets.
5465eb6f0de0SAdrian Chadd 		 */
546622a3aee6SAdrian Chadd 		if (! ath_tx_tid_can_tx_or_sched(sc, tid))
5467eb6f0de0SAdrian Chadd 			break;
5468eb6f0de0SAdrian Chadd 
54693e6cc97fSAdrian Chadd 		bf = ATH_TID_FIRST(tid);
5470eb6f0de0SAdrian Chadd 		if (bf == NULL) {
5471eb6f0de0SAdrian Chadd 			break;
5472eb6f0de0SAdrian Chadd 		}
5473eb6f0de0SAdrian Chadd 
5474eb6f0de0SAdrian Chadd 		/*
5475eb6f0de0SAdrian Chadd 		 * If the packet doesn't fall within the BAW (eg a NULL
5476eb6f0de0SAdrian Chadd 		 * data frame), schedule it directly; continue.
5477eb6f0de0SAdrian Chadd 		 */
5478eb6f0de0SAdrian Chadd 		if (! bf->bf_state.bfs_dobaw) {
5479d4365d16SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
5480d4365d16SAdrian Chadd 			    "%s: non-baw packet\n",
5481eb6f0de0SAdrian Chadd 			    __func__);
54823e6cc97fSAdrian Chadd 			ATH_TID_REMOVE(tid, bf, bf_list);
54832a9f83afSAdrian Chadd 
54842a9f83afSAdrian Chadd 			if (bf->bf_state.bfs_nframes > 1)
548583bbd5ebSRui Paulo 				DPRINTF(sc, ATH_DEBUG_SW_TX,
54862a9f83afSAdrian Chadd 				    "%s: aggr=%d, nframes=%d\n",
54872a9f83afSAdrian Chadd 				    __func__,
54882a9f83afSAdrian Chadd 				    bf->bf_state.bfs_aggr,
54892a9f83afSAdrian Chadd 				    bf->bf_state.bfs_nframes);
54902a9f83afSAdrian Chadd 
54912a9f83afSAdrian Chadd 			/*
54922a9f83afSAdrian Chadd 			 * This shouldn't happen - such frames shouldn't
54932a9f83afSAdrian Chadd 			 * ever have been queued as an aggregate in the
54942a9f83afSAdrian Chadd 			 * first place.  However, make sure the fields
54952a9f83afSAdrian Chadd 			 * are correctly setup just to be totally sure.
54962a9f83afSAdrian Chadd 			 */
5497eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_aggr = 0;
54982a9f83afSAdrian Chadd 			bf->bf_state.bfs_nframes = 1;
54992a9f83afSAdrian Chadd 
55004e81f27cSAdrian Chadd 			/* Update CLRDMASK just before this frame is queued */
55014e81f27cSAdrian Chadd 			ath_tx_update_clrdmask(sc, tid, bf);
55024e81f27cSAdrian Chadd 
5503*cce63444SAdrian Chadd 			ath_tx_do_ratelookup(sc, bf, tid->tid,
5504*cce63444SAdrian Chadd 			    bf->bf_state.bfs_pktlen, false);
5505e2e4a2c2SAdrian Chadd 			ath_tx_calc_duration(sc, bf);
5506e2e4a2c2SAdrian Chadd 			ath_tx_calc_protection(sc, bf);
5507eb6f0de0SAdrian Chadd 			ath_tx_set_rtscts(sc, bf);
5508e2e4a2c2SAdrian Chadd 			ath_tx_rate_fill_rcflags(sc, bf);
5509eb6f0de0SAdrian Chadd 			ath_tx_setds(sc, bf);
5510eb6f0de0SAdrian Chadd 			ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
5511eb6f0de0SAdrian Chadd 
5512eb6f0de0SAdrian Chadd 			sc->sc_aggr_stats.aggr_nonbaw_pkt++;
5513eb6f0de0SAdrian Chadd 
5514eb6f0de0SAdrian Chadd 			/* Queue the packet; continue */
5515eb6f0de0SAdrian Chadd 			goto queuepkt;
5516eb6f0de0SAdrian Chadd 		}
5517eb6f0de0SAdrian Chadd 
5518eb6f0de0SAdrian Chadd 		TAILQ_INIT(&bf_q);
5519eb6f0de0SAdrian Chadd 
5520eb6f0de0SAdrian Chadd 		/*
5521*cce63444SAdrian Chadd 		 * Loop over the swq to find out how long
5522*cce63444SAdrian Chadd 		 * each packet is (up until 64k) and provide that
5523*cce63444SAdrian Chadd 		 * to the rate control lookup.
5524eb6f0de0SAdrian Chadd 		 */
5525*cce63444SAdrian Chadd 		swq_pktbytes = ath_tx_tid_swq_depth_bytes(sc, an, tid);
5526*cce63444SAdrian Chadd 		ath_tx_do_ratelookup(sc, bf, tid->tid, swq_pktbytes, true);
5527e2e4a2c2SAdrian Chadd 
5528*cce63444SAdrian Chadd 		/*
5529*cce63444SAdrian Chadd 		 * Note this only is used for the fragment paths and
5530*cce63444SAdrian Chadd 		 * should really be rethought out if we want to do
5531*cce63444SAdrian Chadd 		 * things like an RTS burst across >1 aggregate.
5532*cce63444SAdrian Chadd 		 */
5533e2e4a2c2SAdrian Chadd 		ath_tx_calc_duration(sc, bf);
5534e2e4a2c2SAdrian Chadd 		ath_tx_calc_protection(sc, bf);
5535e2e4a2c2SAdrian Chadd 
5536e2e4a2c2SAdrian Chadd 		ath_tx_set_rtscts(sc, bf);
5537eb6f0de0SAdrian Chadd 		ath_tx_rate_fill_rcflags(sc, bf);
5538eb6f0de0SAdrian Chadd 
5539eb6f0de0SAdrian Chadd 		status = ath_tx_form_aggr(sc, an, tid, &bf_q);
5540eb6f0de0SAdrian Chadd 
5541eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
5542eb6f0de0SAdrian Chadd 		    "%s: ath_tx_form_aggr() status=%d\n", __func__, status);
5543eb6f0de0SAdrian Chadd 
5544eb6f0de0SAdrian Chadd 		/*
5545eb6f0de0SAdrian Chadd 		 * No frames to be picked up - out of BAW
5546eb6f0de0SAdrian Chadd 		 */
5547eb6f0de0SAdrian Chadd 		if (TAILQ_EMPTY(&bf_q))
5548eb6f0de0SAdrian Chadd 			break;
5549eb6f0de0SAdrian Chadd 
5550eb6f0de0SAdrian Chadd 		/*
5551eb6f0de0SAdrian Chadd 		 * This assumes that the descriptor list in the ath_bufhead
5552eb6f0de0SAdrian Chadd 		 * are already linked together via bf_next pointers.
5553eb6f0de0SAdrian Chadd 		 */
5554eb6f0de0SAdrian Chadd 		bf = TAILQ_FIRST(&bf_q);
5555eb6f0de0SAdrian Chadd 
5556e2e4a2c2SAdrian Chadd 		if (status == ATH_AGGR_8K_LIMITED)
5557e2e4a2c2SAdrian Chadd 			sc->sc_aggr_stats.aggr_rts_aggr_limited++;
5558e2e4a2c2SAdrian Chadd 
5559eb6f0de0SAdrian Chadd 		/*
5560eb6f0de0SAdrian Chadd 		 * If it's the only frame send as non-aggregate
5561eb6f0de0SAdrian Chadd 		 * assume that ath_tx_form_aggr() has checked
5562eb6f0de0SAdrian Chadd 		 * whether it's in the BAW and added it appropriately.
5563eb6f0de0SAdrian Chadd 		 */
5564eb6f0de0SAdrian Chadd 		if (bf->bf_state.bfs_nframes == 1) {
5565eb6f0de0SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
5566eb6f0de0SAdrian Chadd 			    "%s: single-frame aggregate\n", __func__);
55674e81f27cSAdrian Chadd 
55684e81f27cSAdrian Chadd 			/* Update CLRDMASK just before this frame is queued */
55694e81f27cSAdrian Chadd 			ath_tx_update_clrdmask(sc, tid, bf);
55704e81f27cSAdrian Chadd 
5571eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_aggr = 0;
557221840808SAdrian Chadd 			bf->bf_state.bfs_ndelim = 0;
5573eb6f0de0SAdrian Chadd 			ath_tx_setds(sc, bf);
5574eb6f0de0SAdrian Chadd 			ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
5575eb6f0de0SAdrian Chadd 			if (status == ATH_AGGR_BAW_CLOSED)
5576eb6f0de0SAdrian Chadd 				sc->sc_aggr_stats.aggr_baw_closed_single_pkt++;
5577eb6f0de0SAdrian Chadd 			else
5578eb6f0de0SAdrian Chadd 				sc->sc_aggr_stats.aggr_single_pkt++;
5579eb6f0de0SAdrian Chadd 		} else {
5580eb6f0de0SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
5581d4365d16SAdrian Chadd 			    "%s: multi-frame aggregate: %d frames, "
5582d4365d16SAdrian Chadd 			    "length %d\n",
5583eb6f0de0SAdrian Chadd 			     __func__, bf->bf_state.bfs_nframes,
5584eb6f0de0SAdrian Chadd 			    bf->bf_state.bfs_al);
5585eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_aggr = 1;
5586eb6f0de0SAdrian Chadd 			sc->sc_aggr_stats.aggr_pkts[bf->bf_state.bfs_nframes]++;
5587eb6f0de0SAdrian Chadd 			sc->sc_aggr_stats.aggr_aggr_pkt++;
5588eb6f0de0SAdrian Chadd 
55894e81f27cSAdrian Chadd 			/* Update CLRDMASK just before this frame is queued */
55904e81f27cSAdrian Chadd 			ath_tx_update_clrdmask(sc, tid, bf);
55914e81f27cSAdrian Chadd 
5592eb6f0de0SAdrian Chadd 			/*
5593e2e4a2c2SAdrian Chadd 			 * Calculate the duration/protection as required.
5594e2e4a2c2SAdrian Chadd 			 */
5595e2e4a2c2SAdrian Chadd 			ath_tx_calc_duration(sc, bf);
5596e2e4a2c2SAdrian Chadd 			ath_tx_calc_protection(sc, bf);
5597e2e4a2c2SAdrian Chadd 
5598e2e4a2c2SAdrian Chadd 			/*
5599eb6f0de0SAdrian Chadd 			 * Update the rate and rtscts information based on the
5600eb6f0de0SAdrian Chadd 			 * rate decision made by the rate control code;
5601eb6f0de0SAdrian Chadd 			 * the first frame in the aggregate needs it.
5602eb6f0de0SAdrian Chadd 			 */
5603eb6f0de0SAdrian Chadd 			ath_tx_set_rtscts(sc, bf);
5604eb6f0de0SAdrian Chadd 
5605eb6f0de0SAdrian Chadd 			/*
5606eb6f0de0SAdrian Chadd 			 * Setup the relevant descriptor fields
5607eb6f0de0SAdrian Chadd 			 * for aggregation. The first descriptor
5608eb6f0de0SAdrian Chadd 			 * already points to the rest in the chain.
5609eb6f0de0SAdrian Chadd 			 */
5610eb6f0de0SAdrian Chadd 			ath_tx_setds_11n(sc, bf);
5611eb6f0de0SAdrian Chadd 
5612eb6f0de0SAdrian Chadd 		}
5613eb6f0de0SAdrian Chadd 	queuepkt:
5614eb6f0de0SAdrian Chadd 		/* Set completion handler, multi-frame aggregate or not */
5615eb6f0de0SAdrian Chadd 		bf->bf_comp = ath_tx_aggr_comp;
5616eb6f0de0SAdrian Chadd 
5617eb6f0de0SAdrian Chadd 		if (bf->bf_state.bfs_tid == IEEE80211_NONQOS_TID)
561883bbd5ebSRui Paulo 			DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: TID=16?\n", __func__);
5619eb6f0de0SAdrian Chadd 
562022a3aee6SAdrian Chadd 		/*
562122a3aee6SAdrian Chadd 		 * Update leak count and frame config if were leaking frames.
562222a3aee6SAdrian Chadd 		 *
562322a3aee6SAdrian Chadd 		 * XXX TODO: it should update all frames in an aggregate
562422a3aee6SAdrian Chadd 		 * correctly!
562522a3aee6SAdrian Chadd 		 */
562622a3aee6SAdrian Chadd 		ath_tx_leak_count_update(sc, tid, bf);
562722a3aee6SAdrian Chadd 
5628eb6f0de0SAdrian Chadd 		/* Punt to txq */
5629eb6f0de0SAdrian Chadd 		ath_tx_handoff(sc, txq, bf);
5630eb6f0de0SAdrian Chadd 
5631eb6f0de0SAdrian Chadd 		/* Track outstanding buffer count to hardware */
5632eb6f0de0SAdrian Chadd 		/* aggregates are "one" buffer */
5633eb6f0de0SAdrian Chadd 		tid->hwq_depth++;
5634eb6f0de0SAdrian Chadd 
5635eb6f0de0SAdrian Chadd 		/*
5636eb6f0de0SAdrian Chadd 		 * Break out if ath_tx_form_aggr() indicated
5637eb6f0de0SAdrian Chadd 		 * there can't be any further progress (eg BAW is full.)
5638eb6f0de0SAdrian Chadd 		 * Checking for an empty txq is done above.
5639eb6f0de0SAdrian Chadd 		 *
5640eb6f0de0SAdrian Chadd 		 * XXX locking on txq here?
5641eb6f0de0SAdrian Chadd 		 */
564272910f03SAdrian Chadd 		/* XXX TXQ locking */
564372910f03SAdrian Chadd 		if (txq->axq_aggr_depth >= sc->sc_hwq_limit_aggr ||
564422a3aee6SAdrian Chadd 		    (status == ATH_AGGR_BAW_CLOSED ||
564522a3aee6SAdrian Chadd 		     status == ATH_AGGR_LEAK_CLOSED))
5646eb6f0de0SAdrian Chadd 			break;
5647eb6f0de0SAdrian Chadd 	}
5648eb6f0de0SAdrian Chadd }
5649eb6f0de0SAdrian Chadd 
5650eb6f0de0SAdrian Chadd /*
5651eb6f0de0SAdrian Chadd  * Schedule some packets from the given node/TID to the hardware.
565272910f03SAdrian Chadd  *
565372910f03SAdrian Chadd  * XXX TODO: this routine doesn't enforce the maximum TXQ depth.
565472910f03SAdrian Chadd  * It just dumps frames into the TXQ.  We should limit how deep
565572910f03SAdrian Chadd  * the transmit queue can grow for frames dispatched to the given
565672910f03SAdrian Chadd  * TXQ.
565772910f03SAdrian Chadd  *
565872910f03SAdrian Chadd  * To avoid locking issues, either we need to own the TXQ lock
565972910f03SAdrian Chadd  * at this point, or we need to pass in the maximum frame count
566072910f03SAdrian Chadd  * from the caller.
5661eb6f0de0SAdrian Chadd  */
5662eb6f0de0SAdrian Chadd void
5663eb6f0de0SAdrian Chadd ath_tx_tid_hw_queue_norm(struct ath_softc *sc, struct ath_node *an,
5664eb6f0de0SAdrian Chadd     struct ath_tid *tid)
5665eb6f0de0SAdrian Chadd {
5666eb6f0de0SAdrian Chadd 	struct ath_buf *bf;
5667eb6f0de0SAdrian Chadd 	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
5668eb6f0de0SAdrian Chadd 
5669eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: node %p: TID %d: called\n",
5670eb6f0de0SAdrian Chadd 	    __func__, an, tid->tid);
5671eb6f0de0SAdrian Chadd 
5672375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
5673eb6f0de0SAdrian Chadd 
5674eb6f0de0SAdrian Chadd 	/* Check - is AMPDU pending or running? then print out something */
5675eb6f0de0SAdrian Chadd 	if (ath_tx_ampdu_pending(sc, an, tid->tid))
567683bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d, ampdu pending?\n",
5677eb6f0de0SAdrian Chadd 		    __func__, tid->tid);
5678eb6f0de0SAdrian Chadd 	if (ath_tx_ampdu_running(sc, an, tid->tid))
567983bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d, ampdu running?\n",
5680eb6f0de0SAdrian Chadd 		    __func__, tid->tid);
5681eb6f0de0SAdrian Chadd 
5682eb6f0de0SAdrian Chadd 	for (;;) {
5683eb6f0de0SAdrian Chadd 
5684eb6f0de0SAdrian Chadd 		/*
5685eb6f0de0SAdrian Chadd 		 * If the upper layers have paused the TID, don't
5686eb6f0de0SAdrian Chadd 		 * queue any further packets.
568722a3aee6SAdrian Chadd 		 *
568822a3aee6SAdrian Chadd 		 * XXX if we are leaking frames, make sure we decrement
568922a3aee6SAdrian Chadd 		 * that counter _and_ we continue here.
5690eb6f0de0SAdrian Chadd 		 */
569122a3aee6SAdrian Chadd 		if (! ath_tx_tid_can_tx_or_sched(sc, tid))
5692eb6f0de0SAdrian Chadd 			break;
5693eb6f0de0SAdrian Chadd 
56943e6cc97fSAdrian Chadd 		bf = ATH_TID_FIRST(tid);
5695eb6f0de0SAdrian Chadd 		if (bf == NULL) {
5696eb6f0de0SAdrian Chadd 			break;
5697eb6f0de0SAdrian Chadd 		}
5698eb6f0de0SAdrian Chadd 
56993e6cc97fSAdrian Chadd 		ATH_TID_REMOVE(tid, bf, bf_list);
5700eb6f0de0SAdrian Chadd 
5701eb6f0de0SAdrian Chadd 		/* Sanity check! */
5702eb6f0de0SAdrian Chadd 		if (tid->tid != bf->bf_state.bfs_tid) {
570383bbd5ebSRui Paulo 			DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bfs_tid %d !="
570483bbd5ebSRui Paulo 			    " tid %d\n", __func__, bf->bf_state.bfs_tid,
570583bbd5ebSRui Paulo 			    tid->tid);
5706eb6f0de0SAdrian Chadd 		}
5707eb6f0de0SAdrian Chadd 		/* Normal completion handler */
5708eb6f0de0SAdrian Chadd 		bf->bf_comp = ath_tx_normal_comp;
5709eb6f0de0SAdrian Chadd 
57100c54de88SAdrian Chadd 		/*
57110c54de88SAdrian Chadd 		 * Override this for now, until the non-aggregate
57120c54de88SAdrian Chadd 		 * completion handler correctly handles software retransmits.
57130c54de88SAdrian Chadd 		 */
57140c54de88SAdrian Chadd 		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
57150c54de88SAdrian Chadd 
57164e81f27cSAdrian Chadd 		/* Update CLRDMASK just before this frame is queued */
57174e81f27cSAdrian Chadd 		ath_tx_update_clrdmask(sc, tid, bf);
57184e81f27cSAdrian Chadd 
5719eb6f0de0SAdrian Chadd 		/* Program descriptors + rate control */
5720*cce63444SAdrian Chadd 		ath_tx_do_ratelookup(sc, bf, tid->tid,
5721*cce63444SAdrian Chadd 		    bf->bf_state.bfs_pktlen, false);
5722e2e4a2c2SAdrian Chadd 		ath_tx_calc_duration(sc, bf);
5723e2e4a2c2SAdrian Chadd 		ath_tx_calc_protection(sc, bf);
5724eb6f0de0SAdrian Chadd 		ath_tx_set_rtscts(sc, bf);
5725e2e4a2c2SAdrian Chadd 		ath_tx_rate_fill_rcflags(sc, bf);
5726eb6f0de0SAdrian Chadd 		ath_tx_setds(sc, bf);
5727eb6f0de0SAdrian Chadd 
572822a3aee6SAdrian Chadd 		/*
572922a3aee6SAdrian Chadd 		 * Update the current leak count if
573022a3aee6SAdrian Chadd 		 * we're leaking frames; and set the
573122a3aee6SAdrian Chadd 		 * MORE flag as appropriate.
573222a3aee6SAdrian Chadd 		 */
573322a3aee6SAdrian Chadd 		ath_tx_leak_count_update(sc, tid, bf);
573422a3aee6SAdrian Chadd 
5735eb6f0de0SAdrian Chadd 		/* Track outstanding buffer count to hardware */
5736eb6f0de0SAdrian Chadd 		/* aggregates are "one" buffer */
5737eb6f0de0SAdrian Chadd 		tid->hwq_depth++;
5738eb6f0de0SAdrian Chadd 
5739eb6f0de0SAdrian Chadd 		/* Punt to hardware or software txq */
5740eb6f0de0SAdrian Chadd 		ath_tx_handoff(sc, txq, bf);
5741eb6f0de0SAdrian Chadd 	}
5742eb6f0de0SAdrian Chadd }
5743eb6f0de0SAdrian Chadd 
5744eb6f0de0SAdrian Chadd /*
5745eb6f0de0SAdrian Chadd  * Schedule some packets to the given hardware queue.
5746eb6f0de0SAdrian Chadd  *
5747eb6f0de0SAdrian Chadd  * This function walks the list of TIDs (ie, ath_node TIDs
5748eb6f0de0SAdrian Chadd  * with queued traffic) and attempts to schedule traffic
5749eb6f0de0SAdrian Chadd  * from them.
5750eb6f0de0SAdrian Chadd  *
5751eb6f0de0SAdrian Chadd  * TID scheduling is implemented as a FIFO, with TIDs being
5752eb6f0de0SAdrian Chadd  * added to the end of the queue after some frames have been
5753eb6f0de0SAdrian Chadd  * scheduled.
5754eb6f0de0SAdrian Chadd  */
5755eb6f0de0SAdrian Chadd void
5756eb6f0de0SAdrian Chadd ath_txq_sched(struct ath_softc *sc, struct ath_txq *txq)
5757eb6f0de0SAdrian Chadd {
5758eb6f0de0SAdrian Chadd 	struct ath_tid *tid, *next, *last;
5759eb6f0de0SAdrian Chadd 
5760375307d4SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
5761eb6f0de0SAdrian Chadd 
5762eb6f0de0SAdrian Chadd 	/*
576357af292dSAdrian Chadd 	 * For non-EDMA chips, aggr frames that have been built are
576457af292dSAdrian Chadd 	 * in axq_aggr_depth, whether they've been scheduled or not.
576557af292dSAdrian Chadd 	 * There's no FIFO, so txq->axq_depth is what's been scheduled
576657af292dSAdrian Chadd 	 * to the hardware.
576772910f03SAdrian Chadd 	 *
576857af292dSAdrian Chadd 	 * For EDMA chips, we do it in two stages.  The existing code
576957af292dSAdrian Chadd 	 * builds a list of frames to go to the hardware and the EDMA
577057af292dSAdrian Chadd 	 * code turns it into a single entry to push into the FIFO.
577157af292dSAdrian Chadd 	 * That way we don't take up one packet per FIFO slot.
577257af292dSAdrian Chadd 	 * We do push one aggregate per FIFO slot though, just to keep
577357af292dSAdrian Chadd 	 * things simple.
577457af292dSAdrian Chadd 	 *
577557af292dSAdrian Chadd 	 * The FIFO depth is what's in the hardware; the txq->axq_depth
577657af292dSAdrian Chadd 	 * is what's been scheduled to the FIFO.
577757af292dSAdrian Chadd 	 *
577857af292dSAdrian Chadd 	 * fifo.axq_depth is the number of frames (or aggregates) pushed
577957af292dSAdrian Chadd 	 *  into the EDMA FIFO.  For multi-frame lists, this is the number
578057af292dSAdrian Chadd 	 *  of frames pushed in.
578157af292dSAdrian Chadd 	 * axq_fifo_depth is the number of FIFO slots currently busy.
5782eb6f0de0SAdrian Chadd 	 */
578357af292dSAdrian Chadd 
578457af292dSAdrian Chadd 	/* For EDMA and non-EDMA, check built/scheduled against aggr limit */
578557af292dSAdrian Chadd 	if (txq->axq_aggr_depth >= sc->sc_hwq_limit_aggr) {
578672910f03SAdrian Chadd 		sc->sc_aggr_stats.aggr_sched_nopkt++;
578772910f03SAdrian Chadd 		return;
578872910f03SAdrian Chadd 	}
578957af292dSAdrian Chadd 
579057af292dSAdrian Chadd 	/*
579157af292dSAdrian Chadd 	 * For non-EDMA chips, axq_depth is the "what's scheduled to
579257af292dSAdrian Chadd 	 * the hardware list".  For EDMA it's "What's built for the hardware"
579357af292dSAdrian Chadd 	 * and fifo.axq_depth is how many frames have been dispatched
579457af292dSAdrian Chadd 	 * already to the hardware.
579557af292dSAdrian Chadd 	 */
579657af292dSAdrian Chadd 	if (txq->axq_depth + txq->fifo.axq_depth >= sc->sc_hwq_limit_nonaggr) {
5797eb6f0de0SAdrian Chadd 		sc->sc_aggr_stats.aggr_sched_nopkt++;
5798eb6f0de0SAdrian Chadd 		return;
5799eb6f0de0SAdrian Chadd 	}
5800eb6f0de0SAdrian Chadd 
5801eb6f0de0SAdrian Chadd 	last = TAILQ_LAST(&txq->axq_tidq, axq_t_s);
5802eb6f0de0SAdrian Chadd 
5803eb6f0de0SAdrian Chadd 	TAILQ_FOREACH_SAFE(tid, &txq->axq_tidq, axq_qelem, next) {
5804eb6f0de0SAdrian Chadd 		/*
5805eb6f0de0SAdrian Chadd 		 * Suspend paused queues here; they'll be resumed
5806eb6f0de0SAdrian Chadd 		 * once the addba completes or times out.
5807eb6f0de0SAdrian Chadd 		 */
5808eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d, paused=%d\n",
5809eb6f0de0SAdrian Chadd 		    __func__, tid->tid, tid->paused);
5810eb6f0de0SAdrian Chadd 		ath_tx_tid_unsched(sc, tid);
581122a3aee6SAdrian Chadd 		/*
581222a3aee6SAdrian Chadd 		 * This node may be in power-save and we're leaking
581322a3aee6SAdrian Chadd 		 * a frame; be careful.
581422a3aee6SAdrian Chadd 		 */
581522a3aee6SAdrian Chadd 		if (! ath_tx_tid_can_tx_or_sched(sc, tid)) {
58168ec9220eSAdrian Chadd 			goto loop_done;
5817eb6f0de0SAdrian Chadd 		}
5818eb6f0de0SAdrian Chadd 		if (ath_tx_ampdu_running(sc, tid->an, tid->tid))
5819eb6f0de0SAdrian Chadd 			ath_tx_tid_hw_queue_aggr(sc, tid->an, tid);
5820eb6f0de0SAdrian Chadd 		else
5821eb6f0de0SAdrian Chadd 			ath_tx_tid_hw_queue_norm(sc, tid->an, tid);
5822eb6f0de0SAdrian Chadd 
5823eb6f0de0SAdrian Chadd 		/* Not empty? Re-schedule */
5824eb6f0de0SAdrian Chadd 		if (tid->axq_depth != 0)
5825eb6f0de0SAdrian Chadd 			ath_tx_tid_sched(sc, tid);
5826eb6f0de0SAdrian Chadd 
5827b45a991eSAdrian Chadd 		/*
5828b45a991eSAdrian Chadd 		 * Give the software queue time to aggregate more
5829b45a991eSAdrian Chadd 		 * packets.  If we aren't running aggregation then
5830b45a991eSAdrian Chadd 		 * we should still limit the hardware queue depth.
5831b45a991eSAdrian Chadd 		 */
583272910f03SAdrian Chadd 		/* XXX TXQ locking */
583372910f03SAdrian Chadd 		if (txq->axq_aggr_depth + txq->fifo.axq_depth >= sc->sc_hwq_limit_aggr) {
583472910f03SAdrian Chadd 			break;
583572910f03SAdrian Chadd 		}
583672910f03SAdrian Chadd 		if (txq->axq_depth >= sc->sc_hwq_limit_nonaggr) {
5837eb6f0de0SAdrian Chadd 			break;
5838eb6f0de0SAdrian Chadd 		}
58398ec9220eSAdrian Chadd loop_done:
5840eb6f0de0SAdrian Chadd 		/*
5841eb6f0de0SAdrian Chadd 		 * If this was the last entry on the original list, stop.
5842eb6f0de0SAdrian Chadd 		 * Otherwise nodes that have been rescheduled onto the end
5843eb6f0de0SAdrian Chadd 		 * of the TID FIFO list will just keep being rescheduled.
584422a3aee6SAdrian Chadd 		 *
584522a3aee6SAdrian Chadd 		 * XXX What should we do about nodes that were paused
584622a3aee6SAdrian Chadd 		 * but are pending a leaking frame in response to a ps-poll?
584722a3aee6SAdrian Chadd 		 * They'll be put at the front of the list; so they'll
584822a3aee6SAdrian Chadd 		 * prematurely trigger this condition! Ew.
5849eb6f0de0SAdrian Chadd 		 */
5850eb6f0de0SAdrian Chadd 		if (tid == last)
5851eb6f0de0SAdrian Chadd 			break;
5852eb6f0de0SAdrian Chadd 	}
5853eb6f0de0SAdrian Chadd }
5854eb6f0de0SAdrian Chadd 
5855eb6f0de0SAdrian Chadd /*
5856eb6f0de0SAdrian Chadd  * TX addba handling
5857eb6f0de0SAdrian Chadd  */
5858eb6f0de0SAdrian Chadd 
5859eb6f0de0SAdrian Chadd /*
5860eb6f0de0SAdrian Chadd  * Return net80211 TID struct pointer, or NULL for none
5861eb6f0de0SAdrian Chadd  */
5862eb6f0de0SAdrian Chadd struct ieee80211_tx_ampdu *
5863eb6f0de0SAdrian Chadd ath_tx_get_tx_tid(struct ath_node *an, int tid)
5864eb6f0de0SAdrian Chadd {
5865eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = &an->an_node;
5866eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
5867eb6f0de0SAdrian Chadd 
5868eb6f0de0SAdrian Chadd 	if (tid == IEEE80211_NONQOS_TID)
5869eb6f0de0SAdrian Chadd 		return NULL;
5870eb6f0de0SAdrian Chadd 
58712aa563dfSAdrian Chadd 	tap = &ni->ni_tx_ampdu[tid];
5872eb6f0de0SAdrian Chadd 	return tap;
5873eb6f0de0SAdrian Chadd }
5874eb6f0de0SAdrian Chadd 
5875eb6f0de0SAdrian Chadd /*
5876eb6f0de0SAdrian Chadd  * Is AMPDU-TX running?
5877eb6f0de0SAdrian Chadd  */
5878eb6f0de0SAdrian Chadd static int
5879eb6f0de0SAdrian Chadd ath_tx_ampdu_running(struct ath_softc *sc, struct ath_node *an, int tid)
5880eb6f0de0SAdrian Chadd {
5881eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
5882eb6f0de0SAdrian Chadd 
5883eb6f0de0SAdrian Chadd 	if (tid == IEEE80211_NONQOS_TID)
5884eb6f0de0SAdrian Chadd 		return 0;
5885eb6f0de0SAdrian Chadd 
5886eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
5887eb6f0de0SAdrian Chadd 	if (tap == NULL)
5888eb6f0de0SAdrian Chadd 		return 0;	/* Not valid; default to not running */
5889eb6f0de0SAdrian Chadd 
5890eb6f0de0SAdrian Chadd 	return !! (tap->txa_flags & IEEE80211_AGGR_RUNNING);
5891eb6f0de0SAdrian Chadd }
5892eb6f0de0SAdrian Chadd 
5893eb6f0de0SAdrian Chadd /*
5894eb6f0de0SAdrian Chadd  * Is AMPDU-TX negotiation pending?
5895eb6f0de0SAdrian Chadd  */
5896eb6f0de0SAdrian Chadd static int
5897eb6f0de0SAdrian Chadd ath_tx_ampdu_pending(struct ath_softc *sc, struct ath_node *an, int tid)
5898eb6f0de0SAdrian Chadd {
5899eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
5900eb6f0de0SAdrian Chadd 
5901eb6f0de0SAdrian Chadd 	if (tid == IEEE80211_NONQOS_TID)
5902eb6f0de0SAdrian Chadd 		return 0;
5903eb6f0de0SAdrian Chadd 
5904eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
5905eb6f0de0SAdrian Chadd 	if (tap == NULL)
5906eb6f0de0SAdrian Chadd 		return 0;	/* Not valid; default to not pending */
5907eb6f0de0SAdrian Chadd 
5908eb6f0de0SAdrian Chadd 	return !! (tap->txa_flags & IEEE80211_AGGR_XCHGPEND);
5909eb6f0de0SAdrian Chadd }
5910eb6f0de0SAdrian Chadd 
5911eb6f0de0SAdrian Chadd /*
5912eb6f0de0SAdrian Chadd  * Is AMPDU-TX pending for the given TID?
5913eb6f0de0SAdrian Chadd  */
5914eb6f0de0SAdrian Chadd 
5915eb6f0de0SAdrian Chadd 
5916eb6f0de0SAdrian Chadd /*
5917eb6f0de0SAdrian Chadd  * Method to handle sending an ADDBA request.
5918eb6f0de0SAdrian Chadd  *
5919eb6f0de0SAdrian Chadd  * We tap this so the relevant flags can be set to pause the TID
5920eb6f0de0SAdrian Chadd  * whilst waiting for the response.
5921eb6f0de0SAdrian Chadd  *
5922eb6f0de0SAdrian Chadd  * XXX there's no timeout handler we can override?
5923eb6f0de0SAdrian Chadd  */
5924eb6f0de0SAdrian Chadd int
5925eb6f0de0SAdrian Chadd ath_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
5926eb6f0de0SAdrian Chadd     int dialogtoken, int baparamset, int batimeout)
5927eb6f0de0SAdrian Chadd {
59283797bf08SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_softc;
59292aa563dfSAdrian Chadd 	int tid = tap->txa_tid;
5930eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
5931eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
5932eb6f0de0SAdrian Chadd 
5933eb6f0de0SAdrian Chadd 	/*
5934eb6f0de0SAdrian Chadd 	 * XXX danger Will Robinson!
5935eb6f0de0SAdrian Chadd 	 *
5936eb6f0de0SAdrian Chadd 	 * Although the taskqueue may be running and scheduling some more
5937eb6f0de0SAdrian Chadd 	 * packets, these should all be _before_ the addba sequence number.
5938eb6f0de0SAdrian Chadd 	 * However, net80211 will keep self-assigning sequence numbers
5939eb6f0de0SAdrian Chadd 	 * until addba has been negotiated.
5940eb6f0de0SAdrian Chadd 	 *
5941eb6f0de0SAdrian Chadd 	 * In the past, these packets would be "paused" (which still works
5942eb6f0de0SAdrian Chadd 	 * fine, as they're being scheduled to the driver in the same
5943eb6f0de0SAdrian Chadd 	 * serialised method which is calling the addba request routine)
5944eb6f0de0SAdrian Chadd 	 * and when the aggregation session begins, they'll be dequeued
5945eb6f0de0SAdrian Chadd 	 * as aggregate packets and added to the BAW. However, now there's
5946eb6f0de0SAdrian Chadd 	 * a "bf->bf_state.bfs_dobaw" flag, and this isn't set for these
5947eb6f0de0SAdrian Chadd 	 * packets. Thus they never get included in the BAW tracking and
5948eb6f0de0SAdrian Chadd 	 * this can cause the initial burst of packets after the addba
5949eb6f0de0SAdrian Chadd 	 * negotiation to "hang", as they quickly fall outside the BAW.
5950eb6f0de0SAdrian Chadd 	 *
5951eb6f0de0SAdrian Chadd 	 * The "eventual" solution should be to tag these packets with
5952eb6f0de0SAdrian Chadd 	 * dobaw. Although net80211 has given us a sequence number,
5953eb6f0de0SAdrian Chadd 	 * it'll be "after" the left edge of the BAW and thus it'll
5954eb6f0de0SAdrian Chadd 	 * fall within it.
5955eb6f0de0SAdrian Chadd 	 */
5956375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
5957d3a6425bSAdrian Chadd 	/*
5958d3a6425bSAdrian Chadd 	 * This is a bit annoying.  Until net80211 HT code inherits some
5959d3a6425bSAdrian Chadd 	 * (any) locking, we may have this called in parallel BUT only
5960d3a6425bSAdrian Chadd 	 * one response/timeout will be called.  Grr.
5961d3a6425bSAdrian Chadd 	 */
5962d3a6425bSAdrian Chadd 	if (atid->addba_tx_pending == 0) {
5963eb6f0de0SAdrian Chadd 		ath_tx_tid_pause(sc, atid);
5964d3a6425bSAdrian Chadd 		atid->addba_tx_pending = 1;
5965d3a6425bSAdrian Chadd 	}
5966375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
5967eb6f0de0SAdrian Chadd 
5968eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
59699b48fb4bSAdrian Chadd 	    "%s: %6D: called; dialogtoken=%d, baparamset=%d, batimeout=%d\n",
59709b48fb4bSAdrian Chadd 	    __func__,
59719b48fb4bSAdrian Chadd 	    ni->ni_macaddr,
59729b48fb4bSAdrian Chadd 	    ":",
59739b48fb4bSAdrian Chadd 	    dialogtoken, baparamset, batimeout);
5974eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5975eb6f0de0SAdrian Chadd 	    "%s: txa_start=%d, ni_txseqs=%d\n",
5976eb6f0de0SAdrian Chadd 	    __func__, tap->txa_start, ni->ni_txseqs[tid]);
5977eb6f0de0SAdrian Chadd 
5978eb6f0de0SAdrian Chadd 	return sc->sc_addba_request(ni, tap, dialogtoken, baparamset,
5979eb6f0de0SAdrian Chadd 	    batimeout);
5980eb6f0de0SAdrian Chadd }
5981eb6f0de0SAdrian Chadd 
5982eb6f0de0SAdrian Chadd /*
5983eb6f0de0SAdrian Chadd  * Handle an ADDBA response.
5984eb6f0de0SAdrian Chadd  *
5985eb6f0de0SAdrian Chadd  * We unpause the queue so TX'ing can resume.
5986eb6f0de0SAdrian Chadd  *
5987eb6f0de0SAdrian Chadd  * Any packets TX'ed from this point should be "aggregate" (whether
5988eb6f0de0SAdrian Chadd  * aggregate or not) so the BAW is updated.
5989eb6f0de0SAdrian Chadd  *
5990eb6f0de0SAdrian Chadd  * Note! net80211 keeps self-assigning sequence numbers until
5991eb6f0de0SAdrian Chadd  * ampdu is negotiated. This means the initially-negotiated BAW left
5992eb6f0de0SAdrian Chadd  * edge won't match the ni->ni_txseq.
5993eb6f0de0SAdrian Chadd  *
5994eb6f0de0SAdrian Chadd  * So, being very dirty, the BAW left edge is "slid" here to match
5995eb6f0de0SAdrian Chadd  * ni->ni_txseq.
5996eb6f0de0SAdrian Chadd  *
5997eb6f0de0SAdrian Chadd  * What likely SHOULD happen is that all packets subsequent to the
5998eb6f0de0SAdrian Chadd  * addba request should be tagged as aggregate and queued as non-aggregate
5999eb6f0de0SAdrian Chadd  * frames; thus updating the BAW. For now though, I'll just slide the
6000eb6f0de0SAdrian Chadd  * window.
6001eb6f0de0SAdrian Chadd  */
6002eb6f0de0SAdrian Chadd int
6003eb6f0de0SAdrian Chadd ath_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
6004eb6f0de0SAdrian Chadd     int status, int code, int batimeout)
6005eb6f0de0SAdrian Chadd {
60063797bf08SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_softc;
60072aa563dfSAdrian Chadd 	int tid = tap->txa_tid;
6008eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
6009eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
6010eb6f0de0SAdrian Chadd 	int r;
6011eb6f0de0SAdrian Chadd 
6012eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
60139b48fb4bSAdrian Chadd 	    "%s: %6D: called; status=%d, code=%d, batimeout=%d\n", __func__,
60149b48fb4bSAdrian Chadd 	    ni->ni_macaddr,
60159b48fb4bSAdrian Chadd 	    ":",
6016eb6f0de0SAdrian Chadd 	    status, code, batimeout);
6017eb6f0de0SAdrian Chadd 
6018eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
6019eb6f0de0SAdrian Chadd 	    "%s: txa_start=%d, ni_txseqs=%d\n",
6020eb6f0de0SAdrian Chadd 	    __func__, tap->txa_start, ni->ni_txseqs[tid]);
6021eb6f0de0SAdrian Chadd 
6022eb6f0de0SAdrian Chadd 	/*
6023eb6f0de0SAdrian Chadd 	 * Call this first, so the interface flags get updated
6024eb6f0de0SAdrian Chadd 	 * before the TID is unpaused. Otherwise a race condition
6025eb6f0de0SAdrian Chadd 	 * exists where the unpaused TID still doesn't yet have
6026eb6f0de0SAdrian Chadd 	 * IEEE80211_AGGR_RUNNING set.
6027eb6f0de0SAdrian Chadd 	 */
6028eb6f0de0SAdrian Chadd 	r = sc->sc_addba_response(ni, tap, status, code, batimeout);
6029eb6f0de0SAdrian Chadd 
6030375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
6031d3a6425bSAdrian Chadd 	atid->addba_tx_pending = 0;
6032eb6f0de0SAdrian Chadd 	/*
6033eb6f0de0SAdrian Chadd 	 * XXX dirty!
6034eb6f0de0SAdrian Chadd 	 * Slide the BAW left edge to wherever net80211 left it for us.
6035eb6f0de0SAdrian Chadd 	 * Read above for more information.
6036eb6f0de0SAdrian Chadd 	 */
6037eb6f0de0SAdrian Chadd 	tap->txa_start = ni->ni_txseqs[tid];
6038eb6f0de0SAdrian Chadd 	ath_tx_tid_resume(sc, atid);
6039375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
6040eb6f0de0SAdrian Chadd 	return r;
6041eb6f0de0SAdrian Chadd }
6042eb6f0de0SAdrian Chadd 
6043eb6f0de0SAdrian Chadd 
6044eb6f0de0SAdrian Chadd /*
6045eb6f0de0SAdrian Chadd  * Stop ADDBA on a queue.
60468405fe86SAdrian Chadd  *
60478405fe86SAdrian Chadd  * This can be called whilst BAR TX is currently active on the queue,
60488405fe86SAdrian Chadd  * so make sure this is unblocked before continuing.
6049eb6f0de0SAdrian Chadd  */
6050eb6f0de0SAdrian Chadd void
6051eb6f0de0SAdrian Chadd ath_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
6052eb6f0de0SAdrian Chadd {
60533797bf08SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_softc;
60542aa563dfSAdrian Chadd 	int tid = tap->txa_tid;
6055eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
6056eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
605722780332SAdrian Chadd 	ath_bufhead bf_cq;
605822780332SAdrian Chadd 	struct ath_buf *bf;
6059eb6f0de0SAdrian Chadd 
60609b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: %6D: called\n",
60619b48fb4bSAdrian Chadd 	    __func__,
60629b48fb4bSAdrian Chadd 	    ni->ni_macaddr,
60639b48fb4bSAdrian Chadd 	    ":");
6064eb6f0de0SAdrian Chadd 
60658405fe86SAdrian Chadd 	/*
60668405fe86SAdrian Chadd 	 * Pause TID traffic early, so there aren't any races
60678405fe86SAdrian Chadd 	 * Unblock the pending BAR held traffic, if it's currently paused.
60688405fe86SAdrian Chadd 	 */
6069375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
6070eb6f0de0SAdrian Chadd 	ath_tx_tid_pause(sc, atid);
60718405fe86SAdrian Chadd 	if (atid->bar_wait) {
60728405fe86SAdrian Chadd 		/*
60738405fe86SAdrian Chadd 		 * bar_unsuspend() expects bar_tx == 1, as it should be
60748405fe86SAdrian Chadd 		 * called from the TX completion path.  This quietens
60758405fe86SAdrian Chadd 		 * the warning.  It's cleared for us anyway.
60768405fe86SAdrian Chadd 		 */
60778405fe86SAdrian Chadd 		atid->bar_tx = 1;
60788405fe86SAdrian Chadd 		ath_tx_tid_bar_unsuspend(sc, atid);
60798405fe86SAdrian Chadd 	}
6080375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
6081eb6f0de0SAdrian Chadd 
6082eb6f0de0SAdrian Chadd 	/* There's no need to hold the TXQ lock here */
6083eb6f0de0SAdrian Chadd 	sc->sc_addba_stop(ni, tap);
6084eb6f0de0SAdrian Chadd 
6085eb6f0de0SAdrian Chadd 	/*
60864dfd4507SAdrian Chadd 	 * ath_tx_tid_cleanup will resume the TID if possible, otherwise
6087eb6f0de0SAdrian Chadd 	 * it'll set the cleanup flag, and it'll be unpaused once
6088eb6f0de0SAdrian Chadd 	 * things have been cleaned up.
6089eb6f0de0SAdrian Chadd 	 */
609022780332SAdrian Chadd 	TAILQ_INIT(&bf_cq);
609122780332SAdrian Chadd 	ATH_TX_LOCK(sc);
609259fbb530SAdrian Chadd 
609359fbb530SAdrian Chadd 	/*
609459fbb530SAdrian Chadd 	 * In case there's a followup call to this, only call it
609559fbb530SAdrian Chadd 	 * if we don't have a cleanup in progress.
609659fbb530SAdrian Chadd 	 *
609759fbb530SAdrian Chadd 	 * Since we've paused the queue above, we need to make
609859fbb530SAdrian Chadd 	 * sure we unpause if there's already a cleanup in
609959fbb530SAdrian Chadd 	 * progress - it means something else is also doing
610059fbb530SAdrian Chadd 	 * this stuff, so we don't need to also keep it paused.
610159fbb530SAdrian Chadd 	 */
610259fbb530SAdrian Chadd 	if (atid->cleanup_inprogress) {
610359fbb530SAdrian Chadd 		ath_tx_tid_resume(sc, atid);
610459fbb530SAdrian Chadd 	} else {
610522780332SAdrian Chadd 		ath_tx_tid_cleanup(sc, an, tid, &bf_cq);
61065da3fc10SAdrian Chadd 		/*
61075da3fc10SAdrian Chadd 		 * Unpause the TID if no cleanup is required.
61085da3fc10SAdrian Chadd 		 */
61095da3fc10SAdrian Chadd 		if (! atid->cleanup_inprogress)
61105da3fc10SAdrian Chadd 			ath_tx_tid_resume(sc, atid);
611159fbb530SAdrian Chadd 	}
611222780332SAdrian Chadd 	ATH_TX_UNLOCK(sc);
611322780332SAdrian Chadd 
611422780332SAdrian Chadd 	/* Handle completing frames and fail them */
611522780332SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
611622780332SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
611722780332SAdrian Chadd 		ath_tx_default_comp(sc, bf, 1);
611822780332SAdrian Chadd 	}
611922a3aee6SAdrian Chadd 
612022780332SAdrian Chadd }
612122780332SAdrian Chadd 
612222780332SAdrian Chadd /*
612322780332SAdrian Chadd  * Handle a node reassociation.
612422780332SAdrian Chadd  *
612522780332SAdrian Chadd  * We may have a bunch of frames queued to the hardware; those need
612622780332SAdrian Chadd  * to be marked as cleanup.
612722780332SAdrian Chadd  */
612822780332SAdrian Chadd void
612922780332SAdrian Chadd ath_tx_node_reassoc(struct ath_softc *sc, struct ath_node *an)
613022780332SAdrian Chadd {
613122780332SAdrian Chadd 	struct ath_tid *tid;
613222780332SAdrian Chadd 	int i;
613322780332SAdrian Chadd 	ath_bufhead bf_cq;
613422780332SAdrian Chadd 	struct ath_buf *bf;
613522780332SAdrian Chadd 
613622780332SAdrian Chadd 	TAILQ_INIT(&bf_cq);
613722780332SAdrian Chadd 
613822780332SAdrian Chadd 	ATH_TX_UNLOCK_ASSERT(sc);
613922780332SAdrian Chadd 
614022780332SAdrian Chadd 	ATH_TX_LOCK(sc);
614122780332SAdrian Chadd 	for (i = 0; i < IEEE80211_TID_SIZE; i++) {
614222780332SAdrian Chadd 		tid = &an->an_tid[i];
614322780332SAdrian Chadd 		if (tid->hwq_depth == 0)
614422780332SAdrian Chadd 			continue;
614522780332SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE,
614622780332SAdrian Chadd 		    "%s: %6D: TID %d: cleaning up TID\n",
614722780332SAdrian Chadd 		    __func__,
614822780332SAdrian Chadd 		    an->an_node.ni_macaddr,
614922780332SAdrian Chadd 		    ":",
615022780332SAdrian Chadd 		    i);
615159fbb530SAdrian Chadd 		/*
615259fbb530SAdrian Chadd 		 * In case there's a followup call to this, only call it
615359fbb530SAdrian Chadd 		 * if we don't have a cleanup in progress.
615459fbb530SAdrian Chadd 		 */
615559fbb530SAdrian Chadd 		if (! tid->cleanup_inprogress) {
615659fbb530SAdrian Chadd 			ath_tx_tid_pause(sc, tid);
615722780332SAdrian Chadd 			ath_tx_tid_cleanup(sc, an, i, &bf_cq);
61585da3fc10SAdrian Chadd 			/*
61595da3fc10SAdrian Chadd 			 * Unpause the TID if no cleanup is required.
61605da3fc10SAdrian Chadd 			 */
61615da3fc10SAdrian Chadd 			if (! tid->cleanup_inprogress)
61625da3fc10SAdrian Chadd 				ath_tx_tid_resume(sc, tid);
616322780332SAdrian Chadd 		}
616459fbb530SAdrian Chadd 	}
616522780332SAdrian Chadd 	ATH_TX_UNLOCK(sc);
616622780332SAdrian Chadd 
616722780332SAdrian Chadd 	/* Handle completing frames and fail them */
616822780332SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
616922780332SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
617022780332SAdrian Chadd 		ath_tx_default_comp(sc, bf, 1);
617122780332SAdrian Chadd 	}
6172eb6f0de0SAdrian Chadd }
6173eb6f0de0SAdrian Chadd 
6174eb6f0de0SAdrian Chadd /*
6175eb6f0de0SAdrian Chadd  * Note: net80211 bar_timeout() doesn't call this function on BAR failure;
6176eb6f0de0SAdrian Chadd  * it simply tears down the aggregation session. Ew.
6177eb6f0de0SAdrian Chadd  *
6178eb6f0de0SAdrian Chadd  * It however will call ieee80211_ampdu_stop() which will call
6179eb6f0de0SAdrian Chadd  * ic->ic_addba_stop().
6180eb6f0de0SAdrian Chadd  *
6181eb6f0de0SAdrian Chadd  * XXX This uses a hard-coded max BAR count value; the whole
6182eb6f0de0SAdrian Chadd  * XXX BAR TX success or failure should be better handled!
6183eb6f0de0SAdrian Chadd  */
6184eb6f0de0SAdrian Chadd void
6185eb6f0de0SAdrian Chadd ath_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
6186eb6f0de0SAdrian Chadd     int status)
6187eb6f0de0SAdrian Chadd {
61883797bf08SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_softc;
61892aa563dfSAdrian Chadd 	int tid = tap->txa_tid;
6190eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
6191eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
6192eb6f0de0SAdrian Chadd 	int attempts = tap->txa_attempts;
619342fdd8e7SAdrian Chadd 	int old_txa_start;
6194eb6f0de0SAdrian Chadd 
61950e22ed0eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
619642fdd8e7SAdrian Chadd 	    "%s: %6D: called; txa_tid=%d, atid->tid=%d, status=%d, attempts=%d, txa_start=%d, txa_seqpending=%d\n",
61970e22ed0eSAdrian Chadd 	    __func__,
61989b48fb4bSAdrian Chadd 	    ni->ni_macaddr,
61999b48fb4bSAdrian Chadd 	    ":",
6200e60c4fc2SAdrian Chadd 	    tap->txa_tid,
6201e60c4fc2SAdrian Chadd 	    atid->tid,
62020e22ed0eSAdrian Chadd 	    status,
620342fdd8e7SAdrian Chadd 	    attempts,
620442fdd8e7SAdrian Chadd 	    tap->txa_start,
620542fdd8e7SAdrian Chadd 	    tap->txa_seqpending);
6206eb6f0de0SAdrian Chadd 
6207eb6f0de0SAdrian Chadd 	/* Note: This may update the BAW details */
620842fdd8e7SAdrian Chadd 	/*
620942fdd8e7SAdrian Chadd 	 * XXX What if this does slide the BAW along? We need to somehow
621042fdd8e7SAdrian Chadd 	 * XXX either fix things when it does happen, or prevent the
621142fdd8e7SAdrian Chadd 	 * XXX seqpending value to be anything other than exactly what
621242fdd8e7SAdrian Chadd 	 * XXX the hell we want!
621342fdd8e7SAdrian Chadd 	 *
621442fdd8e7SAdrian Chadd 	 * XXX So for now, how I do this inside the TX lock for now
621542fdd8e7SAdrian Chadd 	 * XXX and just correct it afterwards? The below condition should
621642fdd8e7SAdrian Chadd 	 * XXX never happen and if it does I need to fix all kinds of things.
621742fdd8e7SAdrian Chadd 	 */
621842fdd8e7SAdrian Chadd 	ATH_TX_LOCK(sc);
621942fdd8e7SAdrian Chadd 	old_txa_start = tap->txa_start;
6220eb6f0de0SAdrian Chadd 	sc->sc_bar_response(ni, tap, status);
622142fdd8e7SAdrian Chadd 	if (tap->txa_start != old_txa_start) {
622242fdd8e7SAdrian Chadd 		device_printf(sc->sc_dev, "%s: tid=%d; txa_start=%d, old=%d, adjusting\n",
622342fdd8e7SAdrian Chadd 		    __func__,
622442fdd8e7SAdrian Chadd 		    tid,
622542fdd8e7SAdrian Chadd 		    tap->txa_start,
622642fdd8e7SAdrian Chadd 		    old_txa_start);
622742fdd8e7SAdrian Chadd 	}
622842fdd8e7SAdrian Chadd 	tap->txa_start = old_txa_start;
622942fdd8e7SAdrian Chadd 	ATH_TX_UNLOCK(sc);
6230eb6f0de0SAdrian Chadd 
6231eb6f0de0SAdrian Chadd 	/* Unpause the TID */
6232eb6f0de0SAdrian Chadd 	/*
6233eb6f0de0SAdrian Chadd 	 * XXX if this is attempt=50, the TID will be downgraded
6234eb6f0de0SAdrian Chadd 	 * XXX to a non-aggregate session. So we must unpause the
6235eb6f0de0SAdrian Chadd 	 * XXX TID here or it'll never be done.
6236088d8b81SAdrian Chadd 	 *
6237088d8b81SAdrian Chadd 	 * Also, don't call it if bar_tx/bar_wait are 0; something
6238088d8b81SAdrian Chadd 	 * has beaten us to the punch? (XXX figure out what?)
6239eb6f0de0SAdrian Chadd 	 */
6240eb6f0de0SAdrian Chadd 	if (status == 0 || attempts == 50) {
6241375307d4SAdrian Chadd 		ATH_TX_LOCK(sc);
6242088d8b81SAdrian Chadd 		if (atid->bar_tx == 0 || atid->bar_wait == 0)
624383bbd5ebSRui Paulo 			DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
6244088d8b81SAdrian Chadd 			    "%s: huh? bar_tx=%d, bar_wait=%d\n",
6245088d8b81SAdrian Chadd 			    __func__,
6246088d8b81SAdrian Chadd 			    atid->bar_tx, atid->bar_wait);
6247088d8b81SAdrian Chadd 		else
624888b3d483SAdrian Chadd 			ath_tx_tid_bar_unsuspend(sc, atid);
6249375307d4SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6250eb6f0de0SAdrian Chadd 	}
6251eb6f0de0SAdrian Chadd }
6252eb6f0de0SAdrian Chadd 
6253eb6f0de0SAdrian Chadd /*
6254eb6f0de0SAdrian Chadd  * This is called whenever the pending ADDBA request times out.
6255eb6f0de0SAdrian Chadd  * Unpause and reschedule the TID.
6256eb6f0de0SAdrian Chadd  */
6257eb6f0de0SAdrian Chadd void
6258eb6f0de0SAdrian Chadd ath_addba_response_timeout(struct ieee80211_node *ni,
6259eb6f0de0SAdrian Chadd     struct ieee80211_tx_ampdu *tap)
6260eb6f0de0SAdrian Chadd {
62613797bf08SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_softc;
62622aa563dfSAdrian Chadd 	int tid = tap->txa_tid;
6263eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
6264eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
6265eb6f0de0SAdrian Chadd 
6266eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
62676d07d3e0SAdrian Chadd 	    "%s: %6D: TID=%d, called; resuming\n",
62689b48fb4bSAdrian Chadd 	    __func__,
62699b48fb4bSAdrian Chadd 	    ni->ni_macaddr,
62706d07d3e0SAdrian Chadd 	    ":",
62716d07d3e0SAdrian Chadd 	    tid);
6272eb6f0de0SAdrian Chadd 
6273375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
6274d3a6425bSAdrian Chadd 	atid->addba_tx_pending = 0;
6275375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
6276d3a6425bSAdrian Chadd 
6277eb6f0de0SAdrian Chadd 	/* Note: This updates the aggregate state to (again) pending */
6278eb6f0de0SAdrian Chadd 	sc->sc_addba_response_timeout(ni, tap);
6279eb6f0de0SAdrian Chadd 
6280eb6f0de0SAdrian Chadd 	/* Unpause the TID; which reschedules it */
6281375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
6282eb6f0de0SAdrian Chadd 	ath_tx_tid_resume(sc, atid);
6283375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
6284eb6f0de0SAdrian Chadd }
62853fdfc330SAdrian Chadd 
62860eb81626SAdrian Chadd /*
62870eb81626SAdrian Chadd  * Check if a node is asleep or not.
62880eb81626SAdrian Chadd  */
6289548a605dSAdrian Chadd int
62900eb81626SAdrian Chadd ath_tx_node_is_asleep(struct ath_softc *sc, struct ath_node *an)
62910eb81626SAdrian Chadd {
62920eb81626SAdrian Chadd 
629322780332SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
62940eb81626SAdrian Chadd 
62950eb81626SAdrian Chadd 	return (an->an_is_powersave);
62960eb81626SAdrian Chadd }
62970eb81626SAdrian Chadd 
62980eb81626SAdrian Chadd /*
62990eb81626SAdrian Chadd  * Mark a node as currently "in powersaving."
63000eb81626SAdrian Chadd  * This suspends all traffic on the node.
63010eb81626SAdrian Chadd  *
63020eb81626SAdrian Chadd  * This must be called with the node/tx locks free.
63030eb81626SAdrian Chadd  *
63040eb81626SAdrian Chadd  * XXX TODO: the locking silliness below is due to how the node
63050eb81626SAdrian Chadd  * locking currently works.  Right now, the node lock is grabbed
63060eb81626SAdrian Chadd  * to do rate control lookups and these are done with the TX
63070eb81626SAdrian Chadd  * queue lock held.  This means the node lock can't be grabbed
63080eb81626SAdrian Chadd  * first here or a LOR will occur.
63090eb81626SAdrian Chadd  *
63100eb81626SAdrian Chadd  * Eventually (hopefully!) the TX path code will only grab
63110eb81626SAdrian Chadd  * the TXQ lock when transmitting and the ath_node lock when
63120eb81626SAdrian Chadd  * doing node/TID operations.  There are other complications -
63130eb81626SAdrian Chadd  * the sched/unsched operations involve walking the per-txq
63140eb81626SAdrian Chadd  * 'active tid' list and this requires both locks to be held.
63150eb81626SAdrian Chadd  */
63160eb81626SAdrian Chadd void
63170eb81626SAdrian Chadd ath_tx_node_sleep(struct ath_softc *sc, struct ath_node *an)
63180eb81626SAdrian Chadd {
63190eb81626SAdrian Chadd 	struct ath_tid *atid;
63200eb81626SAdrian Chadd 	struct ath_txq *txq;
63210eb81626SAdrian Chadd 	int tid;
63220eb81626SAdrian Chadd 
632322780332SAdrian Chadd 	ATH_TX_UNLOCK_ASSERT(sc);
63240eb81626SAdrian Chadd 
63250eb81626SAdrian Chadd 	/* Suspend all traffic on the node */
6326375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
632722a3aee6SAdrian Chadd 
632822a3aee6SAdrian Chadd 	if (an->an_is_powersave) {
632983bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_XMIT,
633022a3aee6SAdrian Chadd 		    "%s: %6D: node was already asleep!\n",
633183bbd5ebSRui Paulo 		    __func__, an->an_node.ni_macaddr, ":");
633222a3aee6SAdrian Chadd 		ATH_TX_UNLOCK(sc);
633322a3aee6SAdrian Chadd 		return;
633422a3aee6SAdrian Chadd 	}
633522a3aee6SAdrian Chadd 
63360eb81626SAdrian Chadd 	for (tid = 0; tid < IEEE80211_TID_SIZE; tid++) {
63370eb81626SAdrian Chadd 		atid = &an->an_tid[tid];
63380eb81626SAdrian Chadd 		txq = sc->sc_ac2q[atid->ac];
63390eb81626SAdrian Chadd 
63400eb81626SAdrian Chadd 		ath_tx_tid_pause(sc, atid);
63410eb81626SAdrian Chadd 	}
63420eb81626SAdrian Chadd 
63430eb81626SAdrian Chadd 	/* Mark node as in powersaving */
63440eb81626SAdrian Chadd 	an->an_is_powersave = 1;
63450eb81626SAdrian Chadd 
634622780332SAdrian Chadd 	ATH_TX_UNLOCK(sc);
63470eb81626SAdrian Chadd }
63480eb81626SAdrian Chadd 
63490eb81626SAdrian Chadd /*
63500eb81626SAdrian Chadd  * Mark a node as currently "awake."
63510eb81626SAdrian Chadd  * This resumes all traffic to the node.
63520eb81626SAdrian Chadd  */
63530eb81626SAdrian Chadd void
63540eb81626SAdrian Chadd ath_tx_node_wakeup(struct ath_softc *sc, struct ath_node *an)
63550eb81626SAdrian Chadd {
63560eb81626SAdrian Chadd 	struct ath_tid *atid;
63570eb81626SAdrian Chadd 	struct ath_txq *txq;
63580eb81626SAdrian Chadd 	int tid;
63590eb81626SAdrian Chadd 
636022780332SAdrian Chadd 	ATH_TX_UNLOCK_ASSERT(sc);
636122780332SAdrian Chadd 
636222780332SAdrian Chadd 	ATH_TX_LOCK(sc);
63630eb81626SAdrian Chadd 
636422a3aee6SAdrian Chadd 	/* !? */
63650eb81626SAdrian Chadd 	if (an->an_is_powersave == 0) {
636622780332SAdrian Chadd 		ATH_TX_UNLOCK(sc);
636783bbd5ebSRui Paulo 		DPRINTF(sc, ATH_DEBUG_XMIT,
63680eb81626SAdrian Chadd 		    "%s: an=%p: node was already awake\n",
63690eb81626SAdrian Chadd 		    __func__, an);
63700eb81626SAdrian Chadd 		return;
63710eb81626SAdrian Chadd 	}
63720eb81626SAdrian Chadd 
63730eb81626SAdrian Chadd 	/* Mark node as awake */
63740eb81626SAdrian Chadd 	an->an_is_powersave = 0;
637522a3aee6SAdrian Chadd 	/*
637622a3aee6SAdrian Chadd 	 * Clear any pending leaked frame requests
637722a3aee6SAdrian Chadd 	 */
637822a3aee6SAdrian Chadd 	an->an_leak_count = 0;
63790eb81626SAdrian Chadd 
63800eb81626SAdrian Chadd 	for (tid = 0; tid < IEEE80211_TID_SIZE; tid++) {
63810eb81626SAdrian Chadd 		atid = &an->an_tid[tid];
63820eb81626SAdrian Chadd 		txq = sc->sc_ac2q[atid->ac];
63830eb81626SAdrian Chadd 
63840eb81626SAdrian Chadd 		ath_tx_tid_resume(sc, atid);
63850eb81626SAdrian Chadd 	}
6386375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
63870eb81626SAdrian Chadd }
63880eb81626SAdrian Chadd 
63893fdfc330SAdrian Chadd static int
63903fdfc330SAdrian Chadd ath_legacy_dma_txsetup(struct ath_softc *sc)
63913fdfc330SAdrian Chadd {
63923fdfc330SAdrian Chadd 
63933fdfc330SAdrian Chadd 	/* nothing new needed */
63943fdfc330SAdrian Chadd 	return (0);
63953fdfc330SAdrian Chadd }
63963fdfc330SAdrian Chadd 
63973fdfc330SAdrian Chadd static int
63983fdfc330SAdrian Chadd ath_legacy_dma_txteardown(struct ath_softc *sc)
63993fdfc330SAdrian Chadd {
64003fdfc330SAdrian Chadd 
64013fdfc330SAdrian Chadd 	/* nothing new needed */
64023fdfc330SAdrian Chadd 	return (0);
64033fdfc330SAdrian Chadd }
64043fdfc330SAdrian Chadd 
64053fdfc330SAdrian Chadd void
64063fdfc330SAdrian Chadd ath_xmit_setup_legacy(struct ath_softc *sc)
64073fdfc330SAdrian Chadd {
64081006fc0cSAdrian Chadd 	/*
64091006fc0cSAdrian Chadd 	 * For now, just set the descriptor length to sizeof(ath_desc);
64101006fc0cSAdrian Chadd 	 * worry about extracting the real length out of the HAL later.
64111006fc0cSAdrian Chadd 	 */
64121006fc0cSAdrian Chadd 	sc->sc_tx_desclen = sizeof(struct ath_desc);
6413bb327d28SAdrian Chadd 	sc->sc_tx_statuslen = sizeof(struct ath_desc);
64141006fc0cSAdrian Chadd 	sc->sc_tx_nmaps = 1;	/* only one buffer per TX desc */
64153fdfc330SAdrian Chadd 
64163fdfc330SAdrian Chadd 	sc->sc_tx.xmit_setup = ath_legacy_dma_txsetup;
64173fdfc330SAdrian Chadd 	sc->sc_tx.xmit_teardown = ath_legacy_dma_txteardown;
6418f8418db5SAdrian Chadd 	sc->sc_tx.xmit_attach_comp_func = ath_legacy_attach_comp_func;
6419746bab5bSAdrian Chadd 
6420746bab5bSAdrian Chadd 	sc->sc_tx.xmit_dma_restart = ath_legacy_tx_dma_restart;
6421746bab5bSAdrian Chadd 	sc->sc_tx.xmit_handoff = ath_legacy_xmit_handoff;
6422788e6aa9SAdrian Chadd 
6423788e6aa9SAdrian Chadd 	sc->sc_tx.xmit_drain = ath_legacy_tx_drain;
64243fdfc330SAdrian Chadd }
6425