xref: /freebsd/sys/dev/ath/if_ath_tx.c (revision 12be5b9c59e699c36f6653b3caf507d88d29d29f)
1b8e788a5SAdrian Chadd /*-
2b8e788a5SAdrian Chadd  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3b8e788a5SAdrian Chadd  * All rights reserved.
4b8e788a5SAdrian Chadd  *
5b8e788a5SAdrian Chadd  * Redistribution and use in source and binary forms, with or without
6b8e788a5SAdrian Chadd  * modification, are permitted provided that the following conditions
7b8e788a5SAdrian Chadd  * are met:
8b8e788a5SAdrian Chadd  * 1. Redistributions of source code must retain the above copyright
9b8e788a5SAdrian Chadd  *    notice, this list of conditions and the following disclaimer,
10b8e788a5SAdrian Chadd  *    without modification.
11b8e788a5SAdrian Chadd  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12b8e788a5SAdrian Chadd  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13b8e788a5SAdrian Chadd  *    redistribution must be conditioned upon including a substantially
14b8e788a5SAdrian Chadd  *    similar Disclaimer requirement for further binary redistribution.
15b8e788a5SAdrian Chadd  *
16b8e788a5SAdrian Chadd  * NO WARRANTY
17b8e788a5SAdrian Chadd  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18b8e788a5SAdrian Chadd  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19b8e788a5SAdrian Chadd  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20b8e788a5SAdrian Chadd  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21b8e788a5SAdrian Chadd  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22b8e788a5SAdrian Chadd  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23b8e788a5SAdrian Chadd  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24b8e788a5SAdrian Chadd  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25b8e788a5SAdrian Chadd  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26b8e788a5SAdrian Chadd  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27b8e788a5SAdrian Chadd  * THE POSSIBILITY OF SUCH DAMAGES.
28b8e788a5SAdrian Chadd  */
29b8e788a5SAdrian Chadd 
30b8e788a5SAdrian Chadd #include <sys/cdefs.h>
31b8e788a5SAdrian Chadd __FBSDID("$FreeBSD$");
32b8e788a5SAdrian Chadd 
33b8e788a5SAdrian Chadd /*
34b8e788a5SAdrian Chadd  * Driver for the Atheros Wireless LAN controller.
35b8e788a5SAdrian Chadd  *
36b8e788a5SAdrian Chadd  * This software is derived from work of Atsushi Onoe; his contribution
37b8e788a5SAdrian Chadd  * is greatly appreciated.
38b8e788a5SAdrian Chadd  */
39b8e788a5SAdrian Chadd 
40b8e788a5SAdrian Chadd #include "opt_inet.h"
41b8e788a5SAdrian Chadd #include "opt_ath.h"
42b8e788a5SAdrian Chadd #include "opt_wlan.h"
43b8e788a5SAdrian Chadd 
44b8e788a5SAdrian Chadd #include <sys/param.h>
45b8e788a5SAdrian Chadd #include <sys/systm.h>
46b8e788a5SAdrian Chadd #include <sys/sysctl.h>
47b8e788a5SAdrian Chadd #include <sys/mbuf.h>
48b8e788a5SAdrian Chadd #include <sys/malloc.h>
49b8e788a5SAdrian Chadd #include <sys/lock.h>
50b8e788a5SAdrian Chadd #include <sys/mutex.h>
51b8e788a5SAdrian Chadd #include <sys/kernel.h>
52b8e788a5SAdrian Chadd #include <sys/socket.h>
53b8e788a5SAdrian Chadd #include <sys/sockio.h>
54b8e788a5SAdrian Chadd #include <sys/errno.h>
55b8e788a5SAdrian Chadd #include <sys/callout.h>
56b8e788a5SAdrian Chadd #include <sys/bus.h>
57b8e788a5SAdrian Chadd #include <sys/endian.h>
58b8e788a5SAdrian Chadd #include <sys/kthread.h>
59b8e788a5SAdrian Chadd #include <sys/taskqueue.h>
60b8e788a5SAdrian Chadd #include <sys/priv.h>
61b8e788a5SAdrian Chadd 
62b8e788a5SAdrian Chadd #include <machine/bus.h>
63b8e788a5SAdrian Chadd 
64b8e788a5SAdrian Chadd #include <net/if.h>
65b8e788a5SAdrian Chadd #include <net/if_dl.h>
66b8e788a5SAdrian Chadd #include <net/if_media.h>
67b8e788a5SAdrian Chadd #include <net/if_types.h>
68b8e788a5SAdrian Chadd #include <net/if_arp.h>
69b8e788a5SAdrian Chadd #include <net/ethernet.h>
70b8e788a5SAdrian Chadd #include <net/if_llc.h>
71b8e788a5SAdrian Chadd 
72b8e788a5SAdrian Chadd #include <net80211/ieee80211_var.h>
73b8e788a5SAdrian Chadd #include <net80211/ieee80211_regdomain.h>
74b8e788a5SAdrian Chadd #ifdef IEEE80211_SUPPORT_SUPERG
75b8e788a5SAdrian Chadd #include <net80211/ieee80211_superg.h>
76b8e788a5SAdrian Chadd #endif
77b8e788a5SAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
78b8e788a5SAdrian Chadd #include <net80211/ieee80211_tdma.h>
79b8e788a5SAdrian Chadd #endif
80eb6f0de0SAdrian Chadd #include <net80211/ieee80211_ht.h>
81b8e788a5SAdrian Chadd 
82b8e788a5SAdrian Chadd #include <net/bpf.h>
83b8e788a5SAdrian Chadd 
84b8e788a5SAdrian Chadd #ifdef INET
85b8e788a5SAdrian Chadd #include <netinet/in.h>
86b8e788a5SAdrian Chadd #include <netinet/if_ether.h>
87b8e788a5SAdrian Chadd #endif
88b8e788a5SAdrian Chadd 
89b8e788a5SAdrian Chadd #include <dev/ath/if_athvar.h>
90b8e788a5SAdrian Chadd #include <dev/ath/ath_hal/ah_devid.h>		/* XXX for softled */
91b8e788a5SAdrian Chadd #include <dev/ath/ath_hal/ah_diagcodes.h>
92b8e788a5SAdrian Chadd 
93b8e788a5SAdrian Chadd #include <dev/ath/if_ath_debug.h>
94b8e788a5SAdrian Chadd 
95b8e788a5SAdrian Chadd #ifdef ATH_TX99_DIAG
96b8e788a5SAdrian Chadd #include <dev/ath/ath_tx99/ath_tx99.h>
97b8e788a5SAdrian Chadd #endif
98b8e788a5SAdrian Chadd 
99b8e788a5SAdrian Chadd #include <dev/ath/if_ath_misc.h>
100b8e788a5SAdrian Chadd #include <dev/ath/if_ath_tx.h>
101c1782ce0SAdrian Chadd #include <dev/ath/if_ath_tx_ht.h>
102b8e788a5SAdrian Chadd 
10381a82688SAdrian Chadd /*
104eb6f0de0SAdrian Chadd  * How many retries to perform in software
105eb6f0de0SAdrian Chadd  */
106eb6f0de0SAdrian Chadd #define	SWMAX_RETRIES		10
107eb6f0de0SAdrian Chadd 
108eb6f0de0SAdrian Chadd static int ath_tx_ampdu_pending(struct ath_softc *sc, struct ath_node *an,
109eb6f0de0SAdrian Chadd     int tid);
110eb6f0de0SAdrian Chadd static int ath_tx_ampdu_running(struct ath_softc *sc, struct ath_node *an,
111eb6f0de0SAdrian Chadd     int tid);
112eb6f0de0SAdrian Chadd static int ath_tx_action_frame_override_queue(struct ath_softc *sc,
113eb6f0de0SAdrian Chadd     struct ieee80211_node *ni, struct mbuf *m0, int *tid);
1140b96ef63SAdrian Chadd static int ath_tx_seqno_required(struct ath_softc *sc,
1150b96ef63SAdrian Chadd     struct ieee80211_node *ni, struct ath_buf *bf, struct mbuf *m0);
116eb6f0de0SAdrian Chadd 
117eb6f0de0SAdrian Chadd /*
11881a82688SAdrian Chadd  * Whether to use the 11n rate scenario functions or not
11981a82688SAdrian Chadd  */
12081a82688SAdrian Chadd static inline int
12181a82688SAdrian Chadd ath_tx_is_11n(struct ath_softc *sc)
12281a82688SAdrian Chadd {
12381a82688SAdrian Chadd 	return (sc->sc_ah->ah_magic == 0x20065416);
12481a82688SAdrian Chadd }
12581a82688SAdrian Chadd 
126eb6f0de0SAdrian Chadd /*
127eb6f0de0SAdrian Chadd  * Obtain the current TID from the given frame.
128eb6f0de0SAdrian Chadd  *
129eb6f0de0SAdrian Chadd  * Non-QoS frames need to go into TID 16 (IEEE80211_NONQOS_TID.)
130eb6f0de0SAdrian Chadd  * This has implications for which AC/priority the packet is placed
131eb6f0de0SAdrian Chadd  * in.
132eb6f0de0SAdrian Chadd  */
133eb6f0de0SAdrian Chadd static int
134eb6f0de0SAdrian Chadd ath_tx_gettid(struct ath_softc *sc, const struct mbuf *m0)
135eb6f0de0SAdrian Chadd {
136eb6f0de0SAdrian Chadd 	const struct ieee80211_frame *wh;
137eb6f0de0SAdrian Chadd 	int pri = M_WME_GETAC(m0);
138eb6f0de0SAdrian Chadd 
139eb6f0de0SAdrian Chadd 	wh = mtod(m0, const struct ieee80211_frame *);
140eb6f0de0SAdrian Chadd 	if (! IEEE80211_QOS_HAS_SEQ(wh))
141eb6f0de0SAdrian Chadd 		return IEEE80211_NONQOS_TID;
142eb6f0de0SAdrian Chadd 	else
143eb6f0de0SAdrian Chadd 		return WME_AC_TO_TID(pri);
144eb6f0de0SAdrian Chadd }
145eb6f0de0SAdrian Chadd 
146eb6f0de0SAdrian Chadd /*
147eb6f0de0SAdrian Chadd  * Determine what the correct AC queue for the given frame
148eb6f0de0SAdrian Chadd  * should be.
149eb6f0de0SAdrian Chadd  *
150eb6f0de0SAdrian Chadd  * This code assumes that the TIDs map consistently to
151eb6f0de0SAdrian Chadd  * the underlying hardware (or software) ath_txq.
152eb6f0de0SAdrian Chadd  * Since the sender may try to set an AC which is
153eb6f0de0SAdrian Chadd  * arbitrary, non-QoS TIDs may end up being put on
154eb6f0de0SAdrian Chadd  * completely different ACs. There's no way to put a
155eb6f0de0SAdrian Chadd  * TID into multiple ath_txq's for scheduling, so
156eb6f0de0SAdrian Chadd  * for now we override the AC/TXQ selection and set
157eb6f0de0SAdrian Chadd  * non-QOS TID frames into the BE queue.
158eb6f0de0SAdrian Chadd  *
159eb6f0de0SAdrian Chadd  * This may be completely incorrect - specifically,
160eb6f0de0SAdrian Chadd  * some management frames may end up out of order
161eb6f0de0SAdrian Chadd  * compared to the QoS traffic they're controlling.
162eb6f0de0SAdrian Chadd  * I'll look into this later.
163eb6f0de0SAdrian Chadd  */
164eb6f0de0SAdrian Chadd static int
165eb6f0de0SAdrian Chadd ath_tx_getac(struct ath_softc *sc, const struct mbuf *m0)
166eb6f0de0SAdrian Chadd {
167eb6f0de0SAdrian Chadd 	const struct ieee80211_frame *wh;
168eb6f0de0SAdrian Chadd 	int pri = M_WME_GETAC(m0);
169eb6f0de0SAdrian Chadd 	wh = mtod(m0, const struct ieee80211_frame *);
170eb6f0de0SAdrian Chadd 	if (IEEE80211_QOS_HAS_SEQ(wh))
171eb6f0de0SAdrian Chadd 		return pri;
172eb6f0de0SAdrian Chadd 
173eb6f0de0SAdrian Chadd 	return WME_AC_BE;
174eb6f0de0SAdrian Chadd }
175eb6f0de0SAdrian Chadd 
176b8e788a5SAdrian Chadd void
177b8e788a5SAdrian Chadd ath_txfrag_cleanup(struct ath_softc *sc,
178b8e788a5SAdrian Chadd 	ath_bufhead *frags, struct ieee80211_node *ni)
179b8e788a5SAdrian Chadd {
180b8e788a5SAdrian Chadd 	struct ath_buf *bf, *next;
181b8e788a5SAdrian Chadd 
182b8e788a5SAdrian Chadd 	ATH_TXBUF_LOCK_ASSERT(sc);
183b8e788a5SAdrian Chadd 
1846b349e5aSAdrian Chadd 	TAILQ_FOREACH_SAFE(bf, frags, bf_list, next) {
185b8e788a5SAdrian Chadd 		/* NB: bf assumed clean */
1866b349e5aSAdrian Chadd 		TAILQ_REMOVE(frags, bf, bf_list);
1876b349e5aSAdrian Chadd 		TAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
188b8e788a5SAdrian Chadd 		ieee80211_node_decref(ni);
189b8e788a5SAdrian Chadd 	}
190b8e788a5SAdrian Chadd }
191b8e788a5SAdrian Chadd 
192b8e788a5SAdrian Chadd /*
193b8e788a5SAdrian Chadd  * Setup xmit of a fragmented frame.  Allocate a buffer
194b8e788a5SAdrian Chadd  * for each frag and bump the node reference count to
195b8e788a5SAdrian Chadd  * reflect the held reference to be setup by ath_tx_start.
196b8e788a5SAdrian Chadd  */
197b8e788a5SAdrian Chadd int
198b8e788a5SAdrian Chadd ath_txfrag_setup(struct ath_softc *sc, ath_bufhead *frags,
199b8e788a5SAdrian Chadd 	struct mbuf *m0, struct ieee80211_node *ni)
200b8e788a5SAdrian Chadd {
201b8e788a5SAdrian Chadd 	struct mbuf *m;
202b8e788a5SAdrian Chadd 	struct ath_buf *bf;
203b8e788a5SAdrian Chadd 
204b8e788a5SAdrian Chadd 	ATH_TXBUF_LOCK(sc);
205b8e788a5SAdrian Chadd 	for (m = m0->m_nextpkt; m != NULL; m = m->m_nextpkt) {
206b8e788a5SAdrian Chadd 		bf = _ath_getbuf_locked(sc);
207b8e788a5SAdrian Chadd 		if (bf == NULL) {	/* out of buffers, cleanup */
208b8e788a5SAdrian Chadd 			ath_txfrag_cleanup(sc, frags, ni);
209b8e788a5SAdrian Chadd 			break;
210b8e788a5SAdrian Chadd 		}
211b8e788a5SAdrian Chadd 		ieee80211_node_incref(ni);
2126b349e5aSAdrian Chadd 		TAILQ_INSERT_TAIL(frags, bf, bf_list);
213b8e788a5SAdrian Chadd 	}
214b8e788a5SAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
215b8e788a5SAdrian Chadd 
2166b349e5aSAdrian Chadd 	return !TAILQ_EMPTY(frags);
217b8e788a5SAdrian Chadd }
218b8e788a5SAdrian Chadd 
219b8e788a5SAdrian Chadd /*
220b8e788a5SAdrian Chadd  * Reclaim mbuf resources.  For fragmented frames we
221b8e788a5SAdrian Chadd  * need to claim each frag chained with m_nextpkt.
222b8e788a5SAdrian Chadd  */
223b8e788a5SAdrian Chadd void
224b8e788a5SAdrian Chadd ath_freetx(struct mbuf *m)
225b8e788a5SAdrian Chadd {
226b8e788a5SAdrian Chadd 	struct mbuf *next;
227b8e788a5SAdrian Chadd 
228b8e788a5SAdrian Chadd 	do {
229b8e788a5SAdrian Chadd 		next = m->m_nextpkt;
230b8e788a5SAdrian Chadd 		m->m_nextpkt = NULL;
231b8e788a5SAdrian Chadd 		m_freem(m);
232b8e788a5SAdrian Chadd 	} while ((m = next) != NULL);
233b8e788a5SAdrian Chadd }
234b8e788a5SAdrian Chadd 
235b8e788a5SAdrian Chadd static int
236b8e788a5SAdrian Chadd ath_tx_dmasetup(struct ath_softc *sc, struct ath_buf *bf, struct mbuf *m0)
237b8e788a5SAdrian Chadd {
238b8e788a5SAdrian Chadd 	struct mbuf *m;
239b8e788a5SAdrian Chadd 	int error;
240b8e788a5SAdrian Chadd 
241b8e788a5SAdrian Chadd 	/*
242b8e788a5SAdrian Chadd 	 * Load the DMA map so any coalescing is done.  This
243b8e788a5SAdrian Chadd 	 * also calculates the number of descriptors we need.
244b8e788a5SAdrian Chadd 	 */
245b8e788a5SAdrian Chadd 	error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0,
246b8e788a5SAdrian Chadd 				     bf->bf_segs, &bf->bf_nseg,
247b8e788a5SAdrian Chadd 				     BUS_DMA_NOWAIT);
248b8e788a5SAdrian Chadd 	if (error == EFBIG) {
249b8e788a5SAdrian Chadd 		/* XXX packet requires too many descriptors */
250b8e788a5SAdrian Chadd 		bf->bf_nseg = ATH_TXDESC+1;
251b8e788a5SAdrian Chadd 	} else if (error != 0) {
252b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_busdma++;
253b8e788a5SAdrian Chadd 		ath_freetx(m0);
254b8e788a5SAdrian Chadd 		return error;
255b8e788a5SAdrian Chadd 	}
256b8e788a5SAdrian Chadd 	/*
257b8e788a5SAdrian Chadd 	 * Discard null packets and check for packets that
258b8e788a5SAdrian Chadd 	 * require too many TX descriptors.  We try to convert
259b8e788a5SAdrian Chadd 	 * the latter to a cluster.
260b8e788a5SAdrian Chadd 	 */
261b8e788a5SAdrian Chadd 	if (bf->bf_nseg > ATH_TXDESC) {		/* too many desc's, linearize */
262b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_linear++;
263b8e788a5SAdrian Chadd 		m = m_collapse(m0, M_DONTWAIT, ATH_TXDESC);
264b8e788a5SAdrian Chadd 		if (m == NULL) {
265b8e788a5SAdrian Chadd 			ath_freetx(m0);
266b8e788a5SAdrian Chadd 			sc->sc_stats.ast_tx_nombuf++;
267b8e788a5SAdrian Chadd 			return ENOMEM;
268b8e788a5SAdrian Chadd 		}
269b8e788a5SAdrian Chadd 		m0 = m;
270b8e788a5SAdrian Chadd 		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0,
271b8e788a5SAdrian Chadd 					     bf->bf_segs, &bf->bf_nseg,
272b8e788a5SAdrian Chadd 					     BUS_DMA_NOWAIT);
273b8e788a5SAdrian Chadd 		if (error != 0) {
274b8e788a5SAdrian Chadd 			sc->sc_stats.ast_tx_busdma++;
275b8e788a5SAdrian Chadd 			ath_freetx(m0);
276b8e788a5SAdrian Chadd 			return error;
277b8e788a5SAdrian Chadd 		}
278b8e788a5SAdrian Chadd 		KASSERT(bf->bf_nseg <= ATH_TXDESC,
279b8e788a5SAdrian Chadd 		    ("too many segments after defrag; nseg %u", bf->bf_nseg));
280b8e788a5SAdrian Chadd 	} else if (bf->bf_nseg == 0) {		/* null packet, discard */
281b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_nodata++;
282b8e788a5SAdrian Chadd 		ath_freetx(m0);
283b8e788a5SAdrian Chadd 		return EIO;
284b8e788a5SAdrian Chadd 	}
285b8e788a5SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_XMIT, "%s: m %p len %u\n",
286b8e788a5SAdrian Chadd 		__func__, m0, m0->m_pkthdr.len);
287b8e788a5SAdrian Chadd 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
288b8e788a5SAdrian Chadd 	bf->bf_m = m0;
289b8e788a5SAdrian Chadd 
290b8e788a5SAdrian Chadd 	return 0;
291b8e788a5SAdrian Chadd }
292b8e788a5SAdrian Chadd 
2936edf1dc7SAdrian Chadd /*
2946edf1dc7SAdrian Chadd  * Chain together segments+descriptors for a non-11n frame.
2956edf1dc7SAdrian Chadd  */
296b8e788a5SAdrian Chadd static void
297eb6f0de0SAdrian Chadd ath_tx_chaindesclist(struct ath_softc *sc, struct ath_buf *bf)
298b8e788a5SAdrian Chadd {
299b8e788a5SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
300b8e788a5SAdrian Chadd 	struct ath_desc *ds, *ds0;
301b8e788a5SAdrian Chadd 	int i;
302b8e788a5SAdrian Chadd 
303b8e788a5SAdrian Chadd 	/*
304b8e788a5SAdrian Chadd 	 * Fillin the remainder of the descriptor info.
305b8e788a5SAdrian Chadd 	 */
306b8e788a5SAdrian Chadd 	ds0 = ds = bf->bf_desc;
307b8e788a5SAdrian Chadd 	for (i = 0; i < bf->bf_nseg; i++, ds++) {
308b8e788a5SAdrian Chadd 		ds->ds_data = bf->bf_segs[i].ds_addr;
309b8e788a5SAdrian Chadd 		if (i == bf->bf_nseg - 1)
310b8e788a5SAdrian Chadd 			ds->ds_link = 0;
311b8e788a5SAdrian Chadd 		else
312b8e788a5SAdrian Chadd 			ds->ds_link = bf->bf_daddr + sizeof(*ds) * (i + 1);
313b8e788a5SAdrian Chadd 		ath_hal_filltxdesc(ah, ds
314b8e788a5SAdrian Chadd 			, bf->bf_segs[i].ds_len	/* segment length */
315b8e788a5SAdrian Chadd 			, i == 0		/* first segment */
316b8e788a5SAdrian Chadd 			, i == bf->bf_nseg - 1	/* last segment */
317b8e788a5SAdrian Chadd 			, ds0			/* first descriptor */
318b8e788a5SAdrian Chadd 		);
319b8e788a5SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_XMIT,
320b8e788a5SAdrian Chadd 			"%s: %d: %08x %08x %08x %08x %08x %08x\n",
321b8e788a5SAdrian Chadd 			__func__, i, ds->ds_link, ds->ds_data,
322b8e788a5SAdrian Chadd 			ds->ds_ctl0, ds->ds_ctl1, ds->ds_hw[0], ds->ds_hw[1]);
3236edf1dc7SAdrian Chadd 		bf->bf_lastds = ds;
324b8e788a5SAdrian Chadd 	}
32581a82688SAdrian Chadd }
32681a82688SAdrian Chadd 
327eb6f0de0SAdrian Chadd /*
328eb6f0de0SAdrian Chadd  * Fill in the descriptor list for a aggregate subframe.
329eb6f0de0SAdrian Chadd  *
330eb6f0de0SAdrian Chadd  * The subframe is returned with the ds_link field in the last subframe
331eb6f0de0SAdrian Chadd  * pointing to 0.
332eb6f0de0SAdrian Chadd  */
33381a82688SAdrian Chadd static void
334eb6f0de0SAdrian Chadd ath_tx_chaindesclist_subframe(struct ath_softc *sc, struct ath_buf *bf)
33581a82688SAdrian Chadd {
33681a82688SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
337eb6f0de0SAdrian Chadd 	struct ath_desc *ds, *ds0;
338eb6f0de0SAdrian Chadd 	int i;
33981a82688SAdrian Chadd 
340eb6f0de0SAdrian Chadd 	ds0 = ds = bf->bf_desc;
341eb6f0de0SAdrian Chadd 
342eb6f0de0SAdrian Chadd 	/*
343eb6f0de0SAdrian Chadd 	 * There's no need to call ath_hal_setupfirsttxdesc here;
344eb6f0de0SAdrian Chadd 	 * That's only going to occur for the first frame in an aggregate.
345eb6f0de0SAdrian Chadd 	 */
346eb6f0de0SAdrian Chadd 	for (i = 0; i < bf->bf_nseg; i++, ds++) {
347eb6f0de0SAdrian Chadd 		ds->ds_data = bf->bf_segs[i].ds_addr;
348eb6f0de0SAdrian Chadd 		if (i == bf->bf_nseg - 1)
349eb6f0de0SAdrian Chadd 			ds->ds_link = 0;
350eb6f0de0SAdrian Chadd 		else
351eb6f0de0SAdrian Chadd 			ds->ds_link = bf->bf_daddr + sizeof(*ds) * (i + 1);
352eb6f0de0SAdrian Chadd 
353eb6f0de0SAdrian Chadd 		/*
354eb6f0de0SAdrian Chadd 		 * This performs the setup for an aggregate frame.
355eb6f0de0SAdrian Chadd 		 * This includes enabling the aggregate flags if needed.
356eb6f0de0SAdrian Chadd 		 */
357eb6f0de0SAdrian Chadd 		ath_hal_chaintxdesc(ah, ds,
358eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_pktlen,
359eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_hdrlen,
360eb6f0de0SAdrian Chadd 		    HAL_PKT_TYPE_AMPDU,	/* forces aggregate bits to be set */
361eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_keyix,
362eb6f0de0SAdrian Chadd 		    0,			/* cipher, calculated from keyix */
363eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_ndelim,
364eb6f0de0SAdrian Chadd 		    bf->bf_segs[i].ds_len,	/* segment length */
365eb6f0de0SAdrian Chadd 		    i == 0,		/* first segment */
366eb6f0de0SAdrian Chadd 		    i == bf->bf_nseg - 1	/* last segment */
367eb6f0de0SAdrian Chadd 		);
368eb6f0de0SAdrian Chadd 
369eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_XMIT,
370eb6f0de0SAdrian Chadd 			"%s: %d: %08x %08x %08x %08x %08x %08x\n",
371eb6f0de0SAdrian Chadd 			__func__, i, ds->ds_link, ds->ds_data,
372eb6f0de0SAdrian Chadd 			ds->ds_ctl0, ds->ds_ctl1, ds->ds_hw[0], ds->ds_hw[1]);
373eb6f0de0SAdrian Chadd 		bf->bf_lastds = ds;
374eb6f0de0SAdrian Chadd 	}
375eb6f0de0SAdrian Chadd }
376eb6f0de0SAdrian Chadd 
377eb6f0de0SAdrian Chadd /*
378eb6f0de0SAdrian Chadd  * Setup segments+descriptors for an 11n aggregate.
379eb6f0de0SAdrian Chadd  * bf_first is the first buffer in the aggregate.
380eb6f0de0SAdrian Chadd  * The descriptor list must already been linked together using
381eb6f0de0SAdrian Chadd  * bf->bf_next.
382eb6f0de0SAdrian Chadd  */
383eb6f0de0SAdrian Chadd static void
384eb6f0de0SAdrian Chadd ath_tx_setds_11n(struct ath_softc *sc, struct ath_buf *bf_first)
385eb6f0de0SAdrian Chadd {
386eb6f0de0SAdrian Chadd 	struct ath_buf *bf, *bf_prev = NULL;
387eb6f0de0SAdrian Chadd 
388eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: nframes=%d, al=%d\n",
389eb6f0de0SAdrian Chadd 	    __func__, bf_first->bf_state.bfs_nframes,
390eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_al);
391eb6f0de0SAdrian Chadd 
392eb6f0de0SAdrian Chadd 	/*
393eb6f0de0SAdrian Chadd 	 * Setup all descriptors of all subframes.
394eb6f0de0SAdrian Chadd 	 */
395eb6f0de0SAdrian Chadd 	bf = bf_first;
396eb6f0de0SAdrian Chadd 	while (bf != NULL) {
397eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
398eb6f0de0SAdrian Chadd 		    "%s: bf=%p, nseg=%d, pktlen=%d, seqno=%d\n",
399eb6f0de0SAdrian Chadd 		    __func__, bf, bf->bf_nseg, bf->bf_state.bfs_pktlen,
400eb6f0de0SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
401eb6f0de0SAdrian Chadd 
402eb6f0de0SAdrian Chadd 		/* Sub-frame setup */
403eb6f0de0SAdrian Chadd 		ath_tx_chaindesclist_subframe(sc, bf);
404eb6f0de0SAdrian Chadd 
405eb6f0de0SAdrian Chadd 		/*
406eb6f0de0SAdrian Chadd 		 * Link the last descriptor of the previous frame
407eb6f0de0SAdrian Chadd 		 * to the beginning descriptor of this frame.
408eb6f0de0SAdrian Chadd 		 */
409eb6f0de0SAdrian Chadd 		if (bf_prev != NULL)
410eb6f0de0SAdrian Chadd 			bf_prev->bf_lastds->ds_link = bf->bf_daddr;
411eb6f0de0SAdrian Chadd 
412eb6f0de0SAdrian Chadd 		/* Save a copy so we can link the next descriptor in */
413eb6f0de0SAdrian Chadd 		bf_prev = bf;
414eb6f0de0SAdrian Chadd 		bf = bf->bf_next;
415eb6f0de0SAdrian Chadd 	}
416eb6f0de0SAdrian Chadd 
417eb6f0de0SAdrian Chadd 	/*
418eb6f0de0SAdrian Chadd 	 * Setup first descriptor of first frame.
419eb6f0de0SAdrian Chadd 	 * chaintxdesc() overwrites the descriptor entries;
420eb6f0de0SAdrian Chadd 	 * setupfirsttxdesc() merges in things.
421eb6f0de0SAdrian Chadd 	 * Otherwise various fields aren't set correctly (eg flags).
422eb6f0de0SAdrian Chadd 	 */
423eb6f0de0SAdrian Chadd 	ath_hal_setupfirsttxdesc(sc->sc_ah,
424eb6f0de0SAdrian Chadd 	    bf_first->bf_desc,
425eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_al,
426eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_flags | HAL_TXDESC_INTREQ,
427eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_txpower,
428eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_txrate0,
429eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_try0,
430eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_txantenna,
431eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_ctsrate,
432eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_ctsduration);
433eb6f0de0SAdrian Chadd 
434eb6f0de0SAdrian Chadd 	/*
435eb6f0de0SAdrian Chadd 	 * Setup the last descriptor in the list.
436eb6f0de0SAdrian Chadd 	 * bf_prev points to the last; bf is NULL here.
437eb6f0de0SAdrian Chadd 	 */
438d4365d16SAdrian Chadd 	ath_hal_setuplasttxdesc(sc->sc_ah, bf_prev->bf_desc,
439d4365d16SAdrian Chadd 	    bf_first->bf_desc);
440eb6f0de0SAdrian Chadd 
441eb6f0de0SAdrian Chadd 	/*
442eb6f0de0SAdrian Chadd 	 * Set the first descriptor bf_lastds field to point to
443eb6f0de0SAdrian Chadd 	 * the last descriptor in the last subframe, that's where
444eb6f0de0SAdrian Chadd 	 * the status update will occur.
445eb6f0de0SAdrian Chadd 	 */
446eb6f0de0SAdrian Chadd 	bf_first->bf_lastds = bf_prev->bf_lastds;
447eb6f0de0SAdrian Chadd 
448eb6f0de0SAdrian Chadd 	/*
449eb6f0de0SAdrian Chadd 	 * And bf_last in the first descriptor points to the end of
450eb6f0de0SAdrian Chadd 	 * the aggregate list.
451eb6f0de0SAdrian Chadd 	 */
452eb6f0de0SAdrian Chadd 	bf_first->bf_last = bf_prev;
453eb6f0de0SAdrian Chadd 
454eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: end\n", __func__);
455eb6f0de0SAdrian Chadd }
456eb6f0de0SAdrian Chadd 
457eb6f0de0SAdrian Chadd static void
458eb6f0de0SAdrian Chadd ath_tx_handoff_mcast(struct ath_softc *sc, struct ath_txq *txq,
459eb6f0de0SAdrian Chadd     struct ath_buf *bf)
460eb6f0de0SAdrian Chadd {
461eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
462eb6f0de0SAdrian Chadd 	KASSERT((bf->bf_flags & ATH_BUF_BUSY) == 0,
463eb6f0de0SAdrian Chadd 	     ("%s: busy status 0x%x", __func__, bf->bf_flags));
464eb6f0de0SAdrian Chadd 	if (txq->axq_link != NULL) {
465eb6f0de0SAdrian Chadd 		struct ath_buf *last = ATH_TXQ_LAST(txq, axq_q_s);
466eb6f0de0SAdrian Chadd 		struct ieee80211_frame *wh;
467eb6f0de0SAdrian Chadd 
468eb6f0de0SAdrian Chadd 		/* mark previous frame */
469eb6f0de0SAdrian Chadd 		wh = mtod(last->bf_m, struct ieee80211_frame *);
470eb6f0de0SAdrian Chadd 		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
471eb6f0de0SAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, last->bf_dmamap,
472eb6f0de0SAdrian Chadd 		    BUS_DMASYNC_PREWRITE);
473eb6f0de0SAdrian Chadd 
474eb6f0de0SAdrian Chadd 		/* link descriptor */
475eb6f0de0SAdrian Chadd 		*txq->axq_link = bf->bf_daddr;
476eb6f0de0SAdrian Chadd 	}
477eb6f0de0SAdrian Chadd 	ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
478eb6f0de0SAdrian Chadd 	txq->axq_link = &bf->bf_lastds->ds_link;
479eb6f0de0SAdrian Chadd }
480eb6f0de0SAdrian Chadd 
481eb6f0de0SAdrian Chadd /*
482eb6f0de0SAdrian Chadd  * Hand-off packet to a hardware queue.
483eb6f0de0SAdrian Chadd  */
484eb6f0de0SAdrian Chadd static void
485d4365d16SAdrian Chadd ath_tx_handoff_hw(struct ath_softc *sc, struct ath_txq *txq,
486d4365d16SAdrian Chadd     struct ath_buf *bf)
487eb6f0de0SAdrian Chadd {
488eb6f0de0SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
48981a82688SAdrian Chadd 
490b8e788a5SAdrian Chadd 	/*
491b8e788a5SAdrian Chadd 	 * Insert the frame on the outbound list and pass it on
492b8e788a5SAdrian Chadd 	 * to the hardware.  Multicast frames buffered for power
493b8e788a5SAdrian Chadd 	 * save stations and transmit from the CAB queue are stored
494b8e788a5SAdrian Chadd 	 * on a s/w only queue and loaded on to the CAB queue in
495b8e788a5SAdrian Chadd 	 * the SWBA handler since frames only go out on DTIM and
496b8e788a5SAdrian Chadd 	 * to avoid possible races.
497b8e788a5SAdrian Chadd 	 */
498eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
499b8e788a5SAdrian Chadd 	KASSERT((bf->bf_flags & ATH_BUF_BUSY) == 0,
500eb6f0de0SAdrian Chadd 	     ("%s: busy status 0x%x", __func__, bf->bf_flags));
501eb6f0de0SAdrian Chadd 	KASSERT(txq->axq_qnum != ATH_TXQ_SWQ,
502eb6f0de0SAdrian Chadd 	     ("ath_tx_handoff_hw called for mcast queue"));
503eb6f0de0SAdrian Chadd 
504ef27340cSAdrian Chadd #if 0
505ef27340cSAdrian Chadd 	/*
506ef27340cSAdrian Chadd 	 * This causes a LOR. Find out where the PCU lock is being
507ef27340cSAdrian Chadd 	 * held whilst the TXQ lock is grabbed - that shouldn't
508ef27340cSAdrian Chadd 	 * be occuring.
509ef27340cSAdrian Chadd 	 */
510ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
511ef27340cSAdrian Chadd 	if (sc->sc_inreset_cnt) {
512ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
513ef27340cSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_RESET,
514ef27340cSAdrian Chadd 		    "%s: called with sc_in_reset != 0\n",
515ef27340cSAdrian Chadd 		    __func__);
516ef27340cSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_XMIT,
517ef27340cSAdrian Chadd 		    "%s: queued: TXDP[%u] = %p (%p) depth %d\n",
518ef27340cSAdrian Chadd 		    __func__, txq->axq_qnum,
519ef27340cSAdrian Chadd 		    (caddr_t)bf->bf_daddr, bf->bf_desc,
520ef27340cSAdrian Chadd 		    txq->axq_depth);
521ef27340cSAdrian Chadd 		ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
522ef27340cSAdrian Chadd 		if (bf->bf_state.bfs_aggr)
523ef27340cSAdrian Chadd 			txq->axq_aggr_depth++;
524ef27340cSAdrian Chadd 		/*
525ef27340cSAdrian Chadd 		 * There's no need to update axq_link; the hardware
526ef27340cSAdrian Chadd 		 * is in reset and once the reset is complete, any
527ef27340cSAdrian Chadd 		 * non-empty queues will simply have DMA restarted.
528ef27340cSAdrian Chadd 		 */
529ef27340cSAdrian Chadd 		return;
530ef27340cSAdrian Chadd 		}
531ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
532ef27340cSAdrian Chadd #endif
533ef27340cSAdrian Chadd 
534eb6f0de0SAdrian Chadd 	/* For now, so not to generate whitespace diffs */
535eb6f0de0SAdrian Chadd 	if (1) {
536b8e788a5SAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
537b8e788a5SAdrian Chadd 		int qbusy;
538b8e788a5SAdrian Chadd 
539b8e788a5SAdrian Chadd 		ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
540b8e788a5SAdrian Chadd 		qbusy = ath_hal_txqenabled(ah, txq->axq_qnum);
541b8e788a5SAdrian Chadd 		if (txq->axq_link == NULL) {
542b8e788a5SAdrian Chadd 			/*
543b8e788a5SAdrian Chadd 			 * Be careful writing the address to TXDP.  If
544b8e788a5SAdrian Chadd 			 * the tx q is enabled then this write will be
545b8e788a5SAdrian Chadd 			 * ignored.  Normally this is not an issue but
546b8e788a5SAdrian Chadd 			 * when tdma is in use and the q is beacon gated
547b8e788a5SAdrian Chadd 			 * this race can occur.  If the q is busy then
548b8e788a5SAdrian Chadd 			 * defer the work to later--either when another
549b8e788a5SAdrian Chadd 			 * packet comes along or when we prepare a beacon
550b8e788a5SAdrian Chadd 			 * frame at SWBA.
551b8e788a5SAdrian Chadd 			 */
552b8e788a5SAdrian Chadd 			if (!qbusy) {
553d4365d16SAdrian Chadd 				ath_hal_puttxbuf(ah, txq->axq_qnum,
554d4365d16SAdrian Chadd 				    bf->bf_daddr);
555b8e788a5SAdrian Chadd 				txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
556b8e788a5SAdrian Chadd 				DPRINTF(sc, ATH_DEBUG_XMIT,
557b8e788a5SAdrian Chadd 				    "%s: TXDP[%u] = %p (%p) depth %d\n",
558b8e788a5SAdrian Chadd 				    __func__, txq->axq_qnum,
559b8e788a5SAdrian Chadd 				    (caddr_t)bf->bf_daddr, bf->bf_desc,
560b8e788a5SAdrian Chadd 				    txq->axq_depth);
561b8e788a5SAdrian Chadd 			} else {
562b8e788a5SAdrian Chadd 				txq->axq_flags |= ATH_TXQ_PUTPENDING;
563b8e788a5SAdrian Chadd 				DPRINTF(sc, ATH_DEBUG_TDMA | ATH_DEBUG_XMIT,
564b8e788a5SAdrian Chadd 				    "%s: Q%u busy, defer enable\n", __func__,
565b8e788a5SAdrian Chadd 				    txq->axq_qnum);
566b8e788a5SAdrian Chadd 			}
567b8e788a5SAdrian Chadd 		} else {
568b8e788a5SAdrian Chadd 			*txq->axq_link = bf->bf_daddr;
569b8e788a5SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_XMIT,
570b8e788a5SAdrian Chadd 			    "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
571b8e788a5SAdrian Chadd 			    txq->axq_qnum, txq->axq_link,
572d4365d16SAdrian Chadd 			    (caddr_t)bf->bf_daddr, bf->bf_desc,
573d4365d16SAdrian Chadd 			    txq->axq_depth);
574b8e788a5SAdrian Chadd 			if ((txq->axq_flags & ATH_TXQ_PUTPENDING) && !qbusy) {
575b8e788a5SAdrian Chadd 				/*
576b8e788a5SAdrian Chadd 				 * The q was busy when we previously tried
577b8e788a5SAdrian Chadd 				 * to write the address of the first buffer
578b8e788a5SAdrian Chadd 				 * in the chain.  Since it's not busy now
579b8e788a5SAdrian Chadd 				 * handle this chore.  We are certain the
580b8e788a5SAdrian Chadd 				 * buffer at the front is the right one since
581b8e788a5SAdrian Chadd 				 * axq_link is NULL only when the buffer list
582b8e788a5SAdrian Chadd 				 * is/was empty.
583b8e788a5SAdrian Chadd 				 */
584b8e788a5SAdrian Chadd 				ath_hal_puttxbuf(ah, txq->axq_qnum,
5856b349e5aSAdrian Chadd 					TAILQ_FIRST(&txq->axq_q)->bf_daddr);
586b8e788a5SAdrian Chadd 				txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
587b8e788a5SAdrian Chadd 				DPRINTF(sc, ATH_DEBUG_TDMA | ATH_DEBUG_XMIT,
588b8e788a5SAdrian Chadd 				    "%s: Q%u restarted\n", __func__,
589b8e788a5SAdrian Chadd 				    txq->axq_qnum);
590b8e788a5SAdrian Chadd 			}
591b8e788a5SAdrian Chadd 		}
592b8e788a5SAdrian Chadd #else
593b8e788a5SAdrian Chadd 		ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
594b8e788a5SAdrian Chadd 		if (txq->axq_link == NULL) {
595b8e788a5SAdrian Chadd 			ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
596b8e788a5SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_XMIT,
597b8e788a5SAdrian Chadd 			    "%s: TXDP[%u] = %p (%p) depth %d\n",
598b8e788a5SAdrian Chadd 			    __func__, txq->axq_qnum,
599b8e788a5SAdrian Chadd 			    (caddr_t)bf->bf_daddr, bf->bf_desc,
600b8e788a5SAdrian Chadd 			    txq->axq_depth);
601b8e788a5SAdrian Chadd 		} else {
602b8e788a5SAdrian Chadd 			*txq->axq_link = bf->bf_daddr;
603b8e788a5SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_XMIT,
604b8e788a5SAdrian Chadd 			    "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
605b8e788a5SAdrian Chadd 			    txq->axq_qnum, txq->axq_link,
606d4365d16SAdrian Chadd 			    (caddr_t)bf->bf_daddr, bf->bf_desc,
607d4365d16SAdrian Chadd 			    txq->axq_depth);
608b8e788a5SAdrian Chadd 		}
609b8e788a5SAdrian Chadd #endif /* IEEE80211_SUPPORT_TDMA */
6106edf1dc7SAdrian Chadd 		if (bf->bf_state.bfs_aggr)
6116edf1dc7SAdrian Chadd 			txq->axq_aggr_depth++;
6126edf1dc7SAdrian Chadd 		txq->axq_link = &bf->bf_lastds->ds_link;
613b8e788a5SAdrian Chadd 		ath_hal_txstart(ah, txq->axq_qnum);
614b8e788a5SAdrian Chadd 	}
615b8e788a5SAdrian Chadd }
616eb6f0de0SAdrian Chadd 
617eb6f0de0SAdrian Chadd /*
618eb6f0de0SAdrian Chadd  * Restart TX DMA for the given TXQ.
619eb6f0de0SAdrian Chadd  *
620eb6f0de0SAdrian Chadd  * This must be called whether the queue is empty or not.
621eb6f0de0SAdrian Chadd  */
622eb6f0de0SAdrian Chadd void
623eb6f0de0SAdrian Chadd ath_txq_restart_dma(struct ath_softc *sc, struct ath_txq *txq)
624eb6f0de0SAdrian Chadd {
625eb6f0de0SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
626b1f3262cSAdrian Chadd 	struct ath_buf *bf, *bf_last;
627eb6f0de0SAdrian Chadd 
628eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
629eb6f0de0SAdrian Chadd 
630eb6f0de0SAdrian Chadd 	/* This is always going to be cleared, empty or not */
631eb6f0de0SAdrian Chadd 	txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
632eb6f0de0SAdrian Chadd 
633b1f3262cSAdrian Chadd 	/* XXX make this ATH_TXQ_FIRST */
634eb6f0de0SAdrian Chadd 	bf = TAILQ_FIRST(&txq->axq_q);
635b1f3262cSAdrian Chadd 	bf_last = ATH_TXQ_LAST(txq, axq_q_s);
636b1f3262cSAdrian Chadd 
637eb6f0de0SAdrian Chadd 	if (bf == NULL)
638eb6f0de0SAdrian Chadd 		return;
639eb6f0de0SAdrian Chadd 
640eb6f0de0SAdrian Chadd 	ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
641b1f3262cSAdrian Chadd 	txq->axq_link = &bf_last->bf_lastds->ds_link;
642eb6f0de0SAdrian Chadd 	ath_hal_txstart(ah, txq->axq_qnum);
643eb6f0de0SAdrian Chadd }
644eb6f0de0SAdrian Chadd 
645eb6f0de0SAdrian Chadd /*
646eb6f0de0SAdrian Chadd  * Hand off a packet to the hardware (or mcast queue.)
647eb6f0de0SAdrian Chadd  *
648eb6f0de0SAdrian Chadd  * The relevant hardware txq should be locked.
649eb6f0de0SAdrian Chadd  */
650eb6f0de0SAdrian Chadd static void
651eb6f0de0SAdrian Chadd ath_tx_handoff(struct ath_softc *sc, struct ath_txq *txq, struct ath_buf *bf)
652eb6f0de0SAdrian Chadd {
653eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
654eb6f0de0SAdrian Chadd 
655eb6f0de0SAdrian Chadd 	if (txq->axq_qnum == ATH_TXQ_SWQ)
656eb6f0de0SAdrian Chadd 		ath_tx_handoff_mcast(sc, txq, bf);
657eb6f0de0SAdrian Chadd 	else
658eb6f0de0SAdrian Chadd 		ath_tx_handoff_hw(sc, txq, bf);
659b8e788a5SAdrian Chadd }
660b8e788a5SAdrian Chadd 
66181a82688SAdrian Chadd static int
66281a82688SAdrian Chadd ath_tx_tag_crypto(struct ath_softc *sc, struct ieee80211_node *ni,
663d4365d16SAdrian Chadd     struct mbuf *m0, int iswep, int isfrag, int *hdrlen, int *pktlen,
664d4365d16SAdrian Chadd     int *keyix)
66581a82688SAdrian Chadd {
666*12be5b9cSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_XMIT,
667*12be5b9cSAdrian Chadd 	    "%s: hdrlen=%d, pktlen=%d, isfrag=%d, iswep=%d, m0=%p\n",
668*12be5b9cSAdrian Chadd 	    __func__,
669*12be5b9cSAdrian Chadd 	    *hdrlen,
670*12be5b9cSAdrian Chadd 	    *pktlen,
671*12be5b9cSAdrian Chadd 	    isfrag,
672*12be5b9cSAdrian Chadd 	    iswep,
673*12be5b9cSAdrian Chadd 	    m0);
674*12be5b9cSAdrian Chadd 
67581a82688SAdrian Chadd 	if (iswep) {
67681a82688SAdrian Chadd 		const struct ieee80211_cipher *cip;
67781a82688SAdrian Chadd 		struct ieee80211_key *k;
67881a82688SAdrian Chadd 
67981a82688SAdrian Chadd 		/*
68081a82688SAdrian Chadd 		 * Construct the 802.11 header+trailer for an encrypted
68181a82688SAdrian Chadd 		 * frame. The only reason this can fail is because of an
68281a82688SAdrian Chadd 		 * unknown or unsupported cipher/key type.
68381a82688SAdrian Chadd 		 */
68481a82688SAdrian Chadd 		k = ieee80211_crypto_encap(ni, m0);
68581a82688SAdrian Chadd 		if (k == NULL) {
68681a82688SAdrian Chadd 			/*
68781a82688SAdrian Chadd 			 * This can happen when the key is yanked after the
68881a82688SAdrian Chadd 			 * frame was queued.  Just discard the frame; the
68981a82688SAdrian Chadd 			 * 802.11 layer counts failures and provides
69081a82688SAdrian Chadd 			 * debugging/diagnostics.
69181a82688SAdrian Chadd 			 */
692d4365d16SAdrian Chadd 			return (0);
69381a82688SAdrian Chadd 		}
69481a82688SAdrian Chadd 		/*
69581a82688SAdrian Chadd 		 * Adjust the packet + header lengths for the crypto
69681a82688SAdrian Chadd 		 * additions and calculate the h/w key index.  When
69781a82688SAdrian Chadd 		 * a s/w mic is done the frame will have had any mic
69881a82688SAdrian Chadd 		 * added to it prior to entry so m0->m_pkthdr.len will
69981a82688SAdrian Chadd 		 * account for it. Otherwise we need to add it to the
70081a82688SAdrian Chadd 		 * packet length.
70181a82688SAdrian Chadd 		 */
70281a82688SAdrian Chadd 		cip = k->wk_cipher;
70381a82688SAdrian Chadd 		(*hdrlen) += cip->ic_header;
70481a82688SAdrian Chadd 		(*pktlen) += cip->ic_header + cip->ic_trailer;
70581a82688SAdrian Chadd 		/* NB: frags always have any TKIP MIC done in s/w */
70681a82688SAdrian Chadd 		if ((k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && !isfrag)
70781a82688SAdrian Chadd 			(*pktlen) += cip->ic_miclen;
70881a82688SAdrian Chadd 		(*keyix) = k->wk_keyix;
70981a82688SAdrian Chadd 	} else if (ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) {
71081a82688SAdrian Chadd 		/*
71181a82688SAdrian Chadd 		 * Use station key cache slot, if assigned.
71281a82688SAdrian Chadd 		 */
71381a82688SAdrian Chadd 		(*keyix) = ni->ni_ucastkey.wk_keyix;
71481a82688SAdrian Chadd 		if ((*keyix) == IEEE80211_KEYIX_NONE)
71581a82688SAdrian Chadd 			(*keyix) = HAL_TXKEYIX_INVALID;
71681a82688SAdrian Chadd 	} else
71781a82688SAdrian Chadd 		(*keyix) = HAL_TXKEYIX_INVALID;
71881a82688SAdrian Chadd 
719d4365d16SAdrian Chadd 	return (1);
72081a82688SAdrian Chadd }
72181a82688SAdrian Chadd 
722e42b5dbaSAdrian Chadd static uint8_t
723e42b5dbaSAdrian Chadd ath_tx_get_rtscts_rate(struct ath_hal *ah, const HAL_RATE_TABLE *rt,
724eb6f0de0SAdrian Chadd     int cix, int shortPreamble)
72579f02dbfSAdrian Chadd {
726e42b5dbaSAdrian Chadd 	uint8_t ctsrate;
727e42b5dbaSAdrian Chadd 
72879f02dbfSAdrian Chadd 	/*
72979f02dbfSAdrian Chadd 	 * CTS transmit rate is derived from the transmit rate
73079f02dbfSAdrian Chadd 	 * by looking in the h/w rate table.  We must also factor
73179f02dbfSAdrian Chadd 	 * in whether or not a short preamble is to be used.
73279f02dbfSAdrian Chadd 	 */
73379f02dbfSAdrian Chadd 	/* NB: cix is set above where RTS/CTS is enabled */
73479f02dbfSAdrian Chadd 	KASSERT(cix != 0xff, ("cix not setup"));
735e42b5dbaSAdrian Chadd 	ctsrate = rt->info[cix].rateCode;
736e42b5dbaSAdrian Chadd 
737e42b5dbaSAdrian Chadd 	/* XXX this should only matter for legacy rates */
738e42b5dbaSAdrian Chadd 	if (shortPreamble)
739e42b5dbaSAdrian Chadd 		ctsrate |= rt->info[cix].shortPreamble;
740e42b5dbaSAdrian Chadd 
741d4365d16SAdrian Chadd 	return (ctsrate);
742e42b5dbaSAdrian Chadd }
743e42b5dbaSAdrian Chadd 
744e42b5dbaSAdrian Chadd /*
745e42b5dbaSAdrian Chadd  * Calculate the RTS/CTS duration for legacy frames.
746e42b5dbaSAdrian Chadd  */
747e42b5dbaSAdrian Chadd static int
748e42b5dbaSAdrian Chadd ath_tx_calc_ctsduration(struct ath_hal *ah, int rix, int cix,
749e42b5dbaSAdrian Chadd     int shortPreamble, int pktlen, const HAL_RATE_TABLE *rt,
750e42b5dbaSAdrian Chadd     int flags)
751e42b5dbaSAdrian Chadd {
752e42b5dbaSAdrian Chadd 	int ctsduration = 0;
753e42b5dbaSAdrian Chadd 
754e42b5dbaSAdrian Chadd 	/* This mustn't be called for HT modes */
755e42b5dbaSAdrian Chadd 	if (rt->info[cix].phy == IEEE80211_T_HT) {
756e42b5dbaSAdrian Chadd 		printf("%s: HT rate where it shouldn't be (0x%x)\n",
757e42b5dbaSAdrian Chadd 		    __func__, rt->info[cix].rateCode);
758d4365d16SAdrian Chadd 		return (-1);
759e42b5dbaSAdrian Chadd 	}
760e42b5dbaSAdrian Chadd 
76179f02dbfSAdrian Chadd 	/*
76279f02dbfSAdrian Chadd 	 * Compute the transmit duration based on the frame
76379f02dbfSAdrian Chadd 	 * size and the size of an ACK frame.  We call into the
76479f02dbfSAdrian Chadd 	 * HAL to do the computation since it depends on the
76579f02dbfSAdrian Chadd 	 * characteristics of the actual PHY being used.
76679f02dbfSAdrian Chadd 	 *
76779f02dbfSAdrian Chadd 	 * NB: CTS is assumed the same size as an ACK so we can
76879f02dbfSAdrian Chadd 	 *     use the precalculated ACK durations.
76979f02dbfSAdrian Chadd 	 */
77079f02dbfSAdrian Chadd 	if (shortPreamble) {
77179f02dbfSAdrian Chadd 		if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
772e42b5dbaSAdrian Chadd 			ctsduration += rt->info[cix].spAckDuration;
773e42b5dbaSAdrian Chadd 		ctsduration += ath_hal_computetxtime(ah,
77479f02dbfSAdrian Chadd 			rt, pktlen, rix, AH_TRUE);
77579f02dbfSAdrian Chadd 		if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
776e42b5dbaSAdrian Chadd 			ctsduration += rt->info[rix].spAckDuration;
77779f02dbfSAdrian Chadd 	} else {
77879f02dbfSAdrian Chadd 		if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
779e42b5dbaSAdrian Chadd 			ctsduration += rt->info[cix].lpAckDuration;
780e42b5dbaSAdrian Chadd 		ctsduration += ath_hal_computetxtime(ah,
78179f02dbfSAdrian Chadd 			rt, pktlen, rix, AH_FALSE);
78279f02dbfSAdrian Chadd 		if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
783e42b5dbaSAdrian Chadd 			ctsduration += rt->info[rix].lpAckDuration;
78479f02dbfSAdrian Chadd 	}
785e42b5dbaSAdrian Chadd 
786d4365d16SAdrian Chadd 	return (ctsduration);
78779f02dbfSAdrian Chadd }
78879f02dbfSAdrian Chadd 
789eb6f0de0SAdrian Chadd /*
790eb6f0de0SAdrian Chadd  * Update the given ath_buf with updated rts/cts setup and duration
791eb6f0de0SAdrian Chadd  * values.
792eb6f0de0SAdrian Chadd  *
793eb6f0de0SAdrian Chadd  * To support rate lookups for each software retry, the rts/cts rate
794eb6f0de0SAdrian Chadd  * and cts duration must be re-calculated.
795eb6f0de0SAdrian Chadd  *
796eb6f0de0SAdrian Chadd  * This function assumes the RTS/CTS flags have been set as needed;
797eb6f0de0SAdrian Chadd  * mrr has been disabled; and the rate control lookup has been done.
798eb6f0de0SAdrian Chadd  *
799eb6f0de0SAdrian Chadd  * XXX TODO: MRR need only be disabled for the pre-11n NICs.
800eb6f0de0SAdrian Chadd  * XXX The 11n NICs support per-rate RTS/CTS configuration.
801eb6f0de0SAdrian Chadd  */
802eb6f0de0SAdrian Chadd static void
803eb6f0de0SAdrian Chadd ath_tx_set_rtscts(struct ath_softc *sc, struct ath_buf *bf)
804eb6f0de0SAdrian Chadd {
805eb6f0de0SAdrian Chadd 	uint16_t ctsduration = 0;
806eb6f0de0SAdrian Chadd 	uint8_t ctsrate = 0;
807eb6f0de0SAdrian Chadd 	uint8_t rix = bf->bf_state.bfs_rc[0].rix;
808eb6f0de0SAdrian Chadd 	uint8_t cix = 0;
809eb6f0de0SAdrian Chadd 	const HAL_RATE_TABLE *rt = sc->sc_currates;
810eb6f0de0SAdrian Chadd 
811eb6f0de0SAdrian Chadd 	/*
812eb6f0de0SAdrian Chadd 	 * No RTS/CTS enabled? Don't bother.
813eb6f0de0SAdrian Chadd 	 */
814eb6f0de0SAdrian Chadd 	if ((bf->bf_state.bfs_flags &
815eb6f0de0SAdrian Chadd 	    (HAL_TXDESC_RTSENA | HAL_TXDESC_CTSENA)) == 0) {
816eb6f0de0SAdrian Chadd 		/* XXX is this really needed? */
817eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_ctsrate = 0;
818eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_ctsduration = 0;
819eb6f0de0SAdrian Chadd 		return;
820eb6f0de0SAdrian Chadd 	}
821eb6f0de0SAdrian Chadd 
822eb6f0de0SAdrian Chadd 	/*
823eb6f0de0SAdrian Chadd 	 * If protection is enabled, use the protection rix control
824eb6f0de0SAdrian Chadd 	 * rate. Otherwise use the rate0 control rate.
825eb6f0de0SAdrian Chadd 	 */
826eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_doprot)
827eb6f0de0SAdrian Chadd 		rix = sc->sc_protrix;
828eb6f0de0SAdrian Chadd 	else
829eb6f0de0SAdrian Chadd 		rix = bf->bf_state.bfs_rc[0].rix;
830eb6f0de0SAdrian Chadd 
831eb6f0de0SAdrian Chadd 	/*
832eb6f0de0SAdrian Chadd 	 * If the raw path has hard-coded ctsrate0 to something,
833eb6f0de0SAdrian Chadd 	 * use it.
834eb6f0de0SAdrian Chadd 	 */
835eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_ctsrate0 != 0)
836eb6f0de0SAdrian Chadd 		cix = ath_tx_findrix(sc, bf->bf_state.bfs_ctsrate0);
837eb6f0de0SAdrian Chadd 	else
838eb6f0de0SAdrian Chadd 		/* Control rate from above */
839eb6f0de0SAdrian Chadd 		cix = rt->info[rix].controlRate;
840eb6f0de0SAdrian Chadd 
841eb6f0de0SAdrian Chadd 	/* Calculate the rtscts rate for the given cix */
842eb6f0de0SAdrian Chadd 	ctsrate = ath_tx_get_rtscts_rate(sc->sc_ah, rt, cix,
843eb6f0de0SAdrian Chadd 	    bf->bf_state.bfs_shpream);
844eb6f0de0SAdrian Chadd 
845eb6f0de0SAdrian Chadd 	/* The 11n chipsets do ctsduration calculations for you */
846eb6f0de0SAdrian Chadd 	if (! ath_tx_is_11n(sc))
847eb6f0de0SAdrian Chadd 		ctsduration = ath_tx_calc_ctsduration(sc->sc_ah, rix, cix,
848eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_shpream, bf->bf_state.bfs_pktlen,
849eb6f0de0SAdrian Chadd 		    rt, bf->bf_state.bfs_flags);
850eb6f0de0SAdrian Chadd 
851eb6f0de0SAdrian Chadd 	/* Squirrel away in ath_buf */
852eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsrate = ctsrate;
853eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsduration = ctsduration;
854eb6f0de0SAdrian Chadd 
855eb6f0de0SAdrian Chadd 	/*
856eb6f0de0SAdrian Chadd 	 * Must disable multi-rate retry when using RTS/CTS.
857eb6f0de0SAdrian Chadd 	 * XXX TODO: only for pre-11n NICs.
858eb6f0de0SAdrian Chadd 	 */
859eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ismrr = 0;
860eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_try0 =
861eb6f0de0SAdrian Chadd 	    bf->bf_state.bfs_rc[0].tries = ATH_TXMGTTRY;	/* XXX ew */
862eb6f0de0SAdrian Chadd }
863eb6f0de0SAdrian Chadd 
864eb6f0de0SAdrian Chadd /*
865eb6f0de0SAdrian Chadd  * Setup the descriptor chain for a normal or fast-frame
866eb6f0de0SAdrian Chadd  * frame.
867eb6f0de0SAdrian Chadd  */
868eb6f0de0SAdrian Chadd static void
869eb6f0de0SAdrian Chadd ath_tx_setds(struct ath_softc *sc, struct ath_buf *bf)
870eb6f0de0SAdrian Chadd {
871eb6f0de0SAdrian Chadd 	struct ath_desc *ds = bf->bf_desc;
872eb6f0de0SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
873eb6f0de0SAdrian Chadd 
874eb6f0de0SAdrian Chadd 	ath_hal_setuptxdesc(ah, ds
875eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_pktlen	/* packet length */
876eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_hdrlen	/* header length */
877eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_atype	/* Atheros packet type */
878eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_txpower	/* txpower */
879eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_txrate0
880eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_try0		/* series 0 rate/tries */
881eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_keyix	/* key cache index */
882eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_txantenna	/* antenna mode */
883eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_flags	/* flags */
884eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_ctsrate	/* rts/cts rate */
885eb6f0de0SAdrian Chadd 		, bf->bf_state.bfs_ctsduration	/* rts/cts duration */
886eb6f0de0SAdrian Chadd 	);
887eb6f0de0SAdrian Chadd 
888eb6f0de0SAdrian Chadd 	/*
889eb6f0de0SAdrian Chadd 	 * This will be overriden when the descriptor chain is written.
890eb6f0de0SAdrian Chadd 	 */
891eb6f0de0SAdrian Chadd 	bf->bf_lastds = ds;
892eb6f0de0SAdrian Chadd 	bf->bf_last = bf;
893eb6f0de0SAdrian Chadd 
894eb6f0de0SAdrian Chadd 	/* XXX TODO: Setup descriptor chain */
895eb6f0de0SAdrian Chadd }
896eb6f0de0SAdrian Chadd 
897eb6f0de0SAdrian Chadd /*
898eb6f0de0SAdrian Chadd  * Do a rate lookup.
899eb6f0de0SAdrian Chadd  *
900eb6f0de0SAdrian Chadd  * This performs a rate lookup for the given ath_buf only if it's required.
901eb6f0de0SAdrian Chadd  * Non-data frames and raw frames don't require it.
902eb6f0de0SAdrian Chadd  *
903eb6f0de0SAdrian Chadd  * This populates the primary and MRR entries; MRR values are
904eb6f0de0SAdrian Chadd  * then disabled later on if something requires it (eg RTS/CTS on
905eb6f0de0SAdrian Chadd  * pre-11n chipsets.
906eb6f0de0SAdrian Chadd  *
907eb6f0de0SAdrian Chadd  * This needs to be done before the RTS/CTS fields are calculated
908eb6f0de0SAdrian Chadd  * as they may depend upon the rate chosen.
909eb6f0de0SAdrian Chadd  */
910eb6f0de0SAdrian Chadd static void
911eb6f0de0SAdrian Chadd ath_tx_do_ratelookup(struct ath_softc *sc, struct ath_buf *bf)
912eb6f0de0SAdrian Chadd {
913eb6f0de0SAdrian Chadd 	uint8_t rate, rix;
914eb6f0de0SAdrian Chadd 	int try0;
915eb6f0de0SAdrian Chadd 
916eb6f0de0SAdrian Chadd 	if (! bf->bf_state.bfs_doratelookup)
917eb6f0de0SAdrian Chadd 		return;
918eb6f0de0SAdrian Chadd 
919eb6f0de0SAdrian Chadd 	/* Get rid of any previous state */
920eb6f0de0SAdrian Chadd 	bzero(bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
921eb6f0de0SAdrian Chadd 
922eb6f0de0SAdrian Chadd 	ATH_NODE_LOCK(ATH_NODE(bf->bf_node));
923eb6f0de0SAdrian Chadd 	ath_rate_findrate(sc, ATH_NODE(bf->bf_node), bf->bf_state.bfs_shpream,
924eb6f0de0SAdrian Chadd 	    bf->bf_state.bfs_pktlen, &rix, &try0, &rate);
925eb6f0de0SAdrian Chadd 
926eb6f0de0SAdrian Chadd 	/* In case MRR is disabled, make sure rc[0] is setup correctly */
927eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].rix = rix;
928eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].ratecode = rate;
929eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].tries = try0;
930eb6f0de0SAdrian Chadd 
931eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_ismrr && try0 != ATH_TXMAXTRY)
932eb6f0de0SAdrian Chadd 		ath_rate_getxtxrates(sc, ATH_NODE(bf->bf_node), rix,
933eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_rc);
934eb6f0de0SAdrian Chadd 	ATH_NODE_UNLOCK(ATH_NODE(bf->bf_node));
935eb6f0de0SAdrian Chadd 
936eb6f0de0SAdrian Chadd 	sc->sc_txrix = rix;	/* for LED blinking */
937eb6f0de0SAdrian Chadd 	sc->sc_lastdatarix = rix;	/* for fast frames */
938eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_try0 = try0;
939eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txrate0 = rate;
940eb6f0de0SAdrian Chadd }
941eb6f0de0SAdrian Chadd 
942eb6f0de0SAdrian Chadd /*
943eb6f0de0SAdrian Chadd  * Set the rate control fields in the given descriptor based on
944eb6f0de0SAdrian Chadd  * the bf_state fields and node state.
945eb6f0de0SAdrian Chadd  *
946eb6f0de0SAdrian Chadd  * The bfs fields should already be set with the relevant rate
947eb6f0de0SAdrian Chadd  * control information, including whether MRR is to be enabled.
948eb6f0de0SAdrian Chadd  *
949eb6f0de0SAdrian Chadd  * Since the FreeBSD HAL currently sets up the first TX rate
950eb6f0de0SAdrian Chadd  * in ath_hal_setuptxdesc(), this will setup the MRR
951eb6f0de0SAdrian Chadd  * conditionally for the pre-11n chips, and call ath_buf_set_rate
952eb6f0de0SAdrian Chadd  * unconditionally for 11n chips. These require the 11n rate
953eb6f0de0SAdrian Chadd  * scenario to be set if MCS rates are enabled, so it's easier
954eb6f0de0SAdrian Chadd  * to just always call it. The caller can then only set rates 2, 3
955eb6f0de0SAdrian Chadd  * and 4 if multi-rate retry is needed.
956eb6f0de0SAdrian Chadd  */
957eb6f0de0SAdrian Chadd static void
958eb6f0de0SAdrian Chadd ath_tx_set_ratectrl(struct ath_softc *sc, struct ieee80211_node *ni,
959eb6f0de0SAdrian Chadd     struct ath_buf *bf)
960eb6f0de0SAdrian Chadd {
961eb6f0de0SAdrian Chadd 	struct ath_rc_series *rc = bf->bf_state.bfs_rc;
962eb6f0de0SAdrian Chadd 
963eb6f0de0SAdrian Chadd 	/* If mrr is disabled, blank tries 1, 2, 3 */
964eb6f0de0SAdrian Chadd 	if (! bf->bf_state.bfs_ismrr)
965eb6f0de0SAdrian Chadd 		rc[1].tries = rc[2].tries = rc[3].tries = 0;
966eb6f0de0SAdrian Chadd 
967eb6f0de0SAdrian Chadd 	/*
968eb6f0de0SAdrian Chadd 	 * Always call - that way a retried descriptor will
969eb6f0de0SAdrian Chadd 	 * have the MRR fields overwritten.
970eb6f0de0SAdrian Chadd 	 *
971eb6f0de0SAdrian Chadd 	 * XXX TODO: see if this is really needed - setting up
972eb6f0de0SAdrian Chadd 	 * the first descriptor should set the MRR fields to 0
973eb6f0de0SAdrian Chadd 	 * for us anyway.
974eb6f0de0SAdrian Chadd 	 */
975eb6f0de0SAdrian Chadd 	if (ath_tx_is_11n(sc)) {
976eb6f0de0SAdrian Chadd 		ath_buf_set_rate(sc, ni, bf);
977eb6f0de0SAdrian Chadd 	} else {
978eb6f0de0SAdrian Chadd 		ath_hal_setupxtxdesc(sc->sc_ah, bf->bf_desc
979eb6f0de0SAdrian Chadd 			, rc[1].ratecode, rc[1].tries
980eb6f0de0SAdrian Chadd 			, rc[2].ratecode, rc[2].tries
981eb6f0de0SAdrian Chadd 			, rc[3].ratecode, rc[3].tries
982eb6f0de0SAdrian Chadd 		);
983eb6f0de0SAdrian Chadd 	}
984eb6f0de0SAdrian Chadd }
985eb6f0de0SAdrian Chadd 
986eb6f0de0SAdrian Chadd /*
987eb6f0de0SAdrian Chadd  * Transmit the given frame to the hardware.
988eb6f0de0SAdrian Chadd  *
989eb6f0de0SAdrian Chadd  * The frame must already be setup; rate control must already have
990eb6f0de0SAdrian Chadd  * been done.
991eb6f0de0SAdrian Chadd  *
992eb6f0de0SAdrian Chadd  * XXX since the TXQ lock is being held here (and I dislike holding
993eb6f0de0SAdrian Chadd  * it for this long when not doing software aggregation), later on
994eb6f0de0SAdrian Chadd  * break this function into "setup_normal" and "xmit_normal". The
995eb6f0de0SAdrian Chadd  * lock only needs to be held for the ath_tx_handoff call.
996eb6f0de0SAdrian Chadd  */
997eb6f0de0SAdrian Chadd static void
998eb6f0de0SAdrian Chadd ath_tx_xmit_normal(struct ath_softc *sc, struct ath_txq *txq,
999eb6f0de0SAdrian Chadd     struct ath_buf *bf)
1000eb6f0de0SAdrian Chadd {
1001eb6f0de0SAdrian Chadd 
1002eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
1003eb6f0de0SAdrian Chadd 
1004eb6f0de0SAdrian Chadd 	/* Setup the descriptor before handoff */
1005eb6f0de0SAdrian Chadd 	ath_tx_do_ratelookup(sc, bf);
1006eb6f0de0SAdrian Chadd 	ath_tx_rate_fill_rcflags(sc, bf);
1007eb6f0de0SAdrian Chadd 	ath_tx_set_rtscts(sc, bf);
1008eb6f0de0SAdrian Chadd 	ath_tx_setds(sc, bf);
1009eb6f0de0SAdrian Chadd 	ath_tx_set_ratectrl(sc, bf->bf_node, bf);
1010eb6f0de0SAdrian Chadd 	ath_tx_chaindesclist(sc, bf);
1011eb6f0de0SAdrian Chadd 
1012eb6f0de0SAdrian Chadd 	/* Hand off to hardware */
1013eb6f0de0SAdrian Chadd 	ath_tx_handoff(sc, txq, bf);
1014eb6f0de0SAdrian Chadd }
1015eb6f0de0SAdrian Chadd 
1016eb6f0de0SAdrian Chadd 
1017eb6f0de0SAdrian Chadd 
1018eb6f0de0SAdrian Chadd static int
1019eb6f0de0SAdrian Chadd ath_tx_normal_setup(struct ath_softc *sc, struct ieee80211_node *ni,
1020eb6f0de0SAdrian Chadd     struct ath_buf *bf, struct mbuf *m0)
1021b8e788a5SAdrian Chadd {
1022b8e788a5SAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
1023b8e788a5SAdrian Chadd 	struct ath_vap *avp = ATH_VAP(vap);
1024b8e788a5SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
1025b8e788a5SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
1026b8e788a5SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
1027b8e788a5SAdrian Chadd 	const struct chanAccParams *cap = &ic->ic_wme.wme_chanParams;
1028b8e788a5SAdrian Chadd 	int error, iswep, ismcast, isfrag, ismrr;
1029eb6f0de0SAdrian Chadd 	int keyix, hdrlen, pktlen, try0 = 0;
1030eb6f0de0SAdrian Chadd 	u_int8_t rix = 0, txrate = 0;
1031b8e788a5SAdrian Chadd 	struct ath_desc *ds;
1032b8e788a5SAdrian Chadd 	struct ath_txq *txq;
1033b8e788a5SAdrian Chadd 	struct ieee80211_frame *wh;
1034eb6f0de0SAdrian Chadd 	u_int subtype, flags;
1035b8e788a5SAdrian Chadd 	HAL_PKT_TYPE atype;
1036b8e788a5SAdrian Chadd 	const HAL_RATE_TABLE *rt;
1037b8e788a5SAdrian Chadd 	HAL_BOOL shortPreamble;
1038b8e788a5SAdrian Chadd 	struct ath_node *an;
1039b8e788a5SAdrian Chadd 	u_int pri;
1040b8e788a5SAdrian Chadd 
1041b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1042b8e788a5SAdrian Chadd 	iswep = wh->i_fc[1] & IEEE80211_FC1_WEP;
1043b8e788a5SAdrian Chadd 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1044b8e788a5SAdrian Chadd 	isfrag = m0->m_flags & M_FRAG;
1045b8e788a5SAdrian Chadd 	hdrlen = ieee80211_anyhdrsize(wh);
1046b8e788a5SAdrian Chadd 	/*
1047b8e788a5SAdrian Chadd 	 * Packet length must not include any
1048b8e788a5SAdrian Chadd 	 * pad bytes; deduct them here.
1049b8e788a5SAdrian Chadd 	 */
1050b8e788a5SAdrian Chadd 	pktlen = m0->m_pkthdr.len - (hdrlen & 3);
1051b8e788a5SAdrian Chadd 
105281a82688SAdrian Chadd 	/* Handle encryption twiddling if needed */
1053eb6f0de0SAdrian Chadd 	if (! ath_tx_tag_crypto(sc, ni, m0, iswep, isfrag, &hdrlen,
1054eb6f0de0SAdrian Chadd 	    &pktlen, &keyix)) {
1055b8e788a5SAdrian Chadd 		ath_freetx(m0);
1056b8e788a5SAdrian Chadd 		return EIO;
1057b8e788a5SAdrian Chadd 	}
1058b8e788a5SAdrian Chadd 
1059b8e788a5SAdrian Chadd 	/* packet header may have moved, reset our local pointer */
1060b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1061b8e788a5SAdrian Chadd 
1062b8e788a5SAdrian Chadd 	pktlen += IEEE80211_CRC_LEN;
1063b8e788a5SAdrian Chadd 
1064b8e788a5SAdrian Chadd 	/*
1065b8e788a5SAdrian Chadd 	 * Load the DMA map so any coalescing is done.  This
1066b8e788a5SAdrian Chadd 	 * also calculates the number of descriptors we need.
1067b8e788a5SAdrian Chadd 	 */
1068b8e788a5SAdrian Chadd 	error = ath_tx_dmasetup(sc, bf, m0);
1069b8e788a5SAdrian Chadd 	if (error != 0)
1070b8e788a5SAdrian Chadd 		return error;
1071b8e788a5SAdrian Chadd 	bf->bf_node = ni;			/* NB: held reference */
1072b8e788a5SAdrian Chadd 	m0 = bf->bf_m;				/* NB: may have changed */
1073b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1074b8e788a5SAdrian Chadd 
1075b8e788a5SAdrian Chadd 	/* setup descriptors */
1076b8e788a5SAdrian Chadd 	ds = bf->bf_desc;
1077b8e788a5SAdrian Chadd 	rt = sc->sc_currates;
1078b8e788a5SAdrian Chadd 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
1079b8e788a5SAdrian Chadd 
1080b8e788a5SAdrian Chadd 	/*
1081b8e788a5SAdrian Chadd 	 * NB: the 802.11 layer marks whether or not we should
1082b8e788a5SAdrian Chadd 	 * use short preamble based on the current mode and
1083b8e788a5SAdrian Chadd 	 * negotiated parameters.
1084b8e788a5SAdrian Chadd 	 */
1085b8e788a5SAdrian Chadd 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1086b8e788a5SAdrian Chadd 	    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
1087b8e788a5SAdrian Chadd 		shortPreamble = AH_TRUE;
1088b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_shortpre++;
1089b8e788a5SAdrian Chadd 	} else {
1090b8e788a5SAdrian Chadd 		shortPreamble = AH_FALSE;
1091b8e788a5SAdrian Chadd 	}
1092b8e788a5SAdrian Chadd 
1093b8e788a5SAdrian Chadd 	an = ATH_NODE(ni);
1094b8e788a5SAdrian Chadd 	flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for crypto errs */
1095b8e788a5SAdrian Chadd 	ismrr = 0;				/* default no multi-rate retry*/
1096b8e788a5SAdrian Chadd 	pri = M_WME_GETAC(m0);			/* honor classification */
1097b8e788a5SAdrian Chadd 	/* XXX use txparams instead of fixed values */
1098b8e788a5SAdrian Chadd 	/*
1099b8e788a5SAdrian Chadd 	 * Calculate Atheros packet type from IEEE80211 packet header,
1100b8e788a5SAdrian Chadd 	 * setup for rate calculations, and select h/w transmit queue.
1101b8e788a5SAdrian Chadd 	 */
1102b8e788a5SAdrian Chadd 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1103b8e788a5SAdrian Chadd 	case IEEE80211_FC0_TYPE_MGT:
1104b8e788a5SAdrian Chadd 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1105b8e788a5SAdrian Chadd 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON)
1106b8e788a5SAdrian Chadd 			atype = HAL_PKT_TYPE_BEACON;
1107b8e788a5SAdrian Chadd 		else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1108b8e788a5SAdrian Chadd 			atype = HAL_PKT_TYPE_PROBE_RESP;
1109b8e788a5SAdrian Chadd 		else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM)
1110b8e788a5SAdrian Chadd 			atype = HAL_PKT_TYPE_ATIM;
1111b8e788a5SAdrian Chadd 		else
1112b8e788a5SAdrian Chadd 			atype = HAL_PKT_TYPE_NORMAL;	/* XXX */
1113b8e788a5SAdrian Chadd 		rix = an->an_mgmtrix;
1114b8e788a5SAdrian Chadd 		txrate = rt->info[rix].rateCode;
1115b8e788a5SAdrian Chadd 		if (shortPreamble)
1116b8e788a5SAdrian Chadd 			txrate |= rt->info[rix].shortPreamble;
1117b8e788a5SAdrian Chadd 		try0 = ATH_TXMGTTRY;
1118b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_INTREQ;	/* force interrupt */
1119b8e788a5SAdrian Chadd 		break;
1120b8e788a5SAdrian Chadd 	case IEEE80211_FC0_TYPE_CTL:
1121b8e788a5SAdrian Chadd 		atype = HAL_PKT_TYPE_PSPOLL;	/* stop setting of duration */
1122b8e788a5SAdrian Chadd 		rix = an->an_mgmtrix;
1123b8e788a5SAdrian Chadd 		txrate = rt->info[rix].rateCode;
1124b8e788a5SAdrian Chadd 		if (shortPreamble)
1125b8e788a5SAdrian Chadd 			txrate |= rt->info[rix].shortPreamble;
1126b8e788a5SAdrian Chadd 		try0 = ATH_TXMGTTRY;
1127b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_INTREQ;	/* force interrupt */
1128b8e788a5SAdrian Chadd 		break;
1129b8e788a5SAdrian Chadd 	case IEEE80211_FC0_TYPE_DATA:
1130b8e788a5SAdrian Chadd 		atype = HAL_PKT_TYPE_NORMAL;		/* default */
1131b8e788a5SAdrian Chadd 		/*
1132b8e788a5SAdrian Chadd 		 * Data frames: multicast frames go out at a fixed rate,
1133b8e788a5SAdrian Chadd 		 * EAPOL frames use the mgmt frame rate; otherwise consult
1134b8e788a5SAdrian Chadd 		 * the rate control module for the rate to use.
1135b8e788a5SAdrian Chadd 		 */
1136b8e788a5SAdrian Chadd 		if (ismcast) {
1137b8e788a5SAdrian Chadd 			rix = an->an_mcastrix;
1138b8e788a5SAdrian Chadd 			txrate = rt->info[rix].rateCode;
1139b8e788a5SAdrian Chadd 			if (shortPreamble)
1140b8e788a5SAdrian Chadd 				txrate |= rt->info[rix].shortPreamble;
1141b8e788a5SAdrian Chadd 			try0 = 1;
1142b8e788a5SAdrian Chadd 		} else if (m0->m_flags & M_EAPOL) {
1143b8e788a5SAdrian Chadd 			/* XXX? maybe always use long preamble? */
1144b8e788a5SAdrian Chadd 			rix = an->an_mgmtrix;
1145b8e788a5SAdrian Chadd 			txrate = rt->info[rix].rateCode;
1146b8e788a5SAdrian Chadd 			if (shortPreamble)
1147b8e788a5SAdrian Chadd 				txrate |= rt->info[rix].shortPreamble;
1148b8e788a5SAdrian Chadd 			try0 = ATH_TXMAXTRY;	/* XXX?too many? */
1149b8e788a5SAdrian Chadd 		} else {
1150eb6f0de0SAdrian Chadd 			/*
1151eb6f0de0SAdrian Chadd 			 * Do rate lookup on each TX, rather than using
1152eb6f0de0SAdrian Chadd 			 * the hard-coded TX information decided here.
1153eb6f0de0SAdrian Chadd 			 */
1154b8e788a5SAdrian Chadd 			ismrr = 1;
1155eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_doratelookup = 1;
1156b8e788a5SAdrian Chadd 		}
1157b8e788a5SAdrian Chadd 		if (cap->cap_wmeParams[pri].wmep_noackPolicy)
1158b8e788a5SAdrian Chadd 			flags |= HAL_TXDESC_NOACK;
1159b8e788a5SAdrian Chadd 		break;
1160b8e788a5SAdrian Chadd 	default:
1161b8e788a5SAdrian Chadd 		if_printf(ifp, "bogus frame type 0x%x (%s)\n",
1162b8e788a5SAdrian Chadd 			wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__);
1163b8e788a5SAdrian Chadd 		/* XXX statistic */
1164b8e788a5SAdrian Chadd 		ath_freetx(m0);
1165b8e788a5SAdrian Chadd 		return EIO;
1166b8e788a5SAdrian Chadd 	}
1167b8e788a5SAdrian Chadd 	txq = sc->sc_ac2q[pri];
1168b8e788a5SAdrian Chadd 
1169b8e788a5SAdrian Chadd 	/*
1170b8e788a5SAdrian Chadd 	 * When servicing one or more stations in power-save mode
1171b8e788a5SAdrian Chadd 	 * (or) if there is some mcast data waiting on the mcast
1172b8e788a5SAdrian Chadd 	 * queue (to prevent out of order delivery) multicast
1173b8e788a5SAdrian Chadd 	 * frames must be buffered until after the beacon.
1174c5940c30SAdrian Chadd 	 *
1175c5940c30SAdrian Chadd 	 * XXX This likely means that if there's a station in power
1176c5940c30SAdrian Chadd 	 * save mode, we won't be doing any kind of aggregation towards
1177c5940c30SAdrian Chadd 	 * anyone.  This is likely a very suboptimal way of dealing
1178c5940c30SAdrian Chadd 	 * with things.
1179b8e788a5SAdrian Chadd 	 */
1180b8e788a5SAdrian Chadd 	if (ismcast && (vap->iv_ps_sta || avp->av_mcastq.axq_depth))
1181b8e788a5SAdrian Chadd 		txq = &avp->av_mcastq;
1182b8e788a5SAdrian Chadd 
1183b8e788a5SAdrian Chadd 	/*
1184b8e788a5SAdrian Chadd 	 * Calculate miscellaneous flags.
1185b8e788a5SAdrian Chadd 	 */
1186b8e788a5SAdrian Chadd 	if (ismcast) {
1187b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_NOACK;	/* no ack on broad/multicast */
1188b8e788a5SAdrian Chadd 	} else if (pktlen > vap->iv_rtsthreshold &&
1189b8e788a5SAdrian Chadd 	    (ni->ni_ath_flags & IEEE80211_NODE_FF) == 0) {
1190b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_RTSENA;	/* RTS based on frame length */
1191b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_rts++;
1192b8e788a5SAdrian Chadd 	}
1193b8e788a5SAdrian Chadd 	if (flags & HAL_TXDESC_NOACK)		/* NB: avoid double counting */
1194b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_noack++;
1195b8e788a5SAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
1196b8e788a5SAdrian Chadd 	if (sc->sc_tdma && (flags & HAL_TXDESC_NOACK) == 0) {
1197b8e788a5SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_TDMA,
1198b8e788a5SAdrian Chadd 		    "%s: discard frame, ACK required w/ TDMA\n", __func__);
1199b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tdma_ack++;
1200b8e788a5SAdrian Chadd 		ath_freetx(m0);
1201b8e788a5SAdrian Chadd 		return EIO;
1202b8e788a5SAdrian Chadd 	}
1203b8e788a5SAdrian Chadd #endif
1204b8e788a5SAdrian Chadd 
1205b8e788a5SAdrian Chadd 	/*
1206b8e788a5SAdrian Chadd 	 * If 802.11g protection is enabled, determine whether
1207b8e788a5SAdrian Chadd 	 * to use RTS/CTS or just CTS.  Note that this is only
1208b8e788a5SAdrian Chadd 	 * done for OFDM unicast frames.
1209b8e788a5SAdrian Chadd 	 */
1210b8e788a5SAdrian Chadd 	if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1211b8e788a5SAdrian Chadd 	    rt->info[rix].phy == IEEE80211_T_OFDM &&
1212b8e788a5SAdrian Chadd 	    (flags & HAL_TXDESC_NOACK) == 0) {
1213eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_doprot = 1;
1214b8e788a5SAdrian Chadd 		/* XXX fragments must use CCK rates w/ protection */
1215eb6f0de0SAdrian Chadd 		if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) {
1216b8e788a5SAdrian Chadd 			flags |= HAL_TXDESC_RTSENA;
1217eb6f0de0SAdrian Chadd 		} else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) {
1218b8e788a5SAdrian Chadd 			flags |= HAL_TXDESC_CTSENA;
1219eb6f0de0SAdrian Chadd 		}
1220b8e788a5SAdrian Chadd 		/*
1221b8e788a5SAdrian Chadd 		 * For frags it would be desirable to use the
1222b8e788a5SAdrian Chadd 		 * highest CCK rate for RTS/CTS.  But stations
1223b8e788a5SAdrian Chadd 		 * farther away may detect it at a lower CCK rate
1224b8e788a5SAdrian Chadd 		 * so use the configured protection rate instead
1225b8e788a5SAdrian Chadd 		 * (for now).
1226b8e788a5SAdrian Chadd 		 */
1227b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_protect++;
1228b8e788a5SAdrian Chadd 	}
1229b8e788a5SAdrian Chadd 
12304f545a2cSAdrian Chadd #if 0
12314f545a2cSAdrian Chadd 	/*
12324f545a2cSAdrian Chadd 	 * If 11n protection is enabled and it's a HT frame,
12334f545a2cSAdrian Chadd 	 * enable RTS.
12344f545a2cSAdrian Chadd 	 *
12354f545a2cSAdrian Chadd 	 * XXX ic_htprotmode or ic_curhtprotmode?
12364f545a2cSAdrian Chadd 	 * XXX should it_htprotmode only matter if ic_curhtprotmode
12374f545a2cSAdrian Chadd 	 * XXX indicates it's not a HT pure environment?
12384f545a2cSAdrian Chadd 	 */
12394f545a2cSAdrian Chadd 	if ((ic->ic_htprotmode == IEEE80211_PROT_RTSCTS) &&
12404f545a2cSAdrian Chadd 	    rt->info[rix].phy == IEEE80211_T_HT &&
12414f545a2cSAdrian Chadd 	    (flags & HAL_TXDESC_NOACK) == 0) {
12424f545a2cSAdrian Chadd 		cix = rt->info[sc->sc_protrix].controlRate;
12434f545a2cSAdrian Chadd 	    	flags |= HAL_TXDESC_RTSENA;
12444f545a2cSAdrian Chadd 		sc->sc_stats.ast_tx_htprotect++;
12454f545a2cSAdrian Chadd 	}
12464f545a2cSAdrian Chadd #endif
12474f545a2cSAdrian Chadd 
1248b8e788a5SAdrian Chadd 	/*
1249b8e788a5SAdrian Chadd 	 * Calculate duration.  This logically belongs in the 802.11
1250b8e788a5SAdrian Chadd 	 * layer but it lacks sufficient information to calculate it.
1251b8e788a5SAdrian Chadd 	 */
1252b8e788a5SAdrian Chadd 	if ((flags & HAL_TXDESC_NOACK) == 0 &&
1253b8e788a5SAdrian Chadd 	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL) {
1254b8e788a5SAdrian Chadd 		u_int16_t dur;
1255b8e788a5SAdrian Chadd 		if (shortPreamble)
1256b8e788a5SAdrian Chadd 			dur = rt->info[rix].spAckDuration;
1257b8e788a5SAdrian Chadd 		else
1258b8e788a5SAdrian Chadd 			dur = rt->info[rix].lpAckDuration;
1259b8e788a5SAdrian Chadd 		if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) {
1260b8e788a5SAdrian Chadd 			dur += dur;		/* additional SIFS+ACK */
1261b8e788a5SAdrian Chadd 			KASSERT(m0->m_nextpkt != NULL, ("no fragment"));
1262b8e788a5SAdrian Chadd 			/*
1263b8e788a5SAdrian Chadd 			 * Include the size of next fragment so NAV is
1264b8e788a5SAdrian Chadd 			 * updated properly.  The last fragment uses only
1265b8e788a5SAdrian Chadd 			 * the ACK duration
1266b8e788a5SAdrian Chadd 			 */
1267b8e788a5SAdrian Chadd 			dur += ath_hal_computetxtime(ah, rt,
1268b8e788a5SAdrian Chadd 					m0->m_nextpkt->m_pkthdr.len,
1269b8e788a5SAdrian Chadd 					rix, shortPreamble);
1270b8e788a5SAdrian Chadd 		}
1271b8e788a5SAdrian Chadd 		if (isfrag) {
1272b8e788a5SAdrian Chadd 			/*
1273b8e788a5SAdrian Chadd 			 * Force hardware to use computed duration for next
1274b8e788a5SAdrian Chadd 			 * fragment by disabling multi-rate retry which updates
1275b8e788a5SAdrian Chadd 			 * duration based on the multi-rate duration table.
1276b8e788a5SAdrian Chadd 			 */
1277b8e788a5SAdrian Chadd 			ismrr = 0;
1278b8e788a5SAdrian Chadd 			try0 = ATH_TXMGTTRY;	/* XXX? */
1279b8e788a5SAdrian Chadd 		}
1280b8e788a5SAdrian Chadd 		*(u_int16_t *)wh->i_dur = htole16(dur);
1281b8e788a5SAdrian Chadd 	}
1282b8e788a5SAdrian Chadd 
1283b8e788a5SAdrian Chadd 	/*
1284eb6f0de0SAdrian Chadd 	 * Determine if a tx interrupt should be generated for
1285eb6f0de0SAdrian Chadd 	 * this descriptor.  We take a tx interrupt to reap
1286eb6f0de0SAdrian Chadd 	 * descriptors when the h/w hits an EOL condition or
1287eb6f0de0SAdrian Chadd 	 * when the descriptor is specifically marked to generate
1288eb6f0de0SAdrian Chadd 	 * an interrupt.  We periodically mark descriptors in this
1289eb6f0de0SAdrian Chadd 	 * way to insure timely replenishing of the supply needed
1290eb6f0de0SAdrian Chadd 	 * for sending frames.  Defering interrupts reduces system
1291eb6f0de0SAdrian Chadd 	 * load and potentially allows more concurrent work to be
1292eb6f0de0SAdrian Chadd 	 * done but if done to aggressively can cause senders to
1293eb6f0de0SAdrian Chadd 	 * backup.
1294eb6f0de0SAdrian Chadd 	 *
1295eb6f0de0SAdrian Chadd 	 * NB: use >= to deal with sc_txintrperiod changing
1296eb6f0de0SAdrian Chadd 	 *     dynamically through sysctl.
1297b8e788a5SAdrian Chadd 	 */
1298eb6f0de0SAdrian Chadd 	if (flags & HAL_TXDESC_INTREQ) {
1299eb6f0de0SAdrian Chadd 		txq->axq_intrcnt = 0;
1300eb6f0de0SAdrian Chadd 	} else if (++txq->axq_intrcnt >= sc->sc_txintrperiod) {
1301eb6f0de0SAdrian Chadd 		flags |= HAL_TXDESC_INTREQ;
1302eb6f0de0SAdrian Chadd 		txq->axq_intrcnt = 0;
1303eb6f0de0SAdrian Chadd 	}
1304e42b5dbaSAdrian Chadd 
1305eb6f0de0SAdrian Chadd 	/* This point forward is actual TX bits */
1306b8e788a5SAdrian Chadd 
1307b8e788a5SAdrian Chadd 	/*
1308b8e788a5SAdrian Chadd 	 * At this point we are committed to sending the frame
1309b8e788a5SAdrian Chadd 	 * and we don't need to look at m_nextpkt; clear it in
1310b8e788a5SAdrian Chadd 	 * case this frame is part of frag chain.
1311b8e788a5SAdrian Chadd 	 */
1312b8e788a5SAdrian Chadd 	m0->m_nextpkt = NULL;
1313b8e788a5SAdrian Chadd 
1314b8e788a5SAdrian Chadd 	if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
1315b8e788a5SAdrian Chadd 		ieee80211_dump_pkt(ic, mtod(m0, const uint8_t *), m0->m_len,
1316b8e788a5SAdrian Chadd 		    sc->sc_hwmap[rix].ieeerate, -1);
1317b8e788a5SAdrian Chadd 
1318b8e788a5SAdrian Chadd 	if (ieee80211_radiotap_active_vap(vap)) {
1319b8e788a5SAdrian Chadd 		u_int64_t tsf = ath_hal_gettsf64(ah);
1320b8e788a5SAdrian Chadd 
1321b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_tsf = htole64(tsf);
1322b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
1323b8e788a5SAdrian Chadd 		if (iswep)
1324b8e788a5SAdrian Chadd 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
1325b8e788a5SAdrian Chadd 		if (isfrag)
1326b8e788a5SAdrian Chadd 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
1327b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
1328b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_txpower = ni->ni_txpower;
1329b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
1330b8e788a5SAdrian Chadd 
1331b8e788a5SAdrian Chadd 		ieee80211_radiotap_tx(vap, m0);
1332b8e788a5SAdrian Chadd 	}
1333b8e788a5SAdrian Chadd 
1334eb6f0de0SAdrian Chadd 	/* Blank the legacy rate array */
1335eb6f0de0SAdrian Chadd 	bzero(&bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
1336c1782ce0SAdrian Chadd 
1337b8e788a5SAdrian Chadd 	/*
1338eb6f0de0SAdrian Chadd 	 * ath_buf_set_rate needs at least one rate/try to setup
1339eb6f0de0SAdrian Chadd 	 * the rate scenario.
1340b8e788a5SAdrian Chadd 	 */
1341eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].rix = rix;
1342eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].tries = try0;
1343eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].ratecode = txrate;
1344eb6f0de0SAdrian Chadd 
1345eb6f0de0SAdrian Chadd 	/* Store the decided rate index values away */
1346eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_pktlen = pktlen;
1347eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_hdrlen = hdrlen;
1348eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_atype = atype;
1349eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txpower = ni->ni_txpower;
1350eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txrate0 = txrate;
1351eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_try0 = try0;
1352eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_keyix = keyix;
1353eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txantenna = sc->sc_txantenna;
1354eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_flags = flags;
1355b8e788a5SAdrian Chadd 	bf->bf_txflags = flags;
1356eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_shpream = shortPreamble;
1357eb6f0de0SAdrian Chadd 
1358eb6f0de0SAdrian Chadd 	/* XXX this should be done in ath_tx_setrate() */
1359eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsrate0 = 0;	/* ie, no hard-coded ctsrate */
1360eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsrate = 0;	/* calculated later */
1361eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsduration = 0;
1362eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ismrr = ismrr;
1363eb6f0de0SAdrian Chadd 
1364eb6f0de0SAdrian Chadd 	return 0;
1365eb6f0de0SAdrian Chadd }
1366eb6f0de0SAdrian Chadd 
1367b8e788a5SAdrian Chadd /*
1368eb6f0de0SAdrian Chadd  * Direct-dispatch the current frame to the hardware.
1369eb6f0de0SAdrian Chadd  *
1370eb6f0de0SAdrian Chadd  * This can be called by the net80211 code.
1371eb6f0de0SAdrian Chadd  *
1372eb6f0de0SAdrian Chadd  * XXX what about locking? Or, push the seqno assign into the
1373eb6f0de0SAdrian Chadd  * XXX aggregate scheduler so its serialised?
1374b8e788a5SAdrian Chadd  */
1375eb6f0de0SAdrian Chadd int
1376eb6f0de0SAdrian Chadd ath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni,
1377eb6f0de0SAdrian Chadd     struct ath_buf *bf, struct mbuf *m0)
1378eb6f0de0SAdrian Chadd {
1379eb6f0de0SAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
1380eb6f0de0SAdrian Chadd 	struct ath_vap *avp = ATH_VAP(vap);
13819c85ff91SAdrian Chadd 	int r = 0;
1382eb6f0de0SAdrian Chadd 	u_int pri;
1383eb6f0de0SAdrian Chadd 	int tid;
1384eb6f0de0SAdrian Chadd 	struct ath_txq *txq;
1385eb6f0de0SAdrian Chadd 	int ismcast;
1386eb6f0de0SAdrian Chadd 	const struct ieee80211_frame *wh;
1387eb6f0de0SAdrian Chadd 	int is_ampdu, is_ampdu_tx, is_ampdu_pending;
13880b96ef63SAdrian Chadd 	//ieee80211_seq seqno;
1389eb6f0de0SAdrian Chadd 	uint8_t type, subtype;
1390eb6f0de0SAdrian Chadd 
1391eb6f0de0SAdrian Chadd 	/*
1392eb6f0de0SAdrian Chadd 	 * Determine the target hardware queue.
1393eb6f0de0SAdrian Chadd 	 *
1394eb6f0de0SAdrian Chadd 	 * For multicast frames, the txq gets overridden to be the
1395eb6f0de0SAdrian Chadd 	 * software TXQ and it's done via direct-dispatch.
1396eb6f0de0SAdrian Chadd 	 *
1397eb6f0de0SAdrian Chadd 	 * For any other frame, we do a TID/QoS lookup inside the frame
1398eb6f0de0SAdrian Chadd 	 * to see what the TID should be. If it's a non-QoS frame, the
1399eb6f0de0SAdrian Chadd 	 * AC and TID are overridden. The TID/TXQ code assumes the
1400eb6f0de0SAdrian Chadd 	 * TID is on a predictable hardware TXQ, so we don't support
1401eb6f0de0SAdrian Chadd 	 * having a node TID queued to multiple hardware TXQs.
1402eb6f0de0SAdrian Chadd 	 * This may change in the future but would require some locking
1403eb6f0de0SAdrian Chadd 	 * fudgery.
1404eb6f0de0SAdrian Chadd 	 */
1405eb6f0de0SAdrian Chadd 	pri = ath_tx_getac(sc, m0);
1406eb6f0de0SAdrian Chadd 	tid = ath_tx_gettid(sc, m0);
1407eb6f0de0SAdrian Chadd 
1408eb6f0de0SAdrian Chadd 	txq = sc->sc_ac2q[pri];
1409eb6f0de0SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1410eb6f0de0SAdrian Chadd 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1411eb6f0de0SAdrian Chadd 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1412eb6f0de0SAdrian Chadd 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1413eb6f0de0SAdrian Chadd 
14149c85ff91SAdrian Chadd 	/*
14159c85ff91SAdrian Chadd 	 * Enforce how deep the multicast queue can grow.
14169c85ff91SAdrian Chadd 	 *
14179c85ff91SAdrian Chadd 	 * XXX duplicated in ath_raw_xmit().
14189c85ff91SAdrian Chadd 	 */
14199c85ff91SAdrian Chadd 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
14209c85ff91SAdrian Chadd 		ATH_TXQ_LOCK(sc->sc_cabq);
14219c85ff91SAdrian Chadd 
1422b09e37a1SAdrian Chadd 		if (sc->sc_cabq->axq_depth > sc->sc_txq_mcastq_maxdepth) {
14239c85ff91SAdrian Chadd 			sc->sc_stats.ast_tx_mcastq_overflow++;
14249c85ff91SAdrian Chadd 			r = ENOBUFS;
14259c85ff91SAdrian Chadd 		}
14269c85ff91SAdrian Chadd 
14279c85ff91SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_cabq);
14289c85ff91SAdrian Chadd 
14299c85ff91SAdrian Chadd 		if (r != 0) {
14309c85ff91SAdrian Chadd 			m_freem(m0);
14319c85ff91SAdrian Chadd 			return r;
14329c85ff91SAdrian Chadd 		}
14339c85ff91SAdrian Chadd 	}
14349c85ff91SAdrian Chadd 
1435eb6f0de0SAdrian Chadd 	/* A-MPDU TX */
1436eb6f0de0SAdrian Chadd 	is_ampdu_tx = ath_tx_ampdu_running(sc, ATH_NODE(ni), tid);
1437eb6f0de0SAdrian Chadd 	is_ampdu_pending = ath_tx_ampdu_pending(sc, ATH_NODE(ni), tid);
1438eb6f0de0SAdrian Chadd 	is_ampdu = is_ampdu_tx | is_ampdu_pending;
1439eb6f0de0SAdrian Chadd 
14400b96ef63SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX,
14410b96ef63SAdrian Chadd 	    "%s: bf=%p, tid=%d, ac=%d, is_ampdu=%d\n",
14420b96ef63SAdrian Chadd 	    __func__, bf, tid, pri, is_ampdu);
1443eb6f0de0SAdrian Chadd 
1444eb6f0de0SAdrian Chadd 	/* Multicast frames go onto the software multicast queue */
1445eb6f0de0SAdrian Chadd 	if (ismcast)
1446eb6f0de0SAdrian Chadd 		txq = &avp->av_mcastq;
1447eb6f0de0SAdrian Chadd 
1448c5940c30SAdrian Chadd 	/*
1449c5940c30SAdrian Chadd 	 * XXX This likely means that if there's a station in power
1450c5940c30SAdrian Chadd 	 * save mode, we won't be doing any kind of aggregation towards
1451c5940c30SAdrian Chadd 	 * anyone.  This is likely a very suboptimal way of dealing
1452c5940c30SAdrian Chadd 	 * with things.
1453c5940c30SAdrian Chadd 	 */
1454eb6f0de0SAdrian Chadd 	if ((! is_ampdu) && (vap->iv_ps_sta || avp->av_mcastq.axq_depth))
1455eb6f0de0SAdrian Chadd 		txq = &avp->av_mcastq;
1456eb6f0de0SAdrian Chadd 
1457eb6f0de0SAdrian Chadd 	/* Do the generic frame setup */
1458eb6f0de0SAdrian Chadd 	/* XXX should just bzero the bf_state? */
1459eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_dobaw = 0;
14600b96ef63SAdrian Chadd 	bf->bf_state.bfs_seqno_assigned = 0;
14610b96ef63SAdrian Chadd 	bf->bf_state.bfs_need_seqno = 0;
14620b96ef63SAdrian Chadd 	bf->bf_state.bfs_seqno = -1;	/* XXX debugging */
1463eb6f0de0SAdrian Chadd 
1464eb6f0de0SAdrian Chadd 	/* A-MPDU TX? Manually set sequence number */
1465eb6f0de0SAdrian Chadd 	/* Don't do it whilst pending; the net80211 layer still assigns them */
1466eb6f0de0SAdrian Chadd 	/* XXX do we need locking here? */
1467eb6f0de0SAdrian Chadd 	if (is_ampdu_tx) {
1468eb6f0de0SAdrian Chadd 		ATH_TXQ_LOCK(txq);
1469eb6f0de0SAdrian Chadd 		/*
1470eb6f0de0SAdrian Chadd 		 * Always call; this function will
1471eb6f0de0SAdrian Chadd 		 * handle making sure that null data frames
1472eb6f0de0SAdrian Chadd 		 * don't get a sequence number from the current
1473eb6f0de0SAdrian Chadd 		 * TID and thus mess with the BAW.
1474eb6f0de0SAdrian Chadd 		 */
14750b96ef63SAdrian Chadd 		//seqno = ath_tx_tid_seqno_assign(sc, ni, bf, m0);
14760b96ef63SAdrian Chadd 		if (ath_tx_seqno_required(sc, ni, bf, m0)) {
1477eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_dobaw = 1;
14780b96ef63SAdrian Chadd 			bf->bf_state.bfs_need_seqno = 1;
1479eb6f0de0SAdrian Chadd 		}
1480eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
14810b96ef63SAdrian Chadd 	} else {
14820b96ef63SAdrian Chadd 		/* No AMPDU TX, we've been assigned a sequence number. */
14830b96ef63SAdrian Chadd 		if (IEEE80211_QOS_HAS_SEQ(wh)) {
14840b96ef63SAdrian Chadd 			bf->bf_state.bfs_seqno_assigned = 1;
1485*12be5b9cSAdrian Chadd 			/* XXX we should store the frag+seqno in bfs_seqno */
14860b96ef63SAdrian Chadd 			bf->bf_state.bfs_seqno =
14870b96ef63SAdrian Chadd 			    M_SEQNO_GET(m0) << IEEE80211_SEQ_SEQ_SHIFT;
14880b96ef63SAdrian Chadd 		}
1489c1782ce0SAdrian Chadd 	}
1490c1782ce0SAdrian Chadd 
1491eb6f0de0SAdrian Chadd 	/*
1492eb6f0de0SAdrian Chadd 	 * If needed, the sequence number has been assigned.
1493eb6f0de0SAdrian Chadd 	 * Squirrel it away somewhere easy to get to.
1494eb6f0de0SAdrian Chadd 	 */
14950b96ef63SAdrian Chadd 	//bf->bf_state.bfs_seqno = M_SEQNO_GET(m0) << IEEE80211_SEQ_SEQ_SHIFT;
1496b8e788a5SAdrian Chadd 
1497eb6f0de0SAdrian Chadd 	/* Is ampdu pending? fetch the seqno and print it out */
1498eb6f0de0SAdrian Chadd 	if (is_ampdu_pending)
1499eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX,
1500eb6f0de0SAdrian Chadd 		    "%s: tid %d: ampdu pending, seqno %d\n",
1501eb6f0de0SAdrian Chadd 		    __func__, tid, M_SEQNO_GET(m0));
1502eb6f0de0SAdrian Chadd 
1503eb6f0de0SAdrian Chadd 	/* This also sets up the DMA map */
1504eb6f0de0SAdrian Chadd 	r = ath_tx_normal_setup(sc, ni, bf, m0);
1505eb6f0de0SAdrian Chadd 
1506eb6f0de0SAdrian Chadd 	if (r != 0)
1507eb6f0de0SAdrian Chadd 		return r;
1508eb6f0de0SAdrian Chadd 
1509eb6f0de0SAdrian Chadd 	/* At this point m0 could have changed! */
1510eb6f0de0SAdrian Chadd 	m0 = bf->bf_m;
1511eb6f0de0SAdrian Chadd 
15120b96ef63SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX,
15130b96ef63SAdrian Chadd 	    "%s: DONE: bf=%p, tid=%d, ac=%d, is_ampdu=%d, dobaw=%d, seqno=%d\n",
15140b96ef63SAdrian Chadd 	    __func__, bf, tid, pri, is_ampdu, bf->bf_state.bfs_dobaw, M_SEQNO_GET(m0));
15150b96ef63SAdrian Chadd 
1516eb6f0de0SAdrian Chadd #if 1
1517eb6f0de0SAdrian Chadd 	/*
1518eb6f0de0SAdrian Chadd 	 * If it's a multicast frame, do a direct-dispatch to the
1519eb6f0de0SAdrian Chadd 	 * destination hardware queue. Don't bother software
1520eb6f0de0SAdrian Chadd 	 * queuing it.
1521eb6f0de0SAdrian Chadd 	 */
1522eb6f0de0SAdrian Chadd 	/*
1523eb6f0de0SAdrian Chadd 	 * If it's a BAR frame, do a direct dispatch to the
1524eb6f0de0SAdrian Chadd 	 * destination hardware queue. Don't bother software
1525eb6f0de0SAdrian Chadd 	 * queuing it, as the TID will now be paused.
1526eb6f0de0SAdrian Chadd 	 * Sending a BAR frame can occur from the net80211 txa timer
1527eb6f0de0SAdrian Chadd 	 * (ie, retries) or from the ath txtask (completion call.)
1528eb6f0de0SAdrian Chadd 	 * It queues directly to hardware because the TID is paused
1529eb6f0de0SAdrian Chadd 	 * at this point (and won't be unpaused until the BAR has
1530eb6f0de0SAdrian Chadd 	 * either been TXed successfully or max retries has been
1531eb6f0de0SAdrian Chadd 	 * reached.)
1532eb6f0de0SAdrian Chadd 	 */
1533eb6f0de0SAdrian Chadd 	if (txq == &avp->av_mcastq) {
15340b96ef63SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
15350b96ef63SAdrian Chadd 		    "%s: bf=%p: mcastq: TX'ing\n", __func__, bf);
1536eb6f0de0SAdrian Chadd 		ATH_TXQ_LOCK(txq);
1537eb6f0de0SAdrian Chadd 		ath_tx_xmit_normal(sc, txq, bf);
1538eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
1539eb6f0de0SAdrian Chadd 	} else if (type == IEEE80211_FC0_TYPE_CTL &&
1540eb6f0de0SAdrian Chadd 		    subtype == IEEE80211_FC0_SUBTYPE_BAR) {
1541eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
1542eb6f0de0SAdrian Chadd 		    "%s: BAR: TX'ing direct\n", __func__);
1543eb6f0de0SAdrian Chadd 		ATH_TXQ_LOCK(txq);
1544eb6f0de0SAdrian Chadd 		ath_tx_xmit_normal(sc, txq, bf);
1545eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
1546eb6f0de0SAdrian Chadd 	} else {
1547eb6f0de0SAdrian Chadd 		/* add to software queue */
15480b96ef63SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
15490b96ef63SAdrian Chadd 		    "%s: bf=%p: swq: TX'ing\n", __func__, bf);
1550eb6f0de0SAdrian Chadd 		ath_tx_swq(sc, ni, txq, bf);
1551eb6f0de0SAdrian Chadd 	}
1552eb6f0de0SAdrian Chadd #else
1553eb6f0de0SAdrian Chadd 	/*
1554eb6f0de0SAdrian Chadd 	 * For now, since there's no software queue,
1555eb6f0de0SAdrian Chadd 	 * direct-dispatch to the hardware.
1556eb6f0de0SAdrian Chadd 	 */
1557eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(txq);
1558eb6f0de0SAdrian Chadd 	ath_tx_xmit_normal(sc, txq, bf);
1559eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(txq);
1560eb6f0de0SAdrian Chadd #endif
1561eb6f0de0SAdrian Chadd 
1562b8e788a5SAdrian Chadd 	return 0;
1563b8e788a5SAdrian Chadd }
1564b8e788a5SAdrian Chadd 
1565b8e788a5SAdrian Chadd static int
1566b8e788a5SAdrian Chadd ath_tx_raw_start(struct ath_softc *sc, struct ieee80211_node *ni,
1567b8e788a5SAdrian Chadd 	struct ath_buf *bf, struct mbuf *m0,
1568b8e788a5SAdrian Chadd 	const struct ieee80211_bpf_params *params)
1569b8e788a5SAdrian Chadd {
1570b8e788a5SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
1571b8e788a5SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
1572b8e788a5SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
1573b8e788a5SAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
1574b8e788a5SAdrian Chadd 	int error, ismcast, ismrr;
1575b8e788a5SAdrian Chadd 	int keyix, hdrlen, pktlen, try0, txantenna;
1576eb6f0de0SAdrian Chadd 	u_int8_t rix, txrate;
1577b8e788a5SAdrian Chadd 	struct ieee80211_frame *wh;
1578eb6f0de0SAdrian Chadd 	u_int flags;
1579b8e788a5SAdrian Chadd 	HAL_PKT_TYPE atype;
1580b8e788a5SAdrian Chadd 	const HAL_RATE_TABLE *rt;
1581b8e788a5SAdrian Chadd 	struct ath_desc *ds;
1582b8e788a5SAdrian Chadd 	u_int pri;
1583eb6f0de0SAdrian Chadd 	int o_tid = -1;
1584eb6f0de0SAdrian Chadd 	int do_override;
1585b8e788a5SAdrian Chadd 
1586b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1587b8e788a5SAdrian Chadd 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1588b8e788a5SAdrian Chadd 	hdrlen = ieee80211_anyhdrsize(wh);
1589b8e788a5SAdrian Chadd 	/*
1590b8e788a5SAdrian Chadd 	 * Packet length must not include any
1591b8e788a5SAdrian Chadd 	 * pad bytes; deduct them here.
1592b8e788a5SAdrian Chadd 	 */
1593b8e788a5SAdrian Chadd 	/* XXX honor IEEE80211_BPF_DATAPAD */
1594b8e788a5SAdrian Chadd 	pktlen = m0->m_pkthdr.len - (hdrlen & 3) + IEEE80211_CRC_LEN;
1595b8e788a5SAdrian Chadd 
1596eb6f0de0SAdrian Chadd 
1597eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: ismcast=%d\n",
1598eb6f0de0SAdrian Chadd 	    __func__, ismcast);
1599eb6f0de0SAdrian Chadd 
160081a82688SAdrian Chadd 	/* Handle encryption twiddling if needed */
1601eb6f0de0SAdrian Chadd 	if (! ath_tx_tag_crypto(sc, ni,
1602eb6f0de0SAdrian Chadd 	    m0, params->ibp_flags & IEEE80211_BPF_CRYPTO, 0,
1603eb6f0de0SAdrian Chadd 	    &hdrlen, &pktlen, &keyix)) {
1604b8e788a5SAdrian Chadd 		ath_freetx(m0);
1605b8e788a5SAdrian Chadd 		return EIO;
1606b8e788a5SAdrian Chadd 	}
1607b8e788a5SAdrian Chadd 	/* packet header may have moved, reset our local pointer */
1608b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1609b8e788a5SAdrian Chadd 
1610eb6f0de0SAdrian Chadd 	/* Do the generic frame setup */
1611eb6f0de0SAdrian Chadd 	/* XXX should just bzero the bf_state? */
1612eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_dobaw = 0;
1613eb6f0de0SAdrian Chadd 
1614b8e788a5SAdrian Chadd 	error = ath_tx_dmasetup(sc, bf, m0);
1615b8e788a5SAdrian Chadd 	if (error != 0)
1616b8e788a5SAdrian Chadd 		return error;
1617b8e788a5SAdrian Chadd 	m0 = bf->bf_m;				/* NB: may have changed */
1618b8e788a5SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
1619b8e788a5SAdrian Chadd 	bf->bf_node = ni;			/* NB: held reference */
1620b8e788a5SAdrian Chadd 
1621b8e788a5SAdrian Chadd 	flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for crypto errs */
1622b8e788a5SAdrian Chadd 	flags |= HAL_TXDESC_INTREQ;		/* force interrupt */
1623b8e788a5SAdrian Chadd 	if (params->ibp_flags & IEEE80211_BPF_RTS)
1624b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_RTSENA;
1625eb6f0de0SAdrian Chadd 	else if (params->ibp_flags & IEEE80211_BPF_CTS) {
1626eb6f0de0SAdrian Chadd 		/* XXX assume 11g/11n protection? */
1627eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_doprot = 1;
1628b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_CTSENA;
1629eb6f0de0SAdrian Chadd 	}
1630b8e788a5SAdrian Chadd 	/* XXX leave ismcast to injector? */
1631b8e788a5SAdrian Chadd 	if ((params->ibp_flags & IEEE80211_BPF_NOACK) || ismcast)
1632b8e788a5SAdrian Chadd 		flags |= HAL_TXDESC_NOACK;
1633b8e788a5SAdrian Chadd 
1634b8e788a5SAdrian Chadd 	rt = sc->sc_currates;
1635b8e788a5SAdrian Chadd 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
1636b8e788a5SAdrian Chadd 	rix = ath_tx_findrix(sc, params->ibp_rate0);
1637b8e788a5SAdrian Chadd 	txrate = rt->info[rix].rateCode;
1638b8e788a5SAdrian Chadd 	if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
1639b8e788a5SAdrian Chadd 		txrate |= rt->info[rix].shortPreamble;
1640b8e788a5SAdrian Chadd 	sc->sc_txrix = rix;
1641b8e788a5SAdrian Chadd 	try0 = params->ibp_try0;
1642b8e788a5SAdrian Chadd 	ismrr = (params->ibp_try1 != 0);
1643b8e788a5SAdrian Chadd 	txantenna = params->ibp_pri >> 2;
1644b8e788a5SAdrian Chadd 	if (txantenna == 0)			/* XXX? */
1645b8e788a5SAdrian Chadd 		txantenna = sc->sc_txantenna;
164679f02dbfSAdrian Chadd 
164779f02dbfSAdrian Chadd 	/*
1648eb6f0de0SAdrian Chadd 	 * Since ctsrate is fixed, store it away for later
1649eb6f0de0SAdrian Chadd 	 * use when the descriptor fields are being set.
165079f02dbfSAdrian Chadd 	 */
1651eb6f0de0SAdrian Chadd 	if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA))
1652eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_ctsrate0 = params->ibp_ctsrate;
165379f02dbfSAdrian Chadd 
1654b8e788a5SAdrian Chadd 	pri = params->ibp_pri & 3;
1655eb6f0de0SAdrian Chadd 	/* Override pri if the frame isn't a QoS one */
1656eb6f0de0SAdrian Chadd 	if (! IEEE80211_QOS_HAS_SEQ(wh))
1657eb6f0de0SAdrian Chadd 		pri = ath_tx_getac(sc, m0);
1658eb6f0de0SAdrian Chadd 
1659b8e788a5SAdrian Chadd 	/*
1660b8e788a5SAdrian Chadd 	 * NB: we mark all packets as type PSPOLL so the h/w won't
1661b8e788a5SAdrian Chadd 	 * set the sequence number, duration, etc.
1662b8e788a5SAdrian Chadd 	 */
1663b8e788a5SAdrian Chadd 	atype = HAL_PKT_TYPE_PSPOLL;
1664b8e788a5SAdrian Chadd 
1665b8e788a5SAdrian Chadd 	if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
1666b8e788a5SAdrian Chadd 		ieee80211_dump_pkt(ic, mtod(m0, caddr_t), m0->m_len,
1667b8e788a5SAdrian Chadd 		    sc->sc_hwmap[rix].ieeerate, -1);
1668b8e788a5SAdrian Chadd 
1669b8e788a5SAdrian Chadd 	if (ieee80211_radiotap_active_vap(vap)) {
1670b8e788a5SAdrian Chadd 		u_int64_t tsf = ath_hal_gettsf64(ah);
1671b8e788a5SAdrian Chadd 
1672b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_tsf = htole64(tsf);
1673b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
1674b8e788a5SAdrian Chadd 		if (wh->i_fc[1] & IEEE80211_FC1_WEP)
1675b8e788a5SAdrian Chadd 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
1676b8e788a5SAdrian Chadd 		if (m0->m_flags & M_FRAG)
1677b8e788a5SAdrian Chadd 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
1678b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
1679b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_txpower = ni->ni_txpower;
1680b8e788a5SAdrian Chadd 		sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
1681b8e788a5SAdrian Chadd 
1682b8e788a5SAdrian Chadd 		ieee80211_radiotap_tx(vap, m0);
1683b8e788a5SAdrian Chadd 	}
1684b8e788a5SAdrian Chadd 
1685b8e788a5SAdrian Chadd 	/*
1686b8e788a5SAdrian Chadd 	 * Formulate first tx descriptor with tx controls.
1687b8e788a5SAdrian Chadd 	 */
1688b8e788a5SAdrian Chadd 	ds = bf->bf_desc;
1689b8e788a5SAdrian Chadd 	/* XXX check return value? */
1690eb6f0de0SAdrian Chadd 
1691eb6f0de0SAdrian Chadd 	/* Store the decided rate index values away */
1692eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_pktlen = pktlen;
1693eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_hdrlen = hdrlen;
1694eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_atype = atype;
1695eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txpower = params->ibp_power;
1696eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txrate0 = txrate;
1697eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_try0 = try0;
1698eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_keyix = keyix;
1699eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txantenna = txantenna;
1700eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_flags = flags;
1701b8e788a5SAdrian Chadd 	bf->bf_txflags = flags;
1702eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_shpream =
1703eb6f0de0SAdrian Chadd 	    !! (params->ibp_flags & IEEE80211_BPF_SHORTPRE);
1704b8e788a5SAdrian Chadd 
1705eb6f0de0SAdrian Chadd 	/* XXX this should be done in ath_tx_setrate() */
1706eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsrate = 0;
1707eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ctsduration = 0;
1708eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_ismrr = ismrr;
1709eb6f0de0SAdrian Chadd 
1710eb6f0de0SAdrian Chadd 	/* Blank the legacy rate array */
1711eb6f0de0SAdrian Chadd 	bzero(&bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
1712eb6f0de0SAdrian Chadd 
1713eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].rix =
1714eb6f0de0SAdrian Chadd 	    ath_tx_findrix(sc, params->ibp_rate0);
1715eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].tries = try0;
1716eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_rc[0].ratecode = txrate;
1717c1782ce0SAdrian Chadd 
1718c1782ce0SAdrian Chadd 	if (ismrr) {
1719eb6f0de0SAdrian Chadd 		int rix;
1720c1782ce0SAdrian Chadd 
1721b8e788a5SAdrian Chadd 		rix = ath_tx_findrix(sc, params->ibp_rate1);
1722eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[1].rix = rix;
1723eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[1].tries = params->ibp_try1;
1724c1782ce0SAdrian Chadd 
1725eb6f0de0SAdrian Chadd 		rix = ath_tx_findrix(sc, params->ibp_rate2);
1726eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[2].rix = rix;
1727eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[2].tries = params->ibp_try2;
1728eb6f0de0SAdrian Chadd 
1729eb6f0de0SAdrian Chadd 		rix = ath_tx_findrix(sc, params->ibp_rate3);
1730eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[3].rix = rix;
1731eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[3].tries = params->ibp_try3;
1732c1782ce0SAdrian Chadd 	}
1733eb6f0de0SAdrian Chadd 	/*
1734eb6f0de0SAdrian Chadd 	 * All the required rate control decisions have been made;
1735eb6f0de0SAdrian Chadd 	 * fill in the rc flags.
1736eb6f0de0SAdrian Chadd 	 */
1737eb6f0de0SAdrian Chadd 	ath_tx_rate_fill_rcflags(sc, bf);
1738b8e788a5SAdrian Chadd 
1739b8e788a5SAdrian Chadd 	/* NB: no buffered multicast in power save support */
1740eb6f0de0SAdrian Chadd 
1741eb6f0de0SAdrian Chadd 	/* XXX If it's an ADDBA, override the correct queue */
1742eb6f0de0SAdrian Chadd 	do_override = ath_tx_action_frame_override_queue(sc, ni, m0, &o_tid);
1743eb6f0de0SAdrian Chadd 
1744eb6f0de0SAdrian Chadd 	/* Map ADDBA to the correct priority */
1745eb6f0de0SAdrian Chadd 	if (do_override) {
1746eb6f0de0SAdrian Chadd #if 0
1747eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
1748eb6f0de0SAdrian Chadd 		    "%s: overriding tid %d pri %d -> %d\n",
1749eb6f0de0SAdrian Chadd 		    __func__, o_tid, pri, TID_TO_WME_AC(o_tid));
1750eb6f0de0SAdrian Chadd #endif
1751eb6f0de0SAdrian Chadd 		pri = TID_TO_WME_AC(o_tid);
1752eb6f0de0SAdrian Chadd 	}
1753eb6f0de0SAdrian Chadd 
1754eb6f0de0SAdrian Chadd 	/*
1755eb6f0de0SAdrian Chadd 	 * If we're overiding the ADDBA destination, dump directly
1756eb6f0de0SAdrian Chadd 	 * into the hardware queue, right after any pending
1757eb6f0de0SAdrian Chadd 	 * frames to that node are.
1758eb6f0de0SAdrian Chadd 	 */
1759eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: dooverride=%d\n",
1760eb6f0de0SAdrian Chadd 	    __func__, do_override);
1761eb6f0de0SAdrian Chadd 
1762eb6f0de0SAdrian Chadd 	if (do_override) {
1763eb6f0de0SAdrian Chadd 		ATH_TXQ_LOCK(sc->sc_ac2q[pri]);
1764eb6f0de0SAdrian Chadd 		ath_tx_xmit_normal(sc, sc->sc_ac2q[pri], bf);
1765eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[pri]);
1766eb6f0de0SAdrian Chadd 	} else {
1767eb6f0de0SAdrian Chadd 		/* Queue to software queue */
1768eb6f0de0SAdrian Chadd 		ath_tx_swq(sc, ni, sc->sc_ac2q[pri], bf);
1769eb6f0de0SAdrian Chadd 	}
1770eb6f0de0SAdrian Chadd 
1771b8e788a5SAdrian Chadd 	return 0;
1772b8e788a5SAdrian Chadd }
1773b8e788a5SAdrian Chadd 
1774eb6f0de0SAdrian Chadd /*
1775eb6f0de0SAdrian Chadd  * Send a raw frame.
1776eb6f0de0SAdrian Chadd  *
1777eb6f0de0SAdrian Chadd  * This can be called by net80211.
1778eb6f0de0SAdrian Chadd  */
1779b8e788a5SAdrian Chadd int
1780b8e788a5SAdrian Chadd ath_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1781b8e788a5SAdrian Chadd 	const struct ieee80211_bpf_params *params)
1782b8e788a5SAdrian Chadd {
1783b8e788a5SAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
1784b8e788a5SAdrian Chadd 	struct ifnet *ifp = ic->ic_ifp;
1785b8e788a5SAdrian Chadd 	struct ath_softc *sc = ifp->if_softc;
1786b8e788a5SAdrian Chadd 	struct ath_buf *bf;
17879c85ff91SAdrian Chadd 	struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
17889c85ff91SAdrian Chadd 	int error = 0;
1789b8e788a5SAdrian Chadd 
1790ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
1791ef27340cSAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
1792ef27340cSAdrian Chadd 		device_printf(sc->sc_dev, "%s: sc_inreset_cnt > 0; bailing\n",
1793ef27340cSAdrian Chadd 		    __func__);
1794ef27340cSAdrian Chadd 		error = EIO;
1795ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
1796ef27340cSAdrian Chadd 		goto bad0;
1797ef27340cSAdrian Chadd 	}
1798ef27340cSAdrian Chadd 	sc->sc_txstart_cnt++;
1799ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
1800ef27340cSAdrian Chadd 
1801b8e788a5SAdrian Chadd 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->sc_invalid) {
1802b8e788a5SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: discard frame, %s", __func__,
1803b8e788a5SAdrian Chadd 		    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ?
1804b8e788a5SAdrian Chadd 			"!running" : "invalid");
1805b8e788a5SAdrian Chadd 		m_freem(m);
1806b8e788a5SAdrian Chadd 		error = ENETDOWN;
1807b8e788a5SAdrian Chadd 		goto bad;
1808b8e788a5SAdrian Chadd 	}
18099c85ff91SAdrian Chadd 
18109c85ff91SAdrian Chadd 	/*
18119c85ff91SAdrian Chadd 	 * Enforce how deep the multicast queue can grow.
18129c85ff91SAdrian Chadd 	 *
18139c85ff91SAdrian Chadd 	 * XXX duplicated in ath_tx_start().
18149c85ff91SAdrian Chadd 	 */
18159c85ff91SAdrian Chadd 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
18169c85ff91SAdrian Chadd 		ATH_TXQ_LOCK(sc->sc_cabq);
18179c85ff91SAdrian Chadd 
1818b09e37a1SAdrian Chadd 		if (sc->sc_cabq->axq_depth > sc->sc_txq_mcastq_maxdepth) {
18199c85ff91SAdrian Chadd 			sc->sc_stats.ast_tx_mcastq_overflow++;
18209c85ff91SAdrian Chadd 			error = ENOBUFS;
18219c85ff91SAdrian Chadd 		}
18229c85ff91SAdrian Chadd 
18239c85ff91SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_cabq);
18249c85ff91SAdrian Chadd 
18259c85ff91SAdrian Chadd 		if (error != 0) {
18269c85ff91SAdrian Chadd 			m_freem(m);
18279c85ff91SAdrian Chadd 			goto bad;
18289c85ff91SAdrian Chadd 		}
18299c85ff91SAdrian Chadd 	}
18309c85ff91SAdrian Chadd 
1831b8e788a5SAdrian Chadd 	/*
1832b8e788a5SAdrian Chadd 	 * Grab a TX buffer and associated resources.
1833b8e788a5SAdrian Chadd 	 */
1834b8e788a5SAdrian Chadd 	bf = ath_getbuf(sc);
1835b8e788a5SAdrian Chadd 	if (bf == NULL) {
1836b8e788a5SAdrian Chadd 		sc->sc_stats.ast_tx_nobuf++;
1837b8e788a5SAdrian Chadd 		m_freem(m);
1838b8e788a5SAdrian Chadd 		error = ENOBUFS;
1839b8e788a5SAdrian Chadd 		goto bad;
1840b8e788a5SAdrian Chadd 	}
1841b8e788a5SAdrian Chadd 
1842b8e788a5SAdrian Chadd 	if (params == NULL) {
1843b8e788a5SAdrian Chadd 		/*
1844b8e788a5SAdrian Chadd 		 * Legacy path; interpret frame contents to decide
1845b8e788a5SAdrian Chadd 		 * precisely how to send the frame.
1846b8e788a5SAdrian Chadd 		 */
1847b8e788a5SAdrian Chadd 		if (ath_tx_start(sc, ni, bf, m)) {
1848b8e788a5SAdrian Chadd 			error = EIO;		/* XXX */
1849b8e788a5SAdrian Chadd 			goto bad2;
1850b8e788a5SAdrian Chadd 		}
1851b8e788a5SAdrian Chadd 	} else {
1852b8e788a5SAdrian Chadd 		/*
1853b8e788a5SAdrian Chadd 		 * Caller supplied explicit parameters to use in
1854b8e788a5SAdrian Chadd 		 * sending the frame.
1855b8e788a5SAdrian Chadd 		 */
1856b8e788a5SAdrian Chadd 		if (ath_tx_raw_start(sc, ni, bf, m, params)) {
1857b8e788a5SAdrian Chadd 			error = EIO;		/* XXX */
1858b8e788a5SAdrian Chadd 			goto bad2;
1859b8e788a5SAdrian Chadd 		}
1860b8e788a5SAdrian Chadd 	}
1861b8e788a5SAdrian Chadd 	sc->sc_wd_timer = 5;
1862b8e788a5SAdrian Chadd 	ifp->if_opackets++;
1863b8e788a5SAdrian Chadd 	sc->sc_stats.ast_tx_raw++;
1864b8e788a5SAdrian Chadd 
1865ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
1866ef27340cSAdrian Chadd 	sc->sc_txstart_cnt--;
1867ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
1868ef27340cSAdrian Chadd 
1869b8e788a5SAdrian Chadd 	return 0;
1870b8e788a5SAdrian Chadd bad2:
1871b8e788a5SAdrian Chadd 	ATH_TXBUF_LOCK(sc);
18726b349e5aSAdrian Chadd 	TAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
1873b8e788a5SAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
1874b8e788a5SAdrian Chadd bad:
1875ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
1876ef27340cSAdrian Chadd 	sc->sc_txstart_cnt--;
1877ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
1878ef27340cSAdrian Chadd bad0:
1879b8e788a5SAdrian Chadd 	ifp->if_oerrors++;
1880b8e788a5SAdrian Chadd 	sc->sc_stats.ast_tx_raw_fail++;
1881b8e788a5SAdrian Chadd 	ieee80211_free_node(ni);
1882ef27340cSAdrian Chadd 
1883b8e788a5SAdrian Chadd 	return error;
1884b8e788a5SAdrian Chadd }
1885eb6f0de0SAdrian Chadd 
1886eb6f0de0SAdrian Chadd /* Some helper functions */
1887eb6f0de0SAdrian Chadd 
1888eb6f0de0SAdrian Chadd /*
1889eb6f0de0SAdrian Chadd  * ADDBA (and potentially others) need to be placed in the same
1890eb6f0de0SAdrian Chadd  * hardware queue as the TID/node it's relating to. This is so
1891eb6f0de0SAdrian Chadd  * it goes out after any pending non-aggregate frames to the
1892eb6f0de0SAdrian Chadd  * same node/TID.
1893eb6f0de0SAdrian Chadd  *
1894eb6f0de0SAdrian Chadd  * If this isn't done, the ADDBA can go out before the frames
1895eb6f0de0SAdrian Chadd  * queued in hardware. Even though these frames have a sequence
1896eb6f0de0SAdrian Chadd  * number -earlier- than the ADDBA can be transmitted (but
1897eb6f0de0SAdrian Chadd  * no frames whose sequence numbers are after the ADDBA should
1898eb6f0de0SAdrian Chadd  * be!) they'll arrive after the ADDBA - and the receiving end
1899eb6f0de0SAdrian Chadd  * will simply drop them as being out of the BAW.
1900eb6f0de0SAdrian Chadd  *
1901eb6f0de0SAdrian Chadd  * The frames can't be appended to the TID software queue - it'll
1902eb6f0de0SAdrian Chadd  * never be sent out. So these frames have to be directly
1903eb6f0de0SAdrian Chadd  * dispatched to the hardware, rather than queued in software.
1904eb6f0de0SAdrian Chadd  * So if this function returns true, the TXQ has to be
1905eb6f0de0SAdrian Chadd  * overridden and it has to be directly dispatched.
1906eb6f0de0SAdrian Chadd  *
1907eb6f0de0SAdrian Chadd  * It's a dirty hack, but someone's gotta do it.
1908eb6f0de0SAdrian Chadd  */
1909eb6f0de0SAdrian Chadd 
1910eb6f0de0SAdrian Chadd /*
1911eb6f0de0SAdrian Chadd  * XXX doesn't belong here!
1912eb6f0de0SAdrian Chadd  */
1913eb6f0de0SAdrian Chadd static int
1914eb6f0de0SAdrian Chadd ieee80211_is_action(struct ieee80211_frame *wh)
1915eb6f0de0SAdrian Chadd {
1916eb6f0de0SAdrian Chadd 	/* Type: Management frame? */
1917eb6f0de0SAdrian Chadd 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) !=
1918eb6f0de0SAdrian Chadd 	    IEEE80211_FC0_TYPE_MGT)
1919eb6f0de0SAdrian Chadd 		return 0;
1920eb6f0de0SAdrian Chadd 
1921eb6f0de0SAdrian Chadd 	/* Subtype: Action frame? */
1922eb6f0de0SAdrian Chadd 	if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) !=
1923eb6f0de0SAdrian Chadd 	    IEEE80211_FC0_SUBTYPE_ACTION)
1924eb6f0de0SAdrian Chadd 		return 0;
1925eb6f0de0SAdrian Chadd 
1926eb6f0de0SAdrian Chadd 	return 1;
1927eb6f0de0SAdrian Chadd }
1928eb6f0de0SAdrian Chadd 
1929eb6f0de0SAdrian Chadd #define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
1930eb6f0de0SAdrian Chadd /*
1931eb6f0de0SAdrian Chadd  * Return an alternate TID for ADDBA request frames.
1932eb6f0de0SAdrian Chadd  *
1933eb6f0de0SAdrian Chadd  * Yes, this likely should be done in the net80211 layer.
1934eb6f0de0SAdrian Chadd  */
1935eb6f0de0SAdrian Chadd static int
1936eb6f0de0SAdrian Chadd ath_tx_action_frame_override_queue(struct ath_softc *sc,
1937eb6f0de0SAdrian Chadd     struct ieee80211_node *ni,
1938eb6f0de0SAdrian Chadd     struct mbuf *m0, int *tid)
1939eb6f0de0SAdrian Chadd {
1940eb6f0de0SAdrian Chadd 	struct ieee80211_frame *wh = mtod(m0, struct ieee80211_frame *);
1941eb6f0de0SAdrian Chadd 	struct ieee80211_action_ba_addbarequest *ia;
1942eb6f0de0SAdrian Chadd 	uint8_t *frm;
1943eb6f0de0SAdrian Chadd 	uint16_t baparamset;
1944eb6f0de0SAdrian Chadd 
1945eb6f0de0SAdrian Chadd 	/* Not action frame? Bail */
1946eb6f0de0SAdrian Chadd 	if (! ieee80211_is_action(wh))
1947eb6f0de0SAdrian Chadd 		return 0;
1948eb6f0de0SAdrian Chadd 
1949eb6f0de0SAdrian Chadd 	/* XXX Not needed for frames we send? */
1950eb6f0de0SAdrian Chadd #if 0
1951eb6f0de0SAdrian Chadd 	/* Correct length? */
1952eb6f0de0SAdrian Chadd 	if (! ieee80211_parse_action(ni, m))
1953eb6f0de0SAdrian Chadd 		return 0;
1954eb6f0de0SAdrian Chadd #endif
1955eb6f0de0SAdrian Chadd 
1956eb6f0de0SAdrian Chadd 	/* Extract out action frame */
1957eb6f0de0SAdrian Chadd 	frm = (u_int8_t *)&wh[1];
1958eb6f0de0SAdrian Chadd 	ia = (struct ieee80211_action_ba_addbarequest *) frm;
1959eb6f0de0SAdrian Chadd 
1960eb6f0de0SAdrian Chadd 	/* Not ADDBA? Bail */
1961eb6f0de0SAdrian Chadd 	if (ia->rq_header.ia_category != IEEE80211_ACTION_CAT_BA)
1962eb6f0de0SAdrian Chadd 		return 0;
1963eb6f0de0SAdrian Chadd 	if (ia->rq_header.ia_action != IEEE80211_ACTION_BA_ADDBA_REQUEST)
1964eb6f0de0SAdrian Chadd 		return 0;
1965eb6f0de0SAdrian Chadd 
1966eb6f0de0SAdrian Chadd 	/* Extract TID, return it */
1967eb6f0de0SAdrian Chadd 	baparamset = le16toh(ia->rq_baparamset);
1968eb6f0de0SAdrian Chadd 	*tid = (int) MS(baparamset, IEEE80211_BAPS_TID);
1969eb6f0de0SAdrian Chadd 
1970eb6f0de0SAdrian Chadd 	return 1;
1971eb6f0de0SAdrian Chadd }
1972eb6f0de0SAdrian Chadd #undef	MS
1973eb6f0de0SAdrian Chadd 
1974eb6f0de0SAdrian Chadd /* Per-node software queue operations */
1975eb6f0de0SAdrian Chadd 
1976eb6f0de0SAdrian Chadd /*
1977eb6f0de0SAdrian Chadd  * Add the current packet to the given BAW.
1978eb6f0de0SAdrian Chadd  * It is assumed that the current packet
1979eb6f0de0SAdrian Chadd  *
1980eb6f0de0SAdrian Chadd  * + fits inside the BAW;
1981eb6f0de0SAdrian Chadd  * + already has had a sequence number allocated.
1982eb6f0de0SAdrian Chadd  *
1983eb6f0de0SAdrian Chadd  * Since the BAW status may be modified by both the ath task and
1984eb6f0de0SAdrian Chadd  * the net80211/ifnet contexts, the TID must be locked.
1985eb6f0de0SAdrian Chadd  */
1986eb6f0de0SAdrian Chadd void
1987eb6f0de0SAdrian Chadd ath_tx_addto_baw(struct ath_softc *sc, struct ath_node *an,
1988eb6f0de0SAdrian Chadd     struct ath_tid *tid, struct ath_buf *bf)
1989eb6f0de0SAdrian Chadd {
1990eb6f0de0SAdrian Chadd 	int index, cindex;
1991eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
1992eb6f0de0SAdrian Chadd 
1993eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
1994eb6f0de0SAdrian Chadd 
1995eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_isretried)
1996eb6f0de0SAdrian Chadd 		return;
1997eb6f0de0SAdrian Chadd 
19980b96ef63SAdrian Chadd 	/*
19990b96ef63SAdrian Chadd 	 * If this occurs we're in a lot of trouble.  We should try to
20000b96ef63SAdrian Chadd 	 * recover from this without the session hanging?
20010b96ef63SAdrian Chadd 	 */
20020b96ef63SAdrian Chadd 	if (! bf->bf_state.bfs_seqno_assigned) {
20030b96ef63SAdrian Chadd 		device_printf(sc->sc_dev,
20040b96ef63SAdrian Chadd 		    "%s: bf=%p, seqno_assigned is 0?!\n", __func__, bf);
20050b96ef63SAdrian Chadd 		return;
20060b96ef63SAdrian Chadd 	}
20070b96ef63SAdrian Chadd 
2008eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
2009eb6f0de0SAdrian Chadd 
2010eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_addedbaw)
2011eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
20120b96ef63SAdrian Chadd 		    "%s: re-added? bf=%p, tid=%d, seqno %d; window %d:%d; "
2013d4365d16SAdrian Chadd 		    "baw head=%d tail=%d\n",
20140b96ef63SAdrian Chadd 		    __func__, bf, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
2015d4365d16SAdrian Chadd 		    tap->txa_start, tap->txa_wnd, tid->baw_head,
2016d4365d16SAdrian Chadd 		    tid->baw_tail);
2017eb6f0de0SAdrian Chadd 
2018eb6f0de0SAdrian Chadd 	/*
20190b96ef63SAdrian Chadd 	 * Verify that the given sequence number is not outside of the
20200b96ef63SAdrian Chadd 	 * BAW.  Complain loudly if that's the case.
20210b96ef63SAdrian Chadd 	 */
20220b96ef63SAdrian Chadd 	if (! BAW_WITHIN(tap->txa_start, tap->txa_wnd,
20230b96ef63SAdrian Chadd 	    SEQNO(bf->bf_state.bfs_seqno))) {
20240b96ef63SAdrian Chadd 		device_printf(sc->sc_dev,
20250b96ef63SAdrian Chadd 		    "%s: bf=%p: outside of BAW?? tid=%d, seqno %d; window %d:%d; "
20260b96ef63SAdrian Chadd 		    "baw head=%d tail=%d\n",
20270b96ef63SAdrian Chadd 		    __func__, bf, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
20280b96ef63SAdrian Chadd 		    tap->txa_start, tap->txa_wnd, tid->baw_head,
20290b96ef63SAdrian Chadd 		    tid->baw_tail);
20300b96ef63SAdrian Chadd 
20310b96ef63SAdrian Chadd 	}
20320b96ef63SAdrian Chadd 
20330b96ef63SAdrian Chadd 	/*
2034eb6f0de0SAdrian Chadd 	 * ni->ni_txseqs[] is the currently allocated seqno.
2035eb6f0de0SAdrian Chadd 	 * the txa state contains the current baw start.
2036eb6f0de0SAdrian Chadd 	 */
2037eb6f0de0SAdrian Chadd 	index  = ATH_BA_INDEX(tap->txa_start, SEQNO(bf->bf_state.bfs_seqno));
2038eb6f0de0SAdrian Chadd 	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
2039eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
20400b96ef63SAdrian Chadd 	    "%s: bf=%p, tid=%d, seqno %d; window %d:%d; index=%d cindex=%d "
2041d4365d16SAdrian Chadd 	    "baw head=%d tail=%d\n",
20420b96ef63SAdrian Chadd 	    __func__, bf, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
2043d4365d16SAdrian Chadd 	    tap->txa_start, tap->txa_wnd, index, cindex, tid->baw_head,
2044d4365d16SAdrian Chadd 	    tid->baw_tail);
2045eb6f0de0SAdrian Chadd 
2046eb6f0de0SAdrian Chadd 
2047eb6f0de0SAdrian Chadd #if 0
2048eb6f0de0SAdrian Chadd 	assert(tid->tx_buf[cindex] == NULL);
2049eb6f0de0SAdrian Chadd #endif
2050eb6f0de0SAdrian Chadd 	if (tid->tx_buf[cindex] != NULL) {
2051eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
2052eb6f0de0SAdrian Chadd 		    "%s: ba packet dup (index=%d, cindex=%d, "
2053eb6f0de0SAdrian Chadd 		    "head=%d, tail=%d)\n",
2054eb6f0de0SAdrian Chadd 		    __func__, index, cindex, tid->baw_head, tid->baw_tail);
2055eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
2056eb6f0de0SAdrian Chadd 		    "%s: BA bf: %p; seqno=%d ; new bf: %p; seqno=%d\n",
2057eb6f0de0SAdrian Chadd 		    __func__,
2058eb6f0de0SAdrian Chadd 		    tid->tx_buf[cindex],
2059eb6f0de0SAdrian Chadd 		    SEQNO(tid->tx_buf[cindex]->bf_state.bfs_seqno),
2060eb6f0de0SAdrian Chadd 		    bf,
2061eb6f0de0SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno)
2062eb6f0de0SAdrian Chadd 		);
2063eb6f0de0SAdrian Chadd 	}
2064eb6f0de0SAdrian Chadd 	tid->tx_buf[cindex] = bf;
2065eb6f0de0SAdrian Chadd 
2066d4365d16SAdrian Chadd 	if (index >= ((tid->baw_tail - tid->baw_head) &
2067d4365d16SAdrian Chadd 	    (ATH_TID_MAX_BUFS - 1))) {
2068eb6f0de0SAdrian Chadd 		tid->baw_tail = cindex;
2069eb6f0de0SAdrian Chadd 		INCR(tid->baw_tail, ATH_TID_MAX_BUFS);
2070eb6f0de0SAdrian Chadd 	}
2071eb6f0de0SAdrian Chadd }
2072eb6f0de0SAdrian Chadd 
2073eb6f0de0SAdrian Chadd /*
207438962489SAdrian Chadd  * Flip the BAW buffer entry over from the existing one to the new one.
207538962489SAdrian Chadd  *
207638962489SAdrian Chadd  * When software retransmitting a (sub-)frame, it is entirely possible that
207738962489SAdrian Chadd  * the frame ath_buf is marked as BUSY and can't be immediately reused.
207838962489SAdrian Chadd  * In that instance the buffer is cloned and the new buffer is used for
207938962489SAdrian Chadd  * retransmit. We thus need to update the ath_buf slot in the BAW buf
208038962489SAdrian Chadd  * tracking array to maintain consistency.
208138962489SAdrian Chadd  */
208238962489SAdrian Chadd static void
208338962489SAdrian Chadd ath_tx_switch_baw_buf(struct ath_softc *sc, struct ath_node *an,
208438962489SAdrian Chadd     struct ath_tid *tid, struct ath_buf *old_bf, struct ath_buf *new_bf)
208538962489SAdrian Chadd {
208638962489SAdrian Chadd 	int index, cindex;
208738962489SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
208838962489SAdrian Chadd 	int seqno = SEQNO(old_bf->bf_state.bfs_seqno);
208938962489SAdrian Chadd 
209038962489SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
209138962489SAdrian Chadd 
209238962489SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
209338962489SAdrian Chadd 	index  = ATH_BA_INDEX(tap->txa_start, seqno);
209438962489SAdrian Chadd 	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
209538962489SAdrian Chadd 
209638962489SAdrian Chadd 	/*
209738962489SAdrian Chadd 	 * Just warn for now; if it happens then we should find out
209838962489SAdrian Chadd 	 * about it. It's highly likely the aggregation session will
209938962489SAdrian Chadd 	 * soon hang.
210038962489SAdrian Chadd 	 */
210138962489SAdrian Chadd 	if (old_bf->bf_state.bfs_seqno != new_bf->bf_state.bfs_seqno) {
210238962489SAdrian Chadd 		device_printf(sc->sc_dev, "%s: retransmitted buffer"
210338962489SAdrian Chadd 		    " has mismatching seqno's, BA session may hang.\n",
210438962489SAdrian Chadd 		    __func__);
210538962489SAdrian Chadd 		device_printf(sc->sc_dev, "%s: old seqno=%d, new_seqno=%d\n",
210638962489SAdrian Chadd 		    __func__,
210738962489SAdrian Chadd 		    old_bf->bf_state.bfs_seqno,
210838962489SAdrian Chadd 		    new_bf->bf_state.bfs_seqno);
210938962489SAdrian Chadd 	}
211038962489SAdrian Chadd 
211138962489SAdrian Chadd 	if (tid->tx_buf[cindex] != old_bf) {
211238962489SAdrian Chadd 		device_printf(sc->sc_dev, "%s: ath_buf pointer incorrect; "
211338962489SAdrian Chadd 		    " has m BA session may hang.\n",
211438962489SAdrian Chadd 		    __func__);
211538962489SAdrian Chadd 		device_printf(sc->sc_dev, "%s: old bf=%p, new bf=%p\n",
211638962489SAdrian Chadd 		    __func__,
211738962489SAdrian Chadd 		    old_bf, new_bf);
211838962489SAdrian Chadd 	}
211938962489SAdrian Chadd 
212038962489SAdrian Chadd 	tid->tx_buf[cindex] = new_bf;
212138962489SAdrian Chadd }
212238962489SAdrian Chadd 
212338962489SAdrian Chadd /*
2124eb6f0de0SAdrian Chadd  * seq_start - left edge of BAW
2125eb6f0de0SAdrian Chadd  * seq_next - current/next sequence number to allocate
2126eb6f0de0SAdrian Chadd  *
2127eb6f0de0SAdrian Chadd  * Since the BAW status may be modified by both the ath task and
2128eb6f0de0SAdrian Chadd  * the net80211/ifnet contexts, the TID must be locked.
2129eb6f0de0SAdrian Chadd  */
2130eb6f0de0SAdrian Chadd static void
2131eb6f0de0SAdrian Chadd ath_tx_update_baw(struct ath_softc *sc, struct ath_node *an,
2132eb6f0de0SAdrian Chadd     struct ath_tid *tid, const struct ath_buf *bf)
2133eb6f0de0SAdrian Chadd {
2134eb6f0de0SAdrian Chadd 	int index, cindex;
2135eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
2136eb6f0de0SAdrian Chadd 	int seqno = SEQNO(bf->bf_state.bfs_seqno);
2137eb6f0de0SAdrian Chadd 
2138eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
2139eb6f0de0SAdrian Chadd 
2140eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
2141eb6f0de0SAdrian Chadd 	index  = ATH_BA_INDEX(tap->txa_start, seqno);
2142eb6f0de0SAdrian Chadd 	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
2143eb6f0de0SAdrian Chadd 
2144eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
21450b96ef63SAdrian Chadd 	    "%s: bf=%p: tid=%d, baw=%d:%d, seqno=%d, index=%d, cindex=%d, "
2146d4365d16SAdrian Chadd 	    "baw head=%d, tail=%d\n",
21470b96ef63SAdrian Chadd 	    __func__, bf, tid->tid, tap->txa_start, tap->txa_wnd, seqno, index,
2148eb6f0de0SAdrian Chadd 	    cindex, tid->baw_head, tid->baw_tail);
2149eb6f0de0SAdrian Chadd 
2150eb6f0de0SAdrian Chadd 	/*
2151eb6f0de0SAdrian Chadd 	 * If this occurs then we have a big problem - something else
2152eb6f0de0SAdrian Chadd 	 * has slid tap->txa_start along without updating the BAW
2153eb6f0de0SAdrian Chadd 	 * tracking start/end pointers. Thus the TX BAW state is now
2154eb6f0de0SAdrian Chadd 	 * completely busted.
2155eb6f0de0SAdrian Chadd 	 *
2156eb6f0de0SAdrian Chadd 	 * But for now, since I haven't yet fixed TDMA and buffer cloning,
2157eb6f0de0SAdrian Chadd 	 * it's quite possible that a cloned buffer is making its way
2158eb6f0de0SAdrian Chadd 	 * here and causing it to fire off. Disable TDMA for now.
2159eb6f0de0SAdrian Chadd 	 */
2160eb6f0de0SAdrian Chadd 	if (tid->tx_buf[cindex] != bf) {
2161eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
2162eb6f0de0SAdrian Chadd 		    "%s: comp bf=%p, seq=%d; slot bf=%p, seqno=%d\n",
2163eb6f0de0SAdrian Chadd 		    __func__,
2164eb6f0de0SAdrian Chadd 		    bf, SEQNO(bf->bf_state.bfs_seqno),
2165eb6f0de0SAdrian Chadd 		    tid->tx_buf[cindex],
2166eb6f0de0SAdrian Chadd 		    SEQNO(tid->tx_buf[cindex]->bf_state.bfs_seqno));
2167eb6f0de0SAdrian Chadd 	}
2168eb6f0de0SAdrian Chadd 
2169eb6f0de0SAdrian Chadd 	tid->tx_buf[cindex] = NULL;
2170eb6f0de0SAdrian Chadd 
2171d4365d16SAdrian Chadd 	while (tid->baw_head != tid->baw_tail &&
2172d4365d16SAdrian Chadd 	    !tid->tx_buf[tid->baw_head]) {
2173eb6f0de0SAdrian Chadd 		INCR(tap->txa_start, IEEE80211_SEQ_RANGE);
2174eb6f0de0SAdrian Chadd 		INCR(tid->baw_head, ATH_TID_MAX_BUFS);
2175eb6f0de0SAdrian Chadd 	}
2176d4365d16SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2177d4365d16SAdrian Chadd 	    "%s: baw is now %d:%d, baw head=%d\n",
2178eb6f0de0SAdrian Chadd 	    __func__, tap->txa_start, tap->txa_wnd, tid->baw_head);
2179eb6f0de0SAdrian Chadd }
2180eb6f0de0SAdrian Chadd 
2181eb6f0de0SAdrian Chadd /*
2182eb6f0de0SAdrian Chadd  * Mark the current node/TID as ready to TX.
2183eb6f0de0SAdrian Chadd  *
2184eb6f0de0SAdrian Chadd  * This is done to make it easy for the software scheduler to
2185eb6f0de0SAdrian Chadd  * find which nodes have data to send.
2186eb6f0de0SAdrian Chadd  *
2187eb6f0de0SAdrian Chadd  * The TXQ lock must be held.
2188eb6f0de0SAdrian Chadd  */
2189eb6f0de0SAdrian Chadd static void
2190eb6f0de0SAdrian Chadd ath_tx_tid_sched(struct ath_softc *sc, struct ath_tid *tid)
2191eb6f0de0SAdrian Chadd {
2192eb6f0de0SAdrian Chadd 	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
2193eb6f0de0SAdrian Chadd 
2194eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
2195eb6f0de0SAdrian Chadd 
2196eb6f0de0SAdrian Chadd 	if (tid->paused)
2197eb6f0de0SAdrian Chadd 		return;		/* paused, can't schedule yet */
2198eb6f0de0SAdrian Chadd 
2199eb6f0de0SAdrian Chadd 	if (tid->sched)
2200eb6f0de0SAdrian Chadd 		return;		/* already scheduled */
2201eb6f0de0SAdrian Chadd 
2202eb6f0de0SAdrian Chadd 	tid->sched = 1;
2203eb6f0de0SAdrian Chadd 
2204eb6f0de0SAdrian Chadd 	TAILQ_INSERT_TAIL(&txq->axq_tidq, tid, axq_qelem);
2205eb6f0de0SAdrian Chadd }
2206eb6f0de0SAdrian Chadd 
2207eb6f0de0SAdrian Chadd /*
2208eb6f0de0SAdrian Chadd  * Mark the current node as no longer needing to be polled for
2209eb6f0de0SAdrian Chadd  * TX packets.
2210eb6f0de0SAdrian Chadd  *
2211eb6f0de0SAdrian Chadd  * The TXQ lock must be held.
2212eb6f0de0SAdrian Chadd  */
2213eb6f0de0SAdrian Chadd static void
2214eb6f0de0SAdrian Chadd ath_tx_tid_unsched(struct ath_softc *sc, struct ath_tid *tid)
2215eb6f0de0SAdrian Chadd {
2216eb6f0de0SAdrian Chadd 	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
2217eb6f0de0SAdrian Chadd 
2218eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
2219eb6f0de0SAdrian Chadd 
2220eb6f0de0SAdrian Chadd 	if (tid->sched == 0)
2221eb6f0de0SAdrian Chadd 		return;
2222eb6f0de0SAdrian Chadd 
2223eb6f0de0SAdrian Chadd 	tid->sched = 0;
2224eb6f0de0SAdrian Chadd 	TAILQ_REMOVE(&txq->axq_tidq, tid, axq_qelem);
2225eb6f0de0SAdrian Chadd }
2226eb6f0de0SAdrian Chadd 
2227eb6f0de0SAdrian Chadd /*
22280b96ef63SAdrian Chadd  * Return whether a sequence number is actually required.
22290b96ef63SAdrian Chadd  *
22300b96ef63SAdrian Chadd  * A sequence number must only be allocated at the time that a frame
22310b96ef63SAdrian Chadd  * is considered for addition to the BAW/aggregate and being TXed.
22320b96ef63SAdrian Chadd  * The sequence number must not be allocated before the frame
22330b96ef63SAdrian Chadd  * is added to the BAW (protected by the same lock instance)
22340b96ef63SAdrian Chadd  * otherwise a the multi-entrant TX path may result in a later seqno
22350b96ef63SAdrian Chadd  * being added to the BAW first.  The subsequent addition of the
22360b96ef63SAdrian Chadd  * earlier seqno would then not go into the BAW as it's now outside
22370b96ef63SAdrian Chadd  * of said BAW.
22380b96ef63SAdrian Chadd  *
22390b96ef63SAdrian Chadd  * This routine is used by ath_tx_start() to mark whether the frame
22400b96ef63SAdrian Chadd  * should get a sequence number before adding it to the BAW.
22410b96ef63SAdrian Chadd  *
22420b96ef63SAdrian Chadd  * Then the actual aggregate TX routines will check whether this
22430b96ef63SAdrian Chadd  * flag is set and if the frame needs to go into the BAW, it'll
22440b96ef63SAdrian Chadd  * have a sequence number allocated for it.
22450b96ef63SAdrian Chadd  */
22460b96ef63SAdrian Chadd static int
22470b96ef63SAdrian Chadd ath_tx_seqno_required(struct ath_softc *sc, struct ieee80211_node *ni,
22480b96ef63SAdrian Chadd     struct ath_buf *bf, struct mbuf *m0)
22490b96ef63SAdrian Chadd {
22500b96ef63SAdrian Chadd 	const struct ieee80211_frame *wh;
22510b96ef63SAdrian Chadd 	uint8_t subtype;
22520b96ef63SAdrian Chadd 
22530b96ef63SAdrian Chadd 	wh = mtod(m0, const struct ieee80211_frame *);
22540b96ef63SAdrian Chadd 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
22550b96ef63SAdrian Chadd 
22560b96ef63SAdrian Chadd 	/* XXX assert txq lock */
22570b96ef63SAdrian Chadd 	/* XXX assert ampdu is set */
22580b96ef63SAdrian Chadd 
22590b96ef63SAdrian Chadd 	return ((IEEE80211_QOS_HAS_SEQ(wh) &&
22600b96ef63SAdrian Chadd 	    subtype != IEEE80211_FC0_SUBTYPE_QOS_NULL));
22610b96ef63SAdrian Chadd }
22620b96ef63SAdrian Chadd 
22630b96ef63SAdrian Chadd /*
2264eb6f0de0SAdrian Chadd  * Assign a sequence number manually to the given frame.
2265eb6f0de0SAdrian Chadd  *
2266eb6f0de0SAdrian Chadd  * This should only be called for A-MPDU TX frames.
22670b96ef63SAdrian Chadd  *
22680b96ef63SAdrian Chadd  * If this is called after the initial frame setup, make sure you've flushed
22690b96ef63SAdrian Chadd  * the DMA map or you'll risk sending stale data to the NIC.  This routine
22700b96ef63SAdrian Chadd  * updates the actual frame contents with the relevant seqno.
2271eb6f0de0SAdrian Chadd  */
22720b96ef63SAdrian Chadd int
2273eb6f0de0SAdrian Chadd ath_tx_tid_seqno_assign(struct ath_softc *sc, struct ieee80211_node *ni,
2274eb6f0de0SAdrian Chadd     struct ath_buf *bf, struct mbuf *m0)
2275eb6f0de0SAdrian Chadd {
2276eb6f0de0SAdrian Chadd 	struct ieee80211_frame *wh;
2277eb6f0de0SAdrian Chadd 	int tid, pri;
2278eb6f0de0SAdrian Chadd 	ieee80211_seq seqno;
2279eb6f0de0SAdrian Chadd 	uint8_t subtype;
2280eb6f0de0SAdrian Chadd 
2281eb6f0de0SAdrian Chadd 	/* TID lookup */
2282eb6f0de0SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
2283eb6f0de0SAdrian Chadd 	pri = M_WME_GETAC(m0);			/* honor classification */
2284eb6f0de0SAdrian Chadd 	tid = WME_AC_TO_TID(pri);
22850b96ef63SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX,
22860b96ef63SAdrian Chadd 	    "%s: bf=%p, pri=%d, tid=%d, qos has seq=%d\n",
22870b96ef63SAdrian Chadd 	    __func__, bf, pri, tid, IEEE80211_QOS_HAS_SEQ(wh));
22880b96ef63SAdrian Chadd 
22890b96ef63SAdrian Chadd 	if (! bf->bf_state.bfs_need_seqno) {
22900b96ef63SAdrian Chadd 		device_printf(sc->sc_dev, "%s: bf=%p: need_seqno not set?!\n",
22910b96ef63SAdrian Chadd 		    __func__, bf);
22920b96ef63SAdrian Chadd 		return -1;
22930b96ef63SAdrian Chadd 	}
22940b96ef63SAdrian Chadd 	/* XXX check for bfs_need_seqno? */
22950b96ef63SAdrian Chadd 	if (bf->bf_state.bfs_seqno_assigned) {
22960b96ef63SAdrian Chadd 		device_printf(sc->sc_dev,
22970b96ef63SAdrian Chadd 		    "%s: bf=%p: seqno already assigned (%d)?!\n",
22980b96ef63SAdrian Chadd 		    __func__, bf, SEQNO(bf->bf_state.bfs_seqno));
22990b96ef63SAdrian Chadd 		return bf->bf_state.bfs_seqno >> IEEE80211_SEQ_SEQ_SHIFT;
23000b96ef63SAdrian Chadd 	}
2301eb6f0de0SAdrian Chadd 
2302eb6f0de0SAdrian Chadd 	/* XXX Is it a control frame? Ignore */
2303eb6f0de0SAdrian Chadd 
2304eb6f0de0SAdrian Chadd 	/* Does the packet require a sequence number? */
2305eb6f0de0SAdrian Chadd 	if (! IEEE80211_QOS_HAS_SEQ(wh))
2306eb6f0de0SAdrian Chadd 		return -1;
2307eb6f0de0SAdrian Chadd 
2308eb6f0de0SAdrian Chadd 	/*
2309eb6f0de0SAdrian Chadd 	 * Is it a QOS NULL Data frame? Give it a sequence number from
2310eb6f0de0SAdrian Chadd 	 * the default TID (IEEE80211_NONQOS_TID.)
2311eb6f0de0SAdrian Chadd 	 *
2312eb6f0de0SAdrian Chadd 	 * The RX path of everything I've looked at doesn't include the NULL
2313eb6f0de0SAdrian Chadd 	 * data frame sequence number in the aggregation state updates, so
2314eb6f0de0SAdrian Chadd 	 * assigning it a sequence number there will cause a BAW hole on the
2315eb6f0de0SAdrian Chadd 	 * RX side.
2316eb6f0de0SAdrian Chadd 	 */
2317eb6f0de0SAdrian Chadd 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
2318eb6f0de0SAdrian Chadd 	if (subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL) {
2319eb6f0de0SAdrian Chadd 		seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID];
2320eb6f0de0SAdrian Chadd 		INCR(ni->ni_txseqs[IEEE80211_NONQOS_TID], IEEE80211_SEQ_RANGE);
2321eb6f0de0SAdrian Chadd 	} else {
2322eb6f0de0SAdrian Chadd 		/* Manually assign sequence number */
2323eb6f0de0SAdrian Chadd 		seqno = ni->ni_txseqs[tid];
2324eb6f0de0SAdrian Chadd 		INCR(ni->ni_txseqs[tid], IEEE80211_SEQ_RANGE);
2325eb6f0de0SAdrian Chadd 	}
2326eb6f0de0SAdrian Chadd 	*(uint16_t *)&wh->i_seq[0] = htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
2327eb6f0de0SAdrian Chadd 	M_SEQNO_SET(m0, seqno);
23280b96ef63SAdrian Chadd 	bf->bf_state.bfs_seqno = seqno << IEEE80211_SEQ_SEQ_SHIFT;
23290b96ef63SAdrian Chadd 	bf->bf_state.bfs_seqno_assigned = 1;
2330eb6f0de0SAdrian Chadd 
2331eb6f0de0SAdrian Chadd 	/* Return so caller can do something with it if needed */
23320b96ef63SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bf=%p:  -> seqno=%d\n",
23330b96ef63SAdrian Chadd 	    __func__,
23340b96ef63SAdrian Chadd 	    bf,
23350b96ef63SAdrian Chadd 	    seqno);
2336eb6f0de0SAdrian Chadd 	return seqno;
2337eb6f0de0SAdrian Chadd }
2338eb6f0de0SAdrian Chadd 
2339eb6f0de0SAdrian Chadd /*
2340eb6f0de0SAdrian Chadd  * Attempt to direct dispatch an aggregate frame to hardware.
2341eb6f0de0SAdrian Chadd  * If the frame is out of BAW, queue.
2342eb6f0de0SAdrian Chadd  * Otherwise, schedule it as a single frame.
2343eb6f0de0SAdrian Chadd  */
2344eb6f0de0SAdrian Chadd static void
2345eb6f0de0SAdrian Chadd ath_tx_xmit_aggr(struct ath_softc *sc, struct ath_node *an, struct ath_buf *bf)
2346eb6f0de0SAdrian Chadd {
23470b96ef63SAdrian Chadd 	struct ieee80211_node *ni = &an->an_node;
2348eb6f0de0SAdrian Chadd 	struct ath_tid *tid = &an->an_tid[bf->bf_state.bfs_tid];
2349eb6f0de0SAdrian Chadd 	struct ath_txq *txq = bf->bf_state.bfs_txq;
2350eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
23510b96ef63SAdrian Chadd 	int seqno;
2352eb6f0de0SAdrian Chadd 
2353eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
2354eb6f0de0SAdrian Chadd 
2355eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
2356eb6f0de0SAdrian Chadd 
2357eb6f0de0SAdrian Chadd 	/* paused? queue */
2358eb6f0de0SAdrian Chadd 	if (tid->paused) {
2359eb6f0de0SAdrian Chadd 		ATH_TXQ_INSERT_TAIL(tid, bf, bf_list);
2360eb6f0de0SAdrian Chadd 		return;
2361eb6f0de0SAdrian Chadd 	}
2362eb6f0de0SAdrian Chadd 
23630b96ef63SAdrian Chadd 	/*
23640b96ef63SAdrian Chadd 	 * TODO: If it's _before_ the BAW left edge, complain very loudly.
23650b96ef63SAdrian Chadd 	 * This means something (else) has slid the left edge along
23660b96ef63SAdrian Chadd 	 * before we got a chance to be TXed.
23670b96ef63SAdrian Chadd 	 */
23680b96ef63SAdrian Chadd 
23690b96ef63SAdrian Chadd 	/*
23700b96ef63SAdrian Chadd 	 * Is there space in this BAW for another frame?
23710b96ef63SAdrian Chadd 	 * If not, don't bother trying to schedule it; just
23720b96ef63SAdrian Chadd 	 * throw it back on the queue.
23730b96ef63SAdrian Chadd 	 *
23740b96ef63SAdrian Chadd 	 * If we allocate the sequence number before we add
23750b96ef63SAdrian Chadd 	 * it to the BAW, we risk racing with another TX
23760b96ef63SAdrian Chadd 	 * thread that gets in a frame into the BAW with
23770b96ef63SAdrian Chadd 	 * seqno greater than ours.  We'd then fail the
23780b96ef63SAdrian Chadd 	 * below check and throw the frame on the tail of
23790b96ef63SAdrian Chadd 	 * the queue.  The sender would then have a hole.
23800b96ef63SAdrian Chadd 	 *
23810b96ef63SAdrian Chadd 	 * XXX again, we're protecting ni->ni_txseqs[tid]
23820b96ef63SAdrian Chadd 	 * behind this hardware TXQ lock, like the rest of
23830b96ef63SAdrian Chadd 	 * the TIDs that map to it.  Ugh.
23840b96ef63SAdrian Chadd 	 */
23850b96ef63SAdrian Chadd 	if (bf->bf_state.bfs_dobaw) {
23860b96ef63SAdrian Chadd 		if (! BAW_WITHIN(tap->txa_start, tap->txa_wnd,
23870b96ef63SAdrian Chadd 		    ni->ni_txseqs[bf->bf_state.bfs_tid])) {
23880b96ef63SAdrian Chadd 			ATH_TXQ_INSERT_TAIL(tid, bf, bf_list);
23890b96ef63SAdrian Chadd 			ath_tx_tid_sched(sc, tid);
23900b96ef63SAdrian Chadd 			return;
23910b96ef63SAdrian Chadd 		}
23920b96ef63SAdrian Chadd 		if (! bf->bf_state.bfs_seqno_assigned) {
23930b96ef63SAdrian Chadd 			seqno = ath_tx_tid_seqno_assign(sc, ni, bf, bf->bf_m);
23940b96ef63SAdrian Chadd 			if (seqno < 0) {
23950b96ef63SAdrian Chadd 				device_printf(sc->sc_dev,
23960b96ef63SAdrian Chadd 				    "%s: bf=%p, huh, seqno=-1?\n",
23970b96ef63SAdrian Chadd 				    __func__,
23980b96ef63SAdrian Chadd 				    bf);
23990b96ef63SAdrian Chadd 				/* XXX what can we even do here? */
24000b96ef63SAdrian Chadd 			}
24010b96ef63SAdrian Chadd 			/* Flush seqno update to RAM */
24020b96ef63SAdrian Chadd 			/*
24030b96ef63SAdrian Chadd 			 * XXX This is required because the dmasetup
24040b96ef63SAdrian Chadd 			 * XXX is done early rather than at dispatch
24050b96ef63SAdrian Chadd 			 * XXX time. Ew, we should fix this!
24060b96ef63SAdrian Chadd 			 */
24070b96ef63SAdrian Chadd 			bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
24080b96ef63SAdrian Chadd 			    BUS_DMASYNC_PREWRITE);
24090b96ef63SAdrian Chadd 		}
24100b96ef63SAdrian Chadd 	}
24110b96ef63SAdrian Chadd 
2412eb6f0de0SAdrian Chadd 	/* outside baw? queue */
2413eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_dobaw &&
2414eb6f0de0SAdrian Chadd 	    (! BAW_WITHIN(tap->txa_start, tap->txa_wnd,
2415eb6f0de0SAdrian Chadd 	    SEQNO(bf->bf_state.bfs_seqno)))) {
24160b96ef63SAdrian Chadd 		device_printf(sc->sc_dev,
24170b96ef63SAdrian Chadd 		    "%s: bf=%p, shouldn't be outside BAW now?!\n",
24180b96ef63SAdrian Chadd 		    __func__,
24190b96ef63SAdrian Chadd 		    bf);
2420eb6f0de0SAdrian Chadd 		ATH_TXQ_INSERT_TAIL(tid, bf, bf_list);
2421eb6f0de0SAdrian Chadd 		ath_tx_tid_sched(sc, tid);
2422eb6f0de0SAdrian Chadd 		return;
2423eb6f0de0SAdrian Chadd 	}
2424eb6f0de0SAdrian Chadd 
2425eb6f0de0SAdrian Chadd 	/* Direct dispatch to hardware */
2426eb6f0de0SAdrian Chadd 	ath_tx_do_ratelookup(sc, bf);
2427eb6f0de0SAdrian Chadd 	ath_tx_rate_fill_rcflags(sc, bf);
2428eb6f0de0SAdrian Chadd 	ath_tx_set_rtscts(sc, bf);
2429eb6f0de0SAdrian Chadd 	ath_tx_setds(sc, bf);
2430eb6f0de0SAdrian Chadd 	ath_tx_set_ratectrl(sc, bf->bf_node, bf);
2431eb6f0de0SAdrian Chadd 	ath_tx_chaindesclist(sc, bf);
2432eb6f0de0SAdrian Chadd 
2433eb6f0de0SAdrian Chadd 	/* Statistics */
2434eb6f0de0SAdrian Chadd 	sc->sc_aggr_stats.aggr_low_hwq_single_pkt++;
2435eb6f0de0SAdrian Chadd 
2436eb6f0de0SAdrian Chadd 	/* Track per-TID hardware queue depth correctly */
2437eb6f0de0SAdrian Chadd 	tid->hwq_depth++;
2438eb6f0de0SAdrian Chadd 
2439eb6f0de0SAdrian Chadd 	/* Add to BAW */
2440eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_dobaw) {
2441eb6f0de0SAdrian Chadd 		ath_tx_addto_baw(sc, an, tid, bf);
2442eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_addedbaw = 1;
2443eb6f0de0SAdrian Chadd 	}
2444eb6f0de0SAdrian Chadd 
2445eb6f0de0SAdrian Chadd 	/* Set completion handler, multi-frame aggregate or not */
2446eb6f0de0SAdrian Chadd 	bf->bf_comp = ath_tx_aggr_comp;
2447eb6f0de0SAdrian Chadd 
2448eb6f0de0SAdrian Chadd 	/* Hand off to hardware */
2449eb6f0de0SAdrian Chadd 	ath_tx_handoff(sc, txq, bf);
2450eb6f0de0SAdrian Chadd }
2451eb6f0de0SAdrian Chadd 
2452eb6f0de0SAdrian Chadd /*
2453eb6f0de0SAdrian Chadd  * Attempt to send the packet.
2454eb6f0de0SAdrian Chadd  * If the queue isn't busy, direct-dispatch.
2455eb6f0de0SAdrian Chadd  * If the queue is busy enough, queue the given packet on the
2456eb6f0de0SAdrian Chadd  *  relevant software queue.
2457eb6f0de0SAdrian Chadd  */
2458eb6f0de0SAdrian Chadd void
2459eb6f0de0SAdrian Chadd ath_tx_swq(struct ath_softc *sc, struct ieee80211_node *ni, struct ath_txq *txq,
2460eb6f0de0SAdrian Chadd     struct ath_buf *bf)
2461eb6f0de0SAdrian Chadd {
2462eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
2463eb6f0de0SAdrian Chadd 	struct ieee80211_frame *wh;
2464eb6f0de0SAdrian Chadd 	struct ath_tid *atid;
2465eb6f0de0SAdrian Chadd 	int pri, tid;
2466eb6f0de0SAdrian Chadd 	struct mbuf *m0 = bf->bf_m;
2467eb6f0de0SAdrian Chadd 
2468eb6f0de0SAdrian Chadd 	/* Fetch the TID - non-QoS frames get assigned to TID 16 */
2469eb6f0de0SAdrian Chadd 	wh = mtod(m0, struct ieee80211_frame *);
2470eb6f0de0SAdrian Chadd 	pri = ath_tx_getac(sc, m0);
2471eb6f0de0SAdrian Chadd 	tid = ath_tx_gettid(sc, m0);
2472eb6f0de0SAdrian Chadd 	atid = &an->an_tid[tid];
2473eb6f0de0SAdrian Chadd 
24740b96ef63SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bf=%p, pri=%d, tid=%d, qos=%d, seqno=%d\n",
24750b96ef63SAdrian Chadd 	    __func__, bf, pri, tid, IEEE80211_QOS_HAS_SEQ(wh), SEQNO(bf->bf_state.bfs_seqno));
2476eb6f0de0SAdrian Chadd 
2477eb6f0de0SAdrian Chadd 	/* Set local packet state, used to queue packets to hardware */
2478eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_tid = tid;
2479eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_txq = txq;
2480eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_pri = pri;
2481eb6f0de0SAdrian Chadd 
2482eb6f0de0SAdrian Chadd 	/*
2483eb6f0de0SAdrian Chadd 	 * If the hardware queue isn't busy, queue it directly.
2484eb6f0de0SAdrian Chadd 	 * If the hardware queue is busy, queue it.
2485eb6f0de0SAdrian Chadd 	 * If the TID is paused or the traffic it outside BAW, software
2486eb6f0de0SAdrian Chadd 	 * queue it.
2487eb6f0de0SAdrian Chadd 	 */
2488eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(txq);
2489eb6f0de0SAdrian Chadd 	if (atid->paused) {
2490eb6f0de0SAdrian Chadd 		/* TID is paused, queue */
24910b96ef63SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bf=%p: paused\n", __func__, bf);
2492eb6f0de0SAdrian Chadd 		ATH_TXQ_INSERT_TAIL(atid, bf, bf_list);
2493eb6f0de0SAdrian Chadd 	} else if (ath_tx_ampdu_pending(sc, an, tid)) {
2494eb6f0de0SAdrian Chadd 		/* AMPDU pending; queue */
24950b96ef63SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bf=%p: pending\n", __func__, bf);
2496eb6f0de0SAdrian Chadd 		ATH_TXQ_INSERT_TAIL(atid, bf, bf_list);
2497eb6f0de0SAdrian Chadd 		/* XXX sched? */
2498eb6f0de0SAdrian Chadd 	} else if (ath_tx_ampdu_running(sc, an, tid)) {
2499eb6f0de0SAdrian Chadd 		/* AMPDU running, attempt direct dispatch if possible */
2500d4365d16SAdrian Chadd 		if (txq->axq_depth < sc->sc_hwq_limit) {
2501d4365d16SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX,
25020b96ef63SAdrian Chadd 			    "%s: bf=%p: xmit_aggr\n",
25030b96ef63SAdrian Chadd 			    __func__, bf);
25040b96ef63SAdrian Chadd 			ath_tx_xmit_aggr(sc, an, bf);
2505d4365d16SAdrian Chadd 		} else {
2506d4365d16SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX,
25070b96ef63SAdrian Chadd 			    "%s: bf=%p: ampdu; swq'ing\n",
25080b96ef63SAdrian Chadd 			    __func__, bf);
2509eb6f0de0SAdrian Chadd 			ATH_TXQ_INSERT_TAIL(atid, bf, bf_list);
2510eb6f0de0SAdrian Chadd 			ath_tx_tid_sched(sc, atid);
2511eb6f0de0SAdrian Chadd 		}
2512eb6f0de0SAdrian Chadd 	} else if (txq->axq_depth < sc->sc_hwq_limit) {
2513eb6f0de0SAdrian Chadd 		/* AMPDU not running, attempt direct dispatch */
25140b96ef63SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bf=%p: xmit_normal\n", __func__, bf);
2515eb6f0de0SAdrian Chadd 		ath_tx_xmit_normal(sc, txq, bf);
2516eb6f0de0SAdrian Chadd 	} else {
2517eb6f0de0SAdrian Chadd 		/* Busy; queue */
25180b96ef63SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bf=%p: swq'ing\n", __func__, bf);
2519eb6f0de0SAdrian Chadd 		ATH_TXQ_INSERT_TAIL(atid, bf, bf_list);
2520eb6f0de0SAdrian Chadd 		ath_tx_tid_sched(sc, atid);
2521eb6f0de0SAdrian Chadd 	}
2522eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(txq);
2523eb6f0de0SAdrian Chadd }
2524eb6f0de0SAdrian Chadd 
2525eb6f0de0SAdrian Chadd /*
2526eb6f0de0SAdrian Chadd  * Do the basic frame setup stuff that's required before the frame
2527eb6f0de0SAdrian Chadd  * is added to a software queue.
2528eb6f0de0SAdrian Chadd  *
2529eb6f0de0SAdrian Chadd  * All frames get mostly the same treatment and it's done once.
2530eb6f0de0SAdrian Chadd  * Retransmits fiddle with things like the rate control setup,
2531eb6f0de0SAdrian Chadd  * setting the retransmit bit in the packet; doing relevant DMA/bus
2532eb6f0de0SAdrian Chadd  * syncing and relinking it (back) into the hardware TX queue.
2533eb6f0de0SAdrian Chadd  *
2534eb6f0de0SAdrian Chadd  * Note that this may cause the mbuf to be reallocated, so
2535eb6f0de0SAdrian Chadd  * m0 may not be valid.
2536eb6f0de0SAdrian Chadd  */
2537eb6f0de0SAdrian Chadd 
2538eb6f0de0SAdrian Chadd 
2539eb6f0de0SAdrian Chadd /*
2540eb6f0de0SAdrian Chadd  * Configure the per-TID node state.
2541eb6f0de0SAdrian Chadd  *
2542eb6f0de0SAdrian Chadd  * This likely belongs in if_ath_node.c but I can't think of anywhere
2543eb6f0de0SAdrian Chadd  * else to put it just yet.
2544eb6f0de0SAdrian Chadd  *
2545eb6f0de0SAdrian Chadd  * This sets up the SLISTs and the mutex as appropriate.
2546eb6f0de0SAdrian Chadd  */
2547eb6f0de0SAdrian Chadd void
2548eb6f0de0SAdrian Chadd ath_tx_tid_init(struct ath_softc *sc, struct ath_node *an)
2549eb6f0de0SAdrian Chadd {
2550eb6f0de0SAdrian Chadd 	int i, j;
2551eb6f0de0SAdrian Chadd 	struct ath_tid *atid;
2552eb6f0de0SAdrian Chadd 
2553eb6f0de0SAdrian Chadd 	for (i = 0; i < IEEE80211_TID_SIZE; i++) {
2554eb6f0de0SAdrian Chadd 		atid = &an->an_tid[i];
2555eb6f0de0SAdrian Chadd 		TAILQ_INIT(&atid->axq_q);
2556eb6f0de0SAdrian Chadd 		atid->tid = i;
2557eb6f0de0SAdrian Chadd 		atid->an = an;
2558eb6f0de0SAdrian Chadd 		for (j = 0; j < ATH_TID_MAX_BUFS; j++)
2559eb6f0de0SAdrian Chadd 			atid->tx_buf[j] = NULL;
2560eb6f0de0SAdrian Chadd 		atid->baw_head = atid->baw_tail = 0;
2561eb6f0de0SAdrian Chadd 		atid->paused = 0;
2562eb6f0de0SAdrian Chadd 		atid->sched = 0;
2563eb6f0de0SAdrian Chadd 		atid->hwq_depth = 0;
2564eb6f0de0SAdrian Chadd 		atid->cleanup_inprogress = 0;
2565eb6f0de0SAdrian Chadd 		if (i == IEEE80211_NONQOS_TID)
2566eb6f0de0SAdrian Chadd 			atid->ac = WME_AC_BE;
2567eb6f0de0SAdrian Chadd 		else
2568eb6f0de0SAdrian Chadd 			atid->ac = TID_TO_WME_AC(i);
2569eb6f0de0SAdrian Chadd 	}
2570eb6f0de0SAdrian Chadd }
2571eb6f0de0SAdrian Chadd 
2572eb6f0de0SAdrian Chadd /*
2573eb6f0de0SAdrian Chadd  * Pause the current TID. This stops packets from being transmitted
2574eb6f0de0SAdrian Chadd  * on it.
2575eb6f0de0SAdrian Chadd  *
2576eb6f0de0SAdrian Chadd  * Since this is also called from upper layers as well as the driver,
2577eb6f0de0SAdrian Chadd  * it will get the TID lock.
2578eb6f0de0SAdrian Chadd  */
2579eb6f0de0SAdrian Chadd static void
2580eb6f0de0SAdrian Chadd ath_tx_tid_pause(struct ath_softc *sc, struct ath_tid *tid)
2581eb6f0de0SAdrian Chadd {
2582eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[tid->ac]);
2583eb6f0de0SAdrian Chadd 	tid->paused++;
2584eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: paused = %d\n",
2585eb6f0de0SAdrian Chadd 	    __func__, tid->paused);
2586eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[tid->ac]);
2587eb6f0de0SAdrian Chadd }
2588eb6f0de0SAdrian Chadd 
2589eb6f0de0SAdrian Chadd /*
2590eb6f0de0SAdrian Chadd  * Unpause the current TID, and schedule it if needed.
2591eb6f0de0SAdrian Chadd  */
2592eb6f0de0SAdrian Chadd static void
2593eb6f0de0SAdrian Chadd ath_tx_tid_resume(struct ath_softc *sc, struct ath_tid *tid)
2594eb6f0de0SAdrian Chadd {
2595eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
2596eb6f0de0SAdrian Chadd 
2597eb6f0de0SAdrian Chadd 	tid->paused--;
2598eb6f0de0SAdrian Chadd 
2599eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: unpaused = %d\n",
2600eb6f0de0SAdrian Chadd 	    __func__, tid->paused);
2601eb6f0de0SAdrian Chadd 
2602eb6f0de0SAdrian Chadd 	if (tid->paused || tid->axq_depth == 0) {
2603eb6f0de0SAdrian Chadd 		return;
2604eb6f0de0SAdrian Chadd 	}
2605eb6f0de0SAdrian Chadd 
2606eb6f0de0SAdrian Chadd 	ath_tx_tid_sched(sc, tid);
2607eb6f0de0SAdrian Chadd 	/* Punt some frames to the hardware if needed */
2608eb6f0de0SAdrian Chadd 	ath_txq_sched(sc, sc->sc_ac2q[tid->ac]);
2609eb6f0de0SAdrian Chadd }
2610eb6f0de0SAdrian Chadd 
2611eb6f0de0SAdrian Chadd /*
2612eb6f0de0SAdrian Chadd  * Free any packets currently pending in the software TX queue.
2613eb6f0de0SAdrian Chadd  *
2614eb6f0de0SAdrian Chadd  * This will be called when a node is being deleted.
2615eb6f0de0SAdrian Chadd  *
2616eb6f0de0SAdrian Chadd  * It can also be called on an active node during an interface
2617eb6f0de0SAdrian Chadd  * reset or state transition.
2618eb6f0de0SAdrian Chadd  *
2619eb6f0de0SAdrian Chadd  * (From Linux/reference):
2620eb6f0de0SAdrian Chadd  *
2621eb6f0de0SAdrian Chadd  * TODO: For frame(s) that are in the retry state, we will reuse the
2622eb6f0de0SAdrian Chadd  * sequence number(s) without setting the retry bit. The
2623eb6f0de0SAdrian Chadd  * alternative is to give up on these and BAR the receiver's window
2624eb6f0de0SAdrian Chadd  * forward.
2625eb6f0de0SAdrian Chadd  */
2626eb6f0de0SAdrian Chadd static void
2627d4365d16SAdrian Chadd ath_tx_tid_drain(struct ath_softc *sc, struct ath_node *an,
2628d4365d16SAdrian Chadd     struct ath_tid *tid, ath_bufhead *bf_cq)
2629eb6f0de0SAdrian Chadd {
2630eb6f0de0SAdrian Chadd 	struct ath_buf *bf;
2631eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
2632eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = &an->an_node;
2633eb6f0de0SAdrian Chadd 	int t = 0;
2634eb6f0de0SAdrian Chadd 	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
2635eb6f0de0SAdrian Chadd 
2636eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
2637eb6f0de0SAdrian Chadd 
2638eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
2639eb6f0de0SAdrian Chadd 
2640eb6f0de0SAdrian Chadd 	/* Walk the queue, free frames */
2641eb6f0de0SAdrian Chadd 	for (;;) {
2642eb6f0de0SAdrian Chadd 		bf = TAILQ_FIRST(&tid->axq_q);
2643eb6f0de0SAdrian Chadd 		if (bf == NULL) {
2644eb6f0de0SAdrian Chadd 			break;
2645eb6f0de0SAdrian Chadd 		}
2646eb6f0de0SAdrian Chadd 
2647eb6f0de0SAdrian Chadd 		if (t == 0) {
2648eb6f0de0SAdrian Chadd 			device_printf(sc->sc_dev,
2649*12be5b9cSAdrian Chadd 			    "%s: node %p: bf=%p: addbaw=%d, dobaw=%d, "
2650*12be5b9cSAdrian Chadd 			    "seqno_assign=%d, seqno_required=%d, seqno=%d\n",
2651*12be5b9cSAdrian Chadd 			    __func__, ni, bf,
2652*12be5b9cSAdrian Chadd 			    bf->bf_state.bfs_addedbaw,
2653*12be5b9cSAdrian Chadd 			    bf->bf_state.bfs_dobaw,
2654*12be5b9cSAdrian Chadd 			    bf->bf_state.bfs_need_seqno,
2655*12be5b9cSAdrian Chadd 			    bf->bf_state.bfs_seqno_assigned,
2656*12be5b9cSAdrian Chadd 			    SEQNO(bf->bf_state.bfs_seqno));
2657*12be5b9cSAdrian Chadd 			device_printf(sc->sc_dev,
26580b96ef63SAdrian Chadd 			    "%s: node %p: bf=%p: tid %d: txq_depth=%d, "
2659eb6f0de0SAdrian Chadd 			    "txq_aggr_depth=%d, sched=%d, paused=%d, "
2660d4365d16SAdrian Chadd 			    "hwq_depth=%d, incomp=%d, baw_head=%d, "
2661d4365d16SAdrian Chadd 			    "baw_tail=%d txa_start=%d, ni_txseqs=%d\n",
26620b96ef63SAdrian Chadd 			     __func__, ni, bf, tid->tid, txq->axq_depth,
2663eb6f0de0SAdrian Chadd 			     txq->axq_aggr_depth, tid->sched, tid->paused,
2664eb6f0de0SAdrian Chadd 			     tid->hwq_depth, tid->incomp, tid->baw_head,
2665eb6f0de0SAdrian Chadd 			     tid->baw_tail, tap == NULL ? -1 : tap->txa_start,
2666eb6f0de0SAdrian Chadd 			     ni->ni_txseqs[tid->tid]);
2667c0711b97SAdrian Chadd 
2668c0711b97SAdrian Chadd 			/* XXX Dump the frame, see what it is? */
2669c0711b97SAdrian Chadd 			ieee80211_dump_pkt(ni->ni_ic,
2670c0711b97SAdrian Chadd 			    mtod(bf->bf_m, const uint8_t *),
2671c0711b97SAdrian Chadd 			    bf->bf_m->m_len, 0, -1);
2672c0711b97SAdrian Chadd 
26730b96ef63SAdrian Chadd 			//t = 1;
2674eb6f0de0SAdrian Chadd 		}
2675eb6f0de0SAdrian Chadd 
2676eb6f0de0SAdrian Chadd 
2677eb6f0de0SAdrian Chadd 		/*
2678eb6f0de0SAdrian Chadd 		 * If the current TID is running AMPDU, update
2679eb6f0de0SAdrian Chadd 		 * the BAW.
2680eb6f0de0SAdrian Chadd 		 */
2681eb6f0de0SAdrian Chadd 		if (ath_tx_ampdu_running(sc, an, tid->tid) &&
2682eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_dobaw) {
2683eb6f0de0SAdrian Chadd 			/*
2684eb6f0de0SAdrian Chadd 			 * Only remove the frame from the BAW if it's
2685eb6f0de0SAdrian Chadd 			 * been transmitted at least once; this means
2686eb6f0de0SAdrian Chadd 			 * the frame was in the BAW to begin with.
2687eb6f0de0SAdrian Chadd 			 */
2688eb6f0de0SAdrian Chadd 			if (bf->bf_state.bfs_retries > 0) {
2689eb6f0de0SAdrian Chadd 				ath_tx_update_baw(sc, an, tid, bf);
2690eb6f0de0SAdrian Chadd 				bf->bf_state.bfs_dobaw = 0;
2691eb6f0de0SAdrian Chadd 			}
2692eb6f0de0SAdrian Chadd 			/*
2693eb6f0de0SAdrian Chadd 			 * This has become a non-fatal error now
2694eb6f0de0SAdrian Chadd 			 */
2695eb6f0de0SAdrian Chadd 			if (! bf->bf_state.bfs_addedbaw)
2696eb6f0de0SAdrian Chadd 				device_printf(sc->sc_dev,
2697eb6f0de0SAdrian Chadd 				    "%s: wasn't added: seqno %d\n",
2698eb6f0de0SAdrian Chadd 				    __func__, SEQNO(bf->bf_state.bfs_seqno));
2699eb6f0de0SAdrian Chadd 		}
2700eb6f0de0SAdrian Chadd 		ATH_TXQ_REMOVE(tid, bf, bf_list);
2701eb6f0de0SAdrian Chadd 		TAILQ_INSERT_TAIL(bf_cq, bf, bf_list);
2702eb6f0de0SAdrian Chadd 	}
2703eb6f0de0SAdrian Chadd 
2704eb6f0de0SAdrian Chadd 	/*
2705eb6f0de0SAdrian Chadd 	 * Now that it's completed, grab the TID lock and update
2706eb6f0de0SAdrian Chadd 	 * the sequence number and BAW window.
2707eb6f0de0SAdrian Chadd 	 * Because sequence numbers have been assigned to frames
2708eb6f0de0SAdrian Chadd 	 * that haven't been sent yet, it's entirely possible
2709eb6f0de0SAdrian Chadd 	 * we'll be called with some pending frames that have not
2710eb6f0de0SAdrian Chadd 	 * been transmitted.
2711eb6f0de0SAdrian Chadd 	 *
2712eb6f0de0SAdrian Chadd 	 * The cleaner solution is to do the sequence number allocation
2713eb6f0de0SAdrian Chadd 	 * when the packet is first transmitted - and thus the "retries"
2714eb6f0de0SAdrian Chadd 	 * check above would be enough to update the BAW/seqno.
2715eb6f0de0SAdrian Chadd 	 */
2716eb6f0de0SAdrian Chadd 
2717eb6f0de0SAdrian Chadd 	/* But don't do it for non-QoS TIDs */
2718eb6f0de0SAdrian Chadd 	if (tap) {
2719eb6f0de0SAdrian Chadd #if 0
2720eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
2721eb6f0de0SAdrian Chadd 		    "%s: node %p: TID %d: sliding BAW left edge to %d\n",
2722eb6f0de0SAdrian Chadd 		    __func__, an, tid->tid, tap->txa_start);
2723eb6f0de0SAdrian Chadd #endif
2724eb6f0de0SAdrian Chadd 		ni->ni_txseqs[tid->tid] = tap->txa_start;
2725eb6f0de0SAdrian Chadd 		tid->baw_tail = tid->baw_head;
2726eb6f0de0SAdrian Chadd 	}
2727eb6f0de0SAdrian Chadd }
2728eb6f0de0SAdrian Chadd 
2729eb6f0de0SAdrian Chadd /*
2730eb6f0de0SAdrian Chadd  * Flush all software queued packets for the given node.
2731eb6f0de0SAdrian Chadd  *
2732eb6f0de0SAdrian Chadd  * This occurs when a completion handler frees the last buffer
2733eb6f0de0SAdrian Chadd  * for a node, and the node is thus freed. This causes the node
2734eb6f0de0SAdrian Chadd  * to be cleaned up, which ends up calling ath_tx_node_flush.
2735eb6f0de0SAdrian Chadd  */
2736eb6f0de0SAdrian Chadd void
2737eb6f0de0SAdrian Chadd ath_tx_node_flush(struct ath_softc *sc, struct ath_node *an)
2738eb6f0de0SAdrian Chadd {
2739eb6f0de0SAdrian Chadd 	int tid;
2740eb6f0de0SAdrian Chadd 	ath_bufhead bf_cq;
2741eb6f0de0SAdrian Chadd 	struct ath_buf *bf;
2742eb6f0de0SAdrian Chadd 
2743eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_cq);
2744eb6f0de0SAdrian Chadd 
2745eb6f0de0SAdrian Chadd 	for (tid = 0; tid < IEEE80211_TID_SIZE; tid++) {
2746eb6f0de0SAdrian Chadd 		struct ath_tid *atid = &an->an_tid[tid];
2747eb6f0de0SAdrian Chadd 		struct ath_txq *txq = sc->sc_ac2q[atid->ac];
2748eb6f0de0SAdrian Chadd 
2749eb6f0de0SAdrian Chadd 		/* Remove this tid from the list of active tids */
2750eb6f0de0SAdrian Chadd 		ATH_TXQ_LOCK(txq);
2751eb6f0de0SAdrian Chadd 		ath_tx_tid_unsched(sc, atid);
2752eb6f0de0SAdrian Chadd 
2753eb6f0de0SAdrian Chadd 		/* Free packets */
2754eb6f0de0SAdrian Chadd 		ath_tx_tid_drain(sc, an, atid, &bf_cq);
2755eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
2756eb6f0de0SAdrian Chadd 	}
2757eb6f0de0SAdrian Chadd 
2758eb6f0de0SAdrian Chadd 	/* Handle completed frames */
2759eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
2760eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
2761eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
2762eb6f0de0SAdrian Chadd 	}
2763eb6f0de0SAdrian Chadd }
2764eb6f0de0SAdrian Chadd 
2765eb6f0de0SAdrian Chadd /*
2766eb6f0de0SAdrian Chadd  * Drain all the software TXQs currently with traffic queued.
2767eb6f0de0SAdrian Chadd  */
2768eb6f0de0SAdrian Chadd void
2769eb6f0de0SAdrian Chadd ath_tx_txq_drain(struct ath_softc *sc, struct ath_txq *txq)
2770eb6f0de0SAdrian Chadd {
2771eb6f0de0SAdrian Chadd 	struct ath_tid *tid;
2772eb6f0de0SAdrian Chadd 	ath_bufhead bf_cq;
2773eb6f0de0SAdrian Chadd 	struct ath_buf *bf;
2774eb6f0de0SAdrian Chadd 
2775eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_cq);
2776eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(txq);
2777eb6f0de0SAdrian Chadd 
2778eb6f0de0SAdrian Chadd 	/*
2779eb6f0de0SAdrian Chadd 	 * Iterate over all active tids for the given txq,
2780eb6f0de0SAdrian Chadd 	 * flushing and unsched'ing them
2781eb6f0de0SAdrian Chadd 	 */
2782eb6f0de0SAdrian Chadd 	while (! TAILQ_EMPTY(&txq->axq_tidq)) {
2783eb6f0de0SAdrian Chadd 		tid = TAILQ_FIRST(&txq->axq_tidq);
2784eb6f0de0SAdrian Chadd 		ath_tx_tid_drain(sc, tid->an, tid, &bf_cq);
2785eb6f0de0SAdrian Chadd 		ath_tx_tid_unsched(sc, tid);
2786eb6f0de0SAdrian Chadd 	}
2787eb6f0de0SAdrian Chadd 
2788eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(txq);
2789eb6f0de0SAdrian Chadd 
2790eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
2791eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
2792eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
2793eb6f0de0SAdrian Chadd 	}
2794eb6f0de0SAdrian Chadd }
2795eb6f0de0SAdrian Chadd 
2796eb6f0de0SAdrian Chadd /*
2797eb6f0de0SAdrian Chadd  * Handle completion of non-aggregate session frames.
2798eb6f0de0SAdrian Chadd  */
2799eb6f0de0SAdrian Chadd void
2800eb6f0de0SAdrian Chadd ath_tx_normal_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
2801eb6f0de0SAdrian Chadd {
2802eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
2803eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
2804eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
2805eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
2806eb6f0de0SAdrian Chadd 	struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
2807eb6f0de0SAdrian Chadd 
2808eb6f0de0SAdrian Chadd 	/* The TID state is protected behind the TXQ lock */
2809eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
2810eb6f0de0SAdrian Chadd 
2811eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bf=%p: fail=%d, hwq_depth now %d\n",
2812eb6f0de0SAdrian Chadd 	    __func__, bf, fail, atid->hwq_depth - 1);
2813eb6f0de0SAdrian Chadd 
2814eb6f0de0SAdrian Chadd 	atid->hwq_depth--;
2815eb6f0de0SAdrian Chadd 	if (atid->hwq_depth < 0)
2816eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: hwq_depth < 0: %d\n",
2817eb6f0de0SAdrian Chadd 		    __func__, atid->hwq_depth);
2818eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
2819eb6f0de0SAdrian Chadd 
2820eb6f0de0SAdrian Chadd 	/*
2821eb6f0de0SAdrian Chadd 	 * punt to rate control if we're not being cleaned up
2822eb6f0de0SAdrian Chadd 	 * during a hw queue drain and the frame wanted an ACK.
2823eb6f0de0SAdrian Chadd 	 */
2824eb6f0de0SAdrian Chadd 	if (fail == 0 && ((bf->bf_txflags & HAL_TXDESC_NOACK) == 0))
2825eb6f0de0SAdrian Chadd 		ath_tx_update_ratectrl(sc, ni, bf->bf_state.bfs_rc,
2826eb6f0de0SAdrian Chadd 		    ts, bf->bf_state.bfs_pktlen,
2827eb6f0de0SAdrian Chadd 		    1, (ts->ts_status == 0) ? 0 : 1);
2828eb6f0de0SAdrian Chadd 
2829eb6f0de0SAdrian Chadd 	ath_tx_default_comp(sc, bf, fail);
2830eb6f0de0SAdrian Chadd }
2831eb6f0de0SAdrian Chadd 
2832eb6f0de0SAdrian Chadd /*
2833eb6f0de0SAdrian Chadd  * Handle cleanup of aggregate session packets that aren't
2834eb6f0de0SAdrian Chadd  * an A-MPDU.
2835eb6f0de0SAdrian Chadd  *
2836eb6f0de0SAdrian Chadd  * There's no need to update the BAW here - the session is being
2837eb6f0de0SAdrian Chadd  * torn down.
2838eb6f0de0SAdrian Chadd  */
2839eb6f0de0SAdrian Chadd static void
2840eb6f0de0SAdrian Chadd ath_tx_comp_cleanup_unaggr(struct ath_softc *sc, struct ath_buf *bf)
2841eb6f0de0SAdrian Chadd {
2842eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
2843eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
2844eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
2845eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
2846eb6f0de0SAdrian Chadd 
2847eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: TID %d: incomp=%d\n",
2848eb6f0de0SAdrian Chadd 	    __func__, tid, atid->incomp);
2849eb6f0de0SAdrian Chadd 
2850eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
2851eb6f0de0SAdrian Chadd 	atid->incomp--;
2852eb6f0de0SAdrian Chadd 	if (atid->incomp == 0) {
2853eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
2854eb6f0de0SAdrian Chadd 		    "%s: TID %d: cleaned up! resume!\n",
2855eb6f0de0SAdrian Chadd 		    __func__, tid);
2856eb6f0de0SAdrian Chadd 		atid->cleanup_inprogress = 0;
2857eb6f0de0SAdrian Chadd 		ath_tx_tid_resume(sc, atid);
2858eb6f0de0SAdrian Chadd 	}
2859eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
2860eb6f0de0SAdrian Chadd 
2861eb6f0de0SAdrian Chadd 	ath_tx_default_comp(sc, bf, 0);
2862eb6f0de0SAdrian Chadd }
2863eb6f0de0SAdrian Chadd 
2864eb6f0de0SAdrian Chadd /*
2865eb6f0de0SAdrian Chadd  * Performs transmit side cleanup when TID changes from aggregated to
2866eb6f0de0SAdrian Chadd  * unaggregated.
2867eb6f0de0SAdrian Chadd  *
2868eb6f0de0SAdrian Chadd  * - Discard all retry frames from the s/w queue.
2869eb6f0de0SAdrian Chadd  * - Fix the tx completion function for all buffers in s/w queue.
2870eb6f0de0SAdrian Chadd  * - Count the number of unacked frames, and let transmit completion
2871eb6f0de0SAdrian Chadd  *   handle it later.
2872eb6f0de0SAdrian Chadd  *
2873eb6f0de0SAdrian Chadd  * The caller is responsible for pausing the TID.
2874eb6f0de0SAdrian Chadd  */
2875eb6f0de0SAdrian Chadd static void
2876eb6f0de0SAdrian Chadd ath_tx_cleanup(struct ath_softc *sc, struct ath_node *an, int tid)
2877eb6f0de0SAdrian Chadd {
2878eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
2879eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
2880eb6f0de0SAdrian Chadd 	struct ath_buf *bf, *bf_next;
2881eb6f0de0SAdrian Chadd 	ath_bufhead bf_cq;
2882eb6f0de0SAdrian Chadd 
2883eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
2884eb6f0de0SAdrian Chadd 	    "%s: TID %d: called\n", __func__, tid);
2885eb6f0de0SAdrian Chadd 
2886eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_cq);
2887eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
2888eb6f0de0SAdrian Chadd 
2889eb6f0de0SAdrian Chadd 	/*
2890eb6f0de0SAdrian Chadd 	 * Update the frames in the software TX queue:
2891eb6f0de0SAdrian Chadd 	 *
2892eb6f0de0SAdrian Chadd 	 * + Discard retry frames in the queue
2893eb6f0de0SAdrian Chadd 	 * + Fix the completion function to be non-aggregate
2894eb6f0de0SAdrian Chadd 	 */
2895eb6f0de0SAdrian Chadd 	bf = TAILQ_FIRST(&atid->axq_q);
2896eb6f0de0SAdrian Chadd 	while (bf) {
2897eb6f0de0SAdrian Chadd 		if (bf->bf_state.bfs_isretried) {
2898eb6f0de0SAdrian Chadd 			bf_next = TAILQ_NEXT(bf, bf_list);
2899eb6f0de0SAdrian Chadd 			TAILQ_REMOVE(&atid->axq_q, bf, bf_list);
2900eb6f0de0SAdrian Chadd 			atid->axq_depth--;
2901eb6f0de0SAdrian Chadd 			if (bf->bf_state.bfs_dobaw) {
2902eb6f0de0SAdrian Chadd 				ath_tx_update_baw(sc, an, atid, bf);
2903eb6f0de0SAdrian Chadd 				if (! bf->bf_state.bfs_addedbaw)
2904eb6f0de0SAdrian Chadd 					device_printf(sc->sc_dev,
2905eb6f0de0SAdrian Chadd 					    "%s: wasn't added: seqno %d\n",
2906d4365d16SAdrian Chadd 					    __func__,
2907d4365d16SAdrian Chadd 					    SEQNO(bf->bf_state.bfs_seqno));
2908eb6f0de0SAdrian Chadd 			}
2909eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_dobaw = 0;
2910eb6f0de0SAdrian Chadd 			/*
2911eb6f0de0SAdrian Chadd 			 * Call the default completion handler with "fail" just
2912eb6f0de0SAdrian Chadd 			 * so upper levels are suitably notified about this.
2913eb6f0de0SAdrian Chadd 			 */
2914eb6f0de0SAdrian Chadd 			TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
2915eb6f0de0SAdrian Chadd 			bf = bf_next;
2916eb6f0de0SAdrian Chadd 			continue;
2917eb6f0de0SAdrian Chadd 		}
2918eb6f0de0SAdrian Chadd 		/* Give these the default completion handler */
2919eb6f0de0SAdrian Chadd 		bf->bf_comp = ath_tx_normal_comp;
2920eb6f0de0SAdrian Chadd 		bf = TAILQ_NEXT(bf, bf_list);
2921eb6f0de0SAdrian Chadd 	}
2922eb6f0de0SAdrian Chadd 
2923eb6f0de0SAdrian Chadd 	/* The caller is required to pause the TID */
2924eb6f0de0SAdrian Chadd #if 0
2925eb6f0de0SAdrian Chadd 	/* Pause the TID */
2926eb6f0de0SAdrian Chadd 	ath_tx_tid_pause(sc, atid);
2927eb6f0de0SAdrian Chadd #endif
2928eb6f0de0SAdrian Chadd 
2929eb6f0de0SAdrian Chadd 	/*
2930eb6f0de0SAdrian Chadd 	 * Calculate what hardware-queued frames exist based
2931eb6f0de0SAdrian Chadd 	 * on the current BAW size. Ie, what frames have been
2932eb6f0de0SAdrian Chadd 	 * added to the TX hardware queue for this TID but
2933eb6f0de0SAdrian Chadd 	 * not yet ACKed.
2934eb6f0de0SAdrian Chadd 	 */
2935eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
2936eb6f0de0SAdrian Chadd 	/* Need the lock - fiddling with BAW */
2937eb6f0de0SAdrian Chadd 	while (atid->baw_head != atid->baw_tail) {
2938eb6f0de0SAdrian Chadd 		if (atid->tx_buf[atid->baw_head]) {
2939eb6f0de0SAdrian Chadd 			atid->incomp++;
2940eb6f0de0SAdrian Chadd 			atid->cleanup_inprogress = 1;
2941eb6f0de0SAdrian Chadd 			atid->tx_buf[atid->baw_head] = NULL;
2942eb6f0de0SAdrian Chadd 		}
2943eb6f0de0SAdrian Chadd 		INCR(atid->baw_head, ATH_TID_MAX_BUFS);
2944eb6f0de0SAdrian Chadd 		INCR(tap->txa_start, IEEE80211_SEQ_RANGE);
2945eb6f0de0SAdrian Chadd 	}
2946eb6f0de0SAdrian Chadd 
2947eb6f0de0SAdrian Chadd 	/*
2948eb6f0de0SAdrian Chadd 	 * If cleanup is required, defer TID scheduling
2949eb6f0de0SAdrian Chadd 	 * until all the HW queued packets have been
2950eb6f0de0SAdrian Chadd 	 * sent.
2951eb6f0de0SAdrian Chadd 	 */
2952eb6f0de0SAdrian Chadd 	if (! atid->cleanup_inprogress)
2953eb6f0de0SAdrian Chadd 		ath_tx_tid_resume(sc, atid);
2954eb6f0de0SAdrian Chadd 
2955eb6f0de0SAdrian Chadd 	if (atid->cleanup_inprogress)
2956eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
2957eb6f0de0SAdrian Chadd 		    "%s: TID %d: cleanup needed: %d packets\n",
2958eb6f0de0SAdrian Chadd 		    __func__, tid, atid->incomp);
2959eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
2960eb6f0de0SAdrian Chadd 
2961eb6f0de0SAdrian Chadd 	/* Handle completing frames and fail them */
2962eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
2963eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
2964eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 1);
2965eb6f0de0SAdrian Chadd 	}
2966eb6f0de0SAdrian Chadd }
2967eb6f0de0SAdrian Chadd 
2968eb6f0de0SAdrian Chadd static void
2969eb6f0de0SAdrian Chadd ath_tx_set_retry(struct ath_softc *sc, struct ath_buf *bf)
2970eb6f0de0SAdrian Chadd {
2971eb6f0de0SAdrian Chadd 	struct ieee80211_frame *wh;
2972eb6f0de0SAdrian Chadd 
2973eb6f0de0SAdrian Chadd 	wh = mtod(bf->bf_m, struct ieee80211_frame *);
2974eb6f0de0SAdrian Chadd 	/* Only update/resync if needed */
2975eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_isretried == 0) {
2976eb6f0de0SAdrian Chadd 		wh->i_fc[1] |= IEEE80211_FC1_RETRY;
2977eb6f0de0SAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
2978eb6f0de0SAdrian Chadd 		    BUS_DMASYNC_PREWRITE);
2979eb6f0de0SAdrian Chadd 	}
2980eb6f0de0SAdrian Chadd 	sc->sc_stats.ast_tx_swretries++;
2981eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_isretried = 1;
2982eb6f0de0SAdrian Chadd 	bf->bf_state.bfs_retries ++;
2983eb6f0de0SAdrian Chadd }
2984eb6f0de0SAdrian Chadd 
2985eb6f0de0SAdrian Chadd static struct ath_buf *
298638962489SAdrian Chadd ath_tx_retry_clone(struct ath_softc *sc, struct ath_node *an,
298738962489SAdrian Chadd     struct ath_tid *tid, struct ath_buf *bf)
2988eb6f0de0SAdrian Chadd {
2989eb6f0de0SAdrian Chadd 	struct ath_buf *nbf;
2990eb6f0de0SAdrian Chadd 	int error;
2991eb6f0de0SAdrian Chadd 
2992eb6f0de0SAdrian Chadd 	nbf = ath_buf_clone(sc, bf);
2993eb6f0de0SAdrian Chadd 
2994eb6f0de0SAdrian Chadd #if 0
2995eb6f0de0SAdrian Chadd 	device_printf(sc->sc_dev, "%s: ATH_BUF_BUSY; cloning\n",
2996eb6f0de0SAdrian Chadd 	    __func__);
2997eb6f0de0SAdrian Chadd #endif
2998eb6f0de0SAdrian Chadd 
2999eb6f0de0SAdrian Chadd 	if (nbf == NULL) {
3000eb6f0de0SAdrian Chadd 		/* Failed to clone */
3001eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
3002eb6f0de0SAdrian Chadd 		    "%s: failed to clone a busy buffer\n",
3003eb6f0de0SAdrian Chadd 		    __func__);
3004eb6f0de0SAdrian Chadd 		return NULL;
3005eb6f0de0SAdrian Chadd 	}
3006eb6f0de0SAdrian Chadd 
3007eb6f0de0SAdrian Chadd 	/* Setup the dma for the new buffer */
3008eb6f0de0SAdrian Chadd 	error = ath_tx_dmasetup(sc, nbf, nbf->bf_m);
3009eb6f0de0SAdrian Chadd 	if (error != 0) {
3010eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
3011eb6f0de0SAdrian Chadd 		    "%s: failed to setup dma for clone\n",
3012eb6f0de0SAdrian Chadd 		    __func__);
3013eb6f0de0SAdrian Chadd 		/*
3014eb6f0de0SAdrian Chadd 		 * Put this at the head of the list, not tail;
3015eb6f0de0SAdrian Chadd 		 * that way it doesn't interfere with the
3016eb6f0de0SAdrian Chadd 		 * busy buffer logic (which uses the tail of
3017eb6f0de0SAdrian Chadd 		 * the list.)
3018eb6f0de0SAdrian Chadd 		 */
3019eb6f0de0SAdrian Chadd 		ATH_TXBUF_LOCK(sc);
3020eb6f0de0SAdrian Chadd 		TAILQ_INSERT_HEAD(&sc->sc_txbuf, nbf, bf_list);
3021eb6f0de0SAdrian Chadd 		ATH_TXBUF_UNLOCK(sc);
3022eb6f0de0SAdrian Chadd 		return NULL;
3023eb6f0de0SAdrian Chadd 	}
3024eb6f0de0SAdrian Chadd 
302538962489SAdrian Chadd 	/* Update BAW if required, before we free the original buf */
302638962489SAdrian Chadd 	if (bf->bf_state.bfs_dobaw)
302738962489SAdrian Chadd 		ath_tx_switch_baw_buf(sc, an, tid, bf, nbf);
302838962489SAdrian Chadd 
3029eb6f0de0SAdrian Chadd 	/* Free current buffer; return the older buffer */
3030eb6f0de0SAdrian Chadd 	bf->bf_m = NULL;
3031eb6f0de0SAdrian Chadd 	bf->bf_node = NULL;
3032eb6f0de0SAdrian Chadd 	ath_freebuf(sc, bf);
3033eb6f0de0SAdrian Chadd 	return nbf;
3034eb6f0de0SAdrian Chadd }
3035eb6f0de0SAdrian Chadd 
3036eb6f0de0SAdrian Chadd /*
3037eb6f0de0SAdrian Chadd  * Handle retrying an unaggregate frame in an aggregate
3038eb6f0de0SAdrian Chadd  * session.
3039eb6f0de0SAdrian Chadd  *
3040eb6f0de0SAdrian Chadd  * If too many retries occur, pause the TID, wait for
3041eb6f0de0SAdrian Chadd  * any further retransmits (as there's no reason why
3042eb6f0de0SAdrian Chadd  * non-aggregate frames in an aggregate session are
3043eb6f0de0SAdrian Chadd  * transmitted in-order; they just have to be in-BAW)
3044eb6f0de0SAdrian Chadd  * and then queue a BAR.
3045eb6f0de0SAdrian Chadd  */
3046eb6f0de0SAdrian Chadd static void
3047eb6f0de0SAdrian Chadd ath_tx_aggr_retry_unaggr(struct ath_softc *sc, struct ath_buf *bf)
3048eb6f0de0SAdrian Chadd {
3049eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
3050eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
3051eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
3052eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
3053eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
3054eb6f0de0SAdrian Chadd 	int txseq;
3055eb6f0de0SAdrian Chadd 
3056eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
3057eb6f0de0SAdrian Chadd 
3058eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
3059eb6f0de0SAdrian Chadd 
3060eb6f0de0SAdrian Chadd 	/*
3061eb6f0de0SAdrian Chadd 	 * If the buffer is marked as busy, we can't directly
3062eb6f0de0SAdrian Chadd 	 * reuse it. Instead, try to clone the buffer.
3063eb6f0de0SAdrian Chadd 	 * If the clone is successful, recycle the old buffer.
3064eb6f0de0SAdrian Chadd 	 * If the clone is unsuccessful, set bfs_retries to max
3065eb6f0de0SAdrian Chadd 	 * to force the next bit of code to free the buffer
3066eb6f0de0SAdrian Chadd 	 * for us.
3067eb6f0de0SAdrian Chadd 	 */
3068eb6f0de0SAdrian Chadd 	if ((bf->bf_state.bfs_retries < SWMAX_RETRIES) &&
3069eb6f0de0SAdrian Chadd 	    (bf->bf_flags & ATH_BUF_BUSY)) {
3070eb6f0de0SAdrian Chadd 		struct ath_buf *nbf;
307138962489SAdrian Chadd 		nbf = ath_tx_retry_clone(sc, an, atid, bf);
3072eb6f0de0SAdrian Chadd 		if (nbf)
3073eb6f0de0SAdrian Chadd 			/* bf has been freed at this point */
3074eb6f0de0SAdrian Chadd 			bf = nbf;
3075eb6f0de0SAdrian Chadd 		else
3076eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_retries = SWMAX_RETRIES + 1;
3077eb6f0de0SAdrian Chadd 	}
3078eb6f0de0SAdrian Chadd 
3079eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_retries >= SWMAX_RETRIES) {
3080eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_RETRIES,
3081eb6f0de0SAdrian Chadd 		    "%s: exceeded retries; seqno %d\n",
3082eb6f0de0SAdrian Chadd 		    __func__, SEQNO(bf->bf_state.bfs_seqno));
3083eb6f0de0SAdrian Chadd 		sc->sc_stats.ast_tx_swretrymax++;
3084eb6f0de0SAdrian Chadd 
3085eb6f0de0SAdrian Chadd 		/* Update BAW anyway */
3086eb6f0de0SAdrian Chadd 		if (bf->bf_state.bfs_dobaw) {
3087eb6f0de0SAdrian Chadd 			ath_tx_update_baw(sc, an, atid, bf);
3088eb6f0de0SAdrian Chadd 			if (! bf->bf_state.bfs_addedbaw)
3089eb6f0de0SAdrian Chadd 				device_printf(sc->sc_dev,
3090eb6f0de0SAdrian Chadd 				    "%s: wasn't added: seqno %d\n",
3091eb6f0de0SAdrian Chadd 				    __func__, SEQNO(bf->bf_state.bfs_seqno));
3092eb6f0de0SAdrian Chadd 		}
3093eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_dobaw = 0;
3094eb6f0de0SAdrian Chadd 
3095eb6f0de0SAdrian Chadd 		/* Send BAR frame */
3096eb6f0de0SAdrian Chadd 		/*
3097eb6f0de0SAdrian Chadd 		 * This'll end up going into net80211 and back out
3098eb6f0de0SAdrian Chadd 		 * again, via ic->ic_raw_xmit().
3099eb6f0de0SAdrian Chadd 		 */
3100eb6f0de0SAdrian Chadd 		txseq = tap->txa_start;
3101eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3102eb6f0de0SAdrian Chadd 
3103eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
3104eb6f0de0SAdrian Chadd 		    "%s: TID %d: send BAR; seq %d\n", __func__, tid, txseq);
3105eb6f0de0SAdrian Chadd 
3106eb6f0de0SAdrian Chadd 		/* XXX TODO: send BAR */
3107eb6f0de0SAdrian Chadd 
3108eb6f0de0SAdrian Chadd 		/* Free buffer, bf is free after this call */
3109eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
3110eb6f0de0SAdrian Chadd 		return;
3111eb6f0de0SAdrian Chadd 	}
3112eb6f0de0SAdrian Chadd 
3113eb6f0de0SAdrian Chadd 	/*
3114eb6f0de0SAdrian Chadd 	 * This increments the retry counter as well as
3115eb6f0de0SAdrian Chadd 	 * sets the retry flag in the ath_buf and packet
3116eb6f0de0SAdrian Chadd 	 * body.
3117eb6f0de0SAdrian Chadd 	 */
3118eb6f0de0SAdrian Chadd 	ath_tx_set_retry(sc, bf);
3119eb6f0de0SAdrian Chadd 
3120eb6f0de0SAdrian Chadd 	/*
3121eb6f0de0SAdrian Chadd 	 * Insert this at the head of the queue, so it's
3122eb6f0de0SAdrian Chadd 	 * retried before any current/subsequent frames.
3123eb6f0de0SAdrian Chadd 	 */
3124eb6f0de0SAdrian Chadd 	ATH_TXQ_INSERT_HEAD(atid, bf, bf_list);
3125eb6f0de0SAdrian Chadd 	ath_tx_tid_sched(sc, atid);
3126eb6f0de0SAdrian Chadd 
3127eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3128eb6f0de0SAdrian Chadd }
3129eb6f0de0SAdrian Chadd 
3130eb6f0de0SAdrian Chadd /*
3131eb6f0de0SAdrian Chadd  * Common code for aggregate excessive retry/subframe retry.
3132eb6f0de0SAdrian Chadd  * If retrying, queues buffers to bf_q. If not, frees the
3133eb6f0de0SAdrian Chadd  * buffers.
3134eb6f0de0SAdrian Chadd  *
3135eb6f0de0SAdrian Chadd  * XXX should unify this with ath_tx_aggr_retry_unaggr()
3136eb6f0de0SAdrian Chadd  */
3137eb6f0de0SAdrian Chadd static int
3138eb6f0de0SAdrian Chadd ath_tx_retry_subframe(struct ath_softc *sc, struct ath_buf *bf,
3139eb6f0de0SAdrian Chadd     ath_bufhead *bf_q)
3140eb6f0de0SAdrian Chadd {
3141eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
3142eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
3143eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
3144eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
3145eb6f0de0SAdrian Chadd 
3146eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[atid->ac]);
3147eb6f0de0SAdrian Chadd 
3148eb6f0de0SAdrian Chadd 	ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
3149eb6f0de0SAdrian Chadd 	ath_hal_set11nburstduration(sc->sc_ah, bf->bf_desc, 0);
3150eb6f0de0SAdrian Chadd 	/* ath_hal_set11n_virtualmorefrag(sc->sc_ah, bf->bf_desc, 0); */
3151eb6f0de0SAdrian Chadd 
3152eb6f0de0SAdrian Chadd 	/*
3153eb6f0de0SAdrian Chadd 	 * If the buffer is marked as busy, we can't directly
3154eb6f0de0SAdrian Chadd 	 * reuse it. Instead, try to clone the buffer.
3155eb6f0de0SAdrian Chadd 	 * If the clone is successful, recycle the old buffer.
3156eb6f0de0SAdrian Chadd 	 * If the clone is unsuccessful, set bfs_retries to max
3157eb6f0de0SAdrian Chadd 	 * to force the next bit of code to free the buffer
3158eb6f0de0SAdrian Chadd 	 * for us.
3159eb6f0de0SAdrian Chadd 	 */
3160eb6f0de0SAdrian Chadd 	if ((bf->bf_state.bfs_retries < SWMAX_RETRIES) &&
3161eb6f0de0SAdrian Chadd 	    (bf->bf_flags & ATH_BUF_BUSY)) {
3162eb6f0de0SAdrian Chadd 		struct ath_buf *nbf;
316338962489SAdrian Chadd 		nbf = ath_tx_retry_clone(sc, an, atid, bf);
3164eb6f0de0SAdrian Chadd 		if (nbf)
3165eb6f0de0SAdrian Chadd 			/* bf has been freed at this point */
3166eb6f0de0SAdrian Chadd 			bf = nbf;
3167eb6f0de0SAdrian Chadd 		else
3168eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_retries = SWMAX_RETRIES + 1;
3169eb6f0de0SAdrian Chadd 	}
3170eb6f0de0SAdrian Chadd 
3171eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_retries >= SWMAX_RETRIES) {
3172eb6f0de0SAdrian Chadd 		sc->sc_stats.ast_tx_swretrymax++;
3173eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_RETRIES,
3174eb6f0de0SAdrian Chadd 		    "%s: max retries: seqno %d\n",
3175eb6f0de0SAdrian Chadd 		    __func__, SEQNO(bf->bf_state.bfs_seqno));
3176eb6f0de0SAdrian Chadd 		ath_tx_update_baw(sc, an, atid, bf);
3177eb6f0de0SAdrian Chadd 		if (! bf->bf_state.bfs_addedbaw)
3178eb6f0de0SAdrian Chadd 			device_printf(sc->sc_dev,
3179eb6f0de0SAdrian Chadd 			    "%s: wasn't added: seqno %d\n",
3180eb6f0de0SAdrian Chadd 			    __func__, SEQNO(bf->bf_state.bfs_seqno));
3181eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_dobaw = 0;
3182eb6f0de0SAdrian Chadd 		return 1;
3183eb6f0de0SAdrian Chadd 	}
3184eb6f0de0SAdrian Chadd 
3185eb6f0de0SAdrian Chadd 	ath_tx_set_retry(sc, bf);
3186eb6f0de0SAdrian Chadd 	bf->bf_next = NULL;		/* Just to make sure */
3187eb6f0de0SAdrian Chadd 
3188eb6f0de0SAdrian Chadd 	TAILQ_INSERT_TAIL(bf_q, bf, bf_list);
3189eb6f0de0SAdrian Chadd 	return 0;
3190eb6f0de0SAdrian Chadd }
3191eb6f0de0SAdrian Chadd 
3192eb6f0de0SAdrian Chadd /*
3193eb6f0de0SAdrian Chadd  * error pkt completion for an aggregate destination
3194eb6f0de0SAdrian Chadd  */
3195eb6f0de0SAdrian Chadd static void
3196eb6f0de0SAdrian Chadd ath_tx_comp_aggr_error(struct ath_softc *sc, struct ath_buf *bf_first,
3197eb6f0de0SAdrian Chadd     struct ath_tid *tid)
3198eb6f0de0SAdrian Chadd {
3199eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf_first->bf_node;
3200eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
3201eb6f0de0SAdrian Chadd 	struct ath_buf *bf_next, *bf;
3202eb6f0de0SAdrian Chadd 	ath_bufhead bf_q;
3203eb6f0de0SAdrian Chadd 	int drops = 0;
3204eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
3205eb6f0de0SAdrian Chadd 	ath_bufhead bf_cq;
3206eb6f0de0SAdrian Chadd 
3207eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_q);
3208eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_cq);
3209eb6f0de0SAdrian Chadd 
3210eb6f0de0SAdrian Chadd 	/*
3211eb6f0de0SAdrian Chadd 	 * Update rate control - all frames have failed.
3212eb6f0de0SAdrian Chadd 	 *
3213eb6f0de0SAdrian Chadd 	 * XXX use the length in the first frame in the series;
3214eb6f0de0SAdrian Chadd 	 * XXX just so things are consistent for now.
3215eb6f0de0SAdrian Chadd 	 */
3216eb6f0de0SAdrian Chadd 	ath_tx_update_ratectrl(sc, ni, bf_first->bf_state.bfs_rc,
3217eb6f0de0SAdrian Chadd 	    &bf_first->bf_status.ds_txstat,
3218eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_pktlen,
3219eb6f0de0SAdrian Chadd 	    bf_first->bf_state.bfs_nframes, bf_first->bf_state.bfs_nframes);
3220eb6f0de0SAdrian Chadd 
3221eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[tid->ac]);
3222eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
32232d3d4776SAdrian Chadd 	sc->sc_stats.ast_tx_aggr_failall++;
3224eb6f0de0SAdrian Chadd 
3225eb6f0de0SAdrian Chadd 	/* Retry all subframes */
3226eb6f0de0SAdrian Chadd 	bf = bf_first;
3227eb6f0de0SAdrian Chadd 	while (bf) {
3228eb6f0de0SAdrian Chadd 		bf_next = bf->bf_next;
3229eb6f0de0SAdrian Chadd 		bf->bf_next = NULL;	/* Remove it from the aggr list */
32302d3d4776SAdrian Chadd 		sc->sc_stats.ast_tx_aggr_fail++;
3231eb6f0de0SAdrian Chadd 		if (ath_tx_retry_subframe(sc, bf, &bf_q)) {
3232eb6f0de0SAdrian Chadd 			drops++;
3233eb6f0de0SAdrian Chadd 			bf->bf_next = NULL;
3234eb6f0de0SAdrian Chadd 			TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
3235eb6f0de0SAdrian Chadd 		}
3236eb6f0de0SAdrian Chadd 		bf = bf_next;
3237eb6f0de0SAdrian Chadd 	}
3238eb6f0de0SAdrian Chadd 
3239eb6f0de0SAdrian Chadd 	/* Prepend all frames to the beginning of the queue */
3240eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_LAST(&bf_q, ath_bufhead_s)) != NULL) {
3241eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_q, bf, bf_list);
3242eb6f0de0SAdrian Chadd 		ATH_TXQ_INSERT_HEAD(tid, bf, bf_list);
3243eb6f0de0SAdrian Chadd 	}
3244eb6f0de0SAdrian Chadd 
3245eb6f0de0SAdrian Chadd 	ath_tx_tid_sched(sc, tid);
3246eb6f0de0SAdrian Chadd 
3247eb6f0de0SAdrian Chadd 	/*
3248eb6f0de0SAdrian Chadd 	 * send bar if we dropped any frames
3249eb6f0de0SAdrian Chadd 	 *
3250eb6f0de0SAdrian Chadd 	 * Keep the txq lock held for now, as we need to ensure
3251eb6f0de0SAdrian Chadd 	 * that ni_txseqs[] is consistent (as it's being updated
3252eb6f0de0SAdrian Chadd 	 * in the ifnet TX context or raw TX context.)
3253eb6f0de0SAdrian Chadd 	 */
3254eb6f0de0SAdrian Chadd 	if (drops) {
3255eb6f0de0SAdrian Chadd 		int txseq = tap->txa_start;
3256eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[tid->ac]);
3257eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
3258eb6f0de0SAdrian Chadd 		    "%s: TID %d: send BAR; seq %d\n",
3259eb6f0de0SAdrian Chadd 		    __func__, tid->tid, txseq);
3260eb6f0de0SAdrian Chadd 
3261eb6f0de0SAdrian Chadd 		/* XXX TODO: send BAR */
3262eb6f0de0SAdrian Chadd 	} else {
3263eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[tid->ac]);
3264eb6f0de0SAdrian Chadd 	}
3265eb6f0de0SAdrian Chadd 
3266eb6f0de0SAdrian Chadd 	/* Complete frames which errored out */
3267eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
3268eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
3269eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
3270eb6f0de0SAdrian Chadd 	}
3271eb6f0de0SAdrian Chadd }
3272eb6f0de0SAdrian Chadd 
3273eb6f0de0SAdrian Chadd /*
3274eb6f0de0SAdrian Chadd  * Handle clean-up of packets from an aggregate list.
3275eb6f0de0SAdrian Chadd  *
3276eb6f0de0SAdrian Chadd  * There's no need to update the BAW here - the session is being
3277eb6f0de0SAdrian Chadd  * torn down.
3278eb6f0de0SAdrian Chadd  */
3279eb6f0de0SAdrian Chadd static void
3280eb6f0de0SAdrian Chadd ath_tx_comp_cleanup_aggr(struct ath_softc *sc, struct ath_buf *bf_first)
3281eb6f0de0SAdrian Chadd {
3282eb6f0de0SAdrian Chadd 	struct ath_buf *bf, *bf_next;
3283eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf_first->bf_node;
3284eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
3285eb6f0de0SAdrian Chadd 	int tid = bf_first->bf_state.bfs_tid;
3286eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
3287eb6f0de0SAdrian Chadd 
3288eb6f0de0SAdrian Chadd 	bf = bf_first;
3289eb6f0de0SAdrian Chadd 
3290eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
3291eb6f0de0SAdrian Chadd 
3292eb6f0de0SAdrian Chadd 	/* update incomp */
3293eb6f0de0SAdrian Chadd 	while (bf) {
3294eb6f0de0SAdrian Chadd 		atid->incomp--;
3295eb6f0de0SAdrian Chadd 		bf = bf->bf_next;
3296eb6f0de0SAdrian Chadd 	}
3297eb6f0de0SAdrian Chadd 
3298eb6f0de0SAdrian Chadd 	if (atid->incomp == 0) {
3299eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
3300eb6f0de0SAdrian Chadd 		    "%s: TID %d: cleaned up! resume!\n",
3301eb6f0de0SAdrian Chadd 		    __func__, tid);
3302eb6f0de0SAdrian Chadd 		atid->cleanup_inprogress = 0;
3303eb6f0de0SAdrian Chadd 		ath_tx_tid_resume(sc, atid);
3304eb6f0de0SAdrian Chadd 	}
3305eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3306eb6f0de0SAdrian Chadd 
3307eb6f0de0SAdrian Chadd 	/* Handle frame completion */
3308eb6f0de0SAdrian Chadd 	while (bf) {
3309eb6f0de0SAdrian Chadd 		bf_next = bf->bf_next;
3310eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 1);
3311eb6f0de0SAdrian Chadd 		bf = bf_next;
3312eb6f0de0SAdrian Chadd 	}
3313eb6f0de0SAdrian Chadd }
3314eb6f0de0SAdrian Chadd 
3315eb6f0de0SAdrian Chadd /*
3316eb6f0de0SAdrian Chadd  * Handle completion of an set of aggregate frames.
3317eb6f0de0SAdrian Chadd  *
3318eb6f0de0SAdrian Chadd  * XXX for now, simply complete each sub-frame.
3319eb6f0de0SAdrian Chadd  *
3320eb6f0de0SAdrian Chadd  * Note: the completion handler is the last descriptor in the aggregate,
3321eb6f0de0SAdrian Chadd  * not the last descriptor in the first frame.
3322eb6f0de0SAdrian Chadd  */
3323eb6f0de0SAdrian Chadd static void
3324d4365d16SAdrian Chadd ath_tx_aggr_comp_aggr(struct ath_softc *sc, struct ath_buf *bf_first,
3325d4365d16SAdrian Chadd     int fail)
3326eb6f0de0SAdrian Chadd {
3327eb6f0de0SAdrian Chadd 	//struct ath_desc *ds = bf->bf_lastds;
3328eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf_first->bf_node;
3329eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
3330eb6f0de0SAdrian Chadd 	int tid = bf_first->bf_state.bfs_tid;
3331eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
3332eb6f0de0SAdrian Chadd 	struct ath_tx_status ts;
3333eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
3334eb6f0de0SAdrian Chadd 	ath_bufhead bf_q;
3335eb6f0de0SAdrian Chadd 	ath_bufhead bf_cq;
3336eb6f0de0SAdrian Chadd 	int seq_st, tx_ok;
3337eb6f0de0SAdrian Chadd 	int hasba, isaggr;
3338eb6f0de0SAdrian Chadd 	uint32_t ba[2];
3339eb6f0de0SAdrian Chadd 	struct ath_buf *bf, *bf_next;
3340eb6f0de0SAdrian Chadd 	int ba_index;
3341eb6f0de0SAdrian Chadd 	int drops = 0;
3342eb6f0de0SAdrian Chadd 	int nframes = 0, nbad = 0, nf;
3343eb6f0de0SAdrian Chadd 	int pktlen;
3344eb6f0de0SAdrian Chadd 	/* XXX there's too much on the stack? */
3345eb6f0de0SAdrian Chadd 	struct ath_rc_series rc[4];
3346eb6f0de0SAdrian Chadd 	int txseq;
3347eb6f0de0SAdrian Chadd 
3348eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: called; hwq_depth=%d\n",
3349eb6f0de0SAdrian Chadd 	    __func__, atid->hwq_depth);
3350eb6f0de0SAdrian Chadd 
3351eb6f0de0SAdrian Chadd 	/* The TID state is kept behind the TXQ lock */
3352eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
3353eb6f0de0SAdrian Chadd 
3354eb6f0de0SAdrian Chadd 	atid->hwq_depth--;
3355eb6f0de0SAdrian Chadd 	if (atid->hwq_depth < 0)
3356eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: hwq_depth < 0: %d\n",
3357eb6f0de0SAdrian Chadd 		    __func__, atid->hwq_depth);
3358eb6f0de0SAdrian Chadd 
3359eb6f0de0SAdrian Chadd 	/*
3360eb6f0de0SAdrian Chadd 	 * Punt cleanup to the relevant function, not our problem now
3361eb6f0de0SAdrian Chadd 	 */
3362eb6f0de0SAdrian Chadd 	if (atid->cleanup_inprogress) {
3363eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3364eb6f0de0SAdrian Chadd 		ath_tx_comp_cleanup_aggr(sc, bf_first);
3365eb6f0de0SAdrian Chadd 		return;
3366eb6f0de0SAdrian Chadd 	}
3367eb6f0de0SAdrian Chadd 
3368eb6f0de0SAdrian Chadd 	/*
3369eb6f0de0SAdrian Chadd 	 * Take a copy; this may be needed -after- bf_first
3370eb6f0de0SAdrian Chadd 	 * has been completed and freed.
3371eb6f0de0SAdrian Chadd 	 */
3372eb6f0de0SAdrian Chadd 	ts = bf_first->bf_status.ds_txstat;
3373eb6f0de0SAdrian Chadd 	/*
3374eb6f0de0SAdrian Chadd 	 * XXX for now, use the first frame in the aggregate for
3375eb6f0de0SAdrian Chadd 	 * XXX rate control completion; it's at least consistent.
3376eb6f0de0SAdrian Chadd 	 */
3377eb6f0de0SAdrian Chadd 	pktlen = bf_first->bf_state.bfs_pktlen;
3378eb6f0de0SAdrian Chadd 
3379eb6f0de0SAdrian Chadd 	/*
3380eb6f0de0SAdrian Chadd 	 * handle errors first
3381eb6f0de0SAdrian Chadd 	 */
3382eb6f0de0SAdrian Chadd 	if (ts.ts_status & HAL_TXERR_XRETRY) {
3383eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3384eb6f0de0SAdrian Chadd 		ath_tx_comp_aggr_error(sc, bf_first, atid);
3385eb6f0de0SAdrian Chadd 		return;
3386eb6f0de0SAdrian Chadd 	}
3387eb6f0de0SAdrian Chadd 
3388eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_q);
3389eb6f0de0SAdrian Chadd 	TAILQ_INIT(&bf_cq);
3390eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
3391eb6f0de0SAdrian Chadd 
3392eb6f0de0SAdrian Chadd 	/*
3393eb6f0de0SAdrian Chadd 	 * extract starting sequence and block-ack bitmap
3394eb6f0de0SAdrian Chadd 	 */
3395eb6f0de0SAdrian Chadd 	/* XXX endian-ness of seq_st, ba? */
3396eb6f0de0SAdrian Chadd 	seq_st = ts.ts_seqnum;
3397eb6f0de0SAdrian Chadd 	hasba = !! (ts.ts_flags & HAL_TX_BA);
3398eb6f0de0SAdrian Chadd 	tx_ok = (ts.ts_status == 0);
3399eb6f0de0SAdrian Chadd 	isaggr = bf_first->bf_state.bfs_aggr;
3400eb6f0de0SAdrian Chadd 	ba[0] = ts.ts_ba_low;
3401eb6f0de0SAdrian Chadd 	ba[1] = ts.ts_ba_high;
3402eb6f0de0SAdrian Chadd 
3403eb6f0de0SAdrian Chadd 	/*
3404eb6f0de0SAdrian Chadd 	 * Copy the TX completion status and the rate control
3405eb6f0de0SAdrian Chadd 	 * series from the first descriptor, as it may be freed
3406eb6f0de0SAdrian Chadd 	 * before the rate control code can get its grubby fingers
3407eb6f0de0SAdrian Chadd 	 * into things.
3408eb6f0de0SAdrian Chadd 	 */
3409eb6f0de0SAdrian Chadd 	memcpy(rc, bf_first->bf_state.bfs_rc, sizeof(rc));
3410eb6f0de0SAdrian Chadd 
3411eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
3412d4365d16SAdrian Chadd 	    "%s: txa_start=%d, tx_ok=%d, status=%.8x, flags=%.8x, "
3413d4365d16SAdrian Chadd 	    "isaggr=%d, seq_st=%d, hasba=%d, ba=%.8x, %.8x\n",
3414eb6f0de0SAdrian Chadd 	    __func__, tap->txa_start, tx_ok, ts.ts_status, ts.ts_flags,
3415eb6f0de0SAdrian Chadd 	    isaggr, seq_st, hasba, ba[0], ba[1]);
3416eb6f0de0SAdrian Chadd 
3417eb6f0de0SAdrian Chadd 	/* Occasionally, the MAC sends a tx status for the wrong TID. */
3418eb6f0de0SAdrian Chadd 	if (tid != ts.ts_tid) {
3419eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: tid %d != hw tid %d\n",
3420eb6f0de0SAdrian Chadd 		    __func__, tid, ts.ts_tid);
3421eb6f0de0SAdrian Chadd 		tx_ok = 0;
3422eb6f0de0SAdrian Chadd 	}
3423eb6f0de0SAdrian Chadd 
3424eb6f0de0SAdrian Chadd 	/* AR5416 BA bug; this requires an interface reset */
3425eb6f0de0SAdrian Chadd 	if (isaggr && tx_ok && (! hasba)) {
3426eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
3427d4365d16SAdrian Chadd 		    "%s: AR5416 bug: hasba=%d; txok=%d, isaggr=%d, "
3428d4365d16SAdrian Chadd 		    "seq_st=%d\n",
3429eb6f0de0SAdrian Chadd 		    __func__, hasba, tx_ok, isaggr, seq_st);
3430eb6f0de0SAdrian Chadd 		/* XXX TODO: schedule an interface reset */
3431eb6f0de0SAdrian Chadd 	}
3432eb6f0de0SAdrian Chadd 
3433eb6f0de0SAdrian Chadd 	/*
3434eb6f0de0SAdrian Chadd 	 * Walk the list of frames, figure out which ones were correctly
3435eb6f0de0SAdrian Chadd 	 * sent and which weren't.
3436eb6f0de0SAdrian Chadd 	 */
3437eb6f0de0SAdrian Chadd 	bf = bf_first;
3438eb6f0de0SAdrian Chadd 	nf = bf_first->bf_state.bfs_nframes;
3439eb6f0de0SAdrian Chadd 
3440eb6f0de0SAdrian Chadd 	/* bf_first is going to be invalid once this list is walked */
3441eb6f0de0SAdrian Chadd 	bf_first = NULL;
3442eb6f0de0SAdrian Chadd 
3443eb6f0de0SAdrian Chadd 	/*
3444eb6f0de0SAdrian Chadd 	 * Walk the list of completed frames and determine
3445eb6f0de0SAdrian Chadd 	 * which need to be completed and which need to be
3446eb6f0de0SAdrian Chadd 	 * retransmitted.
3447eb6f0de0SAdrian Chadd 	 *
3448eb6f0de0SAdrian Chadd 	 * For completed frames, the completion functions need
3449eb6f0de0SAdrian Chadd 	 * to be called at the end of this function as the last
3450eb6f0de0SAdrian Chadd 	 * node reference may free the node.
3451eb6f0de0SAdrian Chadd 	 *
3452eb6f0de0SAdrian Chadd 	 * Finally, since the TXQ lock can't be held during the
3453eb6f0de0SAdrian Chadd 	 * completion callback (to avoid lock recursion),
3454eb6f0de0SAdrian Chadd 	 * the completion calls have to be done outside of the
3455eb6f0de0SAdrian Chadd 	 * lock.
3456eb6f0de0SAdrian Chadd 	 */
3457eb6f0de0SAdrian Chadd 	while (bf) {
3458eb6f0de0SAdrian Chadd 		nframes++;
3459d4365d16SAdrian Chadd 		ba_index = ATH_BA_INDEX(seq_st,
3460d4365d16SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
3461eb6f0de0SAdrian Chadd 		bf_next = bf->bf_next;
3462eb6f0de0SAdrian Chadd 		bf->bf_next = NULL;	/* Remove it from the aggr list */
3463eb6f0de0SAdrian Chadd 
3464eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
3465eb6f0de0SAdrian Chadd 		    "%s: checking bf=%p seqno=%d; ack=%d\n",
3466eb6f0de0SAdrian Chadd 		    __func__, bf, SEQNO(bf->bf_state.bfs_seqno),
3467eb6f0de0SAdrian Chadd 		    ATH_BA_ISSET(ba, ba_index));
3468eb6f0de0SAdrian Chadd 
3469eb6f0de0SAdrian Chadd 		if (tx_ok && ATH_BA_ISSET(ba, ba_index)) {
34702d3d4776SAdrian Chadd 			sc->sc_stats.ast_tx_aggr_ok++;
3471eb6f0de0SAdrian Chadd 			ath_tx_update_baw(sc, an, atid, bf);
3472eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_dobaw = 0;
3473eb6f0de0SAdrian Chadd 			if (! bf->bf_state.bfs_addedbaw)
3474eb6f0de0SAdrian Chadd 				device_printf(sc->sc_dev,
3475eb6f0de0SAdrian Chadd 				    "%s: wasn't added: seqno %d\n",
3476eb6f0de0SAdrian Chadd 				    __func__, SEQNO(bf->bf_state.bfs_seqno));
3477eb6f0de0SAdrian Chadd 			bf->bf_next = NULL;
3478eb6f0de0SAdrian Chadd 			TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
3479eb6f0de0SAdrian Chadd 		} else {
34802d3d4776SAdrian Chadd 			sc->sc_stats.ast_tx_aggr_fail++;
3481eb6f0de0SAdrian Chadd 			if (ath_tx_retry_subframe(sc, bf, &bf_q)) {
3482eb6f0de0SAdrian Chadd 				drops++;
3483eb6f0de0SAdrian Chadd 				bf->bf_next = NULL;
3484eb6f0de0SAdrian Chadd 				TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
3485eb6f0de0SAdrian Chadd 			}
3486eb6f0de0SAdrian Chadd 			nbad++;
3487eb6f0de0SAdrian Chadd 		}
3488eb6f0de0SAdrian Chadd 		bf = bf_next;
3489eb6f0de0SAdrian Chadd 	}
3490eb6f0de0SAdrian Chadd 
3491eb6f0de0SAdrian Chadd 	/*
3492eb6f0de0SAdrian Chadd 	 * Now that the BAW updates have been done, unlock
3493eb6f0de0SAdrian Chadd 	 *
3494eb6f0de0SAdrian Chadd 	 * txseq is grabbed before the lock is released so we
3495eb6f0de0SAdrian Chadd 	 * have a consistent view of what -was- in the BAW.
3496eb6f0de0SAdrian Chadd 	 * Anything after this point will not yet have been
3497eb6f0de0SAdrian Chadd 	 * TXed.
3498eb6f0de0SAdrian Chadd 	 */
3499eb6f0de0SAdrian Chadd 	txseq = tap->txa_start;
3500eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3501eb6f0de0SAdrian Chadd 
3502eb6f0de0SAdrian Chadd 	if (nframes != nf)
3503eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
3504eb6f0de0SAdrian Chadd 		    "%s: num frames seen=%d; bf nframes=%d\n",
3505eb6f0de0SAdrian Chadd 		    __func__, nframes, nf);
3506eb6f0de0SAdrian Chadd 
3507eb6f0de0SAdrian Chadd 	/*
3508eb6f0de0SAdrian Chadd 	 * Now we know how many frames were bad, call the rate
3509eb6f0de0SAdrian Chadd 	 * control code.
3510eb6f0de0SAdrian Chadd 	 */
3511eb6f0de0SAdrian Chadd 	if (fail == 0)
3512d4365d16SAdrian Chadd 		ath_tx_update_ratectrl(sc, ni, rc, &ts, pktlen, nframes,
3513d4365d16SAdrian Chadd 		    nbad);
3514eb6f0de0SAdrian Chadd 
3515eb6f0de0SAdrian Chadd 	/*
3516eb6f0de0SAdrian Chadd 	 * send bar if we dropped any frames
3517eb6f0de0SAdrian Chadd 	 */
3518eb6f0de0SAdrian Chadd 	if (drops) {
3519eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev,
3520eb6f0de0SAdrian Chadd 		    "%s: TID %d: send BAR; seq %d\n", __func__, tid, txseq);
3521eb6f0de0SAdrian Chadd 		/* XXX TODO: send BAR */
3522eb6f0de0SAdrian Chadd 	}
3523eb6f0de0SAdrian Chadd 
3524eb6f0de0SAdrian Chadd 	/* Prepend all frames to the beginning of the queue */
3525eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
3526eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_LAST(&bf_q, ath_bufhead_s)) != NULL) {
3527eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_q, bf, bf_list);
3528eb6f0de0SAdrian Chadd 		ATH_TXQ_INSERT_HEAD(atid, bf, bf_list);
3529eb6f0de0SAdrian Chadd 	}
3530eb6f0de0SAdrian Chadd 	ath_tx_tid_sched(sc, atid);
3531eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3532eb6f0de0SAdrian Chadd 
3533eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
3534eb6f0de0SAdrian Chadd 	    "%s: txa_start now %d\n", __func__, tap->txa_start);
3535eb6f0de0SAdrian Chadd 
3536eb6f0de0SAdrian Chadd 	/* Do deferred completion */
3537eb6f0de0SAdrian Chadd 	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
3538eb6f0de0SAdrian Chadd 		TAILQ_REMOVE(&bf_cq, bf, bf_list);
3539eb6f0de0SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
3540eb6f0de0SAdrian Chadd 	}
3541eb6f0de0SAdrian Chadd }
3542eb6f0de0SAdrian Chadd 
3543eb6f0de0SAdrian Chadd /*
3544eb6f0de0SAdrian Chadd  * Handle completion of unaggregated frames in an ADDBA
3545eb6f0de0SAdrian Chadd  * session.
3546eb6f0de0SAdrian Chadd  *
3547eb6f0de0SAdrian Chadd  * Fail is set to 1 if the entry is being freed via a call to
3548eb6f0de0SAdrian Chadd  * ath_tx_draintxq().
3549eb6f0de0SAdrian Chadd  */
3550eb6f0de0SAdrian Chadd static void
3551eb6f0de0SAdrian Chadd ath_tx_aggr_comp_unaggr(struct ath_softc *sc, struct ath_buf *bf, int fail)
3552eb6f0de0SAdrian Chadd {
3553eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
3554eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
3555eb6f0de0SAdrian Chadd 	int tid = bf->bf_state.bfs_tid;
3556eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
3557eb6f0de0SAdrian Chadd 	struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
3558eb6f0de0SAdrian Chadd 
3559eb6f0de0SAdrian Chadd 	/*
3560eb6f0de0SAdrian Chadd 	 * Update rate control status here, before we possibly
3561eb6f0de0SAdrian Chadd 	 * punt to retry or cleanup.
3562eb6f0de0SAdrian Chadd 	 *
3563eb6f0de0SAdrian Chadd 	 * Do it outside of the TXQ lock.
3564eb6f0de0SAdrian Chadd 	 */
3565eb6f0de0SAdrian Chadd 	if (fail == 0 && ((bf->bf_txflags & HAL_TXDESC_NOACK) == 0))
3566eb6f0de0SAdrian Chadd 		ath_tx_update_ratectrl(sc, ni, bf->bf_state.bfs_rc,
3567eb6f0de0SAdrian Chadd 		    &bf->bf_status.ds_txstat,
3568eb6f0de0SAdrian Chadd 		    bf->bf_state.bfs_pktlen,
3569eb6f0de0SAdrian Chadd 		    1, (ts->ts_status == 0) ? 0 : 1);
3570eb6f0de0SAdrian Chadd 
3571eb6f0de0SAdrian Chadd 	/*
3572eb6f0de0SAdrian Chadd 	 * This is called early so atid->hwq_depth can be tracked.
3573eb6f0de0SAdrian Chadd 	 * This unfortunately means that it's released and regrabbed
3574eb6f0de0SAdrian Chadd 	 * during retry and cleanup. That's rather inefficient.
3575eb6f0de0SAdrian Chadd 	 */
3576eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
3577eb6f0de0SAdrian Chadd 
3578eb6f0de0SAdrian Chadd 	if (tid == IEEE80211_NONQOS_TID)
3579eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: TID=16!\n", __func__);
3580eb6f0de0SAdrian Chadd 
3581d4365d16SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX,
3582d4365d16SAdrian Chadd 	    "%s: bf=%p: tid=%d, hwq_depth=%d, seqno=%d\n",
3583d4365d16SAdrian Chadd 	    __func__, bf, bf->bf_state.bfs_tid, atid->hwq_depth,
3584d4365d16SAdrian Chadd 	    SEQNO(bf->bf_state.bfs_seqno));
3585eb6f0de0SAdrian Chadd 
3586eb6f0de0SAdrian Chadd 	atid->hwq_depth--;
3587eb6f0de0SAdrian Chadd 	if (atid->hwq_depth < 0)
3588eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: hwq_depth < 0: %d\n",
3589eb6f0de0SAdrian Chadd 		    __func__, atid->hwq_depth);
3590eb6f0de0SAdrian Chadd 
3591eb6f0de0SAdrian Chadd 	/*
3592eb6f0de0SAdrian Chadd 	 * If a cleanup is in progress, punt to comp_cleanup;
3593eb6f0de0SAdrian Chadd 	 * rather than handling it here. It's thus their
3594eb6f0de0SAdrian Chadd 	 * responsibility to clean up, call the completion
3595eb6f0de0SAdrian Chadd 	 * function in net80211, etc.
3596eb6f0de0SAdrian Chadd 	 */
3597eb6f0de0SAdrian Chadd 	if (atid->cleanup_inprogress) {
3598eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3599d4365d16SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: cleanup_unaggr\n",
3600d4365d16SAdrian Chadd 		    __func__);
3601eb6f0de0SAdrian Chadd 		ath_tx_comp_cleanup_unaggr(sc, bf);
3602eb6f0de0SAdrian Chadd 		return;
3603eb6f0de0SAdrian Chadd 	}
3604eb6f0de0SAdrian Chadd 
3605eb6f0de0SAdrian Chadd 	/*
3606eb6f0de0SAdrian Chadd 	 * Don't bother with the retry check if all frames
3607eb6f0de0SAdrian Chadd 	 * are being failed (eg during queue deletion.)
3608eb6f0de0SAdrian Chadd 	 */
3609eb6f0de0SAdrian Chadd 	if (fail == 0 && ts->ts_status & HAL_TXERR_XRETRY) {
3610eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3611d4365d16SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: retry_unaggr\n",
3612d4365d16SAdrian Chadd 		    __func__);
3613eb6f0de0SAdrian Chadd 		ath_tx_aggr_retry_unaggr(sc, bf);
3614eb6f0de0SAdrian Chadd 		return;
3615eb6f0de0SAdrian Chadd 	}
3616eb6f0de0SAdrian Chadd 
3617eb6f0de0SAdrian Chadd 	/* Success? Complete */
3618eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: TID=%d, seqno %d\n",
3619eb6f0de0SAdrian Chadd 	    __func__, tid, SEQNO(bf->bf_state.bfs_seqno));
3620eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_dobaw) {
3621eb6f0de0SAdrian Chadd 		ath_tx_update_baw(sc, an, atid, bf);
3622eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_dobaw = 0;
3623eb6f0de0SAdrian Chadd 		if (! bf->bf_state.bfs_addedbaw)
3624eb6f0de0SAdrian Chadd 			device_printf(sc->sc_dev,
3625eb6f0de0SAdrian Chadd 			    "%s: wasn't added: seqno %d\n",
3626eb6f0de0SAdrian Chadd 			    __func__, SEQNO(bf->bf_state.bfs_seqno));
3627eb6f0de0SAdrian Chadd 	}
3628eb6f0de0SAdrian Chadd 
3629eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3630eb6f0de0SAdrian Chadd 
3631eb6f0de0SAdrian Chadd 	ath_tx_default_comp(sc, bf, fail);
3632eb6f0de0SAdrian Chadd 	/* bf is freed at this point */
3633eb6f0de0SAdrian Chadd }
3634eb6f0de0SAdrian Chadd 
3635eb6f0de0SAdrian Chadd void
3636eb6f0de0SAdrian Chadd ath_tx_aggr_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
3637eb6f0de0SAdrian Chadd {
3638eb6f0de0SAdrian Chadd 	if (bf->bf_state.bfs_aggr)
3639eb6f0de0SAdrian Chadd 		ath_tx_aggr_comp_aggr(sc, bf, fail);
3640eb6f0de0SAdrian Chadd 	else
3641eb6f0de0SAdrian Chadd 		ath_tx_aggr_comp_unaggr(sc, bf, fail);
3642eb6f0de0SAdrian Chadd }
3643eb6f0de0SAdrian Chadd 
3644eb6f0de0SAdrian Chadd /*
3645eb6f0de0SAdrian Chadd  * Schedule some packets from the given node/TID to the hardware.
3646eb6f0de0SAdrian Chadd  *
3647eb6f0de0SAdrian Chadd  * This is the aggregate version.
3648eb6f0de0SAdrian Chadd  */
3649eb6f0de0SAdrian Chadd void
3650eb6f0de0SAdrian Chadd ath_tx_tid_hw_queue_aggr(struct ath_softc *sc, struct ath_node *an,
3651eb6f0de0SAdrian Chadd     struct ath_tid *tid)
3652eb6f0de0SAdrian Chadd {
3653eb6f0de0SAdrian Chadd 	struct ath_buf *bf;
3654eb6f0de0SAdrian Chadd 	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
3655eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
3656eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = &an->an_node;
3657eb6f0de0SAdrian Chadd 	ATH_AGGR_STATUS status;
3658eb6f0de0SAdrian Chadd 	ath_bufhead bf_q;
3659eb6f0de0SAdrian Chadd 
3660eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d\n", __func__, tid->tid);
3661eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
3662eb6f0de0SAdrian Chadd 
3663eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid->tid);
3664eb6f0de0SAdrian Chadd 
3665eb6f0de0SAdrian Chadd 	if (tid->tid == IEEE80211_NONQOS_TID)
3666eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: called for TID=NONQOS_TID?\n",
3667eb6f0de0SAdrian Chadd 		    __func__);
3668eb6f0de0SAdrian Chadd 
3669eb6f0de0SAdrian Chadd 	for (;;) {
3670eb6f0de0SAdrian Chadd 		status = ATH_AGGR_DONE;
3671eb6f0de0SAdrian Chadd 
3672eb6f0de0SAdrian Chadd 		/*
3673eb6f0de0SAdrian Chadd 		 * If the upper layer has paused the TID, don't
3674eb6f0de0SAdrian Chadd 		 * queue any further packets.
3675eb6f0de0SAdrian Chadd 		 *
3676eb6f0de0SAdrian Chadd 		 * This can also occur from the completion task because
3677eb6f0de0SAdrian Chadd 		 * of packet loss; but as its serialised with this code,
3678eb6f0de0SAdrian Chadd 		 * it won't "appear" half way through queuing packets.
3679eb6f0de0SAdrian Chadd 		 */
3680eb6f0de0SAdrian Chadd 		if (tid->paused)
3681eb6f0de0SAdrian Chadd 			break;
3682eb6f0de0SAdrian Chadd 
3683eb6f0de0SAdrian Chadd 		bf = TAILQ_FIRST(&tid->axq_q);
3684eb6f0de0SAdrian Chadd 		if (bf == NULL) {
3685eb6f0de0SAdrian Chadd 			break;
3686eb6f0de0SAdrian Chadd 		}
3687eb6f0de0SAdrian Chadd 
3688eb6f0de0SAdrian Chadd 		/*
3689eb6f0de0SAdrian Chadd 		 * If the packet doesn't fall within the BAW (eg a NULL
3690eb6f0de0SAdrian Chadd 		 * data frame), schedule it directly; continue.
3691eb6f0de0SAdrian Chadd 		 */
3692eb6f0de0SAdrian Chadd 		if (! bf->bf_state.bfs_dobaw) {
3693d4365d16SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
3694d4365d16SAdrian Chadd 			    "%s: non-baw packet\n",
3695eb6f0de0SAdrian Chadd 			    __func__);
3696eb6f0de0SAdrian Chadd 			ATH_TXQ_REMOVE(tid, bf, bf_list);
3697eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_aggr = 0;
3698eb6f0de0SAdrian Chadd 			ath_tx_do_ratelookup(sc, bf);
3699eb6f0de0SAdrian Chadd 			ath_tx_rate_fill_rcflags(sc, bf);
3700eb6f0de0SAdrian Chadd 			ath_tx_set_rtscts(sc, bf);
3701eb6f0de0SAdrian Chadd 			ath_tx_setds(sc, bf);
3702eb6f0de0SAdrian Chadd 			ath_tx_chaindesclist(sc, bf);
3703eb6f0de0SAdrian Chadd 			ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
3704eb6f0de0SAdrian Chadd 			ath_tx_set_ratectrl(sc, ni, bf);
3705eb6f0de0SAdrian Chadd 
3706eb6f0de0SAdrian Chadd 			sc->sc_aggr_stats.aggr_nonbaw_pkt++;
3707eb6f0de0SAdrian Chadd 
3708eb6f0de0SAdrian Chadd 			/* Queue the packet; continue */
3709eb6f0de0SAdrian Chadd 			goto queuepkt;
3710eb6f0de0SAdrian Chadd 		}
3711eb6f0de0SAdrian Chadd 
3712eb6f0de0SAdrian Chadd 		TAILQ_INIT(&bf_q);
3713eb6f0de0SAdrian Chadd 
3714eb6f0de0SAdrian Chadd 		/*
3715eb6f0de0SAdrian Chadd 		 * Do a rate control lookup on the first frame in the
3716eb6f0de0SAdrian Chadd 		 * list. The rate control code needs that to occur
3717eb6f0de0SAdrian Chadd 		 * before it can determine whether to TX.
3718eb6f0de0SAdrian Chadd 		 * It's inaccurate because the rate control code doesn't
3719eb6f0de0SAdrian Chadd 		 * really "do" aggregate lookups, so it only considers
3720eb6f0de0SAdrian Chadd 		 * the size of the first frame.
3721eb6f0de0SAdrian Chadd 		 */
3722eb6f0de0SAdrian Chadd 		ath_tx_do_ratelookup(sc, bf);
3723eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[3].rix = 0;
3724eb6f0de0SAdrian Chadd 		bf->bf_state.bfs_rc[3].tries = 0;
3725eb6f0de0SAdrian Chadd 		ath_tx_rate_fill_rcflags(sc, bf);
3726eb6f0de0SAdrian Chadd 
3727eb6f0de0SAdrian Chadd 		status = ath_tx_form_aggr(sc, an, tid, &bf_q);
3728eb6f0de0SAdrian Chadd 
3729eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
3730eb6f0de0SAdrian Chadd 		    "%s: ath_tx_form_aggr() status=%d\n", __func__, status);
3731eb6f0de0SAdrian Chadd 
3732eb6f0de0SAdrian Chadd 		/*
3733eb6f0de0SAdrian Chadd 		 * No frames to be picked up - out of BAW
3734eb6f0de0SAdrian Chadd 		 */
3735eb6f0de0SAdrian Chadd 		if (TAILQ_EMPTY(&bf_q))
3736eb6f0de0SAdrian Chadd 			break;
3737eb6f0de0SAdrian Chadd 
3738eb6f0de0SAdrian Chadd 		/*
3739eb6f0de0SAdrian Chadd 		 * This assumes that the descriptor list in the ath_bufhead
3740eb6f0de0SAdrian Chadd 		 * are already linked together via bf_next pointers.
3741eb6f0de0SAdrian Chadd 		 */
3742eb6f0de0SAdrian Chadd 		bf = TAILQ_FIRST(&bf_q);
3743eb6f0de0SAdrian Chadd 
3744eb6f0de0SAdrian Chadd 		/*
3745eb6f0de0SAdrian Chadd 		 * If it's the only frame send as non-aggregate
3746eb6f0de0SAdrian Chadd 		 * assume that ath_tx_form_aggr() has checked
3747eb6f0de0SAdrian Chadd 		 * whether it's in the BAW and added it appropriately.
3748eb6f0de0SAdrian Chadd 		 */
3749eb6f0de0SAdrian Chadd 		if (bf->bf_state.bfs_nframes == 1) {
3750eb6f0de0SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
3751eb6f0de0SAdrian Chadd 			    "%s: single-frame aggregate\n", __func__);
3752eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_aggr = 0;
3753eb6f0de0SAdrian Chadd 			ath_tx_set_rtscts(sc, bf);
3754eb6f0de0SAdrian Chadd 			ath_tx_setds(sc, bf);
3755eb6f0de0SAdrian Chadd 			ath_tx_chaindesclist(sc, bf);
3756eb6f0de0SAdrian Chadd 			ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
3757eb6f0de0SAdrian Chadd 			ath_tx_set_ratectrl(sc, ni, bf);
3758eb6f0de0SAdrian Chadd 			if (status == ATH_AGGR_BAW_CLOSED)
3759eb6f0de0SAdrian Chadd 				sc->sc_aggr_stats.aggr_baw_closed_single_pkt++;
3760eb6f0de0SAdrian Chadd 			else
3761eb6f0de0SAdrian Chadd 				sc->sc_aggr_stats.aggr_single_pkt++;
3762eb6f0de0SAdrian Chadd 		} else {
3763eb6f0de0SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
3764d4365d16SAdrian Chadd 			    "%s: multi-frame aggregate: %d frames, "
3765d4365d16SAdrian Chadd 			    "length %d\n",
3766eb6f0de0SAdrian Chadd 			     __func__, bf->bf_state.bfs_nframes,
3767eb6f0de0SAdrian Chadd 			    bf->bf_state.bfs_al);
3768eb6f0de0SAdrian Chadd 			bf->bf_state.bfs_aggr = 1;
3769eb6f0de0SAdrian Chadd 			sc->sc_aggr_stats.aggr_pkts[bf->bf_state.bfs_nframes]++;
3770eb6f0de0SAdrian Chadd 			sc->sc_aggr_stats.aggr_aggr_pkt++;
3771eb6f0de0SAdrian Chadd 
3772eb6f0de0SAdrian Chadd 			/*
3773eb6f0de0SAdrian Chadd 			 * Update the rate and rtscts information based on the
3774eb6f0de0SAdrian Chadd 			 * rate decision made by the rate control code;
3775eb6f0de0SAdrian Chadd 			 * the first frame in the aggregate needs it.
3776eb6f0de0SAdrian Chadd 			 */
3777eb6f0de0SAdrian Chadd 			ath_tx_set_rtscts(sc, bf);
3778eb6f0de0SAdrian Chadd 
3779eb6f0de0SAdrian Chadd 			/*
3780eb6f0de0SAdrian Chadd 			 * Setup the relevant descriptor fields
3781eb6f0de0SAdrian Chadd 			 * for aggregation. The first descriptor
3782eb6f0de0SAdrian Chadd 			 * already points to the rest in the chain.
3783eb6f0de0SAdrian Chadd 			 */
3784eb6f0de0SAdrian Chadd 			ath_tx_setds_11n(sc, bf);
3785eb6f0de0SAdrian Chadd 
3786eb6f0de0SAdrian Chadd 			/*
3787eb6f0de0SAdrian Chadd 			 * setup first desc with rate and aggr info
3788eb6f0de0SAdrian Chadd 			 */
3789eb6f0de0SAdrian Chadd 			ath_tx_set_ratectrl(sc, ni, bf);
3790eb6f0de0SAdrian Chadd 		}
3791eb6f0de0SAdrian Chadd 	queuepkt:
3792eb6f0de0SAdrian Chadd 		//txq = bf->bf_state.bfs_txq;
3793eb6f0de0SAdrian Chadd 
3794eb6f0de0SAdrian Chadd 		/* Set completion handler, multi-frame aggregate or not */
3795eb6f0de0SAdrian Chadd 		bf->bf_comp = ath_tx_aggr_comp;
3796eb6f0de0SAdrian Chadd 
3797eb6f0de0SAdrian Chadd 		if (bf->bf_state.bfs_tid == IEEE80211_NONQOS_TID)
3798eb6f0de0SAdrian Chadd 		    device_printf(sc->sc_dev, "%s: TID=16?\n", __func__);
3799eb6f0de0SAdrian Chadd 
3800eb6f0de0SAdrian Chadd 		/* Punt to txq */
3801eb6f0de0SAdrian Chadd 		ath_tx_handoff(sc, txq, bf);
3802eb6f0de0SAdrian Chadd 
3803eb6f0de0SAdrian Chadd 		/* Track outstanding buffer count to hardware */
3804eb6f0de0SAdrian Chadd 		/* aggregates are "one" buffer */
3805eb6f0de0SAdrian Chadd 		tid->hwq_depth++;
3806eb6f0de0SAdrian Chadd 
3807eb6f0de0SAdrian Chadd 		/*
3808eb6f0de0SAdrian Chadd 		 * Break out if ath_tx_form_aggr() indicated
3809eb6f0de0SAdrian Chadd 		 * there can't be any further progress (eg BAW is full.)
3810eb6f0de0SAdrian Chadd 		 * Checking for an empty txq is done above.
3811eb6f0de0SAdrian Chadd 		 *
3812eb6f0de0SAdrian Chadd 		 * XXX locking on txq here?
3813eb6f0de0SAdrian Chadd 		 */
3814eb6f0de0SAdrian Chadd 		if (txq->axq_aggr_depth >= sc->sc_hwq_limit ||
3815eb6f0de0SAdrian Chadd 		    status == ATH_AGGR_BAW_CLOSED)
3816eb6f0de0SAdrian Chadd 			break;
3817eb6f0de0SAdrian Chadd 	}
3818eb6f0de0SAdrian Chadd }
3819eb6f0de0SAdrian Chadd 
3820eb6f0de0SAdrian Chadd /*
3821eb6f0de0SAdrian Chadd  * Schedule some packets from the given node/TID to the hardware.
3822eb6f0de0SAdrian Chadd  */
3823eb6f0de0SAdrian Chadd void
3824eb6f0de0SAdrian Chadd ath_tx_tid_hw_queue_norm(struct ath_softc *sc, struct ath_node *an,
3825eb6f0de0SAdrian Chadd     struct ath_tid *tid)
3826eb6f0de0SAdrian Chadd {
3827eb6f0de0SAdrian Chadd 	struct ath_buf *bf;
3828eb6f0de0SAdrian Chadd 	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
3829eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = &an->an_node;
3830eb6f0de0SAdrian Chadd 
3831eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: node %p: TID %d: called\n",
3832eb6f0de0SAdrian Chadd 	    __func__, an, tid->tid);
3833eb6f0de0SAdrian Chadd 
3834eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
3835eb6f0de0SAdrian Chadd 
3836eb6f0de0SAdrian Chadd 	/* Check - is AMPDU pending or running? then print out something */
3837eb6f0de0SAdrian Chadd 	if (ath_tx_ampdu_pending(sc, an, tid->tid))
3838eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: tid=%d, ampdu pending?\n",
3839eb6f0de0SAdrian Chadd 		    __func__, tid->tid);
3840eb6f0de0SAdrian Chadd 	if (ath_tx_ampdu_running(sc, an, tid->tid))
3841eb6f0de0SAdrian Chadd 		device_printf(sc->sc_dev, "%s: tid=%d, ampdu running?\n",
3842eb6f0de0SAdrian Chadd 		    __func__, tid->tid);
3843eb6f0de0SAdrian Chadd 
3844eb6f0de0SAdrian Chadd 	for (;;) {
3845eb6f0de0SAdrian Chadd 
3846eb6f0de0SAdrian Chadd 		/*
3847eb6f0de0SAdrian Chadd 		 * If the upper layers have paused the TID, don't
3848eb6f0de0SAdrian Chadd 		 * queue any further packets.
3849eb6f0de0SAdrian Chadd 		 */
3850eb6f0de0SAdrian Chadd 		if (tid->paused)
3851eb6f0de0SAdrian Chadd 			break;
3852eb6f0de0SAdrian Chadd 
3853eb6f0de0SAdrian Chadd 		bf = TAILQ_FIRST(&tid->axq_q);
3854eb6f0de0SAdrian Chadd 		if (bf == NULL) {
3855eb6f0de0SAdrian Chadd 			break;
3856eb6f0de0SAdrian Chadd 		}
3857eb6f0de0SAdrian Chadd 
3858eb6f0de0SAdrian Chadd 		ATH_TXQ_REMOVE(tid, bf, bf_list);
3859eb6f0de0SAdrian Chadd 
3860eb6f0de0SAdrian Chadd 		KASSERT(txq == bf->bf_state.bfs_txq, ("txqs not equal!\n"));
3861eb6f0de0SAdrian Chadd 
3862eb6f0de0SAdrian Chadd 		/* Sanity check! */
3863eb6f0de0SAdrian Chadd 		if (tid->tid != bf->bf_state.bfs_tid) {
3864eb6f0de0SAdrian Chadd 			device_printf(sc->sc_dev, "%s: bfs_tid %d !="
3865eb6f0de0SAdrian Chadd 			    " tid %d\n",
3866eb6f0de0SAdrian Chadd 			    __func__, bf->bf_state.bfs_tid, tid->tid);
3867eb6f0de0SAdrian Chadd 		}
3868eb6f0de0SAdrian Chadd 		/* Normal completion handler */
3869eb6f0de0SAdrian Chadd 		bf->bf_comp = ath_tx_normal_comp;
3870eb6f0de0SAdrian Chadd 
3871eb6f0de0SAdrian Chadd 		/* Program descriptors + rate control */
3872eb6f0de0SAdrian Chadd 		ath_tx_do_ratelookup(sc, bf);
3873eb6f0de0SAdrian Chadd 		ath_tx_rate_fill_rcflags(sc, bf);
3874eb6f0de0SAdrian Chadd 		ath_tx_set_rtscts(sc, bf);
3875eb6f0de0SAdrian Chadd 		ath_tx_setds(sc, bf);
3876eb6f0de0SAdrian Chadd 		ath_tx_chaindesclist(sc, bf);
3877eb6f0de0SAdrian Chadd 		ath_tx_set_ratectrl(sc, ni, bf);
3878eb6f0de0SAdrian Chadd 
3879eb6f0de0SAdrian Chadd 		/* Track outstanding buffer count to hardware */
3880eb6f0de0SAdrian Chadd 		/* aggregates are "one" buffer */
3881eb6f0de0SAdrian Chadd 		tid->hwq_depth++;
3882eb6f0de0SAdrian Chadd 
3883eb6f0de0SAdrian Chadd 		/* Punt to hardware or software txq */
3884eb6f0de0SAdrian Chadd 		ath_tx_handoff(sc, txq, bf);
3885eb6f0de0SAdrian Chadd 	}
3886eb6f0de0SAdrian Chadd }
3887eb6f0de0SAdrian Chadd 
3888eb6f0de0SAdrian Chadd /*
3889eb6f0de0SAdrian Chadd  * Schedule some packets to the given hardware queue.
3890eb6f0de0SAdrian Chadd  *
3891eb6f0de0SAdrian Chadd  * This function walks the list of TIDs (ie, ath_node TIDs
3892eb6f0de0SAdrian Chadd  * with queued traffic) and attempts to schedule traffic
3893eb6f0de0SAdrian Chadd  * from them.
3894eb6f0de0SAdrian Chadd  *
3895eb6f0de0SAdrian Chadd  * TID scheduling is implemented as a FIFO, with TIDs being
3896eb6f0de0SAdrian Chadd  * added to the end of the queue after some frames have been
3897eb6f0de0SAdrian Chadd  * scheduled.
3898eb6f0de0SAdrian Chadd  */
3899eb6f0de0SAdrian Chadd void
3900eb6f0de0SAdrian Chadd ath_txq_sched(struct ath_softc *sc, struct ath_txq *txq)
3901eb6f0de0SAdrian Chadd {
3902eb6f0de0SAdrian Chadd 	struct ath_tid *tid, *next, *last;
3903eb6f0de0SAdrian Chadd 
3904eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
3905eb6f0de0SAdrian Chadd 
3906eb6f0de0SAdrian Chadd 	/*
3907eb6f0de0SAdrian Chadd 	 * Don't schedule if the hardware queue is busy.
3908eb6f0de0SAdrian Chadd 	 * This (hopefully) gives some more time to aggregate
3909eb6f0de0SAdrian Chadd 	 * some packets in the aggregation queue.
3910eb6f0de0SAdrian Chadd 	 */
3911eb6f0de0SAdrian Chadd 	if (txq->axq_aggr_depth >= sc->sc_hwq_limit) {
3912eb6f0de0SAdrian Chadd 		sc->sc_aggr_stats.aggr_sched_nopkt++;
3913eb6f0de0SAdrian Chadd 		return;
3914eb6f0de0SAdrian Chadd 	}
3915eb6f0de0SAdrian Chadd 
3916eb6f0de0SAdrian Chadd 	last = TAILQ_LAST(&txq->axq_tidq, axq_t_s);
3917eb6f0de0SAdrian Chadd 
3918eb6f0de0SAdrian Chadd 	TAILQ_FOREACH_SAFE(tid, &txq->axq_tidq, axq_qelem, next) {
3919eb6f0de0SAdrian Chadd 		/*
3920eb6f0de0SAdrian Chadd 		 * Suspend paused queues here; they'll be resumed
3921eb6f0de0SAdrian Chadd 		 * once the addba completes or times out.
3922eb6f0de0SAdrian Chadd 		 */
3923eb6f0de0SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d, paused=%d\n",
3924eb6f0de0SAdrian Chadd 		    __func__, tid->tid, tid->paused);
3925eb6f0de0SAdrian Chadd 		ath_tx_tid_unsched(sc, tid);
3926eb6f0de0SAdrian Chadd 		if (tid->paused) {
3927eb6f0de0SAdrian Chadd 			continue;
3928eb6f0de0SAdrian Chadd 		}
3929eb6f0de0SAdrian Chadd 		if (ath_tx_ampdu_running(sc, tid->an, tid->tid))
3930eb6f0de0SAdrian Chadd 			ath_tx_tid_hw_queue_aggr(sc, tid->an, tid);
3931eb6f0de0SAdrian Chadd 		else
3932eb6f0de0SAdrian Chadd 			ath_tx_tid_hw_queue_norm(sc, tid->an, tid);
3933eb6f0de0SAdrian Chadd 
3934eb6f0de0SAdrian Chadd 		/* Not empty? Re-schedule */
3935eb6f0de0SAdrian Chadd 		if (tid->axq_depth != 0)
3936eb6f0de0SAdrian Chadd 			ath_tx_tid_sched(sc, tid);
3937eb6f0de0SAdrian Chadd 
3938eb6f0de0SAdrian Chadd 		/* Give the software queue time to aggregate more packets */
3939eb6f0de0SAdrian Chadd 		if (txq->axq_aggr_depth >= sc->sc_hwq_limit) {
3940eb6f0de0SAdrian Chadd 			break;
3941eb6f0de0SAdrian Chadd 		}
3942eb6f0de0SAdrian Chadd 
3943eb6f0de0SAdrian Chadd 		/*
3944eb6f0de0SAdrian Chadd 		 * If this was the last entry on the original list, stop.
3945eb6f0de0SAdrian Chadd 		 * Otherwise nodes that have been rescheduled onto the end
3946eb6f0de0SAdrian Chadd 		 * of the TID FIFO list will just keep being rescheduled.
3947eb6f0de0SAdrian Chadd 		 */
3948eb6f0de0SAdrian Chadd 		if (tid == last)
3949eb6f0de0SAdrian Chadd 			break;
3950eb6f0de0SAdrian Chadd 	}
3951eb6f0de0SAdrian Chadd }
3952eb6f0de0SAdrian Chadd 
3953eb6f0de0SAdrian Chadd /*
3954eb6f0de0SAdrian Chadd  * TX addba handling
3955eb6f0de0SAdrian Chadd  */
3956eb6f0de0SAdrian Chadd 
3957eb6f0de0SAdrian Chadd /*
3958eb6f0de0SAdrian Chadd  * Return net80211 TID struct pointer, or NULL for none
3959eb6f0de0SAdrian Chadd  */
3960eb6f0de0SAdrian Chadd struct ieee80211_tx_ampdu *
3961eb6f0de0SAdrian Chadd ath_tx_get_tx_tid(struct ath_node *an, int tid)
3962eb6f0de0SAdrian Chadd {
3963eb6f0de0SAdrian Chadd 	struct ieee80211_node *ni = &an->an_node;
3964eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
3965eb6f0de0SAdrian Chadd 	int ac;
3966eb6f0de0SAdrian Chadd 
3967eb6f0de0SAdrian Chadd 	if (tid == IEEE80211_NONQOS_TID)
3968eb6f0de0SAdrian Chadd 		return NULL;
3969eb6f0de0SAdrian Chadd 
3970eb6f0de0SAdrian Chadd 	ac = TID_TO_WME_AC(tid);
3971eb6f0de0SAdrian Chadd 
3972eb6f0de0SAdrian Chadd 	tap = &ni->ni_tx_ampdu[ac];
3973eb6f0de0SAdrian Chadd 	return tap;
3974eb6f0de0SAdrian Chadd }
3975eb6f0de0SAdrian Chadd 
3976eb6f0de0SAdrian Chadd /*
3977eb6f0de0SAdrian Chadd  * Is AMPDU-TX running?
3978eb6f0de0SAdrian Chadd  */
3979eb6f0de0SAdrian Chadd static int
3980eb6f0de0SAdrian Chadd ath_tx_ampdu_running(struct ath_softc *sc, struct ath_node *an, int tid)
3981eb6f0de0SAdrian Chadd {
3982eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
3983eb6f0de0SAdrian Chadd 
3984eb6f0de0SAdrian Chadd 	if (tid == IEEE80211_NONQOS_TID)
3985eb6f0de0SAdrian Chadd 		return 0;
3986eb6f0de0SAdrian Chadd 
3987eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
3988eb6f0de0SAdrian Chadd 	if (tap == NULL)
3989eb6f0de0SAdrian Chadd 		return 0;	/* Not valid; default to not running */
3990eb6f0de0SAdrian Chadd 
3991eb6f0de0SAdrian Chadd 	return !! (tap->txa_flags & IEEE80211_AGGR_RUNNING);
3992eb6f0de0SAdrian Chadd }
3993eb6f0de0SAdrian Chadd 
3994eb6f0de0SAdrian Chadd /*
3995eb6f0de0SAdrian Chadd  * Is AMPDU-TX negotiation pending?
3996eb6f0de0SAdrian Chadd  */
3997eb6f0de0SAdrian Chadd static int
3998eb6f0de0SAdrian Chadd ath_tx_ampdu_pending(struct ath_softc *sc, struct ath_node *an, int tid)
3999eb6f0de0SAdrian Chadd {
4000eb6f0de0SAdrian Chadd 	struct ieee80211_tx_ampdu *tap;
4001eb6f0de0SAdrian Chadd 
4002eb6f0de0SAdrian Chadd 	if (tid == IEEE80211_NONQOS_TID)
4003eb6f0de0SAdrian Chadd 		return 0;
4004eb6f0de0SAdrian Chadd 
4005eb6f0de0SAdrian Chadd 	tap = ath_tx_get_tx_tid(an, tid);
4006eb6f0de0SAdrian Chadd 	if (tap == NULL)
4007eb6f0de0SAdrian Chadd 		return 0;	/* Not valid; default to not pending */
4008eb6f0de0SAdrian Chadd 
4009eb6f0de0SAdrian Chadd 	return !! (tap->txa_flags & IEEE80211_AGGR_XCHGPEND);
4010eb6f0de0SAdrian Chadd }
4011eb6f0de0SAdrian Chadd 
4012eb6f0de0SAdrian Chadd /*
4013eb6f0de0SAdrian Chadd  * Is AMPDU-TX pending for the given TID?
4014eb6f0de0SAdrian Chadd  */
4015eb6f0de0SAdrian Chadd 
4016eb6f0de0SAdrian Chadd 
4017eb6f0de0SAdrian Chadd /*
4018eb6f0de0SAdrian Chadd  * Method to handle sending an ADDBA request.
4019eb6f0de0SAdrian Chadd  *
4020eb6f0de0SAdrian Chadd  * We tap this so the relevant flags can be set to pause the TID
4021eb6f0de0SAdrian Chadd  * whilst waiting for the response.
4022eb6f0de0SAdrian Chadd  *
4023eb6f0de0SAdrian Chadd  * XXX there's no timeout handler we can override?
4024eb6f0de0SAdrian Chadd  */
4025eb6f0de0SAdrian Chadd int
4026eb6f0de0SAdrian Chadd ath_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
4027eb6f0de0SAdrian Chadd     int dialogtoken, int baparamset, int batimeout)
4028eb6f0de0SAdrian Chadd {
4029eb6f0de0SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
4030eb6f0de0SAdrian Chadd 	int tid = WME_AC_TO_TID(tap->txa_ac);
4031eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4032eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4033eb6f0de0SAdrian Chadd 
4034eb6f0de0SAdrian Chadd 	/*
4035eb6f0de0SAdrian Chadd 	 * XXX danger Will Robinson!
4036eb6f0de0SAdrian Chadd 	 *
4037eb6f0de0SAdrian Chadd 	 * Although the taskqueue may be running and scheduling some more
4038eb6f0de0SAdrian Chadd 	 * packets, these should all be _before_ the addba sequence number.
4039eb6f0de0SAdrian Chadd 	 * However, net80211 will keep self-assigning sequence numbers
4040eb6f0de0SAdrian Chadd 	 * until addba has been negotiated.
4041eb6f0de0SAdrian Chadd 	 *
4042eb6f0de0SAdrian Chadd 	 * In the past, these packets would be "paused" (which still works
4043eb6f0de0SAdrian Chadd 	 * fine, as they're being scheduled to the driver in the same
4044eb6f0de0SAdrian Chadd 	 * serialised method which is calling the addba request routine)
4045eb6f0de0SAdrian Chadd 	 * and when the aggregation session begins, they'll be dequeued
4046eb6f0de0SAdrian Chadd 	 * as aggregate packets and added to the BAW. However, now there's
4047eb6f0de0SAdrian Chadd 	 * a "bf->bf_state.bfs_dobaw" flag, and this isn't set for these
4048eb6f0de0SAdrian Chadd 	 * packets. Thus they never get included in the BAW tracking and
4049eb6f0de0SAdrian Chadd 	 * this can cause the initial burst of packets after the addba
4050eb6f0de0SAdrian Chadd 	 * negotiation to "hang", as they quickly fall outside the BAW.
4051eb6f0de0SAdrian Chadd 	 *
4052eb6f0de0SAdrian Chadd 	 * The "eventual" solution should be to tag these packets with
4053eb6f0de0SAdrian Chadd 	 * dobaw. Although net80211 has given us a sequence number,
4054eb6f0de0SAdrian Chadd 	 * it'll be "after" the left edge of the BAW and thus it'll
4055eb6f0de0SAdrian Chadd 	 * fall within it.
4056eb6f0de0SAdrian Chadd 	 */
4057eb6f0de0SAdrian Chadd 	ath_tx_tid_pause(sc, atid);
4058eb6f0de0SAdrian Chadd 
4059eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4060eb6f0de0SAdrian Chadd 	    "%s: called; dialogtoken=%d, baparamset=%d, batimeout=%d\n",
4061eb6f0de0SAdrian Chadd 	    __func__, dialogtoken, baparamset, batimeout);
4062eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4063eb6f0de0SAdrian Chadd 	    "%s: txa_start=%d, ni_txseqs=%d\n",
4064eb6f0de0SAdrian Chadd 	    __func__, tap->txa_start, ni->ni_txseqs[tid]);
4065eb6f0de0SAdrian Chadd 
4066eb6f0de0SAdrian Chadd 	return sc->sc_addba_request(ni, tap, dialogtoken, baparamset,
4067eb6f0de0SAdrian Chadd 	    batimeout);
4068eb6f0de0SAdrian Chadd }
4069eb6f0de0SAdrian Chadd 
4070eb6f0de0SAdrian Chadd /*
4071eb6f0de0SAdrian Chadd  * Handle an ADDBA response.
4072eb6f0de0SAdrian Chadd  *
4073eb6f0de0SAdrian Chadd  * We unpause the queue so TX'ing can resume.
4074eb6f0de0SAdrian Chadd  *
4075eb6f0de0SAdrian Chadd  * Any packets TX'ed from this point should be "aggregate" (whether
4076eb6f0de0SAdrian Chadd  * aggregate or not) so the BAW is updated.
4077eb6f0de0SAdrian Chadd  *
4078eb6f0de0SAdrian Chadd  * Note! net80211 keeps self-assigning sequence numbers until
4079eb6f0de0SAdrian Chadd  * ampdu is negotiated. This means the initially-negotiated BAW left
4080eb6f0de0SAdrian Chadd  * edge won't match the ni->ni_txseq.
4081eb6f0de0SAdrian Chadd  *
4082eb6f0de0SAdrian Chadd  * So, being very dirty, the BAW left edge is "slid" here to match
4083eb6f0de0SAdrian Chadd  * ni->ni_txseq.
4084eb6f0de0SAdrian Chadd  *
4085eb6f0de0SAdrian Chadd  * What likely SHOULD happen is that all packets subsequent to the
4086eb6f0de0SAdrian Chadd  * addba request should be tagged as aggregate and queued as non-aggregate
4087eb6f0de0SAdrian Chadd  * frames; thus updating the BAW. For now though, I'll just slide the
4088eb6f0de0SAdrian Chadd  * window.
4089eb6f0de0SAdrian Chadd  */
4090eb6f0de0SAdrian Chadd int
4091eb6f0de0SAdrian Chadd ath_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
4092eb6f0de0SAdrian Chadd     int status, int code, int batimeout)
4093eb6f0de0SAdrian Chadd {
4094eb6f0de0SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
4095eb6f0de0SAdrian Chadd 	int tid = WME_AC_TO_TID(tap->txa_ac);
4096eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4097eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4098eb6f0de0SAdrian Chadd 	int r;
4099eb6f0de0SAdrian Chadd 
4100eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4101eb6f0de0SAdrian Chadd 	    "%s: called; status=%d, code=%d, batimeout=%d\n", __func__,
4102eb6f0de0SAdrian Chadd 	    status, code, batimeout);
4103eb6f0de0SAdrian Chadd 
4104eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4105eb6f0de0SAdrian Chadd 	    "%s: txa_start=%d, ni_txseqs=%d\n",
4106eb6f0de0SAdrian Chadd 	    __func__, tap->txa_start, ni->ni_txseqs[tid]);
4107eb6f0de0SAdrian Chadd 
4108eb6f0de0SAdrian Chadd 	/*
4109eb6f0de0SAdrian Chadd 	 * Call this first, so the interface flags get updated
4110eb6f0de0SAdrian Chadd 	 * before the TID is unpaused. Otherwise a race condition
4111eb6f0de0SAdrian Chadd 	 * exists where the unpaused TID still doesn't yet have
4112eb6f0de0SAdrian Chadd 	 * IEEE80211_AGGR_RUNNING set.
4113eb6f0de0SAdrian Chadd 	 */
4114eb6f0de0SAdrian Chadd 	r = sc->sc_addba_response(ni, tap, status, code, batimeout);
4115eb6f0de0SAdrian Chadd 
4116eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
4117eb6f0de0SAdrian Chadd 	/*
4118eb6f0de0SAdrian Chadd 	 * XXX dirty!
4119eb6f0de0SAdrian Chadd 	 * Slide the BAW left edge to wherever net80211 left it for us.
4120eb6f0de0SAdrian Chadd 	 * Read above for more information.
4121eb6f0de0SAdrian Chadd 	 */
4122eb6f0de0SAdrian Chadd 	tap->txa_start = ni->ni_txseqs[tid];
4123eb6f0de0SAdrian Chadd 	ath_tx_tid_resume(sc, atid);
4124eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4125eb6f0de0SAdrian Chadd 	return r;
4126eb6f0de0SAdrian Chadd }
4127eb6f0de0SAdrian Chadd 
4128eb6f0de0SAdrian Chadd 
4129eb6f0de0SAdrian Chadd /*
4130eb6f0de0SAdrian Chadd  * Stop ADDBA on a queue.
4131eb6f0de0SAdrian Chadd  */
4132eb6f0de0SAdrian Chadd void
4133eb6f0de0SAdrian Chadd ath_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
4134eb6f0de0SAdrian Chadd {
4135eb6f0de0SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
4136eb6f0de0SAdrian Chadd 	int tid = WME_AC_TO_TID(tap->txa_ac);
4137eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4138eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4139eb6f0de0SAdrian Chadd 
4140eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: called\n", __func__);
4141eb6f0de0SAdrian Chadd 
4142eb6f0de0SAdrian Chadd 	/* Pause TID traffic early, so there aren't any races */
4143eb6f0de0SAdrian Chadd 	ath_tx_tid_pause(sc, atid);
4144eb6f0de0SAdrian Chadd 
4145eb6f0de0SAdrian Chadd 	/* There's no need to hold the TXQ lock here */
4146eb6f0de0SAdrian Chadd 	sc->sc_addba_stop(ni, tap);
4147eb6f0de0SAdrian Chadd 
4148eb6f0de0SAdrian Chadd 	/*
4149eb6f0de0SAdrian Chadd 	 * ath_tx_cleanup will resume the TID if possible, otherwise
4150eb6f0de0SAdrian Chadd 	 * it'll set the cleanup flag, and it'll be unpaused once
4151eb6f0de0SAdrian Chadd 	 * things have been cleaned up.
4152eb6f0de0SAdrian Chadd 	 */
4153eb6f0de0SAdrian Chadd 	ath_tx_cleanup(sc, an, tid);
4154eb6f0de0SAdrian Chadd }
4155eb6f0de0SAdrian Chadd 
4156eb6f0de0SAdrian Chadd /*
4157eb6f0de0SAdrian Chadd  * Note: net80211 bar_timeout() doesn't call this function on BAR failure;
4158eb6f0de0SAdrian Chadd  * it simply tears down the aggregation session. Ew.
4159eb6f0de0SAdrian Chadd  *
4160eb6f0de0SAdrian Chadd  * It however will call ieee80211_ampdu_stop() which will call
4161eb6f0de0SAdrian Chadd  * ic->ic_addba_stop().
4162eb6f0de0SAdrian Chadd  *
4163eb6f0de0SAdrian Chadd  * XXX This uses a hard-coded max BAR count value; the whole
4164eb6f0de0SAdrian Chadd  * XXX BAR TX success or failure should be better handled!
4165eb6f0de0SAdrian Chadd  */
4166eb6f0de0SAdrian Chadd void
4167eb6f0de0SAdrian Chadd ath_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
4168eb6f0de0SAdrian Chadd     int status)
4169eb6f0de0SAdrian Chadd {
4170eb6f0de0SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
4171eb6f0de0SAdrian Chadd 	int tid = WME_AC_TO_TID(tap->txa_ac);
4172eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4173eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4174eb6f0de0SAdrian Chadd 	int attempts = tap->txa_attempts;
4175eb6f0de0SAdrian Chadd 
4176eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4177eb6f0de0SAdrian Chadd 	    "%s: called; status=%d\n", __func__, status);
4178eb6f0de0SAdrian Chadd 
4179eb6f0de0SAdrian Chadd 	/* Note: This may update the BAW details */
4180eb6f0de0SAdrian Chadd 	sc->sc_bar_response(ni, tap, status);
4181eb6f0de0SAdrian Chadd 
4182eb6f0de0SAdrian Chadd 	/* Unpause the TID */
4183eb6f0de0SAdrian Chadd 	/*
4184eb6f0de0SAdrian Chadd 	 * XXX if this is attempt=50, the TID will be downgraded
4185eb6f0de0SAdrian Chadd 	 * XXX to a non-aggregate session. So we must unpause the
4186eb6f0de0SAdrian Chadd 	 * XXX TID here or it'll never be done.
4187eb6f0de0SAdrian Chadd 	 */
4188eb6f0de0SAdrian Chadd 	if (status == 0 || attempts == 50) {
4189eb6f0de0SAdrian Chadd 		ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
4190eb6f0de0SAdrian Chadd 		ath_tx_tid_resume(sc, atid);
4191eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4192eb6f0de0SAdrian Chadd 	}
4193eb6f0de0SAdrian Chadd }
4194eb6f0de0SAdrian Chadd 
4195eb6f0de0SAdrian Chadd /*
4196eb6f0de0SAdrian Chadd  * This is called whenever the pending ADDBA request times out.
4197eb6f0de0SAdrian Chadd  * Unpause and reschedule the TID.
4198eb6f0de0SAdrian Chadd  */
4199eb6f0de0SAdrian Chadd void
4200eb6f0de0SAdrian Chadd ath_addba_response_timeout(struct ieee80211_node *ni,
4201eb6f0de0SAdrian Chadd     struct ieee80211_tx_ampdu *tap)
4202eb6f0de0SAdrian Chadd {
4203eb6f0de0SAdrian Chadd 	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
4204eb6f0de0SAdrian Chadd 	int tid = WME_AC_TO_TID(tap->txa_ac);
4205eb6f0de0SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
4206eb6f0de0SAdrian Chadd 	struct ath_tid *atid = &an->an_tid[tid];
4207eb6f0de0SAdrian Chadd 
4208eb6f0de0SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4209eb6f0de0SAdrian Chadd 	    "%s: called; resuming\n", __func__);
4210eb6f0de0SAdrian Chadd 
4211eb6f0de0SAdrian Chadd 	/* Note: This updates the aggregate state to (again) pending */
4212eb6f0de0SAdrian Chadd 	sc->sc_addba_response_timeout(ni, tap);
4213eb6f0de0SAdrian Chadd 
4214eb6f0de0SAdrian Chadd 	/* Unpause the TID; which reschedules it */
4215eb6f0de0SAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
4216eb6f0de0SAdrian Chadd 	ath_tx_tid_resume(sc, atid);
4217eb6f0de0SAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4218eb6f0de0SAdrian Chadd }
4219