if_ath_tx.c revision 248676
1218065Sadrian/*-
2218065Sadrian * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3240592Sadrian * Copyright (c) 2010-2012 Adrian Chadd, Xenion Pty Ltd
4218065Sadrian * All rights reserved.
5218065Sadrian *
6218065Sadrian * Redistribution and use in source and binary forms, with or without
7218065Sadrian * modification, are permitted provided that the following conditions
8218065Sadrian * are met:
9218065Sadrian * 1. Redistributions of source code must retain the above copyright
10218065Sadrian *    notice, this list of conditions and the following disclaimer,
11218065Sadrian *    without modification.
12218065Sadrian * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13218065Sadrian *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
14218065Sadrian *    redistribution must be conditioned upon including a substantially
15218065Sadrian *    similar Disclaimer requirement for further binary redistribution.
16218065Sadrian *
17218065Sadrian * NO WARRANTY
18218065Sadrian * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19218065Sadrian * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20218065Sadrian * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
21218065Sadrian * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22218065Sadrian * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
23218065Sadrian * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24218065Sadrian * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25218065Sadrian * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
26218065Sadrian * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27218065Sadrian * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28218065Sadrian * THE POSSIBILITY OF SUCH DAMAGES.
29218065Sadrian */
30218065Sadrian
31218065Sadrian#include <sys/cdefs.h>
32218065Sadrian__FBSDID("$FreeBSD: head/sys/dev/ath/if_ath_tx.c 248676 2013-03-24 04:09:54Z adrian $");
33218065Sadrian
34218065Sadrian/*
35218065Sadrian * Driver for the Atheros Wireless LAN controller.
36218065Sadrian *
37218065Sadrian * This software is derived from work of Atsushi Onoe; his contribution
38218065Sadrian * is greatly appreciated.
39218065Sadrian */
40218065Sadrian
41218065Sadrian#include "opt_inet.h"
42218065Sadrian#include "opt_ath.h"
43218065Sadrian#include "opt_wlan.h"
44218065Sadrian
45218065Sadrian#include <sys/param.h>
46218065Sadrian#include <sys/systm.h>
47218065Sadrian#include <sys/sysctl.h>
48218065Sadrian#include <sys/mbuf.h>
49218065Sadrian#include <sys/malloc.h>
50218065Sadrian#include <sys/lock.h>
51218065Sadrian#include <sys/mutex.h>
52218065Sadrian#include <sys/kernel.h>
53218065Sadrian#include <sys/socket.h>
54218065Sadrian#include <sys/sockio.h>
55218065Sadrian#include <sys/errno.h>
56218065Sadrian#include <sys/callout.h>
57218065Sadrian#include <sys/bus.h>
58218065Sadrian#include <sys/endian.h>
59218065Sadrian#include <sys/kthread.h>
60218065Sadrian#include <sys/taskqueue.h>
61218065Sadrian#include <sys/priv.h>
62218065Sadrian
63218065Sadrian#include <machine/bus.h>
64218065Sadrian
65218065Sadrian#include <net/if.h>
66218065Sadrian#include <net/if_dl.h>
67218065Sadrian#include <net/if_media.h>
68218065Sadrian#include <net/if_types.h>
69218065Sadrian#include <net/if_arp.h>
70218065Sadrian#include <net/ethernet.h>
71218065Sadrian#include <net/if_llc.h>
72218065Sadrian
73218065Sadrian#include <net80211/ieee80211_var.h>
74218065Sadrian#include <net80211/ieee80211_regdomain.h>
75218065Sadrian#ifdef IEEE80211_SUPPORT_SUPERG
76218065Sadrian#include <net80211/ieee80211_superg.h>
77218065Sadrian#endif
78218065Sadrian#ifdef IEEE80211_SUPPORT_TDMA
79218065Sadrian#include <net80211/ieee80211_tdma.h>
80218065Sadrian#endif
81227364Sadrian#include <net80211/ieee80211_ht.h>
82218065Sadrian
83218065Sadrian#include <net/bpf.h>
84218065Sadrian
85218065Sadrian#ifdef INET
86218065Sadrian#include <netinet/in.h>
87218065Sadrian#include <netinet/if_ether.h>
88218065Sadrian#endif
89218065Sadrian
90218065Sadrian#include <dev/ath/if_athvar.h>
91218065Sadrian#include <dev/ath/ath_hal/ah_devid.h>		/* XXX for softled */
92218065Sadrian#include <dev/ath/ath_hal/ah_diagcodes.h>
93218065Sadrian
94218065Sadrian#include <dev/ath/if_ath_debug.h>
95218065Sadrian
96218065Sadrian#ifdef ATH_TX99_DIAG
97218065Sadrian#include <dev/ath/ath_tx99/ath_tx99.h>
98218065Sadrian#endif
99218065Sadrian
100218065Sadrian#include <dev/ath/if_ath_misc.h>
101218065Sadrian#include <dev/ath/if_ath_tx.h>
102218240Sadrian#include <dev/ath/if_ath_tx_ht.h>
103218065Sadrian
104242782Sadrian#ifdef	ATH_DEBUG_ALQ
105242782Sadrian#include <dev/ath/if_ath_alq.h>
106242782Sadrian#endif
107242782Sadrian
108218154Sadrian/*
109227364Sadrian * How many retries to perform in software
110227364Sadrian */
111227364Sadrian#define	SWMAX_RETRIES		10
112227364Sadrian
113240946Sadrian/*
114240946Sadrian * What queue to throw the non-QoS TID traffic into
115240946Sadrian */
116240946Sadrian#define	ATH_NONQOS_TID_AC	WME_AC_VO
117240946Sadrian
118241170Sadrian#if 0
119241170Sadrianstatic int ath_tx_node_is_asleep(struct ath_softc *sc, struct ath_node *an);
120241170Sadrian#endif
121227364Sadrianstatic int ath_tx_ampdu_pending(struct ath_softc *sc, struct ath_node *an,
122227364Sadrian    int tid);
123227364Sadrianstatic int ath_tx_ampdu_running(struct ath_softc *sc, struct ath_node *an,
124227364Sadrian    int tid);
125236872Sadrianstatic ieee80211_seq ath_tx_tid_seqno_assign(struct ath_softc *sc,
126236872Sadrian    struct ieee80211_node *ni, struct ath_buf *bf, struct mbuf *m0);
127227364Sadrianstatic int ath_tx_action_frame_override_queue(struct ath_softc *sc,
128227364Sadrian    struct ieee80211_node *ni, struct mbuf *m0, int *tid);
129240639Sadrianstatic struct ath_buf *
130240639Sadrianath_tx_retry_clone(struct ath_softc *sc, struct ath_node *an,
131240639Sadrian    struct ath_tid *tid, struct ath_buf *bf);
132227364Sadrian
133243162Sadrian#ifdef	ATH_DEBUG_ALQ
134243162Sadrianvoid
135243162Sadrianath_tx_alq_post(struct ath_softc *sc, struct ath_buf *bf_first)
136243162Sadrian{
137243162Sadrian	struct ath_buf *bf;
138243162Sadrian	int i, n;
139243162Sadrian	const char *ds;
140243162Sadrian
141243162Sadrian	/* XXX we should skip out early if debugging isn't enabled! */
142243162Sadrian	bf = bf_first;
143243162Sadrian
144243162Sadrian	while (bf != NULL) {
145243162Sadrian		/* XXX should ensure bf_nseg > 0! */
146243162Sadrian		if (bf->bf_nseg == 0)
147243162Sadrian			break;
148243162Sadrian		n = ((bf->bf_nseg - 1) / sc->sc_tx_nmaps) + 1;
149243162Sadrian		for (i = 0, ds = (const char *) bf->bf_desc;
150243162Sadrian		    i < n;
151243162Sadrian		    i++, ds += sc->sc_tx_desclen) {
152243162Sadrian			if_ath_alq_post(&sc->sc_alq,
153243162Sadrian			    ATH_ALQ_EDMA_TXDESC,
154243162Sadrian			    sc->sc_tx_desclen,
155243162Sadrian			    ds);
156243162Sadrian		}
157243162Sadrian		bf = bf->bf_next;
158243162Sadrian	}
159243162Sadrian}
160243162Sadrian#endif /* ATH_DEBUG_ALQ */
161243162Sadrian
162227364Sadrian/*
163218154Sadrian * Whether to use the 11n rate scenario functions or not
164218154Sadrian */
165218154Sadrianstatic inline int
166218154Sadrianath_tx_is_11n(struct ath_softc *sc)
167218154Sadrian{
168239198Sadrian	return ((sc->sc_ah->ah_magic == 0x20065416) ||
169239198Sadrian		    (sc->sc_ah->ah_magic == 0x19741014));
170218154Sadrian}
171218154Sadrian
172227364Sadrian/*
173227364Sadrian * Obtain the current TID from the given frame.
174227364Sadrian *
175227364Sadrian * Non-QoS frames need to go into TID 16 (IEEE80211_NONQOS_TID.)
176227364Sadrian * This has implications for which AC/priority the packet is placed
177227364Sadrian * in.
178227364Sadrian */
179227364Sadrianstatic int
180227364Sadrianath_tx_gettid(struct ath_softc *sc, const struct mbuf *m0)
181227364Sadrian{
182227364Sadrian	const struct ieee80211_frame *wh;
183227364Sadrian	int pri = M_WME_GETAC(m0);
184227364Sadrian
185227364Sadrian	wh = mtod(m0, const struct ieee80211_frame *);
186227364Sadrian	if (! IEEE80211_QOS_HAS_SEQ(wh))
187227364Sadrian		return IEEE80211_NONQOS_TID;
188227364Sadrian	else
189227364Sadrian		return WME_AC_TO_TID(pri);
190227364Sadrian}
191227364Sadrian
192240639Sadrianstatic void
193240639Sadrianath_tx_set_retry(struct ath_softc *sc, struct ath_buf *bf)
194240639Sadrian{
195240639Sadrian	struct ieee80211_frame *wh;
196240639Sadrian
197240639Sadrian	wh = mtod(bf->bf_m, struct ieee80211_frame *);
198240639Sadrian	/* Only update/resync if needed */
199240639Sadrian	if (bf->bf_state.bfs_isretried == 0) {
200240639Sadrian		wh->i_fc[1] |= IEEE80211_FC1_RETRY;
201240639Sadrian		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
202240639Sadrian		    BUS_DMASYNC_PREWRITE);
203240639Sadrian	}
204240639Sadrian	bf->bf_state.bfs_isretried = 1;
205240639Sadrian	bf->bf_state.bfs_retries ++;
206240639Sadrian}
207240639Sadrian
208227364Sadrian/*
209227364Sadrian * Determine what the correct AC queue for the given frame
210227364Sadrian * should be.
211227364Sadrian *
212227364Sadrian * This code assumes that the TIDs map consistently to
213227364Sadrian * the underlying hardware (or software) ath_txq.
214227364Sadrian * Since the sender may try to set an AC which is
215227364Sadrian * arbitrary, non-QoS TIDs may end up being put on
216227364Sadrian * completely different ACs. There's no way to put a
217227364Sadrian * TID into multiple ath_txq's for scheduling, so
218227364Sadrian * for now we override the AC/TXQ selection and set
219227364Sadrian * non-QOS TID frames into the BE queue.
220227364Sadrian *
221227364Sadrian * This may be completely incorrect - specifically,
222227364Sadrian * some management frames may end up out of order
223227364Sadrian * compared to the QoS traffic they're controlling.
224227364Sadrian * I'll look into this later.
225227364Sadrian */
226227364Sadrianstatic int
227227364Sadrianath_tx_getac(struct ath_softc *sc, const struct mbuf *m0)
228227364Sadrian{
229227364Sadrian	const struct ieee80211_frame *wh;
230227364Sadrian	int pri = M_WME_GETAC(m0);
231227364Sadrian	wh = mtod(m0, const struct ieee80211_frame *);
232227364Sadrian	if (IEEE80211_QOS_HAS_SEQ(wh))
233227364Sadrian		return pri;
234227364Sadrian
235240946Sadrian	return ATH_NONQOS_TID_AC;
236227364Sadrian}
237227364Sadrian
238218065Sadrianvoid
239218065Sadrianath_txfrag_cleanup(struct ath_softc *sc,
240218065Sadrian	ath_bufhead *frags, struct ieee80211_node *ni)
241218065Sadrian{
242218065Sadrian	struct ath_buf *bf, *next;
243218065Sadrian
244218065Sadrian	ATH_TXBUF_LOCK_ASSERT(sc);
245218065Sadrian
246227344Sadrian	TAILQ_FOREACH_SAFE(bf, frags, bf_list, next) {
247218065Sadrian		/* NB: bf assumed clean */
248227344Sadrian		TAILQ_REMOVE(frags, bf, bf_list);
249236993Sadrian		ath_returnbuf_head(sc, bf);
250218065Sadrian		ieee80211_node_decref(ni);
251218065Sadrian	}
252218065Sadrian}
253218065Sadrian
254218065Sadrian/*
255218065Sadrian * Setup xmit of a fragmented frame.  Allocate a buffer
256218065Sadrian * for each frag and bump the node reference count to
257218065Sadrian * reflect the held reference to be setup by ath_tx_start.
258218065Sadrian */
259218065Sadrianint
260218065Sadrianath_txfrag_setup(struct ath_softc *sc, ath_bufhead *frags,
261218065Sadrian	struct mbuf *m0, struct ieee80211_node *ni)
262218065Sadrian{
263218065Sadrian	struct mbuf *m;
264218065Sadrian	struct ath_buf *bf;
265218065Sadrian
266218065Sadrian	ATH_TXBUF_LOCK(sc);
267218065Sadrian	for (m = m0->m_nextpkt; m != NULL; m = m->m_nextpkt) {
268237000Sadrian		/* XXX non-management? */
269237000Sadrian		bf = _ath_getbuf_locked(sc, ATH_BUFTYPE_NORMAL);
270218065Sadrian		if (bf == NULL) {	/* out of buffers, cleanup */
271234009Sadrian			device_printf(sc->sc_dev, "%s: no buffer?\n",
272234009Sadrian			    __func__);
273218065Sadrian			ath_txfrag_cleanup(sc, frags, ni);
274218065Sadrian			break;
275218065Sadrian		}
276218065Sadrian		ieee80211_node_incref(ni);
277227344Sadrian		TAILQ_INSERT_TAIL(frags, bf, bf_list);
278218065Sadrian	}
279218065Sadrian	ATH_TXBUF_UNLOCK(sc);
280218065Sadrian
281227344Sadrian	return !TAILQ_EMPTY(frags);
282218065Sadrian}
283218065Sadrian
284218065Sadrian/*
285218065Sadrian * Reclaim mbuf resources.  For fragmented frames we
286218065Sadrian * need to claim each frag chained with m_nextpkt.
287218065Sadrian */
288218065Sadrianvoid
289218065Sadrianath_freetx(struct mbuf *m)
290218065Sadrian{
291218065Sadrian	struct mbuf *next;
292218065Sadrian
293218065Sadrian	do {
294218065Sadrian		next = m->m_nextpkt;
295218065Sadrian		m->m_nextpkt = NULL;
296218065Sadrian		m_freem(m);
297218065Sadrian	} while ((m = next) != NULL);
298218065Sadrian}
299218065Sadrian
300218065Sadrianstatic int
301218065Sadrianath_tx_dmasetup(struct ath_softc *sc, struct ath_buf *bf, struct mbuf *m0)
302218065Sadrian{
303218065Sadrian	struct mbuf *m;
304218065Sadrian	int error;
305218065Sadrian
306218065Sadrian	/*
307218065Sadrian	 * Load the DMA map so any coalescing is done.  This
308218065Sadrian	 * also calculates the number of descriptors we need.
309218065Sadrian	 */
310218065Sadrian	error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0,
311218065Sadrian				     bf->bf_segs, &bf->bf_nseg,
312218065Sadrian				     BUS_DMA_NOWAIT);
313218065Sadrian	if (error == EFBIG) {
314218065Sadrian		/* XXX packet requires too many descriptors */
315218065Sadrian		bf->bf_nseg = ATH_TXDESC+1;
316218065Sadrian	} else if (error != 0) {
317218065Sadrian		sc->sc_stats.ast_tx_busdma++;
318218065Sadrian		ath_freetx(m0);
319218065Sadrian		return error;
320218065Sadrian	}
321218065Sadrian	/*
322218065Sadrian	 * Discard null packets and check for packets that
323218065Sadrian	 * require too many TX descriptors.  We try to convert
324218065Sadrian	 * the latter to a cluster.
325218065Sadrian	 */
326218065Sadrian	if (bf->bf_nseg > ATH_TXDESC) {		/* too many desc's, linearize */
327218065Sadrian		sc->sc_stats.ast_tx_linear++;
328243857Sglebius		m = m_collapse(m0, M_NOWAIT, ATH_TXDESC);
329218065Sadrian		if (m == NULL) {
330218065Sadrian			ath_freetx(m0);
331218065Sadrian			sc->sc_stats.ast_tx_nombuf++;
332218065Sadrian			return ENOMEM;
333218065Sadrian		}
334218065Sadrian		m0 = m;
335218065Sadrian		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0,
336218065Sadrian					     bf->bf_segs, &bf->bf_nseg,
337218065Sadrian					     BUS_DMA_NOWAIT);
338218065Sadrian		if (error != 0) {
339218065Sadrian			sc->sc_stats.ast_tx_busdma++;
340218065Sadrian			ath_freetx(m0);
341218065Sadrian			return error;
342218065Sadrian		}
343218065Sadrian		KASSERT(bf->bf_nseg <= ATH_TXDESC,
344218065Sadrian		    ("too many segments after defrag; nseg %u", bf->bf_nseg));
345218065Sadrian	} else if (bf->bf_nseg == 0) {		/* null packet, discard */
346218065Sadrian		sc->sc_stats.ast_tx_nodata++;
347218065Sadrian		ath_freetx(m0);
348218065Sadrian		return EIO;
349218065Sadrian	}
350218065Sadrian	DPRINTF(sc, ATH_DEBUG_XMIT, "%s: m %p len %u\n",
351218065Sadrian		__func__, m0, m0->m_pkthdr.len);
352218065Sadrian	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
353218065Sadrian	bf->bf_m = m0;
354218065Sadrian
355218065Sadrian	return 0;
356218065Sadrian}
357218065Sadrian
358227360Sadrian/*
359242656Sadrian * Chain together segments+descriptors for a frame - 11n or otherwise.
360242656Sadrian *
361242656Sadrian * For aggregates, this is called on each frame in the aggregate.
362227360Sadrian */
363218065Sadrianstatic void
364242656Sadrianath_tx_chaindesclist(struct ath_softc *sc, struct ath_desc *ds0,
365242656Sadrian    struct ath_buf *bf, int is_aggr, int is_first_subframe,
366242656Sadrian    int is_last_subframe)
367218065Sadrian{
368218065Sadrian	struct ath_hal *ah = sc->sc_ah;
369242656Sadrian	char *ds;
370239290Sadrian	int i, bp, dsp;
371239051Sadrian	HAL_DMA_ADDR bufAddrList[4];
372239051Sadrian	uint32_t segLenList[4];
373239290Sadrian	int numTxMaps = 1;
374239380Sadrian	int isFirstDesc = 1;
375239051Sadrian
376238708Sadrian	/*
377238708Sadrian	 * XXX There's txdma and txdma_mgmt; the descriptor
378238708Sadrian	 * sizes must match.
379238708Sadrian	 */
380238708Sadrian	struct ath_descdma *dd = &sc->sc_txdma;
381218065Sadrian
382218065Sadrian	/*
383218065Sadrian	 * Fillin the remainder of the descriptor info.
384218065Sadrian	 */
385239290Sadrian
386239290Sadrian	/*
387248527Sadrian	 * We need the number of TX data pointers in each descriptor.
388248527Sadrian	 * EDMA and later chips support 4 TX buffers per descriptor;
389248527Sadrian	 * previous chips just support one.
390239290Sadrian	 */
391248527Sadrian	numTxMaps = sc->sc_tx_nmaps;
392239290Sadrian
393239290Sadrian	/*
394239290Sadrian	 * For EDMA and later chips ensure the TX map is fully populated
395239290Sadrian	 * before advancing to the next descriptor.
396239290Sadrian	 */
397242656Sadrian	ds = (char *) bf->bf_desc;
398239290Sadrian	bp = dsp = 0;
399239290Sadrian	bzero(bufAddrList, sizeof(bufAddrList));
400239290Sadrian	bzero(segLenList, sizeof(segLenList));
401239290Sadrian	for (i = 0; i < bf->bf_nseg; i++) {
402239290Sadrian		bufAddrList[bp] = bf->bf_segs[i].ds_addr;
403239290Sadrian		segLenList[bp] = bf->bf_segs[i].ds_len;
404239290Sadrian		bp++;
405239051Sadrian
406239290Sadrian		/*
407239290Sadrian		 * Go to the next segment if this isn't the last segment
408239290Sadrian		 * and there's space in the current TX map.
409239290Sadrian		 */
410239290Sadrian		if ((i != bf->bf_nseg - 1) && (bp < numTxMaps))
411239290Sadrian			continue;
412239051Sadrian
413239290Sadrian		/*
414239290Sadrian		 * Last segment or we're out of buffer pointers.
415239290Sadrian		 */
416239290Sadrian		bp = 0;
417239290Sadrian
418218065Sadrian		if (i == bf->bf_nseg - 1)
419239409Sadrian			ath_hal_settxdesclink(ah, (struct ath_desc *) ds, 0);
420218065Sadrian		else
421239409Sadrian			ath_hal_settxdesclink(ah, (struct ath_desc *) ds,
422239290Sadrian			    bf->bf_daddr + dd->dd_descsize * (dsp + 1));
423239051Sadrian
424239051Sadrian		/*
425244109Sadrian		 * XXX This assumes that bfs_txq is the actual destination
426244109Sadrian		 * hardware queue at this point.  It may not have been
427244109Sadrian		 * assigned, it may actually be pointing to the multicast
428244109Sadrian		 * software TXQ id.  These must be fixed!
429239051Sadrian		 */
430239409Sadrian		ath_hal_filltxdesc(ah, (struct ath_desc *) ds
431239051Sadrian			, bufAddrList
432239051Sadrian			, segLenList
433239290Sadrian			, bf->bf_descid		/* XXX desc id */
434244109Sadrian			, bf->bf_state.bfs_tx_queue
435239380Sadrian			, isFirstDesc		/* first segment */
436218065Sadrian			, i == bf->bf_nseg - 1	/* last segment */
437239409Sadrian			, (struct ath_desc *) ds0	/* first descriptor */
438218065Sadrian		);
439240255Sadrian
440242656Sadrian		/*
441242656Sadrian		 * Make sure the 11n aggregate fields are cleared.
442242656Sadrian		 *
443242656Sadrian		 * XXX TODO: this doesn't need to be called for
444242656Sadrian		 * aggregate frames; as it'll be called on all
445242656Sadrian		 * sub-frames.  Since the descriptors are in
446242656Sadrian		 * non-cacheable memory, this leads to some
447242656Sadrian		 * rather slow writes on MIPS/ARM platforms.
448242656Sadrian		 */
449240255Sadrian		if (ath_tx_is_11n(sc))
450240333Sadrian			ath_hal_clr11n_aggr(sc->sc_ah, (struct ath_desc *) ds);
451240255Sadrian
452242656Sadrian		/*
453242656Sadrian		 * If 11n is enabled, set it up as if it's an aggregate
454242656Sadrian		 * frame.
455242656Sadrian		 */
456242656Sadrian		if (is_last_subframe) {
457242656Sadrian			ath_hal_set11n_aggr_last(sc->sc_ah,
458242656Sadrian			    (struct ath_desc *) ds);
459242656Sadrian		} else if (is_aggr) {
460242656Sadrian			/*
461242656Sadrian			 * This clears the aggrlen field; so
462242656Sadrian			 * the caller needs to call set_aggr_first()!
463242656Sadrian			 *
464242656Sadrian			 * XXX TODO: don't call this for the first
465242656Sadrian			 * descriptor in the first frame in an
466242656Sadrian			 * aggregate!
467242656Sadrian			 */
468242656Sadrian			ath_hal_set11n_aggr_middle(sc->sc_ah,
469242656Sadrian			    (struct ath_desc *) ds,
470242656Sadrian			    bf->bf_state.bfs_ndelim);
471242656Sadrian		}
472239380Sadrian		isFirstDesc = 0;
473239409Sadrian		bf->bf_lastds = (struct ath_desc *) ds;
474239290Sadrian
475239290Sadrian		/*
476239290Sadrian		 * Don't forget to skip to the next descriptor.
477239290Sadrian		 */
478239409Sadrian		ds += sc->sc_tx_desclen;
479239290Sadrian		dsp++;
480239290Sadrian
481239290Sadrian		/*
482239290Sadrian		 * .. and don't forget to blank these out!
483239290Sadrian		 */
484239290Sadrian		bzero(bufAddrList, sizeof(bufAddrList));
485239290Sadrian		bzero(segLenList, sizeof(segLenList));
486218065Sadrian	}
487233990Sadrian	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
488218154Sadrian}
489218154Sadrian
490227364Sadrian/*
491238947Sadrian * Set the rate control fields in the given descriptor based on
492238947Sadrian * the bf_state fields and node state.
493238947Sadrian *
494238947Sadrian * The bfs fields should already be set with the relevant rate
495238947Sadrian * control information, including whether MRR is to be enabled.
496238947Sadrian *
497238947Sadrian * Since the FreeBSD HAL currently sets up the first TX rate
498238947Sadrian * in ath_hal_setuptxdesc(), this will setup the MRR
499238947Sadrian * conditionally for the pre-11n chips, and call ath_buf_set_rate
500238947Sadrian * unconditionally for 11n chips. These require the 11n rate
501238947Sadrian * scenario to be set if MCS rates are enabled, so it's easier
502238947Sadrian * to just always call it. The caller can then only set rates 2, 3
503238947Sadrian * and 4 if multi-rate retry is needed.
504238947Sadrian */
505238947Sadrianstatic void
506238947Sadrianath_tx_set_ratectrl(struct ath_softc *sc, struct ieee80211_node *ni,
507238947Sadrian    struct ath_buf *bf)
508238947Sadrian{
509238947Sadrian	struct ath_rc_series *rc = bf->bf_state.bfs_rc;
510238947Sadrian
511238947Sadrian	/* If mrr is disabled, blank tries 1, 2, 3 */
512238947Sadrian	if (! bf->bf_state.bfs_ismrr)
513238947Sadrian		rc[1].tries = rc[2].tries = rc[3].tries = 0;
514238947Sadrian
515243647Sadrian#if 0
516238947Sadrian	/*
517243647Sadrian	 * If NOACK is set, just set ntries=1.
518243647Sadrian	 */
519243647Sadrian	else if (bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) {
520243647Sadrian		rc[1].tries = rc[2].tries = rc[3].tries = 0;
521243647Sadrian		rc[0].tries = 1;
522243647Sadrian	}
523243647Sadrian#endif
524243647Sadrian
525243647Sadrian	/*
526238947Sadrian	 * Always call - that way a retried descriptor will
527238947Sadrian	 * have the MRR fields overwritten.
528238947Sadrian	 *
529238947Sadrian	 * XXX TODO: see if this is really needed - setting up
530238947Sadrian	 * the first descriptor should set the MRR fields to 0
531238947Sadrian	 * for us anyway.
532238947Sadrian	 */
533238947Sadrian	if (ath_tx_is_11n(sc)) {
534238947Sadrian		ath_buf_set_rate(sc, ni, bf);
535238947Sadrian	} else {
536238947Sadrian		ath_hal_setupxtxdesc(sc->sc_ah, bf->bf_desc
537238947Sadrian			, rc[1].ratecode, rc[1].tries
538238947Sadrian			, rc[2].ratecode, rc[2].tries
539238947Sadrian			, rc[3].ratecode, rc[3].tries
540238947Sadrian		);
541238947Sadrian	}
542238947Sadrian}
543238947Sadrian
544238947Sadrian/*
545227364Sadrian * Setup segments+descriptors for an 11n aggregate.
546227364Sadrian * bf_first is the first buffer in the aggregate.
547227364Sadrian * The descriptor list must already been linked together using
548227364Sadrian * bf->bf_next.
549227364Sadrian */
550227364Sadrianstatic void
551227364Sadrianath_tx_setds_11n(struct ath_softc *sc, struct ath_buf *bf_first)
552227364Sadrian{
553227364Sadrian	struct ath_buf *bf, *bf_prev = NULL;
554242656Sadrian	struct ath_desc *ds0 = bf_first->bf_desc;
555227364Sadrian
556227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: nframes=%d, al=%d\n",
557227364Sadrian	    __func__, bf_first->bf_state.bfs_nframes,
558227364Sadrian	    bf_first->bf_state.bfs_al);
559227364Sadrian
560242951Sadrian	bf = bf_first;
561242951Sadrian
562242951Sadrian	if (bf->bf_state.bfs_txrate0 == 0)
563242951Sadrian		device_printf(sc->sc_dev, "%s: bf=%p, txrate0=%d\n",
564242951Sadrian		    __func__, bf, 0);
565242951Sadrian	if (bf->bf_state.bfs_rc[0].ratecode == 0)
566242951Sadrian		device_printf(sc->sc_dev, "%s: bf=%p, rix0=%d\n",
567242951Sadrian		    __func__, bf, 0);
568242951Sadrian
569227364Sadrian	/*
570242656Sadrian	 * Setup all descriptors of all subframes - this will
571242656Sadrian	 * call ath_hal_set11naggrmiddle() on every frame.
572227364Sadrian	 */
573227364Sadrian	while (bf != NULL) {
574227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
575227364Sadrian		    "%s: bf=%p, nseg=%d, pktlen=%d, seqno=%d\n",
576227364Sadrian		    __func__, bf, bf->bf_nseg, bf->bf_state.bfs_pktlen,
577227364Sadrian		    SEQNO(bf->bf_state.bfs_seqno));
578227364Sadrian
579242656Sadrian		/*
580242656Sadrian		 * Setup the initial fields for the first descriptor - all
581242656Sadrian		 * the non-11n specific stuff.
582242656Sadrian		 */
583242656Sadrian		ath_hal_setuptxdesc(sc->sc_ah, bf->bf_desc
584242656Sadrian			, bf->bf_state.bfs_pktlen	/* packet length */
585242656Sadrian			, bf->bf_state.bfs_hdrlen	/* header length */
586242656Sadrian			, bf->bf_state.bfs_atype	/* Atheros packet type */
587242656Sadrian			, bf->bf_state.bfs_txpower	/* txpower */
588242656Sadrian			, bf->bf_state.bfs_txrate0
589242656Sadrian			, bf->bf_state.bfs_try0		/* series 0 rate/tries */
590242656Sadrian			, bf->bf_state.bfs_keyix	/* key cache index */
591242656Sadrian			, bf->bf_state.bfs_txantenna	/* antenna mode */
592242656Sadrian			, bf->bf_state.bfs_txflags | HAL_TXDESC_INTREQ	/* flags */
593242656Sadrian			, bf->bf_state.bfs_ctsrate	/* rts/cts rate */
594242656Sadrian			, bf->bf_state.bfs_ctsduration	/* rts/cts duration */
595242656Sadrian		);
596227364Sadrian
597227364Sadrian		/*
598242656Sadrian		 * First descriptor? Setup the rate control and initial
599242656Sadrian		 * aggregate header information.
600242656Sadrian		 */
601242656Sadrian		if (bf == bf_first) {
602242656Sadrian			/*
603242656Sadrian			 * setup first desc with rate and aggr info
604242656Sadrian			 */
605242656Sadrian			ath_tx_set_ratectrl(sc, bf->bf_node, bf);
606242656Sadrian		}
607242656Sadrian
608242656Sadrian		/*
609242656Sadrian		 * Setup the descriptors for a multi-descriptor frame.
610242656Sadrian		 * This is both aggregate and non-aggregate aware.
611242656Sadrian		 */
612242656Sadrian		ath_tx_chaindesclist(sc, ds0, bf,
613242656Sadrian		    1, /* is_aggr */
614242656Sadrian		    !! (bf == bf_first), /* is_first_subframe */
615242656Sadrian		    !! (bf->bf_next == NULL) /* is_last_subframe */
616242656Sadrian		    );
617242656Sadrian
618242656Sadrian		if (bf == bf_first) {
619242656Sadrian			/*
620242656Sadrian			 * Initialise the first 11n aggregate with the
621242656Sadrian			 * aggregate length and aggregate enable bits.
622242656Sadrian			 */
623242656Sadrian			ath_hal_set11n_aggr_first(sc->sc_ah,
624242656Sadrian			    ds0,
625242656Sadrian			    bf->bf_state.bfs_al,
626242656Sadrian			    bf->bf_state.bfs_ndelim);
627242656Sadrian		}
628242656Sadrian
629242656Sadrian		/*
630227364Sadrian		 * Link the last descriptor of the previous frame
631227364Sadrian		 * to the beginning descriptor of this frame.
632227364Sadrian		 */
633227364Sadrian		if (bf_prev != NULL)
634238609Sadrian			ath_hal_settxdesclink(sc->sc_ah, bf_prev->bf_lastds,
635238609Sadrian			    bf->bf_daddr);
636227364Sadrian
637227364Sadrian		/* Save a copy so we can link the next descriptor in */
638227364Sadrian		bf_prev = bf;
639227364Sadrian		bf = bf->bf_next;
640227364Sadrian	}
641227364Sadrian
642227364Sadrian	/*
643227364Sadrian	 * Set the first descriptor bf_lastds field to point to
644227364Sadrian	 * the last descriptor in the last subframe, that's where
645227364Sadrian	 * the status update will occur.
646227364Sadrian	 */
647227364Sadrian	bf_first->bf_lastds = bf_prev->bf_lastds;
648227364Sadrian
649227364Sadrian	/*
650227364Sadrian	 * And bf_last in the first descriptor points to the end of
651227364Sadrian	 * the aggregate list.
652227364Sadrian	 */
653227364Sadrian	bf_first->bf_last = bf_prev;
654227364Sadrian
655243047Sadrian	/*
656243047Sadrian	 * For non-AR9300 NICs, which require the rate control
657243047Sadrian	 * in the final descriptor - let's set that up now.
658243047Sadrian	 *
659243047Sadrian	 * This is because the filltxdesc() HAL call doesn't
660243047Sadrian	 * populate the last segment with rate control information
661243047Sadrian	 * if firstSeg is also true.  For non-aggregate frames
662243047Sadrian	 * that is fine, as the first frame already has rate control
663243047Sadrian	 * info.  But if the last frame in an aggregate has one
664243047Sadrian	 * descriptor, both firstseg and lastseg will be true and
665243047Sadrian	 * the rate info isn't copied.
666243047Sadrian	 *
667243047Sadrian	 * This is inefficient on MIPS/ARM platforms that have
668243047Sadrian	 * non-cachable memory for TX descriptors, but we'll just
669243047Sadrian	 * make do for now.
670243047Sadrian	 *
671243047Sadrian	 * As to why the rate table is stashed in the last descriptor
672243047Sadrian	 * rather than the first descriptor?  Because proctxdesc()
673243047Sadrian	 * is called on the final descriptor in an MPDU or A-MPDU -
674243047Sadrian	 * ie, the one that gets updated by the hardware upon
675243047Sadrian	 * completion.  That way proctxdesc() doesn't need to know
676243047Sadrian	 * about the first _and_ last TX descriptor.
677243047Sadrian	 */
678243047Sadrian	ath_hal_setuplasttxdesc(sc->sc_ah, bf_prev->bf_lastds, ds0);
679243047Sadrian
680227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: end\n", __func__);
681227364Sadrian}
682227364Sadrian
683239051Sadrian/*
684239051Sadrian * Hand-off a frame to the multicast TX queue.
685239051Sadrian *
686239051Sadrian * This is a software TXQ which will be appended to the CAB queue
687239051Sadrian * during the beacon setup code.
688239051Sadrian *
689239051Sadrian * XXX TODO: since the AR9300 EDMA TX queue support wants the QCU ID
690244109Sadrian * as part of the TX descriptor, bf_state.bfs_tx_queue must be updated
691239051Sadrian * with the actual hardware txq, or all of this will fall apart.
692239051Sadrian *
693239051Sadrian * XXX It may not be a bad idea to just stuff the QCU ID into bf_state
694244109Sadrian * and retire bfs_tx_queue; then make sure the CABQ QCU ID is populated
695239051Sadrian * correctly.
696239051Sadrian */
697227364Sadrianstatic void
698227364Sadrianath_tx_handoff_mcast(struct ath_softc *sc, struct ath_txq *txq,
699227364Sadrian    struct ath_buf *bf)
700227364Sadrian{
701243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
702243786Sadrian
703227364Sadrian	KASSERT((bf->bf_flags & ATH_BUF_BUSY) == 0,
704227364Sadrian	     ("%s: busy status 0x%x", __func__, bf->bf_flags));
705248676Sadrian
706248676Sadrian	ATH_TXQ_LOCK(txq);
707227364Sadrian	if (txq->axq_link != NULL) {
708227364Sadrian		struct ath_buf *last = ATH_TXQ_LAST(txq, axq_q_s);
709227364Sadrian		struct ieee80211_frame *wh;
710227364Sadrian
711227364Sadrian		/* mark previous frame */
712227364Sadrian		wh = mtod(last->bf_m, struct ieee80211_frame *);
713227364Sadrian		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
714227364Sadrian		bus_dmamap_sync(sc->sc_dmat, last->bf_dmamap,
715227364Sadrian		    BUS_DMASYNC_PREWRITE);
716227364Sadrian
717227364Sadrian		/* link descriptor */
718227364Sadrian		*txq->axq_link = bf->bf_daddr;
719227364Sadrian	}
720227364Sadrian	ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
721238609Sadrian	ath_hal_gettxdesclinkptr(sc->sc_ah, bf->bf_lastds, &txq->axq_link);
722248671Sadrian	ATH_TXQ_UNLOCK(txq);
723227364Sadrian}
724227364Sadrian
725227364Sadrian/*
726227364Sadrian * Hand-off packet to a hardware queue.
727227364Sadrian */
728227364Sadrianstatic void
729229949Sadrianath_tx_handoff_hw(struct ath_softc *sc, struct ath_txq *txq,
730229949Sadrian    struct ath_buf *bf)
731227364Sadrian{
732227364Sadrian	struct ath_hal *ah = sc->sc_ah;
733227364Sadrian
734227364Sadrian	/*
735218065Sadrian	 * Insert the frame on the outbound list and pass it on
736218065Sadrian	 * to the hardware.  Multicast frames buffered for power
737218065Sadrian	 * save stations and transmit from the CAB queue are stored
738218065Sadrian	 * on a s/w only queue and loaded on to the CAB queue in
739218065Sadrian	 * the SWBA handler since frames only go out on DTIM and
740218065Sadrian	 * to avoid possible races.
741218065Sadrian	 */
742243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
743218065Sadrian	KASSERT((bf->bf_flags & ATH_BUF_BUSY) == 0,
744227364Sadrian	     ("%s: busy status 0x%x", __func__, bf->bf_flags));
745227364Sadrian	KASSERT(txq->axq_qnum != ATH_TXQ_SWQ,
746227364Sadrian	     ("ath_tx_handoff_hw called for mcast queue"));
747227364Sadrian
748227651Sadrian#if 0
749227651Sadrian	/*
750227651Sadrian	 * This causes a LOR. Find out where the PCU lock is being
751227651Sadrian	 * held whilst the TXQ lock is grabbed - that shouldn't
752227651Sadrian	 * be occuring.
753227651Sadrian	 */
754227651Sadrian	ATH_PCU_LOCK(sc);
755227651Sadrian	if (sc->sc_inreset_cnt) {
756227651Sadrian		ATH_PCU_UNLOCK(sc);
757227651Sadrian		DPRINTF(sc, ATH_DEBUG_RESET,
758227651Sadrian		    "%s: called with sc_in_reset != 0\n",
759227651Sadrian		    __func__);
760227651Sadrian		DPRINTF(sc, ATH_DEBUG_XMIT,
761227651Sadrian		    "%s: queued: TXDP[%u] = %p (%p) depth %d\n",
762227651Sadrian		    __func__, txq->axq_qnum,
763227651Sadrian		    (caddr_t)bf->bf_daddr, bf->bf_desc,
764227651Sadrian		    txq->axq_depth);
765227651Sadrian		ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
766227651Sadrian		if (bf->bf_state.bfs_aggr)
767227651Sadrian			txq->axq_aggr_depth++;
768227651Sadrian		/*
769227651Sadrian		 * There's no need to update axq_link; the hardware
770227651Sadrian		 * is in reset and once the reset is complete, any
771227651Sadrian		 * non-empty queues will simply have DMA restarted.
772227651Sadrian		 */
773227651Sadrian		return;
774227651Sadrian		}
775227651Sadrian	ATH_PCU_UNLOCK(sc);
776227651Sadrian#endif
777227651Sadrian
778227364Sadrian	/* For now, so not to generate whitespace diffs */
779227364Sadrian	if (1) {
780248671Sadrian		ATH_TXQ_LOCK(txq);
781218065Sadrian#ifdef IEEE80211_SUPPORT_TDMA
782218065Sadrian		int qbusy;
783218065Sadrian
784218065Sadrian		ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
785218065Sadrian		qbusy = ath_hal_txqenabled(ah, txq->axq_qnum);
786240899Sadrian
787240899Sadrian		ATH_KTR(sc, ATH_KTR_TX, 4,
788240899Sadrian		    "ath_tx_handoff: txq=%u, add bf=%p, qbusy=%d, depth=%d",
789240899Sadrian		    txq->axq_qnum, bf, qbusy, txq->axq_depth);
790218065Sadrian		if (txq->axq_link == NULL) {
791218065Sadrian			/*
792218065Sadrian			 * Be careful writing the address to TXDP.  If
793218065Sadrian			 * the tx q is enabled then this write will be
794218065Sadrian			 * ignored.  Normally this is not an issue but
795218065Sadrian			 * when tdma is in use and the q is beacon gated
796218065Sadrian			 * this race can occur.  If the q is busy then
797218065Sadrian			 * defer the work to later--either when another
798218065Sadrian			 * packet comes along or when we prepare a beacon
799218065Sadrian			 * frame at SWBA.
800218065Sadrian			 */
801218065Sadrian			if (!qbusy) {
802229949Sadrian				ath_hal_puttxbuf(ah, txq->axq_qnum,
803229949Sadrian				    bf->bf_daddr);
804218065Sadrian				txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
805218065Sadrian				DPRINTF(sc, ATH_DEBUG_XMIT,
806240899Sadrian				    "%s: TXDP[%u] = %p (%p) lastds=%p depth %d\n",
807218065Sadrian				    __func__, txq->axq_qnum,
808218065Sadrian				    (caddr_t)bf->bf_daddr, bf->bf_desc,
809240899Sadrian				    bf->bf_lastds,
810218065Sadrian				    txq->axq_depth);
811240899Sadrian				ATH_KTR(sc, ATH_KTR_TX, 5,
812240899Sadrian				    "ath_tx_handoff: TXDP[%u] = %p (%p) "
813240899Sadrian				    "lastds=%p depth %d",
814240899Sadrian				    txq->axq_qnum,
815240899Sadrian				    (caddr_t)bf->bf_daddr, bf->bf_desc,
816240899Sadrian				    bf->bf_lastds,
817240899Sadrian				    txq->axq_depth);
818218065Sadrian			} else {
819218065Sadrian				txq->axq_flags |= ATH_TXQ_PUTPENDING;
820218065Sadrian				DPRINTF(sc, ATH_DEBUG_TDMA | ATH_DEBUG_XMIT,
821218065Sadrian				    "%s: Q%u busy, defer enable\n", __func__,
822218065Sadrian				    txq->axq_qnum);
823240899Sadrian				ATH_KTR(sc, ATH_KTR_TX, 0, "defer enable");
824218065Sadrian			}
825218065Sadrian		} else {
826218065Sadrian			*txq->axq_link = bf->bf_daddr;
827218065Sadrian			DPRINTF(sc, ATH_DEBUG_XMIT,
828218065Sadrian			    "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
829218065Sadrian			    txq->axq_qnum, txq->axq_link,
830229949Sadrian			    (caddr_t)bf->bf_daddr, bf->bf_desc,
831229949Sadrian			    txq->axq_depth);
832240899Sadrian			ATH_KTR(sc, ATH_KTR_TX, 5,
833240899Sadrian			    "ath_tx_handoff: link[%u](%p)=%p (%p) lastds=%p",
834240899Sadrian			    txq->axq_qnum, txq->axq_link,
835240899Sadrian			    (caddr_t)bf->bf_daddr, bf->bf_desc,
836240899Sadrian			    bf->bf_lastds);
837240899Sadrian
838218065Sadrian			if ((txq->axq_flags & ATH_TXQ_PUTPENDING) && !qbusy) {
839218065Sadrian				/*
840218065Sadrian				 * The q was busy when we previously tried
841218065Sadrian				 * to write the address of the first buffer
842218065Sadrian				 * in the chain.  Since it's not busy now
843218065Sadrian				 * handle this chore.  We are certain the
844218065Sadrian				 * buffer at the front is the right one since
845218065Sadrian				 * axq_link is NULL only when the buffer list
846218065Sadrian				 * is/was empty.
847218065Sadrian				 */
848218065Sadrian				ath_hal_puttxbuf(ah, txq->axq_qnum,
849227344Sadrian					TAILQ_FIRST(&txq->axq_q)->bf_daddr);
850218065Sadrian				txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
851218065Sadrian				DPRINTF(sc, ATH_DEBUG_TDMA | ATH_DEBUG_XMIT,
852218065Sadrian				    "%s: Q%u restarted\n", __func__,
853218065Sadrian				    txq->axq_qnum);
854240899Sadrian				ATH_KTR(sc, ATH_KTR_TX, 4,
855240899Sadrian				  "ath_tx_handoff: txq[%d] restarted, bf=%p "
856240899Sadrian				  "daddr=%p ds=%p",
857240899Sadrian				    txq->axq_qnum,
858240899Sadrian				    bf,
859240899Sadrian				    (caddr_t)bf->bf_daddr,
860240899Sadrian				    bf->bf_desc);
861218065Sadrian			}
862218065Sadrian		}
863218065Sadrian#else
864218065Sadrian		ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
865241500Sadrian		ATH_KTR(sc, ATH_KTR_TX, 3,
866241500Sadrian		    "ath_tx_handoff: non-tdma: txq=%u, add bf=%p "
867240899Sadrian		    "depth=%d",
868240899Sadrian		    txq->axq_qnum,
869240899Sadrian		    bf,
870240899Sadrian		    txq->axq_depth);
871218065Sadrian		if (txq->axq_link == NULL) {
872218065Sadrian			ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
873218065Sadrian			DPRINTF(sc, ATH_DEBUG_XMIT,
874218065Sadrian			    "%s: TXDP[%u] = %p (%p) depth %d\n",
875218065Sadrian			    __func__, txq->axq_qnum,
876218065Sadrian			    (caddr_t)bf->bf_daddr, bf->bf_desc,
877218065Sadrian			    txq->axq_depth);
878240899Sadrian			ATH_KTR(sc, ATH_KTR_TX, 5,
879240899Sadrian			    "ath_tx_handoff: non-tdma: TXDP[%u] = %p (%p) "
880240899Sadrian			    "lastds=%p depth %d",
881240899Sadrian			    txq->axq_qnum,
882240899Sadrian			    (caddr_t)bf->bf_daddr, bf->bf_desc,
883240899Sadrian			    bf->bf_lastds,
884240899Sadrian			    txq->axq_depth);
885240899Sadrian
886218065Sadrian		} else {
887218065Sadrian			*txq->axq_link = bf->bf_daddr;
888218065Sadrian			DPRINTF(sc, ATH_DEBUG_XMIT,
889218065Sadrian			    "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
890218065Sadrian			    txq->axq_qnum, txq->axq_link,
891229949Sadrian			    (caddr_t)bf->bf_daddr, bf->bf_desc,
892229949Sadrian			    txq->axq_depth);
893240899Sadrian			ATH_KTR(sc, ATH_KTR_TX, 5,
894240899Sadrian			    "ath_tx_handoff: non-tdma: link[%u](%p)=%p (%p) "
895240899Sadrian			    "lastds=%d",
896240899Sadrian			    txq->axq_qnum, txq->axq_link,
897240899Sadrian			    (caddr_t)bf->bf_daddr, bf->bf_desc,
898240899Sadrian			    bf->bf_lastds);
899240899Sadrian
900218065Sadrian		}
901218065Sadrian#endif /* IEEE80211_SUPPORT_TDMA */
902227360Sadrian		if (bf->bf_state.bfs_aggr)
903227360Sadrian			txq->axq_aggr_depth++;
904238609Sadrian		ath_hal_gettxdesclinkptr(ah, bf->bf_lastds, &txq->axq_link);
905218065Sadrian		ath_hal_txstart(ah, txq->axq_qnum);
906248671Sadrian		ATH_TXQ_UNLOCK(txq);
907240899Sadrian		ATH_KTR(sc, ATH_KTR_TX, 1,
908240899Sadrian		    "ath_tx_handoff: txq=%u, txstart", txq->axq_qnum);
909227364Sadrian	}
910227364Sadrian}
911218065Sadrian
912227364Sadrian/*
913227364Sadrian * Restart TX DMA for the given TXQ.
914227364Sadrian *
915227364Sadrian * This must be called whether the queue is empty or not.
916227364Sadrian */
917238930Sadrianstatic void
918238930Sadrianath_legacy_tx_dma_restart(struct ath_softc *sc, struct ath_txq *txq)
919227364Sadrian{
920227364Sadrian	struct ath_hal *ah = sc->sc_ah;
921232707Sadrian	struct ath_buf *bf, *bf_last;
922218065Sadrian
923248671Sadrian	ATH_TXQ_LOCK_ASSERT(txq);
924227364Sadrian	/* This is always going to be cleared, empty or not */
925227364Sadrian	txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
926227364Sadrian
927232707Sadrian	/* XXX make this ATH_TXQ_FIRST */
928227364Sadrian	bf = TAILQ_FIRST(&txq->axq_q);
929232707Sadrian	bf_last = ATH_TXQ_LAST(txq, axq_q_s);
930232707Sadrian
931227364Sadrian	if (bf == NULL)
932227364Sadrian		return;
933227364Sadrian
934227364Sadrian	ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
935239120Sadrian	ath_hal_gettxdesclinkptr(ah, bf_last->bf_lastds, &txq->axq_link);
936227364Sadrian	ath_hal_txstart(ah, txq->axq_qnum);
937218065Sadrian}
938218065Sadrian
939227364Sadrian/*
940227364Sadrian * Hand off a packet to the hardware (or mcast queue.)
941227364Sadrian *
942227364Sadrian * The relevant hardware txq should be locked.
943227364Sadrian */
944227364Sadrianstatic void
945238930Sadrianath_legacy_xmit_handoff(struct ath_softc *sc, struct ath_txq *txq,
946238930Sadrian    struct ath_buf *bf)
947227364Sadrian{
948243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
949227364Sadrian
950243162Sadrian#ifdef	ATH_DEBUG_ALQ
951243162Sadrian	if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_EDMA_TXDESC))
952243162Sadrian		ath_tx_alq_post(sc, bf);
953243162Sadrian#endif
954243162Sadrian
955227364Sadrian	if (txq->axq_qnum == ATH_TXQ_SWQ)
956227364Sadrian		ath_tx_handoff_mcast(sc, txq, bf);
957227364Sadrian	else
958227364Sadrian		ath_tx_handoff_hw(sc, txq, bf);
959227364Sadrian}
960227364Sadrian
961218154Sadrianstatic int
962218154Sadrianath_tx_tag_crypto(struct ath_softc *sc, struct ieee80211_node *ni,
963229949Sadrian    struct mbuf *m0, int iswep, int isfrag, int *hdrlen, int *pktlen,
964229949Sadrian    int *keyix)
965218154Sadrian{
966233330Sadrian	DPRINTF(sc, ATH_DEBUG_XMIT,
967233330Sadrian	    "%s: hdrlen=%d, pktlen=%d, isfrag=%d, iswep=%d, m0=%p\n",
968233330Sadrian	    __func__,
969233330Sadrian	    *hdrlen,
970233330Sadrian	    *pktlen,
971233330Sadrian	    isfrag,
972233330Sadrian	    iswep,
973233330Sadrian	    m0);
974233330Sadrian
975218154Sadrian	if (iswep) {
976218154Sadrian		const struct ieee80211_cipher *cip;
977218154Sadrian		struct ieee80211_key *k;
978218154Sadrian
979218154Sadrian		/*
980218154Sadrian		 * Construct the 802.11 header+trailer for an encrypted
981218154Sadrian		 * frame. The only reason this can fail is because of an
982218154Sadrian		 * unknown or unsupported cipher/key type.
983218154Sadrian		 */
984218154Sadrian		k = ieee80211_crypto_encap(ni, m0);
985218154Sadrian		if (k == NULL) {
986218154Sadrian			/*
987218154Sadrian			 * This can happen when the key is yanked after the
988218154Sadrian			 * frame was queued.  Just discard the frame; the
989218154Sadrian			 * 802.11 layer counts failures and provides
990218154Sadrian			 * debugging/diagnostics.
991218154Sadrian			 */
992229949Sadrian			return (0);
993218154Sadrian		}
994218154Sadrian		/*
995218154Sadrian		 * Adjust the packet + header lengths for the crypto
996218154Sadrian		 * additions and calculate the h/w key index.  When
997218154Sadrian		 * a s/w mic is done the frame will have had any mic
998218154Sadrian		 * added to it prior to entry so m0->m_pkthdr.len will
999218154Sadrian		 * account for it. Otherwise we need to add it to the
1000218154Sadrian		 * packet length.
1001218154Sadrian		 */
1002218154Sadrian		cip = k->wk_cipher;
1003218154Sadrian		(*hdrlen) += cip->ic_header;
1004218154Sadrian		(*pktlen) += cip->ic_header + cip->ic_trailer;
1005218154Sadrian		/* NB: frags always have any TKIP MIC done in s/w */
1006218154Sadrian		if ((k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && !isfrag)
1007218154Sadrian			(*pktlen) += cip->ic_miclen;
1008218154Sadrian		(*keyix) = k->wk_keyix;
1009218154Sadrian	} else if (ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) {
1010218154Sadrian		/*
1011218154Sadrian		 * Use station key cache slot, if assigned.
1012218154Sadrian		 */
1013218154Sadrian		(*keyix) = ni->ni_ucastkey.wk_keyix;
1014218154Sadrian		if ((*keyix) == IEEE80211_KEYIX_NONE)
1015218154Sadrian			(*keyix) = HAL_TXKEYIX_INVALID;
1016218154Sadrian	} else
1017218154Sadrian		(*keyix) = HAL_TXKEYIX_INVALID;
1018218154Sadrian
1019229949Sadrian	return (1);
1020218154Sadrian}
1021218154Sadrian
1022233989Sadrian/*
1023233989Sadrian * Calculate whether interoperability protection is required for
1024233989Sadrian * this frame.
1025233989Sadrian *
1026233989Sadrian * This requires the rate control information be filled in,
1027233989Sadrian * as the protection requirement depends upon the current
1028233989Sadrian * operating mode / PHY.
1029233989Sadrian */
1030233989Sadrianstatic void
1031233989Sadrianath_tx_calc_protection(struct ath_softc *sc, struct ath_buf *bf)
1032233989Sadrian{
1033233989Sadrian	struct ieee80211_frame *wh;
1034233989Sadrian	uint8_t rix;
1035233989Sadrian	uint16_t flags;
1036233989Sadrian	int shortPreamble;
1037233989Sadrian	const HAL_RATE_TABLE *rt = sc->sc_currates;
1038233989Sadrian	struct ifnet *ifp = sc->sc_ifp;
1039233989Sadrian	struct ieee80211com *ic = ifp->if_l2com;
1040233989Sadrian
1041233989Sadrian	flags = bf->bf_state.bfs_txflags;
1042233989Sadrian	rix = bf->bf_state.bfs_rc[0].rix;
1043233989Sadrian	shortPreamble = bf->bf_state.bfs_shpream;
1044233989Sadrian	wh = mtod(bf->bf_m, struct ieee80211_frame *);
1045233989Sadrian
1046233989Sadrian	/*
1047233989Sadrian	 * If 802.11g protection is enabled, determine whether
1048233989Sadrian	 * to use RTS/CTS or just CTS.  Note that this is only
1049233989Sadrian	 * done for OFDM unicast frames.
1050233989Sadrian	 */
1051233989Sadrian	if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1052233989Sadrian	    rt->info[rix].phy == IEEE80211_T_OFDM &&
1053233989Sadrian	    (flags & HAL_TXDESC_NOACK) == 0) {
1054233989Sadrian		bf->bf_state.bfs_doprot = 1;
1055233989Sadrian		/* XXX fragments must use CCK rates w/ protection */
1056233989Sadrian		if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) {
1057233989Sadrian			flags |= HAL_TXDESC_RTSENA;
1058233989Sadrian		} else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) {
1059233989Sadrian			flags |= HAL_TXDESC_CTSENA;
1060233989Sadrian		}
1061233989Sadrian		/*
1062233989Sadrian		 * For frags it would be desirable to use the
1063233989Sadrian		 * highest CCK rate for RTS/CTS.  But stations
1064233989Sadrian		 * farther away may detect it at a lower CCK rate
1065233989Sadrian		 * so use the configured protection rate instead
1066233989Sadrian		 * (for now).
1067233989Sadrian		 */
1068233989Sadrian		sc->sc_stats.ast_tx_protect++;
1069233989Sadrian	}
1070233989Sadrian
1071233989Sadrian	/*
1072233989Sadrian	 * If 11n protection is enabled and it's a HT frame,
1073233989Sadrian	 * enable RTS.
1074233989Sadrian	 *
1075233989Sadrian	 * XXX ic_htprotmode or ic_curhtprotmode?
1076233989Sadrian	 * XXX should it_htprotmode only matter if ic_curhtprotmode
1077233989Sadrian	 * XXX indicates it's not a HT pure environment?
1078233989Sadrian	 */
1079233989Sadrian	if ((ic->ic_htprotmode == IEEE80211_PROT_RTSCTS) &&
1080233989Sadrian	    rt->info[rix].phy == IEEE80211_T_HT &&
1081233989Sadrian	    (flags & HAL_TXDESC_NOACK) == 0) {
1082233989Sadrian		flags |= HAL_TXDESC_RTSENA;
1083233989Sadrian		sc->sc_stats.ast_tx_htprotect++;
1084233989Sadrian	}
1085233989Sadrian	bf->bf_state.bfs_txflags = flags;
1086233989Sadrian}
1087233989Sadrian
1088233989Sadrian/*
1089233989Sadrian * Update the frame duration given the currently selected rate.
1090233989Sadrian *
1091233989Sadrian * This also updates the frame duration value, so it will require
1092233989Sadrian * a DMA flush.
1093233989Sadrian */
1094233989Sadrianstatic void
1095233989Sadrianath_tx_calc_duration(struct ath_softc *sc, struct ath_buf *bf)
1096233989Sadrian{
1097233989Sadrian	struct ieee80211_frame *wh;
1098233989Sadrian	uint8_t rix;
1099233989Sadrian	uint16_t flags;
1100233989Sadrian	int shortPreamble;
1101233989Sadrian	struct ath_hal *ah = sc->sc_ah;
1102233989Sadrian	const HAL_RATE_TABLE *rt = sc->sc_currates;
1103233989Sadrian	int isfrag = bf->bf_m->m_flags & M_FRAG;
1104233989Sadrian
1105233989Sadrian	flags = bf->bf_state.bfs_txflags;
1106233989Sadrian	rix = bf->bf_state.bfs_rc[0].rix;
1107233989Sadrian	shortPreamble = bf->bf_state.bfs_shpream;
1108233989Sadrian	wh = mtod(bf->bf_m, struct ieee80211_frame *);
1109233989Sadrian
1110233989Sadrian	/*
1111233989Sadrian	 * Calculate duration.  This logically belongs in the 802.11
1112233989Sadrian	 * layer but it lacks sufficient information to calculate it.
1113233989Sadrian	 */
1114233989Sadrian	if ((flags & HAL_TXDESC_NOACK) == 0 &&
1115233989Sadrian	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL) {
1116233989Sadrian		u_int16_t dur;
1117233989Sadrian		if (shortPreamble)
1118233989Sadrian			dur = rt->info[rix].spAckDuration;
1119233989Sadrian		else
1120233989Sadrian			dur = rt->info[rix].lpAckDuration;
1121233989Sadrian		if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) {
1122233989Sadrian			dur += dur;		/* additional SIFS+ACK */
1123246745Sadrian			KASSERT(bf->bf_m->m_nextpkt != NULL, ("no fragment"));
1124233989Sadrian			/*
1125233989Sadrian			 * Include the size of next fragment so NAV is
1126233989Sadrian			 * updated properly.  The last fragment uses only
1127233989Sadrian			 * the ACK duration
1128242144Sadrian			 *
1129242144Sadrian			 * XXX TODO: ensure that the rate lookup for each
1130242144Sadrian			 * fragment is the same as the rate used by the
1131242144Sadrian			 * first fragment!
1132233989Sadrian			 */
1133233989Sadrian			dur += ath_hal_computetxtime(ah, rt,
1134246745Sadrian					bf->bf_m->m_nextpkt->m_pkthdr.len,
1135233989Sadrian					rix, shortPreamble);
1136233989Sadrian		}
1137233989Sadrian		if (isfrag) {
1138233989Sadrian			/*
1139233989Sadrian			 * Force hardware to use computed duration for next
1140233989Sadrian			 * fragment by disabling multi-rate retry which updates
1141233989Sadrian			 * duration based on the multi-rate duration table.
1142233989Sadrian			 */
1143233989Sadrian			bf->bf_state.bfs_ismrr = 0;
1144233989Sadrian			bf->bf_state.bfs_try0 = ATH_TXMGTTRY;
1145233989Sadrian			/* XXX update bfs_rc[0].try? */
1146233989Sadrian		}
1147233989Sadrian
1148233989Sadrian		/* Update the duration field itself */
1149233989Sadrian		*(u_int16_t *)wh->i_dur = htole16(dur);
1150233989Sadrian	}
1151233989Sadrian}
1152233989Sadrian
1153218932Sadrianstatic uint8_t
1154218932Sadrianath_tx_get_rtscts_rate(struct ath_hal *ah, const HAL_RATE_TABLE *rt,
1155227364Sadrian    int cix, int shortPreamble)
1156218157Sadrian{
1157218932Sadrian	uint8_t ctsrate;
1158218932Sadrian
1159218157Sadrian	/*
1160218157Sadrian	 * CTS transmit rate is derived from the transmit rate
1161218157Sadrian	 * by looking in the h/w rate table.  We must also factor
1162218157Sadrian	 * in whether or not a short preamble is to be used.
1163218157Sadrian	 */
1164218157Sadrian	/* NB: cix is set above where RTS/CTS is enabled */
1165218157Sadrian	KASSERT(cix != 0xff, ("cix not setup"));
1166218932Sadrian	ctsrate = rt->info[cix].rateCode;
1167218932Sadrian
1168218932Sadrian	/* XXX this should only matter for legacy rates */
1169218932Sadrian	if (shortPreamble)
1170218932Sadrian		ctsrate |= rt->info[cix].shortPreamble;
1171218932Sadrian
1172229949Sadrian	return (ctsrate);
1173218932Sadrian}
1174218932Sadrian
1175218932Sadrian/*
1176218932Sadrian * Calculate the RTS/CTS duration for legacy frames.
1177218932Sadrian */
1178218932Sadrianstatic int
1179218932Sadrianath_tx_calc_ctsduration(struct ath_hal *ah, int rix, int cix,
1180218932Sadrian    int shortPreamble, int pktlen, const HAL_RATE_TABLE *rt,
1181218932Sadrian    int flags)
1182218932Sadrian{
1183218932Sadrian	int ctsduration = 0;
1184218932Sadrian
1185218932Sadrian	/* This mustn't be called for HT modes */
1186218932Sadrian	if (rt->info[cix].phy == IEEE80211_T_HT) {
1187218932Sadrian		printf("%s: HT rate where it shouldn't be (0x%x)\n",
1188218932Sadrian		    __func__, rt->info[cix].rateCode);
1189229949Sadrian		return (-1);
1190218932Sadrian	}
1191218932Sadrian
1192218157Sadrian	/*
1193218157Sadrian	 * Compute the transmit duration based on the frame
1194218157Sadrian	 * size and the size of an ACK frame.  We call into the
1195218157Sadrian	 * HAL to do the computation since it depends on the
1196218157Sadrian	 * characteristics of the actual PHY being used.
1197218157Sadrian	 *
1198218157Sadrian	 * NB: CTS is assumed the same size as an ACK so we can
1199218157Sadrian	 *     use the precalculated ACK durations.
1200218157Sadrian	 */
1201218157Sadrian	if (shortPreamble) {
1202218157Sadrian		if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
1203218932Sadrian			ctsduration += rt->info[cix].spAckDuration;
1204218932Sadrian		ctsduration += ath_hal_computetxtime(ah,
1205218157Sadrian			rt, pktlen, rix, AH_TRUE);
1206218157Sadrian		if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
1207218932Sadrian			ctsduration += rt->info[rix].spAckDuration;
1208218157Sadrian	} else {
1209218157Sadrian		if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
1210218932Sadrian			ctsduration += rt->info[cix].lpAckDuration;
1211218932Sadrian		ctsduration += ath_hal_computetxtime(ah,
1212218157Sadrian			rt, pktlen, rix, AH_FALSE);
1213218157Sadrian		if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
1214218932Sadrian			ctsduration += rt->info[rix].lpAckDuration;
1215218157Sadrian	}
1216218932Sadrian
1217229949Sadrian	return (ctsduration);
1218218157Sadrian}
1219218157Sadrian
1220227364Sadrian/*
1221227364Sadrian * Update the given ath_buf with updated rts/cts setup and duration
1222227364Sadrian * values.
1223227364Sadrian *
1224227364Sadrian * To support rate lookups for each software retry, the rts/cts rate
1225227364Sadrian * and cts duration must be re-calculated.
1226227364Sadrian *
1227227364Sadrian * This function assumes the RTS/CTS flags have been set as needed;
1228227364Sadrian * mrr has been disabled; and the rate control lookup has been done.
1229227364Sadrian *
1230227364Sadrian * XXX TODO: MRR need only be disabled for the pre-11n NICs.
1231227364Sadrian * XXX The 11n NICs support per-rate RTS/CTS configuration.
1232227364Sadrian */
1233227364Sadrianstatic void
1234227364Sadrianath_tx_set_rtscts(struct ath_softc *sc, struct ath_buf *bf)
1235218065Sadrian{
1236227364Sadrian	uint16_t ctsduration = 0;
1237227364Sadrian	uint8_t ctsrate = 0;
1238227364Sadrian	uint8_t rix = bf->bf_state.bfs_rc[0].rix;
1239227364Sadrian	uint8_t cix = 0;
1240227364Sadrian	const HAL_RATE_TABLE *rt = sc->sc_currates;
1241227364Sadrian
1242227364Sadrian	/*
1243227364Sadrian	 * No RTS/CTS enabled? Don't bother.
1244227364Sadrian	 */
1245233966Sadrian	if ((bf->bf_state.bfs_txflags &
1246227364Sadrian	    (HAL_TXDESC_RTSENA | HAL_TXDESC_CTSENA)) == 0) {
1247227364Sadrian		/* XXX is this really needed? */
1248227364Sadrian		bf->bf_state.bfs_ctsrate = 0;
1249227364Sadrian		bf->bf_state.bfs_ctsduration = 0;
1250227364Sadrian		return;
1251227364Sadrian	}
1252227364Sadrian
1253227364Sadrian	/*
1254227364Sadrian	 * If protection is enabled, use the protection rix control
1255227364Sadrian	 * rate. Otherwise use the rate0 control rate.
1256227364Sadrian	 */
1257227364Sadrian	if (bf->bf_state.bfs_doprot)
1258227364Sadrian		rix = sc->sc_protrix;
1259227364Sadrian	else
1260227364Sadrian		rix = bf->bf_state.bfs_rc[0].rix;
1261227364Sadrian
1262227364Sadrian	/*
1263227364Sadrian	 * If the raw path has hard-coded ctsrate0 to something,
1264227364Sadrian	 * use it.
1265227364Sadrian	 */
1266227364Sadrian	if (bf->bf_state.bfs_ctsrate0 != 0)
1267227364Sadrian		cix = ath_tx_findrix(sc, bf->bf_state.bfs_ctsrate0);
1268227364Sadrian	else
1269227364Sadrian		/* Control rate from above */
1270227364Sadrian		cix = rt->info[rix].controlRate;
1271227364Sadrian
1272227364Sadrian	/* Calculate the rtscts rate for the given cix */
1273227364Sadrian	ctsrate = ath_tx_get_rtscts_rate(sc->sc_ah, rt, cix,
1274227364Sadrian	    bf->bf_state.bfs_shpream);
1275227364Sadrian
1276227364Sadrian	/* The 11n chipsets do ctsduration calculations for you */
1277227364Sadrian	if (! ath_tx_is_11n(sc))
1278227364Sadrian		ctsduration = ath_tx_calc_ctsduration(sc->sc_ah, rix, cix,
1279227364Sadrian		    bf->bf_state.bfs_shpream, bf->bf_state.bfs_pktlen,
1280233966Sadrian		    rt, bf->bf_state.bfs_txflags);
1281227364Sadrian
1282227364Sadrian	/* Squirrel away in ath_buf */
1283227364Sadrian	bf->bf_state.bfs_ctsrate = ctsrate;
1284227364Sadrian	bf->bf_state.bfs_ctsduration = ctsduration;
1285227364Sadrian
1286227364Sadrian	/*
1287227364Sadrian	 * Must disable multi-rate retry when using RTS/CTS.
1288227364Sadrian	 */
1289238961Sadrian	if (!sc->sc_mrrprot) {
1290238961Sadrian		bf->bf_state.bfs_ismrr = 0;
1291238961Sadrian		bf->bf_state.bfs_try0 =
1292238961Sadrian		    bf->bf_state.bfs_rc[0].tries = ATH_TXMGTTRY; /* XXX ew */
1293238961Sadrian	}
1294227364Sadrian}
1295227364Sadrian
1296227364Sadrian/*
1297227364Sadrian * Setup the descriptor chain for a normal or fast-frame
1298227364Sadrian * frame.
1299239051Sadrian *
1300239051Sadrian * XXX TODO: extend to include the destination hardware QCU ID.
1301239051Sadrian * Make sure that is correct.  Make sure that when being added
1302239051Sadrian * to the mcastq, the CABQ QCUID is set or things will get a bit
1303239051Sadrian * odd.
1304227364Sadrian */
1305227364Sadrianstatic void
1306227364Sadrianath_tx_setds(struct ath_softc *sc, struct ath_buf *bf)
1307227364Sadrian{
1308227364Sadrian	struct ath_desc *ds = bf->bf_desc;
1309227364Sadrian	struct ath_hal *ah = sc->sc_ah;
1310227364Sadrian
1311242951Sadrian	if (bf->bf_state.bfs_txrate0 == 0)
1312242951Sadrian		device_printf(sc->sc_dev, "%s: bf=%p, txrate0=%d\n",
1313242951Sadrian		    __func__, bf, 0);
1314242951Sadrian
1315227364Sadrian	ath_hal_setuptxdesc(ah, ds
1316227364Sadrian		, bf->bf_state.bfs_pktlen	/* packet length */
1317227364Sadrian		, bf->bf_state.bfs_hdrlen	/* header length */
1318227364Sadrian		, bf->bf_state.bfs_atype	/* Atheros packet type */
1319227364Sadrian		, bf->bf_state.bfs_txpower	/* txpower */
1320227364Sadrian		, bf->bf_state.bfs_txrate0
1321227364Sadrian		, bf->bf_state.bfs_try0		/* series 0 rate/tries */
1322227364Sadrian		, bf->bf_state.bfs_keyix	/* key cache index */
1323227364Sadrian		, bf->bf_state.bfs_txantenna	/* antenna mode */
1324233966Sadrian		, bf->bf_state.bfs_txflags	/* flags */
1325227364Sadrian		, bf->bf_state.bfs_ctsrate	/* rts/cts rate */
1326227364Sadrian		, bf->bf_state.bfs_ctsduration	/* rts/cts duration */
1327227364Sadrian	);
1328227364Sadrian
1329227364Sadrian	/*
1330227364Sadrian	 * This will be overriden when the descriptor chain is written.
1331227364Sadrian	 */
1332227364Sadrian	bf->bf_lastds = ds;
1333227364Sadrian	bf->bf_last = bf;
1334227364Sadrian
1335238947Sadrian	/* Set rate control and descriptor chain for this frame */
1336238947Sadrian	ath_tx_set_ratectrl(sc, bf->bf_node, bf);
1337242656Sadrian	ath_tx_chaindesclist(sc, ds, bf, 0, 0, 0);
1338227364Sadrian}
1339227364Sadrian
1340227364Sadrian/*
1341227364Sadrian * Do a rate lookup.
1342227364Sadrian *
1343227364Sadrian * This performs a rate lookup for the given ath_buf only if it's required.
1344227364Sadrian * Non-data frames and raw frames don't require it.
1345227364Sadrian *
1346227364Sadrian * This populates the primary and MRR entries; MRR values are
1347227364Sadrian * then disabled later on if something requires it (eg RTS/CTS on
1348227364Sadrian * pre-11n chipsets.
1349227364Sadrian *
1350227364Sadrian * This needs to be done before the RTS/CTS fields are calculated
1351227364Sadrian * as they may depend upon the rate chosen.
1352227364Sadrian */
1353227364Sadrianstatic void
1354227364Sadrianath_tx_do_ratelookup(struct ath_softc *sc, struct ath_buf *bf)
1355227364Sadrian{
1356227364Sadrian	uint8_t rate, rix;
1357227364Sadrian	int try0;
1358227364Sadrian
1359227364Sadrian	if (! bf->bf_state.bfs_doratelookup)
1360227364Sadrian		return;
1361227364Sadrian
1362227364Sadrian	/* Get rid of any previous state */
1363227364Sadrian	bzero(bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
1364227364Sadrian
1365227364Sadrian	ATH_NODE_LOCK(ATH_NODE(bf->bf_node));
1366227364Sadrian	ath_rate_findrate(sc, ATH_NODE(bf->bf_node), bf->bf_state.bfs_shpream,
1367227364Sadrian	    bf->bf_state.bfs_pktlen, &rix, &try0, &rate);
1368227364Sadrian
1369227364Sadrian	/* In case MRR is disabled, make sure rc[0] is setup correctly */
1370227364Sadrian	bf->bf_state.bfs_rc[0].rix = rix;
1371227364Sadrian	bf->bf_state.bfs_rc[0].ratecode = rate;
1372227364Sadrian	bf->bf_state.bfs_rc[0].tries = try0;
1373227364Sadrian
1374227364Sadrian	if (bf->bf_state.bfs_ismrr && try0 != ATH_TXMAXTRY)
1375227364Sadrian		ath_rate_getxtxrates(sc, ATH_NODE(bf->bf_node), rix,
1376227364Sadrian		    bf->bf_state.bfs_rc);
1377227364Sadrian	ATH_NODE_UNLOCK(ATH_NODE(bf->bf_node));
1378227364Sadrian
1379227364Sadrian	sc->sc_txrix = rix;	/* for LED blinking */
1380227364Sadrian	sc->sc_lastdatarix = rix;	/* for fast frames */
1381227364Sadrian	bf->bf_state.bfs_try0 = try0;
1382227364Sadrian	bf->bf_state.bfs_txrate0 = rate;
1383227364Sadrian}
1384227364Sadrian
1385227364Sadrian/*
1386240883Sadrian * Update the CLRDMASK bit in the ath_buf if it needs to be set.
1387240883Sadrian */
1388240883Sadrianstatic void
1389240883Sadrianath_tx_update_clrdmask(struct ath_softc *sc, struct ath_tid *tid,
1390240883Sadrian    struct ath_buf *bf)
1391240883Sadrian{
1392245708Sadrian	struct ath_node *an = ATH_NODE(bf->bf_node);
1393240883Sadrian
1394243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
1395240883Sadrian
1396245708Sadrian	if (an->clrdmask == 1) {
1397240883Sadrian		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
1398245708Sadrian		an->clrdmask = 0;
1399240883Sadrian	}
1400240883Sadrian}
1401240883Sadrian
1402240883Sadrian/*
1403227364Sadrian * Transmit the given frame to the hardware.
1404227364Sadrian *
1405227364Sadrian * The frame must already be setup; rate control must already have
1406227364Sadrian * been done.
1407227364Sadrian *
1408227364Sadrian * XXX since the TXQ lock is being held here (and I dislike holding
1409227364Sadrian * it for this long when not doing software aggregation), later on
1410227364Sadrian * break this function into "setup_normal" and "xmit_normal". The
1411227364Sadrian * lock only needs to be held for the ath_tx_handoff call.
1412227364Sadrian */
1413227364Sadrianstatic void
1414227364Sadrianath_tx_xmit_normal(struct ath_softc *sc, struct ath_txq *txq,
1415227364Sadrian    struct ath_buf *bf)
1416227364Sadrian{
1417240883Sadrian	struct ath_node *an = ATH_NODE(bf->bf_node);
1418240883Sadrian	struct ath_tid *tid = &an->an_tid[bf->bf_state.bfs_tid];
1419227364Sadrian
1420243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
1421227364Sadrian
1422240883Sadrian	/*
1423240883Sadrian	 * For now, just enable CLRDMASK. ath_tx_xmit_normal() does
1424240883Sadrian	 * set a completion handler however it doesn't (yet) properly
1425240883Sadrian	 * handle the strict ordering requirements needed for normal,
1426240883Sadrian	 * non-aggregate session frames.
1427240883Sadrian	 *
1428240883Sadrian	 * Once this is implemented, only set CLRDMASK like this for
1429240883Sadrian	 * frames that must go out - eg management/raw frames.
1430240883Sadrian	 */
1431240883Sadrian	bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
1432240883Sadrian
1433227364Sadrian	/* Setup the descriptor before handoff */
1434227364Sadrian	ath_tx_do_ratelookup(sc, bf);
1435233989Sadrian	ath_tx_calc_duration(sc, bf);
1436233989Sadrian	ath_tx_calc_protection(sc, bf);
1437233989Sadrian	ath_tx_set_rtscts(sc, bf);
1438227364Sadrian	ath_tx_rate_fill_rcflags(sc, bf);
1439227364Sadrian	ath_tx_setds(sc, bf);
1440227364Sadrian
1441240883Sadrian	/* Track per-TID hardware queue depth correctly */
1442240883Sadrian	tid->hwq_depth++;
1443240724Sadrian
1444240883Sadrian	/* Assign the completion handler */
1445240883Sadrian	bf->bf_comp = ath_tx_normal_comp;
1446240883Sadrian
1447227364Sadrian	/* Hand off to hardware */
1448227364Sadrian	ath_tx_handoff(sc, txq, bf);
1449227364Sadrian}
1450227364Sadrian
1451240722Sadrian/*
1452240722Sadrian * Do the basic frame setup stuff that's required before the frame
1453240722Sadrian * is added to a software queue.
1454240722Sadrian *
1455240722Sadrian * All frames get mostly the same treatment and it's done once.
1456240722Sadrian * Retransmits fiddle with things like the rate control setup,
1457240722Sadrian * setting the retransmit bit in the packet; doing relevant DMA/bus
1458240722Sadrian * syncing and relinking it (back) into the hardware TX queue.
1459240722Sadrian *
1460240722Sadrian * Note that this may cause the mbuf to be reallocated, so
1461240722Sadrian * m0 may not be valid.
1462240722Sadrian */
1463227364Sadrianstatic int
1464227364Sadrianath_tx_normal_setup(struct ath_softc *sc, struct ieee80211_node *ni,
1465234009Sadrian    struct ath_buf *bf, struct mbuf *m0, struct ath_txq *txq)
1466227364Sadrian{
1467218065Sadrian	struct ieee80211vap *vap = ni->ni_vap;
1468218065Sadrian	struct ath_hal *ah = sc->sc_ah;
1469218065Sadrian	struct ifnet *ifp = sc->sc_ifp;
1470218065Sadrian	struct ieee80211com *ic = ifp->if_l2com;
1471218065Sadrian	const struct chanAccParams *cap = &ic->ic_wme.wme_chanParams;
1472218065Sadrian	int error, iswep, ismcast, isfrag, ismrr;
1473227364Sadrian	int keyix, hdrlen, pktlen, try0 = 0;
1474227364Sadrian	u_int8_t rix = 0, txrate = 0;
1475218065Sadrian	struct ath_desc *ds;
1476218065Sadrian	struct ieee80211_frame *wh;
1477227364Sadrian	u_int subtype, flags;
1478218065Sadrian	HAL_PKT_TYPE atype;
1479218065Sadrian	const HAL_RATE_TABLE *rt;
1480218065Sadrian	HAL_BOOL shortPreamble;
1481218065Sadrian	struct ath_node *an;
1482218065Sadrian	u_int pri;
1483218065Sadrian
1484236880Sadrian	/*
1485236880Sadrian	 * To ensure that both sequence numbers and the CCMP PN handling
1486236880Sadrian	 * is "correct", make sure that the relevant TID queue is locked.
1487236880Sadrian	 * Otherwise the CCMP PN and seqno may appear out of order, causing
1488236880Sadrian	 * re-ordered frames to have out of order CCMP PN's, resulting
1489236880Sadrian	 * in many, many frame drops.
1490236880Sadrian	 */
1491243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
1492236880Sadrian
1493218065Sadrian	wh = mtod(m0, struct ieee80211_frame *);
1494218065Sadrian	iswep = wh->i_fc[1] & IEEE80211_FC1_WEP;
1495218065Sadrian	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1496218065Sadrian	isfrag = m0->m_flags & M_FRAG;
1497218065Sadrian	hdrlen = ieee80211_anyhdrsize(wh);
1498218065Sadrian	/*
1499218065Sadrian	 * Packet length must not include any
1500218065Sadrian	 * pad bytes; deduct them here.
1501218065Sadrian	 */
1502218065Sadrian	pktlen = m0->m_pkthdr.len - (hdrlen & 3);
1503218065Sadrian
1504218154Sadrian	/* Handle encryption twiddling if needed */
1505227364Sadrian	if (! ath_tx_tag_crypto(sc, ni, m0, iswep, isfrag, &hdrlen,
1506227364Sadrian	    &pktlen, &keyix)) {
1507218154Sadrian		ath_freetx(m0);
1508218154Sadrian		return EIO;
1509218154Sadrian	}
1510218065Sadrian
1511218154Sadrian	/* packet header may have moved, reset our local pointer */
1512218154Sadrian	wh = mtod(m0, struct ieee80211_frame *);
1513218065Sadrian
1514218065Sadrian	pktlen += IEEE80211_CRC_LEN;
1515218065Sadrian
1516218065Sadrian	/*
1517218065Sadrian	 * Load the DMA map so any coalescing is done.  This
1518218065Sadrian	 * also calculates the number of descriptors we need.
1519218065Sadrian	 */
1520218065Sadrian	error = ath_tx_dmasetup(sc, bf, m0);
1521218065Sadrian	if (error != 0)
1522218065Sadrian		return error;
1523218065Sadrian	bf->bf_node = ni;			/* NB: held reference */
1524218065Sadrian	m0 = bf->bf_m;				/* NB: may have changed */
1525218065Sadrian	wh = mtod(m0, struct ieee80211_frame *);
1526218065Sadrian
1527218065Sadrian	/* setup descriptors */
1528218065Sadrian	ds = bf->bf_desc;
1529218065Sadrian	rt = sc->sc_currates;
1530218065Sadrian	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
1531218065Sadrian
1532218065Sadrian	/*
1533218065Sadrian	 * NB: the 802.11 layer marks whether or not we should
1534218065Sadrian	 * use short preamble based on the current mode and
1535218065Sadrian	 * negotiated parameters.
1536218065Sadrian	 */
1537218065Sadrian	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1538218065Sadrian	    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
1539218065Sadrian		shortPreamble = AH_TRUE;
1540218065Sadrian		sc->sc_stats.ast_tx_shortpre++;
1541218065Sadrian	} else {
1542218065Sadrian		shortPreamble = AH_FALSE;
1543218065Sadrian	}
1544218065Sadrian
1545218065Sadrian	an = ATH_NODE(ni);
1546240724Sadrian	//flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for crypto errs */
1547240724Sadrian	flags = 0;
1548218065Sadrian	ismrr = 0;				/* default no multi-rate retry*/
1549218065Sadrian	pri = M_WME_GETAC(m0);			/* honor classification */
1550218065Sadrian	/* XXX use txparams instead of fixed values */
1551218065Sadrian	/*
1552218065Sadrian	 * Calculate Atheros packet type from IEEE80211 packet header,
1553218065Sadrian	 * setup for rate calculations, and select h/w transmit queue.
1554218065Sadrian	 */
1555218065Sadrian	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1556218065Sadrian	case IEEE80211_FC0_TYPE_MGT:
1557218065Sadrian		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1558218065Sadrian		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON)
1559218065Sadrian			atype = HAL_PKT_TYPE_BEACON;
1560218065Sadrian		else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1561218065Sadrian			atype = HAL_PKT_TYPE_PROBE_RESP;
1562218065Sadrian		else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM)
1563218065Sadrian			atype = HAL_PKT_TYPE_ATIM;
1564218065Sadrian		else
1565218065Sadrian			atype = HAL_PKT_TYPE_NORMAL;	/* XXX */
1566218065Sadrian		rix = an->an_mgmtrix;
1567218065Sadrian		txrate = rt->info[rix].rateCode;
1568218065Sadrian		if (shortPreamble)
1569218065Sadrian			txrate |= rt->info[rix].shortPreamble;
1570218065Sadrian		try0 = ATH_TXMGTTRY;
1571218065Sadrian		flags |= HAL_TXDESC_INTREQ;	/* force interrupt */
1572218065Sadrian		break;
1573218065Sadrian	case IEEE80211_FC0_TYPE_CTL:
1574218065Sadrian		atype = HAL_PKT_TYPE_PSPOLL;	/* stop setting of duration */
1575218065Sadrian		rix = an->an_mgmtrix;
1576218065Sadrian		txrate = rt->info[rix].rateCode;
1577218065Sadrian		if (shortPreamble)
1578218065Sadrian			txrate |= rt->info[rix].shortPreamble;
1579218065Sadrian		try0 = ATH_TXMGTTRY;
1580218065Sadrian		flags |= HAL_TXDESC_INTREQ;	/* force interrupt */
1581218065Sadrian		break;
1582218065Sadrian	case IEEE80211_FC0_TYPE_DATA:
1583218065Sadrian		atype = HAL_PKT_TYPE_NORMAL;		/* default */
1584218065Sadrian		/*
1585218065Sadrian		 * Data frames: multicast frames go out at a fixed rate,
1586218065Sadrian		 * EAPOL frames use the mgmt frame rate; otherwise consult
1587218065Sadrian		 * the rate control module for the rate to use.
1588218065Sadrian		 */
1589218065Sadrian		if (ismcast) {
1590218065Sadrian			rix = an->an_mcastrix;
1591218065Sadrian			txrate = rt->info[rix].rateCode;
1592218065Sadrian			if (shortPreamble)
1593218065Sadrian				txrate |= rt->info[rix].shortPreamble;
1594218065Sadrian			try0 = 1;
1595218065Sadrian		} else if (m0->m_flags & M_EAPOL) {
1596218065Sadrian			/* XXX? maybe always use long preamble? */
1597218065Sadrian			rix = an->an_mgmtrix;
1598218065Sadrian			txrate = rt->info[rix].rateCode;
1599218065Sadrian			if (shortPreamble)
1600218065Sadrian				txrate |= rt->info[rix].shortPreamble;
1601218065Sadrian			try0 = ATH_TXMAXTRY;	/* XXX?too many? */
1602218065Sadrian		} else {
1603227364Sadrian			/*
1604227364Sadrian			 * Do rate lookup on each TX, rather than using
1605227364Sadrian			 * the hard-coded TX information decided here.
1606227364Sadrian			 */
1607227364Sadrian			ismrr = 1;
1608227364Sadrian			bf->bf_state.bfs_doratelookup = 1;
1609218065Sadrian		}
1610218065Sadrian		if (cap->cap_wmeParams[pri].wmep_noackPolicy)
1611218065Sadrian			flags |= HAL_TXDESC_NOACK;
1612218065Sadrian		break;
1613218065Sadrian	default:
1614218065Sadrian		if_printf(ifp, "bogus frame type 0x%x (%s)\n",
1615218065Sadrian			wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__);
1616218065Sadrian		/* XXX statistic */
1617218065Sadrian		ath_freetx(m0);
1618218065Sadrian		return EIO;
1619218065Sadrian	}
1620218065Sadrian
1621237041Sadrian	/*
1622237041Sadrian	 * There are two known scenarios where the frame AC doesn't match
1623237041Sadrian	 * what the destination TXQ is.
1624237041Sadrian	 *
1625237041Sadrian	 * + non-QoS frames (eg management?) that the net80211 stack has
1626237041Sadrian	 *   assigned a higher AC to, but since it's a non-QoS TID, it's
1627237041Sadrian	 *   being thrown into TID 16.  TID 16 gets the AC_BE queue.
1628237041Sadrian	 *   It's quite possible that management frames should just be
1629237041Sadrian	 *   direct dispatched to hardware rather than go via the software
1630237041Sadrian	 *   queue; that should be investigated in the future.  There are
1631237041Sadrian	 *   some specific scenarios where this doesn't make sense, mostly
1632237041Sadrian	 *   surrounding ADDBA request/response - hence why that is special
1633237041Sadrian	 *   cased.
1634237041Sadrian	 *
1635237041Sadrian	 * + Multicast frames going into the VAP mcast queue.  That shows up
1636237041Sadrian	 *   as "TXQ 11".
1637237041Sadrian	 *
1638237041Sadrian	 * This driver should eventually support separate TID and TXQ locking,
1639237041Sadrian	 * allowing for arbitrary AC frames to appear on arbitrary software
1640237041Sadrian	 * queues, being queued to the "correct" hardware queue when needed.
1641237041Sadrian	 */
1642237041Sadrian#if 0
1643235750Sadrian	if (txq != sc->sc_ac2q[pri]) {
1644235750Sadrian		device_printf(sc->sc_dev,
1645235750Sadrian		    "%s: txq=%p (%d), pri=%d, pri txq=%p (%d)\n",
1646235750Sadrian		    __func__,
1647235750Sadrian		    txq,
1648235750Sadrian		    txq->axq_qnum,
1649235750Sadrian		    pri,
1650235750Sadrian		    sc->sc_ac2q[pri],
1651235750Sadrian		    sc->sc_ac2q[pri]->axq_qnum);
1652235750Sadrian	}
1653237041Sadrian#endif
1654235750Sadrian
1655218065Sadrian	/*
1656218065Sadrian	 * Calculate miscellaneous flags.
1657218065Sadrian	 */
1658218065Sadrian	if (ismcast) {
1659218065Sadrian		flags |= HAL_TXDESC_NOACK;	/* no ack on broad/multicast */
1660218065Sadrian	} else if (pktlen > vap->iv_rtsthreshold &&
1661218065Sadrian	    (ni->ni_ath_flags & IEEE80211_NODE_FF) == 0) {
1662218065Sadrian		flags |= HAL_TXDESC_RTSENA;	/* RTS based on frame length */
1663218065Sadrian		sc->sc_stats.ast_tx_rts++;
1664218065Sadrian	}
1665218065Sadrian	if (flags & HAL_TXDESC_NOACK)		/* NB: avoid double counting */
1666218065Sadrian		sc->sc_stats.ast_tx_noack++;
1667218065Sadrian#ifdef IEEE80211_SUPPORT_TDMA
1668218065Sadrian	if (sc->sc_tdma && (flags & HAL_TXDESC_NOACK) == 0) {
1669218065Sadrian		DPRINTF(sc, ATH_DEBUG_TDMA,
1670218065Sadrian		    "%s: discard frame, ACK required w/ TDMA\n", __func__);
1671218065Sadrian		sc->sc_stats.ast_tdma_ack++;
1672218065Sadrian		ath_freetx(m0);
1673218065Sadrian		return EIO;
1674218065Sadrian	}
1675218065Sadrian#endif
1676218065Sadrian
1677218065Sadrian	/*
1678227364Sadrian	 * Determine if a tx interrupt should be generated for
1679227364Sadrian	 * this descriptor.  We take a tx interrupt to reap
1680227364Sadrian	 * descriptors when the h/w hits an EOL condition or
1681227364Sadrian	 * when the descriptor is specifically marked to generate
1682227364Sadrian	 * an interrupt.  We periodically mark descriptors in this
1683227364Sadrian	 * way to insure timely replenishing of the supply needed
1684227364Sadrian	 * for sending frames.  Defering interrupts reduces system
1685227364Sadrian	 * load and potentially allows more concurrent work to be
1686227364Sadrian	 * done but if done to aggressively can cause senders to
1687227364Sadrian	 * backup.
1688227364Sadrian	 *
1689227364Sadrian	 * NB: use >= to deal with sc_txintrperiod changing
1690227364Sadrian	 *     dynamically through sysctl.
1691218065Sadrian	 */
1692227364Sadrian	if (flags & HAL_TXDESC_INTREQ) {
1693227364Sadrian		txq->axq_intrcnt = 0;
1694227364Sadrian	} else if (++txq->axq_intrcnt >= sc->sc_txintrperiod) {
1695227364Sadrian		flags |= HAL_TXDESC_INTREQ;
1696227364Sadrian		txq->axq_intrcnt = 0;
1697227364Sadrian	}
1698218932Sadrian
1699227364Sadrian	/* This point forward is actual TX bits */
1700218065Sadrian
1701218065Sadrian	/*
1702218065Sadrian	 * At this point we are committed to sending the frame
1703218065Sadrian	 * and we don't need to look at m_nextpkt; clear it in
1704218065Sadrian	 * case this frame is part of frag chain.
1705218065Sadrian	 */
1706218065Sadrian	m0->m_nextpkt = NULL;
1707218065Sadrian
1708218065Sadrian	if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
1709218065Sadrian		ieee80211_dump_pkt(ic, mtod(m0, const uint8_t *), m0->m_len,
1710218065Sadrian		    sc->sc_hwmap[rix].ieeerate, -1);
1711218065Sadrian
1712218065Sadrian	if (ieee80211_radiotap_active_vap(vap)) {
1713218065Sadrian		u_int64_t tsf = ath_hal_gettsf64(ah);
1714218065Sadrian
1715218065Sadrian		sc->sc_tx_th.wt_tsf = htole64(tsf);
1716218065Sadrian		sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
1717218065Sadrian		if (iswep)
1718218065Sadrian			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
1719218065Sadrian		if (isfrag)
1720218065Sadrian			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
1721218065Sadrian		sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
1722218065Sadrian		sc->sc_tx_th.wt_txpower = ni->ni_txpower;
1723218065Sadrian		sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
1724218065Sadrian
1725218065Sadrian		ieee80211_radiotap_tx(vap, m0);
1726218065Sadrian	}
1727218065Sadrian
1728227364Sadrian	/* Blank the legacy rate array */
1729227364Sadrian	bzero(&bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
1730227364Sadrian
1731218065Sadrian	/*
1732227364Sadrian	 * ath_buf_set_rate needs at least one rate/try to setup
1733227364Sadrian	 * the rate scenario.
1734227364Sadrian	 */
1735227364Sadrian	bf->bf_state.bfs_rc[0].rix = rix;
1736227364Sadrian	bf->bf_state.bfs_rc[0].tries = try0;
1737227364Sadrian	bf->bf_state.bfs_rc[0].ratecode = txrate;
1738227364Sadrian
1739227364Sadrian	/* Store the decided rate index values away */
1740227364Sadrian	bf->bf_state.bfs_pktlen = pktlen;
1741227364Sadrian	bf->bf_state.bfs_hdrlen = hdrlen;
1742227364Sadrian	bf->bf_state.bfs_atype = atype;
1743227364Sadrian	bf->bf_state.bfs_txpower = ni->ni_txpower;
1744227364Sadrian	bf->bf_state.bfs_txrate0 = txrate;
1745227364Sadrian	bf->bf_state.bfs_try0 = try0;
1746227364Sadrian	bf->bf_state.bfs_keyix = keyix;
1747227364Sadrian	bf->bf_state.bfs_txantenna = sc->sc_txantenna;
1748233966Sadrian	bf->bf_state.bfs_txflags = flags;
1749227364Sadrian	bf->bf_state.bfs_shpream = shortPreamble;
1750227364Sadrian
1751227364Sadrian	/* XXX this should be done in ath_tx_setrate() */
1752227364Sadrian	bf->bf_state.bfs_ctsrate0 = 0;	/* ie, no hard-coded ctsrate */
1753227364Sadrian	bf->bf_state.bfs_ctsrate = 0;	/* calculated later */
1754227364Sadrian	bf->bf_state.bfs_ctsduration = 0;
1755227364Sadrian	bf->bf_state.bfs_ismrr = ismrr;
1756227364Sadrian
1757227364Sadrian	return 0;
1758227364Sadrian}
1759227364Sadrian
1760227364Sadrian/*
1761240724Sadrian * Queue a frame to the hardware or software queue.
1762227364Sadrian *
1763227364Sadrian * This can be called by the net80211 code.
1764227364Sadrian *
1765227364Sadrian * XXX what about locking? Or, push the seqno assign into the
1766227364Sadrian * XXX aggregate scheduler so its serialised?
1767240724Sadrian *
1768240724Sadrian * XXX When sending management frames via ath_raw_xmit(),
1769240724Sadrian *     should CLRDMASK be set unconditionally?
1770227364Sadrian */
1771227364Sadrianint
1772227364Sadrianath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni,
1773227364Sadrian    struct ath_buf *bf, struct mbuf *m0)
1774227364Sadrian{
1775227364Sadrian	struct ieee80211vap *vap = ni->ni_vap;
1776227364Sadrian	struct ath_vap *avp = ATH_VAP(vap);
1777232764Sadrian	int r = 0;
1778227364Sadrian	u_int pri;
1779227364Sadrian	int tid;
1780227364Sadrian	struct ath_txq *txq;
1781227364Sadrian	int ismcast;
1782227364Sadrian	const struct ieee80211_frame *wh;
1783227364Sadrian	int is_ampdu, is_ampdu_tx, is_ampdu_pending;
1784236872Sadrian	ieee80211_seq seqno;
1785227364Sadrian	uint8_t type, subtype;
1786227364Sadrian
1787243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
1788243786Sadrian
1789227364Sadrian	/*
1790227364Sadrian	 * Determine the target hardware queue.
1791218065Sadrian	 *
1792234009Sadrian	 * For multicast frames, the txq gets overridden appropriately
1793234009Sadrian	 * depending upon the state of PS.
1794227364Sadrian	 *
1795227364Sadrian	 * For any other frame, we do a TID/QoS lookup inside the frame
1796227364Sadrian	 * to see what the TID should be. If it's a non-QoS frame, the
1797227364Sadrian	 * AC and TID are overridden. The TID/TXQ code assumes the
1798227364Sadrian	 * TID is on a predictable hardware TXQ, so we don't support
1799227364Sadrian	 * having a node TID queued to multiple hardware TXQs.
1800227364Sadrian	 * This may change in the future but would require some locking
1801227364Sadrian	 * fudgery.
1802218065Sadrian	 */
1803227364Sadrian	pri = ath_tx_getac(sc, m0);
1804227364Sadrian	tid = ath_tx_gettid(sc, m0);
1805218065Sadrian
1806227364Sadrian	txq = sc->sc_ac2q[pri];
1807227364Sadrian	wh = mtod(m0, struct ieee80211_frame *);
1808227364Sadrian	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1809227364Sadrian	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1810227364Sadrian	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1811227364Sadrian
1812232764Sadrian	/*
1813232764Sadrian	 * Enforce how deep the multicast queue can grow.
1814232764Sadrian	 *
1815232764Sadrian	 * XXX duplicated in ath_raw_xmit().
1816232764Sadrian	 */
1817232764Sadrian	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1818232794Sadrian		if (sc->sc_cabq->axq_depth > sc->sc_txq_mcastq_maxdepth) {
1819232764Sadrian			sc->sc_stats.ast_tx_mcastq_overflow++;
1820232764Sadrian			r = ENOBUFS;
1821232764Sadrian		}
1822232764Sadrian		if (r != 0) {
1823232764Sadrian			m_freem(m0);
1824232764Sadrian			return r;
1825232764Sadrian		}
1826232764Sadrian	}
1827232764Sadrian
1828227364Sadrian	/* A-MPDU TX */
1829227364Sadrian	is_ampdu_tx = ath_tx_ampdu_running(sc, ATH_NODE(ni), tid);
1830227364Sadrian	is_ampdu_pending = ath_tx_ampdu_pending(sc, ATH_NODE(ni), tid);
1831227364Sadrian	is_ampdu = is_ampdu_tx | is_ampdu_pending;
1832227364Sadrian
1833236872Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d, ac=%d, is_ampdu=%d\n",
1834236872Sadrian	    __func__, tid, pri, is_ampdu);
1835227364Sadrian
1836239051Sadrian	/* Set local packet state, used to queue packets to hardware */
1837239051Sadrian	bf->bf_state.bfs_tid = tid;
1838244109Sadrian	bf->bf_state.bfs_tx_queue = txq->axq_qnum;
1839239051Sadrian	bf->bf_state.bfs_pri = pri;
1840239051Sadrian
1841248671Sadrian#if 1
1842232753Sadrian	/*
1843234009Sadrian	 * When servicing one or more stations in power-save mode
1844234009Sadrian	 * (or) if there is some mcast data waiting on the mcast
1845234009Sadrian	 * queue (to prevent out of order delivery) multicast frames
1846234009Sadrian	 * must be bufferd until after the beacon.
1847234009Sadrian	 *
1848234009Sadrian	 * TODO: we should lock the mcastq before we check the length.
1849232753Sadrian	 */
1850248671Sadrian	if (sc->sc_cabq_enable && ismcast && (vap->iv_ps_sta || avp->av_mcastq.axq_depth)) {
1851227364Sadrian		txq = &avp->av_mcastq;
1852239051Sadrian		/*
1853239051Sadrian		 * Mark the frame as eventually belonging on the CAB
1854239051Sadrian		 * queue, so the descriptor setup functions will
1855239051Sadrian		 * correctly initialise the descriptor 'qcuId' field.
1856239051Sadrian		 */
1857244109Sadrian		bf->bf_state.bfs_tx_queue = sc->sc_cabq->axq_qnum;
1858239051Sadrian	}
1859248671Sadrian#endif
1860227364Sadrian
1861227364Sadrian	/* Do the generic frame setup */
1862227364Sadrian	/* XXX should just bzero the bf_state? */
1863227364Sadrian	bf->bf_state.bfs_dobaw = 0;
1864227364Sadrian
1865227364Sadrian	/* A-MPDU TX? Manually set sequence number */
1866236880Sadrian	/*
1867236880Sadrian	 * Don't do it whilst pending; the net80211 layer still
1868236880Sadrian	 * assigns them.
1869236880Sadrian	 */
1870227364Sadrian	if (is_ampdu_tx) {
1871227364Sadrian		/*
1872227364Sadrian		 * Always call; this function will
1873227364Sadrian		 * handle making sure that null data frames
1874227364Sadrian		 * don't get a sequence number from the current
1875227364Sadrian		 * TID and thus mess with the BAW.
1876227364Sadrian		 */
1877236872Sadrian		seqno = ath_tx_tid_seqno_assign(sc, ni, bf, m0);
1878236874Sadrian
1879236874Sadrian		/*
1880236874Sadrian		 * Don't add QoS NULL frames to the BAW.
1881236874Sadrian		 */
1882236872Sadrian		if (IEEE80211_QOS_HAS_SEQ(wh) &&
1883236872Sadrian		    subtype != IEEE80211_FC0_SUBTYPE_QOS_NULL) {
1884227364Sadrian			bf->bf_state.bfs_dobaw = 1;
1885227364Sadrian		}
1886218240Sadrian	}
1887218240Sadrian
1888218065Sadrian	/*
1889227364Sadrian	 * If needed, the sequence number has been assigned.
1890227364Sadrian	 * Squirrel it away somewhere easy to get to.
1891218065Sadrian	 */
1892236872Sadrian	bf->bf_state.bfs_seqno = M_SEQNO_GET(m0) << IEEE80211_SEQ_SEQ_SHIFT;
1893227364Sadrian
1894227364Sadrian	/* Is ampdu pending? fetch the seqno and print it out */
1895227364Sadrian	if (is_ampdu_pending)
1896227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX,
1897227364Sadrian		    "%s: tid %d: ampdu pending, seqno %d\n",
1898227364Sadrian		    __func__, tid, M_SEQNO_GET(m0));
1899227364Sadrian
1900227364Sadrian	/* This also sets up the DMA map */
1901234009Sadrian	r = ath_tx_normal_setup(sc, ni, bf, m0, txq);
1902227364Sadrian
1903227364Sadrian	if (r != 0)
1904236880Sadrian		goto done;
1905227364Sadrian
1906227364Sadrian	/* At this point m0 could have changed! */
1907227364Sadrian	m0 = bf->bf_m;
1908227364Sadrian
1909227364Sadrian#if 1
1910218065Sadrian	/*
1911227364Sadrian	 * If it's a multicast frame, do a direct-dispatch to the
1912227364Sadrian	 * destination hardware queue. Don't bother software
1913227364Sadrian	 * queuing it.
1914218065Sadrian	 */
1915227364Sadrian	/*
1916227364Sadrian	 * If it's a BAR frame, do a direct dispatch to the
1917227364Sadrian	 * destination hardware queue. Don't bother software
1918227364Sadrian	 * queuing it, as the TID will now be paused.
1919227364Sadrian	 * Sending a BAR frame can occur from the net80211 txa timer
1920227364Sadrian	 * (ie, retries) or from the ath txtask (completion call.)
1921227364Sadrian	 * It queues directly to hardware because the TID is paused
1922227364Sadrian	 * at this point (and won't be unpaused until the BAR has
1923227364Sadrian	 * either been TXed successfully or max retries has been
1924227364Sadrian	 * reached.)
1925227364Sadrian	 */
1926227364Sadrian	if (txq == &avp->av_mcastq) {
1927235774Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX,
1928233227Sadrian		    "%s: bf=%p: mcastq: TX'ing\n", __func__, bf);
1929240724Sadrian		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
1930227364Sadrian		ath_tx_xmit_normal(sc, txq, bf);
1931227364Sadrian	} else if (type == IEEE80211_FC0_TYPE_CTL &&
1932227364Sadrian		    subtype == IEEE80211_FC0_SUBTYPE_BAR) {
1933235774Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX,
1934227364Sadrian		    "%s: BAR: TX'ing direct\n", __func__);
1935240724Sadrian		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
1936227364Sadrian		ath_tx_xmit_normal(sc, txq, bf);
1937227364Sadrian	} else {
1938227364Sadrian		/* add to software queue */
1939235774Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX,
1940233227Sadrian		    "%s: bf=%p: swq: TX'ing\n", __func__, bf);
1941227364Sadrian		ath_tx_swq(sc, ni, txq, bf);
1942227364Sadrian	}
1943227364Sadrian#else
1944227364Sadrian	/*
1945227364Sadrian	 * For now, since there's no software queue,
1946227364Sadrian	 * direct-dispatch to the hardware.
1947227364Sadrian	 */
1948240724Sadrian	bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
1949227364Sadrian	ath_tx_xmit_normal(sc, txq, bf);
1950236880Sadrian#endif
1951236880Sadriandone:
1952218065Sadrian	return 0;
1953218065Sadrian}
1954218065Sadrian
1955218065Sadrianstatic int
1956218065Sadrianath_tx_raw_start(struct ath_softc *sc, struct ieee80211_node *ni,
1957218065Sadrian	struct ath_buf *bf, struct mbuf *m0,
1958218065Sadrian	const struct ieee80211_bpf_params *params)
1959218065Sadrian{
1960218065Sadrian	struct ifnet *ifp = sc->sc_ifp;
1961218065Sadrian	struct ieee80211com *ic = ifp->if_l2com;
1962218065Sadrian	struct ath_hal *ah = sc->sc_ah;
1963218065Sadrian	struct ieee80211vap *vap = ni->ni_vap;
1964218065Sadrian	int error, ismcast, ismrr;
1965218065Sadrian	int keyix, hdrlen, pktlen, try0, txantenna;
1966227364Sadrian	u_int8_t rix, txrate;
1967218065Sadrian	struct ieee80211_frame *wh;
1968227364Sadrian	u_int flags;
1969218065Sadrian	HAL_PKT_TYPE atype;
1970218065Sadrian	const HAL_RATE_TABLE *rt;
1971218065Sadrian	struct ath_desc *ds;
1972218065Sadrian	u_int pri;
1973227364Sadrian	int o_tid = -1;
1974227364Sadrian	int do_override;
1975218065Sadrian
1976243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
1977243786Sadrian
1978218065Sadrian	wh = mtod(m0, struct ieee80211_frame *);
1979218065Sadrian	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1980218065Sadrian	hdrlen = ieee80211_anyhdrsize(wh);
1981218065Sadrian	/*
1982218065Sadrian	 * Packet length must not include any
1983218065Sadrian	 * pad bytes; deduct them here.
1984218065Sadrian	 */
1985218065Sadrian	/* XXX honor IEEE80211_BPF_DATAPAD */
1986218065Sadrian	pktlen = m0->m_pkthdr.len - (hdrlen & 3) + IEEE80211_CRC_LEN;
1987218065Sadrian
1988240899Sadrian	ATH_KTR(sc, ATH_KTR_TX, 2,
1989240899Sadrian	     "ath_tx_raw_start: ni=%p, bf=%p, raw", ni, bf);
1990240899Sadrian
1991227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: ismcast=%d\n",
1992227364Sadrian	    __func__, ismcast);
1993227364Sadrian
1994236880Sadrian	pri = params->ibp_pri & 3;
1995236880Sadrian	/* Override pri if the frame isn't a QoS one */
1996236880Sadrian	if (! IEEE80211_QOS_HAS_SEQ(wh))
1997236880Sadrian		pri = ath_tx_getac(sc, m0);
1998236880Sadrian
1999236880Sadrian	/* XXX If it's an ADDBA, override the correct queue */
2000236880Sadrian	do_override = ath_tx_action_frame_override_queue(sc, ni, m0, &o_tid);
2001236880Sadrian
2002236880Sadrian	/* Map ADDBA to the correct priority */
2003236880Sadrian	if (do_override) {
2004236880Sadrian#if 0
2005236880Sadrian		device_printf(sc->sc_dev,
2006236880Sadrian		    "%s: overriding tid %d pri %d -> %d\n",
2007236880Sadrian		    __func__, o_tid, pri, TID_TO_WME_AC(o_tid));
2008236880Sadrian#endif
2009236880Sadrian		pri = TID_TO_WME_AC(o_tid);
2010236880Sadrian	}
2011236880Sadrian
2012218154Sadrian	/* Handle encryption twiddling if needed */
2013227364Sadrian	if (! ath_tx_tag_crypto(sc, ni,
2014227364Sadrian	    m0, params->ibp_flags & IEEE80211_BPF_CRYPTO, 0,
2015227364Sadrian	    &hdrlen, &pktlen, &keyix)) {
2016218154Sadrian		ath_freetx(m0);
2017218154Sadrian		return EIO;
2018218154Sadrian	}
2019218154Sadrian	/* packet header may have moved, reset our local pointer */
2020218154Sadrian	wh = mtod(m0, struct ieee80211_frame *);
2021218065Sadrian
2022227364Sadrian	/* Do the generic frame setup */
2023227364Sadrian	/* XXX should just bzero the bf_state? */
2024227364Sadrian	bf->bf_state.bfs_dobaw = 0;
2025227364Sadrian
2026218065Sadrian	error = ath_tx_dmasetup(sc, bf, m0);
2027218065Sadrian	if (error != 0)
2028218065Sadrian		return error;
2029218065Sadrian	m0 = bf->bf_m;				/* NB: may have changed */
2030218065Sadrian	wh = mtod(m0, struct ieee80211_frame *);
2031218065Sadrian	bf->bf_node = ni;			/* NB: held reference */
2032218065Sadrian
2033240724Sadrian	/* Always enable CLRDMASK for raw frames for now.. */
2034218065Sadrian	flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for crypto errs */
2035218065Sadrian	flags |= HAL_TXDESC_INTREQ;		/* force interrupt */
2036218065Sadrian	if (params->ibp_flags & IEEE80211_BPF_RTS)
2037218065Sadrian		flags |= HAL_TXDESC_RTSENA;
2038227364Sadrian	else if (params->ibp_flags & IEEE80211_BPF_CTS) {
2039227364Sadrian		/* XXX assume 11g/11n protection? */
2040227364Sadrian		bf->bf_state.bfs_doprot = 1;
2041218065Sadrian		flags |= HAL_TXDESC_CTSENA;
2042227364Sadrian	}
2043218065Sadrian	/* XXX leave ismcast to injector? */
2044218065Sadrian	if ((params->ibp_flags & IEEE80211_BPF_NOACK) || ismcast)
2045218065Sadrian		flags |= HAL_TXDESC_NOACK;
2046218065Sadrian
2047218065Sadrian	rt = sc->sc_currates;
2048218065Sadrian	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
2049218065Sadrian	rix = ath_tx_findrix(sc, params->ibp_rate0);
2050218065Sadrian	txrate = rt->info[rix].rateCode;
2051218065Sadrian	if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
2052218065Sadrian		txrate |= rt->info[rix].shortPreamble;
2053218065Sadrian	sc->sc_txrix = rix;
2054218065Sadrian	try0 = params->ibp_try0;
2055218065Sadrian	ismrr = (params->ibp_try1 != 0);
2056218065Sadrian	txantenna = params->ibp_pri >> 2;
2057218065Sadrian	if (txantenna == 0)			/* XXX? */
2058218065Sadrian		txantenna = sc->sc_txantenna;
2059218157Sadrian
2060227364Sadrian	/*
2061227364Sadrian	 * Since ctsrate is fixed, store it away for later
2062227364Sadrian	 * use when the descriptor fields are being set.
2063227364Sadrian	 */
2064227364Sadrian	if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA))
2065227364Sadrian		bf->bf_state.bfs_ctsrate0 = params->ibp_ctsrate;
2066218157Sadrian
2067218065Sadrian	/*
2068218065Sadrian	 * NB: we mark all packets as type PSPOLL so the h/w won't
2069218065Sadrian	 * set the sequence number, duration, etc.
2070218065Sadrian	 */
2071218065Sadrian	atype = HAL_PKT_TYPE_PSPOLL;
2072218065Sadrian
2073218065Sadrian	if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
2074218065Sadrian		ieee80211_dump_pkt(ic, mtod(m0, caddr_t), m0->m_len,
2075218065Sadrian		    sc->sc_hwmap[rix].ieeerate, -1);
2076218065Sadrian
2077218065Sadrian	if (ieee80211_radiotap_active_vap(vap)) {
2078218065Sadrian		u_int64_t tsf = ath_hal_gettsf64(ah);
2079218065Sadrian
2080218065Sadrian		sc->sc_tx_th.wt_tsf = htole64(tsf);
2081218065Sadrian		sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
2082218065Sadrian		if (wh->i_fc[1] & IEEE80211_FC1_WEP)
2083218065Sadrian			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2084218065Sadrian		if (m0->m_flags & M_FRAG)
2085218065Sadrian			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
2086218065Sadrian		sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
2087218065Sadrian		sc->sc_tx_th.wt_txpower = ni->ni_txpower;
2088218065Sadrian		sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
2089218065Sadrian
2090218065Sadrian		ieee80211_radiotap_tx(vap, m0);
2091218065Sadrian	}
2092218065Sadrian
2093218065Sadrian	/*
2094218065Sadrian	 * Formulate first tx descriptor with tx controls.
2095218065Sadrian	 */
2096218065Sadrian	ds = bf->bf_desc;
2097218065Sadrian	/* XXX check return value? */
2098227364Sadrian
2099227364Sadrian	/* Store the decided rate index values away */
2100227364Sadrian	bf->bf_state.bfs_pktlen = pktlen;
2101227364Sadrian	bf->bf_state.bfs_hdrlen = hdrlen;
2102227364Sadrian	bf->bf_state.bfs_atype = atype;
2103227364Sadrian	bf->bf_state.bfs_txpower = params->ibp_power;
2104227364Sadrian	bf->bf_state.bfs_txrate0 = txrate;
2105227364Sadrian	bf->bf_state.bfs_try0 = try0;
2106227364Sadrian	bf->bf_state.bfs_keyix = keyix;
2107227364Sadrian	bf->bf_state.bfs_txantenna = txantenna;
2108233966Sadrian	bf->bf_state.bfs_txflags = flags;
2109227364Sadrian	bf->bf_state.bfs_shpream =
2110227364Sadrian	    !! (params->ibp_flags & IEEE80211_BPF_SHORTPRE);
2111218065Sadrian
2112239051Sadrian	/* Set local packet state, used to queue packets to hardware */
2113239051Sadrian	bf->bf_state.bfs_tid = WME_AC_TO_TID(pri);
2114244109Sadrian	bf->bf_state.bfs_tx_queue = sc->sc_ac2q[pri]->axq_qnum;
2115239051Sadrian	bf->bf_state.bfs_pri = pri;
2116239051Sadrian
2117227364Sadrian	/* XXX this should be done in ath_tx_setrate() */
2118227364Sadrian	bf->bf_state.bfs_ctsrate = 0;
2119227364Sadrian	bf->bf_state.bfs_ctsduration = 0;
2120227364Sadrian	bf->bf_state.bfs_ismrr = ismrr;
2121218240Sadrian
2122227364Sadrian	/* Blank the legacy rate array */
2123227364Sadrian	bzero(&bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
2124218240Sadrian
2125227364Sadrian	bf->bf_state.bfs_rc[0].rix =
2126227364Sadrian	    ath_tx_findrix(sc, params->ibp_rate0);
2127227364Sadrian	bf->bf_state.bfs_rc[0].tries = try0;
2128227364Sadrian	bf->bf_state.bfs_rc[0].ratecode = txrate;
2129227364Sadrian
2130227364Sadrian	if (ismrr) {
2131227364Sadrian		int rix;
2132227364Sadrian
2133227364Sadrian		rix = ath_tx_findrix(sc, params->ibp_rate1);
2134227364Sadrian		bf->bf_state.bfs_rc[1].rix = rix;
2135227364Sadrian		bf->bf_state.bfs_rc[1].tries = params->ibp_try1;
2136227364Sadrian
2137227364Sadrian		rix = ath_tx_findrix(sc, params->ibp_rate2);
2138227364Sadrian		bf->bf_state.bfs_rc[2].rix = rix;
2139227364Sadrian		bf->bf_state.bfs_rc[2].tries = params->ibp_try2;
2140227364Sadrian
2141227364Sadrian		rix = ath_tx_findrix(sc, params->ibp_rate3);
2142227364Sadrian		bf->bf_state.bfs_rc[3].rix = rix;
2143227364Sadrian		bf->bf_state.bfs_rc[3].tries = params->ibp_try3;
2144218065Sadrian	}
2145227364Sadrian	/*
2146227364Sadrian	 * All the required rate control decisions have been made;
2147227364Sadrian	 * fill in the rc flags.
2148227364Sadrian	 */
2149227364Sadrian	ath_tx_rate_fill_rcflags(sc, bf);
2150218065Sadrian
2151227364Sadrian	/* NB: no buffered multicast in power save support */
2152227364Sadrian
2153227364Sadrian	/*
2154227364Sadrian	 * If we're overiding the ADDBA destination, dump directly
2155227364Sadrian	 * into the hardware queue, right after any pending
2156227364Sadrian	 * frames to that node are.
2157227364Sadrian	 */
2158227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: dooverride=%d\n",
2159227364Sadrian	    __func__, do_override);
2160227364Sadrian
2161240882Sadrian#if 1
2162227364Sadrian	if (do_override) {
2163240724Sadrian		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
2164227364Sadrian		ath_tx_xmit_normal(sc, sc->sc_ac2q[pri], bf);
2165227364Sadrian	} else {
2166227364Sadrian		/* Queue to software queue */
2167227364Sadrian		ath_tx_swq(sc, ni, sc->sc_ac2q[pri], bf);
2168227364Sadrian	}
2169240882Sadrian#else
2170240882Sadrian	/* Direct-dispatch to the hardware */
2171240882Sadrian	bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
2172240882Sadrian	ath_tx_xmit_normal(sc, sc->sc_ac2q[pri], bf);
2173240882Sadrian#endif
2174218065Sadrian	return 0;
2175218065Sadrian}
2176218065Sadrian
2177227364Sadrian/*
2178227364Sadrian * Send a raw frame.
2179227364Sadrian *
2180227364Sadrian * This can be called by net80211.
2181227364Sadrian */
2182218065Sadrianint
2183218065Sadrianath_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2184218065Sadrian	const struct ieee80211_bpf_params *params)
2185218065Sadrian{
2186218065Sadrian	struct ieee80211com *ic = ni->ni_ic;
2187218065Sadrian	struct ifnet *ifp = ic->ic_ifp;
2188218065Sadrian	struct ath_softc *sc = ifp->if_softc;
2189218065Sadrian	struct ath_buf *bf;
2190232764Sadrian	struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
2191232764Sadrian	int error = 0;
2192218065Sadrian
2193227651Sadrian	ATH_PCU_LOCK(sc);
2194227651Sadrian	if (sc->sc_inreset_cnt > 0) {
2195227651Sadrian		device_printf(sc->sc_dev, "%s: sc_inreset_cnt > 0; bailing\n",
2196227651Sadrian		    __func__);
2197227651Sadrian		error = EIO;
2198227651Sadrian		ATH_PCU_UNLOCK(sc);
2199227651Sadrian		goto bad0;
2200227651Sadrian	}
2201227651Sadrian	sc->sc_txstart_cnt++;
2202227651Sadrian	ATH_PCU_UNLOCK(sc);
2203227651Sadrian
2204242391Sadrian	ATH_TX_LOCK(sc);
2205242391Sadrian
2206218065Sadrian	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->sc_invalid) {
2207218065Sadrian		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: discard frame, %s", __func__,
2208218065Sadrian		    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ?
2209218065Sadrian			"!running" : "invalid");
2210218065Sadrian		m_freem(m);
2211218065Sadrian		error = ENETDOWN;
2212218065Sadrian		goto bad;
2213218065Sadrian	}
2214232764Sadrian
2215218065Sadrian	/*
2216232764Sadrian	 * Enforce how deep the multicast queue can grow.
2217232764Sadrian	 *
2218232764Sadrian	 * XXX duplicated in ath_tx_start().
2219232764Sadrian	 */
2220232764Sadrian	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2221232794Sadrian		if (sc->sc_cabq->axq_depth > sc->sc_txq_mcastq_maxdepth) {
2222232764Sadrian			sc->sc_stats.ast_tx_mcastq_overflow++;
2223232764Sadrian			error = ENOBUFS;
2224232764Sadrian		}
2225232764Sadrian
2226232764Sadrian		if (error != 0) {
2227232764Sadrian			m_freem(m);
2228232764Sadrian			goto bad;
2229232764Sadrian		}
2230232764Sadrian	}
2231232764Sadrian
2232232764Sadrian	/*
2233218065Sadrian	 * Grab a TX buffer and associated resources.
2234218065Sadrian	 */
2235237000Sadrian	bf = ath_getbuf(sc, ATH_BUFTYPE_MGMT);
2236218065Sadrian	if (bf == NULL) {
2237218065Sadrian		sc->sc_stats.ast_tx_nobuf++;
2238218065Sadrian		m_freem(m);
2239218065Sadrian		error = ENOBUFS;
2240218065Sadrian		goto bad;
2241218065Sadrian	}
2242240899Sadrian	ATH_KTR(sc, ATH_KTR_TX, 3, "ath_raw_xmit: m=%p, params=%p, bf=%p\n",
2243240899Sadrian	    m, params,  bf);
2244218065Sadrian
2245218065Sadrian	if (params == NULL) {
2246218065Sadrian		/*
2247218065Sadrian		 * Legacy path; interpret frame contents to decide
2248218065Sadrian		 * precisely how to send the frame.
2249218065Sadrian		 */
2250218065Sadrian		if (ath_tx_start(sc, ni, bf, m)) {
2251218065Sadrian			error = EIO;		/* XXX */
2252218065Sadrian			goto bad2;
2253218065Sadrian		}
2254218065Sadrian	} else {
2255218065Sadrian		/*
2256218065Sadrian		 * Caller supplied explicit parameters to use in
2257218065Sadrian		 * sending the frame.
2258218065Sadrian		 */
2259218065Sadrian		if (ath_tx_raw_start(sc, ni, bf, m, params)) {
2260218065Sadrian			error = EIO;		/* XXX */
2261218065Sadrian			goto bad2;
2262218065Sadrian		}
2263218065Sadrian	}
2264218065Sadrian	sc->sc_wd_timer = 5;
2265218065Sadrian	ifp->if_opackets++;
2266218065Sadrian	sc->sc_stats.ast_tx_raw++;
2267218065Sadrian
2268242271Sadrian	/*
2269242271Sadrian	 * Update the TIM - if there's anything queued to the
2270242271Sadrian	 * software queue and power save is enabled, we should
2271242271Sadrian	 * set the TIM.
2272242271Sadrian	 */
2273242271Sadrian	ath_tx_update_tim(sc, ni, 1);
2274242271Sadrian
2275243787Sadrian	ATH_TX_UNLOCK(sc);
2276243787Sadrian
2277227651Sadrian	ATH_PCU_LOCK(sc);
2278227651Sadrian	sc->sc_txstart_cnt--;
2279227651Sadrian	ATH_PCU_UNLOCK(sc);
2280227651Sadrian
2281218065Sadrian	return 0;
2282218065Sadrianbad2:
2283240899Sadrian	ATH_KTR(sc, ATH_KTR_TX, 3, "ath_raw_xmit: bad2: m=%p, params=%p, "
2284240899Sadrian	    "bf=%p",
2285240899Sadrian	    m,
2286240899Sadrian	    params,
2287240899Sadrian	    bf);
2288218065Sadrian	ATH_TXBUF_LOCK(sc);
2289236993Sadrian	ath_returnbuf_head(sc, bf);
2290218065Sadrian	ATH_TXBUF_UNLOCK(sc);
2291218065Sadrianbad:
2292242391Sadrian
2293242391Sadrian	ATH_TX_UNLOCK(sc);
2294242391Sadrian
2295227651Sadrian	ATH_PCU_LOCK(sc);
2296227651Sadrian	sc->sc_txstart_cnt--;
2297227651Sadrian	ATH_PCU_UNLOCK(sc);
2298227651Sadrianbad0:
2299240899Sadrian	ATH_KTR(sc, ATH_KTR_TX, 2, "ath_raw_xmit: bad0: m=%p, params=%p",
2300240899Sadrian	    m, params);
2301218065Sadrian	ifp->if_oerrors++;
2302218065Sadrian	sc->sc_stats.ast_tx_raw_fail++;
2303218065Sadrian	ieee80211_free_node(ni);
2304227651Sadrian
2305218065Sadrian	return error;
2306218065Sadrian}
2307227364Sadrian
2308227364Sadrian/* Some helper functions */
2309227364Sadrian
2310227364Sadrian/*
2311227364Sadrian * ADDBA (and potentially others) need to be placed in the same
2312227364Sadrian * hardware queue as the TID/node it's relating to. This is so
2313227364Sadrian * it goes out after any pending non-aggregate frames to the
2314227364Sadrian * same node/TID.
2315227364Sadrian *
2316227364Sadrian * If this isn't done, the ADDBA can go out before the frames
2317227364Sadrian * queued in hardware. Even though these frames have a sequence
2318227364Sadrian * number -earlier- than the ADDBA can be transmitted (but
2319227364Sadrian * no frames whose sequence numbers are after the ADDBA should
2320227364Sadrian * be!) they'll arrive after the ADDBA - and the receiving end
2321227364Sadrian * will simply drop them as being out of the BAW.
2322227364Sadrian *
2323227364Sadrian * The frames can't be appended to the TID software queue - it'll
2324227364Sadrian * never be sent out. So these frames have to be directly
2325227364Sadrian * dispatched to the hardware, rather than queued in software.
2326227364Sadrian * So if this function returns true, the TXQ has to be
2327227364Sadrian * overridden and it has to be directly dispatched.
2328227364Sadrian *
2329227364Sadrian * It's a dirty hack, but someone's gotta do it.
2330227364Sadrian */
2331227364Sadrian
2332227364Sadrian/*
2333227364Sadrian * XXX doesn't belong here!
2334227364Sadrian */
2335227364Sadrianstatic int
2336227364Sadrianieee80211_is_action(struct ieee80211_frame *wh)
2337227364Sadrian{
2338227364Sadrian	/* Type: Management frame? */
2339227364Sadrian	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) !=
2340227364Sadrian	    IEEE80211_FC0_TYPE_MGT)
2341227364Sadrian		return 0;
2342227364Sadrian
2343227364Sadrian	/* Subtype: Action frame? */
2344227364Sadrian	if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) !=
2345227364Sadrian	    IEEE80211_FC0_SUBTYPE_ACTION)
2346227364Sadrian		return 0;
2347227364Sadrian
2348227364Sadrian	return 1;
2349227364Sadrian}
2350227364Sadrian
2351227364Sadrian#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
2352227364Sadrian/*
2353227364Sadrian * Return an alternate TID for ADDBA request frames.
2354227364Sadrian *
2355227364Sadrian * Yes, this likely should be done in the net80211 layer.
2356227364Sadrian */
2357227364Sadrianstatic int
2358227364Sadrianath_tx_action_frame_override_queue(struct ath_softc *sc,
2359227364Sadrian    struct ieee80211_node *ni,
2360227364Sadrian    struct mbuf *m0, int *tid)
2361227364Sadrian{
2362227364Sadrian	struct ieee80211_frame *wh = mtod(m0, struct ieee80211_frame *);
2363227364Sadrian	struct ieee80211_action_ba_addbarequest *ia;
2364227364Sadrian	uint8_t *frm;
2365227364Sadrian	uint16_t baparamset;
2366227364Sadrian
2367227364Sadrian	/* Not action frame? Bail */
2368227364Sadrian	if (! ieee80211_is_action(wh))
2369227364Sadrian		return 0;
2370227364Sadrian
2371227364Sadrian	/* XXX Not needed for frames we send? */
2372227364Sadrian#if 0
2373227364Sadrian	/* Correct length? */
2374227364Sadrian	if (! ieee80211_parse_action(ni, m))
2375227364Sadrian		return 0;
2376227364Sadrian#endif
2377227364Sadrian
2378227364Sadrian	/* Extract out action frame */
2379227364Sadrian	frm = (u_int8_t *)&wh[1];
2380227364Sadrian	ia = (struct ieee80211_action_ba_addbarequest *) frm;
2381227364Sadrian
2382227364Sadrian	/* Not ADDBA? Bail */
2383227364Sadrian	if (ia->rq_header.ia_category != IEEE80211_ACTION_CAT_BA)
2384227364Sadrian		return 0;
2385227364Sadrian	if (ia->rq_header.ia_action != IEEE80211_ACTION_BA_ADDBA_REQUEST)
2386227364Sadrian		return 0;
2387227364Sadrian
2388227364Sadrian	/* Extract TID, return it */
2389227364Sadrian	baparamset = le16toh(ia->rq_baparamset);
2390227364Sadrian	*tid = (int) MS(baparamset, IEEE80211_BAPS_TID);
2391227364Sadrian
2392227364Sadrian	return 1;
2393227364Sadrian}
2394227364Sadrian#undef	MS
2395227364Sadrian
2396227364Sadrian/* Per-node software queue operations */
2397227364Sadrian
2398227364Sadrian/*
2399227364Sadrian * Add the current packet to the given BAW.
2400227364Sadrian * It is assumed that the current packet
2401227364Sadrian *
2402227364Sadrian * + fits inside the BAW;
2403227364Sadrian * + already has had a sequence number allocated.
2404227364Sadrian *
2405227364Sadrian * Since the BAW status may be modified by both the ath task and
2406227364Sadrian * the net80211/ifnet contexts, the TID must be locked.
2407227364Sadrian */
2408227364Sadrianvoid
2409227364Sadrianath_tx_addto_baw(struct ath_softc *sc, struct ath_node *an,
2410227364Sadrian    struct ath_tid *tid, struct ath_buf *bf)
2411227364Sadrian{
2412227364Sadrian	int index, cindex;
2413227364Sadrian	struct ieee80211_tx_ampdu *tap;
2414227364Sadrian
2415243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
2416227364Sadrian
2417227364Sadrian	if (bf->bf_state.bfs_isretried)
2418227364Sadrian		return;
2419227364Sadrian
2420236886Sadrian	tap = ath_tx_get_tx_tid(an, tid->tid);
2421236886Sadrian
2422236880Sadrian	if (! bf->bf_state.bfs_dobaw) {
2423236880Sadrian		device_printf(sc->sc_dev,
2424236880Sadrian		    "%s: dobaw=0, seqno=%d, window %d:%d\n",
2425236880Sadrian		    __func__,
2426236880Sadrian		    SEQNO(bf->bf_state.bfs_seqno),
2427236880Sadrian		    tap->txa_start,
2428236880Sadrian		    tap->txa_wnd);
2429236880Sadrian	}
2430236880Sadrian
2431227364Sadrian	if (bf->bf_state.bfs_addedbaw)
2432227364Sadrian		device_printf(sc->sc_dev,
2433236872Sadrian		    "%s: re-added? tid=%d, seqno %d; window %d:%d; "
2434229949Sadrian		    "baw head=%d tail=%d\n",
2435236872Sadrian		    __func__, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
2436229949Sadrian		    tap->txa_start, tap->txa_wnd, tid->baw_head,
2437229949Sadrian		    tid->baw_tail);
2438227364Sadrian
2439227364Sadrian	/*
2440236880Sadrian	 * Verify that the given sequence number is not outside of the
2441236880Sadrian	 * BAW.  Complain loudly if that's the case.
2442236880Sadrian	 */
2443236880Sadrian	if (! BAW_WITHIN(tap->txa_start, tap->txa_wnd,
2444236880Sadrian	    SEQNO(bf->bf_state.bfs_seqno))) {
2445236880Sadrian		device_printf(sc->sc_dev,
2446236880Sadrian		    "%s: bf=%p: outside of BAW?? tid=%d, seqno %d; window %d:%d; "
2447236880Sadrian		    "baw head=%d tail=%d\n",
2448236880Sadrian		    __func__, bf, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
2449236880Sadrian		    tap->txa_start, tap->txa_wnd, tid->baw_head,
2450236880Sadrian		    tid->baw_tail);
2451236880Sadrian	}
2452236880Sadrian
2453236880Sadrian	/*
2454227364Sadrian	 * ni->ni_txseqs[] is the currently allocated seqno.
2455227364Sadrian	 * the txa state contains the current baw start.
2456227364Sadrian	 */
2457227364Sadrian	index  = ATH_BA_INDEX(tap->txa_start, SEQNO(bf->bf_state.bfs_seqno));
2458227364Sadrian	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
2459227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2460236872Sadrian	    "%s: tid=%d, seqno %d; window %d:%d; index=%d cindex=%d "
2461229949Sadrian	    "baw head=%d tail=%d\n",
2462236872Sadrian	    __func__, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
2463229949Sadrian	    tap->txa_start, tap->txa_wnd, index, cindex, tid->baw_head,
2464229949Sadrian	    tid->baw_tail);
2465227364Sadrian
2466227364Sadrian
2467227364Sadrian#if 0
2468227364Sadrian	assert(tid->tx_buf[cindex] == NULL);
2469227364Sadrian#endif
2470227364Sadrian	if (tid->tx_buf[cindex] != NULL) {
2471227364Sadrian		device_printf(sc->sc_dev,
2472227364Sadrian		    "%s: ba packet dup (index=%d, cindex=%d, "
2473227364Sadrian		    "head=%d, tail=%d)\n",
2474227364Sadrian		    __func__, index, cindex, tid->baw_head, tid->baw_tail);
2475227364Sadrian		device_printf(sc->sc_dev,
2476227364Sadrian		    "%s: BA bf: %p; seqno=%d ; new bf: %p; seqno=%d\n",
2477227364Sadrian		    __func__,
2478227364Sadrian		    tid->tx_buf[cindex],
2479227364Sadrian		    SEQNO(tid->tx_buf[cindex]->bf_state.bfs_seqno),
2480227364Sadrian		    bf,
2481227364Sadrian		    SEQNO(bf->bf_state.bfs_seqno)
2482227364Sadrian		);
2483227364Sadrian	}
2484227364Sadrian	tid->tx_buf[cindex] = bf;
2485227364Sadrian
2486229949Sadrian	if (index >= ((tid->baw_tail - tid->baw_head) &
2487229949Sadrian	    (ATH_TID_MAX_BUFS - 1))) {
2488227364Sadrian		tid->baw_tail = cindex;
2489227364Sadrian		INCR(tid->baw_tail, ATH_TID_MAX_BUFS);
2490227364Sadrian	}
2491227364Sadrian}
2492227364Sadrian
2493227364Sadrian/*
2494227398Sadrian * Flip the BAW buffer entry over from the existing one to the new one.
2495227398Sadrian *
2496227398Sadrian * When software retransmitting a (sub-)frame, it is entirely possible that
2497227398Sadrian * the frame ath_buf is marked as BUSY and can't be immediately reused.
2498227398Sadrian * In that instance the buffer is cloned and the new buffer is used for
2499227398Sadrian * retransmit. We thus need to update the ath_buf slot in the BAW buf
2500227398Sadrian * tracking array to maintain consistency.
2501227398Sadrian */
2502227398Sadrianstatic void
2503227398Sadrianath_tx_switch_baw_buf(struct ath_softc *sc, struct ath_node *an,
2504227398Sadrian    struct ath_tid *tid, struct ath_buf *old_bf, struct ath_buf *new_bf)
2505227398Sadrian{
2506227398Sadrian	int index, cindex;
2507227398Sadrian	struct ieee80211_tx_ampdu *tap;
2508227398Sadrian	int seqno = SEQNO(old_bf->bf_state.bfs_seqno);
2509227398Sadrian
2510243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
2511227398Sadrian
2512227398Sadrian	tap = ath_tx_get_tx_tid(an, tid->tid);
2513227398Sadrian	index  = ATH_BA_INDEX(tap->txa_start, seqno);
2514227398Sadrian	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
2515227398Sadrian
2516227398Sadrian	/*
2517227398Sadrian	 * Just warn for now; if it happens then we should find out
2518227398Sadrian	 * about it. It's highly likely the aggregation session will
2519227398Sadrian	 * soon hang.
2520227398Sadrian	 */
2521227398Sadrian	if (old_bf->bf_state.bfs_seqno != new_bf->bf_state.bfs_seqno) {
2522227398Sadrian		device_printf(sc->sc_dev, "%s: retransmitted buffer"
2523227398Sadrian		    " has mismatching seqno's, BA session may hang.\n",
2524227398Sadrian		    __func__);
2525227398Sadrian		device_printf(sc->sc_dev, "%s: old seqno=%d, new_seqno=%d\n",
2526227398Sadrian		    __func__,
2527227398Sadrian		    old_bf->bf_state.bfs_seqno,
2528227398Sadrian		    new_bf->bf_state.bfs_seqno);
2529227398Sadrian	}
2530227398Sadrian
2531227398Sadrian	if (tid->tx_buf[cindex] != old_bf) {
2532227398Sadrian		device_printf(sc->sc_dev, "%s: ath_buf pointer incorrect; "
2533227398Sadrian		    " has m BA session may hang.\n",
2534227398Sadrian		    __func__);
2535227398Sadrian		device_printf(sc->sc_dev, "%s: old bf=%p, new bf=%p\n",
2536227398Sadrian		    __func__,
2537227398Sadrian		    old_bf, new_bf);
2538227398Sadrian	}
2539227398Sadrian
2540227398Sadrian	tid->tx_buf[cindex] = new_bf;
2541227398Sadrian}
2542227398Sadrian
2543227398Sadrian/*
2544227364Sadrian * seq_start - left edge of BAW
2545227364Sadrian * seq_next - current/next sequence number to allocate
2546227364Sadrian *
2547227364Sadrian * Since the BAW status may be modified by both the ath task and
2548227364Sadrian * the net80211/ifnet contexts, the TID must be locked.
2549227364Sadrian */
2550227364Sadrianstatic void
2551227364Sadrianath_tx_update_baw(struct ath_softc *sc, struct ath_node *an,
2552227364Sadrian    struct ath_tid *tid, const struct ath_buf *bf)
2553227364Sadrian{
2554227364Sadrian	int index, cindex;
2555227364Sadrian	struct ieee80211_tx_ampdu *tap;
2556227364Sadrian	int seqno = SEQNO(bf->bf_state.bfs_seqno);
2557227364Sadrian
2558243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
2559227364Sadrian
2560227364Sadrian	tap = ath_tx_get_tx_tid(an, tid->tid);
2561227364Sadrian	index  = ATH_BA_INDEX(tap->txa_start, seqno);
2562227364Sadrian	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
2563227364Sadrian
2564227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2565236872Sadrian	    "%s: tid=%d, baw=%d:%d, seqno=%d, index=%d, cindex=%d, "
2566229949Sadrian	    "baw head=%d, tail=%d\n",
2567236872Sadrian	    __func__, tid->tid, tap->txa_start, tap->txa_wnd, seqno, index,
2568227364Sadrian	    cindex, tid->baw_head, tid->baw_tail);
2569227364Sadrian
2570227364Sadrian	/*
2571227364Sadrian	 * If this occurs then we have a big problem - something else
2572227364Sadrian	 * has slid tap->txa_start along without updating the BAW
2573227364Sadrian	 * tracking start/end pointers. Thus the TX BAW state is now
2574227364Sadrian	 * completely busted.
2575227364Sadrian	 *
2576227364Sadrian	 * But for now, since I haven't yet fixed TDMA and buffer cloning,
2577227364Sadrian	 * it's quite possible that a cloned buffer is making its way
2578227364Sadrian	 * here and causing it to fire off. Disable TDMA for now.
2579227364Sadrian	 */
2580227364Sadrian	if (tid->tx_buf[cindex] != bf) {
2581227364Sadrian		device_printf(sc->sc_dev,
2582227364Sadrian		    "%s: comp bf=%p, seq=%d; slot bf=%p, seqno=%d\n",
2583227364Sadrian		    __func__,
2584227364Sadrian		    bf, SEQNO(bf->bf_state.bfs_seqno),
2585227364Sadrian		    tid->tx_buf[cindex],
2586227364Sadrian		    SEQNO(tid->tx_buf[cindex]->bf_state.bfs_seqno));
2587227364Sadrian	}
2588227364Sadrian
2589227364Sadrian	tid->tx_buf[cindex] = NULL;
2590227364Sadrian
2591229949Sadrian	while (tid->baw_head != tid->baw_tail &&
2592229949Sadrian	    !tid->tx_buf[tid->baw_head]) {
2593227364Sadrian		INCR(tap->txa_start, IEEE80211_SEQ_RANGE);
2594227364Sadrian		INCR(tid->baw_head, ATH_TID_MAX_BUFS);
2595227364Sadrian	}
2596229949Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2597229949Sadrian	    "%s: baw is now %d:%d, baw head=%d\n",
2598227364Sadrian	    __func__, tap->txa_start, tap->txa_wnd, tid->baw_head);
2599227364Sadrian}
2600227364Sadrian
2601227364Sadrian/*
2602227364Sadrian * Mark the current node/TID as ready to TX.
2603227364Sadrian *
2604227364Sadrian * This is done to make it easy for the software scheduler to
2605227364Sadrian * find which nodes have data to send.
2606227364Sadrian *
2607227364Sadrian * The TXQ lock must be held.
2608227364Sadrian */
2609227364Sadrianstatic void
2610227364Sadrianath_tx_tid_sched(struct ath_softc *sc, struct ath_tid *tid)
2611227364Sadrian{
2612227364Sadrian	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
2613227364Sadrian
2614243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
2615227364Sadrian
2616227364Sadrian	if (tid->paused)
2617227364Sadrian		return;		/* paused, can't schedule yet */
2618227364Sadrian
2619227364Sadrian	if (tid->sched)
2620227364Sadrian		return;		/* already scheduled */
2621227364Sadrian
2622227364Sadrian	tid->sched = 1;
2623227364Sadrian
2624227364Sadrian	TAILQ_INSERT_TAIL(&txq->axq_tidq, tid, axq_qelem);
2625227364Sadrian}
2626227364Sadrian
2627227364Sadrian/*
2628227364Sadrian * Mark the current node as no longer needing to be polled for
2629227364Sadrian * TX packets.
2630227364Sadrian *
2631227364Sadrian * The TXQ lock must be held.
2632227364Sadrian */
2633227364Sadrianstatic void
2634227364Sadrianath_tx_tid_unsched(struct ath_softc *sc, struct ath_tid *tid)
2635227364Sadrian{
2636227364Sadrian	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
2637227364Sadrian
2638243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
2639227364Sadrian
2640227364Sadrian	if (tid->sched == 0)
2641227364Sadrian		return;
2642227364Sadrian
2643227364Sadrian	tid->sched = 0;
2644227364Sadrian	TAILQ_REMOVE(&txq->axq_tidq, tid, axq_qelem);
2645227364Sadrian}
2646227364Sadrian
2647227364Sadrian/*
2648227364Sadrian * Assign a sequence number manually to the given frame.
2649227364Sadrian *
2650227364Sadrian * This should only be called for A-MPDU TX frames.
2651227364Sadrian */
2652236872Sadrianstatic ieee80211_seq
2653227364Sadrianath_tx_tid_seqno_assign(struct ath_softc *sc, struct ieee80211_node *ni,
2654227364Sadrian    struct ath_buf *bf, struct mbuf *m0)
2655227364Sadrian{
2656227364Sadrian	struct ieee80211_frame *wh;
2657227364Sadrian	int tid, pri;
2658227364Sadrian	ieee80211_seq seqno;
2659227364Sadrian	uint8_t subtype;
2660227364Sadrian
2661227364Sadrian	/* TID lookup */
2662227364Sadrian	wh = mtod(m0, struct ieee80211_frame *);
2663227364Sadrian	pri = M_WME_GETAC(m0);			/* honor classification */
2664227364Sadrian	tid = WME_AC_TO_TID(pri);
2665236872Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: pri=%d, tid=%d, qos has seq=%d\n",
2666236872Sadrian	    __func__, pri, tid, IEEE80211_QOS_HAS_SEQ(wh));
2667227364Sadrian
2668227364Sadrian	/* XXX Is it a control frame? Ignore */
2669227364Sadrian
2670227364Sadrian	/* Does the packet require a sequence number? */
2671227364Sadrian	if (! IEEE80211_QOS_HAS_SEQ(wh))
2672227364Sadrian		return -1;
2673227364Sadrian
2674243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
2675236880Sadrian
2676227364Sadrian	/*
2677227364Sadrian	 * Is it a QOS NULL Data frame? Give it a sequence number from
2678227364Sadrian	 * the default TID (IEEE80211_NONQOS_TID.)
2679227364Sadrian	 *
2680227364Sadrian	 * The RX path of everything I've looked at doesn't include the NULL
2681227364Sadrian	 * data frame sequence number in the aggregation state updates, so
2682227364Sadrian	 * assigning it a sequence number there will cause a BAW hole on the
2683227364Sadrian	 * RX side.
2684227364Sadrian	 */
2685227364Sadrian	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
2686227364Sadrian	if (subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL) {
2687236880Sadrian		/* XXX no locking for this TID? This is a bit of a problem. */
2688227364Sadrian		seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID];
2689227364Sadrian		INCR(ni->ni_txseqs[IEEE80211_NONQOS_TID], IEEE80211_SEQ_RANGE);
2690227364Sadrian	} else {
2691227364Sadrian		/* Manually assign sequence number */
2692227364Sadrian		seqno = ni->ni_txseqs[tid];
2693227364Sadrian		INCR(ni->ni_txseqs[tid], IEEE80211_SEQ_RANGE);
2694227364Sadrian	}
2695227364Sadrian	*(uint16_t *)&wh->i_seq[0] = htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
2696227364Sadrian	M_SEQNO_SET(m0, seqno);
2697227364Sadrian
2698227364Sadrian	/* Return so caller can do something with it if needed */
2699236872Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s:  -> seqno=%d\n", __func__, seqno);
2700227364Sadrian	return seqno;
2701227364Sadrian}
2702227364Sadrian
2703227364Sadrian/*
2704227364Sadrian * Attempt to direct dispatch an aggregate frame to hardware.
2705227364Sadrian * If the frame is out of BAW, queue.
2706227364Sadrian * Otherwise, schedule it as a single frame.
2707227364Sadrian */
2708227364Sadrianstatic void
2709239051Sadrianath_tx_xmit_aggr(struct ath_softc *sc, struct ath_node *an,
2710239051Sadrian    struct ath_txq *txq, struct ath_buf *bf)
2711227364Sadrian{
2712227364Sadrian	struct ath_tid *tid = &an->an_tid[bf->bf_state.bfs_tid];
2713227364Sadrian	struct ieee80211_tx_ampdu *tap;
2714227364Sadrian
2715243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
2716227364Sadrian
2717227364Sadrian	tap = ath_tx_get_tx_tid(an, tid->tid);
2718227364Sadrian
2719227364Sadrian	/* paused? queue */
2720227364Sadrian	if (tid->paused) {
2721241336Sadrian		ATH_TID_INSERT_HEAD(tid, bf, bf_list);
2722233480Sadrian		/* XXX don't sched - we're paused! */
2723227364Sadrian		return;
2724227364Sadrian	}
2725227364Sadrian
2726227364Sadrian	/* outside baw? queue */
2727227364Sadrian	if (bf->bf_state.bfs_dobaw &&
2728227364Sadrian	    (! BAW_WITHIN(tap->txa_start, tap->txa_wnd,
2729227364Sadrian	    SEQNO(bf->bf_state.bfs_seqno)))) {
2730241336Sadrian		ATH_TID_INSERT_HEAD(tid, bf, bf_list);
2731227364Sadrian		ath_tx_tid_sched(sc, tid);
2732227364Sadrian		return;
2733227364Sadrian	}
2734227364Sadrian
2735240180Sadrian	/*
2736240180Sadrian	 * This is a temporary check and should be removed once
2737240180Sadrian	 * all the relevant code paths have been fixed.
2738240180Sadrian	 *
2739240180Sadrian	 * During aggregate retries, it's possible that the head
2740240180Sadrian	 * frame will fail (which has the bfs_aggr and bfs_nframes
2741240180Sadrian	 * fields set for said aggregate) and will be retried as
2742240180Sadrian	 * a single frame.  In this instance, the values should
2743240180Sadrian	 * be reset or the completion code will get upset with you.
2744240180Sadrian	 */
2745240180Sadrian	if (bf->bf_state.bfs_aggr != 0 || bf->bf_state.bfs_nframes > 1) {
2746240180Sadrian		device_printf(sc->sc_dev, "%s: bfs_aggr=%d, bfs_nframes=%d\n",
2747240180Sadrian		    __func__,
2748240180Sadrian		    bf->bf_state.bfs_aggr,
2749240180Sadrian		    bf->bf_state.bfs_nframes);
2750240180Sadrian		bf->bf_state.bfs_aggr = 0;
2751240180Sadrian		bf->bf_state.bfs_nframes = 1;
2752240180Sadrian	}
2753240180Sadrian
2754240724Sadrian	/* Update CLRDMASK just before this frame is queued */
2755240724Sadrian	ath_tx_update_clrdmask(sc, tid, bf);
2756240724Sadrian
2757227364Sadrian	/* Direct dispatch to hardware */
2758227364Sadrian	ath_tx_do_ratelookup(sc, bf);
2759233989Sadrian	ath_tx_calc_duration(sc, bf);
2760233989Sadrian	ath_tx_calc_protection(sc, bf);
2761233989Sadrian	ath_tx_set_rtscts(sc, bf);
2762227364Sadrian	ath_tx_rate_fill_rcflags(sc, bf);
2763227364Sadrian	ath_tx_setds(sc, bf);
2764227364Sadrian
2765227364Sadrian	/* Statistics */
2766227364Sadrian	sc->sc_aggr_stats.aggr_low_hwq_single_pkt++;
2767227364Sadrian
2768227364Sadrian	/* Track per-TID hardware queue depth correctly */
2769227364Sadrian	tid->hwq_depth++;
2770227364Sadrian
2771227364Sadrian	/* Add to BAW */
2772227364Sadrian	if (bf->bf_state.bfs_dobaw) {
2773227364Sadrian		ath_tx_addto_baw(sc, an, tid, bf);
2774227364Sadrian		bf->bf_state.bfs_addedbaw = 1;
2775227364Sadrian	}
2776227364Sadrian
2777227364Sadrian	/* Set completion handler, multi-frame aggregate or not */
2778227364Sadrian	bf->bf_comp = ath_tx_aggr_comp;
2779227364Sadrian
2780227364Sadrian	/* Hand off to hardware */
2781227364Sadrian	ath_tx_handoff(sc, txq, bf);
2782227364Sadrian}
2783227364Sadrian
2784227364Sadrian/*
2785227364Sadrian * Attempt to send the packet.
2786227364Sadrian * If the queue isn't busy, direct-dispatch.
2787227364Sadrian * If the queue is busy enough, queue the given packet on the
2788227364Sadrian *  relevant software queue.
2789227364Sadrian */
2790227364Sadrianvoid
2791227364Sadrianath_tx_swq(struct ath_softc *sc, struct ieee80211_node *ni, struct ath_txq *txq,
2792227364Sadrian    struct ath_buf *bf)
2793227364Sadrian{
2794227364Sadrian	struct ath_node *an = ATH_NODE(ni);
2795227364Sadrian	struct ieee80211_frame *wh;
2796227364Sadrian	struct ath_tid *atid;
2797227364Sadrian	int pri, tid;
2798227364Sadrian	struct mbuf *m0 = bf->bf_m;
2799227364Sadrian
2800243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
2801236880Sadrian
2802227364Sadrian	/* Fetch the TID - non-QoS frames get assigned to TID 16 */
2803227364Sadrian	wh = mtod(m0, struct ieee80211_frame *);
2804227364Sadrian	pri = ath_tx_getac(sc, m0);
2805227364Sadrian	tid = ath_tx_gettid(sc, m0);
2806227364Sadrian	atid = &an->an_tid[tid];
2807227364Sadrian
2808236872Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bf=%p, pri=%d, tid=%d, qos=%d\n",
2809236872Sadrian	    __func__, bf, pri, tid, IEEE80211_QOS_HAS_SEQ(wh));
2810227364Sadrian
2811227364Sadrian	/* Set local packet state, used to queue packets to hardware */
2812239051Sadrian	/* XXX potentially duplicate info, re-check */
2813227364Sadrian	bf->bf_state.bfs_tid = tid;
2814244109Sadrian	bf->bf_state.bfs_tx_queue = txq->axq_qnum;
2815227364Sadrian	bf->bf_state.bfs_pri = pri;
2816227364Sadrian
2817227364Sadrian	/*
2818227364Sadrian	 * If the hardware queue isn't busy, queue it directly.
2819227364Sadrian	 * If the hardware queue is busy, queue it.
2820227364Sadrian	 * If the TID is paused or the traffic it outside BAW, software
2821227364Sadrian	 * queue it.
2822227364Sadrian	 */
2823227364Sadrian	if (atid->paused) {
2824227364Sadrian		/* TID is paused, queue */
2825236872Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: paused\n", __func__);
2826241336Sadrian		ATH_TID_INSERT_TAIL(atid, bf, bf_list);
2827227364Sadrian	} else if (ath_tx_ampdu_pending(sc, an, tid)) {
2828227364Sadrian		/* AMPDU pending; queue */
2829236872Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: pending\n", __func__);
2830241336Sadrian		ATH_TID_INSERT_TAIL(atid, bf, bf_list);
2831227364Sadrian		/* XXX sched? */
2832227364Sadrian	} else if (ath_tx_ampdu_running(sc, an, tid)) {
2833227364Sadrian		/* AMPDU running, attempt direct dispatch if possible */
2834236877Sadrian
2835236877Sadrian		/*
2836236877Sadrian		 * Always queue the frame to the tail of the list.
2837236877Sadrian		 */
2838241336Sadrian		ATH_TID_INSERT_TAIL(atid, bf, bf_list);
2839236877Sadrian
2840236877Sadrian		/*
2841236877Sadrian		 * If the hardware queue isn't busy, direct dispatch
2842236877Sadrian		 * the head frame in the list.  Don't schedule the
2843236877Sadrian		 * TID - let it build some more frames first?
2844236877Sadrian		 *
2845236877Sadrian		 * Otherwise, schedule the TID.
2846236877Sadrian		 */
2847229949Sadrian		if (txq->axq_depth < sc->sc_hwq_limit) {
2848241336Sadrian			bf = ATH_TID_FIRST(atid);
2849241336Sadrian			ATH_TID_REMOVE(atid, bf, bf_list);
2850240180Sadrian
2851240180Sadrian			/*
2852240180Sadrian			 * Ensure it's definitely treated as a non-AMPDU
2853240180Sadrian			 * frame - this information may have been left
2854240180Sadrian			 * over from a previous attempt.
2855240180Sadrian			 */
2856240180Sadrian			bf->bf_state.bfs_aggr = 0;
2857240180Sadrian			bf->bf_state.bfs_nframes = 1;
2858240180Sadrian
2859240180Sadrian			/* Queue to the hardware */
2860239051Sadrian			ath_tx_xmit_aggr(sc, an, txq, bf);
2861233227Sadrian			DPRINTF(sc, ATH_DEBUG_SW_TX,
2862236872Sadrian			    "%s: xmit_aggr\n",
2863236872Sadrian			    __func__);
2864229949Sadrian		} else {
2865229949Sadrian			DPRINTF(sc, ATH_DEBUG_SW_TX,
2866236872Sadrian			    "%s: ampdu; swq'ing\n",
2867236872Sadrian			    __func__);
2868240899Sadrian
2869227364Sadrian			ath_tx_tid_sched(sc, atid);
2870227364Sadrian		}
2871227364Sadrian	} else if (txq->axq_depth < sc->sc_hwq_limit) {
2872227364Sadrian		/* AMPDU not running, attempt direct dispatch */
2873236872Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: xmit_normal\n", __func__);
2874240926Sadrian		/* See if clrdmask needs to be set */
2875240926Sadrian		ath_tx_update_clrdmask(sc, atid, bf);
2876227364Sadrian		ath_tx_xmit_normal(sc, txq, bf);
2877227364Sadrian	} else {
2878227364Sadrian		/* Busy; queue */
2879236872Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: swq'ing\n", __func__);
2880241336Sadrian		ATH_TID_INSERT_TAIL(atid, bf, bf_list);
2881227364Sadrian		ath_tx_tid_sched(sc, atid);
2882227364Sadrian	}
2883227364Sadrian}
2884227364Sadrian
2885227364Sadrian/*
2886245708Sadrian * Only set the clrdmask bit if none of the nodes are currently
2887245708Sadrian * filtered.
2888245708Sadrian *
2889245708Sadrian * XXX TODO: go through all the callers and check to see
2890245708Sadrian * which are being called in the context of looping over all
2891245708Sadrian * TIDs (eg, if all tids are being paused, resumed, etc.)
2892245708Sadrian * That'll avoid O(n^2) complexity here.
2893245708Sadrian */
2894245708Sadrianstatic void
2895245708Sadrianath_tx_set_clrdmask(struct ath_softc *sc, struct ath_node *an)
2896245708Sadrian{
2897245708Sadrian	int i;
2898245708Sadrian
2899245708Sadrian	ATH_TX_LOCK_ASSERT(sc);
2900245708Sadrian
2901245708Sadrian	for (i = 0; i < IEEE80211_TID_SIZE; i++) {
2902245708Sadrian		if (an->an_tid[i].isfiltered == 1)
2903245739Sadrian			return;
2904245708Sadrian	}
2905245708Sadrian	an->clrdmask = 1;
2906245708Sadrian}
2907245708Sadrian
2908245708Sadrian/*
2909227364Sadrian * Configure the per-TID node state.
2910227364Sadrian *
2911227364Sadrian * This likely belongs in if_ath_node.c but I can't think of anywhere
2912227364Sadrian * else to put it just yet.
2913227364Sadrian *
2914227364Sadrian * This sets up the SLISTs and the mutex as appropriate.
2915227364Sadrian */
2916227364Sadrianvoid
2917227364Sadrianath_tx_tid_init(struct ath_softc *sc, struct ath_node *an)
2918227364Sadrian{
2919227364Sadrian	int i, j;
2920227364Sadrian	struct ath_tid *atid;
2921227364Sadrian
2922227364Sadrian	for (i = 0; i < IEEE80211_TID_SIZE; i++) {
2923227364Sadrian		atid = &an->an_tid[i];
2924240639Sadrian
2925240639Sadrian		/* XXX now with this bzer(), is the field 0'ing needed? */
2926240639Sadrian		bzero(atid, sizeof(*atid));
2927240639Sadrian
2928241336Sadrian		TAILQ_INIT(&atid->tid_q);
2929241336Sadrian		TAILQ_INIT(&atid->filtq.tid_q);
2930227364Sadrian		atid->tid = i;
2931227364Sadrian		atid->an = an;
2932227364Sadrian		for (j = 0; j < ATH_TID_MAX_BUFS; j++)
2933227364Sadrian			atid->tx_buf[j] = NULL;
2934227364Sadrian		atid->baw_head = atid->baw_tail = 0;
2935227364Sadrian		atid->paused = 0;
2936227364Sadrian		atid->sched = 0;
2937227364Sadrian		atid->hwq_depth = 0;
2938227364Sadrian		atid->cleanup_inprogress = 0;
2939227364Sadrian		if (i == IEEE80211_NONQOS_TID)
2940240946Sadrian			atid->ac = ATH_NONQOS_TID_AC;
2941227364Sadrian		else
2942227364Sadrian			atid->ac = TID_TO_WME_AC(i);
2943227364Sadrian	}
2944245708Sadrian	an->clrdmask = 1;	/* Always start by setting this bit */
2945227364Sadrian}
2946227364Sadrian
2947227364Sadrian/*
2948227364Sadrian * Pause the current TID. This stops packets from being transmitted
2949227364Sadrian * on it.
2950227364Sadrian *
2951227364Sadrian * Since this is also called from upper layers as well as the driver,
2952227364Sadrian * it will get the TID lock.
2953227364Sadrian */
2954227364Sadrianstatic void
2955227364Sadrianath_tx_tid_pause(struct ath_softc *sc, struct ath_tid *tid)
2956227364Sadrian{
2957233908Sadrian
2958243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
2959227364Sadrian	tid->paused++;
2960227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: paused = %d\n",
2961227364Sadrian	    __func__, tid->paused);
2962227364Sadrian}
2963227364Sadrian
2964227364Sadrian/*
2965227364Sadrian * Unpause the current TID, and schedule it if needed.
2966227364Sadrian */
2967227364Sadrianstatic void
2968227364Sadrianath_tx_tid_resume(struct ath_softc *sc, struct ath_tid *tid)
2969227364Sadrian{
2970243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
2971243786Sadrian
2972227364Sadrian	tid->paused--;
2973227364Sadrian
2974227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: unpaused = %d\n",
2975227364Sadrian	    __func__, tid->paused);
2976227364Sadrian
2977241170Sadrian	if (tid->paused)
2978227364Sadrian		return;
2979227364Sadrian
2980241170Sadrian	/*
2981241170Sadrian	 * Override the clrdmask configuration for the next frame
2982241170Sadrian	 * from this TID, just to get the ball rolling.
2983241170Sadrian	 */
2984245708Sadrian	ath_tx_set_clrdmask(sc, tid->an);
2985241170Sadrian
2986241170Sadrian	if (tid->axq_depth == 0)
2987241170Sadrian		return;
2988241170Sadrian
2989240639Sadrian	/* XXX isfiltered shouldn't ever be 0 at this point */
2990240639Sadrian	if (tid->isfiltered == 1) {
2991240639Sadrian		device_printf(sc->sc_dev, "%s: filtered?!\n", __func__);
2992240639Sadrian		return;
2993240639Sadrian	}
2994240639Sadrian
2995227364Sadrian	ath_tx_tid_sched(sc, tid);
2996246450Sadrian
2997246450Sadrian	/*
2998246450Sadrian	 * Queue the software TX scheduler.
2999246450Sadrian	 */
3000246450Sadrian	ath_tx_swq_kick(sc);
3001227364Sadrian}
3002227364Sadrian
3003227364Sadrian/*
3004240639Sadrian * Add the given ath_buf to the TID filtered frame list.
3005240639Sadrian * This requires the TID be filtered.
3006240639Sadrian */
3007240639Sadrianstatic void
3008240639Sadrianath_tx_tid_filt_addbuf(struct ath_softc *sc, struct ath_tid *tid,
3009240639Sadrian    struct ath_buf *bf)
3010240639Sadrian{
3011240639Sadrian
3012243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
3013243786Sadrian
3014240639Sadrian	if (! tid->isfiltered)
3015240639Sadrian		device_printf(sc->sc_dev, "%s: not filtered?!\n", __func__);
3016240639Sadrian
3017240639Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_FILT, "%s: bf=%p\n", __func__, bf);
3018240639Sadrian
3019240639Sadrian	/* Set the retry bit and bump the retry counter */
3020240639Sadrian	ath_tx_set_retry(sc, bf);
3021240639Sadrian	sc->sc_stats.ast_tx_swfiltered++;
3022240639Sadrian
3023241566Sadrian	ATH_TID_FILT_INSERT_TAIL(tid, bf, bf_list);
3024240639Sadrian}
3025240639Sadrian
3026240639Sadrian/*
3027240639Sadrian * Handle a completed filtered frame from the given TID.
3028240639Sadrian * This just enables/pauses the filtered frame state if required
3029240639Sadrian * and appends the filtered frame to the filtered queue.
3030240639Sadrian */
3031240639Sadrianstatic void
3032240639Sadrianath_tx_tid_filt_comp_buf(struct ath_softc *sc, struct ath_tid *tid,
3033240639Sadrian    struct ath_buf *bf)
3034240639Sadrian{
3035240639Sadrian
3036243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
3037240639Sadrian
3038240639Sadrian	if (! tid->isfiltered) {
3039240639Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT, "%s: filter transition\n",
3040240639Sadrian		    __func__);
3041240639Sadrian		tid->isfiltered = 1;
3042240639Sadrian		ath_tx_tid_pause(sc, tid);
3043240639Sadrian	}
3044240639Sadrian
3045240639Sadrian	/* Add the frame to the filter queue */
3046240639Sadrian	ath_tx_tid_filt_addbuf(sc, tid, bf);
3047240639Sadrian}
3048240639Sadrian
3049240639Sadrian/*
3050240639Sadrian * Complete the filtered frame TX completion.
3051240639Sadrian *
3052240639Sadrian * If there are no more frames in the hardware queue, unpause/unfilter
3053240639Sadrian * the TID if applicable.  Otherwise we will wait for a node PS transition
3054240639Sadrian * to unfilter.
3055240639Sadrian */
3056240639Sadrianstatic void
3057240639Sadrianath_tx_tid_filt_comp_complete(struct ath_softc *sc, struct ath_tid *tid)
3058240639Sadrian{
3059240639Sadrian	struct ath_buf *bf;
3060240639Sadrian
3061243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
3062240639Sadrian
3063240639Sadrian	if (tid->hwq_depth != 0)
3064240639Sadrian		return;
3065240639Sadrian
3066240639Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_FILT, "%s: hwq=0, transition back\n",
3067240639Sadrian	    __func__);
3068240639Sadrian	tid->isfiltered = 0;
3069245708Sadrian	/* XXX ath_tx_tid_resume() also calls ath_tx_set_clrdmask()! */
3070245708Sadrian	ath_tx_set_clrdmask(sc, tid->an);
3071240639Sadrian
3072240639Sadrian	/* XXX this is really quite inefficient */
3073241566Sadrian	while ((bf = ATH_TID_FILT_LAST(tid, ath_bufhead_s)) != NULL) {
3074241566Sadrian		ATH_TID_FILT_REMOVE(tid, bf, bf_list);
3075241336Sadrian		ATH_TID_INSERT_HEAD(tid, bf, bf_list);
3076240639Sadrian	}
3077240639Sadrian
3078240639Sadrian	ath_tx_tid_resume(sc, tid);
3079240639Sadrian}
3080240639Sadrian
3081240639Sadrian/*
3082240639Sadrian * Called when a single (aggregate or otherwise) frame is completed.
3083240639Sadrian *
3084240639Sadrian * Returns 1 if the buffer could be added to the filtered list
3085240639Sadrian * (cloned or otherwise), 0 if the buffer couldn't be added to the
3086240639Sadrian * filtered list (failed clone; expired retry) and the caller should
3087240639Sadrian * free it and handle it like a failure (eg by sending a BAR.)
3088240639Sadrian */
3089240639Sadrianstatic int
3090240639Sadrianath_tx_tid_filt_comp_single(struct ath_softc *sc, struct ath_tid *tid,
3091240639Sadrian    struct ath_buf *bf)
3092240639Sadrian{
3093240639Sadrian	struct ath_buf *nbf;
3094240639Sadrian	int retval;
3095240639Sadrian
3096243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
3097240639Sadrian
3098240639Sadrian	/*
3099240639Sadrian	 * Don't allow a filtered frame to live forever.
3100240639Sadrian	 */
3101240639Sadrian	if (bf->bf_state.bfs_retries > SWMAX_RETRIES) {
3102241170Sadrian		sc->sc_stats.ast_tx_swretrymax++;
3103240639Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3104240639Sadrian		    "%s: bf=%p, seqno=%d, exceeded retries\n",
3105240639Sadrian		    __func__,
3106240639Sadrian		    bf,
3107240639Sadrian		    bf->bf_state.bfs_seqno);
3108240639Sadrian		return (0);
3109240639Sadrian	}
3110240639Sadrian
3111240639Sadrian	/*
3112240639Sadrian	 * A busy buffer can't be added to the retry list.
3113240639Sadrian	 * It needs to be cloned.
3114240639Sadrian	 */
3115240639Sadrian	if (bf->bf_flags & ATH_BUF_BUSY) {
3116240639Sadrian		nbf = ath_tx_retry_clone(sc, tid->an, tid, bf);
3117240639Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3118240639Sadrian		    "%s: busy buffer clone: %p -> %p\n",
3119240639Sadrian		    __func__, bf, nbf);
3120240639Sadrian	} else {
3121240639Sadrian		nbf = bf;
3122240639Sadrian	}
3123240639Sadrian
3124240639Sadrian	if (nbf == NULL) {
3125240639Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3126240639Sadrian		    "%s: busy buffer couldn't be cloned (%p)!\n",
3127240639Sadrian		    __func__, bf);
3128240639Sadrian		retval = 1;
3129240639Sadrian	} else {
3130240639Sadrian		ath_tx_tid_filt_comp_buf(sc, tid, nbf);
3131240639Sadrian		retval = 0;
3132240639Sadrian	}
3133240639Sadrian	ath_tx_tid_filt_comp_complete(sc, tid);
3134240639Sadrian
3135240639Sadrian	return (retval);
3136240639Sadrian}
3137240639Sadrian
3138240639Sadrianstatic void
3139240639Sadrianath_tx_tid_filt_comp_aggr(struct ath_softc *sc, struct ath_tid *tid,
3140240639Sadrian    struct ath_buf *bf_first, ath_bufhead *bf_q)
3141240639Sadrian{
3142240639Sadrian	struct ath_buf *bf, *bf_next, *nbf;
3143240639Sadrian
3144243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
3145240639Sadrian
3146240639Sadrian	bf = bf_first;
3147240639Sadrian	while (bf) {
3148240639Sadrian		bf_next = bf->bf_next;
3149240639Sadrian		bf->bf_next = NULL;	/* Remove it from the aggr list */
3150240639Sadrian
3151240639Sadrian		/*
3152240639Sadrian		 * Don't allow a filtered frame to live forever.
3153240639Sadrian		 */
3154240639Sadrian		if (bf->bf_state.bfs_retries > SWMAX_RETRIES) {
3155247029Sadrian			sc->sc_stats.ast_tx_swretrymax++;
3156240639Sadrian			DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3157240639Sadrian			    "%s: bf=%p, seqno=%d, exceeded retries\n",
3158240639Sadrian			    __func__,
3159240639Sadrian			    bf,
3160240639Sadrian			    bf->bf_state.bfs_seqno);
3161240639Sadrian			TAILQ_INSERT_TAIL(bf_q, bf, bf_list);
3162240639Sadrian			goto next;
3163240639Sadrian		}
3164240639Sadrian
3165240639Sadrian		if (bf->bf_flags & ATH_BUF_BUSY) {
3166240639Sadrian			nbf = ath_tx_retry_clone(sc, tid->an, tid, bf);
3167240639Sadrian			DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3168240639Sadrian			    "%s: busy buffer cloned: %p -> %p",
3169240639Sadrian			    __func__, bf, nbf);
3170240639Sadrian		} else {
3171240639Sadrian			nbf = bf;
3172240639Sadrian		}
3173240639Sadrian
3174240639Sadrian		/*
3175240639Sadrian		 * If the buffer couldn't be cloned, add it to bf_q;
3176240639Sadrian		 * the caller will free the buffer(s) as required.
3177240639Sadrian		 */
3178240639Sadrian		if (nbf == NULL) {
3179240639Sadrian			DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3180240639Sadrian			    "%s: buffer couldn't be cloned! (%p)\n",
3181240639Sadrian			    __func__, bf);
3182240639Sadrian			TAILQ_INSERT_TAIL(bf_q, bf, bf_list);
3183240639Sadrian		} else {
3184240639Sadrian			ath_tx_tid_filt_comp_buf(sc, tid, nbf);
3185240639Sadrian		}
3186240639Sadriannext:
3187240639Sadrian		bf = bf_next;
3188240639Sadrian	}
3189240639Sadrian
3190240639Sadrian	ath_tx_tid_filt_comp_complete(sc, tid);
3191240639Sadrian}
3192240639Sadrian
3193240639Sadrian/*
3194233908Sadrian * Suspend the queue because we need to TX a BAR.
3195233908Sadrian */
3196233908Sadrianstatic void
3197233908Sadrianath_tx_tid_bar_suspend(struct ath_softc *sc, struct ath_tid *tid)
3198233908Sadrian{
3199233908Sadrian
3200243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
3201243786Sadrian
3202235491Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
3203235676Sadrian	    "%s: tid=%p, bar_wait=%d, bar_tx=%d, called\n",
3204233908Sadrian	    __func__,
3205235676Sadrian	    tid,
3206235676Sadrian	    tid->bar_wait,
3207235676Sadrian	    tid->bar_tx);
3208233908Sadrian
3209233908Sadrian	/* We shouldn't be called when bar_tx is 1 */
3210233908Sadrian	if (tid->bar_tx) {
3211233908Sadrian		device_printf(sc->sc_dev, "%s: bar_tx is 1?!\n",
3212233908Sadrian		    __func__);
3213233908Sadrian	}
3214233908Sadrian
3215233908Sadrian	/* If we've already been called, just be patient. */
3216233908Sadrian	if (tid->bar_wait)
3217233908Sadrian		return;
3218233908Sadrian
3219233908Sadrian	/* Wait! */
3220233908Sadrian	tid->bar_wait = 1;
3221233908Sadrian
3222233908Sadrian	/* Only one pause, no matter how many frames fail */
3223233908Sadrian	ath_tx_tid_pause(sc, tid);
3224233908Sadrian}
3225233908Sadrian
3226233908Sadrian/*
3227233908Sadrian * We've finished with BAR handling - either we succeeded or
3228233908Sadrian * failed. Either way, unsuspend TX.
3229233908Sadrian */
3230233908Sadrianstatic void
3231233908Sadrianath_tx_tid_bar_unsuspend(struct ath_softc *sc, struct ath_tid *tid)
3232233908Sadrian{
3233233908Sadrian
3234243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
3235243786Sadrian
3236235491Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
3237233908Sadrian	    "%s: tid=%p, called\n",
3238233908Sadrian	    __func__,
3239233908Sadrian	    tid);
3240233908Sadrian
3241233908Sadrian	if (tid->bar_tx == 0 || tid->bar_wait == 0) {
3242233908Sadrian		device_printf(sc->sc_dev, "%s: bar_tx=%d, bar_wait=%d: ?\n",
3243233908Sadrian		    __func__, tid->bar_tx, tid->bar_wait);
3244233908Sadrian	}
3245233908Sadrian
3246233908Sadrian	tid->bar_tx = tid->bar_wait = 0;
3247233908Sadrian	ath_tx_tid_resume(sc, tid);
3248233908Sadrian}
3249233908Sadrian
3250233908Sadrian/*
3251233908Sadrian * Return whether we're ready to TX a BAR frame.
3252233908Sadrian *
3253233908Sadrian * Requires the TID lock be held.
3254233908Sadrian */
3255233908Sadrianstatic int
3256233908Sadrianath_tx_tid_bar_tx_ready(struct ath_softc *sc, struct ath_tid *tid)
3257233908Sadrian{
3258233908Sadrian
3259243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
3260233908Sadrian
3261233908Sadrian	if (tid->bar_wait == 0 || tid->hwq_depth > 0)
3262233908Sadrian		return (0);
3263233908Sadrian
3264235491Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR, "%s: tid=%p (%d), bar ready\n",
3265235491Sadrian	    __func__, tid, tid->tid);
3266235491Sadrian
3267233908Sadrian	return (1);
3268233908Sadrian}
3269233908Sadrian
3270233908Sadrian/*
3271233908Sadrian * Check whether the current TID is ready to have a BAR
3272233908Sadrian * TXed and if so, do the TX.
3273233908Sadrian *
3274233908Sadrian * Since the TID/TXQ lock can't be held during a call to
3275233908Sadrian * ieee80211_send_bar(), we have to do the dirty thing of unlocking it,
3276233908Sadrian * sending the BAR and locking it again.
3277233908Sadrian *
3278233908Sadrian * Eventually, the code to send the BAR should be broken out
3279233908Sadrian * from this routine so the lock doesn't have to be reacquired
3280233908Sadrian * just to be immediately dropped by the caller.
3281233908Sadrian */
3282233908Sadrianstatic void
3283233908Sadrianath_tx_tid_bar_tx(struct ath_softc *sc, struct ath_tid *tid)
3284233908Sadrian{
3285233908Sadrian	struct ieee80211_tx_ampdu *tap;
3286233908Sadrian
3287243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
3288233908Sadrian
3289235491Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
3290233908Sadrian	    "%s: tid=%p, called\n",
3291233908Sadrian	    __func__,
3292233908Sadrian	    tid);
3293233908Sadrian
3294233908Sadrian	tap = ath_tx_get_tx_tid(tid->an, tid->tid);
3295233908Sadrian
3296233908Sadrian	/*
3297233908Sadrian	 * This is an error condition!
3298233908Sadrian	 */
3299233908Sadrian	if (tid->bar_wait == 0 || tid->bar_tx == 1) {
3300233908Sadrian		device_printf(sc->sc_dev,
3301233908Sadrian		    "%s: tid=%p, bar_tx=%d, bar_wait=%d: ?\n",
3302233908Sadrian		    __func__,
3303233908Sadrian		    tid,
3304233908Sadrian		    tid->bar_tx,
3305233908Sadrian		    tid->bar_wait);
3306233908Sadrian		return;
3307233908Sadrian	}
3308233908Sadrian
3309233908Sadrian	/* Don't do anything if we still have pending frames */
3310233908Sadrian	if (tid->hwq_depth > 0) {
3311235491Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
3312233908Sadrian		    "%s: tid=%p, hwq_depth=%d, waiting\n",
3313233908Sadrian		    __func__,
3314233908Sadrian		    tid,
3315233908Sadrian		    tid->hwq_depth);
3316233908Sadrian		return;
3317233908Sadrian	}
3318233908Sadrian
3319233908Sadrian	/* We're now about to TX */
3320233908Sadrian	tid->bar_tx = 1;
3321233908Sadrian
3322233908Sadrian	/*
3323240724Sadrian	 * Override the clrdmask configuration for the next frame,
3324240724Sadrian	 * just to get the ball rolling.
3325240724Sadrian	 */
3326245708Sadrian	ath_tx_set_clrdmask(sc, tid->an);
3327240724Sadrian
3328240724Sadrian	/*
3329233908Sadrian	 * Calculate new BAW left edge, now that all frames have either
3330233908Sadrian	 * succeeded or failed.
3331233908Sadrian	 *
3332233908Sadrian	 * XXX verify this is _actually_ the valid value to begin at!
3333233908Sadrian	 */
3334235491Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
3335233908Sadrian	    "%s: tid=%p, new BAW left edge=%d\n",
3336233908Sadrian	    __func__,
3337233908Sadrian	    tid,
3338233908Sadrian	    tap->txa_start);
3339233908Sadrian
3340233908Sadrian	/* Try sending the BAR frame */
3341233908Sadrian	/* We can't hold the lock here! */
3342233908Sadrian
3343243786Sadrian	ATH_TX_UNLOCK(sc);
3344233908Sadrian	if (ieee80211_send_bar(&tid->an->an_node, tap, tap->txa_start) == 0) {
3345233908Sadrian		/* Success? Now we wait for notification that it's done */
3346243786Sadrian		ATH_TX_LOCK(sc);
3347233908Sadrian		return;
3348233908Sadrian	}
3349233908Sadrian
3350233908Sadrian	/* Failure? For now, warn loudly and continue */
3351243786Sadrian	ATH_TX_LOCK(sc);
3352233908Sadrian	device_printf(sc->sc_dev, "%s: tid=%p, failed to TX BAR, continue!\n",
3353233908Sadrian	    __func__, tid);
3354233908Sadrian	ath_tx_tid_bar_unsuspend(sc, tid);
3355233908Sadrian}
3356233908Sadrian
3357240639Sadrianstatic void
3358240639Sadrianath_tx_tid_drain_pkt(struct ath_softc *sc, struct ath_node *an,
3359240639Sadrian    struct ath_tid *tid, ath_bufhead *bf_cq, struct ath_buf *bf)
3360240639Sadrian{
3361233908Sadrian
3362243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
3363240639Sadrian
3364240639Sadrian	/*
3365240639Sadrian	 * If the current TID is running AMPDU, update
3366240639Sadrian	 * the BAW.
3367240639Sadrian	 */
3368240639Sadrian	if (ath_tx_ampdu_running(sc, an, tid->tid) &&
3369240639Sadrian	    bf->bf_state.bfs_dobaw) {
3370240639Sadrian		/*
3371240639Sadrian		 * Only remove the frame from the BAW if it's
3372240639Sadrian		 * been transmitted at least once; this means
3373240639Sadrian		 * the frame was in the BAW to begin with.
3374240639Sadrian		 */
3375240639Sadrian		if (bf->bf_state.bfs_retries > 0) {
3376240639Sadrian			ath_tx_update_baw(sc, an, tid, bf);
3377240639Sadrian			bf->bf_state.bfs_dobaw = 0;
3378240639Sadrian		}
3379247135Sadrian#if 0
3380240639Sadrian		/*
3381240639Sadrian		 * This has become a non-fatal error now
3382240639Sadrian		 */
3383240639Sadrian		if (! bf->bf_state.bfs_addedbaw)
3384240639Sadrian			device_printf(sc->sc_dev,
3385240639Sadrian			    "%s: wasn't added: seqno %d\n",
3386240639Sadrian			    __func__, SEQNO(bf->bf_state.bfs_seqno));
3387247135Sadrian#endif
3388240639Sadrian	}
3389248671Sadrian
3390248671Sadrian	/* Strip it out of an aggregate list if it was in one */
3391248671Sadrian	bf->bf_next = NULL;
3392248671Sadrian
3393248671Sadrian	/* Insert on the free queue to be freed by the caller */
3394240639Sadrian	TAILQ_INSERT_TAIL(bf_cq, bf, bf_list);
3395240639Sadrian}
3396240639Sadrian
3397240639Sadrianstatic void
3398240639Sadrianath_tx_tid_drain_print(struct ath_softc *sc, struct ath_node *an,
3399240899Sadrian    const char *pfx, struct ath_tid *tid, struct ath_buf *bf)
3400240639Sadrian{
3401240639Sadrian	struct ieee80211_node *ni = &an->an_node;
3402240639Sadrian	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
3403240639Sadrian	struct ieee80211_tx_ampdu *tap;
3404240639Sadrian
3405240639Sadrian	tap = ath_tx_get_tx_tid(an, tid->tid);
3406240639Sadrian
3407240639Sadrian	device_printf(sc->sc_dev,
3408240899Sadrian	    "%s: %s: node %p: bf=%p: addbaw=%d, dobaw=%d, "
3409240639Sadrian	    "seqno=%d, retry=%d\n",
3410240899Sadrian	    __func__, pfx, ni, bf,
3411240639Sadrian	    bf->bf_state.bfs_addedbaw,
3412240639Sadrian	    bf->bf_state.bfs_dobaw,
3413240639Sadrian	    SEQNO(bf->bf_state.bfs_seqno),
3414240639Sadrian	    bf->bf_state.bfs_retries);
3415240639Sadrian	device_printf(sc->sc_dev,
3416240899Sadrian	    "%s: node %p: bf=%p: txq[%d] axq_depth=%d, axq_aggr_depth=%d\n",
3417240724Sadrian	        __func__, ni, bf,
3418240899Sadrian	    txq->axq_qnum,
3419240899Sadrian	    txq->axq_depth,
3420240899Sadrian	    txq->axq_aggr_depth);
3421240724Sadrian
3422240724Sadrian	device_printf(sc->sc_dev,
3423240639Sadrian	    "%s: node %p: bf=%p: tid txq_depth=%d hwq_depth=%d, bar_wait=%d, isfiltered=%d\n",
3424240639Sadrian	    __func__, ni, bf,
3425240639Sadrian	    tid->axq_depth,
3426240639Sadrian	    tid->hwq_depth,
3427240639Sadrian	    tid->bar_wait,
3428240639Sadrian	    tid->isfiltered);
3429240639Sadrian	device_printf(sc->sc_dev,
3430240724Sadrian	    "%s: node %p: tid %d: "
3431240724Sadrian	    "sched=%d, paused=%d, "
3432240724Sadrian	    "incomp=%d, baw_head=%d, "
3433240639Sadrian	    "baw_tail=%d txa_start=%d, ni_txseqs=%d\n",
3434240724Sadrian	     __func__, ni, tid->tid,
3435240724Sadrian	     tid->sched, tid->paused,
3436240724Sadrian	     tid->incomp, tid->baw_head,
3437240639Sadrian	     tid->baw_tail, tap == NULL ? -1 : tap->txa_start,
3438240639Sadrian	     ni->ni_txseqs[tid->tid]);
3439240639Sadrian
3440240639Sadrian	/* XXX Dump the frame, see what it is? */
3441240639Sadrian	ieee80211_dump_pkt(ni->ni_ic,
3442240639Sadrian	    mtod(bf->bf_m, const uint8_t *),
3443240639Sadrian	    bf->bf_m->m_len, 0, -1);
3444240639Sadrian}
3445240639Sadrian
3446233908Sadrian/*
3447227364Sadrian * Free any packets currently pending in the software TX queue.
3448227364Sadrian *
3449227364Sadrian * This will be called when a node is being deleted.
3450227364Sadrian *
3451227364Sadrian * It can also be called on an active node during an interface
3452227364Sadrian * reset or state transition.
3453227364Sadrian *
3454227364Sadrian * (From Linux/reference):
3455227364Sadrian *
3456227364Sadrian * TODO: For frame(s) that are in the retry state, we will reuse the
3457227364Sadrian * sequence number(s) without setting the retry bit. The
3458227364Sadrian * alternative is to give up on these and BAR the receiver's window
3459227364Sadrian * forward.
3460227364Sadrian */
3461227364Sadrianstatic void
3462229949Sadrianath_tx_tid_drain(struct ath_softc *sc, struct ath_node *an,
3463229949Sadrian    struct ath_tid *tid, ath_bufhead *bf_cq)
3464227364Sadrian{
3465227364Sadrian	struct ath_buf *bf;
3466227364Sadrian	struct ieee80211_tx_ampdu *tap;
3467227364Sadrian	struct ieee80211_node *ni = &an->an_node;
3468240639Sadrian	int t;
3469227364Sadrian
3470227364Sadrian	tap = ath_tx_get_tx_tid(an, tid->tid);
3471227364Sadrian
3472243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
3473227364Sadrian
3474227364Sadrian	/* Walk the queue, free frames */
3475240639Sadrian	t = 0;
3476227364Sadrian	for (;;) {
3477241336Sadrian		bf = ATH_TID_FIRST(tid);
3478227364Sadrian		if (bf == NULL) {
3479227364Sadrian			break;
3480227364Sadrian		}
3481227364Sadrian
3482227364Sadrian		if (t == 0) {
3483240899Sadrian			ath_tx_tid_drain_print(sc, an, "norm", tid, bf);
3484240639Sadrian			t = 1;
3485240639Sadrian		}
3486229165Sadrian
3487241336Sadrian		ATH_TID_REMOVE(tid, bf, bf_list);
3488240639Sadrian		ath_tx_tid_drain_pkt(sc, an, tid, bf_cq, bf);
3489240639Sadrian	}
3490229165Sadrian
3491240639Sadrian	/* And now, drain the filtered frame queue */
3492240639Sadrian	t = 0;
3493240639Sadrian	for (;;) {
3494241566Sadrian		bf = ATH_TID_FILT_FIRST(tid);
3495240639Sadrian		if (bf == NULL)
3496240639Sadrian			break;
3497240639Sadrian
3498240639Sadrian		if (t == 0) {
3499240899Sadrian			ath_tx_tid_drain_print(sc, an, "filt", tid, bf);
3500233897Sadrian			t = 1;
3501227364Sadrian		}
3502227364Sadrian
3503241566Sadrian		ATH_TID_FILT_REMOVE(tid, bf, bf_list);
3504240639Sadrian		ath_tx_tid_drain_pkt(sc, an, tid, bf_cq, bf);
3505227364Sadrian	}
3506227364Sadrian
3507227364Sadrian	/*
3508240724Sadrian	 * Override the clrdmask configuration for the next frame
3509240724Sadrian	 * in case there is some future transmission, just to get
3510240724Sadrian	 * the ball rolling.
3511240724Sadrian	 *
3512240724Sadrian	 * This won't hurt things if the TID is about to be freed.
3513240724Sadrian	 */
3514245708Sadrian	ath_tx_set_clrdmask(sc, tid->an);
3515240724Sadrian
3516240724Sadrian	/*
3517227364Sadrian	 * Now that it's completed, grab the TID lock and update
3518227364Sadrian	 * the sequence number and BAW window.
3519227364Sadrian	 * Because sequence numbers have been assigned to frames
3520227364Sadrian	 * that haven't been sent yet, it's entirely possible
3521227364Sadrian	 * we'll be called with some pending frames that have not
3522227364Sadrian	 * been transmitted.
3523227364Sadrian	 *
3524227364Sadrian	 * The cleaner solution is to do the sequence number allocation
3525227364Sadrian	 * when the packet is first transmitted - and thus the "retries"
3526227364Sadrian	 * check above would be enough to update the BAW/seqno.
3527227364Sadrian	 */
3528227364Sadrian
3529227364Sadrian	/* But don't do it for non-QoS TIDs */
3530227364Sadrian	if (tap) {
3531227364Sadrian#if 0
3532227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
3533227364Sadrian		    "%s: node %p: TID %d: sliding BAW left edge to %d\n",
3534227364Sadrian		    __func__, an, tid->tid, tap->txa_start);
3535227364Sadrian#endif
3536227364Sadrian		ni->ni_txseqs[tid->tid] = tap->txa_start;
3537227364Sadrian		tid->baw_tail = tid->baw_head;
3538227364Sadrian	}
3539227364Sadrian}
3540227364Sadrian
3541227364Sadrian/*
3542227364Sadrian * Flush all software queued packets for the given node.
3543227364Sadrian *
3544227364Sadrian * This occurs when a completion handler frees the last buffer
3545227364Sadrian * for a node, and the node is thus freed. This causes the node
3546227364Sadrian * to be cleaned up, which ends up calling ath_tx_node_flush.
3547227364Sadrian */
3548227364Sadrianvoid
3549227364Sadrianath_tx_node_flush(struct ath_softc *sc, struct ath_node *an)
3550227364Sadrian{
3551227364Sadrian	int tid;
3552227364Sadrian	ath_bufhead bf_cq;
3553227364Sadrian	struct ath_buf *bf;
3554227364Sadrian
3555227364Sadrian	TAILQ_INIT(&bf_cq);
3556227364Sadrian
3557240899Sadrian	ATH_KTR(sc, ATH_KTR_NODE, 1, "ath_tx_node_flush: flush node; ni=%p",
3558240899Sadrian	    &an->an_node);
3559240899Sadrian
3560243786Sadrian	ATH_TX_LOCK(sc);
3561227364Sadrian	for (tid = 0; tid < IEEE80211_TID_SIZE; tid++) {
3562227364Sadrian		struct ath_tid *atid = &an->an_tid[tid];
3563227364Sadrian
3564227364Sadrian		/* Free packets */
3565227364Sadrian		ath_tx_tid_drain(sc, an, atid, &bf_cq);
3566240914Sadrian		/* Remove this tid from the list of active tids */
3567240914Sadrian		ath_tx_tid_unsched(sc, atid);
3568227364Sadrian	}
3569243786Sadrian	ATH_TX_UNLOCK(sc);
3570227364Sadrian
3571227364Sadrian	/* Handle completed frames */
3572227364Sadrian	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
3573227364Sadrian		TAILQ_REMOVE(&bf_cq, bf, bf_list);
3574227364Sadrian		ath_tx_default_comp(sc, bf, 0);
3575227364Sadrian	}
3576227364Sadrian}
3577227364Sadrian
3578227364Sadrian/*
3579227364Sadrian * Drain all the software TXQs currently with traffic queued.
3580227364Sadrian */
3581227364Sadrianvoid
3582227364Sadrianath_tx_txq_drain(struct ath_softc *sc, struct ath_txq *txq)
3583227364Sadrian{
3584227364Sadrian	struct ath_tid *tid;
3585227364Sadrian	ath_bufhead bf_cq;
3586227364Sadrian	struct ath_buf *bf;
3587227364Sadrian
3588227364Sadrian	TAILQ_INIT(&bf_cq);
3589243786Sadrian	ATH_TX_LOCK(sc);
3590227364Sadrian
3591227364Sadrian	/*
3592227364Sadrian	 * Iterate over all active tids for the given txq,
3593227364Sadrian	 * flushing and unsched'ing them
3594227364Sadrian	 */
3595227364Sadrian	while (! TAILQ_EMPTY(&txq->axq_tidq)) {
3596227364Sadrian		tid = TAILQ_FIRST(&txq->axq_tidq);
3597227364Sadrian		ath_tx_tid_drain(sc, tid->an, tid, &bf_cq);
3598227364Sadrian		ath_tx_tid_unsched(sc, tid);
3599227364Sadrian	}
3600227364Sadrian
3601243786Sadrian	ATH_TX_UNLOCK(sc);
3602227364Sadrian
3603227364Sadrian	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
3604227364Sadrian		TAILQ_REMOVE(&bf_cq, bf, bf_list);
3605227364Sadrian		ath_tx_default_comp(sc, bf, 0);
3606227364Sadrian	}
3607227364Sadrian}
3608227364Sadrian
3609227364Sadrian/*
3610227364Sadrian * Handle completion of non-aggregate session frames.
3611240883Sadrian *
3612240883Sadrian * This (currently) doesn't implement software retransmission of
3613240883Sadrian * non-aggregate frames!
3614240883Sadrian *
3615240883Sadrian * Software retransmission of non-aggregate frames needs to obey
3616240883Sadrian * the strict sequence number ordering, and drop any frames that
3617240883Sadrian * will fail this.
3618240883Sadrian *
3619240883Sadrian * For now, filtered frames and frame transmission will cause
3620240883Sadrian * all kinds of issues.  So we don't support them.
3621240883Sadrian *
3622240883Sadrian * So anyone queuing frames via ath_tx_normal_xmit() or
3623240883Sadrian * ath_tx_hw_queue_norm() must override and set CLRDMASK.
3624227364Sadrian */
3625227364Sadrianvoid
3626227364Sadrianath_tx_normal_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
3627227364Sadrian{
3628227364Sadrian	struct ieee80211_node *ni = bf->bf_node;
3629227364Sadrian	struct ath_node *an = ATH_NODE(ni);
3630227364Sadrian	int tid = bf->bf_state.bfs_tid;
3631227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
3632227364Sadrian	struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
3633227364Sadrian
3634227364Sadrian	/* The TID state is protected behind the TXQ lock */
3635243786Sadrian	ATH_TX_LOCK(sc);
3636227364Sadrian
3637227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bf=%p: fail=%d, hwq_depth now %d\n",
3638227364Sadrian	    __func__, bf, fail, atid->hwq_depth - 1);
3639227364Sadrian
3640227364Sadrian	atid->hwq_depth--;
3641240639Sadrian
3642240883Sadrian#if 0
3643240883Sadrian	/*
3644240883Sadrian	 * If the frame was filtered, stick it on the filter frame
3645240883Sadrian	 * queue and complain about it.  It shouldn't happen!
3646240883Sadrian	 */
3647240883Sadrian	if ((ts->ts_status & HAL_TXERR_FILT) ||
3648240883Sadrian	    (ts->ts_status != 0 && atid->isfiltered)) {
3649240883Sadrian		device_printf(sc->sc_dev,
3650240883Sadrian		    "%s: isfiltered=%d, ts_status=%d: huh?\n",
3651240883Sadrian		    __func__,
3652240883Sadrian		    atid->isfiltered,
3653240883Sadrian		    ts->ts_status);
3654240883Sadrian		ath_tx_tid_filt_comp_buf(sc, atid, bf);
3655240883Sadrian	}
3656240883Sadrian#endif
3657240639Sadrian	if (atid->isfiltered)
3658240883Sadrian		device_printf(sc->sc_dev, "%s: filtered?!\n", __func__);
3659227364Sadrian	if (atid->hwq_depth < 0)
3660227364Sadrian		device_printf(sc->sc_dev, "%s: hwq_depth < 0: %d\n",
3661227364Sadrian		    __func__, atid->hwq_depth);
3662240639Sadrian
3663240639Sadrian	/*
3664240639Sadrian	 * If the queue is filtered, potentially mark it as complete
3665240639Sadrian	 * and reschedule it as needed.
3666240639Sadrian	 *
3667240639Sadrian	 * This is required as there may be a subsequent TX descriptor
3668240639Sadrian	 * for this end-node that has CLRDMASK set, so it's quite possible
3669240639Sadrian	 * that a filtered frame will be followed by a non-filtered
3670240639Sadrian	 * (complete or otherwise) frame.
3671240639Sadrian	 *
3672240639Sadrian	 * XXX should we do this before we complete the frame?
3673240639Sadrian	 */
3674240639Sadrian	if (atid->isfiltered)
3675240639Sadrian		ath_tx_tid_filt_comp_complete(sc, atid);
3676243786Sadrian	ATH_TX_UNLOCK(sc);
3677227364Sadrian
3678227364Sadrian	/*
3679227364Sadrian	 * punt to rate control if we're not being cleaned up
3680227364Sadrian	 * during a hw queue drain and the frame wanted an ACK.
3681227364Sadrian	 */
3682233966Sadrian	if (fail == 0 && ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0))
3683227364Sadrian		ath_tx_update_ratectrl(sc, ni, bf->bf_state.bfs_rc,
3684227364Sadrian		    ts, bf->bf_state.bfs_pktlen,
3685227364Sadrian		    1, (ts->ts_status == 0) ? 0 : 1);
3686227364Sadrian
3687227364Sadrian	ath_tx_default_comp(sc, bf, fail);
3688227364Sadrian}
3689227364Sadrian
3690227364Sadrian/*
3691227364Sadrian * Handle cleanup of aggregate session packets that aren't
3692227364Sadrian * an A-MPDU.
3693227364Sadrian *
3694227364Sadrian * There's no need to update the BAW here - the session is being
3695227364Sadrian * torn down.
3696227364Sadrian */
3697227364Sadrianstatic void
3698227364Sadrianath_tx_comp_cleanup_unaggr(struct ath_softc *sc, struct ath_buf *bf)
3699227364Sadrian{
3700227364Sadrian	struct ieee80211_node *ni = bf->bf_node;
3701227364Sadrian	struct ath_node *an = ATH_NODE(ni);
3702227364Sadrian	int tid = bf->bf_state.bfs_tid;
3703227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
3704227364Sadrian
3705227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: TID %d: incomp=%d\n",
3706227364Sadrian	    __func__, tid, atid->incomp);
3707227364Sadrian
3708243786Sadrian	ATH_TX_LOCK(sc);
3709227364Sadrian	atid->incomp--;
3710227364Sadrian	if (atid->incomp == 0) {
3711227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
3712227364Sadrian		    "%s: TID %d: cleaned up! resume!\n",
3713227364Sadrian		    __func__, tid);
3714227364Sadrian		atid->cleanup_inprogress = 0;
3715227364Sadrian		ath_tx_tid_resume(sc, atid);
3716227364Sadrian	}
3717243786Sadrian	ATH_TX_UNLOCK(sc);
3718227364Sadrian
3719227364Sadrian	ath_tx_default_comp(sc, bf, 0);
3720227364Sadrian}
3721227364Sadrian
3722227364Sadrian/*
3723227364Sadrian * Performs transmit side cleanup when TID changes from aggregated to
3724227364Sadrian * unaggregated.
3725227364Sadrian *
3726227364Sadrian * - Discard all retry frames from the s/w queue.
3727227364Sadrian * - Fix the tx completion function for all buffers in s/w queue.
3728227364Sadrian * - Count the number of unacked frames, and let transmit completion
3729227364Sadrian *   handle it later.
3730227364Sadrian *
3731227364Sadrian * The caller is responsible for pausing the TID.
3732227364Sadrian */
3733227364Sadrianstatic void
3734235749Sadrianath_tx_tid_cleanup(struct ath_softc *sc, struct ath_node *an, int tid)
3735227364Sadrian{
3736227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
3737227364Sadrian	struct ieee80211_tx_ampdu *tap;
3738227364Sadrian	struct ath_buf *bf, *bf_next;
3739227364Sadrian	ath_bufhead bf_cq;
3740227364Sadrian
3741235774Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
3742227364Sadrian	    "%s: TID %d: called\n", __func__, tid);
3743227364Sadrian
3744227364Sadrian	TAILQ_INIT(&bf_cq);
3745243786Sadrian	ATH_TX_LOCK(sc);
3746227364Sadrian
3747227364Sadrian	/*
3748240639Sadrian	 * Move the filtered frames to the TX queue, before
3749240639Sadrian	 * we run off and discard/process things.
3750240639Sadrian	 */
3751240639Sadrian	/* XXX this is really quite inefficient */
3752241566Sadrian	while ((bf = ATH_TID_FILT_LAST(atid, ath_bufhead_s)) != NULL) {
3753241566Sadrian		ATH_TID_FILT_REMOVE(atid, bf, bf_list);
3754241336Sadrian		ATH_TID_INSERT_HEAD(atid, bf, bf_list);
3755240639Sadrian	}
3756240639Sadrian
3757240639Sadrian	/*
3758227364Sadrian	 * Update the frames in the software TX queue:
3759227364Sadrian	 *
3760227364Sadrian	 * + Discard retry frames in the queue
3761227364Sadrian	 * + Fix the completion function to be non-aggregate
3762227364Sadrian	 */
3763241336Sadrian	bf = ATH_TID_FIRST(atid);
3764227364Sadrian	while (bf) {
3765227364Sadrian		if (bf->bf_state.bfs_isretried) {
3766227364Sadrian			bf_next = TAILQ_NEXT(bf, bf_list);
3767241336Sadrian			ATH_TID_REMOVE(atid, bf, bf_list);
3768227364Sadrian			if (bf->bf_state.bfs_dobaw) {
3769227364Sadrian				ath_tx_update_baw(sc, an, atid, bf);
3770227364Sadrian				if (! bf->bf_state.bfs_addedbaw)
3771227364Sadrian					device_printf(sc->sc_dev,
3772227364Sadrian					    "%s: wasn't added: seqno %d\n",
3773229949Sadrian					    __func__,
3774229949Sadrian					    SEQNO(bf->bf_state.bfs_seqno));
3775227364Sadrian			}
3776227364Sadrian			bf->bf_state.bfs_dobaw = 0;
3777227364Sadrian			/*
3778227364Sadrian			 * Call the default completion handler with "fail" just
3779227364Sadrian			 * so upper levels are suitably notified about this.
3780227364Sadrian			 */
3781227364Sadrian			TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
3782227364Sadrian			bf = bf_next;
3783227364Sadrian			continue;
3784227364Sadrian		}
3785227364Sadrian		/* Give these the default completion handler */
3786227364Sadrian		bf->bf_comp = ath_tx_normal_comp;
3787227364Sadrian		bf = TAILQ_NEXT(bf, bf_list);
3788227364Sadrian	}
3789227364Sadrian
3790227364Sadrian	/* The caller is required to pause the TID */
3791227364Sadrian#if 0
3792227364Sadrian	/* Pause the TID */
3793227364Sadrian	ath_tx_tid_pause(sc, atid);
3794227364Sadrian#endif
3795227364Sadrian
3796227364Sadrian	/*
3797227364Sadrian	 * Calculate what hardware-queued frames exist based
3798227364Sadrian	 * on the current BAW size. Ie, what frames have been
3799227364Sadrian	 * added to the TX hardware queue for this TID but
3800227364Sadrian	 * not yet ACKed.
3801227364Sadrian	 */
3802227364Sadrian	tap = ath_tx_get_tx_tid(an, tid);
3803227364Sadrian	/* Need the lock - fiddling with BAW */
3804227364Sadrian	while (atid->baw_head != atid->baw_tail) {
3805227364Sadrian		if (atid->tx_buf[atid->baw_head]) {
3806227364Sadrian			atid->incomp++;
3807227364Sadrian			atid->cleanup_inprogress = 1;
3808227364Sadrian			atid->tx_buf[atid->baw_head] = NULL;
3809227364Sadrian		}
3810227364Sadrian		INCR(atid->baw_head, ATH_TID_MAX_BUFS);
3811227364Sadrian		INCR(tap->txa_start, IEEE80211_SEQ_RANGE);
3812227364Sadrian	}
3813227364Sadrian
3814227364Sadrian	/*
3815227364Sadrian	 * If cleanup is required, defer TID scheduling
3816227364Sadrian	 * until all the HW queued packets have been
3817227364Sadrian	 * sent.
3818227364Sadrian	 */
3819227364Sadrian	if (! atid->cleanup_inprogress)
3820227364Sadrian		ath_tx_tid_resume(sc, atid);
3821227364Sadrian
3822227364Sadrian	if (atid->cleanup_inprogress)
3823227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
3824227364Sadrian		    "%s: TID %d: cleanup needed: %d packets\n",
3825227364Sadrian		    __func__, tid, atid->incomp);
3826243786Sadrian	ATH_TX_UNLOCK(sc);
3827227364Sadrian
3828227364Sadrian	/* Handle completing frames and fail them */
3829227364Sadrian	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
3830227364Sadrian		TAILQ_REMOVE(&bf_cq, bf, bf_list);
3831227364Sadrian		ath_tx_default_comp(sc, bf, 1);
3832227364Sadrian	}
3833227364Sadrian}
3834227364Sadrian
3835227364Sadrianstatic struct ath_buf *
3836227398Sadrianath_tx_retry_clone(struct ath_softc *sc, struct ath_node *an,
3837227398Sadrian    struct ath_tid *tid, struct ath_buf *bf)
3838227364Sadrian{
3839227364Sadrian	struct ath_buf *nbf;
3840227364Sadrian	int error;
3841227364Sadrian
3842227364Sadrian	nbf = ath_buf_clone(sc, bf);
3843227364Sadrian
3844227364Sadrian#if 0
3845227364Sadrian	device_printf(sc->sc_dev, "%s: ATH_BUF_BUSY; cloning\n",
3846227364Sadrian	    __func__);
3847227364Sadrian#endif
3848227364Sadrian
3849227364Sadrian	if (nbf == NULL) {
3850227364Sadrian		/* Failed to clone */
3851227364Sadrian		device_printf(sc->sc_dev,
3852227364Sadrian		    "%s: failed to clone a busy buffer\n",
3853227364Sadrian		    __func__);
3854227364Sadrian		return NULL;
3855227364Sadrian	}
3856227364Sadrian
3857227364Sadrian	/* Setup the dma for the new buffer */
3858227364Sadrian	error = ath_tx_dmasetup(sc, nbf, nbf->bf_m);
3859227364Sadrian	if (error != 0) {
3860227364Sadrian		device_printf(sc->sc_dev,
3861227364Sadrian		    "%s: failed to setup dma for clone\n",
3862227364Sadrian		    __func__);
3863227364Sadrian		/*
3864227364Sadrian		 * Put this at the head of the list, not tail;
3865227364Sadrian		 * that way it doesn't interfere with the
3866227364Sadrian		 * busy buffer logic (which uses the tail of
3867227364Sadrian		 * the list.)
3868227364Sadrian		 */
3869227364Sadrian		ATH_TXBUF_LOCK(sc);
3870236994Sadrian		ath_returnbuf_head(sc, nbf);
3871227364Sadrian		ATH_TXBUF_UNLOCK(sc);
3872227364Sadrian		return NULL;
3873227364Sadrian	}
3874227364Sadrian
3875227398Sadrian	/* Update BAW if required, before we free the original buf */
3876227398Sadrian	if (bf->bf_state.bfs_dobaw)
3877227398Sadrian		ath_tx_switch_baw_buf(sc, an, tid, bf, nbf);
3878227398Sadrian
3879227364Sadrian	/* Free current buffer; return the older buffer */
3880227364Sadrian	bf->bf_m = NULL;
3881227364Sadrian	bf->bf_node = NULL;
3882227364Sadrian	ath_freebuf(sc, bf);
3883240639Sadrian
3884227364Sadrian	return nbf;
3885227364Sadrian}
3886227364Sadrian
3887227364Sadrian/*
3888227364Sadrian * Handle retrying an unaggregate frame in an aggregate
3889227364Sadrian * session.
3890227364Sadrian *
3891227364Sadrian * If too many retries occur, pause the TID, wait for
3892227364Sadrian * any further retransmits (as there's no reason why
3893227364Sadrian * non-aggregate frames in an aggregate session are
3894227364Sadrian * transmitted in-order; they just have to be in-BAW)
3895227364Sadrian * and then queue a BAR.
3896227364Sadrian */
3897227364Sadrianstatic void
3898227364Sadrianath_tx_aggr_retry_unaggr(struct ath_softc *sc, struct ath_buf *bf)
3899227364Sadrian{
3900227364Sadrian	struct ieee80211_node *ni = bf->bf_node;
3901227364Sadrian	struct ath_node *an = ATH_NODE(ni);
3902227364Sadrian	int tid = bf->bf_state.bfs_tid;
3903227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
3904227364Sadrian	struct ieee80211_tx_ampdu *tap;
3905227364Sadrian
3906243786Sadrian	ATH_TX_LOCK(sc);
3907227364Sadrian
3908227364Sadrian	tap = ath_tx_get_tx_tid(an, tid);
3909227364Sadrian
3910227364Sadrian	/*
3911227364Sadrian	 * If the buffer is marked as busy, we can't directly
3912227364Sadrian	 * reuse it. Instead, try to clone the buffer.
3913227364Sadrian	 * If the clone is successful, recycle the old buffer.
3914227364Sadrian	 * If the clone is unsuccessful, set bfs_retries to max
3915227364Sadrian	 * to force the next bit of code to free the buffer
3916227364Sadrian	 * for us.
3917227364Sadrian	 */
3918227364Sadrian	if ((bf->bf_state.bfs_retries < SWMAX_RETRIES) &&
3919227364Sadrian	    (bf->bf_flags & ATH_BUF_BUSY)) {
3920227364Sadrian		struct ath_buf *nbf;
3921227398Sadrian		nbf = ath_tx_retry_clone(sc, an, atid, bf);
3922227364Sadrian		if (nbf)
3923227364Sadrian			/* bf has been freed at this point */
3924227364Sadrian			bf = nbf;
3925227364Sadrian		else
3926227364Sadrian			bf->bf_state.bfs_retries = SWMAX_RETRIES + 1;
3927227364Sadrian	}
3928227364Sadrian
3929227364Sadrian	if (bf->bf_state.bfs_retries >= SWMAX_RETRIES) {
3930227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_RETRIES,
3931227364Sadrian		    "%s: exceeded retries; seqno %d\n",
3932227364Sadrian		    __func__, SEQNO(bf->bf_state.bfs_seqno));
3933227364Sadrian		sc->sc_stats.ast_tx_swretrymax++;
3934227364Sadrian
3935227364Sadrian		/* Update BAW anyway */
3936227364Sadrian		if (bf->bf_state.bfs_dobaw) {
3937227364Sadrian			ath_tx_update_baw(sc, an, atid, bf);
3938227364Sadrian			if (! bf->bf_state.bfs_addedbaw)
3939227364Sadrian				device_printf(sc->sc_dev,
3940227364Sadrian				    "%s: wasn't added: seqno %d\n",
3941227364Sadrian				    __func__, SEQNO(bf->bf_state.bfs_seqno));
3942227364Sadrian		}
3943227364Sadrian		bf->bf_state.bfs_dobaw = 0;
3944227364Sadrian
3945233908Sadrian		/* Suspend the TX queue and get ready to send the BAR */
3946233908Sadrian		ath_tx_tid_bar_suspend(sc, atid);
3947227364Sadrian
3948233908Sadrian		/* Send the BAR if there are no other frames waiting */
3949233908Sadrian		if (ath_tx_tid_bar_tx_ready(sc, atid))
3950233908Sadrian			ath_tx_tid_bar_tx(sc, atid);
3951227364Sadrian
3952243786Sadrian		ATH_TX_UNLOCK(sc);
3953227364Sadrian
3954227364Sadrian		/* Free buffer, bf is free after this call */
3955227364Sadrian		ath_tx_default_comp(sc, bf, 0);
3956227364Sadrian		return;
3957227364Sadrian	}
3958227364Sadrian
3959227364Sadrian	/*
3960227364Sadrian	 * This increments the retry counter as well as
3961227364Sadrian	 * sets the retry flag in the ath_buf and packet
3962227364Sadrian	 * body.
3963227364Sadrian	 */
3964227364Sadrian	ath_tx_set_retry(sc, bf);
3965240639Sadrian	sc->sc_stats.ast_tx_swretries++;
3966227364Sadrian
3967227364Sadrian	/*
3968227364Sadrian	 * Insert this at the head of the queue, so it's
3969227364Sadrian	 * retried before any current/subsequent frames.
3970227364Sadrian	 */
3971241336Sadrian	ATH_TID_INSERT_HEAD(atid, bf, bf_list);
3972227364Sadrian	ath_tx_tid_sched(sc, atid);
3973233908Sadrian	/* Send the BAR if there are no other frames waiting */
3974233908Sadrian	if (ath_tx_tid_bar_tx_ready(sc, atid))
3975233908Sadrian		ath_tx_tid_bar_tx(sc, atid);
3976227364Sadrian
3977243786Sadrian	ATH_TX_UNLOCK(sc);
3978227364Sadrian}
3979227364Sadrian
3980227364Sadrian/*
3981227364Sadrian * Common code for aggregate excessive retry/subframe retry.
3982227364Sadrian * If retrying, queues buffers to bf_q. If not, frees the
3983227364Sadrian * buffers.
3984227364Sadrian *
3985227364Sadrian * XXX should unify this with ath_tx_aggr_retry_unaggr()
3986227364Sadrian */
3987227364Sadrianstatic int
3988227364Sadrianath_tx_retry_subframe(struct ath_softc *sc, struct ath_buf *bf,
3989227364Sadrian    ath_bufhead *bf_q)
3990227364Sadrian{
3991227364Sadrian	struct ieee80211_node *ni = bf->bf_node;
3992227364Sadrian	struct ath_node *an = ATH_NODE(ni);
3993227364Sadrian	int tid = bf->bf_state.bfs_tid;
3994227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
3995227364Sadrian
3996243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
3997227364Sadrian
3998240255Sadrian	/* XXX clr11naggr should be done for all subframes */
3999227364Sadrian	ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
4000227364Sadrian	ath_hal_set11nburstduration(sc->sc_ah, bf->bf_desc, 0);
4001240639Sadrian
4002227364Sadrian	/* ath_hal_set11n_virtualmorefrag(sc->sc_ah, bf->bf_desc, 0); */
4003227364Sadrian
4004227364Sadrian	/*
4005227364Sadrian	 * If the buffer is marked as busy, we can't directly
4006227364Sadrian	 * reuse it. Instead, try to clone the buffer.
4007227364Sadrian	 * If the clone is successful, recycle the old buffer.
4008227364Sadrian	 * If the clone is unsuccessful, set bfs_retries to max
4009227364Sadrian	 * to force the next bit of code to free the buffer
4010227364Sadrian	 * for us.
4011227364Sadrian	 */
4012227364Sadrian	if ((bf->bf_state.bfs_retries < SWMAX_RETRIES) &&
4013227364Sadrian	    (bf->bf_flags & ATH_BUF_BUSY)) {
4014227364Sadrian		struct ath_buf *nbf;
4015227398Sadrian		nbf = ath_tx_retry_clone(sc, an, atid, bf);
4016227364Sadrian		if (nbf)
4017227364Sadrian			/* bf has been freed at this point */
4018227364Sadrian			bf = nbf;
4019227364Sadrian		else
4020227364Sadrian			bf->bf_state.bfs_retries = SWMAX_RETRIES + 1;
4021227364Sadrian	}
4022227364Sadrian
4023227364Sadrian	if (bf->bf_state.bfs_retries >= SWMAX_RETRIES) {
4024227364Sadrian		sc->sc_stats.ast_tx_swretrymax++;
4025227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_RETRIES,
4026227364Sadrian		    "%s: max retries: seqno %d\n",
4027227364Sadrian		    __func__, SEQNO(bf->bf_state.bfs_seqno));
4028227364Sadrian		ath_tx_update_baw(sc, an, atid, bf);
4029227364Sadrian		if (! bf->bf_state.bfs_addedbaw)
4030227364Sadrian			device_printf(sc->sc_dev,
4031227364Sadrian			    "%s: wasn't added: seqno %d\n",
4032227364Sadrian			    __func__, SEQNO(bf->bf_state.bfs_seqno));
4033227364Sadrian		bf->bf_state.bfs_dobaw = 0;
4034227364Sadrian		return 1;
4035227364Sadrian	}
4036227364Sadrian
4037227364Sadrian	ath_tx_set_retry(sc, bf);
4038240639Sadrian	sc->sc_stats.ast_tx_swretries++;
4039227364Sadrian	bf->bf_next = NULL;		/* Just to make sure */
4040227364Sadrian
4041240255Sadrian	/* Clear the aggregate state */
4042240255Sadrian	bf->bf_state.bfs_aggr = 0;
4043240255Sadrian	bf->bf_state.bfs_ndelim = 0;	/* ??? needed? */
4044240255Sadrian	bf->bf_state.bfs_nframes = 1;
4045240255Sadrian
4046227364Sadrian	TAILQ_INSERT_TAIL(bf_q, bf, bf_list);
4047227364Sadrian	return 0;
4048227364Sadrian}
4049227364Sadrian
4050227364Sadrian/*
4051227364Sadrian * error pkt completion for an aggregate destination
4052227364Sadrian */
4053227364Sadrianstatic void
4054227364Sadrianath_tx_comp_aggr_error(struct ath_softc *sc, struct ath_buf *bf_first,
4055227364Sadrian    struct ath_tid *tid)
4056227364Sadrian{
4057227364Sadrian	struct ieee80211_node *ni = bf_first->bf_node;
4058227364Sadrian	struct ath_node *an = ATH_NODE(ni);
4059227364Sadrian	struct ath_buf *bf_next, *bf;
4060227364Sadrian	ath_bufhead bf_q;
4061227364Sadrian	int drops = 0;
4062227364Sadrian	struct ieee80211_tx_ampdu *tap;
4063227364Sadrian	ath_bufhead bf_cq;
4064227364Sadrian
4065227364Sadrian	TAILQ_INIT(&bf_q);
4066227364Sadrian	TAILQ_INIT(&bf_cq);
4067227364Sadrian
4068227364Sadrian	/*
4069227364Sadrian	 * Update rate control - all frames have failed.
4070227364Sadrian	 *
4071227364Sadrian	 * XXX use the length in the first frame in the series;
4072227364Sadrian	 * XXX just so things are consistent for now.
4073227364Sadrian	 */
4074227364Sadrian	ath_tx_update_ratectrl(sc, ni, bf_first->bf_state.bfs_rc,
4075227364Sadrian	    &bf_first->bf_status.ds_txstat,
4076227364Sadrian	    bf_first->bf_state.bfs_pktlen,
4077227364Sadrian	    bf_first->bf_state.bfs_nframes, bf_first->bf_state.bfs_nframes);
4078227364Sadrian
4079243786Sadrian	ATH_TX_LOCK(sc);
4080227364Sadrian	tap = ath_tx_get_tx_tid(an, tid->tid);
4081227868Sadrian	sc->sc_stats.ast_tx_aggr_failall++;
4082227364Sadrian
4083227364Sadrian	/* Retry all subframes */
4084227364Sadrian	bf = bf_first;
4085227364Sadrian	while (bf) {
4086227364Sadrian		bf_next = bf->bf_next;
4087227364Sadrian		bf->bf_next = NULL;	/* Remove it from the aggr list */
4088227868Sadrian		sc->sc_stats.ast_tx_aggr_fail++;
4089227364Sadrian		if (ath_tx_retry_subframe(sc, bf, &bf_q)) {
4090227364Sadrian			drops++;
4091227364Sadrian			bf->bf_next = NULL;
4092227364Sadrian			TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
4093227364Sadrian		}
4094227364Sadrian		bf = bf_next;
4095227364Sadrian	}
4096227364Sadrian
4097227364Sadrian	/* Prepend all frames to the beginning of the queue */
4098227364Sadrian	while ((bf = TAILQ_LAST(&bf_q, ath_bufhead_s)) != NULL) {
4099227364Sadrian		TAILQ_REMOVE(&bf_q, bf, bf_list);
4100241336Sadrian		ATH_TID_INSERT_HEAD(tid, bf, bf_list);
4101227364Sadrian	}
4102227364Sadrian
4103234725Sadrian	/*
4104234725Sadrian	 * Schedule the TID to be re-tried.
4105234725Sadrian	 */
4106227364Sadrian	ath_tx_tid_sched(sc, tid);
4107227364Sadrian
4108227364Sadrian	/*
4109227364Sadrian	 * send bar if we dropped any frames
4110227364Sadrian	 *
4111227364Sadrian	 * Keep the txq lock held for now, as we need to ensure
4112227364Sadrian	 * that ni_txseqs[] is consistent (as it's being updated
4113227364Sadrian	 * in the ifnet TX context or raw TX context.)
4114227364Sadrian	 */
4115227364Sadrian	if (drops) {
4116233908Sadrian		/* Suspend the TX queue and get ready to send the BAR */
4117233908Sadrian		ath_tx_tid_bar_suspend(sc, tid);
4118227364Sadrian	}
4119227364Sadrian
4120233908Sadrian	/*
4121233908Sadrian	 * Send BAR if required
4122233908Sadrian	 */
4123233908Sadrian	if (ath_tx_tid_bar_tx_ready(sc, tid))
4124233908Sadrian		ath_tx_tid_bar_tx(sc, tid);
4125240639Sadrian
4126243786Sadrian	ATH_TX_UNLOCK(sc);
4127233908Sadrian
4128227364Sadrian	/* Complete frames which errored out */
4129227364Sadrian	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
4130227364Sadrian		TAILQ_REMOVE(&bf_cq, bf, bf_list);
4131227364Sadrian		ath_tx_default_comp(sc, bf, 0);
4132227364Sadrian	}
4133227364Sadrian}
4134227364Sadrian
4135227364Sadrian/*
4136227364Sadrian * Handle clean-up of packets from an aggregate list.
4137227364Sadrian *
4138227364Sadrian * There's no need to update the BAW here - the session is being
4139227364Sadrian * torn down.
4140227364Sadrian */
4141227364Sadrianstatic void
4142227364Sadrianath_tx_comp_cleanup_aggr(struct ath_softc *sc, struct ath_buf *bf_first)
4143227364Sadrian{
4144227364Sadrian	struct ath_buf *bf, *bf_next;
4145227364Sadrian	struct ieee80211_node *ni = bf_first->bf_node;
4146227364Sadrian	struct ath_node *an = ATH_NODE(ni);
4147227364Sadrian	int tid = bf_first->bf_state.bfs_tid;
4148227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
4149227364Sadrian
4150243786Sadrian	ATH_TX_LOCK(sc);
4151227364Sadrian
4152227364Sadrian	/* update incomp */
4153248341Sadrian	bf = bf_first;
4154227364Sadrian	while (bf) {
4155227364Sadrian		atid->incomp--;
4156227364Sadrian		bf = bf->bf_next;
4157227364Sadrian	}
4158227364Sadrian
4159227364Sadrian	if (atid->incomp == 0) {
4160227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4161227364Sadrian		    "%s: TID %d: cleaned up! resume!\n",
4162227364Sadrian		    __func__, tid);
4163227364Sadrian		atid->cleanup_inprogress = 0;
4164227364Sadrian		ath_tx_tid_resume(sc, atid);
4165227364Sadrian	}
4166233908Sadrian
4167233908Sadrian	/* Send BAR if required */
4168240639Sadrian	/* XXX why would we send a BAR when transitioning to non-aggregation? */
4169248341Sadrian	/*
4170248341Sadrian	 * XXX TODO: we should likely just tear down the BAR state here,
4171248341Sadrian	 * rather than sending a BAR.
4172248341Sadrian	 */
4173233908Sadrian	if (ath_tx_tid_bar_tx_ready(sc, atid))
4174233908Sadrian		ath_tx_tid_bar_tx(sc, atid);
4175240639Sadrian
4176243786Sadrian	ATH_TX_UNLOCK(sc);
4177227364Sadrian
4178227364Sadrian	/* Handle frame completion */
4179248341Sadrian	bf = bf_first;
4180227364Sadrian	while (bf) {
4181227364Sadrian		bf_next = bf->bf_next;
4182227364Sadrian		ath_tx_default_comp(sc, bf, 1);
4183227364Sadrian		bf = bf_next;
4184227364Sadrian	}
4185227364Sadrian}
4186227364Sadrian
4187227364Sadrian/*
4188227364Sadrian * Handle completion of an set of aggregate frames.
4189227364Sadrian *
4190227364Sadrian * Note: the completion handler is the last descriptor in the aggregate,
4191227364Sadrian * not the last descriptor in the first frame.
4192227364Sadrian */
4193227364Sadrianstatic void
4194229949Sadrianath_tx_aggr_comp_aggr(struct ath_softc *sc, struct ath_buf *bf_first,
4195229949Sadrian    int fail)
4196227364Sadrian{
4197227364Sadrian	//struct ath_desc *ds = bf->bf_lastds;
4198227364Sadrian	struct ieee80211_node *ni = bf_first->bf_node;
4199227364Sadrian	struct ath_node *an = ATH_NODE(ni);
4200227364Sadrian	int tid = bf_first->bf_state.bfs_tid;
4201227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
4202227364Sadrian	struct ath_tx_status ts;
4203227364Sadrian	struct ieee80211_tx_ampdu *tap;
4204227364Sadrian	ath_bufhead bf_q;
4205227364Sadrian	ath_bufhead bf_cq;
4206227364Sadrian	int seq_st, tx_ok;
4207227364Sadrian	int hasba, isaggr;
4208227364Sadrian	uint32_t ba[2];
4209227364Sadrian	struct ath_buf *bf, *bf_next;
4210227364Sadrian	int ba_index;
4211227364Sadrian	int drops = 0;
4212227364Sadrian	int nframes = 0, nbad = 0, nf;
4213227364Sadrian	int pktlen;
4214227364Sadrian	/* XXX there's too much on the stack? */
4215236038Sadrian	struct ath_rc_series rc[ATH_RC_NUM];
4216227364Sadrian	int txseq;
4217227364Sadrian
4218227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: called; hwq_depth=%d\n",
4219227364Sadrian	    __func__, atid->hwq_depth);
4220227364Sadrian
4221240677Sadrian	/*
4222240677Sadrian	 * Take a copy; this may be needed -after- bf_first
4223240677Sadrian	 * has been completed and freed.
4224240677Sadrian	 */
4225240677Sadrian	ts = bf_first->bf_status.ds_txstat;
4226240677Sadrian
4227240639Sadrian	TAILQ_INIT(&bf_q);
4228240639Sadrian	TAILQ_INIT(&bf_cq);
4229240639Sadrian
4230227364Sadrian	/* The TID state is kept behind the TXQ lock */
4231243786Sadrian	ATH_TX_LOCK(sc);
4232227364Sadrian
4233227364Sadrian	atid->hwq_depth--;
4234227364Sadrian	if (atid->hwq_depth < 0)
4235227364Sadrian		device_printf(sc->sc_dev, "%s: hwq_depth < 0: %d\n",
4236227364Sadrian		    __func__, atid->hwq_depth);
4237227364Sadrian
4238227364Sadrian	/*
4239240639Sadrian	 * If the TID is filtered, handle completing the filter
4240240639Sadrian	 * transition before potentially kicking it to the cleanup
4241240639Sadrian	 * function.
4242240677Sadrian	 *
4243240677Sadrian	 * XXX this is duplicate work, ew.
4244240639Sadrian	 */
4245240639Sadrian	if (atid->isfiltered)
4246240639Sadrian		ath_tx_tid_filt_comp_complete(sc, atid);
4247240639Sadrian
4248240639Sadrian	/*
4249227364Sadrian	 * Punt cleanup to the relevant function, not our problem now
4250227364Sadrian	 */
4251227364Sadrian	if (atid->cleanup_inprogress) {
4252240639Sadrian		if (atid->isfiltered)
4253240639Sadrian			device_printf(sc->sc_dev,
4254240639Sadrian			    "%s: isfiltered=1, normal_comp?\n",
4255240639Sadrian			    __func__);
4256243786Sadrian		ATH_TX_UNLOCK(sc);
4257227364Sadrian		ath_tx_comp_cleanup_aggr(sc, bf_first);
4258227364Sadrian		return;
4259227364Sadrian	}
4260227364Sadrian
4261227364Sadrian	/*
4262240639Sadrian	 * If the frame is filtered, transition to filtered frame
4263240639Sadrian	 * mode and add this to the filtered frame list.
4264240639Sadrian	 *
4265240639Sadrian	 * XXX TODO: figure out how this interoperates with
4266240639Sadrian	 * BAR, pause and cleanup states.
4267240639Sadrian	 */
4268240639Sadrian	if ((ts.ts_status & HAL_TXERR_FILT) ||
4269240639Sadrian	    (ts.ts_status != 0 && atid->isfiltered)) {
4270240639Sadrian		if (fail != 0)
4271240639Sadrian			device_printf(sc->sc_dev,
4272240639Sadrian			    "%s: isfiltered=1, fail=%d\n", __func__, fail);
4273240639Sadrian		ath_tx_tid_filt_comp_aggr(sc, atid, bf_first, &bf_cq);
4274240639Sadrian
4275240639Sadrian		/* Remove from BAW */
4276240639Sadrian		TAILQ_FOREACH_SAFE(bf, &bf_cq, bf_list, bf_next) {
4277240639Sadrian			if (bf->bf_state.bfs_addedbaw)
4278240639Sadrian				drops++;
4279240639Sadrian			if (bf->bf_state.bfs_dobaw) {
4280240639Sadrian				ath_tx_update_baw(sc, an, atid, bf);
4281240639Sadrian				if (! bf->bf_state.bfs_addedbaw)
4282240639Sadrian					device_printf(sc->sc_dev,
4283240639Sadrian					    "%s: wasn't added: seqno %d\n",
4284240639Sadrian					    __func__,
4285240639Sadrian					    SEQNO(bf->bf_state.bfs_seqno));
4286240639Sadrian			}
4287240639Sadrian			bf->bf_state.bfs_dobaw = 0;
4288240639Sadrian		}
4289240639Sadrian		/*
4290240639Sadrian		 * If any intermediate frames in the BAW were dropped when
4291240639Sadrian		 * handling filtering things, send a BAR.
4292240639Sadrian		 */
4293240639Sadrian		if (drops)
4294240639Sadrian			ath_tx_tid_bar_suspend(sc, atid);
4295240639Sadrian
4296240639Sadrian		/*
4297240639Sadrian		 * Finish up by sending a BAR if required and freeing
4298240639Sadrian		 * the frames outside of the TX lock.
4299240639Sadrian		 */
4300240639Sadrian		goto finish_send_bar;
4301240639Sadrian	}
4302240639Sadrian
4303240639Sadrian	/*
4304227364Sadrian	 * XXX for now, use the first frame in the aggregate for
4305227364Sadrian	 * XXX rate control completion; it's at least consistent.
4306227364Sadrian	 */
4307227364Sadrian	pktlen = bf_first->bf_state.bfs_pktlen;
4308227364Sadrian
4309227364Sadrian	/*
4310235461Sadrian	 * Handle errors first!
4311235461Sadrian	 *
4312235461Sadrian	 * Here, handle _any_ error as a "exceeded retries" error.
4313235461Sadrian	 * Later on (when filtered frames are to be specially handled)
4314235461Sadrian	 * it'll have to be expanded.
4315227364Sadrian	 */
4316235461Sadrian#if 0
4317227364Sadrian	if (ts.ts_status & HAL_TXERR_XRETRY) {
4318235461Sadrian#endif
4319235461Sadrian	if (ts.ts_status != 0) {
4320243786Sadrian		ATH_TX_UNLOCK(sc);
4321227364Sadrian		ath_tx_comp_aggr_error(sc, bf_first, atid);
4322227364Sadrian		return;
4323227364Sadrian	}
4324227364Sadrian
4325227364Sadrian	tap = ath_tx_get_tx_tid(an, tid);
4326227364Sadrian
4327227364Sadrian	/*
4328227364Sadrian	 * extract starting sequence and block-ack bitmap
4329227364Sadrian	 */
4330227364Sadrian	/* XXX endian-ness of seq_st, ba? */
4331227364Sadrian	seq_st = ts.ts_seqnum;
4332227364Sadrian	hasba = !! (ts.ts_flags & HAL_TX_BA);
4333227364Sadrian	tx_ok = (ts.ts_status == 0);
4334227364Sadrian	isaggr = bf_first->bf_state.bfs_aggr;
4335227364Sadrian	ba[0] = ts.ts_ba_low;
4336227364Sadrian	ba[1] = ts.ts_ba_high;
4337227364Sadrian
4338227364Sadrian	/*
4339227364Sadrian	 * Copy the TX completion status and the rate control
4340227364Sadrian	 * series from the first descriptor, as it may be freed
4341227364Sadrian	 * before the rate control code can get its grubby fingers
4342227364Sadrian	 * into things.
4343227364Sadrian	 */
4344227364Sadrian	memcpy(rc, bf_first->bf_state.bfs_rc, sizeof(rc));
4345227364Sadrian
4346227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4347229949Sadrian	    "%s: txa_start=%d, tx_ok=%d, status=%.8x, flags=%.8x, "
4348229949Sadrian	    "isaggr=%d, seq_st=%d, hasba=%d, ba=%.8x, %.8x\n",
4349227364Sadrian	    __func__, tap->txa_start, tx_ok, ts.ts_status, ts.ts_flags,
4350227364Sadrian	    isaggr, seq_st, hasba, ba[0], ba[1]);
4351227364Sadrian
4352248091Sadrian	/*
4353248091Sadrian	 * The reference driver doesn't do this; it simply ignores
4354248091Sadrian	 * this check in its entirety.
4355248091Sadrian	 *
4356248091Sadrian	 * I've seen this occur when using iperf to send traffic
4357248091Sadrian	 * out tid 1 - the aggregate frames are all marked as TID 1,
4358248091Sadrian	 * but the TXSTATUS has TID=0.  So, let's just ignore this
4359248091Sadrian	 * check.
4360248091Sadrian	 */
4361248091Sadrian#if 0
4362227364Sadrian	/* Occasionally, the MAC sends a tx status for the wrong TID. */
4363227364Sadrian	if (tid != ts.ts_tid) {
4364227364Sadrian		device_printf(sc->sc_dev, "%s: tid %d != hw tid %d\n",
4365227364Sadrian		    __func__, tid, ts.ts_tid);
4366227364Sadrian		tx_ok = 0;
4367227364Sadrian	}
4368248091Sadrian#endif
4369227364Sadrian
4370227364Sadrian	/* AR5416 BA bug; this requires an interface reset */
4371227364Sadrian	if (isaggr && tx_ok && (! hasba)) {
4372227364Sadrian		device_printf(sc->sc_dev,
4373229949Sadrian		    "%s: AR5416 bug: hasba=%d; txok=%d, isaggr=%d, "
4374229949Sadrian		    "seq_st=%d\n",
4375227364Sadrian		    __func__, hasba, tx_ok, isaggr, seq_st);
4376227364Sadrian		/* XXX TODO: schedule an interface reset */
4377238350Sjhb#ifdef ATH_DEBUG
4378238338Sadrian		ath_printtxbuf(sc, bf_first,
4379238338Sadrian		    sc->sc_ac2q[atid->ac]->axq_qnum, 0, 0);
4380238350Sjhb#endif
4381227364Sadrian	}
4382227364Sadrian
4383227364Sadrian	/*
4384227364Sadrian	 * Walk the list of frames, figure out which ones were correctly
4385227364Sadrian	 * sent and which weren't.
4386227364Sadrian	 */
4387227364Sadrian	bf = bf_first;
4388227364Sadrian	nf = bf_first->bf_state.bfs_nframes;
4389227364Sadrian
4390227364Sadrian	/* bf_first is going to be invalid once this list is walked */
4391227364Sadrian	bf_first = NULL;
4392227364Sadrian
4393227364Sadrian	/*
4394227364Sadrian	 * Walk the list of completed frames and determine
4395227364Sadrian	 * which need to be completed and which need to be
4396227364Sadrian	 * retransmitted.
4397227364Sadrian	 *
4398227364Sadrian	 * For completed frames, the completion functions need
4399227364Sadrian	 * to be called at the end of this function as the last
4400227364Sadrian	 * node reference may free the node.
4401227364Sadrian	 *
4402227364Sadrian	 * Finally, since the TXQ lock can't be held during the
4403227364Sadrian	 * completion callback (to avoid lock recursion),
4404227364Sadrian	 * the completion calls have to be done outside of the
4405227364Sadrian	 * lock.
4406227364Sadrian	 */
4407227364Sadrian	while (bf) {
4408227364Sadrian		nframes++;
4409229949Sadrian		ba_index = ATH_BA_INDEX(seq_st,
4410229949Sadrian		    SEQNO(bf->bf_state.bfs_seqno));
4411227364Sadrian		bf_next = bf->bf_next;
4412227364Sadrian		bf->bf_next = NULL;	/* Remove it from the aggr list */
4413227364Sadrian
4414227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4415227364Sadrian		    "%s: checking bf=%p seqno=%d; ack=%d\n",
4416227364Sadrian		    __func__, bf, SEQNO(bf->bf_state.bfs_seqno),
4417227364Sadrian		    ATH_BA_ISSET(ba, ba_index));
4418227364Sadrian
4419227364Sadrian		if (tx_ok && ATH_BA_ISSET(ba, ba_index)) {
4420227868Sadrian			sc->sc_stats.ast_tx_aggr_ok++;
4421227364Sadrian			ath_tx_update_baw(sc, an, atid, bf);
4422227364Sadrian			bf->bf_state.bfs_dobaw = 0;
4423227364Sadrian			if (! bf->bf_state.bfs_addedbaw)
4424227364Sadrian				device_printf(sc->sc_dev,
4425227364Sadrian				    "%s: wasn't added: seqno %d\n",
4426227364Sadrian				    __func__, SEQNO(bf->bf_state.bfs_seqno));
4427227364Sadrian			bf->bf_next = NULL;
4428227364Sadrian			TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
4429227364Sadrian		} else {
4430227868Sadrian			sc->sc_stats.ast_tx_aggr_fail++;
4431227364Sadrian			if (ath_tx_retry_subframe(sc, bf, &bf_q)) {
4432227364Sadrian				drops++;
4433227364Sadrian				bf->bf_next = NULL;
4434227364Sadrian				TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
4435227364Sadrian			}
4436227364Sadrian			nbad++;
4437227364Sadrian		}
4438227364Sadrian		bf = bf_next;
4439227364Sadrian	}
4440227364Sadrian
4441227364Sadrian	/*
4442227364Sadrian	 * Now that the BAW updates have been done, unlock
4443227364Sadrian	 *
4444227364Sadrian	 * txseq is grabbed before the lock is released so we
4445227364Sadrian	 * have a consistent view of what -was- in the BAW.
4446227364Sadrian	 * Anything after this point will not yet have been
4447227364Sadrian	 * TXed.
4448227364Sadrian	 */
4449227364Sadrian	txseq = tap->txa_start;
4450243786Sadrian	ATH_TX_UNLOCK(sc);
4451227364Sadrian
4452227364Sadrian	if (nframes != nf)
4453227364Sadrian		device_printf(sc->sc_dev,
4454227364Sadrian		    "%s: num frames seen=%d; bf nframes=%d\n",
4455227364Sadrian		    __func__, nframes, nf);
4456227364Sadrian
4457227364Sadrian	/*
4458227364Sadrian	 * Now we know how many frames were bad, call the rate
4459227364Sadrian	 * control code.
4460227364Sadrian	 */
4461227364Sadrian	if (fail == 0)
4462229949Sadrian		ath_tx_update_ratectrl(sc, ni, rc, &ts, pktlen, nframes,
4463229949Sadrian		    nbad);
4464227364Sadrian
4465227364Sadrian	/*
4466227364Sadrian	 * send bar if we dropped any frames
4467227364Sadrian	 */
4468227364Sadrian	if (drops) {
4469233908Sadrian		/* Suspend the TX queue and get ready to send the BAR */
4470243786Sadrian		ATH_TX_LOCK(sc);
4471233908Sadrian		ath_tx_tid_bar_suspend(sc, atid);
4472243786Sadrian		ATH_TX_UNLOCK(sc);
4473227364Sadrian	}
4474227364Sadrian
4475234725Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4476234725Sadrian	    "%s: txa_start now %d\n", __func__, tap->txa_start);
4477234725Sadrian
4478243786Sadrian	ATH_TX_LOCK(sc);
4479234725Sadrian
4480227364Sadrian	/* Prepend all frames to the beginning of the queue */
4481227364Sadrian	while ((bf = TAILQ_LAST(&bf_q, ath_bufhead_s)) != NULL) {
4482227364Sadrian		TAILQ_REMOVE(&bf_q, bf, bf_list);
4483241336Sadrian		ATH_TID_INSERT_HEAD(atid, bf, bf_list);
4484227364Sadrian	}
4485234725Sadrian
4486234725Sadrian	/*
4487234725Sadrian	 * Reschedule to grab some further frames.
4488234725Sadrian	 */
4489227364Sadrian	ath_tx_tid_sched(sc, atid);
4490227364Sadrian
4491233908Sadrian	/*
4492240639Sadrian	 * If the queue is filtered, re-schedule as required.
4493240639Sadrian	 *
4494240639Sadrian	 * This is required as there may be a subsequent TX descriptor
4495240639Sadrian	 * for this end-node that has CLRDMASK set, so it's quite possible
4496240639Sadrian	 * that a filtered frame will be followed by a non-filtered
4497240639Sadrian	 * (complete or otherwise) frame.
4498240639Sadrian	 *
4499240639Sadrian	 * XXX should we do this before we complete the frame?
4500240639Sadrian	 */
4501240639Sadrian	if (atid->isfiltered)
4502240639Sadrian		ath_tx_tid_filt_comp_complete(sc, atid);
4503240639Sadrian
4504240639Sadrianfinish_send_bar:
4505240639Sadrian
4506240639Sadrian	/*
4507233908Sadrian	 * Send BAR if required
4508233908Sadrian	 */
4509233908Sadrian	if (ath_tx_tid_bar_tx_ready(sc, atid))
4510233908Sadrian		ath_tx_tid_bar_tx(sc, atid);
4511234725Sadrian
4512243786Sadrian	ATH_TX_UNLOCK(sc);
4513233908Sadrian
4514227364Sadrian	/* Do deferred completion */
4515227364Sadrian	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
4516227364Sadrian		TAILQ_REMOVE(&bf_cq, bf, bf_list);
4517227364Sadrian		ath_tx_default_comp(sc, bf, 0);
4518227364Sadrian	}
4519227364Sadrian}
4520227364Sadrian
4521227364Sadrian/*
4522227364Sadrian * Handle completion of unaggregated frames in an ADDBA
4523227364Sadrian * session.
4524227364Sadrian *
4525227364Sadrian * Fail is set to 1 if the entry is being freed via a call to
4526227364Sadrian * ath_tx_draintxq().
4527227364Sadrian */
4528227364Sadrianstatic void
4529227364Sadrianath_tx_aggr_comp_unaggr(struct ath_softc *sc, struct ath_buf *bf, int fail)
4530227364Sadrian{
4531227364Sadrian	struct ieee80211_node *ni = bf->bf_node;
4532227364Sadrian	struct ath_node *an = ATH_NODE(ni);
4533227364Sadrian	int tid = bf->bf_state.bfs_tid;
4534227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
4535240677Sadrian	struct ath_tx_status ts;
4536240639Sadrian	int drops = 0;
4537227364Sadrian
4538227364Sadrian	/*
4539240677Sadrian	 * Take a copy of this; filtering/cloning the frame may free the
4540240677Sadrian	 * bf pointer.
4541240677Sadrian	 */
4542240677Sadrian	ts = bf->bf_status.ds_txstat;
4543240677Sadrian
4544240677Sadrian	/*
4545227364Sadrian	 * Update rate control status here, before we possibly
4546227364Sadrian	 * punt to retry or cleanup.
4547227364Sadrian	 *
4548227364Sadrian	 * Do it outside of the TXQ lock.
4549227364Sadrian	 */
4550233966Sadrian	if (fail == 0 && ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0))
4551227364Sadrian		ath_tx_update_ratectrl(sc, ni, bf->bf_state.bfs_rc,
4552227364Sadrian		    &bf->bf_status.ds_txstat,
4553227364Sadrian		    bf->bf_state.bfs_pktlen,
4554240677Sadrian		    1, (ts.ts_status == 0) ? 0 : 1);
4555227364Sadrian
4556227364Sadrian	/*
4557227364Sadrian	 * This is called early so atid->hwq_depth can be tracked.
4558227364Sadrian	 * This unfortunately means that it's released and regrabbed
4559227364Sadrian	 * during retry and cleanup. That's rather inefficient.
4560227364Sadrian	 */
4561243786Sadrian	ATH_TX_LOCK(sc);
4562227364Sadrian
4563227364Sadrian	if (tid == IEEE80211_NONQOS_TID)
4564227364Sadrian		device_printf(sc->sc_dev, "%s: TID=16!\n", __func__);
4565227364Sadrian
4566229949Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX,
4567229949Sadrian	    "%s: bf=%p: tid=%d, hwq_depth=%d, seqno=%d\n",
4568229949Sadrian	    __func__, bf, bf->bf_state.bfs_tid, atid->hwq_depth,
4569229949Sadrian	    SEQNO(bf->bf_state.bfs_seqno));
4570227364Sadrian
4571227364Sadrian	atid->hwq_depth--;
4572227364Sadrian	if (atid->hwq_depth < 0)
4573227364Sadrian		device_printf(sc->sc_dev, "%s: hwq_depth < 0: %d\n",
4574227364Sadrian		    __func__, atid->hwq_depth);
4575227364Sadrian
4576227364Sadrian	/*
4577240639Sadrian	 * If the TID is filtered, handle completing the filter
4578240639Sadrian	 * transition before potentially kicking it to the cleanup
4579240639Sadrian	 * function.
4580240639Sadrian	 */
4581240639Sadrian	if (atid->isfiltered)
4582240639Sadrian		ath_tx_tid_filt_comp_complete(sc, atid);
4583240639Sadrian
4584240639Sadrian	/*
4585227364Sadrian	 * If a cleanup is in progress, punt to comp_cleanup;
4586227364Sadrian	 * rather than handling it here. It's thus their
4587227364Sadrian	 * responsibility to clean up, call the completion
4588227364Sadrian	 * function in net80211, etc.
4589227364Sadrian	 */
4590227364Sadrian	if (atid->cleanup_inprogress) {
4591240639Sadrian		if (atid->isfiltered)
4592240639Sadrian			device_printf(sc->sc_dev,
4593240639Sadrian			    "%s: isfiltered=1, normal_comp?\n",
4594240639Sadrian			    __func__);
4595243786Sadrian		ATH_TX_UNLOCK(sc);
4596229949Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: cleanup_unaggr\n",
4597229949Sadrian		    __func__);
4598227364Sadrian		ath_tx_comp_cleanup_unaggr(sc, bf);
4599227364Sadrian		return;
4600227364Sadrian	}
4601227364Sadrian
4602227364Sadrian	/*
4603240639Sadrian	 * XXX TODO: how does cleanup, BAR and filtered frame handling
4604240639Sadrian	 * overlap?
4605240639Sadrian	 *
4606240639Sadrian	 * If the frame is filtered OR if it's any failure but
4607240639Sadrian	 * the TID is filtered, the frame must be added to the
4608240639Sadrian	 * filtered frame list.
4609240639Sadrian	 *
4610240639Sadrian	 * However - a busy buffer can't be added to the filtered
4611240639Sadrian	 * list as it will end up being recycled without having
4612240639Sadrian	 * been made available for the hardware.
4613240639Sadrian	 */
4614240677Sadrian	if ((ts.ts_status & HAL_TXERR_FILT) ||
4615240677Sadrian	    (ts.ts_status != 0 && atid->isfiltered)) {
4616240639Sadrian		int freeframe;
4617240639Sadrian
4618240639Sadrian		if (fail != 0)
4619240639Sadrian			device_printf(sc->sc_dev,
4620240639Sadrian			    "%s: isfiltered=1, fail=%d\n",
4621240639Sadrian			    __func__,
4622240639Sadrian			    fail);
4623240639Sadrian		freeframe = ath_tx_tid_filt_comp_single(sc, atid, bf);
4624240639Sadrian		if (freeframe) {
4625240639Sadrian			/* Remove from BAW */
4626240639Sadrian			if (bf->bf_state.bfs_addedbaw)
4627240639Sadrian				drops++;
4628240639Sadrian			if (bf->bf_state.bfs_dobaw) {
4629240639Sadrian				ath_tx_update_baw(sc, an, atid, bf);
4630240639Sadrian				if (! bf->bf_state.bfs_addedbaw)
4631240639Sadrian					device_printf(sc->sc_dev,
4632240639Sadrian					    "%s: wasn't added: seqno %d\n",
4633240639Sadrian					    __func__, SEQNO(bf->bf_state.bfs_seqno));
4634240639Sadrian			}
4635240639Sadrian			bf->bf_state.bfs_dobaw = 0;
4636240639Sadrian		}
4637240639Sadrian
4638240639Sadrian		/*
4639240639Sadrian		 * If the frame couldn't be filtered, treat it as a drop and
4640240639Sadrian		 * prepare to send a BAR.
4641240639Sadrian		 */
4642240639Sadrian		if (freeframe && drops)
4643240639Sadrian			ath_tx_tid_bar_suspend(sc, atid);
4644240639Sadrian
4645240639Sadrian		/*
4646240639Sadrian		 * Send BAR if required
4647240639Sadrian		 */
4648240639Sadrian		if (ath_tx_tid_bar_tx_ready(sc, atid))
4649240639Sadrian			ath_tx_tid_bar_tx(sc, atid);
4650240639Sadrian
4651243786Sadrian		ATH_TX_UNLOCK(sc);
4652240639Sadrian		/*
4653240639Sadrian		 * If freeframe is set, then the frame couldn't be
4654240639Sadrian		 * cloned and bf is still valid.  Just complete/free it.
4655240639Sadrian		 */
4656240639Sadrian		if (freeframe)
4657240639Sadrian			ath_tx_default_comp(sc, bf, fail);
4658240639Sadrian
4659240639Sadrian
4660240639Sadrian		return;
4661240639Sadrian	}
4662240639Sadrian	/*
4663227364Sadrian	 * Don't bother with the retry check if all frames
4664227364Sadrian	 * are being failed (eg during queue deletion.)
4665227364Sadrian	 */
4666235461Sadrian#if 0
4667227364Sadrian	if (fail == 0 && ts->ts_status & HAL_TXERR_XRETRY) {
4668235461Sadrian#endif
4669240677Sadrian	if (fail == 0 && ts.ts_status != 0) {
4670243786Sadrian		ATH_TX_UNLOCK(sc);
4671229949Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: retry_unaggr\n",
4672229949Sadrian		    __func__);
4673227364Sadrian		ath_tx_aggr_retry_unaggr(sc, bf);
4674227364Sadrian		return;
4675227364Sadrian	}
4676227364Sadrian
4677227364Sadrian	/* Success? Complete */
4678227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: TID=%d, seqno %d\n",
4679227364Sadrian	    __func__, tid, SEQNO(bf->bf_state.bfs_seqno));
4680227364Sadrian	if (bf->bf_state.bfs_dobaw) {
4681227364Sadrian		ath_tx_update_baw(sc, an, atid, bf);
4682227364Sadrian		bf->bf_state.bfs_dobaw = 0;
4683227364Sadrian		if (! bf->bf_state.bfs_addedbaw)
4684227364Sadrian			device_printf(sc->sc_dev,
4685227364Sadrian			    "%s: wasn't added: seqno %d\n",
4686227364Sadrian			    __func__, SEQNO(bf->bf_state.bfs_seqno));
4687227364Sadrian	}
4688227364Sadrian
4689233908Sadrian	/*
4690240639Sadrian	 * If the queue is filtered, re-schedule as required.
4691240639Sadrian	 *
4692240639Sadrian	 * This is required as there may be a subsequent TX descriptor
4693240639Sadrian	 * for this end-node that has CLRDMASK set, so it's quite possible
4694240639Sadrian	 * that a filtered frame will be followed by a non-filtered
4695240639Sadrian	 * (complete or otherwise) frame.
4696240639Sadrian	 *
4697240639Sadrian	 * XXX should we do this before we complete the frame?
4698240639Sadrian	 */
4699240639Sadrian	if (atid->isfiltered)
4700240639Sadrian		ath_tx_tid_filt_comp_complete(sc, atid);
4701240639Sadrian
4702240639Sadrian	/*
4703233908Sadrian	 * Send BAR if required
4704233908Sadrian	 */
4705233908Sadrian	if (ath_tx_tid_bar_tx_ready(sc, atid))
4706233908Sadrian		ath_tx_tid_bar_tx(sc, atid);
4707233908Sadrian
4708243786Sadrian	ATH_TX_UNLOCK(sc);
4709227364Sadrian
4710227364Sadrian	ath_tx_default_comp(sc, bf, fail);
4711227364Sadrian	/* bf is freed at this point */
4712227364Sadrian}
4713227364Sadrian
4714227364Sadrianvoid
4715227364Sadrianath_tx_aggr_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
4716227364Sadrian{
4717227364Sadrian	if (bf->bf_state.bfs_aggr)
4718227364Sadrian		ath_tx_aggr_comp_aggr(sc, bf, fail);
4719227364Sadrian	else
4720227364Sadrian		ath_tx_aggr_comp_unaggr(sc, bf, fail);
4721227364Sadrian}
4722227364Sadrian
4723227364Sadrian/*
4724227364Sadrian * Schedule some packets from the given node/TID to the hardware.
4725227364Sadrian *
4726227364Sadrian * This is the aggregate version.
4727227364Sadrian */
4728227364Sadrianvoid
4729227364Sadrianath_tx_tid_hw_queue_aggr(struct ath_softc *sc, struct ath_node *an,
4730227364Sadrian    struct ath_tid *tid)
4731227364Sadrian{
4732227364Sadrian	struct ath_buf *bf;
4733227364Sadrian	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
4734227364Sadrian	struct ieee80211_tx_ampdu *tap;
4735227364Sadrian	ATH_AGGR_STATUS status;
4736227364Sadrian	ath_bufhead bf_q;
4737227364Sadrian
4738227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d\n", __func__, tid->tid);
4739243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
4740227364Sadrian
4741227364Sadrian	tap = ath_tx_get_tx_tid(an, tid->tid);
4742227364Sadrian
4743227364Sadrian	if (tid->tid == IEEE80211_NONQOS_TID)
4744227364Sadrian		device_printf(sc->sc_dev, "%s: called for TID=NONQOS_TID?\n",
4745227364Sadrian		    __func__);
4746227364Sadrian
4747227364Sadrian	for (;;) {
4748227364Sadrian		status = ATH_AGGR_DONE;
4749227364Sadrian
4750227364Sadrian		/*
4751227364Sadrian		 * If the upper layer has paused the TID, don't
4752227364Sadrian		 * queue any further packets.
4753227364Sadrian		 *
4754227364Sadrian		 * This can also occur from the completion task because
4755227364Sadrian		 * of packet loss; but as its serialised with this code,
4756227364Sadrian		 * it won't "appear" half way through queuing packets.
4757227364Sadrian		 */
4758227364Sadrian		if (tid->paused)
4759227364Sadrian			break;
4760227364Sadrian
4761241336Sadrian		bf = ATH_TID_FIRST(tid);
4762227364Sadrian		if (bf == NULL) {
4763227364Sadrian			break;
4764227364Sadrian		}
4765227364Sadrian
4766227364Sadrian		/*
4767227364Sadrian		 * If the packet doesn't fall within the BAW (eg a NULL
4768227364Sadrian		 * data frame), schedule it directly; continue.
4769227364Sadrian		 */
4770227364Sadrian		if (! bf->bf_state.bfs_dobaw) {
4771229949Sadrian			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4772229949Sadrian			    "%s: non-baw packet\n",
4773227364Sadrian			    __func__);
4774241336Sadrian			ATH_TID_REMOVE(tid, bf, bf_list);
4775240180Sadrian
4776240180Sadrian			if (bf->bf_state.bfs_nframes > 1)
4777240180Sadrian				device_printf(sc->sc_dev,
4778240180Sadrian				    "%s: aggr=%d, nframes=%d\n",
4779240180Sadrian				    __func__,
4780240180Sadrian				    bf->bf_state.bfs_aggr,
4781240180Sadrian				    bf->bf_state.bfs_nframes);
4782240180Sadrian
4783240180Sadrian			/*
4784240180Sadrian			 * This shouldn't happen - such frames shouldn't
4785240180Sadrian			 * ever have been queued as an aggregate in the
4786240180Sadrian			 * first place.  However, make sure the fields
4787240180Sadrian			 * are correctly setup just to be totally sure.
4788240180Sadrian			 */
4789227364Sadrian			bf->bf_state.bfs_aggr = 0;
4790240180Sadrian			bf->bf_state.bfs_nframes = 1;
4791240180Sadrian
4792240724Sadrian			/* Update CLRDMASK just before this frame is queued */
4793240724Sadrian			ath_tx_update_clrdmask(sc, tid, bf);
4794240724Sadrian
4795227364Sadrian			ath_tx_do_ratelookup(sc, bf);
4796233989Sadrian			ath_tx_calc_duration(sc, bf);
4797233989Sadrian			ath_tx_calc_protection(sc, bf);
4798233989Sadrian			ath_tx_set_rtscts(sc, bf);
4799227364Sadrian			ath_tx_rate_fill_rcflags(sc, bf);
4800227364Sadrian			ath_tx_setds(sc, bf);
4801227364Sadrian			ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
4802227364Sadrian
4803227364Sadrian			sc->sc_aggr_stats.aggr_nonbaw_pkt++;
4804227364Sadrian
4805227364Sadrian			/* Queue the packet; continue */
4806227364Sadrian			goto queuepkt;
4807227364Sadrian		}
4808227364Sadrian
4809227364Sadrian		TAILQ_INIT(&bf_q);
4810227364Sadrian
4811227364Sadrian		/*
4812227364Sadrian		 * Do a rate control lookup on the first frame in the
4813227364Sadrian		 * list. The rate control code needs that to occur
4814227364Sadrian		 * before it can determine whether to TX.
4815227364Sadrian		 * It's inaccurate because the rate control code doesn't
4816227364Sadrian		 * really "do" aggregate lookups, so it only considers
4817227364Sadrian		 * the size of the first frame.
4818227364Sadrian		 */
4819227364Sadrian		ath_tx_do_ratelookup(sc, bf);
4820227364Sadrian		bf->bf_state.bfs_rc[3].rix = 0;
4821227364Sadrian		bf->bf_state.bfs_rc[3].tries = 0;
4822233989Sadrian
4823233989Sadrian		ath_tx_calc_duration(sc, bf);
4824233989Sadrian		ath_tx_calc_protection(sc, bf);
4825233989Sadrian
4826233989Sadrian		ath_tx_set_rtscts(sc, bf);
4827227364Sadrian		ath_tx_rate_fill_rcflags(sc, bf);
4828227364Sadrian
4829227364Sadrian		status = ath_tx_form_aggr(sc, an, tid, &bf_q);
4830227364Sadrian
4831227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4832227364Sadrian		    "%s: ath_tx_form_aggr() status=%d\n", __func__, status);
4833227364Sadrian
4834227364Sadrian		/*
4835227364Sadrian		 * No frames to be picked up - out of BAW
4836227364Sadrian		 */
4837227364Sadrian		if (TAILQ_EMPTY(&bf_q))
4838227364Sadrian			break;
4839227364Sadrian
4840227364Sadrian		/*
4841227364Sadrian		 * This assumes that the descriptor list in the ath_bufhead
4842227364Sadrian		 * are already linked together via bf_next pointers.
4843227364Sadrian		 */
4844227364Sadrian		bf = TAILQ_FIRST(&bf_q);
4845227364Sadrian
4846233989Sadrian		if (status == ATH_AGGR_8K_LIMITED)
4847233989Sadrian			sc->sc_aggr_stats.aggr_rts_aggr_limited++;
4848233989Sadrian
4849227364Sadrian		/*
4850227364Sadrian		 * If it's the only frame send as non-aggregate
4851227364Sadrian		 * assume that ath_tx_form_aggr() has checked
4852227364Sadrian		 * whether it's in the BAW and added it appropriately.
4853227364Sadrian		 */
4854227364Sadrian		if (bf->bf_state.bfs_nframes == 1) {
4855227364Sadrian			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4856227364Sadrian			    "%s: single-frame aggregate\n", __func__);
4857240724Sadrian
4858240724Sadrian			/* Update CLRDMASK just before this frame is queued */
4859240724Sadrian			ath_tx_update_clrdmask(sc, tid, bf);
4860240724Sadrian
4861227364Sadrian			bf->bf_state.bfs_aggr = 0;
4862240255Sadrian			bf->bf_state.bfs_ndelim = 0;
4863227364Sadrian			ath_tx_setds(sc, bf);
4864227364Sadrian			ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
4865227364Sadrian			if (status == ATH_AGGR_BAW_CLOSED)
4866227364Sadrian				sc->sc_aggr_stats.aggr_baw_closed_single_pkt++;
4867227364Sadrian			else
4868227364Sadrian				sc->sc_aggr_stats.aggr_single_pkt++;
4869227364Sadrian		} else {
4870227364Sadrian			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4871229949Sadrian			    "%s: multi-frame aggregate: %d frames, "
4872229949Sadrian			    "length %d\n",
4873227364Sadrian			     __func__, bf->bf_state.bfs_nframes,
4874227364Sadrian			    bf->bf_state.bfs_al);
4875227364Sadrian			bf->bf_state.bfs_aggr = 1;
4876227364Sadrian			sc->sc_aggr_stats.aggr_pkts[bf->bf_state.bfs_nframes]++;
4877227364Sadrian			sc->sc_aggr_stats.aggr_aggr_pkt++;
4878227364Sadrian
4879240724Sadrian			/* Update CLRDMASK just before this frame is queued */
4880240724Sadrian			ath_tx_update_clrdmask(sc, tid, bf);
4881240724Sadrian
4882227364Sadrian			/*
4883233989Sadrian			 * Calculate the duration/protection as required.
4884233989Sadrian			 */
4885233989Sadrian			ath_tx_calc_duration(sc, bf);
4886233989Sadrian			ath_tx_calc_protection(sc, bf);
4887233989Sadrian
4888233989Sadrian			/*
4889227364Sadrian			 * Update the rate and rtscts information based on the
4890227364Sadrian			 * rate decision made by the rate control code;
4891227364Sadrian			 * the first frame in the aggregate needs it.
4892227364Sadrian			 */
4893227364Sadrian			ath_tx_set_rtscts(sc, bf);
4894227364Sadrian
4895227364Sadrian			/*
4896227364Sadrian			 * Setup the relevant descriptor fields
4897227364Sadrian			 * for aggregation. The first descriptor
4898227364Sadrian			 * already points to the rest in the chain.
4899227364Sadrian			 */
4900227364Sadrian			ath_tx_setds_11n(sc, bf);
4901227364Sadrian
4902227364Sadrian		}
4903227364Sadrian	queuepkt:
4904227364Sadrian		/* Set completion handler, multi-frame aggregate or not */
4905227364Sadrian		bf->bf_comp = ath_tx_aggr_comp;
4906227364Sadrian
4907227364Sadrian		if (bf->bf_state.bfs_tid == IEEE80211_NONQOS_TID)
4908227364Sadrian		    device_printf(sc->sc_dev, "%s: TID=16?\n", __func__);
4909227364Sadrian
4910227364Sadrian		/* Punt to txq */
4911227364Sadrian		ath_tx_handoff(sc, txq, bf);
4912227364Sadrian
4913227364Sadrian		/* Track outstanding buffer count to hardware */
4914227364Sadrian		/* aggregates are "one" buffer */
4915227364Sadrian		tid->hwq_depth++;
4916227364Sadrian
4917227364Sadrian		/*
4918227364Sadrian		 * Break out if ath_tx_form_aggr() indicated
4919227364Sadrian		 * there can't be any further progress (eg BAW is full.)
4920227364Sadrian		 * Checking for an empty txq is done above.
4921227364Sadrian		 *
4922227364Sadrian		 * XXX locking on txq here?
4923227364Sadrian		 */
4924227364Sadrian		if (txq->axq_aggr_depth >= sc->sc_hwq_limit ||
4925227364Sadrian		    status == ATH_AGGR_BAW_CLOSED)
4926227364Sadrian			break;
4927227364Sadrian	}
4928227364Sadrian}
4929227364Sadrian
4930227364Sadrian/*
4931227364Sadrian * Schedule some packets from the given node/TID to the hardware.
4932227364Sadrian */
4933227364Sadrianvoid
4934227364Sadrianath_tx_tid_hw_queue_norm(struct ath_softc *sc, struct ath_node *an,
4935227364Sadrian    struct ath_tid *tid)
4936227364Sadrian{
4937227364Sadrian	struct ath_buf *bf;
4938227364Sadrian	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
4939227364Sadrian
4940227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: node %p: TID %d: called\n",
4941227364Sadrian	    __func__, an, tid->tid);
4942227364Sadrian
4943243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
4944227364Sadrian
4945227364Sadrian	/* Check - is AMPDU pending or running? then print out something */
4946227364Sadrian	if (ath_tx_ampdu_pending(sc, an, tid->tid))
4947227364Sadrian		device_printf(sc->sc_dev, "%s: tid=%d, ampdu pending?\n",
4948227364Sadrian		    __func__, tid->tid);
4949227364Sadrian	if (ath_tx_ampdu_running(sc, an, tid->tid))
4950227364Sadrian		device_printf(sc->sc_dev, "%s: tid=%d, ampdu running?\n",
4951227364Sadrian		    __func__, tid->tid);
4952227364Sadrian
4953227364Sadrian	for (;;) {
4954227364Sadrian
4955227364Sadrian		/*
4956227364Sadrian		 * If the upper layers have paused the TID, don't
4957227364Sadrian		 * queue any further packets.
4958227364Sadrian		 */
4959227364Sadrian		if (tid->paused)
4960227364Sadrian			break;
4961227364Sadrian
4962241336Sadrian		bf = ATH_TID_FIRST(tid);
4963227364Sadrian		if (bf == NULL) {
4964227364Sadrian			break;
4965227364Sadrian		}
4966227364Sadrian
4967241336Sadrian		ATH_TID_REMOVE(tid, bf, bf_list);
4968227364Sadrian
4969227364Sadrian		/* Sanity check! */
4970227364Sadrian		if (tid->tid != bf->bf_state.bfs_tid) {
4971227364Sadrian			device_printf(sc->sc_dev, "%s: bfs_tid %d !="
4972227364Sadrian			    " tid %d\n",
4973227364Sadrian			    __func__, bf->bf_state.bfs_tid, tid->tid);
4974227364Sadrian		}
4975227364Sadrian		/* Normal completion handler */
4976227364Sadrian		bf->bf_comp = ath_tx_normal_comp;
4977227364Sadrian
4978240883Sadrian		/*
4979240883Sadrian		 * Override this for now, until the non-aggregate
4980240883Sadrian		 * completion handler correctly handles software retransmits.
4981240883Sadrian		 */
4982240883Sadrian		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
4983240883Sadrian
4984240724Sadrian		/* Update CLRDMASK just before this frame is queued */
4985240724Sadrian		ath_tx_update_clrdmask(sc, tid, bf);
4986240724Sadrian
4987227364Sadrian		/* Program descriptors + rate control */
4988227364Sadrian		ath_tx_do_ratelookup(sc, bf);
4989233989Sadrian		ath_tx_calc_duration(sc, bf);
4990233989Sadrian		ath_tx_calc_protection(sc, bf);
4991233989Sadrian		ath_tx_set_rtscts(sc, bf);
4992227364Sadrian		ath_tx_rate_fill_rcflags(sc, bf);
4993227364Sadrian		ath_tx_setds(sc, bf);
4994227364Sadrian
4995227364Sadrian		/* Track outstanding buffer count to hardware */
4996227364Sadrian		/* aggregates are "one" buffer */
4997227364Sadrian		tid->hwq_depth++;
4998227364Sadrian
4999227364Sadrian		/* Punt to hardware or software txq */
5000227364Sadrian		ath_tx_handoff(sc, txq, bf);
5001227364Sadrian	}
5002227364Sadrian}
5003227364Sadrian
5004227364Sadrian/*
5005227364Sadrian * Schedule some packets to the given hardware queue.
5006227364Sadrian *
5007227364Sadrian * This function walks the list of TIDs (ie, ath_node TIDs
5008227364Sadrian * with queued traffic) and attempts to schedule traffic
5009227364Sadrian * from them.
5010227364Sadrian *
5011227364Sadrian * TID scheduling is implemented as a FIFO, with TIDs being
5012227364Sadrian * added to the end of the queue after some frames have been
5013227364Sadrian * scheduled.
5014227364Sadrian */
5015227364Sadrianvoid
5016227364Sadrianath_txq_sched(struct ath_softc *sc, struct ath_txq *txq)
5017227364Sadrian{
5018227364Sadrian	struct ath_tid *tid, *next, *last;
5019227364Sadrian
5020243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
5021227364Sadrian
5022227364Sadrian	/*
5023227364Sadrian	 * Don't schedule if the hardware queue is busy.
5024227364Sadrian	 * This (hopefully) gives some more time to aggregate
5025227364Sadrian	 * some packets in the aggregation queue.
5026227364Sadrian	 */
5027227364Sadrian	if (txq->axq_aggr_depth >= sc->sc_hwq_limit) {
5028227364Sadrian		sc->sc_aggr_stats.aggr_sched_nopkt++;
5029227364Sadrian		return;
5030227364Sadrian	}
5031227364Sadrian
5032227364Sadrian	last = TAILQ_LAST(&txq->axq_tidq, axq_t_s);
5033227364Sadrian
5034227364Sadrian	TAILQ_FOREACH_SAFE(tid, &txq->axq_tidq, axq_qelem, next) {
5035227364Sadrian		/*
5036227364Sadrian		 * Suspend paused queues here; they'll be resumed
5037227364Sadrian		 * once the addba completes or times out.
5038227364Sadrian		 */
5039227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d, paused=%d\n",
5040227364Sadrian		    __func__, tid->tid, tid->paused);
5041227364Sadrian		ath_tx_tid_unsched(sc, tid);
5042227364Sadrian		if (tid->paused) {
5043227364Sadrian			continue;
5044227364Sadrian		}
5045227364Sadrian		if (ath_tx_ampdu_running(sc, tid->an, tid->tid))
5046227364Sadrian			ath_tx_tid_hw_queue_aggr(sc, tid->an, tid);
5047227364Sadrian		else
5048227364Sadrian			ath_tx_tid_hw_queue_norm(sc, tid->an, tid);
5049227364Sadrian
5050227364Sadrian		/* Not empty? Re-schedule */
5051227364Sadrian		if (tid->axq_depth != 0)
5052227364Sadrian			ath_tx_tid_sched(sc, tid);
5053227364Sadrian
5054227364Sadrian		/* Give the software queue time to aggregate more packets */
5055227364Sadrian		if (txq->axq_aggr_depth >= sc->sc_hwq_limit) {
5056227364Sadrian			break;
5057227364Sadrian		}
5058227364Sadrian
5059227364Sadrian		/*
5060227364Sadrian		 * If this was the last entry on the original list, stop.
5061227364Sadrian		 * Otherwise nodes that have been rescheduled onto the end
5062227364Sadrian		 * of the TID FIFO list will just keep being rescheduled.
5063227364Sadrian		 */
5064227364Sadrian		if (tid == last)
5065227364Sadrian			break;
5066227364Sadrian	}
5067227364Sadrian}
5068227364Sadrian
5069227364Sadrian/*
5070227364Sadrian * TX addba handling
5071227364Sadrian */
5072227364Sadrian
5073227364Sadrian/*
5074227364Sadrian * Return net80211 TID struct pointer, or NULL for none
5075227364Sadrian */
5076227364Sadrianstruct ieee80211_tx_ampdu *
5077227364Sadrianath_tx_get_tx_tid(struct ath_node *an, int tid)
5078227364Sadrian{
5079227364Sadrian	struct ieee80211_node *ni = &an->an_node;
5080227364Sadrian	struct ieee80211_tx_ampdu *tap;
5081227364Sadrian
5082227364Sadrian	if (tid == IEEE80211_NONQOS_TID)
5083227364Sadrian		return NULL;
5084227364Sadrian
5085234324Sadrian	tap = &ni->ni_tx_ampdu[tid];
5086227364Sadrian	return tap;
5087227364Sadrian}
5088227364Sadrian
5089227364Sadrian/*
5090227364Sadrian * Is AMPDU-TX running?
5091227364Sadrian */
5092227364Sadrianstatic int
5093227364Sadrianath_tx_ampdu_running(struct ath_softc *sc, struct ath_node *an, int tid)
5094227364Sadrian{
5095227364Sadrian	struct ieee80211_tx_ampdu *tap;
5096227364Sadrian
5097227364Sadrian	if (tid == IEEE80211_NONQOS_TID)
5098227364Sadrian		return 0;
5099227364Sadrian
5100227364Sadrian	tap = ath_tx_get_tx_tid(an, tid);
5101227364Sadrian	if (tap == NULL)
5102227364Sadrian		return 0;	/* Not valid; default to not running */
5103227364Sadrian
5104227364Sadrian	return !! (tap->txa_flags & IEEE80211_AGGR_RUNNING);
5105227364Sadrian}
5106227364Sadrian
5107227364Sadrian/*
5108227364Sadrian * Is AMPDU-TX negotiation pending?
5109227364Sadrian */
5110227364Sadrianstatic int
5111227364Sadrianath_tx_ampdu_pending(struct ath_softc *sc, struct ath_node *an, int tid)
5112227364Sadrian{
5113227364Sadrian	struct ieee80211_tx_ampdu *tap;
5114227364Sadrian
5115227364Sadrian	if (tid == IEEE80211_NONQOS_TID)
5116227364Sadrian		return 0;
5117227364Sadrian
5118227364Sadrian	tap = ath_tx_get_tx_tid(an, tid);
5119227364Sadrian	if (tap == NULL)
5120227364Sadrian		return 0;	/* Not valid; default to not pending */
5121227364Sadrian
5122227364Sadrian	return !! (tap->txa_flags & IEEE80211_AGGR_XCHGPEND);
5123227364Sadrian}
5124227364Sadrian
5125227364Sadrian/*
5126227364Sadrian * Is AMPDU-TX pending for the given TID?
5127227364Sadrian */
5128227364Sadrian
5129227364Sadrian
5130227364Sadrian/*
5131227364Sadrian * Method to handle sending an ADDBA request.
5132227364Sadrian *
5133227364Sadrian * We tap this so the relevant flags can be set to pause the TID
5134227364Sadrian * whilst waiting for the response.
5135227364Sadrian *
5136227364Sadrian * XXX there's no timeout handler we can override?
5137227364Sadrian */
5138227364Sadrianint
5139227364Sadrianath_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
5140227364Sadrian    int dialogtoken, int baparamset, int batimeout)
5141227364Sadrian{
5142227364Sadrian	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
5143234324Sadrian	int tid = tap->txa_tid;
5144227364Sadrian	struct ath_node *an = ATH_NODE(ni);
5145227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
5146227364Sadrian
5147227364Sadrian	/*
5148227364Sadrian	 * XXX danger Will Robinson!
5149227364Sadrian	 *
5150227364Sadrian	 * Although the taskqueue may be running and scheduling some more
5151227364Sadrian	 * packets, these should all be _before_ the addba sequence number.
5152227364Sadrian	 * However, net80211 will keep self-assigning sequence numbers
5153227364Sadrian	 * until addba has been negotiated.
5154227364Sadrian	 *
5155227364Sadrian	 * In the past, these packets would be "paused" (which still works
5156227364Sadrian	 * fine, as they're being scheduled to the driver in the same
5157227364Sadrian	 * serialised method which is calling the addba request routine)
5158227364Sadrian	 * and when the aggregation session begins, they'll be dequeued
5159227364Sadrian	 * as aggregate packets and added to the BAW. However, now there's
5160227364Sadrian	 * a "bf->bf_state.bfs_dobaw" flag, and this isn't set for these
5161227364Sadrian	 * packets. Thus they never get included in the BAW tracking and
5162227364Sadrian	 * this can cause the initial burst of packets after the addba
5163227364Sadrian	 * negotiation to "hang", as they quickly fall outside the BAW.
5164227364Sadrian	 *
5165227364Sadrian	 * The "eventual" solution should be to tag these packets with
5166227364Sadrian	 * dobaw. Although net80211 has given us a sequence number,
5167227364Sadrian	 * it'll be "after" the left edge of the BAW and thus it'll
5168227364Sadrian	 * fall within it.
5169227364Sadrian	 */
5170243786Sadrian	ATH_TX_LOCK(sc);
5171235774Sadrian	/*
5172235774Sadrian	 * This is a bit annoying.  Until net80211 HT code inherits some
5173235774Sadrian	 * (any) locking, we may have this called in parallel BUT only
5174235774Sadrian	 * one response/timeout will be called.  Grr.
5175235774Sadrian	 */
5176235774Sadrian	if (atid->addba_tx_pending == 0) {
5177235774Sadrian		ath_tx_tid_pause(sc, atid);
5178235774Sadrian		atid->addba_tx_pending = 1;
5179235774Sadrian	}
5180243786Sadrian	ATH_TX_UNLOCK(sc);
5181227364Sadrian
5182227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5183227364Sadrian	    "%s: called; dialogtoken=%d, baparamset=%d, batimeout=%d\n",
5184227364Sadrian	    __func__, dialogtoken, baparamset, batimeout);
5185227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5186227364Sadrian	    "%s: txa_start=%d, ni_txseqs=%d\n",
5187227364Sadrian	    __func__, tap->txa_start, ni->ni_txseqs[tid]);
5188227364Sadrian
5189227364Sadrian	return sc->sc_addba_request(ni, tap, dialogtoken, baparamset,
5190227364Sadrian	    batimeout);
5191227364Sadrian}
5192227364Sadrian
5193227364Sadrian/*
5194227364Sadrian * Handle an ADDBA response.
5195227364Sadrian *
5196227364Sadrian * We unpause the queue so TX'ing can resume.
5197227364Sadrian *
5198227364Sadrian * Any packets TX'ed from this point should be "aggregate" (whether
5199227364Sadrian * aggregate or not) so the BAW is updated.
5200227364Sadrian *
5201227364Sadrian * Note! net80211 keeps self-assigning sequence numbers until
5202227364Sadrian * ampdu is negotiated. This means the initially-negotiated BAW left
5203227364Sadrian * edge won't match the ni->ni_txseq.
5204227364Sadrian *
5205227364Sadrian * So, being very dirty, the BAW left edge is "slid" here to match
5206227364Sadrian * ni->ni_txseq.
5207227364Sadrian *
5208227364Sadrian * What likely SHOULD happen is that all packets subsequent to the
5209227364Sadrian * addba request should be tagged as aggregate and queued as non-aggregate
5210227364Sadrian * frames; thus updating the BAW. For now though, I'll just slide the
5211227364Sadrian * window.
5212227364Sadrian */
5213227364Sadrianint
5214227364Sadrianath_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
5215227364Sadrian    int status, int code, int batimeout)
5216227364Sadrian{
5217227364Sadrian	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
5218234324Sadrian	int tid = tap->txa_tid;
5219227364Sadrian	struct ath_node *an = ATH_NODE(ni);
5220227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
5221227364Sadrian	int r;
5222227364Sadrian
5223227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5224227364Sadrian	    "%s: called; status=%d, code=%d, batimeout=%d\n", __func__,
5225227364Sadrian	    status, code, batimeout);
5226227364Sadrian
5227227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5228227364Sadrian	    "%s: txa_start=%d, ni_txseqs=%d\n",
5229227364Sadrian	    __func__, tap->txa_start, ni->ni_txseqs[tid]);
5230227364Sadrian
5231227364Sadrian	/*
5232227364Sadrian	 * Call this first, so the interface flags get updated
5233227364Sadrian	 * before the TID is unpaused. Otherwise a race condition
5234227364Sadrian	 * exists where the unpaused TID still doesn't yet have
5235227364Sadrian	 * IEEE80211_AGGR_RUNNING set.
5236227364Sadrian	 */
5237227364Sadrian	r = sc->sc_addba_response(ni, tap, status, code, batimeout);
5238227364Sadrian
5239243786Sadrian	ATH_TX_LOCK(sc);
5240235774Sadrian	atid->addba_tx_pending = 0;
5241227364Sadrian	/*
5242227364Sadrian	 * XXX dirty!
5243227364Sadrian	 * Slide the BAW left edge to wherever net80211 left it for us.
5244227364Sadrian	 * Read above for more information.
5245227364Sadrian	 */
5246227364Sadrian	tap->txa_start = ni->ni_txseqs[tid];
5247227364Sadrian	ath_tx_tid_resume(sc, atid);
5248243786Sadrian	ATH_TX_UNLOCK(sc);
5249227364Sadrian	return r;
5250227364Sadrian}
5251227364Sadrian
5252227364Sadrian
5253227364Sadrian/*
5254227364Sadrian * Stop ADDBA on a queue.
5255237593Sadrian *
5256237593Sadrian * This can be called whilst BAR TX is currently active on the queue,
5257237593Sadrian * so make sure this is unblocked before continuing.
5258227364Sadrian */
5259227364Sadrianvoid
5260227364Sadrianath_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
5261227364Sadrian{
5262227364Sadrian	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
5263234324Sadrian	int tid = tap->txa_tid;
5264227364Sadrian	struct ath_node *an = ATH_NODE(ni);
5265227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
5266227364Sadrian
5267227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: called\n", __func__);
5268227364Sadrian
5269237593Sadrian	/*
5270237593Sadrian	 * Pause TID traffic early, so there aren't any races
5271237593Sadrian	 * Unblock the pending BAR held traffic, if it's currently paused.
5272237593Sadrian	 */
5273243786Sadrian	ATH_TX_LOCK(sc);
5274227364Sadrian	ath_tx_tid_pause(sc, atid);
5275237593Sadrian	if (atid->bar_wait) {
5276237593Sadrian		/*
5277237593Sadrian		 * bar_unsuspend() expects bar_tx == 1, as it should be
5278237593Sadrian		 * called from the TX completion path.  This quietens
5279237593Sadrian		 * the warning.  It's cleared for us anyway.
5280237593Sadrian		 */
5281237593Sadrian		atid->bar_tx = 1;
5282237593Sadrian		ath_tx_tid_bar_unsuspend(sc, atid);
5283237593Sadrian	}
5284243786Sadrian	ATH_TX_UNLOCK(sc);
5285227364Sadrian
5286227364Sadrian	/* There's no need to hold the TXQ lock here */
5287227364Sadrian	sc->sc_addba_stop(ni, tap);
5288227364Sadrian
5289227364Sadrian	/*
5290235749Sadrian	 * ath_tx_tid_cleanup will resume the TID if possible, otherwise
5291227364Sadrian	 * it'll set the cleanup flag, and it'll be unpaused once
5292227364Sadrian	 * things have been cleaned up.
5293227364Sadrian	 */
5294235749Sadrian	ath_tx_tid_cleanup(sc, an, tid);
5295227364Sadrian}
5296227364Sadrian
5297227364Sadrian/*
5298227364Sadrian * Note: net80211 bar_timeout() doesn't call this function on BAR failure;
5299227364Sadrian * it simply tears down the aggregation session. Ew.
5300227364Sadrian *
5301227364Sadrian * It however will call ieee80211_ampdu_stop() which will call
5302227364Sadrian * ic->ic_addba_stop().
5303227364Sadrian *
5304227364Sadrian * XXX This uses a hard-coded max BAR count value; the whole
5305227364Sadrian * XXX BAR TX success or failure should be better handled!
5306227364Sadrian */
5307227364Sadrianvoid
5308227364Sadrianath_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
5309227364Sadrian    int status)
5310227364Sadrian{
5311227364Sadrian	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
5312234324Sadrian	int tid = tap->txa_tid;
5313227364Sadrian	struct ath_node *an = ATH_NODE(ni);
5314227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
5315227364Sadrian	int attempts = tap->txa_attempts;
5316227364Sadrian
5317235491Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
5318235676Sadrian	    "%s: called; tap=%p, atid=%p, txa_tid=%d, atid->tid=%d, status=%d, attempts=%d\n",
5319235491Sadrian	    __func__,
5320235676Sadrian	    tap,
5321235676Sadrian	    atid,
5322235676Sadrian	    tap->txa_tid,
5323235676Sadrian	    atid->tid,
5324235491Sadrian	    status,
5325235491Sadrian	    attempts);
5326227364Sadrian
5327227364Sadrian	/* Note: This may update the BAW details */
5328227364Sadrian	sc->sc_bar_response(ni, tap, status);
5329227364Sadrian
5330227364Sadrian	/* Unpause the TID */
5331227364Sadrian	/*
5332227364Sadrian	 * XXX if this is attempt=50, the TID will be downgraded
5333227364Sadrian	 * XXX to a non-aggregate session. So we must unpause the
5334227364Sadrian	 * XXX TID here or it'll never be done.
5335240721Sadrian	 *
5336240721Sadrian	 * Also, don't call it if bar_tx/bar_wait are 0; something
5337240721Sadrian	 * has beaten us to the punch? (XXX figure out what?)
5338227364Sadrian	 */
5339227364Sadrian	if (status == 0 || attempts == 50) {
5340243786Sadrian		ATH_TX_LOCK(sc);
5341240721Sadrian		if (atid->bar_tx == 0 || atid->bar_wait == 0)
5342240721Sadrian			device_printf(sc->sc_dev,
5343240721Sadrian			    "%s: huh? bar_tx=%d, bar_wait=%d\n",
5344240721Sadrian			    __func__,
5345240721Sadrian			    atid->bar_tx, atid->bar_wait);
5346240721Sadrian		else
5347240721Sadrian			ath_tx_tid_bar_unsuspend(sc, atid);
5348243786Sadrian		ATH_TX_UNLOCK(sc);
5349227364Sadrian	}
5350227364Sadrian}
5351227364Sadrian
5352227364Sadrian/*
5353227364Sadrian * This is called whenever the pending ADDBA request times out.
5354227364Sadrian * Unpause and reschedule the TID.
5355227364Sadrian */
5356227364Sadrianvoid
5357227364Sadrianath_addba_response_timeout(struct ieee80211_node *ni,
5358227364Sadrian    struct ieee80211_tx_ampdu *tap)
5359227364Sadrian{
5360227364Sadrian	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
5361234324Sadrian	int tid = tap->txa_tid;
5362227364Sadrian	struct ath_node *an = ATH_NODE(ni);
5363227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
5364227364Sadrian
5365227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5366227364Sadrian	    "%s: called; resuming\n", __func__);
5367227364Sadrian
5368243786Sadrian	ATH_TX_LOCK(sc);
5369235774Sadrian	atid->addba_tx_pending = 0;
5370243786Sadrian	ATH_TX_UNLOCK(sc);
5371235774Sadrian
5372227364Sadrian	/* Note: This updates the aggregate state to (again) pending */
5373227364Sadrian	sc->sc_addba_response_timeout(ni, tap);
5374227364Sadrian
5375227364Sadrian	/* Unpause the TID; which reschedules it */
5376243786Sadrian	ATH_TX_LOCK(sc);
5377227364Sadrian	ath_tx_tid_resume(sc, atid);
5378243786Sadrian	ATH_TX_UNLOCK(sc);
5379227364Sadrian}
5380238710Sadrian
5381241170Sadrian/*
5382241170Sadrian * Check if a node is asleep or not.
5383241170Sadrian */
5384242271Sadrianint
5385241170Sadrianath_tx_node_is_asleep(struct ath_softc *sc, struct ath_node *an)
5386241170Sadrian{
5387241170Sadrian
5388241170Sadrian	ATH_NODE_LOCK_ASSERT(an);
5389241170Sadrian
5390241170Sadrian	return (an->an_is_powersave);
5391241170Sadrian}
5392241170Sadrian
5393241170Sadrian/*
5394241170Sadrian * Mark a node as currently "in powersaving."
5395241170Sadrian * This suspends all traffic on the node.
5396241170Sadrian *
5397241170Sadrian * This must be called with the node/tx locks free.
5398241170Sadrian *
5399241170Sadrian * XXX TODO: the locking silliness below is due to how the node
5400241170Sadrian * locking currently works.  Right now, the node lock is grabbed
5401241170Sadrian * to do rate control lookups and these are done with the TX
5402241170Sadrian * queue lock held.  This means the node lock can't be grabbed
5403241170Sadrian * first here or a LOR will occur.
5404241170Sadrian *
5405241170Sadrian * Eventually (hopefully!) the TX path code will only grab
5406241170Sadrian * the TXQ lock when transmitting and the ath_node lock when
5407241170Sadrian * doing node/TID operations.  There are other complications -
5408241170Sadrian * the sched/unsched operations involve walking the per-txq
5409241170Sadrian * 'active tid' list and this requires both locks to be held.
5410241170Sadrian */
5411241170Sadrianvoid
5412241170Sadrianath_tx_node_sleep(struct ath_softc *sc, struct ath_node *an)
5413241170Sadrian{
5414241170Sadrian	struct ath_tid *atid;
5415241170Sadrian	struct ath_txq *txq;
5416241170Sadrian	int tid;
5417241170Sadrian
5418241170Sadrian	ATH_NODE_UNLOCK_ASSERT(an);
5419241170Sadrian
5420241170Sadrian	/*
5421241170Sadrian	 * It's possible that a parallel call to ath_tx_node_wakeup()
5422241170Sadrian	 * will unpause these queues.
5423241170Sadrian	 *
5424241170Sadrian	 * The node lock can't just be grabbed here, as there's places
5425241170Sadrian	 * in the driver where the node lock is grabbed _within_ a
5426241170Sadrian	 * TXQ lock.
5427241170Sadrian	 * So, we do this delicately and unwind state if needed.
5428241170Sadrian	 *
5429241170Sadrian	 * + Pause all the queues
5430241170Sadrian	 * + Grab the node lock
5431241170Sadrian	 * + If the queue is already asleep, unpause and quit
5432241170Sadrian	 * + else just mark as asleep.
5433241170Sadrian	 *
5434241170Sadrian	 * A parallel sleep() call will just pause and then
5435241170Sadrian	 * find they're already paused, so undo it.
5436241170Sadrian	 *
5437241170Sadrian	 * A parallel wakeup() call will check if asleep is 1
5438241170Sadrian	 * and if it's not (ie, it's 0), it'll treat it as already
5439241170Sadrian	 * being awake. If it's 1, it'll mark it as 0 and then
5440241170Sadrian	 * unpause everything.
5441241170Sadrian	 *
5442241170Sadrian	 * (Talk about a delicate hack.)
5443241170Sadrian	 */
5444241170Sadrian
5445241170Sadrian	/* Suspend all traffic on the node */
5446243786Sadrian	ATH_TX_LOCK(sc);
5447241170Sadrian	for (tid = 0; tid < IEEE80211_TID_SIZE; tid++) {
5448241170Sadrian		atid = &an->an_tid[tid];
5449241170Sadrian		txq = sc->sc_ac2q[atid->ac];
5450241170Sadrian
5451241170Sadrian		ath_tx_tid_pause(sc, atid);
5452241170Sadrian	}
5453243786Sadrian	ATH_TX_UNLOCK(sc);
5454241170Sadrian
5455241170Sadrian	ATH_NODE_LOCK(an);
5456241170Sadrian
5457241170Sadrian	/* In case of concurrency races from net80211.. */
5458241170Sadrian	if (an->an_is_powersave == 1) {
5459241170Sadrian		ATH_NODE_UNLOCK(an);
5460241170Sadrian		device_printf(sc->sc_dev,
5461241170Sadrian		    "%s: an=%p: node was already asleep\n",
5462241170Sadrian		    __func__, an);
5463243786Sadrian		ATH_TX_LOCK(sc);
5464241170Sadrian		for (tid = 0; tid < IEEE80211_TID_SIZE; tid++) {
5465241170Sadrian			atid = &an->an_tid[tid];
5466241170Sadrian			txq = sc->sc_ac2q[atid->ac];
5467241170Sadrian
5468241170Sadrian			ath_tx_tid_resume(sc, atid);
5469241170Sadrian		}
5470243786Sadrian		ATH_TX_UNLOCK(sc);
5471241170Sadrian		return;
5472241170Sadrian	}
5473241170Sadrian
5474241170Sadrian	/* Mark node as in powersaving */
5475241170Sadrian	an->an_is_powersave = 1;
5476241170Sadrian
5477241170Sadrian	ATH_NODE_UNLOCK(an);
5478241170Sadrian}
5479241170Sadrian
5480241170Sadrian/*
5481241170Sadrian * Mark a node as currently "awake."
5482241170Sadrian * This resumes all traffic to the node.
5483241170Sadrian */
5484241170Sadrianvoid
5485241170Sadrianath_tx_node_wakeup(struct ath_softc *sc, struct ath_node *an)
5486241170Sadrian{
5487241170Sadrian	struct ath_tid *atid;
5488241170Sadrian	struct ath_txq *txq;
5489241170Sadrian	int tid;
5490241170Sadrian
5491241170Sadrian	ATH_NODE_UNLOCK_ASSERT(an);
5492241170Sadrian	ATH_NODE_LOCK(an);
5493241170Sadrian
5494241170Sadrian	/* In case of concurrency races from net80211.. */
5495241170Sadrian	if (an->an_is_powersave == 0) {
5496241170Sadrian		ATH_NODE_UNLOCK(an);
5497241170Sadrian		device_printf(sc->sc_dev,
5498241170Sadrian		    "%s: an=%p: node was already awake\n",
5499241170Sadrian		    __func__, an);
5500241170Sadrian		return;
5501241170Sadrian	}
5502241170Sadrian
5503241170Sadrian	/* Mark node as awake */
5504241170Sadrian	an->an_is_powersave = 0;
5505241170Sadrian
5506241170Sadrian	ATH_NODE_UNLOCK(an);
5507241170Sadrian
5508243786Sadrian	ATH_TX_LOCK(sc);
5509241170Sadrian	for (tid = 0; tid < IEEE80211_TID_SIZE; tid++) {
5510241170Sadrian		atid = &an->an_tid[tid];
5511241170Sadrian		txq = sc->sc_ac2q[atid->ac];
5512241170Sadrian
5513241170Sadrian		ath_tx_tid_resume(sc, atid);
5514241170Sadrian	}
5515243786Sadrian	ATH_TX_UNLOCK(sc);
5516241170Sadrian}
5517241170Sadrian
5518241170Sadrianstatic int
5519238710Sadrianath_legacy_dma_txsetup(struct ath_softc *sc)
5520238710Sadrian{
5521238710Sadrian
5522238710Sadrian	/* nothing new needed */
5523238710Sadrian	return (0);
5524238710Sadrian}
5525238710Sadrian
5526238710Sadrianstatic int
5527238710Sadrianath_legacy_dma_txteardown(struct ath_softc *sc)
5528238710Sadrian{
5529238710Sadrian
5530238710Sadrian	/* nothing new needed */
5531238710Sadrian	return (0);
5532238710Sadrian}
5533238710Sadrian
5534238710Sadrianvoid
5535238710Sadrianath_xmit_setup_legacy(struct ath_softc *sc)
5536238710Sadrian{
5537238729Sadrian	/*
5538238729Sadrian	 * For now, just set the descriptor length to sizeof(ath_desc);
5539238729Sadrian	 * worry about extracting the real length out of the HAL later.
5540238729Sadrian	 */
5541238729Sadrian	sc->sc_tx_desclen = sizeof(struct ath_desc);
5542243162Sadrian	sc->sc_tx_statuslen = sizeof(struct ath_desc);
5543238729Sadrian	sc->sc_tx_nmaps = 1;	/* only one buffer per TX desc */
5544238710Sadrian
5545238710Sadrian	sc->sc_tx.xmit_setup = ath_legacy_dma_txsetup;
5546238710Sadrian	sc->sc_tx.xmit_teardown = ath_legacy_dma_txteardown;
5547238931Sadrian	sc->sc_tx.xmit_attach_comp_func = ath_legacy_attach_comp_func;
5548238930Sadrian
5549238930Sadrian	sc->sc_tx.xmit_dma_restart = ath_legacy_tx_dma_restart;
5550238930Sadrian	sc->sc_tx.xmit_handoff = ath_legacy_xmit_handoff;
5551239204Sadrian
5552239204Sadrian	sc->sc_tx.xmit_drain = ath_legacy_tx_drain;
5553238710Sadrian}
5554