xref: /freebsd/sys/dev/ath/if_ath.c (revision 4a0d6638b3bb3b7ed39b852f722e904ac9a6e603)
15591b213SSam Leffler /*-
21f1d7810SSam Leffler  * Copyright (c) 2002-2005 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  * 3. Neither the names of the above-listed copyright holders nor the names
165591b213SSam Leffler  *    of any contributors may be used to endorse or promote products derived
175591b213SSam Leffler  *    from this software without specific prior written permission.
185591b213SSam Leffler  *
195591b213SSam Leffler  * Alternatively, this software may be distributed under the terms of the
205591b213SSam Leffler  * GNU General Public License ("GPL") version 2 as published by the Free
215591b213SSam Leffler  * Software Foundation.
225591b213SSam Leffler  *
235591b213SSam Leffler  * NO WARRANTY
245591b213SSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
255591b213SSam Leffler  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
265591b213SSam Leffler  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
275591b213SSam Leffler  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
285591b213SSam Leffler  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
295591b213SSam Leffler  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
305591b213SSam Leffler  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
315591b213SSam Leffler  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
325591b213SSam Leffler  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
335591b213SSam Leffler  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
345591b213SSam Leffler  * THE POSSIBILITY OF SUCH DAMAGES.
355591b213SSam Leffler  */
365591b213SSam Leffler 
375591b213SSam Leffler #include <sys/cdefs.h>
385591b213SSam Leffler __FBSDID("$FreeBSD$");
395591b213SSam Leffler 
405591b213SSam Leffler /*
415591b213SSam Leffler  * Driver for the Atheros Wireless LAN controller.
425f3721d5SSam Leffler  *
435f3721d5SSam Leffler  * This software is derived from work of Atsushi Onoe; his contribution
445f3721d5SSam Leffler  * is greatly appreciated.
455591b213SSam Leffler  */
465591b213SSam Leffler 
475591b213SSam Leffler #include "opt_inet.h"
485591b213SSam Leffler 
495591b213SSam Leffler #include <sys/param.h>
505591b213SSam Leffler #include <sys/systm.h>
515591b213SSam Leffler #include <sys/sysctl.h>
525591b213SSam Leffler #include <sys/mbuf.h>
535591b213SSam Leffler #include <sys/malloc.h>
545591b213SSam Leffler #include <sys/lock.h>
555591b213SSam Leffler #include <sys/mutex.h>
565591b213SSam Leffler #include <sys/kernel.h>
575591b213SSam Leffler #include <sys/socket.h>
585591b213SSam Leffler #include <sys/sockio.h>
595591b213SSam Leffler #include <sys/errno.h>
605591b213SSam Leffler #include <sys/callout.h>
615591b213SSam Leffler #include <sys/bus.h>
625591b213SSam Leffler #include <sys/endian.h>
635591b213SSam Leffler 
645591b213SSam Leffler #include <machine/bus.h>
655591b213SSam Leffler 
665591b213SSam Leffler #include <net/if.h>
675591b213SSam Leffler #include <net/if_dl.h>
685591b213SSam Leffler #include <net/if_media.h>
69fc74a9f9SBrooks Davis #include <net/if_types.h>
705591b213SSam Leffler #include <net/if_arp.h>
715591b213SSam Leffler #include <net/ethernet.h>
725591b213SSam Leffler #include <net/if_llc.h>
735591b213SSam Leffler 
745591b213SSam Leffler #include <net80211/ieee80211_var.h>
755591b213SSam Leffler 
765591b213SSam Leffler #include <net/bpf.h>
775591b213SSam Leffler 
785591b213SSam Leffler #ifdef INET
795591b213SSam Leffler #include <netinet/in.h>
805591b213SSam Leffler #include <netinet/if_ether.h>
815591b213SSam Leffler #endif
825591b213SSam Leffler 
835591b213SSam Leffler #define	AR_DEBUG
845591b213SSam Leffler #include <dev/ath/if_athvar.h>
855591b213SSam Leffler #include <contrib/dev/ath/ah_desc.h>
86c42a7b7eSSam Leffler #include <contrib/dev/ath/ah_devid.h>		/* XXX for softled */
875591b213SSam Leffler 
88e8fd88a3SSam Leffler /* unaligned little endian access */
895591b213SSam Leffler #define LE_READ_2(p)							\
905591b213SSam Leffler 	((u_int16_t)							\
915591b213SSam Leffler 	 ((((u_int8_t *)(p))[0]      ) | (((u_int8_t *)(p))[1] <<  8)))
925591b213SSam Leffler #define LE_READ_4(p)							\
935591b213SSam Leffler 	((u_int32_t)							\
945591b213SSam Leffler 	 ((((u_int8_t *)(p))[0]      ) | (((u_int8_t *)(p))[1] <<  8) |	\
955591b213SSam Leffler 	  (((u_int8_t *)(p))[2] << 16) | (((u_int8_t *)(p))[3] << 24)))
965591b213SSam Leffler 
973e50ec2cSSam Leffler enum {
983e50ec2cSSam Leffler 	ATH_LED_TX,
993e50ec2cSSam Leffler 	ATH_LED_RX,
1003e50ec2cSSam Leffler 	ATH_LED_POLL,
1013e50ec2cSSam Leffler };
1023e50ec2cSSam Leffler 
1035591b213SSam Leffler static void	ath_init(void *);
104c42a7b7eSSam Leffler static void	ath_stop_locked(struct ifnet *);
1055591b213SSam Leffler static void	ath_stop(struct ifnet *);
1065591b213SSam Leffler static void	ath_start(struct ifnet *);
107c42a7b7eSSam Leffler static int	ath_reset(struct ifnet *);
1085591b213SSam Leffler static int	ath_media_change(struct ifnet *);
1095591b213SSam Leffler static void	ath_watchdog(struct ifnet *);
1105591b213SSam Leffler static int	ath_ioctl(struct ifnet *, u_long, caddr_t);
1115591b213SSam Leffler static void	ath_fatal_proc(void *, int);
1125591b213SSam Leffler static void	ath_rxorn_proc(void *, int);
1135591b213SSam Leffler static void	ath_bmiss_proc(void *, int);
114c42a7b7eSSam Leffler static int	ath_key_alloc(struct ieee80211com *,
115c1225b52SSam Leffler 			const struct ieee80211_key *,
116c1225b52SSam Leffler 			ieee80211_keyix *, ieee80211_keyix *);
117c42a7b7eSSam Leffler static int	ath_key_delete(struct ieee80211com *,
118c42a7b7eSSam Leffler 			const struct ieee80211_key *);
119c42a7b7eSSam Leffler static int	ath_key_set(struct ieee80211com *, const struct ieee80211_key *,
120c42a7b7eSSam Leffler 			const u_int8_t mac[IEEE80211_ADDR_LEN]);
121c42a7b7eSSam Leffler static void	ath_key_update_begin(struct ieee80211com *);
122c42a7b7eSSam Leffler static void	ath_key_update_end(struct ieee80211com *);
1235591b213SSam Leffler static void	ath_mode_init(struct ath_softc *);
124c42a7b7eSSam Leffler static void	ath_setslottime(struct ath_softc *);
125c42a7b7eSSam Leffler static void	ath_updateslot(struct ifnet *);
12680d2765fSSam Leffler static int	ath_beaconq_setup(struct ath_hal *);
1275591b213SSam Leffler static int	ath_beacon_alloc(struct ath_softc *, struct ieee80211_node *);
128c42a7b7eSSam Leffler static void	ath_beacon_setup(struct ath_softc *, struct ath_buf *);
1295591b213SSam Leffler static void	ath_beacon_proc(void *, int);
130c42a7b7eSSam Leffler static void	ath_bstuck_proc(void *, int);
1315591b213SSam Leffler static void	ath_beacon_free(struct ath_softc *);
1325591b213SSam Leffler static void	ath_beacon_config(struct ath_softc *);
133c42a7b7eSSam Leffler static void	ath_descdma_cleanup(struct ath_softc *sc,
134c42a7b7eSSam Leffler 			struct ath_descdma *, ath_bufhead *);
1355591b213SSam Leffler static int	ath_desc_alloc(struct ath_softc *);
1365591b213SSam Leffler static void	ath_desc_free(struct ath_softc *);
137c42a7b7eSSam Leffler static struct ieee80211_node *ath_node_alloc(struct ieee80211_node_table *);
138c42a7b7eSSam Leffler static void	ath_node_free(struct ieee80211_node *);
139c42a7b7eSSam Leffler static u_int8_t	ath_node_getrssi(const struct ieee80211_node *);
1405591b213SSam Leffler static int	ath_rxbuf_init(struct ath_softc *, struct ath_buf *);
141c42a7b7eSSam Leffler static void	ath_recv_mgmt(struct ieee80211com *ic, struct mbuf *m,
142c42a7b7eSSam Leffler 			struct ieee80211_node *ni,
143c42a7b7eSSam Leffler 			int subtype, int rssi, u_int32_t rstamp);
144c42a7b7eSSam Leffler static void	ath_setdefantenna(struct ath_softc *, u_int);
1455591b213SSam Leffler static void	ath_rx_proc(void *, int);
146c42a7b7eSSam Leffler static struct ath_txq *ath_txq_setup(struct ath_softc*, int qtype, int subtype);
147c42a7b7eSSam Leffler static int	ath_tx_setup(struct ath_softc *, int, int);
148c42a7b7eSSam Leffler static int	ath_wme_update(struct ieee80211com *);
149c42a7b7eSSam Leffler static void	ath_tx_cleanupq(struct ath_softc *, struct ath_txq *);
150c42a7b7eSSam Leffler static void	ath_tx_cleanup(struct ath_softc *);
1515591b213SSam Leffler static int	ath_tx_start(struct ath_softc *, struct ieee80211_node *,
1525591b213SSam Leffler 			     struct ath_buf *, struct mbuf *);
153c42a7b7eSSam Leffler static void	ath_tx_proc_q0(void *, int);
154c42a7b7eSSam Leffler static void	ath_tx_proc_q0123(void *, int);
1555591b213SSam Leffler static void	ath_tx_proc(void *, int);
1565591b213SSam Leffler static int	ath_chan_set(struct ath_softc *, struct ieee80211_channel *);
1575591b213SSam Leffler static void	ath_draintxq(struct ath_softc *);
1585591b213SSam Leffler static void	ath_stoprecv(struct ath_softc *);
1595591b213SSam Leffler static int	ath_startrecv(struct ath_softc *);
160c42a7b7eSSam Leffler static void	ath_chan_change(struct ath_softc *, struct ieee80211_channel *);
1615591b213SSam Leffler static void	ath_next_scan(void *);
1625591b213SSam Leffler static void	ath_calibrate(void *);
16345bbf62fSSam Leffler static int	ath_newstate(struct ieee80211com *, enum ieee80211_state, int);
164e8fd88a3SSam Leffler static void	ath_setup_stationkey(struct ieee80211_node *);
165e9962332SSam Leffler static void	ath_newassoc(struct ieee80211_node *, int);
166c42a7b7eSSam Leffler static int	ath_getchannels(struct ath_softc *, u_int cc,
167c42a7b7eSSam Leffler 			HAL_BOOL outdoor, HAL_BOOL xchanmode);
1683e50ec2cSSam Leffler static void	ath_led_event(struct ath_softc *, int);
169c42a7b7eSSam Leffler static void	ath_update_txpow(struct ath_softc *);
1705591b213SSam Leffler 
171c42a7b7eSSam Leffler static int	ath_rate_setup(struct ath_softc *, u_int mode);
1725591b213SSam Leffler static void	ath_setcurmode(struct ath_softc *, enum ieee80211_phymode);
173c42a7b7eSSam Leffler 
174c42a7b7eSSam Leffler static void	ath_sysctlattach(struct ath_softc *);
175c42a7b7eSSam Leffler static void	ath_bpfattach(struct ath_softc *);
176c42a7b7eSSam Leffler static void	ath_announce(struct ath_softc *);
1775591b213SSam Leffler 
1785591b213SSam Leffler SYSCTL_DECL(_hw_ath);
1795591b213SSam Leffler 
1805591b213SSam Leffler /* XXX validate sysctl values */
1815591b213SSam Leffler static	int ath_dwelltime = 200;		/* 5 channels/second */
1825591b213SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, dwell, CTLFLAG_RW, &ath_dwelltime,
1835591b213SSam Leffler 	    0, "channel dwell time (ms) for AP/station scanning");
1845591b213SSam Leffler static	int ath_calinterval = 30;		/* calibrate every 30 secs */
1855591b213SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, calibrate, CTLFLAG_RW, &ath_calinterval,
1865591b213SSam Leffler 	    0, "chip calibration interval (secs)");
18745cabbdcSSam Leffler static	int ath_outdoor = AH_TRUE;		/* outdoor operation */
18845cabbdcSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, outdoor, CTLFLAG_RD, &ath_outdoor,
189c42a7b7eSSam Leffler 	    0, "outdoor operation");
1908c0370b7SSam Leffler TUNABLE_INT("hw.ath.outdoor", &ath_outdoor);
191c42a7b7eSSam Leffler static	int ath_xchanmode = AH_TRUE;		/* extended channel use */
192c42a7b7eSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, xchanmode, CTLFLAG_RD, &ath_xchanmode,
193c42a7b7eSSam Leffler 	    0, "extended channel mode");
194c42a7b7eSSam Leffler TUNABLE_INT("hw.ath.xchanmode", &ath_xchanmode);
19545cabbdcSSam Leffler static	int ath_countrycode = CTRY_DEFAULT;	/* country code */
19645cabbdcSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, countrycode, CTLFLAG_RD, &ath_countrycode,
19745cabbdcSSam Leffler 	    0, "country code");
1988c0370b7SSam Leffler TUNABLE_INT("hw.ath.countrycode", &ath_countrycode);
19945cabbdcSSam Leffler static	int ath_regdomain = 0;			/* regulatory domain */
20045cabbdcSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, regdomain, CTLFLAG_RD, &ath_regdomain,
20145cabbdcSSam Leffler 	    0, "regulatory domain");
2025591b213SSam Leffler 
2035591b213SSam Leffler #ifdef AR_DEBUG
204c42a7b7eSSam Leffler static	int ath_debug = 0;
2055591b213SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, debug, CTLFLAG_RW, &ath_debug,
2065591b213SSam Leffler 	    0, "control debugging printfs");
207f3be7956SSam Leffler TUNABLE_INT("hw.ath.debug", &ath_debug);
208e325e530SSam Leffler enum {
209e325e530SSam Leffler 	ATH_DEBUG_XMIT		= 0x00000001,	/* basic xmit operation */
210e325e530SSam Leffler 	ATH_DEBUG_XMIT_DESC	= 0x00000002,	/* xmit descriptors */
211e325e530SSam Leffler 	ATH_DEBUG_RECV		= 0x00000004,	/* basic recv operation */
212e325e530SSam Leffler 	ATH_DEBUG_RECV_DESC	= 0x00000008,	/* recv descriptors */
213e325e530SSam Leffler 	ATH_DEBUG_RATE		= 0x00000010,	/* rate control */
214e325e530SSam Leffler 	ATH_DEBUG_RESET		= 0x00000020,	/* reset processing */
215e325e530SSam Leffler 	ATH_DEBUG_MODE		= 0x00000040,	/* mode init/setup */
216e325e530SSam Leffler 	ATH_DEBUG_BEACON 	= 0x00000080,	/* beacon handling */
217e325e530SSam Leffler 	ATH_DEBUG_WATCHDOG 	= 0x00000100,	/* watchdog timeout */
218e325e530SSam Leffler 	ATH_DEBUG_INTR		= 0x00001000,	/* ISR */
219e325e530SSam Leffler 	ATH_DEBUG_TX_PROC	= 0x00002000,	/* tx ISR proc */
220e325e530SSam Leffler 	ATH_DEBUG_RX_PROC	= 0x00004000,	/* rx ISR proc */
221e325e530SSam Leffler 	ATH_DEBUG_BEACON_PROC	= 0x00008000,	/* beacon ISR proc */
222e325e530SSam Leffler 	ATH_DEBUG_CALIBRATE	= 0x00010000,	/* periodic calibration */
223c42a7b7eSSam Leffler 	ATH_DEBUG_KEYCACHE	= 0x00020000,	/* key cache management */
224c42a7b7eSSam Leffler 	ATH_DEBUG_STATE		= 0x00040000,	/* 802.11 state transitions */
225c42a7b7eSSam Leffler 	ATH_DEBUG_NODE		= 0x00080000,	/* node management */
2263e50ec2cSSam Leffler 	ATH_DEBUG_LED		= 0x00100000,	/* led management */
227c42a7b7eSSam Leffler 	ATH_DEBUG_FATAL		= 0x80000000,	/* fatal errors */
228e325e530SSam Leffler 	ATH_DEBUG_ANY		= 0xffffffff
229e325e530SSam Leffler };
230c42a7b7eSSam Leffler #define	IFF_DUMPPKTS(sc, m) \
2310a1b94c4SSam Leffler 	((sc->sc_debug & (m)) || \
232fc74a9f9SBrooks Davis 	    (sc->sc_ifp->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
233c42a7b7eSSam Leffler #define	DPRINTF(sc, m, fmt, ...) do {				\
2340a1b94c4SSam Leffler 	if (sc->sc_debug & (m))					\
235c42a7b7eSSam Leffler 		printf(fmt, __VA_ARGS__);			\
236c42a7b7eSSam Leffler } while (0)
237c42a7b7eSSam Leffler #define	KEYPRINTF(sc, ix, hk, mac) do {				\
238c42a7b7eSSam Leffler 	if (sc->sc_debug & ATH_DEBUG_KEYCACHE)			\
239c42a7b7eSSam Leffler 		ath_keyprint(__func__, ix, hk, mac);		\
240c42a7b7eSSam Leffler } while (0)
241c42a7b7eSSam Leffler static	void ath_printrxbuf(struct ath_buf *bf, int);
242c42a7b7eSSam Leffler static	void ath_printtxbuf(struct ath_buf *bf, int);
2435591b213SSam Leffler #else
244c42a7b7eSSam Leffler #define	IFF_DUMPPKTS(sc, m) \
245fc74a9f9SBrooks Davis 	((sc->sc_ifp->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
246c42a7b7eSSam Leffler #define	DPRINTF(m, fmt, ...)
247c42a7b7eSSam Leffler #define	KEYPRINTF(sc, k, ix, mac)
2485591b213SSam Leffler #endif
2495591b213SSam Leffler 
250c42a7b7eSSam Leffler MALLOC_DEFINE(M_ATHDEV, "athdev", "ath driver dma buffers");
251c42a7b7eSSam Leffler 
2525591b213SSam Leffler int
2535591b213SSam Leffler ath_attach(u_int16_t devid, struct ath_softc *sc)
2545591b213SSam Leffler {
255fc74a9f9SBrooks Davis 	struct ifnet *ifp;
2565591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
257fc74a9f9SBrooks Davis 	struct ath_hal *ah = NULL;
2585591b213SSam Leffler 	HAL_STATUS status;
259c42a7b7eSSam Leffler 	int error = 0, i;
2605591b213SSam Leffler 
261c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: devid 0x%x\n", __func__, devid);
2625591b213SSam Leffler 
263fc74a9f9SBrooks Davis 	ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
264fc74a9f9SBrooks Davis 	if (ifp == NULL) {
265fc74a9f9SBrooks Davis 		device_printf(sc->sc_dev, "can not if_alloc()\n");
266fc74a9f9SBrooks Davis 		error = ENOSPC;
267fc74a9f9SBrooks Davis 		goto bad;
268fc74a9f9SBrooks Davis 	}
269fc74a9f9SBrooks Davis 
2705591b213SSam Leffler 	/* set these up early for if_printf use */
2719bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(sc->sc_dev),
2729bf40edeSBrooks Davis 		device_get_unit(sc->sc_dev));
2735591b213SSam Leffler 
2745591b213SSam Leffler 	ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh, &status);
2755591b213SSam Leffler 	if (ah == NULL) {
2765591b213SSam Leffler 		if_printf(ifp, "unable to attach hardware; HAL status %u\n",
2775591b213SSam Leffler 			status);
2785591b213SSam Leffler 		error = ENXIO;
2795591b213SSam Leffler 		goto bad;
2805591b213SSam Leffler 	}
28185bdc65aSSam Leffler 	if (ah->ah_abi != HAL_ABI_VERSION) {
282c42a7b7eSSam Leffler 		if_printf(ifp, "HAL ABI mismatch detected "
283c42a7b7eSSam Leffler 			"(HAL:0x%x != driver:0x%x)\n",
28485bdc65aSSam Leffler 			ah->ah_abi, HAL_ABI_VERSION);
28585bdc65aSSam Leffler 		error = ENXIO;
28685bdc65aSSam Leffler 		goto bad;
28785bdc65aSSam Leffler 	}
2885591b213SSam Leffler 	sc->sc_ah = ah;
289b58b3803SSam Leffler 	sc->sc_invalid = 0;	/* ready to go, enable interrupt handling */
2905591b213SSam Leffler 
2915591b213SSam Leffler 	/*
292c42a7b7eSSam Leffler 	 * Check if the MAC has multi-rate retry support.
293c42a7b7eSSam Leffler 	 * We do this by trying to setup a fake extended
294c42a7b7eSSam Leffler 	 * descriptor.  MAC's that don't have support will
295c42a7b7eSSam Leffler 	 * return false w/o doing anything.  MAC's that do
296c42a7b7eSSam Leffler 	 * support it will return true w/o doing anything.
297c42a7b7eSSam Leffler 	 */
298c42a7b7eSSam Leffler 	sc->sc_mrretry = ath_hal_setupxtxdesc(ah, NULL, 0,0, 0,0, 0,0);
299c42a7b7eSSam Leffler 
300c42a7b7eSSam Leffler 	/*
301c42a7b7eSSam Leffler 	 * Check if the device has hardware counters for PHY
302c42a7b7eSSam Leffler 	 * errors.  If so we need to enable the MIB interrupt
303c42a7b7eSSam Leffler 	 * so we can act on stat triggers.
304c42a7b7eSSam Leffler 	 */
305c42a7b7eSSam Leffler 	if (ath_hal_hwphycounters(ah))
306c42a7b7eSSam Leffler 		sc->sc_needmib = 1;
307c42a7b7eSSam Leffler 
308c42a7b7eSSam Leffler 	/*
309c42a7b7eSSam Leffler 	 * Get the hardware key cache size.
310c42a7b7eSSam Leffler 	 */
311c42a7b7eSSam Leffler 	sc->sc_keymax = ath_hal_keycachesize(ah);
312e8fd88a3SSam Leffler 	if (sc->sc_keymax > ATH_KEYMAX) {
313e8fd88a3SSam Leffler 		if_printf(ifp, "Warning, using only %u of %u key cache slots\n",
314e8fd88a3SSam Leffler 			ATH_KEYMAX, sc->sc_keymax);
315e8fd88a3SSam Leffler 		sc->sc_keymax = ATH_KEYMAX;
316c42a7b7eSSam Leffler 	}
317c42a7b7eSSam Leffler 	/*
318c42a7b7eSSam Leffler 	 * Reset the key cache since some parts do not
319c42a7b7eSSam Leffler 	 * reset the contents on initial power up.
320c42a7b7eSSam Leffler 	 */
321c42a7b7eSSam Leffler 	for (i = 0; i < sc->sc_keymax; i++)
322c42a7b7eSSam Leffler 		ath_hal_keyreset(ah, i);
323c42a7b7eSSam Leffler 	/*
324c42a7b7eSSam Leffler 	 * Mark key cache slots associated with global keys
325c42a7b7eSSam Leffler 	 * as in use.  If we knew TKIP was not to be used we
326c42a7b7eSSam Leffler 	 * could leave the +32, +64, and +32+64 slots free.
327c42a7b7eSSam Leffler 	 * XXX only for splitmic.
328c42a7b7eSSam Leffler 	 */
329c42a7b7eSSam Leffler 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
330c42a7b7eSSam Leffler 		setbit(sc->sc_keymap, i);
331c42a7b7eSSam Leffler 		setbit(sc->sc_keymap, i+32);
332c42a7b7eSSam Leffler 		setbit(sc->sc_keymap, i+64);
333c42a7b7eSSam Leffler 		setbit(sc->sc_keymap, i+32+64);
334c42a7b7eSSam Leffler 	}
335c42a7b7eSSam Leffler 
336c42a7b7eSSam Leffler 	/*
3375591b213SSam Leffler 	 * Collect the channel list using the default country
3385591b213SSam Leffler 	 * code and including outdoor channels.  The 802.11 layer
33945cabbdcSSam Leffler 	 * is resposible for filtering this list based on settings
34045cabbdcSSam Leffler 	 * like the phy mode.
3415591b213SSam Leffler 	 */
342c42a7b7eSSam Leffler 	error = ath_getchannels(sc, ath_countrycode,
343c42a7b7eSSam Leffler 			ath_outdoor, ath_xchanmode);
3445591b213SSam Leffler 	if (error != 0)
3455591b213SSam Leffler 		goto bad;
3465591b213SSam Leffler 
3475591b213SSam Leffler 	/*
3485591b213SSam Leffler 	 * Setup rate tables for all potential media types.
3495591b213SSam Leffler 	 */
3505591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11A);
3515591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11B);
3525591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11G);
353c42a7b7eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_A);
354c42a7b7eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_G);
355c42a7b7eSSam Leffler 	/* NB: setup here so ath_rate_update is happy */
356c42a7b7eSSam Leffler 	ath_setcurmode(sc, IEEE80211_MODE_11A);
3575591b213SSam Leffler 
358c42a7b7eSSam Leffler 	/*
359c42a7b7eSSam Leffler 	 * Allocate tx+rx descriptors and populate the lists.
360c42a7b7eSSam Leffler 	 */
3615591b213SSam Leffler 	error = ath_desc_alloc(sc);
3625591b213SSam Leffler 	if (error != 0) {
3635591b213SSam Leffler 		if_printf(ifp, "failed to allocate descriptors: %d\n", error);
3645591b213SSam Leffler 		goto bad;
3655591b213SSam Leffler 	}
366e383b240SSam Leffler 	callout_init(&sc->sc_scan_ch, debug_mpsafenet ? CALLOUT_MPSAFE : 0);
3672274d8c8SSam Leffler 	callout_init(&sc->sc_cal_ch, CALLOUT_MPSAFE);
3685591b213SSam Leffler 
369f0b2a0beSSam Leffler 	ATH_TXBUF_LOCK_INIT(sc);
3705591b213SSam Leffler 
3715591b213SSam Leffler 	TASK_INIT(&sc->sc_rxtask, 0, ath_rx_proc, sc);
3725591b213SSam Leffler 	TASK_INIT(&sc->sc_rxorntask, 0, ath_rxorn_proc, sc);
3735591b213SSam Leffler 	TASK_INIT(&sc->sc_fataltask, 0, ath_fatal_proc, sc);
3745591b213SSam Leffler 	TASK_INIT(&sc->sc_bmisstask, 0, ath_bmiss_proc, sc);
375c42a7b7eSSam Leffler 	TASK_INIT(&sc->sc_bstucktask, 0, ath_bstuck_proc, sc);
3765591b213SSam Leffler 
3775591b213SSam Leffler 	/*
378c42a7b7eSSam Leffler 	 * Allocate hardware transmit queues: one queue for
379c42a7b7eSSam Leffler 	 * beacon frames and one data queue for each QoS
380c42a7b7eSSam Leffler 	 * priority.  Note that the hal handles reseting
381c42a7b7eSSam Leffler 	 * these queues at the needed time.
382c42a7b7eSSam Leffler 	 *
383c42a7b7eSSam Leffler 	 * XXX PS-Poll
3845591b213SSam Leffler 	 */
38580d2765fSSam Leffler 	sc->sc_bhalq = ath_beaconq_setup(ah);
3865591b213SSam Leffler 	if (sc->sc_bhalq == (u_int) -1) {
3875591b213SSam Leffler 		if_printf(ifp, "unable to setup a beacon xmit queue!\n");
388c42a7b7eSSam Leffler 		error = EIO;
389b28b4653SSam Leffler 		goto bad2;
3905591b213SSam Leffler 	}
391c42a7b7eSSam Leffler 	sc->sc_cabq = ath_txq_setup(sc, HAL_TX_QUEUE_CAB, 0);
392c42a7b7eSSam Leffler 	if (sc->sc_cabq == NULL) {
393c42a7b7eSSam Leffler 		if_printf(ifp, "unable to setup CAB xmit queue!\n");
394c42a7b7eSSam Leffler 		error = EIO;
395c42a7b7eSSam Leffler 		goto bad2;
396c42a7b7eSSam Leffler 	}
397c42a7b7eSSam Leffler 	/* NB: insure BK queue is the lowest priority h/w queue */
398c42a7b7eSSam Leffler 	if (!ath_tx_setup(sc, WME_AC_BK, HAL_WME_AC_BK)) {
399c42a7b7eSSam Leffler 		if_printf(ifp, "unable to setup xmit queue for %s traffic!\n",
400c42a7b7eSSam Leffler 			ieee80211_wme_acnames[WME_AC_BK]);
401c42a7b7eSSam Leffler 		error = EIO;
402c42a7b7eSSam Leffler 		goto bad2;
403c42a7b7eSSam Leffler 	}
404c42a7b7eSSam Leffler 	if (!ath_tx_setup(sc, WME_AC_BE, HAL_WME_AC_BE) ||
405c42a7b7eSSam Leffler 	    !ath_tx_setup(sc, WME_AC_VI, HAL_WME_AC_VI) ||
406c42a7b7eSSam Leffler 	    !ath_tx_setup(sc, WME_AC_VO, HAL_WME_AC_VO)) {
407c42a7b7eSSam Leffler 		/*
408c42a7b7eSSam Leffler 		 * Not enough hardware tx queues to properly do WME;
409c42a7b7eSSam Leffler 		 * just punt and assign them all to the same h/w queue.
410c42a7b7eSSam Leffler 		 * We could do a better job of this if, for example,
411c42a7b7eSSam Leffler 		 * we allocate queues when we switch from station to
412c42a7b7eSSam Leffler 		 * AP mode.
413c42a7b7eSSam Leffler 		 */
414c42a7b7eSSam Leffler 		if (sc->sc_ac2q[WME_AC_VI] != NULL)
415c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_VI]);
416c42a7b7eSSam Leffler 		if (sc->sc_ac2q[WME_AC_BE] != NULL)
417c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_BE]);
418c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_BE] = sc->sc_ac2q[WME_AC_BK];
419c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_VI] = sc->sc_ac2q[WME_AC_BK];
420c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_VO] = sc->sc_ac2q[WME_AC_BK];
421c42a7b7eSSam Leffler 	}
422c42a7b7eSSam Leffler 
423c42a7b7eSSam Leffler 	/*
424c42a7b7eSSam Leffler 	 * Special case certain configurations.  Note the
425c42a7b7eSSam Leffler 	 * CAB queue is handled by these specially so don't
426c42a7b7eSSam Leffler 	 * include them when checking the txq setup mask.
427c42a7b7eSSam Leffler 	 */
428c42a7b7eSSam Leffler 	switch (sc->sc_txqsetup &~ (1<<sc->sc_cabq->axq_qnum)) {
429c42a7b7eSSam Leffler 	case 0x01:
430c42a7b7eSSam Leffler 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0, sc);
431c42a7b7eSSam Leffler 		break;
432c42a7b7eSSam Leffler 	case 0x0f:
433c42a7b7eSSam Leffler 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0123, sc);
434c42a7b7eSSam Leffler 		break;
435c42a7b7eSSam Leffler 	default:
436c42a7b7eSSam Leffler 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc, sc);
437c42a7b7eSSam Leffler 		break;
438c42a7b7eSSam Leffler 	}
439c42a7b7eSSam Leffler 
440c42a7b7eSSam Leffler 	/*
441c42a7b7eSSam Leffler 	 * Setup rate control.  Some rate control modules
442c42a7b7eSSam Leffler 	 * call back to change the anntena state so expose
443c42a7b7eSSam Leffler 	 * the necessary entry points.
444c42a7b7eSSam Leffler 	 * XXX maybe belongs in struct ath_ratectrl?
445c42a7b7eSSam Leffler 	 */
446c42a7b7eSSam Leffler 	sc->sc_setdefantenna = ath_setdefantenna;
447c42a7b7eSSam Leffler 	sc->sc_rc = ath_rate_attach(sc);
448c42a7b7eSSam Leffler 	if (sc->sc_rc == NULL) {
449c42a7b7eSSam Leffler 		error = EIO;
450c42a7b7eSSam Leffler 		goto bad2;
451c42a7b7eSSam Leffler 	}
452c42a7b7eSSam Leffler 
4533e50ec2cSSam Leffler 	sc->sc_blinking = 0;
454c42a7b7eSSam Leffler 	sc->sc_ledstate = 1;
4553e50ec2cSSam Leffler 	sc->sc_ledon = 0;			/* low true */
4563e50ec2cSSam Leffler 	sc->sc_ledidle = (2700*hz)/1000;	/* 2.7sec */
4573e50ec2cSSam Leffler 	callout_init(&sc->sc_ledtimer, CALLOUT_MPSAFE);
458c42a7b7eSSam Leffler 	/*
459c42a7b7eSSam Leffler 	 * Auto-enable soft led processing for IBM cards and for
460c42a7b7eSSam Leffler 	 * 5211 minipci cards.  Users can also manually enable/disable
461c42a7b7eSSam Leffler 	 * support with a sysctl.
462c42a7b7eSSam Leffler 	 */
463c42a7b7eSSam Leffler 	sc->sc_softled = (devid == AR5212_DEVID_IBM || devid == AR5211_DEVID);
464c42a7b7eSSam Leffler 	if (sc->sc_softled) {
465c42a7b7eSSam Leffler 		ath_hal_gpioCfgOutput(ah, sc->sc_ledpin);
4663e50ec2cSSam Leffler 		ath_hal_gpioset(ah, sc->sc_ledpin, !sc->sc_ledon);
467c42a7b7eSSam Leffler 	}
4685591b213SSam Leffler 
4695591b213SSam Leffler 	ifp->if_softc = sc;
4705591b213SSam Leffler 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
4715591b213SSam Leffler 	ifp->if_start = ath_start;
4725591b213SSam Leffler 	ifp->if_watchdog = ath_watchdog;
4735591b213SSam Leffler 	ifp->if_ioctl = ath_ioctl;
4745591b213SSam Leffler 	ifp->if_init = ath_init;
475154b8df2SMax Laier 	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
476154b8df2SMax Laier 	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
477154b8df2SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
4785591b213SSam Leffler 
479c42a7b7eSSam Leffler 	ic->ic_ifp = ifp;
480c42a7b7eSSam Leffler 	ic->ic_reset = ath_reset;
4815591b213SSam Leffler 	ic->ic_newassoc = ath_newassoc;
482c42a7b7eSSam Leffler 	ic->ic_updateslot = ath_updateslot;
483c42a7b7eSSam Leffler 	ic->ic_wme.wme_update = ath_wme_update;
4845591b213SSam Leffler 	/* XXX not right but it's not used anywhere important */
4855591b213SSam Leffler 	ic->ic_phytype = IEEE80211_T_OFDM;
4865591b213SSam Leffler 	ic->ic_opmode = IEEE80211_M_STA;
487c42a7b7eSSam Leffler 	ic->ic_caps =
488c42a7b7eSSam Leffler 		  IEEE80211_C_IBSS		/* ibss, nee adhoc, mode */
489fe32c3efSSam Leffler 		| IEEE80211_C_HOSTAP		/* hostap mode */
490fe32c3efSSam Leffler 		| IEEE80211_C_MONITOR		/* monitor mode */
491fe32c3efSSam Leffler 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
492c42a7b7eSSam Leffler 		| IEEE80211_C_SHSLOT		/* short slot time supported */
493c42a7b7eSSam Leffler 		| IEEE80211_C_WPA		/* capable of WPA1+WPA2 */
49401e7e035SSam Leffler 		;
495c42a7b7eSSam Leffler 	/*
496c42a7b7eSSam Leffler 	 * Query the hal to figure out h/w crypto support.
497c42a7b7eSSam Leffler 	 */
498c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_WEP))
499c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_WEP;
500c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_OCB))
501c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_AES;
502c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_CCM))
503c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_AES_CCM;
504c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_CKIP))
505c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_CKIP;
506c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_TKIP)) {
507c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TKIP;
508c42a7b7eSSam Leffler 		/*
509c42a7b7eSSam Leffler 		 * Check if h/w does the MIC and/or whether the
510c42a7b7eSSam Leffler 		 * separate key cache entries are required to
511c42a7b7eSSam Leffler 		 * handle both tx+rx MIC keys.
512c42a7b7eSSam Leffler 		 */
513c42a7b7eSSam Leffler 		if (ath_hal_ciphersupported(ah, HAL_CIPHER_MIC))
514c42a7b7eSSam Leffler 			ic->ic_caps |= IEEE80211_C_TKIPMIC;
515c42a7b7eSSam Leffler 		if (ath_hal_tkipsplit(ah))
516c42a7b7eSSam Leffler 			sc->sc_splitmic = 1;
517c42a7b7eSSam Leffler 	}
518e8fd88a3SSam Leffler 	sc->sc_hasclrkey = ath_hal_ciphersupported(ah, HAL_CIPHER_CLR);
519e8fd88a3SSam Leffler 	sc->sc_mcastkey = ath_hal_getmcastkeysearch(ah);
520c42a7b7eSSam Leffler 	/*
521c42a7b7eSSam Leffler 	 * TPC support can be done either with a global cap or
522c42a7b7eSSam Leffler 	 * per-packet support.  The latter is not available on
523c42a7b7eSSam Leffler 	 * all parts.  We're a bit pedantic here as all parts
524c42a7b7eSSam Leffler 	 * support a global cap.
525c42a7b7eSSam Leffler 	 */
526c59005e9SSam Leffler 	if (ath_hal_hastpc(ah) || ath_hal_hastxpowlimit(ah))
527c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TXPMGT;
528c42a7b7eSSam Leffler 
529c42a7b7eSSam Leffler 	/*
530c42a7b7eSSam Leffler 	 * Mark WME capability only if we have sufficient
531c42a7b7eSSam Leffler 	 * hardware queues to do proper priority scheduling.
532c42a7b7eSSam Leffler 	 */
533c42a7b7eSSam Leffler 	if (sc->sc_ac2q[WME_AC_BE] != sc->sc_ac2q[WME_AC_BK])
534c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_WME;
535c42a7b7eSSam Leffler 	/*
536e8fd88a3SSam Leffler 	 * Check for misc other capabilities.
537c42a7b7eSSam Leffler 	 */
538c42a7b7eSSam Leffler 	if (ath_hal_hasbursting(ah))
539c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_BURST;
540c42a7b7eSSam Leffler 
541c42a7b7eSSam Leffler 	/*
542c42a7b7eSSam Leffler 	 * Indicate we need the 802.11 header padded to a
543c42a7b7eSSam Leffler 	 * 32-bit boundary for 4-address and QoS frames.
544c42a7b7eSSam Leffler 	 */
545c42a7b7eSSam Leffler 	ic->ic_flags |= IEEE80211_F_DATAPAD;
546c42a7b7eSSam Leffler 
547c42a7b7eSSam Leffler 	/*
548c42a7b7eSSam Leffler 	 * Query the hal about antenna support.
549c42a7b7eSSam Leffler 	 */
550c42a7b7eSSam Leffler 	sc->sc_defant = ath_hal_getdefantenna(ah);
551c42a7b7eSSam Leffler 
552c42a7b7eSSam Leffler 	/*
553c42a7b7eSSam Leffler 	 * Not all chips have the VEOL support we want to
554c42a7b7eSSam Leffler 	 * use with IBSS beacons; check here for it.
555c42a7b7eSSam Leffler 	 */
556c42a7b7eSSam Leffler 	sc->sc_hasveol = ath_hal_hasveol(ah);
5575591b213SSam Leffler 
5585591b213SSam Leffler 	/* get mac address from hardware */
5595591b213SSam Leffler 	ath_hal_getmac(ah, ic->ic_myaddr);
5605591b213SSam Leffler 
5615591b213SSam Leffler 	/* call MI attach routine. */
562c42a7b7eSSam Leffler 	ieee80211_ifattach(ic);
5635591b213SSam Leffler 	/* override default methods */
5645591b213SSam Leffler 	ic->ic_node_alloc = ath_node_alloc;
5651e774079SSam Leffler 	sc->sc_node_free = ic->ic_node_free;
5665591b213SSam Leffler 	ic->ic_node_free = ath_node_free;
567de5af704SSam Leffler 	ic->ic_node_getrssi = ath_node_getrssi;
568c42a7b7eSSam Leffler 	sc->sc_recv_mgmt = ic->ic_recv_mgmt;
569c42a7b7eSSam Leffler 	ic->ic_recv_mgmt = ath_recv_mgmt;
57045bbf62fSSam Leffler 	sc->sc_newstate = ic->ic_newstate;
57145bbf62fSSam Leffler 	ic->ic_newstate = ath_newstate;
572c1225b52SSam Leffler 	ic->ic_crypto.cs_max_keyix = sc->sc_keymax;
573c42a7b7eSSam Leffler 	ic->ic_crypto.cs_key_alloc = ath_key_alloc;
574c42a7b7eSSam Leffler 	ic->ic_crypto.cs_key_delete = ath_key_delete;
575c42a7b7eSSam Leffler 	ic->ic_crypto.cs_key_set = ath_key_set;
576c42a7b7eSSam Leffler 	ic->ic_crypto.cs_key_update_begin = ath_key_update_begin;
577c42a7b7eSSam Leffler 	ic->ic_crypto.cs_key_update_end = ath_key_update_end;
57845bbf62fSSam Leffler 	/* complete initialization */
579c42a7b7eSSam Leffler 	ieee80211_media_init(ic, ath_media_change, ieee80211_media_status);
5805591b213SSam Leffler 
581c42a7b7eSSam Leffler 	ath_bpfattach(sc);
5824866e6c2SSam Leffler 	/*
5834866e6c2SSam Leffler 	 * Setup dynamic sysctl's now that country code and
5844866e6c2SSam Leffler 	 * regdomain are available from the hal.
5854866e6c2SSam Leffler 	 */
5864866e6c2SSam Leffler 	ath_sysctlattach(sc);
58773454c73SSam Leffler 
588c42a7b7eSSam Leffler 	if (bootverbose)
589c42a7b7eSSam Leffler 		ieee80211_announce(ic);
590c42a7b7eSSam Leffler 	ath_announce(sc);
5915591b213SSam Leffler 	return 0;
592b28b4653SSam Leffler bad2:
593c42a7b7eSSam Leffler 	ath_tx_cleanup(sc);
594b28b4653SSam Leffler 	ath_desc_free(sc);
5955591b213SSam Leffler bad:
5965591b213SSam Leffler 	if (ah)
5975591b213SSam Leffler 		ath_hal_detach(ah);
598fc74a9f9SBrooks Davis 	if (ifp != NULL)
599fc74a9f9SBrooks Davis 		if_free(ifp);
6005591b213SSam Leffler 	sc->sc_invalid = 1;
6015591b213SSam Leffler 	return error;
6025591b213SSam Leffler }
6035591b213SSam Leffler 
6045591b213SSam Leffler int
6055591b213SSam Leffler ath_detach(struct ath_softc *sc)
6065591b213SSam Leffler {
607fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
6085591b213SSam Leffler 
609c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
610c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
6115591b213SSam Leffler 
6125591b213SSam Leffler 	ath_stop(ifp);
61373454c73SSam Leffler 	bpfdetach(ifp);
614c42a7b7eSSam Leffler 	/*
615c42a7b7eSSam Leffler 	 * NB: the order of these is important:
616c42a7b7eSSam Leffler 	 * o call the 802.11 layer before detaching the hal to
617c42a7b7eSSam Leffler 	 *   insure callbacks into the driver to delete global
618c42a7b7eSSam Leffler 	 *   key cache entries can be handled
619c42a7b7eSSam Leffler 	 * o reclaim the tx queue data structures after calling
620c42a7b7eSSam Leffler 	 *   the 802.11 layer as we'll get called back to reclaim
621c42a7b7eSSam Leffler 	 *   node state and potentially want to use them
622c42a7b7eSSam Leffler 	 * o to cleanup the tx queues the hal is called, so detach
623c42a7b7eSSam Leffler 	 *   it last
624c42a7b7eSSam Leffler 	 * Other than that, it's straightforward...
625c42a7b7eSSam Leffler 	 */
626c42a7b7eSSam Leffler 	ieee80211_ifdetach(&sc->sc_ic);
627c42a7b7eSSam Leffler 	ath_rate_detach(sc->sc_rc);
6285591b213SSam Leffler 	ath_desc_free(sc);
629c42a7b7eSSam Leffler 	ath_tx_cleanup(sc);
6305591b213SSam Leffler 	ath_hal_detach(sc->sc_ah);
631c4c6f08fSRuslan Ermilov 	if_free(ifp);
632f0b2a0beSSam Leffler 
6335591b213SSam Leffler 	return 0;
6345591b213SSam Leffler }
6355591b213SSam Leffler 
6365591b213SSam Leffler void
6375591b213SSam Leffler ath_suspend(struct ath_softc *sc)
6385591b213SSam Leffler {
639fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
6405591b213SSam Leffler 
641c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
642c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
6435591b213SSam Leffler 
6445591b213SSam Leffler 	ath_stop(ifp);
6455591b213SSam Leffler }
6465591b213SSam Leffler 
6475591b213SSam Leffler void
6485591b213SSam Leffler ath_resume(struct ath_softc *sc)
6495591b213SSam Leffler {
650fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
6515591b213SSam Leffler 
652c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
653c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
6545591b213SSam Leffler 
6556b59f5e3SSam Leffler 	if (ifp->if_flags & IFF_UP) {
656fc74a9f9SBrooks Davis 		ath_init(sc);
65713f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
6585591b213SSam Leffler 			ath_start(ifp);
6595591b213SSam Leffler 	}
660b50c8bdeSSam Leffler 	if (sc->sc_softled) {
661b50c8bdeSSam Leffler 		ath_hal_gpioCfgOutput(sc->sc_ah, sc->sc_ledpin);
662b50c8bdeSSam Leffler 		ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, !sc->sc_ledon);
663b50c8bdeSSam Leffler 	}
6646b59f5e3SSam Leffler }
6655591b213SSam Leffler 
6665591b213SSam Leffler void
6675591b213SSam Leffler ath_shutdown(struct ath_softc *sc)
6685591b213SSam Leffler {
669fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
6705591b213SSam Leffler 
671c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
672c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
6735591b213SSam Leffler 
6745591b213SSam Leffler 	ath_stop(ifp);
6755591b213SSam Leffler }
6765591b213SSam Leffler 
677c42a7b7eSSam Leffler /*
678c42a7b7eSSam Leffler  * Interrupt handler.  Most of the actual processing is deferred.
679c42a7b7eSSam Leffler  */
6805591b213SSam Leffler void
6815591b213SSam Leffler ath_intr(void *arg)
6825591b213SSam Leffler {
6835591b213SSam Leffler 	struct ath_softc *sc = arg;
684fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
6855591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
6865591b213SSam Leffler 	HAL_INT status;
6875591b213SSam Leffler 
6885591b213SSam Leffler 	if (sc->sc_invalid) {
6895591b213SSam Leffler 		/*
690b58b3803SSam Leffler 		 * The hardware is not ready/present, don't touch anything.
691b58b3803SSam Leffler 		 * Note this can happen early on if the IRQ is shared.
6925591b213SSam Leffler 		 */
693c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid; ignored\n", __func__);
6945591b213SSam Leffler 		return;
6955591b213SSam Leffler 	}
696fdd758d4SSam Leffler 	if (!ath_hal_intrpend(ah))		/* shared irq, not for us */
697fdd758d4SSam Leffler 		return;
69813f4c340SRobert Watson 	if (!((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags &
69913f4c340SRobert Watson 	    IFF_DRV_RUNNING))) {
700c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
701c42a7b7eSSam Leffler 			__func__, ifp->if_flags);
7025591b213SSam Leffler 		ath_hal_getisr(ah, &status);	/* clear ISR */
7035591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable further intr's */
7045591b213SSam Leffler 		return;
7055591b213SSam Leffler 	}
706c42a7b7eSSam Leffler 	/*
707c42a7b7eSSam Leffler 	 * Figure out the reason(s) for the interrupt.  Note
708c42a7b7eSSam Leffler 	 * that the hal returns a pseudo-ISR that may include
709c42a7b7eSSam Leffler 	 * bits we haven't explicitly enabled so we mask the
710c42a7b7eSSam Leffler 	 * value to insure we only process bits we requested.
711c42a7b7eSSam Leffler 	 */
7125591b213SSam Leffler 	ath_hal_getisr(ah, &status);		/* NB: clears ISR too */
713c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_INTR, "%s: status 0x%x\n", __func__, status);
714ecddff40SSam Leffler 	status &= sc->sc_imask;			/* discard unasked for bits */
7155591b213SSam Leffler 	if (status & HAL_INT_FATAL) {
716c42a7b7eSSam Leffler 		/*
717c42a7b7eSSam Leffler 		 * Fatal errors are unrecoverable.  Typically
718c42a7b7eSSam Leffler 		 * these are caused by DMA errors.  Unfortunately
719c42a7b7eSSam Leffler 		 * the exact reason is not (presently) returned
720c42a7b7eSSam Leffler 		 * by the hal.
721c42a7b7eSSam Leffler 		 */
7225591b213SSam Leffler 		sc->sc_stats.ast_hardware++;
7235591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
7245591b213SSam Leffler 		taskqueue_enqueue(taskqueue_swi, &sc->sc_fataltask);
7255591b213SSam Leffler 	} else if (status & HAL_INT_RXORN) {
7265591b213SSam Leffler 		sc->sc_stats.ast_rxorn++;
7275591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
7285591b213SSam Leffler 		taskqueue_enqueue(taskqueue_swi, &sc->sc_rxorntask);
7295591b213SSam Leffler 	} else {
730c42a7b7eSSam Leffler 		if (status & HAL_INT_SWBA) {
731c42a7b7eSSam Leffler 			/*
732c42a7b7eSSam Leffler 			 * Software beacon alert--time to send a beacon.
733c42a7b7eSSam Leffler 			 * Handle beacon transmission directly; deferring
734c42a7b7eSSam Leffler 			 * this is too slow to meet timing constraints
735c42a7b7eSSam Leffler 			 * under load.
736c42a7b7eSSam Leffler 			 */
737c42a7b7eSSam Leffler 			ath_beacon_proc(sc, 0);
738c42a7b7eSSam Leffler 		}
7395591b213SSam Leffler 		if (status & HAL_INT_RXEOL) {
7405591b213SSam Leffler 			/*
7415591b213SSam Leffler 			 * NB: the hardware should re-read the link when
7425591b213SSam Leffler 			 *     RXE bit is written, but it doesn't work at
7435591b213SSam Leffler 			 *     least on older hardware revs.
7445591b213SSam Leffler 			 */
7455591b213SSam Leffler 			sc->sc_stats.ast_rxeol++;
7465591b213SSam Leffler 			sc->sc_rxlink = NULL;
7475591b213SSam Leffler 		}
7485591b213SSam Leffler 		if (status & HAL_INT_TXURN) {
7495591b213SSam Leffler 			sc->sc_stats.ast_txurn++;
7505591b213SSam Leffler 			/* bump tx trigger level */
7515591b213SSam Leffler 			ath_hal_updatetxtriglevel(ah, AH_TRUE);
7525591b213SSam Leffler 		}
7535591b213SSam Leffler 		if (status & HAL_INT_RX)
7545591b213SSam Leffler 			taskqueue_enqueue(taskqueue_swi, &sc->sc_rxtask);
7555591b213SSam Leffler 		if (status & HAL_INT_TX)
7565591b213SSam Leffler 			taskqueue_enqueue(taskqueue_swi, &sc->sc_txtask);
7575591b213SSam Leffler 		if (status & HAL_INT_BMISS) {
7585591b213SSam Leffler 			sc->sc_stats.ast_bmiss++;
7595591b213SSam Leffler 			taskqueue_enqueue(taskqueue_swi, &sc->sc_bmisstask);
7605591b213SSam Leffler 		}
761c42a7b7eSSam Leffler 		if (status & HAL_INT_MIB) {
762c42a7b7eSSam Leffler 			sc->sc_stats.ast_mib++;
763c42a7b7eSSam Leffler 			/*
764c42a7b7eSSam Leffler 			 * Disable interrupts until we service the MIB
765c42a7b7eSSam Leffler 			 * interrupt; otherwise it will continue to fire.
766c42a7b7eSSam Leffler 			 */
767c42a7b7eSSam Leffler 			ath_hal_intrset(ah, 0);
768c42a7b7eSSam Leffler 			/*
769c42a7b7eSSam Leffler 			 * Let the hal handle the event.  We assume it will
770c42a7b7eSSam Leffler 			 * clear whatever condition caused the interrupt.
771c42a7b7eSSam Leffler 			 */
772c42a7b7eSSam Leffler 			ath_hal_mibevent(ah,
773c42a7b7eSSam Leffler 				&ATH_NODE(sc->sc_ic.ic_bss)->an_halstats);
774c42a7b7eSSam Leffler 			ath_hal_intrset(ah, sc->sc_imask);
775c42a7b7eSSam Leffler 		}
7765591b213SSam Leffler 	}
7775591b213SSam Leffler }
7785591b213SSam Leffler 
7795591b213SSam Leffler static void
7805591b213SSam Leffler ath_fatal_proc(void *arg, int pending)
7815591b213SSam Leffler {
7825591b213SSam Leffler 	struct ath_softc *sc = arg;
783fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
7845591b213SSam Leffler 
785c42a7b7eSSam Leffler 	if_printf(ifp, "hardware error; resetting\n");
786c42a7b7eSSam Leffler 	ath_reset(ifp);
7875591b213SSam Leffler }
7885591b213SSam Leffler 
7895591b213SSam Leffler static void
7905591b213SSam Leffler ath_rxorn_proc(void *arg, int pending)
7915591b213SSam Leffler {
7925591b213SSam Leffler 	struct ath_softc *sc = arg;
793fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
7945591b213SSam Leffler 
795c42a7b7eSSam Leffler 	if_printf(ifp, "rx FIFO overrun; resetting\n");
796c42a7b7eSSam Leffler 	ath_reset(ifp);
7975591b213SSam Leffler }
7985591b213SSam Leffler 
7995591b213SSam Leffler static void
8005591b213SSam Leffler ath_bmiss_proc(void *arg, int pending)
8015591b213SSam Leffler {
8025591b213SSam Leffler 	struct ath_softc *sc = arg;
8035591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
8045591b213SSam Leffler 
805c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: pending %u\n", __func__, pending);
8065591b213SSam Leffler 	KASSERT(ic->ic_opmode == IEEE80211_M_STA,
8075591b213SSam Leffler 		("unexpect operating mode %u", ic->ic_opmode));
808e585d188SSam Leffler 	if (ic->ic_state == IEEE80211_S_RUN) {
809e585d188SSam Leffler 		/*
810e585d188SSam Leffler 		 * Rather than go directly to scan state, try to
811e585d188SSam Leffler 		 * reassociate first.  If that fails then the state
812e585d188SSam Leffler 		 * machine will drop us into scanning after timing
813e585d188SSam Leffler 		 * out waiting for a probe response.
814e585d188SSam Leffler 		 */
815b5f4adb3SSam Leffler 		NET_LOCK_GIANT();
816e585d188SSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1);
817b5f4adb3SSam Leffler 		NET_UNLOCK_GIANT();
818e585d188SSam Leffler 	}
8195591b213SSam Leffler }
8205591b213SSam Leffler 
8215591b213SSam Leffler static u_int
8225591b213SSam Leffler ath_chan2flags(struct ieee80211com *ic, struct ieee80211_channel *chan)
8235591b213SSam Leffler {
824c42a7b7eSSam Leffler #define	N(a)	(sizeof(a) / sizeof(a[0]))
8255591b213SSam Leffler 	static const u_int modeflags[] = {
8265591b213SSam Leffler 		0,			/* IEEE80211_MODE_AUTO */
8275591b213SSam Leffler 		CHANNEL_A,		/* IEEE80211_MODE_11A */
8285591b213SSam Leffler 		CHANNEL_B,		/* IEEE80211_MODE_11B */
8295591b213SSam Leffler 		CHANNEL_PUREG,		/* IEEE80211_MODE_11G */
830c42a7b7eSSam Leffler 		0,			/* IEEE80211_MODE_FH */
831c42a7b7eSSam Leffler 		CHANNEL_T,		/* IEEE80211_MODE_TURBO_A */
832c42a7b7eSSam Leffler 		CHANNEL_108G		/* IEEE80211_MODE_TURBO_G */
8335591b213SSam Leffler 	};
834c42a7b7eSSam Leffler 	enum ieee80211_phymode mode = ieee80211_chan2mode(ic, chan);
835c42a7b7eSSam Leffler 
836c42a7b7eSSam Leffler 	KASSERT(mode < N(modeflags), ("unexpected phy mode %u", mode));
837c42a7b7eSSam Leffler 	KASSERT(modeflags[mode] != 0, ("mode %u undefined", mode));
838c42a7b7eSSam Leffler 	return modeflags[mode];
839c42a7b7eSSam Leffler #undef N
8405591b213SSam Leffler }
8415591b213SSam Leffler 
8425591b213SSam Leffler static void
8435591b213SSam Leffler ath_init(void *arg)
8445591b213SSam Leffler {
8455591b213SSam Leffler 	struct ath_softc *sc = (struct ath_softc *) arg;
8465591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
847fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
8485591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
8495591b213SSam Leffler 	HAL_STATUS status;
8505591b213SSam Leffler 
851c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
852c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
8535591b213SSam Leffler 
854f0b2a0beSSam Leffler 	ATH_LOCK(sc);
8555591b213SSam Leffler 	/*
8565591b213SSam Leffler 	 * Stop anything previously setup.  This is safe
8575591b213SSam Leffler 	 * whether this is the first time through or not.
8585591b213SSam Leffler 	 */
859c42a7b7eSSam Leffler 	ath_stop_locked(ifp);
8605591b213SSam Leffler 
8615591b213SSam Leffler 	/*
8625591b213SSam Leffler 	 * The basic interface to setting the hardware in a good
8635591b213SSam Leffler 	 * state is ``reset''.  On return the hardware is known to
8645591b213SSam Leffler 	 * be powered up and with interrupts disabled.  This must
8655591b213SSam Leffler 	 * be followed by initialization of the appropriate bits
8665591b213SSam Leffler 	 * and then setup of the interrupt mask.
8675591b213SSam Leffler 	 */
868b5c99415SSam Leffler 	sc->sc_curchan.channel = ic->ic_curchan->ic_freq;
869b5c99415SSam Leffler 	sc->sc_curchan.channelFlags = ath_chan2flags(ic, ic->ic_curchan);
870c42a7b7eSSam Leffler 	if (!ath_hal_reset(ah, ic->ic_opmode, &sc->sc_curchan, AH_FALSE, &status)) {
8715591b213SSam Leffler 		if_printf(ifp, "unable to reset hardware; hal status %u\n",
8725591b213SSam Leffler 			status);
8735591b213SSam Leffler 		goto done;
8745591b213SSam Leffler 	}
8755591b213SSam Leffler 
8765591b213SSam Leffler 	/*
877c42a7b7eSSam Leffler 	 * This is needed only to setup initial state
878c42a7b7eSSam Leffler 	 * but it's best done after a reset.
879c42a7b7eSSam Leffler 	 */
880c42a7b7eSSam Leffler 	ath_update_txpow(sc);
881c59005e9SSam Leffler 	/*
882c59005e9SSam Leffler 	 * Likewise this is set during reset so update
883c59005e9SSam Leffler 	 * state cached in the driver.
884c59005e9SSam Leffler 	 */
885c59005e9SSam Leffler 	sc->sc_diversity = ath_hal_getdiversity(ah);
886c42a7b7eSSam Leffler 
887c42a7b7eSSam Leffler 	/*
8885591b213SSam Leffler 	 * Setup the hardware after reset: the key cache
8895591b213SSam Leffler 	 * is filled as needed and the receive engine is
8905591b213SSam Leffler 	 * set going.  Frame transmit is handled entirely
8915591b213SSam Leffler 	 * in the frame output path; there's nothing to do
8925591b213SSam Leffler 	 * here except setup the interrupt mask.
8935591b213SSam Leffler 	 */
8945591b213SSam Leffler 	if (ath_startrecv(sc) != 0) {
8955591b213SSam Leffler 		if_printf(ifp, "unable to start recv logic\n");
8965591b213SSam Leffler 		goto done;
8975591b213SSam Leffler 	}
8985591b213SSam Leffler 
8995591b213SSam Leffler 	/*
9005591b213SSam Leffler 	 * Enable interrupts.
9015591b213SSam Leffler 	 */
9025591b213SSam Leffler 	sc->sc_imask = HAL_INT_RX | HAL_INT_TX
9035591b213SSam Leffler 		  | HAL_INT_RXEOL | HAL_INT_RXORN
9045591b213SSam Leffler 		  | HAL_INT_FATAL | HAL_INT_GLOBAL;
905c42a7b7eSSam Leffler 	/*
906c42a7b7eSSam Leffler 	 * Enable MIB interrupts when there are hardware phy counters.
907c42a7b7eSSam Leffler 	 * Note we only do this (at the moment) for station mode.
908c42a7b7eSSam Leffler 	 */
909c42a7b7eSSam Leffler 	if (sc->sc_needmib && ic->ic_opmode == IEEE80211_M_STA)
910c42a7b7eSSam Leffler 		sc->sc_imask |= HAL_INT_MIB;
9115591b213SSam Leffler 	ath_hal_intrset(ah, sc->sc_imask);
9125591b213SSam Leffler 
91313f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
9145591b213SSam Leffler 	ic->ic_state = IEEE80211_S_INIT;
9155591b213SSam Leffler 
9165591b213SSam Leffler 	/*
9175591b213SSam Leffler 	 * The hardware should be ready to go now so it's safe
9185591b213SSam Leffler 	 * to kick the 802.11 state machine as it's likely to
9195591b213SSam Leffler 	 * immediately call back to us to send mgmt frames.
9205591b213SSam Leffler 	 */
921b5c99415SSam Leffler 	ath_chan_change(sc, ic->ic_curchan);
922c42a7b7eSSam Leffler 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
923c42a7b7eSSam Leffler 		if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
92445bbf62fSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
925c42a7b7eSSam Leffler 	} else
9266b59f5e3SSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
9275591b213SSam Leffler done:
928f0b2a0beSSam Leffler 	ATH_UNLOCK(sc);
9295591b213SSam Leffler }
9305591b213SSam Leffler 
9315591b213SSam Leffler static void
932c42a7b7eSSam Leffler ath_stop_locked(struct ifnet *ifp)
9335591b213SSam Leffler {
9345591b213SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
935c42a7b7eSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
9365591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
9375591b213SSam Leffler 
938c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid %u if_flags 0x%x\n",
939c42a7b7eSSam Leffler 		__func__, sc->sc_invalid, ifp->if_flags);
9405591b213SSam Leffler 
941c42a7b7eSSam Leffler 	ATH_LOCK_ASSERT(sc);
94213f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
9435591b213SSam Leffler 		/*
9445591b213SSam Leffler 		 * Shutdown the hardware and driver:
945c42a7b7eSSam Leffler 		 *    reset 802.11 state machine
9465591b213SSam Leffler 		 *    turn off timers
947c42a7b7eSSam Leffler 		 *    disable interrupts
948c42a7b7eSSam Leffler 		 *    turn off the radio
9495591b213SSam Leffler 		 *    clear transmit machinery
9505591b213SSam Leffler 		 *    clear receive machinery
9515591b213SSam Leffler 		 *    drain and release tx queues
9525591b213SSam Leffler 		 *    reclaim beacon resources
9535591b213SSam Leffler 		 *    power down hardware
9545591b213SSam Leffler 		 *
9555591b213SSam Leffler 		 * Note that some of this work is not possible if the
9565591b213SSam Leffler 		 * hardware is gone (invalid).
9575591b213SSam Leffler 		 */
958c42a7b7eSSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
95913f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
9605591b213SSam Leffler 		ifp->if_timer = 0;
961c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
9623e50ec2cSSam Leffler 			if (sc->sc_softled) {
9633e50ec2cSSam Leffler 				callout_stop(&sc->sc_ledtimer);
9643e50ec2cSSam Leffler 				ath_hal_gpioset(ah, sc->sc_ledpin,
9653e50ec2cSSam Leffler 					!sc->sc_ledon);
9663e50ec2cSSam Leffler 				sc->sc_blinking = 0;
9673e50ec2cSSam Leffler 			}
9685591b213SSam Leffler 			ath_hal_intrset(ah, 0);
969c42a7b7eSSam Leffler 		}
9705591b213SSam Leffler 		ath_draintxq(sc);
971c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
9725591b213SSam Leffler 			ath_stoprecv(sc);
973c42a7b7eSSam Leffler 			ath_hal_phydisable(ah);
974c42a7b7eSSam Leffler 		} else
9755591b213SSam Leffler 			sc->sc_rxlink = NULL;
976154b8df2SMax Laier 		IFQ_DRV_PURGE(&ifp->if_snd);
9775591b213SSam Leffler 		ath_beacon_free(sc);
978c42a7b7eSSam Leffler 	}
979c42a7b7eSSam Leffler }
980c42a7b7eSSam Leffler 
981c42a7b7eSSam Leffler static void
982c42a7b7eSSam Leffler ath_stop(struct ifnet *ifp)
983c42a7b7eSSam Leffler {
984c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
985c42a7b7eSSam Leffler 
986c42a7b7eSSam Leffler 	ATH_LOCK(sc);
987c42a7b7eSSam Leffler 	ath_stop_locked(ifp);
988c42a7b7eSSam Leffler 	if (!sc->sc_invalid) {
989c42a7b7eSSam Leffler 		/*
990c42a7b7eSSam Leffler 		 * Set the chip in full sleep mode.  Note that we are
991c42a7b7eSSam Leffler 		 * careful to do this only when bringing the interface
992c42a7b7eSSam Leffler 		 * completely to a stop.  When the chip is in this state
993c42a7b7eSSam Leffler 		 * it must be carefully woken up or references to
994c42a7b7eSSam Leffler 		 * registers in the PCI clock domain may freeze the bus
995c42a7b7eSSam Leffler 		 * (and system).  This varies by chip and is mostly an
996c42a7b7eSSam Leffler 		 * issue with newer parts that go to sleep more quickly.
997c42a7b7eSSam Leffler 		 */
998c42a7b7eSSam Leffler 		ath_hal_setpower(sc->sc_ah, HAL_PM_FULL_SLEEP, 0);
9995591b213SSam Leffler 	}
1000f0b2a0beSSam Leffler 	ATH_UNLOCK(sc);
10015591b213SSam Leffler }
10025591b213SSam Leffler 
10035591b213SSam Leffler /*
10045591b213SSam Leffler  * Reset the hardware w/o losing operational state.  This is
10055591b213SSam Leffler  * basically a more efficient way of doing ath_stop, ath_init,
10065591b213SSam Leffler  * followed by state transitions to the current 802.11
1007c42a7b7eSSam Leffler  * operational state.  Used to recover from various errors and
1008c42a7b7eSSam Leffler  * to reset or reload hardware state.
10095591b213SSam Leffler  */
1010c42a7b7eSSam Leffler static int
1011c42a7b7eSSam Leffler ath_reset(struct ifnet *ifp)
10125591b213SSam Leffler {
1013c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
10145591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
10155591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
10165591b213SSam Leffler 	struct ieee80211_channel *c;
10175591b213SSam Leffler 	HAL_STATUS status;
10185591b213SSam Leffler 
10195591b213SSam Leffler 	/*
10205591b213SSam Leffler 	 * Convert to a HAL channel description with the flags
10215591b213SSam Leffler 	 * constrained to reflect the current operating mode.
10225591b213SSam Leffler 	 */
1023b5c99415SSam Leffler 	c = ic->ic_curchan;
1024c42a7b7eSSam Leffler 	sc->sc_curchan.channel = c->ic_freq;
1025c42a7b7eSSam Leffler 	sc->sc_curchan.channelFlags = ath_chan2flags(ic, c);
10265591b213SSam Leffler 
10275591b213SSam Leffler 	ath_hal_intrset(ah, 0);		/* disable interrupts */
10285591b213SSam Leffler 	ath_draintxq(sc);		/* stop xmit side */
10295591b213SSam Leffler 	ath_stoprecv(sc);		/* stop recv side */
10305591b213SSam Leffler 	/* NB: indicate channel change so we do a full reset */
1031c42a7b7eSSam Leffler 	if (!ath_hal_reset(ah, ic->ic_opmode, &sc->sc_curchan, AH_TRUE, &status))
10325591b213SSam Leffler 		if_printf(ifp, "%s: unable to reset hardware; hal status %u\n",
10335591b213SSam Leffler 			__func__, status);
1034c42a7b7eSSam Leffler 	ath_update_txpow(sc);		/* update tx power state */
1035c59005e9SSam Leffler 	sc->sc_diversity = ath_hal_getdiversity(ah);
10365591b213SSam Leffler 	if (ath_startrecv(sc) != 0)	/* restart recv */
10375591b213SSam Leffler 		if_printf(ifp, "%s: unable to start recv logic\n", __func__);
1038c42a7b7eSSam Leffler 	/*
1039c42a7b7eSSam Leffler 	 * We may be doing a reset in response to an ioctl
1040c42a7b7eSSam Leffler 	 * that changes the channel so update any state that
1041c42a7b7eSSam Leffler 	 * might change as a result.
1042c42a7b7eSSam Leffler 	 */
1043c42a7b7eSSam Leffler 	ath_chan_change(sc, c);
10445591b213SSam Leffler 	if (ic->ic_state == IEEE80211_S_RUN)
10455591b213SSam Leffler 		ath_beacon_config(sc);	/* restart beacons */
1046c42a7b7eSSam Leffler 	ath_hal_intrset(ah, sc->sc_imask);
1047c42a7b7eSSam Leffler 
1048c42a7b7eSSam Leffler 	ath_start(ifp);			/* restart xmit */
1049c42a7b7eSSam Leffler 	return 0;
10505591b213SSam Leffler }
10515591b213SSam Leffler 
10525591b213SSam Leffler static void
10535591b213SSam Leffler ath_start(struct ifnet *ifp)
10545591b213SSam Leffler {
10555591b213SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
10565591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
10575591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
10585591b213SSam Leffler 	struct ieee80211_node *ni;
10595591b213SSam Leffler 	struct ath_buf *bf;
10605591b213SSam Leffler 	struct mbuf *m;
10615591b213SSam Leffler 	struct ieee80211_frame *wh;
1062c42a7b7eSSam Leffler 	struct ether_header *eh;
10635591b213SSam Leffler 
106413f4c340SRobert Watson 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->sc_invalid)
10655591b213SSam Leffler 		return;
10665591b213SSam Leffler 	for (;;) {
10675591b213SSam Leffler 		/*
10685591b213SSam Leffler 		 * Grab a TX buffer and associated resources.
10695591b213SSam Leffler 		 */
1070f0b2a0beSSam Leffler 		ATH_TXBUF_LOCK(sc);
1071c42a7b7eSSam Leffler 		bf = STAILQ_FIRST(&sc->sc_txbuf);
10725591b213SSam Leffler 		if (bf != NULL)
1073c42a7b7eSSam Leffler 			STAILQ_REMOVE_HEAD(&sc->sc_txbuf, bf_list);
1074f0b2a0beSSam Leffler 		ATH_TXBUF_UNLOCK(sc);
10755591b213SSam Leffler 		if (bf == NULL) {
1076c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY, "%s: out of xmit buffers\n",
1077c42a7b7eSSam Leffler 				__func__);
10785591b213SSam Leffler 			sc->sc_stats.ast_tx_qstop++;
107913f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
10805591b213SSam Leffler 			break;
10815591b213SSam Leffler 		}
10825591b213SSam Leffler 		/*
10835591b213SSam Leffler 		 * Poll the management queue for frames; they
10845591b213SSam Leffler 		 * have priority over normal data frames.
10855591b213SSam Leffler 		 */
10865591b213SSam Leffler 		IF_DEQUEUE(&ic->ic_mgtq, m);
10875591b213SSam Leffler 		if (m == NULL) {
10885591b213SSam Leffler 			/*
10895591b213SSam Leffler 			 * No data frames go out unless we're associated.
10905591b213SSam Leffler 			 */
10915591b213SSam Leffler 			if (ic->ic_state != IEEE80211_S_RUN) {
1092c42a7b7eSSam Leffler 				DPRINTF(sc, ATH_DEBUG_ANY,
1093c42a7b7eSSam Leffler 					"%s: ignore data packet, state %u\n",
1094c42a7b7eSSam Leffler 					__func__, ic->ic_state);
10955591b213SSam Leffler 				sc->sc_stats.ast_tx_discard++;
1096f0b2a0beSSam Leffler 				ATH_TXBUF_LOCK(sc);
1097c42a7b7eSSam Leffler 				STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
1098f0b2a0beSSam Leffler 				ATH_TXBUF_UNLOCK(sc);
10995591b213SSam Leffler 				break;
11005591b213SSam Leffler 			}
1101154b8df2SMax Laier 			IFQ_DRV_DEQUEUE(&ifp->if_snd, m);	/* XXX: LOCK */
11025591b213SSam Leffler 			if (m == NULL) {
1103f0b2a0beSSam Leffler 				ATH_TXBUF_LOCK(sc);
1104c42a7b7eSSam Leffler 				STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
1105f0b2a0beSSam Leffler 				ATH_TXBUF_UNLOCK(sc);
11065591b213SSam Leffler 				break;
11075591b213SSam Leffler 			}
1108c42a7b7eSSam Leffler 			/*
1109c42a7b7eSSam Leffler 			 * Find the node for the destination so we can do
1110c42a7b7eSSam Leffler 			 * things like power save and fast frames aggregation.
1111c42a7b7eSSam Leffler 			 */
1112c42a7b7eSSam Leffler 			if (m->m_len < sizeof(struct ether_header) &&
1113c42a7b7eSSam Leffler 			   (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
1114c42a7b7eSSam Leffler 				ic->ic_stats.is_tx_nobuf++;	/* XXX */
1115c42a7b7eSSam Leffler 				ni = NULL;
1116c42a7b7eSSam Leffler 				goto bad;
1117c42a7b7eSSam Leffler 			}
1118c42a7b7eSSam Leffler 			eh = mtod(m, struct ether_header *);
1119c42a7b7eSSam Leffler 			ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1120c42a7b7eSSam Leffler 			if (ni == NULL) {
1121c42a7b7eSSam Leffler 				/* NB: ieee80211_find_txnode does stat+msg */
1122fe234894SSam Leffler 				m_freem(m);
1123c42a7b7eSSam Leffler 				goto bad;
1124c42a7b7eSSam Leffler 			}
1125c42a7b7eSSam Leffler 			if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
1126c42a7b7eSSam Leffler 			    (m->m_flags & M_PWR_SAV) == 0) {
1127c42a7b7eSSam Leffler 				/*
1128c42a7b7eSSam Leffler 				 * Station in power save mode; pass the frame
1129c42a7b7eSSam Leffler 				 * to the 802.11 layer and continue.  We'll get
1130c42a7b7eSSam Leffler 				 * the frame back when the time is right.
1131c42a7b7eSSam Leffler 				 */
1132c42a7b7eSSam Leffler 				ieee80211_pwrsave(ic, ni, m);
1133c42a7b7eSSam Leffler 				goto reclaim;
1134c42a7b7eSSam Leffler 			}
1135c42a7b7eSSam Leffler 			/* calculate priority so we can find the tx queue */
1136c42a7b7eSSam Leffler 			if (ieee80211_classify(ic, m, ni)) {
1137c42a7b7eSSam Leffler 				DPRINTF(sc, ATH_DEBUG_XMIT,
1138c42a7b7eSSam Leffler 					"%s: discard, classification failure\n",
1139c42a7b7eSSam Leffler 					__func__);
1140fe234894SSam Leffler 				m_freem(m);
1141c42a7b7eSSam Leffler 				goto bad;
1142c42a7b7eSSam Leffler 			}
11435591b213SSam Leffler 			ifp->if_opackets++;
11445591b213SSam Leffler 			BPF_MTAP(ifp, m);
11455591b213SSam Leffler 			/*
11465591b213SSam Leffler 			 * Encapsulate the packet in prep for transmission.
11475591b213SSam Leffler 			 */
1148c42a7b7eSSam Leffler 			m = ieee80211_encap(ic, m, ni);
11495591b213SSam Leffler 			if (m == NULL) {
1150c42a7b7eSSam Leffler 				DPRINTF(sc, ATH_DEBUG_ANY,
1151c42a7b7eSSam Leffler 					"%s: encapsulation failure\n",
1152c42a7b7eSSam Leffler 					__func__);
11535591b213SSam Leffler 				sc->sc_stats.ast_tx_encap++;
11545591b213SSam Leffler 				goto bad;
11555591b213SSam Leffler 			}
11565591b213SSam Leffler 		} else {
11570a915fadSSam Leffler 			/*
11580a915fadSSam Leffler 			 * Hack!  The referenced node pointer is in the
11590a915fadSSam Leffler 			 * rcvif field of the packet header.  This is
11600a915fadSSam Leffler 			 * placed there by ieee80211_mgmt_output because
11610a915fadSSam Leffler 			 * we need to hold the reference with the frame
11620a915fadSSam Leffler 			 * and there's no other way (other than packet
11630a915fadSSam Leffler 			 * tags which we consider too expensive to use)
11640a915fadSSam Leffler 			 * to pass it along.
11650a915fadSSam Leffler 			 */
11660a915fadSSam Leffler 			ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
11670a915fadSSam Leffler 			m->m_pkthdr.rcvif = NULL;
11680a915fadSSam Leffler 
11695591b213SSam Leffler 			wh = mtod(m, struct ieee80211_frame *);
11705591b213SSam Leffler 			if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
11715591b213SSam Leffler 			    IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
11725591b213SSam Leffler 				/* fill time stamp */
11735591b213SSam Leffler 				u_int64_t tsf;
11745591b213SSam Leffler 				u_int32_t *tstamp;
11755591b213SSam Leffler 
11765591b213SSam Leffler 				tsf = ath_hal_gettsf64(ah);
11775591b213SSam Leffler 				/* XXX: adjust 100us delay to xmit */
11785591b213SSam Leffler 				tsf += 100;
11795591b213SSam Leffler 				tstamp = (u_int32_t *)&wh[1];
11805591b213SSam Leffler 				tstamp[0] = htole32(tsf & 0xffffffff);
11815591b213SSam Leffler 				tstamp[1] = htole32(tsf >> 32);
11825591b213SSam Leffler 			}
11835591b213SSam Leffler 			sc->sc_stats.ast_tx_mgmt++;
11845591b213SSam Leffler 		}
118573454c73SSam Leffler 
11865591b213SSam Leffler 		if (ath_tx_start(sc, ni, bf, m)) {
11875591b213SSam Leffler 	bad:
11885591b213SSam Leffler 			ifp->if_oerrors++;
1189c42a7b7eSSam Leffler 	reclaim:
1190c42a7b7eSSam Leffler 			ATH_TXBUF_LOCK(sc);
1191c42a7b7eSSam Leffler 			STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
1192c42a7b7eSSam Leffler 			ATH_TXBUF_UNLOCK(sc);
1193c42a7b7eSSam Leffler 			if (ni != NULL)
1194c42a7b7eSSam Leffler 				ieee80211_free_node(ni);
11955591b213SSam Leffler 			continue;
11965591b213SSam Leffler 		}
11975591b213SSam Leffler 
11985591b213SSam Leffler 		sc->sc_tx_timer = 5;
11995591b213SSam Leffler 		ifp->if_timer = 1;
12005591b213SSam Leffler 	}
12015591b213SSam Leffler }
12025591b213SSam Leffler 
12035591b213SSam Leffler static int
12045591b213SSam Leffler ath_media_change(struct ifnet *ifp)
12055591b213SSam Leffler {
1206c42a7b7eSSam Leffler #define	IS_UP(ifp) \
120713f4c340SRobert Watson 	((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
12085591b213SSam Leffler 	int error;
12095591b213SSam Leffler 
12105591b213SSam Leffler 	error = ieee80211_media_change(ifp);
12115591b213SSam Leffler 	if (error == ENETRESET) {
1212c42a7b7eSSam Leffler 		if (IS_UP(ifp))
1213fc74a9f9SBrooks Davis 			ath_init(ifp->if_softc);	/* XXX lose error */
12145591b213SSam Leffler 		error = 0;
12155591b213SSam Leffler 	}
12165591b213SSam Leffler 	return error;
1217c42a7b7eSSam Leffler #undef IS_UP
12185591b213SSam Leffler }
12195591b213SSam Leffler 
12205591b213SSam Leffler #ifdef AR_DEBUG
1221c42a7b7eSSam Leffler static void
1222c42a7b7eSSam Leffler ath_keyprint(const char *tag, u_int ix,
1223c42a7b7eSSam Leffler 	const HAL_KEYVAL *hk, const u_int8_t mac[IEEE80211_ADDR_LEN])
12245591b213SSam Leffler {
1225c42a7b7eSSam Leffler 	static const char *ciphers[] = {
1226c42a7b7eSSam Leffler 		"WEP",
1227c42a7b7eSSam Leffler 		"AES-OCB",
1228c42a7b7eSSam Leffler 		"AES-CCM",
1229c42a7b7eSSam Leffler 		"CKIP",
1230c42a7b7eSSam Leffler 		"TKIP",
1231c42a7b7eSSam Leffler 		"CLR",
1232c42a7b7eSSam Leffler 	};
1233c42a7b7eSSam Leffler 	int i, n;
12345591b213SSam Leffler 
1235c42a7b7eSSam Leffler 	printf("%s: [%02u] %-7s ", tag, ix, ciphers[hk->kv_type]);
1236c42a7b7eSSam Leffler 	for (i = 0, n = hk->kv_len; i < n; i++)
1237c42a7b7eSSam Leffler 		printf("%02x", hk->kv_val[i]);
1238c42a7b7eSSam Leffler 	printf(" mac %s", ether_sprintf(mac));
1239c42a7b7eSSam Leffler 	if (hk->kv_type == HAL_CIPHER_TKIP) {
1240c42a7b7eSSam Leffler 		printf(" mic ");
1241c42a7b7eSSam Leffler 		for (i = 0; i < sizeof(hk->kv_mic); i++)
1242c42a7b7eSSam Leffler 			printf("%02x", hk->kv_mic[i]);
12432075afbaSSam Leffler 	}
1244c42a7b7eSSam Leffler 	printf("\n");
1245c42a7b7eSSam Leffler }
1246c42a7b7eSSam Leffler #endif
1247c42a7b7eSSam Leffler 
12485591b213SSam Leffler /*
1249c42a7b7eSSam Leffler  * Set a TKIP key into the hardware.  This handles the
1250c42a7b7eSSam Leffler  * potential distribution of key state to multiple key
1251c42a7b7eSSam Leffler  * cache slots for TKIP.
12525591b213SSam Leffler  */
1253c42a7b7eSSam Leffler static int
1254c42a7b7eSSam Leffler ath_keyset_tkip(struct ath_softc *sc, const struct ieee80211_key *k,
1255c42a7b7eSSam Leffler 	HAL_KEYVAL *hk, const u_int8_t mac[IEEE80211_ADDR_LEN])
1256c42a7b7eSSam Leffler {
1257c42a7b7eSSam Leffler #define	IEEE80211_KEY_XR	(IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV)
1258c42a7b7eSSam Leffler 	static const u_int8_t zerobssid[IEEE80211_ADDR_LEN];
12598cec0ab9SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
12608cec0ab9SSam Leffler 
1261c42a7b7eSSam Leffler 	KASSERT(k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP,
1262c42a7b7eSSam Leffler 		("got a non-TKIP key, cipher %u", k->wk_cipher->ic_cipher));
1263c42a7b7eSSam Leffler 	KASSERT(sc->sc_splitmic, ("key cache !split"));
1264c42a7b7eSSam Leffler 	if ((k->wk_flags & IEEE80211_KEY_XR) == IEEE80211_KEY_XR) {
1265c42a7b7eSSam Leffler 		/*
1266c1225b52SSam Leffler 		 * TX key goes at first index, RX key at the rx index.
1267c42a7b7eSSam Leffler 		 * The hal handles the MIC keys at index+64.
1268c42a7b7eSSam Leffler 		 */
1269c42a7b7eSSam Leffler 		memcpy(hk->kv_mic, k->wk_txmic, sizeof(hk->kv_mic));
1270c42a7b7eSSam Leffler 		KEYPRINTF(sc, k->wk_keyix, hk, zerobssid);
1271c42a7b7eSSam Leffler 		if (!ath_hal_keyset(ah, k->wk_keyix, hk, zerobssid))
1272c42a7b7eSSam Leffler 			return 0;
1273c42a7b7eSSam Leffler 
1274c42a7b7eSSam Leffler 		memcpy(hk->kv_mic, k->wk_rxmic, sizeof(hk->kv_mic));
1275c42a7b7eSSam Leffler 		KEYPRINTF(sc, k->wk_keyix+32, hk, mac);
1276c42a7b7eSSam Leffler 		/* XXX delete tx key on failure? */
1277c42a7b7eSSam Leffler 		return ath_hal_keyset(ah, k->wk_keyix+32, hk, mac);
1278c42a7b7eSSam Leffler 	} else if (k->wk_flags & IEEE80211_KEY_XR) {
1279c42a7b7eSSam Leffler 		/*
1280c42a7b7eSSam Leffler 		 * TX/RX key goes at first index.
1281c42a7b7eSSam Leffler 		 * The hal handles the MIC keys are index+64.
1282c42a7b7eSSam Leffler 		 */
1283c42a7b7eSSam Leffler 		memcpy(hk->kv_mic, k->wk_flags & IEEE80211_KEY_XMIT ?
1284c42a7b7eSSam Leffler 			k->wk_txmic : k->wk_rxmic, sizeof(hk->kv_mic));
1285e8fd88a3SSam Leffler 		KEYPRINTF(sc, k->wk_keyix, hk, mac);
1286e8fd88a3SSam Leffler 		return ath_hal_keyset(ah, k->wk_keyix, hk, mac);
1287c42a7b7eSSam Leffler 	}
1288c42a7b7eSSam Leffler 	return 0;
1289c42a7b7eSSam Leffler #undef IEEE80211_KEY_XR
1290c42a7b7eSSam Leffler }
1291c42a7b7eSSam Leffler 
1292c42a7b7eSSam Leffler /*
1293c42a7b7eSSam Leffler  * Set a net80211 key into the hardware.  This handles the
1294c42a7b7eSSam Leffler  * potential distribution of key state to multiple key
1295c42a7b7eSSam Leffler  * cache slots for TKIP with hardware MIC support.
1296c42a7b7eSSam Leffler  */
1297c42a7b7eSSam Leffler static int
1298c42a7b7eSSam Leffler ath_keyset(struct ath_softc *sc, const struct ieee80211_key *k,
1299e8fd88a3SSam Leffler 	const u_int8_t mac0[IEEE80211_ADDR_LEN],
1300e8fd88a3SSam Leffler 	struct ieee80211_node *bss)
1301c42a7b7eSSam Leffler {
1302c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
1303c42a7b7eSSam Leffler 	static const u_int8_t ciphermap[] = {
1304c42a7b7eSSam Leffler 		HAL_CIPHER_WEP,		/* IEEE80211_CIPHER_WEP */
1305c42a7b7eSSam Leffler 		HAL_CIPHER_TKIP,	/* IEEE80211_CIPHER_TKIP */
1306c42a7b7eSSam Leffler 		HAL_CIPHER_AES_OCB,	/* IEEE80211_CIPHER_AES_OCB */
1307c42a7b7eSSam Leffler 		HAL_CIPHER_AES_CCM,	/* IEEE80211_CIPHER_AES_CCM */
1308c42a7b7eSSam Leffler 		(u_int8_t) -1,		/* 4 is not allocated */
1309c42a7b7eSSam Leffler 		HAL_CIPHER_CKIP,	/* IEEE80211_CIPHER_CKIP */
1310c42a7b7eSSam Leffler 		HAL_CIPHER_CLR,		/* IEEE80211_CIPHER_NONE */
1311c42a7b7eSSam Leffler 	};
1312c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1313c42a7b7eSSam Leffler 	const struct ieee80211_cipher *cip = k->wk_cipher;
1314e8fd88a3SSam Leffler 	u_int8_t gmac[IEEE80211_ADDR_LEN];
1315e8fd88a3SSam Leffler 	const u_int8_t *mac;
1316c42a7b7eSSam Leffler 	HAL_KEYVAL hk;
1317c42a7b7eSSam Leffler 
1318c42a7b7eSSam Leffler 	memset(&hk, 0, sizeof(hk));
1319c42a7b7eSSam Leffler 	/*
1320c42a7b7eSSam Leffler 	 * Software crypto uses a "clear key" so non-crypto
1321c42a7b7eSSam Leffler 	 * state kept in the key cache are maintained and
1322c42a7b7eSSam Leffler 	 * so that rx frames have an entry to match.
1323c42a7b7eSSam Leffler 	 */
1324c42a7b7eSSam Leffler 	if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) == 0) {
1325c42a7b7eSSam Leffler 		KASSERT(cip->ic_cipher < N(ciphermap),
1326c42a7b7eSSam Leffler 			("invalid cipher type %u", cip->ic_cipher));
1327c42a7b7eSSam Leffler 		hk.kv_type = ciphermap[cip->ic_cipher];
1328c42a7b7eSSam Leffler 		hk.kv_len = k->wk_keylen;
1329c42a7b7eSSam Leffler 		memcpy(hk.kv_val, k->wk_key, k->wk_keylen);
13308cec0ab9SSam Leffler 	} else
1331c42a7b7eSSam Leffler 		hk.kv_type = HAL_CIPHER_CLR;
1332c42a7b7eSSam Leffler 
1333e8fd88a3SSam Leffler 	if ((k->wk_flags & IEEE80211_KEY_GROUP) && sc->sc_mcastkey) {
1334e8fd88a3SSam Leffler 		/*
1335e8fd88a3SSam Leffler 		 * Group keys on hardware that supports multicast frame
1336e8fd88a3SSam Leffler 		 * key search use a mac that is the sender's address with
1337e8fd88a3SSam Leffler 		 * the high bit set instead of the app-specified address.
1338e8fd88a3SSam Leffler 		 */
1339e8fd88a3SSam Leffler 		IEEE80211_ADDR_COPY(gmac, bss->ni_macaddr);
1340e8fd88a3SSam Leffler 		gmac[0] |= 0x80;
1341e8fd88a3SSam Leffler 		mac = gmac;
1342e8fd88a3SSam Leffler 	} else
1343e8fd88a3SSam Leffler 		mac = mac0;
1344e8fd88a3SSam Leffler 
1345c42a7b7eSSam Leffler 	if (hk.kv_type == HAL_CIPHER_TKIP &&
1346c42a7b7eSSam Leffler 	    (k->wk_flags & IEEE80211_KEY_SWMIC) == 0 &&
1347c42a7b7eSSam Leffler 	    sc->sc_splitmic) {
1348c42a7b7eSSam Leffler 		return ath_keyset_tkip(sc, k, &hk, mac);
1349c42a7b7eSSam Leffler 	} else {
1350c42a7b7eSSam Leffler 		KEYPRINTF(sc, k->wk_keyix, &hk, mac);
1351c42a7b7eSSam Leffler 		return ath_hal_keyset(ah, k->wk_keyix, &hk, mac);
13528cec0ab9SSam Leffler 	}
1353c42a7b7eSSam Leffler #undef N
13545591b213SSam Leffler }
13555591b213SSam Leffler 
13565591b213SSam Leffler /*
1357c42a7b7eSSam Leffler  * Allocate tx/rx key slots for TKIP.  We allocate two slots for
1358c42a7b7eSSam Leffler  * each key, one for decrypt/encrypt and the other for the MIC.
1359c42a7b7eSSam Leffler  */
1360c42a7b7eSSam Leffler static u_int16_t
1361c1225b52SSam Leffler key_alloc_2pair(struct ath_softc *sc,
1362c1225b52SSam Leffler 	ieee80211_keyix *txkeyix, ieee80211_keyix *rxkeyix)
1363c42a7b7eSSam Leffler {
1364c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
1365c42a7b7eSSam Leffler 	u_int i, keyix;
1366c42a7b7eSSam Leffler 
1367c42a7b7eSSam Leffler 	KASSERT(sc->sc_splitmic, ("key cache !split"));
1368c42a7b7eSSam Leffler 	/* XXX could optimize */
1369c42a7b7eSSam Leffler 	for (i = 0; i < N(sc->sc_keymap)/4; i++) {
1370c42a7b7eSSam Leffler 		u_int8_t b = sc->sc_keymap[i];
1371c42a7b7eSSam Leffler 		if (b != 0xff) {
1372c42a7b7eSSam Leffler 			/*
1373c42a7b7eSSam Leffler 			 * One or more slots in this byte are free.
1374c42a7b7eSSam Leffler 			 */
1375c42a7b7eSSam Leffler 			keyix = i*NBBY;
1376c42a7b7eSSam Leffler 			while (b & 1) {
1377c42a7b7eSSam Leffler 		again:
1378c42a7b7eSSam Leffler 				keyix++;
1379c42a7b7eSSam Leffler 				b >>= 1;
1380c42a7b7eSSam Leffler 			}
1381c42a7b7eSSam Leffler 			/* XXX IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV */
1382c42a7b7eSSam Leffler 			if (isset(sc->sc_keymap, keyix+32) ||
1383c42a7b7eSSam Leffler 			    isset(sc->sc_keymap, keyix+64) ||
1384c42a7b7eSSam Leffler 			    isset(sc->sc_keymap, keyix+32+64)) {
1385c42a7b7eSSam Leffler 				/* full pair unavailable */
1386c42a7b7eSSam Leffler 				/* XXX statistic */
1387c42a7b7eSSam Leffler 				if (keyix == (i+1)*NBBY) {
1388c42a7b7eSSam Leffler 					/* no slots were appropriate, advance */
1389c42a7b7eSSam Leffler 					continue;
1390c42a7b7eSSam Leffler 				}
1391c42a7b7eSSam Leffler 				goto again;
1392c42a7b7eSSam Leffler 			}
1393c42a7b7eSSam Leffler 			setbit(sc->sc_keymap, keyix);
1394c42a7b7eSSam Leffler 			setbit(sc->sc_keymap, keyix+64);
1395c42a7b7eSSam Leffler 			setbit(sc->sc_keymap, keyix+32);
1396c42a7b7eSSam Leffler 			setbit(sc->sc_keymap, keyix+32+64);
1397c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_KEYCACHE,
1398c42a7b7eSSam Leffler 				"%s: key pair %u,%u %u,%u\n",
1399c42a7b7eSSam Leffler 				__func__, keyix, keyix+64,
1400c42a7b7eSSam Leffler 				keyix+32, keyix+32+64);
1401c1225b52SSam Leffler 			*txkeyix = keyix;
1402c1225b52SSam Leffler 			*rxkeyix = keyix+32;
1403c1225b52SSam Leffler 			return 1;
1404c42a7b7eSSam Leffler 		}
1405c42a7b7eSSam Leffler 	}
1406c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of pair space\n", __func__);
1407c1225b52SSam Leffler 	return 0;
1408c42a7b7eSSam Leffler #undef N
1409c42a7b7eSSam Leffler }
1410c42a7b7eSSam Leffler 
1411c42a7b7eSSam Leffler /*
1412c42a7b7eSSam Leffler  * Allocate a single key cache slot.
1413c42a7b7eSSam Leffler  */
1414c1225b52SSam Leffler static int
1415c1225b52SSam Leffler key_alloc_single(struct ath_softc *sc,
1416c1225b52SSam Leffler 	ieee80211_keyix *txkeyix, ieee80211_keyix *rxkeyix)
1417c42a7b7eSSam Leffler {
1418c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
1419c42a7b7eSSam Leffler 	u_int i, keyix;
1420c42a7b7eSSam Leffler 
1421c42a7b7eSSam Leffler 	/* XXX try i,i+32,i+64,i+32+64 to minimize key pair conflicts */
1422c42a7b7eSSam Leffler 	for (i = 0; i < N(sc->sc_keymap); i++) {
1423c42a7b7eSSam Leffler 		u_int8_t b = sc->sc_keymap[i];
1424c42a7b7eSSam Leffler 		if (b != 0xff) {
1425c42a7b7eSSam Leffler 			/*
1426c42a7b7eSSam Leffler 			 * One or more slots are free.
1427c42a7b7eSSam Leffler 			 */
1428c42a7b7eSSam Leffler 			keyix = i*NBBY;
1429c42a7b7eSSam Leffler 			while (b & 1)
1430c42a7b7eSSam Leffler 				keyix++, b >>= 1;
1431c42a7b7eSSam Leffler 			setbit(sc->sc_keymap, keyix);
1432c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: key %u\n",
1433c42a7b7eSSam Leffler 				__func__, keyix);
1434c1225b52SSam Leffler 			*txkeyix = *rxkeyix = keyix;
1435c1225b52SSam Leffler 			return 1;
1436c42a7b7eSSam Leffler 		}
1437c42a7b7eSSam Leffler 	}
1438c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of space\n", __func__);
1439c1225b52SSam Leffler 	return 0;
1440c42a7b7eSSam Leffler #undef N
1441c42a7b7eSSam Leffler }
1442c42a7b7eSSam Leffler 
1443c42a7b7eSSam Leffler /*
1444c42a7b7eSSam Leffler  * Allocate one or more key cache slots for a uniacst key.  The
1445c42a7b7eSSam Leffler  * key itself is needed only to identify the cipher.  For hardware
1446c42a7b7eSSam Leffler  * TKIP with split cipher+MIC keys we allocate two key cache slot
1447c42a7b7eSSam Leffler  * pairs so that we can setup separate TX and RX MIC keys.  Note
1448c42a7b7eSSam Leffler  * that the MIC key for a TKIP key at slot i is assumed by the
1449c42a7b7eSSam Leffler  * hardware to be at slot i+64.  This limits TKIP keys to the first
1450c42a7b7eSSam Leffler  * 64 entries.
1451c42a7b7eSSam Leffler  */
1452c42a7b7eSSam Leffler static int
1453c1225b52SSam Leffler ath_key_alloc(struct ieee80211com *ic, const struct ieee80211_key *k,
1454c1225b52SSam Leffler 	ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
1455c42a7b7eSSam Leffler {
1456c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
1457c42a7b7eSSam Leffler 
1458c42a7b7eSSam Leffler 	/*
14598ca623d7SSam Leffler 	 * Group key allocation must be handled specially for
14608ca623d7SSam Leffler 	 * parts that do not support multicast key cache search
14618ca623d7SSam Leffler 	 * functionality.  For those parts the key id must match
14628ca623d7SSam Leffler 	 * the h/w key index so lookups find the right key.  On
14638ca623d7SSam Leffler 	 * parts w/ the key search facility we install the sender's
14648ca623d7SSam Leffler 	 * mac address (with the high bit set) and let the hardware
14658ca623d7SSam Leffler 	 * find the key w/o using the key id.  This is preferred as
14668ca623d7SSam Leffler 	 * it permits us to support multiple users for adhoc and/or
14678ca623d7SSam Leffler 	 * multi-station operation.
14688ca623d7SSam Leffler 	 */
14698ca623d7SSam Leffler 	if ((k->wk_flags & IEEE80211_KEY_GROUP) && !sc->sc_mcastkey) {
14708ca623d7SSam Leffler 		if (!(&ic->ic_nw_keys[0] <= k &&
14718ca623d7SSam Leffler 		      k < &ic->ic_nw_keys[IEEE80211_WEP_NKID])) {
14728ca623d7SSam Leffler 			/* should not happen */
14738ca623d7SSam Leffler 			DPRINTF(sc, ATH_DEBUG_KEYCACHE,
14748ca623d7SSam Leffler 				"%s: bogus group key\n", __func__);
1475c1225b52SSam Leffler 			return 0;
14768ca623d7SSam Leffler 		}
14778ca623d7SSam Leffler 		/*
14788ca623d7SSam Leffler 		 * XXX we pre-allocate the global keys so
14798ca623d7SSam Leffler 		 * have no way to check if they've already been allocated.
14808ca623d7SSam Leffler 		 */
1481c1225b52SSam Leffler 		*keyix = *rxkeyix = k - ic->ic_nw_keys;
1482c1225b52SSam Leffler 		return 1;
14838ca623d7SSam Leffler 	}
14848ca623d7SSam Leffler 
14858ca623d7SSam Leffler 	/*
1486c42a7b7eSSam Leffler 	 * We allocate two pair for TKIP when using the h/w to do
1487c42a7b7eSSam Leffler 	 * the MIC.  For everything else, including software crypto,
1488c42a7b7eSSam Leffler 	 * we allocate a single entry.  Note that s/w crypto requires
1489c42a7b7eSSam Leffler 	 * a pass-through slot on the 5211 and 5212.  The 5210 does
1490c42a7b7eSSam Leffler 	 * not support pass-through cache entries and we map all
1491c42a7b7eSSam Leffler 	 * those requests to slot 0.
1492c42a7b7eSSam Leffler 	 */
1493c42a7b7eSSam Leffler 	if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
1494c1225b52SSam Leffler 		return key_alloc_single(sc, keyix, rxkeyix);
1495c42a7b7eSSam Leffler 	} else if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP &&
1496c42a7b7eSSam Leffler 	    (k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && sc->sc_splitmic) {
1497c1225b52SSam Leffler 		return key_alloc_2pair(sc, keyix, rxkeyix);
1498c42a7b7eSSam Leffler 	} else {
1499c1225b52SSam Leffler 		return key_alloc_single(sc, keyix, rxkeyix);
1500c42a7b7eSSam Leffler 	}
1501c42a7b7eSSam Leffler }
1502c42a7b7eSSam Leffler 
1503c42a7b7eSSam Leffler /*
1504c42a7b7eSSam Leffler  * Delete an entry in the key cache allocated by ath_key_alloc.
1505c42a7b7eSSam Leffler  */
1506c42a7b7eSSam Leffler static int
1507c42a7b7eSSam Leffler ath_key_delete(struct ieee80211com *ic, const struct ieee80211_key *k)
1508c42a7b7eSSam Leffler {
1509c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
1510c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1511c42a7b7eSSam Leffler 	const struct ieee80211_cipher *cip = k->wk_cipher;
1512c42a7b7eSSam Leffler 	u_int keyix = k->wk_keyix;
1513c42a7b7eSSam Leffler 
1514c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: delete key %u\n", __func__, keyix);
1515c42a7b7eSSam Leffler 
1516c42a7b7eSSam Leffler 	ath_hal_keyreset(ah, keyix);
1517c42a7b7eSSam Leffler 	/*
1518c42a7b7eSSam Leffler 	 * Handle split tx/rx keying required for TKIP with h/w MIC.
1519c42a7b7eSSam Leffler 	 */
1520c42a7b7eSSam Leffler 	if (cip->ic_cipher == IEEE80211_CIPHER_TKIP &&
1521c1225b52SSam Leffler 	    (k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && sc->sc_splitmic)
1522c42a7b7eSSam Leffler 		ath_hal_keyreset(ah, keyix+32);		/* RX key */
1523c42a7b7eSSam Leffler 	if (keyix >= IEEE80211_WEP_NKID) {
1524c42a7b7eSSam Leffler 		/*
1525c42a7b7eSSam Leffler 		 * Don't touch keymap entries for global keys so
1526c42a7b7eSSam Leffler 		 * they are never considered for dynamic allocation.
1527c42a7b7eSSam Leffler 		 */
1528c42a7b7eSSam Leffler 		clrbit(sc->sc_keymap, keyix);
1529c42a7b7eSSam Leffler 		if (cip->ic_cipher == IEEE80211_CIPHER_TKIP &&
1530c42a7b7eSSam Leffler 		    (k->wk_flags & IEEE80211_KEY_SWMIC) == 0 &&
1531c42a7b7eSSam Leffler 		    sc->sc_splitmic) {
1532c42a7b7eSSam Leffler 			clrbit(sc->sc_keymap, keyix+64);	/* TX key MIC */
1533c42a7b7eSSam Leffler 			clrbit(sc->sc_keymap, keyix+32);	/* RX key */
1534c42a7b7eSSam Leffler 			clrbit(sc->sc_keymap, keyix+32+64);	/* RX key MIC */
1535c42a7b7eSSam Leffler 		}
1536c42a7b7eSSam Leffler 	}
1537c42a7b7eSSam Leffler 	return 1;
1538c42a7b7eSSam Leffler }
1539c42a7b7eSSam Leffler 
1540c42a7b7eSSam Leffler /*
1541c42a7b7eSSam Leffler  * Set the key cache contents for the specified key.  Key cache
1542c42a7b7eSSam Leffler  * slot(s) must already have been allocated by ath_key_alloc.
1543c42a7b7eSSam Leffler  */
1544c42a7b7eSSam Leffler static int
1545c42a7b7eSSam Leffler ath_key_set(struct ieee80211com *ic, const struct ieee80211_key *k,
1546c42a7b7eSSam Leffler 	const u_int8_t mac[IEEE80211_ADDR_LEN])
1547c42a7b7eSSam Leffler {
1548c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
1549c42a7b7eSSam Leffler 
1550e8fd88a3SSam Leffler 	return ath_keyset(sc, k, mac, ic->ic_bss);
1551c42a7b7eSSam Leffler }
1552c42a7b7eSSam Leffler 
1553c42a7b7eSSam Leffler /*
1554c42a7b7eSSam Leffler  * Block/unblock tx+rx processing while a key change is done.
1555c42a7b7eSSam Leffler  * We assume the caller serializes key management operations
1556c42a7b7eSSam Leffler  * so we only need to worry about synchronization with other
1557c42a7b7eSSam Leffler  * uses that originate in the driver.
1558c42a7b7eSSam Leffler  */
1559c42a7b7eSSam Leffler static void
1560c42a7b7eSSam Leffler ath_key_update_begin(struct ieee80211com *ic)
1561c42a7b7eSSam Leffler {
1562c42a7b7eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
1563c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
1564c42a7b7eSSam Leffler 
1565c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
1566c42a7b7eSSam Leffler #if 0
1567c42a7b7eSSam Leffler 	tasklet_disable(&sc->sc_rxtq);
1568c42a7b7eSSam Leffler #endif
1569c42a7b7eSSam Leffler 	IF_LOCK(&ifp->if_snd);		/* NB: doesn't block mgmt frames */
1570c42a7b7eSSam Leffler }
1571c42a7b7eSSam Leffler 
1572c42a7b7eSSam Leffler static void
1573c42a7b7eSSam Leffler ath_key_update_end(struct ieee80211com *ic)
1574c42a7b7eSSam Leffler {
1575c42a7b7eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
1576c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
1577c42a7b7eSSam Leffler 
1578c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
1579c42a7b7eSSam Leffler 	IF_UNLOCK(&ifp->if_snd);
1580c42a7b7eSSam Leffler #if 0
1581c42a7b7eSSam Leffler 	tasklet_enable(&sc->sc_rxtq);
1582c42a7b7eSSam Leffler #endif
1583c42a7b7eSSam Leffler }
15845591b213SSam Leffler 
15854bc0e754SSam Leffler /*
15864bc0e754SSam Leffler  * Calculate the receive filter according to the
15874bc0e754SSam Leffler  * operating mode and state:
15884bc0e754SSam Leffler  *
15894bc0e754SSam Leffler  * o always accept unicast, broadcast, and multicast traffic
1590c42a7b7eSSam Leffler  * o maintain current state of phy error reception (the hal
1591c42a7b7eSSam Leffler  *   may enable phy error frames for noise immunity work)
15924bc0e754SSam Leffler  * o probe request frames are accepted only when operating in
15934bc0e754SSam Leffler  *   hostap, adhoc, or monitor modes
15944bc0e754SSam Leffler  * o enable promiscuous mode according to the interface state
15954bc0e754SSam Leffler  * o accept beacons:
15964bc0e754SSam Leffler  *   - when operating in adhoc mode so the 802.11 layer creates
15974bc0e754SSam Leffler  *     node table entries for peers,
15984bc0e754SSam Leffler  *   - when operating in station mode for collecting rssi data when
15994bc0e754SSam Leffler  *     the station is otherwise quiet, or
16004bc0e754SSam Leffler  *   - when scanning
16014bc0e754SSam Leffler  */
16024bc0e754SSam Leffler static u_int32_t
1603c42a7b7eSSam Leffler ath_calcrxfilter(struct ath_softc *sc, enum ieee80211_state state)
16044bc0e754SSam Leffler {
16054bc0e754SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
16064bc0e754SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1607fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
16084bc0e754SSam Leffler 	u_int32_t rfilt;
16094bc0e754SSam Leffler 
16104bc0e754SSam Leffler 	rfilt = (ath_hal_getrxfilter(ah) & HAL_RX_FILTER_PHYERR)
16114bc0e754SSam Leffler 	      | HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST;
16124bc0e754SSam Leffler 	if (ic->ic_opmode != IEEE80211_M_STA)
16134bc0e754SSam Leffler 		rfilt |= HAL_RX_FILTER_PROBEREQ;
16144bc0e754SSam Leffler 	if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
16154bc0e754SSam Leffler 	    (ifp->if_flags & IFF_PROMISC))
16164bc0e754SSam Leffler 		rfilt |= HAL_RX_FILTER_PROM;
16174bc0e754SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA ||
16184bc0e754SSam Leffler 	    ic->ic_opmode == IEEE80211_M_IBSS ||
1619c42a7b7eSSam Leffler 	    state == IEEE80211_S_SCAN)
16204bc0e754SSam Leffler 		rfilt |= HAL_RX_FILTER_BEACON;
16214bc0e754SSam Leffler 	return rfilt;
16224bc0e754SSam Leffler }
16234bc0e754SSam Leffler 
16245591b213SSam Leffler static void
16255591b213SSam Leffler ath_mode_init(struct ath_softc *sc)
16265591b213SSam Leffler {
16275591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
16285591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1629fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
16305591b213SSam Leffler 	u_int32_t rfilt, mfilt[2], val;
16315591b213SSam Leffler 	u_int8_t pos;
16325591b213SSam Leffler 	struct ifmultiaddr *ifma;
16335591b213SSam Leffler 
16344bc0e754SSam Leffler 	/* configure rx filter */
1635c42a7b7eSSam Leffler 	rfilt = ath_calcrxfilter(sc, ic->ic_state);
16364bc0e754SSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
16374bc0e754SSam Leffler 
16385591b213SSam Leffler 	/* configure operational mode */
1639c42a7b7eSSam Leffler 	ath_hal_setopmode(ah);
1640c42a7b7eSSam Leffler 
1641c42a7b7eSSam Leffler 	/*
1642c42a7b7eSSam Leffler 	 * Handle any link-level address change.  Note that we only
1643c42a7b7eSSam Leffler 	 * need to force ic_myaddr; any other addresses are handled
1644c42a7b7eSSam Leffler 	 * as a byproduct of the ifnet code marking the interface
1645c42a7b7eSSam Leffler 	 * down then up.
1646c42a7b7eSSam Leffler 	 *
1647c42a7b7eSSam Leffler 	 * XXX should get from lladdr instead of arpcom but that's more work
1648c42a7b7eSSam Leffler 	 */
16494a0d6638SRuslan Ermilov 	IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
1650c42a7b7eSSam Leffler 	ath_hal_setmac(ah, ic->ic_myaddr);
16515591b213SSam Leffler 
16525591b213SSam Leffler 	/* calculate and install multicast filter */
16535591b213SSam Leffler 	if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
16545591b213SSam Leffler 		mfilt[0] = mfilt[1] = 0;
165513b203d0SRobert Watson 		IF_ADDR_LOCK(ifp);
16565591b213SSam Leffler 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
16575591b213SSam Leffler 			caddr_t dl;
16585591b213SSam Leffler 
16595591b213SSam Leffler 			/* calculate XOR of eight 6bit values */
16605591b213SSam Leffler 			dl = LLADDR((struct sockaddr_dl *) ifma->ifma_addr);
16615591b213SSam Leffler 			val = LE_READ_4(dl + 0);
16625591b213SSam Leffler 			pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
16635591b213SSam Leffler 			val = LE_READ_4(dl + 3);
16645591b213SSam Leffler 			pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
16655591b213SSam Leffler 			pos &= 0x3f;
16665591b213SSam Leffler 			mfilt[pos / 32] |= (1 << (pos % 32));
16675591b213SSam Leffler 		}
166813b203d0SRobert Watson 		IF_ADDR_UNLOCK(ifp);
16695591b213SSam Leffler 	} else {
16705591b213SSam Leffler 		mfilt[0] = mfilt[1] = ~0;
16715591b213SSam Leffler 	}
16725591b213SSam Leffler 	ath_hal_setmcastfilter(ah, mfilt[0], mfilt[1]);
1673c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x, MC filter %08x:%08x\n",
1674c42a7b7eSSam Leffler 		__func__, rfilt, mfilt[0], mfilt[1]);
16755591b213SSam Leffler }
16765591b213SSam Leffler 
1677c42a7b7eSSam Leffler /*
1678c42a7b7eSSam Leffler  * Set the slot time based on the current setting.
1679c42a7b7eSSam Leffler  */
1680c42a7b7eSSam Leffler static void
1681c42a7b7eSSam Leffler ath_setslottime(struct ath_softc *sc)
1682c42a7b7eSSam Leffler {
1683c42a7b7eSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
1684c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1685c42a7b7eSSam Leffler 
1686c42a7b7eSSam Leffler 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
1687c42a7b7eSSam Leffler 		ath_hal_setslottime(ah, HAL_SLOT_TIME_9);
1688c42a7b7eSSam Leffler 	else
1689c42a7b7eSSam Leffler 		ath_hal_setslottime(ah, HAL_SLOT_TIME_20);
1690c42a7b7eSSam Leffler 	sc->sc_updateslot = OK;
1691c42a7b7eSSam Leffler }
1692c42a7b7eSSam Leffler 
1693c42a7b7eSSam Leffler /*
1694c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update the
1695c42a7b7eSSam Leffler  * slot time based on the current setting.
1696c42a7b7eSSam Leffler  */
1697c42a7b7eSSam Leffler static void
1698c42a7b7eSSam Leffler ath_updateslot(struct ifnet *ifp)
1699c42a7b7eSSam Leffler {
1700c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
1701c42a7b7eSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
1702c42a7b7eSSam Leffler 
1703c42a7b7eSSam Leffler 	/*
1704c42a7b7eSSam Leffler 	 * When not coordinating the BSS, change the hardware
1705c42a7b7eSSam Leffler 	 * immediately.  For other operation we defer the change
1706c42a7b7eSSam Leffler 	 * until beacon updates have propagated to the stations.
1707c42a7b7eSSam Leffler 	 */
1708c42a7b7eSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1709c42a7b7eSSam Leffler 		sc->sc_updateslot = UPDATE;
1710c42a7b7eSSam Leffler 	else
1711c42a7b7eSSam Leffler 		ath_setslottime(sc);
1712c42a7b7eSSam Leffler }
1713c42a7b7eSSam Leffler 
1714c42a7b7eSSam Leffler /*
171580d2765fSSam Leffler  * Setup a h/w transmit queue for beacons.
171680d2765fSSam Leffler  */
171780d2765fSSam Leffler static int
171880d2765fSSam Leffler ath_beaconq_setup(struct ath_hal *ah)
171980d2765fSSam Leffler {
172080d2765fSSam Leffler 	HAL_TXQ_INFO qi;
172180d2765fSSam Leffler 
172280d2765fSSam Leffler 	memset(&qi, 0, sizeof(qi));
172380d2765fSSam Leffler 	qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
172480d2765fSSam Leffler 	qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
172580d2765fSSam Leffler 	qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
17260f2e86fbSSam Leffler 	/* NB: for dynamic turbo, don't enable any other interrupts */
17270f2e86fbSSam Leffler 	qi.tqi_qflags = TXQ_FLAG_TXDESCINT_ENABLE;
172880d2765fSSam Leffler 	return ath_hal_setuptxqueue(ah, HAL_TX_QUEUE_BEACON, &qi);
172980d2765fSSam Leffler }
173080d2765fSSam Leffler 
173180d2765fSSam Leffler /*
17320f2e86fbSSam Leffler  * Setup the transmit queue parameters for the beacon queue.
17330f2e86fbSSam Leffler  */
17340f2e86fbSSam Leffler static int
17350f2e86fbSSam Leffler ath_beaconq_config(struct ath_softc *sc)
17360f2e86fbSSam Leffler {
17370f2e86fbSSam Leffler #define	ATH_EXPONENT_TO_VALUE(v)	((1<<(v))-1)
17380f2e86fbSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
17390f2e86fbSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
17400f2e86fbSSam Leffler 	HAL_TXQ_INFO qi;
17410f2e86fbSSam Leffler 
17420f2e86fbSSam Leffler 	ath_hal_gettxqueueprops(ah, sc->sc_bhalq, &qi);
17430f2e86fbSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
17440f2e86fbSSam Leffler 		/*
17450f2e86fbSSam Leffler 		 * Always burst out beacon and CAB traffic.
17460f2e86fbSSam Leffler 		 */
17470f2e86fbSSam Leffler 		qi.tqi_aifs = ATH_BEACON_AIFS_DEFAULT;
17480f2e86fbSSam Leffler 		qi.tqi_cwmin = ATH_BEACON_CWMIN_DEFAULT;
17490f2e86fbSSam Leffler 		qi.tqi_cwmax = ATH_BEACON_CWMAX_DEFAULT;
17500f2e86fbSSam Leffler 	} else {
17510f2e86fbSSam Leffler 		struct wmeParams *wmep =
17520f2e86fbSSam Leffler 			&ic->ic_wme.wme_chanParams.cap_wmeParams[WME_AC_BE];
17530f2e86fbSSam Leffler 		/*
17540f2e86fbSSam Leffler 		 * Adhoc mode; important thing is to use 2x cwmin.
17550f2e86fbSSam Leffler 		 */
17560f2e86fbSSam Leffler 		qi.tqi_aifs = wmep->wmep_aifsn;
17570f2e86fbSSam Leffler 		qi.tqi_cwmin = 2*ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
17580f2e86fbSSam Leffler 		qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
17590f2e86fbSSam Leffler 	}
17600f2e86fbSSam Leffler 
17610f2e86fbSSam Leffler 	if (!ath_hal_settxqueueprops(ah, sc->sc_bhalq, &qi)) {
17620f2e86fbSSam Leffler 		device_printf(sc->sc_dev, "unable to update parameters for "
17630f2e86fbSSam Leffler 			"beacon hardware queue!\n");
17640f2e86fbSSam Leffler 		return 0;
17650f2e86fbSSam Leffler 	} else {
17660f2e86fbSSam Leffler 		ath_hal_resettxqueue(ah, sc->sc_bhalq); /* push to h/w */
17670f2e86fbSSam Leffler 		return 1;
17680f2e86fbSSam Leffler 	}
17690f2e86fbSSam Leffler #undef ATH_EXPONENT_TO_VALUE
17700f2e86fbSSam Leffler }
17710f2e86fbSSam Leffler 
17720f2e86fbSSam Leffler /*
1773c42a7b7eSSam Leffler  * Allocate and setup an initial beacon frame.
1774c42a7b7eSSam Leffler  */
17755591b213SSam Leffler static int
17765591b213SSam Leffler ath_beacon_alloc(struct ath_softc *sc, struct ieee80211_node *ni)
17775591b213SSam Leffler {
1778c42a7b7eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
17795591b213SSam Leffler 	struct ath_buf *bf;
17805591b213SSam Leffler 	struct mbuf *m;
1781c42a7b7eSSam Leffler 	int error;
17825591b213SSam Leffler 
1783c42a7b7eSSam Leffler 	bf = STAILQ_FIRST(&sc->sc_bbuf);
1784c42a7b7eSSam Leffler 	if (bf == NULL) {
1785c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON, "%s: no dma buffers\n", __func__);
1786c42a7b7eSSam Leffler 		sc->sc_stats.ast_be_nombuf++;	/* XXX */
1787c42a7b7eSSam Leffler 		return ENOMEM;			/* XXX */
1788c42a7b7eSSam Leffler 	}
17895591b213SSam Leffler 	/*
17905591b213SSam Leffler 	 * NB: the beacon data buffer must be 32-bit aligned;
17915591b213SSam Leffler 	 * we assume the mbuf routines will return us something
17925591b213SSam Leffler 	 * with this alignment (perhaps should assert).
17935591b213SSam Leffler 	 */
1794c42a7b7eSSam Leffler 	m = ieee80211_beacon_alloc(ic, ni, &sc->sc_boff);
17955591b213SSam Leffler 	if (m == NULL) {
1796c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON, "%s: cannot get mbuf\n",
1797c42a7b7eSSam Leffler 			__func__);
17985591b213SSam Leffler 		sc->sc_stats.ast_be_nombuf++;
17995591b213SSam Leffler 		return ENOMEM;
18005591b213SSam Leffler 	}
1801f9e6219bSSam Leffler 	error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m,
1802f9e6219bSSam Leffler 				     bf->bf_segs, &bf->bf_nseg,
18035591b213SSam Leffler 				     BUS_DMA_NOWAIT);
1804c42a7b7eSSam Leffler 	if (error == 0) {
1805c42a7b7eSSam Leffler 		bf->bf_m = m;
1806f818612bSSam Leffler 		bf->bf_node = ieee80211_ref_node(ni);
1807c42a7b7eSSam Leffler 	} else {
18085591b213SSam Leffler 		m_freem(m);
1809c42a7b7eSSam Leffler 	}
18105591b213SSam Leffler 	return error;
18115591b213SSam Leffler }
1812c42a7b7eSSam Leffler 
1813c42a7b7eSSam Leffler /*
1814c42a7b7eSSam Leffler  * Setup the beacon frame for transmit.
1815c42a7b7eSSam Leffler  */
1816c42a7b7eSSam Leffler static void
1817c42a7b7eSSam Leffler ath_beacon_setup(struct ath_softc *sc, struct ath_buf *bf)
1818c42a7b7eSSam Leffler {
1819c42a7b7eSSam Leffler #define	USE_SHPREAMBLE(_ic) \
1820c42a7b7eSSam Leffler 	(((_ic)->ic_flags & (IEEE80211_F_SHPREAMBLE | IEEE80211_F_USEBARKER))\
1821c42a7b7eSSam Leffler 		== IEEE80211_F_SHPREAMBLE)
1822c42a7b7eSSam Leffler 	struct ieee80211_node *ni = bf->bf_node;
1823c42a7b7eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
1824c42a7b7eSSam Leffler 	struct mbuf *m = bf->bf_m;
1825c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1826c42a7b7eSSam Leffler 	struct ath_node *an = ATH_NODE(ni);
1827c42a7b7eSSam Leffler 	struct ath_desc *ds;
1828c42a7b7eSSam Leffler 	int flags, antenna;
1829c42a7b7eSSam Leffler 	u_int8_t rate;
1830c42a7b7eSSam Leffler 
1831c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_BEACON, "%s: m %p len %u\n",
1832c42a7b7eSSam Leffler 		__func__, m, m->m_len);
18335591b213SSam Leffler 
18345591b213SSam Leffler 	/* setup descriptors */
18355591b213SSam Leffler 	ds = bf->bf_desc;
18365591b213SSam Leffler 
1837c42a7b7eSSam Leffler 	flags = HAL_TXDESC_NOACK;
1838c42a7b7eSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_hasveol) {
1839c42a7b7eSSam Leffler 		ds->ds_link = bf->bf_daddr;	/* self-linked */
1840c42a7b7eSSam Leffler 		flags |= HAL_TXDESC_VEOL;
1841c42a7b7eSSam Leffler 		/*
1842c42a7b7eSSam Leffler 		 * Let hardware handle antenna switching.
1843c42a7b7eSSam Leffler 		 */
18444866e6c2SSam Leffler 		antenna = sc->sc_txantenna;
1845c42a7b7eSSam Leffler 	} else {
18465591b213SSam Leffler 		ds->ds_link = 0;
1847c42a7b7eSSam Leffler 		/*
1848c42a7b7eSSam Leffler 		 * Switch antenna every 4 beacons.
1849c42a7b7eSSam Leffler 		 * XXX assumes two antenna
1850c42a7b7eSSam Leffler 		 */
1851c42a7b7eSSam Leffler 		antenna = (sc->sc_stats.ast_be_xmit & 4 ? 2 : 1);
1852c42a7b7eSSam Leffler 	}
1853c42a7b7eSSam Leffler 
1854c42a7b7eSSam Leffler 	KASSERT(bf->bf_nseg == 1,
1855c42a7b7eSSam Leffler 		("multi-segment beacon frame; nseg %u", bf->bf_nseg));
18565591b213SSam Leffler 	ds->ds_data = bf->bf_segs[0].ds_addr;
18575591b213SSam Leffler 	/*
18585591b213SSam Leffler 	 * Calculate rate code.
18595591b213SSam Leffler 	 * XXX everything at min xmit rate
18605591b213SSam Leffler 	 */
1861c42a7b7eSSam Leffler 	if (USE_SHPREAMBLE(ic))
1862c42a7b7eSSam Leffler 		rate = an->an_tx_mgtratesp;
18635591b213SSam Leffler 	else
1864c42a7b7eSSam Leffler 		rate = an->an_tx_mgtrate;
18655591b213SSam Leffler 	ath_hal_setuptxdesc(ah, ds
1866c42a7b7eSSam Leffler 		, m->m_len + IEEE80211_CRC_LEN	/* frame length */
18675591b213SSam Leffler 		, sizeof(struct ieee80211_frame)/* header length */
18685591b213SSam Leffler 		, HAL_PKT_TYPE_BEACON		/* Atheros packet type */
1869c42a7b7eSSam Leffler 		, ni->ni_txpower		/* txpower XXX */
18705591b213SSam Leffler 		, rate, 1			/* series 0 rate/tries */
18715591b213SSam Leffler 		, HAL_TXKEYIX_INVALID		/* no encryption */
1872c42a7b7eSSam Leffler 		, antenna			/* antenna mode */
1873c42a7b7eSSam Leffler 		, flags				/* no ack, veol for beacons */
18745591b213SSam Leffler 		, 0				/* rts/cts rate */
18755591b213SSam Leffler 		, 0				/* rts/cts duration */
18765591b213SSam Leffler 	);
18775591b213SSam Leffler 	/* NB: beacon's BufLen must be a multiple of 4 bytes */
18785591b213SSam Leffler 	ath_hal_filltxdesc(ah, ds
1879c42a7b7eSSam Leffler 		, roundup(m->m_len, 4)		/* buffer length */
18805591b213SSam Leffler 		, AH_TRUE			/* first segment */
18815591b213SSam Leffler 		, AH_TRUE			/* last segment */
1882c42a7b7eSSam Leffler 		, ds				/* first descriptor */
18835591b213SSam Leffler 	);
1884c42a7b7eSSam Leffler #undef USE_SHPREAMBLE
18855591b213SSam Leffler }
18865591b213SSam Leffler 
1887c42a7b7eSSam Leffler /*
1888c42a7b7eSSam Leffler  * Transmit a beacon frame at SWBA.  Dynamic updates to the
1889c42a7b7eSSam Leffler  * frame contents are done as needed and the slot time is
1890c42a7b7eSSam Leffler  * also adjusted based on current state.
1891c42a7b7eSSam Leffler  */
18925591b213SSam Leffler static void
18935591b213SSam Leffler ath_beacon_proc(void *arg, int pending)
18945591b213SSam Leffler {
18955591b213SSam Leffler 	struct ath_softc *sc = arg;
1896c42a7b7eSSam Leffler 	struct ath_buf *bf = STAILQ_FIRST(&sc->sc_bbuf);
1897c42a7b7eSSam Leffler 	struct ieee80211_node *ni = bf->bf_node;
1898c42a7b7eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
18995591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1900c42a7b7eSSam Leffler 	struct mbuf *m;
1901c42a7b7eSSam Leffler 	int ncabq, error, otherant;
19025591b213SSam Leffler 
1903c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_BEACON_PROC, "%s: pending %u\n",
1904c42a7b7eSSam Leffler 		__func__, pending);
1905c42a7b7eSSam Leffler 
19060a915fadSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA ||
1907c42a7b7eSSam Leffler 	    ic->ic_opmode == IEEE80211_M_MONITOR ||
19080a915fadSSam Leffler 	    bf == NULL || bf->bf_m == NULL) {
1909c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: ic_flags=%x bf=%p bf_m=%p\n",
1910c42a7b7eSSam Leffler 			__func__, ic->ic_flags, bf, bf ? bf->bf_m : NULL);
19115591b213SSam Leffler 		return;
19125591b213SSam Leffler 	}
1913c42a7b7eSSam Leffler 	/*
1914c42a7b7eSSam Leffler 	 * Check if the previous beacon has gone out.  If
1915c42a7b7eSSam Leffler 	 * not don't don't try to post another, skip this
1916c42a7b7eSSam Leffler 	 * period and wait for the next.  Missed beacons
1917c42a7b7eSSam Leffler 	 * indicate a problem and should not occur.  If we
1918c42a7b7eSSam Leffler 	 * miss too many consecutive beacons reset the device.
1919c42a7b7eSSam Leffler 	 */
1920c42a7b7eSSam Leffler 	if (ath_hal_numtxpending(ah, sc->sc_bhalq) != 0) {
1921c42a7b7eSSam Leffler 		sc->sc_bmisscount++;
1922c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON_PROC,
1923c42a7b7eSSam Leffler 			"%s: missed %u consecutive beacons\n",
1924c42a7b7eSSam Leffler 			__func__, sc->sc_bmisscount);
1925c42a7b7eSSam Leffler 		if (sc->sc_bmisscount > 3)		/* NB: 3 is a guess */
1926c42a7b7eSSam Leffler 			taskqueue_enqueue(taskqueue_swi, &sc->sc_bstucktask);
1927c42a7b7eSSam Leffler 		return;
1928c42a7b7eSSam Leffler 	}
1929c42a7b7eSSam Leffler 	if (sc->sc_bmisscount != 0) {
1930c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
1931c42a7b7eSSam Leffler 			"%s: resume beacon xmit after %u misses\n",
1932c42a7b7eSSam Leffler 			__func__, sc->sc_bmisscount);
1933c42a7b7eSSam Leffler 		sc->sc_bmisscount = 0;
1934c42a7b7eSSam Leffler 	}
1935c42a7b7eSSam Leffler 
1936c42a7b7eSSam Leffler 	/*
1937c42a7b7eSSam Leffler 	 * Update dynamic beacon contents.  If this returns
1938c42a7b7eSSam Leffler 	 * non-zero then we need to remap the memory because
1939c42a7b7eSSam Leffler 	 * the beacon frame changed size (probably because
1940c42a7b7eSSam Leffler 	 * of the TIM bitmap).
1941c42a7b7eSSam Leffler 	 */
1942c42a7b7eSSam Leffler 	m = bf->bf_m;
1943c42a7b7eSSam Leffler 	ncabq = ath_hal_numtxpending(ah, sc->sc_cabq->axq_qnum);
1944c42a7b7eSSam Leffler 	if (ieee80211_beacon_update(ic, bf->bf_node, &sc->sc_boff, m, ncabq)) {
1945c42a7b7eSSam Leffler 		/* XXX too conservative? */
1946c42a7b7eSSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
1947f9e6219bSSam Leffler 		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m,
1948f9e6219bSSam Leffler 					     bf->bf_segs, &bf->bf_nseg,
1949c42a7b7eSSam Leffler 					     BUS_DMA_NOWAIT);
1950c42a7b7eSSam Leffler 		if (error != 0) {
1951c42a7b7eSSam Leffler 			if_printf(ic->ic_ifp,
1952f9e6219bSSam Leffler 			    "%s: bus_dmamap_load_mbuf_sg failed, error %u\n",
1953c42a7b7eSSam Leffler 			    __func__, error);
1954c42a7b7eSSam Leffler 			return;
1955c42a7b7eSSam Leffler 		}
1956c42a7b7eSSam Leffler 	}
1957c42a7b7eSSam Leffler 
1958c42a7b7eSSam Leffler 	/*
1959c42a7b7eSSam Leffler 	 * Handle slot time change when a non-ERP station joins/leaves
1960c42a7b7eSSam Leffler 	 * an 11g network.  The 802.11 layer notifies us via callback,
1961c42a7b7eSSam Leffler 	 * we mark updateslot, then wait one beacon before effecting
1962c42a7b7eSSam Leffler 	 * the change.  This gives associated stations at least one
1963c42a7b7eSSam Leffler 	 * beacon interval to note the state change.
1964c42a7b7eSSam Leffler 	 */
1965c42a7b7eSSam Leffler 	/* XXX locking */
1966c42a7b7eSSam Leffler 	if (sc->sc_updateslot == UPDATE)
1967c42a7b7eSSam Leffler 		sc->sc_updateslot = COMMIT;	/* commit next beacon */
1968c42a7b7eSSam Leffler 	else if (sc->sc_updateslot == COMMIT)
1969c42a7b7eSSam Leffler 		ath_setslottime(sc);		/* commit change to h/w */
1970c42a7b7eSSam Leffler 
1971c42a7b7eSSam Leffler 	/*
1972c42a7b7eSSam Leffler 	 * Check recent per-antenna transmit statistics and flip
1973c42a7b7eSSam Leffler 	 * the default antenna if noticeably more frames went out
1974c42a7b7eSSam Leffler 	 * on the non-default antenna.
1975c42a7b7eSSam Leffler 	 * XXX assumes 2 anntenae
1976c42a7b7eSSam Leffler 	 */
1977c42a7b7eSSam Leffler 	otherant = sc->sc_defant & 1 ? 2 : 1;
1978c42a7b7eSSam Leffler 	if (sc->sc_ant_tx[otherant] > sc->sc_ant_tx[sc->sc_defant] + 2)
1979c42a7b7eSSam Leffler 		ath_setdefantenna(sc, otherant);
1980c42a7b7eSSam Leffler 	sc->sc_ant_tx[1] = sc->sc_ant_tx[2] = 0;
1981c42a7b7eSSam Leffler 
1982c42a7b7eSSam Leffler 	/*
1983c42a7b7eSSam Leffler 	 * Construct tx descriptor.
1984c42a7b7eSSam Leffler 	 */
1985c42a7b7eSSam Leffler 	ath_beacon_setup(sc, bf);
1986c42a7b7eSSam Leffler 
1987c42a7b7eSSam Leffler 	/*
1988c42a7b7eSSam Leffler 	 * Stop any current dma and put the new frame on the queue.
1989c42a7b7eSSam Leffler 	 * This should never fail since we check above that no frames
1990c42a7b7eSSam Leffler 	 * are still pending on the queue.
1991c42a7b7eSSam Leffler 	 */
19925591b213SSam Leffler 	if (!ath_hal_stoptxdma(ah, sc->sc_bhalq)) {
1993c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY,
1994c42a7b7eSSam Leffler 			"%s: beacon queue %u did not stop?\n",
1995c42a7b7eSSam Leffler 			__func__, sc->sc_bhalq);
19965591b213SSam Leffler 	}
19975591b213SSam Leffler 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
19985591b213SSam Leffler 
1999c42a7b7eSSam Leffler 	/*
2000c42a7b7eSSam Leffler 	 * Enable the CAB queue before the beacon queue to
2001c42a7b7eSSam Leffler 	 * insure cab frames are triggered by this beacon.
2002c42a7b7eSSam Leffler 	 */
2003c42a7b7eSSam Leffler 	if (sc->sc_boff.bo_tim[4] & 1)		/* NB: only at DTIM */
2004c42a7b7eSSam Leffler 		ath_hal_txstart(ah, sc->sc_cabq->axq_qnum);
20055591b213SSam Leffler 	ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr);
20065591b213SSam Leffler 	ath_hal_txstart(ah, sc->sc_bhalq);
2007c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_BEACON_PROC,
2008c42a7b7eSSam Leffler 		"%s: TXDP[%u] = %p (%p)\n", __func__,
2009c42a7b7eSSam Leffler 		sc->sc_bhalq, (caddr_t)bf->bf_daddr, bf->bf_desc);
2010c42a7b7eSSam Leffler 
2011c42a7b7eSSam Leffler 	sc->sc_stats.ast_be_xmit++;
20125591b213SSam Leffler }
20135591b213SSam Leffler 
2014c42a7b7eSSam Leffler /*
2015c42a7b7eSSam Leffler  * Reset the hardware after detecting beacons have stopped.
2016c42a7b7eSSam Leffler  */
2017c42a7b7eSSam Leffler static void
2018c42a7b7eSSam Leffler ath_bstuck_proc(void *arg, int pending)
2019c42a7b7eSSam Leffler {
2020c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
2021fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
2022c42a7b7eSSam Leffler 
2023c42a7b7eSSam Leffler 	if_printf(ifp, "stuck beacon; resetting (bmiss count %u)\n",
2024c42a7b7eSSam Leffler 		sc->sc_bmisscount);
2025c42a7b7eSSam Leffler 	ath_reset(ifp);
2026c42a7b7eSSam Leffler }
2027c42a7b7eSSam Leffler 
2028c42a7b7eSSam Leffler /*
2029c42a7b7eSSam Leffler  * Reclaim beacon resources.
2030c42a7b7eSSam Leffler  */
20315591b213SSam Leffler static void
20325591b213SSam Leffler ath_beacon_free(struct ath_softc *sc)
20335591b213SSam Leffler {
2034c42a7b7eSSam Leffler 	struct ath_buf *bf;
20355591b213SSam Leffler 
2036f818612bSSam Leffler 	STAILQ_FOREACH(bf, &sc->sc_bbuf, bf_list) {
20375591b213SSam Leffler 		if (bf->bf_m != NULL) {
20385591b213SSam Leffler 			bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
20395591b213SSam Leffler 			m_freem(bf->bf_m);
20405591b213SSam Leffler 			bf->bf_m = NULL;
2041f818612bSSam Leffler 		}
2042f818612bSSam Leffler 		if (bf->bf_node != NULL) {
2043f818612bSSam Leffler 			ieee80211_free_node(bf->bf_node);
20445591b213SSam Leffler 			bf->bf_node = NULL;
20455591b213SSam Leffler 		}
20465591b213SSam Leffler 	}
2047f818612bSSam Leffler }
20485591b213SSam Leffler 
20495591b213SSam Leffler /*
20505591b213SSam Leffler  * Configure the beacon and sleep timers.
20515591b213SSam Leffler  *
20525591b213SSam Leffler  * When operating as an AP this resets the TSF and sets
20535591b213SSam Leffler  * up the hardware to notify us when we need to issue beacons.
20545591b213SSam Leffler  *
20555591b213SSam Leffler  * When operating in station mode this sets up the beacon
20565591b213SSam Leffler  * timers according to the timestamp of the last received
20575591b213SSam Leffler  * beacon and the current TSF, configures PCF and DTIM
20585591b213SSam Leffler  * handling, programs the sleep registers so the hardware
20595591b213SSam Leffler  * will wakeup in time to receive beacons, and configures
20605591b213SSam Leffler  * the beacon miss handling so we'll receive a BMISS
20615591b213SSam Leffler  * interrupt when we stop seeing beacons from the AP
20625591b213SSam Leffler  * we've associated with.
20635591b213SSam Leffler  */
20645591b213SSam Leffler static void
20655591b213SSam Leffler ath_beacon_config(struct ath_softc *sc)
20665591b213SSam Leffler {
20678371372bSSam Leffler #define	TSF_TO_TU(_h,_l)	(((_h) << 22) | ((_l) >> 10))
20685591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
20695591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
20705591b213SSam Leffler 	struct ieee80211_node *ni = ic->ic_bss;
2071c42a7b7eSSam Leffler 	u_int32_t nexttbtt, intval;
20725591b213SSam Leffler 
20738371372bSSam Leffler 	/* extract tstamp from last beacon and convert to TU */
20748371372bSSam Leffler 	nexttbtt = TSF_TO_TU(LE_READ_4(ni->ni_tstamp.data + 4),
20758371372bSSam Leffler 			     LE_READ_4(ni->ni_tstamp.data));
20768371372bSSam Leffler 	/* NB: the beacon interval is kept internally in TU's */
20774bacf7c1SSam Leffler 	intval = ni->ni_intval & HAL_BEACON_PERIOD;
2078a6c992f4SSam Leffler 	if (nexttbtt == 0)		/* e.g. for ap mode */
2079a6c992f4SSam Leffler 		nexttbtt = intval;
2080a6c992f4SSam Leffler 	else if (intval)		/* NB: can be 0 for monitor mode */
2081a6c992f4SSam Leffler 		nexttbtt = roundup(nexttbtt, intval);
2082a6c992f4SSam Leffler 	DPRINTF(sc, ATH_DEBUG_BEACON, "%s: nexttbtt %u intval %u (%u)\n",
2083a6c992f4SSam Leffler 		__func__, nexttbtt, intval, ni->ni_intval);
20846b59f5e3SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA) {
20855591b213SSam Leffler 		HAL_BEACON_STATE bs;
20868371372bSSam Leffler 		u_int64_t tsf;
20878371372bSSam Leffler 		u_int32_t tsftu;
20888371372bSSam Leffler 		int dtimperiod, dtimcount;
20898371372bSSam Leffler 		int cfpperiod, cfpcount;
20905591b213SSam Leffler 
20918371372bSSam Leffler 		/*
20928371372bSSam Leffler 		 * Setup dtim and cfp parameters according to
20938371372bSSam Leffler 		 * last beacon we received (which may be none).
20948371372bSSam Leffler 		 */
20958371372bSSam Leffler 		dtimperiod = ni->ni_dtim_period;
20968371372bSSam Leffler 		if (dtimperiod <= 0)		/* NB: 0 if not known */
20978371372bSSam Leffler 			dtimperiod = 1;
20988371372bSSam Leffler 		dtimcount = ni->ni_dtim_count;
20998371372bSSam Leffler 		if (dtimcount >= dtimperiod)	/* NB: sanity check */
21008371372bSSam Leffler 			dtimcount = 0;		/* XXX? */
21018371372bSSam Leffler 		cfpperiod = 1;			/* NB: no PCF support yet */
21028371372bSSam Leffler 		cfpcount = 0;
21038371372bSSam Leffler #define	FUDGE	2
21048371372bSSam Leffler 		/*
21058371372bSSam Leffler 		 * Pull nexttbtt forward to reflect the current
21068371372bSSam Leffler 		 * TSF and calculate dtim+cfp state for the result.
21078371372bSSam Leffler 		 */
21088371372bSSam Leffler 		tsf = ath_hal_gettsf64(ah);
21098371372bSSam Leffler 		tsftu = TSF_TO_TU((u_int32_t)(tsf>>32), (u_int32_t)tsf) + FUDGE;
21108371372bSSam Leffler 		do {
21118371372bSSam Leffler 			nexttbtt += intval;
21128371372bSSam Leffler 			if (--dtimcount < 0) {
21138371372bSSam Leffler 				dtimcount = dtimperiod - 1;
21148371372bSSam Leffler 				if (--cfpcount < 0)
21158371372bSSam Leffler 					cfpcount = cfpperiod - 1;
21168371372bSSam Leffler 			}
21178371372bSSam Leffler 		} while (nexttbtt < tsftu);
21188371372bSSam Leffler #undef FUDGE
21195591b213SSam Leffler 		memset(&bs, 0, sizeof(bs));
2120a6c992f4SSam Leffler 		bs.bs_intval = intval;
21215591b213SSam Leffler 		bs.bs_nexttbtt = nexttbtt;
21228371372bSSam Leffler 		bs.bs_dtimperiod = dtimperiod*intval;
21238371372bSSam Leffler 		bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval;
21248371372bSSam Leffler 		bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod;
21258371372bSSam Leffler 		bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod;
21268371372bSSam Leffler 		bs.bs_cfpmaxduration = 0;
21278371372bSSam Leffler #if 0
21285591b213SSam Leffler 		/*
2129c42a7b7eSSam Leffler 		 * The 802.11 layer records the offset to the DTIM
2130c42a7b7eSSam Leffler 		 * bitmap while receiving beacons; use it here to
2131c42a7b7eSSam Leffler 		 * enable h/w detection of our AID being marked in
2132c42a7b7eSSam Leffler 		 * the bitmap vector (to indicate frames for us are
2133c42a7b7eSSam Leffler 		 * pending at the AP).
21348371372bSSam Leffler 		 * XXX do DTIM handling in s/w to WAR old h/w bugs
21358371372bSSam Leffler 		 * XXX enable based on h/w rev for newer chips
2136c42a7b7eSSam Leffler 		 */
2137c42a7b7eSSam Leffler 		bs.bs_timoffset = ni->ni_timoff;
21388371372bSSam Leffler #endif
2139c42a7b7eSSam Leffler 		/*
21405591b213SSam Leffler 		 * Calculate the number of consecutive beacons to miss
21415591b213SSam Leffler 		 * before taking a BMISS interrupt.  The configuration
21425591b213SSam Leffler 		 * is specified in ms, so we need to convert that to
21435591b213SSam Leffler 		 * TU's and then calculate based on the beacon interval.
21445591b213SSam Leffler 		 * Note that we clamp the result to at most 10 beacons.
21455591b213SSam Leffler 		 */
21464bacf7c1SSam Leffler 		bs.bs_bmissthreshold = howmany(ic->ic_bmisstimeout, intval);
21475591b213SSam Leffler 		if (bs.bs_bmissthreshold > 10)
21485591b213SSam Leffler 			bs.bs_bmissthreshold = 10;
21495591b213SSam Leffler 		else if (bs.bs_bmissthreshold <= 0)
21505591b213SSam Leffler 			bs.bs_bmissthreshold = 1;
21515591b213SSam Leffler 
21525591b213SSam Leffler 		/*
21535591b213SSam Leffler 		 * Calculate sleep duration.  The configuration is
21545591b213SSam Leffler 		 * given in ms.  We insure a multiple of the beacon
21555591b213SSam Leffler 		 * period is used.  Also, if the sleep duration is
21565591b213SSam Leffler 		 * greater than the DTIM period then it makes senses
21575591b213SSam Leffler 		 * to make it a multiple of that.
21585591b213SSam Leffler 		 *
21595591b213SSam Leffler 		 * XXX fixed at 100ms
21605591b213SSam Leffler 		 */
21614bacf7c1SSam Leffler 		bs.bs_sleepduration =
21624bacf7c1SSam Leffler 			roundup(IEEE80211_MS_TO_TU(100), bs.bs_intval);
21635591b213SSam Leffler 		if (bs.bs_sleepduration > bs.bs_dtimperiod)
21645591b213SSam Leffler 			bs.bs_sleepduration = roundup(bs.bs_sleepduration, bs.bs_dtimperiod);
21655591b213SSam Leffler 
2166c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
21678371372bSSam 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"
21685591b213SSam Leffler 			, __func__
21698371372bSSam Leffler 			, tsf, tsftu
21705591b213SSam Leffler 			, bs.bs_intval
21715591b213SSam Leffler 			, bs.bs_nexttbtt
21725591b213SSam Leffler 			, bs.bs_dtimperiod
21735591b213SSam Leffler 			, bs.bs_nextdtim
21745591b213SSam Leffler 			, bs.bs_bmissthreshold
21755591b213SSam Leffler 			, bs.bs_sleepduration
2176c42a7b7eSSam Leffler 			, bs.bs_cfpperiod
2177c42a7b7eSSam Leffler 			, bs.bs_cfpmaxduration
2178c42a7b7eSSam Leffler 			, bs.bs_cfpnext
2179c42a7b7eSSam Leffler 			, bs.bs_timoffset
2180c42a7b7eSSam Leffler 		);
21815591b213SSam Leffler 		ath_hal_intrset(ah, 0);
2182c42a7b7eSSam Leffler 		ath_hal_beacontimers(ah, &bs);
21835591b213SSam Leffler 		sc->sc_imask |= HAL_INT_BMISS;
21845591b213SSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
21855591b213SSam Leffler 	} else {
21865591b213SSam Leffler 		ath_hal_intrset(ah, 0);
2187a6c992f4SSam Leffler 		if (nexttbtt == intval)
2188c42a7b7eSSam Leffler 			intval |= HAL_BEACON_RESET_TSF;
2189c42a7b7eSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS) {
2190c42a7b7eSSam Leffler 			/*
2191c42a7b7eSSam Leffler 			 * In IBSS mode enable the beacon timers but only
2192c42a7b7eSSam Leffler 			 * enable SWBA interrupts if we need to manually
2193c42a7b7eSSam Leffler 			 * prepare beacon frames.  Otherwise we use a
2194c42a7b7eSSam Leffler 			 * self-linked tx descriptor and let the hardware
2195c42a7b7eSSam Leffler 			 * deal with things.
2196c42a7b7eSSam Leffler 			 */
2197c42a7b7eSSam Leffler 			intval |= HAL_BEACON_ENA;
2198c42a7b7eSSam Leffler 			if (!sc->sc_hasveol)
2199c42a7b7eSSam Leffler 				sc->sc_imask |= HAL_INT_SWBA;
22000f2e86fbSSam Leffler 			ath_beaconq_config(sc);
2201c42a7b7eSSam Leffler 		} else if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
2202c42a7b7eSSam Leffler 			/*
2203c42a7b7eSSam Leffler 			 * In AP mode we enable the beacon timers and
2204c42a7b7eSSam Leffler 			 * SWBA interrupts to prepare beacon frames.
2205c42a7b7eSSam Leffler 			 */
2206c42a7b7eSSam Leffler 			intval |= HAL_BEACON_ENA;
22075591b213SSam Leffler 			sc->sc_imask |= HAL_INT_SWBA;	/* beacon prepare */
22080f2e86fbSSam Leffler 			ath_beaconq_config(sc);
2209c42a7b7eSSam Leffler 		}
2210c42a7b7eSSam Leffler 		ath_hal_beaconinit(ah, nexttbtt, intval);
2211c42a7b7eSSam Leffler 		sc->sc_bmisscount = 0;
22125591b213SSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
2213c42a7b7eSSam Leffler 		/*
2214c42a7b7eSSam Leffler 		 * When using a self-linked beacon descriptor in
2215c42a7b7eSSam Leffler 		 * ibss mode load it once here.
2216c42a7b7eSSam Leffler 		 */
2217c42a7b7eSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_hasveol)
2218c42a7b7eSSam Leffler 			ath_beacon_proc(sc, 0);
22195591b213SSam Leffler 	}
22208371372bSSam Leffler #undef TSF_TO_TU
22215591b213SSam Leffler }
22225591b213SSam Leffler 
22235591b213SSam Leffler static void
22245591b213SSam Leffler ath_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
22255591b213SSam Leffler {
22265591b213SSam Leffler 	bus_addr_t *paddr = (bus_addr_t*) arg;
2227d77367bfSSam Leffler 	KASSERT(error == 0, ("error %u on bus_dma callback", error));
22285591b213SSam Leffler 	*paddr = segs->ds_addr;
22295591b213SSam Leffler }
22305591b213SSam Leffler 
22315591b213SSam Leffler static int
2232c42a7b7eSSam Leffler ath_descdma_setup(struct ath_softc *sc,
2233c42a7b7eSSam Leffler 	struct ath_descdma *dd, ath_bufhead *head,
2234c42a7b7eSSam Leffler 	const char *name, int nbuf, int ndesc)
2235c42a7b7eSSam Leffler {
2236c42a7b7eSSam Leffler #define	DS2PHYS(_dd, _ds) \
2237c42a7b7eSSam Leffler 	((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
2238fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
2239c42a7b7eSSam Leffler 	struct ath_desc *ds;
2240c42a7b7eSSam Leffler 	struct ath_buf *bf;
2241c42a7b7eSSam Leffler 	int i, bsize, error;
2242c42a7b7eSSam Leffler 
2243c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA: %u buffers %u desc/buf\n",
2244c42a7b7eSSam Leffler 	    __func__, name, nbuf, ndesc);
2245c42a7b7eSSam Leffler 
2246c42a7b7eSSam Leffler 	dd->dd_name = name;
2247c42a7b7eSSam Leffler 	dd->dd_desc_len = sizeof(struct ath_desc) * nbuf * ndesc;
2248c42a7b7eSSam Leffler 
2249c42a7b7eSSam Leffler 	/*
2250c42a7b7eSSam Leffler 	 * Setup DMA descriptor area.
2251c42a7b7eSSam Leffler 	 */
2252c42a7b7eSSam Leffler 	error = bus_dma_tag_create(NULL,	/* parent */
2253c42a7b7eSSam Leffler 		       PAGE_SIZE, 0,		/* alignment, bounds */
2254c42a7b7eSSam Leffler 		       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
2255c42a7b7eSSam Leffler 		       BUS_SPACE_MAXADDR,	/* highaddr */
2256c42a7b7eSSam Leffler 		       NULL, NULL,		/* filter, filterarg */
2257c42a7b7eSSam Leffler 		       dd->dd_desc_len,		/* maxsize */
2258c42a7b7eSSam Leffler 		       1,			/* nsegments */
2259c42a7b7eSSam Leffler 		       BUS_SPACE_MAXADDR,	/* maxsegsize */
2260c42a7b7eSSam Leffler 		       BUS_DMA_ALLOCNOW,	/* flags */
2261c42a7b7eSSam Leffler 		       NULL,			/* lockfunc */
2262c42a7b7eSSam Leffler 		       NULL,			/* lockarg */
2263c42a7b7eSSam Leffler 		       &dd->dd_dmat);
2264c42a7b7eSSam Leffler 	if (error != 0) {
2265c42a7b7eSSam Leffler 		if_printf(ifp, "cannot allocate %s DMA tag\n", dd->dd_name);
2266c42a7b7eSSam Leffler 		return error;
2267c42a7b7eSSam Leffler 	}
2268c42a7b7eSSam Leffler 
2269c42a7b7eSSam Leffler 	/* allocate descriptors */
2270c42a7b7eSSam Leffler 	error = bus_dmamap_create(dd->dd_dmat, BUS_DMA_NOWAIT, &dd->dd_dmamap);
2271c42a7b7eSSam Leffler 	if (error != 0) {
2272c42a7b7eSSam Leffler 		if_printf(ifp, "unable to create dmamap for %s descriptors, "
2273c42a7b7eSSam Leffler 			"error %u\n", dd->dd_name, error);
2274c42a7b7eSSam Leffler 		goto fail0;
2275c42a7b7eSSam Leffler 	}
2276c42a7b7eSSam Leffler 
2277c42a7b7eSSam Leffler 	error = bus_dmamem_alloc(dd->dd_dmat, (void**) &dd->dd_desc,
2278c42a7b7eSSam Leffler 				 BUS_DMA_NOWAIT, &dd->dd_dmamap);
2279c42a7b7eSSam Leffler 	if (error != 0) {
2280c42a7b7eSSam Leffler 		if_printf(ifp, "unable to alloc memory for %u %s descriptors, "
2281c42a7b7eSSam Leffler 			"error %u\n", nbuf * ndesc, dd->dd_name, error);
2282c42a7b7eSSam Leffler 		goto fail1;
2283c42a7b7eSSam Leffler 	}
2284c42a7b7eSSam Leffler 
2285c42a7b7eSSam Leffler 	error = bus_dmamap_load(dd->dd_dmat, dd->dd_dmamap,
2286c42a7b7eSSam Leffler 				dd->dd_desc, dd->dd_desc_len,
2287c42a7b7eSSam Leffler 				ath_load_cb, &dd->dd_desc_paddr,
2288c42a7b7eSSam Leffler 				BUS_DMA_NOWAIT);
2289c42a7b7eSSam Leffler 	if (error != 0) {
2290c42a7b7eSSam Leffler 		if_printf(ifp, "unable to map %s descriptors, error %u\n",
2291c42a7b7eSSam Leffler 			dd->dd_name, error);
2292c42a7b7eSSam Leffler 		goto fail2;
2293c42a7b7eSSam Leffler 	}
2294c42a7b7eSSam Leffler 
2295c42a7b7eSSam Leffler 	ds = dd->dd_desc;
2296c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA map: %p (%lu) -> %p (%lu)\n",
2297c42a7b7eSSam Leffler 	    __func__, dd->dd_name, ds, (u_long) dd->dd_desc_len,
2298c42a7b7eSSam Leffler 	    (caddr_t) dd->dd_desc_paddr, /*XXX*/ (u_long) dd->dd_desc_len);
2299c42a7b7eSSam Leffler 
2300c42a7b7eSSam Leffler 	/* allocate rx buffers */
2301c42a7b7eSSam Leffler 	bsize = sizeof(struct ath_buf) * nbuf;
2302c42a7b7eSSam Leffler 	bf = malloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO);
2303c42a7b7eSSam Leffler 	if (bf == NULL) {
2304c42a7b7eSSam Leffler 		if_printf(ifp, "malloc of %s buffers failed, size %u\n",
2305c42a7b7eSSam Leffler 			dd->dd_name, bsize);
2306c42a7b7eSSam Leffler 		goto fail3;
2307c42a7b7eSSam Leffler 	}
2308c42a7b7eSSam Leffler 	dd->dd_bufptr = bf;
2309c42a7b7eSSam Leffler 
2310c42a7b7eSSam Leffler 	STAILQ_INIT(head);
2311c42a7b7eSSam Leffler 	for (i = 0; i < nbuf; i++, bf++, ds += ndesc) {
2312c42a7b7eSSam Leffler 		bf->bf_desc = ds;
2313c42a7b7eSSam Leffler 		bf->bf_daddr = DS2PHYS(dd, ds);
2314c42a7b7eSSam Leffler 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
2315c42a7b7eSSam Leffler 				&bf->bf_dmamap);
2316c42a7b7eSSam Leffler 		if (error != 0) {
2317c42a7b7eSSam Leffler 			if_printf(ifp, "unable to create dmamap for %s "
2318c42a7b7eSSam Leffler 				"buffer %u, error %u\n", dd->dd_name, i, error);
2319c42a7b7eSSam Leffler 			ath_descdma_cleanup(sc, dd, head);
2320c42a7b7eSSam Leffler 			return error;
2321c42a7b7eSSam Leffler 		}
2322c42a7b7eSSam Leffler 		STAILQ_INSERT_TAIL(head, bf, bf_list);
2323c42a7b7eSSam Leffler 	}
2324c42a7b7eSSam Leffler 	return 0;
2325c42a7b7eSSam Leffler fail3:
2326c42a7b7eSSam Leffler 	bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
2327c42a7b7eSSam Leffler fail2:
2328c42a7b7eSSam Leffler 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
2329c42a7b7eSSam Leffler fail1:
2330c42a7b7eSSam Leffler 	bus_dmamap_destroy(dd->dd_dmat, dd->dd_dmamap);
2331c42a7b7eSSam Leffler fail0:
2332c42a7b7eSSam Leffler 	bus_dma_tag_destroy(dd->dd_dmat);
2333c42a7b7eSSam Leffler 	memset(dd, 0, sizeof(*dd));
2334c42a7b7eSSam Leffler 	return error;
2335c42a7b7eSSam Leffler #undef DS2PHYS
2336c42a7b7eSSam Leffler }
2337c42a7b7eSSam Leffler 
2338c42a7b7eSSam Leffler static void
2339c42a7b7eSSam Leffler ath_descdma_cleanup(struct ath_softc *sc,
2340c42a7b7eSSam Leffler 	struct ath_descdma *dd, ath_bufhead *head)
2341c42a7b7eSSam Leffler {
2342c42a7b7eSSam Leffler 	struct ath_buf *bf;
2343c42a7b7eSSam Leffler 	struct ieee80211_node *ni;
2344c42a7b7eSSam Leffler 
2345c42a7b7eSSam Leffler 	bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
2346c42a7b7eSSam Leffler 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
2347c42a7b7eSSam Leffler 	bus_dmamap_destroy(dd->dd_dmat, dd->dd_dmamap);
2348c42a7b7eSSam Leffler 	bus_dma_tag_destroy(dd->dd_dmat);
2349c42a7b7eSSam Leffler 
2350c42a7b7eSSam Leffler 	STAILQ_FOREACH(bf, head, bf_list) {
2351c42a7b7eSSam Leffler 		if (bf->bf_m) {
2352c42a7b7eSSam Leffler 			m_freem(bf->bf_m);
2353c42a7b7eSSam Leffler 			bf->bf_m = NULL;
2354c42a7b7eSSam Leffler 		}
2355c42a7b7eSSam Leffler 		if (bf->bf_dmamap != NULL) {
2356c42a7b7eSSam Leffler 			bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
2357c42a7b7eSSam Leffler 			bf->bf_dmamap = NULL;
2358c42a7b7eSSam Leffler 		}
2359c42a7b7eSSam Leffler 		ni = bf->bf_node;
2360c42a7b7eSSam Leffler 		bf->bf_node = NULL;
2361c42a7b7eSSam Leffler 		if (ni != NULL) {
2362c42a7b7eSSam Leffler 			/*
2363c42a7b7eSSam Leffler 			 * Reclaim node reference.
2364c42a7b7eSSam Leffler 			 */
2365c42a7b7eSSam Leffler 			ieee80211_free_node(ni);
2366c42a7b7eSSam Leffler 		}
2367c42a7b7eSSam Leffler 	}
2368c42a7b7eSSam Leffler 
2369c42a7b7eSSam Leffler 	STAILQ_INIT(head);
2370c42a7b7eSSam Leffler 	free(dd->dd_bufptr, M_ATHDEV);
2371c42a7b7eSSam Leffler 	memset(dd, 0, sizeof(*dd));
2372c42a7b7eSSam Leffler }
2373c42a7b7eSSam Leffler 
2374c42a7b7eSSam Leffler static int
23755591b213SSam Leffler ath_desc_alloc(struct ath_softc *sc)
23765591b213SSam Leffler {
2377c42a7b7eSSam Leffler 	int error;
23785591b213SSam Leffler 
2379c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_rxdma, &sc->sc_rxbuf,
2380c42a7b7eSSam Leffler 			"rx", ATH_RXBUF, 1);
23815591b213SSam Leffler 	if (error != 0)
23825591b213SSam Leffler 		return error;
23835591b213SSam Leffler 
2384c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_txdma, &sc->sc_txbuf,
2385c42a7b7eSSam Leffler 			"tx", ATH_TXBUF, ATH_TXDESC);
2386c42a7b7eSSam Leffler 	if (error != 0) {
2387c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
23885591b213SSam Leffler 		return error;
2389c42a7b7eSSam Leffler 	}
2390c42a7b7eSSam Leffler 
2391c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_bdma, &sc->sc_bbuf,
2392c42a7b7eSSam Leffler 			"beacon", 1, 1);
2393c42a7b7eSSam Leffler 	if (error != 0) {
2394c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
2395c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
2396c42a7b7eSSam Leffler 		return error;
2397c42a7b7eSSam Leffler 	}
23985591b213SSam Leffler 	return 0;
23995591b213SSam Leffler }
24005591b213SSam Leffler 
24015591b213SSam Leffler static void
24025591b213SSam Leffler ath_desc_free(struct ath_softc *sc)
24035591b213SSam Leffler {
24045591b213SSam Leffler 
2405c42a7b7eSSam Leffler 	if (sc->sc_bdma.dd_desc_len != 0)
2406c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_bdma, &sc->sc_bbuf);
2407c42a7b7eSSam Leffler 	if (sc->sc_txdma.dd_desc_len != 0)
2408c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
2409c42a7b7eSSam Leffler 	if (sc->sc_rxdma.dd_desc_len != 0)
2410c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
24115591b213SSam Leffler }
24125591b213SSam Leffler 
24135591b213SSam Leffler static struct ieee80211_node *
2414c42a7b7eSSam Leffler ath_node_alloc(struct ieee80211_node_table *nt)
24155591b213SSam Leffler {
2416c42a7b7eSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
2417c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
2418c42a7b7eSSam Leffler 	const size_t space = sizeof(struct ath_node) + sc->sc_rc->arc_space;
2419c42a7b7eSSam Leffler 	struct ath_node *an;
2420c42a7b7eSSam Leffler 
2421c42a7b7eSSam Leffler 	an = malloc(space, M_80211_NODE, M_NOWAIT|M_ZERO);
2422c42a7b7eSSam Leffler 	if (an == NULL) {
2423c42a7b7eSSam Leffler 		/* XXX stat+msg */
2424de5af704SSam Leffler 		return NULL;
24255591b213SSam Leffler 	}
2426c42a7b7eSSam Leffler 	an->an_avgrssi = ATH_RSSI_DUMMY_MARKER;
2427c42a7b7eSSam Leffler 	an->an_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
2428c42a7b7eSSam Leffler 	an->an_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
2429c42a7b7eSSam Leffler 	an->an_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
2430c42a7b7eSSam Leffler 	ath_rate_node_init(sc, an);
24315591b213SSam Leffler 
2432c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: an %p\n", __func__, an);
2433c42a7b7eSSam Leffler 	return &an->an_node;
2434c42a7b7eSSam Leffler }
2435c42a7b7eSSam Leffler 
24365591b213SSam Leffler static void
2437c42a7b7eSSam Leffler ath_node_free(struct ieee80211_node *ni)
24385591b213SSam Leffler {
2439c42a7b7eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2440c42a7b7eSSam Leffler         struct ath_softc *sc = ic->ic_ifp->if_softc;
24411e774079SSam Leffler 
2442c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: ni %p\n", __func__, ni);
2443c42a7b7eSSam Leffler 
2444c42a7b7eSSam Leffler 	ath_rate_node_cleanup(sc, ATH_NODE(ni));
2445c42a7b7eSSam Leffler 	sc->sc_node_free(ni);
24465591b213SSam Leffler }
24475591b213SSam Leffler 
2448de5af704SSam Leffler static u_int8_t
2449c42a7b7eSSam Leffler ath_node_getrssi(const struct ieee80211_node *ni)
2450de5af704SSam Leffler {
2451c42a7b7eSSam Leffler #define	HAL_EP_RND(x, mul) \
2452c42a7b7eSSam Leffler 	((((x)%(mul)) >= ((mul)/2)) ? ((x) + ((mul) - 1)) / (mul) : (x)/(mul))
2453c42a7b7eSSam Leffler 	u_int32_t avgrssi = ATH_NODE_CONST(ni)->an_avgrssi;
2454c42a7b7eSSam Leffler 	int32_t rssi;
2455de5af704SSam Leffler 
2456de5af704SSam Leffler 	/*
2457c42a7b7eSSam Leffler 	 * When only one frame is received there will be no state in
2458c42a7b7eSSam Leffler 	 * avgrssi so fallback on the value recorded by the 802.11 layer.
2459de5af704SSam Leffler 	 */
2460c42a7b7eSSam Leffler 	if (avgrssi != ATH_RSSI_DUMMY_MARKER)
2461c42a7b7eSSam Leffler 		rssi = HAL_EP_RND(avgrssi, HAL_RSSI_EP_MULTIPLIER);
2462de5af704SSam Leffler 	else
2463c42a7b7eSSam Leffler 		rssi = ni->ni_rssi;
2464c42a7b7eSSam Leffler 	/* NB: theoretically we shouldn't need this, but be paranoid */
2465c42a7b7eSSam Leffler 	return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
2466c42a7b7eSSam Leffler #undef HAL_EP_RND
2467de5af704SSam Leffler }
2468de5af704SSam Leffler 
24695591b213SSam Leffler static int
24705591b213SSam Leffler ath_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf)
24715591b213SSam Leffler {
24725591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
24735591b213SSam Leffler 	int error;
24745591b213SSam Leffler 	struct mbuf *m;
24755591b213SSam Leffler 	struct ath_desc *ds;
24765591b213SSam Leffler 
24775591b213SSam Leffler 	m = bf->bf_m;
24785591b213SSam Leffler 	if (m == NULL) {
24795591b213SSam Leffler 		/*
24805591b213SSam Leffler 		 * NB: by assigning a page to the rx dma buffer we
24815591b213SSam Leffler 		 * implicitly satisfy the Atheros requirement that
24825591b213SSam Leffler 		 * this buffer be cache-line-aligned and sized to be
24835591b213SSam Leffler 		 * multiple of the cache line size.  Not doing this
24845591b213SSam Leffler 		 * causes weird stuff to happen (for the 5210 at least).
24855591b213SSam Leffler 		 */
24865591b213SSam Leffler 		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
24875591b213SSam Leffler 		if (m == NULL) {
2488c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
2489c42a7b7eSSam Leffler 				"%s: no mbuf/cluster\n", __func__);
24905591b213SSam Leffler 			sc->sc_stats.ast_rx_nombuf++;
24915591b213SSam Leffler 			return ENOMEM;
24925591b213SSam Leffler 		}
24935591b213SSam Leffler 		bf->bf_m = m;
24945591b213SSam Leffler 		m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
24955591b213SSam Leffler 
2496f9e6219bSSam Leffler 		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat,
2497c42a7b7eSSam Leffler 					     bf->bf_dmamap, m,
2498f9e6219bSSam Leffler 					     bf->bf_segs, &bf->bf_nseg,
24995591b213SSam Leffler 					     BUS_DMA_NOWAIT);
25005591b213SSam Leffler 		if (error != 0) {
2501c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
2502f9e6219bSSam Leffler 			    "%s: bus_dmamap_load_mbuf_sg failed; error %d\n",
2503c42a7b7eSSam Leffler 			    __func__, error);
25045591b213SSam Leffler 			sc->sc_stats.ast_rx_busdma++;
25055591b213SSam Leffler 			return error;
25065591b213SSam Leffler 		}
2507d77367bfSSam Leffler 		KASSERT(bf->bf_nseg == 1,
2508d77367bfSSam Leffler 			("multi-segment packet; nseg %u", bf->bf_nseg));
25095591b213SSam Leffler 	}
25105591b213SSam Leffler 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREREAD);
25115591b213SSam Leffler 
251204e22a02SSam Leffler 	/*
251304e22a02SSam Leffler 	 * Setup descriptors.  For receive we always terminate
251404e22a02SSam Leffler 	 * the descriptor list with a self-linked entry so we'll
251504e22a02SSam Leffler 	 * not get overrun under high load (as can happen with a
2516c42a7b7eSSam Leffler 	 * 5212 when ANI processing enables PHY error frames).
251704e22a02SSam Leffler 	 *
251804e22a02SSam Leffler 	 * To insure the last descriptor is self-linked we create
251904e22a02SSam Leffler 	 * each descriptor as self-linked and add it to the end.  As
252004e22a02SSam Leffler 	 * each additional descriptor is added the previous self-linked
252104e22a02SSam Leffler 	 * entry is ``fixed'' naturally.  This should be safe even
252204e22a02SSam Leffler 	 * if DMA is happening.  When processing RX interrupts we
252304e22a02SSam Leffler 	 * never remove/process the last, self-linked, entry on the
252404e22a02SSam Leffler 	 * descriptor list.  This insures the hardware always has
252504e22a02SSam Leffler 	 * someplace to write a new frame.
252604e22a02SSam Leffler 	 */
25275591b213SSam Leffler 	ds = bf->bf_desc;
252804e22a02SSam Leffler 	ds->ds_link = bf->bf_daddr;	/* link to self */
25295591b213SSam Leffler 	ds->ds_data = bf->bf_segs[0].ds_addr;
25305591b213SSam Leffler 	ath_hal_setuprxdesc(ah, ds
25315591b213SSam Leffler 		, m->m_len		/* buffer size */
25325591b213SSam Leffler 		, 0
25335591b213SSam Leffler 	);
25345591b213SSam Leffler 
25355591b213SSam Leffler 	if (sc->sc_rxlink != NULL)
25365591b213SSam Leffler 		*sc->sc_rxlink = bf->bf_daddr;
25375591b213SSam Leffler 	sc->sc_rxlink = &ds->ds_link;
25385591b213SSam Leffler 	return 0;
25395591b213SSam Leffler }
25405591b213SSam Leffler 
2541c42a7b7eSSam Leffler /*
254203ed599aSSam Leffler  * Extend 15-bit time stamp from rx descriptor to
254303ed599aSSam Leffler  * a full 64-bit TSF using the current h/w TSF.
254403ed599aSSam Leffler  */
254503ed599aSSam Leffler static __inline u_int64_t
254603ed599aSSam Leffler ath_extend_tsf(struct ath_hal *ah, u_int32_t rstamp)
254703ed599aSSam Leffler {
254803ed599aSSam Leffler 	u_int64_t tsf;
254903ed599aSSam Leffler 
255003ed599aSSam Leffler 	tsf = ath_hal_gettsf64(ah);
255103ed599aSSam Leffler 	if ((tsf & 0x7fff) < rstamp)
255203ed599aSSam Leffler 		tsf -= 0x8000;
255303ed599aSSam Leffler 	return ((tsf &~ 0x7fff) | rstamp);
255403ed599aSSam Leffler }
255503ed599aSSam Leffler 
255603ed599aSSam Leffler /*
2557c42a7b7eSSam Leffler  * Intercept management frames to collect beacon rssi data
2558c42a7b7eSSam Leffler  * and to do ibss merges.
2559c42a7b7eSSam Leffler  */
2560c42a7b7eSSam Leffler static void
2561c42a7b7eSSam Leffler ath_recv_mgmt(struct ieee80211com *ic, struct mbuf *m,
2562c42a7b7eSSam Leffler 	struct ieee80211_node *ni,
2563c42a7b7eSSam Leffler 	int subtype, int rssi, u_int32_t rstamp)
2564c42a7b7eSSam Leffler {
2565c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
2566c42a7b7eSSam Leffler 
2567c42a7b7eSSam Leffler 	/*
2568c42a7b7eSSam Leffler 	 * Call up first so subsequent work can use information
2569c42a7b7eSSam Leffler 	 * potentially stored in the node (e.g. for ibss merge).
2570c42a7b7eSSam Leffler 	 */
2571c42a7b7eSSam Leffler 	sc->sc_recv_mgmt(ic, m, ni, subtype, rssi, rstamp);
2572c42a7b7eSSam Leffler 	switch (subtype) {
2573c42a7b7eSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON:
2574c42a7b7eSSam Leffler 		/* update rssi statistics for use by the hal */
2575c42a7b7eSSam Leffler 		ATH_RSSI_LPF(ATH_NODE(ni)->an_halstats.ns_avgbrssi, rssi);
2576c42a7b7eSSam Leffler 		/* fall thru... */
2577c42a7b7eSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
2578c42a7b7eSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS &&
2579c42a7b7eSSam Leffler 		    ic->ic_state == IEEE80211_S_RUN) {
258003ed599aSSam Leffler 			u_int64_t tsf = ath_extend_tsf(sc->sc_ah, rstamp);
2581c42a7b7eSSam Leffler 			/*
2582c42a7b7eSSam Leffler 			 * Handle ibss merge as needed; check the tsf on the
2583c42a7b7eSSam Leffler 			 * frame before attempting the merge.  The 802.11 spec
2584c42a7b7eSSam Leffler 			 * says the station should change it's bssid to match
2585c42a7b7eSSam Leffler 			 * the oldest station with the same ssid, where oldest
2586f818612bSSam Leffler 			 * is determined by the tsf.  Note that hardware
2587f818612bSSam Leffler 			 * reconfiguration happens through callback to
258803ed599aSSam Leffler 			 * ath_newstate as the state machine will go from
258903ed599aSSam Leffler 			 * RUN -> RUN when this happens.
2590c42a7b7eSSam Leffler 			 */
259103ed599aSSam Leffler 			if (le64toh(ni->ni_tstamp.tsf) >= tsf) {
259203ed599aSSam Leffler 				DPRINTF(sc, ATH_DEBUG_STATE,
259333d7d80cSTai-hwa Liang 				    "ibss merge, rstamp %u tsf %ju "
259433d7d80cSTai-hwa Liang 				    "tstamp %ju\n", rstamp, (uintmax_t)tsf,
259533d7d80cSTai-hwa Liang 				    (uintmax_t)ni->ni_tstamp.tsf);
2596641b4d0bSSam Leffler 				(void) ieee80211_ibss_merge(ni);
2597c42a7b7eSSam Leffler 			}
259803ed599aSSam Leffler 		}
2599c42a7b7eSSam Leffler 		break;
2600c42a7b7eSSam Leffler 	}
2601c42a7b7eSSam Leffler }
2602c42a7b7eSSam Leffler 
2603c42a7b7eSSam Leffler /*
2604c42a7b7eSSam Leffler  * Set the default antenna.
2605c42a7b7eSSam Leffler  */
2606c42a7b7eSSam Leffler static void
2607c42a7b7eSSam Leffler ath_setdefantenna(struct ath_softc *sc, u_int antenna)
2608c42a7b7eSSam Leffler {
2609c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2610c42a7b7eSSam Leffler 
2611c42a7b7eSSam Leffler 	/* XXX block beacon interrupts */
2612c42a7b7eSSam Leffler 	ath_hal_setdefantenna(ah, antenna);
2613c42a7b7eSSam Leffler 	if (sc->sc_defant != antenna)
2614c42a7b7eSSam Leffler 		sc->sc_stats.ast_ant_defswitch++;
2615c42a7b7eSSam Leffler 	sc->sc_defant = antenna;
2616c42a7b7eSSam Leffler 	sc->sc_rxotherant = 0;
2617c42a7b7eSSam Leffler }
2618c42a7b7eSSam Leffler 
26195591b213SSam Leffler static void
26205591b213SSam Leffler ath_rx_proc(void *arg, int npending)
26215591b213SSam Leffler {
26228cec0ab9SSam Leffler #define	PA2DESC(_sc, _pa) \
2623c42a7b7eSSam Leffler 	((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
2624c42a7b7eSSam Leffler 		((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
26255591b213SSam Leffler 	struct ath_softc *sc = arg;
26265591b213SSam Leffler 	struct ath_buf *bf;
2627d1d0cf62SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
2628fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
26295591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
26305591b213SSam Leffler 	struct ath_desc *ds;
26315591b213SSam Leffler 	struct mbuf *m;
26320a915fadSSam Leffler 	struct ieee80211_node *ni;
2633de5af704SSam Leffler 	struct ath_node *an;
263431640eb7SSam Leffler 	int len, type;
26355591b213SSam Leffler 	u_int phyerr;
26365591b213SSam Leffler 	HAL_STATUS status;
26375591b213SSam Leffler 
2638b5f4adb3SSam Leffler 	NET_LOCK_GIANT();		/* XXX */
2639b5f4adb3SSam Leffler 
2640c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RX_PROC, "%s: pending %u\n", __func__, npending);
26415591b213SSam Leffler 	do {
2642c42a7b7eSSam Leffler 		bf = STAILQ_FIRST(&sc->sc_rxbuf);
26435591b213SSam Leffler 		if (bf == NULL) {		/* NB: shouldn't happen */
2644c42a7b7eSSam Leffler 			if_printf(ifp, "%s: no buffer!\n", __func__);
26455591b213SSam Leffler 			break;
26465591b213SSam Leffler 		}
264704e22a02SSam Leffler 		ds = bf->bf_desc;
264804e22a02SSam Leffler 		if (ds->ds_link == bf->bf_daddr) {
264904e22a02SSam Leffler 			/* NB: never process the self-linked entry at the end */
265004e22a02SSam Leffler 			break;
265104e22a02SSam Leffler 		}
26525591b213SSam Leffler 		m = bf->bf_m;
26535591b213SSam Leffler 		if (m == NULL) {		/* NB: shouldn't happen */
2654c42a7b7eSSam Leffler 			if_printf(ifp, "%s: no mbuf!\n", __func__);
26555591b213SSam Leffler 			continue;
26565591b213SSam Leffler 		}
26578cec0ab9SSam Leffler 		/* XXX sync descriptor memory */
26588cec0ab9SSam Leffler 		/*
26598cec0ab9SSam Leffler 		 * Must provide the virtual address of the current
26608cec0ab9SSam Leffler 		 * descriptor, the physical address, and the virtual
26618cec0ab9SSam Leffler 		 * address of the next descriptor in the h/w chain.
26628cec0ab9SSam Leffler 		 * This allows the HAL to look ahead to see if the
26638cec0ab9SSam Leffler 		 * hardware is done with a descriptor by checking the
26648cec0ab9SSam Leffler 		 * done bit in the following descriptor and the address
26658cec0ab9SSam Leffler 		 * of the current descriptor the DMA engine is working
26668cec0ab9SSam Leffler 		 * on.  All this is necessary because of our use of
26678cec0ab9SSam Leffler 		 * a self-linked list to avoid rx overruns.
26688cec0ab9SSam Leffler 		 */
26698cec0ab9SSam Leffler 		status = ath_hal_rxprocdesc(ah, ds,
26708cec0ab9SSam Leffler 				bf->bf_daddr, PA2DESC(sc, ds->ds_link));
26715591b213SSam Leffler #ifdef AR_DEBUG
2672c42a7b7eSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_RECV_DESC)
26735591b213SSam Leffler 			ath_printrxbuf(bf, status == HAL_OK);
26745591b213SSam Leffler #endif
26755591b213SSam Leffler 		if (status == HAL_EINPROGRESS)
26765591b213SSam Leffler 			break;
2677c42a7b7eSSam Leffler 		STAILQ_REMOVE_HEAD(&sc->sc_rxbuf, bf_list);
2678c42a7b7eSSam Leffler 		if (ds->ds_rxstat.rs_more) {
2679c42a7b7eSSam Leffler 			/*
2680c42a7b7eSSam Leffler 			 * Frame spans multiple descriptors; this
2681c42a7b7eSSam Leffler 			 * cannot happen yet as we don't support
2682c42a7b7eSSam Leffler 			 * jumbograms.  If not in monitor mode,
2683c42a7b7eSSam Leffler 			 * discard the frame.
2684c42a7b7eSSam Leffler 			 */
2685c42a7b7eSSam Leffler 			if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2686c42a7b7eSSam Leffler 				sc->sc_stats.ast_rx_toobig++;
2687c42a7b7eSSam Leffler 				goto rx_next;
2688c42a7b7eSSam Leffler 			}
2689c42a7b7eSSam Leffler 			/* fall thru for monitor mode handling... */
2690c42a7b7eSSam Leffler 		} else if (ds->ds_rxstat.rs_status != 0) {
26915591b213SSam Leffler 			if (ds->ds_rxstat.rs_status & HAL_RXERR_CRC)
26925591b213SSam Leffler 				sc->sc_stats.ast_rx_crcerr++;
26935591b213SSam Leffler 			if (ds->ds_rxstat.rs_status & HAL_RXERR_FIFO)
26945591b213SSam Leffler 				sc->sc_stats.ast_rx_fifoerr++;
26955591b213SSam Leffler 			if (ds->ds_rxstat.rs_status & HAL_RXERR_PHY) {
26965591b213SSam Leffler 				sc->sc_stats.ast_rx_phyerr++;
26975591b213SSam Leffler 				phyerr = ds->ds_rxstat.rs_phyerr & 0x1f;
26985591b213SSam Leffler 				sc->sc_stats.ast_rx_phy[phyerr]++;
2699c42a7b7eSSam Leffler 				goto rx_next;
2700c42a7b7eSSam Leffler 			}
2701c42a7b7eSSam Leffler 			if (ds->ds_rxstat.rs_status & HAL_RXERR_DECRYPT) {
270285643802SSam Leffler 				/*
2703c42a7b7eSSam Leffler 				 * Decrypt error.  If the error occurred
2704c42a7b7eSSam Leffler 				 * because there was no hardware key, then
2705c42a7b7eSSam Leffler 				 * let the frame through so the upper layers
2706c42a7b7eSSam Leffler 				 * can process it.  This is necessary for 5210
2707c42a7b7eSSam Leffler 				 * parts which have no way to setup a ``clear''
2708c42a7b7eSSam Leffler 				 * key cache entry.
2709c42a7b7eSSam Leffler 				 *
2710c42a7b7eSSam Leffler 				 * XXX do key cache faulting
271185643802SSam Leffler 				 */
2712c42a7b7eSSam Leffler 				if (ds->ds_rxstat.rs_keyix == HAL_RXKEYIX_INVALID)
2713c42a7b7eSSam Leffler 					goto rx_accept;
2714c42a7b7eSSam Leffler 				sc->sc_stats.ast_rx_badcrypt++;
27155591b213SSam Leffler 			}
2716c42a7b7eSSam Leffler 			if (ds->ds_rxstat.rs_status & HAL_RXERR_MIC) {
2717c42a7b7eSSam Leffler 				sc->sc_stats.ast_rx_badmic++;
2718c42a7b7eSSam Leffler 				/*
2719c42a7b7eSSam Leffler 				 * Do minimal work required to hand off
2720c42a7b7eSSam Leffler 				 * the 802.11 header for notifcation.
2721c42a7b7eSSam Leffler 				 */
2722c42a7b7eSSam Leffler 				/* XXX frag's and qos frames */
27235591b213SSam Leffler 				len = ds->ds_rxstat.rs_datalen;
2724c42a7b7eSSam Leffler 				if (len >= sizeof (struct ieee80211_frame)) {
2725c42a7b7eSSam Leffler 					bus_dmamap_sync(sc->sc_dmat,
2726c42a7b7eSSam Leffler 					    bf->bf_dmamap,
2727c42a7b7eSSam Leffler 					    BUS_DMASYNC_POSTREAD);
2728c42a7b7eSSam Leffler 					ieee80211_notify_michael_failure(ic,
2729c42a7b7eSSam Leffler 					    mtod(m, struct ieee80211_frame *),
27300ab4040aSSam Leffler 					    sc->sc_splitmic ?
27310ab4040aSSam Leffler 					        ds->ds_rxstat.rs_keyix-32 :
27320ab4040aSSam Leffler 					        ds->ds_rxstat.rs_keyix
27330ab4040aSSam Leffler 					);
2734c42a7b7eSSam Leffler 				}
2735c42a7b7eSSam Leffler 			}
2736c42a7b7eSSam Leffler 			ifp->if_ierrors++;
2737c42a7b7eSSam Leffler 			/*
2738c42a7b7eSSam Leffler 			 * Reject error frames, we normally don't want
2739c42a7b7eSSam Leffler 			 * to see them in monitor mode (in monitor mode
2740c42a7b7eSSam Leffler 			 * allow through packets that have crypto problems).
2741c42a7b7eSSam Leffler 			 */
2742c42a7b7eSSam Leffler 			if ((ds->ds_rxstat.rs_status &~
2743c42a7b7eSSam Leffler 				(HAL_RXERR_DECRYPT|HAL_RXERR_MIC)) ||
2744c42a7b7eSSam Leffler 			    sc->sc_ic.ic_opmode != IEEE80211_M_MONITOR)
27455591b213SSam Leffler 				goto rx_next;
27465591b213SSam Leffler 		}
2747c42a7b7eSSam Leffler rx_accept:
2748c42a7b7eSSam Leffler 		/*
2749c42a7b7eSSam Leffler 		 * Sync and unmap the frame.  At this point we're
2750c42a7b7eSSam Leffler 		 * committed to passing the mbuf somewhere so clear
2751c42a7b7eSSam Leffler 		 * bf_m; this means a new sk_buff must be allocated
2752c42a7b7eSSam Leffler 		 * when the rx descriptor is setup again to receive
2753c42a7b7eSSam Leffler 		 * another frame.
2754c42a7b7eSSam Leffler 		 */
27555591b213SSam Leffler 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
27565591b213SSam Leffler 		    BUS_DMASYNC_POSTREAD);
27575591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
27585591b213SSam Leffler 		bf->bf_m = NULL;
2759c42a7b7eSSam Leffler 
27605591b213SSam Leffler 		m->m_pkthdr.rcvif = ifp;
2761c42a7b7eSSam Leffler 		len = ds->ds_rxstat.rs_datalen;
27625591b213SSam Leffler 		m->m_pkthdr.len = m->m_len = len;
276373454c73SSam Leffler 
2764c42a7b7eSSam Leffler 		sc->sc_stats.ast_ant_rx[ds->ds_rxstat.rs_antenna]++;
2765c42a7b7eSSam Leffler 
276673454c73SSam Leffler 		if (sc->sc_drvbpf) {
276716b4851aSSam Leffler 			u_int8_t rix;
276816b4851aSSam Leffler 
2769c42a7b7eSSam Leffler 			/*
2770c42a7b7eSSam Leffler 			 * Discard anything shorter than an ack or cts.
2771c42a7b7eSSam Leffler 			 */
2772c42a7b7eSSam Leffler 			if (len < IEEE80211_ACK_LEN) {
2773c42a7b7eSSam Leffler 				DPRINTF(sc, ATH_DEBUG_RECV,
2774c42a7b7eSSam Leffler 					"%s: runt packet %d\n",
2775c42a7b7eSSam Leffler 					__func__, len);
2776c42a7b7eSSam Leffler 				sc->sc_stats.ast_rx_tooshort++;
2777c42a7b7eSSam Leffler 				m_freem(m);
2778c42a7b7eSSam Leffler 				goto rx_next;
2779c42a7b7eSSam Leffler 			}
278016b4851aSSam Leffler 			rix = ds->ds_rxstat.rs_rate;
2781d3be6f5bSSam Leffler 			sc->sc_rx_th.wr_flags = sc->sc_hwmap[rix].rxflags;
27823e50ec2cSSam Leffler 			sc->sc_rx_th.wr_rate = sc->sc_hwmap[rix].ieeerate;
2783437ffe18SSam Leffler 			sc->sc_rx_th.wr_antsignal = ds->ds_rxstat.rs_rssi;
2784437ffe18SSam Leffler 			sc->sc_rx_th.wr_antenna = ds->ds_rxstat.rs_antenna;
278573454c73SSam Leffler 			/* XXX TSF */
278673454c73SSam Leffler 
2787437ffe18SSam Leffler 			bpf_mtap2(sc->sc_drvbpf,
2788d3be6f5bSSam Leffler 				&sc->sc_rx_th, sc->sc_rx_th_len, m);
27895591b213SSam Leffler 		}
27900a915fadSSam Leffler 
27915591b213SSam Leffler 		/*
2792c42a7b7eSSam Leffler 		 * From this point on we assume the frame is at least
2793c42a7b7eSSam Leffler 		 * as large as ieee80211_frame_min; verify that.
27945591b213SSam Leffler 		 */
2795c42a7b7eSSam Leffler 		if (len < IEEE80211_MIN_LEN) {
2796c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_RECV, "%s: short packet %d\n",
2797c42a7b7eSSam Leffler 				__func__, len);
2798c42a7b7eSSam Leffler 			sc->sc_stats.ast_rx_tooshort++;
2799c42a7b7eSSam Leffler 			m_freem(m);
2800c42a7b7eSSam Leffler 			goto rx_next;
28015591b213SSam Leffler 		}
28020a915fadSSam Leffler 
2803c42a7b7eSSam Leffler 		if (IFF_DUMPPKTS(sc, ATH_DEBUG_RECV)) {
2804c42a7b7eSSam Leffler 			ieee80211_dump_pkt(mtod(m, caddr_t), len,
28053e50ec2cSSam Leffler 				   sc->sc_hwmap[ds->ds_rxstat.rs_rate].ieeerate,
2806c42a7b7eSSam Leffler 				   ds->ds_rxstat.rs_rssi);
2807c42a7b7eSSam Leffler 		}
2808c42a7b7eSSam Leffler 
2809c42a7b7eSSam Leffler 		m_adj(m, -IEEE80211_CRC_LEN);
2810de5af704SSam Leffler 
2811de5af704SSam Leffler 		/*
2812c42a7b7eSSam Leffler 		 * Locate the node for sender, track state, and then
2813c42a7b7eSSam Leffler 		 * pass the (referenced) node up to the 802.11 layer
2814c42a7b7eSSam Leffler 		 * for its use.
2815c42a7b7eSSam Leffler 		 */
2816c1225b52SSam Leffler 		ni = ieee80211_find_rxnode_withkey(ic,
2817c1225b52SSam Leffler 			mtod(m, const struct ieee80211_frame_min *),
2818c1225b52SSam Leffler 			ds->ds_rxstat.rs_keyix == HAL_RXKEYIX_INVALID ?
2819c1225b52SSam Leffler 				IEEE80211_KEYIX_NONE : ds->ds_rxstat.rs_keyix);
2820c42a7b7eSSam Leffler 		/*
2821c42a7b7eSSam Leffler 		 * Track rx rssi and do any rx antenna management.
2822de5af704SSam Leffler 		 */
2823de5af704SSam Leffler 		an = ATH_NODE(ni);
2824c42a7b7eSSam Leffler 		ATH_RSSI_LPF(an->an_avgrssi, ds->ds_rxstat.rs_rssi);
2825e8fd88a3SSam Leffler 		/*
2826e8fd88a3SSam Leffler 		 * Send frame up for processing.
2827e8fd88a3SSam Leffler 		 */
2828e8fd88a3SSam Leffler 		type = ieee80211_input(ic, m, ni,
2829e8fd88a3SSam Leffler 			ds->ds_rxstat.rs_rssi, ds->ds_rxstat.rs_tstamp);
2830e8fd88a3SSam Leffler 		ieee80211_free_node(ni);
2831c42a7b7eSSam Leffler 		if (sc->sc_diversity) {
2832c42a7b7eSSam Leffler 			/*
2833c42a7b7eSSam Leffler 			 * When using fast diversity, change the default rx
2834c42a7b7eSSam Leffler 			 * antenna if diversity chooses the other antenna 3
2835c42a7b7eSSam Leffler 			 * times in a row.
2836c42a7b7eSSam Leffler 			 */
2837c42a7b7eSSam Leffler 			if (sc->sc_defant != ds->ds_rxstat.rs_antenna) {
2838c42a7b7eSSam Leffler 				if (++sc->sc_rxotherant >= 3)
2839c42a7b7eSSam Leffler 					ath_setdefantenna(sc,
2840c42a7b7eSSam Leffler 						ds->ds_rxstat.rs_antenna);
2841c42a7b7eSSam Leffler 			} else
2842c42a7b7eSSam Leffler 				sc->sc_rxotherant = 0;
2843c42a7b7eSSam Leffler 		}
28443e50ec2cSSam Leffler 		if (sc->sc_softled) {
28453e50ec2cSSam Leffler 			/*
28463e50ec2cSSam Leffler 			 * Blink for any data frame.  Otherwise do a
28473e50ec2cSSam Leffler 			 * heartbeat-style blink when idle.  The latter
28483e50ec2cSSam Leffler 			 * is mainly for station mode where we depend on
28493e50ec2cSSam Leffler 			 * periodic beacon frames to trigger the poll event.
28503e50ec2cSSam Leffler 			 */
285131640eb7SSam Leffler 			if (type == IEEE80211_FC0_TYPE_DATA) {
28523e50ec2cSSam Leffler 				sc->sc_rxrate = ds->ds_rxstat.rs_rate;
28533e50ec2cSSam Leffler 				ath_led_event(sc, ATH_LED_RX);
28543e50ec2cSSam Leffler 			} else if (ticks - sc->sc_ledevent >= sc->sc_ledidle)
28553e50ec2cSSam Leffler 				ath_led_event(sc, ATH_LED_POLL);
28563e50ec2cSSam Leffler 		}
28575591b213SSam Leffler rx_next:
2858c42a7b7eSSam Leffler 		STAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
28595591b213SSam Leffler 	} while (ath_rxbuf_init(sc, bf) == 0);
28605591b213SSam Leffler 
2861c42a7b7eSSam Leffler 	/* rx signal state monitoring */
2862c42a7b7eSSam Leffler 	ath_hal_rxmonitor(ah, &ATH_NODE(ic->ic_bss)->an_halstats);
2863b5f4adb3SSam Leffler 
2864b5f4adb3SSam Leffler 	NET_UNLOCK_GIANT();		/* XXX */
28658cec0ab9SSam Leffler #undef PA2DESC
28665591b213SSam Leffler }
28675591b213SSam Leffler 
28685591b213SSam Leffler /*
2869c42a7b7eSSam Leffler  * Setup a h/w transmit queue.
28705591b213SSam Leffler  */
2871c42a7b7eSSam Leffler static struct ath_txq *
2872c42a7b7eSSam Leffler ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
2873c42a7b7eSSam Leffler {
2874c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
2875c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2876c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
2877c42a7b7eSSam Leffler 	int qnum;
2878c42a7b7eSSam Leffler 
2879c42a7b7eSSam Leffler 	memset(&qi, 0, sizeof(qi));
2880c42a7b7eSSam Leffler 	qi.tqi_subtype = subtype;
2881c42a7b7eSSam Leffler 	qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
2882c42a7b7eSSam Leffler 	qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
2883c42a7b7eSSam Leffler 	qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
2884c42a7b7eSSam Leffler 	/*
2885c42a7b7eSSam Leffler 	 * Enable interrupts only for EOL and DESC conditions.
2886c42a7b7eSSam Leffler 	 * We mark tx descriptors to receive a DESC interrupt
2887c42a7b7eSSam Leffler 	 * when a tx queue gets deep; otherwise waiting for the
2888c42a7b7eSSam Leffler 	 * EOL to reap descriptors.  Note that this is done to
2889c42a7b7eSSam Leffler 	 * reduce interrupt load and this only defers reaping
2890c42a7b7eSSam Leffler 	 * descriptors, never transmitting frames.  Aside from
2891c42a7b7eSSam Leffler 	 * reducing interrupts this also permits more concurrency.
2892c42a7b7eSSam Leffler 	 * The only potential downside is if the tx queue backs
2893c42a7b7eSSam Leffler 	 * up in which case the top half of the kernel may backup
2894c42a7b7eSSam Leffler 	 * due to a lack of tx descriptors.
2895c42a7b7eSSam Leffler 	 */
2896c42a7b7eSSam Leffler 	qi.tqi_qflags = TXQ_FLAG_TXEOLINT_ENABLE | TXQ_FLAG_TXDESCINT_ENABLE;
2897c42a7b7eSSam Leffler 	qnum = ath_hal_setuptxqueue(ah, qtype, &qi);
2898c42a7b7eSSam Leffler 	if (qnum == -1) {
2899c42a7b7eSSam Leffler 		/*
2900c42a7b7eSSam Leffler 		 * NB: don't print a message, this happens
2901a614e076SSam Leffler 		 * normally on parts with too few tx queues
2902c42a7b7eSSam Leffler 		 */
2903c42a7b7eSSam Leffler 		return NULL;
2904c42a7b7eSSam Leffler 	}
2905c42a7b7eSSam Leffler 	if (qnum >= N(sc->sc_txq)) {
29066891c875SPeter Wemm 		device_printf(sc->sc_dev,
29076891c875SPeter Wemm 			"hal qnum %u out of range, max %zu!\n",
2908c42a7b7eSSam Leffler 			qnum, N(sc->sc_txq));
2909c42a7b7eSSam Leffler 		ath_hal_releasetxqueue(ah, qnum);
2910c42a7b7eSSam Leffler 		return NULL;
2911c42a7b7eSSam Leffler 	}
2912c42a7b7eSSam Leffler 	if (!ATH_TXQ_SETUP(sc, qnum)) {
2913c42a7b7eSSam Leffler 		struct ath_txq *txq = &sc->sc_txq[qnum];
2914c42a7b7eSSam Leffler 
2915c42a7b7eSSam Leffler 		txq->axq_qnum = qnum;
2916c42a7b7eSSam Leffler 		txq->axq_depth = 0;
2917c42a7b7eSSam Leffler 		txq->axq_intrcnt = 0;
2918c42a7b7eSSam Leffler 		txq->axq_link = NULL;
2919c42a7b7eSSam Leffler 		STAILQ_INIT(&txq->axq_q);
2920c42a7b7eSSam Leffler 		ATH_TXQ_LOCK_INIT(sc, txq);
2921c42a7b7eSSam Leffler 		sc->sc_txqsetup |= 1<<qnum;
2922c42a7b7eSSam Leffler 	}
2923c42a7b7eSSam Leffler 	return &sc->sc_txq[qnum];
2924c42a7b7eSSam Leffler #undef N
2925c42a7b7eSSam Leffler }
2926c42a7b7eSSam Leffler 
2927c42a7b7eSSam Leffler /*
2928c42a7b7eSSam Leffler  * Setup a hardware data transmit queue for the specified
2929c42a7b7eSSam Leffler  * access control.  The hal may not support all requested
2930c42a7b7eSSam Leffler  * queues in which case it will return a reference to a
2931c42a7b7eSSam Leffler  * previously setup queue.  We record the mapping from ac's
2932c42a7b7eSSam Leffler  * to h/w queues for use by ath_tx_start and also track
2933c42a7b7eSSam Leffler  * the set of h/w queues being used to optimize work in the
2934c42a7b7eSSam Leffler  * transmit interrupt handler and related routines.
2935c42a7b7eSSam Leffler  */
2936c42a7b7eSSam Leffler static int
2937c42a7b7eSSam Leffler ath_tx_setup(struct ath_softc *sc, int ac, int haltype)
2938c42a7b7eSSam Leffler {
2939c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
2940c42a7b7eSSam Leffler 	struct ath_txq *txq;
2941c42a7b7eSSam Leffler 
2942c42a7b7eSSam Leffler 	if (ac >= N(sc->sc_ac2q)) {
29436891c875SPeter Wemm 		device_printf(sc->sc_dev, "AC %u out of range, max %zu!\n",
2944c42a7b7eSSam Leffler 			ac, N(sc->sc_ac2q));
2945c42a7b7eSSam Leffler 		return 0;
2946c42a7b7eSSam Leffler 	}
2947c42a7b7eSSam Leffler 	txq = ath_txq_setup(sc, HAL_TX_QUEUE_DATA, haltype);
2948c42a7b7eSSam Leffler 	if (txq != NULL) {
2949c42a7b7eSSam Leffler 		sc->sc_ac2q[ac] = txq;
2950c42a7b7eSSam Leffler 		return 1;
2951c42a7b7eSSam Leffler 	} else
2952c42a7b7eSSam Leffler 		return 0;
2953c42a7b7eSSam Leffler #undef N
2954c42a7b7eSSam Leffler }
2955c42a7b7eSSam Leffler 
2956c42a7b7eSSam Leffler /*
2957c42a7b7eSSam Leffler  * Update WME parameters for a transmit queue.
2958c42a7b7eSSam Leffler  */
2959c42a7b7eSSam Leffler static int
2960c42a7b7eSSam Leffler ath_txq_update(struct ath_softc *sc, int ac)
2961c42a7b7eSSam Leffler {
2962c42a7b7eSSam Leffler #define	ATH_EXPONENT_TO_VALUE(v)	((1<<v)-1)
2963c42a7b7eSSam Leffler #define	ATH_TXOP_TO_US(v)		(v<<5)
2964c42a7b7eSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
2965c42a7b7eSSam Leffler 	struct ath_txq *txq = sc->sc_ac2q[ac];
2966c42a7b7eSSam Leffler 	struct wmeParams *wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
2967c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2968c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
2969c42a7b7eSSam Leffler 
2970c42a7b7eSSam Leffler 	ath_hal_gettxqueueprops(ah, txq->axq_qnum, &qi);
2971c42a7b7eSSam Leffler 	qi.tqi_aifs = wmep->wmep_aifsn;
2972c42a7b7eSSam Leffler 	qi.tqi_cwmin = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
2973c42a7b7eSSam Leffler 	qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
2974c42a7b7eSSam Leffler 	qi.tqi_burstTime = ATH_TXOP_TO_US(wmep->wmep_txopLimit);
2975c42a7b7eSSam Leffler 
2976c42a7b7eSSam Leffler 	if (!ath_hal_settxqueueprops(ah, txq->axq_qnum, &qi)) {
2977c42a7b7eSSam Leffler 		device_printf(sc->sc_dev, "unable to update hardware queue "
2978c42a7b7eSSam Leffler 			"parameters for %s traffic!\n",
2979c42a7b7eSSam Leffler 			ieee80211_wme_acnames[ac]);
2980c42a7b7eSSam Leffler 		return 0;
2981c42a7b7eSSam Leffler 	} else {
2982c42a7b7eSSam Leffler 		ath_hal_resettxqueue(ah, txq->axq_qnum); /* push to h/w */
2983c42a7b7eSSam Leffler 		return 1;
2984c42a7b7eSSam Leffler 	}
2985c42a7b7eSSam Leffler #undef ATH_TXOP_TO_US
2986c42a7b7eSSam Leffler #undef ATH_EXPONENT_TO_VALUE
2987c42a7b7eSSam Leffler }
2988c42a7b7eSSam Leffler 
2989c42a7b7eSSam Leffler /*
2990c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update WME parameters.
2991c42a7b7eSSam Leffler  */
2992c42a7b7eSSam Leffler static int
2993c42a7b7eSSam Leffler ath_wme_update(struct ieee80211com *ic)
2994c42a7b7eSSam Leffler {
2995c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
2996c42a7b7eSSam Leffler 
2997c42a7b7eSSam Leffler 	return !ath_txq_update(sc, WME_AC_BE) ||
2998c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_BK) ||
2999c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VI) ||
3000c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VO) ? EIO : 0;
3001c42a7b7eSSam Leffler }
3002c42a7b7eSSam Leffler 
3003c42a7b7eSSam Leffler /*
3004c42a7b7eSSam Leffler  * Reclaim resources for a setup queue.
3005c42a7b7eSSam Leffler  */
3006c42a7b7eSSam Leffler static void
3007c42a7b7eSSam Leffler ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
3008c42a7b7eSSam Leffler {
3009c42a7b7eSSam Leffler 
3010c42a7b7eSSam Leffler 	ath_hal_releasetxqueue(sc->sc_ah, txq->axq_qnum);
3011c42a7b7eSSam Leffler 	ATH_TXQ_LOCK_DESTROY(txq);
3012c42a7b7eSSam Leffler 	sc->sc_txqsetup &= ~(1<<txq->axq_qnum);
3013c42a7b7eSSam Leffler }
3014c42a7b7eSSam Leffler 
3015c42a7b7eSSam Leffler /*
3016c42a7b7eSSam Leffler  * Reclaim all tx queue resources.
3017c42a7b7eSSam Leffler  */
3018c42a7b7eSSam Leffler static void
3019c42a7b7eSSam Leffler ath_tx_cleanup(struct ath_softc *sc)
3020c42a7b7eSSam Leffler {
3021c42a7b7eSSam Leffler 	int i;
3022c42a7b7eSSam Leffler 
3023c42a7b7eSSam Leffler 	ATH_TXBUF_LOCK_DESTROY(sc);
3024c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
3025c42a7b7eSSam Leffler 		if (ATH_TXQ_SETUP(sc, i))
3026c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, &sc->sc_txq[i]);
3027c42a7b7eSSam Leffler }
30285591b213SSam Leffler 
302999d258fdSSam Leffler /*
303099d258fdSSam Leffler  * Defragment an mbuf chain, returning at most maxfrags separate
303199d258fdSSam Leffler  * mbufs+clusters.  If this is not possible NULL is returned and
3032a7073e8bSSam Leffler  * the original mbuf chain is left in it's present (potentially
3033a7073e8bSSam Leffler  * modified) state.  We use two techniques: collapsing consecutive
3034a7073e8bSSam Leffler  * mbufs and replacing consecutive mbufs by a cluster.
303599d258fdSSam Leffler  */
303699d258fdSSam Leffler static struct mbuf *
303799d258fdSSam Leffler ath_defrag(struct mbuf *m0, int how, int maxfrags)
303899d258fdSSam Leffler {
303999d258fdSSam Leffler 	struct mbuf *m, *n, *n2, **prev;
304099d258fdSSam Leffler 	u_int curfrags;
304199d258fdSSam Leffler 
304299d258fdSSam Leffler 	/*
304399d258fdSSam Leffler 	 * Calculate the current number of frags.
304499d258fdSSam Leffler 	 */
304599d258fdSSam Leffler 	curfrags = 0;
304699d258fdSSam Leffler 	for (m = m0; m != NULL; m = m->m_next)
304799d258fdSSam Leffler 		curfrags++;
304899d258fdSSam Leffler 	/*
304999d258fdSSam Leffler 	 * First, try to collapse mbufs.  Note that we always collapse
305099d258fdSSam Leffler 	 * towards the front so we don't need to deal with moving the
305199d258fdSSam Leffler 	 * pkthdr.  This may be suboptimal if the first mbuf has much
305299d258fdSSam Leffler 	 * less data than the following.
305399d258fdSSam Leffler 	 */
305499d258fdSSam Leffler 	m = m0;
305599d258fdSSam Leffler again:
305699d258fdSSam Leffler 	for (;;) {
305799d258fdSSam Leffler 		n = m->m_next;
305899d258fdSSam Leffler 		if (n == NULL)
305999d258fdSSam Leffler 			break;
3060019b9669SSam Leffler 		if ((m->m_flags & M_RDONLY) == 0 &&
3061019b9669SSam Leffler 		    n->m_len < M_TRAILINGSPACE(m)) {
306299d258fdSSam Leffler 			bcopy(mtod(n, void *), mtod(m, char *) + m->m_len,
306399d258fdSSam Leffler 				n->m_len);
306499d258fdSSam Leffler 			m->m_len += n->m_len;
306599d258fdSSam Leffler 			m->m_next = n->m_next;
306699d258fdSSam Leffler 			m_free(n);
306799d258fdSSam Leffler 			if (--curfrags <= maxfrags)
306899d258fdSSam Leffler 				return m0;
306999d258fdSSam Leffler 		} else
307099d258fdSSam Leffler 			m = n;
307199d258fdSSam Leffler 	}
307299d258fdSSam Leffler 	KASSERT(maxfrags > 1,
307399d258fdSSam Leffler 		("maxfrags %u, but normal collapse failed", maxfrags));
307499d258fdSSam Leffler 	/*
307599d258fdSSam Leffler 	 * Collapse consecutive mbufs to a cluster.
307699d258fdSSam Leffler 	 */
307799d258fdSSam Leffler 	prev = &m0->m_next;		/* NB: not the first mbuf */
307899d258fdSSam Leffler 	while ((n = *prev) != NULL) {
307999d258fdSSam Leffler 		if ((n2 = n->m_next) != NULL &&
308099d258fdSSam Leffler 		    n->m_len + n2->m_len < MCLBYTES) {
308199d258fdSSam Leffler 			m = m_getcl(how, MT_DATA, 0);
308299d258fdSSam Leffler 			if (m == NULL)
308399d258fdSSam Leffler 				goto bad;
308499d258fdSSam Leffler 			bcopy(mtod(n, void *), mtod(m, void *), n->m_len);
308599d258fdSSam Leffler 			bcopy(mtod(n2, void *), mtod(m, char *) + n->m_len,
308699d258fdSSam Leffler 				n2->m_len);
308799d258fdSSam Leffler 			m->m_len = n->m_len + n2->m_len;
308899d258fdSSam Leffler 			m->m_next = n2->m_next;
308999d258fdSSam Leffler 			*prev = m;
309099d258fdSSam Leffler 			m_free(n);
309199d258fdSSam Leffler 			m_free(n2);
309299d258fdSSam Leffler 			if (--curfrags <= maxfrags)	/* +1 cl -2 mbufs */
309399d258fdSSam Leffler 				return m0;
309499d258fdSSam Leffler 			/*
309599d258fdSSam Leffler 			 * Still not there, try the normal collapse
309699d258fdSSam Leffler 			 * again before we allocate another cluster.
309799d258fdSSam Leffler 			 */
309899d258fdSSam Leffler 			goto again;
309999d258fdSSam Leffler 		}
310099d258fdSSam Leffler 		prev = &n->m_next;
310199d258fdSSam Leffler 	}
310299d258fdSSam Leffler 	/*
310399d258fdSSam Leffler 	 * No place where we can collapse to a cluster; punt.
310499d258fdSSam Leffler 	 * This can occur if, for example, you request 2 frags
310599d258fdSSam Leffler 	 * but the packet requires that both be clusters (we
310699d258fdSSam Leffler 	 * never reallocate the first mbuf to avoid moving the
310799d258fdSSam Leffler 	 * packet header).
310899d258fdSSam Leffler 	 */
310999d258fdSSam Leffler bad:
311099d258fdSSam Leffler 	return NULL;
311199d258fdSSam Leffler }
311299d258fdSSam Leffler 
31135591b213SSam Leffler static int
31145591b213SSam Leffler ath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni, struct ath_buf *bf,
31155591b213SSam Leffler     struct mbuf *m0)
31165591b213SSam Leffler {
3117c4c3cb46SSam Leffler #define	CTS_DURATION \
3118c4c3cb46SSam Leffler 	ath_hal_computetxtime(ah, rt, IEEE80211_ACK_LEN, cix, AH_TRUE)
3119c4c3cb46SSam Leffler #define	updateCTSForBursting(_ah, _ds, _txq) \
3120c4c3cb46SSam Leffler 	ath_hal_updateCTSForBursting(_ah, _ds, \
3121c4c3cb46SSam Leffler 	    _txq->axq_linkbuf != NULL ? _txq->axq_linkbuf->bf_desc : NULL, \
3122c4c3cb46SSam Leffler 	    _txq->axq_lastdsWithCTS, _txq->axq_gatingds, \
3123c4c3cb46SSam Leffler 	    txopLimit, CTS_DURATION)
31245591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
31255591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3126fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
3127c4c3cb46SSam Leffler 	const struct chanAccParams *cap = &ic->ic_wme.wme_chanParams;
3128c42a7b7eSSam Leffler 	int i, error, iswep, ismcast, keyix, hdrlen, pktlen, try0;
3129c42a7b7eSSam Leffler 	u_int8_t rix, txrate, ctsrate;
3130c42a7b7eSSam Leffler 	u_int8_t cix = 0xff;		/* NB: silence compiler */
3131c42a7b7eSSam Leffler 	struct ath_desc *ds, *ds0;
3132c42a7b7eSSam Leffler 	struct ath_txq *txq;
31335591b213SSam Leffler 	struct ieee80211_frame *wh;
3134c42a7b7eSSam Leffler 	u_int subtype, flags, ctsduration;
31355591b213SSam Leffler 	HAL_PKT_TYPE atype;
31365591b213SSam Leffler 	const HAL_RATE_TABLE *rt;
31375591b213SSam Leffler 	HAL_BOOL shortPreamble;
31385591b213SSam Leffler 	struct ath_node *an;
313999d258fdSSam Leffler 	struct mbuf *m;
3140c4c3cb46SSam Leffler 	u_int pri;
31415591b213SSam Leffler 
31425591b213SSam Leffler 	wh = mtod(m0, struct ieee80211_frame *);
31435591b213SSam Leffler 	iswep = wh->i_fc[1] & IEEE80211_FC1_WEP;
3144c42a7b7eSSam Leffler 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
3145c42a7b7eSSam Leffler 	hdrlen = ieee80211_anyhdrsize(wh);
3146c42a7b7eSSam Leffler 	/*
3147a614e076SSam Leffler 	 * Packet length must not include any
3148a614e076SSam Leffler 	 * pad bytes; deduct them here.
3149c42a7b7eSSam Leffler 	 */
3150c42a7b7eSSam Leffler 	pktlen = m0->m_pkthdr.len - (hdrlen & 3);
31515591b213SSam Leffler 
31525591b213SSam Leffler 	if (iswep) {
3153c42a7b7eSSam Leffler 		const struct ieee80211_cipher *cip;
3154c42a7b7eSSam Leffler 		struct ieee80211_key *k;
3155c42a7b7eSSam Leffler 
3156c42a7b7eSSam Leffler 		/*
3157c42a7b7eSSam Leffler 		 * Construct the 802.11 header+trailer for an encrypted
3158c42a7b7eSSam Leffler 		 * frame. The only reason this can fail is because of an
3159c42a7b7eSSam Leffler 		 * unknown or unsupported cipher/key type.
3160c42a7b7eSSam Leffler 		 */
3161c42a7b7eSSam Leffler 		k = ieee80211_crypto_encap(ic, ni, m0);
3162c42a7b7eSSam Leffler 		if (k == NULL) {
3163c42a7b7eSSam Leffler 			/*
3164c42a7b7eSSam Leffler 			 * This can happen when the key is yanked after the
3165c42a7b7eSSam Leffler 			 * frame was queued.  Just discard the frame; the
3166c42a7b7eSSam Leffler 			 * 802.11 layer counts failures and provides
3167c42a7b7eSSam Leffler 			 * debugging/diagnostics.
3168c42a7b7eSSam Leffler 			 */
31690c97ab96SSam Leffler 			m_freem(m0);
3170c42a7b7eSSam Leffler 			return EIO;
31715591b213SSam Leffler 		}
3172c42a7b7eSSam Leffler 		/*
3173c42a7b7eSSam Leffler 		 * Adjust the packet + header lengths for the crypto
3174c42a7b7eSSam Leffler 		 * additions and calculate the h/w key index.  When
3175c42a7b7eSSam Leffler 		 * a s/w mic is done the frame will have had any mic
3176c42a7b7eSSam Leffler 		 * added to it prior to entry so skb->len above will
3177c42a7b7eSSam Leffler 		 * account for it. Otherwise we need to add it to the
3178c42a7b7eSSam Leffler 		 * packet length.
3179c42a7b7eSSam Leffler 		 */
3180c42a7b7eSSam Leffler 		cip = k->wk_cipher;
3181c42a7b7eSSam Leffler 		hdrlen += cip->ic_header;
3182c42a7b7eSSam Leffler 		pktlen += cip->ic_header + cip->ic_trailer;
3183c42a7b7eSSam Leffler 		if ((k->wk_flags & IEEE80211_KEY_SWMIC) == 0)
3184c42a7b7eSSam Leffler 			pktlen += cip->ic_miclen;
3185c42a7b7eSSam Leffler 		keyix = k->wk_keyix;
3186c42a7b7eSSam Leffler 
3187c42a7b7eSSam Leffler 		/* packet header may have moved, reset our local pointer */
3188167ecdcaSSam Leffler 		wh = mtod(m0, struct ieee80211_frame *);
3189e8fd88a3SSam Leffler 	} else if (ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) {
3190e8fd88a3SSam Leffler 		/*
3191e8fd88a3SSam Leffler 		 * Use station key cache slot, if assigned.
3192e8fd88a3SSam Leffler 		 */
3193e8fd88a3SSam Leffler 		keyix = ni->ni_ucastkey.wk_keyix;
3194e8fd88a3SSam Leffler 		if (keyix == IEEE80211_KEYIX_NONE)
3195e8fd88a3SSam Leffler 			keyix = HAL_TXKEYIX_INVALID;
3196c42a7b7eSSam Leffler 	} else
3197c42a7b7eSSam Leffler 		keyix = HAL_TXKEYIX_INVALID;
3198c42a7b7eSSam Leffler 
31995591b213SSam Leffler 	pktlen += IEEE80211_CRC_LEN;
32005591b213SSam Leffler 
32015591b213SSam Leffler 	/*
32025591b213SSam Leffler 	 * Load the DMA map so any coalescing is done.  This
32035591b213SSam Leffler 	 * also calculates the number of descriptors we need.
32045591b213SSam Leffler 	 */
3205f9e6219bSSam Leffler 	error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0,
3206f9e6219bSSam Leffler 				     bf->bf_segs, &bf->bf_nseg,
32075591b213SSam Leffler 				     BUS_DMA_NOWAIT);
320800a12f3aSSam Leffler 	if (error == EFBIG) {
320900a12f3aSSam Leffler 		/* XXX packet requires too many descriptors */
321000a12f3aSSam Leffler 		bf->bf_nseg = ATH_TXDESC+1;
321100a12f3aSSam Leffler 	} else if (error != 0) {
32125591b213SSam Leffler 		sc->sc_stats.ast_tx_busdma++;
32135591b213SSam Leffler 		m_freem(m0);
32145591b213SSam Leffler 		return error;
32155591b213SSam Leffler 	}
32165591b213SSam Leffler 	/*
32175591b213SSam Leffler 	 * Discard null packets and check for packets that
32185591b213SSam Leffler 	 * require too many TX descriptors.  We try to convert
32195591b213SSam Leffler 	 * the latter to a cluster.
32205591b213SSam Leffler 	 */
32215591b213SSam Leffler 	if (bf->bf_nseg > ATH_TXDESC) {		/* too many desc's, linearize */
32225591b213SSam Leffler 		sc->sc_stats.ast_tx_linear++;
322399d258fdSSam Leffler 		m = ath_defrag(m0, M_DONTWAIT, ATH_TXDESC);
322499d258fdSSam Leffler 		if (m == NULL) {
32255591b213SSam Leffler 			m_freem(m0);
322699d258fdSSam Leffler 			sc->sc_stats.ast_tx_nombuf++;
32275591b213SSam Leffler 			return ENOMEM;
32285591b213SSam Leffler 		}
322999d258fdSSam Leffler 		m0 = m;
3230f9e6219bSSam Leffler 		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0,
3231f9e6219bSSam Leffler 					     bf->bf_segs, &bf->bf_nseg,
32325591b213SSam Leffler 					     BUS_DMA_NOWAIT);
32335591b213SSam Leffler 		if (error != 0) {
32345591b213SSam Leffler 			sc->sc_stats.ast_tx_busdma++;
32355591b213SSam Leffler 			m_freem(m0);
32365591b213SSam Leffler 			return error;
32375591b213SSam Leffler 		}
3238f6b8ec16SSam Leffler 		KASSERT(bf->bf_nseg <= ATH_TXDESC,
3239f6b8ec16SSam Leffler 		    ("too many segments after defrag; nseg %u", bf->bf_nseg));
32405591b213SSam Leffler 	} else if (bf->bf_nseg == 0) {		/* null packet, discard */
32415591b213SSam Leffler 		sc->sc_stats.ast_tx_nodata++;
32425591b213SSam Leffler 		m_freem(m0);
32435591b213SSam Leffler 		return EIO;
32445591b213SSam Leffler 	}
3245c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_XMIT, "%s: m %p len %u\n", __func__, m0, pktlen);
32465591b213SSam Leffler 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
32475591b213SSam Leffler 	bf->bf_m = m0;
32480a915fadSSam Leffler 	bf->bf_node = ni;			/* NB: held reference */
32495591b213SSam Leffler 
32505591b213SSam Leffler 	/* setup descriptors */
32515591b213SSam Leffler 	ds = bf->bf_desc;
32525591b213SSam Leffler 	rt = sc->sc_currates;
32535591b213SSam Leffler 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
32545591b213SSam Leffler 
32555591b213SSam Leffler 	/*
3256c42a7b7eSSam Leffler 	 * NB: the 802.11 layer marks whether or not we should
3257c42a7b7eSSam Leffler 	 * use short preamble based on the current mode and
3258c42a7b7eSSam Leffler 	 * negotiated parameters.
32595591b213SSam Leffler 	 */
3260c42a7b7eSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
3261c42a7b7eSSam Leffler 	    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
3262c42a7b7eSSam Leffler 		shortPreamble = AH_TRUE;
3263c42a7b7eSSam Leffler 		sc->sc_stats.ast_tx_shortpre++;
3264c42a7b7eSSam Leffler 	} else {
3265c42a7b7eSSam Leffler 		shortPreamble = AH_FALSE;
3266c42a7b7eSSam Leffler 	}
3267c42a7b7eSSam Leffler 
3268c42a7b7eSSam Leffler 	an = ATH_NODE(ni);
3269c42a7b7eSSam Leffler 	flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for crypto errs */
3270c42a7b7eSSam Leffler 	/*
3271c42a7b7eSSam Leffler 	 * Calculate Atheros packet type from IEEE80211 packet header,
3272c42a7b7eSSam Leffler 	 * setup for rate calculations, and select h/w transmit queue.
3273c42a7b7eSSam Leffler 	 */
32745591b213SSam Leffler 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
32755591b213SSam Leffler 	case IEEE80211_FC0_TYPE_MGT:
32765591b213SSam Leffler 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
32775591b213SSam Leffler 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON)
32785591b213SSam Leffler 			atype = HAL_PKT_TYPE_BEACON;
32795591b213SSam Leffler 		else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
32805591b213SSam Leffler 			atype = HAL_PKT_TYPE_PROBE_RESP;
32815591b213SSam Leffler 		else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM)
32825591b213SSam Leffler 			atype = HAL_PKT_TYPE_ATIM;
3283c42a7b7eSSam Leffler 		else
3284c42a7b7eSSam Leffler 			atype = HAL_PKT_TYPE_NORMAL;	/* XXX */
32855591b213SSam Leffler 		rix = 0;			/* XXX lowest rate */
3286c42a7b7eSSam Leffler 		try0 = ATH_TXMAXTRY;
3287c42a7b7eSSam Leffler 		if (shortPreamble)
3288c42a7b7eSSam Leffler 			txrate = an->an_tx_mgtratesp;
3289c42a7b7eSSam Leffler 		else
3290c42a7b7eSSam Leffler 			txrate = an->an_tx_mgtrate;
3291c42a7b7eSSam Leffler 		/* NB: force all management frames to highest queue */
3292c42a7b7eSSam Leffler 		if (ni->ni_flags & IEEE80211_NODE_QOS) {
3293c42a7b7eSSam Leffler 			/* NB: force all management frames to highest queue */
3294c4c3cb46SSam Leffler 			pri = WME_AC_VO;
3295c42a7b7eSSam Leffler 		} else
3296c4c3cb46SSam Leffler 			pri = WME_AC_BE;
3297c42a7b7eSSam Leffler 		flags |= HAL_TXDESC_INTREQ;	/* force interrupt */
32985591b213SSam Leffler 		break;
32995591b213SSam Leffler 	case IEEE80211_FC0_TYPE_CTL:
3300c42a7b7eSSam Leffler 		atype = HAL_PKT_TYPE_PSPOLL;	/* stop setting of duration */
33015591b213SSam Leffler 		rix = 0;			/* XXX lowest rate */
3302c42a7b7eSSam Leffler 		try0 = ATH_TXMAXTRY;
3303c42a7b7eSSam Leffler 		if (shortPreamble)
3304c42a7b7eSSam Leffler 			txrate = an->an_tx_mgtratesp;
3305c42a7b7eSSam Leffler 		else
3306c42a7b7eSSam Leffler 			txrate = an->an_tx_mgtrate;
3307c42a7b7eSSam Leffler 		/* NB: force all ctl frames to highest queue */
3308c42a7b7eSSam Leffler 		if (ni->ni_flags & IEEE80211_NODE_QOS) {
3309c42a7b7eSSam Leffler 			/* NB: force all ctl frames to highest queue */
3310c4c3cb46SSam Leffler 			pri = WME_AC_VO;
3311c42a7b7eSSam Leffler 		} else
3312c4c3cb46SSam Leffler 			pri = WME_AC_BE;
3313c42a7b7eSSam Leffler 		flags |= HAL_TXDESC_INTREQ;	/* force interrupt */
3314c42a7b7eSSam Leffler 		break;
3315c42a7b7eSSam Leffler 	case IEEE80211_FC0_TYPE_DATA:
3316c42a7b7eSSam Leffler 		atype = HAL_PKT_TYPE_NORMAL;		/* default */
3317c42a7b7eSSam Leffler 		/*
3318c42a7b7eSSam Leffler 		 * Data frames; consult the rate control module.
3319c42a7b7eSSam Leffler 		 */
3320c42a7b7eSSam Leffler 		ath_rate_findrate(sc, an, shortPreamble, pktlen,
3321c42a7b7eSSam Leffler 			&rix, &try0, &txrate);
33223e50ec2cSSam Leffler 		sc->sc_txrate = txrate;			/* for LED blinking */
3323c42a7b7eSSam Leffler 		/*
3324c42a7b7eSSam Leffler 		 * Default all non-QoS traffic to the background queue.
3325c42a7b7eSSam Leffler 		 */
3326c42a7b7eSSam Leffler 		if (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_QOS) {
3327c4c3cb46SSam Leffler 			pri = M_WME_GETAC(m0);
3328c4c3cb46SSam Leffler 			if (cap->cap_wmeParams[pri].wmep_noackPolicy) {
3329c42a7b7eSSam Leffler 				flags |= HAL_TXDESC_NOACK;
3330aab26fb4SSam Leffler 				sc->sc_stats.ast_tx_noack++;
3331aab26fb4SSam Leffler 			}
3332c42a7b7eSSam Leffler 		} else
3333c4c3cb46SSam Leffler 			pri = WME_AC_BE;
33345591b213SSam Leffler 		break;
33355591b213SSam Leffler 	default:
3336c42a7b7eSSam Leffler 		if_printf(ifp, "bogus frame type 0x%x (%s)\n",
3337c42a7b7eSSam Leffler 			wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__);
3338c42a7b7eSSam Leffler 		/* XXX statistic */
33395591b213SSam Leffler 		m_freem(m0);
33405591b213SSam Leffler 		return EIO;
33415591b213SSam Leffler 	}
3342c4c3cb46SSam Leffler 	txq = sc->sc_ac2q[pri];
3343c42a7b7eSSam Leffler 
33445591b213SSam Leffler 	/*
3345c42a7b7eSSam Leffler 	 * When servicing one or more stations in power-save mode
3346c42a7b7eSSam Leffler 	 * multicast frames must be buffered until after the beacon.
3347c42a7b7eSSam Leffler 	 * We use the CAB queue for that.
33485591b213SSam Leffler 	 */
3349c42a7b7eSSam Leffler 	if (ismcast && ic->ic_ps_sta) {
3350c42a7b7eSSam Leffler 		txq = sc->sc_cabq;
3351c42a7b7eSSam Leffler 		/* XXX? more bit in 802.11 frame header */
33525591b213SSam Leffler 	}
33535591b213SSam Leffler 
33545591b213SSam Leffler 	/*
33555591b213SSam Leffler 	 * Calculate miscellaneous flags.
33565591b213SSam Leffler 	 */
3357c42a7b7eSSam Leffler 	if (ismcast) {
33585591b213SSam Leffler 		flags |= HAL_TXDESC_NOACK;	/* no ack on broad/multicast */
33595591b213SSam Leffler 		sc->sc_stats.ast_tx_noack++;
33605591b213SSam Leffler 	} else if (pktlen > ic->ic_rtsthreshold) {
33615591b213SSam Leffler 		flags |= HAL_TXDESC_RTSENA;	/* RTS based on frame length */
3362c42a7b7eSSam Leffler 		cix = rt->info[rix].controlRate;
33635591b213SSam Leffler 		sc->sc_stats.ast_tx_rts++;
33645591b213SSam Leffler 	}
33655591b213SSam Leffler 
33665591b213SSam Leffler 	/*
3367c42a7b7eSSam Leffler 	 * If 802.11g protection is enabled, determine whether
3368c42a7b7eSSam Leffler 	 * to use RTS/CTS or just CTS.  Note that this is only
3369c42a7b7eSSam Leffler 	 * done for OFDM unicast frames.
3370c42a7b7eSSam Leffler 	 */
3371c42a7b7eSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
3372c42a7b7eSSam Leffler 	    rt->info[rix].phy == IEEE80211_T_OFDM &&
3373c42a7b7eSSam Leffler 	    (flags & HAL_TXDESC_NOACK) == 0) {
3374c42a7b7eSSam Leffler 		/* XXX fragments must use CCK rates w/ protection */
3375c42a7b7eSSam Leffler 		if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
3376c42a7b7eSSam Leffler 			flags |= HAL_TXDESC_RTSENA;
3377c42a7b7eSSam Leffler 		else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
3378c42a7b7eSSam Leffler 			flags |= HAL_TXDESC_CTSENA;
3379c42a7b7eSSam Leffler 		cix = rt->info[sc->sc_protrix].controlRate;
3380c42a7b7eSSam Leffler 		sc->sc_stats.ast_tx_protect++;
3381c42a7b7eSSam Leffler 	}
3382c42a7b7eSSam Leffler 
3383c42a7b7eSSam Leffler 	/*
3384f6aa038bSSam Leffler 	 * Calculate duration.  This logically belongs in the 802.11
3385f6aa038bSSam Leffler 	 * layer but it lacks sufficient information to calculate it.
3386f6aa038bSSam Leffler 	 */
3387f6aa038bSSam Leffler 	if ((flags & HAL_TXDESC_NOACK) == 0 &&
3388f6aa038bSSam Leffler 	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL) {
3389f6aa038bSSam Leffler 		u_int16_t dur;
3390f6aa038bSSam Leffler 		/*
3391f6aa038bSSam Leffler 		 * XXX not right with fragmentation.
3392f6aa038bSSam Leffler 		 */
3393c42a7b7eSSam Leffler 		if (shortPreamble)
3394c42a7b7eSSam Leffler 			dur = rt->info[rix].spAckDuration;
3395c42a7b7eSSam Leffler 		else
3396c42a7b7eSSam Leffler 			dur = rt->info[rix].lpAckDuration;
3397c42a7b7eSSam Leffler 		*(u_int16_t *)wh->i_dur = htole16(dur);
3398f6aa038bSSam Leffler 	}
3399f6aa038bSSam Leffler 
3400f6aa038bSSam Leffler 	/*
34015591b213SSam Leffler 	 * Calculate RTS/CTS rate and duration if needed.
34025591b213SSam Leffler 	 */
34035591b213SSam Leffler 	ctsduration = 0;
34045591b213SSam Leffler 	if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA)) {
34055591b213SSam Leffler 		/*
34065591b213SSam Leffler 		 * CTS transmit rate is derived from the transmit rate
34075591b213SSam Leffler 		 * by looking in the h/w rate table.  We must also factor
34085591b213SSam Leffler 		 * in whether or not a short preamble is to be used.
34095591b213SSam Leffler 		 */
3410c42a7b7eSSam Leffler 		/* NB: cix is set above where RTS/CTS is enabled */
3411c42a7b7eSSam Leffler 		KASSERT(cix != 0xff, ("cix not setup"));
34125591b213SSam Leffler 		ctsrate = rt->info[cix].rateCode;
34135591b213SSam Leffler 		/*
3414c42a7b7eSSam Leffler 		 * Compute the transmit duration based on the frame
3415c42a7b7eSSam Leffler 		 * size and the size of an ACK frame.  We call into the
3416c42a7b7eSSam Leffler 		 * HAL to do the computation since it depends on the
3417c42a7b7eSSam Leffler 		 * characteristics of the actual PHY being used.
3418c42a7b7eSSam Leffler 		 *
3419c42a7b7eSSam Leffler 		 * NB: CTS is assumed the same size as an ACK so we can
3420c42a7b7eSSam Leffler 		 *     use the precalculated ACK durations.
34215591b213SSam Leffler 		 */
3422c42a7b7eSSam Leffler 		if (shortPreamble) {
3423c42a7b7eSSam Leffler 			ctsrate |= rt->info[cix].shortPreamble;
3424c42a7b7eSSam Leffler 			if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
3425c42a7b7eSSam Leffler 				ctsduration += rt->info[cix].spAckDuration;
34265591b213SSam Leffler 			ctsduration += ath_hal_computetxtime(ah,
3427c42a7b7eSSam Leffler 				rt, pktlen, rix, AH_TRUE);
3428c42a7b7eSSam Leffler 			if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
34296ee571b2SSam Leffler 				ctsduration += rt->info[rix].spAckDuration;
3430c42a7b7eSSam Leffler 		} else {
3431c42a7b7eSSam Leffler 			if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
3432c42a7b7eSSam Leffler 				ctsduration += rt->info[cix].lpAckDuration;
3433c42a7b7eSSam Leffler 			ctsduration += ath_hal_computetxtime(ah,
3434c42a7b7eSSam Leffler 				rt, pktlen, rix, AH_FALSE);
3435c42a7b7eSSam Leffler 			if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
34366ee571b2SSam Leffler 				ctsduration += rt->info[rix].lpAckDuration;
34375591b213SSam Leffler 		}
3438c42a7b7eSSam Leffler 		/*
3439c42a7b7eSSam Leffler 		 * Must disable multi-rate retry when using RTS/CTS.
3440c42a7b7eSSam Leffler 		 */
3441c42a7b7eSSam Leffler 		try0 = ATH_TXMAXTRY;
34425591b213SSam Leffler 	} else
34435591b213SSam Leffler 		ctsrate = 0;
34445591b213SSam Leffler 
3445c42a7b7eSSam Leffler 	if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
3446c42a7b7eSSam Leffler 		ieee80211_dump_pkt(mtod(m0, caddr_t), m0->m_len,
34473e50ec2cSSam Leffler 			sc->sc_hwmap[txrate].ieeerate, -1);
34485591b213SSam Leffler 
3449eb2cdcb1SSam Leffler 	if (ic->ic_rawbpf)
3450eb2cdcb1SSam Leffler 		bpf_mtap(ic->ic_rawbpf, m0);
3451eb2cdcb1SSam Leffler 	if (sc->sc_drvbpf) {
3452d3be6f5bSSam Leffler 		sc->sc_tx_th.wt_flags = sc->sc_hwmap[txrate].txflags;
3453eb2cdcb1SSam Leffler 		if (iswep)
3454eb2cdcb1SSam Leffler 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
34553e50ec2cSSam Leffler 		sc->sc_tx_th.wt_rate = sc->sc_hwmap[txrate].ieeerate;
3456c42a7b7eSSam Leffler 		sc->sc_tx_th.wt_txpower = ni->ni_txpower;
3457c42a7b7eSSam Leffler 		sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
3458eb2cdcb1SSam Leffler 
3459eb2cdcb1SSam Leffler 		bpf_mtap2(sc->sc_drvbpf,
34602f1ad18bSSam Leffler 			&sc->sc_tx_th, sc->sc_tx_th_len, m0);
3461eb2cdcb1SSam Leffler 	}
3462eb2cdcb1SSam Leffler 
34635591b213SSam Leffler 	/*
3464c42a7b7eSSam Leffler 	 * Determine if a tx interrupt should be generated for
3465c42a7b7eSSam Leffler 	 * this descriptor.  We take a tx interrupt to reap
3466c42a7b7eSSam Leffler 	 * descriptors when the h/w hits an EOL condition or
3467c42a7b7eSSam Leffler 	 * when the descriptor is specifically marked to generate
3468c42a7b7eSSam Leffler 	 * an interrupt.  We periodically mark descriptors in this
3469c42a7b7eSSam Leffler 	 * way to insure timely replenishing of the supply needed
3470c42a7b7eSSam Leffler 	 * for sending frames.  Defering interrupts reduces system
3471c42a7b7eSSam Leffler 	 * load and potentially allows more concurrent work to be
3472c42a7b7eSSam Leffler 	 * done but if done to aggressively can cause senders to
3473c42a7b7eSSam Leffler 	 * backup.
3474c42a7b7eSSam Leffler 	 *
3475c42a7b7eSSam Leffler 	 * NB: use >= to deal with sc_txintrperiod changing
3476c42a7b7eSSam Leffler 	 *     dynamically through sysctl.
3477c42a7b7eSSam Leffler 	 */
3478c42a7b7eSSam Leffler 	if (flags & HAL_TXDESC_INTREQ) {
3479c42a7b7eSSam Leffler 		txq->axq_intrcnt = 0;
3480c42a7b7eSSam Leffler 	} else if (++txq->axq_intrcnt >= sc->sc_txintrperiod) {
3481c42a7b7eSSam Leffler 		flags |= HAL_TXDESC_INTREQ;
3482c42a7b7eSSam Leffler 		txq->axq_intrcnt = 0;
3483c42a7b7eSSam Leffler 	}
3484c42a7b7eSSam Leffler 
3485c42a7b7eSSam Leffler 	/*
34865591b213SSam Leffler 	 * Formulate first tx descriptor with tx controls.
34875591b213SSam Leffler 	 */
34885591b213SSam Leffler 	/* XXX check return value? */
34895591b213SSam Leffler 	ath_hal_setuptxdesc(ah, ds
34905591b213SSam Leffler 		, pktlen		/* packet length */
34915591b213SSam Leffler 		, hdrlen		/* header length */
34925591b213SSam Leffler 		, atype			/* Atheros packet type */
3493c42a7b7eSSam Leffler 		, ni->ni_txpower	/* txpower */
3494c42a7b7eSSam Leffler 		, txrate, try0		/* series 0 rate/tries */
3495c42a7b7eSSam Leffler 		, keyix			/* key cache index */
3496c42a7b7eSSam Leffler 		, sc->sc_txantenna	/* antenna mode */
34975591b213SSam Leffler 		, flags			/* flags */
34985591b213SSam Leffler 		, ctsrate		/* rts/cts rate */
34995591b213SSam Leffler 		, ctsduration		/* rts/cts duration */
35005591b213SSam Leffler 	);
35018f409431SSam Leffler 	bf->bf_flags = flags;
3502c42a7b7eSSam Leffler 	/*
3503c42a7b7eSSam Leffler 	 * Setup the multi-rate retry state only when we're
3504c42a7b7eSSam Leffler 	 * going to use it.  This assumes ath_hal_setuptxdesc
3505c42a7b7eSSam Leffler 	 * initializes the descriptors (so we don't have to)
3506c42a7b7eSSam Leffler 	 * when the hardware supports multi-rate retry and
3507c42a7b7eSSam Leffler 	 * we don't use it.
3508c42a7b7eSSam Leffler 	 */
3509c42a7b7eSSam Leffler 	if (try0 != ATH_TXMAXTRY)
3510c42a7b7eSSam Leffler 		ath_rate_setupxtxdesc(sc, an, ds, shortPreamble, rix);
3511c42a7b7eSSam Leffler 
35125591b213SSam Leffler 	/*
35135591b213SSam Leffler 	 * Fillin the remainder of the descriptor info.
35145591b213SSam Leffler 	 */
3515c42a7b7eSSam Leffler 	ds0 = ds;
35165591b213SSam Leffler 	for (i = 0; i < bf->bf_nseg; i++, ds++) {
35175591b213SSam Leffler 		ds->ds_data = bf->bf_segs[i].ds_addr;
35185591b213SSam Leffler 		if (i == bf->bf_nseg - 1)
35195591b213SSam Leffler 			ds->ds_link = 0;
35205591b213SSam Leffler 		else
35215591b213SSam Leffler 			ds->ds_link = bf->bf_daddr + sizeof(*ds) * (i + 1);
35225591b213SSam Leffler 		ath_hal_filltxdesc(ah, ds
35235591b213SSam Leffler 			, bf->bf_segs[i].ds_len	/* segment length */
35245591b213SSam Leffler 			, i == 0		/* first segment */
35255591b213SSam Leffler 			, i == bf->bf_nseg - 1	/* last segment */
3526c42a7b7eSSam Leffler 			, ds0			/* first descriptor */
35275591b213SSam Leffler 		);
3528c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT,
3529c42a7b7eSSam Leffler 			"%s: %d: %08x %08x %08x %08x %08x %08x\n",
3530e325e530SSam Leffler 			__func__, i, ds->ds_link, ds->ds_data,
3531c42a7b7eSSam Leffler 			ds->ds_ctl0, ds->ds_ctl1, ds->ds_hw[0], ds->ds_hw[1]);
35325591b213SSam Leffler 	}
35335591b213SSam Leffler 	/*
35345591b213SSam Leffler 	 * Insert the frame on the outbound list and
35355591b213SSam Leffler 	 * pass it on to the hardware.
35365591b213SSam Leffler 	 */
3537c42a7b7eSSam Leffler 	ATH_TXQ_LOCK(txq);
3538c4c3cb46SSam Leffler 	if (flags & (HAL_TXDESC_RTSENA | HAL_TXDESC_CTSENA)) {
3539c4c3cb46SSam Leffler 		u_int32_t txopLimit = IEEE80211_TXOP_TO_US(
3540c4c3cb46SSam Leffler 			cap->cap_wmeParams[pri].wmep_txopLimit);
3541c4c3cb46SSam Leffler 		/*
3542c4c3cb46SSam Leffler 		 * When bursting, potentially extend the CTS duration
3543c4c3cb46SSam Leffler 		 * of a previously queued frame to cover this frame
3544c4c3cb46SSam Leffler 		 * and not exceed the txopLimit.  If that can be done
3545c4c3cb46SSam Leffler 		 * then disable RTS/CTS on this frame since it's now
3546c4c3cb46SSam Leffler 		 * covered (burst extension).  Otherwise we must terminate
3547c4c3cb46SSam Leffler 		 * the burst before this frame goes out so as not to
3548c4c3cb46SSam Leffler 		 * violate the WME parameters.  All this is complicated
3549c4c3cb46SSam Leffler 		 * as we need to update the state of packets on the
3550c4c3cb46SSam Leffler 		 * (live) hardware queue.  The logic is buried in the hal
3551c4c3cb46SSam Leffler 		 * because it's highly chip-specific.
3552c4c3cb46SSam Leffler 		 */
3553c4c3cb46SSam Leffler 		if (txopLimit != 0) {
3554c4c3cb46SSam Leffler 			sc->sc_stats.ast_tx_ctsburst++;
3555c4c3cb46SSam Leffler 			if (updateCTSForBursting(ah, ds0, txq) == 0) {
3556c4c3cb46SSam Leffler 				/*
3557c4c3cb46SSam Leffler 				 * This frame was not covered by RTS/CTS from
3558c4c3cb46SSam Leffler 				 * the previous frame in the burst; update the
3559c4c3cb46SSam Leffler 				 * descriptor pointers so this frame is now
3560c4c3cb46SSam Leffler 				 * treated as the last frame for extending a
3561c4c3cb46SSam Leffler 				 * burst.
3562c4c3cb46SSam Leffler 				 */
3563c4c3cb46SSam Leffler 				txq->axq_lastdsWithCTS = ds0;
3564c4c3cb46SSam Leffler 				/* set gating Desc to final desc */
3565c4c3cb46SSam Leffler 				txq->axq_gatingds =
3566c4c3cb46SSam Leffler 					(struct ath_desc *)txq->axq_link;
3567c4c3cb46SSam Leffler 			} else
3568c4c3cb46SSam Leffler 				sc->sc_stats.ast_tx_ctsext++;
3569c4c3cb46SSam Leffler 		}
3570c4c3cb46SSam Leffler 	}
3571c42a7b7eSSam Leffler 	ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
3572c42a7b7eSSam Leffler 	if (txq->axq_link == NULL) {
3573c42a7b7eSSam Leffler 		ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
3574c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT,
3575c42a7b7eSSam Leffler 			"%s: TXDP[%u] = %p (%p) depth %d\n", __func__,
3576c42a7b7eSSam Leffler 			txq->axq_qnum, (caddr_t)bf->bf_daddr, bf->bf_desc,
3577c42a7b7eSSam Leffler 			txq->axq_depth);
35785591b213SSam Leffler 	} else {
3579c42a7b7eSSam Leffler 		*txq->axq_link = bf->bf_daddr;
3580c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT,
3581c42a7b7eSSam Leffler 			"%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
3582c42a7b7eSSam Leffler 			txq->axq_qnum, txq->axq_link,
3583c42a7b7eSSam Leffler 			(caddr_t)bf->bf_daddr, bf->bf_desc, txq->axq_depth);
35845591b213SSam Leffler 	}
3585c42a7b7eSSam Leffler 	txq->axq_link = &bf->bf_desc[bf->bf_nseg - 1].ds_link;
3586c42a7b7eSSam Leffler 	/*
3587c42a7b7eSSam Leffler 	 * The CAB queue is started from the SWBA handler since
3588c42a7b7eSSam Leffler 	 * frames only go out on DTIM and to avoid possible races.
3589c42a7b7eSSam Leffler 	 */
3590c42a7b7eSSam Leffler 	if (txq != sc->sc_cabq)
3591c42a7b7eSSam Leffler 		ath_hal_txstart(ah, txq->axq_qnum);
3592a8d7e0f6SSam Leffler 	ATH_TXQ_UNLOCK(txq);
3593a8d7e0f6SSam Leffler 
35945591b213SSam Leffler 	return 0;
3595c4c3cb46SSam Leffler #undef updateCTSForBursting
3596c4c3cb46SSam Leffler #undef CTS_DURATION
35975591b213SSam Leffler }
35985591b213SSam Leffler 
3599c42a7b7eSSam Leffler /*
3600c42a7b7eSSam Leffler  * Process completed xmit descriptors from the specified queue.
3601c42a7b7eSSam Leffler  */
36025591b213SSam Leffler static void
3603c42a7b7eSSam Leffler ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq)
36045591b213SSam Leffler {
36055591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
36060a915fadSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
3607c42a7b7eSSam Leffler 	struct ath_buf *bf;
3608c4c3cb46SSam Leffler 	struct ath_desc *ds, *ds0;
36095591b213SSam Leffler 	struct ieee80211_node *ni;
36105591b213SSam Leffler 	struct ath_node *an;
3611c42a7b7eSSam Leffler 	int sr, lr, pri;
36125591b213SSam Leffler 	HAL_STATUS status;
36135591b213SSam Leffler 
3614c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_TX_PROC, "%s: tx queue %u head %p link %p\n",
3615c42a7b7eSSam Leffler 		__func__, txq->axq_qnum,
3616c42a7b7eSSam Leffler 		(caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
3617c42a7b7eSSam Leffler 		txq->axq_link);
36185591b213SSam Leffler 	for (;;) {
3619c42a7b7eSSam Leffler 		ATH_TXQ_LOCK(txq);
3620c42a7b7eSSam Leffler 		txq->axq_intrcnt = 0;	/* reset periodic desc intr count */
3621c42a7b7eSSam Leffler 		bf = STAILQ_FIRST(&txq->axq_q);
36225591b213SSam Leffler 		if (bf == NULL) {
3623c42a7b7eSSam Leffler 			txq->axq_link = NULL;
3624c42a7b7eSSam Leffler 			ATH_TXQ_UNLOCK(txq);
36255591b213SSam Leffler 			break;
36265591b213SSam Leffler 		}
3627c4c3cb46SSam Leffler 		ds0 = &bf->bf_desc[0];
36285591b213SSam Leffler 		ds = &bf->bf_desc[bf->bf_nseg - 1];
36295591b213SSam Leffler 		status = ath_hal_txprocdesc(ah, ds);
36305591b213SSam Leffler #ifdef AR_DEBUG
3631c42a7b7eSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_XMIT_DESC)
36325591b213SSam Leffler 			ath_printtxbuf(bf, status == HAL_OK);
36335591b213SSam Leffler #endif
36345591b213SSam Leffler 		if (status == HAL_EINPROGRESS) {
3635c42a7b7eSSam Leffler 			ATH_TXQ_UNLOCK(txq);
36365591b213SSam Leffler 			break;
36375591b213SSam Leffler 		}
3638c4c3cb46SSam Leffler 		if (ds0 == txq->axq_lastdsWithCTS)
3639c42a7b7eSSam Leffler 			txq->axq_lastdsWithCTS = NULL;
3640c42a7b7eSSam Leffler 		if (ds == txq->axq_gatingds)
3641c42a7b7eSSam Leffler 			txq->axq_gatingds = NULL;
3642c42a7b7eSSam Leffler 		ATH_TXQ_REMOVE_HEAD(txq, bf_list);
3643c42a7b7eSSam Leffler 		ATH_TXQ_UNLOCK(txq);
36445591b213SSam Leffler 
36455591b213SSam Leffler 		ni = bf->bf_node;
36465591b213SSam Leffler 		if (ni != NULL) {
3647c42a7b7eSSam Leffler 			an = ATH_NODE(ni);
36485591b213SSam Leffler 			if (ds->ds_txstat.ts_status == 0) {
3649c42a7b7eSSam Leffler 				u_int8_t txant = ds->ds_txstat.ts_antenna;
3650c42a7b7eSSam Leffler 				sc->sc_stats.ast_ant_tx[txant]++;
3651c42a7b7eSSam Leffler 				sc->sc_ant_tx[txant]++;
3652c42a7b7eSSam Leffler 				if (ds->ds_txstat.ts_rate & HAL_TXSTAT_ALTRATE)
3653c42a7b7eSSam Leffler 					sc->sc_stats.ast_tx_altrate++;
3654c42a7b7eSSam Leffler 				sc->sc_stats.ast_tx_rssi =
3655c42a7b7eSSam Leffler 					ds->ds_txstat.ts_rssi;
3656c42a7b7eSSam Leffler 				ATH_RSSI_LPF(an->an_halstats.ns_avgtxrssi,
3657c42a7b7eSSam Leffler 					ds->ds_txstat.ts_rssi);
3658c42a7b7eSSam Leffler 				pri = M_WME_GETAC(bf->bf_m);
3659c42a7b7eSSam Leffler 				if (pri >= WME_AC_VO)
3660c42a7b7eSSam Leffler 					ic->ic_wme.wme_hipri_traffic++;
3661c42a7b7eSSam Leffler 				ni->ni_inact = ni->ni_inact_reload;
36625591b213SSam Leffler 			} else {
36635591b213SSam Leffler 				if (ds->ds_txstat.ts_status & HAL_TXERR_XRETRY)
36645591b213SSam Leffler 					sc->sc_stats.ast_tx_xretries++;
36655591b213SSam Leffler 				if (ds->ds_txstat.ts_status & HAL_TXERR_FIFO)
36665591b213SSam Leffler 					sc->sc_stats.ast_tx_fifoerr++;
36675591b213SSam Leffler 				if (ds->ds_txstat.ts_status & HAL_TXERR_FILT)
36685591b213SSam Leffler 					sc->sc_stats.ast_tx_filtered++;
36695591b213SSam Leffler 			}
36705591b213SSam Leffler 			sr = ds->ds_txstat.ts_shortretry;
36715591b213SSam Leffler 			lr = ds->ds_txstat.ts_longretry;
36725591b213SSam Leffler 			sc->sc_stats.ast_tx_shortretry += sr;
36735591b213SSam Leffler 			sc->sc_stats.ast_tx_longretry += lr;
3674c42a7b7eSSam Leffler 			/*
3675c42a7b7eSSam Leffler 			 * Hand the descriptor to the rate control algorithm.
3676c42a7b7eSSam Leffler 			 */
36778f409431SSam Leffler 			if ((ds->ds_txstat.ts_status & HAL_TXERR_FILT) == 0 &&
36788f409431SSam Leffler 			    (bf->bf_flags & HAL_TXDESC_NOACK) == 0)
367922233301SSam Leffler 				ath_rate_tx_complete(sc, an, ds, ds0);
36800a915fadSSam Leffler 			/*
36810a915fadSSam Leffler 			 * Reclaim reference to node.
36820a915fadSSam Leffler 			 *
36830a915fadSSam Leffler 			 * NB: the node may be reclaimed here if, for example
36840a915fadSSam Leffler 			 *     this is a DEAUTH message that was sent and the
36850a915fadSSam Leffler 			 *     node was timed out due to inactivity.
36860a915fadSSam Leffler 			 */
3687c42a7b7eSSam Leffler 			ieee80211_free_node(ni);
36885591b213SSam Leffler 		}
36895591b213SSam Leffler 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
36905591b213SSam Leffler 		    BUS_DMASYNC_POSTWRITE);
36915591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
36925591b213SSam Leffler 		m_freem(bf->bf_m);
36935591b213SSam Leffler 		bf->bf_m = NULL;
36945591b213SSam Leffler 		bf->bf_node = NULL;
36955591b213SSam Leffler 
3696f0b2a0beSSam Leffler 		ATH_TXBUF_LOCK(sc);
3697c42a7b7eSSam Leffler 		STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
3698f0b2a0beSSam Leffler 		ATH_TXBUF_UNLOCK(sc);
36995591b213SSam Leffler 	}
3700c42a7b7eSSam Leffler }
3701c42a7b7eSSam Leffler 
3702c42a7b7eSSam Leffler /*
3703c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
3704c42a7b7eSSam Leffler  * for a single hardware transmit queue (e.g. 5210 and 5211).
3705c42a7b7eSSam Leffler  */
3706c42a7b7eSSam Leffler static void
3707c42a7b7eSSam Leffler ath_tx_proc_q0(void *arg, int npending)
3708c42a7b7eSSam Leffler {
3709c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
3710fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
3711c42a7b7eSSam Leffler 
3712c42a7b7eSSam Leffler 	ath_tx_processq(sc, &sc->sc_txq[0]);
3713c42a7b7eSSam Leffler 	ath_tx_processq(sc, sc->sc_cabq);
371413f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
37155591b213SSam Leffler 	sc->sc_tx_timer = 0;
37165591b213SSam Leffler 
37173e50ec2cSSam Leffler 	if (sc->sc_softled)
37183e50ec2cSSam Leffler 		ath_led_event(sc, ATH_LED_TX);
37193e50ec2cSSam Leffler 
37205591b213SSam Leffler 	ath_start(ifp);
37215591b213SSam Leffler }
37225591b213SSam Leffler 
37235591b213SSam Leffler /*
3724c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
3725c42a7b7eSSam Leffler  * for four hardware queues, 0-3 (e.g. 5212 w/ WME support).
37265591b213SSam Leffler  */
37275591b213SSam Leffler static void
3728c42a7b7eSSam Leffler ath_tx_proc_q0123(void *arg, int npending)
3729c42a7b7eSSam Leffler {
3730c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
3731fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
3732c42a7b7eSSam Leffler 
3733c42a7b7eSSam Leffler 	/*
3734c42a7b7eSSam Leffler 	 * Process each active queue.
3735c42a7b7eSSam Leffler 	 */
3736c42a7b7eSSam Leffler 	ath_tx_processq(sc, &sc->sc_txq[0]);
3737c42a7b7eSSam Leffler 	ath_tx_processq(sc, &sc->sc_txq[1]);
3738c42a7b7eSSam Leffler 	ath_tx_processq(sc, &sc->sc_txq[2]);
3739c42a7b7eSSam Leffler 	ath_tx_processq(sc, &sc->sc_txq[3]);
3740c42a7b7eSSam Leffler 	ath_tx_processq(sc, sc->sc_cabq);
3741c42a7b7eSSam Leffler 
374213f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3743c42a7b7eSSam Leffler 	sc->sc_tx_timer = 0;
3744c42a7b7eSSam Leffler 
37453e50ec2cSSam Leffler 	if (sc->sc_softled)
37463e50ec2cSSam Leffler 		ath_led_event(sc, ATH_LED_TX);
37473e50ec2cSSam Leffler 
3748c42a7b7eSSam Leffler 	ath_start(ifp);
3749c42a7b7eSSam Leffler }
3750c42a7b7eSSam Leffler 
3751c42a7b7eSSam Leffler /*
3752c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt.
3753c42a7b7eSSam Leffler  */
3754c42a7b7eSSam Leffler static void
3755c42a7b7eSSam Leffler ath_tx_proc(void *arg, int npending)
3756c42a7b7eSSam Leffler {
3757c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
3758fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
3759c42a7b7eSSam Leffler 	int i;
3760c42a7b7eSSam Leffler 
3761c42a7b7eSSam Leffler 	/*
3762c42a7b7eSSam Leffler 	 * Process each active queue.
3763c42a7b7eSSam Leffler 	 */
3764c42a7b7eSSam Leffler 	/* XXX faster to read ISR_S0_S and ISR_S1_S to determine q's? */
3765c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
3766c42a7b7eSSam Leffler 		if (ATH_TXQ_SETUP(sc, i))
3767c42a7b7eSSam Leffler 			ath_tx_processq(sc, &sc->sc_txq[i]);
3768c42a7b7eSSam Leffler 
376913f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3770c42a7b7eSSam Leffler 	sc->sc_tx_timer = 0;
3771c42a7b7eSSam Leffler 
37723e50ec2cSSam Leffler 	if (sc->sc_softled)
37733e50ec2cSSam Leffler 		ath_led_event(sc, ATH_LED_TX);
37743e50ec2cSSam Leffler 
3775c42a7b7eSSam Leffler 	ath_start(ifp);
3776c42a7b7eSSam Leffler }
3777c42a7b7eSSam Leffler 
3778c42a7b7eSSam Leffler static void
3779c42a7b7eSSam Leffler ath_tx_draintxq(struct ath_softc *sc, struct ath_txq *txq)
37805591b213SSam Leffler {
37815591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
378223428eafSSam Leffler 	struct ieee80211_node *ni;
37835591b213SSam Leffler 	struct ath_buf *bf;
37845591b213SSam Leffler 
3785c42a7b7eSSam Leffler 	/*
3786c42a7b7eSSam Leffler 	 * NB: this assumes output has been stopped and
3787c42a7b7eSSam Leffler 	 *     we do not need to block ath_tx_tasklet
3788c42a7b7eSSam Leffler 	 */
37895591b213SSam Leffler 	for (;;) {
3790c42a7b7eSSam Leffler 		ATH_TXQ_LOCK(txq);
3791c42a7b7eSSam Leffler 		bf = STAILQ_FIRST(&txq->axq_q);
37925591b213SSam Leffler 		if (bf == NULL) {
3793c42a7b7eSSam Leffler 			txq->axq_link = NULL;
3794c42a7b7eSSam Leffler 			ATH_TXQ_UNLOCK(txq);
37955591b213SSam Leffler 			break;
37965591b213SSam Leffler 		}
3797c42a7b7eSSam Leffler 		ATH_TXQ_REMOVE_HEAD(txq, bf_list);
3798c42a7b7eSSam Leffler 		ATH_TXQ_UNLOCK(txq);
37995591b213SSam Leffler #ifdef AR_DEBUG
3800c42a7b7eSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_RESET)
38015591b213SSam Leffler 			ath_printtxbuf(bf,
38025591b213SSam Leffler 				ath_hal_txprocdesc(ah, bf->bf_desc) == HAL_OK);
38035591b213SSam Leffler #endif /* AR_DEBUG */
38045591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
38055591b213SSam Leffler 		m_freem(bf->bf_m);
38065591b213SSam Leffler 		bf->bf_m = NULL;
380723428eafSSam Leffler 		ni = bf->bf_node;
38085591b213SSam Leffler 		bf->bf_node = NULL;
3809c42a7b7eSSam Leffler 		if (ni != NULL) {
381023428eafSSam Leffler 			/*
381123428eafSSam Leffler 			 * Reclaim node reference.
381223428eafSSam Leffler 			 */
3813c42a7b7eSSam Leffler 			ieee80211_free_node(ni);
381423428eafSSam Leffler 		}
3815f0b2a0beSSam Leffler 		ATH_TXBUF_LOCK(sc);
3816c42a7b7eSSam Leffler 		STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
3817f0b2a0beSSam Leffler 		ATH_TXBUF_UNLOCK(sc);
38185591b213SSam Leffler 	}
3819c42a7b7eSSam Leffler }
3820c42a7b7eSSam Leffler 
3821c42a7b7eSSam Leffler static void
3822c42a7b7eSSam Leffler ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
3823c42a7b7eSSam Leffler {
3824c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3825c42a7b7eSSam Leffler 
3826c42a7b7eSSam Leffler 	(void) ath_hal_stoptxdma(ah, txq->axq_qnum);
3827c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n",
3828c42a7b7eSSam Leffler 	    __func__, txq->axq_qnum,
38296891c875SPeter Wemm 	    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, txq->axq_qnum),
38306891c875SPeter Wemm 	    txq->axq_link);
3831c42a7b7eSSam Leffler }
3832c42a7b7eSSam Leffler 
3833c42a7b7eSSam Leffler /*
3834c42a7b7eSSam Leffler  * Drain the transmit queues and reclaim resources.
3835c42a7b7eSSam Leffler  */
3836c42a7b7eSSam Leffler static void
3837c42a7b7eSSam Leffler ath_draintxq(struct ath_softc *sc)
3838c42a7b7eSSam Leffler {
3839c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3840fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
3841c42a7b7eSSam Leffler 	int i;
3842c42a7b7eSSam Leffler 
3843c42a7b7eSSam Leffler 	/* XXX return value */
3844c42a7b7eSSam Leffler 	if (!sc->sc_invalid) {
3845c42a7b7eSSam Leffler 		/* don't touch the hardware if marked invalid */
3846c42a7b7eSSam Leffler 		(void) ath_hal_stoptxdma(ah, sc->sc_bhalq);
3847c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_RESET,
3848c42a7b7eSSam Leffler 		    "%s: beacon queue %p\n", __func__,
3849c42a7b7eSSam Leffler 		    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq));
3850c42a7b7eSSam Leffler 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
3851c42a7b7eSSam Leffler 			if (ATH_TXQ_SETUP(sc, i))
3852c42a7b7eSSam Leffler 				ath_tx_stopdma(sc, &sc->sc_txq[i]);
3853c42a7b7eSSam Leffler 	}
3854c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
3855c42a7b7eSSam Leffler 		if (ATH_TXQ_SETUP(sc, i))
3856c42a7b7eSSam Leffler 			ath_tx_draintxq(sc, &sc->sc_txq[i]);
385713f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
38585591b213SSam Leffler 	sc->sc_tx_timer = 0;
38595591b213SSam Leffler }
38605591b213SSam Leffler 
38615591b213SSam Leffler /*
38625591b213SSam Leffler  * Disable the receive h/w in preparation for a reset.
38635591b213SSam Leffler  */
38645591b213SSam Leffler static void
38655591b213SSam Leffler ath_stoprecv(struct ath_softc *sc)
38665591b213SSam Leffler {
38678cec0ab9SSam Leffler #define	PA2DESC(_sc, _pa) \
3868c42a7b7eSSam Leffler 	((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
3869c42a7b7eSSam Leffler 		((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
38705591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
38715591b213SSam Leffler 
38725591b213SSam Leffler 	ath_hal_stoppcurecv(ah);	/* disable PCU */
38735591b213SSam Leffler 	ath_hal_setrxfilter(ah, 0);	/* clear recv filter */
38745591b213SSam Leffler 	ath_hal_stopdmarecv(ah);	/* disable DMA engine */
3875c42a7b7eSSam Leffler 	DELAY(3000);			/* 3ms is long enough for 1 frame */
38765591b213SSam Leffler #ifdef AR_DEBUG
3877c42a7b7eSSam Leffler 	if (sc->sc_debug & (ATH_DEBUG_RESET | ATH_DEBUG_FATAL)) {
38785591b213SSam Leffler 		struct ath_buf *bf;
38795591b213SSam Leffler 
3880e325e530SSam Leffler 		printf("%s: rx queue %p, link %p\n", __func__,
388130310634SPeter Wemm 			(caddr_t)(uintptr_t) ath_hal_getrxbuf(ah), sc->sc_rxlink);
3882c42a7b7eSSam Leffler 		STAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
38838cec0ab9SSam Leffler 			struct ath_desc *ds = bf->bf_desc;
3884c42a7b7eSSam Leffler 			HAL_STATUS status = ath_hal_rxprocdesc(ah, ds,
3885c42a7b7eSSam Leffler 				bf->bf_daddr, PA2DESC(sc, ds->ds_link));
3886c42a7b7eSSam Leffler 			if (status == HAL_OK || (sc->sc_debug & ATH_DEBUG_FATAL))
3887c42a7b7eSSam Leffler 				ath_printrxbuf(bf, status == HAL_OK);
38885591b213SSam Leffler 		}
38895591b213SSam Leffler 	}
38905591b213SSam Leffler #endif
38915591b213SSam Leffler 	sc->sc_rxlink = NULL;		/* just in case */
38928cec0ab9SSam Leffler #undef PA2DESC
38935591b213SSam Leffler }
38945591b213SSam Leffler 
38955591b213SSam Leffler /*
38965591b213SSam Leffler  * Enable the receive h/w following a reset.
38975591b213SSam Leffler  */
38985591b213SSam Leffler static int
38995591b213SSam Leffler ath_startrecv(struct ath_softc *sc)
39005591b213SSam Leffler {
39015591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
39025591b213SSam Leffler 	struct ath_buf *bf;
39035591b213SSam Leffler 
39045591b213SSam Leffler 	sc->sc_rxlink = NULL;
3905c42a7b7eSSam Leffler 	STAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
39065591b213SSam Leffler 		int error = ath_rxbuf_init(sc, bf);
39075591b213SSam Leffler 		if (error != 0) {
3908c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_RECV,
3909c42a7b7eSSam Leffler 				"%s: ath_rxbuf_init failed %d\n",
3910c42a7b7eSSam Leffler 				__func__, error);
39115591b213SSam Leffler 			return error;
39125591b213SSam Leffler 		}
39135591b213SSam Leffler 	}
39145591b213SSam Leffler 
3915c42a7b7eSSam Leffler 	bf = STAILQ_FIRST(&sc->sc_rxbuf);
39165591b213SSam Leffler 	ath_hal_putrxbuf(ah, bf->bf_daddr);
39175591b213SSam Leffler 	ath_hal_rxena(ah);		/* enable recv descriptors */
39185591b213SSam Leffler 	ath_mode_init(sc);		/* set filters, etc. */
39195591b213SSam Leffler 	ath_hal_startpcurecv(ah);	/* re-enable PCU/DMA engine */
39205591b213SSam Leffler 	return 0;
39215591b213SSam Leffler }
39225591b213SSam Leffler 
39235591b213SSam Leffler /*
3924c42a7b7eSSam Leffler  * Update internal state after a channel change.
3925c42a7b7eSSam Leffler  */
3926c42a7b7eSSam Leffler static void
3927c42a7b7eSSam Leffler ath_chan_change(struct ath_softc *sc, struct ieee80211_channel *chan)
3928c42a7b7eSSam Leffler {
3929c42a7b7eSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
3930c42a7b7eSSam Leffler 	enum ieee80211_phymode mode;
393116b4851aSSam Leffler 	u_int16_t flags;
3932c42a7b7eSSam Leffler 
3933c42a7b7eSSam Leffler 	/*
3934c42a7b7eSSam Leffler 	 * Change channels and update the h/w rate map
3935c42a7b7eSSam Leffler 	 * if we're switching; e.g. 11a to 11b/g.
3936c42a7b7eSSam Leffler 	 */
3937c42a7b7eSSam Leffler 	mode = ieee80211_chan2mode(ic, chan);
3938c42a7b7eSSam Leffler 	if (mode != sc->sc_curmode)
3939c42a7b7eSSam Leffler 		ath_setcurmode(sc, mode);
3940c42a7b7eSSam Leffler 	/*
394116b4851aSSam Leffler 	 * Update BPF state.  NB: ethereal et. al. don't handle
394216b4851aSSam Leffler 	 * merged flags well so pick a unique mode for their use.
3943c42a7b7eSSam Leffler 	 */
394416b4851aSSam Leffler 	if (IEEE80211_IS_CHAN_A(chan))
394516b4851aSSam Leffler 		flags = IEEE80211_CHAN_A;
394616b4851aSSam Leffler 	/* XXX 11g schizophrenia */
394716b4851aSSam Leffler 	else if (IEEE80211_IS_CHAN_G(chan) ||
394816b4851aSSam Leffler 	    IEEE80211_IS_CHAN_PUREG(chan))
394916b4851aSSam Leffler 		flags = IEEE80211_CHAN_G;
395016b4851aSSam Leffler 	else
395116b4851aSSam Leffler 		flags = IEEE80211_CHAN_B;
395216b4851aSSam Leffler 	if (IEEE80211_IS_CHAN_T(chan))
395316b4851aSSam Leffler 		flags |= IEEE80211_CHAN_TURBO;
3954c42a7b7eSSam Leffler 	sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
3955c42a7b7eSSam Leffler 		htole16(chan->ic_freq);
3956c42a7b7eSSam Leffler 	sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =
395716b4851aSSam Leffler 		htole16(flags);
3958c42a7b7eSSam Leffler }
3959c42a7b7eSSam Leffler 
3960c42a7b7eSSam Leffler /*
39615591b213SSam Leffler  * Set/change channels.  If the channel is really being changed,
3962c42a7b7eSSam Leffler  * it's done by reseting the chip.  To accomplish this we must
39635591b213SSam Leffler  * first cleanup any pending DMA, then restart stuff after a la
39645591b213SSam Leffler  * ath_init.
39655591b213SSam Leffler  */
39665591b213SSam Leffler static int
39675591b213SSam Leffler ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan)
39685591b213SSam Leffler {
39695591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
39705591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
39715591b213SSam Leffler 	HAL_CHANNEL hchan;
3972c42a7b7eSSam Leffler 
3973c42a7b7eSSam Leffler 	/*
3974c42a7b7eSSam Leffler 	 * Convert to a HAL channel description with
3975c42a7b7eSSam Leffler 	 * the flags constrained to reflect the current
3976c42a7b7eSSam Leffler 	 * operating mode.
3977c42a7b7eSSam Leffler 	 */
3978c42a7b7eSSam Leffler 	hchan.channel = chan->ic_freq;
3979c42a7b7eSSam Leffler 	hchan.channelFlags = ath_chan2flags(ic, chan);
3980c42a7b7eSSam Leffler 
3981c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %u (%u MHz) -> %u (%u MHz)\n",
3982c42a7b7eSSam Leffler 	    __func__,
3983c42a7b7eSSam Leffler 	    ath_hal_mhz2ieee(sc->sc_curchan.channel,
3984c42a7b7eSSam Leffler 		sc->sc_curchan.channelFlags),
3985c42a7b7eSSam Leffler 	    	sc->sc_curchan.channel,
3986c42a7b7eSSam Leffler 	    ath_hal_mhz2ieee(hchan.channel, hchan.channelFlags), hchan.channel);
3987c42a7b7eSSam Leffler 	if (hchan.channel != sc->sc_curchan.channel ||
3988c42a7b7eSSam Leffler 	    hchan.channelFlags != sc->sc_curchan.channelFlags) {
3989c42a7b7eSSam Leffler 		HAL_STATUS status;
39905591b213SSam Leffler 
39915591b213SSam Leffler 		/*
39925591b213SSam Leffler 		 * To switch channels clear any pending DMA operations;
39935591b213SSam Leffler 		 * wait long enough for the RX fifo to drain, reset the
39945591b213SSam Leffler 		 * hardware at the new frequency, and then re-enable
39955591b213SSam Leffler 		 * the relevant bits of the h/w.
39965591b213SSam Leffler 		 */
39975591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
39985591b213SSam Leffler 		ath_draintxq(sc);		/* clear pending tx frames */
39995591b213SSam Leffler 		ath_stoprecv(sc);		/* turn off frame recv */
40005591b213SSam Leffler 		if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_TRUE, &status)) {
4001c42a7b7eSSam Leffler 			if_printf(ic->ic_ifp, "ath_chan_set: unable to reset "
40025591b213SSam Leffler 				"channel %u (%u Mhz)\n",
40035591b213SSam Leffler 				ieee80211_chan2ieee(ic, chan), chan->ic_freq);
40045591b213SSam Leffler 			return EIO;
40055591b213SSam Leffler 		}
4006c42a7b7eSSam Leffler 		sc->sc_curchan = hchan;
4007c42a7b7eSSam Leffler 		ath_update_txpow(sc);		/* update tx power state */
4008c59005e9SSam Leffler 		sc->sc_diversity = ath_hal_getdiversity(ah);
4009c42a7b7eSSam Leffler 
40105591b213SSam Leffler 		/*
40115591b213SSam Leffler 		 * Re-enable rx framework.
40125591b213SSam Leffler 		 */
40135591b213SSam Leffler 		if (ath_startrecv(sc) != 0) {
4014c42a7b7eSSam Leffler 			if_printf(ic->ic_ifp,
40155591b213SSam Leffler 				"ath_chan_set: unable to restart recv logic\n");
40165591b213SSam Leffler 			return EIO;
40175591b213SSam Leffler 		}
40185591b213SSam Leffler 
40195591b213SSam Leffler 		/*
40205591b213SSam Leffler 		 * Change channels and update the h/w rate map
40215591b213SSam Leffler 		 * if we're switching; e.g. 11a to 11b/g.
40225591b213SSam Leffler 		 */
40235591b213SSam Leffler 		ic->ic_ibss_chan = chan;
4024c42a7b7eSSam Leffler 		ath_chan_change(sc, chan);
40250a915fadSSam Leffler 
40260a915fadSSam Leffler 		/*
40270a915fadSSam Leffler 		 * Re-enable interrupts.
40280a915fadSSam Leffler 		 */
40290a915fadSSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
40305591b213SSam Leffler 	}
40315591b213SSam Leffler 	return 0;
40325591b213SSam Leffler }
40335591b213SSam Leffler 
40345591b213SSam Leffler static void
40355591b213SSam Leffler ath_next_scan(void *arg)
40365591b213SSam Leffler {
40375591b213SSam Leffler 	struct ath_softc *sc = arg;
40385591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
40395591b213SSam Leffler 
40405591b213SSam Leffler 	if (ic->ic_state == IEEE80211_S_SCAN)
4041c42a7b7eSSam Leffler 		ieee80211_next_scan(ic);
40425591b213SSam Leffler }
40435591b213SSam Leffler 
40445591b213SSam Leffler /*
40455591b213SSam Leffler  * Periodically recalibrate the PHY to account
40465591b213SSam Leffler  * for temperature/environment changes.
40475591b213SSam Leffler  */
40485591b213SSam Leffler static void
40495591b213SSam Leffler ath_calibrate(void *arg)
40505591b213SSam Leffler {
40515591b213SSam Leffler 	struct ath_softc *sc = arg;
40525591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
40535591b213SSam Leffler 
40545591b213SSam Leffler 	sc->sc_stats.ast_per_cal++;
40555591b213SSam Leffler 
4056c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: channel %u/%x\n",
4057c42a7b7eSSam Leffler 		__func__, sc->sc_curchan.channel, sc->sc_curchan.channelFlags);
40585591b213SSam Leffler 
40595591b213SSam Leffler 	if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) {
40605591b213SSam Leffler 		/*
40615591b213SSam Leffler 		 * Rfgain is out of bounds, reset the chip
40625591b213SSam Leffler 		 * to load new gain values.
40635591b213SSam Leffler 		 */
40645591b213SSam Leffler 		sc->sc_stats.ast_per_rfgain++;
4065fc74a9f9SBrooks Davis 		ath_reset(sc->sc_ifp);
40665591b213SSam Leffler 	}
4067c42a7b7eSSam Leffler 	if (!ath_hal_calibrate(ah, &sc->sc_curchan)) {
4068c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY,
4069c42a7b7eSSam Leffler 			"%s: calibration of channel %u failed\n",
4070c42a7b7eSSam Leffler 			__func__, sc->sc_curchan.channel);
40715591b213SSam Leffler 		sc->sc_stats.ast_per_calfail++;
40725591b213SSam Leffler 	}
4073c42a7b7eSSam Leffler 	callout_reset(&sc->sc_cal_ch, ath_calinterval * hz, ath_calibrate, sc);
40745591b213SSam Leffler }
40755591b213SSam Leffler 
40765591b213SSam Leffler static int
407745bbf62fSSam Leffler ath_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
40785591b213SSam Leffler {
4079c42a7b7eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
408045bbf62fSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
408145bbf62fSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
40825591b213SSam Leffler 	struct ieee80211_node *ni;
40835591b213SSam Leffler 	int i, error;
40848cec0ab9SSam Leffler 	const u_int8_t *bssid;
40855591b213SSam Leffler 	u_int32_t rfilt;
40865591b213SSam Leffler 	static const HAL_LED_STATE leds[] = {
40875591b213SSam Leffler 	    HAL_LED_INIT,	/* IEEE80211_S_INIT */
40885591b213SSam Leffler 	    HAL_LED_SCAN,	/* IEEE80211_S_SCAN */
40895591b213SSam Leffler 	    HAL_LED_AUTH,	/* IEEE80211_S_AUTH */
40905591b213SSam Leffler 	    HAL_LED_ASSOC, 	/* IEEE80211_S_ASSOC */
40915591b213SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_RUN */
40925591b213SSam Leffler 	};
40935591b213SSam Leffler 
4094c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: %s -> %s\n", __func__,
409545bbf62fSSam Leffler 		ieee80211_state_name[ic->ic_state],
4096c42a7b7eSSam Leffler 		ieee80211_state_name[nstate]);
40975591b213SSam Leffler 
4098c42a7b7eSSam Leffler 	callout_stop(&sc->sc_scan_ch);
4099c42a7b7eSSam Leffler 	callout_stop(&sc->sc_cal_ch);
41005591b213SSam Leffler 	ath_hal_setledstate(ah, leds[nstate]);	/* set LED */
41015591b213SSam Leffler 
41025591b213SSam Leffler 	if (nstate == IEEE80211_S_INIT) {
41035591b213SSam Leffler 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
41044c24deacSSam Leffler 		/*
41054c24deacSSam Leffler 		 * NB: disable interrupts so we don't rx frames.
41064c24deacSSam Leffler 		 */
4107e8fd88a3SSam Leffler 		ath_hal_intrset(ah, sc->sc_imask &~ HAL_INT_GLOBAL);
4108c42a7b7eSSam Leffler 		/*
4109c42a7b7eSSam Leffler 		 * Notify the rate control algorithm.
4110c42a7b7eSSam Leffler 		 */
4111c42a7b7eSSam Leffler 		ath_rate_newstate(sc, nstate);
4112c42a7b7eSSam Leffler 		goto done;
41135591b213SSam Leffler 	}
41145591b213SSam Leffler 	ni = ic->ic_bss;
4115b5c99415SSam Leffler 	error = ath_chan_set(sc, ic->ic_curchan);
41165591b213SSam Leffler 	if (error != 0)
41175591b213SSam Leffler 		goto bad;
4118c42a7b7eSSam Leffler 	rfilt = ath_calcrxfilter(sc, nstate);
4119c42a7b7eSSam Leffler 	if (nstate == IEEE80211_S_SCAN)
41205591b213SSam Leffler 		bssid = ifp->if_broadcastaddr;
4121c42a7b7eSSam Leffler 	else
41225591b213SSam Leffler 		bssid = ni->ni_bssid;
41235591b213SSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
4124c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s\n",
4125c42a7b7eSSam Leffler 		 __func__, rfilt, ether_sprintf(bssid));
41265591b213SSam Leffler 
41275591b213SSam Leffler 	if (nstate == IEEE80211_S_RUN && ic->ic_opmode == IEEE80211_M_STA)
41285591b213SSam Leffler 		ath_hal_setassocid(ah, bssid, ni->ni_associd);
41295591b213SSam Leffler 	else
41305591b213SSam Leffler 		ath_hal_setassocid(ah, bssid, 0);
4131c42a7b7eSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
41325591b213SSam Leffler 		for (i = 0; i < IEEE80211_WEP_NKID; i++)
41335591b213SSam Leffler 			if (ath_hal_keyisvalid(ah, i))
41345591b213SSam Leffler 				ath_hal_keysetmac(ah, i, bssid);
41355591b213SSam Leffler 	}
41365591b213SSam Leffler 
4137c42a7b7eSSam Leffler 	/*
4138c42a7b7eSSam Leffler 	 * Notify the rate control algorithm so rates
4139c42a7b7eSSam Leffler 	 * are setup should ath_beacon_alloc be called.
4140c42a7b7eSSam Leffler 	 */
4141c42a7b7eSSam Leffler 	ath_rate_newstate(sc, nstate);
4142c42a7b7eSSam Leffler 
4143c42a7b7eSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_MONITOR) {
4144c42a7b7eSSam Leffler 		/* nothing to do */;
4145c42a7b7eSSam Leffler 	} else if (nstate == IEEE80211_S_RUN) {
4146c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_STATE,
4147c42a7b7eSSam Leffler 			"%s(RUN): ic_flags=0x%08x iv=%d bssid=%s "
41485591b213SSam Leffler 			"capinfo=0x%04x chan=%d\n"
41495591b213SSam Leffler 			 , __func__
41505591b213SSam Leffler 			 , ic->ic_flags
41515591b213SSam Leffler 			 , ni->ni_intval
41525591b213SSam Leffler 			 , ether_sprintf(ni->ni_bssid)
41535591b213SSam Leffler 			 , ni->ni_capinfo
4154b5c99415SSam Leffler 			 , ieee80211_chan2ieee(ic, ic->ic_curchan));
41555591b213SSam Leffler 
4156e8fd88a3SSam Leffler 		switch (ic->ic_opmode) {
4157e8fd88a3SSam Leffler 		case IEEE80211_M_HOSTAP:
4158e8fd88a3SSam Leffler 		case IEEE80211_M_IBSS:
41595591b213SSam Leffler 			/*
4160e8fd88a3SSam Leffler 			 * Allocate and setup the beacon frame.
4161e8fd88a3SSam Leffler 			 *
4162f818612bSSam Leffler 			 * Stop any previous beacon DMA.  This may be
4163f818612bSSam Leffler 			 * necessary, for example, when an ibss merge
4164f818612bSSam Leffler 			 * causes reconfiguration; there will be a state
4165f818612bSSam Leffler 			 * transition from RUN->RUN that means we may
4166f818612bSSam Leffler 			 * be called with beacon transmission active.
4167f818612bSSam Leffler 			 */
4168f818612bSSam Leffler 			ath_hal_stoptxdma(ah, sc->sc_bhalq);
4169f818612bSSam Leffler 			ath_beacon_free(sc);
41705591b213SSam Leffler 			error = ath_beacon_alloc(sc, ni);
41715591b213SSam Leffler 			if (error != 0)
41725591b213SSam Leffler 				goto bad;
4173e8fd88a3SSam Leffler 			break;
4174e8fd88a3SSam Leffler 		case IEEE80211_M_STA:
4175e8fd88a3SSam Leffler 			/*
4176e8fd88a3SSam Leffler 			 * Allocate a key cache slot to the station.
4177e8fd88a3SSam Leffler 			 */
4178e8fd88a3SSam Leffler 			if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0 &&
4179e8fd88a3SSam Leffler 			    sc->sc_hasclrkey &&
4180e8fd88a3SSam Leffler 			    ni->ni_ucastkey.wk_keyix == IEEE80211_KEYIX_NONE)
4181e8fd88a3SSam Leffler 				ath_setup_stationkey(ni);
4182e8fd88a3SSam Leffler 			break;
4183e8fd88a3SSam Leffler 		default:
4184e8fd88a3SSam Leffler 			break;
41855591b213SSam Leffler 		}
41865591b213SSam Leffler 
41875591b213SSam Leffler 		/*
41885591b213SSam Leffler 		 * Configure the beacon and sleep timers.
41895591b213SSam Leffler 		 */
41905591b213SSam Leffler 		ath_beacon_config(sc);
41915591b213SSam Leffler 	} else {
4192c42a7b7eSSam Leffler 		ath_hal_intrset(ah,
4193c42a7b7eSSam Leffler 			sc->sc_imask &~ (HAL_INT_SWBA | HAL_INT_BMISS));
41945591b213SSam Leffler 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
41955591b213SSam Leffler 	}
4196c42a7b7eSSam Leffler done:
419745bbf62fSSam Leffler 	/*
419845bbf62fSSam Leffler 	 * Invoke the parent method to complete the work.
419945bbf62fSSam Leffler 	 */
4200c42a7b7eSSam Leffler 	error = sc->sc_newstate(ic, nstate, arg);
4201c42a7b7eSSam Leffler 	/*
4202c42a7b7eSSam Leffler 	 * Finally, start any timers.
4203c42a7b7eSSam Leffler 	 */
4204c42a7b7eSSam Leffler 	if (nstate == IEEE80211_S_RUN) {
4205c42a7b7eSSam Leffler 		/* start periodic recalibration timer */
4206c42a7b7eSSam Leffler 		callout_reset(&sc->sc_cal_ch, ath_calinterval * hz,
4207c42a7b7eSSam Leffler 			ath_calibrate, sc);
4208c42a7b7eSSam Leffler 	} else if (nstate == IEEE80211_S_SCAN) {
4209c42a7b7eSSam Leffler 		/* start ap/neighbor scan timer */
4210c42a7b7eSSam Leffler 		callout_reset(&sc->sc_scan_ch, (ath_dwelltime * hz) / 1000,
4211c42a7b7eSSam Leffler 			ath_next_scan, sc);
4212c42a7b7eSSam Leffler 	}
42135591b213SSam Leffler bad:
42145591b213SSam Leffler 	return error;
42155591b213SSam Leffler }
42165591b213SSam Leffler 
42175591b213SSam Leffler /*
4218e8fd88a3SSam Leffler  * Allocate a key cache slot to the station so we can
4219e8fd88a3SSam Leffler  * setup a mapping from key index to node. The key cache
4220e8fd88a3SSam Leffler  * slot is needed for managing antenna state and for
4221e8fd88a3SSam Leffler  * compression when stations do not use crypto.  We do
4222e8fd88a3SSam Leffler  * it uniliaterally here; if crypto is employed this slot
4223e8fd88a3SSam Leffler  * will be reassigned.
4224e8fd88a3SSam Leffler  */
4225e8fd88a3SSam Leffler static void
4226e8fd88a3SSam Leffler ath_setup_stationkey(struct ieee80211_node *ni)
4227e8fd88a3SSam Leffler {
4228e8fd88a3SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
4229e8fd88a3SSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
4230c1225b52SSam Leffler 	ieee80211_keyix keyix, rxkeyix;
4231e8fd88a3SSam Leffler 
4232c1225b52SSam Leffler 	if (!ath_key_alloc(ic, &ni->ni_ucastkey, &keyix, &rxkeyix)) {
4233e8fd88a3SSam Leffler 		/*
4234e8fd88a3SSam Leffler 		 * Key cache is full; we'll fall back to doing
4235e8fd88a3SSam Leffler 		 * the more expensive lookup in software.  Note
4236e8fd88a3SSam Leffler 		 * this also means no h/w compression.
4237e8fd88a3SSam Leffler 		 */
4238e8fd88a3SSam Leffler 		/* XXX msg+statistic */
4239e8fd88a3SSam Leffler 	} else {
4240c1225b52SSam Leffler 		/* XXX locking? */
4241e8fd88a3SSam Leffler 		ni->ni_ucastkey.wk_keyix = keyix;
4242c1225b52SSam Leffler 		ni->ni_ucastkey.wk_rxkeyix = rxkeyix;
4243e8fd88a3SSam Leffler 		/* NB: this will create a pass-thru key entry */
4244e8fd88a3SSam Leffler 		ath_keyset(sc, &ni->ni_ucastkey, ni->ni_macaddr, ic->ic_bss);
4245e8fd88a3SSam Leffler 	}
4246e8fd88a3SSam Leffler }
4247e8fd88a3SSam Leffler 
4248e8fd88a3SSam Leffler /*
42495591b213SSam Leffler  * Setup driver-specific state for a newly associated node.
42505591b213SSam Leffler  * Note that we're called also on a re-associate, the isnew
42515591b213SSam Leffler  * param tells us if this is the first time or not.
42525591b213SSam Leffler  */
42535591b213SSam Leffler static void
4254e9962332SSam Leffler ath_newassoc(struct ieee80211_node *ni, int isnew)
42555591b213SSam Leffler {
4256e9962332SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
4257c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
42585591b213SSam Leffler 
4259c42a7b7eSSam Leffler 	ath_rate_newassoc(sc, ATH_NODE(ni), isnew);
4260e8fd88a3SSam Leffler 	if (isnew &&
4261e8fd88a3SSam Leffler 	    (ic->ic_flags & IEEE80211_F_PRIVACY) == 0 && sc->sc_hasclrkey) {
4262e8fd88a3SSam Leffler 		KASSERT(ni->ni_ucastkey.wk_keyix == IEEE80211_KEYIX_NONE,
4263e8fd88a3SSam Leffler 		    ("new assoc with a unicast key already setup (keyix %u)",
4264e8fd88a3SSam Leffler 		    ni->ni_ucastkey.wk_keyix));
4265e8fd88a3SSam Leffler 		ath_setup_stationkey(ni);
4266e8fd88a3SSam Leffler 	}
42675591b213SSam Leffler }
42685591b213SSam Leffler 
42695591b213SSam Leffler static int
4270c42a7b7eSSam Leffler ath_getchannels(struct ath_softc *sc, u_int cc,
4271c42a7b7eSSam Leffler 	HAL_BOOL outdoor, HAL_BOOL xchanmode)
42725591b213SSam Leffler {
42735591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
4274fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
42755591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
42765591b213SSam Leffler 	HAL_CHANNEL *chans;
42775591b213SSam Leffler 	int i, ix, nchan;
42785591b213SSam Leffler 
42795591b213SSam Leffler 	chans = malloc(IEEE80211_CHAN_MAX * sizeof(HAL_CHANNEL),
42805591b213SSam Leffler 			M_TEMP, M_NOWAIT);
42815591b213SSam Leffler 	if (chans == NULL) {
42825591b213SSam Leffler 		if_printf(ifp, "unable to allocate channel table\n");
42835591b213SSam Leffler 		return ENOMEM;
42845591b213SSam Leffler 	}
42855591b213SSam Leffler 	if (!ath_hal_init_channels(ah, chans, IEEE80211_CHAN_MAX, &nchan,
4286c42a7b7eSSam Leffler 	    cc, HAL_MODE_ALL, outdoor, xchanmode)) {
4287c42a7b7eSSam Leffler 		u_int32_t rd;
4288c42a7b7eSSam Leffler 
4289c42a7b7eSSam Leffler 		ath_hal_getregdomain(ah, &rd);
4290c42a7b7eSSam Leffler 		if_printf(ifp, "unable to collect channel list from hal; "
4291c42a7b7eSSam Leffler 			"regdomain likely %u country code %u\n", rd, cc);
42925591b213SSam Leffler 		free(chans, M_TEMP);
42935591b213SSam Leffler 		return EINVAL;
42945591b213SSam Leffler 	}
42955591b213SSam Leffler 
42965591b213SSam Leffler 	/*
42975591b213SSam Leffler 	 * Convert HAL channels to ieee80211 ones and insert
42985591b213SSam Leffler 	 * them in the table according to their channel number.
42995591b213SSam Leffler 	 */
43005591b213SSam Leffler 	for (i = 0; i < nchan; i++) {
43015591b213SSam Leffler 		HAL_CHANNEL *c = &chans[i];
43025591b213SSam Leffler 		ix = ath_hal_mhz2ieee(c->channel, c->channelFlags);
43035591b213SSam Leffler 		if (ix > IEEE80211_CHAN_MAX) {
43045591b213SSam Leffler 			if_printf(ifp, "bad hal channel %u (%u/%x) ignored\n",
43055591b213SSam Leffler 				ix, c->channel, c->channelFlags);
43065591b213SSam Leffler 			continue;
43075591b213SSam Leffler 		}
43085591b213SSam Leffler 		/* NB: flags are known to be compatible */
43095591b213SSam Leffler 		if (ic->ic_channels[ix].ic_freq == 0) {
43105591b213SSam Leffler 			ic->ic_channels[ix].ic_freq = c->channel;
43115591b213SSam Leffler 			ic->ic_channels[ix].ic_flags = c->channelFlags;
43125591b213SSam Leffler 		} else {
43135591b213SSam Leffler 			/* channels overlap; e.g. 11g and 11b */
43145591b213SSam Leffler 			ic->ic_channels[ix].ic_flags |= c->channelFlags;
43155591b213SSam Leffler 		}
43165591b213SSam Leffler 	}
43175591b213SSam Leffler 	free(chans, M_TEMP);
43185591b213SSam Leffler 	return 0;
43195591b213SSam Leffler }
43205591b213SSam Leffler 
4321c42a7b7eSSam Leffler static void
43223e50ec2cSSam Leffler ath_led_done(void *arg)
4323c42a7b7eSSam Leffler {
43243e50ec2cSSam Leffler 	struct ath_softc *sc = arg;
43253e50ec2cSSam Leffler 
43263e50ec2cSSam Leffler 	sc->sc_blinking = 0;
43273e50ec2cSSam Leffler }
4328c42a7b7eSSam Leffler 
4329c42a7b7eSSam Leffler /*
43303e50ec2cSSam Leffler  * Turn the LED off: flip the pin and then set a timer so no
43313e50ec2cSSam Leffler  * update will happen for the specified duration.
4332c42a7b7eSSam Leffler  */
43333e50ec2cSSam Leffler static void
43343e50ec2cSSam Leffler ath_led_off(void *arg)
43353e50ec2cSSam Leffler {
43363e50ec2cSSam Leffler 	struct ath_softc *sc = arg;
43373e50ec2cSSam Leffler 
43383e50ec2cSSam Leffler 	ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, !sc->sc_ledon);
43393e50ec2cSSam Leffler 	callout_reset(&sc->sc_ledtimer, sc->sc_ledoff, ath_led_done, sc);
4340c42a7b7eSSam Leffler }
43413e50ec2cSSam Leffler 
43423e50ec2cSSam Leffler /*
43433e50ec2cSSam Leffler  * Blink the LED according to the specified on/off times.
43443e50ec2cSSam Leffler  */
43453e50ec2cSSam Leffler static void
43463e50ec2cSSam Leffler ath_led_blink(struct ath_softc *sc, int on, int off)
43473e50ec2cSSam Leffler {
43483e50ec2cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_LED, "%s: on %u off %u\n", __func__, on, off);
43493e50ec2cSSam Leffler 	ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, sc->sc_ledon);
43503e50ec2cSSam Leffler 	sc->sc_blinking = 1;
43513e50ec2cSSam Leffler 	sc->sc_ledoff = off;
43523e50ec2cSSam Leffler 	callout_reset(&sc->sc_ledtimer, on, ath_led_off, sc);
43533e50ec2cSSam Leffler }
43543e50ec2cSSam Leffler 
43553e50ec2cSSam Leffler static void
43563e50ec2cSSam Leffler ath_led_event(struct ath_softc *sc, int event)
43573e50ec2cSSam Leffler {
43583e50ec2cSSam Leffler 
43593e50ec2cSSam Leffler 	sc->sc_ledevent = ticks;	/* time of last event */
43603e50ec2cSSam Leffler 	if (sc->sc_blinking)		/* don't interrupt active blink */
43613e50ec2cSSam Leffler 		return;
43623e50ec2cSSam Leffler 	switch (event) {
43633e50ec2cSSam Leffler 	case ATH_LED_POLL:
43643e50ec2cSSam Leffler 		ath_led_blink(sc, sc->sc_hwmap[0].ledon,
43653e50ec2cSSam Leffler 			sc->sc_hwmap[0].ledoff);
43663e50ec2cSSam Leffler 		break;
43673e50ec2cSSam Leffler 	case ATH_LED_TX:
43683e50ec2cSSam Leffler 		ath_led_blink(sc, sc->sc_hwmap[sc->sc_txrate].ledon,
43693e50ec2cSSam Leffler 			sc->sc_hwmap[sc->sc_txrate].ledoff);
43703e50ec2cSSam Leffler 		break;
43713e50ec2cSSam Leffler 	case ATH_LED_RX:
43723e50ec2cSSam Leffler 		ath_led_blink(sc, sc->sc_hwmap[sc->sc_rxrate].ledon,
43733e50ec2cSSam Leffler 			sc->sc_hwmap[sc->sc_rxrate].ledoff);
43743e50ec2cSSam Leffler 		break;
4375c42a7b7eSSam Leffler 	}
4376c42a7b7eSSam Leffler }
4377c42a7b7eSSam Leffler 
4378c42a7b7eSSam Leffler static void
4379c42a7b7eSSam Leffler ath_update_txpow(struct ath_softc *sc)
4380c42a7b7eSSam Leffler {
4381c42a7b7eSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
4382c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4383c42a7b7eSSam Leffler 	u_int32_t txpow;
4384c42a7b7eSSam Leffler 
4385c42a7b7eSSam Leffler 	if (sc->sc_curtxpow != ic->ic_txpowlimit) {
4386c42a7b7eSSam Leffler 		ath_hal_settxpowlimit(ah, ic->ic_txpowlimit);
4387c42a7b7eSSam Leffler 		/* read back in case value is clamped */
4388c42a7b7eSSam Leffler 		ath_hal_gettxpowlimit(ah, &txpow);
4389c42a7b7eSSam Leffler 		ic->ic_txpowlimit = sc->sc_curtxpow = txpow;
4390c42a7b7eSSam Leffler 	}
4391c42a7b7eSSam Leffler 	/*
4392c42a7b7eSSam Leffler 	 * Fetch max tx power level for status requests.
4393c42a7b7eSSam Leffler 	 */
4394c42a7b7eSSam Leffler 	ath_hal_getmaxtxpow(sc->sc_ah, &txpow);
4395c42a7b7eSSam Leffler 	ic->ic_bss->ni_txpower = txpow;
4396c42a7b7eSSam Leffler }
4397c42a7b7eSSam Leffler 
43985591b213SSam Leffler static int
43995591b213SSam Leffler ath_rate_setup(struct ath_softc *sc, u_int mode)
44005591b213SSam Leffler {
44015591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
44025591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
44035591b213SSam Leffler 	const HAL_RATE_TABLE *rt;
44045591b213SSam Leffler 	struct ieee80211_rateset *rs;
44055591b213SSam Leffler 	int i, maxrates;
44065591b213SSam Leffler 
44075591b213SSam Leffler 	switch (mode) {
44085591b213SSam Leffler 	case IEEE80211_MODE_11A:
44095591b213SSam Leffler 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11A);
44105591b213SSam Leffler 		break;
44115591b213SSam Leffler 	case IEEE80211_MODE_11B:
44125591b213SSam Leffler 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11B);
44135591b213SSam Leffler 		break;
44145591b213SSam Leffler 	case IEEE80211_MODE_11G:
44155591b213SSam Leffler 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11G);
44165591b213SSam Leffler 		break;
4417c42a7b7eSSam Leffler 	case IEEE80211_MODE_TURBO_A:
44185591b213SSam Leffler 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_TURBO);
44195591b213SSam Leffler 		break;
4420c42a7b7eSSam Leffler 	case IEEE80211_MODE_TURBO_G:
4421c42a7b7eSSam Leffler 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_108G);
4422c42a7b7eSSam Leffler 		break;
44235591b213SSam Leffler 	default:
4424c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid mode %u\n",
4425c42a7b7eSSam Leffler 			__func__, mode);
44265591b213SSam Leffler 		return 0;
44275591b213SSam Leffler 	}
44285591b213SSam Leffler 	rt = sc->sc_rates[mode];
44295591b213SSam Leffler 	if (rt == NULL)
44305591b213SSam Leffler 		return 0;
44315591b213SSam Leffler 	if (rt->rateCount > IEEE80211_RATE_MAXSIZE) {
4432c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY,
4433c42a7b7eSSam Leffler 			"%s: rate table too small (%u > %u)\n",
4434c42a7b7eSSam Leffler 			__func__, rt->rateCount, IEEE80211_RATE_MAXSIZE);
44355591b213SSam Leffler 		maxrates = IEEE80211_RATE_MAXSIZE;
44365591b213SSam Leffler 	} else
44375591b213SSam Leffler 		maxrates = rt->rateCount;
44385591b213SSam Leffler 	rs = &ic->ic_sup_rates[mode];
44395591b213SSam Leffler 	for (i = 0; i < maxrates; i++)
44405591b213SSam Leffler 		rs->rs_rates[i] = rt->info[i].dot11Rate;
44415591b213SSam Leffler 	rs->rs_nrates = maxrates;
44425591b213SSam Leffler 	return 1;
44435591b213SSam Leffler }
44445591b213SSam Leffler 
44455591b213SSam Leffler static void
44465591b213SSam Leffler ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode)
44475591b213SSam Leffler {
44483e50ec2cSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
44493e50ec2cSSam Leffler 	/* NB: on/off times from the Atheros NDIS driver, w/ permission */
44503e50ec2cSSam Leffler 	static const struct {
44513e50ec2cSSam Leffler 		u_int		rate;		/* tx/rx 802.11 rate */
44523e50ec2cSSam Leffler 		u_int16_t	timeOn;		/* LED on time (ms) */
44533e50ec2cSSam Leffler 		u_int16_t	timeOff;	/* LED off time (ms) */
44543e50ec2cSSam Leffler 	} blinkrates[] = {
44553e50ec2cSSam Leffler 		{ 108,  40,  10 },
44563e50ec2cSSam Leffler 		{  96,  44,  11 },
44573e50ec2cSSam Leffler 		{  72,  50,  13 },
44583e50ec2cSSam Leffler 		{  48,  57,  14 },
44593e50ec2cSSam Leffler 		{  36,  67,  16 },
44603e50ec2cSSam Leffler 		{  24,  80,  20 },
44613e50ec2cSSam Leffler 		{  22, 100,  25 },
44623e50ec2cSSam Leffler 		{  18, 133,  34 },
44633e50ec2cSSam Leffler 		{  12, 160,  40 },
44643e50ec2cSSam Leffler 		{  10, 200,  50 },
44653e50ec2cSSam Leffler 		{   6, 240,  58 },
44663e50ec2cSSam Leffler 		{   4, 267,  66 },
44673e50ec2cSSam Leffler 		{   2, 400, 100 },
44683e50ec2cSSam Leffler 		{   0, 500, 130 },
44693e50ec2cSSam Leffler 	};
44705591b213SSam Leffler 	const HAL_RATE_TABLE *rt;
44713e50ec2cSSam Leffler 	int i, j;
44725591b213SSam Leffler 
44735591b213SSam Leffler 	memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap));
44745591b213SSam Leffler 	rt = sc->sc_rates[mode];
44755591b213SSam Leffler 	KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode));
44765591b213SSam Leffler 	for (i = 0; i < rt->rateCount; i++)
44775591b213SSam Leffler 		sc->sc_rixmap[rt->info[i].dot11Rate & IEEE80211_RATE_VAL] = i;
44781b1a8e41SSam Leffler 	memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap));
4479c42a7b7eSSam Leffler 	for (i = 0; i < 32; i++) {
4480c42a7b7eSSam Leffler 		u_int8_t ix = rt->rateCodeToIndex[i];
44813e50ec2cSSam Leffler 		if (ix == 0xff) {
44823e50ec2cSSam Leffler 			sc->sc_hwmap[i].ledon = (500 * hz) / 1000;
44833e50ec2cSSam Leffler 			sc->sc_hwmap[i].ledoff = (130 * hz) / 1000;
448416b4851aSSam Leffler 			continue;
44853e50ec2cSSam Leffler 		}
44863e50ec2cSSam Leffler 		sc->sc_hwmap[i].ieeerate =
44873e50ec2cSSam Leffler 			rt->info[ix].dot11Rate & IEEE80211_RATE_VAL;
4488d3be6f5bSSam Leffler 		sc->sc_hwmap[i].txflags = IEEE80211_RADIOTAP_F_DATAPAD;
448916b4851aSSam Leffler 		if (rt->info[ix].shortPreamble ||
449016b4851aSSam Leffler 		    rt->info[ix].phy == IEEE80211_T_OFDM)
4491d3be6f5bSSam Leffler 			sc->sc_hwmap[i].txflags |= IEEE80211_RADIOTAP_F_SHORTPRE;
4492d3be6f5bSSam Leffler 		/* NB: receive frames include FCS */
4493d3be6f5bSSam Leffler 		sc->sc_hwmap[i].rxflags = sc->sc_hwmap[i].txflags |
4494d3be6f5bSSam Leffler 			IEEE80211_RADIOTAP_F_FCS;
44953e50ec2cSSam Leffler 		/* setup blink rate table to avoid per-packet lookup */
44963e50ec2cSSam Leffler 		for (j = 0; j < N(blinkrates)-1; j++)
44973e50ec2cSSam Leffler 			if (blinkrates[j].rate == sc->sc_hwmap[i].ieeerate)
44983e50ec2cSSam Leffler 				break;
44993e50ec2cSSam Leffler 		/* NB: this uses the last entry if the rate isn't found */
45003e50ec2cSSam Leffler 		/* XXX beware of overlow */
45013e50ec2cSSam Leffler 		sc->sc_hwmap[i].ledon = (blinkrates[j].timeOn * hz) / 1000;
45023e50ec2cSSam Leffler 		sc->sc_hwmap[i].ledoff = (blinkrates[j].timeOff * hz) / 1000;
4503c42a7b7eSSam Leffler 	}
45045591b213SSam Leffler 	sc->sc_currates = rt;
45055591b213SSam Leffler 	sc->sc_curmode = mode;
45065591b213SSam Leffler 	/*
4507c42a7b7eSSam Leffler 	 * All protection frames are transmited at 2Mb/s for
4508c42a7b7eSSam Leffler 	 * 11g, otherwise at 1Mb/s.
4509c42a7b7eSSam Leffler 	 * XXX select protection rate index from rate table.
45105591b213SSam Leffler 	 */
4511c42a7b7eSSam Leffler 	sc->sc_protrix = (mode == IEEE80211_MODE_11G ? 1 : 0);
4512c42a7b7eSSam Leffler 	/* NB: caller is responsible for reseting rate control state */
45133e50ec2cSSam Leffler #undef N
45145591b213SSam Leffler }
45155591b213SSam Leffler 
45165591b213SSam Leffler #ifdef AR_DEBUG
45175591b213SSam Leffler static void
45185591b213SSam Leffler ath_printrxbuf(struct ath_buf *bf, int done)
45195591b213SSam Leffler {
45205591b213SSam Leffler 	struct ath_desc *ds;
45215591b213SSam Leffler 	int i;
45225591b213SSam Leffler 
45235591b213SSam Leffler 	for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) {
45245591b213SSam Leffler 		printf("R%d (%p %p) %08x %08x %08x %08x %08x %08x %c\n",
45255591b213SSam Leffler 		    i, ds, (struct ath_desc *)bf->bf_daddr + i,
45265591b213SSam Leffler 		    ds->ds_link, ds->ds_data,
45275591b213SSam Leffler 		    ds->ds_ctl0, ds->ds_ctl1,
45285591b213SSam Leffler 		    ds->ds_hw[0], ds->ds_hw[1],
45295591b213SSam Leffler 		    !done ? ' ' : (ds->ds_rxstat.rs_status == 0) ? '*' : '!');
45305591b213SSam Leffler 	}
45315591b213SSam Leffler }
45325591b213SSam Leffler 
45335591b213SSam Leffler static void
45345591b213SSam Leffler ath_printtxbuf(struct ath_buf *bf, int done)
45355591b213SSam Leffler {
45365591b213SSam Leffler 	struct ath_desc *ds;
45375591b213SSam Leffler 	int i;
45385591b213SSam Leffler 
45395591b213SSam Leffler 	for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) {
45405591b213SSam Leffler 		printf("T%d (%p %p) %08x %08x %08x %08x %08x %08x %08x %08x %c\n",
45415591b213SSam Leffler 		    i, ds, (struct ath_desc *)bf->bf_daddr + i,
45425591b213SSam Leffler 		    ds->ds_link, ds->ds_data,
45435591b213SSam Leffler 		    ds->ds_ctl0, ds->ds_ctl1,
45445591b213SSam Leffler 		    ds->ds_hw[0], ds->ds_hw[1], ds->ds_hw[2], ds->ds_hw[3],
45455591b213SSam Leffler 		    !done ? ' ' : (ds->ds_txstat.ts_status == 0) ? '*' : '!');
45465591b213SSam Leffler 	}
45475591b213SSam Leffler }
45485591b213SSam Leffler #endif /* AR_DEBUG */
4549c42a7b7eSSam Leffler 
4550c42a7b7eSSam Leffler static void
4551c42a7b7eSSam Leffler ath_watchdog(struct ifnet *ifp)
4552c42a7b7eSSam Leffler {
4553c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
4554c42a7b7eSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
4555c42a7b7eSSam Leffler 
4556c42a7b7eSSam Leffler 	ifp->if_timer = 0;
455713f4c340SRobert Watson 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->sc_invalid)
4558c42a7b7eSSam Leffler 		return;
4559c42a7b7eSSam Leffler 	if (sc->sc_tx_timer) {
4560c42a7b7eSSam Leffler 		if (--sc->sc_tx_timer == 0) {
4561c42a7b7eSSam Leffler 			if_printf(ifp, "device timeout\n");
4562c42a7b7eSSam Leffler 			ath_reset(ifp);
4563c42a7b7eSSam Leffler 			ifp->if_oerrors++;
4564c42a7b7eSSam Leffler 			sc->sc_stats.ast_watchdog++;
4565c42a7b7eSSam Leffler 		} else
4566c42a7b7eSSam Leffler 			ifp->if_timer = 1;
4567c42a7b7eSSam Leffler 	}
4568c42a7b7eSSam Leffler 	ieee80211_watchdog(ic);
4569c42a7b7eSSam Leffler }
4570c42a7b7eSSam Leffler 
4571c42a7b7eSSam Leffler /*
4572c42a7b7eSSam Leffler  * Diagnostic interface to the HAL.  This is used by various
4573c42a7b7eSSam Leffler  * tools to do things like retrieve register contents for
4574c42a7b7eSSam Leffler  * debugging.  The mechanism is intentionally opaque so that
4575c42a7b7eSSam Leffler  * it can change frequently w/o concern for compatiblity.
4576c42a7b7eSSam Leffler  */
4577c42a7b7eSSam Leffler static int
4578c42a7b7eSSam Leffler ath_ioctl_diag(struct ath_softc *sc, struct ath_diag *ad)
4579c42a7b7eSSam Leffler {
4580c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4581c42a7b7eSSam Leffler 	u_int id = ad->ad_id & ATH_DIAG_ID;
4582c42a7b7eSSam Leffler 	void *indata = NULL;
4583c42a7b7eSSam Leffler 	void *outdata = NULL;
4584c42a7b7eSSam Leffler 	u_int32_t insize = ad->ad_in_size;
4585c42a7b7eSSam Leffler 	u_int32_t outsize = ad->ad_out_size;
4586c42a7b7eSSam Leffler 	int error = 0;
4587c42a7b7eSSam Leffler 
4588c42a7b7eSSam Leffler 	if (ad->ad_id & ATH_DIAG_IN) {
4589c42a7b7eSSam Leffler 		/*
4590c42a7b7eSSam Leffler 		 * Copy in data.
4591c42a7b7eSSam Leffler 		 */
4592c42a7b7eSSam Leffler 		indata = malloc(insize, M_TEMP, M_NOWAIT);
4593c42a7b7eSSam Leffler 		if (indata == NULL) {
4594c42a7b7eSSam Leffler 			error = ENOMEM;
4595c42a7b7eSSam Leffler 			goto bad;
4596c42a7b7eSSam Leffler 		}
4597c42a7b7eSSam Leffler 		error = copyin(ad->ad_in_data, indata, insize);
4598c42a7b7eSSam Leffler 		if (error)
4599c42a7b7eSSam Leffler 			goto bad;
4600c42a7b7eSSam Leffler 	}
4601c42a7b7eSSam Leffler 	if (ad->ad_id & ATH_DIAG_DYN) {
4602c42a7b7eSSam Leffler 		/*
4603c42a7b7eSSam Leffler 		 * Allocate a buffer for the results (otherwise the HAL
4604c42a7b7eSSam Leffler 		 * returns a pointer to a buffer where we can read the
4605c42a7b7eSSam Leffler 		 * results).  Note that we depend on the HAL leaving this
4606c42a7b7eSSam Leffler 		 * pointer for us to use below in reclaiming the buffer;
4607c42a7b7eSSam Leffler 		 * may want to be more defensive.
4608c42a7b7eSSam Leffler 		 */
4609c42a7b7eSSam Leffler 		outdata = malloc(outsize, M_TEMP, M_NOWAIT);
4610c42a7b7eSSam Leffler 		if (outdata == NULL) {
4611c42a7b7eSSam Leffler 			error = ENOMEM;
4612c42a7b7eSSam Leffler 			goto bad;
4613c42a7b7eSSam Leffler 		}
4614c42a7b7eSSam Leffler 	}
4615c42a7b7eSSam Leffler 	if (ath_hal_getdiagstate(ah, id, indata, insize, &outdata, &outsize)) {
4616c42a7b7eSSam Leffler 		if (outsize < ad->ad_out_size)
4617c42a7b7eSSam Leffler 			ad->ad_out_size = outsize;
4618c42a7b7eSSam Leffler 		if (outdata != NULL)
4619c42a7b7eSSam Leffler 			error = copyout(outdata, ad->ad_out_data,
4620c42a7b7eSSam Leffler 					ad->ad_out_size);
4621c42a7b7eSSam Leffler 	} else {
4622c42a7b7eSSam Leffler 		error = EINVAL;
4623c42a7b7eSSam Leffler 	}
4624c42a7b7eSSam Leffler bad:
4625c42a7b7eSSam Leffler 	if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
4626c42a7b7eSSam Leffler 		free(indata, M_TEMP);
4627c42a7b7eSSam Leffler 	if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
4628c42a7b7eSSam Leffler 		free(outdata, M_TEMP);
4629c42a7b7eSSam Leffler 	return error;
4630c42a7b7eSSam Leffler }
4631c42a7b7eSSam Leffler 
4632c42a7b7eSSam Leffler static int
4633c42a7b7eSSam Leffler ath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
4634c42a7b7eSSam Leffler {
4635c42a7b7eSSam Leffler #define	IS_RUNNING(ifp) \
463613f4c340SRobert Watson 	((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
4637c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
4638c42a7b7eSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
4639c42a7b7eSSam Leffler 	struct ifreq *ifr = (struct ifreq *)data;
4640c42a7b7eSSam Leffler 	int error = 0;
4641c42a7b7eSSam Leffler 
4642c42a7b7eSSam Leffler 	ATH_LOCK(sc);
4643c42a7b7eSSam Leffler 	switch (cmd) {
4644c42a7b7eSSam Leffler 	case SIOCSIFFLAGS:
4645c42a7b7eSSam Leffler 		if (IS_RUNNING(ifp)) {
4646c42a7b7eSSam Leffler 			/*
4647c42a7b7eSSam Leffler 			 * To avoid rescanning another access point,
4648c42a7b7eSSam Leffler 			 * do not call ath_init() here.  Instead,
4649c42a7b7eSSam Leffler 			 * only reflect promisc mode settings.
4650c42a7b7eSSam Leffler 			 */
4651c42a7b7eSSam Leffler 			ath_mode_init(sc);
4652c42a7b7eSSam Leffler 		} else if (ifp->if_flags & IFF_UP) {
4653c42a7b7eSSam Leffler 			/*
4654c42a7b7eSSam Leffler 			 * Beware of being called during attach/detach
4655c42a7b7eSSam Leffler 			 * to reset promiscuous mode.  In that case we
4656c42a7b7eSSam Leffler 			 * will still be marked UP but not RUNNING.
4657c42a7b7eSSam Leffler 			 * However trying to re-init the interface
4658c42a7b7eSSam Leffler 			 * is the wrong thing to do as we've already
4659c42a7b7eSSam Leffler 			 * torn down much of our state.  There's
4660c42a7b7eSSam Leffler 			 * probably a better way to deal with this.
4661c42a7b7eSSam Leffler 			 */
4662c42a7b7eSSam Leffler 			if (!sc->sc_invalid && ic->ic_bss != NULL)
4663fc74a9f9SBrooks Davis 				ath_init(sc);	/* XXX lose error */
4664c42a7b7eSSam Leffler 		} else
4665c42a7b7eSSam Leffler 			ath_stop_locked(ifp);
4666c42a7b7eSSam Leffler 		break;
4667c42a7b7eSSam Leffler 	case SIOCADDMULTI:
4668c42a7b7eSSam Leffler 	case SIOCDELMULTI:
4669c42a7b7eSSam Leffler 		/*
4670c42a7b7eSSam Leffler 		 * The upper layer has already installed/removed
4671c42a7b7eSSam Leffler 		 * the multicast address(es), just recalculate the
4672c42a7b7eSSam Leffler 		 * multicast filter for the card.
4673c42a7b7eSSam Leffler 		 */
467413f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
4675c42a7b7eSSam Leffler 			ath_mode_init(sc);
4676c42a7b7eSSam Leffler 		break;
4677c42a7b7eSSam Leffler 	case SIOCGATHSTATS:
4678c42a7b7eSSam Leffler 		/* NB: embed these numbers to get a consistent view */
4679c42a7b7eSSam Leffler 		sc->sc_stats.ast_tx_packets = ifp->if_opackets;
4680c42a7b7eSSam Leffler 		sc->sc_stats.ast_rx_packets = ifp->if_ipackets;
4681c42a7b7eSSam Leffler 		sc->sc_stats.ast_rx_rssi = ieee80211_getrssi(ic);
4682c42a7b7eSSam Leffler 		ATH_UNLOCK(sc);
4683c42a7b7eSSam Leffler 		/*
4684c42a7b7eSSam Leffler 		 * NB: Drop the softc lock in case of a page fault;
4685c42a7b7eSSam Leffler 		 * we'll accept any potential inconsisentcy in the
4686c42a7b7eSSam Leffler 		 * statistics.  The alternative is to copy the data
4687c42a7b7eSSam Leffler 		 * to a local structure.
4688c42a7b7eSSam Leffler 		 */
4689c42a7b7eSSam Leffler 		return copyout(&sc->sc_stats,
4690c42a7b7eSSam Leffler 				ifr->ifr_data, sizeof (sc->sc_stats));
4691c42a7b7eSSam Leffler 	case SIOCGATHDIAG:
4692c42a7b7eSSam Leffler 		error = ath_ioctl_diag(sc, (struct ath_diag *) ifr);
4693c42a7b7eSSam Leffler 		break;
4694c42a7b7eSSam Leffler 	default:
4695c42a7b7eSSam Leffler 		error = ieee80211_ioctl(ic, cmd, data);
4696c42a7b7eSSam Leffler 		if (error == ENETRESET) {
4697c42a7b7eSSam Leffler 			if (IS_RUNNING(ifp) &&
4698c42a7b7eSSam Leffler 			    ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
4699fc74a9f9SBrooks Davis 				ath_init(sc);	/* XXX lose error */
4700c42a7b7eSSam Leffler 			error = 0;
4701c42a7b7eSSam Leffler 		}
4702c42a7b7eSSam Leffler 		if (error == ERESTART)
4703c42a7b7eSSam Leffler 			error = IS_RUNNING(ifp) ? ath_reset(ifp) : 0;
4704c42a7b7eSSam Leffler 		break;
4705c42a7b7eSSam Leffler 	}
4706c42a7b7eSSam Leffler 	ATH_UNLOCK(sc);
4707c42a7b7eSSam Leffler 	return error;
4708a614e076SSam Leffler #undef IS_RUNNING
4709c42a7b7eSSam Leffler }
4710c42a7b7eSSam Leffler 
4711c42a7b7eSSam Leffler static int
4712c42a7b7eSSam Leffler ath_sysctl_slottime(SYSCTL_HANDLER_ARGS)
4713c42a7b7eSSam Leffler {
4714c42a7b7eSSam Leffler 	struct ath_softc *sc = arg1;
4715c42a7b7eSSam Leffler 	u_int slottime = ath_hal_getslottime(sc->sc_ah);
4716c42a7b7eSSam Leffler 	int error;
4717c42a7b7eSSam Leffler 
4718c42a7b7eSSam Leffler 	error = sysctl_handle_int(oidp, &slottime, 0, req);
4719c42a7b7eSSam Leffler 	if (error || !req->newptr)
4720c42a7b7eSSam Leffler 		return error;
4721c42a7b7eSSam Leffler 	return !ath_hal_setslottime(sc->sc_ah, slottime) ? EINVAL : 0;
4722c42a7b7eSSam Leffler }
4723c42a7b7eSSam Leffler 
4724c42a7b7eSSam Leffler static int
4725c42a7b7eSSam Leffler ath_sysctl_acktimeout(SYSCTL_HANDLER_ARGS)
4726c42a7b7eSSam Leffler {
4727c42a7b7eSSam Leffler 	struct ath_softc *sc = arg1;
4728c42a7b7eSSam Leffler 	u_int acktimeout = ath_hal_getacktimeout(sc->sc_ah);
4729c42a7b7eSSam Leffler 	int error;
4730c42a7b7eSSam Leffler 
4731c42a7b7eSSam Leffler 	error = sysctl_handle_int(oidp, &acktimeout, 0, req);
4732c42a7b7eSSam Leffler 	if (error || !req->newptr)
4733c42a7b7eSSam Leffler 		return error;
4734c42a7b7eSSam Leffler 	return !ath_hal_setacktimeout(sc->sc_ah, acktimeout) ? EINVAL : 0;
4735c42a7b7eSSam Leffler }
4736c42a7b7eSSam Leffler 
4737c42a7b7eSSam Leffler static int
4738c42a7b7eSSam Leffler ath_sysctl_ctstimeout(SYSCTL_HANDLER_ARGS)
4739c42a7b7eSSam Leffler {
4740c42a7b7eSSam Leffler 	struct ath_softc *sc = arg1;
4741c42a7b7eSSam Leffler 	u_int ctstimeout = ath_hal_getctstimeout(sc->sc_ah);
4742c42a7b7eSSam Leffler 	int error;
4743c42a7b7eSSam Leffler 
4744c42a7b7eSSam Leffler 	error = sysctl_handle_int(oidp, &ctstimeout, 0, req);
4745c42a7b7eSSam Leffler 	if (error || !req->newptr)
4746c42a7b7eSSam Leffler 		return error;
4747c42a7b7eSSam Leffler 	return !ath_hal_setctstimeout(sc->sc_ah, ctstimeout) ? EINVAL : 0;
4748c42a7b7eSSam Leffler }
4749c42a7b7eSSam Leffler 
4750c42a7b7eSSam Leffler static int
4751c42a7b7eSSam Leffler ath_sysctl_softled(SYSCTL_HANDLER_ARGS)
4752c42a7b7eSSam Leffler {
4753c42a7b7eSSam Leffler 	struct ath_softc *sc = arg1;
4754c42a7b7eSSam Leffler 	int softled = sc->sc_softled;
4755c42a7b7eSSam Leffler 	int error;
4756c42a7b7eSSam Leffler 
4757c42a7b7eSSam Leffler 	error = sysctl_handle_int(oidp, &softled, 0, req);
4758c42a7b7eSSam Leffler 	if (error || !req->newptr)
4759c42a7b7eSSam Leffler 		return error;
47603e50ec2cSSam Leffler 	softled = (softled != 0);
4761c42a7b7eSSam Leffler 	if (softled != sc->sc_softled) {
47623e50ec2cSSam Leffler 		if (softled) {
47633e50ec2cSSam Leffler 			/* NB: handle any sc_ledpin change */
4764c42a7b7eSSam Leffler 			ath_hal_gpioCfgOutput(sc->sc_ah, sc->sc_ledpin);
47653e50ec2cSSam Leffler 			ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin,
47663e50ec2cSSam Leffler 				!sc->sc_ledon);
47673e50ec2cSSam Leffler 		}
4768c42a7b7eSSam Leffler 		sc->sc_softled = softled;
4769c42a7b7eSSam Leffler 	}
4770c42a7b7eSSam Leffler 	return 0;
4771c42a7b7eSSam Leffler }
4772c42a7b7eSSam Leffler 
4773c42a7b7eSSam Leffler static int
4774c42a7b7eSSam Leffler ath_sysctl_rxantenna(SYSCTL_HANDLER_ARGS)
4775c42a7b7eSSam Leffler {
4776c42a7b7eSSam Leffler 	struct ath_softc *sc = arg1;
4777c42a7b7eSSam Leffler 	u_int defantenna = ath_hal_getdefantenna(sc->sc_ah);
4778c42a7b7eSSam Leffler 	int error;
4779c42a7b7eSSam Leffler 
4780c42a7b7eSSam Leffler 	error = sysctl_handle_int(oidp, &defantenna, 0, req);
4781c42a7b7eSSam Leffler 	if (!error && req->newptr)
4782c42a7b7eSSam Leffler 		ath_hal_setdefantenna(sc->sc_ah, defantenna);
4783c42a7b7eSSam Leffler 	return error;
4784c42a7b7eSSam Leffler }
4785c42a7b7eSSam Leffler 
4786c42a7b7eSSam Leffler static int
4787c42a7b7eSSam Leffler ath_sysctl_diversity(SYSCTL_HANDLER_ARGS)
4788c42a7b7eSSam Leffler {
4789c42a7b7eSSam Leffler 	struct ath_softc *sc = arg1;
4790c59005e9SSam Leffler 	u_int diversity = ath_hal_getdiversity(sc->sc_ah);
4791c42a7b7eSSam Leffler 	int error;
4792c42a7b7eSSam Leffler 
4793c42a7b7eSSam Leffler 	error = sysctl_handle_int(oidp, &diversity, 0, req);
4794c42a7b7eSSam Leffler 	if (error || !req->newptr)
4795c42a7b7eSSam Leffler 		return error;
4796c59005e9SSam Leffler 	if (!ath_hal_setdiversity(sc->sc_ah, diversity))
4797c59005e9SSam Leffler 		return EINVAL;
4798c42a7b7eSSam Leffler 	sc->sc_diversity = diversity;
4799c59005e9SSam Leffler 	return 0;
4800c42a7b7eSSam Leffler }
4801c42a7b7eSSam Leffler 
4802c42a7b7eSSam Leffler static int
4803c42a7b7eSSam Leffler ath_sysctl_diag(SYSCTL_HANDLER_ARGS)
4804c42a7b7eSSam Leffler {
4805c42a7b7eSSam Leffler 	struct ath_softc *sc = arg1;
4806c42a7b7eSSam Leffler 	u_int32_t diag;
4807c42a7b7eSSam Leffler 	int error;
4808c42a7b7eSSam Leffler 
4809c42a7b7eSSam Leffler 	if (!ath_hal_getdiag(sc->sc_ah, &diag))
4810c42a7b7eSSam Leffler 		return EINVAL;
4811c42a7b7eSSam Leffler 	error = sysctl_handle_int(oidp, &diag, 0, req);
4812c42a7b7eSSam Leffler 	if (error || !req->newptr)
4813c42a7b7eSSam Leffler 		return error;
4814c42a7b7eSSam Leffler 	return !ath_hal_setdiag(sc->sc_ah, diag) ? EINVAL : 0;
4815c42a7b7eSSam Leffler }
4816c42a7b7eSSam Leffler 
4817c42a7b7eSSam Leffler static int
4818c42a7b7eSSam Leffler ath_sysctl_tpscale(SYSCTL_HANDLER_ARGS)
4819c42a7b7eSSam Leffler {
4820c42a7b7eSSam Leffler 	struct ath_softc *sc = arg1;
4821fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
4822c42a7b7eSSam Leffler 	u_int32_t scale;
4823c42a7b7eSSam Leffler 	int error;
4824c42a7b7eSSam Leffler 
4825c42a7b7eSSam Leffler 	ath_hal_gettpscale(sc->sc_ah, &scale);
4826c42a7b7eSSam Leffler 	error = sysctl_handle_int(oidp, &scale, 0, req);
4827c42a7b7eSSam Leffler 	if (error || !req->newptr)
4828c42a7b7eSSam Leffler 		return error;
4829c42a7b7eSSam Leffler 	return !ath_hal_settpscale(sc->sc_ah, scale) ? EINVAL : ath_reset(ifp);
4830c42a7b7eSSam Leffler }
4831c42a7b7eSSam Leffler 
4832c42a7b7eSSam Leffler static int
4833c42a7b7eSSam Leffler ath_sysctl_tpc(SYSCTL_HANDLER_ARGS)
4834c42a7b7eSSam Leffler {
4835c42a7b7eSSam Leffler 	struct ath_softc *sc = arg1;
4836c42a7b7eSSam Leffler 	u_int tpc = ath_hal_gettpc(sc->sc_ah);
4837c42a7b7eSSam Leffler 	int error;
4838c42a7b7eSSam Leffler 
4839c42a7b7eSSam Leffler 	error = sysctl_handle_int(oidp, &tpc, 0, req);
4840c42a7b7eSSam Leffler 	if (error || !req->newptr)
4841c42a7b7eSSam Leffler 		return error;
4842c42a7b7eSSam Leffler 	return !ath_hal_settpc(sc->sc_ah, tpc) ? EINVAL : 0;
4843c42a7b7eSSam Leffler }
4844c42a7b7eSSam Leffler 
4845c42a7b7eSSam Leffler static void
4846c42a7b7eSSam Leffler ath_sysctlattach(struct ath_softc *sc)
4847c42a7b7eSSam Leffler {
4848c42a7b7eSSam Leffler 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
4849c42a7b7eSSam Leffler 	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
4850c59005e9SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4851c42a7b7eSSam Leffler 
4852c42a7b7eSSam Leffler 	ath_hal_getcountrycode(sc->sc_ah, &sc->sc_countrycode);
4853c42a7b7eSSam Leffler 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4854c42a7b7eSSam Leffler 		"countrycode", CTLFLAG_RD, &sc->sc_countrycode, 0,
4855c42a7b7eSSam Leffler 		"EEPROM country code");
4856c42a7b7eSSam Leffler 	ath_hal_getregdomain(sc->sc_ah, &sc->sc_regdomain);
4857c42a7b7eSSam Leffler 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4858c42a7b7eSSam Leffler 		"regdomain", CTLFLAG_RD, &sc->sc_regdomain, 0,
4859c42a7b7eSSam Leffler 		"EEPROM regdomain code");
4860c42a7b7eSSam Leffler 	sc->sc_debug = ath_debug;
4861c42a7b7eSSam Leffler 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4862c42a7b7eSSam Leffler 		"debug", CTLFLAG_RW, &sc->sc_debug, 0,
4863c42a7b7eSSam Leffler 		"control debugging printfs");
4864c42a7b7eSSam Leffler 
4865c42a7b7eSSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4866c42a7b7eSSam Leffler 		"slottime", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
4867c42a7b7eSSam Leffler 		ath_sysctl_slottime, "I", "802.11 slot time (us)");
4868c42a7b7eSSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4869c42a7b7eSSam Leffler 		"acktimeout", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
4870c42a7b7eSSam Leffler 		ath_sysctl_acktimeout, "I", "802.11 ACK timeout (us)");
4871c42a7b7eSSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4872c42a7b7eSSam Leffler 		"ctstimeout", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
4873c42a7b7eSSam Leffler 		ath_sysctl_ctstimeout, "I", "802.11 CTS timeout (us)");
4874c42a7b7eSSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4875c42a7b7eSSam Leffler 		"softled", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
4876c42a7b7eSSam Leffler 		ath_sysctl_softled, "I", "enable/disable software LED support");
4877c42a7b7eSSam Leffler 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4878c42a7b7eSSam Leffler 		"ledpin", CTLFLAG_RW, &sc->sc_ledpin, 0,
4879c42a7b7eSSam Leffler 		"GPIO pin connected to LED");
4880c42a7b7eSSam Leffler 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
48813e50ec2cSSam Leffler 		"ledon", CTLFLAG_RW, &sc->sc_ledon, 0,
48823e50ec2cSSam Leffler 		"setting to turn LED on");
48833e50ec2cSSam Leffler 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
48843e50ec2cSSam Leffler 		"ledidle", CTLFLAG_RW, &sc->sc_ledidle, 0,
48853e50ec2cSSam Leffler 		"idle time for inactivity LED (ticks)");
48863e50ec2cSSam Leffler 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4887c42a7b7eSSam Leffler 		"txantenna", CTLFLAG_RW, &sc->sc_txantenna, 0,
4888c42a7b7eSSam Leffler 		"tx antenna (0=auto)");
4889c42a7b7eSSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4890c42a7b7eSSam Leffler 		"rxantenna", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
4891c42a7b7eSSam Leffler 		ath_sysctl_rxantenna, "I", "default/rx antenna");
4892c59005e9SSam Leffler 	if (ath_hal_hasdiversity(ah))
4893c42a7b7eSSam Leffler 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4894c42a7b7eSSam Leffler 			"diversity", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
4895c42a7b7eSSam Leffler 			ath_sysctl_diversity, "I", "antenna diversity");
4896c42a7b7eSSam Leffler 	sc->sc_txintrperiod = ATH_TXINTR_PERIOD;
4897c42a7b7eSSam Leffler 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4898c42a7b7eSSam Leffler 		"txintrperiod", CTLFLAG_RW, &sc->sc_txintrperiod, 0,
4899c42a7b7eSSam Leffler 		"tx descriptor batching");
4900c42a7b7eSSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4901c42a7b7eSSam Leffler 		"diag", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
4902c42a7b7eSSam Leffler 		ath_sysctl_diag, "I", "h/w diagnostic control");
4903c42a7b7eSSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4904c42a7b7eSSam Leffler 		"tpscale", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
4905c42a7b7eSSam Leffler 		ath_sysctl_tpscale, "I", "tx power scaling");
4906c59005e9SSam Leffler 	if (ath_hal_hastpc(ah))
4907c42a7b7eSSam Leffler 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4908c42a7b7eSSam Leffler 			"tpc", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
4909c42a7b7eSSam Leffler 			ath_sysctl_tpc, "I", "enable/disable per-packet TPC");
4910c42a7b7eSSam Leffler }
4911c42a7b7eSSam Leffler 
4912c42a7b7eSSam Leffler static void
4913c42a7b7eSSam Leffler ath_bpfattach(struct ath_softc *sc)
4914c42a7b7eSSam Leffler {
4915fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
4916c42a7b7eSSam Leffler 
4917c42a7b7eSSam Leffler 	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
4918c42a7b7eSSam Leffler 		sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th),
4919c42a7b7eSSam Leffler 		&sc->sc_drvbpf);
4920c42a7b7eSSam Leffler 	/*
4921c42a7b7eSSam Leffler 	 * Initialize constant fields.
4922c42a7b7eSSam Leffler 	 * XXX make header lengths a multiple of 32-bits so subsequent
4923c42a7b7eSSam Leffler 	 *     headers are properly aligned; this is a kludge to keep
4924c42a7b7eSSam Leffler 	 *     certain applications happy.
4925c42a7b7eSSam Leffler 	 *
4926c42a7b7eSSam Leffler 	 * NB: the channel is setup each time we transition to the
4927c42a7b7eSSam Leffler 	 *     RUN state to avoid filling it in for each frame.
4928c42a7b7eSSam Leffler 	 */
4929c42a7b7eSSam Leffler 	sc->sc_tx_th_len = roundup(sizeof(sc->sc_tx_th), sizeof(u_int32_t));
4930c42a7b7eSSam Leffler 	sc->sc_tx_th.wt_ihdr.it_len = htole16(sc->sc_tx_th_len);
4931c42a7b7eSSam Leffler 	sc->sc_tx_th.wt_ihdr.it_present = htole32(ATH_TX_RADIOTAP_PRESENT);
4932c42a7b7eSSam Leffler 
4933d3be6f5bSSam Leffler 	sc->sc_rx_th_len = roundup(sizeof(sc->sc_rx_th), sizeof(u_int32_t));
4934d3be6f5bSSam Leffler 	sc->sc_rx_th.wr_ihdr.it_len = htole16(sc->sc_rx_th_len);
4935c42a7b7eSSam Leffler 	sc->sc_rx_th.wr_ihdr.it_present = htole32(ATH_RX_RADIOTAP_PRESENT);
4936c42a7b7eSSam Leffler }
4937c42a7b7eSSam Leffler 
4938c42a7b7eSSam Leffler /*
4939c42a7b7eSSam Leffler  * Announce various information on device/driver attach.
4940c42a7b7eSSam Leffler  */
4941c42a7b7eSSam Leffler static void
4942c42a7b7eSSam Leffler ath_announce(struct ath_softc *sc)
4943c42a7b7eSSam Leffler {
4944c42a7b7eSSam Leffler #define	HAL_MODE_DUALBAND	(HAL_MODE_11A|HAL_MODE_11B)
4945fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
4946c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4947c42a7b7eSSam Leffler 	u_int modes, cc;
4948c42a7b7eSSam Leffler 
4949c42a7b7eSSam Leffler 	if_printf(ifp, "mac %d.%d phy %d.%d",
4950c42a7b7eSSam Leffler 		ah->ah_macVersion, ah->ah_macRev,
4951c42a7b7eSSam Leffler 		ah->ah_phyRev >> 4, ah->ah_phyRev & 0xf);
4952c42a7b7eSSam Leffler 	/*
4953c42a7b7eSSam Leffler 	 * Print radio revision(s).  We check the wireless modes
4954c42a7b7eSSam Leffler 	 * to avoid falsely printing revs for inoperable parts.
4955c42a7b7eSSam Leffler 	 * Dual-band radio revs are returned in the 5Ghz rev number.
4956c42a7b7eSSam Leffler 	 */
4957c42a7b7eSSam Leffler 	ath_hal_getcountrycode(ah, &cc);
4958c42a7b7eSSam Leffler 	modes = ath_hal_getwirelessmodes(ah, cc);
4959c42a7b7eSSam Leffler 	if ((modes & HAL_MODE_DUALBAND) == HAL_MODE_DUALBAND) {
4960c42a7b7eSSam Leffler 		if (ah->ah_analog5GhzRev && ah->ah_analog2GhzRev)
4961c42a7b7eSSam Leffler 			printf(" 5ghz radio %d.%d 2ghz radio %d.%d",
4962c42a7b7eSSam Leffler 				ah->ah_analog5GhzRev >> 4,
4963c42a7b7eSSam Leffler 				ah->ah_analog5GhzRev & 0xf,
4964c42a7b7eSSam Leffler 				ah->ah_analog2GhzRev >> 4,
4965c42a7b7eSSam Leffler 				ah->ah_analog2GhzRev & 0xf);
4966c42a7b7eSSam Leffler 		else
4967c42a7b7eSSam Leffler 			printf(" radio %d.%d", ah->ah_analog5GhzRev >> 4,
4968c42a7b7eSSam Leffler 				ah->ah_analog5GhzRev & 0xf);
4969c42a7b7eSSam Leffler 	} else
4970c42a7b7eSSam Leffler 		printf(" radio %d.%d", ah->ah_analog5GhzRev >> 4,
4971c42a7b7eSSam Leffler 			ah->ah_analog5GhzRev & 0xf);
4972c42a7b7eSSam Leffler 	printf("\n");
4973c42a7b7eSSam Leffler 	if (bootverbose) {
4974c42a7b7eSSam Leffler 		int i;
4975c42a7b7eSSam Leffler 		for (i = 0; i <= WME_AC_VO; i++) {
4976c42a7b7eSSam Leffler 			struct ath_txq *txq = sc->sc_ac2q[i];
4977c42a7b7eSSam Leffler 			if_printf(ifp, "Use hw queue %u for %s traffic\n",
4978c42a7b7eSSam Leffler 				txq->axq_qnum, ieee80211_wme_acnames[i]);
4979c42a7b7eSSam Leffler 		}
4980c42a7b7eSSam Leffler 		if_printf(ifp, "Use hw queue %u for CAB traffic\n",
4981c42a7b7eSSam Leffler 			sc->sc_cabq->axq_qnum);
4982c42a7b7eSSam Leffler 		if_printf(ifp, "Use hw queue %u for beacons\n", sc->sc_bhalq);
4983c42a7b7eSSam Leffler 	}
4984c42a7b7eSSam Leffler #undef HAL_MODE_DUALBAND
4985c42a7b7eSSam Leffler }
4986