if_ath_tx.c revision 218240
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: head/sys/dev/ath/if_ath_tx.c 218240 2011-02-03 20:30:17Z adrian $");
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
81#include <net/bpf.h>
82
83#ifdef INET
84#include <netinet/in.h>
85#include <netinet/if_ether.h>
86#endif
87
88#include <dev/ath/if_athvar.h>
89#include <dev/ath/ath_hal/ah_devid.h>		/* XXX for softled */
90#include <dev/ath/ath_hal/ah_diagcodes.h>
91
92#include <dev/ath/if_ath_debug.h>
93
94#ifdef ATH_TX99_DIAG
95#include <dev/ath/ath_tx99/ath_tx99.h>
96#endif
97
98#include <dev/ath/if_ath_misc.h>
99#include <dev/ath/if_ath_tx.h>
100#include <dev/ath/if_ath_tx_ht.h>
101
102/*
103 * Whether to use the 11n rate scenario functions or not
104 */
105static inline int
106ath_tx_is_11n(struct ath_softc *sc)
107{
108	return (sc->sc_ah->ah_magic == 0x20065416);
109}
110
111void
112ath_txfrag_cleanup(struct ath_softc *sc,
113	ath_bufhead *frags, struct ieee80211_node *ni)
114{
115	struct ath_buf *bf, *next;
116
117	ATH_TXBUF_LOCK_ASSERT(sc);
118
119	STAILQ_FOREACH_SAFE(bf, frags, bf_list, next) {
120		/* NB: bf assumed clean */
121		STAILQ_REMOVE_HEAD(frags, bf_list);
122		STAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
123		ieee80211_node_decref(ni);
124	}
125}
126
127/*
128 * Setup xmit of a fragmented frame.  Allocate a buffer
129 * for each frag and bump the node reference count to
130 * reflect the held reference to be setup by ath_tx_start.
131 */
132int
133ath_txfrag_setup(struct ath_softc *sc, ath_bufhead *frags,
134	struct mbuf *m0, struct ieee80211_node *ni)
135{
136	struct mbuf *m;
137	struct ath_buf *bf;
138
139	ATH_TXBUF_LOCK(sc);
140	for (m = m0->m_nextpkt; m != NULL; m = m->m_nextpkt) {
141		bf = _ath_getbuf_locked(sc);
142		if (bf == NULL) {	/* out of buffers, cleanup */
143			ath_txfrag_cleanup(sc, frags, ni);
144			break;
145		}
146		ieee80211_node_incref(ni);
147		STAILQ_INSERT_TAIL(frags, bf, bf_list);
148	}
149	ATH_TXBUF_UNLOCK(sc);
150
151	return !STAILQ_EMPTY(frags);
152}
153
154/*
155 * Reclaim mbuf resources.  For fragmented frames we
156 * need to claim each frag chained with m_nextpkt.
157 */
158void
159ath_freetx(struct mbuf *m)
160{
161	struct mbuf *next;
162
163	do {
164		next = m->m_nextpkt;
165		m->m_nextpkt = NULL;
166		m_freem(m);
167	} while ((m = next) != NULL);
168}
169
170static int
171ath_tx_dmasetup(struct ath_softc *sc, struct ath_buf *bf, struct mbuf *m0)
172{
173	struct mbuf *m;
174	int error;
175
176	/*
177	 * Load the DMA map so any coalescing is done.  This
178	 * also calculates the number of descriptors we need.
179	 */
180	error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0,
181				     bf->bf_segs, &bf->bf_nseg,
182				     BUS_DMA_NOWAIT);
183	if (error == EFBIG) {
184		/* XXX packet requires too many descriptors */
185		bf->bf_nseg = ATH_TXDESC+1;
186	} else if (error != 0) {
187		sc->sc_stats.ast_tx_busdma++;
188		ath_freetx(m0);
189		return error;
190	}
191	/*
192	 * Discard null packets and check for packets that
193	 * require too many TX descriptors.  We try to convert
194	 * the latter to a cluster.
195	 */
196	if (bf->bf_nseg > ATH_TXDESC) {		/* too many desc's, linearize */
197		sc->sc_stats.ast_tx_linear++;
198		m = m_collapse(m0, M_DONTWAIT, ATH_TXDESC);
199		if (m == NULL) {
200			ath_freetx(m0);
201			sc->sc_stats.ast_tx_nombuf++;
202			return ENOMEM;
203		}
204		m0 = m;
205		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0,
206					     bf->bf_segs, &bf->bf_nseg,
207					     BUS_DMA_NOWAIT);
208		if (error != 0) {
209			sc->sc_stats.ast_tx_busdma++;
210			ath_freetx(m0);
211			return error;
212		}
213		KASSERT(bf->bf_nseg <= ATH_TXDESC,
214		    ("too many segments after defrag; nseg %u", bf->bf_nseg));
215	} else if (bf->bf_nseg == 0) {		/* null packet, discard */
216		sc->sc_stats.ast_tx_nodata++;
217		ath_freetx(m0);
218		return EIO;
219	}
220	DPRINTF(sc, ATH_DEBUG_XMIT, "%s: m %p len %u\n",
221		__func__, m0, m0->m_pkthdr.len);
222	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
223	bf->bf_m = m0;
224
225	return 0;
226}
227
228static void
229ath_tx_chaindesclist(struct ath_softc *sc, struct ath_txq *txq, struct ath_buf *bf)
230{
231	struct ath_hal *ah = sc->sc_ah;
232	struct ath_desc *ds, *ds0;
233	int i;
234
235	/*
236	 * Fillin the remainder of the descriptor info.
237	 */
238	ds0 = ds = bf->bf_desc;
239	for (i = 0; i < bf->bf_nseg; i++, ds++) {
240		ds->ds_data = bf->bf_segs[i].ds_addr;
241		if (i == bf->bf_nseg - 1)
242			ds->ds_link = 0;
243		else
244			ds->ds_link = bf->bf_daddr + sizeof(*ds) * (i + 1);
245		ath_hal_filltxdesc(ah, ds
246			, bf->bf_segs[i].ds_len	/* segment length */
247			, i == 0		/* first segment */
248			, i == bf->bf_nseg - 1	/* last segment */
249			, ds0			/* first descriptor */
250		);
251		DPRINTF(sc, ATH_DEBUG_XMIT,
252			"%s: %d: %08x %08x %08x %08x %08x %08x\n",
253			__func__, i, ds->ds_link, ds->ds_data,
254			ds->ds_ctl0, ds->ds_ctl1, ds->ds_hw[0], ds->ds_hw[1]);
255	}
256
257}
258
259static void
260ath_tx_handoff(struct ath_softc *sc, struct ath_txq *txq, struct ath_buf *bf)
261{
262	struct ath_hal *ah = sc->sc_ah;
263
264	/* Fill in the details in the descriptor list */
265	ath_tx_chaindesclist(sc, txq, bf);
266
267	/*
268	 * Insert the frame on the outbound list and pass it on
269	 * to the hardware.  Multicast frames buffered for power
270	 * save stations and transmit from the CAB queue are stored
271	 * on a s/w only queue and loaded on to the CAB queue in
272	 * the SWBA handler since frames only go out on DTIM and
273	 * to avoid possible races.
274	 */
275	ATH_TXQ_LOCK(txq);
276	KASSERT((bf->bf_flags & ATH_BUF_BUSY) == 0,
277	     ("busy status 0x%x", bf->bf_flags));
278	if (txq->axq_qnum != ATH_TXQ_SWQ) {
279#ifdef IEEE80211_SUPPORT_TDMA
280		int qbusy;
281
282		ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
283		qbusy = ath_hal_txqenabled(ah, txq->axq_qnum);
284		if (txq->axq_link == NULL) {
285			/*
286			 * Be careful writing the address to TXDP.  If
287			 * the tx q is enabled then this write will be
288			 * ignored.  Normally this is not an issue but
289			 * when tdma is in use and the q is beacon gated
290			 * this race can occur.  If the q is busy then
291			 * defer the work to later--either when another
292			 * packet comes along or when we prepare a beacon
293			 * frame at SWBA.
294			 */
295			if (!qbusy) {
296				ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
297				txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
298				DPRINTF(sc, ATH_DEBUG_XMIT,
299				    "%s: TXDP[%u] = %p (%p) depth %d\n",
300				    __func__, txq->axq_qnum,
301				    (caddr_t)bf->bf_daddr, bf->bf_desc,
302				    txq->axq_depth);
303			} else {
304				txq->axq_flags |= ATH_TXQ_PUTPENDING;
305				DPRINTF(sc, ATH_DEBUG_TDMA | ATH_DEBUG_XMIT,
306				    "%s: Q%u busy, defer enable\n", __func__,
307				    txq->axq_qnum);
308			}
309		} else {
310			*txq->axq_link = bf->bf_daddr;
311			DPRINTF(sc, ATH_DEBUG_XMIT,
312			    "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
313			    txq->axq_qnum, txq->axq_link,
314			    (caddr_t)bf->bf_daddr, bf->bf_desc, txq->axq_depth);
315			if ((txq->axq_flags & ATH_TXQ_PUTPENDING) && !qbusy) {
316				/*
317				 * The q was busy when we previously tried
318				 * to write the address of the first buffer
319				 * in the chain.  Since it's not busy now
320				 * handle this chore.  We are certain the
321				 * buffer at the front is the right one since
322				 * axq_link is NULL only when the buffer list
323				 * is/was empty.
324				 */
325				ath_hal_puttxbuf(ah, txq->axq_qnum,
326					STAILQ_FIRST(&txq->axq_q)->bf_daddr);
327				txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
328				DPRINTF(sc, ATH_DEBUG_TDMA | ATH_DEBUG_XMIT,
329				    "%s: Q%u restarted\n", __func__,
330				    txq->axq_qnum);
331			}
332		}
333#else
334		ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
335		if (txq->axq_link == NULL) {
336			ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
337			DPRINTF(sc, ATH_DEBUG_XMIT,
338			    "%s: TXDP[%u] = %p (%p) depth %d\n",
339			    __func__, txq->axq_qnum,
340			    (caddr_t)bf->bf_daddr, bf->bf_desc,
341			    txq->axq_depth);
342		} else {
343			*txq->axq_link = bf->bf_daddr;
344			DPRINTF(sc, ATH_DEBUG_XMIT,
345			    "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
346			    txq->axq_qnum, txq->axq_link,
347			    (caddr_t)bf->bf_daddr, bf->bf_desc, txq->axq_depth);
348		}
349#endif /* IEEE80211_SUPPORT_TDMA */
350		txq->axq_link = &bf->bf_desc[bf->bf_nseg - 1].ds_link;
351		ath_hal_txstart(ah, txq->axq_qnum);
352	} else {
353		if (txq->axq_link != NULL) {
354			struct ath_buf *last = ATH_TXQ_LAST(txq);
355			struct ieee80211_frame *wh;
356
357			/* mark previous frame */
358			wh = mtod(last->bf_m, struct ieee80211_frame *);
359			wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
360			bus_dmamap_sync(sc->sc_dmat, last->bf_dmamap,
361			    BUS_DMASYNC_PREWRITE);
362
363			/* link descriptor */
364			*txq->axq_link = bf->bf_daddr;
365		}
366		ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
367		txq->axq_link = &bf->bf_desc[bf->bf_nseg - 1].ds_link;
368	}
369	ATH_TXQ_UNLOCK(txq);
370}
371
372static int
373ath_tx_tag_crypto(struct ath_softc *sc, struct ieee80211_node *ni,
374    struct mbuf *m0, int iswep, int isfrag, int *hdrlen, int *pktlen, int *keyix)
375{
376	if (iswep) {
377		const struct ieee80211_cipher *cip;
378		struct ieee80211_key *k;
379
380		/*
381		 * Construct the 802.11 header+trailer for an encrypted
382		 * frame. The only reason this can fail is because of an
383		 * unknown or unsupported cipher/key type.
384		 */
385		k = ieee80211_crypto_encap(ni, m0);
386		if (k == NULL) {
387			/*
388			 * This can happen when the key is yanked after the
389			 * frame was queued.  Just discard the frame; the
390			 * 802.11 layer counts failures and provides
391			 * debugging/diagnostics.
392			 */
393			return 0;
394		}
395		/*
396		 * Adjust the packet + header lengths for the crypto
397		 * additions and calculate the h/w key index.  When
398		 * a s/w mic is done the frame will have had any mic
399		 * added to it prior to entry so m0->m_pkthdr.len will
400		 * account for it. Otherwise we need to add it to the
401		 * packet length.
402		 */
403		cip = k->wk_cipher;
404		(*hdrlen) += cip->ic_header;
405		(*pktlen) += cip->ic_header + cip->ic_trailer;
406		/* NB: frags always have any TKIP MIC done in s/w */
407		if ((k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && !isfrag)
408			(*pktlen) += cip->ic_miclen;
409		(*keyix) = k->wk_keyix;
410	} else if (ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) {
411		/*
412		 * Use station key cache slot, if assigned.
413		 */
414		(*keyix) = ni->ni_ucastkey.wk_keyix;
415		if ((*keyix) == IEEE80211_KEYIX_NONE)
416			(*keyix) = HAL_TXKEYIX_INVALID;
417	} else
418		(*keyix) = HAL_TXKEYIX_INVALID;
419
420	return 1;
421}
422
423static void
424ath_tx_calc_ctsduration(struct ath_hal *ah, int rix, int cix,
425    int shortPreamble, int pktlen, const HAL_RATE_TABLE *rt,
426    int flags, u_int8_t *ctsrate, int *ctsduration)
427{
428	/*
429	 * CTS transmit rate is derived from the transmit rate
430	 * by looking in the h/w rate table.  We must also factor
431	 * in whether or not a short preamble is to be used.
432	 */
433	/* NB: cix is set above where RTS/CTS is enabled */
434	KASSERT(cix != 0xff, ("cix not setup"));
435	(*ctsrate) = rt->info[cix].rateCode;
436	/*
437	 * Compute the transmit duration based on the frame
438	 * size and the size of an ACK frame.  We call into the
439	 * HAL to do the computation since it depends on the
440	 * characteristics of the actual PHY being used.
441	 *
442	 * NB: CTS is assumed the same size as an ACK so we can
443	 *     use the precalculated ACK durations.
444	 */
445	if (shortPreamble) {
446		(*ctsrate) |= rt->info[cix].shortPreamble;
447		if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
448			(*ctsduration) += rt->info[cix].spAckDuration;
449		(*ctsduration) += ath_hal_computetxtime(ah,
450			rt, pktlen, rix, AH_TRUE);
451		if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
452			(*ctsduration) += rt->info[rix].spAckDuration;
453	} else {
454		if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
455			(*ctsduration) += rt->info[cix].lpAckDuration;
456		(*ctsduration) += ath_hal_computetxtime(ah,
457			rt, pktlen, rix, AH_FALSE);
458		if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
459			(*ctsduration) += rt->info[rix].lpAckDuration;
460	}
461}
462
463int
464ath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni, struct ath_buf *bf,
465    struct mbuf *m0)
466{
467	struct ieee80211vap *vap = ni->ni_vap;
468	struct ath_vap *avp = ATH_VAP(vap);
469	struct ath_hal *ah = sc->sc_ah;
470	struct ifnet *ifp = sc->sc_ifp;
471	struct ieee80211com *ic = ifp->if_l2com;
472	const struct chanAccParams *cap = &ic->ic_wme.wme_chanParams;
473	int error, iswep, ismcast, isfrag, ismrr;
474	int keyix, hdrlen, pktlen, try0;
475	u_int8_t rix, txrate, ctsrate;
476	u_int8_t cix = 0xff;		/* NB: silence compiler */
477	struct ath_desc *ds;
478	struct ath_txq *txq;
479	struct ieee80211_frame *wh;
480	u_int subtype, flags, ctsduration;
481	HAL_PKT_TYPE atype;
482	const HAL_RATE_TABLE *rt;
483	HAL_BOOL shortPreamble;
484	struct ath_node *an;
485	u_int pri;
486	uint8_t try[4], rate[4];
487
488	bzero(try, sizeof(try));
489	bzero(rate, sizeof(rate));
490
491	wh = mtod(m0, struct ieee80211_frame *);
492	iswep = wh->i_fc[1] & IEEE80211_FC1_WEP;
493	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
494	isfrag = m0->m_flags & M_FRAG;
495	hdrlen = ieee80211_anyhdrsize(wh);
496	/*
497	 * Packet length must not include any
498	 * pad bytes; deduct them here.
499	 */
500	pktlen = m0->m_pkthdr.len - (hdrlen & 3);
501
502	/* Handle encryption twiddling if needed */
503	if (! ath_tx_tag_crypto(sc, ni, m0, iswep, isfrag, &hdrlen, &pktlen, &keyix)) {
504		ath_freetx(m0);
505		return EIO;
506	}
507
508	/* packet header may have moved, reset our local pointer */
509	wh = mtod(m0, struct ieee80211_frame *);
510
511	pktlen += IEEE80211_CRC_LEN;
512
513	/*
514	 * Load the DMA map so any coalescing is done.  This
515	 * also calculates the number of descriptors we need.
516	 */
517	error = ath_tx_dmasetup(sc, bf, m0);
518	if (error != 0)
519		return error;
520	bf->bf_node = ni;			/* NB: held reference */
521	m0 = bf->bf_m;				/* NB: may have changed */
522	wh = mtod(m0, struct ieee80211_frame *);
523
524	/* setup descriptors */
525	ds = bf->bf_desc;
526	rt = sc->sc_currates;
527	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
528
529	/*
530	 * NB: the 802.11 layer marks whether or not we should
531	 * use short preamble based on the current mode and
532	 * negotiated parameters.
533	 */
534	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
535	    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
536		shortPreamble = AH_TRUE;
537		sc->sc_stats.ast_tx_shortpre++;
538	} else {
539		shortPreamble = AH_FALSE;
540	}
541
542	an = ATH_NODE(ni);
543	flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for crypto errs */
544	ismrr = 0;				/* default no multi-rate retry*/
545	pri = M_WME_GETAC(m0);			/* honor classification */
546	/* XXX use txparams instead of fixed values */
547	/*
548	 * Calculate Atheros packet type from IEEE80211 packet header,
549	 * setup for rate calculations, and select h/w transmit queue.
550	 */
551	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
552	case IEEE80211_FC0_TYPE_MGT:
553		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
554		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON)
555			atype = HAL_PKT_TYPE_BEACON;
556		else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
557			atype = HAL_PKT_TYPE_PROBE_RESP;
558		else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM)
559			atype = HAL_PKT_TYPE_ATIM;
560		else
561			atype = HAL_PKT_TYPE_NORMAL;	/* XXX */
562		rix = an->an_mgmtrix;
563		txrate = rt->info[rix].rateCode;
564		if (shortPreamble)
565			txrate |= rt->info[rix].shortPreamble;
566		try0 = ATH_TXMGTTRY;
567		flags |= HAL_TXDESC_INTREQ;	/* force interrupt */
568		break;
569	case IEEE80211_FC0_TYPE_CTL:
570		atype = HAL_PKT_TYPE_PSPOLL;	/* stop setting of duration */
571		rix = an->an_mgmtrix;
572		txrate = rt->info[rix].rateCode;
573		if (shortPreamble)
574			txrate |= rt->info[rix].shortPreamble;
575		try0 = ATH_TXMGTTRY;
576		flags |= HAL_TXDESC_INTREQ;	/* force interrupt */
577		break;
578	case IEEE80211_FC0_TYPE_DATA:
579		atype = HAL_PKT_TYPE_NORMAL;		/* default */
580		/*
581		 * Data frames: multicast frames go out at a fixed rate,
582		 * EAPOL frames use the mgmt frame rate; otherwise consult
583		 * the rate control module for the rate to use.
584		 */
585		if (ismcast) {
586			rix = an->an_mcastrix;
587			txrate = rt->info[rix].rateCode;
588			if (shortPreamble)
589				txrate |= rt->info[rix].shortPreamble;
590			try0 = 1;
591		} else if (m0->m_flags & M_EAPOL) {
592			/* XXX? maybe always use long preamble? */
593			rix = an->an_mgmtrix;
594			txrate = rt->info[rix].rateCode;
595			if (shortPreamble)
596				txrate |= rt->info[rix].shortPreamble;
597			try0 = ATH_TXMAXTRY;	/* XXX?too many? */
598		} else {
599			ath_rate_findrate(sc, an, shortPreamble, pktlen,
600				&rix, &try0, &txrate);
601			sc->sc_txrix = rix;		/* for LED blinking */
602			sc->sc_lastdatarix = rix;	/* for fast frames */
603			if (try0 != ATH_TXMAXTRY)
604				ismrr = 1;
605		}
606		if (cap->cap_wmeParams[pri].wmep_noackPolicy)
607			flags |= HAL_TXDESC_NOACK;
608		break;
609	default:
610		if_printf(ifp, "bogus frame type 0x%x (%s)\n",
611			wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__);
612		/* XXX statistic */
613		ath_freetx(m0);
614		return EIO;
615	}
616	txq = sc->sc_ac2q[pri];
617
618	/*
619	 * When servicing one or more stations in power-save mode
620	 * (or) if there is some mcast data waiting on the mcast
621	 * queue (to prevent out of order delivery) multicast
622	 * frames must be buffered until after the beacon.
623	 */
624	if (ismcast && (vap->iv_ps_sta || avp->av_mcastq.axq_depth))
625		txq = &avp->av_mcastq;
626
627	/*
628	 * Calculate miscellaneous flags.
629	 */
630	if (ismcast) {
631		flags |= HAL_TXDESC_NOACK;	/* no ack on broad/multicast */
632	} else if (pktlen > vap->iv_rtsthreshold &&
633	    (ni->ni_ath_flags & IEEE80211_NODE_FF) == 0) {
634		flags |= HAL_TXDESC_RTSENA;	/* RTS based on frame length */
635		cix = rt->info[rix].controlRate;
636		sc->sc_stats.ast_tx_rts++;
637	}
638	if (flags & HAL_TXDESC_NOACK)		/* NB: avoid double counting */
639		sc->sc_stats.ast_tx_noack++;
640#ifdef IEEE80211_SUPPORT_TDMA
641	if (sc->sc_tdma && (flags & HAL_TXDESC_NOACK) == 0) {
642		DPRINTF(sc, ATH_DEBUG_TDMA,
643		    "%s: discard frame, ACK required w/ TDMA\n", __func__);
644		sc->sc_stats.ast_tdma_ack++;
645		ath_freetx(m0);
646		return EIO;
647	}
648#endif
649
650	/*
651	 * If 802.11g protection is enabled, determine whether
652	 * to use RTS/CTS or just CTS.  Note that this is only
653	 * done for OFDM unicast frames.
654	 */
655	if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
656	    rt->info[rix].phy == IEEE80211_T_OFDM &&
657	    (flags & HAL_TXDESC_NOACK) == 0) {
658		/* XXX fragments must use CCK rates w/ protection */
659		if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
660			flags |= HAL_TXDESC_RTSENA;
661		else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
662			flags |= HAL_TXDESC_CTSENA;
663		if (isfrag) {
664			/*
665			 * For frags it would be desirable to use the
666			 * highest CCK rate for RTS/CTS.  But stations
667			 * farther away may detect it at a lower CCK rate
668			 * so use the configured protection rate instead
669			 * (for now).
670			 */
671			cix = rt->info[sc->sc_protrix].controlRate;
672		} else
673			cix = rt->info[sc->sc_protrix].controlRate;
674		sc->sc_stats.ast_tx_protect++;
675	}
676
677	/*
678	 * Calculate duration.  This logically belongs in the 802.11
679	 * layer but it lacks sufficient information to calculate it.
680	 */
681	if ((flags & HAL_TXDESC_NOACK) == 0 &&
682	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL) {
683		u_int16_t dur;
684		if (shortPreamble)
685			dur = rt->info[rix].spAckDuration;
686		else
687			dur = rt->info[rix].lpAckDuration;
688		if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) {
689			dur += dur;		/* additional SIFS+ACK */
690			KASSERT(m0->m_nextpkt != NULL, ("no fragment"));
691			/*
692			 * Include the size of next fragment so NAV is
693			 * updated properly.  The last fragment uses only
694			 * the ACK duration
695			 */
696			dur += ath_hal_computetxtime(ah, rt,
697					m0->m_nextpkt->m_pkthdr.len,
698					rix, shortPreamble);
699		}
700		if (isfrag) {
701			/*
702			 * Force hardware to use computed duration for next
703			 * fragment by disabling multi-rate retry which updates
704			 * duration based on the multi-rate duration table.
705			 */
706			ismrr = 0;
707			try0 = ATH_TXMGTTRY;	/* XXX? */
708		}
709		*(u_int16_t *)wh->i_dur = htole16(dur);
710	}
711
712	/*
713	 * Calculate RTS/CTS rate and duration if needed.
714	 */
715	ctsduration = 0;
716	if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA)) {
717		(void) ath_tx_calc_ctsduration(ah, rix, cix, shortPreamble, pktlen,
718		    rt, flags, &ctsrate, &ctsduration);
719		/*
720		 * Must disable multi-rate retry when using RTS/CTS.
721		 */
722		ismrr = 0;
723		try0 = ATH_TXMGTTRY;		/* XXX */
724	} else
725		ctsrate = 0;
726
727	/*
728	 * At this point we are committed to sending the frame
729	 * and we don't need to look at m_nextpkt; clear it in
730	 * case this frame is part of frag chain.
731	 */
732	m0->m_nextpkt = NULL;
733
734	if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
735		ieee80211_dump_pkt(ic, mtod(m0, const uint8_t *), m0->m_len,
736		    sc->sc_hwmap[rix].ieeerate, -1);
737
738	if (ieee80211_radiotap_active_vap(vap)) {
739		u_int64_t tsf = ath_hal_gettsf64(ah);
740
741		sc->sc_tx_th.wt_tsf = htole64(tsf);
742		sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
743		if (iswep)
744			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
745		if (isfrag)
746			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
747		sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
748		sc->sc_tx_th.wt_txpower = ni->ni_txpower;
749		sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
750
751		ieee80211_radiotap_tx(vap, m0);
752	}
753
754	/*
755	 * Determine if a tx interrupt should be generated for
756	 * this descriptor.  We take a tx interrupt to reap
757	 * descriptors when the h/w hits an EOL condition or
758	 * when the descriptor is specifically marked to generate
759	 * an interrupt.  We periodically mark descriptors in this
760	 * way to insure timely replenishing of the supply needed
761	 * for sending frames.  Defering interrupts reduces system
762	 * load and potentially allows more concurrent work to be
763	 * done but if done to aggressively can cause senders to
764	 * backup.
765	 *
766	 * NB: use >= to deal with sc_txintrperiod changing
767	 *     dynamically through sysctl.
768	 */
769	if (flags & HAL_TXDESC_INTREQ) {
770		txq->axq_intrcnt = 0;
771	} else if (++txq->axq_intrcnt >= sc->sc_txintrperiod) {
772		flags |= HAL_TXDESC_INTREQ;
773		txq->axq_intrcnt = 0;
774	}
775
776	if (ath_tx_is_11n(sc)) {
777		rate[0] = rix;
778		try[0] = try0;
779	}
780
781	/*
782	 * Formulate first tx descriptor with tx controls.
783	 */
784	/* XXX check return value? */
785	/* XXX is this ok to call for 11n descriptors? */
786	/* XXX or should it go through the first, next, last 11n calls? */
787	ath_hal_setuptxdesc(ah, ds
788		, pktlen		/* packet length */
789		, hdrlen		/* header length */
790		, atype			/* Atheros packet type */
791		, ni->ni_txpower	/* txpower */
792		, txrate, try0		/* series 0 rate/tries */
793		, keyix			/* key cache index */
794		, sc->sc_txantenna	/* antenna mode */
795		, flags			/* flags */
796		, ctsrate		/* rts/cts rate */
797		, ctsduration		/* rts/cts duration */
798	);
799	bf->bf_txflags = flags;
800	/*
801	 * Setup the multi-rate retry state only when we're
802	 * going to use it.  This assumes ath_hal_setuptxdesc
803	 * initializes the descriptors (so we don't have to)
804	 * when the hardware supports multi-rate retry and
805	 * we don't use it.
806	 */
807        if (ismrr) {
808                if (ath_tx_is_11n(sc))
809                        ath_rate_getxtxrates(sc, an, rix, rate, try);
810                else
811                        ath_rate_setupxtxdesc(sc, an, ds, shortPreamble, rix);
812        }
813
814        if (ath_tx_is_11n(sc)) {
815                ath_buf_set_rate(sc, ni, bf, pktlen, flags, ctsrate, rate, try);
816        }
817
818	ath_tx_handoff(sc, txq, bf);
819	return 0;
820}
821
822static int
823ath_tx_raw_start(struct ath_softc *sc, struct ieee80211_node *ni,
824	struct ath_buf *bf, struct mbuf *m0,
825	const struct ieee80211_bpf_params *params)
826{
827	struct ifnet *ifp = sc->sc_ifp;
828	struct ieee80211com *ic = ifp->if_l2com;
829	struct ath_hal *ah = sc->sc_ah;
830	struct ieee80211vap *vap = ni->ni_vap;
831	int error, ismcast, ismrr;
832	int keyix, hdrlen, pktlen, try0, txantenna;
833	u_int8_t rix, cix, txrate, ctsrate, rate1, rate2, rate3;
834	struct ieee80211_frame *wh;
835	u_int flags, ctsduration;
836	HAL_PKT_TYPE atype;
837	const HAL_RATE_TABLE *rt;
838	struct ath_desc *ds;
839	u_int pri;
840	uint8_t try[4], rate[4];
841
842	bzero(try, sizeof(try));
843	bzero(rate, sizeof(rate));
844
845	wh = mtod(m0, struct ieee80211_frame *);
846	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
847	hdrlen = ieee80211_anyhdrsize(wh);
848	/*
849	 * Packet length must not include any
850	 * pad bytes; deduct them here.
851	 */
852	/* XXX honor IEEE80211_BPF_DATAPAD */
853	pktlen = m0->m_pkthdr.len - (hdrlen & 3) + IEEE80211_CRC_LEN;
854
855	/* Handle encryption twiddling if needed */
856	if (! ath_tx_tag_crypto(sc, ni, m0, params->ibp_flags & IEEE80211_BPF_CRYPTO, 0, &hdrlen, &pktlen, &keyix)) {
857		ath_freetx(m0);
858		return EIO;
859	}
860	/* packet header may have moved, reset our local pointer */
861	wh = mtod(m0, struct ieee80211_frame *);
862
863	error = ath_tx_dmasetup(sc, bf, m0);
864	if (error != 0)
865		return error;
866	m0 = bf->bf_m;				/* NB: may have changed */
867	wh = mtod(m0, struct ieee80211_frame *);
868	bf->bf_node = ni;			/* NB: held reference */
869
870	flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for crypto errs */
871	flags |= HAL_TXDESC_INTREQ;		/* force interrupt */
872	if (params->ibp_flags & IEEE80211_BPF_RTS)
873		flags |= HAL_TXDESC_RTSENA;
874	else if (params->ibp_flags & IEEE80211_BPF_CTS)
875		flags |= HAL_TXDESC_CTSENA;
876	/* XXX leave ismcast to injector? */
877	if ((params->ibp_flags & IEEE80211_BPF_NOACK) || ismcast)
878		flags |= HAL_TXDESC_NOACK;
879
880	rt = sc->sc_currates;
881	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
882	rix = ath_tx_findrix(sc, params->ibp_rate0);
883	txrate = rt->info[rix].rateCode;
884	if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
885		txrate |= rt->info[rix].shortPreamble;
886	sc->sc_txrix = rix;
887	try0 = params->ibp_try0;
888	ismrr = (params->ibp_try1 != 0);
889	txantenna = params->ibp_pri >> 2;
890	if (txantenna == 0)			/* XXX? */
891		txantenna = sc->sc_txantenna;
892
893	ctsduration = 0;
894	if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA)) {
895		cix = ath_tx_findrix(sc, params->ibp_ctsrate);
896		(void) ath_tx_calc_ctsduration(ah, rix, cix,
897		    params->ibp_flags & IEEE80211_BPF_SHORTPRE, pktlen,
898		    rt, flags, &ctsrate, &ctsduration);
899		/*
900		 * Must disable multi-rate retry when using RTS/CTS.
901		 */
902		ismrr = 0;			/* XXX */
903	} else
904		ctsrate = 0;
905
906	pri = params->ibp_pri & 3;
907	/*
908	 * NB: we mark all packets as type PSPOLL so the h/w won't
909	 * set the sequence number, duration, etc.
910	 */
911	atype = HAL_PKT_TYPE_PSPOLL;
912
913	if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
914		ieee80211_dump_pkt(ic, mtod(m0, caddr_t), m0->m_len,
915		    sc->sc_hwmap[rix].ieeerate, -1);
916
917	if (ieee80211_radiotap_active_vap(vap)) {
918		u_int64_t tsf = ath_hal_gettsf64(ah);
919
920		sc->sc_tx_th.wt_tsf = htole64(tsf);
921		sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
922		if (wh->i_fc[1] & IEEE80211_FC1_WEP)
923			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
924		if (m0->m_flags & M_FRAG)
925			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
926		sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
927		sc->sc_tx_th.wt_txpower = ni->ni_txpower;
928		sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
929
930		ieee80211_radiotap_tx(vap, m0);
931	}
932
933	/*
934	 * Formulate first tx descriptor with tx controls.
935	 */
936	ds = bf->bf_desc;
937	/* XXX check return value? */
938	ath_hal_setuptxdesc(ah, ds
939		, pktlen		/* packet length */
940		, hdrlen		/* header length */
941		, atype			/* Atheros packet type */
942		, params->ibp_power	/* txpower */
943		, txrate, try0		/* series 0 rate/tries */
944		, keyix			/* key cache index */
945		, txantenna		/* antenna mode */
946		, flags			/* flags */
947		, ctsrate		/* rts/cts rate */
948		, ctsduration		/* rts/cts duration */
949	);
950	bf->bf_txflags = flags;
951
952	if (ath_tx_is_11n(sc)) {
953		rate[0] = ath_tx_findrix(sc, params->ibp_rate0);
954		try[0] = params->ibp_try0;
955
956		if (ismrr) {
957			/* Remember, rate[] is actually an array of rix's -adrian */
958			rate[0] = ath_tx_findrix(sc, params->ibp_rate0);
959			rate[1] = ath_tx_findrix(sc, params->ibp_rate1);
960			rate[2] = ath_tx_findrix(sc, params->ibp_rate2);
961			rate[3] = ath_tx_findrix(sc, params->ibp_rate3);
962
963			try[0] = params->ibp_try0;
964			try[1] = params->ibp_try1;
965			try[2] = params->ibp_try2;
966			try[3] = params->ibp_try3;
967		}
968	} else {
969		if (ismrr) {
970			rix = ath_tx_findrix(sc, params->ibp_rate1);
971			rate1 = rt->info[rix].rateCode;
972			if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
973				rate1 |= rt->info[rix].shortPreamble;
974			if (params->ibp_try2) {
975				rix = ath_tx_findrix(sc, params->ibp_rate2);
976				rate2 = rt->info[rix].rateCode;
977				if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
978					rate2 |= rt->info[rix].shortPreamble;
979			} else
980				rate2 = 0;
981			if (params->ibp_try3) {
982				rix = ath_tx_findrix(sc, params->ibp_rate3);
983				rate3 = rt->info[rix].rateCode;
984				if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
985					rate3 |= rt->info[rix].shortPreamble;
986			} else
987				rate3 = 0;
988			ath_hal_setupxtxdesc(ah, ds
989				, rate1, params->ibp_try1	/* series 1 */
990				, rate2, params->ibp_try2	/* series 2 */
991				, rate3, params->ibp_try3	/* series 3 */
992			);
993		}
994	}
995
996	if (ath_tx_is_11n(sc)) {
997		/*
998		 * notice that rix doesn't include any of the "magic" flags txrate
999		 * does for communicating "other stuff" to the HAL.
1000		 */
1001		ath_buf_set_rate(sc, ni, bf, pktlen, flags, ctsrate, rate, try);
1002	}
1003
1004	/* NB: no buffered multicast in power save support */
1005	ath_tx_handoff(sc, sc->sc_ac2q[pri], bf);
1006	return 0;
1007}
1008
1009int
1010ath_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1011	const struct ieee80211_bpf_params *params)
1012{
1013	struct ieee80211com *ic = ni->ni_ic;
1014	struct ifnet *ifp = ic->ic_ifp;
1015	struct ath_softc *sc = ifp->if_softc;
1016	struct ath_buf *bf;
1017	int error;
1018
1019	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->sc_invalid) {
1020		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: discard frame, %s", __func__,
1021		    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ?
1022			"!running" : "invalid");
1023		m_freem(m);
1024		error = ENETDOWN;
1025		goto bad;
1026	}
1027	/*
1028	 * Grab a TX buffer and associated resources.
1029	 */
1030	bf = ath_getbuf(sc);
1031	if (bf == NULL) {
1032		sc->sc_stats.ast_tx_nobuf++;
1033		m_freem(m);
1034		error = ENOBUFS;
1035		goto bad;
1036	}
1037
1038	if (params == NULL) {
1039		/*
1040		 * Legacy path; interpret frame contents to decide
1041		 * precisely how to send the frame.
1042		 */
1043		if (ath_tx_start(sc, ni, bf, m)) {
1044			error = EIO;		/* XXX */
1045			goto bad2;
1046		}
1047	} else {
1048		/*
1049		 * Caller supplied explicit parameters to use in
1050		 * sending the frame.
1051		 */
1052		if (ath_tx_raw_start(sc, ni, bf, m, params)) {
1053			error = EIO;		/* XXX */
1054			goto bad2;
1055		}
1056	}
1057	sc->sc_wd_timer = 5;
1058	ifp->if_opackets++;
1059	sc->sc_stats.ast_tx_raw++;
1060
1061	return 0;
1062bad2:
1063	ATH_TXBUF_LOCK(sc);
1064	STAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
1065	ATH_TXBUF_UNLOCK(sc);
1066bad:
1067	ifp->if_oerrors++;
1068	sc->sc_stats.ast_tx_raw_fail++;
1069	ieee80211_free_node(ni);
1070	return error;
1071}
1072