if_ath_tx.c revision 240639
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 240639 2012-09-18 10:14:17Z 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
104218154Sadrian/*
105227364Sadrian * How many retries to perform in software
106227364Sadrian */
107227364Sadrian#define	SWMAX_RETRIES		10
108227364Sadrian
109227364Sadrianstatic int ath_tx_ampdu_pending(struct ath_softc *sc, struct ath_node *an,
110227364Sadrian    int tid);
111227364Sadrianstatic int ath_tx_ampdu_running(struct ath_softc *sc, struct ath_node *an,
112227364Sadrian    int tid);
113236872Sadrianstatic ieee80211_seq ath_tx_tid_seqno_assign(struct ath_softc *sc,
114236872Sadrian    struct ieee80211_node *ni, struct ath_buf *bf, struct mbuf *m0);
115227364Sadrianstatic int ath_tx_action_frame_override_queue(struct ath_softc *sc,
116227364Sadrian    struct ieee80211_node *ni, struct mbuf *m0, int *tid);
117240639Sadrianstatic struct ath_buf *
118240639Sadrianath_tx_retry_clone(struct ath_softc *sc, struct ath_node *an,
119240639Sadrian    struct ath_tid *tid, struct ath_buf *bf);
120227364Sadrian
121227364Sadrian/*
122218154Sadrian * Whether to use the 11n rate scenario functions or not
123218154Sadrian */
124218154Sadrianstatic inline int
125218154Sadrianath_tx_is_11n(struct ath_softc *sc)
126218154Sadrian{
127239198Sadrian	return ((sc->sc_ah->ah_magic == 0x20065416) ||
128239198Sadrian		    (sc->sc_ah->ah_magic == 0x19741014));
129218154Sadrian}
130218154Sadrian
131227364Sadrian/*
132227364Sadrian * Obtain the current TID from the given frame.
133227364Sadrian *
134227364Sadrian * Non-QoS frames need to go into TID 16 (IEEE80211_NONQOS_TID.)
135227364Sadrian * This has implications for which AC/priority the packet is placed
136227364Sadrian * in.
137227364Sadrian */
138227364Sadrianstatic int
139227364Sadrianath_tx_gettid(struct ath_softc *sc, const struct mbuf *m0)
140227364Sadrian{
141227364Sadrian	const struct ieee80211_frame *wh;
142227364Sadrian	int pri = M_WME_GETAC(m0);
143227364Sadrian
144227364Sadrian	wh = mtod(m0, const struct ieee80211_frame *);
145227364Sadrian	if (! IEEE80211_QOS_HAS_SEQ(wh))
146227364Sadrian		return IEEE80211_NONQOS_TID;
147227364Sadrian	else
148227364Sadrian		return WME_AC_TO_TID(pri);
149227364Sadrian}
150227364Sadrian
151240639Sadrianstatic void
152240639Sadrianath_tx_set_retry(struct ath_softc *sc, struct ath_buf *bf)
153240639Sadrian{
154240639Sadrian	struct ieee80211_frame *wh;
155240639Sadrian
156240639Sadrian	wh = mtod(bf->bf_m, struct ieee80211_frame *);
157240639Sadrian	/* Only update/resync if needed */
158240639Sadrian	if (bf->bf_state.bfs_isretried == 0) {
159240639Sadrian		wh->i_fc[1] |= IEEE80211_FC1_RETRY;
160240639Sadrian		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
161240639Sadrian		    BUS_DMASYNC_PREWRITE);
162240639Sadrian	}
163240639Sadrian	bf->bf_state.bfs_isretried = 1;
164240639Sadrian	bf->bf_state.bfs_retries ++;
165240639Sadrian}
166240639Sadrian
167227364Sadrian/*
168227364Sadrian * Determine what the correct AC queue for the given frame
169227364Sadrian * should be.
170227364Sadrian *
171227364Sadrian * This code assumes that the TIDs map consistently to
172227364Sadrian * the underlying hardware (or software) ath_txq.
173227364Sadrian * Since the sender may try to set an AC which is
174227364Sadrian * arbitrary, non-QoS TIDs may end up being put on
175227364Sadrian * completely different ACs. There's no way to put a
176227364Sadrian * TID into multiple ath_txq's for scheduling, so
177227364Sadrian * for now we override the AC/TXQ selection and set
178227364Sadrian * non-QOS TID frames into the BE queue.
179227364Sadrian *
180227364Sadrian * This may be completely incorrect - specifically,
181227364Sadrian * some management frames may end up out of order
182227364Sadrian * compared to the QoS traffic they're controlling.
183227364Sadrian * I'll look into this later.
184227364Sadrian */
185227364Sadrianstatic int
186227364Sadrianath_tx_getac(struct ath_softc *sc, const struct mbuf *m0)
187227364Sadrian{
188227364Sadrian	const struct ieee80211_frame *wh;
189227364Sadrian	int pri = M_WME_GETAC(m0);
190227364Sadrian	wh = mtod(m0, const struct ieee80211_frame *);
191227364Sadrian	if (IEEE80211_QOS_HAS_SEQ(wh))
192227364Sadrian		return pri;
193227364Sadrian
194227364Sadrian	return WME_AC_BE;
195227364Sadrian}
196227364Sadrian
197218065Sadrianvoid
198218065Sadrianath_txfrag_cleanup(struct ath_softc *sc,
199218065Sadrian	ath_bufhead *frags, struct ieee80211_node *ni)
200218065Sadrian{
201218065Sadrian	struct ath_buf *bf, *next;
202218065Sadrian
203218065Sadrian	ATH_TXBUF_LOCK_ASSERT(sc);
204218065Sadrian
205227344Sadrian	TAILQ_FOREACH_SAFE(bf, frags, bf_list, next) {
206218065Sadrian		/* NB: bf assumed clean */
207227344Sadrian		TAILQ_REMOVE(frags, bf, bf_list);
208236993Sadrian		ath_returnbuf_head(sc, bf);
209218065Sadrian		ieee80211_node_decref(ni);
210218065Sadrian	}
211218065Sadrian}
212218065Sadrian
213218065Sadrian/*
214218065Sadrian * Setup xmit of a fragmented frame.  Allocate a buffer
215218065Sadrian * for each frag and bump the node reference count to
216218065Sadrian * reflect the held reference to be setup by ath_tx_start.
217218065Sadrian */
218218065Sadrianint
219218065Sadrianath_txfrag_setup(struct ath_softc *sc, ath_bufhead *frags,
220218065Sadrian	struct mbuf *m0, struct ieee80211_node *ni)
221218065Sadrian{
222218065Sadrian	struct mbuf *m;
223218065Sadrian	struct ath_buf *bf;
224218065Sadrian
225218065Sadrian	ATH_TXBUF_LOCK(sc);
226218065Sadrian	for (m = m0->m_nextpkt; m != NULL; m = m->m_nextpkt) {
227237000Sadrian		/* XXX non-management? */
228237000Sadrian		bf = _ath_getbuf_locked(sc, ATH_BUFTYPE_NORMAL);
229218065Sadrian		if (bf == NULL) {	/* out of buffers, cleanup */
230234009Sadrian			device_printf(sc->sc_dev, "%s: no buffer?\n",
231234009Sadrian			    __func__);
232218065Sadrian			ath_txfrag_cleanup(sc, frags, ni);
233218065Sadrian			break;
234218065Sadrian		}
235218065Sadrian		ieee80211_node_incref(ni);
236227344Sadrian		TAILQ_INSERT_TAIL(frags, bf, bf_list);
237218065Sadrian	}
238218065Sadrian	ATH_TXBUF_UNLOCK(sc);
239218065Sadrian
240227344Sadrian	return !TAILQ_EMPTY(frags);
241218065Sadrian}
242218065Sadrian
243218065Sadrian/*
244218065Sadrian * Reclaim mbuf resources.  For fragmented frames we
245218065Sadrian * need to claim each frag chained with m_nextpkt.
246218065Sadrian */
247218065Sadrianvoid
248218065Sadrianath_freetx(struct mbuf *m)
249218065Sadrian{
250218065Sadrian	struct mbuf *next;
251218065Sadrian
252218065Sadrian	do {
253218065Sadrian		next = m->m_nextpkt;
254218065Sadrian		m->m_nextpkt = NULL;
255218065Sadrian		m_freem(m);
256218065Sadrian	} while ((m = next) != NULL);
257218065Sadrian}
258218065Sadrian
259218065Sadrianstatic int
260218065Sadrianath_tx_dmasetup(struct ath_softc *sc, struct ath_buf *bf, struct mbuf *m0)
261218065Sadrian{
262218065Sadrian	struct mbuf *m;
263218065Sadrian	int error;
264218065Sadrian
265218065Sadrian	/*
266218065Sadrian	 * Load the DMA map so any coalescing is done.  This
267218065Sadrian	 * also calculates the number of descriptors we need.
268218065Sadrian	 */
269218065Sadrian	error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0,
270218065Sadrian				     bf->bf_segs, &bf->bf_nseg,
271218065Sadrian				     BUS_DMA_NOWAIT);
272218065Sadrian	if (error == EFBIG) {
273218065Sadrian		/* XXX packet requires too many descriptors */
274218065Sadrian		bf->bf_nseg = ATH_TXDESC+1;
275218065Sadrian	} else if (error != 0) {
276218065Sadrian		sc->sc_stats.ast_tx_busdma++;
277218065Sadrian		ath_freetx(m0);
278218065Sadrian		return error;
279218065Sadrian	}
280218065Sadrian	/*
281218065Sadrian	 * Discard null packets and check for packets that
282218065Sadrian	 * require too many TX descriptors.  We try to convert
283218065Sadrian	 * the latter to a cluster.
284218065Sadrian	 */
285218065Sadrian	if (bf->bf_nseg > ATH_TXDESC) {		/* too many desc's, linearize */
286218065Sadrian		sc->sc_stats.ast_tx_linear++;
287218065Sadrian		m = m_collapse(m0, M_DONTWAIT, ATH_TXDESC);
288218065Sadrian		if (m == NULL) {
289218065Sadrian			ath_freetx(m0);
290218065Sadrian			sc->sc_stats.ast_tx_nombuf++;
291218065Sadrian			return ENOMEM;
292218065Sadrian		}
293218065Sadrian		m0 = m;
294218065Sadrian		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0,
295218065Sadrian					     bf->bf_segs, &bf->bf_nseg,
296218065Sadrian					     BUS_DMA_NOWAIT);
297218065Sadrian		if (error != 0) {
298218065Sadrian			sc->sc_stats.ast_tx_busdma++;
299218065Sadrian			ath_freetx(m0);
300218065Sadrian			return error;
301218065Sadrian		}
302218065Sadrian		KASSERT(bf->bf_nseg <= ATH_TXDESC,
303218065Sadrian		    ("too many segments after defrag; nseg %u", bf->bf_nseg));
304218065Sadrian	} else if (bf->bf_nseg == 0) {		/* null packet, discard */
305218065Sadrian		sc->sc_stats.ast_tx_nodata++;
306218065Sadrian		ath_freetx(m0);
307218065Sadrian		return EIO;
308218065Sadrian	}
309218065Sadrian	DPRINTF(sc, ATH_DEBUG_XMIT, "%s: m %p len %u\n",
310218065Sadrian		__func__, m0, m0->m_pkthdr.len);
311218065Sadrian	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
312218065Sadrian	bf->bf_m = m0;
313218065Sadrian
314218065Sadrian	return 0;
315218065Sadrian}
316218065Sadrian
317227360Sadrian/*
318227360Sadrian * Chain together segments+descriptors for a non-11n frame.
319227360Sadrian */
320218065Sadrianstatic void
321227364Sadrianath_tx_chaindesclist(struct ath_softc *sc, struct ath_buf *bf)
322218065Sadrian{
323218065Sadrian	struct ath_hal *ah = sc->sc_ah;
324239409Sadrian	char *ds, *ds0;
325239290Sadrian	int i, bp, dsp;
326239051Sadrian	HAL_DMA_ADDR bufAddrList[4];
327239051Sadrian	uint32_t segLenList[4];
328239290Sadrian	int numTxMaps = 1;
329239380Sadrian	int isFirstDesc = 1;
330239465Sadrian	int qnum;
331239051Sadrian
332238708Sadrian	/*
333238708Sadrian	 * XXX There's txdma and txdma_mgmt; the descriptor
334238708Sadrian	 * sizes must match.
335238708Sadrian	 */
336238708Sadrian	struct ath_descdma *dd = &sc->sc_txdma;
337218065Sadrian
338218065Sadrian	/*
339218065Sadrian	 * Fillin the remainder of the descriptor info.
340218065Sadrian	 */
341239290Sadrian
342239290Sadrian	/*
343239290Sadrian	 * For now the HAL doesn't implement halNumTxMaps for non-EDMA
344239290Sadrian	 * (ie it's 0.)  So just work around it.
345239290Sadrian	 *
346239290Sadrian	 * XXX TODO: populate halNumTxMaps for each HAL chip and
347239290Sadrian	 * then undo this hack.
348239290Sadrian	 */
349239290Sadrian	if (sc->sc_ah->ah_magic == 0x19741014)
350239290Sadrian		numTxMaps = 4;
351239290Sadrian
352239290Sadrian	/*
353239290Sadrian	 * For EDMA and later chips ensure the TX map is fully populated
354239290Sadrian	 * before advancing to the next descriptor.
355239290Sadrian	 */
356239409Sadrian	ds0 = ds = (char *) bf->bf_desc;
357239290Sadrian	bp = dsp = 0;
358239290Sadrian	bzero(bufAddrList, sizeof(bufAddrList));
359239290Sadrian	bzero(segLenList, sizeof(segLenList));
360239290Sadrian	for (i = 0; i < bf->bf_nseg; i++) {
361239290Sadrian		bufAddrList[bp] = bf->bf_segs[i].ds_addr;
362239290Sadrian		segLenList[bp] = bf->bf_segs[i].ds_len;
363239290Sadrian		bp++;
364239051Sadrian
365239290Sadrian		/*
366239290Sadrian		 * Go to the next segment if this isn't the last segment
367239290Sadrian		 * and there's space in the current TX map.
368239290Sadrian		 */
369239290Sadrian		if ((i != bf->bf_nseg - 1) && (bp < numTxMaps))
370239290Sadrian			continue;
371239051Sadrian
372239290Sadrian		/*
373239290Sadrian		 * Last segment or we're out of buffer pointers.
374239290Sadrian		 */
375239290Sadrian		bp = 0;
376239290Sadrian
377218065Sadrian		if (i == bf->bf_nseg - 1)
378239409Sadrian			ath_hal_settxdesclink(ah, (struct ath_desc *) ds, 0);
379218065Sadrian		else
380239409Sadrian			ath_hal_settxdesclink(ah, (struct ath_desc *) ds,
381239290Sadrian			    bf->bf_daddr + dd->dd_descsize * (dsp + 1));
382239051Sadrian
383239051Sadrian		/*
384239051Sadrian		 * XXX this assumes that bfs_txq is the actual destination
385239051Sadrian		 * hardware queue at this point.  It may not have been assigned,
386239051Sadrian		 * it may actually be pointing to the multicast software
387239051Sadrian		 * TXQ id.  These must be fixed!
388239051Sadrian		 */
389239465Sadrian		qnum = bf->bf_state.bfs_txq->axq_qnum;
390239465Sadrian
391239409Sadrian		ath_hal_filltxdesc(ah, (struct ath_desc *) ds
392239051Sadrian			, bufAddrList
393239051Sadrian			, segLenList
394239290Sadrian			, bf->bf_descid		/* XXX desc id */
395239465Sadrian			, qnum
396239380Sadrian			, isFirstDesc		/* first segment */
397218065Sadrian			, i == bf->bf_nseg - 1	/* last segment */
398239409Sadrian			, (struct ath_desc *) ds0	/* first descriptor */
399218065Sadrian		);
400240255Sadrian
401240255Sadrian		/* Make sure the 11n aggregate fields are cleared */
402240255Sadrian		if (ath_tx_is_11n(sc))
403240333Sadrian			ath_hal_clr11n_aggr(sc->sc_ah, (struct ath_desc *) ds);
404240255Sadrian
405239380Sadrian		isFirstDesc = 0;
406239438Sadrian#ifdef	ATH_DEBUG
407239409Sadrian		if (sc->sc_debug & ATH_DEBUG_XMIT)
408239409Sadrian			ath_printtxbuf(sc, bf, qnum, 0, 0);
409239438Sadrian#endif
410239409Sadrian		bf->bf_lastds = (struct ath_desc *) ds;
411239290Sadrian
412239290Sadrian		/*
413239290Sadrian		 * Don't forget to skip to the next descriptor.
414239290Sadrian		 */
415239409Sadrian		ds += sc->sc_tx_desclen;
416239290Sadrian		dsp++;
417239290Sadrian
418239290Sadrian		/*
419239290Sadrian		 * .. and don't forget to blank these out!
420239290Sadrian		 */
421239290Sadrian		bzero(bufAddrList, sizeof(bufAddrList));
422239290Sadrian		bzero(segLenList, sizeof(segLenList));
423218065Sadrian	}
424233990Sadrian	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
425218154Sadrian}
426218154Sadrian
427227364Sadrian/*
428227364Sadrian * Fill in the descriptor list for a aggregate subframe.
429227364Sadrian *
430227364Sadrian * The subframe is returned with the ds_link field in the last subframe
431227364Sadrian * pointing to 0.
432227364Sadrian */
433218154Sadrianstatic void
434227364Sadrianath_tx_chaindesclist_subframe(struct ath_softc *sc, struct ath_buf *bf)
435218154Sadrian{
436218154Sadrian	struct ath_hal *ah = sc->sc_ah;
437227364Sadrian	struct ath_desc *ds, *ds0;
438227364Sadrian	int i;
439239053Sadrian	HAL_DMA_ADDR bufAddrList[4];
440239053Sadrian	uint32_t segLenList[4];
441239053Sadrian
442238708Sadrian	/*
443238708Sadrian	 * XXX There's txdma and txdma_mgmt; the descriptor
444238708Sadrian	 * sizes must match.
445238708Sadrian	 */
446238708Sadrian	struct ath_descdma *dd = &sc->sc_txdma;
447218154Sadrian
448227364Sadrian	ds0 = ds = bf->bf_desc;
449218154Sadrian
450218065Sadrian	/*
451227364Sadrian	 * There's no need to call ath_hal_setupfirsttxdesc here;
452227364Sadrian	 * That's only going to occur for the first frame in an aggregate.
453227364Sadrian	 */
454227364Sadrian	for (i = 0; i < bf->bf_nseg; i++, ds++) {
455239053Sadrian		bzero(bufAddrList, sizeof(bufAddrList));
456239053Sadrian		bzero(segLenList, sizeof(segLenList));
457227364Sadrian		if (i == bf->bf_nseg - 1)
458238609Sadrian			ath_hal_settxdesclink(ah, ds, 0);
459227364Sadrian		else
460238609Sadrian			ath_hal_settxdesclink(ah, ds,
461238708Sadrian			    bf->bf_daddr + dd->dd_descsize * (i + 1));
462227364Sadrian
463239053Sadrian		bufAddrList[0] = bf->bf_segs[i].ds_addr;
464239053Sadrian		segLenList[0] = bf->bf_segs[i].ds_len;
465239053Sadrian
466227364Sadrian		/*
467227364Sadrian		 * This performs the setup for an aggregate frame.
468227364Sadrian		 * This includes enabling the aggregate flags if needed.
469227364Sadrian		 */
470227364Sadrian		ath_hal_chaintxdesc(ah, ds,
471239053Sadrian		    bufAddrList,
472239053Sadrian		    segLenList,
473227364Sadrian		    bf->bf_state.bfs_pktlen,
474227364Sadrian		    bf->bf_state.bfs_hdrlen,
475227364Sadrian		    HAL_PKT_TYPE_AMPDU,	/* forces aggregate bits to be set */
476227364Sadrian		    bf->bf_state.bfs_keyix,
477227364Sadrian		    0,			/* cipher, calculated from keyix */
478227364Sadrian		    bf->bf_state.bfs_ndelim,
479227364Sadrian		    i == 0,		/* first segment */
480233895Sadrian		    i == bf->bf_nseg - 1,	/* last segment */
481233895Sadrian		    bf->bf_next == NULL		/* last sub-frame in aggr */
482227364Sadrian		);
483227364Sadrian
484227364Sadrian		DPRINTF(sc, ATH_DEBUG_XMIT,
485227364Sadrian			"%s: %d: %08x %08x %08x %08x %08x %08x\n",
486227364Sadrian			__func__, i, ds->ds_link, ds->ds_data,
487227364Sadrian			ds->ds_ctl0, ds->ds_ctl1, ds->ds_hw[0], ds->ds_hw[1]);
488227364Sadrian		bf->bf_lastds = ds;
489233990Sadrian		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
490233990Sadrian		    BUS_DMASYNC_PREWRITE);
491227364Sadrian	}
492227364Sadrian}
493227364Sadrian
494227364Sadrian/*
495238947Sadrian * Set the rate control fields in the given descriptor based on
496238947Sadrian * the bf_state fields and node state.
497238947Sadrian *
498238947Sadrian * The bfs fields should already be set with the relevant rate
499238947Sadrian * control information, including whether MRR is to be enabled.
500238947Sadrian *
501238947Sadrian * Since the FreeBSD HAL currently sets up the first TX rate
502238947Sadrian * in ath_hal_setuptxdesc(), this will setup the MRR
503238947Sadrian * conditionally for the pre-11n chips, and call ath_buf_set_rate
504238947Sadrian * unconditionally for 11n chips. These require the 11n rate
505238947Sadrian * scenario to be set if MCS rates are enabled, so it's easier
506238947Sadrian * to just always call it. The caller can then only set rates 2, 3
507238947Sadrian * and 4 if multi-rate retry is needed.
508238947Sadrian */
509238947Sadrianstatic void
510238947Sadrianath_tx_set_ratectrl(struct ath_softc *sc, struct ieee80211_node *ni,
511238947Sadrian    struct ath_buf *bf)
512238947Sadrian{
513238947Sadrian	struct ath_rc_series *rc = bf->bf_state.bfs_rc;
514238947Sadrian
515238947Sadrian	/* If mrr is disabled, blank tries 1, 2, 3 */
516238947Sadrian	if (! bf->bf_state.bfs_ismrr)
517238947Sadrian		rc[1].tries = rc[2].tries = rc[3].tries = 0;
518238947Sadrian
519238947Sadrian	/*
520238947Sadrian	 * Always call - that way a retried descriptor will
521238947Sadrian	 * have the MRR fields overwritten.
522238947Sadrian	 *
523238947Sadrian	 * XXX TODO: see if this is really needed - setting up
524238947Sadrian	 * the first descriptor should set the MRR fields to 0
525238947Sadrian	 * for us anyway.
526238947Sadrian	 */
527238947Sadrian	if (ath_tx_is_11n(sc)) {
528238947Sadrian		ath_buf_set_rate(sc, ni, bf);
529238947Sadrian	} else {
530238947Sadrian		ath_hal_setupxtxdesc(sc->sc_ah, bf->bf_desc
531238947Sadrian			, rc[1].ratecode, rc[1].tries
532238947Sadrian			, rc[2].ratecode, rc[2].tries
533238947Sadrian			, rc[3].ratecode, rc[3].tries
534238947Sadrian		);
535238947Sadrian	}
536238947Sadrian}
537238947Sadrian
538238947Sadrian/*
539227364Sadrian * Setup segments+descriptors for an 11n aggregate.
540227364Sadrian * bf_first is the first buffer in the aggregate.
541227364Sadrian * The descriptor list must already been linked together using
542227364Sadrian * bf->bf_next.
543227364Sadrian */
544227364Sadrianstatic void
545227364Sadrianath_tx_setds_11n(struct ath_softc *sc, struct ath_buf *bf_first)
546227364Sadrian{
547227364Sadrian	struct ath_buf *bf, *bf_prev = NULL;
548227364Sadrian
549227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: nframes=%d, al=%d\n",
550227364Sadrian	    __func__, bf_first->bf_state.bfs_nframes,
551227364Sadrian	    bf_first->bf_state.bfs_al);
552227364Sadrian
553227364Sadrian	/*
554227364Sadrian	 * Setup all descriptors of all subframes.
555227364Sadrian	 */
556227364Sadrian	bf = bf_first;
557227364Sadrian	while (bf != NULL) {
558227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
559227364Sadrian		    "%s: bf=%p, nseg=%d, pktlen=%d, seqno=%d\n",
560227364Sadrian		    __func__, bf, bf->bf_nseg, bf->bf_state.bfs_pktlen,
561227364Sadrian		    SEQNO(bf->bf_state.bfs_seqno));
562227364Sadrian
563227364Sadrian		/* Sub-frame setup */
564227364Sadrian		ath_tx_chaindesclist_subframe(sc, bf);
565227364Sadrian
566227364Sadrian		/*
567227364Sadrian		 * Link the last descriptor of the previous frame
568227364Sadrian		 * to the beginning descriptor of this frame.
569227364Sadrian		 */
570227364Sadrian		if (bf_prev != NULL)
571238609Sadrian			ath_hal_settxdesclink(sc->sc_ah, bf_prev->bf_lastds,
572238609Sadrian			    bf->bf_daddr);
573227364Sadrian
574227364Sadrian		/* Save a copy so we can link the next descriptor in */
575227364Sadrian		bf_prev = bf;
576227364Sadrian		bf = bf->bf_next;
577227364Sadrian	}
578227364Sadrian
579227364Sadrian	/*
580227364Sadrian	 * Setup first descriptor of first frame.
581227364Sadrian	 * chaintxdesc() overwrites the descriptor entries;
582227364Sadrian	 * setupfirsttxdesc() merges in things.
583227364Sadrian	 * Otherwise various fields aren't set correctly (eg flags).
584227364Sadrian	 */
585227364Sadrian	ath_hal_setupfirsttxdesc(sc->sc_ah,
586227364Sadrian	    bf_first->bf_desc,
587227364Sadrian	    bf_first->bf_state.bfs_al,
588233966Sadrian	    bf_first->bf_state.bfs_txflags | HAL_TXDESC_INTREQ,
589227364Sadrian	    bf_first->bf_state.bfs_txpower,
590227364Sadrian	    bf_first->bf_state.bfs_txrate0,
591227364Sadrian	    bf_first->bf_state.bfs_try0,
592227364Sadrian	    bf_first->bf_state.bfs_txantenna,
593227364Sadrian	    bf_first->bf_state.bfs_ctsrate,
594227364Sadrian	    bf_first->bf_state.bfs_ctsduration);
595227364Sadrian
596227364Sadrian	/*
597227364Sadrian	 * Set the first descriptor bf_lastds field to point to
598227364Sadrian	 * the last descriptor in the last subframe, that's where
599227364Sadrian	 * the status update will occur.
600227364Sadrian	 */
601227364Sadrian	bf_first->bf_lastds = bf_prev->bf_lastds;
602227364Sadrian
603227364Sadrian	/*
604227364Sadrian	 * And bf_last in the first descriptor points to the end of
605227364Sadrian	 * the aggregate list.
606227364Sadrian	 */
607227364Sadrian	bf_first->bf_last = bf_prev;
608227364Sadrian
609238947Sadrian	/*
610238947Sadrian	 * setup first desc with rate and aggr info
611238947Sadrian	 */
612238947Sadrian	ath_tx_set_ratectrl(sc, bf_first->bf_node, bf_first);
613238947Sadrian
614238949Sadrian	/*
615238949Sadrian	 * Setup the last descriptor in the list.
616238993Sadrian	 *
617238993Sadrian	 * bf_first->bf_lastds already points to it; the rate
618238993Sadrian	 * control information needs to be squirreled away here
619238993Sadrian	 * as well ans clearing the moreaggr/paddelim fields.
620238949Sadrian	 */
621238993Sadrian	ath_hal_setuplasttxdesc(sc->sc_ah, bf_first->bf_lastds,
622238949Sadrian	    bf_first->bf_desc);
623238949Sadrian
624227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: end\n", __func__);
625227364Sadrian}
626227364Sadrian
627239051Sadrian/*
628239051Sadrian * Hand-off a frame to the multicast TX queue.
629239051Sadrian *
630239051Sadrian * This is a software TXQ which will be appended to the CAB queue
631239051Sadrian * during the beacon setup code.
632239051Sadrian *
633239051Sadrian * XXX TODO: since the AR9300 EDMA TX queue support wants the QCU ID
634239051Sadrian * as part of the TX descriptor, bf_state.bfs_txq must be updated
635239051Sadrian * with the actual hardware txq, or all of this will fall apart.
636239051Sadrian *
637239051Sadrian * XXX It may not be a bad idea to just stuff the QCU ID into bf_state
638239051Sadrian * and retire bfs_txq; then make sure the CABQ QCU ID is populated
639239051Sadrian * correctly.
640239051Sadrian */
641227364Sadrianstatic void
642227364Sadrianath_tx_handoff_mcast(struct ath_softc *sc, struct ath_txq *txq,
643227364Sadrian    struct ath_buf *bf)
644227364Sadrian{
645227364Sadrian	ATH_TXQ_LOCK_ASSERT(txq);
646227364Sadrian	KASSERT((bf->bf_flags & ATH_BUF_BUSY) == 0,
647227364Sadrian	     ("%s: busy status 0x%x", __func__, bf->bf_flags));
648227364Sadrian	if (txq->axq_link != NULL) {
649227364Sadrian		struct ath_buf *last = ATH_TXQ_LAST(txq, axq_q_s);
650227364Sadrian		struct ieee80211_frame *wh;
651227364Sadrian
652227364Sadrian		/* mark previous frame */
653227364Sadrian		wh = mtod(last->bf_m, struct ieee80211_frame *);
654227364Sadrian		wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
655227364Sadrian		bus_dmamap_sync(sc->sc_dmat, last->bf_dmamap,
656227364Sadrian		    BUS_DMASYNC_PREWRITE);
657227364Sadrian
658227364Sadrian		/* link descriptor */
659227364Sadrian		*txq->axq_link = bf->bf_daddr;
660227364Sadrian	}
661227364Sadrian	ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
662238609Sadrian	ath_hal_gettxdesclinkptr(sc->sc_ah, bf->bf_lastds, &txq->axq_link);
663227364Sadrian}
664227364Sadrian
665227364Sadrian/*
666227364Sadrian * Hand-off packet to a hardware queue.
667227364Sadrian */
668227364Sadrianstatic void
669229949Sadrianath_tx_handoff_hw(struct ath_softc *sc, struct ath_txq *txq,
670229949Sadrian    struct ath_buf *bf)
671227364Sadrian{
672227364Sadrian	struct ath_hal *ah = sc->sc_ah;
673227364Sadrian
674227364Sadrian	/*
675218065Sadrian	 * Insert the frame on the outbound list and pass it on
676218065Sadrian	 * to the hardware.  Multicast frames buffered for power
677218065Sadrian	 * save stations and transmit from the CAB queue are stored
678218065Sadrian	 * on a s/w only queue and loaded on to the CAB queue in
679218065Sadrian	 * the SWBA handler since frames only go out on DTIM and
680218065Sadrian	 * to avoid possible races.
681218065Sadrian	 */
682227364Sadrian	ATH_TXQ_LOCK_ASSERT(txq);
683218065Sadrian	KASSERT((bf->bf_flags & ATH_BUF_BUSY) == 0,
684227364Sadrian	     ("%s: busy status 0x%x", __func__, bf->bf_flags));
685227364Sadrian	KASSERT(txq->axq_qnum != ATH_TXQ_SWQ,
686227364Sadrian	     ("ath_tx_handoff_hw called for mcast queue"));
687227364Sadrian
688227651Sadrian#if 0
689227651Sadrian	/*
690227651Sadrian	 * This causes a LOR. Find out where the PCU lock is being
691227651Sadrian	 * held whilst the TXQ lock is grabbed - that shouldn't
692227651Sadrian	 * be occuring.
693227651Sadrian	 */
694227651Sadrian	ATH_PCU_LOCK(sc);
695227651Sadrian	if (sc->sc_inreset_cnt) {
696227651Sadrian		ATH_PCU_UNLOCK(sc);
697227651Sadrian		DPRINTF(sc, ATH_DEBUG_RESET,
698227651Sadrian		    "%s: called with sc_in_reset != 0\n",
699227651Sadrian		    __func__);
700227651Sadrian		DPRINTF(sc, ATH_DEBUG_XMIT,
701227651Sadrian		    "%s: queued: TXDP[%u] = %p (%p) depth %d\n",
702227651Sadrian		    __func__, txq->axq_qnum,
703227651Sadrian		    (caddr_t)bf->bf_daddr, bf->bf_desc,
704227651Sadrian		    txq->axq_depth);
705227651Sadrian		ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
706227651Sadrian		if (bf->bf_state.bfs_aggr)
707227651Sadrian			txq->axq_aggr_depth++;
708227651Sadrian		/*
709227651Sadrian		 * There's no need to update axq_link; the hardware
710227651Sadrian		 * is in reset and once the reset is complete, any
711227651Sadrian		 * non-empty queues will simply have DMA restarted.
712227651Sadrian		 */
713227651Sadrian		return;
714227651Sadrian		}
715227651Sadrian	ATH_PCU_UNLOCK(sc);
716227651Sadrian#endif
717227651Sadrian
718227364Sadrian	/* For now, so not to generate whitespace diffs */
719227364Sadrian	if (1) {
720218065Sadrian#ifdef IEEE80211_SUPPORT_TDMA
721218065Sadrian		int qbusy;
722218065Sadrian
723218065Sadrian		ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
724218065Sadrian		qbusy = ath_hal_txqenabled(ah, txq->axq_qnum);
725218065Sadrian		if (txq->axq_link == NULL) {
726218065Sadrian			/*
727218065Sadrian			 * Be careful writing the address to TXDP.  If
728218065Sadrian			 * the tx q is enabled then this write will be
729218065Sadrian			 * ignored.  Normally this is not an issue but
730218065Sadrian			 * when tdma is in use and the q is beacon gated
731218065Sadrian			 * this race can occur.  If the q is busy then
732218065Sadrian			 * defer the work to later--either when another
733218065Sadrian			 * packet comes along or when we prepare a beacon
734218065Sadrian			 * frame at SWBA.
735218065Sadrian			 */
736218065Sadrian			if (!qbusy) {
737229949Sadrian				ath_hal_puttxbuf(ah, txq->axq_qnum,
738229949Sadrian				    bf->bf_daddr);
739218065Sadrian				txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
740218065Sadrian				DPRINTF(sc, ATH_DEBUG_XMIT,
741218065Sadrian				    "%s: TXDP[%u] = %p (%p) depth %d\n",
742218065Sadrian				    __func__, txq->axq_qnum,
743218065Sadrian				    (caddr_t)bf->bf_daddr, bf->bf_desc,
744218065Sadrian				    txq->axq_depth);
745218065Sadrian			} else {
746218065Sadrian				txq->axq_flags |= ATH_TXQ_PUTPENDING;
747218065Sadrian				DPRINTF(sc, ATH_DEBUG_TDMA | ATH_DEBUG_XMIT,
748218065Sadrian				    "%s: Q%u busy, defer enable\n", __func__,
749218065Sadrian				    txq->axq_qnum);
750218065Sadrian			}
751218065Sadrian		} else {
752218065Sadrian			*txq->axq_link = bf->bf_daddr;
753218065Sadrian			DPRINTF(sc, ATH_DEBUG_XMIT,
754218065Sadrian			    "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
755218065Sadrian			    txq->axq_qnum, txq->axq_link,
756229949Sadrian			    (caddr_t)bf->bf_daddr, bf->bf_desc,
757229949Sadrian			    txq->axq_depth);
758218065Sadrian			if ((txq->axq_flags & ATH_TXQ_PUTPENDING) && !qbusy) {
759218065Sadrian				/*
760218065Sadrian				 * The q was busy when we previously tried
761218065Sadrian				 * to write the address of the first buffer
762218065Sadrian				 * in the chain.  Since it's not busy now
763218065Sadrian				 * handle this chore.  We are certain the
764218065Sadrian				 * buffer at the front is the right one since
765218065Sadrian				 * axq_link is NULL only when the buffer list
766218065Sadrian				 * is/was empty.
767218065Sadrian				 */
768218065Sadrian				ath_hal_puttxbuf(ah, txq->axq_qnum,
769227344Sadrian					TAILQ_FIRST(&txq->axq_q)->bf_daddr);
770218065Sadrian				txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
771218065Sadrian				DPRINTF(sc, ATH_DEBUG_TDMA | ATH_DEBUG_XMIT,
772218065Sadrian				    "%s: Q%u restarted\n", __func__,
773218065Sadrian				    txq->axq_qnum);
774218065Sadrian			}
775218065Sadrian		}
776218065Sadrian#else
777218065Sadrian		ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
778218065Sadrian		if (txq->axq_link == NULL) {
779218065Sadrian			ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
780218065Sadrian			DPRINTF(sc, ATH_DEBUG_XMIT,
781218065Sadrian			    "%s: TXDP[%u] = %p (%p) depth %d\n",
782218065Sadrian			    __func__, txq->axq_qnum,
783218065Sadrian			    (caddr_t)bf->bf_daddr, bf->bf_desc,
784218065Sadrian			    txq->axq_depth);
785218065Sadrian		} else {
786218065Sadrian			*txq->axq_link = bf->bf_daddr;
787218065Sadrian			DPRINTF(sc, ATH_DEBUG_XMIT,
788218065Sadrian			    "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
789218065Sadrian			    txq->axq_qnum, txq->axq_link,
790229949Sadrian			    (caddr_t)bf->bf_daddr, bf->bf_desc,
791229949Sadrian			    txq->axq_depth);
792218065Sadrian		}
793218065Sadrian#endif /* IEEE80211_SUPPORT_TDMA */
794227360Sadrian		if (bf->bf_state.bfs_aggr)
795227360Sadrian			txq->axq_aggr_depth++;
796238609Sadrian		ath_hal_gettxdesclinkptr(ah, bf->bf_lastds, &txq->axq_link);
797218065Sadrian		ath_hal_txstart(ah, txq->axq_qnum);
798227364Sadrian	}
799227364Sadrian}
800218065Sadrian
801227364Sadrian/*
802227364Sadrian * Restart TX DMA for the given TXQ.
803227364Sadrian *
804227364Sadrian * This must be called whether the queue is empty or not.
805227364Sadrian */
806238930Sadrianstatic void
807238930Sadrianath_legacy_tx_dma_restart(struct ath_softc *sc, struct ath_txq *txq)
808227364Sadrian{
809227364Sadrian	struct ath_hal *ah = sc->sc_ah;
810232707Sadrian	struct ath_buf *bf, *bf_last;
811218065Sadrian
812227364Sadrian	ATH_TXQ_LOCK_ASSERT(txq);
813227364Sadrian
814227364Sadrian	/* This is always going to be cleared, empty or not */
815227364Sadrian	txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
816227364Sadrian
817232707Sadrian	/* XXX make this ATH_TXQ_FIRST */
818227364Sadrian	bf = TAILQ_FIRST(&txq->axq_q);
819232707Sadrian	bf_last = ATH_TXQ_LAST(txq, axq_q_s);
820232707Sadrian
821227364Sadrian	if (bf == NULL)
822227364Sadrian		return;
823227364Sadrian
824227364Sadrian	ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
825239120Sadrian	ath_hal_gettxdesclinkptr(ah, bf_last->bf_lastds, &txq->axq_link);
826227364Sadrian	ath_hal_txstart(ah, txq->axq_qnum);
827218065Sadrian}
828218065Sadrian
829227364Sadrian/*
830227364Sadrian * Hand off a packet to the hardware (or mcast queue.)
831227364Sadrian *
832227364Sadrian * The relevant hardware txq should be locked.
833227364Sadrian */
834227364Sadrianstatic void
835238930Sadrianath_legacy_xmit_handoff(struct ath_softc *sc, struct ath_txq *txq,
836238930Sadrian    struct ath_buf *bf)
837227364Sadrian{
838227364Sadrian	ATH_TXQ_LOCK_ASSERT(txq);
839227364Sadrian
840227364Sadrian	if (txq->axq_qnum == ATH_TXQ_SWQ)
841227364Sadrian		ath_tx_handoff_mcast(sc, txq, bf);
842227364Sadrian	else
843227364Sadrian		ath_tx_handoff_hw(sc, txq, bf);
844227364Sadrian}
845227364Sadrian
846218154Sadrianstatic int
847218154Sadrianath_tx_tag_crypto(struct ath_softc *sc, struct ieee80211_node *ni,
848229949Sadrian    struct mbuf *m0, int iswep, int isfrag, int *hdrlen, int *pktlen,
849229949Sadrian    int *keyix)
850218154Sadrian{
851233330Sadrian	DPRINTF(sc, ATH_DEBUG_XMIT,
852233330Sadrian	    "%s: hdrlen=%d, pktlen=%d, isfrag=%d, iswep=%d, m0=%p\n",
853233330Sadrian	    __func__,
854233330Sadrian	    *hdrlen,
855233330Sadrian	    *pktlen,
856233330Sadrian	    isfrag,
857233330Sadrian	    iswep,
858233330Sadrian	    m0);
859233330Sadrian
860218154Sadrian	if (iswep) {
861218154Sadrian		const struct ieee80211_cipher *cip;
862218154Sadrian		struct ieee80211_key *k;
863218154Sadrian
864218154Sadrian		/*
865218154Sadrian		 * Construct the 802.11 header+trailer for an encrypted
866218154Sadrian		 * frame. The only reason this can fail is because of an
867218154Sadrian		 * unknown or unsupported cipher/key type.
868218154Sadrian		 */
869218154Sadrian		k = ieee80211_crypto_encap(ni, m0);
870218154Sadrian		if (k == NULL) {
871218154Sadrian			/*
872218154Sadrian			 * This can happen when the key is yanked after the
873218154Sadrian			 * frame was queued.  Just discard the frame; the
874218154Sadrian			 * 802.11 layer counts failures and provides
875218154Sadrian			 * debugging/diagnostics.
876218154Sadrian			 */
877229949Sadrian			return (0);
878218154Sadrian		}
879218154Sadrian		/*
880218154Sadrian		 * Adjust the packet + header lengths for the crypto
881218154Sadrian		 * additions and calculate the h/w key index.  When
882218154Sadrian		 * a s/w mic is done the frame will have had any mic
883218154Sadrian		 * added to it prior to entry so m0->m_pkthdr.len will
884218154Sadrian		 * account for it. Otherwise we need to add it to the
885218154Sadrian		 * packet length.
886218154Sadrian		 */
887218154Sadrian		cip = k->wk_cipher;
888218154Sadrian		(*hdrlen) += cip->ic_header;
889218154Sadrian		(*pktlen) += cip->ic_header + cip->ic_trailer;
890218154Sadrian		/* NB: frags always have any TKIP MIC done in s/w */
891218154Sadrian		if ((k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && !isfrag)
892218154Sadrian			(*pktlen) += cip->ic_miclen;
893218154Sadrian		(*keyix) = k->wk_keyix;
894218154Sadrian	} else if (ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) {
895218154Sadrian		/*
896218154Sadrian		 * Use station key cache slot, if assigned.
897218154Sadrian		 */
898218154Sadrian		(*keyix) = ni->ni_ucastkey.wk_keyix;
899218154Sadrian		if ((*keyix) == IEEE80211_KEYIX_NONE)
900218154Sadrian			(*keyix) = HAL_TXKEYIX_INVALID;
901218154Sadrian	} else
902218154Sadrian		(*keyix) = HAL_TXKEYIX_INVALID;
903218154Sadrian
904229949Sadrian	return (1);
905218154Sadrian}
906218154Sadrian
907233989Sadrian/*
908233989Sadrian * Calculate whether interoperability protection is required for
909233989Sadrian * this frame.
910233989Sadrian *
911233989Sadrian * This requires the rate control information be filled in,
912233989Sadrian * as the protection requirement depends upon the current
913233989Sadrian * operating mode / PHY.
914233989Sadrian */
915233989Sadrianstatic void
916233989Sadrianath_tx_calc_protection(struct ath_softc *sc, struct ath_buf *bf)
917233989Sadrian{
918233989Sadrian	struct ieee80211_frame *wh;
919233989Sadrian	uint8_t rix;
920233989Sadrian	uint16_t flags;
921233989Sadrian	int shortPreamble;
922233989Sadrian	const HAL_RATE_TABLE *rt = sc->sc_currates;
923233989Sadrian	struct ifnet *ifp = sc->sc_ifp;
924233989Sadrian	struct ieee80211com *ic = ifp->if_l2com;
925233989Sadrian
926233989Sadrian	flags = bf->bf_state.bfs_txflags;
927233989Sadrian	rix = bf->bf_state.bfs_rc[0].rix;
928233989Sadrian	shortPreamble = bf->bf_state.bfs_shpream;
929233989Sadrian	wh = mtod(bf->bf_m, struct ieee80211_frame *);
930233989Sadrian
931233989Sadrian	/*
932233989Sadrian	 * If 802.11g protection is enabled, determine whether
933233989Sadrian	 * to use RTS/CTS or just CTS.  Note that this is only
934233989Sadrian	 * done for OFDM unicast frames.
935233989Sadrian	 */
936233989Sadrian	if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
937233989Sadrian	    rt->info[rix].phy == IEEE80211_T_OFDM &&
938233989Sadrian	    (flags & HAL_TXDESC_NOACK) == 0) {
939233989Sadrian		bf->bf_state.bfs_doprot = 1;
940233989Sadrian		/* XXX fragments must use CCK rates w/ protection */
941233989Sadrian		if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) {
942233989Sadrian			flags |= HAL_TXDESC_RTSENA;
943233989Sadrian		} else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) {
944233989Sadrian			flags |= HAL_TXDESC_CTSENA;
945233989Sadrian		}
946233989Sadrian		/*
947233989Sadrian		 * For frags it would be desirable to use the
948233989Sadrian		 * highest CCK rate for RTS/CTS.  But stations
949233989Sadrian		 * farther away may detect it at a lower CCK rate
950233989Sadrian		 * so use the configured protection rate instead
951233989Sadrian		 * (for now).
952233989Sadrian		 */
953233989Sadrian		sc->sc_stats.ast_tx_protect++;
954233989Sadrian	}
955233989Sadrian
956233989Sadrian	/*
957233989Sadrian	 * If 11n protection is enabled and it's a HT frame,
958233989Sadrian	 * enable RTS.
959233989Sadrian	 *
960233989Sadrian	 * XXX ic_htprotmode or ic_curhtprotmode?
961233989Sadrian	 * XXX should it_htprotmode only matter if ic_curhtprotmode
962233989Sadrian	 * XXX indicates it's not a HT pure environment?
963233989Sadrian	 */
964233989Sadrian	if ((ic->ic_htprotmode == IEEE80211_PROT_RTSCTS) &&
965233989Sadrian	    rt->info[rix].phy == IEEE80211_T_HT &&
966233989Sadrian	    (flags & HAL_TXDESC_NOACK) == 0) {
967233989Sadrian		flags |= HAL_TXDESC_RTSENA;
968233989Sadrian		sc->sc_stats.ast_tx_htprotect++;
969233989Sadrian	}
970233989Sadrian	bf->bf_state.bfs_txflags = flags;
971233989Sadrian}
972233989Sadrian
973233989Sadrian/*
974233989Sadrian * Update the frame duration given the currently selected rate.
975233989Sadrian *
976233989Sadrian * This also updates the frame duration value, so it will require
977233989Sadrian * a DMA flush.
978233989Sadrian */
979233989Sadrianstatic void
980233989Sadrianath_tx_calc_duration(struct ath_softc *sc, struct ath_buf *bf)
981233989Sadrian{
982233989Sadrian	struct ieee80211_frame *wh;
983233989Sadrian	uint8_t rix;
984233989Sadrian	uint16_t flags;
985233989Sadrian	int shortPreamble;
986233989Sadrian	struct ath_hal *ah = sc->sc_ah;
987233989Sadrian	const HAL_RATE_TABLE *rt = sc->sc_currates;
988233989Sadrian	int isfrag = bf->bf_m->m_flags & M_FRAG;
989233989Sadrian
990233989Sadrian	flags = bf->bf_state.bfs_txflags;
991233989Sadrian	rix = bf->bf_state.bfs_rc[0].rix;
992233989Sadrian	shortPreamble = bf->bf_state.bfs_shpream;
993233989Sadrian	wh = mtod(bf->bf_m, struct ieee80211_frame *);
994233989Sadrian
995233989Sadrian	/*
996233989Sadrian	 * Calculate duration.  This logically belongs in the 802.11
997233989Sadrian	 * layer but it lacks sufficient information to calculate it.
998233989Sadrian	 */
999233989Sadrian	if ((flags & HAL_TXDESC_NOACK) == 0 &&
1000233989Sadrian	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL) {
1001233989Sadrian		u_int16_t dur;
1002233989Sadrian		if (shortPreamble)
1003233989Sadrian			dur = rt->info[rix].spAckDuration;
1004233989Sadrian		else
1005233989Sadrian			dur = rt->info[rix].lpAckDuration;
1006233989Sadrian		if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) {
1007233989Sadrian			dur += dur;		/* additional SIFS+ACK */
1008233989Sadrian			KASSERT(bf->bf_m->m_nextpkt != NULL, ("no fragment"));
1009233989Sadrian			/*
1010233989Sadrian			 * Include the size of next fragment so NAV is
1011233989Sadrian			 * updated properly.  The last fragment uses only
1012233989Sadrian			 * the ACK duration
1013233989Sadrian			 */
1014233989Sadrian			dur += ath_hal_computetxtime(ah, rt,
1015233989Sadrian					bf->bf_m->m_nextpkt->m_pkthdr.len,
1016233989Sadrian					rix, shortPreamble);
1017233989Sadrian		}
1018233989Sadrian		if (isfrag) {
1019233989Sadrian			/*
1020233989Sadrian			 * Force hardware to use computed duration for next
1021233989Sadrian			 * fragment by disabling multi-rate retry which updates
1022233989Sadrian			 * duration based on the multi-rate duration table.
1023233989Sadrian			 */
1024233989Sadrian			bf->bf_state.bfs_ismrr = 0;
1025233989Sadrian			bf->bf_state.bfs_try0 = ATH_TXMGTTRY;
1026233989Sadrian			/* XXX update bfs_rc[0].try? */
1027233989Sadrian		}
1028233989Sadrian
1029233989Sadrian		/* Update the duration field itself */
1030233989Sadrian		*(u_int16_t *)wh->i_dur = htole16(dur);
1031233989Sadrian	}
1032233989Sadrian}
1033233989Sadrian
1034218932Sadrianstatic uint8_t
1035218932Sadrianath_tx_get_rtscts_rate(struct ath_hal *ah, const HAL_RATE_TABLE *rt,
1036227364Sadrian    int cix, int shortPreamble)
1037218157Sadrian{
1038218932Sadrian	uint8_t ctsrate;
1039218932Sadrian
1040218157Sadrian	/*
1041218157Sadrian	 * CTS transmit rate is derived from the transmit rate
1042218157Sadrian	 * by looking in the h/w rate table.  We must also factor
1043218157Sadrian	 * in whether or not a short preamble is to be used.
1044218157Sadrian	 */
1045218157Sadrian	/* NB: cix is set above where RTS/CTS is enabled */
1046218157Sadrian	KASSERT(cix != 0xff, ("cix not setup"));
1047218932Sadrian	ctsrate = rt->info[cix].rateCode;
1048218932Sadrian
1049218932Sadrian	/* XXX this should only matter for legacy rates */
1050218932Sadrian	if (shortPreamble)
1051218932Sadrian		ctsrate |= rt->info[cix].shortPreamble;
1052218932Sadrian
1053229949Sadrian	return (ctsrate);
1054218932Sadrian}
1055218932Sadrian
1056218932Sadrian/*
1057218932Sadrian * Calculate the RTS/CTS duration for legacy frames.
1058218932Sadrian */
1059218932Sadrianstatic int
1060218932Sadrianath_tx_calc_ctsduration(struct ath_hal *ah, int rix, int cix,
1061218932Sadrian    int shortPreamble, int pktlen, const HAL_RATE_TABLE *rt,
1062218932Sadrian    int flags)
1063218932Sadrian{
1064218932Sadrian	int ctsduration = 0;
1065218932Sadrian
1066218932Sadrian	/* This mustn't be called for HT modes */
1067218932Sadrian	if (rt->info[cix].phy == IEEE80211_T_HT) {
1068218932Sadrian		printf("%s: HT rate where it shouldn't be (0x%x)\n",
1069218932Sadrian		    __func__, rt->info[cix].rateCode);
1070229949Sadrian		return (-1);
1071218932Sadrian	}
1072218932Sadrian
1073218157Sadrian	/*
1074218157Sadrian	 * Compute the transmit duration based on the frame
1075218157Sadrian	 * size and the size of an ACK frame.  We call into the
1076218157Sadrian	 * HAL to do the computation since it depends on the
1077218157Sadrian	 * characteristics of the actual PHY being used.
1078218157Sadrian	 *
1079218157Sadrian	 * NB: CTS is assumed the same size as an ACK so we can
1080218157Sadrian	 *     use the precalculated ACK durations.
1081218157Sadrian	 */
1082218157Sadrian	if (shortPreamble) {
1083218157Sadrian		if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
1084218932Sadrian			ctsduration += rt->info[cix].spAckDuration;
1085218932Sadrian		ctsduration += ath_hal_computetxtime(ah,
1086218157Sadrian			rt, pktlen, rix, AH_TRUE);
1087218157Sadrian		if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
1088218932Sadrian			ctsduration += rt->info[rix].spAckDuration;
1089218157Sadrian	} else {
1090218157Sadrian		if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
1091218932Sadrian			ctsduration += rt->info[cix].lpAckDuration;
1092218932Sadrian		ctsduration += ath_hal_computetxtime(ah,
1093218157Sadrian			rt, pktlen, rix, AH_FALSE);
1094218157Sadrian		if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
1095218932Sadrian			ctsduration += rt->info[rix].lpAckDuration;
1096218157Sadrian	}
1097218932Sadrian
1098229949Sadrian	return (ctsduration);
1099218157Sadrian}
1100218157Sadrian
1101227364Sadrian/*
1102227364Sadrian * Update the given ath_buf with updated rts/cts setup and duration
1103227364Sadrian * values.
1104227364Sadrian *
1105227364Sadrian * To support rate lookups for each software retry, the rts/cts rate
1106227364Sadrian * and cts duration must be re-calculated.
1107227364Sadrian *
1108227364Sadrian * This function assumes the RTS/CTS flags have been set as needed;
1109227364Sadrian * mrr has been disabled; and the rate control lookup has been done.
1110227364Sadrian *
1111227364Sadrian * XXX TODO: MRR need only be disabled for the pre-11n NICs.
1112227364Sadrian * XXX The 11n NICs support per-rate RTS/CTS configuration.
1113227364Sadrian */
1114227364Sadrianstatic void
1115227364Sadrianath_tx_set_rtscts(struct ath_softc *sc, struct ath_buf *bf)
1116218065Sadrian{
1117227364Sadrian	uint16_t ctsduration = 0;
1118227364Sadrian	uint8_t ctsrate = 0;
1119227364Sadrian	uint8_t rix = bf->bf_state.bfs_rc[0].rix;
1120227364Sadrian	uint8_t cix = 0;
1121227364Sadrian	const HAL_RATE_TABLE *rt = sc->sc_currates;
1122227364Sadrian
1123227364Sadrian	/*
1124227364Sadrian	 * No RTS/CTS enabled? Don't bother.
1125227364Sadrian	 */
1126233966Sadrian	if ((bf->bf_state.bfs_txflags &
1127227364Sadrian	    (HAL_TXDESC_RTSENA | HAL_TXDESC_CTSENA)) == 0) {
1128227364Sadrian		/* XXX is this really needed? */
1129227364Sadrian		bf->bf_state.bfs_ctsrate = 0;
1130227364Sadrian		bf->bf_state.bfs_ctsduration = 0;
1131227364Sadrian		return;
1132227364Sadrian	}
1133227364Sadrian
1134227364Sadrian	/*
1135227364Sadrian	 * If protection is enabled, use the protection rix control
1136227364Sadrian	 * rate. Otherwise use the rate0 control rate.
1137227364Sadrian	 */
1138227364Sadrian	if (bf->bf_state.bfs_doprot)
1139227364Sadrian		rix = sc->sc_protrix;
1140227364Sadrian	else
1141227364Sadrian		rix = bf->bf_state.bfs_rc[0].rix;
1142227364Sadrian
1143227364Sadrian	/*
1144227364Sadrian	 * If the raw path has hard-coded ctsrate0 to something,
1145227364Sadrian	 * use it.
1146227364Sadrian	 */
1147227364Sadrian	if (bf->bf_state.bfs_ctsrate0 != 0)
1148227364Sadrian		cix = ath_tx_findrix(sc, bf->bf_state.bfs_ctsrate0);
1149227364Sadrian	else
1150227364Sadrian		/* Control rate from above */
1151227364Sadrian		cix = rt->info[rix].controlRate;
1152227364Sadrian
1153227364Sadrian	/* Calculate the rtscts rate for the given cix */
1154227364Sadrian	ctsrate = ath_tx_get_rtscts_rate(sc->sc_ah, rt, cix,
1155227364Sadrian	    bf->bf_state.bfs_shpream);
1156227364Sadrian
1157227364Sadrian	/* The 11n chipsets do ctsduration calculations for you */
1158227364Sadrian	if (! ath_tx_is_11n(sc))
1159227364Sadrian		ctsduration = ath_tx_calc_ctsduration(sc->sc_ah, rix, cix,
1160227364Sadrian		    bf->bf_state.bfs_shpream, bf->bf_state.bfs_pktlen,
1161233966Sadrian		    rt, bf->bf_state.bfs_txflags);
1162227364Sadrian
1163227364Sadrian	/* Squirrel away in ath_buf */
1164227364Sadrian	bf->bf_state.bfs_ctsrate = ctsrate;
1165227364Sadrian	bf->bf_state.bfs_ctsduration = ctsduration;
1166227364Sadrian
1167227364Sadrian	/*
1168227364Sadrian	 * Must disable multi-rate retry when using RTS/CTS.
1169227364Sadrian	 */
1170238961Sadrian	if (!sc->sc_mrrprot) {
1171238961Sadrian		bf->bf_state.bfs_ismrr = 0;
1172238961Sadrian		bf->bf_state.bfs_try0 =
1173238961Sadrian		    bf->bf_state.bfs_rc[0].tries = ATH_TXMGTTRY; /* XXX ew */
1174238961Sadrian	}
1175227364Sadrian}
1176227364Sadrian
1177227364Sadrian/*
1178227364Sadrian * Setup the descriptor chain for a normal or fast-frame
1179227364Sadrian * frame.
1180239051Sadrian *
1181239051Sadrian * XXX TODO: extend to include the destination hardware QCU ID.
1182239051Sadrian * Make sure that is correct.  Make sure that when being added
1183239051Sadrian * to the mcastq, the CABQ QCUID is set or things will get a bit
1184239051Sadrian * odd.
1185227364Sadrian */
1186227364Sadrianstatic void
1187227364Sadrianath_tx_setds(struct ath_softc *sc, struct ath_buf *bf)
1188227364Sadrian{
1189227364Sadrian	struct ath_desc *ds = bf->bf_desc;
1190227364Sadrian	struct ath_hal *ah = sc->sc_ah;
1191227364Sadrian
1192227364Sadrian	ath_hal_setuptxdesc(ah, ds
1193227364Sadrian		, bf->bf_state.bfs_pktlen	/* packet length */
1194227364Sadrian		, bf->bf_state.bfs_hdrlen	/* header length */
1195227364Sadrian		, bf->bf_state.bfs_atype	/* Atheros packet type */
1196227364Sadrian		, bf->bf_state.bfs_txpower	/* txpower */
1197227364Sadrian		, bf->bf_state.bfs_txrate0
1198227364Sadrian		, bf->bf_state.bfs_try0		/* series 0 rate/tries */
1199227364Sadrian		, bf->bf_state.bfs_keyix	/* key cache index */
1200227364Sadrian		, bf->bf_state.bfs_txantenna	/* antenna mode */
1201233966Sadrian		, bf->bf_state.bfs_txflags	/* flags */
1202227364Sadrian		, bf->bf_state.bfs_ctsrate	/* rts/cts rate */
1203227364Sadrian		, bf->bf_state.bfs_ctsduration	/* rts/cts duration */
1204227364Sadrian	);
1205227364Sadrian
1206227364Sadrian	/*
1207227364Sadrian	 * This will be overriden when the descriptor chain is written.
1208227364Sadrian	 */
1209227364Sadrian	bf->bf_lastds = ds;
1210227364Sadrian	bf->bf_last = bf;
1211227364Sadrian
1212238947Sadrian	/* Set rate control and descriptor chain for this frame */
1213238947Sadrian	ath_tx_set_ratectrl(sc, bf->bf_node, bf);
1214238947Sadrian	ath_tx_chaindesclist(sc, bf);
1215227364Sadrian}
1216227364Sadrian
1217227364Sadrian/*
1218227364Sadrian * Do a rate lookup.
1219227364Sadrian *
1220227364Sadrian * This performs a rate lookup for the given ath_buf only if it's required.
1221227364Sadrian * Non-data frames and raw frames don't require it.
1222227364Sadrian *
1223227364Sadrian * This populates the primary and MRR entries; MRR values are
1224227364Sadrian * then disabled later on if something requires it (eg RTS/CTS on
1225227364Sadrian * pre-11n chipsets.
1226227364Sadrian *
1227227364Sadrian * This needs to be done before the RTS/CTS fields are calculated
1228227364Sadrian * as they may depend upon the rate chosen.
1229227364Sadrian */
1230227364Sadrianstatic void
1231227364Sadrianath_tx_do_ratelookup(struct ath_softc *sc, struct ath_buf *bf)
1232227364Sadrian{
1233227364Sadrian	uint8_t rate, rix;
1234227364Sadrian	int try0;
1235227364Sadrian
1236227364Sadrian	if (! bf->bf_state.bfs_doratelookup)
1237227364Sadrian		return;
1238227364Sadrian
1239227364Sadrian	/* Get rid of any previous state */
1240227364Sadrian	bzero(bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
1241227364Sadrian
1242227364Sadrian	ATH_NODE_LOCK(ATH_NODE(bf->bf_node));
1243227364Sadrian	ath_rate_findrate(sc, ATH_NODE(bf->bf_node), bf->bf_state.bfs_shpream,
1244227364Sadrian	    bf->bf_state.bfs_pktlen, &rix, &try0, &rate);
1245227364Sadrian
1246227364Sadrian	/* In case MRR is disabled, make sure rc[0] is setup correctly */
1247227364Sadrian	bf->bf_state.bfs_rc[0].rix = rix;
1248227364Sadrian	bf->bf_state.bfs_rc[0].ratecode = rate;
1249227364Sadrian	bf->bf_state.bfs_rc[0].tries = try0;
1250227364Sadrian
1251227364Sadrian	if (bf->bf_state.bfs_ismrr && try0 != ATH_TXMAXTRY)
1252227364Sadrian		ath_rate_getxtxrates(sc, ATH_NODE(bf->bf_node), rix,
1253227364Sadrian		    bf->bf_state.bfs_rc);
1254227364Sadrian	ATH_NODE_UNLOCK(ATH_NODE(bf->bf_node));
1255227364Sadrian
1256227364Sadrian	sc->sc_txrix = rix;	/* for LED blinking */
1257227364Sadrian	sc->sc_lastdatarix = rix;	/* for fast frames */
1258227364Sadrian	bf->bf_state.bfs_try0 = try0;
1259227364Sadrian	bf->bf_state.bfs_txrate0 = rate;
1260227364Sadrian}
1261227364Sadrian
1262227364Sadrian/*
1263227364Sadrian * Transmit the given frame to the hardware.
1264227364Sadrian *
1265227364Sadrian * The frame must already be setup; rate control must already have
1266227364Sadrian * been done.
1267227364Sadrian *
1268227364Sadrian * XXX since the TXQ lock is being held here (and I dislike holding
1269227364Sadrian * it for this long when not doing software aggregation), later on
1270227364Sadrian * break this function into "setup_normal" and "xmit_normal". The
1271227364Sadrian * lock only needs to be held for the ath_tx_handoff call.
1272227364Sadrian */
1273227364Sadrianstatic void
1274227364Sadrianath_tx_xmit_normal(struct ath_softc *sc, struct ath_txq *txq,
1275227364Sadrian    struct ath_buf *bf)
1276227364Sadrian{
1277227364Sadrian
1278227364Sadrian	ATH_TXQ_LOCK_ASSERT(txq);
1279227364Sadrian
1280227364Sadrian	/* Setup the descriptor before handoff */
1281227364Sadrian	ath_tx_do_ratelookup(sc, bf);
1282233989Sadrian	ath_tx_calc_duration(sc, bf);
1283233989Sadrian	ath_tx_calc_protection(sc, bf);
1284233989Sadrian	ath_tx_set_rtscts(sc, bf);
1285227364Sadrian	ath_tx_rate_fill_rcflags(sc, bf);
1286227364Sadrian	ath_tx_setds(sc, bf);
1287227364Sadrian
1288227364Sadrian	/* Hand off to hardware */
1289227364Sadrian	ath_tx_handoff(sc, txq, bf);
1290227364Sadrian}
1291227364Sadrian
1292227364Sadrian
1293227364Sadrian
1294227364Sadrianstatic int
1295227364Sadrianath_tx_normal_setup(struct ath_softc *sc, struct ieee80211_node *ni,
1296234009Sadrian    struct ath_buf *bf, struct mbuf *m0, struct ath_txq *txq)
1297227364Sadrian{
1298218065Sadrian	struct ieee80211vap *vap = ni->ni_vap;
1299218065Sadrian	struct ath_hal *ah = sc->sc_ah;
1300218065Sadrian	struct ifnet *ifp = sc->sc_ifp;
1301218065Sadrian	struct ieee80211com *ic = ifp->if_l2com;
1302218065Sadrian	const struct chanAccParams *cap = &ic->ic_wme.wme_chanParams;
1303218065Sadrian	int error, iswep, ismcast, isfrag, ismrr;
1304227364Sadrian	int keyix, hdrlen, pktlen, try0 = 0;
1305227364Sadrian	u_int8_t rix = 0, txrate = 0;
1306218065Sadrian	struct ath_desc *ds;
1307218065Sadrian	struct ieee80211_frame *wh;
1308227364Sadrian	u_int subtype, flags;
1309218065Sadrian	HAL_PKT_TYPE atype;
1310218065Sadrian	const HAL_RATE_TABLE *rt;
1311218065Sadrian	HAL_BOOL shortPreamble;
1312218065Sadrian	struct ath_node *an;
1313218065Sadrian	u_int pri;
1314218065Sadrian
1315236880Sadrian	/*
1316236880Sadrian	 * To ensure that both sequence numbers and the CCMP PN handling
1317236880Sadrian	 * is "correct", make sure that the relevant TID queue is locked.
1318236880Sadrian	 * Otherwise the CCMP PN and seqno may appear out of order, causing
1319236880Sadrian	 * re-ordered frames to have out of order CCMP PN's, resulting
1320236880Sadrian	 * in many, many frame drops.
1321236880Sadrian	 */
1322236880Sadrian	ATH_TXQ_LOCK_ASSERT(txq);
1323236880Sadrian
1324218065Sadrian	wh = mtod(m0, struct ieee80211_frame *);
1325218065Sadrian	iswep = wh->i_fc[1] & IEEE80211_FC1_WEP;
1326218065Sadrian	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1327218065Sadrian	isfrag = m0->m_flags & M_FRAG;
1328218065Sadrian	hdrlen = ieee80211_anyhdrsize(wh);
1329218065Sadrian	/*
1330218065Sadrian	 * Packet length must not include any
1331218065Sadrian	 * pad bytes; deduct them here.
1332218065Sadrian	 */
1333218065Sadrian	pktlen = m0->m_pkthdr.len - (hdrlen & 3);
1334218065Sadrian
1335218154Sadrian	/* Handle encryption twiddling if needed */
1336227364Sadrian	if (! ath_tx_tag_crypto(sc, ni, m0, iswep, isfrag, &hdrlen,
1337227364Sadrian	    &pktlen, &keyix)) {
1338218154Sadrian		ath_freetx(m0);
1339218154Sadrian		return EIO;
1340218154Sadrian	}
1341218065Sadrian
1342218154Sadrian	/* packet header may have moved, reset our local pointer */
1343218154Sadrian	wh = mtod(m0, struct ieee80211_frame *);
1344218065Sadrian
1345218065Sadrian	pktlen += IEEE80211_CRC_LEN;
1346218065Sadrian
1347218065Sadrian	/*
1348218065Sadrian	 * Load the DMA map so any coalescing is done.  This
1349218065Sadrian	 * also calculates the number of descriptors we need.
1350218065Sadrian	 */
1351218065Sadrian	error = ath_tx_dmasetup(sc, bf, m0);
1352218065Sadrian	if (error != 0)
1353218065Sadrian		return error;
1354218065Sadrian	bf->bf_node = ni;			/* NB: held reference */
1355218065Sadrian	m0 = bf->bf_m;				/* NB: may have changed */
1356218065Sadrian	wh = mtod(m0, struct ieee80211_frame *);
1357218065Sadrian
1358218065Sadrian	/* setup descriptors */
1359218065Sadrian	ds = bf->bf_desc;
1360218065Sadrian	rt = sc->sc_currates;
1361218065Sadrian	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
1362218065Sadrian
1363218065Sadrian	/*
1364218065Sadrian	 * NB: the 802.11 layer marks whether or not we should
1365218065Sadrian	 * use short preamble based on the current mode and
1366218065Sadrian	 * negotiated parameters.
1367218065Sadrian	 */
1368218065Sadrian	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1369218065Sadrian	    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
1370218065Sadrian		shortPreamble = AH_TRUE;
1371218065Sadrian		sc->sc_stats.ast_tx_shortpre++;
1372218065Sadrian	} else {
1373218065Sadrian		shortPreamble = AH_FALSE;
1374218065Sadrian	}
1375218065Sadrian
1376218065Sadrian	an = ATH_NODE(ni);
1377218065Sadrian	flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for crypto errs */
1378218065Sadrian	ismrr = 0;				/* default no multi-rate retry*/
1379218065Sadrian	pri = M_WME_GETAC(m0);			/* honor classification */
1380218065Sadrian	/* XXX use txparams instead of fixed values */
1381218065Sadrian	/*
1382218065Sadrian	 * Calculate Atheros packet type from IEEE80211 packet header,
1383218065Sadrian	 * setup for rate calculations, and select h/w transmit queue.
1384218065Sadrian	 */
1385218065Sadrian	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1386218065Sadrian	case IEEE80211_FC0_TYPE_MGT:
1387218065Sadrian		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1388218065Sadrian		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON)
1389218065Sadrian			atype = HAL_PKT_TYPE_BEACON;
1390218065Sadrian		else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1391218065Sadrian			atype = HAL_PKT_TYPE_PROBE_RESP;
1392218065Sadrian		else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM)
1393218065Sadrian			atype = HAL_PKT_TYPE_ATIM;
1394218065Sadrian		else
1395218065Sadrian			atype = HAL_PKT_TYPE_NORMAL;	/* XXX */
1396218065Sadrian		rix = an->an_mgmtrix;
1397218065Sadrian		txrate = rt->info[rix].rateCode;
1398218065Sadrian		if (shortPreamble)
1399218065Sadrian			txrate |= rt->info[rix].shortPreamble;
1400218065Sadrian		try0 = ATH_TXMGTTRY;
1401218065Sadrian		flags |= HAL_TXDESC_INTREQ;	/* force interrupt */
1402218065Sadrian		break;
1403218065Sadrian	case IEEE80211_FC0_TYPE_CTL:
1404218065Sadrian		atype = HAL_PKT_TYPE_PSPOLL;	/* stop setting of duration */
1405218065Sadrian		rix = an->an_mgmtrix;
1406218065Sadrian		txrate = rt->info[rix].rateCode;
1407218065Sadrian		if (shortPreamble)
1408218065Sadrian			txrate |= rt->info[rix].shortPreamble;
1409218065Sadrian		try0 = ATH_TXMGTTRY;
1410218065Sadrian		flags |= HAL_TXDESC_INTREQ;	/* force interrupt */
1411218065Sadrian		break;
1412218065Sadrian	case IEEE80211_FC0_TYPE_DATA:
1413218065Sadrian		atype = HAL_PKT_TYPE_NORMAL;		/* default */
1414218065Sadrian		/*
1415218065Sadrian		 * Data frames: multicast frames go out at a fixed rate,
1416218065Sadrian		 * EAPOL frames use the mgmt frame rate; otherwise consult
1417218065Sadrian		 * the rate control module for the rate to use.
1418218065Sadrian		 */
1419218065Sadrian		if (ismcast) {
1420218065Sadrian			rix = an->an_mcastrix;
1421218065Sadrian			txrate = rt->info[rix].rateCode;
1422218065Sadrian			if (shortPreamble)
1423218065Sadrian				txrate |= rt->info[rix].shortPreamble;
1424218065Sadrian			try0 = 1;
1425218065Sadrian		} else if (m0->m_flags & M_EAPOL) {
1426218065Sadrian			/* XXX? maybe always use long preamble? */
1427218065Sadrian			rix = an->an_mgmtrix;
1428218065Sadrian			txrate = rt->info[rix].rateCode;
1429218065Sadrian			if (shortPreamble)
1430218065Sadrian				txrate |= rt->info[rix].shortPreamble;
1431218065Sadrian			try0 = ATH_TXMAXTRY;	/* XXX?too many? */
1432218065Sadrian		} else {
1433227364Sadrian			/*
1434227364Sadrian			 * Do rate lookup on each TX, rather than using
1435227364Sadrian			 * the hard-coded TX information decided here.
1436227364Sadrian			 */
1437227364Sadrian			ismrr = 1;
1438227364Sadrian			bf->bf_state.bfs_doratelookup = 1;
1439218065Sadrian		}
1440218065Sadrian		if (cap->cap_wmeParams[pri].wmep_noackPolicy)
1441218065Sadrian			flags |= HAL_TXDESC_NOACK;
1442218065Sadrian		break;
1443218065Sadrian	default:
1444218065Sadrian		if_printf(ifp, "bogus frame type 0x%x (%s)\n",
1445218065Sadrian			wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__);
1446218065Sadrian		/* XXX statistic */
1447218065Sadrian		ath_freetx(m0);
1448218065Sadrian		return EIO;
1449218065Sadrian	}
1450218065Sadrian
1451237041Sadrian	/*
1452237041Sadrian	 * There are two known scenarios where the frame AC doesn't match
1453237041Sadrian	 * what the destination TXQ is.
1454237041Sadrian	 *
1455237041Sadrian	 * + non-QoS frames (eg management?) that the net80211 stack has
1456237041Sadrian	 *   assigned a higher AC to, but since it's a non-QoS TID, it's
1457237041Sadrian	 *   being thrown into TID 16.  TID 16 gets the AC_BE queue.
1458237041Sadrian	 *   It's quite possible that management frames should just be
1459237041Sadrian	 *   direct dispatched to hardware rather than go via the software
1460237041Sadrian	 *   queue; that should be investigated in the future.  There are
1461237041Sadrian	 *   some specific scenarios where this doesn't make sense, mostly
1462237041Sadrian	 *   surrounding ADDBA request/response - hence why that is special
1463237041Sadrian	 *   cased.
1464237041Sadrian	 *
1465237041Sadrian	 * + Multicast frames going into the VAP mcast queue.  That shows up
1466237041Sadrian	 *   as "TXQ 11".
1467237041Sadrian	 *
1468237041Sadrian	 * This driver should eventually support separate TID and TXQ locking,
1469237041Sadrian	 * allowing for arbitrary AC frames to appear on arbitrary software
1470237041Sadrian	 * queues, being queued to the "correct" hardware queue when needed.
1471237041Sadrian	 */
1472237041Sadrian#if 0
1473235750Sadrian	if (txq != sc->sc_ac2q[pri]) {
1474235750Sadrian		device_printf(sc->sc_dev,
1475235750Sadrian		    "%s: txq=%p (%d), pri=%d, pri txq=%p (%d)\n",
1476235750Sadrian		    __func__,
1477235750Sadrian		    txq,
1478235750Sadrian		    txq->axq_qnum,
1479235750Sadrian		    pri,
1480235750Sadrian		    sc->sc_ac2q[pri],
1481235750Sadrian		    sc->sc_ac2q[pri]->axq_qnum);
1482235750Sadrian	}
1483237041Sadrian#endif
1484235750Sadrian
1485218065Sadrian	/*
1486218065Sadrian	 * Calculate miscellaneous flags.
1487218065Sadrian	 */
1488218065Sadrian	if (ismcast) {
1489218065Sadrian		flags |= HAL_TXDESC_NOACK;	/* no ack on broad/multicast */
1490218065Sadrian	} else if (pktlen > vap->iv_rtsthreshold &&
1491218065Sadrian	    (ni->ni_ath_flags & IEEE80211_NODE_FF) == 0) {
1492218065Sadrian		flags |= HAL_TXDESC_RTSENA;	/* RTS based on frame length */
1493218065Sadrian		sc->sc_stats.ast_tx_rts++;
1494218065Sadrian	}
1495218065Sadrian	if (flags & HAL_TXDESC_NOACK)		/* NB: avoid double counting */
1496218065Sadrian		sc->sc_stats.ast_tx_noack++;
1497218065Sadrian#ifdef IEEE80211_SUPPORT_TDMA
1498218065Sadrian	if (sc->sc_tdma && (flags & HAL_TXDESC_NOACK) == 0) {
1499218065Sadrian		DPRINTF(sc, ATH_DEBUG_TDMA,
1500218065Sadrian		    "%s: discard frame, ACK required w/ TDMA\n", __func__);
1501218065Sadrian		sc->sc_stats.ast_tdma_ack++;
1502218065Sadrian		ath_freetx(m0);
1503218065Sadrian		return EIO;
1504218065Sadrian	}
1505218065Sadrian#endif
1506218065Sadrian
1507218065Sadrian	/*
1508227364Sadrian	 * Determine if a tx interrupt should be generated for
1509227364Sadrian	 * this descriptor.  We take a tx interrupt to reap
1510227364Sadrian	 * descriptors when the h/w hits an EOL condition or
1511227364Sadrian	 * when the descriptor is specifically marked to generate
1512227364Sadrian	 * an interrupt.  We periodically mark descriptors in this
1513227364Sadrian	 * way to insure timely replenishing of the supply needed
1514227364Sadrian	 * for sending frames.  Defering interrupts reduces system
1515227364Sadrian	 * load and potentially allows more concurrent work to be
1516227364Sadrian	 * done but if done to aggressively can cause senders to
1517227364Sadrian	 * backup.
1518227364Sadrian	 *
1519227364Sadrian	 * NB: use >= to deal with sc_txintrperiod changing
1520227364Sadrian	 *     dynamically through sysctl.
1521218065Sadrian	 */
1522227364Sadrian	if (flags & HAL_TXDESC_INTREQ) {
1523227364Sadrian		txq->axq_intrcnt = 0;
1524227364Sadrian	} else if (++txq->axq_intrcnt >= sc->sc_txintrperiod) {
1525227364Sadrian		flags |= HAL_TXDESC_INTREQ;
1526227364Sadrian		txq->axq_intrcnt = 0;
1527227364Sadrian	}
1528218932Sadrian
1529227364Sadrian	/* This point forward is actual TX bits */
1530218065Sadrian
1531218065Sadrian	/*
1532218065Sadrian	 * At this point we are committed to sending the frame
1533218065Sadrian	 * and we don't need to look at m_nextpkt; clear it in
1534218065Sadrian	 * case this frame is part of frag chain.
1535218065Sadrian	 */
1536218065Sadrian	m0->m_nextpkt = NULL;
1537218065Sadrian
1538218065Sadrian	if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
1539218065Sadrian		ieee80211_dump_pkt(ic, mtod(m0, const uint8_t *), m0->m_len,
1540218065Sadrian		    sc->sc_hwmap[rix].ieeerate, -1);
1541218065Sadrian
1542218065Sadrian	if (ieee80211_radiotap_active_vap(vap)) {
1543218065Sadrian		u_int64_t tsf = ath_hal_gettsf64(ah);
1544218065Sadrian
1545218065Sadrian		sc->sc_tx_th.wt_tsf = htole64(tsf);
1546218065Sadrian		sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
1547218065Sadrian		if (iswep)
1548218065Sadrian			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
1549218065Sadrian		if (isfrag)
1550218065Sadrian			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
1551218065Sadrian		sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
1552218065Sadrian		sc->sc_tx_th.wt_txpower = ni->ni_txpower;
1553218065Sadrian		sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
1554218065Sadrian
1555218065Sadrian		ieee80211_radiotap_tx(vap, m0);
1556218065Sadrian	}
1557218065Sadrian
1558227364Sadrian	/* Blank the legacy rate array */
1559227364Sadrian	bzero(&bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
1560227364Sadrian
1561218065Sadrian	/*
1562227364Sadrian	 * ath_buf_set_rate needs at least one rate/try to setup
1563227364Sadrian	 * the rate scenario.
1564227364Sadrian	 */
1565227364Sadrian	bf->bf_state.bfs_rc[0].rix = rix;
1566227364Sadrian	bf->bf_state.bfs_rc[0].tries = try0;
1567227364Sadrian	bf->bf_state.bfs_rc[0].ratecode = txrate;
1568227364Sadrian
1569227364Sadrian	/* Store the decided rate index values away */
1570227364Sadrian	bf->bf_state.bfs_pktlen = pktlen;
1571227364Sadrian	bf->bf_state.bfs_hdrlen = hdrlen;
1572227364Sadrian	bf->bf_state.bfs_atype = atype;
1573227364Sadrian	bf->bf_state.bfs_txpower = ni->ni_txpower;
1574227364Sadrian	bf->bf_state.bfs_txrate0 = txrate;
1575227364Sadrian	bf->bf_state.bfs_try0 = try0;
1576227364Sadrian	bf->bf_state.bfs_keyix = keyix;
1577227364Sadrian	bf->bf_state.bfs_txantenna = sc->sc_txantenna;
1578233966Sadrian	bf->bf_state.bfs_txflags = flags;
1579227364Sadrian	bf->bf_state.bfs_shpream = shortPreamble;
1580227364Sadrian
1581227364Sadrian	/* XXX this should be done in ath_tx_setrate() */
1582227364Sadrian	bf->bf_state.bfs_ctsrate0 = 0;	/* ie, no hard-coded ctsrate */
1583227364Sadrian	bf->bf_state.bfs_ctsrate = 0;	/* calculated later */
1584227364Sadrian	bf->bf_state.bfs_ctsduration = 0;
1585227364Sadrian	bf->bf_state.bfs_ismrr = ismrr;
1586227364Sadrian
1587227364Sadrian	return 0;
1588227364Sadrian}
1589227364Sadrian
1590227364Sadrian/*
1591227364Sadrian * Direct-dispatch the current frame to the hardware.
1592227364Sadrian *
1593227364Sadrian * This can be called by the net80211 code.
1594227364Sadrian *
1595227364Sadrian * XXX what about locking? Or, push the seqno assign into the
1596227364Sadrian * XXX aggregate scheduler so its serialised?
1597227364Sadrian */
1598227364Sadrianint
1599227364Sadrianath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni,
1600227364Sadrian    struct ath_buf *bf, struct mbuf *m0)
1601227364Sadrian{
1602227364Sadrian	struct ieee80211vap *vap = ni->ni_vap;
1603227364Sadrian	struct ath_vap *avp = ATH_VAP(vap);
1604232764Sadrian	int r = 0;
1605227364Sadrian	u_int pri;
1606227364Sadrian	int tid;
1607227364Sadrian	struct ath_txq *txq;
1608227364Sadrian	int ismcast;
1609227364Sadrian	const struct ieee80211_frame *wh;
1610227364Sadrian	int is_ampdu, is_ampdu_tx, is_ampdu_pending;
1611236872Sadrian	ieee80211_seq seqno;
1612227364Sadrian	uint8_t type, subtype;
1613227364Sadrian
1614227364Sadrian	/*
1615227364Sadrian	 * Determine the target hardware queue.
1616218065Sadrian	 *
1617234009Sadrian	 * For multicast frames, the txq gets overridden appropriately
1618234009Sadrian	 * depending upon the state of PS.
1619227364Sadrian	 *
1620227364Sadrian	 * For any other frame, we do a TID/QoS lookup inside the frame
1621227364Sadrian	 * to see what the TID should be. If it's a non-QoS frame, the
1622227364Sadrian	 * AC and TID are overridden. The TID/TXQ code assumes the
1623227364Sadrian	 * TID is on a predictable hardware TXQ, so we don't support
1624227364Sadrian	 * having a node TID queued to multiple hardware TXQs.
1625227364Sadrian	 * This may change in the future but would require some locking
1626227364Sadrian	 * fudgery.
1627218065Sadrian	 */
1628227364Sadrian	pri = ath_tx_getac(sc, m0);
1629227364Sadrian	tid = ath_tx_gettid(sc, m0);
1630218065Sadrian
1631227364Sadrian	txq = sc->sc_ac2q[pri];
1632227364Sadrian	wh = mtod(m0, struct ieee80211_frame *);
1633227364Sadrian	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1634227364Sadrian	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1635227364Sadrian	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1636227364Sadrian
1637232764Sadrian	/*
1638232764Sadrian	 * Enforce how deep the multicast queue can grow.
1639232764Sadrian	 *
1640232764Sadrian	 * XXX duplicated in ath_raw_xmit().
1641232764Sadrian	 */
1642232764Sadrian	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1643232764Sadrian		ATH_TXQ_LOCK(sc->sc_cabq);
1644232764Sadrian
1645232794Sadrian		if (sc->sc_cabq->axq_depth > sc->sc_txq_mcastq_maxdepth) {
1646232764Sadrian			sc->sc_stats.ast_tx_mcastq_overflow++;
1647232764Sadrian			r = ENOBUFS;
1648232764Sadrian		}
1649232764Sadrian
1650232764Sadrian		ATH_TXQ_UNLOCK(sc->sc_cabq);
1651232764Sadrian
1652232764Sadrian		if (r != 0) {
1653232764Sadrian			m_freem(m0);
1654232764Sadrian			return r;
1655232764Sadrian		}
1656232764Sadrian	}
1657232764Sadrian
1658227364Sadrian	/* A-MPDU TX */
1659227364Sadrian	is_ampdu_tx = ath_tx_ampdu_running(sc, ATH_NODE(ni), tid);
1660227364Sadrian	is_ampdu_pending = ath_tx_ampdu_pending(sc, ATH_NODE(ni), tid);
1661227364Sadrian	is_ampdu = is_ampdu_tx | is_ampdu_pending;
1662227364Sadrian
1663236872Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d, ac=%d, is_ampdu=%d\n",
1664236872Sadrian	    __func__, tid, pri, is_ampdu);
1665227364Sadrian
1666239051Sadrian	/* Set local packet state, used to queue packets to hardware */
1667239051Sadrian	bf->bf_state.bfs_tid = tid;
1668239051Sadrian	bf->bf_state.bfs_txq = txq;
1669239051Sadrian	bf->bf_state.bfs_pri = pri;
1670239051Sadrian
1671232753Sadrian	/*
1672234009Sadrian	 * When servicing one or more stations in power-save mode
1673234009Sadrian	 * (or) if there is some mcast data waiting on the mcast
1674234009Sadrian	 * queue (to prevent out of order delivery) multicast frames
1675234009Sadrian	 * must be bufferd until after the beacon.
1676234009Sadrian	 *
1677234009Sadrian	 * TODO: we should lock the mcastq before we check the length.
1678232753Sadrian	 */
1679239051Sadrian	if (ismcast && (vap->iv_ps_sta || avp->av_mcastq.axq_depth)) {
1680227364Sadrian		txq = &avp->av_mcastq;
1681239051Sadrian		/*
1682239051Sadrian		 * Mark the frame as eventually belonging on the CAB
1683239051Sadrian		 * queue, so the descriptor setup functions will
1684239051Sadrian		 * correctly initialise the descriptor 'qcuId' field.
1685239051Sadrian		 */
1686239051Sadrian		bf->bf_state.bfs_txq = sc->sc_cabq;
1687239051Sadrian	}
1688227364Sadrian
1689227364Sadrian	/* Do the generic frame setup */
1690227364Sadrian	/* XXX should just bzero the bf_state? */
1691227364Sadrian	bf->bf_state.bfs_dobaw = 0;
1692227364Sadrian
1693236880Sadrian	/*
1694236880Sadrian	 * Acquire the TXQ lock early, so both the encap and seqno
1695236880Sadrian	 * are allocated together.
1696239051Sadrian	 *
1697239051Sadrian	 * XXX should TXQ for CABQ traffic be the multicast queue,
1698239051Sadrian	 * or the TXQ the given PRI would allocate from? (eg for
1699239051Sadrian	 * sequence number allocation locking.)
1700236880Sadrian	 */
1701236880Sadrian	ATH_TXQ_LOCK(txq);
1702236880Sadrian
1703227364Sadrian	/* A-MPDU TX? Manually set sequence number */
1704236880Sadrian	/*
1705236880Sadrian	 * Don't do it whilst pending; the net80211 layer still
1706236880Sadrian	 * assigns them.
1707236880Sadrian	 */
1708227364Sadrian	if (is_ampdu_tx) {
1709227364Sadrian		/*
1710227364Sadrian		 * Always call; this function will
1711227364Sadrian		 * handle making sure that null data frames
1712227364Sadrian		 * don't get a sequence number from the current
1713227364Sadrian		 * TID and thus mess with the BAW.
1714227364Sadrian		 */
1715236872Sadrian		seqno = ath_tx_tid_seqno_assign(sc, ni, bf, m0);
1716236874Sadrian
1717236874Sadrian		/*
1718236874Sadrian		 * Don't add QoS NULL frames to the BAW.
1719236874Sadrian		 */
1720236872Sadrian		if (IEEE80211_QOS_HAS_SEQ(wh) &&
1721236872Sadrian		    subtype != IEEE80211_FC0_SUBTYPE_QOS_NULL) {
1722227364Sadrian			bf->bf_state.bfs_dobaw = 1;
1723227364Sadrian		}
1724218240Sadrian	}
1725218240Sadrian
1726218065Sadrian	/*
1727227364Sadrian	 * If needed, the sequence number has been assigned.
1728227364Sadrian	 * Squirrel it away somewhere easy to get to.
1729218065Sadrian	 */
1730236872Sadrian	bf->bf_state.bfs_seqno = M_SEQNO_GET(m0) << IEEE80211_SEQ_SEQ_SHIFT;
1731227364Sadrian
1732227364Sadrian	/* Is ampdu pending? fetch the seqno and print it out */
1733227364Sadrian	if (is_ampdu_pending)
1734227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX,
1735227364Sadrian		    "%s: tid %d: ampdu pending, seqno %d\n",
1736227364Sadrian		    __func__, tid, M_SEQNO_GET(m0));
1737227364Sadrian
1738227364Sadrian	/* This also sets up the DMA map */
1739234009Sadrian	r = ath_tx_normal_setup(sc, ni, bf, m0, txq);
1740227364Sadrian
1741227364Sadrian	if (r != 0)
1742236880Sadrian		goto done;
1743227364Sadrian
1744227364Sadrian	/* At this point m0 could have changed! */
1745227364Sadrian	m0 = bf->bf_m;
1746227364Sadrian
1747227364Sadrian#if 1
1748218065Sadrian	/*
1749227364Sadrian	 * If it's a multicast frame, do a direct-dispatch to the
1750227364Sadrian	 * destination hardware queue. Don't bother software
1751227364Sadrian	 * queuing it.
1752218065Sadrian	 */
1753227364Sadrian	/*
1754227364Sadrian	 * If it's a BAR frame, do a direct dispatch to the
1755227364Sadrian	 * destination hardware queue. Don't bother software
1756227364Sadrian	 * queuing it, as the TID will now be paused.
1757227364Sadrian	 * Sending a BAR frame can occur from the net80211 txa timer
1758227364Sadrian	 * (ie, retries) or from the ath txtask (completion call.)
1759227364Sadrian	 * It queues directly to hardware because the TID is paused
1760227364Sadrian	 * at this point (and won't be unpaused until the BAR has
1761227364Sadrian	 * either been TXed successfully or max retries has been
1762227364Sadrian	 * reached.)
1763227364Sadrian	 */
1764227364Sadrian	if (txq == &avp->av_mcastq) {
1765235774Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX,
1766233227Sadrian		    "%s: bf=%p: mcastq: TX'ing\n", __func__, bf);
1767227364Sadrian		ath_tx_xmit_normal(sc, txq, bf);
1768227364Sadrian	} else if (type == IEEE80211_FC0_TYPE_CTL &&
1769227364Sadrian		    subtype == IEEE80211_FC0_SUBTYPE_BAR) {
1770235774Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX,
1771227364Sadrian		    "%s: BAR: TX'ing direct\n", __func__);
1772227364Sadrian		ath_tx_xmit_normal(sc, txq, bf);
1773227364Sadrian	} else {
1774227364Sadrian		/* add to software queue */
1775235774Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX,
1776233227Sadrian		    "%s: bf=%p: swq: TX'ing\n", __func__, bf);
1777227364Sadrian		ath_tx_swq(sc, ni, txq, bf);
1778227364Sadrian	}
1779227364Sadrian#else
1780227364Sadrian	/*
1781227364Sadrian	 * For now, since there's no software queue,
1782227364Sadrian	 * direct-dispatch to the hardware.
1783227364Sadrian	 */
1784227364Sadrian	ath_tx_xmit_normal(sc, txq, bf);
1785236880Sadrian#endif
1786236880Sadriandone:
1787227364Sadrian	ATH_TXQ_UNLOCK(txq);
1788218065Sadrian
1789218065Sadrian	return 0;
1790218065Sadrian}
1791218065Sadrian
1792218065Sadrianstatic int
1793218065Sadrianath_tx_raw_start(struct ath_softc *sc, struct ieee80211_node *ni,
1794218065Sadrian	struct ath_buf *bf, struct mbuf *m0,
1795218065Sadrian	const struct ieee80211_bpf_params *params)
1796218065Sadrian{
1797218065Sadrian	struct ifnet *ifp = sc->sc_ifp;
1798218065Sadrian	struct ieee80211com *ic = ifp->if_l2com;
1799218065Sadrian	struct ath_hal *ah = sc->sc_ah;
1800218065Sadrian	struct ieee80211vap *vap = ni->ni_vap;
1801218065Sadrian	int error, ismcast, ismrr;
1802218065Sadrian	int keyix, hdrlen, pktlen, try0, txantenna;
1803227364Sadrian	u_int8_t rix, txrate;
1804218065Sadrian	struct ieee80211_frame *wh;
1805227364Sadrian	u_int flags;
1806218065Sadrian	HAL_PKT_TYPE atype;
1807218065Sadrian	const HAL_RATE_TABLE *rt;
1808218065Sadrian	struct ath_desc *ds;
1809218065Sadrian	u_int pri;
1810227364Sadrian	int o_tid = -1;
1811227364Sadrian	int do_override;
1812218065Sadrian
1813218065Sadrian	wh = mtod(m0, struct ieee80211_frame *);
1814218065Sadrian	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1815218065Sadrian	hdrlen = ieee80211_anyhdrsize(wh);
1816218065Sadrian	/*
1817218065Sadrian	 * Packet length must not include any
1818218065Sadrian	 * pad bytes; deduct them here.
1819218065Sadrian	 */
1820218065Sadrian	/* XXX honor IEEE80211_BPF_DATAPAD */
1821218065Sadrian	pktlen = m0->m_pkthdr.len - (hdrlen & 3) + IEEE80211_CRC_LEN;
1822218065Sadrian
1823227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: ismcast=%d\n",
1824227364Sadrian	    __func__, ismcast);
1825227364Sadrian
1826236880Sadrian	pri = params->ibp_pri & 3;
1827236880Sadrian	/* Override pri if the frame isn't a QoS one */
1828236880Sadrian	if (! IEEE80211_QOS_HAS_SEQ(wh))
1829236880Sadrian		pri = ath_tx_getac(sc, m0);
1830236880Sadrian
1831236880Sadrian	/* XXX If it's an ADDBA, override the correct queue */
1832236880Sadrian	do_override = ath_tx_action_frame_override_queue(sc, ni, m0, &o_tid);
1833236880Sadrian
1834236880Sadrian	/* Map ADDBA to the correct priority */
1835236880Sadrian	if (do_override) {
1836236880Sadrian#if 0
1837236880Sadrian		device_printf(sc->sc_dev,
1838236880Sadrian		    "%s: overriding tid %d pri %d -> %d\n",
1839236880Sadrian		    __func__, o_tid, pri, TID_TO_WME_AC(o_tid));
1840236880Sadrian#endif
1841236880Sadrian		pri = TID_TO_WME_AC(o_tid);
1842236880Sadrian	}
1843236880Sadrian
1844236880Sadrian	ATH_TXQ_LOCK(sc->sc_ac2q[pri]);
1845236880Sadrian
1846218154Sadrian	/* Handle encryption twiddling if needed */
1847227364Sadrian	if (! ath_tx_tag_crypto(sc, ni,
1848227364Sadrian	    m0, params->ibp_flags & IEEE80211_BPF_CRYPTO, 0,
1849227364Sadrian	    &hdrlen, &pktlen, &keyix)) {
1850218154Sadrian		ath_freetx(m0);
1851218154Sadrian		return EIO;
1852218154Sadrian	}
1853218154Sadrian	/* packet header may have moved, reset our local pointer */
1854218154Sadrian	wh = mtod(m0, struct ieee80211_frame *);
1855218065Sadrian
1856227364Sadrian	/* Do the generic frame setup */
1857227364Sadrian	/* XXX should just bzero the bf_state? */
1858227364Sadrian	bf->bf_state.bfs_dobaw = 0;
1859227364Sadrian
1860218065Sadrian	error = ath_tx_dmasetup(sc, bf, m0);
1861218065Sadrian	if (error != 0)
1862218065Sadrian		return error;
1863218065Sadrian	m0 = bf->bf_m;				/* NB: may have changed */
1864218065Sadrian	wh = mtod(m0, struct ieee80211_frame *);
1865218065Sadrian	bf->bf_node = ni;			/* NB: held reference */
1866218065Sadrian
1867218065Sadrian	flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for crypto errs */
1868218065Sadrian	flags |= HAL_TXDESC_INTREQ;		/* force interrupt */
1869218065Sadrian	if (params->ibp_flags & IEEE80211_BPF_RTS)
1870218065Sadrian		flags |= HAL_TXDESC_RTSENA;
1871227364Sadrian	else if (params->ibp_flags & IEEE80211_BPF_CTS) {
1872227364Sadrian		/* XXX assume 11g/11n protection? */
1873227364Sadrian		bf->bf_state.bfs_doprot = 1;
1874218065Sadrian		flags |= HAL_TXDESC_CTSENA;
1875227364Sadrian	}
1876218065Sadrian	/* XXX leave ismcast to injector? */
1877218065Sadrian	if ((params->ibp_flags & IEEE80211_BPF_NOACK) || ismcast)
1878218065Sadrian		flags |= HAL_TXDESC_NOACK;
1879218065Sadrian
1880218065Sadrian	rt = sc->sc_currates;
1881218065Sadrian	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
1882218065Sadrian	rix = ath_tx_findrix(sc, params->ibp_rate0);
1883218065Sadrian	txrate = rt->info[rix].rateCode;
1884218065Sadrian	if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
1885218065Sadrian		txrate |= rt->info[rix].shortPreamble;
1886218065Sadrian	sc->sc_txrix = rix;
1887218065Sadrian	try0 = params->ibp_try0;
1888218065Sadrian	ismrr = (params->ibp_try1 != 0);
1889218065Sadrian	txantenna = params->ibp_pri >> 2;
1890218065Sadrian	if (txantenna == 0)			/* XXX? */
1891218065Sadrian		txantenna = sc->sc_txantenna;
1892218157Sadrian
1893227364Sadrian	/*
1894227364Sadrian	 * Since ctsrate is fixed, store it away for later
1895227364Sadrian	 * use when the descriptor fields are being set.
1896227364Sadrian	 */
1897227364Sadrian	if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA))
1898227364Sadrian		bf->bf_state.bfs_ctsrate0 = params->ibp_ctsrate;
1899218157Sadrian
1900218065Sadrian	/*
1901218065Sadrian	 * NB: we mark all packets as type PSPOLL so the h/w won't
1902218065Sadrian	 * set the sequence number, duration, etc.
1903218065Sadrian	 */
1904218065Sadrian	atype = HAL_PKT_TYPE_PSPOLL;
1905218065Sadrian
1906218065Sadrian	if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
1907218065Sadrian		ieee80211_dump_pkt(ic, mtod(m0, caddr_t), m0->m_len,
1908218065Sadrian		    sc->sc_hwmap[rix].ieeerate, -1);
1909218065Sadrian
1910218065Sadrian	if (ieee80211_radiotap_active_vap(vap)) {
1911218065Sadrian		u_int64_t tsf = ath_hal_gettsf64(ah);
1912218065Sadrian
1913218065Sadrian		sc->sc_tx_th.wt_tsf = htole64(tsf);
1914218065Sadrian		sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
1915218065Sadrian		if (wh->i_fc[1] & IEEE80211_FC1_WEP)
1916218065Sadrian			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
1917218065Sadrian		if (m0->m_flags & M_FRAG)
1918218065Sadrian			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
1919218065Sadrian		sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
1920218065Sadrian		sc->sc_tx_th.wt_txpower = ni->ni_txpower;
1921218065Sadrian		sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
1922218065Sadrian
1923218065Sadrian		ieee80211_radiotap_tx(vap, m0);
1924218065Sadrian	}
1925218065Sadrian
1926218065Sadrian	/*
1927218065Sadrian	 * Formulate first tx descriptor with tx controls.
1928218065Sadrian	 */
1929218065Sadrian	ds = bf->bf_desc;
1930218065Sadrian	/* XXX check return value? */
1931227364Sadrian
1932227364Sadrian	/* Store the decided rate index values away */
1933227364Sadrian	bf->bf_state.bfs_pktlen = pktlen;
1934227364Sadrian	bf->bf_state.bfs_hdrlen = hdrlen;
1935227364Sadrian	bf->bf_state.bfs_atype = atype;
1936227364Sadrian	bf->bf_state.bfs_txpower = params->ibp_power;
1937227364Sadrian	bf->bf_state.bfs_txrate0 = txrate;
1938227364Sadrian	bf->bf_state.bfs_try0 = try0;
1939227364Sadrian	bf->bf_state.bfs_keyix = keyix;
1940227364Sadrian	bf->bf_state.bfs_txantenna = txantenna;
1941233966Sadrian	bf->bf_state.bfs_txflags = flags;
1942227364Sadrian	bf->bf_state.bfs_shpream =
1943227364Sadrian	    !! (params->ibp_flags & IEEE80211_BPF_SHORTPRE);
1944218065Sadrian
1945239051Sadrian	/* Set local packet state, used to queue packets to hardware */
1946239051Sadrian	bf->bf_state.bfs_tid = WME_AC_TO_TID(pri);
1947239051Sadrian	bf->bf_state.bfs_txq = sc->sc_ac2q[pri];
1948239051Sadrian	bf->bf_state.bfs_pri = pri;
1949239051Sadrian
1950227364Sadrian	/* XXX this should be done in ath_tx_setrate() */
1951227364Sadrian	bf->bf_state.bfs_ctsrate = 0;
1952227364Sadrian	bf->bf_state.bfs_ctsduration = 0;
1953227364Sadrian	bf->bf_state.bfs_ismrr = ismrr;
1954218240Sadrian
1955227364Sadrian	/* Blank the legacy rate array */
1956227364Sadrian	bzero(&bf->bf_state.bfs_rc, sizeof(bf->bf_state.bfs_rc));
1957218240Sadrian
1958227364Sadrian	bf->bf_state.bfs_rc[0].rix =
1959227364Sadrian	    ath_tx_findrix(sc, params->ibp_rate0);
1960227364Sadrian	bf->bf_state.bfs_rc[0].tries = try0;
1961227364Sadrian	bf->bf_state.bfs_rc[0].ratecode = txrate;
1962227364Sadrian
1963227364Sadrian	if (ismrr) {
1964227364Sadrian		int rix;
1965227364Sadrian
1966227364Sadrian		rix = ath_tx_findrix(sc, params->ibp_rate1);
1967227364Sadrian		bf->bf_state.bfs_rc[1].rix = rix;
1968227364Sadrian		bf->bf_state.bfs_rc[1].tries = params->ibp_try1;
1969227364Sadrian
1970227364Sadrian		rix = ath_tx_findrix(sc, params->ibp_rate2);
1971227364Sadrian		bf->bf_state.bfs_rc[2].rix = rix;
1972227364Sadrian		bf->bf_state.bfs_rc[2].tries = params->ibp_try2;
1973227364Sadrian
1974227364Sadrian		rix = ath_tx_findrix(sc, params->ibp_rate3);
1975227364Sadrian		bf->bf_state.bfs_rc[3].rix = rix;
1976227364Sadrian		bf->bf_state.bfs_rc[3].tries = params->ibp_try3;
1977218065Sadrian	}
1978227364Sadrian	/*
1979227364Sadrian	 * All the required rate control decisions have been made;
1980227364Sadrian	 * fill in the rc flags.
1981227364Sadrian	 */
1982227364Sadrian	ath_tx_rate_fill_rcflags(sc, bf);
1983218065Sadrian
1984227364Sadrian	/* NB: no buffered multicast in power save support */
1985227364Sadrian
1986227364Sadrian	/*
1987227364Sadrian	 * If we're overiding the ADDBA destination, dump directly
1988227364Sadrian	 * into the hardware queue, right after any pending
1989227364Sadrian	 * frames to that node are.
1990227364Sadrian	 */
1991227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: dooverride=%d\n",
1992227364Sadrian	    __func__, do_override);
1993227364Sadrian
1994227364Sadrian	if (do_override) {
1995227364Sadrian		ath_tx_xmit_normal(sc, sc->sc_ac2q[pri], bf);
1996227364Sadrian	} else {
1997227364Sadrian		/* Queue to software queue */
1998227364Sadrian		ath_tx_swq(sc, ni, sc->sc_ac2q[pri], bf);
1999227364Sadrian	}
2000236880Sadrian	ATH_TXQ_UNLOCK(sc->sc_ac2q[pri]);
2001227364Sadrian
2002218065Sadrian	return 0;
2003218065Sadrian}
2004218065Sadrian
2005227364Sadrian/*
2006227364Sadrian * Send a raw frame.
2007227364Sadrian *
2008227364Sadrian * This can be called by net80211.
2009227364Sadrian */
2010218065Sadrianint
2011218065Sadrianath_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2012218065Sadrian	const struct ieee80211_bpf_params *params)
2013218065Sadrian{
2014218065Sadrian	struct ieee80211com *ic = ni->ni_ic;
2015218065Sadrian	struct ifnet *ifp = ic->ic_ifp;
2016218065Sadrian	struct ath_softc *sc = ifp->if_softc;
2017218065Sadrian	struct ath_buf *bf;
2018232764Sadrian	struct ieee80211_frame *wh = mtod(m, struct ieee80211_frame *);
2019232764Sadrian	int error = 0;
2020218065Sadrian
2021227651Sadrian	ATH_PCU_LOCK(sc);
2022227651Sadrian	if (sc->sc_inreset_cnt > 0) {
2023227651Sadrian		device_printf(sc->sc_dev, "%s: sc_inreset_cnt > 0; bailing\n",
2024227651Sadrian		    __func__);
2025227651Sadrian		error = EIO;
2026227651Sadrian		ATH_PCU_UNLOCK(sc);
2027227651Sadrian		goto bad0;
2028227651Sadrian	}
2029227651Sadrian	sc->sc_txstart_cnt++;
2030227651Sadrian	ATH_PCU_UNLOCK(sc);
2031227651Sadrian
2032218065Sadrian	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->sc_invalid) {
2033218065Sadrian		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: discard frame, %s", __func__,
2034218065Sadrian		    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ?
2035218065Sadrian			"!running" : "invalid");
2036218065Sadrian		m_freem(m);
2037218065Sadrian		error = ENETDOWN;
2038218065Sadrian		goto bad;
2039218065Sadrian	}
2040232764Sadrian
2041218065Sadrian	/*
2042232764Sadrian	 * Enforce how deep the multicast queue can grow.
2043232764Sadrian	 *
2044232764Sadrian	 * XXX duplicated in ath_tx_start().
2045232764Sadrian	 */
2046232764Sadrian	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2047232764Sadrian		ATH_TXQ_LOCK(sc->sc_cabq);
2048232764Sadrian
2049232794Sadrian		if (sc->sc_cabq->axq_depth > sc->sc_txq_mcastq_maxdepth) {
2050232764Sadrian			sc->sc_stats.ast_tx_mcastq_overflow++;
2051232764Sadrian			error = ENOBUFS;
2052232764Sadrian		}
2053232764Sadrian
2054232764Sadrian		ATH_TXQ_UNLOCK(sc->sc_cabq);
2055232764Sadrian
2056232764Sadrian		if (error != 0) {
2057232764Sadrian			m_freem(m);
2058232764Sadrian			goto bad;
2059232764Sadrian		}
2060232764Sadrian	}
2061232764Sadrian
2062232764Sadrian	/*
2063218065Sadrian	 * Grab a TX buffer and associated resources.
2064218065Sadrian	 */
2065237000Sadrian	bf = ath_getbuf(sc, ATH_BUFTYPE_MGMT);
2066218065Sadrian	if (bf == NULL) {
2067218065Sadrian		sc->sc_stats.ast_tx_nobuf++;
2068218065Sadrian		m_freem(m);
2069218065Sadrian		error = ENOBUFS;
2070218065Sadrian		goto bad;
2071218065Sadrian	}
2072218065Sadrian
2073218065Sadrian	if (params == NULL) {
2074218065Sadrian		/*
2075218065Sadrian		 * Legacy path; interpret frame contents to decide
2076218065Sadrian		 * precisely how to send the frame.
2077218065Sadrian		 */
2078218065Sadrian		if (ath_tx_start(sc, ni, bf, m)) {
2079218065Sadrian			error = EIO;		/* XXX */
2080218065Sadrian			goto bad2;
2081218065Sadrian		}
2082218065Sadrian	} else {
2083218065Sadrian		/*
2084218065Sadrian		 * Caller supplied explicit parameters to use in
2085218065Sadrian		 * sending the frame.
2086218065Sadrian		 */
2087218065Sadrian		if (ath_tx_raw_start(sc, ni, bf, m, params)) {
2088218065Sadrian			error = EIO;		/* XXX */
2089218065Sadrian			goto bad2;
2090218065Sadrian		}
2091218065Sadrian	}
2092218065Sadrian	sc->sc_wd_timer = 5;
2093218065Sadrian	ifp->if_opackets++;
2094218065Sadrian	sc->sc_stats.ast_tx_raw++;
2095218065Sadrian
2096227651Sadrian	ATH_PCU_LOCK(sc);
2097227651Sadrian	sc->sc_txstart_cnt--;
2098227651Sadrian	ATH_PCU_UNLOCK(sc);
2099227651Sadrian
2100218065Sadrian	return 0;
2101218065Sadrianbad2:
2102218065Sadrian	ATH_TXBUF_LOCK(sc);
2103236993Sadrian	ath_returnbuf_head(sc, bf);
2104218065Sadrian	ATH_TXBUF_UNLOCK(sc);
2105218065Sadrianbad:
2106227651Sadrian	ATH_PCU_LOCK(sc);
2107227651Sadrian	sc->sc_txstart_cnt--;
2108227651Sadrian	ATH_PCU_UNLOCK(sc);
2109227651Sadrianbad0:
2110218065Sadrian	ifp->if_oerrors++;
2111218065Sadrian	sc->sc_stats.ast_tx_raw_fail++;
2112218065Sadrian	ieee80211_free_node(ni);
2113227651Sadrian
2114218065Sadrian	return error;
2115218065Sadrian}
2116227364Sadrian
2117227364Sadrian/* Some helper functions */
2118227364Sadrian
2119227364Sadrian/*
2120227364Sadrian * ADDBA (and potentially others) need to be placed in the same
2121227364Sadrian * hardware queue as the TID/node it's relating to. This is so
2122227364Sadrian * it goes out after any pending non-aggregate frames to the
2123227364Sadrian * same node/TID.
2124227364Sadrian *
2125227364Sadrian * If this isn't done, the ADDBA can go out before the frames
2126227364Sadrian * queued in hardware. Even though these frames have a sequence
2127227364Sadrian * number -earlier- than the ADDBA can be transmitted (but
2128227364Sadrian * no frames whose sequence numbers are after the ADDBA should
2129227364Sadrian * be!) they'll arrive after the ADDBA - and the receiving end
2130227364Sadrian * will simply drop them as being out of the BAW.
2131227364Sadrian *
2132227364Sadrian * The frames can't be appended to the TID software queue - it'll
2133227364Sadrian * never be sent out. So these frames have to be directly
2134227364Sadrian * dispatched to the hardware, rather than queued in software.
2135227364Sadrian * So if this function returns true, the TXQ has to be
2136227364Sadrian * overridden and it has to be directly dispatched.
2137227364Sadrian *
2138227364Sadrian * It's a dirty hack, but someone's gotta do it.
2139227364Sadrian */
2140227364Sadrian
2141227364Sadrian/*
2142227364Sadrian * XXX doesn't belong here!
2143227364Sadrian */
2144227364Sadrianstatic int
2145227364Sadrianieee80211_is_action(struct ieee80211_frame *wh)
2146227364Sadrian{
2147227364Sadrian	/* Type: Management frame? */
2148227364Sadrian	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) !=
2149227364Sadrian	    IEEE80211_FC0_TYPE_MGT)
2150227364Sadrian		return 0;
2151227364Sadrian
2152227364Sadrian	/* Subtype: Action frame? */
2153227364Sadrian	if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) !=
2154227364Sadrian	    IEEE80211_FC0_SUBTYPE_ACTION)
2155227364Sadrian		return 0;
2156227364Sadrian
2157227364Sadrian	return 1;
2158227364Sadrian}
2159227364Sadrian
2160227364Sadrian#define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
2161227364Sadrian/*
2162227364Sadrian * Return an alternate TID for ADDBA request frames.
2163227364Sadrian *
2164227364Sadrian * Yes, this likely should be done in the net80211 layer.
2165227364Sadrian */
2166227364Sadrianstatic int
2167227364Sadrianath_tx_action_frame_override_queue(struct ath_softc *sc,
2168227364Sadrian    struct ieee80211_node *ni,
2169227364Sadrian    struct mbuf *m0, int *tid)
2170227364Sadrian{
2171227364Sadrian	struct ieee80211_frame *wh = mtod(m0, struct ieee80211_frame *);
2172227364Sadrian	struct ieee80211_action_ba_addbarequest *ia;
2173227364Sadrian	uint8_t *frm;
2174227364Sadrian	uint16_t baparamset;
2175227364Sadrian
2176227364Sadrian	/* Not action frame? Bail */
2177227364Sadrian	if (! ieee80211_is_action(wh))
2178227364Sadrian		return 0;
2179227364Sadrian
2180227364Sadrian	/* XXX Not needed for frames we send? */
2181227364Sadrian#if 0
2182227364Sadrian	/* Correct length? */
2183227364Sadrian	if (! ieee80211_parse_action(ni, m))
2184227364Sadrian		return 0;
2185227364Sadrian#endif
2186227364Sadrian
2187227364Sadrian	/* Extract out action frame */
2188227364Sadrian	frm = (u_int8_t *)&wh[1];
2189227364Sadrian	ia = (struct ieee80211_action_ba_addbarequest *) frm;
2190227364Sadrian
2191227364Sadrian	/* Not ADDBA? Bail */
2192227364Sadrian	if (ia->rq_header.ia_category != IEEE80211_ACTION_CAT_BA)
2193227364Sadrian		return 0;
2194227364Sadrian	if (ia->rq_header.ia_action != IEEE80211_ACTION_BA_ADDBA_REQUEST)
2195227364Sadrian		return 0;
2196227364Sadrian
2197227364Sadrian	/* Extract TID, return it */
2198227364Sadrian	baparamset = le16toh(ia->rq_baparamset);
2199227364Sadrian	*tid = (int) MS(baparamset, IEEE80211_BAPS_TID);
2200227364Sadrian
2201227364Sadrian	return 1;
2202227364Sadrian}
2203227364Sadrian#undef	MS
2204227364Sadrian
2205227364Sadrian/* Per-node software queue operations */
2206227364Sadrian
2207227364Sadrian/*
2208227364Sadrian * Add the current packet to the given BAW.
2209227364Sadrian * It is assumed that the current packet
2210227364Sadrian *
2211227364Sadrian * + fits inside the BAW;
2212227364Sadrian * + already has had a sequence number allocated.
2213227364Sadrian *
2214227364Sadrian * Since the BAW status may be modified by both the ath task and
2215227364Sadrian * the net80211/ifnet contexts, the TID must be locked.
2216227364Sadrian */
2217227364Sadrianvoid
2218227364Sadrianath_tx_addto_baw(struct ath_softc *sc, struct ath_node *an,
2219227364Sadrian    struct ath_tid *tid, struct ath_buf *bf)
2220227364Sadrian{
2221227364Sadrian	int index, cindex;
2222227364Sadrian	struct ieee80211_tx_ampdu *tap;
2223227364Sadrian
2224227364Sadrian	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
2225236873Sadrian	ATH_TID_LOCK_ASSERT(sc, tid);
2226227364Sadrian
2227227364Sadrian	if (bf->bf_state.bfs_isretried)
2228227364Sadrian		return;
2229227364Sadrian
2230236886Sadrian	tap = ath_tx_get_tx_tid(an, tid->tid);
2231236886Sadrian
2232236880Sadrian	if (! bf->bf_state.bfs_dobaw) {
2233236880Sadrian		device_printf(sc->sc_dev,
2234236880Sadrian		    "%s: dobaw=0, seqno=%d, window %d:%d\n",
2235236880Sadrian		    __func__,
2236236880Sadrian		    SEQNO(bf->bf_state.bfs_seqno),
2237236880Sadrian		    tap->txa_start,
2238236880Sadrian		    tap->txa_wnd);
2239236880Sadrian	}
2240236880Sadrian
2241227364Sadrian	if (bf->bf_state.bfs_addedbaw)
2242227364Sadrian		device_printf(sc->sc_dev,
2243236872Sadrian		    "%s: re-added? tid=%d, seqno %d; window %d:%d; "
2244229949Sadrian		    "baw head=%d tail=%d\n",
2245236872Sadrian		    __func__, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
2246229949Sadrian		    tap->txa_start, tap->txa_wnd, tid->baw_head,
2247229949Sadrian		    tid->baw_tail);
2248227364Sadrian
2249227364Sadrian	/*
2250236880Sadrian	 * Verify that the given sequence number is not outside of the
2251236880Sadrian	 * BAW.  Complain loudly if that's the case.
2252236880Sadrian	 */
2253236880Sadrian	if (! BAW_WITHIN(tap->txa_start, tap->txa_wnd,
2254236880Sadrian	    SEQNO(bf->bf_state.bfs_seqno))) {
2255236880Sadrian		device_printf(sc->sc_dev,
2256236880Sadrian		    "%s: bf=%p: outside of BAW?? tid=%d, seqno %d; window %d:%d; "
2257236880Sadrian		    "baw head=%d tail=%d\n",
2258236880Sadrian		    __func__, bf, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
2259236880Sadrian		    tap->txa_start, tap->txa_wnd, tid->baw_head,
2260236880Sadrian		    tid->baw_tail);
2261236880Sadrian	}
2262236880Sadrian
2263236880Sadrian	/*
2264227364Sadrian	 * ni->ni_txseqs[] is the currently allocated seqno.
2265227364Sadrian	 * the txa state contains the current baw start.
2266227364Sadrian	 */
2267227364Sadrian	index  = ATH_BA_INDEX(tap->txa_start, SEQNO(bf->bf_state.bfs_seqno));
2268227364Sadrian	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
2269227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2270236872Sadrian	    "%s: tid=%d, seqno %d; window %d:%d; index=%d cindex=%d "
2271229949Sadrian	    "baw head=%d tail=%d\n",
2272236872Sadrian	    __func__, tid->tid, SEQNO(bf->bf_state.bfs_seqno),
2273229949Sadrian	    tap->txa_start, tap->txa_wnd, index, cindex, tid->baw_head,
2274229949Sadrian	    tid->baw_tail);
2275227364Sadrian
2276227364Sadrian
2277227364Sadrian#if 0
2278227364Sadrian	assert(tid->tx_buf[cindex] == NULL);
2279227364Sadrian#endif
2280227364Sadrian	if (tid->tx_buf[cindex] != NULL) {
2281227364Sadrian		device_printf(sc->sc_dev,
2282227364Sadrian		    "%s: ba packet dup (index=%d, cindex=%d, "
2283227364Sadrian		    "head=%d, tail=%d)\n",
2284227364Sadrian		    __func__, index, cindex, tid->baw_head, tid->baw_tail);
2285227364Sadrian		device_printf(sc->sc_dev,
2286227364Sadrian		    "%s: BA bf: %p; seqno=%d ; new bf: %p; seqno=%d\n",
2287227364Sadrian		    __func__,
2288227364Sadrian		    tid->tx_buf[cindex],
2289227364Sadrian		    SEQNO(tid->tx_buf[cindex]->bf_state.bfs_seqno),
2290227364Sadrian		    bf,
2291227364Sadrian		    SEQNO(bf->bf_state.bfs_seqno)
2292227364Sadrian		);
2293227364Sadrian	}
2294227364Sadrian	tid->tx_buf[cindex] = bf;
2295227364Sadrian
2296229949Sadrian	if (index >= ((tid->baw_tail - tid->baw_head) &
2297229949Sadrian	    (ATH_TID_MAX_BUFS - 1))) {
2298227364Sadrian		tid->baw_tail = cindex;
2299227364Sadrian		INCR(tid->baw_tail, ATH_TID_MAX_BUFS);
2300227364Sadrian	}
2301227364Sadrian}
2302227364Sadrian
2303227364Sadrian/*
2304227398Sadrian * Flip the BAW buffer entry over from the existing one to the new one.
2305227398Sadrian *
2306227398Sadrian * When software retransmitting a (sub-)frame, it is entirely possible that
2307227398Sadrian * the frame ath_buf is marked as BUSY and can't be immediately reused.
2308227398Sadrian * In that instance the buffer is cloned and the new buffer is used for
2309227398Sadrian * retransmit. We thus need to update the ath_buf slot in the BAW buf
2310227398Sadrian * tracking array to maintain consistency.
2311227398Sadrian */
2312227398Sadrianstatic void
2313227398Sadrianath_tx_switch_baw_buf(struct ath_softc *sc, struct ath_node *an,
2314227398Sadrian    struct ath_tid *tid, struct ath_buf *old_bf, struct ath_buf *new_bf)
2315227398Sadrian{
2316227398Sadrian	int index, cindex;
2317227398Sadrian	struct ieee80211_tx_ampdu *tap;
2318227398Sadrian	int seqno = SEQNO(old_bf->bf_state.bfs_seqno);
2319227398Sadrian
2320227398Sadrian	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
2321236873Sadrian	ATH_TID_LOCK_ASSERT(sc, tid);
2322227398Sadrian
2323227398Sadrian	tap = ath_tx_get_tx_tid(an, tid->tid);
2324227398Sadrian	index  = ATH_BA_INDEX(tap->txa_start, seqno);
2325227398Sadrian	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
2326227398Sadrian
2327227398Sadrian	/*
2328227398Sadrian	 * Just warn for now; if it happens then we should find out
2329227398Sadrian	 * about it. It's highly likely the aggregation session will
2330227398Sadrian	 * soon hang.
2331227398Sadrian	 */
2332227398Sadrian	if (old_bf->bf_state.bfs_seqno != new_bf->bf_state.bfs_seqno) {
2333227398Sadrian		device_printf(sc->sc_dev, "%s: retransmitted buffer"
2334227398Sadrian		    " has mismatching seqno's, BA session may hang.\n",
2335227398Sadrian		    __func__);
2336227398Sadrian		device_printf(sc->sc_dev, "%s: old seqno=%d, new_seqno=%d\n",
2337227398Sadrian		    __func__,
2338227398Sadrian		    old_bf->bf_state.bfs_seqno,
2339227398Sadrian		    new_bf->bf_state.bfs_seqno);
2340227398Sadrian	}
2341227398Sadrian
2342227398Sadrian	if (tid->tx_buf[cindex] != old_bf) {
2343227398Sadrian		device_printf(sc->sc_dev, "%s: ath_buf pointer incorrect; "
2344227398Sadrian		    " has m BA session may hang.\n",
2345227398Sadrian		    __func__);
2346227398Sadrian		device_printf(sc->sc_dev, "%s: old bf=%p, new bf=%p\n",
2347227398Sadrian		    __func__,
2348227398Sadrian		    old_bf, new_bf);
2349227398Sadrian	}
2350227398Sadrian
2351227398Sadrian	tid->tx_buf[cindex] = new_bf;
2352227398Sadrian}
2353227398Sadrian
2354227398Sadrian/*
2355227364Sadrian * seq_start - left edge of BAW
2356227364Sadrian * seq_next - current/next sequence number to allocate
2357227364Sadrian *
2358227364Sadrian * Since the BAW status may be modified by both the ath task and
2359227364Sadrian * the net80211/ifnet contexts, the TID must be locked.
2360227364Sadrian */
2361227364Sadrianstatic void
2362227364Sadrianath_tx_update_baw(struct ath_softc *sc, struct ath_node *an,
2363227364Sadrian    struct ath_tid *tid, const struct ath_buf *bf)
2364227364Sadrian{
2365227364Sadrian	int index, cindex;
2366227364Sadrian	struct ieee80211_tx_ampdu *tap;
2367227364Sadrian	int seqno = SEQNO(bf->bf_state.bfs_seqno);
2368227364Sadrian
2369227364Sadrian	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
2370236879Sadrian	ATH_TID_LOCK_ASSERT(sc, tid);
2371227364Sadrian
2372227364Sadrian	tap = ath_tx_get_tx_tid(an, tid->tid);
2373227364Sadrian	index  = ATH_BA_INDEX(tap->txa_start, seqno);
2374227364Sadrian	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
2375227364Sadrian
2376227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2377236872Sadrian	    "%s: tid=%d, baw=%d:%d, seqno=%d, index=%d, cindex=%d, "
2378229949Sadrian	    "baw head=%d, tail=%d\n",
2379236872Sadrian	    __func__, tid->tid, tap->txa_start, tap->txa_wnd, seqno, index,
2380227364Sadrian	    cindex, tid->baw_head, tid->baw_tail);
2381227364Sadrian
2382227364Sadrian	/*
2383227364Sadrian	 * If this occurs then we have a big problem - something else
2384227364Sadrian	 * has slid tap->txa_start along without updating the BAW
2385227364Sadrian	 * tracking start/end pointers. Thus the TX BAW state is now
2386227364Sadrian	 * completely busted.
2387227364Sadrian	 *
2388227364Sadrian	 * But for now, since I haven't yet fixed TDMA and buffer cloning,
2389227364Sadrian	 * it's quite possible that a cloned buffer is making its way
2390227364Sadrian	 * here and causing it to fire off. Disable TDMA for now.
2391227364Sadrian	 */
2392227364Sadrian	if (tid->tx_buf[cindex] != bf) {
2393227364Sadrian		device_printf(sc->sc_dev,
2394227364Sadrian		    "%s: comp bf=%p, seq=%d; slot bf=%p, seqno=%d\n",
2395227364Sadrian		    __func__,
2396227364Sadrian		    bf, SEQNO(bf->bf_state.bfs_seqno),
2397227364Sadrian		    tid->tx_buf[cindex],
2398227364Sadrian		    SEQNO(tid->tx_buf[cindex]->bf_state.bfs_seqno));
2399227364Sadrian	}
2400227364Sadrian
2401227364Sadrian	tid->tx_buf[cindex] = NULL;
2402227364Sadrian
2403229949Sadrian	while (tid->baw_head != tid->baw_tail &&
2404229949Sadrian	    !tid->tx_buf[tid->baw_head]) {
2405227364Sadrian		INCR(tap->txa_start, IEEE80211_SEQ_RANGE);
2406227364Sadrian		INCR(tid->baw_head, ATH_TID_MAX_BUFS);
2407227364Sadrian	}
2408229949Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
2409229949Sadrian	    "%s: baw is now %d:%d, baw head=%d\n",
2410227364Sadrian	    __func__, tap->txa_start, tap->txa_wnd, tid->baw_head);
2411227364Sadrian}
2412227364Sadrian
2413227364Sadrian/*
2414227364Sadrian * Mark the current node/TID as ready to TX.
2415227364Sadrian *
2416227364Sadrian * This is done to make it easy for the software scheduler to
2417227364Sadrian * find which nodes have data to send.
2418227364Sadrian *
2419227364Sadrian * The TXQ lock must be held.
2420227364Sadrian */
2421227364Sadrianstatic void
2422227364Sadrianath_tx_tid_sched(struct ath_softc *sc, struct ath_tid *tid)
2423227364Sadrian{
2424227364Sadrian	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
2425227364Sadrian
2426227364Sadrian	ATH_TXQ_LOCK_ASSERT(txq);
2427227364Sadrian
2428227364Sadrian	if (tid->paused)
2429227364Sadrian		return;		/* paused, can't schedule yet */
2430227364Sadrian
2431227364Sadrian	if (tid->sched)
2432227364Sadrian		return;		/* already scheduled */
2433227364Sadrian
2434227364Sadrian	tid->sched = 1;
2435227364Sadrian
2436227364Sadrian	TAILQ_INSERT_TAIL(&txq->axq_tidq, tid, axq_qelem);
2437227364Sadrian}
2438227364Sadrian
2439227364Sadrian/*
2440227364Sadrian * Mark the current node as no longer needing to be polled for
2441227364Sadrian * TX packets.
2442227364Sadrian *
2443227364Sadrian * The TXQ lock must be held.
2444227364Sadrian */
2445227364Sadrianstatic void
2446227364Sadrianath_tx_tid_unsched(struct ath_softc *sc, struct ath_tid *tid)
2447227364Sadrian{
2448227364Sadrian	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
2449227364Sadrian
2450227364Sadrian	ATH_TXQ_LOCK_ASSERT(txq);
2451227364Sadrian
2452227364Sadrian	if (tid->sched == 0)
2453227364Sadrian		return;
2454227364Sadrian
2455227364Sadrian	tid->sched = 0;
2456227364Sadrian	TAILQ_REMOVE(&txq->axq_tidq, tid, axq_qelem);
2457227364Sadrian}
2458227364Sadrian
2459227364Sadrian/*
2460227364Sadrian * Assign a sequence number manually to the given frame.
2461227364Sadrian *
2462227364Sadrian * This should only be called for A-MPDU TX frames.
2463227364Sadrian */
2464236872Sadrianstatic ieee80211_seq
2465227364Sadrianath_tx_tid_seqno_assign(struct ath_softc *sc, struct ieee80211_node *ni,
2466227364Sadrian    struct ath_buf *bf, struct mbuf *m0)
2467227364Sadrian{
2468227364Sadrian	struct ieee80211_frame *wh;
2469227364Sadrian	int tid, pri;
2470227364Sadrian	ieee80211_seq seqno;
2471227364Sadrian	uint8_t subtype;
2472227364Sadrian
2473227364Sadrian	/* TID lookup */
2474227364Sadrian	wh = mtod(m0, struct ieee80211_frame *);
2475227364Sadrian	pri = M_WME_GETAC(m0);			/* honor classification */
2476227364Sadrian	tid = WME_AC_TO_TID(pri);
2477236872Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: pri=%d, tid=%d, qos has seq=%d\n",
2478236872Sadrian	    __func__, pri, tid, IEEE80211_QOS_HAS_SEQ(wh));
2479227364Sadrian
2480227364Sadrian	/* XXX Is it a control frame? Ignore */
2481227364Sadrian
2482227364Sadrian	/* Does the packet require a sequence number? */
2483227364Sadrian	if (! IEEE80211_QOS_HAS_SEQ(wh))
2484227364Sadrian		return -1;
2485227364Sadrian
2486236880Sadrian	ATH_TID_LOCK_ASSERT(sc, &(ATH_NODE(ni)->an_tid[tid]));
2487236880Sadrian
2488227364Sadrian	/*
2489227364Sadrian	 * Is it a QOS NULL Data frame? Give it a sequence number from
2490227364Sadrian	 * the default TID (IEEE80211_NONQOS_TID.)
2491227364Sadrian	 *
2492227364Sadrian	 * The RX path of everything I've looked at doesn't include the NULL
2493227364Sadrian	 * data frame sequence number in the aggregation state updates, so
2494227364Sadrian	 * assigning it a sequence number there will cause a BAW hole on the
2495227364Sadrian	 * RX side.
2496227364Sadrian	 */
2497227364Sadrian	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
2498227364Sadrian	if (subtype == IEEE80211_FC0_SUBTYPE_QOS_NULL) {
2499236880Sadrian		/* XXX no locking for this TID? This is a bit of a problem. */
2500227364Sadrian		seqno = ni->ni_txseqs[IEEE80211_NONQOS_TID];
2501227364Sadrian		INCR(ni->ni_txseqs[IEEE80211_NONQOS_TID], IEEE80211_SEQ_RANGE);
2502227364Sadrian	} else {
2503227364Sadrian		/* Manually assign sequence number */
2504227364Sadrian		seqno = ni->ni_txseqs[tid];
2505227364Sadrian		INCR(ni->ni_txseqs[tid], IEEE80211_SEQ_RANGE);
2506227364Sadrian	}
2507227364Sadrian	*(uint16_t *)&wh->i_seq[0] = htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
2508227364Sadrian	M_SEQNO_SET(m0, seqno);
2509227364Sadrian
2510227364Sadrian	/* Return so caller can do something with it if needed */
2511236872Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s:  -> seqno=%d\n", __func__, seqno);
2512227364Sadrian	return seqno;
2513227364Sadrian}
2514227364Sadrian
2515227364Sadrian/*
2516227364Sadrian * Attempt to direct dispatch an aggregate frame to hardware.
2517227364Sadrian * If the frame is out of BAW, queue.
2518227364Sadrian * Otherwise, schedule it as a single frame.
2519227364Sadrian */
2520227364Sadrianstatic void
2521239051Sadrianath_tx_xmit_aggr(struct ath_softc *sc, struct ath_node *an,
2522239051Sadrian    struct ath_txq *txq, struct ath_buf *bf)
2523227364Sadrian{
2524227364Sadrian	struct ath_tid *tid = &an->an_tid[bf->bf_state.bfs_tid];
2525239051Sadrian//	struct ath_txq *txq = bf->bf_state.bfs_txq;
2526227364Sadrian	struct ieee80211_tx_ampdu *tap;
2527227364Sadrian
2528239051Sadrian	if (txq != bf->bf_state.bfs_txq) {
2529239051Sadrian		device_printf(sc->sc_dev, "%s: txq %d != bfs_txq %d!\n",
2530239051Sadrian		    __func__,
2531239051Sadrian		    txq->axq_qnum,
2532239051Sadrian		    bf->bf_state.bfs_txq->axq_qnum);
2533239051Sadrian	}
2534239051Sadrian
2535227364Sadrian	ATH_TXQ_LOCK_ASSERT(txq);
2536236873Sadrian	ATH_TID_LOCK_ASSERT(sc, tid);
2537227364Sadrian
2538227364Sadrian	tap = ath_tx_get_tx_tid(an, tid->tid);
2539227364Sadrian
2540227364Sadrian	/* paused? queue */
2541227364Sadrian	if (tid->paused) {
2542236876Sadrian		ATH_TXQ_INSERT_HEAD(tid, bf, bf_list);
2543233480Sadrian		/* XXX don't sched - we're paused! */
2544227364Sadrian		return;
2545227364Sadrian	}
2546227364Sadrian
2547227364Sadrian	/* outside baw? queue */
2548227364Sadrian	if (bf->bf_state.bfs_dobaw &&
2549227364Sadrian	    (! BAW_WITHIN(tap->txa_start, tap->txa_wnd,
2550227364Sadrian	    SEQNO(bf->bf_state.bfs_seqno)))) {
2551236878Sadrian		ATH_TXQ_INSERT_HEAD(tid, bf, bf_list);
2552227364Sadrian		ath_tx_tid_sched(sc, tid);
2553227364Sadrian		return;
2554227364Sadrian	}
2555227364Sadrian
2556240180Sadrian	/*
2557240180Sadrian	 * This is a temporary check and should be removed once
2558240180Sadrian	 * all the relevant code paths have been fixed.
2559240180Sadrian	 *
2560240180Sadrian	 * During aggregate retries, it's possible that the head
2561240180Sadrian	 * frame will fail (which has the bfs_aggr and bfs_nframes
2562240180Sadrian	 * fields set for said aggregate) and will be retried as
2563240180Sadrian	 * a single frame.  In this instance, the values should
2564240180Sadrian	 * be reset or the completion code will get upset with you.
2565240180Sadrian	 */
2566240180Sadrian	if (bf->bf_state.bfs_aggr != 0 || bf->bf_state.bfs_nframes > 1) {
2567240180Sadrian		device_printf(sc->sc_dev, "%s: bfs_aggr=%d, bfs_nframes=%d\n",
2568240180Sadrian		    __func__,
2569240180Sadrian		    bf->bf_state.bfs_aggr,
2570240180Sadrian		    bf->bf_state.bfs_nframes);
2571240180Sadrian		bf->bf_state.bfs_aggr = 0;
2572240180Sadrian		bf->bf_state.bfs_nframes = 1;
2573240180Sadrian	}
2574240180Sadrian
2575227364Sadrian	/* Direct dispatch to hardware */
2576227364Sadrian	ath_tx_do_ratelookup(sc, bf);
2577233989Sadrian	ath_tx_calc_duration(sc, bf);
2578233989Sadrian	ath_tx_calc_protection(sc, bf);
2579233989Sadrian	ath_tx_set_rtscts(sc, bf);
2580227364Sadrian	ath_tx_rate_fill_rcflags(sc, bf);
2581227364Sadrian	ath_tx_setds(sc, bf);
2582227364Sadrian
2583227364Sadrian	/* Statistics */
2584227364Sadrian	sc->sc_aggr_stats.aggr_low_hwq_single_pkt++;
2585227364Sadrian
2586227364Sadrian	/* Track per-TID hardware queue depth correctly */
2587227364Sadrian	tid->hwq_depth++;
2588227364Sadrian
2589227364Sadrian	/* Add to BAW */
2590227364Sadrian	if (bf->bf_state.bfs_dobaw) {
2591227364Sadrian		ath_tx_addto_baw(sc, an, tid, bf);
2592227364Sadrian		bf->bf_state.bfs_addedbaw = 1;
2593227364Sadrian	}
2594227364Sadrian
2595227364Sadrian	/* Set completion handler, multi-frame aggregate or not */
2596227364Sadrian	bf->bf_comp = ath_tx_aggr_comp;
2597227364Sadrian
2598227364Sadrian	/* Hand off to hardware */
2599227364Sadrian	ath_tx_handoff(sc, txq, bf);
2600227364Sadrian}
2601227364Sadrian
2602227364Sadrian/*
2603227364Sadrian * Attempt to send the packet.
2604227364Sadrian * If the queue isn't busy, direct-dispatch.
2605227364Sadrian * If the queue is busy enough, queue the given packet on the
2606227364Sadrian *  relevant software queue.
2607227364Sadrian */
2608227364Sadrianvoid
2609227364Sadrianath_tx_swq(struct ath_softc *sc, struct ieee80211_node *ni, struct ath_txq *txq,
2610227364Sadrian    struct ath_buf *bf)
2611227364Sadrian{
2612227364Sadrian	struct ath_node *an = ATH_NODE(ni);
2613227364Sadrian	struct ieee80211_frame *wh;
2614227364Sadrian	struct ath_tid *atid;
2615227364Sadrian	int pri, tid;
2616227364Sadrian	struct mbuf *m0 = bf->bf_m;
2617227364Sadrian
2618236880Sadrian	ATH_TXQ_LOCK_ASSERT(txq);
2619236880Sadrian
2620227364Sadrian	/* Fetch the TID - non-QoS frames get assigned to TID 16 */
2621227364Sadrian	wh = mtod(m0, struct ieee80211_frame *);
2622227364Sadrian	pri = ath_tx_getac(sc, m0);
2623227364Sadrian	tid = ath_tx_gettid(sc, m0);
2624227364Sadrian	atid = &an->an_tid[tid];
2625227364Sadrian
2626236873Sadrian	ATH_TID_LOCK_ASSERT(sc, atid);
2627236873Sadrian
2628236872Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bf=%p, pri=%d, tid=%d, qos=%d\n",
2629236872Sadrian	    __func__, bf, pri, tid, IEEE80211_QOS_HAS_SEQ(wh));
2630227364Sadrian
2631227364Sadrian	/* Set local packet state, used to queue packets to hardware */
2632239051Sadrian	/* XXX potentially duplicate info, re-check */
2633239051Sadrian	/* XXX remember, txq must be the hardware queue, not the av_mcastq */
2634227364Sadrian	bf->bf_state.bfs_tid = tid;
2635227364Sadrian	bf->bf_state.bfs_txq = txq;
2636227364Sadrian	bf->bf_state.bfs_pri = pri;
2637227364Sadrian
2638227364Sadrian	/*
2639227364Sadrian	 * If the hardware queue isn't busy, queue it directly.
2640227364Sadrian	 * If the hardware queue is busy, queue it.
2641227364Sadrian	 * If the TID is paused or the traffic it outside BAW, software
2642227364Sadrian	 * queue it.
2643227364Sadrian	 */
2644227364Sadrian	if (atid->paused) {
2645227364Sadrian		/* TID is paused, queue */
2646236872Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: paused\n", __func__);
2647227364Sadrian		ATH_TXQ_INSERT_TAIL(atid, bf, bf_list);
2648227364Sadrian	} else if (ath_tx_ampdu_pending(sc, an, tid)) {
2649227364Sadrian		/* AMPDU pending; queue */
2650236872Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: pending\n", __func__);
2651227364Sadrian		ATH_TXQ_INSERT_TAIL(atid, bf, bf_list);
2652227364Sadrian		/* XXX sched? */
2653227364Sadrian	} else if (ath_tx_ampdu_running(sc, an, tid)) {
2654227364Sadrian		/* AMPDU running, attempt direct dispatch if possible */
2655236877Sadrian
2656236877Sadrian		/*
2657236877Sadrian		 * Always queue the frame to the tail of the list.
2658236877Sadrian		 */
2659236877Sadrian		ATH_TXQ_INSERT_TAIL(atid, bf, bf_list);
2660236877Sadrian
2661236877Sadrian		/*
2662236877Sadrian		 * If the hardware queue isn't busy, direct dispatch
2663236877Sadrian		 * the head frame in the list.  Don't schedule the
2664236877Sadrian		 * TID - let it build some more frames first?
2665236877Sadrian		 *
2666236877Sadrian		 * Otherwise, schedule the TID.
2667236877Sadrian		 */
2668229949Sadrian		if (txq->axq_depth < sc->sc_hwq_limit) {
2669236877Sadrian			bf = TAILQ_FIRST(&atid->axq_q);
2670236877Sadrian			ATH_TXQ_REMOVE(atid, bf, bf_list);
2671240180Sadrian
2672240180Sadrian			/*
2673240180Sadrian			 * Ensure it's definitely treated as a non-AMPDU
2674240180Sadrian			 * frame - this information may have been left
2675240180Sadrian			 * over from a previous attempt.
2676240180Sadrian			 */
2677240180Sadrian			bf->bf_state.bfs_aggr = 0;
2678240180Sadrian			bf->bf_state.bfs_nframes = 1;
2679240180Sadrian
2680240180Sadrian			/* Queue to the hardware */
2681239051Sadrian			ath_tx_xmit_aggr(sc, an, txq, bf);
2682233227Sadrian			DPRINTF(sc, ATH_DEBUG_SW_TX,
2683236872Sadrian			    "%s: xmit_aggr\n",
2684236872Sadrian			    __func__);
2685229949Sadrian		} else {
2686229949Sadrian			DPRINTF(sc, ATH_DEBUG_SW_TX,
2687236872Sadrian			    "%s: ampdu; swq'ing\n",
2688236872Sadrian			    __func__);
2689227364Sadrian			ath_tx_tid_sched(sc, atid);
2690227364Sadrian		}
2691227364Sadrian	} else if (txq->axq_depth < sc->sc_hwq_limit) {
2692227364Sadrian		/* AMPDU not running, attempt direct dispatch */
2693236872Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: xmit_normal\n", __func__);
2694227364Sadrian		ath_tx_xmit_normal(sc, txq, bf);
2695227364Sadrian	} else {
2696227364Sadrian		/* Busy; queue */
2697236872Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: swq'ing\n", __func__);
2698227364Sadrian		ATH_TXQ_INSERT_TAIL(atid, bf, bf_list);
2699227364Sadrian		ath_tx_tid_sched(sc, atid);
2700227364Sadrian	}
2701227364Sadrian}
2702227364Sadrian
2703227364Sadrian/*
2704227364Sadrian * Do the basic frame setup stuff that's required before the frame
2705227364Sadrian * is added to a software queue.
2706227364Sadrian *
2707227364Sadrian * All frames get mostly the same treatment and it's done once.
2708227364Sadrian * Retransmits fiddle with things like the rate control setup,
2709227364Sadrian * setting the retransmit bit in the packet; doing relevant DMA/bus
2710227364Sadrian * syncing and relinking it (back) into the hardware TX queue.
2711227364Sadrian *
2712227364Sadrian * Note that this may cause the mbuf to be reallocated, so
2713227364Sadrian * m0 may not be valid.
2714227364Sadrian */
2715227364Sadrian
2716227364Sadrian
2717227364Sadrian/*
2718227364Sadrian * Configure the per-TID node state.
2719227364Sadrian *
2720227364Sadrian * This likely belongs in if_ath_node.c but I can't think of anywhere
2721227364Sadrian * else to put it just yet.
2722227364Sadrian *
2723227364Sadrian * This sets up the SLISTs and the mutex as appropriate.
2724227364Sadrian */
2725227364Sadrianvoid
2726227364Sadrianath_tx_tid_init(struct ath_softc *sc, struct ath_node *an)
2727227364Sadrian{
2728227364Sadrian	int i, j;
2729227364Sadrian	struct ath_tid *atid;
2730227364Sadrian
2731227364Sadrian	for (i = 0; i < IEEE80211_TID_SIZE; i++) {
2732227364Sadrian		atid = &an->an_tid[i];
2733240639Sadrian
2734240639Sadrian		/* XXX now with this bzer(), is the field 0'ing needed? */
2735240639Sadrian		bzero(atid, sizeof(*atid));
2736240639Sadrian
2737227364Sadrian		TAILQ_INIT(&atid->axq_q);
2738240639Sadrian		TAILQ_INIT(&atid->filtq.axq_q);
2739227364Sadrian		atid->tid = i;
2740227364Sadrian		atid->an = an;
2741227364Sadrian		for (j = 0; j < ATH_TID_MAX_BUFS; j++)
2742227364Sadrian			atid->tx_buf[j] = NULL;
2743227364Sadrian		atid->baw_head = atid->baw_tail = 0;
2744227364Sadrian		atid->paused = 0;
2745227364Sadrian		atid->sched = 0;
2746227364Sadrian		atid->hwq_depth = 0;
2747227364Sadrian		atid->cleanup_inprogress = 0;
2748240639Sadrian		atid->clrdmask = 1;	/* Always start by setting this bit */
2749227364Sadrian		if (i == IEEE80211_NONQOS_TID)
2750227364Sadrian			atid->ac = WME_AC_BE;
2751227364Sadrian		else
2752227364Sadrian			atid->ac = TID_TO_WME_AC(i);
2753227364Sadrian	}
2754227364Sadrian}
2755227364Sadrian
2756227364Sadrian/*
2757227364Sadrian * Pause the current TID. This stops packets from being transmitted
2758227364Sadrian * on it.
2759227364Sadrian *
2760227364Sadrian * Since this is also called from upper layers as well as the driver,
2761227364Sadrian * it will get the TID lock.
2762227364Sadrian */
2763227364Sadrianstatic void
2764227364Sadrianath_tx_tid_pause(struct ath_softc *sc, struct ath_tid *tid)
2765227364Sadrian{
2766233908Sadrian
2767233908Sadrian	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
2768227364Sadrian	tid->paused++;
2769227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: paused = %d\n",
2770227364Sadrian	    __func__, tid->paused);
2771227364Sadrian}
2772227364Sadrian
2773227364Sadrian/*
2774227364Sadrian * Unpause the current TID, and schedule it if needed.
2775227364Sadrian */
2776227364Sadrianstatic void
2777227364Sadrianath_tx_tid_resume(struct ath_softc *sc, struct ath_tid *tid)
2778227364Sadrian{
2779227364Sadrian	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
2780227364Sadrian
2781227364Sadrian	tid->paused--;
2782227364Sadrian
2783227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: unpaused = %d\n",
2784227364Sadrian	    __func__, tid->paused);
2785227364Sadrian
2786227364Sadrian	if (tid->paused || tid->axq_depth == 0) {
2787227364Sadrian		return;
2788227364Sadrian	}
2789227364Sadrian
2790240639Sadrian	/* XXX isfiltered shouldn't ever be 0 at this point */
2791240639Sadrian	if (tid->isfiltered == 1) {
2792240639Sadrian		device_printf(sc->sc_dev, "%s: filtered?!\n", __func__);
2793240639Sadrian		return;
2794240639Sadrian	}
2795240639Sadrian
2796227364Sadrian	ath_tx_tid_sched(sc, tid);
2797227364Sadrian	/* Punt some frames to the hardware if needed */
2798233673Sadrian	//ath_txq_sched(sc, sc->sc_ac2q[tid->ac]);
2799233673Sadrian	taskqueue_enqueue(sc->sc_tq, &sc->sc_txqtask);
2800227364Sadrian}
2801227364Sadrian
2802227364Sadrian/*
2803240639Sadrian * Add the given ath_buf to the TID filtered frame list.
2804240639Sadrian * This requires the TID be filtered.
2805240639Sadrian */
2806240639Sadrianstatic void
2807240639Sadrianath_tx_tid_filt_addbuf(struct ath_softc *sc, struct ath_tid *tid,
2808240639Sadrian    struct ath_buf *bf)
2809240639Sadrian{
2810240639Sadrian
2811240639Sadrian	ATH_TID_LOCK_ASSERT(sc, tid);
2812240639Sadrian	if (! tid->isfiltered)
2813240639Sadrian		device_printf(sc->sc_dev, "%s: not filtered?!\n", __func__);
2814240639Sadrian
2815240639Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_FILT, "%s: bf=%p\n", __func__, bf);
2816240639Sadrian
2817240639Sadrian	/* Set the retry bit and bump the retry counter */
2818240639Sadrian	ath_tx_set_retry(sc, bf);
2819240639Sadrian	sc->sc_stats.ast_tx_swfiltered++;
2820240639Sadrian
2821240639Sadrian	ATH_TXQ_INSERT_TAIL(&tid->filtq, bf, bf_list);
2822240639Sadrian}
2823240639Sadrian
2824240639Sadrian/*
2825240639Sadrian * Handle a completed filtered frame from the given TID.
2826240639Sadrian * This just enables/pauses the filtered frame state if required
2827240639Sadrian * and appends the filtered frame to the filtered queue.
2828240639Sadrian */
2829240639Sadrianstatic void
2830240639Sadrianath_tx_tid_filt_comp_buf(struct ath_softc *sc, struct ath_tid *tid,
2831240639Sadrian    struct ath_buf *bf)
2832240639Sadrian{
2833240639Sadrian
2834240639Sadrian	ATH_TID_LOCK_ASSERT(sc, tid);
2835240639Sadrian
2836240639Sadrian	if (! tid->isfiltered) {
2837240639Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT, "%s: filter transition\n",
2838240639Sadrian		    __func__);
2839240639Sadrian		tid->isfiltered = 1;
2840240639Sadrian		ath_tx_tid_pause(sc, tid);
2841240639Sadrian	}
2842240639Sadrian
2843240639Sadrian	/* Add the frame to the filter queue */
2844240639Sadrian	ath_tx_tid_filt_addbuf(sc, tid, bf);
2845240639Sadrian}
2846240639Sadrian
2847240639Sadrian/*
2848240639Sadrian * Complete the filtered frame TX completion.
2849240639Sadrian *
2850240639Sadrian * If there are no more frames in the hardware queue, unpause/unfilter
2851240639Sadrian * the TID if applicable.  Otherwise we will wait for a node PS transition
2852240639Sadrian * to unfilter.
2853240639Sadrian */
2854240639Sadrianstatic void
2855240639Sadrianath_tx_tid_filt_comp_complete(struct ath_softc *sc, struct ath_tid *tid)
2856240639Sadrian{
2857240639Sadrian	struct ath_buf *bf;
2858240639Sadrian
2859240639Sadrian	ATH_TID_LOCK_ASSERT(sc, tid);
2860240639Sadrian
2861240639Sadrian	if (tid->hwq_depth != 0)
2862240639Sadrian		return;
2863240639Sadrian
2864240639Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_FILT, "%s: hwq=0, transition back\n",
2865240639Sadrian	    __func__);
2866240639Sadrian	tid->isfiltered = 0;
2867240639Sadrian	tid->clrdmask = 1;
2868240639Sadrian
2869240639Sadrian	/* XXX this is really quite inefficient */
2870240639Sadrian	while ((bf = TAILQ_LAST(&tid->filtq.axq_q, ath_bufhead_s)) != NULL) {
2871240639Sadrian		ATH_TXQ_REMOVE(&tid->filtq, bf, bf_list);
2872240639Sadrian		ATH_TXQ_INSERT_HEAD(tid, bf, bf_list);
2873240639Sadrian	}
2874240639Sadrian
2875240639Sadrian	ath_tx_tid_resume(sc, tid);
2876240639Sadrian}
2877240639Sadrian
2878240639Sadrian/*
2879240639Sadrian * Called when a single (aggregate or otherwise) frame is completed.
2880240639Sadrian *
2881240639Sadrian * Returns 1 if the buffer could be added to the filtered list
2882240639Sadrian * (cloned or otherwise), 0 if the buffer couldn't be added to the
2883240639Sadrian * filtered list (failed clone; expired retry) and the caller should
2884240639Sadrian * free it and handle it like a failure (eg by sending a BAR.)
2885240639Sadrian */
2886240639Sadrianstatic int
2887240639Sadrianath_tx_tid_filt_comp_single(struct ath_softc *sc, struct ath_tid *tid,
2888240639Sadrian    struct ath_buf *bf)
2889240639Sadrian{
2890240639Sadrian	struct ath_buf *nbf;
2891240639Sadrian	int retval;
2892240639Sadrian
2893240639Sadrian	ATH_TID_LOCK_ASSERT(sc, tid);
2894240639Sadrian
2895240639Sadrian	/*
2896240639Sadrian	 * Don't allow a filtered frame to live forever.
2897240639Sadrian	 */
2898240639Sadrian	if (bf->bf_state.bfs_retries > SWMAX_RETRIES) {
2899240639Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
2900240639Sadrian		    "%s: bf=%p, seqno=%d, exceeded retries\n",
2901240639Sadrian		    __func__,
2902240639Sadrian		    bf,
2903240639Sadrian		    bf->bf_state.bfs_seqno);
2904240639Sadrian		return (0);
2905240639Sadrian	}
2906240639Sadrian
2907240639Sadrian	/*
2908240639Sadrian	 * A busy buffer can't be added to the retry list.
2909240639Sadrian	 * It needs to be cloned.
2910240639Sadrian	 */
2911240639Sadrian	if (bf->bf_flags & ATH_BUF_BUSY) {
2912240639Sadrian		nbf = ath_tx_retry_clone(sc, tid->an, tid, bf);
2913240639Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
2914240639Sadrian		    "%s: busy buffer clone: %p -> %p\n",
2915240639Sadrian		    __func__, bf, nbf);
2916240639Sadrian	} else {
2917240639Sadrian		nbf = bf;
2918240639Sadrian	}
2919240639Sadrian
2920240639Sadrian	if (nbf == NULL) {
2921240639Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
2922240639Sadrian		    "%s: busy buffer couldn't be cloned (%p)!\n",
2923240639Sadrian		    __func__, bf);
2924240639Sadrian		retval = 1;
2925240639Sadrian	} else {
2926240639Sadrian		ath_tx_tid_filt_comp_buf(sc, tid, nbf);
2927240639Sadrian		retval = 0;
2928240639Sadrian	}
2929240639Sadrian	ath_tx_tid_filt_comp_complete(sc, tid);
2930240639Sadrian
2931240639Sadrian	return (retval);
2932240639Sadrian}
2933240639Sadrian
2934240639Sadrianstatic void
2935240639Sadrianath_tx_tid_filt_comp_aggr(struct ath_softc *sc, struct ath_tid *tid,
2936240639Sadrian    struct ath_buf *bf_first, ath_bufhead *bf_q)
2937240639Sadrian{
2938240639Sadrian	struct ath_buf *bf, *bf_next, *nbf;
2939240639Sadrian
2940240639Sadrian	ATH_TID_LOCK_ASSERT(sc, tid);
2941240639Sadrian
2942240639Sadrian	bf = bf_first;
2943240639Sadrian	while (bf) {
2944240639Sadrian		bf_next = bf->bf_next;
2945240639Sadrian		bf->bf_next = NULL;	/* Remove it from the aggr list */
2946240639Sadrian
2947240639Sadrian		/*
2948240639Sadrian		 * Don't allow a filtered frame to live forever.
2949240639Sadrian		 */
2950240639Sadrian		if (bf->bf_state.bfs_retries > SWMAX_RETRIES) {
2951240639Sadrian			DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
2952240639Sadrian			    "%s: bf=%p, seqno=%d, exceeded retries\n",
2953240639Sadrian			    __func__,
2954240639Sadrian			    bf,
2955240639Sadrian			    bf->bf_state.bfs_seqno);
2956240639Sadrian			TAILQ_INSERT_TAIL(bf_q, bf, bf_list);
2957240639Sadrian			goto next;
2958240639Sadrian		}
2959240639Sadrian
2960240639Sadrian		if (bf->bf_flags & ATH_BUF_BUSY) {
2961240639Sadrian			nbf = ath_tx_retry_clone(sc, tid->an, tid, bf);
2962240639Sadrian			DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
2963240639Sadrian			    "%s: busy buffer cloned: %p -> %p",
2964240639Sadrian			    __func__, bf, nbf);
2965240639Sadrian		} else {
2966240639Sadrian			nbf = bf;
2967240639Sadrian		}
2968240639Sadrian
2969240639Sadrian		/*
2970240639Sadrian		 * If the buffer couldn't be cloned, add it to bf_q;
2971240639Sadrian		 * the caller will free the buffer(s) as required.
2972240639Sadrian		 */
2973240639Sadrian		if (nbf == NULL) {
2974240639Sadrian			DPRINTF(sc, ATH_DEBUG_SW_TX_FILT,
2975240639Sadrian			    "%s: buffer couldn't be cloned! (%p)\n",
2976240639Sadrian			    __func__, bf);
2977240639Sadrian			TAILQ_INSERT_TAIL(bf_q, bf, bf_list);
2978240639Sadrian		} else {
2979240639Sadrian			ath_tx_tid_filt_comp_buf(sc, tid, nbf);
2980240639Sadrian		}
2981240639Sadriannext:
2982240639Sadrian		bf = bf_next;
2983240639Sadrian	}
2984240639Sadrian
2985240639Sadrian	ath_tx_tid_filt_comp_complete(sc, tid);
2986240639Sadrian}
2987240639Sadrian
2988240639Sadrian/*
2989233908Sadrian * Suspend the queue because we need to TX a BAR.
2990233908Sadrian */
2991233908Sadrianstatic void
2992233908Sadrianath_tx_tid_bar_suspend(struct ath_softc *sc, struct ath_tid *tid)
2993233908Sadrian{
2994233908Sadrian	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
2995233908Sadrian
2996235491Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
2997235676Sadrian	    "%s: tid=%p, bar_wait=%d, bar_tx=%d, called\n",
2998233908Sadrian	    __func__,
2999235676Sadrian	    tid,
3000235676Sadrian	    tid->bar_wait,
3001235676Sadrian	    tid->bar_tx);
3002233908Sadrian
3003233908Sadrian	/* We shouldn't be called when bar_tx is 1 */
3004233908Sadrian	if (tid->bar_tx) {
3005233908Sadrian		device_printf(sc->sc_dev, "%s: bar_tx is 1?!\n",
3006233908Sadrian		    __func__);
3007233908Sadrian	}
3008233908Sadrian
3009233908Sadrian	/* If we've already been called, just be patient. */
3010233908Sadrian	if (tid->bar_wait)
3011233908Sadrian		return;
3012233908Sadrian
3013233908Sadrian	/* Wait! */
3014233908Sadrian	tid->bar_wait = 1;
3015233908Sadrian
3016233908Sadrian	/* Only one pause, no matter how many frames fail */
3017233908Sadrian	ath_tx_tid_pause(sc, tid);
3018233908Sadrian}
3019233908Sadrian
3020233908Sadrian/*
3021233908Sadrian * We've finished with BAR handling - either we succeeded or
3022233908Sadrian * failed. Either way, unsuspend TX.
3023233908Sadrian */
3024233908Sadrianstatic void
3025233908Sadrianath_tx_tid_bar_unsuspend(struct ath_softc *sc, struct ath_tid *tid)
3026233908Sadrian{
3027233908Sadrian	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
3028233908Sadrian
3029235491Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
3030233908Sadrian	    "%s: tid=%p, called\n",
3031233908Sadrian	    __func__,
3032233908Sadrian	    tid);
3033233908Sadrian
3034233908Sadrian	if (tid->bar_tx == 0 || tid->bar_wait == 0) {
3035233908Sadrian		device_printf(sc->sc_dev, "%s: bar_tx=%d, bar_wait=%d: ?\n",
3036233908Sadrian		    __func__, tid->bar_tx, tid->bar_wait);
3037233908Sadrian	}
3038233908Sadrian
3039233908Sadrian	tid->bar_tx = tid->bar_wait = 0;
3040233908Sadrian	ath_tx_tid_resume(sc, tid);
3041233908Sadrian}
3042233908Sadrian
3043233908Sadrian/*
3044233908Sadrian * Return whether we're ready to TX a BAR frame.
3045233908Sadrian *
3046233908Sadrian * Requires the TID lock be held.
3047233908Sadrian */
3048233908Sadrianstatic int
3049233908Sadrianath_tx_tid_bar_tx_ready(struct ath_softc *sc, struct ath_tid *tid)
3050233908Sadrian{
3051233908Sadrian
3052233908Sadrian	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
3053233908Sadrian
3054233908Sadrian	if (tid->bar_wait == 0 || tid->hwq_depth > 0)
3055233908Sadrian		return (0);
3056233908Sadrian
3057235491Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR, "%s: tid=%p (%d), bar ready\n",
3058235491Sadrian	    __func__, tid, tid->tid);
3059235491Sadrian
3060233908Sadrian	return (1);
3061233908Sadrian}
3062233908Sadrian
3063233908Sadrian/*
3064233908Sadrian * Check whether the current TID is ready to have a BAR
3065233908Sadrian * TXed and if so, do the TX.
3066233908Sadrian *
3067233908Sadrian * Since the TID/TXQ lock can't be held during a call to
3068233908Sadrian * ieee80211_send_bar(), we have to do the dirty thing of unlocking it,
3069233908Sadrian * sending the BAR and locking it again.
3070233908Sadrian *
3071233908Sadrian * Eventually, the code to send the BAR should be broken out
3072233908Sadrian * from this routine so the lock doesn't have to be reacquired
3073233908Sadrian * just to be immediately dropped by the caller.
3074233908Sadrian */
3075233908Sadrianstatic void
3076233908Sadrianath_tx_tid_bar_tx(struct ath_softc *sc, struct ath_tid *tid)
3077233908Sadrian{
3078233908Sadrian	struct ieee80211_tx_ampdu *tap;
3079233908Sadrian
3080233908Sadrian	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
3081233908Sadrian
3082235491Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
3083233908Sadrian	    "%s: tid=%p, called\n",
3084233908Sadrian	    __func__,
3085233908Sadrian	    tid);
3086233908Sadrian
3087233908Sadrian	tap = ath_tx_get_tx_tid(tid->an, tid->tid);
3088233908Sadrian
3089233908Sadrian	/*
3090233908Sadrian	 * This is an error condition!
3091233908Sadrian	 */
3092233908Sadrian	if (tid->bar_wait == 0 || tid->bar_tx == 1) {
3093233908Sadrian		device_printf(sc->sc_dev,
3094233908Sadrian		    "%s: tid=%p, bar_tx=%d, bar_wait=%d: ?\n",
3095233908Sadrian		    __func__,
3096233908Sadrian		    tid,
3097233908Sadrian		    tid->bar_tx,
3098233908Sadrian		    tid->bar_wait);
3099233908Sadrian		return;
3100233908Sadrian	}
3101233908Sadrian
3102233908Sadrian	/* Don't do anything if we still have pending frames */
3103233908Sadrian	if (tid->hwq_depth > 0) {
3104235491Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
3105233908Sadrian		    "%s: tid=%p, hwq_depth=%d, waiting\n",
3106233908Sadrian		    __func__,
3107233908Sadrian		    tid,
3108233908Sadrian		    tid->hwq_depth);
3109233908Sadrian		return;
3110233908Sadrian	}
3111233908Sadrian
3112233908Sadrian	/* We're now about to TX */
3113233908Sadrian	tid->bar_tx = 1;
3114233908Sadrian
3115233908Sadrian	/*
3116233908Sadrian	 * Calculate new BAW left edge, now that all frames have either
3117233908Sadrian	 * succeeded or failed.
3118233908Sadrian	 *
3119233908Sadrian	 * XXX verify this is _actually_ the valid value to begin at!
3120233908Sadrian	 */
3121235491Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
3122233908Sadrian	    "%s: tid=%p, new BAW left edge=%d\n",
3123233908Sadrian	    __func__,
3124233908Sadrian	    tid,
3125233908Sadrian	    tap->txa_start);
3126233908Sadrian
3127233908Sadrian	/* Try sending the BAR frame */
3128233908Sadrian	/* We can't hold the lock here! */
3129233908Sadrian
3130233908Sadrian	ATH_TXQ_UNLOCK(sc->sc_ac2q[tid->ac]);
3131233908Sadrian	if (ieee80211_send_bar(&tid->an->an_node, tap, tap->txa_start) == 0) {
3132233908Sadrian		/* Success? Now we wait for notification that it's done */
3133233908Sadrian		ATH_TXQ_LOCK(sc->sc_ac2q[tid->ac]);
3134233908Sadrian		return;
3135233908Sadrian	}
3136233908Sadrian
3137233908Sadrian	/* Failure? For now, warn loudly and continue */
3138233908Sadrian	ATH_TXQ_LOCK(sc->sc_ac2q[tid->ac]);
3139233908Sadrian	device_printf(sc->sc_dev, "%s: tid=%p, failed to TX BAR, continue!\n",
3140233908Sadrian	    __func__, tid);
3141233908Sadrian	ath_tx_tid_bar_unsuspend(sc, tid);
3142233908Sadrian}
3143233908Sadrian
3144240639Sadrianstatic void
3145240639Sadrianath_tx_tid_drain_pkt(struct ath_softc *sc, struct ath_node *an,
3146240639Sadrian    struct ath_tid *tid, ath_bufhead *bf_cq, struct ath_buf *bf)
3147240639Sadrian{
3148233908Sadrian
3149240639Sadrian	ATH_TID_LOCK_ASSERT(sc, tid);
3150240639Sadrian
3151240639Sadrian	/*
3152240639Sadrian	 * If the current TID is running AMPDU, update
3153240639Sadrian	 * the BAW.
3154240639Sadrian	 */
3155240639Sadrian	if (ath_tx_ampdu_running(sc, an, tid->tid) &&
3156240639Sadrian	    bf->bf_state.bfs_dobaw) {
3157240639Sadrian		/*
3158240639Sadrian		 * Only remove the frame from the BAW if it's
3159240639Sadrian		 * been transmitted at least once; this means
3160240639Sadrian		 * the frame was in the BAW to begin with.
3161240639Sadrian		 */
3162240639Sadrian		if (bf->bf_state.bfs_retries > 0) {
3163240639Sadrian			ath_tx_update_baw(sc, an, tid, bf);
3164240639Sadrian			bf->bf_state.bfs_dobaw = 0;
3165240639Sadrian		}
3166240639Sadrian		/*
3167240639Sadrian		 * This has become a non-fatal error now
3168240639Sadrian		 */
3169240639Sadrian		if (! bf->bf_state.bfs_addedbaw)
3170240639Sadrian			device_printf(sc->sc_dev,
3171240639Sadrian			    "%s: wasn't added: seqno %d\n",
3172240639Sadrian			    __func__, SEQNO(bf->bf_state.bfs_seqno));
3173240639Sadrian	}
3174240639Sadrian	TAILQ_INSERT_TAIL(bf_cq, bf, bf_list);
3175240639Sadrian}
3176240639Sadrian
3177240639Sadrianstatic void
3178240639Sadrianath_tx_tid_drain_print(struct ath_softc *sc, struct ath_node *an,
3179240639Sadrian    struct ath_tid *tid, struct ath_buf *bf)
3180240639Sadrian{
3181240639Sadrian	struct ieee80211_node *ni = &an->an_node;
3182240639Sadrian	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
3183240639Sadrian	struct ieee80211_tx_ampdu *tap;
3184240639Sadrian
3185240639Sadrian	tap = ath_tx_get_tx_tid(an, tid->tid);
3186240639Sadrian
3187240639Sadrian	device_printf(sc->sc_dev,
3188240639Sadrian	    "%s: node %p: bf=%p: addbaw=%d, dobaw=%d, "
3189240639Sadrian	    "seqno=%d, retry=%d\n",
3190240639Sadrian	    __func__, ni, bf,
3191240639Sadrian	    bf->bf_state.bfs_addedbaw,
3192240639Sadrian	    bf->bf_state.bfs_dobaw,
3193240639Sadrian	    SEQNO(bf->bf_state.bfs_seqno),
3194240639Sadrian	    bf->bf_state.bfs_retries);
3195240639Sadrian	device_printf(sc->sc_dev,
3196240639Sadrian	    "%s: node %p: bf=%p: tid txq_depth=%d hwq_depth=%d, bar_wait=%d, isfiltered=%d\n",
3197240639Sadrian	    __func__, ni, bf,
3198240639Sadrian	    tid->axq_depth,
3199240639Sadrian	    tid->hwq_depth,
3200240639Sadrian	    tid->bar_wait,
3201240639Sadrian	    tid->isfiltered);
3202240639Sadrian	device_printf(sc->sc_dev,
3203240639Sadrian	    "%s: node %p: tid %d: txq_depth=%d, "
3204240639Sadrian	    "txq_aggr_depth=%d, sched=%d, paused=%d, "
3205240639Sadrian	    "hwq_depth=%d, incomp=%d, baw_head=%d, "
3206240639Sadrian	    "baw_tail=%d txa_start=%d, ni_txseqs=%d\n",
3207240639Sadrian	     __func__, ni, tid->tid, txq->axq_depth,
3208240639Sadrian	     txq->axq_aggr_depth, tid->sched, tid->paused,
3209240639Sadrian	     tid->hwq_depth, tid->incomp, tid->baw_head,
3210240639Sadrian	     tid->baw_tail, tap == NULL ? -1 : tap->txa_start,
3211240639Sadrian	     ni->ni_txseqs[tid->tid]);
3212240639Sadrian
3213240639Sadrian	/* XXX Dump the frame, see what it is? */
3214240639Sadrian	ieee80211_dump_pkt(ni->ni_ic,
3215240639Sadrian	    mtod(bf->bf_m, const uint8_t *),
3216240639Sadrian	    bf->bf_m->m_len, 0, -1);
3217240639Sadrian}
3218240639Sadrian
3219233908Sadrian/*
3220227364Sadrian * Free any packets currently pending in the software TX queue.
3221227364Sadrian *
3222227364Sadrian * This will be called when a node is being deleted.
3223227364Sadrian *
3224227364Sadrian * It can also be called on an active node during an interface
3225227364Sadrian * reset or state transition.
3226227364Sadrian *
3227227364Sadrian * (From Linux/reference):
3228227364Sadrian *
3229227364Sadrian * TODO: For frame(s) that are in the retry state, we will reuse the
3230227364Sadrian * sequence number(s) without setting the retry bit. The
3231227364Sadrian * alternative is to give up on these and BAR the receiver's window
3232227364Sadrian * forward.
3233227364Sadrian */
3234227364Sadrianstatic void
3235229949Sadrianath_tx_tid_drain(struct ath_softc *sc, struct ath_node *an,
3236229949Sadrian    struct ath_tid *tid, ath_bufhead *bf_cq)
3237227364Sadrian{
3238227364Sadrian	struct ath_buf *bf;
3239227364Sadrian	struct ieee80211_tx_ampdu *tap;
3240227364Sadrian	struct ieee80211_node *ni = &an->an_node;
3241240639Sadrian	int t;
3242227364Sadrian
3243227364Sadrian	tap = ath_tx_get_tx_tid(an, tid->tid);
3244227364Sadrian
3245240639Sadrian	ATH_TID_LOCK_ASSERT(sc, tid);
3246227364Sadrian
3247227364Sadrian	/* Walk the queue, free frames */
3248240639Sadrian	t = 0;
3249227364Sadrian	for (;;) {
3250227364Sadrian		bf = TAILQ_FIRST(&tid->axq_q);
3251227364Sadrian		if (bf == NULL) {
3252227364Sadrian			break;
3253227364Sadrian		}
3254227364Sadrian
3255227364Sadrian		if (t == 0) {
3256240639Sadrian			ath_tx_tid_drain_print(sc, an, tid, bf);
3257240639Sadrian			t = 1;
3258240639Sadrian		}
3259229165Sadrian
3260240639Sadrian		ATH_TXQ_REMOVE(tid, bf, bf_list);
3261240639Sadrian		ath_tx_tid_drain_pkt(sc, an, tid, bf_cq, bf);
3262240639Sadrian	}
3263229165Sadrian
3264240639Sadrian	/* And now, drain the filtered frame queue */
3265240639Sadrian	t = 0;
3266240639Sadrian	for (;;) {
3267240639Sadrian		bf = TAILQ_FIRST(&tid->filtq.axq_q);
3268240639Sadrian		if (bf == NULL)
3269240639Sadrian			break;
3270240639Sadrian
3271240639Sadrian		if (t == 0) {
3272240639Sadrian			ath_tx_tid_drain_print(sc, an, tid, bf);
3273233897Sadrian			t = 1;
3274227364Sadrian		}
3275227364Sadrian
3276240639Sadrian		ATH_TXQ_REMOVE(&tid->filtq, bf, bf_list);
3277240639Sadrian		ath_tx_tid_drain_pkt(sc, an, tid, bf_cq, bf);
3278227364Sadrian	}
3279227364Sadrian
3280227364Sadrian	/*
3281227364Sadrian	 * Now that it's completed, grab the TID lock and update
3282227364Sadrian	 * the sequence number and BAW window.
3283227364Sadrian	 * Because sequence numbers have been assigned to frames
3284227364Sadrian	 * that haven't been sent yet, it's entirely possible
3285227364Sadrian	 * we'll be called with some pending frames that have not
3286227364Sadrian	 * been transmitted.
3287227364Sadrian	 *
3288227364Sadrian	 * The cleaner solution is to do the sequence number allocation
3289227364Sadrian	 * when the packet is first transmitted - and thus the "retries"
3290227364Sadrian	 * check above would be enough to update the BAW/seqno.
3291227364Sadrian	 */
3292227364Sadrian
3293227364Sadrian	/* But don't do it for non-QoS TIDs */
3294227364Sadrian	if (tap) {
3295227364Sadrian#if 0
3296227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
3297227364Sadrian		    "%s: node %p: TID %d: sliding BAW left edge to %d\n",
3298227364Sadrian		    __func__, an, tid->tid, tap->txa_start);
3299227364Sadrian#endif
3300227364Sadrian		ni->ni_txseqs[tid->tid] = tap->txa_start;
3301227364Sadrian		tid->baw_tail = tid->baw_head;
3302227364Sadrian	}
3303227364Sadrian}
3304227364Sadrian
3305227364Sadrian/*
3306227364Sadrian * Flush all software queued packets for the given node.
3307227364Sadrian *
3308227364Sadrian * This occurs when a completion handler frees the last buffer
3309227364Sadrian * for a node, and the node is thus freed. This causes the node
3310227364Sadrian * to be cleaned up, which ends up calling ath_tx_node_flush.
3311227364Sadrian */
3312227364Sadrianvoid
3313227364Sadrianath_tx_node_flush(struct ath_softc *sc, struct ath_node *an)
3314227364Sadrian{
3315227364Sadrian	int tid;
3316227364Sadrian	ath_bufhead bf_cq;
3317227364Sadrian	struct ath_buf *bf;
3318227364Sadrian
3319227364Sadrian	TAILQ_INIT(&bf_cq);
3320227364Sadrian
3321227364Sadrian	for (tid = 0; tid < IEEE80211_TID_SIZE; tid++) {
3322227364Sadrian		struct ath_tid *atid = &an->an_tid[tid];
3323227364Sadrian		struct ath_txq *txq = sc->sc_ac2q[atid->ac];
3324227364Sadrian
3325227364Sadrian		/* Remove this tid from the list of active tids */
3326227364Sadrian		ATH_TXQ_LOCK(txq);
3327227364Sadrian		ath_tx_tid_unsched(sc, atid);
3328227364Sadrian
3329227364Sadrian		/* Free packets */
3330227364Sadrian		ath_tx_tid_drain(sc, an, atid, &bf_cq);
3331227364Sadrian		ATH_TXQ_UNLOCK(txq);
3332227364Sadrian	}
3333227364Sadrian
3334227364Sadrian	/* Handle completed frames */
3335227364Sadrian	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
3336227364Sadrian		TAILQ_REMOVE(&bf_cq, bf, bf_list);
3337227364Sadrian		ath_tx_default_comp(sc, bf, 0);
3338227364Sadrian	}
3339227364Sadrian}
3340227364Sadrian
3341227364Sadrian/*
3342227364Sadrian * Drain all the software TXQs currently with traffic queued.
3343227364Sadrian */
3344227364Sadrianvoid
3345227364Sadrianath_tx_txq_drain(struct ath_softc *sc, struct ath_txq *txq)
3346227364Sadrian{
3347227364Sadrian	struct ath_tid *tid;
3348227364Sadrian	ath_bufhead bf_cq;
3349227364Sadrian	struct ath_buf *bf;
3350227364Sadrian
3351227364Sadrian	TAILQ_INIT(&bf_cq);
3352227364Sadrian	ATH_TXQ_LOCK(txq);
3353227364Sadrian
3354227364Sadrian	/*
3355227364Sadrian	 * Iterate over all active tids for the given txq,
3356227364Sadrian	 * flushing and unsched'ing them
3357227364Sadrian	 */
3358227364Sadrian	while (! TAILQ_EMPTY(&txq->axq_tidq)) {
3359227364Sadrian		tid = TAILQ_FIRST(&txq->axq_tidq);
3360227364Sadrian		ath_tx_tid_drain(sc, tid->an, tid, &bf_cq);
3361227364Sadrian		ath_tx_tid_unsched(sc, tid);
3362227364Sadrian	}
3363227364Sadrian
3364227364Sadrian	ATH_TXQ_UNLOCK(txq);
3365227364Sadrian
3366227364Sadrian	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
3367227364Sadrian		TAILQ_REMOVE(&bf_cq, bf, bf_list);
3368227364Sadrian		ath_tx_default_comp(sc, bf, 0);
3369227364Sadrian	}
3370227364Sadrian}
3371227364Sadrian
3372227364Sadrian/*
3373227364Sadrian * Handle completion of non-aggregate session frames.
3374227364Sadrian */
3375227364Sadrianvoid
3376227364Sadrianath_tx_normal_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
3377227364Sadrian{
3378227364Sadrian	struct ieee80211_node *ni = bf->bf_node;
3379227364Sadrian	struct ath_node *an = ATH_NODE(ni);
3380227364Sadrian	int tid = bf->bf_state.bfs_tid;
3381227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
3382227364Sadrian	struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
3383227364Sadrian
3384227364Sadrian	/* The TID state is protected behind the TXQ lock */
3385227364Sadrian	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
3386227364Sadrian
3387227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: bf=%p: fail=%d, hwq_depth now %d\n",
3388227364Sadrian	    __func__, bf, fail, atid->hwq_depth - 1);
3389227364Sadrian
3390227364Sadrian	atid->hwq_depth--;
3391240639Sadrian
3392240639Sadrian	if (atid->isfiltered)
3393240639Sadrian		device_printf(sc->sc_dev, "%s: isfiltered=1, normal_comp?\n",
3394240639Sadrian		    __func__);
3395240639Sadrian
3396227364Sadrian	if (atid->hwq_depth < 0)
3397227364Sadrian		device_printf(sc->sc_dev, "%s: hwq_depth < 0: %d\n",
3398227364Sadrian		    __func__, atid->hwq_depth);
3399240639Sadrian
3400240639Sadrian	/*
3401240639Sadrian	 * If the queue is filtered, potentially mark it as complete
3402240639Sadrian	 * and reschedule it as needed.
3403240639Sadrian	 *
3404240639Sadrian	 * This is required as there may be a subsequent TX descriptor
3405240639Sadrian	 * for this end-node that has CLRDMASK set, so it's quite possible
3406240639Sadrian	 * that a filtered frame will be followed by a non-filtered
3407240639Sadrian	 * (complete or otherwise) frame.
3408240639Sadrian	 *
3409240639Sadrian	 * XXX should we do this before we complete the frame?
3410240639Sadrian	 */
3411240639Sadrian	if (atid->isfiltered)
3412240639Sadrian		ath_tx_tid_filt_comp_complete(sc, atid);
3413240639Sadrian
3414227364Sadrian	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3415227364Sadrian
3416227364Sadrian	/*
3417227364Sadrian	 * punt to rate control if we're not being cleaned up
3418227364Sadrian	 * during a hw queue drain and the frame wanted an ACK.
3419227364Sadrian	 */
3420233966Sadrian	if (fail == 0 && ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0))
3421227364Sadrian		ath_tx_update_ratectrl(sc, ni, bf->bf_state.bfs_rc,
3422227364Sadrian		    ts, bf->bf_state.bfs_pktlen,
3423227364Sadrian		    1, (ts->ts_status == 0) ? 0 : 1);
3424227364Sadrian
3425227364Sadrian	ath_tx_default_comp(sc, bf, fail);
3426227364Sadrian}
3427227364Sadrian
3428227364Sadrian/*
3429227364Sadrian * Handle cleanup of aggregate session packets that aren't
3430227364Sadrian * an A-MPDU.
3431227364Sadrian *
3432227364Sadrian * There's no need to update the BAW here - the session is being
3433227364Sadrian * torn down.
3434227364Sadrian */
3435227364Sadrianstatic void
3436227364Sadrianath_tx_comp_cleanup_unaggr(struct ath_softc *sc, struct ath_buf *bf)
3437227364Sadrian{
3438227364Sadrian	struct ieee80211_node *ni = bf->bf_node;
3439227364Sadrian	struct ath_node *an = ATH_NODE(ni);
3440227364Sadrian	int tid = bf->bf_state.bfs_tid;
3441227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
3442227364Sadrian
3443227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: TID %d: incomp=%d\n",
3444227364Sadrian	    __func__, tid, atid->incomp);
3445227364Sadrian
3446227364Sadrian	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
3447227364Sadrian	atid->incomp--;
3448227364Sadrian	if (atid->incomp == 0) {
3449227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
3450227364Sadrian		    "%s: TID %d: cleaned up! resume!\n",
3451227364Sadrian		    __func__, tid);
3452227364Sadrian		atid->cleanup_inprogress = 0;
3453227364Sadrian		ath_tx_tid_resume(sc, atid);
3454227364Sadrian	}
3455227364Sadrian	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3456227364Sadrian
3457227364Sadrian	ath_tx_default_comp(sc, bf, 0);
3458227364Sadrian}
3459227364Sadrian
3460227364Sadrian/*
3461227364Sadrian * Performs transmit side cleanup when TID changes from aggregated to
3462227364Sadrian * unaggregated.
3463227364Sadrian *
3464227364Sadrian * - Discard all retry frames from the s/w queue.
3465227364Sadrian * - Fix the tx completion function for all buffers in s/w queue.
3466227364Sadrian * - Count the number of unacked frames, and let transmit completion
3467227364Sadrian *   handle it later.
3468227364Sadrian *
3469227364Sadrian * The caller is responsible for pausing the TID.
3470227364Sadrian */
3471227364Sadrianstatic void
3472235749Sadrianath_tx_tid_cleanup(struct ath_softc *sc, struct ath_node *an, int tid)
3473227364Sadrian{
3474227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
3475227364Sadrian	struct ieee80211_tx_ampdu *tap;
3476227364Sadrian	struct ath_buf *bf, *bf_next;
3477227364Sadrian	ath_bufhead bf_cq;
3478227364Sadrian
3479235774Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAW,
3480227364Sadrian	    "%s: TID %d: called\n", __func__, tid);
3481227364Sadrian
3482227364Sadrian	TAILQ_INIT(&bf_cq);
3483227364Sadrian	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
3484227364Sadrian
3485227364Sadrian	/*
3486240639Sadrian	 * Move the filtered frames to the TX queue, before
3487240639Sadrian	 * we run off and discard/process things.
3488240639Sadrian	 */
3489240639Sadrian	/* XXX this is really quite inefficient */
3490240639Sadrian	while ((bf = TAILQ_LAST(&atid->filtq.axq_q, ath_bufhead_s)) != NULL) {
3491240639Sadrian		ATH_TXQ_REMOVE(&atid->filtq, bf, bf_list);
3492240639Sadrian		ATH_TXQ_INSERT_HEAD(atid, bf, bf_list);
3493240639Sadrian	}
3494240639Sadrian
3495240639Sadrian	/*
3496227364Sadrian	 * Update the frames in the software TX queue:
3497227364Sadrian	 *
3498227364Sadrian	 * + Discard retry frames in the queue
3499227364Sadrian	 * + Fix the completion function to be non-aggregate
3500227364Sadrian	 */
3501227364Sadrian	bf = TAILQ_FIRST(&atid->axq_q);
3502227364Sadrian	while (bf) {
3503227364Sadrian		if (bf->bf_state.bfs_isretried) {
3504227364Sadrian			bf_next = TAILQ_NEXT(bf, bf_list);
3505227364Sadrian			TAILQ_REMOVE(&atid->axq_q, bf, bf_list);
3506227364Sadrian			atid->axq_depth--;
3507227364Sadrian			if (bf->bf_state.bfs_dobaw) {
3508227364Sadrian				ath_tx_update_baw(sc, an, atid, bf);
3509227364Sadrian				if (! bf->bf_state.bfs_addedbaw)
3510227364Sadrian					device_printf(sc->sc_dev,
3511227364Sadrian					    "%s: wasn't added: seqno %d\n",
3512229949Sadrian					    __func__,
3513229949Sadrian					    SEQNO(bf->bf_state.bfs_seqno));
3514227364Sadrian			}
3515227364Sadrian			bf->bf_state.bfs_dobaw = 0;
3516227364Sadrian			/*
3517227364Sadrian			 * Call the default completion handler with "fail" just
3518227364Sadrian			 * so upper levels are suitably notified about this.
3519227364Sadrian			 */
3520227364Sadrian			TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
3521227364Sadrian			bf = bf_next;
3522227364Sadrian			continue;
3523227364Sadrian		}
3524227364Sadrian		/* Give these the default completion handler */
3525227364Sadrian		bf->bf_comp = ath_tx_normal_comp;
3526227364Sadrian		bf = TAILQ_NEXT(bf, bf_list);
3527227364Sadrian	}
3528227364Sadrian
3529227364Sadrian	/* The caller is required to pause the TID */
3530227364Sadrian#if 0
3531227364Sadrian	/* Pause the TID */
3532227364Sadrian	ath_tx_tid_pause(sc, atid);
3533227364Sadrian#endif
3534227364Sadrian
3535227364Sadrian	/*
3536227364Sadrian	 * Calculate what hardware-queued frames exist based
3537227364Sadrian	 * on the current BAW size. Ie, what frames have been
3538227364Sadrian	 * added to the TX hardware queue for this TID but
3539227364Sadrian	 * not yet ACKed.
3540227364Sadrian	 */
3541227364Sadrian	tap = ath_tx_get_tx_tid(an, tid);
3542227364Sadrian	/* Need the lock - fiddling with BAW */
3543227364Sadrian	while (atid->baw_head != atid->baw_tail) {
3544227364Sadrian		if (atid->tx_buf[atid->baw_head]) {
3545227364Sadrian			atid->incomp++;
3546227364Sadrian			atid->cleanup_inprogress = 1;
3547227364Sadrian			atid->tx_buf[atid->baw_head] = NULL;
3548227364Sadrian		}
3549227364Sadrian		INCR(atid->baw_head, ATH_TID_MAX_BUFS);
3550227364Sadrian		INCR(tap->txa_start, IEEE80211_SEQ_RANGE);
3551227364Sadrian	}
3552227364Sadrian
3553227364Sadrian	/*
3554227364Sadrian	 * If cleanup is required, defer TID scheduling
3555227364Sadrian	 * until all the HW queued packets have been
3556227364Sadrian	 * sent.
3557227364Sadrian	 */
3558227364Sadrian	if (! atid->cleanup_inprogress)
3559227364Sadrian		ath_tx_tid_resume(sc, atid);
3560227364Sadrian
3561227364Sadrian	if (atid->cleanup_inprogress)
3562227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
3563227364Sadrian		    "%s: TID %d: cleanup needed: %d packets\n",
3564227364Sadrian		    __func__, tid, atid->incomp);
3565227364Sadrian	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3566227364Sadrian
3567227364Sadrian	/* Handle completing frames and fail them */
3568227364Sadrian	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
3569227364Sadrian		TAILQ_REMOVE(&bf_cq, bf, bf_list);
3570227364Sadrian		ath_tx_default_comp(sc, bf, 1);
3571227364Sadrian	}
3572227364Sadrian}
3573227364Sadrian
3574227364Sadrianstatic struct ath_buf *
3575227398Sadrianath_tx_retry_clone(struct ath_softc *sc, struct ath_node *an,
3576227398Sadrian    struct ath_tid *tid, struct ath_buf *bf)
3577227364Sadrian{
3578227364Sadrian	struct ath_buf *nbf;
3579227364Sadrian	int error;
3580227364Sadrian
3581227364Sadrian	nbf = ath_buf_clone(sc, bf);
3582227364Sadrian
3583227364Sadrian#if 0
3584227364Sadrian	device_printf(sc->sc_dev, "%s: ATH_BUF_BUSY; cloning\n",
3585227364Sadrian	    __func__);
3586227364Sadrian#endif
3587227364Sadrian
3588227364Sadrian	if (nbf == NULL) {
3589227364Sadrian		/* Failed to clone */
3590227364Sadrian		device_printf(sc->sc_dev,
3591227364Sadrian		    "%s: failed to clone a busy buffer\n",
3592227364Sadrian		    __func__);
3593227364Sadrian		return NULL;
3594227364Sadrian	}
3595227364Sadrian
3596227364Sadrian	/* Setup the dma for the new buffer */
3597227364Sadrian	error = ath_tx_dmasetup(sc, nbf, nbf->bf_m);
3598227364Sadrian	if (error != 0) {
3599227364Sadrian		device_printf(sc->sc_dev,
3600227364Sadrian		    "%s: failed to setup dma for clone\n",
3601227364Sadrian		    __func__);
3602227364Sadrian		/*
3603227364Sadrian		 * Put this at the head of the list, not tail;
3604227364Sadrian		 * that way it doesn't interfere with the
3605227364Sadrian		 * busy buffer logic (which uses the tail of
3606227364Sadrian		 * the list.)
3607227364Sadrian		 */
3608227364Sadrian		ATH_TXBUF_LOCK(sc);
3609236994Sadrian		ath_returnbuf_head(sc, nbf);
3610227364Sadrian		ATH_TXBUF_UNLOCK(sc);
3611227364Sadrian		return NULL;
3612227364Sadrian	}
3613227364Sadrian
3614227398Sadrian	/* Update BAW if required, before we free the original buf */
3615227398Sadrian	if (bf->bf_state.bfs_dobaw)
3616227398Sadrian		ath_tx_switch_baw_buf(sc, an, tid, bf, nbf);
3617227398Sadrian
3618227364Sadrian	/* Free current buffer; return the older buffer */
3619227364Sadrian	bf->bf_m = NULL;
3620227364Sadrian	bf->bf_node = NULL;
3621227364Sadrian	ath_freebuf(sc, bf);
3622240639Sadrian
3623227364Sadrian	return nbf;
3624227364Sadrian}
3625227364Sadrian
3626227364Sadrian/*
3627227364Sadrian * Handle retrying an unaggregate frame in an aggregate
3628227364Sadrian * session.
3629227364Sadrian *
3630227364Sadrian * If too many retries occur, pause the TID, wait for
3631227364Sadrian * any further retransmits (as there's no reason why
3632227364Sadrian * non-aggregate frames in an aggregate session are
3633227364Sadrian * transmitted in-order; they just have to be in-BAW)
3634227364Sadrian * and then queue a BAR.
3635227364Sadrian */
3636227364Sadrianstatic void
3637227364Sadrianath_tx_aggr_retry_unaggr(struct ath_softc *sc, struct ath_buf *bf)
3638227364Sadrian{
3639227364Sadrian	struct ieee80211_node *ni = bf->bf_node;
3640227364Sadrian	struct ath_node *an = ATH_NODE(ni);
3641227364Sadrian	int tid = bf->bf_state.bfs_tid;
3642227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
3643227364Sadrian	struct ieee80211_tx_ampdu *tap;
3644227364Sadrian
3645227364Sadrian	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
3646227364Sadrian
3647227364Sadrian	tap = ath_tx_get_tx_tid(an, tid);
3648227364Sadrian
3649227364Sadrian	/*
3650227364Sadrian	 * If the buffer is marked as busy, we can't directly
3651227364Sadrian	 * reuse it. Instead, try to clone the buffer.
3652227364Sadrian	 * If the clone is successful, recycle the old buffer.
3653227364Sadrian	 * If the clone is unsuccessful, set bfs_retries to max
3654227364Sadrian	 * to force the next bit of code to free the buffer
3655227364Sadrian	 * for us.
3656227364Sadrian	 */
3657227364Sadrian	if ((bf->bf_state.bfs_retries < SWMAX_RETRIES) &&
3658227364Sadrian	    (bf->bf_flags & ATH_BUF_BUSY)) {
3659227364Sadrian		struct ath_buf *nbf;
3660227398Sadrian		nbf = ath_tx_retry_clone(sc, an, atid, bf);
3661227364Sadrian		if (nbf)
3662227364Sadrian			/* bf has been freed at this point */
3663227364Sadrian			bf = nbf;
3664227364Sadrian		else
3665227364Sadrian			bf->bf_state.bfs_retries = SWMAX_RETRIES + 1;
3666227364Sadrian	}
3667227364Sadrian
3668227364Sadrian	if (bf->bf_state.bfs_retries >= SWMAX_RETRIES) {
3669227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_RETRIES,
3670227364Sadrian		    "%s: exceeded retries; seqno %d\n",
3671227364Sadrian		    __func__, SEQNO(bf->bf_state.bfs_seqno));
3672227364Sadrian		sc->sc_stats.ast_tx_swretrymax++;
3673227364Sadrian
3674227364Sadrian		/* Update BAW anyway */
3675227364Sadrian		if (bf->bf_state.bfs_dobaw) {
3676227364Sadrian			ath_tx_update_baw(sc, an, atid, bf);
3677227364Sadrian			if (! bf->bf_state.bfs_addedbaw)
3678227364Sadrian				device_printf(sc->sc_dev,
3679227364Sadrian				    "%s: wasn't added: seqno %d\n",
3680227364Sadrian				    __func__, SEQNO(bf->bf_state.bfs_seqno));
3681227364Sadrian		}
3682227364Sadrian		bf->bf_state.bfs_dobaw = 0;
3683227364Sadrian
3684233908Sadrian		/* Suspend the TX queue and get ready to send the BAR */
3685233908Sadrian		ath_tx_tid_bar_suspend(sc, atid);
3686227364Sadrian
3687233908Sadrian		/* Send the BAR if there are no other frames waiting */
3688233908Sadrian		if (ath_tx_tid_bar_tx_ready(sc, atid))
3689233908Sadrian			ath_tx_tid_bar_tx(sc, atid);
3690227364Sadrian
3691233908Sadrian		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3692227364Sadrian
3693227364Sadrian		/* Free buffer, bf is free after this call */
3694227364Sadrian		ath_tx_default_comp(sc, bf, 0);
3695227364Sadrian		return;
3696227364Sadrian	}
3697227364Sadrian
3698227364Sadrian	/*
3699227364Sadrian	 * This increments the retry counter as well as
3700227364Sadrian	 * sets the retry flag in the ath_buf and packet
3701227364Sadrian	 * body.
3702227364Sadrian	 */
3703227364Sadrian	ath_tx_set_retry(sc, bf);
3704240639Sadrian	sc->sc_stats.ast_tx_swretries++;
3705227364Sadrian
3706227364Sadrian	/*
3707227364Sadrian	 * Insert this at the head of the queue, so it's
3708227364Sadrian	 * retried before any current/subsequent frames.
3709227364Sadrian	 */
3710227364Sadrian	ATH_TXQ_INSERT_HEAD(atid, bf, bf_list);
3711227364Sadrian	ath_tx_tid_sched(sc, atid);
3712233908Sadrian	/* Send the BAR if there are no other frames waiting */
3713233908Sadrian	if (ath_tx_tid_bar_tx_ready(sc, atid))
3714233908Sadrian		ath_tx_tid_bar_tx(sc, atid);
3715227364Sadrian
3716227364Sadrian	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3717227364Sadrian}
3718227364Sadrian
3719227364Sadrian/*
3720227364Sadrian * Common code for aggregate excessive retry/subframe retry.
3721227364Sadrian * If retrying, queues buffers to bf_q. If not, frees the
3722227364Sadrian * buffers.
3723227364Sadrian *
3724227364Sadrian * XXX should unify this with ath_tx_aggr_retry_unaggr()
3725227364Sadrian */
3726227364Sadrianstatic int
3727227364Sadrianath_tx_retry_subframe(struct ath_softc *sc, struct ath_buf *bf,
3728227364Sadrian    ath_bufhead *bf_q)
3729227364Sadrian{
3730227364Sadrian	struct ieee80211_node *ni = bf->bf_node;
3731227364Sadrian	struct ath_node *an = ATH_NODE(ni);
3732227364Sadrian	int tid = bf->bf_state.bfs_tid;
3733227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
3734227364Sadrian
3735227364Sadrian	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[atid->ac]);
3736227364Sadrian
3737240255Sadrian	/* XXX clr11naggr should be done for all subframes */
3738227364Sadrian	ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
3739227364Sadrian	ath_hal_set11nburstduration(sc->sc_ah, bf->bf_desc, 0);
3740240639Sadrian
3741227364Sadrian	/* ath_hal_set11n_virtualmorefrag(sc->sc_ah, bf->bf_desc, 0); */
3742227364Sadrian
3743227364Sadrian	/*
3744227364Sadrian	 * If the buffer is marked as busy, we can't directly
3745227364Sadrian	 * reuse it. Instead, try to clone the buffer.
3746227364Sadrian	 * If the clone is successful, recycle the old buffer.
3747227364Sadrian	 * If the clone is unsuccessful, set bfs_retries to max
3748227364Sadrian	 * to force the next bit of code to free the buffer
3749227364Sadrian	 * for us.
3750227364Sadrian	 */
3751227364Sadrian	if ((bf->bf_state.bfs_retries < SWMAX_RETRIES) &&
3752227364Sadrian	    (bf->bf_flags & ATH_BUF_BUSY)) {
3753227364Sadrian		struct ath_buf *nbf;
3754227398Sadrian		nbf = ath_tx_retry_clone(sc, an, atid, bf);
3755227364Sadrian		if (nbf)
3756227364Sadrian			/* bf has been freed at this point */
3757227364Sadrian			bf = nbf;
3758227364Sadrian		else
3759227364Sadrian			bf->bf_state.bfs_retries = SWMAX_RETRIES + 1;
3760227364Sadrian	}
3761227364Sadrian
3762227364Sadrian	if (bf->bf_state.bfs_retries >= SWMAX_RETRIES) {
3763227364Sadrian		sc->sc_stats.ast_tx_swretrymax++;
3764227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_RETRIES,
3765227364Sadrian		    "%s: max retries: seqno %d\n",
3766227364Sadrian		    __func__, SEQNO(bf->bf_state.bfs_seqno));
3767227364Sadrian		ath_tx_update_baw(sc, an, atid, bf);
3768227364Sadrian		if (! bf->bf_state.bfs_addedbaw)
3769227364Sadrian			device_printf(sc->sc_dev,
3770227364Sadrian			    "%s: wasn't added: seqno %d\n",
3771227364Sadrian			    __func__, SEQNO(bf->bf_state.bfs_seqno));
3772227364Sadrian		bf->bf_state.bfs_dobaw = 0;
3773227364Sadrian		return 1;
3774227364Sadrian	}
3775227364Sadrian
3776227364Sadrian	ath_tx_set_retry(sc, bf);
3777240639Sadrian	sc->sc_stats.ast_tx_swretries++;
3778227364Sadrian	bf->bf_next = NULL;		/* Just to make sure */
3779227364Sadrian
3780240255Sadrian	/* Clear the aggregate state */
3781240255Sadrian	bf->bf_state.bfs_aggr = 0;
3782240255Sadrian	bf->bf_state.bfs_ndelim = 0;	/* ??? needed? */
3783240255Sadrian	bf->bf_state.bfs_nframes = 1;
3784240255Sadrian
3785227364Sadrian	TAILQ_INSERT_TAIL(bf_q, bf, bf_list);
3786227364Sadrian	return 0;
3787227364Sadrian}
3788227364Sadrian
3789227364Sadrian/*
3790227364Sadrian * error pkt completion for an aggregate destination
3791227364Sadrian */
3792227364Sadrianstatic void
3793227364Sadrianath_tx_comp_aggr_error(struct ath_softc *sc, struct ath_buf *bf_first,
3794227364Sadrian    struct ath_tid *tid)
3795227364Sadrian{
3796227364Sadrian	struct ieee80211_node *ni = bf_first->bf_node;
3797227364Sadrian	struct ath_node *an = ATH_NODE(ni);
3798227364Sadrian	struct ath_buf *bf_next, *bf;
3799227364Sadrian	ath_bufhead bf_q;
3800227364Sadrian	int drops = 0;
3801227364Sadrian	struct ieee80211_tx_ampdu *tap;
3802227364Sadrian	ath_bufhead bf_cq;
3803227364Sadrian
3804227364Sadrian	TAILQ_INIT(&bf_q);
3805227364Sadrian	TAILQ_INIT(&bf_cq);
3806227364Sadrian
3807227364Sadrian	/*
3808227364Sadrian	 * Update rate control - all frames have failed.
3809227364Sadrian	 *
3810227364Sadrian	 * XXX use the length in the first frame in the series;
3811227364Sadrian	 * XXX just so things are consistent for now.
3812227364Sadrian	 */
3813227364Sadrian	ath_tx_update_ratectrl(sc, ni, bf_first->bf_state.bfs_rc,
3814227364Sadrian	    &bf_first->bf_status.ds_txstat,
3815227364Sadrian	    bf_first->bf_state.bfs_pktlen,
3816227364Sadrian	    bf_first->bf_state.bfs_nframes, bf_first->bf_state.bfs_nframes);
3817227364Sadrian
3818227364Sadrian	ATH_TXQ_LOCK(sc->sc_ac2q[tid->ac]);
3819227364Sadrian	tap = ath_tx_get_tx_tid(an, tid->tid);
3820227868Sadrian	sc->sc_stats.ast_tx_aggr_failall++;
3821227364Sadrian
3822227364Sadrian	/* Retry all subframes */
3823227364Sadrian	bf = bf_first;
3824227364Sadrian	while (bf) {
3825227364Sadrian		bf_next = bf->bf_next;
3826227364Sadrian		bf->bf_next = NULL;	/* Remove it from the aggr list */
3827227868Sadrian		sc->sc_stats.ast_tx_aggr_fail++;
3828227364Sadrian		if (ath_tx_retry_subframe(sc, bf, &bf_q)) {
3829227364Sadrian			drops++;
3830227364Sadrian			bf->bf_next = NULL;
3831227364Sadrian			TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
3832227364Sadrian		}
3833227364Sadrian		bf = bf_next;
3834227364Sadrian	}
3835227364Sadrian
3836227364Sadrian	/* Prepend all frames to the beginning of the queue */
3837227364Sadrian	while ((bf = TAILQ_LAST(&bf_q, ath_bufhead_s)) != NULL) {
3838227364Sadrian		TAILQ_REMOVE(&bf_q, bf, bf_list);
3839227364Sadrian		ATH_TXQ_INSERT_HEAD(tid, bf, bf_list);
3840227364Sadrian	}
3841227364Sadrian
3842234725Sadrian	/*
3843234725Sadrian	 * Schedule the TID to be re-tried.
3844234725Sadrian	 */
3845227364Sadrian	ath_tx_tid_sched(sc, tid);
3846227364Sadrian
3847227364Sadrian	/*
3848227364Sadrian	 * send bar if we dropped any frames
3849227364Sadrian	 *
3850227364Sadrian	 * Keep the txq lock held for now, as we need to ensure
3851227364Sadrian	 * that ni_txseqs[] is consistent (as it's being updated
3852227364Sadrian	 * in the ifnet TX context or raw TX context.)
3853227364Sadrian	 */
3854227364Sadrian	if (drops) {
3855233908Sadrian		/* Suspend the TX queue and get ready to send the BAR */
3856233908Sadrian		ath_tx_tid_bar_suspend(sc, tid);
3857227364Sadrian	}
3858227364Sadrian
3859233908Sadrian	/*
3860233908Sadrian	 * Send BAR if required
3861233908Sadrian	 */
3862233908Sadrian	if (ath_tx_tid_bar_tx_ready(sc, tid))
3863233908Sadrian		ath_tx_tid_bar_tx(sc, tid);
3864240639Sadrian
3865233908Sadrian	ATH_TXQ_UNLOCK(sc->sc_ac2q[tid->ac]);
3866233908Sadrian
3867227364Sadrian	/* Complete frames which errored out */
3868227364Sadrian	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
3869227364Sadrian		TAILQ_REMOVE(&bf_cq, bf, bf_list);
3870227364Sadrian		ath_tx_default_comp(sc, bf, 0);
3871227364Sadrian	}
3872227364Sadrian}
3873227364Sadrian
3874227364Sadrian/*
3875227364Sadrian * Handle clean-up of packets from an aggregate list.
3876227364Sadrian *
3877227364Sadrian * There's no need to update the BAW here - the session is being
3878227364Sadrian * torn down.
3879227364Sadrian */
3880227364Sadrianstatic void
3881227364Sadrianath_tx_comp_cleanup_aggr(struct ath_softc *sc, struct ath_buf *bf_first)
3882227364Sadrian{
3883227364Sadrian	struct ath_buf *bf, *bf_next;
3884227364Sadrian	struct ieee80211_node *ni = bf_first->bf_node;
3885227364Sadrian	struct ath_node *an = ATH_NODE(ni);
3886227364Sadrian	int tid = bf_first->bf_state.bfs_tid;
3887227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
3888227364Sadrian
3889227364Sadrian	bf = bf_first;
3890227364Sadrian
3891227364Sadrian	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
3892227364Sadrian
3893227364Sadrian	/* update incomp */
3894227364Sadrian	while (bf) {
3895227364Sadrian		atid->incomp--;
3896227364Sadrian		bf = bf->bf_next;
3897227364Sadrian	}
3898227364Sadrian
3899227364Sadrian	if (atid->incomp == 0) {
3900227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
3901227364Sadrian		    "%s: TID %d: cleaned up! resume!\n",
3902227364Sadrian		    __func__, tid);
3903227364Sadrian		atid->cleanup_inprogress = 0;
3904227364Sadrian		ath_tx_tid_resume(sc, atid);
3905227364Sadrian	}
3906233908Sadrian
3907233908Sadrian	/* Send BAR if required */
3908240639Sadrian	/* XXX why would we send a BAR when transitioning to non-aggregation? */
3909233908Sadrian	if (ath_tx_tid_bar_tx_ready(sc, atid))
3910233908Sadrian		ath_tx_tid_bar_tx(sc, atid);
3911240639Sadrian
3912227364Sadrian	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3913227364Sadrian
3914227364Sadrian	/* Handle frame completion */
3915227364Sadrian	while (bf) {
3916227364Sadrian		bf_next = bf->bf_next;
3917227364Sadrian		ath_tx_default_comp(sc, bf, 1);
3918227364Sadrian		bf = bf_next;
3919227364Sadrian	}
3920227364Sadrian}
3921227364Sadrian
3922227364Sadrian/*
3923227364Sadrian * Handle completion of an set of aggregate frames.
3924227364Sadrian *
3925227364Sadrian * XXX for now, simply complete each sub-frame.
3926227364Sadrian *
3927227364Sadrian * Note: the completion handler is the last descriptor in the aggregate,
3928227364Sadrian * not the last descriptor in the first frame.
3929227364Sadrian */
3930227364Sadrianstatic void
3931229949Sadrianath_tx_aggr_comp_aggr(struct ath_softc *sc, struct ath_buf *bf_first,
3932229949Sadrian    int fail)
3933227364Sadrian{
3934227364Sadrian	//struct ath_desc *ds = bf->bf_lastds;
3935227364Sadrian	struct ieee80211_node *ni = bf_first->bf_node;
3936227364Sadrian	struct ath_node *an = ATH_NODE(ni);
3937227364Sadrian	int tid = bf_first->bf_state.bfs_tid;
3938227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
3939227364Sadrian	struct ath_tx_status ts;
3940227364Sadrian	struct ieee80211_tx_ampdu *tap;
3941227364Sadrian	ath_bufhead bf_q;
3942227364Sadrian	ath_bufhead bf_cq;
3943227364Sadrian	int seq_st, tx_ok;
3944227364Sadrian	int hasba, isaggr;
3945227364Sadrian	uint32_t ba[2];
3946227364Sadrian	struct ath_buf *bf, *bf_next;
3947227364Sadrian	int ba_index;
3948227364Sadrian	int drops = 0;
3949227364Sadrian	int nframes = 0, nbad = 0, nf;
3950227364Sadrian	int pktlen;
3951227364Sadrian	/* XXX there's too much on the stack? */
3952236038Sadrian	struct ath_rc_series rc[ATH_RC_NUM];
3953227364Sadrian	int txseq;
3954227364Sadrian
3955227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: called; hwq_depth=%d\n",
3956227364Sadrian	    __func__, atid->hwq_depth);
3957227364Sadrian
3958240639Sadrian	TAILQ_INIT(&bf_q);
3959240639Sadrian	TAILQ_INIT(&bf_cq);
3960240639Sadrian
3961227364Sadrian	/* The TID state is kept behind the TXQ lock */
3962227364Sadrian	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
3963227364Sadrian
3964227364Sadrian	atid->hwq_depth--;
3965227364Sadrian	if (atid->hwq_depth < 0)
3966227364Sadrian		device_printf(sc->sc_dev, "%s: hwq_depth < 0: %d\n",
3967227364Sadrian		    __func__, atid->hwq_depth);
3968227364Sadrian
3969227364Sadrian	/*
3970240639Sadrian	 * If the TID is filtered, handle completing the filter
3971240639Sadrian	 * transition before potentially kicking it to the cleanup
3972240639Sadrian	 * function.
3973240639Sadrian	 */
3974240639Sadrian	if (atid->isfiltered)
3975240639Sadrian		ath_tx_tid_filt_comp_complete(sc, atid);
3976240639Sadrian
3977240639Sadrian	/*
3978227364Sadrian	 * Punt cleanup to the relevant function, not our problem now
3979227364Sadrian	 */
3980227364Sadrian	if (atid->cleanup_inprogress) {
3981240639Sadrian		if (atid->isfiltered)
3982240639Sadrian			device_printf(sc->sc_dev,
3983240639Sadrian			    "%s: isfiltered=1, normal_comp?\n",
3984240639Sadrian			    __func__);
3985227364Sadrian		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
3986227364Sadrian		ath_tx_comp_cleanup_aggr(sc, bf_first);
3987227364Sadrian		return;
3988227364Sadrian	}
3989227364Sadrian
3990227364Sadrian	/*
3991240639Sadrian	 * If the frame is filtered, transition to filtered frame
3992240639Sadrian	 * mode and add this to the filtered frame list.
3993240639Sadrian	 *
3994240639Sadrian	 * XXX TODO: figure out how this interoperates with
3995240639Sadrian	 * BAR, pause and cleanup states.
3996240639Sadrian	 */
3997240639Sadrian	if ((ts.ts_status & HAL_TXERR_FILT) ||
3998240639Sadrian	    (ts.ts_status != 0 && atid->isfiltered)) {
3999240639Sadrian		if (fail != 0)
4000240639Sadrian			device_printf(sc->sc_dev,
4001240639Sadrian			    "%s: isfiltered=1, fail=%d\n", __func__, fail);
4002240639Sadrian		ath_tx_tid_filt_comp_aggr(sc, atid, bf_first, &bf_cq);
4003240639Sadrian
4004240639Sadrian		/* Remove from BAW */
4005240639Sadrian		TAILQ_FOREACH_SAFE(bf, &bf_cq, bf_list, bf_next) {
4006240639Sadrian			if (bf->bf_state.bfs_addedbaw)
4007240639Sadrian				drops++;
4008240639Sadrian			if (bf->bf_state.bfs_dobaw) {
4009240639Sadrian				ath_tx_update_baw(sc, an, atid, bf);
4010240639Sadrian				if (! bf->bf_state.bfs_addedbaw)
4011240639Sadrian					device_printf(sc->sc_dev,
4012240639Sadrian					    "%s: wasn't added: seqno %d\n",
4013240639Sadrian					    __func__,
4014240639Sadrian					    SEQNO(bf->bf_state.bfs_seqno));
4015240639Sadrian			}
4016240639Sadrian			bf->bf_state.bfs_dobaw = 0;
4017240639Sadrian		}
4018240639Sadrian		/*
4019240639Sadrian		 * If any intermediate frames in the BAW were dropped when
4020240639Sadrian		 * handling filtering things, send a BAR.
4021240639Sadrian		 */
4022240639Sadrian		if (drops)
4023240639Sadrian			ath_tx_tid_bar_suspend(sc, atid);
4024240639Sadrian
4025240639Sadrian		/*
4026240639Sadrian		 * Finish up by sending a BAR if required and freeing
4027240639Sadrian		 * the frames outside of the TX lock.
4028240639Sadrian		 */
4029240639Sadrian		goto finish_send_bar;
4030240639Sadrian	}
4031240639Sadrian
4032240639Sadrian	/*
4033227364Sadrian	 * Take a copy; this may be needed -after- bf_first
4034227364Sadrian	 * has been completed and freed.
4035227364Sadrian	 */
4036227364Sadrian	ts = bf_first->bf_status.ds_txstat;
4037227364Sadrian	/*
4038227364Sadrian	 * XXX for now, use the first frame in the aggregate for
4039227364Sadrian	 * XXX rate control completion; it's at least consistent.
4040227364Sadrian	 */
4041227364Sadrian	pktlen = bf_first->bf_state.bfs_pktlen;
4042227364Sadrian
4043227364Sadrian	/*
4044235461Sadrian	 * Handle errors first!
4045235461Sadrian	 *
4046235461Sadrian	 * Here, handle _any_ error as a "exceeded retries" error.
4047235461Sadrian	 * Later on (when filtered frames are to be specially handled)
4048235461Sadrian	 * it'll have to be expanded.
4049227364Sadrian	 */
4050235461Sadrian#if 0
4051227364Sadrian	if (ts.ts_status & HAL_TXERR_XRETRY) {
4052235461Sadrian#endif
4053235461Sadrian	if (ts.ts_status != 0) {
4054227364Sadrian		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4055227364Sadrian		ath_tx_comp_aggr_error(sc, bf_first, atid);
4056227364Sadrian		return;
4057227364Sadrian	}
4058227364Sadrian
4059227364Sadrian	tap = ath_tx_get_tx_tid(an, tid);
4060227364Sadrian
4061227364Sadrian	/*
4062227364Sadrian	 * extract starting sequence and block-ack bitmap
4063227364Sadrian	 */
4064227364Sadrian	/* XXX endian-ness of seq_st, ba? */
4065227364Sadrian	seq_st = ts.ts_seqnum;
4066227364Sadrian	hasba = !! (ts.ts_flags & HAL_TX_BA);
4067227364Sadrian	tx_ok = (ts.ts_status == 0);
4068227364Sadrian	isaggr = bf_first->bf_state.bfs_aggr;
4069227364Sadrian	ba[0] = ts.ts_ba_low;
4070227364Sadrian	ba[1] = ts.ts_ba_high;
4071227364Sadrian
4072227364Sadrian	/*
4073227364Sadrian	 * Copy the TX completion status and the rate control
4074227364Sadrian	 * series from the first descriptor, as it may be freed
4075227364Sadrian	 * before the rate control code can get its grubby fingers
4076227364Sadrian	 * into things.
4077227364Sadrian	 */
4078227364Sadrian	memcpy(rc, bf_first->bf_state.bfs_rc, sizeof(rc));
4079227364Sadrian
4080227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4081229949Sadrian	    "%s: txa_start=%d, tx_ok=%d, status=%.8x, flags=%.8x, "
4082229949Sadrian	    "isaggr=%d, seq_st=%d, hasba=%d, ba=%.8x, %.8x\n",
4083227364Sadrian	    __func__, tap->txa_start, tx_ok, ts.ts_status, ts.ts_flags,
4084227364Sadrian	    isaggr, seq_st, hasba, ba[0], ba[1]);
4085227364Sadrian
4086227364Sadrian	/* Occasionally, the MAC sends a tx status for the wrong TID. */
4087227364Sadrian	if (tid != ts.ts_tid) {
4088227364Sadrian		device_printf(sc->sc_dev, "%s: tid %d != hw tid %d\n",
4089227364Sadrian		    __func__, tid, ts.ts_tid);
4090227364Sadrian		tx_ok = 0;
4091227364Sadrian	}
4092227364Sadrian
4093227364Sadrian	/* AR5416 BA bug; this requires an interface reset */
4094227364Sadrian	if (isaggr && tx_ok && (! hasba)) {
4095227364Sadrian		device_printf(sc->sc_dev,
4096229949Sadrian		    "%s: AR5416 bug: hasba=%d; txok=%d, isaggr=%d, "
4097229949Sadrian		    "seq_st=%d\n",
4098227364Sadrian		    __func__, hasba, tx_ok, isaggr, seq_st);
4099227364Sadrian		/* XXX TODO: schedule an interface reset */
4100238350Sjhb#ifdef ATH_DEBUG
4101238338Sadrian		ath_printtxbuf(sc, bf_first,
4102238338Sadrian		    sc->sc_ac2q[atid->ac]->axq_qnum, 0, 0);
4103238350Sjhb#endif
4104227364Sadrian	}
4105227364Sadrian
4106227364Sadrian	/*
4107227364Sadrian	 * Walk the list of frames, figure out which ones were correctly
4108227364Sadrian	 * sent and which weren't.
4109227364Sadrian	 */
4110227364Sadrian	bf = bf_first;
4111227364Sadrian	nf = bf_first->bf_state.bfs_nframes;
4112227364Sadrian
4113227364Sadrian	/* bf_first is going to be invalid once this list is walked */
4114227364Sadrian	bf_first = NULL;
4115227364Sadrian
4116227364Sadrian	/*
4117227364Sadrian	 * Walk the list of completed frames and determine
4118227364Sadrian	 * which need to be completed and which need to be
4119227364Sadrian	 * retransmitted.
4120227364Sadrian	 *
4121227364Sadrian	 * For completed frames, the completion functions need
4122227364Sadrian	 * to be called at the end of this function as the last
4123227364Sadrian	 * node reference may free the node.
4124227364Sadrian	 *
4125227364Sadrian	 * Finally, since the TXQ lock can't be held during the
4126227364Sadrian	 * completion callback (to avoid lock recursion),
4127227364Sadrian	 * the completion calls have to be done outside of the
4128227364Sadrian	 * lock.
4129227364Sadrian	 */
4130227364Sadrian	while (bf) {
4131227364Sadrian		nframes++;
4132229949Sadrian		ba_index = ATH_BA_INDEX(seq_st,
4133229949Sadrian		    SEQNO(bf->bf_state.bfs_seqno));
4134227364Sadrian		bf_next = bf->bf_next;
4135227364Sadrian		bf->bf_next = NULL;	/* Remove it from the aggr list */
4136227364Sadrian
4137227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4138227364Sadrian		    "%s: checking bf=%p seqno=%d; ack=%d\n",
4139227364Sadrian		    __func__, bf, SEQNO(bf->bf_state.bfs_seqno),
4140227364Sadrian		    ATH_BA_ISSET(ba, ba_index));
4141227364Sadrian
4142227364Sadrian		if (tx_ok && ATH_BA_ISSET(ba, ba_index)) {
4143227868Sadrian			sc->sc_stats.ast_tx_aggr_ok++;
4144227364Sadrian			ath_tx_update_baw(sc, an, atid, bf);
4145227364Sadrian			bf->bf_state.bfs_dobaw = 0;
4146227364Sadrian			if (! bf->bf_state.bfs_addedbaw)
4147227364Sadrian				device_printf(sc->sc_dev,
4148227364Sadrian				    "%s: wasn't added: seqno %d\n",
4149227364Sadrian				    __func__, SEQNO(bf->bf_state.bfs_seqno));
4150227364Sadrian			bf->bf_next = NULL;
4151227364Sadrian			TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
4152227364Sadrian		} else {
4153227868Sadrian			sc->sc_stats.ast_tx_aggr_fail++;
4154227364Sadrian			if (ath_tx_retry_subframe(sc, bf, &bf_q)) {
4155227364Sadrian				drops++;
4156227364Sadrian				bf->bf_next = NULL;
4157227364Sadrian				TAILQ_INSERT_TAIL(&bf_cq, bf, bf_list);
4158227364Sadrian			}
4159227364Sadrian			nbad++;
4160227364Sadrian		}
4161227364Sadrian		bf = bf_next;
4162227364Sadrian	}
4163227364Sadrian
4164227364Sadrian	/*
4165227364Sadrian	 * Now that the BAW updates have been done, unlock
4166227364Sadrian	 *
4167227364Sadrian	 * txseq is grabbed before the lock is released so we
4168227364Sadrian	 * have a consistent view of what -was- in the BAW.
4169227364Sadrian	 * Anything after this point will not yet have been
4170227364Sadrian	 * TXed.
4171227364Sadrian	 */
4172227364Sadrian	txseq = tap->txa_start;
4173227364Sadrian	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4174227364Sadrian
4175227364Sadrian	if (nframes != nf)
4176227364Sadrian		device_printf(sc->sc_dev,
4177227364Sadrian		    "%s: num frames seen=%d; bf nframes=%d\n",
4178227364Sadrian		    __func__, nframes, nf);
4179227364Sadrian
4180227364Sadrian	/*
4181227364Sadrian	 * Now we know how many frames were bad, call the rate
4182227364Sadrian	 * control code.
4183227364Sadrian	 */
4184227364Sadrian	if (fail == 0)
4185229949Sadrian		ath_tx_update_ratectrl(sc, ni, rc, &ts, pktlen, nframes,
4186229949Sadrian		    nbad);
4187227364Sadrian
4188227364Sadrian	/*
4189227364Sadrian	 * send bar if we dropped any frames
4190227364Sadrian	 */
4191227364Sadrian	if (drops) {
4192233908Sadrian		/* Suspend the TX queue and get ready to send the BAR */
4193233908Sadrian		ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
4194233908Sadrian		ath_tx_tid_bar_suspend(sc, atid);
4195233908Sadrian		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4196227364Sadrian	}
4197227364Sadrian
4198234725Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4199234725Sadrian	    "%s: txa_start now %d\n", __func__, tap->txa_start);
4200234725Sadrian
4201234725Sadrian	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
4202234725Sadrian
4203227364Sadrian	/* Prepend all frames to the beginning of the queue */
4204227364Sadrian	while ((bf = TAILQ_LAST(&bf_q, ath_bufhead_s)) != NULL) {
4205227364Sadrian		TAILQ_REMOVE(&bf_q, bf, bf_list);
4206227364Sadrian		ATH_TXQ_INSERT_HEAD(atid, bf, bf_list);
4207227364Sadrian	}
4208234725Sadrian
4209234725Sadrian	/*
4210234725Sadrian	 * Reschedule to grab some further frames.
4211234725Sadrian	 */
4212227364Sadrian	ath_tx_tid_sched(sc, atid);
4213227364Sadrian
4214233908Sadrian	/*
4215240639Sadrian	 * If the queue is filtered, re-schedule as required.
4216240639Sadrian	 *
4217240639Sadrian	 * This is required as there may be a subsequent TX descriptor
4218240639Sadrian	 * for this end-node that has CLRDMASK set, so it's quite possible
4219240639Sadrian	 * that a filtered frame will be followed by a non-filtered
4220240639Sadrian	 * (complete or otherwise) frame.
4221240639Sadrian	 *
4222240639Sadrian	 * XXX should we do this before we complete the frame?
4223240639Sadrian	 */
4224240639Sadrian	if (atid->isfiltered)
4225240639Sadrian		ath_tx_tid_filt_comp_complete(sc, atid);
4226240639Sadrian
4227240639Sadrianfinish_send_bar:
4228240639Sadrian
4229240639Sadrian	/*
4230233908Sadrian	 * Send BAR if required
4231233908Sadrian	 */
4232233908Sadrian	if (ath_tx_tid_bar_tx_ready(sc, atid))
4233233908Sadrian		ath_tx_tid_bar_tx(sc, atid);
4234234725Sadrian
4235233908Sadrian	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4236233908Sadrian
4237227364Sadrian	/* Do deferred completion */
4238227364Sadrian	while ((bf = TAILQ_FIRST(&bf_cq)) != NULL) {
4239227364Sadrian		TAILQ_REMOVE(&bf_cq, bf, bf_list);
4240227364Sadrian		ath_tx_default_comp(sc, bf, 0);
4241227364Sadrian	}
4242227364Sadrian}
4243227364Sadrian
4244227364Sadrian/*
4245227364Sadrian * Handle completion of unaggregated frames in an ADDBA
4246227364Sadrian * session.
4247227364Sadrian *
4248227364Sadrian * Fail is set to 1 if the entry is being freed via a call to
4249227364Sadrian * ath_tx_draintxq().
4250227364Sadrian */
4251227364Sadrianstatic void
4252227364Sadrianath_tx_aggr_comp_unaggr(struct ath_softc *sc, struct ath_buf *bf, int fail)
4253227364Sadrian{
4254227364Sadrian	struct ieee80211_node *ni = bf->bf_node;
4255227364Sadrian	struct ath_node *an = ATH_NODE(ni);
4256227364Sadrian	int tid = bf->bf_state.bfs_tid;
4257227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
4258227364Sadrian	struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
4259240639Sadrian	int drops = 0;
4260227364Sadrian
4261227364Sadrian	/*
4262227364Sadrian	 * Update rate control status here, before we possibly
4263227364Sadrian	 * punt to retry or cleanup.
4264227364Sadrian	 *
4265227364Sadrian	 * Do it outside of the TXQ lock.
4266227364Sadrian	 */
4267233966Sadrian	if (fail == 0 && ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0))
4268227364Sadrian		ath_tx_update_ratectrl(sc, ni, bf->bf_state.bfs_rc,
4269227364Sadrian		    &bf->bf_status.ds_txstat,
4270227364Sadrian		    bf->bf_state.bfs_pktlen,
4271227364Sadrian		    1, (ts->ts_status == 0) ? 0 : 1);
4272227364Sadrian
4273227364Sadrian	/*
4274227364Sadrian	 * This is called early so atid->hwq_depth can be tracked.
4275227364Sadrian	 * This unfortunately means that it's released and regrabbed
4276227364Sadrian	 * during retry and cleanup. That's rather inefficient.
4277227364Sadrian	 */
4278227364Sadrian	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
4279227364Sadrian
4280227364Sadrian	if (tid == IEEE80211_NONQOS_TID)
4281227364Sadrian		device_printf(sc->sc_dev, "%s: TID=16!\n", __func__);
4282227364Sadrian
4283229949Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX,
4284229949Sadrian	    "%s: bf=%p: tid=%d, hwq_depth=%d, seqno=%d\n",
4285229949Sadrian	    __func__, bf, bf->bf_state.bfs_tid, atid->hwq_depth,
4286229949Sadrian	    SEQNO(bf->bf_state.bfs_seqno));
4287227364Sadrian
4288227364Sadrian	atid->hwq_depth--;
4289227364Sadrian	if (atid->hwq_depth < 0)
4290227364Sadrian		device_printf(sc->sc_dev, "%s: hwq_depth < 0: %d\n",
4291227364Sadrian		    __func__, atid->hwq_depth);
4292227364Sadrian
4293227364Sadrian	/*
4294240639Sadrian	 * If the TID is filtered, handle completing the filter
4295240639Sadrian	 * transition before potentially kicking it to the cleanup
4296240639Sadrian	 * function.
4297240639Sadrian	 */
4298240639Sadrian	if (atid->isfiltered)
4299240639Sadrian		ath_tx_tid_filt_comp_complete(sc, atid);
4300240639Sadrian
4301240639Sadrian	/*
4302227364Sadrian	 * If a cleanup is in progress, punt to comp_cleanup;
4303227364Sadrian	 * rather than handling it here. It's thus their
4304227364Sadrian	 * responsibility to clean up, call the completion
4305227364Sadrian	 * function in net80211, etc.
4306227364Sadrian	 */
4307227364Sadrian	if (atid->cleanup_inprogress) {
4308240639Sadrian		if (atid->isfiltered)
4309240639Sadrian			device_printf(sc->sc_dev,
4310240639Sadrian			    "%s: isfiltered=1, normal_comp?\n",
4311240639Sadrian			    __func__);
4312227364Sadrian		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4313229949Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: cleanup_unaggr\n",
4314229949Sadrian		    __func__);
4315227364Sadrian		ath_tx_comp_cleanup_unaggr(sc, bf);
4316227364Sadrian		return;
4317227364Sadrian	}
4318227364Sadrian
4319227364Sadrian	/*
4320240639Sadrian	 * XXX TODO: how does cleanup, BAR and filtered frame handling
4321240639Sadrian	 * overlap?
4322240639Sadrian	 *
4323240639Sadrian	 * If the frame is filtered OR if it's any failure but
4324240639Sadrian	 * the TID is filtered, the frame must be added to the
4325240639Sadrian	 * filtered frame list.
4326240639Sadrian	 *
4327240639Sadrian	 * However - a busy buffer can't be added to the filtered
4328240639Sadrian	 * list as it will end up being recycled without having
4329240639Sadrian	 * been made available for the hardware.
4330240639Sadrian	 */
4331240639Sadrian	if ((ts->ts_status & HAL_TXERR_FILT) ||
4332240639Sadrian	    (ts->ts_status != 0 && atid->isfiltered)) {
4333240639Sadrian		int freeframe;
4334240639Sadrian
4335240639Sadrian		if (fail != 0)
4336240639Sadrian			device_printf(sc->sc_dev,
4337240639Sadrian			    "%s: isfiltered=1, fail=%d\n",
4338240639Sadrian			    __func__,
4339240639Sadrian			    fail);
4340240639Sadrian		freeframe = ath_tx_tid_filt_comp_single(sc, atid, bf);
4341240639Sadrian		if (freeframe) {
4342240639Sadrian			/* Remove from BAW */
4343240639Sadrian			if (bf->bf_state.bfs_addedbaw)
4344240639Sadrian				drops++;
4345240639Sadrian			if (bf->bf_state.bfs_dobaw) {
4346240639Sadrian				ath_tx_update_baw(sc, an, atid, bf);
4347240639Sadrian				if (! bf->bf_state.bfs_addedbaw)
4348240639Sadrian					device_printf(sc->sc_dev,
4349240639Sadrian					    "%s: wasn't added: seqno %d\n",
4350240639Sadrian					    __func__, SEQNO(bf->bf_state.bfs_seqno));
4351240639Sadrian			}
4352240639Sadrian			bf->bf_state.bfs_dobaw = 0;
4353240639Sadrian		}
4354240639Sadrian
4355240639Sadrian		/*
4356240639Sadrian		 * If the frame couldn't be filtered, treat it as a drop and
4357240639Sadrian		 * prepare to send a BAR.
4358240639Sadrian		 */
4359240639Sadrian		if (freeframe && drops)
4360240639Sadrian			ath_tx_tid_bar_suspend(sc, atid);
4361240639Sadrian
4362240639Sadrian		/*
4363240639Sadrian		 * Send BAR if required
4364240639Sadrian		 */
4365240639Sadrian		if (ath_tx_tid_bar_tx_ready(sc, atid))
4366240639Sadrian			ath_tx_tid_bar_tx(sc, atid);
4367240639Sadrian
4368240639Sadrian		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4369240639Sadrian		/*
4370240639Sadrian		 * If freeframe is set, then the frame couldn't be
4371240639Sadrian		 * cloned and bf is still valid.  Just complete/free it.
4372240639Sadrian		 */
4373240639Sadrian		if (freeframe)
4374240639Sadrian			ath_tx_default_comp(sc, bf, fail);
4375240639Sadrian
4376240639Sadrian
4377240639Sadrian		return;
4378240639Sadrian	}
4379240639Sadrian	/*
4380227364Sadrian	 * Don't bother with the retry check if all frames
4381227364Sadrian	 * are being failed (eg during queue deletion.)
4382227364Sadrian	 */
4383235461Sadrian#if 0
4384227364Sadrian	if (fail == 0 && ts->ts_status & HAL_TXERR_XRETRY) {
4385235461Sadrian#endif
4386235461Sadrian	if (fail == 0 && ts->ts_status != 0) {
4387227364Sadrian		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4388229949Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: retry_unaggr\n",
4389229949Sadrian		    __func__);
4390227364Sadrian		ath_tx_aggr_retry_unaggr(sc, bf);
4391227364Sadrian		return;
4392227364Sadrian	}
4393227364Sadrian
4394227364Sadrian	/* Success? Complete */
4395227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: TID=%d, seqno %d\n",
4396227364Sadrian	    __func__, tid, SEQNO(bf->bf_state.bfs_seqno));
4397227364Sadrian	if (bf->bf_state.bfs_dobaw) {
4398227364Sadrian		ath_tx_update_baw(sc, an, atid, bf);
4399227364Sadrian		bf->bf_state.bfs_dobaw = 0;
4400227364Sadrian		if (! bf->bf_state.bfs_addedbaw)
4401227364Sadrian			device_printf(sc->sc_dev,
4402227364Sadrian			    "%s: wasn't added: seqno %d\n",
4403227364Sadrian			    __func__, SEQNO(bf->bf_state.bfs_seqno));
4404227364Sadrian	}
4405227364Sadrian
4406233908Sadrian	/*
4407240639Sadrian	 * If the queue is filtered, re-schedule as required.
4408240639Sadrian	 *
4409240639Sadrian	 * This is required as there may be a subsequent TX descriptor
4410240639Sadrian	 * for this end-node that has CLRDMASK set, so it's quite possible
4411240639Sadrian	 * that a filtered frame will be followed by a non-filtered
4412240639Sadrian	 * (complete or otherwise) frame.
4413240639Sadrian	 *
4414240639Sadrian	 * XXX should we do this before we complete the frame?
4415240639Sadrian	 */
4416240639Sadrian	if (atid->isfiltered)
4417240639Sadrian		ath_tx_tid_filt_comp_complete(sc, atid);
4418240639Sadrian
4419240639Sadrian	/*
4420233908Sadrian	 * Send BAR if required
4421233908Sadrian	 */
4422233908Sadrian	if (ath_tx_tid_bar_tx_ready(sc, atid))
4423233908Sadrian		ath_tx_tid_bar_tx(sc, atid);
4424233908Sadrian
4425227364Sadrian	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4426227364Sadrian
4427227364Sadrian	ath_tx_default_comp(sc, bf, fail);
4428227364Sadrian	/* bf is freed at this point */
4429227364Sadrian}
4430227364Sadrian
4431227364Sadrianvoid
4432227364Sadrianath_tx_aggr_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
4433227364Sadrian{
4434227364Sadrian	if (bf->bf_state.bfs_aggr)
4435227364Sadrian		ath_tx_aggr_comp_aggr(sc, bf, fail);
4436227364Sadrian	else
4437227364Sadrian		ath_tx_aggr_comp_unaggr(sc, bf, fail);
4438227364Sadrian}
4439227364Sadrian
4440227364Sadrian/*
4441227364Sadrian * Schedule some packets from the given node/TID to the hardware.
4442227364Sadrian *
4443227364Sadrian * This is the aggregate version.
4444227364Sadrian */
4445227364Sadrianvoid
4446227364Sadrianath_tx_tid_hw_queue_aggr(struct ath_softc *sc, struct ath_node *an,
4447227364Sadrian    struct ath_tid *tid)
4448227364Sadrian{
4449227364Sadrian	struct ath_buf *bf;
4450227364Sadrian	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
4451227364Sadrian	struct ieee80211_tx_ampdu *tap;
4452227364Sadrian	ATH_AGGR_STATUS status;
4453227364Sadrian	ath_bufhead bf_q;
4454227364Sadrian
4455227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d\n", __func__, tid->tid);
4456227364Sadrian	ATH_TXQ_LOCK_ASSERT(txq);
4457227364Sadrian
4458227364Sadrian	tap = ath_tx_get_tx_tid(an, tid->tid);
4459227364Sadrian
4460227364Sadrian	if (tid->tid == IEEE80211_NONQOS_TID)
4461227364Sadrian		device_printf(sc->sc_dev, "%s: called for TID=NONQOS_TID?\n",
4462227364Sadrian		    __func__);
4463227364Sadrian
4464227364Sadrian	for (;;) {
4465227364Sadrian		status = ATH_AGGR_DONE;
4466227364Sadrian
4467227364Sadrian		/*
4468227364Sadrian		 * If the upper layer has paused the TID, don't
4469227364Sadrian		 * queue any further packets.
4470227364Sadrian		 *
4471227364Sadrian		 * This can also occur from the completion task because
4472227364Sadrian		 * of packet loss; but as its serialised with this code,
4473227364Sadrian		 * it won't "appear" half way through queuing packets.
4474227364Sadrian		 */
4475227364Sadrian		if (tid->paused)
4476227364Sadrian			break;
4477227364Sadrian
4478227364Sadrian		bf = TAILQ_FIRST(&tid->axq_q);
4479227364Sadrian		if (bf == NULL) {
4480227364Sadrian			break;
4481227364Sadrian		}
4482227364Sadrian
4483227364Sadrian		/*
4484227364Sadrian		 * If the packet doesn't fall within the BAW (eg a NULL
4485227364Sadrian		 * data frame), schedule it directly; continue.
4486227364Sadrian		 */
4487227364Sadrian		if (! bf->bf_state.bfs_dobaw) {
4488229949Sadrian			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4489229949Sadrian			    "%s: non-baw packet\n",
4490227364Sadrian			    __func__);
4491227364Sadrian			ATH_TXQ_REMOVE(tid, bf, bf_list);
4492240180Sadrian
4493240180Sadrian			if (bf->bf_state.bfs_nframes > 1)
4494240180Sadrian				device_printf(sc->sc_dev,
4495240180Sadrian				    "%s: aggr=%d, nframes=%d\n",
4496240180Sadrian				    __func__,
4497240180Sadrian				    bf->bf_state.bfs_aggr,
4498240180Sadrian				    bf->bf_state.bfs_nframes);
4499240180Sadrian
4500240180Sadrian			/*
4501240180Sadrian			 * This shouldn't happen - such frames shouldn't
4502240180Sadrian			 * ever have been queued as an aggregate in the
4503240180Sadrian			 * first place.  However, make sure the fields
4504240180Sadrian			 * are correctly setup just to be totally sure.
4505240180Sadrian			 */
4506227364Sadrian			bf->bf_state.bfs_aggr = 0;
4507240180Sadrian			bf->bf_state.bfs_nframes = 1;
4508240180Sadrian
4509227364Sadrian			ath_tx_do_ratelookup(sc, bf);
4510233989Sadrian			ath_tx_calc_duration(sc, bf);
4511233989Sadrian			ath_tx_calc_protection(sc, bf);
4512233989Sadrian			ath_tx_set_rtscts(sc, bf);
4513227364Sadrian			ath_tx_rate_fill_rcflags(sc, bf);
4514227364Sadrian			ath_tx_setds(sc, bf);
4515227364Sadrian			ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
4516227364Sadrian
4517227364Sadrian			sc->sc_aggr_stats.aggr_nonbaw_pkt++;
4518227364Sadrian
4519227364Sadrian			/* Queue the packet; continue */
4520227364Sadrian			goto queuepkt;
4521227364Sadrian		}
4522227364Sadrian
4523227364Sadrian		TAILQ_INIT(&bf_q);
4524227364Sadrian
4525227364Sadrian		/*
4526227364Sadrian		 * Do a rate control lookup on the first frame in the
4527227364Sadrian		 * list. The rate control code needs that to occur
4528227364Sadrian		 * before it can determine whether to TX.
4529227364Sadrian		 * It's inaccurate because the rate control code doesn't
4530227364Sadrian		 * really "do" aggregate lookups, so it only considers
4531227364Sadrian		 * the size of the first frame.
4532227364Sadrian		 */
4533227364Sadrian		ath_tx_do_ratelookup(sc, bf);
4534227364Sadrian		bf->bf_state.bfs_rc[3].rix = 0;
4535227364Sadrian		bf->bf_state.bfs_rc[3].tries = 0;
4536233989Sadrian
4537233989Sadrian		ath_tx_calc_duration(sc, bf);
4538233989Sadrian		ath_tx_calc_protection(sc, bf);
4539233989Sadrian
4540233989Sadrian		ath_tx_set_rtscts(sc, bf);
4541227364Sadrian		ath_tx_rate_fill_rcflags(sc, bf);
4542227364Sadrian
4543227364Sadrian		status = ath_tx_form_aggr(sc, an, tid, &bf_q);
4544227364Sadrian
4545227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4546227364Sadrian		    "%s: ath_tx_form_aggr() status=%d\n", __func__, status);
4547227364Sadrian
4548227364Sadrian		/*
4549227364Sadrian		 * No frames to be picked up - out of BAW
4550227364Sadrian		 */
4551227364Sadrian		if (TAILQ_EMPTY(&bf_q))
4552227364Sadrian			break;
4553227364Sadrian
4554227364Sadrian		/*
4555227364Sadrian		 * This assumes that the descriptor list in the ath_bufhead
4556227364Sadrian		 * are already linked together via bf_next pointers.
4557227364Sadrian		 */
4558227364Sadrian		bf = TAILQ_FIRST(&bf_q);
4559227364Sadrian
4560233989Sadrian		if (status == ATH_AGGR_8K_LIMITED)
4561233989Sadrian			sc->sc_aggr_stats.aggr_rts_aggr_limited++;
4562233989Sadrian
4563227364Sadrian		/*
4564227364Sadrian		 * If it's the only frame send as non-aggregate
4565227364Sadrian		 * assume that ath_tx_form_aggr() has checked
4566227364Sadrian		 * whether it's in the BAW and added it appropriately.
4567227364Sadrian		 */
4568227364Sadrian		if (bf->bf_state.bfs_nframes == 1) {
4569227364Sadrian			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4570227364Sadrian			    "%s: single-frame aggregate\n", __func__);
4571227364Sadrian			bf->bf_state.bfs_aggr = 0;
4572240255Sadrian			bf->bf_state.bfs_ndelim = 0;
4573227364Sadrian			ath_tx_setds(sc, bf);
4574227364Sadrian			ath_hal_clr11n_aggr(sc->sc_ah, bf->bf_desc);
4575227364Sadrian			if (status == ATH_AGGR_BAW_CLOSED)
4576227364Sadrian				sc->sc_aggr_stats.aggr_baw_closed_single_pkt++;
4577227364Sadrian			else
4578227364Sadrian				sc->sc_aggr_stats.aggr_single_pkt++;
4579227364Sadrian		} else {
4580227364Sadrian			DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
4581229949Sadrian			    "%s: multi-frame aggregate: %d frames, "
4582229949Sadrian			    "length %d\n",
4583227364Sadrian			     __func__, bf->bf_state.bfs_nframes,
4584227364Sadrian			    bf->bf_state.bfs_al);
4585227364Sadrian			bf->bf_state.bfs_aggr = 1;
4586227364Sadrian			sc->sc_aggr_stats.aggr_pkts[bf->bf_state.bfs_nframes]++;
4587227364Sadrian			sc->sc_aggr_stats.aggr_aggr_pkt++;
4588227364Sadrian
4589227364Sadrian			/*
4590233989Sadrian			 * Calculate the duration/protection as required.
4591233989Sadrian			 */
4592233989Sadrian			ath_tx_calc_duration(sc, bf);
4593233989Sadrian			ath_tx_calc_protection(sc, bf);
4594233989Sadrian
4595233989Sadrian			/*
4596227364Sadrian			 * Update the rate and rtscts information based on the
4597227364Sadrian			 * rate decision made by the rate control code;
4598227364Sadrian			 * the first frame in the aggregate needs it.
4599227364Sadrian			 */
4600227364Sadrian			ath_tx_set_rtscts(sc, bf);
4601227364Sadrian
4602227364Sadrian			/*
4603227364Sadrian			 * Setup the relevant descriptor fields
4604227364Sadrian			 * for aggregation. The first descriptor
4605227364Sadrian			 * already points to the rest in the chain.
4606227364Sadrian			 */
4607227364Sadrian			ath_tx_setds_11n(sc, bf);
4608227364Sadrian
4609227364Sadrian		}
4610227364Sadrian	queuepkt:
4611227364Sadrian		//txq = bf->bf_state.bfs_txq;
4612227364Sadrian
4613227364Sadrian		/* Set completion handler, multi-frame aggregate or not */
4614227364Sadrian		bf->bf_comp = ath_tx_aggr_comp;
4615227364Sadrian
4616227364Sadrian		if (bf->bf_state.bfs_tid == IEEE80211_NONQOS_TID)
4617227364Sadrian		    device_printf(sc->sc_dev, "%s: TID=16?\n", __func__);
4618227364Sadrian
4619227364Sadrian		/* Punt to txq */
4620227364Sadrian		ath_tx_handoff(sc, txq, bf);
4621227364Sadrian
4622227364Sadrian		/* Track outstanding buffer count to hardware */
4623227364Sadrian		/* aggregates are "one" buffer */
4624227364Sadrian		tid->hwq_depth++;
4625227364Sadrian
4626227364Sadrian		/*
4627227364Sadrian		 * Break out if ath_tx_form_aggr() indicated
4628227364Sadrian		 * there can't be any further progress (eg BAW is full.)
4629227364Sadrian		 * Checking for an empty txq is done above.
4630227364Sadrian		 *
4631227364Sadrian		 * XXX locking on txq here?
4632227364Sadrian		 */
4633227364Sadrian		if (txq->axq_aggr_depth >= sc->sc_hwq_limit ||
4634227364Sadrian		    status == ATH_AGGR_BAW_CLOSED)
4635227364Sadrian			break;
4636227364Sadrian	}
4637227364Sadrian}
4638227364Sadrian
4639227364Sadrian/*
4640227364Sadrian * Schedule some packets from the given node/TID to the hardware.
4641227364Sadrian */
4642227364Sadrianvoid
4643227364Sadrianath_tx_tid_hw_queue_norm(struct ath_softc *sc, struct ath_node *an,
4644227364Sadrian    struct ath_tid *tid)
4645227364Sadrian{
4646227364Sadrian	struct ath_buf *bf;
4647227364Sadrian	struct ath_txq *txq = sc->sc_ac2q[tid->ac];
4648227364Sadrian
4649227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: node %p: TID %d: called\n",
4650227364Sadrian	    __func__, an, tid->tid);
4651227364Sadrian
4652227364Sadrian	ATH_TXQ_LOCK_ASSERT(txq);
4653227364Sadrian
4654227364Sadrian	/* Check - is AMPDU pending or running? then print out something */
4655227364Sadrian	if (ath_tx_ampdu_pending(sc, an, tid->tid))
4656227364Sadrian		device_printf(sc->sc_dev, "%s: tid=%d, ampdu pending?\n",
4657227364Sadrian		    __func__, tid->tid);
4658227364Sadrian	if (ath_tx_ampdu_running(sc, an, tid->tid))
4659227364Sadrian		device_printf(sc->sc_dev, "%s: tid=%d, ampdu running?\n",
4660227364Sadrian		    __func__, tid->tid);
4661227364Sadrian
4662227364Sadrian	for (;;) {
4663227364Sadrian
4664227364Sadrian		/*
4665227364Sadrian		 * If the upper layers have paused the TID, don't
4666227364Sadrian		 * queue any further packets.
4667227364Sadrian		 */
4668227364Sadrian		if (tid->paused)
4669227364Sadrian			break;
4670227364Sadrian
4671227364Sadrian		bf = TAILQ_FIRST(&tid->axq_q);
4672227364Sadrian		if (bf == NULL) {
4673227364Sadrian			break;
4674227364Sadrian		}
4675227364Sadrian
4676227364Sadrian		ATH_TXQ_REMOVE(tid, bf, bf_list);
4677227364Sadrian
4678227364Sadrian		KASSERT(txq == bf->bf_state.bfs_txq, ("txqs not equal!\n"));
4679227364Sadrian
4680227364Sadrian		/* Sanity check! */
4681227364Sadrian		if (tid->tid != bf->bf_state.bfs_tid) {
4682227364Sadrian			device_printf(sc->sc_dev, "%s: bfs_tid %d !="
4683227364Sadrian			    " tid %d\n",
4684227364Sadrian			    __func__, bf->bf_state.bfs_tid, tid->tid);
4685227364Sadrian		}
4686227364Sadrian		/* Normal completion handler */
4687227364Sadrian		bf->bf_comp = ath_tx_normal_comp;
4688227364Sadrian
4689227364Sadrian		/* Program descriptors + rate control */
4690227364Sadrian		ath_tx_do_ratelookup(sc, bf);
4691233989Sadrian		ath_tx_calc_duration(sc, bf);
4692233989Sadrian		ath_tx_calc_protection(sc, bf);
4693233989Sadrian		ath_tx_set_rtscts(sc, bf);
4694227364Sadrian		ath_tx_rate_fill_rcflags(sc, bf);
4695227364Sadrian		ath_tx_setds(sc, bf);
4696227364Sadrian
4697227364Sadrian		/* Track outstanding buffer count to hardware */
4698227364Sadrian		/* aggregates are "one" buffer */
4699227364Sadrian		tid->hwq_depth++;
4700227364Sadrian
4701227364Sadrian		/* Punt to hardware or software txq */
4702227364Sadrian		ath_tx_handoff(sc, txq, bf);
4703227364Sadrian	}
4704227364Sadrian}
4705227364Sadrian
4706227364Sadrian/*
4707227364Sadrian * Schedule some packets to the given hardware queue.
4708227364Sadrian *
4709227364Sadrian * This function walks the list of TIDs (ie, ath_node TIDs
4710227364Sadrian * with queued traffic) and attempts to schedule traffic
4711227364Sadrian * from them.
4712227364Sadrian *
4713227364Sadrian * TID scheduling is implemented as a FIFO, with TIDs being
4714227364Sadrian * added to the end of the queue after some frames have been
4715227364Sadrian * scheduled.
4716227364Sadrian */
4717227364Sadrianvoid
4718227364Sadrianath_txq_sched(struct ath_softc *sc, struct ath_txq *txq)
4719227364Sadrian{
4720227364Sadrian	struct ath_tid *tid, *next, *last;
4721227364Sadrian
4722227364Sadrian	ATH_TXQ_LOCK_ASSERT(txq);
4723227364Sadrian
4724227364Sadrian	/*
4725227364Sadrian	 * Don't schedule if the hardware queue is busy.
4726227364Sadrian	 * This (hopefully) gives some more time to aggregate
4727227364Sadrian	 * some packets in the aggregation queue.
4728227364Sadrian	 */
4729227364Sadrian	if (txq->axq_aggr_depth >= sc->sc_hwq_limit) {
4730227364Sadrian		sc->sc_aggr_stats.aggr_sched_nopkt++;
4731227364Sadrian		return;
4732227364Sadrian	}
4733227364Sadrian
4734227364Sadrian	last = TAILQ_LAST(&txq->axq_tidq, axq_t_s);
4735227364Sadrian
4736227364Sadrian	TAILQ_FOREACH_SAFE(tid, &txq->axq_tidq, axq_qelem, next) {
4737227364Sadrian		/*
4738227364Sadrian		 * Suspend paused queues here; they'll be resumed
4739227364Sadrian		 * once the addba completes or times out.
4740227364Sadrian		 */
4741227364Sadrian		DPRINTF(sc, ATH_DEBUG_SW_TX, "%s: tid=%d, paused=%d\n",
4742227364Sadrian		    __func__, tid->tid, tid->paused);
4743227364Sadrian		ath_tx_tid_unsched(sc, tid);
4744227364Sadrian		if (tid->paused) {
4745227364Sadrian			continue;
4746227364Sadrian		}
4747227364Sadrian		if (ath_tx_ampdu_running(sc, tid->an, tid->tid))
4748227364Sadrian			ath_tx_tid_hw_queue_aggr(sc, tid->an, tid);
4749227364Sadrian		else
4750227364Sadrian			ath_tx_tid_hw_queue_norm(sc, tid->an, tid);
4751227364Sadrian
4752227364Sadrian		/* Not empty? Re-schedule */
4753227364Sadrian		if (tid->axq_depth != 0)
4754227364Sadrian			ath_tx_tid_sched(sc, tid);
4755227364Sadrian
4756227364Sadrian		/* Give the software queue time to aggregate more packets */
4757227364Sadrian		if (txq->axq_aggr_depth >= sc->sc_hwq_limit) {
4758227364Sadrian			break;
4759227364Sadrian		}
4760227364Sadrian
4761227364Sadrian		/*
4762227364Sadrian		 * If this was the last entry on the original list, stop.
4763227364Sadrian		 * Otherwise nodes that have been rescheduled onto the end
4764227364Sadrian		 * of the TID FIFO list will just keep being rescheduled.
4765227364Sadrian		 */
4766227364Sadrian		if (tid == last)
4767227364Sadrian			break;
4768227364Sadrian	}
4769227364Sadrian}
4770227364Sadrian
4771227364Sadrian/*
4772227364Sadrian * TX addba handling
4773227364Sadrian */
4774227364Sadrian
4775227364Sadrian/*
4776227364Sadrian * Return net80211 TID struct pointer, or NULL for none
4777227364Sadrian */
4778227364Sadrianstruct ieee80211_tx_ampdu *
4779227364Sadrianath_tx_get_tx_tid(struct ath_node *an, int tid)
4780227364Sadrian{
4781227364Sadrian	struct ieee80211_node *ni = &an->an_node;
4782227364Sadrian	struct ieee80211_tx_ampdu *tap;
4783227364Sadrian
4784227364Sadrian	if (tid == IEEE80211_NONQOS_TID)
4785227364Sadrian		return NULL;
4786227364Sadrian
4787234324Sadrian	tap = &ni->ni_tx_ampdu[tid];
4788227364Sadrian	return tap;
4789227364Sadrian}
4790227364Sadrian
4791227364Sadrian/*
4792227364Sadrian * Is AMPDU-TX running?
4793227364Sadrian */
4794227364Sadrianstatic int
4795227364Sadrianath_tx_ampdu_running(struct ath_softc *sc, struct ath_node *an, int tid)
4796227364Sadrian{
4797227364Sadrian	struct ieee80211_tx_ampdu *tap;
4798227364Sadrian
4799227364Sadrian	if (tid == IEEE80211_NONQOS_TID)
4800227364Sadrian		return 0;
4801227364Sadrian
4802227364Sadrian	tap = ath_tx_get_tx_tid(an, tid);
4803227364Sadrian	if (tap == NULL)
4804227364Sadrian		return 0;	/* Not valid; default to not running */
4805227364Sadrian
4806227364Sadrian	return !! (tap->txa_flags & IEEE80211_AGGR_RUNNING);
4807227364Sadrian}
4808227364Sadrian
4809227364Sadrian/*
4810227364Sadrian * Is AMPDU-TX negotiation pending?
4811227364Sadrian */
4812227364Sadrianstatic int
4813227364Sadrianath_tx_ampdu_pending(struct ath_softc *sc, struct ath_node *an, int tid)
4814227364Sadrian{
4815227364Sadrian	struct ieee80211_tx_ampdu *tap;
4816227364Sadrian
4817227364Sadrian	if (tid == IEEE80211_NONQOS_TID)
4818227364Sadrian		return 0;
4819227364Sadrian
4820227364Sadrian	tap = ath_tx_get_tx_tid(an, tid);
4821227364Sadrian	if (tap == NULL)
4822227364Sadrian		return 0;	/* Not valid; default to not pending */
4823227364Sadrian
4824227364Sadrian	return !! (tap->txa_flags & IEEE80211_AGGR_XCHGPEND);
4825227364Sadrian}
4826227364Sadrian
4827227364Sadrian/*
4828227364Sadrian * Is AMPDU-TX pending for the given TID?
4829227364Sadrian */
4830227364Sadrian
4831227364Sadrian
4832227364Sadrian/*
4833227364Sadrian * Method to handle sending an ADDBA request.
4834227364Sadrian *
4835227364Sadrian * We tap this so the relevant flags can be set to pause the TID
4836227364Sadrian * whilst waiting for the response.
4837227364Sadrian *
4838227364Sadrian * XXX there's no timeout handler we can override?
4839227364Sadrian */
4840227364Sadrianint
4841227364Sadrianath_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
4842227364Sadrian    int dialogtoken, int baparamset, int batimeout)
4843227364Sadrian{
4844227364Sadrian	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
4845234324Sadrian	int tid = tap->txa_tid;
4846227364Sadrian	struct ath_node *an = ATH_NODE(ni);
4847227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
4848227364Sadrian
4849227364Sadrian	/*
4850227364Sadrian	 * XXX danger Will Robinson!
4851227364Sadrian	 *
4852227364Sadrian	 * Although the taskqueue may be running and scheduling some more
4853227364Sadrian	 * packets, these should all be _before_ the addba sequence number.
4854227364Sadrian	 * However, net80211 will keep self-assigning sequence numbers
4855227364Sadrian	 * until addba has been negotiated.
4856227364Sadrian	 *
4857227364Sadrian	 * In the past, these packets would be "paused" (which still works
4858227364Sadrian	 * fine, as they're being scheduled to the driver in the same
4859227364Sadrian	 * serialised method which is calling the addba request routine)
4860227364Sadrian	 * and when the aggregation session begins, they'll be dequeued
4861227364Sadrian	 * as aggregate packets and added to the BAW. However, now there's
4862227364Sadrian	 * a "bf->bf_state.bfs_dobaw" flag, and this isn't set for these
4863227364Sadrian	 * packets. Thus they never get included in the BAW tracking and
4864227364Sadrian	 * this can cause the initial burst of packets after the addba
4865227364Sadrian	 * negotiation to "hang", as they quickly fall outside the BAW.
4866227364Sadrian	 *
4867227364Sadrian	 * The "eventual" solution should be to tag these packets with
4868227364Sadrian	 * dobaw. Although net80211 has given us a sequence number,
4869227364Sadrian	 * it'll be "after" the left edge of the BAW and thus it'll
4870227364Sadrian	 * fall within it.
4871227364Sadrian	 */
4872235034Sadrian	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
4873235774Sadrian	/*
4874235774Sadrian	 * This is a bit annoying.  Until net80211 HT code inherits some
4875235774Sadrian	 * (any) locking, we may have this called in parallel BUT only
4876235774Sadrian	 * one response/timeout will be called.  Grr.
4877235774Sadrian	 */
4878235774Sadrian	if (atid->addba_tx_pending == 0) {
4879235774Sadrian		ath_tx_tid_pause(sc, atid);
4880235774Sadrian		atid->addba_tx_pending = 1;
4881235774Sadrian	}
4882235034Sadrian	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4883227364Sadrian
4884227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4885227364Sadrian	    "%s: called; dialogtoken=%d, baparamset=%d, batimeout=%d\n",
4886227364Sadrian	    __func__, dialogtoken, baparamset, batimeout);
4887227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4888227364Sadrian	    "%s: txa_start=%d, ni_txseqs=%d\n",
4889227364Sadrian	    __func__, tap->txa_start, ni->ni_txseqs[tid]);
4890227364Sadrian
4891227364Sadrian	return sc->sc_addba_request(ni, tap, dialogtoken, baparamset,
4892227364Sadrian	    batimeout);
4893227364Sadrian}
4894227364Sadrian
4895227364Sadrian/*
4896227364Sadrian * Handle an ADDBA response.
4897227364Sadrian *
4898227364Sadrian * We unpause the queue so TX'ing can resume.
4899227364Sadrian *
4900227364Sadrian * Any packets TX'ed from this point should be "aggregate" (whether
4901227364Sadrian * aggregate or not) so the BAW is updated.
4902227364Sadrian *
4903227364Sadrian * Note! net80211 keeps self-assigning sequence numbers until
4904227364Sadrian * ampdu is negotiated. This means the initially-negotiated BAW left
4905227364Sadrian * edge won't match the ni->ni_txseq.
4906227364Sadrian *
4907227364Sadrian * So, being very dirty, the BAW left edge is "slid" here to match
4908227364Sadrian * ni->ni_txseq.
4909227364Sadrian *
4910227364Sadrian * What likely SHOULD happen is that all packets subsequent to the
4911227364Sadrian * addba request should be tagged as aggregate and queued as non-aggregate
4912227364Sadrian * frames; thus updating the BAW. For now though, I'll just slide the
4913227364Sadrian * window.
4914227364Sadrian */
4915227364Sadrianint
4916227364Sadrianath_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
4917227364Sadrian    int status, int code, int batimeout)
4918227364Sadrian{
4919227364Sadrian	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
4920234324Sadrian	int tid = tap->txa_tid;
4921227364Sadrian	struct ath_node *an = ATH_NODE(ni);
4922227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
4923227364Sadrian	int r;
4924227364Sadrian
4925227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4926227364Sadrian	    "%s: called; status=%d, code=%d, batimeout=%d\n", __func__,
4927227364Sadrian	    status, code, batimeout);
4928227364Sadrian
4929227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
4930227364Sadrian	    "%s: txa_start=%d, ni_txseqs=%d\n",
4931227364Sadrian	    __func__, tap->txa_start, ni->ni_txseqs[tid]);
4932227364Sadrian
4933227364Sadrian	/*
4934227364Sadrian	 * Call this first, so the interface flags get updated
4935227364Sadrian	 * before the TID is unpaused. Otherwise a race condition
4936227364Sadrian	 * exists where the unpaused TID still doesn't yet have
4937227364Sadrian	 * IEEE80211_AGGR_RUNNING set.
4938227364Sadrian	 */
4939227364Sadrian	r = sc->sc_addba_response(ni, tap, status, code, batimeout);
4940227364Sadrian
4941227364Sadrian	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
4942235774Sadrian	atid->addba_tx_pending = 0;
4943227364Sadrian	/*
4944227364Sadrian	 * XXX dirty!
4945227364Sadrian	 * Slide the BAW left edge to wherever net80211 left it for us.
4946227364Sadrian	 * Read above for more information.
4947227364Sadrian	 */
4948227364Sadrian	tap->txa_start = ni->ni_txseqs[tid];
4949227364Sadrian	ath_tx_tid_resume(sc, atid);
4950227364Sadrian	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4951227364Sadrian	return r;
4952227364Sadrian}
4953227364Sadrian
4954227364Sadrian
4955227364Sadrian/*
4956227364Sadrian * Stop ADDBA on a queue.
4957237593Sadrian *
4958237593Sadrian * This can be called whilst BAR TX is currently active on the queue,
4959237593Sadrian * so make sure this is unblocked before continuing.
4960227364Sadrian */
4961227364Sadrianvoid
4962227364Sadrianath_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
4963227364Sadrian{
4964227364Sadrian	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
4965234324Sadrian	int tid = tap->txa_tid;
4966227364Sadrian	struct ath_node *an = ATH_NODE(ni);
4967227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
4968227364Sadrian
4969227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL, "%s: called\n", __func__);
4970227364Sadrian
4971237593Sadrian	/*
4972237593Sadrian	 * Pause TID traffic early, so there aren't any races
4973237593Sadrian	 * Unblock the pending BAR held traffic, if it's currently paused.
4974237593Sadrian	 */
4975235034Sadrian	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
4976227364Sadrian	ath_tx_tid_pause(sc, atid);
4977237593Sadrian	if (atid->bar_wait) {
4978237593Sadrian		/*
4979237593Sadrian		 * bar_unsuspend() expects bar_tx == 1, as it should be
4980237593Sadrian		 * called from the TX completion path.  This quietens
4981237593Sadrian		 * the warning.  It's cleared for us anyway.
4982237593Sadrian		 */
4983237593Sadrian		atid->bar_tx = 1;
4984237593Sadrian		ath_tx_tid_bar_unsuspend(sc, atid);
4985237593Sadrian	}
4986235034Sadrian	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
4987227364Sadrian
4988227364Sadrian	/* There's no need to hold the TXQ lock here */
4989227364Sadrian	sc->sc_addba_stop(ni, tap);
4990227364Sadrian
4991227364Sadrian	/*
4992235749Sadrian	 * ath_tx_tid_cleanup will resume the TID if possible, otherwise
4993227364Sadrian	 * it'll set the cleanup flag, and it'll be unpaused once
4994227364Sadrian	 * things have been cleaned up.
4995227364Sadrian	 */
4996235749Sadrian	ath_tx_tid_cleanup(sc, an, tid);
4997227364Sadrian}
4998227364Sadrian
4999227364Sadrian/*
5000227364Sadrian * Note: net80211 bar_timeout() doesn't call this function on BAR failure;
5001227364Sadrian * it simply tears down the aggregation session. Ew.
5002227364Sadrian *
5003227364Sadrian * It however will call ieee80211_ampdu_stop() which will call
5004227364Sadrian * ic->ic_addba_stop().
5005227364Sadrian *
5006227364Sadrian * XXX This uses a hard-coded max BAR count value; the whole
5007227364Sadrian * XXX BAR TX success or failure should be better handled!
5008227364Sadrian */
5009227364Sadrianvoid
5010227364Sadrianath_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
5011227364Sadrian    int status)
5012227364Sadrian{
5013227364Sadrian	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
5014234324Sadrian	int tid = tap->txa_tid;
5015227364Sadrian	struct ath_node *an = ATH_NODE(ni);
5016227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
5017227364Sadrian	int attempts = tap->txa_attempts;
5018227364Sadrian
5019235491Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_BAR,
5020235676Sadrian	    "%s: called; tap=%p, atid=%p, txa_tid=%d, atid->tid=%d, status=%d, attempts=%d\n",
5021235491Sadrian	    __func__,
5022235676Sadrian	    tap,
5023235676Sadrian	    atid,
5024235676Sadrian	    tap->txa_tid,
5025235676Sadrian	    atid->tid,
5026235491Sadrian	    status,
5027235491Sadrian	    attempts);
5028227364Sadrian
5029227364Sadrian	/* Note: This may update the BAW details */
5030227364Sadrian	sc->sc_bar_response(ni, tap, status);
5031227364Sadrian
5032227364Sadrian	/* Unpause the TID */
5033227364Sadrian	/*
5034227364Sadrian	 * XXX if this is attempt=50, the TID will be downgraded
5035227364Sadrian	 * XXX to a non-aggregate session. So we must unpause the
5036227364Sadrian	 * XXX TID here or it'll never be done.
5037227364Sadrian	 */
5038227364Sadrian	if (status == 0 || attempts == 50) {
5039227364Sadrian		ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
5040233908Sadrian		ath_tx_tid_bar_unsuspend(sc, atid);
5041227364Sadrian		ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
5042227364Sadrian	}
5043227364Sadrian}
5044227364Sadrian
5045227364Sadrian/*
5046227364Sadrian * This is called whenever the pending ADDBA request times out.
5047227364Sadrian * Unpause and reschedule the TID.
5048227364Sadrian */
5049227364Sadrianvoid
5050227364Sadrianath_addba_response_timeout(struct ieee80211_node *ni,
5051227364Sadrian    struct ieee80211_tx_ampdu *tap)
5052227364Sadrian{
5053227364Sadrian	struct ath_softc *sc = ni->ni_ic->ic_ifp->if_softc;
5054234324Sadrian	int tid = tap->txa_tid;
5055227364Sadrian	struct ath_node *an = ATH_NODE(ni);
5056227364Sadrian	struct ath_tid *atid = &an->an_tid[tid];
5057227364Sadrian
5058227364Sadrian	DPRINTF(sc, ATH_DEBUG_SW_TX_CTRL,
5059227364Sadrian	    "%s: called; resuming\n", __func__);
5060227364Sadrian
5061235774Sadrian	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
5062235774Sadrian	atid->addba_tx_pending = 0;
5063235774Sadrian	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
5064235774Sadrian
5065227364Sadrian	/* Note: This updates the aggregate state to (again) pending */
5066227364Sadrian	sc->sc_addba_response_timeout(ni, tap);
5067227364Sadrian
5068227364Sadrian	/* Unpause the TID; which reschedules it */
5069227364Sadrian	ATH_TXQ_LOCK(sc->sc_ac2q[atid->ac]);
5070227364Sadrian	ath_tx_tid_resume(sc, atid);
5071227364Sadrian	ATH_TXQ_UNLOCK(sc->sc_ac2q[atid->ac]);
5072227364Sadrian}
5073238710Sadrian
5074238710Sadrianstatic int
5075238710Sadrianath_legacy_dma_txsetup(struct ath_softc *sc)
5076238710Sadrian{
5077238710Sadrian
5078238710Sadrian	/* nothing new needed */
5079238710Sadrian	return (0);
5080238710Sadrian}
5081238710Sadrian
5082238710Sadrianstatic int
5083238710Sadrianath_legacy_dma_txteardown(struct ath_softc *sc)
5084238710Sadrian{
5085238710Sadrian
5086238710Sadrian	/* nothing new needed */
5087238710Sadrian	return (0);
5088238710Sadrian}
5089238710Sadrian
5090238710Sadrianvoid
5091238710Sadrianath_xmit_setup_legacy(struct ath_softc *sc)
5092238710Sadrian{
5093238729Sadrian	/*
5094238729Sadrian	 * For now, just set the descriptor length to sizeof(ath_desc);
5095238729Sadrian	 * worry about extracting the real length out of the HAL later.
5096238729Sadrian	 */
5097238729Sadrian	sc->sc_tx_desclen = sizeof(struct ath_desc);
5098238729Sadrian	sc->sc_tx_statuslen = 0;
5099238729Sadrian	sc->sc_tx_nmaps = 1;	/* only one buffer per TX desc */
5100238710Sadrian
5101238710Sadrian	sc->sc_tx.xmit_setup = ath_legacy_dma_txsetup;
5102238710Sadrian	sc->sc_tx.xmit_teardown = ath_legacy_dma_txteardown;
5103238931Sadrian	sc->sc_tx.xmit_attach_comp_func = ath_legacy_attach_comp_func;
5104238930Sadrian
5105238930Sadrian	sc->sc_tx.xmit_dma_restart = ath_legacy_tx_dma_restart;
5106238930Sadrian	sc->sc_tx.xmit_handoff = ath_legacy_xmit_handoff;
5107239204Sadrian
5108239204Sadrian	sc->sc_tx.xmit_drain = ath_legacy_tx_drain;
5109238710Sadrian}
5110