xref: /freebsd/sys/dev/ath/if_ath_tx.c (revision 0c54de88e69122b4d325ff16a98c7324513b5221)
1b8e788a5SAdrian Chadd /*-
2b8e788a5SAdrian Chadd  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3c6e9cee2SAdrian Chadd  * Copyright (c) 2010-2012 Adrian Chadd, Xenion Pty Ltd
4b8e788a5SAdrian Chadd  * All rights reserved.
5b8e788a5SAdrian Chadd  *
6b8e788a5SAdrian Chadd  * Redistribution and use in source and binary forms, with or without
7b8e788a5SAdrian Chadd  * modification, are permitted provided that the following conditions
8b8e788a5SAdrian Chadd  * are met:
9b8e788a5SAdrian Chadd  * 1. Redistributions of source code must retain the above copyright
10b8e788a5SAdrian Chadd  *    notice, this list of conditions and the following disclaimer,
11b8e788a5SAdrian Chadd  *    without modification.
12b8e788a5SAdrian Chadd  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13b8e788a5SAdrian Chadd  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
14b8e788a5SAdrian Chadd  *    redistribution must be conditioned upon including a substantially
15b8e788a5SAdrian Chadd  *    similar Disclaimer requirement for further binary redistribution.
16b8e788a5SAdrian Chadd  *
17b8e788a5SAdrian Chadd  * NO WARRANTY
18b8e788a5SAdrian Chadd  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19b8e788a5SAdrian Chadd  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20b8e788a5SAdrian Chadd  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
21b8e788a5SAdrian Chadd  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22b8e788a5SAdrian Chadd  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
23b8e788a5SAdrian Chadd  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24b8e788a5SAdrian Chadd  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25b8e788a5SAdrian Chadd  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
26b8e788a5SAdrian Chadd  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27b8e788a5SAdrian Chadd  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28b8e788a5SAdrian Chadd  * THE POSSIBILITY OF SUCH DAMAGES.
29b8e788a5SAdrian Chadd  */
30b8e788a5SAdrian Chadd 
31b8e788a5SAdrian Chadd #include <sys/cdefs.h>
32b8e788a5SAdrian Chadd __FBSDID("$FreeBSD$");
33b8e788a5SAdrian Chadd 
34b8e788a5SAdrian Chadd /*
35b8e788a5SAdrian Chadd  * Driver for the Atheros Wireless LAN controller.
36b8e788a5SAdrian Chadd  *
37b8e788a5SAdrian Chadd  * This software is derived from work of Atsushi Onoe; his contribution
38b8e788a5SAdrian Chadd  * is greatly appreciated.
39b8e788a5SAdrian Chadd  */
40b8e788a5SAdrian Chadd 
41b8e788a5SAdrian Chadd #include "opt_inet.h"
42b8e788a5SAdrian Chadd #include "opt_ath.h"
43b8e788a5SAdrian Chadd #include "opt_wlan.h"
44b8e788a5SAdrian Chadd 
45b8e788a5SAdrian Chadd #include <sys/param.h>
46b8e788a5SAdrian Chadd #include <sys/systm.h>
47b8e788a5SAdrian Chadd #include <sys/sysctl.h>
48b8e788a5SAdrian Chadd #include <sys/mbuf.h>
49b8e788a5SAdrian Chadd #include <sys/malloc.h>
50b8e788a5SAdrian Chadd #include <sys/lock.h>
51b8e788a5SAdrian Chadd #include <sys/mutex.h>
52b8e788a5SAdrian Chadd #include <sys/kernel.h>
53b8e788a5SAdrian Chadd #include <sys/socket.h>
54b8e788a5SAdrian Chadd #include <sys/sockio.h>
55b8e788a5SAdrian Chadd #include <sys/errno.h>
56b8e788a5SAdrian Chadd #include <sys/callout.h>
57b8e788a5SAdrian Chadd #include <sys/bus.h>
58b8e788a5SAdrian Chadd #include <sys/endian.h>
59b8e788a5SAdrian Chadd #include <sys/kthread.h>
60b8e788a5SAdrian Chadd #include <sys/taskqueue.h>
61b8e788a5SAdrian Chadd #include <sys/priv.h>
62b8e788a5SAdrian Chadd 
63b8e788a5SAdrian Chadd #include <machine/bus.h>
64b8e788a5SAdrian Chadd 
65b8e788a5SAdrian Chadd #include <net/if.h>
66b8e788a5SAdrian Chadd #include <net/if_dl.h>
67b8e788a5SAdrian Chadd #include <net/if_media.h>
68b8e788a5SAdrian Chadd #include <net/if_types.h>
69b8e788a5SAdrian Chadd #include <net/if_arp.h>
70b8e788a5SAdrian Chadd #include <net/ethernet.h>
71b8e788a5SAdrian Chadd #include <net/if_llc.h>
72b8e788a5SAdrian Chadd 
73b8e788a5SAdrian Chadd #include <net80211/ieee80211_var.h>
74b8e788a5SAdrian Chadd #include <net80211/ieee80211_regdomain.h>
75b8e788a5SAdrian Chadd #ifdef IEEE80211_SUPPORT_SUPERG
76b8e788a5SAdrian Chadd #include <net80211/ieee80211_superg.h>
77b8e788a5SAdrian Chadd #endif
78b8e788a5SAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
79b8e788a5SAdrian Chadd #include <net80211/ieee80211_tdma.h>
80b8e788a5SAdrian Chadd #endif
81eb6f0de0SAdrian Chadd #include <net80211/ieee80211_ht.h>
82b8e788a5SAdrian Chadd 
83b8e788a5SAdrian Chadd #include <net/bpf.h>
84b8e788a5SAdrian Chadd 
85b8e788a5SAdrian Chadd #ifdef INET
86b8e788a5SAdrian Chadd #include <netinet/in.h>
87b8e788a5SAdrian Chadd #include <netinet/if_ether.h>
88b8e788a5SAdrian Chadd #endif
89b8e788a5SAdrian Chadd 
90b8e788a5SAdrian Chadd #include <dev/ath/if_athvar.h>
91b8e788a5SAdrian Chadd #include <dev/ath/ath_hal/ah_devid.h>		/* XXX for softled */
92b8e788a5SAdrian Chadd #include <dev/ath/ath_hal/ah_diagcodes.h>
93b8e788a5SAdrian Chadd 
94b8e788a5SAdrian Chadd #include <dev/ath/if_ath_debug.h>
95b8e788a5SAdrian Chadd 
96b8e788a5SAdrian Chadd #ifdef ATH_TX99_DIAG
97b8e788a5SAdrian Chadd #include <dev/ath/ath_tx99/ath_tx99.h>
98b8e788a5SAdrian Chadd #endif
99b8e788a5SAdrian Chadd 
100b8e788a5SAdrian Chadd #include <dev/ath/if_ath_misc.h>
101b8e788a5SAdrian Chadd #include <dev/ath/if_ath_tx.h>
102c1782ce0SAdrian Chadd #include <dev/ath/if_ath_tx_ht.h>
103b8e788a5SAdrian Chadd 
10481a82688SAdrian Chadd /*
105eb6f0de0SAdrian Chadd  * How many retries to perform in software
106eb6f0de0SAdrian Chadd  */
107eb6f0de0SAdrian Chadd #define	SWMAX_RETRIES		10
108eb6f0de0SAdrian Chadd 
109eb6f0de0SAdrian Chadd static int ath_tx_ampdu_pending(struct ath_softc *sc, struct ath_node *an,
110eb6f0de0SAdrian Chadd     int tid);
111eb6f0de0SAdrian Chadd static int ath_tx_ampdu_running(struct ath_softc *sc, struct ath_node *an,
112eb6f0de0SAdrian Chadd     int tid);
113a108d2d6SAdrian Chadd static ieee80211_seq ath_tx_tid_seqno_assign(struct ath_softc *sc,
114a108d2d6SAdrian Chadd     struct ieee80211_node *ni, struct ath_buf *bf, struct mbuf *m0);
115eb6f0de0SAdrian Chadd static int ath_tx_action_frame_override_queue(struct ath_softc *sc,
116eb6f0de0SAdrian Chadd     struct ieee80211_node *ni, struct mbuf *m0, int *tid);
117f1bc738eSAdrian Chadd static struct ath_buf *
118f1bc738eSAdrian Chadd ath_tx_retry_clone(struct ath_softc *sc, struct ath_node *an,
119f1bc738eSAdrian Chadd     struct ath_tid *tid, struct ath_buf *bf);
120eb6f0de0SAdrian Chadd 
121eb6f0de0SAdrian Chadd /*
12281a82688SAdrian Chadd  * Whether to use the 11n rate scenario functions or not
12381a82688SAdrian Chadd  */
12481a82688SAdrian Chadd static inline int
12581a82688SAdrian Chadd ath_tx_is_11n(struct ath_softc *sc)
12681a82688SAdrian Chadd {
1274ddf2cc3SAdrian Chadd 	return ((sc->sc_ah->ah_magic == 0x20065416) ||
1284ddf2cc3SAdrian Chadd 		    (sc->sc_ah->ah_magic == 0x19741014));
12981a82688SAdrian Chadd }
13081a82688SAdrian Chadd 
131eb6f0de0SAdrian Chadd /*
132eb6f0de0SAdrian Chadd  * Obtain the current TID from the given frame.
133eb6f0de0SAdrian Chadd  *
134eb6f0de0SAdrian Chadd  * Non-QoS frames need to go into TID 16 (IEEE80211_NONQOS_TID.)
135eb6f0de0SAdrian Chadd  * This has implications for which AC/priority the packet is placed
136eb6f0de0SAdrian Chadd  * in.
137eb6f0de0SAdrian Chadd  */
138eb6f0de0SAdrian Chadd static int
139eb6f0de0SAdrian Chadd ath_tx_gettid(struct ath_softc *sc, const struct mbuf *m0)
140eb6f0de0SAdrian Chadd {
141eb6f0de0SAdrian Chadd 	const struct ieee80211_frame *wh;
142eb6f0de0SAdrian Chadd 	int pri = M_WME_GETAC(m0);
143eb6f0de0SAdrian Chadd 
144eb6f0de0SAdrian Chadd 	wh = mtod(m0, const struct ieee80211_frame *);
145eb6f0de0SAdrian Chadd 	if (! IEEE80211_QOS_HAS_SEQ(wh))
146eb6f0de0SAdrian Chadd 		return IEEE80211_NONQOS_TID;
147eb6f0de0SAdrian Chadd 	else
148eb6f0de0SAdrian Chadd 		return WME_AC_TO_TID(pri);
149eb6f0de0SAdrian Chadd }
150eb6f0de0SAdrian Chadd 
151f1bc738eSAdrian Chadd static void
152f1bc738eSAdrian Chadd ath_tx_set_retry(struct ath_softc *sc, struct ath_buf *bf)
153f1bc738eSAdrian Chadd {
154f1bc738eSAdrian Chadd 	struct ieee80211_frame *wh;
155f1bc738eSAdrian Chadd 
156f1bc738eSAdrian Chadd 	wh = mtod(bf->bf_m, struct ieee80211_frame *);
157f1bc738eSAdrian Chadd 	/* Only update/resync if needed */
158f1bc738eSAdrian Chadd 	if (bf->bf_state.bfs_isretried == 0) {
159f1bc738eSAdrian Chadd 		wh->i_fc[1] |= IEEE80211_FC1_RETRY;
160f1bc738eSAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
161f1bc738eSAdrian Chadd 		    BUS_DMASYNC_PREWRITE);
162f1bc738eSAdrian Chadd 	}
163f1bc738eSAdrian Chadd 	bf->bf_state.bfs_isretried = 1;
164f1bc738eSAdrian Chadd 	bf->bf_state.bfs_retries ++;
165f1bc738eSAdrian Chadd }
166f1bc738eSAdrian Chadd 
167eb6f0de0SAdrian Chadd /*
168eb6f0de0SAdrian Chadd  * Determine what the correct AC queue for the given frame
169eb6f0de0SAdrian Chadd  * should be.
170eb6f0de0SAdrian Chadd  *
171eb6f0de0SAdrian Chadd  * This code assumes that the TIDs map consistently to
172eb6f0de0SAdrian Chadd  * the underlying hardware (or software) ath_txq.
173eb6f0de0SAdrian Chadd  * Since the sender may try to set an AC which is
174eb6f0de0SAdrian Chadd  * arbitrary, non-QoS TIDs may end up being put on
175eb6f0de0SAdrian Chadd  * completely different ACs. There's no way to put a
176eb6f0de0SAdrian Chadd  * TID into multiple ath_txq's for scheduling, so
177eb6f0de0SAdrian Chadd  * for now we override the AC/TXQ selection and set
178eb6f0de0SAdrian Chadd  * non-QOS TID frames into the BE queue.
179eb6f0de0SAdrian Chadd  *
180eb6f0de0SAdrian Chadd  * This may be completely incorrect - specifically,
181eb6f0de0SAdrian Chadd  * some management frames may end up out of order
182eb6f0de0SAdrian Chadd  * compared to the QoS traffic they're controlling.
183eb6f0de0SAdrian Chadd  * I'll look into this later.
184eb6f0de0SAdrian Chadd  */
185eb6f0de0SAdrian Chadd static int
186eb6f0de0SAdrian Chadd ath_tx_getac(struct ath_softc *sc, const struct mbuf *m0)
187eb6f0de0SAdrian Chadd {
188eb6f0de0SAdrian Chadd 	const struct ieee80211_frame *wh;
189eb6f0de0SAdrian Chadd 	int pri = M_WME_GETAC(m0);
190eb6f0de0SAdrian Chadd 	wh = mtod(m0, const struct ieee80211_frame *);
191eb6f0de0SAdrian Chadd 	if (IEEE80211_QOS_HAS_SEQ(wh))
192eb6f0de0SAdrian Chadd 		return pri;
193eb6f0de0SAdrian Chadd 
194eb6f0de0SAdrian Chadd 	return WME_AC_BE;
195eb6f0de0SAdrian Chadd }
196eb6f0de0SAdrian Chadd 
197b8e788a5SAdrian Chadd void
198b8e788a5SAdrian Chadd ath_txfrag_cleanup(struct ath_softc *sc,
199b8e788a5SAdrian Chadd 	ath_bufhead *frags, struct ieee80211_node *ni)
200b8e788a5SAdrian Chadd {
201b8e788a5SAdrian Chadd 	struct ath_buf *bf, *next;
202b8e788a5SAdrian Chadd 
203b8e788a5SAdrian Chadd 	ATH_TXBUF_LOCK_ASSERT(sc);
204b8e788a5SAdrian Chadd 
2056b349e5aSAdrian Chadd 	TAILQ_FOREACH_SAFE(bf, frags, bf_list, next) {
206b8e788a5SAdrian Chadd 		/* NB: bf assumed clean */
2076b349e5aSAdrian Chadd 		TAILQ_REMOVE(frags, bf, bf_list);
208e1a50456SAdrian Chadd 		ath_returnbuf_head(sc, bf);
209b8e788a5SAdrian Chadd 		ieee80211_node_decref(ni);
210b8e788a5SAdrian Chadd 	}
211b8e788a5SAdrian Chadd }
212b8e788a5SAdrian Chadd 
213b8e788a5SAdrian Chadd /*
214b8e788a5SAdrian Chadd  * Setup xmit of a fragmented frame.  Allocate a buffer
215b8e788a5SAdrian Chadd  * for each frag and bump the node reference count to
216b8e788a5SAdrian Chadd  * reflect the held reference to be setup by ath_tx_start.
217b8e788a5SAdrian Chadd  */
218b8e788a5SAdrian Chadd int
219b8e788a5SAdrian Chadd ath_txfrag_setup(struct ath_softc *sc, ath_bufhead *frags,
220b8e788a5SAdrian Chadd 	struct mbuf *m0, struct ieee80211_node *ni)
221b8e788a5SAdrian Chadd {
222b8e788a5SAdrian Chadd 	struct mbuf *m;
223b8e788a5SAdrian Chadd 	struct ath_buf *bf;
224b8e788a5SAdrian Chadd 
225b8e788a5SAdrian Chadd 	ATH_TXBUF_LOCK(sc);
226b8e788a5SAdrian Chadd 	for (m = m0->m_nextpkt; m != NULL; m = m->m_nextpkt) {
227af33d486SAdrian Chadd 		/* XXX non-management? */
228af33d486SAdrian Chadd 		bf = _ath_getbuf_locked(sc, ATH_BUFTYPE_NORMAL);
229b8e788a5SAdrian Chadd 		if (bf == NULL) {	/* out of buffers, cleanup */
230b43facbfSAdrian Chadd 			device_printf(sc->sc_dev, "%s: no buffer?\n",
231b43facbfSAdrian Chadd 			    __func__);
232b8e788a5SAdrian Chadd 			ath_txfrag_cleanup(sc, frags, ni);
233b8e788a5SAdrian Chadd 			break;
234b8e788a5SAdrian Chadd 		}
235b8e788a5SAdrian Chadd 		ieee80211_node_incref(ni);
2366b349e5aSAdrian Chadd 		TAILQ_INSERT_TAIL(frags, bf, bf_list);
237b8e788a5SAdrian Chadd 	}
238b8e788a5SAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
239b8e788a5SAdrian Chadd 
2406b349e5aSAdrian Chadd 	return !TAILQ_EMPTY(frags);
241b8e788a5SAdrian Chadd }
242b8e788a5SAdrian Chadd 
243b8e788a5SAdrian Chadd /*
244b8e788a5SAdrian Chadd  * Reclaim mbuf resources.  For fragmented frames we
245b8e788a5SAdrian Chadd  * need to claim each frag chained with m_nextpkt.
246b8e788a5SAdrian Chadd  */
247b8e788a5SAdrian Chadd void
248b8e788a5SAdrian Chadd ath_freetx(struct mbuf *m)
249b8e788a5SAdrian Chadd {
250b8e788a5SAdrian Chadd 	struct mbuf *next;
251b8e788a5SAdrian Chadd 
252b8e788a5SAdrian Chadd 	do {
253b8e788a5SAdrian Chadd 		next = m->m_nextpkt;
254b8e788a5SAdrian Chadd 		m->m_nextpkt = NULL;
255b8e788a5SAdrian Chadd 		m_freem(m);
256b8e788a5SAdrian Chadd 	} while ((m = next) != NULL);
257b8e788a5SAdrian Chadd }
258b8e788a5SAdrian Chadd 
259b8e788a5SAdrian Chadd static int
260b8e788a5SAdrian Chadd ath_tx_dmasetup(struct ath_softc *sc, struct ath_buf *bf, struct mbuf *m0)
261b8e788a5SAdrian Chadd {
262b8e788a5SAdrian Chadd 	struct mbuf *m;
263b8e788a5SAdrian Chadd 	int error;
264b8e788a5SAdrian Chadd 
265b8e788a5SAdrian Chadd 	/*
266b8e788a5SAdrian Chadd 	 * Load the DMA map so any coalescing is done.  This
267b8e788a5SAdrian Chadd 	 * also calculates the number of descriptors we need.
268b8e788a5SAdrian Chadd 	 */
269b8e788a5SAdrian Chadd 	error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0,
270b8e788a5SAdrian Chadd 				     bf->bf_segs, &bf->bf_nseg,
271b8e788a5SAdrian Chadd 				     BUS_DMA_NOWAIT);
272b8e788a5SAdrian Chadd 	if (error == EFBIG) {
273b8e788a5SAdrian Chadd 		/* XXX packet requires too many descriptors */
274b8e788a5SAdrian Chadd 		bf->bf_nseg = ATH_TXDESC+1;
275b8e788a5SAdrian Chadd 	} else if (error != 0) {
276b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_busdma++;
277b8e788a5SAdrian Chadd 		ath_freetx(m0);
278b8e788a5SAdrian Chadd 		return error;
279b8e788a5SAdrian Chadd 	}
280b8e788a5SAdrian Chadd 	/*
281b8e788a5SAdrian Chadd 	 * Discard null packets and check for packets that
282b8e788a5SAdrian Chadd 	 * require too many TX descriptors.  We try to convert
283b8e788a5SAdrian Chadd 	 * the latter to a cluster.
284b8e788a5SAdrian Chadd 	 */
285b8e788a5SAdrian Chadd 	if (bf->bf_nseg > ATH_TXDESC) {		/* too many desc's, linearize */
286b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_linear++;
287b8e788a5SAdrian Chadd 		m = m_collapse(m0, M_DONTWAIT, ATH_TXDESC);
288b8e788a5SAdrian Chadd 		if (m == NULL) {
289b8e788a5SAdrian Chadd 			ath_freetx(m0);
290b8e788a5SAdrian Chadd 			sc->sc_stats.ast_tx_nombuf++;
291b8e788a5SAdrian Chadd 			return ENOMEM;
292b8e788a5SAdrian Chadd 		}
293b8e788a5SAdrian Chadd 		m0 = m;
294b8e788a5SAdrian Chadd 		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0,
295b8e788a5SAdrian Chadd 					     bf->bf_segs, &bf->bf_nseg,
296b8e788a5SAdrian Chadd 					     BUS_DMA_NOWAIT);
297b8e788a5SAdrian Chadd 		if (error != 0) {
298b8e788a5SAdrian Chadd 			sc->sc_stats.ast_tx_busdma++;
299b8e788a5SAdrian Chadd 			ath_freetx(m0);
300b8e788a5SAdrian Chadd 			return error;
301b8e788a5SAdrian Chadd 		}
302b8e788a5SAdrian Chadd 		KASSERT(bf->bf_nseg <= ATH_TXDESC,
303b8e788a5SAdrian Chadd 		    ("too many segments after defrag; nseg %u", bf->bf_nseg));
304b8e788a5SAdrian Chadd 	} else if (bf->bf_nseg == 0) {		/* null packet, discard */
305b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_nodata++;
306b8e788a5SAdrian Chadd 		ath_freetx(m0);
307b8e788a5SAdrian Chadd 		return EIO;
308b8e788a5SAdrian Chadd 	}
309b8e788a5SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_XMIT, "%s: m %p len %u\n",
310b8e788a5SAdrian Chadd 		__func__, m0, m0->m_pkthdr.len);
311b8e788a5SAdrian Chadd 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
312b8e788a5SAdrian Chadd 	bf->bf_m = m0;
313b8e788a5SAdrian Chadd 
314b8e788a5SAdrian Chadd 	return 0;
315b8e788a5SAdrian Chadd }
316b8e788a5SAdrian Chadd 
3176edf1dc7SAdrian Chadd /*
3186edf1dc7SAdrian Chadd  * Chain together segments+descriptors for a non-11n frame.
3196edf1dc7SAdrian Chadd  */
320b8e788a5SAdrian Chadd static void
321eb6f0de0SAdrian Chadd ath_tx_chaindesclist(struct ath_softc *sc, struct ath_buf *bf)
322b8e788a5SAdrian Chadd {
323b8e788a5SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
32442083b3dSAdrian Chadd 	char *ds, *ds0;
3252b200bb4SAdrian Chadd 	int i, bp, dsp;
32646634305SAdrian Chadd 	HAL_DMA_ADDR bufAddrList[4];
32746634305SAdrian Chadd 	uint32_t segLenList[4];
3282b200bb4SAdrian Chadd 	int numTxMaps = 1;
329e2137b86SAdrian Chadd 	int isFirstDesc = 1;
33079b52356SAdrian Chadd 	int qnum;
33146634305SAdrian Chadd 
3323d9b1596SAdrian Chadd 	/*
3333d9b1596SAdrian Chadd 	 * XXX There's txdma and txdma_mgmt; the descriptor
3343d9b1596SAdrian Chadd 	 * sizes must match.
3353d9b1596SAdrian Chadd 	 */
3363d9b1596SAdrian Chadd 	struct ath_descdma *dd = &sc->sc_txdma;
337b8e788a5SAdrian Chadd 
338b8e788a5SAdrian Chadd 	/*
339b8e788a5SAdrian Chadd 	 * Fillin the remainder of the descriptor info.
340b8e788a5SAdrian Chadd 	 */
34146634305SAdrian Chadd 
3422b200bb4SAdrian Chadd 	/*
3432b200bb4SAdrian Chadd 	 * For now the HAL doesn't implement halNumTxMaps for non-EDMA
3442b200bb4SAdrian Chadd 	 * (ie it's 0.)  So just work around it.
3452b200bb4SAdrian Chadd 	 *
3462b200bb4SAdrian Chadd 	 * XXX TODO: populate halNumTxMaps for each HAL chip and
3472b200bb4SAdrian Chadd 	 * then undo this hack.
3482b200bb4SAdrian Chadd 	 */
3492b200bb4SAdrian Chadd 	if (sc->sc_ah->ah_magic == 0x19741014)
3502b200bb4SAdrian Chadd 		numTxMaps = 4;
3512b200bb4SAdrian Chadd 
3522b200bb4SAdrian Chadd 	/*
3532b200bb4SAdrian Chadd 	 * For EDMA and later chips ensure the TX map is fully populated
3542b200bb4SAdrian Chadd 	 * before advancing to the next descriptor.
3552b200bb4SAdrian Chadd 	 */
35642083b3dSAdrian Chadd 	ds0 = ds = (char *) bf->bf_desc;
3572b200bb4SAdrian Chadd 	bp = dsp = 0;
3582b200bb4SAdrian Chadd 	bzero(bufAddrList, sizeof(bufAddrList));
3592b200bb4SAdrian Chadd 	bzero(segLenList, sizeof(segLenList));
3602b200bb4SAdrian Chadd 	for (i = 0; i < bf->bf_nseg; i++) {
3612b200bb4SAdrian Chadd 		bufAddrList[bp] = bf->bf_segs[i].ds_addr;
3622b200bb4SAdrian Chadd 		segLenList[bp] = bf->bf_segs[i].ds_len;
3632b200bb4SAdrian Chadd 		bp++;
3642b200bb4SAdrian Chadd 
3652b200bb4SAdrian Chadd 		/*
3662b200bb4SAdrian Chadd 		 * Go to the next segment if this isn't the last segment
3672b200bb4SAdrian Chadd 		 * and there's space in the current TX map.
3682b200bb4SAdrian Chadd 		 */
3692b200bb4SAdrian Chadd 		if ((i != bf->bf_nseg - 1) && (bp < numTxMaps))
3702b200bb4SAdrian Chadd 			continue;
3712b200bb4SAdrian Chadd 
3722b200bb4SAdrian Chadd 		/*
3732b200bb4SAdrian Chadd 		 * Last segment or we're out of buffer pointers.
3742b200bb4SAdrian Chadd 		 */
3752b200bb4SAdrian Chadd 		bp = 0;
37646634305SAdrian Chadd 
377b8e788a5SAdrian Chadd 		if (i == bf->bf_nseg - 1)
37842083b3dSAdrian Chadd 			ath_hal_settxdesclink(ah, (struct ath_desc *) ds, 0);
379b8e788a5SAdrian Chadd 		else
38042083b3dSAdrian Chadd 			ath_hal_settxdesclink(ah, (struct ath_desc *) ds,
3812b200bb4SAdrian Chadd 			    bf->bf_daddr + dd->dd_descsize * (dsp + 1));
38246634305SAdrian Chadd 
38346634305SAdrian Chadd 		/*
38446634305SAdrian Chadd 		 * XXX this assumes that bfs_txq is the actual destination
38546634305SAdrian Chadd 		 * hardware queue at this point.  It may not have been assigned,
38646634305SAdrian Chadd 		 * it may actually be pointing to the multicast software
38746634305SAdrian Chadd 		 * TXQ id.  These must be fixed!
38846634305SAdrian Chadd 		 */
38979b52356SAdrian Chadd 		qnum = bf->bf_state.bfs_txq->axq_qnum;
39079b52356SAdrian Chadd 
39142083b3dSAdrian Chadd 		ath_hal_filltxdesc(ah, (struct ath_desc *) ds
39246634305SAdrian Chadd 			, bufAddrList
39346634305SAdrian Chadd 			, segLenList
3942b200bb4SAdrian Chadd 			, bf->bf_descid		/* XXX desc id */
39579b52356SAdrian Chadd 			, qnum
396e2137b86SAdrian Chadd 			, isFirstDesc		/* first segment */
397b8e788a5SAdrian Chadd 			, i == bf->bf_nseg - 1	/* last segment */
39842083b3dSAdrian Chadd 			, (struct ath_desc *) ds0	/* first descriptor */
399b8e788a5SAdrian Chadd 		);
40021840808SAdrian Chadd 
40121840808SAdrian Chadd 		/* Make sure the 11n aggregate fields are cleared */
40221840808SAdrian Chadd 		if (ath_tx_is_11n(sc))
4035d9b19f7SAdrian Chadd 			ath_hal_clr11n_aggr(sc->sc_ah, (struct ath_desc *) ds);
40421840808SAdrian Chadd 
405e2137b86SAdrian Chadd 		isFirstDesc = 0;
4060f8423a2SAdrian Chadd #ifdef	ATH_DEBUG
40742083b3dSAdrian Chadd 		if (sc->sc_debug & ATH_DEBUG_XMIT)
40842083b3dSAdrian Chadd 			ath_printtxbuf(sc, bf, qnum, 0, 0);
4090f8423a2SAdrian Chadd #endif
41042083b3dSAdrian Chadd 		bf->bf_lastds = (struct ath_desc *) ds;
4112b200bb4SAdrian Chadd 
4122b200bb4SAdrian Chadd 		/*
4132b200bb4SAdrian Chadd 		 * Don't forget to skip to the next descriptor.
4142b200bb4SAdrian Chadd 		 */
41542083b3dSAdrian Chadd 		ds += sc->sc_tx_desclen;
4162b200bb4SAdrian Chadd 		dsp++;
4172b200bb4SAdrian Chadd 
4182b200bb4SAdrian Chadd 		/*
4192b200bb4SAdrian Chadd 		 * .. and don't forget to blank these out!
4202b200bb4SAdrian Chadd 		 */
4212b200bb4SAdrian Chadd 		bzero(bufAddrList, sizeof(bufAddrList));
4222b200bb4SAdrian Chadd 		bzero(segLenList, sizeof(segLenList));
423b8e788a5SAdrian Chadd 	}
4244d7f8837SAdrian Chadd 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
42581a82688SAdrian Chadd }
42681a82688SAdrian Chadd 
427eb6f0de0SAdrian Chadd /*
428eb6f0de0SAdrian Chadd  * Fill in the descriptor list for a aggregate subframe.
429eb6f0de0SAdrian Chadd  *
430eb6f0de0SAdrian Chadd  * The subframe is returned with the ds_link field in the last subframe
431eb6f0de0SAdrian Chadd  * pointing to 0.
432eb6f0de0SAdrian Chadd  */
43381a82688SAdrian Chadd static void
434eb6f0de0SAdrian Chadd ath_tx_chaindesclist_subframe(struct ath_softc *sc, struct ath_buf *bf)
43581a82688SAdrian Chadd {
43681a82688SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
437eb6f0de0SAdrian Chadd 	struct ath_desc *ds, *ds0;
438eb6f0de0SAdrian Chadd 	int i;
439fffbec86SAdrian Chadd 	HAL_DMA_ADDR bufAddrList[4];
440fffbec86SAdrian Chadd 	uint32_t segLenList[4];
441fffbec86SAdrian Chadd 
4423d9b1596SAdrian Chadd 	/*
4433d9b1596SAdrian Chadd 	 * XXX There's txdma and txdma_mgmt; the descriptor
4443d9b1596SAdrian Chadd 	 * sizes must match.
4453d9b1596SAdrian Chadd 	 */
4463d9b1596SAdrian Chadd 	struct ath_descdma *dd = &sc->sc_txdma;
44781a82688SAdrian Chadd 
448eb6f0de0SAdrian Chadd 	ds0 = ds = bf->bf_desc;
449eb6f0de0SAdrian Chadd 
450eb6f0de0SAdrian Chadd 	/*
451eb6f0de0SAdrian Chadd 	 * There's no need to call ath_hal_setupfirsttxdesc here;
452eb6f0de0SAdrian Chadd 	 * That's only going to occur for the first frame in an aggregate.
453eb6f0de0SAdrian Chadd 	 */
454eb6f0de0SAdrian Chadd 	for (i = 0; i < bf->bf_nseg; i++, ds++) {
455fffbec86SAdrian Chadd 		bzero(bufAddrList, sizeof(bufAddrList));
456fffbec86SAdrian Chadd 		bzero(segLenList, sizeof(segLenList));
457eb6f0de0SAdrian Chadd 		if (i == bf->bf_nseg - 1)
458bb069955SAdrian Chadd 			ath_hal_settxdesclink(ah, ds, 0);
459eb6f0de0SAdrian Chadd 		else
460bb069955SAdrian Chadd 			ath_hal_settxdesclink(ah, ds,
4613d9b1596SAdrian Chadd 			    bf->bf_daddr + dd->dd_descsize * (i + 1));
462eb6f0de0SAdrian Chadd 
463fffbec86SAdrian Chadd 		bufAddrList[0] = bf->bf_segs[i].ds_addr;
464fffbec86SAdrian Chadd 		segLenList[0] = bf->bf_segs[i].ds_len;
465fffbec86SAdrian Chadd 
466eb6f0de0SAdrian Chadd 		/*
467eb6f0de0SAdrian Chadd 		 * This performs the setup for an aggregate frame.
468eb6f0de0SAdrian Chadd 		 * This includes enabling the aggregate flags if needed.
469eb6f0de0SAdrian Chadd 		 */
470eb6f0de0SAdrian Chadd 		ath_hal_chaintxdesc(ah, ds,
471fffbec86SAdrian Chadd 		    bufAddrList,
472fffbec86SAdrian Chadd 		    segLenList,
473eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_pktlen,
474eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_hdrlen,
475eb6f0de0SAdrian Chadd 		    HAL_PKT_TYPE_AMPDU,	/* forces aggregate bits to be set */
476eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_keyix,
477eb6f0de0SAdrian Chadd 		    0,			/* cipher, calculated from keyix */
478eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_ndelim,
479eb6f0de0SAdrian Chadd 		    i == 0,		/* first segment */
48033d34032SAdrian Chadd 		    i == bf->bf_nseg - 1,	/* last segment */
48133d34032SAdrian Chadd 		    bf->bf_next == NULL		/* last sub-frame in aggr */
482eb6f0de0SAdrian Chadd 		);
483eb6f0de0SAdrian Chadd 
484eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_XMIT,
485eb6f0de0SAdrian Chadd 			"%s: %d: %08x %08x %08x %08x %08x %08x\n",
486eb6f0de0SAdrian Chadd 			__func__, i, ds->ds_link, ds->ds_data,
487eb6f0de0SAdrian Chadd 			ds->ds_ctl0, ds->ds_ctl1, ds->ds_hw[0], ds->ds_hw[1]);
488eb6f0de0SAdrian Chadd 		bf->bf_lastds = ds;
4894d7f8837SAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
4904d7f8837SAdrian Chadd 		    BUS_DMASYNC_PREWRITE);
491eb6f0de0SAdrian Chadd 	}
492eb6f0de0SAdrian Chadd }
493eb6f0de0SAdrian Chadd 
494eb6f0de0SAdrian Chadd /*
495d34a7347SAdrian Chadd  * Set the rate control fields in the given descriptor based on
496d34a7347SAdrian Chadd  * the bf_state fields and node state.
497d34a7347SAdrian Chadd  *
498d34a7347SAdrian Chadd  * The bfs fields should already be set with the relevant rate
499d34a7347SAdrian Chadd  * control information, including whether MRR is to be enabled.
500d34a7347SAdrian Chadd  *
501d34a7347SAdrian Chadd  * Since the FreeBSD HAL currently sets up the first TX rate
502d34a7347SAdrian Chadd  * in ath_hal_setuptxdesc(), this will setup the MRR
503d34a7347SAdrian Chadd  * conditionally for the pre-11n chips, and call ath_buf_set_rate
504d34a7347SAdrian Chadd  * unconditionally for 11n chips. These require the 11n rate
505d34a7347SAdrian Chadd  * scenario to be set if MCS rates are enabled, so it's easier
506d34a7347SAdrian Chadd  * to just always call it. The caller can then only set rates 2, 3
507d34a7347SAdrian Chadd  * and 4 if multi-rate retry is needed.
508d34a7347SAdrian Chadd  */
509d34a7347SAdrian Chadd static void
510d34a7347SAdrian Chadd ath_tx_set_ratectrl(struct ath_softc *sc, struct ieee80211_node *ni,
511d34a7347SAdrian Chadd     struct ath_buf *bf)
512d34a7347SAdrian Chadd {
513d34a7347SAdrian Chadd 	struct ath_rc_series *rc = bf->bf_state.bfs_rc;
514d34a7347SAdrian Chadd 
515d34a7347SAdrian Chadd 	/* If mrr is disabled, blank tries 1, 2, 3 */
516d34a7347SAdrian Chadd 	if (! bf->bf_state.bfs_ismrr)
517d34a7347SAdrian Chadd 		rc[1].tries = rc[2].tries = rc[3].tries = 0;
518d34a7347SAdrian Chadd 
519d34a7347SAdrian Chadd 	/*
520d34a7347SAdrian Chadd 	 * Always call - that way a retried descriptor will
521d34a7347SAdrian Chadd 	 * have the MRR fields overwritten.
522d34a7347SAdrian Chadd 	 *
523d34a7347SAdrian Chadd 	 * XXX TODO: see if this is really needed - setting up
524d34a7347SAdrian Chadd 	 * the first descriptor should set the MRR fields to 0
525d34a7347SAdrian Chadd 	 * for us anyway.
526d34a7347SAdrian Chadd 	 */
527d34a7347SAdrian Chadd 	if (ath_tx_is_11n(sc)) {
528d34a7347SAdrian Chadd 		ath_buf_set_rate(sc, ni, bf);
529d34a7347SAdrian Chadd 	} else {
530d34a7347SAdrian Chadd 		ath_hal_setupxtxdesc(sc->sc_ah, bf->bf_desc
531d34a7347SAdrian Chadd 			, rc[1].ratecode, rc[1].tries
532d34a7347SAdrian Chadd 			, rc[2].ratecode, rc[2].tries
533d34a7347SAdrian Chadd 			, rc[3].ratecode, rc[3].tries
534d34a7347SAdrian Chadd 		);
535d34a7347SAdrian Chadd 	}
536d34a7347SAdrian Chadd }
537d34a7347SAdrian Chadd 
538d34a7347SAdrian Chadd /*
539eb6f0de0SAdrian Chadd  * Setup segments+descriptors for an 11n aggregate.
540eb6f0de0SAdrian Chadd  * bf_first is the first buffer in the aggregate.
541eb6f0de0SAdrian Chadd  * The descriptor list must already been linked together using
542eb6f0de0SAdrian Chadd  * bf->bf_next.
543eb6f0de0SAdrian Chadd  */
544eb6f0de0SAdrian Chadd static void
545eb6f0de0SAdrian Chadd ath_tx_setds_11n(struct ath_softc *sc, struct ath_buf *bf_first)
546eb6f0de0SAdrian Chadd {
547eb6f0de0SAdrian Chadd 	struct ath_buf *bf, *bf_prev = NULL;
548eb6f0de0SAdrian Chadd 
549eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: nframes=%d, al=%d\n",
550eb6f0de0SAdrian Chadd 	    __func__, bf_first->bf_state.bfs_nframes,
551eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_al);
552eb6f0de0SAdrian Chadd 
553eb6f0de0SAdrian Chadd 	/*
554eb6f0de0SAdrian Chadd 	 * Setup all descriptors of all subframes.
555eb6f0de0SAdrian Chadd 	 */
556eb6f0de0SAdrian Chadd 	bf = bf_first;
557eb6f0de0SAdrian Chadd 	while (bf != NULL) {
558eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
559eb6f0de0SAdrian Chadd 		    "%s: bf=%p, nseg=%d, pktlen=%d, seqno=%d\n",
560eb6f0de0SAdrian Chadd 		    __func__, bf, bf->bf_nseg, bf->bf_state.bfs_pktlen,
561eb6f0de0SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
562eb6f0de0SAdrian Chadd 
563eb6f0de0SAdrian Chadd 		/* Sub-frame setup */
564eb6f0de0SAdrian Chadd 		ath_tx_chaindesclist_subframe(sc, bf);
565eb6f0de0SAdrian Chadd 
566eb6f0de0SAdrian Chadd 		/*
567eb6f0de0SAdrian Chadd 		 * Link the last descriptor of the previous frame
568eb6f0de0SAdrian Chadd 		 * to the beginning descriptor of this frame.
569eb6f0de0SAdrian Chadd 		 */
570eb6f0de0SAdrian Chadd 		if (bf_prev != NULL)
571bb069955SAdrian Chadd 			ath_hal_settxdesclink(sc->sc_ah, bf_prev->bf_lastds,
572bb069955SAdrian Chadd 			    bf->bf_daddr);
573eb6f0de0SAdrian Chadd 
574eb6f0de0SAdrian Chadd 		/* Save a copy so we can link the next descriptor in */
575eb6f0de0SAdrian Chadd 		bf_prev = bf;
576eb6f0de0SAdrian Chadd 		bf = bf->bf_next;
577eb6f0de0SAdrian Chadd 	}
578eb6f0de0SAdrian Chadd 
579eb6f0de0SAdrian Chadd 	/*
580eb6f0de0SAdrian Chadd 	 * Setup first descriptor of first frame.
581eb6f0de0SAdrian Chadd 	 * chaintxdesc() overwrites the descriptor entries;
582eb6f0de0SAdrian Chadd 	 * setupfirsttxdesc() merges in things.
583eb6f0de0SAdrian Chadd 	 * Otherwise various fields aren't set correctly (eg flags).
584eb6f0de0SAdrian Chadd 	 */
585eb6f0de0SAdrian Chadd 	ath_hal_setupfirsttxdesc(sc->sc_ah,
586eb6f0de0SAdrian Chadd 	    bf_first->bf_desc,
587eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_al,
588875a9451SAdrian Chadd 	    bf_first->bf_state.bfs_txflags | HAL_TXDESC_INTREQ,
589eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_txpower,
590eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_txrate0,
591eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_try0,
592eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_txantenna,
593eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_ctsrate,
594eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_ctsduration);
595eb6f0de0SAdrian Chadd 
596eb6f0de0SAdrian Chadd 	/*
597eb6f0de0SAdrian Chadd 	 * Set the first descriptor bf_lastds field to point to
598eb6f0de0SAdrian Chadd 	 * the last descriptor in the last subframe, that's where
599eb6f0de0SAdrian Chadd 	 * the status update will occur.
600eb6f0de0SAdrian Chadd 	 */
601eb6f0de0SAdrian Chadd 	bf_first->bf_lastds = bf_prev->bf_lastds;
602eb6f0de0SAdrian Chadd 
603eb6f0de0SAdrian Chadd 	/*
604eb6f0de0SAdrian Chadd 	 * And bf_last in the first descriptor points to the end of
605eb6f0de0SAdrian Chadd 	 * the aggregate list.
606eb6f0de0SAdrian Chadd 	 */
607eb6f0de0SAdrian Chadd 	bf_first->bf_last = bf_prev;
608eb6f0de0SAdrian Chadd 
609d34a7347SAdrian Chadd 	/*
610d34a7347SAdrian Chadd 	 * setup first desc with rate and aggr info
611d34a7347SAdrian Chadd 	 */
612d34a7347SAdrian Chadd 	ath_tx_set_ratectrl(sc, bf_first->bf_node, bf_first);
613d34a7347SAdrian Chadd 
6148c08c07aSAdrian Chadd 	/*
6158c08c07aSAdrian Chadd 	 * Setup the last descriptor in the list.
616a6e82959SAdrian Chadd 	 *
617a6e82959SAdrian Chadd 	 * bf_first->bf_lastds already points to it; the rate
618a6e82959SAdrian Chadd 	 * control information needs to be squirreled away here
619a6e82959SAdrian Chadd 	 * as well ans clearing the moreaggr/paddelim fields.
6208c08c07aSAdrian Chadd 	 */
621a6e82959SAdrian Chadd 	ath_hal_setuplasttxdesc(sc->sc_ah, bf_first->bf_lastds,
6228c08c07aSAdrian Chadd 	    bf_first->bf_desc);
6238c08c07aSAdrian Chadd 
624eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: end\n", __func__);
625eb6f0de0SAdrian Chadd }
626eb6f0de0SAdrian Chadd 
62746634305SAdrian Chadd /*
62846634305SAdrian Chadd  * Hand-off a frame to the multicast TX queue.
62946634305SAdrian Chadd  *
63046634305SAdrian Chadd  * This is a software TXQ which will be appended to the CAB queue
63146634305SAdrian Chadd  * during the beacon setup code.
63246634305SAdrian Chadd  *
63346634305SAdrian Chadd  * XXX TODO: since the AR9300 EDMA TX queue support wants the QCU ID
63446634305SAdrian Chadd  * as part of the TX descriptor, bf_state.bfs_txq must be updated
63546634305SAdrian Chadd  * with the actual hardware txq, or all of this will fall apart.
63646634305SAdrian Chadd  *
63746634305SAdrian Chadd  * XXX It may not be a bad idea to just stuff the QCU ID into bf_state
63846634305SAdrian Chadd  * and retire bfs_txq; then make sure the CABQ QCU ID is populated
63946634305SAdrian Chadd  * correctly.
64046634305SAdrian Chadd  */
641eb6f0de0SAdrian Chadd static void
642eb6f0de0SAdrian Chadd ath_tx_handoff_mcast(struct ath_softc *sc, struct ath_txq *txq,
643eb6f0de0SAdrian Chadd     struct ath_buf *bf)
644eb6f0de0SAdrian Chadd {
645eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
646eb6f0de0SAdrian Chadd 	KASSERT((bf->bf_flags & ATH_BUF_BUSY) == 0,
647eb6f0de0SAdrian Chadd 	     ("%s: busy status 0x%x", __func__, bf->bf_flags));
648eb6f0de0SAdrian Chadd 	if (txq->axq_link != NULL) {
649eb6f0de0SAdrian Chadd 		struct ath_buf *last = ATH_TXQ_LAST(txq, axq_q_s);
650eb6f0de0SAdrian Chadd 		struct ieee80211_frame *wh;
651eb6f0de0SAdrian Chadd 
652eb6f0de0SAdrian Chadd 		/* mark previous frame */
653eb6f0de0SAdrian Chadd 		wh = mtod(last->bf_m, struct ieee80211_frame *);
654eb6f0de0SAdrian Chadd 		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
655eb6f0de0SAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, last->bf_dmamap,
656eb6f0de0SAdrian Chadd 		    BUS_DMASYNC_PREWRITE);
657eb6f0de0SAdrian Chadd 
658eb6f0de0SAdrian Chadd 		/* link descriptor */
659eb6f0de0SAdrian Chadd 		*txq->axq_link = bf->bf_daddr;
660eb6f0de0SAdrian Chadd 	}
661eb6f0de0SAdrian Chadd 	ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
662bb069955SAdrian Chadd 	ath_hal_gettxdesclinkptr(sc->sc_ah, bf->bf_lastds, &txq->axq_link);
663eb6f0de0SAdrian Chadd }
664eb6f0de0SAdrian Chadd 
665eb6f0de0SAdrian Chadd /*
666eb6f0de0SAdrian Chadd  * Hand-off packet to a hardware queue.
667eb6f0de0SAdrian Chadd  */
668eb6f0de0SAdrian Chadd static void
669d4365d16SAdrian Chadd ath_tx_handoff_hw(struct ath_softc *sc, struct ath_txq *txq,
670d4365d16SAdrian Chadd     struct ath_buf *bf)
671eb6f0de0SAdrian Chadd {
672eb6f0de0SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
67381a82688SAdrian Chadd 
674b8e788a5SAdrian Chadd 	/*
675b8e788a5SAdrian Chadd 	 * Insert the frame on the outbound list and pass it on
676b8e788a5SAdrian Chadd 	 * to the hardware.  Multicast frames buffered for power
677b8e788a5SAdrian Chadd 	 * save stations and transmit from the CAB queue are stored
678b8e788a5SAdrian Chadd 	 * on a s/w only queue and loaded on to the CAB queue in
679b8e788a5SAdrian Chadd 	 * the SWBA handler since frames only go out on DTIM and
680b8e788a5SAdrian Chadd 	 * to avoid possible races.
681b8e788a5SAdrian Chadd 	 */
682eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
683b8e788a5SAdrian Chadd 	KASSERT((bf->bf_flags & ATH_BUF_BUSY) == 0,
684eb6f0de0SAdrian Chadd 	     ("%s: busy status 0x%x", __func__, bf->bf_flags));
685eb6f0de0SAdrian Chadd 	KASSERT(txq->axq_qnum != ATH_TXQ_SWQ,
686eb6f0de0SAdrian Chadd 	     ("ath_tx_handoff_hw called for mcast queue"));
687eb6f0de0SAdrian Chadd 
688ef27340cSAdrian Chadd #if 0
689ef27340cSAdrian Chadd 	/*
690ef27340cSAdrian Chadd 	 * This causes a LOR. Find out where the PCU lock is being
691ef27340cSAdrian Chadd 	 * held whilst the TXQ lock is grabbed - that shouldn't
692ef27340cSAdrian Chadd 	 * be occuring.
693ef27340cSAdrian Chadd 	 */
694ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
695ef27340cSAdrian Chadd 	if (sc->sc_inreset_cnt) {
696ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
697ef27340cSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_RESET,
698ef27340cSAdrian Chadd 		    "%s: called with sc_in_reset != 0\n",
699ef27340cSAdrian Chadd 		    __func__);
700ef27340cSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_XMIT,
701ef27340cSAdrian Chadd 		    "%s: queued: TXDP[%u] = %p (%p) depth %d\n",
702ef27340cSAdrian Chadd 		    __func__, txq->axq_qnum,
703ef27340cSAdrian Chadd 		    (caddr_t)bf->bf_daddr, bf->bf_desc,
704ef27340cSAdrian Chadd 		    txq->axq_depth);
705ef27340cSAdrian Chadd 		ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
706ef27340cSAdrian Chadd 		if (bf->bf_state.bfs_aggr)
707ef27340cSAdrian Chadd 			txq->axq_aggr_depth++;
708ef27340cSAdrian Chadd 		/*
709ef27340cSAdrian Chadd 		 * There's no need to update axq_link; the hardware
710ef27340cSAdrian Chadd 		 * is in reset and once the reset is complete, any
711ef27340cSAdrian Chadd 		 * non-empty queues will simply have DMA restarted.
712ef27340cSAdrian Chadd 		 */
713ef27340cSAdrian Chadd 		return;
714ef27340cSAdrian Chadd 		}
715ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
716ef27340cSAdrian Chadd #endif
717ef27340cSAdrian Chadd 
718eb6f0de0SAdrian Chadd 	/* For now, so not to generate whitespace diffs */
719eb6f0de0SAdrian Chadd 	if (1) {
720b8e788a5SAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
721b8e788a5SAdrian Chadd 		int qbusy;
722b8e788a5SAdrian Chadd 
723b8e788a5SAdrian Chadd 		ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
724b8e788a5SAdrian Chadd 		qbusy = ath_hal_txqenabled(ah, txq->axq_qnum);
725b8e788a5SAdrian Chadd 		if (txq->axq_link == NULL) {
726b8e788a5SAdrian Chadd 			/*
727b8e788a5SAdrian Chadd 			 * Be careful writing the address to TXDP.  If
728b8e788a5SAdrian Chadd 			 * the tx q is enabled then this write will be
729b8e788a5SAdrian Chadd 			 * ignored.  Normally this is not an issue but
730b8e788a5SAdrian Chadd 			 * when tdma is in use and the q is beacon gated
731b8e788a5SAdrian Chadd 			 * this race can occur.  If the q is busy then
732b8e788a5SAdrian Chadd 			 * defer the work to later--either when another
733b8e788a5SAdrian Chadd 			 * packet comes along or when we prepare a beacon
734b8e788a5SAdrian Chadd 			 * frame at SWBA.
735b8e788a5SAdrian Chadd 			 */
736b8e788a5SAdrian Chadd 			if (!qbusy) {
737d4365d16SAdrian Chadd 				ath_hal_puttxbuf(ah, txq->axq_qnum,
738d4365d16SAdrian Chadd 				    bf->bf_daddr);
739b8e788a5SAdrian Chadd 				txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
740b8e788a5SAdrian Chadd 				DPRINTF(sc, ATH_DEBUG_XMIT,
741b8e788a5SAdrian Chadd 				    "%s: TXDP[%u] = %p (%p) depth %d\n",
742b8e788a5SAdrian Chadd 				    __func__, txq->axq_qnum,
743b8e788a5SAdrian Chadd 				    (caddr_t)bf->bf_daddr, bf->bf_desc,
744b8e788a5SAdrian Chadd 				    txq->axq_depth);
745b8e788a5SAdrian Chadd 			} else {
746b8e788a5SAdrian Chadd 				txq->axq_flags |= ATH_TXQ_PUTPENDING;
747b8e788a5SAdrian Chadd 				DPRINTF(sc, ATH_DEBUG_TDMA | ATH_DEBUG_XMIT,
748b8e788a5SAdrian Chadd 				    "%s: Q%u busy, defer enable\n", __func__,
749b8e788a5SAdrian Chadd 				    txq->axq_qnum);
750b8e788a5SAdrian Chadd 			}
751b8e788a5SAdrian Chadd 		} else {
752b8e788a5SAdrian Chadd 			*txq->axq_link = bf->bf_daddr;
753b8e788a5SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_XMIT,
754b8e788a5SAdrian Chadd 			    "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
755b8e788a5SAdrian Chadd 			    txq->axq_qnum, txq->axq_link,
756d4365d16SAdrian Chadd 			    (caddr_t)bf->bf_daddr, bf->bf_desc,
757d4365d16SAdrian Chadd 			    txq->axq_depth);
758b8e788a5SAdrian Chadd 			if ((txq->axq_flags & ATH_TXQ_PUTPENDING) && !qbusy) {
759b8e788a5SAdrian Chadd 				/*
760b8e788a5SAdrian Chadd 				 * The q was busy when we previously tried
761b8e788a5SAdrian Chadd 				 * to write the address of the first buffer
762b8e788a5SAdrian Chadd 				 * in the chain.  Since it's not busy now
763b8e788a5SAdrian Chadd 				 * handle this chore.  We are certain the
764b8e788a5SAdrian Chadd 				 * buffer at the front is the right one since
765b8e788a5SAdrian Chadd 				 * axq_link is NULL only when the buffer list
766b8e788a5SAdrian Chadd 				 * is/was empty.
767b8e788a5SAdrian Chadd 				 */
768b8e788a5SAdrian Chadd 				ath_hal_puttxbuf(ah, txq->axq_qnum,
7696b349e5aSAdrian Chadd 					TAILQ_FIRST(&txq->axq_q)->bf_daddr);
770b8e788a5SAdrian Chadd 				txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
771b8e788a5SAdrian Chadd 				DPRINTF(sc, ATH_DEBUG_TDMA | ATH_DEBUG_XMIT,
772b8e788a5SAdrian Chadd 				    "%s: Q%u restarted\n", __func__,
773b8e788a5SAdrian Chadd 				    txq->axq_qnum);
774b8e788a5SAdrian Chadd 			}
775b8e788a5SAdrian Chadd 		}
776b8e788a5SAdrian Chadd #else
777b8e788a5SAdrian Chadd 		ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
778b8e788a5SAdrian Chadd 		if (txq->axq_link == NULL) {
779b8e788a5SAdrian Chadd 			ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
780b8e788a5SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_XMIT,
781b8e788a5SAdrian Chadd 			    "%s: TXDP[%u] = %p (%p) depth %d\n",
782b8e788a5SAdrian Chadd 			    __func__, txq->axq_qnum,
783b8e788a5SAdrian Chadd 			    (caddr_t)bf->bf_daddr, bf->bf_desc,
784b8e788a5SAdrian Chadd 			    txq->axq_depth);
785b8e788a5SAdrian Chadd 		} else {
786b8e788a5SAdrian Chadd 			*txq->axq_link = bf->bf_daddr;
787b8e788a5SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_XMIT,
788b8e788a5SAdrian Chadd 			    "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
789b8e788a5SAdrian Chadd 			    txq->axq_qnum, txq->axq_link,
790d4365d16SAdrian Chadd 			    (caddr_t)bf->bf_daddr, bf->bf_desc,
791d4365d16SAdrian Chadd 			    txq->axq_depth);
792b8e788a5SAdrian Chadd 		}
793b8e788a5SAdrian Chadd #endif /* IEEE80211_SUPPORT_TDMA */
7946edf1dc7SAdrian Chadd 		if (bf->bf_state.bfs_aggr)
7956edf1dc7SAdrian Chadd 			txq->axq_aggr_depth++;
796bb069955SAdrian Chadd 		ath_hal_gettxdesclinkptr(ah, bf->bf_lastds, &txq->axq_link);
797b8e788a5SAdrian Chadd 		ath_hal_txstart(ah, txq->axq_qnum);
798b8e788a5SAdrian Chadd 	}
799b8e788a5SAdrian Chadd }
800eb6f0de0SAdrian Chadd 
801eb6f0de0SAdrian Chadd /*
802eb6f0de0SAdrian Chadd  * Restart TX DMA for the given TXQ.
803eb6f0de0SAdrian Chadd  *
804eb6f0de0SAdrian Chadd  * This must be called whether the queue is empty or not.
805eb6f0de0SAdrian Chadd  */
806746bab5bSAdrian Chadd static void
807746bab5bSAdrian Chadd ath_legacy_tx_dma_restart(struct ath_softc *sc, struct ath_txq *txq)
808eb6f0de0SAdrian Chadd {
809eb6f0de0SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
810b1f3262cSAdrian Chadd 	struct ath_buf *bf, *bf_last;
811eb6f0de0SAdrian Chadd 
812eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
813eb6f0de0SAdrian Chadd 
814eb6f0de0SAdrian Chadd 	/* This is always going to be cleared, empty or not */
815eb6f0de0SAdrian Chadd 	txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
816eb6f0de0SAdrian Chadd 
817b1f3262cSAdrian Chadd 	/* XXX make this ATH_TXQ_FIRST */
818eb6f0de0SAdrian Chadd 	bf = TAILQ_FIRST(&txq->axq_q);
819b1f3262cSAdrian Chadd 	bf_last = ATH_TXQ_LAST(txq, axq_q_s);
820b1f3262cSAdrian Chadd 
821eb6f0de0SAdrian Chadd 	if (bf == NULL)
822eb6f0de0SAdrian Chadd 		return;
823eb6f0de0SAdrian Chadd 
824eb6f0de0SAdrian Chadd 	ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
825d2da5544SAdrian Chadd 	ath_hal_gettxdesclinkptr(ah, bf_last->bf_lastds, &txq->axq_link);
826eb6f0de0SAdrian Chadd 	ath_hal_txstart(ah, txq->axq_qnum);
827eb6f0de0SAdrian Chadd }
828eb6f0de0SAdrian Chadd 
829eb6f0de0SAdrian Chadd /*
830eb6f0de0SAdrian Chadd  * Hand off a packet to the hardware (or mcast queue.)
831eb6f0de0SAdrian Chadd  *
832eb6f0de0SAdrian Chadd  * The relevant hardware txq should be locked.
833eb6f0de0SAdrian Chadd  */
834eb6f0de0SAdrian Chadd static void
835746bab5bSAdrian Chadd ath_legacy_xmit_handoff(struct ath_softc *sc, struct ath_txq *txq,
836746bab5bSAdrian Chadd     struct ath_buf *bf)
837eb6f0de0SAdrian Chadd {
838eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
839eb6f0de0SAdrian Chadd 
840eb6f0de0SAdrian Chadd 	if (txq->axq_qnum == ATH_TXQ_SWQ)
841eb6f0de0SAdrian Chadd 		ath_tx_handoff_mcast(sc, txq, bf);
842eb6f0de0SAdrian Chadd 	else
843eb6f0de0SAdrian Chadd 		ath_tx_handoff_hw(sc, txq, bf);
844b8e788a5SAdrian Chadd }
845b8e788a5SAdrian Chadd 
84681a82688SAdrian Chadd static int
84781a82688SAdrian Chadd ath_tx_tag_crypto(struct ath_softc *sc, struct ieee80211_node *ni,
848d4365d16SAdrian Chadd     struct mbuf *m0, int iswep, int isfrag, int *hdrlen, int *pktlen,
849d4365d16SAdrian Chadd     int *keyix)
85081a82688SAdrian Chadd {
85112be5b9cSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_XMIT,
85212be5b9cSAdrian Chadd 	    "%s: hdrlen=%d, pktlen=%d, isfrag=%d, iswep=%d, m0=%p\n",
85312be5b9cSAdrian Chadd 	    __func__,
85412be5b9cSAdrian Chadd 	    *hdrlen,
85512be5b9cSAdrian Chadd 	    *pktlen,
85612be5b9cSAdrian Chadd 	    isfrag,
85712be5b9cSAdrian Chadd 	    iswep,
85812be5b9cSAdrian Chadd 	    m0);
85912be5b9cSAdrian Chadd 
86081a82688SAdrian Chadd 	if (iswep) {
86181a82688SAdrian Chadd 		const struct ieee80211_cipher *cip;
86281a82688SAdrian Chadd 		struct ieee80211_key *k;
86381a82688SAdrian Chadd 
86481a82688SAdrian Chadd 		/*
86581a82688SAdrian Chadd 		 * Construct the 802.11 header+trailer for an encrypted
86681a82688SAdrian Chadd 		 * frame. The only reason this can fail is because of an
86781a82688SAdrian Chadd 		 * unknown or unsupported cipher/key type.
86881a82688SAdrian Chadd 		 */
86981a82688SAdrian Chadd 		k = ieee80211_crypto_encap(ni, m0);
87081a82688SAdrian Chadd 		if (k == NULL) {
87181a82688SAdrian Chadd 			/*
87281a82688SAdrian Chadd 			 * This can happen when the key is yanked after the
87381a82688SAdrian Chadd 			 * frame was queued.  Just discard the frame; the
87481a82688SAdrian Chadd 			 * 802.11 layer counts failures and provides
87581a82688SAdrian Chadd 			 * debugging/diagnostics.
87681a82688SAdrian Chadd 			 */
877d4365d16SAdrian Chadd 			return (0);
87881a82688SAdrian Chadd 		}
87981a82688SAdrian Chadd 		/*
88081a82688SAdrian Chadd 		 * Adjust the packet + header lengths for the crypto
88181a82688SAdrian Chadd 		 * additions and calculate the h/w key index.  When
88281a82688SAdrian Chadd 		 * a s/w mic is done the frame will have had any mic
88381a82688SAdrian Chadd 		 * added to it prior to entry so m0->m_pkthdr.len will
88481a82688SAdrian Chadd 		 * account for it. Otherwise we need to add it to the
88581a82688SAdrian Chadd 		 * packet length.
88681a82688SAdrian Chadd 		 */
88781a82688SAdrian Chadd 		cip = k->wk_cipher;
88881a82688SAdrian Chadd 		(*hdrlen) += cip->ic_header;
88981a82688SAdrian Chadd 		(*pktlen) += cip->ic_header + cip->ic_trailer;
89081a82688SAdrian Chadd 		/* NB: frags always have any TKIP MIC done in s/w */
89181a82688SAdrian Chadd 		if ((k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && !isfrag)
89281a82688SAdrian Chadd 			(*pktlen) += cip->ic_miclen;
89381a82688SAdrian Chadd 		(*keyix) = k->wk_keyix;
89481a82688SAdrian Chadd 	} else if (ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) {
89581a82688SAdrian Chadd 		/*
89681a82688SAdrian Chadd 		 * Use station key cache slot, if assigned.
89781a82688SAdrian Chadd 		 */
89881a82688SAdrian Chadd 		(*keyix) = ni->ni_ucastkey.wk_keyix;
89981a82688SAdrian Chadd 		if ((*keyix) == IEEE80211_KEYIX_NONE)
90081a82688SAdrian Chadd 			(*keyix) = HAL_TXKEYIX_INVALID;
90181a82688SAdrian Chadd 	} else
90281a82688SAdrian Chadd 		(*keyix) = HAL_TXKEYIX_INVALID;
90381a82688SAdrian Chadd 
904d4365d16SAdrian Chadd 	return (1);
90581a82688SAdrian Chadd }
90681a82688SAdrian Chadd 
907e2e4a2c2SAdrian Chadd /*
908e2e4a2c2SAdrian Chadd  * Calculate whether interoperability protection is required for
909e2e4a2c2SAdrian Chadd  * this frame.
910e2e4a2c2SAdrian Chadd  *
911e2e4a2c2SAdrian Chadd  * This requires the rate control information be filled in,
912e2e4a2c2SAdrian Chadd  * as the protection requirement depends upon the current
913e2e4a2c2SAdrian Chadd  * operating mode / PHY.
914e2e4a2c2SAdrian Chadd  */
915e2e4a2c2SAdrian Chadd static void
916e2e4a2c2SAdrian Chadd ath_tx_calc_protection(struct ath_softc *sc, struct ath_buf *bf)
917e2e4a2c2SAdrian Chadd {
918e2e4a2c2SAdrian Chadd 	struct ieee80211_frame *wh;
919e2e4a2c2SAdrian Chadd 	uint8_t rix;
920e2e4a2c2SAdrian Chadd 	uint16_t flags;
921e2e4a2c2SAdrian Chadd 	int shortPreamble;
922e2e4a2c2SAdrian Chadd 	const HAL_RATE_TABLE *rt = sc->sc_currates;
923e2e4a2c2SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
924e2e4a2c2SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
925e2e4a2c2SAdrian Chadd 
926e2e4a2c2SAdrian Chadd 	flags = bf->bf_state.bfs_txflags;
927e2e4a2c2SAdrian Chadd 	rix = bf->bf_state.bfs_rc[0].rix;
928e2e4a2c2SAdrian Chadd 	shortPreamble = bf->bf_state.bfs_shpream;
929e2e4a2c2SAdrian Chadd 	wh = mtod(bf->bf_m, struct ieee80211_frame *);
930e2e4a2c2SAdrian Chadd 
931e2e4a2c2SAdrian Chadd 	/*
932e2e4a2c2SAdrian Chadd 	 * If 802.11g protection is enabled, determine whether
933e2e4a2c2SAdrian Chadd 	 * to use RTS/CTS or just CTS.  Note that this is only
934e2e4a2c2SAdrian Chadd 	 * done for OFDM unicast frames.
935e2e4a2c2SAdrian Chadd 	 */
936e2e4a2c2SAdrian Chadd 	if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
937e2e4a2c2SAdrian Chadd 	    rt->info[rix].phy == IEEE80211_T_OFDM &&
938e2e4a2c2SAdrian Chadd 	    (flags & HAL_TXDESC_NOACK) == 0) {
939e2e4a2c2SAdrian Chadd 		bf->bf_state.bfs_doprot = 1;
940e2e4a2c2SAdrian Chadd 		/* XXX fragments must use CCK rates w/ protection */
941e2e4a2c2SAdrian Chadd 		if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) {
942e2e4a2c2SAdrian Chadd 			flags |= HAL_TXDESC_RTSENA;
943e2e4a2c2SAdrian Chadd 		} else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) {
944e2e4a2c2SAdrian Chadd 			flags |= HAL_TXDESC_CTSENA;
945e2e4a2c2SAdrian Chadd 		}
946e2e4a2c2SAdrian Chadd 		/*
947e2e4a2c2SAdrian Chadd 		 * For frags it would be desirable to use the
948e2e4a2c2SAdrian Chadd 		 * highest CCK rate for RTS/CTS.  But stations
949e2e4a2c2SAdrian Chadd 		 * farther away may detect it at a lower CCK rate
950e2e4a2c2SAdrian Chadd 		 * so use the configured protection rate instead
951e2e4a2c2SAdrian Chadd 		 * (for now).
952e2e4a2c2SAdrian Chadd 		 */
953e2e4a2c2SAdrian Chadd 		sc->sc_stats.ast_tx_protect++;
954e2e4a2c2SAdrian Chadd 	}
955e2e4a2c2SAdrian Chadd 
956e2e4a2c2SAdrian Chadd 	/*
957e2e4a2c2SAdrian Chadd 	 * If 11n protection is enabled and it's a HT frame,
958e2e4a2c2SAdrian Chadd 	 * enable RTS.
959e2e4a2c2SAdrian Chadd 	 *
960e2e4a2c2SAdrian Chadd 	 * XXX ic_htprotmode or ic_curhtprotmode?
961e2e4a2c2SAdrian Chadd 	 * XXX should it_htprotmode only matter if ic_curhtprotmode
962e2e4a2c2SAdrian Chadd 	 * XXX indicates it's not a HT pure environment?
963e2e4a2c2SAdrian Chadd 	 */
964e2e4a2c2SAdrian Chadd 	if ((ic->ic_htprotmode == IEEE80211_PROT_RTSCTS) &&
965e2e4a2c2SAdrian Chadd 	    rt->info[rix].phy == IEEE80211_T_HT &&
966e2e4a2c2SAdrian Chadd 	    (flags & HAL_TXDESC_NOACK) == 0) {
967e2e4a2c2SAdrian Chadd 		flags |= HAL_TXDESC_RTSENA;
968e2e4a2c2SAdrian Chadd 		sc->sc_stats.ast_tx_htprotect++;
969e2e4a2c2SAdrian Chadd 	}
970e2e4a2c2SAdrian Chadd 	bf->bf_state.bfs_txflags = flags;
971e2e4a2c2SAdrian Chadd }
972e2e4a2c2SAdrian Chadd 
973e2e4a2c2SAdrian Chadd /*
974e2e4a2c2SAdrian Chadd  * Update the frame duration given the currently selected rate.
975e2e4a2c2SAdrian Chadd  *
976e2e4a2c2SAdrian Chadd  * This also updates the frame duration value, so it will require
977e2e4a2c2SAdrian Chadd  * a DMA flush.
978e2e4a2c2SAdrian Chadd  */
979e2e4a2c2SAdrian Chadd static void
980e2e4a2c2SAdrian Chadd ath_tx_calc_duration(struct ath_softc *sc, struct ath_buf *bf)
981e2e4a2c2SAdrian Chadd {
982e2e4a2c2SAdrian Chadd 	struct ieee80211_frame *wh;
983e2e4a2c2SAdrian Chadd 	uint8_t rix;
984e2e4a2c2SAdrian Chadd 	uint16_t flags;
985e2e4a2c2SAdrian Chadd 	int shortPreamble;
986e2e4a2c2SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
987e2e4a2c2SAdrian Chadd 	const HAL_RATE_TABLE *rt = sc->sc_currates;
988e2e4a2c2SAdrian Chadd 	int isfrag = bf->bf_m->m_flags & M_FRAG;
989e2e4a2c2SAdrian Chadd 
990e2e4a2c2SAdrian Chadd 	flags = bf->bf_state.bfs_txflags;
991e2e4a2c2SAdrian Chadd 	rix = bf->bf_state.bfs_rc[0].rix;
992e2e4a2c2SAdrian Chadd 	shortPreamble = bf->bf_state.bfs_shpream;
993e2e4a2c2SAdrian Chadd 	wh = mtod(bf->bf_m, struct ieee80211_frame *);
994e2e4a2c2SAdrian Chadd 
995e2e4a2c2SAdrian Chadd 	/*
996e2e4a2c2SAdrian Chadd 	 * Calculate duration.  This logically belongs in the 802.11
997e2e4a2c2SAdrian Chadd 	 * layer but it lacks sufficient information to calculate it.
998e2e4a2c2SAdrian Chadd 	 */
999e2e4a2c2SAdrian Chadd 	if ((flags & HAL_TXDESC_NOACK) == 0 &&
1000e2e4a2c2SAdrian Chadd 	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL) {
1001e2e4a2c2SAdrian Chadd 		u_int16_t dur;
1002e2e4a2c2SAdrian Chadd 		if (shortPreamble)
1003e2e4a2c2SAdrian Chadd 			dur = rt->info[rix].spAckDuration;
1004e2e4a2c2SAdrian Chadd 		else
1005e2e4a2c2SAdrian Chadd 			dur = rt->info[rix].lpAckDuration;
1006e2e4a2c2SAdrian Chadd 		if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) {
1007e2e4a2c2SAdrian Chadd 			dur += dur;		/* additional SIFS+ACK */
1008e2e4a2c2SAdrian Chadd 			KASSERT(bf->bf_m->m_nextpkt != NULL, ("no fragment"));
1009e2e4a2c2SAdrian Chadd 			/*
1010e2e4a2c2SAdrian Chadd 			 * Include the size of next fragment so NAV is
1011e2e4a2c2SAdrian Chadd 			 * updated properly.  The last fragment uses only
1012e2e4a2c2SAdrian Chadd 			 * the ACK duration
1013e2e4a2c2SAdrian Chadd 			 */
1014e2e4a2c2SAdrian Chadd 			dur += ath_hal_computetxtime(ah, rt,
1015e2e4a2c2SAdrian Chadd 					bf->bf_m->m_nextpkt->m_pkthdr.len,
1016e2e4a2c2SAdrian Chadd 					rix, shortPreamble);
1017e2e4a2c2SAdrian Chadd 		}
1018e2e4a2c2SAdrian Chadd 		if (isfrag) {
1019e2e4a2c2SAdrian Chadd 			/*
1020e2e4a2c2SAdrian Chadd 			 * Force hardware to use computed duration for next
1021e2e4a2c2SAdrian Chadd 			 * fragment by disabling multi-rate retry which updates
1022e2e4a2c2SAdrian Chadd 			 * duration based on the multi-rate duration table.
1023e2e4a2c2SAdrian Chadd 			 */
1024e2e4a2c2SAdrian Chadd 			bf->bf_state.bfs_ismrr = 0;
1025e2e4a2c2SAdrian Chadd 			bf->bf_state.bfs_try0 = ATH_TXMGTTRY;
1026e2e4a2c2SAdrian Chadd 			/* XXX update bfs_rc[0].try? */
1027e2e4a2c2SAdrian Chadd 		}
1028e2e4a2c2SAdrian Chadd 
1029e2e4a2c2SAdrian Chadd 		/* Update the duration field itself */
1030e2e4a2c2SAdrian Chadd 		*(u_int16_t *)wh->i_dur = htole16(dur);
1031e2e4a2c2SAdrian Chadd 	}
1032e2e4a2c2SAdrian Chadd }
1033e2e4a2c2SAdrian Chadd 
1034e42b5dbaSAdrian Chadd static uint8_t
1035e42b5dbaSAdrian Chadd ath_tx_get_rtscts_rate(struct ath_hal *ah, const HAL_RATE_TABLE *rt,
1036eb6f0de0SAdrian Chadd     int cix, int shortPreamble)
103779f02dbfSAdrian Chadd {
1038e42b5dbaSAdrian Chadd 	uint8_t ctsrate;
1039e42b5dbaSAdrian Chadd 
104079f02dbfSAdrian Chadd 	/*
104179f02dbfSAdrian Chadd 	 * CTS transmit rate is derived from the transmit rate
104279f02dbfSAdrian Chadd 	 * by looking in the h/w rate table.  We must also factor
104379f02dbfSAdrian Chadd 	 * in whether or not a short preamble is to be used.
104479f02dbfSAdrian Chadd 	 */
104579f02dbfSAdrian Chadd 	/* NB: cix is set above where RTS/CTS is enabled */
104679f02dbfSAdrian Chadd 	KASSERT(cix != 0xff, ("cix not setup"));
1047e42b5dbaSAdrian Chadd 	ctsrate = rt->info[cix].rateCode;
1048e42b5dbaSAdrian Chadd 
1049e42b5dbaSAdrian Chadd 	/* XXX this should only matter for legacy rates */
1050e42b5dbaSAdrian Chadd 	if (shortPreamble)
1051e42b5dbaSAdrian Chadd 		ctsrate |= rt->info[cix].shortPreamble;
1052e42b5dbaSAdrian Chadd 
1053d4365d16SAdrian Chadd 	return (ctsrate);
1054e42b5dbaSAdrian Chadd }
1055e42b5dbaSAdrian Chadd 
1056e42b5dbaSAdrian Chadd /*
1057e42b5dbaSAdrian Chadd  * Calculate the RTS/CTS duration for legacy frames.
1058e42b5dbaSAdrian Chadd  */
1059e42b5dbaSAdrian Chadd static int
1060e42b5dbaSAdrian Chadd ath_tx_calc_ctsduration(struct ath_hal *ah, int rix, int cix,
1061e42b5dbaSAdrian Chadd     int shortPreamble, int pktlen, const HAL_RATE_TABLE *rt,
1062e42b5dbaSAdrian Chadd     int flags)
1063e42b5dbaSAdrian Chadd {
1064e42b5dbaSAdrian Chadd 	int ctsduration = 0;
1065e42b5dbaSAdrian Chadd 
1066e42b5dbaSAdrian Chadd 	/* This mustn't be called for HT modes */
1067e42b5dbaSAdrian Chadd 	if (rt->info[cix].phy == IEEE80211_T_HT) {
1068e42b5dbaSAdrian Chadd 		printf("%s: HT rate where it shouldn't be (0x%x)\n",
1069e42b5dbaSAdrian Chadd 		    __func__, rt->info[cix].rateCode);
1070d4365d16SAdrian Chadd 		return (-1);
1071e42b5dbaSAdrian Chadd 	}
1072e42b5dbaSAdrian Chadd 
107379f02dbfSAdrian Chadd 	/*
107479f02dbfSAdrian Chadd 	 * Compute the transmit duration based on the frame
107579f02dbfSAdrian Chadd 	 * size and the size of an ACK frame.  We call into the
107679f02dbfSAdrian Chadd 	 * HAL to do the computation since it depends on the
107779f02dbfSAdrian Chadd 	 * characteristics of the actual PHY being used.
107879f02dbfSAdrian Chadd 	 *
107979f02dbfSAdrian Chadd 	 * NB: CTS is assumed the same size as an ACK so we can
108079f02dbfSAdrian Chadd 	 *     use the precalculated ACK durations.
108179f02dbfSAdrian Chadd 	 */
108279f02dbfSAdrian Chadd 	if (shortPreamble) {
108379f02dbfSAdrian Chadd 		if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
1084e42b5dbaSAdrian Chadd 			ctsduration += rt->info[cix].spAckDuration;
1085e42b5dbaSAdrian Chadd 		ctsduration += ath_hal_computetxtime(ah,
108679f02dbfSAdrian Chadd 			rt, pktlen, rix, AH_TRUE);
108779f02dbfSAdrian Chadd 		if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
1088e42b5dbaSAdrian Chadd 			ctsduration += rt->info[rix].spAckDuration;
108979f02dbfSAdrian Chadd 	} else {
109079f02dbfSAdrian Chadd 		if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
1091e42b5dbaSAdrian Chadd 			ctsduration += rt->info[cix].lpAckDuration;
1092e42b5dbaSAdrian Chadd 		ctsduration += ath_hal_computetxtime(ah,
109379f02dbfSAdrian Chadd 			rt, pktlen, rix, AH_FALSE);
109479f02dbfSAdrian Chadd 		if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
1095e42b5dbaSAdrian Chadd 			ctsduration += rt->info[rix].lpAckDuration;
109679f02dbfSAdrian Chadd 	}
1097e42b5dbaSAdrian Chadd 
1098d4365d16SAdrian Chadd 	return (ctsduration);
109979f02dbfSAdrian Chadd }
110079f02dbfSAdrian Chadd 
1101eb6f0de0SAdrian Chadd /*
1102eb6f0de0SAdrian Chadd  * Update the given ath_buf with updated rts/cts setup and duration
1103eb6f0de0SAdrian Chadd  * values.
1104eb6f0de0SAdrian Chadd  *
1105eb6f0de0SAdrian Chadd  * To support rate lookups for each software retry, the rts/cts rate
1106eb6f0de0SAdrian Chadd  * and cts duration must be re-calculated.
1107eb6f0de0SAdrian Chadd  *
1108eb6f0de0SAdrian Chadd  * This function assumes the RTS/CTS flags have been set as needed;
1109eb6f0de0SAdrian Chadd  * mrr has been disabled; and the rate control lookup has been done.
1110eb6f0de0SAdrian Chadd  *
1111eb6f0de0SAdrian Chadd  * XXX TODO: MRR need only be disabled for the pre-11n NICs.
1112eb6f0de0SAdrian Chadd  * XXX The 11n NICs support per-rate RTS/CTS configuration.
1113eb6f0de0SAdrian Chadd  */
1114eb6f0de0SAdrian Chadd static void
1115eb6f0de0SAdrian Chadd ath_tx_set_rtscts(struct ath_softc *sc, struct ath_buf *bf)
1116eb6f0de0SAdrian Chadd {
1117eb6f0de0SAdrian Chadd 	uint16_t ctsduration = 0;
1118eb6f0de0SAdrian Chadd 	uint8_t ctsrate = 0;
1119eb6f0de0SAdrian Chadd 	uint8_t rix = bf->bf_state.bfs_rc[0].rix;
1120eb6f0de0SAdrian Chadd 	uint8_t cix = 0;
1121eb6f0de0SAdrian Chadd 	const HAL_RATE_TABLE *rt = sc->sc_currates;
1122eb6f0de0SAdrian Chadd 
1123eb6f0de0SAdrian Chadd 	/*
1124eb6f0de0SAdrian Chadd 	 * No RTS/CTS enabled? Don't bother.
1125eb6f0de0SAdrian Chadd 	 */
1126875a9451SAdrian Chadd 	if ((bf->bf_state.bfs_txflags &
1127eb6f0de0SAdrian Chadd 	    (HAL_TXDESC_RTSENA | HAL_TXDESC_CTSENA)) == 0) {
1128eb6f0de0SAdrian Chadd 		/* XXX is this really needed? */
1129eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_ctsrate = 0;
1130eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_ctsduration = 0;
1131eb6f0de0SAdrian Chadd 		return;
1132eb6f0de0SAdrian Chadd 	}
1133eb6f0de0SAdrian Chadd 
1134eb6f0de0SAdrian Chadd 	/*
1135eb6f0de0SAdrian Chadd 	 * If protection is enabled, use the protection rix control
1136eb6f0de0SAdrian Chadd 	 * rate. Otherwise use the rate0 control rate.
1137eb6f0de0SAdrian Chadd 	 */
1138eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_doprot)
1139eb6f0de0SAdrian Chadd 		rix = sc->sc_protrix;
1140eb6f0de0SAdrian Chadd 	else
1141eb6f0de0SAdrian Chadd 		rix = bf->bf_state.bfs_rc[0].rix;
1142eb6f0de0SAdrian Chadd 
1143eb6f0de0SAdrian Chadd 	/*
1144eb6f0de0SAdrian Chadd 	 * If the raw path has hard-coded ctsrate0 to something,
1145eb6f0de0SAdrian Chadd 	 * use it.
1146eb6f0de0SAdrian Chadd 	 */
1147eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_ctsrate0 != 0)
1148eb6f0de0SAdrian Chadd 		cix = ath_tx_findrix(sc, bf->bf_state.bfs_ctsrate0);
1149eb6f0de0SAdrian Chadd 	else
1150eb6f0de0SAdrian Chadd 		/* Control rate from above */
1151eb6f0de0SAdrian Chadd 		cix = rt->info[rix].controlRate;
1152eb6f0de0SAdrian Chadd 
1153eb6f0de0SAdrian Chadd 	/* Calculate the rtscts rate for the given cix */
1154eb6f0de0SAdrian Chadd 	ctsrate = ath_tx_get_rtscts_rate(sc->sc_ah, rt, cix,
1155eb6f0de0SAdrian Chadd 	    bf->bf_state.bfs_shpream);
1156eb6f0de0SAdrian Chadd 
1157eb6f0de0SAdrian Chadd 	/* The 11n chipsets do ctsduration calculations for you */
1158eb6f0de0SAdrian Chadd 	if (! ath_tx_is_11n(sc))
1159eb6f0de0SAdrian Chadd 		ctsduration = ath_tx_calc_ctsduration(sc->sc_ah, rix, cix,
1160eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_shpream, bf->bf_state.bfs_pktlen,
1161875a9451SAdrian Chadd 		    rt, bf->bf_state.bfs_txflags);
1162eb6f0de0SAdrian Chadd 
1163eb6f0de0SAdrian Chadd 	/* Squirrel away in ath_buf */
1164eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsrate = ctsrate;
1165eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsduration = ctsduration;
1166eb6f0de0SAdrian Chadd 
1167eb6f0de0SAdrian Chadd 	/*
1168eb6f0de0SAdrian Chadd 	 * Must disable multi-rate retry when using RTS/CTS.
1169eb6f0de0SAdrian Chadd 	 */
1170af017101SAdrian Chadd 	if (!sc->sc_mrrprot) {
1171eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_ismrr = 0;
1172eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_try0 =
1173eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_rc[0].tries = ATH_TXMGTTRY; /* XXX ew */
1174eb6f0de0SAdrian Chadd 	}
1175af017101SAdrian Chadd }
1176eb6f0de0SAdrian Chadd 
1177eb6f0de0SAdrian Chadd /*
1178eb6f0de0SAdrian Chadd  * Setup the descriptor chain for a normal or fast-frame
1179eb6f0de0SAdrian Chadd  * frame.
118046634305SAdrian Chadd  *
118146634305SAdrian Chadd  * XXX TODO: extend to include the destination hardware QCU ID.
118246634305SAdrian Chadd  * Make sure that is correct.  Make sure that when being added
118346634305SAdrian Chadd  * to the mcastq, the CABQ QCUID is set or things will get a bit
118446634305SAdrian Chadd  * odd.
1185eb6f0de0SAdrian Chadd  */
1186eb6f0de0SAdrian Chadd static void
1187eb6f0de0SAdrian Chadd ath_tx_setds(struct ath_softc *sc, struct ath_buf *bf)
1188eb6f0de0SAdrian Chadd {
1189eb6f0de0SAdrian Chadd 	struct ath_desc *ds = bf->bf_desc;
1190eb6f0de0SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
1191eb6f0de0SAdrian Chadd 
1192eb6f0de0SAdrian Chadd 	ath_hal_setuptxdesc(ah, ds
1193eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_pktlen	/* packet length */
1194eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_hdrlen	/* header length */
1195eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_atype	/* Atheros packet type */
1196eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_txpower	/* txpower */
1197eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_txrate0
1198eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_try0		/* series 0 rate/tries */
1199eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_keyix	/* key cache index */
1200eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_txantenna	/* antenna mode */
1201875a9451SAdrian Chadd 		, bf->bf_state.bfs_txflags	/* flags */
1202eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_ctsrate	/* rts/cts rate */
1203eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_ctsduration	/* rts/cts duration */
1204eb6f0de0SAdrian Chadd 	);
1205eb6f0de0SAdrian Chadd 
1206eb6f0de0SAdrian Chadd 	/*
1207eb6f0de0SAdrian Chadd 	 * This will be overriden when the descriptor chain is written.
1208eb6f0de0SAdrian Chadd 	 */
1209eb6f0de0SAdrian Chadd 	bf->bf_lastds = ds;
1210eb6f0de0SAdrian Chadd 	bf->bf_last = bf;
1211eb6f0de0SAdrian Chadd 
1212d34a7347SAdrian Chadd 	/* Set rate control and descriptor chain for this frame */
1213d34a7347SAdrian Chadd 	ath_tx_set_ratectrl(sc, bf->bf_node, bf);
1214d34a7347SAdrian Chadd 	ath_tx_chaindesclist(sc, bf);
1215eb6f0de0SAdrian Chadd }
1216eb6f0de0SAdrian Chadd 
1217eb6f0de0SAdrian Chadd /*
1218eb6f0de0SAdrian Chadd  * Do a rate lookup.
1219eb6f0de0SAdrian Chadd  *
1220eb6f0de0SAdrian Chadd  * This performs a rate lookup for the given ath_buf only if it's required.
1221eb6f0de0SAdrian Chadd  * Non-data frames and raw frames don't require it.
1222eb6f0de0SAdrian Chadd  *
1223eb6f0de0SAdrian Chadd  * This populates the primary and MRR entries; MRR values are
1224eb6f0de0SAdrian Chadd  * then disabled later on if something requires it (eg RTS/CTS on
1225eb6f0de0SAdrian Chadd  * pre-11n chipsets.
1226eb6f0de0SAdrian Chadd  *
1227eb6f0de0SAdrian Chadd  * This needs to be done before the RTS/CTS fields are calculated
1228eb6f0de0SAdrian Chadd  * as they may depend upon the rate chosen.
1229eb6f0de0SAdrian Chadd  */
1230eb6f0de0SAdrian Chadd static void
1231eb6f0de0SAdrian Chadd ath_tx_do_ratelookup(struct ath_softc *sc, struct ath_buf *bf)
1232eb6f0de0SAdrian Chadd {
1233eb6f0de0SAdrian Chadd 	uint8_t rate, rix;
1234eb6f0de0SAdrian Chadd 	int try0;
1235eb6f0de0SAdrian Chadd 
1236eb6f0de0SAdrian Chadd 	if (! bf->bf_state.bfs_doratelookup)
1237eb6f0de0SAdrian Chadd 		return;
1238eb6f0de0SAdrian Chadd 
1239eb6f0de0SAdrian Chadd 	/* Get rid of any previous state */
1240eb6f0de0SAdrian Chadd 	bzero(bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
1241eb6f0de0SAdrian Chadd 
1242eb6f0de0SAdrian Chadd 	ATH_NODE_LOCK(ATH_NODE(bf->bf_node));
1243eb6f0de0SAdrian Chadd 	ath_rate_findrate(sc, ATH_NODE(bf->bf_node), bf->bf_state.bfs_shpream,
1244eb6f0de0SAdrian Chadd 	    bf->bf_state.bfs_pktlen, &rix, &try0, &rate);
1245eb6f0de0SAdrian Chadd 
1246eb6f0de0SAdrian Chadd 	/* In case MRR is disabled, make sure rc[0] is setup correctly */
1247eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].rix = rix;
1248eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].ratecode = rate;
1249eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].tries = try0;
1250eb6f0de0SAdrian Chadd 
1251eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_ismrr && try0 != ATH_TXMAXTRY)
1252eb6f0de0SAdrian Chadd 		ath_rate_getxtxrates(sc, ATH_NODE(bf->bf_node), rix,
1253eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_rc);
1254eb6f0de0SAdrian Chadd 	ATH_NODE_UNLOCK(ATH_NODE(bf->bf_node));
1255eb6f0de0SAdrian Chadd 
1256eb6f0de0SAdrian Chadd 	sc->sc_txrix = rix;	/* for LED blinking */
1257eb6f0de0SAdrian Chadd 	sc->sc_lastdatarix = rix;	/* for fast frames */
1258eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_try0 = try0;
1259eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txrate0 = rate;
1260eb6f0de0SAdrian Chadd }
1261eb6f0de0SAdrian Chadd 
1262eb6f0de0SAdrian Chadd /*
1263*0c54de88SAdrian Chadd  * Update the CLRDMASK bit in the ath_buf if it needs to be set.
1264*0c54de88SAdrian Chadd  */
1265*0c54de88SAdrian Chadd static void
1266*0c54de88SAdrian Chadd ath_tx_update_clrdmask(struct ath_softc *sc, struct ath_tid *tid,
1267*0c54de88SAdrian Chadd     struct ath_buf *bf)
1268*0c54de88SAdrian Chadd {
1269*0c54de88SAdrian Chadd 
1270*0c54de88SAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
1271*0c54de88SAdrian Chadd 
1272*0c54de88SAdrian Chadd 	if (tid->clrdmask == 1) {
1273*0c54de88SAdrian Chadd 		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
1274*0c54de88SAdrian Chadd 		tid->clrdmask = 0;
1275*0c54de88SAdrian Chadd 	}
1276*0c54de88SAdrian Chadd }
1277*0c54de88SAdrian Chadd 
1278*0c54de88SAdrian Chadd /*
1279eb6f0de0SAdrian Chadd  * Transmit the given frame to the hardware.
1280eb6f0de0SAdrian Chadd  *
1281eb6f0de0SAdrian Chadd  * The frame must already be setup; rate control must already have
1282eb6f0de0SAdrian Chadd  * been done.
1283eb6f0de0SAdrian Chadd  *
1284eb6f0de0SAdrian Chadd  * XXX since the TXQ lock is being held here (and I dislike holding
1285eb6f0de0SAdrian Chadd  * it for this long when not doing software aggregation), later on
1286eb6f0de0SAdrian Chadd  * break this function into "setup_normal" and "xmit_normal". The
1287eb6f0de0SAdrian Chadd  * lock only needs to be held for the ath_tx_handoff call.
1288eb6f0de0SAdrian Chadd  */
1289eb6f0de0SAdrian Chadd static void
1290eb6f0de0SAdrian Chadd ath_tx_xmit_normal(struct ath_softc *sc, struct ath_txq *txq,
1291eb6f0de0SAdrian Chadd     struct ath_buf *bf)
1292eb6f0de0SAdrian Chadd {
1293*0c54de88SAdrian Chadd 	struct ath_node *an = ATH_NODE(bf->bf_node);
1294*0c54de88SAdrian Chadd 	struct ath_tid *tid = &an->an_tid[bf->bf_state.bfs_tid];
1295eb6f0de0SAdrian Chadd 
1296eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
1297eb6f0de0SAdrian Chadd 
1298*0c54de88SAdrian Chadd 	/*
1299*0c54de88SAdrian Chadd 	 * For now, just enable CLRDMASK. ath_tx_xmit_normal() does
1300*0c54de88SAdrian Chadd 	 * set a completion handler however it doesn't (yet) properly
1301*0c54de88SAdrian Chadd 	 * handle the strict ordering requirements needed for normal,
1302*0c54de88SAdrian Chadd 	 * non-aggregate session frames.
1303*0c54de88SAdrian Chadd 	 *
1304*0c54de88SAdrian Chadd 	 * Once this is implemented, only set CLRDMASK like this for
1305*0c54de88SAdrian Chadd 	 * frames that must go out - eg management/raw frames.
1306*0c54de88SAdrian Chadd 	 */
1307*0c54de88SAdrian Chadd 	bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
1308*0c54de88SAdrian Chadd 
1309*0c54de88SAdrian Chadd 	/* See if clrdmask needs to be set */
1310*0c54de88SAdrian Chadd 	ath_tx_update_clrdmask(sc, tid, bf);
1311*0c54de88SAdrian Chadd 
1312eb6f0de0SAdrian Chadd 	/* Setup the descriptor before handoff */
1313eb6f0de0SAdrian Chadd 	ath_tx_do_ratelookup(sc, bf);
1314e2e4a2c2SAdrian Chadd 	ath_tx_calc_duration(sc, bf);
1315e2e4a2c2SAdrian Chadd 	ath_tx_calc_protection(sc, bf);
1316eb6f0de0SAdrian Chadd 	ath_tx_set_rtscts(sc, bf);
1317e2e4a2c2SAdrian Chadd 	ath_tx_rate_fill_rcflags(sc, bf);
1318eb6f0de0SAdrian Chadd 	ath_tx_setds(sc, bf);
1319eb6f0de0SAdrian Chadd 
1320*0c54de88SAdrian Chadd 	/* Track per-TID hardware queue depth correctly */
1321*0c54de88SAdrian Chadd 	tid->hwq_depth++;
1322*0c54de88SAdrian Chadd 
1323*0c54de88SAdrian Chadd 	/* Assign the completion handler */
1324*0c54de88SAdrian Chadd 	bf->bf_comp = ath_tx_normal_comp;
13254e81f27cSAdrian Chadd 
1326eb6f0de0SAdrian Chadd 	/* Hand off to hardware */
1327eb6f0de0SAdrian Chadd 	ath_tx_handoff(sc, txq, bf);
1328eb6f0de0SAdrian Chadd }
1329eb6f0de0SAdrian Chadd 
1330d05b576dSAdrian Chadd /*
1331d05b576dSAdrian Chadd  * Do the basic frame setup stuff that's required before the frame
1332d05b576dSAdrian Chadd  * is added to a software queue.
1333d05b576dSAdrian Chadd  *
1334d05b576dSAdrian Chadd  * All frames get mostly the same treatment and it's done once.
1335d05b576dSAdrian Chadd  * Retransmits fiddle with things like the rate control setup,
1336d05b576dSAdrian Chadd  * setting the retransmit bit in the packet; doing relevant DMA/bus
1337d05b576dSAdrian Chadd  * syncing and relinking it (back) into the hardware TX queue.
1338d05b576dSAdrian Chadd  *
1339d05b576dSAdrian Chadd  * Note that this may cause the mbuf to be reallocated, so
1340d05b576dSAdrian Chadd  * m0 may not be valid.
1341d05b576dSAdrian Chadd  */
1342eb6f0de0SAdrian Chadd static int
1343eb6f0de0SAdrian Chadd ath_tx_normal_setup(struct ath_softc *sc, struct ieee80211_node *ni,
1344b43facbfSAdrian Chadd     struct ath_buf *bf, struct mbuf *m0, struct ath_txq *txq)
1345b8e788a5SAdrian Chadd {
1346b8e788a5SAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
1347b8e788a5SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
1348b8e788a5SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
1349b8e788a5SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
1350b8e788a5SAdrian Chadd 	const struct chanAccParams *cap = &ic->ic_wme.wme_chanParams;
1351b8e788a5SAdrian Chadd 	int error, iswep, ismcast, isfrag, ismrr;
1352eb6f0de0SAdrian Chadd 	int keyix, hdrlen, pktlen, try0 = 0;
1353eb6f0de0SAdrian Chadd 	u_int8_t rix = 0, txrate = 0;
1354b8e788a5SAdrian Chadd 	struct ath_desc *ds;
1355b8e788a5SAdrian Chadd 	struct ieee80211_frame *wh;
1356eb6f0de0SAdrian Chadd 	u_int subtype, flags;
1357b8e788a5SAdrian Chadd 	HAL_PKT_TYPE atype;
1358b8e788a5SAdrian Chadd 	const HAL_RATE_TABLE *rt;
1359b8e788a5SAdrian Chadd 	HAL_BOOL shortPreamble;
1360b8e788a5SAdrian Chadd 	struct ath_node *an;
1361b8e788a5SAdrian Chadd 	u_int pri;
1362b8e788a5SAdrian Chadd 
13637561cb5cSAdrian Chadd 	/*
13647561cb5cSAdrian Chadd 	 * To ensure that both sequence numbers and the CCMP PN handling
13657561cb5cSAdrian Chadd 	 * is "correct", make sure that the relevant TID queue is locked.
13667561cb5cSAdrian Chadd 	 * Otherwise the CCMP PN and seqno may appear out of order, causing
13677561cb5cSAdrian Chadd 	 * re-ordered frames to have out of order CCMP PN's, resulting
13687561cb5cSAdrian Chadd 	 * in many, many frame drops.
13697561cb5cSAdrian Chadd 	 */
13707561cb5cSAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
13717561cb5cSAdrian Chadd 
1372b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1373b8e788a5SAdrian Chadd 	iswep = wh->i_fc[1] & IEEE80211_FC1_WEP;
1374b8e788a5SAdrian Chadd 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1375b8e788a5SAdrian Chadd 	isfrag = m0->m_flags & M_FRAG;
1376b8e788a5SAdrian Chadd 	hdrlen = ieee80211_anyhdrsize(wh);
1377b8e788a5SAdrian Chadd 	/*
1378b8e788a5SAdrian Chadd 	 * Packet length must not include any
1379b8e788a5SAdrian Chadd 	 * pad bytes; deduct them here.
1380b8e788a5SAdrian Chadd 	 */
1381b8e788a5SAdrian Chadd 	pktlen = m0->m_pkthdr.len - (hdrlen & 3);
1382b8e788a5SAdrian Chadd 
138381a82688SAdrian Chadd 	/* Handle encryption twiddling if needed */
1384eb6f0de0SAdrian Chadd 	if (! ath_tx_tag_crypto(sc, ni, m0, iswep, isfrag, &hdrlen,
1385eb6f0de0SAdrian Chadd 	    &pktlen, &keyix)) {
1386b8e788a5SAdrian Chadd 		ath_freetx(m0);
1387b8e788a5SAdrian Chadd 		return EIO;
1388b8e788a5SAdrian Chadd 	}
1389b8e788a5SAdrian Chadd 
1390b8e788a5SAdrian Chadd 	/* packet header may have moved, reset our local pointer */
1391b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1392b8e788a5SAdrian Chadd 
1393b8e788a5SAdrian Chadd 	pktlen += IEEE80211_CRC_LEN;
1394b8e788a5SAdrian Chadd 
1395b8e788a5SAdrian Chadd 	/*
1396b8e788a5SAdrian Chadd 	 * Load the DMA map so any coalescing is done.  This
1397b8e788a5SAdrian Chadd 	 * also calculates the number of descriptors we need.
1398b8e788a5SAdrian Chadd 	 */
1399b8e788a5SAdrian Chadd 	error = ath_tx_dmasetup(sc, bf, m0);
1400b8e788a5SAdrian Chadd 	if (error != 0)
1401b8e788a5SAdrian Chadd 		return error;
1402b8e788a5SAdrian Chadd 	bf->bf_node = ni;			/* NB: held reference */
1403b8e788a5SAdrian Chadd 	m0 = bf->bf_m;				/* NB: may have changed */
1404b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1405b8e788a5SAdrian Chadd 
1406b8e788a5SAdrian Chadd 	/* setup descriptors */
1407b8e788a5SAdrian Chadd 	ds = bf->bf_desc;
1408b8e788a5SAdrian Chadd 	rt = sc->sc_currates;
1409b8e788a5SAdrian Chadd 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
1410b8e788a5SAdrian Chadd 
1411b8e788a5SAdrian Chadd 	/*
1412b8e788a5SAdrian Chadd 	 * NB: the 802.11 layer marks whether or not we should
1413b8e788a5SAdrian Chadd 	 * use short preamble based on the current mode and
1414b8e788a5SAdrian Chadd 	 * negotiated parameters.
1415b8e788a5SAdrian Chadd 	 */
1416b8e788a5SAdrian Chadd 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1417b8e788a5SAdrian Chadd 	    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
1418b8e788a5SAdrian Chadd 		shortPreamble = AH_TRUE;
1419b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_shortpre++;
1420b8e788a5SAdrian Chadd 	} else {
1421b8e788a5SAdrian Chadd 		shortPreamble = AH_FALSE;
1422b8e788a5SAdrian Chadd 	}
1423b8e788a5SAdrian Chadd 
1424b8e788a5SAdrian Chadd 	an = ATH_NODE(ni);
14254e81f27cSAdrian Chadd 	//flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for crypto errs */
14264e81f27cSAdrian Chadd 	flags = 0;
1427b8e788a5SAdrian Chadd 	ismrr = 0;				/* default no multi-rate retry*/
1428b8e788a5SAdrian Chadd 	pri = M_WME_GETAC(m0);			/* honor classification */
1429b8e788a5SAdrian Chadd 	/* XXX use txparams instead of fixed values */
1430b8e788a5SAdrian Chadd 	/*
1431b8e788a5SAdrian Chadd 	 * Calculate Atheros packet type from IEEE80211 packet header,
1432b8e788a5SAdrian Chadd 	 * setup for rate calculations, and select h/w transmit queue.
1433b8e788a5SAdrian Chadd 	 */
1434b8e788a5SAdrian Chadd 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1435b8e788a5SAdrian Chadd 	case IEEE80211_FC0_TYPE_MGT:
1436b8e788a5SAdrian Chadd 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1437b8e788a5SAdrian Chadd 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON)
1438b8e788a5SAdrian Chadd 			atype = HAL_PKT_TYPE_BEACON;
1439b8e788a5SAdrian Chadd 		else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1440b8e788a5SAdrian Chadd 			atype = HAL_PKT_TYPE_PROBE_RESP;
1441b8e788a5SAdrian Chadd 		else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM)
1442b8e788a5SAdrian Chadd 			atype = HAL_PKT_TYPE_ATIM;
1443b8e788a5SAdrian Chadd 		else
1444b8e788a5SAdrian Chadd 			atype = HAL_PKT_TYPE_NORMAL;	/* XXX */
1445b8e788a5SAdrian Chadd 		rix = an->an_mgmtrix;
1446b8e788a5SAdrian Chadd 		txrate = rt->info[rix].rateCode;
1447b8e788a5SAdrian Chadd 		if (shortPreamble)
1448b8e788a5SAdrian Chadd 			txrate |= rt->info[rix].shortPreamble;
1449b8e788a5SAdrian Chadd 		try0 = ATH_TXMGTTRY;
1450b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_INTREQ;	/* force interrupt */
1451b8e788a5SAdrian Chadd 		break;
1452b8e788a5SAdrian Chadd 	case IEEE80211_FC0_TYPE_CTL:
1453b8e788a5SAdrian Chadd 		atype = HAL_PKT_TYPE_PSPOLL;	/* stop setting of duration */
1454b8e788a5SAdrian Chadd 		rix = an->an_mgmtrix;
1455b8e788a5SAdrian Chadd 		txrate = rt->info[rix].rateCode;
1456b8e788a5SAdrian Chadd 		if (shortPreamble)
1457b8e788a5SAdrian Chadd 			txrate |= rt->info[rix].shortPreamble;
1458b8e788a5SAdrian Chadd 		try0 = ATH_TXMGTTRY;
1459b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_INTREQ;	/* force interrupt */
1460b8e788a5SAdrian Chadd 		break;
1461b8e788a5SAdrian Chadd 	case IEEE80211_FC0_TYPE_DATA:
1462b8e788a5SAdrian Chadd 		atype = HAL_PKT_TYPE_NORMAL;		/* default */
1463b8e788a5SAdrian Chadd 		/*
1464b8e788a5SAdrian Chadd 		 * Data frames: multicast frames go out at a fixed rate,
1465b8e788a5SAdrian Chadd 		 * EAPOL frames use the mgmt frame rate; otherwise consult
1466b8e788a5SAdrian Chadd 		 * the rate control module for the rate to use.
1467b8e788a5SAdrian Chadd 		 */
1468b8e788a5SAdrian Chadd 		if (ismcast) {
1469b8e788a5SAdrian Chadd 			rix = an->an_mcastrix;
1470b8e788a5SAdrian Chadd 			txrate = rt->info[rix].rateCode;
1471b8e788a5SAdrian Chadd 			if (shortPreamble)
1472b8e788a5SAdrian Chadd 				txrate |= rt->info[rix].shortPreamble;
1473b8e788a5SAdrian Chadd 			try0 = 1;
1474b8e788a5SAdrian Chadd 		} else if (m0->m_flags & M_EAPOL) {
1475b8e788a5SAdrian Chadd 			/* XXX? maybe always use long preamble? */
1476b8e788a5SAdrian Chadd 			rix = an->an_mgmtrix;
1477b8e788a5SAdrian Chadd 			txrate = rt->info[rix].rateCode;
1478b8e788a5SAdrian Chadd 			if (shortPreamble)
1479b8e788a5SAdrian Chadd 				txrate |= rt->info[rix].shortPreamble;
1480b8e788a5SAdrian Chadd 			try0 = ATH_TXMAXTRY;	/* XXX?too many? */
1481b8e788a5SAdrian Chadd 		} else {
1482eb6f0de0SAdrian Chadd 			/*
1483eb6f0de0SAdrian Chadd 			 * Do rate lookup on each TX, rather than using
1484eb6f0de0SAdrian Chadd 			 * the hard-coded TX information decided here.
1485eb6f0de0SAdrian Chadd 			 */
1486b8e788a5SAdrian Chadd 			ismrr = 1;
1487eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_doratelookup = 1;
1488b8e788a5SAdrian Chadd 		}
1489b8e788a5SAdrian Chadd 		if (cap->cap_wmeParams[pri].wmep_noackPolicy)
1490b8e788a5SAdrian Chadd 			flags |= HAL_TXDESC_NOACK;
1491b8e788a5SAdrian Chadd 		break;
1492b8e788a5SAdrian Chadd 	default:
1493b8e788a5SAdrian Chadd 		if_printf(ifp, "bogus frame type 0x%x (%s)\n",
1494b8e788a5SAdrian Chadd 			wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__);
1495b8e788a5SAdrian Chadd 		/* XXX statistic */
1496b8e788a5SAdrian Chadd 		ath_freetx(m0);
1497b8e788a5SAdrian Chadd 		return EIO;
1498b8e788a5SAdrian Chadd 	}
1499b8e788a5SAdrian Chadd 
1500447fd44aSAdrian Chadd 	/*
1501447fd44aSAdrian Chadd 	 * There are two known scenarios where the frame AC doesn't match
1502447fd44aSAdrian Chadd 	 * what the destination TXQ is.
1503447fd44aSAdrian Chadd 	 *
1504447fd44aSAdrian Chadd 	 * + non-QoS frames (eg management?) that the net80211 stack has
1505447fd44aSAdrian Chadd 	 *   assigned a higher AC to, but since it's a non-QoS TID, it's
1506447fd44aSAdrian Chadd 	 *   being thrown into TID 16.  TID 16 gets the AC_BE queue.
1507447fd44aSAdrian Chadd 	 *   It's quite possible that management frames should just be
1508447fd44aSAdrian Chadd 	 *   direct dispatched to hardware rather than go via the software
1509447fd44aSAdrian Chadd 	 *   queue; that should be investigated in the future.  There are
1510447fd44aSAdrian Chadd 	 *   some specific scenarios where this doesn't make sense, mostly
1511447fd44aSAdrian Chadd 	 *   surrounding ADDBA request/response - hence why that is special
1512447fd44aSAdrian Chadd 	 *   cased.
1513447fd44aSAdrian Chadd 	 *
1514447fd44aSAdrian Chadd 	 * + Multicast frames going into the VAP mcast queue.  That shows up
1515447fd44aSAdrian Chadd 	 *   as "TXQ 11".
1516447fd44aSAdrian Chadd 	 *
1517447fd44aSAdrian Chadd 	 * This driver should eventually support separate TID and TXQ locking,
1518447fd44aSAdrian Chadd 	 * allowing for arbitrary AC frames to appear on arbitrary software
1519447fd44aSAdrian Chadd 	 * queues, being queued to the "correct" hardware queue when needed.
1520447fd44aSAdrian Chadd 	 */
1521447fd44aSAdrian Chadd #if 0
15226deb7f32SAdrian Chadd 	if (txq != sc->sc_ac2q[pri]) {
15236deb7f32SAdrian Chadd 		device_printf(sc->sc_dev,
15246deb7f32SAdrian Chadd 		    "%s: txq=%p (%d), pri=%d, pri txq=%p (%d)\n",
15256deb7f32SAdrian Chadd 		    __func__,
15266deb7f32SAdrian Chadd 		    txq,
15276deb7f32SAdrian Chadd 		    txq->axq_qnum,
15286deb7f32SAdrian Chadd 		    pri,
15296deb7f32SAdrian Chadd 		    sc->sc_ac2q[pri],
15306deb7f32SAdrian Chadd 		    sc->sc_ac2q[pri]->axq_qnum);
15316deb7f32SAdrian Chadd 	}
1532447fd44aSAdrian Chadd #endif
15336deb7f32SAdrian Chadd 
1534b8e788a5SAdrian Chadd 	/*
1535b8e788a5SAdrian Chadd 	 * Calculate miscellaneous flags.
1536b8e788a5SAdrian Chadd 	 */
1537b8e788a5SAdrian Chadd 	if (ismcast) {
1538b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_NOACK;	/* no ack on broad/multicast */
1539b8e788a5SAdrian Chadd 	} else if (pktlen > vap->iv_rtsthreshold &&
1540b8e788a5SAdrian Chadd 	    (ni->ni_ath_flags & IEEE80211_NODE_FF) == 0) {
1541b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_RTSENA;	/* RTS based on frame length */
1542b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_rts++;
1543b8e788a5SAdrian Chadd 	}
1544b8e788a5SAdrian Chadd 	if (flags & HAL_TXDESC_NOACK)		/* NB: avoid double counting */
1545b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_noack++;
1546b8e788a5SAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
1547b8e788a5SAdrian Chadd 	if (sc->sc_tdma && (flags & HAL_TXDESC_NOACK) == 0) {
1548b8e788a5SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_TDMA,
1549b8e788a5SAdrian Chadd 		    "%s: discard frame, ACK required w/ TDMA\n", __func__);
1550b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tdma_ack++;
1551b8e788a5SAdrian Chadd 		ath_freetx(m0);
1552b8e788a5SAdrian Chadd 		return EIO;
1553b8e788a5SAdrian Chadd 	}
1554b8e788a5SAdrian Chadd #endif
1555b8e788a5SAdrian Chadd 
1556b8e788a5SAdrian Chadd 	/*
1557eb6f0de0SAdrian Chadd 	 * Determine if a tx interrupt should be generated for
1558eb6f0de0SAdrian Chadd 	 * this descriptor.  We take a tx interrupt to reap
1559eb6f0de0SAdrian Chadd 	 * descriptors when the h/w hits an EOL condition or
1560eb6f0de0SAdrian Chadd 	 * when the descriptor is specifically marked to generate
1561eb6f0de0SAdrian Chadd 	 * an interrupt.  We periodically mark descriptors in this
1562eb6f0de0SAdrian Chadd 	 * way to insure timely replenishing of the supply needed
1563eb6f0de0SAdrian Chadd 	 * for sending frames.  Defering interrupts reduces system
1564eb6f0de0SAdrian Chadd 	 * load and potentially allows more concurrent work to be
1565eb6f0de0SAdrian Chadd 	 * done but if done to aggressively can cause senders to
1566eb6f0de0SAdrian Chadd 	 * backup.
1567eb6f0de0SAdrian Chadd 	 *
1568eb6f0de0SAdrian Chadd 	 * NB: use >= to deal with sc_txintrperiod changing
1569eb6f0de0SAdrian Chadd 	 *     dynamically through sysctl.
1570b8e788a5SAdrian Chadd 	 */
1571eb6f0de0SAdrian Chadd 	if (flags & HAL_TXDESC_INTREQ) {
1572eb6f0de0SAdrian Chadd 		txq->axq_intrcnt = 0;
1573eb6f0de0SAdrian Chadd 	} else if (++txq->axq_intrcnt >= sc->sc_txintrperiod) {
1574eb6f0de0SAdrian Chadd 		flags |= HAL_TXDESC_INTREQ;
1575eb6f0de0SAdrian Chadd 		txq->axq_intrcnt = 0;
1576eb6f0de0SAdrian Chadd 	}
1577e42b5dbaSAdrian Chadd 
1578eb6f0de0SAdrian Chadd 	/* This point forward is actual TX bits */
1579b8e788a5SAdrian Chadd 
1580b8e788a5SAdrian Chadd 	/*
1581b8e788a5SAdrian Chadd 	 * At this point we are committed to sending the frame
1582b8e788a5SAdrian Chadd 	 * and we don't need to look at m_nextpkt; clear it in
1583b8e788a5SAdrian Chadd 	 * case this frame is part of frag chain.
1584b8e788a5SAdrian Chadd 	 */
1585b8e788a5SAdrian Chadd 	m0->m_nextpkt = NULL;
1586b8e788a5SAdrian Chadd 
1587b8e788a5SAdrian Chadd 	if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
1588b8e788a5SAdrian Chadd 		ieee80211_dump_pkt(ic, mtod(m0, const uint8_t *), m0->m_len,
1589b8e788a5SAdrian Chadd 		    sc->sc_hwmap[rix].ieeerate, -1);
1590b8e788a5SAdrian Chadd 
1591b8e788a5SAdrian Chadd 	if (ieee80211_radiotap_active_vap(vap)) {
1592b8e788a5SAdrian Chadd 		u_int64_t tsf = ath_hal_gettsf64(ah);
1593b8e788a5SAdrian Chadd 
1594b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_tsf = htole64(tsf);
1595b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
1596b8e788a5SAdrian Chadd 		if (iswep)
1597b8e788a5SAdrian Chadd 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
1598b8e788a5SAdrian Chadd 		if (isfrag)
1599b8e788a5SAdrian Chadd 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
1600b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
1601b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_txpower = ni->ni_txpower;
1602b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
1603b8e788a5SAdrian Chadd 
1604b8e788a5SAdrian Chadd 		ieee80211_radiotap_tx(vap, m0);
1605b8e788a5SAdrian Chadd 	}
1606b8e788a5SAdrian Chadd 
1607eb6f0de0SAdrian Chadd 	/* Blank the legacy rate array */
1608eb6f0de0SAdrian Chadd 	bzero(&bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
1609c1782ce0SAdrian Chadd 
1610b8e788a5SAdrian Chadd 	/*
1611eb6f0de0SAdrian Chadd 	 * ath_buf_set_rate needs at least one rate/try to setup
1612eb6f0de0SAdrian Chadd 	 * the rate scenario.
1613b8e788a5SAdrian Chadd 	 */
1614eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].rix = rix;
1615eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].tries = try0;
1616eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].ratecode = txrate;
1617eb6f0de0SAdrian Chadd 
1618eb6f0de0SAdrian Chadd 	/* Store the decided rate index values away */
1619eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_pktlen = pktlen;
1620eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_hdrlen = hdrlen;
1621eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_atype = atype;
1622eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txpower = ni->ni_txpower;
1623eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txrate0 = txrate;
1624eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_try0 = try0;
1625eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_keyix = keyix;
1626eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txantenna = sc->sc_txantenna;
1627875a9451SAdrian Chadd 	bf->bf_state.bfs_txflags = flags;
1628eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_shpream = shortPreamble;
1629eb6f0de0SAdrian Chadd 
1630eb6f0de0SAdrian Chadd 	/* XXX this should be done in ath_tx_setrate() */
1631eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsrate0 = 0;	/* ie, no hard-coded ctsrate */
1632eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsrate = 0;	/* calculated later */
1633eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsduration = 0;
1634eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ismrr = ismrr;
1635eb6f0de0SAdrian Chadd 
1636eb6f0de0SAdrian Chadd 	return 0;
1637eb6f0de0SAdrian Chadd }
1638eb6f0de0SAdrian Chadd 
1639b8e788a5SAdrian Chadd /*
16404e81f27cSAdrian Chadd  * Queue a frame to the hardware or software queue.
1641eb6f0de0SAdrian Chadd  *
1642eb6f0de0SAdrian Chadd  * This can be called by the net80211 code.
1643eb6f0de0SAdrian Chadd  *
1644eb6f0de0SAdrian Chadd  * XXX what about locking? Or, push the seqno assign into the
1645eb6f0de0SAdrian Chadd  * XXX aggregate scheduler so its serialised?
16464e81f27cSAdrian Chadd  *
16474e81f27cSAdrian Chadd  * XXX When sending management frames via ath_raw_xmit(),
16484e81f27cSAdrian Chadd  *     should CLRDMASK be set unconditionally?
1649b8e788a5SAdrian Chadd  */
1650eb6f0de0SAdrian Chadd int
1651eb6f0de0SAdrian Chadd ath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni,
1652eb6f0de0SAdrian Chadd     struct ath_buf *bf, struct mbuf *m0)
1653eb6f0de0SAdrian Chadd {
1654eb6f0de0SAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
1655eb6f0de0SAdrian Chadd 	struct ath_vap *avp = ATH_VAP(vap);
16569c85ff91SAdrian Chadd 	int r = 0;
1657eb6f0de0SAdrian Chadd 	u_int pri;
1658eb6f0de0SAdrian Chadd 	int tid;
1659eb6f0de0SAdrian Chadd 	struct ath_txq *txq;
1660eb6f0de0SAdrian Chadd 	int ismcast;
1661eb6f0de0SAdrian Chadd 	const struct ieee80211_frame *wh;
1662eb6f0de0SAdrian Chadd 	int is_ampdu, is_ampdu_tx, is_ampdu_pending;
1663a108d2d6SAdrian Chadd 	ieee80211_seq seqno;
1664eb6f0de0SAdrian Chadd 	uint8_t type, subtype;
1665eb6f0de0SAdrian Chadd 
1666eb6f0de0SAdrian Chadd 	/*
1667eb6f0de0SAdrian Chadd 	 * Determine the target hardware queue.
1668eb6f0de0SAdrian Chadd 	 *
1669b43facbfSAdrian Chadd 	 * For multicast frames, the txq gets overridden appropriately
1670b43facbfSAdrian Chadd 	 * depending upon the state of PS.
1671eb6f0de0SAdrian Chadd 	 *
1672eb6f0de0SAdrian Chadd 	 * For any other frame, we do a TID/QoS lookup inside the frame
1673eb6f0de0SAdrian Chadd 	 * to see what the TID should be. If it's a non-QoS frame, the
1674eb6f0de0SAdrian Chadd 	 * AC and TID are overridden. The TID/TXQ code assumes the
1675eb6f0de0SAdrian Chadd 	 * TID is on a predictable hardware TXQ, so we don't support
1676eb6f0de0SAdrian Chadd 	 * having a node TID queued to multiple hardware TXQs.
1677eb6f0de0SAdrian Chadd 	 * This may change in the future but would require some locking
1678eb6f0de0SAdrian Chadd 	 * fudgery.
1679eb6f0de0SAdrian Chadd 	 */
1680eb6f0de0SAdrian Chadd 	pri = ath_tx_getac(sc, m0);
1681eb6f0de0SAdrian Chadd 	tid = ath_tx_gettid(sc, m0);
1682eb6f0de0SAdrian Chadd 
1683eb6f0de0SAdrian Chadd 	txq = sc->sc_ac2q[pri];
1684eb6f0de0SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1685eb6f0de0SAdrian Chadd 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1686eb6f0de0SAdrian Chadd 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1687eb6f0de0SAdrian Chadd 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1688eb6f0de0SAdrian Chadd 
16899c85ff91SAdrian Chadd 	/*
16909c85ff91SAdrian Chadd 	 * Enforce how deep the multicast queue can grow.
16919c85ff91SAdrian Chadd 	 *
16929c85ff91SAdrian Chadd 	 * XXX duplicated in ath_raw_xmit().
16939c85ff91SAdrian Chadd 	 */
16949c85ff91SAdrian Chadd 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
16959c85ff91SAdrian Chadd 		ATH_TXQ_LOCK(sc->sc_cabq);
16969c85ff91SAdrian Chadd 
1697b09e37a1SAdrian Chadd 		if (sc->sc_cabq->axq_depth > sc->sc_txq_mcastq_maxdepth) {
16989c85ff91SAdrian Chadd 			sc->sc_stats.ast_tx_mcastq_overflow++;
16999c85ff91SAdrian Chadd 			r = ENOBUFS;
17009c85ff91SAdrian Chadd 		}
17019c85ff91SAdrian Chadd 
17029c85ff91SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_cabq);
17039c85ff91SAdrian Chadd 
17049c85ff91SAdrian Chadd 		if (r != 0) {
17059c85ff91SAdrian Chadd 			m_freem(m0);
17069c85ff91SAdrian Chadd 			return r;
17079c85ff91SAdrian Chadd 		}
17089c85ff91SAdrian Chadd 	}
17099c85ff91SAdrian Chadd 
1710eb6f0de0SAdrian Chadd 	/* A-MPDU TX */
1711eb6f0de0SAdrian Chadd 	is_ampdu_tx = ath_tx_ampdu_running(sc, ATH_NODE(ni), tid);
1712eb6f0de0SAdrian Chadd 	is_ampdu_pending = ath_tx_ampdu_pending(sc, ATH_NODE(ni), tid);
1713eb6f0de0SAdrian Chadd 	is_ampdu = is_ampdu_tx | is_ampdu_pending;
1714eb6f0de0SAdrian Chadd 
1715a108d2d6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d, ac=%d, is_ampdu=%d\n",
1716a108d2d6SAdrian Chadd 	    __func__, tid, pri, is_ampdu);
1717eb6f0de0SAdrian Chadd 
171846634305SAdrian Chadd 	/* Set local packet state, used to queue packets to hardware */
171946634305SAdrian Chadd 	bf->bf_state.bfs_tid = tid;
172046634305SAdrian Chadd 	bf->bf_state.bfs_txq = txq;
172146634305SAdrian Chadd 	bf->bf_state.bfs_pri = pri;
172246634305SAdrian Chadd 
1723c5940c30SAdrian Chadd 	/*
1724b43facbfSAdrian Chadd 	 * When servicing one or more stations in power-save mode
1725b43facbfSAdrian Chadd 	 * (or) if there is some mcast data waiting on the mcast
1726b43facbfSAdrian Chadd 	 * queue (to prevent out of order delivery) multicast frames
1727b43facbfSAdrian Chadd 	 * must be bufferd until after the beacon.
1728b43facbfSAdrian Chadd 	 *
1729b43facbfSAdrian Chadd 	 * TODO: we should lock the mcastq before we check the length.
1730c5940c30SAdrian Chadd 	 */
173146634305SAdrian Chadd 	if (ismcast && (vap->iv_ps_sta || avp->av_mcastq.axq_depth)) {
1732eb6f0de0SAdrian Chadd 		txq = &avp->av_mcastq;
173346634305SAdrian Chadd 		/*
173446634305SAdrian Chadd 		 * Mark the frame as eventually belonging on the CAB
173546634305SAdrian Chadd 		 * queue, so the descriptor setup functions will
173646634305SAdrian Chadd 		 * correctly initialise the descriptor 'qcuId' field.
173746634305SAdrian Chadd 		 */
173846634305SAdrian Chadd 		bf->bf_state.bfs_txq = sc->sc_cabq;
173946634305SAdrian Chadd 	}
1740eb6f0de0SAdrian Chadd 
1741eb6f0de0SAdrian Chadd 	/* Do the generic frame setup */
1742eb6f0de0SAdrian Chadd 	/* XXX should just bzero the bf_state? */
1743eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_dobaw = 0;
1744eb6f0de0SAdrian Chadd 
17457561cb5cSAdrian Chadd 	/*
17467561cb5cSAdrian Chadd 	 * Acquire the TXQ lock early, so both the encap and seqno
17477561cb5cSAdrian Chadd 	 * are allocated together.
174846634305SAdrian Chadd 	 *
174946634305SAdrian Chadd 	 * XXX should TXQ for CABQ traffic be the multicast queue,
175046634305SAdrian Chadd 	 * or the TXQ the given PRI would allocate from? (eg for
175146634305SAdrian Chadd 	 * sequence number allocation locking.)
17527561cb5cSAdrian Chadd 	 */
1753eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(txq);
17547561cb5cSAdrian Chadd 
17557561cb5cSAdrian Chadd 	/* A-MPDU TX? Manually set sequence number */
17567561cb5cSAdrian Chadd 	/*
17577561cb5cSAdrian Chadd 	 * Don't do it whilst pending; the net80211 layer still
17587561cb5cSAdrian Chadd 	 * assigns them.
17597561cb5cSAdrian Chadd 	 */
17607561cb5cSAdrian Chadd 	if (is_ampdu_tx) {
1761eb6f0de0SAdrian Chadd 		/*
1762eb6f0de0SAdrian Chadd 		 * Always call; this function will
1763eb6f0de0SAdrian Chadd 		 * handle making sure that null data frames
1764eb6f0de0SAdrian Chadd 		 * don't get a sequence number from the current
1765eb6f0de0SAdrian Chadd 		 * TID and thus mess with the BAW.
1766eb6f0de0SAdrian Chadd 		 */
1767a108d2d6SAdrian Chadd 		seqno = ath_tx_tid_seqno_assign(sc, ni, bf, m0);
176842f4d061SAdrian Chadd 
176942f4d061SAdrian Chadd 		/*
177042f4d061SAdrian Chadd 		 * Don't add QoS NULL frames to the BAW.
177142f4d061SAdrian Chadd 		 */
1772a108d2d6SAdrian Chadd 		if (IEEE80211_QOS_HAS_SEQ(wh) &&
1773a108d2d6SAdrian Chadd 		    subtype != IEEE80211_FC0_SUBTYPE_QOS_NULL) {
1774eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_dobaw = 1;
1775eb6f0de0SAdrian Chadd 		}
1776c1782ce0SAdrian Chadd 	}
1777c1782ce0SAdrian Chadd 
1778eb6f0de0SAdrian Chadd 	/*
1779eb6f0de0SAdrian Chadd 	 * If needed, the sequence number has been assigned.
1780eb6f0de0SAdrian Chadd 	 * Squirrel it away somewhere easy to get to.
1781eb6f0de0SAdrian Chadd 	 */
1782a108d2d6SAdrian Chadd 	bf->bf_state.bfs_seqno = M_SEQNO_GET(m0) << IEEE80211_SEQ_SEQ_SHIFT;
1783b8e788a5SAdrian Chadd 
1784eb6f0de0SAdrian Chadd 	/* Is ampdu pending? fetch the seqno and print it out */
1785eb6f0de0SAdrian Chadd 	if (is_ampdu_pending)
1786eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX,
1787eb6f0de0SAdrian Chadd 		    "%s: tid %d: ampdu pending, seqno %d\n",
1788eb6f0de0SAdrian Chadd 		    __func__, tid, M_SEQNO_GET(m0));
1789eb6f0de0SAdrian Chadd 
1790eb6f0de0SAdrian Chadd 	/* This also sets up the DMA map */
1791b43facbfSAdrian Chadd 	r = ath_tx_normal_setup(sc, ni, bf, m0, txq);
1792eb6f0de0SAdrian Chadd 
1793eb6f0de0SAdrian Chadd 	if (r != 0)
17947561cb5cSAdrian Chadd 		goto done;
1795eb6f0de0SAdrian Chadd 
1796eb6f0de0SAdrian Chadd 	/* At this point m0 could have changed! */
1797eb6f0de0SAdrian Chadd 	m0 = bf->bf_m;
1798eb6f0de0SAdrian Chadd 
1799eb6f0de0SAdrian Chadd #if 1
1800eb6f0de0SAdrian Chadd 	/*
1801eb6f0de0SAdrian Chadd 	 * If it's a multicast frame, do a direct-dispatch to the
1802eb6f0de0SAdrian Chadd 	 * destination hardware queue. Don't bother software
1803eb6f0de0SAdrian Chadd 	 * queuing it.
1804eb6f0de0SAdrian Chadd 	 */
1805eb6f0de0SAdrian Chadd 	/*
1806eb6f0de0SAdrian Chadd 	 * If it's a BAR frame, do a direct dispatch to the
1807eb6f0de0SAdrian Chadd 	 * destination hardware queue. Don't bother software
1808eb6f0de0SAdrian Chadd 	 * queuing it, as the TID will now be paused.
1809eb6f0de0SAdrian Chadd 	 * Sending a BAR frame can occur from the net80211 txa timer
1810eb6f0de0SAdrian Chadd 	 * (ie, retries) or from the ath txtask (completion call.)
1811eb6f0de0SAdrian Chadd 	 * It queues directly to hardware because the TID is paused
1812eb6f0de0SAdrian Chadd 	 * at this point (and won't be unpaused until the BAR has
1813eb6f0de0SAdrian Chadd 	 * either been TXed successfully or max retries has been
1814eb6f0de0SAdrian Chadd 	 * reached.)
1815eb6f0de0SAdrian Chadd 	 */
1816eb6f0de0SAdrian Chadd 	if (txq == &avp->av_mcastq) {
1817d3a6425bSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX,
18180b96ef63SAdrian Chadd 		    "%s: bf=%p: mcastq: TX'ing\n", __func__, bf);
18194e81f27cSAdrian Chadd 		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
1820eb6f0de0SAdrian Chadd 		ath_tx_xmit_normal(sc, txq, bf);
1821eb6f0de0SAdrian Chadd 	} else if (type == IEEE80211_FC0_TYPE_CTL &&
1822eb6f0de0SAdrian Chadd 		    subtype == IEEE80211_FC0_SUBTYPE_BAR) {
1823d3a6425bSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX,
1824eb6f0de0SAdrian Chadd 		    "%s: BAR: TX'ing direct\n", __func__);
18254e81f27cSAdrian Chadd 		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
1826eb6f0de0SAdrian Chadd 		ath_tx_xmit_normal(sc, txq, bf);
1827eb6f0de0SAdrian Chadd 	} else {
1828eb6f0de0SAdrian Chadd 		/* add to software queue */
1829d3a6425bSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX,
18300b96ef63SAdrian Chadd 		    "%s: bf=%p: swq: TX'ing\n", __func__, bf);
1831eb6f0de0SAdrian Chadd 		ath_tx_swq(sc, ni, txq, bf);
1832eb6f0de0SAdrian Chadd 	}
1833eb6f0de0SAdrian Chadd #else
1834eb6f0de0SAdrian Chadd 	/*
1835eb6f0de0SAdrian Chadd 	 * For now, since there's no software queue,
1836eb6f0de0SAdrian Chadd 	 * direct-dispatch to the hardware.
1837eb6f0de0SAdrian Chadd 	 */
18384e81f27cSAdrian Chadd 	bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
1839eb6f0de0SAdrian Chadd 	ath_tx_xmit_normal(sc, txq, bf);
1840eb6f0de0SAdrian Chadd #endif
18417561cb5cSAdrian Chadd done:
18427561cb5cSAdrian Chadd 	ATH_TXQ_UNLOCK(txq);
1843eb6f0de0SAdrian Chadd 
1844b8e788a5SAdrian Chadd 	return 0;
1845b8e788a5SAdrian Chadd }
1846b8e788a5SAdrian Chadd 
1847b8e788a5SAdrian Chadd static int
1848b8e788a5SAdrian Chadd ath_tx_raw_start(struct ath_softc *sc, struct ieee80211_node *ni,
1849b8e788a5SAdrian Chadd 	struct ath_buf *bf, struct mbuf *m0,
1850b8e788a5SAdrian Chadd 	const struct ieee80211_bpf_params *params)
1851b8e788a5SAdrian Chadd {
1852b8e788a5SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
1853b8e788a5SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
1854b8e788a5SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
1855b8e788a5SAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
1856b8e788a5SAdrian Chadd 	int error, ismcast, ismrr;
1857b8e788a5SAdrian Chadd 	int keyix, hdrlen, pktlen, try0, txantenna;
1858eb6f0de0SAdrian Chadd 	u_int8_t rix, txrate;
1859b8e788a5SAdrian Chadd 	struct ieee80211_frame *wh;
1860eb6f0de0SAdrian Chadd 	u_int flags;
1861b8e788a5SAdrian Chadd 	HAL_PKT_TYPE atype;
1862b8e788a5SAdrian Chadd 	const HAL_RATE_TABLE *rt;
1863b8e788a5SAdrian Chadd 	struct ath_desc *ds;
1864b8e788a5SAdrian Chadd 	u_int pri;
1865eb6f0de0SAdrian Chadd 	int o_tid = -1;
1866eb6f0de0SAdrian Chadd 	int do_override;
1867b8e788a5SAdrian Chadd 
1868b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1869b8e788a5SAdrian Chadd 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1870b8e788a5SAdrian Chadd 	hdrlen = ieee80211_anyhdrsize(wh);
1871b8e788a5SAdrian Chadd 	/*
1872b8e788a5SAdrian Chadd 	 * Packet length must not include any
1873b8e788a5SAdrian Chadd 	 * pad bytes; deduct them here.
1874b8e788a5SAdrian Chadd 	 */
1875b8e788a5SAdrian Chadd 	/* XXX honor IEEE80211_BPF_DATAPAD */
1876b8e788a5SAdrian Chadd 	pktlen = m0->m_pkthdr.len - (hdrlen & 3) + IEEE80211_CRC_LEN;
1877b8e788a5SAdrian Chadd 
1878eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: ismcast=%d\n",
1879eb6f0de0SAdrian Chadd 	    __func__, ismcast);
1880eb6f0de0SAdrian Chadd 
18817561cb5cSAdrian Chadd 	pri = params->ibp_pri & 3;
18827561cb5cSAdrian Chadd 	/* Override pri if the frame isn't a QoS one */
18837561cb5cSAdrian Chadd 	if (! IEEE80211_QOS_HAS_SEQ(wh))
18847561cb5cSAdrian Chadd 		pri = ath_tx_getac(sc, m0);
18857561cb5cSAdrian Chadd 
18867561cb5cSAdrian Chadd 	/* XXX If it's an ADDBA, override the correct queue */
18877561cb5cSAdrian Chadd 	do_override = ath_tx_action_frame_override_queue(sc, ni, m0, &o_tid);
18887561cb5cSAdrian Chadd 
18897561cb5cSAdrian Chadd 	/* Map ADDBA to the correct priority */
18907561cb5cSAdrian Chadd 	if (do_override) {
18917561cb5cSAdrian Chadd #if 0
18927561cb5cSAdrian Chadd 		device_printf(sc->sc_dev,
18937561cb5cSAdrian Chadd 		    "%s: overriding tid %d pri %d -> %d\n",
18947561cb5cSAdrian Chadd 		    __func__, o_tid, pri, TID_TO_WME_AC(o_tid));
18957561cb5cSAdrian Chadd #endif
18967561cb5cSAdrian Chadd 		pri = TID_TO_WME_AC(o_tid);
18977561cb5cSAdrian Chadd 	}
18987561cb5cSAdrian Chadd 
18997561cb5cSAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[pri]);
19007561cb5cSAdrian Chadd 
190181a82688SAdrian Chadd 	/* Handle encryption twiddling if needed */
1902eb6f0de0SAdrian Chadd 	if (! ath_tx_tag_crypto(sc, ni,
1903eb6f0de0SAdrian Chadd 	    m0, params->ibp_flags & IEEE80211_BPF_CRYPTO, 0,
1904eb6f0de0SAdrian Chadd 	    &hdrlen, &pktlen, &keyix)) {
1905b8e788a5SAdrian Chadd 		ath_freetx(m0);
1906b8e788a5SAdrian Chadd 		return EIO;
1907b8e788a5SAdrian Chadd 	}
1908b8e788a5SAdrian Chadd 	/* packet header may have moved, reset our local pointer */
1909b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1910b8e788a5SAdrian Chadd 
1911eb6f0de0SAdrian Chadd 	/* Do the generic frame setup */
1912eb6f0de0SAdrian Chadd 	/* XXX should just bzero the bf_state? */
1913eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_dobaw = 0;
1914eb6f0de0SAdrian Chadd 
1915b8e788a5SAdrian Chadd 	error = ath_tx_dmasetup(sc, bf, m0);
1916b8e788a5SAdrian Chadd 	if (error != 0)
1917b8e788a5SAdrian Chadd 		return error;
1918b8e788a5SAdrian Chadd 	m0 = bf->bf_m;				/* NB: may have changed */
1919b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1920b8e788a5SAdrian Chadd 	bf->bf_node = ni;			/* NB: held reference */
1921b8e788a5SAdrian Chadd 
19224e81f27cSAdrian Chadd 	/* Always enable CLRDMASK for raw frames for now.. */
1923b8e788a5SAdrian Chadd 	flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for crypto errs */
1924b8e788a5SAdrian Chadd 	flags |= HAL_TXDESC_INTREQ;		/* force interrupt */
1925b8e788a5SAdrian Chadd 	if (params->ibp_flags & IEEE80211_BPF_RTS)
1926b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_RTSENA;
1927eb6f0de0SAdrian Chadd 	else if (params->ibp_flags & IEEE80211_BPF_CTS) {
1928eb6f0de0SAdrian Chadd 		/* XXX assume 11g/11n protection? */
1929eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_doprot = 1;
1930b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_CTSENA;
1931eb6f0de0SAdrian Chadd 	}
1932b8e788a5SAdrian Chadd 	/* XXX leave ismcast to injector? */
1933b8e788a5SAdrian Chadd 	if ((params->ibp_flags & IEEE80211_BPF_NOACK) || ismcast)
1934b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_NOACK;
1935b8e788a5SAdrian Chadd 
1936b8e788a5SAdrian Chadd 	rt = sc->sc_currates;
1937b8e788a5SAdrian Chadd 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
1938b8e788a5SAdrian Chadd 	rix = ath_tx_findrix(sc, params->ibp_rate0);
1939b8e788a5SAdrian Chadd 	txrate = rt->info[rix].rateCode;
1940b8e788a5SAdrian Chadd 	if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
1941b8e788a5SAdrian Chadd 		txrate |= rt->info[rix].shortPreamble;
1942b8e788a5SAdrian Chadd 	sc->sc_txrix = rix;
1943b8e788a5SAdrian Chadd 	try0 = params->ibp_try0;
1944b8e788a5SAdrian Chadd 	ismrr = (params->ibp_try1 != 0);
1945b8e788a5SAdrian Chadd 	txantenna = params->ibp_pri >> 2;
1946b8e788a5SAdrian Chadd 	if (txantenna == 0)			/* XXX? */
1947b8e788a5SAdrian Chadd 		txantenna = sc->sc_txantenna;
194879f02dbfSAdrian Chadd 
194979f02dbfSAdrian Chadd 	/*
1950eb6f0de0SAdrian Chadd 	 * Since ctsrate is fixed, store it away for later
1951eb6f0de0SAdrian Chadd 	 * use when the descriptor fields are being set.
195279f02dbfSAdrian Chadd 	 */
1953eb6f0de0SAdrian Chadd 	if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA))
1954eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_ctsrate0 = params->ibp_ctsrate;
195579f02dbfSAdrian Chadd 
1956b8e788a5SAdrian Chadd 	/*
1957b8e788a5SAdrian Chadd 	 * NB: we mark all packets as type PSPOLL so the h/w won't
1958b8e788a5SAdrian Chadd 	 * set the sequence number, duration, etc.
1959b8e788a5SAdrian Chadd 	 */
1960b8e788a5SAdrian Chadd 	atype = HAL_PKT_TYPE_PSPOLL;
1961b8e788a5SAdrian Chadd 
1962b8e788a5SAdrian Chadd 	if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
1963b8e788a5SAdrian Chadd 		ieee80211_dump_pkt(ic, mtod(m0, caddr_t), m0->m_len,
1964b8e788a5SAdrian Chadd 		    sc->sc_hwmap[rix].ieeerate, -1);
1965b8e788a5SAdrian Chadd 
1966b8e788a5SAdrian Chadd 	if (ieee80211_radiotap_active_vap(vap)) {
1967b8e788a5SAdrian Chadd 		u_int64_t tsf = ath_hal_gettsf64(ah);
1968b8e788a5SAdrian Chadd 
1969b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_tsf = htole64(tsf);
1970b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
1971b8e788a5SAdrian Chadd 		if (wh->i_fc[1] & IEEE80211_FC1_WEP)
1972b8e788a5SAdrian Chadd 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
1973b8e788a5SAdrian Chadd 		if (m0->m_flags & M_FRAG)
1974b8e788a5SAdrian Chadd 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
1975b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
1976b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_txpower = ni->ni_txpower;
1977b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
1978b8e788a5SAdrian Chadd 
1979b8e788a5SAdrian Chadd 		ieee80211_radiotap_tx(vap, m0);
1980b8e788a5SAdrian Chadd 	}
1981b8e788a5SAdrian Chadd 
1982b8e788a5SAdrian Chadd 	/*
1983b8e788a5SAdrian Chadd 	 * Formulate first tx descriptor with tx controls.
1984b8e788a5SAdrian Chadd 	 */
1985b8e788a5SAdrian Chadd 	ds = bf->bf_desc;
1986b8e788a5SAdrian Chadd 	/* XXX check return value? */
1987eb6f0de0SAdrian Chadd 
1988eb6f0de0SAdrian Chadd 	/* Store the decided rate index values away */
1989eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_pktlen = pktlen;
1990eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_hdrlen = hdrlen;
1991eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_atype = atype;
1992eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txpower = params->ibp_power;
1993eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txrate0 = txrate;
1994eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_try0 = try0;
1995eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_keyix = keyix;
1996eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txantenna = txantenna;
1997875a9451SAdrian Chadd 	bf->bf_state.bfs_txflags = flags;
1998eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_shpream =
1999eb6f0de0SAdrian Chadd 	    !! (params->ibp_flags & IEEE80211_BPF_SHORTPRE);
2000b8e788a5SAdrian Chadd 
200146634305SAdrian Chadd 	/* Set local packet state, used to queue packets to hardware */
200246634305SAdrian Chadd 	bf->bf_state.bfs_tid = WME_AC_TO_TID(pri);
200346634305SAdrian Chadd 	bf->bf_state.bfs_txq = sc->sc_ac2q[pri];
200446634305SAdrian Chadd 	bf->bf_state.bfs_pri = pri;
200546634305SAdrian Chadd 
2006eb6f0de0SAdrian Chadd 	/* XXX this should be done in ath_tx_setrate() */
2007eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsrate = 0;
2008eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsduration = 0;
2009eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ismrr = ismrr;
2010eb6f0de0SAdrian Chadd 
2011eb6f0de0SAdrian Chadd 	/* Blank the legacy rate array */
2012eb6f0de0SAdrian Chadd 	bzero(&bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
2013eb6f0de0SAdrian Chadd 
2014eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].rix =
2015eb6f0de0SAdrian Chadd 	    ath_tx_findrix(sc, params->ibp_rate0);
2016eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].tries = try0;
2017eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].ratecode = txrate;
2018c1782ce0SAdrian Chadd 
2019c1782ce0SAdrian Chadd 	if (ismrr) {
2020eb6f0de0SAdrian Chadd 		int rix;
2021c1782ce0SAdrian Chadd 
2022b8e788a5SAdrian Chadd 		rix = ath_tx_findrix(sc, params->ibp_rate1);
2023eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[1].rix = rix;
2024eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[1].tries = params->ibp_try1;
2025c1782ce0SAdrian Chadd 
2026eb6f0de0SAdrian Chadd 		rix = ath_tx_findrix(sc, params->ibp_rate2);
2027eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[2].rix = rix;
2028eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[2].tries = params->ibp_try2;
2029eb6f0de0SAdrian Chadd 
2030eb6f0de0SAdrian Chadd 		rix = ath_tx_findrix(sc, params->ibp_rate3);
2031eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[3].rix = rix;
2032eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[3].tries = params->ibp_try3;
2033c1782ce0SAdrian Chadd 	}
2034eb6f0de0SAdrian Chadd 	/*
2035eb6f0de0SAdrian Chadd 	 * All the required rate control decisions have been made;
2036eb6f0de0SAdrian Chadd 	 * fill in the rc flags.
2037eb6f0de0SAdrian Chadd 	 */
2038eb6f0de0SAdrian Chadd 	ath_tx_rate_fill_rcflags(sc, bf);
2039b8e788a5SAdrian Chadd 
2040b8e788a5SAdrian Chadd 	/* NB: no buffered multicast in power save support */
2041eb6f0de0SAdrian Chadd 
2042eb6f0de0SAdrian Chadd 	/*
2043eb6f0de0SAdrian Chadd 	 * If we're overiding the ADDBA destination, dump directly
2044eb6f0de0SAdrian Chadd 	 * into the hardware queue, right after any pending
2045eb6f0de0SAdrian Chadd 	 * frames to that node are.
2046eb6f0de0SAdrian Chadd 	 */
2047eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: dooverride=%d\n",
2048eb6f0de0SAdrian Chadd 	    __func__, do_override);
2049eb6f0de0SAdrian Chadd 
205094eefcf1SAdrian Chadd #if 1
2051eb6f0de0SAdrian Chadd 	if (do_override) {
20524e81f27cSAdrian Chadd 		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
2053eb6f0de0SAdrian Chadd 		ath_tx_xmit_normal(sc, sc->sc_ac2q[pri], bf);
2054eb6f0de0SAdrian Chadd 	} else {
2055eb6f0de0SAdrian Chadd 		/* Queue to software queue */
2056eb6f0de0SAdrian Chadd 		ath_tx_swq(sc, ni, sc->sc_ac2q[pri], bf);
2057eb6f0de0SAdrian Chadd 	}
205894eefcf1SAdrian Chadd #else
205994eefcf1SAdrian Chadd 	/* Direct-dispatch to the hardware */
206094eefcf1SAdrian Chadd 	bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
206194eefcf1SAdrian Chadd 	ath_tx_xmit_normal(sc, sc->sc_ac2q[pri], bf);
206294eefcf1SAdrian Chadd #endif
20637561cb5cSAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[pri]);
2064eb6f0de0SAdrian Chadd 
2065b8e788a5SAdrian Chadd 	return 0;
2066b8e788a5SAdrian Chadd }
2067b8e788a5SAdrian Chadd 
2068eb6f0de0SAdrian Chadd /*
2069eb6f0de0SAdrian Chadd  * Send a raw frame.
2070eb6f0de0SAdrian Chadd  *
2071eb6f0de0SAdrian Chadd  * This can be called by net80211.
2072eb6f0de0SAdrian Chadd  */
2073b8e788a5SAdrian Chadd int
2074b8e788a5SAdrian Chadd ath_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2075b8e788a5SAdrian Chadd 	const struct ieee80211_bpf_params *params)
2076b8e788a5SAdrian Chadd {
2077b8e788a5SAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
2078b8e788a5SAdrian Chadd 	struct ifnet *ifp = ic->ic_ifp;
2079b8e788a5SAdrian Chadd 	struct ath_softc *sc = ifp->if_softc;
2080b8e788a5SAdrian Chadd 	struct ath_buf *bf;
20819c85ff91SAdrian Chadd 	struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
20829c85ff91SAdrian Chadd 	int error = 0;
2083b8e788a5SAdrian Chadd 
2084ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2085ef27340cSAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
2086ef27340cSAdrian Chadd 		device_printf(sc->sc_dev, "%s: sc_inreset_cnt > 0; bailing\n",
2087ef27340cSAdrian Chadd 		    __func__);
2088ef27340cSAdrian Chadd 		error = EIO;
2089ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2090ef27340cSAdrian Chadd 		goto bad0;
2091ef27340cSAdrian Chadd 	}
2092ef27340cSAdrian Chadd 	sc->sc_txstart_cnt++;
2093ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2094ef27340cSAdrian Chadd 
2095b8e788a5SAdrian Chadd 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->sc_invalid) {
2096b8e788a5SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: discard frame, %s", __func__,
2097b8e788a5SAdrian Chadd 		    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ?
2098b8e788a5SAdrian Chadd 			"!running" : "invalid");
2099b8e788a5SAdrian Chadd 		m_freem(m);
2100b8e788a5SAdrian Chadd 		error = ENETDOWN;
2101b8e788a5SAdrian Chadd 		goto bad;
2102b8e788a5SAdrian Chadd 	}
21039c85ff91SAdrian Chadd 
21049c85ff91SAdrian Chadd 	/*
21059c85ff91SAdrian Chadd 	 * Enforce how deep the multicast queue can grow.
21069c85ff91SAdrian Chadd 	 *
21079c85ff91SAdrian Chadd 	 * XXX duplicated in ath_tx_start().
21089c85ff91SAdrian Chadd 	 */
21099c85ff91SAdrian Chadd 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
21109c85ff91SAdrian Chadd 		ATH_TXQ_LOCK(sc->sc_cabq);
21119c85ff91SAdrian Chadd 
2112b09e37a1SAdrian Chadd 		if (sc->sc_cabq->axq_depth > sc->sc_txq_mcastq_maxdepth) {
21139c85ff91SAdrian Chadd 			sc->sc_stats.ast_tx_mcastq_overflow++;
21149c85ff91SAdrian Chadd 			error = ENOBUFS;
21159c85ff91SAdrian Chadd 		}
21169c85ff91SAdrian Chadd 
21179c85ff91SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_cabq);
21189c85ff91SAdrian Chadd 
21199c85ff91SAdrian Chadd 		if (error != 0) {
21209c85ff91SAdrian Chadd 			m_freem(m);
21219c85ff91SAdrian Chadd 			goto bad;
21229c85ff91SAdrian Chadd 		}
21239c85ff91SAdrian Chadd 	}
21249c85ff91SAdrian Chadd 
2125b8e788a5SAdrian Chadd 	/*
2126b8e788a5SAdrian Chadd 	 * Grab a TX buffer and associated resources.
2127b8e788a5SAdrian Chadd 	 */
2128af33d486SAdrian Chadd 	bf = ath_getbuf(sc, ATH_BUFTYPE_MGMT);
2129b8e788a5SAdrian Chadd 	if (bf == NULL) {
2130b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_nobuf++;
2131b8e788a5SAdrian Chadd 		m_freem(m);
2132b8e788a5SAdrian Chadd 		error = ENOBUFS;
2133b8e788a5SAdrian Chadd 		goto bad;
2134b8e788a5SAdrian Chadd 	}
2135b8e788a5SAdrian Chadd 
2136b8e788a5SAdrian Chadd 	if (params == NULL) {
2137b8e788a5SAdrian Chadd 		/*
2138b8e788a5SAdrian Chadd 		 * Legacy path; interpret frame contents to decide
2139b8e788a5SAdrian Chadd 		 * precisely how to send the frame.
2140b8e788a5SAdrian Chadd 		 */
2141b8e788a5SAdrian Chadd 		if (ath_tx_start(sc, ni, bf, m)) {
2142b8e788a5SAdrian Chadd 			error = EIO;		/* XXX */
2143b8e788a5SAdrian Chadd 			goto bad2;
2144b8e788a5SAdrian Chadd 		}
2145b8e788a5SAdrian Chadd 	} else {
2146b8e788a5SAdrian Chadd 		/*
2147b8e788a5SAdrian Chadd 		 * Caller supplied explicit parameters to use in
2148b8e788a5SAdrian Chadd 		 * sending the frame.
2149b8e788a5SAdrian Chadd 		 */
2150b8e788a5SAdrian Chadd 		if (ath_tx_raw_start(sc, ni, bf, m, params)) {
2151b8e788a5SAdrian Chadd 			error = EIO;		/* XXX */
2152b8e788a5SAdrian Chadd 			goto bad2;
2153b8e788a5SAdrian Chadd 		}
2154b8e788a5SAdrian Chadd 	}
2155b8e788a5SAdrian Chadd 	sc->sc_wd_timer = 5;
2156b8e788a5SAdrian Chadd 	ifp->if_opackets++;
2157b8e788a5SAdrian Chadd 	sc->sc_stats.ast_tx_raw++;
2158b8e788a5SAdrian Chadd 
2159ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2160ef27340cSAdrian Chadd 	sc->sc_txstart_cnt--;
2161ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2162ef27340cSAdrian Chadd 
2163b8e788a5SAdrian Chadd 	return 0;
2164b8e788a5SAdrian Chadd bad2:
2165b8e788a5SAdrian Chadd 	ATH_TXBUF_LOCK(sc);
2166e1a50456SAdrian Chadd 	ath_returnbuf_head(sc, bf);
2167b8e788a5SAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
2168b8e788a5SAdrian Chadd bad:
2169ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2170ef27340cSAdrian Chadd 	sc->sc_txstart_cnt--;
2171ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2172ef27340cSAdrian Chadd bad0:
2173b8e788a5SAdrian Chadd 	ifp->if_oerrors++;
2174b8e788a5SAdrian Chadd 	sc->sc_stats.ast_tx_raw_fail++;
2175b8e788a5SAdrian Chadd 	ieee80211_free_node(ni);
2176ef27340cSAdrian Chadd 
2177b8e788a5SAdrian Chadd 	return error;
2178b8e788a5SAdrian Chadd }
2179eb6f0de0SAdrian Chadd 
2180eb6f0de0SAdrian Chadd /* Some helper functions */
2181eb6f0de0SAdrian Chadd 
2182eb6f0de0SAdrian Chadd /*
2183eb6f0de0SAdrian Chadd  * ADDBA (and potentially others) need to be placed in the same
2184eb6f0de0SAdrian Chadd  * hardware queue as the TID/node it's relating to. This is so
2185eb6f0de0SAdrian Chadd  * it goes out after any pending non-aggregate frames to the
2186eb6f0de0SAdrian Chadd  * same node/TID.
2187eb6f0de0SAdrian Chadd  *
2188eb6f0de0SAdrian Chadd  * If this isn't done, the ADDBA can go out before the frames
2189eb6f0de0SAdrian Chadd  * queued in hardware. Even though these frames have a sequence
2190eb6f0de0SAdrian Chadd  * number -earlier- than the ADDBA can be transmitted (but
2191eb6f0de0SAdrian Chadd  * no frames whose sequence numbers are after the ADDBA should
2192eb6f0de0SAdrian Chadd  * be!) they'll arrive after the ADDBA - and the receiving end
2193eb6f0de0SAdrian Chadd  * will simply drop them as being out of the BAW.
2194eb6f0de0SAdrian Chadd  *
2195eb6f0de0SAdrian Chadd  * The frames can't be appended to the TID software queue - it'll
2196eb6f0de0SAdrian Chadd  * never be sent out. So these frames have to be directly
2197eb6f0de0SAdrian Chadd  * dispatched to the hardware, rather than queued in software.
2198eb6f0de0SAdrian Chadd  * So if this function returns true, the TXQ has to be
2199eb6f0de0SAdrian Chadd  * overridden and it has to be directly dispatched.
2200eb6f0de0SAdrian Chadd  *
2201eb6f0de0SAdrian Chadd  * It's a dirty hack, but someone's gotta do it.
2202eb6f0de0SAdrian Chadd  */
2203eb6f0de0SAdrian Chadd 
2204eb6f0de0SAdrian Chadd /*
2205eb6f0de0SAdrian Chadd  * XXX doesn't belong here!
2206eb6f0de0SAdrian Chadd  */
2207eb6f0de0SAdrian Chadd static int
2208eb6f0de0SAdrian Chadd ieee80211_is_action(struct ieee80211_frame *wh)
2209eb6f0de0SAdrian Chadd {
2210eb6f0de0SAdrian Chadd 	/* Type: Management frame? */
2211eb6f0de0SAdrian Chadd 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) !=
2212eb6f0de0SAdrian Chadd 	    IEEE80211_FC0_TYPE_MGT)
2213eb6f0de0SAdrian Chadd 		return 0;
2214eb6f0de0SAdrian Chadd 
2215eb6f0de0SAdrian Chadd 	/* Subtype: Action frame? */
2216eb6f0de0SAdrian Chadd 	if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) !=
2217eb6f0de0SAdrian Chadd 	    IEEE80211_FC0_SUBTYPE_ACTION)
2218eb6f0de0SAdrian Chadd 		return 0;
2219eb6f0de0SAdrian Chadd 
2220eb6f0de0SAdrian Chadd 	return 1;
2221eb6f0de0SAdrian Chadd }
2222eb6f0de0SAdrian Chadd 
2223eb6f0de0SAdrian Chadd #define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
2224eb6f0de0SAdrian Chadd /*
2225eb6f0de0SAdrian Chadd  * Return an alternate TID for ADDBA request frames.
2226eb6f0de0SAdrian Chadd  *
2227eb6f0de0SAdrian Chadd  * Yes, this likely should be done in the net80211 layer.
2228eb6f0de0SAdrian Chadd  */
2229eb6f0de0SAdrian Chadd static int
2230eb6f0de0SAdrian Chadd ath_tx_action_frame_override_queue(struct ath_softc *sc,
2231eb6f0de0SAdrian Chadd     struct ieee80211_node *ni,
2232eb6f0de0SAdrian Chadd     struct mbuf *m0, int *tid)
2233eb6f0de0SAdrian Chadd {
2234eb6f0de0SAdrian Chadd 	struct ieee80211_frame *wh = mtod(m0, struct ieee80211_frame *);
2235eb6f0de0SAdrian Chadd 	struct ieee80211_action_ba_addbarequest *ia;
2236eb6f0de0SAdrian Chadd 	uint8_t *frm;
2237eb6f0de0SAdrian Chadd 	uint16_t baparamset;
2238eb6f0de0SAdrian Chadd 
2239eb6f0de0SAdrian Chadd 	/* Not action frame? Bail */
2240eb6f0de0SAdrian Chadd 	if (! ieee80211_is_action(wh))
2241eb6f0de0SAdrian Chadd 		return 0;
2242eb6f0de0SAdrian Chadd 
2243eb6f0de0SAdrian Chadd 	/* XXX Not needed for frames we send? */
2244eb6f0de0SAdrian Chadd #if 0
2245eb6f0de0SAdrian Chadd 	/* Correct length? */
2246eb6f0de0SAdrian Chadd 	if (! ieee80211_parse_action(ni, m))
2247eb6f0de0SAdrian Chadd 		return 0;
2248eb6f0de0SAdrian Chadd #endif
2249eb6f0de0SAdrian Chadd 
2250eb6f0de0SAdrian Chadd 	/* Extract out action frame */
2251eb6f0de0SAdrian Chadd 	frm = (u_int8_t *)&wh[1];
2252eb6f0de0SAdrian Chadd 	ia = (struct ieee80211_action_ba_addbarequest *) frm;
2253eb6f0de0SAdrian Chadd 
2254eb6f0de0SAdrian Chadd 	/* Not ADDBA? Bail */
2255eb6f0de0SAdrian Chadd 	if (ia->rq_header.ia_category != IEEE80211_ACTION_CAT_BA)
2256eb6f0de0SAdrian Chadd 		return 0;
2257eb6f0de0SAdrian Chadd 	if (ia->rq_header.ia_action != IEEE80211_ACTION_BA_ADDBA_REQUEST)
2258eb6f0de0SAdrian Chadd 		return 0;
2259eb6f0de0SAdrian Chadd 
2260eb6f0de0SAdrian Chadd 	/* Extract TID, return it */
2261eb6f0de0SAdrian Chadd 	baparamset = le16toh(ia->rq_baparamset);
2262eb6f0de0SAdrian Chadd 	*tid = (int) MS(baparamset, IEEE80211_BAPS_TID);
2263eb6f0de0SAdrian Chadd 
2264eb6f0de0SAdrian Chadd 	return 1;
2265eb6f0de0SAdrian Chadd }
2266eb6f0de0SAdrian Chadd #undef	MS
2267eb6f0de0SAdrian Chadd 
2268eb6f0de0SAdrian Chadd /* Per-node software queue operations */
2269eb6f0de0SAdrian Chadd 
2270eb6f0de0SAdrian Chadd /*
2271eb6f0de0SAdrian Chadd  * Add the current packet to the given BAW.
2272eb6f0de0SAdrian Chadd  * It is assumed that the current packet
2273eb6f0de0SAdrian Chadd  *
2274eb6f0de0SAdrian Chadd  * + fits inside the BAW;
2275eb6f0de0SAdrian Chadd  * + already has had a sequence number allocated.
2276eb6f0de0SAdrian Chadd  *
2277eb6f0de0SAdrian Chadd  * Since the BAW status may be modified by both the ath task and
2278eb6f0de0SAdrian Chadd  * the net80211/ifnet contexts, the TID must be locked.
2279eb6f0de0SAdrian Chadd  */
2280eb6f0de0SAdrian Chadd void
2281eb6f0de0SAdrian Chadd ath_tx_addto_baw(struct ath_softc *sc, struct ath_node *an,
2282eb6f0de0SAdrian Chadd     struct ath_tid *tid, struct ath_buf *bf)
2283eb6f0de0SAdrian Chadd {
2284eb6f0de0SAdrian Chadd 	int index, cindex;
2285eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
2286eb6f0de0SAdrian Chadd 
2287eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
2288c2ac9655SAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
2289eb6f0de0SAdrian Chadd 
2290eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_isretried)
2291eb6f0de0SAdrian Chadd 		return;
2292eb6f0de0SAdrian Chadd 
2293c7c07341SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
2294c7c07341SAdrian Chadd 
22957561cb5cSAdrian Chadd 	if (! bf->bf_state.bfs_dobaw) {
22967561cb5cSAdrian Chadd 		device_printf(sc->sc_dev,
22977561cb5cSAdrian Chadd 		    "%s: dobaw=0, seqno=%d, window %d:%d\n",
22987561cb5cSAdrian Chadd 		    __func__,
22997561cb5cSAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno),
23007561cb5cSAdrian Chadd 		    tap->txa_start,
23017561cb5cSAdrian Chadd 		    tap->txa_wnd);
23027561cb5cSAdrian Chadd 	}
23037561cb5cSAdrian Chadd 
2304eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_addedbaw)
2305eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
2306a108d2d6SAdrian Chadd 		    "%s: re-added? tid=%d, seqno %d; window %d:%d; "
2307d4365d16SAdrian Chadd 		    "baw head=%d tail=%d\n",
2308a108d2d6SAdrian Chadd 		    __func__, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
2309d4365d16SAdrian Chadd 		    tap->txa_start, tap->txa_wnd, tid->baw_head,
2310d4365d16SAdrian Chadd 		    tid->baw_tail);
2311eb6f0de0SAdrian Chadd 
2312eb6f0de0SAdrian Chadd 	/*
23137561cb5cSAdrian Chadd 	 * Verify that the given sequence number is not outside of the
23147561cb5cSAdrian Chadd 	 * BAW.  Complain loudly if that's the case.
23157561cb5cSAdrian Chadd 	 */
23167561cb5cSAdrian Chadd 	if (! BAW_WITHIN(tap->txa_start, tap->txa_wnd,
23177561cb5cSAdrian Chadd 	    SEQNO(bf->bf_state.bfs_seqno))) {
23187561cb5cSAdrian Chadd 		device_printf(sc->sc_dev,
23197561cb5cSAdrian Chadd 		    "%s: bf=%p: outside of BAW?? tid=%d, seqno %d; window %d:%d; "
23207561cb5cSAdrian Chadd 		    "baw head=%d tail=%d\n",
23217561cb5cSAdrian Chadd 		    __func__, bf, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
23227561cb5cSAdrian Chadd 		    tap->txa_start, tap->txa_wnd, tid->baw_head,
23237561cb5cSAdrian Chadd 		    tid->baw_tail);
23247561cb5cSAdrian Chadd 	}
23257561cb5cSAdrian Chadd 
23267561cb5cSAdrian Chadd 	/*
2327eb6f0de0SAdrian Chadd 	 * ni->ni_txseqs[] is the currently allocated seqno.
2328eb6f0de0SAdrian Chadd 	 * the txa state contains the current baw start.
2329eb6f0de0SAdrian Chadd 	 */
2330eb6f0de0SAdrian Chadd 	index  = ATH_BA_INDEX(tap->txa_start, SEQNO(bf->bf_state.bfs_seqno));
2331eb6f0de0SAdrian Chadd 	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
2332eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2333a108d2d6SAdrian Chadd 	    "%s: tid=%d, seqno %d; window %d:%d; index=%d cindex=%d "
2334d4365d16SAdrian Chadd 	    "baw head=%d tail=%d\n",
2335a108d2d6SAdrian Chadd 	    __func__, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
2336d4365d16SAdrian Chadd 	    tap->txa_start, tap->txa_wnd, index, cindex, tid->baw_head,
2337d4365d16SAdrian Chadd 	    tid->baw_tail);
2338eb6f0de0SAdrian Chadd 
2339eb6f0de0SAdrian Chadd 
2340eb6f0de0SAdrian Chadd #if 0
2341eb6f0de0SAdrian Chadd 	assert(tid->tx_buf[cindex] == NULL);
2342eb6f0de0SAdrian Chadd #endif
2343eb6f0de0SAdrian Chadd 	if (tid->tx_buf[cindex] != NULL) {
2344eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
2345eb6f0de0SAdrian Chadd 		    "%s: ba packet dup (index=%d, cindex=%d, "
2346eb6f0de0SAdrian Chadd 		    "head=%d, tail=%d)\n",
2347eb6f0de0SAdrian Chadd 		    __func__, index, cindex, tid->baw_head, tid->baw_tail);
2348eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
2349eb6f0de0SAdrian Chadd 		    "%s: BA bf: %p; seqno=%d ; new bf: %p; seqno=%d\n",
2350eb6f0de0SAdrian Chadd 		    __func__,
2351eb6f0de0SAdrian Chadd 		    tid->tx_buf[cindex],
2352eb6f0de0SAdrian Chadd 		    SEQNO(tid->tx_buf[cindex]->bf_state.bfs_seqno),
2353eb6f0de0SAdrian Chadd 		    bf,
2354eb6f0de0SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno)
2355eb6f0de0SAdrian Chadd 		);
2356eb6f0de0SAdrian Chadd 	}
2357eb6f0de0SAdrian Chadd 	tid->tx_buf[cindex] = bf;
2358eb6f0de0SAdrian Chadd 
2359d4365d16SAdrian Chadd 	if (index >= ((tid->baw_tail - tid->baw_head) &
2360d4365d16SAdrian Chadd 	    (ATH_TID_MAX_BUFS - 1))) {
2361eb6f0de0SAdrian Chadd 		tid->baw_tail = cindex;
2362eb6f0de0SAdrian Chadd 		INCR(tid->baw_tail, ATH_TID_MAX_BUFS);
2363eb6f0de0SAdrian Chadd 	}
2364eb6f0de0SAdrian Chadd }
2365eb6f0de0SAdrian Chadd 
2366eb6f0de0SAdrian Chadd /*
236738962489SAdrian Chadd  * Flip the BAW buffer entry over from the existing one to the new one.
236838962489SAdrian Chadd  *
236938962489SAdrian Chadd  * When software retransmitting a (sub-)frame, it is entirely possible that
237038962489SAdrian Chadd  * the frame ath_buf is marked as BUSY and can't be immediately reused.
237138962489SAdrian Chadd  * In that instance the buffer is cloned and the new buffer is used for
237238962489SAdrian Chadd  * retransmit. We thus need to update the ath_buf slot in the BAW buf
237338962489SAdrian Chadd  * tracking array to maintain consistency.
237438962489SAdrian Chadd  */
237538962489SAdrian Chadd static void
237638962489SAdrian Chadd ath_tx_switch_baw_buf(struct ath_softc *sc, struct ath_node *an,
237738962489SAdrian Chadd     struct ath_tid *tid, struct ath_buf *old_bf, struct ath_buf *new_bf)
237838962489SAdrian Chadd {
237938962489SAdrian Chadd 	int index, cindex;
238038962489SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
238138962489SAdrian Chadd 	int seqno = SEQNO(old_bf->bf_state.bfs_seqno);
238238962489SAdrian Chadd 
238338962489SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
2384c2ac9655SAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
238538962489SAdrian Chadd 
238638962489SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
238738962489SAdrian Chadd 	index  = ATH_BA_INDEX(tap->txa_start, seqno);
238838962489SAdrian Chadd 	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
238938962489SAdrian Chadd 
239038962489SAdrian Chadd 	/*
239138962489SAdrian Chadd 	 * Just warn for now; if it happens then we should find out
239238962489SAdrian Chadd 	 * about it. It's highly likely the aggregation session will
239338962489SAdrian Chadd 	 * soon hang.
239438962489SAdrian Chadd 	 */
239538962489SAdrian Chadd 	if (old_bf->bf_state.bfs_seqno != new_bf->bf_state.bfs_seqno) {
239638962489SAdrian Chadd 		device_printf(sc->sc_dev, "%s: retransmitted buffer"
239738962489SAdrian Chadd 		    " has mismatching seqno's, BA session may hang.\n",
239838962489SAdrian Chadd 		    __func__);
239938962489SAdrian Chadd 		device_printf(sc->sc_dev, "%s: old seqno=%d, new_seqno=%d\n",
240038962489SAdrian Chadd 		    __func__,
240138962489SAdrian Chadd 		    old_bf->bf_state.bfs_seqno,
240238962489SAdrian Chadd 		    new_bf->bf_state.bfs_seqno);
240338962489SAdrian Chadd 	}
240438962489SAdrian Chadd 
240538962489SAdrian Chadd 	if (tid->tx_buf[cindex] != old_bf) {
240638962489SAdrian Chadd 		device_printf(sc->sc_dev, "%s: ath_buf pointer incorrect; "
240738962489SAdrian Chadd 		    " has m BA session may hang.\n",
240838962489SAdrian Chadd 		    __func__);
240938962489SAdrian Chadd 		device_printf(sc->sc_dev, "%s: old bf=%p, new bf=%p\n",
241038962489SAdrian Chadd 		    __func__,
241138962489SAdrian Chadd 		    old_bf, new_bf);
241238962489SAdrian Chadd 	}
241338962489SAdrian Chadd 
241438962489SAdrian Chadd 	tid->tx_buf[cindex] = new_bf;
241538962489SAdrian Chadd }
241638962489SAdrian Chadd 
241738962489SAdrian Chadd /*
2418eb6f0de0SAdrian Chadd  * seq_start - left edge of BAW
2419eb6f0de0SAdrian Chadd  * seq_next - current/next sequence number to allocate
2420eb6f0de0SAdrian Chadd  *
2421eb6f0de0SAdrian Chadd  * Since the BAW status may be modified by both the ath task and
2422eb6f0de0SAdrian Chadd  * the net80211/ifnet contexts, the TID must be locked.
2423eb6f0de0SAdrian Chadd  */
2424eb6f0de0SAdrian Chadd static void
2425eb6f0de0SAdrian Chadd ath_tx_update_baw(struct ath_softc *sc, struct ath_node *an,
2426eb6f0de0SAdrian Chadd     struct ath_tid *tid, const struct ath_buf *bf)
2427eb6f0de0SAdrian Chadd {
2428eb6f0de0SAdrian Chadd 	int index, cindex;
2429eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
2430eb6f0de0SAdrian Chadd 	int seqno = SEQNO(bf->bf_state.bfs_seqno);
2431eb6f0de0SAdrian Chadd 
2432eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
24334b6db404SAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
2434eb6f0de0SAdrian Chadd 
2435eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
2436eb6f0de0SAdrian Chadd 	index  = ATH_BA_INDEX(tap->txa_start, seqno);
2437eb6f0de0SAdrian Chadd 	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
2438eb6f0de0SAdrian Chadd 
2439eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2440a108d2d6SAdrian Chadd 	    "%s: tid=%d, baw=%d:%d, seqno=%d, index=%d, cindex=%d, "
2441d4365d16SAdrian Chadd 	    "baw head=%d, tail=%d\n",
2442a108d2d6SAdrian Chadd 	    __func__, tid->tid, tap->txa_start, tap->txa_wnd, seqno, index,
2443eb6f0de0SAdrian Chadd 	    cindex, tid->baw_head, tid->baw_tail);
2444eb6f0de0SAdrian Chadd 
2445eb6f0de0SAdrian Chadd 	/*
2446eb6f0de0SAdrian Chadd 	 * If this occurs then we have a big problem - something else
2447eb6f0de0SAdrian Chadd 	 * has slid tap->txa_start along without updating the BAW
2448eb6f0de0SAdrian Chadd 	 * tracking start/end pointers. Thus the TX BAW state is now
2449eb6f0de0SAdrian Chadd 	 * completely busted.
2450eb6f0de0SAdrian Chadd 	 *
2451eb6f0de0SAdrian Chadd 	 * But for now, since I haven't yet fixed TDMA and buffer cloning,
2452eb6f0de0SAdrian Chadd 	 * it's quite possible that a cloned buffer is making its way
2453eb6f0de0SAdrian Chadd 	 * here and causing it to fire off. Disable TDMA for now.
2454eb6f0de0SAdrian Chadd 	 */
2455eb6f0de0SAdrian Chadd 	if (tid->tx_buf[cindex] != bf) {
2456eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
2457eb6f0de0SAdrian Chadd 		    "%s: comp bf=%p, seq=%d; slot bf=%p, seqno=%d\n",
2458eb6f0de0SAdrian Chadd 		    __func__,
2459eb6f0de0SAdrian Chadd 		    bf, SEQNO(bf->bf_state.bfs_seqno),
2460eb6f0de0SAdrian Chadd 		    tid->tx_buf[cindex],
2461eb6f0de0SAdrian Chadd 		    SEQNO(tid->tx_buf[cindex]->bf_state.bfs_seqno));
2462eb6f0de0SAdrian Chadd 	}
2463eb6f0de0SAdrian Chadd 
2464eb6f0de0SAdrian Chadd 	tid->tx_buf[cindex] = NULL;
2465eb6f0de0SAdrian Chadd 
2466d4365d16SAdrian Chadd 	while (tid->baw_head != tid->baw_tail &&
2467d4365d16SAdrian Chadd 	    !tid->tx_buf[tid->baw_head]) {
2468eb6f0de0SAdrian Chadd 		INCR(tap->txa_start, IEEE80211_SEQ_RANGE);
2469eb6f0de0SAdrian Chadd 		INCR(tid->baw_head, ATH_TID_MAX_BUFS);
2470eb6f0de0SAdrian Chadd 	}
2471d4365d16SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2472d4365d16SAdrian Chadd 	    "%s: baw is now %d:%d, baw head=%d\n",
2473eb6f0de0SAdrian Chadd 	    __func__, tap->txa_start, tap->txa_wnd, tid->baw_head);
2474eb6f0de0SAdrian Chadd }
2475eb6f0de0SAdrian Chadd 
2476eb6f0de0SAdrian Chadd /*
2477eb6f0de0SAdrian Chadd  * Mark the current node/TID as ready to TX.
2478eb6f0de0SAdrian Chadd  *
2479eb6f0de0SAdrian Chadd  * This is done to make it easy for the software scheduler to
2480eb6f0de0SAdrian Chadd  * find which nodes have data to send.
2481eb6f0de0SAdrian Chadd  *
2482eb6f0de0SAdrian Chadd  * The TXQ lock must be held.
2483eb6f0de0SAdrian Chadd  */
2484eb6f0de0SAdrian Chadd static void
2485eb6f0de0SAdrian Chadd ath_tx_tid_sched(struct ath_softc *sc, struct ath_tid *tid)
2486eb6f0de0SAdrian Chadd {
2487eb6f0de0SAdrian Chadd 	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
2488eb6f0de0SAdrian Chadd 
2489eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
2490eb6f0de0SAdrian Chadd 
2491eb6f0de0SAdrian Chadd 	if (tid->paused)
2492eb6f0de0SAdrian Chadd 		return;		/* paused, can't schedule yet */
2493eb6f0de0SAdrian Chadd 
2494eb6f0de0SAdrian Chadd 	if (tid->sched)
2495eb6f0de0SAdrian Chadd 		return;		/* already scheduled */
2496eb6f0de0SAdrian Chadd 
2497eb6f0de0SAdrian Chadd 	tid->sched = 1;
2498eb6f0de0SAdrian Chadd 
2499eb6f0de0SAdrian Chadd 	TAILQ_INSERT_TAIL(&txq->axq_tidq, tid, axq_qelem);
2500eb6f0de0SAdrian Chadd }
2501eb6f0de0SAdrian Chadd 
2502eb6f0de0SAdrian Chadd /*
2503eb6f0de0SAdrian Chadd  * Mark the current node as no longer needing to be polled for
2504eb6f0de0SAdrian Chadd  * TX packets.
2505eb6f0de0SAdrian Chadd  *
2506eb6f0de0SAdrian Chadd  * The TXQ lock must be held.
2507eb6f0de0SAdrian Chadd  */
2508eb6f0de0SAdrian Chadd static void
2509eb6f0de0SAdrian Chadd ath_tx_tid_unsched(struct ath_softc *sc, struct ath_tid *tid)
2510eb6f0de0SAdrian Chadd {
2511eb6f0de0SAdrian Chadd 	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
2512eb6f0de0SAdrian Chadd 
2513eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
2514eb6f0de0SAdrian Chadd 
2515eb6f0de0SAdrian Chadd 	if (tid->sched == 0)
2516eb6f0de0SAdrian Chadd 		return;
2517eb6f0de0SAdrian Chadd 
2518eb6f0de0SAdrian Chadd 	tid->sched = 0;
2519eb6f0de0SAdrian Chadd 	TAILQ_REMOVE(&txq->axq_tidq, tid, axq_qelem);
2520eb6f0de0SAdrian Chadd }
2521eb6f0de0SAdrian Chadd 
2522eb6f0de0SAdrian Chadd /*
2523eb6f0de0SAdrian Chadd  * Assign a sequence number manually to the given frame.
2524eb6f0de0SAdrian Chadd  *
2525eb6f0de0SAdrian Chadd  * This should only be called for A-MPDU TX frames.
2526eb6f0de0SAdrian Chadd  */
2527a108d2d6SAdrian Chadd static ieee80211_seq
2528eb6f0de0SAdrian Chadd ath_tx_tid_seqno_assign(struct ath_softc *sc, struct ieee80211_node *ni,
2529eb6f0de0SAdrian Chadd     struct ath_buf *bf, struct mbuf *m0)
2530eb6f0de0SAdrian Chadd {
2531eb6f0de0SAdrian Chadd 	struct ieee80211_frame *wh;
2532eb6f0de0SAdrian Chadd 	int tid, pri;
2533eb6f0de0SAdrian Chadd 	ieee80211_seq seqno;
2534eb6f0de0SAdrian Chadd 	uint8_t subtype;
2535eb6f0de0SAdrian Chadd 
2536eb6f0de0SAdrian Chadd 	/* TID lookup */
2537eb6f0de0SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
2538eb6f0de0SAdrian Chadd 	pri = M_WME_GETAC(m0);			/* honor classification */
2539eb6f0de0SAdrian Chadd 	tid = WME_AC_TO_TID(pri);
2540a108d2d6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: pri=%d, tid=%d, qos has seq=%d\n",
2541a108d2d6SAdrian Chadd 	    __func__, pri, tid, IEEE80211_QOS_HAS_SEQ(wh));
2542eb6f0de0SAdrian Chadd 
2543eb6f0de0SAdrian Chadd 	/* XXX Is it a control frame? Ignore */
2544eb6f0de0SAdrian Chadd 
2545eb6f0de0SAdrian Chadd 	/* Does the packet require a sequence number? */
2546eb6f0de0SAdrian Chadd 	if (! IEEE80211_QOS_HAS_SEQ(wh))
2547eb6f0de0SAdrian Chadd 		return -1;
2548eb6f0de0SAdrian Chadd 
25497561cb5cSAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, &(ATH_NODE(ni)->an_tid[tid]));
25507561cb5cSAdrian Chadd 
2551eb6f0de0SAdrian Chadd 	/*
2552eb6f0de0SAdrian Chadd 	 * Is it a QOS NULL Data frame? Give it a sequence number from
2553eb6f0de0SAdrian Chadd 	 * the default TID (IEEE80211_NONQOS_TID.)
2554eb6f0de0SAdrian Chadd 	 *
2555eb6f0de0SAdrian Chadd 	 * The RX path of everything I've looked at doesn't include the NULL
2556eb6f0de0SAdrian Chadd 	 * data frame sequence number in the aggregation state updates, so
2557eb6f0de0SAdrian Chadd 	 * assigning it a sequence number there will cause a BAW hole on the
2558eb6f0de0SAdrian Chadd 	 * RX side.
2559eb6f0de0SAdrian Chadd 	 */
2560eb6f0de0SAdrian Chadd 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
2561eb6f0de0SAdrian Chadd 	if (subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL) {
25627561cb5cSAdrian Chadd 		/* XXX no locking for this TID? This is a bit of a problem. */
2563eb6f0de0SAdrian Chadd 		seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID];
2564eb6f0de0SAdrian Chadd 		INCR(ni->ni_txseqs[IEEE80211_NONQOS_TID], IEEE80211_SEQ_RANGE);
2565eb6f0de0SAdrian Chadd 	} else {
2566eb6f0de0SAdrian Chadd 		/* Manually assign sequence number */
2567eb6f0de0SAdrian Chadd 		seqno = ni->ni_txseqs[tid];
2568eb6f0de0SAdrian Chadd 		INCR(ni->ni_txseqs[tid], IEEE80211_SEQ_RANGE);
2569eb6f0de0SAdrian Chadd 	}
2570eb6f0de0SAdrian Chadd 	*(uint16_t *)&wh->i_seq[0] = htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
2571eb6f0de0SAdrian Chadd 	M_SEQNO_SET(m0, seqno);
2572eb6f0de0SAdrian Chadd 
2573eb6f0de0SAdrian Chadd 	/* Return so caller can do something with it if needed */
2574a108d2d6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s:  -> seqno=%d\n", __func__, seqno);
2575eb6f0de0SAdrian Chadd 	return seqno;
2576eb6f0de0SAdrian Chadd }
2577eb6f0de0SAdrian Chadd 
2578eb6f0de0SAdrian Chadd /*
2579eb6f0de0SAdrian Chadd  * Attempt to direct dispatch an aggregate frame to hardware.
2580eb6f0de0SAdrian Chadd  * If the frame is out of BAW, queue.
2581eb6f0de0SAdrian Chadd  * Otherwise, schedule it as a single frame.
2582eb6f0de0SAdrian Chadd  */
2583eb6f0de0SAdrian Chadd static void
258446634305SAdrian Chadd ath_tx_xmit_aggr(struct ath_softc *sc, struct ath_node *an,
258546634305SAdrian Chadd     struct ath_txq *txq, struct ath_buf *bf)
2586eb6f0de0SAdrian Chadd {
2587eb6f0de0SAdrian Chadd 	struct ath_tid *tid = &an->an_tid[bf->bf_state.bfs_tid];
258846634305SAdrian Chadd //	struct ath_txq *txq = bf->bf_state.bfs_txq;
2589eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
2590eb6f0de0SAdrian Chadd 
259146634305SAdrian Chadd 	if (txq != bf->bf_state.bfs_txq) {
259246634305SAdrian Chadd 		device_printf(sc->sc_dev, "%s: txq %d != bfs_txq %d!\n",
259346634305SAdrian Chadd 		    __func__,
259446634305SAdrian Chadd 		    txq->axq_qnum,
259546634305SAdrian Chadd 		    bf->bf_state.bfs_txq->axq_qnum);
259646634305SAdrian Chadd 	}
259746634305SAdrian Chadd 
2598eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
2599c2ac9655SAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
2600eb6f0de0SAdrian Chadd 
2601eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
2602eb6f0de0SAdrian Chadd 
2603eb6f0de0SAdrian Chadd 	/* paused? queue */
2604eb6f0de0SAdrian Chadd 	if (tid->paused) {
26054547f047SAdrian Chadd 		ATH_TXQ_INSERT_HEAD(tid, bf, bf_list);
26060f04c5a2SAdrian Chadd 		/* XXX don't sched - we're paused! */
2607eb6f0de0SAdrian Chadd 		return;
2608eb6f0de0SAdrian Chadd 	}
2609eb6f0de0SAdrian Chadd 
2610eb6f0de0SAdrian Chadd 	/* outside baw? queue */
2611eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_dobaw &&
2612eb6f0de0SAdrian Chadd 	    (! BAW_WITHIN(tap->txa_start, tap->txa_wnd,
2613eb6f0de0SAdrian Chadd 	    SEQNO(bf->bf_state.bfs_seqno)))) {
2614ba0e58f4SAdrian Chadd 		ATH_TXQ_INSERT_HEAD(tid, bf, bf_list);
2615eb6f0de0SAdrian Chadd 		ath_tx_tid_sched(sc, tid);
2616eb6f0de0SAdrian Chadd 		return;
2617eb6f0de0SAdrian Chadd 	}
2618eb6f0de0SAdrian Chadd 
26192a9f83afSAdrian Chadd 	/*
26202a9f83afSAdrian Chadd 	 * This is a temporary check and should be removed once
26212a9f83afSAdrian Chadd 	 * all the relevant code paths have been fixed.
26222a9f83afSAdrian Chadd 	 *
26232a9f83afSAdrian Chadd 	 * During aggregate retries, it's possible that the head
26242a9f83afSAdrian Chadd 	 * frame will fail (which has the bfs_aggr and bfs_nframes
26252a9f83afSAdrian Chadd 	 * fields set for said aggregate) and will be retried as
26262a9f83afSAdrian Chadd 	 * a single frame.  In this instance, the values should
26272a9f83afSAdrian Chadd 	 * be reset or the completion code will get upset with you.
26282a9f83afSAdrian Chadd 	 */
26292a9f83afSAdrian Chadd 	if (bf->bf_state.bfs_aggr != 0 || bf->bf_state.bfs_nframes > 1) {
26302a9f83afSAdrian Chadd 		device_printf(sc->sc_dev, "%s: bfs_aggr=%d, bfs_nframes=%d\n",
26312a9f83afSAdrian Chadd 		    __func__,
26322a9f83afSAdrian Chadd 		    bf->bf_state.bfs_aggr,
26332a9f83afSAdrian Chadd 		    bf->bf_state.bfs_nframes);
26342a9f83afSAdrian Chadd 		bf->bf_state.bfs_aggr = 0;
26352a9f83afSAdrian Chadd 		bf->bf_state.bfs_nframes = 1;
26362a9f83afSAdrian Chadd 	}
26372a9f83afSAdrian Chadd 
26384e81f27cSAdrian Chadd 	/* Update CLRDMASK just before this frame is queued */
26394e81f27cSAdrian Chadd 	ath_tx_update_clrdmask(sc, tid, bf);
26404e81f27cSAdrian Chadd 
2641eb6f0de0SAdrian Chadd 	/* Direct dispatch to hardware */
2642eb6f0de0SAdrian Chadd 	ath_tx_do_ratelookup(sc, bf);
2643e2e4a2c2SAdrian Chadd 	ath_tx_calc_duration(sc, bf);
2644e2e4a2c2SAdrian Chadd 	ath_tx_calc_protection(sc, bf);
2645eb6f0de0SAdrian Chadd 	ath_tx_set_rtscts(sc, bf);
2646e2e4a2c2SAdrian Chadd 	ath_tx_rate_fill_rcflags(sc, bf);
2647eb6f0de0SAdrian Chadd 	ath_tx_setds(sc, bf);
2648eb6f0de0SAdrian Chadd 
2649eb6f0de0SAdrian Chadd 	/* Statistics */
2650eb6f0de0SAdrian Chadd 	sc->sc_aggr_stats.aggr_low_hwq_single_pkt++;
2651eb6f0de0SAdrian Chadd 
2652eb6f0de0SAdrian Chadd 	/* Track per-TID hardware queue depth correctly */
2653eb6f0de0SAdrian Chadd 	tid->hwq_depth++;
2654eb6f0de0SAdrian Chadd 
2655eb6f0de0SAdrian Chadd 	/* Add to BAW */
2656eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_dobaw) {
2657eb6f0de0SAdrian Chadd 		ath_tx_addto_baw(sc, an, tid, bf);
2658eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_addedbaw = 1;
2659eb6f0de0SAdrian Chadd 	}
2660eb6f0de0SAdrian Chadd 
2661eb6f0de0SAdrian Chadd 	/* Set completion handler, multi-frame aggregate or not */
2662eb6f0de0SAdrian Chadd 	bf->bf_comp = ath_tx_aggr_comp;
2663eb6f0de0SAdrian Chadd 
2664eb6f0de0SAdrian Chadd 	/* Hand off to hardware */
2665eb6f0de0SAdrian Chadd 	ath_tx_handoff(sc, txq, bf);
2666eb6f0de0SAdrian Chadd }
2667eb6f0de0SAdrian Chadd 
2668eb6f0de0SAdrian Chadd /*
2669eb6f0de0SAdrian Chadd  * Attempt to send the packet.
2670eb6f0de0SAdrian Chadd  * If the queue isn't busy, direct-dispatch.
2671eb6f0de0SAdrian Chadd  * If the queue is busy enough, queue the given packet on the
2672eb6f0de0SAdrian Chadd  *  relevant software queue.
2673eb6f0de0SAdrian Chadd  */
2674eb6f0de0SAdrian Chadd void
2675eb6f0de0SAdrian Chadd ath_tx_swq(struct ath_softc *sc, struct ieee80211_node *ni, struct ath_txq *txq,
2676eb6f0de0SAdrian Chadd     struct ath_buf *bf)
2677eb6f0de0SAdrian Chadd {
2678eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
2679eb6f0de0SAdrian Chadd 	struct ieee80211_frame *wh;
2680eb6f0de0SAdrian Chadd 	struct ath_tid *atid;
2681eb6f0de0SAdrian Chadd 	int pri, tid;
2682eb6f0de0SAdrian Chadd 	struct mbuf *m0 = bf->bf_m;
2683eb6f0de0SAdrian Chadd 
26847561cb5cSAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
26857561cb5cSAdrian Chadd 
2686eb6f0de0SAdrian Chadd 	/* Fetch the TID - non-QoS frames get assigned to TID 16 */
2687eb6f0de0SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
2688eb6f0de0SAdrian Chadd 	pri = ath_tx_getac(sc, m0);
2689eb6f0de0SAdrian Chadd 	tid = ath_tx_gettid(sc, m0);
2690eb6f0de0SAdrian Chadd 	atid = &an->an_tid[tid];
2691eb6f0de0SAdrian Chadd 
2692c2ac9655SAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, atid);
2693c2ac9655SAdrian Chadd 
2694a108d2d6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bf=%p, pri=%d, tid=%d, qos=%d\n",
2695a108d2d6SAdrian Chadd 	    __func__, bf, pri, tid, IEEE80211_QOS_HAS_SEQ(wh));
2696eb6f0de0SAdrian Chadd 
2697eb6f0de0SAdrian Chadd 	/* Set local packet state, used to queue packets to hardware */
269846634305SAdrian Chadd 	/* XXX potentially duplicate info, re-check */
269946634305SAdrian Chadd 	/* XXX remember, txq must be the hardware queue, not the av_mcastq */
2700eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_tid = tid;
2701eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txq = txq;
2702eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_pri = pri;
2703eb6f0de0SAdrian Chadd 
2704eb6f0de0SAdrian Chadd 	/*
2705eb6f0de0SAdrian Chadd 	 * If the hardware queue isn't busy, queue it directly.
2706eb6f0de0SAdrian Chadd 	 * If the hardware queue is busy, queue it.
2707eb6f0de0SAdrian Chadd 	 * If the TID is paused or the traffic it outside BAW, software
2708eb6f0de0SAdrian Chadd 	 * queue it.
2709eb6f0de0SAdrian Chadd 	 */
2710eb6f0de0SAdrian Chadd 	if (atid->paused) {
2711eb6f0de0SAdrian Chadd 		/* TID is paused, queue */
2712a108d2d6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: paused\n", __func__);
2713eb6f0de0SAdrian Chadd 		ATH_TXQ_INSERT_TAIL(atid, bf, bf_list);
2714eb6f0de0SAdrian Chadd 	} else if (ath_tx_ampdu_pending(sc, an, tid)) {
2715eb6f0de0SAdrian Chadd 		/* AMPDU pending; queue */
2716a108d2d6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: pending\n", __func__);
2717eb6f0de0SAdrian Chadd 		ATH_TXQ_INSERT_TAIL(atid, bf, bf_list);
2718eb6f0de0SAdrian Chadd 		/* XXX sched? */
2719eb6f0de0SAdrian Chadd 	} else if (ath_tx_ampdu_running(sc, an, tid)) {
2720eb6f0de0SAdrian Chadd 		/* AMPDU running, attempt direct dispatch if possible */
272139f24578SAdrian Chadd 
272239f24578SAdrian Chadd 		/*
272339f24578SAdrian Chadd 		 * Always queue the frame to the tail of the list.
272439f24578SAdrian Chadd 		 */
272539f24578SAdrian Chadd 		ATH_TXQ_INSERT_TAIL(atid, bf, bf_list);
272639f24578SAdrian Chadd 
272739f24578SAdrian Chadd 		/*
272839f24578SAdrian Chadd 		 * If the hardware queue isn't busy, direct dispatch
272939f24578SAdrian Chadd 		 * the head frame in the list.  Don't schedule the
273039f24578SAdrian Chadd 		 * TID - let it build some more frames first?
273139f24578SAdrian Chadd 		 *
273239f24578SAdrian Chadd 		 * Otherwise, schedule the TID.
273339f24578SAdrian Chadd 		 */
2734d4365d16SAdrian Chadd 		if (txq->axq_depth < sc->sc_hwq_limit) {
273539f24578SAdrian Chadd 			bf = TAILQ_FIRST(&atid->axq_q);
273639f24578SAdrian Chadd 			ATH_TXQ_REMOVE(atid, bf, bf_list);
27372a9f83afSAdrian Chadd 
27382a9f83afSAdrian Chadd 			/*
27392a9f83afSAdrian Chadd 			 * Ensure it's definitely treated as a non-AMPDU
27402a9f83afSAdrian Chadd 			 * frame - this information may have been left
27412a9f83afSAdrian Chadd 			 * over from a previous attempt.
27422a9f83afSAdrian Chadd 			 */
27432a9f83afSAdrian Chadd 			bf->bf_state.bfs_aggr = 0;
27442a9f83afSAdrian Chadd 			bf->bf_state.bfs_nframes = 1;
27452a9f83afSAdrian Chadd 
27462a9f83afSAdrian Chadd 			/* Queue to the hardware */
274746634305SAdrian Chadd 			ath_tx_xmit_aggr(sc, an, txq, bf);
2748a108d2d6SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX,
2749a108d2d6SAdrian Chadd 			    "%s: xmit_aggr\n",
2750a108d2d6SAdrian Chadd 			    __func__);
2751d4365d16SAdrian Chadd 		} else {
2752d4365d16SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX,
2753a108d2d6SAdrian Chadd 			    "%s: ampdu; swq'ing\n",
2754a108d2d6SAdrian Chadd 			    __func__);
2755eb6f0de0SAdrian Chadd 			ath_tx_tid_sched(sc, atid);
2756eb6f0de0SAdrian Chadd 		}
2757eb6f0de0SAdrian Chadd 	} else if (txq->axq_depth < sc->sc_hwq_limit) {
2758eb6f0de0SAdrian Chadd 		/* AMPDU not running, attempt direct dispatch */
2759a108d2d6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: xmit_normal\n", __func__);
2760eb6f0de0SAdrian Chadd 		ath_tx_xmit_normal(sc, txq, bf);
2761eb6f0de0SAdrian Chadd 	} else {
2762eb6f0de0SAdrian Chadd 		/* Busy; queue */
2763a108d2d6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: swq'ing\n", __func__);
2764eb6f0de0SAdrian Chadd 		ATH_TXQ_INSERT_TAIL(atid, bf, bf_list);
2765eb6f0de0SAdrian Chadd 		ath_tx_tid_sched(sc, atid);
2766eb6f0de0SAdrian Chadd 	}
2767eb6f0de0SAdrian Chadd }
2768eb6f0de0SAdrian Chadd 
2769eb6f0de0SAdrian Chadd /*
2770eb6f0de0SAdrian Chadd  * Configure the per-TID node state.
2771eb6f0de0SAdrian Chadd  *
2772eb6f0de0SAdrian Chadd  * This likely belongs in if_ath_node.c but I can't think of anywhere
2773eb6f0de0SAdrian Chadd  * else to put it just yet.
2774eb6f0de0SAdrian Chadd  *
2775eb6f0de0SAdrian Chadd  * This sets up the SLISTs and the mutex as appropriate.
2776eb6f0de0SAdrian Chadd  */
2777eb6f0de0SAdrian Chadd void
2778eb6f0de0SAdrian Chadd ath_tx_tid_init(struct ath_softc *sc, struct ath_node *an)
2779eb6f0de0SAdrian Chadd {
2780eb6f0de0SAdrian Chadd 	int i, j;
2781eb6f0de0SAdrian Chadd 	struct ath_tid *atid;
2782eb6f0de0SAdrian Chadd 
2783eb6f0de0SAdrian Chadd 	for (i = 0; i < IEEE80211_TID_SIZE; i++) {
2784eb6f0de0SAdrian Chadd 		atid = &an->an_tid[i];
2785f1bc738eSAdrian Chadd 
2786f1bc738eSAdrian Chadd 		/* XXX now with this bzer(), is the field 0'ing needed? */
2787f1bc738eSAdrian Chadd 		bzero(atid, sizeof(*atid));
2788f1bc738eSAdrian Chadd 
2789eb6f0de0SAdrian Chadd 		TAILQ_INIT(&atid->axq_q);
2790f1bc738eSAdrian Chadd 		TAILQ_INIT(&atid->filtq.axq_q);
2791eb6f0de0SAdrian Chadd 		atid->tid = i;
2792eb6f0de0SAdrian Chadd 		atid->an = an;
2793eb6f0de0SAdrian Chadd 		for (j = 0; j < ATH_TID_MAX_BUFS; j++)
2794eb6f0de0SAdrian Chadd 			atid->tx_buf[j] = NULL;
2795eb6f0de0SAdrian Chadd 		atid->baw_head = atid->baw_tail = 0;
2796eb6f0de0SAdrian Chadd 		atid->paused = 0;
2797eb6f0de0SAdrian Chadd 		atid->sched = 0;
2798eb6f0de0SAdrian Chadd 		atid->hwq_depth = 0;
2799eb6f0de0SAdrian Chadd 		atid->cleanup_inprogress = 0;
2800f1bc738eSAdrian Chadd 		atid->clrdmask = 1;	/* Always start by setting this bit */
2801eb6f0de0SAdrian Chadd 		if (i == IEEE80211_NONQOS_TID)
2802eb6f0de0SAdrian Chadd 			atid->ac = WME_AC_BE;
2803eb6f0de0SAdrian Chadd 		else
2804eb6f0de0SAdrian Chadd 			atid->ac = TID_TO_WME_AC(i);
2805eb6f0de0SAdrian Chadd 	}
2806eb6f0de0SAdrian Chadd }
2807eb6f0de0SAdrian Chadd 
2808eb6f0de0SAdrian Chadd /*
2809eb6f0de0SAdrian Chadd  * Pause the current TID. This stops packets from being transmitted
2810eb6f0de0SAdrian Chadd  * on it.
2811eb6f0de0SAdrian Chadd  *
2812eb6f0de0SAdrian Chadd  * Since this is also called from upper layers as well as the driver,
2813eb6f0de0SAdrian Chadd  * it will get the TID lock.
2814eb6f0de0SAdrian Chadd  */
2815eb6f0de0SAdrian Chadd static void
2816eb6f0de0SAdrian Chadd ath_tx_tid_pause(struct ath_softc *sc, struct ath_tid *tid)
2817eb6f0de0SAdrian Chadd {
281888b3d483SAdrian Chadd 
281988b3d483SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
2820eb6f0de0SAdrian Chadd 	tid->paused++;
2821eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: paused = %d\n",
2822eb6f0de0SAdrian Chadd 	    __func__, tid->paused);
2823eb6f0de0SAdrian Chadd }
2824eb6f0de0SAdrian Chadd 
2825eb6f0de0SAdrian Chadd /*
2826eb6f0de0SAdrian Chadd  * Unpause the current TID, and schedule it if needed.
2827eb6f0de0SAdrian Chadd  */
2828eb6f0de0SAdrian Chadd static void
2829eb6f0de0SAdrian Chadd ath_tx_tid_resume(struct ath_softc *sc, struct ath_tid *tid)
2830eb6f0de0SAdrian Chadd {
2831eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
2832eb6f0de0SAdrian Chadd 
2833eb6f0de0SAdrian Chadd 	tid->paused--;
2834eb6f0de0SAdrian Chadd 
2835eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: unpaused = %d\n",
2836eb6f0de0SAdrian Chadd 	    __func__, tid->paused);
2837eb6f0de0SAdrian Chadd 
2838eb6f0de0SAdrian Chadd 	if (tid->paused || tid->axq_depth == 0) {
2839eb6f0de0SAdrian Chadd 		return;
2840eb6f0de0SAdrian Chadd 	}
2841eb6f0de0SAdrian Chadd 
2842f1bc738eSAdrian Chadd 	/* XXX isfiltered shouldn't ever be 0 at this point */
2843f1bc738eSAdrian Chadd 	if (tid->isfiltered == 1) {
2844f1bc738eSAdrian Chadd 		device_printf(sc->sc_dev, "%s: filtered?!\n", __func__);
2845f1bc738eSAdrian Chadd 		return;
2846f1bc738eSAdrian Chadd 	}
2847f1bc738eSAdrian Chadd 
28484e81f27cSAdrian Chadd 	/*
28494e81f27cSAdrian Chadd 	 * Override the clrdmask configuration for the next frame,
28504e81f27cSAdrian Chadd 	 * just to get the ball rolling.
28514e81f27cSAdrian Chadd 	 */
28524e81f27cSAdrian Chadd 	tid->clrdmask = 1;
28534e81f27cSAdrian Chadd 
2854eb6f0de0SAdrian Chadd 	ath_tx_tid_sched(sc, tid);
2855eb6f0de0SAdrian Chadd 	/* Punt some frames to the hardware if needed */
285603e9308fSAdrian Chadd 	//ath_txq_sched(sc, sc->sc_ac2q[tid->ac]);
285703e9308fSAdrian Chadd 	taskqueue_enqueue(sc->sc_tq, &sc->sc_txqtask);
2858eb6f0de0SAdrian Chadd }
2859eb6f0de0SAdrian Chadd 
2860eb6f0de0SAdrian Chadd /*
2861f1bc738eSAdrian Chadd  * Add the given ath_buf to the TID filtered frame list.
2862f1bc738eSAdrian Chadd  * This requires the TID be filtered.
2863f1bc738eSAdrian Chadd  */
2864f1bc738eSAdrian Chadd static void
2865f1bc738eSAdrian Chadd ath_tx_tid_filt_addbuf(struct ath_softc *sc, struct ath_tid *tid,
2866f1bc738eSAdrian Chadd     struct ath_buf *bf)
2867f1bc738eSAdrian Chadd {
2868f1bc738eSAdrian Chadd 
2869f1bc738eSAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
2870f1bc738eSAdrian Chadd 	if (! tid->isfiltered)
2871f1bc738eSAdrian Chadd 		device_printf(sc->sc_dev, "%s: not filtered?!\n", __func__);
2872f1bc738eSAdrian Chadd 
2873f1bc738eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_FILT, "%s: bf=%p\n", __func__, bf);
2874f1bc738eSAdrian Chadd 
2875f1bc738eSAdrian Chadd 	/* Set the retry bit and bump the retry counter */
2876f1bc738eSAdrian Chadd 	ath_tx_set_retry(sc, bf);
2877f1bc738eSAdrian Chadd 	sc->sc_stats.ast_tx_swfiltered++;
2878f1bc738eSAdrian Chadd 
2879f1bc738eSAdrian Chadd 	ATH_TXQ_INSERT_TAIL(&tid->filtq, bf, bf_list);
2880f1bc738eSAdrian Chadd }
2881f1bc738eSAdrian Chadd 
2882f1bc738eSAdrian Chadd /*
2883f1bc738eSAdrian Chadd  * Handle a completed filtered frame from the given TID.
2884f1bc738eSAdrian Chadd  * This just enables/pauses the filtered frame state if required
2885f1bc738eSAdrian Chadd  * and appends the filtered frame to the filtered queue.
2886f1bc738eSAdrian Chadd  */
2887f1bc738eSAdrian Chadd static void
2888f1bc738eSAdrian Chadd ath_tx_tid_filt_comp_buf(struct ath_softc *sc, struct ath_tid *tid,
2889f1bc738eSAdrian Chadd     struct ath_buf *bf)
2890f1bc738eSAdrian Chadd {
2891f1bc738eSAdrian Chadd 
2892f1bc738eSAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
2893f1bc738eSAdrian Chadd 
2894f1bc738eSAdrian Chadd 	if (! tid->isfiltered) {
2895f1bc738eSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT, "%s: filter transition\n",
2896f1bc738eSAdrian Chadd 		    __func__);
2897f1bc738eSAdrian Chadd 		tid->isfiltered = 1;
2898f1bc738eSAdrian Chadd 		ath_tx_tid_pause(sc, tid);
2899f1bc738eSAdrian Chadd 	}
2900f1bc738eSAdrian Chadd 
2901f1bc738eSAdrian Chadd 	/* Add the frame to the filter queue */
2902f1bc738eSAdrian Chadd 	ath_tx_tid_filt_addbuf(sc, tid, bf);
2903f1bc738eSAdrian Chadd }
2904f1bc738eSAdrian Chadd 
2905f1bc738eSAdrian Chadd /*
2906f1bc738eSAdrian Chadd  * Complete the filtered frame TX completion.
2907f1bc738eSAdrian Chadd  *
2908f1bc738eSAdrian Chadd  * If there are no more frames in the hardware queue, unpause/unfilter
2909f1bc738eSAdrian Chadd  * the TID if applicable.  Otherwise we will wait for a node PS transition
2910f1bc738eSAdrian Chadd  * to unfilter.
2911f1bc738eSAdrian Chadd  */
2912f1bc738eSAdrian Chadd static void
2913f1bc738eSAdrian Chadd ath_tx_tid_filt_comp_complete(struct ath_softc *sc, struct ath_tid *tid)
2914f1bc738eSAdrian Chadd {
2915f1bc738eSAdrian Chadd 	struct ath_buf *bf;
2916f1bc738eSAdrian Chadd 
2917f1bc738eSAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
2918f1bc738eSAdrian Chadd 
2919f1bc738eSAdrian Chadd 	if (tid->hwq_depth != 0)
2920f1bc738eSAdrian Chadd 		return;
2921f1bc738eSAdrian Chadd 
2922f1bc738eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_FILT, "%s: hwq=0, transition back\n",
2923f1bc738eSAdrian Chadd 	    __func__);
2924f1bc738eSAdrian Chadd 	tid->isfiltered = 0;
2925f1bc738eSAdrian Chadd 	tid->clrdmask = 1;
2926f1bc738eSAdrian Chadd 
2927f1bc738eSAdrian Chadd 	/* XXX this is really quite inefficient */
2928f1bc738eSAdrian Chadd 	while ((bf = TAILQ_LAST(&tid->filtq.axq_q, ath_bufhead_s)) != NULL) {
2929f1bc738eSAdrian Chadd 		ATH_TXQ_REMOVE(&tid->filtq, bf, bf_list);
2930f1bc738eSAdrian Chadd 		ATH_TXQ_INSERT_HEAD(tid, bf, bf_list);
2931f1bc738eSAdrian Chadd 	}
2932f1bc738eSAdrian Chadd 
2933f1bc738eSAdrian Chadd 	ath_tx_tid_resume(sc, tid);
2934f1bc738eSAdrian Chadd }
2935f1bc738eSAdrian Chadd 
2936f1bc738eSAdrian Chadd /*
2937f1bc738eSAdrian Chadd  * Called when a single (aggregate or otherwise) frame is completed.
2938f1bc738eSAdrian Chadd  *
2939f1bc738eSAdrian Chadd  * Returns 1 if the buffer could be added to the filtered list
2940f1bc738eSAdrian Chadd  * (cloned or otherwise), 0 if the buffer couldn't be added to the
2941f1bc738eSAdrian Chadd  * filtered list (failed clone; expired retry) and the caller should
2942f1bc738eSAdrian Chadd  * free it and handle it like a failure (eg by sending a BAR.)
2943f1bc738eSAdrian Chadd  */
2944f1bc738eSAdrian Chadd static int
2945f1bc738eSAdrian Chadd ath_tx_tid_filt_comp_single(struct ath_softc *sc, struct ath_tid *tid,
2946f1bc738eSAdrian Chadd     struct ath_buf *bf)
2947f1bc738eSAdrian Chadd {
2948f1bc738eSAdrian Chadd 	struct ath_buf *nbf;
2949f1bc738eSAdrian Chadd 	int retval;
2950f1bc738eSAdrian Chadd 
2951f1bc738eSAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
2952f1bc738eSAdrian Chadd 
2953f1bc738eSAdrian Chadd 	/*
2954f1bc738eSAdrian Chadd 	 * Don't allow a filtered frame to live forever.
2955f1bc738eSAdrian Chadd 	 */
2956f1bc738eSAdrian Chadd 	if (bf->bf_state.bfs_retries > SWMAX_RETRIES) {
2957f1bc738eSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
2958f1bc738eSAdrian Chadd 		    "%s: bf=%p, seqno=%d, exceeded retries\n",
2959f1bc738eSAdrian Chadd 		    __func__,
2960f1bc738eSAdrian Chadd 		    bf,
2961f1bc738eSAdrian Chadd 		    bf->bf_state.bfs_seqno);
2962f1bc738eSAdrian Chadd 		return (0);
2963f1bc738eSAdrian Chadd 	}
2964f1bc738eSAdrian Chadd 
2965f1bc738eSAdrian Chadd 	/*
2966f1bc738eSAdrian Chadd 	 * A busy buffer can't be added to the retry list.
2967f1bc738eSAdrian Chadd 	 * It needs to be cloned.
2968f1bc738eSAdrian Chadd 	 */
2969f1bc738eSAdrian Chadd 	if (bf->bf_flags & ATH_BUF_BUSY) {
2970f1bc738eSAdrian Chadd 		nbf = ath_tx_retry_clone(sc, tid->an, tid, bf);
2971f1bc738eSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
2972f1bc738eSAdrian Chadd 		    "%s: busy buffer clone: %p -> %p\n",
2973f1bc738eSAdrian Chadd 		    __func__, bf, nbf);
2974f1bc738eSAdrian Chadd 	} else {
2975f1bc738eSAdrian Chadd 		nbf = bf;
2976f1bc738eSAdrian Chadd 	}
2977f1bc738eSAdrian Chadd 
2978f1bc738eSAdrian Chadd 	if (nbf == NULL) {
2979f1bc738eSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
2980f1bc738eSAdrian Chadd 		    "%s: busy buffer couldn't be cloned (%p)!\n",
2981f1bc738eSAdrian Chadd 		    __func__, bf);
2982f1bc738eSAdrian Chadd 		retval = 1;
2983f1bc738eSAdrian Chadd 	} else {
2984f1bc738eSAdrian Chadd 		ath_tx_tid_filt_comp_buf(sc, tid, nbf);
2985f1bc738eSAdrian Chadd 		retval = 0;
2986f1bc738eSAdrian Chadd 	}
2987f1bc738eSAdrian Chadd 	ath_tx_tid_filt_comp_complete(sc, tid);
2988f1bc738eSAdrian Chadd 
2989f1bc738eSAdrian Chadd 	return (retval);
2990f1bc738eSAdrian Chadd }
2991f1bc738eSAdrian Chadd 
2992f1bc738eSAdrian Chadd static void
2993f1bc738eSAdrian Chadd ath_tx_tid_filt_comp_aggr(struct ath_softc *sc, struct ath_tid *tid,
2994f1bc738eSAdrian Chadd     struct ath_buf *bf_first, ath_bufhead *bf_q)
2995f1bc738eSAdrian Chadd {
2996f1bc738eSAdrian Chadd 	struct ath_buf *bf, *bf_next, *nbf;
2997f1bc738eSAdrian Chadd 
2998f1bc738eSAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
2999f1bc738eSAdrian Chadd 
3000f1bc738eSAdrian Chadd 	bf = bf_first;
3001f1bc738eSAdrian Chadd 	while (bf) {
3002f1bc738eSAdrian Chadd 		bf_next = bf->bf_next;
3003f1bc738eSAdrian Chadd 		bf->bf_next = NULL;	/* Remove it from the aggr list */
3004f1bc738eSAdrian Chadd 
3005f1bc738eSAdrian Chadd 		/*
3006f1bc738eSAdrian Chadd 		 * Don't allow a filtered frame to live forever.
3007f1bc738eSAdrian Chadd 		 */
3008f1bc738eSAdrian Chadd 		if (bf->bf_state.bfs_retries > SWMAX_RETRIES) {
3009f1bc738eSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3010f1bc738eSAdrian Chadd 			    "%s: bf=%p, seqno=%d, exceeded retries\n",
3011f1bc738eSAdrian Chadd 			    __func__,
3012f1bc738eSAdrian Chadd 			    bf,
3013f1bc738eSAdrian Chadd 			    bf->bf_state.bfs_seqno);
3014f1bc738eSAdrian Chadd 			TAILQ_INSERT_TAIL(bf_q, bf, bf_list);
3015f1bc738eSAdrian Chadd 			goto next;
3016f1bc738eSAdrian Chadd 		}
3017f1bc738eSAdrian Chadd 
3018f1bc738eSAdrian Chadd 		if (bf->bf_flags & ATH_BUF_BUSY) {
3019f1bc738eSAdrian Chadd 			nbf = ath_tx_retry_clone(sc, tid->an, tid, bf);
3020f1bc738eSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3021f1bc738eSAdrian Chadd 			    "%s: busy buffer cloned: %p -> %p",
3022f1bc738eSAdrian Chadd 			    __func__, bf, nbf);
3023f1bc738eSAdrian Chadd 		} else {
3024f1bc738eSAdrian Chadd 			nbf = bf;
3025f1bc738eSAdrian Chadd 		}
3026f1bc738eSAdrian Chadd 
3027f1bc738eSAdrian Chadd 		/*
3028f1bc738eSAdrian Chadd 		 * If the buffer couldn't be cloned, add it to bf_q;
3029f1bc738eSAdrian Chadd 		 * the caller will free the buffer(s) as required.
3030f1bc738eSAdrian Chadd 		 */
3031f1bc738eSAdrian Chadd 		if (nbf == NULL) {
3032f1bc738eSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3033f1bc738eSAdrian Chadd 			    "%s: buffer couldn't be cloned! (%p)\n",
3034f1bc738eSAdrian Chadd 			    __func__, bf);
3035f1bc738eSAdrian Chadd 			TAILQ_INSERT_TAIL(bf_q, bf, bf_list);
3036f1bc738eSAdrian Chadd 		} else {
3037f1bc738eSAdrian Chadd 			ath_tx_tid_filt_comp_buf(sc, tid, nbf);
3038f1bc738eSAdrian Chadd 		}
3039f1bc738eSAdrian Chadd next:
3040f1bc738eSAdrian Chadd 		bf = bf_next;
3041f1bc738eSAdrian Chadd 	}
3042f1bc738eSAdrian Chadd 
3043f1bc738eSAdrian Chadd 	ath_tx_tid_filt_comp_complete(sc, tid);
3044f1bc738eSAdrian Chadd }
3045f1bc738eSAdrian Chadd 
3046f1bc738eSAdrian Chadd /*
304788b3d483SAdrian Chadd  * Suspend the queue because we need to TX a BAR.
304888b3d483SAdrian Chadd  */
304988b3d483SAdrian Chadd static void
305088b3d483SAdrian Chadd ath_tx_tid_bar_suspend(struct ath_softc *sc, struct ath_tid *tid)
305188b3d483SAdrian Chadd {
305288b3d483SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
305388b3d483SAdrian Chadd 
30540e22ed0eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
3055e60c4fc2SAdrian Chadd 	    "%s: tid=%p, bar_wait=%d, bar_tx=%d, called\n",
305688b3d483SAdrian Chadd 	    __func__,
3057e60c4fc2SAdrian Chadd 	    tid,
3058e60c4fc2SAdrian Chadd 	    tid->bar_wait,
3059e60c4fc2SAdrian Chadd 	    tid->bar_tx);
306088b3d483SAdrian Chadd 
306188b3d483SAdrian Chadd 	/* We shouldn't be called when bar_tx is 1 */
306288b3d483SAdrian Chadd 	if (tid->bar_tx) {
306388b3d483SAdrian Chadd 		device_printf(sc->sc_dev, "%s: bar_tx is 1?!\n",
306488b3d483SAdrian Chadd 		    __func__);
306588b3d483SAdrian Chadd 	}
306688b3d483SAdrian Chadd 
306788b3d483SAdrian Chadd 	/* If we've already been called, just be patient. */
306888b3d483SAdrian Chadd 	if (tid->bar_wait)
306988b3d483SAdrian Chadd 		return;
307088b3d483SAdrian Chadd 
307188b3d483SAdrian Chadd 	/* Wait! */
307288b3d483SAdrian Chadd 	tid->bar_wait = 1;
307388b3d483SAdrian Chadd 
307488b3d483SAdrian Chadd 	/* Only one pause, no matter how many frames fail */
307588b3d483SAdrian Chadd 	ath_tx_tid_pause(sc, tid);
307688b3d483SAdrian Chadd }
307788b3d483SAdrian Chadd 
307888b3d483SAdrian Chadd /*
307988b3d483SAdrian Chadd  * We've finished with BAR handling - either we succeeded or
308088b3d483SAdrian Chadd  * failed. Either way, unsuspend TX.
308188b3d483SAdrian Chadd  */
308288b3d483SAdrian Chadd static void
308388b3d483SAdrian Chadd ath_tx_tid_bar_unsuspend(struct ath_softc *sc, struct ath_tid *tid)
308488b3d483SAdrian Chadd {
308588b3d483SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
308688b3d483SAdrian Chadd 
30870e22ed0eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
308888b3d483SAdrian Chadd 	    "%s: tid=%p, called\n",
308988b3d483SAdrian Chadd 	    __func__,
309088b3d483SAdrian Chadd 	    tid);
309188b3d483SAdrian Chadd 
309288b3d483SAdrian Chadd 	if (tid->bar_tx == 0 || tid->bar_wait == 0) {
309388b3d483SAdrian Chadd 		device_printf(sc->sc_dev, "%s: bar_tx=%d, bar_wait=%d: ?\n",
309488b3d483SAdrian Chadd 		    __func__, tid->bar_tx, tid->bar_wait);
309588b3d483SAdrian Chadd 	}
309688b3d483SAdrian Chadd 
309788b3d483SAdrian Chadd 	tid->bar_tx = tid->bar_wait = 0;
309888b3d483SAdrian Chadd 	ath_tx_tid_resume(sc, tid);
309988b3d483SAdrian Chadd }
310088b3d483SAdrian Chadd 
310188b3d483SAdrian Chadd /*
310288b3d483SAdrian Chadd  * Return whether we're ready to TX a BAR frame.
310388b3d483SAdrian Chadd  *
310488b3d483SAdrian Chadd  * Requires the TID lock be held.
310588b3d483SAdrian Chadd  */
310688b3d483SAdrian Chadd static int
310788b3d483SAdrian Chadd ath_tx_tid_bar_tx_ready(struct ath_softc *sc, struct ath_tid *tid)
310888b3d483SAdrian Chadd {
310988b3d483SAdrian Chadd 
311088b3d483SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
311188b3d483SAdrian Chadd 
311288b3d483SAdrian Chadd 	if (tid->bar_wait == 0 || tid->hwq_depth > 0)
311388b3d483SAdrian Chadd 		return (0);
311488b3d483SAdrian Chadd 
31150e22ed0eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR, "%s: tid=%p (%d), bar ready\n",
31160e22ed0eSAdrian Chadd 	    __func__, tid, tid->tid);
31170e22ed0eSAdrian Chadd 
311888b3d483SAdrian Chadd 	return (1);
311988b3d483SAdrian Chadd }
312088b3d483SAdrian Chadd 
312188b3d483SAdrian Chadd /*
312288b3d483SAdrian Chadd  * Check whether the current TID is ready to have a BAR
312388b3d483SAdrian Chadd  * TXed and if so, do the TX.
312488b3d483SAdrian Chadd  *
312588b3d483SAdrian Chadd  * Since the TID/TXQ lock can't be held during a call to
312688b3d483SAdrian Chadd  * ieee80211_send_bar(), we have to do the dirty thing of unlocking it,
312788b3d483SAdrian Chadd  * sending the BAR and locking it again.
312888b3d483SAdrian Chadd  *
312988b3d483SAdrian Chadd  * Eventually, the code to send the BAR should be broken out
313088b3d483SAdrian Chadd  * from this routine so the lock doesn't have to be reacquired
313188b3d483SAdrian Chadd  * just to be immediately dropped by the caller.
313288b3d483SAdrian Chadd  */
313388b3d483SAdrian Chadd static void
313488b3d483SAdrian Chadd ath_tx_tid_bar_tx(struct ath_softc *sc, struct ath_tid *tid)
313588b3d483SAdrian Chadd {
313688b3d483SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
313788b3d483SAdrian Chadd 
313888b3d483SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
313988b3d483SAdrian Chadd 
31400e22ed0eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
314188b3d483SAdrian Chadd 	    "%s: tid=%p, called\n",
314288b3d483SAdrian Chadd 	    __func__,
314388b3d483SAdrian Chadd 	    tid);
314488b3d483SAdrian Chadd 
314588b3d483SAdrian Chadd 	tap = ath_tx_get_tx_tid(tid->an, tid->tid);
314688b3d483SAdrian Chadd 
314788b3d483SAdrian Chadd 	/*
314888b3d483SAdrian Chadd 	 * This is an error condition!
314988b3d483SAdrian Chadd 	 */
315088b3d483SAdrian Chadd 	if (tid->bar_wait == 0 || tid->bar_tx == 1) {
315188b3d483SAdrian Chadd 		device_printf(sc->sc_dev,
315288b3d483SAdrian Chadd 		    "%s: tid=%p, bar_tx=%d, bar_wait=%d: ?\n",
315388b3d483SAdrian Chadd 		    __func__,
315488b3d483SAdrian Chadd 		    tid,
315588b3d483SAdrian Chadd 		    tid->bar_tx,
315688b3d483SAdrian Chadd 		    tid->bar_wait);
315788b3d483SAdrian Chadd 		return;
315888b3d483SAdrian Chadd 	}
315988b3d483SAdrian Chadd 
316088b3d483SAdrian Chadd 	/* Don't do anything if we still have pending frames */
316188b3d483SAdrian Chadd 	if (tid->hwq_depth > 0) {
31620e22ed0eSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
316388b3d483SAdrian Chadd 		    "%s: tid=%p, hwq_depth=%d, waiting\n",
316488b3d483SAdrian Chadd 		    __func__,
316588b3d483SAdrian Chadd 		    tid,
316688b3d483SAdrian Chadd 		    tid->hwq_depth);
316788b3d483SAdrian Chadd 		return;
316888b3d483SAdrian Chadd 	}
316988b3d483SAdrian Chadd 
317088b3d483SAdrian Chadd 	/* We're now about to TX */
317188b3d483SAdrian Chadd 	tid->bar_tx = 1;
317288b3d483SAdrian Chadd 
317388b3d483SAdrian Chadd 	/*
31744e81f27cSAdrian Chadd 	 * Override the clrdmask configuration for the next frame,
31754e81f27cSAdrian Chadd 	 * just to get the ball rolling.
31764e81f27cSAdrian Chadd 	 */
31774e81f27cSAdrian Chadd 	tid->clrdmask = 1;
31784e81f27cSAdrian Chadd 
31794e81f27cSAdrian Chadd 	/*
318088b3d483SAdrian Chadd 	 * Calculate new BAW left edge, now that all frames have either
318188b3d483SAdrian Chadd 	 * succeeded or failed.
318288b3d483SAdrian Chadd 	 *
318388b3d483SAdrian Chadd 	 * XXX verify this is _actually_ the valid value to begin at!
318488b3d483SAdrian Chadd 	 */
31850e22ed0eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
318688b3d483SAdrian Chadd 	    "%s: tid=%p, new BAW left edge=%d\n",
318788b3d483SAdrian Chadd 	    __func__,
318888b3d483SAdrian Chadd 	    tid,
318988b3d483SAdrian Chadd 	    tap->txa_start);
319088b3d483SAdrian Chadd 
319188b3d483SAdrian Chadd 	/* Try sending the BAR frame */
319288b3d483SAdrian Chadd 	/* We can't hold the lock here! */
319388b3d483SAdrian Chadd 
319488b3d483SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[tid->ac]);
319588b3d483SAdrian Chadd 	if (ieee80211_send_bar(&tid->an->an_node, tap, tap->txa_start) == 0) {
319688b3d483SAdrian Chadd 		/* Success? Now we wait for notification that it's done */
319788b3d483SAdrian Chadd 		ATH_TXQ_LOCK(sc->sc_ac2q[tid->ac]);
319888b3d483SAdrian Chadd 		return;
319988b3d483SAdrian Chadd 	}
320088b3d483SAdrian Chadd 
320188b3d483SAdrian Chadd 	/* Failure? For now, warn loudly and continue */
320288b3d483SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[tid->ac]);
320388b3d483SAdrian Chadd 	device_printf(sc->sc_dev, "%s: tid=%p, failed to TX BAR, continue!\n",
320488b3d483SAdrian Chadd 	    __func__, tid);
320588b3d483SAdrian Chadd 	ath_tx_tid_bar_unsuspend(sc, tid);
320688b3d483SAdrian Chadd }
320788b3d483SAdrian Chadd 
3208eb6f0de0SAdrian Chadd static void
3209f1bc738eSAdrian Chadd ath_tx_tid_drain_pkt(struct ath_softc *sc, struct ath_node *an,
3210f1bc738eSAdrian Chadd     struct ath_tid *tid, ath_bufhead *bf_cq, struct ath_buf *bf)
3211eb6f0de0SAdrian Chadd {
3212eb6f0de0SAdrian Chadd 
3213f1bc738eSAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
3214eb6f0de0SAdrian Chadd 
3215eb6f0de0SAdrian Chadd 	/*
3216eb6f0de0SAdrian Chadd 	 * If the current TID is running AMPDU, update
3217eb6f0de0SAdrian Chadd 	 * the BAW.
3218eb6f0de0SAdrian Chadd 	 */
3219eb6f0de0SAdrian Chadd 	if (ath_tx_ampdu_running(sc, an, tid->tid) &&
3220eb6f0de0SAdrian Chadd 	    bf->bf_state.bfs_dobaw) {
3221eb6f0de0SAdrian Chadd 		/*
3222eb6f0de0SAdrian Chadd 		 * Only remove the frame from the BAW if it's
3223eb6f0de0SAdrian Chadd 		 * been transmitted at least once; this means
3224eb6f0de0SAdrian Chadd 		 * the frame was in the BAW to begin with.
3225eb6f0de0SAdrian Chadd 		 */
3226eb6f0de0SAdrian Chadd 		if (bf->bf_state.bfs_retries > 0) {
3227eb6f0de0SAdrian Chadd 			ath_tx_update_baw(sc, an, tid, bf);
3228eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_dobaw = 0;
3229eb6f0de0SAdrian Chadd 		}
3230eb6f0de0SAdrian Chadd 		/*
3231eb6f0de0SAdrian Chadd 		 * This has become a non-fatal error now
3232eb6f0de0SAdrian Chadd 		 */
3233eb6f0de0SAdrian Chadd 		if (! bf->bf_state.bfs_addedbaw)
3234eb6f0de0SAdrian Chadd 			device_printf(sc->sc_dev,
3235eb6f0de0SAdrian Chadd 			    "%s: wasn't added: seqno %d\n",
3236eb6f0de0SAdrian Chadd 			    __func__, SEQNO(bf->bf_state.bfs_seqno));
3237eb6f0de0SAdrian Chadd 	}
3238eb6f0de0SAdrian Chadd 	TAILQ_INSERT_TAIL(bf_cq, bf, bf_list);
3239eb6f0de0SAdrian Chadd }
3240eb6f0de0SAdrian Chadd 
3241f1bc738eSAdrian Chadd static void
3242f1bc738eSAdrian Chadd ath_tx_tid_drain_print(struct ath_softc *sc, struct ath_node *an,
3243f1bc738eSAdrian Chadd     struct ath_tid *tid, struct ath_buf *bf)
3244f1bc738eSAdrian Chadd {
3245f1bc738eSAdrian Chadd 	struct ieee80211_node *ni = &an->an_node;
3246f1bc738eSAdrian Chadd 	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
3247f1bc738eSAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
3248f1bc738eSAdrian Chadd 
3249f1bc738eSAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
3250f1bc738eSAdrian Chadd 
3251f1bc738eSAdrian Chadd 	device_printf(sc->sc_dev,
3252f1bc738eSAdrian Chadd 	    "%s: node %p: bf=%p: addbaw=%d, dobaw=%d, "
3253f1bc738eSAdrian Chadd 	    "seqno=%d, retry=%d\n",
3254f1bc738eSAdrian Chadd 	    __func__, ni, bf,
3255f1bc738eSAdrian Chadd 	    bf->bf_state.bfs_addedbaw,
3256f1bc738eSAdrian Chadd 	    bf->bf_state.bfs_dobaw,
3257f1bc738eSAdrian Chadd 	    SEQNO(bf->bf_state.bfs_seqno),
3258f1bc738eSAdrian Chadd 	    bf->bf_state.bfs_retries);
3259f1bc738eSAdrian Chadd 	device_printf(sc->sc_dev,
32604e81f27cSAdrian Chadd 	    "%s: node %p: bf=%p: txq axq_depth=%d, axq_aggr_depth=%d\n",
32614e81f27cSAdrian Chadd 	        __func__, ni, bf,
32624e81f27cSAdrian Chadd 		txq->axq_depth,
32634e81f27cSAdrian Chadd 		txq->axq_aggr_depth);
32644e81f27cSAdrian Chadd 
32654e81f27cSAdrian Chadd 	device_printf(sc->sc_dev,
3266f1bc738eSAdrian Chadd 	    "%s: node %p: bf=%p: tid txq_depth=%d hwq_depth=%d, bar_wait=%d, isfiltered=%d\n",
3267f1bc738eSAdrian Chadd 	    __func__, ni, bf,
3268f1bc738eSAdrian Chadd 	    tid->axq_depth,
3269f1bc738eSAdrian Chadd 	    tid->hwq_depth,
3270f1bc738eSAdrian Chadd 	    tid->bar_wait,
3271f1bc738eSAdrian Chadd 	    tid->isfiltered);
3272f1bc738eSAdrian Chadd 	device_printf(sc->sc_dev,
32734e81f27cSAdrian Chadd 	    "%s: node %p: tid %d: "
32744e81f27cSAdrian Chadd 	    "sched=%d, paused=%d, "
32754e81f27cSAdrian Chadd 	    "incomp=%d, baw_head=%d, "
3276f1bc738eSAdrian Chadd 	    "baw_tail=%d txa_start=%d, ni_txseqs=%d\n",
32774e81f27cSAdrian Chadd 	     __func__, ni, tid->tid,
32784e81f27cSAdrian Chadd 	     tid->sched, tid->paused,
32794e81f27cSAdrian Chadd 	     tid->incomp, tid->baw_head,
3280f1bc738eSAdrian Chadd 	     tid->baw_tail, tap == NULL ? -1 : tap->txa_start,
3281f1bc738eSAdrian Chadd 	     ni->ni_txseqs[tid->tid]);
3282f1bc738eSAdrian Chadd 
3283f1bc738eSAdrian Chadd 	/* XXX Dump the frame, see what it is? */
3284f1bc738eSAdrian Chadd 	ieee80211_dump_pkt(ni->ni_ic,
3285f1bc738eSAdrian Chadd 	    mtod(bf->bf_m, const uint8_t *),
3286f1bc738eSAdrian Chadd 	    bf->bf_m->m_len, 0, -1);
3287f1bc738eSAdrian Chadd }
3288f1bc738eSAdrian Chadd 
3289f1bc738eSAdrian Chadd /*
3290f1bc738eSAdrian Chadd  * Free any packets currently pending in the software TX queue.
3291f1bc738eSAdrian Chadd  *
3292f1bc738eSAdrian Chadd  * This will be called when a node is being deleted.
3293f1bc738eSAdrian Chadd  *
3294f1bc738eSAdrian Chadd  * It can also be called on an active node during an interface
3295f1bc738eSAdrian Chadd  * reset or state transition.
3296f1bc738eSAdrian Chadd  *
3297f1bc738eSAdrian Chadd  * (From Linux/reference):
3298f1bc738eSAdrian Chadd  *
3299f1bc738eSAdrian Chadd  * TODO: For frame(s) that are in the retry state, we will reuse the
3300f1bc738eSAdrian Chadd  * sequence number(s) without setting the retry bit. The
3301f1bc738eSAdrian Chadd  * alternative is to give up on these and BAR the receiver's window
3302f1bc738eSAdrian Chadd  * forward.
3303f1bc738eSAdrian Chadd  */
3304f1bc738eSAdrian Chadd static void
3305f1bc738eSAdrian Chadd ath_tx_tid_drain(struct ath_softc *sc, struct ath_node *an,
3306f1bc738eSAdrian Chadd     struct ath_tid *tid, ath_bufhead *bf_cq)
3307f1bc738eSAdrian Chadd {
3308f1bc738eSAdrian Chadd 	struct ath_buf *bf;
3309f1bc738eSAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
3310f1bc738eSAdrian Chadd 	struct ieee80211_node *ni = &an->an_node;
3311f1bc738eSAdrian Chadd 	int t;
3312f1bc738eSAdrian Chadd 
3313f1bc738eSAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
3314f1bc738eSAdrian Chadd 
3315f1bc738eSAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
3316f1bc738eSAdrian Chadd 
3317f1bc738eSAdrian Chadd 	/* Walk the queue, free frames */
3318f1bc738eSAdrian Chadd 	t = 0;
3319f1bc738eSAdrian Chadd 	for (;;) {
3320f1bc738eSAdrian Chadd 		bf = TAILQ_FIRST(&tid->axq_q);
3321f1bc738eSAdrian Chadd 		if (bf == NULL) {
3322f1bc738eSAdrian Chadd 			break;
3323f1bc738eSAdrian Chadd 		}
3324f1bc738eSAdrian Chadd 
3325f1bc738eSAdrian Chadd 		if (t == 0) {
3326f1bc738eSAdrian Chadd 			ath_tx_tid_drain_print(sc, an, tid, bf);
3327f1bc738eSAdrian Chadd 			t = 1;
3328f1bc738eSAdrian Chadd 		}
3329f1bc738eSAdrian Chadd 
3330f1bc738eSAdrian Chadd 		ATH_TXQ_REMOVE(tid, bf, bf_list);
3331f1bc738eSAdrian Chadd 		ath_tx_tid_drain_pkt(sc, an, tid, bf_cq, bf);
3332f1bc738eSAdrian Chadd 	}
3333f1bc738eSAdrian Chadd 
3334f1bc738eSAdrian Chadd 	/* And now, drain the filtered frame queue */
3335f1bc738eSAdrian Chadd 	t = 0;
3336f1bc738eSAdrian Chadd 	for (;;) {
3337f1bc738eSAdrian Chadd 		bf = TAILQ_FIRST(&tid->filtq.axq_q);
3338f1bc738eSAdrian Chadd 		if (bf == NULL)
3339f1bc738eSAdrian Chadd 			break;
3340f1bc738eSAdrian Chadd 
3341f1bc738eSAdrian Chadd 		if (t == 0) {
3342f1bc738eSAdrian Chadd 			ath_tx_tid_drain_print(sc, an, tid, bf);
3343f1bc738eSAdrian Chadd 			t = 1;
3344f1bc738eSAdrian Chadd 		}
3345f1bc738eSAdrian Chadd 
3346f1bc738eSAdrian Chadd 		ATH_TXQ_REMOVE(&tid->filtq, bf, bf_list);
3347f1bc738eSAdrian Chadd 		ath_tx_tid_drain_pkt(sc, an, tid, bf_cq, bf);
3348f1bc738eSAdrian Chadd 	}
3349f1bc738eSAdrian Chadd 
3350eb6f0de0SAdrian Chadd 	/*
33514e81f27cSAdrian Chadd 	 * Override the clrdmask configuration for the next frame
33524e81f27cSAdrian Chadd 	 * in case there is some future transmission, just to get
33534e81f27cSAdrian Chadd 	 * the ball rolling.
33544e81f27cSAdrian Chadd 	 *
33554e81f27cSAdrian Chadd 	 * This won't hurt things if the TID is about to be freed.
33564e81f27cSAdrian Chadd 	 */
33574e81f27cSAdrian Chadd 	tid->clrdmask = 1;
33584e81f27cSAdrian Chadd 
33594e81f27cSAdrian Chadd 	/*
3360eb6f0de0SAdrian Chadd 	 * Now that it's completed, grab the TID lock and update
3361eb6f0de0SAdrian Chadd 	 * the sequence number and BAW window.
3362eb6f0de0SAdrian Chadd 	 * Because sequence numbers have been assigned to frames
3363eb6f0de0SAdrian Chadd 	 * that haven't been sent yet, it's entirely possible
3364eb6f0de0SAdrian Chadd 	 * we'll be called with some pending frames that have not
3365eb6f0de0SAdrian Chadd 	 * been transmitted.
3366eb6f0de0SAdrian Chadd 	 *
3367eb6f0de0SAdrian Chadd 	 * The cleaner solution is to do the sequence number allocation
3368eb6f0de0SAdrian Chadd 	 * when the packet is first transmitted - and thus the "retries"
3369eb6f0de0SAdrian Chadd 	 * check above would be enough to update the BAW/seqno.
3370eb6f0de0SAdrian Chadd 	 */
3371eb6f0de0SAdrian Chadd 
3372eb6f0de0SAdrian Chadd 	/* But don't do it for non-QoS TIDs */
3373eb6f0de0SAdrian Chadd 	if (tap) {
3374eb6f0de0SAdrian Chadd #if 0
3375eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
3376eb6f0de0SAdrian Chadd 		    "%s: node %p: TID %d: sliding BAW left edge to %d\n",
3377eb6f0de0SAdrian Chadd 		    __func__, an, tid->tid, tap->txa_start);
3378eb6f0de0SAdrian Chadd #endif
3379eb6f0de0SAdrian Chadd 		ni->ni_txseqs[tid->tid] = tap->txa_start;
3380eb6f0de0SAdrian Chadd 		tid->baw_tail = tid->baw_head;
3381eb6f0de0SAdrian Chadd 	}
3382eb6f0de0SAdrian Chadd }
3383eb6f0de0SAdrian Chadd 
3384eb6f0de0SAdrian Chadd /*
3385eb6f0de0SAdrian Chadd  * Flush all software queued packets for the given node.
3386eb6f0de0SAdrian Chadd  *
3387eb6f0de0SAdrian Chadd  * This occurs when a completion handler frees the last buffer
3388eb6f0de0SAdrian Chadd  * for a node, and the node is thus freed. This causes the node
3389eb6f0de0SAdrian Chadd  * to be cleaned up, which ends up calling ath_tx_node_flush.
3390eb6f0de0SAdrian Chadd  */
3391eb6f0de0SAdrian Chadd void
3392eb6f0de0SAdrian Chadd ath_tx_node_flush(struct ath_softc *sc, struct ath_node *an)
3393eb6f0de0SAdrian Chadd {
3394eb6f0de0SAdrian Chadd 	int tid;
3395eb6f0de0SAdrian Chadd 	ath_bufhead bf_cq;
3396eb6f0de0SAdrian Chadd 	struct ath_buf *bf;
3397eb6f0de0SAdrian Chadd 
3398eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_cq);
3399eb6f0de0SAdrian Chadd 
3400eb6f0de0SAdrian Chadd 	for (tid = 0; tid < IEEE80211_TID_SIZE; tid++) {
3401eb6f0de0SAdrian Chadd 		struct ath_tid *atid = &an->an_tid[tid];
3402eb6f0de0SAdrian Chadd 		struct ath_txq *txq = sc->sc_ac2q[atid->ac];
3403eb6f0de0SAdrian Chadd 
3404eb6f0de0SAdrian Chadd 		/* Remove this tid from the list of active tids */
3405eb6f0de0SAdrian Chadd 		ATH_TXQ_LOCK(txq);
3406eb6f0de0SAdrian Chadd 		ath_tx_tid_unsched(sc, atid);
3407eb6f0de0SAdrian Chadd 
3408eb6f0de0SAdrian Chadd 		/* Free packets */
3409eb6f0de0SAdrian Chadd 		ath_tx_tid_drain(sc, an, atid, &bf_cq);
3410eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
3411eb6f0de0SAdrian Chadd 	}
3412eb6f0de0SAdrian Chadd 
3413eb6f0de0SAdrian Chadd 	/* Handle completed frames */
3414eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
3415eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
3416eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
3417eb6f0de0SAdrian Chadd 	}
3418eb6f0de0SAdrian Chadd }
3419eb6f0de0SAdrian Chadd 
3420eb6f0de0SAdrian Chadd /*
3421eb6f0de0SAdrian Chadd  * Drain all the software TXQs currently with traffic queued.
3422eb6f0de0SAdrian Chadd  */
3423eb6f0de0SAdrian Chadd void
3424eb6f0de0SAdrian Chadd ath_tx_txq_drain(struct ath_softc *sc, struct ath_txq *txq)
3425eb6f0de0SAdrian Chadd {
3426eb6f0de0SAdrian Chadd 	struct ath_tid *tid;
3427eb6f0de0SAdrian Chadd 	ath_bufhead bf_cq;
3428eb6f0de0SAdrian Chadd 	struct ath_buf *bf;
3429eb6f0de0SAdrian Chadd 
3430eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_cq);
3431eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(txq);
3432eb6f0de0SAdrian Chadd 
3433eb6f0de0SAdrian Chadd 	/*
3434eb6f0de0SAdrian Chadd 	 * Iterate over all active tids for the given txq,
3435eb6f0de0SAdrian Chadd 	 * flushing and unsched'ing them
3436eb6f0de0SAdrian Chadd 	 */
3437eb6f0de0SAdrian Chadd 	while (! TAILQ_EMPTY(&txq->axq_tidq)) {
3438eb6f0de0SAdrian Chadd 		tid = TAILQ_FIRST(&txq->axq_tidq);
3439eb6f0de0SAdrian Chadd 		ath_tx_tid_drain(sc, tid->an, tid, &bf_cq);
3440eb6f0de0SAdrian Chadd 		ath_tx_tid_unsched(sc, tid);
3441eb6f0de0SAdrian Chadd 	}
3442eb6f0de0SAdrian Chadd 
3443eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(txq);
3444eb6f0de0SAdrian Chadd 
3445eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
3446eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
3447eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
3448eb6f0de0SAdrian Chadd 	}
3449eb6f0de0SAdrian Chadd }
3450eb6f0de0SAdrian Chadd 
3451eb6f0de0SAdrian Chadd /*
3452eb6f0de0SAdrian Chadd  * Handle completion of non-aggregate session frames.
3453*0c54de88SAdrian Chadd  *
3454*0c54de88SAdrian Chadd  * This (currently) doesn't implement software retransmission of
3455*0c54de88SAdrian Chadd  * non-aggregate frames!
3456*0c54de88SAdrian Chadd  *
3457*0c54de88SAdrian Chadd  * Software retransmission of non-aggregate frames needs to obey
3458*0c54de88SAdrian Chadd  * the strict sequence number ordering, and drop any frames that
3459*0c54de88SAdrian Chadd  * will fail this.
3460*0c54de88SAdrian Chadd  *
3461*0c54de88SAdrian Chadd  * For now, filtered frames and frame transmission will cause
3462*0c54de88SAdrian Chadd  * all kinds of issues.  So we don't support them.
3463*0c54de88SAdrian Chadd  *
3464*0c54de88SAdrian Chadd  * So anyone queuing frames via ath_tx_normal_xmit() or
3465*0c54de88SAdrian Chadd  * ath_tx_hw_queue_norm() must override and set CLRDMASK.
3466eb6f0de0SAdrian Chadd  */
3467eb6f0de0SAdrian Chadd void
3468eb6f0de0SAdrian Chadd ath_tx_normal_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
3469eb6f0de0SAdrian Chadd {
3470eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
3471eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
3472eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
3473eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
3474eb6f0de0SAdrian Chadd 	struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
3475eb6f0de0SAdrian Chadd 
3476eb6f0de0SAdrian Chadd 	/* The TID state is protected behind the TXQ lock */
3477eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
3478eb6f0de0SAdrian Chadd 
3479eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bf=%p: fail=%d, hwq_depth now %d\n",
3480eb6f0de0SAdrian Chadd 	    __func__, bf, fail, atid->hwq_depth - 1);
3481eb6f0de0SAdrian Chadd 
3482eb6f0de0SAdrian Chadd 	atid->hwq_depth--;
3483f1bc738eSAdrian Chadd 
3484*0c54de88SAdrian Chadd #if 0
3485*0c54de88SAdrian Chadd 	/*
3486*0c54de88SAdrian Chadd 	 * If the frame was filtered, stick it on the filter frame
3487*0c54de88SAdrian Chadd 	 * queue and complain about it.  It shouldn't happen!
3488*0c54de88SAdrian Chadd 	 */
3489*0c54de88SAdrian Chadd 	if ((ts->ts_status & HAL_TXERR_FILT) ||
3490*0c54de88SAdrian Chadd 	    (ts->ts_status != 0 && atid->isfiltered)) {
3491*0c54de88SAdrian Chadd 		device_printf(sc->sc_dev,
3492*0c54de88SAdrian Chadd 		    "%s: isfiltered=%d, ts_status=%d: huh?\n",
3493*0c54de88SAdrian Chadd 		    __func__,
3494*0c54de88SAdrian Chadd 		    atid->isfiltered,
3495*0c54de88SAdrian Chadd 		    ts->ts_status);
3496*0c54de88SAdrian Chadd 		ath_tx_tid_filt_comp_buf(sc, atid, bf);
3497*0c54de88SAdrian Chadd 	}
3498*0c54de88SAdrian Chadd #endif
3499f1bc738eSAdrian Chadd 	if (atid->isfiltered)
3500*0c54de88SAdrian Chadd 		device_printf(sc->sc_dev, "%s: filtered?!\n", __func__);
3501eb6f0de0SAdrian Chadd 	if (atid->hwq_depth < 0)
3502eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: hwq_depth < 0: %d\n",
3503eb6f0de0SAdrian Chadd 		    __func__, atid->hwq_depth);
3504f1bc738eSAdrian Chadd 
3505f1bc738eSAdrian Chadd 	/*
3506f1bc738eSAdrian Chadd 	 * If the queue is filtered, potentially mark it as complete
3507f1bc738eSAdrian Chadd 	 * and reschedule it as needed.
3508f1bc738eSAdrian Chadd 	 *
3509f1bc738eSAdrian Chadd 	 * This is required as there may be a subsequent TX descriptor
3510f1bc738eSAdrian Chadd 	 * for this end-node that has CLRDMASK set, so it's quite possible
3511f1bc738eSAdrian Chadd 	 * that a filtered frame will be followed by a non-filtered
3512f1bc738eSAdrian Chadd 	 * (complete or otherwise) frame.
3513f1bc738eSAdrian Chadd 	 *
3514f1bc738eSAdrian Chadd 	 * XXX should we do this before we complete the frame?
3515f1bc738eSAdrian Chadd 	 */
3516f1bc738eSAdrian Chadd 	if (atid->isfiltered)
3517f1bc738eSAdrian Chadd 		ath_tx_tid_filt_comp_complete(sc, atid);
3518eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3519eb6f0de0SAdrian Chadd 
3520eb6f0de0SAdrian Chadd 	/*
3521eb6f0de0SAdrian Chadd 	 * punt to rate control if we're not being cleaned up
3522eb6f0de0SAdrian Chadd 	 * during a hw queue drain and the frame wanted an ACK.
3523eb6f0de0SAdrian Chadd 	 */
3524875a9451SAdrian Chadd 	if (fail == 0 && ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0))
3525eb6f0de0SAdrian Chadd 		ath_tx_update_ratectrl(sc, ni, bf->bf_state.bfs_rc,
3526eb6f0de0SAdrian Chadd 		    ts, bf->bf_state.bfs_pktlen,
3527eb6f0de0SAdrian Chadd 		    1, (ts->ts_status == 0) ? 0 : 1);
3528eb6f0de0SAdrian Chadd 
3529eb6f0de0SAdrian Chadd 	ath_tx_default_comp(sc, bf, fail);
3530eb6f0de0SAdrian Chadd }
3531eb6f0de0SAdrian Chadd 
3532eb6f0de0SAdrian Chadd /*
3533eb6f0de0SAdrian Chadd  * Handle cleanup of aggregate session packets that aren't
3534eb6f0de0SAdrian Chadd  * an A-MPDU.
3535eb6f0de0SAdrian Chadd  *
3536eb6f0de0SAdrian Chadd  * There's no need to update the BAW here - the session is being
3537eb6f0de0SAdrian Chadd  * torn down.
3538eb6f0de0SAdrian Chadd  */
3539eb6f0de0SAdrian Chadd static void
3540eb6f0de0SAdrian Chadd ath_tx_comp_cleanup_unaggr(struct ath_softc *sc, struct ath_buf *bf)
3541eb6f0de0SAdrian Chadd {
3542eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
3543eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
3544eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
3545eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
3546eb6f0de0SAdrian Chadd 
3547eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: TID %d: incomp=%d\n",
3548eb6f0de0SAdrian Chadd 	    __func__, tid, atid->incomp);
3549eb6f0de0SAdrian Chadd 
3550eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
3551eb6f0de0SAdrian Chadd 	atid->incomp--;
3552eb6f0de0SAdrian Chadd 	if (atid->incomp == 0) {
3553eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
3554eb6f0de0SAdrian Chadd 		    "%s: TID %d: cleaned up! resume!\n",
3555eb6f0de0SAdrian Chadd 		    __func__, tid);
3556eb6f0de0SAdrian Chadd 		atid->cleanup_inprogress = 0;
3557eb6f0de0SAdrian Chadd 		ath_tx_tid_resume(sc, atid);
3558eb6f0de0SAdrian Chadd 	}
3559eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3560eb6f0de0SAdrian Chadd 
3561eb6f0de0SAdrian Chadd 	ath_tx_default_comp(sc, bf, 0);
3562eb6f0de0SAdrian Chadd }
3563eb6f0de0SAdrian Chadd 
3564eb6f0de0SAdrian Chadd /*
3565eb6f0de0SAdrian Chadd  * Performs transmit side cleanup when TID changes from aggregated to
3566eb6f0de0SAdrian Chadd  * unaggregated.
3567eb6f0de0SAdrian Chadd  *
3568eb6f0de0SAdrian Chadd  * - Discard all retry frames from the s/w queue.
3569eb6f0de0SAdrian Chadd  * - Fix the tx completion function for all buffers in s/w queue.
3570eb6f0de0SAdrian Chadd  * - Count the number of unacked frames, and let transmit completion
3571eb6f0de0SAdrian Chadd  *   handle it later.
3572eb6f0de0SAdrian Chadd  *
3573eb6f0de0SAdrian Chadd  * The caller is responsible for pausing the TID.
3574eb6f0de0SAdrian Chadd  */
3575eb6f0de0SAdrian Chadd static void
35764dfd4507SAdrian Chadd ath_tx_tid_cleanup(struct ath_softc *sc, struct ath_node *an, int tid)
3577eb6f0de0SAdrian Chadd {
3578eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
3579eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
3580eb6f0de0SAdrian Chadd 	struct ath_buf *bf, *bf_next;
3581eb6f0de0SAdrian Chadd 	ath_bufhead bf_cq;
3582eb6f0de0SAdrian Chadd 
3583d3a6425bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
3584eb6f0de0SAdrian Chadd 	    "%s: TID %d: called\n", __func__, tid);
3585eb6f0de0SAdrian Chadd 
3586eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_cq);
3587eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
3588eb6f0de0SAdrian Chadd 
3589eb6f0de0SAdrian Chadd 	/*
3590f1bc738eSAdrian Chadd 	 * Move the filtered frames to the TX queue, before
3591f1bc738eSAdrian Chadd 	 * we run off and discard/process things.
3592f1bc738eSAdrian Chadd 	 */
3593f1bc738eSAdrian Chadd 	/* XXX this is really quite inefficient */
3594f1bc738eSAdrian Chadd 	while ((bf = TAILQ_LAST(&atid->filtq.axq_q, ath_bufhead_s)) != NULL) {
3595f1bc738eSAdrian Chadd 		ATH_TXQ_REMOVE(&atid->filtq, bf, bf_list);
3596f1bc738eSAdrian Chadd 		ATH_TXQ_INSERT_HEAD(atid, bf, bf_list);
3597f1bc738eSAdrian Chadd 	}
3598f1bc738eSAdrian Chadd 
3599f1bc738eSAdrian Chadd 	/*
3600eb6f0de0SAdrian Chadd 	 * Update the frames in the software TX queue:
3601eb6f0de0SAdrian Chadd 	 *
3602eb6f0de0SAdrian Chadd 	 * + Discard retry frames in the queue
3603eb6f0de0SAdrian Chadd 	 * + Fix the completion function to be non-aggregate
3604eb6f0de0SAdrian Chadd 	 */
3605eb6f0de0SAdrian Chadd 	bf = TAILQ_FIRST(&atid->axq_q);
3606eb6f0de0SAdrian Chadd 	while (bf) {
3607eb6f0de0SAdrian Chadd 		if (bf->bf_state.bfs_isretried) {
3608eb6f0de0SAdrian Chadd 			bf_next = TAILQ_NEXT(bf, bf_list);
3609eb6f0de0SAdrian Chadd 			TAILQ_REMOVE(&atid->axq_q, bf, bf_list);
3610eb6f0de0SAdrian Chadd 			atid->axq_depth--;
3611eb6f0de0SAdrian Chadd 			if (bf->bf_state.bfs_dobaw) {
3612eb6f0de0SAdrian Chadd 				ath_tx_update_baw(sc, an, atid, bf);
3613eb6f0de0SAdrian Chadd 				if (! bf->bf_state.bfs_addedbaw)
3614eb6f0de0SAdrian Chadd 					device_printf(sc->sc_dev,
3615eb6f0de0SAdrian Chadd 					    "%s: wasn't added: seqno %d\n",
3616d4365d16SAdrian Chadd 					    __func__,
3617d4365d16SAdrian Chadd 					    SEQNO(bf->bf_state.bfs_seqno));
3618eb6f0de0SAdrian Chadd 			}
3619eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_dobaw = 0;
3620eb6f0de0SAdrian Chadd 			/*
3621eb6f0de0SAdrian Chadd 			 * Call the default completion handler with "fail" just
3622eb6f0de0SAdrian Chadd 			 * so upper levels are suitably notified about this.
3623eb6f0de0SAdrian Chadd 			 */
3624eb6f0de0SAdrian Chadd 			TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
3625eb6f0de0SAdrian Chadd 			bf = bf_next;
3626eb6f0de0SAdrian Chadd 			continue;
3627eb6f0de0SAdrian Chadd 		}
3628eb6f0de0SAdrian Chadd 		/* Give these the default completion handler */
3629eb6f0de0SAdrian Chadd 		bf->bf_comp = ath_tx_normal_comp;
3630eb6f0de0SAdrian Chadd 		bf = TAILQ_NEXT(bf, bf_list);
3631eb6f0de0SAdrian Chadd 	}
3632eb6f0de0SAdrian Chadd 
3633eb6f0de0SAdrian Chadd 	/* The caller is required to pause the TID */
3634eb6f0de0SAdrian Chadd #if 0
3635eb6f0de0SAdrian Chadd 	/* Pause the TID */
3636eb6f0de0SAdrian Chadd 	ath_tx_tid_pause(sc, atid);
3637eb6f0de0SAdrian Chadd #endif
3638eb6f0de0SAdrian Chadd 
3639eb6f0de0SAdrian Chadd 	/*
3640eb6f0de0SAdrian Chadd 	 * Calculate what hardware-queued frames exist based
3641eb6f0de0SAdrian Chadd 	 * on the current BAW size. Ie, what frames have been
3642eb6f0de0SAdrian Chadd 	 * added to the TX hardware queue for this TID but
3643eb6f0de0SAdrian Chadd 	 * not yet ACKed.
3644eb6f0de0SAdrian Chadd 	 */
3645eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
3646eb6f0de0SAdrian Chadd 	/* Need the lock - fiddling with BAW */
3647eb6f0de0SAdrian Chadd 	while (atid->baw_head != atid->baw_tail) {
3648eb6f0de0SAdrian Chadd 		if (atid->tx_buf[atid->baw_head]) {
3649eb6f0de0SAdrian Chadd 			atid->incomp++;
3650eb6f0de0SAdrian Chadd 			atid->cleanup_inprogress = 1;
3651eb6f0de0SAdrian Chadd 			atid->tx_buf[atid->baw_head] = NULL;
3652eb6f0de0SAdrian Chadd 		}
3653eb6f0de0SAdrian Chadd 		INCR(atid->baw_head, ATH_TID_MAX_BUFS);
3654eb6f0de0SAdrian Chadd 		INCR(tap->txa_start, IEEE80211_SEQ_RANGE);
3655eb6f0de0SAdrian Chadd 	}
3656eb6f0de0SAdrian Chadd 
3657eb6f0de0SAdrian Chadd 	/*
3658eb6f0de0SAdrian Chadd 	 * If cleanup is required, defer TID scheduling
3659eb6f0de0SAdrian Chadd 	 * until all the HW queued packets have been
3660eb6f0de0SAdrian Chadd 	 * sent.
3661eb6f0de0SAdrian Chadd 	 */
3662eb6f0de0SAdrian Chadd 	if (! atid->cleanup_inprogress)
3663eb6f0de0SAdrian Chadd 		ath_tx_tid_resume(sc, atid);
3664eb6f0de0SAdrian Chadd 
3665eb6f0de0SAdrian Chadd 	if (atid->cleanup_inprogress)
3666eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
3667eb6f0de0SAdrian Chadd 		    "%s: TID %d: cleanup needed: %d packets\n",
3668eb6f0de0SAdrian Chadd 		    __func__, tid, atid->incomp);
3669eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3670eb6f0de0SAdrian Chadd 
3671eb6f0de0SAdrian Chadd 	/* Handle completing frames and fail them */
3672eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
3673eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
3674eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 1);
3675eb6f0de0SAdrian Chadd 	}
3676eb6f0de0SAdrian Chadd }
3677eb6f0de0SAdrian Chadd 
3678eb6f0de0SAdrian Chadd static struct ath_buf *
367938962489SAdrian Chadd ath_tx_retry_clone(struct ath_softc *sc, struct ath_node *an,
368038962489SAdrian Chadd     struct ath_tid *tid, struct ath_buf *bf)
3681eb6f0de0SAdrian Chadd {
3682eb6f0de0SAdrian Chadd 	struct ath_buf *nbf;
3683eb6f0de0SAdrian Chadd 	int error;
3684eb6f0de0SAdrian Chadd 
3685eb6f0de0SAdrian Chadd 	nbf = ath_buf_clone(sc, bf);
3686eb6f0de0SAdrian Chadd 
3687eb6f0de0SAdrian Chadd #if 0
3688eb6f0de0SAdrian Chadd 	device_printf(sc->sc_dev, "%s: ATH_BUF_BUSY; cloning\n",
3689eb6f0de0SAdrian Chadd 	    __func__);
3690eb6f0de0SAdrian Chadd #endif
3691eb6f0de0SAdrian Chadd 
3692eb6f0de0SAdrian Chadd 	if (nbf == NULL) {
3693eb6f0de0SAdrian Chadd 		/* Failed to clone */
3694eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
3695eb6f0de0SAdrian Chadd 		    "%s: failed to clone a busy buffer\n",
3696eb6f0de0SAdrian Chadd 		    __func__);
3697eb6f0de0SAdrian Chadd 		return NULL;
3698eb6f0de0SAdrian Chadd 	}
3699eb6f0de0SAdrian Chadd 
3700eb6f0de0SAdrian Chadd 	/* Setup the dma for the new buffer */
3701eb6f0de0SAdrian Chadd 	error = ath_tx_dmasetup(sc, nbf, nbf->bf_m);
3702eb6f0de0SAdrian Chadd 	if (error != 0) {
3703eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
3704eb6f0de0SAdrian Chadd 		    "%s: failed to setup dma for clone\n",
3705eb6f0de0SAdrian Chadd 		    __func__);
3706eb6f0de0SAdrian Chadd 		/*
3707eb6f0de0SAdrian Chadd 		 * Put this at the head of the list, not tail;
3708eb6f0de0SAdrian Chadd 		 * that way it doesn't interfere with the
3709eb6f0de0SAdrian Chadd 		 * busy buffer logic (which uses the tail of
3710eb6f0de0SAdrian Chadd 		 * the list.)
3711eb6f0de0SAdrian Chadd 		 */
3712eb6f0de0SAdrian Chadd 		ATH_TXBUF_LOCK(sc);
371332c387f7SAdrian Chadd 		ath_returnbuf_head(sc, nbf);
3714eb6f0de0SAdrian Chadd 		ATH_TXBUF_UNLOCK(sc);
3715eb6f0de0SAdrian Chadd 		return NULL;
3716eb6f0de0SAdrian Chadd 	}
3717eb6f0de0SAdrian Chadd 
371838962489SAdrian Chadd 	/* Update BAW if required, before we free the original buf */
371938962489SAdrian Chadd 	if (bf->bf_state.bfs_dobaw)
372038962489SAdrian Chadd 		ath_tx_switch_baw_buf(sc, an, tid, bf, nbf);
372138962489SAdrian Chadd 
3722eb6f0de0SAdrian Chadd 	/* Free current buffer; return the older buffer */
3723eb6f0de0SAdrian Chadd 	bf->bf_m = NULL;
3724eb6f0de0SAdrian Chadd 	bf->bf_node = NULL;
3725eb6f0de0SAdrian Chadd 	ath_freebuf(sc, bf);
3726f1bc738eSAdrian Chadd 
3727eb6f0de0SAdrian Chadd 	return nbf;
3728eb6f0de0SAdrian Chadd }
3729eb6f0de0SAdrian Chadd 
3730eb6f0de0SAdrian Chadd /*
3731eb6f0de0SAdrian Chadd  * Handle retrying an unaggregate frame in an aggregate
3732eb6f0de0SAdrian Chadd  * session.
3733eb6f0de0SAdrian Chadd  *
3734eb6f0de0SAdrian Chadd  * If too many retries occur, pause the TID, wait for
3735eb6f0de0SAdrian Chadd  * any further retransmits (as there's no reason why
3736eb6f0de0SAdrian Chadd  * non-aggregate frames in an aggregate session are
3737eb6f0de0SAdrian Chadd  * transmitted in-order; they just have to be in-BAW)
3738eb6f0de0SAdrian Chadd  * and then queue a BAR.
3739eb6f0de0SAdrian Chadd  */
3740eb6f0de0SAdrian Chadd static void
3741eb6f0de0SAdrian Chadd ath_tx_aggr_retry_unaggr(struct ath_softc *sc, struct ath_buf *bf)
3742eb6f0de0SAdrian Chadd {
3743eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
3744eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
3745eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
3746eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
3747eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
3748eb6f0de0SAdrian Chadd 
3749eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
3750eb6f0de0SAdrian Chadd 
3751eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
3752eb6f0de0SAdrian Chadd 
3753eb6f0de0SAdrian Chadd 	/*
3754eb6f0de0SAdrian Chadd 	 * If the buffer is marked as busy, we can't directly
3755eb6f0de0SAdrian Chadd 	 * reuse it. Instead, try to clone the buffer.
3756eb6f0de0SAdrian Chadd 	 * If the clone is successful, recycle the old buffer.
3757eb6f0de0SAdrian Chadd 	 * If the clone is unsuccessful, set bfs_retries to max
3758eb6f0de0SAdrian Chadd 	 * to force the next bit of code to free the buffer
3759eb6f0de0SAdrian Chadd 	 * for us.
3760eb6f0de0SAdrian Chadd 	 */
3761eb6f0de0SAdrian Chadd 	if ((bf->bf_state.bfs_retries < SWMAX_RETRIES) &&
3762eb6f0de0SAdrian Chadd 	    (bf->bf_flags & ATH_BUF_BUSY)) {
3763eb6f0de0SAdrian Chadd 		struct ath_buf *nbf;
376438962489SAdrian Chadd 		nbf = ath_tx_retry_clone(sc, an, atid, bf);
3765eb6f0de0SAdrian Chadd 		if (nbf)
3766eb6f0de0SAdrian Chadd 			/* bf has been freed at this point */
3767eb6f0de0SAdrian Chadd 			bf = nbf;
3768eb6f0de0SAdrian Chadd 		else
3769eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_retries = SWMAX_RETRIES + 1;
3770eb6f0de0SAdrian Chadd 	}
3771eb6f0de0SAdrian Chadd 
3772eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_retries >= SWMAX_RETRIES) {
3773eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_RETRIES,
3774eb6f0de0SAdrian Chadd 		    "%s: exceeded retries; seqno %d\n",
3775eb6f0de0SAdrian Chadd 		    __func__, SEQNO(bf->bf_state.bfs_seqno));
3776eb6f0de0SAdrian Chadd 		sc->sc_stats.ast_tx_swretrymax++;
3777eb6f0de0SAdrian Chadd 
3778eb6f0de0SAdrian Chadd 		/* Update BAW anyway */
3779eb6f0de0SAdrian Chadd 		if (bf->bf_state.bfs_dobaw) {
3780eb6f0de0SAdrian Chadd 			ath_tx_update_baw(sc, an, atid, bf);
3781eb6f0de0SAdrian Chadd 			if (! bf->bf_state.bfs_addedbaw)
3782eb6f0de0SAdrian Chadd 				device_printf(sc->sc_dev,
3783eb6f0de0SAdrian Chadd 				    "%s: wasn't added: seqno %d\n",
3784eb6f0de0SAdrian Chadd 				    __func__, SEQNO(bf->bf_state.bfs_seqno));
3785eb6f0de0SAdrian Chadd 		}
3786eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_dobaw = 0;
3787eb6f0de0SAdrian Chadd 
378888b3d483SAdrian Chadd 		/* Suspend the TX queue and get ready to send the BAR */
378988b3d483SAdrian Chadd 		ath_tx_tid_bar_suspend(sc, atid);
379088b3d483SAdrian Chadd 
379188b3d483SAdrian Chadd 		/* Send the BAR if there are no other frames waiting */
379288b3d483SAdrian Chadd 		if (ath_tx_tid_bar_tx_ready(sc, atid))
379388b3d483SAdrian Chadd 			ath_tx_tid_bar_tx(sc, atid);
379488b3d483SAdrian Chadd 
3795eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3796eb6f0de0SAdrian Chadd 
3797eb6f0de0SAdrian Chadd 		/* Free buffer, bf is free after this call */
3798eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
3799eb6f0de0SAdrian Chadd 		return;
3800eb6f0de0SAdrian Chadd 	}
3801eb6f0de0SAdrian Chadd 
3802eb6f0de0SAdrian Chadd 	/*
3803eb6f0de0SAdrian Chadd 	 * This increments the retry counter as well as
3804eb6f0de0SAdrian Chadd 	 * sets the retry flag in the ath_buf and packet
3805eb6f0de0SAdrian Chadd 	 * body.
3806eb6f0de0SAdrian Chadd 	 */
3807eb6f0de0SAdrian Chadd 	ath_tx_set_retry(sc, bf);
3808f1bc738eSAdrian Chadd 	sc->sc_stats.ast_tx_swretries++;
3809eb6f0de0SAdrian Chadd 
3810eb6f0de0SAdrian Chadd 	/*
3811eb6f0de0SAdrian Chadd 	 * Insert this at the head of the queue, so it's
3812eb6f0de0SAdrian Chadd 	 * retried before any current/subsequent frames.
3813eb6f0de0SAdrian Chadd 	 */
3814eb6f0de0SAdrian Chadd 	ATH_TXQ_INSERT_HEAD(atid, bf, bf_list);
3815eb6f0de0SAdrian Chadd 	ath_tx_tid_sched(sc, atid);
381688b3d483SAdrian Chadd 	/* Send the BAR if there are no other frames waiting */
381788b3d483SAdrian Chadd 	if (ath_tx_tid_bar_tx_ready(sc, atid))
381888b3d483SAdrian Chadd 		ath_tx_tid_bar_tx(sc, atid);
3819eb6f0de0SAdrian Chadd 
3820eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3821eb6f0de0SAdrian Chadd }
3822eb6f0de0SAdrian Chadd 
3823eb6f0de0SAdrian Chadd /*
3824eb6f0de0SAdrian Chadd  * Common code for aggregate excessive retry/subframe retry.
3825eb6f0de0SAdrian Chadd  * If retrying, queues buffers to bf_q. If not, frees the
3826eb6f0de0SAdrian Chadd  * buffers.
3827eb6f0de0SAdrian Chadd  *
3828eb6f0de0SAdrian Chadd  * XXX should unify this with ath_tx_aggr_retry_unaggr()
3829eb6f0de0SAdrian Chadd  */
3830eb6f0de0SAdrian Chadd static int
3831eb6f0de0SAdrian Chadd ath_tx_retry_subframe(struct ath_softc *sc, struct ath_buf *bf,
3832eb6f0de0SAdrian Chadd     ath_bufhead *bf_q)
3833eb6f0de0SAdrian Chadd {
3834eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
3835eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
3836eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
3837eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
3838eb6f0de0SAdrian Chadd 
3839eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[atid->ac]);
3840eb6f0de0SAdrian Chadd 
384121840808SAdrian Chadd 	/* XXX clr11naggr should be done for all subframes */
3842eb6f0de0SAdrian Chadd 	ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
3843eb6f0de0SAdrian Chadd 	ath_hal_set11nburstduration(sc->sc_ah, bf->bf_desc, 0);
3844f1bc738eSAdrian Chadd 
3845eb6f0de0SAdrian Chadd 	/* ath_hal_set11n_virtualmorefrag(sc->sc_ah, bf->bf_desc, 0); */
3846eb6f0de0SAdrian Chadd 
3847eb6f0de0SAdrian Chadd 	/*
3848eb6f0de0SAdrian Chadd 	 * If the buffer is marked as busy, we can't directly
3849eb6f0de0SAdrian Chadd 	 * reuse it. Instead, try to clone the buffer.
3850eb6f0de0SAdrian Chadd 	 * If the clone is successful, recycle the old buffer.
3851eb6f0de0SAdrian Chadd 	 * If the clone is unsuccessful, set bfs_retries to max
3852eb6f0de0SAdrian Chadd 	 * to force the next bit of code to free the buffer
3853eb6f0de0SAdrian Chadd 	 * for us.
3854eb6f0de0SAdrian Chadd 	 */
3855eb6f0de0SAdrian Chadd 	if ((bf->bf_state.bfs_retries < SWMAX_RETRIES) &&
3856eb6f0de0SAdrian Chadd 	    (bf->bf_flags & ATH_BUF_BUSY)) {
3857eb6f0de0SAdrian Chadd 		struct ath_buf *nbf;
385838962489SAdrian Chadd 		nbf = ath_tx_retry_clone(sc, an, atid, bf);
3859eb6f0de0SAdrian Chadd 		if (nbf)
3860eb6f0de0SAdrian Chadd 			/* bf has been freed at this point */
3861eb6f0de0SAdrian Chadd 			bf = nbf;
3862eb6f0de0SAdrian Chadd 		else
3863eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_retries = SWMAX_RETRIES + 1;
3864eb6f0de0SAdrian Chadd 	}
3865eb6f0de0SAdrian Chadd 
3866eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_retries >= SWMAX_RETRIES) {
3867eb6f0de0SAdrian Chadd 		sc->sc_stats.ast_tx_swretrymax++;
3868eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_RETRIES,
3869eb6f0de0SAdrian Chadd 		    "%s: max retries: seqno %d\n",
3870eb6f0de0SAdrian Chadd 		    __func__, SEQNO(bf->bf_state.bfs_seqno));
3871eb6f0de0SAdrian Chadd 		ath_tx_update_baw(sc, an, atid, bf);
3872eb6f0de0SAdrian Chadd 		if (! bf->bf_state.bfs_addedbaw)
3873eb6f0de0SAdrian Chadd 			device_printf(sc->sc_dev,
3874eb6f0de0SAdrian Chadd 			    "%s: wasn't added: seqno %d\n",
3875eb6f0de0SAdrian Chadd 			    __func__, SEQNO(bf->bf_state.bfs_seqno));
3876eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_dobaw = 0;
3877eb6f0de0SAdrian Chadd 		return 1;
3878eb6f0de0SAdrian Chadd 	}
3879eb6f0de0SAdrian Chadd 
3880eb6f0de0SAdrian Chadd 	ath_tx_set_retry(sc, bf);
3881f1bc738eSAdrian Chadd 	sc->sc_stats.ast_tx_swretries++;
3882eb6f0de0SAdrian Chadd 	bf->bf_next = NULL;		/* Just to make sure */
3883eb6f0de0SAdrian Chadd 
388421840808SAdrian Chadd 	/* Clear the aggregate state */
388521840808SAdrian Chadd 	bf->bf_state.bfs_aggr = 0;
388621840808SAdrian Chadd 	bf->bf_state.bfs_ndelim = 0;	/* ??? needed? */
388721840808SAdrian Chadd 	bf->bf_state.bfs_nframes = 1;
388821840808SAdrian Chadd 
3889eb6f0de0SAdrian Chadd 	TAILQ_INSERT_TAIL(bf_q, bf, bf_list);
3890eb6f0de0SAdrian Chadd 	return 0;
3891eb6f0de0SAdrian Chadd }
3892eb6f0de0SAdrian Chadd 
3893eb6f0de0SAdrian Chadd /*
3894eb6f0de0SAdrian Chadd  * error pkt completion for an aggregate destination
3895eb6f0de0SAdrian Chadd  */
3896eb6f0de0SAdrian Chadd static void
3897eb6f0de0SAdrian Chadd ath_tx_comp_aggr_error(struct ath_softc *sc, struct ath_buf *bf_first,
3898eb6f0de0SAdrian Chadd     struct ath_tid *tid)
3899eb6f0de0SAdrian Chadd {
3900eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf_first->bf_node;
3901eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
3902eb6f0de0SAdrian Chadd 	struct ath_buf *bf_next, *bf;
3903eb6f0de0SAdrian Chadd 	ath_bufhead bf_q;
3904eb6f0de0SAdrian Chadd 	int drops = 0;
3905eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
3906eb6f0de0SAdrian Chadd 	ath_bufhead bf_cq;
3907eb6f0de0SAdrian Chadd 
3908eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_q);
3909eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_cq);
3910eb6f0de0SAdrian Chadd 
3911eb6f0de0SAdrian Chadd 	/*
3912eb6f0de0SAdrian Chadd 	 * Update rate control - all frames have failed.
3913eb6f0de0SAdrian Chadd 	 *
3914eb6f0de0SAdrian Chadd 	 * XXX use the length in the first frame in the series;
3915eb6f0de0SAdrian Chadd 	 * XXX just so things are consistent for now.
3916eb6f0de0SAdrian Chadd 	 */
3917eb6f0de0SAdrian Chadd 	ath_tx_update_ratectrl(sc, ni, bf_first->bf_state.bfs_rc,
3918eb6f0de0SAdrian Chadd 	    &bf_first->bf_status.ds_txstat,
3919eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_pktlen,
3920eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_nframes, bf_first->bf_state.bfs_nframes);
3921eb6f0de0SAdrian Chadd 
3922eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[tid->ac]);
3923eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
39242d3d4776SAdrian Chadd 	sc->sc_stats.ast_tx_aggr_failall++;
3925eb6f0de0SAdrian Chadd 
3926eb6f0de0SAdrian Chadd 	/* Retry all subframes */
3927eb6f0de0SAdrian Chadd 	bf = bf_first;
3928eb6f0de0SAdrian Chadd 	while (bf) {
3929eb6f0de0SAdrian Chadd 		bf_next = bf->bf_next;
3930eb6f0de0SAdrian Chadd 		bf->bf_next = NULL;	/* Remove it from the aggr list */
39312d3d4776SAdrian Chadd 		sc->sc_stats.ast_tx_aggr_fail++;
3932eb6f0de0SAdrian Chadd 		if (ath_tx_retry_subframe(sc, bf, &bf_q)) {
3933eb6f0de0SAdrian Chadd 			drops++;
3934eb6f0de0SAdrian Chadd 			bf->bf_next = NULL;
3935eb6f0de0SAdrian Chadd 			TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
3936eb6f0de0SAdrian Chadd 		}
3937eb6f0de0SAdrian Chadd 		bf = bf_next;
3938eb6f0de0SAdrian Chadd 	}
3939eb6f0de0SAdrian Chadd 
3940eb6f0de0SAdrian Chadd 	/* Prepend all frames to the beginning of the queue */
3941eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_LAST(&bf_q, ath_bufhead_s)) != NULL) {
3942eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_q, bf, bf_list);
3943eb6f0de0SAdrian Chadd 		ATH_TXQ_INSERT_HEAD(tid, bf, bf_list);
3944eb6f0de0SAdrian Chadd 	}
3945eb6f0de0SAdrian Chadd 
394639da9d42SAdrian Chadd 	/*
394739da9d42SAdrian Chadd 	 * Schedule the TID to be re-tried.
394839da9d42SAdrian Chadd 	 */
3949eb6f0de0SAdrian Chadd 	ath_tx_tid_sched(sc, tid);
3950eb6f0de0SAdrian Chadd 
3951eb6f0de0SAdrian Chadd 	/*
3952eb6f0de0SAdrian Chadd 	 * send bar if we dropped any frames
3953eb6f0de0SAdrian Chadd 	 *
3954eb6f0de0SAdrian Chadd 	 * Keep the txq lock held for now, as we need to ensure
3955eb6f0de0SAdrian Chadd 	 * that ni_txseqs[] is consistent (as it's being updated
3956eb6f0de0SAdrian Chadd 	 * in the ifnet TX context or raw TX context.)
3957eb6f0de0SAdrian Chadd 	 */
3958eb6f0de0SAdrian Chadd 	if (drops) {
395988b3d483SAdrian Chadd 		/* Suspend the TX queue and get ready to send the BAR */
396088b3d483SAdrian Chadd 		ath_tx_tid_bar_suspend(sc, tid);
3961eb6f0de0SAdrian Chadd 	}
3962eb6f0de0SAdrian Chadd 
396388b3d483SAdrian Chadd 	/*
396488b3d483SAdrian Chadd 	 * Send BAR if required
396588b3d483SAdrian Chadd 	 */
396688b3d483SAdrian Chadd 	if (ath_tx_tid_bar_tx_ready(sc, tid))
396788b3d483SAdrian Chadd 		ath_tx_tid_bar_tx(sc, tid);
3968f1bc738eSAdrian Chadd 
396988b3d483SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[tid->ac]);
397088b3d483SAdrian Chadd 
3971eb6f0de0SAdrian Chadd 	/* Complete frames which errored out */
3972eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
3973eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
3974eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
3975eb6f0de0SAdrian Chadd 	}
3976eb6f0de0SAdrian Chadd }
3977eb6f0de0SAdrian Chadd 
3978eb6f0de0SAdrian Chadd /*
3979eb6f0de0SAdrian Chadd  * Handle clean-up of packets from an aggregate list.
3980eb6f0de0SAdrian Chadd  *
3981eb6f0de0SAdrian Chadd  * There's no need to update the BAW here - the session is being
3982eb6f0de0SAdrian Chadd  * torn down.
3983eb6f0de0SAdrian Chadd  */
3984eb6f0de0SAdrian Chadd static void
3985eb6f0de0SAdrian Chadd ath_tx_comp_cleanup_aggr(struct ath_softc *sc, struct ath_buf *bf_first)
3986eb6f0de0SAdrian Chadd {
3987eb6f0de0SAdrian Chadd 	struct ath_buf *bf, *bf_next;
3988eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf_first->bf_node;
3989eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
3990eb6f0de0SAdrian Chadd 	int tid = bf_first->bf_state.bfs_tid;
3991eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
3992eb6f0de0SAdrian Chadd 
3993eb6f0de0SAdrian Chadd 	bf = bf_first;
3994eb6f0de0SAdrian Chadd 
3995eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
3996eb6f0de0SAdrian Chadd 
3997eb6f0de0SAdrian Chadd 	/* update incomp */
3998eb6f0de0SAdrian Chadd 	while (bf) {
3999eb6f0de0SAdrian Chadd 		atid->incomp--;
4000eb6f0de0SAdrian Chadd 		bf = bf->bf_next;
4001eb6f0de0SAdrian Chadd 	}
4002eb6f0de0SAdrian Chadd 
4003eb6f0de0SAdrian Chadd 	if (atid->incomp == 0) {
4004eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4005eb6f0de0SAdrian Chadd 		    "%s: TID %d: cleaned up! resume!\n",
4006eb6f0de0SAdrian Chadd 		    __func__, tid);
4007eb6f0de0SAdrian Chadd 		atid->cleanup_inprogress = 0;
4008eb6f0de0SAdrian Chadd 		ath_tx_tid_resume(sc, atid);
4009eb6f0de0SAdrian Chadd 	}
401088b3d483SAdrian Chadd 
401188b3d483SAdrian Chadd 	/* Send BAR if required */
4012f1bc738eSAdrian Chadd 	/* XXX why would we send a BAR when transitioning to non-aggregation? */
401388b3d483SAdrian Chadd 	if (ath_tx_tid_bar_tx_ready(sc, atid))
401488b3d483SAdrian Chadd 		ath_tx_tid_bar_tx(sc, atid);
4015f1bc738eSAdrian Chadd 
4016eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4017eb6f0de0SAdrian Chadd 
4018eb6f0de0SAdrian Chadd 	/* Handle frame completion */
4019eb6f0de0SAdrian Chadd 	while (bf) {
4020eb6f0de0SAdrian Chadd 		bf_next = bf->bf_next;
4021eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 1);
4022eb6f0de0SAdrian Chadd 		bf = bf_next;
4023eb6f0de0SAdrian Chadd 	}
4024eb6f0de0SAdrian Chadd }
4025eb6f0de0SAdrian Chadd 
4026eb6f0de0SAdrian Chadd /*
4027eb6f0de0SAdrian Chadd  * Handle completion of an set of aggregate frames.
4028eb6f0de0SAdrian Chadd  *
4029eb6f0de0SAdrian Chadd  * XXX for now, simply complete each sub-frame.
4030eb6f0de0SAdrian Chadd  *
4031eb6f0de0SAdrian Chadd  * Note: the completion handler is the last descriptor in the aggregate,
4032eb6f0de0SAdrian Chadd  * not the last descriptor in the first frame.
4033eb6f0de0SAdrian Chadd  */
4034eb6f0de0SAdrian Chadd static void
4035d4365d16SAdrian Chadd ath_tx_aggr_comp_aggr(struct ath_softc *sc, struct ath_buf *bf_first,
4036d4365d16SAdrian Chadd     int fail)
4037eb6f0de0SAdrian Chadd {
4038eb6f0de0SAdrian Chadd 	//struct ath_desc *ds = bf->bf_lastds;
4039eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf_first->bf_node;
4040eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4041eb6f0de0SAdrian Chadd 	int tid = bf_first->bf_state.bfs_tid;
4042eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4043eb6f0de0SAdrian Chadd 	struct ath_tx_status ts;
4044eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
4045eb6f0de0SAdrian Chadd 	ath_bufhead bf_q;
4046eb6f0de0SAdrian Chadd 	ath_bufhead bf_cq;
4047eb6f0de0SAdrian Chadd 	int seq_st, tx_ok;
4048eb6f0de0SAdrian Chadd 	int hasba, isaggr;
4049eb6f0de0SAdrian Chadd 	uint32_t ba[2];
4050eb6f0de0SAdrian Chadd 	struct ath_buf *bf, *bf_next;
4051eb6f0de0SAdrian Chadd 	int ba_index;
4052eb6f0de0SAdrian Chadd 	int drops = 0;
4053eb6f0de0SAdrian Chadd 	int nframes = 0, nbad = 0, nf;
4054eb6f0de0SAdrian Chadd 	int pktlen;
4055eb6f0de0SAdrian Chadd 	/* XXX there's too much on the stack? */
4056b815538dSAdrian Chadd 	struct ath_rc_series rc[ATH_RC_NUM];
4057eb6f0de0SAdrian Chadd 	int txseq;
4058eb6f0de0SAdrian Chadd 
4059eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: called; hwq_depth=%d\n",
4060eb6f0de0SAdrian Chadd 	    __func__, atid->hwq_depth);
4061eb6f0de0SAdrian Chadd 
40620aa5c1bbSAdrian Chadd 	/*
40630aa5c1bbSAdrian Chadd 	 * Take a copy; this may be needed -after- bf_first
40640aa5c1bbSAdrian Chadd 	 * has been completed and freed.
40650aa5c1bbSAdrian Chadd 	 */
40660aa5c1bbSAdrian Chadd 	ts = bf_first->bf_status.ds_txstat;
40670aa5c1bbSAdrian Chadd 
4068f1bc738eSAdrian Chadd 	TAILQ_INIT(&bf_q);
4069f1bc738eSAdrian Chadd 	TAILQ_INIT(&bf_cq);
4070f1bc738eSAdrian Chadd 
4071eb6f0de0SAdrian Chadd 	/* The TID state is kept behind the TXQ lock */
4072eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
4073eb6f0de0SAdrian Chadd 
4074eb6f0de0SAdrian Chadd 	atid->hwq_depth--;
4075eb6f0de0SAdrian Chadd 	if (atid->hwq_depth < 0)
4076eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: hwq_depth < 0: %d\n",
4077eb6f0de0SAdrian Chadd 		    __func__, atid->hwq_depth);
4078eb6f0de0SAdrian Chadd 
4079eb6f0de0SAdrian Chadd 	/*
4080f1bc738eSAdrian Chadd 	 * If the TID is filtered, handle completing the filter
4081f1bc738eSAdrian Chadd 	 * transition before potentially kicking it to the cleanup
4082f1bc738eSAdrian Chadd 	 * function.
40830aa5c1bbSAdrian Chadd 	 *
40840aa5c1bbSAdrian Chadd 	 * XXX this is duplicate work, ew.
4085f1bc738eSAdrian Chadd 	 */
4086f1bc738eSAdrian Chadd 	if (atid->isfiltered)
4087f1bc738eSAdrian Chadd 		ath_tx_tid_filt_comp_complete(sc, atid);
4088f1bc738eSAdrian Chadd 
4089f1bc738eSAdrian Chadd 	/*
4090eb6f0de0SAdrian Chadd 	 * Punt cleanup to the relevant function, not our problem now
4091eb6f0de0SAdrian Chadd 	 */
4092eb6f0de0SAdrian Chadd 	if (atid->cleanup_inprogress) {
4093f1bc738eSAdrian Chadd 		if (atid->isfiltered)
4094f1bc738eSAdrian Chadd 			device_printf(sc->sc_dev,
4095f1bc738eSAdrian Chadd 			    "%s: isfiltered=1, normal_comp?\n",
4096f1bc738eSAdrian Chadd 			    __func__);
4097eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4098eb6f0de0SAdrian Chadd 		ath_tx_comp_cleanup_aggr(sc, bf_first);
4099eb6f0de0SAdrian Chadd 		return;
4100eb6f0de0SAdrian Chadd 	}
4101eb6f0de0SAdrian Chadd 
4102eb6f0de0SAdrian Chadd 	/*
4103f1bc738eSAdrian Chadd 	 * If the frame is filtered, transition to filtered frame
4104f1bc738eSAdrian Chadd 	 * mode and add this to the filtered frame list.
4105f1bc738eSAdrian Chadd 	 *
4106f1bc738eSAdrian Chadd 	 * XXX TODO: figure out how this interoperates with
4107f1bc738eSAdrian Chadd 	 * BAR, pause and cleanup states.
4108f1bc738eSAdrian Chadd 	 */
4109f1bc738eSAdrian Chadd 	if ((ts.ts_status & HAL_TXERR_FILT) ||
4110f1bc738eSAdrian Chadd 	    (ts.ts_status != 0 && atid->isfiltered)) {
4111f1bc738eSAdrian Chadd 		if (fail != 0)
4112f1bc738eSAdrian Chadd 			device_printf(sc->sc_dev,
4113f1bc738eSAdrian Chadd 			    "%s: isfiltered=1, fail=%d\n", __func__, fail);
4114f1bc738eSAdrian Chadd 		ath_tx_tid_filt_comp_aggr(sc, atid, bf_first, &bf_cq);
4115f1bc738eSAdrian Chadd 
4116f1bc738eSAdrian Chadd 		/* Remove from BAW */
4117f1bc738eSAdrian Chadd 		TAILQ_FOREACH_SAFE(bf, &bf_cq, bf_list, bf_next) {
4118f1bc738eSAdrian Chadd 			if (bf->bf_state.bfs_addedbaw)
4119f1bc738eSAdrian Chadd 				drops++;
4120f1bc738eSAdrian Chadd 			if (bf->bf_state.bfs_dobaw) {
4121f1bc738eSAdrian Chadd 				ath_tx_update_baw(sc, an, atid, bf);
4122f1bc738eSAdrian Chadd 				if (! bf->bf_state.bfs_addedbaw)
4123f1bc738eSAdrian Chadd 					device_printf(sc->sc_dev,
4124f1bc738eSAdrian Chadd 					    "%s: wasn't added: seqno %d\n",
4125f1bc738eSAdrian Chadd 					    __func__,
4126f1bc738eSAdrian Chadd 					    SEQNO(bf->bf_state.bfs_seqno));
4127f1bc738eSAdrian Chadd 			}
4128f1bc738eSAdrian Chadd 			bf->bf_state.bfs_dobaw = 0;
4129f1bc738eSAdrian Chadd 		}
4130f1bc738eSAdrian Chadd 		/*
4131f1bc738eSAdrian Chadd 		 * If any intermediate frames in the BAW were dropped when
4132f1bc738eSAdrian Chadd 		 * handling filtering things, send a BAR.
4133f1bc738eSAdrian Chadd 		 */
4134f1bc738eSAdrian Chadd 		if (drops)
4135f1bc738eSAdrian Chadd 			ath_tx_tid_bar_suspend(sc, atid);
4136f1bc738eSAdrian Chadd 
4137f1bc738eSAdrian Chadd 		/*
4138f1bc738eSAdrian Chadd 		 * Finish up by sending a BAR if required and freeing
4139f1bc738eSAdrian Chadd 		 * the frames outside of the TX lock.
4140f1bc738eSAdrian Chadd 		 */
4141f1bc738eSAdrian Chadd 		goto finish_send_bar;
4142f1bc738eSAdrian Chadd 	}
4143f1bc738eSAdrian Chadd 
4144f1bc738eSAdrian Chadd 	/*
4145eb6f0de0SAdrian Chadd 	 * XXX for now, use the first frame in the aggregate for
4146eb6f0de0SAdrian Chadd 	 * XXX rate control completion; it's at least consistent.
4147eb6f0de0SAdrian Chadd 	 */
4148eb6f0de0SAdrian Chadd 	pktlen = bf_first->bf_state.bfs_pktlen;
4149eb6f0de0SAdrian Chadd 
4150eb6f0de0SAdrian Chadd 	/*
4151e9a6408eSAdrian Chadd 	 * Handle errors first!
4152e9a6408eSAdrian Chadd 	 *
4153e9a6408eSAdrian Chadd 	 * Here, handle _any_ error as a "exceeded retries" error.
4154e9a6408eSAdrian Chadd 	 * Later on (when filtered frames are to be specially handled)
4155e9a6408eSAdrian Chadd 	 * it'll have to be expanded.
4156eb6f0de0SAdrian Chadd 	 */
4157e9a6408eSAdrian Chadd #if 0
4158eb6f0de0SAdrian Chadd 	if (ts.ts_status & HAL_TXERR_XRETRY) {
4159e9a6408eSAdrian Chadd #endif
4160e9a6408eSAdrian Chadd 	if (ts.ts_status != 0) {
4161eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4162eb6f0de0SAdrian Chadd 		ath_tx_comp_aggr_error(sc, bf_first, atid);
4163eb6f0de0SAdrian Chadd 		return;
4164eb6f0de0SAdrian Chadd 	}
4165eb6f0de0SAdrian Chadd 
4166eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
4167eb6f0de0SAdrian Chadd 
4168eb6f0de0SAdrian Chadd 	/*
4169eb6f0de0SAdrian Chadd 	 * extract starting sequence and block-ack bitmap
4170eb6f0de0SAdrian Chadd 	 */
4171eb6f0de0SAdrian Chadd 	/* XXX endian-ness of seq_st, ba? */
4172eb6f0de0SAdrian Chadd 	seq_st = ts.ts_seqnum;
4173eb6f0de0SAdrian Chadd 	hasba = !! (ts.ts_flags & HAL_TX_BA);
4174eb6f0de0SAdrian Chadd 	tx_ok = (ts.ts_status == 0);
4175eb6f0de0SAdrian Chadd 	isaggr = bf_first->bf_state.bfs_aggr;
4176eb6f0de0SAdrian Chadd 	ba[0] = ts.ts_ba_low;
4177eb6f0de0SAdrian Chadd 	ba[1] = ts.ts_ba_high;
4178eb6f0de0SAdrian Chadd 
4179eb6f0de0SAdrian Chadd 	/*
4180eb6f0de0SAdrian Chadd 	 * Copy the TX completion status and the rate control
4181eb6f0de0SAdrian Chadd 	 * series from the first descriptor, as it may be freed
4182eb6f0de0SAdrian Chadd 	 * before the rate control code can get its grubby fingers
4183eb6f0de0SAdrian Chadd 	 * into things.
4184eb6f0de0SAdrian Chadd 	 */
4185eb6f0de0SAdrian Chadd 	memcpy(rc, bf_first->bf_state.bfs_rc, sizeof(rc));
4186eb6f0de0SAdrian Chadd 
4187eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4188d4365d16SAdrian Chadd 	    "%s: txa_start=%d, tx_ok=%d, status=%.8x, flags=%.8x, "
4189d4365d16SAdrian Chadd 	    "isaggr=%d, seq_st=%d, hasba=%d, ba=%.8x, %.8x\n",
4190eb6f0de0SAdrian Chadd 	    __func__, tap->txa_start, tx_ok, ts.ts_status, ts.ts_flags,
4191eb6f0de0SAdrian Chadd 	    isaggr, seq_st, hasba, ba[0], ba[1]);
4192eb6f0de0SAdrian Chadd 
4193eb6f0de0SAdrian Chadd 	/* Occasionally, the MAC sends a tx status for the wrong TID. */
4194eb6f0de0SAdrian Chadd 	if (tid != ts.ts_tid) {
4195eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: tid %d != hw tid %d\n",
4196eb6f0de0SAdrian Chadd 		    __func__, tid, ts.ts_tid);
4197eb6f0de0SAdrian Chadd 		tx_ok = 0;
4198eb6f0de0SAdrian Chadd 	}
4199eb6f0de0SAdrian Chadd 
4200eb6f0de0SAdrian Chadd 	/* AR5416 BA bug; this requires an interface reset */
4201eb6f0de0SAdrian Chadd 	if (isaggr && tx_ok && (! hasba)) {
4202eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
4203d4365d16SAdrian Chadd 		    "%s: AR5416 bug: hasba=%d; txok=%d, isaggr=%d, "
4204d4365d16SAdrian Chadd 		    "seq_st=%d\n",
4205eb6f0de0SAdrian Chadd 		    __func__, hasba, tx_ok, isaggr, seq_st);
4206eb6f0de0SAdrian Chadd 		/* XXX TODO: schedule an interface reset */
42070f078d63SJohn Baldwin #ifdef ATH_DEBUG
42086abbbae5SAdrian Chadd 		ath_printtxbuf(sc, bf_first,
42096abbbae5SAdrian Chadd 		    sc->sc_ac2q[atid->ac]->axq_qnum, 0, 0);
42100f078d63SJohn Baldwin #endif
4211eb6f0de0SAdrian Chadd 	}
4212eb6f0de0SAdrian Chadd 
4213eb6f0de0SAdrian Chadd 	/*
4214eb6f0de0SAdrian Chadd 	 * Walk the list of frames, figure out which ones were correctly
4215eb6f0de0SAdrian Chadd 	 * sent and which weren't.
4216eb6f0de0SAdrian Chadd 	 */
4217eb6f0de0SAdrian Chadd 	bf = bf_first;
4218eb6f0de0SAdrian Chadd 	nf = bf_first->bf_state.bfs_nframes;
4219eb6f0de0SAdrian Chadd 
4220eb6f0de0SAdrian Chadd 	/* bf_first is going to be invalid once this list is walked */
4221eb6f0de0SAdrian Chadd 	bf_first = NULL;
4222eb6f0de0SAdrian Chadd 
4223eb6f0de0SAdrian Chadd 	/*
4224eb6f0de0SAdrian Chadd 	 * Walk the list of completed frames and determine
4225eb6f0de0SAdrian Chadd 	 * which need to be completed and which need to be
4226eb6f0de0SAdrian Chadd 	 * retransmitted.
4227eb6f0de0SAdrian Chadd 	 *
4228eb6f0de0SAdrian Chadd 	 * For completed frames, the completion functions need
4229eb6f0de0SAdrian Chadd 	 * to be called at the end of this function as the last
4230eb6f0de0SAdrian Chadd 	 * node reference may free the node.
4231eb6f0de0SAdrian Chadd 	 *
4232eb6f0de0SAdrian Chadd 	 * Finally, since the TXQ lock can't be held during the
4233eb6f0de0SAdrian Chadd 	 * completion callback (to avoid lock recursion),
4234eb6f0de0SAdrian Chadd 	 * the completion calls have to be done outside of the
4235eb6f0de0SAdrian Chadd 	 * lock.
4236eb6f0de0SAdrian Chadd 	 */
4237eb6f0de0SAdrian Chadd 	while (bf) {
4238eb6f0de0SAdrian Chadd 		nframes++;
4239d4365d16SAdrian Chadd 		ba_index = ATH_BA_INDEX(seq_st,
4240d4365d16SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
4241eb6f0de0SAdrian Chadd 		bf_next = bf->bf_next;
4242eb6f0de0SAdrian Chadd 		bf->bf_next = NULL;	/* Remove it from the aggr list */
4243eb6f0de0SAdrian Chadd 
4244eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4245eb6f0de0SAdrian Chadd 		    "%s: checking bf=%p seqno=%d; ack=%d\n",
4246eb6f0de0SAdrian Chadd 		    __func__, bf, SEQNO(bf->bf_state.bfs_seqno),
4247eb6f0de0SAdrian Chadd 		    ATH_BA_ISSET(ba, ba_index));
4248eb6f0de0SAdrian Chadd 
4249eb6f0de0SAdrian Chadd 		if (tx_ok && ATH_BA_ISSET(ba, ba_index)) {
42502d3d4776SAdrian Chadd 			sc->sc_stats.ast_tx_aggr_ok++;
4251eb6f0de0SAdrian Chadd 			ath_tx_update_baw(sc, an, atid, bf);
4252eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_dobaw = 0;
4253eb6f0de0SAdrian Chadd 			if (! bf->bf_state.bfs_addedbaw)
4254eb6f0de0SAdrian Chadd 				device_printf(sc->sc_dev,
4255eb6f0de0SAdrian Chadd 				    "%s: wasn't added: seqno %d\n",
4256eb6f0de0SAdrian Chadd 				    __func__, SEQNO(bf->bf_state.bfs_seqno));
4257eb6f0de0SAdrian Chadd 			bf->bf_next = NULL;
4258eb6f0de0SAdrian Chadd 			TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
4259eb6f0de0SAdrian Chadd 		} else {
42602d3d4776SAdrian Chadd 			sc->sc_stats.ast_tx_aggr_fail++;
4261eb6f0de0SAdrian Chadd 			if (ath_tx_retry_subframe(sc, bf, &bf_q)) {
4262eb6f0de0SAdrian Chadd 				drops++;
4263eb6f0de0SAdrian Chadd 				bf->bf_next = NULL;
4264eb6f0de0SAdrian Chadd 				TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
4265eb6f0de0SAdrian Chadd 			}
4266eb6f0de0SAdrian Chadd 			nbad++;
4267eb6f0de0SAdrian Chadd 		}
4268eb6f0de0SAdrian Chadd 		bf = bf_next;
4269eb6f0de0SAdrian Chadd 	}
4270eb6f0de0SAdrian Chadd 
4271eb6f0de0SAdrian Chadd 	/*
4272eb6f0de0SAdrian Chadd 	 * Now that the BAW updates have been done, unlock
4273eb6f0de0SAdrian Chadd 	 *
4274eb6f0de0SAdrian Chadd 	 * txseq is grabbed before the lock is released so we
4275eb6f0de0SAdrian Chadd 	 * have a consistent view of what -was- in the BAW.
4276eb6f0de0SAdrian Chadd 	 * Anything after this point will not yet have been
4277eb6f0de0SAdrian Chadd 	 * TXed.
4278eb6f0de0SAdrian Chadd 	 */
4279eb6f0de0SAdrian Chadd 	txseq = tap->txa_start;
4280eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4281eb6f0de0SAdrian Chadd 
4282eb6f0de0SAdrian Chadd 	if (nframes != nf)
4283eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
4284eb6f0de0SAdrian Chadd 		    "%s: num frames seen=%d; bf nframes=%d\n",
4285eb6f0de0SAdrian Chadd 		    __func__, nframes, nf);
4286eb6f0de0SAdrian Chadd 
4287eb6f0de0SAdrian Chadd 	/*
4288eb6f0de0SAdrian Chadd 	 * Now we know how many frames were bad, call the rate
4289eb6f0de0SAdrian Chadd 	 * control code.
4290eb6f0de0SAdrian Chadd 	 */
4291eb6f0de0SAdrian Chadd 	if (fail == 0)
4292d4365d16SAdrian Chadd 		ath_tx_update_ratectrl(sc, ni, rc, &ts, pktlen, nframes,
4293d4365d16SAdrian Chadd 		    nbad);
4294eb6f0de0SAdrian Chadd 
4295eb6f0de0SAdrian Chadd 	/*
4296eb6f0de0SAdrian Chadd 	 * send bar if we dropped any frames
4297eb6f0de0SAdrian Chadd 	 */
4298eb6f0de0SAdrian Chadd 	if (drops) {
429988b3d483SAdrian Chadd 		/* Suspend the TX queue and get ready to send the BAR */
430088b3d483SAdrian Chadd 		ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
430188b3d483SAdrian Chadd 		ath_tx_tid_bar_suspend(sc, atid);
430288b3d483SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4303eb6f0de0SAdrian Chadd 	}
4304eb6f0de0SAdrian Chadd 
430539da9d42SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
430639da9d42SAdrian Chadd 	    "%s: txa_start now %d\n", __func__, tap->txa_start);
430739da9d42SAdrian Chadd 
4308eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
430939da9d42SAdrian Chadd 
431039da9d42SAdrian Chadd 	/* Prepend all frames to the beginning of the queue */
4311eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_LAST(&bf_q, ath_bufhead_s)) != NULL) {
4312eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_q, bf, bf_list);
4313eb6f0de0SAdrian Chadd 		ATH_TXQ_INSERT_HEAD(atid, bf, bf_list);
4314eb6f0de0SAdrian Chadd 	}
4315eb6f0de0SAdrian Chadd 
431639da9d42SAdrian Chadd 	/*
431739da9d42SAdrian Chadd 	 * Reschedule to grab some further frames.
431839da9d42SAdrian Chadd 	 */
431939da9d42SAdrian Chadd 	ath_tx_tid_sched(sc, atid);
4320eb6f0de0SAdrian Chadd 
432188b3d483SAdrian Chadd 	/*
4322f1bc738eSAdrian Chadd 	 * If the queue is filtered, re-schedule as required.
4323f1bc738eSAdrian Chadd 	 *
4324f1bc738eSAdrian Chadd 	 * This is required as there may be a subsequent TX descriptor
4325f1bc738eSAdrian Chadd 	 * for this end-node that has CLRDMASK set, so it's quite possible
4326f1bc738eSAdrian Chadd 	 * that a filtered frame will be followed by a non-filtered
4327f1bc738eSAdrian Chadd 	 * (complete or otherwise) frame.
4328f1bc738eSAdrian Chadd 	 *
4329f1bc738eSAdrian Chadd 	 * XXX should we do this before we complete the frame?
4330f1bc738eSAdrian Chadd 	 */
4331f1bc738eSAdrian Chadd 	if (atid->isfiltered)
4332f1bc738eSAdrian Chadd 		ath_tx_tid_filt_comp_complete(sc, atid);
4333f1bc738eSAdrian Chadd 
4334f1bc738eSAdrian Chadd finish_send_bar:
4335f1bc738eSAdrian Chadd 
4336f1bc738eSAdrian Chadd 	/*
433788b3d483SAdrian Chadd 	 * Send BAR if required
433888b3d483SAdrian Chadd 	 */
433988b3d483SAdrian Chadd 	if (ath_tx_tid_bar_tx_ready(sc, atid))
434088b3d483SAdrian Chadd 		ath_tx_tid_bar_tx(sc, atid);
434139da9d42SAdrian Chadd 
434288b3d483SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
434388b3d483SAdrian Chadd 
4344eb6f0de0SAdrian Chadd 	/* Do deferred completion */
4345eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
4346eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
4347eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
4348eb6f0de0SAdrian Chadd 	}
4349eb6f0de0SAdrian Chadd }
4350eb6f0de0SAdrian Chadd 
4351eb6f0de0SAdrian Chadd /*
4352eb6f0de0SAdrian Chadd  * Handle completion of unaggregated frames in an ADDBA
4353eb6f0de0SAdrian Chadd  * session.
4354eb6f0de0SAdrian Chadd  *
4355eb6f0de0SAdrian Chadd  * Fail is set to 1 if the entry is being freed via a call to
4356eb6f0de0SAdrian Chadd  * ath_tx_draintxq().
4357eb6f0de0SAdrian Chadd  */
4358eb6f0de0SAdrian Chadd static void
4359eb6f0de0SAdrian Chadd ath_tx_aggr_comp_unaggr(struct ath_softc *sc, struct ath_buf *bf, int fail)
4360eb6f0de0SAdrian Chadd {
4361eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
4362eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4363eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
4364eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
43650aa5c1bbSAdrian Chadd 	struct ath_tx_status ts;
4366f1bc738eSAdrian Chadd 	int drops = 0;
4367eb6f0de0SAdrian Chadd 
4368eb6f0de0SAdrian Chadd 	/*
43690aa5c1bbSAdrian Chadd 	 * Take a copy of this; filtering/cloning the frame may free the
43700aa5c1bbSAdrian Chadd 	 * bf pointer.
43710aa5c1bbSAdrian Chadd 	 */
43720aa5c1bbSAdrian Chadd 	ts = bf->bf_status.ds_txstat;
43730aa5c1bbSAdrian Chadd 
43740aa5c1bbSAdrian Chadd 	/*
4375eb6f0de0SAdrian Chadd 	 * Update rate control status here, before we possibly
4376eb6f0de0SAdrian Chadd 	 * punt to retry or cleanup.
4377eb6f0de0SAdrian Chadd 	 *
4378eb6f0de0SAdrian Chadd 	 * Do it outside of the TXQ lock.
4379eb6f0de0SAdrian Chadd 	 */
4380875a9451SAdrian Chadd 	if (fail == 0 && ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0))
4381eb6f0de0SAdrian Chadd 		ath_tx_update_ratectrl(sc, ni, bf->bf_state.bfs_rc,
4382eb6f0de0SAdrian Chadd 		    &bf->bf_status.ds_txstat,
4383eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_pktlen,
43840aa5c1bbSAdrian Chadd 		    1, (ts.ts_status == 0) ? 0 : 1);
4385eb6f0de0SAdrian Chadd 
4386eb6f0de0SAdrian Chadd 	/*
4387eb6f0de0SAdrian Chadd 	 * This is called early so atid->hwq_depth can be tracked.
4388eb6f0de0SAdrian Chadd 	 * This unfortunately means that it's released and regrabbed
4389eb6f0de0SAdrian Chadd 	 * during retry and cleanup. That's rather inefficient.
4390eb6f0de0SAdrian Chadd 	 */
4391eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
4392eb6f0de0SAdrian Chadd 
4393eb6f0de0SAdrian Chadd 	if (tid == IEEE80211_NONQOS_TID)
4394eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: TID=16!\n", __func__);
4395eb6f0de0SAdrian Chadd 
4396d4365d16SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX,
4397d4365d16SAdrian Chadd 	    "%s: bf=%p: tid=%d, hwq_depth=%d, seqno=%d\n",
4398d4365d16SAdrian Chadd 	    __func__, bf, bf->bf_state.bfs_tid, atid->hwq_depth,
4399d4365d16SAdrian Chadd 	    SEQNO(bf->bf_state.bfs_seqno));
4400eb6f0de0SAdrian Chadd 
4401eb6f0de0SAdrian Chadd 	atid->hwq_depth--;
4402eb6f0de0SAdrian Chadd 	if (atid->hwq_depth < 0)
4403eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: hwq_depth < 0: %d\n",
4404eb6f0de0SAdrian Chadd 		    __func__, atid->hwq_depth);
4405eb6f0de0SAdrian Chadd 
4406eb6f0de0SAdrian Chadd 	/*
4407f1bc738eSAdrian Chadd 	 * If the TID is filtered, handle completing the filter
4408f1bc738eSAdrian Chadd 	 * transition before potentially kicking it to the cleanup
4409f1bc738eSAdrian Chadd 	 * function.
4410f1bc738eSAdrian Chadd 	 */
4411f1bc738eSAdrian Chadd 	if (atid->isfiltered)
4412f1bc738eSAdrian Chadd 		ath_tx_tid_filt_comp_complete(sc, atid);
4413f1bc738eSAdrian Chadd 
4414f1bc738eSAdrian Chadd 	/*
4415eb6f0de0SAdrian Chadd 	 * If a cleanup is in progress, punt to comp_cleanup;
4416eb6f0de0SAdrian Chadd 	 * rather than handling it here. It's thus their
4417eb6f0de0SAdrian Chadd 	 * responsibility to clean up, call the completion
4418eb6f0de0SAdrian Chadd 	 * function in net80211, etc.
4419eb6f0de0SAdrian Chadd 	 */
4420eb6f0de0SAdrian Chadd 	if (atid->cleanup_inprogress) {
4421f1bc738eSAdrian Chadd 		if (atid->isfiltered)
4422f1bc738eSAdrian Chadd 			device_printf(sc->sc_dev,
4423f1bc738eSAdrian Chadd 			    "%s: isfiltered=1, normal_comp?\n",
4424f1bc738eSAdrian Chadd 			    __func__);
4425eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4426d4365d16SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: cleanup_unaggr\n",
4427d4365d16SAdrian Chadd 		    __func__);
4428eb6f0de0SAdrian Chadd 		ath_tx_comp_cleanup_unaggr(sc, bf);
4429eb6f0de0SAdrian Chadd 		return;
4430eb6f0de0SAdrian Chadd 	}
4431eb6f0de0SAdrian Chadd 
4432eb6f0de0SAdrian Chadd 	/*
4433f1bc738eSAdrian Chadd 	 * XXX TODO: how does cleanup, BAR and filtered frame handling
4434f1bc738eSAdrian Chadd 	 * overlap?
4435f1bc738eSAdrian Chadd 	 *
4436f1bc738eSAdrian Chadd 	 * If the frame is filtered OR if it's any failure but
4437f1bc738eSAdrian Chadd 	 * the TID is filtered, the frame must be added to the
4438f1bc738eSAdrian Chadd 	 * filtered frame list.
4439f1bc738eSAdrian Chadd 	 *
4440f1bc738eSAdrian Chadd 	 * However - a busy buffer can't be added to the filtered
4441f1bc738eSAdrian Chadd 	 * list as it will end up being recycled without having
4442f1bc738eSAdrian Chadd 	 * been made available for the hardware.
4443f1bc738eSAdrian Chadd 	 */
44440aa5c1bbSAdrian Chadd 	if ((ts.ts_status & HAL_TXERR_FILT) ||
44450aa5c1bbSAdrian Chadd 	    (ts.ts_status != 0 && atid->isfiltered)) {
4446f1bc738eSAdrian Chadd 		int freeframe;
4447f1bc738eSAdrian Chadd 
4448f1bc738eSAdrian Chadd 		if (fail != 0)
4449f1bc738eSAdrian Chadd 			device_printf(sc->sc_dev,
4450f1bc738eSAdrian Chadd 			    "%s: isfiltered=1, fail=%d\n",
4451f1bc738eSAdrian Chadd 			    __func__,
4452f1bc738eSAdrian Chadd 			    fail);
4453f1bc738eSAdrian Chadd 		freeframe = ath_tx_tid_filt_comp_single(sc, atid, bf);
4454f1bc738eSAdrian Chadd 		if (freeframe) {
4455f1bc738eSAdrian Chadd 			/* Remove from BAW */
4456f1bc738eSAdrian Chadd 			if (bf->bf_state.bfs_addedbaw)
4457f1bc738eSAdrian Chadd 				drops++;
4458f1bc738eSAdrian Chadd 			if (bf->bf_state.bfs_dobaw) {
4459f1bc738eSAdrian Chadd 				ath_tx_update_baw(sc, an, atid, bf);
4460f1bc738eSAdrian Chadd 				if (! bf->bf_state.bfs_addedbaw)
4461f1bc738eSAdrian Chadd 					device_printf(sc->sc_dev,
4462f1bc738eSAdrian Chadd 					    "%s: wasn't added: seqno %d\n",
4463f1bc738eSAdrian Chadd 					    __func__, SEQNO(bf->bf_state.bfs_seqno));
4464f1bc738eSAdrian Chadd 			}
4465f1bc738eSAdrian Chadd 			bf->bf_state.bfs_dobaw = 0;
4466f1bc738eSAdrian Chadd 		}
4467f1bc738eSAdrian Chadd 
4468f1bc738eSAdrian Chadd 		/*
4469f1bc738eSAdrian Chadd 		 * If the frame couldn't be filtered, treat it as a drop and
4470f1bc738eSAdrian Chadd 		 * prepare to send a BAR.
4471f1bc738eSAdrian Chadd 		 */
4472f1bc738eSAdrian Chadd 		if (freeframe && drops)
4473f1bc738eSAdrian Chadd 			ath_tx_tid_bar_suspend(sc, atid);
4474f1bc738eSAdrian Chadd 
4475f1bc738eSAdrian Chadd 		/*
4476f1bc738eSAdrian Chadd 		 * Send BAR if required
4477f1bc738eSAdrian Chadd 		 */
4478f1bc738eSAdrian Chadd 		if (ath_tx_tid_bar_tx_ready(sc, atid))
4479f1bc738eSAdrian Chadd 			ath_tx_tid_bar_tx(sc, atid);
4480f1bc738eSAdrian Chadd 
4481f1bc738eSAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4482f1bc738eSAdrian Chadd 		/*
4483f1bc738eSAdrian Chadd 		 * If freeframe is set, then the frame couldn't be
4484f1bc738eSAdrian Chadd 		 * cloned and bf is still valid.  Just complete/free it.
4485f1bc738eSAdrian Chadd 		 */
4486f1bc738eSAdrian Chadd 		if (freeframe)
4487f1bc738eSAdrian Chadd 			ath_tx_default_comp(sc, bf, fail);
4488f1bc738eSAdrian Chadd 
4489f1bc738eSAdrian Chadd 
4490f1bc738eSAdrian Chadd 		return;
4491f1bc738eSAdrian Chadd 	}
4492f1bc738eSAdrian Chadd 	/*
4493eb6f0de0SAdrian Chadd 	 * Don't bother with the retry check if all frames
4494eb6f0de0SAdrian Chadd 	 * are being failed (eg during queue deletion.)
4495eb6f0de0SAdrian Chadd 	 */
4496e9a6408eSAdrian Chadd #if 0
4497eb6f0de0SAdrian Chadd 	if (fail == 0 && ts->ts_status & HAL_TXERR_XRETRY) {
4498e9a6408eSAdrian Chadd #endif
44990aa5c1bbSAdrian Chadd 	if (fail == 0 && ts.ts_status != 0) {
4500eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4501d4365d16SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: retry_unaggr\n",
4502d4365d16SAdrian Chadd 		    __func__);
4503eb6f0de0SAdrian Chadd 		ath_tx_aggr_retry_unaggr(sc, bf);
4504eb6f0de0SAdrian Chadd 		return;
4505eb6f0de0SAdrian Chadd 	}
4506eb6f0de0SAdrian Chadd 
4507eb6f0de0SAdrian Chadd 	/* Success? Complete */
4508eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: TID=%d, seqno %d\n",
4509eb6f0de0SAdrian Chadd 	    __func__, tid, SEQNO(bf->bf_state.bfs_seqno));
4510eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_dobaw) {
4511eb6f0de0SAdrian Chadd 		ath_tx_update_baw(sc, an, atid, bf);
4512eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_dobaw = 0;
4513eb6f0de0SAdrian Chadd 		if (! bf->bf_state.bfs_addedbaw)
4514eb6f0de0SAdrian Chadd 			device_printf(sc->sc_dev,
4515eb6f0de0SAdrian Chadd 			    "%s: wasn't added: seqno %d\n",
4516eb6f0de0SAdrian Chadd 			    __func__, SEQNO(bf->bf_state.bfs_seqno));
4517eb6f0de0SAdrian Chadd 	}
4518eb6f0de0SAdrian Chadd 
451988b3d483SAdrian Chadd 	/*
4520f1bc738eSAdrian Chadd 	 * If the queue is filtered, re-schedule as required.
4521f1bc738eSAdrian Chadd 	 *
4522f1bc738eSAdrian Chadd 	 * This is required as there may be a subsequent TX descriptor
4523f1bc738eSAdrian Chadd 	 * for this end-node that has CLRDMASK set, so it's quite possible
4524f1bc738eSAdrian Chadd 	 * that a filtered frame will be followed by a non-filtered
4525f1bc738eSAdrian Chadd 	 * (complete or otherwise) frame.
4526f1bc738eSAdrian Chadd 	 *
4527f1bc738eSAdrian Chadd 	 * XXX should we do this before we complete the frame?
4528f1bc738eSAdrian Chadd 	 */
4529f1bc738eSAdrian Chadd 	if (atid->isfiltered)
4530f1bc738eSAdrian Chadd 		ath_tx_tid_filt_comp_complete(sc, atid);
4531f1bc738eSAdrian Chadd 
4532f1bc738eSAdrian Chadd 	/*
453388b3d483SAdrian Chadd 	 * Send BAR if required
453488b3d483SAdrian Chadd 	 */
453588b3d483SAdrian Chadd 	if (ath_tx_tid_bar_tx_ready(sc, atid))
453688b3d483SAdrian Chadd 		ath_tx_tid_bar_tx(sc, atid);
453788b3d483SAdrian Chadd 
4538eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4539eb6f0de0SAdrian Chadd 
4540eb6f0de0SAdrian Chadd 	ath_tx_default_comp(sc, bf, fail);
4541eb6f0de0SAdrian Chadd 	/* bf is freed at this point */
4542eb6f0de0SAdrian Chadd }
4543eb6f0de0SAdrian Chadd 
4544eb6f0de0SAdrian Chadd void
4545eb6f0de0SAdrian Chadd ath_tx_aggr_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
4546eb6f0de0SAdrian Chadd {
4547eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_aggr)
4548eb6f0de0SAdrian Chadd 		ath_tx_aggr_comp_aggr(sc, bf, fail);
4549eb6f0de0SAdrian Chadd 	else
4550eb6f0de0SAdrian Chadd 		ath_tx_aggr_comp_unaggr(sc, bf, fail);
4551eb6f0de0SAdrian Chadd }
4552eb6f0de0SAdrian Chadd 
4553eb6f0de0SAdrian Chadd /*
4554eb6f0de0SAdrian Chadd  * Schedule some packets from the given node/TID to the hardware.
4555eb6f0de0SAdrian Chadd  *
4556eb6f0de0SAdrian Chadd  * This is the aggregate version.
4557eb6f0de0SAdrian Chadd  */
4558eb6f0de0SAdrian Chadd void
4559eb6f0de0SAdrian Chadd ath_tx_tid_hw_queue_aggr(struct ath_softc *sc, struct ath_node *an,
4560eb6f0de0SAdrian Chadd     struct ath_tid *tid)
4561eb6f0de0SAdrian Chadd {
4562eb6f0de0SAdrian Chadd 	struct ath_buf *bf;
4563eb6f0de0SAdrian Chadd 	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
4564eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
4565eb6f0de0SAdrian Chadd 	ATH_AGGR_STATUS status;
4566eb6f0de0SAdrian Chadd 	ath_bufhead bf_q;
4567eb6f0de0SAdrian Chadd 
4568eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d\n", __func__, tid->tid);
4569eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
4570eb6f0de0SAdrian Chadd 
4571eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
4572eb6f0de0SAdrian Chadd 
4573eb6f0de0SAdrian Chadd 	if (tid->tid == IEEE80211_NONQOS_TID)
4574eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: called for TID=NONQOS_TID?\n",
4575eb6f0de0SAdrian Chadd 		    __func__);
4576eb6f0de0SAdrian Chadd 
4577eb6f0de0SAdrian Chadd 	for (;;) {
4578eb6f0de0SAdrian Chadd 		status = ATH_AGGR_DONE;
4579eb6f0de0SAdrian Chadd 
4580eb6f0de0SAdrian Chadd 		/*
4581eb6f0de0SAdrian Chadd 		 * If the upper layer has paused the TID, don't
4582eb6f0de0SAdrian Chadd 		 * queue any further packets.
4583eb6f0de0SAdrian Chadd 		 *
4584eb6f0de0SAdrian Chadd 		 * This can also occur from the completion task because
4585eb6f0de0SAdrian Chadd 		 * of packet loss; but as its serialised with this code,
4586eb6f0de0SAdrian Chadd 		 * it won't "appear" half way through queuing packets.
4587eb6f0de0SAdrian Chadd 		 */
4588eb6f0de0SAdrian Chadd 		if (tid->paused)
4589eb6f0de0SAdrian Chadd 			break;
4590eb6f0de0SAdrian Chadd 
4591eb6f0de0SAdrian Chadd 		bf = TAILQ_FIRST(&tid->axq_q);
4592eb6f0de0SAdrian Chadd 		if (bf == NULL) {
4593eb6f0de0SAdrian Chadd 			break;
4594eb6f0de0SAdrian Chadd 		}
4595eb6f0de0SAdrian Chadd 
4596eb6f0de0SAdrian Chadd 		/*
4597eb6f0de0SAdrian Chadd 		 * If the packet doesn't fall within the BAW (eg a NULL
4598eb6f0de0SAdrian Chadd 		 * data frame), schedule it directly; continue.
4599eb6f0de0SAdrian Chadd 		 */
4600eb6f0de0SAdrian Chadd 		if (! bf->bf_state.bfs_dobaw) {
4601d4365d16SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4602d4365d16SAdrian Chadd 			    "%s: non-baw packet\n",
4603eb6f0de0SAdrian Chadd 			    __func__);
4604eb6f0de0SAdrian Chadd 			ATH_TXQ_REMOVE(tid, bf, bf_list);
46052a9f83afSAdrian Chadd 
46062a9f83afSAdrian Chadd 			if (bf->bf_state.bfs_nframes > 1)
46072a9f83afSAdrian Chadd 				device_printf(sc->sc_dev,
46082a9f83afSAdrian Chadd 				    "%s: aggr=%d, nframes=%d\n",
46092a9f83afSAdrian Chadd 				    __func__,
46102a9f83afSAdrian Chadd 				    bf->bf_state.bfs_aggr,
46112a9f83afSAdrian Chadd 				    bf->bf_state.bfs_nframes);
46122a9f83afSAdrian Chadd 
46132a9f83afSAdrian Chadd 			/*
46142a9f83afSAdrian Chadd 			 * This shouldn't happen - such frames shouldn't
46152a9f83afSAdrian Chadd 			 * ever have been queued as an aggregate in the
46162a9f83afSAdrian Chadd 			 * first place.  However, make sure the fields
46172a9f83afSAdrian Chadd 			 * are correctly setup just to be totally sure.
46182a9f83afSAdrian Chadd 			 */
4619eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_aggr = 0;
46202a9f83afSAdrian Chadd 			bf->bf_state.bfs_nframes = 1;
46212a9f83afSAdrian Chadd 
46224e81f27cSAdrian Chadd 			/* Update CLRDMASK just before this frame is queued */
46234e81f27cSAdrian Chadd 			ath_tx_update_clrdmask(sc, tid, bf);
46244e81f27cSAdrian Chadd 
4625eb6f0de0SAdrian Chadd 			ath_tx_do_ratelookup(sc, bf);
4626e2e4a2c2SAdrian Chadd 			ath_tx_calc_duration(sc, bf);
4627e2e4a2c2SAdrian Chadd 			ath_tx_calc_protection(sc, bf);
4628eb6f0de0SAdrian Chadd 			ath_tx_set_rtscts(sc, bf);
4629e2e4a2c2SAdrian Chadd 			ath_tx_rate_fill_rcflags(sc, bf);
4630eb6f0de0SAdrian Chadd 			ath_tx_setds(sc, bf);
4631eb6f0de0SAdrian Chadd 			ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
4632eb6f0de0SAdrian Chadd 
4633eb6f0de0SAdrian Chadd 			sc->sc_aggr_stats.aggr_nonbaw_pkt++;
4634eb6f0de0SAdrian Chadd 
4635eb6f0de0SAdrian Chadd 			/* Queue the packet; continue */
4636eb6f0de0SAdrian Chadd 			goto queuepkt;
4637eb6f0de0SAdrian Chadd 		}
4638eb6f0de0SAdrian Chadd 
4639eb6f0de0SAdrian Chadd 		TAILQ_INIT(&bf_q);
4640eb6f0de0SAdrian Chadd 
4641eb6f0de0SAdrian Chadd 		/*
4642eb6f0de0SAdrian Chadd 		 * Do a rate control lookup on the first frame in the
4643eb6f0de0SAdrian Chadd 		 * list. The rate control code needs that to occur
4644eb6f0de0SAdrian Chadd 		 * before it can determine whether to TX.
4645eb6f0de0SAdrian Chadd 		 * It's inaccurate because the rate control code doesn't
4646eb6f0de0SAdrian Chadd 		 * really "do" aggregate lookups, so it only considers
4647eb6f0de0SAdrian Chadd 		 * the size of the first frame.
4648eb6f0de0SAdrian Chadd 		 */
4649eb6f0de0SAdrian Chadd 		ath_tx_do_ratelookup(sc, bf);
4650eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[3].rix = 0;
4651eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[3].tries = 0;
4652e2e4a2c2SAdrian Chadd 
4653e2e4a2c2SAdrian Chadd 		ath_tx_calc_duration(sc, bf);
4654e2e4a2c2SAdrian Chadd 		ath_tx_calc_protection(sc, bf);
4655e2e4a2c2SAdrian Chadd 
4656e2e4a2c2SAdrian Chadd 		ath_tx_set_rtscts(sc, bf);
4657eb6f0de0SAdrian Chadd 		ath_tx_rate_fill_rcflags(sc, bf);
4658eb6f0de0SAdrian Chadd 
4659eb6f0de0SAdrian Chadd 		status = ath_tx_form_aggr(sc, an, tid, &bf_q);
4660eb6f0de0SAdrian Chadd 
4661eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4662eb6f0de0SAdrian Chadd 		    "%s: ath_tx_form_aggr() status=%d\n", __func__, status);
4663eb6f0de0SAdrian Chadd 
4664eb6f0de0SAdrian Chadd 		/*
4665eb6f0de0SAdrian Chadd 		 * No frames to be picked up - out of BAW
4666eb6f0de0SAdrian Chadd 		 */
4667eb6f0de0SAdrian Chadd 		if (TAILQ_EMPTY(&bf_q))
4668eb6f0de0SAdrian Chadd 			break;
4669eb6f0de0SAdrian Chadd 
4670eb6f0de0SAdrian Chadd 		/*
4671eb6f0de0SAdrian Chadd 		 * This assumes that the descriptor list in the ath_bufhead
4672eb6f0de0SAdrian Chadd 		 * are already linked together via bf_next pointers.
4673eb6f0de0SAdrian Chadd 		 */
4674eb6f0de0SAdrian Chadd 		bf = TAILQ_FIRST(&bf_q);
4675eb6f0de0SAdrian Chadd 
4676e2e4a2c2SAdrian Chadd 		if (status == ATH_AGGR_8K_LIMITED)
4677e2e4a2c2SAdrian Chadd 			sc->sc_aggr_stats.aggr_rts_aggr_limited++;
4678e2e4a2c2SAdrian Chadd 
4679eb6f0de0SAdrian Chadd 		/*
4680eb6f0de0SAdrian Chadd 		 * If it's the only frame send as non-aggregate
4681eb6f0de0SAdrian Chadd 		 * assume that ath_tx_form_aggr() has checked
4682eb6f0de0SAdrian Chadd 		 * whether it's in the BAW and added it appropriately.
4683eb6f0de0SAdrian Chadd 		 */
4684eb6f0de0SAdrian Chadd 		if (bf->bf_state.bfs_nframes == 1) {
4685eb6f0de0SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4686eb6f0de0SAdrian Chadd 			    "%s: single-frame aggregate\n", __func__);
46874e81f27cSAdrian Chadd 
46884e81f27cSAdrian Chadd 			/* Update CLRDMASK just before this frame is queued */
46894e81f27cSAdrian Chadd 			ath_tx_update_clrdmask(sc, tid, bf);
46904e81f27cSAdrian Chadd 
4691eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_aggr = 0;
469221840808SAdrian Chadd 			bf->bf_state.bfs_ndelim = 0;
4693eb6f0de0SAdrian Chadd 			ath_tx_setds(sc, bf);
4694eb6f0de0SAdrian Chadd 			ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
4695eb6f0de0SAdrian Chadd 			if (status == ATH_AGGR_BAW_CLOSED)
4696eb6f0de0SAdrian Chadd 				sc->sc_aggr_stats.aggr_baw_closed_single_pkt++;
4697eb6f0de0SAdrian Chadd 			else
4698eb6f0de0SAdrian Chadd 				sc->sc_aggr_stats.aggr_single_pkt++;
4699eb6f0de0SAdrian Chadd 		} else {
4700eb6f0de0SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4701d4365d16SAdrian Chadd 			    "%s: multi-frame aggregate: %d frames, "
4702d4365d16SAdrian Chadd 			    "length %d\n",
4703eb6f0de0SAdrian Chadd 			     __func__, bf->bf_state.bfs_nframes,
4704eb6f0de0SAdrian Chadd 			    bf->bf_state.bfs_al);
4705eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_aggr = 1;
4706eb6f0de0SAdrian Chadd 			sc->sc_aggr_stats.aggr_pkts[bf->bf_state.bfs_nframes]++;
4707eb6f0de0SAdrian Chadd 			sc->sc_aggr_stats.aggr_aggr_pkt++;
4708eb6f0de0SAdrian Chadd 
47094e81f27cSAdrian Chadd 			/* Update CLRDMASK just before this frame is queued */
47104e81f27cSAdrian Chadd 			ath_tx_update_clrdmask(sc, tid, bf);
47114e81f27cSAdrian Chadd 
4712eb6f0de0SAdrian Chadd 			/*
4713e2e4a2c2SAdrian Chadd 			 * Calculate the duration/protection as required.
4714e2e4a2c2SAdrian Chadd 			 */
4715e2e4a2c2SAdrian Chadd 			ath_tx_calc_duration(sc, bf);
4716e2e4a2c2SAdrian Chadd 			ath_tx_calc_protection(sc, bf);
4717e2e4a2c2SAdrian Chadd 
4718e2e4a2c2SAdrian Chadd 			/*
4719eb6f0de0SAdrian Chadd 			 * Update the rate and rtscts information based on the
4720eb6f0de0SAdrian Chadd 			 * rate decision made by the rate control code;
4721eb6f0de0SAdrian Chadd 			 * the first frame in the aggregate needs it.
4722eb6f0de0SAdrian Chadd 			 */
4723eb6f0de0SAdrian Chadd 			ath_tx_set_rtscts(sc, bf);
4724eb6f0de0SAdrian Chadd 
4725eb6f0de0SAdrian Chadd 			/*
4726eb6f0de0SAdrian Chadd 			 * Setup the relevant descriptor fields
4727eb6f0de0SAdrian Chadd 			 * for aggregation. The first descriptor
4728eb6f0de0SAdrian Chadd 			 * already points to the rest in the chain.
4729eb6f0de0SAdrian Chadd 			 */
4730eb6f0de0SAdrian Chadd 			ath_tx_setds_11n(sc, bf);
4731eb6f0de0SAdrian Chadd 
4732eb6f0de0SAdrian Chadd 		}
4733eb6f0de0SAdrian Chadd 	queuepkt:
4734eb6f0de0SAdrian Chadd 		//txq = bf->bf_state.bfs_txq;
4735eb6f0de0SAdrian Chadd 
4736eb6f0de0SAdrian Chadd 		/* Set completion handler, multi-frame aggregate or not */
4737eb6f0de0SAdrian Chadd 		bf->bf_comp = ath_tx_aggr_comp;
4738eb6f0de0SAdrian Chadd 
4739eb6f0de0SAdrian Chadd 		if (bf->bf_state.bfs_tid == IEEE80211_NONQOS_TID)
4740eb6f0de0SAdrian Chadd 		    device_printf(sc->sc_dev, "%s: TID=16?\n", __func__);
4741eb6f0de0SAdrian Chadd 
4742eb6f0de0SAdrian Chadd 		/* Punt to txq */
4743eb6f0de0SAdrian Chadd 		ath_tx_handoff(sc, txq, bf);
4744eb6f0de0SAdrian Chadd 
4745eb6f0de0SAdrian Chadd 		/* Track outstanding buffer count to hardware */
4746eb6f0de0SAdrian Chadd 		/* aggregates are "one" buffer */
4747eb6f0de0SAdrian Chadd 		tid->hwq_depth++;
4748eb6f0de0SAdrian Chadd 
4749eb6f0de0SAdrian Chadd 		/*
4750eb6f0de0SAdrian Chadd 		 * Break out if ath_tx_form_aggr() indicated
4751eb6f0de0SAdrian Chadd 		 * there can't be any further progress (eg BAW is full.)
4752eb6f0de0SAdrian Chadd 		 * Checking for an empty txq is done above.
4753eb6f0de0SAdrian Chadd 		 *
4754eb6f0de0SAdrian Chadd 		 * XXX locking on txq here?
4755eb6f0de0SAdrian Chadd 		 */
4756eb6f0de0SAdrian Chadd 		if (txq->axq_aggr_depth >= sc->sc_hwq_limit ||
4757eb6f0de0SAdrian Chadd 		    status == ATH_AGGR_BAW_CLOSED)
4758eb6f0de0SAdrian Chadd 			break;
4759eb6f0de0SAdrian Chadd 	}
4760eb6f0de0SAdrian Chadd }
4761eb6f0de0SAdrian Chadd 
4762eb6f0de0SAdrian Chadd /*
4763eb6f0de0SAdrian Chadd  * Schedule some packets from the given node/TID to the hardware.
4764eb6f0de0SAdrian Chadd  */
4765eb6f0de0SAdrian Chadd void
4766eb6f0de0SAdrian Chadd ath_tx_tid_hw_queue_norm(struct ath_softc *sc, struct ath_node *an,
4767eb6f0de0SAdrian Chadd     struct ath_tid *tid)
4768eb6f0de0SAdrian Chadd {
4769eb6f0de0SAdrian Chadd 	struct ath_buf *bf;
4770eb6f0de0SAdrian Chadd 	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
4771eb6f0de0SAdrian Chadd 
4772eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: node %p: TID %d: called\n",
4773eb6f0de0SAdrian Chadd 	    __func__, an, tid->tid);
4774eb6f0de0SAdrian Chadd 
47754e81f27cSAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
4776eb6f0de0SAdrian Chadd 
4777eb6f0de0SAdrian Chadd 	/* Check - is AMPDU pending or running? then print out something */
4778eb6f0de0SAdrian Chadd 	if (ath_tx_ampdu_pending(sc, an, tid->tid))
4779eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: tid=%d, ampdu pending?\n",
4780eb6f0de0SAdrian Chadd 		    __func__, tid->tid);
4781eb6f0de0SAdrian Chadd 	if (ath_tx_ampdu_running(sc, an, tid->tid))
4782eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: tid=%d, ampdu running?\n",
4783eb6f0de0SAdrian Chadd 		    __func__, tid->tid);
4784eb6f0de0SAdrian Chadd 
4785eb6f0de0SAdrian Chadd 	for (;;) {
4786eb6f0de0SAdrian Chadd 
4787eb6f0de0SAdrian Chadd 		/*
4788eb6f0de0SAdrian Chadd 		 * If the upper layers have paused the TID, don't
4789eb6f0de0SAdrian Chadd 		 * queue any further packets.
4790eb6f0de0SAdrian Chadd 		 */
4791eb6f0de0SAdrian Chadd 		if (tid->paused)
4792eb6f0de0SAdrian Chadd 			break;
4793eb6f0de0SAdrian Chadd 
4794eb6f0de0SAdrian Chadd 		bf = TAILQ_FIRST(&tid->axq_q);
4795eb6f0de0SAdrian Chadd 		if (bf == NULL) {
4796eb6f0de0SAdrian Chadd 			break;
4797eb6f0de0SAdrian Chadd 		}
4798eb6f0de0SAdrian Chadd 
4799eb6f0de0SAdrian Chadd 		ATH_TXQ_REMOVE(tid, bf, bf_list);
4800eb6f0de0SAdrian Chadd 
4801eb6f0de0SAdrian Chadd 		KASSERT(txq == bf->bf_state.bfs_txq, ("txqs not equal!\n"));
4802eb6f0de0SAdrian Chadd 
4803eb6f0de0SAdrian Chadd 		/* Sanity check! */
4804eb6f0de0SAdrian Chadd 		if (tid->tid != bf->bf_state.bfs_tid) {
4805eb6f0de0SAdrian Chadd 			device_printf(sc->sc_dev, "%s: bfs_tid %d !="
4806eb6f0de0SAdrian Chadd 			    " tid %d\n",
4807eb6f0de0SAdrian Chadd 			    __func__, bf->bf_state.bfs_tid, tid->tid);
4808eb6f0de0SAdrian Chadd 		}
4809eb6f0de0SAdrian Chadd 		/* Normal completion handler */
4810eb6f0de0SAdrian Chadd 		bf->bf_comp = ath_tx_normal_comp;
4811eb6f0de0SAdrian Chadd 
4812*0c54de88SAdrian Chadd 		/*
4813*0c54de88SAdrian Chadd 		 * Override this for now, until the non-aggregate
4814*0c54de88SAdrian Chadd 		 * completion handler correctly handles software retransmits.
4815*0c54de88SAdrian Chadd 		 */
4816*0c54de88SAdrian Chadd 		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
4817*0c54de88SAdrian Chadd 
48184e81f27cSAdrian Chadd 		/* Update CLRDMASK just before this frame is queued */
48194e81f27cSAdrian Chadd 		ath_tx_update_clrdmask(sc, tid, bf);
48204e81f27cSAdrian Chadd 
4821eb6f0de0SAdrian Chadd 		/* Program descriptors + rate control */
4822eb6f0de0SAdrian Chadd 		ath_tx_do_ratelookup(sc, bf);
4823e2e4a2c2SAdrian Chadd 		ath_tx_calc_duration(sc, bf);
4824e2e4a2c2SAdrian Chadd 		ath_tx_calc_protection(sc, bf);
4825eb6f0de0SAdrian Chadd 		ath_tx_set_rtscts(sc, bf);
4826e2e4a2c2SAdrian Chadd 		ath_tx_rate_fill_rcflags(sc, bf);
4827eb6f0de0SAdrian Chadd 		ath_tx_setds(sc, bf);
4828eb6f0de0SAdrian Chadd 
4829eb6f0de0SAdrian Chadd 		/* Track outstanding buffer count to hardware */
4830eb6f0de0SAdrian Chadd 		/* aggregates are "one" buffer */
4831eb6f0de0SAdrian Chadd 		tid->hwq_depth++;
4832eb6f0de0SAdrian Chadd 
4833eb6f0de0SAdrian Chadd 		/* Punt to hardware or software txq */
4834eb6f0de0SAdrian Chadd 		ath_tx_handoff(sc, txq, bf);
4835eb6f0de0SAdrian Chadd 	}
4836eb6f0de0SAdrian Chadd }
4837eb6f0de0SAdrian Chadd 
4838eb6f0de0SAdrian Chadd /*
4839eb6f0de0SAdrian Chadd  * Schedule some packets to the given hardware queue.
4840eb6f0de0SAdrian Chadd  *
4841eb6f0de0SAdrian Chadd  * This function walks the list of TIDs (ie, ath_node TIDs
4842eb6f0de0SAdrian Chadd  * with queued traffic) and attempts to schedule traffic
4843eb6f0de0SAdrian Chadd  * from them.
4844eb6f0de0SAdrian Chadd  *
4845eb6f0de0SAdrian Chadd  * TID scheduling is implemented as a FIFO, with TIDs being
4846eb6f0de0SAdrian Chadd  * added to the end of the queue after some frames have been
4847eb6f0de0SAdrian Chadd  * scheduled.
4848eb6f0de0SAdrian Chadd  */
4849eb6f0de0SAdrian Chadd void
4850eb6f0de0SAdrian Chadd ath_txq_sched(struct ath_softc *sc, struct ath_txq *txq)
4851eb6f0de0SAdrian Chadd {
4852eb6f0de0SAdrian Chadd 	struct ath_tid *tid, *next, *last;
4853eb6f0de0SAdrian Chadd 
4854eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
4855eb6f0de0SAdrian Chadd 
4856eb6f0de0SAdrian Chadd 	/*
4857eb6f0de0SAdrian Chadd 	 * Don't schedule if the hardware queue is busy.
4858eb6f0de0SAdrian Chadd 	 * This (hopefully) gives some more time to aggregate
4859eb6f0de0SAdrian Chadd 	 * some packets in the aggregation queue.
4860eb6f0de0SAdrian Chadd 	 */
4861eb6f0de0SAdrian Chadd 	if (txq->axq_aggr_depth >= sc->sc_hwq_limit) {
4862eb6f0de0SAdrian Chadd 		sc->sc_aggr_stats.aggr_sched_nopkt++;
4863eb6f0de0SAdrian Chadd 		return;
4864eb6f0de0SAdrian Chadd 	}
4865eb6f0de0SAdrian Chadd 
4866eb6f0de0SAdrian Chadd 	last = TAILQ_LAST(&txq->axq_tidq, axq_t_s);
4867eb6f0de0SAdrian Chadd 
4868eb6f0de0SAdrian Chadd 	TAILQ_FOREACH_SAFE(tid, &txq->axq_tidq, axq_qelem, next) {
4869eb6f0de0SAdrian Chadd 		/*
4870eb6f0de0SAdrian Chadd 		 * Suspend paused queues here; they'll be resumed
4871eb6f0de0SAdrian Chadd 		 * once the addba completes or times out.
4872eb6f0de0SAdrian Chadd 		 */
4873eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d, paused=%d\n",
4874eb6f0de0SAdrian Chadd 		    __func__, tid->tid, tid->paused);
4875eb6f0de0SAdrian Chadd 		ath_tx_tid_unsched(sc, tid);
4876eb6f0de0SAdrian Chadd 		if (tid->paused) {
4877eb6f0de0SAdrian Chadd 			continue;
4878eb6f0de0SAdrian Chadd 		}
4879eb6f0de0SAdrian Chadd 		if (ath_tx_ampdu_running(sc, tid->an, tid->tid))
4880eb6f0de0SAdrian Chadd 			ath_tx_tid_hw_queue_aggr(sc, tid->an, tid);
4881eb6f0de0SAdrian Chadd 		else
4882eb6f0de0SAdrian Chadd 			ath_tx_tid_hw_queue_norm(sc, tid->an, tid);
4883eb6f0de0SAdrian Chadd 
4884eb6f0de0SAdrian Chadd 		/* Not empty? Re-schedule */
4885eb6f0de0SAdrian Chadd 		if (tid->axq_depth != 0)
4886eb6f0de0SAdrian Chadd 			ath_tx_tid_sched(sc, tid);
4887eb6f0de0SAdrian Chadd 
4888eb6f0de0SAdrian Chadd 		/* Give the software queue time to aggregate more packets */
4889eb6f0de0SAdrian Chadd 		if (txq->axq_aggr_depth >= sc->sc_hwq_limit) {
4890eb6f0de0SAdrian Chadd 			break;
4891eb6f0de0SAdrian Chadd 		}
4892eb6f0de0SAdrian Chadd 
4893eb6f0de0SAdrian Chadd 		/*
4894eb6f0de0SAdrian Chadd 		 * If this was the last entry on the original list, stop.
4895eb6f0de0SAdrian Chadd 		 * Otherwise nodes that have been rescheduled onto the end
4896eb6f0de0SAdrian Chadd 		 * of the TID FIFO list will just keep being rescheduled.
4897eb6f0de0SAdrian Chadd 		 */
4898eb6f0de0SAdrian Chadd 		if (tid == last)
4899eb6f0de0SAdrian Chadd 			break;
4900eb6f0de0SAdrian Chadd 	}
4901eb6f0de0SAdrian Chadd }
4902eb6f0de0SAdrian Chadd 
4903eb6f0de0SAdrian Chadd /*
4904eb6f0de0SAdrian Chadd  * TX addba handling
4905eb6f0de0SAdrian Chadd  */
4906eb6f0de0SAdrian Chadd 
4907eb6f0de0SAdrian Chadd /*
4908eb6f0de0SAdrian Chadd  * Return net80211 TID struct pointer, or NULL for none
4909eb6f0de0SAdrian Chadd  */
4910eb6f0de0SAdrian Chadd struct ieee80211_tx_ampdu *
4911eb6f0de0SAdrian Chadd ath_tx_get_tx_tid(struct ath_node *an, int tid)
4912eb6f0de0SAdrian Chadd {
4913eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = &an->an_node;
4914eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
4915eb6f0de0SAdrian Chadd 
4916eb6f0de0SAdrian Chadd 	if (tid == IEEE80211_NONQOS_TID)
4917eb6f0de0SAdrian Chadd 		return NULL;
4918eb6f0de0SAdrian Chadd 
49192aa563dfSAdrian Chadd 	tap = &ni->ni_tx_ampdu[tid];
4920eb6f0de0SAdrian Chadd 	return tap;
4921eb6f0de0SAdrian Chadd }
4922eb6f0de0SAdrian Chadd 
4923eb6f0de0SAdrian Chadd /*
4924eb6f0de0SAdrian Chadd  * Is AMPDU-TX running?
4925eb6f0de0SAdrian Chadd  */
4926eb6f0de0SAdrian Chadd static int
4927eb6f0de0SAdrian Chadd ath_tx_ampdu_running(struct ath_softc *sc, struct ath_node *an, int tid)
4928eb6f0de0SAdrian Chadd {
4929eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
4930eb6f0de0SAdrian Chadd 
4931eb6f0de0SAdrian Chadd 	if (tid == IEEE80211_NONQOS_TID)
4932eb6f0de0SAdrian Chadd 		return 0;
4933eb6f0de0SAdrian Chadd 
4934eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
4935eb6f0de0SAdrian Chadd 	if (tap == NULL)
4936eb6f0de0SAdrian Chadd 		return 0;	/* Not valid; default to not running */
4937eb6f0de0SAdrian Chadd 
4938eb6f0de0SAdrian Chadd 	return !! (tap->txa_flags & IEEE80211_AGGR_RUNNING);
4939eb6f0de0SAdrian Chadd }
4940eb6f0de0SAdrian Chadd 
4941eb6f0de0SAdrian Chadd /*
4942eb6f0de0SAdrian Chadd  * Is AMPDU-TX negotiation pending?
4943eb6f0de0SAdrian Chadd  */
4944eb6f0de0SAdrian Chadd static int
4945eb6f0de0SAdrian Chadd ath_tx_ampdu_pending(struct ath_softc *sc, struct ath_node *an, int tid)
4946eb6f0de0SAdrian Chadd {
4947eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
4948eb6f0de0SAdrian Chadd 
4949eb6f0de0SAdrian Chadd 	if (tid == IEEE80211_NONQOS_TID)
4950eb6f0de0SAdrian Chadd 		return 0;
4951eb6f0de0SAdrian Chadd 
4952eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
4953eb6f0de0SAdrian Chadd 	if (tap == NULL)
4954eb6f0de0SAdrian Chadd 		return 0;	/* Not valid; default to not pending */
4955eb6f0de0SAdrian Chadd 
4956eb6f0de0SAdrian Chadd 	return !! (tap->txa_flags & IEEE80211_AGGR_XCHGPEND);
4957eb6f0de0SAdrian Chadd }
4958eb6f0de0SAdrian Chadd 
4959eb6f0de0SAdrian Chadd /*
4960eb6f0de0SAdrian Chadd  * Is AMPDU-TX pending for the given TID?
4961eb6f0de0SAdrian Chadd  */
4962eb6f0de0SAdrian Chadd 
4963eb6f0de0SAdrian Chadd 
4964eb6f0de0SAdrian Chadd /*
4965eb6f0de0SAdrian Chadd  * Method to handle sending an ADDBA request.
4966eb6f0de0SAdrian Chadd  *
4967eb6f0de0SAdrian Chadd  * We tap this so the relevant flags can be set to pause the TID
4968eb6f0de0SAdrian Chadd  * whilst waiting for the response.
4969eb6f0de0SAdrian Chadd  *
4970eb6f0de0SAdrian Chadd  * XXX there's no timeout handler we can override?
4971eb6f0de0SAdrian Chadd  */
4972eb6f0de0SAdrian Chadd int
4973eb6f0de0SAdrian Chadd ath_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
4974eb6f0de0SAdrian Chadd     int dialogtoken, int baparamset, int batimeout)
4975eb6f0de0SAdrian Chadd {
4976eb6f0de0SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
49772aa563dfSAdrian Chadd 	int tid = tap->txa_tid;
4978eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4979eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4980eb6f0de0SAdrian Chadd 
4981eb6f0de0SAdrian Chadd 	/*
4982eb6f0de0SAdrian Chadd 	 * XXX danger Will Robinson!
4983eb6f0de0SAdrian Chadd 	 *
4984eb6f0de0SAdrian Chadd 	 * Although the taskqueue may be running and scheduling some more
4985eb6f0de0SAdrian Chadd 	 * packets, these should all be _before_ the addba sequence number.
4986eb6f0de0SAdrian Chadd 	 * However, net80211 will keep self-assigning sequence numbers
4987eb6f0de0SAdrian Chadd 	 * until addba has been negotiated.
4988eb6f0de0SAdrian Chadd 	 *
4989eb6f0de0SAdrian Chadd 	 * In the past, these packets would be "paused" (which still works
4990eb6f0de0SAdrian Chadd 	 * fine, as they're being scheduled to the driver in the same
4991eb6f0de0SAdrian Chadd 	 * serialised method which is calling the addba request routine)
4992eb6f0de0SAdrian Chadd 	 * and when the aggregation session begins, they'll be dequeued
4993eb6f0de0SAdrian Chadd 	 * as aggregate packets and added to the BAW. However, now there's
4994eb6f0de0SAdrian Chadd 	 * a "bf->bf_state.bfs_dobaw" flag, and this isn't set for these
4995eb6f0de0SAdrian Chadd 	 * packets. Thus they never get included in the BAW tracking and
4996eb6f0de0SAdrian Chadd 	 * this can cause the initial burst of packets after the addba
4997eb6f0de0SAdrian Chadd 	 * negotiation to "hang", as they quickly fall outside the BAW.
4998eb6f0de0SAdrian Chadd 	 *
4999eb6f0de0SAdrian Chadd 	 * The "eventual" solution should be to tag these packets with
5000eb6f0de0SAdrian Chadd 	 * dobaw. Although net80211 has given us a sequence number,
5001eb6f0de0SAdrian Chadd 	 * it'll be "after" the left edge of the BAW and thus it'll
5002eb6f0de0SAdrian Chadd 	 * fall within it.
5003eb6f0de0SAdrian Chadd 	 */
500496ff26ffSAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
5005d3a6425bSAdrian Chadd 	/*
5006d3a6425bSAdrian Chadd 	 * This is a bit annoying.  Until net80211 HT code inherits some
5007d3a6425bSAdrian Chadd 	 * (any) locking, we may have this called in parallel BUT only
5008d3a6425bSAdrian Chadd 	 * one response/timeout will be called.  Grr.
5009d3a6425bSAdrian Chadd 	 */
5010d3a6425bSAdrian Chadd 	if (atid->addba_tx_pending == 0) {
5011eb6f0de0SAdrian Chadd 		ath_tx_tid_pause(sc, atid);
5012d3a6425bSAdrian Chadd 		atid->addba_tx_pending = 1;
5013d3a6425bSAdrian Chadd 	}
501496ff26ffSAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
5015eb6f0de0SAdrian Chadd 
5016eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5017eb6f0de0SAdrian Chadd 	    "%s: called; dialogtoken=%d, baparamset=%d, batimeout=%d\n",
5018eb6f0de0SAdrian Chadd 	    __func__, dialogtoken, baparamset, batimeout);
5019eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5020eb6f0de0SAdrian Chadd 	    "%s: txa_start=%d, ni_txseqs=%d\n",
5021eb6f0de0SAdrian Chadd 	    __func__, tap->txa_start, ni->ni_txseqs[tid]);
5022eb6f0de0SAdrian Chadd 
5023eb6f0de0SAdrian Chadd 	return sc->sc_addba_request(ni, tap, dialogtoken, baparamset,
5024eb6f0de0SAdrian Chadd 	    batimeout);
5025eb6f0de0SAdrian Chadd }
5026eb6f0de0SAdrian Chadd 
5027eb6f0de0SAdrian Chadd /*
5028eb6f0de0SAdrian Chadd  * Handle an ADDBA response.
5029eb6f0de0SAdrian Chadd  *
5030eb6f0de0SAdrian Chadd  * We unpause the queue so TX'ing can resume.
5031eb6f0de0SAdrian Chadd  *
5032eb6f0de0SAdrian Chadd  * Any packets TX'ed from this point should be "aggregate" (whether
5033eb6f0de0SAdrian Chadd  * aggregate or not) so the BAW is updated.
5034eb6f0de0SAdrian Chadd  *
5035eb6f0de0SAdrian Chadd  * Note! net80211 keeps self-assigning sequence numbers until
5036eb6f0de0SAdrian Chadd  * ampdu is negotiated. This means the initially-negotiated BAW left
5037eb6f0de0SAdrian Chadd  * edge won't match the ni->ni_txseq.
5038eb6f0de0SAdrian Chadd  *
5039eb6f0de0SAdrian Chadd  * So, being very dirty, the BAW left edge is "slid" here to match
5040eb6f0de0SAdrian Chadd  * ni->ni_txseq.
5041eb6f0de0SAdrian Chadd  *
5042eb6f0de0SAdrian Chadd  * What likely SHOULD happen is that all packets subsequent to the
5043eb6f0de0SAdrian Chadd  * addba request should be tagged as aggregate and queued as non-aggregate
5044eb6f0de0SAdrian Chadd  * frames; thus updating the BAW. For now though, I'll just slide the
5045eb6f0de0SAdrian Chadd  * window.
5046eb6f0de0SAdrian Chadd  */
5047eb6f0de0SAdrian Chadd int
5048eb6f0de0SAdrian Chadd ath_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
5049eb6f0de0SAdrian Chadd     int status, int code, int batimeout)
5050eb6f0de0SAdrian Chadd {
5051eb6f0de0SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
50522aa563dfSAdrian Chadd 	int tid = tap->txa_tid;
5053eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
5054eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
5055eb6f0de0SAdrian Chadd 	int r;
5056eb6f0de0SAdrian Chadd 
5057eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5058eb6f0de0SAdrian Chadd 	    "%s: called; status=%d, code=%d, batimeout=%d\n", __func__,
5059eb6f0de0SAdrian Chadd 	    status, code, batimeout);
5060eb6f0de0SAdrian Chadd 
5061eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5062eb6f0de0SAdrian Chadd 	    "%s: txa_start=%d, ni_txseqs=%d\n",
5063eb6f0de0SAdrian Chadd 	    __func__, tap->txa_start, ni->ni_txseqs[tid]);
5064eb6f0de0SAdrian Chadd 
5065eb6f0de0SAdrian Chadd 	/*
5066eb6f0de0SAdrian Chadd 	 * Call this first, so the interface flags get updated
5067eb6f0de0SAdrian Chadd 	 * before the TID is unpaused. Otherwise a race condition
5068eb6f0de0SAdrian Chadd 	 * exists where the unpaused TID still doesn't yet have
5069eb6f0de0SAdrian Chadd 	 * IEEE80211_AGGR_RUNNING set.
5070eb6f0de0SAdrian Chadd 	 */
5071eb6f0de0SAdrian Chadd 	r = sc->sc_addba_response(ni, tap, status, code, batimeout);
5072eb6f0de0SAdrian Chadd 
5073eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
5074d3a6425bSAdrian Chadd 	atid->addba_tx_pending = 0;
5075eb6f0de0SAdrian Chadd 	/*
5076eb6f0de0SAdrian Chadd 	 * XXX dirty!
5077eb6f0de0SAdrian Chadd 	 * Slide the BAW left edge to wherever net80211 left it for us.
5078eb6f0de0SAdrian Chadd 	 * Read above for more information.
5079eb6f0de0SAdrian Chadd 	 */
5080eb6f0de0SAdrian Chadd 	tap->txa_start = ni->ni_txseqs[tid];
5081eb6f0de0SAdrian Chadd 	ath_tx_tid_resume(sc, atid);
5082eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
5083eb6f0de0SAdrian Chadd 	return r;
5084eb6f0de0SAdrian Chadd }
5085eb6f0de0SAdrian Chadd 
5086eb6f0de0SAdrian Chadd 
5087eb6f0de0SAdrian Chadd /*
5088eb6f0de0SAdrian Chadd  * Stop ADDBA on a queue.
50898405fe86SAdrian Chadd  *
50908405fe86SAdrian Chadd  * This can be called whilst BAR TX is currently active on the queue,
50918405fe86SAdrian Chadd  * so make sure this is unblocked before continuing.
5092eb6f0de0SAdrian Chadd  */
5093eb6f0de0SAdrian Chadd void
5094eb6f0de0SAdrian Chadd ath_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
5095eb6f0de0SAdrian Chadd {
5096eb6f0de0SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
50972aa563dfSAdrian Chadd 	int tid = tap->txa_tid;
5098eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
5099eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
5100eb6f0de0SAdrian Chadd 
5101eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: called\n", __func__);
5102eb6f0de0SAdrian Chadd 
51038405fe86SAdrian Chadd 	/*
51048405fe86SAdrian Chadd 	 * Pause TID traffic early, so there aren't any races
51058405fe86SAdrian Chadd 	 * Unblock the pending BAR held traffic, if it's currently paused.
51068405fe86SAdrian Chadd 	 */
510796ff26ffSAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
5108eb6f0de0SAdrian Chadd 	ath_tx_tid_pause(sc, atid);
51098405fe86SAdrian Chadd 	if (atid->bar_wait) {
51108405fe86SAdrian Chadd 		/*
51118405fe86SAdrian Chadd 		 * bar_unsuspend() expects bar_tx == 1, as it should be
51128405fe86SAdrian Chadd 		 * called from the TX completion path.  This quietens
51138405fe86SAdrian Chadd 		 * the warning.  It's cleared for us anyway.
51148405fe86SAdrian Chadd 		 */
51158405fe86SAdrian Chadd 		atid->bar_tx = 1;
51168405fe86SAdrian Chadd 		ath_tx_tid_bar_unsuspend(sc, atid);
51178405fe86SAdrian Chadd 	}
511896ff26ffSAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
5119eb6f0de0SAdrian Chadd 
5120eb6f0de0SAdrian Chadd 	/* There's no need to hold the TXQ lock here */
5121eb6f0de0SAdrian Chadd 	sc->sc_addba_stop(ni, tap);
5122eb6f0de0SAdrian Chadd 
5123eb6f0de0SAdrian Chadd 	/*
51244dfd4507SAdrian Chadd 	 * ath_tx_tid_cleanup will resume the TID if possible, otherwise
5125eb6f0de0SAdrian Chadd 	 * it'll set the cleanup flag, and it'll be unpaused once
5126eb6f0de0SAdrian Chadd 	 * things have been cleaned up.
5127eb6f0de0SAdrian Chadd 	 */
51284dfd4507SAdrian Chadd 	ath_tx_tid_cleanup(sc, an, tid);
5129eb6f0de0SAdrian Chadd }
5130eb6f0de0SAdrian Chadd 
5131eb6f0de0SAdrian Chadd /*
5132eb6f0de0SAdrian Chadd  * Note: net80211 bar_timeout() doesn't call this function on BAR failure;
5133eb6f0de0SAdrian Chadd  * it simply tears down the aggregation session. Ew.
5134eb6f0de0SAdrian Chadd  *
5135eb6f0de0SAdrian Chadd  * It however will call ieee80211_ampdu_stop() which will call
5136eb6f0de0SAdrian Chadd  * ic->ic_addba_stop().
5137eb6f0de0SAdrian Chadd  *
5138eb6f0de0SAdrian Chadd  * XXX This uses a hard-coded max BAR count value; the whole
5139eb6f0de0SAdrian Chadd  * XXX BAR TX success or failure should be better handled!
5140eb6f0de0SAdrian Chadd  */
5141eb6f0de0SAdrian Chadd void
5142eb6f0de0SAdrian Chadd ath_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
5143eb6f0de0SAdrian Chadd     int status)
5144eb6f0de0SAdrian Chadd {
5145eb6f0de0SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
51462aa563dfSAdrian Chadd 	int tid = tap->txa_tid;
5147eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
5148eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
5149eb6f0de0SAdrian Chadd 	int attempts = tap->txa_attempts;
5150eb6f0de0SAdrian Chadd 
51510e22ed0eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
5152e60c4fc2SAdrian Chadd 	    "%s: called; tap=%p, atid=%p, txa_tid=%d, atid->tid=%d, status=%d, attempts=%d\n",
51530e22ed0eSAdrian Chadd 	    __func__,
5154e60c4fc2SAdrian Chadd 	    tap,
5155e60c4fc2SAdrian Chadd 	    atid,
5156e60c4fc2SAdrian Chadd 	    tap->txa_tid,
5157e60c4fc2SAdrian Chadd 	    atid->tid,
51580e22ed0eSAdrian Chadd 	    status,
51590e22ed0eSAdrian Chadd 	    attempts);
5160eb6f0de0SAdrian Chadd 
5161eb6f0de0SAdrian Chadd 	/* Note: This may update the BAW details */
5162eb6f0de0SAdrian Chadd 	sc->sc_bar_response(ni, tap, status);
5163eb6f0de0SAdrian Chadd 
5164eb6f0de0SAdrian Chadd 	/* Unpause the TID */
5165eb6f0de0SAdrian Chadd 	/*
5166eb6f0de0SAdrian Chadd 	 * XXX if this is attempt=50, the TID will be downgraded
5167eb6f0de0SAdrian Chadd 	 * XXX to a non-aggregate session. So we must unpause the
5168eb6f0de0SAdrian Chadd 	 * XXX TID here or it'll never be done.
5169088d8b81SAdrian Chadd 	 *
5170088d8b81SAdrian Chadd 	 * Also, don't call it if bar_tx/bar_wait are 0; something
5171088d8b81SAdrian Chadd 	 * has beaten us to the punch? (XXX figure out what?)
5172eb6f0de0SAdrian Chadd 	 */
5173eb6f0de0SAdrian Chadd 	if (status == 0 || attempts == 50) {
5174eb6f0de0SAdrian Chadd 		ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
5175088d8b81SAdrian Chadd 		if (atid->bar_tx == 0 || atid->bar_wait == 0)
5176088d8b81SAdrian Chadd 			device_printf(sc->sc_dev,
5177088d8b81SAdrian Chadd 			    "%s: huh? bar_tx=%d, bar_wait=%d\n",
5178088d8b81SAdrian Chadd 			    __func__,
5179088d8b81SAdrian Chadd 			    atid->bar_tx, atid->bar_wait);
5180088d8b81SAdrian Chadd 		else
518188b3d483SAdrian Chadd 			ath_tx_tid_bar_unsuspend(sc, atid);
5182eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
5183eb6f0de0SAdrian Chadd 	}
5184eb6f0de0SAdrian Chadd }
5185eb6f0de0SAdrian Chadd 
5186eb6f0de0SAdrian Chadd /*
5187eb6f0de0SAdrian Chadd  * This is called whenever the pending ADDBA request times out.
5188eb6f0de0SAdrian Chadd  * Unpause and reschedule the TID.
5189eb6f0de0SAdrian Chadd  */
5190eb6f0de0SAdrian Chadd void
5191eb6f0de0SAdrian Chadd ath_addba_response_timeout(struct ieee80211_node *ni,
5192eb6f0de0SAdrian Chadd     struct ieee80211_tx_ampdu *tap)
5193eb6f0de0SAdrian Chadd {
5194eb6f0de0SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
51952aa563dfSAdrian Chadd 	int tid = tap->txa_tid;
5196eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
5197eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
5198eb6f0de0SAdrian Chadd 
5199eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5200eb6f0de0SAdrian Chadd 	    "%s: called; resuming\n", __func__);
5201eb6f0de0SAdrian Chadd 
5202d3a6425bSAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
5203d3a6425bSAdrian Chadd 	atid->addba_tx_pending = 0;
5204d3a6425bSAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
5205d3a6425bSAdrian Chadd 
5206eb6f0de0SAdrian Chadd 	/* Note: This updates the aggregate state to (again) pending */
5207eb6f0de0SAdrian Chadd 	sc->sc_addba_response_timeout(ni, tap);
5208eb6f0de0SAdrian Chadd 
5209eb6f0de0SAdrian Chadd 	/* Unpause the TID; which reschedules it */
5210eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
5211eb6f0de0SAdrian Chadd 	ath_tx_tid_resume(sc, atid);
5212eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
5213eb6f0de0SAdrian Chadd }
52143fdfc330SAdrian Chadd 
52153fdfc330SAdrian Chadd static int
52163fdfc330SAdrian Chadd ath_legacy_dma_txsetup(struct ath_softc *sc)
52173fdfc330SAdrian Chadd {
52183fdfc330SAdrian Chadd 
52193fdfc330SAdrian Chadd 	/* nothing new needed */
52203fdfc330SAdrian Chadd 	return (0);
52213fdfc330SAdrian Chadd }
52223fdfc330SAdrian Chadd 
52233fdfc330SAdrian Chadd static int
52243fdfc330SAdrian Chadd ath_legacy_dma_txteardown(struct ath_softc *sc)
52253fdfc330SAdrian Chadd {
52263fdfc330SAdrian Chadd 
52273fdfc330SAdrian Chadd 	/* nothing new needed */
52283fdfc330SAdrian Chadd 	return (0);
52293fdfc330SAdrian Chadd }
52303fdfc330SAdrian Chadd 
52313fdfc330SAdrian Chadd void
52323fdfc330SAdrian Chadd ath_xmit_setup_legacy(struct ath_softc *sc)
52333fdfc330SAdrian Chadd {
52341006fc0cSAdrian Chadd 	/*
52351006fc0cSAdrian Chadd 	 * For now, just set the descriptor length to sizeof(ath_desc);
52361006fc0cSAdrian Chadd 	 * worry about extracting the real length out of the HAL later.
52371006fc0cSAdrian Chadd 	 */
52381006fc0cSAdrian Chadd 	sc->sc_tx_desclen = sizeof(struct ath_desc);
52391006fc0cSAdrian Chadd 	sc->sc_tx_statuslen = 0;
52401006fc0cSAdrian Chadd 	sc->sc_tx_nmaps = 1;	/* only one buffer per TX desc */
52413fdfc330SAdrian Chadd 
52423fdfc330SAdrian Chadd 	sc->sc_tx.xmit_setup = ath_legacy_dma_txsetup;
52433fdfc330SAdrian Chadd 	sc->sc_tx.xmit_teardown = ath_legacy_dma_txteardown;
5244f8418db5SAdrian Chadd 	sc->sc_tx.xmit_attach_comp_func = ath_legacy_attach_comp_func;
5245746bab5bSAdrian Chadd 
5246746bab5bSAdrian Chadd 	sc->sc_tx.xmit_dma_restart = ath_legacy_tx_dma_restart;
5247746bab5bSAdrian Chadd 	sc->sc_tx.xmit_handoff = ath_legacy_xmit_handoff;
5248788e6aa9SAdrian Chadd 
5249788e6aa9SAdrian Chadd 	sc->sc_tx.xmit_drain = ath_legacy_tx_drain;
52503fdfc330SAdrian Chadd }
5251