xref: /freebsd/sys/dev/ath/if_ath_tx.c (revision bbdf3df1c4efc19a55fc1ae2f7c188d00520eb58)
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 
104b69b0dccSAdrian Chadd #ifdef	ATH_DEBUG_ALQ
105b69b0dccSAdrian Chadd #include <dev/ath/if_ath_alq.h>
106b69b0dccSAdrian Chadd #endif
107b69b0dccSAdrian Chadd 
10881a82688SAdrian Chadd /*
109eb6f0de0SAdrian Chadd  * How many retries to perform in software
110eb6f0de0SAdrian Chadd  */
111eb6f0de0SAdrian Chadd #define	SWMAX_RETRIES		10
112eb6f0de0SAdrian Chadd 
1137403d1b9SAdrian Chadd /*
1147403d1b9SAdrian Chadd  * What queue to throw the non-QoS TID traffic into
1157403d1b9SAdrian Chadd  */
1167403d1b9SAdrian Chadd #define	ATH_NONQOS_TID_AC	WME_AC_VO
1177403d1b9SAdrian Chadd 
1180eb81626SAdrian Chadd #if 0
1190eb81626SAdrian Chadd static int ath_tx_node_is_asleep(struct ath_softc *sc, struct ath_node *an);
1200eb81626SAdrian Chadd #endif
121eb6f0de0SAdrian Chadd static int ath_tx_ampdu_pending(struct ath_softc *sc, struct ath_node *an,
122eb6f0de0SAdrian Chadd     int tid);
123eb6f0de0SAdrian Chadd static int ath_tx_ampdu_running(struct ath_softc *sc, struct ath_node *an,
124eb6f0de0SAdrian Chadd     int tid);
125a108d2d6SAdrian Chadd static ieee80211_seq ath_tx_tid_seqno_assign(struct ath_softc *sc,
126a108d2d6SAdrian Chadd     struct ieee80211_node *ni, struct ath_buf *bf, struct mbuf *m0);
127eb6f0de0SAdrian Chadd static int ath_tx_action_frame_override_queue(struct ath_softc *sc,
128eb6f0de0SAdrian Chadd     struct ieee80211_node *ni, struct mbuf *m0, int *tid);
129f1bc738eSAdrian Chadd static struct ath_buf *
130f1bc738eSAdrian Chadd ath_tx_retry_clone(struct ath_softc *sc, struct ath_node *an,
131f1bc738eSAdrian Chadd     struct ath_tid *tid, struct ath_buf *bf);
132eb6f0de0SAdrian Chadd 
133eb6f0de0SAdrian Chadd /*
13481a82688SAdrian Chadd  * Whether to use the 11n rate scenario functions or not
13581a82688SAdrian Chadd  */
13681a82688SAdrian Chadd static inline int
13781a82688SAdrian Chadd ath_tx_is_11n(struct ath_softc *sc)
13881a82688SAdrian Chadd {
1394ddf2cc3SAdrian Chadd 	return ((sc->sc_ah->ah_magic == 0x20065416) ||
1404ddf2cc3SAdrian Chadd 		    (sc->sc_ah->ah_magic == 0x19741014));
14181a82688SAdrian Chadd }
14281a82688SAdrian Chadd 
143eb6f0de0SAdrian Chadd /*
144eb6f0de0SAdrian Chadd  * Obtain the current TID from the given frame.
145eb6f0de0SAdrian Chadd  *
146eb6f0de0SAdrian Chadd  * Non-QoS frames need to go into TID 16 (IEEE80211_NONQOS_TID.)
147eb6f0de0SAdrian Chadd  * This has implications for which AC/priority the packet is placed
148eb6f0de0SAdrian Chadd  * in.
149eb6f0de0SAdrian Chadd  */
150eb6f0de0SAdrian Chadd static int
151eb6f0de0SAdrian Chadd ath_tx_gettid(struct ath_softc *sc, const struct mbuf *m0)
152eb6f0de0SAdrian Chadd {
153eb6f0de0SAdrian Chadd 	const struct ieee80211_frame *wh;
154eb6f0de0SAdrian Chadd 	int pri = M_WME_GETAC(m0);
155eb6f0de0SAdrian Chadd 
156eb6f0de0SAdrian Chadd 	wh = mtod(m0, const struct ieee80211_frame *);
157eb6f0de0SAdrian Chadd 	if (! IEEE80211_QOS_HAS_SEQ(wh))
158eb6f0de0SAdrian Chadd 		return IEEE80211_NONQOS_TID;
159eb6f0de0SAdrian Chadd 	else
160eb6f0de0SAdrian Chadd 		return WME_AC_TO_TID(pri);
161eb6f0de0SAdrian Chadd }
162eb6f0de0SAdrian Chadd 
163f1bc738eSAdrian Chadd static void
164f1bc738eSAdrian Chadd ath_tx_set_retry(struct ath_softc *sc, struct ath_buf *bf)
165f1bc738eSAdrian Chadd {
166f1bc738eSAdrian Chadd 	struct ieee80211_frame *wh;
167f1bc738eSAdrian Chadd 
168f1bc738eSAdrian Chadd 	wh = mtod(bf->bf_m, struct ieee80211_frame *);
169f1bc738eSAdrian Chadd 	/* Only update/resync if needed */
170f1bc738eSAdrian Chadd 	if (bf->bf_state.bfs_isretried == 0) {
171f1bc738eSAdrian Chadd 		wh->i_fc[1] |= IEEE80211_FC1_RETRY;
172f1bc738eSAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
173f1bc738eSAdrian Chadd 		    BUS_DMASYNC_PREWRITE);
174f1bc738eSAdrian Chadd 	}
175f1bc738eSAdrian Chadd 	bf->bf_state.bfs_isretried = 1;
176f1bc738eSAdrian Chadd 	bf->bf_state.bfs_retries ++;
177f1bc738eSAdrian Chadd }
178f1bc738eSAdrian Chadd 
179eb6f0de0SAdrian Chadd /*
180eb6f0de0SAdrian Chadd  * Determine what the correct AC queue for the given frame
181eb6f0de0SAdrian Chadd  * should be.
182eb6f0de0SAdrian Chadd  *
183eb6f0de0SAdrian Chadd  * This code assumes that the TIDs map consistently to
184eb6f0de0SAdrian Chadd  * the underlying hardware (or software) ath_txq.
185eb6f0de0SAdrian Chadd  * Since the sender may try to set an AC which is
186eb6f0de0SAdrian Chadd  * arbitrary, non-QoS TIDs may end up being put on
187eb6f0de0SAdrian Chadd  * completely different ACs. There's no way to put a
188eb6f0de0SAdrian Chadd  * TID into multiple ath_txq's for scheduling, so
189eb6f0de0SAdrian Chadd  * for now we override the AC/TXQ selection and set
190eb6f0de0SAdrian Chadd  * non-QOS TID frames into the BE queue.
191eb6f0de0SAdrian Chadd  *
192eb6f0de0SAdrian Chadd  * This may be completely incorrect - specifically,
193eb6f0de0SAdrian Chadd  * some management frames may end up out of order
194eb6f0de0SAdrian Chadd  * compared to the QoS traffic they're controlling.
195eb6f0de0SAdrian Chadd  * I'll look into this later.
196eb6f0de0SAdrian Chadd  */
197eb6f0de0SAdrian Chadd static int
198eb6f0de0SAdrian Chadd ath_tx_getac(struct ath_softc *sc, const struct mbuf *m0)
199eb6f0de0SAdrian Chadd {
200eb6f0de0SAdrian Chadd 	const struct ieee80211_frame *wh;
201eb6f0de0SAdrian Chadd 	int pri = M_WME_GETAC(m0);
202eb6f0de0SAdrian Chadd 	wh = mtod(m0, const struct ieee80211_frame *);
203eb6f0de0SAdrian Chadd 	if (IEEE80211_QOS_HAS_SEQ(wh))
204eb6f0de0SAdrian Chadd 		return pri;
205eb6f0de0SAdrian Chadd 
2067403d1b9SAdrian Chadd 	return ATH_NONQOS_TID_AC;
207eb6f0de0SAdrian Chadd }
208eb6f0de0SAdrian Chadd 
209b8e788a5SAdrian Chadd void
210b8e788a5SAdrian Chadd ath_txfrag_cleanup(struct ath_softc *sc,
211b8e788a5SAdrian Chadd 	ath_bufhead *frags, struct ieee80211_node *ni)
212b8e788a5SAdrian Chadd {
213b8e788a5SAdrian Chadd 	struct ath_buf *bf, *next;
214b8e788a5SAdrian Chadd 
215b8e788a5SAdrian Chadd 	ATH_TXBUF_LOCK_ASSERT(sc);
216b8e788a5SAdrian Chadd 
2176b349e5aSAdrian Chadd 	TAILQ_FOREACH_SAFE(bf, frags, bf_list, next) {
218b8e788a5SAdrian Chadd 		/* NB: bf assumed clean */
2196b349e5aSAdrian Chadd 		TAILQ_REMOVE(frags, bf, bf_list);
220e1a50456SAdrian Chadd 		ath_returnbuf_head(sc, bf);
221b8e788a5SAdrian Chadd 		ieee80211_node_decref(ni);
222b8e788a5SAdrian Chadd 	}
223b8e788a5SAdrian Chadd }
224b8e788a5SAdrian Chadd 
225b8e788a5SAdrian Chadd /*
226b8e788a5SAdrian Chadd  * Setup xmit of a fragmented frame.  Allocate a buffer
227b8e788a5SAdrian Chadd  * for each frag and bump the node reference count to
228b8e788a5SAdrian Chadd  * reflect the held reference to be setup by ath_tx_start.
229b8e788a5SAdrian Chadd  */
230b8e788a5SAdrian Chadd int
231b8e788a5SAdrian Chadd ath_txfrag_setup(struct ath_softc *sc, ath_bufhead *frags,
232b8e788a5SAdrian Chadd 	struct mbuf *m0, struct ieee80211_node *ni)
233b8e788a5SAdrian Chadd {
234b8e788a5SAdrian Chadd 	struct mbuf *m;
235b8e788a5SAdrian Chadd 	struct ath_buf *bf;
236b8e788a5SAdrian Chadd 
237b8e788a5SAdrian Chadd 	ATH_TXBUF_LOCK(sc);
238b8e788a5SAdrian Chadd 	for (m = m0->m_nextpkt; m != NULL; m = m->m_nextpkt) {
239af33d486SAdrian Chadd 		/* XXX non-management? */
240af33d486SAdrian Chadd 		bf = _ath_getbuf_locked(sc, ATH_BUFTYPE_NORMAL);
241b8e788a5SAdrian Chadd 		if (bf == NULL) {	/* out of buffers, cleanup */
242b43facbfSAdrian Chadd 			device_printf(sc->sc_dev, "%s: no buffer?\n",
243b43facbfSAdrian Chadd 			    __func__);
244b8e788a5SAdrian Chadd 			ath_txfrag_cleanup(sc, frags, ni);
245b8e788a5SAdrian Chadd 			break;
246b8e788a5SAdrian Chadd 		}
247b8e788a5SAdrian Chadd 		ieee80211_node_incref(ni);
2486b349e5aSAdrian Chadd 		TAILQ_INSERT_TAIL(frags, bf, bf_list);
249b8e788a5SAdrian Chadd 	}
250b8e788a5SAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
251b8e788a5SAdrian Chadd 
2526b349e5aSAdrian Chadd 	return !TAILQ_EMPTY(frags);
253b8e788a5SAdrian Chadd }
254b8e788a5SAdrian Chadd 
255b8e788a5SAdrian Chadd /*
256b8e788a5SAdrian Chadd  * Reclaim mbuf resources.  For fragmented frames we
257b8e788a5SAdrian Chadd  * need to claim each frag chained with m_nextpkt.
258b8e788a5SAdrian Chadd  */
259b8e788a5SAdrian Chadd void
260b8e788a5SAdrian Chadd ath_freetx(struct mbuf *m)
261b8e788a5SAdrian Chadd {
262b8e788a5SAdrian Chadd 	struct mbuf *next;
263b8e788a5SAdrian Chadd 
264b8e788a5SAdrian Chadd 	do {
265b8e788a5SAdrian Chadd 		next = m->m_nextpkt;
266b8e788a5SAdrian Chadd 		m->m_nextpkt = NULL;
267b8e788a5SAdrian Chadd 		m_freem(m);
268b8e788a5SAdrian Chadd 	} while ((m = next) != NULL);
269b8e788a5SAdrian Chadd }
270b8e788a5SAdrian Chadd 
271b8e788a5SAdrian Chadd static int
272b8e788a5SAdrian Chadd ath_tx_dmasetup(struct ath_softc *sc, struct ath_buf *bf, struct mbuf *m0)
273b8e788a5SAdrian Chadd {
274b8e788a5SAdrian Chadd 	struct mbuf *m;
275b8e788a5SAdrian Chadd 	int error;
276b8e788a5SAdrian Chadd 
277b8e788a5SAdrian Chadd 	/*
278b8e788a5SAdrian Chadd 	 * Load the DMA map so any coalescing is done.  This
279b8e788a5SAdrian Chadd 	 * also calculates the number of descriptors we need.
280b8e788a5SAdrian Chadd 	 */
281b8e788a5SAdrian Chadd 	error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0,
282b8e788a5SAdrian Chadd 				     bf->bf_segs, &bf->bf_nseg,
283b8e788a5SAdrian Chadd 				     BUS_DMA_NOWAIT);
284b8e788a5SAdrian Chadd 	if (error == EFBIG) {
285b8e788a5SAdrian Chadd 		/* XXX packet requires too many descriptors */
286b8e788a5SAdrian Chadd 		bf->bf_nseg = ATH_TXDESC+1;
287b8e788a5SAdrian Chadd 	} else if (error != 0) {
288b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_busdma++;
289b8e788a5SAdrian Chadd 		ath_freetx(m0);
290b8e788a5SAdrian Chadd 		return error;
291b8e788a5SAdrian Chadd 	}
292b8e788a5SAdrian Chadd 	/*
293b8e788a5SAdrian Chadd 	 * Discard null packets and check for packets that
294b8e788a5SAdrian Chadd 	 * require too many TX descriptors.  We try to convert
295b8e788a5SAdrian Chadd 	 * the latter to a cluster.
296b8e788a5SAdrian Chadd 	 */
297b8e788a5SAdrian Chadd 	if (bf->bf_nseg > ATH_TXDESC) {		/* too many desc's, linearize */
298b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_linear++;
299b8e788a5SAdrian Chadd 		m = m_collapse(m0, M_DONTWAIT, ATH_TXDESC);
300b8e788a5SAdrian Chadd 		if (m == NULL) {
301b8e788a5SAdrian Chadd 			ath_freetx(m0);
302b8e788a5SAdrian Chadd 			sc->sc_stats.ast_tx_nombuf++;
303b8e788a5SAdrian Chadd 			return ENOMEM;
304b8e788a5SAdrian Chadd 		}
305b8e788a5SAdrian Chadd 		m0 = m;
306b8e788a5SAdrian Chadd 		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0,
307b8e788a5SAdrian Chadd 					     bf->bf_segs, &bf->bf_nseg,
308b8e788a5SAdrian Chadd 					     BUS_DMA_NOWAIT);
309b8e788a5SAdrian Chadd 		if (error != 0) {
310b8e788a5SAdrian Chadd 			sc->sc_stats.ast_tx_busdma++;
311b8e788a5SAdrian Chadd 			ath_freetx(m0);
312b8e788a5SAdrian Chadd 			return error;
313b8e788a5SAdrian Chadd 		}
314b8e788a5SAdrian Chadd 		KASSERT(bf->bf_nseg <= ATH_TXDESC,
315b8e788a5SAdrian Chadd 		    ("too many segments after defrag; nseg %u", bf->bf_nseg));
316b8e788a5SAdrian Chadd 	} else if (bf->bf_nseg == 0) {		/* null packet, discard */
317b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_nodata++;
318b8e788a5SAdrian Chadd 		ath_freetx(m0);
319b8e788a5SAdrian Chadd 		return EIO;
320b8e788a5SAdrian Chadd 	}
321b8e788a5SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_XMIT, "%s: m %p len %u\n",
322b8e788a5SAdrian Chadd 		__func__, m0, m0->m_pkthdr.len);
323b8e788a5SAdrian Chadd 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
324b8e788a5SAdrian Chadd 	bf->bf_m = m0;
325b8e788a5SAdrian Chadd 
326b8e788a5SAdrian Chadd 	return 0;
327b8e788a5SAdrian Chadd }
328b8e788a5SAdrian Chadd 
3296edf1dc7SAdrian Chadd /*
3306e84772fSAdrian Chadd  * Chain together segments+descriptors for a frame - 11n or otherwise.
3316e84772fSAdrian Chadd  *
3326e84772fSAdrian Chadd  * For aggregates, this is called on each frame in the aggregate.
3336edf1dc7SAdrian Chadd  */
334b8e788a5SAdrian Chadd static void
3356e84772fSAdrian Chadd ath_tx_chaindesclist(struct ath_softc *sc, struct ath_desc *ds0,
3366e84772fSAdrian Chadd     struct ath_buf *bf, int is_aggr, int is_first_subframe,
3376e84772fSAdrian Chadd     int is_last_subframe)
338b8e788a5SAdrian Chadd {
339b8e788a5SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
3406e84772fSAdrian Chadd 	char *ds;
3412b200bb4SAdrian Chadd 	int i, bp, dsp;
34246634305SAdrian Chadd 	HAL_DMA_ADDR bufAddrList[4];
34346634305SAdrian Chadd 	uint32_t segLenList[4];
3442b200bb4SAdrian Chadd 	int numTxMaps = 1;
345e2137b86SAdrian Chadd 	int isFirstDesc = 1;
34679b52356SAdrian Chadd 	int qnum;
34746634305SAdrian Chadd 
3483d9b1596SAdrian Chadd 	/*
3493d9b1596SAdrian Chadd 	 * XXX There's txdma and txdma_mgmt; the descriptor
3503d9b1596SAdrian Chadd 	 * sizes must match.
3513d9b1596SAdrian Chadd 	 */
3523d9b1596SAdrian Chadd 	struct ath_descdma *dd = &sc->sc_txdma;
353b8e788a5SAdrian Chadd 
354b8e788a5SAdrian Chadd 	/*
355b8e788a5SAdrian Chadd 	 * Fillin the remainder of the descriptor info.
356b8e788a5SAdrian Chadd 	 */
35746634305SAdrian Chadd 
3582b200bb4SAdrian Chadd 	/*
3592b200bb4SAdrian Chadd 	 * For now the HAL doesn't implement halNumTxMaps for non-EDMA
3602b200bb4SAdrian Chadd 	 * (ie it's 0.)  So just work around it.
3612b200bb4SAdrian Chadd 	 *
3622b200bb4SAdrian Chadd 	 * XXX TODO: populate halNumTxMaps for each HAL chip and
3632b200bb4SAdrian Chadd 	 * then undo this hack.
3642b200bb4SAdrian Chadd 	 */
3652b200bb4SAdrian Chadd 	if (sc->sc_ah->ah_magic == 0x19741014)
3662b200bb4SAdrian Chadd 		numTxMaps = 4;
3672b200bb4SAdrian Chadd 
3682b200bb4SAdrian Chadd 	/*
3692b200bb4SAdrian Chadd 	 * For EDMA and later chips ensure the TX map is fully populated
3702b200bb4SAdrian Chadd 	 * before advancing to the next descriptor.
3712b200bb4SAdrian Chadd 	 */
3726e84772fSAdrian Chadd 	ds = (char *) bf->bf_desc;
3732b200bb4SAdrian Chadd 	bp = dsp = 0;
3742b200bb4SAdrian Chadd 	bzero(bufAddrList, sizeof(bufAddrList));
3752b200bb4SAdrian Chadd 	bzero(segLenList, sizeof(segLenList));
3762b200bb4SAdrian Chadd 	for (i = 0; i < bf->bf_nseg; i++) {
3772b200bb4SAdrian Chadd 		bufAddrList[bp] = bf->bf_segs[i].ds_addr;
3782b200bb4SAdrian Chadd 		segLenList[bp] = bf->bf_segs[i].ds_len;
3792b200bb4SAdrian Chadd 		bp++;
3802b200bb4SAdrian Chadd 
3812b200bb4SAdrian Chadd 		/*
3822b200bb4SAdrian Chadd 		 * Go to the next segment if this isn't the last segment
3832b200bb4SAdrian Chadd 		 * and there's space in the current TX map.
3842b200bb4SAdrian Chadd 		 */
3852b200bb4SAdrian Chadd 		if ((i != bf->bf_nseg - 1) && (bp < numTxMaps))
3862b200bb4SAdrian Chadd 			continue;
3872b200bb4SAdrian Chadd 
3882b200bb4SAdrian Chadd 		/*
3892b200bb4SAdrian Chadd 		 * Last segment or we're out of buffer pointers.
3902b200bb4SAdrian Chadd 		 */
3912b200bb4SAdrian Chadd 		bp = 0;
39246634305SAdrian Chadd 
393b8e788a5SAdrian Chadd 		if (i == bf->bf_nseg - 1)
39442083b3dSAdrian Chadd 			ath_hal_settxdesclink(ah, (struct ath_desc *) ds, 0);
395b8e788a5SAdrian Chadd 		else
39642083b3dSAdrian Chadd 			ath_hal_settxdesclink(ah, (struct ath_desc *) ds,
3972b200bb4SAdrian Chadd 			    bf->bf_daddr + dd->dd_descsize * (dsp + 1));
39846634305SAdrian Chadd 
39946634305SAdrian Chadd 		/*
40046634305SAdrian Chadd 		 * XXX this assumes that bfs_txq is the actual destination
40146634305SAdrian Chadd 		 * hardware queue at this point.  It may not have been assigned,
40246634305SAdrian Chadd 		 * it may actually be pointing to the multicast software
40346634305SAdrian Chadd 		 * TXQ id.  These must be fixed!
40446634305SAdrian Chadd 		 */
40579b52356SAdrian Chadd 		qnum = bf->bf_state.bfs_txq->axq_qnum;
40679b52356SAdrian Chadd 
40742083b3dSAdrian Chadd 		ath_hal_filltxdesc(ah, (struct ath_desc *) ds
40846634305SAdrian Chadd 			, bufAddrList
40946634305SAdrian Chadd 			, segLenList
4102b200bb4SAdrian Chadd 			, bf->bf_descid		/* XXX desc id */
41179b52356SAdrian Chadd 			, qnum
412e2137b86SAdrian Chadd 			, isFirstDesc		/* first segment */
413b8e788a5SAdrian Chadd 			, i == bf->bf_nseg - 1	/* last segment */
41442083b3dSAdrian Chadd 			, (struct ath_desc *) ds0	/* first descriptor */
415b8e788a5SAdrian Chadd 		);
41621840808SAdrian Chadd 
4176e84772fSAdrian Chadd 		/*
4186e84772fSAdrian Chadd 		 * Make sure the 11n aggregate fields are cleared.
4196e84772fSAdrian Chadd 		 *
4206e84772fSAdrian Chadd 		 * XXX TODO: this doesn't need to be called for
4216e84772fSAdrian Chadd 		 * aggregate frames; as it'll be called on all
4226e84772fSAdrian Chadd 		 * sub-frames.  Since the descriptors are in
4236e84772fSAdrian Chadd 		 * non-cacheable memory, this leads to some
4246e84772fSAdrian Chadd 		 * rather slow writes on MIPS/ARM platforms.
4256e84772fSAdrian Chadd 		 */
42621840808SAdrian Chadd 		if (ath_tx_is_11n(sc))
4275d9b19f7SAdrian Chadd 			ath_hal_clr11n_aggr(sc->sc_ah, (struct ath_desc *) ds);
42821840808SAdrian Chadd 
4296e84772fSAdrian Chadd 		/*
4306e84772fSAdrian Chadd 		 * If 11n is enabled, set it up as if it's an aggregate
4316e84772fSAdrian Chadd 		 * frame.
4326e84772fSAdrian Chadd 		 */
4336e84772fSAdrian Chadd 		if (is_last_subframe) {
4346e84772fSAdrian Chadd 			ath_hal_set11n_aggr_last(sc->sc_ah,
4356e84772fSAdrian Chadd 			    (struct ath_desc *) ds);
4366e84772fSAdrian Chadd 		} else if (is_aggr) {
4376e84772fSAdrian Chadd 			/*
4386e84772fSAdrian Chadd 			 * This clears the aggrlen field; so
4396e84772fSAdrian Chadd 			 * the caller needs to call set_aggr_first()!
4406e84772fSAdrian Chadd 			 *
4416e84772fSAdrian Chadd 			 * XXX TODO: don't call this for the first
4426e84772fSAdrian Chadd 			 * descriptor in the first frame in an
4436e84772fSAdrian Chadd 			 * aggregate!
4446e84772fSAdrian Chadd 			 */
4456e84772fSAdrian Chadd 			ath_hal_set11n_aggr_middle(sc->sc_ah,
4466e84772fSAdrian Chadd 			    (struct ath_desc *) ds,
4476e84772fSAdrian Chadd 			    bf->bf_state.bfs_ndelim);
4486e84772fSAdrian Chadd 		}
449e2137b86SAdrian Chadd 		isFirstDesc = 0;
4500f8423a2SAdrian Chadd #ifdef	ATH_DEBUG
45142083b3dSAdrian Chadd 		if (sc->sc_debug & ATH_DEBUG_XMIT)
45242083b3dSAdrian Chadd 			ath_printtxbuf(sc, bf, qnum, 0, 0);
4530f8423a2SAdrian Chadd #endif
45442083b3dSAdrian Chadd 		bf->bf_lastds = (struct ath_desc *) ds;
4552b200bb4SAdrian Chadd 
4562b200bb4SAdrian Chadd 		/*
4572b200bb4SAdrian Chadd 		 * Don't forget to skip to the next descriptor.
4582b200bb4SAdrian Chadd 		 */
45942083b3dSAdrian Chadd 		ds += sc->sc_tx_desclen;
4602b200bb4SAdrian Chadd 		dsp++;
4612b200bb4SAdrian Chadd 
4622b200bb4SAdrian Chadd 		/*
4632b200bb4SAdrian Chadd 		 * .. and don't forget to blank these out!
4642b200bb4SAdrian Chadd 		 */
4652b200bb4SAdrian Chadd 		bzero(bufAddrList, sizeof(bufAddrList));
4662b200bb4SAdrian Chadd 		bzero(segLenList, sizeof(segLenList));
467b8e788a5SAdrian Chadd 	}
4684d7f8837SAdrian Chadd 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
46981a82688SAdrian Chadd }
47081a82688SAdrian Chadd 
471eb6f0de0SAdrian Chadd /*
472d34a7347SAdrian Chadd  * Set the rate control fields in the given descriptor based on
473d34a7347SAdrian Chadd  * the bf_state fields and node state.
474d34a7347SAdrian Chadd  *
475d34a7347SAdrian Chadd  * The bfs fields should already be set with the relevant rate
476d34a7347SAdrian Chadd  * control information, including whether MRR is to be enabled.
477d34a7347SAdrian Chadd  *
478d34a7347SAdrian Chadd  * Since the FreeBSD HAL currently sets up the first TX rate
479d34a7347SAdrian Chadd  * in ath_hal_setuptxdesc(), this will setup the MRR
480d34a7347SAdrian Chadd  * conditionally for the pre-11n chips, and call ath_buf_set_rate
481d34a7347SAdrian Chadd  * unconditionally for 11n chips. These require the 11n rate
482d34a7347SAdrian Chadd  * scenario to be set if MCS rates are enabled, so it's easier
483d34a7347SAdrian Chadd  * to just always call it. The caller can then only set rates 2, 3
484d34a7347SAdrian Chadd  * and 4 if multi-rate retry is needed.
485d34a7347SAdrian Chadd  */
486d34a7347SAdrian Chadd static void
487d34a7347SAdrian Chadd ath_tx_set_ratectrl(struct ath_softc *sc, struct ieee80211_node *ni,
488d34a7347SAdrian Chadd     struct ath_buf *bf)
489d34a7347SAdrian Chadd {
490d34a7347SAdrian Chadd 	struct ath_rc_series *rc = bf->bf_state.bfs_rc;
491d34a7347SAdrian Chadd 
492d34a7347SAdrian Chadd 	/* If mrr is disabled, blank tries 1, 2, 3 */
493d34a7347SAdrian Chadd 	if (! bf->bf_state.bfs_ismrr)
494d34a7347SAdrian Chadd 		rc[1].tries = rc[2].tries = rc[3].tries = 0;
495d34a7347SAdrian Chadd 
496d34a7347SAdrian Chadd 	/*
497d34a7347SAdrian Chadd 	 * Always call - that way a retried descriptor will
498d34a7347SAdrian Chadd 	 * have the MRR fields overwritten.
499d34a7347SAdrian Chadd 	 *
500d34a7347SAdrian Chadd 	 * XXX TODO: see if this is really needed - setting up
501d34a7347SAdrian Chadd 	 * the first descriptor should set the MRR fields to 0
502d34a7347SAdrian Chadd 	 * for us anyway.
503d34a7347SAdrian Chadd 	 */
504d34a7347SAdrian Chadd 	if (ath_tx_is_11n(sc)) {
505d34a7347SAdrian Chadd 		ath_buf_set_rate(sc, ni, bf);
506d34a7347SAdrian Chadd 	} else {
507d34a7347SAdrian Chadd 		ath_hal_setupxtxdesc(sc->sc_ah, bf->bf_desc
508d34a7347SAdrian Chadd 			, rc[1].ratecode, rc[1].tries
509d34a7347SAdrian Chadd 			, rc[2].ratecode, rc[2].tries
510d34a7347SAdrian Chadd 			, rc[3].ratecode, rc[3].tries
511d34a7347SAdrian Chadd 		);
512d34a7347SAdrian Chadd 	}
513d34a7347SAdrian Chadd }
514d34a7347SAdrian Chadd 
515d34a7347SAdrian Chadd /*
516eb6f0de0SAdrian Chadd  * Setup segments+descriptors for an 11n aggregate.
517eb6f0de0SAdrian Chadd  * bf_first is the first buffer in the aggregate.
518eb6f0de0SAdrian Chadd  * The descriptor list must already been linked together using
519eb6f0de0SAdrian Chadd  * bf->bf_next.
520eb6f0de0SAdrian Chadd  */
521eb6f0de0SAdrian Chadd static void
522eb6f0de0SAdrian Chadd ath_tx_setds_11n(struct ath_softc *sc, struct ath_buf *bf_first)
523eb6f0de0SAdrian Chadd {
524eb6f0de0SAdrian Chadd 	struct ath_buf *bf, *bf_prev = NULL;
5256e84772fSAdrian Chadd 	struct ath_desc *ds0 = bf_first->bf_desc;
526eb6f0de0SAdrian Chadd 
527eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: nframes=%d, al=%d\n",
528eb6f0de0SAdrian Chadd 	    __func__, bf_first->bf_state.bfs_nframes,
529eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_al);
530eb6f0de0SAdrian Chadd 
5317d9dd2acSAdrian Chadd 	bf = bf_first;
5327d9dd2acSAdrian Chadd 
5337d9dd2acSAdrian Chadd 	if (bf->bf_state.bfs_txrate0 == 0)
5347d9dd2acSAdrian Chadd 		device_printf(sc->sc_dev, "%s: bf=%p, txrate0=%d\n",
5357d9dd2acSAdrian Chadd 		    __func__, bf, 0);
5367d9dd2acSAdrian Chadd 	if (bf->bf_state.bfs_rc[0].ratecode == 0)
5377d9dd2acSAdrian Chadd 		device_printf(sc->sc_dev, "%s: bf=%p, rix0=%d\n",
5387d9dd2acSAdrian Chadd 		    __func__, bf, 0);
5397d9dd2acSAdrian Chadd 
540eb6f0de0SAdrian Chadd 	/*
5416e84772fSAdrian Chadd 	 * Setup all descriptors of all subframes - this will
5426e84772fSAdrian Chadd 	 * call ath_hal_set11naggrmiddle() on every frame.
543eb6f0de0SAdrian Chadd 	 */
544eb6f0de0SAdrian Chadd 	while (bf != NULL) {
545eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
546eb6f0de0SAdrian Chadd 		    "%s: bf=%p, nseg=%d, pktlen=%d, seqno=%d\n",
547eb6f0de0SAdrian Chadd 		    __func__, bf, bf->bf_nseg, bf->bf_state.bfs_pktlen,
548eb6f0de0SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
549eb6f0de0SAdrian Chadd 
5506e84772fSAdrian Chadd 		/*
5516e84772fSAdrian Chadd 		 * Setup the initial fields for the first descriptor - all
5526e84772fSAdrian Chadd 		 * the non-11n specific stuff.
5536e84772fSAdrian Chadd 		 */
5546e84772fSAdrian Chadd 		ath_hal_setuptxdesc(sc->sc_ah, bf->bf_desc
5556e84772fSAdrian Chadd 			, bf->bf_state.bfs_pktlen	/* packet length */
5566e84772fSAdrian Chadd 			, bf->bf_state.bfs_hdrlen	/* header length */
5576e84772fSAdrian Chadd 			, bf->bf_state.bfs_atype	/* Atheros packet type */
5586e84772fSAdrian Chadd 			, bf->bf_state.bfs_txpower	/* txpower */
5596e84772fSAdrian Chadd 			, bf->bf_state.bfs_txrate0
5606e84772fSAdrian Chadd 			, bf->bf_state.bfs_try0		/* series 0 rate/tries */
5616e84772fSAdrian Chadd 			, bf->bf_state.bfs_keyix	/* key cache index */
5626e84772fSAdrian Chadd 			, bf->bf_state.bfs_txantenna	/* antenna mode */
5636e84772fSAdrian Chadd 			, bf->bf_state.bfs_txflags | HAL_TXDESC_INTREQ	/* flags */
5646e84772fSAdrian Chadd 			, bf->bf_state.bfs_ctsrate	/* rts/cts rate */
5656e84772fSAdrian Chadd 			, bf->bf_state.bfs_ctsduration	/* rts/cts duration */
5666e84772fSAdrian Chadd 		);
5676e84772fSAdrian Chadd 
5686e84772fSAdrian Chadd 		/*
5696e84772fSAdrian Chadd 		 * First descriptor? Setup the rate control and initial
5706e84772fSAdrian Chadd 		 * aggregate header information.
5716e84772fSAdrian Chadd 		 */
5726e84772fSAdrian Chadd 		if (bf == bf_first) {
5736e84772fSAdrian Chadd 			/*
5746e84772fSAdrian Chadd 			 * setup first desc with rate and aggr info
5756e84772fSAdrian Chadd 			 */
5766e84772fSAdrian Chadd 			ath_tx_set_ratectrl(sc, bf->bf_node, bf);
5776e84772fSAdrian Chadd 		}
5786e84772fSAdrian Chadd 
5796e84772fSAdrian Chadd 		/*
5806e84772fSAdrian Chadd 		 * Setup the descriptors for a multi-descriptor frame.
5816e84772fSAdrian Chadd 		 * This is both aggregate and non-aggregate aware.
5826e84772fSAdrian Chadd 		 */
5836e84772fSAdrian Chadd 		ath_tx_chaindesclist(sc, ds0, bf,
5846e84772fSAdrian Chadd 		    1, /* is_aggr */
5856e84772fSAdrian Chadd 		    !! (bf == bf_first), /* is_first_subframe */
5866e84772fSAdrian Chadd 		    !! (bf->bf_next == NULL) /* is_last_subframe */
5876e84772fSAdrian Chadd 		    );
5886e84772fSAdrian Chadd 
5896e84772fSAdrian Chadd 		if (bf == bf_first) {
5906e84772fSAdrian Chadd 			/*
5916e84772fSAdrian Chadd 			 * Initialise the first 11n aggregate with the
5926e84772fSAdrian Chadd 			 * aggregate length and aggregate enable bits.
5936e84772fSAdrian Chadd 			 */
5946e84772fSAdrian Chadd 			ath_hal_set11n_aggr_first(sc->sc_ah,
5956e84772fSAdrian Chadd 			    ds0,
5966e84772fSAdrian Chadd 			    bf->bf_state.bfs_al,
5976e84772fSAdrian Chadd 			    bf->bf_state.bfs_ndelim);
5986e84772fSAdrian Chadd 		}
599eb6f0de0SAdrian Chadd 
600eb6f0de0SAdrian Chadd 		/*
601eb6f0de0SAdrian Chadd 		 * Link the last descriptor of the previous frame
602eb6f0de0SAdrian Chadd 		 * to the beginning descriptor of this frame.
603eb6f0de0SAdrian Chadd 		 */
604eb6f0de0SAdrian Chadd 		if (bf_prev != NULL)
605bb069955SAdrian Chadd 			ath_hal_settxdesclink(sc->sc_ah, bf_prev->bf_lastds,
606bb069955SAdrian Chadd 			    bf->bf_daddr);
607eb6f0de0SAdrian Chadd 
608eb6f0de0SAdrian Chadd 		/* Save a copy so we can link the next descriptor in */
609eb6f0de0SAdrian Chadd 		bf_prev = bf;
610eb6f0de0SAdrian Chadd 		bf = bf->bf_next;
611eb6f0de0SAdrian Chadd 	}
612eb6f0de0SAdrian Chadd 
613eb6f0de0SAdrian Chadd 	/*
614eb6f0de0SAdrian Chadd 	 * Set the first descriptor bf_lastds field to point to
615eb6f0de0SAdrian Chadd 	 * the last descriptor in the last subframe, that's where
616eb6f0de0SAdrian Chadd 	 * the status update will occur.
617eb6f0de0SAdrian Chadd 	 */
618eb6f0de0SAdrian Chadd 	bf_first->bf_lastds = bf_prev->bf_lastds;
619eb6f0de0SAdrian Chadd 
620eb6f0de0SAdrian Chadd 	/*
621eb6f0de0SAdrian Chadd 	 * And bf_last in the first descriptor points to the end of
622eb6f0de0SAdrian Chadd 	 * the aggregate list.
623eb6f0de0SAdrian Chadd 	 */
624eb6f0de0SAdrian Chadd 	bf_first->bf_last = bf_prev;
625eb6f0de0SAdrian Chadd 
626*bbdf3df1SAdrian Chadd 	/*
627*bbdf3df1SAdrian Chadd 	 * For non-AR9300 NICs, which require the rate control
628*bbdf3df1SAdrian Chadd 	 * in the final descriptor - let's set that up now.
629*bbdf3df1SAdrian Chadd 	 *
630*bbdf3df1SAdrian Chadd 	 * This is because the filltxdesc() HAL call doesn't
631*bbdf3df1SAdrian Chadd 	 * populate the last segment with rate control information
632*bbdf3df1SAdrian Chadd 	 * if firstSeg is also true.  For non-aggregate frames
633*bbdf3df1SAdrian Chadd 	 * that is fine, as the first frame already has rate control
634*bbdf3df1SAdrian Chadd 	 * info.  But if the last frame in an aggregate has one
635*bbdf3df1SAdrian Chadd 	 * descriptor, both firstseg and lastseg will be true and
636*bbdf3df1SAdrian Chadd 	 * the rate info isn't copied.
637*bbdf3df1SAdrian Chadd 	 *
638*bbdf3df1SAdrian Chadd 	 * This is inefficient on MIPS/ARM platforms that have
639*bbdf3df1SAdrian Chadd 	 * non-cachable memory for TX descriptors, but we'll just
640*bbdf3df1SAdrian Chadd 	 * make do for now.
641*bbdf3df1SAdrian Chadd 	 *
642*bbdf3df1SAdrian Chadd 	 * As to why the rate table is stashed in the last descriptor
643*bbdf3df1SAdrian Chadd 	 * rather than the first descriptor?  Because proctxdesc()
644*bbdf3df1SAdrian Chadd 	 * is called on the final descriptor in an MPDU or A-MPDU -
645*bbdf3df1SAdrian Chadd 	 * ie, the one that gets updated by the hardware upon
646*bbdf3df1SAdrian Chadd 	 * completion.  That way proctxdesc() doesn't need to know
647*bbdf3df1SAdrian Chadd 	 * about the first _and_ last TX descriptor.
648*bbdf3df1SAdrian Chadd 	 */
649*bbdf3df1SAdrian Chadd 	ath_hal_setuplasttxdesc(sc->sc_ah, bf_prev->bf_lastds, ds0);
650*bbdf3df1SAdrian Chadd 
651eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: end\n", __func__);
652eb6f0de0SAdrian Chadd }
653eb6f0de0SAdrian Chadd 
65446634305SAdrian Chadd /*
65546634305SAdrian Chadd  * Hand-off a frame to the multicast TX queue.
65646634305SAdrian Chadd  *
65746634305SAdrian Chadd  * This is a software TXQ which will be appended to the CAB queue
65846634305SAdrian Chadd  * during the beacon setup code.
65946634305SAdrian Chadd  *
66046634305SAdrian Chadd  * XXX TODO: since the AR9300 EDMA TX queue support wants the QCU ID
66146634305SAdrian Chadd  * as part of the TX descriptor, bf_state.bfs_txq must be updated
66246634305SAdrian Chadd  * with the actual hardware txq, or all of this will fall apart.
66346634305SAdrian Chadd  *
66446634305SAdrian Chadd  * XXX It may not be a bad idea to just stuff the QCU ID into bf_state
66546634305SAdrian Chadd  * and retire bfs_txq; then make sure the CABQ QCU ID is populated
66646634305SAdrian Chadd  * correctly.
66746634305SAdrian Chadd  */
668eb6f0de0SAdrian Chadd static void
669eb6f0de0SAdrian Chadd ath_tx_handoff_mcast(struct ath_softc *sc, struct ath_txq *txq,
670eb6f0de0SAdrian Chadd     struct ath_buf *bf)
671eb6f0de0SAdrian Chadd {
672eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
673eb6f0de0SAdrian Chadd 	KASSERT((bf->bf_flags & ATH_BUF_BUSY) == 0,
674eb6f0de0SAdrian Chadd 	     ("%s: busy status 0x%x", __func__, bf->bf_flags));
675eb6f0de0SAdrian Chadd 	if (txq->axq_link != NULL) {
676eb6f0de0SAdrian Chadd 		struct ath_buf *last = ATH_TXQ_LAST(txq, axq_q_s);
677eb6f0de0SAdrian Chadd 		struct ieee80211_frame *wh;
678eb6f0de0SAdrian Chadd 
679eb6f0de0SAdrian Chadd 		/* mark previous frame */
680eb6f0de0SAdrian Chadd 		wh = mtod(last->bf_m, struct ieee80211_frame *);
681eb6f0de0SAdrian Chadd 		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
682eb6f0de0SAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, last->bf_dmamap,
683eb6f0de0SAdrian Chadd 		    BUS_DMASYNC_PREWRITE);
684eb6f0de0SAdrian Chadd 
685eb6f0de0SAdrian Chadd 		/* link descriptor */
686eb6f0de0SAdrian Chadd 		*txq->axq_link = bf->bf_daddr;
687eb6f0de0SAdrian Chadd 	}
688eb6f0de0SAdrian Chadd 	ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
689bb069955SAdrian Chadd 	ath_hal_gettxdesclinkptr(sc->sc_ah, bf->bf_lastds, &txq->axq_link);
690eb6f0de0SAdrian Chadd }
691eb6f0de0SAdrian Chadd 
692eb6f0de0SAdrian Chadd /*
693eb6f0de0SAdrian Chadd  * Hand-off packet to a hardware queue.
694eb6f0de0SAdrian Chadd  */
695eb6f0de0SAdrian Chadd static void
696d4365d16SAdrian Chadd ath_tx_handoff_hw(struct ath_softc *sc, struct ath_txq *txq,
697d4365d16SAdrian Chadd     struct ath_buf *bf)
698eb6f0de0SAdrian Chadd {
699eb6f0de0SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
70081a82688SAdrian Chadd 
701b8e788a5SAdrian Chadd 	/*
702b8e788a5SAdrian Chadd 	 * Insert the frame on the outbound list and pass it on
703b8e788a5SAdrian Chadd 	 * to the hardware.  Multicast frames buffered for power
704b8e788a5SAdrian Chadd 	 * save stations and transmit from the CAB queue are stored
705b8e788a5SAdrian Chadd 	 * on a s/w only queue and loaded on to the CAB queue in
706b8e788a5SAdrian Chadd 	 * the SWBA handler since frames only go out on DTIM and
707b8e788a5SAdrian Chadd 	 * to avoid possible races.
708b8e788a5SAdrian Chadd 	 */
709eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
710b8e788a5SAdrian Chadd 	KASSERT((bf->bf_flags & ATH_BUF_BUSY) == 0,
711eb6f0de0SAdrian Chadd 	     ("%s: busy status 0x%x", __func__, bf->bf_flags));
712eb6f0de0SAdrian Chadd 	KASSERT(txq->axq_qnum != ATH_TXQ_SWQ,
713eb6f0de0SAdrian Chadd 	     ("ath_tx_handoff_hw called for mcast queue"));
714eb6f0de0SAdrian Chadd 
715ef27340cSAdrian Chadd #if 0
716ef27340cSAdrian Chadd 	/*
717ef27340cSAdrian Chadd 	 * This causes a LOR. Find out where the PCU lock is being
718ef27340cSAdrian Chadd 	 * held whilst the TXQ lock is grabbed - that shouldn't
719ef27340cSAdrian Chadd 	 * be occuring.
720ef27340cSAdrian Chadd 	 */
721ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
722ef27340cSAdrian Chadd 	if (sc->sc_inreset_cnt) {
723ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
724ef27340cSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_RESET,
725ef27340cSAdrian Chadd 		    "%s: called with sc_in_reset != 0\n",
726ef27340cSAdrian Chadd 		    __func__);
727ef27340cSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_XMIT,
728ef27340cSAdrian Chadd 		    "%s: queued: TXDP[%u] = %p (%p) depth %d\n",
729ef27340cSAdrian Chadd 		    __func__, txq->axq_qnum,
730ef27340cSAdrian Chadd 		    (caddr_t)bf->bf_daddr, bf->bf_desc,
731ef27340cSAdrian Chadd 		    txq->axq_depth);
732ef27340cSAdrian Chadd 		ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
733ef27340cSAdrian Chadd 		if (bf->bf_state.bfs_aggr)
734ef27340cSAdrian Chadd 			txq->axq_aggr_depth++;
735ef27340cSAdrian Chadd 		/*
736ef27340cSAdrian Chadd 		 * There's no need to update axq_link; the hardware
737ef27340cSAdrian Chadd 		 * is in reset and once the reset is complete, any
738ef27340cSAdrian Chadd 		 * non-empty queues will simply have DMA restarted.
739ef27340cSAdrian Chadd 		 */
740ef27340cSAdrian Chadd 		return;
741ef27340cSAdrian Chadd 		}
742ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
743ef27340cSAdrian Chadd #endif
744ef27340cSAdrian Chadd 
745eb6f0de0SAdrian Chadd 	/* For now, so not to generate whitespace diffs */
746eb6f0de0SAdrian Chadd 	if (1) {
747b8e788a5SAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
748b8e788a5SAdrian Chadd 		int qbusy;
749b8e788a5SAdrian Chadd 
750b8e788a5SAdrian Chadd 		ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
751b8e788a5SAdrian Chadd 		qbusy = ath_hal_txqenabled(ah, txq->axq_qnum);
75203682514SAdrian Chadd 
75303682514SAdrian Chadd 		ATH_KTR(sc, ATH_KTR_TX, 4,
75403682514SAdrian Chadd 		    "ath_tx_handoff: txq=%u, add bf=%p, qbusy=%d, depth=%d",
75503682514SAdrian Chadd 		    txq->axq_qnum, bf, qbusy, txq->axq_depth);
756b8e788a5SAdrian Chadd 		if (txq->axq_link == NULL) {
757b8e788a5SAdrian Chadd 			/*
758b8e788a5SAdrian Chadd 			 * Be careful writing the address to TXDP.  If
759b8e788a5SAdrian Chadd 			 * the tx q is enabled then this write will be
760b8e788a5SAdrian Chadd 			 * ignored.  Normally this is not an issue but
761b8e788a5SAdrian Chadd 			 * when tdma is in use and the q is beacon gated
762b8e788a5SAdrian Chadd 			 * this race can occur.  If the q is busy then
763b8e788a5SAdrian Chadd 			 * defer the work to later--either when another
764b8e788a5SAdrian Chadd 			 * packet comes along or when we prepare a beacon
765b8e788a5SAdrian Chadd 			 * frame at SWBA.
766b8e788a5SAdrian Chadd 			 */
767b8e788a5SAdrian Chadd 			if (!qbusy) {
768d4365d16SAdrian Chadd 				ath_hal_puttxbuf(ah, txq->axq_qnum,
769d4365d16SAdrian Chadd 				    bf->bf_daddr);
770b8e788a5SAdrian Chadd 				txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
771b8e788a5SAdrian Chadd 				DPRINTF(sc, ATH_DEBUG_XMIT,
77203682514SAdrian Chadd 				    "%s: TXDP[%u] = %p (%p) lastds=%p depth %d\n",
773b8e788a5SAdrian Chadd 				    __func__, txq->axq_qnum,
774b8e788a5SAdrian Chadd 				    (caddr_t)bf->bf_daddr, bf->bf_desc,
77503682514SAdrian Chadd 				    bf->bf_lastds,
77603682514SAdrian Chadd 				    txq->axq_depth);
77703682514SAdrian Chadd 				ATH_KTR(sc, ATH_KTR_TX, 5,
77803682514SAdrian Chadd 				    "ath_tx_handoff: TXDP[%u] = %p (%p) "
77903682514SAdrian Chadd 				    "lastds=%p depth %d",
78003682514SAdrian Chadd 				    txq->axq_qnum,
78103682514SAdrian Chadd 				    (caddr_t)bf->bf_daddr, bf->bf_desc,
78203682514SAdrian Chadd 				    bf->bf_lastds,
783b8e788a5SAdrian Chadd 				    txq->axq_depth);
784b8e788a5SAdrian Chadd 			} else {
785b8e788a5SAdrian Chadd 				txq->axq_flags |= ATH_TXQ_PUTPENDING;
786b8e788a5SAdrian Chadd 				DPRINTF(sc, ATH_DEBUG_TDMA | ATH_DEBUG_XMIT,
787b8e788a5SAdrian Chadd 				    "%s: Q%u busy, defer enable\n", __func__,
788b8e788a5SAdrian Chadd 				    txq->axq_qnum);
78903682514SAdrian Chadd 				ATH_KTR(sc, ATH_KTR_TX, 0, "defer enable");
790b8e788a5SAdrian Chadd 			}
791b8e788a5SAdrian Chadd 		} else {
792b8e788a5SAdrian Chadd 			*txq->axq_link = bf->bf_daddr;
793b8e788a5SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_XMIT,
794b8e788a5SAdrian Chadd 			    "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
795b8e788a5SAdrian Chadd 			    txq->axq_qnum, txq->axq_link,
796d4365d16SAdrian Chadd 			    (caddr_t)bf->bf_daddr, bf->bf_desc,
797d4365d16SAdrian Chadd 			    txq->axq_depth);
79803682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_TX, 5,
79903682514SAdrian Chadd 			    "ath_tx_handoff: link[%u](%p)=%p (%p) lastds=%p",
80003682514SAdrian Chadd 			    txq->axq_qnum, txq->axq_link,
80103682514SAdrian Chadd 			    (caddr_t)bf->bf_daddr, bf->bf_desc,
80203682514SAdrian Chadd 			    bf->bf_lastds);
80303682514SAdrian Chadd 
804b8e788a5SAdrian Chadd 			if ((txq->axq_flags & ATH_TXQ_PUTPENDING) && !qbusy) {
805b8e788a5SAdrian Chadd 				/*
806b8e788a5SAdrian Chadd 				 * The q was busy when we previously tried
807b8e788a5SAdrian Chadd 				 * to write the address of the first buffer
808b8e788a5SAdrian Chadd 				 * in the chain.  Since it's not busy now
809b8e788a5SAdrian Chadd 				 * handle this chore.  We are certain the
810b8e788a5SAdrian Chadd 				 * buffer at the front is the right one since
811b8e788a5SAdrian Chadd 				 * axq_link is NULL only when the buffer list
812b8e788a5SAdrian Chadd 				 * is/was empty.
813b8e788a5SAdrian Chadd 				 */
814b8e788a5SAdrian Chadd 				ath_hal_puttxbuf(ah, txq->axq_qnum,
8156b349e5aSAdrian Chadd 					TAILQ_FIRST(&txq->axq_q)->bf_daddr);
816b8e788a5SAdrian Chadd 				txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
817b8e788a5SAdrian Chadd 				DPRINTF(sc, ATH_DEBUG_TDMA | ATH_DEBUG_XMIT,
818b8e788a5SAdrian Chadd 				    "%s: Q%u restarted\n", __func__,
819b8e788a5SAdrian Chadd 				    txq->axq_qnum);
82003682514SAdrian Chadd 				ATH_KTR(sc, ATH_KTR_TX, 4,
82103682514SAdrian Chadd 				  "ath_tx_handoff: txq[%d] restarted, bf=%p "
82203682514SAdrian Chadd 				  "daddr=%p ds=%p",
82303682514SAdrian Chadd 				    txq->axq_qnum,
82403682514SAdrian Chadd 				    bf,
82503682514SAdrian Chadd 				    (caddr_t)bf->bf_daddr,
82603682514SAdrian Chadd 				    bf->bf_desc);
827b8e788a5SAdrian Chadd 			}
828b8e788a5SAdrian Chadd 		}
829b8e788a5SAdrian Chadd #else
830b8e788a5SAdrian Chadd 		ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
831b1dddc28SAdrian Chadd 		ATH_KTR(sc, ATH_KTR_TX, 3,
832b1dddc28SAdrian Chadd 		    "ath_tx_handoff: non-tdma: txq=%u, add bf=%p "
83303682514SAdrian Chadd 		    "depth=%d",
83403682514SAdrian Chadd 		    txq->axq_qnum,
83503682514SAdrian Chadd 		    bf,
83603682514SAdrian Chadd 		    txq->axq_depth);
837b8e788a5SAdrian Chadd 		if (txq->axq_link == NULL) {
838b8e788a5SAdrian Chadd 			ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
839b8e788a5SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_XMIT,
840b8e788a5SAdrian Chadd 			    "%s: TXDP[%u] = %p (%p) depth %d\n",
841b8e788a5SAdrian Chadd 			    __func__, txq->axq_qnum,
842b8e788a5SAdrian Chadd 			    (caddr_t)bf->bf_daddr, bf->bf_desc,
843b8e788a5SAdrian Chadd 			    txq->axq_depth);
84403682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_TX, 5,
84503682514SAdrian Chadd 			    "ath_tx_handoff: non-tdma: TXDP[%u] = %p (%p) "
84603682514SAdrian Chadd 			    "lastds=%p depth %d",
84703682514SAdrian Chadd 			    txq->axq_qnum,
84803682514SAdrian Chadd 			    (caddr_t)bf->bf_daddr, bf->bf_desc,
84903682514SAdrian Chadd 			    bf->bf_lastds,
85003682514SAdrian Chadd 			    txq->axq_depth);
85103682514SAdrian Chadd 
852b8e788a5SAdrian Chadd 		} else {
853b8e788a5SAdrian Chadd 			*txq->axq_link = bf->bf_daddr;
854b8e788a5SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_XMIT,
855b8e788a5SAdrian Chadd 			    "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
856b8e788a5SAdrian Chadd 			    txq->axq_qnum, txq->axq_link,
857d4365d16SAdrian Chadd 			    (caddr_t)bf->bf_daddr, bf->bf_desc,
858d4365d16SAdrian Chadd 			    txq->axq_depth);
85903682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_TX, 5,
86003682514SAdrian Chadd 			    "ath_tx_handoff: non-tdma: link[%u](%p)=%p (%p) "
86103682514SAdrian Chadd 			    "lastds=%d",
86203682514SAdrian Chadd 			    txq->axq_qnum, txq->axq_link,
86303682514SAdrian Chadd 			    (caddr_t)bf->bf_daddr, bf->bf_desc,
86403682514SAdrian Chadd 			    bf->bf_lastds);
86503682514SAdrian Chadd 
866b8e788a5SAdrian Chadd 		}
867b8e788a5SAdrian Chadd #endif /* IEEE80211_SUPPORT_TDMA */
8686edf1dc7SAdrian Chadd 		if (bf->bf_state.bfs_aggr)
8696edf1dc7SAdrian Chadd 			txq->axq_aggr_depth++;
870bb069955SAdrian Chadd 		ath_hal_gettxdesclinkptr(ah, bf->bf_lastds, &txq->axq_link);
871b8e788a5SAdrian Chadd 		ath_hal_txstart(ah, txq->axq_qnum);
87203682514SAdrian Chadd 		ATH_KTR(sc, ATH_KTR_TX, 1,
87303682514SAdrian Chadd 		    "ath_tx_handoff: txq=%u, txstart", txq->axq_qnum);
874b8e788a5SAdrian Chadd 	}
875b8e788a5SAdrian Chadd }
876eb6f0de0SAdrian Chadd 
877eb6f0de0SAdrian Chadd /*
878eb6f0de0SAdrian Chadd  * Restart TX DMA for the given TXQ.
879eb6f0de0SAdrian Chadd  *
880eb6f0de0SAdrian Chadd  * This must be called whether the queue is empty or not.
881eb6f0de0SAdrian Chadd  */
882746bab5bSAdrian Chadd static void
883746bab5bSAdrian Chadd ath_legacy_tx_dma_restart(struct ath_softc *sc, struct ath_txq *txq)
884eb6f0de0SAdrian Chadd {
885eb6f0de0SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
886b1f3262cSAdrian Chadd 	struct ath_buf *bf, *bf_last;
887eb6f0de0SAdrian Chadd 
888eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
889eb6f0de0SAdrian Chadd 
890eb6f0de0SAdrian Chadd 	/* This is always going to be cleared, empty or not */
891eb6f0de0SAdrian Chadd 	txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
892eb6f0de0SAdrian Chadd 
893b1f3262cSAdrian Chadd 	/* XXX make this ATH_TXQ_FIRST */
894eb6f0de0SAdrian Chadd 	bf = TAILQ_FIRST(&txq->axq_q);
895b1f3262cSAdrian Chadd 	bf_last = ATH_TXQ_LAST(txq, axq_q_s);
896b1f3262cSAdrian Chadd 
897eb6f0de0SAdrian Chadd 	if (bf == NULL)
898eb6f0de0SAdrian Chadd 		return;
899eb6f0de0SAdrian Chadd 
900eb6f0de0SAdrian Chadd 	ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
901d2da5544SAdrian Chadd 	ath_hal_gettxdesclinkptr(ah, bf_last->bf_lastds, &txq->axq_link);
902eb6f0de0SAdrian Chadd 	ath_hal_txstart(ah, txq->axq_qnum);
903eb6f0de0SAdrian Chadd }
904eb6f0de0SAdrian Chadd 
905eb6f0de0SAdrian Chadd /*
906eb6f0de0SAdrian Chadd  * Hand off a packet to the hardware (or mcast queue.)
907eb6f0de0SAdrian Chadd  *
908eb6f0de0SAdrian Chadd  * The relevant hardware txq should be locked.
909eb6f0de0SAdrian Chadd  */
910eb6f0de0SAdrian Chadd static void
911746bab5bSAdrian Chadd ath_legacy_xmit_handoff(struct ath_softc *sc, struct ath_txq *txq,
912746bab5bSAdrian Chadd     struct ath_buf *bf)
913eb6f0de0SAdrian Chadd {
914eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
915eb6f0de0SAdrian Chadd 
916eb6f0de0SAdrian Chadd 	if (txq->axq_qnum == ATH_TXQ_SWQ)
917eb6f0de0SAdrian Chadd 		ath_tx_handoff_mcast(sc, txq, bf);
918eb6f0de0SAdrian Chadd 	else
919eb6f0de0SAdrian Chadd 		ath_tx_handoff_hw(sc, txq, bf);
920b8e788a5SAdrian Chadd }
921b8e788a5SAdrian Chadd 
92281a82688SAdrian Chadd static int
92381a82688SAdrian Chadd ath_tx_tag_crypto(struct ath_softc *sc, struct ieee80211_node *ni,
924d4365d16SAdrian Chadd     struct mbuf *m0, int iswep, int isfrag, int *hdrlen, int *pktlen,
925d4365d16SAdrian Chadd     int *keyix)
92681a82688SAdrian Chadd {
92712be5b9cSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_XMIT,
92812be5b9cSAdrian Chadd 	    "%s: hdrlen=%d, pktlen=%d, isfrag=%d, iswep=%d, m0=%p\n",
92912be5b9cSAdrian Chadd 	    __func__,
93012be5b9cSAdrian Chadd 	    *hdrlen,
93112be5b9cSAdrian Chadd 	    *pktlen,
93212be5b9cSAdrian Chadd 	    isfrag,
93312be5b9cSAdrian Chadd 	    iswep,
93412be5b9cSAdrian Chadd 	    m0);
93512be5b9cSAdrian Chadd 
93681a82688SAdrian Chadd 	if (iswep) {
93781a82688SAdrian Chadd 		const struct ieee80211_cipher *cip;
93881a82688SAdrian Chadd 		struct ieee80211_key *k;
93981a82688SAdrian Chadd 
94081a82688SAdrian Chadd 		/*
94181a82688SAdrian Chadd 		 * Construct the 802.11 header+trailer for an encrypted
94281a82688SAdrian Chadd 		 * frame. The only reason this can fail is because of an
94381a82688SAdrian Chadd 		 * unknown or unsupported cipher/key type.
94481a82688SAdrian Chadd 		 */
94581a82688SAdrian Chadd 		k = ieee80211_crypto_encap(ni, m0);
94681a82688SAdrian Chadd 		if (k == NULL) {
94781a82688SAdrian Chadd 			/*
94881a82688SAdrian Chadd 			 * This can happen when the key is yanked after the
94981a82688SAdrian Chadd 			 * frame was queued.  Just discard the frame; the
95081a82688SAdrian Chadd 			 * 802.11 layer counts failures and provides
95181a82688SAdrian Chadd 			 * debugging/diagnostics.
95281a82688SAdrian Chadd 			 */
953d4365d16SAdrian Chadd 			return (0);
95481a82688SAdrian Chadd 		}
95581a82688SAdrian Chadd 		/*
95681a82688SAdrian Chadd 		 * Adjust the packet + header lengths for the crypto
95781a82688SAdrian Chadd 		 * additions and calculate the h/w key index.  When
95881a82688SAdrian Chadd 		 * a s/w mic is done the frame will have had any mic
95981a82688SAdrian Chadd 		 * added to it prior to entry so m0->m_pkthdr.len will
96081a82688SAdrian Chadd 		 * account for it. Otherwise we need to add it to the
96181a82688SAdrian Chadd 		 * packet length.
96281a82688SAdrian Chadd 		 */
96381a82688SAdrian Chadd 		cip = k->wk_cipher;
96481a82688SAdrian Chadd 		(*hdrlen) += cip->ic_header;
96581a82688SAdrian Chadd 		(*pktlen) += cip->ic_header + cip->ic_trailer;
96681a82688SAdrian Chadd 		/* NB: frags always have any TKIP MIC done in s/w */
96781a82688SAdrian Chadd 		if ((k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && !isfrag)
96881a82688SAdrian Chadd 			(*pktlen) += cip->ic_miclen;
96981a82688SAdrian Chadd 		(*keyix) = k->wk_keyix;
97081a82688SAdrian Chadd 	} else if (ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) {
97181a82688SAdrian Chadd 		/*
97281a82688SAdrian Chadd 		 * Use station key cache slot, if assigned.
97381a82688SAdrian Chadd 		 */
97481a82688SAdrian Chadd 		(*keyix) = ni->ni_ucastkey.wk_keyix;
97581a82688SAdrian Chadd 		if ((*keyix) == IEEE80211_KEYIX_NONE)
97681a82688SAdrian Chadd 			(*keyix) = HAL_TXKEYIX_INVALID;
97781a82688SAdrian Chadd 	} else
97881a82688SAdrian Chadd 		(*keyix) = HAL_TXKEYIX_INVALID;
97981a82688SAdrian Chadd 
980d4365d16SAdrian Chadd 	return (1);
98181a82688SAdrian Chadd }
98281a82688SAdrian Chadd 
983e2e4a2c2SAdrian Chadd /*
984e2e4a2c2SAdrian Chadd  * Calculate whether interoperability protection is required for
985e2e4a2c2SAdrian Chadd  * this frame.
986e2e4a2c2SAdrian Chadd  *
987e2e4a2c2SAdrian Chadd  * This requires the rate control information be filled in,
988e2e4a2c2SAdrian Chadd  * as the protection requirement depends upon the current
989e2e4a2c2SAdrian Chadd  * operating mode / PHY.
990e2e4a2c2SAdrian Chadd  */
991e2e4a2c2SAdrian Chadd static void
992e2e4a2c2SAdrian Chadd ath_tx_calc_protection(struct ath_softc *sc, struct ath_buf *bf)
993e2e4a2c2SAdrian Chadd {
994e2e4a2c2SAdrian Chadd 	struct ieee80211_frame *wh;
995e2e4a2c2SAdrian Chadd 	uint8_t rix;
996e2e4a2c2SAdrian Chadd 	uint16_t flags;
997e2e4a2c2SAdrian Chadd 	int shortPreamble;
998e2e4a2c2SAdrian Chadd 	const HAL_RATE_TABLE *rt = sc->sc_currates;
999e2e4a2c2SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
1000e2e4a2c2SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
1001e2e4a2c2SAdrian Chadd 
1002e2e4a2c2SAdrian Chadd 	flags = bf->bf_state.bfs_txflags;
1003e2e4a2c2SAdrian Chadd 	rix = bf->bf_state.bfs_rc[0].rix;
1004e2e4a2c2SAdrian Chadd 	shortPreamble = bf->bf_state.bfs_shpream;
1005e2e4a2c2SAdrian Chadd 	wh = mtod(bf->bf_m, struct ieee80211_frame *);
1006e2e4a2c2SAdrian Chadd 
1007e2e4a2c2SAdrian Chadd 	/*
1008e2e4a2c2SAdrian Chadd 	 * If 802.11g protection is enabled, determine whether
1009e2e4a2c2SAdrian Chadd 	 * to use RTS/CTS or just CTS.  Note that this is only
1010e2e4a2c2SAdrian Chadd 	 * done for OFDM unicast frames.
1011e2e4a2c2SAdrian Chadd 	 */
1012e2e4a2c2SAdrian Chadd 	if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1013e2e4a2c2SAdrian Chadd 	    rt->info[rix].phy == IEEE80211_T_OFDM &&
1014e2e4a2c2SAdrian Chadd 	    (flags & HAL_TXDESC_NOACK) == 0) {
1015e2e4a2c2SAdrian Chadd 		bf->bf_state.bfs_doprot = 1;
1016e2e4a2c2SAdrian Chadd 		/* XXX fragments must use CCK rates w/ protection */
1017e2e4a2c2SAdrian Chadd 		if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) {
1018e2e4a2c2SAdrian Chadd 			flags |= HAL_TXDESC_RTSENA;
1019e2e4a2c2SAdrian Chadd 		} else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) {
1020e2e4a2c2SAdrian Chadd 			flags |= HAL_TXDESC_CTSENA;
1021e2e4a2c2SAdrian Chadd 		}
1022e2e4a2c2SAdrian Chadd 		/*
1023e2e4a2c2SAdrian Chadd 		 * For frags it would be desirable to use the
1024e2e4a2c2SAdrian Chadd 		 * highest CCK rate for RTS/CTS.  But stations
1025e2e4a2c2SAdrian Chadd 		 * farther away may detect it at a lower CCK rate
1026e2e4a2c2SAdrian Chadd 		 * so use the configured protection rate instead
1027e2e4a2c2SAdrian Chadd 		 * (for now).
1028e2e4a2c2SAdrian Chadd 		 */
1029e2e4a2c2SAdrian Chadd 		sc->sc_stats.ast_tx_protect++;
1030e2e4a2c2SAdrian Chadd 	}
1031e2e4a2c2SAdrian Chadd 
1032e2e4a2c2SAdrian Chadd 	/*
1033e2e4a2c2SAdrian Chadd 	 * If 11n protection is enabled and it's a HT frame,
1034e2e4a2c2SAdrian Chadd 	 * enable RTS.
1035e2e4a2c2SAdrian Chadd 	 *
1036e2e4a2c2SAdrian Chadd 	 * XXX ic_htprotmode or ic_curhtprotmode?
1037e2e4a2c2SAdrian Chadd 	 * XXX should it_htprotmode only matter if ic_curhtprotmode
1038e2e4a2c2SAdrian Chadd 	 * XXX indicates it's not a HT pure environment?
1039e2e4a2c2SAdrian Chadd 	 */
1040e2e4a2c2SAdrian Chadd 	if ((ic->ic_htprotmode == IEEE80211_PROT_RTSCTS) &&
1041e2e4a2c2SAdrian Chadd 	    rt->info[rix].phy == IEEE80211_T_HT &&
1042e2e4a2c2SAdrian Chadd 	    (flags & HAL_TXDESC_NOACK) == 0) {
1043e2e4a2c2SAdrian Chadd 		flags |= HAL_TXDESC_RTSENA;
1044e2e4a2c2SAdrian Chadd 		sc->sc_stats.ast_tx_htprotect++;
1045e2e4a2c2SAdrian Chadd 	}
1046e2e4a2c2SAdrian Chadd 	bf->bf_state.bfs_txflags = flags;
1047e2e4a2c2SAdrian Chadd }
1048e2e4a2c2SAdrian Chadd 
1049e2e4a2c2SAdrian Chadd /*
1050e2e4a2c2SAdrian Chadd  * Update the frame duration given the currently selected rate.
1051e2e4a2c2SAdrian Chadd  *
1052e2e4a2c2SAdrian Chadd  * This also updates the frame duration value, so it will require
1053e2e4a2c2SAdrian Chadd  * a DMA flush.
1054e2e4a2c2SAdrian Chadd  */
1055e2e4a2c2SAdrian Chadd static void
1056e2e4a2c2SAdrian Chadd ath_tx_calc_duration(struct ath_softc *sc, struct ath_buf *bf)
1057e2e4a2c2SAdrian Chadd {
1058e2e4a2c2SAdrian Chadd 	struct ieee80211_frame *wh;
1059e2e4a2c2SAdrian Chadd 	uint8_t rix;
1060e2e4a2c2SAdrian Chadd 	uint16_t flags;
1061e2e4a2c2SAdrian Chadd 	int shortPreamble;
1062e2e4a2c2SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
1063e2e4a2c2SAdrian Chadd 	const HAL_RATE_TABLE *rt = sc->sc_currates;
1064e2e4a2c2SAdrian Chadd 	int isfrag = bf->bf_m->m_flags & M_FRAG;
1065e2e4a2c2SAdrian Chadd 
1066e2e4a2c2SAdrian Chadd 	flags = bf->bf_state.bfs_txflags;
1067e2e4a2c2SAdrian Chadd 	rix = bf->bf_state.bfs_rc[0].rix;
1068e2e4a2c2SAdrian Chadd 	shortPreamble = bf->bf_state.bfs_shpream;
1069e2e4a2c2SAdrian Chadd 	wh = mtod(bf->bf_m, struct ieee80211_frame *);
1070e2e4a2c2SAdrian Chadd 
1071e2e4a2c2SAdrian Chadd 	/*
1072e2e4a2c2SAdrian Chadd 	 * Calculate duration.  This logically belongs in the 802.11
1073e2e4a2c2SAdrian Chadd 	 * layer but it lacks sufficient information to calculate it.
1074e2e4a2c2SAdrian Chadd 	 */
1075e2e4a2c2SAdrian Chadd 	if ((flags & HAL_TXDESC_NOACK) == 0 &&
1076e2e4a2c2SAdrian Chadd 	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL) {
1077e2e4a2c2SAdrian Chadd 		u_int16_t dur;
1078e2e4a2c2SAdrian Chadd 		if (shortPreamble)
1079e2e4a2c2SAdrian Chadd 			dur = rt->info[rix].spAckDuration;
1080e2e4a2c2SAdrian Chadd 		else
1081e2e4a2c2SAdrian Chadd 			dur = rt->info[rix].lpAckDuration;
1082e2e4a2c2SAdrian Chadd 		if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) {
1083e2e4a2c2SAdrian Chadd 			dur += dur;		/* additional SIFS+ACK */
1084e2e4a2c2SAdrian Chadd 			KASSERT(bf->bf_m->m_nextpkt != NULL, ("no fragment"));
1085e2e4a2c2SAdrian Chadd 			/*
1086e2e4a2c2SAdrian Chadd 			 * Include the size of next fragment so NAV is
1087e2e4a2c2SAdrian Chadd 			 * updated properly.  The last fragment uses only
1088e2e4a2c2SAdrian Chadd 			 * the ACK duration
10899572684aSAdrian Chadd 			 *
10909572684aSAdrian Chadd 			 * XXX TODO: ensure that the rate lookup for each
10919572684aSAdrian Chadd 			 * fragment is the same as the rate used by the
10929572684aSAdrian Chadd 			 * first fragment!
1093e2e4a2c2SAdrian Chadd 			 */
1094e2e4a2c2SAdrian Chadd 			dur += ath_hal_computetxtime(ah, rt,
1095e2e4a2c2SAdrian Chadd 					bf->bf_m->m_nextpkt->m_pkthdr.len,
1096e2e4a2c2SAdrian Chadd 					rix, shortPreamble);
1097e2e4a2c2SAdrian Chadd 		}
1098e2e4a2c2SAdrian Chadd 		if (isfrag) {
1099e2e4a2c2SAdrian Chadd 			/*
1100e2e4a2c2SAdrian Chadd 			 * Force hardware to use computed duration for next
1101e2e4a2c2SAdrian Chadd 			 * fragment by disabling multi-rate retry which updates
1102e2e4a2c2SAdrian Chadd 			 * duration based on the multi-rate duration table.
1103e2e4a2c2SAdrian Chadd 			 */
1104e2e4a2c2SAdrian Chadd 			bf->bf_state.bfs_ismrr = 0;
1105e2e4a2c2SAdrian Chadd 			bf->bf_state.bfs_try0 = ATH_TXMGTTRY;
1106e2e4a2c2SAdrian Chadd 			/* XXX update bfs_rc[0].try? */
1107e2e4a2c2SAdrian Chadd 		}
1108e2e4a2c2SAdrian Chadd 
1109e2e4a2c2SAdrian Chadd 		/* Update the duration field itself */
1110e2e4a2c2SAdrian Chadd 		*(u_int16_t *)wh->i_dur = htole16(dur);
1111e2e4a2c2SAdrian Chadd 	}
1112e2e4a2c2SAdrian Chadd }
1113e2e4a2c2SAdrian Chadd 
1114e42b5dbaSAdrian Chadd static uint8_t
1115e42b5dbaSAdrian Chadd ath_tx_get_rtscts_rate(struct ath_hal *ah, const HAL_RATE_TABLE *rt,
1116eb6f0de0SAdrian Chadd     int cix, int shortPreamble)
111779f02dbfSAdrian Chadd {
1118e42b5dbaSAdrian Chadd 	uint8_t ctsrate;
1119e42b5dbaSAdrian Chadd 
112079f02dbfSAdrian Chadd 	/*
112179f02dbfSAdrian Chadd 	 * CTS transmit rate is derived from the transmit rate
112279f02dbfSAdrian Chadd 	 * by looking in the h/w rate table.  We must also factor
112379f02dbfSAdrian Chadd 	 * in whether or not a short preamble is to be used.
112479f02dbfSAdrian Chadd 	 */
112579f02dbfSAdrian Chadd 	/* NB: cix is set above where RTS/CTS is enabled */
112679f02dbfSAdrian Chadd 	KASSERT(cix != 0xff, ("cix not setup"));
1127e42b5dbaSAdrian Chadd 	ctsrate = rt->info[cix].rateCode;
1128e42b5dbaSAdrian Chadd 
1129e42b5dbaSAdrian Chadd 	/* XXX this should only matter for legacy rates */
1130e42b5dbaSAdrian Chadd 	if (shortPreamble)
1131e42b5dbaSAdrian Chadd 		ctsrate |= rt->info[cix].shortPreamble;
1132e42b5dbaSAdrian Chadd 
1133d4365d16SAdrian Chadd 	return (ctsrate);
1134e42b5dbaSAdrian Chadd }
1135e42b5dbaSAdrian Chadd 
1136e42b5dbaSAdrian Chadd /*
1137e42b5dbaSAdrian Chadd  * Calculate the RTS/CTS duration for legacy frames.
1138e42b5dbaSAdrian Chadd  */
1139e42b5dbaSAdrian Chadd static int
1140e42b5dbaSAdrian Chadd ath_tx_calc_ctsduration(struct ath_hal *ah, int rix, int cix,
1141e42b5dbaSAdrian Chadd     int shortPreamble, int pktlen, const HAL_RATE_TABLE *rt,
1142e42b5dbaSAdrian Chadd     int flags)
1143e42b5dbaSAdrian Chadd {
1144e42b5dbaSAdrian Chadd 	int ctsduration = 0;
1145e42b5dbaSAdrian Chadd 
1146e42b5dbaSAdrian Chadd 	/* This mustn't be called for HT modes */
1147e42b5dbaSAdrian Chadd 	if (rt->info[cix].phy == IEEE80211_T_HT) {
1148e42b5dbaSAdrian Chadd 		printf("%s: HT rate where it shouldn't be (0x%x)\n",
1149e42b5dbaSAdrian Chadd 		    __func__, rt->info[cix].rateCode);
1150d4365d16SAdrian Chadd 		return (-1);
1151e42b5dbaSAdrian Chadd 	}
1152e42b5dbaSAdrian Chadd 
115379f02dbfSAdrian Chadd 	/*
115479f02dbfSAdrian Chadd 	 * Compute the transmit duration based on the frame
115579f02dbfSAdrian Chadd 	 * size and the size of an ACK frame.  We call into the
115679f02dbfSAdrian Chadd 	 * HAL to do the computation since it depends on the
115779f02dbfSAdrian Chadd 	 * characteristics of the actual PHY being used.
115879f02dbfSAdrian Chadd 	 *
115979f02dbfSAdrian Chadd 	 * NB: CTS is assumed the same size as an ACK so we can
116079f02dbfSAdrian Chadd 	 *     use the precalculated ACK durations.
116179f02dbfSAdrian Chadd 	 */
116279f02dbfSAdrian Chadd 	if (shortPreamble) {
116379f02dbfSAdrian Chadd 		if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
1164e42b5dbaSAdrian Chadd 			ctsduration += rt->info[cix].spAckDuration;
1165e42b5dbaSAdrian Chadd 		ctsduration += ath_hal_computetxtime(ah,
116679f02dbfSAdrian Chadd 			rt, pktlen, rix, AH_TRUE);
116779f02dbfSAdrian Chadd 		if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
1168e42b5dbaSAdrian Chadd 			ctsduration += rt->info[rix].spAckDuration;
116979f02dbfSAdrian Chadd 	} else {
117079f02dbfSAdrian Chadd 		if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
1171e42b5dbaSAdrian Chadd 			ctsduration += rt->info[cix].lpAckDuration;
1172e42b5dbaSAdrian Chadd 		ctsduration += ath_hal_computetxtime(ah,
117379f02dbfSAdrian Chadd 			rt, pktlen, rix, AH_FALSE);
117479f02dbfSAdrian Chadd 		if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
1175e42b5dbaSAdrian Chadd 			ctsduration += rt->info[rix].lpAckDuration;
117679f02dbfSAdrian Chadd 	}
1177e42b5dbaSAdrian Chadd 
1178d4365d16SAdrian Chadd 	return (ctsduration);
117979f02dbfSAdrian Chadd }
118079f02dbfSAdrian Chadd 
1181eb6f0de0SAdrian Chadd /*
1182eb6f0de0SAdrian Chadd  * Update the given ath_buf with updated rts/cts setup and duration
1183eb6f0de0SAdrian Chadd  * values.
1184eb6f0de0SAdrian Chadd  *
1185eb6f0de0SAdrian Chadd  * To support rate lookups for each software retry, the rts/cts rate
1186eb6f0de0SAdrian Chadd  * and cts duration must be re-calculated.
1187eb6f0de0SAdrian Chadd  *
1188eb6f0de0SAdrian Chadd  * This function assumes the RTS/CTS flags have been set as needed;
1189eb6f0de0SAdrian Chadd  * mrr has been disabled; and the rate control lookup has been done.
1190eb6f0de0SAdrian Chadd  *
1191eb6f0de0SAdrian Chadd  * XXX TODO: MRR need only be disabled for the pre-11n NICs.
1192eb6f0de0SAdrian Chadd  * XXX The 11n NICs support per-rate RTS/CTS configuration.
1193eb6f0de0SAdrian Chadd  */
1194eb6f0de0SAdrian Chadd static void
1195eb6f0de0SAdrian Chadd ath_tx_set_rtscts(struct ath_softc *sc, struct ath_buf *bf)
1196eb6f0de0SAdrian Chadd {
1197eb6f0de0SAdrian Chadd 	uint16_t ctsduration = 0;
1198eb6f0de0SAdrian Chadd 	uint8_t ctsrate = 0;
1199eb6f0de0SAdrian Chadd 	uint8_t rix = bf->bf_state.bfs_rc[0].rix;
1200eb6f0de0SAdrian Chadd 	uint8_t cix = 0;
1201eb6f0de0SAdrian Chadd 	const HAL_RATE_TABLE *rt = sc->sc_currates;
1202eb6f0de0SAdrian Chadd 
1203eb6f0de0SAdrian Chadd 	/*
1204eb6f0de0SAdrian Chadd 	 * No RTS/CTS enabled? Don't bother.
1205eb6f0de0SAdrian Chadd 	 */
1206875a9451SAdrian Chadd 	if ((bf->bf_state.bfs_txflags &
1207eb6f0de0SAdrian Chadd 	    (HAL_TXDESC_RTSENA | HAL_TXDESC_CTSENA)) == 0) {
1208eb6f0de0SAdrian Chadd 		/* XXX is this really needed? */
1209eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_ctsrate = 0;
1210eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_ctsduration = 0;
1211eb6f0de0SAdrian Chadd 		return;
1212eb6f0de0SAdrian Chadd 	}
1213eb6f0de0SAdrian Chadd 
1214eb6f0de0SAdrian Chadd 	/*
1215eb6f0de0SAdrian Chadd 	 * If protection is enabled, use the protection rix control
1216eb6f0de0SAdrian Chadd 	 * rate. Otherwise use the rate0 control rate.
1217eb6f0de0SAdrian Chadd 	 */
1218eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_doprot)
1219eb6f0de0SAdrian Chadd 		rix = sc->sc_protrix;
1220eb6f0de0SAdrian Chadd 	else
1221eb6f0de0SAdrian Chadd 		rix = bf->bf_state.bfs_rc[0].rix;
1222eb6f0de0SAdrian Chadd 
1223eb6f0de0SAdrian Chadd 	/*
1224eb6f0de0SAdrian Chadd 	 * If the raw path has hard-coded ctsrate0 to something,
1225eb6f0de0SAdrian Chadd 	 * use it.
1226eb6f0de0SAdrian Chadd 	 */
1227eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_ctsrate0 != 0)
1228eb6f0de0SAdrian Chadd 		cix = ath_tx_findrix(sc, bf->bf_state.bfs_ctsrate0);
1229eb6f0de0SAdrian Chadd 	else
1230eb6f0de0SAdrian Chadd 		/* Control rate from above */
1231eb6f0de0SAdrian Chadd 		cix = rt->info[rix].controlRate;
1232eb6f0de0SAdrian Chadd 
1233eb6f0de0SAdrian Chadd 	/* Calculate the rtscts rate for the given cix */
1234eb6f0de0SAdrian Chadd 	ctsrate = ath_tx_get_rtscts_rate(sc->sc_ah, rt, cix,
1235eb6f0de0SAdrian Chadd 	    bf->bf_state.bfs_shpream);
1236eb6f0de0SAdrian Chadd 
1237eb6f0de0SAdrian Chadd 	/* The 11n chipsets do ctsduration calculations for you */
1238eb6f0de0SAdrian Chadd 	if (! ath_tx_is_11n(sc))
1239eb6f0de0SAdrian Chadd 		ctsduration = ath_tx_calc_ctsduration(sc->sc_ah, rix, cix,
1240eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_shpream, bf->bf_state.bfs_pktlen,
1241875a9451SAdrian Chadd 		    rt, bf->bf_state.bfs_txflags);
1242eb6f0de0SAdrian Chadd 
1243eb6f0de0SAdrian Chadd 	/* Squirrel away in ath_buf */
1244eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsrate = ctsrate;
1245eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsduration = ctsduration;
1246eb6f0de0SAdrian Chadd 
1247eb6f0de0SAdrian Chadd 	/*
1248eb6f0de0SAdrian Chadd 	 * Must disable multi-rate retry when using RTS/CTS.
1249eb6f0de0SAdrian Chadd 	 */
1250af017101SAdrian Chadd 	if (!sc->sc_mrrprot) {
1251eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_ismrr = 0;
1252eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_try0 =
1253eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_rc[0].tries = ATH_TXMGTTRY; /* XXX ew */
1254eb6f0de0SAdrian Chadd 	}
1255af017101SAdrian Chadd }
1256eb6f0de0SAdrian Chadd 
1257eb6f0de0SAdrian Chadd /*
1258eb6f0de0SAdrian Chadd  * Setup the descriptor chain for a normal or fast-frame
1259eb6f0de0SAdrian Chadd  * frame.
126046634305SAdrian Chadd  *
126146634305SAdrian Chadd  * XXX TODO: extend to include the destination hardware QCU ID.
126246634305SAdrian Chadd  * Make sure that is correct.  Make sure that when being added
126346634305SAdrian Chadd  * to the mcastq, the CABQ QCUID is set or things will get a bit
126446634305SAdrian Chadd  * odd.
1265eb6f0de0SAdrian Chadd  */
1266eb6f0de0SAdrian Chadd static void
1267eb6f0de0SAdrian Chadd ath_tx_setds(struct ath_softc *sc, struct ath_buf *bf)
1268eb6f0de0SAdrian Chadd {
1269eb6f0de0SAdrian Chadd 	struct ath_desc *ds = bf->bf_desc;
1270eb6f0de0SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
1271eb6f0de0SAdrian Chadd 
12727d9dd2acSAdrian Chadd 	if (bf->bf_state.bfs_txrate0 == 0)
12737d9dd2acSAdrian Chadd 		device_printf(sc->sc_dev, "%s: bf=%p, txrate0=%d\n",
12747d9dd2acSAdrian Chadd 		    __func__, bf, 0);
12757d9dd2acSAdrian Chadd 
1276eb6f0de0SAdrian Chadd 	ath_hal_setuptxdesc(ah, ds
1277eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_pktlen	/* packet length */
1278eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_hdrlen	/* header length */
1279eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_atype	/* Atheros packet type */
1280eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_txpower	/* txpower */
1281eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_txrate0
1282eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_try0		/* series 0 rate/tries */
1283eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_keyix	/* key cache index */
1284eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_txantenna	/* antenna mode */
1285875a9451SAdrian Chadd 		, bf->bf_state.bfs_txflags	/* flags */
1286eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_ctsrate	/* rts/cts rate */
1287eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_ctsduration	/* rts/cts duration */
1288eb6f0de0SAdrian Chadd 	);
1289eb6f0de0SAdrian Chadd 
1290eb6f0de0SAdrian Chadd 	/*
1291eb6f0de0SAdrian Chadd 	 * This will be overriden when the descriptor chain is written.
1292eb6f0de0SAdrian Chadd 	 */
1293eb6f0de0SAdrian Chadd 	bf->bf_lastds = ds;
1294eb6f0de0SAdrian Chadd 	bf->bf_last = bf;
1295eb6f0de0SAdrian Chadd 
1296d34a7347SAdrian Chadd 	/* Set rate control and descriptor chain for this frame */
1297d34a7347SAdrian Chadd 	ath_tx_set_ratectrl(sc, bf->bf_node, bf);
12986e84772fSAdrian Chadd 	ath_tx_chaindesclist(sc, ds, bf, 0, 0, 0);
1299eb6f0de0SAdrian Chadd }
1300eb6f0de0SAdrian Chadd 
1301eb6f0de0SAdrian Chadd /*
1302eb6f0de0SAdrian Chadd  * Do a rate lookup.
1303eb6f0de0SAdrian Chadd  *
1304eb6f0de0SAdrian Chadd  * This performs a rate lookup for the given ath_buf only if it's required.
1305eb6f0de0SAdrian Chadd  * Non-data frames and raw frames don't require it.
1306eb6f0de0SAdrian Chadd  *
1307eb6f0de0SAdrian Chadd  * This populates the primary and MRR entries; MRR values are
1308eb6f0de0SAdrian Chadd  * then disabled later on if something requires it (eg RTS/CTS on
1309eb6f0de0SAdrian Chadd  * pre-11n chipsets.
1310eb6f0de0SAdrian Chadd  *
1311eb6f0de0SAdrian Chadd  * This needs to be done before the RTS/CTS fields are calculated
1312eb6f0de0SAdrian Chadd  * as they may depend upon the rate chosen.
1313eb6f0de0SAdrian Chadd  */
1314eb6f0de0SAdrian Chadd static void
1315eb6f0de0SAdrian Chadd ath_tx_do_ratelookup(struct ath_softc *sc, struct ath_buf *bf)
1316eb6f0de0SAdrian Chadd {
1317eb6f0de0SAdrian Chadd 	uint8_t rate, rix;
1318eb6f0de0SAdrian Chadd 	int try0;
1319eb6f0de0SAdrian Chadd 
1320eb6f0de0SAdrian Chadd 	if (! bf->bf_state.bfs_doratelookup)
1321eb6f0de0SAdrian Chadd 		return;
1322eb6f0de0SAdrian Chadd 
1323eb6f0de0SAdrian Chadd 	/* Get rid of any previous state */
1324eb6f0de0SAdrian Chadd 	bzero(bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
1325eb6f0de0SAdrian Chadd 
1326eb6f0de0SAdrian Chadd 	ATH_NODE_LOCK(ATH_NODE(bf->bf_node));
1327eb6f0de0SAdrian Chadd 	ath_rate_findrate(sc, ATH_NODE(bf->bf_node), bf->bf_state.bfs_shpream,
1328eb6f0de0SAdrian Chadd 	    bf->bf_state.bfs_pktlen, &rix, &try0, &rate);
1329eb6f0de0SAdrian Chadd 
1330eb6f0de0SAdrian Chadd 	/* In case MRR is disabled, make sure rc[0] is setup correctly */
1331eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].rix = rix;
1332eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].ratecode = rate;
1333eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].tries = try0;
1334eb6f0de0SAdrian Chadd 
1335eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_ismrr && try0 != ATH_TXMAXTRY)
1336eb6f0de0SAdrian Chadd 		ath_rate_getxtxrates(sc, ATH_NODE(bf->bf_node), rix,
1337eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_rc);
1338eb6f0de0SAdrian Chadd 	ATH_NODE_UNLOCK(ATH_NODE(bf->bf_node));
1339eb6f0de0SAdrian Chadd 
1340eb6f0de0SAdrian Chadd 	sc->sc_txrix = rix;	/* for LED blinking */
1341eb6f0de0SAdrian Chadd 	sc->sc_lastdatarix = rix;	/* for fast frames */
1342eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_try0 = try0;
1343eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txrate0 = rate;
1344eb6f0de0SAdrian Chadd }
1345eb6f0de0SAdrian Chadd 
1346eb6f0de0SAdrian Chadd /*
13470c54de88SAdrian Chadd  * Update the CLRDMASK bit in the ath_buf if it needs to be set.
13480c54de88SAdrian Chadd  */
13490c54de88SAdrian Chadd static void
13500c54de88SAdrian Chadd ath_tx_update_clrdmask(struct ath_softc *sc, struct ath_tid *tid,
13510c54de88SAdrian Chadd     struct ath_buf *bf)
13520c54de88SAdrian Chadd {
13530c54de88SAdrian Chadd 
13540c54de88SAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
13550c54de88SAdrian Chadd 
13560c54de88SAdrian Chadd 	if (tid->clrdmask == 1) {
13570c54de88SAdrian Chadd 		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
13580c54de88SAdrian Chadd 		tid->clrdmask = 0;
13590c54de88SAdrian Chadd 	}
13600c54de88SAdrian Chadd }
13610c54de88SAdrian Chadd 
13620c54de88SAdrian Chadd /*
1363eb6f0de0SAdrian Chadd  * Transmit the given frame to the hardware.
1364eb6f0de0SAdrian Chadd  *
1365eb6f0de0SAdrian Chadd  * The frame must already be setup; rate control must already have
1366eb6f0de0SAdrian Chadd  * been done.
1367eb6f0de0SAdrian Chadd  *
1368eb6f0de0SAdrian Chadd  * XXX since the TXQ lock is being held here (and I dislike holding
1369eb6f0de0SAdrian Chadd  * it for this long when not doing software aggregation), later on
1370eb6f0de0SAdrian Chadd  * break this function into "setup_normal" and "xmit_normal". The
1371eb6f0de0SAdrian Chadd  * lock only needs to be held for the ath_tx_handoff call.
1372eb6f0de0SAdrian Chadd  */
1373eb6f0de0SAdrian Chadd static void
1374eb6f0de0SAdrian Chadd ath_tx_xmit_normal(struct ath_softc *sc, struct ath_txq *txq,
1375eb6f0de0SAdrian Chadd     struct ath_buf *bf)
1376eb6f0de0SAdrian Chadd {
13770c54de88SAdrian Chadd 	struct ath_node *an = ATH_NODE(bf->bf_node);
13780c54de88SAdrian Chadd 	struct ath_tid *tid = &an->an_tid[bf->bf_state.bfs_tid];
1379eb6f0de0SAdrian Chadd 
1380eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
1381eb6f0de0SAdrian Chadd 
13820c54de88SAdrian Chadd 	/*
13830c54de88SAdrian Chadd 	 * For now, just enable CLRDMASK. ath_tx_xmit_normal() does
13840c54de88SAdrian Chadd 	 * set a completion handler however it doesn't (yet) properly
13850c54de88SAdrian Chadd 	 * handle the strict ordering requirements needed for normal,
13860c54de88SAdrian Chadd 	 * non-aggregate session frames.
13870c54de88SAdrian Chadd 	 *
13880c54de88SAdrian Chadd 	 * Once this is implemented, only set CLRDMASK like this for
13890c54de88SAdrian Chadd 	 * frames that must go out - eg management/raw frames.
13900c54de88SAdrian Chadd 	 */
13910c54de88SAdrian Chadd 	bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
13920c54de88SAdrian Chadd 
1393eb6f0de0SAdrian Chadd 	/* Setup the descriptor before handoff */
1394eb6f0de0SAdrian Chadd 	ath_tx_do_ratelookup(sc, bf);
1395e2e4a2c2SAdrian Chadd 	ath_tx_calc_duration(sc, bf);
1396e2e4a2c2SAdrian Chadd 	ath_tx_calc_protection(sc, bf);
1397eb6f0de0SAdrian Chadd 	ath_tx_set_rtscts(sc, bf);
1398e2e4a2c2SAdrian Chadd 	ath_tx_rate_fill_rcflags(sc, bf);
1399eb6f0de0SAdrian Chadd 	ath_tx_setds(sc, bf);
1400eb6f0de0SAdrian Chadd 
14010c54de88SAdrian Chadd 	/* Track per-TID hardware queue depth correctly */
14020c54de88SAdrian Chadd 	tid->hwq_depth++;
14030c54de88SAdrian Chadd 
14040c54de88SAdrian Chadd 	/* Assign the completion handler */
14050c54de88SAdrian Chadd 	bf->bf_comp = ath_tx_normal_comp;
14064e81f27cSAdrian Chadd 
1407eb6f0de0SAdrian Chadd 	/* Hand off to hardware */
1408eb6f0de0SAdrian Chadd 	ath_tx_handoff(sc, txq, bf);
1409eb6f0de0SAdrian Chadd }
1410eb6f0de0SAdrian Chadd 
1411d05b576dSAdrian Chadd /*
1412d05b576dSAdrian Chadd  * Do the basic frame setup stuff that's required before the frame
1413d05b576dSAdrian Chadd  * is added to a software queue.
1414d05b576dSAdrian Chadd  *
1415d05b576dSAdrian Chadd  * All frames get mostly the same treatment and it's done once.
1416d05b576dSAdrian Chadd  * Retransmits fiddle with things like the rate control setup,
1417d05b576dSAdrian Chadd  * setting the retransmit bit in the packet; doing relevant DMA/bus
1418d05b576dSAdrian Chadd  * syncing and relinking it (back) into the hardware TX queue.
1419d05b576dSAdrian Chadd  *
1420d05b576dSAdrian Chadd  * Note that this may cause the mbuf to be reallocated, so
1421d05b576dSAdrian Chadd  * m0 may not be valid.
1422d05b576dSAdrian Chadd  */
1423eb6f0de0SAdrian Chadd static int
1424eb6f0de0SAdrian Chadd ath_tx_normal_setup(struct ath_softc *sc, struct ieee80211_node *ni,
1425b43facbfSAdrian Chadd     struct ath_buf *bf, struct mbuf *m0, struct ath_txq *txq)
1426b8e788a5SAdrian Chadd {
1427b8e788a5SAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
1428b8e788a5SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
1429b8e788a5SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
1430b8e788a5SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
1431b8e788a5SAdrian Chadd 	const struct chanAccParams *cap = &ic->ic_wme.wme_chanParams;
1432b8e788a5SAdrian Chadd 	int error, iswep, ismcast, isfrag, ismrr;
1433eb6f0de0SAdrian Chadd 	int keyix, hdrlen, pktlen, try0 = 0;
1434eb6f0de0SAdrian Chadd 	u_int8_t rix = 0, txrate = 0;
1435b8e788a5SAdrian Chadd 	struct ath_desc *ds;
1436b8e788a5SAdrian Chadd 	struct ieee80211_frame *wh;
1437eb6f0de0SAdrian Chadd 	u_int subtype, flags;
1438b8e788a5SAdrian Chadd 	HAL_PKT_TYPE atype;
1439b8e788a5SAdrian Chadd 	const HAL_RATE_TABLE *rt;
1440b8e788a5SAdrian Chadd 	HAL_BOOL shortPreamble;
1441b8e788a5SAdrian Chadd 	struct ath_node *an;
1442b8e788a5SAdrian Chadd 	u_int pri;
1443b8e788a5SAdrian Chadd 
14447561cb5cSAdrian Chadd 	/*
14457561cb5cSAdrian Chadd 	 * To ensure that both sequence numbers and the CCMP PN handling
14467561cb5cSAdrian Chadd 	 * is "correct", make sure that the relevant TID queue is locked.
14477561cb5cSAdrian Chadd 	 * Otherwise the CCMP PN and seqno may appear out of order, causing
14487561cb5cSAdrian Chadd 	 * re-ordered frames to have out of order CCMP PN's, resulting
14497561cb5cSAdrian Chadd 	 * in many, many frame drops.
14507561cb5cSAdrian Chadd 	 */
14517561cb5cSAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
14527561cb5cSAdrian Chadd 
1453b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1454b8e788a5SAdrian Chadd 	iswep = wh->i_fc[1] & IEEE80211_FC1_WEP;
1455b8e788a5SAdrian Chadd 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1456b8e788a5SAdrian Chadd 	isfrag = m0->m_flags & M_FRAG;
1457b8e788a5SAdrian Chadd 	hdrlen = ieee80211_anyhdrsize(wh);
1458b8e788a5SAdrian Chadd 	/*
1459b8e788a5SAdrian Chadd 	 * Packet length must not include any
1460b8e788a5SAdrian Chadd 	 * pad bytes; deduct them here.
1461b8e788a5SAdrian Chadd 	 */
1462b8e788a5SAdrian Chadd 	pktlen = m0->m_pkthdr.len - (hdrlen & 3);
1463b8e788a5SAdrian Chadd 
146481a82688SAdrian Chadd 	/* Handle encryption twiddling if needed */
1465eb6f0de0SAdrian Chadd 	if (! ath_tx_tag_crypto(sc, ni, m0, iswep, isfrag, &hdrlen,
1466eb6f0de0SAdrian Chadd 	    &pktlen, &keyix)) {
1467b8e788a5SAdrian Chadd 		ath_freetx(m0);
1468b8e788a5SAdrian Chadd 		return EIO;
1469b8e788a5SAdrian Chadd 	}
1470b8e788a5SAdrian Chadd 
1471b8e788a5SAdrian Chadd 	/* packet header may have moved, reset our local pointer */
1472b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1473b8e788a5SAdrian Chadd 
1474b8e788a5SAdrian Chadd 	pktlen += IEEE80211_CRC_LEN;
1475b8e788a5SAdrian Chadd 
1476b8e788a5SAdrian Chadd 	/*
1477b8e788a5SAdrian Chadd 	 * Load the DMA map so any coalescing is done.  This
1478b8e788a5SAdrian Chadd 	 * also calculates the number of descriptors we need.
1479b8e788a5SAdrian Chadd 	 */
1480b8e788a5SAdrian Chadd 	error = ath_tx_dmasetup(sc, bf, m0);
1481b8e788a5SAdrian Chadd 	if (error != 0)
1482b8e788a5SAdrian Chadd 		return error;
1483b8e788a5SAdrian Chadd 	bf->bf_node = ni;			/* NB: held reference */
1484b8e788a5SAdrian Chadd 	m0 = bf->bf_m;				/* NB: may have changed */
1485b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1486b8e788a5SAdrian Chadd 
1487b8e788a5SAdrian Chadd 	/* setup descriptors */
1488b8e788a5SAdrian Chadd 	ds = bf->bf_desc;
1489b8e788a5SAdrian Chadd 	rt = sc->sc_currates;
1490b8e788a5SAdrian Chadd 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
1491b8e788a5SAdrian Chadd 
1492b8e788a5SAdrian Chadd 	/*
1493b8e788a5SAdrian Chadd 	 * NB: the 802.11 layer marks whether or not we should
1494b8e788a5SAdrian Chadd 	 * use short preamble based on the current mode and
1495b8e788a5SAdrian Chadd 	 * negotiated parameters.
1496b8e788a5SAdrian Chadd 	 */
1497b8e788a5SAdrian Chadd 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1498b8e788a5SAdrian Chadd 	    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
1499b8e788a5SAdrian Chadd 		shortPreamble = AH_TRUE;
1500b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_shortpre++;
1501b8e788a5SAdrian Chadd 	} else {
1502b8e788a5SAdrian Chadd 		shortPreamble = AH_FALSE;
1503b8e788a5SAdrian Chadd 	}
1504b8e788a5SAdrian Chadd 
1505b8e788a5SAdrian Chadd 	an = ATH_NODE(ni);
15064e81f27cSAdrian Chadd 	//flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for crypto errs */
15074e81f27cSAdrian Chadd 	flags = 0;
1508b8e788a5SAdrian Chadd 	ismrr = 0;				/* default no multi-rate retry*/
1509b8e788a5SAdrian Chadd 	pri = M_WME_GETAC(m0);			/* honor classification */
1510b8e788a5SAdrian Chadd 	/* XXX use txparams instead of fixed values */
1511b8e788a5SAdrian Chadd 	/*
1512b8e788a5SAdrian Chadd 	 * Calculate Atheros packet type from IEEE80211 packet header,
1513b8e788a5SAdrian Chadd 	 * setup for rate calculations, and select h/w transmit queue.
1514b8e788a5SAdrian Chadd 	 */
1515b8e788a5SAdrian Chadd 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1516b8e788a5SAdrian Chadd 	case IEEE80211_FC0_TYPE_MGT:
1517b8e788a5SAdrian Chadd 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1518b8e788a5SAdrian Chadd 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON)
1519b8e788a5SAdrian Chadd 			atype = HAL_PKT_TYPE_BEACON;
1520b8e788a5SAdrian Chadd 		else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1521b8e788a5SAdrian Chadd 			atype = HAL_PKT_TYPE_PROBE_RESP;
1522b8e788a5SAdrian Chadd 		else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM)
1523b8e788a5SAdrian Chadd 			atype = HAL_PKT_TYPE_ATIM;
1524b8e788a5SAdrian Chadd 		else
1525b8e788a5SAdrian Chadd 			atype = HAL_PKT_TYPE_NORMAL;	/* XXX */
1526b8e788a5SAdrian Chadd 		rix = an->an_mgmtrix;
1527b8e788a5SAdrian Chadd 		txrate = rt->info[rix].rateCode;
1528b8e788a5SAdrian Chadd 		if (shortPreamble)
1529b8e788a5SAdrian Chadd 			txrate |= rt->info[rix].shortPreamble;
1530b8e788a5SAdrian Chadd 		try0 = ATH_TXMGTTRY;
1531b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_INTREQ;	/* force interrupt */
1532b8e788a5SAdrian Chadd 		break;
1533b8e788a5SAdrian Chadd 	case IEEE80211_FC0_TYPE_CTL:
1534b8e788a5SAdrian Chadd 		atype = HAL_PKT_TYPE_PSPOLL;	/* stop setting of duration */
1535b8e788a5SAdrian Chadd 		rix = an->an_mgmtrix;
1536b8e788a5SAdrian Chadd 		txrate = rt->info[rix].rateCode;
1537b8e788a5SAdrian Chadd 		if (shortPreamble)
1538b8e788a5SAdrian Chadd 			txrate |= rt->info[rix].shortPreamble;
1539b8e788a5SAdrian Chadd 		try0 = ATH_TXMGTTRY;
1540b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_INTREQ;	/* force interrupt */
1541b8e788a5SAdrian Chadd 		break;
1542b8e788a5SAdrian Chadd 	case IEEE80211_FC0_TYPE_DATA:
1543b8e788a5SAdrian Chadd 		atype = HAL_PKT_TYPE_NORMAL;		/* default */
1544b8e788a5SAdrian Chadd 		/*
1545b8e788a5SAdrian Chadd 		 * Data frames: multicast frames go out at a fixed rate,
1546b8e788a5SAdrian Chadd 		 * EAPOL frames use the mgmt frame rate; otherwise consult
1547b8e788a5SAdrian Chadd 		 * the rate control module for the rate to use.
1548b8e788a5SAdrian Chadd 		 */
1549b8e788a5SAdrian Chadd 		if (ismcast) {
1550b8e788a5SAdrian Chadd 			rix = an->an_mcastrix;
1551b8e788a5SAdrian Chadd 			txrate = rt->info[rix].rateCode;
1552b8e788a5SAdrian Chadd 			if (shortPreamble)
1553b8e788a5SAdrian Chadd 				txrate |= rt->info[rix].shortPreamble;
1554b8e788a5SAdrian Chadd 			try0 = 1;
1555b8e788a5SAdrian Chadd 		} else if (m0->m_flags & M_EAPOL) {
1556b8e788a5SAdrian Chadd 			/* XXX? maybe always use long preamble? */
1557b8e788a5SAdrian Chadd 			rix = an->an_mgmtrix;
1558b8e788a5SAdrian Chadd 			txrate = rt->info[rix].rateCode;
1559b8e788a5SAdrian Chadd 			if (shortPreamble)
1560b8e788a5SAdrian Chadd 				txrate |= rt->info[rix].shortPreamble;
1561b8e788a5SAdrian Chadd 			try0 = ATH_TXMAXTRY;	/* XXX?too many? */
1562b8e788a5SAdrian Chadd 		} else {
1563eb6f0de0SAdrian Chadd 			/*
1564eb6f0de0SAdrian Chadd 			 * Do rate lookup on each TX, rather than using
1565eb6f0de0SAdrian Chadd 			 * the hard-coded TX information decided here.
1566eb6f0de0SAdrian Chadd 			 */
1567b8e788a5SAdrian Chadd 			ismrr = 1;
1568eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_doratelookup = 1;
1569b8e788a5SAdrian Chadd 		}
1570b8e788a5SAdrian Chadd 		if (cap->cap_wmeParams[pri].wmep_noackPolicy)
1571b8e788a5SAdrian Chadd 			flags |= HAL_TXDESC_NOACK;
1572b8e788a5SAdrian Chadd 		break;
1573b8e788a5SAdrian Chadd 	default:
1574b8e788a5SAdrian Chadd 		if_printf(ifp, "bogus frame type 0x%x (%s)\n",
1575b8e788a5SAdrian Chadd 			wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__);
1576b8e788a5SAdrian Chadd 		/* XXX statistic */
1577b8e788a5SAdrian Chadd 		ath_freetx(m0);
1578b8e788a5SAdrian Chadd 		return EIO;
1579b8e788a5SAdrian Chadd 	}
1580b8e788a5SAdrian Chadd 
1581447fd44aSAdrian Chadd 	/*
1582447fd44aSAdrian Chadd 	 * There are two known scenarios where the frame AC doesn't match
1583447fd44aSAdrian Chadd 	 * what the destination TXQ is.
1584447fd44aSAdrian Chadd 	 *
1585447fd44aSAdrian Chadd 	 * + non-QoS frames (eg management?) that the net80211 stack has
1586447fd44aSAdrian Chadd 	 *   assigned a higher AC to, but since it's a non-QoS TID, it's
1587447fd44aSAdrian Chadd 	 *   being thrown into TID 16.  TID 16 gets the AC_BE queue.
1588447fd44aSAdrian Chadd 	 *   It's quite possible that management frames should just be
1589447fd44aSAdrian Chadd 	 *   direct dispatched to hardware rather than go via the software
1590447fd44aSAdrian Chadd 	 *   queue; that should be investigated in the future.  There are
1591447fd44aSAdrian Chadd 	 *   some specific scenarios where this doesn't make sense, mostly
1592447fd44aSAdrian Chadd 	 *   surrounding ADDBA request/response - hence why that is special
1593447fd44aSAdrian Chadd 	 *   cased.
1594447fd44aSAdrian Chadd 	 *
1595447fd44aSAdrian Chadd 	 * + Multicast frames going into the VAP mcast queue.  That shows up
1596447fd44aSAdrian Chadd 	 *   as "TXQ 11".
1597447fd44aSAdrian Chadd 	 *
1598447fd44aSAdrian Chadd 	 * This driver should eventually support separate TID and TXQ locking,
1599447fd44aSAdrian Chadd 	 * allowing for arbitrary AC frames to appear on arbitrary software
1600447fd44aSAdrian Chadd 	 * queues, being queued to the "correct" hardware queue when needed.
1601447fd44aSAdrian Chadd 	 */
1602447fd44aSAdrian Chadd #if 0
16036deb7f32SAdrian Chadd 	if (txq != sc->sc_ac2q[pri]) {
16046deb7f32SAdrian Chadd 		device_printf(sc->sc_dev,
16056deb7f32SAdrian Chadd 		    "%s: txq=%p (%d), pri=%d, pri txq=%p (%d)\n",
16066deb7f32SAdrian Chadd 		    __func__,
16076deb7f32SAdrian Chadd 		    txq,
16086deb7f32SAdrian Chadd 		    txq->axq_qnum,
16096deb7f32SAdrian Chadd 		    pri,
16106deb7f32SAdrian Chadd 		    sc->sc_ac2q[pri],
16116deb7f32SAdrian Chadd 		    sc->sc_ac2q[pri]->axq_qnum);
16126deb7f32SAdrian Chadd 	}
1613447fd44aSAdrian Chadd #endif
16146deb7f32SAdrian Chadd 
1615b8e788a5SAdrian Chadd 	/*
1616b8e788a5SAdrian Chadd 	 * Calculate miscellaneous flags.
1617b8e788a5SAdrian Chadd 	 */
1618b8e788a5SAdrian Chadd 	if (ismcast) {
1619b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_NOACK;	/* no ack on broad/multicast */
1620b8e788a5SAdrian Chadd 	} else if (pktlen > vap->iv_rtsthreshold &&
1621b8e788a5SAdrian Chadd 	    (ni->ni_ath_flags & IEEE80211_NODE_FF) == 0) {
1622b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_RTSENA;	/* RTS based on frame length */
1623b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_rts++;
1624b8e788a5SAdrian Chadd 	}
1625b8e788a5SAdrian Chadd 	if (flags & HAL_TXDESC_NOACK)		/* NB: avoid double counting */
1626b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_noack++;
1627b8e788a5SAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
1628b8e788a5SAdrian Chadd 	if (sc->sc_tdma && (flags & HAL_TXDESC_NOACK) == 0) {
1629b8e788a5SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_TDMA,
1630b8e788a5SAdrian Chadd 		    "%s: discard frame, ACK required w/ TDMA\n", __func__);
1631b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tdma_ack++;
1632b8e788a5SAdrian Chadd 		ath_freetx(m0);
1633b8e788a5SAdrian Chadd 		return EIO;
1634b8e788a5SAdrian Chadd 	}
1635b8e788a5SAdrian Chadd #endif
1636b8e788a5SAdrian Chadd 
1637b8e788a5SAdrian Chadd 	/*
1638eb6f0de0SAdrian Chadd 	 * Determine if a tx interrupt should be generated for
1639eb6f0de0SAdrian Chadd 	 * this descriptor.  We take a tx interrupt to reap
1640eb6f0de0SAdrian Chadd 	 * descriptors when the h/w hits an EOL condition or
1641eb6f0de0SAdrian Chadd 	 * when the descriptor is specifically marked to generate
1642eb6f0de0SAdrian Chadd 	 * an interrupt.  We periodically mark descriptors in this
1643eb6f0de0SAdrian Chadd 	 * way to insure timely replenishing of the supply needed
1644eb6f0de0SAdrian Chadd 	 * for sending frames.  Defering interrupts reduces system
1645eb6f0de0SAdrian Chadd 	 * load and potentially allows more concurrent work to be
1646eb6f0de0SAdrian Chadd 	 * done but if done to aggressively can cause senders to
1647eb6f0de0SAdrian Chadd 	 * backup.
1648eb6f0de0SAdrian Chadd 	 *
1649eb6f0de0SAdrian Chadd 	 * NB: use >= to deal with sc_txintrperiod changing
1650eb6f0de0SAdrian Chadd 	 *     dynamically through sysctl.
1651b8e788a5SAdrian Chadd 	 */
1652eb6f0de0SAdrian Chadd 	if (flags & HAL_TXDESC_INTREQ) {
1653eb6f0de0SAdrian Chadd 		txq->axq_intrcnt = 0;
1654eb6f0de0SAdrian Chadd 	} else if (++txq->axq_intrcnt >= sc->sc_txintrperiod) {
1655eb6f0de0SAdrian Chadd 		flags |= HAL_TXDESC_INTREQ;
1656eb6f0de0SAdrian Chadd 		txq->axq_intrcnt = 0;
1657eb6f0de0SAdrian Chadd 	}
1658e42b5dbaSAdrian Chadd 
1659eb6f0de0SAdrian Chadd 	/* This point forward is actual TX bits */
1660b8e788a5SAdrian Chadd 
1661b8e788a5SAdrian Chadd 	/*
1662b8e788a5SAdrian Chadd 	 * At this point we are committed to sending the frame
1663b8e788a5SAdrian Chadd 	 * and we don't need to look at m_nextpkt; clear it in
1664b8e788a5SAdrian Chadd 	 * case this frame is part of frag chain.
1665b8e788a5SAdrian Chadd 	 */
1666b8e788a5SAdrian Chadd 	m0->m_nextpkt = NULL;
1667b8e788a5SAdrian Chadd 
1668b8e788a5SAdrian Chadd 	if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
1669b8e788a5SAdrian Chadd 		ieee80211_dump_pkt(ic, mtod(m0, const uint8_t *), m0->m_len,
1670b8e788a5SAdrian Chadd 		    sc->sc_hwmap[rix].ieeerate, -1);
1671b8e788a5SAdrian Chadd 
1672b8e788a5SAdrian Chadd 	if (ieee80211_radiotap_active_vap(vap)) {
1673b8e788a5SAdrian Chadd 		u_int64_t tsf = ath_hal_gettsf64(ah);
1674b8e788a5SAdrian Chadd 
1675b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_tsf = htole64(tsf);
1676b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
1677b8e788a5SAdrian Chadd 		if (iswep)
1678b8e788a5SAdrian Chadd 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
1679b8e788a5SAdrian Chadd 		if (isfrag)
1680b8e788a5SAdrian Chadd 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
1681b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
1682b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_txpower = ni->ni_txpower;
1683b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
1684b8e788a5SAdrian Chadd 
1685b8e788a5SAdrian Chadd 		ieee80211_radiotap_tx(vap, m0);
1686b8e788a5SAdrian Chadd 	}
1687b8e788a5SAdrian Chadd 
1688eb6f0de0SAdrian Chadd 	/* Blank the legacy rate array */
1689eb6f0de0SAdrian Chadd 	bzero(&bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
1690c1782ce0SAdrian Chadd 
1691b8e788a5SAdrian Chadd 	/*
1692eb6f0de0SAdrian Chadd 	 * ath_buf_set_rate needs at least one rate/try to setup
1693eb6f0de0SAdrian Chadd 	 * the rate scenario.
1694b8e788a5SAdrian Chadd 	 */
1695eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].rix = rix;
1696eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].tries = try0;
1697eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].ratecode = txrate;
1698eb6f0de0SAdrian Chadd 
1699eb6f0de0SAdrian Chadd 	/* Store the decided rate index values away */
1700eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_pktlen = pktlen;
1701eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_hdrlen = hdrlen;
1702eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_atype = atype;
1703eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txpower = ni->ni_txpower;
1704eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txrate0 = txrate;
1705eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_try0 = try0;
1706eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_keyix = keyix;
1707eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txantenna = sc->sc_txantenna;
1708875a9451SAdrian Chadd 	bf->bf_state.bfs_txflags = flags;
1709eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_shpream = shortPreamble;
1710eb6f0de0SAdrian Chadd 
1711eb6f0de0SAdrian Chadd 	/* XXX this should be done in ath_tx_setrate() */
1712eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsrate0 = 0;	/* ie, no hard-coded ctsrate */
1713eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsrate = 0;	/* calculated later */
1714eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsduration = 0;
1715eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ismrr = ismrr;
1716eb6f0de0SAdrian Chadd 
1717eb6f0de0SAdrian Chadd 	return 0;
1718eb6f0de0SAdrian Chadd }
1719eb6f0de0SAdrian Chadd 
1720b8e788a5SAdrian Chadd /*
17214e81f27cSAdrian Chadd  * Queue a frame to the hardware or software queue.
1722eb6f0de0SAdrian Chadd  *
1723eb6f0de0SAdrian Chadd  * This can be called by the net80211 code.
1724eb6f0de0SAdrian Chadd  *
1725eb6f0de0SAdrian Chadd  * XXX what about locking? Or, push the seqno assign into the
1726eb6f0de0SAdrian Chadd  * XXX aggregate scheduler so its serialised?
17274e81f27cSAdrian Chadd  *
17284e81f27cSAdrian Chadd  * XXX When sending management frames via ath_raw_xmit(),
17294e81f27cSAdrian Chadd  *     should CLRDMASK be set unconditionally?
1730b8e788a5SAdrian Chadd  */
1731eb6f0de0SAdrian Chadd int
1732eb6f0de0SAdrian Chadd ath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni,
1733eb6f0de0SAdrian Chadd     struct ath_buf *bf, struct mbuf *m0)
1734eb6f0de0SAdrian Chadd {
1735eb6f0de0SAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
1736eb6f0de0SAdrian Chadd 	struct ath_vap *avp = ATH_VAP(vap);
17379c85ff91SAdrian Chadd 	int r = 0;
1738eb6f0de0SAdrian Chadd 	u_int pri;
1739eb6f0de0SAdrian Chadd 	int tid;
1740eb6f0de0SAdrian Chadd 	struct ath_txq *txq;
1741eb6f0de0SAdrian Chadd 	int ismcast;
1742eb6f0de0SAdrian Chadd 	const struct ieee80211_frame *wh;
1743eb6f0de0SAdrian Chadd 	int is_ampdu, is_ampdu_tx, is_ampdu_pending;
1744a108d2d6SAdrian Chadd 	ieee80211_seq seqno;
1745eb6f0de0SAdrian Chadd 	uint8_t type, subtype;
1746eb6f0de0SAdrian Chadd 
1747eb6f0de0SAdrian Chadd 	/*
1748eb6f0de0SAdrian Chadd 	 * Determine the target hardware queue.
1749eb6f0de0SAdrian Chadd 	 *
1750b43facbfSAdrian Chadd 	 * For multicast frames, the txq gets overridden appropriately
1751b43facbfSAdrian Chadd 	 * depending upon the state of PS.
1752eb6f0de0SAdrian Chadd 	 *
1753eb6f0de0SAdrian Chadd 	 * For any other frame, we do a TID/QoS lookup inside the frame
1754eb6f0de0SAdrian Chadd 	 * to see what the TID should be. If it's a non-QoS frame, the
1755eb6f0de0SAdrian Chadd 	 * AC and TID are overridden. The TID/TXQ code assumes the
1756eb6f0de0SAdrian Chadd 	 * TID is on a predictable hardware TXQ, so we don't support
1757eb6f0de0SAdrian Chadd 	 * having a node TID queued to multiple hardware TXQs.
1758eb6f0de0SAdrian Chadd 	 * This may change in the future but would require some locking
1759eb6f0de0SAdrian Chadd 	 * fudgery.
1760eb6f0de0SAdrian Chadd 	 */
1761eb6f0de0SAdrian Chadd 	pri = ath_tx_getac(sc, m0);
1762eb6f0de0SAdrian Chadd 	tid = ath_tx_gettid(sc, m0);
1763eb6f0de0SAdrian Chadd 
1764eb6f0de0SAdrian Chadd 	txq = sc->sc_ac2q[pri];
1765eb6f0de0SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1766eb6f0de0SAdrian Chadd 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1767eb6f0de0SAdrian Chadd 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1768eb6f0de0SAdrian Chadd 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1769eb6f0de0SAdrian Chadd 
17709c85ff91SAdrian Chadd 	/*
17719c85ff91SAdrian Chadd 	 * Enforce how deep the multicast queue can grow.
17729c85ff91SAdrian Chadd 	 *
17739c85ff91SAdrian Chadd 	 * XXX duplicated in ath_raw_xmit().
17749c85ff91SAdrian Chadd 	 */
17759c85ff91SAdrian Chadd 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
17769c85ff91SAdrian Chadd 		ATH_TXQ_LOCK(sc->sc_cabq);
17779c85ff91SAdrian Chadd 
1778b09e37a1SAdrian Chadd 		if (sc->sc_cabq->axq_depth > sc->sc_txq_mcastq_maxdepth) {
17799c85ff91SAdrian Chadd 			sc->sc_stats.ast_tx_mcastq_overflow++;
17809c85ff91SAdrian Chadd 			r = ENOBUFS;
17819c85ff91SAdrian Chadd 		}
17829c85ff91SAdrian Chadd 
17839c85ff91SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_cabq);
17849c85ff91SAdrian Chadd 
17859c85ff91SAdrian Chadd 		if (r != 0) {
17869c85ff91SAdrian Chadd 			m_freem(m0);
17879c85ff91SAdrian Chadd 			return r;
17889c85ff91SAdrian Chadd 		}
17899c85ff91SAdrian Chadd 	}
17909c85ff91SAdrian Chadd 
1791eb6f0de0SAdrian Chadd 	/* A-MPDU TX */
1792eb6f0de0SAdrian Chadd 	is_ampdu_tx = ath_tx_ampdu_running(sc, ATH_NODE(ni), tid);
1793eb6f0de0SAdrian Chadd 	is_ampdu_pending = ath_tx_ampdu_pending(sc, ATH_NODE(ni), tid);
1794eb6f0de0SAdrian Chadd 	is_ampdu = is_ampdu_tx | is_ampdu_pending;
1795eb6f0de0SAdrian Chadd 
1796a108d2d6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d, ac=%d, is_ampdu=%d\n",
1797a108d2d6SAdrian Chadd 	    __func__, tid, pri, is_ampdu);
1798eb6f0de0SAdrian Chadd 
179946634305SAdrian Chadd 	/* Set local packet state, used to queue packets to hardware */
180046634305SAdrian Chadd 	bf->bf_state.bfs_tid = tid;
180146634305SAdrian Chadd 	bf->bf_state.bfs_txq = txq;
180246634305SAdrian Chadd 	bf->bf_state.bfs_pri = pri;
180346634305SAdrian Chadd 
1804c5940c30SAdrian Chadd 	/*
1805b43facbfSAdrian Chadd 	 * When servicing one or more stations in power-save mode
1806b43facbfSAdrian Chadd 	 * (or) if there is some mcast data waiting on the mcast
1807b43facbfSAdrian Chadd 	 * queue (to prevent out of order delivery) multicast frames
1808b43facbfSAdrian Chadd 	 * must be bufferd until after the beacon.
1809b43facbfSAdrian Chadd 	 *
1810b43facbfSAdrian Chadd 	 * TODO: we should lock the mcastq before we check the length.
1811c5940c30SAdrian Chadd 	 */
181246634305SAdrian Chadd 	if (ismcast && (vap->iv_ps_sta || avp->av_mcastq.axq_depth)) {
1813eb6f0de0SAdrian Chadd 		txq = &avp->av_mcastq;
181446634305SAdrian Chadd 		/*
181546634305SAdrian Chadd 		 * Mark the frame as eventually belonging on the CAB
181646634305SAdrian Chadd 		 * queue, so the descriptor setup functions will
181746634305SAdrian Chadd 		 * correctly initialise the descriptor 'qcuId' field.
181846634305SAdrian Chadd 		 */
181946634305SAdrian Chadd 		bf->bf_state.bfs_txq = sc->sc_cabq;
182046634305SAdrian Chadd 	}
1821eb6f0de0SAdrian Chadd 
1822eb6f0de0SAdrian Chadd 	/* Do the generic frame setup */
1823eb6f0de0SAdrian Chadd 	/* XXX should just bzero the bf_state? */
1824eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_dobaw = 0;
1825eb6f0de0SAdrian Chadd 
18267561cb5cSAdrian Chadd 	/*
18277561cb5cSAdrian Chadd 	 * Acquire the TXQ lock early, so both the encap and seqno
18287561cb5cSAdrian Chadd 	 * are allocated together.
182946634305SAdrian Chadd 	 *
183046634305SAdrian Chadd 	 * XXX should TXQ for CABQ traffic be the multicast queue,
183146634305SAdrian Chadd 	 * or the TXQ the given PRI would allocate from? (eg for
183246634305SAdrian Chadd 	 * sequence number allocation locking.)
18337561cb5cSAdrian Chadd 	 */
1834eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(txq);
18357561cb5cSAdrian Chadd 
18367561cb5cSAdrian Chadd 	/* A-MPDU TX? Manually set sequence number */
18377561cb5cSAdrian Chadd 	/*
18387561cb5cSAdrian Chadd 	 * Don't do it whilst pending; the net80211 layer still
18397561cb5cSAdrian Chadd 	 * assigns them.
18407561cb5cSAdrian Chadd 	 */
18417561cb5cSAdrian Chadd 	if (is_ampdu_tx) {
1842eb6f0de0SAdrian Chadd 		/*
1843eb6f0de0SAdrian Chadd 		 * Always call; this function will
1844eb6f0de0SAdrian Chadd 		 * handle making sure that null data frames
1845eb6f0de0SAdrian Chadd 		 * don't get a sequence number from the current
1846eb6f0de0SAdrian Chadd 		 * TID and thus mess with the BAW.
1847eb6f0de0SAdrian Chadd 		 */
1848a108d2d6SAdrian Chadd 		seqno = ath_tx_tid_seqno_assign(sc, ni, bf, m0);
184942f4d061SAdrian Chadd 
185042f4d061SAdrian Chadd 		/*
185142f4d061SAdrian Chadd 		 * Don't add QoS NULL frames to the BAW.
185242f4d061SAdrian Chadd 		 */
1853a108d2d6SAdrian Chadd 		if (IEEE80211_QOS_HAS_SEQ(wh) &&
1854a108d2d6SAdrian Chadd 		    subtype != IEEE80211_FC0_SUBTYPE_QOS_NULL) {
1855eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_dobaw = 1;
1856eb6f0de0SAdrian Chadd 		}
1857c1782ce0SAdrian Chadd 	}
1858c1782ce0SAdrian Chadd 
1859eb6f0de0SAdrian Chadd 	/*
1860eb6f0de0SAdrian Chadd 	 * If needed, the sequence number has been assigned.
1861eb6f0de0SAdrian Chadd 	 * Squirrel it away somewhere easy to get to.
1862eb6f0de0SAdrian Chadd 	 */
1863a108d2d6SAdrian Chadd 	bf->bf_state.bfs_seqno = M_SEQNO_GET(m0) << IEEE80211_SEQ_SEQ_SHIFT;
1864b8e788a5SAdrian Chadd 
1865eb6f0de0SAdrian Chadd 	/* Is ampdu pending? fetch the seqno and print it out */
1866eb6f0de0SAdrian Chadd 	if (is_ampdu_pending)
1867eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX,
1868eb6f0de0SAdrian Chadd 		    "%s: tid %d: ampdu pending, seqno %d\n",
1869eb6f0de0SAdrian Chadd 		    __func__, tid, M_SEQNO_GET(m0));
1870eb6f0de0SAdrian Chadd 
1871eb6f0de0SAdrian Chadd 	/* This also sets up the DMA map */
1872b43facbfSAdrian Chadd 	r = ath_tx_normal_setup(sc, ni, bf, m0, txq);
1873eb6f0de0SAdrian Chadd 
1874eb6f0de0SAdrian Chadd 	if (r != 0)
18757561cb5cSAdrian Chadd 		goto done;
1876eb6f0de0SAdrian Chadd 
1877eb6f0de0SAdrian Chadd 	/* At this point m0 could have changed! */
1878eb6f0de0SAdrian Chadd 	m0 = bf->bf_m;
1879eb6f0de0SAdrian Chadd 
1880eb6f0de0SAdrian Chadd #if 1
1881eb6f0de0SAdrian Chadd 	/*
1882eb6f0de0SAdrian Chadd 	 * If it's a multicast frame, do a direct-dispatch to the
1883eb6f0de0SAdrian Chadd 	 * destination hardware queue. Don't bother software
1884eb6f0de0SAdrian Chadd 	 * queuing it.
1885eb6f0de0SAdrian Chadd 	 */
1886eb6f0de0SAdrian Chadd 	/*
1887eb6f0de0SAdrian Chadd 	 * If it's a BAR frame, do a direct dispatch to the
1888eb6f0de0SAdrian Chadd 	 * destination hardware queue. Don't bother software
1889eb6f0de0SAdrian Chadd 	 * queuing it, as the TID will now be paused.
1890eb6f0de0SAdrian Chadd 	 * Sending a BAR frame can occur from the net80211 txa timer
1891eb6f0de0SAdrian Chadd 	 * (ie, retries) or from the ath txtask (completion call.)
1892eb6f0de0SAdrian Chadd 	 * It queues directly to hardware because the TID is paused
1893eb6f0de0SAdrian Chadd 	 * at this point (and won't be unpaused until the BAR has
1894eb6f0de0SAdrian Chadd 	 * either been TXed successfully or max retries has been
1895eb6f0de0SAdrian Chadd 	 * reached.)
1896eb6f0de0SAdrian Chadd 	 */
1897eb6f0de0SAdrian Chadd 	if (txq == &avp->av_mcastq) {
1898d3a6425bSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX,
18990b96ef63SAdrian Chadd 		    "%s: bf=%p: mcastq: TX'ing\n", __func__, bf);
19004e81f27cSAdrian Chadd 		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
1901eb6f0de0SAdrian Chadd 		ath_tx_xmit_normal(sc, txq, bf);
1902eb6f0de0SAdrian Chadd 	} else if (type == IEEE80211_FC0_TYPE_CTL &&
1903eb6f0de0SAdrian Chadd 		    subtype == IEEE80211_FC0_SUBTYPE_BAR) {
1904d3a6425bSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX,
1905eb6f0de0SAdrian Chadd 		    "%s: BAR: TX'ing direct\n", __func__);
19064e81f27cSAdrian Chadd 		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
1907eb6f0de0SAdrian Chadd 		ath_tx_xmit_normal(sc, txq, bf);
1908eb6f0de0SAdrian Chadd 	} else {
1909eb6f0de0SAdrian Chadd 		/* add to software queue */
1910d3a6425bSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX,
19110b96ef63SAdrian Chadd 		    "%s: bf=%p: swq: TX'ing\n", __func__, bf);
1912eb6f0de0SAdrian Chadd 		ath_tx_swq(sc, ni, txq, bf);
1913eb6f0de0SAdrian Chadd 	}
1914eb6f0de0SAdrian Chadd #else
1915eb6f0de0SAdrian Chadd 	/*
1916eb6f0de0SAdrian Chadd 	 * For now, since there's no software queue,
1917eb6f0de0SAdrian Chadd 	 * direct-dispatch to the hardware.
1918eb6f0de0SAdrian Chadd 	 */
19194e81f27cSAdrian Chadd 	bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
1920eb6f0de0SAdrian Chadd 	ath_tx_xmit_normal(sc, txq, bf);
1921eb6f0de0SAdrian Chadd #endif
19227561cb5cSAdrian Chadd done:
19237561cb5cSAdrian Chadd 	ATH_TXQ_UNLOCK(txq);
1924eb6f0de0SAdrian Chadd 
1925b8e788a5SAdrian Chadd 	return 0;
1926b8e788a5SAdrian Chadd }
1927b8e788a5SAdrian Chadd 
1928b8e788a5SAdrian Chadd static int
1929b8e788a5SAdrian Chadd ath_tx_raw_start(struct ath_softc *sc, struct ieee80211_node *ni,
1930b8e788a5SAdrian Chadd 	struct ath_buf *bf, struct mbuf *m0,
1931b8e788a5SAdrian Chadd 	const struct ieee80211_bpf_params *params)
1932b8e788a5SAdrian Chadd {
1933b8e788a5SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
1934b8e788a5SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
1935b8e788a5SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
1936b8e788a5SAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
1937b8e788a5SAdrian Chadd 	int error, ismcast, ismrr;
1938b8e788a5SAdrian Chadd 	int keyix, hdrlen, pktlen, try0, txantenna;
1939eb6f0de0SAdrian Chadd 	u_int8_t rix, txrate;
1940b8e788a5SAdrian Chadd 	struct ieee80211_frame *wh;
1941eb6f0de0SAdrian Chadd 	u_int flags;
1942b8e788a5SAdrian Chadd 	HAL_PKT_TYPE atype;
1943b8e788a5SAdrian Chadd 	const HAL_RATE_TABLE *rt;
1944b8e788a5SAdrian Chadd 	struct ath_desc *ds;
1945b8e788a5SAdrian Chadd 	u_int pri;
1946eb6f0de0SAdrian Chadd 	int o_tid = -1;
1947eb6f0de0SAdrian Chadd 	int do_override;
1948b8e788a5SAdrian Chadd 
1949b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1950b8e788a5SAdrian Chadd 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1951b8e788a5SAdrian Chadd 	hdrlen = ieee80211_anyhdrsize(wh);
1952b8e788a5SAdrian Chadd 	/*
1953b8e788a5SAdrian Chadd 	 * Packet length must not include any
1954b8e788a5SAdrian Chadd 	 * pad bytes; deduct them here.
1955b8e788a5SAdrian Chadd 	 */
1956b8e788a5SAdrian Chadd 	/* XXX honor IEEE80211_BPF_DATAPAD */
1957b8e788a5SAdrian Chadd 	pktlen = m0->m_pkthdr.len - (hdrlen & 3) + IEEE80211_CRC_LEN;
1958b8e788a5SAdrian Chadd 
195903682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 2,
196003682514SAdrian Chadd 	     "ath_tx_raw_start: ni=%p, bf=%p, raw", ni, bf);
196103682514SAdrian Chadd 
1962eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: ismcast=%d\n",
1963eb6f0de0SAdrian Chadd 	    __func__, ismcast);
1964eb6f0de0SAdrian Chadd 
19657561cb5cSAdrian Chadd 	pri = params->ibp_pri & 3;
19667561cb5cSAdrian Chadd 	/* Override pri if the frame isn't a QoS one */
19677561cb5cSAdrian Chadd 	if (! IEEE80211_QOS_HAS_SEQ(wh))
19687561cb5cSAdrian Chadd 		pri = ath_tx_getac(sc, m0);
19697561cb5cSAdrian Chadd 
19707561cb5cSAdrian Chadd 	/* XXX If it's an ADDBA, override the correct queue */
19717561cb5cSAdrian Chadd 	do_override = ath_tx_action_frame_override_queue(sc, ni, m0, &o_tid);
19727561cb5cSAdrian Chadd 
19737561cb5cSAdrian Chadd 	/* Map ADDBA to the correct priority */
19747561cb5cSAdrian Chadd 	if (do_override) {
19757561cb5cSAdrian Chadd #if 0
19767561cb5cSAdrian Chadd 		device_printf(sc->sc_dev,
19777561cb5cSAdrian Chadd 		    "%s: overriding tid %d pri %d -> %d\n",
19787561cb5cSAdrian Chadd 		    __func__, o_tid, pri, TID_TO_WME_AC(o_tid));
19797561cb5cSAdrian Chadd #endif
19807561cb5cSAdrian Chadd 		pri = TID_TO_WME_AC(o_tid);
19817561cb5cSAdrian Chadd 	}
19827561cb5cSAdrian Chadd 
19837561cb5cSAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[pri]);
19847561cb5cSAdrian Chadd 
198581a82688SAdrian Chadd 	/* Handle encryption twiddling if needed */
1986eb6f0de0SAdrian Chadd 	if (! ath_tx_tag_crypto(sc, ni,
1987eb6f0de0SAdrian Chadd 	    m0, params->ibp_flags & IEEE80211_BPF_CRYPTO, 0,
1988eb6f0de0SAdrian Chadd 	    &hdrlen, &pktlen, &keyix)) {
1989b8e788a5SAdrian Chadd 		ath_freetx(m0);
1990b8e788a5SAdrian Chadd 		return EIO;
1991b8e788a5SAdrian Chadd 	}
1992b8e788a5SAdrian Chadd 	/* packet header may have moved, reset our local pointer */
1993b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1994b8e788a5SAdrian Chadd 
1995eb6f0de0SAdrian Chadd 	/* Do the generic frame setup */
1996eb6f0de0SAdrian Chadd 	/* XXX should just bzero the bf_state? */
1997eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_dobaw = 0;
1998eb6f0de0SAdrian Chadd 
1999b8e788a5SAdrian Chadd 	error = ath_tx_dmasetup(sc, bf, m0);
2000b8e788a5SAdrian Chadd 	if (error != 0)
2001b8e788a5SAdrian Chadd 		return error;
2002b8e788a5SAdrian Chadd 	m0 = bf->bf_m;				/* NB: may have changed */
2003b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
2004b8e788a5SAdrian Chadd 	bf->bf_node = ni;			/* NB: held reference */
2005b8e788a5SAdrian Chadd 
20064e81f27cSAdrian Chadd 	/* Always enable CLRDMASK for raw frames for now.. */
2007b8e788a5SAdrian Chadd 	flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for crypto errs */
2008b8e788a5SAdrian Chadd 	flags |= HAL_TXDESC_INTREQ;		/* force interrupt */
2009b8e788a5SAdrian Chadd 	if (params->ibp_flags & IEEE80211_BPF_RTS)
2010b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_RTSENA;
2011eb6f0de0SAdrian Chadd 	else if (params->ibp_flags & IEEE80211_BPF_CTS) {
2012eb6f0de0SAdrian Chadd 		/* XXX assume 11g/11n protection? */
2013eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_doprot = 1;
2014b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_CTSENA;
2015eb6f0de0SAdrian Chadd 	}
2016b8e788a5SAdrian Chadd 	/* XXX leave ismcast to injector? */
2017b8e788a5SAdrian Chadd 	if ((params->ibp_flags & IEEE80211_BPF_NOACK) || ismcast)
2018b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_NOACK;
2019b8e788a5SAdrian Chadd 
2020b8e788a5SAdrian Chadd 	rt = sc->sc_currates;
2021b8e788a5SAdrian Chadd 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
2022b8e788a5SAdrian Chadd 	rix = ath_tx_findrix(sc, params->ibp_rate0);
2023b8e788a5SAdrian Chadd 	txrate = rt->info[rix].rateCode;
2024b8e788a5SAdrian Chadd 	if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
2025b8e788a5SAdrian Chadd 		txrate |= rt->info[rix].shortPreamble;
2026b8e788a5SAdrian Chadd 	sc->sc_txrix = rix;
2027b8e788a5SAdrian Chadd 	try0 = params->ibp_try0;
2028b8e788a5SAdrian Chadd 	ismrr = (params->ibp_try1 != 0);
2029b8e788a5SAdrian Chadd 	txantenna = params->ibp_pri >> 2;
2030b8e788a5SAdrian Chadd 	if (txantenna == 0)			/* XXX? */
2031b8e788a5SAdrian Chadd 		txantenna = sc->sc_txantenna;
203279f02dbfSAdrian Chadd 
203379f02dbfSAdrian Chadd 	/*
2034eb6f0de0SAdrian Chadd 	 * Since ctsrate is fixed, store it away for later
2035eb6f0de0SAdrian Chadd 	 * use when the descriptor fields are being set.
203679f02dbfSAdrian Chadd 	 */
2037eb6f0de0SAdrian Chadd 	if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA))
2038eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_ctsrate0 = params->ibp_ctsrate;
203979f02dbfSAdrian Chadd 
2040b8e788a5SAdrian Chadd 	/*
2041b8e788a5SAdrian Chadd 	 * NB: we mark all packets as type PSPOLL so the h/w won't
2042b8e788a5SAdrian Chadd 	 * set the sequence number, duration, etc.
2043b8e788a5SAdrian Chadd 	 */
2044b8e788a5SAdrian Chadd 	atype = HAL_PKT_TYPE_PSPOLL;
2045b8e788a5SAdrian Chadd 
2046b8e788a5SAdrian Chadd 	if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
2047b8e788a5SAdrian Chadd 		ieee80211_dump_pkt(ic, mtod(m0, caddr_t), m0->m_len,
2048b8e788a5SAdrian Chadd 		    sc->sc_hwmap[rix].ieeerate, -1);
2049b8e788a5SAdrian Chadd 
2050b8e788a5SAdrian Chadd 	if (ieee80211_radiotap_active_vap(vap)) {
2051b8e788a5SAdrian Chadd 		u_int64_t tsf = ath_hal_gettsf64(ah);
2052b8e788a5SAdrian Chadd 
2053b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_tsf = htole64(tsf);
2054b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
2055b8e788a5SAdrian Chadd 		if (wh->i_fc[1] & IEEE80211_FC1_WEP)
2056b8e788a5SAdrian Chadd 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2057b8e788a5SAdrian Chadd 		if (m0->m_flags & M_FRAG)
2058b8e788a5SAdrian Chadd 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
2059b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
2060b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_txpower = ni->ni_txpower;
2061b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
2062b8e788a5SAdrian Chadd 
2063b8e788a5SAdrian Chadd 		ieee80211_radiotap_tx(vap, m0);
2064b8e788a5SAdrian Chadd 	}
2065b8e788a5SAdrian Chadd 
2066b8e788a5SAdrian Chadd 	/*
2067b8e788a5SAdrian Chadd 	 * Formulate first tx descriptor with tx controls.
2068b8e788a5SAdrian Chadd 	 */
2069b8e788a5SAdrian Chadd 	ds = bf->bf_desc;
2070b8e788a5SAdrian Chadd 	/* XXX check return value? */
2071eb6f0de0SAdrian Chadd 
2072eb6f0de0SAdrian Chadd 	/* Store the decided rate index values away */
2073eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_pktlen = pktlen;
2074eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_hdrlen = hdrlen;
2075eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_atype = atype;
2076eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txpower = params->ibp_power;
2077eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txrate0 = txrate;
2078eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_try0 = try0;
2079eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_keyix = keyix;
2080eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txantenna = txantenna;
2081875a9451SAdrian Chadd 	bf->bf_state.bfs_txflags = flags;
2082eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_shpream =
2083eb6f0de0SAdrian Chadd 	    !! (params->ibp_flags & IEEE80211_BPF_SHORTPRE);
2084b8e788a5SAdrian Chadd 
208546634305SAdrian Chadd 	/* Set local packet state, used to queue packets to hardware */
208646634305SAdrian Chadd 	bf->bf_state.bfs_tid = WME_AC_TO_TID(pri);
208746634305SAdrian Chadd 	bf->bf_state.bfs_txq = sc->sc_ac2q[pri];
208846634305SAdrian Chadd 	bf->bf_state.bfs_pri = pri;
208946634305SAdrian Chadd 
2090eb6f0de0SAdrian Chadd 	/* XXX this should be done in ath_tx_setrate() */
2091eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsrate = 0;
2092eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsduration = 0;
2093eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ismrr = ismrr;
2094eb6f0de0SAdrian Chadd 
2095eb6f0de0SAdrian Chadd 	/* Blank the legacy rate array */
2096eb6f0de0SAdrian Chadd 	bzero(&bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
2097eb6f0de0SAdrian Chadd 
2098eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].rix =
2099eb6f0de0SAdrian Chadd 	    ath_tx_findrix(sc, params->ibp_rate0);
2100eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].tries = try0;
2101eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].ratecode = txrate;
2102c1782ce0SAdrian Chadd 
2103c1782ce0SAdrian Chadd 	if (ismrr) {
2104eb6f0de0SAdrian Chadd 		int rix;
2105c1782ce0SAdrian Chadd 
2106b8e788a5SAdrian Chadd 		rix = ath_tx_findrix(sc, params->ibp_rate1);
2107eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[1].rix = rix;
2108eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[1].tries = params->ibp_try1;
2109c1782ce0SAdrian Chadd 
2110eb6f0de0SAdrian Chadd 		rix = ath_tx_findrix(sc, params->ibp_rate2);
2111eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[2].rix = rix;
2112eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[2].tries = params->ibp_try2;
2113eb6f0de0SAdrian Chadd 
2114eb6f0de0SAdrian Chadd 		rix = ath_tx_findrix(sc, params->ibp_rate3);
2115eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[3].rix = rix;
2116eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[3].tries = params->ibp_try3;
2117c1782ce0SAdrian Chadd 	}
2118eb6f0de0SAdrian Chadd 	/*
2119eb6f0de0SAdrian Chadd 	 * All the required rate control decisions have been made;
2120eb6f0de0SAdrian Chadd 	 * fill in the rc flags.
2121eb6f0de0SAdrian Chadd 	 */
2122eb6f0de0SAdrian Chadd 	ath_tx_rate_fill_rcflags(sc, bf);
2123b8e788a5SAdrian Chadd 
2124b8e788a5SAdrian Chadd 	/* NB: no buffered multicast in power save support */
2125eb6f0de0SAdrian Chadd 
2126eb6f0de0SAdrian Chadd 	/*
2127eb6f0de0SAdrian Chadd 	 * If we're overiding the ADDBA destination, dump directly
2128eb6f0de0SAdrian Chadd 	 * into the hardware queue, right after any pending
2129eb6f0de0SAdrian Chadd 	 * frames to that node are.
2130eb6f0de0SAdrian Chadd 	 */
2131eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: dooverride=%d\n",
2132eb6f0de0SAdrian Chadd 	    __func__, do_override);
2133eb6f0de0SAdrian Chadd 
213494eefcf1SAdrian Chadd #if 1
2135eb6f0de0SAdrian Chadd 	if (do_override) {
21364e81f27cSAdrian Chadd 		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
2137eb6f0de0SAdrian Chadd 		ath_tx_xmit_normal(sc, sc->sc_ac2q[pri], bf);
2138eb6f0de0SAdrian Chadd 	} else {
2139eb6f0de0SAdrian Chadd 		/* Queue to software queue */
2140eb6f0de0SAdrian Chadd 		ath_tx_swq(sc, ni, sc->sc_ac2q[pri], bf);
2141eb6f0de0SAdrian Chadd 	}
214294eefcf1SAdrian Chadd #else
214394eefcf1SAdrian Chadd 	/* Direct-dispatch to the hardware */
214494eefcf1SAdrian Chadd 	bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
214594eefcf1SAdrian Chadd 	ath_tx_xmit_normal(sc, sc->sc_ac2q[pri], bf);
214694eefcf1SAdrian Chadd #endif
21477561cb5cSAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[pri]);
2148eb6f0de0SAdrian Chadd 
2149b8e788a5SAdrian Chadd 	return 0;
2150b8e788a5SAdrian Chadd }
2151b8e788a5SAdrian Chadd 
2152eb6f0de0SAdrian Chadd /*
2153eb6f0de0SAdrian Chadd  * Send a raw frame.
2154eb6f0de0SAdrian Chadd  *
2155eb6f0de0SAdrian Chadd  * This can be called by net80211.
2156eb6f0de0SAdrian Chadd  */
2157b8e788a5SAdrian Chadd int
2158b8e788a5SAdrian Chadd ath_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2159b8e788a5SAdrian Chadd 	const struct ieee80211_bpf_params *params)
2160b8e788a5SAdrian Chadd {
2161b8e788a5SAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
2162b8e788a5SAdrian Chadd 	struct ifnet *ifp = ic->ic_ifp;
2163b8e788a5SAdrian Chadd 	struct ath_softc *sc = ifp->if_softc;
2164b8e788a5SAdrian Chadd 	struct ath_buf *bf;
21659c85ff91SAdrian Chadd 	struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
21669c85ff91SAdrian Chadd 	int error = 0;
2167b8e788a5SAdrian Chadd 
2168ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2169ef27340cSAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
2170ef27340cSAdrian Chadd 		device_printf(sc->sc_dev, "%s: sc_inreset_cnt > 0; bailing\n",
2171ef27340cSAdrian Chadd 		    __func__);
2172ef27340cSAdrian Chadd 		error = EIO;
2173ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2174ef27340cSAdrian Chadd 		goto bad0;
2175ef27340cSAdrian Chadd 	}
2176ef27340cSAdrian Chadd 	sc->sc_txstart_cnt++;
2177ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2178ef27340cSAdrian Chadd 
21791b5c5f5aSAdrian Chadd 	ATH_TX_LOCK(sc);
21801b5c5f5aSAdrian Chadd 
2181b8e788a5SAdrian Chadd 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->sc_invalid) {
2182b8e788a5SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: discard frame, %s", __func__,
2183b8e788a5SAdrian Chadd 		    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ?
2184b8e788a5SAdrian Chadd 			"!running" : "invalid");
2185b8e788a5SAdrian Chadd 		m_freem(m);
2186b8e788a5SAdrian Chadd 		error = ENETDOWN;
2187b8e788a5SAdrian Chadd 		goto bad;
2188b8e788a5SAdrian Chadd 	}
21899c85ff91SAdrian Chadd 
21909c85ff91SAdrian Chadd 	/*
21919c85ff91SAdrian Chadd 	 * Enforce how deep the multicast queue can grow.
21929c85ff91SAdrian Chadd 	 *
21939c85ff91SAdrian Chadd 	 * XXX duplicated in ath_tx_start().
21949c85ff91SAdrian Chadd 	 */
21959c85ff91SAdrian Chadd 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
21969c85ff91SAdrian Chadd 		ATH_TXQ_LOCK(sc->sc_cabq);
21979c85ff91SAdrian Chadd 
2198b09e37a1SAdrian Chadd 		if (sc->sc_cabq->axq_depth > sc->sc_txq_mcastq_maxdepth) {
21999c85ff91SAdrian Chadd 			sc->sc_stats.ast_tx_mcastq_overflow++;
22009c85ff91SAdrian Chadd 			error = ENOBUFS;
22019c85ff91SAdrian Chadd 		}
22029c85ff91SAdrian Chadd 
22039c85ff91SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_cabq);
22049c85ff91SAdrian Chadd 
22059c85ff91SAdrian Chadd 		if (error != 0) {
22069c85ff91SAdrian Chadd 			m_freem(m);
22079c85ff91SAdrian Chadd 			goto bad;
22089c85ff91SAdrian Chadd 		}
22099c85ff91SAdrian Chadd 	}
22109c85ff91SAdrian Chadd 
2211b8e788a5SAdrian Chadd 	/*
2212b8e788a5SAdrian Chadd 	 * Grab a TX buffer and associated resources.
2213b8e788a5SAdrian Chadd 	 */
2214af33d486SAdrian Chadd 	bf = ath_getbuf(sc, ATH_BUFTYPE_MGMT);
2215b8e788a5SAdrian Chadd 	if (bf == NULL) {
2216b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_nobuf++;
2217b8e788a5SAdrian Chadd 		m_freem(m);
2218b8e788a5SAdrian Chadd 		error = ENOBUFS;
2219b8e788a5SAdrian Chadd 		goto bad;
2220b8e788a5SAdrian Chadd 	}
222103682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 3, "ath_raw_xmit: m=%p, params=%p, bf=%p\n",
222203682514SAdrian Chadd 	    m, params,  bf);
2223b8e788a5SAdrian Chadd 
2224b8e788a5SAdrian Chadd 	if (params == NULL) {
2225b8e788a5SAdrian Chadd 		/*
2226b8e788a5SAdrian Chadd 		 * Legacy path; interpret frame contents to decide
2227b8e788a5SAdrian Chadd 		 * precisely how to send the frame.
2228b8e788a5SAdrian Chadd 		 */
2229b8e788a5SAdrian Chadd 		if (ath_tx_start(sc, ni, bf, m)) {
2230b8e788a5SAdrian Chadd 			error = EIO;		/* XXX */
2231b8e788a5SAdrian Chadd 			goto bad2;
2232b8e788a5SAdrian Chadd 		}
2233b8e788a5SAdrian Chadd 	} else {
2234b8e788a5SAdrian Chadd 		/*
2235b8e788a5SAdrian Chadd 		 * Caller supplied explicit parameters to use in
2236b8e788a5SAdrian Chadd 		 * sending the frame.
2237b8e788a5SAdrian Chadd 		 */
2238b8e788a5SAdrian Chadd 		if (ath_tx_raw_start(sc, ni, bf, m, params)) {
2239b8e788a5SAdrian Chadd 			error = EIO;		/* XXX */
2240b8e788a5SAdrian Chadd 			goto bad2;
2241b8e788a5SAdrian Chadd 		}
2242b8e788a5SAdrian Chadd 	}
2243b8e788a5SAdrian Chadd 	sc->sc_wd_timer = 5;
2244b8e788a5SAdrian Chadd 	ifp->if_opackets++;
2245b8e788a5SAdrian Chadd 	sc->sc_stats.ast_tx_raw++;
2246b8e788a5SAdrian Chadd 
2247548a605dSAdrian Chadd 	/*
2248548a605dSAdrian Chadd 	 * Update the TIM - if there's anything queued to the
2249548a605dSAdrian Chadd 	 * software queue and power save is enabled, we should
2250548a605dSAdrian Chadd 	 * set the TIM.
2251548a605dSAdrian Chadd 	 */
2252548a605dSAdrian Chadd 	ath_tx_update_tim(sc, ni, 1);
2253548a605dSAdrian Chadd 
2254ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2255ef27340cSAdrian Chadd 	sc->sc_txstart_cnt--;
2256ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2257ef27340cSAdrian Chadd 
22581b5c5f5aSAdrian Chadd 	ATH_TX_UNLOCK(sc);
22591b5c5f5aSAdrian Chadd 
2260b8e788a5SAdrian Chadd 	return 0;
2261b8e788a5SAdrian Chadd bad2:
226203682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 3, "ath_raw_xmit: bad2: m=%p, params=%p, "
226303682514SAdrian Chadd 	    "bf=%p",
226403682514SAdrian Chadd 	    m,
226503682514SAdrian Chadd 	    params,
226603682514SAdrian Chadd 	    bf);
2267b8e788a5SAdrian Chadd 	ATH_TXBUF_LOCK(sc);
2268e1a50456SAdrian Chadd 	ath_returnbuf_head(sc, bf);
2269b8e788a5SAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
2270b8e788a5SAdrian Chadd bad:
22711b5c5f5aSAdrian Chadd 
22721b5c5f5aSAdrian Chadd 	ATH_TX_UNLOCK(sc);
22731b5c5f5aSAdrian Chadd 
2274ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2275ef27340cSAdrian Chadd 	sc->sc_txstart_cnt--;
2276ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2277ef27340cSAdrian Chadd bad0:
227803682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 2, "ath_raw_xmit: bad0: m=%p, params=%p",
227903682514SAdrian Chadd 	    m, params);
2280b8e788a5SAdrian Chadd 	ifp->if_oerrors++;
2281b8e788a5SAdrian Chadd 	sc->sc_stats.ast_tx_raw_fail++;
2282b8e788a5SAdrian Chadd 	ieee80211_free_node(ni);
2283ef27340cSAdrian Chadd 
2284b8e788a5SAdrian Chadd 	return error;
2285b8e788a5SAdrian Chadd }
2286eb6f0de0SAdrian Chadd 
2287eb6f0de0SAdrian Chadd /* Some helper functions */
2288eb6f0de0SAdrian Chadd 
2289eb6f0de0SAdrian Chadd /*
2290eb6f0de0SAdrian Chadd  * ADDBA (and potentially others) need to be placed in the same
2291eb6f0de0SAdrian Chadd  * hardware queue as the TID/node it's relating to. This is so
2292eb6f0de0SAdrian Chadd  * it goes out after any pending non-aggregate frames to the
2293eb6f0de0SAdrian Chadd  * same node/TID.
2294eb6f0de0SAdrian Chadd  *
2295eb6f0de0SAdrian Chadd  * If this isn't done, the ADDBA can go out before the frames
2296eb6f0de0SAdrian Chadd  * queued in hardware. Even though these frames have a sequence
2297eb6f0de0SAdrian Chadd  * number -earlier- than the ADDBA can be transmitted (but
2298eb6f0de0SAdrian Chadd  * no frames whose sequence numbers are after the ADDBA should
2299eb6f0de0SAdrian Chadd  * be!) they'll arrive after the ADDBA - and the receiving end
2300eb6f0de0SAdrian Chadd  * will simply drop them as being out of the BAW.
2301eb6f0de0SAdrian Chadd  *
2302eb6f0de0SAdrian Chadd  * The frames can't be appended to the TID software queue - it'll
2303eb6f0de0SAdrian Chadd  * never be sent out. So these frames have to be directly
2304eb6f0de0SAdrian Chadd  * dispatched to the hardware, rather than queued in software.
2305eb6f0de0SAdrian Chadd  * So if this function returns true, the TXQ has to be
2306eb6f0de0SAdrian Chadd  * overridden and it has to be directly dispatched.
2307eb6f0de0SAdrian Chadd  *
2308eb6f0de0SAdrian Chadd  * It's a dirty hack, but someone's gotta do it.
2309eb6f0de0SAdrian Chadd  */
2310eb6f0de0SAdrian Chadd 
2311eb6f0de0SAdrian Chadd /*
2312eb6f0de0SAdrian Chadd  * XXX doesn't belong here!
2313eb6f0de0SAdrian Chadd  */
2314eb6f0de0SAdrian Chadd static int
2315eb6f0de0SAdrian Chadd ieee80211_is_action(struct ieee80211_frame *wh)
2316eb6f0de0SAdrian Chadd {
2317eb6f0de0SAdrian Chadd 	/* Type: Management frame? */
2318eb6f0de0SAdrian Chadd 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) !=
2319eb6f0de0SAdrian Chadd 	    IEEE80211_FC0_TYPE_MGT)
2320eb6f0de0SAdrian Chadd 		return 0;
2321eb6f0de0SAdrian Chadd 
2322eb6f0de0SAdrian Chadd 	/* Subtype: Action frame? */
2323eb6f0de0SAdrian Chadd 	if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) !=
2324eb6f0de0SAdrian Chadd 	    IEEE80211_FC0_SUBTYPE_ACTION)
2325eb6f0de0SAdrian Chadd 		return 0;
2326eb6f0de0SAdrian Chadd 
2327eb6f0de0SAdrian Chadd 	return 1;
2328eb6f0de0SAdrian Chadd }
2329eb6f0de0SAdrian Chadd 
2330eb6f0de0SAdrian Chadd #define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
2331eb6f0de0SAdrian Chadd /*
2332eb6f0de0SAdrian Chadd  * Return an alternate TID for ADDBA request frames.
2333eb6f0de0SAdrian Chadd  *
2334eb6f0de0SAdrian Chadd  * Yes, this likely should be done in the net80211 layer.
2335eb6f0de0SAdrian Chadd  */
2336eb6f0de0SAdrian Chadd static int
2337eb6f0de0SAdrian Chadd ath_tx_action_frame_override_queue(struct ath_softc *sc,
2338eb6f0de0SAdrian Chadd     struct ieee80211_node *ni,
2339eb6f0de0SAdrian Chadd     struct mbuf *m0, int *tid)
2340eb6f0de0SAdrian Chadd {
2341eb6f0de0SAdrian Chadd 	struct ieee80211_frame *wh = mtod(m0, struct ieee80211_frame *);
2342eb6f0de0SAdrian Chadd 	struct ieee80211_action_ba_addbarequest *ia;
2343eb6f0de0SAdrian Chadd 	uint8_t *frm;
2344eb6f0de0SAdrian Chadd 	uint16_t baparamset;
2345eb6f0de0SAdrian Chadd 
2346eb6f0de0SAdrian Chadd 	/* Not action frame? Bail */
2347eb6f0de0SAdrian Chadd 	if (! ieee80211_is_action(wh))
2348eb6f0de0SAdrian Chadd 		return 0;
2349eb6f0de0SAdrian Chadd 
2350eb6f0de0SAdrian Chadd 	/* XXX Not needed for frames we send? */
2351eb6f0de0SAdrian Chadd #if 0
2352eb6f0de0SAdrian Chadd 	/* Correct length? */
2353eb6f0de0SAdrian Chadd 	if (! ieee80211_parse_action(ni, m))
2354eb6f0de0SAdrian Chadd 		return 0;
2355eb6f0de0SAdrian Chadd #endif
2356eb6f0de0SAdrian Chadd 
2357eb6f0de0SAdrian Chadd 	/* Extract out action frame */
2358eb6f0de0SAdrian Chadd 	frm = (u_int8_t *)&wh[1];
2359eb6f0de0SAdrian Chadd 	ia = (struct ieee80211_action_ba_addbarequest *) frm;
2360eb6f0de0SAdrian Chadd 
2361eb6f0de0SAdrian Chadd 	/* Not ADDBA? Bail */
2362eb6f0de0SAdrian Chadd 	if (ia->rq_header.ia_category != IEEE80211_ACTION_CAT_BA)
2363eb6f0de0SAdrian Chadd 		return 0;
2364eb6f0de0SAdrian Chadd 	if (ia->rq_header.ia_action != IEEE80211_ACTION_BA_ADDBA_REQUEST)
2365eb6f0de0SAdrian Chadd 		return 0;
2366eb6f0de0SAdrian Chadd 
2367eb6f0de0SAdrian Chadd 	/* Extract TID, return it */
2368eb6f0de0SAdrian Chadd 	baparamset = le16toh(ia->rq_baparamset);
2369eb6f0de0SAdrian Chadd 	*tid = (int) MS(baparamset, IEEE80211_BAPS_TID);
2370eb6f0de0SAdrian Chadd 
2371eb6f0de0SAdrian Chadd 	return 1;
2372eb6f0de0SAdrian Chadd }
2373eb6f0de0SAdrian Chadd #undef	MS
2374eb6f0de0SAdrian Chadd 
2375eb6f0de0SAdrian Chadd /* Per-node software queue operations */
2376eb6f0de0SAdrian Chadd 
2377eb6f0de0SAdrian Chadd /*
2378eb6f0de0SAdrian Chadd  * Add the current packet to the given BAW.
2379eb6f0de0SAdrian Chadd  * It is assumed that the current packet
2380eb6f0de0SAdrian Chadd  *
2381eb6f0de0SAdrian Chadd  * + fits inside the BAW;
2382eb6f0de0SAdrian Chadd  * + already has had a sequence number allocated.
2383eb6f0de0SAdrian Chadd  *
2384eb6f0de0SAdrian Chadd  * Since the BAW status may be modified by both the ath task and
2385eb6f0de0SAdrian Chadd  * the net80211/ifnet contexts, the TID must be locked.
2386eb6f0de0SAdrian Chadd  */
2387eb6f0de0SAdrian Chadd void
2388eb6f0de0SAdrian Chadd ath_tx_addto_baw(struct ath_softc *sc, struct ath_node *an,
2389eb6f0de0SAdrian Chadd     struct ath_tid *tid, struct ath_buf *bf)
2390eb6f0de0SAdrian Chadd {
2391eb6f0de0SAdrian Chadd 	int index, cindex;
2392eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
2393eb6f0de0SAdrian Chadd 
2394eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
2395c2ac9655SAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
2396eb6f0de0SAdrian Chadd 
2397eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_isretried)
2398eb6f0de0SAdrian Chadd 		return;
2399eb6f0de0SAdrian Chadd 
2400c7c07341SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
2401c7c07341SAdrian Chadd 
24027561cb5cSAdrian Chadd 	if (! bf->bf_state.bfs_dobaw) {
24037561cb5cSAdrian Chadd 		device_printf(sc->sc_dev,
24047561cb5cSAdrian Chadd 		    "%s: dobaw=0, seqno=%d, window %d:%d\n",
24057561cb5cSAdrian Chadd 		    __func__,
24067561cb5cSAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno),
24077561cb5cSAdrian Chadd 		    tap->txa_start,
24087561cb5cSAdrian Chadd 		    tap->txa_wnd);
24097561cb5cSAdrian Chadd 	}
24107561cb5cSAdrian Chadd 
2411eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_addedbaw)
2412eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
2413a108d2d6SAdrian Chadd 		    "%s: re-added? tid=%d, seqno %d; window %d:%d; "
2414d4365d16SAdrian Chadd 		    "baw head=%d tail=%d\n",
2415a108d2d6SAdrian Chadd 		    __func__, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
2416d4365d16SAdrian Chadd 		    tap->txa_start, tap->txa_wnd, tid->baw_head,
2417d4365d16SAdrian Chadd 		    tid->baw_tail);
2418eb6f0de0SAdrian Chadd 
2419eb6f0de0SAdrian Chadd 	/*
24207561cb5cSAdrian Chadd 	 * Verify that the given sequence number is not outside of the
24217561cb5cSAdrian Chadd 	 * BAW.  Complain loudly if that's the case.
24227561cb5cSAdrian Chadd 	 */
24237561cb5cSAdrian Chadd 	if (! BAW_WITHIN(tap->txa_start, tap->txa_wnd,
24247561cb5cSAdrian Chadd 	    SEQNO(bf->bf_state.bfs_seqno))) {
24257561cb5cSAdrian Chadd 		device_printf(sc->sc_dev,
24267561cb5cSAdrian Chadd 		    "%s: bf=%p: outside of BAW?? tid=%d, seqno %d; window %d:%d; "
24277561cb5cSAdrian Chadd 		    "baw head=%d tail=%d\n",
24287561cb5cSAdrian Chadd 		    __func__, bf, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
24297561cb5cSAdrian Chadd 		    tap->txa_start, tap->txa_wnd, tid->baw_head,
24307561cb5cSAdrian Chadd 		    tid->baw_tail);
24317561cb5cSAdrian Chadd 	}
24327561cb5cSAdrian Chadd 
24337561cb5cSAdrian Chadd 	/*
2434eb6f0de0SAdrian Chadd 	 * ni->ni_txseqs[] is the currently allocated seqno.
2435eb6f0de0SAdrian Chadd 	 * the txa state contains the current baw start.
2436eb6f0de0SAdrian Chadd 	 */
2437eb6f0de0SAdrian Chadd 	index  = ATH_BA_INDEX(tap->txa_start, SEQNO(bf->bf_state.bfs_seqno));
2438eb6f0de0SAdrian Chadd 	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
2439eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2440a108d2d6SAdrian Chadd 	    "%s: tid=%d, seqno %d; window %d:%d; index=%d cindex=%d "
2441d4365d16SAdrian Chadd 	    "baw head=%d tail=%d\n",
2442a108d2d6SAdrian Chadd 	    __func__, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
2443d4365d16SAdrian Chadd 	    tap->txa_start, tap->txa_wnd, index, cindex, tid->baw_head,
2444d4365d16SAdrian Chadd 	    tid->baw_tail);
2445eb6f0de0SAdrian Chadd 
2446eb6f0de0SAdrian Chadd 
2447eb6f0de0SAdrian Chadd #if 0
2448eb6f0de0SAdrian Chadd 	assert(tid->tx_buf[cindex] == NULL);
2449eb6f0de0SAdrian Chadd #endif
2450eb6f0de0SAdrian Chadd 	if (tid->tx_buf[cindex] != NULL) {
2451eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
2452eb6f0de0SAdrian Chadd 		    "%s: ba packet dup (index=%d, cindex=%d, "
2453eb6f0de0SAdrian Chadd 		    "head=%d, tail=%d)\n",
2454eb6f0de0SAdrian Chadd 		    __func__, index, cindex, tid->baw_head, tid->baw_tail);
2455eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
2456eb6f0de0SAdrian Chadd 		    "%s: BA bf: %p; seqno=%d ; new bf: %p; seqno=%d\n",
2457eb6f0de0SAdrian Chadd 		    __func__,
2458eb6f0de0SAdrian Chadd 		    tid->tx_buf[cindex],
2459eb6f0de0SAdrian Chadd 		    SEQNO(tid->tx_buf[cindex]->bf_state.bfs_seqno),
2460eb6f0de0SAdrian Chadd 		    bf,
2461eb6f0de0SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno)
2462eb6f0de0SAdrian Chadd 		);
2463eb6f0de0SAdrian Chadd 	}
2464eb6f0de0SAdrian Chadd 	tid->tx_buf[cindex] = bf;
2465eb6f0de0SAdrian Chadd 
2466d4365d16SAdrian Chadd 	if (index >= ((tid->baw_tail - tid->baw_head) &
2467d4365d16SAdrian Chadd 	    (ATH_TID_MAX_BUFS - 1))) {
2468eb6f0de0SAdrian Chadd 		tid->baw_tail = cindex;
2469eb6f0de0SAdrian Chadd 		INCR(tid->baw_tail, ATH_TID_MAX_BUFS);
2470eb6f0de0SAdrian Chadd 	}
2471eb6f0de0SAdrian Chadd }
2472eb6f0de0SAdrian Chadd 
2473eb6f0de0SAdrian Chadd /*
247438962489SAdrian Chadd  * Flip the BAW buffer entry over from the existing one to the new one.
247538962489SAdrian Chadd  *
247638962489SAdrian Chadd  * When software retransmitting a (sub-)frame, it is entirely possible that
247738962489SAdrian Chadd  * the frame ath_buf is marked as BUSY and can't be immediately reused.
247838962489SAdrian Chadd  * In that instance the buffer is cloned and the new buffer is used for
247938962489SAdrian Chadd  * retransmit. We thus need to update the ath_buf slot in the BAW buf
248038962489SAdrian Chadd  * tracking array to maintain consistency.
248138962489SAdrian Chadd  */
248238962489SAdrian Chadd static void
248338962489SAdrian Chadd ath_tx_switch_baw_buf(struct ath_softc *sc, struct ath_node *an,
248438962489SAdrian Chadd     struct ath_tid *tid, struct ath_buf *old_bf, struct ath_buf *new_bf)
248538962489SAdrian Chadd {
248638962489SAdrian Chadd 	int index, cindex;
248738962489SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
248838962489SAdrian Chadd 	int seqno = SEQNO(old_bf->bf_state.bfs_seqno);
248938962489SAdrian Chadd 
249038962489SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
2491c2ac9655SAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
249238962489SAdrian Chadd 
249338962489SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
249438962489SAdrian Chadd 	index  = ATH_BA_INDEX(tap->txa_start, seqno);
249538962489SAdrian Chadd 	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
249638962489SAdrian Chadd 
249738962489SAdrian Chadd 	/*
249838962489SAdrian Chadd 	 * Just warn for now; if it happens then we should find out
249938962489SAdrian Chadd 	 * about it. It's highly likely the aggregation session will
250038962489SAdrian Chadd 	 * soon hang.
250138962489SAdrian Chadd 	 */
250238962489SAdrian Chadd 	if (old_bf->bf_state.bfs_seqno != new_bf->bf_state.bfs_seqno) {
250338962489SAdrian Chadd 		device_printf(sc->sc_dev, "%s: retransmitted buffer"
250438962489SAdrian Chadd 		    " has mismatching seqno's, BA session may hang.\n",
250538962489SAdrian Chadd 		    __func__);
250638962489SAdrian Chadd 		device_printf(sc->sc_dev, "%s: old seqno=%d, new_seqno=%d\n",
250738962489SAdrian Chadd 		    __func__,
250838962489SAdrian Chadd 		    old_bf->bf_state.bfs_seqno,
250938962489SAdrian Chadd 		    new_bf->bf_state.bfs_seqno);
251038962489SAdrian Chadd 	}
251138962489SAdrian Chadd 
251238962489SAdrian Chadd 	if (tid->tx_buf[cindex] != old_bf) {
251338962489SAdrian Chadd 		device_printf(sc->sc_dev, "%s: ath_buf pointer incorrect; "
251438962489SAdrian Chadd 		    " has m BA session may hang.\n",
251538962489SAdrian Chadd 		    __func__);
251638962489SAdrian Chadd 		device_printf(sc->sc_dev, "%s: old bf=%p, new bf=%p\n",
251738962489SAdrian Chadd 		    __func__,
251838962489SAdrian Chadd 		    old_bf, new_bf);
251938962489SAdrian Chadd 	}
252038962489SAdrian Chadd 
252138962489SAdrian Chadd 	tid->tx_buf[cindex] = new_bf;
252238962489SAdrian Chadd }
252338962489SAdrian Chadd 
252438962489SAdrian Chadd /*
2525eb6f0de0SAdrian Chadd  * seq_start - left edge of BAW
2526eb6f0de0SAdrian Chadd  * seq_next - current/next sequence number to allocate
2527eb6f0de0SAdrian Chadd  *
2528eb6f0de0SAdrian Chadd  * Since the BAW status may be modified by both the ath task and
2529eb6f0de0SAdrian Chadd  * the net80211/ifnet contexts, the TID must be locked.
2530eb6f0de0SAdrian Chadd  */
2531eb6f0de0SAdrian Chadd static void
2532eb6f0de0SAdrian Chadd ath_tx_update_baw(struct ath_softc *sc, struct ath_node *an,
2533eb6f0de0SAdrian Chadd     struct ath_tid *tid, const struct ath_buf *bf)
2534eb6f0de0SAdrian Chadd {
2535eb6f0de0SAdrian Chadd 	int index, cindex;
2536eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
2537eb6f0de0SAdrian Chadd 	int seqno = SEQNO(bf->bf_state.bfs_seqno);
2538eb6f0de0SAdrian Chadd 
2539eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
25404b6db404SAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
2541eb6f0de0SAdrian Chadd 
2542eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
2543eb6f0de0SAdrian Chadd 	index  = ATH_BA_INDEX(tap->txa_start, seqno);
2544eb6f0de0SAdrian Chadd 	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
2545eb6f0de0SAdrian Chadd 
2546eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2547a108d2d6SAdrian Chadd 	    "%s: tid=%d, baw=%d:%d, seqno=%d, index=%d, cindex=%d, "
2548d4365d16SAdrian Chadd 	    "baw head=%d, tail=%d\n",
2549a108d2d6SAdrian Chadd 	    __func__, tid->tid, tap->txa_start, tap->txa_wnd, seqno, index,
2550eb6f0de0SAdrian Chadd 	    cindex, tid->baw_head, tid->baw_tail);
2551eb6f0de0SAdrian Chadd 
2552eb6f0de0SAdrian Chadd 	/*
2553eb6f0de0SAdrian Chadd 	 * If this occurs then we have a big problem - something else
2554eb6f0de0SAdrian Chadd 	 * has slid tap->txa_start along without updating the BAW
2555eb6f0de0SAdrian Chadd 	 * tracking start/end pointers. Thus the TX BAW state is now
2556eb6f0de0SAdrian Chadd 	 * completely busted.
2557eb6f0de0SAdrian Chadd 	 *
2558eb6f0de0SAdrian Chadd 	 * But for now, since I haven't yet fixed TDMA and buffer cloning,
2559eb6f0de0SAdrian Chadd 	 * it's quite possible that a cloned buffer is making its way
2560eb6f0de0SAdrian Chadd 	 * here and causing it to fire off. Disable TDMA for now.
2561eb6f0de0SAdrian Chadd 	 */
2562eb6f0de0SAdrian Chadd 	if (tid->tx_buf[cindex] != bf) {
2563eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
2564eb6f0de0SAdrian Chadd 		    "%s: comp bf=%p, seq=%d; slot bf=%p, seqno=%d\n",
2565eb6f0de0SAdrian Chadd 		    __func__,
2566eb6f0de0SAdrian Chadd 		    bf, SEQNO(bf->bf_state.bfs_seqno),
2567eb6f0de0SAdrian Chadd 		    tid->tx_buf[cindex],
2568eb6f0de0SAdrian Chadd 		    SEQNO(tid->tx_buf[cindex]->bf_state.bfs_seqno));
2569eb6f0de0SAdrian Chadd 	}
2570eb6f0de0SAdrian Chadd 
2571eb6f0de0SAdrian Chadd 	tid->tx_buf[cindex] = NULL;
2572eb6f0de0SAdrian Chadd 
2573d4365d16SAdrian Chadd 	while (tid->baw_head != tid->baw_tail &&
2574d4365d16SAdrian Chadd 	    !tid->tx_buf[tid->baw_head]) {
2575eb6f0de0SAdrian Chadd 		INCR(tap->txa_start, IEEE80211_SEQ_RANGE);
2576eb6f0de0SAdrian Chadd 		INCR(tid->baw_head, ATH_TID_MAX_BUFS);
2577eb6f0de0SAdrian Chadd 	}
2578d4365d16SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2579d4365d16SAdrian Chadd 	    "%s: baw is now %d:%d, baw head=%d\n",
2580eb6f0de0SAdrian Chadd 	    __func__, tap->txa_start, tap->txa_wnd, tid->baw_head);
2581eb6f0de0SAdrian Chadd }
2582eb6f0de0SAdrian Chadd 
2583eb6f0de0SAdrian Chadd /*
2584eb6f0de0SAdrian Chadd  * Mark the current node/TID as ready to TX.
2585eb6f0de0SAdrian Chadd  *
2586eb6f0de0SAdrian Chadd  * This is done to make it easy for the software scheduler to
2587eb6f0de0SAdrian Chadd  * find which nodes have data to send.
2588eb6f0de0SAdrian Chadd  *
2589eb6f0de0SAdrian Chadd  * The TXQ lock must be held.
2590eb6f0de0SAdrian Chadd  */
2591eb6f0de0SAdrian Chadd static void
2592eb6f0de0SAdrian Chadd ath_tx_tid_sched(struct ath_softc *sc, struct ath_tid *tid)
2593eb6f0de0SAdrian Chadd {
2594eb6f0de0SAdrian Chadd 	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
2595eb6f0de0SAdrian Chadd 
2596eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
2597eb6f0de0SAdrian Chadd 
2598eb6f0de0SAdrian Chadd 	if (tid->paused)
2599eb6f0de0SAdrian Chadd 		return;		/* paused, can't schedule yet */
2600eb6f0de0SAdrian Chadd 
2601eb6f0de0SAdrian Chadd 	if (tid->sched)
2602eb6f0de0SAdrian Chadd 		return;		/* already scheduled */
2603eb6f0de0SAdrian Chadd 
2604eb6f0de0SAdrian Chadd 	tid->sched = 1;
2605eb6f0de0SAdrian Chadd 
2606eb6f0de0SAdrian Chadd 	TAILQ_INSERT_TAIL(&txq->axq_tidq, tid, axq_qelem);
2607eb6f0de0SAdrian Chadd }
2608eb6f0de0SAdrian Chadd 
2609eb6f0de0SAdrian Chadd /*
2610eb6f0de0SAdrian Chadd  * Mark the current node as no longer needing to be polled for
2611eb6f0de0SAdrian Chadd  * TX packets.
2612eb6f0de0SAdrian Chadd  *
2613eb6f0de0SAdrian Chadd  * The TXQ lock must be held.
2614eb6f0de0SAdrian Chadd  */
2615eb6f0de0SAdrian Chadd static void
2616eb6f0de0SAdrian Chadd ath_tx_tid_unsched(struct ath_softc *sc, struct ath_tid *tid)
2617eb6f0de0SAdrian Chadd {
2618eb6f0de0SAdrian Chadd 	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
2619eb6f0de0SAdrian Chadd 
2620eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
2621eb6f0de0SAdrian Chadd 
2622eb6f0de0SAdrian Chadd 	if (tid->sched == 0)
2623eb6f0de0SAdrian Chadd 		return;
2624eb6f0de0SAdrian Chadd 
2625eb6f0de0SAdrian Chadd 	tid->sched = 0;
2626eb6f0de0SAdrian Chadd 	TAILQ_REMOVE(&txq->axq_tidq, tid, axq_qelem);
2627eb6f0de0SAdrian Chadd }
2628eb6f0de0SAdrian Chadd 
2629eb6f0de0SAdrian Chadd /*
2630eb6f0de0SAdrian Chadd  * Assign a sequence number manually to the given frame.
2631eb6f0de0SAdrian Chadd  *
2632eb6f0de0SAdrian Chadd  * This should only be called for A-MPDU TX frames.
2633eb6f0de0SAdrian Chadd  */
2634a108d2d6SAdrian Chadd static ieee80211_seq
2635eb6f0de0SAdrian Chadd ath_tx_tid_seqno_assign(struct ath_softc *sc, struct ieee80211_node *ni,
2636eb6f0de0SAdrian Chadd     struct ath_buf *bf, struct mbuf *m0)
2637eb6f0de0SAdrian Chadd {
2638eb6f0de0SAdrian Chadd 	struct ieee80211_frame *wh;
2639eb6f0de0SAdrian Chadd 	int tid, pri;
2640eb6f0de0SAdrian Chadd 	ieee80211_seq seqno;
2641eb6f0de0SAdrian Chadd 	uint8_t subtype;
2642eb6f0de0SAdrian Chadd 
2643eb6f0de0SAdrian Chadd 	/* TID lookup */
2644eb6f0de0SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
2645eb6f0de0SAdrian Chadd 	pri = M_WME_GETAC(m0);			/* honor classification */
2646eb6f0de0SAdrian Chadd 	tid = WME_AC_TO_TID(pri);
2647a108d2d6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: pri=%d, tid=%d, qos has seq=%d\n",
2648a108d2d6SAdrian Chadd 	    __func__, pri, tid, IEEE80211_QOS_HAS_SEQ(wh));
2649eb6f0de0SAdrian Chadd 
2650eb6f0de0SAdrian Chadd 	/* XXX Is it a control frame? Ignore */
2651eb6f0de0SAdrian Chadd 
2652eb6f0de0SAdrian Chadd 	/* Does the packet require a sequence number? */
2653eb6f0de0SAdrian Chadd 	if (! IEEE80211_QOS_HAS_SEQ(wh))
2654eb6f0de0SAdrian Chadd 		return -1;
2655eb6f0de0SAdrian Chadd 
26567561cb5cSAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, &(ATH_NODE(ni)->an_tid[tid]));
26577561cb5cSAdrian Chadd 
2658eb6f0de0SAdrian Chadd 	/*
2659eb6f0de0SAdrian Chadd 	 * Is it a QOS NULL Data frame? Give it a sequence number from
2660eb6f0de0SAdrian Chadd 	 * the default TID (IEEE80211_NONQOS_TID.)
2661eb6f0de0SAdrian Chadd 	 *
2662eb6f0de0SAdrian Chadd 	 * The RX path of everything I've looked at doesn't include the NULL
2663eb6f0de0SAdrian Chadd 	 * data frame sequence number in the aggregation state updates, so
2664eb6f0de0SAdrian Chadd 	 * assigning it a sequence number there will cause a BAW hole on the
2665eb6f0de0SAdrian Chadd 	 * RX side.
2666eb6f0de0SAdrian Chadd 	 */
2667eb6f0de0SAdrian Chadd 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
2668eb6f0de0SAdrian Chadd 	if (subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL) {
26697561cb5cSAdrian Chadd 		/* XXX no locking for this TID? This is a bit of a problem. */
2670eb6f0de0SAdrian Chadd 		seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID];
2671eb6f0de0SAdrian Chadd 		INCR(ni->ni_txseqs[IEEE80211_NONQOS_TID], IEEE80211_SEQ_RANGE);
2672eb6f0de0SAdrian Chadd 	} else {
2673eb6f0de0SAdrian Chadd 		/* Manually assign sequence number */
2674eb6f0de0SAdrian Chadd 		seqno = ni->ni_txseqs[tid];
2675eb6f0de0SAdrian Chadd 		INCR(ni->ni_txseqs[tid], IEEE80211_SEQ_RANGE);
2676eb6f0de0SAdrian Chadd 	}
2677eb6f0de0SAdrian Chadd 	*(uint16_t *)&wh->i_seq[0] = htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
2678eb6f0de0SAdrian Chadd 	M_SEQNO_SET(m0, seqno);
2679eb6f0de0SAdrian Chadd 
2680eb6f0de0SAdrian Chadd 	/* Return so caller can do something with it if needed */
2681a108d2d6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s:  -> seqno=%d\n", __func__, seqno);
2682eb6f0de0SAdrian Chadd 	return seqno;
2683eb6f0de0SAdrian Chadd }
2684eb6f0de0SAdrian Chadd 
2685eb6f0de0SAdrian Chadd /*
2686eb6f0de0SAdrian Chadd  * Attempt to direct dispatch an aggregate frame to hardware.
2687eb6f0de0SAdrian Chadd  * If the frame is out of BAW, queue.
2688eb6f0de0SAdrian Chadd  * Otherwise, schedule it as a single frame.
2689eb6f0de0SAdrian Chadd  */
2690eb6f0de0SAdrian Chadd static void
269146634305SAdrian Chadd ath_tx_xmit_aggr(struct ath_softc *sc, struct ath_node *an,
269246634305SAdrian Chadd     struct ath_txq *txq, struct ath_buf *bf)
2693eb6f0de0SAdrian Chadd {
2694eb6f0de0SAdrian Chadd 	struct ath_tid *tid = &an->an_tid[bf->bf_state.bfs_tid];
269546634305SAdrian Chadd //	struct ath_txq *txq = bf->bf_state.bfs_txq;
2696eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
2697eb6f0de0SAdrian Chadd 
269846634305SAdrian Chadd 	if (txq != bf->bf_state.bfs_txq) {
269946634305SAdrian Chadd 		device_printf(sc->sc_dev, "%s: txq %d != bfs_txq %d!\n",
270046634305SAdrian Chadd 		    __func__,
270146634305SAdrian Chadd 		    txq->axq_qnum,
270246634305SAdrian Chadd 		    bf->bf_state.bfs_txq->axq_qnum);
270346634305SAdrian Chadd 	}
270446634305SAdrian Chadd 
2705eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
2706c2ac9655SAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
2707eb6f0de0SAdrian Chadd 
2708eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
2709eb6f0de0SAdrian Chadd 
2710eb6f0de0SAdrian Chadd 	/* paused? queue */
2711eb6f0de0SAdrian Chadd 	if (tid->paused) {
27123e6cc97fSAdrian Chadd 		ATH_TID_INSERT_HEAD(tid, bf, bf_list);
27130f04c5a2SAdrian Chadd 		/* XXX don't sched - we're paused! */
2714eb6f0de0SAdrian Chadd 		return;
2715eb6f0de0SAdrian Chadd 	}
2716eb6f0de0SAdrian Chadd 
2717eb6f0de0SAdrian Chadd 	/* outside baw? queue */
2718eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_dobaw &&
2719eb6f0de0SAdrian Chadd 	    (! BAW_WITHIN(tap->txa_start, tap->txa_wnd,
2720eb6f0de0SAdrian Chadd 	    SEQNO(bf->bf_state.bfs_seqno)))) {
27213e6cc97fSAdrian Chadd 		ATH_TID_INSERT_HEAD(tid, bf, bf_list);
2722eb6f0de0SAdrian Chadd 		ath_tx_tid_sched(sc, tid);
2723eb6f0de0SAdrian Chadd 		return;
2724eb6f0de0SAdrian Chadd 	}
2725eb6f0de0SAdrian Chadd 
27262a9f83afSAdrian Chadd 	/*
27272a9f83afSAdrian Chadd 	 * This is a temporary check and should be removed once
27282a9f83afSAdrian Chadd 	 * all the relevant code paths have been fixed.
27292a9f83afSAdrian Chadd 	 *
27302a9f83afSAdrian Chadd 	 * During aggregate retries, it's possible that the head
27312a9f83afSAdrian Chadd 	 * frame will fail (which has the bfs_aggr and bfs_nframes
27322a9f83afSAdrian Chadd 	 * fields set for said aggregate) and will be retried as
27332a9f83afSAdrian Chadd 	 * a single frame.  In this instance, the values should
27342a9f83afSAdrian Chadd 	 * be reset or the completion code will get upset with you.
27352a9f83afSAdrian Chadd 	 */
27362a9f83afSAdrian Chadd 	if (bf->bf_state.bfs_aggr != 0 || bf->bf_state.bfs_nframes > 1) {
27372a9f83afSAdrian Chadd 		device_printf(sc->sc_dev, "%s: bfs_aggr=%d, bfs_nframes=%d\n",
27382a9f83afSAdrian Chadd 		    __func__,
27392a9f83afSAdrian Chadd 		    bf->bf_state.bfs_aggr,
27402a9f83afSAdrian Chadd 		    bf->bf_state.bfs_nframes);
27412a9f83afSAdrian Chadd 		bf->bf_state.bfs_aggr = 0;
27422a9f83afSAdrian Chadd 		bf->bf_state.bfs_nframes = 1;
27432a9f83afSAdrian Chadd 	}
27442a9f83afSAdrian Chadd 
27454e81f27cSAdrian Chadd 	/* Update CLRDMASK just before this frame is queued */
27464e81f27cSAdrian Chadd 	ath_tx_update_clrdmask(sc, tid, bf);
27474e81f27cSAdrian Chadd 
2748eb6f0de0SAdrian Chadd 	/* Direct dispatch to hardware */
2749eb6f0de0SAdrian Chadd 	ath_tx_do_ratelookup(sc, bf);
2750e2e4a2c2SAdrian Chadd 	ath_tx_calc_duration(sc, bf);
2751e2e4a2c2SAdrian Chadd 	ath_tx_calc_protection(sc, bf);
2752eb6f0de0SAdrian Chadd 	ath_tx_set_rtscts(sc, bf);
2753e2e4a2c2SAdrian Chadd 	ath_tx_rate_fill_rcflags(sc, bf);
2754eb6f0de0SAdrian Chadd 	ath_tx_setds(sc, bf);
2755eb6f0de0SAdrian Chadd 
2756eb6f0de0SAdrian Chadd 	/* Statistics */
2757eb6f0de0SAdrian Chadd 	sc->sc_aggr_stats.aggr_low_hwq_single_pkt++;
2758eb6f0de0SAdrian Chadd 
2759eb6f0de0SAdrian Chadd 	/* Track per-TID hardware queue depth correctly */
2760eb6f0de0SAdrian Chadd 	tid->hwq_depth++;
2761eb6f0de0SAdrian Chadd 
2762eb6f0de0SAdrian Chadd 	/* Add to BAW */
2763eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_dobaw) {
2764eb6f0de0SAdrian Chadd 		ath_tx_addto_baw(sc, an, tid, bf);
2765eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_addedbaw = 1;
2766eb6f0de0SAdrian Chadd 	}
2767eb6f0de0SAdrian Chadd 
2768eb6f0de0SAdrian Chadd 	/* Set completion handler, multi-frame aggregate or not */
2769eb6f0de0SAdrian Chadd 	bf->bf_comp = ath_tx_aggr_comp;
2770eb6f0de0SAdrian Chadd 
2771eb6f0de0SAdrian Chadd 	/* Hand off to hardware */
2772eb6f0de0SAdrian Chadd 	ath_tx_handoff(sc, txq, bf);
2773eb6f0de0SAdrian Chadd }
2774eb6f0de0SAdrian Chadd 
2775eb6f0de0SAdrian Chadd /*
2776eb6f0de0SAdrian Chadd  * Attempt to send the packet.
2777eb6f0de0SAdrian Chadd  * If the queue isn't busy, direct-dispatch.
2778eb6f0de0SAdrian Chadd  * If the queue is busy enough, queue the given packet on the
2779eb6f0de0SAdrian Chadd  *  relevant software queue.
2780eb6f0de0SAdrian Chadd  */
2781eb6f0de0SAdrian Chadd void
2782eb6f0de0SAdrian Chadd ath_tx_swq(struct ath_softc *sc, struct ieee80211_node *ni, struct ath_txq *txq,
2783eb6f0de0SAdrian Chadd     struct ath_buf *bf)
2784eb6f0de0SAdrian Chadd {
2785eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
2786eb6f0de0SAdrian Chadd 	struct ieee80211_frame *wh;
2787eb6f0de0SAdrian Chadd 	struct ath_tid *atid;
2788eb6f0de0SAdrian Chadd 	int pri, tid;
2789eb6f0de0SAdrian Chadd 	struct mbuf *m0 = bf->bf_m;
2790eb6f0de0SAdrian Chadd 
27917561cb5cSAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
27927561cb5cSAdrian Chadd 
2793eb6f0de0SAdrian Chadd 	/* Fetch the TID - non-QoS frames get assigned to TID 16 */
2794eb6f0de0SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
2795eb6f0de0SAdrian Chadd 	pri = ath_tx_getac(sc, m0);
2796eb6f0de0SAdrian Chadd 	tid = ath_tx_gettid(sc, m0);
2797eb6f0de0SAdrian Chadd 	atid = &an->an_tid[tid];
2798eb6f0de0SAdrian Chadd 
2799c2ac9655SAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, atid);
2800c2ac9655SAdrian Chadd 
2801a108d2d6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bf=%p, pri=%d, tid=%d, qos=%d\n",
2802a108d2d6SAdrian Chadd 	    __func__, bf, pri, tid, IEEE80211_QOS_HAS_SEQ(wh));
2803eb6f0de0SAdrian Chadd 
2804eb6f0de0SAdrian Chadd 	/* Set local packet state, used to queue packets to hardware */
280546634305SAdrian Chadd 	/* XXX potentially duplicate info, re-check */
280646634305SAdrian Chadd 	/* XXX remember, txq must be the hardware queue, not the av_mcastq */
2807eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_tid = tid;
2808eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txq = txq;
2809eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_pri = pri;
2810eb6f0de0SAdrian Chadd 
2811eb6f0de0SAdrian Chadd 	/*
2812eb6f0de0SAdrian Chadd 	 * If the hardware queue isn't busy, queue it directly.
2813eb6f0de0SAdrian Chadd 	 * If the hardware queue is busy, queue it.
2814eb6f0de0SAdrian Chadd 	 * If the TID is paused or the traffic it outside BAW, software
2815eb6f0de0SAdrian Chadd 	 * queue it.
2816eb6f0de0SAdrian Chadd 	 */
2817eb6f0de0SAdrian Chadd 	if (atid->paused) {
2818eb6f0de0SAdrian Chadd 		/* TID is paused, queue */
2819a108d2d6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: paused\n", __func__);
28203e6cc97fSAdrian Chadd 		ATH_TID_INSERT_TAIL(atid, bf, bf_list);
2821eb6f0de0SAdrian Chadd 	} else if (ath_tx_ampdu_pending(sc, an, tid)) {
2822eb6f0de0SAdrian Chadd 		/* AMPDU pending; queue */
2823a108d2d6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: pending\n", __func__);
28243e6cc97fSAdrian Chadd 		ATH_TID_INSERT_TAIL(atid, bf, bf_list);
2825eb6f0de0SAdrian Chadd 		/* XXX sched? */
2826eb6f0de0SAdrian Chadd 	} else if (ath_tx_ampdu_running(sc, an, tid)) {
2827eb6f0de0SAdrian Chadd 		/* AMPDU running, attempt direct dispatch if possible */
282839f24578SAdrian Chadd 
282939f24578SAdrian Chadd 		/*
283039f24578SAdrian Chadd 		 * Always queue the frame to the tail of the list.
283139f24578SAdrian Chadd 		 */
28323e6cc97fSAdrian Chadd 		ATH_TID_INSERT_TAIL(atid, bf, bf_list);
283339f24578SAdrian Chadd 
283439f24578SAdrian Chadd 		/*
283539f24578SAdrian Chadd 		 * If the hardware queue isn't busy, direct dispatch
283639f24578SAdrian Chadd 		 * the head frame in the list.  Don't schedule the
283739f24578SAdrian Chadd 		 * TID - let it build some more frames first?
283839f24578SAdrian Chadd 		 *
283939f24578SAdrian Chadd 		 * Otherwise, schedule the TID.
284039f24578SAdrian Chadd 		 */
2841d4365d16SAdrian Chadd 		if (txq->axq_depth < sc->sc_hwq_limit) {
28423e6cc97fSAdrian Chadd 			bf = ATH_TID_FIRST(atid);
28433e6cc97fSAdrian Chadd 			ATH_TID_REMOVE(atid, bf, bf_list);
28442a9f83afSAdrian Chadd 
28452a9f83afSAdrian Chadd 			/*
28462a9f83afSAdrian Chadd 			 * Ensure it's definitely treated as a non-AMPDU
28472a9f83afSAdrian Chadd 			 * frame - this information may have been left
28482a9f83afSAdrian Chadd 			 * over from a previous attempt.
28492a9f83afSAdrian Chadd 			 */
28502a9f83afSAdrian Chadd 			bf->bf_state.bfs_aggr = 0;
28512a9f83afSAdrian Chadd 			bf->bf_state.bfs_nframes = 1;
28522a9f83afSAdrian Chadd 
28532a9f83afSAdrian Chadd 			/* Queue to the hardware */
285446634305SAdrian Chadd 			ath_tx_xmit_aggr(sc, an, txq, bf);
2855a108d2d6SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX,
2856a108d2d6SAdrian Chadd 			    "%s: xmit_aggr\n",
2857a108d2d6SAdrian Chadd 			    __func__);
2858d4365d16SAdrian Chadd 		} else {
2859d4365d16SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX,
2860a108d2d6SAdrian Chadd 			    "%s: ampdu; swq'ing\n",
2861a108d2d6SAdrian Chadd 			    __func__);
286203682514SAdrian Chadd 
2863eb6f0de0SAdrian Chadd 			ath_tx_tid_sched(sc, atid);
2864eb6f0de0SAdrian Chadd 		}
2865eb6f0de0SAdrian Chadd 	} else if (txq->axq_depth < sc->sc_hwq_limit) {
2866eb6f0de0SAdrian Chadd 		/* AMPDU not running, attempt direct dispatch */
2867a108d2d6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: xmit_normal\n", __func__);
28680a544719SAdrian Chadd 		/* See if clrdmask needs to be set */
28690a544719SAdrian Chadd 		ath_tx_update_clrdmask(sc, atid, bf);
2870eb6f0de0SAdrian Chadd 		ath_tx_xmit_normal(sc, txq, bf);
2871eb6f0de0SAdrian Chadd 	} else {
2872eb6f0de0SAdrian Chadd 		/* Busy; queue */
2873a108d2d6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: swq'ing\n", __func__);
28743e6cc97fSAdrian Chadd 		ATH_TID_INSERT_TAIL(atid, bf, bf_list);
2875eb6f0de0SAdrian Chadd 		ath_tx_tid_sched(sc, atid);
2876eb6f0de0SAdrian Chadd 	}
2877eb6f0de0SAdrian Chadd }
2878eb6f0de0SAdrian Chadd 
2879eb6f0de0SAdrian Chadd /*
2880eb6f0de0SAdrian Chadd  * Configure the per-TID node state.
2881eb6f0de0SAdrian Chadd  *
2882eb6f0de0SAdrian Chadd  * This likely belongs in if_ath_node.c but I can't think of anywhere
2883eb6f0de0SAdrian Chadd  * else to put it just yet.
2884eb6f0de0SAdrian Chadd  *
2885eb6f0de0SAdrian Chadd  * This sets up the SLISTs and the mutex as appropriate.
2886eb6f0de0SAdrian Chadd  */
2887eb6f0de0SAdrian Chadd void
2888eb6f0de0SAdrian Chadd ath_tx_tid_init(struct ath_softc *sc, struct ath_node *an)
2889eb6f0de0SAdrian Chadd {
2890eb6f0de0SAdrian Chadd 	int i, j;
2891eb6f0de0SAdrian Chadd 	struct ath_tid *atid;
2892eb6f0de0SAdrian Chadd 
2893eb6f0de0SAdrian Chadd 	for (i = 0; i < IEEE80211_TID_SIZE; i++) {
2894eb6f0de0SAdrian Chadd 		atid = &an->an_tid[i];
2895f1bc738eSAdrian Chadd 
2896f1bc738eSAdrian Chadd 		/* XXX now with this bzer(), is the field 0'ing needed? */
2897f1bc738eSAdrian Chadd 		bzero(atid, sizeof(*atid));
2898f1bc738eSAdrian Chadd 
28993e6cc97fSAdrian Chadd 		TAILQ_INIT(&atid->tid_q);
29003e6cc97fSAdrian Chadd 		TAILQ_INIT(&atid->filtq.tid_q);
2901eb6f0de0SAdrian Chadd 		atid->tid = i;
2902eb6f0de0SAdrian Chadd 		atid->an = an;
2903eb6f0de0SAdrian Chadd 		for (j = 0; j < ATH_TID_MAX_BUFS; j++)
2904eb6f0de0SAdrian Chadd 			atid->tx_buf[j] = NULL;
2905eb6f0de0SAdrian Chadd 		atid->baw_head = atid->baw_tail = 0;
2906eb6f0de0SAdrian Chadd 		atid->paused = 0;
2907eb6f0de0SAdrian Chadd 		atid->sched = 0;
2908eb6f0de0SAdrian Chadd 		atid->hwq_depth = 0;
2909eb6f0de0SAdrian Chadd 		atid->cleanup_inprogress = 0;
2910f1bc738eSAdrian Chadd 		atid->clrdmask = 1;	/* Always start by setting this bit */
2911eb6f0de0SAdrian Chadd 		if (i == IEEE80211_NONQOS_TID)
29127403d1b9SAdrian Chadd 			atid->ac = ATH_NONQOS_TID_AC;
2913eb6f0de0SAdrian Chadd 		else
2914eb6f0de0SAdrian Chadd 			atid->ac = TID_TO_WME_AC(i);
2915eb6f0de0SAdrian Chadd 	}
2916eb6f0de0SAdrian Chadd }
2917eb6f0de0SAdrian Chadd 
2918eb6f0de0SAdrian Chadd /*
2919eb6f0de0SAdrian Chadd  * Pause the current TID. This stops packets from being transmitted
2920eb6f0de0SAdrian Chadd  * on it.
2921eb6f0de0SAdrian Chadd  *
2922eb6f0de0SAdrian Chadd  * Since this is also called from upper layers as well as the driver,
2923eb6f0de0SAdrian Chadd  * it will get the TID lock.
2924eb6f0de0SAdrian Chadd  */
2925eb6f0de0SAdrian Chadd static void
2926eb6f0de0SAdrian Chadd ath_tx_tid_pause(struct ath_softc *sc, struct ath_tid *tid)
2927eb6f0de0SAdrian Chadd {
292888b3d483SAdrian Chadd 
292988b3d483SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
2930eb6f0de0SAdrian Chadd 	tid->paused++;
2931eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: paused = %d\n",
2932eb6f0de0SAdrian Chadd 	    __func__, tid->paused);
2933eb6f0de0SAdrian Chadd }
2934eb6f0de0SAdrian Chadd 
2935eb6f0de0SAdrian Chadd /*
2936eb6f0de0SAdrian Chadd  * Unpause the current TID, and schedule it if needed.
2937eb6f0de0SAdrian Chadd  */
2938eb6f0de0SAdrian Chadd static void
2939eb6f0de0SAdrian Chadd ath_tx_tid_resume(struct ath_softc *sc, struct ath_tid *tid)
2940eb6f0de0SAdrian Chadd {
2941eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
2942eb6f0de0SAdrian Chadd 
2943eb6f0de0SAdrian Chadd 	tid->paused--;
2944eb6f0de0SAdrian Chadd 
2945eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: unpaused = %d\n",
2946eb6f0de0SAdrian Chadd 	    __func__, tid->paused);
2947eb6f0de0SAdrian Chadd 
29480eb81626SAdrian Chadd 	if (tid->paused)
2949eb6f0de0SAdrian Chadd 		return;
29500eb81626SAdrian Chadd 
29510eb81626SAdrian Chadd 	/*
29520eb81626SAdrian Chadd 	 * Override the clrdmask configuration for the next frame
29530eb81626SAdrian Chadd 	 * from this TID, just to get the ball rolling.
29540eb81626SAdrian Chadd 	 */
29550eb81626SAdrian Chadd 	tid->clrdmask = 1;
29560eb81626SAdrian Chadd 
29570eb81626SAdrian Chadd 	if (tid->axq_depth == 0)
29580eb81626SAdrian Chadd 		return;
2959eb6f0de0SAdrian Chadd 
2960f1bc738eSAdrian Chadd 	/* XXX isfiltered shouldn't ever be 0 at this point */
2961f1bc738eSAdrian Chadd 	if (tid->isfiltered == 1) {
2962f1bc738eSAdrian Chadd 		device_printf(sc->sc_dev, "%s: filtered?!\n", __func__);
2963f1bc738eSAdrian Chadd 		return;
2964f1bc738eSAdrian Chadd 	}
2965f1bc738eSAdrian Chadd 
2966eb6f0de0SAdrian Chadd 	ath_tx_tid_sched(sc, tid);
2967eb6f0de0SAdrian Chadd 	/* Punt some frames to the hardware if needed */
296803e9308fSAdrian Chadd 	//ath_txq_sched(sc, sc->sc_ac2q[tid->ac]);
296903e9308fSAdrian Chadd 	taskqueue_enqueue(sc->sc_tq, &sc->sc_txqtask);
2970eb6f0de0SAdrian Chadd }
2971eb6f0de0SAdrian Chadd 
2972eb6f0de0SAdrian Chadd /*
2973f1bc738eSAdrian Chadd  * Add the given ath_buf to the TID filtered frame list.
2974f1bc738eSAdrian Chadd  * This requires the TID be filtered.
2975f1bc738eSAdrian Chadd  */
2976f1bc738eSAdrian Chadd static void
2977f1bc738eSAdrian Chadd ath_tx_tid_filt_addbuf(struct ath_softc *sc, struct ath_tid *tid,
2978f1bc738eSAdrian Chadd     struct ath_buf *bf)
2979f1bc738eSAdrian Chadd {
2980f1bc738eSAdrian Chadd 
2981f1bc738eSAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
2982f1bc738eSAdrian Chadd 	if (! tid->isfiltered)
2983f1bc738eSAdrian Chadd 		device_printf(sc->sc_dev, "%s: not filtered?!\n", __func__);
2984f1bc738eSAdrian Chadd 
2985f1bc738eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_FILT, "%s: bf=%p\n", __func__, bf);
2986f1bc738eSAdrian Chadd 
2987f1bc738eSAdrian Chadd 	/* Set the retry bit and bump the retry counter */
2988f1bc738eSAdrian Chadd 	ath_tx_set_retry(sc, bf);
2989f1bc738eSAdrian Chadd 	sc->sc_stats.ast_tx_swfiltered++;
2990f1bc738eSAdrian Chadd 
299113aa9ee5SAdrian Chadd 	ATH_TID_FILT_INSERT_TAIL(tid, bf, bf_list);
2992f1bc738eSAdrian Chadd }
2993f1bc738eSAdrian Chadd 
2994f1bc738eSAdrian Chadd /*
2995f1bc738eSAdrian Chadd  * Handle a completed filtered frame from the given TID.
2996f1bc738eSAdrian Chadd  * This just enables/pauses the filtered frame state if required
2997f1bc738eSAdrian Chadd  * and appends the filtered frame to the filtered queue.
2998f1bc738eSAdrian Chadd  */
2999f1bc738eSAdrian Chadd static void
3000f1bc738eSAdrian Chadd ath_tx_tid_filt_comp_buf(struct ath_softc *sc, struct ath_tid *tid,
3001f1bc738eSAdrian Chadd     struct ath_buf *bf)
3002f1bc738eSAdrian Chadd {
3003f1bc738eSAdrian Chadd 
3004f1bc738eSAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
3005f1bc738eSAdrian Chadd 
3006f1bc738eSAdrian Chadd 	if (! tid->isfiltered) {
3007f1bc738eSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT, "%s: filter transition\n",
3008f1bc738eSAdrian Chadd 		    __func__);
3009f1bc738eSAdrian Chadd 		tid->isfiltered = 1;
3010f1bc738eSAdrian Chadd 		ath_tx_tid_pause(sc, tid);
3011f1bc738eSAdrian Chadd 	}
3012f1bc738eSAdrian Chadd 
3013f1bc738eSAdrian Chadd 	/* Add the frame to the filter queue */
3014f1bc738eSAdrian Chadd 	ath_tx_tid_filt_addbuf(sc, tid, bf);
3015f1bc738eSAdrian Chadd }
3016f1bc738eSAdrian Chadd 
3017f1bc738eSAdrian Chadd /*
3018f1bc738eSAdrian Chadd  * Complete the filtered frame TX completion.
3019f1bc738eSAdrian Chadd  *
3020f1bc738eSAdrian Chadd  * If there are no more frames in the hardware queue, unpause/unfilter
3021f1bc738eSAdrian Chadd  * the TID if applicable.  Otherwise we will wait for a node PS transition
3022f1bc738eSAdrian Chadd  * to unfilter.
3023f1bc738eSAdrian Chadd  */
3024f1bc738eSAdrian Chadd static void
3025f1bc738eSAdrian Chadd ath_tx_tid_filt_comp_complete(struct ath_softc *sc, struct ath_tid *tid)
3026f1bc738eSAdrian Chadd {
3027f1bc738eSAdrian Chadd 	struct ath_buf *bf;
3028f1bc738eSAdrian Chadd 
3029f1bc738eSAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
3030f1bc738eSAdrian Chadd 
3031f1bc738eSAdrian Chadd 	if (tid->hwq_depth != 0)
3032f1bc738eSAdrian Chadd 		return;
3033f1bc738eSAdrian Chadd 
3034f1bc738eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_FILT, "%s: hwq=0, transition back\n",
3035f1bc738eSAdrian Chadd 	    __func__);
3036f1bc738eSAdrian Chadd 	tid->isfiltered = 0;
3037f1bc738eSAdrian Chadd 	tid->clrdmask = 1;
3038f1bc738eSAdrian Chadd 
3039f1bc738eSAdrian Chadd 	/* XXX this is really quite inefficient */
304013aa9ee5SAdrian Chadd 	while ((bf = ATH_TID_FILT_LAST(tid, ath_bufhead_s)) != NULL) {
304113aa9ee5SAdrian Chadd 		ATH_TID_FILT_REMOVE(tid, bf, bf_list);
30423e6cc97fSAdrian Chadd 		ATH_TID_INSERT_HEAD(tid, bf, bf_list);
3043f1bc738eSAdrian Chadd 	}
3044f1bc738eSAdrian Chadd 
3045f1bc738eSAdrian Chadd 	ath_tx_tid_resume(sc, tid);
3046f1bc738eSAdrian Chadd }
3047f1bc738eSAdrian Chadd 
3048f1bc738eSAdrian Chadd /*
3049f1bc738eSAdrian Chadd  * Called when a single (aggregate or otherwise) frame is completed.
3050f1bc738eSAdrian Chadd  *
3051f1bc738eSAdrian Chadd  * Returns 1 if the buffer could be added to the filtered list
3052f1bc738eSAdrian Chadd  * (cloned or otherwise), 0 if the buffer couldn't be added to the
3053f1bc738eSAdrian Chadd  * filtered list (failed clone; expired retry) and the caller should
3054f1bc738eSAdrian Chadd  * free it and handle it like a failure (eg by sending a BAR.)
3055f1bc738eSAdrian Chadd  */
3056f1bc738eSAdrian Chadd static int
3057f1bc738eSAdrian Chadd ath_tx_tid_filt_comp_single(struct ath_softc *sc, struct ath_tid *tid,
3058f1bc738eSAdrian Chadd     struct ath_buf *bf)
3059f1bc738eSAdrian Chadd {
3060f1bc738eSAdrian Chadd 	struct ath_buf *nbf;
3061f1bc738eSAdrian Chadd 	int retval;
3062f1bc738eSAdrian Chadd 
3063f1bc738eSAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
3064f1bc738eSAdrian Chadd 
3065f1bc738eSAdrian Chadd 	/*
3066f1bc738eSAdrian Chadd 	 * Don't allow a filtered frame to live forever.
3067f1bc738eSAdrian Chadd 	 */
3068f1bc738eSAdrian Chadd 	if (bf->bf_state.bfs_retries > SWMAX_RETRIES) {
30690eb81626SAdrian Chadd 		sc->sc_stats.ast_tx_swretrymax++;
3070f1bc738eSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3071f1bc738eSAdrian Chadd 		    "%s: bf=%p, seqno=%d, exceeded retries\n",
3072f1bc738eSAdrian Chadd 		    __func__,
3073f1bc738eSAdrian Chadd 		    bf,
3074f1bc738eSAdrian Chadd 		    bf->bf_state.bfs_seqno);
3075f1bc738eSAdrian Chadd 		return (0);
3076f1bc738eSAdrian Chadd 	}
3077f1bc738eSAdrian Chadd 
3078f1bc738eSAdrian Chadd 	/*
3079f1bc738eSAdrian Chadd 	 * A busy buffer can't be added to the retry list.
3080f1bc738eSAdrian Chadd 	 * It needs to be cloned.
3081f1bc738eSAdrian Chadd 	 */
3082f1bc738eSAdrian Chadd 	if (bf->bf_flags & ATH_BUF_BUSY) {
3083f1bc738eSAdrian Chadd 		nbf = ath_tx_retry_clone(sc, tid->an, tid, bf);
3084f1bc738eSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3085f1bc738eSAdrian Chadd 		    "%s: busy buffer clone: %p -> %p\n",
3086f1bc738eSAdrian Chadd 		    __func__, bf, nbf);
3087f1bc738eSAdrian Chadd 	} else {
3088f1bc738eSAdrian Chadd 		nbf = bf;
3089f1bc738eSAdrian Chadd 	}
3090f1bc738eSAdrian Chadd 
3091f1bc738eSAdrian Chadd 	if (nbf == NULL) {
3092f1bc738eSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3093f1bc738eSAdrian Chadd 		    "%s: busy buffer couldn't be cloned (%p)!\n",
3094f1bc738eSAdrian Chadd 		    __func__, bf);
3095f1bc738eSAdrian Chadd 		retval = 1;
3096f1bc738eSAdrian Chadd 	} else {
3097f1bc738eSAdrian Chadd 		ath_tx_tid_filt_comp_buf(sc, tid, nbf);
3098f1bc738eSAdrian Chadd 		retval = 0;
3099f1bc738eSAdrian Chadd 	}
3100f1bc738eSAdrian Chadd 	ath_tx_tid_filt_comp_complete(sc, tid);
3101f1bc738eSAdrian Chadd 
3102f1bc738eSAdrian Chadd 	return (retval);
3103f1bc738eSAdrian Chadd }
3104f1bc738eSAdrian Chadd 
3105f1bc738eSAdrian Chadd static void
3106f1bc738eSAdrian Chadd ath_tx_tid_filt_comp_aggr(struct ath_softc *sc, struct ath_tid *tid,
3107f1bc738eSAdrian Chadd     struct ath_buf *bf_first, ath_bufhead *bf_q)
3108f1bc738eSAdrian Chadd {
3109f1bc738eSAdrian Chadd 	struct ath_buf *bf, *bf_next, *nbf;
3110f1bc738eSAdrian Chadd 
3111f1bc738eSAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
3112f1bc738eSAdrian Chadd 
3113f1bc738eSAdrian Chadd 	bf = bf_first;
3114f1bc738eSAdrian Chadd 	while (bf) {
3115f1bc738eSAdrian Chadd 		bf_next = bf->bf_next;
3116f1bc738eSAdrian Chadd 		bf->bf_next = NULL;	/* Remove it from the aggr list */
3117f1bc738eSAdrian Chadd 
3118f1bc738eSAdrian Chadd 		/*
3119f1bc738eSAdrian Chadd 		 * Don't allow a filtered frame to live forever.
3120f1bc738eSAdrian Chadd 		 */
3121f1bc738eSAdrian Chadd 		if (bf->bf_state.bfs_retries > SWMAX_RETRIES) {
31220eb81626SAdrian Chadd 		sc->sc_stats.ast_tx_swretrymax++;
3123f1bc738eSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3124f1bc738eSAdrian Chadd 			    "%s: bf=%p, seqno=%d, exceeded retries\n",
3125f1bc738eSAdrian Chadd 			    __func__,
3126f1bc738eSAdrian Chadd 			    bf,
3127f1bc738eSAdrian Chadd 			    bf->bf_state.bfs_seqno);
3128f1bc738eSAdrian Chadd 			TAILQ_INSERT_TAIL(bf_q, bf, bf_list);
3129f1bc738eSAdrian Chadd 			goto next;
3130f1bc738eSAdrian Chadd 		}
3131f1bc738eSAdrian Chadd 
3132f1bc738eSAdrian Chadd 		if (bf->bf_flags & ATH_BUF_BUSY) {
3133f1bc738eSAdrian Chadd 			nbf = ath_tx_retry_clone(sc, tid->an, tid, bf);
3134f1bc738eSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3135f1bc738eSAdrian Chadd 			    "%s: busy buffer cloned: %p -> %p",
3136f1bc738eSAdrian Chadd 			    __func__, bf, nbf);
3137f1bc738eSAdrian Chadd 		} else {
3138f1bc738eSAdrian Chadd 			nbf = bf;
3139f1bc738eSAdrian Chadd 		}
3140f1bc738eSAdrian Chadd 
3141f1bc738eSAdrian Chadd 		/*
3142f1bc738eSAdrian Chadd 		 * If the buffer couldn't be cloned, add it to bf_q;
3143f1bc738eSAdrian Chadd 		 * the caller will free the buffer(s) as required.
3144f1bc738eSAdrian Chadd 		 */
3145f1bc738eSAdrian Chadd 		if (nbf == NULL) {
3146f1bc738eSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3147f1bc738eSAdrian Chadd 			    "%s: buffer couldn't be cloned! (%p)\n",
3148f1bc738eSAdrian Chadd 			    __func__, bf);
3149f1bc738eSAdrian Chadd 			TAILQ_INSERT_TAIL(bf_q, bf, bf_list);
3150f1bc738eSAdrian Chadd 		} else {
3151f1bc738eSAdrian Chadd 			ath_tx_tid_filt_comp_buf(sc, tid, nbf);
3152f1bc738eSAdrian Chadd 		}
3153f1bc738eSAdrian Chadd next:
3154f1bc738eSAdrian Chadd 		bf = bf_next;
3155f1bc738eSAdrian Chadd 	}
3156f1bc738eSAdrian Chadd 
3157f1bc738eSAdrian Chadd 	ath_tx_tid_filt_comp_complete(sc, tid);
3158f1bc738eSAdrian Chadd }
3159f1bc738eSAdrian Chadd 
3160f1bc738eSAdrian Chadd /*
316188b3d483SAdrian Chadd  * Suspend the queue because we need to TX a BAR.
316288b3d483SAdrian Chadd  */
316388b3d483SAdrian Chadd static void
316488b3d483SAdrian Chadd ath_tx_tid_bar_suspend(struct ath_softc *sc, struct ath_tid *tid)
316588b3d483SAdrian Chadd {
316688b3d483SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
316788b3d483SAdrian Chadd 
31680e22ed0eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
3169e60c4fc2SAdrian Chadd 	    "%s: tid=%p, bar_wait=%d, bar_tx=%d, called\n",
317088b3d483SAdrian Chadd 	    __func__,
3171e60c4fc2SAdrian Chadd 	    tid,
3172e60c4fc2SAdrian Chadd 	    tid->bar_wait,
3173e60c4fc2SAdrian Chadd 	    tid->bar_tx);
317488b3d483SAdrian Chadd 
317588b3d483SAdrian Chadd 	/* We shouldn't be called when bar_tx is 1 */
317688b3d483SAdrian Chadd 	if (tid->bar_tx) {
317788b3d483SAdrian Chadd 		device_printf(sc->sc_dev, "%s: bar_tx is 1?!\n",
317888b3d483SAdrian Chadd 		    __func__);
317988b3d483SAdrian Chadd 	}
318088b3d483SAdrian Chadd 
318188b3d483SAdrian Chadd 	/* If we've already been called, just be patient. */
318288b3d483SAdrian Chadd 	if (tid->bar_wait)
318388b3d483SAdrian Chadd 		return;
318488b3d483SAdrian Chadd 
318588b3d483SAdrian Chadd 	/* Wait! */
318688b3d483SAdrian Chadd 	tid->bar_wait = 1;
318788b3d483SAdrian Chadd 
318888b3d483SAdrian Chadd 	/* Only one pause, no matter how many frames fail */
318988b3d483SAdrian Chadd 	ath_tx_tid_pause(sc, tid);
319088b3d483SAdrian Chadd }
319188b3d483SAdrian Chadd 
319288b3d483SAdrian Chadd /*
319388b3d483SAdrian Chadd  * We've finished with BAR handling - either we succeeded or
319488b3d483SAdrian Chadd  * failed. Either way, unsuspend TX.
319588b3d483SAdrian Chadd  */
319688b3d483SAdrian Chadd static void
319788b3d483SAdrian Chadd ath_tx_tid_bar_unsuspend(struct ath_softc *sc, struct ath_tid *tid)
319888b3d483SAdrian Chadd {
319988b3d483SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
320088b3d483SAdrian Chadd 
32010e22ed0eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
320288b3d483SAdrian Chadd 	    "%s: tid=%p, called\n",
320388b3d483SAdrian Chadd 	    __func__,
320488b3d483SAdrian Chadd 	    tid);
320588b3d483SAdrian Chadd 
320688b3d483SAdrian Chadd 	if (tid->bar_tx == 0 || tid->bar_wait == 0) {
320788b3d483SAdrian Chadd 		device_printf(sc->sc_dev, "%s: bar_tx=%d, bar_wait=%d: ?\n",
320888b3d483SAdrian Chadd 		    __func__, tid->bar_tx, tid->bar_wait);
320988b3d483SAdrian Chadd 	}
321088b3d483SAdrian Chadd 
321188b3d483SAdrian Chadd 	tid->bar_tx = tid->bar_wait = 0;
321288b3d483SAdrian Chadd 	ath_tx_tid_resume(sc, tid);
321388b3d483SAdrian Chadd }
321488b3d483SAdrian Chadd 
321588b3d483SAdrian Chadd /*
321688b3d483SAdrian Chadd  * Return whether we're ready to TX a BAR frame.
321788b3d483SAdrian Chadd  *
321888b3d483SAdrian Chadd  * Requires the TID lock be held.
321988b3d483SAdrian Chadd  */
322088b3d483SAdrian Chadd static int
322188b3d483SAdrian Chadd ath_tx_tid_bar_tx_ready(struct ath_softc *sc, struct ath_tid *tid)
322288b3d483SAdrian Chadd {
322388b3d483SAdrian Chadd 
322488b3d483SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
322588b3d483SAdrian Chadd 
322688b3d483SAdrian Chadd 	if (tid->bar_wait == 0 || tid->hwq_depth > 0)
322788b3d483SAdrian Chadd 		return (0);
322888b3d483SAdrian Chadd 
32290e22ed0eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR, "%s: tid=%p (%d), bar ready\n",
32300e22ed0eSAdrian Chadd 	    __func__, tid, tid->tid);
32310e22ed0eSAdrian Chadd 
323288b3d483SAdrian Chadd 	return (1);
323388b3d483SAdrian Chadd }
323488b3d483SAdrian Chadd 
323588b3d483SAdrian Chadd /*
323688b3d483SAdrian Chadd  * Check whether the current TID is ready to have a BAR
323788b3d483SAdrian Chadd  * TXed and if so, do the TX.
323888b3d483SAdrian Chadd  *
323988b3d483SAdrian Chadd  * Since the TID/TXQ lock can't be held during a call to
324088b3d483SAdrian Chadd  * ieee80211_send_bar(), we have to do the dirty thing of unlocking it,
324188b3d483SAdrian Chadd  * sending the BAR and locking it again.
324288b3d483SAdrian Chadd  *
324388b3d483SAdrian Chadd  * Eventually, the code to send the BAR should be broken out
324488b3d483SAdrian Chadd  * from this routine so the lock doesn't have to be reacquired
324588b3d483SAdrian Chadd  * just to be immediately dropped by the caller.
324688b3d483SAdrian Chadd  */
324788b3d483SAdrian Chadd static void
324888b3d483SAdrian Chadd ath_tx_tid_bar_tx(struct ath_softc *sc, struct ath_tid *tid)
324988b3d483SAdrian Chadd {
325088b3d483SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
325188b3d483SAdrian Chadd 
325288b3d483SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
325388b3d483SAdrian Chadd 
32540e22ed0eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
325588b3d483SAdrian Chadd 	    "%s: tid=%p, called\n",
325688b3d483SAdrian Chadd 	    __func__,
325788b3d483SAdrian Chadd 	    tid);
325888b3d483SAdrian Chadd 
325988b3d483SAdrian Chadd 	tap = ath_tx_get_tx_tid(tid->an, tid->tid);
326088b3d483SAdrian Chadd 
326188b3d483SAdrian Chadd 	/*
326288b3d483SAdrian Chadd 	 * This is an error condition!
326388b3d483SAdrian Chadd 	 */
326488b3d483SAdrian Chadd 	if (tid->bar_wait == 0 || tid->bar_tx == 1) {
326588b3d483SAdrian Chadd 		device_printf(sc->sc_dev,
326688b3d483SAdrian Chadd 		    "%s: tid=%p, bar_tx=%d, bar_wait=%d: ?\n",
326788b3d483SAdrian Chadd 		    __func__,
326888b3d483SAdrian Chadd 		    tid,
326988b3d483SAdrian Chadd 		    tid->bar_tx,
327088b3d483SAdrian Chadd 		    tid->bar_wait);
327188b3d483SAdrian Chadd 		return;
327288b3d483SAdrian Chadd 	}
327388b3d483SAdrian Chadd 
327488b3d483SAdrian Chadd 	/* Don't do anything if we still have pending frames */
327588b3d483SAdrian Chadd 	if (tid->hwq_depth > 0) {
32760e22ed0eSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
327788b3d483SAdrian Chadd 		    "%s: tid=%p, hwq_depth=%d, waiting\n",
327888b3d483SAdrian Chadd 		    __func__,
327988b3d483SAdrian Chadd 		    tid,
328088b3d483SAdrian Chadd 		    tid->hwq_depth);
328188b3d483SAdrian Chadd 		return;
328288b3d483SAdrian Chadd 	}
328388b3d483SAdrian Chadd 
328488b3d483SAdrian Chadd 	/* We're now about to TX */
328588b3d483SAdrian Chadd 	tid->bar_tx = 1;
328688b3d483SAdrian Chadd 
328788b3d483SAdrian Chadd 	/*
32884e81f27cSAdrian Chadd 	 * Override the clrdmask configuration for the next frame,
32894e81f27cSAdrian Chadd 	 * just to get the ball rolling.
32904e81f27cSAdrian Chadd 	 */
32914e81f27cSAdrian Chadd 	tid->clrdmask = 1;
32924e81f27cSAdrian Chadd 
32934e81f27cSAdrian Chadd 	/*
329488b3d483SAdrian Chadd 	 * Calculate new BAW left edge, now that all frames have either
329588b3d483SAdrian Chadd 	 * succeeded or failed.
329688b3d483SAdrian Chadd 	 *
329788b3d483SAdrian Chadd 	 * XXX verify this is _actually_ the valid value to begin at!
329888b3d483SAdrian Chadd 	 */
32990e22ed0eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
330088b3d483SAdrian Chadd 	    "%s: tid=%p, new BAW left edge=%d\n",
330188b3d483SAdrian Chadd 	    __func__,
330288b3d483SAdrian Chadd 	    tid,
330388b3d483SAdrian Chadd 	    tap->txa_start);
330488b3d483SAdrian Chadd 
330588b3d483SAdrian Chadd 	/* Try sending the BAR frame */
330688b3d483SAdrian Chadd 	/* We can't hold the lock here! */
330788b3d483SAdrian Chadd 
330888b3d483SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[tid->ac]);
330988b3d483SAdrian Chadd 	if (ieee80211_send_bar(&tid->an->an_node, tap, tap->txa_start) == 0) {
331088b3d483SAdrian Chadd 		/* Success? Now we wait for notification that it's done */
331188b3d483SAdrian Chadd 		ATH_TXQ_LOCK(sc->sc_ac2q[tid->ac]);
331288b3d483SAdrian Chadd 		return;
331388b3d483SAdrian Chadd 	}
331488b3d483SAdrian Chadd 
331588b3d483SAdrian Chadd 	/* Failure? For now, warn loudly and continue */
331688b3d483SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[tid->ac]);
331788b3d483SAdrian Chadd 	device_printf(sc->sc_dev, "%s: tid=%p, failed to TX BAR, continue!\n",
331888b3d483SAdrian Chadd 	    __func__, tid);
331988b3d483SAdrian Chadd 	ath_tx_tid_bar_unsuspend(sc, tid);
332088b3d483SAdrian Chadd }
332188b3d483SAdrian Chadd 
3322eb6f0de0SAdrian Chadd static void
3323f1bc738eSAdrian Chadd ath_tx_tid_drain_pkt(struct ath_softc *sc, struct ath_node *an,
3324f1bc738eSAdrian Chadd     struct ath_tid *tid, ath_bufhead *bf_cq, struct ath_buf *bf)
3325eb6f0de0SAdrian Chadd {
3326eb6f0de0SAdrian Chadd 
3327f1bc738eSAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
3328eb6f0de0SAdrian Chadd 
3329eb6f0de0SAdrian Chadd 	/*
3330eb6f0de0SAdrian Chadd 	 * If the current TID is running AMPDU, update
3331eb6f0de0SAdrian Chadd 	 * the BAW.
3332eb6f0de0SAdrian Chadd 	 */
3333eb6f0de0SAdrian Chadd 	if (ath_tx_ampdu_running(sc, an, tid->tid) &&
3334eb6f0de0SAdrian Chadd 	    bf->bf_state.bfs_dobaw) {
3335eb6f0de0SAdrian Chadd 		/*
3336eb6f0de0SAdrian Chadd 		 * Only remove the frame from the BAW if it's
3337eb6f0de0SAdrian Chadd 		 * been transmitted at least once; this means
3338eb6f0de0SAdrian Chadd 		 * the frame was in the BAW to begin with.
3339eb6f0de0SAdrian Chadd 		 */
3340eb6f0de0SAdrian Chadd 		if (bf->bf_state.bfs_retries > 0) {
3341eb6f0de0SAdrian Chadd 			ath_tx_update_baw(sc, an, tid, bf);
3342eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_dobaw = 0;
3343eb6f0de0SAdrian Chadd 		}
3344eb6f0de0SAdrian Chadd 		/*
3345eb6f0de0SAdrian Chadd 		 * This has become a non-fatal error now
3346eb6f0de0SAdrian Chadd 		 */
3347eb6f0de0SAdrian Chadd 		if (! bf->bf_state.bfs_addedbaw)
3348eb6f0de0SAdrian Chadd 			device_printf(sc->sc_dev,
3349eb6f0de0SAdrian Chadd 			    "%s: wasn't added: seqno %d\n",
3350eb6f0de0SAdrian Chadd 			    __func__, SEQNO(bf->bf_state.bfs_seqno));
3351eb6f0de0SAdrian Chadd 	}
3352eb6f0de0SAdrian Chadd 	TAILQ_INSERT_TAIL(bf_cq, bf, bf_list);
3353eb6f0de0SAdrian Chadd }
3354eb6f0de0SAdrian Chadd 
3355f1bc738eSAdrian Chadd static void
3356f1bc738eSAdrian Chadd ath_tx_tid_drain_print(struct ath_softc *sc, struct ath_node *an,
335703682514SAdrian Chadd     const char *pfx, struct ath_tid *tid, struct ath_buf *bf)
3358f1bc738eSAdrian Chadd {
3359f1bc738eSAdrian Chadd 	struct ieee80211_node *ni = &an->an_node;
3360f1bc738eSAdrian Chadd 	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
3361f1bc738eSAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
3362f1bc738eSAdrian Chadd 
3363f1bc738eSAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
3364f1bc738eSAdrian Chadd 
3365f1bc738eSAdrian Chadd 	device_printf(sc->sc_dev,
336603682514SAdrian Chadd 	    "%s: %s: node %p: bf=%p: addbaw=%d, dobaw=%d, "
3367f1bc738eSAdrian Chadd 	    "seqno=%d, retry=%d\n",
336803682514SAdrian Chadd 	    __func__, pfx, ni, bf,
3369f1bc738eSAdrian Chadd 	    bf->bf_state.bfs_addedbaw,
3370f1bc738eSAdrian Chadd 	    bf->bf_state.bfs_dobaw,
3371f1bc738eSAdrian Chadd 	    SEQNO(bf->bf_state.bfs_seqno),
3372f1bc738eSAdrian Chadd 	    bf->bf_state.bfs_retries);
3373f1bc738eSAdrian Chadd 	device_printf(sc->sc_dev,
337403682514SAdrian Chadd 	    "%s: node %p: bf=%p: txq[%d] axq_depth=%d, axq_aggr_depth=%d\n",
33754e81f27cSAdrian Chadd 	        __func__, ni, bf,
337603682514SAdrian Chadd 	    txq->axq_qnum,
33774e81f27cSAdrian Chadd 	    txq->axq_depth,
33784e81f27cSAdrian Chadd 	    txq->axq_aggr_depth);
33794e81f27cSAdrian Chadd 
33804e81f27cSAdrian Chadd 	device_printf(sc->sc_dev,
3381f1bc738eSAdrian Chadd 	    "%s: node %p: bf=%p: tid txq_depth=%d hwq_depth=%d, bar_wait=%d, isfiltered=%d\n",
3382f1bc738eSAdrian Chadd 	    __func__, ni, bf,
3383f1bc738eSAdrian Chadd 	    tid->axq_depth,
3384f1bc738eSAdrian Chadd 	    tid->hwq_depth,
3385f1bc738eSAdrian Chadd 	    tid->bar_wait,
3386f1bc738eSAdrian Chadd 	    tid->isfiltered);
3387f1bc738eSAdrian Chadd 	device_printf(sc->sc_dev,
33884e81f27cSAdrian Chadd 	    "%s: node %p: tid %d: "
33894e81f27cSAdrian Chadd 	    "sched=%d, paused=%d, "
33904e81f27cSAdrian Chadd 	    "incomp=%d, baw_head=%d, "
3391f1bc738eSAdrian Chadd 	    "baw_tail=%d txa_start=%d, ni_txseqs=%d\n",
33924e81f27cSAdrian Chadd 	     __func__, ni, tid->tid,
33934e81f27cSAdrian Chadd 	     tid->sched, tid->paused,
33944e81f27cSAdrian Chadd 	     tid->incomp, tid->baw_head,
3395f1bc738eSAdrian Chadd 	     tid->baw_tail, tap == NULL ? -1 : tap->txa_start,
3396f1bc738eSAdrian Chadd 	     ni->ni_txseqs[tid->tid]);
3397f1bc738eSAdrian Chadd 
3398f1bc738eSAdrian Chadd 	/* XXX Dump the frame, see what it is? */
3399f1bc738eSAdrian Chadd 	ieee80211_dump_pkt(ni->ni_ic,
3400f1bc738eSAdrian Chadd 	    mtod(bf->bf_m, const uint8_t *),
3401f1bc738eSAdrian Chadd 	    bf->bf_m->m_len, 0, -1);
3402f1bc738eSAdrian Chadd }
3403f1bc738eSAdrian Chadd 
3404f1bc738eSAdrian Chadd /*
3405f1bc738eSAdrian Chadd  * Free any packets currently pending in the software TX queue.
3406f1bc738eSAdrian Chadd  *
3407f1bc738eSAdrian Chadd  * This will be called when a node is being deleted.
3408f1bc738eSAdrian Chadd  *
3409f1bc738eSAdrian Chadd  * It can also be called on an active node during an interface
3410f1bc738eSAdrian Chadd  * reset or state transition.
3411f1bc738eSAdrian Chadd  *
3412f1bc738eSAdrian Chadd  * (From Linux/reference):
3413f1bc738eSAdrian Chadd  *
3414f1bc738eSAdrian Chadd  * TODO: For frame(s) that are in the retry state, we will reuse the
3415f1bc738eSAdrian Chadd  * sequence number(s) without setting the retry bit. The
3416f1bc738eSAdrian Chadd  * alternative is to give up on these and BAR the receiver's window
3417f1bc738eSAdrian Chadd  * forward.
3418f1bc738eSAdrian Chadd  */
3419f1bc738eSAdrian Chadd static void
3420f1bc738eSAdrian Chadd ath_tx_tid_drain(struct ath_softc *sc, struct ath_node *an,
3421f1bc738eSAdrian Chadd     struct ath_tid *tid, ath_bufhead *bf_cq)
3422f1bc738eSAdrian Chadd {
3423f1bc738eSAdrian Chadd 	struct ath_buf *bf;
3424f1bc738eSAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
3425f1bc738eSAdrian Chadd 	struct ieee80211_node *ni = &an->an_node;
3426f1bc738eSAdrian Chadd 	int t;
3427f1bc738eSAdrian Chadd 
3428f1bc738eSAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
3429f1bc738eSAdrian Chadd 
3430f1bc738eSAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
3431f1bc738eSAdrian Chadd 
3432f1bc738eSAdrian Chadd 	/* Walk the queue, free frames */
3433f1bc738eSAdrian Chadd 	t = 0;
3434f1bc738eSAdrian Chadd 	for (;;) {
34353e6cc97fSAdrian Chadd 		bf = ATH_TID_FIRST(tid);
3436f1bc738eSAdrian Chadd 		if (bf == NULL) {
3437f1bc738eSAdrian Chadd 			break;
3438f1bc738eSAdrian Chadd 		}
3439f1bc738eSAdrian Chadd 
3440f1bc738eSAdrian Chadd 		if (t == 0) {
344103682514SAdrian Chadd 			ath_tx_tid_drain_print(sc, an, "norm", tid, bf);
3442f1bc738eSAdrian Chadd 			t = 1;
3443f1bc738eSAdrian Chadd 		}
3444f1bc738eSAdrian Chadd 
34453e6cc97fSAdrian Chadd 		ATH_TID_REMOVE(tid, bf, bf_list);
3446f1bc738eSAdrian Chadd 		ath_tx_tid_drain_pkt(sc, an, tid, bf_cq, bf);
3447f1bc738eSAdrian Chadd 	}
3448f1bc738eSAdrian Chadd 
3449f1bc738eSAdrian Chadd 	/* And now, drain the filtered frame queue */
3450f1bc738eSAdrian Chadd 	t = 0;
3451f1bc738eSAdrian Chadd 	for (;;) {
345213aa9ee5SAdrian Chadd 		bf = ATH_TID_FILT_FIRST(tid);
3453f1bc738eSAdrian Chadd 		if (bf == NULL)
3454f1bc738eSAdrian Chadd 			break;
3455f1bc738eSAdrian Chadd 
3456f1bc738eSAdrian Chadd 		if (t == 0) {
345703682514SAdrian Chadd 			ath_tx_tid_drain_print(sc, an, "filt", tid, bf);
3458f1bc738eSAdrian Chadd 			t = 1;
3459f1bc738eSAdrian Chadd 		}
3460f1bc738eSAdrian Chadd 
346113aa9ee5SAdrian Chadd 		ATH_TID_FILT_REMOVE(tid, bf, bf_list);
3462f1bc738eSAdrian Chadd 		ath_tx_tid_drain_pkt(sc, an, tid, bf_cq, bf);
3463f1bc738eSAdrian Chadd 	}
3464f1bc738eSAdrian Chadd 
3465eb6f0de0SAdrian Chadd 	/*
34664e81f27cSAdrian Chadd 	 * Override the clrdmask configuration for the next frame
34674e81f27cSAdrian Chadd 	 * in case there is some future transmission, just to get
34684e81f27cSAdrian Chadd 	 * the ball rolling.
34694e81f27cSAdrian Chadd 	 *
34704e81f27cSAdrian Chadd 	 * This won't hurt things if the TID is about to be freed.
34714e81f27cSAdrian Chadd 	 */
34724e81f27cSAdrian Chadd 	tid->clrdmask = 1;
34734e81f27cSAdrian Chadd 
34744e81f27cSAdrian Chadd 	/*
3475eb6f0de0SAdrian Chadd 	 * Now that it's completed, grab the TID lock and update
3476eb6f0de0SAdrian Chadd 	 * the sequence number and BAW window.
3477eb6f0de0SAdrian Chadd 	 * Because sequence numbers have been assigned to frames
3478eb6f0de0SAdrian Chadd 	 * that haven't been sent yet, it's entirely possible
3479eb6f0de0SAdrian Chadd 	 * we'll be called with some pending frames that have not
3480eb6f0de0SAdrian Chadd 	 * been transmitted.
3481eb6f0de0SAdrian Chadd 	 *
3482eb6f0de0SAdrian Chadd 	 * The cleaner solution is to do the sequence number allocation
3483eb6f0de0SAdrian Chadd 	 * when the packet is first transmitted - and thus the "retries"
3484eb6f0de0SAdrian Chadd 	 * check above would be enough to update the BAW/seqno.
3485eb6f0de0SAdrian Chadd 	 */
3486eb6f0de0SAdrian Chadd 
3487eb6f0de0SAdrian Chadd 	/* But don't do it for non-QoS TIDs */
3488eb6f0de0SAdrian Chadd 	if (tap) {
3489eb6f0de0SAdrian Chadd #if 0
3490eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
3491eb6f0de0SAdrian Chadd 		    "%s: node %p: TID %d: sliding BAW left edge to %d\n",
3492eb6f0de0SAdrian Chadd 		    __func__, an, tid->tid, tap->txa_start);
3493eb6f0de0SAdrian Chadd #endif
3494eb6f0de0SAdrian Chadd 		ni->ni_txseqs[tid->tid] = tap->txa_start;
3495eb6f0de0SAdrian Chadd 		tid->baw_tail = tid->baw_head;
3496eb6f0de0SAdrian Chadd 	}
3497eb6f0de0SAdrian Chadd }
3498eb6f0de0SAdrian Chadd 
3499eb6f0de0SAdrian Chadd /*
3500eb6f0de0SAdrian Chadd  * Flush all software queued packets for the given node.
3501eb6f0de0SAdrian Chadd  *
3502eb6f0de0SAdrian Chadd  * This occurs when a completion handler frees the last buffer
3503eb6f0de0SAdrian Chadd  * for a node, and the node is thus freed. This causes the node
3504eb6f0de0SAdrian Chadd  * to be cleaned up, which ends up calling ath_tx_node_flush.
3505eb6f0de0SAdrian Chadd  */
3506eb6f0de0SAdrian Chadd void
3507eb6f0de0SAdrian Chadd ath_tx_node_flush(struct ath_softc *sc, struct ath_node *an)
3508eb6f0de0SAdrian Chadd {
3509eb6f0de0SAdrian Chadd 	int tid;
3510eb6f0de0SAdrian Chadd 	ath_bufhead bf_cq;
3511eb6f0de0SAdrian Chadd 	struct ath_buf *bf;
3512eb6f0de0SAdrian Chadd 
3513eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_cq);
3514eb6f0de0SAdrian Chadd 
351503682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_NODE, 1, "ath_tx_node_flush: flush node; ni=%p",
351603682514SAdrian Chadd 	    &an->an_node);
351703682514SAdrian Chadd 
3518eb6f0de0SAdrian Chadd 	for (tid = 0; tid < IEEE80211_TID_SIZE; tid++) {
3519eb6f0de0SAdrian Chadd 		struct ath_tid *atid = &an->an_tid[tid];
3520eb6f0de0SAdrian Chadd 		struct ath_txq *txq = sc->sc_ac2q[atid->ac];
3521eb6f0de0SAdrian Chadd 
3522eb6f0de0SAdrian Chadd 		ATH_TXQ_LOCK(txq);
3523eb6f0de0SAdrian Chadd 		/* Free packets */
3524eb6f0de0SAdrian Chadd 		ath_tx_tid_drain(sc, an, atid, &bf_cq);
352523f44d2bSAdrian Chadd 		/* Remove this tid from the list of active tids */
352623f44d2bSAdrian Chadd 		ath_tx_tid_unsched(sc, atid);
3527eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
3528eb6f0de0SAdrian Chadd 	}
3529eb6f0de0SAdrian Chadd 
3530eb6f0de0SAdrian Chadd 	/* Handle completed frames */
3531eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
3532eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
3533eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
3534eb6f0de0SAdrian Chadd 	}
3535eb6f0de0SAdrian Chadd }
3536eb6f0de0SAdrian Chadd 
3537eb6f0de0SAdrian Chadd /*
3538eb6f0de0SAdrian Chadd  * Drain all the software TXQs currently with traffic queued.
3539eb6f0de0SAdrian Chadd  */
3540eb6f0de0SAdrian Chadd void
3541eb6f0de0SAdrian Chadd ath_tx_txq_drain(struct ath_softc *sc, struct ath_txq *txq)
3542eb6f0de0SAdrian Chadd {
3543eb6f0de0SAdrian Chadd 	struct ath_tid *tid;
3544eb6f0de0SAdrian Chadd 	ath_bufhead bf_cq;
3545eb6f0de0SAdrian Chadd 	struct ath_buf *bf;
3546eb6f0de0SAdrian Chadd 
3547eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_cq);
3548eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(txq);
3549eb6f0de0SAdrian Chadd 
3550eb6f0de0SAdrian Chadd 	/*
3551eb6f0de0SAdrian Chadd 	 * Iterate over all active tids for the given txq,
3552eb6f0de0SAdrian Chadd 	 * flushing and unsched'ing them
3553eb6f0de0SAdrian Chadd 	 */
3554eb6f0de0SAdrian Chadd 	while (! TAILQ_EMPTY(&txq->axq_tidq)) {
3555eb6f0de0SAdrian Chadd 		tid = TAILQ_FIRST(&txq->axq_tidq);
3556eb6f0de0SAdrian Chadd 		ath_tx_tid_drain(sc, tid->an, tid, &bf_cq);
3557eb6f0de0SAdrian Chadd 		ath_tx_tid_unsched(sc, tid);
3558eb6f0de0SAdrian Chadd 	}
3559eb6f0de0SAdrian Chadd 
3560eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(txq);
3561eb6f0de0SAdrian Chadd 
3562eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
3563eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
3564eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
3565eb6f0de0SAdrian Chadd 	}
3566eb6f0de0SAdrian Chadd }
3567eb6f0de0SAdrian Chadd 
3568eb6f0de0SAdrian Chadd /*
3569eb6f0de0SAdrian Chadd  * Handle completion of non-aggregate session frames.
35700c54de88SAdrian Chadd  *
35710c54de88SAdrian Chadd  * This (currently) doesn't implement software retransmission of
35720c54de88SAdrian Chadd  * non-aggregate frames!
35730c54de88SAdrian Chadd  *
35740c54de88SAdrian Chadd  * Software retransmission of non-aggregate frames needs to obey
35750c54de88SAdrian Chadd  * the strict sequence number ordering, and drop any frames that
35760c54de88SAdrian Chadd  * will fail this.
35770c54de88SAdrian Chadd  *
35780c54de88SAdrian Chadd  * For now, filtered frames and frame transmission will cause
35790c54de88SAdrian Chadd  * all kinds of issues.  So we don't support them.
35800c54de88SAdrian Chadd  *
35810c54de88SAdrian Chadd  * So anyone queuing frames via ath_tx_normal_xmit() or
35820c54de88SAdrian Chadd  * ath_tx_hw_queue_norm() must override and set CLRDMASK.
3583eb6f0de0SAdrian Chadd  */
3584eb6f0de0SAdrian Chadd void
3585eb6f0de0SAdrian Chadd ath_tx_normal_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
3586eb6f0de0SAdrian Chadd {
3587eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
3588eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
3589eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
3590eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
3591eb6f0de0SAdrian Chadd 	struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
3592eb6f0de0SAdrian Chadd 
3593eb6f0de0SAdrian Chadd 	/* The TID state is protected behind the TXQ lock */
3594eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
3595eb6f0de0SAdrian Chadd 
3596eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bf=%p: fail=%d, hwq_depth now %d\n",
3597eb6f0de0SAdrian Chadd 	    __func__, bf, fail, atid->hwq_depth - 1);
3598eb6f0de0SAdrian Chadd 
3599eb6f0de0SAdrian Chadd 	atid->hwq_depth--;
3600f1bc738eSAdrian Chadd 
36010c54de88SAdrian Chadd #if 0
36020c54de88SAdrian Chadd 	/*
36030c54de88SAdrian Chadd 	 * If the frame was filtered, stick it on the filter frame
36040c54de88SAdrian Chadd 	 * queue and complain about it.  It shouldn't happen!
36050c54de88SAdrian Chadd 	 */
36060c54de88SAdrian Chadd 	if ((ts->ts_status & HAL_TXERR_FILT) ||
36070c54de88SAdrian Chadd 	    (ts->ts_status != 0 && atid->isfiltered)) {
36080c54de88SAdrian Chadd 		device_printf(sc->sc_dev,
36090c54de88SAdrian Chadd 		    "%s: isfiltered=%d, ts_status=%d: huh?\n",
36100c54de88SAdrian Chadd 		    __func__,
36110c54de88SAdrian Chadd 		    atid->isfiltered,
36120c54de88SAdrian Chadd 		    ts->ts_status);
36130c54de88SAdrian Chadd 		ath_tx_tid_filt_comp_buf(sc, atid, bf);
36140c54de88SAdrian Chadd 	}
36150c54de88SAdrian Chadd #endif
3616f1bc738eSAdrian Chadd 	if (atid->isfiltered)
36170c54de88SAdrian Chadd 		device_printf(sc->sc_dev, "%s: filtered?!\n", __func__);
3618eb6f0de0SAdrian Chadd 	if (atid->hwq_depth < 0)
3619eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: hwq_depth < 0: %d\n",
3620eb6f0de0SAdrian Chadd 		    __func__, atid->hwq_depth);
3621f1bc738eSAdrian Chadd 
3622f1bc738eSAdrian Chadd 	/*
3623f1bc738eSAdrian Chadd 	 * If the queue is filtered, potentially mark it as complete
3624f1bc738eSAdrian Chadd 	 * and reschedule it as needed.
3625f1bc738eSAdrian Chadd 	 *
3626f1bc738eSAdrian Chadd 	 * This is required as there may be a subsequent TX descriptor
3627f1bc738eSAdrian Chadd 	 * for this end-node that has CLRDMASK set, so it's quite possible
3628f1bc738eSAdrian Chadd 	 * that a filtered frame will be followed by a non-filtered
3629f1bc738eSAdrian Chadd 	 * (complete or otherwise) frame.
3630f1bc738eSAdrian Chadd 	 *
3631f1bc738eSAdrian Chadd 	 * XXX should we do this before we complete the frame?
3632f1bc738eSAdrian Chadd 	 */
3633f1bc738eSAdrian Chadd 	if (atid->isfiltered)
3634f1bc738eSAdrian Chadd 		ath_tx_tid_filt_comp_complete(sc, atid);
3635eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3636eb6f0de0SAdrian Chadd 
3637eb6f0de0SAdrian Chadd 	/*
3638eb6f0de0SAdrian Chadd 	 * punt to rate control if we're not being cleaned up
3639eb6f0de0SAdrian Chadd 	 * during a hw queue drain and the frame wanted an ACK.
3640eb6f0de0SAdrian Chadd 	 */
3641875a9451SAdrian Chadd 	if (fail == 0 && ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0))
3642eb6f0de0SAdrian Chadd 		ath_tx_update_ratectrl(sc, ni, bf->bf_state.bfs_rc,
3643eb6f0de0SAdrian Chadd 		    ts, bf->bf_state.bfs_pktlen,
3644eb6f0de0SAdrian Chadd 		    1, (ts->ts_status == 0) ? 0 : 1);
3645eb6f0de0SAdrian Chadd 
3646eb6f0de0SAdrian Chadd 	ath_tx_default_comp(sc, bf, fail);
3647eb6f0de0SAdrian Chadd }
3648eb6f0de0SAdrian Chadd 
3649eb6f0de0SAdrian Chadd /*
3650eb6f0de0SAdrian Chadd  * Handle cleanup of aggregate session packets that aren't
3651eb6f0de0SAdrian Chadd  * an A-MPDU.
3652eb6f0de0SAdrian Chadd  *
3653eb6f0de0SAdrian Chadd  * There's no need to update the BAW here - the session is being
3654eb6f0de0SAdrian Chadd  * torn down.
3655eb6f0de0SAdrian Chadd  */
3656eb6f0de0SAdrian Chadd static void
3657eb6f0de0SAdrian Chadd ath_tx_comp_cleanup_unaggr(struct ath_softc *sc, struct ath_buf *bf)
3658eb6f0de0SAdrian Chadd {
3659eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
3660eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
3661eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
3662eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
3663eb6f0de0SAdrian Chadd 
3664eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: TID %d: incomp=%d\n",
3665eb6f0de0SAdrian Chadd 	    __func__, tid, atid->incomp);
3666eb6f0de0SAdrian Chadd 
3667eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
3668eb6f0de0SAdrian Chadd 	atid->incomp--;
3669eb6f0de0SAdrian Chadd 	if (atid->incomp == 0) {
3670eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
3671eb6f0de0SAdrian Chadd 		    "%s: TID %d: cleaned up! resume!\n",
3672eb6f0de0SAdrian Chadd 		    __func__, tid);
3673eb6f0de0SAdrian Chadd 		atid->cleanup_inprogress = 0;
3674eb6f0de0SAdrian Chadd 		ath_tx_tid_resume(sc, atid);
3675eb6f0de0SAdrian Chadd 	}
3676eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3677eb6f0de0SAdrian Chadd 
3678eb6f0de0SAdrian Chadd 	ath_tx_default_comp(sc, bf, 0);
3679eb6f0de0SAdrian Chadd }
3680eb6f0de0SAdrian Chadd 
3681eb6f0de0SAdrian Chadd /*
3682eb6f0de0SAdrian Chadd  * Performs transmit side cleanup when TID changes from aggregated to
3683eb6f0de0SAdrian Chadd  * unaggregated.
3684eb6f0de0SAdrian Chadd  *
3685eb6f0de0SAdrian Chadd  * - Discard all retry frames from the s/w queue.
3686eb6f0de0SAdrian Chadd  * - Fix the tx completion function for all buffers in s/w queue.
3687eb6f0de0SAdrian Chadd  * - Count the number of unacked frames, and let transmit completion
3688eb6f0de0SAdrian Chadd  *   handle it later.
3689eb6f0de0SAdrian Chadd  *
3690eb6f0de0SAdrian Chadd  * The caller is responsible for pausing the TID.
3691eb6f0de0SAdrian Chadd  */
3692eb6f0de0SAdrian Chadd static void
36934dfd4507SAdrian Chadd ath_tx_tid_cleanup(struct ath_softc *sc, struct ath_node *an, int tid)
3694eb6f0de0SAdrian Chadd {
3695eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
3696eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
3697eb6f0de0SAdrian Chadd 	struct ath_buf *bf, *bf_next;
3698eb6f0de0SAdrian Chadd 	ath_bufhead bf_cq;
3699eb6f0de0SAdrian Chadd 
3700d3a6425bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
3701eb6f0de0SAdrian Chadd 	    "%s: TID %d: called\n", __func__, tid);
3702eb6f0de0SAdrian Chadd 
3703eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_cq);
3704eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
3705eb6f0de0SAdrian Chadd 
3706eb6f0de0SAdrian Chadd 	/*
3707f1bc738eSAdrian Chadd 	 * Move the filtered frames to the TX queue, before
3708f1bc738eSAdrian Chadd 	 * we run off and discard/process things.
3709f1bc738eSAdrian Chadd 	 */
3710f1bc738eSAdrian Chadd 	/* XXX this is really quite inefficient */
371113aa9ee5SAdrian Chadd 	while ((bf = ATH_TID_FILT_LAST(atid, ath_bufhead_s)) != NULL) {
371213aa9ee5SAdrian Chadd 		ATH_TID_FILT_REMOVE(atid, bf, bf_list);
37133e6cc97fSAdrian Chadd 		ATH_TID_INSERT_HEAD(atid, bf, bf_list);
3714f1bc738eSAdrian Chadd 	}
3715f1bc738eSAdrian Chadd 
3716f1bc738eSAdrian Chadd 	/*
3717eb6f0de0SAdrian Chadd 	 * Update the frames in the software TX queue:
3718eb6f0de0SAdrian Chadd 	 *
3719eb6f0de0SAdrian Chadd 	 * + Discard retry frames in the queue
3720eb6f0de0SAdrian Chadd 	 * + Fix the completion function to be non-aggregate
3721eb6f0de0SAdrian Chadd 	 */
37223e6cc97fSAdrian Chadd 	bf = ATH_TID_FIRST(atid);
3723eb6f0de0SAdrian Chadd 	while (bf) {
3724eb6f0de0SAdrian Chadd 		if (bf->bf_state.bfs_isretried) {
3725eb6f0de0SAdrian Chadd 			bf_next = TAILQ_NEXT(bf, bf_list);
37263e6cc97fSAdrian Chadd 			ATH_TID_REMOVE(atid, bf, bf_list);
3727eb6f0de0SAdrian Chadd 			atid->axq_depth--;
3728eb6f0de0SAdrian Chadd 			if (bf->bf_state.bfs_dobaw) {
3729eb6f0de0SAdrian Chadd 				ath_tx_update_baw(sc, an, atid, bf);
3730eb6f0de0SAdrian Chadd 				if (! bf->bf_state.bfs_addedbaw)
3731eb6f0de0SAdrian Chadd 					device_printf(sc->sc_dev,
3732eb6f0de0SAdrian Chadd 					    "%s: wasn't added: seqno %d\n",
3733d4365d16SAdrian Chadd 					    __func__,
3734d4365d16SAdrian Chadd 					    SEQNO(bf->bf_state.bfs_seqno));
3735eb6f0de0SAdrian Chadd 			}
3736eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_dobaw = 0;
3737eb6f0de0SAdrian Chadd 			/*
3738eb6f0de0SAdrian Chadd 			 * Call the default completion handler with "fail" just
3739eb6f0de0SAdrian Chadd 			 * so upper levels are suitably notified about this.
3740eb6f0de0SAdrian Chadd 			 */
3741eb6f0de0SAdrian Chadd 			TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
3742eb6f0de0SAdrian Chadd 			bf = bf_next;
3743eb6f0de0SAdrian Chadd 			continue;
3744eb6f0de0SAdrian Chadd 		}
3745eb6f0de0SAdrian Chadd 		/* Give these the default completion handler */
3746eb6f0de0SAdrian Chadd 		bf->bf_comp = ath_tx_normal_comp;
3747eb6f0de0SAdrian Chadd 		bf = TAILQ_NEXT(bf, bf_list);
3748eb6f0de0SAdrian Chadd 	}
3749eb6f0de0SAdrian Chadd 
3750eb6f0de0SAdrian Chadd 	/* The caller is required to pause the TID */
3751eb6f0de0SAdrian Chadd #if 0
3752eb6f0de0SAdrian Chadd 	/* Pause the TID */
3753eb6f0de0SAdrian Chadd 	ath_tx_tid_pause(sc, atid);
3754eb6f0de0SAdrian Chadd #endif
3755eb6f0de0SAdrian Chadd 
3756eb6f0de0SAdrian Chadd 	/*
3757eb6f0de0SAdrian Chadd 	 * Calculate what hardware-queued frames exist based
3758eb6f0de0SAdrian Chadd 	 * on the current BAW size. Ie, what frames have been
3759eb6f0de0SAdrian Chadd 	 * added to the TX hardware queue for this TID but
3760eb6f0de0SAdrian Chadd 	 * not yet ACKed.
3761eb6f0de0SAdrian Chadd 	 */
3762eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
3763eb6f0de0SAdrian Chadd 	/* Need the lock - fiddling with BAW */
3764eb6f0de0SAdrian Chadd 	while (atid->baw_head != atid->baw_tail) {
3765eb6f0de0SAdrian Chadd 		if (atid->tx_buf[atid->baw_head]) {
3766eb6f0de0SAdrian Chadd 			atid->incomp++;
3767eb6f0de0SAdrian Chadd 			atid->cleanup_inprogress = 1;
3768eb6f0de0SAdrian Chadd 			atid->tx_buf[atid->baw_head] = NULL;
3769eb6f0de0SAdrian Chadd 		}
3770eb6f0de0SAdrian Chadd 		INCR(atid->baw_head, ATH_TID_MAX_BUFS);
3771eb6f0de0SAdrian Chadd 		INCR(tap->txa_start, IEEE80211_SEQ_RANGE);
3772eb6f0de0SAdrian Chadd 	}
3773eb6f0de0SAdrian Chadd 
3774eb6f0de0SAdrian Chadd 	/*
3775eb6f0de0SAdrian Chadd 	 * If cleanup is required, defer TID scheduling
3776eb6f0de0SAdrian Chadd 	 * until all the HW queued packets have been
3777eb6f0de0SAdrian Chadd 	 * sent.
3778eb6f0de0SAdrian Chadd 	 */
3779eb6f0de0SAdrian Chadd 	if (! atid->cleanup_inprogress)
3780eb6f0de0SAdrian Chadd 		ath_tx_tid_resume(sc, atid);
3781eb6f0de0SAdrian Chadd 
3782eb6f0de0SAdrian Chadd 	if (atid->cleanup_inprogress)
3783eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
3784eb6f0de0SAdrian Chadd 		    "%s: TID %d: cleanup needed: %d packets\n",
3785eb6f0de0SAdrian Chadd 		    __func__, tid, atid->incomp);
3786eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3787eb6f0de0SAdrian Chadd 
3788eb6f0de0SAdrian Chadd 	/* Handle completing frames and fail them */
3789eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
3790eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
3791eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 1);
3792eb6f0de0SAdrian Chadd 	}
3793eb6f0de0SAdrian Chadd }
3794eb6f0de0SAdrian Chadd 
3795eb6f0de0SAdrian Chadd static struct ath_buf *
379638962489SAdrian Chadd ath_tx_retry_clone(struct ath_softc *sc, struct ath_node *an,
379738962489SAdrian Chadd     struct ath_tid *tid, struct ath_buf *bf)
3798eb6f0de0SAdrian Chadd {
3799eb6f0de0SAdrian Chadd 	struct ath_buf *nbf;
3800eb6f0de0SAdrian Chadd 	int error;
3801eb6f0de0SAdrian Chadd 
3802eb6f0de0SAdrian Chadd 	nbf = ath_buf_clone(sc, bf);
3803eb6f0de0SAdrian Chadd 
3804eb6f0de0SAdrian Chadd #if 0
3805eb6f0de0SAdrian Chadd 	device_printf(sc->sc_dev, "%s: ATH_BUF_BUSY; cloning\n",
3806eb6f0de0SAdrian Chadd 	    __func__);
3807eb6f0de0SAdrian Chadd #endif
3808eb6f0de0SAdrian Chadd 
3809eb6f0de0SAdrian Chadd 	if (nbf == NULL) {
3810eb6f0de0SAdrian Chadd 		/* Failed to clone */
3811eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
3812eb6f0de0SAdrian Chadd 		    "%s: failed to clone a busy buffer\n",
3813eb6f0de0SAdrian Chadd 		    __func__);
3814eb6f0de0SAdrian Chadd 		return NULL;
3815eb6f0de0SAdrian Chadd 	}
3816eb6f0de0SAdrian Chadd 
3817eb6f0de0SAdrian Chadd 	/* Setup the dma for the new buffer */
3818eb6f0de0SAdrian Chadd 	error = ath_tx_dmasetup(sc, nbf, nbf->bf_m);
3819eb6f0de0SAdrian Chadd 	if (error != 0) {
3820eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
3821eb6f0de0SAdrian Chadd 		    "%s: failed to setup dma for clone\n",
3822eb6f0de0SAdrian Chadd 		    __func__);
3823eb6f0de0SAdrian Chadd 		/*
3824eb6f0de0SAdrian Chadd 		 * Put this at the head of the list, not tail;
3825eb6f0de0SAdrian Chadd 		 * that way it doesn't interfere with the
3826eb6f0de0SAdrian Chadd 		 * busy buffer logic (which uses the tail of
3827eb6f0de0SAdrian Chadd 		 * the list.)
3828eb6f0de0SAdrian Chadd 		 */
3829eb6f0de0SAdrian Chadd 		ATH_TXBUF_LOCK(sc);
383032c387f7SAdrian Chadd 		ath_returnbuf_head(sc, nbf);
3831eb6f0de0SAdrian Chadd 		ATH_TXBUF_UNLOCK(sc);
3832eb6f0de0SAdrian Chadd 		return NULL;
3833eb6f0de0SAdrian Chadd 	}
3834eb6f0de0SAdrian Chadd 
383538962489SAdrian Chadd 	/* Update BAW if required, before we free the original buf */
383638962489SAdrian Chadd 	if (bf->bf_state.bfs_dobaw)
383738962489SAdrian Chadd 		ath_tx_switch_baw_buf(sc, an, tid, bf, nbf);
383838962489SAdrian Chadd 
3839eb6f0de0SAdrian Chadd 	/* Free current buffer; return the older buffer */
3840eb6f0de0SAdrian Chadd 	bf->bf_m = NULL;
3841eb6f0de0SAdrian Chadd 	bf->bf_node = NULL;
3842eb6f0de0SAdrian Chadd 	ath_freebuf(sc, bf);
3843f1bc738eSAdrian Chadd 
3844eb6f0de0SAdrian Chadd 	return nbf;
3845eb6f0de0SAdrian Chadd }
3846eb6f0de0SAdrian Chadd 
3847eb6f0de0SAdrian Chadd /*
3848eb6f0de0SAdrian Chadd  * Handle retrying an unaggregate frame in an aggregate
3849eb6f0de0SAdrian Chadd  * session.
3850eb6f0de0SAdrian Chadd  *
3851eb6f0de0SAdrian Chadd  * If too many retries occur, pause the TID, wait for
3852eb6f0de0SAdrian Chadd  * any further retransmits (as there's no reason why
3853eb6f0de0SAdrian Chadd  * non-aggregate frames in an aggregate session are
3854eb6f0de0SAdrian Chadd  * transmitted in-order; they just have to be in-BAW)
3855eb6f0de0SAdrian Chadd  * and then queue a BAR.
3856eb6f0de0SAdrian Chadd  */
3857eb6f0de0SAdrian Chadd static void
3858eb6f0de0SAdrian Chadd ath_tx_aggr_retry_unaggr(struct ath_softc *sc, struct ath_buf *bf)
3859eb6f0de0SAdrian Chadd {
3860eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
3861eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
3862eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
3863eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
3864eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
3865eb6f0de0SAdrian Chadd 
3866eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
3867eb6f0de0SAdrian Chadd 
3868eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
3869eb6f0de0SAdrian Chadd 
3870eb6f0de0SAdrian Chadd 	/*
3871eb6f0de0SAdrian Chadd 	 * If the buffer is marked as busy, we can't directly
3872eb6f0de0SAdrian Chadd 	 * reuse it. Instead, try to clone the buffer.
3873eb6f0de0SAdrian Chadd 	 * If the clone is successful, recycle the old buffer.
3874eb6f0de0SAdrian Chadd 	 * If the clone is unsuccessful, set bfs_retries to max
3875eb6f0de0SAdrian Chadd 	 * to force the next bit of code to free the buffer
3876eb6f0de0SAdrian Chadd 	 * for us.
3877eb6f0de0SAdrian Chadd 	 */
3878eb6f0de0SAdrian Chadd 	if ((bf->bf_state.bfs_retries < SWMAX_RETRIES) &&
3879eb6f0de0SAdrian Chadd 	    (bf->bf_flags & ATH_BUF_BUSY)) {
3880eb6f0de0SAdrian Chadd 		struct ath_buf *nbf;
388138962489SAdrian Chadd 		nbf = ath_tx_retry_clone(sc, an, atid, bf);
3882eb6f0de0SAdrian Chadd 		if (nbf)
3883eb6f0de0SAdrian Chadd 			/* bf has been freed at this point */
3884eb6f0de0SAdrian Chadd 			bf = nbf;
3885eb6f0de0SAdrian Chadd 		else
3886eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_retries = SWMAX_RETRIES + 1;
3887eb6f0de0SAdrian Chadd 	}
3888eb6f0de0SAdrian Chadd 
3889eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_retries >= SWMAX_RETRIES) {
3890eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_RETRIES,
3891eb6f0de0SAdrian Chadd 		    "%s: exceeded retries; seqno %d\n",
3892eb6f0de0SAdrian Chadd 		    __func__, SEQNO(bf->bf_state.bfs_seqno));
3893eb6f0de0SAdrian Chadd 		sc->sc_stats.ast_tx_swretrymax++;
3894eb6f0de0SAdrian Chadd 
3895eb6f0de0SAdrian Chadd 		/* Update BAW anyway */
3896eb6f0de0SAdrian Chadd 		if (bf->bf_state.bfs_dobaw) {
3897eb6f0de0SAdrian Chadd 			ath_tx_update_baw(sc, an, atid, bf);
3898eb6f0de0SAdrian Chadd 			if (! bf->bf_state.bfs_addedbaw)
3899eb6f0de0SAdrian Chadd 				device_printf(sc->sc_dev,
3900eb6f0de0SAdrian Chadd 				    "%s: wasn't added: seqno %d\n",
3901eb6f0de0SAdrian Chadd 				    __func__, SEQNO(bf->bf_state.bfs_seqno));
3902eb6f0de0SAdrian Chadd 		}
3903eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_dobaw = 0;
3904eb6f0de0SAdrian Chadd 
390588b3d483SAdrian Chadd 		/* Suspend the TX queue and get ready to send the BAR */
390688b3d483SAdrian Chadd 		ath_tx_tid_bar_suspend(sc, atid);
390788b3d483SAdrian Chadd 
390888b3d483SAdrian Chadd 		/* Send the BAR if there are no other frames waiting */
390988b3d483SAdrian Chadd 		if (ath_tx_tid_bar_tx_ready(sc, atid))
391088b3d483SAdrian Chadd 			ath_tx_tid_bar_tx(sc, atid);
391188b3d483SAdrian Chadd 
3912eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3913eb6f0de0SAdrian Chadd 
3914eb6f0de0SAdrian Chadd 		/* Free buffer, bf is free after this call */
3915eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
3916eb6f0de0SAdrian Chadd 		return;
3917eb6f0de0SAdrian Chadd 	}
3918eb6f0de0SAdrian Chadd 
3919eb6f0de0SAdrian Chadd 	/*
3920eb6f0de0SAdrian Chadd 	 * This increments the retry counter as well as
3921eb6f0de0SAdrian Chadd 	 * sets the retry flag in the ath_buf and packet
3922eb6f0de0SAdrian Chadd 	 * body.
3923eb6f0de0SAdrian Chadd 	 */
3924eb6f0de0SAdrian Chadd 	ath_tx_set_retry(sc, bf);
3925f1bc738eSAdrian Chadd 	sc->sc_stats.ast_tx_swretries++;
3926eb6f0de0SAdrian Chadd 
3927eb6f0de0SAdrian Chadd 	/*
3928eb6f0de0SAdrian Chadd 	 * Insert this at the head of the queue, so it's
3929eb6f0de0SAdrian Chadd 	 * retried before any current/subsequent frames.
3930eb6f0de0SAdrian Chadd 	 */
39313e6cc97fSAdrian Chadd 	ATH_TID_INSERT_HEAD(atid, bf, bf_list);
3932eb6f0de0SAdrian Chadd 	ath_tx_tid_sched(sc, atid);
393388b3d483SAdrian Chadd 	/* Send the BAR if there are no other frames waiting */
393488b3d483SAdrian Chadd 	if (ath_tx_tid_bar_tx_ready(sc, atid))
393588b3d483SAdrian Chadd 		ath_tx_tid_bar_tx(sc, atid);
3936eb6f0de0SAdrian Chadd 
3937eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3938eb6f0de0SAdrian Chadd }
3939eb6f0de0SAdrian Chadd 
3940eb6f0de0SAdrian Chadd /*
3941eb6f0de0SAdrian Chadd  * Common code for aggregate excessive retry/subframe retry.
3942eb6f0de0SAdrian Chadd  * If retrying, queues buffers to bf_q. If not, frees the
3943eb6f0de0SAdrian Chadd  * buffers.
3944eb6f0de0SAdrian Chadd  *
3945eb6f0de0SAdrian Chadd  * XXX should unify this with ath_tx_aggr_retry_unaggr()
3946eb6f0de0SAdrian Chadd  */
3947eb6f0de0SAdrian Chadd static int
3948eb6f0de0SAdrian Chadd ath_tx_retry_subframe(struct ath_softc *sc, struct ath_buf *bf,
3949eb6f0de0SAdrian Chadd     ath_bufhead *bf_q)
3950eb6f0de0SAdrian Chadd {
3951eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
3952eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
3953eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
3954eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
3955eb6f0de0SAdrian Chadd 
3956eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[atid->ac]);
3957eb6f0de0SAdrian Chadd 
395821840808SAdrian Chadd 	/* XXX clr11naggr should be done for all subframes */
3959eb6f0de0SAdrian Chadd 	ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
3960eb6f0de0SAdrian Chadd 	ath_hal_set11nburstduration(sc->sc_ah, bf->bf_desc, 0);
3961f1bc738eSAdrian Chadd 
3962eb6f0de0SAdrian Chadd 	/* ath_hal_set11n_virtualmorefrag(sc->sc_ah, bf->bf_desc, 0); */
3963eb6f0de0SAdrian Chadd 
3964eb6f0de0SAdrian Chadd 	/*
3965eb6f0de0SAdrian Chadd 	 * If the buffer is marked as busy, we can't directly
3966eb6f0de0SAdrian Chadd 	 * reuse it. Instead, try to clone the buffer.
3967eb6f0de0SAdrian Chadd 	 * If the clone is successful, recycle the old buffer.
3968eb6f0de0SAdrian Chadd 	 * If the clone is unsuccessful, set bfs_retries to max
3969eb6f0de0SAdrian Chadd 	 * to force the next bit of code to free the buffer
3970eb6f0de0SAdrian Chadd 	 * for us.
3971eb6f0de0SAdrian Chadd 	 */
3972eb6f0de0SAdrian Chadd 	if ((bf->bf_state.bfs_retries < SWMAX_RETRIES) &&
3973eb6f0de0SAdrian Chadd 	    (bf->bf_flags & ATH_BUF_BUSY)) {
3974eb6f0de0SAdrian Chadd 		struct ath_buf *nbf;
397538962489SAdrian Chadd 		nbf = ath_tx_retry_clone(sc, an, atid, bf);
3976eb6f0de0SAdrian Chadd 		if (nbf)
3977eb6f0de0SAdrian Chadd 			/* bf has been freed at this point */
3978eb6f0de0SAdrian Chadd 			bf = nbf;
3979eb6f0de0SAdrian Chadd 		else
3980eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_retries = SWMAX_RETRIES + 1;
3981eb6f0de0SAdrian Chadd 	}
3982eb6f0de0SAdrian Chadd 
3983eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_retries >= SWMAX_RETRIES) {
3984eb6f0de0SAdrian Chadd 		sc->sc_stats.ast_tx_swretrymax++;
3985eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_RETRIES,
3986eb6f0de0SAdrian Chadd 		    "%s: max retries: seqno %d\n",
3987eb6f0de0SAdrian Chadd 		    __func__, SEQNO(bf->bf_state.bfs_seqno));
3988eb6f0de0SAdrian Chadd 		ath_tx_update_baw(sc, an, atid, bf);
3989eb6f0de0SAdrian Chadd 		if (! bf->bf_state.bfs_addedbaw)
3990eb6f0de0SAdrian Chadd 			device_printf(sc->sc_dev,
3991eb6f0de0SAdrian Chadd 			    "%s: wasn't added: seqno %d\n",
3992eb6f0de0SAdrian Chadd 			    __func__, SEQNO(bf->bf_state.bfs_seqno));
3993eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_dobaw = 0;
3994eb6f0de0SAdrian Chadd 		return 1;
3995eb6f0de0SAdrian Chadd 	}
3996eb6f0de0SAdrian Chadd 
3997eb6f0de0SAdrian Chadd 	ath_tx_set_retry(sc, bf);
3998f1bc738eSAdrian Chadd 	sc->sc_stats.ast_tx_swretries++;
3999eb6f0de0SAdrian Chadd 	bf->bf_next = NULL;		/* Just to make sure */
4000eb6f0de0SAdrian Chadd 
400121840808SAdrian Chadd 	/* Clear the aggregate state */
400221840808SAdrian Chadd 	bf->bf_state.bfs_aggr = 0;
400321840808SAdrian Chadd 	bf->bf_state.bfs_ndelim = 0;	/* ??? needed? */
400421840808SAdrian Chadd 	bf->bf_state.bfs_nframes = 1;
400521840808SAdrian Chadd 
4006eb6f0de0SAdrian Chadd 	TAILQ_INSERT_TAIL(bf_q, bf, bf_list);
4007eb6f0de0SAdrian Chadd 	return 0;
4008eb6f0de0SAdrian Chadd }
4009eb6f0de0SAdrian Chadd 
4010eb6f0de0SAdrian Chadd /*
4011eb6f0de0SAdrian Chadd  * error pkt completion for an aggregate destination
4012eb6f0de0SAdrian Chadd  */
4013eb6f0de0SAdrian Chadd static void
4014eb6f0de0SAdrian Chadd ath_tx_comp_aggr_error(struct ath_softc *sc, struct ath_buf *bf_first,
4015eb6f0de0SAdrian Chadd     struct ath_tid *tid)
4016eb6f0de0SAdrian Chadd {
4017eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf_first->bf_node;
4018eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4019eb6f0de0SAdrian Chadd 	struct ath_buf *bf_next, *bf;
4020eb6f0de0SAdrian Chadd 	ath_bufhead bf_q;
4021eb6f0de0SAdrian Chadd 	int drops = 0;
4022eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
4023eb6f0de0SAdrian Chadd 	ath_bufhead bf_cq;
4024eb6f0de0SAdrian Chadd 
4025eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_q);
4026eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_cq);
4027eb6f0de0SAdrian Chadd 
4028eb6f0de0SAdrian Chadd 	/*
4029eb6f0de0SAdrian Chadd 	 * Update rate control - all frames have failed.
4030eb6f0de0SAdrian Chadd 	 *
4031eb6f0de0SAdrian Chadd 	 * XXX use the length in the first frame in the series;
4032eb6f0de0SAdrian Chadd 	 * XXX just so things are consistent for now.
4033eb6f0de0SAdrian Chadd 	 */
4034eb6f0de0SAdrian Chadd 	ath_tx_update_ratectrl(sc, ni, bf_first->bf_state.bfs_rc,
4035eb6f0de0SAdrian Chadd 	    &bf_first->bf_status.ds_txstat,
4036eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_pktlen,
4037eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_nframes, bf_first->bf_state.bfs_nframes);
4038eb6f0de0SAdrian Chadd 
4039eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[tid->ac]);
4040eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
40412d3d4776SAdrian Chadd 	sc->sc_stats.ast_tx_aggr_failall++;
4042eb6f0de0SAdrian Chadd 
4043eb6f0de0SAdrian Chadd 	/* Retry all subframes */
4044eb6f0de0SAdrian Chadd 	bf = bf_first;
4045eb6f0de0SAdrian Chadd 	while (bf) {
4046eb6f0de0SAdrian Chadd 		bf_next = bf->bf_next;
4047eb6f0de0SAdrian Chadd 		bf->bf_next = NULL;	/* Remove it from the aggr list */
40482d3d4776SAdrian Chadd 		sc->sc_stats.ast_tx_aggr_fail++;
4049eb6f0de0SAdrian Chadd 		if (ath_tx_retry_subframe(sc, bf, &bf_q)) {
4050eb6f0de0SAdrian Chadd 			drops++;
4051eb6f0de0SAdrian Chadd 			bf->bf_next = NULL;
4052eb6f0de0SAdrian Chadd 			TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
4053eb6f0de0SAdrian Chadd 		}
4054eb6f0de0SAdrian Chadd 		bf = bf_next;
4055eb6f0de0SAdrian Chadd 	}
4056eb6f0de0SAdrian Chadd 
4057eb6f0de0SAdrian Chadd 	/* Prepend all frames to the beginning of the queue */
4058eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_LAST(&bf_q, ath_bufhead_s)) != NULL) {
4059eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_q, bf, bf_list);
40603e6cc97fSAdrian Chadd 		ATH_TID_INSERT_HEAD(tid, bf, bf_list);
4061eb6f0de0SAdrian Chadd 	}
4062eb6f0de0SAdrian Chadd 
406339da9d42SAdrian Chadd 	/*
406439da9d42SAdrian Chadd 	 * Schedule the TID to be re-tried.
406539da9d42SAdrian Chadd 	 */
4066eb6f0de0SAdrian Chadd 	ath_tx_tid_sched(sc, tid);
4067eb6f0de0SAdrian Chadd 
4068eb6f0de0SAdrian Chadd 	/*
4069eb6f0de0SAdrian Chadd 	 * send bar if we dropped any frames
4070eb6f0de0SAdrian Chadd 	 *
4071eb6f0de0SAdrian Chadd 	 * Keep the txq lock held for now, as we need to ensure
4072eb6f0de0SAdrian Chadd 	 * that ni_txseqs[] is consistent (as it's being updated
4073eb6f0de0SAdrian Chadd 	 * in the ifnet TX context or raw TX context.)
4074eb6f0de0SAdrian Chadd 	 */
4075eb6f0de0SAdrian Chadd 	if (drops) {
407688b3d483SAdrian Chadd 		/* Suspend the TX queue and get ready to send the BAR */
407788b3d483SAdrian Chadd 		ath_tx_tid_bar_suspend(sc, tid);
4078eb6f0de0SAdrian Chadd 	}
4079eb6f0de0SAdrian Chadd 
408088b3d483SAdrian Chadd 	/*
408188b3d483SAdrian Chadd 	 * Send BAR if required
408288b3d483SAdrian Chadd 	 */
408388b3d483SAdrian Chadd 	if (ath_tx_tid_bar_tx_ready(sc, tid))
408488b3d483SAdrian Chadd 		ath_tx_tid_bar_tx(sc, tid);
4085f1bc738eSAdrian Chadd 
408688b3d483SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[tid->ac]);
408788b3d483SAdrian Chadd 
4088eb6f0de0SAdrian Chadd 	/* Complete frames which errored out */
4089eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
4090eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
4091eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
4092eb6f0de0SAdrian Chadd 	}
4093eb6f0de0SAdrian Chadd }
4094eb6f0de0SAdrian Chadd 
4095eb6f0de0SAdrian Chadd /*
4096eb6f0de0SAdrian Chadd  * Handle clean-up of packets from an aggregate list.
4097eb6f0de0SAdrian Chadd  *
4098eb6f0de0SAdrian Chadd  * There's no need to update the BAW here - the session is being
4099eb6f0de0SAdrian Chadd  * torn down.
4100eb6f0de0SAdrian Chadd  */
4101eb6f0de0SAdrian Chadd static void
4102eb6f0de0SAdrian Chadd ath_tx_comp_cleanup_aggr(struct ath_softc *sc, struct ath_buf *bf_first)
4103eb6f0de0SAdrian Chadd {
4104eb6f0de0SAdrian Chadd 	struct ath_buf *bf, *bf_next;
4105eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf_first->bf_node;
4106eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4107eb6f0de0SAdrian Chadd 	int tid = bf_first->bf_state.bfs_tid;
4108eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4109eb6f0de0SAdrian Chadd 
4110eb6f0de0SAdrian Chadd 	bf = bf_first;
4111eb6f0de0SAdrian Chadd 
4112eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
4113eb6f0de0SAdrian Chadd 
4114eb6f0de0SAdrian Chadd 	/* update incomp */
4115eb6f0de0SAdrian Chadd 	while (bf) {
4116eb6f0de0SAdrian Chadd 		atid->incomp--;
4117eb6f0de0SAdrian Chadd 		bf = bf->bf_next;
4118eb6f0de0SAdrian Chadd 	}
4119eb6f0de0SAdrian Chadd 
4120eb6f0de0SAdrian Chadd 	if (atid->incomp == 0) {
4121eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4122eb6f0de0SAdrian Chadd 		    "%s: TID %d: cleaned up! resume!\n",
4123eb6f0de0SAdrian Chadd 		    __func__, tid);
4124eb6f0de0SAdrian Chadd 		atid->cleanup_inprogress = 0;
4125eb6f0de0SAdrian Chadd 		ath_tx_tid_resume(sc, atid);
4126eb6f0de0SAdrian Chadd 	}
412788b3d483SAdrian Chadd 
412888b3d483SAdrian Chadd 	/* Send BAR if required */
4129f1bc738eSAdrian Chadd 	/* XXX why would we send a BAR when transitioning to non-aggregation? */
413088b3d483SAdrian Chadd 	if (ath_tx_tid_bar_tx_ready(sc, atid))
413188b3d483SAdrian Chadd 		ath_tx_tid_bar_tx(sc, atid);
4132f1bc738eSAdrian Chadd 
4133eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4134eb6f0de0SAdrian Chadd 
4135eb6f0de0SAdrian Chadd 	/* Handle frame completion */
4136eb6f0de0SAdrian Chadd 	while (bf) {
4137eb6f0de0SAdrian Chadd 		bf_next = bf->bf_next;
4138eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 1);
4139eb6f0de0SAdrian Chadd 		bf = bf_next;
4140eb6f0de0SAdrian Chadd 	}
4141eb6f0de0SAdrian Chadd }
4142eb6f0de0SAdrian Chadd 
4143eb6f0de0SAdrian Chadd /*
4144eb6f0de0SAdrian Chadd  * Handle completion of an set of aggregate frames.
4145eb6f0de0SAdrian Chadd  *
4146eb6f0de0SAdrian Chadd  * XXX for now, simply complete each sub-frame.
4147eb6f0de0SAdrian Chadd  *
4148eb6f0de0SAdrian Chadd  * Note: the completion handler is the last descriptor in the aggregate,
4149eb6f0de0SAdrian Chadd  * not the last descriptor in the first frame.
4150eb6f0de0SAdrian Chadd  */
4151eb6f0de0SAdrian Chadd static void
4152d4365d16SAdrian Chadd ath_tx_aggr_comp_aggr(struct ath_softc *sc, struct ath_buf *bf_first,
4153d4365d16SAdrian Chadd     int fail)
4154eb6f0de0SAdrian Chadd {
4155eb6f0de0SAdrian Chadd 	//struct ath_desc *ds = bf->bf_lastds;
4156eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf_first->bf_node;
4157eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4158eb6f0de0SAdrian Chadd 	int tid = bf_first->bf_state.bfs_tid;
4159eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4160eb6f0de0SAdrian Chadd 	struct ath_tx_status ts;
4161eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
4162eb6f0de0SAdrian Chadd 	ath_bufhead bf_q;
4163eb6f0de0SAdrian Chadd 	ath_bufhead bf_cq;
4164eb6f0de0SAdrian Chadd 	int seq_st, tx_ok;
4165eb6f0de0SAdrian Chadd 	int hasba, isaggr;
4166eb6f0de0SAdrian Chadd 	uint32_t ba[2];
4167eb6f0de0SAdrian Chadd 	struct ath_buf *bf, *bf_next;
4168eb6f0de0SAdrian Chadd 	int ba_index;
4169eb6f0de0SAdrian Chadd 	int drops = 0;
4170eb6f0de0SAdrian Chadd 	int nframes = 0, nbad = 0, nf;
4171eb6f0de0SAdrian Chadd 	int pktlen;
4172eb6f0de0SAdrian Chadd 	/* XXX there's too much on the stack? */
4173b815538dSAdrian Chadd 	struct ath_rc_series rc[ATH_RC_NUM];
4174eb6f0de0SAdrian Chadd 	int txseq;
4175eb6f0de0SAdrian Chadd 
4176eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: called; hwq_depth=%d\n",
4177eb6f0de0SAdrian Chadd 	    __func__, atid->hwq_depth);
4178eb6f0de0SAdrian Chadd 
41790aa5c1bbSAdrian Chadd 	/*
41800aa5c1bbSAdrian Chadd 	 * Take a copy; this may be needed -after- bf_first
41810aa5c1bbSAdrian Chadd 	 * has been completed and freed.
41820aa5c1bbSAdrian Chadd 	 */
41830aa5c1bbSAdrian Chadd 	ts = bf_first->bf_status.ds_txstat;
41840aa5c1bbSAdrian Chadd 
4185f1bc738eSAdrian Chadd 	TAILQ_INIT(&bf_q);
4186f1bc738eSAdrian Chadd 	TAILQ_INIT(&bf_cq);
4187f1bc738eSAdrian Chadd 
4188eb6f0de0SAdrian Chadd 	/* The TID state is kept behind the TXQ lock */
4189eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
4190eb6f0de0SAdrian Chadd 
4191eb6f0de0SAdrian Chadd 	atid->hwq_depth--;
4192eb6f0de0SAdrian Chadd 	if (atid->hwq_depth < 0)
4193eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: hwq_depth < 0: %d\n",
4194eb6f0de0SAdrian Chadd 		    __func__, atid->hwq_depth);
4195eb6f0de0SAdrian Chadd 
4196eb6f0de0SAdrian Chadd 	/*
4197f1bc738eSAdrian Chadd 	 * If the TID is filtered, handle completing the filter
4198f1bc738eSAdrian Chadd 	 * transition before potentially kicking it to the cleanup
4199f1bc738eSAdrian Chadd 	 * function.
42000aa5c1bbSAdrian Chadd 	 *
42010aa5c1bbSAdrian Chadd 	 * XXX this is duplicate work, ew.
4202f1bc738eSAdrian Chadd 	 */
4203f1bc738eSAdrian Chadd 	if (atid->isfiltered)
4204f1bc738eSAdrian Chadd 		ath_tx_tid_filt_comp_complete(sc, atid);
4205f1bc738eSAdrian Chadd 
4206f1bc738eSAdrian Chadd 	/*
4207eb6f0de0SAdrian Chadd 	 * Punt cleanup to the relevant function, not our problem now
4208eb6f0de0SAdrian Chadd 	 */
4209eb6f0de0SAdrian Chadd 	if (atid->cleanup_inprogress) {
4210f1bc738eSAdrian Chadd 		if (atid->isfiltered)
4211f1bc738eSAdrian Chadd 			device_printf(sc->sc_dev,
4212f1bc738eSAdrian Chadd 			    "%s: isfiltered=1, normal_comp?\n",
4213f1bc738eSAdrian Chadd 			    __func__);
4214eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4215eb6f0de0SAdrian Chadd 		ath_tx_comp_cleanup_aggr(sc, bf_first);
4216eb6f0de0SAdrian Chadd 		return;
4217eb6f0de0SAdrian Chadd 	}
4218eb6f0de0SAdrian Chadd 
4219eb6f0de0SAdrian Chadd 	/*
4220f1bc738eSAdrian Chadd 	 * If the frame is filtered, transition to filtered frame
4221f1bc738eSAdrian Chadd 	 * mode and add this to the filtered frame list.
4222f1bc738eSAdrian Chadd 	 *
4223f1bc738eSAdrian Chadd 	 * XXX TODO: figure out how this interoperates with
4224f1bc738eSAdrian Chadd 	 * BAR, pause and cleanup states.
4225f1bc738eSAdrian Chadd 	 */
4226f1bc738eSAdrian Chadd 	if ((ts.ts_status & HAL_TXERR_FILT) ||
4227f1bc738eSAdrian Chadd 	    (ts.ts_status != 0 && atid->isfiltered)) {
4228f1bc738eSAdrian Chadd 		if (fail != 0)
4229f1bc738eSAdrian Chadd 			device_printf(sc->sc_dev,
4230f1bc738eSAdrian Chadd 			    "%s: isfiltered=1, fail=%d\n", __func__, fail);
4231f1bc738eSAdrian Chadd 		ath_tx_tid_filt_comp_aggr(sc, atid, bf_first, &bf_cq);
4232f1bc738eSAdrian Chadd 
4233f1bc738eSAdrian Chadd 		/* Remove from BAW */
4234f1bc738eSAdrian Chadd 		TAILQ_FOREACH_SAFE(bf, &bf_cq, bf_list, bf_next) {
4235f1bc738eSAdrian Chadd 			if (bf->bf_state.bfs_addedbaw)
4236f1bc738eSAdrian Chadd 				drops++;
4237f1bc738eSAdrian Chadd 			if (bf->bf_state.bfs_dobaw) {
4238f1bc738eSAdrian Chadd 				ath_tx_update_baw(sc, an, atid, bf);
4239f1bc738eSAdrian Chadd 				if (! bf->bf_state.bfs_addedbaw)
4240f1bc738eSAdrian Chadd 					device_printf(sc->sc_dev,
4241f1bc738eSAdrian Chadd 					    "%s: wasn't added: seqno %d\n",
4242f1bc738eSAdrian Chadd 					    __func__,
4243f1bc738eSAdrian Chadd 					    SEQNO(bf->bf_state.bfs_seqno));
4244f1bc738eSAdrian Chadd 			}
4245f1bc738eSAdrian Chadd 			bf->bf_state.bfs_dobaw = 0;
4246f1bc738eSAdrian Chadd 		}
4247f1bc738eSAdrian Chadd 		/*
4248f1bc738eSAdrian Chadd 		 * If any intermediate frames in the BAW were dropped when
4249f1bc738eSAdrian Chadd 		 * handling filtering things, send a BAR.
4250f1bc738eSAdrian Chadd 		 */
4251f1bc738eSAdrian Chadd 		if (drops)
4252f1bc738eSAdrian Chadd 			ath_tx_tid_bar_suspend(sc, atid);
4253f1bc738eSAdrian Chadd 
4254f1bc738eSAdrian Chadd 		/*
4255f1bc738eSAdrian Chadd 		 * Finish up by sending a BAR if required and freeing
4256f1bc738eSAdrian Chadd 		 * the frames outside of the TX lock.
4257f1bc738eSAdrian Chadd 		 */
4258f1bc738eSAdrian Chadd 		goto finish_send_bar;
4259f1bc738eSAdrian Chadd 	}
4260f1bc738eSAdrian Chadd 
4261f1bc738eSAdrian Chadd 	/*
4262eb6f0de0SAdrian Chadd 	 * XXX for now, use the first frame in the aggregate for
4263eb6f0de0SAdrian Chadd 	 * XXX rate control completion; it's at least consistent.
4264eb6f0de0SAdrian Chadd 	 */
4265eb6f0de0SAdrian Chadd 	pktlen = bf_first->bf_state.bfs_pktlen;
4266eb6f0de0SAdrian Chadd 
4267eb6f0de0SAdrian Chadd 	/*
4268e9a6408eSAdrian Chadd 	 * Handle errors first!
4269e9a6408eSAdrian Chadd 	 *
4270e9a6408eSAdrian Chadd 	 * Here, handle _any_ error as a "exceeded retries" error.
4271e9a6408eSAdrian Chadd 	 * Later on (when filtered frames are to be specially handled)
4272e9a6408eSAdrian Chadd 	 * it'll have to be expanded.
4273eb6f0de0SAdrian Chadd 	 */
4274e9a6408eSAdrian Chadd #if 0
4275eb6f0de0SAdrian Chadd 	if (ts.ts_status & HAL_TXERR_XRETRY) {
4276e9a6408eSAdrian Chadd #endif
4277e9a6408eSAdrian Chadd 	if (ts.ts_status != 0) {
4278eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4279eb6f0de0SAdrian Chadd 		ath_tx_comp_aggr_error(sc, bf_first, atid);
4280eb6f0de0SAdrian Chadd 		return;
4281eb6f0de0SAdrian Chadd 	}
4282eb6f0de0SAdrian Chadd 
4283eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
4284eb6f0de0SAdrian Chadd 
4285eb6f0de0SAdrian Chadd 	/*
4286eb6f0de0SAdrian Chadd 	 * extract starting sequence and block-ack bitmap
4287eb6f0de0SAdrian Chadd 	 */
4288eb6f0de0SAdrian Chadd 	/* XXX endian-ness of seq_st, ba? */
4289eb6f0de0SAdrian Chadd 	seq_st = ts.ts_seqnum;
4290eb6f0de0SAdrian Chadd 	hasba = !! (ts.ts_flags & HAL_TX_BA);
4291eb6f0de0SAdrian Chadd 	tx_ok = (ts.ts_status == 0);
4292eb6f0de0SAdrian Chadd 	isaggr = bf_first->bf_state.bfs_aggr;
4293eb6f0de0SAdrian Chadd 	ba[0] = ts.ts_ba_low;
4294eb6f0de0SAdrian Chadd 	ba[1] = ts.ts_ba_high;
4295eb6f0de0SAdrian Chadd 
4296eb6f0de0SAdrian Chadd 	/*
4297eb6f0de0SAdrian Chadd 	 * Copy the TX completion status and the rate control
4298eb6f0de0SAdrian Chadd 	 * series from the first descriptor, as it may be freed
4299eb6f0de0SAdrian Chadd 	 * before the rate control code can get its grubby fingers
4300eb6f0de0SAdrian Chadd 	 * into things.
4301eb6f0de0SAdrian Chadd 	 */
4302eb6f0de0SAdrian Chadd 	memcpy(rc, bf_first->bf_state.bfs_rc, sizeof(rc));
4303eb6f0de0SAdrian Chadd 
4304eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4305d4365d16SAdrian Chadd 	    "%s: txa_start=%d, tx_ok=%d, status=%.8x, flags=%.8x, "
4306d4365d16SAdrian Chadd 	    "isaggr=%d, seq_st=%d, hasba=%d, ba=%.8x, %.8x\n",
4307eb6f0de0SAdrian Chadd 	    __func__, tap->txa_start, tx_ok, ts.ts_status, ts.ts_flags,
4308eb6f0de0SAdrian Chadd 	    isaggr, seq_st, hasba, ba[0], ba[1]);
4309eb6f0de0SAdrian Chadd 
4310eb6f0de0SAdrian Chadd 	/* Occasionally, the MAC sends a tx status for the wrong TID. */
4311eb6f0de0SAdrian Chadd 	if (tid != ts.ts_tid) {
4312eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: tid %d != hw tid %d\n",
4313eb6f0de0SAdrian Chadd 		    __func__, tid, ts.ts_tid);
4314eb6f0de0SAdrian Chadd 		tx_ok = 0;
4315eb6f0de0SAdrian Chadd 	}
4316eb6f0de0SAdrian Chadd 
4317eb6f0de0SAdrian Chadd 	/* AR5416 BA bug; this requires an interface reset */
4318eb6f0de0SAdrian Chadd 	if (isaggr && tx_ok && (! hasba)) {
4319eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
4320d4365d16SAdrian Chadd 		    "%s: AR5416 bug: hasba=%d; txok=%d, isaggr=%d, "
4321d4365d16SAdrian Chadd 		    "seq_st=%d\n",
4322eb6f0de0SAdrian Chadd 		    __func__, hasba, tx_ok, isaggr, seq_st);
4323eb6f0de0SAdrian Chadd 		/* XXX TODO: schedule an interface reset */
43240f078d63SJohn Baldwin #ifdef ATH_DEBUG
43256abbbae5SAdrian Chadd 		ath_printtxbuf(sc, bf_first,
43266abbbae5SAdrian Chadd 		    sc->sc_ac2q[atid->ac]->axq_qnum, 0, 0);
43270f078d63SJohn Baldwin #endif
4328eb6f0de0SAdrian Chadd 	}
4329eb6f0de0SAdrian Chadd 
4330eb6f0de0SAdrian Chadd 	/*
4331eb6f0de0SAdrian Chadd 	 * Walk the list of frames, figure out which ones were correctly
4332eb6f0de0SAdrian Chadd 	 * sent and which weren't.
4333eb6f0de0SAdrian Chadd 	 */
4334eb6f0de0SAdrian Chadd 	bf = bf_first;
4335eb6f0de0SAdrian Chadd 	nf = bf_first->bf_state.bfs_nframes;
4336eb6f0de0SAdrian Chadd 
4337eb6f0de0SAdrian Chadd 	/* bf_first is going to be invalid once this list is walked */
4338eb6f0de0SAdrian Chadd 	bf_first = NULL;
4339eb6f0de0SAdrian Chadd 
4340eb6f0de0SAdrian Chadd 	/*
4341eb6f0de0SAdrian Chadd 	 * Walk the list of completed frames and determine
4342eb6f0de0SAdrian Chadd 	 * which need to be completed and which need to be
4343eb6f0de0SAdrian Chadd 	 * retransmitted.
4344eb6f0de0SAdrian Chadd 	 *
4345eb6f0de0SAdrian Chadd 	 * For completed frames, the completion functions need
4346eb6f0de0SAdrian Chadd 	 * to be called at the end of this function as the last
4347eb6f0de0SAdrian Chadd 	 * node reference may free the node.
4348eb6f0de0SAdrian Chadd 	 *
4349eb6f0de0SAdrian Chadd 	 * Finally, since the TXQ lock can't be held during the
4350eb6f0de0SAdrian Chadd 	 * completion callback (to avoid lock recursion),
4351eb6f0de0SAdrian Chadd 	 * the completion calls have to be done outside of the
4352eb6f0de0SAdrian Chadd 	 * lock.
4353eb6f0de0SAdrian Chadd 	 */
4354eb6f0de0SAdrian Chadd 	while (bf) {
4355eb6f0de0SAdrian Chadd 		nframes++;
4356d4365d16SAdrian Chadd 		ba_index = ATH_BA_INDEX(seq_st,
4357d4365d16SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
4358eb6f0de0SAdrian Chadd 		bf_next = bf->bf_next;
4359eb6f0de0SAdrian Chadd 		bf->bf_next = NULL;	/* Remove it from the aggr list */
4360eb6f0de0SAdrian Chadd 
4361eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4362eb6f0de0SAdrian Chadd 		    "%s: checking bf=%p seqno=%d; ack=%d\n",
4363eb6f0de0SAdrian Chadd 		    __func__, bf, SEQNO(bf->bf_state.bfs_seqno),
4364eb6f0de0SAdrian Chadd 		    ATH_BA_ISSET(ba, ba_index));
4365eb6f0de0SAdrian Chadd 
4366eb6f0de0SAdrian Chadd 		if (tx_ok && ATH_BA_ISSET(ba, ba_index)) {
43672d3d4776SAdrian Chadd 			sc->sc_stats.ast_tx_aggr_ok++;
4368eb6f0de0SAdrian Chadd 			ath_tx_update_baw(sc, an, atid, bf);
4369eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_dobaw = 0;
4370eb6f0de0SAdrian Chadd 			if (! bf->bf_state.bfs_addedbaw)
4371eb6f0de0SAdrian Chadd 				device_printf(sc->sc_dev,
4372eb6f0de0SAdrian Chadd 				    "%s: wasn't added: seqno %d\n",
4373eb6f0de0SAdrian Chadd 				    __func__, SEQNO(bf->bf_state.bfs_seqno));
4374eb6f0de0SAdrian Chadd 			bf->bf_next = NULL;
4375eb6f0de0SAdrian Chadd 			TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
4376eb6f0de0SAdrian Chadd 		} else {
43772d3d4776SAdrian Chadd 			sc->sc_stats.ast_tx_aggr_fail++;
4378eb6f0de0SAdrian Chadd 			if (ath_tx_retry_subframe(sc, bf, &bf_q)) {
4379eb6f0de0SAdrian Chadd 				drops++;
4380eb6f0de0SAdrian Chadd 				bf->bf_next = NULL;
4381eb6f0de0SAdrian Chadd 				TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
4382eb6f0de0SAdrian Chadd 			}
4383eb6f0de0SAdrian Chadd 			nbad++;
4384eb6f0de0SAdrian Chadd 		}
4385eb6f0de0SAdrian Chadd 		bf = bf_next;
4386eb6f0de0SAdrian Chadd 	}
4387eb6f0de0SAdrian Chadd 
4388eb6f0de0SAdrian Chadd 	/*
4389eb6f0de0SAdrian Chadd 	 * Now that the BAW updates have been done, unlock
4390eb6f0de0SAdrian Chadd 	 *
4391eb6f0de0SAdrian Chadd 	 * txseq is grabbed before the lock is released so we
4392eb6f0de0SAdrian Chadd 	 * have a consistent view of what -was- in the BAW.
4393eb6f0de0SAdrian Chadd 	 * Anything after this point will not yet have been
4394eb6f0de0SAdrian Chadd 	 * TXed.
4395eb6f0de0SAdrian Chadd 	 */
4396eb6f0de0SAdrian Chadd 	txseq = tap->txa_start;
4397eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4398eb6f0de0SAdrian Chadd 
4399eb6f0de0SAdrian Chadd 	if (nframes != nf)
4400eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
4401eb6f0de0SAdrian Chadd 		    "%s: num frames seen=%d; bf nframes=%d\n",
4402eb6f0de0SAdrian Chadd 		    __func__, nframes, nf);
4403eb6f0de0SAdrian Chadd 
4404eb6f0de0SAdrian Chadd 	/*
4405eb6f0de0SAdrian Chadd 	 * Now we know how many frames were bad, call the rate
4406eb6f0de0SAdrian Chadd 	 * control code.
4407eb6f0de0SAdrian Chadd 	 */
4408eb6f0de0SAdrian Chadd 	if (fail == 0)
4409d4365d16SAdrian Chadd 		ath_tx_update_ratectrl(sc, ni, rc, &ts, pktlen, nframes,
4410d4365d16SAdrian Chadd 		    nbad);
4411eb6f0de0SAdrian Chadd 
4412eb6f0de0SAdrian Chadd 	/*
4413eb6f0de0SAdrian Chadd 	 * send bar if we dropped any frames
4414eb6f0de0SAdrian Chadd 	 */
4415eb6f0de0SAdrian Chadd 	if (drops) {
441688b3d483SAdrian Chadd 		/* Suspend the TX queue and get ready to send the BAR */
441788b3d483SAdrian Chadd 		ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
441888b3d483SAdrian Chadd 		ath_tx_tid_bar_suspend(sc, atid);
441988b3d483SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4420eb6f0de0SAdrian Chadd 	}
4421eb6f0de0SAdrian Chadd 
442239da9d42SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
442339da9d42SAdrian Chadd 	    "%s: txa_start now %d\n", __func__, tap->txa_start);
442439da9d42SAdrian Chadd 
4425eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
442639da9d42SAdrian Chadd 
442739da9d42SAdrian Chadd 	/* Prepend all frames to the beginning of the queue */
4428eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_LAST(&bf_q, ath_bufhead_s)) != NULL) {
4429eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_q, bf, bf_list);
44303e6cc97fSAdrian Chadd 		ATH_TID_INSERT_HEAD(atid, bf, bf_list);
4431eb6f0de0SAdrian Chadd 	}
4432eb6f0de0SAdrian Chadd 
443339da9d42SAdrian Chadd 	/*
443439da9d42SAdrian Chadd 	 * Reschedule to grab some further frames.
443539da9d42SAdrian Chadd 	 */
443639da9d42SAdrian Chadd 	ath_tx_tid_sched(sc, atid);
4437eb6f0de0SAdrian Chadd 
443888b3d483SAdrian Chadd 	/*
4439f1bc738eSAdrian Chadd 	 * If the queue is filtered, re-schedule as required.
4440f1bc738eSAdrian Chadd 	 *
4441f1bc738eSAdrian Chadd 	 * This is required as there may be a subsequent TX descriptor
4442f1bc738eSAdrian Chadd 	 * for this end-node that has CLRDMASK set, so it's quite possible
4443f1bc738eSAdrian Chadd 	 * that a filtered frame will be followed by a non-filtered
4444f1bc738eSAdrian Chadd 	 * (complete or otherwise) frame.
4445f1bc738eSAdrian Chadd 	 *
4446f1bc738eSAdrian Chadd 	 * XXX should we do this before we complete the frame?
4447f1bc738eSAdrian Chadd 	 */
4448f1bc738eSAdrian Chadd 	if (atid->isfiltered)
4449f1bc738eSAdrian Chadd 		ath_tx_tid_filt_comp_complete(sc, atid);
4450f1bc738eSAdrian Chadd 
4451f1bc738eSAdrian Chadd finish_send_bar:
4452f1bc738eSAdrian Chadd 
4453f1bc738eSAdrian Chadd 	/*
445488b3d483SAdrian Chadd 	 * Send BAR if required
445588b3d483SAdrian Chadd 	 */
445688b3d483SAdrian Chadd 	if (ath_tx_tid_bar_tx_ready(sc, atid))
445788b3d483SAdrian Chadd 		ath_tx_tid_bar_tx(sc, atid);
445839da9d42SAdrian Chadd 
445988b3d483SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
446088b3d483SAdrian Chadd 
4461eb6f0de0SAdrian Chadd 	/* Do deferred completion */
4462eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
4463eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
4464eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
4465eb6f0de0SAdrian Chadd 	}
4466eb6f0de0SAdrian Chadd }
4467eb6f0de0SAdrian Chadd 
4468eb6f0de0SAdrian Chadd /*
4469eb6f0de0SAdrian Chadd  * Handle completion of unaggregated frames in an ADDBA
4470eb6f0de0SAdrian Chadd  * session.
4471eb6f0de0SAdrian Chadd  *
4472eb6f0de0SAdrian Chadd  * Fail is set to 1 if the entry is being freed via a call to
4473eb6f0de0SAdrian Chadd  * ath_tx_draintxq().
4474eb6f0de0SAdrian Chadd  */
4475eb6f0de0SAdrian Chadd static void
4476eb6f0de0SAdrian Chadd ath_tx_aggr_comp_unaggr(struct ath_softc *sc, struct ath_buf *bf, int fail)
4477eb6f0de0SAdrian Chadd {
4478eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
4479eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4480eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
4481eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
44820aa5c1bbSAdrian Chadd 	struct ath_tx_status ts;
4483f1bc738eSAdrian Chadd 	int drops = 0;
4484eb6f0de0SAdrian Chadd 
4485eb6f0de0SAdrian Chadd 	/*
44860aa5c1bbSAdrian Chadd 	 * Take a copy of this; filtering/cloning the frame may free the
44870aa5c1bbSAdrian Chadd 	 * bf pointer.
44880aa5c1bbSAdrian Chadd 	 */
44890aa5c1bbSAdrian Chadd 	ts = bf->bf_status.ds_txstat;
44900aa5c1bbSAdrian Chadd 
44910aa5c1bbSAdrian Chadd 	/*
4492eb6f0de0SAdrian Chadd 	 * Update rate control status here, before we possibly
4493eb6f0de0SAdrian Chadd 	 * punt to retry or cleanup.
4494eb6f0de0SAdrian Chadd 	 *
4495eb6f0de0SAdrian Chadd 	 * Do it outside of the TXQ lock.
4496eb6f0de0SAdrian Chadd 	 */
4497875a9451SAdrian Chadd 	if (fail == 0 && ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0))
4498eb6f0de0SAdrian Chadd 		ath_tx_update_ratectrl(sc, ni, bf->bf_state.bfs_rc,
4499eb6f0de0SAdrian Chadd 		    &bf->bf_status.ds_txstat,
4500eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_pktlen,
45010aa5c1bbSAdrian Chadd 		    1, (ts.ts_status == 0) ? 0 : 1);
4502eb6f0de0SAdrian Chadd 
4503eb6f0de0SAdrian Chadd 	/*
4504eb6f0de0SAdrian Chadd 	 * This is called early so atid->hwq_depth can be tracked.
4505eb6f0de0SAdrian Chadd 	 * This unfortunately means that it's released and regrabbed
4506eb6f0de0SAdrian Chadd 	 * during retry and cleanup. That's rather inefficient.
4507eb6f0de0SAdrian Chadd 	 */
4508eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
4509eb6f0de0SAdrian Chadd 
4510eb6f0de0SAdrian Chadd 	if (tid == IEEE80211_NONQOS_TID)
4511eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: TID=16!\n", __func__);
4512eb6f0de0SAdrian Chadd 
4513d4365d16SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX,
4514d4365d16SAdrian Chadd 	    "%s: bf=%p: tid=%d, hwq_depth=%d, seqno=%d\n",
4515d4365d16SAdrian Chadd 	    __func__, bf, bf->bf_state.bfs_tid, atid->hwq_depth,
4516d4365d16SAdrian Chadd 	    SEQNO(bf->bf_state.bfs_seqno));
4517eb6f0de0SAdrian Chadd 
4518eb6f0de0SAdrian Chadd 	atid->hwq_depth--;
4519eb6f0de0SAdrian Chadd 	if (atid->hwq_depth < 0)
4520eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: hwq_depth < 0: %d\n",
4521eb6f0de0SAdrian Chadd 		    __func__, atid->hwq_depth);
4522eb6f0de0SAdrian Chadd 
4523eb6f0de0SAdrian Chadd 	/*
4524f1bc738eSAdrian Chadd 	 * If the TID is filtered, handle completing the filter
4525f1bc738eSAdrian Chadd 	 * transition before potentially kicking it to the cleanup
4526f1bc738eSAdrian Chadd 	 * function.
4527f1bc738eSAdrian Chadd 	 */
4528f1bc738eSAdrian Chadd 	if (atid->isfiltered)
4529f1bc738eSAdrian Chadd 		ath_tx_tid_filt_comp_complete(sc, atid);
4530f1bc738eSAdrian Chadd 
4531f1bc738eSAdrian Chadd 	/*
4532eb6f0de0SAdrian Chadd 	 * If a cleanup is in progress, punt to comp_cleanup;
4533eb6f0de0SAdrian Chadd 	 * rather than handling it here. It's thus their
4534eb6f0de0SAdrian Chadd 	 * responsibility to clean up, call the completion
4535eb6f0de0SAdrian Chadd 	 * function in net80211, etc.
4536eb6f0de0SAdrian Chadd 	 */
4537eb6f0de0SAdrian Chadd 	if (atid->cleanup_inprogress) {
4538f1bc738eSAdrian Chadd 		if (atid->isfiltered)
4539f1bc738eSAdrian Chadd 			device_printf(sc->sc_dev,
4540f1bc738eSAdrian Chadd 			    "%s: isfiltered=1, normal_comp?\n",
4541f1bc738eSAdrian Chadd 			    __func__);
4542eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4543d4365d16SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: cleanup_unaggr\n",
4544d4365d16SAdrian Chadd 		    __func__);
4545eb6f0de0SAdrian Chadd 		ath_tx_comp_cleanup_unaggr(sc, bf);
4546eb6f0de0SAdrian Chadd 		return;
4547eb6f0de0SAdrian Chadd 	}
4548eb6f0de0SAdrian Chadd 
4549eb6f0de0SAdrian Chadd 	/*
4550f1bc738eSAdrian Chadd 	 * XXX TODO: how does cleanup, BAR and filtered frame handling
4551f1bc738eSAdrian Chadd 	 * overlap?
4552f1bc738eSAdrian Chadd 	 *
4553f1bc738eSAdrian Chadd 	 * If the frame is filtered OR if it's any failure but
4554f1bc738eSAdrian Chadd 	 * the TID is filtered, the frame must be added to the
4555f1bc738eSAdrian Chadd 	 * filtered frame list.
4556f1bc738eSAdrian Chadd 	 *
4557f1bc738eSAdrian Chadd 	 * However - a busy buffer can't be added to the filtered
4558f1bc738eSAdrian Chadd 	 * list as it will end up being recycled without having
4559f1bc738eSAdrian Chadd 	 * been made available for the hardware.
4560f1bc738eSAdrian Chadd 	 */
45610aa5c1bbSAdrian Chadd 	if ((ts.ts_status & HAL_TXERR_FILT) ||
45620aa5c1bbSAdrian Chadd 	    (ts.ts_status != 0 && atid->isfiltered)) {
4563f1bc738eSAdrian Chadd 		int freeframe;
4564f1bc738eSAdrian Chadd 
4565f1bc738eSAdrian Chadd 		if (fail != 0)
4566f1bc738eSAdrian Chadd 			device_printf(sc->sc_dev,
4567f1bc738eSAdrian Chadd 			    "%s: isfiltered=1, fail=%d\n",
4568f1bc738eSAdrian Chadd 			    __func__,
4569f1bc738eSAdrian Chadd 			    fail);
4570f1bc738eSAdrian Chadd 		freeframe = ath_tx_tid_filt_comp_single(sc, atid, bf);
4571f1bc738eSAdrian Chadd 		if (freeframe) {
4572f1bc738eSAdrian Chadd 			/* Remove from BAW */
4573f1bc738eSAdrian Chadd 			if (bf->bf_state.bfs_addedbaw)
4574f1bc738eSAdrian Chadd 				drops++;
4575f1bc738eSAdrian Chadd 			if (bf->bf_state.bfs_dobaw) {
4576f1bc738eSAdrian Chadd 				ath_tx_update_baw(sc, an, atid, bf);
4577f1bc738eSAdrian Chadd 				if (! bf->bf_state.bfs_addedbaw)
4578f1bc738eSAdrian Chadd 					device_printf(sc->sc_dev,
4579f1bc738eSAdrian Chadd 					    "%s: wasn't added: seqno %d\n",
4580f1bc738eSAdrian Chadd 					    __func__, SEQNO(bf->bf_state.bfs_seqno));
4581f1bc738eSAdrian Chadd 			}
4582f1bc738eSAdrian Chadd 			bf->bf_state.bfs_dobaw = 0;
4583f1bc738eSAdrian Chadd 		}
4584f1bc738eSAdrian Chadd 
4585f1bc738eSAdrian Chadd 		/*
4586f1bc738eSAdrian Chadd 		 * If the frame couldn't be filtered, treat it as a drop and
4587f1bc738eSAdrian Chadd 		 * prepare to send a BAR.
4588f1bc738eSAdrian Chadd 		 */
4589f1bc738eSAdrian Chadd 		if (freeframe && drops)
4590f1bc738eSAdrian Chadd 			ath_tx_tid_bar_suspend(sc, atid);
4591f1bc738eSAdrian Chadd 
4592f1bc738eSAdrian Chadd 		/*
4593f1bc738eSAdrian Chadd 		 * Send BAR if required
4594f1bc738eSAdrian Chadd 		 */
4595f1bc738eSAdrian Chadd 		if (ath_tx_tid_bar_tx_ready(sc, atid))
4596f1bc738eSAdrian Chadd 			ath_tx_tid_bar_tx(sc, atid);
4597f1bc738eSAdrian Chadd 
4598f1bc738eSAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4599f1bc738eSAdrian Chadd 		/*
4600f1bc738eSAdrian Chadd 		 * If freeframe is set, then the frame couldn't be
4601f1bc738eSAdrian Chadd 		 * cloned and bf is still valid.  Just complete/free it.
4602f1bc738eSAdrian Chadd 		 */
4603f1bc738eSAdrian Chadd 		if (freeframe)
4604f1bc738eSAdrian Chadd 			ath_tx_default_comp(sc, bf, fail);
4605f1bc738eSAdrian Chadd 
4606f1bc738eSAdrian Chadd 
4607f1bc738eSAdrian Chadd 		return;
4608f1bc738eSAdrian Chadd 	}
4609f1bc738eSAdrian Chadd 	/*
4610eb6f0de0SAdrian Chadd 	 * Don't bother with the retry check if all frames
4611eb6f0de0SAdrian Chadd 	 * are being failed (eg during queue deletion.)
4612eb6f0de0SAdrian Chadd 	 */
4613e9a6408eSAdrian Chadd #if 0
4614eb6f0de0SAdrian Chadd 	if (fail == 0 && ts->ts_status & HAL_TXERR_XRETRY) {
4615e9a6408eSAdrian Chadd #endif
46160aa5c1bbSAdrian Chadd 	if (fail == 0 && ts.ts_status != 0) {
4617eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4618d4365d16SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: retry_unaggr\n",
4619d4365d16SAdrian Chadd 		    __func__);
4620eb6f0de0SAdrian Chadd 		ath_tx_aggr_retry_unaggr(sc, bf);
4621eb6f0de0SAdrian Chadd 		return;
4622eb6f0de0SAdrian Chadd 	}
4623eb6f0de0SAdrian Chadd 
4624eb6f0de0SAdrian Chadd 	/* Success? Complete */
4625eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: TID=%d, seqno %d\n",
4626eb6f0de0SAdrian Chadd 	    __func__, tid, SEQNO(bf->bf_state.bfs_seqno));
4627eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_dobaw) {
4628eb6f0de0SAdrian Chadd 		ath_tx_update_baw(sc, an, atid, bf);
4629eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_dobaw = 0;
4630eb6f0de0SAdrian Chadd 		if (! bf->bf_state.bfs_addedbaw)
4631eb6f0de0SAdrian Chadd 			device_printf(sc->sc_dev,
4632eb6f0de0SAdrian Chadd 			    "%s: wasn't added: seqno %d\n",
4633eb6f0de0SAdrian Chadd 			    __func__, SEQNO(bf->bf_state.bfs_seqno));
4634eb6f0de0SAdrian Chadd 	}
4635eb6f0de0SAdrian Chadd 
463688b3d483SAdrian Chadd 	/*
4637f1bc738eSAdrian Chadd 	 * If the queue is filtered, re-schedule as required.
4638f1bc738eSAdrian Chadd 	 *
4639f1bc738eSAdrian Chadd 	 * This is required as there may be a subsequent TX descriptor
4640f1bc738eSAdrian Chadd 	 * for this end-node that has CLRDMASK set, so it's quite possible
4641f1bc738eSAdrian Chadd 	 * that a filtered frame will be followed by a non-filtered
4642f1bc738eSAdrian Chadd 	 * (complete or otherwise) frame.
4643f1bc738eSAdrian Chadd 	 *
4644f1bc738eSAdrian Chadd 	 * XXX should we do this before we complete the frame?
4645f1bc738eSAdrian Chadd 	 */
4646f1bc738eSAdrian Chadd 	if (atid->isfiltered)
4647f1bc738eSAdrian Chadd 		ath_tx_tid_filt_comp_complete(sc, atid);
4648f1bc738eSAdrian Chadd 
4649f1bc738eSAdrian Chadd 	/*
465088b3d483SAdrian Chadd 	 * Send BAR if required
465188b3d483SAdrian Chadd 	 */
465288b3d483SAdrian Chadd 	if (ath_tx_tid_bar_tx_ready(sc, atid))
465388b3d483SAdrian Chadd 		ath_tx_tid_bar_tx(sc, atid);
465488b3d483SAdrian Chadd 
4655eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4656eb6f0de0SAdrian Chadd 
4657eb6f0de0SAdrian Chadd 	ath_tx_default_comp(sc, bf, fail);
4658eb6f0de0SAdrian Chadd 	/* bf is freed at this point */
4659eb6f0de0SAdrian Chadd }
4660eb6f0de0SAdrian Chadd 
4661eb6f0de0SAdrian Chadd void
4662eb6f0de0SAdrian Chadd ath_tx_aggr_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
4663eb6f0de0SAdrian Chadd {
4664eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_aggr)
4665eb6f0de0SAdrian Chadd 		ath_tx_aggr_comp_aggr(sc, bf, fail);
4666eb6f0de0SAdrian Chadd 	else
4667eb6f0de0SAdrian Chadd 		ath_tx_aggr_comp_unaggr(sc, bf, fail);
4668eb6f0de0SAdrian Chadd }
4669eb6f0de0SAdrian Chadd 
4670eb6f0de0SAdrian Chadd /*
4671eb6f0de0SAdrian Chadd  * Schedule some packets from the given node/TID to the hardware.
4672eb6f0de0SAdrian Chadd  *
4673eb6f0de0SAdrian Chadd  * This is the aggregate version.
4674eb6f0de0SAdrian Chadd  */
4675eb6f0de0SAdrian Chadd void
4676eb6f0de0SAdrian Chadd ath_tx_tid_hw_queue_aggr(struct ath_softc *sc, struct ath_node *an,
4677eb6f0de0SAdrian Chadd     struct ath_tid *tid)
4678eb6f0de0SAdrian Chadd {
4679eb6f0de0SAdrian Chadd 	struct ath_buf *bf;
4680eb6f0de0SAdrian Chadd 	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
4681eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
4682eb6f0de0SAdrian Chadd 	ATH_AGGR_STATUS status;
4683eb6f0de0SAdrian Chadd 	ath_bufhead bf_q;
4684eb6f0de0SAdrian Chadd 
4685eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d\n", __func__, tid->tid);
4686eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
4687eb6f0de0SAdrian Chadd 
4688eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
4689eb6f0de0SAdrian Chadd 
4690eb6f0de0SAdrian Chadd 	if (tid->tid == IEEE80211_NONQOS_TID)
4691eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: called for TID=NONQOS_TID?\n",
4692eb6f0de0SAdrian Chadd 		    __func__);
4693eb6f0de0SAdrian Chadd 
4694eb6f0de0SAdrian Chadd 	for (;;) {
4695eb6f0de0SAdrian Chadd 		status = ATH_AGGR_DONE;
4696eb6f0de0SAdrian Chadd 
4697eb6f0de0SAdrian Chadd 		/*
4698eb6f0de0SAdrian Chadd 		 * If the upper layer has paused the TID, don't
4699eb6f0de0SAdrian Chadd 		 * queue any further packets.
4700eb6f0de0SAdrian Chadd 		 *
4701eb6f0de0SAdrian Chadd 		 * This can also occur from the completion task because
4702eb6f0de0SAdrian Chadd 		 * of packet loss; but as its serialised with this code,
4703eb6f0de0SAdrian Chadd 		 * it won't "appear" half way through queuing packets.
4704eb6f0de0SAdrian Chadd 		 */
4705eb6f0de0SAdrian Chadd 		if (tid->paused)
4706eb6f0de0SAdrian Chadd 			break;
4707eb6f0de0SAdrian Chadd 
47083e6cc97fSAdrian Chadd 		bf = ATH_TID_FIRST(tid);
4709eb6f0de0SAdrian Chadd 		if (bf == NULL) {
4710eb6f0de0SAdrian Chadd 			break;
4711eb6f0de0SAdrian Chadd 		}
4712eb6f0de0SAdrian Chadd 
4713eb6f0de0SAdrian Chadd 		/*
4714eb6f0de0SAdrian Chadd 		 * If the packet doesn't fall within the BAW (eg a NULL
4715eb6f0de0SAdrian Chadd 		 * data frame), schedule it directly; continue.
4716eb6f0de0SAdrian Chadd 		 */
4717eb6f0de0SAdrian Chadd 		if (! bf->bf_state.bfs_dobaw) {
4718d4365d16SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4719d4365d16SAdrian Chadd 			    "%s: non-baw packet\n",
4720eb6f0de0SAdrian Chadd 			    __func__);
47213e6cc97fSAdrian Chadd 			ATH_TID_REMOVE(tid, bf, bf_list);
47222a9f83afSAdrian Chadd 
47232a9f83afSAdrian Chadd 			if (bf->bf_state.bfs_nframes > 1)
47242a9f83afSAdrian Chadd 				device_printf(sc->sc_dev,
47252a9f83afSAdrian Chadd 				    "%s: aggr=%d, nframes=%d\n",
47262a9f83afSAdrian Chadd 				    __func__,
47272a9f83afSAdrian Chadd 				    bf->bf_state.bfs_aggr,
47282a9f83afSAdrian Chadd 				    bf->bf_state.bfs_nframes);
47292a9f83afSAdrian Chadd 
47302a9f83afSAdrian Chadd 			/*
47312a9f83afSAdrian Chadd 			 * This shouldn't happen - such frames shouldn't
47322a9f83afSAdrian Chadd 			 * ever have been queued as an aggregate in the
47332a9f83afSAdrian Chadd 			 * first place.  However, make sure the fields
47342a9f83afSAdrian Chadd 			 * are correctly setup just to be totally sure.
47352a9f83afSAdrian Chadd 			 */
4736eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_aggr = 0;
47372a9f83afSAdrian Chadd 			bf->bf_state.bfs_nframes = 1;
47382a9f83afSAdrian Chadd 
47394e81f27cSAdrian Chadd 			/* Update CLRDMASK just before this frame is queued */
47404e81f27cSAdrian Chadd 			ath_tx_update_clrdmask(sc, tid, bf);
47414e81f27cSAdrian Chadd 
4742eb6f0de0SAdrian Chadd 			ath_tx_do_ratelookup(sc, bf);
4743e2e4a2c2SAdrian Chadd 			ath_tx_calc_duration(sc, bf);
4744e2e4a2c2SAdrian Chadd 			ath_tx_calc_protection(sc, bf);
4745eb6f0de0SAdrian Chadd 			ath_tx_set_rtscts(sc, bf);
4746e2e4a2c2SAdrian Chadd 			ath_tx_rate_fill_rcflags(sc, bf);
4747eb6f0de0SAdrian Chadd 			ath_tx_setds(sc, bf);
4748eb6f0de0SAdrian Chadd 			ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
4749eb6f0de0SAdrian Chadd 
4750eb6f0de0SAdrian Chadd 			sc->sc_aggr_stats.aggr_nonbaw_pkt++;
4751eb6f0de0SAdrian Chadd 
4752eb6f0de0SAdrian Chadd 			/* Queue the packet; continue */
4753eb6f0de0SAdrian Chadd 			goto queuepkt;
4754eb6f0de0SAdrian Chadd 		}
4755eb6f0de0SAdrian Chadd 
4756eb6f0de0SAdrian Chadd 		TAILQ_INIT(&bf_q);
4757eb6f0de0SAdrian Chadd 
4758eb6f0de0SAdrian Chadd 		/*
4759eb6f0de0SAdrian Chadd 		 * Do a rate control lookup on the first frame in the
4760eb6f0de0SAdrian Chadd 		 * list. The rate control code needs that to occur
4761eb6f0de0SAdrian Chadd 		 * before it can determine whether to TX.
4762eb6f0de0SAdrian Chadd 		 * It's inaccurate because the rate control code doesn't
4763eb6f0de0SAdrian Chadd 		 * really "do" aggregate lookups, so it only considers
4764eb6f0de0SAdrian Chadd 		 * the size of the first frame.
4765eb6f0de0SAdrian Chadd 		 */
4766eb6f0de0SAdrian Chadd 		ath_tx_do_ratelookup(sc, bf);
4767eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[3].rix = 0;
4768eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[3].tries = 0;
4769e2e4a2c2SAdrian Chadd 
4770e2e4a2c2SAdrian Chadd 		ath_tx_calc_duration(sc, bf);
4771e2e4a2c2SAdrian Chadd 		ath_tx_calc_protection(sc, bf);
4772e2e4a2c2SAdrian Chadd 
4773e2e4a2c2SAdrian Chadd 		ath_tx_set_rtscts(sc, bf);
4774eb6f0de0SAdrian Chadd 		ath_tx_rate_fill_rcflags(sc, bf);
4775eb6f0de0SAdrian Chadd 
4776eb6f0de0SAdrian Chadd 		status = ath_tx_form_aggr(sc, an, tid, &bf_q);
4777eb6f0de0SAdrian Chadd 
4778eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4779eb6f0de0SAdrian Chadd 		    "%s: ath_tx_form_aggr() status=%d\n", __func__, status);
4780eb6f0de0SAdrian Chadd 
4781eb6f0de0SAdrian Chadd 		/*
4782eb6f0de0SAdrian Chadd 		 * No frames to be picked up - out of BAW
4783eb6f0de0SAdrian Chadd 		 */
4784eb6f0de0SAdrian Chadd 		if (TAILQ_EMPTY(&bf_q))
4785eb6f0de0SAdrian Chadd 			break;
4786eb6f0de0SAdrian Chadd 
4787eb6f0de0SAdrian Chadd 		/*
4788eb6f0de0SAdrian Chadd 		 * This assumes that the descriptor list in the ath_bufhead
4789eb6f0de0SAdrian Chadd 		 * are already linked together via bf_next pointers.
4790eb6f0de0SAdrian Chadd 		 */
4791eb6f0de0SAdrian Chadd 		bf = TAILQ_FIRST(&bf_q);
4792eb6f0de0SAdrian Chadd 
4793e2e4a2c2SAdrian Chadd 		if (status == ATH_AGGR_8K_LIMITED)
4794e2e4a2c2SAdrian Chadd 			sc->sc_aggr_stats.aggr_rts_aggr_limited++;
4795e2e4a2c2SAdrian Chadd 
4796eb6f0de0SAdrian Chadd 		/*
4797eb6f0de0SAdrian Chadd 		 * If it's the only frame send as non-aggregate
4798eb6f0de0SAdrian Chadd 		 * assume that ath_tx_form_aggr() has checked
4799eb6f0de0SAdrian Chadd 		 * whether it's in the BAW and added it appropriately.
4800eb6f0de0SAdrian Chadd 		 */
4801eb6f0de0SAdrian Chadd 		if (bf->bf_state.bfs_nframes == 1) {
4802eb6f0de0SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4803eb6f0de0SAdrian Chadd 			    "%s: single-frame aggregate\n", __func__);
48044e81f27cSAdrian Chadd 
48054e81f27cSAdrian Chadd 			/* Update CLRDMASK just before this frame is queued */
48064e81f27cSAdrian Chadd 			ath_tx_update_clrdmask(sc, tid, bf);
48074e81f27cSAdrian Chadd 
4808eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_aggr = 0;
480921840808SAdrian Chadd 			bf->bf_state.bfs_ndelim = 0;
4810eb6f0de0SAdrian Chadd 			ath_tx_setds(sc, bf);
4811eb6f0de0SAdrian Chadd 			ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
4812eb6f0de0SAdrian Chadd 			if (status == ATH_AGGR_BAW_CLOSED)
4813eb6f0de0SAdrian Chadd 				sc->sc_aggr_stats.aggr_baw_closed_single_pkt++;
4814eb6f0de0SAdrian Chadd 			else
4815eb6f0de0SAdrian Chadd 				sc->sc_aggr_stats.aggr_single_pkt++;
4816eb6f0de0SAdrian Chadd 		} else {
4817eb6f0de0SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4818d4365d16SAdrian Chadd 			    "%s: multi-frame aggregate: %d frames, "
4819d4365d16SAdrian Chadd 			    "length %d\n",
4820eb6f0de0SAdrian Chadd 			     __func__, bf->bf_state.bfs_nframes,
4821eb6f0de0SAdrian Chadd 			    bf->bf_state.bfs_al);
4822eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_aggr = 1;
4823eb6f0de0SAdrian Chadd 			sc->sc_aggr_stats.aggr_pkts[bf->bf_state.bfs_nframes]++;
4824eb6f0de0SAdrian Chadd 			sc->sc_aggr_stats.aggr_aggr_pkt++;
4825eb6f0de0SAdrian Chadd 
48264e81f27cSAdrian Chadd 			/* Update CLRDMASK just before this frame is queued */
48274e81f27cSAdrian Chadd 			ath_tx_update_clrdmask(sc, tid, bf);
48284e81f27cSAdrian Chadd 
4829eb6f0de0SAdrian Chadd 			/*
4830e2e4a2c2SAdrian Chadd 			 * Calculate the duration/protection as required.
4831e2e4a2c2SAdrian Chadd 			 */
4832e2e4a2c2SAdrian Chadd 			ath_tx_calc_duration(sc, bf);
4833e2e4a2c2SAdrian Chadd 			ath_tx_calc_protection(sc, bf);
4834e2e4a2c2SAdrian Chadd 
4835e2e4a2c2SAdrian Chadd 			/*
4836eb6f0de0SAdrian Chadd 			 * Update the rate and rtscts information based on the
4837eb6f0de0SAdrian Chadd 			 * rate decision made by the rate control code;
4838eb6f0de0SAdrian Chadd 			 * the first frame in the aggregate needs it.
4839eb6f0de0SAdrian Chadd 			 */
4840eb6f0de0SAdrian Chadd 			ath_tx_set_rtscts(sc, bf);
4841eb6f0de0SAdrian Chadd 
4842eb6f0de0SAdrian Chadd 			/*
4843eb6f0de0SAdrian Chadd 			 * Setup the relevant descriptor fields
4844eb6f0de0SAdrian Chadd 			 * for aggregation. The first descriptor
4845eb6f0de0SAdrian Chadd 			 * already points to the rest in the chain.
4846eb6f0de0SAdrian Chadd 			 */
4847eb6f0de0SAdrian Chadd 			ath_tx_setds_11n(sc, bf);
4848eb6f0de0SAdrian Chadd 
4849eb6f0de0SAdrian Chadd 		}
4850eb6f0de0SAdrian Chadd 	queuepkt:
4851eb6f0de0SAdrian Chadd 		//txq = bf->bf_state.bfs_txq;
4852eb6f0de0SAdrian Chadd 
4853eb6f0de0SAdrian Chadd 		/* Set completion handler, multi-frame aggregate or not */
4854eb6f0de0SAdrian Chadd 		bf->bf_comp = ath_tx_aggr_comp;
4855eb6f0de0SAdrian Chadd 
4856eb6f0de0SAdrian Chadd 		if (bf->bf_state.bfs_tid == IEEE80211_NONQOS_TID)
4857eb6f0de0SAdrian Chadd 		    device_printf(sc->sc_dev, "%s: TID=16?\n", __func__);
4858eb6f0de0SAdrian Chadd 
4859eb6f0de0SAdrian Chadd 		/* Punt to txq */
4860eb6f0de0SAdrian Chadd 		ath_tx_handoff(sc, txq, bf);
4861eb6f0de0SAdrian Chadd 
4862eb6f0de0SAdrian Chadd 		/* Track outstanding buffer count to hardware */
4863eb6f0de0SAdrian Chadd 		/* aggregates are "one" buffer */
4864eb6f0de0SAdrian Chadd 		tid->hwq_depth++;
4865eb6f0de0SAdrian Chadd 
4866eb6f0de0SAdrian Chadd 		/*
4867eb6f0de0SAdrian Chadd 		 * Break out if ath_tx_form_aggr() indicated
4868eb6f0de0SAdrian Chadd 		 * there can't be any further progress (eg BAW is full.)
4869eb6f0de0SAdrian Chadd 		 * Checking for an empty txq is done above.
4870eb6f0de0SAdrian Chadd 		 *
4871eb6f0de0SAdrian Chadd 		 * XXX locking on txq here?
4872eb6f0de0SAdrian Chadd 		 */
4873eb6f0de0SAdrian Chadd 		if (txq->axq_aggr_depth >= sc->sc_hwq_limit ||
4874eb6f0de0SAdrian Chadd 		    status == ATH_AGGR_BAW_CLOSED)
4875eb6f0de0SAdrian Chadd 			break;
4876eb6f0de0SAdrian Chadd 	}
4877eb6f0de0SAdrian Chadd }
4878eb6f0de0SAdrian Chadd 
4879eb6f0de0SAdrian Chadd /*
4880eb6f0de0SAdrian Chadd  * Schedule some packets from the given node/TID to the hardware.
4881eb6f0de0SAdrian Chadd  */
4882eb6f0de0SAdrian Chadd void
4883eb6f0de0SAdrian Chadd ath_tx_tid_hw_queue_norm(struct ath_softc *sc, struct ath_node *an,
4884eb6f0de0SAdrian Chadd     struct ath_tid *tid)
4885eb6f0de0SAdrian Chadd {
4886eb6f0de0SAdrian Chadd 	struct ath_buf *bf;
4887eb6f0de0SAdrian Chadd 	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
4888eb6f0de0SAdrian Chadd 
4889eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: node %p: TID %d: called\n",
4890eb6f0de0SAdrian Chadd 	    __func__, an, tid->tid);
4891eb6f0de0SAdrian Chadd 
48924e81f27cSAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
4893eb6f0de0SAdrian Chadd 
4894eb6f0de0SAdrian Chadd 	/* Check - is AMPDU pending or running? then print out something */
4895eb6f0de0SAdrian Chadd 	if (ath_tx_ampdu_pending(sc, an, tid->tid))
4896eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: tid=%d, ampdu pending?\n",
4897eb6f0de0SAdrian Chadd 		    __func__, tid->tid);
4898eb6f0de0SAdrian Chadd 	if (ath_tx_ampdu_running(sc, an, tid->tid))
4899eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: tid=%d, ampdu running?\n",
4900eb6f0de0SAdrian Chadd 		    __func__, tid->tid);
4901eb6f0de0SAdrian Chadd 
4902eb6f0de0SAdrian Chadd 	for (;;) {
4903eb6f0de0SAdrian Chadd 
4904eb6f0de0SAdrian Chadd 		/*
4905eb6f0de0SAdrian Chadd 		 * If the upper layers have paused the TID, don't
4906eb6f0de0SAdrian Chadd 		 * queue any further packets.
4907eb6f0de0SAdrian Chadd 		 */
4908eb6f0de0SAdrian Chadd 		if (tid->paused)
4909eb6f0de0SAdrian Chadd 			break;
4910eb6f0de0SAdrian Chadd 
49113e6cc97fSAdrian Chadd 		bf = ATH_TID_FIRST(tid);
4912eb6f0de0SAdrian Chadd 		if (bf == NULL) {
4913eb6f0de0SAdrian Chadd 			break;
4914eb6f0de0SAdrian Chadd 		}
4915eb6f0de0SAdrian Chadd 
49163e6cc97fSAdrian Chadd 		ATH_TID_REMOVE(tid, bf, bf_list);
4917eb6f0de0SAdrian Chadd 
4918eb6f0de0SAdrian Chadd 		KASSERT(txq == bf->bf_state.bfs_txq, ("txqs not equal!\n"));
4919eb6f0de0SAdrian Chadd 
4920eb6f0de0SAdrian Chadd 		/* Sanity check! */
4921eb6f0de0SAdrian Chadd 		if (tid->tid != bf->bf_state.bfs_tid) {
4922eb6f0de0SAdrian Chadd 			device_printf(sc->sc_dev, "%s: bfs_tid %d !="
4923eb6f0de0SAdrian Chadd 			    " tid %d\n",
4924eb6f0de0SAdrian Chadd 			    __func__, bf->bf_state.bfs_tid, tid->tid);
4925eb6f0de0SAdrian Chadd 		}
4926eb6f0de0SAdrian Chadd 		/* Normal completion handler */
4927eb6f0de0SAdrian Chadd 		bf->bf_comp = ath_tx_normal_comp;
4928eb6f0de0SAdrian Chadd 
49290c54de88SAdrian Chadd 		/*
49300c54de88SAdrian Chadd 		 * Override this for now, until the non-aggregate
49310c54de88SAdrian Chadd 		 * completion handler correctly handles software retransmits.
49320c54de88SAdrian Chadd 		 */
49330c54de88SAdrian Chadd 		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
49340c54de88SAdrian Chadd 
49354e81f27cSAdrian Chadd 		/* Update CLRDMASK just before this frame is queued */
49364e81f27cSAdrian Chadd 		ath_tx_update_clrdmask(sc, tid, bf);
49374e81f27cSAdrian Chadd 
4938eb6f0de0SAdrian Chadd 		/* Program descriptors + rate control */
4939eb6f0de0SAdrian Chadd 		ath_tx_do_ratelookup(sc, bf);
4940e2e4a2c2SAdrian Chadd 		ath_tx_calc_duration(sc, bf);
4941e2e4a2c2SAdrian Chadd 		ath_tx_calc_protection(sc, bf);
4942eb6f0de0SAdrian Chadd 		ath_tx_set_rtscts(sc, bf);
4943e2e4a2c2SAdrian Chadd 		ath_tx_rate_fill_rcflags(sc, bf);
4944eb6f0de0SAdrian Chadd 		ath_tx_setds(sc, bf);
4945eb6f0de0SAdrian Chadd 
4946eb6f0de0SAdrian Chadd 		/* Track outstanding buffer count to hardware */
4947eb6f0de0SAdrian Chadd 		/* aggregates are "one" buffer */
4948eb6f0de0SAdrian Chadd 		tid->hwq_depth++;
4949eb6f0de0SAdrian Chadd 
4950eb6f0de0SAdrian Chadd 		/* Punt to hardware or software txq */
4951eb6f0de0SAdrian Chadd 		ath_tx_handoff(sc, txq, bf);
4952eb6f0de0SAdrian Chadd 	}
4953eb6f0de0SAdrian Chadd }
4954eb6f0de0SAdrian Chadd 
4955eb6f0de0SAdrian Chadd /*
4956eb6f0de0SAdrian Chadd  * Schedule some packets to the given hardware queue.
4957eb6f0de0SAdrian Chadd  *
4958eb6f0de0SAdrian Chadd  * This function walks the list of TIDs (ie, ath_node TIDs
4959eb6f0de0SAdrian Chadd  * with queued traffic) and attempts to schedule traffic
4960eb6f0de0SAdrian Chadd  * from them.
4961eb6f0de0SAdrian Chadd  *
4962eb6f0de0SAdrian Chadd  * TID scheduling is implemented as a FIFO, with TIDs being
4963eb6f0de0SAdrian Chadd  * added to the end of the queue after some frames have been
4964eb6f0de0SAdrian Chadd  * scheduled.
4965eb6f0de0SAdrian Chadd  */
4966eb6f0de0SAdrian Chadd void
4967eb6f0de0SAdrian Chadd ath_txq_sched(struct ath_softc *sc, struct ath_txq *txq)
4968eb6f0de0SAdrian Chadd {
4969eb6f0de0SAdrian Chadd 	struct ath_tid *tid, *next, *last;
4970eb6f0de0SAdrian Chadd 
4971eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
4972eb6f0de0SAdrian Chadd 
4973eb6f0de0SAdrian Chadd 	/*
4974eb6f0de0SAdrian Chadd 	 * Don't schedule if the hardware queue is busy.
4975eb6f0de0SAdrian Chadd 	 * This (hopefully) gives some more time to aggregate
4976eb6f0de0SAdrian Chadd 	 * some packets in the aggregation queue.
4977eb6f0de0SAdrian Chadd 	 */
4978eb6f0de0SAdrian Chadd 	if (txq->axq_aggr_depth >= sc->sc_hwq_limit) {
4979eb6f0de0SAdrian Chadd 		sc->sc_aggr_stats.aggr_sched_nopkt++;
4980eb6f0de0SAdrian Chadd 		return;
4981eb6f0de0SAdrian Chadd 	}
4982eb6f0de0SAdrian Chadd 
4983eb6f0de0SAdrian Chadd 	last = TAILQ_LAST(&txq->axq_tidq, axq_t_s);
4984eb6f0de0SAdrian Chadd 
4985eb6f0de0SAdrian Chadd 	TAILQ_FOREACH_SAFE(tid, &txq->axq_tidq, axq_qelem, next) {
4986eb6f0de0SAdrian Chadd 		/*
4987eb6f0de0SAdrian Chadd 		 * Suspend paused queues here; they'll be resumed
4988eb6f0de0SAdrian Chadd 		 * once the addba completes or times out.
4989eb6f0de0SAdrian Chadd 		 */
4990eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d, paused=%d\n",
4991eb6f0de0SAdrian Chadd 		    __func__, tid->tid, tid->paused);
4992eb6f0de0SAdrian Chadd 		ath_tx_tid_unsched(sc, tid);
4993eb6f0de0SAdrian Chadd 		if (tid->paused) {
4994eb6f0de0SAdrian Chadd 			continue;
4995eb6f0de0SAdrian Chadd 		}
4996eb6f0de0SAdrian Chadd 		if (ath_tx_ampdu_running(sc, tid->an, tid->tid))
4997eb6f0de0SAdrian Chadd 			ath_tx_tid_hw_queue_aggr(sc, tid->an, tid);
4998eb6f0de0SAdrian Chadd 		else
4999eb6f0de0SAdrian Chadd 			ath_tx_tid_hw_queue_norm(sc, tid->an, tid);
5000eb6f0de0SAdrian Chadd 
5001eb6f0de0SAdrian Chadd 		/* Not empty? Re-schedule */
5002eb6f0de0SAdrian Chadd 		if (tid->axq_depth != 0)
5003eb6f0de0SAdrian Chadd 			ath_tx_tid_sched(sc, tid);
5004eb6f0de0SAdrian Chadd 
5005eb6f0de0SAdrian Chadd 		/* Give the software queue time to aggregate more packets */
5006eb6f0de0SAdrian Chadd 		if (txq->axq_aggr_depth >= sc->sc_hwq_limit) {
5007eb6f0de0SAdrian Chadd 			break;
5008eb6f0de0SAdrian Chadd 		}
5009eb6f0de0SAdrian Chadd 
5010eb6f0de0SAdrian Chadd 		/*
5011eb6f0de0SAdrian Chadd 		 * If this was the last entry on the original list, stop.
5012eb6f0de0SAdrian Chadd 		 * Otherwise nodes that have been rescheduled onto the end
5013eb6f0de0SAdrian Chadd 		 * of the TID FIFO list will just keep being rescheduled.
5014eb6f0de0SAdrian Chadd 		 */
5015eb6f0de0SAdrian Chadd 		if (tid == last)
5016eb6f0de0SAdrian Chadd 			break;
5017eb6f0de0SAdrian Chadd 	}
5018eb6f0de0SAdrian Chadd }
5019eb6f0de0SAdrian Chadd 
5020eb6f0de0SAdrian Chadd /*
5021eb6f0de0SAdrian Chadd  * TX addba handling
5022eb6f0de0SAdrian Chadd  */
5023eb6f0de0SAdrian Chadd 
5024eb6f0de0SAdrian Chadd /*
5025eb6f0de0SAdrian Chadd  * Return net80211 TID struct pointer, or NULL for none
5026eb6f0de0SAdrian Chadd  */
5027eb6f0de0SAdrian Chadd struct ieee80211_tx_ampdu *
5028eb6f0de0SAdrian Chadd ath_tx_get_tx_tid(struct ath_node *an, int tid)
5029eb6f0de0SAdrian Chadd {
5030eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = &an->an_node;
5031eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
5032eb6f0de0SAdrian Chadd 
5033eb6f0de0SAdrian Chadd 	if (tid == IEEE80211_NONQOS_TID)
5034eb6f0de0SAdrian Chadd 		return NULL;
5035eb6f0de0SAdrian Chadd 
50362aa563dfSAdrian Chadd 	tap = &ni->ni_tx_ampdu[tid];
5037eb6f0de0SAdrian Chadd 	return tap;
5038eb6f0de0SAdrian Chadd }
5039eb6f0de0SAdrian Chadd 
5040eb6f0de0SAdrian Chadd /*
5041eb6f0de0SAdrian Chadd  * Is AMPDU-TX running?
5042eb6f0de0SAdrian Chadd  */
5043eb6f0de0SAdrian Chadd static int
5044eb6f0de0SAdrian Chadd ath_tx_ampdu_running(struct ath_softc *sc, struct ath_node *an, int tid)
5045eb6f0de0SAdrian Chadd {
5046eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
5047eb6f0de0SAdrian Chadd 
5048eb6f0de0SAdrian Chadd 	if (tid == IEEE80211_NONQOS_TID)
5049eb6f0de0SAdrian Chadd 		return 0;
5050eb6f0de0SAdrian Chadd 
5051eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
5052eb6f0de0SAdrian Chadd 	if (tap == NULL)
5053eb6f0de0SAdrian Chadd 		return 0;	/* Not valid; default to not running */
5054eb6f0de0SAdrian Chadd 
5055eb6f0de0SAdrian Chadd 	return !! (tap->txa_flags & IEEE80211_AGGR_RUNNING);
5056eb6f0de0SAdrian Chadd }
5057eb6f0de0SAdrian Chadd 
5058eb6f0de0SAdrian Chadd /*
5059eb6f0de0SAdrian Chadd  * Is AMPDU-TX negotiation pending?
5060eb6f0de0SAdrian Chadd  */
5061eb6f0de0SAdrian Chadd static int
5062eb6f0de0SAdrian Chadd ath_tx_ampdu_pending(struct ath_softc *sc, struct ath_node *an, int tid)
5063eb6f0de0SAdrian Chadd {
5064eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
5065eb6f0de0SAdrian Chadd 
5066eb6f0de0SAdrian Chadd 	if (tid == IEEE80211_NONQOS_TID)
5067eb6f0de0SAdrian Chadd 		return 0;
5068eb6f0de0SAdrian Chadd 
5069eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
5070eb6f0de0SAdrian Chadd 	if (tap == NULL)
5071eb6f0de0SAdrian Chadd 		return 0;	/* Not valid; default to not pending */
5072eb6f0de0SAdrian Chadd 
5073eb6f0de0SAdrian Chadd 	return !! (tap->txa_flags & IEEE80211_AGGR_XCHGPEND);
5074eb6f0de0SAdrian Chadd }
5075eb6f0de0SAdrian Chadd 
5076eb6f0de0SAdrian Chadd /*
5077eb6f0de0SAdrian Chadd  * Is AMPDU-TX pending for the given TID?
5078eb6f0de0SAdrian Chadd  */
5079eb6f0de0SAdrian Chadd 
5080eb6f0de0SAdrian Chadd 
5081eb6f0de0SAdrian Chadd /*
5082eb6f0de0SAdrian Chadd  * Method to handle sending an ADDBA request.
5083eb6f0de0SAdrian Chadd  *
5084eb6f0de0SAdrian Chadd  * We tap this so the relevant flags can be set to pause the TID
5085eb6f0de0SAdrian Chadd  * whilst waiting for the response.
5086eb6f0de0SAdrian Chadd  *
5087eb6f0de0SAdrian Chadd  * XXX there's no timeout handler we can override?
5088eb6f0de0SAdrian Chadd  */
5089eb6f0de0SAdrian Chadd int
5090eb6f0de0SAdrian Chadd ath_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
5091eb6f0de0SAdrian Chadd     int dialogtoken, int baparamset, int batimeout)
5092eb6f0de0SAdrian Chadd {
5093eb6f0de0SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
50942aa563dfSAdrian Chadd 	int tid = tap->txa_tid;
5095eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
5096eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
5097eb6f0de0SAdrian Chadd 
5098eb6f0de0SAdrian Chadd 	/*
5099eb6f0de0SAdrian Chadd 	 * XXX danger Will Robinson!
5100eb6f0de0SAdrian Chadd 	 *
5101eb6f0de0SAdrian Chadd 	 * Although the taskqueue may be running and scheduling some more
5102eb6f0de0SAdrian Chadd 	 * packets, these should all be _before_ the addba sequence number.
5103eb6f0de0SAdrian Chadd 	 * However, net80211 will keep self-assigning sequence numbers
5104eb6f0de0SAdrian Chadd 	 * until addba has been negotiated.
5105eb6f0de0SAdrian Chadd 	 *
5106eb6f0de0SAdrian Chadd 	 * In the past, these packets would be "paused" (which still works
5107eb6f0de0SAdrian Chadd 	 * fine, as they're being scheduled to the driver in the same
5108eb6f0de0SAdrian Chadd 	 * serialised method which is calling the addba request routine)
5109eb6f0de0SAdrian Chadd 	 * and when the aggregation session begins, they'll be dequeued
5110eb6f0de0SAdrian Chadd 	 * as aggregate packets and added to the BAW. However, now there's
5111eb6f0de0SAdrian Chadd 	 * a "bf->bf_state.bfs_dobaw" flag, and this isn't set for these
5112eb6f0de0SAdrian Chadd 	 * packets. Thus they never get included in the BAW tracking and
5113eb6f0de0SAdrian Chadd 	 * this can cause the initial burst of packets after the addba
5114eb6f0de0SAdrian Chadd 	 * negotiation to "hang", as they quickly fall outside the BAW.
5115eb6f0de0SAdrian Chadd 	 *
5116eb6f0de0SAdrian Chadd 	 * The "eventual" solution should be to tag these packets with
5117eb6f0de0SAdrian Chadd 	 * dobaw. Although net80211 has given us a sequence number,
5118eb6f0de0SAdrian Chadd 	 * it'll be "after" the left edge of the BAW and thus it'll
5119eb6f0de0SAdrian Chadd 	 * fall within it.
5120eb6f0de0SAdrian Chadd 	 */
512196ff26ffSAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
5122d3a6425bSAdrian Chadd 	/*
5123d3a6425bSAdrian Chadd 	 * This is a bit annoying.  Until net80211 HT code inherits some
5124d3a6425bSAdrian Chadd 	 * (any) locking, we may have this called in parallel BUT only
5125d3a6425bSAdrian Chadd 	 * one response/timeout will be called.  Grr.
5126d3a6425bSAdrian Chadd 	 */
5127d3a6425bSAdrian Chadd 	if (atid->addba_tx_pending == 0) {
5128eb6f0de0SAdrian Chadd 		ath_tx_tid_pause(sc, atid);
5129d3a6425bSAdrian Chadd 		atid->addba_tx_pending = 1;
5130d3a6425bSAdrian Chadd 	}
513196ff26ffSAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
5132eb6f0de0SAdrian Chadd 
5133eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5134eb6f0de0SAdrian Chadd 	    "%s: called; dialogtoken=%d, baparamset=%d, batimeout=%d\n",
5135eb6f0de0SAdrian Chadd 	    __func__, dialogtoken, baparamset, batimeout);
5136eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5137eb6f0de0SAdrian Chadd 	    "%s: txa_start=%d, ni_txseqs=%d\n",
5138eb6f0de0SAdrian Chadd 	    __func__, tap->txa_start, ni->ni_txseqs[tid]);
5139eb6f0de0SAdrian Chadd 
5140eb6f0de0SAdrian Chadd 	return sc->sc_addba_request(ni, tap, dialogtoken, baparamset,
5141eb6f0de0SAdrian Chadd 	    batimeout);
5142eb6f0de0SAdrian Chadd }
5143eb6f0de0SAdrian Chadd 
5144eb6f0de0SAdrian Chadd /*
5145eb6f0de0SAdrian Chadd  * Handle an ADDBA response.
5146eb6f0de0SAdrian Chadd  *
5147eb6f0de0SAdrian Chadd  * We unpause the queue so TX'ing can resume.
5148eb6f0de0SAdrian Chadd  *
5149eb6f0de0SAdrian Chadd  * Any packets TX'ed from this point should be "aggregate" (whether
5150eb6f0de0SAdrian Chadd  * aggregate or not) so the BAW is updated.
5151eb6f0de0SAdrian Chadd  *
5152eb6f0de0SAdrian Chadd  * Note! net80211 keeps self-assigning sequence numbers until
5153eb6f0de0SAdrian Chadd  * ampdu is negotiated. This means the initially-negotiated BAW left
5154eb6f0de0SAdrian Chadd  * edge won't match the ni->ni_txseq.
5155eb6f0de0SAdrian Chadd  *
5156eb6f0de0SAdrian Chadd  * So, being very dirty, the BAW left edge is "slid" here to match
5157eb6f0de0SAdrian Chadd  * ni->ni_txseq.
5158eb6f0de0SAdrian Chadd  *
5159eb6f0de0SAdrian Chadd  * What likely SHOULD happen is that all packets subsequent to the
5160eb6f0de0SAdrian Chadd  * addba request should be tagged as aggregate and queued as non-aggregate
5161eb6f0de0SAdrian Chadd  * frames; thus updating the BAW. For now though, I'll just slide the
5162eb6f0de0SAdrian Chadd  * window.
5163eb6f0de0SAdrian Chadd  */
5164eb6f0de0SAdrian Chadd int
5165eb6f0de0SAdrian Chadd ath_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
5166eb6f0de0SAdrian Chadd     int status, int code, int batimeout)
5167eb6f0de0SAdrian Chadd {
5168eb6f0de0SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
51692aa563dfSAdrian Chadd 	int tid = tap->txa_tid;
5170eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
5171eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
5172eb6f0de0SAdrian Chadd 	int r;
5173eb6f0de0SAdrian Chadd 
5174eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5175eb6f0de0SAdrian Chadd 	    "%s: called; status=%d, code=%d, batimeout=%d\n", __func__,
5176eb6f0de0SAdrian Chadd 	    status, code, batimeout);
5177eb6f0de0SAdrian Chadd 
5178eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5179eb6f0de0SAdrian Chadd 	    "%s: txa_start=%d, ni_txseqs=%d\n",
5180eb6f0de0SAdrian Chadd 	    __func__, tap->txa_start, ni->ni_txseqs[tid]);
5181eb6f0de0SAdrian Chadd 
5182eb6f0de0SAdrian Chadd 	/*
5183eb6f0de0SAdrian Chadd 	 * Call this first, so the interface flags get updated
5184eb6f0de0SAdrian Chadd 	 * before the TID is unpaused. Otherwise a race condition
5185eb6f0de0SAdrian Chadd 	 * exists where the unpaused TID still doesn't yet have
5186eb6f0de0SAdrian Chadd 	 * IEEE80211_AGGR_RUNNING set.
5187eb6f0de0SAdrian Chadd 	 */
5188eb6f0de0SAdrian Chadd 	r = sc->sc_addba_response(ni, tap, status, code, batimeout);
5189eb6f0de0SAdrian Chadd 
5190eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
5191d3a6425bSAdrian Chadd 	atid->addba_tx_pending = 0;
5192eb6f0de0SAdrian Chadd 	/*
5193eb6f0de0SAdrian Chadd 	 * XXX dirty!
5194eb6f0de0SAdrian Chadd 	 * Slide the BAW left edge to wherever net80211 left it for us.
5195eb6f0de0SAdrian Chadd 	 * Read above for more information.
5196eb6f0de0SAdrian Chadd 	 */
5197eb6f0de0SAdrian Chadd 	tap->txa_start = ni->ni_txseqs[tid];
5198eb6f0de0SAdrian Chadd 	ath_tx_tid_resume(sc, atid);
5199eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
5200eb6f0de0SAdrian Chadd 	return r;
5201eb6f0de0SAdrian Chadd }
5202eb6f0de0SAdrian Chadd 
5203eb6f0de0SAdrian Chadd 
5204eb6f0de0SAdrian Chadd /*
5205eb6f0de0SAdrian Chadd  * Stop ADDBA on a queue.
52068405fe86SAdrian Chadd  *
52078405fe86SAdrian Chadd  * This can be called whilst BAR TX is currently active on the queue,
52088405fe86SAdrian Chadd  * so make sure this is unblocked before continuing.
5209eb6f0de0SAdrian Chadd  */
5210eb6f0de0SAdrian Chadd void
5211eb6f0de0SAdrian Chadd ath_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
5212eb6f0de0SAdrian Chadd {
5213eb6f0de0SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
52142aa563dfSAdrian Chadd 	int tid = tap->txa_tid;
5215eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
5216eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
5217eb6f0de0SAdrian Chadd 
5218eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: called\n", __func__);
5219eb6f0de0SAdrian Chadd 
52208405fe86SAdrian Chadd 	/*
52218405fe86SAdrian Chadd 	 * Pause TID traffic early, so there aren't any races
52228405fe86SAdrian Chadd 	 * Unblock the pending BAR held traffic, if it's currently paused.
52238405fe86SAdrian Chadd 	 */
522496ff26ffSAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
5225eb6f0de0SAdrian Chadd 	ath_tx_tid_pause(sc, atid);
52268405fe86SAdrian Chadd 	if (atid->bar_wait) {
52278405fe86SAdrian Chadd 		/*
52288405fe86SAdrian Chadd 		 * bar_unsuspend() expects bar_tx == 1, as it should be
52298405fe86SAdrian Chadd 		 * called from the TX completion path.  This quietens
52308405fe86SAdrian Chadd 		 * the warning.  It's cleared for us anyway.
52318405fe86SAdrian Chadd 		 */
52328405fe86SAdrian Chadd 		atid->bar_tx = 1;
52338405fe86SAdrian Chadd 		ath_tx_tid_bar_unsuspend(sc, atid);
52348405fe86SAdrian Chadd 	}
523596ff26ffSAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
5236eb6f0de0SAdrian Chadd 
5237eb6f0de0SAdrian Chadd 	/* There's no need to hold the TXQ lock here */
5238eb6f0de0SAdrian Chadd 	sc->sc_addba_stop(ni, tap);
5239eb6f0de0SAdrian Chadd 
5240eb6f0de0SAdrian Chadd 	/*
52414dfd4507SAdrian Chadd 	 * ath_tx_tid_cleanup will resume the TID if possible, otherwise
5242eb6f0de0SAdrian Chadd 	 * it'll set the cleanup flag, and it'll be unpaused once
5243eb6f0de0SAdrian Chadd 	 * things have been cleaned up.
5244eb6f0de0SAdrian Chadd 	 */
52454dfd4507SAdrian Chadd 	ath_tx_tid_cleanup(sc, an, tid);
5246eb6f0de0SAdrian Chadd }
5247eb6f0de0SAdrian Chadd 
5248eb6f0de0SAdrian Chadd /*
5249eb6f0de0SAdrian Chadd  * Note: net80211 bar_timeout() doesn't call this function on BAR failure;
5250eb6f0de0SAdrian Chadd  * it simply tears down the aggregation session. Ew.
5251eb6f0de0SAdrian Chadd  *
5252eb6f0de0SAdrian Chadd  * It however will call ieee80211_ampdu_stop() which will call
5253eb6f0de0SAdrian Chadd  * ic->ic_addba_stop().
5254eb6f0de0SAdrian Chadd  *
5255eb6f0de0SAdrian Chadd  * XXX This uses a hard-coded max BAR count value; the whole
5256eb6f0de0SAdrian Chadd  * XXX BAR TX success or failure should be better handled!
5257eb6f0de0SAdrian Chadd  */
5258eb6f0de0SAdrian Chadd void
5259eb6f0de0SAdrian Chadd ath_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
5260eb6f0de0SAdrian Chadd     int status)
5261eb6f0de0SAdrian Chadd {
5262eb6f0de0SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
52632aa563dfSAdrian Chadd 	int tid = tap->txa_tid;
5264eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
5265eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
5266eb6f0de0SAdrian Chadd 	int attempts = tap->txa_attempts;
5267eb6f0de0SAdrian Chadd 
52680e22ed0eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
5269e60c4fc2SAdrian Chadd 	    "%s: called; tap=%p, atid=%p, txa_tid=%d, atid->tid=%d, status=%d, attempts=%d\n",
52700e22ed0eSAdrian Chadd 	    __func__,
5271e60c4fc2SAdrian Chadd 	    tap,
5272e60c4fc2SAdrian Chadd 	    atid,
5273e60c4fc2SAdrian Chadd 	    tap->txa_tid,
5274e60c4fc2SAdrian Chadd 	    atid->tid,
52750e22ed0eSAdrian Chadd 	    status,
52760e22ed0eSAdrian Chadd 	    attempts);
5277eb6f0de0SAdrian Chadd 
5278eb6f0de0SAdrian Chadd 	/* Note: This may update the BAW details */
5279eb6f0de0SAdrian Chadd 	sc->sc_bar_response(ni, tap, status);
5280eb6f0de0SAdrian Chadd 
5281eb6f0de0SAdrian Chadd 	/* Unpause the TID */
5282eb6f0de0SAdrian Chadd 	/*
5283eb6f0de0SAdrian Chadd 	 * XXX if this is attempt=50, the TID will be downgraded
5284eb6f0de0SAdrian Chadd 	 * XXX to a non-aggregate session. So we must unpause the
5285eb6f0de0SAdrian Chadd 	 * XXX TID here or it'll never be done.
5286088d8b81SAdrian Chadd 	 *
5287088d8b81SAdrian Chadd 	 * Also, don't call it if bar_tx/bar_wait are 0; something
5288088d8b81SAdrian Chadd 	 * has beaten us to the punch? (XXX figure out what?)
5289eb6f0de0SAdrian Chadd 	 */
5290eb6f0de0SAdrian Chadd 	if (status == 0 || attempts == 50) {
5291eb6f0de0SAdrian Chadd 		ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
5292088d8b81SAdrian Chadd 		if (atid->bar_tx == 0 || atid->bar_wait == 0)
5293088d8b81SAdrian Chadd 			device_printf(sc->sc_dev,
5294088d8b81SAdrian Chadd 			    "%s: huh? bar_tx=%d, bar_wait=%d\n",
5295088d8b81SAdrian Chadd 			    __func__,
5296088d8b81SAdrian Chadd 			    atid->bar_tx, atid->bar_wait);
5297088d8b81SAdrian Chadd 		else
529888b3d483SAdrian Chadd 			ath_tx_tid_bar_unsuspend(sc, atid);
5299eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
5300eb6f0de0SAdrian Chadd 	}
5301eb6f0de0SAdrian Chadd }
5302eb6f0de0SAdrian Chadd 
5303eb6f0de0SAdrian Chadd /*
5304eb6f0de0SAdrian Chadd  * This is called whenever the pending ADDBA request times out.
5305eb6f0de0SAdrian Chadd  * Unpause and reschedule the TID.
5306eb6f0de0SAdrian Chadd  */
5307eb6f0de0SAdrian Chadd void
5308eb6f0de0SAdrian Chadd ath_addba_response_timeout(struct ieee80211_node *ni,
5309eb6f0de0SAdrian Chadd     struct ieee80211_tx_ampdu *tap)
5310eb6f0de0SAdrian Chadd {
5311eb6f0de0SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
53122aa563dfSAdrian Chadd 	int tid = tap->txa_tid;
5313eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
5314eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
5315eb6f0de0SAdrian Chadd 
5316eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5317eb6f0de0SAdrian Chadd 	    "%s: called; resuming\n", __func__);
5318eb6f0de0SAdrian Chadd 
5319d3a6425bSAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
5320d3a6425bSAdrian Chadd 	atid->addba_tx_pending = 0;
5321d3a6425bSAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
5322d3a6425bSAdrian Chadd 
5323eb6f0de0SAdrian Chadd 	/* Note: This updates the aggregate state to (again) pending */
5324eb6f0de0SAdrian Chadd 	sc->sc_addba_response_timeout(ni, tap);
5325eb6f0de0SAdrian Chadd 
5326eb6f0de0SAdrian Chadd 	/* Unpause the TID; which reschedules it */
5327eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
5328eb6f0de0SAdrian Chadd 	ath_tx_tid_resume(sc, atid);
5329eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
5330eb6f0de0SAdrian Chadd }
53313fdfc330SAdrian Chadd 
53320eb81626SAdrian Chadd /*
53330eb81626SAdrian Chadd  * Check if a node is asleep or not.
53340eb81626SAdrian Chadd  */
5335548a605dSAdrian Chadd int
53360eb81626SAdrian Chadd ath_tx_node_is_asleep(struct ath_softc *sc, struct ath_node *an)
53370eb81626SAdrian Chadd {
53380eb81626SAdrian Chadd 
53390eb81626SAdrian Chadd 	ATH_NODE_LOCK_ASSERT(an);
53400eb81626SAdrian Chadd 
53410eb81626SAdrian Chadd 	return (an->an_is_powersave);
53420eb81626SAdrian Chadd }
53430eb81626SAdrian Chadd 
53440eb81626SAdrian Chadd /*
53450eb81626SAdrian Chadd  * Mark a node as currently "in powersaving."
53460eb81626SAdrian Chadd  * This suspends all traffic on the node.
53470eb81626SAdrian Chadd  *
53480eb81626SAdrian Chadd  * This must be called with the node/tx locks free.
53490eb81626SAdrian Chadd  *
53500eb81626SAdrian Chadd  * XXX TODO: the locking silliness below is due to how the node
53510eb81626SAdrian Chadd  * locking currently works.  Right now, the node lock is grabbed
53520eb81626SAdrian Chadd  * to do rate control lookups and these are done with the TX
53530eb81626SAdrian Chadd  * queue lock held.  This means the node lock can't be grabbed
53540eb81626SAdrian Chadd  * first here or a LOR will occur.
53550eb81626SAdrian Chadd  *
53560eb81626SAdrian Chadd  * Eventually (hopefully!) the TX path code will only grab
53570eb81626SAdrian Chadd  * the TXQ lock when transmitting and the ath_node lock when
53580eb81626SAdrian Chadd  * doing node/TID operations.  There are other complications -
53590eb81626SAdrian Chadd  * the sched/unsched operations involve walking the per-txq
53600eb81626SAdrian Chadd  * 'active tid' list and this requires both locks to be held.
53610eb81626SAdrian Chadd  */
53620eb81626SAdrian Chadd void
53630eb81626SAdrian Chadd ath_tx_node_sleep(struct ath_softc *sc, struct ath_node *an)
53640eb81626SAdrian Chadd {
53650eb81626SAdrian Chadd 	struct ath_tid *atid;
53660eb81626SAdrian Chadd 	struct ath_txq *txq;
53670eb81626SAdrian Chadd 	int tid;
53680eb81626SAdrian Chadd 
53690eb81626SAdrian Chadd 	ATH_NODE_UNLOCK_ASSERT(an);
53700eb81626SAdrian Chadd 
53710eb81626SAdrian Chadd 	/*
53720eb81626SAdrian Chadd 	 * It's possible that a parallel call to ath_tx_node_wakeup()
53730eb81626SAdrian Chadd 	 * will unpause these queues.
53740eb81626SAdrian Chadd 	 *
53750eb81626SAdrian Chadd 	 * The node lock can't just be grabbed here, as there's places
53760eb81626SAdrian Chadd 	 * in the driver where the node lock is grabbed _within_ a
53770eb81626SAdrian Chadd 	 * TXQ lock.
53780eb81626SAdrian Chadd 	 * So, we do this delicately and unwind state if needed.
53790eb81626SAdrian Chadd 	 *
53800eb81626SAdrian Chadd 	 * + Pause all the queues
53810eb81626SAdrian Chadd 	 * + Grab the node lock
53820eb81626SAdrian Chadd 	 * + If the queue is already asleep, unpause and quit
53830eb81626SAdrian Chadd 	 * + else just mark as asleep.
53840eb81626SAdrian Chadd 	 *
53850eb81626SAdrian Chadd 	 * A parallel sleep() call will just pause and then
53860eb81626SAdrian Chadd 	 * find they're already paused, so undo it.
53870eb81626SAdrian Chadd 	 *
53880eb81626SAdrian Chadd 	 * A parallel wakeup() call will check if asleep is 1
53890eb81626SAdrian Chadd 	 * and if it's not (ie, it's 0), it'll treat it as already
53900eb81626SAdrian Chadd 	 * being awake. If it's 1, it'll mark it as 0 and then
53910eb81626SAdrian Chadd 	 * unpause everything.
53920eb81626SAdrian Chadd 	 *
53930eb81626SAdrian Chadd 	 * (Talk about a delicate hack.)
53940eb81626SAdrian Chadd 	 */
53950eb81626SAdrian Chadd 
53960eb81626SAdrian Chadd 	/* Suspend all traffic on the node */
53970eb81626SAdrian Chadd 	for (tid = 0; tid < IEEE80211_TID_SIZE; tid++) {
53980eb81626SAdrian Chadd 		atid = &an->an_tid[tid];
53990eb81626SAdrian Chadd 		txq = sc->sc_ac2q[atid->ac];
54000eb81626SAdrian Chadd 
54010eb81626SAdrian Chadd 		ATH_TXQ_LOCK(txq);
54020eb81626SAdrian Chadd 		ath_tx_tid_pause(sc, atid);
54030eb81626SAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
54040eb81626SAdrian Chadd 	}
54050eb81626SAdrian Chadd 
54060eb81626SAdrian Chadd 	ATH_NODE_LOCK(an);
54070eb81626SAdrian Chadd 
54080eb81626SAdrian Chadd 	/* In case of concurrency races from net80211.. */
54090eb81626SAdrian Chadd 	if (an->an_is_powersave == 1) {
54100eb81626SAdrian Chadd 		ATH_NODE_UNLOCK(an);
54110eb81626SAdrian Chadd 		device_printf(sc->sc_dev,
54120eb81626SAdrian Chadd 		    "%s: an=%p: node was already asleep\n",
54130eb81626SAdrian Chadd 		    __func__, an);
54140eb81626SAdrian Chadd 		for (tid = 0; tid < IEEE80211_TID_SIZE; tid++) {
54150eb81626SAdrian Chadd 			atid = &an->an_tid[tid];
54160eb81626SAdrian Chadd 			txq = sc->sc_ac2q[atid->ac];
54170eb81626SAdrian Chadd 
54180eb81626SAdrian Chadd 			ATH_TXQ_LOCK(txq);
54190eb81626SAdrian Chadd 			ath_tx_tid_resume(sc, atid);
54200eb81626SAdrian Chadd 			ATH_TXQ_UNLOCK(txq);
54210eb81626SAdrian Chadd 		}
54220eb81626SAdrian Chadd 		return;
54230eb81626SAdrian Chadd 	}
54240eb81626SAdrian Chadd 
54250eb81626SAdrian Chadd 	/* Mark node as in powersaving */
54260eb81626SAdrian Chadd 	an->an_is_powersave = 1;
54270eb81626SAdrian Chadd 
54280eb81626SAdrian Chadd 	ATH_NODE_UNLOCK(an);
54290eb81626SAdrian Chadd }
54300eb81626SAdrian Chadd 
54310eb81626SAdrian Chadd /*
54320eb81626SAdrian Chadd  * Mark a node as currently "awake."
54330eb81626SAdrian Chadd  * This resumes all traffic to the node.
54340eb81626SAdrian Chadd  */
54350eb81626SAdrian Chadd void
54360eb81626SAdrian Chadd ath_tx_node_wakeup(struct ath_softc *sc, struct ath_node *an)
54370eb81626SAdrian Chadd {
54380eb81626SAdrian Chadd 	struct ath_tid *atid;
54390eb81626SAdrian Chadd 	struct ath_txq *txq;
54400eb81626SAdrian Chadd 	int tid;
54410eb81626SAdrian Chadd 
54420eb81626SAdrian Chadd 	ATH_NODE_UNLOCK_ASSERT(an);
54430eb81626SAdrian Chadd 	ATH_NODE_LOCK(an);
54440eb81626SAdrian Chadd 
54450eb81626SAdrian Chadd 	/* In case of concurrency races from net80211.. */
54460eb81626SAdrian Chadd 	if (an->an_is_powersave == 0) {
54470eb81626SAdrian Chadd 		ATH_NODE_UNLOCK(an);
54480eb81626SAdrian Chadd 		device_printf(sc->sc_dev,
54490eb81626SAdrian Chadd 		    "%s: an=%p: node was already awake\n",
54500eb81626SAdrian Chadd 		    __func__, an);
54510eb81626SAdrian Chadd 		return;
54520eb81626SAdrian Chadd 	}
54530eb81626SAdrian Chadd 
54540eb81626SAdrian Chadd 	/* Mark node as awake */
54550eb81626SAdrian Chadd 	an->an_is_powersave = 0;
54560eb81626SAdrian Chadd 
54570eb81626SAdrian Chadd 	ATH_NODE_UNLOCK(an);
54580eb81626SAdrian Chadd 
54590eb81626SAdrian Chadd 	for (tid = 0; tid < IEEE80211_TID_SIZE; tid++) {
54600eb81626SAdrian Chadd 		atid = &an->an_tid[tid];
54610eb81626SAdrian Chadd 		txq = sc->sc_ac2q[atid->ac];
54620eb81626SAdrian Chadd 
54630eb81626SAdrian Chadd 		ATH_TXQ_LOCK(txq);
54640eb81626SAdrian Chadd 		ath_tx_tid_resume(sc, atid);
54650eb81626SAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
54660eb81626SAdrian Chadd 	}
54670eb81626SAdrian Chadd }
54680eb81626SAdrian Chadd 
54693fdfc330SAdrian Chadd static int
54703fdfc330SAdrian Chadd ath_legacy_dma_txsetup(struct ath_softc *sc)
54713fdfc330SAdrian Chadd {
54723fdfc330SAdrian Chadd 
54733fdfc330SAdrian Chadd 	/* nothing new needed */
54743fdfc330SAdrian Chadd 	return (0);
54753fdfc330SAdrian Chadd }
54763fdfc330SAdrian Chadd 
54773fdfc330SAdrian Chadd static int
54783fdfc330SAdrian Chadd ath_legacy_dma_txteardown(struct ath_softc *sc)
54793fdfc330SAdrian Chadd {
54803fdfc330SAdrian Chadd 
54813fdfc330SAdrian Chadd 	/* nothing new needed */
54823fdfc330SAdrian Chadd 	return (0);
54833fdfc330SAdrian Chadd }
54843fdfc330SAdrian Chadd 
54853fdfc330SAdrian Chadd void
54863fdfc330SAdrian Chadd ath_xmit_setup_legacy(struct ath_softc *sc)
54873fdfc330SAdrian Chadd {
54881006fc0cSAdrian Chadd 	/*
54891006fc0cSAdrian Chadd 	 * For now, just set the descriptor length to sizeof(ath_desc);
54901006fc0cSAdrian Chadd 	 * worry about extracting the real length out of the HAL later.
54911006fc0cSAdrian Chadd 	 */
54921006fc0cSAdrian Chadd 	sc->sc_tx_desclen = sizeof(struct ath_desc);
54931006fc0cSAdrian Chadd 	sc->sc_tx_statuslen = 0;
54941006fc0cSAdrian Chadd 	sc->sc_tx_nmaps = 1;	/* only one buffer per TX desc */
54953fdfc330SAdrian Chadd 
54963fdfc330SAdrian Chadd 	sc->sc_tx.xmit_setup = ath_legacy_dma_txsetup;
54973fdfc330SAdrian Chadd 	sc->sc_tx.xmit_teardown = ath_legacy_dma_txteardown;
5498f8418db5SAdrian Chadd 	sc->sc_tx.xmit_attach_comp_func = ath_legacy_attach_comp_func;
5499746bab5bSAdrian Chadd 
5500746bab5bSAdrian Chadd 	sc->sc_tx.xmit_dma_restart = ath_legacy_tx_dma_restart;
5501746bab5bSAdrian Chadd 	sc->sc_tx.xmit_handoff = ath_legacy_xmit_handoff;
5502788e6aa9SAdrian Chadd 
5503788e6aa9SAdrian Chadd 	sc->sc_tx.xmit_drain = ath_legacy_tx_drain;
55043fdfc330SAdrian Chadd }
5505