xref: /freebsd/sys/dev/ath/if_ath_tx.c (revision c6e9cee205d6c2279524b82d54dc7a292d3eac36)
1b8e788a5SAdrian Chadd /*-
2b8e788a5SAdrian Chadd  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3*c6e9cee2SAdrian Chadd  * Copyright (c) 2010-2012 Adrian Chadd, Xenion Pty Ltd
4b8e788a5SAdrian Chadd  * All rights reserved.
5b8e788a5SAdrian Chadd  *
6b8e788a5SAdrian Chadd  * Redistribution and use in source and binary forms, with or without
7b8e788a5SAdrian Chadd  * modification, are permitted provided that the following conditions
8b8e788a5SAdrian Chadd  * are met:
9b8e788a5SAdrian Chadd  * 1. Redistributions of source code must retain the above copyright
10b8e788a5SAdrian Chadd  *    notice, this list of conditions and the following disclaimer,
11b8e788a5SAdrian Chadd  *    without modification.
12b8e788a5SAdrian Chadd  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13b8e788a5SAdrian Chadd  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
14b8e788a5SAdrian Chadd  *    redistribution must be conditioned upon including a substantially
15b8e788a5SAdrian Chadd  *    similar Disclaimer requirement for further binary redistribution.
16b8e788a5SAdrian Chadd  *
17b8e788a5SAdrian Chadd  * NO WARRANTY
18b8e788a5SAdrian Chadd  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19b8e788a5SAdrian Chadd  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20b8e788a5SAdrian Chadd  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
21b8e788a5SAdrian Chadd  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22b8e788a5SAdrian Chadd  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
23b8e788a5SAdrian Chadd  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24b8e788a5SAdrian Chadd  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25b8e788a5SAdrian Chadd  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
26b8e788a5SAdrian Chadd  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27b8e788a5SAdrian Chadd  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28b8e788a5SAdrian Chadd  * THE POSSIBILITY OF SUCH DAMAGES.
29b8e788a5SAdrian Chadd  */
30b8e788a5SAdrian Chadd 
31b8e788a5SAdrian Chadd #include <sys/cdefs.h>
32b8e788a5SAdrian Chadd __FBSDID("$FreeBSD$");
33b8e788a5SAdrian Chadd 
34b8e788a5SAdrian Chadd /*
35b8e788a5SAdrian Chadd  * Driver for the Atheros Wireless LAN controller.
36b8e788a5SAdrian Chadd  *
37b8e788a5SAdrian Chadd  * This software is derived from work of Atsushi Onoe; his contribution
38b8e788a5SAdrian Chadd  * is greatly appreciated.
39b8e788a5SAdrian Chadd  */
40b8e788a5SAdrian Chadd 
41b8e788a5SAdrian Chadd #include "opt_inet.h"
42b8e788a5SAdrian Chadd #include "opt_ath.h"
43b8e788a5SAdrian Chadd #include "opt_wlan.h"
44b8e788a5SAdrian Chadd 
45b8e788a5SAdrian Chadd #include <sys/param.h>
46b8e788a5SAdrian Chadd #include <sys/systm.h>
47b8e788a5SAdrian Chadd #include <sys/sysctl.h>
48b8e788a5SAdrian Chadd #include <sys/mbuf.h>
49b8e788a5SAdrian Chadd #include <sys/malloc.h>
50b8e788a5SAdrian Chadd #include <sys/lock.h>
51b8e788a5SAdrian Chadd #include <sys/mutex.h>
52b8e788a5SAdrian Chadd #include <sys/kernel.h>
53b8e788a5SAdrian Chadd #include <sys/socket.h>
54b8e788a5SAdrian Chadd #include <sys/sockio.h>
55b8e788a5SAdrian Chadd #include <sys/errno.h>
56b8e788a5SAdrian Chadd #include <sys/callout.h>
57b8e788a5SAdrian Chadd #include <sys/bus.h>
58b8e788a5SAdrian Chadd #include <sys/endian.h>
59b8e788a5SAdrian Chadd #include <sys/kthread.h>
60b8e788a5SAdrian Chadd #include <sys/taskqueue.h>
61b8e788a5SAdrian Chadd #include <sys/priv.h>
62b8e788a5SAdrian Chadd 
63b8e788a5SAdrian Chadd #include <machine/bus.h>
64b8e788a5SAdrian Chadd 
65b8e788a5SAdrian Chadd #include <net/if.h>
66b8e788a5SAdrian Chadd #include <net/if_dl.h>
67b8e788a5SAdrian Chadd #include <net/if_media.h>
68b8e788a5SAdrian Chadd #include <net/if_types.h>
69b8e788a5SAdrian Chadd #include <net/if_arp.h>
70b8e788a5SAdrian Chadd #include <net/ethernet.h>
71b8e788a5SAdrian Chadd #include <net/if_llc.h>
72b8e788a5SAdrian Chadd 
73b8e788a5SAdrian Chadd #include <net80211/ieee80211_var.h>
74b8e788a5SAdrian Chadd #include <net80211/ieee80211_regdomain.h>
75b8e788a5SAdrian Chadd #ifdef IEEE80211_SUPPORT_SUPERG
76b8e788a5SAdrian Chadd #include <net80211/ieee80211_superg.h>
77b8e788a5SAdrian Chadd #endif
78b8e788a5SAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
79b8e788a5SAdrian Chadd #include <net80211/ieee80211_tdma.h>
80b8e788a5SAdrian Chadd #endif
81eb6f0de0SAdrian Chadd #include <net80211/ieee80211_ht.h>
82b8e788a5SAdrian Chadd 
83b8e788a5SAdrian Chadd #include <net/bpf.h>
84b8e788a5SAdrian Chadd 
85b8e788a5SAdrian Chadd #ifdef INET
86b8e788a5SAdrian Chadd #include <netinet/in.h>
87b8e788a5SAdrian Chadd #include <netinet/if_ether.h>
88b8e788a5SAdrian Chadd #endif
89b8e788a5SAdrian Chadd 
90b8e788a5SAdrian Chadd #include <dev/ath/if_athvar.h>
91b8e788a5SAdrian Chadd #include <dev/ath/ath_hal/ah_devid.h>		/* XXX for softled */
92b8e788a5SAdrian Chadd #include <dev/ath/ath_hal/ah_diagcodes.h>
93b8e788a5SAdrian Chadd 
94b8e788a5SAdrian Chadd #include <dev/ath/if_ath_debug.h>
95b8e788a5SAdrian Chadd 
96b8e788a5SAdrian Chadd #ifdef ATH_TX99_DIAG
97b8e788a5SAdrian Chadd #include <dev/ath/ath_tx99/ath_tx99.h>
98b8e788a5SAdrian Chadd #endif
99b8e788a5SAdrian Chadd 
100b8e788a5SAdrian Chadd #include <dev/ath/if_ath_misc.h>
101b8e788a5SAdrian Chadd #include <dev/ath/if_ath_tx.h>
102c1782ce0SAdrian Chadd #include <dev/ath/if_ath_tx_ht.h>
103b8e788a5SAdrian Chadd 
10481a82688SAdrian Chadd /*
105eb6f0de0SAdrian Chadd  * How many retries to perform in software
106eb6f0de0SAdrian Chadd  */
107eb6f0de0SAdrian Chadd #define	SWMAX_RETRIES		10
108eb6f0de0SAdrian Chadd 
109eb6f0de0SAdrian Chadd static int ath_tx_ampdu_pending(struct ath_softc *sc, struct ath_node *an,
110eb6f0de0SAdrian Chadd     int tid);
111eb6f0de0SAdrian Chadd static int ath_tx_ampdu_running(struct ath_softc *sc, struct ath_node *an,
112eb6f0de0SAdrian Chadd     int tid);
113a108d2d6SAdrian Chadd static ieee80211_seq ath_tx_tid_seqno_assign(struct ath_softc *sc,
114a108d2d6SAdrian Chadd     struct ieee80211_node *ni, struct ath_buf *bf, struct mbuf *m0);
115eb6f0de0SAdrian Chadd static int ath_tx_action_frame_override_queue(struct ath_softc *sc,
116eb6f0de0SAdrian Chadd     struct ieee80211_node *ni, struct mbuf *m0, int *tid);
117eb6f0de0SAdrian Chadd 
118eb6f0de0SAdrian Chadd /*
11981a82688SAdrian Chadd  * Whether to use the 11n rate scenario functions or not
12081a82688SAdrian Chadd  */
12181a82688SAdrian Chadd static inline int
12281a82688SAdrian Chadd ath_tx_is_11n(struct ath_softc *sc)
12381a82688SAdrian Chadd {
1244ddf2cc3SAdrian Chadd 	return ((sc->sc_ah->ah_magic == 0x20065416) ||
1254ddf2cc3SAdrian Chadd 		    (sc->sc_ah->ah_magic == 0x19741014));
12681a82688SAdrian Chadd }
12781a82688SAdrian Chadd 
128eb6f0de0SAdrian Chadd /*
129eb6f0de0SAdrian Chadd  * Obtain the current TID from the given frame.
130eb6f0de0SAdrian Chadd  *
131eb6f0de0SAdrian Chadd  * Non-QoS frames need to go into TID 16 (IEEE80211_NONQOS_TID.)
132eb6f0de0SAdrian Chadd  * This has implications for which AC/priority the packet is placed
133eb6f0de0SAdrian Chadd  * in.
134eb6f0de0SAdrian Chadd  */
135eb6f0de0SAdrian Chadd static int
136eb6f0de0SAdrian Chadd ath_tx_gettid(struct ath_softc *sc, const struct mbuf *m0)
137eb6f0de0SAdrian Chadd {
138eb6f0de0SAdrian Chadd 	const struct ieee80211_frame *wh;
139eb6f0de0SAdrian Chadd 	int pri = M_WME_GETAC(m0);
140eb6f0de0SAdrian Chadd 
141eb6f0de0SAdrian Chadd 	wh = mtod(m0, const struct ieee80211_frame *);
142eb6f0de0SAdrian Chadd 	if (! IEEE80211_QOS_HAS_SEQ(wh))
143eb6f0de0SAdrian Chadd 		return IEEE80211_NONQOS_TID;
144eb6f0de0SAdrian Chadd 	else
145eb6f0de0SAdrian Chadd 		return WME_AC_TO_TID(pri);
146eb6f0de0SAdrian Chadd }
147eb6f0de0SAdrian Chadd 
148eb6f0de0SAdrian Chadd /*
149eb6f0de0SAdrian Chadd  * Determine what the correct AC queue for the given frame
150eb6f0de0SAdrian Chadd  * should be.
151eb6f0de0SAdrian Chadd  *
152eb6f0de0SAdrian Chadd  * This code assumes that the TIDs map consistently to
153eb6f0de0SAdrian Chadd  * the underlying hardware (or software) ath_txq.
154eb6f0de0SAdrian Chadd  * Since the sender may try to set an AC which is
155eb6f0de0SAdrian Chadd  * arbitrary, non-QoS TIDs may end up being put on
156eb6f0de0SAdrian Chadd  * completely different ACs. There's no way to put a
157eb6f0de0SAdrian Chadd  * TID into multiple ath_txq's for scheduling, so
158eb6f0de0SAdrian Chadd  * for now we override the AC/TXQ selection and set
159eb6f0de0SAdrian Chadd  * non-QOS TID frames into the BE queue.
160eb6f0de0SAdrian Chadd  *
161eb6f0de0SAdrian Chadd  * This may be completely incorrect - specifically,
162eb6f0de0SAdrian Chadd  * some management frames may end up out of order
163eb6f0de0SAdrian Chadd  * compared to the QoS traffic they're controlling.
164eb6f0de0SAdrian Chadd  * I'll look into this later.
165eb6f0de0SAdrian Chadd  */
166eb6f0de0SAdrian Chadd static int
167eb6f0de0SAdrian Chadd ath_tx_getac(struct ath_softc *sc, const struct mbuf *m0)
168eb6f0de0SAdrian Chadd {
169eb6f0de0SAdrian Chadd 	const struct ieee80211_frame *wh;
170eb6f0de0SAdrian Chadd 	int pri = M_WME_GETAC(m0);
171eb6f0de0SAdrian Chadd 	wh = mtod(m0, const struct ieee80211_frame *);
172eb6f0de0SAdrian Chadd 	if (IEEE80211_QOS_HAS_SEQ(wh))
173eb6f0de0SAdrian Chadd 		return pri;
174eb6f0de0SAdrian Chadd 
175eb6f0de0SAdrian Chadd 	return WME_AC_BE;
176eb6f0de0SAdrian Chadd }
177eb6f0de0SAdrian Chadd 
178b8e788a5SAdrian Chadd void
179b8e788a5SAdrian Chadd ath_txfrag_cleanup(struct ath_softc *sc,
180b8e788a5SAdrian Chadd 	ath_bufhead *frags, struct ieee80211_node *ni)
181b8e788a5SAdrian Chadd {
182b8e788a5SAdrian Chadd 	struct ath_buf *bf, *next;
183b8e788a5SAdrian Chadd 
184b8e788a5SAdrian Chadd 	ATH_TXBUF_LOCK_ASSERT(sc);
185b8e788a5SAdrian Chadd 
1866b349e5aSAdrian Chadd 	TAILQ_FOREACH_SAFE(bf, frags, bf_list, next) {
187b8e788a5SAdrian Chadd 		/* NB: bf assumed clean */
1886b349e5aSAdrian Chadd 		TAILQ_REMOVE(frags, bf, bf_list);
189e1a50456SAdrian Chadd 		ath_returnbuf_head(sc, bf);
190b8e788a5SAdrian Chadd 		ieee80211_node_decref(ni);
191b8e788a5SAdrian Chadd 	}
192b8e788a5SAdrian Chadd }
193b8e788a5SAdrian Chadd 
194b8e788a5SAdrian Chadd /*
195b8e788a5SAdrian Chadd  * Setup xmit of a fragmented frame.  Allocate a buffer
196b8e788a5SAdrian Chadd  * for each frag and bump the node reference count to
197b8e788a5SAdrian Chadd  * reflect the held reference to be setup by ath_tx_start.
198b8e788a5SAdrian Chadd  */
199b8e788a5SAdrian Chadd int
200b8e788a5SAdrian Chadd ath_txfrag_setup(struct ath_softc *sc, ath_bufhead *frags,
201b8e788a5SAdrian Chadd 	struct mbuf *m0, struct ieee80211_node *ni)
202b8e788a5SAdrian Chadd {
203b8e788a5SAdrian Chadd 	struct mbuf *m;
204b8e788a5SAdrian Chadd 	struct ath_buf *bf;
205b8e788a5SAdrian Chadd 
206b8e788a5SAdrian Chadd 	ATH_TXBUF_LOCK(sc);
207b8e788a5SAdrian Chadd 	for (m = m0->m_nextpkt; m != NULL; m = m->m_nextpkt) {
208af33d486SAdrian Chadd 		/* XXX non-management? */
209af33d486SAdrian Chadd 		bf = _ath_getbuf_locked(sc, ATH_BUFTYPE_NORMAL);
210b8e788a5SAdrian Chadd 		if (bf == NULL) {	/* out of buffers, cleanup */
211b43facbfSAdrian Chadd 			device_printf(sc->sc_dev, "%s: no buffer?\n",
212b43facbfSAdrian Chadd 			    __func__);
213b8e788a5SAdrian Chadd 			ath_txfrag_cleanup(sc, frags, ni);
214b8e788a5SAdrian Chadd 			break;
215b8e788a5SAdrian Chadd 		}
216b8e788a5SAdrian Chadd 		ieee80211_node_incref(ni);
2176b349e5aSAdrian Chadd 		TAILQ_INSERT_TAIL(frags, bf, bf_list);
218b8e788a5SAdrian Chadd 	}
219b8e788a5SAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
220b8e788a5SAdrian Chadd 
2216b349e5aSAdrian Chadd 	return !TAILQ_EMPTY(frags);
222b8e788a5SAdrian Chadd }
223b8e788a5SAdrian Chadd 
224b8e788a5SAdrian Chadd /*
225b8e788a5SAdrian Chadd  * Reclaim mbuf resources.  For fragmented frames we
226b8e788a5SAdrian Chadd  * need to claim each frag chained with m_nextpkt.
227b8e788a5SAdrian Chadd  */
228b8e788a5SAdrian Chadd void
229b8e788a5SAdrian Chadd ath_freetx(struct mbuf *m)
230b8e788a5SAdrian Chadd {
231b8e788a5SAdrian Chadd 	struct mbuf *next;
232b8e788a5SAdrian Chadd 
233b8e788a5SAdrian Chadd 	do {
234b8e788a5SAdrian Chadd 		next = m->m_nextpkt;
235b8e788a5SAdrian Chadd 		m->m_nextpkt = NULL;
236b8e788a5SAdrian Chadd 		m_freem(m);
237b8e788a5SAdrian Chadd 	} while ((m = next) != NULL);
238b8e788a5SAdrian Chadd }
239b8e788a5SAdrian Chadd 
240b8e788a5SAdrian Chadd static int
241b8e788a5SAdrian Chadd ath_tx_dmasetup(struct ath_softc *sc, struct ath_buf *bf, struct mbuf *m0)
242b8e788a5SAdrian Chadd {
243b8e788a5SAdrian Chadd 	struct mbuf *m;
244b8e788a5SAdrian Chadd 	int error;
245b8e788a5SAdrian Chadd 
246b8e788a5SAdrian Chadd 	/*
247b8e788a5SAdrian Chadd 	 * Load the DMA map so any coalescing is done.  This
248b8e788a5SAdrian Chadd 	 * also calculates the number of descriptors we need.
249b8e788a5SAdrian Chadd 	 */
250b8e788a5SAdrian Chadd 	error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0,
251b8e788a5SAdrian Chadd 				     bf->bf_segs, &bf->bf_nseg,
252b8e788a5SAdrian Chadd 				     BUS_DMA_NOWAIT);
253b8e788a5SAdrian Chadd 	if (error == EFBIG) {
254b8e788a5SAdrian Chadd 		/* XXX packet requires too many descriptors */
255b8e788a5SAdrian Chadd 		bf->bf_nseg = ATH_TXDESC+1;
256b8e788a5SAdrian Chadd 	} else if (error != 0) {
257b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_busdma++;
258b8e788a5SAdrian Chadd 		ath_freetx(m0);
259b8e788a5SAdrian Chadd 		return error;
260b8e788a5SAdrian Chadd 	}
261b8e788a5SAdrian Chadd 	/*
262b8e788a5SAdrian Chadd 	 * Discard null packets and check for packets that
263b8e788a5SAdrian Chadd 	 * require too many TX descriptors.  We try to convert
264b8e788a5SAdrian Chadd 	 * the latter to a cluster.
265b8e788a5SAdrian Chadd 	 */
266b8e788a5SAdrian Chadd 	if (bf->bf_nseg > ATH_TXDESC) {		/* too many desc's, linearize */
267b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_linear++;
268b8e788a5SAdrian Chadd 		m = m_collapse(m0, M_DONTWAIT, ATH_TXDESC);
269b8e788a5SAdrian Chadd 		if (m == NULL) {
270b8e788a5SAdrian Chadd 			ath_freetx(m0);
271b8e788a5SAdrian Chadd 			sc->sc_stats.ast_tx_nombuf++;
272b8e788a5SAdrian Chadd 			return ENOMEM;
273b8e788a5SAdrian Chadd 		}
274b8e788a5SAdrian Chadd 		m0 = m;
275b8e788a5SAdrian Chadd 		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0,
276b8e788a5SAdrian Chadd 					     bf->bf_segs, &bf->bf_nseg,
277b8e788a5SAdrian Chadd 					     BUS_DMA_NOWAIT);
278b8e788a5SAdrian Chadd 		if (error != 0) {
279b8e788a5SAdrian Chadd 			sc->sc_stats.ast_tx_busdma++;
280b8e788a5SAdrian Chadd 			ath_freetx(m0);
281b8e788a5SAdrian Chadd 			return error;
282b8e788a5SAdrian Chadd 		}
283b8e788a5SAdrian Chadd 		KASSERT(bf->bf_nseg <= ATH_TXDESC,
284b8e788a5SAdrian Chadd 		    ("too many segments after defrag; nseg %u", bf->bf_nseg));
285b8e788a5SAdrian Chadd 	} else if (bf->bf_nseg == 0) {		/* null packet, discard */
286b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_nodata++;
287b8e788a5SAdrian Chadd 		ath_freetx(m0);
288b8e788a5SAdrian Chadd 		return EIO;
289b8e788a5SAdrian Chadd 	}
290b8e788a5SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_XMIT, "%s: m %p len %u\n",
291b8e788a5SAdrian Chadd 		__func__, m0, m0->m_pkthdr.len);
292b8e788a5SAdrian Chadd 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
293b8e788a5SAdrian Chadd 	bf->bf_m = m0;
294b8e788a5SAdrian Chadd 
295b8e788a5SAdrian Chadd 	return 0;
296b8e788a5SAdrian Chadd }
297b8e788a5SAdrian Chadd 
2986edf1dc7SAdrian Chadd /*
2996edf1dc7SAdrian Chadd  * Chain together segments+descriptors for a non-11n frame.
3006edf1dc7SAdrian Chadd  */
301b8e788a5SAdrian Chadd static void
302eb6f0de0SAdrian Chadd ath_tx_chaindesclist(struct ath_softc *sc, struct ath_buf *bf)
303b8e788a5SAdrian Chadd {
304b8e788a5SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
30542083b3dSAdrian Chadd 	char *ds, *ds0;
3062b200bb4SAdrian Chadd 	int i, bp, dsp;
30746634305SAdrian Chadd 	HAL_DMA_ADDR bufAddrList[4];
30846634305SAdrian Chadd 	uint32_t segLenList[4];
3092b200bb4SAdrian Chadd 	int numTxMaps = 1;
310e2137b86SAdrian Chadd 	int isFirstDesc = 1;
31179b52356SAdrian Chadd 	int qnum;
31246634305SAdrian Chadd 
3133d9b1596SAdrian Chadd 	/*
3143d9b1596SAdrian Chadd 	 * XXX There's txdma and txdma_mgmt; the descriptor
3153d9b1596SAdrian Chadd 	 * sizes must match.
3163d9b1596SAdrian Chadd 	 */
3173d9b1596SAdrian Chadd 	struct ath_descdma *dd = &sc->sc_txdma;
318b8e788a5SAdrian Chadd 
319b8e788a5SAdrian Chadd 	/*
320b8e788a5SAdrian Chadd 	 * Fillin the remainder of the descriptor info.
321b8e788a5SAdrian Chadd 	 */
32246634305SAdrian Chadd 
3232b200bb4SAdrian Chadd 	/*
3242b200bb4SAdrian Chadd 	 * For now the HAL doesn't implement halNumTxMaps for non-EDMA
3252b200bb4SAdrian Chadd 	 * (ie it's 0.)  So just work around it.
3262b200bb4SAdrian Chadd 	 *
3272b200bb4SAdrian Chadd 	 * XXX TODO: populate halNumTxMaps for each HAL chip and
3282b200bb4SAdrian Chadd 	 * then undo this hack.
3292b200bb4SAdrian Chadd 	 */
3302b200bb4SAdrian Chadd 	if (sc->sc_ah->ah_magic == 0x19741014)
3312b200bb4SAdrian Chadd 		numTxMaps = 4;
3322b200bb4SAdrian Chadd 
3332b200bb4SAdrian Chadd 	/*
3342b200bb4SAdrian Chadd 	 * For EDMA and later chips ensure the TX map is fully populated
3352b200bb4SAdrian Chadd 	 * before advancing to the next descriptor.
3362b200bb4SAdrian Chadd 	 */
33742083b3dSAdrian Chadd 	ds0 = ds = (char *) bf->bf_desc;
3382b200bb4SAdrian Chadd 	bp = dsp = 0;
3392b200bb4SAdrian Chadd 	bzero(bufAddrList, sizeof(bufAddrList));
3402b200bb4SAdrian Chadd 	bzero(segLenList, sizeof(segLenList));
3412b200bb4SAdrian Chadd 	for (i = 0; i < bf->bf_nseg; i++) {
3422b200bb4SAdrian Chadd 		bufAddrList[bp] = bf->bf_segs[i].ds_addr;
3432b200bb4SAdrian Chadd 		segLenList[bp] = bf->bf_segs[i].ds_len;
3442b200bb4SAdrian Chadd 		bp++;
3452b200bb4SAdrian Chadd 
3462b200bb4SAdrian Chadd 		/*
3472b200bb4SAdrian Chadd 		 * Go to the next segment if this isn't the last segment
3482b200bb4SAdrian Chadd 		 * and there's space in the current TX map.
3492b200bb4SAdrian Chadd 		 */
3502b200bb4SAdrian Chadd 		if ((i != bf->bf_nseg - 1) && (bp < numTxMaps))
3512b200bb4SAdrian Chadd 			continue;
3522b200bb4SAdrian Chadd 
3532b200bb4SAdrian Chadd 		/*
3542b200bb4SAdrian Chadd 		 * Last segment or we're out of buffer pointers.
3552b200bb4SAdrian Chadd 		 */
3562b200bb4SAdrian Chadd 		bp = 0;
35746634305SAdrian Chadd 
358b8e788a5SAdrian Chadd 		if (i == bf->bf_nseg - 1)
35942083b3dSAdrian Chadd 			ath_hal_settxdesclink(ah, (struct ath_desc *) ds, 0);
360b8e788a5SAdrian Chadd 		else
36142083b3dSAdrian Chadd 			ath_hal_settxdesclink(ah, (struct ath_desc *) ds,
3622b200bb4SAdrian Chadd 			    bf->bf_daddr + dd->dd_descsize * (dsp + 1));
36346634305SAdrian Chadd 
36446634305SAdrian Chadd 		/*
36546634305SAdrian Chadd 		 * XXX this assumes that bfs_txq is the actual destination
36646634305SAdrian Chadd 		 * hardware queue at this point.  It may not have been assigned,
36746634305SAdrian Chadd 		 * it may actually be pointing to the multicast software
36846634305SAdrian Chadd 		 * TXQ id.  These must be fixed!
36946634305SAdrian Chadd 		 */
37079b52356SAdrian Chadd 		qnum = bf->bf_state.bfs_txq->axq_qnum;
37179b52356SAdrian Chadd 
37242083b3dSAdrian Chadd 		ath_hal_filltxdesc(ah, (struct ath_desc *) ds
37346634305SAdrian Chadd 			, bufAddrList
37446634305SAdrian Chadd 			, segLenList
3752b200bb4SAdrian Chadd 			, bf->bf_descid		/* XXX desc id */
37679b52356SAdrian Chadd 			, qnum
377e2137b86SAdrian Chadd 			, isFirstDesc		/* first segment */
378b8e788a5SAdrian Chadd 			, i == bf->bf_nseg - 1	/* last segment */
37942083b3dSAdrian Chadd 			, (struct ath_desc *) ds0	/* first descriptor */
380b8e788a5SAdrian Chadd 		);
38121840808SAdrian Chadd 
38221840808SAdrian Chadd 		/* Make sure the 11n aggregate fields are cleared */
38321840808SAdrian Chadd 		if (ath_tx_is_11n(sc))
3845d9b19f7SAdrian Chadd 			ath_hal_clr11n_aggr(sc->sc_ah, (struct ath_desc *) ds);
38521840808SAdrian Chadd 
386e2137b86SAdrian Chadd 		isFirstDesc = 0;
3870f8423a2SAdrian Chadd #ifdef	ATH_DEBUG
38842083b3dSAdrian Chadd 		if (sc->sc_debug & ATH_DEBUG_XMIT)
38942083b3dSAdrian Chadd 			ath_printtxbuf(sc, bf, qnum, 0, 0);
3900f8423a2SAdrian Chadd #endif
39142083b3dSAdrian Chadd 		bf->bf_lastds = (struct ath_desc *) ds;
3922b200bb4SAdrian Chadd 
3932b200bb4SAdrian Chadd 		/*
3942b200bb4SAdrian Chadd 		 * Don't forget to skip to the next descriptor.
3952b200bb4SAdrian Chadd 		 */
39642083b3dSAdrian Chadd 		ds += sc->sc_tx_desclen;
3972b200bb4SAdrian Chadd 		dsp++;
3982b200bb4SAdrian Chadd 
3992b200bb4SAdrian Chadd 		/*
4002b200bb4SAdrian Chadd 		 * .. and don't forget to blank these out!
4012b200bb4SAdrian Chadd 		 */
4022b200bb4SAdrian Chadd 		bzero(bufAddrList, sizeof(bufAddrList));
4032b200bb4SAdrian Chadd 		bzero(segLenList, sizeof(segLenList));
404b8e788a5SAdrian Chadd 	}
4054d7f8837SAdrian Chadd 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
40681a82688SAdrian Chadd }
40781a82688SAdrian Chadd 
408eb6f0de0SAdrian Chadd /*
409eb6f0de0SAdrian Chadd  * Fill in the descriptor list for a aggregate subframe.
410eb6f0de0SAdrian Chadd  *
411eb6f0de0SAdrian Chadd  * The subframe is returned with the ds_link field in the last subframe
412eb6f0de0SAdrian Chadd  * pointing to 0.
413eb6f0de0SAdrian Chadd  */
41481a82688SAdrian Chadd static void
415eb6f0de0SAdrian Chadd ath_tx_chaindesclist_subframe(struct ath_softc *sc, struct ath_buf *bf)
41681a82688SAdrian Chadd {
41781a82688SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
418eb6f0de0SAdrian Chadd 	struct ath_desc *ds, *ds0;
419eb6f0de0SAdrian Chadd 	int i;
420fffbec86SAdrian Chadd 	HAL_DMA_ADDR bufAddrList[4];
421fffbec86SAdrian Chadd 	uint32_t segLenList[4];
422fffbec86SAdrian Chadd 
4233d9b1596SAdrian Chadd 	/*
4243d9b1596SAdrian Chadd 	 * XXX There's txdma and txdma_mgmt; the descriptor
4253d9b1596SAdrian Chadd 	 * sizes must match.
4263d9b1596SAdrian Chadd 	 */
4273d9b1596SAdrian Chadd 	struct ath_descdma *dd = &sc->sc_txdma;
42881a82688SAdrian Chadd 
429eb6f0de0SAdrian Chadd 	ds0 = ds = bf->bf_desc;
430eb6f0de0SAdrian Chadd 
431eb6f0de0SAdrian Chadd 	/*
432eb6f0de0SAdrian Chadd 	 * There's no need to call ath_hal_setupfirsttxdesc here;
433eb6f0de0SAdrian Chadd 	 * That's only going to occur for the first frame in an aggregate.
434eb6f0de0SAdrian Chadd 	 */
435eb6f0de0SAdrian Chadd 	for (i = 0; i < bf->bf_nseg; i++, ds++) {
436fffbec86SAdrian Chadd 		bzero(bufAddrList, sizeof(bufAddrList));
437fffbec86SAdrian Chadd 		bzero(segLenList, sizeof(segLenList));
438eb6f0de0SAdrian Chadd 		if (i == bf->bf_nseg - 1)
439bb069955SAdrian Chadd 			ath_hal_settxdesclink(ah, ds, 0);
440eb6f0de0SAdrian Chadd 		else
441bb069955SAdrian Chadd 			ath_hal_settxdesclink(ah, ds,
4423d9b1596SAdrian Chadd 			    bf->bf_daddr + dd->dd_descsize * (i + 1));
443eb6f0de0SAdrian Chadd 
444fffbec86SAdrian Chadd 		bufAddrList[0] = bf->bf_segs[i].ds_addr;
445fffbec86SAdrian Chadd 		segLenList[0] = bf->bf_segs[i].ds_len;
446fffbec86SAdrian Chadd 
447eb6f0de0SAdrian Chadd 		/*
448eb6f0de0SAdrian Chadd 		 * This performs the setup for an aggregate frame.
449eb6f0de0SAdrian Chadd 		 * This includes enabling the aggregate flags if needed.
450eb6f0de0SAdrian Chadd 		 */
451eb6f0de0SAdrian Chadd 		ath_hal_chaintxdesc(ah, ds,
452fffbec86SAdrian Chadd 		    bufAddrList,
453fffbec86SAdrian Chadd 		    segLenList,
454eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_pktlen,
455eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_hdrlen,
456eb6f0de0SAdrian Chadd 		    HAL_PKT_TYPE_AMPDU,	/* forces aggregate bits to be set */
457eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_keyix,
458eb6f0de0SAdrian Chadd 		    0,			/* cipher, calculated from keyix */
459eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_ndelim,
460eb6f0de0SAdrian Chadd 		    i == 0,		/* first segment */
46133d34032SAdrian Chadd 		    i == bf->bf_nseg - 1,	/* last segment */
46233d34032SAdrian Chadd 		    bf->bf_next == NULL		/* last sub-frame in aggr */
463eb6f0de0SAdrian Chadd 		);
464eb6f0de0SAdrian Chadd 
465eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_XMIT,
466eb6f0de0SAdrian Chadd 			"%s: %d: %08x %08x %08x %08x %08x %08x\n",
467eb6f0de0SAdrian Chadd 			__func__, i, ds->ds_link, ds->ds_data,
468eb6f0de0SAdrian Chadd 			ds->ds_ctl0, ds->ds_ctl1, ds->ds_hw[0], ds->ds_hw[1]);
469eb6f0de0SAdrian Chadd 		bf->bf_lastds = ds;
4704d7f8837SAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
4714d7f8837SAdrian Chadd 		    BUS_DMASYNC_PREWRITE);
472eb6f0de0SAdrian Chadd 	}
473eb6f0de0SAdrian Chadd }
474eb6f0de0SAdrian Chadd 
475eb6f0de0SAdrian Chadd /*
476d34a7347SAdrian Chadd  * Set the rate control fields in the given descriptor based on
477d34a7347SAdrian Chadd  * the bf_state fields and node state.
478d34a7347SAdrian Chadd  *
479d34a7347SAdrian Chadd  * The bfs fields should already be set with the relevant rate
480d34a7347SAdrian Chadd  * control information, including whether MRR is to be enabled.
481d34a7347SAdrian Chadd  *
482d34a7347SAdrian Chadd  * Since the FreeBSD HAL currently sets up the first TX rate
483d34a7347SAdrian Chadd  * in ath_hal_setuptxdesc(), this will setup the MRR
484d34a7347SAdrian Chadd  * conditionally for the pre-11n chips, and call ath_buf_set_rate
485d34a7347SAdrian Chadd  * unconditionally for 11n chips. These require the 11n rate
486d34a7347SAdrian Chadd  * scenario to be set if MCS rates are enabled, so it's easier
487d34a7347SAdrian Chadd  * to just always call it. The caller can then only set rates 2, 3
488d34a7347SAdrian Chadd  * and 4 if multi-rate retry is needed.
489d34a7347SAdrian Chadd  */
490d34a7347SAdrian Chadd static void
491d34a7347SAdrian Chadd ath_tx_set_ratectrl(struct ath_softc *sc, struct ieee80211_node *ni,
492d34a7347SAdrian Chadd     struct ath_buf *bf)
493d34a7347SAdrian Chadd {
494d34a7347SAdrian Chadd 	struct ath_rc_series *rc = bf->bf_state.bfs_rc;
495d34a7347SAdrian Chadd 
496d34a7347SAdrian Chadd 	/* If mrr is disabled, blank tries 1, 2, 3 */
497d34a7347SAdrian Chadd 	if (! bf->bf_state.bfs_ismrr)
498d34a7347SAdrian Chadd 		rc[1].tries = rc[2].tries = rc[3].tries = 0;
499d34a7347SAdrian Chadd 
500d34a7347SAdrian Chadd 	/*
501d34a7347SAdrian Chadd 	 * Always call - that way a retried descriptor will
502d34a7347SAdrian Chadd 	 * have the MRR fields overwritten.
503d34a7347SAdrian Chadd 	 *
504d34a7347SAdrian Chadd 	 * XXX TODO: see if this is really needed - setting up
505d34a7347SAdrian Chadd 	 * the first descriptor should set the MRR fields to 0
506d34a7347SAdrian Chadd 	 * for us anyway.
507d34a7347SAdrian Chadd 	 */
508d34a7347SAdrian Chadd 	if (ath_tx_is_11n(sc)) {
509d34a7347SAdrian Chadd 		ath_buf_set_rate(sc, ni, bf);
510d34a7347SAdrian Chadd 	} else {
511d34a7347SAdrian Chadd 		ath_hal_setupxtxdesc(sc->sc_ah, bf->bf_desc
512d34a7347SAdrian Chadd 			, rc[1].ratecode, rc[1].tries
513d34a7347SAdrian Chadd 			, rc[2].ratecode, rc[2].tries
514d34a7347SAdrian Chadd 			, rc[3].ratecode, rc[3].tries
515d34a7347SAdrian Chadd 		);
516d34a7347SAdrian Chadd 	}
517d34a7347SAdrian Chadd }
518d34a7347SAdrian Chadd 
519d34a7347SAdrian Chadd /*
520eb6f0de0SAdrian Chadd  * Setup segments+descriptors for an 11n aggregate.
521eb6f0de0SAdrian Chadd  * bf_first is the first buffer in the aggregate.
522eb6f0de0SAdrian Chadd  * The descriptor list must already been linked together using
523eb6f0de0SAdrian Chadd  * bf->bf_next.
524eb6f0de0SAdrian Chadd  */
525eb6f0de0SAdrian Chadd static void
526eb6f0de0SAdrian Chadd ath_tx_setds_11n(struct ath_softc *sc, struct ath_buf *bf_first)
527eb6f0de0SAdrian Chadd {
528eb6f0de0SAdrian Chadd 	struct ath_buf *bf, *bf_prev = NULL;
529eb6f0de0SAdrian Chadd 
530eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: nframes=%d, al=%d\n",
531eb6f0de0SAdrian Chadd 	    __func__, bf_first->bf_state.bfs_nframes,
532eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_al);
533eb6f0de0SAdrian Chadd 
534eb6f0de0SAdrian Chadd 	/*
535eb6f0de0SAdrian Chadd 	 * Setup all descriptors of all subframes.
536eb6f0de0SAdrian Chadd 	 */
537eb6f0de0SAdrian Chadd 	bf = bf_first;
538eb6f0de0SAdrian Chadd 	while (bf != NULL) {
539eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
540eb6f0de0SAdrian Chadd 		    "%s: bf=%p, nseg=%d, pktlen=%d, seqno=%d\n",
541eb6f0de0SAdrian Chadd 		    __func__, bf, bf->bf_nseg, bf->bf_state.bfs_pktlen,
542eb6f0de0SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
543eb6f0de0SAdrian Chadd 
544eb6f0de0SAdrian Chadd 		/* Sub-frame setup */
545eb6f0de0SAdrian Chadd 		ath_tx_chaindesclist_subframe(sc, bf);
546eb6f0de0SAdrian Chadd 
547eb6f0de0SAdrian Chadd 		/*
548eb6f0de0SAdrian Chadd 		 * Link the last descriptor of the previous frame
549eb6f0de0SAdrian Chadd 		 * to the beginning descriptor of this frame.
550eb6f0de0SAdrian Chadd 		 */
551eb6f0de0SAdrian Chadd 		if (bf_prev != NULL)
552bb069955SAdrian Chadd 			ath_hal_settxdesclink(sc->sc_ah, bf_prev->bf_lastds,
553bb069955SAdrian Chadd 			    bf->bf_daddr);
554eb6f0de0SAdrian Chadd 
555eb6f0de0SAdrian Chadd 		/* Save a copy so we can link the next descriptor in */
556eb6f0de0SAdrian Chadd 		bf_prev = bf;
557eb6f0de0SAdrian Chadd 		bf = bf->bf_next;
558eb6f0de0SAdrian Chadd 	}
559eb6f0de0SAdrian Chadd 
560eb6f0de0SAdrian Chadd 	/*
561eb6f0de0SAdrian Chadd 	 * Setup first descriptor of first frame.
562eb6f0de0SAdrian Chadd 	 * chaintxdesc() overwrites the descriptor entries;
563eb6f0de0SAdrian Chadd 	 * setupfirsttxdesc() merges in things.
564eb6f0de0SAdrian Chadd 	 * Otherwise various fields aren't set correctly (eg flags).
565eb6f0de0SAdrian Chadd 	 */
566eb6f0de0SAdrian Chadd 	ath_hal_setupfirsttxdesc(sc->sc_ah,
567eb6f0de0SAdrian Chadd 	    bf_first->bf_desc,
568eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_al,
569875a9451SAdrian Chadd 	    bf_first->bf_state.bfs_txflags | HAL_TXDESC_INTREQ,
570eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_txpower,
571eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_txrate0,
572eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_try0,
573eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_txantenna,
574eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_ctsrate,
575eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_ctsduration);
576eb6f0de0SAdrian Chadd 
577eb6f0de0SAdrian Chadd 	/*
578eb6f0de0SAdrian Chadd 	 * Set the first descriptor bf_lastds field to point to
579eb6f0de0SAdrian Chadd 	 * the last descriptor in the last subframe, that's where
580eb6f0de0SAdrian Chadd 	 * the status update will occur.
581eb6f0de0SAdrian Chadd 	 */
582eb6f0de0SAdrian Chadd 	bf_first->bf_lastds = bf_prev->bf_lastds;
583eb6f0de0SAdrian Chadd 
584eb6f0de0SAdrian Chadd 	/*
585eb6f0de0SAdrian Chadd 	 * And bf_last in the first descriptor points to the end of
586eb6f0de0SAdrian Chadd 	 * the aggregate list.
587eb6f0de0SAdrian Chadd 	 */
588eb6f0de0SAdrian Chadd 	bf_first->bf_last = bf_prev;
589eb6f0de0SAdrian Chadd 
590d34a7347SAdrian Chadd 	/*
591d34a7347SAdrian Chadd 	 * setup first desc with rate and aggr info
592d34a7347SAdrian Chadd 	 */
593d34a7347SAdrian Chadd 	ath_tx_set_ratectrl(sc, bf_first->bf_node, bf_first);
594d34a7347SAdrian Chadd 
5958c08c07aSAdrian Chadd 	/*
5968c08c07aSAdrian Chadd 	 * Setup the last descriptor in the list.
597a6e82959SAdrian Chadd 	 *
598a6e82959SAdrian Chadd 	 * bf_first->bf_lastds already points to it; the rate
599a6e82959SAdrian Chadd 	 * control information needs to be squirreled away here
600a6e82959SAdrian Chadd 	 * as well ans clearing the moreaggr/paddelim fields.
6018c08c07aSAdrian Chadd 	 */
602a6e82959SAdrian Chadd 	ath_hal_setuplasttxdesc(sc->sc_ah, bf_first->bf_lastds,
6038c08c07aSAdrian Chadd 	    bf_first->bf_desc);
6048c08c07aSAdrian Chadd 
605eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: end\n", __func__);
606eb6f0de0SAdrian Chadd }
607eb6f0de0SAdrian Chadd 
60846634305SAdrian Chadd /*
60946634305SAdrian Chadd  * Hand-off a frame to the multicast TX queue.
61046634305SAdrian Chadd  *
61146634305SAdrian Chadd  * This is a software TXQ which will be appended to the CAB queue
61246634305SAdrian Chadd  * during the beacon setup code.
61346634305SAdrian Chadd  *
61446634305SAdrian Chadd  * XXX TODO: since the AR9300 EDMA TX queue support wants the QCU ID
61546634305SAdrian Chadd  * as part of the TX descriptor, bf_state.bfs_txq must be updated
61646634305SAdrian Chadd  * with the actual hardware txq, or all of this will fall apart.
61746634305SAdrian Chadd  *
61846634305SAdrian Chadd  * XXX It may not be a bad idea to just stuff the QCU ID into bf_state
61946634305SAdrian Chadd  * and retire bfs_txq; then make sure the CABQ QCU ID is populated
62046634305SAdrian Chadd  * correctly.
62146634305SAdrian Chadd  */
622eb6f0de0SAdrian Chadd static void
623eb6f0de0SAdrian Chadd ath_tx_handoff_mcast(struct ath_softc *sc, struct ath_txq *txq,
624eb6f0de0SAdrian Chadd     struct ath_buf *bf)
625eb6f0de0SAdrian Chadd {
626eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
627eb6f0de0SAdrian Chadd 	KASSERT((bf->bf_flags & ATH_BUF_BUSY) == 0,
628eb6f0de0SAdrian Chadd 	     ("%s: busy status 0x%x", __func__, bf->bf_flags));
629eb6f0de0SAdrian Chadd 	if (txq->axq_link != NULL) {
630eb6f0de0SAdrian Chadd 		struct ath_buf *last = ATH_TXQ_LAST(txq, axq_q_s);
631eb6f0de0SAdrian Chadd 		struct ieee80211_frame *wh;
632eb6f0de0SAdrian Chadd 
633eb6f0de0SAdrian Chadd 		/* mark previous frame */
634eb6f0de0SAdrian Chadd 		wh = mtod(last->bf_m, struct ieee80211_frame *);
635eb6f0de0SAdrian Chadd 		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
636eb6f0de0SAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, last->bf_dmamap,
637eb6f0de0SAdrian Chadd 		    BUS_DMASYNC_PREWRITE);
638eb6f0de0SAdrian Chadd 
639eb6f0de0SAdrian Chadd 		/* link descriptor */
640eb6f0de0SAdrian Chadd 		*txq->axq_link = bf->bf_daddr;
641eb6f0de0SAdrian Chadd 	}
642eb6f0de0SAdrian Chadd 	ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
643bb069955SAdrian Chadd 	ath_hal_gettxdesclinkptr(sc->sc_ah, bf->bf_lastds, &txq->axq_link);
644eb6f0de0SAdrian Chadd }
645eb6f0de0SAdrian Chadd 
646eb6f0de0SAdrian Chadd /*
647eb6f0de0SAdrian Chadd  * Hand-off packet to a hardware queue.
648eb6f0de0SAdrian Chadd  */
649eb6f0de0SAdrian Chadd static void
650d4365d16SAdrian Chadd ath_tx_handoff_hw(struct ath_softc *sc, struct ath_txq *txq,
651d4365d16SAdrian Chadd     struct ath_buf *bf)
652eb6f0de0SAdrian Chadd {
653eb6f0de0SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
65481a82688SAdrian Chadd 
655b8e788a5SAdrian Chadd 	/*
656b8e788a5SAdrian Chadd 	 * Insert the frame on the outbound list and pass it on
657b8e788a5SAdrian Chadd 	 * to the hardware.  Multicast frames buffered for power
658b8e788a5SAdrian Chadd 	 * save stations and transmit from the CAB queue are stored
659b8e788a5SAdrian Chadd 	 * on a s/w only queue and loaded on to the CAB queue in
660b8e788a5SAdrian Chadd 	 * the SWBA handler since frames only go out on DTIM and
661b8e788a5SAdrian Chadd 	 * to avoid possible races.
662b8e788a5SAdrian Chadd 	 */
663eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
664b8e788a5SAdrian Chadd 	KASSERT((bf->bf_flags & ATH_BUF_BUSY) == 0,
665eb6f0de0SAdrian Chadd 	     ("%s: busy status 0x%x", __func__, bf->bf_flags));
666eb6f0de0SAdrian Chadd 	KASSERT(txq->axq_qnum != ATH_TXQ_SWQ,
667eb6f0de0SAdrian Chadd 	     ("ath_tx_handoff_hw called for mcast queue"));
668eb6f0de0SAdrian Chadd 
669ef27340cSAdrian Chadd #if 0
670ef27340cSAdrian Chadd 	/*
671ef27340cSAdrian Chadd 	 * This causes a LOR. Find out where the PCU lock is being
672ef27340cSAdrian Chadd 	 * held whilst the TXQ lock is grabbed - that shouldn't
673ef27340cSAdrian Chadd 	 * be occuring.
674ef27340cSAdrian Chadd 	 */
675ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
676ef27340cSAdrian Chadd 	if (sc->sc_inreset_cnt) {
677ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
678ef27340cSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_RESET,
679ef27340cSAdrian Chadd 		    "%s: called with sc_in_reset != 0\n",
680ef27340cSAdrian Chadd 		    __func__);
681ef27340cSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_XMIT,
682ef27340cSAdrian Chadd 		    "%s: queued: TXDP[%u] = %p (%p) depth %d\n",
683ef27340cSAdrian Chadd 		    __func__, txq->axq_qnum,
684ef27340cSAdrian Chadd 		    (caddr_t)bf->bf_daddr, bf->bf_desc,
685ef27340cSAdrian Chadd 		    txq->axq_depth);
686ef27340cSAdrian Chadd 		ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
687ef27340cSAdrian Chadd 		if (bf->bf_state.bfs_aggr)
688ef27340cSAdrian Chadd 			txq->axq_aggr_depth++;
689ef27340cSAdrian Chadd 		/*
690ef27340cSAdrian Chadd 		 * There's no need to update axq_link; the hardware
691ef27340cSAdrian Chadd 		 * is in reset and once the reset is complete, any
692ef27340cSAdrian Chadd 		 * non-empty queues will simply have DMA restarted.
693ef27340cSAdrian Chadd 		 */
694ef27340cSAdrian Chadd 		return;
695ef27340cSAdrian Chadd 		}
696ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
697ef27340cSAdrian Chadd #endif
698ef27340cSAdrian Chadd 
699eb6f0de0SAdrian Chadd 	/* For now, so not to generate whitespace diffs */
700eb6f0de0SAdrian Chadd 	if (1) {
701b8e788a5SAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
702b8e788a5SAdrian Chadd 		int qbusy;
703b8e788a5SAdrian Chadd 
704b8e788a5SAdrian Chadd 		ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
705b8e788a5SAdrian Chadd 		qbusy = ath_hal_txqenabled(ah, txq->axq_qnum);
706b8e788a5SAdrian Chadd 		if (txq->axq_link == NULL) {
707b8e788a5SAdrian Chadd 			/*
708b8e788a5SAdrian Chadd 			 * Be careful writing the address to TXDP.  If
709b8e788a5SAdrian Chadd 			 * the tx q is enabled then this write will be
710b8e788a5SAdrian Chadd 			 * ignored.  Normally this is not an issue but
711b8e788a5SAdrian Chadd 			 * when tdma is in use and the q is beacon gated
712b8e788a5SAdrian Chadd 			 * this race can occur.  If the q is busy then
713b8e788a5SAdrian Chadd 			 * defer the work to later--either when another
714b8e788a5SAdrian Chadd 			 * packet comes along or when we prepare a beacon
715b8e788a5SAdrian Chadd 			 * frame at SWBA.
716b8e788a5SAdrian Chadd 			 */
717b8e788a5SAdrian Chadd 			if (!qbusy) {
718d4365d16SAdrian Chadd 				ath_hal_puttxbuf(ah, txq->axq_qnum,
719d4365d16SAdrian Chadd 				    bf->bf_daddr);
720b8e788a5SAdrian Chadd 				txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
721b8e788a5SAdrian Chadd 				DPRINTF(sc, ATH_DEBUG_XMIT,
722b8e788a5SAdrian Chadd 				    "%s: TXDP[%u] = %p (%p) depth %d\n",
723b8e788a5SAdrian Chadd 				    __func__, txq->axq_qnum,
724b8e788a5SAdrian Chadd 				    (caddr_t)bf->bf_daddr, bf->bf_desc,
725b8e788a5SAdrian Chadd 				    txq->axq_depth);
726b8e788a5SAdrian Chadd 			} else {
727b8e788a5SAdrian Chadd 				txq->axq_flags |= ATH_TXQ_PUTPENDING;
728b8e788a5SAdrian Chadd 				DPRINTF(sc, ATH_DEBUG_TDMA | ATH_DEBUG_XMIT,
729b8e788a5SAdrian Chadd 				    "%s: Q%u busy, defer enable\n", __func__,
730b8e788a5SAdrian Chadd 				    txq->axq_qnum);
731b8e788a5SAdrian Chadd 			}
732b8e788a5SAdrian Chadd 		} else {
733b8e788a5SAdrian Chadd 			*txq->axq_link = bf->bf_daddr;
734b8e788a5SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_XMIT,
735b8e788a5SAdrian Chadd 			    "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
736b8e788a5SAdrian Chadd 			    txq->axq_qnum, txq->axq_link,
737d4365d16SAdrian Chadd 			    (caddr_t)bf->bf_daddr, bf->bf_desc,
738d4365d16SAdrian Chadd 			    txq->axq_depth);
739b8e788a5SAdrian Chadd 			if ((txq->axq_flags & ATH_TXQ_PUTPENDING) && !qbusy) {
740b8e788a5SAdrian Chadd 				/*
741b8e788a5SAdrian Chadd 				 * The q was busy when we previously tried
742b8e788a5SAdrian Chadd 				 * to write the address of the first buffer
743b8e788a5SAdrian Chadd 				 * in the chain.  Since it's not busy now
744b8e788a5SAdrian Chadd 				 * handle this chore.  We are certain the
745b8e788a5SAdrian Chadd 				 * buffer at the front is the right one since
746b8e788a5SAdrian Chadd 				 * axq_link is NULL only when the buffer list
747b8e788a5SAdrian Chadd 				 * is/was empty.
748b8e788a5SAdrian Chadd 				 */
749b8e788a5SAdrian Chadd 				ath_hal_puttxbuf(ah, txq->axq_qnum,
7506b349e5aSAdrian Chadd 					TAILQ_FIRST(&txq->axq_q)->bf_daddr);
751b8e788a5SAdrian Chadd 				txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
752b8e788a5SAdrian Chadd 				DPRINTF(sc, ATH_DEBUG_TDMA | ATH_DEBUG_XMIT,
753b8e788a5SAdrian Chadd 				    "%s: Q%u restarted\n", __func__,
754b8e788a5SAdrian Chadd 				    txq->axq_qnum);
755b8e788a5SAdrian Chadd 			}
756b8e788a5SAdrian Chadd 		}
757b8e788a5SAdrian Chadd #else
758b8e788a5SAdrian Chadd 		ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
759b8e788a5SAdrian Chadd 		if (txq->axq_link == NULL) {
760b8e788a5SAdrian Chadd 			ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
761b8e788a5SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_XMIT,
762b8e788a5SAdrian Chadd 			    "%s: TXDP[%u] = %p (%p) depth %d\n",
763b8e788a5SAdrian Chadd 			    __func__, txq->axq_qnum,
764b8e788a5SAdrian Chadd 			    (caddr_t)bf->bf_daddr, bf->bf_desc,
765b8e788a5SAdrian Chadd 			    txq->axq_depth);
766b8e788a5SAdrian Chadd 		} else {
767b8e788a5SAdrian Chadd 			*txq->axq_link = bf->bf_daddr;
768b8e788a5SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_XMIT,
769b8e788a5SAdrian Chadd 			    "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
770b8e788a5SAdrian Chadd 			    txq->axq_qnum, txq->axq_link,
771d4365d16SAdrian Chadd 			    (caddr_t)bf->bf_daddr, bf->bf_desc,
772d4365d16SAdrian Chadd 			    txq->axq_depth);
773b8e788a5SAdrian Chadd 		}
774b8e788a5SAdrian Chadd #endif /* IEEE80211_SUPPORT_TDMA */
7756edf1dc7SAdrian Chadd 		if (bf->bf_state.bfs_aggr)
7766edf1dc7SAdrian Chadd 			txq->axq_aggr_depth++;
777bb069955SAdrian Chadd 		ath_hal_gettxdesclinkptr(ah, bf->bf_lastds, &txq->axq_link);
778b8e788a5SAdrian Chadd 		ath_hal_txstart(ah, txq->axq_qnum);
779b8e788a5SAdrian Chadd 	}
780b8e788a5SAdrian Chadd }
781eb6f0de0SAdrian Chadd 
782eb6f0de0SAdrian Chadd /*
783eb6f0de0SAdrian Chadd  * Restart TX DMA for the given TXQ.
784eb6f0de0SAdrian Chadd  *
785eb6f0de0SAdrian Chadd  * This must be called whether the queue is empty or not.
786eb6f0de0SAdrian Chadd  */
787746bab5bSAdrian Chadd static void
788746bab5bSAdrian Chadd ath_legacy_tx_dma_restart(struct ath_softc *sc, struct ath_txq *txq)
789eb6f0de0SAdrian Chadd {
790eb6f0de0SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
791b1f3262cSAdrian Chadd 	struct ath_buf *bf, *bf_last;
792eb6f0de0SAdrian Chadd 
793eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
794eb6f0de0SAdrian Chadd 
795eb6f0de0SAdrian Chadd 	/* This is always going to be cleared, empty or not */
796eb6f0de0SAdrian Chadd 	txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
797eb6f0de0SAdrian Chadd 
798b1f3262cSAdrian Chadd 	/* XXX make this ATH_TXQ_FIRST */
799eb6f0de0SAdrian Chadd 	bf = TAILQ_FIRST(&txq->axq_q);
800b1f3262cSAdrian Chadd 	bf_last = ATH_TXQ_LAST(txq, axq_q_s);
801b1f3262cSAdrian Chadd 
802eb6f0de0SAdrian Chadd 	if (bf == NULL)
803eb6f0de0SAdrian Chadd 		return;
804eb6f0de0SAdrian Chadd 
805eb6f0de0SAdrian Chadd 	ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
806d2da5544SAdrian Chadd 	ath_hal_gettxdesclinkptr(ah, bf_last->bf_lastds, &txq->axq_link);
807eb6f0de0SAdrian Chadd 	ath_hal_txstart(ah, txq->axq_qnum);
808eb6f0de0SAdrian Chadd }
809eb6f0de0SAdrian Chadd 
810eb6f0de0SAdrian Chadd /*
811eb6f0de0SAdrian Chadd  * Hand off a packet to the hardware (or mcast queue.)
812eb6f0de0SAdrian Chadd  *
813eb6f0de0SAdrian Chadd  * The relevant hardware txq should be locked.
814eb6f0de0SAdrian Chadd  */
815eb6f0de0SAdrian Chadd static void
816746bab5bSAdrian Chadd ath_legacy_xmit_handoff(struct ath_softc *sc, struct ath_txq *txq,
817746bab5bSAdrian Chadd     struct ath_buf *bf)
818eb6f0de0SAdrian Chadd {
819eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
820eb6f0de0SAdrian Chadd 
821eb6f0de0SAdrian Chadd 	if (txq->axq_qnum == ATH_TXQ_SWQ)
822eb6f0de0SAdrian Chadd 		ath_tx_handoff_mcast(sc, txq, bf);
823eb6f0de0SAdrian Chadd 	else
824eb6f0de0SAdrian Chadd 		ath_tx_handoff_hw(sc, txq, bf);
825b8e788a5SAdrian Chadd }
826b8e788a5SAdrian Chadd 
82781a82688SAdrian Chadd static int
82881a82688SAdrian Chadd ath_tx_tag_crypto(struct ath_softc *sc, struct ieee80211_node *ni,
829d4365d16SAdrian Chadd     struct mbuf *m0, int iswep, int isfrag, int *hdrlen, int *pktlen,
830d4365d16SAdrian Chadd     int *keyix)
83181a82688SAdrian Chadd {
83212be5b9cSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_XMIT,
83312be5b9cSAdrian Chadd 	    "%s: hdrlen=%d, pktlen=%d, isfrag=%d, iswep=%d, m0=%p\n",
83412be5b9cSAdrian Chadd 	    __func__,
83512be5b9cSAdrian Chadd 	    *hdrlen,
83612be5b9cSAdrian Chadd 	    *pktlen,
83712be5b9cSAdrian Chadd 	    isfrag,
83812be5b9cSAdrian Chadd 	    iswep,
83912be5b9cSAdrian Chadd 	    m0);
84012be5b9cSAdrian Chadd 
84181a82688SAdrian Chadd 	if (iswep) {
84281a82688SAdrian Chadd 		const struct ieee80211_cipher *cip;
84381a82688SAdrian Chadd 		struct ieee80211_key *k;
84481a82688SAdrian Chadd 
84581a82688SAdrian Chadd 		/*
84681a82688SAdrian Chadd 		 * Construct the 802.11 header+trailer for an encrypted
84781a82688SAdrian Chadd 		 * frame. The only reason this can fail is because of an
84881a82688SAdrian Chadd 		 * unknown or unsupported cipher/key type.
84981a82688SAdrian Chadd 		 */
85081a82688SAdrian Chadd 		k = ieee80211_crypto_encap(ni, m0);
85181a82688SAdrian Chadd 		if (k == NULL) {
85281a82688SAdrian Chadd 			/*
85381a82688SAdrian Chadd 			 * This can happen when the key is yanked after the
85481a82688SAdrian Chadd 			 * frame was queued.  Just discard the frame; the
85581a82688SAdrian Chadd 			 * 802.11 layer counts failures and provides
85681a82688SAdrian Chadd 			 * debugging/diagnostics.
85781a82688SAdrian Chadd 			 */
858d4365d16SAdrian Chadd 			return (0);
85981a82688SAdrian Chadd 		}
86081a82688SAdrian Chadd 		/*
86181a82688SAdrian Chadd 		 * Adjust the packet + header lengths for the crypto
86281a82688SAdrian Chadd 		 * additions and calculate the h/w key index.  When
86381a82688SAdrian Chadd 		 * a s/w mic is done the frame will have had any mic
86481a82688SAdrian Chadd 		 * added to it prior to entry so m0->m_pkthdr.len will
86581a82688SAdrian Chadd 		 * account for it. Otherwise we need to add it to the
86681a82688SAdrian Chadd 		 * packet length.
86781a82688SAdrian Chadd 		 */
86881a82688SAdrian Chadd 		cip = k->wk_cipher;
86981a82688SAdrian Chadd 		(*hdrlen) += cip->ic_header;
87081a82688SAdrian Chadd 		(*pktlen) += cip->ic_header + cip->ic_trailer;
87181a82688SAdrian Chadd 		/* NB: frags always have any TKIP MIC done in s/w */
87281a82688SAdrian Chadd 		if ((k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && !isfrag)
87381a82688SAdrian Chadd 			(*pktlen) += cip->ic_miclen;
87481a82688SAdrian Chadd 		(*keyix) = k->wk_keyix;
87581a82688SAdrian Chadd 	} else if (ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) {
87681a82688SAdrian Chadd 		/*
87781a82688SAdrian Chadd 		 * Use station key cache slot, if assigned.
87881a82688SAdrian Chadd 		 */
87981a82688SAdrian Chadd 		(*keyix) = ni->ni_ucastkey.wk_keyix;
88081a82688SAdrian Chadd 		if ((*keyix) == IEEE80211_KEYIX_NONE)
88181a82688SAdrian Chadd 			(*keyix) = HAL_TXKEYIX_INVALID;
88281a82688SAdrian Chadd 	} else
88381a82688SAdrian Chadd 		(*keyix) = HAL_TXKEYIX_INVALID;
88481a82688SAdrian Chadd 
885d4365d16SAdrian Chadd 	return (1);
88681a82688SAdrian Chadd }
88781a82688SAdrian Chadd 
888e2e4a2c2SAdrian Chadd /*
889e2e4a2c2SAdrian Chadd  * Calculate whether interoperability protection is required for
890e2e4a2c2SAdrian Chadd  * this frame.
891e2e4a2c2SAdrian Chadd  *
892e2e4a2c2SAdrian Chadd  * This requires the rate control information be filled in,
893e2e4a2c2SAdrian Chadd  * as the protection requirement depends upon the current
894e2e4a2c2SAdrian Chadd  * operating mode / PHY.
895e2e4a2c2SAdrian Chadd  */
896e2e4a2c2SAdrian Chadd static void
897e2e4a2c2SAdrian Chadd ath_tx_calc_protection(struct ath_softc *sc, struct ath_buf *bf)
898e2e4a2c2SAdrian Chadd {
899e2e4a2c2SAdrian Chadd 	struct ieee80211_frame *wh;
900e2e4a2c2SAdrian Chadd 	uint8_t rix;
901e2e4a2c2SAdrian Chadd 	uint16_t flags;
902e2e4a2c2SAdrian Chadd 	int shortPreamble;
903e2e4a2c2SAdrian Chadd 	const HAL_RATE_TABLE *rt = sc->sc_currates;
904e2e4a2c2SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
905e2e4a2c2SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
906e2e4a2c2SAdrian Chadd 
907e2e4a2c2SAdrian Chadd 	flags = bf->bf_state.bfs_txflags;
908e2e4a2c2SAdrian Chadd 	rix = bf->bf_state.bfs_rc[0].rix;
909e2e4a2c2SAdrian Chadd 	shortPreamble = bf->bf_state.bfs_shpream;
910e2e4a2c2SAdrian Chadd 	wh = mtod(bf->bf_m, struct ieee80211_frame *);
911e2e4a2c2SAdrian Chadd 
912e2e4a2c2SAdrian Chadd 	/*
913e2e4a2c2SAdrian Chadd 	 * If 802.11g protection is enabled, determine whether
914e2e4a2c2SAdrian Chadd 	 * to use RTS/CTS or just CTS.  Note that this is only
915e2e4a2c2SAdrian Chadd 	 * done for OFDM unicast frames.
916e2e4a2c2SAdrian Chadd 	 */
917e2e4a2c2SAdrian Chadd 	if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
918e2e4a2c2SAdrian Chadd 	    rt->info[rix].phy == IEEE80211_T_OFDM &&
919e2e4a2c2SAdrian Chadd 	    (flags & HAL_TXDESC_NOACK) == 0) {
920e2e4a2c2SAdrian Chadd 		bf->bf_state.bfs_doprot = 1;
921e2e4a2c2SAdrian Chadd 		/* XXX fragments must use CCK rates w/ protection */
922e2e4a2c2SAdrian Chadd 		if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) {
923e2e4a2c2SAdrian Chadd 			flags |= HAL_TXDESC_RTSENA;
924e2e4a2c2SAdrian Chadd 		} else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) {
925e2e4a2c2SAdrian Chadd 			flags |= HAL_TXDESC_CTSENA;
926e2e4a2c2SAdrian Chadd 		}
927e2e4a2c2SAdrian Chadd 		/*
928e2e4a2c2SAdrian Chadd 		 * For frags it would be desirable to use the
929e2e4a2c2SAdrian Chadd 		 * highest CCK rate for RTS/CTS.  But stations
930e2e4a2c2SAdrian Chadd 		 * farther away may detect it at a lower CCK rate
931e2e4a2c2SAdrian Chadd 		 * so use the configured protection rate instead
932e2e4a2c2SAdrian Chadd 		 * (for now).
933e2e4a2c2SAdrian Chadd 		 */
934e2e4a2c2SAdrian Chadd 		sc->sc_stats.ast_tx_protect++;
935e2e4a2c2SAdrian Chadd 	}
936e2e4a2c2SAdrian Chadd 
937e2e4a2c2SAdrian Chadd 	/*
938e2e4a2c2SAdrian Chadd 	 * If 11n protection is enabled and it's a HT frame,
939e2e4a2c2SAdrian Chadd 	 * enable RTS.
940e2e4a2c2SAdrian Chadd 	 *
941e2e4a2c2SAdrian Chadd 	 * XXX ic_htprotmode or ic_curhtprotmode?
942e2e4a2c2SAdrian Chadd 	 * XXX should it_htprotmode only matter if ic_curhtprotmode
943e2e4a2c2SAdrian Chadd 	 * XXX indicates it's not a HT pure environment?
944e2e4a2c2SAdrian Chadd 	 */
945e2e4a2c2SAdrian Chadd 	if ((ic->ic_htprotmode == IEEE80211_PROT_RTSCTS) &&
946e2e4a2c2SAdrian Chadd 	    rt->info[rix].phy == IEEE80211_T_HT &&
947e2e4a2c2SAdrian Chadd 	    (flags & HAL_TXDESC_NOACK) == 0) {
948e2e4a2c2SAdrian Chadd 		flags |= HAL_TXDESC_RTSENA;
949e2e4a2c2SAdrian Chadd 		sc->sc_stats.ast_tx_htprotect++;
950e2e4a2c2SAdrian Chadd 	}
951e2e4a2c2SAdrian Chadd 	bf->bf_state.bfs_txflags = flags;
952e2e4a2c2SAdrian Chadd }
953e2e4a2c2SAdrian Chadd 
954e2e4a2c2SAdrian Chadd /*
955e2e4a2c2SAdrian Chadd  * Update the frame duration given the currently selected rate.
956e2e4a2c2SAdrian Chadd  *
957e2e4a2c2SAdrian Chadd  * This also updates the frame duration value, so it will require
958e2e4a2c2SAdrian Chadd  * a DMA flush.
959e2e4a2c2SAdrian Chadd  */
960e2e4a2c2SAdrian Chadd static void
961e2e4a2c2SAdrian Chadd ath_tx_calc_duration(struct ath_softc *sc, struct ath_buf *bf)
962e2e4a2c2SAdrian Chadd {
963e2e4a2c2SAdrian Chadd 	struct ieee80211_frame *wh;
964e2e4a2c2SAdrian Chadd 	uint8_t rix;
965e2e4a2c2SAdrian Chadd 	uint16_t flags;
966e2e4a2c2SAdrian Chadd 	int shortPreamble;
967e2e4a2c2SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
968e2e4a2c2SAdrian Chadd 	const HAL_RATE_TABLE *rt = sc->sc_currates;
969e2e4a2c2SAdrian Chadd 	int isfrag = bf->bf_m->m_flags & M_FRAG;
970e2e4a2c2SAdrian Chadd 
971e2e4a2c2SAdrian Chadd 	flags = bf->bf_state.bfs_txflags;
972e2e4a2c2SAdrian Chadd 	rix = bf->bf_state.bfs_rc[0].rix;
973e2e4a2c2SAdrian Chadd 	shortPreamble = bf->bf_state.bfs_shpream;
974e2e4a2c2SAdrian Chadd 	wh = mtod(bf->bf_m, struct ieee80211_frame *);
975e2e4a2c2SAdrian Chadd 
976e2e4a2c2SAdrian Chadd 	/*
977e2e4a2c2SAdrian Chadd 	 * Calculate duration.  This logically belongs in the 802.11
978e2e4a2c2SAdrian Chadd 	 * layer but it lacks sufficient information to calculate it.
979e2e4a2c2SAdrian Chadd 	 */
980e2e4a2c2SAdrian Chadd 	if ((flags & HAL_TXDESC_NOACK) == 0 &&
981e2e4a2c2SAdrian Chadd 	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL) {
982e2e4a2c2SAdrian Chadd 		u_int16_t dur;
983e2e4a2c2SAdrian Chadd 		if (shortPreamble)
984e2e4a2c2SAdrian Chadd 			dur = rt->info[rix].spAckDuration;
985e2e4a2c2SAdrian Chadd 		else
986e2e4a2c2SAdrian Chadd 			dur = rt->info[rix].lpAckDuration;
987e2e4a2c2SAdrian Chadd 		if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) {
988e2e4a2c2SAdrian Chadd 			dur += dur;		/* additional SIFS+ACK */
989e2e4a2c2SAdrian Chadd 			KASSERT(bf->bf_m->m_nextpkt != NULL, ("no fragment"));
990e2e4a2c2SAdrian Chadd 			/*
991e2e4a2c2SAdrian Chadd 			 * Include the size of next fragment so NAV is
992e2e4a2c2SAdrian Chadd 			 * updated properly.  The last fragment uses only
993e2e4a2c2SAdrian Chadd 			 * the ACK duration
994e2e4a2c2SAdrian Chadd 			 */
995e2e4a2c2SAdrian Chadd 			dur += ath_hal_computetxtime(ah, rt,
996e2e4a2c2SAdrian Chadd 					bf->bf_m->m_nextpkt->m_pkthdr.len,
997e2e4a2c2SAdrian Chadd 					rix, shortPreamble);
998e2e4a2c2SAdrian Chadd 		}
999e2e4a2c2SAdrian Chadd 		if (isfrag) {
1000e2e4a2c2SAdrian Chadd 			/*
1001e2e4a2c2SAdrian Chadd 			 * Force hardware to use computed duration for next
1002e2e4a2c2SAdrian Chadd 			 * fragment by disabling multi-rate retry which updates
1003e2e4a2c2SAdrian Chadd 			 * duration based on the multi-rate duration table.
1004e2e4a2c2SAdrian Chadd 			 */
1005e2e4a2c2SAdrian Chadd 			bf->bf_state.bfs_ismrr = 0;
1006e2e4a2c2SAdrian Chadd 			bf->bf_state.bfs_try0 = ATH_TXMGTTRY;
1007e2e4a2c2SAdrian Chadd 			/* XXX update bfs_rc[0].try? */
1008e2e4a2c2SAdrian Chadd 		}
1009e2e4a2c2SAdrian Chadd 
1010e2e4a2c2SAdrian Chadd 		/* Update the duration field itself */
1011e2e4a2c2SAdrian Chadd 		*(u_int16_t *)wh->i_dur = htole16(dur);
1012e2e4a2c2SAdrian Chadd 	}
1013e2e4a2c2SAdrian Chadd }
1014e2e4a2c2SAdrian Chadd 
1015e42b5dbaSAdrian Chadd static uint8_t
1016e42b5dbaSAdrian Chadd ath_tx_get_rtscts_rate(struct ath_hal *ah, const HAL_RATE_TABLE *rt,
1017eb6f0de0SAdrian Chadd     int cix, int shortPreamble)
101879f02dbfSAdrian Chadd {
1019e42b5dbaSAdrian Chadd 	uint8_t ctsrate;
1020e42b5dbaSAdrian Chadd 
102179f02dbfSAdrian Chadd 	/*
102279f02dbfSAdrian Chadd 	 * CTS transmit rate is derived from the transmit rate
102379f02dbfSAdrian Chadd 	 * by looking in the h/w rate table.  We must also factor
102479f02dbfSAdrian Chadd 	 * in whether or not a short preamble is to be used.
102579f02dbfSAdrian Chadd 	 */
102679f02dbfSAdrian Chadd 	/* NB: cix is set above where RTS/CTS is enabled */
102779f02dbfSAdrian Chadd 	KASSERT(cix != 0xff, ("cix not setup"));
1028e42b5dbaSAdrian Chadd 	ctsrate = rt->info[cix].rateCode;
1029e42b5dbaSAdrian Chadd 
1030e42b5dbaSAdrian Chadd 	/* XXX this should only matter for legacy rates */
1031e42b5dbaSAdrian Chadd 	if (shortPreamble)
1032e42b5dbaSAdrian Chadd 		ctsrate |= rt->info[cix].shortPreamble;
1033e42b5dbaSAdrian Chadd 
1034d4365d16SAdrian Chadd 	return (ctsrate);
1035e42b5dbaSAdrian Chadd }
1036e42b5dbaSAdrian Chadd 
1037e42b5dbaSAdrian Chadd /*
1038e42b5dbaSAdrian Chadd  * Calculate the RTS/CTS duration for legacy frames.
1039e42b5dbaSAdrian Chadd  */
1040e42b5dbaSAdrian Chadd static int
1041e42b5dbaSAdrian Chadd ath_tx_calc_ctsduration(struct ath_hal *ah, int rix, int cix,
1042e42b5dbaSAdrian Chadd     int shortPreamble, int pktlen, const HAL_RATE_TABLE *rt,
1043e42b5dbaSAdrian Chadd     int flags)
1044e42b5dbaSAdrian Chadd {
1045e42b5dbaSAdrian Chadd 	int ctsduration = 0;
1046e42b5dbaSAdrian Chadd 
1047e42b5dbaSAdrian Chadd 	/* This mustn't be called for HT modes */
1048e42b5dbaSAdrian Chadd 	if (rt->info[cix].phy == IEEE80211_T_HT) {
1049e42b5dbaSAdrian Chadd 		printf("%s: HT rate where it shouldn't be (0x%x)\n",
1050e42b5dbaSAdrian Chadd 		    __func__, rt->info[cix].rateCode);
1051d4365d16SAdrian Chadd 		return (-1);
1052e42b5dbaSAdrian Chadd 	}
1053e42b5dbaSAdrian Chadd 
105479f02dbfSAdrian Chadd 	/*
105579f02dbfSAdrian Chadd 	 * Compute the transmit duration based on the frame
105679f02dbfSAdrian Chadd 	 * size and the size of an ACK frame.  We call into the
105779f02dbfSAdrian Chadd 	 * HAL to do the computation since it depends on the
105879f02dbfSAdrian Chadd 	 * characteristics of the actual PHY being used.
105979f02dbfSAdrian Chadd 	 *
106079f02dbfSAdrian Chadd 	 * NB: CTS is assumed the same size as an ACK so we can
106179f02dbfSAdrian Chadd 	 *     use the precalculated ACK durations.
106279f02dbfSAdrian Chadd 	 */
106379f02dbfSAdrian Chadd 	if (shortPreamble) {
106479f02dbfSAdrian Chadd 		if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
1065e42b5dbaSAdrian Chadd 			ctsduration += rt->info[cix].spAckDuration;
1066e42b5dbaSAdrian Chadd 		ctsduration += ath_hal_computetxtime(ah,
106779f02dbfSAdrian Chadd 			rt, pktlen, rix, AH_TRUE);
106879f02dbfSAdrian Chadd 		if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
1069e42b5dbaSAdrian Chadd 			ctsduration += rt->info[rix].spAckDuration;
107079f02dbfSAdrian Chadd 	} else {
107179f02dbfSAdrian Chadd 		if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
1072e42b5dbaSAdrian Chadd 			ctsduration += rt->info[cix].lpAckDuration;
1073e42b5dbaSAdrian Chadd 		ctsduration += ath_hal_computetxtime(ah,
107479f02dbfSAdrian Chadd 			rt, pktlen, rix, AH_FALSE);
107579f02dbfSAdrian Chadd 		if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
1076e42b5dbaSAdrian Chadd 			ctsduration += rt->info[rix].lpAckDuration;
107779f02dbfSAdrian Chadd 	}
1078e42b5dbaSAdrian Chadd 
1079d4365d16SAdrian Chadd 	return (ctsduration);
108079f02dbfSAdrian Chadd }
108179f02dbfSAdrian Chadd 
1082eb6f0de0SAdrian Chadd /*
1083eb6f0de0SAdrian Chadd  * Update the given ath_buf with updated rts/cts setup and duration
1084eb6f0de0SAdrian Chadd  * values.
1085eb6f0de0SAdrian Chadd  *
1086eb6f0de0SAdrian Chadd  * To support rate lookups for each software retry, the rts/cts rate
1087eb6f0de0SAdrian Chadd  * and cts duration must be re-calculated.
1088eb6f0de0SAdrian Chadd  *
1089eb6f0de0SAdrian Chadd  * This function assumes the RTS/CTS flags have been set as needed;
1090eb6f0de0SAdrian Chadd  * mrr has been disabled; and the rate control lookup has been done.
1091eb6f0de0SAdrian Chadd  *
1092eb6f0de0SAdrian Chadd  * XXX TODO: MRR need only be disabled for the pre-11n NICs.
1093eb6f0de0SAdrian Chadd  * XXX The 11n NICs support per-rate RTS/CTS configuration.
1094eb6f0de0SAdrian Chadd  */
1095eb6f0de0SAdrian Chadd static void
1096eb6f0de0SAdrian Chadd ath_tx_set_rtscts(struct ath_softc *sc, struct ath_buf *bf)
1097eb6f0de0SAdrian Chadd {
1098eb6f0de0SAdrian Chadd 	uint16_t ctsduration = 0;
1099eb6f0de0SAdrian Chadd 	uint8_t ctsrate = 0;
1100eb6f0de0SAdrian Chadd 	uint8_t rix = bf->bf_state.bfs_rc[0].rix;
1101eb6f0de0SAdrian Chadd 	uint8_t cix = 0;
1102eb6f0de0SAdrian Chadd 	const HAL_RATE_TABLE *rt = sc->sc_currates;
1103eb6f0de0SAdrian Chadd 
1104eb6f0de0SAdrian Chadd 	/*
1105eb6f0de0SAdrian Chadd 	 * No RTS/CTS enabled? Don't bother.
1106eb6f0de0SAdrian Chadd 	 */
1107875a9451SAdrian Chadd 	if ((bf->bf_state.bfs_txflags &
1108eb6f0de0SAdrian Chadd 	    (HAL_TXDESC_RTSENA | HAL_TXDESC_CTSENA)) == 0) {
1109eb6f0de0SAdrian Chadd 		/* XXX is this really needed? */
1110eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_ctsrate = 0;
1111eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_ctsduration = 0;
1112eb6f0de0SAdrian Chadd 		return;
1113eb6f0de0SAdrian Chadd 	}
1114eb6f0de0SAdrian Chadd 
1115eb6f0de0SAdrian Chadd 	/*
1116eb6f0de0SAdrian Chadd 	 * If protection is enabled, use the protection rix control
1117eb6f0de0SAdrian Chadd 	 * rate. Otherwise use the rate0 control rate.
1118eb6f0de0SAdrian Chadd 	 */
1119eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_doprot)
1120eb6f0de0SAdrian Chadd 		rix = sc->sc_protrix;
1121eb6f0de0SAdrian Chadd 	else
1122eb6f0de0SAdrian Chadd 		rix = bf->bf_state.bfs_rc[0].rix;
1123eb6f0de0SAdrian Chadd 
1124eb6f0de0SAdrian Chadd 	/*
1125eb6f0de0SAdrian Chadd 	 * If the raw path has hard-coded ctsrate0 to something,
1126eb6f0de0SAdrian Chadd 	 * use it.
1127eb6f0de0SAdrian Chadd 	 */
1128eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_ctsrate0 != 0)
1129eb6f0de0SAdrian Chadd 		cix = ath_tx_findrix(sc, bf->bf_state.bfs_ctsrate0);
1130eb6f0de0SAdrian Chadd 	else
1131eb6f0de0SAdrian Chadd 		/* Control rate from above */
1132eb6f0de0SAdrian Chadd 		cix = rt->info[rix].controlRate;
1133eb6f0de0SAdrian Chadd 
1134eb6f0de0SAdrian Chadd 	/* Calculate the rtscts rate for the given cix */
1135eb6f0de0SAdrian Chadd 	ctsrate = ath_tx_get_rtscts_rate(sc->sc_ah, rt, cix,
1136eb6f0de0SAdrian Chadd 	    bf->bf_state.bfs_shpream);
1137eb6f0de0SAdrian Chadd 
1138eb6f0de0SAdrian Chadd 	/* The 11n chipsets do ctsduration calculations for you */
1139eb6f0de0SAdrian Chadd 	if (! ath_tx_is_11n(sc))
1140eb6f0de0SAdrian Chadd 		ctsduration = ath_tx_calc_ctsduration(sc->sc_ah, rix, cix,
1141eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_shpream, bf->bf_state.bfs_pktlen,
1142875a9451SAdrian Chadd 		    rt, bf->bf_state.bfs_txflags);
1143eb6f0de0SAdrian Chadd 
1144eb6f0de0SAdrian Chadd 	/* Squirrel away in ath_buf */
1145eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsrate = ctsrate;
1146eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsduration = ctsduration;
1147eb6f0de0SAdrian Chadd 
1148eb6f0de0SAdrian Chadd 	/*
1149eb6f0de0SAdrian Chadd 	 * Must disable multi-rate retry when using RTS/CTS.
1150eb6f0de0SAdrian Chadd 	 */
1151af017101SAdrian Chadd 	if (!sc->sc_mrrprot) {
1152eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_ismrr = 0;
1153eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_try0 =
1154eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_rc[0].tries = ATH_TXMGTTRY; /* XXX ew */
1155eb6f0de0SAdrian Chadd 	}
1156af017101SAdrian Chadd }
1157eb6f0de0SAdrian Chadd 
1158eb6f0de0SAdrian Chadd /*
1159eb6f0de0SAdrian Chadd  * Setup the descriptor chain for a normal or fast-frame
1160eb6f0de0SAdrian Chadd  * frame.
116146634305SAdrian Chadd  *
116246634305SAdrian Chadd  * XXX TODO: extend to include the destination hardware QCU ID.
116346634305SAdrian Chadd  * Make sure that is correct.  Make sure that when being added
116446634305SAdrian Chadd  * to the mcastq, the CABQ QCUID is set or things will get a bit
116546634305SAdrian Chadd  * odd.
1166eb6f0de0SAdrian Chadd  */
1167eb6f0de0SAdrian Chadd static void
1168eb6f0de0SAdrian Chadd ath_tx_setds(struct ath_softc *sc, struct ath_buf *bf)
1169eb6f0de0SAdrian Chadd {
1170eb6f0de0SAdrian Chadd 	struct ath_desc *ds = bf->bf_desc;
1171eb6f0de0SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
1172eb6f0de0SAdrian Chadd 
1173eb6f0de0SAdrian Chadd 	ath_hal_setuptxdesc(ah, ds
1174eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_pktlen	/* packet length */
1175eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_hdrlen	/* header length */
1176eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_atype	/* Atheros packet type */
1177eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_txpower	/* txpower */
1178eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_txrate0
1179eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_try0		/* series 0 rate/tries */
1180eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_keyix	/* key cache index */
1181eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_txantenna	/* antenna mode */
1182875a9451SAdrian Chadd 		, bf->bf_state.bfs_txflags	/* flags */
1183eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_ctsrate	/* rts/cts rate */
1184eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_ctsduration	/* rts/cts duration */
1185eb6f0de0SAdrian Chadd 	);
1186eb6f0de0SAdrian Chadd 
1187eb6f0de0SAdrian Chadd 	/*
1188eb6f0de0SAdrian Chadd 	 * This will be overriden when the descriptor chain is written.
1189eb6f0de0SAdrian Chadd 	 */
1190eb6f0de0SAdrian Chadd 	bf->bf_lastds = ds;
1191eb6f0de0SAdrian Chadd 	bf->bf_last = bf;
1192eb6f0de0SAdrian Chadd 
1193d34a7347SAdrian Chadd 	/* Set rate control and descriptor chain for this frame */
1194d34a7347SAdrian Chadd 	ath_tx_set_ratectrl(sc, bf->bf_node, bf);
1195d34a7347SAdrian Chadd 	ath_tx_chaindesclist(sc, bf);
1196eb6f0de0SAdrian Chadd }
1197eb6f0de0SAdrian Chadd 
1198eb6f0de0SAdrian Chadd /*
1199eb6f0de0SAdrian Chadd  * Do a rate lookup.
1200eb6f0de0SAdrian Chadd  *
1201eb6f0de0SAdrian Chadd  * This performs a rate lookup for the given ath_buf only if it's required.
1202eb6f0de0SAdrian Chadd  * Non-data frames and raw frames don't require it.
1203eb6f0de0SAdrian Chadd  *
1204eb6f0de0SAdrian Chadd  * This populates the primary and MRR entries; MRR values are
1205eb6f0de0SAdrian Chadd  * then disabled later on if something requires it (eg RTS/CTS on
1206eb6f0de0SAdrian Chadd  * pre-11n chipsets.
1207eb6f0de0SAdrian Chadd  *
1208eb6f0de0SAdrian Chadd  * This needs to be done before the RTS/CTS fields are calculated
1209eb6f0de0SAdrian Chadd  * as they may depend upon the rate chosen.
1210eb6f0de0SAdrian Chadd  */
1211eb6f0de0SAdrian Chadd static void
1212eb6f0de0SAdrian Chadd ath_tx_do_ratelookup(struct ath_softc *sc, struct ath_buf *bf)
1213eb6f0de0SAdrian Chadd {
1214eb6f0de0SAdrian Chadd 	uint8_t rate, rix;
1215eb6f0de0SAdrian Chadd 	int try0;
1216eb6f0de0SAdrian Chadd 
1217eb6f0de0SAdrian Chadd 	if (! bf->bf_state.bfs_doratelookup)
1218eb6f0de0SAdrian Chadd 		return;
1219eb6f0de0SAdrian Chadd 
1220eb6f0de0SAdrian Chadd 	/* Get rid of any previous state */
1221eb6f0de0SAdrian Chadd 	bzero(bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
1222eb6f0de0SAdrian Chadd 
1223eb6f0de0SAdrian Chadd 	ATH_NODE_LOCK(ATH_NODE(bf->bf_node));
1224eb6f0de0SAdrian Chadd 	ath_rate_findrate(sc, ATH_NODE(bf->bf_node), bf->bf_state.bfs_shpream,
1225eb6f0de0SAdrian Chadd 	    bf->bf_state.bfs_pktlen, &rix, &try0, &rate);
1226eb6f0de0SAdrian Chadd 
1227eb6f0de0SAdrian Chadd 	/* In case MRR is disabled, make sure rc[0] is setup correctly */
1228eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].rix = rix;
1229eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].ratecode = rate;
1230eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].tries = try0;
1231eb6f0de0SAdrian Chadd 
1232eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_ismrr && try0 != ATH_TXMAXTRY)
1233eb6f0de0SAdrian Chadd 		ath_rate_getxtxrates(sc, ATH_NODE(bf->bf_node), rix,
1234eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_rc);
1235eb6f0de0SAdrian Chadd 	ATH_NODE_UNLOCK(ATH_NODE(bf->bf_node));
1236eb6f0de0SAdrian Chadd 
1237eb6f0de0SAdrian Chadd 	sc->sc_txrix = rix;	/* for LED blinking */
1238eb6f0de0SAdrian Chadd 	sc->sc_lastdatarix = rix;	/* for fast frames */
1239eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_try0 = try0;
1240eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txrate0 = rate;
1241eb6f0de0SAdrian Chadd }
1242eb6f0de0SAdrian Chadd 
1243eb6f0de0SAdrian Chadd /*
1244eb6f0de0SAdrian Chadd  * Transmit the given frame to the hardware.
1245eb6f0de0SAdrian Chadd  *
1246eb6f0de0SAdrian Chadd  * The frame must already be setup; rate control must already have
1247eb6f0de0SAdrian Chadd  * been done.
1248eb6f0de0SAdrian Chadd  *
1249eb6f0de0SAdrian Chadd  * XXX since the TXQ lock is being held here (and I dislike holding
1250eb6f0de0SAdrian Chadd  * it for this long when not doing software aggregation), later on
1251eb6f0de0SAdrian Chadd  * break this function into "setup_normal" and "xmit_normal". The
1252eb6f0de0SAdrian Chadd  * lock only needs to be held for the ath_tx_handoff call.
1253eb6f0de0SAdrian Chadd  */
1254eb6f0de0SAdrian Chadd static void
1255eb6f0de0SAdrian Chadd ath_tx_xmit_normal(struct ath_softc *sc, struct ath_txq *txq,
1256eb6f0de0SAdrian Chadd     struct ath_buf *bf)
1257eb6f0de0SAdrian Chadd {
1258eb6f0de0SAdrian Chadd 
1259eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
1260eb6f0de0SAdrian Chadd 
1261eb6f0de0SAdrian Chadd 	/* Setup the descriptor before handoff */
1262eb6f0de0SAdrian Chadd 	ath_tx_do_ratelookup(sc, bf);
1263e2e4a2c2SAdrian Chadd 	ath_tx_calc_duration(sc, bf);
1264e2e4a2c2SAdrian Chadd 	ath_tx_calc_protection(sc, bf);
1265eb6f0de0SAdrian Chadd 	ath_tx_set_rtscts(sc, bf);
1266e2e4a2c2SAdrian Chadd 	ath_tx_rate_fill_rcflags(sc, bf);
1267eb6f0de0SAdrian Chadd 	ath_tx_setds(sc, bf);
1268eb6f0de0SAdrian Chadd 
1269eb6f0de0SAdrian Chadd 	/* Hand off to hardware */
1270eb6f0de0SAdrian Chadd 	ath_tx_handoff(sc, txq, bf);
1271eb6f0de0SAdrian Chadd }
1272eb6f0de0SAdrian Chadd 
1273eb6f0de0SAdrian Chadd 
1274eb6f0de0SAdrian Chadd 
1275eb6f0de0SAdrian Chadd static int
1276eb6f0de0SAdrian Chadd ath_tx_normal_setup(struct ath_softc *sc, struct ieee80211_node *ni,
1277b43facbfSAdrian Chadd     struct ath_buf *bf, struct mbuf *m0, struct ath_txq *txq)
1278b8e788a5SAdrian Chadd {
1279b8e788a5SAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
1280b8e788a5SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
1281b8e788a5SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
1282b8e788a5SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
1283b8e788a5SAdrian Chadd 	const struct chanAccParams *cap = &ic->ic_wme.wme_chanParams;
1284b8e788a5SAdrian Chadd 	int error, iswep, ismcast, isfrag, ismrr;
1285eb6f0de0SAdrian Chadd 	int keyix, hdrlen, pktlen, try0 = 0;
1286eb6f0de0SAdrian Chadd 	u_int8_t rix = 0, txrate = 0;
1287b8e788a5SAdrian Chadd 	struct ath_desc *ds;
1288b8e788a5SAdrian Chadd 	struct ieee80211_frame *wh;
1289eb6f0de0SAdrian Chadd 	u_int subtype, flags;
1290b8e788a5SAdrian Chadd 	HAL_PKT_TYPE atype;
1291b8e788a5SAdrian Chadd 	const HAL_RATE_TABLE *rt;
1292b8e788a5SAdrian Chadd 	HAL_BOOL shortPreamble;
1293b8e788a5SAdrian Chadd 	struct ath_node *an;
1294b8e788a5SAdrian Chadd 	u_int pri;
1295b8e788a5SAdrian Chadd 
12967561cb5cSAdrian Chadd 	/*
12977561cb5cSAdrian Chadd 	 * To ensure that both sequence numbers and the CCMP PN handling
12987561cb5cSAdrian Chadd 	 * is "correct", make sure that the relevant TID queue is locked.
12997561cb5cSAdrian Chadd 	 * Otherwise the CCMP PN and seqno may appear out of order, causing
13007561cb5cSAdrian Chadd 	 * re-ordered frames to have out of order CCMP PN's, resulting
13017561cb5cSAdrian Chadd 	 * in many, many frame drops.
13027561cb5cSAdrian Chadd 	 */
13037561cb5cSAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
13047561cb5cSAdrian Chadd 
1305b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1306b8e788a5SAdrian Chadd 	iswep = wh->i_fc[1] & IEEE80211_FC1_WEP;
1307b8e788a5SAdrian Chadd 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1308b8e788a5SAdrian Chadd 	isfrag = m0->m_flags & M_FRAG;
1309b8e788a5SAdrian Chadd 	hdrlen = ieee80211_anyhdrsize(wh);
1310b8e788a5SAdrian Chadd 	/*
1311b8e788a5SAdrian Chadd 	 * Packet length must not include any
1312b8e788a5SAdrian Chadd 	 * pad bytes; deduct them here.
1313b8e788a5SAdrian Chadd 	 */
1314b8e788a5SAdrian Chadd 	pktlen = m0->m_pkthdr.len - (hdrlen & 3);
1315b8e788a5SAdrian Chadd 
131681a82688SAdrian Chadd 	/* Handle encryption twiddling if needed */
1317eb6f0de0SAdrian Chadd 	if (! ath_tx_tag_crypto(sc, ni, m0, iswep, isfrag, &hdrlen,
1318eb6f0de0SAdrian Chadd 	    &pktlen, &keyix)) {
1319b8e788a5SAdrian Chadd 		ath_freetx(m0);
1320b8e788a5SAdrian Chadd 		return EIO;
1321b8e788a5SAdrian Chadd 	}
1322b8e788a5SAdrian Chadd 
1323b8e788a5SAdrian Chadd 	/* packet header may have moved, reset our local pointer */
1324b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1325b8e788a5SAdrian Chadd 
1326b8e788a5SAdrian Chadd 	pktlen += IEEE80211_CRC_LEN;
1327b8e788a5SAdrian Chadd 
1328b8e788a5SAdrian Chadd 	/*
1329b8e788a5SAdrian Chadd 	 * Load the DMA map so any coalescing is done.  This
1330b8e788a5SAdrian Chadd 	 * also calculates the number of descriptors we need.
1331b8e788a5SAdrian Chadd 	 */
1332b8e788a5SAdrian Chadd 	error = ath_tx_dmasetup(sc, bf, m0);
1333b8e788a5SAdrian Chadd 	if (error != 0)
1334b8e788a5SAdrian Chadd 		return error;
1335b8e788a5SAdrian Chadd 	bf->bf_node = ni;			/* NB: held reference */
1336b8e788a5SAdrian Chadd 	m0 = bf->bf_m;				/* NB: may have changed */
1337b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1338b8e788a5SAdrian Chadd 
1339b8e788a5SAdrian Chadd 	/* setup descriptors */
1340b8e788a5SAdrian Chadd 	ds = bf->bf_desc;
1341b8e788a5SAdrian Chadd 	rt = sc->sc_currates;
1342b8e788a5SAdrian Chadd 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
1343b8e788a5SAdrian Chadd 
1344b8e788a5SAdrian Chadd 	/*
1345b8e788a5SAdrian Chadd 	 * NB: the 802.11 layer marks whether or not we should
1346b8e788a5SAdrian Chadd 	 * use short preamble based on the current mode and
1347b8e788a5SAdrian Chadd 	 * negotiated parameters.
1348b8e788a5SAdrian Chadd 	 */
1349b8e788a5SAdrian Chadd 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1350b8e788a5SAdrian Chadd 	    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
1351b8e788a5SAdrian Chadd 		shortPreamble = AH_TRUE;
1352b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_shortpre++;
1353b8e788a5SAdrian Chadd 	} else {
1354b8e788a5SAdrian Chadd 		shortPreamble = AH_FALSE;
1355b8e788a5SAdrian Chadd 	}
1356b8e788a5SAdrian Chadd 
1357b8e788a5SAdrian Chadd 	an = ATH_NODE(ni);
1358b8e788a5SAdrian Chadd 	flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for crypto errs */
1359b8e788a5SAdrian Chadd 	ismrr = 0;				/* default no multi-rate retry*/
1360b8e788a5SAdrian Chadd 	pri = M_WME_GETAC(m0);			/* honor classification */
1361b8e788a5SAdrian Chadd 	/* XXX use txparams instead of fixed values */
1362b8e788a5SAdrian Chadd 	/*
1363b8e788a5SAdrian Chadd 	 * Calculate Atheros packet type from IEEE80211 packet header,
1364b8e788a5SAdrian Chadd 	 * setup for rate calculations, and select h/w transmit queue.
1365b8e788a5SAdrian Chadd 	 */
1366b8e788a5SAdrian Chadd 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1367b8e788a5SAdrian Chadd 	case IEEE80211_FC0_TYPE_MGT:
1368b8e788a5SAdrian Chadd 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1369b8e788a5SAdrian Chadd 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON)
1370b8e788a5SAdrian Chadd 			atype = HAL_PKT_TYPE_BEACON;
1371b8e788a5SAdrian Chadd 		else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1372b8e788a5SAdrian Chadd 			atype = HAL_PKT_TYPE_PROBE_RESP;
1373b8e788a5SAdrian Chadd 		else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM)
1374b8e788a5SAdrian Chadd 			atype = HAL_PKT_TYPE_ATIM;
1375b8e788a5SAdrian Chadd 		else
1376b8e788a5SAdrian Chadd 			atype = HAL_PKT_TYPE_NORMAL;	/* XXX */
1377b8e788a5SAdrian Chadd 		rix = an->an_mgmtrix;
1378b8e788a5SAdrian Chadd 		txrate = rt->info[rix].rateCode;
1379b8e788a5SAdrian Chadd 		if (shortPreamble)
1380b8e788a5SAdrian Chadd 			txrate |= rt->info[rix].shortPreamble;
1381b8e788a5SAdrian Chadd 		try0 = ATH_TXMGTTRY;
1382b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_INTREQ;	/* force interrupt */
1383b8e788a5SAdrian Chadd 		break;
1384b8e788a5SAdrian Chadd 	case IEEE80211_FC0_TYPE_CTL:
1385b8e788a5SAdrian Chadd 		atype = HAL_PKT_TYPE_PSPOLL;	/* stop setting of duration */
1386b8e788a5SAdrian Chadd 		rix = an->an_mgmtrix;
1387b8e788a5SAdrian Chadd 		txrate = rt->info[rix].rateCode;
1388b8e788a5SAdrian Chadd 		if (shortPreamble)
1389b8e788a5SAdrian Chadd 			txrate |= rt->info[rix].shortPreamble;
1390b8e788a5SAdrian Chadd 		try0 = ATH_TXMGTTRY;
1391b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_INTREQ;	/* force interrupt */
1392b8e788a5SAdrian Chadd 		break;
1393b8e788a5SAdrian Chadd 	case IEEE80211_FC0_TYPE_DATA:
1394b8e788a5SAdrian Chadd 		atype = HAL_PKT_TYPE_NORMAL;		/* default */
1395b8e788a5SAdrian Chadd 		/*
1396b8e788a5SAdrian Chadd 		 * Data frames: multicast frames go out at a fixed rate,
1397b8e788a5SAdrian Chadd 		 * EAPOL frames use the mgmt frame rate; otherwise consult
1398b8e788a5SAdrian Chadd 		 * the rate control module for the rate to use.
1399b8e788a5SAdrian Chadd 		 */
1400b8e788a5SAdrian Chadd 		if (ismcast) {
1401b8e788a5SAdrian Chadd 			rix = an->an_mcastrix;
1402b8e788a5SAdrian Chadd 			txrate = rt->info[rix].rateCode;
1403b8e788a5SAdrian Chadd 			if (shortPreamble)
1404b8e788a5SAdrian Chadd 				txrate |= rt->info[rix].shortPreamble;
1405b8e788a5SAdrian Chadd 			try0 = 1;
1406b8e788a5SAdrian Chadd 		} else if (m0->m_flags & M_EAPOL) {
1407b8e788a5SAdrian Chadd 			/* XXX? maybe always use long preamble? */
1408b8e788a5SAdrian Chadd 			rix = an->an_mgmtrix;
1409b8e788a5SAdrian Chadd 			txrate = rt->info[rix].rateCode;
1410b8e788a5SAdrian Chadd 			if (shortPreamble)
1411b8e788a5SAdrian Chadd 				txrate |= rt->info[rix].shortPreamble;
1412b8e788a5SAdrian Chadd 			try0 = ATH_TXMAXTRY;	/* XXX?too many? */
1413b8e788a5SAdrian Chadd 		} else {
1414eb6f0de0SAdrian Chadd 			/*
1415eb6f0de0SAdrian Chadd 			 * Do rate lookup on each TX, rather than using
1416eb6f0de0SAdrian Chadd 			 * the hard-coded TX information decided here.
1417eb6f0de0SAdrian Chadd 			 */
1418b8e788a5SAdrian Chadd 			ismrr = 1;
1419eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_doratelookup = 1;
1420b8e788a5SAdrian Chadd 		}
1421b8e788a5SAdrian Chadd 		if (cap->cap_wmeParams[pri].wmep_noackPolicy)
1422b8e788a5SAdrian Chadd 			flags |= HAL_TXDESC_NOACK;
1423b8e788a5SAdrian Chadd 		break;
1424b8e788a5SAdrian Chadd 	default:
1425b8e788a5SAdrian Chadd 		if_printf(ifp, "bogus frame type 0x%x (%s)\n",
1426b8e788a5SAdrian Chadd 			wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__);
1427b8e788a5SAdrian Chadd 		/* XXX statistic */
1428b8e788a5SAdrian Chadd 		ath_freetx(m0);
1429b8e788a5SAdrian Chadd 		return EIO;
1430b8e788a5SAdrian Chadd 	}
1431b8e788a5SAdrian Chadd 
1432447fd44aSAdrian Chadd 	/*
1433447fd44aSAdrian Chadd 	 * There are two known scenarios where the frame AC doesn't match
1434447fd44aSAdrian Chadd 	 * what the destination TXQ is.
1435447fd44aSAdrian Chadd 	 *
1436447fd44aSAdrian Chadd 	 * + non-QoS frames (eg management?) that the net80211 stack has
1437447fd44aSAdrian Chadd 	 *   assigned a higher AC to, but since it's a non-QoS TID, it's
1438447fd44aSAdrian Chadd 	 *   being thrown into TID 16.  TID 16 gets the AC_BE queue.
1439447fd44aSAdrian Chadd 	 *   It's quite possible that management frames should just be
1440447fd44aSAdrian Chadd 	 *   direct dispatched to hardware rather than go via the software
1441447fd44aSAdrian Chadd 	 *   queue; that should be investigated in the future.  There are
1442447fd44aSAdrian Chadd 	 *   some specific scenarios where this doesn't make sense, mostly
1443447fd44aSAdrian Chadd 	 *   surrounding ADDBA request/response - hence why that is special
1444447fd44aSAdrian Chadd 	 *   cased.
1445447fd44aSAdrian Chadd 	 *
1446447fd44aSAdrian Chadd 	 * + Multicast frames going into the VAP mcast queue.  That shows up
1447447fd44aSAdrian Chadd 	 *   as "TXQ 11".
1448447fd44aSAdrian Chadd 	 *
1449447fd44aSAdrian Chadd 	 * This driver should eventually support separate TID and TXQ locking,
1450447fd44aSAdrian Chadd 	 * allowing for arbitrary AC frames to appear on arbitrary software
1451447fd44aSAdrian Chadd 	 * queues, being queued to the "correct" hardware queue when needed.
1452447fd44aSAdrian Chadd 	 */
1453447fd44aSAdrian Chadd #if 0
14546deb7f32SAdrian Chadd 	if (txq != sc->sc_ac2q[pri]) {
14556deb7f32SAdrian Chadd 		device_printf(sc->sc_dev,
14566deb7f32SAdrian Chadd 		    "%s: txq=%p (%d), pri=%d, pri txq=%p (%d)\n",
14576deb7f32SAdrian Chadd 		    __func__,
14586deb7f32SAdrian Chadd 		    txq,
14596deb7f32SAdrian Chadd 		    txq->axq_qnum,
14606deb7f32SAdrian Chadd 		    pri,
14616deb7f32SAdrian Chadd 		    sc->sc_ac2q[pri],
14626deb7f32SAdrian Chadd 		    sc->sc_ac2q[pri]->axq_qnum);
14636deb7f32SAdrian Chadd 	}
1464447fd44aSAdrian Chadd #endif
14656deb7f32SAdrian Chadd 
1466b8e788a5SAdrian Chadd 	/*
1467b8e788a5SAdrian Chadd 	 * Calculate miscellaneous flags.
1468b8e788a5SAdrian Chadd 	 */
1469b8e788a5SAdrian Chadd 	if (ismcast) {
1470b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_NOACK;	/* no ack on broad/multicast */
1471b8e788a5SAdrian Chadd 	} else if (pktlen > vap->iv_rtsthreshold &&
1472b8e788a5SAdrian Chadd 	    (ni->ni_ath_flags & IEEE80211_NODE_FF) == 0) {
1473b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_RTSENA;	/* RTS based on frame length */
1474b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_rts++;
1475b8e788a5SAdrian Chadd 	}
1476b8e788a5SAdrian Chadd 	if (flags & HAL_TXDESC_NOACK)		/* NB: avoid double counting */
1477b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_noack++;
1478b8e788a5SAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
1479b8e788a5SAdrian Chadd 	if (sc->sc_tdma && (flags & HAL_TXDESC_NOACK) == 0) {
1480b8e788a5SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_TDMA,
1481b8e788a5SAdrian Chadd 		    "%s: discard frame, ACK required w/ TDMA\n", __func__);
1482b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tdma_ack++;
1483b8e788a5SAdrian Chadd 		ath_freetx(m0);
1484b8e788a5SAdrian Chadd 		return EIO;
1485b8e788a5SAdrian Chadd 	}
1486b8e788a5SAdrian Chadd #endif
1487b8e788a5SAdrian Chadd 
1488b8e788a5SAdrian Chadd 	/*
1489eb6f0de0SAdrian Chadd 	 * Determine if a tx interrupt should be generated for
1490eb6f0de0SAdrian Chadd 	 * this descriptor.  We take a tx interrupt to reap
1491eb6f0de0SAdrian Chadd 	 * descriptors when the h/w hits an EOL condition or
1492eb6f0de0SAdrian Chadd 	 * when the descriptor is specifically marked to generate
1493eb6f0de0SAdrian Chadd 	 * an interrupt.  We periodically mark descriptors in this
1494eb6f0de0SAdrian Chadd 	 * way to insure timely replenishing of the supply needed
1495eb6f0de0SAdrian Chadd 	 * for sending frames.  Defering interrupts reduces system
1496eb6f0de0SAdrian Chadd 	 * load and potentially allows more concurrent work to be
1497eb6f0de0SAdrian Chadd 	 * done but if done to aggressively can cause senders to
1498eb6f0de0SAdrian Chadd 	 * backup.
1499eb6f0de0SAdrian Chadd 	 *
1500eb6f0de0SAdrian Chadd 	 * NB: use >= to deal with sc_txintrperiod changing
1501eb6f0de0SAdrian Chadd 	 *     dynamically through sysctl.
1502b8e788a5SAdrian Chadd 	 */
1503eb6f0de0SAdrian Chadd 	if (flags & HAL_TXDESC_INTREQ) {
1504eb6f0de0SAdrian Chadd 		txq->axq_intrcnt = 0;
1505eb6f0de0SAdrian Chadd 	} else if (++txq->axq_intrcnt >= sc->sc_txintrperiod) {
1506eb6f0de0SAdrian Chadd 		flags |= HAL_TXDESC_INTREQ;
1507eb6f0de0SAdrian Chadd 		txq->axq_intrcnt = 0;
1508eb6f0de0SAdrian Chadd 	}
1509e42b5dbaSAdrian Chadd 
1510eb6f0de0SAdrian Chadd 	/* This point forward is actual TX bits */
1511b8e788a5SAdrian Chadd 
1512b8e788a5SAdrian Chadd 	/*
1513b8e788a5SAdrian Chadd 	 * At this point we are committed to sending the frame
1514b8e788a5SAdrian Chadd 	 * and we don't need to look at m_nextpkt; clear it in
1515b8e788a5SAdrian Chadd 	 * case this frame is part of frag chain.
1516b8e788a5SAdrian Chadd 	 */
1517b8e788a5SAdrian Chadd 	m0->m_nextpkt = NULL;
1518b8e788a5SAdrian Chadd 
1519b8e788a5SAdrian Chadd 	if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
1520b8e788a5SAdrian Chadd 		ieee80211_dump_pkt(ic, mtod(m0, const uint8_t *), m0->m_len,
1521b8e788a5SAdrian Chadd 		    sc->sc_hwmap[rix].ieeerate, -1);
1522b8e788a5SAdrian Chadd 
1523b8e788a5SAdrian Chadd 	if (ieee80211_radiotap_active_vap(vap)) {
1524b8e788a5SAdrian Chadd 		u_int64_t tsf = ath_hal_gettsf64(ah);
1525b8e788a5SAdrian Chadd 
1526b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_tsf = htole64(tsf);
1527b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
1528b8e788a5SAdrian Chadd 		if (iswep)
1529b8e788a5SAdrian Chadd 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
1530b8e788a5SAdrian Chadd 		if (isfrag)
1531b8e788a5SAdrian Chadd 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
1532b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
1533b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_txpower = ni->ni_txpower;
1534b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
1535b8e788a5SAdrian Chadd 
1536b8e788a5SAdrian Chadd 		ieee80211_radiotap_tx(vap, m0);
1537b8e788a5SAdrian Chadd 	}
1538b8e788a5SAdrian Chadd 
1539eb6f0de0SAdrian Chadd 	/* Blank the legacy rate array */
1540eb6f0de0SAdrian Chadd 	bzero(&bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
1541c1782ce0SAdrian Chadd 
1542b8e788a5SAdrian Chadd 	/*
1543eb6f0de0SAdrian Chadd 	 * ath_buf_set_rate needs at least one rate/try to setup
1544eb6f0de0SAdrian Chadd 	 * the rate scenario.
1545b8e788a5SAdrian Chadd 	 */
1546eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].rix = rix;
1547eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].tries = try0;
1548eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].ratecode = txrate;
1549eb6f0de0SAdrian Chadd 
1550eb6f0de0SAdrian Chadd 	/* Store the decided rate index values away */
1551eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_pktlen = pktlen;
1552eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_hdrlen = hdrlen;
1553eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_atype = atype;
1554eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txpower = ni->ni_txpower;
1555eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txrate0 = txrate;
1556eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_try0 = try0;
1557eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_keyix = keyix;
1558eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txantenna = sc->sc_txantenna;
1559875a9451SAdrian Chadd 	bf->bf_state.bfs_txflags = flags;
1560eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_shpream = shortPreamble;
1561eb6f0de0SAdrian Chadd 
1562eb6f0de0SAdrian Chadd 	/* XXX this should be done in ath_tx_setrate() */
1563eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsrate0 = 0;	/* ie, no hard-coded ctsrate */
1564eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsrate = 0;	/* calculated later */
1565eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsduration = 0;
1566eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ismrr = ismrr;
1567eb6f0de0SAdrian Chadd 
1568eb6f0de0SAdrian Chadd 	return 0;
1569eb6f0de0SAdrian Chadd }
1570eb6f0de0SAdrian Chadd 
1571b8e788a5SAdrian Chadd /*
1572eb6f0de0SAdrian Chadd  * Direct-dispatch the current frame to the hardware.
1573eb6f0de0SAdrian Chadd  *
1574eb6f0de0SAdrian Chadd  * This can be called by the net80211 code.
1575eb6f0de0SAdrian Chadd  *
1576eb6f0de0SAdrian Chadd  * XXX what about locking? Or, push the seqno assign into the
1577eb6f0de0SAdrian Chadd  * XXX aggregate scheduler so its serialised?
1578b8e788a5SAdrian Chadd  */
1579eb6f0de0SAdrian Chadd int
1580eb6f0de0SAdrian Chadd ath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni,
1581eb6f0de0SAdrian Chadd     struct ath_buf *bf, struct mbuf *m0)
1582eb6f0de0SAdrian Chadd {
1583eb6f0de0SAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
1584eb6f0de0SAdrian Chadd 	struct ath_vap *avp = ATH_VAP(vap);
15859c85ff91SAdrian Chadd 	int r = 0;
1586eb6f0de0SAdrian Chadd 	u_int pri;
1587eb6f0de0SAdrian Chadd 	int tid;
1588eb6f0de0SAdrian Chadd 	struct ath_txq *txq;
1589eb6f0de0SAdrian Chadd 	int ismcast;
1590eb6f0de0SAdrian Chadd 	const struct ieee80211_frame *wh;
1591eb6f0de0SAdrian Chadd 	int is_ampdu, is_ampdu_tx, is_ampdu_pending;
1592a108d2d6SAdrian Chadd 	ieee80211_seq seqno;
1593eb6f0de0SAdrian Chadd 	uint8_t type, subtype;
1594eb6f0de0SAdrian Chadd 
1595eb6f0de0SAdrian Chadd 	/*
1596eb6f0de0SAdrian Chadd 	 * Determine the target hardware queue.
1597eb6f0de0SAdrian Chadd 	 *
1598b43facbfSAdrian Chadd 	 * For multicast frames, the txq gets overridden appropriately
1599b43facbfSAdrian Chadd 	 * depending upon the state of PS.
1600eb6f0de0SAdrian Chadd 	 *
1601eb6f0de0SAdrian Chadd 	 * For any other frame, we do a TID/QoS lookup inside the frame
1602eb6f0de0SAdrian Chadd 	 * to see what the TID should be. If it's a non-QoS frame, the
1603eb6f0de0SAdrian Chadd 	 * AC and TID are overridden. The TID/TXQ code assumes the
1604eb6f0de0SAdrian Chadd 	 * TID is on a predictable hardware TXQ, so we don't support
1605eb6f0de0SAdrian Chadd 	 * having a node TID queued to multiple hardware TXQs.
1606eb6f0de0SAdrian Chadd 	 * This may change in the future but would require some locking
1607eb6f0de0SAdrian Chadd 	 * fudgery.
1608eb6f0de0SAdrian Chadd 	 */
1609eb6f0de0SAdrian Chadd 	pri = ath_tx_getac(sc, m0);
1610eb6f0de0SAdrian Chadd 	tid = ath_tx_gettid(sc, m0);
1611eb6f0de0SAdrian Chadd 
1612eb6f0de0SAdrian Chadd 	txq = sc->sc_ac2q[pri];
1613eb6f0de0SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1614eb6f0de0SAdrian Chadd 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1615eb6f0de0SAdrian Chadd 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1616eb6f0de0SAdrian Chadd 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1617eb6f0de0SAdrian Chadd 
16189c85ff91SAdrian Chadd 	/*
16199c85ff91SAdrian Chadd 	 * Enforce how deep the multicast queue can grow.
16209c85ff91SAdrian Chadd 	 *
16219c85ff91SAdrian Chadd 	 * XXX duplicated in ath_raw_xmit().
16229c85ff91SAdrian Chadd 	 */
16239c85ff91SAdrian Chadd 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
16249c85ff91SAdrian Chadd 		ATH_TXQ_LOCK(sc->sc_cabq);
16259c85ff91SAdrian Chadd 
1626b09e37a1SAdrian Chadd 		if (sc->sc_cabq->axq_depth > sc->sc_txq_mcastq_maxdepth) {
16279c85ff91SAdrian Chadd 			sc->sc_stats.ast_tx_mcastq_overflow++;
16289c85ff91SAdrian Chadd 			r = ENOBUFS;
16299c85ff91SAdrian Chadd 		}
16309c85ff91SAdrian Chadd 
16319c85ff91SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_cabq);
16329c85ff91SAdrian Chadd 
16339c85ff91SAdrian Chadd 		if (r != 0) {
16349c85ff91SAdrian Chadd 			m_freem(m0);
16359c85ff91SAdrian Chadd 			return r;
16369c85ff91SAdrian Chadd 		}
16379c85ff91SAdrian Chadd 	}
16389c85ff91SAdrian Chadd 
1639eb6f0de0SAdrian Chadd 	/* A-MPDU TX */
1640eb6f0de0SAdrian Chadd 	is_ampdu_tx = ath_tx_ampdu_running(sc, ATH_NODE(ni), tid);
1641eb6f0de0SAdrian Chadd 	is_ampdu_pending = ath_tx_ampdu_pending(sc, ATH_NODE(ni), tid);
1642eb6f0de0SAdrian Chadd 	is_ampdu = is_ampdu_tx | is_ampdu_pending;
1643eb6f0de0SAdrian Chadd 
1644a108d2d6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d, ac=%d, is_ampdu=%d\n",
1645a108d2d6SAdrian Chadd 	    __func__, tid, pri, is_ampdu);
1646eb6f0de0SAdrian Chadd 
164746634305SAdrian Chadd 	/* Set local packet state, used to queue packets to hardware */
164846634305SAdrian Chadd 	bf->bf_state.bfs_tid = tid;
164946634305SAdrian Chadd 	bf->bf_state.bfs_txq = txq;
165046634305SAdrian Chadd 	bf->bf_state.bfs_pri = pri;
165146634305SAdrian Chadd 
1652c5940c30SAdrian Chadd 	/*
1653b43facbfSAdrian Chadd 	 * When servicing one or more stations in power-save mode
1654b43facbfSAdrian Chadd 	 * (or) if there is some mcast data waiting on the mcast
1655b43facbfSAdrian Chadd 	 * queue (to prevent out of order delivery) multicast frames
1656b43facbfSAdrian Chadd 	 * must be bufferd until after the beacon.
1657b43facbfSAdrian Chadd 	 *
1658b43facbfSAdrian Chadd 	 * TODO: we should lock the mcastq before we check the length.
1659c5940c30SAdrian Chadd 	 */
166046634305SAdrian Chadd 	if (ismcast && (vap->iv_ps_sta || avp->av_mcastq.axq_depth)) {
1661eb6f0de0SAdrian Chadd 		txq = &avp->av_mcastq;
166246634305SAdrian Chadd 		/*
166346634305SAdrian Chadd 		 * Mark the frame as eventually belonging on the CAB
166446634305SAdrian Chadd 		 * queue, so the descriptor setup functions will
166546634305SAdrian Chadd 		 * correctly initialise the descriptor 'qcuId' field.
166646634305SAdrian Chadd 		 */
166746634305SAdrian Chadd 		bf->bf_state.bfs_txq = sc->sc_cabq;
166846634305SAdrian Chadd 	}
1669eb6f0de0SAdrian Chadd 
1670eb6f0de0SAdrian Chadd 	/* Do the generic frame setup */
1671eb6f0de0SAdrian Chadd 	/* XXX should just bzero the bf_state? */
1672eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_dobaw = 0;
1673eb6f0de0SAdrian Chadd 
16747561cb5cSAdrian Chadd 	/*
16757561cb5cSAdrian Chadd 	 * Acquire the TXQ lock early, so both the encap and seqno
16767561cb5cSAdrian Chadd 	 * are allocated together.
167746634305SAdrian Chadd 	 *
167846634305SAdrian Chadd 	 * XXX should TXQ for CABQ traffic be the multicast queue,
167946634305SAdrian Chadd 	 * or the TXQ the given PRI would allocate from? (eg for
168046634305SAdrian Chadd 	 * sequence number allocation locking.)
16817561cb5cSAdrian Chadd 	 */
1682eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(txq);
16837561cb5cSAdrian Chadd 
16847561cb5cSAdrian Chadd 	/* A-MPDU TX? Manually set sequence number */
16857561cb5cSAdrian Chadd 	/*
16867561cb5cSAdrian Chadd 	 * Don't do it whilst pending; the net80211 layer still
16877561cb5cSAdrian Chadd 	 * assigns them.
16887561cb5cSAdrian Chadd 	 */
16897561cb5cSAdrian Chadd 	if (is_ampdu_tx) {
1690eb6f0de0SAdrian Chadd 		/*
1691eb6f0de0SAdrian Chadd 		 * Always call; this function will
1692eb6f0de0SAdrian Chadd 		 * handle making sure that null data frames
1693eb6f0de0SAdrian Chadd 		 * don't get a sequence number from the current
1694eb6f0de0SAdrian Chadd 		 * TID and thus mess with the BAW.
1695eb6f0de0SAdrian Chadd 		 */
1696a108d2d6SAdrian Chadd 		seqno = ath_tx_tid_seqno_assign(sc, ni, bf, m0);
169742f4d061SAdrian Chadd 
169842f4d061SAdrian Chadd 		/*
169942f4d061SAdrian Chadd 		 * Don't add QoS NULL frames to the BAW.
170042f4d061SAdrian Chadd 		 */
1701a108d2d6SAdrian Chadd 		if (IEEE80211_QOS_HAS_SEQ(wh) &&
1702a108d2d6SAdrian Chadd 		    subtype != IEEE80211_FC0_SUBTYPE_QOS_NULL) {
1703eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_dobaw = 1;
1704eb6f0de0SAdrian Chadd 		}
1705c1782ce0SAdrian Chadd 	}
1706c1782ce0SAdrian Chadd 
1707eb6f0de0SAdrian Chadd 	/*
1708eb6f0de0SAdrian Chadd 	 * If needed, the sequence number has been assigned.
1709eb6f0de0SAdrian Chadd 	 * Squirrel it away somewhere easy to get to.
1710eb6f0de0SAdrian Chadd 	 */
1711a108d2d6SAdrian Chadd 	bf->bf_state.bfs_seqno = M_SEQNO_GET(m0) << IEEE80211_SEQ_SEQ_SHIFT;
1712b8e788a5SAdrian Chadd 
1713eb6f0de0SAdrian Chadd 	/* Is ampdu pending? fetch the seqno and print it out */
1714eb6f0de0SAdrian Chadd 	if (is_ampdu_pending)
1715eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX,
1716eb6f0de0SAdrian Chadd 		    "%s: tid %d: ampdu pending, seqno %d\n",
1717eb6f0de0SAdrian Chadd 		    __func__, tid, M_SEQNO_GET(m0));
1718eb6f0de0SAdrian Chadd 
1719eb6f0de0SAdrian Chadd 	/* This also sets up the DMA map */
1720b43facbfSAdrian Chadd 	r = ath_tx_normal_setup(sc, ni, bf, m0, txq);
1721eb6f0de0SAdrian Chadd 
1722eb6f0de0SAdrian Chadd 	if (r != 0)
17237561cb5cSAdrian Chadd 		goto done;
1724eb6f0de0SAdrian Chadd 
1725eb6f0de0SAdrian Chadd 	/* At this point m0 could have changed! */
1726eb6f0de0SAdrian Chadd 	m0 = bf->bf_m;
1727eb6f0de0SAdrian Chadd 
1728eb6f0de0SAdrian Chadd #if 1
1729eb6f0de0SAdrian Chadd 	/*
1730eb6f0de0SAdrian Chadd 	 * If it's a multicast frame, do a direct-dispatch to the
1731eb6f0de0SAdrian Chadd 	 * destination hardware queue. Don't bother software
1732eb6f0de0SAdrian Chadd 	 * queuing it.
1733eb6f0de0SAdrian Chadd 	 */
1734eb6f0de0SAdrian Chadd 	/*
1735eb6f0de0SAdrian Chadd 	 * If it's a BAR frame, do a direct dispatch to the
1736eb6f0de0SAdrian Chadd 	 * destination hardware queue. Don't bother software
1737eb6f0de0SAdrian Chadd 	 * queuing it, as the TID will now be paused.
1738eb6f0de0SAdrian Chadd 	 * Sending a BAR frame can occur from the net80211 txa timer
1739eb6f0de0SAdrian Chadd 	 * (ie, retries) or from the ath txtask (completion call.)
1740eb6f0de0SAdrian Chadd 	 * It queues directly to hardware because the TID is paused
1741eb6f0de0SAdrian Chadd 	 * at this point (and won't be unpaused until the BAR has
1742eb6f0de0SAdrian Chadd 	 * either been TXed successfully or max retries has been
1743eb6f0de0SAdrian Chadd 	 * reached.)
1744eb6f0de0SAdrian Chadd 	 */
1745eb6f0de0SAdrian Chadd 	if (txq == &avp->av_mcastq) {
1746d3a6425bSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX,
17470b96ef63SAdrian Chadd 		    "%s: bf=%p: mcastq: TX'ing\n", __func__, bf);
1748eb6f0de0SAdrian Chadd 		ath_tx_xmit_normal(sc, txq, bf);
1749eb6f0de0SAdrian Chadd 	} else if (type == IEEE80211_FC0_TYPE_CTL &&
1750eb6f0de0SAdrian Chadd 		    subtype == IEEE80211_FC0_SUBTYPE_BAR) {
1751d3a6425bSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX,
1752eb6f0de0SAdrian Chadd 		    "%s: BAR: TX'ing direct\n", __func__);
1753eb6f0de0SAdrian Chadd 		ath_tx_xmit_normal(sc, txq, bf);
1754eb6f0de0SAdrian Chadd 	} else {
1755eb6f0de0SAdrian Chadd 		/* add to software queue */
1756d3a6425bSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX,
17570b96ef63SAdrian Chadd 		    "%s: bf=%p: swq: TX'ing\n", __func__, bf);
1758eb6f0de0SAdrian Chadd 		ath_tx_swq(sc, ni, txq, bf);
1759eb6f0de0SAdrian Chadd 	}
1760eb6f0de0SAdrian Chadd #else
1761eb6f0de0SAdrian Chadd 	/*
1762eb6f0de0SAdrian Chadd 	 * For now, since there's no software queue,
1763eb6f0de0SAdrian Chadd 	 * direct-dispatch to the hardware.
1764eb6f0de0SAdrian Chadd 	 */
1765eb6f0de0SAdrian Chadd 	ath_tx_xmit_normal(sc, txq, bf);
1766eb6f0de0SAdrian Chadd #endif
17677561cb5cSAdrian Chadd done:
17687561cb5cSAdrian Chadd 	ATH_TXQ_UNLOCK(txq);
1769eb6f0de0SAdrian Chadd 
1770b8e788a5SAdrian Chadd 	return 0;
1771b8e788a5SAdrian Chadd }
1772b8e788a5SAdrian Chadd 
1773b8e788a5SAdrian Chadd static int
1774b8e788a5SAdrian Chadd ath_tx_raw_start(struct ath_softc *sc, struct ieee80211_node *ni,
1775b8e788a5SAdrian Chadd 	struct ath_buf *bf, struct mbuf *m0,
1776b8e788a5SAdrian Chadd 	const struct ieee80211_bpf_params *params)
1777b8e788a5SAdrian Chadd {
1778b8e788a5SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
1779b8e788a5SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
1780b8e788a5SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
1781b8e788a5SAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
1782b8e788a5SAdrian Chadd 	int error, ismcast, ismrr;
1783b8e788a5SAdrian Chadd 	int keyix, hdrlen, pktlen, try0, txantenna;
1784eb6f0de0SAdrian Chadd 	u_int8_t rix, txrate;
1785b8e788a5SAdrian Chadd 	struct ieee80211_frame *wh;
1786eb6f0de0SAdrian Chadd 	u_int flags;
1787b8e788a5SAdrian Chadd 	HAL_PKT_TYPE atype;
1788b8e788a5SAdrian Chadd 	const HAL_RATE_TABLE *rt;
1789b8e788a5SAdrian Chadd 	struct ath_desc *ds;
1790b8e788a5SAdrian Chadd 	u_int pri;
1791eb6f0de0SAdrian Chadd 	int o_tid = -1;
1792eb6f0de0SAdrian Chadd 	int do_override;
1793b8e788a5SAdrian Chadd 
1794b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1795b8e788a5SAdrian Chadd 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1796b8e788a5SAdrian Chadd 	hdrlen = ieee80211_anyhdrsize(wh);
1797b8e788a5SAdrian Chadd 	/*
1798b8e788a5SAdrian Chadd 	 * Packet length must not include any
1799b8e788a5SAdrian Chadd 	 * pad bytes; deduct them here.
1800b8e788a5SAdrian Chadd 	 */
1801b8e788a5SAdrian Chadd 	/* XXX honor IEEE80211_BPF_DATAPAD */
1802b8e788a5SAdrian Chadd 	pktlen = m0->m_pkthdr.len - (hdrlen & 3) + IEEE80211_CRC_LEN;
1803b8e788a5SAdrian Chadd 
1804eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: ismcast=%d\n",
1805eb6f0de0SAdrian Chadd 	    __func__, ismcast);
1806eb6f0de0SAdrian Chadd 
18077561cb5cSAdrian Chadd 	pri = params->ibp_pri & 3;
18087561cb5cSAdrian Chadd 	/* Override pri if the frame isn't a QoS one */
18097561cb5cSAdrian Chadd 	if (! IEEE80211_QOS_HAS_SEQ(wh))
18107561cb5cSAdrian Chadd 		pri = ath_tx_getac(sc, m0);
18117561cb5cSAdrian Chadd 
18127561cb5cSAdrian Chadd 	/* XXX If it's an ADDBA, override the correct queue */
18137561cb5cSAdrian Chadd 	do_override = ath_tx_action_frame_override_queue(sc, ni, m0, &o_tid);
18147561cb5cSAdrian Chadd 
18157561cb5cSAdrian Chadd 	/* Map ADDBA to the correct priority */
18167561cb5cSAdrian Chadd 	if (do_override) {
18177561cb5cSAdrian Chadd #if 0
18187561cb5cSAdrian Chadd 		device_printf(sc->sc_dev,
18197561cb5cSAdrian Chadd 		    "%s: overriding tid %d pri %d -> %d\n",
18207561cb5cSAdrian Chadd 		    __func__, o_tid, pri, TID_TO_WME_AC(o_tid));
18217561cb5cSAdrian Chadd #endif
18227561cb5cSAdrian Chadd 		pri = TID_TO_WME_AC(o_tid);
18237561cb5cSAdrian Chadd 	}
18247561cb5cSAdrian Chadd 
18257561cb5cSAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[pri]);
18267561cb5cSAdrian Chadd 
182781a82688SAdrian Chadd 	/* Handle encryption twiddling if needed */
1828eb6f0de0SAdrian Chadd 	if (! ath_tx_tag_crypto(sc, ni,
1829eb6f0de0SAdrian Chadd 	    m0, params->ibp_flags & IEEE80211_BPF_CRYPTO, 0,
1830eb6f0de0SAdrian Chadd 	    &hdrlen, &pktlen, &keyix)) {
1831b8e788a5SAdrian Chadd 		ath_freetx(m0);
1832b8e788a5SAdrian Chadd 		return EIO;
1833b8e788a5SAdrian Chadd 	}
1834b8e788a5SAdrian Chadd 	/* packet header may have moved, reset our local pointer */
1835b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1836b8e788a5SAdrian Chadd 
1837eb6f0de0SAdrian Chadd 	/* Do the generic frame setup */
1838eb6f0de0SAdrian Chadd 	/* XXX should just bzero the bf_state? */
1839eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_dobaw = 0;
1840eb6f0de0SAdrian Chadd 
1841b8e788a5SAdrian Chadd 	error = ath_tx_dmasetup(sc, bf, m0);
1842b8e788a5SAdrian Chadd 	if (error != 0)
1843b8e788a5SAdrian Chadd 		return error;
1844b8e788a5SAdrian Chadd 	m0 = bf->bf_m;				/* NB: may have changed */
1845b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1846b8e788a5SAdrian Chadd 	bf->bf_node = ni;			/* NB: held reference */
1847b8e788a5SAdrian Chadd 
1848b8e788a5SAdrian Chadd 	flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for crypto errs */
1849b8e788a5SAdrian Chadd 	flags |= HAL_TXDESC_INTREQ;		/* force interrupt */
1850b8e788a5SAdrian Chadd 	if (params->ibp_flags & IEEE80211_BPF_RTS)
1851b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_RTSENA;
1852eb6f0de0SAdrian Chadd 	else if (params->ibp_flags & IEEE80211_BPF_CTS) {
1853eb6f0de0SAdrian Chadd 		/* XXX assume 11g/11n protection? */
1854eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_doprot = 1;
1855b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_CTSENA;
1856eb6f0de0SAdrian Chadd 	}
1857b8e788a5SAdrian Chadd 	/* XXX leave ismcast to injector? */
1858b8e788a5SAdrian Chadd 	if ((params->ibp_flags & IEEE80211_BPF_NOACK) || ismcast)
1859b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_NOACK;
1860b8e788a5SAdrian Chadd 
1861b8e788a5SAdrian Chadd 	rt = sc->sc_currates;
1862b8e788a5SAdrian Chadd 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
1863b8e788a5SAdrian Chadd 	rix = ath_tx_findrix(sc, params->ibp_rate0);
1864b8e788a5SAdrian Chadd 	txrate = rt->info[rix].rateCode;
1865b8e788a5SAdrian Chadd 	if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
1866b8e788a5SAdrian Chadd 		txrate |= rt->info[rix].shortPreamble;
1867b8e788a5SAdrian Chadd 	sc->sc_txrix = rix;
1868b8e788a5SAdrian Chadd 	try0 = params->ibp_try0;
1869b8e788a5SAdrian Chadd 	ismrr = (params->ibp_try1 != 0);
1870b8e788a5SAdrian Chadd 	txantenna = params->ibp_pri >> 2;
1871b8e788a5SAdrian Chadd 	if (txantenna == 0)			/* XXX? */
1872b8e788a5SAdrian Chadd 		txantenna = sc->sc_txantenna;
187379f02dbfSAdrian Chadd 
187479f02dbfSAdrian Chadd 	/*
1875eb6f0de0SAdrian Chadd 	 * Since ctsrate is fixed, store it away for later
1876eb6f0de0SAdrian Chadd 	 * use when the descriptor fields are being set.
187779f02dbfSAdrian Chadd 	 */
1878eb6f0de0SAdrian Chadd 	if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA))
1879eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_ctsrate0 = params->ibp_ctsrate;
188079f02dbfSAdrian Chadd 
1881b8e788a5SAdrian Chadd 	/*
1882b8e788a5SAdrian Chadd 	 * NB: we mark all packets as type PSPOLL so the h/w won't
1883b8e788a5SAdrian Chadd 	 * set the sequence number, duration, etc.
1884b8e788a5SAdrian Chadd 	 */
1885b8e788a5SAdrian Chadd 	atype = HAL_PKT_TYPE_PSPOLL;
1886b8e788a5SAdrian Chadd 
1887b8e788a5SAdrian Chadd 	if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
1888b8e788a5SAdrian Chadd 		ieee80211_dump_pkt(ic, mtod(m0, caddr_t), m0->m_len,
1889b8e788a5SAdrian Chadd 		    sc->sc_hwmap[rix].ieeerate, -1);
1890b8e788a5SAdrian Chadd 
1891b8e788a5SAdrian Chadd 	if (ieee80211_radiotap_active_vap(vap)) {
1892b8e788a5SAdrian Chadd 		u_int64_t tsf = ath_hal_gettsf64(ah);
1893b8e788a5SAdrian Chadd 
1894b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_tsf = htole64(tsf);
1895b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
1896b8e788a5SAdrian Chadd 		if (wh->i_fc[1] & IEEE80211_FC1_WEP)
1897b8e788a5SAdrian Chadd 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
1898b8e788a5SAdrian Chadd 		if (m0->m_flags & M_FRAG)
1899b8e788a5SAdrian Chadd 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
1900b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
1901b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_txpower = ni->ni_txpower;
1902b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
1903b8e788a5SAdrian Chadd 
1904b8e788a5SAdrian Chadd 		ieee80211_radiotap_tx(vap, m0);
1905b8e788a5SAdrian Chadd 	}
1906b8e788a5SAdrian Chadd 
1907b8e788a5SAdrian Chadd 	/*
1908b8e788a5SAdrian Chadd 	 * Formulate first tx descriptor with tx controls.
1909b8e788a5SAdrian Chadd 	 */
1910b8e788a5SAdrian Chadd 	ds = bf->bf_desc;
1911b8e788a5SAdrian Chadd 	/* XXX check return value? */
1912eb6f0de0SAdrian Chadd 
1913eb6f0de0SAdrian Chadd 	/* Store the decided rate index values away */
1914eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_pktlen = pktlen;
1915eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_hdrlen = hdrlen;
1916eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_atype = atype;
1917eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txpower = params->ibp_power;
1918eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txrate0 = txrate;
1919eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_try0 = try0;
1920eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_keyix = keyix;
1921eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txantenna = txantenna;
1922875a9451SAdrian Chadd 	bf->bf_state.bfs_txflags = flags;
1923eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_shpream =
1924eb6f0de0SAdrian Chadd 	    !! (params->ibp_flags & IEEE80211_BPF_SHORTPRE);
1925b8e788a5SAdrian Chadd 
192646634305SAdrian Chadd 	/* Set local packet state, used to queue packets to hardware */
192746634305SAdrian Chadd 	bf->bf_state.bfs_tid = WME_AC_TO_TID(pri);
192846634305SAdrian Chadd 	bf->bf_state.bfs_txq = sc->sc_ac2q[pri];
192946634305SAdrian Chadd 	bf->bf_state.bfs_pri = pri;
193046634305SAdrian Chadd 
1931eb6f0de0SAdrian Chadd 	/* XXX this should be done in ath_tx_setrate() */
1932eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsrate = 0;
1933eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsduration = 0;
1934eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ismrr = ismrr;
1935eb6f0de0SAdrian Chadd 
1936eb6f0de0SAdrian Chadd 	/* Blank the legacy rate array */
1937eb6f0de0SAdrian Chadd 	bzero(&bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
1938eb6f0de0SAdrian Chadd 
1939eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].rix =
1940eb6f0de0SAdrian Chadd 	    ath_tx_findrix(sc, params->ibp_rate0);
1941eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].tries = try0;
1942eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].ratecode = txrate;
1943c1782ce0SAdrian Chadd 
1944c1782ce0SAdrian Chadd 	if (ismrr) {
1945eb6f0de0SAdrian Chadd 		int rix;
1946c1782ce0SAdrian Chadd 
1947b8e788a5SAdrian Chadd 		rix = ath_tx_findrix(sc, params->ibp_rate1);
1948eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[1].rix = rix;
1949eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[1].tries = params->ibp_try1;
1950c1782ce0SAdrian Chadd 
1951eb6f0de0SAdrian Chadd 		rix = ath_tx_findrix(sc, params->ibp_rate2);
1952eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[2].rix = rix;
1953eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[2].tries = params->ibp_try2;
1954eb6f0de0SAdrian Chadd 
1955eb6f0de0SAdrian Chadd 		rix = ath_tx_findrix(sc, params->ibp_rate3);
1956eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[3].rix = rix;
1957eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[3].tries = params->ibp_try3;
1958c1782ce0SAdrian Chadd 	}
1959eb6f0de0SAdrian Chadd 	/*
1960eb6f0de0SAdrian Chadd 	 * All the required rate control decisions have been made;
1961eb6f0de0SAdrian Chadd 	 * fill in the rc flags.
1962eb6f0de0SAdrian Chadd 	 */
1963eb6f0de0SAdrian Chadd 	ath_tx_rate_fill_rcflags(sc, bf);
1964b8e788a5SAdrian Chadd 
1965b8e788a5SAdrian Chadd 	/* NB: no buffered multicast in power save support */
1966eb6f0de0SAdrian Chadd 
1967eb6f0de0SAdrian Chadd 	/*
1968eb6f0de0SAdrian Chadd 	 * If we're overiding the ADDBA destination, dump directly
1969eb6f0de0SAdrian Chadd 	 * into the hardware queue, right after any pending
1970eb6f0de0SAdrian Chadd 	 * frames to that node are.
1971eb6f0de0SAdrian Chadd 	 */
1972eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: dooverride=%d\n",
1973eb6f0de0SAdrian Chadd 	    __func__, do_override);
1974eb6f0de0SAdrian Chadd 
1975eb6f0de0SAdrian Chadd 	if (do_override) {
1976eb6f0de0SAdrian Chadd 		ath_tx_xmit_normal(sc, sc->sc_ac2q[pri], bf);
1977eb6f0de0SAdrian Chadd 	} else {
1978eb6f0de0SAdrian Chadd 		/* Queue to software queue */
1979eb6f0de0SAdrian Chadd 		ath_tx_swq(sc, ni, sc->sc_ac2q[pri], bf);
1980eb6f0de0SAdrian Chadd 	}
19817561cb5cSAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[pri]);
1982eb6f0de0SAdrian Chadd 
1983b8e788a5SAdrian Chadd 	return 0;
1984b8e788a5SAdrian Chadd }
1985b8e788a5SAdrian Chadd 
1986eb6f0de0SAdrian Chadd /*
1987eb6f0de0SAdrian Chadd  * Send a raw frame.
1988eb6f0de0SAdrian Chadd  *
1989eb6f0de0SAdrian Chadd  * This can be called by net80211.
1990eb6f0de0SAdrian Chadd  */
1991b8e788a5SAdrian Chadd int
1992b8e788a5SAdrian Chadd ath_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1993b8e788a5SAdrian Chadd 	const struct ieee80211_bpf_params *params)
1994b8e788a5SAdrian Chadd {
1995b8e788a5SAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
1996b8e788a5SAdrian Chadd 	struct ifnet *ifp = ic->ic_ifp;
1997b8e788a5SAdrian Chadd 	struct ath_softc *sc = ifp->if_softc;
1998b8e788a5SAdrian Chadd 	struct ath_buf *bf;
19999c85ff91SAdrian Chadd 	struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
20009c85ff91SAdrian Chadd 	int error = 0;
2001b8e788a5SAdrian Chadd 
2002ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2003ef27340cSAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
2004ef27340cSAdrian Chadd 		device_printf(sc->sc_dev, "%s: sc_inreset_cnt > 0; bailing\n",
2005ef27340cSAdrian Chadd 		    __func__);
2006ef27340cSAdrian Chadd 		error = EIO;
2007ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2008ef27340cSAdrian Chadd 		goto bad0;
2009ef27340cSAdrian Chadd 	}
2010ef27340cSAdrian Chadd 	sc->sc_txstart_cnt++;
2011ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2012ef27340cSAdrian Chadd 
2013b8e788a5SAdrian Chadd 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->sc_invalid) {
2014b8e788a5SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: discard frame, %s", __func__,
2015b8e788a5SAdrian Chadd 		    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ?
2016b8e788a5SAdrian Chadd 			"!running" : "invalid");
2017b8e788a5SAdrian Chadd 		m_freem(m);
2018b8e788a5SAdrian Chadd 		error = ENETDOWN;
2019b8e788a5SAdrian Chadd 		goto bad;
2020b8e788a5SAdrian Chadd 	}
20219c85ff91SAdrian Chadd 
20229c85ff91SAdrian Chadd 	/*
20239c85ff91SAdrian Chadd 	 * Enforce how deep the multicast queue can grow.
20249c85ff91SAdrian Chadd 	 *
20259c85ff91SAdrian Chadd 	 * XXX duplicated in ath_tx_start().
20269c85ff91SAdrian Chadd 	 */
20279c85ff91SAdrian Chadd 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
20289c85ff91SAdrian Chadd 		ATH_TXQ_LOCK(sc->sc_cabq);
20299c85ff91SAdrian Chadd 
2030b09e37a1SAdrian Chadd 		if (sc->sc_cabq->axq_depth > sc->sc_txq_mcastq_maxdepth) {
20319c85ff91SAdrian Chadd 			sc->sc_stats.ast_tx_mcastq_overflow++;
20329c85ff91SAdrian Chadd 			error = ENOBUFS;
20339c85ff91SAdrian Chadd 		}
20349c85ff91SAdrian Chadd 
20359c85ff91SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_cabq);
20369c85ff91SAdrian Chadd 
20379c85ff91SAdrian Chadd 		if (error != 0) {
20389c85ff91SAdrian Chadd 			m_freem(m);
20399c85ff91SAdrian Chadd 			goto bad;
20409c85ff91SAdrian Chadd 		}
20419c85ff91SAdrian Chadd 	}
20429c85ff91SAdrian Chadd 
2043b8e788a5SAdrian Chadd 	/*
2044b8e788a5SAdrian Chadd 	 * Grab a TX buffer and associated resources.
2045b8e788a5SAdrian Chadd 	 */
2046af33d486SAdrian Chadd 	bf = ath_getbuf(sc, ATH_BUFTYPE_MGMT);
2047b8e788a5SAdrian Chadd 	if (bf == NULL) {
2048b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_nobuf++;
2049b8e788a5SAdrian Chadd 		m_freem(m);
2050b8e788a5SAdrian Chadd 		error = ENOBUFS;
2051b8e788a5SAdrian Chadd 		goto bad;
2052b8e788a5SAdrian Chadd 	}
2053b8e788a5SAdrian Chadd 
2054b8e788a5SAdrian Chadd 	if (params == NULL) {
2055b8e788a5SAdrian Chadd 		/*
2056b8e788a5SAdrian Chadd 		 * Legacy path; interpret frame contents to decide
2057b8e788a5SAdrian Chadd 		 * precisely how to send the frame.
2058b8e788a5SAdrian Chadd 		 */
2059b8e788a5SAdrian Chadd 		if (ath_tx_start(sc, ni, bf, m)) {
2060b8e788a5SAdrian Chadd 			error = EIO;		/* XXX */
2061b8e788a5SAdrian Chadd 			goto bad2;
2062b8e788a5SAdrian Chadd 		}
2063b8e788a5SAdrian Chadd 	} else {
2064b8e788a5SAdrian Chadd 		/*
2065b8e788a5SAdrian Chadd 		 * Caller supplied explicit parameters to use in
2066b8e788a5SAdrian Chadd 		 * sending the frame.
2067b8e788a5SAdrian Chadd 		 */
2068b8e788a5SAdrian Chadd 		if (ath_tx_raw_start(sc, ni, bf, m, params)) {
2069b8e788a5SAdrian Chadd 			error = EIO;		/* XXX */
2070b8e788a5SAdrian Chadd 			goto bad2;
2071b8e788a5SAdrian Chadd 		}
2072b8e788a5SAdrian Chadd 	}
2073b8e788a5SAdrian Chadd 	sc->sc_wd_timer = 5;
2074b8e788a5SAdrian Chadd 	ifp->if_opackets++;
2075b8e788a5SAdrian Chadd 	sc->sc_stats.ast_tx_raw++;
2076b8e788a5SAdrian Chadd 
2077ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2078ef27340cSAdrian Chadd 	sc->sc_txstart_cnt--;
2079ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2080ef27340cSAdrian Chadd 
2081b8e788a5SAdrian Chadd 	return 0;
2082b8e788a5SAdrian Chadd bad2:
2083b8e788a5SAdrian Chadd 	ATH_TXBUF_LOCK(sc);
2084e1a50456SAdrian Chadd 	ath_returnbuf_head(sc, bf);
2085b8e788a5SAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
2086b8e788a5SAdrian Chadd bad:
2087ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2088ef27340cSAdrian Chadd 	sc->sc_txstart_cnt--;
2089ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2090ef27340cSAdrian Chadd bad0:
2091b8e788a5SAdrian Chadd 	ifp->if_oerrors++;
2092b8e788a5SAdrian Chadd 	sc->sc_stats.ast_tx_raw_fail++;
2093b8e788a5SAdrian Chadd 	ieee80211_free_node(ni);
2094ef27340cSAdrian Chadd 
2095b8e788a5SAdrian Chadd 	return error;
2096b8e788a5SAdrian Chadd }
2097eb6f0de0SAdrian Chadd 
2098eb6f0de0SAdrian Chadd /* Some helper functions */
2099eb6f0de0SAdrian Chadd 
2100eb6f0de0SAdrian Chadd /*
2101eb6f0de0SAdrian Chadd  * ADDBA (and potentially others) need to be placed in the same
2102eb6f0de0SAdrian Chadd  * hardware queue as the TID/node it's relating to. This is so
2103eb6f0de0SAdrian Chadd  * it goes out after any pending non-aggregate frames to the
2104eb6f0de0SAdrian Chadd  * same node/TID.
2105eb6f0de0SAdrian Chadd  *
2106eb6f0de0SAdrian Chadd  * If this isn't done, the ADDBA can go out before the frames
2107eb6f0de0SAdrian Chadd  * queued in hardware. Even though these frames have a sequence
2108eb6f0de0SAdrian Chadd  * number -earlier- than the ADDBA can be transmitted (but
2109eb6f0de0SAdrian Chadd  * no frames whose sequence numbers are after the ADDBA should
2110eb6f0de0SAdrian Chadd  * be!) they'll arrive after the ADDBA - and the receiving end
2111eb6f0de0SAdrian Chadd  * will simply drop them as being out of the BAW.
2112eb6f0de0SAdrian Chadd  *
2113eb6f0de0SAdrian Chadd  * The frames can't be appended to the TID software queue - it'll
2114eb6f0de0SAdrian Chadd  * never be sent out. So these frames have to be directly
2115eb6f0de0SAdrian Chadd  * dispatched to the hardware, rather than queued in software.
2116eb6f0de0SAdrian Chadd  * So if this function returns true, the TXQ has to be
2117eb6f0de0SAdrian Chadd  * overridden and it has to be directly dispatched.
2118eb6f0de0SAdrian Chadd  *
2119eb6f0de0SAdrian Chadd  * It's a dirty hack, but someone's gotta do it.
2120eb6f0de0SAdrian Chadd  */
2121eb6f0de0SAdrian Chadd 
2122eb6f0de0SAdrian Chadd /*
2123eb6f0de0SAdrian Chadd  * XXX doesn't belong here!
2124eb6f0de0SAdrian Chadd  */
2125eb6f0de0SAdrian Chadd static int
2126eb6f0de0SAdrian Chadd ieee80211_is_action(struct ieee80211_frame *wh)
2127eb6f0de0SAdrian Chadd {
2128eb6f0de0SAdrian Chadd 	/* Type: Management frame? */
2129eb6f0de0SAdrian Chadd 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) !=
2130eb6f0de0SAdrian Chadd 	    IEEE80211_FC0_TYPE_MGT)
2131eb6f0de0SAdrian Chadd 		return 0;
2132eb6f0de0SAdrian Chadd 
2133eb6f0de0SAdrian Chadd 	/* Subtype: Action frame? */
2134eb6f0de0SAdrian Chadd 	if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) !=
2135eb6f0de0SAdrian Chadd 	    IEEE80211_FC0_SUBTYPE_ACTION)
2136eb6f0de0SAdrian Chadd 		return 0;
2137eb6f0de0SAdrian Chadd 
2138eb6f0de0SAdrian Chadd 	return 1;
2139eb6f0de0SAdrian Chadd }
2140eb6f0de0SAdrian Chadd 
2141eb6f0de0SAdrian Chadd #define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
2142eb6f0de0SAdrian Chadd /*
2143eb6f0de0SAdrian Chadd  * Return an alternate TID for ADDBA request frames.
2144eb6f0de0SAdrian Chadd  *
2145eb6f0de0SAdrian Chadd  * Yes, this likely should be done in the net80211 layer.
2146eb6f0de0SAdrian Chadd  */
2147eb6f0de0SAdrian Chadd static int
2148eb6f0de0SAdrian Chadd ath_tx_action_frame_override_queue(struct ath_softc *sc,
2149eb6f0de0SAdrian Chadd     struct ieee80211_node *ni,
2150eb6f0de0SAdrian Chadd     struct mbuf *m0, int *tid)
2151eb6f0de0SAdrian Chadd {
2152eb6f0de0SAdrian Chadd 	struct ieee80211_frame *wh = mtod(m0, struct ieee80211_frame *);
2153eb6f0de0SAdrian Chadd 	struct ieee80211_action_ba_addbarequest *ia;
2154eb6f0de0SAdrian Chadd 	uint8_t *frm;
2155eb6f0de0SAdrian Chadd 	uint16_t baparamset;
2156eb6f0de0SAdrian Chadd 
2157eb6f0de0SAdrian Chadd 	/* Not action frame? Bail */
2158eb6f0de0SAdrian Chadd 	if (! ieee80211_is_action(wh))
2159eb6f0de0SAdrian Chadd 		return 0;
2160eb6f0de0SAdrian Chadd 
2161eb6f0de0SAdrian Chadd 	/* XXX Not needed for frames we send? */
2162eb6f0de0SAdrian Chadd #if 0
2163eb6f0de0SAdrian Chadd 	/* Correct length? */
2164eb6f0de0SAdrian Chadd 	if (! ieee80211_parse_action(ni, m))
2165eb6f0de0SAdrian Chadd 		return 0;
2166eb6f0de0SAdrian Chadd #endif
2167eb6f0de0SAdrian Chadd 
2168eb6f0de0SAdrian Chadd 	/* Extract out action frame */
2169eb6f0de0SAdrian Chadd 	frm = (u_int8_t *)&wh[1];
2170eb6f0de0SAdrian Chadd 	ia = (struct ieee80211_action_ba_addbarequest *) frm;
2171eb6f0de0SAdrian Chadd 
2172eb6f0de0SAdrian Chadd 	/* Not ADDBA? Bail */
2173eb6f0de0SAdrian Chadd 	if (ia->rq_header.ia_category != IEEE80211_ACTION_CAT_BA)
2174eb6f0de0SAdrian Chadd 		return 0;
2175eb6f0de0SAdrian Chadd 	if (ia->rq_header.ia_action != IEEE80211_ACTION_BA_ADDBA_REQUEST)
2176eb6f0de0SAdrian Chadd 		return 0;
2177eb6f0de0SAdrian Chadd 
2178eb6f0de0SAdrian Chadd 	/* Extract TID, return it */
2179eb6f0de0SAdrian Chadd 	baparamset = le16toh(ia->rq_baparamset);
2180eb6f0de0SAdrian Chadd 	*tid = (int) MS(baparamset, IEEE80211_BAPS_TID);
2181eb6f0de0SAdrian Chadd 
2182eb6f0de0SAdrian Chadd 	return 1;
2183eb6f0de0SAdrian Chadd }
2184eb6f0de0SAdrian Chadd #undef	MS
2185eb6f0de0SAdrian Chadd 
2186eb6f0de0SAdrian Chadd /* Per-node software queue operations */
2187eb6f0de0SAdrian Chadd 
2188eb6f0de0SAdrian Chadd /*
2189eb6f0de0SAdrian Chadd  * Add the current packet to the given BAW.
2190eb6f0de0SAdrian Chadd  * It is assumed that the current packet
2191eb6f0de0SAdrian Chadd  *
2192eb6f0de0SAdrian Chadd  * + fits inside the BAW;
2193eb6f0de0SAdrian Chadd  * + already has had a sequence number allocated.
2194eb6f0de0SAdrian Chadd  *
2195eb6f0de0SAdrian Chadd  * Since the BAW status may be modified by both the ath task and
2196eb6f0de0SAdrian Chadd  * the net80211/ifnet contexts, the TID must be locked.
2197eb6f0de0SAdrian Chadd  */
2198eb6f0de0SAdrian Chadd void
2199eb6f0de0SAdrian Chadd ath_tx_addto_baw(struct ath_softc *sc, struct ath_node *an,
2200eb6f0de0SAdrian Chadd     struct ath_tid *tid, struct ath_buf *bf)
2201eb6f0de0SAdrian Chadd {
2202eb6f0de0SAdrian Chadd 	int index, cindex;
2203eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
2204eb6f0de0SAdrian Chadd 
2205eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
2206c2ac9655SAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
2207eb6f0de0SAdrian Chadd 
2208eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_isretried)
2209eb6f0de0SAdrian Chadd 		return;
2210eb6f0de0SAdrian Chadd 
2211c7c07341SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
2212c7c07341SAdrian Chadd 
22137561cb5cSAdrian Chadd 	if (! bf->bf_state.bfs_dobaw) {
22147561cb5cSAdrian Chadd 		device_printf(sc->sc_dev,
22157561cb5cSAdrian Chadd 		    "%s: dobaw=0, seqno=%d, window %d:%d\n",
22167561cb5cSAdrian Chadd 		    __func__,
22177561cb5cSAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno),
22187561cb5cSAdrian Chadd 		    tap->txa_start,
22197561cb5cSAdrian Chadd 		    tap->txa_wnd);
22207561cb5cSAdrian Chadd 	}
22217561cb5cSAdrian Chadd 
2222eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_addedbaw)
2223eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
2224a108d2d6SAdrian Chadd 		    "%s: re-added? tid=%d, seqno %d; window %d:%d; "
2225d4365d16SAdrian Chadd 		    "baw head=%d tail=%d\n",
2226a108d2d6SAdrian Chadd 		    __func__, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
2227d4365d16SAdrian Chadd 		    tap->txa_start, tap->txa_wnd, tid->baw_head,
2228d4365d16SAdrian Chadd 		    tid->baw_tail);
2229eb6f0de0SAdrian Chadd 
2230eb6f0de0SAdrian Chadd 	/*
22317561cb5cSAdrian Chadd 	 * Verify that the given sequence number is not outside of the
22327561cb5cSAdrian Chadd 	 * BAW.  Complain loudly if that's the case.
22337561cb5cSAdrian Chadd 	 */
22347561cb5cSAdrian Chadd 	if (! BAW_WITHIN(tap->txa_start, tap->txa_wnd,
22357561cb5cSAdrian Chadd 	    SEQNO(bf->bf_state.bfs_seqno))) {
22367561cb5cSAdrian Chadd 		device_printf(sc->sc_dev,
22377561cb5cSAdrian Chadd 		    "%s: bf=%p: outside of BAW?? tid=%d, seqno %d; window %d:%d; "
22387561cb5cSAdrian Chadd 		    "baw head=%d tail=%d\n",
22397561cb5cSAdrian Chadd 		    __func__, bf, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
22407561cb5cSAdrian Chadd 		    tap->txa_start, tap->txa_wnd, tid->baw_head,
22417561cb5cSAdrian Chadd 		    tid->baw_tail);
22427561cb5cSAdrian Chadd 	}
22437561cb5cSAdrian Chadd 
22447561cb5cSAdrian Chadd 	/*
2245eb6f0de0SAdrian Chadd 	 * ni->ni_txseqs[] is the currently allocated seqno.
2246eb6f0de0SAdrian Chadd 	 * the txa state contains the current baw start.
2247eb6f0de0SAdrian Chadd 	 */
2248eb6f0de0SAdrian Chadd 	index  = ATH_BA_INDEX(tap->txa_start, SEQNO(bf->bf_state.bfs_seqno));
2249eb6f0de0SAdrian Chadd 	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
2250eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2251a108d2d6SAdrian Chadd 	    "%s: tid=%d, seqno %d; window %d:%d; index=%d cindex=%d "
2252d4365d16SAdrian Chadd 	    "baw head=%d tail=%d\n",
2253a108d2d6SAdrian Chadd 	    __func__, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
2254d4365d16SAdrian Chadd 	    tap->txa_start, tap->txa_wnd, index, cindex, tid->baw_head,
2255d4365d16SAdrian Chadd 	    tid->baw_tail);
2256eb6f0de0SAdrian Chadd 
2257eb6f0de0SAdrian Chadd 
2258eb6f0de0SAdrian Chadd #if 0
2259eb6f0de0SAdrian Chadd 	assert(tid->tx_buf[cindex] == NULL);
2260eb6f0de0SAdrian Chadd #endif
2261eb6f0de0SAdrian Chadd 	if (tid->tx_buf[cindex] != NULL) {
2262eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
2263eb6f0de0SAdrian Chadd 		    "%s: ba packet dup (index=%d, cindex=%d, "
2264eb6f0de0SAdrian Chadd 		    "head=%d, tail=%d)\n",
2265eb6f0de0SAdrian Chadd 		    __func__, index, cindex, tid->baw_head, tid->baw_tail);
2266eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
2267eb6f0de0SAdrian Chadd 		    "%s: BA bf: %p; seqno=%d ; new bf: %p; seqno=%d\n",
2268eb6f0de0SAdrian Chadd 		    __func__,
2269eb6f0de0SAdrian Chadd 		    tid->tx_buf[cindex],
2270eb6f0de0SAdrian Chadd 		    SEQNO(tid->tx_buf[cindex]->bf_state.bfs_seqno),
2271eb6f0de0SAdrian Chadd 		    bf,
2272eb6f0de0SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno)
2273eb6f0de0SAdrian Chadd 		);
2274eb6f0de0SAdrian Chadd 	}
2275eb6f0de0SAdrian Chadd 	tid->tx_buf[cindex] = bf;
2276eb6f0de0SAdrian Chadd 
2277d4365d16SAdrian Chadd 	if (index >= ((tid->baw_tail - tid->baw_head) &
2278d4365d16SAdrian Chadd 	    (ATH_TID_MAX_BUFS - 1))) {
2279eb6f0de0SAdrian Chadd 		tid->baw_tail = cindex;
2280eb6f0de0SAdrian Chadd 		INCR(tid->baw_tail, ATH_TID_MAX_BUFS);
2281eb6f0de0SAdrian Chadd 	}
2282eb6f0de0SAdrian Chadd }
2283eb6f0de0SAdrian Chadd 
2284eb6f0de0SAdrian Chadd /*
228538962489SAdrian Chadd  * Flip the BAW buffer entry over from the existing one to the new one.
228638962489SAdrian Chadd  *
228738962489SAdrian Chadd  * When software retransmitting a (sub-)frame, it is entirely possible that
228838962489SAdrian Chadd  * the frame ath_buf is marked as BUSY and can't be immediately reused.
228938962489SAdrian Chadd  * In that instance the buffer is cloned and the new buffer is used for
229038962489SAdrian Chadd  * retransmit. We thus need to update the ath_buf slot in the BAW buf
229138962489SAdrian Chadd  * tracking array to maintain consistency.
229238962489SAdrian Chadd  */
229338962489SAdrian Chadd static void
229438962489SAdrian Chadd ath_tx_switch_baw_buf(struct ath_softc *sc, struct ath_node *an,
229538962489SAdrian Chadd     struct ath_tid *tid, struct ath_buf *old_bf, struct ath_buf *new_bf)
229638962489SAdrian Chadd {
229738962489SAdrian Chadd 	int index, cindex;
229838962489SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
229938962489SAdrian Chadd 	int seqno = SEQNO(old_bf->bf_state.bfs_seqno);
230038962489SAdrian Chadd 
230138962489SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
2302c2ac9655SAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
230338962489SAdrian Chadd 
230438962489SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
230538962489SAdrian Chadd 	index  = ATH_BA_INDEX(tap->txa_start, seqno);
230638962489SAdrian Chadd 	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
230738962489SAdrian Chadd 
230838962489SAdrian Chadd 	/*
230938962489SAdrian Chadd 	 * Just warn for now; if it happens then we should find out
231038962489SAdrian Chadd 	 * about it. It's highly likely the aggregation session will
231138962489SAdrian Chadd 	 * soon hang.
231238962489SAdrian Chadd 	 */
231338962489SAdrian Chadd 	if (old_bf->bf_state.bfs_seqno != new_bf->bf_state.bfs_seqno) {
231438962489SAdrian Chadd 		device_printf(sc->sc_dev, "%s: retransmitted buffer"
231538962489SAdrian Chadd 		    " has mismatching seqno's, BA session may hang.\n",
231638962489SAdrian Chadd 		    __func__);
231738962489SAdrian Chadd 		device_printf(sc->sc_dev, "%s: old seqno=%d, new_seqno=%d\n",
231838962489SAdrian Chadd 		    __func__,
231938962489SAdrian Chadd 		    old_bf->bf_state.bfs_seqno,
232038962489SAdrian Chadd 		    new_bf->bf_state.bfs_seqno);
232138962489SAdrian Chadd 	}
232238962489SAdrian Chadd 
232338962489SAdrian Chadd 	if (tid->tx_buf[cindex] != old_bf) {
232438962489SAdrian Chadd 		device_printf(sc->sc_dev, "%s: ath_buf pointer incorrect; "
232538962489SAdrian Chadd 		    " has m BA session may hang.\n",
232638962489SAdrian Chadd 		    __func__);
232738962489SAdrian Chadd 		device_printf(sc->sc_dev, "%s: old bf=%p, new bf=%p\n",
232838962489SAdrian Chadd 		    __func__,
232938962489SAdrian Chadd 		    old_bf, new_bf);
233038962489SAdrian Chadd 	}
233138962489SAdrian Chadd 
233238962489SAdrian Chadd 	tid->tx_buf[cindex] = new_bf;
233338962489SAdrian Chadd }
233438962489SAdrian Chadd 
233538962489SAdrian Chadd /*
2336eb6f0de0SAdrian Chadd  * seq_start - left edge of BAW
2337eb6f0de0SAdrian Chadd  * seq_next - current/next sequence number to allocate
2338eb6f0de0SAdrian Chadd  *
2339eb6f0de0SAdrian Chadd  * Since the BAW status may be modified by both the ath task and
2340eb6f0de0SAdrian Chadd  * the net80211/ifnet contexts, the TID must be locked.
2341eb6f0de0SAdrian Chadd  */
2342eb6f0de0SAdrian Chadd static void
2343eb6f0de0SAdrian Chadd ath_tx_update_baw(struct ath_softc *sc, struct ath_node *an,
2344eb6f0de0SAdrian Chadd     struct ath_tid *tid, const struct ath_buf *bf)
2345eb6f0de0SAdrian Chadd {
2346eb6f0de0SAdrian Chadd 	int index, cindex;
2347eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
2348eb6f0de0SAdrian Chadd 	int seqno = SEQNO(bf->bf_state.bfs_seqno);
2349eb6f0de0SAdrian Chadd 
2350eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
23514b6db404SAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
2352eb6f0de0SAdrian Chadd 
2353eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
2354eb6f0de0SAdrian Chadd 	index  = ATH_BA_INDEX(tap->txa_start, seqno);
2355eb6f0de0SAdrian Chadd 	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
2356eb6f0de0SAdrian Chadd 
2357eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2358a108d2d6SAdrian Chadd 	    "%s: tid=%d, baw=%d:%d, seqno=%d, index=%d, cindex=%d, "
2359d4365d16SAdrian Chadd 	    "baw head=%d, tail=%d\n",
2360a108d2d6SAdrian Chadd 	    __func__, tid->tid, tap->txa_start, tap->txa_wnd, seqno, index,
2361eb6f0de0SAdrian Chadd 	    cindex, tid->baw_head, tid->baw_tail);
2362eb6f0de0SAdrian Chadd 
2363eb6f0de0SAdrian Chadd 	/*
2364eb6f0de0SAdrian Chadd 	 * If this occurs then we have a big problem - something else
2365eb6f0de0SAdrian Chadd 	 * has slid tap->txa_start along without updating the BAW
2366eb6f0de0SAdrian Chadd 	 * tracking start/end pointers. Thus the TX BAW state is now
2367eb6f0de0SAdrian Chadd 	 * completely busted.
2368eb6f0de0SAdrian Chadd 	 *
2369eb6f0de0SAdrian Chadd 	 * But for now, since I haven't yet fixed TDMA and buffer cloning,
2370eb6f0de0SAdrian Chadd 	 * it's quite possible that a cloned buffer is making its way
2371eb6f0de0SAdrian Chadd 	 * here and causing it to fire off. Disable TDMA for now.
2372eb6f0de0SAdrian Chadd 	 */
2373eb6f0de0SAdrian Chadd 	if (tid->tx_buf[cindex] != bf) {
2374eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
2375eb6f0de0SAdrian Chadd 		    "%s: comp bf=%p, seq=%d; slot bf=%p, seqno=%d\n",
2376eb6f0de0SAdrian Chadd 		    __func__,
2377eb6f0de0SAdrian Chadd 		    bf, SEQNO(bf->bf_state.bfs_seqno),
2378eb6f0de0SAdrian Chadd 		    tid->tx_buf[cindex],
2379eb6f0de0SAdrian Chadd 		    SEQNO(tid->tx_buf[cindex]->bf_state.bfs_seqno));
2380eb6f0de0SAdrian Chadd 	}
2381eb6f0de0SAdrian Chadd 
2382eb6f0de0SAdrian Chadd 	tid->tx_buf[cindex] = NULL;
2383eb6f0de0SAdrian Chadd 
2384d4365d16SAdrian Chadd 	while (tid->baw_head != tid->baw_tail &&
2385d4365d16SAdrian Chadd 	    !tid->tx_buf[tid->baw_head]) {
2386eb6f0de0SAdrian Chadd 		INCR(tap->txa_start, IEEE80211_SEQ_RANGE);
2387eb6f0de0SAdrian Chadd 		INCR(tid->baw_head, ATH_TID_MAX_BUFS);
2388eb6f0de0SAdrian Chadd 	}
2389d4365d16SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2390d4365d16SAdrian Chadd 	    "%s: baw is now %d:%d, baw head=%d\n",
2391eb6f0de0SAdrian Chadd 	    __func__, tap->txa_start, tap->txa_wnd, tid->baw_head);
2392eb6f0de0SAdrian Chadd }
2393eb6f0de0SAdrian Chadd 
2394eb6f0de0SAdrian Chadd /*
2395eb6f0de0SAdrian Chadd  * Mark the current node/TID as ready to TX.
2396eb6f0de0SAdrian Chadd  *
2397eb6f0de0SAdrian Chadd  * This is done to make it easy for the software scheduler to
2398eb6f0de0SAdrian Chadd  * find which nodes have data to send.
2399eb6f0de0SAdrian Chadd  *
2400eb6f0de0SAdrian Chadd  * The TXQ lock must be held.
2401eb6f0de0SAdrian Chadd  */
2402eb6f0de0SAdrian Chadd static void
2403eb6f0de0SAdrian Chadd ath_tx_tid_sched(struct ath_softc *sc, struct ath_tid *tid)
2404eb6f0de0SAdrian Chadd {
2405eb6f0de0SAdrian Chadd 	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
2406eb6f0de0SAdrian Chadd 
2407eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
2408eb6f0de0SAdrian Chadd 
2409eb6f0de0SAdrian Chadd 	if (tid->paused)
2410eb6f0de0SAdrian Chadd 		return;		/* paused, can't schedule yet */
2411eb6f0de0SAdrian Chadd 
2412eb6f0de0SAdrian Chadd 	if (tid->sched)
2413eb6f0de0SAdrian Chadd 		return;		/* already scheduled */
2414eb6f0de0SAdrian Chadd 
2415eb6f0de0SAdrian Chadd 	tid->sched = 1;
2416eb6f0de0SAdrian Chadd 
2417eb6f0de0SAdrian Chadd 	TAILQ_INSERT_TAIL(&txq->axq_tidq, tid, axq_qelem);
2418eb6f0de0SAdrian Chadd }
2419eb6f0de0SAdrian Chadd 
2420eb6f0de0SAdrian Chadd /*
2421eb6f0de0SAdrian Chadd  * Mark the current node as no longer needing to be polled for
2422eb6f0de0SAdrian Chadd  * TX packets.
2423eb6f0de0SAdrian Chadd  *
2424eb6f0de0SAdrian Chadd  * The TXQ lock must be held.
2425eb6f0de0SAdrian Chadd  */
2426eb6f0de0SAdrian Chadd static void
2427eb6f0de0SAdrian Chadd ath_tx_tid_unsched(struct ath_softc *sc, struct ath_tid *tid)
2428eb6f0de0SAdrian Chadd {
2429eb6f0de0SAdrian Chadd 	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
2430eb6f0de0SAdrian Chadd 
2431eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
2432eb6f0de0SAdrian Chadd 
2433eb6f0de0SAdrian Chadd 	if (tid->sched == 0)
2434eb6f0de0SAdrian Chadd 		return;
2435eb6f0de0SAdrian Chadd 
2436eb6f0de0SAdrian Chadd 	tid->sched = 0;
2437eb6f0de0SAdrian Chadd 	TAILQ_REMOVE(&txq->axq_tidq, tid, axq_qelem);
2438eb6f0de0SAdrian Chadd }
2439eb6f0de0SAdrian Chadd 
2440eb6f0de0SAdrian Chadd /*
2441eb6f0de0SAdrian Chadd  * Assign a sequence number manually to the given frame.
2442eb6f0de0SAdrian Chadd  *
2443eb6f0de0SAdrian Chadd  * This should only be called for A-MPDU TX frames.
2444eb6f0de0SAdrian Chadd  */
2445a108d2d6SAdrian Chadd static ieee80211_seq
2446eb6f0de0SAdrian Chadd ath_tx_tid_seqno_assign(struct ath_softc *sc, struct ieee80211_node *ni,
2447eb6f0de0SAdrian Chadd     struct ath_buf *bf, struct mbuf *m0)
2448eb6f0de0SAdrian Chadd {
2449eb6f0de0SAdrian Chadd 	struct ieee80211_frame *wh;
2450eb6f0de0SAdrian Chadd 	int tid, pri;
2451eb6f0de0SAdrian Chadd 	ieee80211_seq seqno;
2452eb6f0de0SAdrian Chadd 	uint8_t subtype;
2453eb6f0de0SAdrian Chadd 
2454eb6f0de0SAdrian Chadd 	/* TID lookup */
2455eb6f0de0SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
2456eb6f0de0SAdrian Chadd 	pri = M_WME_GETAC(m0);			/* honor classification */
2457eb6f0de0SAdrian Chadd 	tid = WME_AC_TO_TID(pri);
2458a108d2d6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: pri=%d, tid=%d, qos has seq=%d\n",
2459a108d2d6SAdrian Chadd 	    __func__, pri, tid, IEEE80211_QOS_HAS_SEQ(wh));
2460eb6f0de0SAdrian Chadd 
2461eb6f0de0SAdrian Chadd 	/* XXX Is it a control frame? Ignore */
2462eb6f0de0SAdrian Chadd 
2463eb6f0de0SAdrian Chadd 	/* Does the packet require a sequence number? */
2464eb6f0de0SAdrian Chadd 	if (! IEEE80211_QOS_HAS_SEQ(wh))
2465eb6f0de0SAdrian Chadd 		return -1;
2466eb6f0de0SAdrian Chadd 
24677561cb5cSAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, &(ATH_NODE(ni)->an_tid[tid]));
24687561cb5cSAdrian Chadd 
2469eb6f0de0SAdrian Chadd 	/*
2470eb6f0de0SAdrian Chadd 	 * Is it a QOS NULL Data frame? Give it a sequence number from
2471eb6f0de0SAdrian Chadd 	 * the default TID (IEEE80211_NONQOS_TID.)
2472eb6f0de0SAdrian Chadd 	 *
2473eb6f0de0SAdrian Chadd 	 * The RX path of everything I've looked at doesn't include the NULL
2474eb6f0de0SAdrian Chadd 	 * data frame sequence number in the aggregation state updates, so
2475eb6f0de0SAdrian Chadd 	 * assigning it a sequence number there will cause a BAW hole on the
2476eb6f0de0SAdrian Chadd 	 * RX side.
2477eb6f0de0SAdrian Chadd 	 */
2478eb6f0de0SAdrian Chadd 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
2479eb6f0de0SAdrian Chadd 	if (subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL) {
24807561cb5cSAdrian Chadd 		/* XXX no locking for this TID? This is a bit of a problem. */
2481eb6f0de0SAdrian Chadd 		seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID];
2482eb6f0de0SAdrian Chadd 		INCR(ni->ni_txseqs[IEEE80211_NONQOS_TID], IEEE80211_SEQ_RANGE);
2483eb6f0de0SAdrian Chadd 	} else {
2484eb6f0de0SAdrian Chadd 		/* Manually assign sequence number */
2485eb6f0de0SAdrian Chadd 		seqno = ni->ni_txseqs[tid];
2486eb6f0de0SAdrian Chadd 		INCR(ni->ni_txseqs[tid], IEEE80211_SEQ_RANGE);
2487eb6f0de0SAdrian Chadd 	}
2488eb6f0de0SAdrian Chadd 	*(uint16_t *)&wh->i_seq[0] = htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
2489eb6f0de0SAdrian Chadd 	M_SEQNO_SET(m0, seqno);
2490eb6f0de0SAdrian Chadd 
2491eb6f0de0SAdrian Chadd 	/* Return so caller can do something with it if needed */
2492a108d2d6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s:  -> seqno=%d\n", __func__, seqno);
2493eb6f0de0SAdrian Chadd 	return seqno;
2494eb6f0de0SAdrian Chadd }
2495eb6f0de0SAdrian Chadd 
2496eb6f0de0SAdrian Chadd /*
2497eb6f0de0SAdrian Chadd  * Attempt to direct dispatch an aggregate frame to hardware.
2498eb6f0de0SAdrian Chadd  * If the frame is out of BAW, queue.
2499eb6f0de0SAdrian Chadd  * Otherwise, schedule it as a single frame.
2500eb6f0de0SAdrian Chadd  */
2501eb6f0de0SAdrian Chadd static void
250246634305SAdrian Chadd ath_tx_xmit_aggr(struct ath_softc *sc, struct ath_node *an,
250346634305SAdrian Chadd     struct ath_txq *txq, struct ath_buf *bf)
2504eb6f0de0SAdrian Chadd {
2505eb6f0de0SAdrian Chadd 	struct ath_tid *tid = &an->an_tid[bf->bf_state.bfs_tid];
250646634305SAdrian Chadd //	struct ath_txq *txq = bf->bf_state.bfs_txq;
2507eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
2508eb6f0de0SAdrian Chadd 
250946634305SAdrian Chadd 	if (txq != bf->bf_state.bfs_txq) {
251046634305SAdrian Chadd 		device_printf(sc->sc_dev, "%s: txq %d != bfs_txq %d!\n",
251146634305SAdrian Chadd 		    __func__,
251246634305SAdrian Chadd 		    txq->axq_qnum,
251346634305SAdrian Chadd 		    bf->bf_state.bfs_txq->axq_qnum);
251446634305SAdrian Chadd 	}
251546634305SAdrian Chadd 
2516eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
2517c2ac9655SAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, tid);
2518eb6f0de0SAdrian Chadd 
2519eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
2520eb6f0de0SAdrian Chadd 
2521eb6f0de0SAdrian Chadd 	/* paused? queue */
2522eb6f0de0SAdrian Chadd 	if (tid->paused) {
25234547f047SAdrian Chadd 		ATH_TXQ_INSERT_HEAD(tid, bf, bf_list);
25240f04c5a2SAdrian Chadd 		/* XXX don't sched - we're paused! */
2525eb6f0de0SAdrian Chadd 		return;
2526eb6f0de0SAdrian Chadd 	}
2527eb6f0de0SAdrian Chadd 
2528eb6f0de0SAdrian Chadd 	/* outside baw? queue */
2529eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_dobaw &&
2530eb6f0de0SAdrian Chadd 	    (! BAW_WITHIN(tap->txa_start, tap->txa_wnd,
2531eb6f0de0SAdrian Chadd 	    SEQNO(bf->bf_state.bfs_seqno)))) {
2532ba0e58f4SAdrian Chadd 		ATH_TXQ_INSERT_HEAD(tid, bf, bf_list);
2533eb6f0de0SAdrian Chadd 		ath_tx_tid_sched(sc, tid);
2534eb6f0de0SAdrian Chadd 		return;
2535eb6f0de0SAdrian Chadd 	}
2536eb6f0de0SAdrian Chadd 
25372a9f83afSAdrian Chadd 	/*
25382a9f83afSAdrian Chadd 	 * This is a temporary check and should be removed once
25392a9f83afSAdrian Chadd 	 * all the relevant code paths have been fixed.
25402a9f83afSAdrian Chadd 	 *
25412a9f83afSAdrian Chadd 	 * During aggregate retries, it's possible that the head
25422a9f83afSAdrian Chadd 	 * frame will fail (which has the bfs_aggr and bfs_nframes
25432a9f83afSAdrian Chadd 	 * fields set for said aggregate) and will be retried as
25442a9f83afSAdrian Chadd 	 * a single frame.  In this instance, the values should
25452a9f83afSAdrian Chadd 	 * be reset or the completion code will get upset with you.
25462a9f83afSAdrian Chadd 	 */
25472a9f83afSAdrian Chadd 	if (bf->bf_state.bfs_aggr != 0 || bf->bf_state.bfs_nframes > 1) {
25482a9f83afSAdrian Chadd 		device_printf(sc->sc_dev, "%s: bfs_aggr=%d, bfs_nframes=%d\n",
25492a9f83afSAdrian Chadd 		    __func__,
25502a9f83afSAdrian Chadd 		    bf->bf_state.bfs_aggr,
25512a9f83afSAdrian Chadd 		    bf->bf_state.bfs_nframes);
25522a9f83afSAdrian Chadd 		bf->bf_state.bfs_aggr = 0;
25532a9f83afSAdrian Chadd 		bf->bf_state.bfs_nframes = 1;
25542a9f83afSAdrian Chadd 	}
25552a9f83afSAdrian Chadd 
2556eb6f0de0SAdrian Chadd 	/* Direct dispatch to hardware */
2557eb6f0de0SAdrian Chadd 	ath_tx_do_ratelookup(sc, bf);
2558e2e4a2c2SAdrian Chadd 	ath_tx_calc_duration(sc, bf);
2559e2e4a2c2SAdrian Chadd 	ath_tx_calc_protection(sc, bf);
2560eb6f0de0SAdrian Chadd 	ath_tx_set_rtscts(sc, bf);
2561e2e4a2c2SAdrian Chadd 	ath_tx_rate_fill_rcflags(sc, bf);
2562eb6f0de0SAdrian Chadd 	ath_tx_setds(sc, bf);
2563eb6f0de0SAdrian Chadd 
2564eb6f0de0SAdrian Chadd 	/* Statistics */
2565eb6f0de0SAdrian Chadd 	sc->sc_aggr_stats.aggr_low_hwq_single_pkt++;
2566eb6f0de0SAdrian Chadd 
2567eb6f0de0SAdrian Chadd 	/* Track per-TID hardware queue depth correctly */
2568eb6f0de0SAdrian Chadd 	tid->hwq_depth++;
2569eb6f0de0SAdrian Chadd 
2570eb6f0de0SAdrian Chadd 	/* Add to BAW */
2571eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_dobaw) {
2572eb6f0de0SAdrian Chadd 		ath_tx_addto_baw(sc, an, tid, bf);
2573eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_addedbaw = 1;
2574eb6f0de0SAdrian Chadd 	}
2575eb6f0de0SAdrian Chadd 
2576eb6f0de0SAdrian Chadd 	/* Set completion handler, multi-frame aggregate or not */
2577eb6f0de0SAdrian Chadd 	bf->bf_comp = ath_tx_aggr_comp;
2578eb6f0de0SAdrian Chadd 
2579eb6f0de0SAdrian Chadd 	/* Hand off to hardware */
2580eb6f0de0SAdrian Chadd 	ath_tx_handoff(sc, txq, bf);
2581eb6f0de0SAdrian Chadd }
2582eb6f0de0SAdrian Chadd 
2583eb6f0de0SAdrian Chadd /*
2584eb6f0de0SAdrian Chadd  * Attempt to send the packet.
2585eb6f0de0SAdrian Chadd  * If the queue isn't busy, direct-dispatch.
2586eb6f0de0SAdrian Chadd  * If the queue is busy enough, queue the given packet on the
2587eb6f0de0SAdrian Chadd  *  relevant software queue.
2588eb6f0de0SAdrian Chadd  */
2589eb6f0de0SAdrian Chadd void
2590eb6f0de0SAdrian Chadd ath_tx_swq(struct ath_softc *sc, struct ieee80211_node *ni, struct ath_txq *txq,
2591eb6f0de0SAdrian Chadd     struct ath_buf *bf)
2592eb6f0de0SAdrian Chadd {
2593eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
2594eb6f0de0SAdrian Chadd 	struct ieee80211_frame *wh;
2595eb6f0de0SAdrian Chadd 	struct ath_tid *atid;
2596eb6f0de0SAdrian Chadd 	int pri, tid;
2597eb6f0de0SAdrian Chadd 	struct mbuf *m0 = bf->bf_m;
2598eb6f0de0SAdrian Chadd 
25997561cb5cSAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
26007561cb5cSAdrian Chadd 
2601eb6f0de0SAdrian Chadd 	/* Fetch the TID - non-QoS frames get assigned to TID 16 */
2602eb6f0de0SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
2603eb6f0de0SAdrian Chadd 	pri = ath_tx_getac(sc, m0);
2604eb6f0de0SAdrian Chadd 	tid = ath_tx_gettid(sc, m0);
2605eb6f0de0SAdrian Chadd 	atid = &an->an_tid[tid];
2606eb6f0de0SAdrian Chadd 
2607c2ac9655SAdrian Chadd 	ATH_TID_LOCK_ASSERT(sc, atid);
2608c2ac9655SAdrian Chadd 
2609a108d2d6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bf=%p, pri=%d, tid=%d, qos=%d\n",
2610a108d2d6SAdrian Chadd 	    __func__, bf, pri, tid, IEEE80211_QOS_HAS_SEQ(wh));
2611eb6f0de0SAdrian Chadd 
2612eb6f0de0SAdrian Chadd 	/* Set local packet state, used to queue packets to hardware */
261346634305SAdrian Chadd 	/* XXX potentially duplicate info, re-check */
261446634305SAdrian Chadd 	/* XXX remember, txq must be the hardware queue, not the av_mcastq */
2615eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_tid = tid;
2616eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txq = txq;
2617eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_pri = pri;
2618eb6f0de0SAdrian Chadd 
2619eb6f0de0SAdrian Chadd 	/*
2620eb6f0de0SAdrian Chadd 	 * If the hardware queue isn't busy, queue it directly.
2621eb6f0de0SAdrian Chadd 	 * If the hardware queue is busy, queue it.
2622eb6f0de0SAdrian Chadd 	 * If the TID is paused or the traffic it outside BAW, software
2623eb6f0de0SAdrian Chadd 	 * queue it.
2624eb6f0de0SAdrian Chadd 	 */
2625eb6f0de0SAdrian Chadd 	if (atid->paused) {
2626eb6f0de0SAdrian Chadd 		/* TID is paused, queue */
2627a108d2d6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: paused\n", __func__);
2628eb6f0de0SAdrian Chadd 		ATH_TXQ_INSERT_TAIL(atid, bf, bf_list);
2629eb6f0de0SAdrian Chadd 	} else if (ath_tx_ampdu_pending(sc, an, tid)) {
2630eb6f0de0SAdrian Chadd 		/* AMPDU pending; queue */
2631a108d2d6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: pending\n", __func__);
2632eb6f0de0SAdrian Chadd 		ATH_TXQ_INSERT_TAIL(atid, bf, bf_list);
2633eb6f0de0SAdrian Chadd 		/* XXX sched? */
2634eb6f0de0SAdrian Chadd 	} else if (ath_tx_ampdu_running(sc, an, tid)) {
2635eb6f0de0SAdrian Chadd 		/* AMPDU running, attempt direct dispatch if possible */
263639f24578SAdrian Chadd 
263739f24578SAdrian Chadd 		/*
263839f24578SAdrian Chadd 		 * Always queue the frame to the tail of the list.
263939f24578SAdrian Chadd 		 */
264039f24578SAdrian Chadd 		ATH_TXQ_INSERT_TAIL(atid, bf, bf_list);
264139f24578SAdrian Chadd 
264239f24578SAdrian Chadd 		/*
264339f24578SAdrian Chadd 		 * If the hardware queue isn't busy, direct dispatch
264439f24578SAdrian Chadd 		 * the head frame in the list.  Don't schedule the
264539f24578SAdrian Chadd 		 * TID - let it build some more frames first?
264639f24578SAdrian Chadd 		 *
264739f24578SAdrian Chadd 		 * Otherwise, schedule the TID.
264839f24578SAdrian Chadd 		 */
2649d4365d16SAdrian Chadd 		if (txq->axq_depth < sc->sc_hwq_limit) {
265039f24578SAdrian Chadd 			bf = TAILQ_FIRST(&atid->axq_q);
265139f24578SAdrian Chadd 			ATH_TXQ_REMOVE(atid, bf, bf_list);
26522a9f83afSAdrian Chadd 
26532a9f83afSAdrian Chadd 			/*
26542a9f83afSAdrian Chadd 			 * Ensure it's definitely treated as a non-AMPDU
26552a9f83afSAdrian Chadd 			 * frame - this information may have been left
26562a9f83afSAdrian Chadd 			 * over from a previous attempt.
26572a9f83afSAdrian Chadd 			 */
26582a9f83afSAdrian Chadd 			bf->bf_state.bfs_aggr = 0;
26592a9f83afSAdrian Chadd 			bf->bf_state.bfs_nframes = 1;
26602a9f83afSAdrian Chadd 
26612a9f83afSAdrian Chadd 			/* Queue to the hardware */
266246634305SAdrian Chadd 			ath_tx_xmit_aggr(sc, an, txq, bf);
2663a108d2d6SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX,
2664a108d2d6SAdrian Chadd 			    "%s: xmit_aggr\n",
2665a108d2d6SAdrian Chadd 			    __func__);
2666d4365d16SAdrian Chadd 		} else {
2667d4365d16SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX,
2668a108d2d6SAdrian Chadd 			    "%s: ampdu; swq'ing\n",
2669a108d2d6SAdrian Chadd 			    __func__);
2670eb6f0de0SAdrian Chadd 			ath_tx_tid_sched(sc, atid);
2671eb6f0de0SAdrian Chadd 		}
2672eb6f0de0SAdrian Chadd 	} else if (txq->axq_depth < sc->sc_hwq_limit) {
2673eb6f0de0SAdrian Chadd 		/* AMPDU not running, attempt direct dispatch */
2674a108d2d6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: xmit_normal\n", __func__);
2675eb6f0de0SAdrian Chadd 		ath_tx_xmit_normal(sc, txq, bf);
2676eb6f0de0SAdrian Chadd 	} else {
2677eb6f0de0SAdrian Chadd 		/* Busy; queue */
2678a108d2d6SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: swq'ing\n", __func__);
2679eb6f0de0SAdrian Chadd 		ATH_TXQ_INSERT_TAIL(atid, bf, bf_list);
2680eb6f0de0SAdrian Chadd 		ath_tx_tid_sched(sc, atid);
2681eb6f0de0SAdrian Chadd 	}
2682eb6f0de0SAdrian Chadd }
2683eb6f0de0SAdrian Chadd 
2684eb6f0de0SAdrian Chadd /*
2685eb6f0de0SAdrian Chadd  * Do the basic frame setup stuff that's required before the frame
2686eb6f0de0SAdrian Chadd  * is added to a software queue.
2687eb6f0de0SAdrian Chadd  *
2688eb6f0de0SAdrian Chadd  * All frames get mostly the same treatment and it's done once.
2689eb6f0de0SAdrian Chadd  * Retransmits fiddle with things like the rate control setup,
2690eb6f0de0SAdrian Chadd  * setting the retransmit bit in the packet; doing relevant DMA/bus
2691eb6f0de0SAdrian Chadd  * syncing and relinking it (back) into the hardware TX queue.
2692eb6f0de0SAdrian Chadd  *
2693eb6f0de0SAdrian Chadd  * Note that this may cause the mbuf to be reallocated, so
2694eb6f0de0SAdrian Chadd  * m0 may not be valid.
2695eb6f0de0SAdrian Chadd  */
2696eb6f0de0SAdrian Chadd 
2697eb6f0de0SAdrian Chadd 
2698eb6f0de0SAdrian Chadd /*
2699eb6f0de0SAdrian Chadd  * Configure the per-TID node state.
2700eb6f0de0SAdrian Chadd  *
2701eb6f0de0SAdrian Chadd  * This likely belongs in if_ath_node.c but I can't think of anywhere
2702eb6f0de0SAdrian Chadd  * else to put it just yet.
2703eb6f0de0SAdrian Chadd  *
2704eb6f0de0SAdrian Chadd  * This sets up the SLISTs and the mutex as appropriate.
2705eb6f0de0SAdrian Chadd  */
2706eb6f0de0SAdrian Chadd void
2707eb6f0de0SAdrian Chadd ath_tx_tid_init(struct ath_softc *sc, struct ath_node *an)
2708eb6f0de0SAdrian Chadd {
2709eb6f0de0SAdrian Chadd 	int i, j;
2710eb6f0de0SAdrian Chadd 	struct ath_tid *atid;
2711eb6f0de0SAdrian Chadd 
2712eb6f0de0SAdrian Chadd 	for (i = 0; i < IEEE80211_TID_SIZE; i++) {
2713eb6f0de0SAdrian Chadd 		atid = &an->an_tid[i];
2714eb6f0de0SAdrian Chadd 		TAILQ_INIT(&atid->axq_q);
2715eb6f0de0SAdrian Chadd 		atid->tid = i;
2716eb6f0de0SAdrian Chadd 		atid->an = an;
2717eb6f0de0SAdrian Chadd 		for (j = 0; j < ATH_TID_MAX_BUFS; j++)
2718eb6f0de0SAdrian Chadd 			atid->tx_buf[j] = NULL;
2719eb6f0de0SAdrian Chadd 		atid->baw_head = atid->baw_tail = 0;
2720eb6f0de0SAdrian Chadd 		atid->paused = 0;
2721eb6f0de0SAdrian Chadd 		atid->sched = 0;
2722eb6f0de0SAdrian Chadd 		atid->hwq_depth = 0;
2723eb6f0de0SAdrian Chadd 		atid->cleanup_inprogress = 0;
2724eb6f0de0SAdrian Chadd 		if (i == IEEE80211_NONQOS_TID)
2725eb6f0de0SAdrian Chadd 			atid->ac = WME_AC_BE;
2726eb6f0de0SAdrian Chadd 		else
2727eb6f0de0SAdrian Chadd 			atid->ac = TID_TO_WME_AC(i);
2728eb6f0de0SAdrian Chadd 	}
2729eb6f0de0SAdrian Chadd }
2730eb6f0de0SAdrian Chadd 
2731eb6f0de0SAdrian Chadd /*
2732eb6f0de0SAdrian Chadd  * Pause the current TID. This stops packets from being transmitted
2733eb6f0de0SAdrian Chadd  * on it.
2734eb6f0de0SAdrian Chadd  *
2735eb6f0de0SAdrian Chadd  * Since this is also called from upper layers as well as the driver,
2736eb6f0de0SAdrian Chadd  * it will get the TID lock.
2737eb6f0de0SAdrian Chadd  */
2738eb6f0de0SAdrian Chadd static void
2739eb6f0de0SAdrian Chadd ath_tx_tid_pause(struct ath_softc *sc, struct ath_tid *tid)
2740eb6f0de0SAdrian Chadd {
274188b3d483SAdrian Chadd 
274288b3d483SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
2743eb6f0de0SAdrian Chadd 	tid->paused++;
2744eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: paused = %d\n",
2745eb6f0de0SAdrian Chadd 	    __func__, tid->paused);
2746eb6f0de0SAdrian Chadd }
2747eb6f0de0SAdrian Chadd 
2748eb6f0de0SAdrian Chadd /*
2749eb6f0de0SAdrian Chadd  * Unpause the current TID, and schedule it if needed.
2750eb6f0de0SAdrian Chadd  */
2751eb6f0de0SAdrian Chadd static void
2752eb6f0de0SAdrian Chadd ath_tx_tid_resume(struct ath_softc *sc, struct ath_tid *tid)
2753eb6f0de0SAdrian Chadd {
2754eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
2755eb6f0de0SAdrian Chadd 
2756eb6f0de0SAdrian Chadd 	tid->paused--;
2757eb6f0de0SAdrian Chadd 
2758eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: unpaused = %d\n",
2759eb6f0de0SAdrian Chadd 	    __func__, tid->paused);
2760eb6f0de0SAdrian Chadd 
2761eb6f0de0SAdrian Chadd 	if (tid->paused || tid->axq_depth == 0) {
2762eb6f0de0SAdrian Chadd 		return;
2763eb6f0de0SAdrian Chadd 	}
2764eb6f0de0SAdrian Chadd 
2765eb6f0de0SAdrian Chadd 	ath_tx_tid_sched(sc, tid);
2766eb6f0de0SAdrian Chadd 	/* Punt some frames to the hardware if needed */
276703e9308fSAdrian Chadd 	//ath_txq_sched(sc, sc->sc_ac2q[tid->ac]);
276803e9308fSAdrian Chadd 	taskqueue_enqueue(sc->sc_tq, &sc->sc_txqtask);
2769eb6f0de0SAdrian Chadd }
2770eb6f0de0SAdrian Chadd 
2771eb6f0de0SAdrian Chadd /*
277288b3d483SAdrian Chadd  * Suspend the queue because we need to TX a BAR.
277388b3d483SAdrian Chadd  */
277488b3d483SAdrian Chadd static void
277588b3d483SAdrian Chadd ath_tx_tid_bar_suspend(struct ath_softc *sc, struct ath_tid *tid)
277688b3d483SAdrian Chadd {
277788b3d483SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
277888b3d483SAdrian Chadd 
27790e22ed0eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
2780e60c4fc2SAdrian Chadd 	    "%s: tid=%p, bar_wait=%d, bar_tx=%d, called\n",
278188b3d483SAdrian Chadd 	    __func__,
2782e60c4fc2SAdrian Chadd 	    tid,
2783e60c4fc2SAdrian Chadd 	    tid->bar_wait,
2784e60c4fc2SAdrian Chadd 	    tid->bar_tx);
278588b3d483SAdrian Chadd 
278688b3d483SAdrian Chadd 	/* We shouldn't be called when bar_tx is 1 */
278788b3d483SAdrian Chadd 	if (tid->bar_tx) {
278888b3d483SAdrian Chadd 		device_printf(sc->sc_dev, "%s: bar_tx is 1?!\n",
278988b3d483SAdrian Chadd 		    __func__);
279088b3d483SAdrian Chadd 	}
279188b3d483SAdrian Chadd 
279288b3d483SAdrian Chadd 	/* If we've already been called, just be patient. */
279388b3d483SAdrian Chadd 	if (tid->bar_wait)
279488b3d483SAdrian Chadd 		return;
279588b3d483SAdrian Chadd 
279688b3d483SAdrian Chadd 	/* Wait! */
279788b3d483SAdrian Chadd 	tid->bar_wait = 1;
279888b3d483SAdrian Chadd 
279988b3d483SAdrian Chadd 	/* Only one pause, no matter how many frames fail */
280088b3d483SAdrian Chadd 	ath_tx_tid_pause(sc, tid);
280188b3d483SAdrian Chadd }
280288b3d483SAdrian Chadd 
280388b3d483SAdrian Chadd /*
280488b3d483SAdrian Chadd  * We've finished with BAR handling - either we succeeded or
280588b3d483SAdrian Chadd  * failed. Either way, unsuspend TX.
280688b3d483SAdrian Chadd  */
280788b3d483SAdrian Chadd static void
280888b3d483SAdrian Chadd ath_tx_tid_bar_unsuspend(struct ath_softc *sc, struct ath_tid *tid)
280988b3d483SAdrian Chadd {
281088b3d483SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
281188b3d483SAdrian Chadd 
28120e22ed0eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
281388b3d483SAdrian Chadd 	    "%s: tid=%p, called\n",
281488b3d483SAdrian Chadd 	    __func__,
281588b3d483SAdrian Chadd 	    tid);
281688b3d483SAdrian Chadd 
281788b3d483SAdrian Chadd 	if (tid->bar_tx == 0 || tid->bar_wait == 0) {
281888b3d483SAdrian Chadd 		device_printf(sc->sc_dev, "%s: bar_tx=%d, bar_wait=%d: ?\n",
281988b3d483SAdrian Chadd 		    __func__, tid->bar_tx, tid->bar_wait);
282088b3d483SAdrian Chadd 	}
282188b3d483SAdrian Chadd 
282288b3d483SAdrian Chadd 	tid->bar_tx = tid->bar_wait = 0;
282388b3d483SAdrian Chadd 	ath_tx_tid_resume(sc, tid);
282488b3d483SAdrian Chadd }
282588b3d483SAdrian Chadd 
282688b3d483SAdrian Chadd /*
282788b3d483SAdrian Chadd  * Return whether we're ready to TX a BAR frame.
282888b3d483SAdrian Chadd  *
282988b3d483SAdrian Chadd  * Requires the TID lock be held.
283088b3d483SAdrian Chadd  */
283188b3d483SAdrian Chadd static int
283288b3d483SAdrian Chadd ath_tx_tid_bar_tx_ready(struct ath_softc *sc, struct ath_tid *tid)
283388b3d483SAdrian Chadd {
283488b3d483SAdrian Chadd 
283588b3d483SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
283688b3d483SAdrian Chadd 
283788b3d483SAdrian Chadd 	if (tid->bar_wait == 0 || tid->hwq_depth > 0)
283888b3d483SAdrian Chadd 		return (0);
283988b3d483SAdrian Chadd 
28400e22ed0eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR, "%s: tid=%p (%d), bar ready\n",
28410e22ed0eSAdrian Chadd 	    __func__, tid, tid->tid);
28420e22ed0eSAdrian Chadd 
284388b3d483SAdrian Chadd 	return (1);
284488b3d483SAdrian Chadd }
284588b3d483SAdrian Chadd 
284688b3d483SAdrian Chadd /*
284788b3d483SAdrian Chadd  * Check whether the current TID is ready to have a BAR
284888b3d483SAdrian Chadd  * TXed and if so, do the TX.
284988b3d483SAdrian Chadd  *
285088b3d483SAdrian Chadd  * Since the TID/TXQ lock can't be held during a call to
285188b3d483SAdrian Chadd  * ieee80211_send_bar(), we have to do the dirty thing of unlocking it,
285288b3d483SAdrian Chadd  * sending the BAR and locking it again.
285388b3d483SAdrian Chadd  *
285488b3d483SAdrian Chadd  * Eventually, the code to send the BAR should be broken out
285588b3d483SAdrian Chadd  * from this routine so the lock doesn't have to be reacquired
285688b3d483SAdrian Chadd  * just to be immediately dropped by the caller.
285788b3d483SAdrian Chadd  */
285888b3d483SAdrian Chadd static void
285988b3d483SAdrian Chadd ath_tx_tid_bar_tx(struct ath_softc *sc, struct ath_tid *tid)
286088b3d483SAdrian Chadd {
286188b3d483SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
286288b3d483SAdrian Chadd 
286388b3d483SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
286488b3d483SAdrian Chadd 
28650e22ed0eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
286688b3d483SAdrian Chadd 	    "%s: tid=%p, called\n",
286788b3d483SAdrian Chadd 	    __func__,
286888b3d483SAdrian Chadd 	    tid);
286988b3d483SAdrian Chadd 
287088b3d483SAdrian Chadd 	tap = ath_tx_get_tx_tid(tid->an, tid->tid);
287188b3d483SAdrian Chadd 
287288b3d483SAdrian Chadd 	/*
287388b3d483SAdrian Chadd 	 * This is an error condition!
287488b3d483SAdrian Chadd 	 */
287588b3d483SAdrian Chadd 	if (tid->bar_wait == 0 || tid->bar_tx == 1) {
287688b3d483SAdrian Chadd 		device_printf(sc->sc_dev,
287788b3d483SAdrian Chadd 		    "%s: tid=%p, bar_tx=%d, bar_wait=%d: ?\n",
287888b3d483SAdrian Chadd 		    __func__,
287988b3d483SAdrian Chadd 		    tid,
288088b3d483SAdrian Chadd 		    tid->bar_tx,
288188b3d483SAdrian Chadd 		    tid->bar_wait);
288288b3d483SAdrian Chadd 		return;
288388b3d483SAdrian Chadd 	}
288488b3d483SAdrian Chadd 
288588b3d483SAdrian Chadd 	/* Don't do anything if we still have pending frames */
288688b3d483SAdrian Chadd 	if (tid->hwq_depth > 0) {
28870e22ed0eSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
288888b3d483SAdrian Chadd 		    "%s: tid=%p, hwq_depth=%d, waiting\n",
288988b3d483SAdrian Chadd 		    __func__,
289088b3d483SAdrian Chadd 		    tid,
289188b3d483SAdrian Chadd 		    tid->hwq_depth);
289288b3d483SAdrian Chadd 		return;
289388b3d483SAdrian Chadd 	}
289488b3d483SAdrian Chadd 
289588b3d483SAdrian Chadd 	/* We're now about to TX */
289688b3d483SAdrian Chadd 	tid->bar_tx = 1;
289788b3d483SAdrian Chadd 
289888b3d483SAdrian Chadd 	/*
289988b3d483SAdrian Chadd 	 * Calculate new BAW left edge, now that all frames have either
290088b3d483SAdrian Chadd 	 * succeeded or failed.
290188b3d483SAdrian Chadd 	 *
290288b3d483SAdrian Chadd 	 * XXX verify this is _actually_ the valid value to begin at!
290388b3d483SAdrian Chadd 	 */
29040e22ed0eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
290588b3d483SAdrian Chadd 	    "%s: tid=%p, new BAW left edge=%d\n",
290688b3d483SAdrian Chadd 	    __func__,
290788b3d483SAdrian Chadd 	    tid,
290888b3d483SAdrian Chadd 	    tap->txa_start);
290988b3d483SAdrian Chadd 
291088b3d483SAdrian Chadd 	/* Try sending the BAR frame */
291188b3d483SAdrian Chadd 	/* We can't hold the lock here! */
291288b3d483SAdrian Chadd 
291388b3d483SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[tid->ac]);
291488b3d483SAdrian Chadd 	if (ieee80211_send_bar(&tid->an->an_node, tap, tap->txa_start) == 0) {
291588b3d483SAdrian Chadd 		/* Success? Now we wait for notification that it's done */
291688b3d483SAdrian Chadd 		ATH_TXQ_LOCK(sc->sc_ac2q[tid->ac]);
291788b3d483SAdrian Chadd 		return;
291888b3d483SAdrian Chadd 	}
291988b3d483SAdrian Chadd 
292088b3d483SAdrian Chadd 	/* Failure? For now, warn loudly and continue */
292188b3d483SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[tid->ac]);
292288b3d483SAdrian Chadd 	device_printf(sc->sc_dev, "%s: tid=%p, failed to TX BAR, continue!\n",
292388b3d483SAdrian Chadd 	    __func__, tid);
292488b3d483SAdrian Chadd 	ath_tx_tid_bar_unsuspend(sc, tid);
292588b3d483SAdrian Chadd }
292688b3d483SAdrian Chadd 
292788b3d483SAdrian Chadd 
292888b3d483SAdrian Chadd /*
2929eb6f0de0SAdrian Chadd  * Free any packets currently pending in the software TX queue.
2930eb6f0de0SAdrian Chadd  *
2931eb6f0de0SAdrian Chadd  * This will be called when a node is being deleted.
2932eb6f0de0SAdrian Chadd  *
2933eb6f0de0SAdrian Chadd  * It can also be called on an active node during an interface
2934eb6f0de0SAdrian Chadd  * reset or state transition.
2935eb6f0de0SAdrian Chadd  *
2936eb6f0de0SAdrian Chadd  * (From Linux/reference):
2937eb6f0de0SAdrian Chadd  *
2938eb6f0de0SAdrian Chadd  * TODO: For frame(s) that are in the retry state, we will reuse the
2939eb6f0de0SAdrian Chadd  * sequence number(s) without setting the retry bit. The
2940eb6f0de0SAdrian Chadd  * alternative is to give up on these and BAR the receiver's window
2941eb6f0de0SAdrian Chadd  * forward.
2942eb6f0de0SAdrian Chadd  */
2943eb6f0de0SAdrian Chadd static void
2944d4365d16SAdrian Chadd ath_tx_tid_drain(struct ath_softc *sc, struct ath_node *an,
2945d4365d16SAdrian Chadd     struct ath_tid *tid, ath_bufhead *bf_cq)
2946eb6f0de0SAdrian Chadd {
2947eb6f0de0SAdrian Chadd 	struct ath_buf *bf;
2948eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
2949eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = &an->an_node;
2950eb6f0de0SAdrian Chadd 	int t = 0;
2951eb6f0de0SAdrian Chadd 	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
2952eb6f0de0SAdrian Chadd 
2953eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
2954eb6f0de0SAdrian Chadd 
2955eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
2956eb6f0de0SAdrian Chadd 
2957eb6f0de0SAdrian Chadd 	/* Walk the queue, free frames */
2958eb6f0de0SAdrian Chadd 	for (;;) {
2959eb6f0de0SAdrian Chadd 		bf = TAILQ_FIRST(&tid->axq_q);
2960eb6f0de0SAdrian Chadd 		if (bf == NULL) {
2961eb6f0de0SAdrian Chadd 			break;
2962eb6f0de0SAdrian Chadd 		}
2963eb6f0de0SAdrian Chadd 
2964eb6f0de0SAdrian Chadd 		if (t == 0) {
2965eb6f0de0SAdrian Chadd 			device_printf(sc->sc_dev,
296612be5b9cSAdrian Chadd 			    "%s: node %p: bf=%p: addbaw=%d, dobaw=%d, "
2967a108d2d6SAdrian Chadd 			    "seqno=%d, retry=%d\n",
296812be5b9cSAdrian Chadd 			    __func__, ni, bf,
296912be5b9cSAdrian Chadd 			    bf->bf_state.bfs_addedbaw,
297012be5b9cSAdrian Chadd 			    bf->bf_state.bfs_dobaw,
29710f04c5a2SAdrian Chadd 			    SEQNO(bf->bf_state.bfs_seqno),
29720f04c5a2SAdrian Chadd 			    bf->bf_state.bfs_retries);
29730f04c5a2SAdrian Chadd 			device_printf(sc->sc_dev,
29740e22ed0eSAdrian Chadd 			    "%s: node %p: bf=%p: tid txq_depth=%d hwq_depth=%d, bar_wait=%d\n",
29750f04c5a2SAdrian Chadd 			    __func__, ni, bf,
29760f04c5a2SAdrian Chadd 			    tid->axq_depth,
29770e22ed0eSAdrian Chadd 			    tid->hwq_depth,
29780e22ed0eSAdrian Chadd 			    tid->bar_wait);
297912be5b9cSAdrian Chadd 			device_printf(sc->sc_dev,
2980a108d2d6SAdrian Chadd 			    "%s: node %p: tid %d: txq_depth=%d, "
2981eb6f0de0SAdrian Chadd 			    "txq_aggr_depth=%d, sched=%d, paused=%d, "
2982d4365d16SAdrian Chadd 			    "hwq_depth=%d, incomp=%d, baw_head=%d, "
2983d4365d16SAdrian Chadd 			    "baw_tail=%d txa_start=%d, ni_txseqs=%d\n",
2984a108d2d6SAdrian Chadd 			     __func__, ni, tid->tid, txq->axq_depth,
2985eb6f0de0SAdrian Chadd 			     txq->axq_aggr_depth, tid->sched, tid->paused,
2986eb6f0de0SAdrian Chadd 			     tid->hwq_depth, tid->incomp, tid->baw_head,
2987eb6f0de0SAdrian Chadd 			     tid->baw_tail, tap == NULL ? -1 : tap->txa_start,
2988eb6f0de0SAdrian Chadd 			     ni->ni_txseqs[tid->tid]);
2989c0711b97SAdrian Chadd 
2990c0711b97SAdrian Chadd 			/* XXX Dump the frame, see what it is? */
2991c0711b97SAdrian Chadd 			ieee80211_dump_pkt(ni->ni_ic,
2992c0711b97SAdrian Chadd 			    mtod(bf->bf_m, const uint8_t *),
2993c0711b97SAdrian Chadd 			    bf->bf_m->m_len, 0, -1);
2994c0711b97SAdrian Chadd 
2995d743debcSAdrian Chadd 			t = 1;
2996eb6f0de0SAdrian Chadd 		}
2997eb6f0de0SAdrian Chadd 
2998eb6f0de0SAdrian Chadd 
2999eb6f0de0SAdrian Chadd 		/*
3000eb6f0de0SAdrian Chadd 		 * If the current TID is running AMPDU, update
3001eb6f0de0SAdrian Chadd 		 * the BAW.
3002eb6f0de0SAdrian Chadd 		 */
3003eb6f0de0SAdrian Chadd 		if (ath_tx_ampdu_running(sc, an, tid->tid) &&
3004eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_dobaw) {
3005eb6f0de0SAdrian Chadd 			/*
3006eb6f0de0SAdrian Chadd 			 * Only remove the frame from the BAW if it's
3007eb6f0de0SAdrian Chadd 			 * been transmitted at least once; this means
3008eb6f0de0SAdrian Chadd 			 * the frame was in the BAW to begin with.
3009eb6f0de0SAdrian Chadd 			 */
3010eb6f0de0SAdrian Chadd 			if (bf->bf_state.bfs_retries > 0) {
3011eb6f0de0SAdrian Chadd 				ath_tx_update_baw(sc, an, tid, bf);
3012eb6f0de0SAdrian Chadd 				bf->bf_state.bfs_dobaw = 0;
3013eb6f0de0SAdrian Chadd 			}
3014eb6f0de0SAdrian Chadd 			/*
3015eb6f0de0SAdrian Chadd 			 * This has become a non-fatal error now
3016eb6f0de0SAdrian Chadd 			 */
3017eb6f0de0SAdrian Chadd 			if (! bf->bf_state.bfs_addedbaw)
3018eb6f0de0SAdrian Chadd 				device_printf(sc->sc_dev,
3019eb6f0de0SAdrian Chadd 				    "%s: wasn't added: seqno %d\n",
3020eb6f0de0SAdrian Chadd 				    __func__, SEQNO(bf->bf_state.bfs_seqno));
3021eb6f0de0SAdrian Chadd 		}
3022eb6f0de0SAdrian Chadd 		ATH_TXQ_REMOVE(tid, bf, bf_list);
3023eb6f0de0SAdrian Chadd 		TAILQ_INSERT_TAIL(bf_cq, bf, bf_list);
3024eb6f0de0SAdrian Chadd 	}
3025eb6f0de0SAdrian Chadd 
3026eb6f0de0SAdrian Chadd 	/*
3027eb6f0de0SAdrian Chadd 	 * Now that it's completed, grab the TID lock and update
3028eb6f0de0SAdrian Chadd 	 * the sequence number and BAW window.
3029eb6f0de0SAdrian Chadd 	 * Because sequence numbers have been assigned to frames
3030eb6f0de0SAdrian Chadd 	 * that haven't been sent yet, it's entirely possible
3031eb6f0de0SAdrian Chadd 	 * we'll be called with some pending frames that have not
3032eb6f0de0SAdrian Chadd 	 * been transmitted.
3033eb6f0de0SAdrian Chadd 	 *
3034eb6f0de0SAdrian Chadd 	 * The cleaner solution is to do the sequence number allocation
3035eb6f0de0SAdrian Chadd 	 * when the packet is first transmitted - and thus the "retries"
3036eb6f0de0SAdrian Chadd 	 * check above would be enough to update the BAW/seqno.
3037eb6f0de0SAdrian Chadd 	 */
3038eb6f0de0SAdrian Chadd 
3039eb6f0de0SAdrian Chadd 	/* But don't do it for non-QoS TIDs */
3040eb6f0de0SAdrian Chadd 	if (tap) {
3041eb6f0de0SAdrian Chadd #if 0
3042eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
3043eb6f0de0SAdrian Chadd 		    "%s: node %p: TID %d: sliding BAW left edge to %d\n",
3044eb6f0de0SAdrian Chadd 		    __func__, an, tid->tid, tap->txa_start);
3045eb6f0de0SAdrian Chadd #endif
3046eb6f0de0SAdrian Chadd 		ni->ni_txseqs[tid->tid] = tap->txa_start;
3047eb6f0de0SAdrian Chadd 		tid->baw_tail = tid->baw_head;
3048eb6f0de0SAdrian Chadd 	}
3049eb6f0de0SAdrian Chadd }
3050eb6f0de0SAdrian Chadd 
3051eb6f0de0SAdrian Chadd /*
3052eb6f0de0SAdrian Chadd  * Flush all software queued packets for the given node.
3053eb6f0de0SAdrian Chadd  *
3054eb6f0de0SAdrian Chadd  * This occurs when a completion handler frees the last buffer
3055eb6f0de0SAdrian Chadd  * for a node, and the node is thus freed. This causes the node
3056eb6f0de0SAdrian Chadd  * to be cleaned up, which ends up calling ath_tx_node_flush.
3057eb6f0de0SAdrian Chadd  */
3058eb6f0de0SAdrian Chadd void
3059eb6f0de0SAdrian Chadd ath_tx_node_flush(struct ath_softc *sc, struct ath_node *an)
3060eb6f0de0SAdrian Chadd {
3061eb6f0de0SAdrian Chadd 	int tid;
3062eb6f0de0SAdrian Chadd 	ath_bufhead bf_cq;
3063eb6f0de0SAdrian Chadd 	struct ath_buf *bf;
3064eb6f0de0SAdrian Chadd 
3065eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_cq);
3066eb6f0de0SAdrian Chadd 
3067eb6f0de0SAdrian Chadd 	for (tid = 0; tid < IEEE80211_TID_SIZE; tid++) {
3068eb6f0de0SAdrian Chadd 		struct ath_tid *atid = &an->an_tid[tid];
3069eb6f0de0SAdrian Chadd 		struct ath_txq *txq = sc->sc_ac2q[atid->ac];
3070eb6f0de0SAdrian Chadd 
3071eb6f0de0SAdrian Chadd 		/* Remove this tid from the list of active tids */
3072eb6f0de0SAdrian Chadd 		ATH_TXQ_LOCK(txq);
3073eb6f0de0SAdrian Chadd 		ath_tx_tid_unsched(sc, atid);
3074eb6f0de0SAdrian Chadd 
3075eb6f0de0SAdrian Chadd 		/* Free packets */
3076eb6f0de0SAdrian Chadd 		ath_tx_tid_drain(sc, an, atid, &bf_cq);
3077eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
3078eb6f0de0SAdrian Chadd 	}
3079eb6f0de0SAdrian Chadd 
3080eb6f0de0SAdrian Chadd 	/* Handle completed frames */
3081eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
3082eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
3083eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
3084eb6f0de0SAdrian Chadd 	}
3085eb6f0de0SAdrian Chadd }
3086eb6f0de0SAdrian Chadd 
3087eb6f0de0SAdrian Chadd /*
3088eb6f0de0SAdrian Chadd  * Drain all the software TXQs currently with traffic queued.
3089eb6f0de0SAdrian Chadd  */
3090eb6f0de0SAdrian Chadd void
3091eb6f0de0SAdrian Chadd ath_tx_txq_drain(struct ath_softc *sc, struct ath_txq *txq)
3092eb6f0de0SAdrian Chadd {
3093eb6f0de0SAdrian Chadd 	struct ath_tid *tid;
3094eb6f0de0SAdrian Chadd 	ath_bufhead bf_cq;
3095eb6f0de0SAdrian Chadd 	struct ath_buf *bf;
3096eb6f0de0SAdrian Chadd 
3097eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_cq);
3098eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(txq);
3099eb6f0de0SAdrian Chadd 
3100eb6f0de0SAdrian Chadd 	/*
3101eb6f0de0SAdrian Chadd 	 * Iterate over all active tids for the given txq,
3102eb6f0de0SAdrian Chadd 	 * flushing and unsched'ing them
3103eb6f0de0SAdrian Chadd 	 */
3104eb6f0de0SAdrian Chadd 	while (! TAILQ_EMPTY(&txq->axq_tidq)) {
3105eb6f0de0SAdrian Chadd 		tid = TAILQ_FIRST(&txq->axq_tidq);
3106eb6f0de0SAdrian Chadd 		ath_tx_tid_drain(sc, tid->an, tid, &bf_cq);
3107eb6f0de0SAdrian Chadd 		ath_tx_tid_unsched(sc, tid);
3108eb6f0de0SAdrian Chadd 	}
3109eb6f0de0SAdrian Chadd 
3110eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(txq);
3111eb6f0de0SAdrian Chadd 
3112eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
3113eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
3114eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
3115eb6f0de0SAdrian Chadd 	}
3116eb6f0de0SAdrian Chadd }
3117eb6f0de0SAdrian Chadd 
3118eb6f0de0SAdrian Chadd /*
3119eb6f0de0SAdrian Chadd  * Handle completion of non-aggregate session frames.
3120eb6f0de0SAdrian Chadd  */
3121eb6f0de0SAdrian Chadd void
3122eb6f0de0SAdrian Chadd ath_tx_normal_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
3123eb6f0de0SAdrian Chadd {
3124eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
3125eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
3126eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
3127eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
3128eb6f0de0SAdrian Chadd 	struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
3129eb6f0de0SAdrian Chadd 
3130eb6f0de0SAdrian Chadd 	/* The TID state is protected behind the TXQ lock */
3131eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
3132eb6f0de0SAdrian Chadd 
3133eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bf=%p: fail=%d, hwq_depth now %d\n",
3134eb6f0de0SAdrian Chadd 	    __func__, bf, fail, atid->hwq_depth - 1);
3135eb6f0de0SAdrian Chadd 
3136eb6f0de0SAdrian Chadd 	atid->hwq_depth--;
3137eb6f0de0SAdrian Chadd 	if (atid->hwq_depth < 0)
3138eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: hwq_depth < 0: %d\n",
3139eb6f0de0SAdrian Chadd 		    __func__, atid->hwq_depth);
3140eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3141eb6f0de0SAdrian Chadd 
3142eb6f0de0SAdrian Chadd 	/*
3143eb6f0de0SAdrian Chadd 	 * punt to rate control if we're not being cleaned up
3144eb6f0de0SAdrian Chadd 	 * during a hw queue drain and the frame wanted an ACK.
3145eb6f0de0SAdrian Chadd 	 */
3146875a9451SAdrian Chadd 	if (fail == 0 && ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0))
3147eb6f0de0SAdrian Chadd 		ath_tx_update_ratectrl(sc, ni, bf->bf_state.bfs_rc,
3148eb6f0de0SAdrian Chadd 		    ts, bf->bf_state.bfs_pktlen,
3149eb6f0de0SAdrian Chadd 		    1, (ts->ts_status == 0) ? 0 : 1);
3150eb6f0de0SAdrian Chadd 
3151eb6f0de0SAdrian Chadd 	ath_tx_default_comp(sc, bf, fail);
3152eb6f0de0SAdrian Chadd }
3153eb6f0de0SAdrian Chadd 
3154eb6f0de0SAdrian Chadd /*
3155eb6f0de0SAdrian Chadd  * Handle cleanup of aggregate session packets that aren't
3156eb6f0de0SAdrian Chadd  * an A-MPDU.
3157eb6f0de0SAdrian Chadd  *
3158eb6f0de0SAdrian Chadd  * There's no need to update the BAW here - the session is being
3159eb6f0de0SAdrian Chadd  * torn down.
3160eb6f0de0SAdrian Chadd  */
3161eb6f0de0SAdrian Chadd static void
3162eb6f0de0SAdrian Chadd ath_tx_comp_cleanup_unaggr(struct ath_softc *sc, struct ath_buf *bf)
3163eb6f0de0SAdrian Chadd {
3164eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
3165eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
3166eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
3167eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
3168eb6f0de0SAdrian Chadd 
3169eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: TID %d: incomp=%d\n",
3170eb6f0de0SAdrian Chadd 	    __func__, tid, atid->incomp);
3171eb6f0de0SAdrian Chadd 
3172eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
3173eb6f0de0SAdrian Chadd 	atid->incomp--;
3174eb6f0de0SAdrian Chadd 	if (atid->incomp == 0) {
3175eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
3176eb6f0de0SAdrian Chadd 		    "%s: TID %d: cleaned up! resume!\n",
3177eb6f0de0SAdrian Chadd 		    __func__, tid);
3178eb6f0de0SAdrian Chadd 		atid->cleanup_inprogress = 0;
3179eb6f0de0SAdrian Chadd 		ath_tx_tid_resume(sc, atid);
3180eb6f0de0SAdrian Chadd 	}
3181eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3182eb6f0de0SAdrian Chadd 
3183eb6f0de0SAdrian Chadd 	ath_tx_default_comp(sc, bf, 0);
3184eb6f0de0SAdrian Chadd }
3185eb6f0de0SAdrian Chadd 
3186eb6f0de0SAdrian Chadd /*
3187eb6f0de0SAdrian Chadd  * Performs transmit side cleanup when TID changes from aggregated to
3188eb6f0de0SAdrian Chadd  * unaggregated.
3189eb6f0de0SAdrian Chadd  *
3190eb6f0de0SAdrian Chadd  * - Discard all retry frames from the s/w queue.
3191eb6f0de0SAdrian Chadd  * - Fix the tx completion function for all buffers in s/w queue.
3192eb6f0de0SAdrian Chadd  * - Count the number of unacked frames, and let transmit completion
3193eb6f0de0SAdrian Chadd  *   handle it later.
3194eb6f0de0SAdrian Chadd  *
3195eb6f0de0SAdrian Chadd  * The caller is responsible for pausing the TID.
3196eb6f0de0SAdrian Chadd  */
3197eb6f0de0SAdrian Chadd static void
31984dfd4507SAdrian Chadd ath_tx_tid_cleanup(struct ath_softc *sc, struct ath_node *an, int tid)
3199eb6f0de0SAdrian Chadd {
3200eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
3201eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
3202eb6f0de0SAdrian Chadd 	struct ath_buf *bf, *bf_next;
3203eb6f0de0SAdrian Chadd 	ath_bufhead bf_cq;
3204eb6f0de0SAdrian Chadd 
3205d3a6425bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
3206eb6f0de0SAdrian Chadd 	    "%s: TID %d: called\n", __func__, tid);
3207eb6f0de0SAdrian Chadd 
3208eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_cq);
3209eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
3210eb6f0de0SAdrian Chadd 
3211eb6f0de0SAdrian Chadd 	/*
3212eb6f0de0SAdrian Chadd 	 * Update the frames in the software TX queue:
3213eb6f0de0SAdrian Chadd 	 *
3214eb6f0de0SAdrian Chadd 	 * + Discard retry frames in the queue
3215eb6f0de0SAdrian Chadd 	 * + Fix the completion function to be non-aggregate
3216eb6f0de0SAdrian Chadd 	 */
3217eb6f0de0SAdrian Chadd 	bf = TAILQ_FIRST(&atid->axq_q);
3218eb6f0de0SAdrian Chadd 	while (bf) {
3219eb6f0de0SAdrian Chadd 		if (bf->bf_state.bfs_isretried) {
3220eb6f0de0SAdrian Chadd 			bf_next = TAILQ_NEXT(bf, bf_list);
3221eb6f0de0SAdrian Chadd 			TAILQ_REMOVE(&atid->axq_q, bf, bf_list);
3222eb6f0de0SAdrian Chadd 			atid->axq_depth--;
3223eb6f0de0SAdrian Chadd 			if (bf->bf_state.bfs_dobaw) {
3224eb6f0de0SAdrian Chadd 				ath_tx_update_baw(sc, an, atid, bf);
3225eb6f0de0SAdrian Chadd 				if (! bf->bf_state.bfs_addedbaw)
3226eb6f0de0SAdrian Chadd 					device_printf(sc->sc_dev,
3227eb6f0de0SAdrian Chadd 					    "%s: wasn't added: seqno %d\n",
3228d4365d16SAdrian Chadd 					    __func__,
3229d4365d16SAdrian Chadd 					    SEQNO(bf->bf_state.bfs_seqno));
3230eb6f0de0SAdrian Chadd 			}
3231eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_dobaw = 0;
3232eb6f0de0SAdrian Chadd 			/*
3233eb6f0de0SAdrian Chadd 			 * Call the default completion handler with "fail" just
3234eb6f0de0SAdrian Chadd 			 * so upper levels are suitably notified about this.
3235eb6f0de0SAdrian Chadd 			 */
3236eb6f0de0SAdrian Chadd 			TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
3237eb6f0de0SAdrian Chadd 			bf = bf_next;
3238eb6f0de0SAdrian Chadd 			continue;
3239eb6f0de0SAdrian Chadd 		}
3240eb6f0de0SAdrian Chadd 		/* Give these the default completion handler */
3241eb6f0de0SAdrian Chadd 		bf->bf_comp = ath_tx_normal_comp;
3242eb6f0de0SAdrian Chadd 		bf = TAILQ_NEXT(bf, bf_list);
3243eb6f0de0SAdrian Chadd 	}
3244eb6f0de0SAdrian Chadd 
3245eb6f0de0SAdrian Chadd 	/* The caller is required to pause the TID */
3246eb6f0de0SAdrian Chadd #if 0
3247eb6f0de0SAdrian Chadd 	/* Pause the TID */
3248eb6f0de0SAdrian Chadd 	ath_tx_tid_pause(sc, atid);
3249eb6f0de0SAdrian Chadd #endif
3250eb6f0de0SAdrian Chadd 
3251eb6f0de0SAdrian Chadd 	/*
3252eb6f0de0SAdrian Chadd 	 * Calculate what hardware-queued frames exist based
3253eb6f0de0SAdrian Chadd 	 * on the current BAW size. Ie, what frames have been
3254eb6f0de0SAdrian Chadd 	 * added to the TX hardware queue for this TID but
3255eb6f0de0SAdrian Chadd 	 * not yet ACKed.
3256eb6f0de0SAdrian Chadd 	 */
3257eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
3258eb6f0de0SAdrian Chadd 	/* Need the lock - fiddling with BAW */
3259eb6f0de0SAdrian Chadd 	while (atid->baw_head != atid->baw_tail) {
3260eb6f0de0SAdrian Chadd 		if (atid->tx_buf[atid->baw_head]) {
3261eb6f0de0SAdrian Chadd 			atid->incomp++;
3262eb6f0de0SAdrian Chadd 			atid->cleanup_inprogress = 1;
3263eb6f0de0SAdrian Chadd 			atid->tx_buf[atid->baw_head] = NULL;
3264eb6f0de0SAdrian Chadd 		}
3265eb6f0de0SAdrian Chadd 		INCR(atid->baw_head, ATH_TID_MAX_BUFS);
3266eb6f0de0SAdrian Chadd 		INCR(tap->txa_start, IEEE80211_SEQ_RANGE);
3267eb6f0de0SAdrian Chadd 	}
3268eb6f0de0SAdrian Chadd 
3269eb6f0de0SAdrian Chadd 	/*
3270eb6f0de0SAdrian Chadd 	 * If cleanup is required, defer TID scheduling
3271eb6f0de0SAdrian Chadd 	 * until all the HW queued packets have been
3272eb6f0de0SAdrian Chadd 	 * sent.
3273eb6f0de0SAdrian Chadd 	 */
3274eb6f0de0SAdrian Chadd 	if (! atid->cleanup_inprogress)
3275eb6f0de0SAdrian Chadd 		ath_tx_tid_resume(sc, atid);
3276eb6f0de0SAdrian Chadd 
3277eb6f0de0SAdrian Chadd 	if (atid->cleanup_inprogress)
3278eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
3279eb6f0de0SAdrian Chadd 		    "%s: TID %d: cleanup needed: %d packets\n",
3280eb6f0de0SAdrian Chadd 		    __func__, tid, atid->incomp);
3281eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3282eb6f0de0SAdrian Chadd 
3283eb6f0de0SAdrian Chadd 	/* Handle completing frames and fail them */
3284eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
3285eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
3286eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 1);
3287eb6f0de0SAdrian Chadd 	}
3288eb6f0de0SAdrian Chadd }
3289eb6f0de0SAdrian Chadd 
3290eb6f0de0SAdrian Chadd static void
3291eb6f0de0SAdrian Chadd ath_tx_set_retry(struct ath_softc *sc, struct ath_buf *bf)
3292eb6f0de0SAdrian Chadd {
3293eb6f0de0SAdrian Chadd 	struct ieee80211_frame *wh;
3294eb6f0de0SAdrian Chadd 
3295eb6f0de0SAdrian Chadd 	wh = mtod(bf->bf_m, struct ieee80211_frame *);
3296eb6f0de0SAdrian Chadd 	/* Only update/resync if needed */
3297eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_isretried == 0) {
3298eb6f0de0SAdrian Chadd 		wh->i_fc[1] |= IEEE80211_FC1_RETRY;
3299eb6f0de0SAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
3300eb6f0de0SAdrian Chadd 		    BUS_DMASYNC_PREWRITE);
3301eb6f0de0SAdrian Chadd 	}
3302eb6f0de0SAdrian Chadd 	sc->sc_stats.ast_tx_swretries++;
3303eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_isretried = 1;
3304eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_retries ++;
3305eb6f0de0SAdrian Chadd }
3306eb6f0de0SAdrian Chadd 
3307eb6f0de0SAdrian Chadd static struct ath_buf *
330838962489SAdrian Chadd ath_tx_retry_clone(struct ath_softc *sc, struct ath_node *an,
330938962489SAdrian Chadd     struct ath_tid *tid, struct ath_buf *bf)
3310eb6f0de0SAdrian Chadd {
3311eb6f0de0SAdrian Chadd 	struct ath_buf *nbf;
3312eb6f0de0SAdrian Chadd 	int error;
3313eb6f0de0SAdrian Chadd 
3314eb6f0de0SAdrian Chadd 	nbf = ath_buf_clone(sc, bf);
3315eb6f0de0SAdrian Chadd 
3316eb6f0de0SAdrian Chadd #if 0
3317eb6f0de0SAdrian Chadd 	device_printf(sc->sc_dev, "%s: ATH_BUF_BUSY; cloning\n",
3318eb6f0de0SAdrian Chadd 	    __func__);
3319eb6f0de0SAdrian Chadd #endif
3320eb6f0de0SAdrian Chadd 
3321eb6f0de0SAdrian Chadd 	if (nbf == NULL) {
3322eb6f0de0SAdrian Chadd 		/* Failed to clone */
3323eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
3324eb6f0de0SAdrian Chadd 		    "%s: failed to clone a busy buffer\n",
3325eb6f0de0SAdrian Chadd 		    __func__);
3326eb6f0de0SAdrian Chadd 		return NULL;
3327eb6f0de0SAdrian Chadd 	}
3328eb6f0de0SAdrian Chadd 
3329eb6f0de0SAdrian Chadd 	/* Setup the dma for the new buffer */
3330eb6f0de0SAdrian Chadd 	error = ath_tx_dmasetup(sc, nbf, nbf->bf_m);
3331eb6f0de0SAdrian Chadd 	if (error != 0) {
3332eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
3333eb6f0de0SAdrian Chadd 		    "%s: failed to setup dma for clone\n",
3334eb6f0de0SAdrian Chadd 		    __func__);
3335eb6f0de0SAdrian Chadd 		/*
3336eb6f0de0SAdrian Chadd 		 * Put this at the head of the list, not tail;
3337eb6f0de0SAdrian Chadd 		 * that way it doesn't interfere with the
3338eb6f0de0SAdrian Chadd 		 * busy buffer logic (which uses the tail of
3339eb6f0de0SAdrian Chadd 		 * the list.)
3340eb6f0de0SAdrian Chadd 		 */
3341eb6f0de0SAdrian Chadd 		ATH_TXBUF_LOCK(sc);
334232c387f7SAdrian Chadd 		ath_returnbuf_head(sc, nbf);
3343eb6f0de0SAdrian Chadd 		ATH_TXBUF_UNLOCK(sc);
3344eb6f0de0SAdrian Chadd 		return NULL;
3345eb6f0de0SAdrian Chadd 	}
3346eb6f0de0SAdrian Chadd 
334738962489SAdrian Chadd 	/* Update BAW if required, before we free the original buf */
334838962489SAdrian Chadd 	if (bf->bf_state.bfs_dobaw)
334938962489SAdrian Chadd 		ath_tx_switch_baw_buf(sc, an, tid, bf, nbf);
335038962489SAdrian Chadd 
3351eb6f0de0SAdrian Chadd 	/* Free current buffer; return the older buffer */
3352eb6f0de0SAdrian Chadd 	bf->bf_m = NULL;
3353eb6f0de0SAdrian Chadd 	bf->bf_node = NULL;
3354eb6f0de0SAdrian Chadd 	ath_freebuf(sc, bf);
3355eb6f0de0SAdrian Chadd 	return nbf;
3356eb6f0de0SAdrian Chadd }
3357eb6f0de0SAdrian Chadd 
3358eb6f0de0SAdrian Chadd /*
3359eb6f0de0SAdrian Chadd  * Handle retrying an unaggregate frame in an aggregate
3360eb6f0de0SAdrian Chadd  * session.
3361eb6f0de0SAdrian Chadd  *
3362eb6f0de0SAdrian Chadd  * If too many retries occur, pause the TID, wait for
3363eb6f0de0SAdrian Chadd  * any further retransmits (as there's no reason why
3364eb6f0de0SAdrian Chadd  * non-aggregate frames in an aggregate session are
3365eb6f0de0SAdrian Chadd  * transmitted in-order; they just have to be in-BAW)
3366eb6f0de0SAdrian Chadd  * and then queue a BAR.
3367eb6f0de0SAdrian Chadd  */
3368eb6f0de0SAdrian Chadd static void
3369eb6f0de0SAdrian Chadd ath_tx_aggr_retry_unaggr(struct ath_softc *sc, struct ath_buf *bf)
3370eb6f0de0SAdrian Chadd {
3371eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
3372eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
3373eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
3374eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
3375eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
3376eb6f0de0SAdrian Chadd 
3377eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
3378eb6f0de0SAdrian Chadd 
3379eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
3380eb6f0de0SAdrian Chadd 
3381eb6f0de0SAdrian Chadd 	/*
3382eb6f0de0SAdrian Chadd 	 * If the buffer is marked as busy, we can't directly
3383eb6f0de0SAdrian Chadd 	 * reuse it. Instead, try to clone the buffer.
3384eb6f0de0SAdrian Chadd 	 * If the clone is successful, recycle the old buffer.
3385eb6f0de0SAdrian Chadd 	 * If the clone is unsuccessful, set bfs_retries to max
3386eb6f0de0SAdrian Chadd 	 * to force the next bit of code to free the buffer
3387eb6f0de0SAdrian Chadd 	 * for us.
3388eb6f0de0SAdrian Chadd 	 */
3389eb6f0de0SAdrian Chadd 	if ((bf->bf_state.bfs_retries < SWMAX_RETRIES) &&
3390eb6f0de0SAdrian Chadd 	    (bf->bf_flags & ATH_BUF_BUSY)) {
3391eb6f0de0SAdrian Chadd 		struct ath_buf *nbf;
339238962489SAdrian Chadd 		nbf = ath_tx_retry_clone(sc, an, atid, bf);
3393eb6f0de0SAdrian Chadd 		if (nbf)
3394eb6f0de0SAdrian Chadd 			/* bf has been freed at this point */
3395eb6f0de0SAdrian Chadd 			bf = nbf;
3396eb6f0de0SAdrian Chadd 		else
3397eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_retries = SWMAX_RETRIES + 1;
3398eb6f0de0SAdrian Chadd 	}
3399eb6f0de0SAdrian Chadd 
3400eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_retries >= SWMAX_RETRIES) {
3401eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_RETRIES,
3402eb6f0de0SAdrian Chadd 		    "%s: exceeded retries; seqno %d\n",
3403eb6f0de0SAdrian Chadd 		    __func__, SEQNO(bf->bf_state.bfs_seqno));
3404eb6f0de0SAdrian Chadd 		sc->sc_stats.ast_tx_swretrymax++;
3405eb6f0de0SAdrian Chadd 
3406eb6f0de0SAdrian Chadd 		/* Update BAW anyway */
3407eb6f0de0SAdrian Chadd 		if (bf->bf_state.bfs_dobaw) {
3408eb6f0de0SAdrian Chadd 			ath_tx_update_baw(sc, an, atid, bf);
3409eb6f0de0SAdrian Chadd 			if (! bf->bf_state.bfs_addedbaw)
3410eb6f0de0SAdrian Chadd 				device_printf(sc->sc_dev,
3411eb6f0de0SAdrian Chadd 				    "%s: wasn't added: seqno %d\n",
3412eb6f0de0SAdrian Chadd 				    __func__, SEQNO(bf->bf_state.bfs_seqno));
3413eb6f0de0SAdrian Chadd 		}
3414eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_dobaw = 0;
3415eb6f0de0SAdrian Chadd 
341688b3d483SAdrian Chadd 		/* Suspend the TX queue and get ready to send the BAR */
341788b3d483SAdrian Chadd 		ath_tx_tid_bar_suspend(sc, atid);
341888b3d483SAdrian Chadd 
341988b3d483SAdrian Chadd 		/* Send the BAR if there are no other frames waiting */
342088b3d483SAdrian Chadd 		if (ath_tx_tid_bar_tx_ready(sc, atid))
342188b3d483SAdrian Chadd 			ath_tx_tid_bar_tx(sc, atid);
342288b3d483SAdrian Chadd 
3423eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3424eb6f0de0SAdrian Chadd 
3425eb6f0de0SAdrian Chadd 		/* Free buffer, bf is free after this call */
3426eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
3427eb6f0de0SAdrian Chadd 		return;
3428eb6f0de0SAdrian Chadd 	}
3429eb6f0de0SAdrian Chadd 
3430eb6f0de0SAdrian Chadd 	/*
3431eb6f0de0SAdrian Chadd 	 * This increments the retry counter as well as
3432eb6f0de0SAdrian Chadd 	 * sets the retry flag in the ath_buf and packet
3433eb6f0de0SAdrian Chadd 	 * body.
3434eb6f0de0SAdrian Chadd 	 */
3435eb6f0de0SAdrian Chadd 	ath_tx_set_retry(sc, bf);
3436eb6f0de0SAdrian Chadd 
3437eb6f0de0SAdrian Chadd 	/*
3438eb6f0de0SAdrian Chadd 	 * Insert this at the head of the queue, so it's
3439eb6f0de0SAdrian Chadd 	 * retried before any current/subsequent frames.
3440eb6f0de0SAdrian Chadd 	 */
3441eb6f0de0SAdrian Chadd 	ATH_TXQ_INSERT_HEAD(atid, bf, bf_list);
3442eb6f0de0SAdrian Chadd 	ath_tx_tid_sched(sc, atid);
344388b3d483SAdrian Chadd 	/* Send the BAR if there are no other frames waiting */
344488b3d483SAdrian Chadd 	if (ath_tx_tid_bar_tx_ready(sc, atid))
344588b3d483SAdrian Chadd 		ath_tx_tid_bar_tx(sc, atid);
3446eb6f0de0SAdrian Chadd 
3447eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3448eb6f0de0SAdrian Chadd }
3449eb6f0de0SAdrian Chadd 
3450eb6f0de0SAdrian Chadd /*
3451eb6f0de0SAdrian Chadd  * Common code for aggregate excessive retry/subframe retry.
3452eb6f0de0SAdrian Chadd  * If retrying, queues buffers to bf_q. If not, frees the
3453eb6f0de0SAdrian Chadd  * buffers.
3454eb6f0de0SAdrian Chadd  *
3455eb6f0de0SAdrian Chadd  * XXX should unify this with ath_tx_aggr_retry_unaggr()
3456eb6f0de0SAdrian Chadd  */
3457eb6f0de0SAdrian Chadd static int
3458eb6f0de0SAdrian Chadd ath_tx_retry_subframe(struct ath_softc *sc, struct ath_buf *bf,
3459eb6f0de0SAdrian Chadd     ath_bufhead *bf_q)
3460eb6f0de0SAdrian Chadd {
3461eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
3462eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
3463eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
3464eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
3465eb6f0de0SAdrian Chadd 
3466eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[atid->ac]);
3467eb6f0de0SAdrian Chadd 
346821840808SAdrian Chadd 	/* XXX clr11naggr should be done for all subframes */
3469eb6f0de0SAdrian Chadd 	ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
3470eb6f0de0SAdrian Chadd 	ath_hal_set11nburstduration(sc->sc_ah, bf->bf_desc, 0);
3471eb6f0de0SAdrian Chadd 	/* ath_hal_set11n_virtualmorefrag(sc->sc_ah, bf->bf_desc, 0); */
3472eb6f0de0SAdrian Chadd 
3473eb6f0de0SAdrian Chadd 	/*
3474eb6f0de0SAdrian Chadd 	 * If the buffer is marked as busy, we can't directly
3475eb6f0de0SAdrian Chadd 	 * reuse it. Instead, try to clone the buffer.
3476eb6f0de0SAdrian Chadd 	 * If the clone is successful, recycle the old buffer.
3477eb6f0de0SAdrian Chadd 	 * If the clone is unsuccessful, set bfs_retries to max
3478eb6f0de0SAdrian Chadd 	 * to force the next bit of code to free the buffer
3479eb6f0de0SAdrian Chadd 	 * for us.
3480eb6f0de0SAdrian Chadd 	 */
3481eb6f0de0SAdrian Chadd 	if ((bf->bf_state.bfs_retries < SWMAX_RETRIES) &&
3482eb6f0de0SAdrian Chadd 	    (bf->bf_flags & ATH_BUF_BUSY)) {
3483eb6f0de0SAdrian Chadd 		struct ath_buf *nbf;
348438962489SAdrian Chadd 		nbf = ath_tx_retry_clone(sc, an, atid, bf);
3485eb6f0de0SAdrian Chadd 		if (nbf)
3486eb6f0de0SAdrian Chadd 			/* bf has been freed at this point */
3487eb6f0de0SAdrian Chadd 			bf = nbf;
3488eb6f0de0SAdrian Chadd 		else
3489eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_retries = SWMAX_RETRIES + 1;
3490eb6f0de0SAdrian Chadd 	}
3491eb6f0de0SAdrian Chadd 
3492eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_retries >= SWMAX_RETRIES) {
3493eb6f0de0SAdrian Chadd 		sc->sc_stats.ast_tx_swretrymax++;
3494eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_RETRIES,
3495eb6f0de0SAdrian Chadd 		    "%s: max retries: seqno %d\n",
3496eb6f0de0SAdrian Chadd 		    __func__, SEQNO(bf->bf_state.bfs_seqno));
3497eb6f0de0SAdrian Chadd 		ath_tx_update_baw(sc, an, atid, bf);
3498eb6f0de0SAdrian Chadd 		if (! bf->bf_state.bfs_addedbaw)
3499eb6f0de0SAdrian Chadd 			device_printf(sc->sc_dev,
3500eb6f0de0SAdrian Chadd 			    "%s: wasn't added: seqno %d\n",
3501eb6f0de0SAdrian Chadd 			    __func__, SEQNO(bf->bf_state.bfs_seqno));
3502eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_dobaw = 0;
3503eb6f0de0SAdrian Chadd 		return 1;
3504eb6f0de0SAdrian Chadd 	}
3505eb6f0de0SAdrian Chadd 
3506eb6f0de0SAdrian Chadd 	ath_tx_set_retry(sc, bf);
3507eb6f0de0SAdrian Chadd 	bf->bf_next = NULL;		/* Just to make sure */
3508eb6f0de0SAdrian Chadd 
350921840808SAdrian Chadd 	/* Clear the aggregate state */
351021840808SAdrian Chadd 	bf->bf_state.bfs_aggr = 0;
351121840808SAdrian Chadd 	bf->bf_state.bfs_ndelim = 0;	/* ??? needed? */
351221840808SAdrian Chadd 	bf->bf_state.bfs_nframes = 1;
351321840808SAdrian Chadd 
3514eb6f0de0SAdrian Chadd 	TAILQ_INSERT_TAIL(bf_q, bf, bf_list);
3515eb6f0de0SAdrian Chadd 	return 0;
3516eb6f0de0SAdrian Chadd }
3517eb6f0de0SAdrian Chadd 
3518eb6f0de0SAdrian Chadd /*
3519eb6f0de0SAdrian Chadd  * error pkt completion for an aggregate destination
3520eb6f0de0SAdrian Chadd  */
3521eb6f0de0SAdrian Chadd static void
3522eb6f0de0SAdrian Chadd ath_tx_comp_aggr_error(struct ath_softc *sc, struct ath_buf *bf_first,
3523eb6f0de0SAdrian Chadd     struct ath_tid *tid)
3524eb6f0de0SAdrian Chadd {
3525eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf_first->bf_node;
3526eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
3527eb6f0de0SAdrian Chadd 	struct ath_buf *bf_next, *bf;
3528eb6f0de0SAdrian Chadd 	ath_bufhead bf_q;
3529eb6f0de0SAdrian Chadd 	int drops = 0;
3530eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
3531eb6f0de0SAdrian Chadd 	ath_bufhead bf_cq;
3532eb6f0de0SAdrian Chadd 
3533eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_q);
3534eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_cq);
3535eb6f0de0SAdrian Chadd 
3536eb6f0de0SAdrian Chadd 	/*
3537eb6f0de0SAdrian Chadd 	 * Update rate control - all frames have failed.
3538eb6f0de0SAdrian Chadd 	 *
3539eb6f0de0SAdrian Chadd 	 * XXX use the length in the first frame in the series;
3540eb6f0de0SAdrian Chadd 	 * XXX just so things are consistent for now.
3541eb6f0de0SAdrian Chadd 	 */
3542eb6f0de0SAdrian Chadd 	ath_tx_update_ratectrl(sc, ni, bf_first->bf_state.bfs_rc,
3543eb6f0de0SAdrian Chadd 	    &bf_first->bf_status.ds_txstat,
3544eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_pktlen,
3545eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_nframes, bf_first->bf_state.bfs_nframes);
3546eb6f0de0SAdrian Chadd 
3547eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[tid->ac]);
3548eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
35492d3d4776SAdrian Chadd 	sc->sc_stats.ast_tx_aggr_failall++;
3550eb6f0de0SAdrian Chadd 
3551eb6f0de0SAdrian Chadd 	/* Retry all subframes */
3552eb6f0de0SAdrian Chadd 	bf = bf_first;
3553eb6f0de0SAdrian Chadd 	while (bf) {
3554eb6f0de0SAdrian Chadd 		bf_next = bf->bf_next;
3555eb6f0de0SAdrian Chadd 		bf->bf_next = NULL;	/* Remove it from the aggr list */
35562d3d4776SAdrian Chadd 		sc->sc_stats.ast_tx_aggr_fail++;
3557eb6f0de0SAdrian Chadd 		if (ath_tx_retry_subframe(sc, bf, &bf_q)) {
3558eb6f0de0SAdrian Chadd 			drops++;
3559eb6f0de0SAdrian Chadd 			bf->bf_next = NULL;
3560eb6f0de0SAdrian Chadd 			TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
3561eb6f0de0SAdrian Chadd 		}
3562eb6f0de0SAdrian Chadd 		bf = bf_next;
3563eb6f0de0SAdrian Chadd 	}
3564eb6f0de0SAdrian Chadd 
3565eb6f0de0SAdrian Chadd 	/* Prepend all frames to the beginning of the queue */
3566eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_LAST(&bf_q, ath_bufhead_s)) != NULL) {
3567eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_q, bf, bf_list);
3568eb6f0de0SAdrian Chadd 		ATH_TXQ_INSERT_HEAD(tid, bf, bf_list);
3569eb6f0de0SAdrian Chadd 	}
3570eb6f0de0SAdrian Chadd 
357139da9d42SAdrian Chadd 	/*
357239da9d42SAdrian Chadd 	 * Schedule the TID to be re-tried.
357339da9d42SAdrian Chadd 	 */
3574eb6f0de0SAdrian Chadd 	ath_tx_tid_sched(sc, tid);
3575eb6f0de0SAdrian Chadd 
3576eb6f0de0SAdrian Chadd 	/*
3577eb6f0de0SAdrian Chadd 	 * send bar if we dropped any frames
3578eb6f0de0SAdrian Chadd 	 *
3579eb6f0de0SAdrian Chadd 	 * Keep the txq lock held for now, as we need to ensure
3580eb6f0de0SAdrian Chadd 	 * that ni_txseqs[] is consistent (as it's being updated
3581eb6f0de0SAdrian Chadd 	 * in the ifnet TX context or raw TX context.)
3582eb6f0de0SAdrian Chadd 	 */
3583eb6f0de0SAdrian Chadd 	if (drops) {
358488b3d483SAdrian Chadd 		/* Suspend the TX queue and get ready to send the BAR */
358588b3d483SAdrian Chadd 		ath_tx_tid_bar_suspend(sc, tid);
3586eb6f0de0SAdrian Chadd 	}
3587eb6f0de0SAdrian Chadd 
358888b3d483SAdrian Chadd 	/*
358988b3d483SAdrian Chadd 	 * Send BAR if required
359088b3d483SAdrian Chadd 	 */
359188b3d483SAdrian Chadd 	if (ath_tx_tid_bar_tx_ready(sc, tid))
359288b3d483SAdrian Chadd 		ath_tx_tid_bar_tx(sc, tid);
359388b3d483SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[tid->ac]);
359488b3d483SAdrian Chadd 
3595eb6f0de0SAdrian Chadd 	/* Complete frames which errored out */
3596eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
3597eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
3598eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
3599eb6f0de0SAdrian Chadd 	}
3600eb6f0de0SAdrian Chadd }
3601eb6f0de0SAdrian Chadd 
3602eb6f0de0SAdrian Chadd /*
3603eb6f0de0SAdrian Chadd  * Handle clean-up of packets from an aggregate list.
3604eb6f0de0SAdrian Chadd  *
3605eb6f0de0SAdrian Chadd  * There's no need to update the BAW here - the session is being
3606eb6f0de0SAdrian Chadd  * torn down.
3607eb6f0de0SAdrian Chadd  */
3608eb6f0de0SAdrian Chadd static void
3609eb6f0de0SAdrian Chadd ath_tx_comp_cleanup_aggr(struct ath_softc *sc, struct ath_buf *bf_first)
3610eb6f0de0SAdrian Chadd {
3611eb6f0de0SAdrian Chadd 	struct ath_buf *bf, *bf_next;
3612eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf_first->bf_node;
3613eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
3614eb6f0de0SAdrian Chadd 	int tid = bf_first->bf_state.bfs_tid;
3615eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
3616eb6f0de0SAdrian Chadd 
3617eb6f0de0SAdrian Chadd 	bf = bf_first;
3618eb6f0de0SAdrian Chadd 
3619eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
3620eb6f0de0SAdrian Chadd 
3621eb6f0de0SAdrian Chadd 	/* update incomp */
3622eb6f0de0SAdrian Chadd 	while (bf) {
3623eb6f0de0SAdrian Chadd 		atid->incomp--;
3624eb6f0de0SAdrian Chadd 		bf = bf->bf_next;
3625eb6f0de0SAdrian Chadd 	}
3626eb6f0de0SAdrian Chadd 
3627eb6f0de0SAdrian Chadd 	if (atid->incomp == 0) {
3628eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
3629eb6f0de0SAdrian Chadd 		    "%s: TID %d: cleaned up! resume!\n",
3630eb6f0de0SAdrian Chadd 		    __func__, tid);
3631eb6f0de0SAdrian Chadd 		atid->cleanup_inprogress = 0;
3632eb6f0de0SAdrian Chadd 		ath_tx_tid_resume(sc, atid);
3633eb6f0de0SAdrian Chadd 	}
363488b3d483SAdrian Chadd 
363588b3d483SAdrian Chadd 	/* Send BAR if required */
363688b3d483SAdrian Chadd 	if (ath_tx_tid_bar_tx_ready(sc, atid))
363788b3d483SAdrian Chadd 		ath_tx_tid_bar_tx(sc, atid);
3638eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3639eb6f0de0SAdrian Chadd 
3640eb6f0de0SAdrian Chadd 	/* Handle frame completion */
3641eb6f0de0SAdrian Chadd 	while (bf) {
3642eb6f0de0SAdrian Chadd 		bf_next = bf->bf_next;
3643eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 1);
3644eb6f0de0SAdrian Chadd 		bf = bf_next;
3645eb6f0de0SAdrian Chadd 	}
3646eb6f0de0SAdrian Chadd }
3647eb6f0de0SAdrian Chadd 
3648eb6f0de0SAdrian Chadd /*
3649eb6f0de0SAdrian Chadd  * Handle completion of an set of aggregate frames.
3650eb6f0de0SAdrian Chadd  *
3651eb6f0de0SAdrian Chadd  * XXX for now, simply complete each sub-frame.
3652eb6f0de0SAdrian Chadd  *
3653eb6f0de0SAdrian Chadd  * Note: the completion handler is the last descriptor in the aggregate,
3654eb6f0de0SAdrian Chadd  * not the last descriptor in the first frame.
3655eb6f0de0SAdrian Chadd  */
3656eb6f0de0SAdrian Chadd static void
3657d4365d16SAdrian Chadd ath_tx_aggr_comp_aggr(struct ath_softc *sc, struct ath_buf *bf_first,
3658d4365d16SAdrian Chadd     int fail)
3659eb6f0de0SAdrian Chadd {
3660eb6f0de0SAdrian Chadd 	//struct ath_desc *ds = bf->bf_lastds;
3661eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf_first->bf_node;
3662eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
3663eb6f0de0SAdrian Chadd 	int tid = bf_first->bf_state.bfs_tid;
3664eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
3665eb6f0de0SAdrian Chadd 	struct ath_tx_status ts;
3666eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
3667eb6f0de0SAdrian Chadd 	ath_bufhead bf_q;
3668eb6f0de0SAdrian Chadd 	ath_bufhead bf_cq;
3669eb6f0de0SAdrian Chadd 	int seq_st, tx_ok;
3670eb6f0de0SAdrian Chadd 	int hasba, isaggr;
3671eb6f0de0SAdrian Chadd 	uint32_t ba[2];
3672eb6f0de0SAdrian Chadd 	struct ath_buf *bf, *bf_next;
3673eb6f0de0SAdrian Chadd 	int ba_index;
3674eb6f0de0SAdrian Chadd 	int drops = 0;
3675eb6f0de0SAdrian Chadd 	int nframes = 0, nbad = 0, nf;
3676eb6f0de0SAdrian Chadd 	int pktlen;
3677eb6f0de0SAdrian Chadd 	/* XXX there's too much on the stack? */
3678b815538dSAdrian Chadd 	struct ath_rc_series rc[ATH_RC_NUM];
3679eb6f0de0SAdrian Chadd 	int txseq;
3680eb6f0de0SAdrian Chadd 
3681eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: called; hwq_depth=%d\n",
3682eb6f0de0SAdrian Chadd 	    __func__, atid->hwq_depth);
3683eb6f0de0SAdrian Chadd 
3684eb6f0de0SAdrian Chadd 	/* The TID state is kept behind the TXQ lock */
3685eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
3686eb6f0de0SAdrian Chadd 
3687eb6f0de0SAdrian Chadd 	atid->hwq_depth--;
3688eb6f0de0SAdrian Chadd 	if (atid->hwq_depth < 0)
3689eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: hwq_depth < 0: %d\n",
3690eb6f0de0SAdrian Chadd 		    __func__, atid->hwq_depth);
3691eb6f0de0SAdrian Chadd 
3692eb6f0de0SAdrian Chadd 	/*
3693eb6f0de0SAdrian Chadd 	 * Punt cleanup to the relevant function, not our problem now
3694eb6f0de0SAdrian Chadd 	 */
3695eb6f0de0SAdrian Chadd 	if (atid->cleanup_inprogress) {
3696eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3697eb6f0de0SAdrian Chadd 		ath_tx_comp_cleanup_aggr(sc, bf_first);
3698eb6f0de0SAdrian Chadd 		return;
3699eb6f0de0SAdrian Chadd 	}
3700eb6f0de0SAdrian Chadd 
3701eb6f0de0SAdrian Chadd 	/*
3702eb6f0de0SAdrian Chadd 	 * Take a copy; this may be needed -after- bf_first
3703eb6f0de0SAdrian Chadd 	 * has been completed and freed.
3704eb6f0de0SAdrian Chadd 	 */
3705eb6f0de0SAdrian Chadd 	ts = bf_first->bf_status.ds_txstat;
3706eb6f0de0SAdrian Chadd 	/*
3707eb6f0de0SAdrian Chadd 	 * XXX for now, use the first frame in the aggregate for
3708eb6f0de0SAdrian Chadd 	 * XXX rate control completion; it's at least consistent.
3709eb6f0de0SAdrian Chadd 	 */
3710eb6f0de0SAdrian Chadd 	pktlen = bf_first->bf_state.bfs_pktlen;
3711eb6f0de0SAdrian Chadd 
3712eb6f0de0SAdrian Chadd 	/*
3713e9a6408eSAdrian Chadd 	 * Handle errors first!
3714e9a6408eSAdrian Chadd 	 *
3715e9a6408eSAdrian Chadd 	 * Here, handle _any_ error as a "exceeded retries" error.
3716e9a6408eSAdrian Chadd 	 * Later on (when filtered frames are to be specially handled)
3717e9a6408eSAdrian Chadd 	 * it'll have to be expanded.
3718eb6f0de0SAdrian Chadd 	 */
3719e9a6408eSAdrian Chadd #if 0
3720eb6f0de0SAdrian Chadd 	if (ts.ts_status & HAL_TXERR_XRETRY) {
3721e9a6408eSAdrian Chadd #endif
3722e9a6408eSAdrian Chadd 	if (ts.ts_status != 0) {
3723eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3724eb6f0de0SAdrian Chadd 		ath_tx_comp_aggr_error(sc, bf_first, atid);
3725eb6f0de0SAdrian Chadd 		return;
3726eb6f0de0SAdrian Chadd 	}
3727eb6f0de0SAdrian Chadd 
3728eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_q);
3729eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_cq);
3730eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
3731eb6f0de0SAdrian Chadd 
3732eb6f0de0SAdrian Chadd 	/*
3733eb6f0de0SAdrian Chadd 	 * extract starting sequence and block-ack bitmap
3734eb6f0de0SAdrian Chadd 	 */
3735eb6f0de0SAdrian Chadd 	/* XXX endian-ness of seq_st, ba? */
3736eb6f0de0SAdrian Chadd 	seq_st = ts.ts_seqnum;
3737eb6f0de0SAdrian Chadd 	hasba = !! (ts.ts_flags & HAL_TX_BA);
3738eb6f0de0SAdrian Chadd 	tx_ok = (ts.ts_status == 0);
3739eb6f0de0SAdrian Chadd 	isaggr = bf_first->bf_state.bfs_aggr;
3740eb6f0de0SAdrian Chadd 	ba[0] = ts.ts_ba_low;
3741eb6f0de0SAdrian Chadd 	ba[1] = ts.ts_ba_high;
3742eb6f0de0SAdrian Chadd 
3743eb6f0de0SAdrian Chadd 	/*
3744eb6f0de0SAdrian Chadd 	 * Copy the TX completion status and the rate control
3745eb6f0de0SAdrian Chadd 	 * series from the first descriptor, as it may be freed
3746eb6f0de0SAdrian Chadd 	 * before the rate control code can get its grubby fingers
3747eb6f0de0SAdrian Chadd 	 * into things.
3748eb6f0de0SAdrian Chadd 	 */
3749eb6f0de0SAdrian Chadd 	memcpy(rc, bf_first->bf_state.bfs_rc, sizeof(rc));
3750eb6f0de0SAdrian Chadd 
3751eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
3752d4365d16SAdrian Chadd 	    "%s: txa_start=%d, tx_ok=%d, status=%.8x, flags=%.8x, "
3753d4365d16SAdrian Chadd 	    "isaggr=%d, seq_st=%d, hasba=%d, ba=%.8x, %.8x\n",
3754eb6f0de0SAdrian Chadd 	    __func__, tap->txa_start, tx_ok, ts.ts_status, ts.ts_flags,
3755eb6f0de0SAdrian Chadd 	    isaggr, seq_st, hasba, ba[0], ba[1]);
3756eb6f0de0SAdrian Chadd 
3757eb6f0de0SAdrian Chadd 	/* Occasionally, the MAC sends a tx status for the wrong TID. */
3758eb6f0de0SAdrian Chadd 	if (tid != ts.ts_tid) {
3759eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: tid %d != hw tid %d\n",
3760eb6f0de0SAdrian Chadd 		    __func__, tid, ts.ts_tid);
3761eb6f0de0SAdrian Chadd 		tx_ok = 0;
3762eb6f0de0SAdrian Chadd 	}
3763eb6f0de0SAdrian Chadd 
3764eb6f0de0SAdrian Chadd 	/* AR5416 BA bug; this requires an interface reset */
3765eb6f0de0SAdrian Chadd 	if (isaggr && tx_ok && (! hasba)) {
3766eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
3767d4365d16SAdrian Chadd 		    "%s: AR5416 bug: hasba=%d; txok=%d, isaggr=%d, "
3768d4365d16SAdrian Chadd 		    "seq_st=%d\n",
3769eb6f0de0SAdrian Chadd 		    __func__, hasba, tx_ok, isaggr, seq_st);
3770eb6f0de0SAdrian Chadd 		/* XXX TODO: schedule an interface reset */
37710f078d63SJohn Baldwin #ifdef ATH_DEBUG
37726abbbae5SAdrian Chadd 		ath_printtxbuf(sc, bf_first,
37736abbbae5SAdrian Chadd 		    sc->sc_ac2q[atid->ac]->axq_qnum, 0, 0);
37740f078d63SJohn Baldwin #endif
3775eb6f0de0SAdrian Chadd 	}
3776eb6f0de0SAdrian Chadd 
3777eb6f0de0SAdrian Chadd 	/*
3778eb6f0de0SAdrian Chadd 	 * Walk the list of frames, figure out which ones were correctly
3779eb6f0de0SAdrian Chadd 	 * sent and which weren't.
3780eb6f0de0SAdrian Chadd 	 */
3781eb6f0de0SAdrian Chadd 	bf = bf_first;
3782eb6f0de0SAdrian Chadd 	nf = bf_first->bf_state.bfs_nframes;
3783eb6f0de0SAdrian Chadd 
3784eb6f0de0SAdrian Chadd 	/* bf_first is going to be invalid once this list is walked */
3785eb6f0de0SAdrian Chadd 	bf_first = NULL;
3786eb6f0de0SAdrian Chadd 
3787eb6f0de0SAdrian Chadd 	/*
3788eb6f0de0SAdrian Chadd 	 * Walk the list of completed frames and determine
3789eb6f0de0SAdrian Chadd 	 * which need to be completed and which need to be
3790eb6f0de0SAdrian Chadd 	 * retransmitted.
3791eb6f0de0SAdrian Chadd 	 *
3792eb6f0de0SAdrian Chadd 	 * For completed frames, the completion functions need
3793eb6f0de0SAdrian Chadd 	 * to be called at the end of this function as the last
3794eb6f0de0SAdrian Chadd 	 * node reference may free the node.
3795eb6f0de0SAdrian Chadd 	 *
3796eb6f0de0SAdrian Chadd 	 * Finally, since the TXQ lock can't be held during the
3797eb6f0de0SAdrian Chadd 	 * completion callback (to avoid lock recursion),
3798eb6f0de0SAdrian Chadd 	 * the completion calls have to be done outside of the
3799eb6f0de0SAdrian Chadd 	 * lock.
3800eb6f0de0SAdrian Chadd 	 */
3801eb6f0de0SAdrian Chadd 	while (bf) {
3802eb6f0de0SAdrian Chadd 		nframes++;
3803d4365d16SAdrian Chadd 		ba_index = ATH_BA_INDEX(seq_st,
3804d4365d16SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
3805eb6f0de0SAdrian Chadd 		bf_next = bf->bf_next;
3806eb6f0de0SAdrian Chadd 		bf->bf_next = NULL;	/* Remove it from the aggr list */
3807eb6f0de0SAdrian Chadd 
3808eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
3809eb6f0de0SAdrian Chadd 		    "%s: checking bf=%p seqno=%d; ack=%d\n",
3810eb6f0de0SAdrian Chadd 		    __func__, bf, SEQNO(bf->bf_state.bfs_seqno),
3811eb6f0de0SAdrian Chadd 		    ATH_BA_ISSET(ba, ba_index));
3812eb6f0de0SAdrian Chadd 
3813eb6f0de0SAdrian Chadd 		if (tx_ok && ATH_BA_ISSET(ba, ba_index)) {
38142d3d4776SAdrian Chadd 			sc->sc_stats.ast_tx_aggr_ok++;
3815eb6f0de0SAdrian Chadd 			ath_tx_update_baw(sc, an, atid, bf);
3816eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_dobaw = 0;
3817eb6f0de0SAdrian Chadd 			if (! bf->bf_state.bfs_addedbaw)
3818eb6f0de0SAdrian Chadd 				device_printf(sc->sc_dev,
3819eb6f0de0SAdrian Chadd 				    "%s: wasn't added: seqno %d\n",
3820eb6f0de0SAdrian Chadd 				    __func__, SEQNO(bf->bf_state.bfs_seqno));
3821eb6f0de0SAdrian Chadd 			bf->bf_next = NULL;
3822eb6f0de0SAdrian Chadd 			TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
3823eb6f0de0SAdrian Chadd 		} else {
38242d3d4776SAdrian Chadd 			sc->sc_stats.ast_tx_aggr_fail++;
3825eb6f0de0SAdrian Chadd 			if (ath_tx_retry_subframe(sc, bf, &bf_q)) {
3826eb6f0de0SAdrian Chadd 				drops++;
3827eb6f0de0SAdrian Chadd 				bf->bf_next = NULL;
3828eb6f0de0SAdrian Chadd 				TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
3829eb6f0de0SAdrian Chadd 			}
3830eb6f0de0SAdrian Chadd 			nbad++;
3831eb6f0de0SAdrian Chadd 		}
3832eb6f0de0SAdrian Chadd 		bf = bf_next;
3833eb6f0de0SAdrian Chadd 	}
3834eb6f0de0SAdrian Chadd 
3835eb6f0de0SAdrian Chadd 	/*
3836eb6f0de0SAdrian Chadd 	 * Now that the BAW updates have been done, unlock
3837eb6f0de0SAdrian Chadd 	 *
3838eb6f0de0SAdrian Chadd 	 * txseq is grabbed before the lock is released so we
3839eb6f0de0SAdrian Chadd 	 * have a consistent view of what -was- in the BAW.
3840eb6f0de0SAdrian Chadd 	 * Anything after this point will not yet have been
3841eb6f0de0SAdrian Chadd 	 * TXed.
3842eb6f0de0SAdrian Chadd 	 */
3843eb6f0de0SAdrian Chadd 	txseq = tap->txa_start;
3844eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3845eb6f0de0SAdrian Chadd 
3846eb6f0de0SAdrian Chadd 	if (nframes != nf)
3847eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
3848eb6f0de0SAdrian Chadd 		    "%s: num frames seen=%d; bf nframes=%d\n",
3849eb6f0de0SAdrian Chadd 		    __func__, nframes, nf);
3850eb6f0de0SAdrian Chadd 
3851eb6f0de0SAdrian Chadd 	/*
3852eb6f0de0SAdrian Chadd 	 * Now we know how many frames were bad, call the rate
3853eb6f0de0SAdrian Chadd 	 * control code.
3854eb6f0de0SAdrian Chadd 	 */
3855eb6f0de0SAdrian Chadd 	if (fail == 0)
3856d4365d16SAdrian Chadd 		ath_tx_update_ratectrl(sc, ni, rc, &ts, pktlen, nframes,
3857d4365d16SAdrian Chadd 		    nbad);
3858eb6f0de0SAdrian Chadd 
3859eb6f0de0SAdrian Chadd 	/*
3860eb6f0de0SAdrian Chadd 	 * send bar if we dropped any frames
3861eb6f0de0SAdrian Chadd 	 */
3862eb6f0de0SAdrian Chadd 	if (drops) {
386388b3d483SAdrian Chadd 		/* Suspend the TX queue and get ready to send the BAR */
386488b3d483SAdrian Chadd 		ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
386588b3d483SAdrian Chadd 		ath_tx_tid_bar_suspend(sc, atid);
386688b3d483SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3867eb6f0de0SAdrian Chadd 	}
3868eb6f0de0SAdrian Chadd 
386939da9d42SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
387039da9d42SAdrian Chadd 	    "%s: txa_start now %d\n", __func__, tap->txa_start);
387139da9d42SAdrian Chadd 
3872eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
387339da9d42SAdrian Chadd 
387439da9d42SAdrian Chadd 	/* Prepend all frames to the beginning of the queue */
3875eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_LAST(&bf_q, ath_bufhead_s)) != NULL) {
3876eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_q, bf, bf_list);
3877eb6f0de0SAdrian Chadd 		ATH_TXQ_INSERT_HEAD(atid, bf, bf_list);
3878eb6f0de0SAdrian Chadd 	}
3879eb6f0de0SAdrian Chadd 
388039da9d42SAdrian Chadd 	/*
388139da9d42SAdrian Chadd 	 * Reschedule to grab some further frames.
388239da9d42SAdrian Chadd 	 */
388339da9d42SAdrian Chadd 	ath_tx_tid_sched(sc, atid);
3884eb6f0de0SAdrian Chadd 
388588b3d483SAdrian Chadd 	/*
388688b3d483SAdrian Chadd 	 * Send BAR if required
388788b3d483SAdrian Chadd 	 */
388888b3d483SAdrian Chadd 	if (ath_tx_tid_bar_tx_ready(sc, atid))
388988b3d483SAdrian Chadd 		ath_tx_tid_bar_tx(sc, atid);
389039da9d42SAdrian Chadd 
389188b3d483SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
389288b3d483SAdrian Chadd 
3893eb6f0de0SAdrian Chadd 	/* Do deferred completion */
3894eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
3895eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
3896eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
3897eb6f0de0SAdrian Chadd 	}
3898eb6f0de0SAdrian Chadd }
3899eb6f0de0SAdrian Chadd 
3900eb6f0de0SAdrian Chadd /*
3901eb6f0de0SAdrian Chadd  * Handle completion of unaggregated frames in an ADDBA
3902eb6f0de0SAdrian Chadd  * session.
3903eb6f0de0SAdrian Chadd  *
3904eb6f0de0SAdrian Chadd  * Fail is set to 1 if the entry is being freed via a call to
3905eb6f0de0SAdrian Chadd  * ath_tx_draintxq().
3906eb6f0de0SAdrian Chadd  */
3907eb6f0de0SAdrian Chadd static void
3908eb6f0de0SAdrian Chadd ath_tx_aggr_comp_unaggr(struct ath_softc *sc, struct ath_buf *bf, int fail)
3909eb6f0de0SAdrian Chadd {
3910eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
3911eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
3912eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
3913eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
3914eb6f0de0SAdrian Chadd 	struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
3915eb6f0de0SAdrian Chadd 
3916eb6f0de0SAdrian Chadd 	/*
3917eb6f0de0SAdrian Chadd 	 * Update rate control status here, before we possibly
3918eb6f0de0SAdrian Chadd 	 * punt to retry or cleanup.
3919eb6f0de0SAdrian Chadd 	 *
3920eb6f0de0SAdrian Chadd 	 * Do it outside of the TXQ lock.
3921eb6f0de0SAdrian Chadd 	 */
3922875a9451SAdrian Chadd 	if (fail == 0 && ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0))
3923eb6f0de0SAdrian Chadd 		ath_tx_update_ratectrl(sc, ni, bf->bf_state.bfs_rc,
3924eb6f0de0SAdrian Chadd 		    &bf->bf_status.ds_txstat,
3925eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_pktlen,
3926eb6f0de0SAdrian Chadd 		    1, (ts->ts_status == 0) ? 0 : 1);
3927eb6f0de0SAdrian Chadd 
3928eb6f0de0SAdrian Chadd 	/*
3929eb6f0de0SAdrian Chadd 	 * This is called early so atid->hwq_depth can be tracked.
3930eb6f0de0SAdrian Chadd 	 * This unfortunately means that it's released and regrabbed
3931eb6f0de0SAdrian Chadd 	 * during retry and cleanup. That's rather inefficient.
3932eb6f0de0SAdrian Chadd 	 */
3933eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
3934eb6f0de0SAdrian Chadd 
3935eb6f0de0SAdrian Chadd 	if (tid == IEEE80211_NONQOS_TID)
3936eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: TID=16!\n", __func__);
3937eb6f0de0SAdrian Chadd 
3938d4365d16SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX,
3939d4365d16SAdrian Chadd 	    "%s: bf=%p: tid=%d, hwq_depth=%d, seqno=%d\n",
3940d4365d16SAdrian Chadd 	    __func__, bf, bf->bf_state.bfs_tid, atid->hwq_depth,
3941d4365d16SAdrian Chadd 	    SEQNO(bf->bf_state.bfs_seqno));
3942eb6f0de0SAdrian Chadd 
3943eb6f0de0SAdrian Chadd 	atid->hwq_depth--;
3944eb6f0de0SAdrian Chadd 	if (atid->hwq_depth < 0)
3945eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: hwq_depth < 0: %d\n",
3946eb6f0de0SAdrian Chadd 		    __func__, atid->hwq_depth);
3947eb6f0de0SAdrian Chadd 
3948eb6f0de0SAdrian Chadd 	/*
3949eb6f0de0SAdrian Chadd 	 * If a cleanup is in progress, punt to comp_cleanup;
3950eb6f0de0SAdrian Chadd 	 * rather than handling it here. It's thus their
3951eb6f0de0SAdrian Chadd 	 * responsibility to clean up, call the completion
3952eb6f0de0SAdrian Chadd 	 * function in net80211, etc.
3953eb6f0de0SAdrian Chadd 	 */
3954eb6f0de0SAdrian Chadd 	if (atid->cleanup_inprogress) {
3955eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3956d4365d16SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: cleanup_unaggr\n",
3957d4365d16SAdrian Chadd 		    __func__);
3958eb6f0de0SAdrian Chadd 		ath_tx_comp_cleanup_unaggr(sc, bf);
3959eb6f0de0SAdrian Chadd 		return;
3960eb6f0de0SAdrian Chadd 	}
3961eb6f0de0SAdrian Chadd 
3962eb6f0de0SAdrian Chadd 	/*
3963eb6f0de0SAdrian Chadd 	 * Don't bother with the retry check if all frames
3964eb6f0de0SAdrian Chadd 	 * are being failed (eg during queue deletion.)
3965eb6f0de0SAdrian Chadd 	 */
3966e9a6408eSAdrian Chadd #if 0
3967eb6f0de0SAdrian Chadd 	if (fail == 0 && ts->ts_status & HAL_TXERR_XRETRY) {
3968e9a6408eSAdrian Chadd #endif
3969e9a6408eSAdrian Chadd 	if (fail == 0 && ts->ts_status != 0) {
3970eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3971d4365d16SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: retry_unaggr\n",
3972d4365d16SAdrian Chadd 		    __func__);
3973eb6f0de0SAdrian Chadd 		ath_tx_aggr_retry_unaggr(sc, bf);
3974eb6f0de0SAdrian Chadd 		return;
3975eb6f0de0SAdrian Chadd 	}
3976eb6f0de0SAdrian Chadd 
3977eb6f0de0SAdrian Chadd 	/* Success? Complete */
3978eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: TID=%d, seqno %d\n",
3979eb6f0de0SAdrian Chadd 	    __func__, tid, SEQNO(bf->bf_state.bfs_seqno));
3980eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_dobaw) {
3981eb6f0de0SAdrian Chadd 		ath_tx_update_baw(sc, an, atid, bf);
3982eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_dobaw = 0;
3983eb6f0de0SAdrian Chadd 		if (! bf->bf_state.bfs_addedbaw)
3984eb6f0de0SAdrian Chadd 			device_printf(sc->sc_dev,
3985eb6f0de0SAdrian Chadd 			    "%s: wasn't added: seqno %d\n",
3986eb6f0de0SAdrian Chadd 			    __func__, SEQNO(bf->bf_state.bfs_seqno));
3987eb6f0de0SAdrian Chadd 	}
3988eb6f0de0SAdrian Chadd 
398988b3d483SAdrian Chadd 	/*
399088b3d483SAdrian Chadd 	 * Send BAR if required
399188b3d483SAdrian Chadd 	 */
399288b3d483SAdrian Chadd 	if (ath_tx_tid_bar_tx_ready(sc, atid))
399388b3d483SAdrian Chadd 		ath_tx_tid_bar_tx(sc, atid);
399488b3d483SAdrian Chadd 
3995eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3996eb6f0de0SAdrian Chadd 
3997eb6f0de0SAdrian Chadd 	ath_tx_default_comp(sc, bf, fail);
3998eb6f0de0SAdrian Chadd 	/* bf is freed at this point */
3999eb6f0de0SAdrian Chadd }
4000eb6f0de0SAdrian Chadd 
4001eb6f0de0SAdrian Chadd void
4002eb6f0de0SAdrian Chadd ath_tx_aggr_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
4003eb6f0de0SAdrian Chadd {
4004eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_aggr)
4005eb6f0de0SAdrian Chadd 		ath_tx_aggr_comp_aggr(sc, bf, fail);
4006eb6f0de0SAdrian Chadd 	else
4007eb6f0de0SAdrian Chadd 		ath_tx_aggr_comp_unaggr(sc, bf, fail);
4008eb6f0de0SAdrian Chadd }
4009eb6f0de0SAdrian Chadd 
4010eb6f0de0SAdrian Chadd /*
4011eb6f0de0SAdrian Chadd  * Schedule some packets from the given node/TID to the hardware.
4012eb6f0de0SAdrian Chadd  *
4013eb6f0de0SAdrian Chadd  * This is the aggregate version.
4014eb6f0de0SAdrian Chadd  */
4015eb6f0de0SAdrian Chadd void
4016eb6f0de0SAdrian Chadd ath_tx_tid_hw_queue_aggr(struct ath_softc *sc, struct ath_node *an,
4017eb6f0de0SAdrian Chadd     struct ath_tid *tid)
4018eb6f0de0SAdrian Chadd {
4019eb6f0de0SAdrian Chadd 	struct ath_buf *bf;
4020eb6f0de0SAdrian Chadd 	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
4021eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
4022eb6f0de0SAdrian Chadd 	ATH_AGGR_STATUS status;
4023eb6f0de0SAdrian Chadd 	ath_bufhead bf_q;
4024eb6f0de0SAdrian Chadd 
4025eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d\n", __func__, tid->tid);
4026eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
4027eb6f0de0SAdrian Chadd 
4028eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
4029eb6f0de0SAdrian Chadd 
4030eb6f0de0SAdrian Chadd 	if (tid->tid == IEEE80211_NONQOS_TID)
4031eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: called for TID=NONQOS_TID?\n",
4032eb6f0de0SAdrian Chadd 		    __func__);
4033eb6f0de0SAdrian Chadd 
4034eb6f0de0SAdrian Chadd 	for (;;) {
4035eb6f0de0SAdrian Chadd 		status = ATH_AGGR_DONE;
4036eb6f0de0SAdrian Chadd 
4037eb6f0de0SAdrian Chadd 		/*
4038eb6f0de0SAdrian Chadd 		 * If the upper layer has paused the TID, don't
4039eb6f0de0SAdrian Chadd 		 * queue any further packets.
4040eb6f0de0SAdrian Chadd 		 *
4041eb6f0de0SAdrian Chadd 		 * This can also occur from the completion task because
4042eb6f0de0SAdrian Chadd 		 * of packet loss; but as its serialised with this code,
4043eb6f0de0SAdrian Chadd 		 * it won't "appear" half way through queuing packets.
4044eb6f0de0SAdrian Chadd 		 */
4045eb6f0de0SAdrian Chadd 		if (tid->paused)
4046eb6f0de0SAdrian Chadd 			break;
4047eb6f0de0SAdrian Chadd 
4048eb6f0de0SAdrian Chadd 		bf = TAILQ_FIRST(&tid->axq_q);
4049eb6f0de0SAdrian Chadd 		if (bf == NULL) {
4050eb6f0de0SAdrian Chadd 			break;
4051eb6f0de0SAdrian Chadd 		}
4052eb6f0de0SAdrian Chadd 
4053eb6f0de0SAdrian Chadd 		/*
4054eb6f0de0SAdrian Chadd 		 * If the packet doesn't fall within the BAW (eg a NULL
4055eb6f0de0SAdrian Chadd 		 * data frame), schedule it directly; continue.
4056eb6f0de0SAdrian Chadd 		 */
4057eb6f0de0SAdrian Chadd 		if (! bf->bf_state.bfs_dobaw) {
4058d4365d16SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4059d4365d16SAdrian Chadd 			    "%s: non-baw packet\n",
4060eb6f0de0SAdrian Chadd 			    __func__);
4061eb6f0de0SAdrian Chadd 			ATH_TXQ_REMOVE(tid, bf, bf_list);
40622a9f83afSAdrian Chadd 
40632a9f83afSAdrian Chadd 			if (bf->bf_state.bfs_nframes > 1)
40642a9f83afSAdrian Chadd 				device_printf(sc->sc_dev,
40652a9f83afSAdrian Chadd 				    "%s: aggr=%d, nframes=%d\n",
40662a9f83afSAdrian Chadd 				    __func__,
40672a9f83afSAdrian Chadd 				    bf->bf_state.bfs_aggr,
40682a9f83afSAdrian Chadd 				    bf->bf_state.bfs_nframes);
40692a9f83afSAdrian Chadd 
40702a9f83afSAdrian Chadd 			/*
40712a9f83afSAdrian Chadd 			 * This shouldn't happen - such frames shouldn't
40722a9f83afSAdrian Chadd 			 * ever have been queued as an aggregate in the
40732a9f83afSAdrian Chadd 			 * first place.  However, make sure the fields
40742a9f83afSAdrian Chadd 			 * are correctly setup just to be totally sure.
40752a9f83afSAdrian Chadd 			 */
4076eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_aggr = 0;
40772a9f83afSAdrian Chadd 			bf->bf_state.bfs_nframes = 1;
40782a9f83afSAdrian Chadd 
4079eb6f0de0SAdrian Chadd 			ath_tx_do_ratelookup(sc, bf);
4080e2e4a2c2SAdrian Chadd 			ath_tx_calc_duration(sc, bf);
4081e2e4a2c2SAdrian Chadd 			ath_tx_calc_protection(sc, bf);
4082eb6f0de0SAdrian Chadd 			ath_tx_set_rtscts(sc, bf);
4083e2e4a2c2SAdrian Chadd 			ath_tx_rate_fill_rcflags(sc, bf);
4084eb6f0de0SAdrian Chadd 			ath_tx_setds(sc, bf);
4085eb6f0de0SAdrian Chadd 			ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
4086eb6f0de0SAdrian Chadd 
4087eb6f0de0SAdrian Chadd 			sc->sc_aggr_stats.aggr_nonbaw_pkt++;
4088eb6f0de0SAdrian Chadd 
4089eb6f0de0SAdrian Chadd 			/* Queue the packet; continue */
4090eb6f0de0SAdrian Chadd 			goto queuepkt;
4091eb6f0de0SAdrian Chadd 		}
4092eb6f0de0SAdrian Chadd 
4093eb6f0de0SAdrian Chadd 		TAILQ_INIT(&bf_q);
4094eb6f0de0SAdrian Chadd 
4095eb6f0de0SAdrian Chadd 		/*
4096eb6f0de0SAdrian Chadd 		 * Do a rate control lookup on the first frame in the
4097eb6f0de0SAdrian Chadd 		 * list. The rate control code needs that to occur
4098eb6f0de0SAdrian Chadd 		 * before it can determine whether to TX.
4099eb6f0de0SAdrian Chadd 		 * It's inaccurate because the rate control code doesn't
4100eb6f0de0SAdrian Chadd 		 * really "do" aggregate lookups, so it only considers
4101eb6f0de0SAdrian Chadd 		 * the size of the first frame.
4102eb6f0de0SAdrian Chadd 		 */
4103eb6f0de0SAdrian Chadd 		ath_tx_do_ratelookup(sc, bf);
4104eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[3].rix = 0;
4105eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[3].tries = 0;
4106e2e4a2c2SAdrian Chadd 
4107e2e4a2c2SAdrian Chadd 		ath_tx_calc_duration(sc, bf);
4108e2e4a2c2SAdrian Chadd 		ath_tx_calc_protection(sc, bf);
4109e2e4a2c2SAdrian Chadd 
4110e2e4a2c2SAdrian Chadd 		ath_tx_set_rtscts(sc, bf);
4111eb6f0de0SAdrian Chadd 		ath_tx_rate_fill_rcflags(sc, bf);
4112eb6f0de0SAdrian Chadd 
4113eb6f0de0SAdrian Chadd 		status = ath_tx_form_aggr(sc, an, tid, &bf_q);
4114eb6f0de0SAdrian Chadd 
4115eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4116eb6f0de0SAdrian Chadd 		    "%s: ath_tx_form_aggr() status=%d\n", __func__, status);
4117eb6f0de0SAdrian Chadd 
4118eb6f0de0SAdrian Chadd 		/*
4119eb6f0de0SAdrian Chadd 		 * No frames to be picked up - out of BAW
4120eb6f0de0SAdrian Chadd 		 */
4121eb6f0de0SAdrian Chadd 		if (TAILQ_EMPTY(&bf_q))
4122eb6f0de0SAdrian Chadd 			break;
4123eb6f0de0SAdrian Chadd 
4124eb6f0de0SAdrian Chadd 		/*
4125eb6f0de0SAdrian Chadd 		 * This assumes that the descriptor list in the ath_bufhead
4126eb6f0de0SAdrian Chadd 		 * are already linked together via bf_next pointers.
4127eb6f0de0SAdrian Chadd 		 */
4128eb6f0de0SAdrian Chadd 		bf = TAILQ_FIRST(&bf_q);
4129eb6f0de0SAdrian Chadd 
4130e2e4a2c2SAdrian Chadd 		if (status == ATH_AGGR_8K_LIMITED)
4131e2e4a2c2SAdrian Chadd 			sc->sc_aggr_stats.aggr_rts_aggr_limited++;
4132e2e4a2c2SAdrian Chadd 
4133eb6f0de0SAdrian Chadd 		/*
4134eb6f0de0SAdrian Chadd 		 * If it's the only frame send as non-aggregate
4135eb6f0de0SAdrian Chadd 		 * assume that ath_tx_form_aggr() has checked
4136eb6f0de0SAdrian Chadd 		 * whether it's in the BAW and added it appropriately.
4137eb6f0de0SAdrian Chadd 		 */
4138eb6f0de0SAdrian Chadd 		if (bf->bf_state.bfs_nframes == 1) {
4139eb6f0de0SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4140eb6f0de0SAdrian Chadd 			    "%s: single-frame aggregate\n", __func__);
4141eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_aggr = 0;
414221840808SAdrian Chadd 			bf->bf_state.bfs_ndelim = 0;
4143eb6f0de0SAdrian Chadd 			ath_tx_setds(sc, bf);
4144eb6f0de0SAdrian Chadd 			ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
4145eb6f0de0SAdrian Chadd 			if (status == ATH_AGGR_BAW_CLOSED)
4146eb6f0de0SAdrian Chadd 				sc->sc_aggr_stats.aggr_baw_closed_single_pkt++;
4147eb6f0de0SAdrian Chadd 			else
4148eb6f0de0SAdrian Chadd 				sc->sc_aggr_stats.aggr_single_pkt++;
4149eb6f0de0SAdrian Chadd 		} else {
4150eb6f0de0SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4151d4365d16SAdrian Chadd 			    "%s: multi-frame aggregate: %d frames, "
4152d4365d16SAdrian Chadd 			    "length %d\n",
4153eb6f0de0SAdrian Chadd 			     __func__, bf->bf_state.bfs_nframes,
4154eb6f0de0SAdrian Chadd 			    bf->bf_state.bfs_al);
4155eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_aggr = 1;
4156eb6f0de0SAdrian Chadd 			sc->sc_aggr_stats.aggr_pkts[bf->bf_state.bfs_nframes]++;
4157eb6f0de0SAdrian Chadd 			sc->sc_aggr_stats.aggr_aggr_pkt++;
4158eb6f0de0SAdrian Chadd 
4159eb6f0de0SAdrian Chadd 			/*
4160e2e4a2c2SAdrian Chadd 			 * Calculate the duration/protection as required.
4161e2e4a2c2SAdrian Chadd 			 */
4162e2e4a2c2SAdrian Chadd 			ath_tx_calc_duration(sc, bf);
4163e2e4a2c2SAdrian Chadd 			ath_tx_calc_protection(sc, bf);
4164e2e4a2c2SAdrian Chadd 
4165e2e4a2c2SAdrian Chadd 			/*
4166eb6f0de0SAdrian Chadd 			 * Update the rate and rtscts information based on the
4167eb6f0de0SAdrian Chadd 			 * rate decision made by the rate control code;
4168eb6f0de0SAdrian Chadd 			 * the first frame in the aggregate needs it.
4169eb6f0de0SAdrian Chadd 			 */
4170eb6f0de0SAdrian Chadd 			ath_tx_set_rtscts(sc, bf);
4171eb6f0de0SAdrian Chadd 
4172eb6f0de0SAdrian Chadd 			/*
4173eb6f0de0SAdrian Chadd 			 * Setup the relevant descriptor fields
4174eb6f0de0SAdrian Chadd 			 * for aggregation. The first descriptor
4175eb6f0de0SAdrian Chadd 			 * already points to the rest in the chain.
4176eb6f0de0SAdrian Chadd 			 */
4177eb6f0de0SAdrian Chadd 			ath_tx_setds_11n(sc, bf);
4178eb6f0de0SAdrian Chadd 
4179eb6f0de0SAdrian Chadd 		}
4180eb6f0de0SAdrian Chadd 	queuepkt:
4181eb6f0de0SAdrian Chadd 		//txq = bf->bf_state.bfs_txq;
4182eb6f0de0SAdrian Chadd 
4183eb6f0de0SAdrian Chadd 		/* Set completion handler, multi-frame aggregate or not */
4184eb6f0de0SAdrian Chadd 		bf->bf_comp = ath_tx_aggr_comp;
4185eb6f0de0SAdrian Chadd 
4186eb6f0de0SAdrian Chadd 		if (bf->bf_state.bfs_tid == IEEE80211_NONQOS_TID)
4187eb6f0de0SAdrian Chadd 		    device_printf(sc->sc_dev, "%s: TID=16?\n", __func__);
4188eb6f0de0SAdrian Chadd 
4189eb6f0de0SAdrian Chadd 		/* Punt to txq */
4190eb6f0de0SAdrian Chadd 		ath_tx_handoff(sc, txq, bf);
4191eb6f0de0SAdrian Chadd 
4192eb6f0de0SAdrian Chadd 		/* Track outstanding buffer count to hardware */
4193eb6f0de0SAdrian Chadd 		/* aggregates are "one" buffer */
4194eb6f0de0SAdrian Chadd 		tid->hwq_depth++;
4195eb6f0de0SAdrian Chadd 
4196eb6f0de0SAdrian Chadd 		/*
4197eb6f0de0SAdrian Chadd 		 * Break out if ath_tx_form_aggr() indicated
4198eb6f0de0SAdrian Chadd 		 * there can't be any further progress (eg BAW is full.)
4199eb6f0de0SAdrian Chadd 		 * Checking for an empty txq is done above.
4200eb6f0de0SAdrian Chadd 		 *
4201eb6f0de0SAdrian Chadd 		 * XXX locking on txq here?
4202eb6f0de0SAdrian Chadd 		 */
4203eb6f0de0SAdrian Chadd 		if (txq->axq_aggr_depth >= sc->sc_hwq_limit ||
4204eb6f0de0SAdrian Chadd 		    status == ATH_AGGR_BAW_CLOSED)
4205eb6f0de0SAdrian Chadd 			break;
4206eb6f0de0SAdrian Chadd 	}
4207eb6f0de0SAdrian Chadd }
4208eb6f0de0SAdrian Chadd 
4209eb6f0de0SAdrian Chadd /*
4210eb6f0de0SAdrian Chadd  * Schedule some packets from the given node/TID to the hardware.
4211eb6f0de0SAdrian Chadd  */
4212eb6f0de0SAdrian Chadd void
4213eb6f0de0SAdrian Chadd ath_tx_tid_hw_queue_norm(struct ath_softc *sc, struct ath_node *an,
4214eb6f0de0SAdrian Chadd     struct ath_tid *tid)
4215eb6f0de0SAdrian Chadd {
4216eb6f0de0SAdrian Chadd 	struct ath_buf *bf;
4217eb6f0de0SAdrian Chadd 	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
4218eb6f0de0SAdrian Chadd 
4219eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: node %p: TID %d: called\n",
4220eb6f0de0SAdrian Chadd 	    __func__, an, tid->tid);
4221eb6f0de0SAdrian Chadd 
4222eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
4223eb6f0de0SAdrian Chadd 
4224eb6f0de0SAdrian Chadd 	/* Check - is AMPDU pending or running? then print out something */
4225eb6f0de0SAdrian Chadd 	if (ath_tx_ampdu_pending(sc, an, tid->tid))
4226eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: tid=%d, ampdu pending?\n",
4227eb6f0de0SAdrian Chadd 		    __func__, tid->tid);
4228eb6f0de0SAdrian Chadd 	if (ath_tx_ampdu_running(sc, an, tid->tid))
4229eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: tid=%d, ampdu running?\n",
4230eb6f0de0SAdrian Chadd 		    __func__, tid->tid);
4231eb6f0de0SAdrian Chadd 
4232eb6f0de0SAdrian Chadd 	for (;;) {
4233eb6f0de0SAdrian Chadd 
4234eb6f0de0SAdrian Chadd 		/*
4235eb6f0de0SAdrian Chadd 		 * If the upper layers have paused the TID, don't
4236eb6f0de0SAdrian Chadd 		 * queue any further packets.
4237eb6f0de0SAdrian Chadd 		 */
4238eb6f0de0SAdrian Chadd 		if (tid->paused)
4239eb6f0de0SAdrian Chadd 			break;
4240eb6f0de0SAdrian Chadd 
4241eb6f0de0SAdrian Chadd 		bf = TAILQ_FIRST(&tid->axq_q);
4242eb6f0de0SAdrian Chadd 		if (bf == NULL) {
4243eb6f0de0SAdrian Chadd 			break;
4244eb6f0de0SAdrian Chadd 		}
4245eb6f0de0SAdrian Chadd 
4246eb6f0de0SAdrian Chadd 		ATH_TXQ_REMOVE(tid, bf, bf_list);
4247eb6f0de0SAdrian Chadd 
4248eb6f0de0SAdrian Chadd 		KASSERT(txq == bf->bf_state.bfs_txq, ("txqs not equal!\n"));
4249eb6f0de0SAdrian Chadd 
4250eb6f0de0SAdrian Chadd 		/* Sanity check! */
4251eb6f0de0SAdrian Chadd 		if (tid->tid != bf->bf_state.bfs_tid) {
4252eb6f0de0SAdrian Chadd 			device_printf(sc->sc_dev, "%s: bfs_tid %d !="
4253eb6f0de0SAdrian Chadd 			    " tid %d\n",
4254eb6f0de0SAdrian Chadd 			    __func__, bf->bf_state.bfs_tid, tid->tid);
4255eb6f0de0SAdrian Chadd 		}
4256eb6f0de0SAdrian Chadd 		/* Normal completion handler */
4257eb6f0de0SAdrian Chadd 		bf->bf_comp = ath_tx_normal_comp;
4258eb6f0de0SAdrian Chadd 
4259eb6f0de0SAdrian Chadd 		/* Program descriptors + rate control */
4260eb6f0de0SAdrian Chadd 		ath_tx_do_ratelookup(sc, bf);
4261e2e4a2c2SAdrian Chadd 		ath_tx_calc_duration(sc, bf);
4262e2e4a2c2SAdrian Chadd 		ath_tx_calc_protection(sc, bf);
4263eb6f0de0SAdrian Chadd 		ath_tx_set_rtscts(sc, bf);
4264e2e4a2c2SAdrian Chadd 		ath_tx_rate_fill_rcflags(sc, bf);
4265eb6f0de0SAdrian Chadd 		ath_tx_setds(sc, bf);
4266eb6f0de0SAdrian Chadd 
4267eb6f0de0SAdrian Chadd 		/* Track outstanding buffer count to hardware */
4268eb6f0de0SAdrian Chadd 		/* aggregates are "one" buffer */
4269eb6f0de0SAdrian Chadd 		tid->hwq_depth++;
4270eb6f0de0SAdrian Chadd 
4271eb6f0de0SAdrian Chadd 		/* Punt to hardware or software txq */
4272eb6f0de0SAdrian Chadd 		ath_tx_handoff(sc, txq, bf);
4273eb6f0de0SAdrian Chadd 	}
4274eb6f0de0SAdrian Chadd }
4275eb6f0de0SAdrian Chadd 
4276eb6f0de0SAdrian Chadd /*
4277eb6f0de0SAdrian Chadd  * Schedule some packets to the given hardware queue.
4278eb6f0de0SAdrian Chadd  *
4279eb6f0de0SAdrian Chadd  * This function walks the list of TIDs (ie, ath_node TIDs
4280eb6f0de0SAdrian Chadd  * with queued traffic) and attempts to schedule traffic
4281eb6f0de0SAdrian Chadd  * from them.
4282eb6f0de0SAdrian Chadd  *
4283eb6f0de0SAdrian Chadd  * TID scheduling is implemented as a FIFO, with TIDs being
4284eb6f0de0SAdrian Chadd  * added to the end of the queue after some frames have been
4285eb6f0de0SAdrian Chadd  * scheduled.
4286eb6f0de0SAdrian Chadd  */
4287eb6f0de0SAdrian Chadd void
4288eb6f0de0SAdrian Chadd ath_txq_sched(struct ath_softc *sc, struct ath_txq *txq)
4289eb6f0de0SAdrian Chadd {
4290eb6f0de0SAdrian Chadd 	struct ath_tid *tid, *next, *last;
4291eb6f0de0SAdrian Chadd 
4292eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
4293eb6f0de0SAdrian Chadd 
4294eb6f0de0SAdrian Chadd 	/*
4295eb6f0de0SAdrian Chadd 	 * Don't schedule if the hardware queue is busy.
4296eb6f0de0SAdrian Chadd 	 * This (hopefully) gives some more time to aggregate
4297eb6f0de0SAdrian Chadd 	 * some packets in the aggregation queue.
4298eb6f0de0SAdrian Chadd 	 */
4299eb6f0de0SAdrian Chadd 	if (txq->axq_aggr_depth >= sc->sc_hwq_limit) {
4300eb6f0de0SAdrian Chadd 		sc->sc_aggr_stats.aggr_sched_nopkt++;
4301eb6f0de0SAdrian Chadd 		return;
4302eb6f0de0SAdrian Chadd 	}
4303eb6f0de0SAdrian Chadd 
4304eb6f0de0SAdrian Chadd 	last = TAILQ_LAST(&txq->axq_tidq, axq_t_s);
4305eb6f0de0SAdrian Chadd 
4306eb6f0de0SAdrian Chadd 	TAILQ_FOREACH_SAFE(tid, &txq->axq_tidq, axq_qelem, next) {
4307eb6f0de0SAdrian Chadd 		/*
4308eb6f0de0SAdrian Chadd 		 * Suspend paused queues here; they'll be resumed
4309eb6f0de0SAdrian Chadd 		 * once the addba completes or times out.
4310eb6f0de0SAdrian Chadd 		 */
4311eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d, paused=%d\n",
4312eb6f0de0SAdrian Chadd 		    __func__, tid->tid, tid->paused);
4313eb6f0de0SAdrian Chadd 		ath_tx_tid_unsched(sc, tid);
4314eb6f0de0SAdrian Chadd 		if (tid->paused) {
4315eb6f0de0SAdrian Chadd 			continue;
4316eb6f0de0SAdrian Chadd 		}
4317eb6f0de0SAdrian Chadd 		if (ath_tx_ampdu_running(sc, tid->an, tid->tid))
4318eb6f0de0SAdrian Chadd 			ath_tx_tid_hw_queue_aggr(sc, tid->an, tid);
4319eb6f0de0SAdrian Chadd 		else
4320eb6f0de0SAdrian Chadd 			ath_tx_tid_hw_queue_norm(sc, tid->an, tid);
4321eb6f0de0SAdrian Chadd 
4322eb6f0de0SAdrian Chadd 		/* Not empty? Re-schedule */
4323eb6f0de0SAdrian Chadd 		if (tid->axq_depth != 0)
4324eb6f0de0SAdrian Chadd 			ath_tx_tid_sched(sc, tid);
4325eb6f0de0SAdrian Chadd 
4326eb6f0de0SAdrian Chadd 		/* Give the software queue time to aggregate more packets */
4327eb6f0de0SAdrian Chadd 		if (txq->axq_aggr_depth >= sc->sc_hwq_limit) {
4328eb6f0de0SAdrian Chadd 			break;
4329eb6f0de0SAdrian Chadd 		}
4330eb6f0de0SAdrian Chadd 
4331eb6f0de0SAdrian Chadd 		/*
4332eb6f0de0SAdrian Chadd 		 * If this was the last entry on the original list, stop.
4333eb6f0de0SAdrian Chadd 		 * Otherwise nodes that have been rescheduled onto the end
4334eb6f0de0SAdrian Chadd 		 * of the TID FIFO list will just keep being rescheduled.
4335eb6f0de0SAdrian Chadd 		 */
4336eb6f0de0SAdrian Chadd 		if (tid == last)
4337eb6f0de0SAdrian Chadd 			break;
4338eb6f0de0SAdrian Chadd 	}
4339eb6f0de0SAdrian Chadd }
4340eb6f0de0SAdrian Chadd 
4341eb6f0de0SAdrian Chadd /*
4342eb6f0de0SAdrian Chadd  * TX addba handling
4343eb6f0de0SAdrian Chadd  */
4344eb6f0de0SAdrian Chadd 
4345eb6f0de0SAdrian Chadd /*
4346eb6f0de0SAdrian Chadd  * Return net80211 TID struct pointer, or NULL for none
4347eb6f0de0SAdrian Chadd  */
4348eb6f0de0SAdrian Chadd struct ieee80211_tx_ampdu *
4349eb6f0de0SAdrian Chadd ath_tx_get_tx_tid(struct ath_node *an, int tid)
4350eb6f0de0SAdrian Chadd {
4351eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = &an->an_node;
4352eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
4353eb6f0de0SAdrian Chadd 
4354eb6f0de0SAdrian Chadd 	if (tid == IEEE80211_NONQOS_TID)
4355eb6f0de0SAdrian Chadd 		return NULL;
4356eb6f0de0SAdrian Chadd 
43572aa563dfSAdrian Chadd 	tap = &ni->ni_tx_ampdu[tid];
4358eb6f0de0SAdrian Chadd 	return tap;
4359eb6f0de0SAdrian Chadd }
4360eb6f0de0SAdrian Chadd 
4361eb6f0de0SAdrian Chadd /*
4362eb6f0de0SAdrian Chadd  * Is AMPDU-TX running?
4363eb6f0de0SAdrian Chadd  */
4364eb6f0de0SAdrian Chadd static int
4365eb6f0de0SAdrian Chadd ath_tx_ampdu_running(struct ath_softc *sc, struct ath_node *an, int tid)
4366eb6f0de0SAdrian Chadd {
4367eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
4368eb6f0de0SAdrian Chadd 
4369eb6f0de0SAdrian Chadd 	if (tid == IEEE80211_NONQOS_TID)
4370eb6f0de0SAdrian Chadd 		return 0;
4371eb6f0de0SAdrian Chadd 
4372eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
4373eb6f0de0SAdrian Chadd 	if (tap == NULL)
4374eb6f0de0SAdrian Chadd 		return 0;	/* Not valid; default to not running */
4375eb6f0de0SAdrian Chadd 
4376eb6f0de0SAdrian Chadd 	return !! (tap->txa_flags & IEEE80211_AGGR_RUNNING);
4377eb6f0de0SAdrian Chadd }
4378eb6f0de0SAdrian Chadd 
4379eb6f0de0SAdrian Chadd /*
4380eb6f0de0SAdrian Chadd  * Is AMPDU-TX negotiation pending?
4381eb6f0de0SAdrian Chadd  */
4382eb6f0de0SAdrian Chadd static int
4383eb6f0de0SAdrian Chadd ath_tx_ampdu_pending(struct ath_softc *sc, struct ath_node *an, int tid)
4384eb6f0de0SAdrian Chadd {
4385eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
4386eb6f0de0SAdrian Chadd 
4387eb6f0de0SAdrian Chadd 	if (tid == IEEE80211_NONQOS_TID)
4388eb6f0de0SAdrian Chadd 		return 0;
4389eb6f0de0SAdrian Chadd 
4390eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
4391eb6f0de0SAdrian Chadd 	if (tap == NULL)
4392eb6f0de0SAdrian Chadd 		return 0;	/* Not valid; default to not pending */
4393eb6f0de0SAdrian Chadd 
4394eb6f0de0SAdrian Chadd 	return !! (tap->txa_flags & IEEE80211_AGGR_XCHGPEND);
4395eb6f0de0SAdrian Chadd }
4396eb6f0de0SAdrian Chadd 
4397eb6f0de0SAdrian Chadd /*
4398eb6f0de0SAdrian Chadd  * Is AMPDU-TX pending for the given TID?
4399eb6f0de0SAdrian Chadd  */
4400eb6f0de0SAdrian Chadd 
4401eb6f0de0SAdrian Chadd 
4402eb6f0de0SAdrian Chadd /*
4403eb6f0de0SAdrian Chadd  * Method to handle sending an ADDBA request.
4404eb6f0de0SAdrian Chadd  *
4405eb6f0de0SAdrian Chadd  * We tap this so the relevant flags can be set to pause the TID
4406eb6f0de0SAdrian Chadd  * whilst waiting for the response.
4407eb6f0de0SAdrian Chadd  *
4408eb6f0de0SAdrian Chadd  * XXX there's no timeout handler we can override?
4409eb6f0de0SAdrian Chadd  */
4410eb6f0de0SAdrian Chadd int
4411eb6f0de0SAdrian Chadd ath_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
4412eb6f0de0SAdrian Chadd     int dialogtoken, int baparamset, int batimeout)
4413eb6f0de0SAdrian Chadd {
4414eb6f0de0SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
44152aa563dfSAdrian Chadd 	int tid = tap->txa_tid;
4416eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4417eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4418eb6f0de0SAdrian Chadd 
4419eb6f0de0SAdrian Chadd 	/*
4420eb6f0de0SAdrian Chadd 	 * XXX danger Will Robinson!
4421eb6f0de0SAdrian Chadd 	 *
4422eb6f0de0SAdrian Chadd 	 * Although the taskqueue may be running and scheduling some more
4423eb6f0de0SAdrian Chadd 	 * packets, these should all be _before_ the addba sequence number.
4424eb6f0de0SAdrian Chadd 	 * However, net80211 will keep self-assigning sequence numbers
4425eb6f0de0SAdrian Chadd 	 * until addba has been negotiated.
4426eb6f0de0SAdrian Chadd 	 *
4427eb6f0de0SAdrian Chadd 	 * In the past, these packets would be "paused" (which still works
4428eb6f0de0SAdrian Chadd 	 * fine, as they're being scheduled to the driver in the same
4429eb6f0de0SAdrian Chadd 	 * serialised method which is calling the addba request routine)
4430eb6f0de0SAdrian Chadd 	 * and when the aggregation session begins, they'll be dequeued
4431eb6f0de0SAdrian Chadd 	 * as aggregate packets and added to the BAW. However, now there's
4432eb6f0de0SAdrian Chadd 	 * a "bf->bf_state.bfs_dobaw" flag, and this isn't set for these
4433eb6f0de0SAdrian Chadd 	 * packets. Thus they never get included in the BAW tracking and
4434eb6f0de0SAdrian Chadd 	 * this can cause the initial burst of packets after the addba
4435eb6f0de0SAdrian Chadd 	 * negotiation to "hang", as they quickly fall outside the BAW.
4436eb6f0de0SAdrian Chadd 	 *
4437eb6f0de0SAdrian Chadd 	 * The "eventual" solution should be to tag these packets with
4438eb6f0de0SAdrian Chadd 	 * dobaw. Although net80211 has given us a sequence number,
4439eb6f0de0SAdrian Chadd 	 * it'll be "after" the left edge of the BAW and thus it'll
4440eb6f0de0SAdrian Chadd 	 * fall within it.
4441eb6f0de0SAdrian Chadd 	 */
444296ff26ffSAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
4443d3a6425bSAdrian Chadd 	/*
4444d3a6425bSAdrian Chadd 	 * This is a bit annoying.  Until net80211 HT code inherits some
4445d3a6425bSAdrian Chadd 	 * (any) locking, we may have this called in parallel BUT only
4446d3a6425bSAdrian Chadd 	 * one response/timeout will be called.  Grr.
4447d3a6425bSAdrian Chadd 	 */
4448d3a6425bSAdrian Chadd 	if (atid->addba_tx_pending == 0) {
4449eb6f0de0SAdrian Chadd 		ath_tx_tid_pause(sc, atid);
4450d3a6425bSAdrian Chadd 		atid->addba_tx_pending = 1;
4451d3a6425bSAdrian Chadd 	}
445296ff26ffSAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4453eb6f0de0SAdrian Chadd 
4454eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4455eb6f0de0SAdrian Chadd 	    "%s: called; dialogtoken=%d, baparamset=%d, batimeout=%d\n",
4456eb6f0de0SAdrian Chadd 	    __func__, dialogtoken, baparamset, batimeout);
4457eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4458eb6f0de0SAdrian Chadd 	    "%s: txa_start=%d, ni_txseqs=%d\n",
4459eb6f0de0SAdrian Chadd 	    __func__, tap->txa_start, ni->ni_txseqs[tid]);
4460eb6f0de0SAdrian Chadd 
4461eb6f0de0SAdrian Chadd 	return sc->sc_addba_request(ni, tap, dialogtoken, baparamset,
4462eb6f0de0SAdrian Chadd 	    batimeout);
4463eb6f0de0SAdrian Chadd }
4464eb6f0de0SAdrian Chadd 
4465eb6f0de0SAdrian Chadd /*
4466eb6f0de0SAdrian Chadd  * Handle an ADDBA response.
4467eb6f0de0SAdrian Chadd  *
4468eb6f0de0SAdrian Chadd  * We unpause the queue so TX'ing can resume.
4469eb6f0de0SAdrian Chadd  *
4470eb6f0de0SAdrian Chadd  * Any packets TX'ed from this point should be "aggregate" (whether
4471eb6f0de0SAdrian Chadd  * aggregate or not) so the BAW is updated.
4472eb6f0de0SAdrian Chadd  *
4473eb6f0de0SAdrian Chadd  * Note! net80211 keeps self-assigning sequence numbers until
4474eb6f0de0SAdrian Chadd  * ampdu is negotiated. This means the initially-negotiated BAW left
4475eb6f0de0SAdrian Chadd  * edge won't match the ni->ni_txseq.
4476eb6f0de0SAdrian Chadd  *
4477eb6f0de0SAdrian Chadd  * So, being very dirty, the BAW left edge is "slid" here to match
4478eb6f0de0SAdrian Chadd  * ni->ni_txseq.
4479eb6f0de0SAdrian Chadd  *
4480eb6f0de0SAdrian Chadd  * What likely SHOULD happen is that all packets subsequent to the
4481eb6f0de0SAdrian Chadd  * addba request should be tagged as aggregate and queued as non-aggregate
4482eb6f0de0SAdrian Chadd  * frames; thus updating the BAW. For now though, I'll just slide the
4483eb6f0de0SAdrian Chadd  * window.
4484eb6f0de0SAdrian Chadd  */
4485eb6f0de0SAdrian Chadd int
4486eb6f0de0SAdrian Chadd ath_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
4487eb6f0de0SAdrian Chadd     int status, int code, int batimeout)
4488eb6f0de0SAdrian Chadd {
4489eb6f0de0SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
44902aa563dfSAdrian Chadd 	int tid = tap->txa_tid;
4491eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4492eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4493eb6f0de0SAdrian Chadd 	int r;
4494eb6f0de0SAdrian Chadd 
4495eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4496eb6f0de0SAdrian Chadd 	    "%s: called; status=%d, code=%d, batimeout=%d\n", __func__,
4497eb6f0de0SAdrian Chadd 	    status, code, batimeout);
4498eb6f0de0SAdrian Chadd 
4499eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4500eb6f0de0SAdrian Chadd 	    "%s: txa_start=%d, ni_txseqs=%d\n",
4501eb6f0de0SAdrian Chadd 	    __func__, tap->txa_start, ni->ni_txseqs[tid]);
4502eb6f0de0SAdrian Chadd 
4503eb6f0de0SAdrian Chadd 	/*
4504eb6f0de0SAdrian Chadd 	 * Call this first, so the interface flags get updated
4505eb6f0de0SAdrian Chadd 	 * before the TID is unpaused. Otherwise a race condition
4506eb6f0de0SAdrian Chadd 	 * exists where the unpaused TID still doesn't yet have
4507eb6f0de0SAdrian Chadd 	 * IEEE80211_AGGR_RUNNING set.
4508eb6f0de0SAdrian Chadd 	 */
4509eb6f0de0SAdrian Chadd 	r = sc->sc_addba_response(ni, tap, status, code, batimeout);
4510eb6f0de0SAdrian Chadd 
4511eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
4512d3a6425bSAdrian Chadd 	atid->addba_tx_pending = 0;
4513eb6f0de0SAdrian Chadd 	/*
4514eb6f0de0SAdrian Chadd 	 * XXX dirty!
4515eb6f0de0SAdrian Chadd 	 * Slide the BAW left edge to wherever net80211 left it for us.
4516eb6f0de0SAdrian Chadd 	 * Read above for more information.
4517eb6f0de0SAdrian Chadd 	 */
4518eb6f0de0SAdrian Chadd 	tap->txa_start = ni->ni_txseqs[tid];
4519eb6f0de0SAdrian Chadd 	ath_tx_tid_resume(sc, atid);
4520eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4521eb6f0de0SAdrian Chadd 	return r;
4522eb6f0de0SAdrian Chadd }
4523eb6f0de0SAdrian Chadd 
4524eb6f0de0SAdrian Chadd 
4525eb6f0de0SAdrian Chadd /*
4526eb6f0de0SAdrian Chadd  * Stop ADDBA on a queue.
45278405fe86SAdrian Chadd  *
45288405fe86SAdrian Chadd  * This can be called whilst BAR TX is currently active on the queue,
45298405fe86SAdrian Chadd  * so make sure this is unblocked before continuing.
4530eb6f0de0SAdrian Chadd  */
4531eb6f0de0SAdrian Chadd void
4532eb6f0de0SAdrian Chadd ath_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
4533eb6f0de0SAdrian Chadd {
4534eb6f0de0SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
45352aa563dfSAdrian Chadd 	int tid = tap->txa_tid;
4536eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4537eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4538eb6f0de0SAdrian Chadd 
4539eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: called\n", __func__);
4540eb6f0de0SAdrian Chadd 
45418405fe86SAdrian Chadd 	/*
45428405fe86SAdrian Chadd 	 * Pause TID traffic early, so there aren't any races
45438405fe86SAdrian Chadd 	 * Unblock the pending BAR held traffic, if it's currently paused.
45448405fe86SAdrian Chadd 	 */
454596ff26ffSAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
4546eb6f0de0SAdrian Chadd 	ath_tx_tid_pause(sc, atid);
45478405fe86SAdrian Chadd 	if (atid->bar_wait) {
45488405fe86SAdrian Chadd 		/*
45498405fe86SAdrian Chadd 		 * bar_unsuspend() expects bar_tx == 1, as it should be
45508405fe86SAdrian Chadd 		 * called from the TX completion path.  This quietens
45518405fe86SAdrian Chadd 		 * the warning.  It's cleared for us anyway.
45528405fe86SAdrian Chadd 		 */
45538405fe86SAdrian Chadd 		atid->bar_tx = 1;
45548405fe86SAdrian Chadd 		ath_tx_tid_bar_unsuspend(sc, atid);
45558405fe86SAdrian Chadd 	}
455696ff26ffSAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4557eb6f0de0SAdrian Chadd 
4558eb6f0de0SAdrian Chadd 	/* There's no need to hold the TXQ lock here */
4559eb6f0de0SAdrian Chadd 	sc->sc_addba_stop(ni, tap);
4560eb6f0de0SAdrian Chadd 
4561eb6f0de0SAdrian Chadd 	/*
45624dfd4507SAdrian Chadd 	 * ath_tx_tid_cleanup will resume the TID if possible, otherwise
4563eb6f0de0SAdrian Chadd 	 * it'll set the cleanup flag, and it'll be unpaused once
4564eb6f0de0SAdrian Chadd 	 * things have been cleaned up.
4565eb6f0de0SAdrian Chadd 	 */
45664dfd4507SAdrian Chadd 	ath_tx_tid_cleanup(sc, an, tid);
4567eb6f0de0SAdrian Chadd }
4568eb6f0de0SAdrian Chadd 
4569eb6f0de0SAdrian Chadd /*
4570eb6f0de0SAdrian Chadd  * Note: net80211 bar_timeout() doesn't call this function on BAR failure;
4571eb6f0de0SAdrian Chadd  * it simply tears down the aggregation session. Ew.
4572eb6f0de0SAdrian Chadd  *
4573eb6f0de0SAdrian Chadd  * It however will call ieee80211_ampdu_stop() which will call
4574eb6f0de0SAdrian Chadd  * ic->ic_addba_stop().
4575eb6f0de0SAdrian Chadd  *
4576eb6f0de0SAdrian Chadd  * XXX This uses a hard-coded max BAR count value; the whole
4577eb6f0de0SAdrian Chadd  * XXX BAR TX success or failure should be better handled!
4578eb6f0de0SAdrian Chadd  */
4579eb6f0de0SAdrian Chadd void
4580eb6f0de0SAdrian Chadd ath_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
4581eb6f0de0SAdrian Chadd     int status)
4582eb6f0de0SAdrian Chadd {
4583eb6f0de0SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
45842aa563dfSAdrian Chadd 	int tid = tap->txa_tid;
4585eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4586eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4587eb6f0de0SAdrian Chadd 	int attempts = tap->txa_attempts;
4588eb6f0de0SAdrian Chadd 
45890e22ed0eSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
4590e60c4fc2SAdrian Chadd 	    "%s: called; tap=%p, atid=%p, txa_tid=%d, atid->tid=%d, status=%d, attempts=%d\n",
45910e22ed0eSAdrian Chadd 	    __func__,
4592e60c4fc2SAdrian Chadd 	    tap,
4593e60c4fc2SAdrian Chadd 	    atid,
4594e60c4fc2SAdrian Chadd 	    tap->txa_tid,
4595e60c4fc2SAdrian Chadd 	    atid->tid,
45960e22ed0eSAdrian Chadd 	    status,
45970e22ed0eSAdrian Chadd 	    attempts);
4598eb6f0de0SAdrian Chadd 
4599eb6f0de0SAdrian Chadd 	/* Note: This may update the BAW details */
4600eb6f0de0SAdrian Chadd 	sc->sc_bar_response(ni, tap, status);
4601eb6f0de0SAdrian Chadd 
4602eb6f0de0SAdrian Chadd 	/* Unpause the TID */
4603eb6f0de0SAdrian Chadd 	/*
4604eb6f0de0SAdrian Chadd 	 * XXX if this is attempt=50, the TID will be downgraded
4605eb6f0de0SAdrian Chadd 	 * XXX to a non-aggregate session. So we must unpause the
4606eb6f0de0SAdrian Chadd 	 * XXX TID here or it'll never be done.
4607eb6f0de0SAdrian Chadd 	 */
4608eb6f0de0SAdrian Chadd 	if (status == 0 || attempts == 50) {
4609eb6f0de0SAdrian Chadd 		ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
461088b3d483SAdrian Chadd 		ath_tx_tid_bar_unsuspend(sc, atid);
4611eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4612eb6f0de0SAdrian Chadd 	}
4613eb6f0de0SAdrian Chadd }
4614eb6f0de0SAdrian Chadd 
4615eb6f0de0SAdrian Chadd /*
4616eb6f0de0SAdrian Chadd  * This is called whenever the pending ADDBA request times out.
4617eb6f0de0SAdrian Chadd  * Unpause and reschedule the TID.
4618eb6f0de0SAdrian Chadd  */
4619eb6f0de0SAdrian Chadd void
4620eb6f0de0SAdrian Chadd ath_addba_response_timeout(struct ieee80211_node *ni,
4621eb6f0de0SAdrian Chadd     struct ieee80211_tx_ampdu *tap)
4622eb6f0de0SAdrian Chadd {
4623eb6f0de0SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
46242aa563dfSAdrian Chadd 	int tid = tap->txa_tid;
4625eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4626eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4627eb6f0de0SAdrian Chadd 
4628eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4629eb6f0de0SAdrian Chadd 	    "%s: called; resuming\n", __func__);
4630eb6f0de0SAdrian Chadd 
4631d3a6425bSAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
4632d3a6425bSAdrian Chadd 	atid->addba_tx_pending = 0;
4633d3a6425bSAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4634d3a6425bSAdrian Chadd 
4635eb6f0de0SAdrian Chadd 	/* Note: This updates the aggregate state to (again) pending */
4636eb6f0de0SAdrian Chadd 	sc->sc_addba_response_timeout(ni, tap);
4637eb6f0de0SAdrian Chadd 
4638eb6f0de0SAdrian Chadd 	/* Unpause the TID; which reschedules it */
4639eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
4640eb6f0de0SAdrian Chadd 	ath_tx_tid_resume(sc, atid);
4641eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4642eb6f0de0SAdrian Chadd }
46433fdfc330SAdrian Chadd 
46443fdfc330SAdrian Chadd static int
46453fdfc330SAdrian Chadd ath_legacy_dma_txsetup(struct ath_softc *sc)
46463fdfc330SAdrian Chadd {
46473fdfc330SAdrian Chadd 
46483fdfc330SAdrian Chadd 	/* nothing new needed */
46493fdfc330SAdrian Chadd 	return (0);
46503fdfc330SAdrian Chadd }
46513fdfc330SAdrian Chadd 
46523fdfc330SAdrian Chadd static int
46533fdfc330SAdrian Chadd ath_legacy_dma_txteardown(struct ath_softc *sc)
46543fdfc330SAdrian Chadd {
46553fdfc330SAdrian Chadd 
46563fdfc330SAdrian Chadd 	/* nothing new needed */
46573fdfc330SAdrian Chadd 	return (0);
46583fdfc330SAdrian Chadd }
46593fdfc330SAdrian Chadd 
46603fdfc330SAdrian Chadd void
46613fdfc330SAdrian Chadd ath_xmit_setup_legacy(struct ath_softc *sc)
46623fdfc330SAdrian Chadd {
46631006fc0cSAdrian Chadd 	/*
46641006fc0cSAdrian Chadd 	 * For now, just set the descriptor length to sizeof(ath_desc);
46651006fc0cSAdrian Chadd 	 * worry about extracting the real length out of the HAL later.
46661006fc0cSAdrian Chadd 	 */
46671006fc0cSAdrian Chadd 	sc->sc_tx_desclen = sizeof(struct ath_desc);
46681006fc0cSAdrian Chadd 	sc->sc_tx_statuslen = 0;
46691006fc0cSAdrian Chadd 	sc->sc_tx_nmaps = 1;	/* only one buffer per TX desc */
46703fdfc330SAdrian Chadd 
46713fdfc330SAdrian Chadd 	sc->sc_tx.xmit_setup = ath_legacy_dma_txsetup;
46723fdfc330SAdrian Chadd 	sc->sc_tx.xmit_teardown = ath_legacy_dma_txteardown;
4673f8418db5SAdrian Chadd 	sc->sc_tx.xmit_attach_comp_func = ath_legacy_attach_comp_func;
4674746bab5bSAdrian Chadd 
4675746bab5bSAdrian Chadd 	sc->sc_tx.xmit_dma_restart = ath_legacy_tx_dma_restart;
4676746bab5bSAdrian Chadd 	sc->sc_tx.xmit_handoff = ath_legacy_xmit_handoff;
4677788e6aa9SAdrian Chadd 
4678788e6aa9SAdrian Chadd 	sc->sc_tx.xmit_drain = ath_legacy_tx_drain;
46793fdfc330SAdrian Chadd }
4680