xref: /freebsd/sys/dev/ath/if_ath.c (revision 77d5e06852be4c71423c6b627a7248f7e10b06e6)
15591b213SSam Leffler /*-
2515d617eSSam Leffler  * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
35591b213SSam Leffler  * All rights reserved.
45591b213SSam Leffler  *
55591b213SSam Leffler  * Redistribution and use in source and binary forms, with or without
65591b213SSam Leffler  * modification, are permitted provided that the following conditions
75591b213SSam Leffler  * are met:
85591b213SSam Leffler  * 1. Redistributions of source code must retain the above copyright
95591b213SSam Leffler  *    notice, this list of conditions and the following disclaimer,
105591b213SSam Leffler  *    without modification.
115591b213SSam Leffler  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
125591b213SSam Leffler  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
135591b213SSam Leffler  *    redistribution must be conditioned upon including a substantially
145591b213SSam Leffler  *    similar Disclaimer requirement for further binary redistribution.
155591b213SSam Leffler  *
165591b213SSam Leffler  * NO WARRANTY
175591b213SSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
185591b213SSam Leffler  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
195591b213SSam Leffler  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
205591b213SSam Leffler  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
215591b213SSam Leffler  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
225591b213SSam Leffler  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
235591b213SSam Leffler  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
245591b213SSam Leffler  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
255591b213SSam Leffler  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
265591b213SSam Leffler  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
275591b213SSam Leffler  * THE POSSIBILITY OF SUCH DAMAGES.
285591b213SSam Leffler  */
295591b213SSam Leffler 
305591b213SSam Leffler #include <sys/cdefs.h>
315591b213SSam Leffler __FBSDID("$FreeBSD$");
325591b213SSam Leffler 
335591b213SSam Leffler /*
345591b213SSam Leffler  * Driver for the Atheros Wireless LAN controller.
355f3721d5SSam Leffler  *
365f3721d5SSam Leffler  * This software is derived from work of Atsushi Onoe; his contribution
375f3721d5SSam Leffler  * is greatly appreciated.
385591b213SSam Leffler  */
395591b213SSam Leffler 
405591b213SSam Leffler #include "opt_inet.h"
41a585a9a1SSam Leffler #include "opt_ath.h"
425591b213SSam Leffler 
435591b213SSam Leffler #include <sys/param.h>
445591b213SSam Leffler #include <sys/systm.h>
455591b213SSam Leffler #include <sys/sysctl.h>
465591b213SSam Leffler #include <sys/mbuf.h>
475591b213SSam Leffler #include <sys/malloc.h>
485591b213SSam Leffler #include <sys/lock.h>
495591b213SSam Leffler #include <sys/mutex.h>
505591b213SSam Leffler #include <sys/kernel.h>
515591b213SSam Leffler #include <sys/socket.h>
525591b213SSam Leffler #include <sys/sockio.h>
535591b213SSam Leffler #include <sys/errno.h>
545591b213SSam Leffler #include <sys/callout.h>
555591b213SSam Leffler #include <sys/bus.h>
565591b213SSam Leffler #include <sys/endian.h>
570bbf5441SSam Leffler #include <sys/kthread.h>
580bbf5441SSam Leffler #include <sys/taskqueue.h>
595591b213SSam Leffler 
605591b213SSam Leffler #include <machine/bus.h>
615591b213SSam Leffler 
625591b213SSam Leffler #include <net/if.h>
635591b213SSam Leffler #include <net/if_dl.h>
645591b213SSam Leffler #include <net/if_media.h>
65fc74a9f9SBrooks Davis #include <net/if_types.h>
665591b213SSam Leffler #include <net/if_arp.h>
675591b213SSam Leffler #include <net/ethernet.h>
685591b213SSam Leffler #include <net/if_llc.h>
695591b213SSam Leffler 
705591b213SSam Leffler #include <net80211/ieee80211_var.h>
715591b213SSam Leffler 
725591b213SSam Leffler #include <net/bpf.h>
735591b213SSam Leffler 
745591b213SSam Leffler #ifdef INET
755591b213SSam Leffler #include <netinet/in.h>
765591b213SSam Leffler #include <netinet/if_ether.h>
775591b213SSam Leffler #endif
785591b213SSam Leffler 
795591b213SSam Leffler #include <dev/ath/if_athvar.h>
805591b213SSam Leffler #include <contrib/dev/ath/ah_desc.h>
81c42a7b7eSSam Leffler #include <contrib/dev/ath/ah_devid.h>		/* XXX for softled */
825591b213SSam Leffler 
8386e07743SSam Leffler #ifdef ATH_TX99_DIAG
8486e07743SSam Leffler #include <dev/ath/ath_tx99/ath_tx99.h>
8586e07743SSam Leffler #endif
8686e07743SSam Leffler 
87e8fd88a3SSam Leffler /* unaligned little endian access */
885591b213SSam Leffler #define LE_READ_2(p)							\
895591b213SSam Leffler 	((u_int16_t)							\
905591b213SSam Leffler 	 ((((u_int8_t *)(p))[0]      ) | (((u_int8_t *)(p))[1] <<  8)))
915591b213SSam Leffler #define LE_READ_4(p)							\
925591b213SSam Leffler 	((u_int32_t)							\
935591b213SSam Leffler 	 ((((u_int8_t *)(p))[0]      ) | (((u_int8_t *)(p))[1] <<  8) |	\
945591b213SSam Leffler 	  (((u_int8_t *)(p))[2] << 16) | (((u_int8_t *)(p))[3] << 24)))
955591b213SSam Leffler 
963e50ec2cSSam Leffler enum {
973e50ec2cSSam Leffler 	ATH_LED_TX,
983e50ec2cSSam Leffler 	ATH_LED_RX,
993e50ec2cSSam Leffler 	ATH_LED_POLL,
1003e50ec2cSSam Leffler };
1013e50ec2cSSam Leffler 
1025591b213SSam Leffler static void	ath_init(void *);
103c42a7b7eSSam Leffler static void	ath_stop_locked(struct ifnet *);
1045591b213SSam Leffler static void	ath_stop(struct ifnet *);
1055591b213SSam Leffler static void	ath_start(struct ifnet *);
106c42a7b7eSSam Leffler static int	ath_reset(struct ifnet *);
1075591b213SSam Leffler static int	ath_media_change(struct ifnet *);
1085591b213SSam Leffler static void	ath_watchdog(struct ifnet *);
1095591b213SSam Leffler static int	ath_ioctl(struct ifnet *, u_long, caddr_t);
1105591b213SSam Leffler static void	ath_fatal_proc(void *, int);
1115591b213SSam Leffler static void	ath_rxorn_proc(void *, int);
1125591b213SSam Leffler static void	ath_bmiss_proc(void *, int);
113c42a7b7eSSam Leffler static int	ath_key_alloc(struct ieee80211com *,
114c1225b52SSam Leffler 			const struct ieee80211_key *,
115c1225b52SSam Leffler 			ieee80211_keyix *, ieee80211_keyix *);
116c42a7b7eSSam Leffler static int	ath_key_delete(struct ieee80211com *,
117c42a7b7eSSam Leffler 			const struct ieee80211_key *);
118c42a7b7eSSam Leffler static int	ath_key_set(struct ieee80211com *, const struct ieee80211_key *,
119c42a7b7eSSam Leffler 			const u_int8_t mac[IEEE80211_ADDR_LEN]);
120c42a7b7eSSam Leffler static void	ath_key_update_begin(struct ieee80211com *);
121c42a7b7eSSam Leffler static void	ath_key_update_end(struct ieee80211com *);
1225591b213SSam Leffler static void	ath_mode_init(struct ath_softc *);
123c42a7b7eSSam Leffler static void	ath_setslottime(struct ath_softc *);
124c42a7b7eSSam Leffler static void	ath_updateslot(struct ifnet *);
12580d2765fSSam Leffler static int	ath_beaconq_setup(struct ath_hal *);
1265591b213SSam Leffler static int	ath_beacon_alloc(struct ath_softc *, struct ieee80211_node *);
127c42a7b7eSSam Leffler static void	ath_beacon_setup(struct ath_softc *, struct ath_buf *);
1285591b213SSam Leffler static void	ath_beacon_proc(void *, int);
129c42a7b7eSSam Leffler static void	ath_bstuck_proc(void *, int);
1305591b213SSam Leffler static void	ath_beacon_free(struct ath_softc *);
1315591b213SSam Leffler static void	ath_beacon_config(struct ath_softc *);
132c42a7b7eSSam Leffler static void	ath_descdma_cleanup(struct ath_softc *sc,
133c42a7b7eSSam Leffler 			struct ath_descdma *, ath_bufhead *);
1345591b213SSam Leffler static int	ath_desc_alloc(struct ath_softc *);
1355591b213SSam Leffler static void	ath_desc_free(struct ath_softc *);
136c42a7b7eSSam Leffler static struct ieee80211_node *ath_node_alloc(struct ieee80211_node_table *);
137c42a7b7eSSam Leffler static void	ath_node_free(struct ieee80211_node *);
13868e8e04eSSam Leffler static int8_t	ath_node_getrssi(const struct ieee80211_node *);
13968e8e04eSSam Leffler static void	ath_node_getsignal(const struct ieee80211_node *,
14068e8e04eSSam Leffler 			int8_t *, int8_t *);
1415591b213SSam Leffler static int	ath_rxbuf_init(struct ath_softc *, struct ath_buf *);
142c42a7b7eSSam Leffler static void	ath_recv_mgmt(struct ieee80211com *ic, struct mbuf *m,
143c42a7b7eSSam Leffler 			struct ieee80211_node *ni,
14468e8e04eSSam Leffler 			int subtype, int rssi, int noise, u_int32_t rstamp);
145c42a7b7eSSam Leffler static void	ath_setdefantenna(struct ath_softc *, u_int);
1465591b213SSam Leffler static void	ath_rx_proc(void *, int);
147622b3fd2SSam Leffler static void	ath_txq_init(struct ath_softc *sc, struct ath_txq *, int);
148c42a7b7eSSam Leffler static struct ath_txq *ath_txq_setup(struct ath_softc*, int qtype, int subtype);
149c42a7b7eSSam Leffler static int	ath_tx_setup(struct ath_softc *, int, int);
150c42a7b7eSSam Leffler static int	ath_wme_update(struct ieee80211com *);
151c42a7b7eSSam Leffler static void	ath_tx_cleanupq(struct ath_softc *, struct ath_txq *);
152c42a7b7eSSam Leffler static void	ath_tx_cleanup(struct ath_softc *);
15368e8e04eSSam Leffler static void	ath_freetx(struct mbuf *);
1545591b213SSam Leffler static int	ath_tx_start(struct ath_softc *, struct ieee80211_node *,
1555591b213SSam Leffler 			     struct ath_buf *, struct mbuf *);
156c42a7b7eSSam Leffler static void	ath_tx_proc_q0(void *, int);
157c42a7b7eSSam Leffler static void	ath_tx_proc_q0123(void *, int);
1585591b213SSam Leffler static void	ath_tx_proc(void *, int);
1595591b213SSam Leffler static int	ath_chan_set(struct ath_softc *, struct ieee80211_channel *);
1605591b213SSam Leffler static void	ath_draintxq(struct ath_softc *);
1615591b213SSam Leffler static void	ath_stoprecv(struct ath_softc *);
1625591b213SSam Leffler static int	ath_startrecv(struct ath_softc *);
163c42a7b7eSSam Leffler static void	ath_chan_change(struct ath_softc *, struct ieee80211_channel *);
16468e8e04eSSam Leffler static void	ath_scan_start(struct ieee80211com *);
16568e8e04eSSam Leffler static void	ath_scan_end(struct ieee80211com *);
16668e8e04eSSam Leffler static void	ath_set_channel(struct ieee80211com *);
1675591b213SSam Leffler static void	ath_calibrate(void *);
16845bbf62fSSam Leffler static int	ath_newstate(struct ieee80211com *, enum ieee80211_state, int);
169e8fd88a3SSam Leffler static void	ath_setup_stationkey(struct ieee80211_node *);
170e9962332SSam Leffler static void	ath_newassoc(struct ieee80211_node *, int);
171aaa70f2fSSam Leffler static int	ath_getchannels(struct ath_softc *,
172aaa70f2fSSam Leffler 			HAL_REG_DOMAIN, HAL_CTRY_CODE, HAL_BOOL, HAL_BOOL);
1733e50ec2cSSam Leffler static void	ath_led_event(struct ath_softc *, int);
174c42a7b7eSSam Leffler static void	ath_update_txpow(struct ath_softc *);
1755591b213SSam Leffler 
176c42a7b7eSSam Leffler static int	ath_rate_setup(struct ath_softc *, u_int mode);
1775591b213SSam Leffler static void	ath_setcurmode(struct ath_softc *, enum ieee80211_phymode);
178c42a7b7eSSam Leffler 
179c42a7b7eSSam Leffler static void	ath_sysctlattach(struct ath_softc *);
180664443d0SSam Leffler static int	ath_raw_xmit(struct ieee80211_node *,
181664443d0SSam Leffler 			struct mbuf *, const struct ieee80211_bpf_params *);
182c42a7b7eSSam Leffler static void	ath_bpfattach(struct ath_softc *);
183c42a7b7eSSam Leffler static void	ath_announce(struct ath_softc *);
1845591b213SSam Leffler 
1855591b213SSam Leffler SYSCTL_DECL(_hw_ath);
1865591b213SSam Leffler 
1875591b213SSam Leffler /* XXX validate sysctl values */
1885591b213SSam Leffler static	int ath_calinterval = 30;		/* calibrate every 30 secs */
1895591b213SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, calibrate, CTLFLAG_RW, &ath_calinterval,
1905591b213SSam Leffler 	    0, "chip calibration interval (secs)");
19145cabbdcSSam Leffler static	int ath_outdoor = AH_TRUE;		/* outdoor operation */
192aaa70f2fSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, outdoor, CTLFLAG_RW, &ath_outdoor,
193c42a7b7eSSam Leffler 	    0, "outdoor operation");
1948c0370b7SSam Leffler TUNABLE_INT("hw.ath.outdoor", &ath_outdoor);
195c42a7b7eSSam Leffler static	int ath_xchanmode = AH_TRUE;		/* extended channel use */
196aaa70f2fSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, xchanmode, CTLFLAG_RW, &ath_xchanmode,
197c42a7b7eSSam Leffler 	    0, "extended channel mode");
198c42a7b7eSSam Leffler TUNABLE_INT("hw.ath.xchanmode", &ath_xchanmode);
19945cabbdcSSam Leffler static	int ath_countrycode = CTRY_DEFAULT;	/* country code */
200aaa70f2fSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, countrycode, CTLFLAG_RW, &ath_countrycode,
20145cabbdcSSam Leffler 	    0, "country code");
2028c0370b7SSam Leffler TUNABLE_INT("hw.ath.countrycode", &ath_countrycode);
20345cabbdcSSam Leffler static	int ath_regdomain = 0;			/* regulatory domain */
20445cabbdcSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, regdomain, CTLFLAG_RD, &ath_regdomain,
20545cabbdcSSam Leffler 	    0, "regulatory domain");
2065591b213SSam Leffler 
207e2d787faSSam Leffler static	int ath_rxbuf = ATH_RXBUF;		/* # rx buffers to allocate */
208aaa70f2fSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, rxbuf, CTLFLAG_RW, &ath_rxbuf,
209e2d787faSSam Leffler 	    0, "rx buffers allocated");
210e2d787faSSam Leffler TUNABLE_INT("hw.ath.rxbuf", &ath_rxbuf);
211e2d787faSSam Leffler static	int ath_txbuf = ATH_TXBUF;		/* # tx buffers to allocate */
212aaa70f2fSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, txbuf, CTLFLAG_RW, &ath_txbuf,
213e2d787faSSam Leffler 	    0, "tx buffers allocated");
214e2d787faSSam Leffler TUNABLE_INT("hw.ath.txbuf", &ath_txbuf);
215e2d787faSSam Leffler 
216a585a9a1SSam Leffler #ifdef ATH_DEBUG
217c42a7b7eSSam Leffler static	int ath_debug = 0;
2185591b213SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, debug, CTLFLAG_RW, &ath_debug,
2195591b213SSam Leffler 	    0, "control debugging printfs");
220f3be7956SSam Leffler TUNABLE_INT("hw.ath.debug", &ath_debug);
221e325e530SSam Leffler enum {
222e325e530SSam Leffler 	ATH_DEBUG_XMIT		= 0x00000001,	/* basic xmit operation */
223e325e530SSam Leffler 	ATH_DEBUG_XMIT_DESC	= 0x00000002,	/* xmit descriptors */
224e325e530SSam Leffler 	ATH_DEBUG_RECV		= 0x00000004,	/* basic recv operation */
225e325e530SSam Leffler 	ATH_DEBUG_RECV_DESC	= 0x00000008,	/* recv descriptors */
226e325e530SSam Leffler 	ATH_DEBUG_RATE		= 0x00000010,	/* rate control */
227e325e530SSam Leffler 	ATH_DEBUG_RESET		= 0x00000020,	/* reset processing */
228e325e530SSam Leffler 	ATH_DEBUG_MODE		= 0x00000040,	/* mode init/setup */
229e325e530SSam Leffler 	ATH_DEBUG_BEACON 	= 0x00000080,	/* beacon handling */
230e325e530SSam Leffler 	ATH_DEBUG_WATCHDOG 	= 0x00000100,	/* watchdog timeout */
231e325e530SSam Leffler 	ATH_DEBUG_INTR		= 0x00001000,	/* ISR */
232e325e530SSam Leffler 	ATH_DEBUG_TX_PROC	= 0x00002000,	/* tx ISR proc */
233e325e530SSam Leffler 	ATH_DEBUG_RX_PROC	= 0x00004000,	/* rx ISR proc */
234e325e530SSam Leffler 	ATH_DEBUG_BEACON_PROC	= 0x00008000,	/* beacon ISR proc */
235e325e530SSam Leffler 	ATH_DEBUG_CALIBRATE	= 0x00010000,	/* periodic calibration */
236c42a7b7eSSam Leffler 	ATH_DEBUG_KEYCACHE	= 0x00020000,	/* key cache management */
237c42a7b7eSSam Leffler 	ATH_DEBUG_STATE		= 0x00040000,	/* 802.11 state transitions */
238c42a7b7eSSam Leffler 	ATH_DEBUG_NODE		= 0x00080000,	/* node management */
2393e50ec2cSSam Leffler 	ATH_DEBUG_LED		= 0x00100000,	/* led management */
240bd5a9920SSam Leffler 	ATH_DEBUG_FF		= 0x00200000,	/* fast frames */
241bd5a9920SSam Leffler 	ATH_DEBUG_DFS		= 0x00400000,	/* DFS processing */
242c42a7b7eSSam Leffler 	ATH_DEBUG_FATAL		= 0x80000000,	/* fatal errors */
243e325e530SSam Leffler 	ATH_DEBUG_ANY		= 0xffffffff
244e325e530SSam Leffler };
245c42a7b7eSSam Leffler #define	IFF_DUMPPKTS(sc, m) \
2460a1b94c4SSam Leffler 	((sc->sc_debug & (m)) || \
247fc74a9f9SBrooks Davis 	    (sc->sc_ifp->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
248c42a7b7eSSam Leffler #define	DPRINTF(sc, m, fmt, ...) do {				\
2490a1b94c4SSam Leffler 	if (sc->sc_debug & (m))					\
250c42a7b7eSSam Leffler 		printf(fmt, __VA_ARGS__);			\
251c42a7b7eSSam Leffler } while (0)
252c42a7b7eSSam Leffler #define	KEYPRINTF(sc, ix, hk, mac) do {				\
253c42a7b7eSSam Leffler 	if (sc->sc_debug & ATH_DEBUG_KEYCACHE)			\
2545901d2d3SSam Leffler 		ath_keyprint(sc, __func__, ix, hk, mac);	\
255c42a7b7eSSam Leffler } while (0)
25665f9edeeSSam Leffler static	void ath_printrxbuf(const struct ath_buf *bf, u_int ix, int);
25765f9edeeSSam Leffler static	void ath_printtxbuf(const struct ath_buf *bf, u_int qnum, u_int ix, int done);
2585591b213SSam Leffler #else
259c42a7b7eSSam Leffler #define	IFF_DUMPPKTS(sc, m) \
260fc74a9f9SBrooks Davis 	((sc->sc_ifp->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
261d2f6ed15SSam Leffler #define	DPRINTF(sc, m, fmt, ...) do {				\
262d2f6ed15SSam Leffler 	(void) sc;						\
263d2f6ed15SSam Leffler } while (0)
264d2f6ed15SSam Leffler #define	KEYPRINTF(sc, k, ix, mac) do {				\
265d2f6ed15SSam Leffler 	(void) sc;						\
266d2f6ed15SSam Leffler } while (0)
2675591b213SSam Leffler #endif
2685591b213SSam Leffler 
269c42a7b7eSSam Leffler MALLOC_DEFINE(M_ATHDEV, "athdev", "ath driver dma buffers");
270c42a7b7eSSam Leffler 
2715591b213SSam Leffler int
2725591b213SSam Leffler ath_attach(u_int16_t devid, struct ath_softc *sc)
2735591b213SSam Leffler {
274fc74a9f9SBrooks Davis 	struct ifnet *ifp;
2755591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
276fc74a9f9SBrooks Davis 	struct ath_hal *ah = NULL;
2775591b213SSam Leffler 	HAL_STATUS status;
278c42a7b7eSSam Leffler 	int error = 0, i;
2795591b213SSam Leffler 
280c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: devid 0x%x\n", __func__, devid);
2815591b213SSam Leffler 
282fc74a9f9SBrooks Davis 	ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
283fc74a9f9SBrooks Davis 	if (ifp == NULL) {
284fc74a9f9SBrooks Davis 		device_printf(sc->sc_dev, "can not if_alloc()\n");
285fc74a9f9SBrooks Davis 		error = ENOSPC;
286fc74a9f9SBrooks Davis 		goto bad;
287fc74a9f9SBrooks Davis 	}
288fc74a9f9SBrooks Davis 
2895591b213SSam Leffler 	/* set these up early for if_printf use */
2909bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(sc->sc_dev),
2919bf40edeSBrooks Davis 		device_get_unit(sc->sc_dev));
2925591b213SSam Leffler 
293f9fc583fSSam Leffler 	ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh, &status);
2945591b213SSam Leffler 	if (ah == NULL) {
2955591b213SSam Leffler 		if_printf(ifp, "unable to attach hardware; HAL status %u\n",
2965591b213SSam Leffler 			status);
2975591b213SSam Leffler 		error = ENXIO;
2985591b213SSam Leffler 		goto bad;
2995591b213SSam Leffler 	}
30085bdc65aSSam Leffler 	if (ah->ah_abi != HAL_ABI_VERSION) {
301c42a7b7eSSam Leffler 		if_printf(ifp, "HAL ABI mismatch detected "
302c42a7b7eSSam Leffler 			"(HAL:0x%x != driver:0x%x)\n",
30385bdc65aSSam Leffler 			ah->ah_abi, HAL_ABI_VERSION);
30485bdc65aSSam Leffler 		error = ENXIO;
30585bdc65aSSam Leffler 		goto bad;
30685bdc65aSSam Leffler 	}
3075591b213SSam Leffler 	sc->sc_ah = ah;
308b58b3803SSam Leffler 	sc->sc_invalid = 0;	/* ready to go, enable interrupt handling */
3095591b213SSam Leffler 
3105591b213SSam Leffler 	/*
311c42a7b7eSSam Leffler 	 * Check if the MAC has multi-rate retry support.
312c42a7b7eSSam Leffler 	 * We do this by trying to setup a fake extended
313c42a7b7eSSam Leffler 	 * descriptor.  MAC's that don't have support will
314c42a7b7eSSam Leffler 	 * return false w/o doing anything.  MAC's that do
315c42a7b7eSSam Leffler 	 * support it will return true w/o doing anything.
316c42a7b7eSSam Leffler 	 */
317c42a7b7eSSam Leffler 	sc->sc_mrretry = ath_hal_setupxtxdesc(ah, NULL, 0,0, 0,0, 0,0);
318c42a7b7eSSam Leffler 
319c42a7b7eSSam Leffler 	/*
320c42a7b7eSSam Leffler 	 * Check if the device has hardware counters for PHY
321c42a7b7eSSam Leffler 	 * errors.  If so we need to enable the MIB interrupt
322c42a7b7eSSam Leffler 	 * so we can act on stat triggers.
323c42a7b7eSSam Leffler 	 */
324c42a7b7eSSam Leffler 	if (ath_hal_hwphycounters(ah))
325c42a7b7eSSam Leffler 		sc->sc_needmib = 1;
326c42a7b7eSSam Leffler 
327c42a7b7eSSam Leffler 	/*
328c42a7b7eSSam Leffler 	 * Get the hardware key cache size.
329c42a7b7eSSam Leffler 	 */
330c42a7b7eSSam Leffler 	sc->sc_keymax = ath_hal_keycachesize(ah);
331e8fd88a3SSam Leffler 	if (sc->sc_keymax > ATH_KEYMAX) {
332e8fd88a3SSam Leffler 		if_printf(ifp, "Warning, using only %u of %u key cache slots\n",
333e8fd88a3SSam Leffler 			ATH_KEYMAX, sc->sc_keymax);
334e8fd88a3SSam Leffler 		sc->sc_keymax = ATH_KEYMAX;
335c42a7b7eSSam Leffler 	}
336c42a7b7eSSam Leffler 	/*
337c42a7b7eSSam Leffler 	 * Reset the key cache since some parts do not
338c42a7b7eSSam Leffler 	 * reset the contents on initial power up.
339c42a7b7eSSam Leffler 	 */
340c42a7b7eSSam Leffler 	for (i = 0; i < sc->sc_keymax; i++)
341c42a7b7eSSam Leffler 		ath_hal_keyreset(ah, i);
342c42a7b7eSSam Leffler 
343c42a7b7eSSam Leffler 	/*
3445591b213SSam Leffler 	 * Collect the channel list using the default country
3455591b213SSam Leffler 	 * code and including outdoor channels.  The 802.11 layer
34645cabbdcSSam Leffler 	 * is resposible for filtering this list based on settings
34745cabbdcSSam Leffler 	 * like the phy mode.
3485591b213SSam Leffler 	 */
349aaa70f2fSSam Leffler 	error = ath_getchannels(sc, ath_regdomain, ath_countrycode,
35068e8e04eSSam Leffler 			ath_outdoor != 0, ath_xchanmode != 0);
3515591b213SSam Leffler 	if (error != 0)
3525591b213SSam Leffler 		goto bad;
3535591b213SSam Leffler 
3545591b213SSam Leffler 	/*
3555591b213SSam Leffler 	 * Setup rate tables for all potential media types.
3565591b213SSam Leffler 	 */
3575591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11A);
3585591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11B);
3595591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11G);
360c42a7b7eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_A);
361c42a7b7eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_G);
36268e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_STURBO_A);
36368e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11NA);
36468e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11NG);
365724c193aSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_HALF);
366724c193aSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_QUARTER);
367aaa70f2fSSam Leffler 
368c42a7b7eSSam Leffler 	/* NB: setup here so ath_rate_update is happy */
369c42a7b7eSSam Leffler 	ath_setcurmode(sc, IEEE80211_MODE_11A);
3705591b213SSam Leffler 
371c42a7b7eSSam Leffler 	/*
372c42a7b7eSSam Leffler 	 * Allocate tx+rx descriptors and populate the lists.
373c42a7b7eSSam Leffler 	 */
3745591b213SSam Leffler 	error = ath_desc_alloc(sc);
3755591b213SSam Leffler 	if (error != 0) {
3765591b213SSam Leffler 		if_printf(ifp, "failed to allocate descriptors: %d\n", error);
3775591b213SSam Leffler 		goto bad;
3785591b213SSam Leffler 	}
3792274d8c8SSam Leffler 	callout_init(&sc->sc_cal_ch, CALLOUT_MPSAFE);
380bd5a9920SSam Leffler 	callout_init(&sc->sc_dfs_ch, CALLOUT_MPSAFE);
3815591b213SSam Leffler 
382f0b2a0beSSam Leffler 	ATH_TXBUF_LOCK_INIT(sc);
3835591b213SSam Leffler 
3840bbf5441SSam Leffler 	sc->sc_tq = taskqueue_create("ath_taskq", M_NOWAIT,
3850bbf5441SSam Leffler 		taskqueue_thread_enqueue, &sc->sc_tq);
3860bbf5441SSam Leffler 	taskqueue_start_threads(&sc->sc_tq, 1, PI_NET,
3870bbf5441SSam Leffler 		"%s taskq", ifp->if_xname);
3880bbf5441SSam Leffler 
3895591b213SSam Leffler 	TASK_INIT(&sc->sc_rxtask, 0, ath_rx_proc, sc);
3905591b213SSam Leffler 	TASK_INIT(&sc->sc_rxorntask, 0, ath_rxorn_proc, sc);
3915591b213SSam Leffler 	TASK_INIT(&sc->sc_bmisstask, 0, ath_bmiss_proc, sc);
392c42a7b7eSSam Leffler 	TASK_INIT(&sc->sc_bstucktask,0, ath_bstuck_proc, sc);
3935591b213SSam Leffler 
3945591b213SSam Leffler 	/*
395c42a7b7eSSam Leffler 	 * Allocate hardware transmit queues: one queue for
396c42a7b7eSSam Leffler 	 * beacon frames and one data queue for each QoS
397c42a7b7eSSam Leffler 	 * priority.  Note that the hal handles reseting
398c42a7b7eSSam Leffler 	 * these queues at the needed time.
399c42a7b7eSSam Leffler 	 *
400c42a7b7eSSam Leffler 	 * XXX PS-Poll
4015591b213SSam Leffler 	 */
40280d2765fSSam Leffler 	sc->sc_bhalq = ath_beaconq_setup(ah);
4035591b213SSam Leffler 	if (sc->sc_bhalq == (u_int) -1) {
4045591b213SSam Leffler 		if_printf(ifp, "unable to setup a beacon xmit queue!\n");
405c42a7b7eSSam Leffler 		error = EIO;
406b28b4653SSam Leffler 		goto bad2;
4075591b213SSam Leffler 	}
408c42a7b7eSSam Leffler 	sc->sc_cabq = ath_txq_setup(sc, HAL_TX_QUEUE_CAB, 0);
409c42a7b7eSSam Leffler 	if (sc->sc_cabq == NULL) {
410c42a7b7eSSam Leffler 		if_printf(ifp, "unable to setup CAB xmit queue!\n");
411c42a7b7eSSam Leffler 		error = EIO;
412c42a7b7eSSam Leffler 		goto bad2;
413c42a7b7eSSam Leffler 	}
4141fba0fdcSSam Leffler 	/* NB: s/w q, qnum used only by WITNESS */
4151fba0fdcSSam Leffler 	ath_txq_init(sc, &sc->sc_mcastq, HAL_NUM_TX_QUEUES+1);
416c42a7b7eSSam Leffler 	/* NB: insure BK queue is the lowest priority h/w queue */
417c42a7b7eSSam Leffler 	if (!ath_tx_setup(sc, WME_AC_BK, HAL_WME_AC_BK)) {
418c42a7b7eSSam Leffler 		if_printf(ifp, "unable to setup xmit queue for %s traffic!\n",
419c42a7b7eSSam Leffler 			ieee80211_wme_acnames[WME_AC_BK]);
420c42a7b7eSSam Leffler 		error = EIO;
421c42a7b7eSSam Leffler 		goto bad2;
422c42a7b7eSSam Leffler 	}
423c42a7b7eSSam Leffler 	if (!ath_tx_setup(sc, WME_AC_BE, HAL_WME_AC_BE) ||
424c42a7b7eSSam Leffler 	    !ath_tx_setup(sc, WME_AC_VI, HAL_WME_AC_VI) ||
425c42a7b7eSSam Leffler 	    !ath_tx_setup(sc, WME_AC_VO, HAL_WME_AC_VO)) {
426c42a7b7eSSam Leffler 		/*
427c42a7b7eSSam Leffler 		 * Not enough hardware tx queues to properly do WME;
428c42a7b7eSSam Leffler 		 * just punt and assign them all to the same h/w queue.
429c42a7b7eSSam Leffler 		 * We could do a better job of this if, for example,
430c42a7b7eSSam Leffler 		 * we allocate queues when we switch from station to
431c42a7b7eSSam Leffler 		 * AP mode.
432c42a7b7eSSam Leffler 		 */
433c42a7b7eSSam Leffler 		if (sc->sc_ac2q[WME_AC_VI] != NULL)
434c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_VI]);
435c42a7b7eSSam Leffler 		if (sc->sc_ac2q[WME_AC_BE] != NULL)
436c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_BE]);
437c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_BE] = sc->sc_ac2q[WME_AC_BK];
438c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_VI] = sc->sc_ac2q[WME_AC_BK];
439c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_VO] = sc->sc_ac2q[WME_AC_BK];
440c42a7b7eSSam Leffler 	}
441c42a7b7eSSam Leffler 
442c42a7b7eSSam Leffler 	/*
443c42a7b7eSSam Leffler 	 * Special case certain configurations.  Note the
444c42a7b7eSSam Leffler 	 * CAB queue is handled by these specially so don't
445c42a7b7eSSam Leffler 	 * include them when checking the txq setup mask.
446c42a7b7eSSam Leffler 	 */
447c42a7b7eSSam Leffler 	switch (sc->sc_txqsetup &~ (1<<sc->sc_cabq->axq_qnum)) {
448c42a7b7eSSam Leffler 	case 0x01:
449c42a7b7eSSam Leffler 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0, sc);
450c42a7b7eSSam Leffler 		break;
451c42a7b7eSSam Leffler 	case 0x0f:
452c42a7b7eSSam Leffler 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0123, sc);
453c42a7b7eSSam Leffler 		break;
454c42a7b7eSSam Leffler 	default:
455c42a7b7eSSam Leffler 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc, sc);
456c42a7b7eSSam Leffler 		break;
457c42a7b7eSSam Leffler 	}
458c42a7b7eSSam Leffler 
459c42a7b7eSSam Leffler 	/*
460c42a7b7eSSam Leffler 	 * Setup rate control.  Some rate control modules
461c42a7b7eSSam Leffler 	 * call back to change the anntena state so expose
462c42a7b7eSSam Leffler 	 * the necessary entry points.
463c42a7b7eSSam Leffler 	 * XXX maybe belongs in struct ath_ratectrl?
464c42a7b7eSSam Leffler 	 */
465c42a7b7eSSam Leffler 	sc->sc_setdefantenna = ath_setdefantenna;
466c42a7b7eSSam Leffler 	sc->sc_rc = ath_rate_attach(sc);
467c42a7b7eSSam Leffler 	if (sc->sc_rc == NULL) {
468c42a7b7eSSam Leffler 		error = EIO;
469c42a7b7eSSam Leffler 		goto bad2;
470c42a7b7eSSam Leffler 	}
471c42a7b7eSSam Leffler 
4723e50ec2cSSam Leffler 	sc->sc_blinking = 0;
473c42a7b7eSSam Leffler 	sc->sc_ledstate = 1;
4743e50ec2cSSam Leffler 	sc->sc_ledon = 0;			/* low true */
4753e50ec2cSSam Leffler 	sc->sc_ledidle = (2700*hz)/1000;	/* 2.7sec */
4763e50ec2cSSam Leffler 	callout_init(&sc->sc_ledtimer, CALLOUT_MPSAFE);
477c42a7b7eSSam Leffler 	/*
478c42a7b7eSSam Leffler 	 * Auto-enable soft led processing for IBM cards and for
479c42a7b7eSSam Leffler 	 * 5211 minipci cards.  Users can also manually enable/disable
480c42a7b7eSSam Leffler 	 * support with a sysctl.
481c42a7b7eSSam Leffler 	 */
482c42a7b7eSSam Leffler 	sc->sc_softled = (devid == AR5212_DEVID_IBM || devid == AR5211_DEVID);
483c42a7b7eSSam Leffler 	if (sc->sc_softled) {
484c42a7b7eSSam Leffler 		ath_hal_gpioCfgOutput(ah, sc->sc_ledpin);
4853e50ec2cSSam Leffler 		ath_hal_gpioset(ah, sc->sc_ledpin, !sc->sc_ledon);
486c42a7b7eSSam Leffler 	}
4875591b213SSam Leffler 
4885591b213SSam Leffler 	ifp->if_softc = sc;
4895591b213SSam Leffler 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
4905591b213SSam Leffler 	ifp->if_start = ath_start;
4915591b213SSam Leffler 	ifp->if_watchdog = ath_watchdog;
4925591b213SSam Leffler 	ifp->if_ioctl = ath_ioctl;
4935591b213SSam Leffler 	ifp->if_init = ath_init;
494154b8df2SMax Laier 	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
495154b8df2SMax Laier 	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
496154b8df2SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
4975591b213SSam Leffler 
498c42a7b7eSSam Leffler 	ic->ic_ifp = ifp;
499c42a7b7eSSam Leffler 	ic->ic_reset = ath_reset;
5005591b213SSam Leffler 	ic->ic_newassoc = ath_newassoc;
501c42a7b7eSSam Leffler 	ic->ic_updateslot = ath_updateslot;
502c42a7b7eSSam Leffler 	ic->ic_wme.wme_update = ath_wme_update;
5035591b213SSam Leffler 	/* XXX not right but it's not used anywhere important */
5045591b213SSam Leffler 	ic->ic_phytype = IEEE80211_T_OFDM;
5055591b213SSam Leffler 	ic->ic_opmode = IEEE80211_M_STA;
506c42a7b7eSSam Leffler 	ic->ic_caps =
507c42a7b7eSSam Leffler 		  IEEE80211_C_IBSS		/* ibss, nee adhoc, mode */
508fe32c3efSSam Leffler 		| IEEE80211_C_HOSTAP		/* hostap mode */
509fe32c3efSSam Leffler 		| IEEE80211_C_MONITOR		/* monitor mode */
5107a04dc27SSam Leffler 		| IEEE80211_C_AHDEMO		/* adhoc demo mode */
511fe32c3efSSam Leffler 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
512c42a7b7eSSam Leffler 		| IEEE80211_C_SHSLOT		/* short slot time supported */
513c42a7b7eSSam Leffler 		| IEEE80211_C_WPA		/* capable of WPA1+WPA2 */
51468e8e04eSSam Leffler 		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
51568e8e04eSSam Leffler 		| IEEE80211_C_TXFRAG		/* handle tx frags */
51601e7e035SSam Leffler 		;
517c42a7b7eSSam Leffler 	/*
518c42a7b7eSSam Leffler 	 * Query the hal to figure out h/w crypto support.
519c42a7b7eSSam Leffler 	 */
520c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_WEP))
521c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_WEP;
522c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_OCB))
523c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_AES;
524c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_CCM))
525c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_AES_CCM;
526c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_CKIP))
527c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_CKIP;
528c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_TKIP)) {
529c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TKIP;
530c42a7b7eSSam Leffler 		/*
531c42a7b7eSSam Leffler 		 * Check if h/w does the MIC and/or whether the
532c42a7b7eSSam Leffler 		 * separate key cache entries are required to
533c42a7b7eSSam Leffler 		 * handle both tx+rx MIC keys.
534c42a7b7eSSam Leffler 		 */
535c42a7b7eSSam Leffler 		if (ath_hal_ciphersupported(ah, HAL_CIPHER_MIC))
536c42a7b7eSSam Leffler 			ic->ic_caps |= IEEE80211_C_TKIPMIC;
5375901d2d3SSam Leffler 		/*
5385901d2d3SSam Leffler 		 * If the h/w supports storing tx+rx MIC keys
5395901d2d3SSam Leffler 		 * in one cache slot automatically enable use.
5405901d2d3SSam Leffler 		 */
5415901d2d3SSam Leffler 		if (ath_hal_hastkipsplit(ah) ||
5425901d2d3SSam Leffler 		    !ath_hal_settkipsplit(ah, AH_FALSE))
543c42a7b7eSSam Leffler 			sc->sc_splitmic = 1;
544c42a7b7eSSam Leffler 	}
545e8fd88a3SSam Leffler 	sc->sc_hasclrkey = ath_hal_ciphersupported(ah, HAL_CIPHER_CLR);
546e8fd88a3SSam Leffler 	sc->sc_mcastkey = ath_hal_getmcastkeysearch(ah);
547c42a7b7eSSam Leffler 	/*
5485901d2d3SSam Leffler 	 * Mark key cache slots associated with global keys
5495901d2d3SSam Leffler 	 * as in use.  If we knew TKIP was not to be used we
5505901d2d3SSam Leffler 	 * could leave the +32, +64, and +32+64 slots free.
5515901d2d3SSam Leffler 	 */
5525901d2d3SSam Leffler 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
5535901d2d3SSam Leffler 		setbit(sc->sc_keymap, i);
5545901d2d3SSam Leffler 		setbit(sc->sc_keymap, i+64);
5555901d2d3SSam Leffler 		if (sc->sc_splitmic) {
5565901d2d3SSam Leffler 			setbit(sc->sc_keymap, i+32);
5575901d2d3SSam Leffler 			setbit(sc->sc_keymap, i+32+64);
5585901d2d3SSam Leffler 		}
5595901d2d3SSam Leffler 	}
5605901d2d3SSam Leffler 	/*
561c42a7b7eSSam Leffler 	 * TPC support can be done either with a global cap or
562c42a7b7eSSam Leffler 	 * per-packet support.  The latter is not available on
563c42a7b7eSSam Leffler 	 * all parts.  We're a bit pedantic here as all parts
564c42a7b7eSSam Leffler 	 * support a global cap.
565c42a7b7eSSam Leffler 	 */
566c59005e9SSam Leffler 	if (ath_hal_hastpc(ah) || ath_hal_hastxpowlimit(ah))
567c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TXPMGT;
568c42a7b7eSSam Leffler 
569c42a7b7eSSam Leffler 	/*
570c42a7b7eSSam Leffler 	 * Mark WME capability only if we have sufficient
571c42a7b7eSSam Leffler 	 * hardware queues to do proper priority scheduling.
572c42a7b7eSSam Leffler 	 */
573c42a7b7eSSam Leffler 	if (sc->sc_ac2q[WME_AC_BE] != sc->sc_ac2q[WME_AC_BK])
574c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_WME;
575c42a7b7eSSam Leffler 	/*
576e8fd88a3SSam Leffler 	 * Check for misc other capabilities.
577c42a7b7eSSam Leffler 	 */
578c42a7b7eSSam Leffler 	if (ath_hal_hasbursting(ah))
579c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_BURST;
58068e8e04eSSam Leffler 	if (ath_hal_hasfastframes(ah))
58168e8e04eSSam Leffler 		ic->ic_caps |= IEEE80211_C_FF;
58268e8e04eSSam Leffler 	if (ath_hal_getwirelessmodes(ah, ath_countrycode) & (HAL_MODE_108G|HAL_MODE_TURBO))
58368e8e04eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TURBOP;
584c42a7b7eSSam Leffler 
585c42a7b7eSSam Leffler 	/*
586c42a7b7eSSam Leffler 	 * Indicate we need the 802.11 header padded to a
587c42a7b7eSSam Leffler 	 * 32-bit boundary for 4-address and QoS frames.
588c42a7b7eSSam Leffler 	 */
589c42a7b7eSSam Leffler 	ic->ic_flags |= IEEE80211_F_DATAPAD;
590c42a7b7eSSam Leffler 
591c42a7b7eSSam Leffler 	/*
592c42a7b7eSSam Leffler 	 * Query the hal about antenna support.
593c42a7b7eSSam Leffler 	 */
594c42a7b7eSSam Leffler 	sc->sc_defant = ath_hal_getdefantenna(ah);
595c42a7b7eSSam Leffler 
596c42a7b7eSSam Leffler 	/*
597c42a7b7eSSam Leffler 	 * Not all chips have the VEOL support we want to
598c42a7b7eSSam Leffler 	 * use with IBSS beacons; check here for it.
599c42a7b7eSSam Leffler 	 */
600c42a7b7eSSam Leffler 	sc->sc_hasveol = ath_hal_hasveol(ah);
6015591b213SSam Leffler 
6025591b213SSam Leffler 	/* get mac address from hardware */
6035591b213SSam Leffler 	ath_hal_getmac(ah, ic->ic_myaddr);
6045591b213SSam Leffler 
6055591b213SSam Leffler 	/* call MI attach routine. */
606c42a7b7eSSam Leffler 	ieee80211_ifattach(ic);
6077a04dc27SSam Leffler 	sc->sc_opmode = ic->ic_opmode;
6085591b213SSam Leffler 	/* override default methods */
6095591b213SSam Leffler 	ic->ic_node_alloc = ath_node_alloc;
6101e774079SSam Leffler 	sc->sc_node_free = ic->ic_node_free;
6115591b213SSam Leffler 	ic->ic_node_free = ath_node_free;
612de5af704SSam Leffler 	ic->ic_node_getrssi = ath_node_getrssi;
61368e8e04eSSam Leffler 	ic->ic_node_getsignal = ath_node_getsignal;
614c42a7b7eSSam Leffler 	sc->sc_recv_mgmt = ic->ic_recv_mgmt;
615c42a7b7eSSam Leffler 	ic->ic_recv_mgmt = ath_recv_mgmt;
61645bbf62fSSam Leffler 	sc->sc_newstate = ic->ic_newstate;
61745bbf62fSSam Leffler 	ic->ic_newstate = ath_newstate;
61868e8e04eSSam Leffler 	ic->ic_scan_start = ath_scan_start;
61968e8e04eSSam Leffler 	ic->ic_scan_end = ath_scan_end;
62068e8e04eSSam Leffler 	ic->ic_set_channel = ath_set_channel;
621c1225b52SSam Leffler 	ic->ic_crypto.cs_max_keyix = sc->sc_keymax;
622c42a7b7eSSam Leffler 	ic->ic_crypto.cs_key_alloc = ath_key_alloc;
623c42a7b7eSSam Leffler 	ic->ic_crypto.cs_key_delete = ath_key_delete;
624c42a7b7eSSam Leffler 	ic->ic_crypto.cs_key_set = ath_key_set;
625c42a7b7eSSam Leffler 	ic->ic_crypto.cs_key_update_begin = ath_key_update_begin;
626c42a7b7eSSam Leffler 	ic->ic_crypto.cs_key_update_end = ath_key_update_end;
627664443d0SSam Leffler 	ic->ic_raw_xmit = ath_raw_xmit;
62845bbf62fSSam Leffler 	/* complete initialization */
629c42a7b7eSSam Leffler 	ieee80211_media_init(ic, ath_media_change, ieee80211_media_status);
6305591b213SSam Leffler 
631c42a7b7eSSam Leffler 	ath_bpfattach(sc);
6324866e6c2SSam Leffler 	/*
6334866e6c2SSam Leffler 	 * Setup dynamic sysctl's now that country code and
6344866e6c2SSam Leffler 	 * regdomain are available from the hal.
6354866e6c2SSam Leffler 	 */
6364866e6c2SSam Leffler 	ath_sysctlattach(sc);
63773454c73SSam Leffler 
638c42a7b7eSSam Leffler 	if (bootverbose)
639c42a7b7eSSam Leffler 		ieee80211_announce(ic);
640c42a7b7eSSam Leffler 	ath_announce(sc);
6415591b213SSam Leffler 	return 0;
642b28b4653SSam Leffler bad2:
643c42a7b7eSSam Leffler 	ath_tx_cleanup(sc);
644b28b4653SSam Leffler 	ath_desc_free(sc);
6455591b213SSam Leffler bad:
6465591b213SSam Leffler 	if (ah)
6475591b213SSam Leffler 		ath_hal_detach(ah);
648fc74a9f9SBrooks Davis 	if (ifp != NULL)
649fc74a9f9SBrooks Davis 		if_free(ifp);
6505591b213SSam Leffler 	sc->sc_invalid = 1;
6515591b213SSam Leffler 	return error;
6525591b213SSam Leffler }
6535591b213SSam Leffler 
6545591b213SSam Leffler int
6555591b213SSam Leffler ath_detach(struct ath_softc *sc)
6565591b213SSam Leffler {
657fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
6585591b213SSam Leffler 
659c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
660c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
6615591b213SSam Leffler 
6625591b213SSam Leffler 	ath_stop(ifp);
66373454c73SSam Leffler 	bpfdetach(ifp);
664c42a7b7eSSam Leffler 	/*
665c42a7b7eSSam Leffler 	 * NB: the order of these is important:
666c42a7b7eSSam Leffler 	 * o call the 802.11 layer before detaching the hal to
667c42a7b7eSSam Leffler 	 *   insure callbacks into the driver to delete global
668c42a7b7eSSam Leffler 	 *   key cache entries can be handled
669c42a7b7eSSam Leffler 	 * o reclaim the tx queue data structures after calling
670c42a7b7eSSam Leffler 	 *   the 802.11 layer as we'll get called back to reclaim
671c42a7b7eSSam Leffler 	 *   node state and potentially want to use them
672c42a7b7eSSam Leffler 	 * o to cleanup the tx queues the hal is called, so detach
673c42a7b7eSSam Leffler 	 *   it last
674c42a7b7eSSam Leffler 	 * Other than that, it's straightforward...
675c42a7b7eSSam Leffler 	 */
676c42a7b7eSSam Leffler 	ieee80211_ifdetach(&sc->sc_ic);
67786e07743SSam Leffler #ifdef ATH_TX99_DIAG
67886e07743SSam Leffler 	if (sc->sc_tx99 != NULL)
67986e07743SSam Leffler 		sc->sc_tx99->detach(sc->sc_tx99);
68086e07743SSam Leffler #endif
6810bbf5441SSam Leffler 	taskqueue_free(sc->sc_tq);
682c42a7b7eSSam Leffler 	ath_rate_detach(sc->sc_rc);
6835591b213SSam Leffler 	ath_desc_free(sc);
684c42a7b7eSSam Leffler 	ath_tx_cleanup(sc);
6855591b213SSam Leffler 	ath_hal_detach(sc->sc_ah);
686c4c6f08fSRuslan Ermilov 	if_free(ifp);
687f0b2a0beSSam Leffler 
6885591b213SSam Leffler 	return 0;
6895591b213SSam Leffler }
6905591b213SSam Leffler 
6915591b213SSam Leffler void
6925591b213SSam Leffler ath_suspend(struct ath_softc *sc)
6935591b213SSam Leffler {
694fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
6955591b213SSam Leffler 
696c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
697c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
6985591b213SSam Leffler 
6995591b213SSam Leffler 	ath_stop(ifp);
7005591b213SSam Leffler }
7015591b213SSam Leffler 
7025591b213SSam Leffler void
7035591b213SSam Leffler ath_resume(struct ath_softc *sc)
7045591b213SSam Leffler {
705fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
7065591b213SSam Leffler 
707c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
708c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
7095591b213SSam Leffler 
7106b59f5e3SSam Leffler 	if (ifp->if_flags & IFF_UP) {
711fc74a9f9SBrooks Davis 		ath_init(sc);
71213f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
7135591b213SSam Leffler 			ath_start(ifp);
7145591b213SSam Leffler 	}
715b50c8bdeSSam Leffler 	if (sc->sc_softled) {
716b50c8bdeSSam Leffler 		ath_hal_gpioCfgOutput(sc->sc_ah, sc->sc_ledpin);
717b50c8bdeSSam Leffler 		ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, !sc->sc_ledon);
718b50c8bdeSSam Leffler 	}
7196b59f5e3SSam Leffler }
7205591b213SSam Leffler 
7215591b213SSam Leffler void
7225591b213SSam Leffler ath_shutdown(struct ath_softc *sc)
7235591b213SSam Leffler {
724fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
7255591b213SSam Leffler 
726c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
727c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
7285591b213SSam Leffler 
7295591b213SSam Leffler 	ath_stop(ifp);
7305591b213SSam Leffler }
7315591b213SSam Leffler 
732c42a7b7eSSam Leffler /*
733c42a7b7eSSam Leffler  * Interrupt handler.  Most of the actual processing is deferred.
734c42a7b7eSSam Leffler  */
7355591b213SSam Leffler void
7365591b213SSam Leffler ath_intr(void *arg)
7375591b213SSam Leffler {
7385591b213SSam Leffler 	struct ath_softc *sc = arg;
739fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
7405591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
7415591b213SSam Leffler 	HAL_INT status;
7425591b213SSam Leffler 
7435591b213SSam Leffler 	if (sc->sc_invalid) {
7445591b213SSam Leffler 		/*
745b58b3803SSam Leffler 		 * The hardware is not ready/present, don't touch anything.
746b58b3803SSam Leffler 		 * Note this can happen early on if the IRQ is shared.
7475591b213SSam Leffler 		 */
748c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid; ignored\n", __func__);
7495591b213SSam Leffler 		return;
7505591b213SSam Leffler 	}
751fdd758d4SSam Leffler 	if (!ath_hal_intrpend(ah))		/* shared irq, not for us */
752fdd758d4SSam Leffler 		return;
75368e8e04eSSam Leffler 	if ((ifp->if_flags & IFF_UP) == 0 ||
75468e8e04eSSam Leffler 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
75568e8e04eSSam Leffler 		HAL_INT status;
75668e8e04eSSam Leffler 
757c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
758c42a7b7eSSam Leffler 			__func__, ifp->if_flags);
7595591b213SSam Leffler 		ath_hal_getisr(ah, &status);	/* clear ISR */
7605591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable further intr's */
7615591b213SSam Leffler 		return;
7625591b213SSam Leffler 	}
763c42a7b7eSSam Leffler 	/*
764c42a7b7eSSam Leffler 	 * Figure out the reason(s) for the interrupt.  Note
765c42a7b7eSSam Leffler 	 * that the hal returns a pseudo-ISR that may include
766c42a7b7eSSam Leffler 	 * bits we haven't explicitly enabled so we mask the
767c42a7b7eSSam Leffler 	 * value to insure we only process bits we requested.
768c42a7b7eSSam Leffler 	 */
7695591b213SSam Leffler 	ath_hal_getisr(ah, &status);		/* NB: clears ISR too */
770c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_INTR, "%s: status 0x%x\n", __func__, status);
771ecddff40SSam Leffler 	status &= sc->sc_imask;			/* discard unasked for bits */
7725591b213SSam Leffler 	if (status & HAL_INT_FATAL) {
7735591b213SSam Leffler 		sc->sc_stats.ast_hardware++;
7745591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
77516c8acaaSSam Leffler 		ath_fatal_proc(sc, 0);
7765591b213SSam Leffler 	} else if (status & HAL_INT_RXORN) {
7775591b213SSam Leffler 		sc->sc_stats.ast_rxorn++;
7785591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
7790bbf5441SSam Leffler 		taskqueue_enqueue(sc->sc_tq, &sc->sc_rxorntask);
7805591b213SSam Leffler 	} else {
781c42a7b7eSSam Leffler 		if (status & HAL_INT_SWBA) {
782c42a7b7eSSam Leffler 			/*
783c42a7b7eSSam Leffler 			 * Software beacon alert--time to send a beacon.
784c42a7b7eSSam Leffler 			 * Handle beacon transmission directly; deferring
785c42a7b7eSSam Leffler 			 * this is too slow to meet timing constraints
786c42a7b7eSSam Leffler 			 * under load.
787c42a7b7eSSam Leffler 			 */
788c42a7b7eSSam Leffler 			ath_beacon_proc(sc, 0);
789c42a7b7eSSam Leffler 		}
7905591b213SSam Leffler 		if (status & HAL_INT_RXEOL) {
7915591b213SSam Leffler 			/*
7925591b213SSam Leffler 			 * NB: the hardware should re-read the link when
7935591b213SSam Leffler 			 *     RXE bit is written, but it doesn't work at
7945591b213SSam Leffler 			 *     least on older hardware revs.
7955591b213SSam Leffler 			 */
7965591b213SSam Leffler 			sc->sc_stats.ast_rxeol++;
7975591b213SSam Leffler 			sc->sc_rxlink = NULL;
7985591b213SSam Leffler 		}
7995591b213SSam Leffler 		if (status & HAL_INT_TXURN) {
8005591b213SSam Leffler 			sc->sc_stats.ast_txurn++;
8015591b213SSam Leffler 			/* bump tx trigger level */
8025591b213SSam Leffler 			ath_hal_updatetxtriglevel(ah, AH_TRUE);
8035591b213SSam Leffler 		}
8045591b213SSam Leffler 		if (status & HAL_INT_RX)
8050bbf5441SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
8065591b213SSam Leffler 		if (status & HAL_INT_TX)
8070bbf5441SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_txtask);
8085591b213SSam Leffler 		if (status & HAL_INT_BMISS) {
8095591b213SSam Leffler 			sc->sc_stats.ast_bmiss++;
8100bbf5441SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_bmisstask);
8115591b213SSam Leffler 		}
812c42a7b7eSSam Leffler 		if (status & HAL_INT_MIB) {
813c42a7b7eSSam Leffler 			sc->sc_stats.ast_mib++;
814c42a7b7eSSam Leffler 			/*
815c42a7b7eSSam Leffler 			 * Disable interrupts until we service the MIB
816c42a7b7eSSam Leffler 			 * interrupt; otherwise it will continue to fire.
817c42a7b7eSSam Leffler 			 */
818c42a7b7eSSam Leffler 			ath_hal_intrset(ah, 0);
819c42a7b7eSSam Leffler 			/*
820c42a7b7eSSam Leffler 			 * Let the hal handle the event.  We assume it will
821c42a7b7eSSam Leffler 			 * clear whatever condition caused the interrupt.
822c42a7b7eSSam Leffler 			 */
823ffa2cab6SSam Leffler 			ath_hal_mibevent(ah, &sc->sc_halstats);
824c42a7b7eSSam Leffler 			ath_hal_intrset(ah, sc->sc_imask);
825c42a7b7eSSam Leffler 		}
8265591b213SSam Leffler 	}
8275591b213SSam Leffler }
8285591b213SSam Leffler 
8295591b213SSam Leffler static void
8305591b213SSam Leffler ath_fatal_proc(void *arg, int pending)
8315591b213SSam Leffler {
8325591b213SSam Leffler 	struct ath_softc *sc = arg;
833fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
83416c8acaaSSam Leffler 	u_int32_t *state;
83516c8acaaSSam Leffler 	u_int32_t len;
83668e8e04eSSam Leffler 	void *sp;
8375591b213SSam Leffler 
838c42a7b7eSSam Leffler 	if_printf(ifp, "hardware error; resetting\n");
83916c8acaaSSam Leffler 	/*
84016c8acaaSSam Leffler 	 * Fatal errors are unrecoverable.  Typically these
84116c8acaaSSam Leffler 	 * are caused by DMA errors.  Collect h/w state from
84216c8acaaSSam Leffler 	 * the hal so we can diagnose what's going on.
84316c8acaaSSam Leffler 	 */
84468e8e04eSSam Leffler 	if (ath_hal_getfatalstate(sc->sc_ah, &sp, &len)) {
84516c8acaaSSam Leffler 		KASSERT(len >= 6*sizeof(u_int32_t), ("len %u bytes", len));
84668e8e04eSSam Leffler 		state = sp;
84716c8acaaSSam Leffler 		if_printf(ifp, "0x%08x 0x%08x 0x%08x, 0x%08x 0x%08x 0x%08x\n",
84816c8acaaSSam Leffler 		    state[0], state[1] , state[2], state[3],
84916c8acaaSSam Leffler 		    state[4], state[5]);
85016c8acaaSSam Leffler 	}
851c42a7b7eSSam Leffler 	ath_reset(ifp);
8525591b213SSam Leffler }
8535591b213SSam Leffler 
8545591b213SSam Leffler static void
8555591b213SSam Leffler ath_rxorn_proc(void *arg, int pending)
8565591b213SSam Leffler {
8575591b213SSam Leffler 	struct ath_softc *sc = arg;
858fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
8595591b213SSam Leffler 
860c42a7b7eSSam Leffler 	if_printf(ifp, "rx FIFO overrun; resetting\n");
861c42a7b7eSSam Leffler 	ath_reset(ifp);
8625591b213SSam Leffler }
8635591b213SSam Leffler 
8645591b213SSam Leffler static void
8655591b213SSam Leffler ath_bmiss_proc(void *arg, int pending)
8665591b213SSam Leffler {
8675591b213SSam Leffler 	struct ath_softc *sc = arg;
8685591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
8695591b213SSam Leffler 
870c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: pending %u\n", __func__, pending);
8715591b213SSam Leffler 	KASSERT(ic->ic_opmode == IEEE80211_M_STA,
8725591b213SSam Leffler 		("unexpect operating mode %u", ic->ic_opmode));
873e585d188SSam Leffler 	if (ic->ic_state == IEEE80211_S_RUN) {
874d7736e13SSam Leffler 		u_int64_t lastrx = sc->sc_lastrx;
875d7736e13SSam Leffler 		u_int64_t tsf = ath_hal_gettsf64(sc->sc_ah);
876d7736e13SSam Leffler 		u_int bmisstimeout =
877d7736e13SSam Leffler 			ic->ic_bmissthreshold * ic->ic_bss->ni_intval * 1024;
878d7736e13SSam Leffler 
879d7736e13SSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
880d7736e13SSam Leffler 		    "%s: tsf %llu lastrx %lld (%llu) bmiss %u\n",
881d7736e13SSam Leffler 		    __func__, (unsigned long long) tsf,
882d7736e13SSam Leffler 		    (unsigned long long)(tsf - lastrx),
883d7736e13SSam Leffler 		    (unsigned long long) lastrx, bmisstimeout);
884e585d188SSam Leffler 		/*
885d7736e13SSam Leffler 		 * Workaround phantom bmiss interrupts by sanity-checking
886d7736e13SSam Leffler 		 * the time of our last rx'd frame.  If it is within the
887d7736e13SSam Leffler 		 * beacon miss interval then ignore the interrupt.  If it's
888d7736e13SSam Leffler 		 * truly a bmiss we'll get another interrupt soon and that'll
889d7736e13SSam Leffler 		 * be dispatched up for processing.
890e585d188SSam Leffler 		 */
8910bf686c1SRobert Watson 		if (tsf - lastrx > bmisstimeout)
892d7736e13SSam Leffler 			ieee80211_beacon_miss(ic);
8930bf686c1SRobert Watson 		else
894d7736e13SSam Leffler 			sc->sc_stats.ast_bmiss_phantom++;
895e585d188SSam Leffler 	}
8965591b213SSam Leffler }
8975591b213SSam Leffler 
898724c193aSSam Leffler /*
899724c193aSSam Leffler  * Convert net80211 channel to a HAL channel with the flags
900724c193aSSam Leffler  * constrained to reflect the current operating mode and
901724c193aSSam Leffler  * the frequency possibly mapped for GSM channels.
902724c193aSSam Leffler  */
903724c193aSSam Leffler static void
90468e8e04eSSam Leffler ath_mapchan(HAL_CHANNEL *hc, const struct ieee80211_channel *chan)
9055591b213SSam Leffler {
906c42a7b7eSSam Leffler #define	N(a)	(sizeof(a) / sizeof(a[0]))
90768e8e04eSSam Leffler 	static const u_int modeflags[IEEE80211_MODE_MAX] = {
9085591b213SSam Leffler 		0,			/* IEEE80211_MODE_AUTO */
9095591b213SSam Leffler 		CHANNEL_A,		/* IEEE80211_MODE_11A */
9105591b213SSam Leffler 		CHANNEL_B,		/* IEEE80211_MODE_11B */
9115591b213SSam Leffler 		CHANNEL_PUREG,		/* IEEE80211_MODE_11G */
912c42a7b7eSSam Leffler 		0,			/* IEEE80211_MODE_FH */
91368e8e04eSSam Leffler 		CHANNEL_108A,		/* IEEE80211_MODE_TURBO_A */
91468e8e04eSSam Leffler 		CHANNEL_108G,		/* IEEE80211_MODE_TURBO_G */
91568e8e04eSSam Leffler 		CHANNEL_ST,		/* IEEE80211_MODE_STURBO_A */
91668e8e04eSSam Leffler 		CHANNEL_A,		/* IEEE80211_MODE_11NA */
91768e8e04eSSam Leffler 		CHANNEL_PUREG,		/* IEEE80211_MODE_11NG */
9185591b213SSam Leffler 	};
91968e8e04eSSam Leffler 	enum ieee80211_phymode mode = ieee80211_chan2mode(chan);
920c42a7b7eSSam Leffler 
921c42a7b7eSSam Leffler 	KASSERT(mode < N(modeflags), ("unexpected phy mode %u", mode));
922c42a7b7eSSam Leffler 	KASSERT(modeflags[mode] != 0, ("mode %u undefined", mode));
923724c193aSSam Leffler 	hc->channelFlags = modeflags[mode];
924aaa70f2fSSam Leffler 	if (IEEE80211_IS_CHAN_HALF(chan))
925724c193aSSam Leffler 		hc->channelFlags |= CHANNEL_HALF;
926aaa70f2fSSam Leffler 	if (IEEE80211_IS_CHAN_QUARTER(chan))
927724c193aSSam Leffler 		hc->channelFlags |= CHANNEL_QUARTER;
92868e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_HT20(chan))
92968e8e04eSSam Leffler 		hc->channelFlags |= CHANNEL_HT20;
93068e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_HT40D(chan))
93168e8e04eSSam Leffler 		hc->channelFlags |= CHANNEL_HT40MINUS;
93268e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_HT40U(chan))
93368e8e04eSSam Leffler 		hc->channelFlags |= CHANNEL_HT40PLUS;
934724c193aSSam Leffler 
935724c193aSSam Leffler 	hc->channel = IEEE80211_IS_CHAN_GSM(chan) ?
936724c193aSSam Leffler 		2422 + (922 - chan->ic_freq) : chan->ic_freq;
937c42a7b7eSSam Leffler #undef N
9385591b213SSam Leffler }
9395591b213SSam Leffler 
9405591b213SSam Leffler static void
9415591b213SSam Leffler ath_init(void *arg)
9425591b213SSam Leffler {
9435591b213SSam Leffler 	struct ath_softc *sc = (struct ath_softc *) arg;
9445591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
945fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
9465591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
9475591b213SSam Leffler 	HAL_STATUS status;
9485591b213SSam Leffler 
949c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
950c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
9515591b213SSam Leffler 
952f0b2a0beSSam Leffler 	ATH_LOCK(sc);
9535591b213SSam Leffler 	/*
9545591b213SSam Leffler 	 * Stop anything previously setup.  This is safe
9555591b213SSam Leffler 	 * whether this is the first time through or not.
9565591b213SSam Leffler 	 */
957c42a7b7eSSam Leffler 	ath_stop_locked(ifp);
9585591b213SSam Leffler 
9595591b213SSam Leffler 	/*
9605591b213SSam Leffler 	 * The basic interface to setting the hardware in a good
9615591b213SSam Leffler 	 * state is ``reset''.  On return the hardware is known to
9625591b213SSam Leffler 	 * be powered up and with interrupts disabled.  This must
9635591b213SSam Leffler 	 * be followed by initialization of the appropriate bits
9645591b213SSam Leffler 	 * and then setup of the interrupt mask.
9655591b213SSam Leffler 	 */
96668e8e04eSSam Leffler 	ath_mapchan(&sc->sc_curchan, ic->ic_curchan);
9677a04dc27SSam Leffler 	if (!ath_hal_reset(ah, sc->sc_opmode, &sc->sc_curchan, AH_FALSE, &status)) {
9685591b213SSam Leffler 		if_printf(ifp, "unable to reset hardware; hal status %u\n",
9695591b213SSam Leffler 			status);
9705591b213SSam Leffler 		goto done;
9715591b213SSam Leffler 	}
9725591b213SSam Leffler 
9735591b213SSam Leffler 	/*
974c42a7b7eSSam Leffler 	 * This is needed only to setup initial state
975c42a7b7eSSam Leffler 	 * but it's best done after a reset.
976c42a7b7eSSam Leffler 	 */
977c42a7b7eSSam Leffler 	ath_update_txpow(sc);
978c59005e9SSam Leffler 	/*
979c59005e9SSam Leffler 	 * Likewise this is set during reset so update
980c59005e9SSam Leffler 	 * state cached in the driver.
981c59005e9SSam Leffler 	 */
982c59005e9SSam Leffler 	sc->sc_diversity = ath_hal_getdiversity(ah);
983bd5a9920SSam Leffler 	sc->sc_calinterval = 1;
984bd5a9920SSam Leffler 	sc->sc_caltries = 0;
985c42a7b7eSSam Leffler 
986c42a7b7eSSam Leffler 	/*
9875591b213SSam Leffler 	 * Setup the hardware after reset: the key cache
9885591b213SSam Leffler 	 * is filled as needed and the receive engine is
9895591b213SSam Leffler 	 * set going.  Frame transmit is handled entirely
9905591b213SSam Leffler 	 * in the frame output path; there's nothing to do
9915591b213SSam Leffler 	 * here except setup the interrupt mask.
9925591b213SSam Leffler 	 */
9935591b213SSam Leffler 	if (ath_startrecv(sc) != 0) {
9945591b213SSam Leffler 		if_printf(ifp, "unable to start recv logic\n");
9955591b213SSam Leffler 		goto done;
9965591b213SSam Leffler 	}
9975591b213SSam Leffler 
9985591b213SSam Leffler 	/*
9995591b213SSam Leffler 	 * Enable interrupts.
10005591b213SSam Leffler 	 */
10015591b213SSam Leffler 	sc->sc_imask = HAL_INT_RX | HAL_INT_TX
10025591b213SSam Leffler 		  | HAL_INT_RXEOL | HAL_INT_RXORN
10035591b213SSam Leffler 		  | HAL_INT_FATAL | HAL_INT_GLOBAL;
1004c42a7b7eSSam Leffler 	/*
1005c42a7b7eSSam Leffler 	 * Enable MIB interrupts when there are hardware phy counters.
1006c42a7b7eSSam Leffler 	 * Note we only do this (at the moment) for station mode.
1007c42a7b7eSSam Leffler 	 */
1008c42a7b7eSSam Leffler 	if (sc->sc_needmib && ic->ic_opmode == IEEE80211_M_STA)
1009c42a7b7eSSam Leffler 		sc->sc_imask |= HAL_INT_MIB;
10105591b213SSam Leffler 	ath_hal_intrset(ah, sc->sc_imask);
10115591b213SSam Leffler 
101213f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
10135591b213SSam Leffler 	ic->ic_state = IEEE80211_S_INIT;
10145591b213SSam Leffler 
10155591b213SSam Leffler 	/*
10165591b213SSam Leffler 	 * The hardware should be ready to go now so it's safe
10175591b213SSam Leffler 	 * to kick the 802.11 state machine as it's likely to
10185591b213SSam Leffler 	 * immediately call back to us to send mgmt frames.
10195591b213SSam Leffler 	 */
1020b5c99415SSam Leffler 	ath_chan_change(sc, ic->ic_curchan);
102186e07743SSam Leffler #ifdef ATH_TX99_DIAG
102286e07743SSam Leffler 	if (sc->sc_tx99 != NULL)
102386e07743SSam Leffler 		sc->sc_tx99->start(sc->sc_tx99);
102486e07743SSam Leffler 	else
102586e07743SSam Leffler #endif
1026c42a7b7eSSam Leffler 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1027c42a7b7eSSam Leffler 		if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
102845bbf62fSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
1029c42a7b7eSSam Leffler 	} else
10306b59f5e3SSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
10315591b213SSam Leffler done:
1032f0b2a0beSSam Leffler 	ATH_UNLOCK(sc);
10335591b213SSam Leffler }
10345591b213SSam Leffler 
10355591b213SSam Leffler static void
1036c42a7b7eSSam Leffler ath_stop_locked(struct ifnet *ifp)
10375591b213SSam Leffler {
10385591b213SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
1039c42a7b7eSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
10405591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
10415591b213SSam Leffler 
1042c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid %u if_flags 0x%x\n",
1043c42a7b7eSSam Leffler 		__func__, sc->sc_invalid, ifp->if_flags);
10445591b213SSam Leffler 
1045c42a7b7eSSam Leffler 	ATH_LOCK_ASSERT(sc);
104613f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
10475591b213SSam Leffler 		/*
10485591b213SSam Leffler 		 * Shutdown the hardware and driver:
1049c42a7b7eSSam Leffler 		 *    reset 802.11 state machine
10505591b213SSam Leffler 		 *    turn off timers
1051c42a7b7eSSam Leffler 		 *    disable interrupts
1052c42a7b7eSSam Leffler 		 *    turn off the radio
10535591b213SSam Leffler 		 *    clear transmit machinery
10545591b213SSam Leffler 		 *    clear receive machinery
10555591b213SSam Leffler 		 *    drain and release tx queues
10565591b213SSam Leffler 		 *    reclaim beacon resources
10575591b213SSam Leffler 		 *    power down hardware
10585591b213SSam Leffler 		 *
10595591b213SSam Leffler 		 * Note that some of this work is not possible if the
10605591b213SSam Leffler 		 * hardware is gone (invalid).
10615591b213SSam Leffler 		 */
106286e07743SSam Leffler #ifdef ATH_TX99_DIAG
106386e07743SSam Leffler 		if (sc->sc_tx99 != NULL)
106486e07743SSam Leffler 			sc->sc_tx99->stop(sc->sc_tx99);
106586e07743SSam Leffler #endif
1066c42a7b7eSSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
106713f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
10685591b213SSam Leffler 		ifp->if_timer = 0;
1069c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
10703e50ec2cSSam Leffler 			if (sc->sc_softled) {
10713e50ec2cSSam Leffler 				callout_stop(&sc->sc_ledtimer);
10723e50ec2cSSam Leffler 				ath_hal_gpioset(ah, sc->sc_ledpin,
10733e50ec2cSSam Leffler 					!sc->sc_ledon);
10743e50ec2cSSam Leffler 				sc->sc_blinking = 0;
10753e50ec2cSSam Leffler 			}
10765591b213SSam Leffler 			ath_hal_intrset(ah, 0);
1077c42a7b7eSSam Leffler 		}
10785591b213SSam Leffler 		ath_draintxq(sc);
1079c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
10805591b213SSam Leffler 			ath_stoprecv(sc);
1081c42a7b7eSSam Leffler 			ath_hal_phydisable(ah);
1082c42a7b7eSSam Leffler 		} else
10835591b213SSam Leffler 			sc->sc_rxlink = NULL;
1084154b8df2SMax Laier 		IFQ_DRV_PURGE(&ifp->if_snd);
10855591b213SSam Leffler 		ath_beacon_free(sc);
1086c42a7b7eSSam Leffler 	}
1087c42a7b7eSSam Leffler }
1088c42a7b7eSSam Leffler 
1089c42a7b7eSSam Leffler static void
1090c42a7b7eSSam Leffler ath_stop(struct ifnet *ifp)
1091c42a7b7eSSam Leffler {
1092c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
1093c42a7b7eSSam Leffler 
1094c42a7b7eSSam Leffler 	ATH_LOCK(sc);
1095c42a7b7eSSam Leffler 	ath_stop_locked(ifp);
1096c42a7b7eSSam Leffler 	if (!sc->sc_invalid) {
1097c42a7b7eSSam Leffler 		/*
1098c42a7b7eSSam Leffler 		 * Set the chip in full sleep mode.  Note that we are
1099c42a7b7eSSam Leffler 		 * careful to do this only when bringing the interface
1100c42a7b7eSSam Leffler 		 * completely to a stop.  When the chip is in this state
1101c42a7b7eSSam Leffler 		 * it must be carefully woken up or references to
1102c42a7b7eSSam Leffler 		 * registers in the PCI clock domain may freeze the bus
1103c42a7b7eSSam Leffler 		 * (and system).  This varies by chip and is mostly an
1104c42a7b7eSSam Leffler 		 * issue with newer parts that go to sleep more quickly.
1105c42a7b7eSSam Leffler 		 */
1106bd5a9920SSam Leffler 		ath_hal_setpower(sc->sc_ah, HAL_PM_FULL_SLEEP);
11075591b213SSam Leffler 	}
1108f0b2a0beSSam Leffler 	ATH_UNLOCK(sc);
11095591b213SSam Leffler }
11105591b213SSam Leffler 
11115591b213SSam Leffler /*
11125591b213SSam Leffler  * Reset the hardware w/o losing operational state.  This is
11135591b213SSam Leffler  * basically a more efficient way of doing ath_stop, ath_init,
11145591b213SSam Leffler  * followed by state transitions to the current 802.11
1115c42a7b7eSSam Leffler  * operational state.  Used to recover from various errors and
1116c42a7b7eSSam Leffler  * to reset or reload hardware state.
11175591b213SSam Leffler  */
1118c42a7b7eSSam Leffler static int
1119c42a7b7eSSam Leffler ath_reset(struct ifnet *ifp)
11205591b213SSam Leffler {
1121c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
11225591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
11235591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
11245591b213SSam Leffler 	HAL_STATUS status;
11255591b213SSam Leffler 
11265591b213SSam Leffler 	/*
11275591b213SSam Leffler 	 * Convert to a HAL channel description with the flags
11285591b213SSam Leffler 	 * constrained to reflect the current operating mode.
11295591b213SSam Leffler 	 */
113068e8e04eSSam Leffler 	ath_mapchan(&sc->sc_curchan, ic->ic_curchan);
11315591b213SSam Leffler 
11325591b213SSam Leffler 	ath_hal_intrset(ah, 0);		/* disable interrupts */
11335591b213SSam Leffler 	ath_draintxq(sc);		/* stop xmit side */
11345591b213SSam Leffler 	ath_stoprecv(sc);		/* stop recv side */
11355591b213SSam Leffler 	/* NB: indicate channel change so we do a full reset */
11367a04dc27SSam Leffler 	if (!ath_hal_reset(ah, sc->sc_opmode, &sc->sc_curchan, AH_TRUE, &status))
11375591b213SSam Leffler 		if_printf(ifp, "%s: unable to reset hardware; hal status %u\n",
11385591b213SSam Leffler 			__func__, status);
1139c42a7b7eSSam Leffler 	ath_update_txpow(sc);		/* update tx power state */
1140c59005e9SSam Leffler 	sc->sc_diversity = ath_hal_getdiversity(ah);
1141bd5a9920SSam Leffler 	sc->sc_calinterval = 1;
1142bd5a9920SSam Leffler 	sc->sc_caltries = 0;
114368e8e04eSSam Leffler 	if (ath_startrecv(sc) != 0)	/* restart recv */
114468e8e04eSSam Leffler 		if_printf(ifp, "%s: unable to start recv logic\n", __func__);
1145c42a7b7eSSam Leffler 	/*
1146c42a7b7eSSam Leffler 	 * We may be doing a reset in response to an ioctl
1147c42a7b7eSSam Leffler 	 * that changes the channel so update any state that
1148c42a7b7eSSam Leffler 	 * might change as a result.
1149c42a7b7eSSam Leffler 	 */
1150724c193aSSam Leffler 	ath_chan_change(sc, ic->ic_curchan);
11515591b213SSam Leffler 	if (ic->ic_state == IEEE80211_S_RUN)
11525591b213SSam Leffler 		ath_beacon_config(sc);	/* restart beacons */
1153c42a7b7eSSam Leffler 	ath_hal_intrset(ah, sc->sc_imask);
1154c42a7b7eSSam Leffler 
1155c42a7b7eSSam Leffler 	ath_start(ifp);			/* restart xmit */
1156c42a7b7eSSam Leffler 	return 0;
11575591b213SSam Leffler }
11585591b213SSam Leffler 
115968e8e04eSSam Leffler static int
116068e8e04eSSam Leffler ath_ff_always(struct ath_txq *txq, struct ath_buf *bf)
116168e8e04eSSam Leffler {
116268e8e04eSSam Leffler 	return 0;
116368e8e04eSSam Leffler }
116468e8e04eSSam Leffler 
116568e8e04eSSam Leffler #if 0
116668e8e04eSSam Leffler static int
116768e8e04eSSam Leffler ath_ff_ageflushtestdone(struct ath_txq *txq, struct ath_buf *bf)
116868e8e04eSSam Leffler {
116968e8e04eSSam Leffler 	return (txq->axq_curage - bf->bf_age) < ATH_FF_STAGEMAX;
117068e8e04eSSam Leffler }
117168e8e04eSSam Leffler #endif
117268e8e04eSSam Leffler 
117368e8e04eSSam Leffler /*
117468e8e04eSSam Leffler  * Flush FF staging queue.
117568e8e04eSSam Leffler  */
117668e8e04eSSam Leffler static void
117768e8e04eSSam Leffler ath_ff_stageq_flush(struct ath_softc *sc, struct ath_txq *txq,
117868e8e04eSSam Leffler 	int (*ath_ff_flushdonetest)(struct ath_txq *txq, struct ath_buf *bf))
117968e8e04eSSam Leffler {
118068e8e04eSSam Leffler 	struct ath_buf *bf;
118168e8e04eSSam Leffler 	struct ieee80211_node *ni;
118268e8e04eSSam Leffler 	int pktlen, pri;
118368e8e04eSSam Leffler 
118468e8e04eSSam Leffler 	for (;;) {
118568e8e04eSSam Leffler 		ATH_TXQ_LOCK(txq);
118668e8e04eSSam Leffler 		/*
118768e8e04eSSam Leffler 		 * Go from the back (oldest) to front so we can
118868e8e04eSSam Leffler 		 * stop early based on the age of the entry.
118968e8e04eSSam Leffler 		 */
119068e8e04eSSam Leffler 		bf = TAILQ_LAST(&txq->axq_stageq, axq_headtype);
119168e8e04eSSam Leffler 		if (bf == NULL || ath_ff_flushdonetest(txq, bf)) {
119268e8e04eSSam Leffler 			ATH_TXQ_UNLOCK(txq);
119368e8e04eSSam Leffler 			break;
119468e8e04eSSam Leffler 		}
119568e8e04eSSam Leffler 
119668e8e04eSSam Leffler 		ni = bf->bf_node;
119768e8e04eSSam Leffler 		pri = M_WME_GETAC(bf->bf_m);
119868e8e04eSSam Leffler 		KASSERT(ATH_NODE(ni)->an_ff_buf[pri],
119968e8e04eSSam Leffler 			("no bf on staging queue %p", bf));
120068e8e04eSSam Leffler 		ATH_NODE(ni)->an_ff_buf[pri] = NULL;
120168e8e04eSSam Leffler 		TAILQ_REMOVE(&txq->axq_stageq, bf, bf_stagelist);
120268e8e04eSSam Leffler 
120368e8e04eSSam Leffler 		ATH_TXQ_UNLOCK(txq);
120468e8e04eSSam Leffler 
120568e8e04eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_FF, "%s: flush frame, age %u\n",
120668e8e04eSSam Leffler 			__func__, bf->bf_age);
120768e8e04eSSam Leffler 
120868e8e04eSSam Leffler 		sc->sc_stats.ast_ff_flush++;
120968e8e04eSSam Leffler 
121068e8e04eSSam Leffler 		/* encap and xmit */
121168e8e04eSSam Leffler 		bf->bf_m = ieee80211_encap(&sc->sc_ic, bf->bf_m, ni);
121268e8e04eSSam Leffler 		if (bf->bf_m == NULL) {
121368e8e04eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_XMIT | ATH_DEBUG_FF,
121468e8e04eSSam Leffler 				"%s: discard, encapsulation failure\n",
121568e8e04eSSam Leffler 				__func__);
121668e8e04eSSam Leffler 			sc->sc_stats.ast_tx_encap++;
121768e8e04eSSam Leffler 			goto bad;
121868e8e04eSSam Leffler 		}
121968e8e04eSSam Leffler 		pktlen = bf->bf_m->m_pkthdr.len; /* NB: don't reference below */
122068e8e04eSSam Leffler 		if (ath_tx_start(sc, ni, bf, bf->bf_m) == 0) {
122168e8e04eSSam Leffler #if 0 /*XXX*/
122268e8e04eSSam Leffler 			ifp->if_opackets++;
122368e8e04eSSam Leffler #endif
122468e8e04eSSam Leffler 			continue;
122568e8e04eSSam Leffler 		}
122668e8e04eSSam Leffler 	bad:
122768e8e04eSSam Leffler 		if (ni != NULL)
122868e8e04eSSam Leffler 			ieee80211_free_node(ni);
122968e8e04eSSam Leffler 		bf->bf_node = NULL;
123068e8e04eSSam Leffler 		if (bf->bf_m != NULL) {
123168e8e04eSSam Leffler 			m_freem(bf->bf_m);
123268e8e04eSSam Leffler 			bf->bf_m = NULL;
123368e8e04eSSam Leffler 		}
123468e8e04eSSam Leffler 
123568e8e04eSSam Leffler 		ATH_TXBUF_LOCK(sc);
123668e8e04eSSam Leffler 		STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
123768e8e04eSSam Leffler 		ATH_TXBUF_UNLOCK(sc);
123868e8e04eSSam Leffler 	}
123968e8e04eSSam Leffler }
124068e8e04eSSam Leffler 
124168e8e04eSSam Leffler static __inline u_int32_t
124268e8e04eSSam Leffler ath_ff_approx_txtime(struct ath_softc *sc, struct ath_node *an, struct mbuf *m)
124368e8e04eSSam Leffler {
124468e8e04eSSam Leffler 	u_int32_t framelen;
124568e8e04eSSam Leffler 	struct ath_buf *bf;
124668e8e04eSSam Leffler 
124768e8e04eSSam Leffler 	/*
124868e8e04eSSam Leffler 	 * Approximate the frame length to be transmitted. A swag to add
124968e8e04eSSam Leffler 	 * the following maximal values to the skb payload:
125068e8e04eSSam Leffler 	 *   - 32: 802.11 encap + CRC
125168e8e04eSSam Leffler 	 *   - 24: encryption overhead (if wep bit)
125268e8e04eSSam Leffler 	 *   - 4 + 6: fast-frame header and padding
125368e8e04eSSam Leffler 	 *   - 16: 2 LLC FF tunnel headers
125468e8e04eSSam Leffler 	 *   - 14: 1 802.3 FF tunnel header (skb already accounts for 2nd)
125568e8e04eSSam Leffler 	 */
125668e8e04eSSam Leffler 	framelen = m->m_pkthdr.len + 32 + 4 + 6 + 16 + 14;
125768e8e04eSSam Leffler 	if (sc->sc_ic.ic_flags & IEEE80211_F_PRIVACY)
125868e8e04eSSam Leffler 		framelen += 24;
125968e8e04eSSam Leffler 	bf = an->an_ff_buf[M_WME_GETAC(m)];
126068e8e04eSSam Leffler 	if (bf != NULL)
126168e8e04eSSam Leffler 		framelen += bf->bf_m->m_pkthdr.len;
126268e8e04eSSam Leffler 	return ath_hal_computetxtime(sc->sc_ah, sc->sc_currates, framelen,
126368e8e04eSSam Leffler 			sc->sc_lastdatarix, AH_FALSE);
126468e8e04eSSam Leffler }
126568e8e04eSSam Leffler 
126668e8e04eSSam Leffler /*
126768e8e04eSSam Leffler  * Determine if a data frame may be aggregated via ff tunnelling.
126868e8e04eSSam Leffler  * Note the caller is responsible for checking if the destination
126968e8e04eSSam Leffler  * supports fast frames.
127068e8e04eSSam Leffler  *
127168e8e04eSSam Leffler  *  NB: allowing EAPOL frames to be aggregated with other unicast traffic.
127268e8e04eSSam Leffler  *      Do 802.1x EAPOL frames proceed in the clear? Then they couldn't
127368e8e04eSSam Leffler  *      be aggregated with other types of frames when encryption is on?
127468e8e04eSSam Leffler  *
127568e8e04eSSam Leffler  *  NB: assumes lock on an_ff_buf effectively held by txq lock mechanism.
127668e8e04eSSam Leffler  */
127768e8e04eSSam Leffler static __inline int
127868e8e04eSSam Leffler ath_ff_can_aggregate(struct ath_softc *sc,
127968e8e04eSSam Leffler 	struct ath_node *an, struct mbuf *m, int *flushq)
128068e8e04eSSam Leffler {
128168e8e04eSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
128268e8e04eSSam Leffler 	struct ath_txq *txq;
128368e8e04eSSam Leffler 	u_int32_t txoplimit;
128468e8e04eSSam Leffler 	u_int pri;
128568e8e04eSSam Leffler 
128668e8e04eSSam Leffler 	*flushq = 0;
128768e8e04eSSam Leffler 
128868e8e04eSSam Leffler 	/*
128968e8e04eSSam Leffler 	 * If there is no frame to combine with and the txq has
129068e8e04eSSam Leffler 	 * fewer frames than the minimum required; then do not
129168e8e04eSSam Leffler 	 * attempt to aggregate this frame.
129268e8e04eSSam Leffler 	 */
129368e8e04eSSam Leffler 	pri = M_WME_GETAC(m);
129468e8e04eSSam Leffler 	txq = sc->sc_ac2q[pri];
129568e8e04eSSam Leffler 	if (an->an_ff_buf[pri] == NULL && txq->axq_depth < sc->sc_fftxqmin)
129668e8e04eSSam Leffler 		return 0;
129768e8e04eSSam Leffler 	/*
129868e8e04eSSam Leffler 	 * When not in station mode never aggregate a multicast
129968e8e04eSSam Leffler 	 * frame; this insures, for example, that a combined frame
130068e8e04eSSam Leffler 	 * does not require multiple encryption keys when using
130168e8e04eSSam Leffler 	 * 802.1x/WPA.
130268e8e04eSSam Leffler 	 */
130368e8e04eSSam Leffler 	if (ic->ic_opmode != IEEE80211_M_STA &&
130468e8e04eSSam Leffler 	    ETHER_IS_MULTICAST(mtod(m, struct ether_header *)->ether_dhost))
130568e8e04eSSam Leffler 		return 0;
130668e8e04eSSam Leffler 	/*
130768e8e04eSSam Leffler 	 * Consult the max bursting interval to insure a combined
130868e8e04eSSam Leffler 	 * frame fits within the TxOp window.
130968e8e04eSSam Leffler 	 */
131068e8e04eSSam Leffler 	txoplimit = IEEE80211_TXOP_TO_US(
131168e8e04eSSam Leffler 		ic->ic_wme.wme_chanParams.cap_wmeParams[pri].wmep_txopLimit);
131268e8e04eSSam Leffler 	if (txoplimit != 0 && ath_ff_approx_txtime(sc, an, m) > txoplimit) {
131368e8e04eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT | ATH_DEBUG_FF,
131468e8e04eSSam Leffler 			"%s: FF TxOp violation\n", __func__);
131568e8e04eSSam Leffler 		if (an->an_ff_buf[pri] != NULL)
131668e8e04eSSam Leffler 			*flushq = 1;
131768e8e04eSSam Leffler 		return 0;
131868e8e04eSSam Leffler 	}
131968e8e04eSSam Leffler 	return 1;		/* try to aggregate */
132068e8e04eSSam Leffler }
132168e8e04eSSam Leffler 
132268e8e04eSSam Leffler /*
132368e8e04eSSam Leffler  * Check if the supplied frame can be partnered with an existing
132468e8e04eSSam Leffler  * or pending frame.  Return a reference to any frame that should be
132568e8e04eSSam Leffler  * sent on return; otherwise return NULL.
132668e8e04eSSam Leffler  */
132768e8e04eSSam Leffler static struct mbuf *
132868e8e04eSSam Leffler ath_ff_check(struct ath_softc *sc, struct ath_txq *txq,
132968e8e04eSSam Leffler 	struct ath_buf *bf, struct mbuf *m, struct ieee80211_node *ni)
133068e8e04eSSam Leffler {
133168e8e04eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
133268e8e04eSSam Leffler 	struct ath_node *an = ATH_NODE(ni);
133368e8e04eSSam Leffler 	struct ath_buf *bfstaged;
133468e8e04eSSam Leffler 	int ff_flush, pri;
133568e8e04eSSam Leffler 
133668e8e04eSSam Leffler 	/*
133768e8e04eSSam Leffler 	 * Check if the supplied frame can be aggregated.
133868e8e04eSSam Leffler 	 *
133968e8e04eSSam Leffler 	 * NB: we use the txq lock to protect references to
134068e8e04eSSam Leffler 	 *     an->an_ff_txbuf in ath_ff_can_aggregate().
134168e8e04eSSam Leffler 	 */
134268e8e04eSSam Leffler 	ATH_TXQ_LOCK(txq);
134368e8e04eSSam Leffler 	pri = M_WME_GETAC(m);
134468e8e04eSSam Leffler 	if (ath_ff_can_aggregate(sc, an, m, &ff_flush)) {
134568e8e04eSSam Leffler 		struct ath_buf *bfstaged = an->an_ff_buf[pri];
134668e8e04eSSam Leffler 		if (bfstaged != NULL) {
134768e8e04eSSam Leffler 			/*
134868e8e04eSSam Leffler 			 * A frame is available for partnering; remove
134968e8e04eSSam Leffler 			 * it, chain it to this one, and encapsulate.
135068e8e04eSSam Leffler 			 */
135168e8e04eSSam Leffler 			an->an_ff_buf[pri] = NULL;
135268e8e04eSSam Leffler 			TAILQ_REMOVE(&txq->axq_stageq, bfstaged, bf_stagelist);
135368e8e04eSSam Leffler 			ATH_TXQ_UNLOCK(txq);
135468e8e04eSSam Leffler 
135568e8e04eSSam Leffler 			/*
135668e8e04eSSam Leffler 			 * Chain mbufs and add FF magic.
135768e8e04eSSam Leffler 			 */
135868e8e04eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_FF,
135968e8e04eSSam Leffler 				"[%s] aggregate fast-frame, age %u\n",
136068e8e04eSSam Leffler 				ether_sprintf(ni->ni_macaddr), txq->axq_curage);
136168e8e04eSSam Leffler 			m->m_nextpkt = NULL;
136268e8e04eSSam Leffler 			bfstaged->bf_m->m_nextpkt = m;
136368e8e04eSSam Leffler 			m = bfstaged->bf_m;
136468e8e04eSSam Leffler 			bfstaged->bf_m = NULL;
136568e8e04eSSam Leffler 			m->m_flags |= M_FF;
136668e8e04eSSam Leffler 			/*
136768e8e04eSSam Leffler 			 * Release the node reference held while
136868e8e04eSSam Leffler 			 * the packet sat on an_ff_buf[]
136968e8e04eSSam Leffler 			 */
137068e8e04eSSam Leffler 			bfstaged->bf_node = NULL;
137168e8e04eSSam Leffler 			ieee80211_free_node(ni);
137268e8e04eSSam Leffler 
137368e8e04eSSam Leffler 			/*
137468e8e04eSSam Leffler 			 * Return bfstaged to the free list.
137568e8e04eSSam Leffler 			 */
137668e8e04eSSam Leffler 			ATH_TXBUF_LOCK(sc);
137768e8e04eSSam Leffler 			STAILQ_INSERT_TAIL(&sc->sc_txbuf, bfstaged, bf_list);
137868e8e04eSSam Leffler 			ATH_TXBUF_UNLOCK(sc);
137968e8e04eSSam Leffler 
138068e8e04eSSam Leffler 			return m;		/* ready to go */
138168e8e04eSSam Leffler 		} else {
138268e8e04eSSam Leffler 			/*
138368e8e04eSSam Leffler 			 * No frame available, queue this frame to wait
138468e8e04eSSam Leffler 			 * for a partner.  Note that we hold the buffer
138568e8e04eSSam Leffler 			 * and a reference to the node; we need the
138668e8e04eSSam Leffler 			 * buffer in particular so we're certain we
138768e8e04eSSam Leffler 			 * can flush the frame at a later time.
138868e8e04eSSam Leffler 			 */
138968e8e04eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_FF,
139068e8e04eSSam Leffler 				"[%s] stage fast-frame, age %u\n",
139168e8e04eSSam Leffler 				ether_sprintf(ni->ni_macaddr), txq->axq_curage);
139268e8e04eSSam Leffler 
139368e8e04eSSam Leffler 			bf->bf_m = m;
139468e8e04eSSam Leffler 			bf->bf_node = ni;	/* NB: held reference */
139568e8e04eSSam Leffler 			bf->bf_age = txq->axq_curage;
139668e8e04eSSam Leffler 			an->an_ff_buf[pri] = bf;
139768e8e04eSSam Leffler 			TAILQ_INSERT_HEAD(&txq->axq_stageq, bf, bf_stagelist);
139868e8e04eSSam Leffler 			ATH_TXQ_UNLOCK(txq);
139968e8e04eSSam Leffler 
140068e8e04eSSam Leffler 			return NULL;		/* consumed */
140168e8e04eSSam Leffler 		}
140268e8e04eSSam Leffler 	}
140368e8e04eSSam Leffler 	/*
140468e8e04eSSam Leffler 	 * Frame could not be aggregated, it needs to be returned
140568e8e04eSSam Leffler 	 * to the caller for immediate transmission.  In addition
140668e8e04eSSam Leffler 	 * we check if we should first flush a frame from the
140768e8e04eSSam Leffler 	 * staging queue before sending this one.
140868e8e04eSSam Leffler 	 *
140968e8e04eSSam Leffler 	 * NB: ath_ff_can_aggregate only marks ff_flush if a frame
141068e8e04eSSam Leffler 	 *     is present to flush.
141168e8e04eSSam Leffler 	 */
141268e8e04eSSam Leffler 	if (ff_flush) {
141368e8e04eSSam Leffler 		int pktlen;
141468e8e04eSSam Leffler 
141568e8e04eSSam Leffler 		bfstaged = an->an_ff_buf[pri];
141668e8e04eSSam Leffler 		an->an_ff_buf[pri] = NULL;
141768e8e04eSSam Leffler 		TAILQ_REMOVE(&txq->axq_stageq, bfstaged, bf_stagelist);
141868e8e04eSSam Leffler 		ATH_TXQ_UNLOCK(txq);
141968e8e04eSSam Leffler 
142068e8e04eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_FF, "[%s] flush staged frame\n",
142168e8e04eSSam Leffler 			ether_sprintf(an->an_node.ni_macaddr));
142268e8e04eSSam Leffler 
142368e8e04eSSam Leffler 		/* encap and xmit */
142468e8e04eSSam Leffler 		bfstaged->bf_m = ieee80211_encap(ic, bfstaged->bf_m, ni);
142568e8e04eSSam Leffler 		if (bfstaged->bf_m == NULL) {
142668e8e04eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_XMIT | ATH_DEBUG_FF,
142768e8e04eSSam Leffler 				"%s: discard, encap failure\n", __func__);
142868e8e04eSSam Leffler 			sc->sc_stats.ast_tx_encap++;
142968e8e04eSSam Leffler 			goto ff_flushbad;
143068e8e04eSSam Leffler 		}
143168e8e04eSSam Leffler 		pktlen = bfstaged->bf_m->m_pkthdr.len;
143268e8e04eSSam Leffler 		if (ath_tx_start(sc, ni, bfstaged, bfstaged->bf_m)) {
143368e8e04eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_XMIT,
143468e8e04eSSam Leffler 				"%s: discard, xmit failure\n", __func__);
143568e8e04eSSam Leffler 	ff_flushbad:
143668e8e04eSSam Leffler 			/*
143768e8e04eSSam Leffler 			 * Unable to transmit frame that was on the staging
143868e8e04eSSam Leffler 			 * queue.  Reclaim the node reference and other
143968e8e04eSSam Leffler 			 * resources.
144068e8e04eSSam Leffler 			 */
144168e8e04eSSam Leffler 			if (ni != NULL)
144268e8e04eSSam Leffler 				ieee80211_free_node(ni);
144368e8e04eSSam Leffler 			bfstaged->bf_node = NULL;
144468e8e04eSSam Leffler 			if (bfstaged->bf_m != NULL) {
144568e8e04eSSam Leffler 				m_freem(bfstaged->bf_m);
144668e8e04eSSam Leffler 				bfstaged->bf_m = NULL;
144768e8e04eSSam Leffler 			}
144868e8e04eSSam Leffler 
144968e8e04eSSam Leffler 			ATH_TXBUF_LOCK(sc);
145068e8e04eSSam Leffler 			STAILQ_INSERT_TAIL(&sc->sc_txbuf, bfstaged, bf_list);
145168e8e04eSSam Leffler 			ATH_TXBUF_UNLOCK(sc);
145268e8e04eSSam Leffler 		} else {
145368e8e04eSSam Leffler #if 0
145468e8e04eSSam Leffler 			ifp->if_opackets++;
145568e8e04eSSam Leffler #endif
145668e8e04eSSam Leffler 		}
145768e8e04eSSam Leffler 	} else {
145868e8e04eSSam Leffler 		if (an->an_ff_buf[pri] != NULL) {
145968e8e04eSSam Leffler 			/*
146068e8e04eSSam Leffler 			 * XXX: out-of-order condition only occurs for AP
146168e8e04eSSam Leffler 			 * mode and multicast.  There may be no valid way
146268e8e04eSSam Leffler 			 * to get this condition.
146368e8e04eSSam Leffler 			 */
146468e8e04eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_FF, "[%s] out-of-order frame\n",
146568e8e04eSSam Leffler 				ether_sprintf(an->an_node.ni_macaddr));
146668e8e04eSSam Leffler 			/* XXX stat */
146768e8e04eSSam Leffler 		}
146868e8e04eSSam Leffler 		ATH_TXQ_UNLOCK(txq);
146968e8e04eSSam Leffler 	}
147068e8e04eSSam Leffler 	return m;
147168e8e04eSSam Leffler }
147268e8e04eSSam Leffler 
147368e8e04eSSam Leffler /*
147468e8e04eSSam Leffler  * Cleanup driver resources when we run out of buffers
147568e8e04eSSam Leffler  * while processing fragments; return the tx buffers
147668e8e04eSSam Leffler  * allocated and drop node references.
147768e8e04eSSam Leffler  */
147868e8e04eSSam Leffler static void
147968e8e04eSSam Leffler ath_txfrag_cleanup(struct ath_softc *sc,
148068e8e04eSSam Leffler 	ath_bufhead *frags, struct ieee80211_node *ni)
148168e8e04eSSam Leffler {
148268e8e04eSSam Leffler 	struct ath_buf *bf, *next;
148368e8e04eSSam Leffler 
148468e8e04eSSam Leffler 	ATH_TXBUF_LOCK_ASSERT(sc);
148568e8e04eSSam Leffler 
148668e8e04eSSam Leffler 	STAILQ_FOREACH_SAFE(bf, frags, bf_list, next) {
148768e8e04eSSam Leffler 		/* NB: bf assumed clean */
148868e8e04eSSam Leffler 		STAILQ_REMOVE_HEAD(frags, bf_list);
148968e8e04eSSam Leffler 		STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
149068e8e04eSSam Leffler 		ieee80211_node_decref(ni);
149168e8e04eSSam Leffler 	}
149268e8e04eSSam Leffler }
149368e8e04eSSam Leffler 
149468e8e04eSSam Leffler /*
149568e8e04eSSam Leffler  * Setup xmit of a fragmented frame.  Allocate a buffer
149668e8e04eSSam Leffler  * for each frag and bump the node reference count to
149768e8e04eSSam Leffler  * reflect the held reference to be setup by ath_tx_start.
149868e8e04eSSam Leffler  */
149968e8e04eSSam Leffler static int
150068e8e04eSSam Leffler ath_txfrag_setup(struct ath_softc *sc, ath_bufhead *frags,
150168e8e04eSSam Leffler 	struct mbuf *m0, struct ieee80211_node *ni)
150268e8e04eSSam Leffler {
150368e8e04eSSam Leffler 	struct mbuf *m;
150468e8e04eSSam Leffler 	struct ath_buf *bf;
150568e8e04eSSam Leffler 
150668e8e04eSSam Leffler 	ATH_TXBUF_LOCK(sc);
150768e8e04eSSam Leffler 	for (m = m0->m_nextpkt; m != NULL; m = m->m_nextpkt) {
150868e8e04eSSam Leffler 		bf = STAILQ_FIRST(&sc->sc_txbuf);
150968e8e04eSSam Leffler 		if (bf == NULL) {	/* out of buffers, cleanup */
151068e8e04eSSam Leffler 			ath_txfrag_cleanup(sc, frags, ni);
151168e8e04eSSam Leffler 			break;
151268e8e04eSSam Leffler 		}
151368e8e04eSSam Leffler 		STAILQ_REMOVE_HEAD(&sc->sc_txbuf, bf_list);
151468e8e04eSSam Leffler 		ieee80211_node_incref(ni);
151568e8e04eSSam Leffler 		STAILQ_INSERT_TAIL(frags, bf, bf_list);
151668e8e04eSSam Leffler 	}
151768e8e04eSSam Leffler 	ATH_TXBUF_UNLOCK(sc);
151868e8e04eSSam Leffler 
151968e8e04eSSam Leffler 	return !STAILQ_EMPTY(frags);
152068e8e04eSSam Leffler }
152168e8e04eSSam Leffler 
15225591b213SSam Leffler static void
15235591b213SSam Leffler ath_start(struct ifnet *ifp)
15245591b213SSam Leffler {
15255591b213SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
15265591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
15275591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
15285591b213SSam Leffler 	struct ieee80211_node *ni;
15295591b213SSam Leffler 	struct ath_buf *bf;
153068e8e04eSSam Leffler 	struct mbuf *m, *next;
15315591b213SSam Leffler 	struct ieee80211_frame *wh;
1532c42a7b7eSSam Leffler 	struct ether_header *eh;
153368e8e04eSSam Leffler 	struct ath_txq *txq;
153468e8e04eSSam Leffler 	ath_bufhead frags;
153568e8e04eSSam Leffler 	int pri;
15365591b213SSam Leffler 
153713f4c340SRobert Watson 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->sc_invalid)
15385591b213SSam Leffler 		return;
15395591b213SSam Leffler 	for (;;) {
15405591b213SSam Leffler 		/*
15415591b213SSam Leffler 		 * Grab a TX buffer and associated resources.
15425591b213SSam Leffler 		 */
1543f0b2a0beSSam Leffler 		ATH_TXBUF_LOCK(sc);
1544c42a7b7eSSam Leffler 		bf = STAILQ_FIRST(&sc->sc_txbuf);
1545ebecf802SSam Leffler 		if (bf != NULL)
1546c42a7b7eSSam Leffler 			STAILQ_REMOVE_HEAD(&sc->sc_txbuf, bf_list);
1547f0b2a0beSSam Leffler 		ATH_TXBUF_UNLOCK(sc);
15485591b213SSam Leffler 		if (bf == NULL) {
1549ebecf802SSam Leffler 			DPRINTF(sc, ATH_DEBUG_XMIT, "%s: out of xmit buffers\n",
1550ebecf802SSam Leffler 				__func__);
15515591b213SSam Leffler 			sc->sc_stats.ast_tx_qstop++;
155213f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
15535591b213SSam Leffler 			break;
15545591b213SSam Leffler 		}
15555591b213SSam Leffler 		/*
15565591b213SSam Leffler 		 * Poll the management queue for frames; they
15575591b213SSam Leffler 		 * have priority over normal data frames.
15585591b213SSam Leffler 		 */
15595591b213SSam Leffler 		IF_DEQUEUE(&ic->ic_mgtq, m);
15605591b213SSam Leffler 		if (m == NULL) {
15615591b213SSam Leffler 			/*
15625591b213SSam Leffler 			 * No data frames go out unless we're associated.
15635591b213SSam Leffler 			 */
15645591b213SSam Leffler 			if (ic->ic_state != IEEE80211_S_RUN) {
1565370572d9SSam Leffler 				DPRINTF(sc, ATH_DEBUG_XMIT,
1566370572d9SSam Leffler 				    "%s: discard data packet, state %s\n",
1567370572d9SSam Leffler 				    __func__,
1568370572d9SSam Leffler 				    ieee80211_state_name[ic->ic_state]);
15695591b213SSam Leffler 				sc->sc_stats.ast_tx_discard++;
1570f0b2a0beSSam Leffler 				ATH_TXBUF_LOCK(sc);
1571ebecf802SSam Leffler 				STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
1572f0b2a0beSSam Leffler 				ATH_TXBUF_UNLOCK(sc);
15735591b213SSam Leffler 				break;
15745591b213SSam Leffler 			}
1575154b8df2SMax Laier 			IFQ_DRV_DEQUEUE(&ifp->if_snd, m);	/* XXX: LOCK */
15765591b213SSam Leffler 			if (m == NULL) {
1577f0b2a0beSSam Leffler 				ATH_TXBUF_LOCK(sc);
1578ebecf802SSam Leffler 				STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
1579f0b2a0beSSam Leffler 				ATH_TXBUF_UNLOCK(sc);
15805591b213SSam Leffler 				break;
15815591b213SSam Leffler 			}
15822b9411e2SSam Leffler 			/*
15832b9411e2SSam Leffler 			 * Cancel any background scan.
15842b9411e2SSam Leffler 			 */
15852b9411e2SSam Leffler 			if (ic->ic_flags & IEEE80211_F_SCAN)
15862b9411e2SSam Leffler 				ieee80211_cancel_scan(ic);
15872b9411e2SSam Leffler 
158868e8e04eSSam Leffler 			STAILQ_INIT(&frags);
1589c42a7b7eSSam Leffler 			/*
1590c42a7b7eSSam Leffler 			 * Find the node for the destination so we can do
1591c42a7b7eSSam Leffler 			 * things like power save and fast frames aggregation.
1592c42a7b7eSSam Leffler 			 */
1593c42a7b7eSSam Leffler 			if (m->m_len < sizeof(struct ether_header) &&
1594c42a7b7eSSam Leffler 			   (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
1595c42a7b7eSSam Leffler 				ic->ic_stats.is_tx_nobuf++;	/* XXX */
1596c42a7b7eSSam Leffler 				ni = NULL;
1597c42a7b7eSSam Leffler 				goto bad;
1598c42a7b7eSSam Leffler 			}
1599c42a7b7eSSam Leffler 			eh = mtod(m, struct ether_header *);
1600c42a7b7eSSam Leffler 			ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1601c42a7b7eSSam Leffler 			if (ni == NULL) {
1602c42a7b7eSSam Leffler 				/* NB: ieee80211_find_txnode does stat+msg */
1603fe234894SSam Leffler 				m_freem(m);
1604c42a7b7eSSam Leffler 				goto bad;
1605c42a7b7eSSam Leffler 			}
1606c42a7b7eSSam Leffler 			if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
1607c42a7b7eSSam Leffler 			    (m->m_flags & M_PWR_SAV) == 0) {
1608c42a7b7eSSam Leffler 				/*
1609c42a7b7eSSam Leffler 				 * Station in power save mode; pass the frame
1610c42a7b7eSSam Leffler 				 * to the 802.11 layer and continue.  We'll get
1611c42a7b7eSSam Leffler 				 * the frame back when the time is right.
1612c42a7b7eSSam Leffler 				 */
161368e8e04eSSam Leffler 				ieee80211_pwrsave(ni, m);
1614c42a7b7eSSam Leffler 				goto reclaim;
1615c42a7b7eSSam Leffler 			}
1616c42a7b7eSSam Leffler 			/* calculate priority so we can find the tx queue */
1617c42a7b7eSSam Leffler 			if (ieee80211_classify(ic, m, ni)) {
1618c42a7b7eSSam Leffler 				DPRINTF(sc, ATH_DEBUG_XMIT,
1619c42a7b7eSSam Leffler 					"%s: discard, classification failure\n",
1620c42a7b7eSSam Leffler 					__func__);
1621fe234894SSam Leffler 				m_freem(m);
1622c42a7b7eSSam Leffler 				goto bad;
1623c42a7b7eSSam Leffler 			}
162468e8e04eSSam Leffler 			pri = M_WME_GETAC(m);
162568e8e04eSSam Leffler 			txq = sc->sc_ac2q[pri];
162668e8e04eSSam Leffler 			if (ni->ni_ath_flags & IEEE80211_NODE_FF) {
162768e8e04eSSam Leffler 				/*
162868e8e04eSSam Leffler 				 * Check queue length; if too deep drop this
162968e8e04eSSam Leffler 				 * frame (tail drop considered good).
163068e8e04eSSam Leffler 				 */
163168e8e04eSSam Leffler 				if (txq->axq_depth >= sc->sc_fftxqmax) {
163268e8e04eSSam Leffler 					DPRINTF(sc, ATH_DEBUG_FF,
163368e8e04eSSam Leffler 					    "[%s] tail drop on q %u depth %u\n",
163468e8e04eSSam Leffler 					    ether_sprintf(ni->ni_macaddr),
163568e8e04eSSam Leffler 					    txq->axq_qnum, txq->axq_depth);
163668e8e04eSSam Leffler 					sc->sc_stats.ast_tx_qfull++;
163768e8e04eSSam Leffler 					m_freem(m);
163868e8e04eSSam Leffler 					goto reclaim;
163968e8e04eSSam Leffler 				}
164068e8e04eSSam Leffler 				m = ath_ff_check(sc, txq, bf, m, ni);
164168e8e04eSSam Leffler 				if (m == NULL) {
164268e8e04eSSam Leffler 					/* NB: ni ref & bf held on stageq */
164368e8e04eSSam Leffler 					continue;
164468e8e04eSSam Leffler 				}
164568e8e04eSSam Leffler 			}
16465591b213SSam Leffler 			ifp->if_opackets++;
16475591b213SSam Leffler 			BPF_MTAP(ifp, m);
16485591b213SSam Leffler 			/*
16495591b213SSam Leffler 			 * Encapsulate the packet in prep for transmission.
16505591b213SSam Leffler 			 */
1651c42a7b7eSSam Leffler 			m = ieee80211_encap(ic, m, ni);
16525591b213SSam Leffler 			if (m == NULL) {
1653370572d9SSam Leffler 				DPRINTF(sc, ATH_DEBUG_XMIT,
1654c42a7b7eSSam Leffler 					"%s: encapsulation failure\n",
1655c42a7b7eSSam Leffler 					__func__);
16565591b213SSam Leffler 				sc->sc_stats.ast_tx_encap++;
16575591b213SSam Leffler 				goto bad;
16585591b213SSam Leffler 			}
165968e8e04eSSam Leffler 			/*
166068e8e04eSSam Leffler 			 * Check for fragmentation.  If this frame
166168e8e04eSSam Leffler 			 * has been broken up verify we have enough
166268e8e04eSSam Leffler 			 * buffers to send all the fragments so all
166368e8e04eSSam Leffler 			 * go out or none...
166468e8e04eSSam Leffler 			 */
166568e8e04eSSam Leffler 			if ((m->m_flags & M_FRAG) &&
166668e8e04eSSam Leffler 			    !ath_txfrag_setup(sc, &frags, m, ni)) {
166768e8e04eSSam Leffler 				DPRINTF(sc, ATH_DEBUG_XMIT,
166868e8e04eSSam Leffler 				    "%s: out of txfrag buffers\n", __func__);
166968e8e04eSSam Leffler 				ic->ic_stats.is_tx_nobuf++;	/* XXX */
167068e8e04eSSam Leffler 				ath_freetx(m);
167168e8e04eSSam Leffler 				goto bad;
167268e8e04eSSam Leffler 			}
16735591b213SSam Leffler 		} else {
16740a915fadSSam Leffler 			/*
16750a915fadSSam Leffler 			 * Hack!  The referenced node pointer is in the
16760a915fadSSam Leffler 			 * rcvif field of the packet header.  This is
16770a915fadSSam Leffler 			 * placed there by ieee80211_mgmt_output because
16780a915fadSSam Leffler 			 * we need to hold the reference with the frame
16790a915fadSSam Leffler 			 * and there's no other way (other than packet
16800a915fadSSam Leffler 			 * tags which we consider too expensive to use)
16810a915fadSSam Leffler 			 * to pass it along.
16820a915fadSSam Leffler 			 */
16830a915fadSSam Leffler 			ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
16840a915fadSSam Leffler 			m->m_pkthdr.rcvif = NULL;
16850a915fadSSam Leffler 
16865591b213SSam Leffler 			wh = mtod(m, struct ieee80211_frame *);
16875591b213SSam Leffler 			if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
16885591b213SSam Leffler 			    IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
16895591b213SSam Leffler 				/* fill time stamp */
16905591b213SSam Leffler 				u_int64_t tsf;
16915591b213SSam Leffler 				u_int32_t *tstamp;
16925591b213SSam Leffler 
16935591b213SSam Leffler 				tsf = ath_hal_gettsf64(ah);
16945591b213SSam Leffler 				/* XXX: adjust 100us delay to xmit */
16955591b213SSam Leffler 				tsf += 100;
16965591b213SSam Leffler 				tstamp = (u_int32_t *)&wh[1];
16975591b213SSam Leffler 				tstamp[0] = htole32(tsf & 0xffffffff);
16985591b213SSam Leffler 				tstamp[1] = htole32(tsf >> 32);
16995591b213SSam Leffler 			}
17005591b213SSam Leffler 			sc->sc_stats.ast_tx_mgmt++;
17015591b213SSam Leffler 		}
170273454c73SSam Leffler 
170368e8e04eSSam Leffler 	nextfrag:
170468e8e04eSSam Leffler 		/*
170568e8e04eSSam Leffler 		 * Pass the frame to the h/w for transmission.
170668e8e04eSSam Leffler 		 * Fragmented frames have each frag chained together
170768e8e04eSSam Leffler 		 * with m_nextpkt.  We know there are sufficient ath_buf's
170868e8e04eSSam Leffler 		 * to send all the frags because of work done by
170968e8e04eSSam Leffler 		 * ath_txfrag_setup.  We leave m_nextpkt set while
171068e8e04eSSam Leffler 		 * calling ath_tx_start so it can use it to extend the
171168e8e04eSSam Leffler 		 * the tx duration to cover the subsequent frag and
171268e8e04eSSam Leffler 		 * so it can reclaim all the mbufs in case of an error;
171368e8e04eSSam Leffler 		 * ath_tx_start clears m_nextpkt once it commits to
171468e8e04eSSam Leffler 		 * handing the frame to the hardware.
171568e8e04eSSam Leffler 		 */
171668e8e04eSSam Leffler 		next = m->m_nextpkt;
17175591b213SSam Leffler 		if (ath_tx_start(sc, ni, bf, m)) {
17185591b213SSam Leffler 	bad:
17195591b213SSam Leffler 			ifp->if_oerrors++;
1720c42a7b7eSSam Leffler 	reclaim:
172168e8e04eSSam Leffler 			bf->bf_m = NULL;
172268e8e04eSSam Leffler 			bf->bf_node = NULL;
1723c42a7b7eSSam Leffler 			ATH_TXBUF_LOCK(sc);
1724ebecf802SSam Leffler 			STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
172568e8e04eSSam Leffler 			ath_txfrag_cleanup(sc, &frags, ni);
1726c42a7b7eSSam Leffler 			ATH_TXBUF_UNLOCK(sc);
1727c42a7b7eSSam Leffler 			if (ni != NULL)
1728c42a7b7eSSam Leffler 				ieee80211_free_node(ni);
17295591b213SSam Leffler 			continue;
17305591b213SSam Leffler 		}
173168e8e04eSSam Leffler 		if (next != NULL) {
173268e8e04eSSam Leffler 			/*
173368e8e04eSSam Leffler 			 * Beware of state changing between frags.
173468e8e04eSSam Leffler 			 * XXX check sta power-save state?
173568e8e04eSSam Leffler 			 */
173668e8e04eSSam Leffler 			if (ic->ic_state != IEEE80211_S_RUN) {
173768e8e04eSSam Leffler 				DPRINTF(sc, ATH_DEBUG_XMIT,
173868e8e04eSSam Leffler 				    "%s: flush fragmented packet, state %s\n",
173968e8e04eSSam Leffler 				    __func__,
174068e8e04eSSam Leffler 				    ieee80211_state_name[ic->ic_state]);
174168e8e04eSSam Leffler 				ath_freetx(next);
174268e8e04eSSam Leffler 				goto reclaim;
174368e8e04eSSam Leffler 			}
174468e8e04eSSam Leffler 			m = next;
174568e8e04eSSam Leffler 			bf = STAILQ_FIRST(&frags);
174668e8e04eSSam Leffler 			KASSERT(bf != NULL, ("no buf for txfrag"));
174768e8e04eSSam Leffler 			STAILQ_REMOVE_HEAD(&frags, bf_list);
174868e8e04eSSam Leffler 			goto nextfrag;
174968e8e04eSSam Leffler 		}
17505591b213SSam Leffler 
175168e8e04eSSam Leffler 		ifp->if_timer = 5;
17522b9411e2SSam Leffler 		ic->ic_lastdata = ticks;
175368e8e04eSSam Leffler #if 0
175468e8e04eSSam Leffler 		/*
175568e8e04eSSam Leffler 		 * Flush stale frames from the fast-frame staging queue.
175668e8e04eSSam Leffler 		 */
175768e8e04eSSam Leffler 		if (ic->ic_opmode != IEEE80211_M_STA)
175868e8e04eSSam Leffler 			ath_ff_stageq_flush(sc, txq, ath_ff_ageflushtestdone);
175968e8e04eSSam Leffler #endif
17605591b213SSam Leffler 	}
17615591b213SSam Leffler }
17625591b213SSam Leffler 
17635591b213SSam Leffler static int
17645591b213SSam Leffler ath_media_change(struct ifnet *ifp)
17655591b213SSam Leffler {
1766c42a7b7eSSam Leffler #define	IS_UP(ifp) \
176713f4c340SRobert Watson 	((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
17685591b213SSam Leffler 	int error;
17695591b213SSam Leffler 
17705591b213SSam Leffler 	error = ieee80211_media_change(ifp);
17715591b213SSam Leffler 	if (error == ENETRESET) {
17727a04dc27SSam Leffler 		struct ath_softc *sc = ifp->if_softc;
17737a04dc27SSam Leffler 		struct ieee80211com *ic = &sc->sc_ic;
17747a04dc27SSam Leffler 
17757a04dc27SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_AHDEMO) {
17767a04dc27SSam Leffler 			/*
17777a04dc27SSam Leffler 			 * Adhoc demo mode is just ibss mode w/o beacons
17787a04dc27SSam Leffler 			 * (mostly).  The hal knows nothing about it;
17797a04dc27SSam Leffler 			 * tell it we're operating in ibss mode.
17807a04dc27SSam Leffler 			 */
17817a04dc27SSam Leffler 			sc->sc_opmode = HAL_M_IBSS;
17827a04dc27SSam Leffler 		} else
17837a04dc27SSam Leffler 			sc->sc_opmode = ic->ic_opmode;
1784c42a7b7eSSam Leffler 		if (IS_UP(ifp))
178568e8e04eSSam Leffler 			ath_init(sc);		/* XXX lose error */
17865591b213SSam Leffler 		error = 0;
17875591b213SSam Leffler 	}
17885591b213SSam Leffler 	return error;
1789c42a7b7eSSam Leffler #undef IS_UP
17905591b213SSam Leffler }
17915591b213SSam Leffler 
1792a585a9a1SSam Leffler #ifdef ATH_DEBUG
1793c42a7b7eSSam Leffler static void
17945901d2d3SSam Leffler ath_keyprint(struct ath_softc *sc, const char *tag, u_int ix,
1795c42a7b7eSSam Leffler 	const HAL_KEYVAL *hk, const u_int8_t mac[IEEE80211_ADDR_LEN])
17965591b213SSam Leffler {
1797c42a7b7eSSam Leffler 	static const char *ciphers[] = {
1798c42a7b7eSSam Leffler 		"WEP",
1799c42a7b7eSSam Leffler 		"AES-OCB",
1800c42a7b7eSSam Leffler 		"AES-CCM",
1801c42a7b7eSSam Leffler 		"CKIP",
1802c42a7b7eSSam Leffler 		"TKIP",
1803c42a7b7eSSam Leffler 		"CLR",
1804c42a7b7eSSam Leffler 	};
1805c42a7b7eSSam Leffler 	int i, n;
18065591b213SSam Leffler 
1807c42a7b7eSSam Leffler 	printf("%s: [%02u] %-7s ", tag, ix, ciphers[hk->kv_type]);
1808c42a7b7eSSam Leffler 	for (i = 0, n = hk->kv_len; i < n; i++)
1809c42a7b7eSSam Leffler 		printf("%02x", hk->kv_val[i]);
1810c42a7b7eSSam Leffler 	printf(" mac %s", ether_sprintf(mac));
1811c42a7b7eSSam Leffler 	if (hk->kv_type == HAL_CIPHER_TKIP) {
18125901d2d3SSam Leffler 		printf(" %s ", sc->sc_splitmic ? "mic" : "rxmic");
1813c42a7b7eSSam Leffler 		for (i = 0; i < sizeof(hk->kv_mic); i++)
1814c42a7b7eSSam Leffler 			printf("%02x", hk->kv_mic[i]);
18155901d2d3SSam Leffler #if HAL_ABI_VERSION > 0x06052200
18165901d2d3SSam Leffler 		if (!sc->sc_splitmic) {
18175901d2d3SSam Leffler 			printf(" txmic ");
18185901d2d3SSam Leffler 			for (i = 0; i < sizeof(hk->kv_txmic); i++)
18195901d2d3SSam Leffler 				printf("%02x", hk->kv_txmic[i]);
18205901d2d3SSam Leffler 		}
18215901d2d3SSam Leffler #endif
18222075afbaSSam Leffler 	}
1823c42a7b7eSSam Leffler 	printf("\n");
1824c42a7b7eSSam Leffler }
1825c42a7b7eSSam Leffler #endif
1826c42a7b7eSSam Leffler 
18275591b213SSam Leffler /*
1828c42a7b7eSSam Leffler  * Set a TKIP key into the hardware.  This handles the
1829c42a7b7eSSam Leffler  * potential distribution of key state to multiple key
1830c42a7b7eSSam Leffler  * cache slots for TKIP.
18315591b213SSam Leffler  */
1832c42a7b7eSSam Leffler static int
1833c42a7b7eSSam Leffler ath_keyset_tkip(struct ath_softc *sc, const struct ieee80211_key *k,
1834c42a7b7eSSam Leffler 	HAL_KEYVAL *hk, const u_int8_t mac[IEEE80211_ADDR_LEN])
1835c42a7b7eSSam Leffler {
1836c42a7b7eSSam Leffler #define	IEEE80211_KEY_XR	(IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV)
1837c42a7b7eSSam Leffler 	static const u_int8_t zerobssid[IEEE80211_ADDR_LEN];
18388cec0ab9SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
18398cec0ab9SSam Leffler 
1840c42a7b7eSSam Leffler 	KASSERT(k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP,
1841c42a7b7eSSam Leffler 		("got a non-TKIP key, cipher %u", k->wk_cipher->ic_cipher));
1842c42a7b7eSSam Leffler 	if ((k->wk_flags & IEEE80211_KEY_XR) == IEEE80211_KEY_XR) {
18435901d2d3SSam Leffler 		if (sc->sc_splitmic) {
1844c42a7b7eSSam Leffler 			/*
1845c1225b52SSam Leffler 			 * TX key goes at first index, RX key at the rx index.
1846c42a7b7eSSam Leffler 			 * The hal handles the MIC keys at index+64.
1847c42a7b7eSSam Leffler 			 */
1848c42a7b7eSSam Leffler 			memcpy(hk->kv_mic, k->wk_txmic, sizeof(hk->kv_mic));
1849c42a7b7eSSam Leffler 			KEYPRINTF(sc, k->wk_keyix, hk, zerobssid);
1850c42a7b7eSSam Leffler 			if (!ath_hal_keyset(ah, k->wk_keyix, hk, zerobssid))
1851c42a7b7eSSam Leffler 				return 0;
1852c42a7b7eSSam Leffler 
1853c42a7b7eSSam Leffler 			memcpy(hk->kv_mic, k->wk_rxmic, sizeof(hk->kv_mic));
1854c42a7b7eSSam Leffler 			KEYPRINTF(sc, k->wk_keyix+32, hk, mac);
1855c42a7b7eSSam Leffler 			/* XXX delete tx key on failure? */
1856c42a7b7eSSam Leffler 			return ath_hal_keyset(ah, k->wk_keyix+32, hk, mac);
18575901d2d3SSam Leffler 		} else {
18585901d2d3SSam Leffler 			/*
18595901d2d3SSam Leffler 			 * Room for both TX+RX MIC keys in one key cache
18605901d2d3SSam Leffler 			 * slot, just set key at the first index; the hal
18615901d2d3SSam Leffler 			 * will handle the reset.
18625901d2d3SSam Leffler 			 */
18635901d2d3SSam Leffler 			memcpy(hk->kv_mic, k->wk_rxmic, sizeof(hk->kv_mic));
18645901d2d3SSam Leffler #if HAL_ABI_VERSION > 0x06052200
18655901d2d3SSam Leffler 			memcpy(hk->kv_txmic, k->wk_txmic, sizeof(hk->kv_txmic));
18665901d2d3SSam Leffler #endif
18675901d2d3SSam Leffler 			KEYPRINTF(sc, k->wk_keyix, hk, mac);
18685901d2d3SSam Leffler 			return ath_hal_keyset(ah, k->wk_keyix, hk, mac);
18695901d2d3SSam Leffler 		}
1870c42a7b7eSSam Leffler 	} else if (k->wk_flags & IEEE80211_KEY_XR) {
1871c42a7b7eSSam Leffler 		/*
1872c42a7b7eSSam Leffler 		 * TX/RX key goes at first index.
1873c42a7b7eSSam Leffler 		 * The hal handles the MIC keys are index+64.
1874c42a7b7eSSam Leffler 		 */
1875c42a7b7eSSam Leffler 		memcpy(hk->kv_mic, k->wk_flags & IEEE80211_KEY_XMIT ?
1876c42a7b7eSSam Leffler 			k->wk_txmic : k->wk_rxmic, sizeof(hk->kv_mic));
1877e8fd88a3SSam Leffler 		KEYPRINTF(sc, k->wk_keyix, hk, mac);
1878e8fd88a3SSam Leffler 		return ath_hal_keyset(ah, k->wk_keyix, hk, mac);
1879c42a7b7eSSam Leffler 	}
1880c42a7b7eSSam Leffler 	return 0;
1881c42a7b7eSSam Leffler #undef IEEE80211_KEY_XR
1882c42a7b7eSSam Leffler }
1883c42a7b7eSSam Leffler 
1884c42a7b7eSSam Leffler /*
1885c42a7b7eSSam Leffler  * Set a net80211 key into the hardware.  This handles the
1886c42a7b7eSSam Leffler  * potential distribution of key state to multiple key
1887c42a7b7eSSam Leffler  * cache slots for TKIP with hardware MIC support.
1888c42a7b7eSSam Leffler  */
1889c42a7b7eSSam Leffler static int
1890c42a7b7eSSam Leffler ath_keyset(struct ath_softc *sc, const struct ieee80211_key *k,
1891e8fd88a3SSam Leffler 	const u_int8_t mac0[IEEE80211_ADDR_LEN],
1892e8fd88a3SSam Leffler 	struct ieee80211_node *bss)
1893c42a7b7eSSam Leffler {
1894c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
1895c42a7b7eSSam Leffler 	static const u_int8_t ciphermap[] = {
1896c42a7b7eSSam Leffler 		HAL_CIPHER_WEP,		/* IEEE80211_CIPHER_WEP */
1897c42a7b7eSSam Leffler 		HAL_CIPHER_TKIP,	/* IEEE80211_CIPHER_TKIP */
1898c42a7b7eSSam Leffler 		HAL_CIPHER_AES_OCB,	/* IEEE80211_CIPHER_AES_OCB */
1899c42a7b7eSSam Leffler 		HAL_CIPHER_AES_CCM,	/* IEEE80211_CIPHER_AES_CCM */
1900c42a7b7eSSam Leffler 		(u_int8_t) -1,		/* 4 is not allocated */
1901c42a7b7eSSam Leffler 		HAL_CIPHER_CKIP,	/* IEEE80211_CIPHER_CKIP */
1902c42a7b7eSSam Leffler 		HAL_CIPHER_CLR,		/* IEEE80211_CIPHER_NONE */
1903c42a7b7eSSam Leffler 	};
1904c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1905c42a7b7eSSam Leffler 	const struct ieee80211_cipher *cip = k->wk_cipher;
1906e8fd88a3SSam Leffler 	u_int8_t gmac[IEEE80211_ADDR_LEN];
1907e8fd88a3SSam Leffler 	const u_int8_t *mac;
1908c42a7b7eSSam Leffler 	HAL_KEYVAL hk;
1909c42a7b7eSSam Leffler 
1910c42a7b7eSSam Leffler 	memset(&hk, 0, sizeof(hk));
1911c42a7b7eSSam Leffler 	/*
1912c42a7b7eSSam Leffler 	 * Software crypto uses a "clear key" so non-crypto
1913c42a7b7eSSam Leffler 	 * state kept in the key cache are maintained and
1914c42a7b7eSSam Leffler 	 * so that rx frames have an entry to match.
1915c42a7b7eSSam Leffler 	 */
1916c42a7b7eSSam Leffler 	if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) == 0) {
1917c42a7b7eSSam Leffler 		KASSERT(cip->ic_cipher < N(ciphermap),
1918c42a7b7eSSam Leffler 			("invalid cipher type %u", cip->ic_cipher));
1919c42a7b7eSSam Leffler 		hk.kv_type = ciphermap[cip->ic_cipher];
1920c42a7b7eSSam Leffler 		hk.kv_len = k->wk_keylen;
1921c42a7b7eSSam Leffler 		memcpy(hk.kv_val, k->wk_key, k->wk_keylen);
19228cec0ab9SSam Leffler 	} else
1923c42a7b7eSSam Leffler 		hk.kv_type = HAL_CIPHER_CLR;
1924c42a7b7eSSam Leffler 
1925e8fd88a3SSam Leffler 	if ((k->wk_flags & IEEE80211_KEY_GROUP) && sc->sc_mcastkey) {
1926e8fd88a3SSam Leffler 		/*
1927e8fd88a3SSam Leffler 		 * Group keys on hardware that supports multicast frame
1928e8fd88a3SSam Leffler 		 * key search use a mac that is the sender's address with
1929e8fd88a3SSam Leffler 		 * the high bit set instead of the app-specified address.
1930e8fd88a3SSam Leffler 		 */
1931e8fd88a3SSam Leffler 		IEEE80211_ADDR_COPY(gmac, bss->ni_macaddr);
1932e8fd88a3SSam Leffler 		gmac[0] |= 0x80;
1933e8fd88a3SSam Leffler 		mac = gmac;
1934e8fd88a3SSam Leffler 	} else
1935e8fd88a3SSam Leffler 		mac = mac0;
1936e8fd88a3SSam Leffler 
1937c42a7b7eSSam Leffler 	if (hk.kv_type == HAL_CIPHER_TKIP &&
19385901d2d3SSam Leffler 	    (k->wk_flags & IEEE80211_KEY_SWMIC) == 0) {
1939c42a7b7eSSam Leffler 		return ath_keyset_tkip(sc, k, &hk, mac);
1940c42a7b7eSSam Leffler 	} else {
1941c42a7b7eSSam Leffler 		KEYPRINTF(sc, k->wk_keyix, &hk, mac);
1942c42a7b7eSSam Leffler 		return ath_hal_keyset(ah, k->wk_keyix, &hk, mac);
19438cec0ab9SSam Leffler 	}
1944c42a7b7eSSam Leffler #undef N
19455591b213SSam Leffler }
19465591b213SSam Leffler 
19475591b213SSam Leffler /*
1948c42a7b7eSSam Leffler  * Allocate tx/rx key slots for TKIP.  We allocate two slots for
1949c42a7b7eSSam Leffler  * each key, one for decrypt/encrypt and the other for the MIC.
1950c42a7b7eSSam Leffler  */
1951c42a7b7eSSam Leffler static u_int16_t
1952c1225b52SSam Leffler key_alloc_2pair(struct ath_softc *sc,
1953c1225b52SSam Leffler 	ieee80211_keyix *txkeyix, ieee80211_keyix *rxkeyix)
1954c42a7b7eSSam Leffler {
1955c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
1956c42a7b7eSSam Leffler 	u_int i, keyix;
1957c42a7b7eSSam Leffler 
1958c42a7b7eSSam Leffler 	KASSERT(sc->sc_splitmic, ("key cache !split"));
1959c42a7b7eSSam Leffler 	/* XXX could optimize */
1960c42a7b7eSSam Leffler 	for (i = 0; i < N(sc->sc_keymap)/4; i++) {
1961c42a7b7eSSam Leffler 		u_int8_t b = sc->sc_keymap[i];
1962c42a7b7eSSam Leffler 		if (b != 0xff) {
1963c42a7b7eSSam Leffler 			/*
1964c42a7b7eSSam Leffler 			 * One or more slots in this byte are free.
1965c42a7b7eSSam Leffler 			 */
1966c42a7b7eSSam Leffler 			keyix = i*NBBY;
1967c42a7b7eSSam Leffler 			while (b & 1) {
1968c42a7b7eSSam Leffler 		again:
1969c42a7b7eSSam Leffler 				keyix++;
1970c42a7b7eSSam Leffler 				b >>= 1;
1971c42a7b7eSSam Leffler 			}
1972c42a7b7eSSam Leffler 			/* XXX IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV */
1973c42a7b7eSSam Leffler 			if (isset(sc->sc_keymap, keyix+32) ||
1974c42a7b7eSSam Leffler 			    isset(sc->sc_keymap, keyix+64) ||
1975c42a7b7eSSam Leffler 			    isset(sc->sc_keymap, keyix+32+64)) {
1976c42a7b7eSSam Leffler 				/* full pair unavailable */
1977c42a7b7eSSam Leffler 				/* XXX statistic */
1978c42a7b7eSSam Leffler 				if (keyix == (i+1)*NBBY) {
1979c42a7b7eSSam Leffler 					/* no slots were appropriate, advance */
1980c42a7b7eSSam Leffler 					continue;
1981c42a7b7eSSam Leffler 				}
1982c42a7b7eSSam Leffler 				goto again;
1983c42a7b7eSSam Leffler 			}
1984c42a7b7eSSam Leffler 			setbit(sc->sc_keymap, keyix);
1985c42a7b7eSSam Leffler 			setbit(sc->sc_keymap, keyix+64);
1986c42a7b7eSSam Leffler 			setbit(sc->sc_keymap, keyix+32);
1987c42a7b7eSSam Leffler 			setbit(sc->sc_keymap, keyix+32+64);
1988c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_KEYCACHE,
1989c42a7b7eSSam Leffler 				"%s: key pair %u,%u %u,%u\n",
1990c42a7b7eSSam Leffler 				__func__, keyix, keyix+64,
1991c42a7b7eSSam Leffler 				keyix+32, keyix+32+64);
1992c1225b52SSam Leffler 			*txkeyix = keyix;
1993c1225b52SSam Leffler 			*rxkeyix = keyix+32;
1994c1225b52SSam Leffler 			return 1;
1995c42a7b7eSSam Leffler 		}
1996c42a7b7eSSam Leffler 	}
1997c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of pair space\n", __func__);
1998c1225b52SSam Leffler 	return 0;
1999c42a7b7eSSam Leffler #undef N
2000c42a7b7eSSam Leffler }
2001c42a7b7eSSam Leffler 
2002c42a7b7eSSam Leffler /*
20035901d2d3SSam Leffler  * Allocate tx/rx key slots for TKIP.  We allocate two slots for
20045901d2d3SSam Leffler  * each key, one for decrypt/encrypt and the other for the MIC.
20055901d2d3SSam Leffler  */
20065901d2d3SSam Leffler static u_int16_t
20075901d2d3SSam Leffler key_alloc_pair(struct ath_softc *sc,
20085901d2d3SSam Leffler 	ieee80211_keyix *txkeyix, ieee80211_keyix *rxkeyix)
20095901d2d3SSam Leffler {
20105901d2d3SSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
20115901d2d3SSam Leffler 	u_int i, keyix;
20125901d2d3SSam Leffler 
20135901d2d3SSam Leffler 	KASSERT(!sc->sc_splitmic, ("key cache split"));
20145901d2d3SSam Leffler 	/* XXX could optimize */
20155901d2d3SSam Leffler 	for (i = 0; i < N(sc->sc_keymap)/4; i++) {
20165901d2d3SSam Leffler 		u_int8_t b = sc->sc_keymap[i];
20175901d2d3SSam Leffler 		if (b != 0xff) {
20185901d2d3SSam Leffler 			/*
20195901d2d3SSam Leffler 			 * One or more slots in this byte are free.
20205901d2d3SSam Leffler 			 */
20215901d2d3SSam Leffler 			keyix = i*NBBY;
20225901d2d3SSam Leffler 			while (b & 1) {
20235901d2d3SSam Leffler 		again:
20245901d2d3SSam Leffler 				keyix++;
20255901d2d3SSam Leffler 				b >>= 1;
20265901d2d3SSam Leffler 			}
20275901d2d3SSam Leffler 			if (isset(sc->sc_keymap, keyix+64)) {
20285901d2d3SSam Leffler 				/* full pair unavailable */
20295901d2d3SSam Leffler 				/* XXX statistic */
20305901d2d3SSam Leffler 				if (keyix == (i+1)*NBBY) {
20315901d2d3SSam Leffler 					/* no slots were appropriate, advance */
20325901d2d3SSam Leffler 					continue;
20335901d2d3SSam Leffler 				}
20345901d2d3SSam Leffler 				goto again;
20355901d2d3SSam Leffler 			}
20365901d2d3SSam Leffler 			setbit(sc->sc_keymap, keyix);
20375901d2d3SSam Leffler 			setbit(sc->sc_keymap, keyix+64);
20385901d2d3SSam Leffler 			DPRINTF(sc, ATH_DEBUG_KEYCACHE,
20395901d2d3SSam Leffler 				"%s: key pair %u,%u\n",
20405901d2d3SSam Leffler 				__func__, keyix, keyix+64);
20415901d2d3SSam Leffler 			*txkeyix = *rxkeyix = keyix;
20425901d2d3SSam Leffler 			return 1;
20435901d2d3SSam Leffler 		}
20445901d2d3SSam Leffler 	}
20455901d2d3SSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of pair space\n", __func__);
20465901d2d3SSam Leffler 	return 0;
20475901d2d3SSam Leffler #undef N
20485901d2d3SSam Leffler }
20495901d2d3SSam Leffler 
20505901d2d3SSam Leffler /*
2051c42a7b7eSSam Leffler  * Allocate a single key cache slot.
2052c42a7b7eSSam Leffler  */
2053c1225b52SSam Leffler static int
2054c1225b52SSam Leffler key_alloc_single(struct ath_softc *sc,
2055c1225b52SSam Leffler 	ieee80211_keyix *txkeyix, ieee80211_keyix *rxkeyix)
2056c42a7b7eSSam Leffler {
2057c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
2058c42a7b7eSSam Leffler 	u_int i, keyix;
2059c42a7b7eSSam Leffler 
2060c42a7b7eSSam Leffler 	/* XXX try i,i+32,i+64,i+32+64 to minimize key pair conflicts */
2061c42a7b7eSSam Leffler 	for (i = 0; i < N(sc->sc_keymap); i++) {
2062c42a7b7eSSam Leffler 		u_int8_t b = sc->sc_keymap[i];
2063c42a7b7eSSam Leffler 		if (b != 0xff) {
2064c42a7b7eSSam Leffler 			/*
2065c42a7b7eSSam Leffler 			 * One or more slots are free.
2066c42a7b7eSSam Leffler 			 */
2067c42a7b7eSSam Leffler 			keyix = i*NBBY;
2068c42a7b7eSSam Leffler 			while (b & 1)
2069c42a7b7eSSam Leffler 				keyix++, b >>= 1;
2070c42a7b7eSSam Leffler 			setbit(sc->sc_keymap, keyix);
2071c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: key %u\n",
2072c42a7b7eSSam Leffler 				__func__, keyix);
2073c1225b52SSam Leffler 			*txkeyix = *rxkeyix = keyix;
2074c1225b52SSam Leffler 			return 1;
2075c42a7b7eSSam Leffler 		}
2076c42a7b7eSSam Leffler 	}
2077c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of space\n", __func__);
2078c1225b52SSam Leffler 	return 0;
2079c42a7b7eSSam Leffler #undef N
2080c42a7b7eSSam Leffler }
2081c42a7b7eSSam Leffler 
2082c42a7b7eSSam Leffler /*
2083c42a7b7eSSam Leffler  * Allocate one or more key cache slots for a uniacst key.  The
2084c42a7b7eSSam Leffler  * key itself is needed only to identify the cipher.  For hardware
2085c42a7b7eSSam Leffler  * TKIP with split cipher+MIC keys we allocate two key cache slot
2086c42a7b7eSSam Leffler  * pairs so that we can setup separate TX and RX MIC keys.  Note
2087c42a7b7eSSam Leffler  * that the MIC key for a TKIP key at slot i is assumed by the
2088c42a7b7eSSam Leffler  * hardware to be at slot i+64.  This limits TKIP keys to the first
2089c42a7b7eSSam Leffler  * 64 entries.
2090c42a7b7eSSam Leffler  */
2091c42a7b7eSSam Leffler static int
2092c1225b52SSam Leffler ath_key_alloc(struct ieee80211com *ic, const struct ieee80211_key *k,
2093c1225b52SSam Leffler 	ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
2094c42a7b7eSSam Leffler {
2095c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
2096c42a7b7eSSam Leffler 
2097c42a7b7eSSam Leffler 	/*
20988ca623d7SSam Leffler 	 * Group key allocation must be handled specially for
20998ca623d7SSam Leffler 	 * parts that do not support multicast key cache search
21008ca623d7SSam Leffler 	 * functionality.  For those parts the key id must match
21018ca623d7SSam Leffler 	 * the h/w key index so lookups find the right key.  On
21028ca623d7SSam Leffler 	 * parts w/ the key search facility we install the sender's
21038ca623d7SSam Leffler 	 * mac address (with the high bit set) and let the hardware
21048ca623d7SSam Leffler 	 * find the key w/o using the key id.  This is preferred as
21058ca623d7SSam Leffler 	 * it permits us to support multiple users for adhoc and/or
21068ca623d7SSam Leffler 	 * multi-station operation.
21078ca623d7SSam Leffler 	 */
21088ca623d7SSam Leffler 	if ((k->wk_flags & IEEE80211_KEY_GROUP) && !sc->sc_mcastkey) {
21098ca623d7SSam Leffler 		if (!(&ic->ic_nw_keys[0] <= k &&
21108ca623d7SSam Leffler 		      k < &ic->ic_nw_keys[IEEE80211_WEP_NKID])) {
21118ca623d7SSam Leffler 			/* should not happen */
21128ca623d7SSam Leffler 			DPRINTF(sc, ATH_DEBUG_KEYCACHE,
21138ca623d7SSam Leffler 				"%s: bogus group key\n", __func__);
2114c1225b52SSam Leffler 			return 0;
21158ca623d7SSam Leffler 		}
21168ca623d7SSam Leffler 		/*
21178ca623d7SSam Leffler 		 * XXX we pre-allocate the global keys so
21188ca623d7SSam Leffler 		 * have no way to check if they've already been allocated.
21198ca623d7SSam Leffler 		 */
2120c1225b52SSam Leffler 		*keyix = *rxkeyix = k - ic->ic_nw_keys;
2121c1225b52SSam Leffler 		return 1;
21228ca623d7SSam Leffler 	}
21238ca623d7SSam Leffler 
21248ca623d7SSam Leffler 	/*
2125c42a7b7eSSam Leffler 	 * We allocate two pair for TKIP when using the h/w to do
2126c42a7b7eSSam Leffler 	 * the MIC.  For everything else, including software crypto,
2127c42a7b7eSSam Leffler 	 * we allocate a single entry.  Note that s/w crypto requires
2128c42a7b7eSSam Leffler 	 * a pass-through slot on the 5211 and 5212.  The 5210 does
2129c42a7b7eSSam Leffler 	 * not support pass-through cache entries and we map all
2130c42a7b7eSSam Leffler 	 * those requests to slot 0.
2131c42a7b7eSSam Leffler 	 */
2132c42a7b7eSSam Leffler 	if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
2133c1225b52SSam Leffler 		return key_alloc_single(sc, keyix, rxkeyix);
2134c42a7b7eSSam Leffler 	} else if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP &&
21355901d2d3SSam Leffler 	    (k->wk_flags & IEEE80211_KEY_SWMIC) == 0) {
21365901d2d3SSam Leffler 		if (sc->sc_splitmic)
2137c1225b52SSam Leffler 			return key_alloc_2pair(sc, keyix, rxkeyix);
21385901d2d3SSam Leffler 		else
21395901d2d3SSam Leffler 			return key_alloc_pair(sc, keyix, rxkeyix);
2140c42a7b7eSSam Leffler 	} else {
2141c1225b52SSam Leffler 		return key_alloc_single(sc, keyix, rxkeyix);
2142c42a7b7eSSam Leffler 	}
2143c42a7b7eSSam Leffler }
2144c42a7b7eSSam Leffler 
2145c42a7b7eSSam Leffler /*
2146c42a7b7eSSam Leffler  * Delete an entry in the key cache allocated by ath_key_alloc.
2147c42a7b7eSSam Leffler  */
2148c42a7b7eSSam Leffler static int
2149c42a7b7eSSam Leffler ath_key_delete(struct ieee80211com *ic, const struct ieee80211_key *k)
2150c42a7b7eSSam Leffler {
2151c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
2152c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2153c42a7b7eSSam Leffler 	const struct ieee80211_cipher *cip = k->wk_cipher;
2154c42a7b7eSSam Leffler 	u_int keyix = k->wk_keyix;
2155c42a7b7eSSam Leffler 
2156c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: delete key %u\n", __func__, keyix);
2157c42a7b7eSSam Leffler 
2158c42a7b7eSSam Leffler 	ath_hal_keyreset(ah, keyix);
2159c42a7b7eSSam Leffler 	/*
2160c42a7b7eSSam Leffler 	 * Handle split tx/rx keying required for TKIP with h/w MIC.
2161c42a7b7eSSam Leffler 	 */
2162c42a7b7eSSam Leffler 	if (cip->ic_cipher == IEEE80211_CIPHER_TKIP &&
2163c1225b52SSam Leffler 	    (k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && sc->sc_splitmic)
2164c42a7b7eSSam Leffler 		ath_hal_keyreset(ah, keyix+32);		/* RX key */
2165c42a7b7eSSam Leffler 	if (keyix >= IEEE80211_WEP_NKID) {
2166c42a7b7eSSam Leffler 		/*
2167c42a7b7eSSam Leffler 		 * Don't touch keymap entries for global keys so
2168c42a7b7eSSam Leffler 		 * they are never considered for dynamic allocation.
2169c42a7b7eSSam Leffler 		 */
2170c42a7b7eSSam Leffler 		clrbit(sc->sc_keymap, keyix);
2171c42a7b7eSSam Leffler 		if (cip->ic_cipher == IEEE80211_CIPHER_TKIP &&
21725901d2d3SSam Leffler 		    (k->wk_flags & IEEE80211_KEY_SWMIC) == 0) {
2173c42a7b7eSSam Leffler 			clrbit(sc->sc_keymap, keyix+64);	/* TX key MIC */
21745901d2d3SSam Leffler 			if (sc->sc_splitmic) {
21755901d2d3SSam Leffler 				/* +32 for RX key, +32+64 for RX key MIC */
21765901d2d3SSam Leffler 				clrbit(sc->sc_keymap, keyix+32);
21775901d2d3SSam Leffler 				clrbit(sc->sc_keymap, keyix+32+64);
21785901d2d3SSam Leffler 			}
2179c42a7b7eSSam Leffler 		}
2180c42a7b7eSSam Leffler 	}
2181c42a7b7eSSam Leffler 	return 1;
2182c42a7b7eSSam Leffler }
2183c42a7b7eSSam Leffler 
2184c42a7b7eSSam Leffler /*
2185c42a7b7eSSam Leffler  * Set the key cache contents for the specified key.  Key cache
2186c42a7b7eSSam Leffler  * slot(s) must already have been allocated by ath_key_alloc.
2187c42a7b7eSSam Leffler  */
2188c42a7b7eSSam Leffler static int
2189c42a7b7eSSam Leffler ath_key_set(struct ieee80211com *ic, const struct ieee80211_key *k,
2190c42a7b7eSSam Leffler 	const u_int8_t mac[IEEE80211_ADDR_LEN])
2191c42a7b7eSSam Leffler {
2192c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
2193c42a7b7eSSam Leffler 
2194e8fd88a3SSam Leffler 	return ath_keyset(sc, k, mac, ic->ic_bss);
2195c42a7b7eSSam Leffler }
2196c42a7b7eSSam Leffler 
2197c42a7b7eSSam Leffler /*
2198c42a7b7eSSam Leffler  * Block/unblock tx+rx processing while a key change is done.
2199c42a7b7eSSam Leffler  * We assume the caller serializes key management operations
2200c42a7b7eSSam Leffler  * so we only need to worry about synchronization with other
2201c42a7b7eSSam Leffler  * uses that originate in the driver.
2202c42a7b7eSSam Leffler  */
2203c42a7b7eSSam Leffler static void
2204c42a7b7eSSam Leffler ath_key_update_begin(struct ieee80211com *ic)
2205c42a7b7eSSam Leffler {
2206c42a7b7eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
2207c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2208c42a7b7eSSam Leffler 
2209c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
2210c42a7b7eSSam Leffler #if 0
2211c42a7b7eSSam Leffler 	tasklet_disable(&sc->sc_rxtq);
2212c42a7b7eSSam Leffler #endif
2213c42a7b7eSSam Leffler 	IF_LOCK(&ifp->if_snd);		/* NB: doesn't block mgmt frames */
2214c42a7b7eSSam Leffler }
2215c42a7b7eSSam Leffler 
2216c42a7b7eSSam Leffler static void
2217c42a7b7eSSam Leffler ath_key_update_end(struct ieee80211com *ic)
2218c42a7b7eSSam Leffler {
2219c42a7b7eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
2220c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2221c42a7b7eSSam Leffler 
2222c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
2223c42a7b7eSSam Leffler 	IF_UNLOCK(&ifp->if_snd);
2224c42a7b7eSSam Leffler #if 0
2225c42a7b7eSSam Leffler 	tasklet_enable(&sc->sc_rxtq);
2226c42a7b7eSSam Leffler #endif
2227c42a7b7eSSam Leffler }
22285591b213SSam Leffler 
22294bc0e754SSam Leffler /*
22304bc0e754SSam Leffler  * Calculate the receive filter according to the
22314bc0e754SSam Leffler  * operating mode and state:
22324bc0e754SSam Leffler  *
22334bc0e754SSam Leffler  * o always accept unicast, broadcast, and multicast traffic
2234c42a7b7eSSam Leffler  * o maintain current state of phy error reception (the hal
2235c42a7b7eSSam Leffler  *   may enable phy error frames for noise immunity work)
22364bc0e754SSam Leffler  * o probe request frames are accepted only when operating in
22374bc0e754SSam Leffler  *   hostap, adhoc, or monitor modes
22384bc0e754SSam Leffler  * o enable promiscuous mode according to the interface state
22394bc0e754SSam Leffler  * o accept beacons:
22404bc0e754SSam Leffler  *   - when operating in adhoc mode so the 802.11 layer creates
22414bc0e754SSam Leffler  *     node table entries for peers,
22424bc0e754SSam Leffler  *   - when operating in station mode for collecting rssi data when
22434bc0e754SSam Leffler  *     the station is otherwise quiet, or
22444bc0e754SSam Leffler  *   - when scanning
22456f48c956SSam Leffler  * o accept control frames:
22466f48c956SSam Leffler  *   - when in monitor mode
22474bc0e754SSam Leffler  */
22484bc0e754SSam Leffler static u_int32_t
224968e8e04eSSam Leffler ath_calcrxfilter(struct ath_softc *sc)
22504bc0e754SSam Leffler {
2251bd5a9920SSam Leffler #define	RX_FILTER_PRESERVE	(HAL_RX_FILTER_PHYERR | HAL_RX_FILTER_PHYRADAR)
22524bc0e754SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
22534bc0e754SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2254fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
22554bc0e754SSam Leffler 	u_int32_t rfilt;
22564bc0e754SSam Leffler 
2257bd5a9920SSam Leffler 	rfilt = (ath_hal_getrxfilter(ah) & RX_FILTER_PRESERVE)
22584bc0e754SSam Leffler 	      | HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST;
22594bc0e754SSam Leffler 	if (ic->ic_opmode != IEEE80211_M_STA)
22604bc0e754SSam Leffler 		rfilt |= HAL_RX_FILTER_PROBEREQ;
22614bc0e754SSam Leffler 	if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
22624bc0e754SSam Leffler 	    (ifp->if_flags & IFF_PROMISC))
22634bc0e754SSam Leffler 		rfilt |= HAL_RX_FILTER_PROM;
22644bc0e754SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA ||
22654bc0e754SSam Leffler 	    ic->ic_opmode == IEEE80211_M_IBSS ||
226668e8e04eSSam Leffler 	    sc->sc_scanning)
22674bc0e754SSam Leffler 		rfilt |= HAL_RX_FILTER_BEACON;
22686f48c956SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
22696f48c956SSam Leffler 		rfilt |= HAL_RX_FILTER_CONTROL;
22704bc0e754SSam Leffler 	return rfilt;
2271bd5a9920SSam Leffler #undef RX_FILTER_PRESERVE
22724bc0e754SSam Leffler }
22734bc0e754SSam Leffler 
22745591b213SSam Leffler static void
22755591b213SSam Leffler ath_mode_init(struct ath_softc *sc)
22765591b213SSam Leffler {
22775591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
22785591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2279fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
22805591b213SSam Leffler 	u_int32_t rfilt, mfilt[2], val;
22815591b213SSam Leffler 	u_int8_t pos;
22825591b213SSam Leffler 	struct ifmultiaddr *ifma;
22835591b213SSam Leffler 
22844bc0e754SSam Leffler 	/* configure rx filter */
228568e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
22864bc0e754SSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
22874bc0e754SSam Leffler 
22885591b213SSam Leffler 	/* configure operational mode */
2289c42a7b7eSSam Leffler 	ath_hal_setopmode(ah);
2290c42a7b7eSSam Leffler 
2291c42a7b7eSSam Leffler 	/*
2292c42a7b7eSSam Leffler 	 * Handle any link-level address change.  Note that we only
2293c42a7b7eSSam Leffler 	 * need to force ic_myaddr; any other addresses are handled
2294c42a7b7eSSam Leffler 	 * as a byproduct of the ifnet code marking the interface
2295c42a7b7eSSam Leffler 	 * down then up.
2296c42a7b7eSSam Leffler 	 *
2297c42a7b7eSSam Leffler 	 * XXX should get from lladdr instead of arpcom but that's more work
2298c42a7b7eSSam Leffler 	 */
22994a0d6638SRuslan Ermilov 	IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2300c42a7b7eSSam Leffler 	ath_hal_setmac(ah, ic->ic_myaddr);
23015591b213SSam Leffler 
23025591b213SSam Leffler 	/* calculate and install multicast filter */
23035591b213SSam Leffler 	if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
23045591b213SSam Leffler 		mfilt[0] = mfilt[1] = 0;
230513b203d0SRobert Watson 		IF_ADDR_LOCK(ifp);
23065591b213SSam Leffler 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
23075591b213SSam Leffler 			caddr_t dl;
23085591b213SSam Leffler 
23095591b213SSam Leffler 			/* calculate XOR of eight 6bit values */
23105591b213SSam Leffler 			dl = LLADDR((struct sockaddr_dl *) ifma->ifma_addr);
23115591b213SSam Leffler 			val = LE_READ_4(dl + 0);
23125591b213SSam Leffler 			pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
23135591b213SSam Leffler 			val = LE_READ_4(dl + 3);
23145591b213SSam Leffler 			pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
23155591b213SSam Leffler 			pos &= 0x3f;
23165591b213SSam Leffler 			mfilt[pos / 32] |= (1 << (pos % 32));
23175591b213SSam Leffler 		}
231813b203d0SRobert Watson 		IF_ADDR_UNLOCK(ifp);
23195591b213SSam Leffler 	} else {
23205591b213SSam Leffler 		mfilt[0] = mfilt[1] = ~0;
23215591b213SSam Leffler 	}
23225591b213SSam Leffler 	ath_hal_setmcastfilter(ah, mfilt[0], mfilt[1]);
2323c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x, MC filter %08x:%08x\n",
2324c42a7b7eSSam Leffler 		__func__, rfilt, mfilt[0], mfilt[1]);
23255591b213SSam Leffler }
23265591b213SSam Leffler 
2327c42a7b7eSSam Leffler /*
2328c42a7b7eSSam Leffler  * Set the slot time based on the current setting.
2329c42a7b7eSSam Leffler  */
2330c42a7b7eSSam Leffler static void
2331c42a7b7eSSam Leffler ath_setslottime(struct ath_softc *sc)
2332c42a7b7eSSam Leffler {
2333c42a7b7eSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
2334c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2335aaa70f2fSSam Leffler 	u_int usec;
2336c42a7b7eSSam Leffler 
2337aaa70f2fSSam Leffler 	if (IEEE80211_IS_CHAN_HALF(ic->ic_curchan))
2338aaa70f2fSSam Leffler 		usec = 13;
2339aaa70f2fSSam Leffler 	else if (IEEE80211_IS_CHAN_QUARTER(ic->ic_curchan))
2340aaa70f2fSSam Leffler 		usec = 21;
2341724c193aSSam Leffler 	else if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan)) {
2342724c193aSSam Leffler 		/* honor short/long slot time only in 11g */
2343724c193aSSam Leffler 		/* XXX shouldn't honor on pure g or turbo g channel */
2344724c193aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
2345aaa70f2fSSam Leffler 			usec = HAL_SLOT_TIME_9;
2346aaa70f2fSSam Leffler 		else
2347aaa70f2fSSam Leffler 			usec = HAL_SLOT_TIME_20;
2348724c193aSSam Leffler 	} else
2349724c193aSSam Leffler 		usec = HAL_SLOT_TIME_9;
2350aaa70f2fSSam Leffler 
2351aaa70f2fSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET,
2352aaa70f2fSSam Leffler 	    "%s: chan %u MHz flags 0x%x %s slot, %u usec\n",
2353aaa70f2fSSam Leffler 	    __func__, ic->ic_curchan->ic_freq, ic->ic_curchan->ic_flags,
2354aaa70f2fSSam Leffler 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", usec);
2355aaa70f2fSSam Leffler 
2356aaa70f2fSSam Leffler 	ath_hal_setslottime(ah, usec);
2357c42a7b7eSSam Leffler 	sc->sc_updateslot = OK;
2358c42a7b7eSSam Leffler }
2359c42a7b7eSSam Leffler 
2360c42a7b7eSSam Leffler /*
2361c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update the
2362c42a7b7eSSam Leffler  * slot time based on the current setting.
2363c42a7b7eSSam Leffler  */
2364c42a7b7eSSam Leffler static void
2365c42a7b7eSSam Leffler ath_updateslot(struct ifnet *ifp)
2366c42a7b7eSSam Leffler {
2367c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2368c42a7b7eSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
2369c42a7b7eSSam Leffler 
2370c42a7b7eSSam Leffler 	/*
2371c42a7b7eSSam Leffler 	 * When not coordinating the BSS, change the hardware
2372c42a7b7eSSam Leffler 	 * immediately.  For other operation we defer the change
2373c42a7b7eSSam Leffler 	 * until beacon updates have propagated to the stations.
2374c42a7b7eSSam Leffler 	 */
2375c42a7b7eSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP)
2376c42a7b7eSSam Leffler 		sc->sc_updateslot = UPDATE;
2377c42a7b7eSSam Leffler 	else
2378c42a7b7eSSam Leffler 		ath_setslottime(sc);
2379c42a7b7eSSam Leffler }
2380c42a7b7eSSam Leffler 
2381c42a7b7eSSam Leffler /*
238280d2765fSSam Leffler  * Setup a h/w transmit queue for beacons.
238380d2765fSSam Leffler  */
238480d2765fSSam Leffler static int
238580d2765fSSam Leffler ath_beaconq_setup(struct ath_hal *ah)
238680d2765fSSam Leffler {
238780d2765fSSam Leffler 	HAL_TXQ_INFO qi;
238880d2765fSSam Leffler 
238980d2765fSSam Leffler 	memset(&qi, 0, sizeof(qi));
239080d2765fSSam Leffler 	qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
239180d2765fSSam Leffler 	qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
239280d2765fSSam Leffler 	qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
23930f2e86fbSSam Leffler 	/* NB: for dynamic turbo, don't enable any other interrupts */
2394bd5a9920SSam Leffler 	qi.tqi_qflags = HAL_TXQ_TXDESCINT_ENABLE;
239580d2765fSSam Leffler 	return ath_hal_setuptxqueue(ah, HAL_TX_QUEUE_BEACON, &qi);
239680d2765fSSam Leffler }
239780d2765fSSam Leffler 
239880d2765fSSam Leffler /*
23990f2e86fbSSam Leffler  * Setup the transmit queue parameters for the beacon queue.
24000f2e86fbSSam Leffler  */
24010f2e86fbSSam Leffler static int
24020f2e86fbSSam Leffler ath_beaconq_config(struct ath_softc *sc)
24030f2e86fbSSam Leffler {
24040f2e86fbSSam Leffler #define	ATH_EXPONENT_TO_VALUE(v)	((1<<(v))-1)
24050f2e86fbSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
24060f2e86fbSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
24070f2e86fbSSam Leffler 	HAL_TXQ_INFO qi;
24080f2e86fbSSam Leffler 
24090f2e86fbSSam Leffler 	ath_hal_gettxqueueprops(ah, sc->sc_bhalq, &qi);
24100f2e86fbSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
24110f2e86fbSSam Leffler 		/*
24120f2e86fbSSam Leffler 		 * Always burst out beacon and CAB traffic.
24130f2e86fbSSam Leffler 		 */
24140f2e86fbSSam Leffler 		qi.tqi_aifs = ATH_BEACON_AIFS_DEFAULT;
24150f2e86fbSSam Leffler 		qi.tqi_cwmin = ATH_BEACON_CWMIN_DEFAULT;
24160f2e86fbSSam Leffler 		qi.tqi_cwmax = ATH_BEACON_CWMAX_DEFAULT;
24170f2e86fbSSam Leffler 	} else {
24180f2e86fbSSam Leffler 		struct wmeParams *wmep =
24190f2e86fbSSam Leffler 			&ic->ic_wme.wme_chanParams.cap_wmeParams[WME_AC_BE];
24200f2e86fbSSam Leffler 		/*
24210f2e86fbSSam Leffler 		 * Adhoc mode; important thing is to use 2x cwmin.
24220f2e86fbSSam Leffler 		 */
24230f2e86fbSSam Leffler 		qi.tqi_aifs = wmep->wmep_aifsn;
24240f2e86fbSSam Leffler 		qi.tqi_cwmin = 2*ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
24250f2e86fbSSam Leffler 		qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
24260f2e86fbSSam Leffler 	}
24270f2e86fbSSam Leffler 
24280f2e86fbSSam Leffler 	if (!ath_hal_settxqueueprops(ah, sc->sc_bhalq, &qi)) {
24290f2e86fbSSam Leffler 		device_printf(sc->sc_dev, "unable to update parameters for "
24300f2e86fbSSam Leffler 			"beacon hardware queue!\n");
24310f2e86fbSSam Leffler 		return 0;
24320f2e86fbSSam Leffler 	} else {
24330f2e86fbSSam Leffler 		ath_hal_resettxqueue(ah, sc->sc_bhalq); /* push to h/w */
24340f2e86fbSSam Leffler 		return 1;
24350f2e86fbSSam Leffler 	}
24360f2e86fbSSam Leffler #undef ATH_EXPONENT_TO_VALUE
24370f2e86fbSSam Leffler }
24380f2e86fbSSam Leffler 
24390f2e86fbSSam Leffler /*
2440c42a7b7eSSam Leffler  * Allocate and setup an initial beacon frame.
2441c42a7b7eSSam Leffler  */
24425591b213SSam Leffler static int
24435591b213SSam Leffler ath_beacon_alloc(struct ath_softc *sc, struct ieee80211_node *ni)
24445591b213SSam Leffler {
2445c42a7b7eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
24465591b213SSam Leffler 	struct ath_buf *bf;
24475591b213SSam Leffler 	struct mbuf *m;
2448c42a7b7eSSam Leffler 	int error;
24495591b213SSam Leffler 
2450c42a7b7eSSam Leffler 	bf = STAILQ_FIRST(&sc->sc_bbuf);
2451c42a7b7eSSam Leffler 	if (bf == NULL) {
2452c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON, "%s: no dma buffers\n", __func__);
2453c42a7b7eSSam Leffler 		sc->sc_stats.ast_be_nombuf++;	/* XXX */
2454c42a7b7eSSam Leffler 		return ENOMEM;			/* XXX */
2455c42a7b7eSSam Leffler 	}
24565591b213SSam Leffler 	/*
24575591b213SSam Leffler 	 * NB: the beacon data buffer must be 32-bit aligned;
24585591b213SSam Leffler 	 * we assume the mbuf routines will return us something
24595591b213SSam Leffler 	 * with this alignment (perhaps should assert).
24605591b213SSam Leffler 	 */
2461c42a7b7eSSam Leffler 	m = ieee80211_beacon_alloc(ic, ni, &sc->sc_boff);
24625591b213SSam Leffler 	if (m == NULL) {
2463c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON, "%s: cannot get mbuf\n",
2464c42a7b7eSSam Leffler 			__func__);
24655591b213SSam Leffler 		sc->sc_stats.ast_be_nombuf++;
24665591b213SSam Leffler 		return ENOMEM;
24675591b213SSam Leffler 	}
2468f9e6219bSSam Leffler 	error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m,
2469f9e6219bSSam Leffler 				     bf->bf_segs, &bf->bf_nseg,
24705591b213SSam Leffler 				     BUS_DMA_NOWAIT);
2471c42a7b7eSSam Leffler 	if (error == 0) {
2472c42a7b7eSSam Leffler 		bf->bf_m = m;
2473f818612bSSam Leffler 		bf->bf_node = ieee80211_ref_node(ni);
2474c42a7b7eSSam Leffler 	} else {
24755591b213SSam Leffler 		m_freem(m);
2476c42a7b7eSSam Leffler 	}
24775591b213SSam Leffler 	return error;
24785591b213SSam Leffler }
2479c42a7b7eSSam Leffler 
2480c42a7b7eSSam Leffler /*
2481c42a7b7eSSam Leffler  * Setup the beacon frame for transmit.
2482c42a7b7eSSam Leffler  */
2483c42a7b7eSSam Leffler static void
2484c42a7b7eSSam Leffler ath_beacon_setup(struct ath_softc *sc, struct ath_buf *bf)
2485c42a7b7eSSam Leffler {
2486c42a7b7eSSam Leffler #define	USE_SHPREAMBLE(_ic) \
2487c42a7b7eSSam Leffler 	(((_ic)->ic_flags & (IEEE80211_F_SHPREAMBLE | IEEE80211_F_USEBARKER))\
2488c42a7b7eSSam Leffler 		== IEEE80211_F_SHPREAMBLE)
2489c42a7b7eSSam Leffler 	struct ieee80211_node *ni = bf->bf_node;
2490c42a7b7eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2491c42a7b7eSSam Leffler 	struct mbuf *m = bf->bf_m;
2492c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2493c42a7b7eSSam Leffler 	struct ath_desc *ds;
2494c42a7b7eSSam Leffler 	int flags, antenna;
249555f63772SSam Leffler 	const HAL_RATE_TABLE *rt;
249655f63772SSam Leffler 	u_int8_t rix, rate;
2497c42a7b7eSSam Leffler 
24984a3ac3fcSSam Leffler 	DPRINTF(sc, ATH_DEBUG_BEACON_PROC, "%s: m %p len %u\n",
2499c42a7b7eSSam Leffler 		__func__, m, m->m_len);
25005591b213SSam Leffler 
25015591b213SSam Leffler 	/* setup descriptors */
25025591b213SSam Leffler 	ds = bf->bf_desc;
25035591b213SSam Leffler 
2504c42a7b7eSSam Leffler 	flags = HAL_TXDESC_NOACK;
2505c42a7b7eSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_hasveol) {
2506c42a7b7eSSam Leffler 		ds->ds_link = bf->bf_daddr;	/* self-linked */
2507c42a7b7eSSam Leffler 		flags |= HAL_TXDESC_VEOL;
2508c42a7b7eSSam Leffler 		/*
2509c42a7b7eSSam Leffler 		 * Let hardware handle antenna switching.
2510c42a7b7eSSam Leffler 		 */
25114866e6c2SSam Leffler 		antenna = sc->sc_txantenna;
2512c42a7b7eSSam Leffler 	} else {
25135591b213SSam Leffler 		ds->ds_link = 0;
2514c42a7b7eSSam Leffler 		/*
2515c42a7b7eSSam Leffler 		 * Switch antenna every 4 beacons.
2516c42a7b7eSSam Leffler 		 * XXX assumes two antenna
2517c42a7b7eSSam Leffler 		 */
2518df4d04afSSam Leffler 		antenna = sc->sc_txantenna != 0 ? sc->sc_txantenna
2519df4d04afSSam Leffler 			: (sc->sc_stats.ast_be_xmit & 4 ? 2 : 1);
2520c42a7b7eSSam Leffler 	}
2521c42a7b7eSSam Leffler 
2522c42a7b7eSSam Leffler 	KASSERT(bf->bf_nseg == 1,
2523c42a7b7eSSam Leffler 		("multi-segment beacon frame; nseg %u", bf->bf_nseg));
25245591b213SSam Leffler 	ds->ds_data = bf->bf_segs[0].ds_addr;
25255591b213SSam Leffler 	/*
25265591b213SSam Leffler 	 * Calculate rate code.
25275591b213SSam Leffler 	 * XXX everything at min xmit rate
25285591b213SSam Leffler 	 */
252955f63772SSam Leffler 	rix = sc->sc_minrateix;
253055f63772SSam Leffler 	rt = sc->sc_currates;
253155f63772SSam Leffler 	rate = rt->info[rix].rateCode;
2532c42a7b7eSSam Leffler 	if (USE_SHPREAMBLE(ic))
253355f63772SSam Leffler 		rate |= rt->info[rix].shortPreamble;
25345591b213SSam Leffler 	ath_hal_setuptxdesc(ah, ds
2535c42a7b7eSSam Leffler 		, m->m_len + IEEE80211_CRC_LEN	/* frame length */
25365591b213SSam Leffler 		, sizeof(struct ieee80211_frame)/* header length */
25375591b213SSam Leffler 		, HAL_PKT_TYPE_BEACON		/* Atheros packet type */
2538c42a7b7eSSam Leffler 		, ni->ni_txpower		/* txpower XXX */
25395591b213SSam Leffler 		, rate, 1			/* series 0 rate/tries */
25405591b213SSam Leffler 		, HAL_TXKEYIX_INVALID		/* no encryption */
2541c42a7b7eSSam Leffler 		, antenna			/* antenna mode */
2542c42a7b7eSSam Leffler 		, flags				/* no ack, veol for beacons */
25435591b213SSam Leffler 		, 0				/* rts/cts rate */
25445591b213SSam Leffler 		, 0				/* rts/cts duration */
25455591b213SSam Leffler 	);
25465591b213SSam Leffler 	/* NB: beacon's BufLen must be a multiple of 4 bytes */
25475591b213SSam Leffler 	ath_hal_filltxdesc(ah, ds
2548c42a7b7eSSam Leffler 		, roundup(m->m_len, 4)		/* buffer length */
25495591b213SSam Leffler 		, AH_TRUE			/* first segment */
25505591b213SSam Leffler 		, AH_TRUE			/* last segment */
2551c42a7b7eSSam Leffler 		, ds				/* first descriptor */
25525591b213SSam Leffler 	);
2553c42a7b7eSSam Leffler #undef USE_SHPREAMBLE
25545591b213SSam Leffler }
25555591b213SSam Leffler 
2556c42a7b7eSSam Leffler /*
2557622b3fd2SSam Leffler  * Append the contents of src to dst; both queues
2558622b3fd2SSam Leffler  * are assumed to be locked.
2559622b3fd2SSam Leffler  */
2560622b3fd2SSam Leffler static void
2561622b3fd2SSam Leffler ath_txqmove(struct ath_txq *dst, struct ath_txq *src)
2562622b3fd2SSam Leffler {
2563622b3fd2SSam Leffler 	STAILQ_CONCAT(&dst->axq_q, &src->axq_q);
2564622b3fd2SSam Leffler 	dst->axq_link = src->axq_link;
2565622b3fd2SSam Leffler 	src->axq_link = NULL;
2566622b3fd2SSam Leffler 	dst->axq_depth += src->axq_depth;
2567622b3fd2SSam Leffler 	src->axq_depth = 0;
2568622b3fd2SSam Leffler }
2569622b3fd2SSam Leffler 
2570622b3fd2SSam Leffler /*
2571c42a7b7eSSam Leffler  * Transmit a beacon frame at SWBA.  Dynamic updates to the
2572c42a7b7eSSam Leffler  * frame contents are done as needed and the slot time is
2573c42a7b7eSSam Leffler  * also adjusted based on current state.
2574c42a7b7eSSam Leffler  */
25755591b213SSam Leffler static void
25765591b213SSam Leffler ath_beacon_proc(void *arg, int pending)
25775591b213SSam Leffler {
25785591b213SSam Leffler 	struct ath_softc *sc = arg;
2579c42a7b7eSSam Leffler 	struct ath_buf *bf = STAILQ_FIRST(&sc->sc_bbuf);
2580c42a7b7eSSam Leffler 	struct ieee80211_node *ni = bf->bf_node;
2581c42a7b7eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
25825591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2583622b3fd2SSam Leffler 	struct ath_txq *cabq = sc->sc_cabq;
2584c42a7b7eSSam Leffler 	struct mbuf *m;
2585622b3fd2SSam Leffler 	int ncabq, nmcastq, error, otherant;
25865591b213SSam Leffler 
2587c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_BEACON_PROC, "%s: pending %u\n",
2588c42a7b7eSSam Leffler 		__func__, pending);
2589c42a7b7eSSam Leffler 
25900a915fadSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA ||
2591c42a7b7eSSam Leffler 	    ic->ic_opmode == IEEE80211_M_MONITOR ||
25920a915fadSSam Leffler 	    bf == NULL || bf->bf_m == NULL) {
2593c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: ic_flags=%x bf=%p bf_m=%p\n",
2594c42a7b7eSSam Leffler 			__func__, ic->ic_flags, bf, bf ? bf->bf_m : NULL);
25955591b213SSam Leffler 		return;
25965591b213SSam Leffler 	}
2597c42a7b7eSSam Leffler 	/*
2598c42a7b7eSSam Leffler 	 * Check if the previous beacon has gone out.  If
2599c66c48cbSSam Leffler 	 * not don't try to post another, skip this period
2600c66c48cbSSam Leffler 	 * and wait for the next.  Missed beacons indicate
2601c66c48cbSSam Leffler 	 * a problem and should not occur.  If we miss too
2602c66c48cbSSam Leffler 	 * many consecutive beacons reset the device.
2603c42a7b7eSSam Leffler 	 */
2604c42a7b7eSSam Leffler 	if (ath_hal_numtxpending(ah, sc->sc_bhalq) != 0) {
2605c42a7b7eSSam Leffler 		sc->sc_bmisscount++;
26064a3ac3fcSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
2607c42a7b7eSSam Leffler 			"%s: missed %u consecutive beacons\n",
2608c42a7b7eSSam Leffler 			__func__, sc->sc_bmisscount);
2609c42a7b7eSSam Leffler 		if (sc->sc_bmisscount > 3)		/* NB: 3 is a guess */
26100bbf5441SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_bstucktask);
2611c42a7b7eSSam Leffler 		return;
2612c42a7b7eSSam Leffler 	}
2613c42a7b7eSSam Leffler 	if (sc->sc_bmisscount != 0) {
2614c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
2615c42a7b7eSSam Leffler 			"%s: resume beacon xmit after %u misses\n",
2616c42a7b7eSSam Leffler 			__func__, sc->sc_bmisscount);
2617c42a7b7eSSam Leffler 		sc->sc_bmisscount = 0;
2618c42a7b7eSSam Leffler 	}
2619c42a7b7eSSam Leffler 
2620c42a7b7eSSam Leffler 	/*
2621c42a7b7eSSam Leffler 	 * Update dynamic beacon contents.  If this returns
2622c42a7b7eSSam Leffler 	 * non-zero then we need to remap the memory because
2623c42a7b7eSSam Leffler 	 * the beacon frame changed size (probably because
2624c42a7b7eSSam Leffler 	 * of the TIM bitmap).
2625c42a7b7eSSam Leffler 	 */
2626c42a7b7eSSam Leffler 	m = bf->bf_m;
2627622b3fd2SSam Leffler 	nmcastq = sc->sc_mcastq.axq_depth;
2628622b3fd2SSam Leffler 	ncabq = ath_hal_numtxpending(ah, cabq->axq_qnum);
2629622b3fd2SSam Leffler 	if (ieee80211_beacon_update(ic, bf->bf_node, &sc->sc_boff, m, ncabq+nmcastq)) {
2630c42a7b7eSSam Leffler 		/* XXX too conservative? */
2631c42a7b7eSSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
2632f9e6219bSSam Leffler 		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m,
2633f9e6219bSSam Leffler 					     bf->bf_segs, &bf->bf_nseg,
2634c42a7b7eSSam Leffler 					     BUS_DMA_NOWAIT);
2635c42a7b7eSSam Leffler 		if (error != 0) {
2636c42a7b7eSSam Leffler 			if_printf(ic->ic_ifp,
2637f9e6219bSSam Leffler 			    "%s: bus_dmamap_load_mbuf_sg failed, error %u\n",
2638c42a7b7eSSam Leffler 			    __func__, error);
2639c42a7b7eSSam Leffler 			return;
2640c42a7b7eSSam Leffler 		}
2641c42a7b7eSSam Leffler 	}
2642622b3fd2SSam Leffler 	if (ncabq && (sc->sc_boff.bo_tim[4] & 1)) {
2643622b3fd2SSam Leffler 		/*
2644622b3fd2SSam Leffler 		 * CABQ traffic from the previous DTIM is still pending.
2645622b3fd2SSam Leffler 		 * This is ok for now but when there are multiple vap's
2646622b3fd2SSam Leffler 		 * and we are using staggered beacons we'll want to drain
2647622b3fd2SSam Leffler 		 * the cabq before loading frames for the different vap.
2648622b3fd2SSam Leffler 		 */
2649622b3fd2SSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
2650622b3fd2SSam Leffler 		    "%s: cabq did not drain, mcastq %u cabq %u/%u\n",
2651622b3fd2SSam Leffler 		    __func__, nmcastq, ncabq, cabq->axq_depth);
2652622b3fd2SSam Leffler 		sc->sc_stats.ast_cabq_busy++;
2653622b3fd2SSam Leffler 	}
2654c42a7b7eSSam Leffler 
2655c42a7b7eSSam Leffler 	/*
2656c42a7b7eSSam Leffler 	 * Handle slot time change when a non-ERP station joins/leaves
2657c42a7b7eSSam Leffler 	 * an 11g network.  The 802.11 layer notifies us via callback,
2658c42a7b7eSSam Leffler 	 * we mark updateslot, then wait one beacon before effecting
2659c42a7b7eSSam Leffler 	 * the change.  This gives associated stations at least one
2660c42a7b7eSSam Leffler 	 * beacon interval to note the state change.
2661c42a7b7eSSam Leffler 	 */
2662c42a7b7eSSam Leffler 	/* XXX locking */
2663c42a7b7eSSam Leffler 	if (sc->sc_updateslot == UPDATE)
2664c42a7b7eSSam Leffler 		sc->sc_updateslot = COMMIT;	/* commit next beacon */
2665c42a7b7eSSam Leffler 	else if (sc->sc_updateslot == COMMIT)
2666c42a7b7eSSam Leffler 		ath_setslottime(sc);		/* commit change to h/w */
2667c42a7b7eSSam Leffler 
2668c42a7b7eSSam Leffler 	/*
2669c42a7b7eSSam Leffler 	 * Check recent per-antenna transmit statistics and flip
2670c42a7b7eSSam Leffler 	 * the default antenna if noticeably more frames went out
2671c42a7b7eSSam Leffler 	 * on the non-default antenna.
2672c42a7b7eSSam Leffler 	 * XXX assumes 2 anntenae
2673c42a7b7eSSam Leffler 	 */
2674c42a7b7eSSam Leffler 	otherant = sc->sc_defant & 1 ? 2 : 1;
2675c42a7b7eSSam Leffler 	if (sc->sc_ant_tx[otherant] > sc->sc_ant_tx[sc->sc_defant] + 2)
2676c42a7b7eSSam Leffler 		ath_setdefantenna(sc, otherant);
2677c42a7b7eSSam Leffler 	sc->sc_ant_tx[1] = sc->sc_ant_tx[2] = 0;
2678c42a7b7eSSam Leffler 
2679c42a7b7eSSam Leffler 	/*
2680c42a7b7eSSam Leffler 	 * Construct tx descriptor.
2681c42a7b7eSSam Leffler 	 */
2682c42a7b7eSSam Leffler 	ath_beacon_setup(sc, bf);
2683c42a7b7eSSam Leffler 
2684c42a7b7eSSam Leffler 	/*
2685c42a7b7eSSam Leffler 	 * Stop any current dma and put the new frame on the queue.
2686c42a7b7eSSam Leffler 	 * This should never fail since we check above that no frames
2687c42a7b7eSSam Leffler 	 * are still pending on the queue.
2688c42a7b7eSSam Leffler 	 */
26895591b213SSam Leffler 	if (!ath_hal_stoptxdma(ah, sc->sc_bhalq)) {
2690c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY,
2691c42a7b7eSSam Leffler 			"%s: beacon queue %u did not stop?\n",
2692c42a7b7eSSam Leffler 			__func__, sc->sc_bhalq);
26935591b213SSam Leffler 	}
26945591b213SSam Leffler 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
26955591b213SSam Leffler 
2696c42a7b7eSSam Leffler 	/*
2697c42a7b7eSSam Leffler 	 * Enable the CAB queue before the beacon queue to
2698c42a7b7eSSam Leffler 	 * insure cab frames are triggered by this beacon.
2699c42a7b7eSSam Leffler 	 */
2700f3af83f7SSam Leffler 	if (sc->sc_boff.bo_tim_len && (sc->sc_boff.bo_tim[4] & 1)) {
2701f3af83f7SSam Leffler 		/* NB: only at DTIM */
2702622b3fd2SSam Leffler 		ATH_TXQ_LOCK(cabq);
2703622b3fd2SSam Leffler 		ATH_TXQ_LOCK(&sc->sc_mcastq);
2704622b3fd2SSam Leffler 		if (nmcastq) {
2705622b3fd2SSam Leffler 			struct ath_buf *bfm;
2706622b3fd2SSam Leffler 
2707622b3fd2SSam Leffler 			/*
2708622b3fd2SSam Leffler 			 * Move frames from the s/w mcast q to the h/w cab q.
2709622b3fd2SSam Leffler 			 */
2710622b3fd2SSam Leffler 			bfm = STAILQ_FIRST(&sc->sc_mcastq.axq_q);
2711622b3fd2SSam Leffler 			if (cabq->axq_link != NULL) {
2712622b3fd2SSam Leffler 				*cabq->axq_link = bfm->bf_daddr;
2713622b3fd2SSam Leffler 			} else
2714622b3fd2SSam Leffler 				ath_hal_puttxbuf(ah, cabq->axq_qnum,
2715622b3fd2SSam Leffler 					bfm->bf_daddr);
2716622b3fd2SSam Leffler 			ath_txqmove(cabq, &sc->sc_mcastq);
2717622b3fd2SSam Leffler 
2718622b3fd2SSam Leffler 			sc->sc_stats.ast_cabq_xmit += nmcastq;
2719622b3fd2SSam Leffler 		}
2720622b3fd2SSam Leffler 		/* NB: gated by beacon so safe to start here */
2721622b3fd2SSam Leffler 		ath_hal_txstart(ah, cabq->axq_qnum);
2722622b3fd2SSam Leffler 		ATH_TXQ_UNLOCK(cabq);
2723622b3fd2SSam Leffler 		ATH_TXQ_UNLOCK(&sc->sc_mcastq);
2724622b3fd2SSam Leffler 	}
27255591b213SSam Leffler 	ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr);
27265591b213SSam Leffler 	ath_hal_txstart(ah, sc->sc_bhalq);
2727c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_BEACON_PROC,
2728c42a7b7eSSam Leffler 		"%s: TXDP[%u] = %p (%p)\n", __func__,
2729c42a7b7eSSam Leffler 		sc->sc_bhalq, (caddr_t)bf->bf_daddr, bf->bf_desc);
2730c42a7b7eSSam Leffler 
2731c42a7b7eSSam Leffler 	sc->sc_stats.ast_be_xmit++;
27325591b213SSam Leffler }
27335591b213SSam Leffler 
2734c42a7b7eSSam Leffler /*
2735c42a7b7eSSam Leffler  * Reset the hardware after detecting beacons have stopped.
2736c42a7b7eSSam Leffler  */
2737c42a7b7eSSam Leffler static void
2738c42a7b7eSSam Leffler ath_bstuck_proc(void *arg, int pending)
2739c42a7b7eSSam Leffler {
2740c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
2741fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
2742c42a7b7eSSam Leffler 
2743c42a7b7eSSam Leffler 	if_printf(ifp, "stuck beacon; resetting (bmiss count %u)\n",
2744c42a7b7eSSam Leffler 		sc->sc_bmisscount);
2745c42a7b7eSSam Leffler 	ath_reset(ifp);
2746c42a7b7eSSam Leffler }
2747c42a7b7eSSam Leffler 
2748c42a7b7eSSam Leffler /*
2749c42a7b7eSSam Leffler  * Reclaim beacon resources.
2750c42a7b7eSSam Leffler  */
27515591b213SSam Leffler static void
27525591b213SSam Leffler ath_beacon_free(struct ath_softc *sc)
27535591b213SSam Leffler {
2754c42a7b7eSSam Leffler 	struct ath_buf *bf;
27555591b213SSam Leffler 
2756f818612bSSam Leffler 	STAILQ_FOREACH(bf, &sc->sc_bbuf, bf_list) {
27575591b213SSam Leffler 		if (bf->bf_m != NULL) {
27585591b213SSam Leffler 			bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
27595591b213SSam Leffler 			m_freem(bf->bf_m);
27605591b213SSam Leffler 			bf->bf_m = NULL;
2761f818612bSSam Leffler 		}
2762f818612bSSam Leffler 		if (bf->bf_node != NULL) {
2763f818612bSSam Leffler 			ieee80211_free_node(bf->bf_node);
27645591b213SSam Leffler 			bf->bf_node = NULL;
27655591b213SSam Leffler 		}
27665591b213SSam Leffler 	}
2767f818612bSSam Leffler }
27685591b213SSam Leffler 
27695591b213SSam Leffler /*
27705591b213SSam Leffler  * Configure the beacon and sleep timers.
27715591b213SSam Leffler  *
27725591b213SSam Leffler  * When operating as an AP this resets the TSF and sets
27735591b213SSam Leffler  * up the hardware to notify us when we need to issue beacons.
27745591b213SSam Leffler  *
27755591b213SSam Leffler  * When operating in station mode this sets up the beacon
27765591b213SSam Leffler  * timers according to the timestamp of the last received
27775591b213SSam Leffler  * beacon and the current TSF, configures PCF and DTIM
27785591b213SSam Leffler  * handling, programs the sleep registers so the hardware
27795591b213SSam Leffler  * will wakeup in time to receive beacons, and configures
27805591b213SSam Leffler  * the beacon miss handling so we'll receive a BMISS
27815591b213SSam Leffler  * interrupt when we stop seeing beacons from the AP
27825591b213SSam Leffler  * we've associated with.
27835591b213SSam Leffler  */
27845591b213SSam Leffler static void
27855591b213SSam Leffler ath_beacon_config(struct ath_softc *sc)
27865591b213SSam Leffler {
278780d939bfSSam Leffler #define	TSF_TO_TU(_h,_l) \
278880d939bfSSam Leffler 	((((u_int32_t)(_h)) << 22) | (((u_int32_t)(_l)) >> 10))
278980d939bfSSam Leffler #define	FUDGE	2
27905591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
27915591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
27925591b213SSam Leffler 	struct ieee80211_node *ni = ic->ic_bss;
279380d939bfSSam Leffler 	u_int32_t nexttbtt, intval, tsftu;
279480d939bfSSam Leffler 	u_int64_t tsf;
27955591b213SSam Leffler 
27968371372bSSam Leffler 	/* extract tstamp from last beacon and convert to TU */
27978371372bSSam Leffler 	nexttbtt = TSF_TO_TU(LE_READ_4(ni->ni_tstamp.data + 4),
27988371372bSSam Leffler 			     LE_READ_4(ni->ni_tstamp.data));
27998371372bSSam Leffler 	/* NB: the beacon interval is kept internally in TU's */
28004bacf7c1SSam Leffler 	intval = ni->ni_intval & HAL_BEACON_PERIOD;
2801a6c992f4SSam Leffler 	if (nexttbtt == 0)		/* e.g. for ap mode */
2802a6c992f4SSam Leffler 		nexttbtt = intval;
2803a6c992f4SSam Leffler 	else if (intval)		/* NB: can be 0 for monitor mode */
2804a6c992f4SSam Leffler 		nexttbtt = roundup(nexttbtt, intval);
2805a6c992f4SSam Leffler 	DPRINTF(sc, ATH_DEBUG_BEACON, "%s: nexttbtt %u intval %u (%u)\n",
2806a6c992f4SSam Leffler 		__func__, nexttbtt, intval, ni->ni_intval);
28076b59f5e3SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA) {
28085591b213SSam Leffler 		HAL_BEACON_STATE bs;
28098371372bSSam Leffler 		int dtimperiod, dtimcount;
28108371372bSSam Leffler 		int cfpperiod, cfpcount;
28115591b213SSam Leffler 
28128371372bSSam Leffler 		/*
28138371372bSSam Leffler 		 * Setup dtim and cfp parameters according to
28148371372bSSam Leffler 		 * last beacon we received (which may be none).
28158371372bSSam Leffler 		 */
28168371372bSSam Leffler 		dtimperiod = ni->ni_dtim_period;
28178371372bSSam Leffler 		if (dtimperiod <= 0)		/* NB: 0 if not known */
28188371372bSSam Leffler 			dtimperiod = 1;
28198371372bSSam Leffler 		dtimcount = ni->ni_dtim_count;
28208371372bSSam Leffler 		if (dtimcount >= dtimperiod)	/* NB: sanity check */
28218371372bSSam Leffler 			dtimcount = 0;		/* XXX? */
28228371372bSSam Leffler 		cfpperiod = 1;			/* NB: no PCF support yet */
28238371372bSSam Leffler 		cfpcount = 0;
28248371372bSSam Leffler 		/*
28258371372bSSam Leffler 		 * Pull nexttbtt forward to reflect the current
28268371372bSSam Leffler 		 * TSF and calculate dtim+cfp state for the result.
28278371372bSSam Leffler 		 */
28288371372bSSam Leffler 		tsf = ath_hal_gettsf64(ah);
282980d939bfSSam Leffler 		tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
28308371372bSSam Leffler 		do {
28318371372bSSam Leffler 			nexttbtt += intval;
28328371372bSSam Leffler 			if (--dtimcount < 0) {
28338371372bSSam Leffler 				dtimcount = dtimperiod - 1;
28348371372bSSam Leffler 				if (--cfpcount < 0)
28358371372bSSam Leffler 					cfpcount = cfpperiod - 1;
28368371372bSSam Leffler 			}
28378371372bSSam Leffler 		} while (nexttbtt < tsftu);
28385591b213SSam Leffler 		memset(&bs, 0, sizeof(bs));
2839a6c992f4SSam Leffler 		bs.bs_intval = intval;
28405591b213SSam Leffler 		bs.bs_nexttbtt = nexttbtt;
28418371372bSSam Leffler 		bs.bs_dtimperiod = dtimperiod*intval;
28428371372bSSam Leffler 		bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval;
28438371372bSSam Leffler 		bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod;
28448371372bSSam Leffler 		bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod;
28458371372bSSam Leffler 		bs.bs_cfpmaxduration = 0;
28468371372bSSam Leffler #if 0
28475591b213SSam Leffler 		/*
2848c42a7b7eSSam Leffler 		 * The 802.11 layer records the offset to the DTIM
2849c42a7b7eSSam Leffler 		 * bitmap while receiving beacons; use it here to
2850c42a7b7eSSam Leffler 		 * enable h/w detection of our AID being marked in
2851c42a7b7eSSam Leffler 		 * the bitmap vector (to indicate frames for us are
2852c42a7b7eSSam Leffler 		 * pending at the AP).
28538371372bSSam Leffler 		 * XXX do DTIM handling in s/w to WAR old h/w bugs
28548371372bSSam Leffler 		 * XXX enable based on h/w rev for newer chips
2855c42a7b7eSSam Leffler 		 */
2856c42a7b7eSSam Leffler 		bs.bs_timoffset = ni->ni_timoff;
28578371372bSSam Leffler #endif
2858c42a7b7eSSam Leffler 		/*
28595591b213SSam Leffler 		 * Calculate the number of consecutive beacons to miss
286068e8e04eSSam Leffler 		 * before taking a BMISS interrupt.
28615591b213SSam Leffler 		 * Note that we clamp the result to at most 10 beacons.
28625591b213SSam Leffler 		 */
2863b9919097SSam Leffler 		bs.bs_bmissthreshold = ic->ic_bmissthreshold;
28645591b213SSam Leffler 		if (bs.bs_bmissthreshold > 10)
28655591b213SSam Leffler 			bs.bs_bmissthreshold = 10;
28665591b213SSam Leffler 		else if (bs.bs_bmissthreshold <= 0)
28675591b213SSam Leffler 			bs.bs_bmissthreshold = 1;
28685591b213SSam Leffler 
28695591b213SSam Leffler 		/*
28705591b213SSam Leffler 		 * Calculate sleep duration.  The configuration is
28715591b213SSam Leffler 		 * given in ms.  We insure a multiple of the beacon
28725591b213SSam Leffler 		 * period is used.  Also, if the sleep duration is
28735591b213SSam Leffler 		 * greater than the DTIM period then it makes senses
28745591b213SSam Leffler 		 * to make it a multiple of that.
28755591b213SSam Leffler 		 *
28765591b213SSam Leffler 		 * XXX fixed at 100ms
28775591b213SSam Leffler 		 */
28784bacf7c1SSam Leffler 		bs.bs_sleepduration =
28794bacf7c1SSam Leffler 			roundup(IEEE80211_MS_TO_TU(100), bs.bs_intval);
28805591b213SSam Leffler 		if (bs.bs_sleepduration > bs.bs_dtimperiod)
28815591b213SSam Leffler 			bs.bs_sleepduration = roundup(bs.bs_sleepduration, bs.bs_dtimperiod);
28825591b213SSam Leffler 
2883c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
28848371372bSSam Leffler 			"%s: tsf %ju tsf:tu %u intval %u nexttbtt %u dtim %u nextdtim %u bmiss %u sleep %u cfp:period %u maxdur %u next %u timoffset %u\n"
28855591b213SSam Leffler 			, __func__
28868371372bSSam Leffler 			, tsf, tsftu
28875591b213SSam Leffler 			, bs.bs_intval
28885591b213SSam Leffler 			, bs.bs_nexttbtt
28895591b213SSam Leffler 			, bs.bs_dtimperiod
28905591b213SSam Leffler 			, bs.bs_nextdtim
28915591b213SSam Leffler 			, bs.bs_bmissthreshold
28925591b213SSam Leffler 			, bs.bs_sleepduration
2893c42a7b7eSSam Leffler 			, bs.bs_cfpperiod
2894c42a7b7eSSam Leffler 			, bs.bs_cfpmaxduration
2895c42a7b7eSSam Leffler 			, bs.bs_cfpnext
2896c42a7b7eSSam Leffler 			, bs.bs_timoffset
2897c42a7b7eSSam Leffler 		);
28985591b213SSam Leffler 		ath_hal_intrset(ah, 0);
2899c42a7b7eSSam Leffler 		ath_hal_beacontimers(ah, &bs);
29005591b213SSam Leffler 		sc->sc_imask |= HAL_INT_BMISS;
29015591b213SSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
29025591b213SSam Leffler 	} else {
29035591b213SSam Leffler 		ath_hal_intrset(ah, 0);
2904a6c992f4SSam Leffler 		if (nexttbtt == intval)
2905c42a7b7eSSam Leffler 			intval |= HAL_BEACON_RESET_TSF;
2906c42a7b7eSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS) {
2907c42a7b7eSSam Leffler 			/*
2908c42a7b7eSSam Leffler 			 * In IBSS mode enable the beacon timers but only
2909c42a7b7eSSam Leffler 			 * enable SWBA interrupts if we need to manually
2910c42a7b7eSSam Leffler 			 * prepare beacon frames.  Otherwise we use a
2911c42a7b7eSSam Leffler 			 * self-linked tx descriptor and let the hardware
2912c42a7b7eSSam Leffler 			 * deal with things.
2913c42a7b7eSSam Leffler 			 */
2914c42a7b7eSSam Leffler 			intval |= HAL_BEACON_ENA;
2915c42a7b7eSSam Leffler 			if (!sc->sc_hasveol)
2916c42a7b7eSSam Leffler 				sc->sc_imask |= HAL_INT_SWBA;
291780d939bfSSam Leffler 			if ((intval & HAL_BEACON_RESET_TSF) == 0) {
291880d939bfSSam Leffler 				/*
291980d939bfSSam Leffler 				 * Pull nexttbtt forward to reflect
292080d939bfSSam Leffler 				 * the current TSF.
292180d939bfSSam Leffler 				 */
292280d939bfSSam Leffler 				tsf = ath_hal_gettsf64(ah);
292380d939bfSSam Leffler 				tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
292480d939bfSSam Leffler 				do {
292580d939bfSSam Leffler 					nexttbtt += intval;
292680d939bfSSam Leffler 				} while (nexttbtt < tsftu);
292780d939bfSSam Leffler 			}
29280f2e86fbSSam Leffler 			ath_beaconq_config(sc);
2929c42a7b7eSSam Leffler 		} else if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
2930c42a7b7eSSam Leffler 			/*
2931c42a7b7eSSam Leffler 			 * In AP mode we enable the beacon timers and
2932c42a7b7eSSam Leffler 			 * SWBA interrupts to prepare beacon frames.
2933c42a7b7eSSam Leffler 			 */
2934c42a7b7eSSam Leffler 			intval |= HAL_BEACON_ENA;
29355591b213SSam Leffler 			sc->sc_imask |= HAL_INT_SWBA;	/* beacon prepare */
29360f2e86fbSSam Leffler 			ath_beaconq_config(sc);
2937c42a7b7eSSam Leffler 		}
2938c42a7b7eSSam Leffler 		ath_hal_beaconinit(ah, nexttbtt, intval);
2939c42a7b7eSSam Leffler 		sc->sc_bmisscount = 0;
29405591b213SSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
2941c42a7b7eSSam Leffler 		/*
2942c42a7b7eSSam Leffler 		 * When using a self-linked beacon descriptor in
2943c42a7b7eSSam Leffler 		 * ibss mode load it once here.
2944c42a7b7eSSam Leffler 		 */
2945c42a7b7eSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_hasveol)
2946c42a7b7eSSam Leffler 			ath_beacon_proc(sc, 0);
29475591b213SSam Leffler 	}
294880d939bfSSam Leffler 	sc->sc_syncbeacon = 0;
294980d939bfSSam Leffler #undef FUDGE
29508371372bSSam Leffler #undef TSF_TO_TU
29515591b213SSam Leffler }
29525591b213SSam Leffler 
29535591b213SSam Leffler static void
29545591b213SSam Leffler ath_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
29555591b213SSam Leffler {
29565591b213SSam Leffler 	bus_addr_t *paddr = (bus_addr_t*) arg;
2957d77367bfSSam Leffler 	KASSERT(error == 0, ("error %u on bus_dma callback", error));
29585591b213SSam Leffler 	*paddr = segs->ds_addr;
29595591b213SSam Leffler }
29605591b213SSam Leffler 
29615591b213SSam Leffler static int
2962c42a7b7eSSam Leffler ath_descdma_setup(struct ath_softc *sc,
2963c42a7b7eSSam Leffler 	struct ath_descdma *dd, ath_bufhead *head,
2964c42a7b7eSSam Leffler 	const char *name, int nbuf, int ndesc)
2965c42a7b7eSSam Leffler {
2966c42a7b7eSSam Leffler #define	DS2PHYS(_dd, _ds) \
2967c42a7b7eSSam Leffler 	((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
2968fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
2969c42a7b7eSSam Leffler 	struct ath_desc *ds;
2970c42a7b7eSSam Leffler 	struct ath_buf *bf;
2971c42a7b7eSSam Leffler 	int i, bsize, error;
2972c42a7b7eSSam Leffler 
2973c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA: %u buffers %u desc/buf\n",
2974c42a7b7eSSam Leffler 	    __func__, name, nbuf, ndesc);
2975c42a7b7eSSam Leffler 
2976c42a7b7eSSam Leffler 	dd->dd_name = name;
2977c42a7b7eSSam Leffler 	dd->dd_desc_len = sizeof(struct ath_desc) * nbuf * ndesc;
2978c42a7b7eSSam Leffler 
2979c42a7b7eSSam Leffler 	/*
2980c42a7b7eSSam Leffler 	 * Setup DMA descriptor area.
2981c42a7b7eSSam Leffler 	 */
2982c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev),	/* parent */
2983c42a7b7eSSam Leffler 		       PAGE_SIZE, 0,		/* alignment, bounds */
2984c42a7b7eSSam Leffler 		       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
2985c42a7b7eSSam Leffler 		       BUS_SPACE_MAXADDR,	/* highaddr */
2986c42a7b7eSSam Leffler 		       NULL, NULL,		/* filter, filterarg */
2987c42a7b7eSSam Leffler 		       dd->dd_desc_len,		/* maxsize */
2988c42a7b7eSSam Leffler 		       1,			/* nsegments */
29896ccb8ea7SSam Leffler 		       dd->dd_desc_len,		/* maxsegsize */
2990c42a7b7eSSam Leffler 		       BUS_DMA_ALLOCNOW,	/* flags */
2991c42a7b7eSSam Leffler 		       NULL,			/* lockfunc */
2992c42a7b7eSSam Leffler 		       NULL,			/* lockarg */
2993c42a7b7eSSam Leffler 		       &dd->dd_dmat);
2994c42a7b7eSSam Leffler 	if (error != 0) {
2995c42a7b7eSSam Leffler 		if_printf(ifp, "cannot allocate %s DMA tag\n", dd->dd_name);
2996c42a7b7eSSam Leffler 		return error;
2997c42a7b7eSSam Leffler 	}
2998c42a7b7eSSam Leffler 
2999c42a7b7eSSam Leffler 	/* allocate descriptors */
3000c42a7b7eSSam Leffler 	error = bus_dmamap_create(dd->dd_dmat, BUS_DMA_NOWAIT, &dd->dd_dmamap);
3001c42a7b7eSSam Leffler 	if (error != 0) {
3002c42a7b7eSSam Leffler 		if_printf(ifp, "unable to create dmamap for %s descriptors, "
3003c42a7b7eSSam Leffler 			"error %u\n", dd->dd_name, error);
3004c42a7b7eSSam Leffler 		goto fail0;
3005c42a7b7eSSam Leffler 	}
3006c42a7b7eSSam Leffler 
3007c42a7b7eSSam Leffler 	error = bus_dmamem_alloc(dd->dd_dmat, (void**) &dd->dd_desc,
30080553a01fSSam Leffler 				 BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
30090553a01fSSam Leffler 				 &dd->dd_dmamap);
3010c42a7b7eSSam Leffler 	if (error != 0) {
3011c42a7b7eSSam Leffler 		if_printf(ifp, "unable to alloc memory for %u %s descriptors, "
3012c42a7b7eSSam Leffler 			"error %u\n", nbuf * ndesc, dd->dd_name, error);
3013c42a7b7eSSam Leffler 		goto fail1;
3014c42a7b7eSSam Leffler 	}
3015c42a7b7eSSam Leffler 
3016c42a7b7eSSam Leffler 	error = bus_dmamap_load(dd->dd_dmat, dd->dd_dmamap,
3017c42a7b7eSSam Leffler 				dd->dd_desc, dd->dd_desc_len,
3018c42a7b7eSSam Leffler 				ath_load_cb, &dd->dd_desc_paddr,
3019c42a7b7eSSam Leffler 				BUS_DMA_NOWAIT);
3020c42a7b7eSSam Leffler 	if (error != 0) {
3021c42a7b7eSSam Leffler 		if_printf(ifp, "unable to map %s descriptors, error %u\n",
3022c42a7b7eSSam Leffler 			dd->dd_name, error);
3023c42a7b7eSSam Leffler 		goto fail2;
3024c42a7b7eSSam Leffler 	}
3025c42a7b7eSSam Leffler 
3026c42a7b7eSSam Leffler 	ds = dd->dd_desc;
3027c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA map: %p (%lu) -> %p (%lu)\n",
3028c42a7b7eSSam Leffler 	    __func__, dd->dd_name, ds, (u_long) dd->dd_desc_len,
3029c42a7b7eSSam Leffler 	    (caddr_t) dd->dd_desc_paddr, /*XXX*/ (u_long) dd->dd_desc_len);
3030c42a7b7eSSam Leffler 
3031ebecf802SSam Leffler 	/* allocate rx buffers */
3032c42a7b7eSSam Leffler 	bsize = sizeof(struct ath_buf) * nbuf;
3033c42a7b7eSSam Leffler 	bf = malloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO);
3034c42a7b7eSSam Leffler 	if (bf == NULL) {
3035c42a7b7eSSam Leffler 		if_printf(ifp, "malloc of %s buffers failed, size %u\n",
3036c42a7b7eSSam Leffler 			dd->dd_name, bsize);
3037c42a7b7eSSam Leffler 		goto fail3;
3038c42a7b7eSSam Leffler 	}
3039c42a7b7eSSam Leffler 	dd->dd_bufptr = bf;
3040c42a7b7eSSam Leffler 
3041c42a7b7eSSam Leffler 	STAILQ_INIT(head);
3042c42a7b7eSSam Leffler 	for (i = 0; i < nbuf; i++, bf++, ds += ndesc) {
3043c42a7b7eSSam Leffler 		bf->bf_desc = ds;
3044c42a7b7eSSam Leffler 		bf->bf_daddr = DS2PHYS(dd, ds);
3045c42a7b7eSSam Leffler 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
3046c42a7b7eSSam Leffler 				&bf->bf_dmamap);
3047c42a7b7eSSam Leffler 		if (error != 0) {
3048c42a7b7eSSam Leffler 			if_printf(ifp, "unable to create dmamap for %s "
3049c42a7b7eSSam Leffler 				"buffer %u, error %u\n", dd->dd_name, i, error);
3050c42a7b7eSSam Leffler 			ath_descdma_cleanup(sc, dd, head);
3051c42a7b7eSSam Leffler 			return error;
3052c42a7b7eSSam Leffler 		}
3053c42a7b7eSSam Leffler 		STAILQ_INSERT_TAIL(head, bf, bf_list);
3054c42a7b7eSSam Leffler 	}
3055c42a7b7eSSam Leffler 	return 0;
3056c42a7b7eSSam Leffler fail3:
3057c42a7b7eSSam Leffler 	bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
3058c42a7b7eSSam Leffler fail2:
3059c42a7b7eSSam Leffler 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3060c42a7b7eSSam Leffler fail1:
3061c42a7b7eSSam Leffler 	bus_dmamap_destroy(dd->dd_dmat, dd->dd_dmamap);
3062c42a7b7eSSam Leffler fail0:
3063c42a7b7eSSam Leffler 	bus_dma_tag_destroy(dd->dd_dmat);
3064c42a7b7eSSam Leffler 	memset(dd, 0, sizeof(*dd));
3065c42a7b7eSSam Leffler 	return error;
3066c42a7b7eSSam Leffler #undef DS2PHYS
3067c42a7b7eSSam Leffler }
3068c42a7b7eSSam Leffler 
3069c42a7b7eSSam Leffler static void
3070c42a7b7eSSam Leffler ath_descdma_cleanup(struct ath_softc *sc,
3071c42a7b7eSSam Leffler 	struct ath_descdma *dd, ath_bufhead *head)
3072c42a7b7eSSam Leffler {
3073c42a7b7eSSam Leffler 	struct ath_buf *bf;
3074c42a7b7eSSam Leffler 	struct ieee80211_node *ni;
3075c42a7b7eSSam Leffler 
3076c42a7b7eSSam Leffler 	bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
3077c42a7b7eSSam Leffler 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3078c42a7b7eSSam Leffler 	bus_dmamap_destroy(dd->dd_dmat, dd->dd_dmamap);
3079c42a7b7eSSam Leffler 	bus_dma_tag_destroy(dd->dd_dmat);
3080c42a7b7eSSam Leffler 
3081c42a7b7eSSam Leffler 	STAILQ_FOREACH(bf, head, bf_list) {
3082c42a7b7eSSam Leffler 		if (bf->bf_m) {
3083c42a7b7eSSam Leffler 			m_freem(bf->bf_m);
3084c42a7b7eSSam Leffler 			bf->bf_m = NULL;
3085c42a7b7eSSam Leffler 		}
3086c42a7b7eSSam Leffler 		if (bf->bf_dmamap != NULL) {
3087c42a7b7eSSam Leffler 			bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
3088c42a7b7eSSam Leffler 			bf->bf_dmamap = NULL;
3089c42a7b7eSSam Leffler 		}
3090c42a7b7eSSam Leffler 		ni = bf->bf_node;
3091c42a7b7eSSam Leffler 		bf->bf_node = NULL;
3092c42a7b7eSSam Leffler 		if (ni != NULL) {
3093c42a7b7eSSam Leffler 			/*
3094c42a7b7eSSam Leffler 			 * Reclaim node reference.
3095c42a7b7eSSam Leffler 			 */
3096c42a7b7eSSam Leffler 			ieee80211_free_node(ni);
3097c42a7b7eSSam Leffler 		}
3098c42a7b7eSSam Leffler 	}
3099c42a7b7eSSam Leffler 
3100c42a7b7eSSam Leffler 	STAILQ_INIT(head);
3101c42a7b7eSSam Leffler 	free(dd->dd_bufptr, M_ATHDEV);
3102c42a7b7eSSam Leffler 	memset(dd, 0, sizeof(*dd));
3103c42a7b7eSSam Leffler }
3104c42a7b7eSSam Leffler 
3105c42a7b7eSSam Leffler static int
31065591b213SSam Leffler ath_desc_alloc(struct ath_softc *sc)
31075591b213SSam Leffler {
3108c42a7b7eSSam Leffler 	int error;
31095591b213SSam Leffler 
3110c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_rxdma, &sc->sc_rxbuf,
3111e2d787faSSam Leffler 			"rx", ath_rxbuf, 1);
31125591b213SSam Leffler 	if (error != 0)
31135591b213SSam Leffler 		return error;
31145591b213SSam Leffler 
3115c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_txdma, &sc->sc_txbuf,
3116e2d787faSSam Leffler 			"tx", ath_txbuf, ATH_TXDESC);
3117c42a7b7eSSam Leffler 	if (error != 0) {
3118c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
31195591b213SSam Leffler 		return error;
3120c42a7b7eSSam Leffler 	}
3121c42a7b7eSSam Leffler 
3122c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_bdma, &sc->sc_bbuf,
3123c42a7b7eSSam Leffler 			"beacon", 1, 1);
3124c42a7b7eSSam Leffler 	if (error != 0) {
3125c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3126c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
3127c42a7b7eSSam Leffler 		return error;
3128c42a7b7eSSam Leffler 	}
31295591b213SSam Leffler 	return 0;
31305591b213SSam Leffler }
31315591b213SSam Leffler 
31325591b213SSam Leffler static void
31335591b213SSam Leffler ath_desc_free(struct ath_softc *sc)
31345591b213SSam Leffler {
31355591b213SSam Leffler 
3136c42a7b7eSSam Leffler 	if (sc->sc_bdma.dd_desc_len != 0)
3137c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_bdma, &sc->sc_bbuf);
3138c42a7b7eSSam Leffler 	if (sc->sc_txdma.dd_desc_len != 0)
3139c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3140c42a7b7eSSam Leffler 	if (sc->sc_rxdma.dd_desc_len != 0)
3141c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
31425591b213SSam Leffler }
31435591b213SSam Leffler 
31445591b213SSam Leffler static struct ieee80211_node *
3145c42a7b7eSSam Leffler ath_node_alloc(struct ieee80211_node_table *nt)
31465591b213SSam Leffler {
3147c42a7b7eSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
3148c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
3149c42a7b7eSSam Leffler 	const size_t space = sizeof(struct ath_node) + sc->sc_rc->arc_space;
3150c42a7b7eSSam Leffler 	struct ath_node *an;
3151c42a7b7eSSam Leffler 
3152c42a7b7eSSam Leffler 	an = malloc(space, M_80211_NODE, M_NOWAIT|M_ZERO);
3153c42a7b7eSSam Leffler 	if (an == NULL) {
3154c42a7b7eSSam Leffler 		/* XXX stat+msg */
3155de5af704SSam Leffler 		return NULL;
31565591b213SSam Leffler 	}
3157c42a7b7eSSam Leffler 	an->an_avgrssi = ATH_RSSI_DUMMY_MARKER;
3158c42a7b7eSSam Leffler 	ath_rate_node_init(sc, an);
31595591b213SSam Leffler 
3160c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: an %p\n", __func__, an);
3161c42a7b7eSSam Leffler 	return &an->an_node;
3162c42a7b7eSSam Leffler }
3163c42a7b7eSSam Leffler 
31645591b213SSam Leffler static void
3165c42a7b7eSSam Leffler ath_node_free(struct ieee80211_node *ni)
31665591b213SSam Leffler {
3167c42a7b7eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
3168c42a7b7eSSam Leffler         struct ath_softc *sc = ic->ic_ifp->if_softc;
31691e774079SSam Leffler 
3170c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: ni %p\n", __func__, ni);
3171c42a7b7eSSam Leffler 
3172c42a7b7eSSam Leffler 	ath_rate_node_cleanup(sc, ATH_NODE(ni));
3173c42a7b7eSSam Leffler 	sc->sc_node_free(ni);
31745591b213SSam Leffler }
31755591b213SSam Leffler 
317668e8e04eSSam Leffler static int8_t
3177c42a7b7eSSam Leffler ath_node_getrssi(const struct ieee80211_node *ni)
3178de5af704SSam Leffler {
3179c42a7b7eSSam Leffler #define	HAL_EP_RND(x, mul) \
3180c42a7b7eSSam Leffler 	((((x)%(mul)) >= ((mul)/2)) ? ((x) + ((mul) - 1)) / (mul) : (x)/(mul))
3181c42a7b7eSSam Leffler 	u_int32_t avgrssi = ATH_NODE_CONST(ni)->an_avgrssi;
3182c42a7b7eSSam Leffler 	int32_t rssi;
3183de5af704SSam Leffler 
3184de5af704SSam Leffler 	/*
3185c42a7b7eSSam Leffler 	 * When only one frame is received there will be no state in
3186c42a7b7eSSam Leffler 	 * avgrssi so fallback on the value recorded by the 802.11 layer.
3187de5af704SSam Leffler 	 */
3188c42a7b7eSSam Leffler 	if (avgrssi != ATH_RSSI_DUMMY_MARKER)
3189c42a7b7eSSam Leffler 		rssi = HAL_EP_RND(avgrssi, HAL_RSSI_EP_MULTIPLIER);
3190de5af704SSam Leffler 	else
3191c42a7b7eSSam Leffler 		rssi = ni->ni_rssi;
3192c42a7b7eSSam Leffler 	return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
3193c42a7b7eSSam Leffler #undef HAL_EP_RND
3194de5af704SSam Leffler }
3195de5af704SSam Leffler 
319668e8e04eSSam Leffler static void
319768e8e04eSSam Leffler ath_node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
319868e8e04eSSam Leffler {
319968e8e04eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
320068e8e04eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
320168e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
320268e8e04eSSam Leffler 	HAL_CHANNEL hchan;
320368e8e04eSSam Leffler 
320468e8e04eSSam Leffler 	*rssi = ath_node_getrssi(ni);
320568e8e04eSSam Leffler 	if (ni->ni_chan != IEEE80211_CHAN_ANYC) {
320668e8e04eSSam Leffler 		ath_mapchan(&hchan, ni->ni_chan);
320768e8e04eSSam Leffler 		*noise = ath_hal_getchannoise(ah, &hchan);
320868e8e04eSSam Leffler 	} else
320968e8e04eSSam Leffler 		*noise = -95;		/* nominally correct */
321068e8e04eSSam Leffler }
321168e8e04eSSam Leffler 
32125591b213SSam Leffler static int
32135591b213SSam Leffler ath_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf)
32145591b213SSam Leffler {
32155591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
32165591b213SSam Leffler 	int error;
32175591b213SSam Leffler 	struct mbuf *m;
32185591b213SSam Leffler 	struct ath_desc *ds;
32195591b213SSam Leffler 
32205591b213SSam Leffler 	m = bf->bf_m;
32215591b213SSam Leffler 	if (m == NULL) {
32225591b213SSam Leffler 		/*
32235591b213SSam Leffler 		 * NB: by assigning a page to the rx dma buffer we
32245591b213SSam Leffler 		 * implicitly satisfy the Atheros requirement that
32255591b213SSam Leffler 		 * this buffer be cache-line-aligned and sized to be
32265591b213SSam Leffler 		 * multiple of the cache line size.  Not doing this
32275591b213SSam Leffler 		 * causes weird stuff to happen (for the 5210 at least).
32285591b213SSam Leffler 		 */
32295591b213SSam Leffler 		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
32305591b213SSam Leffler 		if (m == NULL) {
3231c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
3232c42a7b7eSSam Leffler 				"%s: no mbuf/cluster\n", __func__);
32335591b213SSam Leffler 			sc->sc_stats.ast_rx_nombuf++;
32345591b213SSam Leffler 			return ENOMEM;
32355591b213SSam Leffler 		}
32365591b213SSam Leffler 		m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
32375591b213SSam Leffler 
3238f9e6219bSSam Leffler 		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat,
3239c42a7b7eSSam Leffler 					     bf->bf_dmamap, m,
3240f9e6219bSSam Leffler 					     bf->bf_segs, &bf->bf_nseg,
32415591b213SSam Leffler 					     BUS_DMA_NOWAIT);
32425591b213SSam Leffler 		if (error != 0) {
3243c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
3244f9e6219bSSam Leffler 			    "%s: bus_dmamap_load_mbuf_sg failed; error %d\n",
3245c42a7b7eSSam Leffler 			    __func__, error);
32465591b213SSam Leffler 			sc->sc_stats.ast_rx_busdma++;
3247b2792ff6SSam Leffler 			m_freem(m);
32485591b213SSam Leffler 			return error;
32495591b213SSam Leffler 		}
3250d77367bfSSam Leffler 		KASSERT(bf->bf_nseg == 1,
3251d77367bfSSam Leffler 			("multi-segment packet; nseg %u", bf->bf_nseg));
3252b2792ff6SSam Leffler 		bf->bf_m = m;
32535591b213SSam Leffler 	}
32545591b213SSam Leffler 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREREAD);
32555591b213SSam Leffler 
325604e22a02SSam Leffler 	/*
325704e22a02SSam Leffler 	 * Setup descriptors.  For receive we always terminate
325804e22a02SSam Leffler 	 * the descriptor list with a self-linked entry so we'll
325904e22a02SSam Leffler 	 * not get overrun under high load (as can happen with a
3260c42a7b7eSSam Leffler 	 * 5212 when ANI processing enables PHY error frames).
326104e22a02SSam Leffler 	 *
326204e22a02SSam Leffler 	 * To insure the last descriptor is self-linked we create
326304e22a02SSam Leffler 	 * each descriptor as self-linked and add it to the end.  As
326404e22a02SSam Leffler 	 * each additional descriptor is added the previous self-linked
326504e22a02SSam Leffler 	 * entry is ``fixed'' naturally.  This should be safe even
326604e22a02SSam Leffler 	 * if DMA is happening.  When processing RX interrupts we
326704e22a02SSam Leffler 	 * never remove/process the last, self-linked, entry on the
326804e22a02SSam Leffler 	 * descriptor list.  This insures the hardware always has
326904e22a02SSam Leffler 	 * someplace to write a new frame.
327004e22a02SSam Leffler 	 */
32715591b213SSam Leffler 	ds = bf->bf_desc;
327204e22a02SSam Leffler 	ds->ds_link = bf->bf_daddr;	/* link to self */
32735591b213SSam Leffler 	ds->ds_data = bf->bf_segs[0].ds_addr;
32745591b213SSam Leffler 	ath_hal_setuprxdesc(ah, ds
32755591b213SSam Leffler 		, m->m_len		/* buffer size */
32765591b213SSam Leffler 		, 0
32775591b213SSam Leffler 	);
32785591b213SSam Leffler 
32795591b213SSam Leffler 	if (sc->sc_rxlink != NULL)
32805591b213SSam Leffler 		*sc->sc_rxlink = bf->bf_daddr;
32815591b213SSam Leffler 	sc->sc_rxlink = &ds->ds_link;
32825591b213SSam Leffler 	return 0;
32835591b213SSam Leffler }
32845591b213SSam Leffler 
3285c42a7b7eSSam Leffler /*
328603ed599aSSam Leffler  * Extend 15-bit time stamp from rx descriptor to
32877b0c77ecSSam Leffler  * a full 64-bit TSF using the specified TSF.
328803ed599aSSam Leffler  */
328903ed599aSSam Leffler static __inline u_int64_t
32907b0c77ecSSam Leffler ath_extend_tsf(u_int32_t rstamp, u_int64_t tsf)
329103ed599aSSam Leffler {
329203ed599aSSam Leffler 	if ((tsf & 0x7fff) < rstamp)
329303ed599aSSam Leffler 		tsf -= 0x8000;
329403ed599aSSam Leffler 	return ((tsf &~ 0x7fff) | rstamp);
329503ed599aSSam Leffler }
329603ed599aSSam Leffler 
329703ed599aSSam Leffler /*
3298c42a7b7eSSam Leffler  * Intercept management frames to collect beacon rssi data
3299c42a7b7eSSam Leffler  * and to do ibss merges.
3300c42a7b7eSSam Leffler  */
3301c42a7b7eSSam Leffler static void
3302c42a7b7eSSam Leffler ath_recv_mgmt(struct ieee80211com *ic, struct mbuf *m,
3303c42a7b7eSSam Leffler 	struct ieee80211_node *ni,
330468e8e04eSSam Leffler 	int subtype, int rssi, int noise, u_int32_t rstamp)
3305c42a7b7eSSam Leffler {
3306c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
3307c42a7b7eSSam Leffler 
3308c42a7b7eSSam Leffler 	/*
3309c42a7b7eSSam Leffler 	 * Call up first so subsequent work can use information
3310c42a7b7eSSam Leffler 	 * potentially stored in the node (e.g. for ibss merge).
3311c42a7b7eSSam Leffler 	 */
331268e8e04eSSam Leffler 	sc->sc_recv_mgmt(ic, m, ni, subtype, rssi, noise, rstamp);
3313c42a7b7eSSam Leffler 	switch (subtype) {
3314c42a7b7eSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON:
3315c42a7b7eSSam Leffler 		/* update rssi statistics for use by the hal */
3316ffa2cab6SSam Leffler 		ATH_RSSI_LPF(sc->sc_halstats.ns_avgbrssi, rssi);
331780d939bfSSam Leffler 		if (sc->sc_syncbeacon &&
331880d939bfSSam Leffler 		    ni == ic->ic_bss && ic->ic_state == IEEE80211_S_RUN) {
331980d939bfSSam Leffler 			/*
332080d939bfSSam Leffler 			 * Resync beacon timers using the tsf of the beacon
332180d939bfSSam Leffler 			 * frame we just received.
332280d939bfSSam Leffler 			 */
332380d939bfSSam Leffler 			ath_beacon_config(sc);
332480d939bfSSam Leffler 		}
3325c42a7b7eSSam Leffler 		/* fall thru... */
3326c42a7b7eSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
3327c42a7b7eSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS &&
3328c42a7b7eSSam Leffler 		    ic->ic_state == IEEE80211_S_RUN) {
33297b0c77ecSSam Leffler 			u_int64_t tsf = ath_extend_tsf(rstamp,
33307b0c77ecSSam Leffler 				ath_hal_gettsf64(sc->sc_ah));
3331c42a7b7eSSam Leffler 			/*
3332c42a7b7eSSam Leffler 			 * Handle ibss merge as needed; check the tsf on the
3333c42a7b7eSSam Leffler 			 * frame before attempting the merge.  The 802.11 spec
3334c42a7b7eSSam Leffler 			 * says the station should change it's bssid to match
3335c42a7b7eSSam Leffler 			 * the oldest station with the same ssid, where oldest
3336f818612bSSam Leffler 			 * is determined by the tsf.  Note that hardware
3337f818612bSSam Leffler 			 * reconfiguration happens through callback to
333803ed599aSSam Leffler 			 * ath_newstate as the state machine will go from
333903ed599aSSam Leffler 			 * RUN -> RUN when this happens.
3340c42a7b7eSSam Leffler 			 */
334103ed599aSSam Leffler 			if (le64toh(ni->ni_tstamp.tsf) >= tsf) {
334203ed599aSSam Leffler 				DPRINTF(sc, ATH_DEBUG_STATE,
334333d7d80cSTai-hwa Liang 				    "ibss merge, rstamp %u tsf %ju "
334433d7d80cSTai-hwa Liang 				    "tstamp %ju\n", rstamp, (uintmax_t)tsf,
334533d7d80cSTai-hwa Liang 				    (uintmax_t)ni->ni_tstamp.tsf);
3346641b4d0bSSam Leffler 				(void) ieee80211_ibss_merge(ni);
3347c42a7b7eSSam Leffler 			}
334803ed599aSSam Leffler 		}
3349c42a7b7eSSam Leffler 		break;
3350c42a7b7eSSam Leffler 	}
3351c42a7b7eSSam Leffler }
3352c42a7b7eSSam Leffler 
3353c42a7b7eSSam Leffler /*
3354c42a7b7eSSam Leffler  * Set the default antenna.
3355c42a7b7eSSam Leffler  */
3356c42a7b7eSSam Leffler static void
3357c42a7b7eSSam Leffler ath_setdefantenna(struct ath_softc *sc, u_int antenna)
3358c42a7b7eSSam Leffler {
3359c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3360c42a7b7eSSam Leffler 
3361c42a7b7eSSam Leffler 	/* XXX block beacon interrupts */
3362c42a7b7eSSam Leffler 	ath_hal_setdefantenna(ah, antenna);
3363c42a7b7eSSam Leffler 	if (sc->sc_defant != antenna)
3364c42a7b7eSSam Leffler 		sc->sc_stats.ast_ant_defswitch++;
3365c42a7b7eSSam Leffler 	sc->sc_defant = antenna;
3366c42a7b7eSSam Leffler 	sc->sc_rxotherant = 0;
3367c42a7b7eSSam Leffler }
3368c42a7b7eSSam Leffler 
33697b0c77ecSSam Leffler static int
33707b0c77ecSSam Leffler ath_rx_tap(struct ath_softc *sc, struct mbuf *m,
337165f9edeeSSam Leffler 	const struct ath_rx_status *rs, u_int64_t tsf, int16_t nf)
33727b0c77ecSSam Leffler {
337368e8e04eSSam Leffler #define	CHANNEL_HT	(CHANNEL_HT20|CHANNEL_HT40PLUS|CHANNEL_HT40MINUS)
33747b0c77ecSSam Leffler 	u_int8_t rix;
33757b0c77ecSSam Leffler 
33767b0c77ecSSam Leffler 	KASSERT(sc->sc_drvbpf != NULL, ("no tap"));
33777b0c77ecSSam Leffler 
33787b0c77ecSSam Leffler 	/*
33797b0c77ecSSam Leffler 	 * Discard anything shorter than an ack or cts.
33807b0c77ecSSam Leffler 	 */
33817b0c77ecSSam Leffler 	if (m->m_pkthdr.len < IEEE80211_ACK_LEN) {
33827b0c77ecSSam Leffler 		DPRINTF(sc, ATH_DEBUG_RECV, "%s: runt packet %d\n",
33837b0c77ecSSam Leffler 			__func__, m->m_pkthdr.len);
33847b0c77ecSSam Leffler 		sc->sc_stats.ast_rx_tooshort++;
33857b0c77ecSSam Leffler 		return 0;
33867b0c77ecSSam Leffler 	}
338765f9edeeSSam Leffler 	rix = rs->rs_rate;
338868e8e04eSSam Leffler 	sc->sc_rx_th.wr_rate = sc->sc_hwmap[rix].ieeerate;
33897b0c77ecSSam Leffler 	sc->sc_rx_th.wr_flags = sc->sc_hwmap[rix].rxflags;
339068e8e04eSSam Leffler #if HAL_ABI_VERSION >= 0x07050400
339168e8e04eSSam Leffler 	if (sc->sc_curchan.channelFlags & CHANNEL_HT) {
339268e8e04eSSam Leffler 		/*
339368e8e04eSSam Leffler 		 * For HT operation we must specify the channel
339468e8e04eSSam Leffler 		 * attributes for each packet since they vary.
339568e8e04eSSam Leffler 		 * We deduce this by from HT40 bit in the rx
339668e8e04eSSam Leffler 		 * status and the MCS/legacy rate bit.
339768e8e04eSSam Leffler 		 */
339868e8e04eSSam Leffler 		sc->sc_rx_th.wr_chan_flags &= ~IEEE80211_CHAN_HT;
339968e8e04eSSam Leffler 		if (sc->sc_rx_th.wr_rate & 0x80) {	/* HT rate */
340068e8e04eSSam Leffler 			/* XXX 40U/40D */
340168e8e04eSSam Leffler 			sc->sc_rx_th.wr_chan_flags |=
340268e8e04eSSam Leffler 			    (rs->rs_flags & HAL_RX_2040) ?
340368e8e04eSSam Leffler 				IEEE80211_CHAN_HT40U : IEEE80211_CHAN_HT20;
340468e8e04eSSam Leffler 			if ((rs->rs_flags & HAL_RX_GI) == 0)
340568e8e04eSSam Leffler 				sc->sc_rx_th.wr_flags |=
340668e8e04eSSam Leffler 				    IEEE80211_RADIOTAP_F_SHORTGI;
340768e8e04eSSam Leffler 		}
340868e8e04eSSam Leffler 	}
340968e8e04eSSam Leffler #endif
341068e8e04eSSam Leffler 	sc->sc_rx_th.wr_tsf = htole64(ath_extend_tsf(rs->rs_tstamp, tsf));
341165f9edeeSSam Leffler 	if (rs->rs_status & HAL_RXERR_CRC)
34127b0c77ecSSam Leffler 		sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
34137b0c77ecSSam Leffler 	/* XXX propagate other error flags from descriptor */
341465f9edeeSSam Leffler 	sc->sc_rx_th.wr_antsignal = rs->rs_rssi + nf;
34157b0c77ecSSam Leffler 	sc->sc_rx_th.wr_antnoise = nf;
341665f9edeeSSam Leffler 	sc->sc_rx_th.wr_antenna = rs->rs_antenna;
34177b0c77ecSSam Leffler 
34187b0c77ecSSam Leffler 	bpf_mtap2(sc->sc_drvbpf, &sc->sc_rx_th, sc->sc_rx_th_len, m);
34197b0c77ecSSam Leffler 
34207b0c77ecSSam Leffler 	return 1;
342168e8e04eSSam Leffler #undef CHANNEL_HT
34227b0c77ecSSam Leffler }
34237b0c77ecSSam Leffler 
34245591b213SSam Leffler static void
34255591b213SSam Leffler ath_rx_proc(void *arg, int npending)
34265591b213SSam Leffler {
34278cec0ab9SSam Leffler #define	PA2DESC(_sc, _pa) \
3428c42a7b7eSSam Leffler 	((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
3429c42a7b7eSSam Leffler 		((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
34305591b213SSam Leffler 	struct ath_softc *sc = arg;
34315591b213SSam Leffler 	struct ath_buf *bf;
3432d1d0cf62SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
3433fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
34345591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
34355591b213SSam Leffler 	struct ath_desc *ds;
343665f9edeeSSam Leffler 	struct ath_rx_status *rs;
34375591b213SSam Leffler 	struct mbuf *m;
34380a915fadSSam Leffler 	struct ieee80211_node *ni;
3439de5af704SSam Leffler 	struct ath_node *an;
3440d7736e13SSam Leffler 	int len, type, ngood;
34415591b213SSam Leffler 	u_int phyerr;
34425591b213SSam Leffler 	HAL_STATUS status;
34437b0c77ecSSam Leffler 	int16_t nf;
34447b0c77ecSSam Leffler 	u_int64_t tsf;
34455591b213SSam Leffler 
3446b5f4adb3SSam Leffler 
3447c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RX_PROC, "%s: pending %u\n", __func__, npending);
3448d7736e13SSam Leffler 	ngood = 0;
34497b0c77ecSSam Leffler 	nf = ath_hal_getchannoise(ah, &sc->sc_curchan);
34507b0c77ecSSam Leffler 	tsf = ath_hal_gettsf64(ah);
34515591b213SSam Leffler 	do {
3452c42a7b7eSSam Leffler 		bf = STAILQ_FIRST(&sc->sc_rxbuf);
34535591b213SSam Leffler 		if (bf == NULL) {		/* NB: shouldn't happen */
3454c42a7b7eSSam Leffler 			if_printf(ifp, "%s: no buffer!\n", __func__);
34555591b213SSam Leffler 			break;
34565591b213SSam Leffler 		}
3457b2792ff6SSam Leffler 		m = bf->bf_m;
3458b2792ff6SSam Leffler 		if (m == NULL) {		/* NB: shouldn't happen */
3459b2792ff6SSam Leffler 			/*
3460b2792ff6SSam Leffler 			 * If mbuf allocation failed previously there
3461b2792ff6SSam Leffler 			 * will be no mbuf; try again to re-populate it.
3462b2792ff6SSam Leffler 			 */
3463b2792ff6SSam Leffler 			/* XXX make debug msg */
3464b2792ff6SSam Leffler 			if_printf(ifp, "%s: no mbuf!\n", __func__);
3465b2792ff6SSam Leffler 			STAILQ_REMOVE_HEAD(&sc->sc_rxbuf, bf_list);
3466b2792ff6SSam Leffler 			goto rx_next;
3467b2792ff6SSam Leffler 		}
346804e22a02SSam Leffler 		ds = bf->bf_desc;
346904e22a02SSam Leffler 		if (ds->ds_link == bf->bf_daddr) {
347004e22a02SSam Leffler 			/* NB: never process the self-linked entry at the end */
347104e22a02SSam Leffler 			break;
347204e22a02SSam Leffler 		}
34738cec0ab9SSam Leffler 		/* XXX sync descriptor memory */
34748cec0ab9SSam Leffler 		/*
34758cec0ab9SSam Leffler 		 * Must provide the virtual address of the current
34768cec0ab9SSam Leffler 		 * descriptor, the physical address, and the virtual
34778cec0ab9SSam Leffler 		 * address of the next descriptor in the h/w chain.
34788cec0ab9SSam Leffler 		 * This allows the HAL to look ahead to see if the
34798cec0ab9SSam Leffler 		 * hardware is done with a descriptor by checking the
34808cec0ab9SSam Leffler 		 * done bit in the following descriptor and the address
34818cec0ab9SSam Leffler 		 * of the current descriptor the DMA engine is working
34828cec0ab9SSam Leffler 		 * on.  All this is necessary because of our use of
34838cec0ab9SSam Leffler 		 * a self-linked list to avoid rx overruns.
34848cec0ab9SSam Leffler 		 */
348565f9edeeSSam Leffler 		rs = &bf->bf_status.ds_rxstat;
34868cec0ab9SSam Leffler 		status = ath_hal_rxprocdesc(ah, ds,
348765f9edeeSSam Leffler 				bf->bf_daddr, PA2DESC(sc, ds->ds_link), rs);
3488a585a9a1SSam Leffler #ifdef ATH_DEBUG
3489c42a7b7eSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_RECV_DESC)
34907a4c5ed9SSam Leffler 			ath_printrxbuf(bf, 0, status == HAL_OK);
34915591b213SSam Leffler #endif
34925591b213SSam Leffler 		if (status == HAL_EINPROGRESS)
34935591b213SSam Leffler 			break;
3494c42a7b7eSSam Leffler 		STAILQ_REMOVE_HEAD(&sc->sc_rxbuf, bf_list);
349568e8e04eSSam Leffler 		if (rs->rs_status != 0) {
349665f9edeeSSam Leffler 			if (rs->rs_status & HAL_RXERR_CRC)
34975591b213SSam Leffler 				sc->sc_stats.ast_rx_crcerr++;
349865f9edeeSSam Leffler 			if (rs->rs_status & HAL_RXERR_FIFO)
34995591b213SSam Leffler 				sc->sc_stats.ast_rx_fifoerr++;
350065f9edeeSSam Leffler 			if (rs->rs_status & HAL_RXERR_PHY) {
35015591b213SSam Leffler 				sc->sc_stats.ast_rx_phyerr++;
350265f9edeeSSam Leffler 				phyerr = rs->rs_phyerr & 0x1f;
35035591b213SSam Leffler 				sc->sc_stats.ast_rx_phy[phyerr]++;
350468e8e04eSSam Leffler 				goto rx_error;	/* NB: don't count in ierrors */
3505c42a7b7eSSam Leffler 			}
350665f9edeeSSam Leffler 			if (rs->rs_status & HAL_RXERR_DECRYPT) {
350785643802SSam Leffler 				/*
3508c42a7b7eSSam Leffler 				 * Decrypt error.  If the error occurred
3509c42a7b7eSSam Leffler 				 * because there was no hardware key, then
3510c42a7b7eSSam Leffler 				 * let the frame through so the upper layers
3511c42a7b7eSSam Leffler 				 * can process it.  This is necessary for 5210
3512c42a7b7eSSam Leffler 				 * parts which have no way to setup a ``clear''
3513c42a7b7eSSam Leffler 				 * key cache entry.
3514c42a7b7eSSam Leffler 				 *
3515c42a7b7eSSam Leffler 				 * XXX do key cache faulting
351685643802SSam Leffler 				 */
351765f9edeeSSam Leffler 				if (rs->rs_keyix == HAL_RXKEYIX_INVALID)
3518c42a7b7eSSam Leffler 					goto rx_accept;
3519c42a7b7eSSam Leffler 				sc->sc_stats.ast_rx_badcrypt++;
35205591b213SSam Leffler 			}
352165f9edeeSSam Leffler 			if (rs->rs_status & HAL_RXERR_MIC) {
3522c42a7b7eSSam Leffler 				sc->sc_stats.ast_rx_badmic++;
3523c42a7b7eSSam Leffler 				/*
3524c42a7b7eSSam Leffler 				 * Do minimal work required to hand off
3525c42a7b7eSSam Leffler 				 * the 802.11 header for notifcation.
3526c42a7b7eSSam Leffler 				 */
3527c42a7b7eSSam Leffler 				/* XXX frag's and qos frames */
352865f9edeeSSam Leffler 				len = rs->rs_datalen;
3529c42a7b7eSSam Leffler 				if (len >= sizeof (struct ieee80211_frame)) {
3530c42a7b7eSSam Leffler 					bus_dmamap_sync(sc->sc_dmat,
3531c42a7b7eSSam Leffler 					    bf->bf_dmamap,
3532c42a7b7eSSam Leffler 					    BUS_DMASYNC_POSTREAD);
3533c42a7b7eSSam Leffler 					ieee80211_notify_michael_failure(ic,
3534c42a7b7eSSam Leffler 					    mtod(m, struct ieee80211_frame *),
35350ab4040aSSam Leffler 					    sc->sc_splitmic ?
353665f9edeeSSam Leffler 					        rs->rs_keyix-32 : rs->rs_keyix
35370ab4040aSSam Leffler 					);
3538c42a7b7eSSam Leffler 				}
3539c42a7b7eSSam Leffler 			}
3540c42a7b7eSSam Leffler 			ifp->if_ierrors++;
354168e8e04eSSam Leffler rx_error:
354268e8e04eSSam Leffler 			/*
354368e8e04eSSam Leffler 			 * Cleanup any pending partial frame.
354468e8e04eSSam Leffler 			 */
354568e8e04eSSam Leffler 			if (sc->sc_rxpending != NULL) {
354668e8e04eSSam Leffler 				m_freem(sc->sc_rxpending);
354768e8e04eSSam Leffler 				sc->sc_rxpending = NULL;
354868e8e04eSSam Leffler 			}
3549c42a7b7eSSam Leffler 			/*
35507b0c77ecSSam Leffler 			 * When a tap is present pass error frames
35517b0c77ecSSam Leffler 			 * that have been requested.  By default we
35527b0c77ecSSam Leffler 			 * pass decrypt+mic errors but others may be
35537b0c77ecSSam Leffler 			 * interesting (e.g. crc).
3554c42a7b7eSSam Leffler 			 */
355516d878ccSChristian S.J. Peron 			if (bpf_peers_present(sc->sc_drvbpf) &&
355665f9edeeSSam Leffler 			    (rs->rs_status & sc->sc_monpass)) {
35577b0c77ecSSam Leffler 				bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
35587b0c77ecSSam Leffler 				    BUS_DMASYNC_POSTREAD);
35597b0c77ecSSam Leffler 				/* NB: bpf needs the mbuf length setup */
356065f9edeeSSam Leffler 				len = rs->rs_datalen;
35617b0c77ecSSam Leffler 				m->m_pkthdr.len = m->m_len = len;
356265f9edeeSSam Leffler 				(void) ath_rx_tap(sc, m, rs, tsf, nf);
35637b0c77ecSSam Leffler 			}
35647b0c77ecSSam Leffler 			/* XXX pass MIC errors up for s/w reclaculation */
35655591b213SSam Leffler 			goto rx_next;
35665591b213SSam Leffler 		}
3567c42a7b7eSSam Leffler rx_accept:
3568c42a7b7eSSam Leffler 		/*
3569c42a7b7eSSam Leffler 		 * Sync and unmap the frame.  At this point we're
3570c42a7b7eSSam Leffler 		 * committed to passing the mbuf somewhere so clear
3571c66c48cbSSam Leffler 		 * bf_m; this means a new mbuf must be allocated
3572c42a7b7eSSam Leffler 		 * when the rx descriptor is setup again to receive
3573c42a7b7eSSam Leffler 		 * another frame.
3574c42a7b7eSSam Leffler 		 */
35755591b213SSam Leffler 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
35765591b213SSam Leffler 		    BUS_DMASYNC_POSTREAD);
35775591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
35785591b213SSam Leffler 		bf->bf_m = NULL;
3579c42a7b7eSSam Leffler 
358065f9edeeSSam Leffler 		len = rs->rs_datalen;
358168e8e04eSSam Leffler 		m->m_len = len;
358268e8e04eSSam Leffler 
358368e8e04eSSam Leffler 		if (rs->rs_more) {
358468e8e04eSSam Leffler 			/*
358568e8e04eSSam Leffler 			 * Frame spans multiple descriptors; save
358668e8e04eSSam Leffler 			 * it for the next completed descriptor, it
358768e8e04eSSam Leffler 			 * will be used to construct a jumbogram.
358868e8e04eSSam Leffler 			 */
358968e8e04eSSam Leffler 			if (sc->sc_rxpending != NULL) {
359068e8e04eSSam Leffler 				/* NB: max frame size is currently 2 clusters */
359168e8e04eSSam Leffler 				sc->sc_stats.ast_rx_toobig++;
359268e8e04eSSam Leffler 				m_freem(sc->sc_rxpending);
359368e8e04eSSam Leffler 			}
359468e8e04eSSam Leffler 			m->m_pkthdr.rcvif = ifp;
359568e8e04eSSam Leffler 			m->m_pkthdr.len = len;
359668e8e04eSSam Leffler 			sc->sc_rxpending = m;
359768e8e04eSSam Leffler 			goto rx_next;
359868e8e04eSSam Leffler 		} else if (sc->sc_rxpending != NULL) {
359968e8e04eSSam Leffler 			/*
360068e8e04eSSam Leffler 			 * This is the second part of a jumbogram,
360168e8e04eSSam Leffler 			 * chain it to the first mbuf, adjust the
360268e8e04eSSam Leffler 			 * frame length, and clear the rxpending state.
360368e8e04eSSam Leffler 			 */
360468e8e04eSSam Leffler 			sc->sc_rxpending->m_next = m;
360568e8e04eSSam Leffler 			sc->sc_rxpending->m_pkthdr.len += len;
360668e8e04eSSam Leffler 			m = sc->sc_rxpending;
360768e8e04eSSam Leffler 			sc->sc_rxpending = NULL;
360868e8e04eSSam Leffler 		} else {
360968e8e04eSSam Leffler 			/*
361068e8e04eSSam Leffler 			 * Normal single-descriptor receive; setup
361168e8e04eSSam Leffler 			 * the rcvif and packet length.
361268e8e04eSSam Leffler 			 */
361368e8e04eSSam Leffler 			m->m_pkthdr.rcvif = ifp;
361468e8e04eSSam Leffler 			m->m_pkthdr.len = len;
361568e8e04eSSam Leffler 		}
361673454c73SSam Leffler 
361765f9edeeSSam Leffler 		sc->sc_stats.ast_ant_rx[rs->rs_antenna]++;
3618c42a7b7eSSam Leffler 
361916d878ccSChristian S.J. Peron 		if (bpf_peers_present(sc->sc_drvbpf) &&
362065f9edeeSSam Leffler 		    !ath_rx_tap(sc, m, rs, tsf, nf)) {
36217b0c77ecSSam Leffler 			m_freem(m);		/* XXX reclaim */
3622c42a7b7eSSam Leffler 			goto rx_next;
3623c42a7b7eSSam Leffler 		}
36240a915fadSSam Leffler 
36255591b213SSam Leffler 		/*
3626c42a7b7eSSam Leffler 		 * From this point on we assume the frame is at least
3627c42a7b7eSSam Leffler 		 * as large as ieee80211_frame_min; verify that.
36285591b213SSam Leffler 		 */
3629c42a7b7eSSam Leffler 		if (len < IEEE80211_MIN_LEN) {
3630c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_RECV, "%s: short packet %d\n",
3631c42a7b7eSSam Leffler 				__func__, len);
3632c42a7b7eSSam Leffler 			sc->sc_stats.ast_rx_tooshort++;
3633c42a7b7eSSam Leffler 			m_freem(m);
3634c42a7b7eSSam Leffler 			goto rx_next;
36355591b213SSam Leffler 		}
36360a915fadSSam Leffler 
3637c42a7b7eSSam Leffler 		if (IFF_DUMPPKTS(sc, ATH_DEBUG_RECV)) {
363868e8e04eSSam Leffler 			ieee80211_dump_pkt(ic, mtod(m, caddr_t), len,
363965f9edeeSSam Leffler 				   sc->sc_hwmap[rs->rs_rate].ieeerate,
364065f9edeeSSam Leffler 				   rs->rs_rssi);
3641c42a7b7eSSam Leffler 		}
3642c42a7b7eSSam Leffler 
3643c42a7b7eSSam Leffler 		m_adj(m, -IEEE80211_CRC_LEN);
3644de5af704SSam Leffler 
3645de5af704SSam Leffler 		/*
3646c42a7b7eSSam Leffler 		 * Locate the node for sender, track state, and then
3647c42a7b7eSSam Leffler 		 * pass the (referenced) node up to the 802.11 layer
3648c42a7b7eSSam Leffler 		 * for its use.
3649c42a7b7eSSam Leffler 		 */
3650c1225b52SSam Leffler 		ni = ieee80211_find_rxnode_withkey(ic,
3651c1225b52SSam Leffler 			mtod(m, const struct ieee80211_frame_min *),
365265f9edeeSSam Leffler 			rs->rs_keyix == HAL_RXKEYIX_INVALID ?
365365f9edeeSSam Leffler 				IEEE80211_KEYIX_NONE : rs->rs_keyix);
3654c42a7b7eSSam Leffler 		/*
3655c42a7b7eSSam Leffler 		 * Track rx rssi and do any rx antenna management.
3656de5af704SSam Leffler 		 */
3657de5af704SSam Leffler 		an = ATH_NODE(ni);
365865f9edeeSSam Leffler 		ATH_RSSI_LPF(an->an_avgrssi, rs->rs_rssi);
365965f9edeeSSam Leffler 		ATH_RSSI_LPF(sc->sc_halstats.ns_avgrssi, rs->rs_rssi);
3660e8fd88a3SSam Leffler 		/*
3661e8fd88a3SSam Leffler 		 * Send frame up for processing.
3662e8fd88a3SSam Leffler 		 */
366368e8e04eSSam Leffler 		type = ieee80211_input(ic, m, ni,
366468e8e04eSSam Leffler 			rs->rs_rssi, nf, rs->rs_tstamp);
3665e8fd88a3SSam Leffler 		ieee80211_free_node(ni);
3666c42a7b7eSSam Leffler 		if (sc->sc_diversity) {
3667c42a7b7eSSam Leffler 			/*
3668c42a7b7eSSam Leffler 			 * When using fast diversity, change the default rx
3669c42a7b7eSSam Leffler 			 * antenna if diversity chooses the other antenna 3
3670c42a7b7eSSam Leffler 			 * times in a row.
3671c42a7b7eSSam Leffler 			 */
367265f9edeeSSam Leffler 			if (sc->sc_defant != rs->rs_antenna) {
3673c42a7b7eSSam Leffler 				if (++sc->sc_rxotherant >= 3)
367465f9edeeSSam Leffler 					ath_setdefantenna(sc, rs->rs_antenna);
3675c42a7b7eSSam Leffler 			} else
3676c42a7b7eSSam Leffler 				sc->sc_rxotherant = 0;
3677c42a7b7eSSam Leffler 		}
36783e50ec2cSSam Leffler 		if (sc->sc_softled) {
36793e50ec2cSSam Leffler 			/*
36803e50ec2cSSam Leffler 			 * Blink for any data frame.  Otherwise do a
36813e50ec2cSSam Leffler 			 * heartbeat-style blink when idle.  The latter
36823e50ec2cSSam Leffler 			 * is mainly for station mode where we depend on
36833e50ec2cSSam Leffler 			 * periodic beacon frames to trigger the poll event.
36843e50ec2cSSam Leffler 			 */
368531640eb7SSam Leffler 			if (type == IEEE80211_FC0_TYPE_DATA) {
368665f9edeeSSam Leffler 				sc->sc_rxrate = rs->rs_rate;
36873e50ec2cSSam Leffler 				ath_led_event(sc, ATH_LED_RX);
36883e50ec2cSSam Leffler 			} else if (ticks - sc->sc_ledevent >= sc->sc_ledidle)
36893e50ec2cSSam Leffler 				ath_led_event(sc, ATH_LED_POLL);
36903e50ec2cSSam Leffler 		}
3691d7736e13SSam Leffler 		/*
3692d7736e13SSam Leffler 		 * Arrange to update the last rx timestamp only for
3693d7736e13SSam Leffler 		 * frames from our ap when operating in station mode.
3694d7736e13SSam Leffler 		 * This assumes the rx key is always setup when associated.
3695d7736e13SSam Leffler 		 */
3696d7736e13SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_STA &&
369765f9edeeSSam Leffler 		    rs->rs_keyix != HAL_RXKEYIX_INVALID)
3698d7736e13SSam Leffler 			ngood++;
36995591b213SSam Leffler rx_next:
3700c42a7b7eSSam Leffler 		STAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
37015591b213SSam Leffler 	} while (ath_rxbuf_init(sc, bf) == 0);
37025591b213SSam Leffler 
3703c42a7b7eSSam Leffler 	/* rx signal state monitoring */
3704bd5a9920SSam Leffler 	ath_hal_rxmonitor(ah, &sc->sc_halstats, &sc->sc_curchan);
3705d7736e13SSam Leffler 	if (ngood)
3706d7736e13SSam Leffler 		sc->sc_lastrx = tsf;
3707b5f4adb3SSam Leffler 
3708cd196bb2SSam Leffler 	/* NB: may want to check mgtq too */
3709cd196bb2SSam Leffler 	if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 &&
3710cd196bb2SSam Leffler 	    !IFQ_IS_EMPTY(&ifp->if_snd))
3711cd196bb2SSam Leffler 		ath_start(ifp);
3712cd196bb2SSam Leffler 
37138cec0ab9SSam Leffler #undef PA2DESC
37145591b213SSam Leffler }
37155591b213SSam Leffler 
3716622b3fd2SSam Leffler static void
3717622b3fd2SSam Leffler ath_txq_init(struct ath_softc *sc, struct ath_txq *txq, int qnum)
3718622b3fd2SSam Leffler {
3719622b3fd2SSam Leffler 	txq->axq_qnum = qnum;
3720622b3fd2SSam Leffler 	txq->axq_depth = 0;
3721622b3fd2SSam Leffler 	txq->axq_intrcnt = 0;
3722622b3fd2SSam Leffler 	txq->axq_link = NULL;
3723622b3fd2SSam Leffler 	STAILQ_INIT(&txq->axq_q);
3724622b3fd2SSam Leffler 	ATH_TXQ_LOCK_INIT(sc, txq);
372568e8e04eSSam Leffler 	TAILQ_INIT(&txq->axq_stageq);
372668e8e04eSSam Leffler 	txq->axq_curage = 0;
3727622b3fd2SSam Leffler }
3728622b3fd2SSam Leffler 
37295591b213SSam Leffler /*
3730c42a7b7eSSam Leffler  * Setup a h/w transmit queue.
37315591b213SSam Leffler  */
3732c42a7b7eSSam Leffler static struct ath_txq *
3733c42a7b7eSSam Leffler ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
3734c42a7b7eSSam Leffler {
3735c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
3736c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3737c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
3738c42a7b7eSSam Leffler 	int qnum;
3739c42a7b7eSSam Leffler 
3740c42a7b7eSSam Leffler 	memset(&qi, 0, sizeof(qi));
3741c42a7b7eSSam Leffler 	qi.tqi_subtype = subtype;
3742c42a7b7eSSam Leffler 	qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
3743c42a7b7eSSam Leffler 	qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
3744c42a7b7eSSam Leffler 	qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
3745c42a7b7eSSam Leffler 	/*
3746c42a7b7eSSam Leffler 	 * Enable interrupts only for EOL and DESC conditions.
3747c42a7b7eSSam Leffler 	 * We mark tx descriptors to receive a DESC interrupt
3748c42a7b7eSSam Leffler 	 * when a tx queue gets deep; otherwise waiting for the
3749c42a7b7eSSam Leffler 	 * EOL to reap descriptors.  Note that this is done to
3750c42a7b7eSSam Leffler 	 * reduce interrupt load and this only defers reaping
3751c42a7b7eSSam Leffler 	 * descriptors, never transmitting frames.  Aside from
3752c42a7b7eSSam Leffler 	 * reducing interrupts this also permits more concurrency.
3753c42a7b7eSSam Leffler 	 * The only potential downside is if the tx queue backs
3754c42a7b7eSSam Leffler 	 * up in which case the top half of the kernel may backup
3755c42a7b7eSSam Leffler 	 * due to a lack of tx descriptors.
3756c42a7b7eSSam Leffler 	 */
3757bd5a9920SSam Leffler 	qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE | HAL_TXQ_TXDESCINT_ENABLE;
3758c42a7b7eSSam Leffler 	qnum = ath_hal_setuptxqueue(ah, qtype, &qi);
3759c42a7b7eSSam Leffler 	if (qnum == -1) {
3760c42a7b7eSSam Leffler 		/*
3761c42a7b7eSSam Leffler 		 * NB: don't print a message, this happens
3762a614e076SSam Leffler 		 * normally on parts with too few tx queues
3763c42a7b7eSSam Leffler 		 */
3764c42a7b7eSSam Leffler 		return NULL;
3765c42a7b7eSSam Leffler 	}
3766c42a7b7eSSam Leffler 	if (qnum >= N(sc->sc_txq)) {
37676891c875SPeter Wemm 		device_printf(sc->sc_dev,
37686891c875SPeter Wemm 			"hal qnum %u out of range, max %zu!\n",
3769c42a7b7eSSam Leffler 			qnum, N(sc->sc_txq));
3770c42a7b7eSSam Leffler 		ath_hal_releasetxqueue(ah, qnum);
3771c42a7b7eSSam Leffler 		return NULL;
3772c42a7b7eSSam Leffler 	}
3773c42a7b7eSSam Leffler 	if (!ATH_TXQ_SETUP(sc, qnum)) {
3774622b3fd2SSam Leffler 		ath_txq_init(sc, &sc->sc_txq[qnum], qnum);
3775c42a7b7eSSam Leffler 		sc->sc_txqsetup |= 1<<qnum;
3776c42a7b7eSSam Leffler 	}
3777c42a7b7eSSam Leffler 	return &sc->sc_txq[qnum];
3778c42a7b7eSSam Leffler #undef N
3779c42a7b7eSSam Leffler }
3780c42a7b7eSSam Leffler 
3781c42a7b7eSSam Leffler /*
3782c42a7b7eSSam Leffler  * Setup a hardware data transmit queue for the specified
3783c42a7b7eSSam Leffler  * access control.  The hal may not support all requested
3784c42a7b7eSSam Leffler  * queues in which case it will return a reference to a
3785c42a7b7eSSam Leffler  * previously setup queue.  We record the mapping from ac's
3786c42a7b7eSSam Leffler  * to h/w queues for use by ath_tx_start and also track
3787c42a7b7eSSam Leffler  * the set of h/w queues being used to optimize work in the
3788c42a7b7eSSam Leffler  * transmit interrupt handler and related routines.
3789c42a7b7eSSam Leffler  */
3790c42a7b7eSSam Leffler static int
3791c42a7b7eSSam Leffler ath_tx_setup(struct ath_softc *sc, int ac, int haltype)
3792c42a7b7eSSam Leffler {
3793c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
3794c42a7b7eSSam Leffler 	struct ath_txq *txq;
3795c42a7b7eSSam Leffler 
3796c42a7b7eSSam Leffler 	if (ac >= N(sc->sc_ac2q)) {
37976891c875SPeter Wemm 		device_printf(sc->sc_dev, "AC %u out of range, max %zu!\n",
3798c42a7b7eSSam Leffler 			ac, N(sc->sc_ac2q));
3799c42a7b7eSSam Leffler 		return 0;
3800c42a7b7eSSam Leffler 	}
3801c42a7b7eSSam Leffler 	txq = ath_txq_setup(sc, HAL_TX_QUEUE_DATA, haltype);
3802c42a7b7eSSam Leffler 	if (txq != NULL) {
3803c42a7b7eSSam Leffler 		sc->sc_ac2q[ac] = txq;
3804c42a7b7eSSam Leffler 		return 1;
3805c42a7b7eSSam Leffler 	} else
3806c42a7b7eSSam Leffler 		return 0;
3807c42a7b7eSSam Leffler #undef N
3808c42a7b7eSSam Leffler }
3809c42a7b7eSSam Leffler 
3810c42a7b7eSSam Leffler /*
3811c42a7b7eSSam Leffler  * Update WME parameters for a transmit queue.
3812c42a7b7eSSam Leffler  */
3813c42a7b7eSSam Leffler static int
3814c42a7b7eSSam Leffler ath_txq_update(struct ath_softc *sc, int ac)
3815c42a7b7eSSam Leffler {
3816c42a7b7eSSam Leffler #define	ATH_EXPONENT_TO_VALUE(v)	((1<<v)-1)
3817c42a7b7eSSam Leffler #define	ATH_TXOP_TO_US(v)		(v<<5)
3818c42a7b7eSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
3819c42a7b7eSSam Leffler 	struct ath_txq *txq = sc->sc_ac2q[ac];
3820c42a7b7eSSam Leffler 	struct wmeParams *wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
3821c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3822c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
3823c42a7b7eSSam Leffler 
3824c42a7b7eSSam Leffler 	ath_hal_gettxqueueprops(ah, txq->axq_qnum, &qi);
3825c42a7b7eSSam Leffler 	qi.tqi_aifs = wmep->wmep_aifsn;
3826c42a7b7eSSam Leffler 	qi.tqi_cwmin = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
3827c42a7b7eSSam Leffler 	qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
3828c42a7b7eSSam Leffler 	qi.tqi_burstTime = ATH_TXOP_TO_US(wmep->wmep_txopLimit);
3829c42a7b7eSSam Leffler 
3830c42a7b7eSSam Leffler 	if (!ath_hal_settxqueueprops(ah, txq->axq_qnum, &qi)) {
3831c42a7b7eSSam Leffler 		device_printf(sc->sc_dev, "unable to update hardware queue "
3832c42a7b7eSSam Leffler 			"parameters for %s traffic!\n",
3833c42a7b7eSSam Leffler 			ieee80211_wme_acnames[ac]);
3834c42a7b7eSSam Leffler 		return 0;
3835c42a7b7eSSam Leffler 	} else {
3836c42a7b7eSSam Leffler 		ath_hal_resettxqueue(ah, txq->axq_qnum); /* push to h/w */
3837c42a7b7eSSam Leffler 		return 1;
3838c42a7b7eSSam Leffler 	}
3839c42a7b7eSSam Leffler #undef ATH_TXOP_TO_US
3840c42a7b7eSSam Leffler #undef ATH_EXPONENT_TO_VALUE
3841c42a7b7eSSam Leffler }
3842c42a7b7eSSam Leffler 
3843c42a7b7eSSam Leffler /*
3844c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update WME parameters.
3845c42a7b7eSSam Leffler  */
3846c42a7b7eSSam Leffler static int
3847c42a7b7eSSam Leffler ath_wme_update(struct ieee80211com *ic)
3848c42a7b7eSSam Leffler {
3849c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
3850c42a7b7eSSam Leffler 
3851c42a7b7eSSam Leffler 	return !ath_txq_update(sc, WME_AC_BE) ||
3852c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_BK) ||
3853c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VI) ||
3854c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VO) ? EIO : 0;
3855c42a7b7eSSam Leffler }
3856c42a7b7eSSam Leffler 
3857c42a7b7eSSam Leffler /*
3858c42a7b7eSSam Leffler  * Reclaim resources for a setup queue.
3859c42a7b7eSSam Leffler  */
3860c42a7b7eSSam Leffler static void
3861c42a7b7eSSam Leffler ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
3862c42a7b7eSSam Leffler {
3863c42a7b7eSSam Leffler 
3864c42a7b7eSSam Leffler 	ath_hal_releasetxqueue(sc->sc_ah, txq->axq_qnum);
3865c42a7b7eSSam Leffler 	ATH_TXQ_LOCK_DESTROY(txq);
3866c42a7b7eSSam Leffler 	sc->sc_txqsetup &= ~(1<<txq->axq_qnum);
3867c42a7b7eSSam Leffler }
3868c42a7b7eSSam Leffler 
3869c42a7b7eSSam Leffler /*
3870c42a7b7eSSam Leffler  * Reclaim all tx queue resources.
3871c42a7b7eSSam Leffler  */
3872c42a7b7eSSam Leffler static void
3873c42a7b7eSSam Leffler ath_tx_cleanup(struct ath_softc *sc)
3874c42a7b7eSSam Leffler {
3875c42a7b7eSSam Leffler 	int i;
3876c42a7b7eSSam Leffler 
3877c42a7b7eSSam Leffler 	ATH_TXBUF_LOCK_DESTROY(sc);
3878c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
3879c42a7b7eSSam Leffler 		if (ATH_TXQ_SETUP(sc, i))
3880c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, &sc->sc_txq[i]);
3881622b3fd2SSam Leffler 	ATH_TXQ_LOCK_DESTROY(&sc->sc_mcastq);
3882c42a7b7eSSam Leffler }
38835591b213SSam Leffler 
388499d258fdSSam Leffler /*
388599d258fdSSam Leffler  * Defragment an mbuf chain, returning at most maxfrags separate
388699d258fdSSam Leffler  * mbufs+clusters.  If this is not possible NULL is returned and
3887a7073e8bSSam Leffler  * the original mbuf chain is left in it's present (potentially
3888a7073e8bSSam Leffler  * modified) state.  We use two techniques: collapsing consecutive
3889a7073e8bSSam Leffler  * mbufs and replacing consecutive mbufs by a cluster.
389099d258fdSSam Leffler  */
389199d258fdSSam Leffler static struct mbuf *
389299d258fdSSam Leffler ath_defrag(struct mbuf *m0, int how, int maxfrags)
389399d258fdSSam Leffler {
389499d258fdSSam Leffler 	struct mbuf *m, *n, *n2, **prev;
389599d258fdSSam Leffler 	u_int curfrags;
389699d258fdSSam Leffler 
389799d258fdSSam Leffler 	/*
389899d258fdSSam Leffler 	 * Calculate the current number of frags.
389999d258fdSSam Leffler 	 */
390099d258fdSSam Leffler 	curfrags = 0;
390199d258fdSSam Leffler 	for (m = m0; m != NULL; m = m->m_next)
390299d258fdSSam Leffler 		curfrags++;
390399d258fdSSam Leffler 	/*
390499d258fdSSam Leffler 	 * First, try to collapse mbufs.  Note that we always collapse
390599d258fdSSam Leffler 	 * towards the front so we don't need to deal with moving the
390699d258fdSSam Leffler 	 * pkthdr.  This may be suboptimal if the first mbuf has much
390799d258fdSSam Leffler 	 * less data than the following.
390899d258fdSSam Leffler 	 */
390999d258fdSSam Leffler 	m = m0;
391099d258fdSSam Leffler again:
391199d258fdSSam Leffler 	for (;;) {
391299d258fdSSam Leffler 		n = m->m_next;
391399d258fdSSam Leffler 		if (n == NULL)
391499d258fdSSam Leffler 			break;
3915019b9669SSam Leffler 		if ((m->m_flags & M_RDONLY) == 0 &&
3916019b9669SSam Leffler 		    n->m_len < M_TRAILINGSPACE(m)) {
391799d258fdSSam Leffler 			bcopy(mtod(n, void *), mtod(m, char *) + m->m_len,
391899d258fdSSam Leffler 				n->m_len);
391999d258fdSSam Leffler 			m->m_len += n->m_len;
392099d258fdSSam Leffler 			m->m_next = n->m_next;
392199d258fdSSam Leffler 			m_free(n);
392299d258fdSSam Leffler 			if (--curfrags <= maxfrags)
392399d258fdSSam Leffler 				return m0;
392499d258fdSSam Leffler 		} else
392599d258fdSSam Leffler 			m = n;
392699d258fdSSam Leffler 	}
392799d258fdSSam Leffler 	KASSERT(maxfrags > 1,
392899d258fdSSam Leffler 		("maxfrags %u, but normal collapse failed", maxfrags));
392999d258fdSSam Leffler 	/*
393099d258fdSSam Leffler 	 * Collapse consecutive mbufs to a cluster.
393199d258fdSSam Leffler 	 */
393299d258fdSSam Leffler 	prev = &m0->m_next;		/* NB: not the first mbuf */
393399d258fdSSam Leffler 	while ((n = *prev) != NULL) {
393499d258fdSSam Leffler 		if ((n2 = n->m_next) != NULL &&
393599d258fdSSam Leffler 		    n->m_len + n2->m_len < MCLBYTES) {
393699d258fdSSam Leffler 			m = m_getcl(how, MT_DATA, 0);
393799d258fdSSam Leffler 			if (m == NULL)
393899d258fdSSam Leffler 				goto bad;
393999d258fdSSam Leffler 			bcopy(mtod(n, void *), mtod(m, void *), n->m_len);
394099d258fdSSam Leffler 			bcopy(mtod(n2, void *), mtod(m, char *) + n->m_len,
394199d258fdSSam Leffler 				n2->m_len);
394299d258fdSSam Leffler 			m->m_len = n->m_len + n2->m_len;
394399d258fdSSam Leffler 			m->m_next = n2->m_next;
394499d258fdSSam Leffler 			*prev = m;
394599d258fdSSam Leffler 			m_free(n);
394699d258fdSSam Leffler 			m_free(n2);
394799d258fdSSam Leffler 			if (--curfrags <= maxfrags)	/* +1 cl -2 mbufs */
394899d258fdSSam Leffler 				return m0;
394999d258fdSSam Leffler 			/*
395099d258fdSSam Leffler 			 * Still not there, try the normal collapse
395199d258fdSSam Leffler 			 * again before we allocate another cluster.
395299d258fdSSam Leffler 			 */
395399d258fdSSam Leffler 			goto again;
395499d258fdSSam Leffler 		}
395599d258fdSSam Leffler 		prev = &n->m_next;
395699d258fdSSam Leffler 	}
395799d258fdSSam Leffler 	/*
395899d258fdSSam Leffler 	 * No place where we can collapse to a cluster; punt.
395999d258fdSSam Leffler 	 * This can occur if, for example, you request 2 frags
396099d258fdSSam Leffler 	 * but the packet requires that both be clusters (we
396199d258fdSSam Leffler 	 * never reallocate the first mbuf to avoid moving the
396299d258fdSSam Leffler 	 * packet header).
396399d258fdSSam Leffler 	 */
396499d258fdSSam Leffler bad:
396599d258fdSSam Leffler 	return NULL;
396699d258fdSSam Leffler }
396799d258fdSSam Leffler 
39688b5341deSSam Leffler /*
39698b5341deSSam Leffler  * Return h/w rate index for an IEEE rate (w/o basic rate bit).
39708b5341deSSam Leffler  */
39718b5341deSSam Leffler static int
39728b5341deSSam Leffler ath_tx_findrix(const HAL_RATE_TABLE *rt, int rate)
39738b5341deSSam Leffler {
39748b5341deSSam Leffler 	int i;
39758b5341deSSam Leffler 
39768b5341deSSam Leffler 	for (i = 0; i < rt->rateCount; i++)
39778b5341deSSam Leffler 		if ((rt->info[i].dot11Rate & IEEE80211_RATE_VAL) == rate)
39788b5341deSSam Leffler 			return i;
39798b5341deSSam Leffler 	return 0;		/* NB: lowest rate */
39808b5341deSSam Leffler }
39818b5341deSSam Leffler 
398268e8e04eSSam Leffler /*
398368e8e04eSSam Leffler  * Reclaim mbuf resources.  For fragmented frames we
398468e8e04eSSam Leffler  * need to claim each frag chained with m_nextpkt.
398568e8e04eSSam Leffler  */
398668e8e04eSSam Leffler static void
398768e8e04eSSam Leffler ath_freetx(struct mbuf *m)
398868e8e04eSSam Leffler {
398968e8e04eSSam Leffler 	struct mbuf *next;
399068e8e04eSSam Leffler 
399168e8e04eSSam Leffler 	do {
399268e8e04eSSam Leffler 		next = m->m_nextpkt;
399368e8e04eSSam Leffler 		m->m_nextpkt = NULL;
399468e8e04eSSam Leffler 		m_freem(m);
399568e8e04eSSam Leffler 	} while ((m = next) != NULL);
399668e8e04eSSam Leffler }
399768e8e04eSSam Leffler 
39985591b213SSam Leffler static int
3999664443d0SSam Leffler ath_tx_dmasetup(struct ath_softc *sc, struct ath_buf *bf, struct mbuf *m0)
4000664443d0SSam Leffler {
4001664443d0SSam Leffler 	struct mbuf *m;
4002664443d0SSam Leffler 	int error;
4003664443d0SSam Leffler 
4004664443d0SSam Leffler 	/*
4005664443d0SSam Leffler 	 * Load the DMA map so any coalescing is done.  This
4006664443d0SSam Leffler 	 * also calculates the number of descriptors we need.
4007664443d0SSam Leffler 	 */
4008664443d0SSam Leffler 	error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0,
4009664443d0SSam Leffler 				     bf->bf_segs, &bf->bf_nseg,
4010664443d0SSam Leffler 				     BUS_DMA_NOWAIT);
4011664443d0SSam Leffler 	if (error == EFBIG) {
4012664443d0SSam Leffler 		/* XXX packet requires too many descriptors */
4013664443d0SSam Leffler 		bf->bf_nseg = ATH_TXDESC+1;
4014664443d0SSam Leffler 	} else if (error != 0) {
4015664443d0SSam Leffler 		sc->sc_stats.ast_tx_busdma++;
401668e8e04eSSam Leffler 		ath_freetx(m0);
4017664443d0SSam Leffler 		return error;
4018664443d0SSam Leffler 	}
4019664443d0SSam Leffler 	/*
4020664443d0SSam Leffler 	 * Discard null packets and check for packets that
4021664443d0SSam Leffler 	 * require too many TX descriptors.  We try to convert
4022664443d0SSam Leffler 	 * the latter to a cluster.
4023664443d0SSam Leffler 	 */
4024664443d0SSam Leffler 	if (bf->bf_nseg > ATH_TXDESC) {		/* too many desc's, linearize */
4025664443d0SSam Leffler 		sc->sc_stats.ast_tx_linear++;
4026664443d0SSam Leffler 		m = ath_defrag(m0, M_DONTWAIT, ATH_TXDESC);
4027664443d0SSam Leffler 		if (m == NULL) {
402868e8e04eSSam Leffler 			ath_freetx(m0);
4029664443d0SSam Leffler 			sc->sc_stats.ast_tx_nombuf++;
4030664443d0SSam Leffler 			return ENOMEM;
4031664443d0SSam Leffler 		}
4032664443d0SSam Leffler 		m0 = m;
4033664443d0SSam Leffler 		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0,
4034664443d0SSam Leffler 					     bf->bf_segs, &bf->bf_nseg,
4035664443d0SSam Leffler 					     BUS_DMA_NOWAIT);
4036664443d0SSam Leffler 		if (error != 0) {
4037664443d0SSam Leffler 			sc->sc_stats.ast_tx_busdma++;
403868e8e04eSSam Leffler 			ath_freetx(m0);
4039664443d0SSam Leffler 			return error;
4040664443d0SSam Leffler 		}
4041664443d0SSam Leffler 		KASSERT(bf->bf_nseg <= ATH_TXDESC,
4042664443d0SSam Leffler 		    ("too many segments after defrag; nseg %u", bf->bf_nseg));
4043664443d0SSam Leffler 	} else if (bf->bf_nseg == 0) {		/* null packet, discard */
4044664443d0SSam Leffler 		sc->sc_stats.ast_tx_nodata++;
404568e8e04eSSam Leffler 		ath_freetx(m0);
4046664443d0SSam Leffler 		return EIO;
4047664443d0SSam Leffler 	}
4048664443d0SSam Leffler 	DPRINTF(sc, ATH_DEBUG_XMIT, "%s: m %p len %u\n",
4049664443d0SSam Leffler 		__func__, m0, m0->m_pkthdr.len);
4050664443d0SSam Leffler 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
4051664443d0SSam Leffler 	bf->bf_m = m0;
4052664443d0SSam Leffler 
4053664443d0SSam Leffler 	return 0;
4054664443d0SSam Leffler }
4055664443d0SSam Leffler 
4056664443d0SSam Leffler static void
4057664443d0SSam Leffler ath_tx_handoff(struct ath_softc *sc, struct ath_txq *txq, struct ath_buf *bf)
4058664443d0SSam Leffler {
4059664443d0SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4060664443d0SSam Leffler 	struct ath_desc *ds, *ds0;
4061664443d0SSam Leffler 	int i;
4062664443d0SSam Leffler 
4063664443d0SSam Leffler 	/*
4064664443d0SSam Leffler 	 * Fillin the remainder of the descriptor info.
4065664443d0SSam Leffler 	 */
4066664443d0SSam Leffler 	ds0 = ds = bf->bf_desc;
4067664443d0SSam Leffler 	for (i = 0; i < bf->bf_nseg; i++, ds++) {
4068664443d0SSam Leffler 		ds->ds_data = bf->bf_segs[i].ds_addr;
4069664443d0SSam Leffler 		if (i == bf->bf_nseg - 1)
4070664443d0SSam Leffler 			ds->ds_link = 0;
4071664443d0SSam Leffler 		else
4072664443d0SSam Leffler 			ds->ds_link = bf->bf_daddr + sizeof(*ds) * (i + 1);
4073664443d0SSam Leffler 		ath_hal_filltxdesc(ah, ds
4074664443d0SSam Leffler 			, bf->bf_segs[i].ds_len	/* segment length */
4075664443d0SSam Leffler 			, i == 0		/* first segment */
4076664443d0SSam Leffler 			, i == bf->bf_nseg - 1	/* last segment */
4077664443d0SSam Leffler 			, ds0			/* first descriptor */
4078664443d0SSam Leffler 		);
4079664443d0SSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT,
4080664443d0SSam Leffler 			"%s: %d: %08x %08x %08x %08x %08x %08x\n",
4081664443d0SSam Leffler 			__func__, i, ds->ds_link, ds->ds_data,
4082664443d0SSam Leffler 			ds->ds_ctl0, ds->ds_ctl1, ds->ds_hw[0], ds->ds_hw[1]);
4083664443d0SSam Leffler 	}
4084664443d0SSam Leffler 	/*
4085664443d0SSam Leffler 	 * Insert the frame on the outbound list and pass it on
4086664443d0SSam Leffler 	 * to the hardware.  Multicast frames buffered for power
4087664443d0SSam Leffler 	 * save stations and transmit from the CAB queue are stored
4088664443d0SSam Leffler 	 * on a s/w only queue and loaded on to the CAB queue in
4089664443d0SSam Leffler 	 * the SWBA handler since frames only go out on DTIM and
4090664443d0SSam Leffler 	 * to avoid possible races.
4091664443d0SSam Leffler 	 */
4092664443d0SSam Leffler 	ATH_TXQ_LOCK(txq);
4093664443d0SSam Leffler 	ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
4094664443d0SSam Leffler 	if (txq != &sc->sc_mcastq) {
4095664443d0SSam Leffler 		if (txq->axq_link == NULL) {
4096664443d0SSam Leffler 			ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
4097664443d0SSam Leffler 			DPRINTF(sc, ATH_DEBUG_XMIT,
4098664443d0SSam Leffler 			    "%s: TXDP[%u] = %p (%p) depth %d\n", __func__,
4099664443d0SSam Leffler 			    txq->axq_qnum, (caddr_t)bf->bf_daddr, bf->bf_desc,
4100664443d0SSam Leffler 			    txq->axq_depth);
4101664443d0SSam Leffler 		} else {
4102664443d0SSam Leffler 			*txq->axq_link = bf->bf_daddr;
4103664443d0SSam Leffler 			DPRINTF(sc, ATH_DEBUG_XMIT,
4104664443d0SSam Leffler 			    "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
4105664443d0SSam Leffler 			    txq->axq_qnum, txq->axq_link,
4106664443d0SSam Leffler 			    (caddr_t)bf->bf_daddr, bf->bf_desc, txq->axq_depth);
4107664443d0SSam Leffler 		}
4108664443d0SSam Leffler 		txq->axq_link = &bf->bf_desc[bf->bf_nseg - 1].ds_link;
4109664443d0SSam Leffler 		ath_hal_txstart(ah, txq->axq_qnum);
4110664443d0SSam Leffler 	} else {
4111664443d0SSam Leffler 		if (txq->axq_link != NULL)
4112664443d0SSam Leffler 			*txq->axq_link = bf->bf_daddr;
4113664443d0SSam Leffler 		txq->axq_link = &bf->bf_desc[bf->bf_nseg - 1].ds_link;
4114664443d0SSam Leffler 	}
4115664443d0SSam Leffler 	ATH_TXQ_UNLOCK(txq);
4116664443d0SSam Leffler }
4117664443d0SSam Leffler 
4118664443d0SSam Leffler static int
41195591b213SSam Leffler ath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni, struct ath_buf *bf,
41205591b213SSam Leffler     struct mbuf *m0)
41215591b213SSam Leffler {
41225591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
41235591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4124fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
4125c4c3cb46SSam Leffler 	const struct chanAccParams *cap = &ic->ic_wme.wme_chanParams;
412668e8e04eSSam Leffler 	int error, iswep, ismcast, isfrag, ismrr;
4127be613480SSam Leffler 	int keyix, hdrlen, pktlen, try0;
4128c42a7b7eSSam Leffler 	u_int8_t rix, txrate, ctsrate;
4129c42a7b7eSSam Leffler 	u_int8_t cix = 0xff;		/* NB: silence compiler */
4130664443d0SSam Leffler 	struct ath_desc *ds;
4131c42a7b7eSSam Leffler 	struct ath_txq *txq;
41325591b213SSam Leffler 	struct ieee80211_frame *wh;
4133c42a7b7eSSam Leffler 	u_int subtype, flags, ctsduration;
41345591b213SSam Leffler 	HAL_PKT_TYPE atype;
41355591b213SSam Leffler 	const HAL_RATE_TABLE *rt;
41365591b213SSam Leffler 	HAL_BOOL shortPreamble;
41375591b213SSam Leffler 	struct ath_node *an;
4138c4c3cb46SSam Leffler 	u_int pri;
41395591b213SSam Leffler 
41405591b213SSam Leffler 	wh = mtod(m0, struct ieee80211_frame *);
41415591b213SSam Leffler 	iswep = wh->i_fc[1] & IEEE80211_FC1_WEP;
4142c42a7b7eSSam Leffler 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
414368e8e04eSSam Leffler 	isfrag = m0->m_flags & M_FRAG;
4144c42a7b7eSSam Leffler 	hdrlen = ieee80211_anyhdrsize(wh);
4145c42a7b7eSSam Leffler 	/*
4146a614e076SSam Leffler 	 * Packet length must not include any
4147a614e076SSam Leffler 	 * pad bytes; deduct them here.
4148c42a7b7eSSam Leffler 	 */
4149c42a7b7eSSam Leffler 	pktlen = m0->m_pkthdr.len - (hdrlen & 3);
41505591b213SSam Leffler 
41515591b213SSam Leffler 	if (iswep) {
4152c42a7b7eSSam Leffler 		const struct ieee80211_cipher *cip;
4153c42a7b7eSSam Leffler 		struct ieee80211_key *k;
4154c42a7b7eSSam Leffler 
4155c42a7b7eSSam Leffler 		/*
4156c42a7b7eSSam Leffler 		 * Construct the 802.11 header+trailer for an encrypted
4157c42a7b7eSSam Leffler 		 * frame. The only reason this can fail is because of an
4158c42a7b7eSSam Leffler 		 * unknown or unsupported cipher/key type.
4159c42a7b7eSSam Leffler 		 */
4160c42a7b7eSSam Leffler 		k = ieee80211_crypto_encap(ic, ni, m0);
4161c42a7b7eSSam Leffler 		if (k == NULL) {
4162c42a7b7eSSam Leffler 			/*
4163c42a7b7eSSam Leffler 			 * This can happen when the key is yanked after the
4164c42a7b7eSSam Leffler 			 * frame was queued.  Just discard the frame; the
4165c42a7b7eSSam Leffler 			 * 802.11 layer counts failures and provides
4166c42a7b7eSSam Leffler 			 * debugging/diagnostics.
4167c42a7b7eSSam Leffler 			 */
416868e8e04eSSam Leffler 			ath_freetx(m0);
4169c42a7b7eSSam Leffler 			return EIO;
41705591b213SSam Leffler 		}
4171c42a7b7eSSam Leffler 		/*
4172c42a7b7eSSam Leffler 		 * Adjust the packet + header lengths for the crypto
4173c42a7b7eSSam Leffler 		 * additions and calculate the h/w key index.  When
4174c42a7b7eSSam Leffler 		 * a s/w mic is done the frame will have had any mic
417568e8e04eSSam Leffler 		 * added to it prior to entry so m0->m_pkthdr.len will
4176c42a7b7eSSam Leffler 		 * account for it. Otherwise we need to add it to the
4177c42a7b7eSSam Leffler 		 * packet length.
4178c42a7b7eSSam Leffler 		 */
4179c42a7b7eSSam Leffler 		cip = k->wk_cipher;
4180c42a7b7eSSam Leffler 		hdrlen += cip->ic_header;
4181c42a7b7eSSam Leffler 		pktlen += cip->ic_header + cip->ic_trailer;
418268e8e04eSSam Leffler 		/* NB: frags always have any TKIP MIC done in s/w */
418368e8e04eSSam Leffler 		if ((k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && !isfrag)
4184c42a7b7eSSam Leffler 			pktlen += cip->ic_miclen;
4185c42a7b7eSSam Leffler 		keyix = k->wk_keyix;
4186c42a7b7eSSam Leffler 
4187c42a7b7eSSam Leffler 		/* packet header may have moved, reset our local pointer */
4188167ecdcaSSam Leffler 		wh = mtod(m0, struct ieee80211_frame *);
4189e8fd88a3SSam Leffler 	} else if (ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) {
4190e8fd88a3SSam Leffler 		/*
4191e8fd88a3SSam Leffler 		 * Use station key cache slot, if assigned.
4192e8fd88a3SSam Leffler 		 */
4193e8fd88a3SSam Leffler 		keyix = ni->ni_ucastkey.wk_keyix;
4194e8fd88a3SSam Leffler 		if (keyix == IEEE80211_KEYIX_NONE)
4195e8fd88a3SSam Leffler 			keyix = HAL_TXKEYIX_INVALID;
4196c42a7b7eSSam Leffler 	} else
4197c42a7b7eSSam Leffler 		keyix = HAL_TXKEYIX_INVALID;
4198c42a7b7eSSam Leffler 
41995591b213SSam Leffler 	pktlen += IEEE80211_CRC_LEN;
42005591b213SSam Leffler 
42015591b213SSam Leffler 	/*
42025591b213SSam Leffler 	 * Load the DMA map so any coalescing is done.  This
42035591b213SSam Leffler 	 * also calculates the number of descriptors we need.
42045591b213SSam Leffler 	 */
4205664443d0SSam Leffler 	error = ath_tx_dmasetup(sc, bf, m0);
420605680ab6SSam Leffler 	if (error != 0)
420705680ab6SSam Leffler 		return error;
42080a915fadSSam Leffler 	bf->bf_node = ni;			/* NB: held reference */
4209664443d0SSam Leffler 	m0 = bf->bf_m;				/* NB: may have changed */
4210664443d0SSam Leffler 	wh = mtod(m0, struct ieee80211_frame *);
42115591b213SSam Leffler 
42125591b213SSam Leffler 	/* setup descriptors */
42135591b213SSam Leffler 	ds = bf->bf_desc;
42145591b213SSam Leffler 	rt = sc->sc_currates;
42155591b213SSam Leffler 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
42165591b213SSam Leffler 
42175591b213SSam Leffler 	/*
4218c42a7b7eSSam Leffler 	 * NB: the 802.11 layer marks whether or not we should
4219c42a7b7eSSam Leffler 	 * use short preamble based on the current mode and
4220c42a7b7eSSam Leffler 	 * negotiated parameters.
42215591b213SSam Leffler 	 */
4222c42a7b7eSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
4223c42a7b7eSSam Leffler 	    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
4224c42a7b7eSSam Leffler 		shortPreamble = AH_TRUE;
4225c42a7b7eSSam Leffler 		sc->sc_stats.ast_tx_shortpre++;
4226c42a7b7eSSam Leffler 	} else {
4227c42a7b7eSSam Leffler 		shortPreamble = AH_FALSE;
4228c42a7b7eSSam Leffler 	}
4229c42a7b7eSSam Leffler 
4230c42a7b7eSSam Leffler 	an = ATH_NODE(ni);
4231c42a7b7eSSam Leffler 	flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for crypto errs */
4232be613480SSam Leffler 	ismrr = 0;				/* default no multi-rate retry*/
4233c42a7b7eSSam Leffler 	/*
4234c42a7b7eSSam Leffler 	 * Calculate Atheros packet type from IEEE80211 packet header,
4235c42a7b7eSSam Leffler 	 * setup for rate calculations, and select h/w transmit queue.
4236c42a7b7eSSam Leffler 	 */
42375591b213SSam Leffler 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
42385591b213SSam Leffler 	case IEEE80211_FC0_TYPE_MGT:
42395591b213SSam Leffler 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
42405591b213SSam Leffler 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON)
42415591b213SSam Leffler 			atype = HAL_PKT_TYPE_BEACON;
42425591b213SSam Leffler 		else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
42435591b213SSam Leffler 			atype = HAL_PKT_TYPE_PROBE_RESP;
42445591b213SSam Leffler 		else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM)
42455591b213SSam Leffler 			atype = HAL_PKT_TYPE_ATIM;
4246c42a7b7eSSam Leffler 		else
4247c42a7b7eSSam Leffler 			atype = HAL_PKT_TYPE_NORMAL;	/* XXX */
424855f63772SSam Leffler 		rix = sc->sc_minrateix;
424955f63772SSam Leffler 		txrate = rt->info[rix].rateCode;
4250c42a7b7eSSam Leffler 		if (shortPreamble)
425155f63772SSam Leffler 			txrate |= rt->info[rix].shortPreamble;
4252be613480SSam Leffler 		try0 = ATH_TXMGTTRY;
4253c42a7b7eSSam Leffler 		/* NB: force all management frames to highest queue */
4254c42a7b7eSSam Leffler 		if (ni->ni_flags & IEEE80211_NODE_QOS) {
4255c42a7b7eSSam Leffler 			/* NB: force all management frames to highest queue */
4256c4c3cb46SSam Leffler 			pri = WME_AC_VO;
4257c42a7b7eSSam Leffler 		} else
4258c4c3cb46SSam Leffler 			pri = WME_AC_BE;
4259c42a7b7eSSam Leffler 		flags |= HAL_TXDESC_INTREQ;	/* force interrupt */
42605591b213SSam Leffler 		break;
42615591b213SSam Leffler 	case IEEE80211_FC0_TYPE_CTL:
4262c42a7b7eSSam Leffler 		atype = HAL_PKT_TYPE_PSPOLL;	/* stop setting of duration */
426355f63772SSam Leffler 		rix = sc->sc_minrateix;
426455f63772SSam Leffler 		txrate = rt->info[rix].rateCode;
4265c42a7b7eSSam Leffler 		if (shortPreamble)
426655f63772SSam Leffler 			txrate |= rt->info[rix].shortPreamble;
4267be613480SSam Leffler 		try0 = ATH_TXMGTTRY;
4268c42a7b7eSSam Leffler 		/* NB: force all ctl frames to highest queue */
4269c42a7b7eSSam Leffler 		if (ni->ni_flags & IEEE80211_NODE_QOS) {
4270c42a7b7eSSam Leffler 			/* NB: force all ctl frames to highest queue */
4271c4c3cb46SSam Leffler 			pri = WME_AC_VO;
4272c42a7b7eSSam Leffler 		} else
4273c4c3cb46SSam Leffler 			pri = WME_AC_BE;
4274c42a7b7eSSam Leffler 		flags |= HAL_TXDESC_INTREQ;	/* force interrupt */
4275c42a7b7eSSam Leffler 		break;
4276c42a7b7eSSam Leffler 	case IEEE80211_FC0_TYPE_DATA:
4277c42a7b7eSSam Leffler 		atype = HAL_PKT_TYPE_NORMAL;		/* default */
4278c42a7b7eSSam Leffler 		/*
42798b5341deSSam Leffler 		 * Data frames: multicast frames go out at a fixed rate,
42808b5341deSSam Leffler 		 * otherwise consult the rate control module for the
42818b5341deSSam Leffler 		 * rate to use.
4282c42a7b7eSSam Leffler 		 */
42838b5341deSSam Leffler 		if (ismcast) {
42848b5341deSSam Leffler 			/*
42858b5341deSSam Leffler 			 * Check mcast rate setting in case it's changed.
42868b5341deSSam Leffler 			 * XXX move out of fastpath
42878b5341deSSam Leffler 			 */
42888b5341deSSam Leffler 			if (ic->ic_mcast_rate != sc->sc_mcastrate) {
42898b5341deSSam Leffler 				sc->sc_mcastrix =
42908b5341deSSam Leffler 					ath_tx_findrix(rt, ic->ic_mcast_rate);
42918b5341deSSam Leffler 				sc->sc_mcastrate = ic->ic_mcast_rate;
42928b5341deSSam Leffler 			}
42938b5341deSSam Leffler 			rix = sc->sc_mcastrix;
42948b5341deSSam Leffler 			txrate = rt->info[rix].rateCode;
42958b5341deSSam Leffler 			if (shortPreamble)
42968b5341deSSam Leffler 				txrate |= rt->info[rix].shortPreamble;
42978b5341deSSam Leffler 			try0 = 1;
42988b5341deSSam Leffler 		} else {
4299c42a7b7eSSam Leffler 			ath_rate_findrate(sc, an, shortPreamble, pktlen,
4300c42a7b7eSSam Leffler 				&rix, &try0, &txrate);
43013e50ec2cSSam Leffler 			sc->sc_txrate = txrate;		/* for LED blinking */
430268e8e04eSSam Leffler 			sc->sc_lastdatarix = rix;	/* for fast frames */
4303be613480SSam Leffler 			if (try0 != ATH_TXMAXTRY)
4304be613480SSam Leffler 				ismrr = 1;
43058b5341deSSam Leffler 		}
4306c4c3cb46SSam Leffler 		pri = M_WME_GETAC(m0);
4307f9748b9dSSam Leffler 		if (cap->cap_wmeParams[pri].wmep_noackPolicy)
4308c42a7b7eSSam Leffler 			flags |= HAL_TXDESC_NOACK;
43095591b213SSam Leffler 		break;
43105591b213SSam Leffler 	default:
4311c42a7b7eSSam Leffler 		if_printf(ifp, "bogus frame type 0x%x (%s)\n",
4312c42a7b7eSSam Leffler 			wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__);
4313c42a7b7eSSam Leffler 		/* XXX statistic */
431468e8e04eSSam Leffler 		ath_freetx(m0);
43155591b213SSam Leffler 		return EIO;
43165591b213SSam Leffler 	}
4317c4c3cb46SSam Leffler 	txq = sc->sc_ac2q[pri];
4318c42a7b7eSSam Leffler 
43195591b213SSam Leffler 	/*
4320c42a7b7eSSam Leffler 	 * When servicing one or more stations in power-save mode
4321622b3fd2SSam Leffler 	 * (or) if there is some mcast data waiting on the mcast
4322622b3fd2SSam Leffler 	 * queue (to prevent out of order delivery) multicast
4323622b3fd2SSam Leffler 	 * frames must be buffered until after the beacon.
43245591b213SSam Leffler 	 */
4325622b3fd2SSam Leffler 	if (ismcast && (ic->ic_ps_sta || sc->sc_mcastq.axq_depth)) {
4326622b3fd2SSam Leffler 		txq = &sc->sc_mcastq;
4327c42a7b7eSSam Leffler 		/* XXX? more bit in 802.11 frame header */
43285591b213SSam Leffler 	}
43295591b213SSam Leffler 
43305591b213SSam Leffler 	/*
43315591b213SSam Leffler 	 * Calculate miscellaneous flags.
43325591b213SSam Leffler 	 */
4333c42a7b7eSSam Leffler 	if (ismcast) {
43345591b213SSam Leffler 		flags |= HAL_TXDESC_NOACK;	/* no ack on broad/multicast */
433568e8e04eSSam Leffler 	} else if (pktlen > ic->ic_rtsthreshold &&
433668e8e04eSSam Leffler 	    (ni->ni_ath_flags & IEEE80211_NODE_FF) == 0) {
43375591b213SSam Leffler 		flags |= HAL_TXDESC_RTSENA;	/* RTS based on frame length */
4338c42a7b7eSSam Leffler 		cix = rt->info[rix].controlRate;
43395591b213SSam Leffler 		sc->sc_stats.ast_tx_rts++;
43405591b213SSam Leffler 	}
4341f9748b9dSSam Leffler 	if (flags & HAL_TXDESC_NOACK)		/* NB: avoid double counting */
4342f9748b9dSSam Leffler 		sc->sc_stats.ast_tx_noack++;
43435591b213SSam Leffler 
43445591b213SSam Leffler 	/*
4345c42a7b7eSSam Leffler 	 * If 802.11g protection is enabled, determine whether
4346c42a7b7eSSam Leffler 	 * to use RTS/CTS or just CTS.  Note that this is only
4347c42a7b7eSSam Leffler 	 * done for OFDM unicast frames.
4348c42a7b7eSSam Leffler 	 */
4349c42a7b7eSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
4350c42a7b7eSSam Leffler 	    rt->info[rix].phy == IEEE80211_T_OFDM &&
4351c42a7b7eSSam Leffler 	    (flags & HAL_TXDESC_NOACK) == 0) {
4352c42a7b7eSSam Leffler 		/* XXX fragments must use CCK rates w/ protection */
4353c42a7b7eSSam Leffler 		if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
4354c42a7b7eSSam Leffler 			flags |= HAL_TXDESC_RTSENA;
4355c42a7b7eSSam Leffler 		else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
4356c42a7b7eSSam Leffler 			flags |= HAL_TXDESC_CTSENA;
435768e8e04eSSam Leffler 		if (isfrag) {
435868e8e04eSSam Leffler 			/*
435968e8e04eSSam Leffler 			 * For frags it would be desirable to use the
436068e8e04eSSam Leffler 			 * highest CCK rate for RTS/CTS.  But stations
436168e8e04eSSam Leffler 			 * farther away may detect it at a lower CCK rate
436268e8e04eSSam Leffler 			 * so use the configured protection rate instead
436368e8e04eSSam Leffler 			 * (for now).
436468e8e04eSSam Leffler 			 */
436568e8e04eSSam Leffler 			cix = rt->info[sc->sc_protrix].controlRate;
436668e8e04eSSam Leffler 		} else
4367c42a7b7eSSam Leffler 			cix = rt->info[sc->sc_protrix].controlRate;
4368c42a7b7eSSam Leffler 		sc->sc_stats.ast_tx_protect++;
4369c42a7b7eSSam Leffler 	}
4370c42a7b7eSSam Leffler 
4371c42a7b7eSSam Leffler 	/*
4372f6aa038bSSam Leffler 	 * Calculate duration.  This logically belongs in the 802.11
4373f6aa038bSSam Leffler 	 * layer but it lacks sufficient information to calculate it.
4374f6aa038bSSam Leffler 	 */
4375f6aa038bSSam Leffler 	if ((flags & HAL_TXDESC_NOACK) == 0 &&
4376f6aa038bSSam Leffler 	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL) {
4377f6aa038bSSam Leffler 		u_int16_t dur;
4378c42a7b7eSSam Leffler 		if (shortPreamble)
4379c42a7b7eSSam Leffler 			dur = rt->info[rix].spAckDuration;
4380c42a7b7eSSam Leffler 		else
4381c42a7b7eSSam Leffler 			dur = rt->info[rix].lpAckDuration;
438268e8e04eSSam Leffler 		if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) {
438368e8e04eSSam Leffler 			dur += dur;		/* additional SIFS+ACK */
438468e8e04eSSam Leffler 			KASSERT(m0->m_nextpkt != NULL, ("no fragment"));
438568e8e04eSSam Leffler 			/*
438668e8e04eSSam Leffler 			 * Include the size of next fragment so NAV is
438768e8e04eSSam Leffler 			 * updated properly.  The last fragment uses only
438868e8e04eSSam Leffler 			 * the ACK duration
438968e8e04eSSam Leffler 			 */
439068e8e04eSSam Leffler 			dur += ath_hal_computetxtime(ah, rt,
439168e8e04eSSam Leffler 					m0->m_nextpkt->m_pkthdr.len,
439268e8e04eSSam Leffler 					rix, shortPreamble);
439368e8e04eSSam Leffler 		}
439468e8e04eSSam Leffler 		if (isfrag) {
439568e8e04eSSam Leffler 			/*
439668e8e04eSSam Leffler 			 * Force hardware to use computed duration for next
439768e8e04eSSam Leffler 			 * fragment by disabling multi-rate retry which updates
439868e8e04eSSam Leffler 			 * duration based on the multi-rate duration table.
439968e8e04eSSam Leffler 			 */
440068e8e04eSSam Leffler 			ismrr = 0;
440168e8e04eSSam Leffler 			try0 = ATH_TXMGTTRY;	/* XXX? */
440268e8e04eSSam Leffler 		}
4403c42a7b7eSSam Leffler 		*(u_int16_t *)wh->i_dur = htole16(dur);
4404f6aa038bSSam Leffler 	}
4405f6aa038bSSam Leffler 
4406f6aa038bSSam Leffler 	/*
44075591b213SSam Leffler 	 * Calculate RTS/CTS rate and duration if needed.
44085591b213SSam Leffler 	 */
44095591b213SSam Leffler 	ctsduration = 0;
44105591b213SSam Leffler 	if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA)) {
44115591b213SSam Leffler 		/*
44125591b213SSam Leffler 		 * CTS transmit rate is derived from the transmit rate
44135591b213SSam Leffler 		 * by looking in the h/w rate table.  We must also factor
44145591b213SSam Leffler 		 * in whether or not a short preamble is to be used.
44155591b213SSam Leffler 		 */
4416c42a7b7eSSam Leffler 		/* NB: cix is set above where RTS/CTS is enabled */
4417c42a7b7eSSam Leffler 		KASSERT(cix != 0xff, ("cix not setup"));
44185591b213SSam Leffler 		ctsrate = rt->info[cix].rateCode;
44195591b213SSam Leffler 		/*
4420c42a7b7eSSam Leffler 		 * Compute the transmit duration based on the frame
4421c42a7b7eSSam Leffler 		 * size and the size of an ACK frame.  We call into the
4422c42a7b7eSSam Leffler 		 * HAL to do the computation since it depends on the
4423c42a7b7eSSam Leffler 		 * characteristics of the actual PHY being used.
4424c42a7b7eSSam Leffler 		 *
4425c42a7b7eSSam Leffler 		 * NB: CTS is assumed the same size as an ACK so we can
4426c42a7b7eSSam Leffler 		 *     use the precalculated ACK durations.
44275591b213SSam Leffler 		 */
4428c42a7b7eSSam Leffler 		if (shortPreamble) {
4429c42a7b7eSSam Leffler 			ctsrate |= rt->info[cix].shortPreamble;
4430c42a7b7eSSam Leffler 			if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
4431c42a7b7eSSam Leffler 				ctsduration += rt->info[cix].spAckDuration;
44325591b213SSam Leffler 			ctsduration += ath_hal_computetxtime(ah,
4433c42a7b7eSSam Leffler 				rt, pktlen, rix, AH_TRUE);
4434c42a7b7eSSam Leffler 			if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
44356ee571b2SSam Leffler 				ctsduration += rt->info[rix].spAckDuration;
4436c42a7b7eSSam Leffler 		} else {
4437c42a7b7eSSam Leffler 			if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
4438c42a7b7eSSam Leffler 				ctsduration += rt->info[cix].lpAckDuration;
4439c42a7b7eSSam Leffler 			ctsduration += ath_hal_computetxtime(ah,
4440c42a7b7eSSam Leffler 				rt, pktlen, rix, AH_FALSE);
4441c42a7b7eSSam Leffler 			if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
44426ee571b2SSam Leffler 				ctsduration += rt->info[rix].lpAckDuration;
44435591b213SSam Leffler 		}
4444c42a7b7eSSam Leffler 		/*
4445c42a7b7eSSam Leffler 		 * Must disable multi-rate retry when using RTS/CTS.
4446c42a7b7eSSam Leffler 		 */
4447be613480SSam Leffler 		ismrr = 0;
4448be613480SSam Leffler 		try0 = ATH_TXMGTTRY;		/* XXX */
44495591b213SSam Leffler 	} else
44505591b213SSam Leffler 		ctsrate = 0;
44515591b213SSam Leffler 
445268e8e04eSSam Leffler 	/*
445368e8e04eSSam Leffler 	 * At this point we are committed to sending the frame
445468e8e04eSSam Leffler 	 * and we don't need to look at m_nextpkt; clear it in
445568e8e04eSSam Leffler 	 * case this frame is part of frag chain.
445668e8e04eSSam Leffler 	 */
445768e8e04eSSam Leffler 	m0->m_nextpkt = NULL;
445868e8e04eSSam Leffler 
4459c42a7b7eSSam Leffler 	if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
446068e8e04eSSam Leffler 		ieee80211_dump_pkt(ic, mtod(m0, caddr_t), m0->m_len,
44613e50ec2cSSam Leffler 			sc->sc_hwmap[txrate].ieeerate, -1);
44625591b213SSam Leffler 
4463ff046a6cSSam Leffler 	if (bpf_peers_present(ic->ic_rawbpf))
4464eb2cdcb1SSam Leffler 		bpf_mtap(ic->ic_rawbpf, m0);
446516d878ccSChristian S.J. Peron 	if (bpf_peers_present(sc->sc_drvbpf)) {
44667b0c77ecSSam Leffler 		u_int64_t tsf = ath_hal_gettsf64(ah);
44677b0c77ecSSam Leffler 
44687b0c77ecSSam Leffler 		sc->sc_tx_th.wt_tsf = htole64(tsf);
4469d3be6f5bSSam Leffler 		sc->sc_tx_th.wt_flags = sc->sc_hwmap[txrate].txflags;
4470eb2cdcb1SSam Leffler 		if (iswep)
4471eb2cdcb1SSam Leffler 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
447268e8e04eSSam Leffler 		if (isfrag)
447368e8e04eSSam Leffler 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
44743e50ec2cSSam Leffler 		sc->sc_tx_th.wt_rate = sc->sc_hwmap[txrate].ieeerate;
4475c42a7b7eSSam Leffler 		sc->sc_tx_th.wt_txpower = ni->ni_txpower;
4476c42a7b7eSSam Leffler 		sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
4477eb2cdcb1SSam Leffler 
4478eb2cdcb1SSam Leffler 		bpf_mtap2(sc->sc_drvbpf,
44792f1ad18bSSam Leffler 			&sc->sc_tx_th, sc->sc_tx_th_len, m0);
4480eb2cdcb1SSam Leffler 	}
4481eb2cdcb1SSam Leffler 
44825591b213SSam Leffler 	/*
4483c42a7b7eSSam Leffler 	 * Determine if a tx interrupt should be generated for
4484c42a7b7eSSam Leffler 	 * this descriptor.  We take a tx interrupt to reap
4485c42a7b7eSSam Leffler 	 * descriptors when the h/w hits an EOL condition or
4486c42a7b7eSSam Leffler 	 * when the descriptor is specifically marked to generate
4487c42a7b7eSSam Leffler 	 * an interrupt.  We periodically mark descriptors in this
4488c42a7b7eSSam Leffler 	 * way to insure timely replenishing of the supply needed
4489c42a7b7eSSam Leffler 	 * for sending frames.  Defering interrupts reduces system
4490c42a7b7eSSam Leffler 	 * load and potentially allows more concurrent work to be
4491c42a7b7eSSam Leffler 	 * done but if done to aggressively can cause senders to
4492c42a7b7eSSam Leffler 	 * backup.
4493c42a7b7eSSam Leffler 	 *
4494c42a7b7eSSam Leffler 	 * NB: use >= to deal with sc_txintrperiod changing
4495c42a7b7eSSam Leffler 	 *     dynamically through sysctl.
4496c42a7b7eSSam Leffler 	 */
4497c42a7b7eSSam Leffler 	if (flags & HAL_TXDESC_INTREQ) {
4498c42a7b7eSSam Leffler 		txq->axq_intrcnt = 0;
4499c42a7b7eSSam Leffler 	} else if (++txq->axq_intrcnt >= sc->sc_txintrperiod) {
4500c42a7b7eSSam Leffler 		flags |= HAL_TXDESC_INTREQ;
4501c42a7b7eSSam Leffler 		txq->axq_intrcnt = 0;
4502c42a7b7eSSam Leffler 	}
4503c42a7b7eSSam Leffler 
4504c42a7b7eSSam Leffler 	/*
45055591b213SSam Leffler 	 * Formulate first tx descriptor with tx controls.
45065591b213SSam Leffler 	 */
45075591b213SSam Leffler 	/* XXX check return value? */
45085591b213SSam Leffler 	ath_hal_setuptxdesc(ah, ds
45095591b213SSam Leffler 		, pktlen		/* packet length */
45105591b213SSam Leffler 		, hdrlen		/* header length */
45115591b213SSam Leffler 		, atype			/* Atheros packet type */
4512c42a7b7eSSam Leffler 		, ni->ni_txpower	/* txpower */
4513c42a7b7eSSam Leffler 		, txrate, try0		/* series 0 rate/tries */
4514c42a7b7eSSam Leffler 		, keyix			/* key cache index */
4515c42a7b7eSSam Leffler 		, sc->sc_txantenna	/* antenna mode */
45165591b213SSam Leffler 		, flags			/* flags */
45175591b213SSam Leffler 		, ctsrate		/* rts/cts rate */
45185591b213SSam Leffler 		, ctsduration		/* rts/cts duration */
45195591b213SSam Leffler 	);
4520ebecf802SSam Leffler 	bf->bf_flags = flags;
4521c42a7b7eSSam Leffler 	/*
4522c42a7b7eSSam Leffler 	 * Setup the multi-rate retry state only when we're
4523c42a7b7eSSam Leffler 	 * going to use it.  This assumes ath_hal_setuptxdesc
4524c42a7b7eSSam Leffler 	 * initializes the descriptors (so we don't have to)
4525c42a7b7eSSam Leffler 	 * when the hardware supports multi-rate retry and
4526c42a7b7eSSam Leffler 	 * we don't use it.
4527c42a7b7eSSam Leffler 	 */
4528be613480SSam Leffler 	if (ismrr)
4529c42a7b7eSSam Leffler 		ath_rate_setupxtxdesc(sc, an, ds, shortPreamble, rix);
4530c42a7b7eSSam Leffler 
4531664443d0SSam Leffler 	ath_tx_handoff(sc, txq, bf);
45325591b213SSam Leffler 	return 0;
45335591b213SSam Leffler }
45345591b213SSam Leffler 
4535c42a7b7eSSam Leffler /*
4536c42a7b7eSSam Leffler  * Process completed xmit descriptors from the specified queue.
4537c42a7b7eSSam Leffler  */
4538d7736e13SSam Leffler static int
4539c42a7b7eSSam Leffler ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq)
45405591b213SSam Leffler {
45415591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
45420a915fadSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
4543ebecf802SSam Leffler 	struct ath_buf *bf;
4544c4c3cb46SSam Leffler 	struct ath_desc *ds, *ds0;
454565f9edeeSSam Leffler 	struct ath_tx_status *ts;
45465591b213SSam Leffler 	struct ieee80211_node *ni;
45475591b213SSam Leffler 	struct ath_node *an;
4548d7736e13SSam Leffler 	int sr, lr, pri, nacked;
45495591b213SSam Leffler 	HAL_STATUS status;
45505591b213SSam Leffler 
4551c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_TX_PROC, "%s: tx queue %u head %p link %p\n",
4552c42a7b7eSSam Leffler 		__func__, txq->axq_qnum,
4553c42a7b7eSSam Leffler 		(caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
4554c42a7b7eSSam Leffler 		txq->axq_link);
4555d7736e13SSam Leffler 	nacked = 0;
45565591b213SSam Leffler 	for (;;) {
4557c42a7b7eSSam Leffler 		ATH_TXQ_LOCK(txq);
4558c42a7b7eSSam Leffler 		txq->axq_intrcnt = 0;	/* reset periodic desc intr count */
4559c42a7b7eSSam Leffler 		bf = STAILQ_FIRST(&txq->axq_q);
45605591b213SSam Leffler 		if (bf == NULL) {
4561c42a7b7eSSam Leffler 			ATH_TXQ_UNLOCK(txq);
45625591b213SSam Leffler 			break;
45635591b213SSam Leffler 		}
4564c4c3cb46SSam Leffler 		ds0 = &bf->bf_desc[0];
45655591b213SSam Leffler 		ds = &bf->bf_desc[bf->bf_nseg - 1];
456665f9edeeSSam Leffler 		ts = &bf->bf_status.ds_txstat;
456765f9edeeSSam Leffler 		status = ath_hal_txprocdesc(ah, ds, ts);
4568a585a9a1SSam Leffler #ifdef ATH_DEBUG
4569c42a7b7eSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_XMIT_DESC)
45707a4c5ed9SSam Leffler 			ath_printtxbuf(bf, txq->axq_qnum, 0, status == HAL_OK);
45715591b213SSam Leffler #endif
45725591b213SSam Leffler 		if (status == HAL_EINPROGRESS) {
4573c42a7b7eSSam Leffler 			ATH_TXQ_UNLOCK(txq);
45745591b213SSam Leffler 			break;
45755591b213SSam Leffler 		}
4576c42a7b7eSSam Leffler 		ATH_TXQ_REMOVE_HEAD(txq, bf_list);
4577ebecf802SSam Leffler 		if (txq->axq_depth == 0)
45781539af1eSSam Leffler 			txq->axq_link = NULL;
4579c42a7b7eSSam Leffler 		ATH_TXQ_UNLOCK(txq);
45805591b213SSam Leffler 
45815591b213SSam Leffler 		ni = bf->bf_node;
45825591b213SSam Leffler 		if (ni != NULL) {
4583c42a7b7eSSam Leffler 			an = ATH_NODE(ni);
458465f9edeeSSam Leffler 			if (ts->ts_status == 0) {
458565f9edeeSSam Leffler 				u_int8_t txant = ts->ts_antenna;
4586c42a7b7eSSam Leffler 				sc->sc_stats.ast_ant_tx[txant]++;
4587c42a7b7eSSam Leffler 				sc->sc_ant_tx[txant]++;
458865f9edeeSSam Leffler 				if (ts->ts_rate & HAL_TXSTAT_ALTRATE)
4589c42a7b7eSSam Leffler 					sc->sc_stats.ast_tx_altrate++;
459065f9edeeSSam Leffler 				sc->sc_stats.ast_tx_rssi = ts->ts_rssi;
4591ffa2cab6SSam Leffler 				ATH_RSSI_LPF(sc->sc_halstats.ns_avgtxrssi,
459265f9edeeSSam Leffler 					ts->ts_rssi);
4593c42a7b7eSSam Leffler 				pri = M_WME_GETAC(bf->bf_m);
4594c42a7b7eSSam Leffler 				if (pri >= WME_AC_VO)
4595c42a7b7eSSam Leffler 					ic->ic_wme.wme_hipri_traffic++;
4596c42a7b7eSSam Leffler 				ni->ni_inact = ni->ni_inact_reload;
45975591b213SSam Leffler 			} else {
459865f9edeeSSam Leffler 				if (ts->ts_status & HAL_TXERR_XRETRY)
45995591b213SSam Leffler 					sc->sc_stats.ast_tx_xretries++;
460065f9edeeSSam Leffler 				if (ts->ts_status & HAL_TXERR_FIFO)
46015591b213SSam Leffler 					sc->sc_stats.ast_tx_fifoerr++;
460265f9edeeSSam Leffler 				if (ts->ts_status & HAL_TXERR_FILT)
46035591b213SSam Leffler 					sc->sc_stats.ast_tx_filtered++;
460468e8e04eSSam Leffler 				if (bf->bf_m->m_flags & M_FF)
460568e8e04eSSam Leffler 					sc->sc_stats.ast_ff_txerr++;
46065591b213SSam Leffler 			}
460765f9edeeSSam Leffler 			sr = ts->ts_shortretry;
460865f9edeeSSam Leffler 			lr = ts->ts_longretry;
46095591b213SSam Leffler 			sc->sc_stats.ast_tx_shortretry += sr;
46105591b213SSam Leffler 			sc->sc_stats.ast_tx_longretry += lr;
4611c42a7b7eSSam Leffler 			/*
4612c42a7b7eSSam Leffler 			 * Hand the descriptor to the rate control algorithm.
4613c42a7b7eSSam Leffler 			 */
461465f9edeeSSam Leffler 			if ((ts->ts_status & HAL_TXERR_FILT) == 0 &&
4615ebecf802SSam Leffler 			    (bf->bf_flags & HAL_TXDESC_NOACK) == 0) {
4616d7736e13SSam Leffler 				/*
4617d7736e13SSam Leffler 				 * If frame was ack'd update the last rx time
4618d7736e13SSam Leffler 				 * used to workaround phantom bmiss interrupts.
4619d7736e13SSam Leffler 				 */
462065f9edeeSSam Leffler 				if (ts->ts_status == 0)
4621d7736e13SSam Leffler 					nacked++;
462265f9edeeSSam Leffler 				ath_rate_tx_complete(sc, an, bf);
4623d7736e13SSam Leffler 			}
46240a915fadSSam Leffler 			/*
462568e8e04eSSam Leffler 			 * Do any tx complete callback.  Note this must
462668e8e04eSSam Leffler 			 * be done before releasing the node reference.
462768e8e04eSSam Leffler 			 */
462868e8e04eSSam Leffler 			if (bf->bf_m->m_flags & M_TXCB)
462968e8e04eSSam Leffler 				ieee80211_process_callback(ni, bf->bf_m,
463068e8e04eSSam Leffler 					ts->ts_status);
463168e8e04eSSam Leffler 			/*
46320a915fadSSam Leffler 			 * Reclaim reference to node.
46330a915fadSSam Leffler 			 *
46340a915fadSSam Leffler 			 * NB: the node may be reclaimed here if, for example
46350a915fadSSam Leffler 			 *     this is a DEAUTH message that was sent and the
46360a915fadSSam Leffler 			 *     node was timed out due to inactivity.
46370a915fadSSam Leffler 			 */
4638c42a7b7eSSam Leffler 			ieee80211_free_node(ni);
46395591b213SSam Leffler 		}
46405591b213SSam Leffler 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
46415591b213SSam Leffler 		    BUS_DMASYNC_POSTWRITE);
46425591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
464368e8e04eSSam Leffler 
46445591b213SSam Leffler 		m_freem(bf->bf_m);
46455591b213SSam Leffler 		bf->bf_m = NULL;
46465591b213SSam Leffler 		bf->bf_node = NULL;
46475591b213SSam Leffler 
4648f0b2a0beSSam Leffler 		ATH_TXBUF_LOCK(sc);
4649c42a7b7eSSam Leffler 		STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
4650f0b2a0beSSam Leffler 		ATH_TXBUF_UNLOCK(sc);
46515591b213SSam Leffler 	}
465268e8e04eSSam Leffler 	/*
465368e8e04eSSam Leffler 	 * Flush fast-frame staging queue when traffic slows.
465468e8e04eSSam Leffler 	 */
465568e8e04eSSam Leffler 	if (txq->axq_depth <= 1)
465668e8e04eSSam Leffler 		ath_ff_stageq_flush(sc, txq, ath_ff_always);
4657d7736e13SSam Leffler 	return nacked;
4658d7736e13SSam Leffler }
4659d7736e13SSam Leffler 
4660d7736e13SSam Leffler static __inline int
4661d7736e13SSam Leffler txqactive(struct ath_hal *ah, int qnum)
4662d7736e13SSam Leffler {
4663e2815d69SSam Leffler 	u_int32_t txqs = 1<<qnum;
4664e2815d69SSam Leffler 	ath_hal_gettxintrtxqs(ah, &txqs);
46659760f8aeSSam Leffler 	return (txqs & (1<<qnum));
4666c42a7b7eSSam Leffler }
4667c42a7b7eSSam Leffler 
4668c42a7b7eSSam Leffler /*
4669c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
4670c42a7b7eSSam Leffler  * for a single hardware transmit queue (e.g. 5210 and 5211).
4671c42a7b7eSSam Leffler  */
4672c42a7b7eSSam Leffler static void
4673c42a7b7eSSam Leffler ath_tx_proc_q0(void *arg, int npending)
4674c42a7b7eSSam Leffler {
4675c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4676fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
4677c42a7b7eSSam Leffler 
4678d7736e13SSam Leffler 	if (txqactive(sc->sc_ah, 0) && ath_tx_processq(sc, &sc->sc_txq[0]))
4679d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4680d7736e13SSam Leffler 	if (txqactive(sc->sc_ah, sc->sc_cabq->axq_qnum))
4681d7736e13SSam Leffler 		ath_tx_processq(sc, sc->sc_cabq);
468213f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
468368e8e04eSSam Leffler 	ifp->if_timer = 0;
46845591b213SSam Leffler 
46853e50ec2cSSam Leffler 	if (sc->sc_softled)
46863e50ec2cSSam Leffler 		ath_led_event(sc, ATH_LED_TX);
46873e50ec2cSSam Leffler 
46885591b213SSam Leffler 	ath_start(ifp);
46895591b213SSam Leffler }
46905591b213SSam Leffler 
46915591b213SSam Leffler /*
4692c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
4693c42a7b7eSSam Leffler  * for four hardware queues, 0-3 (e.g. 5212 w/ WME support).
46945591b213SSam Leffler  */
46955591b213SSam Leffler static void
4696c42a7b7eSSam Leffler ath_tx_proc_q0123(void *arg, int npending)
4697c42a7b7eSSam Leffler {
4698c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4699fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
4700d7736e13SSam Leffler 	int nacked;
4701c42a7b7eSSam Leffler 
4702c42a7b7eSSam Leffler 	/*
4703c42a7b7eSSam Leffler 	 * Process each active queue.
4704c42a7b7eSSam Leffler 	 */
4705d7736e13SSam Leffler 	nacked = 0;
4706d7736e13SSam Leffler 	if (txqactive(sc->sc_ah, 0))
4707d7736e13SSam Leffler 		nacked += ath_tx_processq(sc, &sc->sc_txq[0]);
4708d7736e13SSam Leffler 	if (txqactive(sc->sc_ah, 1))
4709d7736e13SSam Leffler 		nacked += ath_tx_processq(sc, &sc->sc_txq[1]);
4710d7736e13SSam Leffler 	if (txqactive(sc->sc_ah, 2))
4711d7736e13SSam Leffler 		nacked += ath_tx_processq(sc, &sc->sc_txq[2]);
4712d7736e13SSam Leffler 	if (txqactive(sc->sc_ah, 3))
4713d7736e13SSam Leffler 		nacked += ath_tx_processq(sc, &sc->sc_txq[3]);
4714d7736e13SSam Leffler 	if (txqactive(sc->sc_ah, sc->sc_cabq->axq_qnum))
4715c42a7b7eSSam Leffler 		ath_tx_processq(sc, sc->sc_cabq);
4716d7736e13SSam Leffler 	if (nacked)
4717d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4718c42a7b7eSSam Leffler 
471913f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
472068e8e04eSSam Leffler 	ifp->if_timer = 0;
4721c42a7b7eSSam Leffler 
47223e50ec2cSSam Leffler 	if (sc->sc_softled)
47233e50ec2cSSam Leffler 		ath_led_event(sc, ATH_LED_TX);
47243e50ec2cSSam Leffler 
4725c42a7b7eSSam Leffler 	ath_start(ifp);
4726c42a7b7eSSam Leffler }
4727c42a7b7eSSam Leffler 
4728c42a7b7eSSam Leffler /*
4729c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt.
4730c42a7b7eSSam Leffler  */
4731c42a7b7eSSam Leffler static void
4732c42a7b7eSSam Leffler ath_tx_proc(void *arg, int npending)
4733c42a7b7eSSam Leffler {
4734c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4735fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
4736d7736e13SSam Leffler 	int i, nacked;
4737c42a7b7eSSam Leffler 
4738c42a7b7eSSam Leffler 	/*
4739c42a7b7eSSam Leffler 	 * Process each active queue.
4740c42a7b7eSSam Leffler 	 */
4741d7736e13SSam Leffler 	nacked = 0;
4742c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
4743d7736e13SSam Leffler 		if (ATH_TXQ_SETUP(sc, i) && txqactive(sc->sc_ah, i))
4744d7736e13SSam Leffler 			nacked += ath_tx_processq(sc, &sc->sc_txq[i]);
4745d7736e13SSam Leffler 	if (nacked)
4746d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4747c42a7b7eSSam Leffler 
474813f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
474968e8e04eSSam Leffler 	ifp->if_timer = 0;
4750c42a7b7eSSam Leffler 
47513e50ec2cSSam Leffler 	if (sc->sc_softled)
47523e50ec2cSSam Leffler 		ath_led_event(sc, ATH_LED_TX);
47533e50ec2cSSam Leffler 
4754c42a7b7eSSam Leffler 	ath_start(ifp);
4755c42a7b7eSSam Leffler }
4756c42a7b7eSSam Leffler 
4757c42a7b7eSSam Leffler static void
4758c42a7b7eSSam Leffler ath_tx_draintxq(struct ath_softc *sc, struct ath_txq *txq)
47595591b213SSam Leffler {
4760a585a9a1SSam Leffler #ifdef ATH_DEBUG
47615591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4762d2f6ed15SSam Leffler #endif
476323428eafSSam Leffler 	struct ieee80211_node *ni;
47645591b213SSam Leffler 	struct ath_buf *bf;
47657a4c5ed9SSam Leffler 	u_int ix;
47665591b213SSam Leffler 
4767c42a7b7eSSam Leffler 	/*
4768c42a7b7eSSam Leffler 	 * NB: this assumes output has been stopped and
4769ebecf802SSam Leffler 	 *     we do not need to block ath_tx_tasklet
4770c42a7b7eSSam Leffler 	 */
47717a4c5ed9SSam Leffler 	for (ix = 0;; ix++) {
4772c42a7b7eSSam Leffler 		ATH_TXQ_LOCK(txq);
4773c42a7b7eSSam Leffler 		bf = STAILQ_FIRST(&txq->axq_q);
47745591b213SSam Leffler 		if (bf == NULL) {
4775ebecf802SSam Leffler 			txq->axq_link = NULL;
4776c42a7b7eSSam Leffler 			ATH_TXQ_UNLOCK(txq);
47775591b213SSam Leffler 			break;
47785591b213SSam Leffler 		}
4779c42a7b7eSSam Leffler 		ATH_TXQ_REMOVE_HEAD(txq, bf_list);
4780c42a7b7eSSam Leffler 		ATH_TXQ_UNLOCK(txq);
4781a585a9a1SSam Leffler #ifdef ATH_DEBUG
47824a3ac3fcSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_RESET) {
47837a4c5ed9SSam Leffler 			ath_printtxbuf(bf, txq->axq_qnum, ix,
478465f9edeeSSam Leffler 				ath_hal_txprocdesc(ah, bf->bf_desc,
478565f9edeeSSam Leffler 				    &bf->bf_status.ds_txstat) == HAL_OK);
478668e8e04eSSam Leffler 			ieee80211_dump_pkt(&sc->sc_ic, mtod(bf->bf_m, caddr_t),
47874a3ac3fcSSam Leffler 				bf->bf_m->m_len, 0, -1);
47884a3ac3fcSSam Leffler 		}
4789a585a9a1SSam Leffler #endif /* ATH_DEBUG */
47905591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
479123428eafSSam Leffler 		ni = bf->bf_node;
47925591b213SSam Leffler 		bf->bf_node = NULL;
4793c42a7b7eSSam Leffler 		if (ni != NULL) {
479423428eafSSam Leffler 			/*
4795d50ea6acSSam Leffler 			 * Do any callback and reclaim the node reference.
479623428eafSSam Leffler 			 */
4797d50ea6acSSam Leffler 			if (bf->bf_m->m_flags & M_TXCB)
4798d50ea6acSSam Leffler 				ieee80211_process_callback(ni, bf->bf_m, -1);
4799c42a7b7eSSam Leffler 			ieee80211_free_node(ni);
480023428eafSSam Leffler 		}
480168e8e04eSSam Leffler 		m_freem(bf->bf_m);
480268e8e04eSSam Leffler 		bf->bf_m = NULL;
480368e8e04eSSam Leffler 
4804f0b2a0beSSam Leffler 		ATH_TXBUF_LOCK(sc);
4805c42a7b7eSSam Leffler 		STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
4806f0b2a0beSSam Leffler 		ATH_TXBUF_UNLOCK(sc);
48075591b213SSam Leffler 	}
4808c42a7b7eSSam Leffler }
4809c42a7b7eSSam Leffler 
4810c42a7b7eSSam Leffler static void
4811c42a7b7eSSam Leffler ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
4812c42a7b7eSSam Leffler {
4813c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4814c42a7b7eSSam Leffler 
4815c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n",
4816c42a7b7eSSam Leffler 	    __func__, txq->axq_qnum,
48176891c875SPeter Wemm 	    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, txq->axq_qnum),
48186891c875SPeter Wemm 	    txq->axq_link);
48194a3ac3fcSSam Leffler 	(void) ath_hal_stoptxdma(ah, txq->axq_qnum);
4820c42a7b7eSSam Leffler }
4821c42a7b7eSSam Leffler 
4822c42a7b7eSSam Leffler /*
4823c42a7b7eSSam Leffler  * Drain the transmit queues and reclaim resources.
4824c42a7b7eSSam Leffler  */
4825c42a7b7eSSam Leffler static void
4826c42a7b7eSSam Leffler ath_draintxq(struct ath_softc *sc)
4827c42a7b7eSSam Leffler {
4828c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4829fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
4830c42a7b7eSSam Leffler 	int i;
4831c42a7b7eSSam Leffler 
4832c42a7b7eSSam Leffler 	/* XXX return value */
4833c42a7b7eSSam Leffler 	if (!sc->sc_invalid) {
4834c42a7b7eSSam Leffler 		/* don't touch the hardware if marked invalid */
48354a3ac3fcSSam Leffler 		DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n",
48364a3ac3fcSSam Leffler 		    __func__, sc->sc_bhalq,
48374a3ac3fcSSam Leffler 		    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq),
48384a3ac3fcSSam Leffler 		    NULL);
4839c42a7b7eSSam Leffler 		(void) ath_hal_stoptxdma(ah, sc->sc_bhalq);
4840c42a7b7eSSam Leffler 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
4841c42a7b7eSSam Leffler 			if (ATH_TXQ_SETUP(sc, i))
4842c42a7b7eSSam Leffler 				ath_tx_stopdma(sc, &sc->sc_txq[i]);
4843c42a7b7eSSam Leffler 	}
4844c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
4845c42a7b7eSSam Leffler 		if (ATH_TXQ_SETUP(sc, i))
4846c42a7b7eSSam Leffler 			ath_tx_draintxq(sc, &sc->sc_txq[i]);
4847622b3fd2SSam Leffler 	ath_tx_draintxq(sc, &sc->sc_mcastq);
48484a3ac3fcSSam Leffler #ifdef ATH_DEBUG
48494a3ac3fcSSam Leffler 	if (sc->sc_debug & ATH_DEBUG_RESET) {
48504a3ac3fcSSam Leffler 		struct ath_buf *bf = STAILQ_FIRST(&sc->sc_bbuf);
48514a3ac3fcSSam Leffler 		if (bf != NULL && bf->bf_m != NULL) {
48524a3ac3fcSSam Leffler 			ath_printtxbuf(bf, sc->sc_bhalq, 0,
485365f9edeeSSam Leffler 				ath_hal_txprocdesc(ah, bf->bf_desc,
485465f9edeeSSam Leffler 				    &bf->bf_status.ds_txstat) == HAL_OK);
485568e8e04eSSam Leffler 			ieee80211_dump_pkt(&sc->sc_ic, mtod(bf->bf_m, caddr_t),
48564a3ac3fcSSam Leffler 				bf->bf_m->m_len, 0, -1);
48574a3ac3fcSSam Leffler 		}
48584a3ac3fcSSam Leffler 	}
48594a3ac3fcSSam Leffler #endif /* ATH_DEBUG */
486013f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
486168e8e04eSSam Leffler 	ifp->if_timer = 0;
48625591b213SSam Leffler }
48635591b213SSam Leffler 
48645591b213SSam Leffler /*
48655591b213SSam Leffler  * Disable the receive h/w in preparation for a reset.
48665591b213SSam Leffler  */
48675591b213SSam Leffler static void
48685591b213SSam Leffler ath_stoprecv(struct ath_softc *sc)
48695591b213SSam Leffler {
48708cec0ab9SSam Leffler #define	PA2DESC(_sc, _pa) \
4871c42a7b7eSSam Leffler 	((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
4872c42a7b7eSSam Leffler 		((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
48735591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
48745591b213SSam Leffler 
48755591b213SSam Leffler 	ath_hal_stoppcurecv(ah);	/* disable PCU */
48765591b213SSam Leffler 	ath_hal_setrxfilter(ah, 0);	/* clear recv filter */
48775591b213SSam Leffler 	ath_hal_stopdmarecv(ah);	/* disable DMA engine */
4878c42a7b7eSSam Leffler 	DELAY(3000);			/* 3ms is long enough for 1 frame */
4879a585a9a1SSam Leffler #ifdef ATH_DEBUG
4880c42a7b7eSSam Leffler 	if (sc->sc_debug & (ATH_DEBUG_RESET | ATH_DEBUG_FATAL)) {
48815591b213SSam Leffler 		struct ath_buf *bf;
48827a4c5ed9SSam Leffler 		u_int ix;
48835591b213SSam Leffler 
4884e325e530SSam Leffler 		printf("%s: rx queue %p, link %p\n", __func__,
488530310634SPeter Wemm 			(caddr_t)(uintptr_t) ath_hal_getrxbuf(ah), sc->sc_rxlink);
48867a4c5ed9SSam Leffler 		ix = 0;
4887c42a7b7eSSam Leffler 		STAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
48888cec0ab9SSam Leffler 			struct ath_desc *ds = bf->bf_desc;
488965f9edeeSSam Leffler 			struct ath_rx_status *rs = &bf->bf_status.ds_rxstat;
4890c42a7b7eSSam Leffler 			HAL_STATUS status = ath_hal_rxprocdesc(ah, ds,
489165f9edeeSSam Leffler 				bf->bf_daddr, PA2DESC(sc, ds->ds_link), rs);
4892c42a7b7eSSam Leffler 			if (status == HAL_OK || (sc->sc_debug & ATH_DEBUG_FATAL))
48937a4c5ed9SSam Leffler 				ath_printrxbuf(bf, ix, status == HAL_OK);
48947a4c5ed9SSam Leffler 			ix++;
48955591b213SSam Leffler 		}
48965591b213SSam Leffler 	}
48975591b213SSam Leffler #endif
489868e8e04eSSam Leffler 	if (sc->sc_rxpending != NULL) {
489968e8e04eSSam Leffler 		m_freem(sc->sc_rxpending);
490068e8e04eSSam Leffler 		sc->sc_rxpending = NULL;
490168e8e04eSSam Leffler 	}
49025591b213SSam Leffler 	sc->sc_rxlink = NULL;		/* just in case */
49038cec0ab9SSam Leffler #undef PA2DESC
49045591b213SSam Leffler }
49055591b213SSam Leffler 
49065591b213SSam Leffler /*
49075591b213SSam Leffler  * Enable the receive h/w following a reset.
49085591b213SSam Leffler  */
49095591b213SSam Leffler static int
49105591b213SSam Leffler ath_startrecv(struct ath_softc *sc)
49115591b213SSam Leffler {
49125591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
49135591b213SSam Leffler 	struct ath_buf *bf;
49145591b213SSam Leffler 
49155591b213SSam Leffler 	sc->sc_rxlink = NULL;
491668e8e04eSSam Leffler 	sc->sc_rxpending = NULL;
4917c42a7b7eSSam Leffler 	STAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
49185591b213SSam Leffler 		int error = ath_rxbuf_init(sc, bf);
49195591b213SSam Leffler 		if (error != 0) {
4920c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_RECV,
4921c42a7b7eSSam Leffler 				"%s: ath_rxbuf_init failed %d\n",
4922c42a7b7eSSam Leffler 				__func__, error);
49235591b213SSam Leffler 			return error;
49245591b213SSam Leffler 		}
49255591b213SSam Leffler 	}
49265591b213SSam Leffler 
4927c42a7b7eSSam Leffler 	bf = STAILQ_FIRST(&sc->sc_rxbuf);
49285591b213SSam Leffler 	ath_hal_putrxbuf(ah, bf->bf_daddr);
49295591b213SSam Leffler 	ath_hal_rxena(ah);		/* enable recv descriptors */
49305591b213SSam Leffler 	ath_mode_init(sc);		/* set filters, etc. */
49315591b213SSam Leffler 	ath_hal_startpcurecv(ah);	/* re-enable PCU/DMA engine */
49325591b213SSam Leffler 	return 0;
49335591b213SSam Leffler }
49345591b213SSam Leffler 
49355591b213SSam Leffler /*
4936c42a7b7eSSam Leffler  * Update internal state after a channel change.
4937c42a7b7eSSam Leffler  */
4938c42a7b7eSSam Leffler static void
4939c42a7b7eSSam Leffler ath_chan_change(struct ath_softc *sc, struct ieee80211_channel *chan)
4940c42a7b7eSSam Leffler {
4941c42a7b7eSSam Leffler 	enum ieee80211_phymode mode;
4942c42a7b7eSSam Leffler 
4943c42a7b7eSSam Leffler 	/*
4944c42a7b7eSSam Leffler 	 * Change channels and update the h/w rate map
4945c42a7b7eSSam Leffler 	 * if we're switching; e.g. 11a to 11b/g.
4946c42a7b7eSSam Leffler 	 */
4947aaa70f2fSSam Leffler 	if (IEEE80211_IS_CHAN_HALF(chan))
4948724c193aSSam Leffler 		mode = IEEE80211_MODE_HALF;
4949aaa70f2fSSam Leffler 	else if (IEEE80211_IS_CHAN_QUARTER(chan))
4950724c193aSSam Leffler 		mode = IEEE80211_MODE_QUARTER;
4951724c193aSSam Leffler 	else
495268e8e04eSSam Leffler 		mode = ieee80211_chan2mode(chan);
4953c42a7b7eSSam Leffler 	if (mode != sc->sc_curmode)
4954c42a7b7eSSam Leffler 		ath_setcurmode(sc, mode);
495568e8e04eSSam Leffler 
495668e8e04eSSam Leffler 	sc->sc_rx_th.wr_chan_flags = htole32(chan->ic_flags);
495768e8e04eSSam Leffler 	sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags;
495868e8e04eSSam Leffler 	sc->sc_rx_th.wr_chan_freq = htole16(chan->ic_freq);
495968e8e04eSSam Leffler 	sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq;
496068e8e04eSSam Leffler 	sc->sc_rx_th.wr_chan_ieee = chan->ic_ieee;
496168e8e04eSSam Leffler 	sc->sc_tx_th.wt_chan_ieee = sc->sc_rx_th.wr_chan_ieee;
496268e8e04eSSam Leffler 	sc->sc_rx_th.wr_chan_maxpow = chan->ic_maxregpower;
496368e8e04eSSam Leffler 	sc->sc_tx_th.wt_chan_maxpow = sc->sc_rx_th.wr_chan_maxpow;
4964c42a7b7eSSam Leffler }
4965c42a7b7eSSam Leffler 
4966c42a7b7eSSam Leffler /*
4967bd5a9920SSam Leffler  * Poll for a channel clear indication; this is required
4968bd5a9920SSam Leffler  * for channels requiring DFS and not previously visited
4969bd5a9920SSam Leffler  * and/or with a recent radar detection.
4970bd5a9920SSam Leffler  */
4971bd5a9920SSam Leffler static void
4972bd5a9920SSam Leffler ath_dfswait(void *arg)
4973bd5a9920SSam Leffler {
4974bd5a9920SSam Leffler 	struct ath_softc *sc = arg;
4975bd5a9920SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4976bd5a9920SSam Leffler 	HAL_CHANNEL hchan;
4977bd5a9920SSam Leffler 
4978bd5a9920SSam Leffler 	ath_hal_radar_wait(ah, &hchan);
4979bd5a9920SSam Leffler 	DPRINTF(sc, ATH_DEBUG_DFS, "%s: radar_wait %u/%x/%x\n",
4980bd5a9920SSam Leffler 	    __func__, hchan.channel, hchan.channelFlags, hchan.privFlags);
4981bd5a9920SSam Leffler 
4982bd5a9920SSam Leffler 	if (hchan.privFlags & CHANNEL_INTERFERENCE) {
4983bd5a9920SSam Leffler 		if_printf(sc->sc_ifp,
4984bd5a9920SSam Leffler 		    "channel %u/0x%x/0x%x has interference\n",
4985bd5a9920SSam Leffler 		    hchan.channel, hchan.channelFlags, hchan.privFlags);
4986bd5a9920SSam Leffler 		return;
4987bd5a9920SSam Leffler 	}
4988bd5a9920SSam Leffler 	if ((hchan.privFlags & CHANNEL_DFS) == 0) {
4989bd5a9920SSam Leffler 		/* XXX should not happen */
4990bd5a9920SSam Leffler 		return;
4991bd5a9920SSam Leffler 	}
4992bd5a9920SSam Leffler 	if (hchan.privFlags & CHANNEL_DFS_CLEAR) {
4993bd5a9920SSam Leffler 		sc->sc_curchan.privFlags |= CHANNEL_DFS_CLEAR;
4994bd5a9920SSam Leffler 		sc->sc_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4995bd5a9920SSam Leffler 		if_printf(sc->sc_ifp,
4996bd5a9920SSam Leffler 		    "channel %u/0x%x/0x%x marked clear\n",
4997bd5a9920SSam Leffler 		    hchan.channel, hchan.channelFlags, hchan.privFlags);
4998bd5a9920SSam Leffler 	} else
4999bd5a9920SSam Leffler 		callout_reset(&sc->sc_dfs_ch, 2 * hz, ath_dfswait, sc);
5000bd5a9920SSam Leffler }
5001bd5a9920SSam Leffler 
5002bd5a9920SSam Leffler /*
50035591b213SSam Leffler  * Set/change channels.  If the channel is really being changed,
5004c42a7b7eSSam Leffler  * it's done by reseting the chip.  To accomplish this we must
50055591b213SSam Leffler  * first cleanup any pending DMA, then restart stuff after a la
50065591b213SSam Leffler  * ath_init.
50075591b213SSam Leffler  */
50085591b213SSam Leffler static int
50095591b213SSam Leffler ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan)
50105591b213SSam Leffler {
50115591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
50125591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
50135591b213SSam Leffler 	HAL_CHANNEL hchan;
5014c42a7b7eSSam Leffler 
5015c42a7b7eSSam Leffler 	/*
5016c42a7b7eSSam Leffler 	 * Convert to a HAL channel description with
5017c42a7b7eSSam Leffler 	 * the flags constrained to reflect the current
5018c42a7b7eSSam Leffler 	 * operating mode.
5019c42a7b7eSSam Leffler 	 */
502068e8e04eSSam Leffler 	ath_mapchan(&hchan, chan);
5021c42a7b7eSSam Leffler 
5022370572d9SSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET,
5023370572d9SSam Leffler 	    "%s: %u (%u MHz, hal flags 0x%x) -> %u (%u MHz, hal flags 0x%x)\n",
5024c42a7b7eSSam Leffler 	    __func__,
5025bd5a9920SSam Leffler 	    ath_hal_mhz2ieee(ah, sc->sc_curchan.channel,
5026c42a7b7eSSam Leffler 		sc->sc_curchan.channelFlags),
5027370572d9SSam Leffler 	    	sc->sc_curchan.channel, sc->sc_curchan.channelFlags,
5028bd5a9920SSam Leffler 	    ath_hal_mhz2ieee(ah, hchan.channel, hchan.channelFlags),
5029370572d9SSam Leffler 	        hchan.channel, hchan.channelFlags);
5030c42a7b7eSSam Leffler 	if (hchan.channel != sc->sc_curchan.channel ||
5031c42a7b7eSSam Leffler 	    hchan.channelFlags != sc->sc_curchan.channelFlags) {
5032c42a7b7eSSam Leffler 		HAL_STATUS status;
50335591b213SSam Leffler 
50345591b213SSam Leffler 		/*
50355591b213SSam Leffler 		 * To switch channels clear any pending DMA operations;
50365591b213SSam Leffler 		 * wait long enough for the RX fifo to drain, reset the
50375591b213SSam Leffler 		 * hardware at the new frequency, and then re-enable
50385591b213SSam Leffler 		 * the relevant bits of the h/w.
50395591b213SSam Leffler 		 */
50405591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
50415591b213SSam Leffler 		ath_draintxq(sc);		/* clear pending tx frames */
50425591b213SSam Leffler 		ath_stoprecv(sc);		/* turn off frame recv */
50437a04dc27SSam Leffler 		if (!ath_hal_reset(ah, sc->sc_opmode, &hchan, AH_TRUE, &status)) {
5044370572d9SSam Leffler 			if_printf(ic->ic_ifp, "%s: unable to reset "
5045370572d9SSam Leffler 			    "channel %u (%u Mhz, flags 0x%x hal flags 0x%x)\n",
5046370572d9SSam Leffler 			    __func__, ieee80211_chan2ieee(ic, chan),
5047370572d9SSam Leffler 			    chan->ic_freq, chan->ic_flags, hchan.channelFlags);
50485591b213SSam Leffler 			return EIO;
50495591b213SSam Leffler 		}
5050c42a7b7eSSam Leffler 		sc->sc_curchan = hchan;
5051c42a7b7eSSam Leffler 		ath_update_txpow(sc);		/* update tx power state */
5052c59005e9SSam Leffler 		sc->sc_diversity = ath_hal_getdiversity(ah);
5053bd5a9920SSam Leffler 		sc->sc_calinterval = 1;
5054bd5a9920SSam Leffler 		sc->sc_caltries = 0;
5055c42a7b7eSSam Leffler 
50565591b213SSam Leffler 		/*
50575591b213SSam Leffler 		 * Re-enable rx framework.
50585591b213SSam Leffler 		 */
50595591b213SSam Leffler 		if (ath_startrecv(sc) != 0) {
5060c42a7b7eSSam Leffler 			if_printf(ic->ic_ifp,
5061370572d9SSam Leffler 				"%s: unable to restart recv logic\n", __func__);
50625591b213SSam Leffler 			return EIO;
50635591b213SSam Leffler 		}
50645591b213SSam Leffler 
50655591b213SSam Leffler 		/*
50665591b213SSam Leffler 		 * Change channels and update the h/w rate map
50675591b213SSam Leffler 		 * if we're switching; e.g. 11a to 11b/g.
50685591b213SSam Leffler 		 */
5069c42a7b7eSSam Leffler 		ath_chan_change(sc, chan);
50700a915fadSSam Leffler 
50710a915fadSSam Leffler 		/*
5072bd5a9920SSam Leffler 		 * Handle DFS required waiting period to determine
5073bd5a9920SSam Leffler 		 * if channel is clear of radar traffic.
5074bd5a9920SSam Leffler 		 */
5075bd5a9920SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
5076bd5a9920SSam Leffler #define	DFS_AND_NOT_CLEAR(_c) \
5077bd5a9920SSam Leffler 	(((_c)->privFlags & (CHANNEL_DFS | CHANNEL_DFS_CLEAR)) == CHANNEL_DFS)
5078bd5a9920SSam Leffler 			if (DFS_AND_NOT_CLEAR(&sc->sc_curchan)) {
5079bd5a9920SSam Leffler 				if_printf(sc->sc_ifp,
5080bd5a9920SSam Leffler 					"wait for DFS clear channel signal\n");
5081bd5a9920SSam Leffler 				/* XXX stop sndq */
5082bd5a9920SSam Leffler 				sc->sc_ifp->if_drv_flags |= IFF_DRV_OACTIVE;
5083bd5a9920SSam Leffler 				callout_reset(&sc->sc_dfs_ch,
5084bd5a9920SSam Leffler 					2 * hz, ath_dfswait, sc);
5085bd5a9920SSam Leffler 			} else
5086bd5a9920SSam Leffler 				callout_stop(&sc->sc_dfs_ch);
5087bd5a9920SSam Leffler #undef DFS_NOT_CLEAR
5088bd5a9920SSam Leffler 		}
5089bd5a9920SSam Leffler 
5090bd5a9920SSam Leffler 		/*
50910a915fadSSam Leffler 		 * Re-enable interrupts.
50920a915fadSSam Leffler 		 */
50930a915fadSSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
50945591b213SSam Leffler 	}
50955591b213SSam Leffler 	return 0;
50965591b213SSam Leffler }
50975591b213SSam Leffler 
50985591b213SSam Leffler /*
50995591b213SSam Leffler  * Periodically recalibrate the PHY to account
51005591b213SSam Leffler  * for temperature/environment changes.
51015591b213SSam Leffler  */
51025591b213SSam Leffler static void
51035591b213SSam Leffler ath_calibrate(void *arg)
51045591b213SSam Leffler {
51055591b213SSam Leffler 	struct ath_softc *sc = arg;
51065591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5107bd5a9920SSam Leffler 	HAL_BOOL iqCalDone;
51085591b213SSam Leffler 
51095591b213SSam Leffler 	sc->sc_stats.ast_per_cal++;
51105591b213SSam Leffler 
51115591b213SSam Leffler 	if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) {
51125591b213SSam Leffler 		/*
51135591b213SSam Leffler 		 * Rfgain is out of bounds, reset the chip
51145591b213SSam Leffler 		 * to load new gain values.
51155591b213SSam Leffler 		 */
5116370572d9SSam Leffler 		DPRINTF(sc, ATH_DEBUG_CALIBRATE,
5117370572d9SSam Leffler 			"%s: rfgain change\n", __func__);
51185591b213SSam Leffler 		sc->sc_stats.ast_per_rfgain++;
5119fc74a9f9SBrooks Davis 		ath_reset(sc->sc_ifp);
51205591b213SSam Leffler 	}
5121bd5a9920SSam Leffler 	if (!ath_hal_calibrate(ah, &sc->sc_curchan, &iqCalDone)) {
5122c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY,
5123c42a7b7eSSam Leffler 			"%s: calibration of channel %u failed\n",
5124c42a7b7eSSam Leffler 			__func__, sc->sc_curchan.channel);
51255591b213SSam Leffler 		sc->sc_stats.ast_per_calfail++;
51265591b213SSam Leffler 	}
51277b0c77ecSSam Leffler 	/*
51287b0c77ecSSam Leffler 	 * Calibrate noise floor data again in case of change.
51297b0c77ecSSam Leffler 	 */
51307b0c77ecSSam Leffler 	ath_hal_process_noisefloor(ah);
5131bd5a9920SSam Leffler 	/*
5132bd5a9920SSam Leffler 	 * Poll more frequently when the IQ calibration is in
5133bd5a9920SSam Leffler 	 * progress to speedup loading the final settings.
5134bd5a9920SSam Leffler 	 * We temper this aggressive polling with an exponential
5135bd5a9920SSam Leffler 	 * back off after 4 tries up to ath_calinterval.
5136bd5a9920SSam Leffler 	 */
5137bd5a9920SSam Leffler 	if (iqCalDone || sc->sc_calinterval >= ath_calinterval) {
5138bd5a9920SSam Leffler 		sc->sc_caltries = 0;
5139bd5a9920SSam Leffler 		sc->sc_calinterval = ath_calinterval;
5140bd5a9920SSam Leffler 	} else if (sc->sc_caltries > 4) {
5141bd5a9920SSam Leffler 		sc->sc_caltries = 0;
5142bd5a9920SSam Leffler 		sc->sc_calinterval <<= 1;
5143bd5a9920SSam Leffler 		if (sc->sc_calinterval > ath_calinterval)
5144bd5a9920SSam Leffler 			sc->sc_calinterval = ath_calinterval;
5145bd5a9920SSam Leffler 	}
5146bd5a9920SSam Leffler 	KASSERT(0 < sc->sc_calinterval && sc->sc_calinterval <= ath_calinterval,
5147bd5a9920SSam Leffler 		("bad calibration interval %u", sc->sc_calinterval));
5148bd5a9920SSam Leffler 
5149bd5a9920SSam Leffler 	DPRINTF(sc, ATH_DEBUG_CALIBRATE,
5150bd5a9920SSam Leffler 		"%s: next +%u (%siqCalDone tries %u)\n", __func__,
5151bd5a9920SSam Leffler 		sc->sc_calinterval, iqCalDone ? "" : "!", sc->sc_caltries);
5152bd5a9920SSam Leffler 	sc->sc_caltries++;
5153bd5a9920SSam Leffler 	callout_reset(&sc->sc_cal_ch, sc->sc_calinterval * hz,
5154bd5a9920SSam Leffler 		ath_calibrate, sc);
51555591b213SSam Leffler }
51565591b213SSam Leffler 
515768e8e04eSSam Leffler static void
515868e8e04eSSam Leffler ath_scan_start(struct ieee80211com *ic)
515968e8e04eSSam Leffler {
516068e8e04eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
516168e8e04eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
516268e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
516368e8e04eSSam Leffler 	u_int32_t rfilt;
516468e8e04eSSam Leffler 
516568e8e04eSSam Leffler 	/* XXX calibration timer? */
516668e8e04eSSam Leffler 
516768e8e04eSSam Leffler 	sc->sc_scanning = 1;
516868e8e04eSSam Leffler 	sc->sc_syncbeacon = 0;
516968e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
517068e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
517168e8e04eSSam Leffler 	ath_hal_setassocid(ah, ifp->if_broadcastaddr, 0);
517268e8e04eSSam Leffler 
517368e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0\n",
517468e8e04eSSam Leffler 		 __func__, rfilt, ether_sprintf(ifp->if_broadcastaddr));
517568e8e04eSSam Leffler }
517668e8e04eSSam Leffler 
517768e8e04eSSam Leffler static void
517868e8e04eSSam Leffler ath_scan_end(struct ieee80211com *ic)
517968e8e04eSSam Leffler {
518068e8e04eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
518168e8e04eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
518268e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
518368e8e04eSSam Leffler 	u_int32_t rfilt;
518468e8e04eSSam Leffler 
518568e8e04eSSam Leffler 	sc->sc_scanning = 0;
518668e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
518768e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
518868e8e04eSSam Leffler 	ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
518968e8e04eSSam Leffler 
519068e8e04eSSam Leffler 	ath_hal_process_noisefloor(ah);
519168e8e04eSSam Leffler 
519268e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
519368e8e04eSSam Leffler 		 __func__, rfilt, ether_sprintf(sc->sc_curbssid),
519468e8e04eSSam Leffler 		 sc->sc_curaid);
519568e8e04eSSam Leffler }
519668e8e04eSSam Leffler 
519768e8e04eSSam Leffler static void
519868e8e04eSSam Leffler ath_set_channel(struct ieee80211com *ic)
519968e8e04eSSam Leffler {
520068e8e04eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
520168e8e04eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
520268e8e04eSSam Leffler 
520368e8e04eSSam Leffler 	(void) ath_chan_set(sc, ic->ic_curchan);
520468e8e04eSSam Leffler 	/*
520568e8e04eSSam Leffler 	 * If we are returning to our bss channel then mark state
520668e8e04eSSam Leffler 	 * so the next recv'd beacon's tsf will be used to sync the
520768e8e04eSSam Leffler 	 * beacon timers.  Note that since we only hear beacons in
520868e8e04eSSam Leffler 	 * sta/ibss mode this has no effect in other operating modes.
520968e8e04eSSam Leffler 	 */
521068e8e04eSSam Leffler 	if (!sc->sc_scanning && ic->ic_curchan == ic->ic_bsschan)
521168e8e04eSSam Leffler 		sc->sc_syncbeacon = 1;
521268e8e04eSSam Leffler }
521368e8e04eSSam Leffler 
52145591b213SSam Leffler static int
521545bbf62fSSam Leffler ath_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
52165591b213SSam Leffler {
5217c42a7b7eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
521845bbf62fSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
521945bbf62fSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
52205591b213SSam Leffler 	struct ieee80211_node *ni;
522168e8e04eSSam Leffler 	int i, error, stamode;
52225591b213SSam Leffler 	u_int32_t rfilt;
52235591b213SSam Leffler 	static const HAL_LED_STATE leds[] = {
52245591b213SSam Leffler 	    HAL_LED_INIT,	/* IEEE80211_S_INIT */
52255591b213SSam Leffler 	    HAL_LED_SCAN,	/* IEEE80211_S_SCAN */
52265591b213SSam Leffler 	    HAL_LED_AUTH,	/* IEEE80211_S_AUTH */
52275591b213SSam Leffler 	    HAL_LED_ASSOC, 	/* IEEE80211_S_ASSOC */
522877d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_CAC */
52295591b213SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_RUN */
523077d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_CSA */
523177d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_SLEEP */
52325591b213SSam Leffler 	};
52335591b213SSam Leffler 
5234c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: %s -> %s\n", __func__,
523545bbf62fSSam Leffler 		ieee80211_state_name[ic->ic_state],
5236c42a7b7eSSam Leffler 		ieee80211_state_name[nstate]);
52375591b213SSam Leffler 
5238c42a7b7eSSam Leffler 	callout_stop(&sc->sc_cal_ch);
5239bd5a9920SSam Leffler 	callout_stop(&sc->sc_dfs_ch);
52405591b213SSam Leffler 	ath_hal_setledstate(ah, leds[nstate]);	/* set LED */
52415591b213SSam Leffler 
52425591b213SSam Leffler 	if (nstate == IEEE80211_S_INIT) {
524358769f58SSam Leffler 		/*
524458769f58SSam Leffler 		 * Shutdown host/driver operation:
524558769f58SSam Leffler 		 * o disable interrupts so we don't rx frames
524658769f58SSam Leffler 		 * o clean any pending items on the task q
524758769f58SSam Leffler 		 * o notify the rate control algorithm
524858769f58SSam Leffler 		 */
52495591b213SSam Leffler 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
5250e8fd88a3SSam Leffler 		ath_hal_intrset(ah, sc->sc_imask &~ HAL_INT_GLOBAL);
52516a1d2520SSam Leffler #if 0
525258769f58SSam Leffler 		/* XXX can't use taskqueue_drain 'cuz we're holding sc_mtx */
525358769f58SSam Leffler 		taskqueue_drain(sc->sc_tq, &sc->sc_rxtask);
525458769f58SSam Leffler 		taskqueue_drain(sc->sc_tq, &sc->sc_rxorntask);
525558769f58SSam Leffler 		taskqueue_drain(sc->sc_tq, &sc->sc_bmisstask);
525658769f58SSam Leffler 		taskqueue_drain(sc->sc_tq, &sc->sc_bstucktask);
52576a1d2520SSam Leffler #endif
5258c42a7b7eSSam Leffler 		ath_rate_newstate(sc, nstate);
5259c42a7b7eSSam Leffler 		goto done;
52605591b213SSam Leffler 	}
52615591b213SSam Leffler 	ni = ic->ic_bss;
52625591b213SSam Leffler 
526368e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
526468e8e04eSSam Leffler 	stamode = (sc->sc_opmode == HAL_M_STA || sc->sc_opmode == HAL_M_IBSS);
526568e8e04eSSam Leffler 	if (stamode && nstate == IEEE80211_S_RUN) {
526668e8e04eSSam Leffler 		sc->sc_curaid = ni->ni_associd;
526768e8e04eSSam Leffler 		IEEE80211_ADDR_COPY(sc->sc_curbssid, ni->ni_bssid);
526868e8e04eSSam Leffler 	} else
526968e8e04eSSam Leffler 		sc->sc_curaid = 0;
527068e8e04eSSam Leffler 
527168e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
527268e8e04eSSam Leffler 		 __func__, rfilt, ether_sprintf(sc->sc_curbssid),
527368e8e04eSSam Leffler 		 sc->sc_curaid);
527468e8e04eSSam Leffler 
527568e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
527668e8e04eSSam Leffler 	if (stamode)
527768e8e04eSSam Leffler 		ath_hal_setassocid(ah, sc->sc_curbssid, ni->ni_associd);
527868e8e04eSSam Leffler 
527968e8e04eSSam Leffler 	if (ic->ic_opmode != IEEE80211_M_STA &&
528068e8e04eSSam Leffler 	    (ic->ic_flags & IEEE80211_F_PRIVACY)) {
52815591b213SSam Leffler 		for (i = 0; i < IEEE80211_WEP_NKID; i++)
52825591b213SSam Leffler 			if (ath_hal_keyisvalid(ah, i))
528368e8e04eSSam Leffler 				ath_hal_keysetmac(ah, i, ni->ni_bssid);
52845591b213SSam Leffler 	}
52855591b213SSam Leffler 
5286c42a7b7eSSam Leffler 	/*
5287c42a7b7eSSam Leffler 	 * Notify the rate control algorithm so rates
5288c42a7b7eSSam Leffler 	 * are setup should ath_beacon_alloc be called.
5289c42a7b7eSSam Leffler 	 */
5290c42a7b7eSSam Leffler 	ath_rate_newstate(sc, nstate);
5291c42a7b7eSSam Leffler 
529268e8e04eSSam Leffler 	if (nstate == IEEE80211_S_RUN) {
5293c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_STATE,
5294c42a7b7eSSam Leffler 			"%s(RUN): ic_flags=0x%08x iv=%d bssid=%s "
52955591b213SSam Leffler 			"capinfo=0x%04x chan=%d\n"
52965591b213SSam Leffler 			 , __func__
52975591b213SSam Leffler 			 , ic->ic_flags
52985591b213SSam Leffler 			 , ni->ni_intval
52995591b213SSam Leffler 			 , ether_sprintf(ni->ni_bssid)
53005591b213SSam Leffler 			 , ni->ni_capinfo
5301b5c99415SSam Leffler 			 , ieee80211_chan2ieee(ic, ic->ic_curchan));
53025591b213SSam Leffler 
5303e8fd88a3SSam Leffler 		switch (ic->ic_opmode) {
5304e8fd88a3SSam Leffler 		case IEEE80211_M_HOSTAP:
5305e8fd88a3SSam Leffler 		case IEEE80211_M_IBSS:
53065591b213SSam Leffler 			/*
5307e8fd88a3SSam Leffler 			 * Allocate and setup the beacon frame.
5308e8fd88a3SSam Leffler 			 *
5309f818612bSSam Leffler 			 * Stop any previous beacon DMA.  This may be
5310f818612bSSam Leffler 			 * necessary, for example, when an ibss merge
5311f818612bSSam Leffler 			 * causes reconfiguration; there will be a state
5312f818612bSSam Leffler 			 * transition from RUN->RUN that means we may
5313f818612bSSam Leffler 			 * be called with beacon transmission active.
5314f818612bSSam Leffler 			 */
5315f818612bSSam Leffler 			ath_hal_stoptxdma(ah, sc->sc_bhalq);
5316f818612bSSam Leffler 			ath_beacon_free(sc);
53175591b213SSam Leffler 			error = ath_beacon_alloc(sc, ni);
53185591b213SSam Leffler 			if (error != 0)
53195591b213SSam Leffler 				goto bad;
53207a04dc27SSam Leffler 			/*
532180d939bfSSam Leffler 			 * If joining an adhoc network defer beacon timer
532280d939bfSSam Leffler 			 * configuration to the next beacon frame so we
532380d939bfSSam Leffler 			 * have a current TSF to use.  Otherwise we're
532480d939bfSSam Leffler 			 * starting an ibss/bss so there's no need to delay.
53257a04dc27SSam Leffler 			 */
532680d939bfSSam Leffler 			if (ic->ic_opmode == IEEE80211_M_IBSS &&
532780d939bfSSam Leffler 			    ic->ic_bss->ni_tstamp.tsf != 0)
532880d939bfSSam Leffler 				sc->sc_syncbeacon = 1;
532980d939bfSSam Leffler 			else
53307a04dc27SSam Leffler 				ath_beacon_config(sc);
5331e8fd88a3SSam Leffler 			break;
5332e8fd88a3SSam Leffler 		case IEEE80211_M_STA:
5333e8fd88a3SSam Leffler 			/*
5334e8fd88a3SSam Leffler 			 * Allocate a key cache slot to the station.
5335e8fd88a3SSam Leffler 			 */
5336e8fd88a3SSam Leffler 			if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0 &&
5337e8fd88a3SSam Leffler 			    sc->sc_hasclrkey &&
5338e8fd88a3SSam Leffler 			    ni->ni_ucastkey.wk_keyix == IEEE80211_KEYIX_NONE)
5339e8fd88a3SSam Leffler 				ath_setup_stationkey(ni);
53407a04dc27SSam Leffler 			/*
534180d939bfSSam Leffler 			 * Defer beacon timer configuration to the next
534280d939bfSSam Leffler 			 * beacon frame so we have a current TSF to use
534380d939bfSSam Leffler 			 * (any TSF collected when scanning is likely old).
53447a04dc27SSam Leffler 			 */
534580d939bfSSam Leffler 			sc->sc_syncbeacon = 1;
5346e8fd88a3SSam Leffler 			break;
5347e8fd88a3SSam Leffler 		default:
5348e8fd88a3SSam Leffler 			break;
53495591b213SSam Leffler 		}
53505591b213SSam Leffler 		/*
53517b0c77ecSSam Leffler 		 * Let the hal process statistics collected during a
53527b0c77ecSSam Leffler 		 * scan so it can provide calibrated noise floor data.
53537b0c77ecSSam Leffler 		 */
53547b0c77ecSSam Leffler 		ath_hal_process_noisefloor(ah);
53557b0c77ecSSam Leffler 		/*
5356ffa2cab6SSam Leffler 		 * Reset rssi stats; maybe not the best place...
5357ffa2cab6SSam Leffler 		 */
5358ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
5359ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
5360ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
53615591b213SSam Leffler 	} else {
5362c42a7b7eSSam Leffler 		ath_hal_intrset(ah,
5363c42a7b7eSSam Leffler 		    sc->sc_imask &~ (HAL_INT_SWBA | HAL_INT_BMISS));
53645591b213SSam Leffler 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
53655591b213SSam Leffler 	}
5366c42a7b7eSSam Leffler done:
536745bbf62fSSam Leffler 	/*
536845bbf62fSSam Leffler 	 * Invoke the parent method to complete the work.
536945bbf62fSSam Leffler 	 */
5370c42a7b7eSSam Leffler 	error = sc->sc_newstate(ic, nstate, arg);
5371c42a7b7eSSam Leffler 	/*
5372c42a7b7eSSam Leffler 	 * Finally, start any timers.
5373c42a7b7eSSam Leffler 	 */
5374c42a7b7eSSam Leffler 	if (nstate == IEEE80211_S_RUN) {
5375c42a7b7eSSam Leffler 		/* start periodic recalibration timer */
5376bd5a9920SSam Leffler 		callout_reset(&sc->sc_cal_ch, sc->sc_calinterval * hz,
5377c42a7b7eSSam Leffler 			ath_calibrate, sc);
5378c42a7b7eSSam Leffler 	}
53795591b213SSam Leffler bad:
53805591b213SSam Leffler 	return error;
53815591b213SSam Leffler }
53825591b213SSam Leffler 
53835591b213SSam Leffler /*
5384e8fd88a3SSam Leffler  * Allocate a key cache slot to the station so we can
5385e8fd88a3SSam Leffler  * setup a mapping from key index to node. The key cache
5386e8fd88a3SSam Leffler  * slot is needed for managing antenna state and for
5387e8fd88a3SSam Leffler  * compression when stations do not use crypto.  We do
5388e8fd88a3SSam Leffler  * it uniliaterally here; if crypto is employed this slot
5389e8fd88a3SSam Leffler  * will be reassigned.
5390e8fd88a3SSam Leffler  */
5391e8fd88a3SSam Leffler static void
5392e8fd88a3SSam Leffler ath_setup_stationkey(struct ieee80211_node *ni)
5393e8fd88a3SSam Leffler {
5394e8fd88a3SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
5395e8fd88a3SSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
5396c1225b52SSam Leffler 	ieee80211_keyix keyix, rxkeyix;
5397e8fd88a3SSam Leffler 
5398c1225b52SSam Leffler 	if (!ath_key_alloc(ic, &ni->ni_ucastkey, &keyix, &rxkeyix)) {
5399e8fd88a3SSam Leffler 		/*
5400e8fd88a3SSam Leffler 		 * Key cache is full; we'll fall back to doing
5401e8fd88a3SSam Leffler 		 * the more expensive lookup in software.  Note
5402e8fd88a3SSam Leffler 		 * this also means no h/w compression.
5403e8fd88a3SSam Leffler 		 */
5404e8fd88a3SSam Leffler 		/* XXX msg+statistic */
5405e8fd88a3SSam Leffler 	} else {
5406c1225b52SSam Leffler 		/* XXX locking? */
5407e8fd88a3SSam Leffler 		ni->ni_ucastkey.wk_keyix = keyix;
5408c1225b52SSam Leffler 		ni->ni_ucastkey.wk_rxkeyix = rxkeyix;
5409e8fd88a3SSam Leffler 		/* NB: this will create a pass-thru key entry */
5410e8fd88a3SSam Leffler 		ath_keyset(sc, &ni->ni_ucastkey, ni->ni_macaddr, ic->ic_bss);
5411e8fd88a3SSam Leffler 	}
5412e8fd88a3SSam Leffler }
5413e8fd88a3SSam Leffler 
5414e8fd88a3SSam Leffler /*
54155591b213SSam Leffler  * Setup driver-specific state for a newly associated node.
54165591b213SSam Leffler  * Note that we're called also on a re-associate, the isnew
54175591b213SSam Leffler  * param tells us if this is the first time or not.
54185591b213SSam Leffler  */
54195591b213SSam Leffler static void
5420e9962332SSam Leffler ath_newassoc(struct ieee80211_node *ni, int isnew)
54215591b213SSam Leffler {
5422e9962332SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
5423c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
54245591b213SSam Leffler 
5425c42a7b7eSSam Leffler 	ath_rate_newassoc(sc, ATH_NODE(ni), isnew);
5426e8fd88a3SSam Leffler 	if (isnew &&
5427e8fd88a3SSam Leffler 	    (ic->ic_flags & IEEE80211_F_PRIVACY) == 0 && sc->sc_hasclrkey) {
5428e8fd88a3SSam Leffler 		KASSERT(ni->ni_ucastkey.wk_keyix == IEEE80211_KEYIX_NONE,
5429e8fd88a3SSam Leffler 		    ("new assoc with a unicast key already setup (keyix %u)",
5430e8fd88a3SSam Leffler 		    ni->ni_ucastkey.wk_keyix));
5431e8fd88a3SSam Leffler 		ath_setup_stationkey(ni);
5432e8fd88a3SSam Leffler 	}
54335591b213SSam Leffler }
54345591b213SSam Leffler 
54355591b213SSam Leffler static int
5436aaa70f2fSSam Leffler ath_getchannels(struct ath_softc *sc,
5437aaa70f2fSSam Leffler     HAL_REG_DOMAIN rd, HAL_CTRY_CODE cc, HAL_BOOL outdoor, HAL_BOOL xchanmode)
54385591b213SSam Leffler {
54395591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
5440fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
54415591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
54425591b213SSam Leffler 	HAL_CHANNEL *chans;
544368e8e04eSSam Leffler 	int i, nchan;
5444aaa70f2fSSam Leffler 	u_int32_t regdomain;
54455591b213SSam Leffler 
54465591b213SSam Leffler 	chans = malloc(IEEE80211_CHAN_MAX * sizeof(HAL_CHANNEL),
54475591b213SSam Leffler 			M_TEMP, M_NOWAIT);
54485591b213SSam Leffler 	if (chans == NULL) {
54495591b213SSam Leffler 		if_printf(ifp, "unable to allocate channel table\n");
54505591b213SSam Leffler 		return ENOMEM;
54515591b213SSam Leffler 	}
54525591b213SSam Leffler 	if (!ath_hal_init_channels(ah, chans, IEEE80211_CHAN_MAX, &nchan,
5453aaa70f2fSSam Leffler 	    NULL, 0, NULL, cc, HAL_MODE_ALL, outdoor, xchanmode)) {
5454ee7d6840SSam Leffler 		(void) ath_hal_getregdomain(ah, &regdomain);
5455c42a7b7eSSam Leffler 		if_printf(ifp, "unable to collect channel list from hal; "
5456aaa70f2fSSam Leffler 			"regdomain likely %u country code %u\n", regdomain, cc);
54575591b213SSam Leffler 		free(chans, M_TEMP);
54585591b213SSam Leffler 		return EINVAL;
54595591b213SSam Leffler 	}
54605591b213SSam Leffler 
54615591b213SSam Leffler 	/*
546268e8e04eSSam Leffler 	 * Convert HAL channels to ieee80211 ones.
54635591b213SSam Leffler 	 */
5464aaa70f2fSSam Leffler 	memset(ic->ic_channels, 0, sizeof(ic->ic_channels));
54655591b213SSam Leffler 	for (i = 0; i < nchan; i++) {
54665591b213SSam Leffler 		HAL_CHANNEL *c = &chans[i];
546768e8e04eSSam Leffler 		struct ieee80211_channel *ichan = &ic->ic_channels[i];
5468bd5a9920SSam Leffler 
546968e8e04eSSam Leffler 		ichan->ic_ieee = ath_hal_mhz2ieee(ah, c->channel,
547068e8e04eSSam Leffler 					c->channelFlags);
5471724c193aSSam Leffler 		if (bootverbose)
5472724c193aSSam Leffler 			if_printf(ifp, "hal channel %u/%x -> %u\n",
547368e8e04eSSam Leffler 			    c->channel, c->channelFlags, ichan->ic_ieee);
547468e8e04eSSam Leffler 		ichan->ic_freq = c->channel;
547568e8e04eSSam Leffler 
547668e8e04eSSam Leffler 		if ((c->channelFlags & CHANNEL_PUREG) == CHANNEL_PUREG) {
5477bd5a9920SSam Leffler 			/*
547868e8e04eSSam Leffler 			 * Except for AR5211, HAL's PUREG means mixed
547968e8e04eSSam Leffler 			 * DSSS and OFDM.
5480bd5a9920SSam Leffler 			 */
548168e8e04eSSam Leffler 			ichan->ic_flags = c->channelFlags &~ CHANNEL_PUREG;
548268e8e04eSSam Leffler 			ichan->ic_flags |= IEEE80211_CHAN_G;
548368e8e04eSSam Leffler 		} else {
548468e8e04eSSam Leffler 			ichan->ic_flags = c->channelFlags;
548568e8e04eSSam Leffler 		}
548668e8e04eSSam Leffler 
5487724c193aSSam Leffler 		if (ath_hal_isgsmsku(ah)) {
5488724c193aSSam Leffler 			/* remap to true frequencies */
548968e8e04eSSam Leffler 			ichan->ic_freq = 922 + (2422 - ichan->ic_freq);
549068e8e04eSSam Leffler 			ichan->ic_flags |= IEEE80211_CHAN_GSM;
549168e8e04eSSam Leffler 			ichan->ic_ieee = ieee80211_mhz2ieee(ichan->ic_freq,
549268e8e04eSSam Leffler 						    ichan->ic_flags);
5493724c193aSSam Leffler 		}
549468e8e04eSSam Leffler 		ichan->ic_maxregpower = c->maxRegTxPower;	/* dBm */
549568e8e04eSSam Leffler 		ichan->ic_maxpower = c->maxTxPower;		/* 1/2 dBm */
549668e8e04eSSam Leffler 		ichan->ic_minpower = c->minTxPower;		/* 1/2 dBm */
54975591b213SSam Leffler 	}
549868e8e04eSSam Leffler 	ic->ic_nchans = nchan;
54995591b213SSam Leffler 	free(chans, M_TEMP);
5500ee7d6840SSam Leffler 	(void) ath_hal_getregdomain(ah, &sc->sc_regdomain);
5501aaa70f2fSSam Leffler 	ath_hal_getcountrycode(ah, &sc->sc_countrycode);
5502aaa70f2fSSam Leffler 	sc->sc_xchanmode = xchanmode;
5503aaa70f2fSSam Leffler 	sc->sc_outdoor = outdoor;
55045591b213SSam Leffler 	return 0;
55055591b213SSam Leffler }
55065591b213SSam Leffler 
5507c42a7b7eSSam Leffler static void
55083e50ec2cSSam Leffler ath_led_done(void *arg)
5509c42a7b7eSSam Leffler {
55103e50ec2cSSam Leffler 	struct ath_softc *sc = arg;
55113e50ec2cSSam Leffler 
55123e50ec2cSSam Leffler 	sc->sc_blinking = 0;
55133e50ec2cSSam Leffler }
5514c42a7b7eSSam Leffler 
5515c42a7b7eSSam Leffler /*
55163e50ec2cSSam Leffler  * Turn the LED off: flip the pin and then set a timer so no
55173e50ec2cSSam Leffler  * update will happen for the specified duration.
5518c42a7b7eSSam Leffler  */
55193e50ec2cSSam Leffler static void
55203e50ec2cSSam Leffler ath_led_off(void *arg)
55213e50ec2cSSam Leffler {
55223e50ec2cSSam Leffler 	struct ath_softc *sc = arg;
55233e50ec2cSSam Leffler 
55243e50ec2cSSam Leffler 	ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, !sc->sc_ledon);
55253e50ec2cSSam Leffler 	callout_reset(&sc->sc_ledtimer, sc->sc_ledoff, ath_led_done, sc);
5526c42a7b7eSSam Leffler }
55273e50ec2cSSam Leffler 
55283e50ec2cSSam Leffler /*
55293e50ec2cSSam Leffler  * Blink the LED according to the specified on/off times.
55303e50ec2cSSam Leffler  */
55313e50ec2cSSam Leffler static void
55323e50ec2cSSam Leffler ath_led_blink(struct ath_softc *sc, int on, int off)
55333e50ec2cSSam Leffler {
55343e50ec2cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_LED, "%s: on %u off %u\n", __func__, on, off);
55353e50ec2cSSam Leffler 	ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, sc->sc_ledon);
55363e50ec2cSSam Leffler 	sc->sc_blinking = 1;
55373e50ec2cSSam Leffler 	sc->sc_ledoff = off;
55383e50ec2cSSam Leffler 	callout_reset(&sc->sc_ledtimer, on, ath_led_off, sc);
55393e50ec2cSSam Leffler }
55403e50ec2cSSam Leffler 
55413e50ec2cSSam Leffler static void
55423e50ec2cSSam Leffler ath_led_event(struct ath_softc *sc, int event)
55433e50ec2cSSam Leffler {
55443e50ec2cSSam Leffler 
55453e50ec2cSSam Leffler 	sc->sc_ledevent = ticks;	/* time of last event */
55463e50ec2cSSam Leffler 	if (sc->sc_blinking)		/* don't interrupt active blink */
55473e50ec2cSSam Leffler 		return;
55483e50ec2cSSam Leffler 	switch (event) {
55493e50ec2cSSam Leffler 	case ATH_LED_POLL:
55503e50ec2cSSam Leffler 		ath_led_blink(sc, sc->sc_hwmap[0].ledon,
55513e50ec2cSSam Leffler 			sc->sc_hwmap[0].ledoff);
55523e50ec2cSSam Leffler 		break;
55533e50ec2cSSam Leffler 	case ATH_LED_TX:
55543e50ec2cSSam Leffler 		ath_led_blink(sc, sc->sc_hwmap[sc->sc_txrate].ledon,
55553e50ec2cSSam Leffler 			sc->sc_hwmap[sc->sc_txrate].ledoff);
55563e50ec2cSSam Leffler 		break;
55573e50ec2cSSam Leffler 	case ATH_LED_RX:
55583e50ec2cSSam Leffler 		ath_led_blink(sc, sc->sc_hwmap[sc->sc_rxrate].ledon,
55593e50ec2cSSam Leffler 			sc->sc_hwmap[sc->sc_rxrate].ledoff);
55603e50ec2cSSam Leffler 		break;
5561c42a7b7eSSam Leffler 	}
5562c42a7b7eSSam Leffler }
5563c42a7b7eSSam Leffler 
5564c42a7b7eSSam Leffler static void
5565c42a7b7eSSam Leffler ath_update_txpow(struct ath_softc *sc)
5566c42a7b7eSSam Leffler {
5567c42a7b7eSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
5568c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5569c42a7b7eSSam Leffler 	u_int32_t txpow;
5570c42a7b7eSSam Leffler 
5571c42a7b7eSSam Leffler 	if (sc->sc_curtxpow != ic->ic_txpowlimit) {
5572c42a7b7eSSam Leffler 		ath_hal_settxpowlimit(ah, ic->ic_txpowlimit);
5573c42a7b7eSSam Leffler 		/* read back in case value is clamped */
5574ee7d6840SSam Leffler 		if (ath_hal_gettxpowlimit(ah, &txpow))
5575c42a7b7eSSam Leffler 			ic->ic_txpowlimit = sc->sc_curtxpow = txpow;
5576c42a7b7eSSam Leffler 	}
5577c42a7b7eSSam Leffler 	/*
5578c42a7b7eSSam Leffler 	 * Fetch max tx power level for status requests.
5579c42a7b7eSSam Leffler 	 */
5580ee7d6840SSam Leffler 	if (ath_hal_getmaxtxpow(sc->sc_ah, &txpow))
5581c42a7b7eSSam Leffler 		ic->ic_bss->ni_txpower = txpow;
5582c42a7b7eSSam Leffler }
5583c42a7b7eSSam Leffler 
55846c4612b9SSam Leffler static int
55856c4612b9SSam Leffler ath_rate_setup(struct ath_softc *sc, u_int mode)
55866c4612b9SSam Leffler {
55876c4612b9SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
55886c4612b9SSam Leffler 	const HAL_RATE_TABLE *rt;
55896c4612b9SSam Leffler 
55906c4612b9SSam Leffler 	switch (mode) {
55916c4612b9SSam Leffler 	case IEEE80211_MODE_11A:
55926c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A);
55936c4612b9SSam Leffler 		break;
5594724c193aSSam Leffler 	case IEEE80211_MODE_HALF:
5595aaa70f2fSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_HALF_RATE);
5596aaa70f2fSSam Leffler 		break;
5597724c193aSSam Leffler 	case IEEE80211_MODE_QUARTER:
5598aaa70f2fSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_QUARTER_RATE);
5599aaa70f2fSSam Leffler 		break;
56006c4612b9SSam Leffler 	case IEEE80211_MODE_11B:
56016c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11B);
56026c4612b9SSam Leffler 		break;
56036c4612b9SSam Leffler 	case IEEE80211_MODE_11G:
56046c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11G);
56056c4612b9SSam Leffler 		break;
56066c4612b9SSam Leffler 	case IEEE80211_MODE_TURBO_A:
560768e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_108A);
56086c4612b9SSam Leffler 		break;
56096c4612b9SSam Leffler 	case IEEE80211_MODE_TURBO_G:
56106c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_108G);
56116c4612b9SSam Leffler 		break;
561268e8e04eSSam Leffler 	case IEEE80211_MODE_STURBO_A:
561368e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_TURBO);
561468e8e04eSSam Leffler 		break;
561568e8e04eSSam Leffler 	case IEEE80211_MODE_11NA:
561668e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11NA_HT20);
561768e8e04eSSam Leffler 		break;
561868e8e04eSSam Leffler 	case IEEE80211_MODE_11NG:
561968e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11NG_HT20);
562068e8e04eSSam Leffler 		break;
56216c4612b9SSam Leffler 	default:
56226c4612b9SSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid mode %u\n",
56236c4612b9SSam Leffler 			__func__, mode);
56246c4612b9SSam Leffler 		return 0;
56256c4612b9SSam Leffler 	}
56266c4612b9SSam Leffler 	sc->sc_rates[mode] = rt;
5627aaa70f2fSSam Leffler 	return (rt != NULL);
56285591b213SSam Leffler }
56295591b213SSam Leffler 
56305591b213SSam Leffler static void
56315591b213SSam Leffler ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode)
56325591b213SSam Leffler {
56333e50ec2cSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
56343e50ec2cSSam Leffler 	/* NB: on/off times from the Atheros NDIS driver, w/ permission */
56353e50ec2cSSam Leffler 	static const struct {
56363e50ec2cSSam Leffler 		u_int		rate;		/* tx/rx 802.11 rate */
56373e50ec2cSSam Leffler 		u_int16_t	timeOn;		/* LED on time (ms) */
56383e50ec2cSSam Leffler 		u_int16_t	timeOff;	/* LED off time (ms) */
56393e50ec2cSSam Leffler 	} blinkrates[] = {
56403e50ec2cSSam Leffler 		{ 108,  40,  10 },
56413e50ec2cSSam Leffler 		{  96,  44,  11 },
56423e50ec2cSSam Leffler 		{  72,  50,  13 },
56433e50ec2cSSam Leffler 		{  48,  57,  14 },
56443e50ec2cSSam Leffler 		{  36,  67,  16 },
56453e50ec2cSSam Leffler 		{  24,  80,  20 },
56463e50ec2cSSam Leffler 		{  22, 100,  25 },
56473e50ec2cSSam Leffler 		{  18, 133,  34 },
56483e50ec2cSSam Leffler 		{  12, 160,  40 },
56493e50ec2cSSam Leffler 		{  10, 200,  50 },
56503e50ec2cSSam Leffler 		{   6, 240,  58 },
56513e50ec2cSSam Leffler 		{   4, 267,  66 },
56523e50ec2cSSam Leffler 		{   2, 400, 100 },
56533e50ec2cSSam Leffler 		{   0, 500, 130 },
5654724c193aSSam Leffler 		/* XXX half/quarter rates */
56553e50ec2cSSam Leffler 	};
56565591b213SSam Leffler 	const HAL_RATE_TABLE *rt;
56573e50ec2cSSam Leffler 	int i, j;
56585591b213SSam Leffler 
56595591b213SSam Leffler 	memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap));
56605591b213SSam Leffler 	rt = sc->sc_rates[mode];
56615591b213SSam Leffler 	KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode));
56625591b213SSam Leffler 	for (i = 0; i < rt->rateCount; i++)
56635591b213SSam Leffler 		sc->sc_rixmap[rt->info[i].dot11Rate & IEEE80211_RATE_VAL] = i;
56641b1a8e41SSam Leffler 	memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap));
5665c42a7b7eSSam Leffler 	for (i = 0; i < 32; i++) {
5666c42a7b7eSSam Leffler 		u_int8_t ix = rt->rateCodeToIndex[i];
56673e50ec2cSSam Leffler 		if (ix == 0xff) {
56683e50ec2cSSam Leffler 			sc->sc_hwmap[i].ledon = (500 * hz) / 1000;
56693e50ec2cSSam Leffler 			sc->sc_hwmap[i].ledoff = (130 * hz) / 1000;
567016b4851aSSam Leffler 			continue;
56713e50ec2cSSam Leffler 		}
56723e50ec2cSSam Leffler 		sc->sc_hwmap[i].ieeerate =
56733e50ec2cSSam Leffler 			rt->info[ix].dot11Rate & IEEE80211_RATE_VAL;
567468e8e04eSSam Leffler 		if (rt->info[ix].phy == IEEE80211_T_HT)
567568e8e04eSSam Leffler 			sc->sc_hwmap[i].ieeerate |= 0x80;	/* MCS */
5676d3be6f5bSSam Leffler 		sc->sc_hwmap[i].txflags = IEEE80211_RADIOTAP_F_DATAPAD;
567716b4851aSSam Leffler 		if (rt->info[ix].shortPreamble ||
567816b4851aSSam Leffler 		    rt->info[ix].phy == IEEE80211_T_OFDM)
5679d3be6f5bSSam Leffler 			sc->sc_hwmap[i].txflags |= IEEE80211_RADIOTAP_F_SHORTPRE;
5680d3be6f5bSSam Leffler 		/* NB: receive frames include FCS */
5681d3be6f5bSSam Leffler 		sc->sc_hwmap[i].rxflags = sc->sc_hwmap[i].txflags |
5682d3be6f5bSSam Leffler 			IEEE80211_RADIOTAP_F_FCS;
56833e50ec2cSSam Leffler 		/* setup blink rate table to avoid per-packet lookup */
56843e50ec2cSSam Leffler 		for (j = 0; j < N(blinkrates)-1; j++)
56853e50ec2cSSam Leffler 			if (blinkrates[j].rate == sc->sc_hwmap[i].ieeerate)
56863e50ec2cSSam Leffler 				break;
56873e50ec2cSSam Leffler 		/* NB: this uses the last entry if the rate isn't found */
56883e50ec2cSSam Leffler 		/* XXX beware of overlow */
56893e50ec2cSSam Leffler 		sc->sc_hwmap[i].ledon = (blinkrates[j].timeOn * hz) / 1000;
56903e50ec2cSSam Leffler 		sc->sc_hwmap[i].ledoff = (blinkrates[j].timeOff * hz) / 1000;
5691c42a7b7eSSam Leffler 	}
56925591b213SSam Leffler 	sc->sc_currates = rt;
56935591b213SSam Leffler 	sc->sc_curmode = mode;
56945591b213SSam Leffler 	/*
5695c42a7b7eSSam Leffler 	 * All protection frames are transmited at 2Mb/s for
5696c42a7b7eSSam Leffler 	 * 11g, otherwise at 1Mb/s.
56975591b213SSam Leffler 	 */
5698913a1ba1SSam Leffler 	if (mode == IEEE80211_MODE_11G)
5699913a1ba1SSam Leffler 		sc->sc_protrix = ath_tx_findrix(rt, 2*2);
5700913a1ba1SSam Leffler 	else
5701913a1ba1SSam Leffler 		sc->sc_protrix = ath_tx_findrix(rt, 2*1);
570255f63772SSam Leffler 	/* rate index used to send management frames */
570355f63772SSam Leffler 	sc->sc_minrateix = 0;
57048b5341deSSam Leffler 	/*
57058b5341deSSam Leffler 	 * Setup multicast rate state.
57068b5341deSSam Leffler 	 */
57078b5341deSSam Leffler 	/* XXX layering violation */
57088b5341deSSam Leffler 	sc->sc_mcastrix = ath_tx_findrix(rt, sc->sc_ic.ic_mcast_rate);
57098b5341deSSam Leffler 	sc->sc_mcastrate = sc->sc_ic.ic_mcast_rate;
5710c42a7b7eSSam Leffler 	/* NB: caller is responsible for reseting rate control state */
57113e50ec2cSSam Leffler #undef N
57125591b213SSam Leffler }
57135591b213SSam Leffler 
5714a585a9a1SSam Leffler #ifdef ATH_DEBUG
57155591b213SSam Leffler static void
571665f9edeeSSam Leffler ath_printrxbuf(const struct ath_buf *bf, u_int ix, int done)
57175591b213SSam Leffler {
571865f9edeeSSam Leffler 	const struct ath_rx_status *rs = &bf->bf_status.ds_rxstat;
571965f9edeeSSam Leffler 	const struct ath_desc *ds;
57205591b213SSam Leffler 	int i;
57215591b213SSam Leffler 
57225591b213SSam Leffler 	for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) {
57237a4c5ed9SSam Leffler 		printf("R[%2u] (DS.V:%p DS.P:%p) L:%08x D:%08x%s\n"
57247a4c5ed9SSam Leffler 		       "      %08x %08x %08x %08x\n",
572565f9edeeSSam Leffler 		    ix, ds, (const struct ath_desc *)bf->bf_daddr + i,
57265591b213SSam Leffler 		    ds->ds_link, ds->ds_data,
572765f9edeeSSam Leffler 		    !done ? "" : (rs->rs_status == 0) ? " *" : " !",
57285591b213SSam Leffler 		    ds->ds_ctl0, ds->ds_ctl1,
57297a4c5ed9SSam Leffler 		    ds->ds_hw[0], ds->ds_hw[1]);
57305591b213SSam Leffler 	}
57315591b213SSam Leffler }
57325591b213SSam Leffler 
57335591b213SSam Leffler static void
573465f9edeeSSam Leffler ath_printtxbuf(const struct ath_buf *bf, u_int qnum, u_int ix, int done)
57355591b213SSam Leffler {
573665f9edeeSSam Leffler 	const struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
573765f9edeeSSam Leffler 	const struct ath_desc *ds;
57385591b213SSam Leffler 	int i;
57395591b213SSam Leffler 
57407a4c5ed9SSam Leffler 	printf("Q%u[%3u]", qnum, ix);
57415591b213SSam Leffler 	for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) {
5742ebecf802SSam Leffler 		printf(" (DS.V:%p DS.P:%p) L:%08x D:%08x F:04%x%s\n"
57437a4c5ed9SSam Leffler 		       "        %08x %08x %08x %08x %08x %08x\n",
574465f9edeeSSam Leffler 		    ds, (const struct ath_desc *)bf->bf_daddr + i,
5745ebecf802SSam Leffler 		    ds->ds_link, ds->ds_data, bf->bf_flags,
574665f9edeeSSam Leffler 		    !done ? "" : (ts->ts_status == 0) ? " *" : " !",
57475591b213SSam Leffler 		    ds->ds_ctl0, ds->ds_ctl1,
57487a4c5ed9SSam Leffler 		    ds->ds_hw[0], ds->ds_hw[1], ds->ds_hw[2], ds->ds_hw[3]);
57495591b213SSam Leffler 	}
57505591b213SSam Leffler }
5751a585a9a1SSam Leffler #endif /* ATH_DEBUG */
5752c42a7b7eSSam Leffler 
5753c42a7b7eSSam Leffler static void
5754c42a7b7eSSam Leffler ath_watchdog(struct ifnet *ifp)
5755c42a7b7eSSam Leffler {
5756c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
5757c42a7b7eSSam Leffler 
575868e8e04eSSam Leffler 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) && !sc->sc_invalid) {
5759c42a7b7eSSam Leffler 		if_printf(ifp, "device timeout\n");
5760c42a7b7eSSam Leffler 		ath_reset(ifp);
5761c42a7b7eSSam Leffler 		ifp->if_oerrors++;
5762c42a7b7eSSam Leffler 		sc->sc_stats.ast_watchdog++;
5763c42a7b7eSSam Leffler 	}
5764c42a7b7eSSam Leffler }
5765c42a7b7eSSam Leffler 
5766a585a9a1SSam Leffler #ifdef ATH_DIAGAPI
5767c42a7b7eSSam Leffler /*
5768c42a7b7eSSam Leffler  * Diagnostic interface to the HAL.  This is used by various
5769c42a7b7eSSam Leffler  * tools to do things like retrieve register contents for
5770c42a7b7eSSam Leffler  * debugging.  The mechanism is intentionally opaque so that
5771c42a7b7eSSam Leffler  * it can change frequently w/o concern for compatiblity.
5772c42a7b7eSSam Leffler  */
5773c42a7b7eSSam Leffler static int
5774c42a7b7eSSam Leffler ath_ioctl_diag(struct ath_softc *sc, struct ath_diag *ad)
5775c42a7b7eSSam Leffler {
5776c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5777c42a7b7eSSam Leffler 	u_int id = ad->ad_id & ATH_DIAG_ID;
5778c42a7b7eSSam Leffler 	void *indata = NULL;
5779c42a7b7eSSam Leffler 	void *outdata = NULL;
5780c42a7b7eSSam Leffler 	u_int32_t insize = ad->ad_in_size;
5781c42a7b7eSSam Leffler 	u_int32_t outsize = ad->ad_out_size;
5782c42a7b7eSSam Leffler 	int error = 0;
5783c42a7b7eSSam Leffler 
5784c42a7b7eSSam Leffler 	if (ad->ad_id & ATH_DIAG_IN) {
5785c42a7b7eSSam Leffler 		/*
5786c42a7b7eSSam Leffler 		 * Copy in data.
5787c42a7b7eSSam Leffler 		 */
5788c42a7b7eSSam Leffler 		indata = malloc(insize, M_TEMP, M_NOWAIT);
5789c42a7b7eSSam Leffler 		if (indata == NULL) {
5790c42a7b7eSSam Leffler 			error = ENOMEM;
5791c42a7b7eSSam Leffler 			goto bad;
5792c42a7b7eSSam Leffler 		}
5793c42a7b7eSSam Leffler 		error = copyin(ad->ad_in_data, indata, insize);
5794c42a7b7eSSam Leffler 		if (error)
5795c42a7b7eSSam Leffler 			goto bad;
5796c42a7b7eSSam Leffler 	}
5797c42a7b7eSSam Leffler 	if (ad->ad_id & ATH_DIAG_DYN) {
5798c42a7b7eSSam Leffler 		/*
5799c42a7b7eSSam Leffler 		 * Allocate a buffer for the results (otherwise the HAL
5800c42a7b7eSSam Leffler 		 * returns a pointer to a buffer where we can read the
5801c42a7b7eSSam Leffler 		 * results).  Note that we depend on the HAL leaving this
5802c42a7b7eSSam Leffler 		 * pointer for us to use below in reclaiming the buffer;
5803c42a7b7eSSam Leffler 		 * may want to be more defensive.
5804c42a7b7eSSam Leffler 		 */
5805c42a7b7eSSam Leffler 		outdata = malloc(outsize, M_TEMP, M_NOWAIT);
5806c42a7b7eSSam Leffler 		if (outdata == NULL) {
5807c42a7b7eSSam Leffler 			error = ENOMEM;
5808c42a7b7eSSam Leffler 			goto bad;
5809c42a7b7eSSam Leffler 		}
5810c42a7b7eSSam Leffler 	}
5811c42a7b7eSSam Leffler 	if (ath_hal_getdiagstate(ah, id, indata, insize, &outdata, &outsize)) {
5812c42a7b7eSSam Leffler 		if (outsize < ad->ad_out_size)
5813c42a7b7eSSam Leffler 			ad->ad_out_size = outsize;
5814c42a7b7eSSam Leffler 		if (outdata != NULL)
5815c42a7b7eSSam Leffler 			error = copyout(outdata, ad->ad_out_data,
5816c42a7b7eSSam Leffler 					ad->ad_out_size);
5817c42a7b7eSSam Leffler 	} else {
5818c42a7b7eSSam Leffler 		error = EINVAL;
5819c42a7b7eSSam Leffler 	}
5820c42a7b7eSSam Leffler bad:
5821c42a7b7eSSam Leffler 	if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
5822c42a7b7eSSam Leffler 		free(indata, M_TEMP);
5823c42a7b7eSSam Leffler 	if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
5824c42a7b7eSSam Leffler 		free(outdata, M_TEMP);
5825c42a7b7eSSam Leffler 	return error;
5826c42a7b7eSSam Leffler }
5827a585a9a1SSam Leffler #endif /* ATH_DIAGAPI */
5828c42a7b7eSSam Leffler 
5829c42a7b7eSSam Leffler static int
5830c42a7b7eSSam Leffler ath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
5831c42a7b7eSSam Leffler {
5832c42a7b7eSSam Leffler #define	IS_RUNNING(ifp) \
583313f4c340SRobert Watson 	((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
5834c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
5835c42a7b7eSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
5836c42a7b7eSSam Leffler 	struct ifreq *ifr = (struct ifreq *)data;
5837c42a7b7eSSam Leffler 	int error = 0;
5838c42a7b7eSSam Leffler 
5839c42a7b7eSSam Leffler 	ATH_LOCK(sc);
5840c42a7b7eSSam Leffler 	switch (cmd) {
5841c42a7b7eSSam Leffler 	case SIOCSIFFLAGS:
5842c42a7b7eSSam Leffler 		if (IS_RUNNING(ifp)) {
5843c42a7b7eSSam Leffler 			/*
5844c42a7b7eSSam Leffler 			 * To avoid rescanning another access point,
5845c42a7b7eSSam Leffler 			 * do not call ath_init() here.  Instead,
5846c42a7b7eSSam Leffler 			 * only reflect promisc mode settings.
5847c42a7b7eSSam Leffler 			 */
5848c42a7b7eSSam Leffler 			ath_mode_init(sc);
5849c42a7b7eSSam Leffler 		} else if (ifp->if_flags & IFF_UP) {
5850c42a7b7eSSam Leffler 			/*
5851c42a7b7eSSam Leffler 			 * Beware of being called during attach/detach
5852c42a7b7eSSam Leffler 			 * to reset promiscuous mode.  In that case we
5853c42a7b7eSSam Leffler 			 * will still be marked UP but not RUNNING.
5854c42a7b7eSSam Leffler 			 * However trying to re-init the interface
5855c42a7b7eSSam Leffler 			 * is the wrong thing to do as we've already
5856c42a7b7eSSam Leffler 			 * torn down much of our state.  There's
5857c42a7b7eSSam Leffler 			 * probably a better way to deal with this.
5858c42a7b7eSSam Leffler 			 */
5859c42a7b7eSSam Leffler 			if (!sc->sc_invalid && ic->ic_bss != NULL)
5860fc74a9f9SBrooks Davis 				ath_init(sc);	/* XXX lose error */
5861c42a7b7eSSam Leffler 		} else
5862c42a7b7eSSam Leffler 			ath_stop_locked(ifp);
5863c42a7b7eSSam Leffler 		break;
5864c42a7b7eSSam Leffler 	case SIOCADDMULTI:
5865c42a7b7eSSam Leffler 	case SIOCDELMULTI:
5866c42a7b7eSSam Leffler 		/*
5867c42a7b7eSSam Leffler 		 * The upper layer has already installed/removed
5868c42a7b7eSSam Leffler 		 * the multicast address(es), just recalculate the
5869c42a7b7eSSam Leffler 		 * multicast filter for the card.
5870c42a7b7eSSam Leffler 		 */
587113f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
5872c42a7b7eSSam Leffler 			ath_mode_init(sc);
5873c42a7b7eSSam Leffler 		break;
5874c42a7b7eSSam Leffler 	case SIOCGATHSTATS:
5875c42a7b7eSSam Leffler 		/* NB: embed these numbers to get a consistent view */
5876c42a7b7eSSam Leffler 		sc->sc_stats.ast_tx_packets = ifp->if_opackets;
5877c42a7b7eSSam Leffler 		sc->sc_stats.ast_rx_packets = ifp->if_ipackets;
587868e8e04eSSam Leffler 		ieee80211_getsignal(ic, &sc->sc_stats.ast_rx_rssi,
587968e8e04eSSam Leffler 			&sc->sc_stats.ast_rx_noise);
58806bf62dd1SSam Leffler 		sc->sc_stats.ast_tx_rate = sc->sc_hwmap[sc->sc_txrate].ieeerate;
5881c42a7b7eSSam Leffler 		ATH_UNLOCK(sc);
5882c42a7b7eSSam Leffler 		/*
5883c42a7b7eSSam Leffler 		 * NB: Drop the softc lock in case of a page fault;
5884c42a7b7eSSam Leffler 		 * we'll accept any potential inconsisentcy in the
5885c42a7b7eSSam Leffler 		 * statistics.  The alternative is to copy the data
5886c42a7b7eSSam Leffler 		 * to a local structure.
5887c42a7b7eSSam Leffler 		 */
5888c42a7b7eSSam Leffler 		return copyout(&sc->sc_stats,
5889c42a7b7eSSam Leffler 				ifr->ifr_data, sizeof (sc->sc_stats));
5890a585a9a1SSam Leffler #ifdef ATH_DIAGAPI
5891c42a7b7eSSam Leffler 	case SIOCGATHDIAG:
58921d89d44fSSam Leffler 		ATH_UNLOCK(sc);
5893c42a7b7eSSam Leffler 		error = ath_ioctl_diag(sc, (struct ath_diag *) ifr);
58941d89d44fSSam Leffler 		ATH_LOCK(sc);
5895c42a7b7eSSam Leffler 		break;
5896a585a9a1SSam Leffler #endif
5897c42a7b7eSSam Leffler 	default:
5898c42a7b7eSSam Leffler 		error = ieee80211_ioctl(ic, cmd, data);
5899c42a7b7eSSam Leffler 		if (error == ENETRESET) {
5900c42a7b7eSSam Leffler 			if (IS_RUNNING(ifp) &&
5901c42a7b7eSSam Leffler 			    ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
5902fc74a9f9SBrooks Davis 				ath_init(sc);	/* XXX lose error */
5903c42a7b7eSSam Leffler 			error = 0;
5904c42a7b7eSSam Leffler 		}
5905c42a7b7eSSam Leffler 		if (error == ERESTART)
5906c42a7b7eSSam Leffler 			error = IS_RUNNING(ifp) ? ath_reset(ifp) : 0;
5907c42a7b7eSSam Leffler 		break;
5908c42a7b7eSSam Leffler 	}
5909c42a7b7eSSam Leffler 	ATH_UNLOCK(sc);
5910c42a7b7eSSam Leffler 	return error;
5911a614e076SSam Leffler #undef IS_RUNNING
5912c42a7b7eSSam Leffler }
5913c42a7b7eSSam Leffler 
5914c42a7b7eSSam Leffler static int
5915c42a7b7eSSam Leffler ath_sysctl_slottime(SYSCTL_HANDLER_ARGS)
5916c42a7b7eSSam Leffler {
5917c42a7b7eSSam Leffler 	struct ath_softc *sc = arg1;
5918c42a7b7eSSam Leffler 	u_int slottime = ath_hal_getslottime(sc->sc_ah);
5919c42a7b7eSSam Leffler 	int error;
5920c42a7b7eSSam Leffler 
5921c42a7b7eSSam Leffler 	error = sysctl_handle_int(oidp, &slottime, 0, req);
5922c42a7b7eSSam Leffler 	if (error || !req->newptr)
5923c42a7b7eSSam Leffler 		return error;
5924c42a7b7eSSam Leffler 	return !ath_hal_setslottime(sc->sc_ah, slottime) ? EINVAL : 0;
5925c42a7b7eSSam Leffler }
5926c42a7b7eSSam Leffler 
5927c42a7b7eSSam Leffler static int
5928c42a7b7eSSam Leffler ath_sysctl_acktimeout(SYSCTL_HANDLER_ARGS)
5929c42a7b7eSSam Leffler {
5930c42a7b7eSSam Leffler 	struct ath_softc *sc = arg1;
5931c42a7b7eSSam Leffler 	u_int acktimeout = ath_hal_getacktimeout(sc->sc_ah);
5932c42a7b7eSSam Leffler 	int error;
5933c42a7b7eSSam Leffler 
5934c42a7b7eSSam Leffler 	error = sysctl_handle_int(oidp, &acktimeout, 0, req);
5935c42a7b7eSSam Leffler 	if (error || !req->newptr)
5936c42a7b7eSSam Leffler 		return error;
5937c42a7b7eSSam Leffler 	return !ath_hal_setacktimeout(sc->sc_ah, acktimeout) ? EINVAL : 0;
5938c42a7b7eSSam Leffler }
5939c42a7b7eSSam Leffler 
5940c42a7b7eSSam Leffler static int
5941c42a7b7eSSam Leffler ath_sysctl_ctstimeout(SYSCTL_HANDLER_ARGS)
5942c42a7b7eSSam Leffler {
5943c42a7b7eSSam Leffler 	struct ath_softc *sc = arg1;
5944c42a7b7eSSam Leffler 	u_int ctstimeout = ath_hal_getctstimeout(sc->sc_ah);
5945c42a7b7eSSam Leffler 	int error;
5946c42a7b7eSSam Leffler 
5947c42a7b7eSSam Leffler 	error = sysctl_handle_int(oidp, &ctstimeout, 0, req);
5948c42a7b7eSSam Leffler 	if (error || !req->newptr)
5949c42a7b7eSSam Leffler 		return error;
5950c42a7b7eSSam Leffler 	return !ath_hal_setctstimeout(sc->sc_ah, ctstimeout) ? EINVAL : 0;
5951c42a7b7eSSam Leffler }
5952c42a7b7eSSam Leffler 
5953c42a7b7eSSam Leffler static int
5954c42a7b7eSSam Leffler ath_sysctl_softled(SYSCTL_HANDLER_ARGS)
5955c42a7b7eSSam Leffler {
5956c42a7b7eSSam Leffler 	struct ath_softc *sc = arg1;
5957c42a7b7eSSam Leffler 	int softled = sc->sc_softled;
5958c42a7b7eSSam Leffler 	int error;
5959c42a7b7eSSam Leffler 
5960c42a7b7eSSam Leffler 	error = sysctl_handle_int(oidp, &softled, 0, req);
5961c42a7b7eSSam Leffler 	if (error || !req->newptr)
5962c42a7b7eSSam Leffler 		return error;
59633e50ec2cSSam Leffler 	softled = (softled != 0);
5964c42a7b7eSSam Leffler 	if (softled != sc->sc_softled) {
59653e50ec2cSSam Leffler 		if (softled) {
59663e50ec2cSSam Leffler 			/* NB: handle any sc_ledpin change */
5967c42a7b7eSSam Leffler 			ath_hal_gpioCfgOutput(sc->sc_ah, sc->sc_ledpin);
59683e50ec2cSSam Leffler 			ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin,
59693e50ec2cSSam Leffler 				!sc->sc_ledon);
59703e50ec2cSSam Leffler 		}
5971c42a7b7eSSam Leffler 		sc->sc_softled = softled;
5972c42a7b7eSSam Leffler 	}
5973c42a7b7eSSam Leffler 	return 0;
5974c42a7b7eSSam Leffler }
5975c42a7b7eSSam Leffler 
5976c42a7b7eSSam Leffler static int
5977b298baf2SSam Leffler ath_sysctl_ledpin(SYSCTL_HANDLER_ARGS)
5978b298baf2SSam Leffler {
5979b298baf2SSam Leffler 	struct ath_softc *sc = arg1;
5980b298baf2SSam Leffler 	int ledpin = sc->sc_ledpin;
5981b298baf2SSam Leffler 	int error;
5982b298baf2SSam Leffler 
5983b298baf2SSam Leffler 	error = sysctl_handle_int(oidp, &ledpin, 0, req);
5984b298baf2SSam Leffler 	if (error || !req->newptr)
5985b298baf2SSam Leffler 		return error;
5986b298baf2SSam Leffler 	if (ledpin != sc->sc_ledpin) {
5987b298baf2SSam Leffler 		sc->sc_ledpin = ledpin;
5988b298baf2SSam Leffler 		if (sc->sc_softled) {
5989b298baf2SSam Leffler 			ath_hal_gpioCfgOutput(sc->sc_ah, sc->sc_ledpin);
5990b298baf2SSam Leffler 			ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin,
5991b298baf2SSam Leffler 				!sc->sc_ledon);
5992b298baf2SSam Leffler 		}
5993b298baf2SSam Leffler 	}
5994b298baf2SSam Leffler 	return 0;
5995b298baf2SSam Leffler }
5996b298baf2SSam Leffler 
5997b298baf2SSam Leffler static int
59988debcae4SSam Leffler ath_sysctl_txantenna(SYSCTL_HANDLER_ARGS)
59998debcae4SSam Leffler {
60008debcae4SSam Leffler 	struct ath_softc *sc = arg1;
60018debcae4SSam Leffler 	u_int txantenna = ath_hal_getantennaswitch(sc->sc_ah);
60028debcae4SSam Leffler 	int error;
60038debcae4SSam Leffler 
60048debcae4SSam Leffler 	error = sysctl_handle_int(oidp, &txantenna, 0, req);
60058debcae4SSam Leffler 	if (!error && req->newptr) {
60068debcae4SSam Leffler 		/* XXX assumes 2 antenna ports */
60078debcae4SSam Leffler 		if (txantenna < HAL_ANT_VARIABLE || txantenna > HAL_ANT_FIXED_B)
60088debcae4SSam Leffler 			return EINVAL;
60098debcae4SSam Leffler 		ath_hal_setantennaswitch(sc->sc_ah, txantenna);
60108debcae4SSam Leffler 		/*
60118debcae4SSam Leffler 		 * NB: with the switch locked this isn't meaningful,
60128debcae4SSam Leffler 		 *     but set it anyway so things like radiotap get
60138debcae4SSam Leffler 		 *     consistent info in their data.
60148debcae4SSam Leffler 		 */
60158debcae4SSam Leffler 		sc->sc_txantenna = txantenna;
60168debcae4SSam Leffler 	}
60178debcae4SSam Leffler 	return error;
60188debcae4SSam Leffler }
60198debcae4SSam Leffler 
60208debcae4SSam Leffler static int
6021c42a7b7eSSam Leffler ath_sysctl_rxantenna(SYSCTL_HANDLER_ARGS)
6022c42a7b7eSSam Leffler {
6023c42a7b7eSSam Leffler 	struct ath_softc *sc = arg1;
6024c42a7b7eSSam Leffler 	u_int defantenna = ath_hal_getdefantenna(sc->sc_ah);
6025c42a7b7eSSam Leffler 	int error;
6026c42a7b7eSSam Leffler 
6027c42a7b7eSSam Leffler 	error = sysctl_handle_int(oidp, &defantenna, 0, req);
6028c42a7b7eSSam Leffler 	if (!error && req->newptr)
6029c42a7b7eSSam Leffler 		ath_hal_setdefantenna(sc->sc_ah, defantenna);
6030c42a7b7eSSam Leffler 	return error;
6031c42a7b7eSSam Leffler }
6032c42a7b7eSSam Leffler 
6033c42a7b7eSSam Leffler static int
6034c42a7b7eSSam Leffler ath_sysctl_diversity(SYSCTL_HANDLER_ARGS)
6035c42a7b7eSSam Leffler {
6036c42a7b7eSSam Leffler 	struct ath_softc *sc = arg1;
6037c59005e9SSam Leffler 	u_int diversity = ath_hal_getdiversity(sc->sc_ah);
6038c42a7b7eSSam Leffler 	int error;
6039c42a7b7eSSam Leffler 
6040c42a7b7eSSam Leffler 	error = sysctl_handle_int(oidp, &diversity, 0, req);
6041c42a7b7eSSam Leffler 	if (error || !req->newptr)
6042c42a7b7eSSam Leffler 		return error;
6043c59005e9SSam Leffler 	if (!ath_hal_setdiversity(sc->sc_ah, diversity))
6044c59005e9SSam Leffler 		return EINVAL;
6045c42a7b7eSSam Leffler 	sc->sc_diversity = diversity;
6046c59005e9SSam Leffler 	return 0;
6047c42a7b7eSSam Leffler }
6048c42a7b7eSSam Leffler 
6049c42a7b7eSSam Leffler static int
6050c42a7b7eSSam Leffler ath_sysctl_diag(SYSCTL_HANDLER_ARGS)
6051c42a7b7eSSam Leffler {
6052c42a7b7eSSam Leffler 	struct ath_softc *sc = arg1;
6053c42a7b7eSSam Leffler 	u_int32_t diag;
6054c42a7b7eSSam Leffler 	int error;
6055c42a7b7eSSam Leffler 
6056c42a7b7eSSam Leffler 	if (!ath_hal_getdiag(sc->sc_ah, &diag))
6057c42a7b7eSSam Leffler 		return EINVAL;
6058c42a7b7eSSam Leffler 	error = sysctl_handle_int(oidp, &diag, 0, req);
6059c42a7b7eSSam Leffler 	if (error || !req->newptr)
6060c42a7b7eSSam Leffler 		return error;
6061c42a7b7eSSam Leffler 	return !ath_hal_setdiag(sc->sc_ah, diag) ? EINVAL : 0;
6062c42a7b7eSSam Leffler }
6063c42a7b7eSSam Leffler 
6064c42a7b7eSSam Leffler static int
6065c42a7b7eSSam Leffler ath_sysctl_tpscale(SYSCTL_HANDLER_ARGS)
6066c42a7b7eSSam Leffler {
6067c42a7b7eSSam Leffler 	struct ath_softc *sc = arg1;
6068fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
6069c42a7b7eSSam Leffler 	u_int32_t scale;
6070c42a7b7eSSam Leffler 	int error;
6071c42a7b7eSSam Leffler 
6072ee7d6840SSam Leffler 	(void) ath_hal_gettpscale(sc->sc_ah, &scale);
6073c42a7b7eSSam Leffler 	error = sysctl_handle_int(oidp, &scale, 0, req);
6074c42a7b7eSSam Leffler 	if (error || !req->newptr)
6075c42a7b7eSSam Leffler 		return error;
607616d84e01SSam Leffler 	return !ath_hal_settpscale(sc->sc_ah, scale) ? EINVAL :
607716d84e01SSam Leffler 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) ? ath_reset(ifp) : 0;
6078c42a7b7eSSam Leffler }
6079c42a7b7eSSam Leffler 
6080c42a7b7eSSam Leffler static int
6081c42a7b7eSSam Leffler ath_sysctl_tpc(SYSCTL_HANDLER_ARGS)
6082c42a7b7eSSam Leffler {
6083c42a7b7eSSam Leffler 	struct ath_softc *sc = arg1;
6084c42a7b7eSSam Leffler 	u_int tpc = ath_hal_gettpc(sc->sc_ah);
6085c42a7b7eSSam Leffler 	int error;
6086c42a7b7eSSam Leffler 
6087c42a7b7eSSam Leffler 	error = sysctl_handle_int(oidp, &tpc, 0, req);
6088c42a7b7eSSam Leffler 	if (error || !req->newptr)
6089c42a7b7eSSam Leffler 		return error;
6090c42a7b7eSSam Leffler 	return !ath_hal_settpc(sc->sc_ah, tpc) ? EINVAL : 0;
6091c42a7b7eSSam Leffler }
6092c42a7b7eSSam Leffler 
609317f3f177SSam Leffler static int
6094bd5a9920SSam Leffler ath_sysctl_rfkill(SYSCTL_HANDLER_ARGS)
6095bd5a9920SSam Leffler {
6096bd5a9920SSam Leffler 	struct ath_softc *sc = arg1;
609716d84e01SSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
6098bd5a9920SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
6099bd5a9920SSam Leffler 	u_int rfkill = ath_hal_getrfkill(ah);
6100bd5a9920SSam Leffler 	int error;
6101bd5a9920SSam Leffler 
6102bd5a9920SSam Leffler 	error = sysctl_handle_int(oidp, &rfkill, 0, req);
6103bd5a9920SSam Leffler 	if (error || !req->newptr)
6104bd5a9920SSam Leffler 		return error;
6105bd5a9920SSam Leffler 	if (rfkill == ath_hal_getrfkill(ah))	/* unchanged */
6106bd5a9920SSam Leffler 		return 0;
610716d84e01SSam Leffler 	if (!ath_hal_setrfkill(ah, rfkill))
6108bd5a9920SSam Leffler 		return EINVAL;
610916d84e01SSam Leffler 	return (ifp->if_drv_flags & IFF_DRV_RUNNING) ? ath_reset(ifp) : 0;
6110bd5a9920SSam Leffler }
6111bd5a9920SSam Leffler 
6112bd5a9920SSam Leffler static int
6113bd5a9920SSam Leffler ath_sysctl_rfsilent(SYSCTL_HANDLER_ARGS)
6114bd5a9920SSam Leffler {
6115bd5a9920SSam Leffler 	struct ath_softc *sc = arg1;
6116bd5a9920SSam Leffler 	u_int rfsilent;
6117bd5a9920SSam Leffler 	int error;
6118bd5a9920SSam Leffler 
6119ee7d6840SSam Leffler 	(void) ath_hal_getrfsilent(sc->sc_ah, &rfsilent);
6120bd5a9920SSam Leffler 	error = sysctl_handle_int(oidp, &rfsilent, 0, req);
6121bd5a9920SSam Leffler 	if (error || !req->newptr)
6122bd5a9920SSam Leffler 		return error;
6123bd5a9920SSam Leffler 	if (!ath_hal_setrfsilent(sc->sc_ah, rfsilent))
6124bd5a9920SSam Leffler 		return EINVAL;
6125bd5a9920SSam Leffler 	sc->sc_rfsilentpin = rfsilent & 0x1c;
6126bd5a9920SSam Leffler 	sc->sc_rfsilentpol = (rfsilent & 0x2) != 0;
6127bd5a9920SSam Leffler 	return 0;
6128bd5a9920SSam Leffler }
6129bd5a9920SSam Leffler 
6130bd5a9920SSam Leffler static int
6131aaa70f2fSSam Leffler ath_sysctl_countrycode(SYSCTL_HANDLER_ARGS)
6132aaa70f2fSSam Leffler {
6133aaa70f2fSSam Leffler 	struct ath_softc *sc = arg1;
6134aaa70f2fSSam Leffler 	u_int32_t cc = sc->sc_countrycode;
6135aaa70f2fSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
6136aaa70f2fSSam Leffler 	int error;
6137aaa70f2fSSam Leffler 
6138aaa70f2fSSam Leffler 	error = sysctl_handle_int(oidp, &cc, 0, req);
6139aaa70f2fSSam Leffler 	if (error || !req->newptr)
6140aaa70f2fSSam Leffler 		return error;
6141aaa70f2fSSam Leffler 	error = ath_getchannels(sc, sc->sc_regdomain, cc,
614268e8e04eSSam Leffler 			sc->sc_outdoor != 0, sc->sc_xchanmode != 0);
6143aaa70f2fSSam Leffler 	if (error != 0)
6144aaa70f2fSSam Leffler 		return error;
6145aaa70f2fSSam Leffler 	ieee80211_media_init(ic, ath_media_change, ieee80211_media_status);
6146aaa70f2fSSam Leffler 	/* setcurmode? */
6147aaa70f2fSSam Leffler 	return 0;
6148aaa70f2fSSam Leffler }
6149aaa70f2fSSam Leffler 
6150aaa70f2fSSam Leffler static int
615117f3f177SSam Leffler ath_sysctl_regdomain(SYSCTL_HANDLER_ARGS)
615217f3f177SSam Leffler {
615317f3f177SSam Leffler 	struct ath_softc *sc = arg1;
6154aaa70f2fSSam Leffler 	u_int32_t rd = sc->sc_regdomain;
6155aaa70f2fSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
615617f3f177SSam Leffler 	int error;
615717f3f177SSam Leffler 
615817f3f177SSam Leffler 	error = sysctl_handle_int(oidp, &rd, 0, req);
615917f3f177SSam Leffler 	if (error || !req->newptr)
616017f3f177SSam Leffler 		return error;
6161aaa70f2fSSam Leffler 	if (!ath_hal_setregdomain(sc->sc_ah, rd))
6162aaa70f2fSSam Leffler 		return EINVAL;
6163aaa70f2fSSam Leffler 	error = ath_getchannels(sc, rd, sc->sc_countrycode,
616468e8e04eSSam Leffler 			sc->sc_outdoor != 0, sc->sc_xchanmode != 0);
6165aaa70f2fSSam Leffler 	if (error != 0)
6166aaa70f2fSSam Leffler 		return error;
6167aaa70f2fSSam Leffler 	ieee80211_media_init(ic, ath_media_change, ieee80211_media_status);
6168aaa70f2fSSam Leffler 	/* setcurmode? */
6169aaa70f2fSSam Leffler 	return 0;
617017f3f177SSam Leffler }
617117f3f177SSam Leffler 
6172bd5a9920SSam Leffler static int
6173bd5a9920SSam Leffler ath_sysctl_tpack(SYSCTL_HANDLER_ARGS)
6174bd5a9920SSam Leffler {
6175bd5a9920SSam Leffler 	struct ath_softc *sc = arg1;
6176bd5a9920SSam Leffler 	u_int32_t tpack;
6177bd5a9920SSam Leffler 	int error;
6178bd5a9920SSam Leffler 
6179ee7d6840SSam Leffler 	(void) ath_hal_gettpack(sc->sc_ah, &tpack);
6180bd5a9920SSam Leffler 	error = sysctl_handle_int(oidp, &tpack, 0, req);
6181bd5a9920SSam Leffler 	if (error || !req->newptr)
6182bd5a9920SSam Leffler 		return error;
6183bd5a9920SSam Leffler 	return !ath_hal_settpack(sc->sc_ah, tpack) ? EINVAL : 0;
6184bd5a9920SSam Leffler }
6185bd5a9920SSam Leffler 
6186bd5a9920SSam Leffler static int
6187bd5a9920SSam Leffler ath_sysctl_tpcts(SYSCTL_HANDLER_ARGS)
6188bd5a9920SSam Leffler {
6189bd5a9920SSam Leffler 	struct ath_softc *sc = arg1;
6190bd5a9920SSam Leffler 	u_int32_t tpcts;
6191bd5a9920SSam Leffler 	int error;
6192bd5a9920SSam Leffler 
6193ee7d6840SSam Leffler 	(void) ath_hal_gettpcts(sc->sc_ah, &tpcts);
6194bd5a9920SSam Leffler 	error = sysctl_handle_int(oidp, &tpcts, 0, req);
6195bd5a9920SSam Leffler 	if (error || !req->newptr)
6196bd5a9920SSam Leffler 		return error;
6197bd5a9920SSam Leffler 	return !ath_hal_settpcts(sc->sc_ah, tpcts) ? EINVAL : 0;
6198bd5a9920SSam Leffler }
6199bd5a9920SSam Leffler 
6200c42a7b7eSSam Leffler static void
6201c42a7b7eSSam Leffler ath_sysctlattach(struct ath_softc *sc)
6202c42a7b7eSSam Leffler {
6203c42a7b7eSSam Leffler 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
6204c42a7b7eSSam Leffler 	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
6205c59005e9SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
6206c42a7b7eSSam Leffler 
6207aaa70f2fSSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
6208aaa70f2fSSam Leffler 		"countrycode", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
6209aaa70f2fSSam Leffler 		ath_sysctl_countrycode, "I", "country code");
621017f3f177SSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
621117f3f177SSam Leffler 		"regdomain", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
621217f3f177SSam Leffler 		ath_sysctl_regdomain, "I", "EEPROM regdomain code");
6213a585a9a1SSam Leffler #ifdef	ATH_DEBUG
6214c42a7b7eSSam Leffler 	sc->sc_debug = ath_debug;
6215c42a7b7eSSam Leffler 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
6216c42a7b7eSSam Leffler 		"debug", CTLFLAG_RW, &sc->sc_debug, 0,
6217c42a7b7eSSam Leffler 		"control debugging printfs");
6218d2f6ed15SSam Leffler #endif
6219c42a7b7eSSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
6220c42a7b7eSSam Leffler 		"slottime", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
6221c42a7b7eSSam Leffler 		ath_sysctl_slottime, "I", "802.11 slot time (us)");
6222c42a7b7eSSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
6223c42a7b7eSSam Leffler 		"acktimeout", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
6224c42a7b7eSSam Leffler 		ath_sysctl_acktimeout, "I", "802.11 ACK timeout (us)");
6225c42a7b7eSSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
6226c42a7b7eSSam Leffler 		"ctstimeout", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
6227c42a7b7eSSam Leffler 		ath_sysctl_ctstimeout, "I", "802.11 CTS timeout (us)");
6228c42a7b7eSSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
6229c42a7b7eSSam Leffler 		"softled", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
6230c42a7b7eSSam Leffler 		ath_sysctl_softled, "I", "enable/disable software LED support");
6231b298baf2SSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
6232b298baf2SSam Leffler 		"ledpin", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
6233b298baf2SSam Leffler 		ath_sysctl_ledpin, "I", "GPIO pin connected to LED");
6234c42a7b7eSSam Leffler 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
62353e50ec2cSSam Leffler 		"ledon", CTLFLAG_RW, &sc->sc_ledon, 0,
62363e50ec2cSSam Leffler 		"setting to turn LED on");
62373e50ec2cSSam Leffler 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
62383e50ec2cSSam Leffler 		"ledidle", CTLFLAG_RW, &sc->sc_ledidle, 0,
62393e50ec2cSSam Leffler 		"idle time for inactivity LED (ticks)");
62408debcae4SSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
62418debcae4SSam Leffler 		"txantenna", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
62428debcae4SSam Leffler 		ath_sysctl_txantenna, "I", "antenna switch");
6243c42a7b7eSSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
6244c42a7b7eSSam Leffler 		"rxantenna", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
6245c42a7b7eSSam Leffler 		ath_sysctl_rxantenna, "I", "default/rx antenna");
6246c59005e9SSam Leffler 	if (ath_hal_hasdiversity(ah))
6247c42a7b7eSSam Leffler 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
6248c42a7b7eSSam Leffler 			"diversity", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
6249c42a7b7eSSam Leffler 			ath_sysctl_diversity, "I", "antenna diversity");
6250c42a7b7eSSam Leffler 	sc->sc_txintrperiod = ATH_TXINTR_PERIOD;
6251c42a7b7eSSam Leffler 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
6252c42a7b7eSSam Leffler 		"txintrperiod", CTLFLAG_RW, &sc->sc_txintrperiod, 0,
6253c42a7b7eSSam Leffler 		"tx descriptor batching");
6254c42a7b7eSSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
6255c42a7b7eSSam Leffler 		"diag", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
6256c42a7b7eSSam Leffler 		ath_sysctl_diag, "I", "h/w diagnostic control");
6257c42a7b7eSSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
6258c42a7b7eSSam Leffler 		"tpscale", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
6259c42a7b7eSSam Leffler 		ath_sysctl_tpscale, "I", "tx power scaling");
6260bd5a9920SSam Leffler 	if (ath_hal_hastpc(ah)) {
6261c42a7b7eSSam Leffler 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
6262c42a7b7eSSam Leffler 			"tpc", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
6263c42a7b7eSSam Leffler 			ath_sysctl_tpc, "I", "enable/disable per-packet TPC");
6264bd5a9920SSam Leffler 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
6265bd5a9920SSam Leffler 			"tpack", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
6266bd5a9920SSam Leffler 			ath_sysctl_tpack, "I", "tx power for ack frames");
6267bd5a9920SSam Leffler 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
6268bd5a9920SSam Leffler 			"tpcts", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
6269bd5a9920SSam Leffler 			ath_sysctl_tpcts, "I", "tx power for cts frames");
6270bd5a9920SSam Leffler 	}
627168e8e04eSSam Leffler 	if (ath_hal_hasfastframes(sc->sc_ah)) {
627268e8e04eSSam Leffler 		sc->sc_fftxqmin = ATH_FF_TXQMIN;
627368e8e04eSSam Leffler 		SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
627468e8e04eSSam Leffler 			"fftxqmin", CTLFLAG_RW, &sc->sc_fftxqmin, 0,
627568e8e04eSSam Leffler 			"min frames before fast-frame staging");
627668e8e04eSSam Leffler 		sc->sc_fftxqmax = ATH_FF_TXQMAX;
627768e8e04eSSam Leffler 		SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
627868e8e04eSSam Leffler 			"fftxqmax", CTLFLAG_RW, &sc->sc_fftxqmax, 0,
627968e8e04eSSam Leffler 			"max queued frames before tail drop");
628068e8e04eSSam Leffler 	}
6281bd5a9920SSam Leffler 	if (ath_hal_hasrfsilent(ah)) {
6282bd5a9920SSam Leffler 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
6283bd5a9920SSam Leffler 			"rfsilent", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
6284bd5a9920SSam Leffler 			ath_sysctl_rfsilent, "I", "h/w RF silent config");
6285bd5a9920SSam Leffler 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
6286bd5a9920SSam Leffler 			"rfkill", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
6287bd5a9920SSam Leffler 			ath_sysctl_rfkill, "I", "enable/disable RF kill switch");
6288bd5a9920SSam Leffler 	}
62897b0c77ecSSam Leffler 	sc->sc_monpass = HAL_RXERR_DECRYPT | HAL_RXERR_MIC;
62907b0c77ecSSam Leffler 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
62917b0c77ecSSam Leffler 		"monpass", CTLFLAG_RW, &sc->sc_monpass, 0,
62927b0c77ecSSam Leffler 		"mask of error frames to pass when monitoring");
6293c42a7b7eSSam Leffler }
6294c42a7b7eSSam Leffler 
6295c42a7b7eSSam Leffler static void
6296c42a7b7eSSam Leffler ath_bpfattach(struct ath_softc *sc)
6297c42a7b7eSSam Leffler {
6298fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
6299c42a7b7eSSam Leffler 
6300c42a7b7eSSam Leffler 	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
6301c42a7b7eSSam Leffler 		sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th),
6302c42a7b7eSSam Leffler 		&sc->sc_drvbpf);
6303c42a7b7eSSam Leffler 	/*
6304c42a7b7eSSam Leffler 	 * Initialize constant fields.
6305c42a7b7eSSam Leffler 	 * XXX make header lengths a multiple of 32-bits so subsequent
6306c42a7b7eSSam Leffler 	 *     headers are properly aligned; this is a kludge to keep
6307c42a7b7eSSam Leffler 	 *     certain applications happy.
6308c42a7b7eSSam Leffler 	 *
6309c42a7b7eSSam Leffler 	 * NB: the channel is setup each time we transition to the
6310c42a7b7eSSam Leffler 	 *     RUN state to avoid filling it in for each frame.
6311c42a7b7eSSam Leffler 	 */
6312c42a7b7eSSam Leffler 	sc->sc_tx_th_len = roundup(sizeof(sc->sc_tx_th), sizeof(u_int32_t));
6313c42a7b7eSSam Leffler 	sc->sc_tx_th.wt_ihdr.it_len = htole16(sc->sc_tx_th_len);
6314c42a7b7eSSam Leffler 	sc->sc_tx_th.wt_ihdr.it_present = htole32(ATH_TX_RADIOTAP_PRESENT);
6315c42a7b7eSSam Leffler 
6316d3be6f5bSSam Leffler 	sc->sc_rx_th_len = roundup(sizeof(sc->sc_rx_th), sizeof(u_int32_t));
6317d3be6f5bSSam Leffler 	sc->sc_rx_th.wr_ihdr.it_len = htole16(sc->sc_rx_th_len);
6318c42a7b7eSSam Leffler 	sc->sc_rx_th.wr_ihdr.it_present = htole32(ATH_RX_RADIOTAP_PRESENT);
6319c42a7b7eSSam Leffler }
6320c42a7b7eSSam Leffler 
6321664443d0SSam Leffler static int
6322664443d0SSam Leffler ath_tx_raw_start(struct ath_softc *sc, struct ieee80211_node *ni,
6323664443d0SSam Leffler 	struct ath_buf *bf, struct mbuf *m0,
6324664443d0SSam Leffler 	const struct ieee80211_bpf_params *params)
6325664443d0SSam Leffler {
6326664443d0SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
6327664443d0SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
6328664443d0SSam Leffler 	int error, ismcast, ismrr;
6329664443d0SSam Leffler 	int hdrlen, pktlen, try0, txantenna;
6330664443d0SSam Leffler 	u_int8_t rix, cix, txrate, ctsrate, rate1, rate2, rate3;
6331664443d0SSam Leffler 	struct ath_txq *txq;
6332664443d0SSam Leffler 	struct ieee80211_frame *wh;
6333664443d0SSam Leffler 	u_int flags, ctsduration;
6334664443d0SSam Leffler 	HAL_PKT_TYPE atype;
6335664443d0SSam Leffler 	const HAL_RATE_TABLE *rt;
6336664443d0SSam Leffler 	struct ath_desc *ds;
6337664443d0SSam Leffler 	u_int pri;
6338664443d0SSam Leffler 
6339664443d0SSam Leffler 	wh = mtod(m0, struct ieee80211_frame *);
6340664443d0SSam Leffler 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
6341664443d0SSam Leffler 	hdrlen = ieee80211_anyhdrsize(wh);
6342664443d0SSam Leffler 	/*
6343664443d0SSam Leffler 	 * Packet length must not include any
6344664443d0SSam Leffler 	 * pad bytes; deduct them here.
6345664443d0SSam Leffler 	 */
6346664443d0SSam Leffler 	/* XXX honor IEEE80211_BPF_DATAPAD */
6347664443d0SSam Leffler 	pktlen = m0->m_pkthdr.len - (hdrlen & 3) + IEEE80211_CRC_LEN;
6348664443d0SSam Leffler 
6349664443d0SSam Leffler 	error = ath_tx_dmasetup(sc, bf, m0);
6350664443d0SSam Leffler 	if (error != 0)
6351664443d0SSam Leffler 		return error;
6352664443d0SSam Leffler 	m0 = bf->bf_m;				/* NB: may have changed */
6353664443d0SSam Leffler 	wh = mtod(m0, struct ieee80211_frame *);
6354664443d0SSam Leffler 	bf->bf_node = ni;			/* NB: held reference */
6355664443d0SSam Leffler 
6356664443d0SSam Leffler 	flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for crypto errs */
6357664443d0SSam Leffler 	flags |= HAL_TXDESC_INTREQ;		/* force interrupt */
6358664443d0SSam Leffler 	if (params->ibp_flags & IEEE80211_BPF_RTS)
6359664443d0SSam Leffler 		flags |= HAL_TXDESC_RTSENA;
6360664443d0SSam Leffler 	else if (params->ibp_flags & IEEE80211_BPF_CTS)
6361664443d0SSam Leffler 		flags |= HAL_TXDESC_CTSENA;
6362664443d0SSam Leffler 	/* XXX leave ismcast to injector? */
6363664443d0SSam Leffler 	if ((params->ibp_flags & IEEE80211_BPF_NOACK) || ismcast)
6364664443d0SSam Leffler 		flags |= HAL_TXDESC_NOACK;
6365664443d0SSam Leffler 
6366664443d0SSam Leffler 	rt = sc->sc_currates;
6367664443d0SSam Leffler 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
6368664443d0SSam Leffler 	rix = ath_tx_findrix(rt, params->ibp_rate0);
6369664443d0SSam Leffler 	txrate = rt->info[rix].rateCode;
6370664443d0SSam Leffler 	if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
6371664443d0SSam Leffler 		txrate |= rt->info[rix].shortPreamble;
63726bf62dd1SSam Leffler 	sc->sc_txrate = txrate;
6373664443d0SSam Leffler 	try0 = params->ibp_try0;
6374664443d0SSam Leffler 	ismrr = (params->ibp_try1 != 0);
6375664443d0SSam Leffler 	txantenna = params->ibp_pri >> 2;
6376664443d0SSam Leffler 	if (txantenna == 0)			/* XXX? */
6377664443d0SSam Leffler 		txantenna = sc->sc_txantenna;
6378664443d0SSam Leffler 	ctsduration = 0;
6379664443d0SSam Leffler 	if (flags & (HAL_TXDESC_CTSENA | HAL_TXDESC_RTSENA)) {
6380664443d0SSam Leffler 		cix = ath_tx_findrix(rt, params->ibp_ctsrate);
6381664443d0SSam Leffler 		ctsrate = rt->info[cix].rateCode;
6382664443d0SSam Leffler 		if (params->ibp_flags & IEEE80211_BPF_SHORTPRE) {
6383664443d0SSam Leffler 			ctsrate |= rt->info[cix].shortPreamble;
6384664443d0SSam Leffler 			if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
6385664443d0SSam Leffler 				ctsduration += rt->info[cix].spAckDuration;
6386664443d0SSam Leffler 			ctsduration += ath_hal_computetxtime(ah,
6387664443d0SSam Leffler 				rt, pktlen, rix, AH_TRUE);
6388664443d0SSam Leffler 			if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
6389664443d0SSam Leffler 				ctsduration += rt->info[rix].spAckDuration;
6390664443d0SSam Leffler 		} else {
6391664443d0SSam Leffler 			if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
6392664443d0SSam Leffler 				ctsduration += rt->info[cix].lpAckDuration;
6393664443d0SSam Leffler 			ctsduration += ath_hal_computetxtime(ah,
6394664443d0SSam Leffler 				rt, pktlen, rix, AH_FALSE);
6395664443d0SSam Leffler 			if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
6396664443d0SSam Leffler 				ctsduration += rt->info[rix].lpAckDuration;
6397664443d0SSam Leffler 		}
6398664443d0SSam Leffler 		ismrr = 0;			/* XXX */
6399664443d0SSam Leffler 	} else
6400664443d0SSam Leffler 		ctsrate = 0;
6401664443d0SSam Leffler 	pri = params->ibp_pri & 3;
6402664443d0SSam Leffler 	/*
6403664443d0SSam Leffler 	 * NB: we mark all packets as type PSPOLL so the h/w won't
6404664443d0SSam Leffler 	 * set the sequence number, duration, etc.
6405664443d0SSam Leffler 	 */
6406664443d0SSam Leffler 	atype = HAL_PKT_TYPE_PSPOLL;
6407664443d0SSam Leffler 
6408664443d0SSam Leffler 	if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
640968e8e04eSSam Leffler 		ieee80211_dump_pkt(ic, mtod(m0, caddr_t), m0->m_len,
6410664443d0SSam Leffler 			sc->sc_hwmap[txrate].ieeerate, -1);
6411664443d0SSam Leffler 
6412664443d0SSam Leffler 	if (bpf_peers_present(ic->ic_rawbpf))
6413664443d0SSam Leffler 		bpf_mtap(ic->ic_rawbpf, m0);
6414664443d0SSam Leffler 	if (bpf_peers_present(sc->sc_drvbpf)) {
6415664443d0SSam Leffler 		u_int64_t tsf = ath_hal_gettsf64(ah);
6416664443d0SSam Leffler 
6417664443d0SSam Leffler 		sc->sc_tx_th.wt_tsf = htole64(tsf);
6418664443d0SSam Leffler 		sc->sc_tx_th.wt_flags = sc->sc_hwmap[txrate].txflags;
6419664443d0SSam Leffler 		if (wh->i_fc[1] & IEEE80211_FC1_WEP)
6420664443d0SSam Leffler 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
6421664443d0SSam Leffler 		sc->sc_tx_th.wt_rate = sc->sc_hwmap[txrate].ieeerate;
6422664443d0SSam Leffler 		sc->sc_tx_th.wt_txpower = ni->ni_txpower;
6423664443d0SSam Leffler 		sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
6424664443d0SSam Leffler 
6425664443d0SSam Leffler 		bpf_mtap2(sc->sc_drvbpf,
6426664443d0SSam Leffler 			&sc->sc_tx_th, sc->sc_tx_th_len, m0);
6427664443d0SSam Leffler 	}
6428664443d0SSam Leffler 
6429664443d0SSam Leffler 	/*
6430664443d0SSam Leffler 	 * Formulate first tx descriptor with tx controls.
6431664443d0SSam Leffler 	 */
6432664443d0SSam Leffler 	ds = bf->bf_desc;
6433664443d0SSam Leffler 	/* XXX check return value? */
6434664443d0SSam Leffler 	ath_hal_setuptxdesc(ah, ds
6435664443d0SSam Leffler 		, pktlen		/* packet length */
6436664443d0SSam Leffler 		, hdrlen		/* header length */
6437664443d0SSam Leffler 		, atype			/* Atheros packet type */
6438664443d0SSam Leffler 		, params->ibp_power	/* txpower */
6439664443d0SSam Leffler 		, txrate, try0		/* series 0 rate/tries */
6440664443d0SSam Leffler 		, HAL_TXKEYIX_INVALID	/* key cache index */
6441664443d0SSam Leffler 		, txantenna		/* antenna mode */
6442664443d0SSam Leffler 		, flags			/* flags */
6443664443d0SSam Leffler 		, ctsrate		/* rts/cts rate */
6444664443d0SSam Leffler 		, ctsduration		/* rts/cts duration */
6445664443d0SSam Leffler 	);
6446664443d0SSam Leffler 	bf->bf_flags = flags;
6447664443d0SSam Leffler 
6448664443d0SSam Leffler 	if (ismrr) {
6449664443d0SSam Leffler 		rix = ath_tx_findrix(rt, params->ibp_rate1);
6450664443d0SSam Leffler 		rate1 = rt->info[rix].rateCode;
6451664443d0SSam Leffler 		if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
6452664443d0SSam Leffler 			rate1 |= rt->info[rix].shortPreamble;
6453664443d0SSam Leffler 		if (params->ibp_try2) {
6454664443d0SSam Leffler 			rix = ath_tx_findrix(rt, params->ibp_rate2);
6455664443d0SSam Leffler 			rate2 = rt->info[rix].rateCode;
6456664443d0SSam Leffler 			if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
6457664443d0SSam Leffler 				rate2 |= rt->info[rix].shortPreamble;
6458664443d0SSam Leffler 		} else
6459664443d0SSam Leffler 			rate2 = 0;
6460664443d0SSam Leffler 		if (params->ibp_try3) {
6461664443d0SSam Leffler 			rix = ath_tx_findrix(rt, params->ibp_rate3);
6462664443d0SSam Leffler 			rate3 = rt->info[rix].rateCode;
6463664443d0SSam Leffler 			if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
6464664443d0SSam Leffler 				rate3 |= rt->info[rix].shortPreamble;
6465664443d0SSam Leffler 		} else
6466664443d0SSam Leffler 			rate3 = 0;
6467664443d0SSam Leffler 		ath_hal_setupxtxdesc(ah, ds
6468664443d0SSam Leffler 			, rate1, params->ibp_try1	/* series 1 */
6469664443d0SSam Leffler 			, rate2, params->ibp_try2	/* series 2 */
6470664443d0SSam Leffler 			, rate3, params->ibp_try3	/* series 3 */
6471664443d0SSam Leffler 		);
6472664443d0SSam Leffler 	}
6473664443d0SSam Leffler 
6474664443d0SSam Leffler 	/*
6475664443d0SSam Leffler 	 * When servicing one or more stations in power-save mode
6476664443d0SSam Leffler 	 * (or) if there is some mcast data waiting on the mcast
6477664443d0SSam Leffler 	 * queue (to prevent out of order delivery) multicast
6478664443d0SSam Leffler 	 * frames must be buffered until after the beacon.
6479664443d0SSam Leffler 	 */
6480664443d0SSam Leffler 	txq = sc->sc_ac2q[pri];
6481664443d0SSam Leffler 	if (ismcast && (ic->ic_ps_sta || sc->sc_mcastq.axq_depth))
6482664443d0SSam Leffler 		txq = &sc->sc_mcastq;
6483664443d0SSam Leffler 	ath_tx_handoff(sc, txq, bf);
6484664443d0SSam Leffler 	return 0;
6485664443d0SSam Leffler }
6486664443d0SSam Leffler 
6487664443d0SSam Leffler static int
6488664443d0SSam Leffler ath_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
6489664443d0SSam Leffler 	const struct ieee80211_bpf_params *params)
6490664443d0SSam Leffler {
6491664443d0SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
6492664443d0SSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
6493664443d0SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
6494664443d0SSam Leffler 	struct ath_buf *bf;
6495664443d0SSam Leffler 
6496664443d0SSam Leffler 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->sc_invalid) {
6497b03cfe23SSepherosa Ziehau 		ieee80211_free_node(ni);
6498664443d0SSam Leffler 		m_freem(m);
6499664443d0SSam Leffler 		return ENETDOWN;
6500664443d0SSam Leffler 	}
6501664443d0SSam Leffler 	/*
6502664443d0SSam Leffler 	 * Grab a TX buffer and associated resources.
6503664443d0SSam Leffler 	 */
6504664443d0SSam Leffler 	ATH_TXBUF_LOCK(sc);
6505664443d0SSam Leffler 	bf = STAILQ_FIRST(&sc->sc_txbuf);
6506664443d0SSam Leffler 	if (bf != NULL)
6507664443d0SSam Leffler 		STAILQ_REMOVE_HEAD(&sc->sc_txbuf, bf_list);
6508664443d0SSam Leffler 	ATH_TXBUF_UNLOCK(sc);
6509664443d0SSam Leffler 	if (bf == NULL) {
6510664443d0SSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: out of xmit buffers\n",
6511664443d0SSam Leffler 			__func__);
6512664443d0SSam Leffler 		sc->sc_stats.ast_tx_qstop++;
6513664443d0SSam Leffler 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
6514b03cfe23SSepherosa Ziehau 		ieee80211_free_node(ni);
6515664443d0SSam Leffler 		m_freem(m);
6516664443d0SSam Leffler 		return ENOBUFS;
6517664443d0SSam Leffler 	}
6518664443d0SSam Leffler 
6519664443d0SSam Leffler 	ifp->if_opackets++;
6520664443d0SSam Leffler 	sc->sc_stats.ast_tx_raw++;
6521664443d0SSam Leffler 
6522664443d0SSam Leffler 	if (params == NULL) {
6523664443d0SSam Leffler 		/*
6524664443d0SSam Leffler 		 * Legacy path; interpret frame contents to decide
6525664443d0SSam Leffler 		 * precisely how to send the frame.
6526664443d0SSam Leffler 		 */
6527664443d0SSam Leffler 		if (ath_tx_start(sc, ni, bf, m))
6528664443d0SSam Leffler 			goto bad;
6529664443d0SSam Leffler 	} else {
6530664443d0SSam Leffler 		/*
6531664443d0SSam Leffler 		 * Caller supplied explicit parameters to use in
6532664443d0SSam Leffler 		 * sending the frame.
6533664443d0SSam Leffler 		 */
6534664443d0SSam Leffler 		if (ath_tx_raw_start(sc, ni, bf, m, params))
6535664443d0SSam Leffler 			goto bad;
6536664443d0SSam Leffler 	}
653768e8e04eSSam Leffler 	ifp->if_timer = 5;
6538664443d0SSam Leffler 
6539664443d0SSam Leffler 	return 0;
6540664443d0SSam Leffler bad:
6541664443d0SSam Leffler 	ifp->if_oerrors++;
6542664443d0SSam Leffler 	ATH_TXBUF_LOCK(sc);
6543664443d0SSam Leffler 	STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
6544664443d0SSam Leffler 	ATH_TXBUF_UNLOCK(sc);
6545664443d0SSam Leffler 	ieee80211_free_node(ni);
6546664443d0SSam Leffler 	return EIO;		/* XXX */
6547664443d0SSam Leffler }
6548664443d0SSam Leffler 
6549c42a7b7eSSam Leffler /*
6550c42a7b7eSSam Leffler  * Announce various information on device/driver attach.
6551c42a7b7eSSam Leffler  */
6552c42a7b7eSSam Leffler static void
6553c42a7b7eSSam Leffler ath_announce(struct ath_softc *sc)
6554c42a7b7eSSam Leffler {
6555c42a7b7eSSam Leffler #define	HAL_MODE_DUALBAND	(HAL_MODE_11A|HAL_MODE_11B)
6556fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
6557c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
6558c42a7b7eSSam Leffler 	u_int modes, cc;
6559c42a7b7eSSam Leffler 
6560c42a7b7eSSam Leffler 	if_printf(ifp, "mac %d.%d phy %d.%d",
6561c42a7b7eSSam Leffler 		ah->ah_macVersion, ah->ah_macRev,
6562c42a7b7eSSam Leffler 		ah->ah_phyRev >> 4, ah->ah_phyRev & 0xf);
6563c42a7b7eSSam Leffler 	/*
6564c42a7b7eSSam Leffler 	 * Print radio revision(s).  We check the wireless modes
6565c42a7b7eSSam Leffler 	 * to avoid falsely printing revs for inoperable parts.
6566c42a7b7eSSam Leffler 	 * Dual-band radio revs are returned in the 5Ghz rev number.
6567c42a7b7eSSam Leffler 	 */
6568c42a7b7eSSam Leffler 	ath_hal_getcountrycode(ah, &cc);
6569c42a7b7eSSam Leffler 	modes = ath_hal_getwirelessmodes(ah, cc);
6570c42a7b7eSSam Leffler 	if ((modes & HAL_MODE_DUALBAND) == HAL_MODE_DUALBAND) {
6571c42a7b7eSSam Leffler 		if (ah->ah_analog5GhzRev && ah->ah_analog2GhzRev)
6572c42a7b7eSSam Leffler 			printf(" 5ghz radio %d.%d 2ghz radio %d.%d",
6573c42a7b7eSSam Leffler 				ah->ah_analog5GhzRev >> 4,
6574c42a7b7eSSam Leffler 				ah->ah_analog5GhzRev & 0xf,
6575c42a7b7eSSam Leffler 				ah->ah_analog2GhzRev >> 4,
6576c42a7b7eSSam Leffler 				ah->ah_analog2GhzRev & 0xf);
6577c42a7b7eSSam Leffler 		else
6578c42a7b7eSSam Leffler 			printf(" radio %d.%d", ah->ah_analog5GhzRev >> 4,
6579c42a7b7eSSam Leffler 				ah->ah_analog5GhzRev & 0xf);
6580c42a7b7eSSam Leffler 	} else
6581c42a7b7eSSam Leffler 		printf(" radio %d.%d", ah->ah_analog5GhzRev >> 4,
6582c42a7b7eSSam Leffler 			ah->ah_analog5GhzRev & 0xf);
6583c42a7b7eSSam Leffler 	printf("\n");
6584c42a7b7eSSam Leffler 	if (bootverbose) {
6585c42a7b7eSSam Leffler 		int i;
6586c42a7b7eSSam Leffler 		for (i = 0; i <= WME_AC_VO; i++) {
6587c42a7b7eSSam Leffler 			struct ath_txq *txq = sc->sc_ac2q[i];
6588c42a7b7eSSam Leffler 			if_printf(ifp, "Use hw queue %u for %s traffic\n",
6589c42a7b7eSSam Leffler 				txq->axq_qnum, ieee80211_wme_acnames[i]);
6590c42a7b7eSSam Leffler 		}
6591c42a7b7eSSam Leffler 		if_printf(ifp, "Use hw queue %u for CAB traffic\n",
6592c42a7b7eSSam Leffler 			sc->sc_cabq->axq_qnum);
6593c42a7b7eSSam Leffler 		if_printf(ifp, "Use hw queue %u for beacons\n", sc->sc_bhalq);
6594c42a7b7eSSam Leffler 	}
6595e2d787faSSam Leffler 	if (ath_rxbuf != ATH_RXBUF)
6596e2d787faSSam Leffler 		if_printf(ifp, "using %u rx buffers\n", ath_rxbuf);
6597e2d787faSSam Leffler 	if (ath_txbuf != ATH_TXBUF)
6598e2d787faSSam Leffler 		if_printf(ifp, "using %u tx buffers\n", ath_txbuf);
6599c42a7b7eSSam Leffler #undef HAL_MODE_DUALBAND
6600c42a7b7eSSam Leffler }
6601