if_ath_tx.c revision 218932
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 218932 2011-02-22 00:37:53Z 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 uint8_t
424ath_tx_get_rtscts_rate(struct ath_hal *ah, const HAL_RATE_TABLE *rt,
425    int rix, int cix, int shortPreamble)
426{
427	uint8_t ctsrate;
428
429	/*
430	 * CTS transmit rate is derived from the transmit rate
431	 * by looking in the h/w rate table.  We must also factor
432	 * in whether or not a short preamble is to be used.
433	 */
434	/* NB: cix is set above where RTS/CTS is enabled */
435	KASSERT(cix != 0xff, ("cix not setup"));
436	ctsrate = rt->info[cix].rateCode;
437
438	/* XXX this should only matter for legacy rates */
439	if (shortPreamble)
440		ctsrate |= rt->info[cix].shortPreamble;
441
442	return ctsrate;
443}
444
445
446/*
447 * Calculate the RTS/CTS duration for legacy frames.
448 */
449static int
450ath_tx_calc_ctsduration(struct ath_hal *ah, int rix, int cix,
451    int shortPreamble, int pktlen, const HAL_RATE_TABLE *rt,
452    int flags)
453{
454	int ctsduration = 0;
455
456	/* This mustn't be called for HT modes */
457	if (rt->info[cix].phy == IEEE80211_T_HT) {
458		printf("%s: HT rate where it shouldn't be (0x%x)\n",
459		    __func__, rt->info[cix].rateCode);
460		return -1;
461	}
462
463	/*
464	 * Compute the transmit duration based on the frame
465	 * size and the size of an ACK frame.  We call into the
466	 * HAL to do the computation since it depends on the
467	 * characteristics of the actual PHY being used.
468	 *
469	 * NB: CTS is assumed the same size as an ACK so we can
470	 *     use the precalculated ACK durations.
471	 */
472	if (shortPreamble) {
473		if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
474			ctsduration += rt->info[cix].spAckDuration;
475		ctsduration += ath_hal_computetxtime(ah,
476			rt, pktlen, rix, AH_TRUE);
477		if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
478			ctsduration += rt->info[rix].spAckDuration;
479	} else {
480		if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
481			ctsduration += rt->info[cix].lpAckDuration;
482		ctsduration += ath_hal_computetxtime(ah,
483			rt, pktlen, rix, AH_FALSE);
484		if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
485			ctsduration += rt->info[rix].lpAckDuration;
486	}
487
488	return ctsduration;
489}
490
491int
492ath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni, struct ath_buf *bf,
493    struct mbuf *m0)
494{
495	struct ieee80211vap *vap = ni->ni_vap;
496	struct ath_vap *avp = ATH_VAP(vap);
497	struct ath_hal *ah = sc->sc_ah;
498	struct ifnet *ifp = sc->sc_ifp;
499	struct ieee80211com *ic = ifp->if_l2com;
500	const struct chanAccParams *cap = &ic->ic_wme.wme_chanParams;
501	int error, iswep, ismcast, isfrag, ismrr;
502	int keyix, hdrlen, pktlen, try0;
503	u_int8_t rix, txrate, ctsrate;
504	u_int8_t cix = 0xff;		/* NB: silence compiler */
505	struct ath_desc *ds;
506	struct ath_txq *txq;
507	struct ieee80211_frame *wh;
508	u_int subtype, flags, ctsduration;
509	HAL_PKT_TYPE atype;
510	const HAL_RATE_TABLE *rt;
511	HAL_BOOL shortPreamble;
512	struct ath_node *an;
513	u_int pri;
514	uint8_t try[4], rate[4];
515
516	bzero(try, sizeof(try));
517	bzero(rate, sizeof(rate));
518
519	wh = mtod(m0, struct ieee80211_frame *);
520	iswep = wh->i_fc[1] & IEEE80211_FC1_WEP;
521	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
522	isfrag = m0->m_flags & M_FRAG;
523	hdrlen = ieee80211_anyhdrsize(wh);
524	/*
525	 * Packet length must not include any
526	 * pad bytes; deduct them here.
527	 */
528	pktlen = m0->m_pkthdr.len - (hdrlen & 3);
529
530	/* Handle encryption twiddling if needed */
531	if (! ath_tx_tag_crypto(sc, ni, m0, iswep, isfrag, &hdrlen, &pktlen, &keyix)) {
532		ath_freetx(m0);
533		return EIO;
534	}
535
536	/* packet header may have moved, reset our local pointer */
537	wh = mtod(m0, struct ieee80211_frame *);
538
539	pktlen += IEEE80211_CRC_LEN;
540
541	/*
542	 * Load the DMA map so any coalescing is done.  This
543	 * also calculates the number of descriptors we need.
544	 */
545	error = ath_tx_dmasetup(sc, bf, m0);
546	if (error != 0)
547		return error;
548	bf->bf_node = ni;			/* NB: held reference */
549	m0 = bf->bf_m;				/* NB: may have changed */
550	wh = mtod(m0, struct ieee80211_frame *);
551
552	/* setup descriptors */
553	ds = bf->bf_desc;
554	rt = sc->sc_currates;
555	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
556
557	/*
558	 * NB: the 802.11 layer marks whether or not we should
559	 * use short preamble based on the current mode and
560	 * negotiated parameters.
561	 */
562	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
563	    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
564		shortPreamble = AH_TRUE;
565		sc->sc_stats.ast_tx_shortpre++;
566	} else {
567		shortPreamble = AH_FALSE;
568	}
569
570	an = ATH_NODE(ni);
571	flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for crypto errs */
572	ismrr = 0;				/* default no multi-rate retry*/
573	pri = M_WME_GETAC(m0);			/* honor classification */
574	/* XXX use txparams instead of fixed values */
575	/*
576	 * Calculate Atheros packet type from IEEE80211 packet header,
577	 * setup for rate calculations, and select h/w transmit queue.
578	 */
579	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
580	case IEEE80211_FC0_TYPE_MGT:
581		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
582		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON)
583			atype = HAL_PKT_TYPE_BEACON;
584		else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
585			atype = HAL_PKT_TYPE_PROBE_RESP;
586		else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM)
587			atype = HAL_PKT_TYPE_ATIM;
588		else
589			atype = HAL_PKT_TYPE_NORMAL;	/* XXX */
590		rix = an->an_mgmtrix;
591		txrate = rt->info[rix].rateCode;
592		if (shortPreamble)
593			txrate |= rt->info[rix].shortPreamble;
594		try0 = ATH_TXMGTTRY;
595		flags |= HAL_TXDESC_INTREQ;	/* force interrupt */
596		break;
597	case IEEE80211_FC0_TYPE_CTL:
598		atype = HAL_PKT_TYPE_PSPOLL;	/* stop setting of duration */
599		rix = an->an_mgmtrix;
600		txrate = rt->info[rix].rateCode;
601		if (shortPreamble)
602			txrate |= rt->info[rix].shortPreamble;
603		try0 = ATH_TXMGTTRY;
604		flags |= HAL_TXDESC_INTREQ;	/* force interrupt */
605		break;
606	case IEEE80211_FC0_TYPE_DATA:
607		atype = HAL_PKT_TYPE_NORMAL;		/* default */
608		/*
609		 * Data frames: multicast frames go out at a fixed rate,
610		 * EAPOL frames use the mgmt frame rate; otherwise consult
611		 * the rate control module for the rate to use.
612		 */
613		if (ismcast) {
614			rix = an->an_mcastrix;
615			txrate = rt->info[rix].rateCode;
616			if (shortPreamble)
617				txrate |= rt->info[rix].shortPreamble;
618			try0 = 1;
619		} else if (m0->m_flags & M_EAPOL) {
620			/* XXX? maybe always use long preamble? */
621			rix = an->an_mgmtrix;
622			txrate = rt->info[rix].rateCode;
623			if (shortPreamble)
624				txrate |= rt->info[rix].shortPreamble;
625			try0 = ATH_TXMAXTRY;	/* XXX?too many? */
626		} else {
627			ath_rate_findrate(sc, an, shortPreamble, pktlen,
628				&rix, &try0, &txrate);
629			sc->sc_txrix = rix;		/* for LED blinking */
630			sc->sc_lastdatarix = rix;	/* for fast frames */
631			if (try0 != ATH_TXMAXTRY)
632				ismrr = 1;
633		}
634		if (cap->cap_wmeParams[pri].wmep_noackPolicy)
635			flags |= HAL_TXDESC_NOACK;
636		break;
637	default:
638		if_printf(ifp, "bogus frame type 0x%x (%s)\n",
639			wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__);
640		/* XXX statistic */
641		ath_freetx(m0);
642		return EIO;
643	}
644	txq = sc->sc_ac2q[pri];
645
646	/*
647	 * When servicing one or more stations in power-save mode
648	 * (or) if there is some mcast data waiting on the mcast
649	 * queue (to prevent out of order delivery) multicast
650	 * frames must be buffered until after the beacon.
651	 */
652	if (ismcast && (vap->iv_ps_sta || avp->av_mcastq.axq_depth))
653		txq = &avp->av_mcastq;
654
655	/*
656	 * Calculate miscellaneous flags.
657	 */
658	if (ismcast) {
659		flags |= HAL_TXDESC_NOACK;	/* no ack on broad/multicast */
660	} else if (pktlen > vap->iv_rtsthreshold &&
661	    (ni->ni_ath_flags & IEEE80211_NODE_FF) == 0) {
662		flags |= HAL_TXDESC_RTSENA;	/* RTS based on frame length */
663		cix = rt->info[rix].controlRate;
664		sc->sc_stats.ast_tx_rts++;
665	}
666	if (flags & HAL_TXDESC_NOACK)		/* NB: avoid double counting */
667		sc->sc_stats.ast_tx_noack++;
668#ifdef IEEE80211_SUPPORT_TDMA
669	if (sc->sc_tdma && (flags & HAL_TXDESC_NOACK) == 0) {
670		DPRINTF(sc, ATH_DEBUG_TDMA,
671		    "%s: discard frame, ACK required w/ TDMA\n", __func__);
672		sc->sc_stats.ast_tdma_ack++;
673		ath_freetx(m0);
674		return EIO;
675	}
676#endif
677
678	/*
679	 * If 802.11g protection is enabled, determine whether
680	 * to use RTS/CTS or just CTS.  Note that this is only
681	 * done for OFDM unicast frames.
682	 */
683	if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
684	    rt->info[rix].phy == IEEE80211_T_OFDM &&
685	    (flags & HAL_TXDESC_NOACK) == 0) {
686		/* XXX fragments must use CCK rates w/ protection */
687		if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
688			flags |= HAL_TXDESC_RTSENA;
689		else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
690			flags |= HAL_TXDESC_CTSENA;
691		if (isfrag) {
692			/*
693			 * For frags it would be desirable to use the
694			 * highest CCK rate for RTS/CTS.  But stations
695			 * farther away may detect it at a lower CCK rate
696			 * so use the configured protection rate instead
697			 * (for now).
698			 */
699			cix = rt->info[sc->sc_protrix].controlRate;
700		} else
701			cix = rt->info[sc->sc_protrix].controlRate;
702		sc->sc_stats.ast_tx_protect++;
703	}
704
705	/*
706	 * Calculate duration.  This logically belongs in the 802.11
707	 * layer but it lacks sufficient information to calculate it.
708	 */
709	if ((flags & HAL_TXDESC_NOACK) == 0 &&
710	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL) {
711		u_int16_t dur;
712		if (shortPreamble)
713			dur = rt->info[rix].spAckDuration;
714		else
715			dur = rt->info[rix].lpAckDuration;
716		if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) {
717			dur += dur;		/* additional SIFS+ACK */
718			KASSERT(m0->m_nextpkt != NULL, ("no fragment"));
719			/*
720			 * Include the size of next fragment so NAV is
721			 * updated properly.  The last fragment uses only
722			 * the ACK duration
723			 */
724			dur += ath_hal_computetxtime(ah, rt,
725					m0->m_nextpkt->m_pkthdr.len,
726					rix, shortPreamble);
727		}
728		if (isfrag) {
729			/*
730			 * Force hardware to use computed duration for next
731			 * fragment by disabling multi-rate retry which updates
732			 * duration based on the multi-rate duration table.
733			 */
734			ismrr = 0;
735			try0 = ATH_TXMGTTRY;	/* XXX? */
736		}
737		*(u_int16_t *)wh->i_dur = htole16(dur);
738	}
739
740	/*
741	 * Calculate RTS/CTS rate and duration if needed.
742	 */
743	ctsduration = 0;
744	if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA)) {
745		ctsrate = ath_tx_get_rtscts_rate(ah, rt, rix, cix, shortPreamble);
746
747		/* The 11n chipsets do ctsduration calculations for you */
748		if (! ath_tx_is_11n(sc))
749			ctsduration = ath_tx_calc_ctsduration(ah, rix, cix, shortPreamble,
750			    pktlen, rt, flags);
751		/*
752		 * Must disable multi-rate retry when using RTS/CTS.
753		 */
754		ismrr = 0;
755		try0 = ATH_TXMGTTRY;		/* XXX */
756	} else
757		ctsrate = 0;
758
759	/*
760	 * At this point we are committed to sending the frame
761	 * and we don't need to look at m_nextpkt; clear it in
762	 * case this frame is part of frag chain.
763	 */
764	m0->m_nextpkt = NULL;
765
766	if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
767		ieee80211_dump_pkt(ic, mtod(m0, const uint8_t *), m0->m_len,
768		    sc->sc_hwmap[rix].ieeerate, -1);
769
770	if (ieee80211_radiotap_active_vap(vap)) {
771		u_int64_t tsf = ath_hal_gettsf64(ah);
772
773		sc->sc_tx_th.wt_tsf = htole64(tsf);
774		sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
775		if (iswep)
776			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
777		if (isfrag)
778			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
779		sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
780		sc->sc_tx_th.wt_txpower = ni->ni_txpower;
781		sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
782
783		ieee80211_radiotap_tx(vap, m0);
784	}
785
786	/*
787	 * Determine if a tx interrupt should be generated for
788	 * this descriptor.  We take a tx interrupt to reap
789	 * descriptors when the h/w hits an EOL condition or
790	 * when the descriptor is specifically marked to generate
791	 * an interrupt.  We periodically mark descriptors in this
792	 * way to insure timely replenishing of the supply needed
793	 * for sending frames.  Defering interrupts reduces system
794	 * load and potentially allows more concurrent work to be
795	 * done but if done to aggressively can cause senders to
796	 * backup.
797	 *
798	 * NB: use >= to deal with sc_txintrperiod changing
799	 *     dynamically through sysctl.
800	 */
801	if (flags & HAL_TXDESC_INTREQ) {
802		txq->axq_intrcnt = 0;
803	} else if (++txq->axq_intrcnt >= sc->sc_txintrperiod) {
804		flags |= HAL_TXDESC_INTREQ;
805		txq->axq_intrcnt = 0;
806	}
807
808	if (ath_tx_is_11n(sc)) {
809		rate[0] = rix;
810		try[0] = try0;
811	}
812
813	/*
814	 * Formulate first tx descriptor with tx controls.
815	 */
816	/* XXX check return value? */
817	/* XXX is this ok to call for 11n descriptors? */
818	/* XXX or should it go through the first, next, last 11n calls? */
819	ath_hal_setuptxdesc(ah, ds
820		, pktlen		/* packet length */
821		, hdrlen		/* header length */
822		, atype			/* Atheros packet type */
823		, ni->ni_txpower	/* txpower */
824		, txrate, try0		/* series 0 rate/tries */
825		, keyix			/* key cache index */
826		, sc->sc_txantenna	/* antenna mode */
827		, flags			/* flags */
828		, ctsrate		/* rts/cts rate */
829		, ctsduration		/* rts/cts duration */
830	);
831	bf->bf_txflags = flags;
832	/*
833	 * Setup the multi-rate retry state only when we're
834	 * going to use it.  This assumes ath_hal_setuptxdesc
835	 * initializes the descriptors (so we don't have to)
836	 * when the hardware supports multi-rate retry and
837	 * we don't use it.
838	 */
839        if (ismrr) {
840                if (ath_tx_is_11n(sc))
841                        ath_rate_getxtxrates(sc, an, rix, rate, try);
842                else
843                        ath_rate_setupxtxdesc(sc, an, ds, shortPreamble, rix);
844        }
845
846        if (ath_tx_is_11n(sc)) {
847                ath_buf_set_rate(sc, ni, bf, pktlen, flags, ctsrate, (atype == HAL_PKT_TYPE_PSPOLL), rate, try);
848        }
849
850	ath_tx_handoff(sc, txq, bf);
851	return 0;
852}
853
854static int
855ath_tx_raw_start(struct ath_softc *sc, struct ieee80211_node *ni,
856	struct ath_buf *bf, struct mbuf *m0,
857	const struct ieee80211_bpf_params *params)
858{
859	struct ifnet *ifp = sc->sc_ifp;
860	struct ieee80211com *ic = ifp->if_l2com;
861	struct ath_hal *ah = sc->sc_ah;
862	struct ieee80211vap *vap = ni->ni_vap;
863	int error, ismcast, ismrr;
864	int keyix, hdrlen, pktlen, try0, txantenna;
865	u_int8_t rix, cix, txrate, ctsrate, rate1, rate2, rate3;
866	struct ieee80211_frame *wh;
867	u_int flags, ctsduration;
868	HAL_PKT_TYPE atype;
869	const HAL_RATE_TABLE *rt;
870	struct ath_desc *ds;
871	u_int pri;
872	uint8_t try[4], rate[4];
873
874	bzero(try, sizeof(try));
875	bzero(rate, sizeof(rate));
876
877	wh = mtod(m0, struct ieee80211_frame *);
878	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
879	hdrlen = ieee80211_anyhdrsize(wh);
880	/*
881	 * Packet length must not include any
882	 * pad bytes; deduct them here.
883	 */
884	/* XXX honor IEEE80211_BPF_DATAPAD */
885	pktlen = m0->m_pkthdr.len - (hdrlen & 3) + IEEE80211_CRC_LEN;
886
887	/* Handle encryption twiddling if needed */
888	if (! ath_tx_tag_crypto(sc, ni, m0, params->ibp_flags & IEEE80211_BPF_CRYPTO, 0, &hdrlen, &pktlen, &keyix)) {
889		ath_freetx(m0);
890		return EIO;
891	}
892	/* packet header may have moved, reset our local pointer */
893	wh = mtod(m0, struct ieee80211_frame *);
894
895	error = ath_tx_dmasetup(sc, bf, m0);
896	if (error != 0)
897		return error;
898	m0 = bf->bf_m;				/* NB: may have changed */
899	wh = mtod(m0, struct ieee80211_frame *);
900	bf->bf_node = ni;			/* NB: held reference */
901
902	flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for crypto errs */
903	flags |= HAL_TXDESC_INTREQ;		/* force interrupt */
904	if (params->ibp_flags & IEEE80211_BPF_RTS)
905		flags |= HAL_TXDESC_RTSENA;
906	else if (params->ibp_flags & IEEE80211_BPF_CTS)
907		flags |= HAL_TXDESC_CTSENA;
908	/* XXX leave ismcast to injector? */
909	if ((params->ibp_flags & IEEE80211_BPF_NOACK) || ismcast)
910		flags |= HAL_TXDESC_NOACK;
911
912	rt = sc->sc_currates;
913	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
914	rix = ath_tx_findrix(sc, params->ibp_rate0);
915	txrate = rt->info[rix].rateCode;
916	if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
917		txrate |= rt->info[rix].shortPreamble;
918	sc->sc_txrix = rix;
919	try0 = params->ibp_try0;
920	ismrr = (params->ibp_try1 != 0);
921	txantenna = params->ibp_pri >> 2;
922	if (txantenna == 0)			/* XXX? */
923		txantenna = sc->sc_txantenna;
924
925	ctsduration = 0;
926	if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA)) {
927		cix = ath_tx_findrix(sc, params->ibp_ctsrate);
928		ctsrate = ath_tx_get_rtscts_rate(ah, rt, rix, cix, params->ibp_flags & IEEE80211_BPF_SHORTPRE);
929		/* The 11n chipsets do ctsduration calculations for you */
930		if (! ath_tx_is_11n(sc))
931			ctsduration = ath_tx_calc_ctsduration(ah, rix, cix,
932			    params->ibp_flags & IEEE80211_BPF_SHORTPRE, pktlen,
933			    rt, flags);
934		/*
935		 * Must disable multi-rate retry when using RTS/CTS.
936		 */
937		ismrr = 0;			/* XXX */
938	} else
939		ctsrate = 0;
940
941	pri = params->ibp_pri & 3;
942	/*
943	 * NB: we mark all packets as type PSPOLL so the h/w won't
944	 * set the sequence number, duration, etc.
945	 */
946	atype = HAL_PKT_TYPE_PSPOLL;
947
948	if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
949		ieee80211_dump_pkt(ic, mtod(m0, caddr_t), m0->m_len,
950		    sc->sc_hwmap[rix].ieeerate, -1);
951
952	if (ieee80211_radiotap_active_vap(vap)) {
953		u_int64_t tsf = ath_hal_gettsf64(ah);
954
955		sc->sc_tx_th.wt_tsf = htole64(tsf);
956		sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
957		if (wh->i_fc[1] & IEEE80211_FC1_WEP)
958			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
959		if (m0->m_flags & M_FRAG)
960			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
961		sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
962		sc->sc_tx_th.wt_txpower = ni->ni_txpower;
963		sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
964
965		ieee80211_radiotap_tx(vap, m0);
966	}
967
968	/*
969	 * Formulate first tx descriptor with tx controls.
970	 */
971	ds = bf->bf_desc;
972	/* XXX check return value? */
973	ath_hal_setuptxdesc(ah, ds
974		, pktlen		/* packet length */
975		, hdrlen		/* header length */
976		, atype			/* Atheros packet type */
977		, params->ibp_power	/* txpower */
978		, txrate, try0		/* series 0 rate/tries */
979		, keyix			/* key cache index */
980		, txantenna		/* antenna mode */
981		, flags			/* flags */
982		, ctsrate		/* rts/cts rate */
983		, ctsduration		/* rts/cts duration */
984	);
985	bf->bf_txflags = flags;
986
987	if (ath_tx_is_11n(sc)) {
988		rate[0] = ath_tx_findrix(sc, params->ibp_rate0);
989		try[0] = params->ibp_try0;
990
991		if (ismrr) {
992			/* Remember, rate[] is actually an array of rix's -adrian */
993			rate[0] = ath_tx_findrix(sc, params->ibp_rate0);
994			rate[1] = ath_tx_findrix(sc, params->ibp_rate1);
995			rate[2] = ath_tx_findrix(sc, params->ibp_rate2);
996			rate[3] = ath_tx_findrix(sc, params->ibp_rate3);
997
998			try[0] = params->ibp_try0;
999			try[1] = params->ibp_try1;
1000			try[2] = params->ibp_try2;
1001			try[3] = params->ibp_try3;
1002		}
1003	} else {
1004		if (ismrr) {
1005			rix = ath_tx_findrix(sc, params->ibp_rate1);
1006			rate1 = rt->info[rix].rateCode;
1007			if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
1008				rate1 |= rt->info[rix].shortPreamble;
1009			if (params->ibp_try2) {
1010				rix = ath_tx_findrix(sc, params->ibp_rate2);
1011				rate2 = rt->info[rix].rateCode;
1012				if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
1013					rate2 |= rt->info[rix].shortPreamble;
1014			} else
1015				rate2 = 0;
1016			if (params->ibp_try3) {
1017				rix = ath_tx_findrix(sc, params->ibp_rate3);
1018				rate3 = rt->info[rix].rateCode;
1019				if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
1020					rate3 |= rt->info[rix].shortPreamble;
1021			} else
1022				rate3 = 0;
1023			ath_hal_setupxtxdesc(ah, ds
1024				, rate1, params->ibp_try1	/* series 1 */
1025				, rate2, params->ibp_try2	/* series 2 */
1026				, rate3, params->ibp_try3	/* series 3 */
1027			);
1028		}
1029	}
1030
1031	if (ath_tx_is_11n(sc)) {
1032		/*
1033		 * notice that rix doesn't include any of the "magic" flags txrate
1034		 * does for communicating "other stuff" to the HAL.
1035		 */
1036		ath_buf_set_rate(sc, ni, bf, pktlen, flags, ctsrate, (atype == HAL_PKT_TYPE_PSPOLL), rate, try);
1037	}
1038
1039	/* NB: no buffered multicast in power save support */
1040	ath_tx_handoff(sc, sc->sc_ac2q[pri], bf);
1041	return 0;
1042}
1043
1044int
1045ath_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1046	const struct ieee80211_bpf_params *params)
1047{
1048	struct ieee80211com *ic = ni->ni_ic;
1049	struct ifnet *ifp = ic->ic_ifp;
1050	struct ath_softc *sc = ifp->if_softc;
1051	struct ath_buf *bf;
1052	int error;
1053
1054	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->sc_invalid) {
1055		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: discard frame, %s", __func__,
1056		    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ?
1057			"!running" : "invalid");
1058		m_freem(m);
1059		error = ENETDOWN;
1060		goto bad;
1061	}
1062	/*
1063	 * Grab a TX buffer and associated resources.
1064	 */
1065	bf = ath_getbuf(sc);
1066	if (bf == NULL) {
1067		sc->sc_stats.ast_tx_nobuf++;
1068		m_freem(m);
1069		error = ENOBUFS;
1070		goto bad;
1071	}
1072
1073	if (params == NULL) {
1074		/*
1075		 * Legacy path; interpret frame contents to decide
1076		 * precisely how to send the frame.
1077		 */
1078		if (ath_tx_start(sc, ni, bf, m)) {
1079			error = EIO;		/* XXX */
1080			goto bad2;
1081		}
1082	} else {
1083		/*
1084		 * Caller supplied explicit parameters to use in
1085		 * sending the frame.
1086		 */
1087		if (ath_tx_raw_start(sc, ni, bf, m, params)) {
1088			error = EIO;		/* XXX */
1089			goto bad2;
1090		}
1091	}
1092	sc->sc_wd_timer = 5;
1093	ifp->if_opackets++;
1094	sc->sc_stats.ast_tx_raw++;
1095
1096	return 0;
1097bad2:
1098	ATH_TXBUF_LOCK(sc);
1099	STAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
1100	ATH_TXBUF_UNLOCK(sc);
1101bad:
1102	ifp->if_oerrors++;
1103	sc->sc_stats.ast_tx_raw_fail++;
1104	ieee80211_free_node(ni);
1105	return error;
1106}
1107