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