if_ath_tx.c revision 248988
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 248988 2013-04-01 20:57:13Z 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 */
315248985Sadrian		bf->bf_nseg = ATH_MAX_SCATTER + 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	 */
326248985Sadrian	if (bf->bf_nseg > ATH_MAX_SCATTER) {		/* too many desc's, linearize */
327218065Sadrian		sc->sc_stats.ast_tx_linear++;
328248985Sadrian		m = m_collapse(m0, M_NOWAIT, ATH_MAX_SCATTER);
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		}
343248985Sadrian		KASSERT(bf->bf_nseg <= ATH_MAX_SCATTER,
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);
707248713Sadrian	if (ATH_TXQ_LAST(txq, axq_q_s) != NULL) {
708248713Sadrian		struct ath_buf *bf_last = ATH_TXQ_LAST(txq, axq_q_s);
709227364Sadrian		struct ieee80211_frame *wh;
710227364Sadrian
711227364Sadrian		/* mark previous frame */
712248713Sadrian		wh = mtod(bf_last->bf_m, struct ieee80211_frame *);
713227364Sadrian		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
714248713Sadrian		bus_dmamap_sync(sc->sc_dmat, bf_last->bf_dmamap,
715227364Sadrian		    BUS_DMASYNC_PREWRITE);
716227364Sadrian
717227364Sadrian		/* link descriptor */
718248713Sadrian		ath_hal_settxdesclink(sc->sc_ah,
719248713Sadrian		    bf_last->bf_lastds,
720248713Sadrian		    bf->bf_daddr);
721227364Sadrian	}
722227364Sadrian	ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
723248671Sadrian	ATH_TXQ_UNLOCK(txq);
724227364Sadrian}
725227364Sadrian
726227364Sadrian/*
727227364Sadrian * Hand-off packet to a hardware queue.
728227364Sadrian */
729227364Sadrianstatic void
730229949Sadrianath_tx_handoff_hw(struct ath_softc *sc, struct ath_txq *txq,
731229949Sadrian    struct ath_buf *bf)
732227364Sadrian{
733227364Sadrian	struct ath_hal *ah = sc->sc_ah;
734227364Sadrian
735227364Sadrian	/*
736218065Sadrian	 * Insert the frame on the outbound list and pass it on
737218065Sadrian	 * to the hardware.  Multicast frames buffered for power
738218065Sadrian	 * save stations and transmit from the CAB queue are stored
739218065Sadrian	 * on a s/w only queue and loaded on to the CAB queue in
740218065Sadrian	 * the SWBA handler since frames only go out on DTIM and
741218065Sadrian	 * to avoid possible races.
742218065Sadrian	 */
743243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
744218065Sadrian	KASSERT((bf->bf_flags & ATH_BUF_BUSY) == 0,
745227364Sadrian	     ("%s: busy status 0x%x", __func__, bf->bf_flags));
746227364Sadrian	KASSERT(txq->axq_qnum != ATH_TXQ_SWQ,
747227364Sadrian	     ("ath_tx_handoff_hw called for mcast queue"));
748227364Sadrian
749227651Sadrian#if 0
750227651Sadrian	/*
751227651Sadrian	 * This causes a LOR. Find out where the PCU lock is being
752227651Sadrian	 * held whilst the TXQ lock is grabbed - that shouldn't
753227651Sadrian	 * be occuring.
754227651Sadrian	 */
755227651Sadrian	ATH_PCU_LOCK(sc);
756227651Sadrian	if (sc->sc_inreset_cnt) {
757227651Sadrian		ATH_PCU_UNLOCK(sc);
758227651Sadrian		DPRINTF(sc, ATH_DEBUG_RESET,
759227651Sadrian		    "%s: called with sc_in_reset != 0\n",
760227651Sadrian		    __func__);
761227651Sadrian		DPRINTF(sc, ATH_DEBUG_XMIT,
762227651Sadrian		    "%s: queued: TXDP[%u] = %p (%p) depth %d\n",
763227651Sadrian		    __func__, txq->axq_qnum,
764227651Sadrian		    (caddr_t)bf->bf_daddr, bf->bf_desc,
765227651Sadrian		    txq->axq_depth);
766227651Sadrian		ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
767227651Sadrian		if (bf->bf_state.bfs_aggr)
768227651Sadrian			txq->axq_aggr_depth++;
769227651Sadrian		/*
770227651Sadrian		 * There's no need to update axq_link; the hardware
771227651Sadrian		 * is in reset and once the reset is complete, any
772227651Sadrian		 * non-empty queues will simply have DMA restarted.
773227651Sadrian		 */
774227651Sadrian		return;
775227651Sadrian		}
776227651Sadrian	ATH_PCU_UNLOCK(sc);
777227651Sadrian#endif
778227651Sadrian
779227364Sadrian	/* For now, so not to generate whitespace diffs */
780227364Sadrian	if (1) {
781248671Sadrian		ATH_TXQ_LOCK(txq);
782218065Sadrian#ifdef IEEE80211_SUPPORT_TDMA
783218065Sadrian		int qbusy;
784218065Sadrian
785218065Sadrian		ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
786218065Sadrian		qbusy = ath_hal_txqenabled(ah, txq->axq_qnum);
787240899Sadrian
788240899Sadrian		ATH_KTR(sc, ATH_KTR_TX, 4,
789240899Sadrian		    "ath_tx_handoff: txq=%u, add bf=%p, qbusy=%d, depth=%d",
790240899Sadrian		    txq->axq_qnum, bf, qbusy, txq->axq_depth);
791218065Sadrian		if (txq->axq_link == NULL) {
792218065Sadrian			/*
793218065Sadrian			 * Be careful writing the address to TXDP.  If
794218065Sadrian			 * the tx q is enabled then this write will be
795218065Sadrian			 * ignored.  Normally this is not an issue but
796218065Sadrian			 * when tdma is in use and the q is beacon gated
797218065Sadrian			 * this race can occur.  If the q is busy then
798218065Sadrian			 * defer the work to later--either when another
799218065Sadrian			 * packet comes along or when we prepare a beacon
800218065Sadrian			 * frame at SWBA.
801218065Sadrian			 */
802218065Sadrian			if (!qbusy) {
803229949Sadrian				ath_hal_puttxbuf(ah, txq->axq_qnum,
804229949Sadrian				    bf->bf_daddr);
805218065Sadrian				txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
806218065Sadrian				DPRINTF(sc, ATH_DEBUG_XMIT,
807240899Sadrian				    "%s: TXDP[%u] = %p (%p) lastds=%p depth %d\n",
808218065Sadrian				    __func__, txq->axq_qnum,
809218065Sadrian				    (caddr_t)bf->bf_daddr, bf->bf_desc,
810240899Sadrian				    bf->bf_lastds,
811218065Sadrian				    txq->axq_depth);
812240899Sadrian				ATH_KTR(sc, ATH_KTR_TX, 5,
813240899Sadrian				    "ath_tx_handoff: TXDP[%u] = %p (%p) "
814240899Sadrian				    "lastds=%p depth %d",
815240899Sadrian				    txq->axq_qnum,
816240899Sadrian				    (caddr_t)bf->bf_daddr, bf->bf_desc,
817240899Sadrian				    bf->bf_lastds,
818240899Sadrian				    txq->axq_depth);
819218065Sadrian			} else {
820218065Sadrian				txq->axq_flags |= ATH_TXQ_PUTPENDING;
821218065Sadrian				DPRINTF(sc, ATH_DEBUG_TDMA | ATH_DEBUG_XMIT,
822218065Sadrian				    "%s: Q%u busy, defer enable\n", __func__,
823218065Sadrian				    txq->axq_qnum);
824240899Sadrian				ATH_KTR(sc, ATH_KTR_TX, 0, "defer enable");
825218065Sadrian			}
826218065Sadrian		} else {
827218065Sadrian			*txq->axq_link = bf->bf_daddr;
828218065Sadrian			DPRINTF(sc, ATH_DEBUG_XMIT,
829218065Sadrian			    "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
830218065Sadrian			    txq->axq_qnum, txq->axq_link,
831229949Sadrian			    (caddr_t)bf->bf_daddr, bf->bf_desc,
832229949Sadrian			    txq->axq_depth);
833240899Sadrian			ATH_KTR(sc, ATH_KTR_TX, 5,
834240899Sadrian			    "ath_tx_handoff: link[%u](%p)=%p (%p) lastds=%p",
835240899Sadrian			    txq->axq_qnum, txq->axq_link,
836240899Sadrian			    (caddr_t)bf->bf_daddr, bf->bf_desc,
837240899Sadrian			    bf->bf_lastds);
838240899Sadrian
839218065Sadrian			if ((txq->axq_flags & ATH_TXQ_PUTPENDING) && !qbusy) {
840218065Sadrian				/*
841218065Sadrian				 * The q was busy when we previously tried
842218065Sadrian				 * to write the address of the first buffer
843218065Sadrian				 * in the chain.  Since it's not busy now
844218065Sadrian				 * handle this chore.  We are certain the
845218065Sadrian				 * buffer at the front is the right one since
846218065Sadrian				 * axq_link is NULL only when the buffer list
847218065Sadrian				 * is/was empty.
848218065Sadrian				 */
849218065Sadrian				ath_hal_puttxbuf(ah, txq->axq_qnum,
850227344Sadrian					TAILQ_FIRST(&txq->axq_q)->bf_daddr);
851218065Sadrian				txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
852218065Sadrian				DPRINTF(sc, ATH_DEBUG_TDMA | ATH_DEBUG_XMIT,
853218065Sadrian				    "%s: Q%u restarted\n", __func__,
854218065Sadrian				    txq->axq_qnum);
855240899Sadrian				ATH_KTR(sc, ATH_KTR_TX, 4,
856240899Sadrian				  "ath_tx_handoff: txq[%d] restarted, bf=%p "
857240899Sadrian				  "daddr=%p ds=%p",
858240899Sadrian				    txq->axq_qnum,
859240899Sadrian				    bf,
860240899Sadrian				    (caddr_t)bf->bf_daddr,
861240899Sadrian				    bf->bf_desc);
862218065Sadrian			}
863218065Sadrian		}
864218065Sadrian#else
865218065Sadrian		ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
866241500Sadrian		ATH_KTR(sc, ATH_KTR_TX, 3,
867241500Sadrian		    "ath_tx_handoff: non-tdma: txq=%u, add bf=%p "
868240899Sadrian		    "depth=%d",
869240899Sadrian		    txq->axq_qnum,
870240899Sadrian		    bf,
871240899Sadrian		    txq->axq_depth);
872218065Sadrian		if (txq->axq_link == NULL) {
873218065Sadrian			ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
874218065Sadrian			DPRINTF(sc, ATH_DEBUG_XMIT,
875218065Sadrian			    "%s: TXDP[%u] = %p (%p) depth %d\n",
876218065Sadrian			    __func__, txq->axq_qnum,
877218065Sadrian			    (caddr_t)bf->bf_daddr, bf->bf_desc,
878218065Sadrian			    txq->axq_depth);
879240899Sadrian			ATH_KTR(sc, ATH_KTR_TX, 5,
880240899Sadrian			    "ath_tx_handoff: non-tdma: TXDP[%u] = %p (%p) "
881240899Sadrian			    "lastds=%p depth %d",
882240899Sadrian			    txq->axq_qnum,
883240899Sadrian			    (caddr_t)bf->bf_daddr, bf->bf_desc,
884240899Sadrian			    bf->bf_lastds,
885240899Sadrian			    txq->axq_depth);
886240899Sadrian
887218065Sadrian		} else {
888218065Sadrian			*txq->axq_link = bf->bf_daddr;
889218065Sadrian			DPRINTF(sc, ATH_DEBUG_XMIT,
890218065Sadrian			    "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
891218065Sadrian			    txq->axq_qnum, txq->axq_link,
892229949Sadrian			    (caddr_t)bf->bf_daddr, bf->bf_desc,
893229949Sadrian			    txq->axq_depth);
894240899Sadrian			ATH_KTR(sc, ATH_KTR_TX, 5,
895240899Sadrian			    "ath_tx_handoff: non-tdma: link[%u](%p)=%p (%p) "
896240899Sadrian			    "lastds=%d",
897240899Sadrian			    txq->axq_qnum, txq->axq_link,
898240899Sadrian			    (caddr_t)bf->bf_daddr, bf->bf_desc,
899240899Sadrian			    bf->bf_lastds);
900240899Sadrian
901218065Sadrian		}
902218065Sadrian#endif /* IEEE80211_SUPPORT_TDMA */
903227360Sadrian		if (bf->bf_state.bfs_aggr)
904227360Sadrian			txq->axq_aggr_depth++;
905238609Sadrian		ath_hal_gettxdesclinkptr(ah, bf->bf_lastds, &txq->axq_link);
906218065Sadrian		ath_hal_txstart(ah, txq->axq_qnum);
907248671Sadrian		ATH_TXQ_UNLOCK(txq);
908240899Sadrian		ATH_KTR(sc, ATH_KTR_TX, 1,
909240899Sadrian		    "ath_tx_handoff: txq=%u, txstart", txq->axq_qnum);
910227364Sadrian	}
911227364Sadrian}
912218065Sadrian
913227364Sadrian/*
914227364Sadrian * Restart TX DMA for the given TXQ.
915227364Sadrian *
916227364Sadrian * This must be called whether the queue is empty or not.
917227364Sadrian */
918238930Sadrianstatic void
919238930Sadrianath_legacy_tx_dma_restart(struct ath_softc *sc, struct ath_txq *txq)
920227364Sadrian{
921227364Sadrian	struct ath_hal *ah = sc->sc_ah;
922232707Sadrian	struct ath_buf *bf, *bf_last;
923218065Sadrian
924248671Sadrian	ATH_TXQ_LOCK_ASSERT(txq);
925227364Sadrian	/* This is always going to be cleared, empty or not */
926227364Sadrian	txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
927227364Sadrian
928232707Sadrian	/* XXX make this ATH_TXQ_FIRST */
929227364Sadrian	bf = TAILQ_FIRST(&txq->axq_q);
930232707Sadrian	bf_last = ATH_TXQ_LAST(txq, axq_q_s);
931232707Sadrian
932227364Sadrian	if (bf == NULL)
933227364Sadrian		return;
934227364Sadrian
935227364Sadrian	ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
936239120Sadrian	ath_hal_gettxdesclinkptr(ah, bf_last->bf_lastds, &txq->axq_link);
937227364Sadrian	ath_hal_txstart(ah, txq->axq_qnum);
938218065Sadrian}
939218065Sadrian
940227364Sadrian/*
941227364Sadrian * Hand off a packet to the hardware (or mcast queue.)
942227364Sadrian *
943227364Sadrian * The relevant hardware txq should be locked.
944227364Sadrian */
945227364Sadrianstatic void
946238930Sadrianath_legacy_xmit_handoff(struct ath_softc *sc, struct ath_txq *txq,
947238930Sadrian    struct ath_buf *bf)
948227364Sadrian{
949243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
950227364Sadrian
951243162Sadrian#ifdef	ATH_DEBUG_ALQ
952243162Sadrian	if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_EDMA_TXDESC))
953243162Sadrian		ath_tx_alq_post(sc, bf);
954243162Sadrian#endif
955243162Sadrian
956227364Sadrian	if (txq->axq_qnum == ATH_TXQ_SWQ)
957227364Sadrian		ath_tx_handoff_mcast(sc, txq, bf);
958227364Sadrian	else
959227364Sadrian		ath_tx_handoff_hw(sc, txq, bf);
960227364Sadrian}
961227364Sadrian
962218154Sadrianstatic int
963218154Sadrianath_tx_tag_crypto(struct ath_softc *sc, struct ieee80211_node *ni,
964229949Sadrian    struct mbuf *m0, int iswep, int isfrag, int *hdrlen, int *pktlen,
965229949Sadrian    int *keyix)
966218154Sadrian{
967233330Sadrian	DPRINTF(sc, ATH_DEBUG_XMIT,
968233330Sadrian	    "%s: hdrlen=%d, pktlen=%d, isfrag=%d, iswep=%d, m0=%p\n",
969233330Sadrian	    __func__,
970233330Sadrian	    *hdrlen,
971233330Sadrian	    *pktlen,
972233330Sadrian	    isfrag,
973233330Sadrian	    iswep,
974233330Sadrian	    m0);
975233330Sadrian
976218154Sadrian	if (iswep) {
977218154Sadrian		const struct ieee80211_cipher *cip;
978218154Sadrian		struct ieee80211_key *k;
979218154Sadrian
980218154Sadrian		/*
981218154Sadrian		 * Construct the 802.11 header+trailer for an encrypted
982218154Sadrian		 * frame. The only reason this can fail is because of an
983218154Sadrian		 * unknown or unsupported cipher/key type.
984218154Sadrian		 */
985218154Sadrian		k = ieee80211_crypto_encap(ni, m0);
986218154Sadrian		if (k == NULL) {
987218154Sadrian			/*
988218154Sadrian			 * This can happen when the key is yanked after the
989218154Sadrian			 * frame was queued.  Just discard the frame; the
990218154Sadrian			 * 802.11 layer counts failures and provides
991218154Sadrian			 * debugging/diagnostics.
992218154Sadrian			 */
993229949Sadrian			return (0);
994218154Sadrian		}
995218154Sadrian		/*
996218154Sadrian		 * Adjust the packet + header lengths for the crypto
997218154Sadrian		 * additions and calculate the h/w key index.  When
998218154Sadrian		 * a s/w mic is done the frame will have had any mic
999218154Sadrian		 * added to it prior to entry so m0->m_pkthdr.len will
1000218154Sadrian		 * account for it. Otherwise we need to add it to the
1001218154Sadrian		 * packet length.
1002218154Sadrian		 */
1003218154Sadrian		cip = k->wk_cipher;
1004218154Sadrian		(*hdrlen) += cip->ic_header;
1005218154Sadrian		(*pktlen) += cip->ic_header + cip->ic_trailer;
1006218154Sadrian		/* NB: frags always have any TKIP MIC done in s/w */
1007218154Sadrian		if ((k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && !isfrag)
1008218154Sadrian			(*pktlen) += cip->ic_miclen;
1009218154Sadrian		(*keyix) = k->wk_keyix;
1010218154Sadrian	} else if (ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) {
1011218154Sadrian		/*
1012218154Sadrian		 * Use station key cache slot, if assigned.
1013218154Sadrian		 */
1014218154Sadrian		(*keyix) = ni->ni_ucastkey.wk_keyix;
1015218154Sadrian		if ((*keyix) == IEEE80211_KEYIX_NONE)
1016218154Sadrian			(*keyix) = HAL_TXKEYIX_INVALID;
1017218154Sadrian	} else
1018218154Sadrian		(*keyix) = HAL_TXKEYIX_INVALID;
1019218154Sadrian
1020229949Sadrian	return (1);
1021218154Sadrian}
1022218154Sadrian
1023233989Sadrian/*
1024233989Sadrian * Calculate whether interoperability protection is required for
1025233989Sadrian * this frame.
1026233989Sadrian *
1027233989Sadrian * This requires the rate control information be filled in,
1028233989Sadrian * as the protection requirement depends upon the current
1029233989Sadrian * operating mode / PHY.
1030233989Sadrian */
1031233989Sadrianstatic void
1032233989Sadrianath_tx_calc_protection(struct ath_softc *sc, struct ath_buf *bf)
1033233989Sadrian{
1034233989Sadrian	struct ieee80211_frame *wh;
1035233989Sadrian	uint8_t rix;
1036233989Sadrian	uint16_t flags;
1037233989Sadrian	int shortPreamble;
1038233989Sadrian	const HAL_RATE_TABLE *rt = sc->sc_currates;
1039233989Sadrian	struct ifnet *ifp = sc->sc_ifp;
1040233989Sadrian	struct ieee80211com *ic = ifp->if_l2com;
1041233989Sadrian
1042233989Sadrian	flags = bf->bf_state.bfs_txflags;
1043233989Sadrian	rix = bf->bf_state.bfs_rc[0].rix;
1044233989Sadrian	shortPreamble = bf->bf_state.bfs_shpream;
1045233989Sadrian	wh = mtod(bf->bf_m, struct ieee80211_frame *);
1046233989Sadrian
1047233989Sadrian	/*
1048233989Sadrian	 * If 802.11g protection is enabled, determine whether
1049233989Sadrian	 * to use RTS/CTS or just CTS.  Note that this is only
1050233989Sadrian	 * done for OFDM unicast frames.
1051233989Sadrian	 */
1052233989Sadrian	if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1053233989Sadrian	    rt->info[rix].phy == IEEE80211_T_OFDM &&
1054233989Sadrian	    (flags & HAL_TXDESC_NOACK) == 0) {
1055233989Sadrian		bf->bf_state.bfs_doprot = 1;
1056233989Sadrian		/* XXX fragments must use CCK rates w/ protection */
1057233989Sadrian		if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) {
1058233989Sadrian			flags |= HAL_TXDESC_RTSENA;
1059233989Sadrian		} else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) {
1060233989Sadrian			flags |= HAL_TXDESC_CTSENA;
1061233989Sadrian		}
1062233989Sadrian		/*
1063233989Sadrian		 * For frags it would be desirable to use the
1064233989Sadrian		 * highest CCK rate for RTS/CTS.  But stations
1065233989Sadrian		 * farther away may detect it at a lower CCK rate
1066233989Sadrian		 * so use the configured protection rate instead
1067233989Sadrian		 * (for now).
1068233989Sadrian		 */
1069233989Sadrian		sc->sc_stats.ast_tx_protect++;
1070233989Sadrian	}
1071233989Sadrian
1072233989Sadrian	/*
1073233989Sadrian	 * If 11n protection is enabled and it's a HT frame,
1074233989Sadrian	 * enable RTS.
1075233989Sadrian	 *
1076233989Sadrian	 * XXX ic_htprotmode or ic_curhtprotmode?
1077233989Sadrian	 * XXX should it_htprotmode only matter if ic_curhtprotmode
1078233989Sadrian	 * XXX indicates it's not a HT pure environment?
1079233989Sadrian	 */
1080233989Sadrian	if ((ic->ic_htprotmode == IEEE80211_PROT_RTSCTS) &&
1081233989Sadrian	    rt->info[rix].phy == IEEE80211_T_HT &&
1082233989Sadrian	    (flags & HAL_TXDESC_NOACK) == 0) {
1083233989Sadrian		flags |= HAL_TXDESC_RTSENA;
1084233989Sadrian		sc->sc_stats.ast_tx_htprotect++;
1085233989Sadrian	}
1086233989Sadrian	bf->bf_state.bfs_txflags = flags;
1087233989Sadrian}
1088233989Sadrian
1089233989Sadrian/*
1090233989Sadrian * Update the frame duration given the currently selected rate.
1091233989Sadrian *
1092233989Sadrian * This also updates the frame duration value, so it will require
1093233989Sadrian * a DMA flush.
1094233989Sadrian */
1095233989Sadrianstatic void
1096233989Sadrianath_tx_calc_duration(struct ath_softc *sc, struct ath_buf *bf)
1097233989Sadrian{
1098233989Sadrian	struct ieee80211_frame *wh;
1099233989Sadrian	uint8_t rix;
1100233989Sadrian	uint16_t flags;
1101233989Sadrian	int shortPreamble;
1102233989Sadrian	struct ath_hal *ah = sc->sc_ah;
1103233989Sadrian	const HAL_RATE_TABLE *rt = sc->sc_currates;
1104233989Sadrian	int isfrag = bf->bf_m->m_flags & M_FRAG;
1105233989Sadrian
1106233989Sadrian	flags = bf->bf_state.bfs_txflags;
1107233989Sadrian	rix = bf->bf_state.bfs_rc[0].rix;
1108233989Sadrian	shortPreamble = bf->bf_state.bfs_shpream;
1109233989Sadrian	wh = mtod(bf->bf_m, struct ieee80211_frame *);
1110233989Sadrian
1111233989Sadrian	/*
1112233989Sadrian	 * Calculate duration.  This logically belongs in the 802.11
1113233989Sadrian	 * layer but it lacks sufficient information to calculate it.
1114233989Sadrian	 */
1115233989Sadrian	if ((flags & HAL_TXDESC_NOACK) == 0 &&
1116233989Sadrian	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL) {
1117233989Sadrian		u_int16_t dur;
1118233989Sadrian		if (shortPreamble)
1119233989Sadrian			dur = rt->info[rix].spAckDuration;
1120233989Sadrian		else
1121233989Sadrian			dur = rt->info[rix].lpAckDuration;
1122233989Sadrian		if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) {
1123233989Sadrian			dur += dur;		/* additional SIFS+ACK */
1124246745Sadrian			KASSERT(bf->bf_m->m_nextpkt != NULL, ("no fragment"));
1125233989Sadrian			/*
1126233989Sadrian			 * Include the size of next fragment so NAV is
1127233989Sadrian			 * updated properly.  The last fragment uses only
1128233989Sadrian			 * the ACK duration
1129242144Sadrian			 *
1130242144Sadrian			 * XXX TODO: ensure that the rate lookup for each
1131242144Sadrian			 * fragment is the same as the rate used by the
1132242144Sadrian			 * first fragment!
1133233989Sadrian			 */
1134233989Sadrian			dur += ath_hal_computetxtime(ah, rt,
1135246745Sadrian					bf->bf_m->m_nextpkt->m_pkthdr.len,
1136233989Sadrian					rix, shortPreamble);
1137233989Sadrian		}
1138233989Sadrian		if (isfrag) {
1139233989Sadrian			/*
1140233989Sadrian			 * Force hardware to use computed duration for next
1141233989Sadrian			 * fragment by disabling multi-rate retry which updates
1142233989Sadrian			 * duration based on the multi-rate duration table.
1143233989Sadrian			 */
1144233989Sadrian			bf->bf_state.bfs_ismrr = 0;
1145233989Sadrian			bf->bf_state.bfs_try0 = ATH_TXMGTTRY;
1146233989Sadrian			/* XXX update bfs_rc[0].try? */
1147233989Sadrian		}
1148233989Sadrian
1149233989Sadrian		/* Update the duration field itself */
1150233989Sadrian		*(u_int16_t *)wh->i_dur = htole16(dur);
1151233989Sadrian	}
1152233989Sadrian}
1153233989Sadrian
1154218932Sadrianstatic uint8_t
1155218932Sadrianath_tx_get_rtscts_rate(struct ath_hal *ah, const HAL_RATE_TABLE *rt,
1156227364Sadrian    int cix, int shortPreamble)
1157218157Sadrian{
1158218932Sadrian	uint8_t ctsrate;
1159218932Sadrian
1160218157Sadrian	/*
1161218157Sadrian	 * CTS transmit rate is derived from the transmit rate
1162218157Sadrian	 * by looking in the h/w rate table.  We must also factor
1163218157Sadrian	 * in whether or not a short preamble is to be used.
1164218157Sadrian	 */
1165218157Sadrian	/* NB: cix is set above where RTS/CTS is enabled */
1166218157Sadrian	KASSERT(cix != 0xff, ("cix not setup"));
1167218932Sadrian	ctsrate = rt->info[cix].rateCode;
1168218932Sadrian
1169218932Sadrian	/* XXX this should only matter for legacy rates */
1170218932Sadrian	if (shortPreamble)
1171218932Sadrian		ctsrate |= rt->info[cix].shortPreamble;
1172218932Sadrian
1173229949Sadrian	return (ctsrate);
1174218932Sadrian}
1175218932Sadrian
1176218932Sadrian/*
1177218932Sadrian * Calculate the RTS/CTS duration for legacy frames.
1178218932Sadrian */
1179218932Sadrianstatic int
1180218932Sadrianath_tx_calc_ctsduration(struct ath_hal *ah, int rix, int cix,
1181218932Sadrian    int shortPreamble, int pktlen, const HAL_RATE_TABLE *rt,
1182218932Sadrian    int flags)
1183218932Sadrian{
1184218932Sadrian	int ctsduration = 0;
1185218932Sadrian
1186218932Sadrian	/* This mustn't be called for HT modes */
1187218932Sadrian	if (rt->info[cix].phy == IEEE80211_T_HT) {
1188218932Sadrian		printf("%s: HT rate where it shouldn't be (0x%x)\n",
1189218932Sadrian		    __func__, rt->info[cix].rateCode);
1190229949Sadrian		return (-1);
1191218932Sadrian	}
1192218932Sadrian
1193218157Sadrian	/*
1194218157Sadrian	 * Compute the transmit duration based on the frame
1195218157Sadrian	 * size and the size of an ACK frame.  We call into the
1196218157Sadrian	 * HAL to do the computation since it depends on the
1197218157Sadrian	 * characteristics of the actual PHY being used.
1198218157Sadrian	 *
1199218157Sadrian	 * NB: CTS is assumed the same size as an ACK so we can
1200218157Sadrian	 *     use the precalculated ACK durations.
1201218157Sadrian	 */
1202218157Sadrian	if (shortPreamble) {
1203218157Sadrian		if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
1204218932Sadrian			ctsduration += rt->info[cix].spAckDuration;
1205218932Sadrian		ctsduration += ath_hal_computetxtime(ah,
1206218157Sadrian			rt, pktlen, rix, AH_TRUE);
1207218157Sadrian		if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
1208218932Sadrian			ctsduration += rt->info[rix].spAckDuration;
1209218157Sadrian	} else {
1210218157Sadrian		if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
1211218932Sadrian			ctsduration += rt->info[cix].lpAckDuration;
1212218932Sadrian		ctsduration += ath_hal_computetxtime(ah,
1213218157Sadrian			rt, pktlen, rix, AH_FALSE);
1214218157Sadrian		if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
1215218932Sadrian			ctsduration += rt->info[rix].lpAckDuration;
1216218157Sadrian	}
1217218932Sadrian
1218229949Sadrian	return (ctsduration);
1219218157Sadrian}
1220218157Sadrian
1221227364Sadrian/*
1222227364Sadrian * Update the given ath_buf with updated rts/cts setup and duration
1223227364Sadrian * values.
1224227364Sadrian *
1225227364Sadrian * To support rate lookups for each software retry, the rts/cts rate
1226227364Sadrian * and cts duration must be re-calculated.
1227227364Sadrian *
1228227364Sadrian * This function assumes the RTS/CTS flags have been set as needed;
1229227364Sadrian * mrr has been disabled; and the rate control lookup has been done.
1230227364Sadrian *
1231227364Sadrian * XXX TODO: MRR need only be disabled for the pre-11n NICs.
1232227364Sadrian * XXX The 11n NICs support per-rate RTS/CTS configuration.
1233227364Sadrian */
1234227364Sadrianstatic void
1235227364Sadrianath_tx_set_rtscts(struct ath_softc *sc, struct ath_buf *bf)
1236218065Sadrian{
1237227364Sadrian	uint16_t ctsduration = 0;
1238227364Sadrian	uint8_t ctsrate = 0;
1239227364Sadrian	uint8_t rix = bf->bf_state.bfs_rc[0].rix;
1240227364Sadrian	uint8_t cix = 0;
1241227364Sadrian	const HAL_RATE_TABLE *rt = sc->sc_currates;
1242227364Sadrian
1243227364Sadrian	/*
1244227364Sadrian	 * No RTS/CTS enabled? Don't bother.
1245227364Sadrian	 */
1246233966Sadrian	if ((bf->bf_state.bfs_txflags &
1247227364Sadrian	    (HAL_TXDESC_RTSENA | HAL_TXDESC_CTSENA)) == 0) {
1248227364Sadrian		/* XXX is this really needed? */
1249227364Sadrian		bf->bf_state.bfs_ctsrate = 0;
1250227364Sadrian		bf->bf_state.bfs_ctsduration = 0;
1251227364Sadrian		return;
1252227364Sadrian	}
1253227364Sadrian
1254227364Sadrian	/*
1255227364Sadrian	 * If protection is enabled, use the protection rix control
1256227364Sadrian	 * rate. Otherwise use the rate0 control rate.
1257227364Sadrian	 */
1258227364Sadrian	if (bf->bf_state.bfs_doprot)
1259227364Sadrian		rix = sc->sc_protrix;
1260227364Sadrian	else
1261227364Sadrian		rix = bf->bf_state.bfs_rc[0].rix;
1262227364Sadrian
1263227364Sadrian	/*
1264227364Sadrian	 * If the raw path has hard-coded ctsrate0 to something,
1265227364Sadrian	 * use it.
1266227364Sadrian	 */
1267227364Sadrian	if (bf->bf_state.bfs_ctsrate0 != 0)
1268227364Sadrian		cix = ath_tx_findrix(sc, bf->bf_state.bfs_ctsrate0);
1269227364Sadrian	else
1270227364Sadrian		/* Control rate from above */
1271227364Sadrian		cix = rt->info[rix].controlRate;
1272227364Sadrian
1273227364Sadrian	/* Calculate the rtscts rate for the given cix */
1274227364Sadrian	ctsrate = ath_tx_get_rtscts_rate(sc->sc_ah, rt, cix,
1275227364Sadrian	    bf->bf_state.bfs_shpream);
1276227364Sadrian
1277227364Sadrian	/* The 11n chipsets do ctsduration calculations for you */
1278227364Sadrian	if (! ath_tx_is_11n(sc))
1279227364Sadrian		ctsduration = ath_tx_calc_ctsduration(sc->sc_ah, rix, cix,
1280227364Sadrian		    bf->bf_state.bfs_shpream, bf->bf_state.bfs_pktlen,
1281233966Sadrian		    rt, bf->bf_state.bfs_txflags);
1282227364Sadrian
1283227364Sadrian	/* Squirrel away in ath_buf */
1284227364Sadrian	bf->bf_state.bfs_ctsrate = ctsrate;
1285227364Sadrian	bf->bf_state.bfs_ctsduration = ctsduration;
1286227364Sadrian
1287227364Sadrian	/*
1288227364Sadrian	 * Must disable multi-rate retry when using RTS/CTS.
1289227364Sadrian	 */
1290238961Sadrian	if (!sc->sc_mrrprot) {
1291238961Sadrian		bf->bf_state.bfs_ismrr = 0;
1292238961Sadrian		bf->bf_state.bfs_try0 =
1293238961Sadrian		    bf->bf_state.bfs_rc[0].tries = ATH_TXMGTTRY; /* XXX ew */
1294238961Sadrian	}
1295227364Sadrian}
1296227364Sadrian
1297227364Sadrian/*
1298227364Sadrian * Setup the descriptor chain for a normal or fast-frame
1299227364Sadrian * frame.
1300239051Sadrian *
1301239051Sadrian * XXX TODO: extend to include the destination hardware QCU ID.
1302239051Sadrian * Make sure that is correct.  Make sure that when being added
1303239051Sadrian * to the mcastq, the CABQ QCUID is set or things will get a bit
1304239051Sadrian * odd.
1305227364Sadrian */
1306227364Sadrianstatic void
1307227364Sadrianath_tx_setds(struct ath_softc *sc, struct ath_buf *bf)
1308227364Sadrian{
1309227364Sadrian	struct ath_desc *ds = bf->bf_desc;
1310227364Sadrian	struct ath_hal *ah = sc->sc_ah;
1311227364Sadrian
1312242951Sadrian	if (bf->bf_state.bfs_txrate0 == 0)
1313242951Sadrian		device_printf(sc->sc_dev, "%s: bf=%p, txrate0=%d\n",
1314242951Sadrian		    __func__, bf, 0);
1315242951Sadrian
1316227364Sadrian	ath_hal_setuptxdesc(ah, ds
1317227364Sadrian		, bf->bf_state.bfs_pktlen	/* packet length */
1318227364Sadrian		, bf->bf_state.bfs_hdrlen	/* header length */
1319227364Sadrian		, bf->bf_state.bfs_atype	/* Atheros packet type */
1320227364Sadrian		, bf->bf_state.bfs_txpower	/* txpower */
1321227364Sadrian		, bf->bf_state.bfs_txrate0
1322227364Sadrian		, bf->bf_state.bfs_try0		/* series 0 rate/tries */
1323227364Sadrian		, bf->bf_state.bfs_keyix	/* key cache index */
1324227364Sadrian		, bf->bf_state.bfs_txantenna	/* antenna mode */
1325233966Sadrian		, bf->bf_state.bfs_txflags	/* flags */
1326227364Sadrian		, bf->bf_state.bfs_ctsrate	/* rts/cts rate */
1327227364Sadrian		, bf->bf_state.bfs_ctsduration	/* rts/cts duration */
1328227364Sadrian	);
1329227364Sadrian
1330227364Sadrian	/*
1331227364Sadrian	 * This will be overriden when the descriptor chain is written.
1332227364Sadrian	 */
1333227364Sadrian	bf->bf_lastds = ds;
1334227364Sadrian	bf->bf_last = bf;
1335227364Sadrian
1336238947Sadrian	/* Set rate control and descriptor chain for this frame */
1337238947Sadrian	ath_tx_set_ratectrl(sc, bf->bf_node, bf);
1338242656Sadrian	ath_tx_chaindesclist(sc, ds, bf, 0, 0, 0);
1339227364Sadrian}
1340227364Sadrian
1341227364Sadrian/*
1342227364Sadrian * Do a rate lookup.
1343227364Sadrian *
1344227364Sadrian * This performs a rate lookup for the given ath_buf only if it's required.
1345227364Sadrian * Non-data frames and raw frames don't require it.
1346227364Sadrian *
1347227364Sadrian * This populates the primary and MRR entries; MRR values are
1348227364Sadrian * then disabled later on if something requires it (eg RTS/CTS on
1349227364Sadrian * pre-11n chipsets.
1350227364Sadrian *
1351227364Sadrian * This needs to be done before the RTS/CTS fields are calculated
1352227364Sadrian * as they may depend upon the rate chosen.
1353227364Sadrian */
1354227364Sadrianstatic void
1355227364Sadrianath_tx_do_ratelookup(struct ath_softc *sc, struct ath_buf *bf)
1356227364Sadrian{
1357227364Sadrian	uint8_t rate, rix;
1358227364Sadrian	int try0;
1359227364Sadrian
1360227364Sadrian	if (! bf->bf_state.bfs_doratelookup)
1361227364Sadrian		return;
1362227364Sadrian
1363227364Sadrian	/* Get rid of any previous state */
1364227364Sadrian	bzero(bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
1365227364Sadrian
1366227364Sadrian	ATH_NODE_LOCK(ATH_NODE(bf->bf_node));
1367227364Sadrian	ath_rate_findrate(sc, ATH_NODE(bf->bf_node), bf->bf_state.bfs_shpream,
1368227364Sadrian	    bf->bf_state.bfs_pktlen, &rix, &try0, &rate);
1369227364Sadrian
1370227364Sadrian	/* In case MRR is disabled, make sure rc[0] is setup correctly */
1371227364Sadrian	bf->bf_state.bfs_rc[0].rix = rix;
1372227364Sadrian	bf->bf_state.bfs_rc[0].ratecode = rate;
1373227364Sadrian	bf->bf_state.bfs_rc[0].tries = try0;
1374227364Sadrian
1375227364Sadrian	if (bf->bf_state.bfs_ismrr && try0 != ATH_TXMAXTRY)
1376227364Sadrian		ath_rate_getxtxrates(sc, ATH_NODE(bf->bf_node), rix,
1377227364Sadrian		    bf->bf_state.bfs_rc);
1378227364Sadrian	ATH_NODE_UNLOCK(ATH_NODE(bf->bf_node));
1379227364Sadrian
1380227364Sadrian	sc->sc_txrix = rix;	/* for LED blinking */
1381227364Sadrian	sc->sc_lastdatarix = rix;	/* for fast frames */
1382227364Sadrian	bf->bf_state.bfs_try0 = try0;
1383227364Sadrian	bf->bf_state.bfs_txrate0 = rate;
1384227364Sadrian}
1385227364Sadrian
1386227364Sadrian/*
1387240883Sadrian * Update the CLRDMASK bit in the ath_buf if it needs to be set.
1388240883Sadrian */
1389240883Sadrianstatic void
1390240883Sadrianath_tx_update_clrdmask(struct ath_softc *sc, struct ath_tid *tid,
1391240883Sadrian    struct ath_buf *bf)
1392240883Sadrian{
1393245708Sadrian	struct ath_node *an = ATH_NODE(bf->bf_node);
1394240883Sadrian
1395243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
1396240883Sadrian
1397245708Sadrian	if (an->clrdmask == 1) {
1398240883Sadrian		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
1399245708Sadrian		an->clrdmask = 0;
1400240883Sadrian	}
1401240883Sadrian}
1402240883Sadrian
1403240883Sadrian/*
1404227364Sadrian * Transmit the given frame to the hardware.
1405227364Sadrian *
1406227364Sadrian * The frame must already be setup; rate control must already have
1407227364Sadrian * been done.
1408227364Sadrian *
1409227364Sadrian * XXX since the TXQ lock is being held here (and I dislike holding
1410227364Sadrian * it for this long when not doing software aggregation), later on
1411227364Sadrian * break this function into "setup_normal" and "xmit_normal". The
1412227364Sadrian * lock only needs to be held for the ath_tx_handoff call.
1413227364Sadrian */
1414227364Sadrianstatic void
1415227364Sadrianath_tx_xmit_normal(struct ath_softc *sc, struct ath_txq *txq,
1416227364Sadrian    struct ath_buf *bf)
1417227364Sadrian{
1418240883Sadrian	struct ath_node *an = ATH_NODE(bf->bf_node);
1419240883Sadrian	struct ath_tid *tid = &an->an_tid[bf->bf_state.bfs_tid];
1420227364Sadrian
1421243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
1422227364Sadrian
1423240883Sadrian	/*
1424240883Sadrian	 * For now, just enable CLRDMASK. ath_tx_xmit_normal() does
1425240883Sadrian	 * set a completion handler however it doesn't (yet) properly
1426240883Sadrian	 * handle the strict ordering requirements needed for normal,
1427240883Sadrian	 * non-aggregate session frames.
1428240883Sadrian	 *
1429240883Sadrian	 * Once this is implemented, only set CLRDMASK like this for
1430240883Sadrian	 * frames that must go out - eg management/raw frames.
1431240883Sadrian	 */
1432240883Sadrian	bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
1433240883Sadrian
1434227364Sadrian	/* Setup the descriptor before handoff */
1435227364Sadrian	ath_tx_do_ratelookup(sc, bf);
1436233989Sadrian	ath_tx_calc_duration(sc, bf);
1437233989Sadrian	ath_tx_calc_protection(sc, bf);
1438233989Sadrian	ath_tx_set_rtscts(sc, bf);
1439227364Sadrian	ath_tx_rate_fill_rcflags(sc, bf);
1440227364Sadrian	ath_tx_setds(sc, bf);
1441227364Sadrian
1442240883Sadrian	/* Track per-TID hardware queue depth correctly */
1443240883Sadrian	tid->hwq_depth++;
1444240724Sadrian
1445240883Sadrian	/* Assign the completion handler */
1446240883Sadrian	bf->bf_comp = ath_tx_normal_comp;
1447240883Sadrian
1448227364Sadrian	/* Hand off to hardware */
1449227364Sadrian	ath_tx_handoff(sc, txq, bf);
1450227364Sadrian}
1451227364Sadrian
1452240722Sadrian/*
1453240722Sadrian * Do the basic frame setup stuff that's required before the frame
1454240722Sadrian * is added to a software queue.
1455240722Sadrian *
1456240722Sadrian * All frames get mostly the same treatment and it's done once.
1457240722Sadrian * Retransmits fiddle with things like the rate control setup,
1458240722Sadrian * setting the retransmit bit in the packet; doing relevant DMA/bus
1459240722Sadrian * syncing and relinking it (back) into the hardware TX queue.
1460240722Sadrian *
1461240722Sadrian * Note that this may cause the mbuf to be reallocated, so
1462240722Sadrian * m0 may not be valid.
1463240722Sadrian */
1464227364Sadrianstatic int
1465227364Sadrianath_tx_normal_setup(struct ath_softc *sc, struct ieee80211_node *ni,
1466234009Sadrian    struct ath_buf *bf, struct mbuf *m0, struct ath_txq *txq)
1467227364Sadrian{
1468218065Sadrian	struct ieee80211vap *vap = ni->ni_vap;
1469218065Sadrian	struct ath_hal *ah = sc->sc_ah;
1470218065Sadrian	struct ifnet *ifp = sc->sc_ifp;
1471218065Sadrian	struct ieee80211com *ic = ifp->if_l2com;
1472218065Sadrian	const struct chanAccParams *cap = &ic->ic_wme.wme_chanParams;
1473218065Sadrian	int error, iswep, ismcast, isfrag, ismrr;
1474227364Sadrian	int keyix, hdrlen, pktlen, try0 = 0;
1475227364Sadrian	u_int8_t rix = 0, txrate = 0;
1476218065Sadrian	struct ath_desc *ds;
1477218065Sadrian	struct ieee80211_frame *wh;
1478227364Sadrian	u_int subtype, flags;
1479218065Sadrian	HAL_PKT_TYPE atype;
1480218065Sadrian	const HAL_RATE_TABLE *rt;
1481218065Sadrian	HAL_BOOL shortPreamble;
1482218065Sadrian	struct ath_node *an;
1483218065Sadrian	u_int pri;
1484218065Sadrian
1485236880Sadrian	/*
1486236880Sadrian	 * To ensure that both sequence numbers and the CCMP PN handling
1487236880Sadrian	 * is "correct", make sure that the relevant TID queue is locked.
1488236880Sadrian	 * Otherwise the CCMP PN and seqno may appear out of order, causing
1489236880Sadrian	 * re-ordered frames to have out of order CCMP PN's, resulting
1490236880Sadrian	 * in many, many frame drops.
1491236880Sadrian	 */
1492243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
1493236880Sadrian
1494218065Sadrian	wh = mtod(m0, struct ieee80211_frame *);
1495218065Sadrian	iswep = wh->i_fc[1] & IEEE80211_FC1_WEP;
1496218065Sadrian	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1497218065Sadrian	isfrag = m0->m_flags & M_FRAG;
1498218065Sadrian	hdrlen = ieee80211_anyhdrsize(wh);
1499218065Sadrian	/*
1500218065Sadrian	 * Packet length must not include any
1501218065Sadrian	 * pad bytes; deduct them here.
1502218065Sadrian	 */
1503218065Sadrian	pktlen = m0->m_pkthdr.len - (hdrlen & 3);
1504218065Sadrian
1505218154Sadrian	/* Handle encryption twiddling if needed */
1506227364Sadrian	if (! ath_tx_tag_crypto(sc, ni, m0, iswep, isfrag, &hdrlen,
1507227364Sadrian	    &pktlen, &keyix)) {
1508218154Sadrian		ath_freetx(m0);
1509218154Sadrian		return EIO;
1510218154Sadrian	}
1511218065Sadrian
1512218154Sadrian	/* packet header may have moved, reset our local pointer */
1513218154Sadrian	wh = mtod(m0, struct ieee80211_frame *);
1514218065Sadrian
1515218065Sadrian	pktlen += IEEE80211_CRC_LEN;
1516218065Sadrian
1517218065Sadrian	/*
1518218065Sadrian	 * Load the DMA map so any coalescing is done.  This
1519218065Sadrian	 * also calculates the number of descriptors we need.
1520218065Sadrian	 */
1521218065Sadrian	error = ath_tx_dmasetup(sc, bf, m0);
1522218065Sadrian	if (error != 0)
1523218065Sadrian		return error;
1524218065Sadrian	bf->bf_node = ni;			/* NB: held reference */
1525218065Sadrian	m0 = bf->bf_m;				/* NB: may have changed */
1526218065Sadrian	wh = mtod(m0, struct ieee80211_frame *);
1527218065Sadrian
1528218065Sadrian	/* setup descriptors */
1529218065Sadrian	ds = bf->bf_desc;
1530218065Sadrian	rt = sc->sc_currates;
1531218065Sadrian	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
1532218065Sadrian
1533218065Sadrian	/*
1534218065Sadrian	 * NB: the 802.11 layer marks whether or not we should
1535218065Sadrian	 * use short preamble based on the current mode and
1536218065Sadrian	 * negotiated parameters.
1537218065Sadrian	 */
1538218065Sadrian	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1539218065Sadrian	    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
1540218065Sadrian		shortPreamble = AH_TRUE;
1541218065Sadrian		sc->sc_stats.ast_tx_shortpre++;
1542218065Sadrian	} else {
1543218065Sadrian		shortPreamble = AH_FALSE;
1544218065Sadrian	}
1545218065Sadrian
1546218065Sadrian	an = ATH_NODE(ni);
1547240724Sadrian	//flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for crypto errs */
1548240724Sadrian	flags = 0;
1549218065Sadrian	ismrr = 0;				/* default no multi-rate retry*/
1550218065Sadrian	pri = M_WME_GETAC(m0);			/* honor classification */
1551218065Sadrian	/* XXX use txparams instead of fixed values */
1552218065Sadrian	/*
1553218065Sadrian	 * Calculate Atheros packet type from IEEE80211 packet header,
1554218065Sadrian	 * setup for rate calculations, and select h/w transmit queue.
1555218065Sadrian	 */
1556218065Sadrian	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1557218065Sadrian	case IEEE80211_FC0_TYPE_MGT:
1558218065Sadrian		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1559218065Sadrian		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON)
1560218065Sadrian			atype = HAL_PKT_TYPE_BEACON;
1561218065Sadrian		else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1562218065Sadrian			atype = HAL_PKT_TYPE_PROBE_RESP;
1563218065Sadrian		else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM)
1564218065Sadrian			atype = HAL_PKT_TYPE_ATIM;
1565218065Sadrian		else
1566218065Sadrian			atype = HAL_PKT_TYPE_NORMAL;	/* XXX */
1567218065Sadrian		rix = an->an_mgmtrix;
1568218065Sadrian		txrate = rt->info[rix].rateCode;
1569218065Sadrian		if (shortPreamble)
1570218065Sadrian			txrate |= rt->info[rix].shortPreamble;
1571218065Sadrian		try0 = ATH_TXMGTTRY;
1572218065Sadrian		flags |= HAL_TXDESC_INTREQ;	/* force interrupt */
1573218065Sadrian		break;
1574218065Sadrian	case IEEE80211_FC0_TYPE_CTL:
1575218065Sadrian		atype = HAL_PKT_TYPE_PSPOLL;	/* stop setting of duration */
1576218065Sadrian		rix = an->an_mgmtrix;
1577218065Sadrian		txrate = rt->info[rix].rateCode;
1578218065Sadrian		if (shortPreamble)
1579218065Sadrian			txrate |= rt->info[rix].shortPreamble;
1580218065Sadrian		try0 = ATH_TXMGTTRY;
1581218065Sadrian		flags |= HAL_TXDESC_INTREQ;	/* force interrupt */
1582218065Sadrian		break;
1583218065Sadrian	case IEEE80211_FC0_TYPE_DATA:
1584218065Sadrian		atype = HAL_PKT_TYPE_NORMAL;		/* default */
1585218065Sadrian		/*
1586218065Sadrian		 * Data frames: multicast frames go out at a fixed rate,
1587218065Sadrian		 * EAPOL frames use the mgmt frame rate; otherwise consult
1588218065Sadrian		 * the rate control module for the rate to use.
1589218065Sadrian		 */
1590218065Sadrian		if (ismcast) {
1591218065Sadrian			rix = an->an_mcastrix;
1592218065Sadrian			txrate = rt->info[rix].rateCode;
1593218065Sadrian			if (shortPreamble)
1594218065Sadrian				txrate |= rt->info[rix].shortPreamble;
1595218065Sadrian			try0 = 1;
1596218065Sadrian		} else if (m0->m_flags & M_EAPOL) {
1597218065Sadrian			/* XXX? maybe always use long preamble? */
1598218065Sadrian			rix = an->an_mgmtrix;
1599218065Sadrian			txrate = rt->info[rix].rateCode;
1600218065Sadrian			if (shortPreamble)
1601218065Sadrian				txrate |= rt->info[rix].shortPreamble;
1602218065Sadrian			try0 = ATH_TXMAXTRY;	/* XXX?too many? */
1603218065Sadrian		} else {
1604227364Sadrian			/*
1605227364Sadrian			 * Do rate lookup on each TX, rather than using
1606227364Sadrian			 * the hard-coded TX information decided here.
1607227364Sadrian			 */
1608227364Sadrian			ismrr = 1;
1609227364Sadrian			bf->bf_state.bfs_doratelookup = 1;
1610218065Sadrian		}
1611218065Sadrian		if (cap->cap_wmeParams[pri].wmep_noackPolicy)
1612218065Sadrian			flags |= HAL_TXDESC_NOACK;
1613218065Sadrian		break;
1614218065Sadrian	default:
1615218065Sadrian		if_printf(ifp, "bogus frame type 0x%x (%s)\n",
1616218065Sadrian			wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__);
1617218065Sadrian		/* XXX statistic */
1618218065Sadrian		ath_freetx(m0);
1619218065Sadrian		return EIO;
1620218065Sadrian	}
1621218065Sadrian
1622237041Sadrian	/*
1623237041Sadrian	 * There are two known scenarios where the frame AC doesn't match
1624237041Sadrian	 * what the destination TXQ is.
1625237041Sadrian	 *
1626237041Sadrian	 * + non-QoS frames (eg management?) that the net80211 stack has
1627237041Sadrian	 *   assigned a higher AC to, but since it's a non-QoS TID, it's
1628237041Sadrian	 *   being thrown into TID 16.  TID 16 gets the AC_BE queue.
1629237041Sadrian	 *   It's quite possible that management frames should just be
1630237041Sadrian	 *   direct dispatched to hardware rather than go via the software
1631237041Sadrian	 *   queue; that should be investigated in the future.  There are
1632237041Sadrian	 *   some specific scenarios where this doesn't make sense, mostly
1633237041Sadrian	 *   surrounding ADDBA request/response - hence why that is special
1634237041Sadrian	 *   cased.
1635237041Sadrian	 *
1636237041Sadrian	 * + Multicast frames going into the VAP mcast queue.  That shows up
1637237041Sadrian	 *   as "TXQ 11".
1638237041Sadrian	 *
1639237041Sadrian	 * This driver should eventually support separate TID and TXQ locking,
1640237041Sadrian	 * allowing for arbitrary AC frames to appear on arbitrary software
1641237041Sadrian	 * queues, being queued to the "correct" hardware queue when needed.
1642237041Sadrian	 */
1643237041Sadrian#if 0
1644235750Sadrian	if (txq != sc->sc_ac2q[pri]) {
1645235750Sadrian		device_printf(sc->sc_dev,
1646235750Sadrian		    "%s: txq=%p (%d), pri=%d, pri txq=%p (%d)\n",
1647235750Sadrian		    __func__,
1648235750Sadrian		    txq,
1649235750Sadrian		    txq->axq_qnum,
1650235750Sadrian		    pri,
1651235750Sadrian		    sc->sc_ac2q[pri],
1652235750Sadrian		    sc->sc_ac2q[pri]->axq_qnum);
1653235750Sadrian	}
1654237041Sadrian#endif
1655235750Sadrian
1656218065Sadrian	/*
1657218065Sadrian	 * Calculate miscellaneous flags.
1658218065Sadrian	 */
1659218065Sadrian	if (ismcast) {
1660218065Sadrian		flags |= HAL_TXDESC_NOACK;	/* no ack on broad/multicast */
1661218065Sadrian	} else if (pktlen > vap->iv_rtsthreshold &&
1662218065Sadrian	    (ni->ni_ath_flags & IEEE80211_NODE_FF) == 0) {
1663218065Sadrian		flags |= HAL_TXDESC_RTSENA;	/* RTS based on frame length */
1664218065Sadrian		sc->sc_stats.ast_tx_rts++;
1665218065Sadrian	}
1666218065Sadrian	if (flags & HAL_TXDESC_NOACK)		/* NB: avoid double counting */
1667218065Sadrian		sc->sc_stats.ast_tx_noack++;
1668218065Sadrian#ifdef IEEE80211_SUPPORT_TDMA
1669218065Sadrian	if (sc->sc_tdma && (flags & HAL_TXDESC_NOACK) == 0) {
1670218065Sadrian		DPRINTF(sc, ATH_DEBUG_TDMA,
1671218065Sadrian		    "%s: discard frame, ACK required w/ TDMA\n", __func__);
1672218065Sadrian		sc->sc_stats.ast_tdma_ack++;
1673218065Sadrian		ath_freetx(m0);
1674218065Sadrian		return EIO;
1675218065Sadrian	}
1676218065Sadrian#endif
1677218065Sadrian
1678218065Sadrian	/*
1679227364Sadrian	 * Determine if a tx interrupt should be generated for
1680227364Sadrian	 * this descriptor.  We take a tx interrupt to reap
1681227364Sadrian	 * descriptors when the h/w hits an EOL condition or
1682227364Sadrian	 * when the descriptor is specifically marked to generate
1683227364Sadrian	 * an interrupt.  We periodically mark descriptors in this
1684227364Sadrian	 * way to insure timely replenishing of the supply needed
1685227364Sadrian	 * for sending frames.  Defering interrupts reduces system
1686227364Sadrian	 * load and potentially allows more concurrent work to be
1687227364Sadrian	 * done but if done to aggressively can cause senders to
1688227364Sadrian	 * backup.
1689227364Sadrian	 *
1690227364Sadrian	 * NB: use >= to deal with sc_txintrperiod changing
1691227364Sadrian	 *     dynamically through sysctl.
1692218065Sadrian	 */
1693227364Sadrian	if (flags & HAL_TXDESC_INTREQ) {
1694227364Sadrian		txq->axq_intrcnt = 0;
1695227364Sadrian	} else if (++txq->axq_intrcnt >= sc->sc_txintrperiod) {
1696227364Sadrian		flags |= HAL_TXDESC_INTREQ;
1697227364Sadrian		txq->axq_intrcnt = 0;
1698227364Sadrian	}
1699218932Sadrian
1700227364Sadrian	/* This point forward is actual TX bits */
1701218065Sadrian
1702218065Sadrian	/*
1703218065Sadrian	 * At this point we are committed to sending the frame
1704218065Sadrian	 * and we don't need to look at m_nextpkt; clear it in
1705218065Sadrian	 * case this frame is part of frag chain.
1706218065Sadrian	 */
1707218065Sadrian	m0->m_nextpkt = NULL;
1708218065Sadrian
1709218065Sadrian	if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
1710218065Sadrian		ieee80211_dump_pkt(ic, mtod(m0, const uint8_t *), m0->m_len,
1711218065Sadrian		    sc->sc_hwmap[rix].ieeerate, -1);
1712218065Sadrian
1713218065Sadrian	if (ieee80211_radiotap_active_vap(vap)) {
1714218065Sadrian		u_int64_t tsf = ath_hal_gettsf64(ah);
1715218065Sadrian
1716218065Sadrian		sc->sc_tx_th.wt_tsf = htole64(tsf);
1717218065Sadrian		sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
1718218065Sadrian		if (iswep)
1719218065Sadrian			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
1720218065Sadrian		if (isfrag)
1721218065Sadrian			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
1722218065Sadrian		sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
1723218065Sadrian		sc->sc_tx_th.wt_txpower = ni->ni_txpower;
1724218065Sadrian		sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
1725218065Sadrian
1726218065Sadrian		ieee80211_radiotap_tx(vap, m0);
1727218065Sadrian	}
1728218065Sadrian
1729227364Sadrian	/* Blank the legacy rate array */
1730227364Sadrian	bzero(&bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
1731227364Sadrian
1732218065Sadrian	/*
1733227364Sadrian	 * ath_buf_set_rate needs at least one rate/try to setup
1734227364Sadrian	 * the rate scenario.
1735227364Sadrian	 */
1736227364Sadrian	bf->bf_state.bfs_rc[0].rix = rix;
1737227364Sadrian	bf->bf_state.bfs_rc[0].tries = try0;
1738227364Sadrian	bf->bf_state.bfs_rc[0].ratecode = txrate;
1739227364Sadrian
1740227364Sadrian	/* Store the decided rate index values away */
1741227364Sadrian	bf->bf_state.bfs_pktlen = pktlen;
1742227364Sadrian	bf->bf_state.bfs_hdrlen = hdrlen;
1743227364Sadrian	bf->bf_state.bfs_atype = atype;
1744227364Sadrian	bf->bf_state.bfs_txpower = ni->ni_txpower;
1745227364Sadrian	bf->bf_state.bfs_txrate0 = txrate;
1746227364Sadrian	bf->bf_state.bfs_try0 = try0;
1747227364Sadrian	bf->bf_state.bfs_keyix = keyix;
1748227364Sadrian	bf->bf_state.bfs_txantenna = sc->sc_txantenna;
1749233966Sadrian	bf->bf_state.bfs_txflags = flags;
1750227364Sadrian	bf->bf_state.bfs_shpream = shortPreamble;
1751227364Sadrian
1752227364Sadrian	/* XXX this should be done in ath_tx_setrate() */
1753227364Sadrian	bf->bf_state.bfs_ctsrate0 = 0;	/* ie, no hard-coded ctsrate */
1754227364Sadrian	bf->bf_state.bfs_ctsrate = 0;	/* calculated later */
1755227364Sadrian	bf->bf_state.bfs_ctsduration = 0;
1756227364Sadrian	bf->bf_state.bfs_ismrr = ismrr;
1757227364Sadrian
1758227364Sadrian	return 0;
1759227364Sadrian}
1760227364Sadrian
1761227364Sadrian/*
1762240724Sadrian * Queue a frame to the hardware or software queue.
1763227364Sadrian *
1764227364Sadrian * This can be called by the net80211 code.
1765227364Sadrian *
1766227364Sadrian * XXX what about locking? Or, push the seqno assign into the
1767227364Sadrian * XXX aggregate scheduler so its serialised?
1768240724Sadrian *
1769240724Sadrian * XXX When sending management frames via ath_raw_xmit(),
1770240724Sadrian *     should CLRDMASK be set unconditionally?
1771227364Sadrian */
1772227364Sadrianint
1773227364Sadrianath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni,
1774227364Sadrian    struct ath_buf *bf, struct mbuf *m0)
1775227364Sadrian{
1776227364Sadrian	struct ieee80211vap *vap = ni->ni_vap;
1777227364Sadrian	struct ath_vap *avp = ATH_VAP(vap);
1778232764Sadrian	int r = 0;
1779227364Sadrian	u_int pri;
1780227364Sadrian	int tid;
1781227364Sadrian	struct ath_txq *txq;
1782227364Sadrian	int ismcast;
1783227364Sadrian	const struct ieee80211_frame *wh;
1784227364Sadrian	int is_ampdu, is_ampdu_tx, is_ampdu_pending;
1785236872Sadrian	ieee80211_seq seqno;
1786227364Sadrian	uint8_t type, subtype;
1787227364Sadrian
1788243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
1789243786Sadrian
1790227364Sadrian	/*
1791227364Sadrian	 * Determine the target hardware queue.
1792218065Sadrian	 *
1793234009Sadrian	 * For multicast frames, the txq gets overridden appropriately
1794234009Sadrian	 * depending upon the state of PS.
1795227364Sadrian	 *
1796227364Sadrian	 * For any other frame, we do a TID/QoS lookup inside the frame
1797227364Sadrian	 * to see what the TID should be. If it's a non-QoS frame, the
1798227364Sadrian	 * AC and TID are overridden. The TID/TXQ code assumes the
1799227364Sadrian	 * TID is on a predictable hardware TXQ, so we don't support
1800227364Sadrian	 * having a node TID queued to multiple hardware TXQs.
1801227364Sadrian	 * This may change in the future but would require some locking
1802227364Sadrian	 * fudgery.
1803218065Sadrian	 */
1804227364Sadrian	pri = ath_tx_getac(sc, m0);
1805227364Sadrian	tid = ath_tx_gettid(sc, m0);
1806218065Sadrian
1807227364Sadrian	txq = sc->sc_ac2q[pri];
1808227364Sadrian	wh = mtod(m0, struct ieee80211_frame *);
1809227364Sadrian	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1810227364Sadrian	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1811227364Sadrian	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1812227364Sadrian
1813232764Sadrian	/*
1814232764Sadrian	 * Enforce how deep the multicast queue can grow.
1815232764Sadrian	 *
1816232764Sadrian	 * XXX duplicated in ath_raw_xmit().
1817232764Sadrian	 */
1818232764Sadrian	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1819248750Sadrian		if (sc->sc_cabq->axq_depth + sc->sc_cabq->fifo.axq_depth
1820248750Sadrian		    > sc->sc_txq_mcastq_maxdepth) {
1821232764Sadrian			sc->sc_stats.ast_tx_mcastq_overflow++;
1822232764Sadrian			r = ENOBUFS;
1823232764Sadrian		}
1824232764Sadrian		if (r != 0) {
1825232764Sadrian			m_freem(m0);
1826232764Sadrian			return r;
1827232764Sadrian		}
1828232764Sadrian	}
1829232764Sadrian
1830227364Sadrian	/* A-MPDU TX */
1831227364Sadrian	is_ampdu_tx = ath_tx_ampdu_running(sc, ATH_NODE(ni), tid);
1832227364Sadrian	is_ampdu_pending = ath_tx_ampdu_pending(sc, ATH_NODE(ni), tid);
1833227364Sadrian	is_ampdu = is_ampdu_tx | is_ampdu_pending;
1834227364Sadrian
1835236872Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d, ac=%d, is_ampdu=%d\n",
1836236872Sadrian	    __func__, tid, pri, is_ampdu);
1837227364Sadrian
1838239051Sadrian	/* Set local packet state, used to queue packets to hardware */
1839239051Sadrian	bf->bf_state.bfs_tid = tid;
1840244109Sadrian	bf->bf_state.bfs_tx_queue = txq->axq_qnum;
1841239051Sadrian	bf->bf_state.bfs_pri = pri;
1842239051Sadrian
1843248671Sadrian#if 1
1844232753Sadrian	/*
1845234009Sadrian	 * When servicing one or more stations in power-save mode
1846234009Sadrian	 * (or) if there is some mcast data waiting on the mcast
1847234009Sadrian	 * queue (to prevent out of order delivery) multicast frames
1848234009Sadrian	 * must be bufferd until after the beacon.
1849234009Sadrian	 *
1850234009Sadrian	 * TODO: we should lock the mcastq before we check the length.
1851232753Sadrian	 */
1852248671Sadrian	if (sc->sc_cabq_enable && ismcast && (vap->iv_ps_sta || avp->av_mcastq.axq_depth)) {
1853227364Sadrian		txq = &avp->av_mcastq;
1854239051Sadrian		/*
1855239051Sadrian		 * Mark the frame as eventually belonging on the CAB
1856239051Sadrian		 * queue, so the descriptor setup functions will
1857239051Sadrian		 * correctly initialise the descriptor 'qcuId' field.
1858239051Sadrian		 */
1859244109Sadrian		bf->bf_state.bfs_tx_queue = sc->sc_cabq->axq_qnum;
1860239051Sadrian	}
1861248671Sadrian#endif
1862227364Sadrian
1863227364Sadrian	/* Do the generic frame setup */
1864227364Sadrian	/* XXX should just bzero the bf_state? */
1865227364Sadrian	bf->bf_state.bfs_dobaw = 0;
1866227364Sadrian
1867227364Sadrian	/* A-MPDU TX? Manually set sequence number */
1868236880Sadrian	/*
1869236880Sadrian	 * Don't do it whilst pending; the net80211 layer still
1870236880Sadrian	 * assigns them.
1871236880Sadrian	 */
1872227364Sadrian	if (is_ampdu_tx) {
1873227364Sadrian		/*
1874227364Sadrian		 * Always call; this function will
1875227364Sadrian		 * handle making sure that null data frames
1876227364Sadrian		 * don't get a sequence number from the current
1877227364Sadrian		 * TID and thus mess with the BAW.
1878227364Sadrian		 */
1879236872Sadrian		seqno = ath_tx_tid_seqno_assign(sc, ni, bf, m0);
1880236874Sadrian
1881236874Sadrian		/*
1882236874Sadrian		 * Don't add QoS NULL frames to the BAW.
1883236874Sadrian		 */
1884236872Sadrian		if (IEEE80211_QOS_HAS_SEQ(wh) &&
1885236872Sadrian		    subtype != IEEE80211_FC0_SUBTYPE_QOS_NULL) {
1886227364Sadrian			bf->bf_state.bfs_dobaw = 1;
1887227364Sadrian		}
1888218240Sadrian	}
1889218240Sadrian
1890218065Sadrian	/*
1891227364Sadrian	 * If needed, the sequence number has been assigned.
1892227364Sadrian	 * Squirrel it away somewhere easy to get to.
1893218065Sadrian	 */
1894236872Sadrian	bf->bf_state.bfs_seqno = M_SEQNO_GET(m0) << IEEE80211_SEQ_SEQ_SHIFT;
1895227364Sadrian
1896227364Sadrian	/* Is ampdu pending? fetch the seqno and print it out */
1897227364Sadrian	if (is_ampdu_pending)
1898227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX,
1899227364Sadrian		    "%s: tid %d: ampdu pending, seqno %d\n",
1900227364Sadrian		    __func__, tid, M_SEQNO_GET(m0));
1901227364Sadrian
1902227364Sadrian	/* This also sets up the DMA map */
1903234009Sadrian	r = ath_tx_normal_setup(sc, ni, bf, m0, txq);
1904227364Sadrian
1905227364Sadrian	if (r != 0)
1906236880Sadrian		goto done;
1907227364Sadrian
1908227364Sadrian	/* At this point m0 could have changed! */
1909227364Sadrian	m0 = bf->bf_m;
1910227364Sadrian
1911227364Sadrian#if 1
1912218065Sadrian	/*
1913227364Sadrian	 * If it's a multicast frame, do a direct-dispatch to the
1914227364Sadrian	 * destination hardware queue. Don't bother software
1915227364Sadrian	 * queuing it.
1916218065Sadrian	 */
1917227364Sadrian	/*
1918227364Sadrian	 * If it's a BAR frame, do a direct dispatch to the
1919227364Sadrian	 * destination hardware queue. Don't bother software
1920227364Sadrian	 * queuing it, as the TID will now be paused.
1921227364Sadrian	 * Sending a BAR frame can occur from the net80211 txa timer
1922227364Sadrian	 * (ie, retries) or from the ath txtask (completion call.)
1923227364Sadrian	 * It queues directly to hardware because the TID is paused
1924227364Sadrian	 * at this point (and won't be unpaused until the BAR has
1925227364Sadrian	 * either been TXed successfully or max retries has been
1926227364Sadrian	 * reached.)
1927227364Sadrian	 */
1928227364Sadrian	if (txq == &avp->av_mcastq) {
1929235774Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX,
1930233227Sadrian		    "%s: bf=%p: mcastq: TX'ing\n", __func__, bf);
1931240724Sadrian		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
1932227364Sadrian		ath_tx_xmit_normal(sc, txq, bf);
1933227364Sadrian	} else if (type == IEEE80211_FC0_TYPE_CTL &&
1934227364Sadrian		    subtype == IEEE80211_FC0_SUBTYPE_BAR) {
1935235774Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX,
1936227364Sadrian		    "%s: BAR: TX'ing direct\n", __func__);
1937240724Sadrian		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
1938227364Sadrian		ath_tx_xmit_normal(sc, txq, bf);
1939227364Sadrian	} else {
1940227364Sadrian		/* add to software queue */
1941235774Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX,
1942233227Sadrian		    "%s: bf=%p: swq: TX'ing\n", __func__, bf);
1943227364Sadrian		ath_tx_swq(sc, ni, txq, bf);
1944227364Sadrian	}
1945227364Sadrian#else
1946227364Sadrian	/*
1947227364Sadrian	 * For now, since there's no software queue,
1948227364Sadrian	 * direct-dispatch to the hardware.
1949227364Sadrian	 */
1950240724Sadrian	bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
1951227364Sadrian	ath_tx_xmit_normal(sc, txq, bf);
1952236880Sadrian#endif
1953236880Sadriandone:
1954218065Sadrian	return 0;
1955218065Sadrian}
1956218065Sadrian
1957218065Sadrianstatic int
1958218065Sadrianath_tx_raw_start(struct ath_softc *sc, struct ieee80211_node *ni,
1959218065Sadrian	struct ath_buf *bf, struct mbuf *m0,
1960218065Sadrian	const struct ieee80211_bpf_params *params)
1961218065Sadrian{
1962218065Sadrian	struct ifnet *ifp = sc->sc_ifp;
1963218065Sadrian	struct ieee80211com *ic = ifp->if_l2com;
1964218065Sadrian	struct ath_hal *ah = sc->sc_ah;
1965218065Sadrian	struct ieee80211vap *vap = ni->ni_vap;
1966218065Sadrian	int error, ismcast, ismrr;
1967218065Sadrian	int keyix, hdrlen, pktlen, try0, txantenna;
1968227364Sadrian	u_int8_t rix, txrate;
1969218065Sadrian	struct ieee80211_frame *wh;
1970227364Sadrian	u_int flags;
1971218065Sadrian	HAL_PKT_TYPE atype;
1972218065Sadrian	const HAL_RATE_TABLE *rt;
1973218065Sadrian	struct ath_desc *ds;
1974218065Sadrian	u_int pri;
1975227364Sadrian	int o_tid = -1;
1976227364Sadrian	int do_override;
1977218065Sadrian
1978243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
1979243786Sadrian
1980218065Sadrian	wh = mtod(m0, struct ieee80211_frame *);
1981218065Sadrian	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1982218065Sadrian	hdrlen = ieee80211_anyhdrsize(wh);
1983218065Sadrian	/*
1984218065Sadrian	 * Packet length must not include any
1985218065Sadrian	 * pad bytes; deduct them here.
1986218065Sadrian	 */
1987218065Sadrian	/* XXX honor IEEE80211_BPF_DATAPAD */
1988218065Sadrian	pktlen = m0->m_pkthdr.len - (hdrlen & 3) + IEEE80211_CRC_LEN;
1989218065Sadrian
1990240899Sadrian	ATH_KTR(sc, ATH_KTR_TX, 2,
1991240899Sadrian	     "ath_tx_raw_start: ni=%p, bf=%p, raw", ni, bf);
1992240899Sadrian
1993227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: ismcast=%d\n",
1994227364Sadrian	    __func__, ismcast);
1995227364Sadrian
1996236880Sadrian	pri = params->ibp_pri & 3;
1997236880Sadrian	/* Override pri if the frame isn't a QoS one */
1998236880Sadrian	if (! IEEE80211_QOS_HAS_SEQ(wh))
1999236880Sadrian		pri = ath_tx_getac(sc, m0);
2000236880Sadrian
2001236880Sadrian	/* XXX If it's an ADDBA, override the correct queue */
2002236880Sadrian	do_override = ath_tx_action_frame_override_queue(sc, ni, m0, &o_tid);
2003236880Sadrian
2004236880Sadrian	/* Map ADDBA to the correct priority */
2005236880Sadrian	if (do_override) {
2006236880Sadrian#if 0
2007236880Sadrian		device_printf(sc->sc_dev,
2008236880Sadrian		    "%s: overriding tid %d pri %d -> %d\n",
2009236880Sadrian		    __func__, o_tid, pri, TID_TO_WME_AC(o_tid));
2010236880Sadrian#endif
2011236880Sadrian		pri = TID_TO_WME_AC(o_tid);
2012236880Sadrian	}
2013236880Sadrian
2014218154Sadrian	/* Handle encryption twiddling if needed */
2015227364Sadrian	if (! ath_tx_tag_crypto(sc, ni,
2016227364Sadrian	    m0, params->ibp_flags & IEEE80211_BPF_CRYPTO, 0,
2017227364Sadrian	    &hdrlen, &pktlen, &keyix)) {
2018218154Sadrian		ath_freetx(m0);
2019218154Sadrian		return EIO;
2020218154Sadrian	}
2021218154Sadrian	/* packet header may have moved, reset our local pointer */
2022218154Sadrian	wh = mtod(m0, struct ieee80211_frame *);
2023218065Sadrian
2024227364Sadrian	/* Do the generic frame setup */
2025227364Sadrian	/* XXX should just bzero the bf_state? */
2026227364Sadrian	bf->bf_state.bfs_dobaw = 0;
2027227364Sadrian
2028218065Sadrian	error = ath_tx_dmasetup(sc, bf, m0);
2029218065Sadrian	if (error != 0)
2030218065Sadrian		return error;
2031218065Sadrian	m0 = bf->bf_m;				/* NB: may have changed */
2032218065Sadrian	wh = mtod(m0, struct ieee80211_frame *);
2033218065Sadrian	bf->bf_node = ni;			/* NB: held reference */
2034218065Sadrian
2035240724Sadrian	/* Always enable CLRDMASK for raw frames for now.. */
2036218065Sadrian	flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for crypto errs */
2037218065Sadrian	flags |= HAL_TXDESC_INTREQ;		/* force interrupt */
2038218065Sadrian	if (params->ibp_flags & IEEE80211_BPF_RTS)
2039218065Sadrian		flags |= HAL_TXDESC_RTSENA;
2040227364Sadrian	else if (params->ibp_flags & IEEE80211_BPF_CTS) {
2041227364Sadrian		/* XXX assume 11g/11n protection? */
2042227364Sadrian		bf->bf_state.bfs_doprot = 1;
2043218065Sadrian		flags |= HAL_TXDESC_CTSENA;
2044227364Sadrian	}
2045218065Sadrian	/* XXX leave ismcast to injector? */
2046218065Sadrian	if ((params->ibp_flags & IEEE80211_BPF_NOACK) || ismcast)
2047218065Sadrian		flags |= HAL_TXDESC_NOACK;
2048218065Sadrian
2049218065Sadrian	rt = sc->sc_currates;
2050218065Sadrian	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
2051218065Sadrian	rix = ath_tx_findrix(sc, params->ibp_rate0);
2052218065Sadrian	txrate = rt->info[rix].rateCode;
2053218065Sadrian	if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
2054218065Sadrian		txrate |= rt->info[rix].shortPreamble;
2055218065Sadrian	sc->sc_txrix = rix;
2056218065Sadrian	try0 = params->ibp_try0;
2057218065Sadrian	ismrr = (params->ibp_try1 != 0);
2058218065Sadrian	txantenna = params->ibp_pri >> 2;
2059218065Sadrian	if (txantenna == 0)			/* XXX? */
2060218065Sadrian		txantenna = sc->sc_txantenna;
2061218157Sadrian
2062227364Sadrian	/*
2063227364Sadrian	 * Since ctsrate is fixed, store it away for later
2064227364Sadrian	 * use when the descriptor fields are being set.
2065227364Sadrian	 */
2066227364Sadrian	if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA))
2067227364Sadrian		bf->bf_state.bfs_ctsrate0 = params->ibp_ctsrate;
2068218157Sadrian
2069218065Sadrian	/*
2070218065Sadrian	 * NB: we mark all packets as type PSPOLL so the h/w won't
2071218065Sadrian	 * set the sequence number, duration, etc.
2072218065Sadrian	 */
2073218065Sadrian	atype = HAL_PKT_TYPE_PSPOLL;
2074218065Sadrian
2075218065Sadrian	if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
2076218065Sadrian		ieee80211_dump_pkt(ic, mtod(m0, caddr_t), m0->m_len,
2077218065Sadrian		    sc->sc_hwmap[rix].ieeerate, -1);
2078218065Sadrian
2079218065Sadrian	if (ieee80211_radiotap_active_vap(vap)) {
2080218065Sadrian		u_int64_t tsf = ath_hal_gettsf64(ah);
2081218065Sadrian
2082218065Sadrian		sc->sc_tx_th.wt_tsf = htole64(tsf);
2083218065Sadrian		sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
2084218065Sadrian		if (wh->i_fc[1] & IEEE80211_FC1_WEP)
2085218065Sadrian			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2086218065Sadrian		if (m0->m_flags & M_FRAG)
2087218065Sadrian			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
2088218065Sadrian		sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
2089218065Sadrian		sc->sc_tx_th.wt_txpower = ni->ni_txpower;
2090218065Sadrian		sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
2091218065Sadrian
2092218065Sadrian		ieee80211_radiotap_tx(vap, m0);
2093218065Sadrian	}
2094218065Sadrian
2095218065Sadrian	/*
2096218065Sadrian	 * Formulate first tx descriptor with tx controls.
2097218065Sadrian	 */
2098218065Sadrian	ds = bf->bf_desc;
2099218065Sadrian	/* XXX check return value? */
2100227364Sadrian
2101227364Sadrian	/* Store the decided rate index values away */
2102227364Sadrian	bf->bf_state.bfs_pktlen = pktlen;
2103227364Sadrian	bf->bf_state.bfs_hdrlen = hdrlen;
2104227364Sadrian	bf->bf_state.bfs_atype = atype;
2105227364Sadrian	bf->bf_state.bfs_txpower = params->ibp_power;
2106227364Sadrian	bf->bf_state.bfs_txrate0 = txrate;
2107227364Sadrian	bf->bf_state.bfs_try0 = try0;
2108227364Sadrian	bf->bf_state.bfs_keyix = keyix;
2109227364Sadrian	bf->bf_state.bfs_txantenna = txantenna;
2110233966Sadrian	bf->bf_state.bfs_txflags = flags;
2111227364Sadrian	bf->bf_state.bfs_shpream =
2112227364Sadrian	    !! (params->ibp_flags & IEEE80211_BPF_SHORTPRE);
2113218065Sadrian
2114239051Sadrian	/* Set local packet state, used to queue packets to hardware */
2115239051Sadrian	bf->bf_state.bfs_tid = WME_AC_TO_TID(pri);
2116244109Sadrian	bf->bf_state.bfs_tx_queue = sc->sc_ac2q[pri]->axq_qnum;
2117239051Sadrian	bf->bf_state.bfs_pri = pri;
2118239051Sadrian
2119227364Sadrian	/* XXX this should be done in ath_tx_setrate() */
2120227364Sadrian	bf->bf_state.bfs_ctsrate = 0;
2121227364Sadrian	bf->bf_state.bfs_ctsduration = 0;
2122227364Sadrian	bf->bf_state.bfs_ismrr = ismrr;
2123218240Sadrian
2124227364Sadrian	/* Blank the legacy rate array */
2125227364Sadrian	bzero(&bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
2126218240Sadrian
2127227364Sadrian	bf->bf_state.bfs_rc[0].rix =
2128227364Sadrian	    ath_tx_findrix(sc, params->ibp_rate0);
2129227364Sadrian	bf->bf_state.bfs_rc[0].tries = try0;
2130227364Sadrian	bf->bf_state.bfs_rc[0].ratecode = txrate;
2131227364Sadrian
2132227364Sadrian	if (ismrr) {
2133227364Sadrian		int rix;
2134227364Sadrian
2135227364Sadrian		rix = ath_tx_findrix(sc, params->ibp_rate1);
2136227364Sadrian		bf->bf_state.bfs_rc[1].rix = rix;
2137227364Sadrian		bf->bf_state.bfs_rc[1].tries = params->ibp_try1;
2138227364Sadrian
2139227364Sadrian		rix = ath_tx_findrix(sc, params->ibp_rate2);
2140227364Sadrian		bf->bf_state.bfs_rc[2].rix = rix;
2141227364Sadrian		bf->bf_state.bfs_rc[2].tries = params->ibp_try2;
2142227364Sadrian
2143227364Sadrian		rix = ath_tx_findrix(sc, params->ibp_rate3);
2144227364Sadrian		bf->bf_state.bfs_rc[3].rix = rix;
2145227364Sadrian		bf->bf_state.bfs_rc[3].tries = params->ibp_try3;
2146218065Sadrian	}
2147227364Sadrian	/*
2148227364Sadrian	 * All the required rate control decisions have been made;
2149227364Sadrian	 * fill in the rc flags.
2150227364Sadrian	 */
2151227364Sadrian	ath_tx_rate_fill_rcflags(sc, bf);
2152218065Sadrian
2153227364Sadrian	/* NB: no buffered multicast in power save support */
2154227364Sadrian
2155227364Sadrian	/*
2156227364Sadrian	 * If we're overiding the ADDBA destination, dump directly
2157227364Sadrian	 * into the hardware queue, right after any pending
2158227364Sadrian	 * frames to that node are.
2159227364Sadrian	 */
2160227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: dooverride=%d\n",
2161227364Sadrian	    __func__, do_override);
2162227364Sadrian
2163240882Sadrian#if 1
2164227364Sadrian	if (do_override) {
2165240724Sadrian		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
2166227364Sadrian		ath_tx_xmit_normal(sc, sc->sc_ac2q[pri], bf);
2167227364Sadrian	} else {
2168227364Sadrian		/* Queue to software queue */
2169227364Sadrian		ath_tx_swq(sc, ni, sc->sc_ac2q[pri], bf);
2170227364Sadrian	}
2171240882Sadrian#else
2172240882Sadrian	/* Direct-dispatch to the hardware */
2173240882Sadrian	bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
2174240882Sadrian	ath_tx_xmit_normal(sc, sc->sc_ac2q[pri], bf);
2175240882Sadrian#endif
2176218065Sadrian	return 0;
2177218065Sadrian}
2178218065Sadrian
2179227364Sadrian/*
2180227364Sadrian * Send a raw frame.
2181227364Sadrian *
2182227364Sadrian * This can be called by net80211.
2183227364Sadrian */
2184218065Sadrianint
2185218065Sadrianath_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2186218065Sadrian	const struct ieee80211_bpf_params *params)
2187218065Sadrian{
2188218065Sadrian	struct ieee80211com *ic = ni->ni_ic;
2189218065Sadrian	struct ifnet *ifp = ic->ic_ifp;
2190218065Sadrian	struct ath_softc *sc = ifp->if_softc;
2191218065Sadrian	struct ath_buf *bf;
2192232764Sadrian	struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
2193232764Sadrian	int error = 0;
2194218065Sadrian
2195227651Sadrian	ATH_PCU_LOCK(sc);
2196227651Sadrian	if (sc->sc_inreset_cnt > 0) {
2197227651Sadrian		device_printf(sc->sc_dev, "%s: sc_inreset_cnt > 0; bailing\n",
2198227651Sadrian		    __func__);
2199227651Sadrian		error = EIO;
2200227651Sadrian		ATH_PCU_UNLOCK(sc);
2201227651Sadrian		goto bad0;
2202227651Sadrian	}
2203227651Sadrian	sc->sc_txstart_cnt++;
2204227651Sadrian	ATH_PCU_UNLOCK(sc);
2205227651Sadrian
2206242391Sadrian	ATH_TX_LOCK(sc);
2207242391Sadrian
2208218065Sadrian	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->sc_invalid) {
2209218065Sadrian		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: discard frame, %s", __func__,
2210218065Sadrian		    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ?
2211218065Sadrian			"!running" : "invalid");
2212218065Sadrian		m_freem(m);
2213218065Sadrian		error = ENETDOWN;
2214218065Sadrian		goto bad;
2215218065Sadrian	}
2216232764Sadrian
2217218065Sadrian	/*
2218232764Sadrian	 * Enforce how deep the multicast queue can grow.
2219232764Sadrian	 *
2220232764Sadrian	 * XXX duplicated in ath_tx_start().
2221232764Sadrian	 */
2222232764Sadrian	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2223248750Sadrian		if (sc->sc_cabq->axq_depth + sc->sc_cabq->fifo.axq_depth
2224248750Sadrian		    > sc->sc_txq_mcastq_maxdepth) {
2225232764Sadrian			sc->sc_stats.ast_tx_mcastq_overflow++;
2226232764Sadrian			error = ENOBUFS;
2227232764Sadrian		}
2228232764Sadrian
2229232764Sadrian		if (error != 0) {
2230232764Sadrian			m_freem(m);
2231232764Sadrian			goto bad;
2232232764Sadrian		}
2233232764Sadrian	}
2234232764Sadrian
2235232764Sadrian	/*
2236218065Sadrian	 * Grab a TX buffer and associated resources.
2237218065Sadrian	 */
2238237000Sadrian	bf = ath_getbuf(sc, ATH_BUFTYPE_MGMT);
2239218065Sadrian	if (bf == NULL) {
2240218065Sadrian		sc->sc_stats.ast_tx_nobuf++;
2241218065Sadrian		m_freem(m);
2242218065Sadrian		error = ENOBUFS;
2243218065Sadrian		goto bad;
2244218065Sadrian	}
2245240899Sadrian	ATH_KTR(sc, ATH_KTR_TX, 3, "ath_raw_xmit: m=%p, params=%p, bf=%p\n",
2246240899Sadrian	    m, params,  bf);
2247218065Sadrian
2248218065Sadrian	if (params == NULL) {
2249218065Sadrian		/*
2250218065Sadrian		 * Legacy path; interpret frame contents to decide
2251218065Sadrian		 * precisely how to send the frame.
2252218065Sadrian		 */
2253218065Sadrian		if (ath_tx_start(sc, ni, bf, m)) {
2254218065Sadrian			error = EIO;		/* XXX */
2255218065Sadrian			goto bad2;
2256218065Sadrian		}
2257218065Sadrian	} else {
2258218065Sadrian		/*
2259218065Sadrian		 * Caller supplied explicit parameters to use in
2260218065Sadrian		 * sending the frame.
2261218065Sadrian		 */
2262218065Sadrian		if (ath_tx_raw_start(sc, ni, bf, m, params)) {
2263218065Sadrian			error = EIO;		/* XXX */
2264218065Sadrian			goto bad2;
2265218065Sadrian		}
2266218065Sadrian	}
2267218065Sadrian	sc->sc_wd_timer = 5;
2268218065Sadrian	ifp->if_opackets++;
2269218065Sadrian	sc->sc_stats.ast_tx_raw++;
2270218065Sadrian
2271242271Sadrian	/*
2272242271Sadrian	 * Update the TIM - if there's anything queued to the
2273242271Sadrian	 * software queue and power save is enabled, we should
2274242271Sadrian	 * set the TIM.
2275242271Sadrian	 */
2276242271Sadrian	ath_tx_update_tim(sc, ni, 1);
2277242271Sadrian
2278243787Sadrian	ATH_TX_UNLOCK(sc);
2279243787Sadrian
2280227651Sadrian	ATH_PCU_LOCK(sc);
2281227651Sadrian	sc->sc_txstart_cnt--;
2282227651Sadrian	ATH_PCU_UNLOCK(sc);
2283227651Sadrian
2284218065Sadrian	return 0;
2285218065Sadrianbad2:
2286240899Sadrian	ATH_KTR(sc, ATH_KTR_TX, 3, "ath_raw_xmit: bad2: m=%p, params=%p, "
2287240899Sadrian	    "bf=%p",
2288240899Sadrian	    m,
2289240899Sadrian	    params,
2290240899Sadrian	    bf);
2291218065Sadrian	ATH_TXBUF_LOCK(sc);
2292236993Sadrian	ath_returnbuf_head(sc, bf);
2293218065Sadrian	ATH_TXBUF_UNLOCK(sc);
2294218065Sadrianbad:
2295242391Sadrian
2296242391Sadrian	ATH_TX_UNLOCK(sc);
2297242391Sadrian
2298227651Sadrian	ATH_PCU_LOCK(sc);
2299227651Sadrian	sc->sc_txstart_cnt--;
2300227651Sadrian	ATH_PCU_UNLOCK(sc);
2301227651Sadrianbad0:
2302240899Sadrian	ATH_KTR(sc, ATH_KTR_TX, 2, "ath_raw_xmit: bad0: m=%p, params=%p",
2303240899Sadrian	    m, params);
2304218065Sadrian	ifp->if_oerrors++;
2305218065Sadrian	sc->sc_stats.ast_tx_raw_fail++;
2306218065Sadrian	ieee80211_free_node(ni);
2307227651Sadrian
2308218065Sadrian	return error;
2309218065Sadrian}
2310227364Sadrian
2311227364Sadrian/* Some helper functions */
2312227364Sadrian
2313227364Sadrian/*
2314227364Sadrian * ADDBA (and potentially others) need to be placed in the same
2315227364Sadrian * hardware queue as the TID/node it's relating to. This is so
2316227364Sadrian * it goes out after any pending non-aggregate frames to the
2317227364Sadrian * same node/TID.
2318227364Sadrian *
2319227364Sadrian * If this isn't done, the ADDBA can go out before the frames
2320227364Sadrian * queued in hardware. Even though these frames have a sequence
2321227364Sadrian * number -earlier- than the ADDBA can be transmitted (but
2322227364Sadrian * no frames whose sequence numbers are after the ADDBA should
2323227364Sadrian * be!) they'll arrive after the ADDBA - and the receiving end
2324227364Sadrian * will simply drop them as being out of the BAW.
2325227364Sadrian *
2326227364Sadrian * The frames can't be appended to the TID software queue - it'll
2327227364Sadrian * never be sent out. So these frames have to be directly
2328227364Sadrian * dispatched to the hardware, rather than queued in software.
2329227364Sadrian * So if this function returns true, the TXQ has to be
2330227364Sadrian * overridden and it has to be directly dispatched.
2331227364Sadrian *
2332227364Sadrian * It's a dirty hack, but someone's gotta do it.
2333227364Sadrian */
2334227364Sadrian
2335227364Sadrian/*
2336227364Sadrian * XXX doesn't belong here!
2337227364Sadrian */
2338227364Sadrianstatic int
2339227364Sadrianieee80211_is_action(struct ieee80211_frame *wh)
2340227364Sadrian{
2341227364Sadrian	/* Type: Management frame? */
2342227364Sadrian	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) !=
2343227364Sadrian	    IEEE80211_FC0_TYPE_MGT)
2344227364Sadrian		return 0;
2345227364Sadrian
2346227364Sadrian	/* Subtype: Action frame? */
2347227364Sadrian	if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) !=
2348227364Sadrian	    IEEE80211_FC0_SUBTYPE_ACTION)
2349227364Sadrian		return 0;
2350227364Sadrian
2351227364Sadrian	return 1;
2352227364Sadrian}
2353227364Sadrian
2354227364Sadrian#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
2355227364Sadrian/*
2356227364Sadrian * Return an alternate TID for ADDBA request frames.
2357227364Sadrian *
2358227364Sadrian * Yes, this likely should be done in the net80211 layer.
2359227364Sadrian */
2360227364Sadrianstatic int
2361227364Sadrianath_tx_action_frame_override_queue(struct ath_softc *sc,
2362227364Sadrian    struct ieee80211_node *ni,
2363227364Sadrian    struct mbuf *m0, int *tid)
2364227364Sadrian{
2365227364Sadrian	struct ieee80211_frame *wh = mtod(m0, struct ieee80211_frame *);
2366227364Sadrian	struct ieee80211_action_ba_addbarequest *ia;
2367227364Sadrian	uint8_t *frm;
2368227364Sadrian	uint16_t baparamset;
2369227364Sadrian
2370227364Sadrian	/* Not action frame? Bail */
2371227364Sadrian	if (! ieee80211_is_action(wh))
2372227364Sadrian		return 0;
2373227364Sadrian
2374227364Sadrian	/* XXX Not needed for frames we send? */
2375227364Sadrian#if 0
2376227364Sadrian	/* Correct length? */
2377227364Sadrian	if (! ieee80211_parse_action(ni, m))
2378227364Sadrian		return 0;
2379227364Sadrian#endif
2380227364Sadrian
2381227364Sadrian	/* Extract out action frame */
2382227364Sadrian	frm = (u_int8_t *)&wh[1];
2383227364Sadrian	ia = (struct ieee80211_action_ba_addbarequest *) frm;
2384227364Sadrian
2385227364Sadrian	/* Not ADDBA? Bail */
2386227364Sadrian	if (ia->rq_header.ia_category != IEEE80211_ACTION_CAT_BA)
2387227364Sadrian		return 0;
2388227364Sadrian	if (ia->rq_header.ia_action != IEEE80211_ACTION_BA_ADDBA_REQUEST)
2389227364Sadrian		return 0;
2390227364Sadrian
2391227364Sadrian	/* Extract TID, return it */
2392227364Sadrian	baparamset = le16toh(ia->rq_baparamset);
2393227364Sadrian	*tid = (int) MS(baparamset, IEEE80211_BAPS_TID);
2394227364Sadrian
2395227364Sadrian	return 1;
2396227364Sadrian}
2397227364Sadrian#undef	MS
2398227364Sadrian
2399227364Sadrian/* Per-node software queue operations */
2400227364Sadrian
2401227364Sadrian/*
2402227364Sadrian * Add the current packet to the given BAW.
2403227364Sadrian * It is assumed that the current packet
2404227364Sadrian *
2405227364Sadrian * + fits inside the BAW;
2406227364Sadrian * + already has had a sequence number allocated.
2407227364Sadrian *
2408227364Sadrian * Since the BAW status may be modified by both the ath task and
2409227364Sadrian * the net80211/ifnet contexts, the TID must be locked.
2410227364Sadrian */
2411227364Sadrianvoid
2412227364Sadrianath_tx_addto_baw(struct ath_softc *sc, struct ath_node *an,
2413227364Sadrian    struct ath_tid *tid, struct ath_buf *bf)
2414227364Sadrian{
2415227364Sadrian	int index, cindex;
2416227364Sadrian	struct ieee80211_tx_ampdu *tap;
2417227364Sadrian
2418243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
2419227364Sadrian
2420227364Sadrian	if (bf->bf_state.bfs_isretried)
2421227364Sadrian		return;
2422227364Sadrian
2423236886Sadrian	tap = ath_tx_get_tx_tid(an, tid->tid);
2424236886Sadrian
2425236880Sadrian	if (! bf->bf_state.bfs_dobaw) {
2426236880Sadrian		device_printf(sc->sc_dev,
2427236880Sadrian		    "%s: dobaw=0, seqno=%d, window %d:%d\n",
2428236880Sadrian		    __func__,
2429236880Sadrian		    SEQNO(bf->bf_state.bfs_seqno),
2430236880Sadrian		    tap->txa_start,
2431236880Sadrian		    tap->txa_wnd);
2432236880Sadrian	}
2433236880Sadrian
2434227364Sadrian	if (bf->bf_state.bfs_addedbaw)
2435227364Sadrian		device_printf(sc->sc_dev,
2436236872Sadrian		    "%s: re-added? tid=%d, seqno %d; window %d:%d; "
2437229949Sadrian		    "baw head=%d tail=%d\n",
2438236872Sadrian		    __func__, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
2439229949Sadrian		    tap->txa_start, tap->txa_wnd, tid->baw_head,
2440229949Sadrian		    tid->baw_tail);
2441227364Sadrian
2442227364Sadrian	/*
2443236880Sadrian	 * Verify that the given sequence number is not outside of the
2444236880Sadrian	 * BAW.  Complain loudly if that's the case.
2445236880Sadrian	 */
2446236880Sadrian	if (! BAW_WITHIN(tap->txa_start, tap->txa_wnd,
2447236880Sadrian	    SEQNO(bf->bf_state.bfs_seqno))) {
2448236880Sadrian		device_printf(sc->sc_dev,
2449236880Sadrian		    "%s: bf=%p: outside of BAW?? tid=%d, seqno %d; window %d:%d; "
2450236880Sadrian		    "baw head=%d tail=%d\n",
2451236880Sadrian		    __func__, bf, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
2452236880Sadrian		    tap->txa_start, tap->txa_wnd, tid->baw_head,
2453236880Sadrian		    tid->baw_tail);
2454236880Sadrian	}
2455236880Sadrian
2456236880Sadrian	/*
2457227364Sadrian	 * ni->ni_txseqs[] is the currently allocated seqno.
2458227364Sadrian	 * the txa state contains the current baw start.
2459227364Sadrian	 */
2460227364Sadrian	index  = ATH_BA_INDEX(tap->txa_start, SEQNO(bf->bf_state.bfs_seqno));
2461227364Sadrian	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
2462227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2463236872Sadrian	    "%s: tid=%d, seqno %d; window %d:%d; index=%d cindex=%d "
2464229949Sadrian	    "baw head=%d tail=%d\n",
2465236872Sadrian	    __func__, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
2466229949Sadrian	    tap->txa_start, tap->txa_wnd, index, cindex, tid->baw_head,
2467229949Sadrian	    tid->baw_tail);
2468227364Sadrian
2469227364Sadrian
2470227364Sadrian#if 0
2471227364Sadrian	assert(tid->tx_buf[cindex] == NULL);
2472227364Sadrian#endif
2473227364Sadrian	if (tid->tx_buf[cindex] != NULL) {
2474227364Sadrian		device_printf(sc->sc_dev,
2475227364Sadrian		    "%s: ba packet dup (index=%d, cindex=%d, "
2476227364Sadrian		    "head=%d, tail=%d)\n",
2477227364Sadrian		    __func__, index, cindex, tid->baw_head, tid->baw_tail);
2478227364Sadrian		device_printf(sc->sc_dev,
2479227364Sadrian		    "%s: BA bf: %p; seqno=%d ; new bf: %p; seqno=%d\n",
2480227364Sadrian		    __func__,
2481227364Sadrian		    tid->tx_buf[cindex],
2482227364Sadrian		    SEQNO(tid->tx_buf[cindex]->bf_state.bfs_seqno),
2483227364Sadrian		    bf,
2484227364Sadrian		    SEQNO(bf->bf_state.bfs_seqno)
2485227364Sadrian		);
2486227364Sadrian	}
2487227364Sadrian	tid->tx_buf[cindex] = bf;
2488227364Sadrian
2489229949Sadrian	if (index >= ((tid->baw_tail - tid->baw_head) &
2490229949Sadrian	    (ATH_TID_MAX_BUFS - 1))) {
2491227364Sadrian		tid->baw_tail = cindex;
2492227364Sadrian		INCR(tid->baw_tail, ATH_TID_MAX_BUFS);
2493227364Sadrian	}
2494227364Sadrian}
2495227364Sadrian
2496227364Sadrian/*
2497227398Sadrian * Flip the BAW buffer entry over from the existing one to the new one.
2498227398Sadrian *
2499227398Sadrian * When software retransmitting a (sub-)frame, it is entirely possible that
2500227398Sadrian * the frame ath_buf is marked as BUSY and can't be immediately reused.
2501227398Sadrian * In that instance the buffer is cloned and the new buffer is used for
2502227398Sadrian * retransmit. We thus need to update the ath_buf slot in the BAW buf
2503227398Sadrian * tracking array to maintain consistency.
2504227398Sadrian */
2505227398Sadrianstatic void
2506227398Sadrianath_tx_switch_baw_buf(struct ath_softc *sc, struct ath_node *an,
2507227398Sadrian    struct ath_tid *tid, struct ath_buf *old_bf, struct ath_buf *new_bf)
2508227398Sadrian{
2509227398Sadrian	int index, cindex;
2510227398Sadrian	struct ieee80211_tx_ampdu *tap;
2511227398Sadrian	int seqno = SEQNO(old_bf->bf_state.bfs_seqno);
2512227398Sadrian
2513243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
2514227398Sadrian
2515227398Sadrian	tap = ath_tx_get_tx_tid(an, tid->tid);
2516227398Sadrian	index  = ATH_BA_INDEX(tap->txa_start, seqno);
2517227398Sadrian	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
2518227398Sadrian
2519227398Sadrian	/*
2520227398Sadrian	 * Just warn for now; if it happens then we should find out
2521227398Sadrian	 * about it. It's highly likely the aggregation session will
2522227398Sadrian	 * soon hang.
2523227398Sadrian	 */
2524227398Sadrian	if (old_bf->bf_state.bfs_seqno != new_bf->bf_state.bfs_seqno) {
2525227398Sadrian		device_printf(sc->sc_dev, "%s: retransmitted buffer"
2526227398Sadrian		    " has mismatching seqno's, BA session may hang.\n",
2527227398Sadrian		    __func__);
2528227398Sadrian		device_printf(sc->sc_dev, "%s: old seqno=%d, new_seqno=%d\n",
2529227398Sadrian		    __func__,
2530227398Sadrian		    old_bf->bf_state.bfs_seqno,
2531227398Sadrian		    new_bf->bf_state.bfs_seqno);
2532227398Sadrian	}
2533227398Sadrian
2534227398Sadrian	if (tid->tx_buf[cindex] != old_bf) {
2535227398Sadrian		device_printf(sc->sc_dev, "%s: ath_buf pointer incorrect; "
2536227398Sadrian		    " has m BA session may hang.\n",
2537227398Sadrian		    __func__);
2538227398Sadrian		device_printf(sc->sc_dev, "%s: old bf=%p, new bf=%p\n",
2539227398Sadrian		    __func__,
2540227398Sadrian		    old_bf, new_bf);
2541227398Sadrian	}
2542227398Sadrian
2543227398Sadrian	tid->tx_buf[cindex] = new_bf;
2544227398Sadrian}
2545227398Sadrian
2546227398Sadrian/*
2547227364Sadrian * seq_start - left edge of BAW
2548227364Sadrian * seq_next - current/next sequence number to allocate
2549227364Sadrian *
2550227364Sadrian * Since the BAW status may be modified by both the ath task and
2551227364Sadrian * the net80211/ifnet contexts, the TID must be locked.
2552227364Sadrian */
2553227364Sadrianstatic void
2554227364Sadrianath_tx_update_baw(struct ath_softc *sc, struct ath_node *an,
2555227364Sadrian    struct ath_tid *tid, const struct ath_buf *bf)
2556227364Sadrian{
2557227364Sadrian	int index, cindex;
2558227364Sadrian	struct ieee80211_tx_ampdu *tap;
2559227364Sadrian	int seqno = SEQNO(bf->bf_state.bfs_seqno);
2560227364Sadrian
2561243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
2562227364Sadrian
2563227364Sadrian	tap = ath_tx_get_tx_tid(an, tid->tid);
2564227364Sadrian	index  = ATH_BA_INDEX(tap->txa_start, seqno);
2565227364Sadrian	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
2566227364Sadrian
2567227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2568236872Sadrian	    "%s: tid=%d, baw=%d:%d, seqno=%d, index=%d, cindex=%d, "
2569229949Sadrian	    "baw head=%d, tail=%d\n",
2570236872Sadrian	    __func__, tid->tid, tap->txa_start, tap->txa_wnd, seqno, index,
2571227364Sadrian	    cindex, tid->baw_head, tid->baw_tail);
2572227364Sadrian
2573227364Sadrian	/*
2574227364Sadrian	 * If this occurs then we have a big problem - something else
2575227364Sadrian	 * has slid tap->txa_start along without updating the BAW
2576227364Sadrian	 * tracking start/end pointers. Thus the TX BAW state is now
2577227364Sadrian	 * completely busted.
2578227364Sadrian	 *
2579227364Sadrian	 * But for now, since I haven't yet fixed TDMA and buffer cloning,
2580227364Sadrian	 * it's quite possible that a cloned buffer is making its way
2581227364Sadrian	 * here and causing it to fire off. Disable TDMA for now.
2582227364Sadrian	 */
2583227364Sadrian	if (tid->tx_buf[cindex] != bf) {
2584227364Sadrian		device_printf(sc->sc_dev,
2585227364Sadrian		    "%s: comp bf=%p, seq=%d; slot bf=%p, seqno=%d\n",
2586227364Sadrian		    __func__,
2587227364Sadrian		    bf, SEQNO(bf->bf_state.bfs_seqno),
2588227364Sadrian		    tid->tx_buf[cindex],
2589227364Sadrian		    SEQNO(tid->tx_buf[cindex]->bf_state.bfs_seqno));
2590227364Sadrian	}
2591227364Sadrian
2592227364Sadrian	tid->tx_buf[cindex] = NULL;
2593227364Sadrian
2594229949Sadrian	while (tid->baw_head != tid->baw_tail &&
2595229949Sadrian	    !tid->tx_buf[tid->baw_head]) {
2596227364Sadrian		INCR(tap->txa_start, IEEE80211_SEQ_RANGE);
2597227364Sadrian		INCR(tid->baw_head, ATH_TID_MAX_BUFS);
2598227364Sadrian	}
2599229949Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2600229949Sadrian	    "%s: baw is now %d:%d, baw head=%d\n",
2601227364Sadrian	    __func__, tap->txa_start, tap->txa_wnd, tid->baw_head);
2602227364Sadrian}
2603227364Sadrian
2604227364Sadrian/*
2605227364Sadrian * Mark the current node/TID as ready to TX.
2606227364Sadrian *
2607227364Sadrian * This is done to make it easy for the software scheduler to
2608227364Sadrian * find which nodes have data to send.
2609227364Sadrian *
2610227364Sadrian * The TXQ lock must be held.
2611227364Sadrian */
2612227364Sadrianstatic void
2613227364Sadrianath_tx_tid_sched(struct ath_softc *sc, struct ath_tid *tid)
2614227364Sadrian{
2615227364Sadrian	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
2616227364Sadrian
2617243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
2618227364Sadrian
2619227364Sadrian	if (tid->paused)
2620227364Sadrian		return;		/* paused, can't schedule yet */
2621227364Sadrian
2622227364Sadrian	if (tid->sched)
2623227364Sadrian		return;		/* already scheduled */
2624227364Sadrian
2625227364Sadrian	tid->sched = 1;
2626227364Sadrian
2627227364Sadrian	TAILQ_INSERT_TAIL(&txq->axq_tidq, tid, axq_qelem);
2628227364Sadrian}
2629227364Sadrian
2630227364Sadrian/*
2631227364Sadrian * Mark the current node as no longer needing to be polled for
2632227364Sadrian * TX packets.
2633227364Sadrian *
2634227364Sadrian * The TXQ lock must be held.
2635227364Sadrian */
2636227364Sadrianstatic void
2637227364Sadrianath_tx_tid_unsched(struct ath_softc *sc, struct ath_tid *tid)
2638227364Sadrian{
2639227364Sadrian	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
2640227364Sadrian
2641243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
2642227364Sadrian
2643227364Sadrian	if (tid->sched == 0)
2644227364Sadrian		return;
2645227364Sadrian
2646227364Sadrian	tid->sched = 0;
2647227364Sadrian	TAILQ_REMOVE(&txq->axq_tidq, tid, axq_qelem);
2648227364Sadrian}
2649227364Sadrian
2650227364Sadrian/*
2651227364Sadrian * Assign a sequence number manually to the given frame.
2652227364Sadrian *
2653227364Sadrian * This should only be called for A-MPDU TX frames.
2654227364Sadrian */
2655236872Sadrianstatic ieee80211_seq
2656227364Sadrianath_tx_tid_seqno_assign(struct ath_softc *sc, struct ieee80211_node *ni,
2657227364Sadrian    struct ath_buf *bf, struct mbuf *m0)
2658227364Sadrian{
2659227364Sadrian	struct ieee80211_frame *wh;
2660227364Sadrian	int tid, pri;
2661227364Sadrian	ieee80211_seq seqno;
2662227364Sadrian	uint8_t subtype;
2663227364Sadrian
2664227364Sadrian	/* TID lookup */
2665227364Sadrian	wh = mtod(m0, struct ieee80211_frame *);
2666227364Sadrian	pri = M_WME_GETAC(m0);			/* honor classification */
2667227364Sadrian	tid = WME_AC_TO_TID(pri);
2668236872Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: pri=%d, tid=%d, qos has seq=%d\n",
2669236872Sadrian	    __func__, pri, tid, IEEE80211_QOS_HAS_SEQ(wh));
2670227364Sadrian
2671227364Sadrian	/* XXX Is it a control frame? Ignore */
2672227364Sadrian
2673227364Sadrian	/* Does the packet require a sequence number? */
2674227364Sadrian	if (! IEEE80211_QOS_HAS_SEQ(wh))
2675227364Sadrian		return -1;
2676227364Sadrian
2677243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
2678236880Sadrian
2679227364Sadrian	/*
2680227364Sadrian	 * Is it a QOS NULL Data frame? Give it a sequence number from
2681227364Sadrian	 * the default TID (IEEE80211_NONQOS_TID.)
2682227364Sadrian	 *
2683227364Sadrian	 * The RX path of everything I've looked at doesn't include the NULL
2684227364Sadrian	 * data frame sequence number in the aggregation state updates, so
2685227364Sadrian	 * assigning it a sequence number there will cause a BAW hole on the
2686227364Sadrian	 * RX side.
2687227364Sadrian	 */
2688227364Sadrian	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
2689227364Sadrian	if (subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL) {
2690236880Sadrian		/* XXX no locking for this TID? This is a bit of a problem. */
2691227364Sadrian		seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID];
2692227364Sadrian		INCR(ni->ni_txseqs[IEEE80211_NONQOS_TID], IEEE80211_SEQ_RANGE);
2693227364Sadrian	} else {
2694227364Sadrian		/* Manually assign sequence number */
2695227364Sadrian		seqno = ni->ni_txseqs[tid];
2696227364Sadrian		INCR(ni->ni_txseqs[tid], IEEE80211_SEQ_RANGE);
2697227364Sadrian	}
2698227364Sadrian	*(uint16_t *)&wh->i_seq[0] = htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
2699227364Sadrian	M_SEQNO_SET(m0, seqno);
2700227364Sadrian
2701227364Sadrian	/* Return so caller can do something with it if needed */
2702236872Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s:  -> seqno=%d\n", __func__, seqno);
2703227364Sadrian	return seqno;
2704227364Sadrian}
2705227364Sadrian
2706227364Sadrian/*
2707227364Sadrian * Attempt to direct dispatch an aggregate frame to hardware.
2708227364Sadrian * If the frame is out of BAW, queue.
2709227364Sadrian * Otherwise, schedule it as a single frame.
2710227364Sadrian */
2711227364Sadrianstatic void
2712239051Sadrianath_tx_xmit_aggr(struct ath_softc *sc, struct ath_node *an,
2713239051Sadrian    struct ath_txq *txq, struct ath_buf *bf)
2714227364Sadrian{
2715227364Sadrian	struct ath_tid *tid = &an->an_tid[bf->bf_state.bfs_tid];
2716227364Sadrian	struct ieee80211_tx_ampdu *tap;
2717227364Sadrian
2718243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
2719227364Sadrian
2720227364Sadrian	tap = ath_tx_get_tx_tid(an, tid->tid);
2721227364Sadrian
2722227364Sadrian	/* paused? queue */
2723227364Sadrian	if (tid->paused) {
2724241336Sadrian		ATH_TID_INSERT_HEAD(tid, bf, bf_list);
2725233480Sadrian		/* XXX don't sched - we're paused! */
2726227364Sadrian		return;
2727227364Sadrian	}
2728227364Sadrian
2729227364Sadrian	/* outside baw? queue */
2730227364Sadrian	if (bf->bf_state.bfs_dobaw &&
2731227364Sadrian	    (! BAW_WITHIN(tap->txa_start, tap->txa_wnd,
2732227364Sadrian	    SEQNO(bf->bf_state.bfs_seqno)))) {
2733241336Sadrian		ATH_TID_INSERT_HEAD(tid, bf, bf_list);
2734227364Sadrian		ath_tx_tid_sched(sc, tid);
2735227364Sadrian		return;
2736227364Sadrian	}
2737227364Sadrian
2738240180Sadrian	/*
2739240180Sadrian	 * This is a temporary check and should be removed once
2740240180Sadrian	 * all the relevant code paths have been fixed.
2741240180Sadrian	 *
2742240180Sadrian	 * During aggregate retries, it's possible that the head
2743240180Sadrian	 * frame will fail (which has the bfs_aggr and bfs_nframes
2744240180Sadrian	 * fields set for said aggregate) and will be retried as
2745240180Sadrian	 * a single frame.  In this instance, the values should
2746240180Sadrian	 * be reset or the completion code will get upset with you.
2747240180Sadrian	 */
2748240180Sadrian	if (bf->bf_state.bfs_aggr != 0 || bf->bf_state.bfs_nframes > 1) {
2749240180Sadrian		device_printf(sc->sc_dev, "%s: bfs_aggr=%d, bfs_nframes=%d\n",
2750240180Sadrian		    __func__,
2751240180Sadrian		    bf->bf_state.bfs_aggr,
2752240180Sadrian		    bf->bf_state.bfs_nframes);
2753240180Sadrian		bf->bf_state.bfs_aggr = 0;
2754240180Sadrian		bf->bf_state.bfs_nframes = 1;
2755240180Sadrian	}
2756240180Sadrian
2757240724Sadrian	/* Update CLRDMASK just before this frame is queued */
2758240724Sadrian	ath_tx_update_clrdmask(sc, tid, bf);
2759240724Sadrian
2760227364Sadrian	/* Direct dispatch to hardware */
2761227364Sadrian	ath_tx_do_ratelookup(sc, bf);
2762233989Sadrian	ath_tx_calc_duration(sc, bf);
2763233989Sadrian	ath_tx_calc_protection(sc, bf);
2764233989Sadrian	ath_tx_set_rtscts(sc, bf);
2765227364Sadrian	ath_tx_rate_fill_rcflags(sc, bf);
2766227364Sadrian	ath_tx_setds(sc, bf);
2767227364Sadrian
2768227364Sadrian	/* Statistics */
2769227364Sadrian	sc->sc_aggr_stats.aggr_low_hwq_single_pkt++;
2770227364Sadrian
2771227364Sadrian	/* Track per-TID hardware queue depth correctly */
2772227364Sadrian	tid->hwq_depth++;
2773227364Sadrian
2774227364Sadrian	/* Add to BAW */
2775227364Sadrian	if (bf->bf_state.bfs_dobaw) {
2776227364Sadrian		ath_tx_addto_baw(sc, an, tid, bf);
2777227364Sadrian		bf->bf_state.bfs_addedbaw = 1;
2778227364Sadrian	}
2779227364Sadrian
2780227364Sadrian	/* Set completion handler, multi-frame aggregate or not */
2781227364Sadrian	bf->bf_comp = ath_tx_aggr_comp;
2782227364Sadrian
2783227364Sadrian	/* Hand off to hardware */
2784227364Sadrian	ath_tx_handoff(sc, txq, bf);
2785227364Sadrian}
2786227364Sadrian
2787227364Sadrian/*
2788227364Sadrian * Attempt to send the packet.
2789227364Sadrian * If the queue isn't busy, direct-dispatch.
2790227364Sadrian * If the queue is busy enough, queue the given packet on the
2791227364Sadrian *  relevant software queue.
2792227364Sadrian */
2793227364Sadrianvoid
2794227364Sadrianath_tx_swq(struct ath_softc *sc, struct ieee80211_node *ni, struct ath_txq *txq,
2795227364Sadrian    struct ath_buf *bf)
2796227364Sadrian{
2797227364Sadrian	struct ath_node *an = ATH_NODE(ni);
2798227364Sadrian	struct ieee80211_frame *wh;
2799227364Sadrian	struct ath_tid *atid;
2800227364Sadrian	int pri, tid;
2801227364Sadrian	struct mbuf *m0 = bf->bf_m;
2802227364Sadrian
2803243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
2804236880Sadrian
2805227364Sadrian	/* Fetch the TID - non-QoS frames get assigned to TID 16 */
2806227364Sadrian	wh = mtod(m0, struct ieee80211_frame *);
2807227364Sadrian	pri = ath_tx_getac(sc, m0);
2808227364Sadrian	tid = ath_tx_gettid(sc, m0);
2809227364Sadrian	atid = &an->an_tid[tid];
2810227364Sadrian
2811236872Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bf=%p, pri=%d, tid=%d, qos=%d\n",
2812236872Sadrian	    __func__, bf, pri, tid, IEEE80211_QOS_HAS_SEQ(wh));
2813227364Sadrian
2814227364Sadrian	/* Set local packet state, used to queue packets to hardware */
2815239051Sadrian	/* XXX potentially duplicate info, re-check */
2816227364Sadrian	bf->bf_state.bfs_tid = tid;
2817244109Sadrian	bf->bf_state.bfs_tx_queue = txq->axq_qnum;
2818227364Sadrian	bf->bf_state.bfs_pri = pri;
2819227364Sadrian
2820227364Sadrian	/*
2821227364Sadrian	 * If the hardware queue isn't busy, queue it directly.
2822227364Sadrian	 * If the hardware queue is busy, queue it.
2823227364Sadrian	 * If the TID is paused or the traffic it outside BAW, software
2824227364Sadrian	 * queue it.
2825227364Sadrian	 */
2826227364Sadrian	if (atid->paused) {
2827227364Sadrian		/* TID is paused, queue */
2828236872Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: paused\n", __func__);
2829241336Sadrian		ATH_TID_INSERT_TAIL(atid, bf, bf_list);
2830227364Sadrian	} else if (ath_tx_ampdu_pending(sc, an, tid)) {
2831227364Sadrian		/* AMPDU pending; queue */
2832236872Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: pending\n", __func__);
2833241336Sadrian		ATH_TID_INSERT_TAIL(atid, bf, bf_list);
2834227364Sadrian		/* XXX sched? */
2835227364Sadrian	} else if (ath_tx_ampdu_running(sc, an, tid)) {
2836227364Sadrian		/* AMPDU running, attempt direct dispatch if possible */
2837236877Sadrian
2838236877Sadrian		/*
2839236877Sadrian		 * Always queue the frame to the tail of the list.
2840236877Sadrian		 */
2841241336Sadrian		ATH_TID_INSERT_TAIL(atid, bf, bf_list);
2842236877Sadrian
2843236877Sadrian		/*
2844236877Sadrian		 * If the hardware queue isn't busy, direct dispatch
2845236877Sadrian		 * the head frame in the list.  Don't schedule the
2846236877Sadrian		 * TID - let it build some more frames first?
2847236877Sadrian		 *
2848236877Sadrian		 * Otherwise, schedule the TID.
2849236877Sadrian		 */
2850248750Sadrian		if (txq->axq_depth + txq->fifo.axq_depth < sc->sc_hwq_limit) {
2851241336Sadrian			bf = ATH_TID_FIRST(atid);
2852241336Sadrian			ATH_TID_REMOVE(atid, bf, bf_list);
2853240180Sadrian
2854240180Sadrian			/*
2855240180Sadrian			 * Ensure it's definitely treated as a non-AMPDU
2856240180Sadrian			 * frame - this information may have been left
2857240180Sadrian			 * over from a previous attempt.
2858240180Sadrian			 */
2859240180Sadrian			bf->bf_state.bfs_aggr = 0;
2860240180Sadrian			bf->bf_state.bfs_nframes = 1;
2861240180Sadrian
2862240180Sadrian			/* Queue to the hardware */
2863239051Sadrian			ath_tx_xmit_aggr(sc, an, txq, bf);
2864233227Sadrian			DPRINTF(sc, ATH_DEBUG_SW_TX,
2865236872Sadrian			    "%s: xmit_aggr\n",
2866236872Sadrian			    __func__);
2867229949Sadrian		} else {
2868229949Sadrian			DPRINTF(sc, ATH_DEBUG_SW_TX,
2869236872Sadrian			    "%s: ampdu; swq'ing\n",
2870236872Sadrian			    __func__);
2871240899Sadrian
2872227364Sadrian			ath_tx_tid_sched(sc, atid);
2873227364Sadrian		}
2874248750Sadrian	} else if (txq->axq_depth + txq->fifo.axq_depth < sc->sc_hwq_limit) {
2875227364Sadrian		/* AMPDU not running, attempt direct dispatch */
2876236872Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: xmit_normal\n", __func__);
2877240926Sadrian		/* See if clrdmask needs to be set */
2878240926Sadrian		ath_tx_update_clrdmask(sc, atid, bf);
2879227364Sadrian		ath_tx_xmit_normal(sc, txq, bf);
2880227364Sadrian	} else {
2881227364Sadrian		/* Busy; queue */
2882236872Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: swq'ing\n", __func__);
2883241336Sadrian		ATH_TID_INSERT_TAIL(atid, bf, bf_list);
2884227364Sadrian		ath_tx_tid_sched(sc, atid);
2885227364Sadrian	}
2886227364Sadrian}
2887227364Sadrian
2888227364Sadrian/*
2889245708Sadrian * Only set the clrdmask bit if none of the nodes are currently
2890245708Sadrian * filtered.
2891245708Sadrian *
2892245708Sadrian * XXX TODO: go through all the callers and check to see
2893245708Sadrian * which are being called in the context of looping over all
2894245708Sadrian * TIDs (eg, if all tids are being paused, resumed, etc.)
2895245708Sadrian * That'll avoid O(n^2) complexity here.
2896245708Sadrian */
2897245708Sadrianstatic void
2898245708Sadrianath_tx_set_clrdmask(struct ath_softc *sc, struct ath_node *an)
2899245708Sadrian{
2900245708Sadrian	int i;
2901245708Sadrian
2902245708Sadrian	ATH_TX_LOCK_ASSERT(sc);
2903245708Sadrian
2904245708Sadrian	for (i = 0; i < IEEE80211_TID_SIZE; i++) {
2905245708Sadrian		if (an->an_tid[i].isfiltered == 1)
2906245739Sadrian			return;
2907245708Sadrian	}
2908245708Sadrian	an->clrdmask = 1;
2909245708Sadrian}
2910245708Sadrian
2911245708Sadrian/*
2912227364Sadrian * Configure the per-TID node state.
2913227364Sadrian *
2914227364Sadrian * This likely belongs in if_ath_node.c but I can't think of anywhere
2915227364Sadrian * else to put it just yet.
2916227364Sadrian *
2917227364Sadrian * This sets up the SLISTs and the mutex as appropriate.
2918227364Sadrian */
2919227364Sadrianvoid
2920227364Sadrianath_tx_tid_init(struct ath_softc *sc, struct ath_node *an)
2921227364Sadrian{
2922227364Sadrian	int i, j;
2923227364Sadrian	struct ath_tid *atid;
2924227364Sadrian
2925227364Sadrian	for (i = 0; i < IEEE80211_TID_SIZE; i++) {
2926227364Sadrian		atid = &an->an_tid[i];
2927240639Sadrian
2928240639Sadrian		/* XXX now with this bzer(), is the field 0'ing needed? */
2929240639Sadrian		bzero(atid, sizeof(*atid));
2930240639Sadrian
2931241336Sadrian		TAILQ_INIT(&atid->tid_q);
2932241336Sadrian		TAILQ_INIT(&atid->filtq.tid_q);
2933227364Sadrian		atid->tid = i;
2934227364Sadrian		atid->an = an;
2935227364Sadrian		for (j = 0; j < ATH_TID_MAX_BUFS; j++)
2936227364Sadrian			atid->tx_buf[j] = NULL;
2937227364Sadrian		atid->baw_head = atid->baw_tail = 0;
2938227364Sadrian		atid->paused = 0;
2939227364Sadrian		atid->sched = 0;
2940227364Sadrian		atid->hwq_depth = 0;
2941227364Sadrian		atid->cleanup_inprogress = 0;
2942227364Sadrian		if (i == IEEE80211_NONQOS_TID)
2943240946Sadrian			atid->ac = ATH_NONQOS_TID_AC;
2944227364Sadrian		else
2945227364Sadrian			atid->ac = TID_TO_WME_AC(i);
2946227364Sadrian	}
2947245708Sadrian	an->clrdmask = 1;	/* Always start by setting this bit */
2948227364Sadrian}
2949227364Sadrian
2950227364Sadrian/*
2951227364Sadrian * Pause the current TID. This stops packets from being transmitted
2952227364Sadrian * on it.
2953227364Sadrian *
2954227364Sadrian * Since this is also called from upper layers as well as the driver,
2955227364Sadrian * it will get the TID lock.
2956227364Sadrian */
2957227364Sadrianstatic void
2958227364Sadrianath_tx_tid_pause(struct ath_softc *sc, struct ath_tid *tid)
2959227364Sadrian{
2960233908Sadrian
2961243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
2962227364Sadrian	tid->paused++;
2963227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: paused = %d\n",
2964227364Sadrian	    __func__, tid->paused);
2965227364Sadrian}
2966227364Sadrian
2967227364Sadrian/*
2968227364Sadrian * Unpause the current TID, and schedule it if needed.
2969227364Sadrian */
2970227364Sadrianstatic void
2971227364Sadrianath_tx_tid_resume(struct ath_softc *sc, struct ath_tid *tid)
2972227364Sadrian{
2973243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
2974243786Sadrian
2975227364Sadrian	tid->paused--;
2976227364Sadrian
2977227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: unpaused = %d\n",
2978227364Sadrian	    __func__, tid->paused);
2979227364Sadrian
2980241170Sadrian	if (tid->paused)
2981227364Sadrian		return;
2982227364Sadrian
2983241170Sadrian	/*
2984241170Sadrian	 * Override the clrdmask configuration for the next frame
2985241170Sadrian	 * from this TID, just to get the ball rolling.
2986241170Sadrian	 */
2987245708Sadrian	ath_tx_set_clrdmask(sc, tid->an);
2988241170Sadrian
2989241170Sadrian	if (tid->axq_depth == 0)
2990241170Sadrian		return;
2991241170Sadrian
2992240639Sadrian	/* XXX isfiltered shouldn't ever be 0 at this point */
2993240639Sadrian	if (tid->isfiltered == 1) {
2994240639Sadrian		device_printf(sc->sc_dev, "%s: filtered?!\n", __func__);
2995240639Sadrian		return;
2996240639Sadrian	}
2997240639Sadrian
2998227364Sadrian	ath_tx_tid_sched(sc, tid);
2999246450Sadrian
3000246450Sadrian	/*
3001246450Sadrian	 * Queue the software TX scheduler.
3002246450Sadrian	 */
3003246450Sadrian	ath_tx_swq_kick(sc);
3004227364Sadrian}
3005227364Sadrian
3006227364Sadrian/*
3007240639Sadrian * Add the given ath_buf to the TID filtered frame list.
3008240639Sadrian * This requires the TID be filtered.
3009240639Sadrian */
3010240639Sadrianstatic void
3011240639Sadrianath_tx_tid_filt_addbuf(struct ath_softc *sc, struct ath_tid *tid,
3012240639Sadrian    struct ath_buf *bf)
3013240639Sadrian{
3014240639Sadrian
3015243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
3016243786Sadrian
3017240639Sadrian	if (! tid->isfiltered)
3018240639Sadrian		device_printf(sc->sc_dev, "%s: not filtered?!\n", __func__);
3019240639Sadrian
3020240639Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_FILT, "%s: bf=%p\n", __func__, bf);
3021240639Sadrian
3022240639Sadrian	/* Set the retry bit and bump the retry counter */
3023240639Sadrian	ath_tx_set_retry(sc, bf);
3024240639Sadrian	sc->sc_stats.ast_tx_swfiltered++;
3025240639Sadrian
3026241566Sadrian	ATH_TID_FILT_INSERT_TAIL(tid, bf, bf_list);
3027240639Sadrian}
3028240639Sadrian
3029240639Sadrian/*
3030240639Sadrian * Handle a completed filtered frame from the given TID.
3031240639Sadrian * This just enables/pauses the filtered frame state if required
3032240639Sadrian * and appends the filtered frame to the filtered queue.
3033240639Sadrian */
3034240639Sadrianstatic void
3035240639Sadrianath_tx_tid_filt_comp_buf(struct ath_softc *sc, struct ath_tid *tid,
3036240639Sadrian    struct ath_buf *bf)
3037240639Sadrian{
3038240639Sadrian
3039243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
3040240639Sadrian
3041240639Sadrian	if (! tid->isfiltered) {
3042240639Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT, "%s: filter transition\n",
3043240639Sadrian		    __func__);
3044240639Sadrian		tid->isfiltered = 1;
3045240639Sadrian		ath_tx_tid_pause(sc, tid);
3046240639Sadrian	}
3047240639Sadrian
3048240639Sadrian	/* Add the frame to the filter queue */
3049240639Sadrian	ath_tx_tid_filt_addbuf(sc, tid, bf);
3050240639Sadrian}
3051240639Sadrian
3052240639Sadrian/*
3053240639Sadrian * Complete the filtered frame TX completion.
3054240639Sadrian *
3055240639Sadrian * If there are no more frames in the hardware queue, unpause/unfilter
3056240639Sadrian * the TID if applicable.  Otherwise we will wait for a node PS transition
3057240639Sadrian * to unfilter.
3058240639Sadrian */
3059240639Sadrianstatic void
3060240639Sadrianath_tx_tid_filt_comp_complete(struct ath_softc *sc, struct ath_tid *tid)
3061240639Sadrian{
3062240639Sadrian	struct ath_buf *bf;
3063240639Sadrian
3064243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
3065240639Sadrian
3066240639Sadrian	if (tid->hwq_depth != 0)
3067240639Sadrian		return;
3068240639Sadrian
3069240639Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_FILT, "%s: hwq=0, transition back\n",
3070240639Sadrian	    __func__);
3071240639Sadrian	tid->isfiltered = 0;
3072245708Sadrian	/* XXX ath_tx_tid_resume() also calls ath_tx_set_clrdmask()! */
3073245708Sadrian	ath_tx_set_clrdmask(sc, tid->an);
3074240639Sadrian
3075240639Sadrian	/* XXX this is really quite inefficient */
3076241566Sadrian	while ((bf = ATH_TID_FILT_LAST(tid, ath_bufhead_s)) != NULL) {
3077241566Sadrian		ATH_TID_FILT_REMOVE(tid, bf, bf_list);
3078241336Sadrian		ATH_TID_INSERT_HEAD(tid, bf, bf_list);
3079240639Sadrian	}
3080240639Sadrian
3081240639Sadrian	ath_tx_tid_resume(sc, tid);
3082240639Sadrian}
3083240639Sadrian
3084240639Sadrian/*
3085240639Sadrian * Called when a single (aggregate or otherwise) frame is completed.
3086240639Sadrian *
3087240639Sadrian * Returns 1 if the buffer could be added to the filtered list
3088240639Sadrian * (cloned or otherwise), 0 if the buffer couldn't be added to the
3089240639Sadrian * filtered list (failed clone; expired retry) and the caller should
3090240639Sadrian * free it and handle it like a failure (eg by sending a BAR.)
3091240639Sadrian */
3092240639Sadrianstatic int
3093240639Sadrianath_tx_tid_filt_comp_single(struct ath_softc *sc, struct ath_tid *tid,
3094240639Sadrian    struct ath_buf *bf)
3095240639Sadrian{
3096240639Sadrian	struct ath_buf *nbf;
3097240639Sadrian	int retval;
3098240639Sadrian
3099243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
3100240639Sadrian
3101240639Sadrian	/*
3102240639Sadrian	 * Don't allow a filtered frame to live forever.
3103240639Sadrian	 */
3104240639Sadrian	if (bf->bf_state.bfs_retries > SWMAX_RETRIES) {
3105241170Sadrian		sc->sc_stats.ast_tx_swretrymax++;
3106240639Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3107240639Sadrian		    "%s: bf=%p, seqno=%d, exceeded retries\n",
3108240639Sadrian		    __func__,
3109240639Sadrian		    bf,
3110240639Sadrian		    bf->bf_state.bfs_seqno);
3111240639Sadrian		return (0);
3112240639Sadrian	}
3113240639Sadrian
3114240639Sadrian	/*
3115240639Sadrian	 * A busy buffer can't be added to the retry list.
3116240639Sadrian	 * It needs to be cloned.
3117240639Sadrian	 */
3118240639Sadrian	if (bf->bf_flags & ATH_BUF_BUSY) {
3119240639Sadrian		nbf = ath_tx_retry_clone(sc, tid->an, tid, bf);
3120240639Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3121240639Sadrian		    "%s: busy buffer clone: %p -> %p\n",
3122240639Sadrian		    __func__, bf, nbf);
3123240639Sadrian	} else {
3124240639Sadrian		nbf = bf;
3125240639Sadrian	}
3126240639Sadrian
3127240639Sadrian	if (nbf == NULL) {
3128240639Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3129240639Sadrian		    "%s: busy buffer couldn't be cloned (%p)!\n",
3130240639Sadrian		    __func__, bf);
3131240639Sadrian		retval = 1;
3132240639Sadrian	} else {
3133240639Sadrian		ath_tx_tid_filt_comp_buf(sc, tid, nbf);
3134240639Sadrian		retval = 0;
3135240639Sadrian	}
3136240639Sadrian	ath_tx_tid_filt_comp_complete(sc, tid);
3137240639Sadrian
3138240639Sadrian	return (retval);
3139240639Sadrian}
3140240639Sadrian
3141240639Sadrianstatic void
3142240639Sadrianath_tx_tid_filt_comp_aggr(struct ath_softc *sc, struct ath_tid *tid,
3143240639Sadrian    struct ath_buf *bf_first, ath_bufhead *bf_q)
3144240639Sadrian{
3145240639Sadrian	struct ath_buf *bf, *bf_next, *nbf;
3146240639Sadrian
3147243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
3148240639Sadrian
3149240639Sadrian	bf = bf_first;
3150240639Sadrian	while (bf) {
3151240639Sadrian		bf_next = bf->bf_next;
3152240639Sadrian		bf->bf_next = NULL;	/* Remove it from the aggr list */
3153240639Sadrian
3154240639Sadrian		/*
3155240639Sadrian		 * Don't allow a filtered frame to live forever.
3156240639Sadrian		 */
3157240639Sadrian		if (bf->bf_state.bfs_retries > SWMAX_RETRIES) {
3158247029Sadrian			sc->sc_stats.ast_tx_swretrymax++;
3159240639Sadrian			DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3160240639Sadrian			    "%s: bf=%p, seqno=%d, exceeded retries\n",
3161240639Sadrian			    __func__,
3162240639Sadrian			    bf,
3163240639Sadrian			    bf->bf_state.bfs_seqno);
3164240639Sadrian			TAILQ_INSERT_TAIL(bf_q, bf, bf_list);
3165240639Sadrian			goto next;
3166240639Sadrian		}
3167240639Sadrian
3168240639Sadrian		if (bf->bf_flags & ATH_BUF_BUSY) {
3169240639Sadrian			nbf = ath_tx_retry_clone(sc, tid->an, tid, bf);
3170240639Sadrian			DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3171240639Sadrian			    "%s: busy buffer cloned: %p -> %p",
3172240639Sadrian			    __func__, bf, nbf);
3173240639Sadrian		} else {
3174240639Sadrian			nbf = bf;
3175240639Sadrian		}
3176240639Sadrian
3177240639Sadrian		/*
3178240639Sadrian		 * If the buffer couldn't be cloned, add it to bf_q;
3179240639Sadrian		 * the caller will free the buffer(s) as required.
3180240639Sadrian		 */
3181240639Sadrian		if (nbf == NULL) {
3182240639Sadrian			DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
3183240639Sadrian			    "%s: buffer couldn't be cloned! (%p)\n",
3184240639Sadrian			    __func__, bf);
3185240639Sadrian			TAILQ_INSERT_TAIL(bf_q, bf, bf_list);
3186240639Sadrian		} else {
3187240639Sadrian			ath_tx_tid_filt_comp_buf(sc, tid, nbf);
3188240639Sadrian		}
3189240639Sadriannext:
3190240639Sadrian		bf = bf_next;
3191240639Sadrian	}
3192240639Sadrian
3193240639Sadrian	ath_tx_tid_filt_comp_complete(sc, tid);
3194240639Sadrian}
3195240639Sadrian
3196240639Sadrian/*
3197233908Sadrian * Suspend the queue because we need to TX a BAR.
3198233908Sadrian */
3199233908Sadrianstatic void
3200233908Sadrianath_tx_tid_bar_suspend(struct ath_softc *sc, struct ath_tid *tid)
3201233908Sadrian{
3202233908Sadrian
3203243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
3204243786Sadrian
3205235491Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
3206235676Sadrian	    "%s: tid=%p, bar_wait=%d, bar_tx=%d, called\n",
3207233908Sadrian	    __func__,
3208235676Sadrian	    tid,
3209235676Sadrian	    tid->bar_wait,
3210235676Sadrian	    tid->bar_tx);
3211233908Sadrian
3212233908Sadrian	/* We shouldn't be called when bar_tx is 1 */
3213233908Sadrian	if (tid->bar_tx) {
3214233908Sadrian		device_printf(sc->sc_dev, "%s: bar_tx is 1?!\n",
3215233908Sadrian		    __func__);
3216233908Sadrian	}
3217233908Sadrian
3218233908Sadrian	/* If we've already been called, just be patient. */
3219233908Sadrian	if (tid->bar_wait)
3220233908Sadrian		return;
3221233908Sadrian
3222233908Sadrian	/* Wait! */
3223233908Sadrian	tid->bar_wait = 1;
3224233908Sadrian
3225233908Sadrian	/* Only one pause, no matter how many frames fail */
3226233908Sadrian	ath_tx_tid_pause(sc, tid);
3227233908Sadrian}
3228233908Sadrian
3229233908Sadrian/*
3230233908Sadrian * We've finished with BAR handling - either we succeeded or
3231233908Sadrian * failed. Either way, unsuspend TX.
3232233908Sadrian */
3233233908Sadrianstatic void
3234233908Sadrianath_tx_tid_bar_unsuspend(struct ath_softc *sc, struct ath_tid *tid)
3235233908Sadrian{
3236233908Sadrian
3237243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
3238243786Sadrian
3239235491Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
3240233908Sadrian	    "%s: tid=%p, called\n",
3241233908Sadrian	    __func__,
3242233908Sadrian	    tid);
3243233908Sadrian
3244233908Sadrian	if (tid->bar_tx == 0 || tid->bar_wait == 0) {
3245233908Sadrian		device_printf(sc->sc_dev, "%s: bar_tx=%d, bar_wait=%d: ?\n",
3246233908Sadrian		    __func__, tid->bar_tx, tid->bar_wait);
3247233908Sadrian	}
3248233908Sadrian
3249233908Sadrian	tid->bar_tx = tid->bar_wait = 0;
3250233908Sadrian	ath_tx_tid_resume(sc, tid);
3251233908Sadrian}
3252233908Sadrian
3253233908Sadrian/*
3254233908Sadrian * Return whether we're ready to TX a BAR frame.
3255233908Sadrian *
3256233908Sadrian * Requires the TID lock be held.
3257233908Sadrian */
3258233908Sadrianstatic int
3259233908Sadrianath_tx_tid_bar_tx_ready(struct ath_softc *sc, struct ath_tid *tid)
3260233908Sadrian{
3261233908Sadrian
3262243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
3263233908Sadrian
3264233908Sadrian	if (tid->bar_wait == 0 || tid->hwq_depth > 0)
3265233908Sadrian		return (0);
3266233908Sadrian
3267235491Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR, "%s: tid=%p (%d), bar ready\n",
3268235491Sadrian	    __func__, tid, tid->tid);
3269235491Sadrian
3270233908Sadrian	return (1);
3271233908Sadrian}
3272233908Sadrian
3273233908Sadrian/*
3274233908Sadrian * Check whether the current TID is ready to have a BAR
3275233908Sadrian * TXed and if so, do the TX.
3276233908Sadrian *
3277233908Sadrian * Since the TID/TXQ lock can't be held during a call to
3278233908Sadrian * ieee80211_send_bar(), we have to do the dirty thing of unlocking it,
3279233908Sadrian * sending the BAR and locking it again.
3280233908Sadrian *
3281233908Sadrian * Eventually, the code to send the BAR should be broken out
3282233908Sadrian * from this routine so the lock doesn't have to be reacquired
3283233908Sadrian * just to be immediately dropped by the caller.
3284233908Sadrian */
3285233908Sadrianstatic void
3286233908Sadrianath_tx_tid_bar_tx(struct ath_softc *sc, struct ath_tid *tid)
3287233908Sadrian{
3288233908Sadrian	struct ieee80211_tx_ampdu *tap;
3289233908Sadrian
3290243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
3291233908Sadrian
3292235491Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
3293233908Sadrian	    "%s: tid=%p, called\n",
3294233908Sadrian	    __func__,
3295233908Sadrian	    tid);
3296233908Sadrian
3297233908Sadrian	tap = ath_tx_get_tx_tid(tid->an, tid->tid);
3298233908Sadrian
3299233908Sadrian	/*
3300233908Sadrian	 * This is an error condition!
3301233908Sadrian	 */
3302233908Sadrian	if (tid->bar_wait == 0 || tid->bar_tx == 1) {
3303233908Sadrian		device_printf(sc->sc_dev,
3304233908Sadrian		    "%s: tid=%p, bar_tx=%d, bar_wait=%d: ?\n",
3305233908Sadrian		    __func__,
3306233908Sadrian		    tid,
3307233908Sadrian		    tid->bar_tx,
3308233908Sadrian		    tid->bar_wait);
3309233908Sadrian		return;
3310233908Sadrian	}
3311233908Sadrian
3312233908Sadrian	/* Don't do anything if we still have pending frames */
3313233908Sadrian	if (tid->hwq_depth > 0) {
3314235491Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
3315233908Sadrian		    "%s: tid=%p, hwq_depth=%d, waiting\n",
3316233908Sadrian		    __func__,
3317233908Sadrian		    tid,
3318233908Sadrian		    tid->hwq_depth);
3319233908Sadrian		return;
3320233908Sadrian	}
3321233908Sadrian
3322233908Sadrian	/* We're now about to TX */
3323233908Sadrian	tid->bar_tx = 1;
3324233908Sadrian
3325233908Sadrian	/*
3326240724Sadrian	 * Override the clrdmask configuration for the next frame,
3327240724Sadrian	 * just to get the ball rolling.
3328240724Sadrian	 */
3329245708Sadrian	ath_tx_set_clrdmask(sc, tid->an);
3330240724Sadrian
3331240724Sadrian	/*
3332233908Sadrian	 * Calculate new BAW left edge, now that all frames have either
3333233908Sadrian	 * succeeded or failed.
3334233908Sadrian	 *
3335233908Sadrian	 * XXX verify this is _actually_ the valid value to begin at!
3336233908Sadrian	 */
3337235491Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
3338233908Sadrian	    "%s: tid=%p, new BAW left edge=%d\n",
3339233908Sadrian	    __func__,
3340233908Sadrian	    tid,
3341233908Sadrian	    tap->txa_start);
3342233908Sadrian
3343233908Sadrian	/* Try sending the BAR frame */
3344233908Sadrian	/* We can't hold the lock here! */
3345233908Sadrian
3346243786Sadrian	ATH_TX_UNLOCK(sc);
3347233908Sadrian	if (ieee80211_send_bar(&tid->an->an_node, tap, tap->txa_start) == 0) {
3348233908Sadrian		/* Success? Now we wait for notification that it's done */
3349243786Sadrian		ATH_TX_LOCK(sc);
3350233908Sadrian		return;
3351233908Sadrian	}
3352233908Sadrian
3353233908Sadrian	/* Failure? For now, warn loudly and continue */
3354243786Sadrian	ATH_TX_LOCK(sc);
3355233908Sadrian	device_printf(sc->sc_dev, "%s: tid=%p, failed to TX BAR, continue!\n",
3356233908Sadrian	    __func__, tid);
3357233908Sadrian	ath_tx_tid_bar_unsuspend(sc, tid);
3358233908Sadrian}
3359233908Sadrian
3360240639Sadrianstatic void
3361240639Sadrianath_tx_tid_drain_pkt(struct ath_softc *sc, struct ath_node *an,
3362240639Sadrian    struct ath_tid *tid, ath_bufhead *bf_cq, struct ath_buf *bf)
3363240639Sadrian{
3364233908Sadrian
3365243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
3366240639Sadrian
3367240639Sadrian	/*
3368240639Sadrian	 * If the current TID is running AMPDU, update
3369240639Sadrian	 * the BAW.
3370240639Sadrian	 */
3371240639Sadrian	if (ath_tx_ampdu_running(sc, an, tid->tid) &&
3372240639Sadrian	    bf->bf_state.bfs_dobaw) {
3373240639Sadrian		/*
3374240639Sadrian		 * Only remove the frame from the BAW if it's
3375240639Sadrian		 * been transmitted at least once; this means
3376240639Sadrian		 * the frame was in the BAW to begin with.
3377240639Sadrian		 */
3378240639Sadrian		if (bf->bf_state.bfs_retries > 0) {
3379240639Sadrian			ath_tx_update_baw(sc, an, tid, bf);
3380240639Sadrian			bf->bf_state.bfs_dobaw = 0;
3381240639Sadrian		}
3382247135Sadrian#if 0
3383240639Sadrian		/*
3384240639Sadrian		 * This has become a non-fatal error now
3385240639Sadrian		 */
3386240639Sadrian		if (! bf->bf_state.bfs_addedbaw)
3387240639Sadrian			device_printf(sc->sc_dev,
3388240639Sadrian			    "%s: wasn't added: seqno %d\n",
3389240639Sadrian			    __func__, SEQNO(bf->bf_state.bfs_seqno));
3390247135Sadrian#endif
3391240639Sadrian	}
3392248671Sadrian
3393248671Sadrian	/* Strip it out of an aggregate list if it was in one */
3394248671Sadrian	bf->bf_next = NULL;
3395248671Sadrian
3396248671Sadrian	/* Insert on the free queue to be freed by the caller */
3397240639Sadrian	TAILQ_INSERT_TAIL(bf_cq, bf, bf_list);
3398240639Sadrian}
3399240639Sadrian
3400240639Sadrianstatic void
3401240639Sadrianath_tx_tid_drain_print(struct ath_softc *sc, struct ath_node *an,
3402240899Sadrian    const char *pfx, struct ath_tid *tid, struct ath_buf *bf)
3403240639Sadrian{
3404240639Sadrian	struct ieee80211_node *ni = &an->an_node;
3405240639Sadrian	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
3406240639Sadrian	struct ieee80211_tx_ampdu *tap;
3407240639Sadrian
3408240639Sadrian	tap = ath_tx_get_tx_tid(an, tid->tid);
3409240639Sadrian
3410240639Sadrian	device_printf(sc->sc_dev,
3411240899Sadrian	    "%s: %s: node %p: bf=%p: addbaw=%d, dobaw=%d, "
3412240639Sadrian	    "seqno=%d, retry=%d\n",
3413240899Sadrian	    __func__, pfx, ni, bf,
3414240639Sadrian	    bf->bf_state.bfs_addedbaw,
3415240639Sadrian	    bf->bf_state.bfs_dobaw,
3416240639Sadrian	    SEQNO(bf->bf_state.bfs_seqno),
3417240639Sadrian	    bf->bf_state.bfs_retries);
3418240639Sadrian	device_printf(sc->sc_dev,
3419240899Sadrian	    "%s: node %p: bf=%p: txq[%d] axq_depth=%d, axq_aggr_depth=%d\n",
3420240724Sadrian	        __func__, ni, bf,
3421240899Sadrian	    txq->axq_qnum,
3422240899Sadrian	    txq->axq_depth,
3423240899Sadrian	    txq->axq_aggr_depth);
3424240724Sadrian
3425240724Sadrian	device_printf(sc->sc_dev,
3426240639Sadrian	    "%s: node %p: bf=%p: tid txq_depth=%d hwq_depth=%d, bar_wait=%d, isfiltered=%d\n",
3427240639Sadrian	    __func__, ni, bf,
3428240639Sadrian	    tid->axq_depth,
3429240639Sadrian	    tid->hwq_depth,
3430240639Sadrian	    tid->bar_wait,
3431240639Sadrian	    tid->isfiltered);
3432240639Sadrian	device_printf(sc->sc_dev,
3433240724Sadrian	    "%s: node %p: tid %d: "
3434240724Sadrian	    "sched=%d, paused=%d, "
3435240724Sadrian	    "incomp=%d, baw_head=%d, "
3436240639Sadrian	    "baw_tail=%d txa_start=%d, ni_txseqs=%d\n",
3437240724Sadrian	     __func__, ni, tid->tid,
3438240724Sadrian	     tid->sched, tid->paused,
3439240724Sadrian	     tid->incomp, tid->baw_head,
3440240639Sadrian	     tid->baw_tail, tap == NULL ? -1 : tap->txa_start,
3441240639Sadrian	     ni->ni_txseqs[tid->tid]);
3442240639Sadrian
3443240639Sadrian	/* XXX Dump the frame, see what it is? */
3444240639Sadrian	ieee80211_dump_pkt(ni->ni_ic,
3445240639Sadrian	    mtod(bf->bf_m, const uint8_t *),
3446240639Sadrian	    bf->bf_m->m_len, 0, -1);
3447240639Sadrian}
3448240639Sadrian
3449233908Sadrian/*
3450227364Sadrian * Free any packets currently pending in the software TX queue.
3451227364Sadrian *
3452227364Sadrian * This will be called when a node is being deleted.
3453227364Sadrian *
3454227364Sadrian * It can also be called on an active node during an interface
3455227364Sadrian * reset or state transition.
3456227364Sadrian *
3457227364Sadrian * (From Linux/reference):
3458227364Sadrian *
3459227364Sadrian * TODO: For frame(s) that are in the retry state, we will reuse the
3460227364Sadrian * sequence number(s) without setting the retry bit. The
3461227364Sadrian * alternative is to give up on these and BAR the receiver's window
3462227364Sadrian * forward.
3463227364Sadrian */
3464227364Sadrianstatic void
3465229949Sadrianath_tx_tid_drain(struct ath_softc *sc, struct ath_node *an,
3466229949Sadrian    struct ath_tid *tid, ath_bufhead *bf_cq)
3467227364Sadrian{
3468227364Sadrian	struct ath_buf *bf;
3469227364Sadrian	struct ieee80211_tx_ampdu *tap;
3470227364Sadrian	struct ieee80211_node *ni = &an->an_node;
3471240639Sadrian	int t;
3472227364Sadrian
3473227364Sadrian	tap = ath_tx_get_tx_tid(an, tid->tid);
3474227364Sadrian
3475243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
3476227364Sadrian
3477227364Sadrian	/* Walk the queue, free frames */
3478240639Sadrian	t = 0;
3479227364Sadrian	for (;;) {
3480241336Sadrian		bf = ATH_TID_FIRST(tid);
3481227364Sadrian		if (bf == NULL) {
3482227364Sadrian			break;
3483227364Sadrian		}
3484227364Sadrian
3485227364Sadrian		if (t == 0) {
3486240899Sadrian			ath_tx_tid_drain_print(sc, an, "norm", tid, bf);
3487240639Sadrian			t = 1;
3488240639Sadrian		}
3489229165Sadrian
3490241336Sadrian		ATH_TID_REMOVE(tid, bf, bf_list);
3491240639Sadrian		ath_tx_tid_drain_pkt(sc, an, tid, bf_cq, bf);
3492240639Sadrian	}
3493229165Sadrian
3494240639Sadrian	/* And now, drain the filtered frame queue */
3495240639Sadrian	t = 0;
3496240639Sadrian	for (;;) {
3497241566Sadrian		bf = ATH_TID_FILT_FIRST(tid);
3498240639Sadrian		if (bf == NULL)
3499240639Sadrian			break;
3500240639Sadrian
3501240639Sadrian		if (t == 0) {
3502240899Sadrian			ath_tx_tid_drain_print(sc, an, "filt", tid, bf);
3503233897Sadrian			t = 1;
3504227364Sadrian		}
3505227364Sadrian
3506241566Sadrian		ATH_TID_FILT_REMOVE(tid, bf, bf_list);
3507240639Sadrian		ath_tx_tid_drain_pkt(sc, an, tid, bf_cq, bf);
3508227364Sadrian	}
3509227364Sadrian
3510227364Sadrian	/*
3511240724Sadrian	 * Override the clrdmask configuration for the next frame
3512240724Sadrian	 * in case there is some future transmission, just to get
3513240724Sadrian	 * the ball rolling.
3514240724Sadrian	 *
3515240724Sadrian	 * This won't hurt things if the TID is about to be freed.
3516240724Sadrian	 */
3517245708Sadrian	ath_tx_set_clrdmask(sc, tid->an);
3518240724Sadrian
3519240724Sadrian	/*
3520227364Sadrian	 * Now that it's completed, grab the TID lock and update
3521227364Sadrian	 * the sequence number and BAW window.
3522227364Sadrian	 * Because sequence numbers have been assigned to frames
3523227364Sadrian	 * that haven't been sent yet, it's entirely possible
3524227364Sadrian	 * we'll be called with some pending frames that have not
3525227364Sadrian	 * been transmitted.
3526227364Sadrian	 *
3527227364Sadrian	 * The cleaner solution is to do the sequence number allocation
3528227364Sadrian	 * when the packet is first transmitted - and thus the "retries"
3529227364Sadrian	 * check above would be enough to update the BAW/seqno.
3530227364Sadrian	 */
3531227364Sadrian
3532227364Sadrian	/* But don't do it for non-QoS TIDs */
3533227364Sadrian	if (tap) {
3534227364Sadrian#if 0
3535227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
3536227364Sadrian		    "%s: node %p: TID %d: sliding BAW left edge to %d\n",
3537227364Sadrian		    __func__, an, tid->tid, tap->txa_start);
3538227364Sadrian#endif
3539227364Sadrian		ni->ni_txseqs[tid->tid] = tap->txa_start;
3540227364Sadrian		tid->baw_tail = tid->baw_head;
3541227364Sadrian	}
3542227364Sadrian}
3543227364Sadrian
3544227364Sadrian/*
3545227364Sadrian * Flush all software queued packets for the given node.
3546227364Sadrian *
3547227364Sadrian * This occurs when a completion handler frees the last buffer
3548227364Sadrian * for a node, and the node is thus freed. This causes the node
3549227364Sadrian * to be cleaned up, which ends up calling ath_tx_node_flush.
3550227364Sadrian */
3551227364Sadrianvoid
3552227364Sadrianath_tx_node_flush(struct ath_softc *sc, struct ath_node *an)
3553227364Sadrian{
3554227364Sadrian	int tid;
3555227364Sadrian	ath_bufhead bf_cq;
3556227364Sadrian	struct ath_buf *bf;
3557227364Sadrian
3558227364Sadrian	TAILQ_INIT(&bf_cq);
3559227364Sadrian
3560240899Sadrian	ATH_KTR(sc, ATH_KTR_NODE, 1, "ath_tx_node_flush: flush node; ni=%p",
3561240899Sadrian	    &an->an_node);
3562240899Sadrian
3563243786Sadrian	ATH_TX_LOCK(sc);
3564227364Sadrian	for (tid = 0; tid < IEEE80211_TID_SIZE; tid++) {
3565227364Sadrian		struct ath_tid *atid = &an->an_tid[tid];
3566227364Sadrian
3567227364Sadrian		/* Free packets */
3568227364Sadrian		ath_tx_tid_drain(sc, an, atid, &bf_cq);
3569240914Sadrian		/* Remove this tid from the list of active tids */
3570240914Sadrian		ath_tx_tid_unsched(sc, atid);
3571227364Sadrian	}
3572243786Sadrian	ATH_TX_UNLOCK(sc);
3573227364Sadrian
3574227364Sadrian	/* Handle completed frames */
3575227364Sadrian	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
3576227364Sadrian		TAILQ_REMOVE(&bf_cq, bf, bf_list);
3577227364Sadrian		ath_tx_default_comp(sc, bf, 0);
3578227364Sadrian	}
3579227364Sadrian}
3580227364Sadrian
3581227364Sadrian/*
3582227364Sadrian * Drain all the software TXQs currently with traffic queued.
3583227364Sadrian */
3584227364Sadrianvoid
3585227364Sadrianath_tx_txq_drain(struct ath_softc *sc, struct ath_txq *txq)
3586227364Sadrian{
3587227364Sadrian	struct ath_tid *tid;
3588227364Sadrian	ath_bufhead bf_cq;
3589227364Sadrian	struct ath_buf *bf;
3590227364Sadrian
3591227364Sadrian	TAILQ_INIT(&bf_cq);
3592243786Sadrian	ATH_TX_LOCK(sc);
3593227364Sadrian
3594227364Sadrian	/*
3595227364Sadrian	 * Iterate over all active tids for the given txq,
3596227364Sadrian	 * flushing and unsched'ing them
3597227364Sadrian	 */
3598227364Sadrian	while (! TAILQ_EMPTY(&txq->axq_tidq)) {
3599227364Sadrian		tid = TAILQ_FIRST(&txq->axq_tidq);
3600227364Sadrian		ath_tx_tid_drain(sc, tid->an, tid, &bf_cq);
3601227364Sadrian		ath_tx_tid_unsched(sc, tid);
3602227364Sadrian	}
3603227364Sadrian
3604243786Sadrian	ATH_TX_UNLOCK(sc);
3605227364Sadrian
3606227364Sadrian	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
3607227364Sadrian		TAILQ_REMOVE(&bf_cq, bf, bf_list);
3608227364Sadrian		ath_tx_default_comp(sc, bf, 0);
3609227364Sadrian	}
3610227364Sadrian}
3611227364Sadrian
3612227364Sadrian/*
3613227364Sadrian * Handle completion of non-aggregate session frames.
3614240883Sadrian *
3615240883Sadrian * This (currently) doesn't implement software retransmission of
3616240883Sadrian * non-aggregate frames!
3617240883Sadrian *
3618240883Sadrian * Software retransmission of non-aggregate frames needs to obey
3619240883Sadrian * the strict sequence number ordering, and drop any frames that
3620240883Sadrian * will fail this.
3621240883Sadrian *
3622240883Sadrian * For now, filtered frames and frame transmission will cause
3623240883Sadrian * all kinds of issues.  So we don't support them.
3624240883Sadrian *
3625240883Sadrian * So anyone queuing frames via ath_tx_normal_xmit() or
3626240883Sadrian * ath_tx_hw_queue_norm() must override and set CLRDMASK.
3627227364Sadrian */
3628227364Sadrianvoid
3629227364Sadrianath_tx_normal_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
3630227364Sadrian{
3631227364Sadrian	struct ieee80211_node *ni = bf->bf_node;
3632227364Sadrian	struct ath_node *an = ATH_NODE(ni);
3633227364Sadrian	int tid = bf->bf_state.bfs_tid;
3634227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
3635227364Sadrian	struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
3636227364Sadrian
3637227364Sadrian	/* The TID state is protected behind the TXQ lock */
3638243786Sadrian	ATH_TX_LOCK(sc);
3639227364Sadrian
3640227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bf=%p: fail=%d, hwq_depth now %d\n",
3641227364Sadrian	    __func__, bf, fail, atid->hwq_depth - 1);
3642227364Sadrian
3643227364Sadrian	atid->hwq_depth--;
3644240639Sadrian
3645240883Sadrian#if 0
3646240883Sadrian	/*
3647240883Sadrian	 * If the frame was filtered, stick it on the filter frame
3648240883Sadrian	 * queue and complain about it.  It shouldn't happen!
3649240883Sadrian	 */
3650240883Sadrian	if ((ts->ts_status & HAL_TXERR_FILT) ||
3651240883Sadrian	    (ts->ts_status != 0 && atid->isfiltered)) {
3652240883Sadrian		device_printf(sc->sc_dev,
3653240883Sadrian		    "%s: isfiltered=%d, ts_status=%d: huh?\n",
3654240883Sadrian		    __func__,
3655240883Sadrian		    atid->isfiltered,
3656240883Sadrian		    ts->ts_status);
3657240883Sadrian		ath_tx_tid_filt_comp_buf(sc, atid, bf);
3658240883Sadrian	}
3659240883Sadrian#endif
3660240639Sadrian	if (atid->isfiltered)
3661240883Sadrian		device_printf(sc->sc_dev, "%s: filtered?!\n", __func__);
3662227364Sadrian	if (atid->hwq_depth < 0)
3663227364Sadrian		device_printf(sc->sc_dev, "%s: hwq_depth < 0: %d\n",
3664227364Sadrian		    __func__, atid->hwq_depth);
3665240639Sadrian
3666240639Sadrian	/*
3667240639Sadrian	 * If the queue is filtered, potentially mark it as complete
3668240639Sadrian	 * and reschedule it as needed.
3669240639Sadrian	 *
3670240639Sadrian	 * This is required as there may be a subsequent TX descriptor
3671240639Sadrian	 * for this end-node that has CLRDMASK set, so it's quite possible
3672240639Sadrian	 * that a filtered frame will be followed by a non-filtered
3673240639Sadrian	 * (complete or otherwise) frame.
3674240639Sadrian	 *
3675240639Sadrian	 * XXX should we do this before we complete the frame?
3676240639Sadrian	 */
3677240639Sadrian	if (atid->isfiltered)
3678240639Sadrian		ath_tx_tid_filt_comp_complete(sc, atid);
3679243786Sadrian	ATH_TX_UNLOCK(sc);
3680227364Sadrian
3681227364Sadrian	/*
3682227364Sadrian	 * punt to rate control if we're not being cleaned up
3683227364Sadrian	 * during a hw queue drain and the frame wanted an ACK.
3684227364Sadrian	 */
3685233966Sadrian	if (fail == 0 && ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0))
3686227364Sadrian		ath_tx_update_ratectrl(sc, ni, bf->bf_state.bfs_rc,
3687227364Sadrian		    ts, bf->bf_state.bfs_pktlen,
3688227364Sadrian		    1, (ts->ts_status == 0) ? 0 : 1);
3689227364Sadrian
3690227364Sadrian	ath_tx_default_comp(sc, bf, fail);
3691227364Sadrian}
3692227364Sadrian
3693227364Sadrian/*
3694227364Sadrian * Handle cleanup of aggregate session packets that aren't
3695227364Sadrian * an A-MPDU.
3696227364Sadrian *
3697227364Sadrian * There's no need to update the BAW here - the session is being
3698227364Sadrian * torn down.
3699227364Sadrian */
3700227364Sadrianstatic void
3701227364Sadrianath_tx_comp_cleanup_unaggr(struct ath_softc *sc, struct ath_buf *bf)
3702227364Sadrian{
3703227364Sadrian	struct ieee80211_node *ni = bf->bf_node;
3704227364Sadrian	struct ath_node *an = ATH_NODE(ni);
3705227364Sadrian	int tid = bf->bf_state.bfs_tid;
3706227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
3707227364Sadrian
3708227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: TID %d: incomp=%d\n",
3709227364Sadrian	    __func__, tid, atid->incomp);
3710227364Sadrian
3711243786Sadrian	ATH_TX_LOCK(sc);
3712227364Sadrian	atid->incomp--;
3713227364Sadrian	if (atid->incomp == 0) {
3714227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
3715227364Sadrian		    "%s: TID %d: cleaned up! resume!\n",
3716227364Sadrian		    __func__, tid);
3717227364Sadrian		atid->cleanup_inprogress = 0;
3718227364Sadrian		ath_tx_tid_resume(sc, atid);
3719227364Sadrian	}
3720243786Sadrian	ATH_TX_UNLOCK(sc);
3721227364Sadrian
3722227364Sadrian	ath_tx_default_comp(sc, bf, 0);
3723227364Sadrian}
3724227364Sadrian
3725227364Sadrian/*
3726227364Sadrian * Performs transmit side cleanup when TID changes from aggregated to
3727227364Sadrian * unaggregated.
3728227364Sadrian *
3729227364Sadrian * - Discard all retry frames from the s/w queue.
3730227364Sadrian * - Fix the tx completion function for all buffers in s/w queue.
3731227364Sadrian * - Count the number of unacked frames, and let transmit completion
3732227364Sadrian *   handle it later.
3733227364Sadrian *
3734227364Sadrian * The caller is responsible for pausing the TID.
3735227364Sadrian */
3736227364Sadrianstatic void
3737235749Sadrianath_tx_tid_cleanup(struct ath_softc *sc, struct ath_node *an, int tid)
3738227364Sadrian{
3739227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
3740227364Sadrian	struct ieee80211_tx_ampdu *tap;
3741227364Sadrian	struct ath_buf *bf, *bf_next;
3742227364Sadrian	ath_bufhead bf_cq;
3743227364Sadrian
3744235774Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
3745227364Sadrian	    "%s: TID %d: called\n", __func__, tid);
3746227364Sadrian
3747227364Sadrian	TAILQ_INIT(&bf_cq);
3748243786Sadrian	ATH_TX_LOCK(sc);
3749227364Sadrian
3750227364Sadrian	/*
3751240639Sadrian	 * Move the filtered frames to the TX queue, before
3752240639Sadrian	 * we run off and discard/process things.
3753240639Sadrian	 */
3754240639Sadrian	/* XXX this is really quite inefficient */
3755241566Sadrian	while ((bf = ATH_TID_FILT_LAST(atid, ath_bufhead_s)) != NULL) {
3756241566Sadrian		ATH_TID_FILT_REMOVE(atid, bf, bf_list);
3757241336Sadrian		ATH_TID_INSERT_HEAD(atid, bf, bf_list);
3758240639Sadrian	}
3759240639Sadrian
3760240639Sadrian	/*
3761227364Sadrian	 * Update the frames in the software TX queue:
3762227364Sadrian	 *
3763227364Sadrian	 * + Discard retry frames in the queue
3764227364Sadrian	 * + Fix the completion function to be non-aggregate
3765227364Sadrian	 */
3766241336Sadrian	bf = ATH_TID_FIRST(atid);
3767227364Sadrian	while (bf) {
3768227364Sadrian		if (bf->bf_state.bfs_isretried) {
3769227364Sadrian			bf_next = TAILQ_NEXT(bf, bf_list);
3770241336Sadrian			ATH_TID_REMOVE(atid, bf, bf_list);
3771227364Sadrian			if (bf->bf_state.bfs_dobaw) {
3772227364Sadrian				ath_tx_update_baw(sc, an, atid, bf);
3773227364Sadrian				if (! bf->bf_state.bfs_addedbaw)
3774227364Sadrian					device_printf(sc->sc_dev,
3775227364Sadrian					    "%s: wasn't added: seqno %d\n",
3776229949Sadrian					    __func__,
3777229949Sadrian					    SEQNO(bf->bf_state.bfs_seqno));
3778227364Sadrian			}
3779227364Sadrian			bf->bf_state.bfs_dobaw = 0;
3780227364Sadrian			/*
3781227364Sadrian			 * Call the default completion handler with "fail" just
3782227364Sadrian			 * so upper levels are suitably notified about this.
3783227364Sadrian			 */
3784227364Sadrian			TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
3785227364Sadrian			bf = bf_next;
3786227364Sadrian			continue;
3787227364Sadrian		}
3788227364Sadrian		/* Give these the default completion handler */
3789227364Sadrian		bf->bf_comp = ath_tx_normal_comp;
3790227364Sadrian		bf = TAILQ_NEXT(bf, bf_list);
3791227364Sadrian	}
3792227364Sadrian
3793227364Sadrian	/* The caller is required to pause the TID */
3794227364Sadrian#if 0
3795227364Sadrian	/* Pause the TID */
3796227364Sadrian	ath_tx_tid_pause(sc, atid);
3797227364Sadrian#endif
3798227364Sadrian
3799227364Sadrian	/*
3800227364Sadrian	 * Calculate what hardware-queued frames exist based
3801227364Sadrian	 * on the current BAW size. Ie, what frames have been
3802227364Sadrian	 * added to the TX hardware queue for this TID but
3803227364Sadrian	 * not yet ACKed.
3804227364Sadrian	 */
3805227364Sadrian	tap = ath_tx_get_tx_tid(an, tid);
3806227364Sadrian	/* Need the lock - fiddling with BAW */
3807227364Sadrian	while (atid->baw_head != atid->baw_tail) {
3808227364Sadrian		if (atid->tx_buf[atid->baw_head]) {
3809227364Sadrian			atid->incomp++;
3810227364Sadrian			atid->cleanup_inprogress = 1;
3811227364Sadrian			atid->tx_buf[atid->baw_head] = NULL;
3812227364Sadrian		}
3813227364Sadrian		INCR(atid->baw_head, ATH_TID_MAX_BUFS);
3814227364Sadrian		INCR(tap->txa_start, IEEE80211_SEQ_RANGE);
3815227364Sadrian	}
3816227364Sadrian
3817227364Sadrian	/*
3818227364Sadrian	 * If cleanup is required, defer TID scheduling
3819227364Sadrian	 * until all the HW queued packets have been
3820227364Sadrian	 * sent.
3821227364Sadrian	 */
3822227364Sadrian	if (! atid->cleanup_inprogress)
3823227364Sadrian		ath_tx_tid_resume(sc, atid);
3824227364Sadrian
3825227364Sadrian	if (atid->cleanup_inprogress)
3826227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
3827227364Sadrian		    "%s: TID %d: cleanup needed: %d packets\n",
3828227364Sadrian		    __func__, tid, atid->incomp);
3829243786Sadrian	ATH_TX_UNLOCK(sc);
3830227364Sadrian
3831227364Sadrian	/* Handle completing frames and fail them */
3832227364Sadrian	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
3833227364Sadrian		TAILQ_REMOVE(&bf_cq, bf, bf_list);
3834227364Sadrian		ath_tx_default_comp(sc, bf, 1);
3835227364Sadrian	}
3836227364Sadrian}
3837227364Sadrian
3838227364Sadrianstatic struct ath_buf *
3839227398Sadrianath_tx_retry_clone(struct ath_softc *sc, struct ath_node *an,
3840227398Sadrian    struct ath_tid *tid, struct ath_buf *bf)
3841227364Sadrian{
3842227364Sadrian	struct ath_buf *nbf;
3843227364Sadrian	int error;
3844227364Sadrian
3845248988Sadrian	/*
3846248988Sadrian	 * Clone the buffer.  This will handle the dma unmap and
3847248988Sadrian	 * copy the node reference to the new buffer.  If this
3848248988Sadrian	 * works out, 'bf' will have no DMA mapping, no mbuf
3849248988Sadrian	 * pointer and no node reference.
3850248988Sadrian	 */
3851227364Sadrian	nbf = ath_buf_clone(sc, bf);
3852227364Sadrian
3853227364Sadrian#if 0
3854227364Sadrian	device_printf(sc->sc_dev, "%s: ATH_BUF_BUSY; cloning\n",
3855227364Sadrian	    __func__);
3856227364Sadrian#endif
3857227364Sadrian
3858227364Sadrian	if (nbf == NULL) {
3859227364Sadrian		/* Failed to clone */
3860227364Sadrian		device_printf(sc->sc_dev,
3861227364Sadrian		    "%s: failed to clone a busy buffer\n",
3862227364Sadrian		    __func__);
3863227364Sadrian		return NULL;
3864227364Sadrian	}
3865227364Sadrian
3866227364Sadrian	/* Setup the dma for the new buffer */
3867227364Sadrian	error = ath_tx_dmasetup(sc, nbf, nbf->bf_m);
3868227364Sadrian	if (error != 0) {
3869227364Sadrian		device_printf(sc->sc_dev,
3870227364Sadrian		    "%s: failed to setup dma for clone\n",
3871227364Sadrian		    __func__);
3872227364Sadrian		/*
3873227364Sadrian		 * Put this at the head of the list, not tail;
3874227364Sadrian		 * that way it doesn't interfere with the
3875227364Sadrian		 * busy buffer logic (which uses the tail of
3876227364Sadrian		 * the list.)
3877227364Sadrian		 */
3878227364Sadrian		ATH_TXBUF_LOCK(sc);
3879236994Sadrian		ath_returnbuf_head(sc, nbf);
3880227364Sadrian		ATH_TXBUF_UNLOCK(sc);
3881227364Sadrian		return NULL;
3882227364Sadrian	}
3883227364Sadrian
3884227398Sadrian	/* Update BAW if required, before we free the original buf */
3885227398Sadrian	if (bf->bf_state.bfs_dobaw)
3886227398Sadrian		ath_tx_switch_baw_buf(sc, an, tid, bf, nbf);
3887227398Sadrian
3888248988Sadrian	/* Free original buffer; return new buffer */
3889227364Sadrian	ath_freebuf(sc, bf);
3890240639Sadrian
3891227364Sadrian	return nbf;
3892227364Sadrian}
3893227364Sadrian
3894227364Sadrian/*
3895227364Sadrian * Handle retrying an unaggregate frame in an aggregate
3896227364Sadrian * session.
3897227364Sadrian *
3898227364Sadrian * If too many retries occur, pause the TID, wait for
3899227364Sadrian * any further retransmits (as there's no reason why
3900227364Sadrian * non-aggregate frames in an aggregate session are
3901227364Sadrian * transmitted in-order; they just have to be in-BAW)
3902227364Sadrian * and then queue a BAR.
3903227364Sadrian */
3904227364Sadrianstatic void
3905227364Sadrianath_tx_aggr_retry_unaggr(struct ath_softc *sc, struct ath_buf *bf)
3906227364Sadrian{
3907227364Sadrian	struct ieee80211_node *ni = bf->bf_node;
3908227364Sadrian	struct ath_node *an = ATH_NODE(ni);
3909227364Sadrian	int tid = bf->bf_state.bfs_tid;
3910227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
3911227364Sadrian	struct ieee80211_tx_ampdu *tap;
3912227364Sadrian
3913243786Sadrian	ATH_TX_LOCK(sc);
3914227364Sadrian
3915227364Sadrian	tap = ath_tx_get_tx_tid(an, tid);
3916227364Sadrian
3917227364Sadrian	/*
3918227364Sadrian	 * If the buffer is marked as busy, we can't directly
3919227364Sadrian	 * reuse it. Instead, try to clone the buffer.
3920227364Sadrian	 * If the clone is successful, recycle the old buffer.
3921227364Sadrian	 * If the clone is unsuccessful, set bfs_retries to max
3922227364Sadrian	 * to force the next bit of code to free the buffer
3923227364Sadrian	 * for us.
3924227364Sadrian	 */
3925227364Sadrian	if ((bf->bf_state.bfs_retries < SWMAX_RETRIES) &&
3926227364Sadrian	    (bf->bf_flags & ATH_BUF_BUSY)) {
3927227364Sadrian		struct ath_buf *nbf;
3928227398Sadrian		nbf = ath_tx_retry_clone(sc, an, atid, bf);
3929227364Sadrian		if (nbf)
3930227364Sadrian			/* bf has been freed at this point */
3931227364Sadrian			bf = nbf;
3932227364Sadrian		else
3933227364Sadrian			bf->bf_state.bfs_retries = SWMAX_RETRIES + 1;
3934227364Sadrian	}
3935227364Sadrian
3936227364Sadrian	if (bf->bf_state.bfs_retries >= SWMAX_RETRIES) {
3937227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_RETRIES,
3938227364Sadrian		    "%s: exceeded retries; seqno %d\n",
3939227364Sadrian		    __func__, SEQNO(bf->bf_state.bfs_seqno));
3940227364Sadrian		sc->sc_stats.ast_tx_swretrymax++;
3941227364Sadrian
3942227364Sadrian		/* Update BAW anyway */
3943227364Sadrian		if (bf->bf_state.bfs_dobaw) {
3944227364Sadrian			ath_tx_update_baw(sc, an, atid, bf);
3945227364Sadrian			if (! bf->bf_state.bfs_addedbaw)
3946227364Sadrian				device_printf(sc->sc_dev,
3947227364Sadrian				    "%s: wasn't added: seqno %d\n",
3948227364Sadrian				    __func__, SEQNO(bf->bf_state.bfs_seqno));
3949227364Sadrian		}
3950227364Sadrian		bf->bf_state.bfs_dobaw = 0;
3951227364Sadrian
3952233908Sadrian		/* Suspend the TX queue and get ready to send the BAR */
3953233908Sadrian		ath_tx_tid_bar_suspend(sc, atid);
3954227364Sadrian
3955233908Sadrian		/* Send the BAR if there are no other frames waiting */
3956233908Sadrian		if (ath_tx_tid_bar_tx_ready(sc, atid))
3957233908Sadrian			ath_tx_tid_bar_tx(sc, atid);
3958227364Sadrian
3959243786Sadrian		ATH_TX_UNLOCK(sc);
3960227364Sadrian
3961227364Sadrian		/* Free buffer, bf is free after this call */
3962227364Sadrian		ath_tx_default_comp(sc, bf, 0);
3963227364Sadrian		return;
3964227364Sadrian	}
3965227364Sadrian
3966227364Sadrian	/*
3967227364Sadrian	 * This increments the retry counter as well as
3968227364Sadrian	 * sets the retry flag in the ath_buf and packet
3969227364Sadrian	 * body.
3970227364Sadrian	 */
3971227364Sadrian	ath_tx_set_retry(sc, bf);
3972240639Sadrian	sc->sc_stats.ast_tx_swretries++;
3973227364Sadrian
3974227364Sadrian	/*
3975227364Sadrian	 * Insert this at the head of the queue, so it's
3976227364Sadrian	 * retried before any current/subsequent frames.
3977227364Sadrian	 */
3978241336Sadrian	ATH_TID_INSERT_HEAD(atid, bf, bf_list);
3979227364Sadrian	ath_tx_tid_sched(sc, atid);
3980233908Sadrian	/* Send the BAR if there are no other frames waiting */
3981233908Sadrian	if (ath_tx_tid_bar_tx_ready(sc, atid))
3982233908Sadrian		ath_tx_tid_bar_tx(sc, atid);
3983227364Sadrian
3984243786Sadrian	ATH_TX_UNLOCK(sc);
3985227364Sadrian}
3986227364Sadrian
3987227364Sadrian/*
3988227364Sadrian * Common code for aggregate excessive retry/subframe retry.
3989227364Sadrian * If retrying, queues buffers to bf_q. If not, frees the
3990227364Sadrian * buffers.
3991227364Sadrian *
3992227364Sadrian * XXX should unify this with ath_tx_aggr_retry_unaggr()
3993227364Sadrian */
3994227364Sadrianstatic int
3995227364Sadrianath_tx_retry_subframe(struct ath_softc *sc, struct ath_buf *bf,
3996227364Sadrian    ath_bufhead *bf_q)
3997227364Sadrian{
3998227364Sadrian	struct ieee80211_node *ni = bf->bf_node;
3999227364Sadrian	struct ath_node *an = ATH_NODE(ni);
4000227364Sadrian	int tid = bf->bf_state.bfs_tid;
4001227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
4002227364Sadrian
4003243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
4004227364Sadrian
4005240255Sadrian	/* XXX clr11naggr should be done for all subframes */
4006227364Sadrian	ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
4007227364Sadrian	ath_hal_set11nburstduration(sc->sc_ah, bf->bf_desc, 0);
4008240639Sadrian
4009227364Sadrian	/* ath_hal_set11n_virtualmorefrag(sc->sc_ah, bf->bf_desc, 0); */
4010227364Sadrian
4011227364Sadrian	/*
4012227364Sadrian	 * If the buffer is marked as busy, we can't directly
4013227364Sadrian	 * reuse it. Instead, try to clone the buffer.
4014227364Sadrian	 * If the clone is successful, recycle the old buffer.
4015227364Sadrian	 * If the clone is unsuccessful, set bfs_retries to max
4016227364Sadrian	 * to force the next bit of code to free the buffer
4017227364Sadrian	 * for us.
4018227364Sadrian	 */
4019227364Sadrian	if ((bf->bf_state.bfs_retries < SWMAX_RETRIES) &&
4020227364Sadrian	    (bf->bf_flags & ATH_BUF_BUSY)) {
4021227364Sadrian		struct ath_buf *nbf;
4022227398Sadrian		nbf = ath_tx_retry_clone(sc, an, atid, bf);
4023227364Sadrian		if (nbf)
4024227364Sadrian			/* bf has been freed at this point */
4025227364Sadrian			bf = nbf;
4026227364Sadrian		else
4027227364Sadrian			bf->bf_state.bfs_retries = SWMAX_RETRIES + 1;
4028227364Sadrian	}
4029227364Sadrian
4030227364Sadrian	if (bf->bf_state.bfs_retries >= SWMAX_RETRIES) {
4031227364Sadrian		sc->sc_stats.ast_tx_swretrymax++;
4032227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_RETRIES,
4033227364Sadrian		    "%s: max retries: seqno %d\n",
4034227364Sadrian		    __func__, SEQNO(bf->bf_state.bfs_seqno));
4035227364Sadrian		ath_tx_update_baw(sc, an, atid, bf);
4036227364Sadrian		if (! bf->bf_state.bfs_addedbaw)
4037227364Sadrian			device_printf(sc->sc_dev,
4038227364Sadrian			    "%s: wasn't added: seqno %d\n",
4039227364Sadrian			    __func__, SEQNO(bf->bf_state.bfs_seqno));
4040227364Sadrian		bf->bf_state.bfs_dobaw = 0;
4041227364Sadrian		return 1;
4042227364Sadrian	}
4043227364Sadrian
4044227364Sadrian	ath_tx_set_retry(sc, bf);
4045240639Sadrian	sc->sc_stats.ast_tx_swretries++;
4046227364Sadrian	bf->bf_next = NULL;		/* Just to make sure */
4047227364Sadrian
4048240255Sadrian	/* Clear the aggregate state */
4049240255Sadrian	bf->bf_state.bfs_aggr = 0;
4050240255Sadrian	bf->bf_state.bfs_ndelim = 0;	/* ??? needed? */
4051240255Sadrian	bf->bf_state.bfs_nframes = 1;
4052240255Sadrian
4053227364Sadrian	TAILQ_INSERT_TAIL(bf_q, bf, bf_list);
4054227364Sadrian	return 0;
4055227364Sadrian}
4056227364Sadrian
4057227364Sadrian/*
4058227364Sadrian * error pkt completion for an aggregate destination
4059227364Sadrian */
4060227364Sadrianstatic void
4061227364Sadrianath_tx_comp_aggr_error(struct ath_softc *sc, struct ath_buf *bf_first,
4062227364Sadrian    struct ath_tid *tid)
4063227364Sadrian{
4064227364Sadrian	struct ieee80211_node *ni = bf_first->bf_node;
4065227364Sadrian	struct ath_node *an = ATH_NODE(ni);
4066227364Sadrian	struct ath_buf *bf_next, *bf;
4067227364Sadrian	ath_bufhead bf_q;
4068227364Sadrian	int drops = 0;
4069227364Sadrian	struct ieee80211_tx_ampdu *tap;
4070227364Sadrian	ath_bufhead bf_cq;
4071227364Sadrian
4072227364Sadrian	TAILQ_INIT(&bf_q);
4073227364Sadrian	TAILQ_INIT(&bf_cq);
4074227364Sadrian
4075227364Sadrian	/*
4076227364Sadrian	 * Update rate control - all frames have failed.
4077227364Sadrian	 *
4078227364Sadrian	 * XXX use the length in the first frame in the series;
4079227364Sadrian	 * XXX just so things are consistent for now.
4080227364Sadrian	 */
4081227364Sadrian	ath_tx_update_ratectrl(sc, ni, bf_first->bf_state.bfs_rc,
4082227364Sadrian	    &bf_first->bf_status.ds_txstat,
4083227364Sadrian	    bf_first->bf_state.bfs_pktlen,
4084227364Sadrian	    bf_first->bf_state.bfs_nframes, bf_first->bf_state.bfs_nframes);
4085227364Sadrian
4086243786Sadrian	ATH_TX_LOCK(sc);
4087227364Sadrian	tap = ath_tx_get_tx_tid(an, tid->tid);
4088227868Sadrian	sc->sc_stats.ast_tx_aggr_failall++;
4089227364Sadrian
4090227364Sadrian	/* Retry all subframes */
4091227364Sadrian	bf = bf_first;
4092227364Sadrian	while (bf) {
4093227364Sadrian		bf_next = bf->bf_next;
4094227364Sadrian		bf->bf_next = NULL;	/* Remove it from the aggr list */
4095227868Sadrian		sc->sc_stats.ast_tx_aggr_fail++;
4096227364Sadrian		if (ath_tx_retry_subframe(sc, bf, &bf_q)) {
4097227364Sadrian			drops++;
4098227364Sadrian			bf->bf_next = NULL;
4099227364Sadrian			TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
4100227364Sadrian		}
4101227364Sadrian		bf = bf_next;
4102227364Sadrian	}
4103227364Sadrian
4104227364Sadrian	/* Prepend all frames to the beginning of the queue */
4105227364Sadrian	while ((bf = TAILQ_LAST(&bf_q, ath_bufhead_s)) != NULL) {
4106227364Sadrian		TAILQ_REMOVE(&bf_q, bf, bf_list);
4107241336Sadrian		ATH_TID_INSERT_HEAD(tid, bf, bf_list);
4108227364Sadrian	}
4109227364Sadrian
4110234725Sadrian	/*
4111234725Sadrian	 * Schedule the TID to be re-tried.
4112234725Sadrian	 */
4113227364Sadrian	ath_tx_tid_sched(sc, tid);
4114227364Sadrian
4115227364Sadrian	/*
4116227364Sadrian	 * send bar if we dropped any frames
4117227364Sadrian	 *
4118227364Sadrian	 * Keep the txq lock held for now, as we need to ensure
4119227364Sadrian	 * that ni_txseqs[] is consistent (as it's being updated
4120227364Sadrian	 * in the ifnet TX context or raw TX context.)
4121227364Sadrian	 */
4122227364Sadrian	if (drops) {
4123233908Sadrian		/* Suspend the TX queue and get ready to send the BAR */
4124233908Sadrian		ath_tx_tid_bar_suspend(sc, tid);
4125227364Sadrian	}
4126227364Sadrian
4127233908Sadrian	/*
4128233908Sadrian	 * Send BAR if required
4129233908Sadrian	 */
4130233908Sadrian	if (ath_tx_tid_bar_tx_ready(sc, tid))
4131233908Sadrian		ath_tx_tid_bar_tx(sc, tid);
4132240639Sadrian
4133243786Sadrian	ATH_TX_UNLOCK(sc);
4134233908Sadrian
4135227364Sadrian	/* Complete frames which errored out */
4136227364Sadrian	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
4137227364Sadrian		TAILQ_REMOVE(&bf_cq, bf, bf_list);
4138227364Sadrian		ath_tx_default_comp(sc, bf, 0);
4139227364Sadrian	}
4140227364Sadrian}
4141227364Sadrian
4142227364Sadrian/*
4143227364Sadrian * Handle clean-up of packets from an aggregate list.
4144227364Sadrian *
4145227364Sadrian * There's no need to update the BAW here - the session is being
4146227364Sadrian * torn down.
4147227364Sadrian */
4148227364Sadrianstatic void
4149227364Sadrianath_tx_comp_cleanup_aggr(struct ath_softc *sc, struct ath_buf *bf_first)
4150227364Sadrian{
4151227364Sadrian	struct ath_buf *bf, *bf_next;
4152227364Sadrian	struct ieee80211_node *ni = bf_first->bf_node;
4153227364Sadrian	struct ath_node *an = ATH_NODE(ni);
4154227364Sadrian	int tid = bf_first->bf_state.bfs_tid;
4155227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
4156227364Sadrian
4157243786Sadrian	ATH_TX_LOCK(sc);
4158227364Sadrian
4159227364Sadrian	/* update incomp */
4160248341Sadrian	bf = bf_first;
4161227364Sadrian	while (bf) {
4162227364Sadrian		atid->incomp--;
4163227364Sadrian		bf = bf->bf_next;
4164227364Sadrian	}
4165227364Sadrian
4166227364Sadrian	if (atid->incomp == 0) {
4167227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4168227364Sadrian		    "%s: TID %d: cleaned up! resume!\n",
4169227364Sadrian		    __func__, tid);
4170227364Sadrian		atid->cleanup_inprogress = 0;
4171227364Sadrian		ath_tx_tid_resume(sc, atid);
4172227364Sadrian	}
4173233908Sadrian
4174233908Sadrian	/* Send BAR if required */
4175240639Sadrian	/* XXX why would we send a BAR when transitioning to non-aggregation? */
4176248341Sadrian	/*
4177248341Sadrian	 * XXX TODO: we should likely just tear down the BAR state here,
4178248341Sadrian	 * rather than sending a BAR.
4179248341Sadrian	 */
4180233908Sadrian	if (ath_tx_tid_bar_tx_ready(sc, atid))
4181233908Sadrian		ath_tx_tid_bar_tx(sc, atid);
4182240639Sadrian
4183243786Sadrian	ATH_TX_UNLOCK(sc);
4184227364Sadrian
4185227364Sadrian	/* Handle frame completion */
4186248341Sadrian	bf = bf_first;
4187227364Sadrian	while (bf) {
4188227364Sadrian		bf_next = bf->bf_next;
4189227364Sadrian		ath_tx_default_comp(sc, bf, 1);
4190227364Sadrian		bf = bf_next;
4191227364Sadrian	}
4192227364Sadrian}
4193227364Sadrian
4194227364Sadrian/*
4195227364Sadrian * Handle completion of an set of aggregate frames.
4196227364Sadrian *
4197227364Sadrian * Note: the completion handler is the last descriptor in the aggregate,
4198227364Sadrian * not the last descriptor in the first frame.
4199227364Sadrian */
4200227364Sadrianstatic void
4201229949Sadrianath_tx_aggr_comp_aggr(struct ath_softc *sc, struct ath_buf *bf_first,
4202229949Sadrian    int fail)
4203227364Sadrian{
4204227364Sadrian	//struct ath_desc *ds = bf->bf_lastds;
4205227364Sadrian	struct ieee80211_node *ni = bf_first->bf_node;
4206227364Sadrian	struct ath_node *an = ATH_NODE(ni);
4207227364Sadrian	int tid = bf_first->bf_state.bfs_tid;
4208227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
4209227364Sadrian	struct ath_tx_status ts;
4210227364Sadrian	struct ieee80211_tx_ampdu *tap;
4211227364Sadrian	ath_bufhead bf_q;
4212227364Sadrian	ath_bufhead bf_cq;
4213227364Sadrian	int seq_st, tx_ok;
4214227364Sadrian	int hasba, isaggr;
4215227364Sadrian	uint32_t ba[2];
4216227364Sadrian	struct ath_buf *bf, *bf_next;
4217227364Sadrian	int ba_index;
4218227364Sadrian	int drops = 0;
4219227364Sadrian	int nframes = 0, nbad = 0, nf;
4220227364Sadrian	int pktlen;
4221227364Sadrian	/* XXX there's too much on the stack? */
4222236038Sadrian	struct ath_rc_series rc[ATH_RC_NUM];
4223227364Sadrian	int txseq;
4224227364Sadrian
4225227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: called; hwq_depth=%d\n",
4226227364Sadrian	    __func__, atid->hwq_depth);
4227227364Sadrian
4228240677Sadrian	/*
4229240677Sadrian	 * Take a copy; this may be needed -after- bf_first
4230240677Sadrian	 * has been completed and freed.
4231240677Sadrian	 */
4232240677Sadrian	ts = bf_first->bf_status.ds_txstat;
4233240677Sadrian
4234240639Sadrian	TAILQ_INIT(&bf_q);
4235240639Sadrian	TAILQ_INIT(&bf_cq);
4236240639Sadrian
4237227364Sadrian	/* The TID state is kept behind the TXQ lock */
4238243786Sadrian	ATH_TX_LOCK(sc);
4239227364Sadrian
4240227364Sadrian	atid->hwq_depth--;
4241227364Sadrian	if (atid->hwq_depth < 0)
4242227364Sadrian		device_printf(sc->sc_dev, "%s: hwq_depth < 0: %d\n",
4243227364Sadrian		    __func__, atid->hwq_depth);
4244227364Sadrian
4245227364Sadrian	/*
4246240639Sadrian	 * If the TID is filtered, handle completing the filter
4247240639Sadrian	 * transition before potentially kicking it to the cleanup
4248240639Sadrian	 * function.
4249240677Sadrian	 *
4250240677Sadrian	 * XXX this is duplicate work, ew.
4251240639Sadrian	 */
4252240639Sadrian	if (atid->isfiltered)
4253240639Sadrian		ath_tx_tid_filt_comp_complete(sc, atid);
4254240639Sadrian
4255240639Sadrian	/*
4256227364Sadrian	 * Punt cleanup to the relevant function, not our problem now
4257227364Sadrian	 */
4258227364Sadrian	if (atid->cleanup_inprogress) {
4259240639Sadrian		if (atid->isfiltered)
4260240639Sadrian			device_printf(sc->sc_dev,
4261240639Sadrian			    "%s: isfiltered=1, normal_comp?\n",
4262240639Sadrian			    __func__);
4263243786Sadrian		ATH_TX_UNLOCK(sc);
4264227364Sadrian		ath_tx_comp_cleanup_aggr(sc, bf_first);
4265227364Sadrian		return;
4266227364Sadrian	}
4267227364Sadrian
4268227364Sadrian	/*
4269240639Sadrian	 * If the frame is filtered, transition to filtered frame
4270240639Sadrian	 * mode and add this to the filtered frame list.
4271240639Sadrian	 *
4272240639Sadrian	 * XXX TODO: figure out how this interoperates with
4273240639Sadrian	 * BAR, pause and cleanup states.
4274240639Sadrian	 */
4275240639Sadrian	if ((ts.ts_status & HAL_TXERR_FILT) ||
4276240639Sadrian	    (ts.ts_status != 0 && atid->isfiltered)) {
4277240639Sadrian		if (fail != 0)
4278240639Sadrian			device_printf(sc->sc_dev,
4279240639Sadrian			    "%s: isfiltered=1, fail=%d\n", __func__, fail);
4280240639Sadrian		ath_tx_tid_filt_comp_aggr(sc, atid, bf_first, &bf_cq);
4281240639Sadrian
4282240639Sadrian		/* Remove from BAW */
4283240639Sadrian		TAILQ_FOREACH_SAFE(bf, &bf_cq, bf_list, bf_next) {
4284240639Sadrian			if (bf->bf_state.bfs_addedbaw)
4285240639Sadrian				drops++;
4286240639Sadrian			if (bf->bf_state.bfs_dobaw) {
4287240639Sadrian				ath_tx_update_baw(sc, an, atid, bf);
4288240639Sadrian				if (! bf->bf_state.bfs_addedbaw)
4289240639Sadrian					device_printf(sc->sc_dev,
4290240639Sadrian					    "%s: wasn't added: seqno %d\n",
4291240639Sadrian					    __func__,
4292240639Sadrian					    SEQNO(bf->bf_state.bfs_seqno));
4293240639Sadrian			}
4294240639Sadrian			bf->bf_state.bfs_dobaw = 0;
4295240639Sadrian		}
4296240639Sadrian		/*
4297240639Sadrian		 * If any intermediate frames in the BAW were dropped when
4298240639Sadrian		 * handling filtering things, send a BAR.
4299240639Sadrian		 */
4300240639Sadrian		if (drops)
4301240639Sadrian			ath_tx_tid_bar_suspend(sc, atid);
4302240639Sadrian
4303240639Sadrian		/*
4304240639Sadrian		 * Finish up by sending a BAR if required and freeing
4305240639Sadrian		 * the frames outside of the TX lock.
4306240639Sadrian		 */
4307240639Sadrian		goto finish_send_bar;
4308240639Sadrian	}
4309240639Sadrian
4310240639Sadrian	/*
4311227364Sadrian	 * XXX for now, use the first frame in the aggregate for
4312227364Sadrian	 * XXX rate control completion; it's at least consistent.
4313227364Sadrian	 */
4314227364Sadrian	pktlen = bf_first->bf_state.bfs_pktlen;
4315227364Sadrian
4316227364Sadrian	/*
4317235461Sadrian	 * Handle errors first!
4318235461Sadrian	 *
4319235461Sadrian	 * Here, handle _any_ error as a "exceeded retries" error.
4320235461Sadrian	 * Later on (when filtered frames are to be specially handled)
4321235461Sadrian	 * it'll have to be expanded.
4322227364Sadrian	 */
4323235461Sadrian#if 0
4324227364Sadrian	if (ts.ts_status & HAL_TXERR_XRETRY) {
4325235461Sadrian#endif
4326235461Sadrian	if (ts.ts_status != 0) {
4327243786Sadrian		ATH_TX_UNLOCK(sc);
4328227364Sadrian		ath_tx_comp_aggr_error(sc, bf_first, atid);
4329227364Sadrian		return;
4330227364Sadrian	}
4331227364Sadrian
4332227364Sadrian	tap = ath_tx_get_tx_tid(an, tid);
4333227364Sadrian
4334227364Sadrian	/*
4335227364Sadrian	 * extract starting sequence and block-ack bitmap
4336227364Sadrian	 */
4337227364Sadrian	/* XXX endian-ness of seq_st, ba? */
4338227364Sadrian	seq_st = ts.ts_seqnum;
4339227364Sadrian	hasba = !! (ts.ts_flags & HAL_TX_BA);
4340227364Sadrian	tx_ok = (ts.ts_status == 0);
4341227364Sadrian	isaggr = bf_first->bf_state.bfs_aggr;
4342227364Sadrian	ba[0] = ts.ts_ba_low;
4343227364Sadrian	ba[1] = ts.ts_ba_high;
4344227364Sadrian
4345227364Sadrian	/*
4346227364Sadrian	 * Copy the TX completion status and the rate control
4347227364Sadrian	 * series from the first descriptor, as it may be freed
4348227364Sadrian	 * before the rate control code can get its grubby fingers
4349227364Sadrian	 * into things.
4350227364Sadrian	 */
4351227364Sadrian	memcpy(rc, bf_first->bf_state.bfs_rc, sizeof(rc));
4352227364Sadrian
4353227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4354229949Sadrian	    "%s: txa_start=%d, tx_ok=%d, status=%.8x, flags=%.8x, "
4355229949Sadrian	    "isaggr=%d, seq_st=%d, hasba=%d, ba=%.8x, %.8x\n",
4356227364Sadrian	    __func__, tap->txa_start, tx_ok, ts.ts_status, ts.ts_flags,
4357227364Sadrian	    isaggr, seq_st, hasba, ba[0], ba[1]);
4358227364Sadrian
4359248091Sadrian	/*
4360248091Sadrian	 * The reference driver doesn't do this; it simply ignores
4361248091Sadrian	 * this check in its entirety.
4362248091Sadrian	 *
4363248091Sadrian	 * I've seen this occur when using iperf to send traffic
4364248091Sadrian	 * out tid 1 - the aggregate frames are all marked as TID 1,
4365248091Sadrian	 * but the TXSTATUS has TID=0.  So, let's just ignore this
4366248091Sadrian	 * check.
4367248091Sadrian	 */
4368248091Sadrian#if 0
4369227364Sadrian	/* Occasionally, the MAC sends a tx status for the wrong TID. */
4370227364Sadrian	if (tid != ts.ts_tid) {
4371227364Sadrian		device_printf(sc->sc_dev, "%s: tid %d != hw tid %d\n",
4372227364Sadrian		    __func__, tid, ts.ts_tid);
4373227364Sadrian		tx_ok = 0;
4374227364Sadrian	}
4375248091Sadrian#endif
4376227364Sadrian
4377227364Sadrian	/* AR5416 BA bug; this requires an interface reset */
4378227364Sadrian	if (isaggr && tx_ok && (! hasba)) {
4379227364Sadrian		device_printf(sc->sc_dev,
4380229949Sadrian		    "%s: AR5416 bug: hasba=%d; txok=%d, isaggr=%d, "
4381229949Sadrian		    "seq_st=%d\n",
4382227364Sadrian		    __func__, hasba, tx_ok, isaggr, seq_st);
4383227364Sadrian		/* XXX TODO: schedule an interface reset */
4384238350Sjhb#ifdef ATH_DEBUG
4385238338Sadrian		ath_printtxbuf(sc, bf_first,
4386238338Sadrian		    sc->sc_ac2q[atid->ac]->axq_qnum, 0, 0);
4387238350Sjhb#endif
4388227364Sadrian	}
4389227364Sadrian
4390227364Sadrian	/*
4391227364Sadrian	 * Walk the list of frames, figure out which ones were correctly
4392227364Sadrian	 * sent and which weren't.
4393227364Sadrian	 */
4394227364Sadrian	bf = bf_first;
4395227364Sadrian	nf = bf_first->bf_state.bfs_nframes;
4396227364Sadrian
4397227364Sadrian	/* bf_first is going to be invalid once this list is walked */
4398227364Sadrian	bf_first = NULL;
4399227364Sadrian
4400227364Sadrian	/*
4401227364Sadrian	 * Walk the list of completed frames and determine
4402227364Sadrian	 * which need to be completed and which need to be
4403227364Sadrian	 * retransmitted.
4404227364Sadrian	 *
4405227364Sadrian	 * For completed frames, the completion functions need
4406227364Sadrian	 * to be called at the end of this function as the last
4407227364Sadrian	 * node reference may free the node.
4408227364Sadrian	 *
4409227364Sadrian	 * Finally, since the TXQ lock can't be held during the
4410227364Sadrian	 * completion callback (to avoid lock recursion),
4411227364Sadrian	 * the completion calls have to be done outside of the
4412227364Sadrian	 * lock.
4413227364Sadrian	 */
4414227364Sadrian	while (bf) {
4415227364Sadrian		nframes++;
4416229949Sadrian		ba_index = ATH_BA_INDEX(seq_st,
4417229949Sadrian		    SEQNO(bf->bf_state.bfs_seqno));
4418227364Sadrian		bf_next = bf->bf_next;
4419227364Sadrian		bf->bf_next = NULL;	/* Remove it from the aggr list */
4420227364Sadrian
4421227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4422227364Sadrian		    "%s: checking bf=%p seqno=%d; ack=%d\n",
4423227364Sadrian		    __func__, bf, SEQNO(bf->bf_state.bfs_seqno),
4424227364Sadrian		    ATH_BA_ISSET(ba, ba_index));
4425227364Sadrian
4426227364Sadrian		if (tx_ok && ATH_BA_ISSET(ba, ba_index)) {
4427227868Sadrian			sc->sc_stats.ast_tx_aggr_ok++;
4428227364Sadrian			ath_tx_update_baw(sc, an, atid, bf);
4429227364Sadrian			bf->bf_state.bfs_dobaw = 0;
4430227364Sadrian			if (! bf->bf_state.bfs_addedbaw)
4431227364Sadrian				device_printf(sc->sc_dev,
4432227364Sadrian				    "%s: wasn't added: seqno %d\n",
4433227364Sadrian				    __func__, SEQNO(bf->bf_state.bfs_seqno));
4434227364Sadrian			bf->bf_next = NULL;
4435227364Sadrian			TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
4436227364Sadrian		} else {
4437227868Sadrian			sc->sc_stats.ast_tx_aggr_fail++;
4438227364Sadrian			if (ath_tx_retry_subframe(sc, bf, &bf_q)) {
4439227364Sadrian				drops++;
4440227364Sadrian				bf->bf_next = NULL;
4441227364Sadrian				TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
4442227364Sadrian			}
4443227364Sadrian			nbad++;
4444227364Sadrian		}
4445227364Sadrian		bf = bf_next;
4446227364Sadrian	}
4447227364Sadrian
4448227364Sadrian	/*
4449227364Sadrian	 * Now that the BAW updates have been done, unlock
4450227364Sadrian	 *
4451227364Sadrian	 * txseq is grabbed before the lock is released so we
4452227364Sadrian	 * have a consistent view of what -was- in the BAW.
4453227364Sadrian	 * Anything after this point will not yet have been
4454227364Sadrian	 * TXed.
4455227364Sadrian	 */
4456227364Sadrian	txseq = tap->txa_start;
4457243786Sadrian	ATH_TX_UNLOCK(sc);
4458227364Sadrian
4459227364Sadrian	if (nframes != nf)
4460227364Sadrian		device_printf(sc->sc_dev,
4461227364Sadrian		    "%s: num frames seen=%d; bf nframes=%d\n",
4462227364Sadrian		    __func__, nframes, nf);
4463227364Sadrian
4464227364Sadrian	/*
4465227364Sadrian	 * Now we know how many frames were bad, call the rate
4466227364Sadrian	 * control code.
4467227364Sadrian	 */
4468227364Sadrian	if (fail == 0)
4469229949Sadrian		ath_tx_update_ratectrl(sc, ni, rc, &ts, pktlen, nframes,
4470229949Sadrian		    nbad);
4471227364Sadrian
4472227364Sadrian	/*
4473227364Sadrian	 * send bar if we dropped any frames
4474227364Sadrian	 */
4475227364Sadrian	if (drops) {
4476233908Sadrian		/* Suspend the TX queue and get ready to send the BAR */
4477243786Sadrian		ATH_TX_LOCK(sc);
4478233908Sadrian		ath_tx_tid_bar_suspend(sc, atid);
4479243786Sadrian		ATH_TX_UNLOCK(sc);
4480227364Sadrian	}
4481227364Sadrian
4482234725Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4483234725Sadrian	    "%s: txa_start now %d\n", __func__, tap->txa_start);
4484234725Sadrian
4485243786Sadrian	ATH_TX_LOCK(sc);
4486234725Sadrian
4487227364Sadrian	/* Prepend all frames to the beginning of the queue */
4488227364Sadrian	while ((bf = TAILQ_LAST(&bf_q, ath_bufhead_s)) != NULL) {
4489227364Sadrian		TAILQ_REMOVE(&bf_q, bf, bf_list);
4490241336Sadrian		ATH_TID_INSERT_HEAD(atid, bf, bf_list);
4491227364Sadrian	}
4492234725Sadrian
4493234725Sadrian	/*
4494234725Sadrian	 * Reschedule to grab some further frames.
4495234725Sadrian	 */
4496227364Sadrian	ath_tx_tid_sched(sc, atid);
4497227364Sadrian
4498233908Sadrian	/*
4499240639Sadrian	 * If the queue is filtered, re-schedule as required.
4500240639Sadrian	 *
4501240639Sadrian	 * This is required as there may be a subsequent TX descriptor
4502240639Sadrian	 * for this end-node that has CLRDMASK set, so it's quite possible
4503240639Sadrian	 * that a filtered frame will be followed by a non-filtered
4504240639Sadrian	 * (complete or otherwise) frame.
4505240639Sadrian	 *
4506240639Sadrian	 * XXX should we do this before we complete the frame?
4507240639Sadrian	 */
4508240639Sadrian	if (atid->isfiltered)
4509240639Sadrian		ath_tx_tid_filt_comp_complete(sc, atid);
4510240639Sadrian
4511240639Sadrianfinish_send_bar:
4512240639Sadrian
4513240639Sadrian	/*
4514233908Sadrian	 * Send BAR if required
4515233908Sadrian	 */
4516233908Sadrian	if (ath_tx_tid_bar_tx_ready(sc, atid))
4517233908Sadrian		ath_tx_tid_bar_tx(sc, atid);
4518234725Sadrian
4519243786Sadrian	ATH_TX_UNLOCK(sc);
4520233908Sadrian
4521227364Sadrian	/* Do deferred completion */
4522227364Sadrian	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
4523227364Sadrian		TAILQ_REMOVE(&bf_cq, bf, bf_list);
4524227364Sadrian		ath_tx_default_comp(sc, bf, 0);
4525227364Sadrian	}
4526227364Sadrian}
4527227364Sadrian
4528227364Sadrian/*
4529227364Sadrian * Handle completion of unaggregated frames in an ADDBA
4530227364Sadrian * session.
4531227364Sadrian *
4532227364Sadrian * Fail is set to 1 if the entry is being freed via a call to
4533227364Sadrian * ath_tx_draintxq().
4534227364Sadrian */
4535227364Sadrianstatic void
4536227364Sadrianath_tx_aggr_comp_unaggr(struct ath_softc *sc, struct ath_buf *bf, int fail)
4537227364Sadrian{
4538227364Sadrian	struct ieee80211_node *ni = bf->bf_node;
4539227364Sadrian	struct ath_node *an = ATH_NODE(ni);
4540227364Sadrian	int tid = bf->bf_state.bfs_tid;
4541227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
4542240677Sadrian	struct ath_tx_status ts;
4543240639Sadrian	int drops = 0;
4544227364Sadrian
4545227364Sadrian	/*
4546240677Sadrian	 * Take a copy of this; filtering/cloning the frame may free the
4547240677Sadrian	 * bf pointer.
4548240677Sadrian	 */
4549240677Sadrian	ts = bf->bf_status.ds_txstat;
4550240677Sadrian
4551240677Sadrian	/*
4552227364Sadrian	 * Update rate control status here, before we possibly
4553227364Sadrian	 * punt to retry or cleanup.
4554227364Sadrian	 *
4555227364Sadrian	 * Do it outside of the TXQ lock.
4556227364Sadrian	 */
4557233966Sadrian	if (fail == 0 && ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0))
4558227364Sadrian		ath_tx_update_ratectrl(sc, ni, bf->bf_state.bfs_rc,
4559227364Sadrian		    &bf->bf_status.ds_txstat,
4560227364Sadrian		    bf->bf_state.bfs_pktlen,
4561240677Sadrian		    1, (ts.ts_status == 0) ? 0 : 1);
4562227364Sadrian
4563227364Sadrian	/*
4564227364Sadrian	 * This is called early so atid->hwq_depth can be tracked.
4565227364Sadrian	 * This unfortunately means that it's released and regrabbed
4566227364Sadrian	 * during retry and cleanup. That's rather inefficient.
4567227364Sadrian	 */
4568243786Sadrian	ATH_TX_LOCK(sc);
4569227364Sadrian
4570227364Sadrian	if (tid == IEEE80211_NONQOS_TID)
4571227364Sadrian		device_printf(sc->sc_dev, "%s: TID=16!\n", __func__);
4572227364Sadrian
4573229949Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX,
4574229949Sadrian	    "%s: bf=%p: tid=%d, hwq_depth=%d, seqno=%d\n",
4575229949Sadrian	    __func__, bf, bf->bf_state.bfs_tid, atid->hwq_depth,
4576229949Sadrian	    SEQNO(bf->bf_state.bfs_seqno));
4577227364Sadrian
4578227364Sadrian	atid->hwq_depth--;
4579227364Sadrian	if (atid->hwq_depth < 0)
4580227364Sadrian		device_printf(sc->sc_dev, "%s: hwq_depth < 0: %d\n",
4581227364Sadrian		    __func__, atid->hwq_depth);
4582227364Sadrian
4583227364Sadrian	/*
4584240639Sadrian	 * If the TID is filtered, handle completing the filter
4585240639Sadrian	 * transition before potentially kicking it to the cleanup
4586240639Sadrian	 * function.
4587240639Sadrian	 */
4588240639Sadrian	if (atid->isfiltered)
4589240639Sadrian		ath_tx_tid_filt_comp_complete(sc, atid);
4590240639Sadrian
4591240639Sadrian	/*
4592227364Sadrian	 * If a cleanup is in progress, punt to comp_cleanup;
4593227364Sadrian	 * rather than handling it here. It's thus their
4594227364Sadrian	 * responsibility to clean up, call the completion
4595227364Sadrian	 * function in net80211, etc.
4596227364Sadrian	 */
4597227364Sadrian	if (atid->cleanup_inprogress) {
4598240639Sadrian		if (atid->isfiltered)
4599240639Sadrian			device_printf(sc->sc_dev,
4600240639Sadrian			    "%s: isfiltered=1, normal_comp?\n",
4601240639Sadrian			    __func__);
4602243786Sadrian		ATH_TX_UNLOCK(sc);
4603229949Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: cleanup_unaggr\n",
4604229949Sadrian		    __func__);
4605227364Sadrian		ath_tx_comp_cleanup_unaggr(sc, bf);
4606227364Sadrian		return;
4607227364Sadrian	}
4608227364Sadrian
4609227364Sadrian	/*
4610240639Sadrian	 * XXX TODO: how does cleanup, BAR and filtered frame handling
4611240639Sadrian	 * overlap?
4612240639Sadrian	 *
4613240639Sadrian	 * If the frame is filtered OR if it's any failure but
4614240639Sadrian	 * the TID is filtered, the frame must be added to the
4615240639Sadrian	 * filtered frame list.
4616240639Sadrian	 *
4617240639Sadrian	 * However - a busy buffer can't be added to the filtered
4618240639Sadrian	 * list as it will end up being recycled without having
4619240639Sadrian	 * been made available for the hardware.
4620240639Sadrian	 */
4621240677Sadrian	if ((ts.ts_status & HAL_TXERR_FILT) ||
4622240677Sadrian	    (ts.ts_status != 0 && atid->isfiltered)) {
4623240639Sadrian		int freeframe;
4624240639Sadrian
4625240639Sadrian		if (fail != 0)
4626240639Sadrian			device_printf(sc->sc_dev,
4627240639Sadrian			    "%s: isfiltered=1, fail=%d\n",
4628240639Sadrian			    __func__,
4629240639Sadrian			    fail);
4630240639Sadrian		freeframe = ath_tx_tid_filt_comp_single(sc, atid, bf);
4631240639Sadrian		if (freeframe) {
4632240639Sadrian			/* Remove from BAW */
4633240639Sadrian			if (bf->bf_state.bfs_addedbaw)
4634240639Sadrian				drops++;
4635240639Sadrian			if (bf->bf_state.bfs_dobaw) {
4636240639Sadrian				ath_tx_update_baw(sc, an, atid, bf);
4637240639Sadrian				if (! bf->bf_state.bfs_addedbaw)
4638240639Sadrian					device_printf(sc->sc_dev,
4639240639Sadrian					    "%s: wasn't added: seqno %d\n",
4640240639Sadrian					    __func__, SEQNO(bf->bf_state.bfs_seqno));
4641240639Sadrian			}
4642240639Sadrian			bf->bf_state.bfs_dobaw = 0;
4643240639Sadrian		}
4644240639Sadrian
4645240639Sadrian		/*
4646240639Sadrian		 * If the frame couldn't be filtered, treat it as a drop and
4647240639Sadrian		 * prepare to send a BAR.
4648240639Sadrian		 */
4649240639Sadrian		if (freeframe && drops)
4650240639Sadrian			ath_tx_tid_bar_suspend(sc, atid);
4651240639Sadrian
4652240639Sadrian		/*
4653240639Sadrian		 * Send BAR if required
4654240639Sadrian		 */
4655240639Sadrian		if (ath_tx_tid_bar_tx_ready(sc, atid))
4656240639Sadrian			ath_tx_tid_bar_tx(sc, atid);
4657240639Sadrian
4658243786Sadrian		ATH_TX_UNLOCK(sc);
4659240639Sadrian		/*
4660240639Sadrian		 * If freeframe is set, then the frame couldn't be
4661240639Sadrian		 * cloned and bf is still valid.  Just complete/free it.
4662240639Sadrian		 */
4663240639Sadrian		if (freeframe)
4664240639Sadrian			ath_tx_default_comp(sc, bf, fail);
4665240639Sadrian
4666240639Sadrian
4667240639Sadrian		return;
4668240639Sadrian	}
4669240639Sadrian	/*
4670227364Sadrian	 * Don't bother with the retry check if all frames
4671227364Sadrian	 * are being failed (eg during queue deletion.)
4672227364Sadrian	 */
4673235461Sadrian#if 0
4674227364Sadrian	if (fail == 0 && ts->ts_status & HAL_TXERR_XRETRY) {
4675235461Sadrian#endif
4676240677Sadrian	if (fail == 0 && ts.ts_status != 0) {
4677243786Sadrian		ATH_TX_UNLOCK(sc);
4678229949Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: retry_unaggr\n",
4679229949Sadrian		    __func__);
4680227364Sadrian		ath_tx_aggr_retry_unaggr(sc, bf);
4681227364Sadrian		return;
4682227364Sadrian	}
4683227364Sadrian
4684227364Sadrian	/* Success? Complete */
4685227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: TID=%d, seqno %d\n",
4686227364Sadrian	    __func__, tid, SEQNO(bf->bf_state.bfs_seqno));
4687227364Sadrian	if (bf->bf_state.bfs_dobaw) {
4688227364Sadrian		ath_tx_update_baw(sc, an, atid, bf);
4689227364Sadrian		bf->bf_state.bfs_dobaw = 0;
4690227364Sadrian		if (! bf->bf_state.bfs_addedbaw)
4691227364Sadrian			device_printf(sc->sc_dev,
4692227364Sadrian			    "%s: wasn't added: seqno %d\n",
4693227364Sadrian			    __func__, SEQNO(bf->bf_state.bfs_seqno));
4694227364Sadrian	}
4695227364Sadrian
4696233908Sadrian	/*
4697240639Sadrian	 * If the queue is filtered, re-schedule as required.
4698240639Sadrian	 *
4699240639Sadrian	 * This is required as there may be a subsequent TX descriptor
4700240639Sadrian	 * for this end-node that has CLRDMASK set, so it's quite possible
4701240639Sadrian	 * that a filtered frame will be followed by a non-filtered
4702240639Sadrian	 * (complete or otherwise) frame.
4703240639Sadrian	 *
4704240639Sadrian	 * XXX should we do this before we complete the frame?
4705240639Sadrian	 */
4706240639Sadrian	if (atid->isfiltered)
4707240639Sadrian		ath_tx_tid_filt_comp_complete(sc, atid);
4708240639Sadrian
4709240639Sadrian	/*
4710233908Sadrian	 * Send BAR if required
4711233908Sadrian	 */
4712233908Sadrian	if (ath_tx_tid_bar_tx_ready(sc, atid))
4713233908Sadrian		ath_tx_tid_bar_tx(sc, atid);
4714233908Sadrian
4715243786Sadrian	ATH_TX_UNLOCK(sc);
4716227364Sadrian
4717227364Sadrian	ath_tx_default_comp(sc, bf, fail);
4718227364Sadrian	/* bf is freed at this point */
4719227364Sadrian}
4720227364Sadrian
4721227364Sadrianvoid
4722227364Sadrianath_tx_aggr_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
4723227364Sadrian{
4724227364Sadrian	if (bf->bf_state.bfs_aggr)
4725227364Sadrian		ath_tx_aggr_comp_aggr(sc, bf, fail);
4726227364Sadrian	else
4727227364Sadrian		ath_tx_aggr_comp_unaggr(sc, bf, fail);
4728227364Sadrian}
4729227364Sadrian
4730227364Sadrian/*
4731227364Sadrian * Schedule some packets from the given node/TID to the hardware.
4732227364Sadrian *
4733227364Sadrian * This is the aggregate version.
4734227364Sadrian */
4735227364Sadrianvoid
4736227364Sadrianath_tx_tid_hw_queue_aggr(struct ath_softc *sc, struct ath_node *an,
4737227364Sadrian    struct ath_tid *tid)
4738227364Sadrian{
4739227364Sadrian	struct ath_buf *bf;
4740227364Sadrian	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
4741227364Sadrian	struct ieee80211_tx_ampdu *tap;
4742227364Sadrian	ATH_AGGR_STATUS status;
4743227364Sadrian	ath_bufhead bf_q;
4744227364Sadrian
4745227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d\n", __func__, tid->tid);
4746243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
4747227364Sadrian
4748227364Sadrian	tap = ath_tx_get_tx_tid(an, tid->tid);
4749227364Sadrian
4750227364Sadrian	if (tid->tid == IEEE80211_NONQOS_TID)
4751227364Sadrian		device_printf(sc->sc_dev, "%s: called for TID=NONQOS_TID?\n",
4752227364Sadrian		    __func__);
4753227364Sadrian
4754227364Sadrian	for (;;) {
4755227364Sadrian		status = ATH_AGGR_DONE;
4756227364Sadrian
4757227364Sadrian		/*
4758227364Sadrian		 * If the upper layer has paused the TID, don't
4759227364Sadrian		 * queue any further packets.
4760227364Sadrian		 *
4761227364Sadrian		 * This can also occur from the completion task because
4762227364Sadrian		 * of packet loss; but as its serialised with this code,
4763227364Sadrian		 * it won't "appear" half way through queuing packets.
4764227364Sadrian		 */
4765227364Sadrian		if (tid->paused)
4766227364Sadrian			break;
4767227364Sadrian
4768241336Sadrian		bf = ATH_TID_FIRST(tid);
4769227364Sadrian		if (bf == NULL) {
4770227364Sadrian			break;
4771227364Sadrian		}
4772227364Sadrian
4773227364Sadrian		/*
4774227364Sadrian		 * If the packet doesn't fall within the BAW (eg a NULL
4775227364Sadrian		 * data frame), schedule it directly; continue.
4776227364Sadrian		 */
4777227364Sadrian		if (! bf->bf_state.bfs_dobaw) {
4778229949Sadrian			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4779229949Sadrian			    "%s: non-baw packet\n",
4780227364Sadrian			    __func__);
4781241336Sadrian			ATH_TID_REMOVE(tid, bf, bf_list);
4782240180Sadrian
4783240180Sadrian			if (bf->bf_state.bfs_nframes > 1)
4784240180Sadrian				device_printf(sc->sc_dev,
4785240180Sadrian				    "%s: aggr=%d, nframes=%d\n",
4786240180Sadrian				    __func__,
4787240180Sadrian				    bf->bf_state.bfs_aggr,
4788240180Sadrian				    bf->bf_state.bfs_nframes);
4789240180Sadrian
4790240180Sadrian			/*
4791240180Sadrian			 * This shouldn't happen - such frames shouldn't
4792240180Sadrian			 * ever have been queued as an aggregate in the
4793240180Sadrian			 * first place.  However, make sure the fields
4794240180Sadrian			 * are correctly setup just to be totally sure.
4795240180Sadrian			 */
4796227364Sadrian			bf->bf_state.bfs_aggr = 0;
4797240180Sadrian			bf->bf_state.bfs_nframes = 1;
4798240180Sadrian
4799240724Sadrian			/* Update CLRDMASK just before this frame is queued */
4800240724Sadrian			ath_tx_update_clrdmask(sc, tid, bf);
4801240724Sadrian
4802227364Sadrian			ath_tx_do_ratelookup(sc, bf);
4803233989Sadrian			ath_tx_calc_duration(sc, bf);
4804233989Sadrian			ath_tx_calc_protection(sc, bf);
4805233989Sadrian			ath_tx_set_rtscts(sc, bf);
4806227364Sadrian			ath_tx_rate_fill_rcflags(sc, bf);
4807227364Sadrian			ath_tx_setds(sc, bf);
4808227364Sadrian			ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
4809227364Sadrian
4810227364Sadrian			sc->sc_aggr_stats.aggr_nonbaw_pkt++;
4811227364Sadrian
4812227364Sadrian			/* Queue the packet; continue */
4813227364Sadrian			goto queuepkt;
4814227364Sadrian		}
4815227364Sadrian
4816227364Sadrian		TAILQ_INIT(&bf_q);
4817227364Sadrian
4818227364Sadrian		/*
4819227364Sadrian		 * Do a rate control lookup on the first frame in the
4820227364Sadrian		 * list. The rate control code needs that to occur
4821227364Sadrian		 * before it can determine whether to TX.
4822227364Sadrian		 * It's inaccurate because the rate control code doesn't
4823227364Sadrian		 * really "do" aggregate lookups, so it only considers
4824227364Sadrian		 * the size of the first frame.
4825227364Sadrian		 */
4826227364Sadrian		ath_tx_do_ratelookup(sc, bf);
4827227364Sadrian		bf->bf_state.bfs_rc[3].rix = 0;
4828227364Sadrian		bf->bf_state.bfs_rc[3].tries = 0;
4829233989Sadrian
4830233989Sadrian		ath_tx_calc_duration(sc, bf);
4831233989Sadrian		ath_tx_calc_protection(sc, bf);
4832233989Sadrian
4833233989Sadrian		ath_tx_set_rtscts(sc, bf);
4834227364Sadrian		ath_tx_rate_fill_rcflags(sc, bf);
4835227364Sadrian
4836227364Sadrian		status = ath_tx_form_aggr(sc, an, tid, &bf_q);
4837227364Sadrian
4838227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4839227364Sadrian		    "%s: ath_tx_form_aggr() status=%d\n", __func__, status);
4840227364Sadrian
4841227364Sadrian		/*
4842227364Sadrian		 * No frames to be picked up - out of BAW
4843227364Sadrian		 */
4844227364Sadrian		if (TAILQ_EMPTY(&bf_q))
4845227364Sadrian			break;
4846227364Sadrian
4847227364Sadrian		/*
4848227364Sadrian		 * This assumes that the descriptor list in the ath_bufhead
4849227364Sadrian		 * are already linked together via bf_next pointers.
4850227364Sadrian		 */
4851227364Sadrian		bf = TAILQ_FIRST(&bf_q);
4852227364Sadrian
4853233989Sadrian		if (status == ATH_AGGR_8K_LIMITED)
4854233989Sadrian			sc->sc_aggr_stats.aggr_rts_aggr_limited++;
4855233989Sadrian
4856227364Sadrian		/*
4857227364Sadrian		 * If it's the only frame send as non-aggregate
4858227364Sadrian		 * assume that ath_tx_form_aggr() has checked
4859227364Sadrian		 * whether it's in the BAW and added it appropriately.
4860227364Sadrian		 */
4861227364Sadrian		if (bf->bf_state.bfs_nframes == 1) {
4862227364Sadrian			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4863227364Sadrian			    "%s: single-frame aggregate\n", __func__);
4864240724Sadrian
4865240724Sadrian			/* Update CLRDMASK just before this frame is queued */
4866240724Sadrian			ath_tx_update_clrdmask(sc, tid, bf);
4867240724Sadrian
4868227364Sadrian			bf->bf_state.bfs_aggr = 0;
4869240255Sadrian			bf->bf_state.bfs_ndelim = 0;
4870227364Sadrian			ath_tx_setds(sc, bf);
4871227364Sadrian			ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
4872227364Sadrian			if (status == ATH_AGGR_BAW_CLOSED)
4873227364Sadrian				sc->sc_aggr_stats.aggr_baw_closed_single_pkt++;
4874227364Sadrian			else
4875227364Sadrian				sc->sc_aggr_stats.aggr_single_pkt++;
4876227364Sadrian		} else {
4877227364Sadrian			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4878229949Sadrian			    "%s: multi-frame aggregate: %d frames, "
4879229949Sadrian			    "length %d\n",
4880227364Sadrian			     __func__, bf->bf_state.bfs_nframes,
4881227364Sadrian			    bf->bf_state.bfs_al);
4882227364Sadrian			bf->bf_state.bfs_aggr = 1;
4883227364Sadrian			sc->sc_aggr_stats.aggr_pkts[bf->bf_state.bfs_nframes]++;
4884227364Sadrian			sc->sc_aggr_stats.aggr_aggr_pkt++;
4885227364Sadrian
4886240724Sadrian			/* Update CLRDMASK just before this frame is queued */
4887240724Sadrian			ath_tx_update_clrdmask(sc, tid, bf);
4888240724Sadrian
4889227364Sadrian			/*
4890233989Sadrian			 * Calculate the duration/protection as required.
4891233989Sadrian			 */
4892233989Sadrian			ath_tx_calc_duration(sc, bf);
4893233989Sadrian			ath_tx_calc_protection(sc, bf);
4894233989Sadrian
4895233989Sadrian			/*
4896227364Sadrian			 * Update the rate and rtscts information based on the
4897227364Sadrian			 * rate decision made by the rate control code;
4898227364Sadrian			 * the first frame in the aggregate needs it.
4899227364Sadrian			 */
4900227364Sadrian			ath_tx_set_rtscts(sc, bf);
4901227364Sadrian
4902227364Sadrian			/*
4903227364Sadrian			 * Setup the relevant descriptor fields
4904227364Sadrian			 * for aggregation. The first descriptor
4905227364Sadrian			 * already points to the rest in the chain.
4906227364Sadrian			 */
4907227364Sadrian			ath_tx_setds_11n(sc, bf);
4908227364Sadrian
4909227364Sadrian		}
4910227364Sadrian	queuepkt:
4911227364Sadrian		/* Set completion handler, multi-frame aggregate or not */
4912227364Sadrian		bf->bf_comp = ath_tx_aggr_comp;
4913227364Sadrian
4914227364Sadrian		if (bf->bf_state.bfs_tid == IEEE80211_NONQOS_TID)
4915227364Sadrian		    device_printf(sc->sc_dev, "%s: TID=16?\n", __func__);
4916227364Sadrian
4917227364Sadrian		/* Punt to txq */
4918227364Sadrian		ath_tx_handoff(sc, txq, bf);
4919227364Sadrian
4920227364Sadrian		/* Track outstanding buffer count to hardware */
4921227364Sadrian		/* aggregates are "one" buffer */
4922227364Sadrian		tid->hwq_depth++;
4923227364Sadrian
4924227364Sadrian		/*
4925227364Sadrian		 * Break out if ath_tx_form_aggr() indicated
4926227364Sadrian		 * there can't be any further progress (eg BAW is full.)
4927227364Sadrian		 * Checking for an empty txq is done above.
4928227364Sadrian		 *
4929227364Sadrian		 * XXX locking on txq here?
4930227364Sadrian		 */
4931227364Sadrian		if (txq->axq_aggr_depth >= sc->sc_hwq_limit ||
4932227364Sadrian		    status == ATH_AGGR_BAW_CLOSED)
4933227364Sadrian			break;
4934227364Sadrian	}
4935227364Sadrian}
4936227364Sadrian
4937227364Sadrian/*
4938227364Sadrian * Schedule some packets from the given node/TID to the hardware.
4939227364Sadrian */
4940227364Sadrianvoid
4941227364Sadrianath_tx_tid_hw_queue_norm(struct ath_softc *sc, struct ath_node *an,
4942227364Sadrian    struct ath_tid *tid)
4943227364Sadrian{
4944227364Sadrian	struct ath_buf *bf;
4945227364Sadrian	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
4946227364Sadrian
4947227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: node %p: TID %d: called\n",
4948227364Sadrian	    __func__, an, tid->tid);
4949227364Sadrian
4950243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
4951227364Sadrian
4952227364Sadrian	/* Check - is AMPDU pending or running? then print out something */
4953227364Sadrian	if (ath_tx_ampdu_pending(sc, an, tid->tid))
4954227364Sadrian		device_printf(sc->sc_dev, "%s: tid=%d, ampdu pending?\n",
4955227364Sadrian		    __func__, tid->tid);
4956227364Sadrian	if (ath_tx_ampdu_running(sc, an, tid->tid))
4957227364Sadrian		device_printf(sc->sc_dev, "%s: tid=%d, ampdu running?\n",
4958227364Sadrian		    __func__, tid->tid);
4959227364Sadrian
4960227364Sadrian	for (;;) {
4961227364Sadrian
4962227364Sadrian		/*
4963227364Sadrian		 * If the upper layers have paused the TID, don't
4964227364Sadrian		 * queue any further packets.
4965227364Sadrian		 */
4966227364Sadrian		if (tid->paused)
4967227364Sadrian			break;
4968227364Sadrian
4969241336Sadrian		bf = ATH_TID_FIRST(tid);
4970227364Sadrian		if (bf == NULL) {
4971227364Sadrian			break;
4972227364Sadrian		}
4973227364Sadrian
4974241336Sadrian		ATH_TID_REMOVE(tid, bf, bf_list);
4975227364Sadrian
4976227364Sadrian		/* Sanity check! */
4977227364Sadrian		if (tid->tid != bf->bf_state.bfs_tid) {
4978227364Sadrian			device_printf(sc->sc_dev, "%s: bfs_tid %d !="
4979227364Sadrian			    " tid %d\n",
4980227364Sadrian			    __func__, bf->bf_state.bfs_tid, tid->tid);
4981227364Sadrian		}
4982227364Sadrian		/* Normal completion handler */
4983227364Sadrian		bf->bf_comp = ath_tx_normal_comp;
4984227364Sadrian
4985240883Sadrian		/*
4986240883Sadrian		 * Override this for now, until the non-aggregate
4987240883Sadrian		 * completion handler correctly handles software retransmits.
4988240883Sadrian		 */
4989240883Sadrian		bf->bf_state.bfs_txflags |= HAL_TXDESC_CLRDMASK;
4990240883Sadrian
4991240724Sadrian		/* Update CLRDMASK just before this frame is queued */
4992240724Sadrian		ath_tx_update_clrdmask(sc, tid, bf);
4993240724Sadrian
4994227364Sadrian		/* Program descriptors + rate control */
4995227364Sadrian		ath_tx_do_ratelookup(sc, bf);
4996233989Sadrian		ath_tx_calc_duration(sc, bf);
4997233989Sadrian		ath_tx_calc_protection(sc, bf);
4998233989Sadrian		ath_tx_set_rtscts(sc, bf);
4999227364Sadrian		ath_tx_rate_fill_rcflags(sc, bf);
5000227364Sadrian		ath_tx_setds(sc, bf);
5001227364Sadrian
5002227364Sadrian		/* Track outstanding buffer count to hardware */
5003227364Sadrian		/* aggregates are "one" buffer */
5004227364Sadrian		tid->hwq_depth++;
5005227364Sadrian
5006227364Sadrian		/* Punt to hardware or software txq */
5007227364Sadrian		ath_tx_handoff(sc, txq, bf);
5008227364Sadrian	}
5009227364Sadrian}
5010227364Sadrian
5011227364Sadrian/*
5012227364Sadrian * Schedule some packets to the given hardware queue.
5013227364Sadrian *
5014227364Sadrian * This function walks the list of TIDs (ie, ath_node TIDs
5015227364Sadrian * with queued traffic) and attempts to schedule traffic
5016227364Sadrian * from them.
5017227364Sadrian *
5018227364Sadrian * TID scheduling is implemented as a FIFO, with TIDs being
5019227364Sadrian * added to the end of the queue after some frames have been
5020227364Sadrian * scheduled.
5021227364Sadrian */
5022227364Sadrianvoid
5023227364Sadrianath_txq_sched(struct ath_softc *sc, struct ath_txq *txq)
5024227364Sadrian{
5025227364Sadrian	struct ath_tid *tid, *next, *last;
5026227364Sadrian
5027243786Sadrian	ATH_TX_LOCK_ASSERT(sc);
5028227364Sadrian
5029227364Sadrian	/*
5030227364Sadrian	 * Don't schedule if the hardware queue is busy.
5031227364Sadrian	 * This (hopefully) gives some more time to aggregate
5032227364Sadrian	 * some packets in the aggregation queue.
5033227364Sadrian	 */
5034227364Sadrian	if (txq->axq_aggr_depth >= sc->sc_hwq_limit) {
5035227364Sadrian		sc->sc_aggr_stats.aggr_sched_nopkt++;
5036227364Sadrian		return;
5037227364Sadrian	}
5038227364Sadrian
5039227364Sadrian	last = TAILQ_LAST(&txq->axq_tidq, axq_t_s);
5040227364Sadrian
5041227364Sadrian	TAILQ_FOREACH_SAFE(tid, &txq->axq_tidq, axq_qelem, next) {
5042227364Sadrian		/*
5043227364Sadrian		 * Suspend paused queues here; they'll be resumed
5044227364Sadrian		 * once the addba completes or times out.
5045227364Sadrian		 */
5046227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d, paused=%d\n",
5047227364Sadrian		    __func__, tid->tid, tid->paused);
5048227364Sadrian		ath_tx_tid_unsched(sc, tid);
5049227364Sadrian		if (tid->paused) {
5050227364Sadrian			continue;
5051227364Sadrian		}
5052227364Sadrian		if (ath_tx_ampdu_running(sc, tid->an, tid->tid))
5053227364Sadrian			ath_tx_tid_hw_queue_aggr(sc, tid->an, tid);
5054227364Sadrian		else
5055227364Sadrian			ath_tx_tid_hw_queue_norm(sc, tid->an, tid);
5056227364Sadrian
5057227364Sadrian		/* Not empty? Re-schedule */
5058227364Sadrian		if (tid->axq_depth != 0)
5059227364Sadrian			ath_tx_tid_sched(sc, tid);
5060227364Sadrian
5061227364Sadrian		/* Give the software queue time to aggregate more packets */
5062227364Sadrian		if (txq->axq_aggr_depth >= sc->sc_hwq_limit) {
5063227364Sadrian			break;
5064227364Sadrian		}
5065227364Sadrian
5066227364Sadrian		/*
5067227364Sadrian		 * If this was the last entry on the original list, stop.
5068227364Sadrian		 * Otherwise nodes that have been rescheduled onto the end
5069227364Sadrian		 * of the TID FIFO list will just keep being rescheduled.
5070227364Sadrian		 */
5071227364Sadrian		if (tid == last)
5072227364Sadrian			break;
5073227364Sadrian	}
5074227364Sadrian}
5075227364Sadrian
5076227364Sadrian/*
5077227364Sadrian * TX addba handling
5078227364Sadrian */
5079227364Sadrian
5080227364Sadrian/*
5081227364Sadrian * Return net80211 TID struct pointer, or NULL for none
5082227364Sadrian */
5083227364Sadrianstruct ieee80211_tx_ampdu *
5084227364Sadrianath_tx_get_tx_tid(struct ath_node *an, int tid)
5085227364Sadrian{
5086227364Sadrian	struct ieee80211_node *ni = &an->an_node;
5087227364Sadrian	struct ieee80211_tx_ampdu *tap;
5088227364Sadrian
5089227364Sadrian	if (tid == IEEE80211_NONQOS_TID)
5090227364Sadrian		return NULL;
5091227364Sadrian
5092234324Sadrian	tap = &ni->ni_tx_ampdu[tid];
5093227364Sadrian	return tap;
5094227364Sadrian}
5095227364Sadrian
5096227364Sadrian/*
5097227364Sadrian * Is AMPDU-TX running?
5098227364Sadrian */
5099227364Sadrianstatic int
5100227364Sadrianath_tx_ampdu_running(struct ath_softc *sc, struct ath_node *an, int tid)
5101227364Sadrian{
5102227364Sadrian	struct ieee80211_tx_ampdu *tap;
5103227364Sadrian
5104227364Sadrian	if (tid == IEEE80211_NONQOS_TID)
5105227364Sadrian		return 0;
5106227364Sadrian
5107227364Sadrian	tap = ath_tx_get_tx_tid(an, tid);
5108227364Sadrian	if (tap == NULL)
5109227364Sadrian		return 0;	/* Not valid; default to not running */
5110227364Sadrian
5111227364Sadrian	return !! (tap->txa_flags & IEEE80211_AGGR_RUNNING);
5112227364Sadrian}
5113227364Sadrian
5114227364Sadrian/*
5115227364Sadrian * Is AMPDU-TX negotiation pending?
5116227364Sadrian */
5117227364Sadrianstatic int
5118227364Sadrianath_tx_ampdu_pending(struct ath_softc *sc, struct ath_node *an, int tid)
5119227364Sadrian{
5120227364Sadrian	struct ieee80211_tx_ampdu *tap;
5121227364Sadrian
5122227364Sadrian	if (tid == IEEE80211_NONQOS_TID)
5123227364Sadrian		return 0;
5124227364Sadrian
5125227364Sadrian	tap = ath_tx_get_tx_tid(an, tid);
5126227364Sadrian	if (tap == NULL)
5127227364Sadrian		return 0;	/* Not valid; default to not pending */
5128227364Sadrian
5129227364Sadrian	return !! (tap->txa_flags & IEEE80211_AGGR_XCHGPEND);
5130227364Sadrian}
5131227364Sadrian
5132227364Sadrian/*
5133227364Sadrian * Is AMPDU-TX pending for the given TID?
5134227364Sadrian */
5135227364Sadrian
5136227364Sadrian
5137227364Sadrian/*
5138227364Sadrian * Method to handle sending an ADDBA request.
5139227364Sadrian *
5140227364Sadrian * We tap this so the relevant flags can be set to pause the TID
5141227364Sadrian * whilst waiting for the response.
5142227364Sadrian *
5143227364Sadrian * XXX there's no timeout handler we can override?
5144227364Sadrian */
5145227364Sadrianint
5146227364Sadrianath_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
5147227364Sadrian    int dialogtoken, int baparamset, int batimeout)
5148227364Sadrian{
5149227364Sadrian	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
5150234324Sadrian	int tid = tap->txa_tid;
5151227364Sadrian	struct ath_node *an = ATH_NODE(ni);
5152227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
5153227364Sadrian
5154227364Sadrian	/*
5155227364Sadrian	 * XXX danger Will Robinson!
5156227364Sadrian	 *
5157227364Sadrian	 * Although the taskqueue may be running and scheduling some more
5158227364Sadrian	 * packets, these should all be _before_ the addba sequence number.
5159227364Sadrian	 * However, net80211 will keep self-assigning sequence numbers
5160227364Sadrian	 * until addba has been negotiated.
5161227364Sadrian	 *
5162227364Sadrian	 * In the past, these packets would be "paused" (which still works
5163227364Sadrian	 * fine, as they're being scheduled to the driver in the same
5164227364Sadrian	 * serialised method which is calling the addba request routine)
5165227364Sadrian	 * and when the aggregation session begins, they'll be dequeued
5166227364Sadrian	 * as aggregate packets and added to the BAW. However, now there's
5167227364Sadrian	 * a "bf->bf_state.bfs_dobaw" flag, and this isn't set for these
5168227364Sadrian	 * packets. Thus they never get included in the BAW tracking and
5169227364Sadrian	 * this can cause the initial burst of packets after the addba
5170227364Sadrian	 * negotiation to "hang", as they quickly fall outside the BAW.
5171227364Sadrian	 *
5172227364Sadrian	 * The "eventual" solution should be to tag these packets with
5173227364Sadrian	 * dobaw. Although net80211 has given us a sequence number,
5174227364Sadrian	 * it'll be "after" the left edge of the BAW and thus it'll
5175227364Sadrian	 * fall within it.
5176227364Sadrian	 */
5177243786Sadrian	ATH_TX_LOCK(sc);
5178235774Sadrian	/*
5179235774Sadrian	 * This is a bit annoying.  Until net80211 HT code inherits some
5180235774Sadrian	 * (any) locking, we may have this called in parallel BUT only
5181235774Sadrian	 * one response/timeout will be called.  Grr.
5182235774Sadrian	 */
5183235774Sadrian	if (atid->addba_tx_pending == 0) {
5184235774Sadrian		ath_tx_tid_pause(sc, atid);
5185235774Sadrian		atid->addba_tx_pending = 1;
5186235774Sadrian	}
5187243786Sadrian	ATH_TX_UNLOCK(sc);
5188227364Sadrian
5189227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5190227364Sadrian	    "%s: called; dialogtoken=%d, baparamset=%d, batimeout=%d\n",
5191227364Sadrian	    __func__, dialogtoken, baparamset, batimeout);
5192227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5193227364Sadrian	    "%s: txa_start=%d, ni_txseqs=%d\n",
5194227364Sadrian	    __func__, tap->txa_start, ni->ni_txseqs[tid]);
5195227364Sadrian
5196227364Sadrian	return sc->sc_addba_request(ni, tap, dialogtoken, baparamset,
5197227364Sadrian	    batimeout);
5198227364Sadrian}
5199227364Sadrian
5200227364Sadrian/*
5201227364Sadrian * Handle an ADDBA response.
5202227364Sadrian *
5203227364Sadrian * We unpause the queue so TX'ing can resume.
5204227364Sadrian *
5205227364Sadrian * Any packets TX'ed from this point should be "aggregate" (whether
5206227364Sadrian * aggregate or not) so the BAW is updated.
5207227364Sadrian *
5208227364Sadrian * Note! net80211 keeps self-assigning sequence numbers until
5209227364Sadrian * ampdu is negotiated. This means the initially-negotiated BAW left
5210227364Sadrian * edge won't match the ni->ni_txseq.
5211227364Sadrian *
5212227364Sadrian * So, being very dirty, the BAW left edge is "slid" here to match
5213227364Sadrian * ni->ni_txseq.
5214227364Sadrian *
5215227364Sadrian * What likely SHOULD happen is that all packets subsequent to the
5216227364Sadrian * addba request should be tagged as aggregate and queued as non-aggregate
5217227364Sadrian * frames; thus updating the BAW. For now though, I'll just slide the
5218227364Sadrian * window.
5219227364Sadrian */
5220227364Sadrianint
5221227364Sadrianath_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
5222227364Sadrian    int status, int code, int batimeout)
5223227364Sadrian{
5224227364Sadrian	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
5225234324Sadrian	int tid = tap->txa_tid;
5226227364Sadrian	struct ath_node *an = ATH_NODE(ni);
5227227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
5228227364Sadrian	int r;
5229227364Sadrian
5230227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5231227364Sadrian	    "%s: called; status=%d, code=%d, batimeout=%d\n", __func__,
5232227364Sadrian	    status, code, batimeout);
5233227364Sadrian
5234227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5235227364Sadrian	    "%s: txa_start=%d, ni_txseqs=%d\n",
5236227364Sadrian	    __func__, tap->txa_start, ni->ni_txseqs[tid]);
5237227364Sadrian
5238227364Sadrian	/*
5239227364Sadrian	 * Call this first, so the interface flags get updated
5240227364Sadrian	 * before the TID is unpaused. Otherwise a race condition
5241227364Sadrian	 * exists where the unpaused TID still doesn't yet have
5242227364Sadrian	 * IEEE80211_AGGR_RUNNING set.
5243227364Sadrian	 */
5244227364Sadrian	r = sc->sc_addba_response(ni, tap, status, code, batimeout);
5245227364Sadrian
5246243786Sadrian	ATH_TX_LOCK(sc);
5247235774Sadrian	atid->addba_tx_pending = 0;
5248227364Sadrian	/*
5249227364Sadrian	 * XXX dirty!
5250227364Sadrian	 * Slide the BAW left edge to wherever net80211 left it for us.
5251227364Sadrian	 * Read above for more information.
5252227364Sadrian	 */
5253227364Sadrian	tap->txa_start = ni->ni_txseqs[tid];
5254227364Sadrian	ath_tx_tid_resume(sc, atid);
5255243786Sadrian	ATH_TX_UNLOCK(sc);
5256227364Sadrian	return r;
5257227364Sadrian}
5258227364Sadrian
5259227364Sadrian
5260227364Sadrian/*
5261227364Sadrian * Stop ADDBA on a queue.
5262237593Sadrian *
5263237593Sadrian * This can be called whilst BAR TX is currently active on the queue,
5264237593Sadrian * so make sure this is unblocked before continuing.
5265227364Sadrian */
5266227364Sadrianvoid
5267227364Sadrianath_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
5268227364Sadrian{
5269227364Sadrian	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
5270234324Sadrian	int tid = tap->txa_tid;
5271227364Sadrian	struct ath_node *an = ATH_NODE(ni);
5272227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
5273227364Sadrian
5274227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: called\n", __func__);
5275227364Sadrian
5276237593Sadrian	/*
5277237593Sadrian	 * Pause TID traffic early, so there aren't any races
5278237593Sadrian	 * Unblock the pending BAR held traffic, if it's currently paused.
5279237593Sadrian	 */
5280243786Sadrian	ATH_TX_LOCK(sc);
5281227364Sadrian	ath_tx_tid_pause(sc, atid);
5282237593Sadrian	if (atid->bar_wait) {
5283237593Sadrian		/*
5284237593Sadrian		 * bar_unsuspend() expects bar_tx == 1, as it should be
5285237593Sadrian		 * called from the TX completion path.  This quietens
5286237593Sadrian		 * the warning.  It's cleared for us anyway.
5287237593Sadrian		 */
5288237593Sadrian		atid->bar_tx = 1;
5289237593Sadrian		ath_tx_tid_bar_unsuspend(sc, atid);
5290237593Sadrian	}
5291243786Sadrian	ATH_TX_UNLOCK(sc);
5292227364Sadrian
5293227364Sadrian	/* There's no need to hold the TXQ lock here */
5294227364Sadrian	sc->sc_addba_stop(ni, tap);
5295227364Sadrian
5296227364Sadrian	/*
5297235749Sadrian	 * ath_tx_tid_cleanup will resume the TID if possible, otherwise
5298227364Sadrian	 * it'll set the cleanup flag, and it'll be unpaused once
5299227364Sadrian	 * things have been cleaned up.
5300227364Sadrian	 */
5301235749Sadrian	ath_tx_tid_cleanup(sc, an, tid);
5302227364Sadrian}
5303227364Sadrian
5304227364Sadrian/*
5305227364Sadrian * Note: net80211 bar_timeout() doesn't call this function on BAR failure;
5306227364Sadrian * it simply tears down the aggregation session. Ew.
5307227364Sadrian *
5308227364Sadrian * It however will call ieee80211_ampdu_stop() which will call
5309227364Sadrian * ic->ic_addba_stop().
5310227364Sadrian *
5311227364Sadrian * XXX This uses a hard-coded max BAR count value; the whole
5312227364Sadrian * XXX BAR TX success or failure should be better handled!
5313227364Sadrian */
5314227364Sadrianvoid
5315227364Sadrianath_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
5316227364Sadrian    int status)
5317227364Sadrian{
5318227364Sadrian	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
5319234324Sadrian	int tid = tap->txa_tid;
5320227364Sadrian	struct ath_node *an = ATH_NODE(ni);
5321227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
5322227364Sadrian	int attempts = tap->txa_attempts;
5323227364Sadrian
5324235491Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
5325235676Sadrian	    "%s: called; tap=%p, atid=%p, txa_tid=%d, atid->tid=%d, status=%d, attempts=%d\n",
5326235491Sadrian	    __func__,
5327235676Sadrian	    tap,
5328235676Sadrian	    atid,
5329235676Sadrian	    tap->txa_tid,
5330235676Sadrian	    atid->tid,
5331235491Sadrian	    status,
5332235491Sadrian	    attempts);
5333227364Sadrian
5334227364Sadrian	/* Note: This may update the BAW details */
5335227364Sadrian	sc->sc_bar_response(ni, tap, status);
5336227364Sadrian
5337227364Sadrian	/* Unpause the TID */
5338227364Sadrian	/*
5339227364Sadrian	 * XXX if this is attempt=50, the TID will be downgraded
5340227364Sadrian	 * XXX to a non-aggregate session. So we must unpause the
5341227364Sadrian	 * XXX TID here or it'll never be done.
5342240721Sadrian	 *
5343240721Sadrian	 * Also, don't call it if bar_tx/bar_wait are 0; something
5344240721Sadrian	 * has beaten us to the punch? (XXX figure out what?)
5345227364Sadrian	 */
5346227364Sadrian	if (status == 0 || attempts == 50) {
5347243786Sadrian		ATH_TX_LOCK(sc);
5348240721Sadrian		if (atid->bar_tx == 0 || atid->bar_wait == 0)
5349240721Sadrian			device_printf(sc->sc_dev,
5350240721Sadrian			    "%s: huh? bar_tx=%d, bar_wait=%d\n",
5351240721Sadrian			    __func__,
5352240721Sadrian			    atid->bar_tx, atid->bar_wait);
5353240721Sadrian		else
5354240721Sadrian			ath_tx_tid_bar_unsuspend(sc, atid);
5355243786Sadrian		ATH_TX_UNLOCK(sc);
5356227364Sadrian	}
5357227364Sadrian}
5358227364Sadrian
5359227364Sadrian/*
5360227364Sadrian * This is called whenever the pending ADDBA request times out.
5361227364Sadrian * Unpause and reschedule the TID.
5362227364Sadrian */
5363227364Sadrianvoid
5364227364Sadrianath_addba_response_timeout(struct ieee80211_node *ni,
5365227364Sadrian    struct ieee80211_tx_ampdu *tap)
5366227364Sadrian{
5367227364Sadrian	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
5368234324Sadrian	int tid = tap->txa_tid;
5369227364Sadrian	struct ath_node *an = ATH_NODE(ni);
5370227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
5371227364Sadrian
5372227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5373227364Sadrian	    "%s: called; resuming\n", __func__);
5374227364Sadrian
5375243786Sadrian	ATH_TX_LOCK(sc);
5376235774Sadrian	atid->addba_tx_pending = 0;
5377243786Sadrian	ATH_TX_UNLOCK(sc);
5378235774Sadrian
5379227364Sadrian	/* Note: This updates the aggregate state to (again) pending */
5380227364Sadrian	sc->sc_addba_response_timeout(ni, tap);
5381227364Sadrian
5382227364Sadrian	/* Unpause the TID; which reschedules it */
5383243786Sadrian	ATH_TX_LOCK(sc);
5384227364Sadrian	ath_tx_tid_resume(sc, atid);
5385243786Sadrian	ATH_TX_UNLOCK(sc);
5386227364Sadrian}
5387238710Sadrian
5388241170Sadrian/*
5389241170Sadrian * Check if a node is asleep or not.
5390241170Sadrian */
5391242271Sadrianint
5392241170Sadrianath_tx_node_is_asleep(struct ath_softc *sc, struct ath_node *an)
5393241170Sadrian{
5394241170Sadrian
5395241170Sadrian	ATH_NODE_LOCK_ASSERT(an);
5396241170Sadrian
5397241170Sadrian	return (an->an_is_powersave);
5398241170Sadrian}
5399241170Sadrian
5400241170Sadrian/*
5401241170Sadrian * Mark a node as currently "in powersaving."
5402241170Sadrian * This suspends all traffic on the node.
5403241170Sadrian *
5404241170Sadrian * This must be called with the node/tx locks free.
5405241170Sadrian *
5406241170Sadrian * XXX TODO: the locking silliness below is due to how the node
5407241170Sadrian * locking currently works.  Right now, the node lock is grabbed
5408241170Sadrian * to do rate control lookups and these are done with the TX
5409241170Sadrian * queue lock held.  This means the node lock can't be grabbed
5410241170Sadrian * first here or a LOR will occur.
5411241170Sadrian *
5412241170Sadrian * Eventually (hopefully!) the TX path code will only grab
5413241170Sadrian * the TXQ lock when transmitting and the ath_node lock when
5414241170Sadrian * doing node/TID operations.  There are other complications -
5415241170Sadrian * the sched/unsched operations involve walking the per-txq
5416241170Sadrian * 'active tid' list and this requires both locks to be held.
5417241170Sadrian */
5418241170Sadrianvoid
5419241170Sadrianath_tx_node_sleep(struct ath_softc *sc, struct ath_node *an)
5420241170Sadrian{
5421241170Sadrian	struct ath_tid *atid;
5422241170Sadrian	struct ath_txq *txq;
5423241170Sadrian	int tid;
5424241170Sadrian
5425241170Sadrian	ATH_NODE_UNLOCK_ASSERT(an);
5426241170Sadrian
5427241170Sadrian	/*
5428241170Sadrian	 * It's possible that a parallel call to ath_tx_node_wakeup()
5429241170Sadrian	 * will unpause these queues.
5430241170Sadrian	 *
5431241170Sadrian	 * The node lock can't just be grabbed here, as there's places
5432241170Sadrian	 * in the driver where the node lock is grabbed _within_ a
5433241170Sadrian	 * TXQ lock.
5434241170Sadrian	 * So, we do this delicately and unwind state if needed.
5435241170Sadrian	 *
5436241170Sadrian	 * + Pause all the queues
5437241170Sadrian	 * + Grab the node lock
5438241170Sadrian	 * + If the queue is already asleep, unpause and quit
5439241170Sadrian	 * + else just mark as asleep.
5440241170Sadrian	 *
5441241170Sadrian	 * A parallel sleep() call will just pause and then
5442241170Sadrian	 * find they're already paused, so undo it.
5443241170Sadrian	 *
5444241170Sadrian	 * A parallel wakeup() call will check if asleep is 1
5445241170Sadrian	 * and if it's not (ie, it's 0), it'll treat it as already
5446241170Sadrian	 * being awake. If it's 1, it'll mark it as 0 and then
5447241170Sadrian	 * unpause everything.
5448241170Sadrian	 *
5449241170Sadrian	 * (Talk about a delicate hack.)
5450241170Sadrian	 */
5451241170Sadrian
5452241170Sadrian	/* Suspend all traffic on the node */
5453243786Sadrian	ATH_TX_LOCK(sc);
5454241170Sadrian	for (tid = 0; tid < IEEE80211_TID_SIZE; tid++) {
5455241170Sadrian		atid = &an->an_tid[tid];
5456241170Sadrian		txq = sc->sc_ac2q[atid->ac];
5457241170Sadrian
5458241170Sadrian		ath_tx_tid_pause(sc, atid);
5459241170Sadrian	}
5460243786Sadrian	ATH_TX_UNLOCK(sc);
5461241170Sadrian
5462241170Sadrian	ATH_NODE_LOCK(an);
5463241170Sadrian
5464241170Sadrian	/* In case of concurrency races from net80211.. */
5465241170Sadrian	if (an->an_is_powersave == 1) {
5466241170Sadrian		ATH_NODE_UNLOCK(an);
5467241170Sadrian		device_printf(sc->sc_dev,
5468241170Sadrian		    "%s: an=%p: node was already asleep\n",
5469241170Sadrian		    __func__, an);
5470243786Sadrian		ATH_TX_LOCK(sc);
5471241170Sadrian		for (tid = 0; tid < IEEE80211_TID_SIZE; tid++) {
5472241170Sadrian			atid = &an->an_tid[tid];
5473241170Sadrian			txq = sc->sc_ac2q[atid->ac];
5474241170Sadrian
5475241170Sadrian			ath_tx_tid_resume(sc, atid);
5476241170Sadrian		}
5477243786Sadrian		ATH_TX_UNLOCK(sc);
5478241170Sadrian		return;
5479241170Sadrian	}
5480241170Sadrian
5481241170Sadrian	/* Mark node as in powersaving */
5482241170Sadrian	an->an_is_powersave = 1;
5483241170Sadrian
5484241170Sadrian	ATH_NODE_UNLOCK(an);
5485241170Sadrian}
5486241170Sadrian
5487241170Sadrian/*
5488241170Sadrian * Mark a node as currently "awake."
5489241170Sadrian * This resumes all traffic to the node.
5490241170Sadrian */
5491241170Sadrianvoid
5492241170Sadrianath_tx_node_wakeup(struct ath_softc *sc, struct ath_node *an)
5493241170Sadrian{
5494241170Sadrian	struct ath_tid *atid;
5495241170Sadrian	struct ath_txq *txq;
5496241170Sadrian	int tid;
5497241170Sadrian
5498241170Sadrian	ATH_NODE_UNLOCK_ASSERT(an);
5499241170Sadrian	ATH_NODE_LOCK(an);
5500241170Sadrian
5501241170Sadrian	/* In case of concurrency races from net80211.. */
5502241170Sadrian	if (an->an_is_powersave == 0) {
5503241170Sadrian		ATH_NODE_UNLOCK(an);
5504241170Sadrian		device_printf(sc->sc_dev,
5505241170Sadrian		    "%s: an=%p: node was already awake\n",
5506241170Sadrian		    __func__, an);
5507241170Sadrian		return;
5508241170Sadrian	}
5509241170Sadrian
5510241170Sadrian	/* Mark node as awake */
5511241170Sadrian	an->an_is_powersave = 0;
5512241170Sadrian
5513241170Sadrian	ATH_NODE_UNLOCK(an);
5514241170Sadrian
5515243786Sadrian	ATH_TX_LOCK(sc);
5516241170Sadrian	for (tid = 0; tid < IEEE80211_TID_SIZE; tid++) {
5517241170Sadrian		atid = &an->an_tid[tid];
5518241170Sadrian		txq = sc->sc_ac2q[atid->ac];
5519241170Sadrian
5520241170Sadrian		ath_tx_tid_resume(sc, atid);
5521241170Sadrian	}
5522243786Sadrian	ATH_TX_UNLOCK(sc);
5523241170Sadrian}
5524241170Sadrian
5525241170Sadrianstatic int
5526238710Sadrianath_legacy_dma_txsetup(struct ath_softc *sc)
5527238710Sadrian{
5528238710Sadrian
5529238710Sadrian	/* nothing new needed */
5530238710Sadrian	return (0);
5531238710Sadrian}
5532238710Sadrian
5533238710Sadrianstatic int
5534238710Sadrianath_legacy_dma_txteardown(struct ath_softc *sc)
5535238710Sadrian{
5536238710Sadrian
5537238710Sadrian	/* nothing new needed */
5538238710Sadrian	return (0);
5539238710Sadrian}
5540238710Sadrian
5541238710Sadrianvoid
5542238710Sadrianath_xmit_setup_legacy(struct ath_softc *sc)
5543238710Sadrian{
5544238729Sadrian	/*
5545238729Sadrian	 * For now, just set the descriptor length to sizeof(ath_desc);
5546238729Sadrian	 * worry about extracting the real length out of the HAL later.
5547238729Sadrian	 */
5548238729Sadrian	sc->sc_tx_desclen = sizeof(struct ath_desc);
5549243162Sadrian	sc->sc_tx_statuslen = sizeof(struct ath_desc);
5550238729Sadrian	sc->sc_tx_nmaps = 1;	/* only one buffer per TX desc */
5551238710Sadrian
5552238710Sadrian	sc->sc_tx.xmit_setup = ath_legacy_dma_txsetup;
5553238710Sadrian	sc->sc_tx.xmit_teardown = ath_legacy_dma_txteardown;
5554238931Sadrian	sc->sc_tx.xmit_attach_comp_func = ath_legacy_attach_comp_func;
5555238930Sadrian
5556238930Sadrian	sc->sc_tx.xmit_dma_restart = ath_legacy_tx_dma_restart;
5557238930Sadrian	sc->sc_tx.xmit_handoff = ath_legacy_xmit_handoff;
5558239204Sadrian
5559239204Sadrian	sc->sc_tx.xmit_drain = ath_legacy_tx_drain;
5560238710Sadrian}
5561