xref: /freebsd/sys/dev/ath/if_ath.c (revision f846cf42aba965444d2fa8025aa380197f85b777)
15591b213SSam Leffler /*-
210ad9a77SSam Leffler  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
35591b213SSam Leffler  * All rights reserved.
45591b213SSam Leffler  *
55591b213SSam Leffler  * Redistribution and use in source and binary forms, with or without
65591b213SSam Leffler  * modification, are permitted provided that the following conditions
75591b213SSam Leffler  * are met:
85591b213SSam Leffler  * 1. Redistributions of source code must retain the above copyright
95591b213SSam Leffler  *    notice, this list of conditions and the following disclaimer,
105591b213SSam Leffler  *    without modification.
115591b213SSam Leffler  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
125591b213SSam Leffler  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
135591b213SSam Leffler  *    redistribution must be conditioned upon including a substantially
145591b213SSam Leffler  *    similar Disclaimer requirement for further binary redistribution.
155591b213SSam Leffler  *
165591b213SSam Leffler  * NO WARRANTY
175591b213SSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
185591b213SSam Leffler  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
195591b213SSam Leffler  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
205591b213SSam Leffler  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
215591b213SSam Leffler  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
225591b213SSam Leffler  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
235591b213SSam Leffler  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
245591b213SSam Leffler  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
255591b213SSam Leffler  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
265591b213SSam Leffler  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
275591b213SSam Leffler  * THE POSSIBILITY OF SUCH DAMAGES.
285591b213SSam Leffler  */
295591b213SSam Leffler 
305591b213SSam Leffler #include <sys/cdefs.h>
315591b213SSam Leffler __FBSDID("$FreeBSD$");
325591b213SSam Leffler 
335591b213SSam Leffler /*
345591b213SSam Leffler  * Driver for the Atheros Wireless LAN controller.
355f3721d5SSam Leffler  *
365f3721d5SSam Leffler  * This software is derived from work of Atsushi Onoe; his contribution
375f3721d5SSam Leffler  * is greatly appreciated.
385591b213SSam Leffler  */
395591b213SSam Leffler 
405591b213SSam Leffler #include "opt_inet.h"
41a585a9a1SSam Leffler #include "opt_ath.h"
423f3087fdSAdrian Chadd /*
433f3087fdSAdrian Chadd  * This is needed for register operations which are performed
443f3087fdSAdrian Chadd  * by the driver - eg, calls to ath_hal_gettsf32().
4558816f3fSAdrian Chadd  *
4658816f3fSAdrian Chadd  * It's also required for any AH_DEBUG checks in here, eg the
4758816f3fSAdrian Chadd  * module dependencies.
483f3087fdSAdrian Chadd  */
493f3087fdSAdrian Chadd #include "opt_ah.h"
50584f7327SSam Leffler #include "opt_wlan.h"
515591b213SSam Leffler 
525591b213SSam Leffler #include <sys/param.h>
535591b213SSam Leffler #include <sys/systm.h>
545591b213SSam Leffler #include <sys/sysctl.h>
555591b213SSam Leffler #include <sys/mbuf.h>
565591b213SSam Leffler #include <sys/malloc.h>
575591b213SSam Leffler #include <sys/lock.h>
585591b213SSam Leffler #include <sys/mutex.h>
595591b213SSam Leffler #include <sys/kernel.h>
605591b213SSam Leffler #include <sys/socket.h>
615591b213SSam Leffler #include <sys/sockio.h>
625591b213SSam Leffler #include <sys/errno.h>
635591b213SSam Leffler #include <sys/callout.h>
645591b213SSam Leffler #include <sys/bus.h>
655591b213SSam Leffler #include <sys/endian.h>
660bbf5441SSam Leffler #include <sys/kthread.h>
670bbf5441SSam Leffler #include <sys/taskqueue.h>
683fc21fedSSam Leffler #include <sys/priv.h>
69dba9c859SAdrian Chadd #include <sys/module.h>
70f52d3452SAdrian Chadd #include <sys/ktr.h>
71ddbe3036SAdrian Chadd #include <sys/smp.h>	/* for mp_ncpus */
725591b213SSam Leffler 
735591b213SSam Leffler #include <machine/bus.h>
745591b213SSam Leffler 
755591b213SSam Leffler #include <net/if.h>
765591b213SSam Leffler #include <net/if_dl.h>
775591b213SSam Leffler #include <net/if_media.h>
78fc74a9f9SBrooks Davis #include <net/if_types.h>
795591b213SSam Leffler #include <net/if_arp.h>
805591b213SSam Leffler #include <net/ethernet.h>
815591b213SSam Leffler #include <net/if_llc.h>
825591b213SSam Leffler 
835591b213SSam Leffler #include <net80211/ieee80211_var.h>
8459efa8b5SSam Leffler #include <net80211/ieee80211_regdomain.h>
85339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
86339ccfb3SSam Leffler #include <net80211/ieee80211_superg.h>
87339ccfb3SSam Leffler #endif
88584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
8910ad9a77SSam Leffler #include <net80211/ieee80211_tdma.h>
9010ad9a77SSam Leffler #endif
915591b213SSam Leffler 
925591b213SSam Leffler #include <net/bpf.h>
935591b213SSam Leffler 
945591b213SSam Leffler #ifdef INET
955591b213SSam Leffler #include <netinet/in.h>
965591b213SSam Leffler #include <netinet/if_ether.h>
975591b213SSam Leffler #endif
985591b213SSam Leffler 
995591b213SSam Leffler #include <dev/ath/if_athvar.h>
10033644623SSam Leffler #include <dev/ath/ath_hal/ah_devid.h>		/* XXX for softled */
1010dbe9289SAdrian Chadd #include <dev/ath/ath_hal/ah_diagcodes.h>
1025591b213SSam Leffler 
1035bc8125aSAdrian Chadd #include <dev/ath/if_ath_debug.h>
104b8e788a5SAdrian Chadd #include <dev/ath/if_ath_misc.h>
105b8e788a5SAdrian Chadd #include <dev/ath/if_ath_tx.h>
1066079fdbeSAdrian Chadd #include <dev/ath/if_ath_sysctl.h>
107c65ee21dSAdrian Chadd #include <dev/ath/if_ath_led.h>
108d2d7a00aSAdrian Chadd #include <dev/ath/if_ath_keycache.h>
10948237774SAdrian Chadd #include <dev/ath/if_athdfs.h>
1105bc8125aSAdrian Chadd 
11186e07743SSam Leffler #ifdef ATH_TX99_DIAG
11286e07743SSam Leffler #include <dev/ath/ath_tx99/ath_tx99.h>
11386e07743SSam Leffler #endif
11486e07743SSam Leffler 
115f52d3452SAdrian Chadd #define	ATH_KTR_INTR	KTR_SPARE4
116f52d3452SAdrian Chadd #define	ATH_KTR_ERR	KTR_SPARE3
11748237774SAdrian Chadd 
118b032f27cSSam Leffler /*
119b032f27cSSam Leffler  * ATH_BCBUF determines the number of vap's that can transmit
120b032f27cSSam Leffler  * beacons and also (currently) the number of vap's that can
121b032f27cSSam Leffler  * have unique mac addresses/bssid.  When staggering beacons
122b032f27cSSam Leffler  * 4 is probably a good max as otherwise the beacons become
123b032f27cSSam Leffler  * very closely spaced and there is limited time for cab q traffic
124b032f27cSSam Leffler  * to go out.  You can burst beacons instead but that is not good
125b032f27cSSam Leffler  * for stations in power save and at some point you really want
126b032f27cSSam Leffler  * another radio (and channel).
127b032f27cSSam Leffler  *
128b032f27cSSam Leffler  * The limit on the number of mac addresses is tied to our use of
129b032f27cSSam Leffler  * the U/L bit and tracking addresses in a byte; it would be
130b032f27cSSam Leffler  * worthwhile to allow more for applications like proxy sta.
131b032f27cSSam Leffler  */
132b032f27cSSam Leffler CTASSERT(ATH_BCBUF <= 8);
133b032f27cSSam Leffler 
134b032f27cSSam Leffler static struct ieee80211vap *ath_vap_create(struct ieee80211com *,
135fcd9500fSBernhard Schmidt 		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
136fcd9500fSBernhard Schmidt 		    const uint8_t [IEEE80211_ADDR_LEN],
137fcd9500fSBernhard Schmidt 		    const uint8_t [IEEE80211_ADDR_LEN]);
138b032f27cSSam Leffler static void	ath_vap_delete(struct ieee80211vap *);
1395591b213SSam Leffler static void	ath_init(void *);
140c42a7b7eSSam Leffler static void	ath_stop_locked(struct ifnet *);
1415591b213SSam Leffler static void	ath_stop(struct ifnet *);
1425591b213SSam Leffler static void	ath_start(struct ifnet *);
143b032f27cSSam Leffler static int	ath_reset_vap(struct ieee80211vap *, u_long);
1445591b213SSam Leffler static int	ath_media_change(struct ifnet *);
1452e986da5SSam Leffler static void	ath_watchdog(void *);
1465591b213SSam Leffler static int	ath_ioctl(struct ifnet *, u_long, caddr_t);
1475591b213SSam Leffler static void	ath_fatal_proc(void *, int);
148b032f27cSSam Leffler static void	ath_bmiss_vap(struct ieee80211vap *);
1495591b213SSam Leffler static void	ath_bmiss_proc(void *, int);
150b032f27cSSam Leffler static void	ath_key_update_begin(struct ieee80211vap *);
151b032f27cSSam Leffler static void	ath_key_update_end(struct ieee80211vap *);
152b032f27cSSam Leffler static void	ath_update_mcast(struct ifnet *);
153b032f27cSSam Leffler static void	ath_update_promisc(struct ifnet *);
1545591b213SSam Leffler static void	ath_mode_init(struct ath_softc *);
155c42a7b7eSSam Leffler static void	ath_setslottime(struct ath_softc *);
156c42a7b7eSSam Leffler static void	ath_updateslot(struct ifnet *);
15780d2765fSSam Leffler static int	ath_beaconq_setup(struct ath_hal *);
1585591b213SSam Leffler static int	ath_beacon_alloc(struct ath_softc *, struct ieee80211_node *);
159b032f27cSSam Leffler static void	ath_beacon_update(struct ieee80211vap *, int item);
160c42a7b7eSSam Leffler static void	ath_beacon_setup(struct ath_softc *, struct ath_buf *);
1615591b213SSam Leffler static void	ath_beacon_proc(void *, int);
162b032f27cSSam Leffler static struct ath_buf *ath_beacon_generate(struct ath_softc *,
163b032f27cSSam Leffler 			struct ieee80211vap *);
164c42a7b7eSSam Leffler static void	ath_bstuck_proc(void *, int);
165d52f7132SAdrian Chadd static void	ath_reset_proc(void *, int);
166b032f27cSSam Leffler static void	ath_beacon_return(struct ath_softc *, struct ath_buf *);
1675591b213SSam Leffler static void	ath_beacon_free(struct ath_softc *);
168b032f27cSSam Leffler static void	ath_beacon_config(struct ath_softc *, struct ieee80211vap *);
169c42a7b7eSSam Leffler static void	ath_descdma_cleanup(struct ath_softc *sc,
170c42a7b7eSSam Leffler 			struct ath_descdma *, ath_bufhead *);
1715591b213SSam Leffler static int	ath_desc_alloc(struct ath_softc *);
1725591b213SSam Leffler static void	ath_desc_free(struct ath_softc *);
17338c208f8SSam Leffler static struct ieee80211_node *ath_node_alloc(struct ieee80211vap *,
17438c208f8SSam Leffler 			const uint8_t [IEEE80211_ADDR_LEN]);
1754afa805eSAdrian Chadd static void	ath_node_cleanup(struct ieee80211_node *);
176c42a7b7eSSam Leffler static void	ath_node_free(struct ieee80211_node *);
17768e8e04eSSam Leffler static void	ath_node_getsignal(const struct ieee80211_node *,
17868e8e04eSSam Leffler 			int8_t *, int8_t *);
1795591b213SSam Leffler static int	ath_rxbuf_init(struct ath_softc *, struct ath_buf *);
180b032f27cSSam Leffler static void	ath_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
1815463c4a4SSam Leffler 			int subtype, int rssi, int nf);
182c42a7b7eSSam Leffler static void	ath_setdefantenna(struct ath_softc *, u_int);
18396ff485dSAdrian Chadd static void	ath_rx_proc(struct ath_softc *sc, int);
18496ff485dSAdrian Chadd static void	ath_rx_tasklet(void *, int);
185622b3fd2SSam Leffler static void	ath_txq_init(struct ath_softc *sc, struct ath_txq *, int);
186c42a7b7eSSam Leffler static struct ath_txq *ath_txq_setup(struct ath_softc*, int qtype, int subtype);
187c42a7b7eSSam Leffler static int	ath_tx_setup(struct ath_softc *, int, int);
188c42a7b7eSSam Leffler static int	ath_wme_update(struct ieee80211com *);
189c42a7b7eSSam Leffler static void	ath_tx_cleanupq(struct ath_softc *, struct ath_txq *);
190c42a7b7eSSam Leffler static void	ath_tx_cleanup(struct ath_softc *);
191c42a7b7eSSam Leffler static void	ath_tx_proc_q0(void *, int);
192c42a7b7eSSam Leffler static void	ath_tx_proc_q0123(void *, int);
1935591b213SSam Leffler static void	ath_tx_proc(void *, int);
19403e9308fSAdrian Chadd static void	ath_txq_sched_tasklet(void *, int);
1955591b213SSam Leffler static int	ath_chan_set(struct ath_softc *, struct ieee80211_channel *);
196517526efSAdrian Chadd static void	ath_draintxq(struct ath_softc *, ATH_RESET_TYPE reset_type);
1979a842e8bSAdrian Chadd static void	ath_stoprecv(struct ath_softc *, int);
1985591b213SSam Leffler static int	ath_startrecv(struct ath_softc *);
199c42a7b7eSSam Leffler static void	ath_chan_change(struct ath_softc *, struct ieee80211_channel *);
20068e8e04eSSam Leffler static void	ath_scan_start(struct ieee80211com *);
20168e8e04eSSam Leffler static void	ath_scan_end(struct ieee80211com *);
20268e8e04eSSam Leffler static void	ath_set_channel(struct ieee80211com *);
203fdd72b4aSAdrian Chadd #ifdef	ATH_ENABLE_11N
204e7200579SAdrian Chadd static void	ath_update_chw(struct ieee80211com *);
205fdd72b4aSAdrian Chadd #endif	/* ATH_ENABLE_11N */
2065591b213SSam Leffler static void	ath_calibrate(void *);
207b032f27cSSam Leffler static int	ath_newstate(struct ieee80211vap *, enum ieee80211_state, int);
208e8fd88a3SSam Leffler static void	ath_setup_stationkey(struct ieee80211_node *);
209e9962332SSam Leffler static void	ath_newassoc(struct ieee80211_node *, int);
210b032f27cSSam Leffler static int	ath_setregdomain(struct ieee80211com *,
211b032f27cSSam Leffler 		    struct ieee80211_regdomain *, int,
212b032f27cSSam Leffler 		    struct ieee80211_channel []);
2135fe9f044SSam Leffler static void	ath_getradiocaps(struct ieee80211com *, int, int *,
214b032f27cSSam Leffler 		    struct ieee80211_channel []);
215b032f27cSSam Leffler static int	ath_getchannels(struct ath_softc *);
2165591b213SSam Leffler 
217c42a7b7eSSam Leffler static int	ath_rate_setup(struct ath_softc *, u_int mode);
2185591b213SSam Leffler static void	ath_setcurmode(struct ath_softc *, enum ieee80211_phymode);
219c42a7b7eSSam Leffler 
220c42a7b7eSSam Leffler static void	ath_announce(struct ath_softc *);
2215591b213SSam Leffler 
22248237774SAdrian Chadd static void	ath_dfs_tasklet(void *, int);
22348237774SAdrian Chadd 
224584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
22510ad9a77SSam Leffler static void	ath_tdma_settimers(struct ath_softc *sc, u_int32_t nexttbtt,
22610ad9a77SSam Leffler 		    u_int32_t bintval);
22710ad9a77SSam Leffler static void	ath_tdma_bintvalsetup(struct ath_softc *sc,
22810ad9a77SSam Leffler 		    const struct ieee80211_tdma_state *tdma);
22910ad9a77SSam Leffler static void	ath_tdma_config(struct ath_softc *sc, struct ieee80211vap *vap);
23010ad9a77SSam Leffler static void	ath_tdma_update(struct ieee80211_node *ni,
2312bc3ce77SSam Leffler 		    const struct ieee80211_tdma_param *tdma, int);
23210ad9a77SSam Leffler static void	ath_tdma_beacon_send(struct ath_softc *sc,
23310ad9a77SSam Leffler 		    struct ieee80211vap *vap);
23410ad9a77SSam Leffler 
23510ad9a77SSam Leffler #define	TDMA_EP_MULTIPLIER	(1<<10) /* pow2 to optimize out * and / */
23610ad9a77SSam Leffler #define	TDMA_LPF_LEN		6
23710ad9a77SSam Leffler #define	TDMA_DUMMY_MARKER	0x127
23810ad9a77SSam Leffler #define	TDMA_EP_MUL(x, mul)	((x) * (mul))
23910ad9a77SSam Leffler #define	TDMA_IN(x)		(TDMA_EP_MUL((x), TDMA_EP_MULTIPLIER))
24010ad9a77SSam Leffler #define	TDMA_LPF(x, y, len) \
24110ad9a77SSam Leffler     ((x != TDMA_DUMMY_MARKER) ? (((x) * ((len)-1) + (y)) / (len)) : (y))
24210ad9a77SSam Leffler #define	TDMA_SAMPLE(x, y) do {					\
24310ad9a77SSam Leffler 	x = TDMA_LPF((x), TDMA_IN(y), TDMA_LPF_LEN);		\
24410ad9a77SSam Leffler } while (0)
24510ad9a77SSam Leffler #define	TDMA_EP_RND(x,mul) \
24610ad9a77SSam Leffler 	((((x)%(mul)) >= ((mul)/2)) ? ((x) + ((mul) - 1)) / (mul) : (x)/(mul))
24710ad9a77SSam Leffler #define	TDMA_AVG(x)		TDMA_EP_RND(x, TDMA_EP_MULTIPLIER)
248584f7327SSam Leffler #endif /* IEEE80211_SUPPORT_TDMA */
24910ad9a77SSam Leffler 
2505591b213SSam Leffler SYSCTL_DECL(_hw_ath);
2515591b213SSam Leffler 
2525591b213SSam Leffler /* XXX validate sysctl values */
2532dc7fcc4SSam Leffler static	int ath_longcalinterval = 30;		/* long cals every 30 secs */
2542dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, longcal, CTLFLAG_RW, &ath_longcalinterval,
2552dc7fcc4SSam Leffler 	    0, "long chip calibration interval (secs)");
2562dc7fcc4SSam Leffler static	int ath_shortcalinterval = 100;		/* short cals every 100 ms */
2572dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, shortcal, CTLFLAG_RW, &ath_shortcalinterval,
2582dc7fcc4SSam Leffler 	    0, "short chip calibration interval (msecs)");
2592dc7fcc4SSam Leffler static	int ath_resetcalinterval = 20*60;	/* reset cal state 20 mins */
2602dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, resetcal, CTLFLAG_RW, &ath_resetcalinterval,
2612dc7fcc4SSam Leffler 	    0, "reset chip calibration results (secs)");
262a108ab63SAdrian Chadd static	int ath_anicalinterval = 100;		/* ANI calibration - 100 msec */
263a108ab63SAdrian Chadd SYSCTL_INT(_hw_ath, OID_AUTO, anical, CTLFLAG_RW, &ath_anicalinterval,
264a108ab63SAdrian Chadd 	    0, "ANI calibration (msecs)");
2655591b213SSam Leffler 
266e2d787faSSam Leffler static	int ath_rxbuf = ATH_RXBUF;		/* # rx buffers to allocate */
267aaa70f2fSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, rxbuf, CTLFLAG_RW, &ath_rxbuf,
268e2d787faSSam Leffler 	    0, "rx buffers allocated");
269e2d787faSSam Leffler TUNABLE_INT("hw.ath.rxbuf", &ath_rxbuf);
270e2d787faSSam Leffler static	int ath_txbuf = ATH_TXBUF;		/* # tx buffers to allocate */
271aaa70f2fSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, txbuf, CTLFLAG_RW, &ath_txbuf,
272e2d787faSSam Leffler 	    0, "tx buffers allocated");
273e2d787faSSam Leffler TUNABLE_INT("hw.ath.txbuf", &ath_txbuf);
274e2d787faSSam Leffler 
275a32ac9d3SSam Leffler static	int ath_bstuck_threshold = 4;		/* max missed beacons */
276a32ac9d3SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, bstuck, CTLFLAG_RW, &ath_bstuck_threshold,
277a32ac9d3SSam Leffler 	    0, "max missed beacon xmits before chip reset");
278a32ac9d3SSam Leffler 
2796b349e5aSAdrian Chadd MALLOC_DEFINE(M_ATHDEV, "athdev", "ath driver dma buffers");
280c42a7b7eSSam Leffler 
28167397d39SAdrian Chadd #define	HAL_MODE_HT20 (HAL_MODE_11NG_HT20 | HAL_MODE_11NA_HT20)
28267397d39SAdrian Chadd #define	HAL_MODE_HT40 \
28367397d39SAdrian Chadd 	(HAL_MODE_11NG_HT40PLUS | HAL_MODE_11NG_HT40MINUS | \
28467397d39SAdrian Chadd 	HAL_MODE_11NA_HT40PLUS | HAL_MODE_11NA_HT40MINUS)
2855591b213SSam Leffler int
2865591b213SSam Leffler ath_attach(u_int16_t devid, struct ath_softc *sc)
2875591b213SSam Leffler {
288fc74a9f9SBrooks Davis 	struct ifnet *ifp;
289b032f27cSSam Leffler 	struct ieee80211com *ic;
290fc74a9f9SBrooks Davis 	struct ath_hal *ah = NULL;
2915591b213SSam Leffler 	HAL_STATUS status;
292c42a7b7eSSam Leffler 	int error = 0, i;
293411373ebSSam Leffler 	u_int wmodes;
29429aca940SSam Leffler 	uint8_t macaddr[IEEE80211_ADDR_LEN];
295a865860dSAdrian Chadd 	int rx_chainmask, tx_chainmask;
2965591b213SSam Leffler 
297c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: devid 0x%x\n", __func__, devid);
2985591b213SSam Leffler 
299b032f27cSSam Leffler 	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
300fc74a9f9SBrooks Davis 	if (ifp == NULL) {
301fc74a9f9SBrooks Davis 		device_printf(sc->sc_dev, "can not if_alloc()\n");
302fc74a9f9SBrooks Davis 		error = ENOSPC;
303fc74a9f9SBrooks Davis 		goto bad;
304fc74a9f9SBrooks Davis 	}
305b032f27cSSam Leffler 	ic = ifp->if_l2com;
306fc74a9f9SBrooks Davis 
3075591b213SSam Leffler 	/* set these up early for if_printf use */
3089bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(sc->sc_dev),
3099bf40edeSBrooks Davis 		device_get_unit(sc->sc_dev));
3105591b213SSam Leffler 
3117e97436bSAdrian Chadd 	ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh,
3127e97436bSAdrian Chadd 	    sc->sc_eepromdata, &status);
3135591b213SSam Leffler 	if (ah == NULL) {
3145591b213SSam Leffler 		if_printf(ifp, "unable to attach hardware; HAL status %u\n",
3155591b213SSam Leffler 			status);
3165591b213SSam Leffler 		error = ENXIO;
3175591b213SSam Leffler 		goto bad;
3185591b213SSam Leffler 	}
3195591b213SSam Leffler 	sc->sc_ah = ah;
320b58b3803SSam Leffler 	sc->sc_invalid = 0;	/* ready to go, enable interrupt handling */
3213297be13SSam Leffler #ifdef	ATH_DEBUG
3223297be13SSam Leffler 	sc->sc_debug = ath_debug;
3233297be13SSam Leffler #endif
3245591b213SSam Leffler 
3255591b213SSam Leffler 	/*
326c42a7b7eSSam Leffler 	 * Check if the MAC has multi-rate retry support.
327c42a7b7eSSam Leffler 	 * We do this by trying to setup a fake extended
328c42a7b7eSSam Leffler 	 * descriptor.  MAC's that don't have support will
329c42a7b7eSSam Leffler 	 * return false w/o doing anything.  MAC's that do
330c42a7b7eSSam Leffler 	 * support it will return true w/o doing anything.
331c42a7b7eSSam Leffler 	 */
332c42a7b7eSSam Leffler 	sc->sc_mrretry = ath_hal_setupxtxdesc(ah, NULL, 0,0, 0,0, 0,0);
333c42a7b7eSSam Leffler 
334c42a7b7eSSam Leffler 	/*
335c42a7b7eSSam Leffler 	 * Check if the device has hardware counters for PHY
336c42a7b7eSSam Leffler 	 * errors.  If so we need to enable the MIB interrupt
337c42a7b7eSSam Leffler 	 * so we can act on stat triggers.
338c42a7b7eSSam Leffler 	 */
339c42a7b7eSSam Leffler 	if (ath_hal_hwphycounters(ah))
340c42a7b7eSSam Leffler 		sc->sc_needmib = 1;
341c42a7b7eSSam Leffler 
342c42a7b7eSSam Leffler 	/*
343c42a7b7eSSam Leffler 	 * Get the hardware key cache size.
344c42a7b7eSSam Leffler 	 */
345c42a7b7eSSam Leffler 	sc->sc_keymax = ath_hal_keycachesize(ah);
346e8fd88a3SSam Leffler 	if (sc->sc_keymax > ATH_KEYMAX) {
347e8fd88a3SSam Leffler 		if_printf(ifp, "Warning, using only %u of %u key cache slots\n",
348e8fd88a3SSam Leffler 			ATH_KEYMAX, sc->sc_keymax);
349e8fd88a3SSam Leffler 		sc->sc_keymax = ATH_KEYMAX;
350c42a7b7eSSam Leffler 	}
351c42a7b7eSSam Leffler 	/*
352c42a7b7eSSam Leffler 	 * Reset the key cache since some parts do not
353c42a7b7eSSam Leffler 	 * reset the contents on initial power up.
354c42a7b7eSSam Leffler 	 */
355c42a7b7eSSam Leffler 	for (i = 0; i < sc->sc_keymax; i++)
356c42a7b7eSSam Leffler 		ath_hal_keyreset(ah, i);
357c42a7b7eSSam Leffler 
358c42a7b7eSSam Leffler 	/*
359b032f27cSSam Leffler 	 * Collect the default channel list.
3605591b213SSam Leffler 	 */
361b032f27cSSam Leffler 	error = ath_getchannels(sc);
3625591b213SSam Leffler 	if (error != 0)
3635591b213SSam Leffler 		goto bad;
3645591b213SSam Leffler 
3655591b213SSam Leffler 	/*
3665591b213SSam Leffler 	 * Setup rate tables for all potential media types.
3675591b213SSam Leffler 	 */
3685591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11A);
3695591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11B);
3705591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11G);
371c42a7b7eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_A);
372c42a7b7eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_G);
37368e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_STURBO_A);
37468e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11NA);
37568e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11NG);
376724c193aSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_HALF);
377724c193aSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_QUARTER);
378aaa70f2fSSam Leffler 
379c42a7b7eSSam Leffler 	/* NB: setup here so ath_rate_update is happy */
380c42a7b7eSSam Leffler 	ath_setcurmode(sc, IEEE80211_MODE_11A);
3815591b213SSam Leffler 
382c42a7b7eSSam Leffler 	/*
383c42a7b7eSSam Leffler 	 * Allocate tx+rx descriptors and populate the lists.
384c42a7b7eSSam Leffler 	 */
3855591b213SSam Leffler 	error = ath_desc_alloc(sc);
3865591b213SSam Leffler 	if (error != 0) {
3875591b213SSam Leffler 		if_printf(ifp, "failed to allocate descriptors: %d\n", error);
3885591b213SSam Leffler 		goto bad;
3895591b213SSam Leffler 	}
3902e986da5SSam Leffler 	callout_init_mtx(&sc->sc_cal_ch, &sc->sc_mtx, 0);
3912e986da5SSam Leffler 	callout_init_mtx(&sc->sc_wd_ch, &sc->sc_mtx, 0);
3925591b213SSam Leffler 
393f0b2a0beSSam Leffler 	ATH_TXBUF_LOCK_INIT(sc);
3945591b213SSam Leffler 
3950bbf5441SSam Leffler 	sc->sc_tq = taskqueue_create("ath_taskq", M_NOWAIT,
3960bbf5441SSam Leffler 		taskqueue_thread_enqueue, &sc->sc_tq);
3970bbf5441SSam Leffler 	taskqueue_start_threads(&sc->sc_tq, 1, PI_NET,
3980bbf5441SSam Leffler 		"%s taskq", ifp->if_xname);
3990bbf5441SSam Leffler 
40096ff485dSAdrian Chadd 	TASK_INIT(&sc->sc_rxtask, 0, ath_rx_tasklet, sc);
4015591b213SSam Leffler 	TASK_INIT(&sc->sc_bmisstask, 0, ath_bmiss_proc, sc);
402c42a7b7eSSam Leffler 	TASK_INIT(&sc->sc_bstucktask,0, ath_bstuck_proc, sc);
403d52f7132SAdrian Chadd 	TASK_INIT(&sc->sc_resettask,0, ath_reset_proc, sc);
40403e9308fSAdrian Chadd 	TASK_INIT(&sc->sc_txqtask,0, ath_txq_sched_tasklet, sc);
405*f846cf42SAdrian Chadd 	TASK_INIT(&sc->sc_fataltask,0, ath_fatal_proc, sc);
4065591b213SSam Leffler 
4075591b213SSam Leffler 	/*
408c42a7b7eSSam Leffler 	 * Allocate hardware transmit queues: one queue for
409c42a7b7eSSam Leffler 	 * beacon frames and one data queue for each QoS
4104fa8d4efSDaniel Eischen 	 * priority.  Note that the hal handles resetting
411c42a7b7eSSam Leffler 	 * these queues at the needed time.
412c42a7b7eSSam Leffler 	 *
413c42a7b7eSSam Leffler 	 * XXX PS-Poll
4145591b213SSam Leffler 	 */
41580d2765fSSam Leffler 	sc->sc_bhalq = ath_beaconq_setup(ah);
4165591b213SSam Leffler 	if (sc->sc_bhalq == (u_int) -1) {
4175591b213SSam Leffler 		if_printf(ifp, "unable to setup a beacon xmit queue!\n");
418c42a7b7eSSam Leffler 		error = EIO;
419b28b4653SSam Leffler 		goto bad2;
4205591b213SSam Leffler 	}
421c42a7b7eSSam Leffler 	sc->sc_cabq = ath_txq_setup(sc, HAL_TX_QUEUE_CAB, 0);
422c42a7b7eSSam Leffler 	if (sc->sc_cabq == NULL) {
423c42a7b7eSSam Leffler 		if_printf(ifp, "unable to setup CAB xmit queue!\n");
424c42a7b7eSSam Leffler 		error = EIO;
425c42a7b7eSSam Leffler 		goto bad2;
426c42a7b7eSSam Leffler 	}
427c42a7b7eSSam Leffler 	/* NB: insure BK queue is the lowest priority h/w queue */
428c42a7b7eSSam Leffler 	if (!ath_tx_setup(sc, WME_AC_BK, HAL_WME_AC_BK)) {
429c42a7b7eSSam Leffler 		if_printf(ifp, "unable to setup xmit queue for %s traffic!\n",
430c42a7b7eSSam Leffler 			ieee80211_wme_acnames[WME_AC_BK]);
431c42a7b7eSSam Leffler 		error = EIO;
432c42a7b7eSSam Leffler 		goto bad2;
433c42a7b7eSSam Leffler 	}
434c42a7b7eSSam Leffler 	if (!ath_tx_setup(sc, WME_AC_BE, HAL_WME_AC_BE) ||
435c42a7b7eSSam Leffler 	    !ath_tx_setup(sc, WME_AC_VI, HAL_WME_AC_VI) ||
436c42a7b7eSSam Leffler 	    !ath_tx_setup(sc, WME_AC_VO, HAL_WME_AC_VO)) {
437c42a7b7eSSam Leffler 		/*
438c42a7b7eSSam Leffler 		 * Not enough hardware tx queues to properly do WME;
439c42a7b7eSSam Leffler 		 * just punt and assign them all to the same h/w queue.
440c42a7b7eSSam Leffler 		 * We could do a better job of this if, for example,
441c42a7b7eSSam Leffler 		 * we allocate queues when we switch from station to
442c42a7b7eSSam Leffler 		 * AP mode.
443c42a7b7eSSam Leffler 		 */
444c42a7b7eSSam Leffler 		if (sc->sc_ac2q[WME_AC_VI] != NULL)
445c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_VI]);
446c42a7b7eSSam Leffler 		if (sc->sc_ac2q[WME_AC_BE] != NULL)
447c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_BE]);
448c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_BE] = sc->sc_ac2q[WME_AC_BK];
449c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_VI] = sc->sc_ac2q[WME_AC_BK];
450c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_VO] = sc->sc_ac2q[WME_AC_BK];
451c42a7b7eSSam Leffler 	}
452c42a7b7eSSam Leffler 
453c42a7b7eSSam Leffler 	/*
454c42a7b7eSSam Leffler 	 * Special case certain configurations.  Note the
455c42a7b7eSSam Leffler 	 * CAB queue is handled by these specially so don't
456c42a7b7eSSam Leffler 	 * include them when checking the txq setup mask.
457c42a7b7eSSam Leffler 	 */
458c42a7b7eSSam Leffler 	switch (sc->sc_txqsetup &~ (1<<sc->sc_cabq->axq_qnum)) {
459c42a7b7eSSam Leffler 	case 0x01:
460c42a7b7eSSam Leffler 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0, sc);
461c42a7b7eSSam Leffler 		break;
462c42a7b7eSSam Leffler 	case 0x0f:
463c42a7b7eSSam Leffler 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0123, sc);
464c42a7b7eSSam Leffler 		break;
465c42a7b7eSSam Leffler 	default:
466c42a7b7eSSam Leffler 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc, sc);
467c42a7b7eSSam Leffler 		break;
468c42a7b7eSSam Leffler 	}
469c42a7b7eSSam Leffler 
470c42a7b7eSSam Leffler 	/*
471c42a7b7eSSam Leffler 	 * Setup rate control.  Some rate control modules
472c42a7b7eSSam Leffler 	 * call back to change the anntena state so expose
473c42a7b7eSSam Leffler 	 * the necessary entry points.
474c42a7b7eSSam Leffler 	 * XXX maybe belongs in struct ath_ratectrl?
475c42a7b7eSSam Leffler 	 */
476c42a7b7eSSam Leffler 	sc->sc_setdefantenna = ath_setdefantenna;
477c42a7b7eSSam Leffler 	sc->sc_rc = ath_rate_attach(sc);
478c42a7b7eSSam Leffler 	if (sc->sc_rc == NULL) {
479c42a7b7eSSam Leffler 		error = EIO;
480c42a7b7eSSam Leffler 		goto bad2;
481c42a7b7eSSam Leffler 	}
482c42a7b7eSSam Leffler 
48348237774SAdrian Chadd 	/* Attach DFS module */
48448237774SAdrian Chadd 	if (! ath_dfs_attach(sc)) {
4857e97436bSAdrian Chadd 		device_printf(sc->sc_dev,
4867e97436bSAdrian Chadd 		    "%s: unable to attach DFS\n", __func__);
48748237774SAdrian Chadd 		error = EIO;
48848237774SAdrian Chadd 		goto bad2;
48948237774SAdrian Chadd 	}
49048237774SAdrian Chadd 
49148237774SAdrian Chadd 	/* Start DFS processing tasklet */
49248237774SAdrian Chadd 	TASK_INIT(&sc->sc_dfstask, 0, ath_dfs_tasklet, sc);
49348237774SAdrian Chadd 
4943440495aSAdrian Chadd 	/* Configure LED state */
4953e50ec2cSSam Leffler 	sc->sc_blinking = 0;
496c42a7b7eSSam Leffler 	sc->sc_ledstate = 1;
4973e50ec2cSSam Leffler 	sc->sc_ledon = 0;			/* low true */
4983e50ec2cSSam Leffler 	sc->sc_ledidle = (2700*hz)/1000;	/* 2.7sec */
4993e50ec2cSSam Leffler 	callout_init(&sc->sc_ledtimer, CALLOUT_MPSAFE);
5003440495aSAdrian Chadd 
5013440495aSAdrian Chadd 	/*
5023440495aSAdrian Chadd 	 * Don't setup hardware-based blinking.
5033440495aSAdrian Chadd 	 *
5043440495aSAdrian Chadd 	 * Although some NICs may have this configured in the
5053440495aSAdrian Chadd 	 * default reset register values, the user may wish
5063440495aSAdrian Chadd 	 * to alter which pins have which function.
5073440495aSAdrian Chadd 	 *
5083440495aSAdrian Chadd 	 * The reference driver attaches the MAC network LED to GPIO1 and
5093440495aSAdrian Chadd 	 * the MAC power LED to GPIO2.  However, the DWA-552 cardbus
5103440495aSAdrian Chadd 	 * NIC has these reversed.
5113440495aSAdrian Chadd 	 */
5123440495aSAdrian Chadd 	sc->sc_hardled = (1 == 0);
5133440495aSAdrian Chadd 	sc->sc_led_net_pin = -1;
5143440495aSAdrian Chadd 	sc->sc_led_pwr_pin = -1;
515c42a7b7eSSam Leffler 	/*
516c42a7b7eSSam Leffler 	 * Auto-enable soft led processing for IBM cards and for
517c42a7b7eSSam Leffler 	 * 5211 minipci cards.  Users can also manually enable/disable
518c42a7b7eSSam Leffler 	 * support with a sysctl.
519c42a7b7eSSam Leffler 	 */
520c42a7b7eSSam Leffler 	sc->sc_softled = (devid == AR5212_DEVID_IBM || devid == AR5211_DEVID);
5216558ffd9SAdrian Chadd 	ath_led_config(sc);
522a497cd88SAdrian Chadd 	ath_hal_setledstate(ah, HAL_LED_INIT);
5235591b213SSam Leffler 
5245591b213SSam Leffler 	ifp->if_softc = sc;
5255591b213SSam Leffler 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
5265591b213SSam Leffler 	ifp->if_start = ath_start;
5275591b213SSam Leffler 	ifp->if_ioctl = ath_ioctl;
5285591b213SSam Leffler 	ifp->if_init = ath_init;
529e50d35e6SMaxim Sobolev 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
530e50d35e6SMaxim Sobolev 	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
531154b8df2SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
5325591b213SSam Leffler 
533c42a7b7eSSam Leffler 	ic->ic_ifp = ifp;
5345591b213SSam Leffler 	/* XXX not right but it's not used anywhere important */
5355591b213SSam Leffler 	ic->ic_phytype = IEEE80211_T_OFDM;
5365591b213SSam Leffler 	ic->ic_opmode = IEEE80211_M_STA;
537c42a7b7eSSam Leffler 	ic->ic_caps =
538c43feedeSSam Leffler 		  IEEE80211_C_STA		/* station mode */
539c43feedeSSam Leffler 		| IEEE80211_C_IBSS		/* ibss, nee adhoc, mode */
540fe32c3efSSam Leffler 		| IEEE80211_C_HOSTAP		/* hostap mode */
541fe32c3efSSam Leffler 		| IEEE80211_C_MONITOR		/* monitor mode */
5427a04dc27SSam Leffler 		| IEEE80211_C_AHDEMO		/* adhoc demo mode */
543b032f27cSSam Leffler 		| IEEE80211_C_WDS		/* 4-address traffic works */
54459aa14a9SRui Paulo 		| IEEE80211_C_MBSS		/* mesh point link mode */
545fe32c3efSSam Leffler 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
546c42a7b7eSSam Leffler 		| IEEE80211_C_SHSLOT		/* short slot time supported */
547c42a7b7eSSam Leffler 		| IEEE80211_C_WPA		/* capable of WPA1+WPA2 */
54868e8e04eSSam Leffler 		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
54968e8e04eSSam Leffler 		| IEEE80211_C_TXFRAG		/* handle tx frags */
55010dc8de4SAdrian Chadd #ifdef	ATH_ENABLE_DFS
5517e97436bSAdrian Chadd 		| IEEE80211_C_DFS		/* Enable radar detection */
55210dc8de4SAdrian Chadd #endif
55301e7e035SSam Leffler 		;
554c42a7b7eSSam Leffler 	/*
555c42a7b7eSSam Leffler 	 * Query the hal to figure out h/w crypto support.
556c42a7b7eSSam Leffler 	 */
557c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_WEP))
558b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP;
559c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_OCB))
560b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_OCB;
561c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_CCM))
562b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_CCM;
563c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_CKIP))
564b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_CKIP;
565c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_TKIP)) {
566b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIP;
567c42a7b7eSSam Leffler 		/*
568c42a7b7eSSam Leffler 		 * Check if h/w does the MIC and/or whether the
569c42a7b7eSSam Leffler 		 * separate key cache entries are required to
570c42a7b7eSSam Leffler 		 * handle both tx+rx MIC keys.
571c42a7b7eSSam Leffler 		 */
572c42a7b7eSSam Leffler 		if (ath_hal_ciphersupported(ah, HAL_CIPHER_MIC))
573b032f27cSSam Leffler 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
5745901d2d3SSam Leffler 		/*
5755901d2d3SSam Leffler 		 * If the h/w supports storing tx+rx MIC keys
5765901d2d3SSam Leffler 		 * in one cache slot automatically enable use.
5775901d2d3SSam Leffler 		 */
5785901d2d3SSam Leffler 		if (ath_hal_hastkipsplit(ah) ||
5795901d2d3SSam Leffler 		    !ath_hal_settkipsplit(ah, AH_FALSE))
580c42a7b7eSSam Leffler 			sc->sc_splitmic = 1;
581b032f27cSSam Leffler 		/*
582b032f27cSSam Leffler 		 * If the h/w can do TKIP MIC together with WME then
583b032f27cSSam Leffler 		 * we use it; otherwise we force the MIC to be done
584b032f27cSSam Leffler 		 * in software by the net80211 layer.
585b032f27cSSam Leffler 		 */
586b032f27cSSam Leffler 		if (ath_hal_haswmetkipmic(ah))
587b032f27cSSam Leffler 			sc->sc_wmetkipmic = 1;
588c42a7b7eSSam Leffler 	}
589e8fd88a3SSam Leffler 	sc->sc_hasclrkey = ath_hal_ciphersupported(ah, HAL_CIPHER_CLR);
5909ac01d39SRui Paulo 	/*
5911ac5dac2SRui Paulo 	 * Check for multicast key search support.
5929ac01d39SRui Paulo 	 */
5939ac01d39SRui Paulo 	if (ath_hal_hasmcastkeysearch(sc->sc_ah) &&
5949ac01d39SRui Paulo 	    !ath_hal_getmcastkeysearch(sc->sc_ah)) {
5959ac01d39SRui Paulo 		ath_hal_setmcastkeysearch(sc->sc_ah, 1);
5969ac01d39SRui Paulo 	}
597e8fd88a3SSam Leffler 	sc->sc_mcastkey = ath_hal_getmcastkeysearch(ah);
598c42a7b7eSSam Leffler 	/*
5995901d2d3SSam Leffler 	 * Mark key cache slots associated with global keys
6005901d2d3SSam Leffler 	 * as in use.  If we knew TKIP was not to be used we
6015901d2d3SSam Leffler 	 * could leave the +32, +64, and +32+64 slots free.
6025901d2d3SSam Leffler 	 */
6035901d2d3SSam Leffler 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
6045901d2d3SSam Leffler 		setbit(sc->sc_keymap, i);
6055901d2d3SSam Leffler 		setbit(sc->sc_keymap, i+64);
6065901d2d3SSam Leffler 		if (sc->sc_splitmic) {
6075901d2d3SSam Leffler 			setbit(sc->sc_keymap, i+32);
6085901d2d3SSam Leffler 			setbit(sc->sc_keymap, i+32+64);
6095901d2d3SSam Leffler 		}
6105901d2d3SSam Leffler 	}
6115901d2d3SSam Leffler 	/*
612c42a7b7eSSam Leffler 	 * TPC support can be done either with a global cap or
613c42a7b7eSSam Leffler 	 * per-packet support.  The latter is not available on
614c42a7b7eSSam Leffler 	 * all parts.  We're a bit pedantic here as all parts
615c42a7b7eSSam Leffler 	 * support a global cap.
616c42a7b7eSSam Leffler 	 */
617c59005e9SSam Leffler 	if (ath_hal_hastpc(ah) || ath_hal_hastxpowlimit(ah))
618c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TXPMGT;
619c42a7b7eSSam Leffler 
620c42a7b7eSSam Leffler 	/*
621c42a7b7eSSam Leffler 	 * Mark WME capability only if we have sufficient
622c42a7b7eSSam Leffler 	 * hardware queues to do proper priority scheduling.
623c42a7b7eSSam Leffler 	 */
624c42a7b7eSSam Leffler 	if (sc->sc_ac2q[WME_AC_BE] != sc->sc_ac2q[WME_AC_BK])
625c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_WME;
626c42a7b7eSSam Leffler 	/*
627e8fd88a3SSam Leffler 	 * Check for misc other capabilities.
628c42a7b7eSSam Leffler 	 */
629c42a7b7eSSam Leffler 	if (ath_hal_hasbursting(ah))
630c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_BURST;
631b032f27cSSam Leffler 	sc->sc_hasbmask = ath_hal_hasbssidmask(ah);
63259aa14a9SRui Paulo 	sc->sc_hasbmatch = ath_hal_hasbssidmatch(ah);
633b032f27cSSam Leffler 	sc->sc_hastsfadd = ath_hal_hastsfadjust(ah);
6348a2a6beeSAdrian Chadd 	sc->sc_rxslink = ath_hal_self_linked_final_rxdesc(ah);
635fc4de9b7SAdrian Chadd 	sc->sc_rxtsf32 = ath_hal_has_long_rxdesc_tsf(ah);
63668e8e04eSSam Leffler 	if (ath_hal_hasfastframes(ah))
63768e8e04eSSam Leffler 		ic->ic_caps |= IEEE80211_C_FF;
63859efa8b5SSam Leffler 	wmodes = ath_hal_getwirelessmodes(ah);
639411373ebSSam Leffler 	if (wmodes & (HAL_MODE_108G|HAL_MODE_TURBO))
64068e8e04eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TURBOP;
641584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
64210ad9a77SSam Leffler 	if (ath_hal_macversion(ah) > 0x78) {
64310ad9a77SSam Leffler 		ic->ic_caps |= IEEE80211_C_TDMA; /* capable of TDMA */
64410ad9a77SSam Leffler 		ic->ic_tdma_update = ath_tdma_update;
64510ad9a77SSam Leffler 	}
64610ad9a77SSam Leffler #endif
64767397d39SAdrian Chadd 
64867397d39SAdrian Chadd 	/*
6499c85ff91SAdrian Chadd 	 * TODO: enforce that at least this many frames are available
6509c85ff91SAdrian Chadd 	 * in the txbuf list before allowing data frames (raw or
6519c85ff91SAdrian Chadd 	 * otherwise) to be transmitted.
6529c85ff91SAdrian Chadd 	 */
6539c85ff91SAdrian Chadd 	sc->sc_txq_data_minfree = 10;
6549c85ff91SAdrian Chadd 	/*
6559c85ff91SAdrian Chadd 	 * Leave this as default to maintain legacy behaviour.
6569c85ff91SAdrian Chadd 	 * Shortening the cabq/mcastq may end up causing some
6579c85ff91SAdrian Chadd 	 * undesirable behaviour.
6589c85ff91SAdrian Chadd 	 */
6599c85ff91SAdrian Chadd 	sc->sc_txq_mcastq_maxdepth = ath_txbuf;
6609c85ff91SAdrian Chadd 
6619c85ff91SAdrian Chadd 	/*
662a865860dSAdrian Chadd 	 * Allow the TX and RX chainmasks to be overridden by
663a865860dSAdrian Chadd 	 * environment variables and/or device.hints.
664a865860dSAdrian Chadd 	 *
665a865860dSAdrian Chadd 	 * This must be done early - before the hardware is
666a865860dSAdrian Chadd 	 * calibrated or before the 802.11n stream calculation
667a865860dSAdrian Chadd 	 * is done.
668a865860dSAdrian Chadd 	 */
669a865860dSAdrian Chadd 	if (resource_int_value(device_get_name(sc->sc_dev),
670a865860dSAdrian Chadd 	    device_get_unit(sc->sc_dev), "rx_chainmask",
671a865860dSAdrian Chadd 	    &rx_chainmask) == 0) {
672a865860dSAdrian Chadd 		device_printf(sc->sc_dev, "Setting RX chainmask to 0x%x\n",
673a865860dSAdrian Chadd 		    rx_chainmask);
674a865860dSAdrian Chadd 		(void) ath_hal_setrxchainmask(sc->sc_ah, rx_chainmask);
675a865860dSAdrian Chadd 	}
676a865860dSAdrian Chadd 	if (resource_int_value(device_get_name(sc->sc_dev),
677a865860dSAdrian Chadd 	    device_get_unit(sc->sc_dev), "tx_chainmask",
678a865860dSAdrian Chadd 	    &tx_chainmask) == 0) {
679a865860dSAdrian Chadd 		device_printf(sc->sc_dev, "Setting TX chainmask to 0x%x\n",
680a865860dSAdrian Chadd 		    tx_chainmask);
681dc8552d5SAdrian Chadd 		(void) ath_hal_settxchainmask(sc->sc_ah, tx_chainmask);
682a865860dSAdrian Chadd 	}
683a865860dSAdrian Chadd 
684a865860dSAdrian Chadd 	/*
68567397d39SAdrian Chadd 	 * The if_ath 11n support is completely not ready for normal use.
68667397d39SAdrian Chadd 	 * Enabling this option will likely break everything and everything.
68767397d39SAdrian Chadd 	 * Don't think of doing that unless you know what you're doing.
68867397d39SAdrian Chadd 	 */
68967397d39SAdrian Chadd 
6908fd67f92SAdrian Chadd #ifdef	ATH_ENABLE_11N
69167397d39SAdrian Chadd 	/*
69267397d39SAdrian Chadd 	 * Query HT capabilities
69367397d39SAdrian Chadd 	 */
69467397d39SAdrian Chadd 	if (ath_hal_getcapability(ah, HAL_CAP_HT, 0, NULL) == HAL_OK &&
69567397d39SAdrian Chadd 	    (wmodes & (HAL_MODE_HT20 | HAL_MODE_HT40))) {
69667397d39SAdrian Chadd 		int rxs, txs;
69767397d39SAdrian Chadd 
69867397d39SAdrian Chadd 		device_printf(sc->sc_dev, "[HT] enabling HT modes\n");
69967397d39SAdrian Chadd 		ic->ic_htcaps = IEEE80211_HTC_HT	/* HT operation */
70067397d39SAdrian Chadd 			    | IEEE80211_HTC_AMPDU	/* A-MPDU tx/rx */
70167397d39SAdrian Chadd 			    | IEEE80211_HTC_AMSDU	/* A-MSDU tx/rx */
7027e97436bSAdrian Chadd 			    | IEEE80211_HTCAP_MAXAMSDU_3839
7037e97436bSAdrian Chadd 			    				/* max A-MSDU length */
70467397d39SAdrian Chadd 			    | IEEE80211_HTCAP_SMPS_OFF;	/* SM power save off */
70567397d39SAdrian Chadd 			;
70667397d39SAdrian Chadd 
70776355edbSAdrian Chadd 		/*
70876355edbSAdrian Chadd 		 * Enable short-GI for HT20 only if the hardware
70976355edbSAdrian Chadd 		 * advertises support.
71076355edbSAdrian Chadd 		 * Notably, anything earlier than the AR9287 doesn't.
71176355edbSAdrian Chadd 		 */
71276355edbSAdrian Chadd 		if ((ath_hal_getcapability(ah,
71376355edbSAdrian Chadd 		    HAL_CAP_HT20_SGI, 0, NULL) == HAL_OK) &&
71476355edbSAdrian Chadd 		    (wmodes & HAL_MODE_HT20)) {
71576355edbSAdrian Chadd 			device_printf(sc->sc_dev,
71676355edbSAdrian Chadd 			    "[HT] enabling short-GI in 20MHz mode\n");
71776355edbSAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_SHORTGI20;
71876355edbSAdrian Chadd 		}
71976355edbSAdrian Chadd 
72067397d39SAdrian Chadd 		if (wmodes & HAL_MODE_HT40)
72167397d39SAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40
72267397d39SAdrian Chadd 			    |  IEEE80211_HTCAP_SHORTGI40;
72367397d39SAdrian Chadd 
72467397d39SAdrian Chadd 		/*
7257e97436bSAdrian Chadd 		 * TX/RX streams need to be taken into account when
7267e97436bSAdrian Chadd 		 * negotiating which MCS rates it'll receive and
72767397d39SAdrian Chadd 		 * what MCS rates are available for TX.
72867397d39SAdrian Chadd 		 */
72954517070SAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 0, &txs);
73054517070SAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 1, &rxs);
73167397d39SAdrian Chadd 
73267397d39SAdrian Chadd 		ath_hal_getrxchainmask(ah, &sc->sc_rxchainmask);
73367397d39SAdrian Chadd 		ath_hal_gettxchainmask(ah, &sc->sc_txchainmask);
73467397d39SAdrian Chadd 
73567397d39SAdrian Chadd 		ic->ic_txstream = txs;
73667397d39SAdrian Chadd 		ic->ic_rxstream = rxs;
73767397d39SAdrian Chadd 
738ce656facSAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_RTS_AGGR_LIMIT, 1,
739ce656facSAdrian Chadd 		    &sc->sc_rts_aggr_limit);
740ce656facSAdrian Chadd 		if (sc->sc_rts_aggr_limit != (64 * 1024))
741ce656facSAdrian Chadd 			device_printf(sc->sc_dev,
742ce656facSAdrian Chadd 			    "[HT] RTS aggregates limited to %d KiB\n",
743ce656facSAdrian Chadd 			    sc->sc_rts_aggr_limit / 1024);
744ce656facSAdrian Chadd 
7457e97436bSAdrian Chadd 		device_printf(sc->sc_dev,
7467e97436bSAdrian Chadd 		    "[HT] %d RX streams; %d TX streams\n", rxs, txs);
74767397d39SAdrian Chadd 	}
74867397d39SAdrian Chadd #endif
74967397d39SAdrian Chadd 
750c42a7b7eSSam Leffler 	/*
751ddbe3036SAdrian Chadd 	 * Check if the hardware requires PCI register serialisation.
752ddbe3036SAdrian Chadd 	 * Some of the Owl based MACs require this.
753ddbe3036SAdrian Chadd 	 */
754ddbe3036SAdrian Chadd 	if (mp_ncpus > 1 &&
755ddbe3036SAdrian Chadd 	    ath_hal_getcapability(ah, HAL_CAP_SERIALISE_WAR,
756ddbe3036SAdrian Chadd 	     0, NULL) == HAL_OK) {
757ddbe3036SAdrian Chadd 		sc->sc_ah->ah_config.ah_serialise_reg_war = 1;
7587e97436bSAdrian Chadd 		device_printf(sc->sc_dev,
7597e97436bSAdrian Chadd 		    "Enabling register serialisation\n");
760ddbe3036SAdrian Chadd 	}
761ddbe3036SAdrian Chadd 
762ddbe3036SAdrian Chadd 	/*
763c42a7b7eSSam Leffler 	 * Indicate we need the 802.11 header padded to a
764c42a7b7eSSam Leffler 	 * 32-bit boundary for 4-address and QoS frames.
765c42a7b7eSSam Leffler 	 */
766c42a7b7eSSam Leffler 	ic->ic_flags |= IEEE80211_F_DATAPAD;
767c42a7b7eSSam Leffler 
768c42a7b7eSSam Leffler 	/*
769c42a7b7eSSam Leffler 	 * Query the hal about antenna support.
770c42a7b7eSSam Leffler 	 */
771c42a7b7eSSam Leffler 	sc->sc_defant = ath_hal_getdefantenna(ah);
772c42a7b7eSSam Leffler 
773c42a7b7eSSam Leffler 	/*
774c42a7b7eSSam Leffler 	 * Not all chips have the VEOL support we want to
775c42a7b7eSSam Leffler 	 * use with IBSS beacons; check here for it.
776c42a7b7eSSam Leffler 	 */
777c42a7b7eSSam Leffler 	sc->sc_hasveol = ath_hal_hasveol(ah);
7785591b213SSam Leffler 
7795591b213SSam Leffler 	/* get mac address from hardware */
78029aca940SSam Leffler 	ath_hal_getmac(ah, macaddr);
781b032f27cSSam Leffler 	if (sc->sc_hasbmask)
782b032f27cSSam Leffler 		ath_hal_getbssidmask(ah, sc->sc_hwbssidmask);
7835591b213SSam Leffler 
784b032f27cSSam Leffler 	/* NB: used to size node table key mapping array */
785b032f27cSSam Leffler 	ic->ic_max_keyix = sc->sc_keymax;
7865591b213SSam Leffler 	/* call MI attach routine. */
78729aca940SSam Leffler 	ieee80211_ifattach(ic, macaddr);
788b032f27cSSam Leffler 	ic->ic_setregdomain = ath_setregdomain;
789b032f27cSSam Leffler 	ic->ic_getradiocaps = ath_getradiocaps;
790b032f27cSSam Leffler 	sc->sc_opmode = HAL_M_STA;
791b032f27cSSam Leffler 
7925591b213SSam Leffler 	/* override default methods */
793b032f27cSSam Leffler 	ic->ic_newassoc = ath_newassoc;
794b032f27cSSam Leffler 	ic->ic_updateslot = ath_updateslot;
795b032f27cSSam Leffler 	ic->ic_wme.wme_update = ath_wme_update;
796b032f27cSSam Leffler 	ic->ic_vap_create = ath_vap_create;
797b032f27cSSam Leffler 	ic->ic_vap_delete = ath_vap_delete;
798b032f27cSSam Leffler 	ic->ic_raw_xmit = ath_raw_xmit;
799b032f27cSSam Leffler 	ic->ic_update_mcast = ath_update_mcast;
800b032f27cSSam Leffler 	ic->ic_update_promisc = ath_update_promisc;
8015591b213SSam Leffler 	ic->ic_node_alloc = ath_node_alloc;
8021e774079SSam Leffler 	sc->sc_node_free = ic->ic_node_free;
8035591b213SSam Leffler 	ic->ic_node_free = ath_node_free;
8044afa805eSAdrian Chadd 	sc->sc_node_cleanup = ic->ic_node_cleanup;
8054afa805eSAdrian Chadd 	ic->ic_node_cleanup = ath_node_cleanup;
80668e8e04eSSam Leffler 	ic->ic_node_getsignal = ath_node_getsignal;
80768e8e04eSSam Leffler 	ic->ic_scan_start = ath_scan_start;
80868e8e04eSSam Leffler 	ic->ic_scan_end = ath_scan_end;
80968e8e04eSSam Leffler 	ic->ic_set_channel = ath_set_channel;
810fdd72b4aSAdrian Chadd #ifdef	ATH_ENABLE_11N
811eb6f0de0SAdrian Chadd 	/* 802.11n specific - but just override anyway */
812eb6f0de0SAdrian Chadd 	sc->sc_addba_request = ic->ic_addba_request;
813eb6f0de0SAdrian Chadd 	sc->sc_addba_response = ic->ic_addba_response;
814eb6f0de0SAdrian Chadd 	sc->sc_addba_stop = ic->ic_addba_stop;
815eb6f0de0SAdrian Chadd 	sc->sc_bar_response = ic->ic_bar_response;
816eb6f0de0SAdrian Chadd 	sc->sc_addba_response_timeout = ic->ic_addba_response_timeout;
817eb6f0de0SAdrian Chadd 
818eb6f0de0SAdrian Chadd 	ic->ic_addba_request = ath_addba_request;
819eb6f0de0SAdrian Chadd 	ic->ic_addba_response = ath_addba_response;
820eb6f0de0SAdrian Chadd 	ic->ic_addba_response_timeout = ath_addba_response_timeout;
821eb6f0de0SAdrian Chadd 	ic->ic_addba_stop = ath_addba_stop;
822eb6f0de0SAdrian Chadd 	ic->ic_bar_response = ath_bar_response;
823eb6f0de0SAdrian Chadd 
824fdd72b4aSAdrian Chadd 	ic->ic_update_chw = ath_update_chw;
825fdd72b4aSAdrian Chadd #endif	/* ATH_ENABLE_11N */
826fdd72b4aSAdrian Chadd 
8275463c4a4SSam Leffler 	ieee80211_radiotap_attach(ic,
8285463c4a4SSam Leffler 	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
8295463c4a4SSam Leffler 		ATH_TX_RADIOTAP_PRESENT,
8305463c4a4SSam Leffler 	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
8315463c4a4SSam Leffler 		ATH_RX_RADIOTAP_PRESENT);
8325463c4a4SSam Leffler 
8334866e6c2SSam Leffler 	/*
8344866e6c2SSam Leffler 	 * Setup dynamic sysctl's now that country code and
8354866e6c2SSam Leffler 	 * regdomain are available from the hal.
8364866e6c2SSam Leffler 	 */
8374866e6c2SSam Leffler 	ath_sysctlattach(sc);
838e8dabfbeSAdrian Chadd 	ath_sysctl_stats_attach(sc);
83937931a35SAdrian Chadd 	ath_sysctl_hal_attach(sc);
84073454c73SSam Leffler 
841c42a7b7eSSam Leffler 	if (bootverbose)
842c42a7b7eSSam Leffler 		ieee80211_announce(ic);
843c42a7b7eSSam Leffler 	ath_announce(sc);
8445591b213SSam Leffler 	return 0;
845b28b4653SSam Leffler bad2:
846c42a7b7eSSam Leffler 	ath_tx_cleanup(sc);
847b28b4653SSam Leffler 	ath_desc_free(sc);
8485591b213SSam Leffler bad:
8495591b213SSam Leffler 	if (ah)
8505591b213SSam Leffler 		ath_hal_detach(ah);
851fc74a9f9SBrooks Davis 	if (ifp != NULL)
852fc74a9f9SBrooks Davis 		if_free(ifp);
8535591b213SSam Leffler 	sc->sc_invalid = 1;
8545591b213SSam Leffler 	return error;
8555591b213SSam Leffler }
8565591b213SSam Leffler 
8575591b213SSam Leffler int
8585591b213SSam Leffler ath_detach(struct ath_softc *sc)
8595591b213SSam Leffler {
860fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
8615591b213SSam Leffler 
862c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
863c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
8645591b213SSam Leffler 
865c42a7b7eSSam Leffler 	/*
866c42a7b7eSSam Leffler 	 * NB: the order of these is important:
86771b85077SSam Leffler 	 * o stop the chip so no more interrupts will fire
868c42a7b7eSSam Leffler 	 * o call the 802.11 layer before detaching the hal to
869c42a7b7eSSam Leffler 	 *   insure callbacks into the driver to delete global
870c42a7b7eSSam Leffler 	 *   key cache entries can be handled
87171b85077SSam Leffler 	 * o free the taskqueue which drains any pending tasks
872c42a7b7eSSam Leffler 	 * o reclaim the tx queue data structures after calling
873c42a7b7eSSam Leffler 	 *   the 802.11 layer as we'll get called back to reclaim
874c42a7b7eSSam Leffler 	 *   node state and potentially want to use them
875c42a7b7eSSam Leffler 	 * o to cleanup the tx queues the hal is called, so detach
876c42a7b7eSSam Leffler 	 *   it last
877c42a7b7eSSam Leffler 	 * Other than that, it's straightforward...
878c42a7b7eSSam Leffler 	 */
87971b85077SSam Leffler 	ath_stop(ifp);
880b032f27cSSam Leffler 	ieee80211_ifdetach(ifp->if_l2com);
88171b85077SSam Leffler 	taskqueue_free(sc->sc_tq);
88286e07743SSam Leffler #ifdef ATH_TX99_DIAG
88386e07743SSam Leffler 	if (sc->sc_tx99 != NULL)
88486e07743SSam Leffler 		sc->sc_tx99->detach(sc->sc_tx99);
88586e07743SSam Leffler #endif
886c42a7b7eSSam Leffler 	ath_rate_detach(sc->sc_rc);
88748237774SAdrian Chadd 
88848237774SAdrian Chadd 	ath_dfs_detach(sc);
8895591b213SSam Leffler 	ath_desc_free(sc);
890c42a7b7eSSam Leffler 	ath_tx_cleanup(sc);
89171b85077SSam Leffler 	ath_hal_detach(sc->sc_ah);	/* NB: sets chip in full sleep */
892c4c6f08fSRuslan Ermilov 	if_free(ifp);
893f0b2a0beSSam Leffler 
8945591b213SSam Leffler 	return 0;
8955591b213SSam Leffler }
8965591b213SSam Leffler 
897b032f27cSSam Leffler /*
898b032f27cSSam Leffler  * MAC address handling for multiple BSS on the same radio.
899b032f27cSSam Leffler  * The first vap uses the MAC address from the EEPROM.  For
900b032f27cSSam Leffler  * subsequent vap's we set the U/L bit (bit 1) in the MAC
901b032f27cSSam Leffler  * address and use the next six bits as an index.
902b032f27cSSam Leffler  */
903b032f27cSSam Leffler static void
904b032f27cSSam Leffler assign_address(struct ath_softc *sc, uint8_t mac[IEEE80211_ADDR_LEN], int clone)
905b032f27cSSam Leffler {
906b032f27cSSam Leffler 	int i;
907b032f27cSSam Leffler 
908b032f27cSSam Leffler 	if (clone && sc->sc_hasbmask) {
909b032f27cSSam Leffler 		/* NB: we only do this if h/w supports multiple bssid */
910b032f27cSSam Leffler 		for (i = 0; i < 8; i++)
911b032f27cSSam Leffler 			if ((sc->sc_bssidmask & (1<<i)) == 0)
912b032f27cSSam Leffler 				break;
913b032f27cSSam Leffler 		if (i != 0)
914b032f27cSSam Leffler 			mac[0] |= (i << 2)|0x2;
915b032f27cSSam Leffler 	} else
916b032f27cSSam Leffler 		i = 0;
917b032f27cSSam Leffler 	sc->sc_bssidmask |= 1<<i;
918b032f27cSSam Leffler 	sc->sc_hwbssidmask[0] &= ~mac[0];
919b032f27cSSam Leffler 	if (i == 0)
920b032f27cSSam Leffler 		sc->sc_nbssid0++;
921b032f27cSSam Leffler }
922b032f27cSSam Leffler 
923b032f27cSSam Leffler static void
924b032f27cSSam Leffler reclaim_address(struct ath_softc *sc, const uint8_t mac[IEEE80211_ADDR_LEN])
925b032f27cSSam Leffler {
926b032f27cSSam Leffler 	int i = mac[0] >> 2;
927b032f27cSSam Leffler 	uint8_t mask;
928b032f27cSSam Leffler 
929b032f27cSSam Leffler 	if (i != 0 || --sc->sc_nbssid0 == 0) {
930b032f27cSSam Leffler 		sc->sc_bssidmask &= ~(1<<i);
931b032f27cSSam Leffler 		/* recalculate bssid mask from remaining addresses */
932b032f27cSSam Leffler 		mask = 0xff;
933b032f27cSSam Leffler 		for (i = 1; i < 8; i++)
934b032f27cSSam Leffler 			if (sc->sc_bssidmask & (1<<i))
935b032f27cSSam Leffler 				mask &= ~((i<<2)|0x2);
936b032f27cSSam Leffler 		sc->sc_hwbssidmask[0] |= mask;
937b032f27cSSam Leffler 	}
938b032f27cSSam Leffler }
939b032f27cSSam Leffler 
940b032f27cSSam Leffler /*
941b032f27cSSam Leffler  * Assign a beacon xmit slot.  We try to space out
942b032f27cSSam Leffler  * assignments so when beacons are staggered the
943b032f27cSSam Leffler  * traffic coming out of the cab q has maximal time
944b032f27cSSam Leffler  * to go out before the next beacon is scheduled.
945b032f27cSSam Leffler  */
946b032f27cSSam Leffler static int
947b032f27cSSam Leffler assign_bslot(struct ath_softc *sc)
948b032f27cSSam Leffler {
949b032f27cSSam Leffler 	u_int slot, free;
950b032f27cSSam Leffler 
951b032f27cSSam Leffler 	free = 0;
952b032f27cSSam Leffler 	for (slot = 0; slot < ATH_BCBUF; slot++)
953b032f27cSSam Leffler 		if (sc->sc_bslot[slot] == NULL) {
954b032f27cSSam Leffler 			if (sc->sc_bslot[(slot+1)%ATH_BCBUF] == NULL &&
955b032f27cSSam Leffler 			    sc->sc_bslot[(slot-1)%ATH_BCBUF] == NULL)
956b032f27cSSam Leffler 				return slot;
957b032f27cSSam Leffler 			free = slot;
958b032f27cSSam Leffler 			/* NB: keep looking for a double slot */
959b032f27cSSam Leffler 		}
960b032f27cSSam Leffler 	return free;
961b032f27cSSam Leffler }
962b032f27cSSam Leffler 
963b032f27cSSam Leffler static struct ieee80211vap *
964fcd9500fSBernhard Schmidt ath_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
965fcd9500fSBernhard Schmidt     enum ieee80211_opmode opmode, int flags,
966b032f27cSSam Leffler     const uint8_t bssid[IEEE80211_ADDR_LEN],
967b032f27cSSam Leffler     const uint8_t mac0[IEEE80211_ADDR_LEN])
968b032f27cSSam Leffler {
969b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
970b032f27cSSam Leffler 	struct ath_vap *avp;
971b032f27cSSam Leffler 	struct ieee80211vap *vap;
972b032f27cSSam Leffler 	uint8_t mac[IEEE80211_ADDR_LEN];
973fcd9500fSBernhard Schmidt 	int needbeacon, error;
974fcd9500fSBernhard Schmidt 	enum ieee80211_opmode ic_opmode;
975b032f27cSSam Leffler 
976b032f27cSSam Leffler 	avp = (struct ath_vap *) malloc(sizeof(struct ath_vap),
977b032f27cSSam Leffler 	    M_80211_VAP, M_WAITOK | M_ZERO);
978b032f27cSSam Leffler 	needbeacon = 0;
979b032f27cSSam Leffler 	IEEE80211_ADDR_COPY(mac, mac0);
980b032f27cSSam Leffler 
981b032f27cSSam Leffler 	ATH_LOCK(sc);
982a8962181SSam Leffler 	ic_opmode = opmode;		/* default to opmode of new vap */
983b032f27cSSam Leffler 	switch (opmode) {
984b032f27cSSam Leffler 	case IEEE80211_M_STA:
985a8962181SSam Leffler 		if (sc->sc_nstavaps != 0) {	/* XXX only 1 for now */
986b032f27cSSam Leffler 			device_printf(sc->sc_dev, "only 1 sta vap supported\n");
987b032f27cSSam Leffler 			goto bad;
988b032f27cSSam Leffler 		}
989b032f27cSSam Leffler 		if (sc->sc_nvaps) {
990b032f27cSSam Leffler 			/*
991a8962181SSam Leffler 			 * With multiple vaps we must fall back
992a8962181SSam Leffler 			 * to s/w beacon miss handling.
993b032f27cSSam Leffler 			 */
994b032f27cSSam Leffler 			flags |= IEEE80211_CLONE_NOBEACONS;
995b032f27cSSam Leffler 		}
996a8962181SSam Leffler 		if (flags & IEEE80211_CLONE_NOBEACONS) {
997a8962181SSam Leffler 			/*
998a8962181SSam Leffler 			 * Station mode w/o beacons are implemented w/ AP mode.
999a8962181SSam Leffler 			 */
1000b032f27cSSam Leffler 			ic_opmode = IEEE80211_M_HOSTAP;
1001a8962181SSam Leffler 		}
1002b032f27cSSam Leffler 		break;
1003b032f27cSSam Leffler 	case IEEE80211_M_IBSS:
1004b032f27cSSam Leffler 		if (sc->sc_nvaps != 0) {	/* XXX only 1 for now */
1005b032f27cSSam Leffler 			device_printf(sc->sc_dev,
1006b032f27cSSam Leffler 			    "only 1 ibss vap supported\n");
1007b032f27cSSam Leffler 			goto bad;
1008b032f27cSSam Leffler 		}
1009b032f27cSSam Leffler 		needbeacon = 1;
1010b032f27cSSam Leffler 		break;
1011b032f27cSSam Leffler 	case IEEE80211_M_AHDEMO:
1012584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
101310ad9a77SSam Leffler 		if (flags & IEEE80211_CLONE_TDMA) {
1014a8962181SSam Leffler 			if (sc->sc_nvaps != 0) {
1015a8962181SSam Leffler 				device_printf(sc->sc_dev,
1016a8962181SSam Leffler 				    "only 1 tdma vap supported\n");
1017a8962181SSam Leffler 				goto bad;
1018a8962181SSam Leffler 			}
101910ad9a77SSam Leffler 			needbeacon = 1;
102010ad9a77SSam Leffler 			flags |= IEEE80211_CLONE_NOBEACONS;
102110ad9a77SSam Leffler 		}
1022b032f27cSSam Leffler 		/* fall thru... */
102310ad9a77SSam Leffler #endif
1024b032f27cSSam Leffler 	case IEEE80211_M_MONITOR:
1025b032f27cSSam Leffler 		if (sc->sc_nvaps != 0 && ic->ic_opmode != opmode) {
1026a8962181SSam Leffler 			/*
1027a8962181SSam Leffler 			 * Adopt existing mode.  Adding a monitor or ahdemo
1028a8962181SSam Leffler 			 * vap to an existing configuration is of dubious
1029a8962181SSam Leffler 			 * value but should be ok.
1030a8962181SSam Leffler 			 */
1031b032f27cSSam Leffler 			/* XXX not right for monitor mode */
1032b032f27cSSam Leffler 			ic_opmode = ic->ic_opmode;
1033a8962181SSam Leffler 		}
1034b032f27cSSam Leffler 		break;
1035b032f27cSSam Leffler 	case IEEE80211_M_HOSTAP:
103659aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
1037b032f27cSSam Leffler 		needbeacon = 1;
1038a8962181SSam Leffler 		break;
1039b032f27cSSam Leffler 	case IEEE80211_M_WDS:
1040a8962181SSam Leffler 		if (sc->sc_nvaps != 0 && ic->ic_opmode == IEEE80211_M_STA) {
1041b032f27cSSam Leffler 			device_printf(sc->sc_dev,
1042b032f27cSSam Leffler 			    "wds not supported in sta mode\n");
1043b032f27cSSam Leffler 			goto bad;
1044b032f27cSSam Leffler 		}
1045b032f27cSSam Leffler 		/*
1046b032f27cSSam Leffler 		 * Silently remove any request for a unique
1047b032f27cSSam Leffler 		 * bssid; WDS vap's always share the local
1048b032f27cSSam Leffler 		 * mac address.
1049b032f27cSSam Leffler 		 */
1050b032f27cSSam Leffler 		flags &= ~IEEE80211_CLONE_BSSID;
1051a8962181SSam Leffler 		if (sc->sc_nvaps == 0)
1052b032f27cSSam Leffler 			ic_opmode = IEEE80211_M_HOSTAP;
1053a8962181SSam Leffler 		else
1054a8962181SSam Leffler 			ic_opmode = ic->ic_opmode;
10557d261891SRui Paulo 		break;
1056b032f27cSSam Leffler 	default:
1057b032f27cSSam Leffler 		device_printf(sc->sc_dev, "unknown opmode %d\n", opmode);
1058b032f27cSSam Leffler 		goto bad;
1059b032f27cSSam Leffler 	}
1060b032f27cSSam Leffler 	/*
1061b032f27cSSam Leffler 	 * Check that a beacon buffer is available; the code below assumes it.
1062b032f27cSSam Leffler 	 */
10636b349e5aSAdrian Chadd 	if (needbeacon & TAILQ_EMPTY(&sc->sc_bbuf)) {
1064b032f27cSSam Leffler 		device_printf(sc->sc_dev, "no beacon buffer available\n");
1065b032f27cSSam Leffler 		goto bad;
1066b032f27cSSam Leffler 	}
1067b032f27cSSam Leffler 
1068b032f27cSSam Leffler 	/* STA, AHDEMO? */
106959aa14a9SRui Paulo 	if (opmode == IEEE80211_M_HOSTAP || opmode == IEEE80211_M_MBSS) {
1070b032f27cSSam Leffler 		assign_address(sc, mac, flags & IEEE80211_CLONE_BSSID);
1071b032f27cSSam Leffler 		ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
1072b032f27cSSam Leffler 	}
1073b032f27cSSam Leffler 
1074b032f27cSSam Leffler 	vap = &avp->av_vap;
1075b032f27cSSam Leffler 	/* XXX can't hold mutex across if_alloc */
1076b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1077b032f27cSSam Leffler 	error = ieee80211_vap_setup(ic, vap, name, unit, opmode, flags,
1078b032f27cSSam Leffler 	    bssid, mac);
1079b032f27cSSam Leffler 	ATH_LOCK(sc);
1080b032f27cSSam Leffler 	if (error != 0) {
1081b032f27cSSam Leffler 		device_printf(sc->sc_dev, "%s: error %d creating vap\n",
1082b032f27cSSam Leffler 		    __func__, error);
1083b032f27cSSam Leffler 		goto bad2;
1084b032f27cSSam Leffler 	}
1085b032f27cSSam Leffler 
1086b032f27cSSam Leffler 	/* h/w crypto support */
1087b032f27cSSam Leffler 	vap->iv_key_alloc = ath_key_alloc;
1088b032f27cSSam Leffler 	vap->iv_key_delete = ath_key_delete;
1089b032f27cSSam Leffler 	vap->iv_key_set = ath_key_set;
1090b032f27cSSam Leffler 	vap->iv_key_update_begin = ath_key_update_begin;
1091b032f27cSSam Leffler 	vap->iv_key_update_end = ath_key_update_end;
1092b032f27cSSam Leffler 
1093b032f27cSSam Leffler 	/* override various methods */
1094b032f27cSSam Leffler 	avp->av_recv_mgmt = vap->iv_recv_mgmt;
1095b032f27cSSam Leffler 	vap->iv_recv_mgmt = ath_recv_mgmt;
1096b032f27cSSam Leffler 	vap->iv_reset = ath_reset_vap;
1097b032f27cSSam Leffler 	vap->iv_update_beacon = ath_beacon_update;
1098b032f27cSSam Leffler 	avp->av_newstate = vap->iv_newstate;
1099b032f27cSSam Leffler 	vap->iv_newstate = ath_newstate;
1100b032f27cSSam Leffler 	avp->av_bmiss = vap->iv_bmiss;
1101b032f27cSSam Leffler 	vap->iv_bmiss = ath_bmiss_vap;
1102b032f27cSSam Leffler 
11039be25f4aSAdrian Chadd 	/* Set default parameters */
11049be25f4aSAdrian Chadd 
11059be25f4aSAdrian Chadd 	/*
11069be25f4aSAdrian Chadd 	 * Anything earlier than some AR9300 series MACs don't
11079be25f4aSAdrian Chadd 	 * support a smaller MPDU density.
11089be25f4aSAdrian Chadd 	 */
11099be25f4aSAdrian Chadd 	vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_8;
11109be25f4aSAdrian Chadd 	/*
11119be25f4aSAdrian Chadd 	 * All NICs can handle the maximum size, however
11129be25f4aSAdrian Chadd 	 * AR5416 based MACs can only TX aggregates w/ RTS
11139be25f4aSAdrian Chadd 	 * protection when the total aggregate size is <= 8k.
11149be25f4aSAdrian Chadd 	 * However, for now that's enforced by the TX path.
11159be25f4aSAdrian Chadd 	 */
11169be25f4aSAdrian Chadd 	vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_64K;
11179be25f4aSAdrian Chadd 
1118b032f27cSSam Leffler 	avp->av_bslot = -1;
1119b032f27cSSam Leffler 	if (needbeacon) {
1120b032f27cSSam Leffler 		/*
1121b032f27cSSam Leffler 		 * Allocate beacon state and setup the q for buffered
1122b032f27cSSam Leffler 		 * multicast frames.  We know a beacon buffer is
1123b032f27cSSam Leffler 		 * available because we checked above.
1124b032f27cSSam Leffler 		 */
11256b349e5aSAdrian Chadd 		avp->av_bcbuf = TAILQ_FIRST(&sc->sc_bbuf);
11266b349e5aSAdrian Chadd 		TAILQ_REMOVE(&sc->sc_bbuf, avp->av_bcbuf, bf_list);
1127b032f27cSSam Leffler 		if (opmode != IEEE80211_M_IBSS || !sc->sc_hasveol) {
1128b032f27cSSam Leffler 			/*
1129b032f27cSSam Leffler 			 * Assign the vap to a beacon xmit slot.  As above
1130b032f27cSSam Leffler 			 * this cannot fail to find a free one.
1131b032f27cSSam Leffler 			 */
1132b032f27cSSam Leffler 			avp->av_bslot = assign_bslot(sc);
1133b032f27cSSam Leffler 			KASSERT(sc->sc_bslot[avp->av_bslot] == NULL,
1134b032f27cSSam Leffler 			    ("beacon slot %u not empty", avp->av_bslot));
1135b032f27cSSam Leffler 			sc->sc_bslot[avp->av_bslot] = vap;
1136b032f27cSSam Leffler 			sc->sc_nbcnvaps++;
1137b032f27cSSam Leffler 		}
1138b032f27cSSam Leffler 		if (sc->sc_hastsfadd && sc->sc_nbcnvaps > 0) {
1139b032f27cSSam Leffler 			/*
1140b032f27cSSam Leffler 			 * Multple vaps are to transmit beacons and we
1141b032f27cSSam Leffler 			 * have h/w support for TSF adjusting; enable
1142b032f27cSSam Leffler 			 * use of staggered beacons.
1143b032f27cSSam Leffler 			 */
1144b032f27cSSam Leffler 			sc->sc_stagbeacons = 1;
1145b032f27cSSam Leffler 		}
1146b032f27cSSam Leffler 		ath_txq_init(sc, &avp->av_mcastq, ATH_TXQ_SWQ);
1147b032f27cSSam Leffler 	}
1148b032f27cSSam Leffler 
1149b032f27cSSam Leffler 	ic->ic_opmode = ic_opmode;
1150b032f27cSSam Leffler 	if (opmode != IEEE80211_M_WDS) {
1151b032f27cSSam Leffler 		sc->sc_nvaps++;
1152b032f27cSSam Leffler 		if (opmode == IEEE80211_M_STA)
1153b032f27cSSam Leffler 			sc->sc_nstavaps++;
1154fe0dd789SSam Leffler 		if (opmode == IEEE80211_M_MBSS)
1155fe0dd789SSam Leffler 			sc->sc_nmeshvaps++;
1156b032f27cSSam Leffler 	}
1157b032f27cSSam Leffler 	switch (ic_opmode) {
1158b032f27cSSam Leffler 	case IEEE80211_M_IBSS:
1159b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_IBSS;
1160b032f27cSSam Leffler 		break;
1161b032f27cSSam Leffler 	case IEEE80211_M_STA:
1162b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_STA;
1163b032f27cSSam Leffler 		break;
1164b032f27cSSam Leffler 	case IEEE80211_M_AHDEMO:
1165584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
116610ad9a77SSam Leffler 		if (vap->iv_caps & IEEE80211_C_TDMA) {
116710ad9a77SSam Leffler 			sc->sc_tdma = 1;
116810ad9a77SSam Leffler 			/* NB: disable tsf adjust */
116910ad9a77SSam Leffler 			sc->sc_stagbeacons = 0;
117010ad9a77SSam Leffler 		}
117110ad9a77SSam Leffler 		/*
117210ad9a77SSam Leffler 		 * NB: adhoc demo mode is a pseudo mode; to the hal it's
117310ad9a77SSam Leffler 		 * just ap mode.
117410ad9a77SSam Leffler 		 */
117510ad9a77SSam Leffler 		/* fall thru... */
117610ad9a77SSam Leffler #endif
1177b032f27cSSam Leffler 	case IEEE80211_M_HOSTAP:
117859aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
1179b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_HOSTAP;
1180b032f27cSSam Leffler 		break;
1181b032f27cSSam Leffler 	case IEEE80211_M_MONITOR:
1182b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_MONITOR;
1183b032f27cSSam Leffler 		break;
1184b032f27cSSam Leffler 	default:
1185b032f27cSSam Leffler 		/* XXX should not happen */
1186b032f27cSSam Leffler 		break;
1187b032f27cSSam Leffler 	}
1188b032f27cSSam Leffler 	if (sc->sc_hastsfadd) {
1189b032f27cSSam Leffler 		/*
1190b032f27cSSam Leffler 		 * Configure whether or not TSF adjust should be done.
1191b032f27cSSam Leffler 		 */
1192b032f27cSSam Leffler 		ath_hal_settsfadjust(sc->sc_ah, sc->sc_stagbeacons);
1193b032f27cSSam Leffler 	}
119410ad9a77SSam Leffler 	if (flags & IEEE80211_CLONE_NOBEACONS) {
119510ad9a77SSam Leffler 		/*
119610ad9a77SSam Leffler 		 * Enable s/w beacon miss handling.
119710ad9a77SSam Leffler 		 */
119810ad9a77SSam Leffler 		sc->sc_swbmiss = 1;
119910ad9a77SSam Leffler 	}
1200b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1201b032f27cSSam Leffler 
1202b032f27cSSam Leffler 	/* complete setup */
1203b032f27cSSam Leffler 	ieee80211_vap_attach(vap, ath_media_change, ieee80211_media_status);
1204b032f27cSSam Leffler 	return vap;
1205b032f27cSSam Leffler bad2:
1206b032f27cSSam Leffler 	reclaim_address(sc, mac);
1207b032f27cSSam Leffler 	ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
1208b032f27cSSam Leffler bad:
1209b032f27cSSam Leffler 	free(avp, M_80211_VAP);
1210b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1211b032f27cSSam Leffler 	return NULL;
1212b032f27cSSam Leffler }
1213b032f27cSSam Leffler 
1214b032f27cSSam Leffler static void
1215b032f27cSSam Leffler ath_vap_delete(struct ieee80211vap *vap)
1216b032f27cSSam Leffler {
1217b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1218b032f27cSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
1219b032f27cSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
1220b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1221b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
1222b032f27cSSam Leffler 
1223f52d3452SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: called\n", __func__);
1224b032f27cSSam Leffler 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1225b032f27cSSam Leffler 		/*
1226b032f27cSSam Leffler 		 * Quiesce the hardware while we remove the vap.  In
1227b032f27cSSam Leffler 		 * particular we need to reclaim all references to
1228b032f27cSSam Leffler 		 * the vap state by any frames pending on the tx queues.
1229b032f27cSSam Leffler 		 */
1230b032f27cSSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
1231517526efSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_DEFAULT);		/* stop hw xmit side */
1232517526efSAdrian Chadd 		/* XXX Do all frames from all vaps/nodes need draining here? */
12339a842e8bSAdrian Chadd 		ath_stoprecv(sc, 1);		/* stop recv side */
1234b032f27cSSam Leffler 	}
1235b032f27cSSam Leffler 
1236b032f27cSSam Leffler 	ieee80211_vap_detach(vap);
123716d4de92SAdrian Chadd 
123816d4de92SAdrian Chadd 	/*
123916d4de92SAdrian Chadd 	 * XXX Danger Will Robinson! Danger!
124016d4de92SAdrian Chadd 	 *
124116d4de92SAdrian Chadd 	 * Because ieee80211_vap_detach() can queue a frame (the station
124216d4de92SAdrian Chadd 	 * diassociate message?) after we've drained the TXQ and
124316d4de92SAdrian Chadd 	 * flushed the software TXQ, we will end up with a frame queued
124416d4de92SAdrian Chadd 	 * to a node whose vap is about to be freed.
124516d4de92SAdrian Chadd 	 *
124616d4de92SAdrian Chadd 	 * To work around this, flush the hardware/software again.
124716d4de92SAdrian Chadd 	 * This may be racy - the ath task may be running and the packet
124816d4de92SAdrian Chadd 	 * may be being scheduled between sw->hw txq. Tsk.
124916d4de92SAdrian Chadd 	 *
125016d4de92SAdrian Chadd 	 * TODO: figure out why a new node gets allocated somewhere around
125116d4de92SAdrian Chadd 	 * here (after the ath_tx_swq() call; and after an ath_stop_locked()
125216d4de92SAdrian Chadd 	 * call!)
125316d4de92SAdrian Chadd 	 */
125416d4de92SAdrian Chadd 
125516d4de92SAdrian Chadd 	ath_draintxq(sc, ATH_RESET_DEFAULT);
125616d4de92SAdrian Chadd 
1257b032f27cSSam Leffler 	ATH_LOCK(sc);
1258b032f27cSSam Leffler 	/*
1259b032f27cSSam Leffler 	 * Reclaim beacon state.  Note this must be done before
1260b032f27cSSam Leffler 	 * the vap instance is reclaimed as we may have a reference
1261b032f27cSSam Leffler 	 * to it in the buffer for the beacon frame.
1262b032f27cSSam Leffler 	 */
1263b032f27cSSam Leffler 	if (avp->av_bcbuf != NULL) {
1264b032f27cSSam Leffler 		if (avp->av_bslot != -1) {
1265b032f27cSSam Leffler 			sc->sc_bslot[avp->av_bslot] = NULL;
1266b032f27cSSam Leffler 			sc->sc_nbcnvaps--;
1267b032f27cSSam Leffler 		}
1268b032f27cSSam Leffler 		ath_beacon_return(sc, avp->av_bcbuf);
1269b032f27cSSam Leffler 		avp->av_bcbuf = NULL;
1270b032f27cSSam Leffler 		if (sc->sc_nbcnvaps == 0) {
1271b032f27cSSam Leffler 			sc->sc_stagbeacons = 0;
1272b032f27cSSam Leffler 			if (sc->sc_hastsfadd)
1273b032f27cSSam Leffler 				ath_hal_settsfadjust(sc->sc_ah, 0);
1274b032f27cSSam Leffler 		}
1275b032f27cSSam Leffler 		/*
1276b032f27cSSam Leffler 		 * Reclaim any pending mcast frames for the vap.
1277b032f27cSSam Leffler 		 */
1278b032f27cSSam Leffler 		ath_tx_draintxq(sc, &avp->av_mcastq);
1279b032f27cSSam Leffler 		ATH_TXQ_LOCK_DESTROY(&avp->av_mcastq);
1280b032f27cSSam Leffler 	}
1281b032f27cSSam Leffler 	/*
1282b032f27cSSam Leffler 	 * Update bookkeeping.
1283b032f27cSSam Leffler 	 */
1284b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA) {
1285b032f27cSSam Leffler 		sc->sc_nstavaps--;
1286b032f27cSSam Leffler 		if (sc->sc_nstavaps == 0 && sc->sc_swbmiss)
1287b032f27cSSam Leffler 			sc->sc_swbmiss = 0;
128859aa14a9SRui Paulo 	} else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
128959aa14a9SRui Paulo 	    vap->iv_opmode == IEEE80211_M_MBSS) {
1290b032f27cSSam Leffler 		reclaim_address(sc, vap->iv_myaddr);
1291b032f27cSSam Leffler 		ath_hal_setbssidmask(ah, sc->sc_hwbssidmask);
1292fe0dd789SSam Leffler 		if (vap->iv_opmode == IEEE80211_M_MBSS)
1293fe0dd789SSam Leffler 			sc->sc_nmeshvaps--;
1294b032f27cSSam Leffler 	}
1295b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_WDS)
1296b032f27cSSam Leffler 		sc->sc_nvaps--;
1297584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
129810ad9a77SSam Leffler 	/* TDMA operation ceases when the last vap is destroyed */
129910ad9a77SSam Leffler 	if (sc->sc_tdma && sc->sc_nvaps == 0) {
130010ad9a77SSam Leffler 		sc->sc_tdma = 0;
130110ad9a77SSam Leffler 		sc->sc_swbmiss = 0;
130210ad9a77SSam Leffler 	}
130310ad9a77SSam Leffler #endif
1304b032f27cSSam Leffler 	free(avp, M_80211_VAP);
1305b032f27cSSam Leffler 
1306b032f27cSSam Leffler 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1307b032f27cSSam Leffler 		/*
1308b032f27cSSam Leffler 		 * Restart rx+tx machines if still running (RUNNING will
1309b032f27cSSam Leffler 		 * be reset if we just destroyed the last vap).
1310b032f27cSSam Leffler 		 */
1311b032f27cSSam Leffler 		if (ath_startrecv(sc) != 0)
1312b032f27cSSam Leffler 			if_printf(ifp, "%s: unable to restart recv logic\n",
1313b032f27cSSam Leffler 			    __func__);
1314c89b957aSSam Leffler 		if (sc->sc_beacons) {		/* restart beacons */
1315c89b957aSSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
1316c89b957aSSam Leffler 			if (sc->sc_tdma)
1317c89b957aSSam Leffler 				ath_tdma_config(sc, NULL);
1318c89b957aSSam Leffler 			else
1319c89b957aSSam Leffler #endif
1320b032f27cSSam Leffler 				ath_beacon_config(sc, NULL);
1321c89b957aSSam Leffler 		}
1322b032f27cSSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
1323b032f27cSSam Leffler 	}
132416d4de92SAdrian Chadd 	ATH_UNLOCK(sc);
1325b032f27cSSam Leffler }
1326b032f27cSSam Leffler 
13275591b213SSam Leffler void
13285591b213SSam Leffler ath_suspend(struct ath_softc *sc)
13295591b213SSam Leffler {
1330fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
1331d3ac945bSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
13325591b213SSam Leffler 
1333c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1334c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
13355591b213SSam Leffler 
1336d3ac945bSSam Leffler 	sc->sc_resume_up = (ifp->if_flags & IFF_UP) != 0;
1337d3ac945bSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA)
13385591b213SSam Leffler 		ath_stop(ifp);
1339d3ac945bSSam Leffler 	else
1340d3ac945bSSam Leffler 		ieee80211_suspend_all(ic);
1341d3ac945bSSam Leffler 	/*
1342d3ac945bSSam Leffler 	 * NB: don't worry about putting the chip in low power
1343d3ac945bSSam Leffler 	 * mode; pci will power off our socket on suspend and
1344f29b8b7fSWarner Losh 	 * CardBus detaches the device.
1345d3ac945bSSam Leffler 	 */
1346d3ac945bSSam Leffler }
1347d3ac945bSSam Leffler 
1348d3ac945bSSam Leffler /*
1349d3ac945bSSam Leffler  * Reset the key cache since some parts do not reset the
1350d3ac945bSSam Leffler  * contents on resume.  First we clear all entries, then
1351d3ac945bSSam Leffler  * re-load keys that the 802.11 layer assumes are setup
1352d3ac945bSSam Leffler  * in h/w.
1353d3ac945bSSam Leffler  */
1354d3ac945bSSam Leffler static void
1355d3ac945bSSam Leffler ath_reset_keycache(struct ath_softc *sc)
1356d3ac945bSSam Leffler {
1357d3ac945bSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
1358d3ac945bSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
1359d3ac945bSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1360d3ac945bSSam Leffler 	int i;
1361d3ac945bSSam Leffler 
1362d3ac945bSSam Leffler 	for (i = 0; i < sc->sc_keymax; i++)
1363d3ac945bSSam Leffler 		ath_hal_keyreset(ah, i);
1364d3ac945bSSam Leffler 	ieee80211_crypto_reload_keys(ic);
13655591b213SSam Leffler }
13665591b213SSam Leffler 
13675591b213SSam Leffler void
13685591b213SSam Leffler ath_resume(struct ath_softc *sc)
13695591b213SSam Leffler {
1370fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
1371d3ac945bSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
1372d3ac945bSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1373d3ac945bSSam Leffler 	HAL_STATUS status;
13745591b213SSam Leffler 
1375c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1376c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
13775591b213SSam Leffler 
1378d3ac945bSSam Leffler 	/*
1379d3ac945bSSam Leffler 	 * Must reset the chip before we reload the
1380d3ac945bSSam Leffler 	 * keycache as we were powered down on suspend.
1381d3ac945bSSam Leffler 	 */
1382054d7b69SSam Leffler 	ath_hal_reset(ah, sc->sc_opmode,
1383054d7b69SSam Leffler 	    sc->sc_curchan != NULL ? sc->sc_curchan : ic->ic_curchan,
1384054d7b69SSam Leffler 	    AH_FALSE, &status);
1385d3ac945bSSam Leffler 	ath_reset_keycache(sc);
13867e5eb44dSAdrian Chadd 
13877e5eb44dSAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
13887e5eb44dSAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
13897e5eb44dSAdrian Chadd 
1390a497cd88SAdrian Chadd 	/* Restore the LED configuration */
1391a497cd88SAdrian Chadd 	ath_led_config(sc);
1392a497cd88SAdrian Chadd 	ath_hal_setledstate(ah, HAL_LED_INIT);
1393a497cd88SAdrian Chadd 
1394d3ac945bSSam Leffler 	if (sc->sc_resume_up) {
1395d3ac945bSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_STA) {
1396fc74a9f9SBrooks Davis 			ath_init(sc);
1397a497cd88SAdrian Chadd 			ath_hal_setledstate(ah, HAL_LED_RUN);
1398394f34a5SSam Leffler 			/*
1399394f34a5SSam Leffler 			 * Program the beacon registers using the last rx'd
1400394f34a5SSam Leffler 			 * beacon frame and enable sync on the next beacon
1401394f34a5SSam Leffler 			 * we see.  This should handle the case where we
1402394f34a5SSam Leffler 			 * wakeup and find the same AP and also the case where
1403394f34a5SSam Leffler 			 * we wakeup and need to roam.  For the latter we
1404394f34a5SSam Leffler 			 * should get bmiss events that trigger a roam.
1405394f34a5SSam Leffler 			 */
1406394f34a5SSam Leffler 			ath_beacon_config(sc, NULL);
1407394f34a5SSam Leffler 			sc->sc_syncbeacon = 1;
1408d3ac945bSSam Leffler 		} else
1409d3ac945bSSam Leffler 			ieee80211_resume_all(ic);
14105591b213SSam Leffler 	}
14112fd9aabbSAdrian Chadd 
14122fd9aabbSAdrian Chadd 	/* XXX beacons ? */
14136b59f5e3SSam Leffler }
14145591b213SSam Leffler 
14155591b213SSam Leffler void
14165591b213SSam Leffler ath_shutdown(struct ath_softc *sc)
14175591b213SSam Leffler {
1418fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
14195591b213SSam Leffler 
1420c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1421c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
14225591b213SSam Leffler 
14235591b213SSam Leffler 	ath_stop(ifp);
1424d3ac945bSSam Leffler 	/* NB: no point powering down chip as we're about to reboot */
14255591b213SSam Leffler }
14265591b213SSam Leffler 
1427c42a7b7eSSam Leffler /*
1428c42a7b7eSSam Leffler  * Interrupt handler.  Most of the actual processing is deferred.
1429c42a7b7eSSam Leffler  */
14305591b213SSam Leffler void
14315591b213SSam Leffler ath_intr(void *arg)
14325591b213SSam Leffler {
14335591b213SSam Leffler 	struct ath_softc *sc = arg;
1434fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
14355591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
14366f5fe81eSAdrian Chadd 	HAL_INT status = 0;
14378f939e79SAdrian Chadd 	uint32_t txqs;
14385591b213SSam Leffler 
1439ef27340cSAdrian Chadd 	/*
1440ef27340cSAdrian Chadd 	 * If we're inside a reset path, just print a warning and
1441ef27340cSAdrian Chadd 	 * clear the ISR. The reset routine will finish it for us.
1442ef27340cSAdrian Chadd 	 */
1443ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
1444ef27340cSAdrian Chadd 	if (sc->sc_inreset_cnt) {
1445ef27340cSAdrian Chadd 		HAL_INT status;
1446ef27340cSAdrian Chadd 		ath_hal_getisr(ah, &status);	/* clear ISR */
1447ef27340cSAdrian Chadd 		ath_hal_intrset(ah, 0);		/* disable further intr's */
1448ef27340cSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_ANY,
1449ef27340cSAdrian Chadd 		    "%s: in reset, ignoring: status=0x%x\n",
1450ef27340cSAdrian Chadd 		    __func__, status);
1451ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
1452ef27340cSAdrian Chadd 		return;
1453ef27340cSAdrian Chadd 	}
1454ef27340cSAdrian Chadd 
14555591b213SSam Leffler 	if (sc->sc_invalid) {
14565591b213SSam Leffler 		/*
1457b58b3803SSam Leffler 		 * The hardware is not ready/present, don't touch anything.
1458b58b3803SSam Leffler 		 * Note this can happen early on if the IRQ is shared.
14595591b213SSam Leffler 		 */
1460c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid; ignored\n", __func__);
1461ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
14625591b213SSam Leffler 		return;
14635591b213SSam Leffler 	}
1464ef27340cSAdrian Chadd 	if (!ath_hal_intrpend(ah)) {		/* shared irq, not for us */
1465ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
1466fdd758d4SSam Leffler 		return;
1467ef27340cSAdrian Chadd 	}
1468ef27340cSAdrian Chadd 
146968e8e04eSSam Leffler 	if ((ifp->if_flags & IFF_UP) == 0 ||
147068e8e04eSSam Leffler 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
147168e8e04eSSam Leffler 		HAL_INT status;
147268e8e04eSSam Leffler 
1473c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
1474c42a7b7eSSam Leffler 			__func__, ifp->if_flags);
14755591b213SSam Leffler 		ath_hal_getisr(ah, &status);	/* clear ISR */
14765591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable further intr's */
1477ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
14785591b213SSam Leffler 		return;
14795591b213SSam Leffler 	}
1480ef27340cSAdrian Chadd 
1481c42a7b7eSSam Leffler 	/*
1482c42a7b7eSSam Leffler 	 * Figure out the reason(s) for the interrupt.  Note
1483c42a7b7eSSam Leffler 	 * that the hal returns a pseudo-ISR that may include
1484c42a7b7eSSam Leffler 	 * bits we haven't explicitly enabled so we mask the
1485c42a7b7eSSam Leffler 	 * value to insure we only process bits we requested.
1486c42a7b7eSSam Leffler 	 */
14875591b213SSam Leffler 	ath_hal_getisr(ah, &status);		/* NB: clears ISR too */
1488c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_INTR, "%s: status 0x%x\n", __func__, status);
1489f52d3452SAdrian Chadd 	CTR1(ATH_KTR_INTR, "ath_intr: mask=0x%.8x", status);
149031fdf3d6SAdrian Chadd #ifdef	ATH_KTR_INTR_DEBUG
1491f52d3452SAdrian Chadd 	CTR5(ATH_KTR_INTR,
1492f52d3452SAdrian Chadd 	    "ath_intr: ISR=0x%.8x, ISR_S0=0x%.8x, ISR_S1=0x%.8x, ISR_S2=0x%.8x, ISR_S5=0x%.8x",
1493f52d3452SAdrian Chadd 	    ah->ah_intrstate[0],
1494f52d3452SAdrian Chadd 	    ah->ah_intrstate[1],
1495f52d3452SAdrian Chadd 	    ah->ah_intrstate[2],
1496f52d3452SAdrian Chadd 	    ah->ah_intrstate[3],
1497f52d3452SAdrian Chadd 	    ah->ah_intrstate[6]);
149831fdf3d6SAdrian Chadd #endif
14999467e3f3SAdrian Chadd 
15009467e3f3SAdrian Chadd 	/* Squirrel away SYNC interrupt debugging */
15019467e3f3SAdrian Chadd 	if (ah->ah_syncstate != 0) {
15029467e3f3SAdrian Chadd 		int i;
15039467e3f3SAdrian Chadd 		for (i = 0; i < 32; i++)
15049467e3f3SAdrian Chadd 			if (ah->ah_syncstate & (i << i))
15059467e3f3SAdrian Chadd 				sc->sc_intr_stats.sync_intr[i]++;
15069467e3f3SAdrian Chadd 	}
15079467e3f3SAdrian Chadd 
1508ecddff40SSam Leffler 	status &= sc->sc_imask;			/* discard unasked for bits */
15096f5fe81eSAdrian Chadd 
15106f5fe81eSAdrian Chadd 	/* Short-circuit un-handled interrupts */
1511ef27340cSAdrian Chadd 	if (status == 0x0) {
1512ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
15136f5fe81eSAdrian Chadd 		return;
1514ef27340cSAdrian Chadd 	}
15156f5fe81eSAdrian Chadd 
1516ef27340cSAdrian Chadd 	/*
1517ef27340cSAdrian Chadd 	 * Take a note that we're inside the interrupt handler, so
1518ef27340cSAdrian Chadd 	 * the reset routines know to wait.
1519ef27340cSAdrian Chadd 	 */
1520ef27340cSAdrian Chadd 	sc->sc_intr_cnt++;
1521ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
1522ef27340cSAdrian Chadd 
1523ef27340cSAdrian Chadd 	/*
1524ef27340cSAdrian Chadd 	 * Handle the interrupt. We won't run concurrent with the reset
1525ef27340cSAdrian Chadd 	 * or channel change routines as they'll wait for sc_intr_cnt
1526ef27340cSAdrian Chadd 	 * to be 0 before continuing.
1527ef27340cSAdrian Chadd 	 */
15285591b213SSam Leffler 	if (status & HAL_INT_FATAL) {
15295591b213SSam Leffler 		sc->sc_stats.ast_hardware++;
15305591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
1531*f846cf42SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_fataltask);
15325591b213SSam Leffler 	} else {
1533c42a7b7eSSam Leffler 		if (status & HAL_INT_SWBA) {
1534c42a7b7eSSam Leffler 			/*
1535c42a7b7eSSam Leffler 			 * Software beacon alert--time to send a beacon.
1536c42a7b7eSSam Leffler 			 * Handle beacon transmission directly; deferring
1537c42a7b7eSSam Leffler 			 * this is too slow to meet timing constraints
1538c42a7b7eSSam Leffler 			 * under load.
1539c42a7b7eSSam Leffler 			 */
1540584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
154110ad9a77SSam Leffler 			if (sc->sc_tdma) {
154210ad9a77SSam Leffler 				if (sc->sc_tdmaswba == 0) {
154310ad9a77SSam Leffler 					struct ieee80211com *ic = ifp->if_l2com;
154410ad9a77SSam Leffler 					struct ieee80211vap *vap =
154510ad9a77SSam Leffler 					    TAILQ_FIRST(&ic->ic_vaps);
154610ad9a77SSam Leffler 					ath_tdma_beacon_send(sc, vap);
154710ad9a77SSam Leffler 					sc->sc_tdmaswba =
154810ad9a77SSam Leffler 					    vap->iv_tdma->tdma_bintval;
154910ad9a77SSam Leffler 				} else
155010ad9a77SSam Leffler 					sc->sc_tdmaswba--;
155110ad9a77SSam Leffler 			} else
155210ad9a77SSam Leffler #endif
1553339ccfb3SSam Leffler 			{
1554c42a7b7eSSam Leffler 				ath_beacon_proc(sc, 0);
1555339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
1556339ccfb3SSam Leffler 				/*
1557339ccfb3SSam Leffler 				 * Schedule the rx taskq in case there's no
1558339ccfb3SSam Leffler 				 * traffic so any frames held on the staging
1559339ccfb3SSam Leffler 				 * queue are aged and potentially flushed.
1560339ccfb3SSam Leffler 				 */
1561339ccfb3SSam Leffler 				taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
1562339ccfb3SSam Leffler #endif
1563339ccfb3SSam Leffler 			}
1564c42a7b7eSSam Leffler 		}
15655591b213SSam Leffler 		if (status & HAL_INT_RXEOL) {
15668f939e79SAdrian Chadd 			int imask;
1567f52d3452SAdrian Chadd 			CTR0(ATH_KTR_ERR, "ath_intr: RXEOL");
1568ef27340cSAdrian Chadd 			ATH_PCU_LOCK(sc);
15695591b213SSam Leffler 			/*
15705591b213SSam Leffler 			 * NB: the hardware should re-read the link when
15715591b213SSam Leffler 			 *     RXE bit is written, but it doesn't work at
15725591b213SSam Leffler 			 *     least on older hardware revs.
15735591b213SSam Leffler 			 */
15745591b213SSam Leffler 			sc->sc_stats.ast_rxeol++;
157573f895fcSAdrian Chadd 			/*
157673f895fcSAdrian Chadd 			 * Disable RXEOL/RXORN - prevent an interrupt
157773f895fcSAdrian Chadd 			 * storm until the PCU logic can be reset.
15781fdadc0fSAdrian Chadd 			 * In case the interface is reset some other
15791fdadc0fSAdrian Chadd 			 * way before "sc_kickpcu" is called, don't
15801fdadc0fSAdrian Chadd 			 * modify sc_imask - that way if it is reset
15811fdadc0fSAdrian Chadd 			 * by a call to ath_reset() somehow, the
15821fdadc0fSAdrian Chadd 			 * interrupt mask will be correctly reprogrammed.
158373f895fcSAdrian Chadd 			 */
15848f939e79SAdrian Chadd 			imask = sc->sc_imask;
15851fdadc0fSAdrian Chadd 			imask &= ~(HAL_INT_RXEOL | HAL_INT_RXORN);
15861fdadc0fSAdrian Chadd 			ath_hal_intrset(ah, imask);
15871fdadc0fSAdrian Chadd 			/*
15888f939e79SAdrian Chadd 			 * Only blank sc_rxlink if we've not yet kicked
15898f939e79SAdrian Chadd 			 * the PCU.
15908f939e79SAdrian Chadd 			 *
15918f939e79SAdrian Chadd 			 * This isn't entirely correct - the correct solution
15928f939e79SAdrian Chadd 			 * would be to have a PCU lock and engage that for
15938f939e79SAdrian Chadd 			 * the duration of the PCU fiddling; which would include
15948f939e79SAdrian Chadd 			 * running the RX process. Otherwise we could end up
15958f939e79SAdrian Chadd 			 * messing up the RX descriptor chain and making the
15968f939e79SAdrian Chadd 			 * RX desc list much shorter.
15978f939e79SAdrian Chadd 			 */
15988f939e79SAdrian Chadd 			if (! sc->sc_kickpcu)
15998f939e79SAdrian Chadd 				sc->sc_rxlink = NULL;
16008f939e79SAdrian Chadd 			sc->sc_kickpcu = 1;
16018f939e79SAdrian Chadd 			/*
16021fdadc0fSAdrian Chadd 			 * Enqueue an RX proc, to handled whatever
16031fdadc0fSAdrian Chadd 			 * is in the RX queue.
16041fdadc0fSAdrian Chadd 			 * This will then kick the PCU.
16051fdadc0fSAdrian Chadd 			 */
16061fdadc0fSAdrian Chadd 			taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
1607ef27340cSAdrian Chadd 			ATH_PCU_UNLOCK(sc);
16085591b213SSam Leffler 		}
16095591b213SSam Leffler 		if (status & HAL_INT_TXURN) {
16105591b213SSam Leffler 			sc->sc_stats.ast_txurn++;
16115591b213SSam Leffler 			/* bump tx trigger level */
16125591b213SSam Leffler 			ath_hal_updatetxtriglevel(ah, AH_TRUE);
16135591b213SSam Leffler 		}
16148f939e79SAdrian Chadd 		if (status & HAL_INT_RX) {
16158f939e79SAdrian Chadd 			sc->sc_stats.ast_rx_intr++;
16160bbf5441SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
16178f939e79SAdrian Chadd 		}
16188f939e79SAdrian Chadd 		if (status & HAL_INT_TX) {
16198f939e79SAdrian Chadd 			sc->sc_stats.ast_tx_intr++;
16208f939e79SAdrian Chadd 			/*
16218f939e79SAdrian Chadd 			 * Grab all the currently set bits in the HAL txq bitmap
16228f939e79SAdrian Chadd 			 * and blank them. This is the only place we should be
16238f939e79SAdrian Chadd 			 * doing this.
16248f939e79SAdrian Chadd 			 */
1625ef27340cSAdrian Chadd 			ATH_PCU_LOCK(sc);
16268f939e79SAdrian Chadd 			txqs = 0xffffffff;
16278f939e79SAdrian Chadd 			ath_hal_gettxintrtxqs(sc->sc_ah, &txqs);
16288f939e79SAdrian Chadd 			sc->sc_txq_active |= txqs;
16290bbf5441SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_txtask);
1630ef27340cSAdrian Chadd 			ATH_PCU_UNLOCK(sc);
16318f939e79SAdrian Chadd 		}
16325591b213SSam Leffler 		if (status & HAL_INT_BMISS) {
16335591b213SSam Leffler 			sc->sc_stats.ast_bmiss++;
16340bbf5441SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_bmisstask);
16355591b213SSam Leffler 		}
16366ad02dbaSAdrian Chadd 		if (status & HAL_INT_GTT)
16376ad02dbaSAdrian Chadd 			sc->sc_stats.ast_tx_timeout++;
16385594f5c0SAdrian Chadd 		if (status & HAL_INT_CST)
16395594f5c0SAdrian Chadd 			sc->sc_stats.ast_tx_cst++;
1640c42a7b7eSSam Leffler 		if (status & HAL_INT_MIB) {
1641c42a7b7eSSam Leffler 			sc->sc_stats.ast_mib++;
1642ef27340cSAdrian Chadd 			ATH_PCU_LOCK(sc);
1643c42a7b7eSSam Leffler 			/*
1644c42a7b7eSSam Leffler 			 * Disable interrupts until we service the MIB
1645c42a7b7eSSam Leffler 			 * interrupt; otherwise it will continue to fire.
1646c42a7b7eSSam Leffler 			 */
1647c42a7b7eSSam Leffler 			ath_hal_intrset(ah, 0);
1648c42a7b7eSSam Leffler 			/*
1649c42a7b7eSSam Leffler 			 * Let the hal handle the event.  We assume it will
1650c42a7b7eSSam Leffler 			 * clear whatever condition caused the interrupt.
1651c42a7b7eSSam Leffler 			 */
1652ffa2cab6SSam Leffler 			ath_hal_mibevent(ah, &sc->sc_halstats);
16538f939e79SAdrian Chadd 			/*
16548f939e79SAdrian Chadd 			 * Don't reset the interrupt if we've just
16558f939e79SAdrian Chadd 			 * kicked the PCU, or we may get a nested
16568f939e79SAdrian Chadd 			 * RXEOL before the rxproc has had a chance
16578f939e79SAdrian Chadd 			 * to run.
16588f939e79SAdrian Chadd 			 */
16598f939e79SAdrian Chadd 			if (sc->sc_kickpcu == 0)
1660c42a7b7eSSam Leffler 				ath_hal_intrset(ah, sc->sc_imask);
1661ef27340cSAdrian Chadd 			ATH_PCU_UNLOCK(sc);
1662c42a7b7eSSam Leffler 		}
16639c4fc1e8SSam Leffler 		if (status & HAL_INT_RXORN) {
16649c4fc1e8SSam Leffler 			/* NB: hal marks HAL_INT_FATAL when RXORN is fatal */
1665f52d3452SAdrian Chadd 			CTR0(ATH_KTR_ERR, "ath_intr: RXORN");
16669c4fc1e8SSam Leffler 			sc->sc_stats.ast_rxorn++;
16679c4fc1e8SSam Leffler 		}
16685591b213SSam Leffler 	}
1669ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
1670ef27340cSAdrian Chadd 	sc->sc_intr_cnt--;
1671ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
16725591b213SSam Leffler }
16735591b213SSam Leffler 
16745591b213SSam Leffler static void
16755591b213SSam Leffler ath_fatal_proc(void *arg, int pending)
16765591b213SSam Leffler {
16775591b213SSam Leffler 	struct ath_softc *sc = arg;
1678fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
167916c8acaaSSam Leffler 	u_int32_t *state;
168016c8acaaSSam Leffler 	u_int32_t len;
168168e8e04eSSam Leffler 	void *sp;
16825591b213SSam Leffler 
1683c42a7b7eSSam Leffler 	if_printf(ifp, "hardware error; resetting\n");
168416c8acaaSSam Leffler 	/*
168516c8acaaSSam Leffler 	 * Fatal errors are unrecoverable.  Typically these
168616c8acaaSSam Leffler 	 * are caused by DMA errors.  Collect h/w state from
168716c8acaaSSam Leffler 	 * the hal so we can diagnose what's going on.
168816c8acaaSSam Leffler 	 */
168968e8e04eSSam Leffler 	if (ath_hal_getfatalstate(sc->sc_ah, &sp, &len)) {
169016c8acaaSSam Leffler 		KASSERT(len >= 6*sizeof(u_int32_t), ("len %u bytes", len));
169168e8e04eSSam Leffler 		state = sp;
169216c8acaaSSam Leffler 		if_printf(ifp, "0x%08x 0x%08x 0x%08x, 0x%08x 0x%08x 0x%08x\n",
169316c8acaaSSam Leffler 		    state[0], state[1] , state[2], state[3],
169416c8acaaSSam Leffler 		    state[4], state[5]);
169516c8acaaSSam Leffler 	}
1696517526efSAdrian Chadd 	ath_reset(ifp, ATH_RESET_NOLOSS);
16975591b213SSam Leffler }
16985591b213SSam Leffler 
16995591b213SSam Leffler static void
1700b032f27cSSam Leffler ath_bmiss_vap(struct ieee80211vap *vap)
17015591b213SSam Leffler {
170259fbb257SSam Leffler 	/*
170359fbb257SSam Leffler 	 * Workaround phantom bmiss interrupts by sanity-checking
170459fbb257SSam Leffler 	 * the time of our last rx'd frame.  If it is within the
170559fbb257SSam Leffler 	 * beacon miss interval then ignore the interrupt.  If it's
170659fbb257SSam Leffler 	 * truly a bmiss we'll get another interrupt soon and that'll
170759fbb257SSam Leffler 	 * be dispatched up for processing.  Note this applies only
170859fbb257SSam Leffler 	 * for h/w beacon miss events.
170959fbb257SSam Leffler 	 */
171059fbb257SSam Leffler 	if ((vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) == 0) {
1711a7ace843SSam Leffler 		struct ifnet *ifp = vap->iv_ic->ic_ifp;
1712a7ace843SSam Leffler 		struct ath_softc *sc = ifp->if_softc;
1713d7736e13SSam Leffler 		u_int64_t lastrx = sc->sc_lastrx;
1714d7736e13SSam Leffler 		u_int64_t tsf = ath_hal_gettsf64(sc->sc_ah);
171580767531SAdrian Chadd 		/* XXX should take a locked ref to iv_bss */
1716d7736e13SSam Leffler 		u_int bmisstimeout =
1717b032f27cSSam Leffler 			vap->iv_bmissthreshold * vap->iv_bss->ni_intval * 1024;
1718d7736e13SSam Leffler 
1719d7736e13SSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
1720d7736e13SSam Leffler 		    "%s: tsf %llu lastrx %lld (%llu) bmiss %u\n",
1721d7736e13SSam Leffler 		    __func__, (unsigned long long) tsf,
1722d7736e13SSam Leffler 		    (unsigned long long)(tsf - lastrx),
1723d7736e13SSam Leffler 		    (unsigned long long) lastrx, bmisstimeout);
172459fbb257SSam Leffler 
172559fbb257SSam Leffler 		if (tsf - lastrx <= bmisstimeout) {
1726d7736e13SSam Leffler 			sc->sc_stats.ast_bmiss_phantom++;
172759fbb257SSam Leffler 			return;
172859fbb257SSam Leffler 		}
172959fbb257SSam Leffler 	}
173059fbb257SSam Leffler 	ATH_VAP(vap)->av_bmiss(vap);
1731e585d188SSam Leffler }
1732b032f27cSSam Leffler 
1733459bc4f0SSam Leffler static int
1734459bc4f0SSam Leffler ath_hal_gethangstate(struct ath_hal *ah, uint32_t mask, uint32_t *hangs)
1735459bc4f0SSam Leffler {
1736459bc4f0SSam Leffler 	uint32_t rsize;
1737459bc4f0SSam Leffler 	void *sp;
1738459bc4f0SSam Leffler 
173925c96056SAdrian Chadd 	if (!ath_hal_getdiagstate(ah, HAL_DIAG_CHECK_HANGS, &mask, sizeof(mask), &sp, &rsize))
1740459bc4f0SSam Leffler 		return 0;
1741459bc4f0SSam Leffler 	KASSERT(rsize == sizeof(uint32_t), ("resultsize %u", rsize));
1742459bc4f0SSam Leffler 	*hangs = *(uint32_t *)sp;
1743459bc4f0SSam Leffler 	return 1;
1744459bc4f0SSam Leffler }
1745459bc4f0SSam Leffler 
1746b032f27cSSam Leffler static void
1747b032f27cSSam Leffler ath_bmiss_proc(void *arg, int pending)
1748b032f27cSSam Leffler {
1749b032f27cSSam Leffler 	struct ath_softc *sc = arg;
1750b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
1751459bc4f0SSam Leffler 	uint32_t hangs;
1752b032f27cSSam Leffler 
1753b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: pending %u\n", __func__, pending);
1754459bc4f0SSam Leffler 
1755459bc4f0SSam Leffler 	if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0) {
17564fa8d4efSDaniel Eischen 		if_printf(ifp, "bb hang detected (0x%x), resetting\n", hangs);
1757517526efSAdrian Chadd 		ath_reset(ifp, ATH_RESET_NOLOSS);
1758459bc4f0SSam Leffler 	} else
1759b032f27cSSam Leffler 		ieee80211_beacon_miss(ifp->if_l2com);
17605591b213SSam Leffler }
17615591b213SSam Leffler 
1762724c193aSSam Leffler /*
1763b032f27cSSam Leffler  * Handle TKIP MIC setup to deal hardware that doesn't do MIC
1764b032f27cSSam Leffler  * calcs together with WME.  If necessary disable the crypto
1765b032f27cSSam Leffler  * hardware and mark the 802.11 state so keys will be setup
1766b032f27cSSam Leffler  * with the MIC work done in software.
1767b032f27cSSam Leffler  */
1768b032f27cSSam Leffler static void
1769b032f27cSSam Leffler ath_settkipmic(struct ath_softc *sc)
1770b032f27cSSam Leffler {
1771b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
1772b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
1773b032f27cSSam Leffler 
1774b032f27cSSam Leffler 	if ((ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIP) && !sc->sc_wmetkipmic) {
1775b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_WME) {
1776b032f27cSSam Leffler 			ath_hal_settkipmic(sc->sc_ah, AH_FALSE);
1777b032f27cSSam Leffler 			ic->ic_cryptocaps &= ~IEEE80211_CRYPTO_TKIPMIC;
1778b032f27cSSam Leffler 		} else {
1779b032f27cSSam Leffler 			ath_hal_settkipmic(sc->sc_ah, AH_TRUE);
1780b032f27cSSam Leffler 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
1781b032f27cSSam Leffler 		}
1782b032f27cSSam Leffler 	}
1783b032f27cSSam Leffler }
1784b032f27cSSam Leffler 
17855591b213SSam Leffler static void
17865591b213SSam Leffler ath_init(void *arg)
17875591b213SSam Leffler {
17885591b213SSam Leffler 	struct ath_softc *sc = (struct ath_softc *) arg;
1789fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
1790b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
17915591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
17925591b213SSam Leffler 	HAL_STATUS status;
17935591b213SSam Leffler 
1794c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
1795c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
17965591b213SSam Leffler 
1797f0b2a0beSSam Leffler 	ATH_LOCK(sc);
17985591b213SSam Leffler 	/*
17995591b213SSam Leffler 	 * Stop anything previously setup.  This is safe
18005591b213SSam Leffler 	 * whether this is the first time through or not.
18015591b213SSam Leffler 	 */
1802c42a7b7eSSam Leffler 	ath_stop_locked(ifp);
18035591b213SSam Leffler 
18045591b213SSam Leffler 	/*
18055591b213SSam Leffler 	 * The basic interface to setting the hardware in a good
18065591b213SSam Leffler 	 * state is ``reset''.  On return the hardware is known to
18075591b213SSam Leffler 	 * be powered up and with interrupts disabled.  This must
18085591b213SSam Leffler 	 * be followed by initialization of the appropriate bits
18095591b213SSam Leffler 	 * and then setup of the interrupt mask.
18105591b213SSam Leffler 	 */
1811b032f27cSSam Leffler 	ath_settkipmic(sc);
181259efa8b5SSam Leffler 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_FALSE, &status)) {
18135591b213SSam Leffler 		if_printf(ifp, "unable to reset hardware; hal status %u\n",
18145591b213SSam Leffler 			status);
1815b032f27cSSam Leffler 		ATH_UNLOCK(sc);
1816b032f27cSSam Leffler 		return;
18175591b213SSam Leffler 	}
1818b032f27cSSam Leffler 	ath_chan_change(sc, ic->ic_curchan);
18195591b213SSam Leffler 
182048237774SAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
182148237774SAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
182248237774SAdrian Chadd 
18235591b213SSam Leffler 	/*
1824c59005e9SSam Leffler 	 * Likewise this is set during reset so update
1825c59005e9SSam Leffler 	 * state cached in the driver.
1826c59005e9SSam Leffler 	 */
1827c59005e9SSam Leffler 	sc->sc_diversity = ath_hal_getdiversity(ah);
18282dc7fcc4SSam Leffler 	sc->sc_lastlongcal = 0;
18292dc7fcc4SSam Leffler 	sc->sc_resetcal = 1;
18302dc7fcc4SSam Leffler 	sc->sc_lastcalreset = 0;
1831a108ab63SAdrian Chadd 	sc->sc_lastani = 0;
1832a108ab63SAdrian Chadd 	sc->sc_lastshortcal = 0;
1833a108ab63SAdrian Chadd 	sc->sc_doresetcal = AH_FALSE;
18342fd9aabbSAdrian Chadd 	/*
18352fd9aabbSAdrian Chadd 	 * Beacon timers were cleared here; give ath_newstate()
18362fd9aabbSAdrian Chadd 	 * a hint that the beacon timers should be poked when
18372fd9aabbSAdrian Chadd 	 * things transition to the RUN state.
18382fd9aabbSAdrian Chadd 	 */
18392fd9aabbSAdrian Chadd 	sc->sc_beacons = 0;
1840c42a7b7eSSam Leffler 
1841c42a7b7eSSam Leffler 	/*
18428f939e79SAdrian Chadd 	 * Initial aggregation settings.
18438f939e79SAdrian Chadd 	 */
18448f939e79SAdrian Chadd 	sc->sc_hwq_limit = ATH_AGGR_MIN_QDEPTH;
18458f939e79SAdrian Chadd 	sc->sc_tid_hwq_lo = ATH_AGGR_SCHED_LOW;
18468f939e79SAdrian Chadd 	sc->sc_tid_hwq_hi = ATH_AGGR_SCHED_HIGH;
18478f939e79SAdrian Chadd 
18488f939e79SAdrian Chadd 	/*
18495591b213SSam Leffler 	 * Setup the hardware after reset: the key cache
18505591b213SSam Leffler 	 * is filled as needed and the receive engine is
18515591b213SSam Leffler 	 * set going.  Frame transmit is handled entirely
18525591b213SSam Leffler 	 * in the frame output path; there's nothing to do
18535591b213SSam Leffler 	 * here except setup the interrupt mask.
18545591b213SSam Leffler 	 */
18555591b213SSam Leffler 	if (ath_startrecv(sc) != 0) {
18565591b213SSam Leffler 		if_printf(ifp, "unable to start recv logic\n");
1857b032f27cSSam Leffler 		ATH_UNLOCK(sc);
1858b032f27cSSam Leffler 		return;
18595591b213SSam Leffler 	}
18605591b213SSam Leffler 
18615591b213SSam Leffler 	/*
18625591b213SSam Leffler 	 * Enable interrupts.
18635591b213SSam Leffler 	 */
18645591b213SSam Leffler 	sc->sc_imask = HAL_INT_RX | HAL_INT_TX
18655591b213SSam Leffler 		  | HAL_INT_RXEOL | HAL_INT_RXORN
18665591b213SSam Leffler 		  | HAL_INT_FATAL | HAL_INT_GLOBAL;
1867c42a7b7eSSam Leffler 	/*
1868c42a7b7eSSam Leffler 	 * Enable MIB interrupts when there are hardware phy counters.
1869c42a7b7eSSam Leffler 	 * Note we only do this (at the moment) for station mode.
1870c42a7b7eSSam Leffler 	 */
1871c42a7b7eSSam Leffler 	if (sc->sc_needmib && ic->ic_opmode == IEEE80211_M_STA)
1872c42a7b7eSSam Leffler 		sc->sc_imask |= HAL_INT_MIB;
18735591b213SSam Leffler 
18745594f5c0SAdrian Chadd 	/* Enable global TX timeout and carrier sense timeout if available */
18756ad02dbaSAdrian Chadd 	if (ath_hal_gtxto_supported(ah))
18763788ebedSAdrian Chadd 		sc->sc_imask |= HAL_INT_GTT;
1877d0a0ebc6SAdrian Chadd 
1878d0a0ebc6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: imask=0x%x\n",
1879d0a0ebc6SAdrian Chadd 		__func__, sc->sc_imask);
18806ad02dbaSAdrian Chadd 
188113f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
18822e986da5SSam Leffler 	callout_reset(&sc->sc_wd_ch, hz, ath_watchdog, sc);
1883b032f27cSSam Leffler 	ath_hal_intrset(ah, sc->sc_imask);
18845591b213SSam Leffler 
1885b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1886b032f27cSSam Leffler 
188786e07743SSam Leffler #ifdef ATH_TX99_DIAG
188886e07743SSam Leffler 	if (sc->sc_tx99 != NULL)
188986e07743SSam Leffler 		sc->sc_tx99->start(sc->sc_tx99);
189086e07743SSam Leffler 	else
189186e07743SSam Leffler #endif
1892b032f27cSSam Leffler 	ieee80211_start_all(ic);		/* start all vap's */
18935591b213SSam Leffler }
18945591b213SSam Leffler 
18955591b213SSam Leffler static void
1896c42a7b7eSSam Leffler ath_stop_locked(struct ifnet *ifp)
18975591b213SSam Leffler {
18985591b213SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
18995591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
19005591b213SSam Leffler 
1901c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid %u if_flags 0x%x\n",
1902c42a7b7eSSam Leffler 		__func__, sc->sc_invalid, ifp->if_flags);
19035591b213SSam Leffler 
1904c42a7b7eSSam Leffler 	ATH_LOCK_ASSERT(sc);
190513f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
19065591b213SSam Leffler 		/*
19075591b213SSam Leffler 		 * Shutdown the hardware and driver:
1908c42a7b7eSSam Leffler 		 *    reset 802.11 state machine
19095591b213SSam Leffler 		 *    turn off timers
1910c42a7b7eSSam Leffler 		 *    disable interrupts
1911c42a7b7eSSam Leffler 		 *    turn off the radio
19125591b213SSam Leffler 		 *    clear transmit machinery
19135591b213SSam Leffler 		 *    clear receive machinery
19145591b213SSam Leffler 		 *    drain and release tx queues
19155591b213SSam Leffler 		 *    reclaim beacon resources
19165591b213SSam Leffler 		 *    power down hardware
19175591b213SSam Leffler 		 *
19185591b213SSam Leffler 		 * Note that some of this work is not possible if the
19195591b213SSam Leffler 		 * hardware is gone (invalid).
19205591b213SSam Leffler 		 */
192186e07743SSam Leffler #ifdef ATH_TX99_DIAG
192286e07743SSam Leffler 		if (sc->sc_tx99 != NULL)
192386e07743SSam Leffler 			sc->sc_tx99->stop(sc->sc_tx99);
192486e07743SSam Leffler #endif
19252e986da5SSam Leffler 		callout_stop(&sc->sc_wd_ch);
19262e986da5SSam Leffler 		sc->sc_wd_timer = 0;
192713f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1928c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
19293e50ec2cSSam Leffler 			if (sc->sc_softled) {
19303e50ec2cSSam Leffler 				callout_stop(&sc->sc_ledtimer);
19313e50ec2cSSam Leffler 				ath_hal_gpioset(ah, sc->sc_ledpin,
19323e50ec2cSSam Leffler 					!sc->sc_ledon);
19333e50ec2cSSam Leffler 				sc->sc_blinking = 0;
19343e50ec2cSSam Leffler 			}
19355591b213SSam Leffler 			ath_hal_intrset(ah, 0);
1936c42a7b7eSSam Leffler 		}
1937517526efSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_DEFAULT);
1938c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
19399a842e8bSAdrian Chadd 			ath_stoprecv(sc, 1);
1940c42a7b7eSSam Leffler 			ath_hal_phydisable(ah);
1941c42a7b7eSSam Leffler 		} else
19425591b213SSam Leffler 			sc->sc_rxlink = NULL;
1943b032f27cSSam Leffler 		ath_beacon_free(sc);	/* XXX not needed */
1944c42a7b7eSSam Leffler 	}
1945c42a7b7eSSam Leffler }
1946c42a7b7eSSam Leffler 
1947ef27340cSAdrian Chadd #define	MAX_TXRX_ITERATIONS	1000
1948ef27340cSAdrian Chadd static void
194921008bf1SAdrian Chadd ath_txrx_stop_locked(struct ath_softc *sc)
1950ef27340cSAdrian Chadd {
1951ef27340cSAdrian Chadd 	int i = MAX_TXRX_ITERATIONS;
1952ef27340cSAdrian Chadd 
1953ef27340cSAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
195421008bf1SAdrian Chadd 	ATH_PCU_LOCK_ASSERT(sc);
195521008bf1SAdrian Chadd 
1956ef27340cSAdrian Chadd 	/*
1957ef27340cSAdrian Chadd 	 * Sleep until all the pending operations have completed.
1958ef27340cSAdrian Chadd 	 *
1959ef27340cSAdrian Chadd 	 * The caller must ensure that reset has been incremented
1960ef27340cSAdrian Chadd 	 * or the pending operations may continue being queued.
1961ef27340cSAdrian Chadd 	 */
1962ef27340cSAdrian Chadd 	while (sc->sc_rxproc_cnt || sc->sc_txproc_cnt ||
1963ef27340cSAdrian Chadd 	    sc->sc_txstart_cnt || sc->sc_intr_cnt) {
1964ef27340cSAdrian Chadd 		if (i <= 0)
1965ef27340cSAdrian Chadd 			break;
1966a2d8240dSAdrian Chadd 		msleep(sc, &sc->sc_pcu_mtx, 0, "ath_txrx_stop", 1);
1967ef27340cSAdrian Chadd 		i--;
1968ef27340cSAdrian Chadd 	}
1969ef27340cSAdrian Chadd 
1970ef27340cSAdrian Chadd 	if (i <= 0)
1971ef27340cSAdrian Chadd 		device_printf(sc->sc_dev,
1972ef27340cSAdrian Chadd 		    "%s: didn't finish after %d iterations\n",
1973ef27340cSAdrian Chadd 		    __func__, MAX_TXRX_ITERATIONS);
1974ef27340cSAdrian Chadd }
1975ef27340cSAdrian Chadd #undef	MAX_TXRX_ITERATIONS
1976ef27340cSAdrian Chadd 
1977e78719adSAdrian Chadd #if 0
1978ef27340cSAdrian Chadd static void
197921008bf1SAdrian Chadd ath_txrx_stop(struct ath_softc *sc)
198021008bf1SAdrian Chadd {
198121008bf1SAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
198221008bf1SAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
198321008bf1SAdrian Chadd 
198421008bf1SAdrian Chadd 	ATH_PCU_LOCK(sc);
198521008bf1SAdrian Chadd 	ath_txrx_stop_locked(sc);
198621008bf1SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
198721008bf1SAdrian Chadd }
1988e78719adSAdrian Chadd #endif
198921008bf1SAdrian Chadd 
199021008bf1SAdrian Chadd static void
1991ef27340cSAdrian Chadd ath_txrx_start(struct ath_softc *sc)
1992ef27340cSAdrian Chadd {
1993ef27340cSAdrian Chadd 
1994ef27340cSAdrian Chadd 	taskqueue_unblock(sc->sc_tq);
1995ef27340cSAdrian Chadd }
1996ef27340cSAdrian Chadd 
1997ee321975SAdrian Chadd /*
1998ee321975SAdrian Chadd  * Grab the reset lock, and wait around until noone else
1999ee321975SAdrian Chadd  * is trying to do anything with it.
2000ee321975SAdrian Chadd  *
2001ee321975SAdrian Chadd  * This is totally horrible but we can't hold this lock for
2002ee321975SAdrian Chadd  * long enough to do TX/RX or we end up with net80211/ip stack
2003ee321975SAdrian Chadd  * LORs and eventual deadlock.
2004ee321975SAdrian Chadd  *
2005ee321975SAdrian Chadd  * "dowait" signals whether to spin, waiting for the reset
2006ee321975SAdrian Chadd  * lock count to reach 0. This should (for now) only be used
2007ee321975SAdrian Chadd  * during the reset path, as the rest of the code may not
2008ee321975SAdrian Chadd  * be locking-reentrant enough to behave correctly.
2009ee321975SAdrian Chadd  *
2010ee321975SAdrian Chadd  * Another, cleaner way should be found to serialise all of
2011ee321975SAdrian Chadd  * these operations.
2012ee321975SAdrian Chadd  */
2013ee321975SAdrian Chadd #define	MAX_RESET_ITERATIONS	10
2014ee321975SAdrian Chadd static int
2015ee321975SAdrian Chadd ath_reset_grablock(struct ath_softc *sc, int dowait)
2016ee321975SAdrian Chadd {
2017ee321975SAdrian Chadd 	int w = 0;
2018ee321975SAdrian Chadd 	int i = MAX_RESET_ITERATIONS;
2019ee321975SAdrian Chadd 
2020ee321975SAdrian Chadd 	ATH_PCU_LOCK_ASSERT(sc);
2021ee321975SAdrian Chadd 	do {
2022ee321975SAdrian Chadd 		if (sc->sc_inreset_cnt == 0) {
2023ee321975SAdrian Chadd 			w = 1;
2024ee321975SAdrian Chadd 			break;
2025ee321975SAdrian Chadd 		}
2026ee321975SAdrian Chadd 		if (dowait == 0) {
2027ee321975SAdrian Chadd 			w = 0;
2028ee321975SAdrian Chadd 			break;
2029ee321975SAdrian Chadd 		}
2030ee321975SAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2031ee321975SAdrian Chadd 		pause("ath_reset_grablock", 1);
2032ee321975SAdrian Chadd 		i--;
2033ee321975SAdrian Chadd 		ATH_PCU_LOCK(sc);
2034ee321975SAdrian Chadd 	} while (i > 0);
2035ee321975SAdrian Chadd 
2036ee321975SAdrian Chadd 	/*
2037ee321975SAdrian Chadd 	 * We always increment the refcounter, regardless
2038ee321975SAdrian Chadd 	 * of whether we succeeded to get it in an exclusive
2039ee321975SAdrian Chadd 	 * way.
2040ee321975SAdrian Chadd 	 */
2041ee321975SAdrian Chadd 	sc->sc_inreset_cnt++;
2042ee321975SAdrian Chadd 
2043ee321975SAdrian Chadd 	if (i <= 0)
2044ee321975SAdrian Chadd 		device_printf(sc->sc_dev,
2045ee321975SAdrian Chadd 		    "%s: didn't finish after %d iterations\n",
2046ee321975SAdrian Chadd 		    __func__, MAX_RESET_ITERATIONS);
2047ee321975SAdrian Chadd 
2048ee321975SAdrian Chadd 	if (w == 0)
2049ee321975SAdrian Chadd 		device_printf(sc->sc_dev,
2050ee321975SAdrian Chadd 		    "%s: warning, recursive reset path!\n",
2051ee321975SAdrian Chadd 		    __func__);
2052ee321975SAdrian Chadd 
2053ee321975SAdrian Chadd 	return w;
2054ee321975SAdrian Chadd }
2055ee321975SAdrian Chadd #undef MAX_RESET_ITERATIONS
2056ee321975SAdrian Chadd 
2057ee321975SAdrian Chadd /*
2058ee321975SAdrian Chadd  * XXX TODO: write ath_reset_releaselock
2059ee321975SAdrian Chadd  */
2060ee321975SAdrian Chadd 
2061c42a7b7eSSam Leffler static void
2062c42a7b7eSSam Leffler ath_stop(struct ifnet *ifp)
2063c42a7b7eSSam Leffler {
2064c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2065c42a7b7eSSam Leffler 
2066c42a7b7eSSam Leffler 	ATH_LOCK(sc);
2067c42a7b7eSSam Leffler 	ath_stop_locked(ifp);
2068f0b2a0beSSam Leffler 	ATH_UNLOCK(sc);
20695591b213SSam Leffler }
20705591b213SSam Leffler 
20715591b213SSam Leffler /*
20725591b213SSam Leffler  * Reset the hardware w/o losing operational state.  This is
20735591b213SSam Leffler  * basically a more efficient way of doing ath_stop, ath_init,
20745591b213SSam Leffler  * followed by state transitions to the current 802.11
2075c42a7b7eSSam Leffler  * operational state.  Used to recover from various errors and
2076c42a7b7eSSam Leffler  * to reset or reload hardware state.
20775591b213SSam Leffler  */
20786079fdbeSAdrian Chadd int
2079517526efSAdrian Chadd ath_reset(struct ifnet *ifp, ATH_RESET_TYPE reset_type)
20805591b213SSam Leffler {
2081c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2082b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
20835591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
20845591b213SSam Leffler 	HAL_STATUS status;
2085ef27340cSAdrian Chadd 	int i;
20865591b213SSam Leffler 
2087f52d3452SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: called\n", __func__);
208816d4de92SAdrian Chadd 
2089ee321975SAdrian Chadd 	/* Ensure ATH_LOCK isn't held; ath_rx_proc can't be locked */
2090ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
2091ef27340cSAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
2092ef27340cSAdrian Chadd 
2093d52f7132SAdrian Chadd 	/* Try to (stop any further TX/RX from occuring */
2094d52f7132SAdrian Chadd 	taskqueue_block(sc->sc_tq);
2095d52f7132SAdrian Chadd 
2096ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2097e78719adSAdrian Chadd 	ath_hal_intrset(ah, 0);		/* disable interrupts */
2098e78719adSAdrian Chadd 	ath_txrx_stop_locked(sc);	/* Ensure TX/RX is stopped */
2099ee321975SAdrian Chadd 	if (ath_reset_grablock(sc, 1) == 0) {
2100ee321975SAdrian Chadd 		device_printf(sc->sc_dev, "%s: concurrent reset! Danger!\n",
2101ef27340cSAdrian Chadd 		    __func__);
2102ef27340cSAdrian Chadd 	}
2103ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2104ef27340cSAdrian Chadd 
2105f52d3452SAdrian Chadd 	/*
21069a842e8bSAdrian Chadd 	 * Should now wait for pending TX/RX to complete
21079a842e8bSAdrian Chadd 	 * and block future ones from occuring. This needs to be
21089a842e8bSAdrian Chadd 	 * done before the TX queue is drained.
2109f52d3452SAdrian Chadd 	 */
2110ef27340cSAdrian Chadd 	ath_draintxq(sc, reset_type);	/* stop xmit side */
2111ef27340cSAdrian Chadd 
2112ef27340cSAdrian Chadd 	/*
2113ef27340cSAdrian Chadd 	 * Regardless of whether we're doing a no-loss flush or
2114ef27340cSAdrian Chadd 	 * not, stop the PCU and handle what's in the RX queue.
2115ef27340cSAdrian Chadd 	 * That way frames aren't dropped which shouldn't be.
2116ef27340cSAdrian Chadd 	 */
21179a842e8bSAdrian Chadd 	ath_stoprecv(sc, (reset_type != ATH_RESET_NOLOSS));
2118ef27340cSAdrian Chadd 	ath_rx_proc(sc, 0);
2119ef27340cSAdrian Chadd 
2120b032f27cSSam Leffler 	ath_settkipmic(sc);		/* configure TKIP MIC handling */
21215591b213SSam Leffler 	/* NB: indicate channel change so we do a full reset */
212259efa8b5SSam Leffler 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_TRUE, &status))
21235591b213SSam Leffler 		if_printf(ifp, "%s: unable to reset hardware; hal status %u\n",
21245591b213SSam Leffler 			__func__, status);
2125c59005e9SSam Leffler 	sc->sc_diversity = ath_hal_getdiversity(ah);
212648237774SAdrian Chadd 
212748237774SAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
212848237774SAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
212948237774SAdrian Chadd 
213068e8e04eSSam Leffler 	if (ath_startrecv(sc) != 0)	/* restart recv */
213168e8e04eSSam Leffler 		if_printf(ifp, "%s: unable to start recv logic\n", __func__);
2132c42a7b7eSSam Leffler 	/*
2133c42a7b7eSSam Leffler 	 * We may be doing a reset in response to an ioctl
2134c42a7b7eSSam Leffler 	 * that changes the channel so update any state that
2135c42a7b7eSSam Leffler 	 * might change as a result.
2136c42a7b7eSSam Leffler 	 */
2137724c193aSSam Leffler 	ath_chan_change(sc, ic->ic_curchan);
2138c89b957aSSam Leffler 	if (sc->sc_beacons) {		/* restart beacons */
2139584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
214010ad9a77SSam Leffler 		if (sc->sc_tdma)
214110ad9a77SSam Leffler 			ath_tdma_config(sc, NULL);
214210ad9a77SSam Leffler 		else
214310ad9a77SSam Leffler #endif
2144c89b957aSSam Leffler 			ath_beacon_config(sc, NULL);
214510ad9a77SSam Leffler 	}
2146c42a7b7eSSam Leffler 
2147ef27340cSAdrian Chadd 	/*
2148ef27340cSAdrian Chadd 	 * Release the reset lock and re-enable interrupts here.
2149ef27340cSAdrian Chadd 	 * If an interrupt was being processed in ath_intr(),
2150ef27340cSAdrian Chadd 	 * it would disable interrupts at this point. So we have
2151ef27340cSAdrian Chadd 	 * to atomically enable interrupts and decrement the
2152ef27340cSAdrian Chadd 	 * reset counter - this way ath_intr() doesn't end up
2153ef27340cSAdrian Chadd 	 * disabling interrupts without a corresponding enable
2154ef27340cSAdrian Chadd 	 * in the rest or channel change path.
2155ef27340cSAdrian Chadd 	 */
2156ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2157ef27340cSAdrian Chadd 	sc->sc_inreset_cnt--;
2158ef27340cSAdrian Chadd 	/* XXX only do this if sc_inreset_cnt == 0? */
2159ef27340cSAdrian Chadd 	ath_hal_intrset(ah, sc->sc_imask);
2160ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2161ef27340cSAdrian Chadd 
2162ef27340cSAdrian Chadd 	/*
2163ef27340cSAdrian Chadd 	 * TX and RX can be started here. If it were started with
2164ef27340cSAdrian Chadd 	 * sc_inreset_cnt > 0, the TX and RX path would abort.
2165ef27340cSAdrian Chadd 	 * Thus if this is a nested call through the reset or
2166ef27340cSAdrian Chadd 	 * channel change code, TX completion will occur but
2167ef27340cSAdrian Chadd 	 * RX completion and ath_start / ath_tx_start will not
2168ef27340cSAdrian Chadd 	 * run.
2169ef27340cSAdrian Chadd 	 */
2170ef27340cSAdrian Chadd 
2171ef27340cSAdrian Chadd 	/* Restart TX/RX as needed */
2172ef27340cSAdrian Chadd 	ath_txrx_start(sc);
2173ef27340cSAdrian Chadd 
2174ef27340cSAdrian Chadd 	/* XXX Restart TX completion and pending TX */
2175ef27340cSAdrian Chadd 	if (reset_type == ATH_RESET_NOLOSS) {
2176ef27340cSAdrian Chadd 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
2177ef27340cSAdrian Chadd 			if (ATH_TXQ_SETUP(sc, i)) {
2178ef27340cSAdrian Chadd 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
2179ef27340cSAdrian Chadd 				ath_txq_restart_dma(sc, &sc->sc_txq[i]);
2180ef27340cSAdrian Chadd 				ath_txq_sched(sc, &sc->sc_txq[i]);
2181ef27340cSAdrian Chadd 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
2182ef27340cSAdrian Chadd 			}
2183ef27340cSAdrian Chadd 		}
2184ef27340cSAdrian Chadd 	}
2185ef27340cSAdrian Chadd 
2186ef27340cSAdrian Chadd 	/*
2187ef27340cSAdrian Chadd 	 * This may have been set during an ath_start() call which
2188ef27340cSAdrian Chadd 	 * set this once it detected a concurrent TX was going on.
2189ef27340cSAdrian Chadd 	 * So, clear it.
2190ef27340cSAdrian Chadd 	 */
2191e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
2192ef27340cSAdrian Chadd 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2193e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
2194ef27340cSAdrian Chadd 
2195ef27340cSAdrian Chadd 	/* Handle any frames in the TX queue */
2196ef27340cSAdrian Chadd 	/*
2197ef27340cSAdrian Chadd 	 * XXX should this be done by the caller, rather than
2198ef27340cSAdrian Chadd 	 * ath_reset() ?
2199ef27340cSAdrian Chadd 	 */
2200c42a7b7eSSam Leffler 	ath_start(ifp);			/* restart xmit */
2201c42a7b7eSSam Leffler 	return 0;
22025591b213SSam Leffler }
22035591b213SSam Leffler 
220468e8e04eSSam Leffler static int
2205b032f27cSSam Leffler ath_reset_vap(struct ieee80211vap *vap, u_long cmd)
2206b032f27cSSam Leffler {
22074b54a231SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
22084b54a231SSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
22094b54a231SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
22104b54a231SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
22114b54a231SSam Leffler 
22124b54a231SSam Leffler 	switch (cmd) {
22134b54a231SSam Leffler 	case IEEE80211_IOC_TXPOWER:
22144b54a231SSam Leffler 		/*
22154b54a231SSam Leffler 		 * If per-packet TPC is enabled, then we have nothing
22164b54a231SSam Leffler 		 * to do; otherwise we need to force the global limit.
22174b54a231SSam Leffler 		 * All this can happen directly; no need to reset.
22184b54a231SSam Leffler 		 */
22194b54a231SSam Leffler 		if (!ath_hal_gettpc(ah))
22204b54a231SSam Leffler 			ath_hal_settxpowlimit(ah, ic->ic_txpowlimit);
22214b54a231SSam Leffler 		return 0;
22224b54a231SSam Leffler 	}
2223517526efSAdrian Chadd 	/* XXX? Full or NOLOSS? */
2224517526efSAdrian Chadd 	return ath_reset(ifp, ATH_RESET_FULL);
2225b032f27cSSam Leffler }
2226b032f27cSSam Leffler 
2227b8e788a5SAdrian Chadd struct ath_buf *
222810ad9a77SSam Leffler _ath_getbuf_locked(struct ath_softc *sc)
222910ad9a77SSam Leffler {
223010ad9a77SSam Leffler 	struct ath_buf *bf;
223110ad9a77SSam Leffler 
223210ad9a77SSam Leffler 	ATH_TXBUF_LOCK_ASSERT(sc);
223310ad9a77SSam Leffler 
22346b349e5aSAdrian Chadd 	bf = TAILQ_FIRST(&sc->sc_txbuf);
2235e346b073SAdrian Chadd 	if (bf == NULL) {
2236e346b073SAdrian Chadd 		sc->sc_stats.ast_tx_getnobuf++;
2237e346b073SAdrian Chadd 	} else {
2238e346b073SAdrian Chadd 		if (bf->bf_flags & ATH_BUF_BUSY) {
2239e346b073SAdrian Chadd 			sc->sc_stats.ast_tx_getbusybuf++;
2240e346b073SAdrian Chadd 			bf = NULL;
2241e346b073SAdrian Chadd 		}
2242e346b073SAdrian Chadd 	}
2243e346b073SAdrian Chadd 
224410ad9a77SSam Leffler 	if (bf != NULL && (bf->bf_flags & ATH_BUF_BUSY) == 0)
22456b349e5aSAdrian Chadd 		TAILQ_REMOVE(&sc->sc_txbuf, bf, bf_list);
224610ad9a77SSam Leffler 	else
224710ad9a77SSam Leffler 		bf = NULL;
2248e346b073SAdrian Chadd 
224910ad9a77SSam Leffler 	if (bf == NULL) {
225010ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: %s\n", __func__,
22516b349e5aSAdrian Chadd 		    TAILQ_FIRST(&sc->sc_txbuf) == NULL ?
225210ad9a77SSam Leffler 			"out of xmit buffers" : "xmit buffer busy");
2253e346b073SAdrian Chadd 		return NULL;
225410ad9a77SSam Leffler 	}
2255e346b073SAdrian Chadd 
2256e346b073SAdrian Chadd 	/* Valid bf here; clear some basic fields */
2257e346b073SAdrian Chadd 	bf->bf_next = NULL;	/* XXX just to be sure */
2258e346b073SAdrian Chadd 	bf->bf_last = NULL;	/* XXX again, just to be sure */
2259e346b073SAdrian Chadd 	bf->bf_comp = NULL;	/* XXX again, just to be sure */
2260e346b073SAdrian Chadd 	bzero(&bf->bf_state, sizeof(bf->bf_state));
2261e346b073SAdrian Chadd 
226210ad9a77SSam Leffler 	return bf;
226310ad9a77SSam Leffler }
226410ad9a77SSam Leffler 
2265e346b073SAdrian Chadd /*
2266e346b073SAdrian Chadd  * When retrying a software frame, buffers marked ATH_BUF_BUSY
2267e346b073SAdrian Chadd  * can't be thrown back on the queue as they could still be
2268e346b073SAdrian Chadd  * in use by the hardware.
2269e346b073SAdrian Chadd  *
2270e346b073SAdrian Chadd  * This duplicates the buffer, or returns NULL.
2271e346b073SAdrian Chadd  *
2272e346b073SAdrian Chadd  * The descriptor is also copied but the link pointers and
2273e346b073SAdrian Chadd  * the DMA segments aren't copied; this frame should thus
2274e346b073SAdrian Chadd  * be again passed through the descriptor setup/chain routines
2275e346b073SAdrian Chadd  * so the link is correct.
2276e346b073SAdrian Chadd  *
2277e346b073SAdrian Chadd  * The caller must free the buffer using ath_freebuf().
2278e346b073SAdrian Chadd  *
2279e346b073SAdrian Chadd  * XXX TODO: this call shouldn't fail as it'll cause packet loss
2280e346b073SAdrian Chadd  * XXX in the TX pathway when retries are needed.
2281e346b073SAdrian Chadd  * XXX Figure out how to keep some buffers free, or factor the
2282e346b073SAdrian Chadd  * XXX number of busy buffers into the xmit path (ath_start())
2283e346b073SAdrian Chadd  * XXX so we don't over-commit.
2284e346b073SAdrian Chadd  */
2285e346b073SAdrian Chadd struct ath_buf *
2286e346b073SAdrian Chadd ath_buf_clone(struct ath_softc *sc, const struct ath_buf *bf)
2287e346b073SAdrian Chadd {
2288e346b073SAdrian Chadd 	struct ath_buf *tbf;
2289e346b073SAdrian Chadd 
2290e346b073SAdrian Chadd 	tbf = ath_getbuf(sc);
2291e346b073SAdrian Chadd 	if (tbf == NULL)
2292e346b073SAdrian Chadd 		return NULL;	/* XXX failure? Why? */
2293e346b073SAdrian Chadd 
2294e346b073SAdrian Chadd 	/* Copy basics */
2295e346b073SAdrian Chadd 	tbf->bf_next = NULL;
2296e346b073SAdrian Chadd 	tbf->bf_nseg = bf->bf_nseg;
2297e346b073SAdrian Chadd 	tbf->bf_flags = bf->bf_flags & ~ATH_BUF_BUSY;
2298e346b073SAdrian Chadd 	tbf->bf_status = bf->bf_status;
2299e346b073SAdrian Chadd 	tbf->bf_m = bf->bf_m;
2300e346b073SAdrian Chadd 	tbf->bf_node = bf->bf_node;
2301e346b073SAdrian Chadd 	/* will be setup by the chain/setup function */
2302e346b073SAdrian Chadd 	tbf->bf_lastds = NULL;
2303e346b073SAdrian Chadd 	/* for now, last == self */
2304e346b073SAdrian Chadd 	tbf->bf_last = tbf;
2305e346b073SAdrian Chadd 	tbf->bf_comp = bf->bf_comp;
2306e346b073SAdrian Chadd 
2307e346b073SAdrian Chadd 	/* NOTE: DMA segments will be setup by the setup/chain functions */
2308e346b073SAdrian Chadd 
2309e346b073SAdrian Chadd 	/* The caller has to re-init the descriptor + links */
2310e346b073SAdrian Chadd 
2311e346b073SAdrian Chadd 	/* Copy state */
2312e346b073SAdrian Chadd 	memcpy(&tbf->bf_state, &bf->bf_state, sizeof(bf->bf_state));
2313e346b073SAdrian Chadd 
2314e346b073SAdrian Chadd 	return tbf;
2315e346b073SAdrian Chadd }
2316e346b073SAdrian Chadd 
2317b8e788a5SAdrian Chadd struct ath_buf *
231810ad9a77SSam Leffler ath_getbuf(struct ath_softc *sc)
231910ad9a77SSam Leffler {
232010ad9a77SSam Leffler 	struct ath_buf *bf;
232110ad9a77SSam Leffler 
232210ad9a77SSam Leffler 	ATH_TXBUF_LOCK(sc);
232310ad9a77SSam Leffler 	bf = _ath_getbuf_locked(sc);
2324e4e7938aSAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
232510ad9a77SSam Leffler 	if (bf == NULL) {
232610ad9a77SSam Leffler 		struct ifnet *ifp = sc->sc_ifp;
232710ad9a77SSam Leffler 
232810ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: stop queue\n", __func__);
232910ad9a77SSam Leffler 		sc->sc_stats.ast_tx_qstop++;
2330e4e7938aSAdrian Chadd 		IF_LOCK(&ifp->if_snd);
233110ad9a77SSam Leffler 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2332e4e7938aSAdrian Chadd 		IF_UNLOCK(&ifp->if_snd);
233310ad9a77SSam Leffler 	}
233410ad9a77SSam Leffler 	return bf;
233510ad9a77SSam Leffler }
233610ad9a77SSam Leffler 
23375591b213SSam Leffler static void
23385591b213SSam Leffler ath_start(struct ifnet *ifp)
23395591b213SSam Leffler {
23405591b213SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
23415591b213SSam Leffler 	struct ieee80211_node *ni;
23425591b213SSam Leffler 	struct ath_buf *bf;
234368e8e04eSSam Leffler 	struct mbuf *m, *next;
234468e8e04eSSam Leffler 	ath_bufhead frags;
23455591b213SSam Leffler 
234613f4c340SRobert Watson 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->sc_invalid)
23475591b213SSam Leffler 		return;
2348ef27340cSAdrian Chadd 
2349ef27340cSAdrian Chadd 	/* XXX is it ok to hold the ATH_LOCK here? */
2350ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2351ef27340cSAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
2352ef27340cSAdrian Chadd 		device_printf(sc->sc_dev,
2353ef27340cSAdrian Chadd 		    "%s: sc_inreset_cnt > 0; bailing\n", __func__);
2354ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2355e4e7938aSAdrian Chadd 		IF_LOCK(&ifp->if_snd);
2356e4e7938aSAdrian Chadd 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2357e4e7938aSAdrian Chadd 		IF_UNLOCK(&ifp->if_snd);
2358ef27340cSAdrian Chadd 		return;
2359ef27340cSAdrian Chadd 	}
2360ef27340cSAdrian Chadd 	sc->sc_txstart_cnt++;
2361ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2362ef27340cSAdrian Chadd 
23635591b213SSam Leffler 	for (;;) {
23645591b213SSam Leffler 		/*
23655591b213SSam Leffler 		 * Grab a TX buffer and associated resources.
23665591b213SSam Leffler 		 */
236710ad9a77SSam Leffler 		bf = ath_getbuf(sc);
236810ad9a77SSam Leffler 		if (bf == NULL)
23695591b213SSam Leffler 			break;
23702b9411e2SSam Leffler 
2371b032f27cSSam Leffler 		IFQ_DEQUEUE(&ifp->if_snd, m);
2372b032f27cSSam Leffler 		if (m == NULL) {
2373b032f27cSSam Leffler 			ATH_TXBUF_LOCK(sc);
23746b349e5aSAdrian Chadd 			TAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
2375b032f27cSSam Leffler 			ATH_TXBUF_UNLOCK(sc);
2376b032f27cSSam Leffler 			break;
2377b032f27cSSam Leffler 		}
2378b032f27cSSam Leffler 		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
237968e8e04eSSam Leffler 		/*
238068e8e04eSSam Leffler 		 * Check for fragmentation.  If this frame
238168e8e04eSSam Leffler 		 * has been broken up verify we have enough
238268e8e04eSSam Leffler 		 * buffers to send all the fragments so all
238368e8e04eSSam Leffler 		 * go out or none...
238468e8e04eSSam Leffler 		 */
23856b349e5aSAdrian Chadd 		TAILQ_INIT(&frags);
238668e8e04eSSam Leffler 		if ((m->m_flags & M_FRAG) &&
238768e8e04eSSam Leffler 		    !ath_txfrag_setup(sc, &frags, m, ni)) {
238868e8e04eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_XMIT,
238968e8e04eSSam Leffler 			    "%s: out of txfrag buffers\n", __func__);
239036c6be9aSSam Leffler 			sc->sc_stats.ast_tx_nofrag++;
23919cb93076SSam Leffler 			ifp->if_oerrors++;
239268e8e04eSSam Leffler 			ath_freetx(m);
239368e8e04eSSam Leffler 			goto bad;
239468e8e04eSSam Leffler 		}
2395339ccfb3SSam Leffler 		ifp->if_opackets++;
239668e8e04eSSam Leffler 	nextfrag:
239768e8e04eSSam Leffler 		/*
239868e8e04eSSam Leffler 		 * Pass the frame to the h/w for transmission.
239968e8e04eSSam Leffler 		 * Fragmented frames have each frag chained together
240068e8e04eSSam Leffler 		 * with m_nextpkt.  We know there are sufficient ath_buf's
240168e8e04eSSam Leffler 		 * to send all the frags because of work done by
240268e8e04eSSam Leffler 		 * ath_txfrag_setup.  We leave m_nextpkt set while
240368e8e04eSSam Leffler 		 * calling ath_tx_start so it can use it to extend the
240468e8e04eSSam Leffler 		 * the tx duration to cover the subsequent frag and
240568e8e04eSSam Leffler 		 * so it can reclaim all the mbufs in case of an error;
240668e8e04eSSam Leffler 		 * ath_tx_start clears m_nextpkt once it commits to
240768e8e04eSSam Leffler 		 * handing the frame to the hardware.
240868e8e04eSSam Leffler 		 */
240968e8e04eSSam Leffler 		next = m->m_nextpkt;
24105591b213SSam Leffler 		if (ath_tx_start(sc, ni, bf, m)) {
24115591b213SSam Leffler 	bad:
24125591b213SSam Leffler 			ifp->if_oerrors++;
2413c42a7b7eSSam Leffler 	reclaim:
241468e8e04eSSam Leffler 			bf->bf_m = NULL;
241568e8e04eSSam Leffler 			bf->bf_node = NULL;
2416c42a7b7eSSam Leffler 			ATH_TXBUF_LOCK(sc);
24176b349e5aSAdrian Chadd 			TAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
241868e8e04eSSam Leffler 			ath_txfrag_cleanup(sc, &frags, ni);
2419c42a7b7eSSam Leffler 			ATH_TXBUF_UNLOCK(sc);
2420c42a7b7eSSam Leffler 			if (ni != NULL)
2421c42a7b7eSSam Leffler 				ieee80211_free_node(ni);
24225591b213SSam Leffler 			continue;
24235591b213SSam Leffler 		}
242468e8e04eSSam Leffler 		if (next != NULL) {
242568e8e04eSSam Leffler 			/*
242668e8e04eSSam Leffler 			 * Beware of state changing between frags.
242768e8e04eSSam Leffler 			 * XXX check sta power-save state?
242868e8e04eSSam Leffler 			 */
2429b032f27cSSam Leffler 			if (ni->ni_vap->iv_state != IEEE80211_S_RUN) {
243068e8e04eSSam Leffler 				DPRINTF(sc, ATH_DEBUG_XMIT,
243168e8e04eSSam Leffler 				    "%s: flush fragmented packet, state %s\n",
243268e8e04eSSam Leffler 				    __func__,
2433b032f27cSSam Leffler 				    ieee80211_state_name[ni->ni_vap->iv_state]);
243468e8e04eSSam Leffler 				ath_freetx(next);
243568e8e04eSSam Leffler 				goto reclaim;
243668e8e04eSSam Leffler 			}
243768e8e04eSSam Leffler 			m = next;
24386b349e5aSAdrian Chadd 			bf = TAILQ_FIRST(&frags);
243968e8e04eSSam Leffler 			KASSERT(bf != NULL, ("no buf for txfrag"));
24406b349e5aSAdrian Chadd 			TAILQ_REMOVE(&frags, bf, bf_list);
244168e8e04eSSam Leffler 			goto nextfrag;
244268e8e04eSSam Leffler 		}
24435591b213SSam Leffler 
24442e986da5SSam Leffler 		sc->sc_wd_timer = 5;
24455591b213SSam Leffler 	}
2446ef27340cSAdrian Chadd 
2447ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2448ef27340cSAdrian Chadd 	sc->sc_txstart_cnt--;
2449ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
24505591b213SSam Leffler }
24515591b213SSam Leffler 
24525591b213SSam Leffler static int
24535591b213SSam Leffler ath_media_change(struct ifnet *ifp)
24545591b213SSam Leffler {
2455b032f27cSSam Leffler 	int error = ieee80211_media_change(ifp);
2456b032f27cSSam Leffler 	/* NB: only the fixed rate can change and that doesn't need a reset */
2457b032f27cSSam Leffler 	return (error == ENETRESET ? 0 : error);
24585591b213SSam Leffler }
24595591b213SSam Leffler 
2460c42a7b7eSSam Leffler /*
2461c42a7b7eSSam Leffler  * Block/unblock tx+rx processing while a key change is done.
2462c42a7b7eSSam Leffler  * We assume the caller serializes key management operations
2463c42a7b7eSSam Leffler  * so we only need to worry about synchronization with other
2464c42a7b7eSSam Leffler  * uses that originate in the driver.
2465c42a7b7eSSam Leffler  */
2466c42a7b7eSSam Leffler static void
2467b032f27cSSam Leffler ath_key_update_begin(struct ieee80211vap *vap)
2468c42a7b7eSSam Leffler {
2469b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ic->ic_ifp;
2470c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2471c42a7b7eSSam Leffler 
2472c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
2473b032f27cSSam Leffler 	taskqueue_block(sc->sc_tq);
2474c42a7b7eSSam Leffler 	IF_LOCK(&ifp->if_snd);		/* NB: doesn't block mgmt frames */
2475c42a7b7eSSam Leffler }
2476c42a7b7eSSam Leffler 
2477c42a7b7eSSam Leffler static void
2478b032f27cSSam Leffler ath_key_update_end(struct ieee80211vap *vap)
2479c42a7b7eSSam Leffler {
2480b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ic->ic_ifp;
2481c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2482c42a7b7eSSam Leffler 
2483c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
2484c42a7b7eSSam Leffler 	IF_UNLOCK(&ifp->if_snd);
2485b032f27cSSam Leffler 	taskqueue_unblock(sc->sc_tq);
2486c42a7b7eSSam Leffler }
24875591b213SSam Leffler 
24884bc0e754SSam Leffler /*
24894bc0e754SSam Leffler  * Calculate the receive filter according to the
24904bc0e754SSam Leffler  * operating mode and state:
24914bc0e754SSam Leffler  *
24924bc0e754SSam Leffler  * o always accept unicast, broadcast, and multicast traffic
2493b032f27cSSam Leffler  * o accept PHY error frames when hardware doesn't have MIB support
2494411373ebSSam Leffler  *   to count and we need them for ANI (sta mode only until recently)
2495b032f27cSSam Leffler  *   and we are not scanning (ANI is disabled)
2496411373ebSSam Leffler  *   NB: older hal's add rx filter bits out of sight and we need to
2497411373ebSSam Leffler  *	 blindly preserve them
24984bc0e754SSam Leffler  * o probe request frames are accepted only when operating in
249959aa14a9SRui Paulo  *   hostap, adhoc, mesh, or monitor modes
2500b032f27cSSam Leffler  * o enable promiscuous mode
2501b032f27cSSam Leffler  *   - when in monitor mode
2502b032f27cSSam Leffler  *   - if interface marked PROMISC (assumes bridge setting is filtered)
25034bc0e754SSam Leffler  * o accept beacons:
25044bc0e754SSam Leffler  *   - when operating in station mode for collecting rssi data when
25054bc0e754SSam Leffler  *     the station is otherwise quiet, or
2506b032f27cSSam Leffler  *   - when operating in adhoc mode so the 802.11 layer creates
2507b032f27cSSam Leffler  *     node table entries for peers,
25084bc0e754SSam Leffler  *   - when scanning
2509b032f27cSSam Leffler  *   - when doing s/w beacon miss (e.g. for ap+sta)
2510b032f27cSSam Leffler  *   - when operating in ap mode in 11g to detect overlapping bss that
2511b032f27cSSam Leffler  *     require protection
251259aa14a9SRui Paulo  *   - when operating in mesh mode to detect neighbors
25136f48c956SSam Leffler  * o accept control frames:
25146f48c956SSam Leffler  *   - when in monitor mode
2515b032f27cSSam Leffler  * XXX HT protection for 11n
25164bc0e754SSam Leffler  */
25174bc0e754SSam Leffler static u_int32_t
251868e8e04eSSam Leffler ath_calcrxfilter(struct ath_softc *sc)
25194bc0e754SSam Leffler {
2520fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
2521b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
25224bc0e754SSam Leffler 	u_int32_t rfilt;
25234bc0e754SSam Leffler 
2524b032f27cSSam Leffler 	rfilt = HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST;
2525411373ebSSam Leffler 	if (!sc->sc_needmib && !sc->sc_scanning)
2526411373ebSSam Leffler 		rfilt |= HAL_RX_FILTER_PHYERR;
25274bc0e754SSam Leffler 	if (ic->ic_opmode != IEEE80211_M_STA)
25284bc0e754SSam Leffler 		rfilt |= HAL_RX_FILTER_PROBEREQ;
25295463c4a4SSam Leffler 	/* XXX ic->ic_monvaps != 0? */
2530b032f27cSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_MONITOR || (ifp->if_flags & IFF_PROMISC))
25314bc0e754SSam Leffler 		rfilt |= HAL_RX_FILTER_PROM;
25324bc0e754SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA ||
253347db982fSSam Leffler 	    ic->ic_opmode == IEEE80211_M_IBSS ||
2534b032f27cSSam Leffler 	    sc->sc_swbmiss || sc->sc_scanning)
2535b032f27cSSam Leffler 		rfilt |= HAL_RX_FILTER_BEACON;
2536b032f27cSSam Leffler 	/*
2537b032f27cSSam Leffler 	 * NB: We don't recalculate the rx filter when
2538b032f27cSSam Leffler 	 * ic_protmode changes; otherwise we could do
2539b032f27cSSam Leffler 	 * this only when ic_protmode != NONE.
2540b032f27cSSam Leffler 	 */
2541b032f27cSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
2542b032f27cSSam Leffler 	    IEEE80211_IS_CHAN_ANYG(ic->ic_curchan))
25434bc0e754SSam Leffler 		rfilt |= HAL_RX_FILTER_BEACON;
2544f378d4c8SAdrian Chadd 
2545f378d4c8SAdrian Chadd 	/*
25464aa18e9dSAdrian Chadd 	 * Enable hardware PS-POLL RX only for hostap mode;
2547f378d4c8SAdrian Chadd 	 * STA mode sends PS-POLL frames but never
25484aa18e9dSAdrian Chadd 	 * receives them.
2549f378d4c8SAdrian Chadd 	 */
2550dce0bccaSAdrian Chadd 	if (ath_hal_getcapability(sc->sc_ah, HAL_CAP_PSPOLL,
2551f378d4c8SAdrian Chadd 	    0, NULL) == HAL_OK &&
2552f378d4c8SAdrian Chadd 	    ic->ic_opmode == IEEE80211_M_HOSTAP)
2553f378d4c8SAdrian Chadd 		rfilt |= HAL_RX_FILTER_PSPOLL;
2554f378d4c8SAdrian Chadd 
2555fe0dd789SSam Leffler 	if (sc->sc_nmeshvaps) {
255659aa14a9SRui Paulo 		rfilt |= HAL_RX_FILTER_BEACON;
255759aa14a9SRui Paulo 		if (sc->sc_hasbmatch)
255859aa14a9SRui Paulo 			rfilt |= HAL_RX_FILTER_BSSID;
255959aa14a9SRui Paulo 		else
256059aa14a9SRui Paulo 			rfilt |= HAL_RX_FILTER_PROM;
256159aa14a9SRui Paulo 	}
25626f48c956SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
25636f48c956SSam Leffler 		rfilt |= HAL_RX_FILTER_CONTROL;
2564f378d4c8SAdrian Chadd 
2565f378d4c8SAdrian Chadd 	/*
2566f378d4c8SAdrian Chadd 	 * Enable RX of compressed BAR frames only when doing
2567f378d4c8SAdrian Chadd 	 * 802.11n. Required for A-MPDU.
2568f378d4c8SAdrian Chadd 	 */
2569a83df4d3SAdrian Chadd 	if (IEEE80211_IS_CHAN_HT(ic->ic_curchan))
2570a83df4d3SAdrian Chadd 		rfilt |= HAL_RX_FILTER_COMPBAR;
2571f378d4c8SAdrian Chadd 
2572fad901ebSAdrian Chadd 	/*
2573fad901ebSAdrian Chadd 	 * Enable radar PHY errors if requested by the
2574fad901ebSAdrian Chadd 	 * DFS module.
2575fad901ebSAdrian Chadd 	 */
2576fad901ebSAdrian Chadd 	if (sc->sc_dodfs)
2577fad901ebSAdrian Chadd 		rfilt |= HAL_RX_FILTER_PHYRADAR;
2578fad901ebSAdrian Chadd 
2579b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x, %s if_flags 0x%x\n",
2580b032f27cSSam Leffler 	    __func__, rfilt, ieee80211_opmode_name[ic->ic_opmode], ifp->if_flags);
25814bc0e754SSam Leffler 	return rfilt;
2582b032f27cSSam Leffler }
2583b032f27cSSam Leffler 
2584b032f27cSSam Leffler static void
2585b032f27cSSam Leffler ath_update_promisc(struct ifnet *ifp)
2586b032f27cSSam Leffler {
2587b032f27cSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2588b032f27cSSam Leffler 	u_int32_t rfilt;
2589b032f27cSSam Leffler 
2590b032f27cSSam Leffler 	/* configure rx filter */
2591b032f27cSSam Leffler 	rfilt = ath_calcrxfilter(sc);
2592b032f27cSSam Leffler 	ath_hal_setrxfilter(sc->sc_ah, rfilt);
2593b032f27cSSam Leffler 
2594b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x\n", __func__, rfilt);
2595b032f27cSSam Leffler }
2596b032f27cSSam Leffler 
2597b032f27cSSam Leffler static void
2598b032f27cSSam Leffler ath_update_mcast(struct ifnet *ifp)
2599b032f27cSSam Leffler {
2600b032f27cSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2601b032f27cSSam Leffler 	u_int32_t mfilt[2];
2602b032f27cSSam Leffler 
2603b032f27cSSam Leffler 	/* calculate and install multicast filter */
2604b032f27cSSam Leffler 	if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
2605b032f27cSSam Leffler 		struct ifmultiaddr *ifma;
2606b032f27cSSam Leffler 		/*
2607b032f27cSSam Leffler 		 * Merge multicast addresses to form the hardware filter.
2608b032f27cSSam Leffler 		 */
2609b032f27cSSam Leffler 		mfilt[0] = mfilt[1] = 0;
2610eb956cd0SRobert Watson 		if_maddr_rlock(ifp);	/* XXX need some fiddling to remove? */
2611b032f27cSSam Leffler 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2612b032f27cSSam Leffler 			caddr_t dl;
2613b032f27cSSam Leffler 			u_int32_t val;
2614b032f27cSSam Leffler 			u_int8_t pos;
2615b032f27cSSam Leffler 
2616b032f27cSSam Leffler 			/* calculate XOR of eight 6bit values */
2617b032f27cSSam Leffler 			dl = LLADDR((struct sockaddr_dl *) ifma->ifma_addr);
2618b032f27cSSam Leffler 			val = LE_READ_4(dl + 0);
2619b032f27cSSam Leffler 			pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
2620b032f27cSSam Leffler 			val = LE_READ_4(dl + 3);
2621b032f27cSSam Leffler 			pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
2622b032f27cSSam Leffler 			pos &= 0x3f;
2623b032f27cSSam Leffler 			mfilt[pos / 32] |= (1 << (pos % 32));
2624b032f27cSSam Leffler 		}
2625eb956cd0SRobert Watson 		if_maddr_runlock(ifp);
2626b032f27cSSam Leffler 	} else
2627b032f27cSSam Leffler 		mfilt[0] = mfilt[1] = ~0;
2628b032f27cSSam Leffler 	ath_hal_setmcastfilter(sc->sc_ah, mfilt[0], mfilt[1]);
2629b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: MC filter %08x:%08x\n",
2630b032f27cSSam Leffler 		__func__, mfilt[0], mfilt[1]);
26314bc0e754SSam Leffler }
26324bc0e754SSam Leffler 
26335591b213SSam Leffler static void
26345591b213SSam Leffler ath_mode_init(struct ath_softc *sc)
26355591b213SSam Leffler {
2636fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
2637b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2638b032f27cSSam Leffler 	u_int32_t rfilt;
26395591b213SSam Leffler 
26404bc0e754SSam Leffler 	/* configure rx filter */
264168e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
26424bc0e754SSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
26434bc0e754SSam Leffler 
26445591b213SSam Leffler 	/* configure operational mode */
2645c42a7b7eSSam Leffler 	ath_hal_setopmode(ah);
2646c42a7b7eSSam Leffler 
264729aca940SSam Leffler 	/* handle any link-level address change */
264829aca940SSam Leffler 	ath_hal_setmac(ah, IF_LLADDR(ifp));
26495591b213SSam Leffler 
26505591b213SSam Leffler 	/* calculate and install multicast filter */
2651b032f27cSSam Leffler 	ath_update_mcast(ifp);
26525591b213SSam Leffler }
26535591b213SSam Leffler 
2654c42a7b7eSSam Leffler /*
2655c42a7b7eSSam Leffler  * Set the slot time based on the current setting.
2656c42a7b7eSSam Leffler  */
2657c42a7b7eSSam Leffler static void
2658c42a7b7eSSam Leffler ath_setslottime(struct ath_softc *sc)
2659c42a7b7eSSam Leffler {
2660b032f27cSSam Leffler 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2661c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2662aaa70f2fSSam Leffler 	u_int usec;
2663c42a7b7eSSam Leffler 
2664aaa70f2fSSam Leffler 	if (IEEE80211_IS_CHAN_HALF(ic->ic_curchan))
2665aaa70f2fSSam Leffler 		usec = 13;
2666aaa70f2fSSam Leffler 	else if (IEEE80211_IS_CHAN_QUARTER(ic->ic_curchan))
2667aaa70f2fSSam Leffler 		usec = 21;
2668724c193aSSam Leffler 	else if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan)) {
2669724c193aSSam Leffler 		/* honor short/long slot time only in 11g */
2670724c193aSSam Leffler 		/* XXX shouldn't honor on pure g or turbo g channel */
2671724c193aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
2672aaa70f2fSSam Leffler 			usec = HAL_SLOT_TIME_9;
2673aaa70f2fSSam Leffler 		else
2674aaa70f2fSSam Leffler 			usec = HAL_SLOT_TIME_20;
2675724c193aSSam Leffler 	} else
2676724c193aSSam Leffler 		usec = HAL_SLOT_TIME_9;
2677aaa70f2fSSam Leffler 
2678aaa70f2fSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET,
2679aaa70f2fSSam Leffler 	    "%s: chan %u MHz flags 0x%x %s slot, %u usec\n",
2680aaa70f2fSSam Leffler 	    __func__, ic->ic_curchan->ic_freq, ic->ic_curchan->ic_flags,
2681aaa70f2fSSam Leffler 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", usec);
2682aaa70f2fSSam Leffler 
2683aaa70f2fSSam Leffler 	ath_hal_setslottime(ah, usec);
2684c42a7b7eSSam Leffler 	sc->sc_updateslot = OK;
2685c42a7b7eSSam Leffler }
2686c42a7b7eSSam Leffler 
2687c42a7b7eSSam Leffler /*
2688c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update the
2689c42a7b7eSSam Leffler  * slot time based on the current setting.
2690c42a7b7eSSam Leffler  */
2691c42a7b7eSSam Leffler static void
2692c42a7b7eSSam Leffler ath_updateslot(struct ifnet *ifp)
2693c42a7b7eSSam Leffler {
2694c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2695b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
2696c42a7b7eSSam Leffler 
2697c42a7b7eSSam Leffler 	/*
2698c42a7b7eSSam Leffler 	 * When not coordinating the BSS, change the hardware
2699c42a7b7eSSam Leffler 	 * immediately.  For other operation we defer the change
2700c42a7b7eSSam Leffler 	 * until beacon updates have propagated to the stations.
2701c42a7b7eSSam Leffler 	 */
270259aa14a9SRui Paulo 	if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
270359aa14a9SRui Paulo 	    ic->ic_opmode == IEEE80211_M_MBSS)
2704c42a7b7eSSam Leffler 		sc->sc_updateslot = UPDATE;
2705c42a7b7eSSam Leffler 	else
2706c42a7b7eSSam Leffler 		ath_setslottime(sc);
2707c42a7b7eSSam Leffler }
2708c42a7b7eSSam Leffler 
2709c42a7b7eSSam Leffler /*
271080d2765fSSam Leffler  * Setup a h/w transmit queue for beacons.
271180d2765fSSam Leffler  */
271280d2765fSSam Leffler static int
271380d2765fSSam Leffler ath_beaconq_setup(struct ath_hal *ah)
271480d2765fSSam Leffler {
271580d2765fSSam Leffler 	HAL_TXQ_INFO qi;
271680d2765fSSam Leffler 
271780d2765fSSam Leffler 	memset(&qi, 0, sizeof(qi));
271880d2765fSSam Leffler 	qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
271980d2765fSSam Leffler 	qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
272080d2765fSSam Leffler 	qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
27210f2e86fbSSam Leffler 	/* NB: for dynamic turbo, don't enable any other interrupts */
2722bd5a9920SSam Leffler 	qi.tqi_qflags = HAL_TXQ_TXDESCINT_ENABLE;
272380d2765fSSam Leffler 	return ath_hal_setuptxqueue(ah, HAL_TX_QUEUE_BEACON, &qi);
272480d2765fSSam Leffler }
272580d2765fSSam Leffler 
272680d2765fSSam Leffler /*
27270f2e86fbSSam Leffler  * Setup the transmit queue parameters for the beacon queue.
27280f2e86fbSSam Leffler  */
27290f2e86fbSSam Leffler static int
27300f2e86fbSSam Leffler ath_beaconq_config(struct ath_softc *sc)
27310f2e86fbSSam Leffler {
27320f2e86fbSSam Leffler #define	ATH_EXPONENT_TO_VALUE(v)	((1<<(v))-1)
2733b032f27cSSam Leffler 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
27340f2e86fbSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
27350f2e86fbSSam Leffler 	HAL_TXQ_INFO qi;
27360f2e86fbSSam Leffler 
27370f2e86fbSSam Leffler 	ath_hal_gettxqueueprops(ah, sc->sc_bhalq, &qi);
273859aa14a9SRui Paulo 	if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
273959aa14a9SRui Paulo 	    ic->ic_opmode == IEEE80211_M_MBSS) {
27400f2e86fbSSam Leffler 		/*
27410f2e86fbSSam Leffler 		 * Always burst out beacon and CAB traffic.
27420f2e86fbSSam Leffler 		 */
27430f2e86fbSSam Leffler 		qi.tqi_aifs = ATH_BEACON_AIFS_DEFAULT;
27440f2e86fbSSam Leffler 		qi.tqi_cwmin = ATH_BEACON_CWMIN_DEFAULT;
27450f2e86fbSSam Leffler 		qi.tqi_cwmax = ATH_BEACON_CWMAX_DEFAULT;
27460f2e86fbSSam Leffler 	} else {
27470f2e86fbSSam Leffler 		struct wmeParams *wmep =
27480f2e86fbSSam Leffler 			&ic->ic_wme.wme_chanParams.cap_wmeParams[WME_AC_BE];
27490f2e86fbSSam Leffler 		/*
27500f2e86fbSSam Leffler 		 * Adhoc mode; important thing is to use 2x cwmin.
27510f2e86fbSSam Leffler 		 */
27520f2e86fbSSam Leffler 		qi.tqi_aifs = wmep->wmep_aifsn;
27530f2e86fbSSam Leffler 		qi.tqi_cwmin = 2*ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
27540f2e86fbSSam Leffler 		qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
27550f2e86fbSSam Leffler 	}
27560f2e86fbSSam Leffler 
27570f2e86fbSSam Leffler 	if (!ath_hal_settxqueueprops(ah, sc->sc_bhalq, &qi)) {
27580f2e86fbSSam Leffler 		device_printf(sc->sc_dev, "unable to update parameters for "
27590f2e86fbSSam Leffler 			"beacon hardware queue!\n");
27600f2e86fbSSam Leffler 		return 0;
27610f2e86fbSSam Leffler 	} else {
27620f2e86fbSSam Leffler 		ath_hal_resettxqueue(ah, sc->sc_bhalq); /* push to h/w */
27630f2e86fbSSam Leffler 		return 1;
27640f2e86fbSSam Leffler 	}
27650f2e86fbSSam Leffler #undef ATH_EXPONENT_TO_VALUE
27660f2e86fbSSam Leffler }
27670f2e86fbSSam Leffler 
27680f2e86fbSSam Leffler /*
2769c42a7b7eSSam Leffler  * Allocate and setup an initial beacon frame.
2770c42a7b7eSSam Leffler  */
27715591b213SSam Leffler static int
27725591b213SSam Leffler ath_beacon_alloc(struct ath_softc *sc, struct ieee80211_node *ni)
27735591b213SSam Leffler {
2774b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
2775b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
27765591b213SSam Leffler 	struct ath_buf *bf;
27775591b213SSam Leffler 	struct mbuf *m;
2778c42a7b7eSSam Leffler 	int error;
27795591b213SSam Leffler 
2780b032f27cSSam Leffler 	bf = avp->av_bcbuf;
27817ebd03d7SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: bf_m=%p, bf_node=%p\n",
27827ebd03d7SAdrian Chadd 	    __func__, bf->bf_m, bf->bf_node);
2783b032f27cSSam Leffler 	if (bf->bf_m != NULL) {
2784b032f27cSSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
2785b032f27cSSam Leffler 		m_freem(bf->bf_m);
2786b032f27cSSam Leffler 		bf->bf_m = NULL;
2787c42a7b7eSSam Leffler 	}
2788b032f27cSSam Leffler 	if (bf->bf_node != NULL) {
2789b032f27cSSam Leffler 		ieee80211_free_node(bf->bf_node);
2790b032f27cSSam Leffler 		bf->bf_node = NULL;
2791b032f27cSSam Leffler 	}
2792b032f27cSSam Leffler 
27935591b213SSam Leffler 	/*
27945591b213SSam Leffler 	 * NB: the beacon data buffer must be 32-bit aligned;
27955591b213SSam Leffler 	 * we assume the mbuf routines will return us something
27965591b213SSam Leffler 	 * with this alignment (perhaps should assert).
27975591b213SSam Leffler 	 */
2798b032f27cSSam Leffler 	m = ieee80211_beacon_alloc(ni, &avp->av_boff);
27995591b213SSam Leffler 	if (m == NULL) {
2800b032f27cSSam Leffler 		device_printf(sc->sc_dev, "%s: cannot get mbuf\n", __func__);
28015591b213SSam Leffler 		sc->sc_stats.ast_be_nombuf++;
28025591b213SSam Leffler 		return ENOMEM;
28035591b213SSam Leffler 	}
2804f9e6219bSSam Leffler 	error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m,
2805f9e6219bSSam Leffler 				     bf->bf_segs, &bf->bf_nseg,
28065591b213SSam Leffler 				     BUS_DMA_NOWAIT);
2807b032f27cSSam Leffler 	if (error != 0) {
2808b032f27cSSam Leffler 		device_printf(sc->sc_dev,
2809b032f27cSSam Leffler 		    "%s: cannot map mbuf, bus_dmamap_load_mbuf_sg returns %d\n",
2810b032f27cSSam Leffler 		    __func__, error);
2811b032f27cSSam Leffler 		m_freem(m);
2812b032f27cSSam Leffler 		return error;
2813b032f27cSSam Leffler 	}
2814b032f27cSSam Leffler 
2815b032f27cSSam Leffler 	/*
2816b032f27cSSam Leffler 	 * Calculate a TSF adjustment factor required for staggered
2817b032f27cSSam Leffler 	 * beacons.  Note that we assume the format of the beacon
2818b032f27cSSam Leffler 	 * frame leaves the tstamp field immediately following the
2819b032f27cSSam Leffler 	 * header.
2820b032f27cSSam Leffler 	 */
2821b032f27cSSam Leffler 	if (sc->sc_stagbeacons && avp->av_bslot > 0) {
2822b032f27cSSam Leffler 		uint64_t tsfadjust;
2823b032f27cSSam Leffler 		struct ieee80211_frame *wh;
2824b032f27cSSam Leffler 
2825b032f27cSSam Leffler 		/*
2826b032f27cSSam Leffler 		 * The beacon interval is in TU's; the TSF is in usecs.
2827b032f27cSSam Leffler 		 * We figure out how many TU's to add to align the timestamp
2828b032f27cSSam Leffler 		 * then convert to TSF units and handle byte swapping before
2829b032f27cSSam Leffler 		 * inserting it in the frame.  The hardware will then add this
2830b032f27cSSam Leffler 		 * each time a beacon frame is sent.  Note that we align vap's
2831b032f27cSSam Leffler 		 * 1..N and leave vap 0 untouched.  This means vap 0 has a
2832b032f27cSSam Leffler 		 * timestamp in one beacon interval while the others get a
2833b032f27cSSam Leffler 		 * timstamp aligned to the next interval.
2834b032f27cSSam Leffler 		 */
2835b032f27cSSam Leffler 		tsfadjust = ni->ni_intval *
2836b032f27cSSam Leffler 		    (ATH_BCBUF - avp->av_bslot) / ATH_BCBUF;
2837b032f27cSSam Leffler 		tsfadjust = htole64(tsfadjust << 10);	/* TU -> TSF */
2838b032f27cSSam Leffler 
2839b032f27cSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
2840b032f27cSSam Leffler 		    "%s: %s beacons bslot %d intval %u tsfadjust %llu\n",
2841b032f27cSSam Leffler 		    __func__, sc->sc_stagbeacons ? "stagger" : "burst",
28423627e321SSam Leffler 		    avp->av_bslot, ni->ni_intval,
28433627e321SSam Leffler 		    (long long unsigned) le64toh(tsfadjust));
2844b032f27cSSam Leffler 
2845b032f27cSSam Leffler 		wh = mtod(m, struct ieee80211_frame *);
2846b032f27cSSam Leffler 		memcpy(&wh[1], &tsfadjust, sizeof(tsfadjust));
2847b032f27cSSam Leffler 	}
2848c42a7b7eSSam Leffler 	bf->bf_m = m;
2849f818612bSSam Leffler 	bf->bf_node = ieee80211_ref_node(ni);
2850b032f27cSSam Leffler 
2851b032f27cSSam Leffler 	return 0;
28525591b213SSam Leffler }
2853c42a7b7eSSam Leffler 
2854c42a7b7eSSam Leffler /*
2855c42a7b7eSSam Leffler  * Setup the beacon frame for transmit.
2856c42a7b7eSSam Leffler  */
2857c42a7b7eSSam Leffler static void
2858c42a7b7eSSam Leffler ath_beacon_setup(struct ath_softc *sc, struct ath_buf *bf)
2859c42a7b7eSSam Leffler {
2860c42a7b7eSSam Leffler #define	USE_SHPREAMBLE(_ic) \
2861c42a7b7eSSam Leffler 	(((_ic)->ic_flags & (IEEE80211_F_SHPREAMBLE | IEEE80211_F_USEBARKER))\
2862c42a7b7eSSam Leffler 		== IEEE80211_F_SHPREAMBLE)
2863c42a7b7eSSam Leffler 	struct ieee80211_node *ni = bf->bf_node;
2864c42a7b7eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2865c42a7b7eSSam Leffler 	struct mbuf *m = bf->bf_m;
2866c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2867c42a7b7eSSam Leffler 	struct ath_desc *ds;
2868c42a7b7eSSam Leffler 	int flags, antenna;
286955f63772SSam Leffler 	const HAL_RATE_TABLE *rt;
287055f63772SSam Leffler 	u_int8_t rix, rate;
2871c42a7b7eSSam Leffler 
28724a3ac3fcSSam Leffler 	DPRINTF(sc, ATH_DEBUG_BEACON_PROC, "%s: m %p len %u\n",
2873c42a7b7eSSam Leffler 		__func__, m, m->m_len);
28745591b213SSam Leffler 
28755591b213SSam Leffler 	/* setup descriptors */
28765591b213SSam Leffler 	ds = bf->bf_desc;
28776edf1dc7SAdrian Chadd 	bf->bf_last = bf;
28786edf1dc7SAdrian Chadd 	bf->bf_lastds = ds;
28795591b213SSam Leffler 
2880c42a7b7eSSam Leffler 	flags = HAL_TXDESC_NOACK;
2881c42a7b7eSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_hasveol) {
2882c42a7b7eSSam Leffler 		ds->ds_link = bf->bf_daddr;	/* self-linked */
2883c42a7b7eSSam Leffler 		flags |= HAL_TXDESC_VEOL;
2884c42a7b7eSSam Leffler 		/*
2885c42a7b7eSSam Leffler 		 * Let hardware handle antenna switching.
2886c42a7b7eSSam Leffler 		 */
28874866e6c2SSam Leffler 		antenna = sc->sc_txantenna;
2888c42a7b7eSSam Leffler 	} else {
28895591b213SSam Leffler 		ds->ds_link = 0;
2890c42a7b7eSSam Leffler 		/*
2891c42a7b7eSSam Leffler 		 * Switch antenna every 4 beacons.
2892c42a7b7eSSam Leffler 		 * XXX assumes two antenna
2893c42a7b7eSSam Leffler 		 */
2894b032f27cSSam Leffler 		if (sc->sc_txantenna != 0)
2895b032f27cSSam Leffler 			antenna = sc->sc_txantenna;
2896b032f27cSSam Leffler 		else if (sc->sc_stagbeacons && sc->sc_nbcnvaps != 0)
2897b032f27cSSam Leffler 			antenna = ((sc->sc_stats.ast_be_xmit / sc->sc_nbcnvaps) & 4 ? 2 : 1);
2898b032f27cSSam Leffler 		else
2899b032f27cSSam Leffler 			antenna = (sc->sc_stats.ast_be_xmit & 4 ? 2 : 1);
2900c42a7b7eSSam Leffler 	}
2901c42a7b7eSSam Leffler 
2902c42a7b7eSSam Leffler 	KASSERT(bf->bf_nseg == 1,
2903c42a7b7eSSam Leffler 		("multi-segment beacon frame; nseg %u", bf->bf_nseg));
29045591b213SSam Leffler 	ds->ds_data = bf->bf_segs[0].ds_addr;
29055591b213SSam Leffler 	/*
29065591b213SSam Leffler 	 * Calculate rate code.
29075591b213SSam Leffler 	 * XXX everything at min xmit rate
29085591b213SSam Leffler 	 */
2909b032f27cSSam Leffler 	rix = 0;
291055f63772SSam Leffler 	rt = sc->sc_currates;
291155f63772SSam Leffler 	rate = rt->info[rix].rateCode;
2912c42a7b7eSSam Leffler 	if (USE_SHPREAMBLE(ic))
291355f63772SSam Leffler 		rate |= rt->info[rix].shortPreamble;
29145591b213SSam Leffler 	ath_hal_setuptxdesc(ah, ds
2915c42a7b7eSSam Leffler 		, m->m_len + IEEE80211_CRC_LEN	/* frame length */
29165591b213SSam Leffler 		, sizeof(struct ieee80211_frame)/* header length */
29175591b213SSam Leffler 		, HAL_PKT_TYPE_BEACON		/* Atheros packet type */
2918c42a7b7eSSam Leffler 		, ni->ni_txpower		/* txpower XXX */
29195591b213SSam Leffler 		, rate, 1			/* series 0 rate/tries */
29205591b213SSam Leffler 		, HAL_TXKEYIX_INVALID		/* no encryption */
2921c42a7b7eSSam Leffler 		, antenna			/* antenna mode */
2922c42a7b7eSSam Leffler 		, flags				/* no ack, veol for beacons */
29235591b213SSam Leffler 		, 0				/* rts/cts rate */
29245591b213SSam Leffler 		, 0				/* rts/cts duration */
29255591b213SSam Leffler 	);
29265591b213SSam Leffler 	/* NB: beacon's BufLen must be a multiple of 4 bytes */
29275591b213SSam Leffler 	ath_hal_filltxdesc(ah, ds
2928c42a7b7eSSam Leffler 		, roundup(m->m_len, 4)		/* buffer length */
29295591b213SSam Leffler 		, AH_TRUE			/* first segment */
29305591b213SSam Leffler 		, AH_TRUE			/* last segment */
2931c42a7b7eSSam Leffler 		, ds				/* first descriptor */
29325591b213SSam Leffler 	);
2933b032f27cSSam Leffler #if 0
2934b032f27cSSam Leffler 	ath_desc_swap(ds);
2935b032f27cSSam Leffler #endif
2936c42a7b7eSSam Leffler #undef USE_SHPREAMBLE
29375591b213SSam Leffler }
29385591b213SSam Leffler 
2939b105a069SSam Leffler static void
2940b032f27cSSam Leffler ath_beacon_update(struct ieee80211vap *vap, int item)
2941b105a069SSam Leffler {
2942b032f27cSSam Leffler 	struct ieee80211_beacon_offsets *bo = &ATH_VAP(vap)->av_boff;
2943b105a069SSam Leffler 
2944b105a069SSam Leffler 	setbit(bo->bo_flags, item);
2945b105a069SSam Leffler }
2946b105a069SSam Leffler 
2947c42a7b7eSSam Leffler /*
2948622b3fd2SSam Leffler  * Append the contents of src to dst; both queues
2949622b3fd2SSam Leffler  * are assumed to be locked.
2950622b3fd2SSam Leffler  */
2951622b3fd2SSam Leffler static void
2952622b3fd2SSam Leffler ath_txqmove(struct ath_txq *dst, struct ath_txq *src)
2953622b3fd2SSam Leffler {
2954e86fd7a7SAdrian Chadd 
2955e86fd7a7SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(dst);
2956e86fd7a7SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(src);
2957e86fd7a7SAdrian Chadd 
29586b349e5aSAdrian Chadd 	TAILQ_CONCAT(&dst->axq_q, &src->axq_q, bf_list);
2959622b3fd2SSam Leffler 	dst->axq_link = src->axq_link;
2960622b3fd2SSam Leffler 	src->axq_link = NULL;
2961622b3fd2SSam Leffler 	dst->axq_depth += src->axq_depth;
29626edf1dc7SAdrian Chadd 	dst->axq_aggr_depth += src->axq_aggr_depth;
2963622b3fd2SSam Leffler 	src->axq_depth = 0;
29646edf1dc7SAdrian Chadd 	src->axq_aggr_depth = 0;
2965622b3fd2SSam Leffler }
2966622b3fd2SSam Leffler 
2967622b3fd2SSam Leffler /*
2968c42a7b7eSSam Leffler  * Transmit a beacon frame at SWBA.  Dynamic updates to the
2969c42a7b7eSSam Leffler  * frame contents are done as needed and the slot time is
2970c42a7b7eSSam Leffler  * also adjusted based on current state.
2971c42a7b7eSSam Leffler  */
29725591b213SSam Leffler static void
29735591b213SSam Leffler ath_beacon_proc(void *arg, int pending)
29745591b213SSam Leffler {
29755591b213SSam Leffler 	struct ath_softc *sc = arg;
29765591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2977b032f27cSSam Leffler 	struct ieee80211vap *vap;
2978b032f27cSSam Leffler 	struct ath_buf *bf;
2979b032f27cSSam Leffler 	int slot, otherant;
2980b032f27cSSam Leffler 	uint32_t bfaddr;
29815591b213SSam Leffler 
2982c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_BEACON_PROC, "%s: pending %u\n",
2983c42a7b7eSSam Leffler 		__func__, pending);
2984c42a7b7eSSam Leffler 	/*
2985c42a7b7eSSam Leffler 	 * Check if the previous beacon has gone out.  If
2986c66c48cbSSam Leffler 	 * not don't try to post another, skip this period
2987c66c48cbSSam Leffler 	 * and wait for the next.  Missed beacons indicate
2988c66c48cbSSam Leffler 	 * a problem and should not occur.  If we miss too
2989c66c48cbSSam Leffler 	 * many consecutive beacons reset the device.
2990c42a7b7eSSam Leffler 	 */
2991c42a7b7eSSam Leffler 	if (ath_hal_numtxpending(ah, sc->sc_bhalq) != 0) {
2992c42a7b7eSSam Leffler 		sc->sc_bmisscount++;
29937ec4e6b8SAdrian Chadd 		sc->sc_stats.ast_be_missed++;
29944a3ac3fcSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
2995c42a7b7eSSam Leffler 			"%s: missed %u consecutive beacons\n",
2996c42a7b7eSSam Leffler 			__func__, sc->sc_bmisscount);
2997a32ac9d3SSam Leffler 		if (sc->sc_bmisscount >= ath_bstuck_threshold)
29980bbf5441SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_bstucktask);
2999c42a7b7eSSam Leffler 		return;
3000c42a7b7eSSam Leffler 	}
3001c42a7b7eSSam Leffler 	if (sc->sc_bmisscount != 0) {
3002c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
3003c42a7b7eSSam Leffler 			"%s: resume beacon xmit after %u misses\n",
3004c42a7b7eSSam Leffler 			__func__, sc->sc_bmisscount);
3005c42a7b7eSSam Leffler 		sc->sc_bmisscount = 0;
3006c42a7b7eSSam Leffler 	}
3007c42a7b7eSSam Leffler 
3008b032f27cSSam Leffler 	if (sc->sc_stagbeacons) {			/* staggered beacons */
3009b032f27cSSam Leffler 		struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3010b032f27cSSam Leffler 		uint32_t tsftu;
3011b032f27cSSam Leffler 
3012b032f27cSSam Leffler 		tsftu = ath_hal_gettsf32(ah) >> 10;
3013b032f27cSSam Leffler 		/* XXX lintval */
3014b032f27cSSam Leffler 		slot = ((tsftu % ic->ic_lintval) * ATH_BCBUF) / ic->ic_lintval;
3015b032f27cSSam Leffler 		vap = sc->sc_bslot[(slot+1) % ATH_BCBUF];
3016b032f27cSSam Leffler 		bfaddr = 0;
3017309a3e45SSam Leffler 		if (vap != NULL && vap->iv_state >= IEEE80211_S_RUN) {
3018b032f27cSSam Leffler 			bf = ath_beacon_generate(sc, vap);
3019b032f27cSSam Leffler 			if (bf != NULL)
3020b032f27cSSam Leffler 				bfaddr = bf->bf_daddr;
3021b032f27cSSam Leffler 		}
3022b032f27cSSam Leffler 	} else {					/* burst'd beacons */
3023b032f27cSSam Leffler 		uint32_t *bflink = &bfaddr;
3024b032f27cSSam Leffler 
3025b032f27cSSam Leffler 		for (slot = 0; slot < ATH_BCBUF; slot++) {
3026b032f27cSSam Leffler 			vap = sc->sc_bslot[slot];
3027309a3e45SSam Leffler 			if (vap != NULL && vap->iv_state >= IEEE80211_S_RUN) {
3028b032f27cSSam Leffler 				bf = ath_beacon_generate(sc, vap);
3029b032f27cSSam Leffler 				if (bf != NULL) {
3030b032f27cSSam Leffler 					*bflink = bf->bf_daddr;
3031b032f27cSSam Leffler 					bflink = &bf->bf_desc->ds_link;
3032c42a7b7eSSam Leffler 				}
3033c42a7b7eSSam Leffler 			}
3034b032f27cSSam Leffler 		}
3035b032f27cSSam Leffler 		*bflink = 0;				/* terminate list */
3036622b3fd2SSam Leffler 	}
3037c42a7b7eSSam Leffler 
3038c42a7b7eSSam Leffler 	/*
3039c42a7b7eSSam Leffler 	 * Handle slot time change when a non-ERP station joins/leaves
3040c42a7b7eSSam Leffler 	 * an 11g network.  The 802.11 layer notifies us via callback,
3041c42a7b7eSSam Leffler 	 * we mark updateslot, then wait one beacon before effecting
3042c42a7b7eSSam Leffler 	 * the change.  This gives associated stations at least one
3043c42a7b7eSSam Leffler 	 * beacon interval to note the state change.
3044c42a7b7eSSam Leffler 	 */
3045c42a7b7eSSam Leffler 	/* XXX locking */
3046b032f27cSSam Leffler 	if (sc->sc_updateslot == UPDATE) {
3047c42a7b7eSSam Leffler 		sc->sc_updateslot = COMMIT;	/* commit next beacon */
3048b032f27cSSam Leffler 		sc->sc_slotupdate = slot;
3049b032f27cSSam Leffler 	} else if (sc->sc_updateslot == COMMIT && sc->sc_slotupdate == slot)
3050c42a7b7eSSam Leffler 		ath_setslottime(sc);		/* commit change to h/w */
3051c42a7b7eSSam Leffler 
3052c42a7b7eSSam Leffler 	/*
3053c42a7b7eSSam Leffler 	 * Check recent per-antenna transmit statistics and flip
3054c42a7b7eSSam Leffler 	 * the default antenna if noticeably more frames went out
3055c42a7b7eSSam Leffler 	 * on the non-default antenna.
3056c42a7b7eSSam Leffler 	 * XXX assumes 2 anntenae
3057c42a7b7eSSam Leffler 	 */
3058b032f27cSSam Leffler 	if (!sc->sc_diversity && (!sc->sc_stagbeacons || slot == 0)) {
3059c42a7b7eSSam Leffler 		otherant = sc->sc_defant & 1 ? 2 : 1;
3060c42a7b7eSSam Leffler 		if (sc->sc_ant_tx[otherant] > sc->sc_ant_tx[sc->sc_defant] + 2)
3061c42a7b7eSSam Leffler 			ath_setdefantenna(sc, otherant);
3062c42a7b7eSSam Leffler 		sc->sc_ant_tx[1] = sc->sc_ant_tx[2] = 0;
3063b032f27cSSam Leffler 	}
3064c42a7b7eSSam Leffler 
3065b032f27cSSam Leffler 	if (bfaddr != 0) {
3066c42a7b7eSSam Leffler 		/*
3067c42a7b7eSSam Leffler 		 * Stop any current dma and put the new frame on the queue.
3068c42a7b7eSSam Leffler 		 * This should never fail since we check above that no frames
3069c42a7b7eSSam Leffler 		 * are still pending on the queue.
3070c42a7b7eSSam Leffler 		 */
30715591b213SSam Leffler 		if (!ath_hal_stoptxdma(ah, sc->sc_bhalq)) {
3072c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
3073c42a7b7eSSam Leffler 				"%s: beacon queue %u did not stop?\n",
3074c42a7b7eSSam Leffler 				__func__, sc->sc_bhalq);
30755591b213SSam Leffler 		}
3076b032f27cSSam Leffler 		/* NB: cabq traffic should already be queued and primed */
3077b032f27cSSam Leffler 		ath_hal_puttxbuf(ah, sc->sc_bhalq, bfaddr);
3078b032f27cSSam Leffler 		ath_hal_txstart(ah, sc->sc_bhalq);
3079b032f27cSSam Leffler 
3080b032f27cSSam Leffler 		sc->sc_stats.ast_be_xmit++;
3081b032f27cSSam Leffler 	}
3082b032f27cSSam Leffler }
3083b032f27cSSam Leffler 
3084b032f27cSSam Leffler static struct ath_buf *
3085b032f27cSSam Leffler ath_beacon_generate(struct ath_softc *sc, struct ieee80211vap *vap)
3086b032f27cSSam Leffler {
3087b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
3088b032f27cSSam Leffler 	struct ath_txq *cabq = sc->sc_cabq;
3089b032f27cSSam Leffler 	struct ath_buf *bf;
3090b032f27cSSam Leffler 	struct mbuf *m;
3091b032f27cSSam Leffler 	int nmcastq, error;
3092b032f27cSSam Leffler 
3093309a3e45SSam Leffler 	KASSERT(vap->iv_state >= IEEE80211_S_RUN,
3094b032f27cSSam Leffler 	    ("not running, state %d", vap->iv_state));
3095b032f27cSSam Leffler 	KASSERT(avp->av_bcbuf != NULL, ("no beacon buffer"));
3096b032f27cSSam Leffler 
3097b032f27cSSam Leffler 	/*
3098b032f27cSSam Leffler 	 * Update dynamic beacon contents.  If this returns
3099b032f27cSSam Leffler 	 * non-zero then we need to remap the memory because
3100b032f27cSSam Leffler 	 * the beacon frame changed size (probably because
3101b032f27cSSam Leffler 	 * of the TIM bitmap).
3102b032f27cSSam Leffler 	 */
3103b032f27cSSam Leffler 	bf = avp->av_bcbuf;
3104b032f27cSSam Leffler 	m = bf->bf_m;
310591d92caeSAdrian Chadd 	/* XXX lock mcastq? */
3106b032f27cSSam Leffler 	nmcastq = avp->av_mcastq.axq_depth;
310791d92caeSAdrian Chadd 
3108b032f27cSSam Leffler 	if (ieee80211_beacon_update(bf->bf_node, &avp->av_boff, m, nmcastq)) {
3109b032f27cSSam Leffler 		/* XXX too conservative? */
3110b032f27cSSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
3111b032f27cSSam Leffler 		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m,
3112b032f27cSSam Leffler 					     bf->bf_segs, &bf->bf_nseg,
3113b032f27cSSam Leffler 					     BUS_DMA_NOWAIT);
3114b032f27cSSam Leffler 		if (error != 0) {
3115b032f27cSSam Leffler 			if_printf(vap->iv_ifp,
3116b032f27cSSam Leffler 			    "%s: bus_dmamap_load_mbuf_sg failed, error %u\n",
3117b032f27cSSam Leffler 			    __func__, error);
3118b032f27cSSam Leffler 			return NULL;
3119b032f27cSSam Leffler 		}
3120b032f27cSSam Leffler 	}
3121b032f27cSSam Leffler 	if ((avp->av_boff.bo_tim[4] & 1) && cabq->axq_depth) {
3122b032f27cSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
3123b032f27cSSam Leffler 		    "%s: cabq did not drain, mcastq %u cabq %u\n",
3124b032f27cSSam Leffler 		    __func__, nmcastq, cabq->axq_depth);
3125b032f27cSSam Leffler 		sc->sc_stats.ast_cabq_busy++;
3126b032f27cSSam Leffler 		if (sc->sc_nvaps > 1 && sc->sc_stagbeacons) {
3127b032f27cSSam Leffler 			/*
3128b032f27cSSam Leffler 			 * CABQ traffic from a previous vap is still pending.
3129b032f27cSSam Leffler 			 * We must drain the q before this beacon frame goes
3130b032f27cSSam Leffler 			 * out as otherwise this vap's stations will get cab
3131b032f27cSSam Leffler 			 * frames from a different vap.
3132b032f27cSSam Leffler 			 * XXX could be slow causing us to miss DBA
3133b032f27cSSam Leffler 			 */
3134b032f27cSSam Leffler 			ath_tx_draintxq(sc, cabq);
3135b032f27cSSam Leffler 		}
3136b032f27cSSam Leffler 	}
3137b032f27cSSam Leffler 	ath_beacon_setup(sc, bf);
31385591b213SSam Leffler 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
31395591b213SSam Leffler 
3140c42a7b7eSSam Leffler 	/*
3141c42a7b7eSSam Leffler 	 * Enable the CAB queue before the beacon queue to
3142c42a7b7eSSam Leffler 	 * insure cab frames are triggered by this beacon.
3143c42a7b7eSSam Leffler 	 */
3144b032f27cSSam Leffler 	if (avp->av_boff.bo_tim[4] & 1) {
3145b032f27cSSam Leffler 		struct ath_hal *ah = sc->sc_ah;
3146b032f27cSSam Leffler 
3147f3af83f7SSam Leffler 		/* NB: only at DTIM */
3148622b3fd2SSam Leffler 		ATH_TXQ_LOCK(cabq);
3149b032f27cSSam Leffler 		ATH_TXQ_LOCK(&avp->av_mcastq);
3150622b3fd2SSam Leffler 		if (nmcastq) {
3151622b3fd2SSam Leffler 			struct ath_buf *bfm;
3152622b3fd2SSam Leffler 
3153622b3fd2SSam Leffler 			/*
3154622b3fd2SSam Leffler 			 * Move frames from the s/w mcast q to the h/w cab q.
3155b032f27cSSam Leffler 			 * XXX MORE_DATA bit
3156622b3fd2SSam Leffler 			 */
31576b349e5aSAdrian Chadd 			bfm = TAILQ_FIRST(&avp->av_mcastq.axq_q);
3158622b3fd2SSam Leffler 			if (cabq->axq_link != NULL) {
3159622b3fd2SSam Leffler 				*cabq->axq_link = bfm->bf_daddr;
3160622b3fd2SSam Leffler 			} else
3161622b3fd2SSam Leffler 				ath_hal_puttxbuf(ah, cabq->axq_qnum,
3162622b3fd2SSam Leffler 					bfm->bf_daddr);
3163b032f27cSSam Leffler 			ath_txqmove(cabq, &avp->av_mcastq);
3164622b3fd2SSam Leffler 
3165622b3fd2SSam Leffler 			sc->sc_stats.ast_cabq_xmit += nmcastq;
3166622b3fd2SSam Leffler 		}
3167622b3fd2SSam Leffler 		/* NB: gated by beacon so safe to start here */
31686b349e5aSAdrian Chadd 		if (! TAILQ_EMPTY(&(cabq->axq_q)))
3169622b3fd2SSam Leffler 			ath_hal_txstart(ah, cabq->axq_qnum);
3170b032f27cSSam Leffler 		ATH_TXQ_UNLOCK(&avp->av_mcastq);
31717b15790aSAdrian Chadd 		ATH_TXQ_UNLOCK(cabq);
3172622b3fd2SSam Leffler 	}
3173b032f27cSSam Leffler 	return bf;
3174b032f27cSSam Leffler }
3175b032f27cSSam Leffler 
3176b032f27cSSam Leffler static void
3177b032f27cSSam Leffler ath_beacon_start_adhoc(struct ath_softc *sc, struct ieee80211vap *vap)
3178b032f27cSSam Leffler {
3179b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
3180b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3181b032f27cSSam Leffler 	struct ath_buf *bf;
3182b032f27cSSam Leffler 	struct mbuf *m;
3183b032f27cSSam Leffler 	int error;
3184b032f27cSSam Leffler 
3185b032f27cSSam Leffler 	KASSERT(avp->av_bcbuf != NULL, ("no beacon buffer"));
3186b032f27cSSam Leffler 
3187b032f27cSSam Leffler 	/*
3188b032f27cSSam Leffler 	 * Update dynamic beacon contents.  If this returns
3189b032f27cSSam Leffler 	 * non-zero then we need to remap the memory because
3190b032f27cSSam Leffler 	 * the beacon frame changed size (probably because
3191b032f27cSSam Leffler 	 * of the TIM bitmap).
3192b032f27cSSam Leffler 	 */
3193b032f27cSSam Leffler 	bf = avp->av_bcbuf;
3194b032f27cSSam Leffler 	m = bf->bf_m;
3195b032f27cSSam Leffler 	if (ieee80211_beacon_update(bf->bf_node, &avp->av_boff, m, 0)) {
3196b032f27cSSam Leffler 		/* XXX too conservative? */
3197b032f27cSSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
3198b032f27cSSam Leffler 		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m,
3199b032f27cSSam Leffler 					     bf->bf_segs, &bf->bf_nseg,
3200b032f27cSSam Leffler 					     BUS_DMA_NOWAIT);
3201b032f27cSSam Leffler 		if (error != 0) {
3202b032f27cSSam Leffler 			if_printf(vap->iv_ifp,
3203b032f27cSSam Leffler 			    "%s: bus_dmamap_load_mbuf_sg failed, error %u\n",
3204b032f27cSSam Leffler 			    __func__, error);
3205b032f27cSSam Leffler 			return;
3206b032f27cSSam Leffler 		}
3207b032f27cSSam Leffler 	}
3208b032f27cSSam Leffler 	ath_beacon_setup(sc, bf);
3209b032f27cSSam Leffler 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
3210b032f27cSSam Leffler 
3211b032f27cSSam Leffler 	/* NB: caller is known to have already stopped tx dma */
32125591b213SSam Leffler 	ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr);
32135591b213SSam Leffler 	ath_hal_txstart(ah, sc->sc_bhalq);
32145591b213SSam Leffler }
32155591b213SSam Leffler 
3216c42a7b7eSSam Leffler /*
3217d52f7132SAdrian Chadd  * Reset the hardware, with no loss.
3218d52f7132SAdrian Chadd  *
3219d52f7132SAdrian Chadd  * This can't be used for a general case reset.
3220d52f7132SAdrian Chadd  */
3221d52f7132SAdrian Chadd static void
3222d52f7132SAdrian Chadd ath_reset_proc(void *arg, int pending)
3223d52f7132SAdrian Chadd {
3224d52f7132SAdrian Chadd 	struct ath_softc *sc = arg;
3225d52f7132SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
3226d52f7132SAdrian Chadd 
3227d52f7132SAdrian Chadd #if 0
3228d52f7132SAdrian Chadd 	if_printf(ifp, "%s: resetting\n", __func__);
3229d52f7132SAdrian Chadd #endif
3230d52f7132SAdrian Chadd 	ath_reset(ifp, ATH_RESET_NOLOSS);
3231d52f7132SAdrian Chadd }
3232d52f7132SAdrian Chadd 
3233d52f7132SAdrian Chadd /*
3234c42a7b7eSSam Leffler  * Reset the hardware after detecting beacons have stopped.
3235c42a7b7eSSam Leffler  */
3236c42a7b7eSSam Leffler static void
3237c42a7b7eSSam Leffler ath_bstuck_proc(void *arg, int pending)
3238c42a7b7eSSam Leffler {
3239c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
3240fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
324116d4de92SAdrian Chadd 	uint32_t hangs = 0;
324216d4de92SAdrian Chadd 
324316d4de92SAdrian Chadd 	if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0)
324416d4de92SAdrian Chadd 		if_printf(ifp, "bb hang detected (0x%x)\n", hangs);
3245c42a7b7eSSam Leffler 
3246c42a7b7eSSam Leffler 	if_printf(ifp, "stuck beacon; resetting (bmiss count %u)\n",
3247c42a7b7eSSam Leffler 		sc->sc_bmisscount);
3248c2e34459SSam Leffler 	sc->sc_stats.ast_bstuck++;
324916d4de92SAdrian Chadd 	/*
325016d4de92SAdrian Chadd 	 * This assumes that there's no simultaneous channel mode change
325116d4de92SAdrian Chadd 	 * occuring.
325216d4de92SAdrian Chadd 	 */
3253517526efSAdrian Chadd 	ath_reset(ifp, ATH_RESET_NOLOSS);
3254c42a7b7eSSam Leffler }
3255c42a7b7eSSam Leffler 
3256c42a7b7eSSam Leffler /*
3257b032f27cSSam Leffler  * Reclaim beacon resources and return buffer to the pool.
3258b032f27cSSam Leffler  */
3259b032f27cSSam Leffler static void
3260b032f27cSSam Leffler ath_beacon_return(struct ath_softc *sc, struct ath_buf *bf)
3261b032f27cSSam Leffler {
3262b032f27cSSam Leffler 
32637ebd03d7SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: free bf=%p, bf_m=%p, bf_node=%p\n",
32647ebd03d7SAdrian Chadd 	    __func__, bf, bf->bf_m, bf->bf_node);
3265b032f27cSSam Leffler 	if (bf->bf_m != NULL) {
3266b032f27cSSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
3267b032f27cSSam Leffler 		m_freem(bf->bf_m);
3268b032f27cSSam Leffler 		bf->bf_m = NULL;
3269b032f27cSSam Leffler 	}
3270b032f27cSSam Leffler 	if (bf->bf_node != NULL) {
3271b032f27cSSam Leffler 		ieee80211_free_node(bf->bf_node);
3272b032f27cSSam Leffler 		bf->bf_node = NULL;
3273b032f27cSSam Leffler 	}
32746b349e5aSAdrian Chadd 	TAILQ_INSERT_TAIL(&sc->sc_bbuf, bf, bf_list);
3275b032f27cSSam Leffler }
3276b032f27cSSam Leffler 
3277b032f27cSSam Leffler /*
3278c42a7b7eSSam Leffler  * Reclaim beacon resources.
3279c42a7b7eSSam Leffler  */
32805591b213SSam Leffler static void
32815591b213SSam Leffler ath_beacon_free(struct ath_softc *sc)
32825591b213SSam Leffler {
3283c42a7b7eSSam Leffler 	struct ath_buf *bf;
32845591b213SSam Leffler 
32856b349e5aSAdrian Chadd 	TAILQ_FOREACH(bf, &sc->sc_bbuf, bf_list) {
32867ebd03d7SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE,
32877ebd03d7SAdrian Chadd 		    "%s: free bf=%p, bf_m=%p, bf_node=%p\n",
32887ebd03d7SAdrian Chadd 		        __func__, bf, bf->bf_m, bf->bf_node);
32895591b213SSam Leffler 		if (bf->bf_m != NULL) {
32905591b213SSam Leffler 			bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
32915591b213SSam Leffler 			m_freem(bf->bf_m);
32925591b213SSam Leffler 			bf->bf_m = NULL;
3293f818612bSSam Leffler 		}
3294f818612bSSam Leffler 		if (bf->bf_node != NULL) {
3295f818612bSSam Leffler 			ieee80211_free_node(bf->bf_node);
32965591b213SSam Leffler 			bf->bf_node = NULL;
32975591b213SSam Leffler 		}
32985591b213SSam Leffler 	}
3299f818612bSSam Leffler }
33005591b213SSam Leffler 
33015591b213SSam Leffler /*
33025591b213SSam Leffler  * Configure the beacon and sleep timers.
33035591b213SSam Leffler  *
33045591b213SSam Leffler  * When operating as an AP this resets the TSF and sets
33055591b213SSam Leffler  * up the hardware to notify us when we need to issue beacons.
33065591b213SSam Leffler  *
33075591b213SSam Leffler  * When operating in station mode this sets up the beacon
33085591b213SSam Leffler  * timers according to the timestamp of the last received
33095591b213SSam Leffler  * beacon and the current TSF, configures PCF and DTIM
33105591b213SSam Leffler  * handling, programs the sleep registers so the hardware
33115591b213SSam Leffler  * will wakeup in time to receive beacons, and configures
33125591b213SSam Leffler  * the beacon miss handling so we'll receive a BMISS
33135591b213SSam Leffler  * interrupt when we stop seeing beacons from the AP
33145591b213SSam Leffler  * we've associated with.
33155591b213SSam Leffler  */
33165591b213SSam Leffler static void
3317b032f27cSSam Leffler ath_beacon_config(struct ath_softc *sc, struct ieee80211vap *vap)
33185591b213SSam Leffler {
331980d939bfSSam Leffler #define	TSF_TO_TU(_h,_l) \
332080d939bfSSam Leffler 	((((u_int32_t)(_h)) << 22) | (((u_int32_t)(_l)) >> 10))
332180d939bfSSam Leffler #define	FUDGE	2
33225591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3323b032f27cSSam Leffler 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3324b032f27cSSam Leffler 	struct ieee80211_node *ni;
332580d939bfSSam Leffler 	u_int32_t nexttbtt, intval, tsftu;
332680d939bfSSam Leffler 	u_int64_t tsf;
33275591b213SSam Leffler 
3328b032f27cSSam Leffler 	if (vap == NULL)
3329b032f27cSSam Leffler 		vap = TAILQ_FIRST(&ic->ic_vaps);	/* XXX */
333080767531SAdrian Chadd 	ni = ieee80211_ref_node(vap->iv_bss);
3331b032f27cSSam Leffler 
33328371372bSSam Leffler 	/* extract tstamp from last beacon and convert to TU */
33338371372bSSam Leffler 	nexttbtt = TSF_TO_TU(LE_READ_4(ni->ni_tstamp.data + 4),
33348371372bSSam Leffler 			     LE_READ_4(ni->ni_tstamp.data));
333559aa14a9SRui Paulo 	if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
333659aa14a9SRui Paulo 	    ic->ic_opmode == IEEE80211_M_MBSS) {
3337b032f27cSSam Leffler 		/*
333859aa14a9SRui Paulo 		 * For multi-bss ap/mesh support beacons are either staggered
3339b032f27cSSam Leffler 		 * evenly over N slots or burst together.  For the former
3340b032f27cSSam Leffler 		 * arrange for the SWBA to be delivered for each slot.
3341b032f27cSSam Leffler 		 * Slots that are not occupied will generate nothing.
3342b032f27cSSam Leffler 		 */
33438371372bSSam Leffler 		/* NB: the beacon interval is kept internally in TU's */
33444bacf7c1SSam Leffler 		intval = ni->ni_intval & HAL_BEACON_PERIOD;
3345b032f27cSSam Leffler 		if (sc->sc_stagbeacons)
3346b032f27cSSam Leffler 			intval /= ATH_BCBUF;
3347b032f27cSSam Leffler 	} else {
3348b032f27cSSam Leffler 		/* NB: the beacon interval is kept internally in TU's */
3349b032f27cSSam Leffler 		intval = ni->ni_intval & HAL_BEACON_PERIOD;
3350b032f27cSSam Leffler 	}
3351a6c992f4SSam Leffler 	if (nexttbtt == 0)		/* e.g. for ap mode */
3352a6c992f4SSam Leffler 		nexttbtt = intval;
3353a6c992f4SSam Leffler 	else if (intval)		/* NB: can be 0 for monitor mode */
3354a6c992f4SSam Leffler 		nexttbtt = roundup(nexttbtt, intval);
3355a6c992f4SSam Leffler 	DPRINTF(sc, ATH_DEBUG_BEACON, "%s: nexttbtt %u intval %u (%u)\n",
3356a6c992f4SSam Leffler 		__func__, nexttbtt, intval, ni->ni_intval);
3357b032f27cSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA && !sc->sc_swbmiss) {
33585591b213SSam Leffler 		HAL_BEACON_STATE bs;
33598371372bSSam Leffler 		int dtimperiod, dtimcount;
33608371372bSSam Leffler 		int cfpperiod, cfpcount;
33615591b213SSam Leffler 
33628371372bSSam Leffler 		/*
33638371372bSSam Leffler 		 * Setup dtim and cfp parameters according to
33648371372bSSam Leffler 		 * last beacon we received (which may be none).
33658371372bSSam Leffler 		 */
33668371372bSSam Leffler 		dtimperiod = ni->ni_dtim_period;
33678371372bSSam Leffler 		if (dtimperiod <= 0)		/* NB: 0 if not known */
33688371372bSSam Leffler 			dtimperiod = 1;
33698371372bSSam Leffler 		dtimcount = ni->ni_dtim_count;
33708371372bSSam Leffler 		if (dtimcount >= dtimperiod)	/* NB: sanity check */
33718371372bSSam Leffler 			dtimcount = 0;		/* XXX? */
33728371372bSSam Leffler 		cfpperiod = 1;			/* NB: no PCF support yet */
33738371372bSSam Leffler 		cfpcount = 0;
33748371372bSSam Leffler 		/*
33758371372bSSam Leffler 		 * Pull nexttbtt forward to reflect the current
33768371372bSSam Leffler 		 * TSF and calculate dtim+cfp state for the result.
33778371372bSSam Leffler 		 */
33788371372bSSam Leffler 		tsf = ath_hal_gettsf64(ah);
337980d939bfSSam Leffler 		tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
33808371372bSSam Leffler 		do {
33818371372bSSam Leffler 			nexttbtt += intval;
33828371372bSSam Leffler 			if (--dtimcount < 0) {
33838371372bSSam Leffler 				dtimcount = dtimperiod - 1;
33848371372bSSam Leffler 				if (--cfpcount < 0)
33858371372bSSam Leffler 					cfpcount = cfpperiod - 1;
33868371372bSSam Leffler 			}
33878371372bSSam Leffler 		} while (nexttbtt < tsftu);
33885591b213SSam Leffler 		memset(&bs, 0, sizeof(bs));
3389a6c992f4SSam Leffler 		bs.bs_intval = intval;
33905591b213SSam Leffler 		bs.bs_nexttbtt = nexttbtt;
33918371372bSSam Leffler 		bs.bs_dtimperiod = dtimperiod*intval;
33928371372bSSam Leffler 		bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval;
33938371372bSSam Leffler 		bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod;
33948371372bSSam Leffler 		bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod;
33958371372bSSam Leffler 		bs.bs_cfpmaxduration = 0;
33968371372bSSam Leffler #if 0
33975591b213SSam Leffler 		/*
3398c42a7b7eSSam Leffler 		 * The 802.11 layer records the offset to the DTIM
3399c42a7b7eSSam Leffler 		 * bitmap while receiving beacons; use it here to
3400c42a7b7eSSam Leffler 		 * enable h/w detection of our AID being marked in
3401c42a7b7eSSam Leffler 		 * the bitmap vector (to indicate frames for us are
3402c42a7b7eSSam Leffler 		 * pending at the AP).
34038371372bSSam Leffler 		 * XXX do DTIM handling in s/w to WAR old h/w bugs
34048371372bSSam Leffler 		 * XXX enable based on h/w rev for newer chips
3405c42a7b7eSSam Leffler 		 */
3406c42a7b7eSSam Leffler 		bs.bs_timoffset = ni->ni_timoff;
34078371372bSSam Leffler #endif
3408c42a7b7eSSam Leffler 		/*
34095591b213SSam Leffler 		 * Calculate the number of consecutive beacons to miss
341068e8e04eSSam Leffler 		 * before taking a BMISS interrupt.
34115591b213SSam Leffler 		 * Note that we clamp the result to at most 10 beacons.
34125591b213SSam Leffler 		 */
3413b032f27cSSam Leffler 		bs.bs_bmissthreshold = vap->iv_bmissthreshold;
34145591b213SSam Leffler 		if (bs.bs_bmissthreshold > 10)
34155591b213SSam Leffler 			bs.bs_bmissthreshold = 10;
34165591b213SSam Leffler 		else if (bs.bs_bmissthreshold <= 0)
34175591b213SSam Leffler 			bs.bs_bmissthreshold = 1;
34185591b213SSam Leffler 
34195591b213SSam Leffler 		/*
34205591b213SSam Leffler 		 * Calculate sleep duration.  The configuration is
34215591b213SSam Leffler 		 * given in ms.  We insure a multiple of the beacon
34225591b213SSam Leffler 		 * period is used.  Also, if the sleep duration is
34235591b213SSam Leffler 		 * greater than the DTIM period then it makes senses
34245591b213SSam Leffler 		 * to make it a multiple of that.
34255591b213SSam Leffler 		 *
34265591b213SSam Leffler 		 * XXX fixed at 100ms
34275591b213SSam Leffler 		 */
34284bacf7c1SSam Leffler 		bs.bs_sleepduration =
34294bacf7c1SSam Leffler 			roundup(IEEE80211_MS_TO_TU(100), bs.bs_intval);
34305591b213SSam Leffler 		if (bs.bs_sleepduration > bs.bs_dtimperiod)
34315591b213SSam Leffler 			bs.bs_sleepduration = roundup(bs.bs_sleepduration, bs.bs_dtimperiod);
34325591b213SSam Leffler 
3433c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
34348371372bSSam 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"
34355591b213SSam Leffler 			, __func__
34368371372bSSam Leffler 			, tsf, tsftu
34375591b213SSam Leffler 			, bs.bs_intval
34385591b213SSam Leffler 			, bs.bs_nexttbtt
34395591b213SSam Leffler 			, bs.bs_dtimperiod
34405591b213SSam Leffler 			, bs.bs_nextdtim
34415591b213SSam Leffler 			, bs.bs_bmissthreshold
34425591b213SSam Leffler 			, bs.bs_sleepduration
3443c42a7b7eSSam Leffler 			, bs.bs_cfpperiod
3444c42a7b7eSSam Leffler 			, bs.bs_cfpmaxduration
3445c42a7b7eSSam Leffler 			, bs.bs_cfpnext
3446c42a7b7eSSam Leffler 			, bs.bs_timoffset
3447c42a7b7eSSam Leffler 		);
34485591b213SSam Leffler 		ath_hal_intrset(ah, 0);
3449c42a7b7eSSam Leffler 		ath_hal_beacontimers(ah, &bs);
34505591b213SSam Leffler 		sc->sc_imask |= HAL_INT_BMISS;
34515591b213SSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
34525591b213SSam Leffler 	} else {
34535591b213SSam Leffler 		ath_hal_intrset(ah, 0);
3454a6c992f4SSam Leffler 		if (nexttbtt == intval)
3455c42a7b7eSSam Leffler 			intval |= HAL_BEACON_RESET_TSF;
3456c42a7b7eSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS) {
3457c42a7b7eSSam Leffler 			/*
3458c42a7b7eSSam Leffler 			 * In IBSS mode enable the beacon timers but only
3459c42a7b7eSSam Leffler 			 * enable SWBA interrupts if we need to manually
3460c42a7b7eSSam Leffler 			 * prepare beacon frames.  Otherwise we use a
3461c42a7b7eSSam Leffler 			 * self-linked tx descriptor and let the hardware
3462c42a7b7eSSam Leffler 			 * deal with things.
3463c42a7b7eSSam Leffler 			 */
3464c42a7b7eSSam Leffler 			intval |= HAL_BEACON_ENA;
3465c42a7b7eSSam Leffler 			if (!sc->sc_hasveol)
3466c42a7b7eSSam Leffler 				sc->sc_imask |= HAL_INT_SWBA;
346780d939bfSSam Leffler 			if ((intval & HAL_BEACON_RESET_TSF) == 0) {
346880d939bfSSam Leffler 				/*
346980d939bfSSam Leffler 				 * Pull nexttbtt forward to reflect
347080d939bfSSam Leffler 				 * the current TSF.
347180d939bfSSam Leffler 				 */
347280d939bfSSam Leffler 				tsf = ath_hal_gettsf64(ah);
347380d939bfSSam Leffler 				tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
347480d939bfSSam Leffler 				do {
347580d939bfSSam Leffler 					nexttbtt += intval;
347680d939bfSSam Leffler 				} while (nexttbtt < tsftu);
347780d939bfSSam Leffler 			}
34780f2e86fbSSam Leffler 			ath_beaconq_config(sc);
347959aa14a9SRui Paulo 		} else if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
348059aa14a9SRui Paulo 		    ic->ic_opmode == IEEE80211_M_MBSS) {
3481c42a7b7eSSam Leffler 			/*
348259aa14a9SRui Paulo 			 * In AP/mesh mode we enable the beacon timers
348359aa14a9SRui Paulo 			 * and SWBA interrupts to prepare beacon frames.
3484c42a7b7eSSam Leffler 			 */
3485c42a7b7eSSam Leffler 			intval |= HAL_BEACON_ENA;
34865591b213SSam Leffler 			sc->sc_imask |= HAL_INT_SWBA;	/* beacon prepare */
34870f2e86fbSSam Leffler 			ath_beaconq_config(sc);
3488c42a7b7eSSam Leffler 		}
3489c42a7b7eSSam Leffler 		ath_hal_beaconinit(ah, nexttbtt, intval);
3490c42a7b7eSSam Leffler 		sc->sc_bmisscount = 0;
34915591b213SSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
3492c42a7b7eSSam Leffler 		/*
3493c42a7b7eSSam Leffler 		 * When using a self-linked beacon descriptor in
3494c42a7b7eSSam Leffler 		 * ibss mode load it once here.
3495c42a7b7eSSam Leffler 		 */
3496c42a7b7eSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_hasveol)
3497b032f27cSSam Leffler 			ath_beacon_start_adhoc(sc, vap);
34985591b213SSam Leffler 	}
349980d939bfSSam Leffler 	sc->sc_syncbeacon = 0;
350080767531SAdrian Chadd 	ieee80211_free_node(ni);
350180d939bfSSam Leffler #undef FUDGE
35028371372bSSam Leffler #undef TSF_TO_TU
35035591b213SSam Leffler }
35045591b213SSam Leffler 
35055591b213SSam Leffler static void
35065591b213SSam Leffler ath_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
35075591b213SSam Leffler {
35085591b213SSam Leffler 	bus_addr_t *paddr = (bus_addr_t*) arg;
3509d77367bfSSam Leffler 	KASSERT(error == 0, ("error %u on bus_dma callback", error));
35105591b213SSam Leffler 	*paddr = segs->ds_addr;
35115591b213SSam Leffler }
35125591b213SSam Leffler 
35135591b213SSam Leffler static int
3514c42a7b7eSSam Leffler ath_descdma_setup(struct ath_softc *sc,
3515c42a7b7eSSam Leffler 	struct ath_descdma *dd, ath_bufhead *head,
3516c42a7b7eSSam Leffler 	const char *name, int nbuf, int ndesc)
3517c42a7b7eSSam Leffler {
3518c42a7b7eSSam Leffler #define	DS2PHYS(_dd, _ds) \
3519c42a7b7eSSam Leffler 	((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
352045abcd6cSAdrian Chadd #define	ATH_DESC_4KB_BOUND_CHECK(_daddr, _len) \
352145abcd6cSAdrian Chadd 	((((u_int32_t)(_daddr) & 0xFFF) > (0x1000 - (_len))) ? 1 : 0)
3522fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
352345abcd6cSAdrian Chadd 	uint8_t *ds;
3524c42a7b7eSSam Leffler 	struct ath_buf *bf;
3525c42a7b7eSSam Leffler 	int i, bsize, error;
352645abcd6cSAdrian Chadd 	int desc_len;
352745abcd6cSAdrian Chadd 
352845abcd6cSAdrian Chadd 	desc_len = sizeof(struct ath_desc);
3529c42a7b7eSSam Leffler 
3530c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA: %u buffers %u desc/buf\n",
3531c42a7b7eSSam Leffler 	    __func__, name, nbuf, ndesc);
3532c42a7b7eSSam Leffler 
3533c42a7b7eSSam Leffler 	dd->dd_name = name;
353445abcd6cSAdrian Chadd 	dd->dd_desc_len = desc_len * nbuf * ndesc;
353545abcd6cSAdrian Chadd 
353645abcd6cSAdrian Chadd 	/*
353745abcd6cSAdrian Chadd 	 * Merlin work-around:
353845abcd6cSAdrian Chadd 	 * Descriptors that cross the 4KB boundary can't be used.
353945abcd6cSAdrian Chadd 	 * Assume one skipped descriptor per 4KB page.
354045abcd6cSAdrian Chadd 	 */
354145abcd6cSAdrian Chadd 	if (! ath_hal_split4ktrans(sc->sc_ah)) {
354245abcd6cSAdrian Chadd 		int numdescpage = 4096 / (desc_len * ndesc);
354345abcd6cSAdrian Chadd 		dd->dd_desc_len = (nbuf / numdescpage + 1) * 4096;
354445abcd6cSAdrian Chadd 	}
3545c42a7b7eSSam Leffler 
3546c42a7b7eSSam Leffler 	/*
3547c42a7b7eSSam Leffler 	 * Setup DMA descriptor area.
3548c42a7b7eSSam Leffler 	 */
3549c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev),	/* parent */
3550c42a7b7eSSam Leffler 		       PAGE_SIZE, 0,		/* alignment, bounds */
3551c42a7b7eSSam Leffler 		       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
3552c42a7b7eSSam Leffler 		       BUS_SPACE_MAXADDR,	/* highaddr */
3553c42a7b7eSSam Leffler 		       NULL, NULL,		/* filter, filterarg */
3554c42a7b7eSSam Leffler 		       dd->dd_desc_len,		/* maxsize */
3555c42a7b7eSSam Leffler 		       1,			/* nsegments */
35566ccb8ea7SSam Leffler 		       dd->dd_desc_len,		/* maxsegsize */
3557c42a7b7eSSam Leffler 		       BUS_DMA_ALLOCNOW,	/* flags */
3558c42a7b7eSSam Leffler 		       NULL,			/* lockfunc */
3559c42a7b7eSSam Leffler 		       NULL,			/* lockarg */
3560c42a7b7eSSam Leffler 		       &dd->dd_dmat);
3561c42a7b7eSSam Leffler 	if (error != 0) {
3562c42a7b7eSSam Leffler 		if_printf(ifp, "cannot allocate %s DMA tag\n", dd->dd_name);
3563c42a7b7eSSam Leffler 		return error;
3564c42a7b7eSSam Leffler 	}
3565c42a7b7eSSam Leffler 
3566c42a7b7eSSam Leffler 	/* allocate descriptors */
3567c42a7b7eSSam Leffler 	error = bus_dmamap_create(dd->dd_dmat, BUS_DMA_NOWAIT, &dd->dd_dmamap);
3568c42a7b7eSSam Leffler 	if (error != 0) {
3569c42a7b7eSSam Leffler 		if_printf(ifp, "unable to create dmamap for %s descriptors, "
3570c42a7b7eSSam Leffler 			"error %u\n", dd->dd_name, error);
3571c42a7b7eSSam Leffler 		goto fail0;
3572c42a7b7eSSam Leffler 	}
3573c42a7b7eSSam Leffler 
3574c42a7b7eSSam Leffler 	error = bus_dmamem_alloc(dd->dd_dmat, (void**) &dd->dd_desc,
35750553a01fSSam Leffler 				 BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
35760553a01fSSam Leffler 				 &dd->dd_dmamap);
3577c42a7b7eSSam Leffler 	if (error != 0) {
3578c42a7b7eSSam Leffler 		if_printf(ifp, "unable to alloc memory for %u %s descriptors, "
3579c42a7b7eSSam Leffler 			"error %u\n", nbuf * ndesc, dd->dd_name, error);
3580c42a7b7eSSam Leffler 		goto fail1;
3581c42a7b7eSSam Leffler 	}
3582c42a7b7eSSam Leffler 
3583c42a7b7eSSam Leffler 	error = bus_dmamap_load(dd->dd_dmat, dd->dd_dmamap,
3584c42a7b7eSSam Leffler 				dd->dd_desc, dd->dd_desc_len,
3585c42a7b7eSSam Leffler 				ath_load_cb, &dd->dd_desc_paddr,
3586c42a7b7eSSam Leffler 				BUS_DMA_NOWAIT);
3587c42a7b7eSSam Leffler 	if (error != 0) {
3588c42a7b7eSSam Leffler 		if_printf(ifp, "unable to map %s descriptors, error %u\n",
3589c42a7b7eSSam Leffler 			dd->dd_name, error);
3590c42a7b7eSSam Leffler 		goto fail2;
3591c42a7b7eSSam Leffler 	}
3592c42a7b7eSSam Leffler 
359345abcd6cSAdrian Chadd 	ds = (uint8_t *) dd->dd_desc;
3594c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA map: %p (%lu) -> %p (%lu)\n",
3595c42a7b7eSSam Leffler 	    __func__, dd->dd_name, ds, (u_long) dd->dd_desc_len,
3596c42a7b7eSSam Leffler 	    (caddr_t) dd->dd_desc_paddr, /*XXX*/ (u_long) dd->dd_desc_len);
3597c42a7b7eSSam Leffler 
3598ebecf802SSam Leffler 	/* allocate rx buffers */
3599c42a7b7eSSam Leffler 	bsize = sizeof(struct ath_buf) * nbuf;
3600c42a7b7eSSam Leffler 	bf = malloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO);
3601c42a7b7eSSam Leffler 	if (bf == NULL) {
3602c42a7b7eSSam Leffler 		if_printf(ifp, "malloc of %s buffers failed, size %u\n",
3603c42a7b7eSSam Leffler 			dd->dd_name, bsize);
3604c42a7b7eSSam Leffler 		goto fail3;
3605c42a7b7eSSam Leffler 	}
3606c42a7b7eSSam Leffler 	dd->dd_bufptr = bf;
3607c42a7b7eSSam Leffler 
36086b349e5aSAdrian Chadd 	TAILQ_INIT(head);
360945abcd6cSAdrian Chadd 	for (i = 0; i < nbuf; i++, bf++, ds += (ndesc * desc_len)) {
361045abcd6cSAdrian Chadd 		bf->bf_desc = (struct ath_desc *) ds;
3611c42a7b7eSSam Leffler 		bf->bf_daddr = DS2PHYS(dd, ds);
361245abcd6cSAdrian Chadd 		if (! ath_hal_split4ktrans(sc->sc_ah)) {
361345abcd6cSAdrian Chadd 			/*
361445abcd6cSAdrian Chadd 			 * Merlin WAR: Skip descriptor addresses which
361545abcd6cSAdrian Chadd 			 * cause 4KB boundary crossing along any point
361645abcd6cSAdrian Chadd 			 * in the descriptor.
361745abcd6cSAdrian Chadd 			 */
361845abcd6cSAdrian Chadd 			 if (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr,
361945abcd6cSAdrian Chadd 			     desc_len * ndesc)) {
362045abcd6cSAdrian Chadd 				/* Start at the next page */
362145abcd6cSAdrian Chadd 				ds += 0x1000 - (bf->bf_daddr & 0xFFF);
362245abcd6cSAdrian Chadd 				bf->bf_desc = (struct ath_desc *) ds;
362345abcd6cSAdrian Chadd 				bf->bf_daddr = DS2PHYS(dd, ds);
362445abcd6cSAdrian Chadd 			}
362545abcd6cSAdrian Chadd 		}
3626c42a7b7eSSam Leffler 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
3627c42a7b7eSSam Leffler 				&bf->bf_dmamap);
3628c42a7b7eSSam Leffler 		if (error != 0) {
3629c42a7b7eSSam Leffler 			if_printf(ifp, "unable to create dmamap for %s "
3630c42a7b7eSSam Leffler 				"buffer %u, error %u\n", dd->dd_name, i, error);
3631c42a7b7eSSam Leffler 			ath_descdma_cleanup(sc, dd, head);
3632c42a7b7eSSam Leffler 			return error;
3633c42a7b7eSSam Leffler 		}
36346edf1dc7SAdrian Chadd 		bf->bf_lastds = bf->bf_desc;	/* Just an initial value */
36356b349e5aSAdrian Chadd 		TAILQ_INSERT_TAIL(head, bf, bf_list);
3636c42a7b7eSSam Leffler 	}
3637c42a7b7eSSam Leffler 	return 0;
3638c42a7b7eSSam Leffler fail3:
3639c42a7b7eSSam Leffler 	bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
3640c42a7b7eSSam Leffler fail2:
3641c42a7b7eSSam Leffler 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3642c42a7b7eSSam Leffler fail1:
3643c42a7b7eSSam Leffler 	bus_dmamap_destroy(dd->dd_dmat, dd->dd_dmamap);
3644c42a7b7eSSam Leffler fail0:
3645c42a7b7eSSam Leffler 	bus_dma_tag_destroy(dd->dd_dmat);
3646c42a7b7eSSam Leffler 	memset(dd, 0, sizeof(*dd));
3647c42a7b7eSSam Leffler 	return error;
3648c42a7b7eSSam Leffler #undef DS2PHYS
364945abcd6cSAdrian Chadd #undef ATH_DESC_4KB_BOUND_CHECK
3650c42a7b7eSSam Leffler }
3651c42a7b7eSSam Leffler 
3652c42a7b7eSSam Leffler static void
3653c42a7b7eSSam Leffler ath_descdma_cleanup(struct ath_softc *sc,
3654c42a7b7eSSam Leffler 	struct ath_descdma *dd, ath_bufhead *head)
3655c42a7b7eSSam Leffler {
3656c42a7b7eSSam Leffler 	struct ath_buf *bf;
3657c42a7b7eSSam Leffler 	struct ieee80211_node *ni;
3658c42a7b7eSSam Leffler 
3659c42a7b7eSSam Leffler 	bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
3660c42a7b7eSSam Leffler 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3661c42a7b7eSSam Leffler 	bus_dmamap_destroy(dd->dd_dmat, dd->dd_dmamap);
3662c42a7b7eSSam Leffler 	bus_dma_tag_destroy(dd->dd_dmat);
3663c42a7b7eSSam Leffler 
36646b349e5aSAdrian Chadd 	TAILQ_FOREACH(bf, head, bf_list) {
3665c42a7b7eSSam Leffler 		if (bf->bf_m) {
3666c42a7b7eSSam Leffler 			m_freem(bf->bf_m);
3667c42a7b7eSSam Leffler 			bf->bf_m = NULL;
3668c42a7b7eSSam Leffler 		}
3669c42a7b7eSSam Leffler 		if (bf->bf_dmamap != NULL) {
3670c42a7b7eSSam Leffler 			bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
3671c42a7b7eSSam Leffler 			bf->bf_dmamap = NULL;
3672c42a7b7eSSam Leffler 		}
3673c42a7b7eSSam Leffler 		ni = bf->bf_node;
3674c42a7b7eSSam Leffler 		bf->bf_node = NULL;
3675c42a7b7eSSam Leffler 		if (ni != NULL) {
3676c42a7b7eSSam Leffler 			/*
3677c42a7b7eSSam Leffler 			 * Reclaim node reference.
3678c42a7b7eSSam Leffler 			 */
3679c42a7b7eSSam Leffler 			ieee80211_free_node(ni);
3680c42a7b7eSSam Leffler 		}
3681c42a7b7eSSam Leffler 	}
3682c42a7b7eSSam Leffler 
36836b349e5aSAdrian Chadd 	TAILQ_INIT(head);
3684c42a7b7eSSam Leffler 	free(dd->dd_bufptr, M_ATHDEV);
3685c42a7b7eSSam Leffler 	memset(dd, 0, sizeof(*dd));
3686c42a7b7eSSam Leffler }
3687c42a7b7eSSam Leffler 
3688c42a7b7eSSam Leffler static int
36895591b213SSam Leffler ath_desc_alloc(struct ath_softc *sc)
36905591b213SSam Leffler {
3691c42a7b7eSSam Leffler 	int error;
36925591b213SSam Leffler 
3693c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_rxdma, &sc->sc_rxbuf,
3694e2d787faSSam Leffler 			"rx", ath_rxbuf, 1);
36955591b213SSam Leffler 	if (error != 0)
36965591b213SSam Leffler 		return error;
36975591b213SSam Leffler 
3698c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_txdma, &sc->sc_txbuf,
3699e2d787faSSam Leffler 			"tx", ath_txbuf, ATH_TXDESC);
3700c42a7b7eSSam Leffler 	if (error != 0) {
3701c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
37025591b213SSam Leffler 		return error;
3703c42a7b7eSSam Leffler 	}
3704c42a7b7eSSam Leffler 
3705c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_bdma, &sc->sc_bbuf,
3706b032f27cSSam Leffler 			"beacon", ATH_BCBUF, 1);
3707c42a7b7eSSam Leffler 	if (error != 0) {
3708c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3709c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
3710c42a7b7eSSam Leffler 		return error;
3711c42a7b7eSSam Leffler 	}
37125591b213SSam Leffler 	return 0;
37135591b213SSam Leffler }
37145591b213SSam Leffler 
37155591b213SSam Leffler static void
37165591b213SSam Leffler ath_desc_free(struct ath_softc *sc)
37175591b213SSam Leffler {
37185591b213SSam Leffler 
3719c42a7b7eSSam Leffler 	if (sc->sc_bdma.dd_desc_len != 0)
3720c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_bdma, &sc->sc_bbuf);
3721c42a7b7eSSam Leffler 	if (sc->sc_txdma.dd_desc_len != 0)
3722c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3723c42a7b7eSSam Leffler 	if (sc->sc_rxdma.dd_desc_len != 0)
3724c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
37255591b213SSam Leffler }
37265591b213SSam Leffler 
37275591b213SSam Leffler static struct ieee80211_node *
372838c208f8SSam Leffler ath_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
37295591b213SSam Leffler {
373038c208f8SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
3731c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
3732c42a7b7eSSam Leffler 	const size_t space = sizeof(struct ath_node) + sc->sc_rc->arc_space;
3733c42a7b7eSSam Leffler 	struct ath_node *an;
3734c42a7b7eSSam Leffler 
3735c42a7b7eSSam Leffler 	an = malloc(space, M_80211_NODE, M_NOWAIT|M_ZERO);
3736c42a7b7eSSam Leffler 	if (an == NULL) {
3737c42a7b7eSSam Leffler 		/* XXX stat+msg */
3738de5af704SSam Leffler 		return NULL;
37395591b213SSam Leffler 	}
3740c42a7b7eSSam Leffler 	ath_rate_node_init(sc, an);
37415591b213SSam Leffler 
37423dd85b26SAdrian Chadd 	/* Setup the mutex - there's no associd yet so set the name to NULL */
37433dd85b26SAdrian Chadd 	snprintf(an->an_name, sizeof(an->an_name), "%s: node %p",
37443dd85b26SAdrian Chadd 	    device_get_nameunit(sc->sc_dev), an);
37453dd85b26SAdrian Chadd 	mtx_init(&an->an_mtx, an->an_name, NULL, MTX_DEF);
37463dd85b26SAdrian Chadd 
3747eb6f0de0SAdrian Chadd 	/* XXX setup ath_tid */
3748eb6f0de0SAdrian Chadd 	ath_tx_tid_init(sc, an);
3749eb6f0de0SAdrian Chadd 
3750c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: an %p\n", __func__, an);
3751c42a7b7eSSam Leffler 	return &an->an_node;
3752c42a7b7eSSam Leffler }
3753c42a7b7eSSam Leffler 
37545591b213SSam Leffler static void
37554afa805eSAdrian Chadd ath_node_cleanup(struct ieee80211_node *ni)
37564afa805eSAdrian Chadd {
37574afa805eSAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
37584afa805eSAdrian Chadd 	struct ath_softc *sc = ic->ic_ifp->if_softc;
37594afa805eSAdrian Chadd 
37604afa805eSAdrian Chadd 	/* Cleanup ath_tid, free unused bufs, unlink bufs in TXQ */
3761eb6f0de0SAdrian Chadd 	ath_tx_node_flush(sc, ATH_NODE(ni));
37624afa805eSAdrian Chadd 	ath_rate_node_cleanup(sc, ATH_NODE(ni));
37634afa805eSAdrian Chadd 	sc->sc_node_cleanup(ni);
37644afa805eSAdrian Chadd }
37654afa805eSAdrian Chadd 
37664afa805eSAdrian Chadd static void
3767c42a7b7eSSam Leffler ath_node_free(struct ieee80211_node *ni)
37685591b213SSam Leffler {
3769c42a7b7eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
3770c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
37711e774079SSam Leffler 
3772c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: ni %p\n", __func__, ni);
37733dd85b26SAdrian Chadd 	mtx_destroy(&ATH_NODE(ni)->an_mtx);
3774c42a7b7eSSam Leffler 	sc->sc_node_free(ni);
37755591b213SSam Leffler }
37765591b213SSam Leffler 
377768e8e04eSSam Leffler static void
377868e8e04eSSam Leffler ath_node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
377968e8e04eSSam Leffler {
378068e8e04eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
378168e8e04eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
378268e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
378368e8e04eSSam Leffler 
3784b032f27cSSam Leffler 	*rssi = ic->ic_node_getrssi(ni);
378559efa8b5SSam Leffler 	if (ni->ni_chan != IEEE80211_CHAN_ANYC)
378659efa8b5SSam Leffler 		*noise = ath_hal_getchannoise(ah, ni->ni_chan);
378759efa8b5SSam Leffler 	else
378868e8e04eSSam Leffler 		*noise = -95;		/* nominally correct */
378968e8e04eSSam Leffler }
379068e8e04eSSam Leffler 
37915591b213SSam Leffler static int
37925591b213SSam Leffler ath_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf)
37935591b213SSam Leffler {
37945591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
37955591b213SSam Leffler 	int error;
37965591b213SSam Leffler 	struct mbuf *m;
37975591b213SSam Leffler 	struct ath_desc *ds;
37985591b213SSam Leffler 
37995591b213SSam Leffler 	m = bf->bf_m;
38005591b213SSam Leffler 	if (m == NULL) {
38015591b213SSam Leffler 		/*
38025591b213SSam Leffler 		 * NB: by assigning a page to the rx dma buffer we
38035591b213SSam Leffler 		 * implicitly satisfy the Atheros requirement that
38045591b213SSam Leffler 		 * this buffer be cache-line-aligned and sized to be
38055591b213SSam Leffler 		 * multiple of the cache line size.  Not doing this
38065591b213SSam Leffler 		 * causes weird stuff to happen (for the 5210 at least).
38075591b213SSam Leffler 		 */
38085591b213SSam Leffler 		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
38095591b213SSam Leffler 		if (m == NULL) {
3810c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
3811c42a7b7eSSam Leffler 				"%s: no mbuf/cluster\n", __func__);
38125591b213SSam Leffler 			sc->sc_stats.ast_rx_nombuf++;
38135591b213SSam Leffler 			return ENOMEM;
38145591b213SSam Leffler 		}
38155591b213SSam Leffler 		m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
38165591b213SSam Leffler 
3817f9e6219bSSam Leffler 		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat,
3818c42a7b7eSSam Leffler 					     bf->bf_dmamap, m,
3819f9e6219bSSam Leffler 					     bf->bf_segs, &bf->bf_nseg,
38205591b213SSam Leffler 					     BUS_DMA_NOWAIT);
38215591b213SSam Leffler 		if (error != 0) {
3822c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
3823f9e6219bSSam Leffler 			    "%s: bus_dmamap_load_mbuf_sg failed; error %d\n",
3824c42a7b7eSSam Leffler 			    __func__, error);
38255591b213SSam Leffler 			sc->sc_stats.ast_rx_busdma++;
3826b2792ff6SSam Leffler 			m_freem(m);
38275591b213SSam Leffler 			return error;
38285591b213SSam Leffler 		}
3829d77367bfSSam Leffler 		KASSERT(bf->bf_nseg == 1,
3830d77367bfSSam Leffler 			("multi-segment packet; nseg %u", bf->bf_nseg));
3831b2792ff6SSam Leffler 		bf->bf_m = m;
38325591b213SSam Leffler 	}
38335591b213SSam Leffler 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREREAD);
38345591b213SSam Leffler 
383504e22a02SSam Leffler 	/*
383604e22a02SSam Leffler 	 * Setup descriptors.  For receive we always terminate
383704e22a02SSam Leffler 	 * the descriptor list with a self-linked entry so we'll
383804e22a02SSam Leffler 	 * not get overrun under high load (as can happen with a
3839c42a7b7eSSam Leffler 	 * 5212 when ANI processing enables PHY error frames).
384004e22a02SSam Leffler 	 *
384104e22a02SSam Leffler 	 * To insure the last descriptor is self-linked we create
384204e22a02SSam Leffler 	 * each descriptor as self-linked and add it to the end.  As
384304e22a02SSam Leffler 	 * each additional descriptor is added the previous self-linked
384404e22a02SSam Leffler 	 * entry is ``fixed'' naturally.  This should be safe even
384504e22a02SSam Leffler 	 * if DMA is happening.  When processing RX interrupts we
384604e22a02SSam Leffler 	 * never remove/process the last, self-linked, entry on the
384704e22a02SSam Leffler 	 * descriptor list.  This insures the hardware always has
384804e22a02SSam Leffler 	 * someplace to write a new frame.
384904e22a02SSam Leffler 	 */
38508a2a6beeSAdrian Chadd 	/*
38518a2a6beeSAdrian Chadd 	 * 11N: we can no longer afford to self link the last descriptor.
38528a2a6beeSAdrian Chadd 	 * MAC acknowledges BA status as long as it copies frames to host
38538a2a6beeSAdrian Chadd 	 * buffer (or rx fifo). This can incorrectly acknowledge packets
38548a2a6beeSAdrian Chadd 	 * to a sender if last desc is self-linked.
38558a2a6beeSAdrian Chadd 	 */
38565591b213SSam Leffler 	ds = bf->bf_desc;
38578a2a6beeSAdrian Chadd 	if (sc->sc_rxslink)
385804e22a02SSam Leffler 		ds->ds_link = bf->bf_daddr;	/* link to self */
38598a2a6beeSAdrian Chadd 	else
38608a2a6beeSAdrian Chadd 		ds->ds_link = 0;		/* terminate the list */
38615591b213SSam Leffler 	ds->ds_data = bf->bf_segs[0].ds_addr;
38625591b213SSam Leffler 	ath_hal_setuprxdesc(ah, ds
38635591b213SSam Leffler 		, m->m_len		/* buffer size */
38645591b213SSam Leffler 		, 0
38655591b213SSam Leffler 	);
38665591b213SSam Leffler 
38675591b213SSam Leffler 	if (sc->sc_rxlink != NULL)
38685591b213SSam Leffler 		*sc->sc_rxlink = bf->bf_daddr;
38695591b213SSam Leffler 	sc->sc_rxlink = &ds->ds_link;
38705591b213SSam Leffler 	return 0;
38715591b213SSam Leffler }
38725591b213SSam Leffler 
3873c42a7b7eSSam Leffler /*
387403ed599aSSam Leffler  * Extend 15-bit time stamp from rx descriptor to
38757b0c77ecSSam Leffler  * a full 64-bit TSF using the specified TSF.
387603ed599aSSam Leffler  */
387703ed599aSSam Leffler static __inline u_int64_t
3878fc4de9b7SAdrian Chadd ath_extend_tsf15(u_int32_t rstamp, u_int64_t tsf)
387903ed599aSSam Leffler {
388003ed599aSSam Leffler 	if ((tsf & 0x7fff) < rstamp)
388103ed599aSSam Leffler 		tsf -= 0x8000;
3882fc4de9b7SAdrian Chadd 
388303ed599aSSam Leffler 	return ((tsf &~ 0x7fff) | rstamp);
388403ed599aSSam Leffler }
388503ed599aSSam Leffler 
388603ed599aSSam Leffler /*
3887fc4de9b7SAdrian Chadd  * Extend 32-bit time stamp from rx descriptor to
3888fc4de9b7SAdrian Chadd  * a full 64-bit TSF using the specified TSF.
3889fc4de9b7SAdrian Chadd  */
3890fc4de9b7SAdrian Chadd static __inline u_int64_t
3891fc4de9b7SAdrian Chadd ath_extend_tsf32(u_int32_t rstamp, u_int64_t tsf)
3892fc4de9b7SAdrian Chadd {
3893fc4de9b7SAdrian Chadd 	u_int32_t tsf_low = tsf & 0xffffffff;
3894fc4de9b7SAdrian Chadd 	u_int64_t tsf64 = (tsf & ~0xffffffffULL) | rstamp;
3895fc4de9b7SAdrian Chadd 
3896fc4de9b7SAdrian Chadd 	if (rstamp > tsf_low && (rstamp - tsf_low > 0x10000000))
3897fc4de9b7SAdrian Chadd 		tsf64 -= 0x100000000ULL;
3898fc4de9b7SAdrian Chadd 
3899fc4de9b7SAdrian Chadd 	if (rstamp < tsf_low && (tsf_low - rstamp > 0x10000000))
3900fc4de9b7SAdrian Chadd 		tsf64 += 0x100000000ULL;
3901fc4de9b7SAdrian Chadd 
3902fc4de9b7SAdrian Chadd 	return tsf64;
3903fc4de9b7SAdrian Chadd }
3904fc4de9b7SAdrian Chadd 
3905fc4de9b7SAdrian Chadd /*
3906fc4de9b7SAdrian Chadd  * Extend the TSF from the RX descriptor to a full 64 bit TSF.
3907fc4de9b7SAdrian Chadd  * Earlier hardware versions only wrote the low 15 bits of the
3908fc4de9b7SAdrian Chadd  * TSF into the RX descriptor; later versions (AR5416 and up)
3909fc4de9b7SAdrian Chadd  * include the 32 bit TSF value.
3910fc4de9b7SAdrian Chadd  */
3911fc4de9b7SAdrian Chadd static __inline u_int64_t
3912fc4de9b7SAdrian Chadd ath_extend_tsf(struct ath_softc *sc, u_int32_t rstamp, u_int64_t tsf)
3913fc4de9b7SAdrian Chadd {
3914fc4de9b7SAdrian Chadd 	if (sc->sc_rxtsf32)
3915fc4de9b7SAdrian Chadd 		return ath_extend_tsf32(rstamp, tsf);
3916fc4de9b7SAdrian Chadd 	else
3917fc4de9b7SAdrian Chadd 		return ath_extend_tsf15(rstamp, tsf);
3918fc4de9b7SAdrian Chadd }
3919fc4de9b7SAdrian Chadd 
3920fc4de9b7SAdrian Chadd /*
3921c42a7b7eSSam Leffler  * Intercept management frames to collect beacon rssi data
3922c42a7b7eSSam Leffler  * and to do ibss merges.
3923c42a7b7eSSam Leffler  */
3924c42a7b7eSSam Leffler static void
3925b032f27cSSam Leffler ath_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
39265463c4a4SSam Leffler 	int subtype, int rssi, int nf)
3927c42a7b7eSSam Leffler {
3928b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
3929b032f27cSSam Leffler 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
3930c42a7b7eSSam Leffler 
3931c42a7b7eSSam Leffler 	/*
3932c42a7b7eSSam Leffler 	 * Call up first so subsequent work can use information
3933c42a7b7eSSam Leffler 	 * potentially stored in the node (e.g. for ibss merge).
3934c42a7b7eSSam Leffler 	 */
39355463c4a4SSam Leffler 	ATH_VAP(vap)->av_recv_mgmt(ni, m, subtype, rssi, nf);
3936c42a7b7eSSam Leffler 	switch (subtype) {
3937c42a7b7eSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON:
3938c42a7b7eSSam Leffler 		/* update rssi statistics for use by the hal */
393980767531SAdrian Chadd 		/* XXX unlocked check against vap->iv_bss? */
3940ffa2cab6SSam Leffler 		ATH_RSSI_LPF(sc->sc_halstats.ns_avgbrssi, rssi);
394180d939bfSSam Leffler 		if (sc->sc_syncbeacon &&
3942b032f27cSSam Leffler 		    ni == vap->iv_bss && vap->iv_state == IEEE80211_S_RUN) {
394380d939bfSSam Leffler 			/*
394480d939bfSSam Leffler 			 * Resync beacon timers using the tsf of the beacon
394580d939bfSSam Leffler 			 * frame we just received.
394680d939bfSSam Leffler 			 */
3947b032f27cSSam Leffler 			ath_beacon_config(sc, vap);
394880d939bfSSam Leffler 		}
3949c42a7b7eSSam Leffler 		/* fall thru... */
3950c42a7b7eSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
3951b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_IBSS &&
3952b032f27cSSam Leffler 		    vap->iv_state == IEEE80211_S_RUN) {
39537041d50cSBernhard Schmidt 			uint32_t rstamp = sc->sc_lastrs->rs_tstamp;
3954fc4de9b7SAdrian Chadd 			uint64_t tsf = ath_extend_tsf(sc, rstamp,
39557b0c77ecSSam Leffler 				ath_hal_gettsf64(sc->sc_ah));
3956c42a7b7eSSam Leffler 			/*
3957c42a7b7eSSam Leffler 			 * Handle ibss merge as needed; check the tsf on the
3958c42a7b7eSSam Leffler 			 * frame before attempting the merge.  The 802.11 spec
3959c42a7b7eSSam Leffler 			 * says the station should change it's bssid to match
3960c42a7b7eSSam Leffler 			 * the oldest station with the same ssid, where oldest
3961f818612bSSam Leffler 			 * is determined by the tsf.  Note that hardware
3962f818612bSSam Leffler 			 * reconfiguration happens through callback to
396303ed599aSSam Leffler 			 * ath_newstate as the state machine will go from
396403ed599aSSam Leffler 			 * RUN -> RUN when this happens.
3965c42a7b7eSSam Leffler 			 */
396603ed599aSSam Leffler 			if (le64toh(ni->ni_tstamp.tsf) >= tsf) {
396703ed599aSSam Leffler 				DPRINTF(sc, ATH_DEBUG_STATE,
396833d7d80cSTai-hwa Liang 				    "ibss merge, rstamp %u tsf %ju "
396933d7d80cSTai-hwa Liang 				    "tstamp %ju\n", rstamp, (uintmax_t)tsf,
397033d7d80cSTai-hwa Liang 				    (uintmax_t)ni->ni_tstamp.tsf);
3971641b4d0bSSam Leffler 				(void) ieee80211_ibss_merge(ni);
3972c42a7b7eSSam Leffler 			}
397303ed599aSSam Leffler 		}
3974c42a7b7eSSam Leffler 		break;
3975c42a7b7eSSam Leffler 	}
3976c42a7b7eSSam Leffler }
3977c42a7b7eSSam Leffler 
3978c42a7b7eSSam Leffler /*
3979c42a7b7eSSam Leffler  * Set the default antenna.
3980c42a7b7eSSam Leffler  */
3981c42a7b7eSSam Leffler static void
3982c42a7b7eSSam Leffler ath_setdefantenna(struct ath_softc *sc, u_int antenna)
3983c42a7b7eSSam Leffler {
3984c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3985c42a7b7eSSam Leffler 
3986c42a7b7eSSam Leffler 	/* XXX block beacon interrupts */
3987c42a7b7eSSam Leffler 	ath_hal_setdefantenna(ah, antenna);
3988c42a7b7eSSam Leffler 	if (sc->sc_defant != antenna)
3989c42a7b7eSSam Leffler 		sc->sc_stats.ast_ant_defswitch++;
3990c42a7b7eSSam Leffler 	sc->sc_defant = antenna;
3991c42a7b7eSSam Leffler 	sc->sc_rxotherant = 0;
3992c42a7b7eSSam Leffler }
3993c42a7b7eSSam Leffler 
39945463c4a4SSam Leffler static void
3995b032f27cSSam Leffler ath_rx_tap(struct ifnet *ifp, struct mbuf *m,
399665f9edeeSSam Leffler 	const struct ath_rx_status *rs, u_int64_t tsf, int16_t nf)
39977b0c77ecSSam Leffler {
3998e387d629SSam Leffler #define	CHAN_HT20	htole32(IEEE80211_CHAN_HT20)
3999e387d629SSam Leffler #define	CHAN_HT40U	htole32(IEEE80211_CHAN_HT40U)
4000e387d629SSam Leffler #define	CHAN_HT40D	htole32(IEEE80211_CHAN_HT40D)
400146d4d74cSSam Leffler #define	CHAN_HT		(CHAN_HT20|CHAN_HT40U|CHAN_HT40D)
4002b032f27cSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
400346d4d74cSSam Leffler 	const HAL_RATE_TABLE *rt;
400446d4d74cSSam Leffler 	uint8_t rix;
40057b0c77ecSSam Leffler 
400646d4d74cSSam Leffler 	rt = sc->sc_currates;
400746d4d74cSSam Leffler 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
400846d4d74cSSam Leffler 	rix = rt->rateCodeToIndex[rs->rs_rate];
400968e8e04eSSam Leffler 	sc->sc_rx_th.wr_rate = sc->sc_hwmap[rix].ieeerate;
40107b0c77ecSSam Leffler 	sc->sc_rx_th.wr_flags = sc->sc_hwmap[rix].rxflags;
401146d4d74cSSam Leffler #ifdef AH_SUPPORT_AR5416
4012e387d629SSam Leffler 	sc->sc_rx_th.wr_chan_flags &= ~CHAN_HT;
401346d4d74cSSam Leffler 	if (sc->sc_rx_th.wr_rate & IEEE80211_RATE_MCS) {	/* HT rate */
401459efa8b5SSam Leffler 		struct ieee80211com *ic = ifp->if_l2com;
401559efa8b5SSam Leffler 
4016e387d629SSam Leffler 		if ((rs->rs_flags & HAL_RX_2040) == 0)
4017e387d629SSam Leffler 			sc->sc_rx_th.wr_chan_flags |= CHAN_HT20;
401859efa8b5SSam Leffler 		else if (IEEE80211_IS_CHAN_HT40U(ic->ic_curchan))
4019e387d629SSam Leffler 			sc->sc_rx_th.wr_chan_flags |= CHAN_HT40U;
4020e387d629SSam Leffler 		else
4021e387d629SSam Leffler 			sc->sc_rx_th.wr_chan_flags |= CHAN_HT40D;
402268e8e04eSSam Leffler 		if ((rs->rs_flags & HAL_RX_GI) == 0)
4023e387d629SSam Leffler 			sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
402468e8e04eSSam Leffler 	}
402568e8e04eSSam Leffler #endif
4026fc4de9b7SAdrian Chadd 	sc->sc_rx_th.wr_tsf = htole64(ath_extend_tsf(sc, rs->rs_tstamp, tsf));
402765f9edeeSSam Leffler 	if (rs->rs_status & HAL_RXERR_CRC)
40287b0c77ecSSam Leffler 		sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
40297b0c77ecSSam Leffler 	/* XXX propagate other error flags from descriptor */
40307b0c77ecSSam Leffler 	sc->sc_rx_th.wr_antnoise = nf;
40315463c4a4SSam Leffler 	sc->sc_rx_th.wr_antsignal = nf + rs->rs_rssi;
403265f9edeeSSam Leffler 	sc->sc_rx_th.wr_antenna = rs->rs_antenna;
403346d4d74cSSam Leffler #undef CHAN_HT
4034e387d629SSam Leffler #undef CHAN_HT20
4035e387d629SSam Leffler #undef CHAN_HT40U
4036e387d629SSam Leffler #undef CHAN_HT40D
40377b0c77ecSSam Leffler }
40387b0c77ecSSam Leffler 
40395591b213SSam Leffler static void
4040b032f27cSSam Leffler ath_handle_micerror(struct ieee80211com *ic,
4041b032f27cSSam Leffler 	struct ieee80211_frame *wh, int keyix)
4042b032f27cSSam Leffler {
4043b032f27cSSam Leffler 	struct ieee80211_node *ni;
4044b032f27cSSam Leffler 
4045b032f27cSSam Leffler 	/* XXX recheck MIC to deal w/ chips that lie */
4046b032f27cSSam Leffler 	/* XXX discard MIC errors on !data frames */
4047b032f27cSSam Leffler 	ni = ieee80211_find_rxnode(ic, (const struct ieee80211_frame_min *) wh);
4048b032f27cSSam Leffler 	if (ni != NULL) {
4049b032f27cSSam Leffler 		ieee80211_notify_michael_failure(ni->ni_vap, wh, keyix);
4050b032f27cSSam Leffler 		ieee80211_free_node(ni);
4051b032f27cSSam Leffler 	}
4052b032f27cSSam Leffler }
4053b032f27cSSam Leffler 
405496ff485dSAdrian Chadd /*
405596ff485dSAdrian Chadd  * Only run the RX proc if it's not already running.
405696ff485dSAdrian Chadd  * Since this may get run as part of the reset/flush path,
405796ff485dSAdrian Chadd  * the task can't clash with an existing, running tasklet.
405896ff485dSAdrian Chadd  */
4059b032f27cSSam Leffler static void
406096ff485dSAdrian Chadd ath_rx_tasklet(void *arg, int npending)
406196ff485dSAdrian Chadd {
406296ff485dSAdrian Chadd 	struct ath_softc *sc = arg;
406396ff485dSAdrian Chadd 
406496ff485dSAdrian Chadd 	CTR1(ATH_KTR_INTR, "ath_rx_proc: pending=%d", npending);
406596ff485dSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RX_PROC, "%s: pending %u\n", __func__, npending);
4066ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4067ef27340cSAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
4068ef27340cSAdrian Chadd 		device_printf(sc->sc_dev,
4069ef27340cSAdrian Chadd 		    "%s: sc_inreset_cnt > 0; skipping\n", __func__);
4070ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
4071ef27340cSAdrian Chadd 		return;
4072ef27340cSAdrian Chadd 	}
4073ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
407496ff485dSAdrian Chadd 	ath_rx_proc(sc, 1);
407596ff485dSAdrian Chadd }
407696ff485dSAdrian Chadd 
407796ff485dSAdrian Chadd static void
407896ff485dSAdrian Chadd ath_rx_proc(struct ath_softc *sc, int resched)
40795591b213SSam Leffler {
40808cec0ab9SSam Leffler #define	PA2DESC(_sc, _pa) \
4081c42a7b7eSSam Leffler 	((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
4082c42a7b7eSSam Leffler 		((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
40835591b213SSam Leffler 	struct ath_buf *bf;
4084fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
4085b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
40865591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
40875591b213SSam Leffler 	struct ath_desc *ds;
408865f9edeeSSam Leffler 	struct ath_rx_status *rs;
40895591b213SSam Leffler 	struct mbuf *m;
40900a915fadSSam Leffler 	struct ieee80211_node *ni;
4091d7736e13SSam Leffler 	int len, type, ngood;
40925591b213SSam Leffler 	HAL_STATUS status;
40937b0c77ecSSam Leffler 	int16_t nf;
409406fc4a10SAdrian Chadd 	u_int64_t tsf, rstamp;
40958f939e79SAdrian Chadd 	int npkts = 0;
40965591b213SSam Leffler 
4097ef27340cSAdrian Chadd 	/* XXX we must not hold the ATH_LOCK here */
4098ef27340cSAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
4099ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
4100ef27340cSAdrian Chadd 
4101ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4102ef27340cSAdrian Chadd 	sc->sc_rxproc_cnt++;
4103ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
4104ef27340cSAdrian Chadd 
410596ff485dSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RX_PROC, "%s: called\n", __func__);
4106d7736e13SSam Leffler 	ngood = 0;
410759efa8b5SSam Leffler 	nf = ath_hal_getchannoise(ah, sc->sc_curchan);
410884784be1SSam Leffler 	sc->sc_stats.ast_rx_noise = nf;
41097b0c77ecSSam Leffler 	tsf = ath_hal_gettsf64(ah);
41105591b213SSam Leffler 	do {
41116b349e5aSAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_rxbuf);
41128a2a6beeSAdrian Chadd 		if (sc->sc_rxslink && bf == NULL) {	/* NB: shouldn't happen */
4113c42a7b7eSSam Leffler 			if_printf(ifp, "%s: no buffer!\n", __func__);
41145591b213SSam Leffler 			break;
41158a2a6beeSAdrian Chadd 		} else if (bf == NULL) {
41168a2a6beeSAdrian Chadd 			/*
41178a2a6beeSAdrian Chadd 			 * End of List:
41188a2a6beeSAdrian Chadd 			 * this can happen for non-self-linked RX chains
41198a2a6beeSAdrian Chadd 			 */
41208a2a6beeSAdrian Chadd 			sc->sc_stats.ast_rx_hitqueueend++;
41218a2a6beeSAdrian Chadd 			break;
41225591b213SSam Leffler 		}
4123b2792ff6SSam Leffler 		m = bf->bf_m;
4124b2792ff6SSam Leffler 		if (m == NULL) {		/* NB: shouldn't happen */
4125b2792ff6SSam Leffler 			/*
4126b2792ff6SSam Leffler 			 * If mbuf allocation failed previously there
4127b2792ff6SSam Leffler 			 * will be no mbuf; try again to re-populate it.
4128b2792ff6SSam Leffler 			 */
4129b2792ff6SSam Leffler 			/* XXX make debug msg */
4130b2792ff6SSam Leffler 			if_printf(ifp, "%s: no mbuf!\n", __func__);
41316b349e5aSAdrian Chadd 			TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
4132b2792ff6SSam Leffler 			goto rx_next;
4133b2792ff6SSam Leffler 		}
413404e22a02SSam Leffler 		ds = bf->bf_desc;
413504e22a02SSam Leffler 		if (ds->ds_link == bf->bf_daddr) {
413604e22a02SSam Leffler 			/* NB: never process the self-linked entry at the end */
4137f77057dbSAdrian Chadd 			sc->sc_stats.ast_rx_hitqueueend++;
413804e22a02SSam Leffler 			break;
413904e22a02SSam Leffler 		}
41408cec0ab9SSam Leffler 		/* XXX sync descriptor memory */
41418cec0ab9SSam Leffler 		/*
41428cec0ab9SSam Leffler 		 * Must provide the virtual address of the current
41438cec0ab9SSam Leffler 		 * descriptor, the physical address, and the virtual
41448cec0ab9SSam Leffler 		 * address of the next descriptor in the h/w chain.
41458cec0ab9SSam Leffler 		 * This allows the HAL to look ahead to see if the
41468cec0ab9SSam Leffler 		 * hardware is done with a descriptor by checking the
41478cec0ab9SSam Leffler 		 * done bit in the following descriptor and the address
41488cec0ab9SSam Leffler 		 * of the current descriptor the DMA engine is working
41498cec0ab9SSam Leffler 		 * on.  All this is necessary because of our use of
41508cec0ab9SSam Leffler 		 * a self-linked list to avoid rx overruns.
41518cec0ab9SSam Leffler 		 */
415265f9edeeSSam Leffler 		rs = &bf->bf_status.ds_rxstat;
41538cec0ab9SSam Leffler 		status = ath_hal_rxprocdesc(ah, ds,
415465f9edeeSSam Leffler 				bf->bf_daddr, PA2DESC(sc, ds->ds_link), rs);
4155a585a9a1SSam Leffler #ifdef ATH_DEBUG
4156c42a7b7eSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_RECV_DESC)
41576902009eSSam Leffler 			ath_printrxbuf(sc, bf, 0, status == HAL_OK);
41585591b213SSam Leffler #endif
41595591b213SSam Leffler 		if (status == HAL_EINPROGRESS)
41605591b213SSam Leffler 			break;
41616b349e5aSAdrian Chadd 
41626b349e5aSAdrian Chadd 		TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
41638f939e79SAdrian Chadd 		npkts++;
4164f9aa1d90SAdrian Chadd 
416506fc4a10SAdrian Chadd 		/*
416606fc4a10SAdrian Chadd 		 * Calculate the correct 64 bit TSF given
416706fc4a10SAdrian Chadd 		 * the TSF64 register value and rs_tstamp.
416806fc4a10SAdrian Chadd 		 */
416906fc4a10SAdrian Chadd 		rstamp = ath_extend_tsf(sc, rs->rs_tstamp, tsf);
417006fc4a10SAdrian Chadd 
4171f9aa1d90SAdrian Chadd 		/* These aren't specifically errors */
41726e0f1168SAdrian Chadd #ifdef	AH_SUPPORT_AR5416
4173f9aa1d90SAdrian Chadd 		if (rs->rs_flags & HAL_RX_GI)
4174f9aa1d90SAdrian Chadd 			sc->sc_stats.ast_rx_halfgi++;
4175f9aa1d90SAdrian Chadd 		if (rs->rs_flags & HAL_RX_2040)
4176f9aa1d90SAdrian Chadd 			sc->sc_stats.ast_rx_2040++;
4177f9aa1d90SAdrian Chadd 		if (rs->rs_flags & HAL_RX_DELIM_CRC_PRE)
4178f9aa1d90SAdrian Chadd 			sc->sc_stats.ast_rx_pre_crc_err++;
4179f9aa1d90SAdrian Chadd 		if (rs->rs_flags & HAL_RX_DELIM_CRC_POST)
4180f9aa1d90SAdrian Chadd 			sc->sc_stats.ast_rx_post_crc_err++;
4181f9aa1d90SAdrian Chadd 		if (rs->rs_flags & HAL_RX_DECRYPT_BUSY)
4182f9aa1d90SAdrian Chadd 			sc->sc_stats.ast_rx_decrypt_busy_err++;
4183f9aa1d90SAdrian Chadd 		if (rs->rs_flags & HAL_RX_HI_RX_CHAIN)
4184f9aa1d90SAdrian Chadd 			sc->sc_stats.ast_rx_hi_rx_chain++;
41856e0f1168SAdrian Chadd #endif /* AH_SUPPORT_AR5416 */
4186f9aa1d90SAdrian Chadd 
418768e8e04eSSam Leffler 		if (rs->rs_status != 0) {
418865f9edeeSSam Leffler 			if (rs->rs_status & HAL_RXERR_CRC)
41895591b213SSam Leffler 				sc->sc_stats.ast_rx_crcerr++;
419065f9edeeSSam Leffler 			if (rs->rs_status & HAL_RXERR_FIFO)
41915591b213SSam Leffler 				sc->sc_stats.ast_rx_fifoerr++;
419265f9edeeSSam Leffler 			if (rs->rs_status & HAL_RXERR_PHY) {
41935591b213SSam Leffler 				sc->sc_stats.ast_rx_phyerr++;
419448237774SAdrian Chadd 				/* Process DFS radar events */
4195373815efSAdrian Chadd 				if ((rs->rs_phyerr == HAL_PHYERR_RADAR) ||
4196373815efSAdrian Chadd 				    (rs->rs_phyerr == HAL_PHYERR_FALSE_RADAR_EXT)) {
4197373815efSAdrian Chadd 					/* Since we're touching the frame data, sync it */
4198373815efSAdrian Chadd 					bus_dmamap_sync(sc->sc_dmat,
4199373815efSAdrian Chadd 					    bf->bf_dmamap,
4200373815efSAdrian Chadd 					    BUS_DMASYNC_POSTREAD);
4201373815efSAdrian Chadd 					/* Now pass it to the radar processing code */
420206fc4a10SAdrian Chadd 					ath_dfs_process_phy_err(sc, mtod(m, char *), rstamp, rs);
4203373815efSAdrian Chadd 				}
420448237774SAdrian Chadd 
4205f9aa1d90SAdrian Chadd 				/* Be suitably paranoid about receiving phy errors out of the stats array bounds */
4206f9aa1d90SAdrian Chadd 				if (rs->rs_phyerr < 64)
4207f9aa1d90SAdrian Chadd 					sc->sc_stats.ast_rx_phy[rs->rs_phyerr]++;
420868e8e04eSSam Leffler 				goto rx_error;	/* NB: don't count in ierrors */
4209c42a7b7eSSam Leffler 			}
421065f9edeeSSam Leffler 			if (rs->rs_status & HAL_RXERR_DECRYPT) {
421185643802SSam Leffler 				/*
4212c42a7b7eSSam Leffler 				 * Decrypt error.  If the error occurred
4213c42a7b7eSSam Leffler 				 * because there was no hardware key, then
4214c42a7b7eSSam Leffler 				 * let the frame through so the upper layers
4215c42a7b7eSSam Leffler 				 * can process it.  This is necessary for 5210
4216c42a7b7eSSam Leffler 				 * parts which have no way to setup a ``clear''
4217c42a7b7eSSam Leffler 				 * key cache entry.
4218c42a7b7eSSam Leffler 				 *
4219c42a7b7eSSam Leffler 				 * XXX do key cache faulting
422085643802SSam Leffler 				 */
422165f9edeeSSam Leffler 				if (rs->rs_keyix == HAL_RXKEYIX_INVALID)
4222c42a7b7eSSam Leffler 					goto rx_accept;
4223c42a7b7eSSam Leffler 				sc->sc_stats.ast_rx_badcrypt++;
42245591b213SSam Leffler 			}
422565f9edeeSSam Leffler 			if (rs->rs_status & HAL_RXERR_MIC) {
4226c42a7b7eSSam Leffler 				sc->sc_stats.ast_rx_badmic++;
4227c42a7b7eSSam Leffler 				/*
4228c42a7b7eSSam Leffler 				 * Do minimal work required to hand off
42295463c4a4SSam Leffler 				 * the 802.11 header for notification.
4230c42a7b7eSSam Leffler 				 */
4231c42a7b7eSSam Leffler 				/* XXX frag's and qos frames */
423265f9edeeSSam Leffler 				len = rs->rs_datalen;
4233c42a7b7eSSam Leffler 				if (len >= sizeof (struct ieee80211_frame)) {
4234c42a7b7eSSam Leffler 					bus_dmamap_sync(sc->sc_dmat,
4235c42a7b7eSSam Leffler 					    bf->bf_dmamap,
4236c42a7b7eSSam Leffler 					    BUS_DMASYNC_POSTREAD);
4237b032f27cSSam Leffler 					ath_handle_micerror(ic,
4238c42a7b7eSSam Leffler 					    mtod(m, struct ieee80211_frame *),
42390ab4040aSSam Leffler 					    sc->sc_splitmic ?
4240b032f27cSSam Leffler 						rs->rs_keyix-32 : rs->rs_keyix);
4241c42a7b7eSSam Leffler 				}
4242c42a7b7eSSam Leffler 			}
4243c42a7b7eSSam Leffler 			ifp->if_ierrors++;
424468e8e04eSSam Leffler rx_error:
424568e8e04eSSam Leffler 			/*
424668e8e04eSSam Leffler 			 * Cleanup any pending partial frame.
424768e8e04eSSam Leffler 			 */
424868e8e04eSSam Leffler 			if (sc->sc_rxpending != NULL) {
424968e8e04eSSam Leffler 				m_freem(sc->sc_rxpending);
425068e8e04eSSam Leffler 				sc->sc_rxpending = NULL;
425168e8e04eSSam Leffler 			}
4252c42a7b7eSSam Leffler 			/*
42537b0c77ecSSam Leffler 			 * When a tap is present pass error frames
42547b0c77ecSSam Leffler 			 * that have been requested.  By default we
42557b0c77ecSSam Leffler 			 * pass decrypt+mic errors but others may be
42567b0c77ecSSam Leffler 			 * interesting (e.g. crc).
4257c42a7b7eSSam Leffler 			 */
42585463c4a4SSam Leffler 			if (ieee80211_radiotap_active(ic) &&
425965f9edeeSSam Leffler 			    (rs->rs_status & sc->sc_monpass)) {
42607b0c77ecSSam Leffler 				bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
42617b0c77ecSSam Leffler 				    BUS_DMASYNC_POSTREAD);
42627b0c77ecSSam Leffler 				/* NB: bpf needs the mbuf length setup */
426365f9edeeSSam Leffler 				len = rs->rs_datalen;
42647b0c77ecSSam Leffler 				m->m_pkthdr.len = m->m_len = len;
4265dcfd99a7SAdrian Chadd 				bf->bf_m = NULL;
426606fc4a10SAdrian Chadd 				ath_rx_tap(ifp, m, rs, rstamp, nf);
42675463c4a4SSam Leffler 				ieee80211_radiotap_rx_all(ic, m);
4268dcfd99a7SAdrian Chadd 				m_freem(m);
42697b0c77ecSSam Leffler 			}
42707b0c77ecSSam Leffler 			/* XXX pass MIC errors up for s/w reclaculation */
42715591b213SSam Leffler 			goto rx_next;
42725591b213SSam Leffler 		}
4273c42a7b7eSSam Leffler rx_accept:
4274c42a7b7eSSam Leffler 		/*
4275c42a7b7eSSam Leffler 		 * Sync and unmap the frame.  At this point we're
4276c42a7b7eSSam Leffler 		 * committed to passing the mbuf somewhere so clear
4277c66c48cbSSam Leffler 		 * bf_m; this means a new mbuf must be allocated
4278c42a7b7eSSam Leffler 		 * when the rx descriptor is setup again to receive
4279c42a7b7eSSam Leffler 		 * another frame.
4280c42a7b7eSSam Leffler 		 */
42815591b213SSam Leffler 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
42825591b213SSam Leffler 		    BUS_DMASYNC_POSTREAD);
42835591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
42845591b213SSam Leffler 		bf->bf_m = NULL;
4285c42a7b7eSSam Leffler 
428665f9edeeSSam Leffler 		len = rs->rs_datalen;
428768e8e04eSSam Leffler 		m->m_len = len;
428868e8e04eSSam Leffler 
428968e8e04eSSam Leffler 		if (rs->rs_more) {
429068e8e04eSSam Leffler 			/*
429168e8e04eSSam Leffler 			 * Frame spans multiple descriptors; save
429268e8e04eSSam Leffler 			 * it for the next completed descriptor, it
429368e8e04eSSam Leffler 			 * will be used to construct a jumbogram.
429468e8e04eSSam Leffler 			 */
429568e8e04eSSam Leffler 			if (sc->sc_rxpending != NULL) {
429668e8e04eSSam Leffler 				/* NB: max frame size is currently 2 clusters */
429768e8e04eSSam Leffler 				sc->sc_stats.ast_rx_toobig++;
429868e8e04eSSam Leffler 				m_freem(sc->sc_rxpending);
429968e8e04eSSam Leffler 			}
430068e8e04eSSam Leffler 			m->m_pkthdr.rcvif = ifp;
430168e8e04eSSam Leffler 			m->m_pkthdr.len = len;
430268e8e04eSSam Leffler 			sc->sc_rxpending = m;
430368e8e04eSSam Leffler 			goto rx_next;
430468e8e04eSSam Leffler 		} else if (sc->sc_rxpending != NULL) {
430568e8e04eSSam Leffler 			/*
430668e8e04eSSam Leffler 			 * This is the second part of a jumbogram,
430768e8e04eSSam Leffler 			 * chain it to the first mbuf, adjust the
430868e8e04eSSam Leffler 			 * frame length, and clear the rxpending state.
430968e8e04eSSam Leffler 			 */
431068e8e04eSSam Leffler 			sc->sc_rxpending->m_next = m;
431168e8e04eSSam Leffler 			sc->sc_rxpending->m_pkthdr.len += len;
431268e8e04eSSam Leffler 			m = sc->sc_rxpending;
431368e8e04eSSam Leffler 			sc->sc_rxpending = NULL;
431468e8e04eSSam Leffler 		} else {
431568e8e04eSSam Leffler 			/*
431668e8e04eSSam Leffler 			 * Normal single-descriptor receive; setup
431768e8e04eSSam Leffler 			 * the rcvif and packet length.
431868e8e04eSSam Leffler 			 */
431968e8e04eSSam Leffler 			m->m_pkthdr.rcvif = ifp;
432068e8e04eSSam Leffler 			m->m_pkthdr.len = len;
432168e8e04eSSam Leffler 		}
432273454c73SSam Leffler 
4323197d53c5SAdrian Chadd 		/*
4324197d53c5SAdrian Chadd 		 * Validate rs->rs_antenna.
4325197d53c5SAdrian Chadd 		 *
4326197d53c5SAdrian Chadd 		 * Some users w/ AR9285 NICs have reported crashes
4327197d53c5SAdrian Chadd 		 * here because rs_antenna field is bogusly large.
4328197d53c5SAdrian Chadd 		 * Let's enforce the maximum antenna limit of 8
4329197d53c5SAdrian Chadd 		 * (and it shouldn't be hard coded, but that's a
4330197d53c5SAdrian Chadd 		 * separate problem) and if there's an issue, print
4331197d53c5SAdrian Chadd 		 * out an error and adjust rs_antenna to something
4332197d53c5SAdrian Chadd 		 * sensible.
4333197d53c5SAdrian Chadd 		 *
4334197d53c5SAdrian Chadd 		 * This code should be removed once the actual
4335197d53c5SAdrian Chadd 		 * root cause of the issue has been identified.
4336197d53c5SAdrian Chadd 		 * For example, it may be that the rs_antenna
4337197d53c5SAdrian Chadd 		 * field is only valid for the lsat frame of
4338197d53c5SAdrian Chadd 		 * an aggregate and it just happens that it is
4339197d53c5SAdrian Chadd 		 * "mostly" right. (This is a general statement -
4340197d53c5SAdrian Chadd 		 * the majority of the statistics are only valid
4341197d53c5SAdrian Chadd 		 * for the last frame in an aggregate.
4342197d53c5SAdrian Chadd 		 */
4343197d53c5SAdrian Chadd 		if (rs->rs_antenna > 7) {
4344197d53c5SAdrian Chadd 			device_printf(sc->sc_dev, "%s: rs_antenna > 7 (%d)\n",
4345197d53c5SAdrian Chadd 			    __func__, rs->rs_antenna);
4346197d53c5SAdrian Chadd #ifdef	ATH_DEBUG
4347197d53c5SAdrian Chadd 			ath_printrxbuf(sc, bf, 0, status == HAL_OK);
4348197d53c5SAdrian Chadd #endif /* ATH_DEBUG */
4349197d53c5SAdrian Chadd 			rs->rs_antenna = 0;	/* XXX better than nothing */
4350197d53c5SAdrian Chadd 		}
4351197d53c5SAdrian Chadd 
4352b032f27cSSam Leffler 		ifp->if_ipackets++;
435365f9edeeSSam Leffler 		sc->sc_stats.ast_ant_rx[rs->rs_antenna]++;
4354c42a7b7eSSam Leffler 
43555463c4a4SSam Leffler 		/*
43565463c4a4SSam Leffler 		 * Populate the rx status block.  When there are bpf
43575463c4a4SSam Leffler 		 * listeners we do the additional work to provide
43585463c4a4SSam Leffler 		 * complete status.  Otherwise we fill in only the
43595463c4a4SSam Leffler 		 * material required by ieee80211_input.  Note that
43605463c4a4SSam Leffler 		 * noise setting is filled in above.
43615463c4a4SSam Leffler 		 */
43625463c4a4SSam Leffler 		if (ieee80211_radiotap_active(ic))
436306fc4a10SAdrian Chadd 			ath_rx_tap(ifp, m, rs, rstamp, nf);
43640a915fadSSam Leffler 
43655591b213SSam Leffler 		/*
4366c42a7b7eSSam Leffler 		 * From this point on we assume the frame is at least
4367c42a7b7eSSam Leffler 		 * as large as ieee80211_frame_min; verify that.
43685591b213SSam Leffler 		 */
4369c42a7b7eSSam Leffler 		if (len < IEEE80211_MIN_LEN) {
43705463c4a4SSam Leffler 			if (!ieee80211_radiotap_active(ic)) {
43715463c4a4SSam Leffler 				DPRINTF(sc, ATH_DEBUG_RECV,
43725463c4a4SSam Leffler 				    "%s: short packet %d\n", __func__, len);
4373c42a7b7eSSam Leffler 				sc->sc_stats.ast_rx_tooshort++;
43745463c4a4SSam Leffler 			} else {
43755463c4a4SSam Leffler 				/* NB: in particular this captures ack's */
43765463c4a4SSam Leffler 				ieee80211_radiotap_rx_all(ic, m);
43775463c4a4SSam Leffler 			}
4378c42a7b7eSSam Leffler 			m_freem(m);
4379c42a7b7eSSam Leffler 			goto rx_next;
43805591b213SSam Leffler 		}
43810a915fadSSam Leffler 
4382c42a7b7eSSam Leffler 		if (IFF_DUMPPKTS(sc, ATH_DEBUG_RECV)) {
438346d4d74cSSam Leffler 			const HAL_RATE_TABLE *rt = sc->sc_currates;
438446d4d74cSSam Leffler 			uint8_t rix = rt->rateCodeToIndex[rs->rs_rate];
438546d4d74cSSam Leffler 
438668e8e04eSSam Leffler 			ieee80211_dump_pkt(ic, mtod(m, caddr_t), len,
438746d4d74cSSam Leffler 			    sc->sc_hwmap[rix].ieeerate, rs->rs_rssi);
4388c42a7b7eSSam Leffler 		}
4389c42a7b7eSSam Leffler 
4390c42a7b7eSSam Leffler 		m_adj(m, -IEEE80211_CRC_LEN);
4391de5af704SSam Leffler 
4392de5af704SSam Leffler 		/*
4393c42a7b7eSSam Leffler 		 * Locate the node for sender, track state, and then
4394c42a7b7eSSam Leffler 		 * pass the (referenced) node up to the 802.11 layer
4395c42a7b7eSSam Leffler 		 * for its use.
4396c42a7b7eSSam Leffler 		 */
4397c1225b52SSam Leffler 		ni = ieee80211_find_rxnode_withkey(ic,
4398c1225b52SSam Leffler 			mtod(m, const struct ieee80211_frame_min *),
439965f9edeeSSam Leffler 			rs->rs_keyix == HAL_RXKEYIX_INVALID ?
440065f9edeeSSam Leffler 				IEEE80211_KEYIX_NONE : rs->rs_keyix);
44017041d50cSBernhard Schmidt 		sc->sc_lastrs = rs;
4402a07e9ddbSAdrian Chadd 
44036e0f1168SAdrian Chadd #ifdef	AH_SUPPORT_AR5416
4404a07e9ddbSAdrian Chadd 		if (rs->rs_isaggr)
4405a07e9ddbSAdrian Chadd 			sc->sc_stats.ast_rx_agg++;
44066e0f1168SAdrian Chadd #endif /* AH_SUPPORT_AR5416 */
4407a07e9ddbSAdrian Chadd 
4408a07e9ddbSAdrian Chadd 		if (ni != NULL) {
4409b032f27cSSam Leffler 			/*
4410e57539afSAdrian Chadd  			 * Only punt packets for ampdu reorder processing for
4411e57539afSAdrian Chadd 			 * 11n nodes; net80211 enforces that M_AMPDU is only
4412e57539afSAdrian Chadd 			 * set for 11n nodes.
441300fc8705SAdrian Chadd  			 */
441400fc8705SAdrian Chadd 			if (ni->ni_flags & IEEE80211_NODE_HT)
441500fc8705SAdrian Chadd 				m->m_flags |= M_AMPDU;
441600fc8705SAdrian Chadd 
441700fc8705SAdrian Chadd 			/*
4418b032f27cSSam Leffler 			 * Sending station is known, dispatch directly.
4419b032f27cSSam Leffler 			 */
44205463c4a4SSam Leffler 			type = ieee80211_input(ni, m, rs->rs_rssi, nf);
4421b032f27cSSam Leffler 			ieee80211_free_node(ni);
4422b032f27cSSam Leffler 			/*
4423b032f27cSSam Leffler 			 * Arrange to update the last rx timestamp only for
4424b032f27cSSam Leffler 			 * frames from our ap when operating in station mode.
4425b032f27cSSam Leffler 			 * This assumes the rx key is always setup when
4426b032f27cSSam Leffler 			 * associated.
4427b032f27cSSam Leffler 			 */
4428b032f27cSSam Leffler 			if (ic->ic_opmode == IEEE80211_M_STA &&
4429b032f27cSSam Leffler 			    rs->rs_keyix != HAL_RXKEYIX_INVALID)
4430b032f27cSSam Leffler 				ngood++;
4431b032f27cSSam Leffler 		} else {
44325463c4a4SSam Leffler 			type = ieee80211_input_all(ic, m, rs->rs_rssi, nf);
4433b032f27cSSam Leffler 		}
4434c42a7b7eSSam Leffler 		/*
4435c42a7b7eSSam Leffler 		 * Track rx rssi and do any rx antenna management.
4436de5af704SSam Leffler 		 */
443765f9edeeSSam Leffler 		ATH_RSSI_LPF(sc->sc_halstats.ns_avgrssi, rs->rs_rssi);
4438c42a7b7eSSam Leffler 		if (sc->sc_diversity) {
4439c42a7b7eSSam Leffler 			/*
4440c42a7b7eSSam Leffler 			 * When using fast diversity, change the default rx
4441c42a7b7eSSam Leffler 			 * antenna if diversity chooses the other antenna 3
4442c42a7b7eSSam Leffler 			 * times in a row.
4443c42a7b7eSSam Leffler 			 */
444465f9edeeSSam Leffler 			if (sc->sc_defant != rs->rs_antenna) {
4445c42a7b7eSSam Leffler 				if (++sc->sc_rxotherant >= 3)
444665f9edeeSSam Leffler 					ath_setdefantenna(sc, rs->rs_antenna);
4447c42a7b7eSSam Leffler 			} else
4448c42a7b7eSSam Leffler 				sc->sc_rxotherant = 0;
4449c42a7b7eSSam Leffler 		}
4450235ab70eSAdrian Chadd 
4451235ab70eSAdrian Chadd 		/* Newer school diversity - kite specific for now */
4452235ab70eSAdrian Chadd 		/* XXX perhaps migrate the normal diversity code to this? */
4453235ab70eSAdrian Chadd 		if ((ah)->ah_rxAntCombDiversity)
4454235ab70eSAdrian Chadd 			(*(ah)->ah_rxAntCombDiversity)(ah, rs, ticks, hz);
4455235ab70eSAdrian Chadd 
44563e50ec2cSSam Leffler 		if (sc->sc_softled) {
44573e50ec2cSSam Leffler 			/*
44583e50ec2cSSam Leffler 			 * Blink for any data frame.  Otherwise do a
44593e50ec2cSSam Leffler 			 * heartbeat-style blink when idle.  The latter
44603e50ec2cSSam Leffler 			 * is mainly for station mode where we depend on
44613e50ec2cSSam Leffler 			 * periodic beacon frames to trigger the poll event.
44623e50ec2cSSam Leffler 			 */
446331640eb7SSam Leffler 			if (type == IEEE80211_FC0_TYPE_DATA) {
446446d4d74cSSam Leffler 				const HAL_RATE_TABLE *rt = sc->sc_currates;
446546d4d74cSSam Leffler 				ath_led_event(sc,
446646d4d74cSSam Leffler 				    rt->rateCodeToIndex[rs->rs_rate]);
44673e50ec2cSSam Leffler 			} else if (ticks - sc->sc_ledevent >= sc->sc_ledidle)
446846d4d74cSSam Leffler 				ath_led_event(sc, 0);
44693e50ec2cSSam Leffler 		}
44705591b213SSam Leffler rx_next:
44716b349e5aSAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
44725591b213SSam Leffler 	} while (ath_rxbuf_init(sc, bf) == 0);
44735591b213SSam Leffler 
4474c42a7b7eSSam Leffler 	/* rx signal state monitoring */
447559efa8b5SSam Leffler 	ath_hal_rxmonitor(ah, &sc->sc_halstats, sc->sc_curchan);
4476d7736e13SSam Leffler 	if (ngood)
4477d7736e13SSam Leffler 		sc->sc_lastrx = tsf;
4478b5f4adb3SSam Leffler 
4479f52d3452SAdrian Chadd 	CTR2(ATH_KTR_INTR, "ath_rx_proc: npkts=%d, ngood=%d", npkts, ngood);
448048237774SAdrian Chadd 	/* Queue DFS tasklet if needed */
448196ff485dSAdrian Chadd 	if (resched && ath_dfs_tasklet_needed(sc, sc->sc_curchan))
448248237774SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_dfstask);
448348237774SAdrian Chadd 
44841fdadc0fSAdrian Chadd 	/*
44851fdadc0fSAdrian Chadd 	 * Now that all the RX frames were handled that
44861fdadc0fSAdrian Chadd 	 * need to be handled, kick the PCU if there's
44871fdadc0fSAdrian Chadd 	 * been an RXEOL condition.
44881fdadc0fSAdrian Chadd 	 */
4489ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
449096ff485dSAdrian Chadd 	if (resched && sc->sc_kickpcu) {
4491f52d3452SAdrian Chadd 		CTR0(ATH_KTR_ERR, "ath_rx_proc: kickpcu");
44928f939e79SAdrian Chadd 		device_printf(sc->sc_dev, "%s: kickpcu; handled %d packets\n",
44938f939e79SAdrian Chadd 		    __func__, npkts);
44948f939e79SAdrian Chadd 
44958f939e79SAdrian Chadd 		/* XXX rxslink? */
4496ef27340cSAdrian Chadd 		/*
4497ef27340cSAdrian Chadd 		 * XXX can we hold the PCU lock here?
4498ef27340cSAdrian Chadd 		 * Are there any net80211 buffer calls involved?
4499ef27340cSAdrian Chadd 		 */
45008f939e79SAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_rxbuf);
45018f939e79SAdrian Chadd 		ath_hal_putrxbuf(ah, bf->bf_daddr);
45028f939e79SAdrian Chadd 		ath_hal_rxena(ah);		/* enable recv descriptors */
45038f939e79SAdrian Chadd 		ath_mode_init(sc);		/* set filters, etc. */
45048f939e79SAdrian Chadd 		ath_hal_startpcurecv(ah);	/* re-enable PCU/DMA engine */
45058f939e79SAdrian Chadd 
45061fdadc0fSAdrian Chadd 		ath_hal_intrset(ah, sc->sc_imask);
45078f939e79SAdrian Chadd 		sc->sc_kickpcu = 0;
45081fdadc0fSAdrian Chadd 	}
4509ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
45101fdadc0fSAdrian Chadd 
4511ef27340cSAdrian Chadd 	/* XXX check this inside of IF_LOCK? */
451296ff485dSAdrian Chadd 	if (resched && (ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0) {
4513339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
451404f19fd6SSam Leffler 		ieee80211_ff_age_all(ic, 100);
4515339ccfb3SSam Leffler #endif
4516339ccfb3SSam Leffler 		if (!IFQ_IS_EMPTY(&ifp->if_snd))
4517cd196bb2SSam Leffler 			ath_start(ifp);
4518339ccfb3SSam Leffler 	}
45198cec0ab9SSam Leffler #undef PA2DESC
4520ef27340cSAdrian Chadd 
4521ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4522ef27340cSAdrian Chadd 	sc->sc_rxproc_cnt--;
4523ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
45245591b213SSam Leffler }
45255591b213SSam Leffler 
4526622b3fd2SSam Leffler static void
4527622b3fd2SSam Leffler ath_txq_init(struct ath_softc *sc, struct ath_txq *txq, int qnum)
4528622b3fd2SSam Leffler {
4529622b3fd2SSam Leffler 	txq->axq_qnum = qnum;
4530339ccfb3SSam Leffler 	txq->axq_ac = 0;
4531622b3fd2SSam Leffler 	txq->axq_depth = 0;
453216d4de92SAdrian Chadd 	txq->axq_aggr_depth = 0;
4533622b3fd2SSam Leffler 	txq->axq_intrcnt = 0;
4534622b3fd2SSam Leffler 	txq->axq_link = NULL;
45356b349e5aSAdrian Chadd 	txq->axq_softc = sc;
45366b349e5aSAdrian Chadd 	TAILQ_INIT(&txq->axq_q);
45376b349e5aSAdrian Chadd 	TAILQ_INIT(&txq->axq_tidq);
4538622b3fd2SSam Leffler 	ATH_TXQ_LOCK_INIT(sc, txq);
4539622b3fd2SSam Leffler }
4540622b3fd2SSam Leffler 
45415591b213SSam Leffler /*
4542c42a7b7eSSam Leffler  * Setup a h/w transmit queue.
45435591b213SSam Leffler  */
4544c42a7b7eSSam Leffler static struct ath_txq *
4545c42a7b7eSSam Leffler ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
4546c42a7b7eSSam Leffler {
4547c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
4548c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4549c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
4550c42a7b7eSSam Leffler 	int qnum;
4551c42a7b7eSSam Leffler 
4552c42a7b7eSSam Leffler 	memset(&qi, 0, sizeof(qi));
4553c42a7b7eSSam Leffler 	qi.tqi_subtype = subtype;
4554c42a7b7eSSam Leffler 	qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
4555c42a7b7eSSam Leffler 	qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
4556c42a7b7eSSam Leffler 	qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
4557c42a7b7eSSam Leffler 	/*
4558c42a7b7eSSam Leffler 	 * Enable interrupts only for EOL and DESC conditions.
4559c42a7b7eSSam Leffler 	 * We mark tx descriptors to receive a DESC interrupt
4560c42a7b7eSSam Leffler 	 * when a tx queue gets deep; otherwise waiting for the
4561c42a7b7eSSam Leffler 	 * EOL to reap descriptors.  Note that this is done to
4562c42a7b7eSSam Leffler 	 * reduce interrupt load and this only defers reaping
4563c42a7b7eSSam Leffler 	 * descriptors, never transmitting frames.  Aside from
4564c42a7b7eSSam Leffler 	 * reducing interrupts this also permits more concurrency.
4565c42a7b7eSSam Leffler 	 * The only potential downside is if the tx queue backs
4566c42a7b7eSSam Leffler 	 * up in which case the top half of the kernel may backup
4567c42a7b7eSSam Leffler 	 * due to a lack of tx descriptors.
4568c42a7b7eSSam Leffler 	 */
4569bd5a9920SSam Leffler 	qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE | HAL_TXQ_TXDESCINT_ENABLE;
4570c42a7b7eSSam Leffler 	qnum = ath_hal_setuptxqueue(ah, qtype, &qi);
4571c42a7b7eSSam Leffler 	if (qnum == -1) {
4572c42a7b7eSSam Leffler 		/*
4573c42a7b7eSSam Leffler 		 * NB: don't print a message, this happens
4574a614e076SSam Leffler 		 * normally on parts with too few tx queues
4575c42a7b7eSSam Leffler 		 */
4576c42a7b7eSSam Leffler 		return NULL;
4577c42a7b7eSSam Leffler 	}
4578c42a7b7eSSam Leffler 	if (qnum >= N(sc->sc_txq)) {
45796891c875SPeter Wemm 		device_printf(sc->sc_dev,
45806891c875SPeter Wemm 			"hal qnum %u out of range, max %zu!\n",
4581c42a7b7eSSam Leffler 			qnum, N(sc->sc_txq));
4582c42a7b7eSSam Leffler 		ath_hal_releasetxqueue(ah, qnum);
4583c42a7b7eSSam Leffler 		return NULL;
4584c42a7b7eSSam Leffler 	}
4585c42a7b7eSSam Leffler 	if (!ATH_TXQ_SETUP(sc, qnum)) {
4586622b3fd2SSam Leffler 		ath_txq_init(sc, &sc->sc_txq[qnum], qnum);
4587c42a7b7eSSam Leffler 		sc->sc_txqsetup |= 1<<qnum;
4588c42a7b7eSSam Leffler 	}
4589c42a7b7eSSam Leffler 	return &sc->sc_txq[qnum];
4590c42a7b7eSSam Leffler #undef N
4591c42a7b7eSSam Leffler }
4592c42a7b7eSSam Leffler 
4593c42a7b7eSSam Leffler /*
4594c42a7b7eSSam Leffler  * Setup a hardware data transmit queue for the specified
4595c42a7b7eSSam Leffler  * access control.  The hal may not support all requested
4596c42a7b7eSSam Leffler  * queues in which case it will return a reference to a
4597c42a7b7eSSam Leffler  * previously setup queue.  We record the mapping from ac's
4598c42a7b7eSSam Leffler  * to h/w queues for use by ath_tx_start and also track
4599c42a7b7eSSam Leffler  * the set of h/w queues being used to optimize work in the
4600c42a7b7eSSam Leffler  * transmit interrupt handler and related routines.
4601c42a7b7eSSam Leffler  */
4602c42a7b7eSSam Leffler static int
4603c42a7b7eSSam Leffler ath_tx_setup(struct ath_softc *sc, int ac, int haltype)
4604c42a7b7eSSam Leffler {
4605c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
4606c42a7b7eSSam Leffler 	struct ath_txq *txq;
4607c42a7b7eSSam Leffler 
4608c42a7b7eSSam Leffler 	if (ac >= N(sc->sc_ac2q)) {
46096891c875SPeter Wemm 		device_printf(sc->sc_dev, "AC %u out of range, max %zu!\n",
4610c42a7b7eSSam Leffler 			ac, N(sc->sc_ac2q));
4611c42a7b7eSSam Leffler 		return 0;
4612c42a7b7eSSam Leffler 	}
4613c42a7b7eSSam Leffler 	txq = ath_txq_setup(sc, HAL_TX_QUEUE_DATA, haltype);
4614c42a7b7eSSam Leffler 	if (txq != NULL) {
4615339ccfb3SSam Leffler 		txq->axq_ac = ac;
4616c42a7b7eSSam Leffler 		sc->sc_ac2q[ac] = txq;
4617c42a7b7eSSam Leffler 		return 1;
4618c42a7b7eSSam Leffler 	} else
4619c42a7b7eSSam Leffler 		return 0;
4620c42a7b7eSSam Leffler #undef N
4621c42a7b7eSSam Leffler }
4622c42a7b7eSSam Leffler 
4623c42a7b7eSSam Leffler /*
4624c42a7b7eSSam Leffler  * Update WME parameters for a transmit queue.
4625c42a7b7eSSam Leffler  */
4626c42a7b7eSSam Leffler static int
4627c42a7b7eSSam Leffler ath_txq_update(struct ath_softc *sc, int ac)
4628c42a7b7eSSam Leffler {
4629c42a7b7eSSam Leffler #define	ATH_EXPONENT_TO_VALUE(v)	((1<<v)-1)
4630c42a7b7eSSam Leffler #define	ATH_TXOP_TO_US(v)		(v<<5)
4631b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
4632b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
4633c42a7b7eSSam Leffler 	struct ath_txq *txq = sc->sc_ac2q[ac];
4634c42a7b7eSSam Leffler 	struct wmeParams *wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
4635c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4636c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
4637c42a7b7eSSam Leffler 
4638c42a7b7eSSam Leffler 	ath_hal_gettxqueueprops(ah, txq->axq_qnum, &qi);
4639584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
464010ad9a77SSam Leffler 	if (sc->sc_tdma) {
464110ad9a77SSam Leffler 		/*
464210ad9a77SSam Leffler 		 * AIFS is zero so there's no pre-transmit wait.  The
464310ad9a77SSam Leffler 		 * burst time defines the slot duration and is configured
464409be6601SSam Leffler 		 * through net80211.  The QCU is setup to not do post-xmit
464510ad9a77SSam Leffler 		 * back off, lockout all lower-priority QCU's, and fire
464610ad9a77SSam Leffler 		 * off the DMA beacon alert timer which is setup based
464710ad9a77SSam Leffler 		 * on the slot configuration.
464810ad9a77SSam Leffler 		 */
464910ad9a77SSam Leffler 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
465010ad9a77SSam Leffler 			      | HAL_TXQ_TXERRINT_ENABLE
465110ad9a77SSam Leffler 			      | HAL_TXQ_TXURNINT_ENABLE
465210ad9a77SSam Leffler 			      | HAL_TXQ_TXEOLINT_ENABLE
465310ad9a77SSam Leffler 			      | HAL_TXQ_DBA_GATED
465410ad9a77SSam Leffler 			      | HAL_TXQ_BACKOFF_DISABLE
465510ad9a77SSam Leffler 			      | HAL_TXQ_ARB_LOCKOUT_GLOBAL
465610ad9a77SSam Leffler 			      ;
465710ad9a77SSam Leffler 		qi.tqi_aifs = 0;
465810ad9a77SSam Leffler 		/* XXX +dbaprep? */
465910ad9a77SSam Leffler 		qi.tqi_readyTime = sc->sc_tdmaslotlen;
466010ad9a77SSam Leffler 		qi.tqi_burstTime = qi.tqi_readyTime;
466110ad9a77SSam Leffler 	} else {
466210ad9a77SSam Leffler #endif
466316d4de92SAdrian Chadd 		/*
466416d4de92SAdrian Chadd 		 * XXX shouldn't this just use the default flags
466516d4de92SAdrian Chadd 		 * used in the previous queue setup?
466616d4de92SAdrian Chadd 		 */
466710ad9a77SSam Leffler 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
466810ad9a77SSam Leffler 			      | HAL_TXQ_TXERRINT_ENABLE
466910ad9a77SSam Leffler 			      | HAL_TXQ_TXDESCINT_ENABLE
467010ad9a77SSam Leffler 			      | HAL_TXQ_TXURNINT_ENABLE
46711f25c0f7SAdrian Chadd 			      | HAL_TXQ_TXEOLINT_ENABLE
467210ad9a77SSam Leffler 			      ;
4673c42a7b7eSSam Leffler 		qi.tqi_aifs = wmep->wmep_aifsn;
4674c42a7b7eSSam Leffler 		qi.tqi_cwmin = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
4675c42a7b7eSSam Leffler 		qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
467610ad9a77SSam Leffler 		qi.tqi_readyTime = 0;
4677c42a7b7eSSam Leffler 		qi.tqi_burstTime = ATH_TXOP_TO_US(wmep->wmep_txopLimit);
4678584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
467910ad9a77SSam Leffler 	}
468010ad9a77SSam Leffler #endif
468110ad9a77SSam Leffler 
468210ad9a77SSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET,
468310ad9a77SSam Leffler 	    "%s: Q%u qflags 0x%x aifs %u cwmin %u cwmax %u burstTime %u\n",
468410ad9a77SSam Leffler 	    __func__, txq->axq_qnum, qi.tqi_qflags,
468510ad9a77SSam Leffler 	    qi.tqi_aifs, qi.tqi_cwmin, qi.tqi_cwmax, qi.tqi_burstTime);
4686c42a7b7eSSam Leffler 
4687c42a7b7eSSam Leffler 	if (!ath_hal_settxqueueprops(ah, txq->axq_qnum, &qi)) {
4688b032f27cSSam Leffler 		if_printf(ifp, "unable to update hardware queue "
4689c42a7b7eSSam Leffler 			"parameters for %s traffic!\n",
4690c42a7b7eSSam Leffler 			ieee80211_wme_acnames[ac]);
4691c42a7b7eSSam Leffler 		return 0;
4692c42a7b7eSSam Leffler 	} else {
4693c42a7b7eSSam Leffler 		ath_hal_resettxqueue(ah, txq->axq_qnum); /* push to h/w */
4694c42a7b7eSSam Leffler 		return 1;
4695c42a7b7eSSam Leffler 	}
4696c42a7b7eSSam Leffler #undef ATH_TXOP_TO_US
4697c42a7b7eSSam Leffler #undef ATH_EXPONENT_TO_VALUE
4698c42a7b7eSSam Leffler }
4699c42a7b7eSSam Leffler 
4700c42a7b7eSSam Leffler /*
4701c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update WME parameters.
4702c42a7b7eSSam Leffler  */
4703c42a7b7eSSam Leffler static int
4704c42a7b7eSSam Leffler ath_wme_update(struct ieee80211com *ic)
4705c42a7b7eSSam Leffler {
4706c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
4707c42a7b7eSSam Leffler 
4708c42a7b7eSSam Leffler 	return !ath_txq_update(sc, WME_AC_BE) ||
4709c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_BK) ||
4710c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VI) ||
4711c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VO) ? EIO : 0;
4712c42a7b7eSSam Leffler }
4713c42a7b7eSSam Leffler 
4714c42a7b7eSSam Leffler /*
4715c42a7b7eSSam Leffler  * Reclaim resources for a setup queue.
4716c42a7b7eSSam Leffler  */
4717c42a7b7eSSam Leffler static void
4718c42a7b7eSSam Leffler ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
4719c42a7b7eSSam Leffler {
4720c42a7b7eSSam Leffler 
4721c42a7b7eSSam Leffler 	ath_hal_releasetxqueue(sc->sc_ah, txq->axq_qnum);
4722c42a7b7eSSam Leffler 	ATH_TXQ_LOCK_DESTROY(txq);
4723c42a7b7eSSam Leffler 	sc->sc_txqsetup &= ~(1<<txq->axq_qnum);
4724c42a7b7eSSam Leffler }
4725c42a7b7eSSam Leffler 
4726c42a7b7eSSam Leffler /*
4727c42a7b7eSSam Leffler  * Reclaim all tx queue resources.
4728c42a7b7eSSam Leffler  */
4729c42a7b7eSSam Leffler static void
4730c42a7b7eSSam Leffler ath_tx_cleanup(struct ath_softc *sc)
4731c42a7b7eSSam Leffler {
4732c42a7b7eSSam Leffler 	int i;
4733c42a7b7eSSam Leffler 
4734c42a7b7eSSam Leffler 	ATH_TXBUF_LOCK_DESTROY(sc);
4735c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
4736c42a7b7eSSam Leffler 		if (ATH_TXQ_SETUP(sc, i))
4737c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, &sc->sc_txq[i]);
4738c42a7b7eSSam Leffler }
47395591b213SSam Leffler 
474099d258fdSSam Leffler /*
4741ab06fdf2SSam Leffler  * Return h/w rate index for an IEEE rate (w/o basic rate bit)
4742ab06fdf2SSam Leffler  * using the current rates in sc_rixmap.
47438b5341deSSam Leffler  */
4744b8e788a5SAdrian Chadd int
4745ab06fdf2SSam Leffler ath_tx_findrix(const struct ath_softc *sc, uint8_t rate)
47468b5341deSSam Leffler {
4747ab06fdf2SSam Leffler 	int rix = sc->sc_rixmap[rate];
4748ab06fdf2SSam Leffler 	/* NB: return lowest rix for invalid rate */
4749ab06fdf2SSam Leffler 	return (rix == 0xff ? 0 : rix);
47508b5341deSSam Leffler }
47518b5341deSSam Leffler 
47529352fb7aSAdrian Chadd static void
47539352fb7aSAdrian Chadd ath_tx_update_stats(struct ath_softc *sc, struct ath_tx_status *ts,
47549352fb7aSAdrian Chadd     struct ath_buf *bf)
47559352fb7aSAdrian Chadd {
47569352fb7aSAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
47579352fb7aSAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
47589352fb7aSAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
47599352fb7aSAdrian Chadd 	int sr, lr, pri;
47609352fb7aSAdrian Chadd 
47619352fb7aSAdrian Chadd 	if (ts->ts_status == 0) {
47629352fb7aSAdrian Chadd 		u_int8_t txant = ts->ts_antenna;
47639352fb7aSAdrian Chadd 		sc->sc_stats.ast_ant_tx[txant]++;
47649352fb7aSAdrian Chadd 		sc->sc_ant_tx[txant]++;
47659352fb7aSAdrian Chadd 		if (ts->ts_finaltsi != 0)
47669352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_altrate++;
47679352fb7aSAdrian Chadd 		pri = M_WME_GETAC(bf->bf_m);
47689352fb7aSAdrian Chadd 		if (pri >= WME_AC_VO)
47699352fb7aSAdrian Chadd 			ic->ic_wme.wme_hipri_traffic++;
4770875a9451SAdrian Chadd 		if ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0)
47719352fb7aSAdrian Chadd 			ni->ni_inact = ni->ni_inact_reload;
47729352fb7aSAdrian Chadd 	} else {
47739352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_XRETRY)
47749352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_xretries++;
47759352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_FIFO)
47769352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_fifoerr++;
47779352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_FILT)
47789352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_filtered++;
47799352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_XTXOP)
47809352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_xtxop++;
47819352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_TIMER_EXPIRED)
47829352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_timerexpired++;
47839352fb7aSAdrian Chadd 
47849352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TX_DATA_UNDERRUN)
47859352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_data_underrun++;
47869352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TX_DELIM_UNDERRUN)
47879352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_delim_underrun++;
47889352fb7aSAdrian Chadd 
47899352fb7aSAdrian Chadd 		if (bf->bf_m->m_flags & M_FF)
47909352fb7aSAdrian Chadd 			sc->sc_stats.ast_ff_txerr++;
47919352fb7aSAdrian Chadd 	}
47929352fb7aSAdrian Chadd 	/* XXX when is this valid? */
47939352fb7aSAdrian Chadd 	if (ts->ts_status & HAL_TX_DESC_CFG_ERR)
47949352fb7aSAdrian Chadd 		sc->sc_stats.ast_tx_desccfgerr++;
47959352fb7aSAdrian Chadd 
47969352fb7aSAdrian Chadd 	sr = ts->ts_shortretry;
47979352fb7aSAdrian Chadd 	lr = ts->ts_longretry;
47989352fb7aSAdrian Chadd 	sc->sc_stats.ast_tx_shortretry += sr;
47999352fb7aSAdrian Chadd 	sc->sc_stats.ast_tx_longretry += lr;
48009352fb7aSAdrian Chadd 
48019352fb7aSAdrian Chadd }
48029352fb7aSAdrian Chadd 
48039352fb7aSAdrian Chadd /*
48049352fb7aSAdrian Chadd  * The default completion. If fail is 1, this means
48059352fb7aSAdrian Chadd  * "please don't retry the frame, and just return -1 status
48069352fb7aSAdrian Chadd  * to the net80211 stack.
48079352fb7aSAdrian Chadd  */
48089352fb7aSAdrian Chadd void
48099352fb7aSAdrian Chadd ath_tx_default_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
48109352fb7aSAdrian Chadd {
48119352fb7aSAdrian Chadd 	struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
48129352fb7aSAdrian Chadd 	int st;
48139352fb7aSAdrian Chadd 
48149352fb7aSAdrian Chadd 	if (fail == 1)
48159352fb7aSAdrian Chadd 		st = -1;
48169352fb7aSAdrian Chadd 	else
4817875a9451SAdrian Chadd 		st = ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0) ?
48189352fb7aSAdrian Chadd 		    ts->ts_status : HAL_TXERR_XRETRY;
48199352fb7aSAdrian Chadd 
48209352fb7aSAdrian Chadd 	if (bf->bf_state.bfs_dobaw)
48219352fb7aSAdrian Chadd 		device_printf(sc->sc_dev,
4822a66d5089SAdrian Chadd 		    "%s: bf %p: seqno %d: dobaw should've been cleared!\n",
4823a66d5089SAdrian Chadd 		    __func__,
4824a66d5089SAdrian Chadd 		    bf,
4825a66d5089SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
48269352fb7aSAdrian Chadd 	if (bf->bf_next != NULL)
48279352fb7aSAdrian Chadd 		device_printf(sc->sc_dev,
4828a66d5089SAdrian Chadd 		    "%s: bf %p: seqno %d: bf_next not NULL!\n",
4829a66d5089SAdrian Chadd 		    __func__,
4830a66d5089SAdrian Chadd 		    bf,
4831a66d5089SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
48329352fb7aSAdrian Chadd 
48339352fb7aSAdrian Chadd 	/*
48349352fb7aSAdrian Chadd 	 * Do any tx complete callback.  Note this must
48359352fb7aSAdrian Chadd 	 * be done before releasing the node reference.
48369352fb7aSAdrian Chadd 	 * This will free the mbuf, release the net80211
48379352fb7aSAdrian Chadd 	 * node and recycle the ath_buf.
48389352fb7aSAdrian Chadd 	 */
48399352fb7aSAdrian Chadd 	ath_tx_freebuf(sc, bf, st);
48409352fb7aSAdrian Chadd }
48419352fb7aSAdrian Chadd 
48429352fb7aSAdrian Chadd /*
4843eb6f0de0SAdrian Chadd  * Update rate control with the given completion status.
4844eb6f0de0SAdrian Chadd  */
4845eb6f0de0SAdrian Chadd void
4846eb6f0de0SAdrian Chadd ath_tx_update_ratectrl(struct ath_softc *sc, struct ieee80211_node *ni,
4847eb6f0de0SAdrian Chadd     struct ath_rc_series *rc, struct ath_tx_status *ts, int frmlen,
4848eb6f0de0SAdrian Chadd     int nframes, int nbad)
4849eb6f0de0SAdrian Chadd {
4850eb6f0de0SAdrian Chadd 	struct ath_node *an;
4851eb6f0de0SAdrian Chadd 
4852eb6f0de0SAdrian Chadd 	/* Only for unicast frames */
4853eb6f0de0SAdrian Chadd 	if (ni == NULL)
4854eb6f0de0SAdrian Chadd 		return;
4855eb6f0de0SAdrian Chadd 
4856eb6f0de0SAdrian Chadd 	an = ATH_NODE(ni);
4857eb6f0de0SAdrian Chadd 
4858eb6f0de0SAdrian Chadd 	if ((ts->ts_status & HAL_TXERR_FILT) == 0) {
4859eb6f0de0SAdrian Chadd 		ATH_NODE_LOCK(an);
4860eb6f0de0SAdrian Chadd 		ath_rate_tx_complete(sc, an, rc, ts, frmlen, nframes, nbad);
4861eb6f0de0SAdrian Chadd 		ATH_NODE_UNLOCK(an);
4862eb6f0de0SAdrian Chadd 	}
4863eb6f0de0SAdrian Chadd }
4864eb6f0de0SAdrian Chadd 
4865eb6f0de0SAdrian Chadd /*
48669352fb7aSAdrian Chadd  * Update the busy status of the last frame on the free list.
48679352fb7aSAdrian Chadd  * When doing TDMA, the busy flag tracks whether the hardware
48689352fb7aSAdrian Chadd  * currently points to this buffer or not, and thus gated DMA
48699352fb7aSAdrian Chadd  * may restart by re-reading the last descriptor in this
48709352fb7aSAdrian Chadd  * buffer.
48719352fb7aSAdrian Chadd  *
48729352fb7aSAdrian Chadd  * This should be called in the completion function once one
48739352fb7aSAdrian Chadd  * of the buffers has been used.
48749352fb7aSAdrian Chadd  */
48759352fb7aSAdrian Chadd static void
48769352fb7aSAdrian Chadd ath_tx_update_busy(struct ath_softc *sc)
48779352fb7aSAdrian Chadd {
48789352fb7aSAdrian Chadd 	struct ath_buf *last;
48799352fb7aSAdrian Chadd 
48809352fb7aSAdrian Chadd 	/*
48819352fb7aSAdrian Chadd 	 * Since the last frame may still be marked
48829352fb7aSAdrian Chadd 	 * as ATH_BUF_BUSY, unmark it here before
48839352fb7aSAdrian Chadd 	 * finishing the frame processing.
48849352fb7aSAdrian Chadd 	 * Since we've completed a frame (aggregate
48859352fb7aSAdrian Chadd 	 * or otherwise), the hardware has moved on
48869352fb7aSAdrian Chadd 	 * and is no longer referencing the previous
48879352fb7aSAdrian Chadd 	 * descriptor.
48889352fb7aSAdrian Chadd 	 */
48899352fb7aSAdrian Chadd 	ATH_TXBUF_LOCK_ASSERT(sc);
48909352fb7aSAdrian Chadd 	last = TAILQ_LAST(&sc->sc_txbuf, ath_bufhead_s);
48919352fb7aSAdrian Chadd 	if (last != NULL)
48929352fb7aSAdrian Chadd 		last->bf_flags &= ~ATH_BUF_BUSY;
48939352fb7aSAdrian Chadd }
48949352fb7aSAdrian Chadd 
48959352fb7aSAdrian Chadd 
489668e8e04eSSam Leffler /*
4897c42a7b7eSSam Leffler  * Process completed xmit descriptors from the specified queue.
4898eb6f0de0SAdrian Chadd  * Kick the packet scheduler if needed. This can occur from this
4899eb6f0de0SAdrian Chadd  * particular task.
4900c42a7b7eSSam Leffler  */
4901d7736e13SSam Leffler static int
490296ff485dSAdrian Chadd ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq, int dosched)
49035591b213SSam Leffler {
49045591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
49059352fb7aSAdrian Chadd 	struct ath_buf *bf;
49066edf1dc7SAdrian Chadd 	struct ath_desc *ds;
490765f9edeeSSam Leffler 	struct ath_tx_status *ts;
49085591b213SSam Leffler 	struct ieee80211_node *ni;
4909eb6f0de0SAdrian Chadd 	struct ath_node *an;
491053e98d5aSAdrian Chadd #ifdef	IEEE80211_SUPPORT_SUPERG
491143faa6b2SAdrian Chadd 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
491253e98d5aSAdrian Chadd #endif	/* IEEE80211_SUPPORT_SUPERG */
49139352fb7aSAdrian Chadd 	int nacked;
49145591b213SSam Leffler 	HAL_STATUS status;
49155591b213SSam Leffler 
4916c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_TX_PROC, "%s: tx queue %u head %p link %p\n",
4917c42a7b7eSSam Leffler 		__func__, txq->axq_qnum,
4918c42a7b7eSSam Leffler 		(caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
4919c42a7b7eSSam Leffler 		txq->axq_link);
4920d7736e13SSam Leffler 	nacked = 0;
49215591b213SSam Leffler 	for (;;) {
4922c42a7b7eSSam Leffler 		ATH_TXQ_LOCK(txq);
4923c42a7b7eSSam Leffler 		txq->axq_intrcnt = 0;	/* reset periodic desc intr count */
49246b349e5aSAdrian Chadd 		bf = TAILQ_FIRST(&txq->axq_q);
49255591b213SSam Leffler 		if (bf == NULL) {
4926c42a7b7eSSam Leffler 			ATH_TXQ_UNLOCK(txq);
49275591b213SSam Leffler 			break;
49285591b213SSam Leffler 		}
49296edf1dc7SAdrian Chadd 		ds = bf->bf_lastds;	/* XXX must be setup correctly! */
493065f9edeeSSam Leffler 		ts = &bf->bf_status.ds_txstat;
493165f9edeeSSam Leffler 		status = ath_hal_txprocdesc(ah, ds, ts);
4932a585a9a1SSam Leffler #ifdef ATH_DEBUG
4933c42a7b7eSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_XMIT_DESC)
49346902009eSSam Leffler 			ath_printtxbuf(sc, bf, txq->axq_qnum, 0,
49356902009eSSam Leffler 			    status == HAL_OK);
4936d6b20023SAdrian Chadd 		else if ((sc->sc_debug & ATH_DEBUG_RESET) && (dosched == 0)) {
4937d6b20023SAdrian Chadd 			ath_printtxbuf(sc, bf, txq->axq_qnum, 0,
4938d6b20023SAdrian Chadd 			    status == HAL_OK);
4939d6b20023SAdrian Chadd 		}
49405591b213SSam Leffler #endif
49415591b213SSam Leffler 		if (status == HAL_EINPROGRESS) {
4942c42a7b7eSSam Leffler 			ATH_TXQ_UNLOCK(txq);
49435591b213SSam Leffler 			break;
49445591b213SSam Leffler 		}
49456b349e5aSAdrian Chadd 		ATH_TXQ_REMOVE(txq, bf, bf_list);
4946584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
494710ad9a77SSam Leffler 		if (txq->axq_depth > 0) {
494810ad9a77SSam Leffler 			/*
494910ad9a77SSam Leffler 			 * More frames follow.  Mark the buffer busy
495010ad9a77SSam Leffler 			 * so it's not re-used while the hardware may
495110ad9a77SSam Leffler 			 * still re-read the link field in the descriptor.
49526edf1dc7SAdrian Chadd 			 *
49536edf1dc7SAdrian Chadd 			 * Use the last buffer in an aggregate as that
49546edf1dc7SAdrian Chadd 			 * is where the hardware may be - intermediate
49556edf1dc7SAdrian Chadd 			 * descriptors won't be "busy".
495610ad9a77SSam Leffler 			 */
49576edf1dc7SAdrian Chadd 			bf->bf_last->bf_flags |= ATH_BUF_BUSY;
495810ad9a77SSam Leffler 		} else
495910ad9a77SSam Leffler #else
4960ebecf802SSam Leffler 		if (txq->axq_depth == 0)
496110ad9a77SSam Leffler #endif
49621539af1eSSam Leffler 			txq->axq_link = NULL;
49636edf1dc7SAdrian Chadd 		if (bf->bf_state.bfs_aggr)
49646edf1dc7SAdrian Chadd 			txq->axq_aggr_depth--;
49655591b213SSam Leffler 
49665591b213SSam Leffler 		ni = bf->bf_node;
4967c42a7b7eSSam Leffler 		/*
49689352fb7aSAdrian Chadd 		 * If unicast frame was ack'd update RSSI,
496984784be1SSam Leffler 		 * including the last rx time used to
497084784be1SSam Leffler 		 * workaround phantom bmiss interrupts.
4971d7736e13SSam Leffler 		 */
49729352fb7aSAdrian Chadd 		if (ni != NULL && ts->ts_status == 0 &&
4973875a9451SAdrian Chadd 		    ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0)) {
4974d7736e13SSam Leffler 			nacked++;
497584784be1SSam Leffler 			sc->sc_stats.ast_tx_rssi = ts->ts_rssi;
497684784be1SSam Leffler 			ATH_RSSI_LPF(sc->sc_halstats.ns_avgtxrssi,
497784784be1SSam Leffler 				ts->ts_rssi);
497884784be1SSam Leffler 		}
49799352fb7aSAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
49809352fb7aSAdrian Chadd 
49819352fb7aSAdrian Chadd 		/* If unicast frame, update general statistics */
49829352fb7aSAdrian Chadd 		if (ni != NULL) {
4983eb6f0de0SAdrian Chadd 			an = ATH_NODE(ni);
49849352fb7aSAdrian Chadd 			/* update statistics */
49859352fb7aSAdrian Chadd 			ath_tx_update_stats(sc, ts, bf);
4986d7736e13SSam Leffler 		}
49879352fb7aSAdrian Chadd 
49880a915fadSSam Leffler 		/*
49899352fb7aSAdrian Chadd 		 * Call the completion handler.
49909352fb7aSAdrian Chadd 		 * The completion handler is responsible for
49919352fb7aSAdrian Chadd 		 * calling the rate control code.
49929352fb7aSAdrian Chadd 		 *
49939352fb7aSAdrian Chadd 		 * Frames with no completion handler get the
49949352fb7aSAdrian Chadd 		 * rate control code called here.
499568e8e04eSSam Leffler 		 */
49969352fb7aSAdrian Chadd 		if (bf->bf_comp == NULL) {
49979352fb7aSAdrian Chadd 			if ((ts->ts_status & HAL_TXERR_FILT) == 0 &&
4998875a9451SAdrian Chadd 			    (bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0) {
49999352fb7aSAdrian Chadd 				/*
50009352fb7aSAdrian Chadd 				 * XXX assume this isn't an aggregate
50019352fb7aSAdrian Chadd 				 * frame.
50029352fb7aSAdrian Chadd 				 */
5003eb6f0de0SAdrian Chadd 				ath_tx_update_ratectrl(sc, ni,
5004eb6f0de0SAdrian Chadd 				     bf->bf_state.bfs_rc, ts,
5005eb6f0de0SAdrian Chadd 				    bf->bf_state.bfs_pktlen, 1,
5006eb6f0de0SAdrian Chadd 				    (ts->ts_status == 0 ? 0 : 1));
50075591b213SSam Leffler 			}
50089352fb7aSAdrian Chadd 			ath_tx_default_comp(sc, bf, 0);
50099352fb7aSAdrian Chadd 		} else
50109352fb7aSAdrian Chadd 			bf->bf_comp(sc, bf, 0);
50115591b213SSam Leffler 	}
5012339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
501368e8e04eSSam Leffler 	/*
501468e8e04eSSam Leffler 	 * Flush fast-frame staging queue when traffic slows.
501568e8e04eSSam Leffler 	 */
501668e8e04eSSam Leffler 	if (txq->axq_depth <= 1)
501704f19fd6SSam Leffler 		ieee80211_ff_flush(ic, txq->axq_ac);
5018339ccfb3SSam Leffler #endif
5019eb6f0de0SAdrian Chadd 
5020eb6f0de0SAdrian Chadd 	/* Kick the TXQ scheduler */
5021eb6f0de0SAdrian Chadd 	if (dosched) {
5022eb6f0de0SAdrian Chadd 		ATH_TXQ_LOCK(txq);
5023eb6f0de0SAdrian Chadd 		ath_txq_sched(sc, txq);
5024eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
5025eb6f0de0SAdrian Chadd 	}
5026eb6f0de0SAdrian Chadd 
5027d7736e13SSam Leffler 	return nacked;
5028d7736e13SSam Leffler }
5029d7736e13SSam Leffler 
50308f939e79SAdrian Chadd #define	TXQACTIVE(t, q)		( (t) & (1 << (q)))
5031c42a7b7eSSam Leffler 
5032c42a7b7eSSam Leffler /*
5033c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
5034c42a7b7eSSam Leffler  * for a single hardware transmit queue (e.g. 5210 and 5211).
5035c42a7b7eSSam Leffler  */
5036c42a7b7eSSam Leffler static void
5037c42a7b7eSSam Leffler ath_tx_proc_q0(void *arg, int npending)
5038c42a7b7eSSam Leffler {
5039c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
5040fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
50418f939e79SAdrian Chadd 	uint32_t txqs;
5042c42a7b7eSSam Leffler 
5043ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
5044ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
50458f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
50468f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
5047ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
50488f939e79SAdrian Chadd 
504996ff485dSAdrian Chadd 	if (TXQACTIVE(txqs, 0) && ath_tx_processq(sc, &sc->sc_txq[0], 1))
50508f939e79SAdrian Chadd 		/* XXX why is lastrx updated in tx code? */
5051d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
50528f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum))
505396ff485dSAdrian Chadd 		ath_tx_processq(sc, sc->sc_cabq, 1);
5054e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
505513f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
5056e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
50572e986da5SSam Leffler 	sc->sc_wd_timer = 0;
50585591b213SSam Leffler 
50593e50ec2cSSam Leffler 	if (sc->sc_softled)
506046d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
50613e50ec2cSSam Leffler 
5062ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
5063ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
5064ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
5065ef27340cSAdrian Chadd 
50665591b213SSam Leffler 	ath_start(ifp);
50675591b213SSam Leffler }
50685591b213SSam Leffler 
50695591b213SSam Leffler /*
5070c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
5071c42a7b7eSSam Leffler  * for four hardware queues, 0-3 (e.g. 5212 w/ WME support).
50725591b213SSam Leffler  */
50735591b213SSam Leffler static void
5074c42a7b7eSSam Leffler ath_tx_proc_q0123(void *arg, int npending)
5075c42a7b7eSSam Leffler {
5076c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
5077fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
5078d7736e13SSam Leffler 	int nacked;
50798f939e79SAdrian Chadd 	uint32_t txqs;
50808f939e79SAdrian Chadd 
5081ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
5082ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
50838f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
50848f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
5085ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
5086c42a7b7eSSam Leffler 
5087c42a7b7eSSam Leffler 	/*
5088c42a7b7eSSam Leffler 	 * Process each active queue.
5089c42a7b7eSSam Leffler 	 */
5090d7736e13SSam Leffler 	nacked = 0;
50918f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 0))
509296ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[0], 1);
50938f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 1))
509496ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[1], 1);
50958f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 2))
509696ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[2], 1);
50978f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 3))
509896ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[3], 1);
50998f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum))
510096ff485dSAdrian Chadd 		ath_tx_processq(sc, sc->sc_cabq, 1);
5101d7736e13SSam Leffler 	if (nacked)
5102d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
5103c42a7b7eSSam Leffler 
5104e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
510513f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
5106e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
51072e986da5SSam Leffler 	sc->sc_wd_timer = 0;
5108c42a7b7eSSam Leffler 
51093e50ec2cSSam Leffler 	if (sc->sc_softled)
511046d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
51113e50ec2cSSam Leffler 
5112ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
5113ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
5114ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
5115ef27340cSAdrian Chadd 
5116c42a7b7eSSam Leffler 	ath_start(ifp);
5117c42a7b7eSSam Leffler }
5118c42a7b7eSSam Leffler 
5119c42a7b7eSSam Leffler /*
5120c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt.
5121c42a7b7eSSam Leffler  */
5122c42a7b7eSSam Leffler static void
5123c42a7b7eSSam Leffler ath_tx_proc(void *arg, int npending)
5124c42a7b7eSSam Leffler {
5125c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
5126fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
5127d7736e13SSam Leffler 	int i, nacked;
51288f939e79SAdrian Chadd 	uint32_t txqs;
51298f939e79SAdrian Chadd 
5130ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
5131ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
51328f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
51338f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
5134ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
5135c42a7b7eSSam Leffler 
5136c42a7b7eSSam Leffler 	/*
5137c42a7b7eSSam Leffler 	 * Process each active queue.
5138c42a7b7eSSam Leffler 	 */
5139d7736e13SSam Leffler 	nacked = 0;
5140c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
51418f939e79SAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i) && TXQACTIVE(txqs, i))
514296ff485dSAdrian Chadd 			nacked += ath_tx_processq(sc, &sc->sc_txq[i], 1);
5143d7736e13SSam Leffler 	if (nacked)
5144d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
5145c42a7b7eSSam Leffler 
5146ef27340cSAdrian Chadd 	/* XXX check this inside of IF_LOCK? */
5147e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
514813f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
5149e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
51502e986da5SSam Leffler 	sc->sc_wd_timer = 0;
5151c42a7b7eSSam Leffler 
51523e50ec2cSSam Leffler 	if (sc->sc_softled)
515346d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
51543e50ec2cSSam Leffler 
5155ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
5156ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
5157ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
5158ef27340cSAdrian Chadd 
5159c42a7b7eSSam Leffler 	ath_start(ifp);
5160c42a7b7eSSam Leffler }
516116d4de92SAdrian Chadd #undef	TXQACTIVE
5162c42a7b7eSSam Leffler 
51639352fb7aSAdrian Chadd /*
516403e9308fSAdrian Chadd  * Deferred processing of TXQ rescheduling.
516503e9308fSAdrian Chadd  */
516603e9308fSAdrian Chadd static void
516703e9308fSAdrian Chadd ath_txq_sched_tasklet(void *arg, int npending)
516803e9308fSAdrian Chadd {
516903e9308fSAdrian Chadd 	struct ath_softc *sc = arg;
517003e9308fSAdrian Chadd 	int i;
517103e9308fSAdrian Chadd 
517203e9308fSAdrian Chadd 	/* XXX is skipping ok? */
517303e9308fSAdrian Chadd 	ATH_PCU_LOCK(sc);
517403e9308fSAdrian Chadd #if 0
517503e9308fSAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
517603e9308fSAdrian Chadd 		device_printf(sc->sc_dev,
517703e9308fSAdrian Chadd 		    "%s: sc_inreset_cnt > 0; skipping\n", __func__);
517803e9308fSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
517903e9308fSAdrian Chadd 		return;
518003e9308fSAdrian Chadd 	}
518103e9308fSAdrian Chadd #endif
518203e9308fSAdrian Chadd 	sc->sc_txproc_cnt++;
518303e9308fSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
518403e9308fSAdrian Chadd 
518503e9308fSAdrian Chadd 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
5186b5a9dfd5SAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i)) {
5187b5a9dfd5SAdrian Chadd 			ATH_TXQ_LOCK(&sc->sc_txq[i]);
518803e9308fSAdrian Chadd 			ath_txq_sched(sc, &sc->sc_txq[i]);
5189b5a9dfd5SAdrian Chadd 			ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
5190b5a9dfd5SAdrian Chadd 		}
519103e9308fSAdrian Chadd 	}
519203e9308fSAdrian Chadd 
519303e9308fSAdrian Chadd 	ATH_PCU_LOCK(sc);
519403e9308fSAdrian Chadd 	sc->sc_txproc_cnt--;
519503e9308fSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
519603e9308fSAdrian Chadd }
519703e9308fSAdrian Chadd 
519803e9308fSAdrian Chadd /*
51999352fb7aSAdrian Chadd  * Return a buffer to the pool and update the 'busy' flag on the
52009352fb7aSAdrian Chadd  * previous 'tail' entry.
52019352fb7aSAdrian Chadd  *
52029352fb7aSAdrian Chadd  * This _must_ only be called when the buffer is involved in a completed
52039352fb7aSAdrian Chadd  * TX. The logic is that if it was part of an active TX, the previous
52049352fb7aSAdrian Chadd  * buffer on the list is now not involved in a halted TX DMA queue, waiting
52059352fb7aSAdrian Chadd  * for restart (eg for TDMA.)
52069352fb7aSAdrian Chadd  *
52079352fb7aSAdrian Chadd  * The caller must free the mbuf and recycle the node reference.
52089352fb7aSAdrian Chadd  */
52099352fb7aSAdrian Chadd void
52109352fb7aSAdrian Chadd ath_freebuf(struct ath_softc *sc, struct ath_buf *bf)
52119352fb7aSAdrian Chadd {
52129352fb7aSAdrian Chadd 	bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
52139352fb7aSAdrian Chadd 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_POSTWRITE);
52149352fb7aSAdrian Chadd 
52159352fb7aSAdrian Chadd 	KASSERT((bf->bf_node == NULL), ("%s: bf->bf_node != NULL\n", __func__));
52169352fb7aSAdrian Chadd 	KASSERT((bf->bf_m == NULL), ("%s: bf->bf_m != NULL\n", __func__));
52179352fb7aSAdrian Chadd 
52189352fb7aSAdrian Chadd 	ATH_TXBUF_LOCK(sc);
52199352fb7aSAdrian Chadd 	ath_tx_update_busy(sc);
52209352fb7aSAdrian Chadd 	TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
52219352fb7aSAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
52229352fb7aSAdrian Chadd }
52239352fb7aSAdrian Chadd 
52249352fb7aSAdrian Chadd /*
52259352fb7aSAdrian Chadd  * This is currently used by ath_tx_draintxq() and
52269352fb7aSAdrian Chadd  * ath_tx_tid_free_pkts().
52279352fb7aSAdrian Chadd  *
52289352fb7aSAdrian Chadd  * It recycles a single ath_buf.
52299352fb7aSAdrian Chadd  */
52309352fb7aSAdrian Chadd void
52319352fb7aSAdrian Chadd ath_tx_freebuf(struct ath_softc *sc, struct ath_buf *bf, int status)
52329352fb7aSAdrian Chadd {
52339352fb7aSAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
52349352fb7aSAdrian Chadd 	struct mbuf *m0 = bf->bf_m;
52359352fb7aSAdrian Chadd 
52369352fb7aSAdrian Chadd 	bf->bf_node = NULL;
52379352fb7aSAdrian Chadd 	bf->bf_m = NULL;
52389352fb7aSAdrian Chadd 
52399352fb7aSAdrian Chadd 	/* Free the buffer, it's not needed any longer */
52409352fb7aSAdrian Chadd 	ath_freebuf(sc, bf);
52419352fb7aSAdrian Chadd 
52429352fb7aSAdrian Chadd 	if (ni != NULL) {
52439352fb7aSAdrian Chadd 		/*
52449352fb7aSAdrian Chadd 		 * Do any callback and reclaim the node reference.
52459352fb7aSAdrian Chadd 		 */
52469352fb7aSAdrian Chadd 		if (m0->m_flags & M_TXCB)
52479352fb7aSAdrian Chadd 			ieee80211_process_callback(ni, m0, status);
52489352fb7aSAdrian Chadd 		ieee80211_free_node(ni);
52499352fb7aSAdrian Chadd 	}
52509352fb7aSAdrian Chadd 	m_freem(m0);
52519352fb7aSAdrian Chadd 
52529352fb7aSAdrian Chadd 	/*
52539352fb7aSAdrian Chadd 	 * XXX the buffer used to be freed -after-, but the DMA map was
52549352fb7aSAdrian Chadd 	 * freed where ath_freebuf() now is. I've no idea what this
52559352fb7aSAdrian Chadd 	 * will do.
52569352fb7aSAdrian Chadd 	 */
52579352fb7aSAdrian Chadd }
52589352fb7aSAdrian Chadd 
52599352fb7aSAdrian Chadd void
5260c42a7b7eSSam Leffler ath_tx_draintxq(struct ath_softc *sc, struct ath_txq *txq)
52615591b213SSam Leffler {
5262a585a9a1SSam Leffler #ifdef ATH_DEBUG
52635591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5264d2f6ed15SSam Leffler #endif
52655591b213SSam Leffler 	struct ath_buf *bf;
52667a4c5ed9SSam Leffler 	u_int ix;
52675591b213SSam Leffler 
5268c42a7b7eSSam Leffler 	/*
5269c42a7b7eSSam Leffler 	 * NB: this assumes output has been stopped and
52705d61b5e8SSam Leffler 	 *     we do not need to block ath_tx_proc
5271c42a7b7eSSam Leffler 	 */
527210ad9a77SSam Leffler 	ATH_TXBUF_LOCK(sc);
52736b349e5aSAdrian Chadd 	bf = TAILQ_LAST(&sc->sc_txbuf, ath_bufhead_s);
527410ad9a77SSam Leffler 	if (bf != NULL)
527510ad9a77SSam Leffler 		bf->bf_flags &= ~ATH_BUF_BUSY;
527610ad9a77SSam Leffler 	ATH_TXBUF_UNLOCK(sc);
52779352fb7aSAdrian Chadd 
52787a4c5ed9SSam Leffler 	for (ix = 0;; ix++) {
5279c42a7b7eSSam Leffler 		ATH_TXQ_LOCK(txq);
52806b349e5aSAdrian Chadd 		bf = TAILQ_FIRST(&txq->axq_q);
52815591b213SSam Leffler 		if (bf == NULL) {
5282ebecf802SSam Leffler 			txq->axq_link = NULL;
5283c42a7b7eSSam Leffler 			ATH_TXQ_UNLOCK(txq);
52845591b213SSam Leffler 			break;
52855591b213SSam Leffler 		}
52866b349e5aSAdrian Chadd 		ATH_TXQ_REMOVE(txq, bf, bf_list);
52876edf1dc7SAdrian Chadd 		if (bf->bf_state.bfs_aggr)
52886edf1dc7SAdrian Chadd 			txq->axq_aggr_depth--;
5289a585a9a1SSam Leffler #ifdef ATH_DEBUG
52904a3ac3fcSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_RESET) {
5291b032f27cSSam Leffler 			struct ieee80211com *ic = sc->sc_ifp->if_l2com;
5292b032f27cSSam Leffler 
52936902009eSSam Leffler 			ath_printtxbuf(sc, bf, txq->axq_qnum, ix,
52946edf1dc7SAdrian Chadd 				ath_hal_txprocdesc(ah, bf->bf_lastds,
529565f9edeeSSam Leffler 				    &bf->bf_status.ds_txstat) == HAL_OK);
5296e40b6ab1SSam Leffler 			ieee80211_dump_pkt(ic, mtod(bf->bf_m, const uint8_t *),
52974a3ac3fcSSam Leffler 			    bf->bf_m->m_len, 0, -1);
52984a3ac3fcSSam Leffler 		}
5299a585a9a1SSam Leffler #endif /* ATH_DEBUG */
530023428eafSSam Leffler 		/*
53019352fb7aSAdrian Chadd 		 * Since we're now doing magic in the completion
53029352fb7aSAdrian Chadd 		 * functions, we -must- call it for aggregation
53039352fb7aSAdrian Chadd 		 * destinations or BAW tracking will get upset.
530423428eafSSam Leffler 		 */
53059352fb7aSAdrian Chadd 		/*
53069352fb7aSAdrian Chadd 		 * Clear ATH_BUF_BUSY; the completion handler
53079352fb7aSAdrian Chadd 		 * will free the buffer.
53089352fb7aSAdrian Chadd 		 */
53099352fb7aSAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
531010ad9a77SSam Leffler 		bf->bf_flags &= ~ATH_BUF_BUSY;
53119352fb7aSAdrian Chadd 		if (bf->bf_comp)
53129352fb7aSAdrian Chadd 			bf->bf_comp(sc, bf, 1);
53139352fb7aSAdrian Chadd 		else
53149352fb7aSAdrian Chadd 			ath_tx_default_comp(sc, bf, 1);
53155591b213SSam Leffler 	}
53169352fb7aSAdrian Chadd 
5317eb6f0de0SAdrian Chadd 	/*
5318eb6f0de0SAdrian Chadd 	 * Drain software queued frames which are on
5319eb6f0de0SAdrian Chadd 	 * active TIDs.
5320eb6f0de0SAdrian Chadd 	 */
5321eb6f0de0SAdrian Chadd 	ath_tx_txq_drain(sc, txq);
5322c42a7b7eSSam Leffler }
5323c42a7b7eSSam Leffler 
5324c42a7b7eSSam Leffler static void
5325c42a7b7eSSam Leffler ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
5326c42a7b7eSSam Leffler {
5327c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5328c42a7b7eSSam Leffler 
5329c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n",
5330c42a7b7eSSam Leffler 	    __func__, txq->axq_qnum,
53316891c875SPeter Wemm 	    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, txq->axq_qnum),
53326891c875SPeter Wemm 	    txq->axq_link);
53334a3ac3fcSSam Leffler 	(void) ath_hal_stoptxdma(ah, txq->axq_qnum);
5334c42a7b7eSSam Leffler }
5335c42a7b7eSSam Leffler 
53362d433424SAdrian Chadd static int
53372d433424SAdrian Chadd ath_stoptxdma(struct ath_softc *sc)
5338c42a7b7eSSam Leffler {
5339c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5340c42a7b7eSSam Leffler 	int i;
5341c42a7b7eSSam Leffler 
5342c42a7b7eSSam Leffler 	/* XXX return value */
53432d433424SAdrian Chadd 	if (sc->sc_invalid)
53442d433424SAdrian Chadd 		return 0;
53452d433424SAdrian Chadd 
5346c42a7b7eSSam Leffler 	if (!sc->sc_invalid) {
5347c42a7b7eSSam Leffler 		/* don't touch the hardware if marked invalid */
53484a3ac3fcSSam Leffler 		DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n",
53494a3ac3fcSSam Leffler 		    __func__, sc->sc_bhalq,
53504a3ac3fcSSam Leffler 		    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq),
53514a3ac3fcSSam Leffler 		    NULL);
5352c42a7b7eSSam Leffler 		(void) ath_hal_stoptxdma(ah, sc->sc_bhalq);
5353c42a7b7eSSam Leffler 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
5354c42a7b7eSSam Leffler 			if (ATH_TXQ_SETUP(sc, i))
5355c42a7b7eSSam Leffler 				ath_tx_stopdma(sc, &sc->sc_txq[i]);
5356c42a7b7eSSam Leffler 	}
53572d433424SAdrian Chadd 
53582d433424SAdrian Chadd 	return 1;
53592d433424SAdrian Chadd }
53602d433424SAdrian Chadd 
53612d433424SAdrian Chadd /*
53622d433424SAdrian Chadd  * Drain the transmit queues and reclaim resources.
53632d433424SAdrian Chadd  */
53642d433424SAdrian Chadd static void
53652d433424SAdrian Chadd ath_draintxq(struct ath_softc *sc, ATH_RESET_TYPE reset_type)
53662d433424SAdrian Chadd {
53672d433424SAdrian Chadd #ifdef	ATH_DEBUG
53682d433424SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
53692d433424SAdrian Chadd #endif
53702d433424SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
53712d433424SAdrian Chadd 	int i;
53722d433424SAdrian Chadd 
53732d433424SAdrian Chadd 	(void) ath_stoptxdma(sc);
53742d433424SAdrian Chadd 
5375ef27340cSAdrian Chadd 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
5376ef27340cSAdrian Chadd 		/*
5377ef27340cSAdrian Chadd 		 * XXX TODO: should we just handle the completed TX frames
5378ef27340cSAdrian Chadd 		 * here, whether or not the reset is a full one or not?
5379ef27340cSAdrian Chadd 		 */
5380ef27340cSAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i)) {
5381ef27340cSAdrian Chadd 			if (reset_type == ATH_RESET_NOLOSS)
5382ef27340cSAdrian Chadd 				ath_tx_processq(sc, &sc->sc_txq[i], 0);
5383ef27340cSAdrian Chadd 			else
5384c42a7b7eSSam Leffler 				ath_tx_draintxq(sc, &sc->sc_txq[i]);
5385ef27340cSAdrian Chadd 		}
5386ef27340cSAdrian Chadd 	}
53874a3ac3fcSSam Leffler #ifdef ATH_DEBUG
53884a3ac3fcSSam Leffler 	if (sc->sc_debug & ATH_DEBUG_RESET) {
53896b349e5aSAdrian Chadd 		struct ath_buf *bf = TAILQ_FIRST(&sc->sc_bbuf);
53904a3ac3fcSSam Leffler 		if (bf != NULL && bf->bf_m != NULL) {
53916902009eSSam Leffler 			ath_printtxbuf(sc, bf, sc->sc_bhalq, 0,
53926edf1dc7SAdrian Chadd 				ath_hal_txprocdesc(ah, bf->bf_lastds,
539365f9edeeSSam Leffler 				    &bf->bf_status.ds_txstat) == HAL_OK);
5394e40b6ab1SSam Leffler 			ieee80211_dump_pkt(ifp->if_l2com,
5395e40b6ab1SSam Leffler 			    mtod(bf->bf_m, const uint8_t *), bf->bf_m->m_len,
5396e40b6ab1SSam Leffler 			    0, -1);
53974a3ac3fcSSam Leffler 		}
53984a3ac3fcSSam Leffler 	}
53994a3ac3fcSSam Leffler #endif /* ATH_DEBUG */
5400e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
540113f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
5402e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
54032e986da5SSam Leffler 	sc->sc_wd_timer = 0;
54045591b213SSam Leffler }
54055591b213SSam Leffler 
54065591b213SSam Leffler /*
54075591b213SSam Leffler  * Disable the receive h/w in preparation for a reset.
54085591b213SSam Leffler  */
54095591b213SSam Leffler static void
54109a842e8bSAdrian Chadd ath_stoprecv(struct ath_softc *sc, int dodelay)
54115591b213SSam Leffler {
54128cec0ab9SSam Leffler #define	PA2DESC(_sc, _pa) \
5413c42a7b7eSSam Leffler 	((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
5414c42a7b7eSSam Leffler 		((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
54155591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
54165591b213SSam Leffler 
54175591b213SSam Leffler 	ath_hal_stoppcurecv(ah);	/* disable PCU */
54185591b213SSam Leffler 	ath_hal_setrxfilter(ah, 0);	/* clear recv filter */
54195591b213SSam Leffler 	ath_hal_stopdmarecv(ah);	/* disable DMA engine */
54209a842e8bSAdrian Chadd 	if (dodelay)
5421c42a7b7eSSam Leffler 		DELAY(3000);		/* 3ms is long enough for 1 frame */
5422a585a9a1SSam Leffler #ifdef ATH_DEBUG
5423c42a7b7eSSam Leffler 	if (sc->sc_debug & (ATH_DEBUG_RESET | ATH_DEBUG_FATAL)) {
54245591b213SSam Leffler 		struct ath_buf *bf;
54257a4c5ed9SSam Leffler 		u_int ix;
54265591b213SSam Leffler 
5427a66d5089SAdrian Chadd 		device_printf(sc->sc_dev,
5428a66d5089SAdrian Chadd 		    "%s: rx queue %p, link %p\n",
5429a66d5089SAdrian Chadd 		    __func__,
5430a66d5089SAdrian Chadd 		    (caddr_t)(uintptr_t) ath_hal_getrxbuf(ah),
5431a66d5089SAdrian Chadd 		    sc->sc_rxlink);
54327a4c5ed9SSam Leffler 		ix = 0;
54336b349e5aSAdrian Chadd 		TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
54348cec0ab9SSam Leffler 			struct ath_desc *ds = bf->bf_desc;
543565f9edeeSSam Leffler 			struct ath_rx_status *rs = &bf->bf_status.ds_rxstat;
5436c42a7b7eSSam Leffler 			HAL_STATUS status = ath_hal_rxprocdesc(ah, ds,
543765f9edeeSSam Leffler 				bf->bf_daddr, PA2DESC(sc, ds->ds_link), rs);
5438c42a7b7eSSam Leffler 			if (status == HAL_OK || (sc->sc_debug & ATH_DEBUG_FATAL))
54396902009eSSam Leffler 				ath_printrxbuf(sc, bf, ix, status == HAL_OK);
54407a4c5ed9SSam Leffler 			ix++;
54415591b213SSam Leffler 		}
54425591b213SSam Leffler 	}
54435591b213SSam Leffler #endif
544468e8e04eSSam Leffler 	if (sc->sc_rxpending != NULL) {
544568e8e04eSSam Leffler 		m_freem(sc->sc_rxpending);
544668e8e04eSSam Leffler 		sc->sc_rxpending = NULL;
544768e8e04eSSam Leffler 	}
54485591b213SSam Leffler 	sc->sc_rxlink = NULL;		/* just in case */
54498cec0ab9SSam Leffler #undef PA2DESC
54505591b213SSam Leffler }
54515591b213SSam Leffler 
54525591b213SSam Leffler /*
54535591b213SSam Leffler  * Enable the receive h/w following a reset.
54545591b213SSam Leffler  */
54555591b213SSam Leffler static int
54565591b213SSam Leffler ath_startrecv(struct ath_softc *sc)
54575591b213SSam Leffler {
54585591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
54595591b213SSam Leffler 	struct ath_buf *bf;
54605591b213SSam Leffler 
54615591b213SSam Leffler 	sc->sc_rxlink = NULL;
546268e8e04eSSam Leffler 	sc->sc_rxpending = NULL;
54636b349e5aSAdrian Chadd 	TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
54645591b213SSam Leffler 		int error = ath_rxbuf_init(sc, bf);
54655591b213SSam Leffler 		if (error != 0) {
5466c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_RECV,
5467c42a7b7eSSam Leffler 				"%s: ath_rxbuf_init failed %d\n",
5468c42a7b7eSSam Leffler 				__func__, error);
54695591b213SSam Leffler 			return error;
54705591b213SSam Leffler 		}
54715591b213SSam Leffler 	}
54725591b213SSam Leffler 
54736b349e5aSAdrian Chadd 	bf = TAILQ_FIRST(&sc->sc_rxbuf);
54745591b213SSam Leffler 	ath_hal_putrxbuf(ah, bf->bf_daddr);
54755591b213SSam Leffler 	ath_hal_rxena(ah);		/* enable recv descriptors */
54765591b213SSam Leffler 	ath_mode_init(sc);		/* set filters, etc. */
54775591b213SSam Leffler 	ath_hal_startpcurecv(ah);	/* re-enable PCU/DMA engine */
54785591b213SSam Leffler 	return 0;
54795591b213SSam Leffler }
54805591b213SSam Leffler 
54815591b213SSam Leffler /*
5482c42a7b7eSSam Leffler  * Update internal state after a channel change.
5483c42a7b7eSSam Leffler  */
5484c42a7b7eSSam Leffler static void
5485c42a7b7eSSam Leffler ath_chan_change(struct ath_softc *sc, struct ieee80211_channel *chan)
5486c42a7b7eSSam Leffler {
5487c42a7b7eSSam Leffler 	enum ieee80211_phymode mode;
5488c42a7b7eSSam Leffler 
5489c42a7b7eSSam Leffler 	/*
5490c42a7b7eSSam Leffler 	 * Change channels and update the h/w rate map
5491c42a7b7eSSam Leffler 	 * if we're switching; e.g. 11a to 11b/g.
5492c42a7b7eSSam Leffler 	 */
549368e8e04eSSam Leffler 	mode = ieee80211_chan2mode(chan);
5494c42a7b7eSSam Leffler 	if (mode != sc->sc_curmode)
5495c42a7b7eSSam Leffler 		ath_setcurmode(sc, mode);
549659efa8b5SSam Leffler 	sc->sc_curchan = chan;
5497c42a7b7eSSam Leffler }
5498c42a7b7eSSam Leffler 
5499c42a7b7eSSam Leffler /*
55005591b213SSam Leffler  * Set/change channels.  If the channel is really being changed,
55014fa8d4efSDaniel Eischen  * it's done by resetting the chip.  To accomplish this we must
55025591b213SSam Leffler  * first cleanup any pending DMA, then restart stuff after a la
55035591b213SSam Leffler  * ath_init.
55045591b213SSam Leffler  */
55055591b213SSam Leffler static int
55065591b213SSam Leffler ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan)
55075591b213SSam Leffler {
5508b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
5509b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
55105591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5511ef27340cSAdrian Chadd 	int ret = 0;
5512ef27340cSAdrian Chadd 
5513ef27340cSAdrian Chadd 	/* Treat this as an interface reset */
5514d52f7132SAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
5515d52f7132SAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
5516d52f7132SAdrian Chadd 
5517d52f7132SAdrian Chadd 	/* (Try to) stop TX/RX from occuring */
5518d52f7132SAdrian Chadd 	taskqueue_block(sc->sc_tq);
5519d52f7132SAdrian Chadd 
5520ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
5521e78719adSAdrian Chadd 	ath_hal_intrset(ah, 0);		/* Stop new RX/TX completion */
5522e78719adSAdrian Chadd 	ath_txrx_stop_locked(sc);	/* Stop pending RX/TX completion */
5523ee321975SAdrian Chadd 	if (ath_reset_grablock(sc, 1) == 0) {
5524ee321975SAdrian Chadd 		device_printf(sc->sc_dev, "%s: concurrent reset! Danger!\n",
5525ef27340cSAdrian Chadd 		    __func__);
5526ee321975SAdrian Chadd 	}
5527ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
5528c42a7b7eSSam Leffler 
552959efa8b5SSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %u (%u MHz, flags 0x%x)\n",
553059efa8b5SSam Leffler 	    __func__, ieee80211_chan2ieee(ic, chan),
553159efa8b5SSam Leffler 	    chan->ic_freq, chan->ic_flags);
553259efa8b5SSam Leffler 	if (chan != sc->sc_curchan) {
5533c42a7b7eSSam Leffler 		HAL_STATUS status;
55345591b213SSam Leffler 		/*
55355591b213SSam Leffler 		 * To switch channels clear any pending DMA operations;
55365591b213SSam Leffler 		 * wait long enough for the RX fifo to drain, reset the
55375591b213SSam Leffler 		 * hardware at the new frequency, and then re-enable
55385591b213SSam Leffler 		 * the relevant bits of the h/w.
55395591b213SSam Leffler 		 */
5540ef27340cSAdrian Chadd #if 0
55415591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
5542ef27340cSAdrian Chadd #endif
55439a842e8bSAdrian Chadd 		ath_stoprecv(sc, 1);		/* turn off frame recv */
55449a842e8bSAdrian Chadd 		/*
55459a842e8bSAdrian Chadd 		 * First, handle completed TX/RX frames.
55469a842e8bSAdrian Chadd 		 */
55479a842e8bSAdrian Chadd 		ath_rx_proc(sc, 0);
55489a842e8bSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_NOLOSS);
55499a842e8bSAdrian Chadd 		/*
55509a842e8bSAdrian Chadd 		 * Next, flush the non-scheduled frames.
55519a842e8bSAdrian Chadd 		 */
5552517526efSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_FULL);	/* clear pending tx frames */
55539a842e8bSAdrian Chadd 
555459efa8b5SSam Leffler 		if (!ath_hal_reset(ah, sc->sc_opmode, chan, AH_TRUE, &status)) {
5555b032f27cSSam Leffler 			if_printf(ifp, "%s: unable to reset "
555679649302SGavin Atkinson 			    "channel %u (%u MHz, flags 0x%x), hal status %u\n",
555759efa8b5SSam Leffler 			    __func__, ieee80211_chan2ieee(ic, chan),
555859efa8b5SSam Leffler 			    chan->ic_freq, chan->ic_flags, status);
5559ef27340cSAdrian Chadd 			ret = EIO;
5560ef27340cSAdrian Chadd 			goto finish;
55615591b213SSam Leffler 		}
5562c59005e9SSam Leffler 		sc->sc_diversity = ath_hal_getdiversity(ah);
5563c42a7b7eSSam Leffler 
556448237774SAdrian Chadd 		/* Let DFS at it in case it's a DFS channel */
5565398bca2eSAdrian Chadd 		ath_dfs_radar_enable(sc, chan);
556648237774SAdrian Chadd 
55675591b213SSam Leffler 		/*
55685591b213SSam Leffler 		 * Re-enable rx framework.
55695591b213SSam Leffler 		 */
55705591b213SSam Leffler 		if (ath_startrecv(sc) != 0) {
5571b032f27cSSam Leffler 			if_printf(ifp, "%s: unable to restart recv logic\n",
5572b032f27cSSam Leffler 			    __func__);
5573ef27340cSAdrian Chadd 			ret = EIO;
5574ef27340cSAdrian Chadd 			goto finish;
55755591b213SSam Leffler 		}
55765591b213SSam Leffler 
55775591b213SSam Leffler 		/*
55785591b213SSam Leffler 		 * Change channels and update the h/w rate map
55795591b213SSam Leffler 		 * if we're switching; e.g. 11a to 11b/g.
55805591b213SSam Leffler 		 */
5581c42a7b7eSSam Leffler 		ath_chan_change(sc, chan);
55820a915fadSSam Leffler 
55830a915fadSSam Leffler 		/*
55842fd9aabbSAdrian Chadd 		 * Reset clears the beacon timers; reset them
55852fd9aabbSAdrian Chadd 		 * here if needed.
55862fd9aabbSAdrian Chadd 		 */
55872fd9aabbSAdrian Chadd 		if (sc->sc_beacons) {		/* restart beacons */
55882fd9aabbSAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
55892fd9aabbSAdrian Chadd 			if (sc->sc_tdma)
55902fd9aabbSAdrian Chadd 				ath_tdma_config(sc, NULL);
55912fd9aabbSAdrian Chadd 			else
55922fd9aabbSAdrian Chadd #endif
55932fd9aabbSAdrian Chadd 			ath_beacon_config(sc, NULL);
55942fd9aabbSAdrian Chadd 		}
55952fd9aabbSAdrian Chadd 
55962fd9aabbSAdrian Chadd 		/*
55970a915fadSSam Leffler 		 * Re-enable interrupts.
55980a915fadSSam Leffler 		 */
5599e78719adSAdrian Chadd #if 0
56000a915fadSSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
5601ef27340cSAdrian Chadd #endif
56025591b213SSam Leffler 	}
5603ef27340cSAdrian Chadd 
5604ef27340cSAdrian Chadd finish:
5605ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
5606ef27340cSAdrian Chadd 	sc->sc_inreset_cnt--;
5607ef27340cSAdrian Chadd 	/* XXX only do this if sc_inreset_cnt == 0? */
5608ef27340cSAdrian Chadd 	ath_hal_intrset(ah, sc->sc_imask);
5609ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
5610ef27340cSAdrian Chadd 
5611e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
5612ef27340cSAdrian Chadd 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
5613e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
5614ef27340cSAdrian Chadd 	ath_txrx_start(sc);
5615ef27340cSAdrian Chadd 	/* XXX ath_start? */
5616ef27340cSAdrian Chadd 
5617ef27340cSAdrian Chadd 	return ret;
56185591b213SSam Leffler }
56195591b213SSam Leffler 
56205591b213SSam Leffler /*
56215591b213SSam Leffler  * Periodically recalibrate the PHY to account
56225591b213SSam Leffler  * for temperature/environment changes.
56235591b213SSam Leffler  */
56245591b213SSam Leffler static void
56255591b213SSam Leffler ath_calibrate(void *arg)
56265591b213SSam Leffler {
56275591b213SSam Leffler 	struct ath_softc *sc = arg;
56285591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
56292dc7fcc4SSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
56308d91de92SSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
56312dc7fcc4SSam Leffler 	HAL_BOOL longCal, isCalDone;
5632a108ab63SAdrian Chadd 	HAL_BOOL aniCal, shortCal = AH_FALSE;
56332dc7fcc4SSam Leffler 	int nextcal;
56345591b213SSam Leffler 
56358d91de92SSam Leffler 	if (ic->ic_flags & IEEE80211_F_SCAN)	/* defer, off channel */
56368d91de92SSam Leffler 		goto restart;
56372dc7fcc4SSam Leffler 	longCal = (ticks - sc->sc_lastlongcal >= ath_longcalinterval*hz);
5638a108ab63SAdrian Chadd 	aniCal = (ticks - sc->sc_lastani >= ath_anicalinterval*hz/1000);
5639a108ab63SAdrian Chadd 	if (sc->sc_doresetcal)
5640a108ab63SAdrian Chadd 		shortCal = (ticks - sc->sc_lastshortcal >= ath_shortcalinterval*hz/1000);
5641a108ab63SAdrian Chadd 
5642a108ab63SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: shortCal=%d; longCal=%d; aniCal=%d\n", __func__, shortCal, longCal, aniCal);
5643a108ab63SAdrian Chadd 	if (aniCal) {
5644a108ab63SAdrian Chadd 		sc->sc_stats.ast_ani_cal++;
5645a108ab63SAdrian Chadd 		sc->sc_lastani = ticks;
5646a108ab63SAdrian Chadd 		ath_hal_ani_poll(ah, sc->sc_curchan);
5647a108ab63SAdrian Chadd 	}
5648a108ab63SAdrian Chadd 
56492dc7fcc4SSam Leffler 	if (longCal) {
56505591b213SSam Leffler 		sc->sc_stats.ast_per_cal++;
56518197f57eSAdrian Chadd 		sc->sc_lastlongcal = ticks;
56525591b213SSam Leffler 		if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) {
56535591b213SSam Leffler 			/*
56545591b213SSam Leffler 			 * Rfgain is out of bounds, reset the chip
56555591b213SSam Leffler 			 * to load new gain values.
56565591b213SSam Leffler 			 */
5657370572d9SSam Leffler 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
5658370572d9SSam Leffler 				"%s: rfgain change\n", __func__);
56595591b213SSam Leffler 			sc->sc_stats.ast_per_rfgain++;
5660ef27340cSAdrian Chadd 			sc->sc_resetcal = 0;
5661ef27340cSAdrian Chadd 			sc->sc_doresetcal = AH_TRUE;
5662d52f7132SAdrian Chadd 			taskqueue_enqueue(sc->sc_tq, &sc->sc_resettask);
5663d52f7132SAdrian Chadd 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
5664ef27340cSAdrian Chadd 			return;
56655591b213SSam Leffler 		}
56662dc7fcc4SSam Leffler 		/*
56672dc7fcc4SSam Leffler 		 * If this long cal is after an idle period, then
56682dc7fcc4SSam Leffler 		 * reset the data collection state so we start fresh.
56692dc7fcc4SSam Leffler 		 */
56702dc7fcc4SSam Leffler 		if (sc->sc_resetcal) {
567159efa8b5SSam Leffler 			(void) ath_hal_calreset(ah, sc->sc_curchan);
56722dc7fcc4SSam Leffler 			sc->sc_lastcalreset = ticks;
5673a108ab63SAdrian Chadd 			sc->sc_lastshortcal = ticks;
56742dc7fcc4SSam Leffler 			sc->sc_resetcal = 0;
5675a108ab63SAdrian Chadd 			sc->sc_doresetcal = AH_TRUE;
56762dc7fcc4SSam Leffler 		}
56772dc7fcc4SSam Leffler 	}
5678a108ab63SAdrian Chadd 
5679a108ab63SAdrian Chadd 	/* Only call if we're doing a short/long cal, not for ANI calibration */
5680a108ab63SAdrian Chadd 	if (shortCal || longCal) {
568159efa8b5SSam Leffler 		if (ath_hal_calibrateN(ah, sc->sc_curchan, longCal, &isCalDone)) {
56822dc7fcc4SSam Leffler 			if (longCal) {
56832dc7fcc4SSam Leffler 				/*
56842dc7fcc4SSam Leffler 				 * Calibrate noise floor data again in case of change.
56852dc7fcc4SSam Leffler 				 */
56862dc7fcc4SSam Leffler 				ath_hal_process_noisefloor(ah);
56872dc7fcc4SSam Leffler 			}
56882dc7fcc4SSam Leffler 		} else {
5689c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
5690c42a7b7eSSam Leffler 				"%s: calibration of channel %u failed\n",
569159efa8b5SSam Leffler 				__func__, sc->sc_curchan->ic_freq);
56925591b213SSam Leffler 			sc->sc_stats.ast_per_calfail++;
56935591b213SSam Leffler 		}
5694a108ab63SAdrian Chadd 		if (shortCal)
5695a108ab63SAdrian Chadd 			sc->sc_lastshortcal = ticks;
5696a108ab63SAdrian Chadd 	}
56972dc7fcc4SSam Leffler 	if (!isCalDone) {
56988d91de92SSam Leffler restart:
56997b0c77ecSSam Leffler 		/*
57002dc7fcc4SSam Leffler 		 * Use a shorter interval to potentially collect multiple
57012dc7fcc4SSam Leffler 		 * data samples required to complete calibration.  Once
57022dc7fcc4SSam Leffler 		 * we're told the work is done we drop back to a longer
57032dc7fcc4SSam Leffler 		 * interval between requests.  We're more aggressive doing
57042dc7fcc4SSam Leffler 		 * work when operating as an AP to improve operation right
57052dc7fcc4SSam Leffler 		 * after startup.
57067b0c77ecSSam Leffler 		 */
5707a108ab63SAdrian Chadd 		sc->sc_lastshortcal = ticks;
5708a108ab63SAdrian Chadd 		nextcal = ath_shortcalinterval*hz/1000;
57092dc7fcc4SSam Leffler 		if (sc->sc_opmode != HAL_M_HOSTAP)
57102dc7fcc4SSam Leffler 			nextcal *= 10;
5711a108ab63SAdrian Chadd 		sc->sc_doresetcal = AH_TRUE;
57122dc7fcc4SSam Leffler 	} else {
5713a108ab63SAdrian Chadd 		/* nextcal should be the shortest time for next event */
57142dc7fcc4SSam Leffler 		nextcal = ath_longcalinterval*hz;
57152dc7fcc4SSam Leffler 		if (sc->sc_lastcalreset == 0)
57162dc7fcc4SSam Leffler 			sc->sc_lastcalreset = sc->sc_lastlongcal;
57172dc7fcc4SSam Leffler 		else if (ticks - sc->sc_lastcalreset >= ath_resetcalinterval*hz)
57182dc7fcc4SSam Leffler 			sc->sc_resetcal = 1;	/* setup reset next trip */
5719a108ab63SAdrian Chadd 		sc->sc_doresetcal = AH_FALSE;
5720bd5a9920SSam Leffler 	}
5721a108ab63SAdrian Chadd 	/* ANI calibration may occur more often than short/long/resetcal */
5722a108ab63SAdrian Chadd 	if (ath_anicalinterval > 0)
5723a108ab63SAdrian Chadd 		nextcal = MIN(nextcal, ath_anicalinterval*hz/1000);
5724bd5a9920SSam Leffler 
57252dc7fcc4SSam Leffler 	if (nextcal != 0) {
57262dc7fcc4SSam Leffler 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: next +%u (%sisCalDone)\n",
57272dc7fcc4SSam Leffler 		    __func__, nextcal, isCalDone ? "" : "!");
57282dc7fcc4SSam Leffler 		callout_reset(&sc->sc_cal_ch, nextcal, ath_calibrate, sc);
57292dc7fcc4SSam Leffler 	} else {
57302dc7fcc4SSam Leffler 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: calibration disabled\n",
57312dc7fcc4SSam Leffler 		    __func__);
57322dc7fcc4SSam Leffler 		/* NB: don't rearm timer */
57332dc7fcc4SSam Leffler 	}
57345591b213SSam Leffler }
57355591b213SSam Leffler 
573668e8e04eSSam Leffler static void
573768e8e04eSSam Leffler ath_scan_start(struct ieee80211com *ic)
573868e8e04eSSam Leffler {
573968e8e04eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
574068e8e04eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
574168e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
574268e8e04eSSam Leffler 	u_int32_t rfilt;
574368e8e04eSSam Leffler 
574468e8e04eSSam Leffler 	/* XXX calibration timer? */
574568e8e04eSSam Leffler 
5746c98cefc5SAdrian Chadd 	ATH_LOCK(sc);
574768e8e04eSSam Leffler 	sc->sc_scanning = 1;
574868e8e04eSSam Leffler 	sc->sc_syncbeacon = 0;
574968e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
5750c98cefc5SAdrian Chadd 	ATH_UNLOCK(sc);
5751c98cefc5SAdrian Chadd 
5752c98cefc5SAdrian Chadd 	ATH_PCU_LOCK(sc);
575368e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
575468e8e04eSSam Leffler 	ath_hal_setassocid(ah, ifp->if_broadcastaddr, 0);
5755c98cefc5SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
575668e8e04eSSam Leffler 
575768e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0\n",
575868e8e04eSSam Leffler 		 __func__, rfilt, ether_sprintf(ifp->if_broadcastaddr));
575968e8e04eSSam Leffler }
576068e8e04eSSam Leffler 
576168e8e04eSSam Leffler static void
576268e8e04eSSam Leffler ath_scan_end(struct ieee80211com *ic)
576368e8e04eSSam Leffler {
576468e8e04eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
576568e8e04eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
576668e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
576768e8e04eSSam Leffler 	u_int32_t rfilt;
576868e8e04eSSam Leffler 
5769c98cefc5SAdrian Chadd 	ATH_LOCK(sc);
577068e8e04eSSam Leffler 	sc->sc_scanning = 0;
577168e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
5772c98cefc5SAdrian Chadd 	ATH_UNLOCK(sc);
5773c98cefc5SAdrian Chadd 
5774c98cefc5SAdrian Chadd 	ATH_PCU_LOCK(sc);
577568e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
577668e8e04eSSam Leffler 	ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
577768e8e04eSSam Leffler 
577868e8e04eSSam Leffler 	ath_hal_process_noisefloor(ah);
5779c98cefc5SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
578068e8e04eSSam Leffler 
578168e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
578268e8e04eSSam Leffler 		 __func__, rfilt, ether_sprintf(sc->sc_curbssid),
578368e8e04eSSam Leffler 		 sc->sc_curaid);
578468e8e04eSSam Leffler }
578568e8e04eSSam Leffler 
5786fdd72b4aSAdrian Chadd #ifdef	ATH_ENABLE_11N
5787e7200579SAdrian Chadd /*
5788e7200579SAdrian Chadd  * For now, just do a channel change.
5789e7200579SAdrian Chadd  *
5790e7200579SAdrian Chadd  * Later, we'll go through the hard slog of suspending tx/rx, changing rate
5791e7200579SAdrian Chadd  * control state and resetting the hardware without dropping frames out
5792e7200579SAdrian Chadd  * of the queue.
5793e7200579SAdrian Chadd  *
5794e7200579SAdrian Chadd  * The unfortunate trouble here is making absolutely sure that the
5795e7200579SAdrian Chadd  * channel width change has propagated enough so the hardware
5796e7200579SAdrian Chadd  * absolutely isn't handed bogus frames for it's current operating
5797e7200579SAdrian Chadd  * mode. (Eg, 40MHz frames in 20MHz mode.) Since TX and RX can and
5798e7200579SAdrian Chadd  * does occur in parallel, we need to make certain we've blocked
5799e7200579SAdrian Chadd  * any further ongoing TX (and RX, that can cause raw TX)
5800e7200579SAdrian Chadd  * before we do this.
5801e7200579SAdrian Chadd  */
5802e7200579SAdrian Chadd static void
5803e7200579SAdrian Chadd ath_update_chw(struct ieee80211com *ic)
5804e7200579SAdrian Chadd {
5805e7200579SAdrian Chadd 	struct ifnet *ifp = ic->ic_ifp;
5806e7200579SAdrian Chadd 	struct ath_softc *sc = ifp->if_softc;
5807e7200579SAdrian Chadd 
5808e7200579SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: called\n", __func__);
5809e7200579SAdrian Chadd 	ath_set_channel(ic);
5810e7200579SAdrian Chadd }
5811fdd72b4aSAdrian Chadd #endif	/* ATH_ENABLE_11N */
5812e7200579SAdrian Chadd 
581368e8e04eSSam Leffler static void
581468e8e04eSSam Leffler ath_set_channel(struct ieee80211com *ic)
581568e8e04eSSam Leffler {
581668e8e04eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
581768e8e04eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
581868e8e04eSSam Leffler 
581968e8e04eSSam Leffler 	(void) ath_chan_set(sc, ic->ic_curchan);
582068e8e04eSSam Leffler 	/*
582168e8e04eSSam Leffler 	 * If we are returning to our bss channel then mark state
582268e8e04eSSam Leffler 	 * so the next recv'd beacon's tsf will be used to sync the
582368e8e04eSSam Leffler 	 * beacon timers.  Note that since we only hear beacons in
582468e8e04eSSam Leffler 	 * sta/ibss mode this has no effect in other operating modes.
582568e8e04eSSam Leffler 	 */
5826a887b1e3SAdrian Chadd 	ATH_LOCK(sc);
582768e8e04eSSam Leffler 	if (!sc->sc_scanning && ic->ic_curchan == ic->ic_bsschan)
582868e8e04eSSam Leffler 		sc->sc_syncbeacon = 1;
5829a887b1e3SAdrian Chadd 	ATH_UNLOCK(sc);
583068e8e04eSSam Leffler }
583168e8e04eSSam Leffler 
5832b032f27cSSam Leffler /*
5833b032f27cSSam Leffler  * Walk the vap list and check if there any vap's in RUN state.
5834b032f27cSSam Leffler  */
58355591b213SSam Leffler static int
5836b032f27cSSam Leffler ath_isanyrunningvaps(struct ieee80211vap *this)
58375591b213SSam Leffler {
5838b032f27cSSam Leffler 	struct ieee80211com *ic = this->iv_ic;
5839b032f27cSSam Leffler 	struct ieee80211vap *vap;
5840b032f27cSSam Leffler 
5841b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
5842b032f27cSSam Leffler 
5843b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
5844309a3e45SSam Leffler 		if (vap != this && vap->iv_state >= IEEE80211_S_RUN)
5845b032f27cSSam Leffler 			return 1;
5846b032f27cSSam Leffler 	}
5847b032f27cSSam Leffler 	return 0;
5848b032f27cSSam Leffler }
5849b032f27cSSam Leffler 
5850b032f27cSSam Leffler static int
5851b032f27cSSam Leffler ath_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
5852b032f27cSSam Leffler {
5853b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
5854b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
5855b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
585645bbf62fSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5857b032f27cSSam Leffler 	struct ieee80211_node *ni = NULL;
585868e8e04eSSam Leffler 	int i, error, stamode;
58595591b213SSam Leffler 	u_int32_t rfilt;
5860f52efb6dSAdrian Chadd 	int csa_run_transition = 0;
58615591b213SSam Leffler 	static const HAL_LED_STATE leds[] = {
58625591b213SSam Leffler 	    HAL_LED_INIT,	/* IEEE80211_S_INIT */
58635591b213SSam Leffler 	    HAL_LED_SCAN,	/* IEEE80211_S_SCAN */
58645591b213SSam Leffler 	    HAL_LED_AUTH,	/* IEEE80211_S_AUTH */
58655591b213SSam Leffler 	    HAL_LED_ASSOC, 	/* IEEE80211_S_ASSOC */
586677d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_CAC */
58675591b213SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_RUN */
586877d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_CSA */
586977d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_SLEEP */
58705591b213SSam Leffler 	};
58715591b213SSam Leffler 
5872c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: %s -> %s\n", __func__,
5873b032f27cSSam Leffler 		ieee80211_state_name[vap->iv_state],
5874c42a7b7eSSam Leffler 		ieee80211_state_name[nstate]);
58755591b213SSam Leffler 
5876107fdf96SAdrian Chadd 	/*
5877107fdf96SAdrian Chadd 	 * net80211 _should_ have the comlock asserted at this point.
5878107fdf96SAdrian Chadd 	 * There are some comments around the calls to vap->iv_newstate
5879107fdf96SAdrian Chadd 	 * which indicate that it (newstate) may end up dropping the
5880107fdf96SAdrian Chadd 	 * lock.  This and the subsequent lock assert check after newstate
5881107fdf96SAdrian Chadd 	 * are an attempt to catch these and figure out how/why.
5882107fdf96SAdrian Chadd 	 */
5883107fdf96SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
5884107fdf96SAdrian Chadd 
5885f52efb6dSAdrian Chadd 	if (vap->iv_state == IEEE80211_S_CSA && nstate == IEEE80211_S_RUN)
5886f52efb6dSAdrian Chadd 		csa_run_transition = 1;
5887f52efb6dSAdrian Chadd 
58882e986da5SSam Leffler 	callout_drain(&sc->sc_cal_ch);
58895591b213SSam Leffler 	ath_hal_setledstate(ah, leds[nstate]);	/* set LED */
58905591b213SSam Leffler 
5891b032f27cSSam Leffler 	if (nstate == IEEE80211_S_SCAN) {
589258769f58SSam Leffler 		/*
5893b032f27cSSam Leffler 		 * Scanning: turn off beacon miss and don't beacon.
5894b032f27cSSam Leffler 		 * Mark beacon state so when we reach RUN state we'll
5895b032f27cSSam Leffler 		 * [re]setup beacons.  Unblock the task q thread so
5896b032f27cSSam Leffler 		 * deferred interrupt processing is done.
589758769f58SSam Leffler 		 */
5898b032f27cSSam Leffler 		ath_hal_intrset(ah,
5899b032f27cSSam Leffler 		    sc->sc_imask &~ (HAL_INT_SWBA | HAL_INT_BMISS));
59005591b213SSam Leffler 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
5901b032f27cSSam Leffler 		sc->sc_beacons = 0;
5902b032f27cSSam Leffler 		taskqueue_unblock(sc->sc_tq);
59035591b213SSam Leffler 	}
59045591b213SSam Leffler 
590580767531SAdrian Chadd 	ni = ieee80211_ref_node(vap->iv_bss);
590668e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
5907b032f27cSSam Leffler 	stamode = (vap->iv_opmode == IEEE80211_M_STA ||
59087b916f89SSam Leffler 		   vap->iv_opmode == IEEE80211_M_AHDEMO ||
5909b032f27cSSam Leffler 		   vap->iv_opmode == IEEE80211_M_IBSS);
591068e8e04eSSam Leffler 	if (stamode && nstate == IEEE80211_S_RUN) {
591168e8e04eSSam Leffler 		sc->sc_curaid = ni->ni_associd;
591268e8e04eSSam Leffler 		IEEE80211_ADDR_COPY(sc->sc_curbssid, ni->ni_bssid);
5913b032f27cSSam Leffler 		ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
5914b032f27cSSam Leffler 	}
591568e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
5916b032f27cSSam Leffler 	   __func__, rfilt, ether_sprintf(sc->sc_curbssid), sc->sc_curaid);
591768e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
591868e8e04eSSam Leffler 
5919b032f27cSSam Leffler 	/* XXX is this to restore keycache on resume? */
5920b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_STA &&
5921b032f27cSSam Leffler 	    (vap->iv_flags & IEEE80211_F_PRIVACY)) {
59225591b213SSam Leffler 		for (i = 0; i < IEEE80211_WEP_NKID; i++)
59235591b213SSam Leffler 			if (ath_hal_keyisvalid(ah, i))
592468e8e04eSSam Leffler 				ath_hal_keysetmac(ah, i, ni->ni_bssid);
59255591b213SSam Leffler 	}
5926b032f27cSSam Leffler 
5927b032f27cSSam Leffler 	/*
5928b032f27cSSam Leffler 	 * Invoke the parent method to do net80211 work.
5929b032f27cSSam Leffler 	 */
5930b032f27cSSam Leffler 	error = avp->av_newstate(vap, nstate, arg);
5931b032f27cSSam Leffler 	if (error != 0)
5932b032f27cSSam Leffler 		goto bad;
5933c42a7b7eSSam Leffler 
5934107fdf96SAdrian Chadd 	/*
5935107fdf96SAdrian Chadd 	 * See above: ensure av_newstate() doesn't drop the lock
5936107fdf96SAdrian Chadd 	 * on us.
5937107fdf96SAdrian Chadd 	 */
5938107fdf96SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
5939107fdf96SAdrian Chadd 
594068e8e04eSSam Leffler 	if (nstate == IEEE80211_S_RUN) {
5941b032f27cSSam Leffler 		/* NB: collect bss node again, it may have changed */
594280767531SAdrian Chadd 		ieee80211_free_node(ni);
594380767531SAdrian Chadd 		ni = ieee80211_ref_node(vap->iv_bss);
59445591b213SSam Leffler 
5945b032f27cSSam Leffler 		DPRINTF(sc, ATH_DEBUG_STATE,
5946b032f27cSSam Leffler 		    "%s(RUN): iv_flags 0x%08x bintvl %d bssid %s "
5947b032f27cSSam Leffler 		    "capinfo 0x%04x chan %d\n", __func__,
5948b032f27cSSam Leffler 		    vap->iv_flags, ni->ni_intval, ether_sprintf(ni->ni_bssid),
5949b032f27cSSam Leffler 		    ni->ni_capinfo, ieee80211_chan2ieee(ic, ic->ic_curchan));
5950b032f27cSSam Leffler 
5951b032f27cSSam Leffler 		switch (vap->iv_opmode) {
5952584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
595310ad9a77SSam Leffler 		case IEEE80211_M_AHDEMO:
595410ad9a77SSam Leffler 			if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
595510ad9a77SSam Leffler 				break;
595610ad9a77SSam Leffler 			/* fall thru... */
595710ad9a77SSam Leffler #endif
5958e8fd88a3SSam Leffler 		case IEEE80211_M_HOSTAP:
5959e8fd88a3SSam Leffler 		case IEEE80211_M_IBSS:
596059aa14a9SRui Paulo 		case IEEE80211_M_MBSS:
59615591b213SSam Leffler 			/*
5962e8fd88a3SSam Leffler 			 * Allocate and setup the beacon frame.
5963e8fd88a3SSam Leffler 			 *
5964f818612bSSam Leffler 			 * Stop any previous beacon DMA.  This may be
5965f818612bSSam Leffler 			 * necessary, for example, when an ibss merge
5966f818612bSSam Leffler 			 * causes reconfiguration; there will be a state
5967f818612bSSam Leffler 			 * transition from RUN->RUN that means we may
5968f818612bSSam Leffler 			 * be called with beacon transmission active.
5969f818612bSSam Leffler 			 */
5970f818612bSSam Leffler 			ath_hal_stoptxdma(ah, sc->sc_bhalq);
5971b032f27cSSam Leffler 
59725591b213SSam Leffler 			error = ath_beacon_alloc(sc, ni);
59735591b213SSam Leffler 			if (error != 0)
59745591b213SSam Leffler 				goto bad;
59757a04dc27SSam Leffler 			/*
597680d939bfSSam Leffler 			 * If joining an adhoc network defer beacon timer
597780d939bfSSam Leffler 			 * configuration to the next beacon frame so we
597880d939bfSSam Leffler 			 * have a current TSF to use.  Otherwise we're
5979b032f27cSSam Leffler 			 * starting an ibss/bss so there's no need to delay;
5980b032f27cSSam Leffler 			 * if this is the first vap moving to RUN state, then
5981b032f27cSSam Leffler 			 * beacon state needs to be [re]configured.
59827a04dc27SSam Leffler 			 */
5983b032f27cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_IBSS &&
5984b032f27cSSam Leffler 			    ni->ni_tstamp.tsf != 0) {
598580d939bfSSam Leffler 				sc->sc_syncbeacon = 1;
5986b032f27cSSam Leffler 			} else if (!sc->sc_beacons) {
5987584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
598810ad9a77SSam Leffler 				if (vap->iv_caps & IEEE80211_C_TDMA)
598910ad9a77SSam Leffler 					ath_tdma_config(sc, vap);
599010ad9a77SSam Leffler 				else
599110ad9a77SSam Leffler #endif
5992b032f27cSSam Leffler 					ath_beacon_config(sc, vap);
5993b032f27cSSam Leffler 				sc->sc_beacons = 1;
5994b032f27cSSam Leffler 			}
5995e8fd88a3SSam Leffler 			break;
5996e8fd88a3SSam Leffler 		case IEEE80211_M_STA:
5997e8fd88a3SSam Leffler 			/*
599880d939bfSSam Leffler 			 * Defer beacon timer configuration to the next
599980d939bfSSam Leffler 			 * beacon frame so we have a current TSF to use
600080d939bfSSam Leffler 			 * (any TSF collected when scanning is likely old).
6001f52efb6dSAdrian Chadd 			 * However if it's due to a CSA -> RUN transition,
6002f52efb6dSAdrian Chadd 			 * force a beacon update so we pick up a lack of
6003f52efb6dSAdrian Chadd 			 * beacons from an AP in CAC and thus force a
6004f52efb6dSAdrian Chadd 			 * scan.
60057a04dc27SSam Leffler 			 */
600680d939bfSSam Leffler 			sc->sc_syncbeacon = 1;
6007f52efb6dSAdrian Chadd 			if (csa_run_transition)
6008f52efb6dSAdrian Chadd 				ath_beacon_config(sc, vap);
6009e8fd88a3SSam Leffler 			break;
6010b032f27cSSam Leffler 		case IEEE80211_M_MONITOR:
6011b032f27cSSam Leffler 			/*
6012b032f27cSSam Leffler 			 * Monitor mode vaps have only INIT->RUN and RUN->RUN
6013b032f27cSSam Leffler 			 * transitions so we must re-enable interrupts here to
6014b032f27cSSam Leffler 			 * handle the case of a single monitor mode vap.
6015b032f27cSSam Leffler 			 */
6016b032f27cSSam Leffler 			ath_hal_intrset(ah, sc->sc_imask);
6017b032f27cSSam Leffler 			break;
6018b032f27cSSam Leffler 		case IEEE80211_M_WDS:
6019b032f27cSSam Leffler 			break;
6020e8fd88a3SSam Leffler 		default:
6021e8fd88a3SSam Leffler 			break;
60225591b213SSam Leffler 		}
60235591b213SSam Leffler 		/*
60247b0c77ecSSam Leffler 		 * Let the hal process statistics collected during a
60257b0c77ecSSam Leffler 		 * scan so it can provide calibrated noise floor data.
60267b0c77ecSSam Leffler 		 */
60277b0c77ecSSam Leffler 		ath_hal_process_noisefloor(ah);
60287b0c77ecSSam Leffler 		/*
6029ffa2cab6SSam Leffler 		 * Reset rssi stats; maybe not the best place...
6030ffa2cab6SSam Leffler 		 */
6031ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
6032ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
6033ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
603445bbf62fSSam Leffler 		/*
6035b032f27cSSam Leffler 		 * Finally, start any timers and the task q thread
6036b032f27cSSam Leffler 		 * (in case we didn't go through SCAN state).
603745bbf62fSSam Leffler 		 */
60382dc7fcc4SSam Leffler 		if (ath_longcalinterval != 0) {
6039c42a7b7eSSam Leffler 			/* start periodic recalibration timer */
60402dc7fcc4SSam Leffler 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
60412dc7fcc4SSam Leffler 		} else {
60422dc7fcc4SSam Leffler 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
60432dc7fcc4SSam Leffler 			    "%s: calibration disabled\n", __func__);
6044c42a7b7eSSam Leffler 		}
6045b032f27cSSam Leffler 		taskqueue_unblock(sc->sc_tq);
6046b032f27cSSam Leffler 	} else if (nstate == IEEE80211_S_INIT) {
6047b032f27cSSam Leffler 		/*
6048b032f27cSSam Leffler 		 * If there are no vaps left in RUN state then
6049b032f27cSSam Leffler 		 * shutdown host/driver operation:
6050b032f27cSSam Leffler 		 * o disable interrupts
6051b032f27cSSam Leffler 		 * o disable the task queue thread
6052b032f27cSSam Leffler 		 * o mark beacon processing as stopped
6053b032f27cSSam Leffler 		 */
6054b032f27cSSam Leffler 		if (!ath_isanyrunningvaps(vap)) {
6055b032f27cSSam Leffler 			sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
6056b032f27cSSam Leffler 			/* disable interrupts  */
6057b032f27cSSam Leffler 			ath_hal_intrset(ah, sc->sc_imask &~ HAL_INT_GLOBAL);
6058b032f27cSSam Leffler 			taskqueue_block(sc->sc_tq);
6059b032f27cSSam Leffler 			sc->sc_beacons = 0;
6060b032f27cSSam Leffler 		}
6061584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
606210ad9a77SSam Leffler 		ath_hal_setcca(ah, AH_TRUE);
606310ad9a77SSam Leffler #endif
6064b032f27cSSam Leffler 	}
60655591b213SSam Leffler bad:
606680767531SAdrian Chadd 	ieee80211_free_node(ni);
60675591b213SSam Leffler 	return error;
60685591b213SSam Leffler }
60695591b213SSam Leffler 
60705591b213SSam Leffler /*
6071e8fd88a3SSam Leffler  * Allocate a key cache slot to the station so we can
6072e8fd88a3SSam Leffler  * setup a mapping from key index to node. The key cache
6073e8fd88a3SSam Leffler  * slot is needed for managing antenna state and for
6074e8fd88a3SSam Leffler  * compression when stations do not use crypto.  We do
6075e8fd88a3SSam Leffler  * it uniliaterally here; if crypto is employed this slot
6076e8fd88a3SSam Leffler  * will be reassigned.
6077e8fd88a3SSam Leffler  */
6078e8fd88a3SSam Leffler static void
6079e8fd88a3SSam Leffler ath_setup_stationkey(struct ieee80211_node *ni)
6080e8fd88a3SSam Leffler {
6081b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
6082b032f27cSSam Leffler 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
6083c1225b52SSam Leffler 	ieee80211_keyix keyix, rxkeyix;
6084e8fd88a3SSam Leffler 
608580767531SAdrian Chadd 	/* XXX should take a locked ref to vap->iv_bss */
6086b032f27cSSam Leffler 	if (!ath_key_alloc(vap, &ni->ni_ucastkey, &keyix, &rxkeyix)) {
6087e8fd88a3SSam Leffler 		/*
6088e8fd88a3SSam Leffler 		 * Key cache is full; we'll fall back to doing
6089e8fd88a3SSam Leffler 		 * the more expensive lookup in software.  Note
6090e8fd88a3SSam Leffler 		 * this also means no h/w compression.
6091e8fd88a3SSam Leffler 		 */
6092e8fd88a3SSam Leffler 		/* XXX msg+statistic */
6093e8fd88a3SSam Leffler 	} else {
6094c1225b52SSam Leffler 		/* XXX locking? */
6095e8fd88a3SSam Leffler 		ni->ni_ucastkey.wk_keyix = keyix;
6096c1225b52SSam Leffler 		ni->ni_ucastkey.wk_rxkeyix = rxkeyix;
609733052833SSam Leffler 		/* NB: must mark device key to get called back on delete */
609833052833SSam Leffler 		ni->ni_ucastkey.wk_flags |= IEEE80211_KEY_DEVKEY;
6099d3ac945bSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_ucastkey.wk_macaddr, ni->ni_macaddr);
6100e8fd88a3SSam Leffler 		/* NB: this will create a pass-thru key entry */
610155c7b877SAdrian Chadd 		ath_keyset(sc, vap, &ni->ni_ucastkey, vap->iv_bss);
6102e8fd88a3SSam Leffler 	}
6103e8fd88a3SSam Leffler }
6104e8fd88a3SSam Leffler 
6105e8fd88a3SSam Leffler /*
61065591b213SSam Leffler  * Setup driver-specific state for a newly associated node.
61075591b213SSam Leffler  * Note that we're called also on a re-associate, the isnew
61085591b213SSam Leffler  * param tells us if this is the first time or not.
61095591b213SSam Leffler  */
61105591b213SSam Leffler static void
6111e9962332SSam Leffler ath_newassoc(struct ieee80211_node *ni, int isnew)
61125591b213SSam Leffler {
6113b032f27cSSam Leffler 	struct ath_node *an = ATH_NODE(ni);
6114b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
6115b032f27cSSam Leffler 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
6116c62362cbSSam Leffler 	const struct ieee80211_txparam *tp = ni->ni_txparms;
61175591b213SSam Leffler 
6118ab06fdf2SSam Leffler 	an->an_mcastrix = ath_tx_findrix(sc, tp->mcastrate);
6119ab06fdf2SSam Leffler 	an->an_mgmtrix = ath_tx_findrix(sc, tp->mgmtrate);
6120b032f27cSSam Leffler 
6121b032f27cSSam Leffler 	ath_rate_newassoc(sc, an, isnew);
6122e8fd88a3SSam Leffler 	if (isnew &&
6123b032f27cSSam Leffler 	    (vap->iv_flags & IEEE80211_F_PRIVACY) == 0 && sc->sc_hasclrkey &&
6124b032f27cSSam Leffler 	    ni->ni_ucastkey.wk_keyix == IEEE80211_KEYIX_NONE)
6125e8fd88a3SSam Leffler 		ath_setup_stationkey(ni);
6126e8fd88a3SSam Leffler }
61275591b213SSam Leffler 
61285591b213SSam Leffler static int
612959efa8b5SSam Leffler ath_setregdomain(struct ieee80211com *ic, struct ieee80211_regdomain *reg,
6130b032f27cSSam Leffler 	int nchans, struct ieee80211_channel chans[])
6131b032f27cSSam Leffler {
6132b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
6133b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
613459efa8b5SSam Leffler 	HAL_STATUS status;
6135b032f27cSSam Leffler 
6136033022a9SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
613759efa8b5SSam Leffler 	    "%s: rd %u cc %u location %c%s\n",
613859efa8b5SSam Leffler 	    __func__, reg->regdomain, reg->country, reg->location,
613959efa8b5SSam Leffler 	    reg->ecm ? " ecm" : "");
6140033022a9SSam Leffler 
614159efa8b5SSam Leffler 	status = ath_hal_set_channels(ah, chans, nchans,
614259efa8b5SSam Leffler 	    reg->country, reg->regdomain);
614359efa8b5SSam Leffler 	if (status != HAL_OK) {
614459efa8b5SSam Leffler 		DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: failed, status %u\n",
614559efa8b5SSam Leffler 		    __func__, status);
614659efa8b5SSam Leffler 		return EINVAL;		/* XXX */
6147b032f27cSSam Leffler 	}
61488db87e40SAdrian Chadd 
6149b032f27cSSam Leffler 	return 0;
6150b032f27cSSam Leffler }
6151b032f27cSSam Leffler 
6152b032f27cSSam Leffler static void
6153b032f27cSSam Leffler ath_getradiocaps(struct ieee80211com *ic,
61545fe9f044SSam Leffler 	int maxchans, int *nchans, struct ieee80211_channel chans[])
6155b032f27cSSam Leffler {
6156b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
6157b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
6158b032f27cSSam Leffler 
615959efa8b5SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: use rd %u cc %d\n",
616059efa8b5SSam Leffler 	    __func__, SKU_DEBUG, CTRY_DEFAULT);
6161033022a9SSam Leffler 
616259efa8b5SSam Leffler 	/* XXX check return */
616359efa8b5SSam Leffler 	(void) ath_hal_getchannels(ah, chans, maxchans, nchans,
616459efa8b5SSam Leffler 	    HAL_MODE_ALL, CTRY_DEFAULT, SKU_DEBUG, AH_TRUE);
6165033022a9SSam Leffler 
6166b032f27cSSam Leffler }
6167b032f27cSSam Leffler 
6168b032f27cSSam Leffler static int
6169b032f27cSSam Leffler ath_getchannels(struct ath_softc *sc)
6170b032f27cSSam Leffler {
6171b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
6172b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
6173b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
617459efa8b5SSam Leffler 	HAL_STATUS status;
6175b032f27cSSam Leffler 
6176b032f27cSSam Leffler 	/*
617759efa8b5SSam Leffler 	 * Collect channel set based on EEPROM contents.
6178b032f27cSSam Leffler 	 */
617959efa8b5SSam Leffler 	status = ath_hal_init_channels(ah, ic->ic_channels, IEEE80211_CHAN_MAX,
618059efa8b5SSam Leffler 	    &ic->ic_nchans, HAL_MODE_ALL, CTRY_DEFAULT, SKU_NONE, AH_TRUE);
618159efa8b5SSam Leffler 	if (status != HAL_OK) {
618259efa8b5SSam Leffler 		if_printf(ifp, "%s: unable to collect channel list from hal, "
618359efa8b5SSam Leffler 		    "status %d\n", __func__, status);
618459efa8b5SSam Leffler 		return EINVAL;
618559efa8b5SSam Leffler 	}
6186ca876918SSam Leffler 	(void) ath_hal_getregdomain(ah, &sc->sc_eerd);
6187ca876918SSam Leffler 	ath_hal_getcountrycode(ah, &sc->sc_eecc);	/* NB: cannot fail */
618859efa8b5SSam Leffler 	/* XXX map Atheros sku's to net80211 SKU's */
618959efa8b5SSam Leffler 	/* XXX net80211 types too small */
619059efa8b5SSam Leffler 	ic->ic_regdomain.regdomain = (uint16_t) sc->sc_eerd;
619159efa8b5SSam Leffler 	ic->ic_regdomain.country = (uint16_t) sc->sc_eecc;
619259efa8b5SSam Leffler 	ic->ic_regdomain.isocc[0] = ' ';	/* XXX don't know */
619359efa8b5SSam Leffler 	ic->ic_regdomain.isocc[1] = ' ';
619459efa8b5SSam Leffler 
6195b032f27cSSam Leffler 	ic->ic_regdomain.ecm = 1;
6196b032f27cSSam Leffler 	ic->ic_regdomain.location = 'I';
6197033022a9SSam Leffler 
6198033022a9SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
619959efa8b5SSam Leffler 	    "%s: eeprom rd %u cc %u (mapped rd %u cc %u) location %c%s\n",
6200033022a9SSam Leffler 	    __func__, sc->sc_eerd, sc->sc_eecc,
6201033022a9SSam Leffler 	    ic->ic_regdomain.regdomain, ic->ic_regdomain.country,
620259efa8b5SSam Leffler 	    ic->ic_regdomain.location, ic->ic_regdomain.ecm ? " ecm" : "");
62035591b213SSam Leffler 	return 0;
62045591b213SSam Leffler }
62055591b213SSam Leffler 
62066c4612b9SSam Leffler static int
62076c4612b9SSam Leffler ath_rate_setup(struct ath_softc *sc, u_int mode)
62086c4612b9SSam Leffler {
62096c4612b9SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
62106c4612b9SSam Leffler 	const HAL_RATE_TABLE *rt;
62116c4612b9SSam Leffler 
62126c4612b9SSam Leffler 	switch (mode) {
62136c4612b9SSam Leffler 	case IEEE80211_MODE_11A:
62146c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A);
62156c4612b9SSam Leffler 		break;
6216724c193aSSam Leffler 	case IEEE80211_MODE_HALF:
6217aaa70f2fSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_HALF_RATE);
6218aaa70f2fSSam Leffler 		break;
6219724c193aSSam Leffler 	case IEEE80211_MODE_QUARTER:
6220aaa70f2fSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_QUARTER_RATE);
6221aaa70f2fSSam Leffler 		break;
62226c4612b9SSam Leffler 	case IEEE80211_MODE_11B:
62236c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11B);
62246c4612b9SSam Leffler 		break;
62256c4612b9SSam Leffler 	case IEEE80211_MODE_11G:
62266c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11G);
62276c4612b9SSam Leffler 		break;
62286c4612b9SSam Leffler 	case IEEE80211_MODE_TURBO_A:
622968e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_108A);
62306c4612b9SSam Leffler 		break;
62316c4612b9SSam Leffler 	case IEEE80211_MODE_TURBO_G:
62326c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_108G);
62336c4612b9SSam Leffler 		break;
623468e8e04eSSam Leffler 	case IEEE80211_MODE_STURBO_A:
623568e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_TURBO);
623668e8e04eSSam Leffler 		break;
623768e8e04eSSam Leffler 	case IEEE80211_MODE_11NA:
623868e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11NA_HT20);
623968e8e04eSSam Leffler 		break;
624068e8e04eSSam Leffler 	case IEEE80211_MODE_11NG:
624168e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11NG_HT20);
624268e8e04eSSam Leffler 		break;
62436c4612b9SSam Leffler 	default:
62446c4612b9SSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid mode %u\n",
62456c4612b9SSam Leffler 			__func__, mode);
62466c4612b9SSam Leffler 		return 0;
62476c4612b9SSam Leffler 	}
62486c4612b9SSam Leffler 	sc->sc_rates[mode] = rt;
6249aaa70f2fSSam Leffler 	return (rt != NULL);
62505591b213SSam Leffler }
62515591b213SSam Leffler 
62525591b213SSam Leffler static void
62535591b213SSam Leffler ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode)
62545591b213SSam Leffler {
62553e50ec2cSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
62563e50ec2cSSam Leffler 	/* NB: on/off times from the Atheros NDIS driver, w/ permission */
62573e50ec2cSSam Leffler 	static const struct {
62583e50ec2cSSam Leffler 		u_int		rate;		/* tx/rx 802.11 rate */
62593e50ec2cSSam Leffler 		u_int16_t	timeOn;		/* LED on time (ms) */
62603e50ec2cSSam Leffler 		u_int16_t	timeOff;	/* LED off time (ms) */
62613e50ec2cSSam Leffler 	} blinkrates[] = {
62623e50ec2cSSam Leffler 		{ 108,  40,  10 },
62633e50ec2cSSam Leffler 		{  96,  44,  11 },
62643e50ec2cSSam Leffler 		{  72,  50,  13 },
62653e50ec2cSSam Leffler 		{  48,  57,  14 },
62663e50ec2cSSam Leffler 		{  36,  67,  16 },
62673e50ec2cSSam Leffler 		{  24,  80,  20 },
62683e50ec2cSSam Leffler 		{  22, 100,  25 },
62693e50ec2cSSam Leffler 		{  18, 133,  34 },
62703e50ec2cSSam Leffler 		{  12, 160,  40 },
62713e50ec2cSSam Leffler 		{  10, 200,  50 },
62723e50ec2cSSam Leffler 		{   6, 240,  58 },
62733e50ec2cSSam Leffler 		{   4, 267,  66 },
62743e50ec2cSSam Leffler 		{   2, 400, 100 },
62753e50ec2cSSam Leffler 		{   0, 500, 130 },
6276724c193aSSam Leffler 		/* XXX half/quarter rates */
62773e50ec2cSSam Leffler 	};
62785591b213SSam Leffler 	const HAL_RATE_TABLE *rt;
62793e50ec2cSSam Leffler 	int i, j;
62805591b213SSam Leffler 
62815591b213SSam Leffler 	memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap));
62825591b213SSam Leffler 	rt = sc->sc_rates[mode];
62835591b213SSam Leffler 	KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode));
6284180f268dSSam Leffler 	for (i = 0; i < rt->rateCount; i++) {
6285180f268dSSam Leffler 		uint8_t ieeerate = rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
6286180f268dSSam Leffler 		if (rt->info[i].phy != IEEE80211_T_HT)
6287180f268dSSam Leffler 			sc->sc_rixmap[ieeerate] = i;
6288180f268dSSam Leffler 		else
6289180f268dSSam Leffler 			sc->sc_rixmap[ieeerate | IEEE80211_RATE_MCS] = i;
6290180f268dSSam Leffler 	}
62911b1a8e41SSam Leffler 	memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap));
629246d4d74cSSam Leffler 	for (i = 0; i < N(sc->sc_hwmap); i++) {
629346d4d74cSSam Leffler 		if (i >= rt->rateCount) {
62943e50ec2cSSam Leffler 			sc->sc_hwmap[i].ledon = (500 * hz) / 1000;
62953e50ec2cSSam Leffler 			sc->sc_hwmap[i].ledoff = (130 * hz) / 1000;
629616b4851aSSam Leffler 			continue;
62973e50ec2cSSam Leffler 		}
62983e50ec2cSSam Leffler 		sc->sc_hwmap[i].ieeerate =
629946d4d74cSSam Leffler 			rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
630046d4d74cSSam Leffler 		if (rt->info[i].phy == IEEE80211_T_HT)
630126041a14SSam Leffler 			sc->sc_hwmap[i].ieeerate |= IEEE80211_RATE_MCS;
6302d3be6f5bSSam Leffler 		sc->sc_hwmap[i].txflags = IEEE80211_RADIOTAP_F_DATAPAD;
630346d4d74cSSam Leffler 		if (rt->info[i].shortPreamble ||
630446d4d74cSSam Leffler 		    rt->info[i].phy == IEEE80211_T_OFDM)
6305d3be6f5bSSam Leffler 			sc->sc_hwmap[i].txflags |= IEEE80211_RADIOTAP_F_SHORTPRE;
63065463c4a4SSam Leffler 		sc->sc_hwmap[i].rxflags = sc->sc_hwmap[i].txflags;
63073e50ec2cSSam Leffler 		for (j = 0; j < N(blinkrates)-1; j++)
63083e50ec2cSSam Leffler 			if (blinkrates[j].rate == sc->sc_hwmap[i].ieeerate)
63093e50ec2cSSam Leffler 				break;
63103e50ec2cSSam Leffler 		/* NB: this uses the last entry if the rate isn't found */
63113e50ec2cSSam Leffler 		/* XXX beware of overlow */
63123e50ec2cSSam Leffler 		sc->sc_hwmap[i].ledon = (blinkrates[j].timeOn * hz) / 1000;
63133e50ec2cSSam Leffler 		sc->sc_hwmap[i].ledoff = (blinkrates[j].timeOff * hz) / 1000;
6314c42a7b7eSSam Leffler 	}
63155591b213SSam Leffler 	sc->sc_currates = rt;
63165591b213SSam Leffler 	sc->sc_curmode = mode;
63175591b213SSam Leffler 	/*
6318c42a7b7eSSam Leffler 	 * All protection frames are transmited at 2Mb/s for
6319c42a7b7eSSam Leffler 	 * 11g, otherwise at 1Mb/s.
63205591b213SSam Leffler 	 */
6321913a1ba1SSam Leffler 	if (mode == IEEE80211_MODE_11G)
6322ab06fdf2SSam Leffler 		sc->sc_protrix = ath_tx_findrix(sc, 2*2);
6323913a1ba1SSam Leffler 	else
6324ab06fdf2SSam Leffler 		sc->sc_protrix = ath_tx_findrix(sc, 2*1);
63254fa8d4efSDaniel Eischen 	/* NB: caller is responsible for resetting rate control state */
63263e50ec2cSSam Leffler #undef N
63275591b213SSam Leffler }
63285591b213SSam Leffler 
6329c42a7b7eSSam Leffler static void
63302e986da5SSam Leffler ath_watchdog(void *arg)
6331c42a7b7eSSam Leffler {
63322e986da5SSam Leffler 	struct ath_softc *sc = arg;
6333ef27340cSAdrian Chadd 	int do_reset = 0;
6334c42a7b7eSSam Leffler 
63352e986da5SSam Leffler 	if (sc->sc_wd_timer != 0 && --sc->sc_wd_timer == 0) {
63362e986da5SSam Leffler 		struct ifnet *ifp = sc->sc_ifp;
6337459bc4f0SSam Leffler 		uint32_t hangs;
6338459bc4f0SSam Leffler 
6339459bc4f0SSam Leffler 		if (ath_hal_gethangstate(sc->sc_ah, 0xffff, &hangs) &&
6340459bc4f0SSam Leffler 		    hangs != 0) {
6341459bc4f0SSam Leffler 			if_printf(ifp, "%s hang detected (0x%x)\n",
6342459bc4f0SSam Leffler 			    hangs & 0xff ? "bb" : "mac", hangs);
6343459bc4f0SSam Leffler 		} else
6344c42a7b7eSSam Leffler 			if_printf(ifp, "device timeout\n");
6345ef27340cSAdrian Chadd 		do_reset = 1;
6346c42a7b7eSSam Leffler 		ifp->if_oerrors++;
6347c42a7b7eSSam Leffler 		sc->sc_stats.ast_watchdog++;
6348c42a7b7eSSam Leffler 	}
6349ef27340cSAdrian Chadd 
6350ef27340cSAdrian Chadd 	/*
6351ef27340cSAdrian Chadd 	 * We can't hold the lock across the ath_reset() call.
6352d52f7132SAdrian Chadd 	 *
6353d52f7132SAdrian Chadd 	 * And since this routine can't hold a lock and sleep,
6354d52f7132SAdrian Chadd 	 * do the reset deferred.
6355ef27340cSAdrian Chadd 	 */
6356ef27340cSAdrian Chadd 	if (do_reset) {
6357d52f7132SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_resettask);
6358ef27340cSAdrian Chadd 	}
6359ef27340cSAdrian Chadd 
63602e986da5SSam Leffler 	callout_schedule(&sc->sc_wd_ch, hz);
6361c42a7b7eSSam Leffler }
6362c42a7b7eSSam Leffler 
6363a585a9a1SSam Leffler #ifdef ATH_DIAGAPI
6364c42a7b7eSSam Leffler /*
6365c42a7b7eSSam Leffler  * Diagnostic interface to the HAL.  This is used by various
6366c42a7b7eSSam Leffler  * tools to do things like retrieve register contents for
6367c42a7b7eSSam Leffler  * debugging.  The mechanism is intentionally opaque so that
6368c42a7b7eSSam Leffler  * it can change frequently w/o concern for compatiblity.
6369c42a7b7eSSam Leffler  */
6370c42a7b7eSSam Leffler static int
6371c42a7b7eSSam Leffler ath_ioctl_diag(struct ath_softc *sc, struct ath_diag *ad)
6372c42a7b7eSSam Leffler {
6373c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
6374c42a7b7eSSam Leffler 	u_int id = ad->ad_id & ATH_DIAG_ID;
6375c42a7b7eSSam Leffler 	void *indata = NULL;
6376c42a7b7eSSam Leffler 	void *outdata = NULL;
6377c42a7b7eSSam Leffler 	u_int32_t insize = ad->ad_in_size;
6378c42a7b7eSSam Leffler 	u_int32_t outsize = ad->ad_out_size;
6379c42a7b7eSSam Leffler 	int error = 0;
6380c42a7b7eSSam Leffler 
6381c42a7b7eSSam Leffler 	if (ad->ad_id & ATH_DIAG_IN) {
6382c42a7b7eSSam Leffler 		/*
6383c42a7b7eSSam Leffler 		 * Copy in data.
6384c42a7b7eSSam Leffler 		 */
6385c42a7b7eSSam Leffler 		indata = malloc(insize, M_TEMP, M_NOWAIT);
6386c42a7b7eSSam Leffler 		if (indata == NULL) {
6387c42a7b7eSSam Leffler 			error = ENOMEM;
6388c42a7b7eSSam Leffler 			goto bad;
6389c42a7b7eSSam Leffler 		}
6390c42a7b7eSSam Leffler 		error = copyin(ad->ad_in_data, indata, insize);
6391c42a7b7eSSam Leffler 		if (error)
6392c42a7b7eSSam Leffler 			goto bad;
6393c42a7b7eSSam Leffler 	}
6394c42a7b7eSSam Leffler 	if (ad->ad_id & ATH_DIAG_DYN) {
6395c42a7b7eSSam Leffler 		/*
6396c42a7b7eSSam Leffler 		 * Allocate a buffer for the results (otherwise the HAL
6397c42a7b7eSSam Leffler 		 * returns a pointer to a buffer where we can read the
6398c42a7b7eSSam Leffler 		 * results).  Note that we depend on the HAL leaving this
6399c42a7b7eSSam Leffler 		 * pointer for us to use below in reclaiming the buffer;
6400c42a7b7eSSam Leffler 		 * may want to be more defensive.
6401c42a7b7eSSam Leffler 		 */
6402c42a7b7eSSam Leffler 		outdata = malloc(outsize, M_TEMP, M_NOWAIT);
6403c42a7b7eSSam Leffler 		if (outdata == NULL) {
6404c42a7b7eSSam Leffler 			error = ENOMEM;
6405c42a7b7eSSam Leffler 			goto bad;
6406c42a7b7eSSam Leffler 		}
6407c42a7b7eSSam Leffler 	}
6408c42a7b7eSSam Leffler 	if (ath_hal_getdiagstate(ah, id, indata, insize, &outdata, &outsize)) {
6409c42a7b7eSSam Leffler 		if (outsize < ad->ad_out_size)
6410c42a7b7eSSam Leffler 			ad->ad_out_size = outsize;
6411c42a7b7eSSam Leffler 		if (outdata != NULL)
6412c42a7b7eSSam Leffler 			error = copyout(outdata, ad->ad_out_data,
6413c42a7b7eSSam Leffler 					ad->ad_out_size);
6414c42a7b7eSSam Leffler 	} else {
6415c42a7b7eSSam Leffler 		error = EINVAL;
6416c42a7b7eSSam Leffler 	}
6417c42a7b7eSSam Leffler bad:
6418c42a7b7eSSam Leffler 	if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
6419c42a7b7eSSam Leffler 		free(indata, M_TEMP);
6420c42a7b7eSSam Leffler 	if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
6421c42a7b7eSSam Leffler 		free(outdata, M_TEMP);
6422c42a7b7eSSam Leffler 	return error;
6423c42a7b7eSSam Leffler }
6424a585a9a1SSam Leffler #endif /* ATH_DIAGAPI */
6425c42a7b7eSSam Leffler 
6426c42a7b7eSSam Leffler static int
6427c42a7b7eSSam Leffler ath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
6428c42a7b7eSSam Leffler {
6429c42a7b7eSSam Leffler #define	IS_RUNNING(ifp) \
643013f4c340SRobert Watson 	((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
6431c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
6432b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
6433c42a7b7eSSam Leffler 	struct ifreq *ifr = (struct ifreq *)data;
643484784be1SSam Leffler 	const HAL_RATE_TABLE *rt;
6435c42a7b7eSSam Leffler 	int error = 0;
6436c42a7b7eSSam Leffler 
6437c42a7b7eSSam Leffler 	switch (cmd) {
6438c42a7b7eSSam Leffler 	case SIOCSIFFLAGS:
643931a8c1edSAndrew Thompson 		ATH_LOCK(sc);
6440c42a7b7eSSam Leffler 		if (IS_RUNNING(ifp)) {
6441c42a7b7eSSam Leffler 			/*
6442c42a7b7eSSam Leffler 			 * To avoid rescanning another access point,
6443c42a7b7eSSam Leffler 			 * do not call ath_init() here.  Instead,
6444c42a7b7eSSam Leffler 			 * only reflect promisc mode settings.
6445c42a7b7eSSam Leffler 			 */
6446c42a7b7eSSam Leffler 			ath_mode_init(sc);
6447c42a7b7eSSam Leffler 		} else if (ifp->if_flags & IFF_UP) {
6448c42a7b7eSSam Leffler 			/*
6449c42a7b7eSSam Leffler 			 * Beware of being called during attach/detach
6450c42a7b7eSSam Leffler 			 * to reset promiscuous mode.  In that case we
6451c42a7b7eSSam Leffler 			 * will still be marked UP but not RUNNING.
6452c42a7b7eSSam Leffler 			 * However trying to re-init the interface
6453c42a7b7eSSam Leffler 			 * is the wrong thing to do as we've already
6454c42a7b7eSSam Leffler 			 * torn down much of our state.  There's
6455c42a7b7eSSam Leffler 			 * probably a better way to deal with this.
6456c42a7b7eSSam Leffler 			 */
6457b032f27cSSam Leffler 			if (!sc->sc_invalid)
6458fc74a9f9SBrooks Davis 				ath_init(sc);	/* XXX lose error */
6459d3ac945bSSam Leffler 		} else {
6460c42a7b7eSSam Leffler 			ath_stop_locked(ifp);
6461d3ac945bSSam Leffler #ifdef notyet
6462d3ac945bSSam Leffler 			/* XXX must wakeup in places like ath_vap_delete */
6463d3ac945bSSam Leffler 			if (!sc->sc_invalid)
6464d3ac945bSSam Leffler 				ath_hal_setpower(sc->sc_ah, HAL_PM_FULL_SLEEP);
6465d3ac945bSSam Leffler #endif
6466d3ac945bSSam Leffler 		}
646731a8c1edSAndrew Thompson 		ATH_UNLOCK(sc);
6468c42a7b7eSSam Leffler 		break;
6469b032f27cSSam Leffler 	case SIOCGIFMEDIA:
6470b032f27cSSam Leffler 	case SIOCSIFMEDIA:
6471b032f27cSSam Leffler 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
6472b032f27cSSam Leffler 		break;
6473c42a7b7eSSam Leffler 	case SIOCGATHSTATS:
6474c42a7b7eSSam Leffler 		/* NB: embed these numbers to get a consistent view */
6475c42a7b7eSSam Leffler 		sc->sc_stats.ast_tx_packets = ifp->if_opackets;
6476c42a7b7eSSam Leffler 		sc->sc_stats.ast_rx_packets = ifp->if_ipackets;
647784784be1SSam Leffler 		sc->sc_stats.ast_tx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgtxrssi);
647884784be1SSam Leffler 		sc->sc_stats.ast_rx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgrssi);
6479584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
648010ad9a77SSam Leffler 		sc->sc_stats.ast_tdma_tsfadjp = TDMA_AVG(sc->sc_avgtsfdeltap);
648110ad9a77SSam Leffler 		sc->sc_stats.ast_tdma_tsfadjm = TDMA_AVG(sc->sc_avgtsfdeltam);
648210ad9a77SSam Leffler #endif
648384784be1SSam Leffler 		rt = sc->sc_currates;
648446d4d74cSSam Leffler 		sc->sc_stats.ast_tx_rate =
648546d4d74cSSam Leffler 		    rt->info[sc->sc_txrix].dot11Rate &~ IEEE80211_RATE_BASIC;
64866aa113fdSAdrian Chadd 		if (rt->info[sc->sc_txrix].phy & IEEE80211_T_HT)
64876aa113fdSAdrian Chadd 			sc->sc_stats.ast_tx_rate |= IEEE80211_RATE_MCS;
6488c42a7b7eSSam Leffler 		return copyout(&sc->sc_stats,
6489c42a7b7eSSam Leffler 		    ifr->ifr_data, sizeof (sc->sc_stats));
64903fc21fedSSam Leffler 	case SIOCZATHSTATS:
64913fc21fedSSam Leffler 		error = priv_check(curthread, PRIV_DRIVER);
64929467e3f3SAdrian Chadd 		if (error == 0) {
64933fc21fedSSam Leffler 			memset(&sc->sc_stats, 0, sizeof(sc->sc_stats));
649441b6b507SAdrian Chadd 			memset(&sc->sc_aggr_stats, 0,
649541b6b507SAdrian Chadd 			    sizeof(sc->sc_aggr_stats));
64969467e3f3SAdrian Chadd 			memset(&sc->sc_intr_stats, 0,
64979467e3f3SAdrian Chadd 			    sizeof(sc->sc_intr_stats));
64989467e3f3SAdrian Chadd 		}
64993fc21fedSSam Leffler 		break;
6500a585a9a1SSam Leffler #ifdef ATH_DIAGAPI
6501c42a7b7eSSam Leffler 	case SIOCGATHDIAG:
6502c42a7b7eSSam Leffler 		error = ath_ioctl_diag(sc, (struct ath_diag *) ifr);
6503c42a7b7eSSam Leffler 		break;
6504f51c84eaSAdrian Chadd 	case SIOCGATHPHYERR:
6505f51c84eaSAdrian Chadd 		error = ath_ioctl_phyerr(sc,(struct ath_diag*) ifr);
6506f51c84eaSAdrian Chadd 		break;
6507a585a9a1SSam Leffler #endif
650831a8c1edSAndrew Thompson 	case SIOCGIFADDR:
6509b032f27cSSam Leffler 		error = ether_ioctl(ifp, cmd, data);
6510c42a7b7eSSam Leffler 		break;
651131a8c1edSAndrew Thompson 	default:
651231a8c1edSAndrew Thompson 		error = EINVAL;
651331a8c1edSAndrew Thompson 		break;
6514c42a7b7eSSam Leffler 	}
6515c42a7b7eSSam Leffler 	return error;
6516a614e076SSam Leffler #undef IS_RUNNING
6517c42a7b7eSSam Leffler }
6518c42a7b7eSSam Leffler 
6519c42a7b7eSSam Leffler /*
6520c42a7b7eSSam Leffler  * Announce various information on device/driver attach.
6521c42a7b7eSSam Leffler  */
6522c42a7b7eSSam Leffler static void
6523c42a7b7eSSam Leffler ath_announce(struct ath_softc *sc)
6524c42a7b7eSSam Leffler {
6525fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
6526c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
6527c42a7b7eSSam Leffler 
6528498657cfSSam Leffler 	if_printf(ifp, "AR%s mac %d.%d RF%s phy %d.%d\n",
6529498657cfSSam Leffler 		ath_hal_mac_name(ah), ah->ah_macVersion, ah->ah_macRev,
6530498657cfSSam Leffler 		ath_hal_rf_name(ah), ah->ah_phyRev >> 4, ah->ah_phyRev & 0xf);
653146a924c4SAdrian Chadd 	if_printf(ifp, "2GHz radio: 0x%.4x; 5GHz radio: 0x%.4x\n",
653246a924c4SAdrian Chadd 		ah->ah_analog2GhzRev, ah->ah_analog5GhzRev);
6533c42a7b7eSSam Leffler 	if (bootverbose) {
6534c42a7b7eSSam Leffler 		int i;
6535c42a7b7eSSam Leffler 		for (i = 0; i <= WME_AC_VO; i++) {
6536c42a7b7eSSam Leffler 			struct ath_txq *txq = sc->sc_ac2q[i];
6537c42a7b7eSSam Leffler 			if_printf(ifp, "Use hw queue %u for %s traffic\n",
6538c42a7b7eSSam Leffler 				txq->axq_qnum, ieee80211_wme_acnames[i]);
6539c42a7b7eSSam Leffler 		}
6540c42a7b7eSSam Leffler 		if_printf(ifp, "Use hw queue %u for CAB traffic\n",
6541c42a7b7eSSam Leffler 			sc->sc_cabq->axq_qnum);
6542c42a7b7eSSam Leffler 		if_printf(ifp, "Use hw queue %u for beacons\n", sc->sc_bhalq);
6543c42a7b7eSSam Leffler 	}
6544e2d787faSSam Leffler 	if (ath_rxbuf != ATH_RXBUF)
6545e2d787faSSam Leffler 		if_printf(ifp, "using %u rx buffers\n", ath_rxbuf);
6546e2d787faSSam Leffler 	if (ath_txbuf != ATH_TXBUF)
6547e2d787faSSam Leffler 		if_printf(ifp, "using %u tx buffers\n", ath_txbuf);
65489ac01d39SRui Paulo 	if (sc->sc_mcastkey && bootverbose)
65499ac01d39SRui Paulo 		if_printf(ifp, "using multicast key search\n");
6550c42a7b7eSSam Leffler }
655110ad9a77SSam Leffler 
6552584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
655310ad9a77SSam Leffler static void
655410ad9a77SSam Leffler ath_tdma_settimers(struct ath_softc *sc, u_int32_t nexttbtt, u_int32_t bintval)
655510ad9a77SSam Leffler {
655610ad9a77SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
655710ad9a77SSam Leffler 	HAL_BEACON_TIMERS bt;
655810ad9a77SSam Leffler 
655910ad9a77SSam Leffler 	bt.bt_intval = bintval | HAL_BEACON_ENA;
656010ad9a77SSam Leffler 	bt.bt_nexttbtt = nexttbtt;
656110ad9a77SSam Leffler 	bt.bt_nextdba = (nexttbtt<<3) - sc->sc_tdmadbaprep;
656210ad9a77SSam Leffler 	bt.bt_nextswba = (nexttbtt<<3) - sc->sc_tdmaswbaprep;
656310ad9a77SSam Leffler 	bt.bt_nextatim = nexttbtt+1;
6564f3fb1687SAdrian Chadd 	/* Enables TBTT, DBA, SWBA timers by default */
6565f3fb1687SAdrian Chadd 	bt.bt_flags = 0;
656610ad9a77SSam Leffler 	ath_hal_beaconsettimers(ah, &bt);
656710ad9a77SSam Leffler }
656810ad9a77SSam Leffler 
656910ad9a77SSam Leffler /*
657010ad9a77SSam Leffler  * Calculate the beacon interval.  This is periodic in the
657110ad9a77SSam Leffler  * superframe for the bss.  We assume each station is configured
657210ad9a77SSam Leffler  * identically wrt transmit rate so the guard time we calculate
657310ad9a77SSam Leffler  * above will be the same on all stations.  Note we need to
657410ad9a77SSam Leffler  * factor in the xmit time because the hardware will schedule
657510ad9a77SSam Leffler  * a frame for transmit if the start of the frame is within
657610ad9a77SSam Leffler  * the burst time.  When we get hardware that properly kills
657710ad9a77SSam Leffler  * frames in the PCU we can reduce/eliminate the guard time.
657810ad9a77SSam Leffler  *
657910ad9a77SSam Leffler  * Roundup to 1024 is so we have 1 TU buffer in the guard time
658010ad9a77SSam Leffler  * to deal with the granularity of the nexttbtt timer.  11n MAC's
658110ad9a77SSam Leffler  * with 1us timer granularity should allow us to reduce/eliminate
658210ad9a77SSam Leffler  * this.
658310ad9a77SSam Leffler  */
658410ad9a77SSam Leffler static void
658510ad9a77SSam Leffler ath_tdma_bintvalsetup(struct ath_softc *sc,
658610ad9a77SSam Leffler 	const struct ieee80211_tdma_state *tdma)
658710ad9a77SSam Leffler {
658810ad9a77SSam Leffler 	/* copy from vap state (XXX check all vaps have same value?) */
658910ad9a77SSam Leffler 	sc->sc_tdmaslotlen = tdma->tdma_slotlen;
659010ad9a77SSam Leffler 
659110ad9a77SSam Leffler 	sc->sc_tdmabintval = roundup((sc->sc_tdmaslotlen+sc->sc_tdmaguard) *
659210ad9a77SSam Leffler 		tdma->tdma_slotcnt, 1024);
659310ad9a77SSam Leffler 	sc->sc_tdmabintval >>= 10;		/* TSF -> TU */
659410ad9a77SSam Leffler 	if (sc->sc_tdmabintval & 1)
659510ad9a77SSam Leffler 		sc->sc_tdmabintval++;
659610ad9a77SSam Leffler 
659710ad9a77SSam Leffler 	if (tdma->tdma_slot == 0) {
659810ad9a77SSam Leffler 		/*
659910ad9a77SSam Leffler 		 * Only slot 0 beacons; other slots respond.
660010ad9a77SSam Leffler 		 */
660110ad9a77SSam Leffler 		sc->sc_imask |= HAL_INT_SWBA;
660210ad9a77SSam Leffler 		sc->sc_tdmaswba = 0;		/* beacon immediately */
660310ad9a77SSam Leffler 	} else {
660410ad9a77SSam Leffler 		/* XXX all vaps must be slot 0 or slot !0 */
660510ad9a77SSam Leffler 		sc->sc_imask &= ~HAL_INT_SWBA;
660610ad9a77SSam Leffler 	}
660710ad9a77SSam Leffler }
660810ad9a77SSam Leffler 
660910ad9a77SSam Leffler /*
661010ad9a77SSam Leffler  * Max 802.11 overhead.  This assumes no 4-address frames and
661110ad9a77SSam Leffler  * the encapsulation done by ieee80211_encap (llc).  We also
661210ad9a77SSam Leffler  * include potential crypto overhead.
661310ad9a77SSam Leffler  */
661410ad9a77SSam Leffler #define	IEEE80211_MAXOVERHEAD \
661510ad9a77SSam Leffler 	(sizeof(struct ieee80211_qosframe) \
661610ad9a77SSam Leffler 	 + sizeof(struct llc) \
661710ad9a77SSam Leffler 	 + IEEE80211_ADDR_LEN \
661810ad9a77SSam Leffler 	 + IEEE80211_WEP_IVLEN \
661910ad9a77SSam Leffler 	 + IEEE80211_WEP_KIDLEN \
662010ad9a77SSam Leffler 	 + IEEE80211_WEP_CRCLEN \
662110ad9a77SSam Leffler 	 + IEEE80211_WEP_MICLEN \
662210ad9a77SSam Leffler 	 + IEEE80211_CRC_LEN)
662310ad9a77SSam Leffler 
662410ad9a77SSam Leffler /*
662510ad9a77SSam Leffler  * Setup initially for tdma operation.  Start the beacon
662610ad9a77SSam Leffler  * timers and enable SWBA if we are slot 0.  Otherwise
662710ad9a77SSam Leffler  * we wait for slot 0 to arrive so we can sync up before
662810ad9a77SSam Leffler  * starting to transmit.
662910ad9a77SSam Leffler  */
663010ad9a77SSam Leffler static void
663110ad9a77SSam Leffler ath_tdma_config(struct ath_softc *sc, struct ieee80211vap *vap)
663210ad9a77SSam Leffler {
663310ad9a77SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
663410ad9a77SSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
663510ad9a77SSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
663610ad9a77SSam Leffler 	const struct ieee80211_txparam *tp;
663710ad9a77SSam Leffler 	const struct ieee80211_tdma_state *tdma = NULL;
663810ad9a77SSam Leffler 	int rix;
663910ad9a77SSam Leffler 
664010ad9a77SSam Leffler 	if (vap == NULL) {
664110ad9a77SSam Leffler 		vap = TAILQ_FIRST(&ic->ic_vaps);   /* XXX */
664210ad9a77SSam Leffler 		if (vap == NULL) {
664310ad9a77SSam Leffler 			if_printf(ifp, "%s: no vaps?\n", __func__);
664410ad9a77SSam Leffler 			return;
664510ad9a77SSam Leffler 		}
664610ad9a77SSam Leffler 	}
664780767531SAdrian Chadd 	/* XXX should take a locked ref to iv_bss */
664810ad9a77SSam Leffler 	tp = vap->iv_bss->ni_txparms;
664910ad9a77SSam Leffler 	/*
665010ad9a77SSam Leffler 	 * Calculate the guard time for each slot.  This is the
665110ad9a77SSam Leffler 	 * time to send a maximal-size frame according to the
665210ad9a77SSam Leffler 	 * fixed/lowest transmit rate.  Note that the interface
665310ad9a77SSam Leffler 	 * mtu does not include the 802.11 overhead so we must
665410ad9a77SSam Leffler 	 * tack that on (ath_hal_computetxtime includes the
665510ad9a77SSam Leffler 	 * preamble and plcp in it's calculation).
665610ad9a77SSam Leffler 	 */
665710ad9a77SSam Leffler 	tdma = vap->iv_tdma;
665810ad9a77SSam Leffler 	if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
6659ab06fdf2SSam Leffler 		rix = ath_tx_findrix(sc, tp->ucastrate);
666010ad9a77SSam Leffler 	else
6661ab06fdf2SSam Leffler 		rix = ath_tx_findrix(sc, tp->mcastrate);
666210ad9a77SSam Leffler 	/* XXX short preamble assumed */
666310ad9a77SSam Leffler 	sc->sc_tdmaguard = ath_hal_computetxtime(ah, sc->sc_currates,
666410ad9a77SSam Leffler 		ifp->if_mtu + IEEE80211_MAXOVERHEAD, rix, AH_TRUE);
666510ad9a77SSam Leffler 
666610ad9a77SSam Leffler 	ath_hal_intrset(ah, 0);
666710ad9a77SSam Leffler 
666810ad9a77SSam Leffler 	ath_beaconq_config(sc);			/* setup h/w beacon q */
66699c859a04SSam Leffler 	if (sc->sc_setcca)
667010ad9a77SSam Leffler 		ath_hal_setcca(ah, AH_FALSE);	/* disable CCA */
667110ad9a77SSam Leffler 	ath_tdma_bintvalsetup(sc, tdma);	/* calculate beacon interval */
667210ad9a77SSam Leffler 	ath_tdma_settimers(sc, sc->sc_tdmabintval,
667310ad9a77SSam Leffler 		sc->sc_tdmabintval | HAL_BEACON_RESET_TSF);
667410ad9a77SSam Leffler 	sc->sc_syncbeacon = 0;
667510ad9a77SSam Leffler 
667610ad9a77SSam Leffler 	sc->sc_avgtsfdeltap = TDMA_DUMMY_MARKER;
667710ad9a77SSam Leffler 	sc->sc_avgtsfdeltam = TDMA_DUMMY_MARKER;
667810ad9a77SSam Leffler 
667910ad9a77SSam Leffler 	ath_hal_intrset(ah, sc->sc_imask);
668010ad9a77SSam Leffler 
668110ad9a77SSam Leffler 	DPRINTF(sc, ATH_DEBUG_TDMA, "%s: slot %u len %uus cnt %u "
668210ad9a77SSam Leffler 	    "bsched %u guard %uus bintval %u TU dba prep %u\n", __func__,
668310ad9a77SSam Leffler 	    tdma->tdma_slot, tdma->tdma_slotlen, tdma->tdma_slotcnt,
668410ad9a77SSam Leffler 	    tdma->tdma_bintval, sc->sc_tdmaguard, sc->sc_tdmabintval,
668510ad9a77SSam Leffler 	    sc->sc_tdmadbaprep);
668610ad9a77SSam Leffler }
668710ad9a77SSam Leffler 
668810ad9a77SSam Leffler /*
668910ad9a77SSam Leffler  * Update tdma operation.  Called from the 802.11 layer
669010ad9a77SSam Leffler  * when a beacon is received from the TDMA station operating
669110ad9a77SSam Leffler  * in the slot immediately preceding us in the bss.  Use
669210ad9a77SSam Leffler  * the rx timestamp for the beacon frame to update our
669310ad9a77SSam Leffler  * beacon timers so we follow their schedule.  Note that
669410ad9a77SSam Leffler  * by using the rx timestamp we implicitly include the
669510ad9a77SSam Leffler  * propagation delay in our schedule.
669610ad9a77SSam Leffler  */
669710ad9a77SSam Leffler static void
669810ad9a77SSam Leffler ath_tdma_update(struct ieee80211_node *ni,
66992bc3ce77SSam Leffler 	const struct ieee80211_tdma_param *tdma, int changed)
670010ad9a77SSam Leffler {
670110ad9a77SSam Leffler #define	TSF_TO_TU(_h,_l) \
670210ad9a77SSam Leffler 	((((u_int32_t)(_h)) << 22) | (((u_int32_t)(_l)) >> 10))
670310ad9a77SSam Leffler #define	TU_TO_TSF(_tu)	(((u_int64_t)(_tu)) << 10)
670410ad9a77SSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
670510ad9a77SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
670610ad9a77SSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
670710ad9a77SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
670810ad9a77SSam Leffler 	const HAL_RATE_TABLE *rt = sc->sc_currates;
6709fc4de9b7SAdrian Chadd 	u_int64_t tsf, rstamp, nextslot, nexttbtt;
6710fc4de9b7SAdrian Chadd 	u_int32_t txtime, nextslottu;
671110ad9a77SSam Leffler 	int32_t tudelta, tsfdelta;
671210ad9a77SSam Leffler 	const struct ath_rx_status *rs;
671310ad9a77SSam Leffler 	int rix;
671410ad9a77SSam Leffler 
671510ad9a77SSam Leffler 	sc->sc_stats.ast_tdma_update++;
671610ad9a77SSam Leffler 
671710ad9a77SSam Leffler 	/*
671810ad9a77SSam Leffler 	 * Check for and adopt configuration changes.
671910ad9a77SSam Leffler 	 */
67202bc3ce77SSam Leffler 	if (changed != 0) {
672110ad9a77SSam Leffler 		const struct ieee80211_tdma_state *ts = vap->iv_tdma;
672210ad9a77SSam Leffler 
672310ad9a77SSam Leffler 		ath_tdma_bintvalsetup(sc, ts);
6724040972a1SSam Leffler 		if (changed & TDMA_UPDATE_SLOTLEN)
6725040972a1SSam Leffler 			ath_wme_update(ic);
672610ad9a77SSam Leffler 
672710ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_TDMA,
672810ad9a77SSam Leffler 		    "%s: adopt slot %u slotcnt %u slotlen %u us "
672910ad9a77SSam Leffler 		    "bintval %u TU\n", __func__,
673010ad9a77SSam Leffler 		    ts->tdma_slot, ts->tdma_slotcnt, ts->tdma_slotlen,
673110ad9a77SSam Leffler 		    sc->sc_tdmabintval);
673210ad9a77SSam Leffler 
673310ad9a77SSam Leffler 		/* XXX right? */
673410ad9a77SSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
673510ad9a77SSam Leffler 		/* NB: beacon timers programmed below */
673610ad9a77SSam Leffler 	}
673710ad9a77SSam Leffler 
673810ad9a77SSam Leffler 	/* extend rx timestamp to 64 bits */
67395463c4a4SSam Leffler 	rs = sc->sc_lastrs;
674010ad9a77SSam Leffler 	tsf = ath_hal_gettsf64(ah);
6741fc4de9b7SAdrian Chadd 	rstamp = ath_extend_tsf(sc, rs->rs_tstamp, tsf);
674210ad9a77SSam Leffler 	/*
674310ad9a77SSam Leffler 	 * The rx timestamp is set by the hardware on completing
674410ad9a77SSam Leffler 	 * reception (at the point where the rx descriptor is DMA'd
674510ad9a77SSam Leffler 	 * to the host).  To find the start of our next slot we
674610ad9a77SSam Leffler 	 * must adjust this time by the time required to send
674710ad9a77SSam Leffler 	 * the packet just received.
674810ad9a77SSam Leffler 	 */
674910ad9a77SSam Leffler 	rix = rt->rateCodeToIndex[rs->rs_rate];
675010ad9a77SSam Leffler 	txtime = ath_hal_computetxtime(ah, rt, rs->rs_datalen, rix,
675110ad9a77SSam Leffler 	    rt->info[rix].shortPreamble);
675210ad9a77SSam Leffler 	/* NB: << 9 is to cvt to TU and /2 */
675310ad9a77SSam Leffler 	nextslot = (rstamp - txtime) + (sc->sc_tdmabintval << 9);
675410ad9a77SSam Leffler 	nextslottu = TSF_TO_TU(nextslot>>32, nextslot) & HAL_BEACON_PERIOD;
675510ad9a77SSam Leffler 
675610ad9a77SSam Leffler 	/*
6757fc4de9b7SAdrian Chadd 	 * Retrieve the hardware NextTBTT in usecs
6758fc4de9b7SAdrian Chadd 	 * and calculate the difference between what the
675910ad9a77SSam Leffler 	 * other station thinks and what we have programmed.  This
676010ad9a77SSam Leffler 	 * lets us figure how to adjust our timers to match.  The
676110ad9a77SSam Leffler 	 * adjustments are done by pulling the TSF forward and possibly
676210ad9a77SSam Leffler 	 * rewriting the beacon timers.
676310ad9a77SSam Leffler 	 */
6764fc4de9b7SAdrian Chadd 	nexttbtt = ath_hal_getnexttbtt(ah);
6765fc4de9b7SAdrian Chadd 	tsfdelta = (int32_t)((nextslot % TU_TO_TSF(HAL_BEACON_PERIOD + 1)) - nexttbtt);
676610ad9a77SSam Leffler 
676710ad9a77SSam Leffler 	DPRINTF(sc, ATH_DEBUG_TDMA_TIMER,
676810ad9a77SSam Leffler 	    "tsfdelta %d avg +%d/-%d\n", tsfdelta,
676910ad9a77SSam Leffler 	    TDMA_AVG(sc->sc_avgtsfdeltap), TDMA_AVG(sc->sc_avgtsfdeltam));
677010ad9a77SSam Leffler 
677110ad9a77SSam Leffler 	if (tsfdelta < 0) {
677210ad9a77SSam Leffler 		TDMA_SAMPLE(sc->sc_avgtsfdeltap, 0);
677310ad9a77SSam Leffler 		TDMA_SAMPLE(sc->sc_avgtsfdeltam, -tsfdelta);
677410ad9a77SSam Leffler 		tsfdelta = -tsfdelta % 1024;
677510ad9a77SSam Leffler 		nextslottu++;
677610ad9a77SSam Leffler 	} else if (tsfdelta > 0) {
677710ad9a77SSam Leffler 		TDMA_SAMPLE(sc->sc_avgtsfdeltap, tsfdelta);
677810ad9a77SSam Leffler 		TDMA_SAMPLE(sc->sc_avgtsfdeltam, 0);
677910ad9a77SSam Leffler 		tsfdelta = 1024 - (tsfdelta % 1024);
678010ad9a77SSam Leffler 		nextslottu++;
678110ad9a77SSam Leffler 	} else {
678210ad9a77SSam Leffler 		TDMA_SAMPLE(sc->sc_avgtsfdeltap, 0);
678310ad9a77SSam Leffler 		TDMA_SAMPLE(sc->sc_avgtsfdeltam, 0);
678410ad9a77SSam Leffler 	}
6785fc4de9b7SAdrian Chadd 	tudelta = nextslottu - TSF_TO_TU(nexttbtt >> 32, nexttbtt);
678610ad9a77SSam Leffler 
678710ad9a77SSam Leffler 	/*
678810ad9a77SSam Leffler 	 * Copy sender's timetstamp into tdma ie so they can
678910ad9a77SSam Leffler 	 * calculate roundtrip time.  We submit a beacon frame
679010ad9a77SSam Leffler 	 * below after any timer adjustment.  The frame goes out
679110ad9a77SSam Leffler 	 * at the next TBTT so the sender can calculate the
679210ad9a77SSam Leffler 	 * roundtrip by inspecting the tdma ie in our beacon frame.
679310ad9a77SSam Leffler 	 *
679410ad9a77SSam Leffler 	 * NB: This tstamp is subtlely preserved when
679510ad9a77SSam Leffler 	 *     IEEE80211_BEACON_TDMA is marked (e.g. when the
679610ad9a77SSam Leffler 	 *     slot position changes) because ieee80211_add_tdma
679710ad9a77SSam Leffler 	 *     skips over the data.
679810ad9a77SSam Leffler 	 */
679910ad9a77SSam Leffler 	memcpy(ATH_VAP(vap)->av_boff.bo_tdma +
680010ad9a77SSam Leffler 		__offsetof(struct ieee80211_tdma_param, tdma_tstamp),
680110ad9a77SSam Leffler 		&ni->ni_tstamp.data, 8);
680210ad9a77SSam Leffler #if 0
680310ad9a77SSam Leffler 	DPRINTF(sc, ATH_DEBUG_TDMA_TIMER,
6804fc4de9b7SAdrian Chadd 	    "tsf %llu nextslot %llu (%d, %d) nextslottu %u nexttbtt %llu (%d)\n",
680510ad9a77SSam Leffler 	    (unsigned long long) tsf, (unsigned long long) nextslot,
6806fc4de9b7SAdrian Chadd 	    (int)(nextslot - tsf), tsfdelta, nextslottu, nexttbtt, tudelta);
680710ad9a77SSam Leffler #endif
680810ad9a77SSam Leffler 	/*
680910ad9a77SSam Leffler 	 * Adjust the beacon timers only when pulling them forward
681010ad9a77SSam Leffler 	 * or when going back by less than the beacon interval.
681110ad9a77SSam Leffler 	 * Negative jumps larger than the beacon interval seem to
681210ad9a77SSam Leffler 	 * cause the timers to stop and generally cause instability.
681310ad9a77SSam Leffler 	 * This basically filters out jumps due to missed beacons.
681410ad9a77SSam Leffler 	 */
681510ad9a77SSam Leffler 	if (tudelta != 0 && (tudelta > 0 || -tudelta < sc->sc_tdmabintval)) {
681610ad9a77SSam Leffler 		ath_tdma_settimers(sc, nextslottu, sc->sc_tdmabintval);
681710ad9a77SSam Leffler 		sc->sc_stats.ast_tdma_timers++;
681810ad9a77SSam Leffler 	}
681910ad9a77SSam Leffler 	if (tsfdelta > 0) {
682010ad9a77SSam Leffler 		ath_hal_adjusttsf(ah, tsfdelta);
682110ad9a77SSam Leffler 		sc->sc_stats.ast_tdma_tsf++;
682210ad9a77SSam Leffler 	}
682310ad9a77SSam Leffler 	ath_tdma_beacon_send(sc, vap);		/* prepare response */
682410ad9a77SSam Leffler #undef TU_TO_TSF
682510ad9a77SSam Leffler #undef TSF_TO_TU
682610ad9a77SSam Leffler }
682710ad9a77SSam Leffler 
682810ad9a77SSam Leffler /*
682910ad9a77SSam Leffler  * Transmit a beacon frame at SWBA.  Dynamic updates
683010ad9a77SSam Leffler  * to the frame contents are done as needed.
683110ad9a77SSam Leffler  */
683210ad9a77SSam Leffler static void
683310ad9a77SSam Leffler ath_tdma_beacon_send(struct ath_softc *sc, struct ieee80211vap *vap)
683410ad9a77SSam Leffler {
683510ad9a77SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
683610ad9a77SSam Leffler 	struct ath_buf *bf;
683710ad9a77SSam Leffler 	int otherant;
683810ad9a77SSam Leffler 
683910ad9a77SSam Leffler 	/*
684010ad9a77SSam Leffler 	 * Check if the previous beacon has gone out.  If
684110ad9a77SSam Leffler 	 * not don't try to post another, skip this period
684210ad9a77SSam Leffler 	 * and wait for the next.  Missed beacons indicate
684310ad9a77SSam Leffler 	 * a problem and should not occur.  If we miss too
684410ad9a77SSam Leffler 	 * many consecutive beacons reset the device.
684510ad9a77SSam Leffler 	 */
684610ad9a77SSam Leffler 	if (ath_hal_numtxpending(ah, sc->sc_bhalq) != 0) {
684710ad9a77SSam Leffler 		sc->sc_bmisscount++;
684810ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
684910ad9a77SSam Leffler 			"%s: missed %u consecutive beacons\n",
685010ad9a77SSam Leffler 			__func__, sc->sc_bmisscount);
6851a32ac9d3SSam Leffler 		if (sc->sc_bmisscount >= ath_bstuck_threshold)
685210ad9a77SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_bstucktask);
685310ad9a77SSam Leffler 		return;
685410ad9a77SSam Leffler 	}
685510ad9a77SSam Leffler 	if (sc->sc_bmisscount != 0) {
685610ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
685710ad9a77SSam Leffler 			"%s: resume beacon xmit after %u misses\n",
685810ad9a77SSam Leffler 			__func__, sc->sc_bmisscount);
685910ad9a77SSam Leffler 		sc->sc_bmisscount = 0;
686010ad9a77SSam Leffler 	}
686110ad9a77SSam Leffler 
686210ad9a77SSam Leffler 	/*
686310ad9a77SSam Leffler 	 * Check recent per-antenna transmit statistics and flip
686410ad9a77SSam Leffler 	 * the default antenna if noticeably more frames went out
686510ad9a77SSam Leffler 	 * on the non-default antenna.
686610ad9a77SSam Leffler 	 * XXX assumes 2 anntenae
686710ad9a77SSam Leffler 	 */
686810ad9a77SSam Leffler 	if (!sc->sc_diversity) {
686910ad9a77SSam Leffler 		otherant = sc->sc_defant & 1 ? 2 : 1;
687010ad9a77SSam Leffler 		if (sc->sc_ant_tx[otherant] > sc->sc_ant_tx[sc->sc_defant] + 2)
687110ad9a77SSam Leffler 			ath_setdefantenna(sc, otherant);
687210ad9a77SSam Leffler 		sc->sc_ant_tx[1] = sc->sc_ant_tx[2] = 0;
687310ad9a77SSam Leffler 	}
687410ad9a77SSam Leffler 
687510ad9a77SSam Leffler 	bf = ath_beacon_generate(sc, vap);
687610ad9a77SSam Leffler 	if (bf != NULL) {
687710ad9a77SSam Leffler 		/*
687810ad9a77SSam Leffler 		 * Stop any current dma and put the new frame on the queue.
687910ad9a77SSam Leffler 		 * This should never fail since we check above that no frames
688010ad9a77SSam Leffler 		 * are still pending on the queue.
688110ad9a77SSam Leffler 		 */
688210ad9a77SSam Leffler 		if (!ath_hal_stoptxdma(ah, sc->sc_bhalq)) {
688310ad9a77SSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
688410ad9a77SSam Leffler 				"%s: beacon queue %u did not stop?\n",
688510ad9a77SSam Leffler 				__func__, sc->sc_bhalq);
688610ad9a77SSam Leffler 			/* NB: the HAL still stops DMA, so proceed */
688710ad9a77SSam Leffler 		}
688810ad9a77SSam Leffler 		ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr);
688910ad9a77SSam Leffler 		ath_hal_txstart(ah, sc->sc_bhalq);
689010ad9a77SSam Leffler 
689110ad9a77SSam Leffler 		sc->sc_stats.ast_be_xmit++;		/* XXX per-vap? */
689210ad9a77SSam Leffler 
689310ad9a77SSam Leffler 		/*
689410ad9a77SSam Leffler 		 * Record local TSF for our last send for use
689510ad9a77SSam Leffler 		 * in arbitrating slot collisions.
689610ad9a77SSam Leffler 		 */
689780767531SAdrian Chadd 		/* XXX should take a locked ref to iv_bss */
689810ad9a77SSam Leffler 		vap->iv_bss->ni_tstamp.tsf = ath_hal_gettsf64(ah);
689910ad9a77SSam Leffler 	}
690010ad9a77SSam Leffler }
6901584f7327SSam Leffler #endif /* IEEE80211_SUPPORT_TDMA */
6902e8dabfbeSAdrian Chadd 
690348237774SAdrian Chadd static void
690448237774SAdrian Chadd ath_dfs_tasklet(void *p, int npending)
690548237774SAdrian Chadd {
690648237774SAdrian Chadd 	struct ath_softc *sc = (struct ath_softc *) p;
690748237774SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
690848237774SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
690948237774SAdrian Chadd 
691048237774SAdrian Chadd 	/*
691148237774SAdrian Chadd 	 * If previous processing has found a radar event,
691248237774SAdrian Chadd 	 * signal this to the net80211 layer to begin DFS
691348237774SAdrian Chadd 	 * processing.
691448237774SAdrian Chadd 	 */
691548237774SAdrian Chadd 	if (ath_dfs_process_radar_event(sc, sc->sc_curchan)) {
691648237774SAdrian Chadd 		/* DFS event found, initiate channel change */
691706fc4a10SAdrian Chadd 		/*
691806fc4a10SAdrian Chadd 		 * XXX doesn't currently tell us whether the event
691906fc4a10SAdrian Chadd 		 * XXX was found in the primary or extension
692006fc4a10SAdrian Chadd 		 * XXX channel!
692106fc4a10SAdrian Chadd 		 */
692206fc4a10SAdrian Chadd 		IEEE80211_LOCK(ic);
692348237774SAdrian Chadd 		ieee80211_dfs_notify_radar(ic, sc->sc_curchan);
692406fc4a10SAdrian Chadd 		IEEE80211_UNLOCK(ic);
692548237774SAdrian Chadd 	}
692648237774SAdrian Chadd }
692748237774SAdrian Chadd 
6928dba9c859SAdrian Chadd MODULE_VERSION(if_ath, 1);
6929dba9c859SAdrian Chadd MODULE_DEPEND(if_ath, wlan, 1, 1, 1);          /* 802.11 media layer */
693058816f3fSAdrian Chadd #if	defined(IEEE80211_ALQ) || defined(AH_DEBUG_ALQ)
693158816f3fSAdrian Chadd MODULE_DEPEND(if_ath, alq, 1, 1, 1);
693258816f3fSAdrian Chadd #endif
6933