xref: /freebsd/sys/dev/ath/if_ath.c (revision 96ff485dc313b174de168ba5262f0bcbf0617d78)
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().
453f3087fdSAdrian Chadd  */
463f3087fdSAdrian Chadd #include "opt_ah.h"
47584f7327SSam Leffler #include "opt_wlan.h"
485591b213SSam Leffler 
495591b213SSam Leffler #include <sys/param.h>
505591b213SSam Leffler #include <sys/systm.h>
515591b213SSam Leffler #include <sys/sysctl.h>
525591b213SSam Leffler #include <sys/mbuf.h>
535591b213SSam Leffler #include <sys/malloc.h>
545591b213SSam Leffler #include <sys/lock.h>
555591b213SSam Leffler #include <sys/mutex.h>
565591b213SSam Leffler #include <sys/kernel.h>
575591b213SSam Leffler #include <sys/socket.h>
585591b213SSam Leffler #include <sys/sockio.h>
595591b213SSam Leffler #include <sys/errno.h>
605591b213SSam Leffler #include <sys/callout.h>
615591b213SSam Leffler #include <sys/bus.h>
625591b213SSam Leffler #include <sys/endian.h>
630bbf5441SSam Leffler #include <sys/kthread.h>
640bbf5441SSam Leffler #include <sys/taskqueue.h>
653fc21fedSSam Leffler #include <sys/priv.h>
66dba9c859SAdrian Chadd #include <sys/module.h>
675591b213SSam Leffler 
685591b213SSam Leffler #include <machine/bus.h>
695591b213SSam Leffler 
705591b213SSam Leffler #include <net/if.h>
715591b213SSam Leffler #include <net/if_dl.h>
725591b213SSam Leffler #include <net/if_media.h>
73fc74a9f9SBrooks Davis #include <net/if_types.h>
745591b213SSam Leffler #include <net/if_arp.h>
755591b213SSam Leffler #include <net/ethernet.h>
765591b213SSam Leffler #include <net/if_llc.h>
775591b213SSam Leffler 
785591b213SSam Leffler #include <net80211/ieee80211_var.h>
7959efa8b5SSam Leffler #include <net80211/ieee80211_regdomain.h>
80339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
81339ccfb3SSam Leffler #include <net80211/ieee80211_superg.h>
82339ccfb3SSam Leffler #endif
83584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
8410ad9a77SSam Leffler #include <net80211/ieee80211_tdma.h>
8510ad9a77SSam Leffler #endif
865591b213SSam Leffler 
875591b213SSam Leffler #include <net/bpf.h>
885591b213SSam Leffler 
895591b213SSam Leffler #ifdef INET
905591b213SSam Leffler #include <netinet/in.h>
915591b213SSam Leffler #include <netinet/if_ether.h>
925591b213SSam Leffler #endif
935591b213SSam Leffler 
945591b213SSam Leffler #include <dev/ath/if_athvar.h>
9533644623SSam Leffler #include <dev/ath/ath_hal/ah_devid.h>		/* XXX for softled */
960dbe9289SAdrian Chadd #include <dev/ath/ath_hal/ah_diagcodes.h>
975591b213SSam Leffler 
985bc8125aSAdrian Chadd #include <dev/ath/if_ath_debug.h>
99b8e788a5SAdrian Chadd #include <dev/ath/if_ath_misc.h>
100b8e788a5SAdrian Chadd #include <dev/ath/if_ath_tx.h>
1016079fdbeSAdrian Chadd #include <dev/ath/if_ath_sysctl.h>
102d2d7a00aSAdrian Chadd #include <dev/ath/if_ath_keycache.h>
10348237774SAdrian Chadd #include <dev/ath/if_athdfs.h>
1045bc8125aSAdrian Chadd 
10586e07743SSam Leffler #ifdef ATH_TX99_DIAG
10686e07743SSam Leffler #include <dev/ath/ath_tx99/ath_tx99.h>
10786e07743SSam Leffler #endif
10886e07743SSam Leffler 
10948237774SAdrian Chadd 
110b032f27cSSam Leffler /*
111b032f27cSSam Leffler  * ATH_BCBUF determines the number of vap's that can transmit
112b032f27cSSam Leffler  * beacons and also (currently) the number of vap's that can
113b032f27cSSam Leffler  * have unique mac addresses/bssid.  When staggering beacons
114b032f27cSSam Leffler  * 4 is probably a good max as otherwise the beacons become
115b032f27cSSam Leffler  * very closely spaced and there is limited time for cab q traffic
116b032f27cSSam Leffler  * to go out.  You can burst beacons instead but that is not good
117b032f27cSSam Leffler  * for stations in power save and at some point you really want
118b032f27cSSam Leffler  * another radio (and channel).
119b032f27cSSam Leffler  *
120b032f27cSSam Leffler  * The limit on the number of mac addresses is tied to our use of
121b032f27cSSam Leffler  * the U/L bit and tracking addresses in a byte; it would be
122b032f27cSSam Leffler  * worthwhile to allow more for applications like proxy sta.
123b032f27cSSam Leffler  */
124b032f27cSSam Leffler CTASSERT(ATH_BCBUF <= 8);
125b032f27cSSam Leffler 
126b032f27cSSam Leffler static struct ieee80211vap *ath_vap_create(struct ieee80211com *,
127b032f27cSSam Leffler 		    const char name[IFNAMSIZ], int unit, int opmode,
128b032f27cSSam Leffler 		    int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
129b032f27cSSam Leffler 		    const uint8_t mac[IEEE80211_ADDR_LEN]);
130b032f27cSSam Leffler static void	ath_vap_delete(struct ieee80211vap *);
1315591b213SSam Leffler static void	ath_init(void *);
132c42a7b7eSSam Leffler static void	ath_stop_locked(struct ifnet *);
1335591b213SSam Leffler static void	ath_stop(struct ifnet *);
1345591b213SSam Leffler static void	ath_start(struct ifnet *);
135b032f27cSSam Leffler static int	ath_reset_vap(struct ieee80211vap *, u_long);
1365591b213SSam Leffler static int	ath_media_change(struct ifnet *);
1372e986da5SSam Leffler static void	ath_watchdog(void *);
1385591b213SSam Leffler static int	ath_ioctl(struct ifnet *, u_long, caddr_t);
1395591b213SSam Leffler static void	ath_fatal_proc(void *, int);
140b032f27cSSam Leffler static void	ath_bmiss_vap(struct ieee80211vap *);
1415591b213SSam Leffler static void	ath_bmiss_proc(void *, int);
142b032f27cSSam Leffler static void	ath_key_update_begin(struct ieee80211vap *);
143b032f27cSSam Leffler static void	ath_key_update_end(struct ieee80211vap *);
144b032f27cSSam Leffler static void	ath_update_mcast(struct ifnet *);
145b032f27cSSam Leffler static void	ath_update_promisc(struct ifnet *);
1465591b213SSam Leffler static void	ath_mode_init(struct ath_softc *);
147c42a7b7eSSam Leffler static void	ath_setslottime(struct ath_softc *);
148c42a7b7eSSam Leffler static void	ath_updateslot(struct ifnet *);
14980d2765fSSam Leffler static int	ath_beaconq_setup(struct ath_hal *);
1505591b213SSam Leffler static int	ath_beacon_alloc(struct ath_softc *, struct ieee80211_node *);
151b032f27cSSam Leffler static void	ath_beacon_update(struct ieee80211vap *, int item);
152c42a7b7eSSam Leffler static void	ath_beacon_setup(struct ath_softc *, struct ath_buf *);
1535591b213SSam Leffler static void	ath_beacon_proc(void *, int);
154b032f27cSSam Leffler static struct ath_buf *ath_beacon_generate(struct ath_softc *,
155b032f27cSSam Leffler 			struct ieee80211vap *);
156c42a7b7eSSam Leffler static void	ath_bstuck_proc(void *, int);
157b032f27cSSam Leffler static void	ath_beacon_return(struct ath_softc *, struct ath_buf *);
1585591b213SSam Leffler static void	ath_beacon_free(struct ath_softc *);
159b032f27cSSam Leffler static void	ath_beacon_config(struct ath_softc *, struct ieee80211vap *);
160c42a7b7eSSam Leffler static void	ath_descdma_cleanup(struct ath_softc *sc,
161c42a7b7eSSam Leffler 			struct ath_descdma *, ath_bufhead *);
1625591b213SSam Leffler static int	ath_desc_alloc(struct ath_softc *);
1635591b213SSam Leffler static void	ath_desc_free(struct ath_softc *);
16438c208f8SSam Leffler static struct ieee80211_node *ath_node_alloc(struct ieee80211vap *,
16538c208f8SSam Leffler 			const uint8_t [IEEE80211_ADDR_LEN]);
166c42a7b7eSSam Leffler static void	ath_node_free(struct ieee80211_node *);
16768e8e04eSSam Leffler static void	ath_node_getsignal(const struct ieee80211_node *,
16868e8e04eSSam Leffler 			int8_t *, int8_t *);
1695591b213SSam Leffler static int	ath_rxbuf_init(struct ath_softc *, struct ath_buf *);
170b032f27cSSam Leffler static void	ath_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
1715463c4a4SSam Leffler 			int subtype, int rssi, int nf);
172c42a7b7eSSam Leffler static void	ath_setdefantenna(struct ath_softc *, u_int);
173*96ff485dSAdrian Chadd static void	ath_rx_proc(struct ath_softc *sc, int);
174*96ff485dSAdrian Chadd static void	ath_rx_tasklet(void *, int);
175622b3fd2SSam Leffler static void	ath_txq_init(struct ath_softc *sc, struct ath_txq *, int);
176c42a7b7eSSam Leffler static struct ath_txq *ath_txq_setup(struct ath_softc*, int qtype, int subtype);
177c42a7b7eSSam Leffler static int	ath_tx_setup(struct ath_softc *, int, int);
178c42a7b7eSSam Leffler static int	ath_wme_update(struct ieee80211com *);
179c42a7b7eSSam Leffler static void	ath_tx_cleanupq(struct ath_softc *, struct ath_txq *);
180c42a7b7eSSam Leffler static void	ath_tx_cleanup(struct ath_softc *);
181c42a7b7eSSam Leffler static void	ath_tx_proc_q0(void *, int);
182c42a7b7eSSam Leffler static void	ath_tx_proc_q0123(void *, int);
1835591b213SSam Leffler static void	ath_tx_proc(void *, int);
184b032f27cSSam Leffler static void	ath_tx_draintxq(struct ath_softc *, struct ath_txq *);
1855591b213SSam Leffler static int	ath_chan_set(struct ath_softc *, struct ieee80211_channel *);
1865591b213SSam Leffler static void	ath_draintxq(struct ath_softc *);
1875591b213SSam Leffler static void	ath_stoprecv(struct ath_softc *);
1885591b213SSam Leffler static int	ath_startrecv(struct ath_softc *);
189c42a7b7eSSam Leffler static void	ath_chan_change(struct ath_softc *, struct ieee80211_channel *);
19068e8e04eSSam Leffler static void	ath_scan_start(struct ieee80211com *);
19168e8e04eSSam Leffler static void	ath_scan_end(struct ieee80211com *);
19268e8e04eSSam Leffler static void	ath_set_channel(struct ieee80211com *);
1935591b213SSam Leffler static void	ath_calibrate(void *);
194b032f27cSSam Leffler static int	ath_newstate(struct ieee80211vap *, enum ieee80211_state, int);
195e8fd88a3SSam Leffler static void	ath_setup_stationkey(struct ieee80211_node *);
196e9962332SSam Leffler static void	ath_newassoc(struct ieee80211_node *, int);
197b032f27cSSam Leffler static int	ath_setregdomain(struct ieee80211com *,
198b032f27cSSam Leffler 		    struct ieee80211_regdomain *, int,
199b032f27cSSam Leffler 		    struct ieee80211_channel []);
2005fe9f044SSam Leffler static void	ath_getradiocaps(struct ieee80211com *, int, int *,
201b032f27cSSam Leffler 		    struct ieee80211_channel []);
202b032f27cSSam Leffler static int	ath_getchannels(struct ath_softc *);
2033e50ec2cSSam Leffler static void	ath_led_event(struct ath_softc *, int);
2045591b213SSam Leffler 
205c42a7b7eSSam Leffler static int	ath_rate_setup(struct ath_softc *, u_int mode);
2065591b213SSam Leffler static void	ath_setcurmode(struct ath_softc *, enum ieee80211_phymode);
207c42a7b7eSSam Leffler 
208c42a7b7eSSam Leffler static void	ath_announce(struct ath_softc *);
2095591b213SSam Leffler 
21048237774SAdrian Chadd static void	ath_dfs_tasklet(void *, int);
21148237774SAdrian Chadd 
212584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
21310ad9a77SSam Leffler static void	ath_tdma_settimers(struct ath_softc *sc, u_int32_t nexttbtt,
21410ad9a77SSam Leffler 		    u_int32_t bintval);
21510ad9a77SSam Leffler static void	ath_tdma_bintvalsetup(struct ath_softc *sc,
21610ad9a77SSam Leffler 		    const struct ieee80211_tdma_state *tdma);
21710ad9a77SSam Leffler static void	ath_tdma_config(struct ath_softc *sc, struct ieee80211vap *vap);
21810ad9a77SSam Leffler static void	ath_tdma_update(struct ieee80211_node *ni,
2192bc3ce77SSam Leffler 		    const struct ieee80211_tdma_param *tdma, int);
22010ad9a77SSam Leffler static void	ath_tdma_beacon_send(struct ath_softc *sc,
22110ad9a77SSam Leffler 		    struct ieee80211vap *vap);
22210ad9a77SSam Leffler 
22310ad9a77SSam Leffler #define	TDMA_EP_MULTIPLIER	(1<<10) /* pow2 to optimize out * and / */
22410ad9a77SSam Leffler #define	TDMA_LPF_LEN		6
22510ad9a77SSam Leffler #define	TDMA_DUMMY_MARKER	0x127
22610ad9a77SSam Leffler #define	TDMA_EP_MUL(x, mul)	((x) * (mul))
22710ad9a77SSam Leffler #define	TDMA_IN(x)		(TDMA_EP_MUL((x), TDMA_EP_MULTIPLIER))
22810ad9a77SSam Leffler #define	TDMA_LPF(x, y, len) \
22910ad9a77SSam Leffler     ((x != TDMA_DUMMY_MARKER) ? (((x) * ((len)-1) + (y)) / (len)) : (y))
23010ad9a77SSam Leffler #define	TDMA_SAMPLE(x, y) do {					\
23110ad9a77SSam Leffler 	x = TDMA_LPF((x), TDMA_IN(y), TDMA_LPF_LEN);		\
23210ad9a77SSam Leffler } while (0)
23310ad9a77SSam Leffler #define	TDMA_EP_RND(x,mul) \
23410ad9a77SSam Leffler 	((((x)%(mul)) >= ((mul)/2)) ? ((x) + ((mul) - 1)) / (mul) : (x)/(mul))
23510ad9a77SSam Leffler #define	TDMA_AVG(x)		TDMA_EP_RND(x, TDMA_EP_MULTIPLIER)
236584f7327SSam Leffler #endif /* IEEE80211_SUPPORT_TDMA */
23710ad9a77SSam Leffler 
2385591b213SSam Leffler SYSCTL_DECL(_hw_ath);
2395591b213SSam Leffler 
2405591b213SSam Leffler /* XXX validate sysctl values */
2412dc7fcc4SSam Leffler static	int ath_longcalinterval = 30;		/* long cals every 30 secs */
2422dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, longcal, CTLFLAG_RW, &ath_longcalinterval,
2432dc7fcc4SSam Leffler 	    0, "long chip calibration interval (secs)");
2442dc7fcc4SSam Leffler static	int ath_shortcalinterval = 100;		/* short cals every 100 ms */
2452dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, shortcal, CTLFLAG_RW, &ath_shortcalinterval,
2462dc7fcc4SSam Leffler 	    0, "short chip calibration interval (msecs)");
2472dc7fcc4SSam Leffler static	int ath_resetcalinterval = 20*60;	/* reset cal state 20 mins */
2482dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, resetcal, CTLFLAG_RW, &ath_resetcalinterval,
2492dc7fcc4SSam Leffler 	    0, "reset chip calibration results (secs)");
250a108ab63SAdrian Chadd static	int ath_anicalinterval = 100;		/* ANI calibration - 100 msec */
251a108ab63SAdrian Chadd SYSCTL_INT(_hw_ath, OID_AUTO, anical, CTLFLAG_RW, &ath_anicalinterval,
252a108ab63SAdrian Chadd 	    0, "ANI calibration (msecs)");
2535591b213SSam Leffler 
254e2d787faSSam Leffler static	int ath_rxbuf = ATH_RXBUF;		/* # rx buffers to allocate */
255aaa70f2fSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, rxbuf, CTLFLAG_RW, &ath_rxbuf,
256e2d787faSSam Leffler 	    0, "rx buffers allocated");
257e2d787faSSam Leffler TUNABLE_INT("hw.ath.rxbuf", &ath_rxbuf);
258e2d787faSSam Leffler static	int ath_txbuf = ATH_TXBUF;		/* # tx buffers to allocate */
259aaa70f2fSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, txbuf, CTLFLAG_RW, &ath_txbuf,
260e2d787faSSam Leffler 	    0, "tx buffers allocated");
261e2d787faSSam Leffler TUNABLE_INT("hw.ath.txbuf", &ath_txbuf);
262e2d787faSSam Leffler 
263a32ac9d3SSam Leffler static	int ath_bstuck_threshold = 4;		/* max missed beacons */
264a32ac9d3SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, bstuck, CTLFLAG_RW, &ath_bstuck_threshold,
265a32ac9d3SSam Leffler 	    0, "max missed beacon xmits before chip reset");
266a32ac9d3SSam Leffler 
2676b349e5aSAdrian Chadd MALLOC_DEFINE(M_ATHDEV, "athdev", "ath driver dma buffers");
268c42a7b7eSSam Leffler 
26967397d39SAdrian Chadd #define	HAL_MODE_HT20 (HAL_MODE_11NG_HT20 | HAL_MODE_11NA_HT20)
27067397d39SAdrian Chadd #define	HAL_MODE_HT40 \
27167397d39SAdrian Chadd 	(HAL_MODE_11NG_HT40PLUS | HAL_MODE_11NG_HT40MINUS | \
27267397d39SAdrian Chadd 	HAL_MODE_11NA_HT40PLUS | HAL_MODE_11NA_HT40MINUS)
2735591b213SSam Leffler int
2745591b213SSam Leffler ath_attach(u_int16_t devid, struct ath_softc *sc)
2755591b213SSam Leffler {
276fc74a9f9SBrooks Davis 	struct ifnet *ifp;
277b032f27cSSam Leffler 	struct ieee80211com *ic;
278fc74a9f9SBrooks Davis 	struct ath_hal *ah = NULL;
2795591b213SSam Leffler 	HAL_STATUS status;
280c42a7b7eSSam Leffler 	int error = 0, i;
281411373ebSSam Leffler 	u_int wmodes;
28229aca940SSam Leffler 	uint8_t macaddr[IEEE80211_ADDR_LEN];
2835591b213SSam Leffler 
284c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: devid 0x%x\n", __func__, devid);
2855591b213SSam Leffler 
286b032f27cSSam Leffler 	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
287fc74a9f9SBrooks Davis 	if (ifp == NULL) {
288fc74a9f9SBrooks Davis 		device_printf(sc->sc_dev, "can not if_alloc()\n");
289fc74a9f9SBrooks Davis 		error = ENOSPC;
290fc74a9f9SBrooks Davis 		goto bad;
291fc74a9f9SBrooks Davis 	}
292b032f27cSSam Leffler 	ic = ifp->if_l2com;
293fc74a9f9SBrooks Davis 
2945591b213SSam Leffler 	/* set these up early for if_printf use */
2959bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(sc->sc_dev),
2969bf40edeSBrooks Davis 		device_get_unit(sc->sc_dev));
2975591b213SSam Leffler 
29888117a53SAdrian Chadd 	ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh, sc->sc_eepromdata, &status);
2995591b213SSam Leffler 	if (ah == NULL) {
3005591b213SSam Leffler 		if_printf(ifp, "unable to attach hardware; HAL status %u\n",
3015591b213SSam Leffler 			status);
3025591b213SSam Leffler 		error = ENXIO;
3035591b213SSam Leffler 		goto bad;
3045591b213SSam Leffler 	}
3055591b213SSam Leffler 	sc->sc_ah = ah;
306b58b3803SSam Leffler 	sc->sc_invalid = 0;	/* ready to go, enable interrupt handling */
3073297be13SSam Leffler #ifdef	ATH_DEBUG
3083297be13SSam Leffler 	sc->sc_debug = ath_debug;
3093297be13SSam Leffler #endif
3105591b213SSam Leffler 
3115591b213SSam Leffler 	/*
312c42a7b7eSSam Leffler 	 * Check if the MAC has multi-rate retry support.
313c42a7b7eSSam Leffler 	 * We do this by trying to setup a fake extended
314c42a7b7eSSam Leffler 	 * descriptor.  MAC's that don't have support will
315c42a7b7eSSam Leffler 	 * return false w/o doing anything.  MAC's that do
316c42a7b7eSSam Leffler 	 * support it will return true w/o doing anything.
317c42a7b7eSSam Leffler 	 */
318c42a7b7eSSam Leffler 	sc->sc_mrretry = ath_hal_setupxtxdesc(ah, NULL, 0,0, 0,0, 0,0);
319c42a7b7eSSam Leffler 
320c42a7b7eSSam Leffler 	/*
321c42a7b7eSSam Leffler 	 * Check if the device has hardware counters for PHY
322c42a7b7eSSam Leffler 	 * errors.  If so we need to enable the MIB interrupt
323c42a7b7eSSam Leffler 	 * so we can act on stat triggers.
324c42a7b7eSSam Leffler 	 */
325c42a7b7eSSam Leffler 	if (ath_hal_hwphycounters(ah))
326c42a7b7eSSam Leffler 		sc->sc_needmib = 1;
327c42a7b7eSSam Leffler 
328c42a7b7eSSam Leffler 	/*
329c42a7b7eSSam Leffler 	 * Get the hardware key cache size.
330c42a7b7eSSam Leffler 	 */
331c42a7b7eSSam Leffler 	sc->sc_keymax = ath_hal_keycachesize(ah);
332e8fd88a3SSam Leffler 	if (sc->sc_keymax > ATH_KEYMAX) {
333e8fd88a3SSam Leffler 		if_printf(ifp, "Warning, using only %u of %u key cache slots\n",
334e8fd88a3SSam Leffler 			ATH_KEYMAX, sc->sc_keymax);
335e8fd88a3SSam Leffler 		sc->sc_keymax = ATH_KEYMAX;
336c42a7b7eSSam Leffler 	}
337c42a7b7eSSam Leffler 	/*
338c42a7b7eSSam Leffler 	 * Reset the key cache since some parts do not
339c42a7b7eSSam Leffler 	 * reset the contents on initial power up.
340c42a7b7eSSam Leffler 	 */
341c42a7b7eSSam Leffler 	for (i = 0; i < sc->sc_keymax; i++)
342c42a7b7eSSam Leffler 		ath_hal_keyreset(ah, i);
343c42a7b7eSSam Leffler 
344c42a7b7eSSam Leffler 	/*
345b032f27cSSam Leffler 	 * Collect the default channel list.
3465591b213SSam Leffler 	 */
347b032f27cSSam Leffler 	error = ath_getchannels(sc);
3485591b213SSam Leffler 	if (error != 0)
3495591b213SSam Leffler 		goto bad;
3505591b213SSam Leffler 
3515591b213SSam Leffler 	/*
3525591b213SSam Leffler 	 * Setup rate tables for all potential media types.
3535591b213SSam Leffler 	 */
3545591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11A);
3555591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11B);
3565591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11G);
357c42a7b7eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_A);
358c42a7b7eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_G);
35968e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_STURBO_A);
36068e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11NA);
36168e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11NG);
362724c193aSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_HALF);
363724c193aSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_QUARTER);
364aaa70f2fSSam Leffler 
365c42a7b7eSSam Leffler 	/* NB: setup here so ath_rate_update is happy */
366c42a7b7eSSam Leffler 	ath_setcurmode(sc, IEEE80211_MODE_11A);
3675591b213SSam Leffler 
368c42a7b7eSSam Leffler 	/*
369c42a7b7eSSam Leffler 	 * Allocate tx+rx descriptors and populate the lists.
370c42a7b7eSSam Leffler 	 */
3715591b213SSam Leffler 	error = ath_desc_alloc(sc);
3725591b213SSam Leffler 	if (error != 0) {
3735591b213SSam Leffler 		if_printf(ifp, "failed to allocate descriptors: %d\n", error);
3745591b213SSam Leffler 		goto bad;
3755591b213SSam Leffler 	}
3762e986da5SSam Leffler 	callout_init_mtx(&sc->sc_cal_ch, &sc->sc_mtx, 0);
3772e986da5SSam Leffler 	callout_init_mtx(&sc->sc_wd_ch, &sc->sc_mtx, 0);
3785591b213SSam Leffler 
379f0b2a0beSSam Leffler 	ATH_TXBUF_LOCK_INIT(sc);
3805591b213SSam Leffler 
3810bbf5441SSam Leffler 	sc->sc_tq = taskqueue_create("ath_taskq", M_NOWAIT,
3820bbf5441SSam Leffler 		taskqueue_thread_enqueue, &sc->sc_tq);
3830bbf5441SSam Leffler 	taskqueue_start_threads(&sc->sc_tq, 1, PI_NET,
3840bbf5441SSam Leffler 		"%s taskq", ifp->if_xname);
3850bbf5441SSam Leffler 
386*96ff485dSAdrian Chadd 	TASK_INIT(&sc->sc_rxtask, 0, ath_rx_tasklet, sc);
3875591b213SSam Leffler 	TASK_INIT(&sc->sc_bmisstask, 0, ath_bmiss_proc, sc);
388c42a7b7eSSam Leffler 	TASK_INIT(&sc->sc_bstucktask,0, ath_bstuck_proc, sc);
3895591b213SSam Leffler 
3905591b213SSam Leffler 	/*
391c42a7b7eSSam Leffler 	 * Allocate hardware transmit queues: one queue for
392c42a7b7eSSam Leffler 	 * beacon frames and one data queue for each QoS
3934fa8d4efSDaniel Eischen 	 * priority.  Note that the hal handles resetting
394c42a7b7eSSam Leffler 	 * these queues at the needed time.
395c42a7b7eSSam Leffler 	 *
396c42a7b7eSSam Leffler 	 * XXX PS-Poll
3975591b213SSam Leffler 	 */
39880d2765fSSam Leffler 	sc->sc_bhalq = ath_beaconq_setup(ah);
3995591b213SSam Leffler 	if (sc->sc_bhalq == (u_int) -1) {
4005591b213SSam Leffler 		if_printf(ifp, "unable to setup a beacon xmit queue!\n");
401c42a7b7eSSam Leffler 		error = EIO;
402b28b4653SSam Leffler 		goto bad2;
4035591b213SSam Leffler 	}
404c42a7b7eSSam Leffler 	sc->sc_cabq = ath_txq_setup(sc, HAL_TX_QUEUE_CAB, 0);
405c42a7b7eSSam Leffler 	if (sc->sc_cabq == NULL) {
406c42a7b7eSSam Leffler 		if_printf(ifp, "unable to setup CAB xmit queue!\n");
407c42a7b7eSSam Leffler 		error = EIO;
408c42a7b7eSSam Leffler 		goto bad2;
409c42a7b7eSSam Leffler 	}
410c42a7b7eSSam Leffler 	/* NB: insure BK queue is the lowest priority h/w queue */
411c42a7b7eSSam Leffler 	if (!ath_tx_setup(sc, WME_AC_BK, HAL_WME_AC_BK)) {
412c42a7b7eSSam Leffler 		if_printf(ifp, "unable to setup xmit queue for %s traffic!\n",
413c42a7b7eSSam Leffler 			ieee80211_wme_acnames[WME_AC_BK]);
414c42a7b7eSSam Leffler 		error = EIO;
415c42a7b7eSSam Leffler 		goto bad2;
416c42a7b7eSSam Leffler 	}
417c42a7b7eSSam Leffler 	if (!ath_tx_setup(sc, WME_AC_BE, HAL_WME_AC_BE) ||
418c42a7b7eSSam Leffler 	    !ath_tx_setup(sc, WME_AC_VI, HAL_WME_AC_VI) ||
419c42a7b7eSSam Leffler 	    !ath_tx_setup(sc, WME_AC_VO, HAL_WME_AC_VO)) {
420c42a7b7eSSam Leffler 		/*
421c42a7b7eSSam Leffler 		 * Not enough hardware tx queues to properly do WME;
422c42a7b7eSSam Leffler 		 * just punt and assign them all to the same h/w queue.
423c42a7b7eSSam Leffler 		 * We could do a better job of this if, for example,
424c42a7b7eSSam Leffler 		 * we allocate queues when we switch from station to
425c42a7b7eSSam Leffler 		 * AP mode.
426c42a7b7eSSam Leffler 		 */
427c42a7b7eSSam Leffler 		if (sc->sc_ac2q[WME_AC_VI] != NULL)
428c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_VI]);
429c42a7b7eSSam Leffler 		if (sc->sc_ac2q[WME_AC_BE] != NULL)
430c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_BE]);
431c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_BE] = sc->sc_ac2q[WME_AC_BK];
432c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_VI] = sc->sc_ac2q[WME_AC_BK];
433c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_VO] = sc->sc_ac2q[WME_AC_BK];
434c42a7b7eSSam Leffler 	}
435c42a7b7eSSam Leffler 
436c42a7b7eSSam Leffler 	/*
437c42a7b7eSSam Leffler 	 * Special case certain configurations.  Note the
438c42a7b7eSSam Leffler 	 * CAB queue is handled by these specially so don't
439c42a7b7eSSam Leffler 	 * include them when checking the txq setup mask.
440c42a7b7eSSam Leffler 	 */
441c42a7b7eSSam Leffler 	switch (sc->sc_txqsetup &~ (1<<sc->sc_cabq->axq_qnum)) {
442c42a7b7eSSam Leffler 	case 0x01:
443c42a7b7eSSam Leffler 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0, sc);
444c42a7b7eSSam Leffler 		break;
445c42a7b7eSSam Leffler 	case 0x0f:
446c42a7b7eSSam Leffler 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0123, sc);
447c42a7b7eSSam Leffler 		break;
448c42a7b7eSSam Leffler 	default:
449c42a7b7eSSam Leffler 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc, sc);
450c42a7b7eSSam Leffler 		break;
451c42a7b7eSSam Leffler 	}
452c42a7b7eSSam Leffler 
453c42a7b7eSSam Leffler 	/*
454c42a7b7eSSam Leffler 	 * Setup rate control.  Some rate control modules
455c42a7b7eSSam Leffler 	 * call back to change the anntena state so expose
456c42a7b7eSSam Leffler 	 * the necessary entry points.
457c42a7b7eSSam Leffler 	 * XXX maybe belongs in struct ath_ratectrl?
458c42a7b7eSSam Leffler 	 */
459c42a7b7eSSam Leffler 	sc->sc_setdefantenna = ath_setdefantenna;
460c42a7b7eSSam Leffler 	sc->sc_rc = ath_rate_attach(sc);
461c42a7b7eSSam Leffler 	if (sc->sc_rc == NULL) {
462c42a7b7eSSam Leffler 		error = EIO;
463c42a7b7eSSam Leffler 		goto bad2;
464c42a7b7eSSam Leffler 	}
465c42a7b7eSSam Leffler 
46648237774SAdrian Chadd 	/* Attach DFS module */
46748237774SAdrian Chadd 	if (! ath_dfs_attach(sc)) {
46848237774SAdrian Chadd 		device_printf(sc->sc_dev, "%s: unable to attach DFS\n", __func__);
46948237774SAdrian Chadd 		error = EIO;
47048237774SAdrian Chadd 		goto bad2;
47148237774SAdrian Chadd 	}
47248237774SAdrian Chadd 
47348237774SAdrian Chadd 	/* Start DFS processing tasklet */
47448237774SAdrian Chadd 	TASK_INIT(&sc->sc_dfstask, 0, ath_dfs_tasklet, sc);
47548237774SAdrian Chadd 
4763e50ec2cSSam Leffler 	sc->sc_blinking = 0;
477c42a7b7eSSam Leffler 	sc->sc_ledstate = 1;
4783e50ec2cSSam Leffler 	sc->sc_ledon = 0;			/* low true */
4793e50ec2cSSam Leffler 	sc->sc_ledidle = (2700*hz)/1000;	/* 2.7sec */
4803e50ec2cSSam Leffler 	callout_init(&sc->sc_ledtimer, CALLOUT_MPSAFE);
481c42a7b7eSSam Leffler 	/*
482c42a7b7eSSam Leffler 	 * Auto-enable soft led processing for IBM cards and for
483c42a7b7eSSam Leffler 	 * 5211 minipci cards.  Users can also manually enable/disable
484c42a7b7eSSam Leffler 	 * support with a sysctl.
485c42a7b7eSSam Leffler 	 */
486c42a7b7eSSam Leffler 	sc->sc_softled = (devid == AR5212_DEVID_IBM || devid == AR5211_DEVID);
487c42a7b7eSSam Leffler 	if (sc->sc_softled) {
488869ff02eSSam Leffler 		ath_hal_gpioCfgOutput(ah, sc->sc_ledpin,
489869ff02eSSam Leffler 		    HAL_GPIO_MUX_MAC_NETWORK_LED);
4903e50ec2cSSam Leffler 		ath_hal_gpioset(ah, sc->sc_ledpin, !sc->sc_ledon);
491c42a7b7eSSam Leffler 	}
4925591b213SSam Leffler 
4935591b213SSam Leffler 	ifp->if_softc = sc;
4945591b213SSam Leffler 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
4955591b213SSam Leffler 	ifp->if_start = ath_start;
4965591b213SSam Leffler 	ifp->if_ioctl = ath_ioctl;
4975591b213SSam Leffler 	ifp->if_init = ath_init;
498e50d35e6SMaxim Sobolev 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
499e50d35e6SMaxim Sobolev 	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
500154b8df2SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
5015591b213SSam Leffler 
502c42a7b7eSSam Leffler 	ic->ic_ifp = ifp;
5035591b213SSam Leffler 	/* XXX not right but it's not used anywhere important */
5045591b213SSam Leffler 	ic->ic_phytype = IEEE80211_T_OFDM;
5055591b213SSam Leffler 	ic->ic_opmode = IEEE80211_M_STA;
506c42a7b7eSSam Leffler 	ic->ic_caps =
507c43feedeSSam Leffler 		  IEEE80211_C_STA		/* station mode */
508c43feedeSSam Leffler 		| IEEE80211_C_IBSS		/* ibss, nee adhoc, mode */
509fe32c3efSSam Leffler 		| IEEE80211_C_HOSTAP		/* hostap mode */
510fe32c3efSSam Leffler 		| IEEE80211_C_MONITOR		/* monitor mode */
5117a04dc27SSam Leffler 		| IEEE80211_C_AHDEMO		/* adhoc demo mode */
512b032f27cSSam Leffler 		| IEEE80211_C_WDS		/* 4-address traffic works */
51359aa14a9SRui Paulo 		| IEEE80211_C_MBSS		/* mesh point link mode */
514fe32c3efSSam Leffler 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
515c42a7b7eSSam Leffler 		| IEEE80211_C_SHSLOT		/* short slot time supported */
516c42a7b7eSSam Leffler 		| IEEE80211_C_WPA		/* capable of WPA1+WPA2 */
51768e8e04eSSam Leffler 		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
51868e8e04eSSam Leffler 		| IEEE80211_C_TXFRAG		/* handle tx frags */
51910dc8de4SAdrian Chadd #ifdef	ATH_ENABLE_DFS
52010dc8de4SAdrian Chadd 		| IEEE80211_C_DFS		/* Enable DFS radar detection */
52110dc8de4SAdrian Chadd #endif
52201e7e035SSam Leffler 		;
523c42a7b7eSSam Leffler 	/*
524c42a7b7eSSam Leffler 	 * Query the hal to figure out h/w crypto support.
525c42a7b7eSSam Leffler 	 */
526c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_WEP))
527b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP;
528c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_OCB))
529b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_OCB;
530c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_CCM))
531b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_CCM;
532c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_CKIP))
533b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_CKIP;
534c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_TKIP)) {
535b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIP;
536c42a7b7eSSam Leffler 		/*
537c42a7b7eSSam Leffler 		 * Check if h/w does the MIC and/or whether the
538c42a7b7eSSam Leffler 		 * separate key cache entries are required to
539c42a7b7eSSam Leffler 		 * handle both tx+rx MIC keys.
540c42a7b7eSSam Leffler 		 */
541c42a7b7eSSam Leffler 		if (ath_hal_ciphersupported(ah, HAL_CIPHER_MIC))
542b032f27cSSam Leffler 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
5435901d2d3SSam Leffler 		/*
5445901d2d3SSam Leffler 		 * If the h/w supports storing tx+rx MIC keys
5455901d2d3SSam Leffler 		 * in one cache slot automatically enable use.
5465901d2d3SSam Leffler 		 */
5475901d2d3SSam Leffler 		if (ath_hal_hastkipsplit(ah) ||
5485901d2d3SSam Leffler 		    !ath_hal_settkipsplit(ah, AH_FALSE))
549c42a7b7eSSam Leffler 			sc->sc_splitmic = 1;
550b032f27cSSam Leffler 		/*
551b032f27cSSam Leffler 		 * If the h/w can do TKIP MIC together with WME then
552b032f27cSSam Leffler 		 * we use it; otherwise we force the MIC to be done
553b032f27cSSam Leffler 		 * in software by the net80211 layer.
554b032f27cSSam Leffler 		 */
555b032f27cSSam Leffler 		if (ath_hal_haswmetkipmic(ah))
556b032f27cSSam Leffler 			sc->sc_wmetkipmic = 1;
557c42a7b7eSSam Leffler 	}
558e8fd88a3SSam Leffler 	sc->sc_hasclrkey = ath_hal_ciphersupported(ah, HAL_CIPHER_CLR);
5599ac01d39SRui Paulo 	/*
5601ac5dac2SRui Paulo 	 * Check for multicast key search support.
5619ac01d39SRui Paulo 	 */
5629ac01d39SRui Paulo 	if (ath_hal_hasmcastkeysearch(sc->sc_ah) &&
5639ac01d39SRui Paulo 	    !ath_hal_getmcastkeysearch(sc->sc_ah)) {
5649ac01d39SRui Paulo 		ath_hal_setmcastkeysearch(sc->sc_ah, 1);
5659ac01d39SRui Paulo 	}
566e8fd88a3SSam Leffler 	sc->sc_mcastkey = ath_hal_getmcastkeysearch(ah);
567c42a7b7eSSam Leffler 	/*
5685901d2d3SSam Leffler 	 * Mark key cache slots associated with global keys
5695901d2d3SSam Leffler 	 * as in use.  If we knew TKIP was not to be used we
5705901d2d3SSam Leffler 	 * could leave the +32, +64, and +32+64 slots free.
5715901d2d3SSam Leffler 	 */
5725901d2d3SSam Leffler 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
5735901d2d3SSam Leffler 		setbit(sc->sc_keymap, i);
5745901d2d3SSam Leffler 		setbit(sc->sc_keymap, i+64);
5755901d2d3SSam Leffler 		if (sc->sc_splitmic) {
5765901d2d3SSam Leffler 			setbit(sc->sc_keymap, i+32);
5775901d2d3SSam Leffler 			setbit(sc->sc_keymap, i+32+64);
5785901d2d3SSam Leffler 		}
5795901d2d3SSam Leffler 	}
5805901d2d3SSam Leffler 	/*
581c42a7b7eSSam Leffler 	 * TPC support can be done either with a global cap or
582c42a7b7eSSam Leffler 	 * per-packet support.  The latter is not available on
583c42a7b7eSSam Leffler 	 * all parts.  We're a bit pedantic here as all parts
584c42a7b7eSSam Leffler 	 * support a global cap.
585c42a7b7eSSam Leffler 	 */
586c59005e9SSam Leffler 	if (ath_hal_hastpc(ah) || ath_hal_hastxpowlimit(ah))
587c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TXPMGT;
588c42a7b7eSSam Leffler 
589c42a7b7eSSam Leffler 	/*
590c42a7b7eSSam Leffler 	 * Mark WME capability only if we have sufficient
591c42a7b7eSSam Leffler 	 * hardware queues to do proper priority scheduling.
592c42a7b7eSSam Leffler 	 */
593c42a7b7eSSam Leffler 	if (sc->sc_ac2q[WME_AC_BE] != sc->sc_ac2q[WME_AC_BK])
594c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_WME;
595c42a7b7eSSam Leffler 	/*
596e8fd88a3SSam Leffler 	 * Check for misc other capabilities.
597c42a7b7eSSam Leffler 	 */
598c42a7b7eSSam Leffler 	if (ath_hal_hasbursting(ah))
599c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_BURST;
600b032f27cSSam Leffler 	sc->sc_hasbmask = ath_hal_hasbssidmask(ah);
60159aa14a9SRui Paulo 	sc->sc_hasbmatch = ath_hal_hasbssidmatch(ah);
602b032f27cSSam Leffler 	sc->sc_hastsfadd = ath_hal_hastsfadjust(ah);
6038a2a6beeSAdrian Chadd 	sc->sc_rxslink = ath_hal_self_linked_final_rxdesc(ah);
604fc4de9b7SAdrian Chadd 	sc->sc_rxtsf32 = ath_hal_has_long_rxdesc_tsf(ah);
60568e8e04eSSam Leffler 	if (ath_hal_hasfastframes(ah))
60668e8e04eSSam Leffler 		ic->ic_caps |= IEEE80211_C_FF;
60759efa8b5SSam Leffler 	wmodes = ath_hal_getwirelessmodes(ah);
608411373ebSSam Leffler 	if (wmodes & (HAL_MODE_108G|HAL_MODE_TURBO))
60968e8e04eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TURBOP;
610584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
61110ad9a77SSam Leffler 	if (ath_hal_macversion(ah) > 0x78) {
61210ad9a77SSam Leffler 		ic->ic_caps |= IEEE80211_C_TDMA; /* capable of TDMA */
61310ad9a77SSam Leffler 		ic->ic_tdma_update = ath_tdma_update;
61410ad9a77SSam Leffler 	}
61510ad9a77SSam Leffler #endif
61667397d39SAdrian Chadd 
61767397d39SAdrian Chadd 	/*
61867397d39SAdrian Chadd 	 * The if_ath 11n support is completely not ready for normal use.
61967397d39SAdrian Chadd 	 * Enabling this option will likely break everything and everything.
62067397d39SAdrian Chadd 	 * Don't think of doing that unless you know what you're doing.
62167397d39SAdrian Chadd 	 */
62267397d39SAdrian Chadd 
6238fd67f92SAdrian Chadd #ifdef	ATH_ENABLE_11N
62467397d39SAdrian Chadd 	/*
62567397d39SAdrian Chadd 	 * Query HT capabilities
62667397d39SAdrian Chadd 	 */
62767397d39SAdrian Chadd 	if (ath_hal_getcapability(ah, HAL_CAP_HT, 0, NULL) == HAL_OK &&
62867397d39SAdrian Chadd 	    (wmodes & (HAL_MODE_HT20 | HAL_MODE_HT40))) {
62967397d39SAdrian Chadd 		int rxs, txs;
63067397d39SAdrian Chadd 
63167397d39SAdrian Chadd 		device_printf(sc->sc_dev, "[HT] enabling HT modes\n");
63267397d39SAdrian Chadd 		ic->ic_htcaps = IEEE80211_HTC_HT		/* HT operation */
63367397d39SAdrian Chadd 			    | IEEE80211_HTC_AMPDU		/* A-MPDU tx/rx */
63467397d39SAdrian Chadd 			    | IEEE80211_HTC_AMSDU		/* A-MSDU tx/rx */
63567397d39SAdrian Chadd 			    | IEEE80211_HTCAP_MAXAMSDU_3839	/* max A-MSDU length */
63667397d39SAdrian Chadd 			    | IEEE80211_HTCAP_SMPS_OFF;		/* SM power save off */
63767397d39SAdrian Chadd 			;
63867397d39SAdrian Chadd 
63976355edbSAdrian Chadd 		/*
64076355edbSAdrian Chadd 		 * Enable short-GI for HT20 only if the hardware
64176355edbSAdrian Chadd 		 * advertises support.
64276355edbSAdrian Chadd 		 * Notably, anything earlier than the AR9287 doesn't.
64376355edbSAdrian Chadd 		 */
64476355edbSAdrian Chadd 		if ((ath_hal_getcapability(ah,
64576355edbSAdrian Chadd 		    HAL_CAP_HT20_SGI, 0, NULL) == HAL_OK) &&
64676355edbSAdrian Chadd 		    (wmodes & HAL_MODE_HT20)) {
64776355edbSAdrian Chadd 			device_printf(sc->sc_dev,
64876355edbSAdrian Chadd 			    "[HT] enabling short-GI in 20MHz mode\n");
64976355edbSAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_SHORTGI20;
65076355edbSAdrian Chadd 		}
65176355edbSAdrian Chadd 
65267397d39SAdrian Chadd 		if (wmodes & HAL_MODE_HT40)
65367397d39SAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40
65467397d39SAdrian Chadd 			    |  IEEE80211_HTCAP_SHORTGI40;
65567397d39SAdrian Chadd 
65667397d39SAdrian Chadd 		/*
65767397d39SAdrian Chadd 		 * rx/tx stream is not currently used anywhere; it needs to be taken
65867397d39SAdrian Chadd 		 * into account when negotiating which MCS rates it'll receive and
65967397d39SAdrian Chadd 		 * what MCS rates are available for TX.
66067397d39SAdrian Chadd 		 */
66167397d39SAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 0, &rxs);
66267397d39SAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 1, &txs);
66367397d39SAdrian Chadd 
66467397d39SAdrian Chadd 		ath_hal_getrxchainmask(ah, &sc->sc_rxchainmask);
66567397d39SAdrian Chadd 		ath_hal_gettxchainmask(ah, &sc->sc_txchainmask);
66667397d39SAdrian Chadd 
66767397d39SAdrian Chadd 		ic->ic_txstream = txs;
66867397d39SAdrian Chadd 		ic->ic_rxstream = rxs;
66967397d39SAdrian Chadd 
67067397d39SAdrian Chadd 		device_printf(sc->sc_dev, "[HT] %d RX streams; %d TX streams\n", rxs, txs);
67167397d39SAdrian Chadd 	}
67267397d39SAdrian Chadd #endif
67367397d39SAdrian Chadd 
674c42a7b7eSSam Leffler 	/*
675c42a7b7eSSam Leffler 	 * Indicate we need the 802.11 header padded to a
676c42a7b7eSSam Leffler 	 * 32-bit boundary for 4-address and QoS frames.
677c42a7b7eSSam Leffler 	 */
678c42a7b7eSSam Leffler 	ic->ic_flags |= IEEE80211_F_DATAPAD;
679c42a7b7eSSam Leffler 
680c42a7b7eSSam Leffler 	/*
681c42a7b7eSSam Leffler 	 * Query the hal about antenna support.
682c42a7b7eSSam Leffler 	 */
683c42a7b7eSSam Leffler 	sc->sc_defant = ath_hal_getdefantenna(ah);
684c42a7b7eSSam Leffler 
685c42a7b7eSSam Leffler 	/*
686c42a7b7eSSam Leffler 	 * Not all chips have the VEOL support we want to
687c42a7b7eSSam Leffler 	 * use with IBSS beacons; check here for it.
688c42a7b7eSSam Leffler 	 */
689c42a7b7eSSam Leffler 	sc->sc_hasveol = ath_hal_hasveol(ah);
6905591b213SSam Leffler 
6915591b213SSam Leffler 	/* get mac address from hardware */
69229aca940SSam Leffler 	ath_hal_getmac(ah, macaddr);
693b032f27cSSam Leffler 	if (sc->sc_hasbmask)
694b032f27cSSam Leffler 		ath_hal_getbssidmask(ah, sc->sc_hwbssidmask);
6955591b213SSam Leffler 
696b032f27cSSam Leffler 	/* NB: used to size node table key mapping array */
697b032f27cSSam Leffler 	ic->ic_max_keyix = sc->sc_keymax;
6985591b213SSam Leffler 	/* call MI attach routine. */
69929aca940SSam Leffler 	ieee80211_ifattach(ic, macaddr);
700b032f27cSSam Leffler 	ic->ic_setregdomain = ath_setregdomain;
701b032f27cSSam Leffler 	ic->ic_getradiocaps = ath_getradiocaps;
702b032f27cSSam Leffler 	sc->sc_opmode = HAL_M_STA;
703b032f27cSSam Leffler 
7045591b213SSam Leffler 	/* override default methods */
705b032f27cSSam Leffler 	ic->ic_newassoc = ath_newassoc;
706b032f27cSSam Leffler 	ic->ic_updateslot = ath_updateslot;
707b032f27cSSam Leffler 	ic->ic_wme.wme_update = ath_wme_update;
708b032f27cSSam Leffler 	ic->ic_vap_create = ath_vap_create;
709b032f27cSSam Leffler 	ic->ic_vap_delete = ath_vap_delete;
710b032f27cSSam Leffler 	ic->ic_raw_xmit = ath_raw_xmit;
711b032f27cSSam Leffler 	ic->ic_update_mcast = ath_update_mcast;
712b032f27cSSam Leffler 	ic->ic_update_promisc = ath_update_promisc;
7135591b213SSam Leffler 	ic->ic_node_alloc = ath_node_alloc;
7141e774079SSam Leffler 	sc->sc_node_free = ic->ic_node_free;
7155591b213SSam Leffler 	ic->ic_node_free = ath_node_free;
71668e8e04eSSam Leffler 	ic->ic_node_getsignal = ath_node_getsignal;
71768e8e04eSSam Leffler 	ic->ic_scan_start = ath_scan_start;
71868e8e04eSSam Leffler 	ic->ic_scan_end = ath_scan_end;
71968e8e04eSSam Leffler 	ic->ic_set_channel = ath_set_channel;
7205591b213SSam Leffler 
7215463c4a4SSam Leffler 	ieee80211_radiotap_attach(ic,
7225463c4a4SSam Leffler 	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
7235463c4a4SSam Leffler 		ATH_TX_RADIOTAP_PRESENT,
7245463c4a4SSam Leffler 	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
7255463c4a4SSam Leffler 		ATH_RX_RADIOTAP_PRESENT);
7265463c4a4SSam Leffler 
7274866e6c2SSam Leffler 	/*
7284866e6c2SSam Leffler 	 * Setup dynamic sysctl's now that country code and
7294866e6c2SSam Leffler 	 * regdomain are available from the hal.
7304866e6c2SSam Leffler 	 */
7314866e6c2SSam Leffler 	ath_sysctlattach(sc);
732e8dabfbeSAdrian Chadd 	ath_sysctl_stats_attach(sc);
73337931a35SAdrian Chadd 	ath_sysctl_hal_attach(sc);
73473454c73SSam Leffler 
735c42a7b7eSSam Leffler 	if (bootverbose)
736c42a7b7eSSam Leffler 		ieee80211_announce(ic);
737c42a7b7eSSam Leffler 	ath_announce(sc);
7385591b213SSam Leffler 	return 0;
739b28b4653SSam Leffler bad2:
740c42a7b7eSSam Leffler 	ath_tx_cleanup(sc);
741b28b4653SSam Leffler 	ath_desc_free(sc);
7425591b213SSam Leffler bad:
7435591b213SSam Leffler 	if (ah)
7445591b213SSam Leffler 		ath_hal_detach(ah);
745fc74a9f9SBrooks Davis 	if (ifp != NULL)
746fc74a9f9SBrooks Davis 		if_free(ifp);
7475591b213SSam Leffler 	sc->sc_invalid = 1;
7485591b213SSam Leffler 	return error;
7495591b213SSam Leffler }
7505591b213SSam Leffler 
7515591b213SSam Leffler int
7525591b213SSam Leffler ath_detach(struct ath_softc *sc)
7535591b213SSam Leffler {
754fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
7555591b213SSam Leffler 
756c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
757c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
7585591b213SSam Leffler 
759c42a7b7eSSam Leffler 	/*
760c42a7b7eSSam Leffler 	 * NB: the order of these is important:
76171b85077SSam Leffler 	 * o stop the chip so no more interrupts will fire
762c42a7b7eSSam Leffler 	 * o call the 802.11 layer before detaching the hal to
763c42a7b7eSSam Leffler 	 *   insure callbacks into the driver to delete global
764c42a7b7eSSam Leffler 	 *   key cache entries can be handled
76571b85077SSam Leffler 	 * o free the taskqueue which drains any pending tasks
766c42a7b7eSSam Leffler 	 * o reclaim the tx queue data structures after calling
767c42a7b7eSSam Leffler 	 *   the 802.11 layer as we'll get called back to reclaim
768c42a7b7eSSam Leffler 	 *   node state and potentially want to use them
769c42a7b7eSSam Leffler 	 * o to cleanup the tx queues the hal is called, so detach
770c42a7b7eSSam Leffler 	 *   it last
771c42a7b7eSSam Leffler 	 * Other than that, it's straightforward...
772c42a7b7eSSam Leffler 	 */
77371b85077SSam Leffler 	ath_stop(ifp);
774b032f27cSSam Leffler 	ieee80211_ifdetach(ifp->if_l2com);
77571b85077SSam Leffler 	taskqueue_free(sc->sc_tq);
77686e07743SSam Leffler #ifdef ATH_TX99_DIAG
77786e07743SSam Leffler 	if (sc->sc_tx99 != NULL)
77886e07743SSam Leffler 		sc->sc_tx99->detach(sc->sc_tx99);
77986e07743SSam Leffler #endif
780c42a7b7eSSam Leffler 	ath_rate_detach(sc->sc_rc);
78148237774SAdrian Chadd 
78248237774SAdrian Chadd 	ath_dfs_detach(sc);
7835591b213SSam Leffler 	ath_desc_free(sc);
784c42a7b7eSSam Leffler 	ath_tx_cleanup(sc);
78571b85077SSam Leffler 	ath_hal_detach(sc->sc_ah);	/* NB: sets chip in full sleep */
786c4c6f08fSRuslan Ermilov 	if_free(ifp);
787f0b2a0beSSam Leffler 
7885591b213SSam Leffler 	return 0;
7895591b213SSam Leffler }
7905591b213SSam Leffler 
791b032f27cSSam Leffler /*
792b032f27cSSam Leffler  * MAC address handling for multiple BSS on the same radio.
793b032f27cSSam Leffler  * The first vap uses the MAC address from the EEPROM.  For
794b032f27cSSam Leffler  * subsequent vap's we set the U/L bit (bit 1) in the MAC
795b032f27cSSam Leffler  * address and use the next six bits as an index.
796b032f27cSSam Leffler  */
797b032f27cSSam Leffler static void
798b032f27cSSam Leffler assign_address(struct ath_softc *sc, uint8_t mac[IEEE80211_ADDR_LEN], int clone)
799b032f27cSSam Leffler {
800b032f27cSSam Leffler 	int i;
801b032f27cSSam Leffler 
802b032f27cSSam Leffler 	if (clone && sc->sc_hasbmask) {
803b032f27cSSam Leffler 		/* NB: we only do this if h/w supports multiple bssid */
804b032f27cSSam Leffler 		for (i = 0; i < 8; i++)
805b032f27cSSam Leffler 			if ((sc->sc_bssidmask & (1<<i)) == 0)
806b032f27cSSam Leffler 				break;
807b032f27cSSam Leffler 		if (i != 0)
808b032f27cSSam Leffler 			mac[0] |= (i << 2)|0x2;
809b032f27cSSam Leffler 	} else
810b032f27cSSam Leffler 		i = 0;
811b032f27cSSam Leffler 	sc->sc_bssidmask |= 1<<i;
812b032f27cSSam Leffler 	sc->sc_hwbssidmask[0] &= ~mac[0];
813b032f27cSSam Leffler 	if (i == 0)
814b032f27cSSam Leffler 		sc->sc_nbssid0++;
815b032f27cSSam Leffler }
816b032f27cSSam Leffler 
817b032f27cSSam Leffler static void
818b032f27cSSam Leffler reclaim_address(struct ath_softc *sc, const uint8_t mac[IEEE80211_ADDR_LEN])
819b032f27cSSam Leffler {
820b032f27cSSam Leffler 	int i = mac[0] >> 2;
821b032f27cSSam Leffler 	uint8_t mask;
822b032f27cSSam Leffler 
823b032f27cSSam Leffler 	if (i != 0 || --sc->sc_nbssid0 == 0) {
824b032f27cSSam Leffler 		sc->sc_bssidmask &= ~(1<<i);
825b032f27cSSam Leffler 		/* recalculate bssid mask from remaining addresses */
826b032f27cSSam Leffler 		mask = 0xff;
827b032f27cSSam Leffler 		for (i = 1; i < 8; i++)
828b032f27cSSam Leffler 			if (sc->sc_bssidmask & (1<<i))
829b032f27cSSam Leffler 				mask &= ~((i<<2)|0x2);
830b032f27cSSam Leffler 		sc->sc_hwbssidmask[0] |= mask;
831b032f27cSSam Leffler 	}
832b032f27cSSam Leffler }
833b032f27cSSam Leffler 
834b032f27cSSam Leffler /*
835b032f27cSSam Leffler  * Assign a beacon xmit slot.  We try to space out
836b032f27cSSam Leffler  * assignments so when beacons are staggered the
837b032f27cSSam Leffler  * traffic coming out of the cab q has maximal time
838b032f27cSSam Leffler  * to go out before the next beacon is scheduled.
839b032f27cSSam Leffler  */
840b032f27cSSam Leffler static int
841b032f27cSSam Leffler assign_bslot(struct ath_softc *sc)
842b032f27cSSam Leffler {
843b032f27cSSam Leffler 	u_int slot, free;
844b032f27cSSam Leffler 
845b032f27cSSam Leffler 	free = 0;
846b032f27cSSam Leffler 	for (slot = 0; slot < ATH_BCBUF; slot++)
847b032f27cSSam Leffler 		if (sc->sc_bslot[slot] == NULL) {
848b032f27cSSam Leffler 			if (sc->sc_bslot[(slot+1)%ATH_BCBUF] == NULL &&
849b032f27cSSam Leffler 			    sc->sc_bslot[(slot-1)%ATH_BCBUF] == NULL)
850b032f27cSSam Leffler 				return slot;
851b032f27cSSam Leffler 			free = slot;
852b032f27cSSam Leffler 			/* NB: keep looking for a double slot */
853b032f27cSSam Leffler 		}
854b032f27cSSam Leffler 	return free;
855b032f27cSSam Leffler }
856b032f27cSSam Leffler 
857b032f27cSSam Leffler static struct ieee80211vap *
858b032f27cSSam Leffler ath_vap_create(struct ieee80211com *ic,
859b032f27cSSam Leffler 	const char name[IFNAMSIZ], int unit, int opmode, int flags,
860b032f27cSSam Leffler 	const uint8_t bssid[IEEE80211_ADDR_LEN],
861b032f27cSSam Leffler 	const uint8_t mac0[IEEE80211_ADDR_LEN])
862b032f27cSSam Leffler {
863b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
864b032f27cSSam Leffler 	struct ath_vap *avp;
865b032f27cSSam Leffler 	struct ieee80211vap *vap;
866b032f27cSSam Leffler 	uint8_t mac[IEEE80211_ADDR_LEN];
867b032f27cSSam Leffler 	int ic_opmode, needbeacon, error;
868b032f27cSSam Leffler 
869b032f27cSSam Leffler 	avp = (struct ath_vap *) malloc(sizeof(struct ath_vap),
870b032f27cSSam Leffler 	    M_80211_VAP, M_WAITOK | M_ZERO);
871b032f27cSSam Leffler 	needbeacon = 0;
872b032f27cSSam Leffler 	IEEE80211_ADDR_COPY(mac, mac0);
873b032f27cSSam Leffler 
874b032f27cSSam Leffler 	ATH_LOCK(sc);
875a8962181SSam Leffler 	ic_opmode = opmode;		/* default to opmode of new vap */
876b032f27cSSam Leffler 	switch (opmode) {
877b032f27cSSam Leffler 	case IEEE80211_M_STA:
878a8962181SSam Leffler 		if (sc->sc_nstavaps != 0) {	/* XXX only 1 for now */
879b032f27cSSam Leffler 			device_printf(sc->sc_dev, "only 1 sta vap supported\n");
880b032f27cSSam Leffler 			goto bad;
881b032f27cSSam Leffler 		}
882b032f27cSSam Leffler 		if (sc->sc_nvaps) {
883b032f27cSSam Leffler 			/*
884a8962181SSam Leffler 			 * With multiple vaps we must fall back
885a8962181SSam Leffler 			 * to s/w beacon miss handling.
886b032f27cSSam Leffler 			 */
887b032f27cSSam Leffler 			flags |= IEEE80211_CLONE_NOBEACONS;
888b032f27cSSam Leffler 		}
889a8962181SSam Leffler 		if (flags & IEEE80211_CLONE_NOBEACONS) {
890a8962181SSam Leffler 			/*
891a8962181SSam Leffler 			 * Station mode w/o beacons are implemented w/ AP mode.
892a8962181SSam Leffler 			 */
893b032f27cSSam Leffler 			ic_opmode = IEEE80211_M_HOSTAP;
894a8962181SSam Leffler 		}
895b032f27cSSam Leffler 		break;
896b032f27cSSam Leffler 	case IEEE80211_M_IBSS:
897b032f27cSSam Leffler 		if (sc->sc_nvaps != 0) {	/* XXX only 1 for now */
898b032f27cSSam Leffler 			device_printf(sc->sc_dev,
899b032f27cSSam Leffler 			    "only 1 ibss vap supported\n");
900b032f27cSSam Leffler 			goto bad;
901b032f27cSSam Leffler 		}
902b032f27cSSam Leffler 		needbeacon = 1;
903b032f27cSSam Leffler 		break;
904b032f27cSSam Leffler 	case IEEE80211_M_AHDEMO:
905584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
90610ad9a77SSam Leffler 		if (flags & IEEE80211_CLONE_TDMA) {
907a8962181SSam Leffler 			if (sc->sc_nvaps != 0) {
908a8962181SSam Leffler 				device_printf(sc->sc_dev,
909a8962181SSam Leffler 				    "only 1 tdma vap supported\n");
910a8962181SSam Leffler 				goto bad;
911a8962181SSam Leffler 			}
91210ad9a77SSam Leffler 			needbeacon = 1;
91310ad9a77SSam Leffler 			flags |= IEEE80211_CLONE_NOBEACONS;
91410ad9a77SSam Leffler 		}
915b032f27cSSam Leffler 		/* fall thru... */
91610ad9a77SSam Leffler #endif
917b032f27cSSam Leffler 	case IEEE80211_M_MONITOR:
918b032f27cSSam Leffler 		if (sc->sc_nvaps != 0 && ic->ic_opmode != opmode) {
919a8962181SSam Leffler 			/*
920a8962181SSam Leffler 			 * Adopt existing mode.  Adding a monitor or ahdemo
921a8962181SSam Leffler 			 * vap to an existing configuration is of dubious
922a8962181SSam Leffler 			 * value but should be ok.
923a8962181SSam Leffler 			 */
924b032f27cSSam Leffler 			/* XXX not right for monitor mode */
925b032f27cSSam Leffler 			ic_opmode = ic->ic_opmode;
926a8962181SSam Leffler 		}
927b032f27cSSam Leffler 		break;
928b032f27cSSam Leffler 	case IEEE80211_M_HOSTAP:
92959aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
930b032f27cSSam Leffler 		needbeacon = 1;
931a8962181SSam Leffler 		break;
932b032f27cSSam Leffler 	case IEEE80211_M_WDS:
933a8962181SSam Leffler 		if (sc->sc_nvaps != 0 && ic->ic_opmode == IEEE80211_M_STA) {
934b032f27cSSam Leffler 			device_printf(sc->sc_dev,
935b032f27cSSam Leffler 			    "wds not supported in sta mode\n");
936b032f27cSSam Leffler 			goto bad;
937b032f27cSSam Leffler 		}
938b032f27cSSam Leffler 		/*
939b032f27cSSam Leffler 		 * Silently remove any request for a unique
940b032f27cSSam Leffler 		 * bssid; WDS vap's always share the local
941b032f27cSSam Leffler 		 * mac address.
942b032f27cSSam Leffler 		 */
943b032f27cSSam Leffler 		flags &= ~IEEE80211_CLONE_BSSID;
944a8962181SSam Leffler 		if (sc->sc_nvaps == 0)
945b032f27cSSam Leffler 			ic_opmode = IEEE80211_M_HOSTAP;
946a8962181SSam Leffler 		else
947a8962181SSam Leffler 			ic_opmode = ic->ic_opmode;
9487d261891SRui Paulo 		break;
949b032f27cSSam Leffler 	default:
950b032f27cSSam Leffler 		device_printf(sc->sc_dev, "unknown opmode %d\n", opmode);
951b032f27cSSam Leffler 		goto bad;
952b032f27cSSam Leffler 	}
953b032f27cSSam Leffler 	/*
954b032f27cSSam Leffler 	 * Check that a beacon buffer is available; the code below assumes it.
955b032f27cSSam Leffler 	 */
9566b349e5aSAdrian Chadd 	if (needbeacon & TAILQ_EMPTY(&sc->sc_bbuf)) {
957b032f27cSSam Leffler 		device_printf(sc->sc_dev, "no beacon buffer available\n");
958b032f27cSSam Leffler 		goto bad;
959b032f27cSSam Leffler 	}
960b032f27cSSam Leffler 
961b032f27cSSam Leffler 	/* STA, AHDEMO? */
96259aa14a9SRui Paulo 	if (opmode == IEEE80211_M_HOSTAP || opmode == IEEE80211_M_MBSS) {
963b032f27cSSam Leffler 		assign_address(sc, mac, flags & IEEE80211_CLONE_BSSID);
964b032f27cSSam Leffler 		ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
965b032f27cSSam Leffler 	}
966b032f27cSSam Leffler 
967b032f27cSSam Leffler 	vap = &avp->av_vap;
968b032f27cSSam Leffler 	/* XXX can't hold mutex across if_alloc */
969b032f27cSSam Leffler 	ATH_UNLOCK(sc);
970b032f27cSSam Leffler 	error = ieee80211_vap_setup(ic, vap, name, unit, opmode, flags,
971b032f27cSSam Leffler 	    bssid, mac);
972b032f27cSSam Leffler 	ATH_LOCK(sc);
973b032f27cSSam Leffler 	if (error != 0) {
974b032f27cSSam Leffler 		device_printf(sc->sc_dev, "%s: error %d creating vap\n",
975b032f27cSSam Leffler 		    __func__, error);
976b032f27cSSam Leffler 		goto bad2;
977b032f27cSSam Leffler 	}
978b032f27cSSam Leffler 
979b032f27cSSam Leffler 	/* h/w crypto support */
980b032f27cSSam Leffler 	vap->iv_key_alloc = ath_key_alloc;
981b032f27cSSam Leffler 	vap->iv_key_delete = ath_key_delete;
982b032f27cSSam Leffler 	vap->iv_key_set = ath_key_set;
983b032f27cSSam Leffler 	vap->iv_key_update_begin = ath_key_update_begin;
984b032f27cSSam Leffler 	vap->iv_key_update_end = ath_key_update_end;
985b032f27cSSam Leffler 
986b032f27cSSam Leffler 	/* override various methods */
987b032f27cSSam Leffler 	avp->av_recv_mgmt = vap->iv_recv_mgmt;
988b032f27cSSam Leffler 	vap->iv_recv_mgmt = ath_recv_mgmt;
989b032f27cSSam Leffler 	vap->iv_reset = ath_reset_vap;
990b032f27cSSam Leffler 	vap->iv_update_beacon = ath_beacon_update;
991b032f27cSSam Leffler 	avp->av_newstate = vap->iv_newstate;
992b032f27cSSam Leffler 	vap->iv_newstate = ath_newstate;
993b032f27cSSam Leffler 	avp->av_bmiss = vap->iv_bmiss;
994b032f27cSSam Leffler 	vap->iv_bmiss = ath_bmiss_vap;
995b032f27cSSam Leffler 
9969be25f4aSAdrian Chadd 	/* Set default parameters */
9979be25f4aSAdrian Chadd 
9989be25f4aSAdrian Chadd 	/*
9999be25f4aSAdrian Chadd 	 * Anything earlier than some AR9300 series MACs don't
10009be25f4aSAdrian Chadd 	 * support a smaller MPDU density.
10019be25f4aSAdrian Chadd 	 */
10029be25f4aSAdrian Chadd 	vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_8;
10039be25f4aSAdrian Chadd 	/*
10049be25f4aSAdrian Chadd 	 * All NICs can handle the maximum size, however
10059be25f4aSAdrian Chadd 	 * AR5416 based MACs can only TX aggregates w/ RTS
10069be25f4aSAdrian Chadd 	 * protection when the total aggregate size is <= 8k.
10079be25f4aSAdrian Chadd 	 * However, for now that's enforced by the TX path.
10089be25f4aSAdrian Chadd 	 */
10099be25f4aSAdrian Chadd 	vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_64K;
10109be25f4aSAdrian Chadd 
1011b032f27cSSam Leffler 	avp->av_bslot = -1;
1012b032f27cSSam Leffler 	if (needbeacon) {
1013b032f27cSSam Leffler 		/*
1014b032f27cSSam Leffler 		 * Allocate beacon state and setup the q for buffered
1015b032f27cSSam Leffler 		 * multicast frames.  We know a beacon buffer is
1016b032f27cSSam Leffler 		 * available because we checked above.
1017b032f27cSSam Leffler 		 */
10186b349e5aSAdrian Chadd 		avp->av_bcbuf = TAILQ_FIRST(&sc->sc_bbuf);
10196b349e5aSAdrian Chadd 		TAILQ_REMOVE(&sc->sc_bbuf, avp->av_bcbuf, bf_list);
1020b032f27cSSam Leffler 		if (opmode != IEEE80211_M_IBSS || !sc->sc_hasveol) {
1021b032f27cSSam Leffler 			/*
1022b032f27cSSam Leffler 			 * Assign the vap to a beacon xmit slot.  As above
1023b032f27cSSam Leffler 			 * this cannot fail to find a free one.
1024b032f27cSSam Leffler 			 */
1025b032f27cSSam Leffler 			avp->av_bslot = assign_bslot(sc);
1026b032f27cSSam Leffler 			KASSERT(sc->sc_bslot[avp->av_bslot] == NULL,
1027b032f27cSSam Leffler 			    ("beacon slot %u not empty", avp->av_bslot));
1028b032f27cSSam Leffler 			sc->sc_bslot[avp->av_bslot] = vap;
1029b032f27cSSam Leffler 			sc->sc_nbcnvaps++;
1030b032f27cSSam Leffler 		}
1031b032f27cSSam Leffler 		if (sc->sc_hastsfadd && sc->sc_nbcnvaps > 0) {
1032b032f27cSSam Leffler 			/*
1033b032f27cSSam Leffler 			 * Multple vaps are to transmit beacons and we
1034b032f27cSSam Leffler 			 * have h/w support for TSF adjusting; enable
1035b032f27cSSam Leffler 			 * use of staggered beacons.
1036b032f27cSSam Leffler 			 */
1037b032f27cSSam Leffler 			sc->sc_stagbeacons = 1;
1038b032f27cSSam Leffler 		}
1039b032f27cSSam Leffler 		ath_txq_init(sc, &avp->av_mcastq, ATH_TXQ_SWQ);
1040b032f27cSSam Leffler 	}
1041b032f27cSSam Leffler 
1042b032f27cSSam Leffler 	ic->ic_opmode = ic_opmode;
1043b032f27cSSam Leffler 	if (opmode != IEEE80211_M_WDS) {
1044b032f27cSSam Leffler 		sc->sc_nvaps++;
1045b032f27cSSam Leffler 		if (opmode == IEEE80211_M_STA)
1046b032f27cSSam Leffler 			sc->sc_nstavaps++;
1047fe0dd789SSam Leffler 		if (opmode == IEEE80211_M_MBSS)
1048fe0dd789SSam Leffler 			sc->sc_nmeshvaps++;
1049b032f27cSSam Leffler 	}
1050b032f27cSSam Leffler 	switch (ic_opmode) {
1051b032f27cSSam Leffler 	case IEEE80211_M_IBSS:
1052b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_IBSS;
1053b032f27cSSam Leffler 		break;
1054b032f27cSSam Leffler 	case IEEE80211_M_STA:
1055b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_STA;
1056b032f27cSSam Leffler 		break;
1057b032f27cSSam Leffler 	case IEEE80211_M_AHDEMO:
1058584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
105910ad9a77SSam Leffler 		if (vap->iv_caps & IEEE80211_C_TDMA) {
106010ad9a77SSam Leffler 			sc->sc_tdma = 1;
106110ad9a77SSam Leffler 			/* NB: disable tsf adjust */
106210ad9a77SSam Leffler 			sc->sc_stagbeacons = 0;
106310ad9a77SSam Leffler 		}
106410ad9a77SSam Leffler 		/*
106510ad9a77SSam Leffler 		 * NB: adhoc demo mode is a pseudo mode; to the hal it's
106610ad9a77SSam Leffler 		 * just ap mode.
106710ad9a77SSam Leffler 		 */
106810ad9a77SSam Leffler 		/* fall thru... */
106910ad9a77SSam Leffler #endif
1070b032f27cSSam Leffler 	case IEEE80211_M_HOSTAP:
107159aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
1072b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_HOSTAP;
1073b032f27cSSam Leffler 		break;
1074b032f27cSSam Leffler 	case IEEE80211_M_MONITOR:
1075b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_MONITOR;
1076b032f27cSSam Leffler 		break;
1077b032f27cSSam Leffler 	default:
1078b032f27cSSam Leffler 		/* XXX should not happen */
1079b032f27cSSam Leffler 		break;
1080b032f27cSSam Leffler 	}
1081b032f27cSSam Leffler 	if (sc->sc_hastsfadd) {
1082b032f27cSSam Leffler 		/*
1083b032f27cSSam Leffler 		 * Configure whether or not TSF adjust should be done.
1084b032f27cSSam Leffler 		 */
1085b032f27cSSam Leffler 		ath_hal_settsfadjust(sc->sc_ah, sc->sc_stagbeacons);
1086b032f27cSSam Leffler 	}
108710ad9a77SSam Leffler 	if (flags & IEEE80211_CLONE_NOBEACONS) {
108810ad9a77SSam Leffler 		/*
108910ad9a77SSam Leffler 		 * Enable s/w beacon miss handling.
109010ad9a77SSam Leffler 		 */
109110ad9a77SSam Leffler 		sc->sc_swbmiss = 1;
109210ad9a77SSam Leffler 	}
1093b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1094b032f27cSSam Leffler 
1095b032f27cSSam Leffler 	/* complete setup */
1096b032f27cSSam Leffler 	ieee80211_vap_attach(vap, ath_media_change, ieee80211_media_status);
1097b032f27cSSam Leffler 	return vap;
1098b032f27cSSam Leffler bad2:
1099b032f27cSSam Leffler 	reclaim_address(sc, mac);
1100b032f27cSSam Leffler 	ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
1101b032f27cSSam Leffler bad:
1102b032f27cSSam Leffler 	free(avp, M_80211_VAP);
1103b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1104b032f27cSSam Leffler 	return NULL;
1105b032f27cSSam Leffler }
1106b032f27cSSam Leffler 
1107b032f27cSSam Leffler static void
1108b032f27cSSam Leffler ath_vap_delete(struct ieee80211vap *vap)
1109b032f27cSSam Leffler {
1110b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1111b032f27cSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
1112b032f27cSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
1113b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1114b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
1115b032f27cSSam Leffler 
1116b032f27cSSam Leffler 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1117b032f27cSSam Leffler 		/*
1118b032f27cSSam Leffler 		 * Quiesce the hardware while we remove the vap.  In
1119b032f27cSSam Leffler 		 * particular we need to reclaim all references to
1120b032f27cSSam Leffler 		 * the vap state by any frames pending on the tx queues.
1121b032f27cSSam Leffler 		 */
1122b032f27cSSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
1123b032f27cSSam Leffler 		ath_draintxq(sc);		/* stop xmit side */
1124b032f27cSSam Leffler 		ath_stoprecv(sc);		/* stop recv side */
1125b032f27cSSam Leffler 	}
1126b032f27cSSam Leffler 
1127b032f27cSSam Leffler 	ieee80211_vap_detach(vap);
1128b032f27cSSam Leffler 	ATH_LOCK(sc);
1129b032f27cSSam Leffler 	/*
1130b032f27cSSam Leffler 	 * Reclaim beacon state.  Note this must be done before
1131b032f27cSSam Leffler 	 * the vap instance is reclaimed as we may have a reference
1132b032f27cSSam Leffler 	 * to it in the buffer for the beacon frame.
1133b032f27cSSam Leffler 	 */
1134b032f27cSSam Leffler 	if (avp->av_bcbuf != NULL) {
1135b032f27cSSam Leffler 		if (avp->av_bslot != -1) {
1136b032f27cSSam Leffler 			sc->sc_bslot[avp->av_bslot] = NULL;
1137b032f27cSSam Leffler 			sc->sc_nbcnvaps--;
1138b032f27cSSam Leffler 		}
1139b032f27cSSam Leffler 		ath_beacon_return(sc, avp->av_bcbuf);
1140b032f27cSSam Leffler 		avp->av_bcbuf = NULL;
1141b032f27cSSam Leffler 		if (sc->sc_nbcnvaps == 0) {
1142b032f27cSSam Leffler 			sc->sc_stagbeacons = 0;
1143b032f27cSSam Leffler 			if (sc->sc_hastsfadd)
1144b032f27cSSam Leffler 				ath_hal_settsfadjust(sc->sc_ah, 0);
1145b032f27cSSam Leffler 		}
1146b032f27cSSam Leffler 		/*
1147b032f27cSSam Leffler 		 * Reclaim any pending mcast frames for the vap.
1148b032f27cSSam Leffler 		 */
1149b032f27cSSam Leffler 		ath_tx_draintxq(sc, &avp->av_mcastq);
1150b032f27cSSam Leffler 		ATH_TXQ_LOCK_DESTROY(&avp->av_mcastq);
1151b032f27cSSam Leffler 	}
1152b032f27cSSam Leffler 	/*
1153b032f27cSSam Leffler 	 * Update bookkeeping.
1154b032f27cSSam Leffler 	 */
1155b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA) {
1156b032f27cSSam Leffler 		sc->sc_nstavaps--;
1157b032f27cSSam Leffler 		if (sc->sc_nstavaps == 0 && sc->sc_swbmiss)
1158b032f27cSSam Leffler 			sc->sc_swbmiss = 0;
115959aa14a9SRui Paulo 	} else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
116059aa14a9SRui Paulo 	    vap->iv_opmode == IEEE80211_M_MBSS) {
1161b032f27cSSam Leffler 		reclaim_address(sc, vap->iv_myaddr);
1162b032f27cSSam Leffler 		ath_hal_setbssidmask(ah, sc->sc_hwbssidmask);
1163fe0dd789SSam Leffler 		if (vap->iv_opmode == IEEE80211_M_MBSS)
1164fe0dd789SSam Leffler 			sc->sc_nmeshvaps--;
1165b032f27cSSam Leffler 	}
1166b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_WDS)
1167b032f27cSSam Leffler 		sc->sc_nvaps--;
1168584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
116910ad9a77SSam Leffler 	/* TDMA operation ceases when the last vap is destroyed */
117010ad9a77SSam Leffler 	if (sc->sc_tdma && sc->sc_nvaps == 0) {
117110ad9a77SSam Leffler 		sc->sc_tdma = 0;
117210ad9a77SSam Leffler 		sc->sc_swbmiss = 0;
117310ad9a77SSam Leffler 	}
117410ad9a77SSam Leffler #endif
1175b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1176b032f27cSSam Leffler 	free(avp, M_80211_VAP);
1177b032f27cSSam Leffler 
1178b032f27cSSam Leffler 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1179b032f27cSSam Leffler 		/*
1180b032f27cSSam Leffler 		 * Restart rx+tx machines if still running (RUNNING will
1181b032f27cSSam Leffler 		 * be reset if we just destroyed the last vap).
1182b032f27cSSam Leffler 		 */
1183b032f27cSSam Leffler 		if (ath_startrecv(sc) != 0)
1184b032f27cSSam Leffler 			if_printf(ifp, "%s: unable to restart recv logic\n",
1185b032f27cSSam Leffler 			    __func__);
1186c89b957aSSam Leffler 		if (sc->sc_beacons) {		/* restart beacons */
1187c89b957aSSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
1188c89b957aSSam Leffler 			if (sc->sc_tdma)
1189c89b957aSSam Leffler 				ath_tdma_config(sc, NULL);
1190c89b957aSSam Leffler 			else
1191c89b957aSSam Leffler #endif
1192b032f27cSSam Leffler 				ath_beacon_config(sc, NULL);
1193c89b957aSSam Leffler 		}
1194b032f27cSSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
1195b032f27cSSam Leffler 	}
1196b032f27cSSam Leffler }
1197b032f27cSSam Leffler 
11985591b213SSam Leffler void
11995591b213SSam Leffler ath_suspend(struct ath_softc *sc)
12005591b213SSam Leffler {
1201fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
1202d3ac945bSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
12035591b213SSam Leffler 
1204c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1205c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
12065591b213SSam Leffler 
1207d3ac945bSSam Leffler 	sc->sc_resume_up = (ifp->if_flags & IFF_UP) != 0;
1208d3ac945bSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA)
12095591b213SSam Leffler 		ath_stop(ifp);
1210d3ac945bSSam Leffler 	else
1211d3ac945bSSam Leffler 		ieee80211_suspend_all(ic);
1212d3ac945bSSam Leffler 	/*
1213d3ac945bSSam Leffler 	 * NB: don't worry about putting the chip in low power
1214d3ac945bSSam Leffler 	 * mode; pci will power off our socket on suspend and
1215f29b8b7fSWarner Losh 	 * CardBus detaches the device.
1216d3ac945bSSam Leffler 	 */
1217d3ac945bSSam Leffler }
1218d3ac945bSSam Leffler 
1219d3ac945bSSam Leffler /*
1220d3ac945bSSam Leffler  * Reset the key cache since some parts do not reset the
1221d3ac945bSSam Leffler  * contents on resume.  First we clear all entries, then
1222d3ac945bSSam Leffler  * re-load keys that the 802.11 layer assumes are setup
1223d3ac945bSSam Leffler  * in h/w.
1224d3ac945bSSam Leffler  */
1225d3ac945bSSam Leffler static void
1226d3ac945bSSam Leffler ath_reset_keycache(struct ath_softc *sc)
1227d3ac945bSSam Leffler {
1228d3ac945bSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
1229d3ac945bSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
1230d3ac945bSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1231d3ac945bSSam Leffler 	int i;
1232d3ac945bSSam Leffler 
1233d3ac945bSSam Leffler 	for (i = 0; i < sc->sc_keymax; i++)
1234d3ac945bSSam Leffler 		ath_hal_keyreset(ah, i);
1235d3ac945bSSam Leffler 	ieee80211_crypto_reload_keys(ic);
12365591b213SSam Leffler }
12375591b213SSam Leffler 
12385591b213SSam Leffler void
12395591b213SSam Leffler ath_resume(struct ath_softc *sc)
12405591b213SSam Leffler {
1241fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
1242d3ac945bSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
1243d3ac945bSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1244d3ac945bSSam Leffler 	HAL_STATUS status;
12455591b213SSam Leffler 
1246c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1247c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
12485591b213SSam Leffler 
1249d3ac945bSSam Leffler 	/*
1250d3ac945bSSam Leffler 	 * Must reset the chip before we reload the
1251d3ac945bSSam Leffler 	 * keycache as we were powered down on suspend.
1252d3ac945bSSam Leffler 	 */
1253054d7b69SSam Leffler 	ath_hal_reset(ah, sc->sc_opmode,
1254054d7b69SSam Leffler 	    sc->sc_curchan != NULL ? sc->sc_curchan : ic->ic_curchan,
1255054d7b69SSam Leffler 	    AH_FALSE, &status);
1256d3ac945bSSam Leffler 	ath_reset_keycache(sc);
12577e5eb44dSAdrian Chadd 
12587e5eb44dSAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
12597e5eb44dSAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
12607e5eb44dSAdrian Chadd 
1261d3ac945bSSam Leffler 	if (sc->sc_resume_up) {
1262d3ac945bSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_STA) {
1263fc74a9f9SBrooks Davis 			ath_init(sc);
1264394f34a5SSam Leffler 			/*
1265394f34a5SSam Leffler 			 * Program the beacon registers using the last rx'd
1266394f34a5SSam Leffler 			 * beacon frame and enable sync on the next beacon
1267394f34a5SSam Leffler 			 * we see.  This should handle the case where we
1268394f34a5SSam Leffler 			 * wakeup and find the same AP and also the case where
1269394f34a5SSam Leffler 			 * we wakeup and need to roam.  For the latter we
1270394f34a5SSam Leffler 			 * should get bmiss events that trigger a roam.
1271394f34a5SSam Leffler 			 */
1272394f34a5SSam Leffler 			ath_beacon_config(sc, NULL);
1273394f34a5SSam Leffler 			sc->sc_syncbeacon = 1;
1274d3ac945bSSam Leffler 		} else
1275d3ac945bSSam Leffler 			ieee80211_resume_all(ic);
12765591b213SSam Leffler 	}
1277b50c8bdeSSam Leffler 	if (sc->sc_softled) {
1278869ff02eSSam Leffler 		ath_hal_gpioCfgOutput(ah, sc->sc_ledpin,
1279869ff02eSSam Leffler 		    HAL_GPIO_MUX_MAC_NETWORK_LED);
1280d3ac945bSSam Leffler 		ath_hal_gpioset(ah, sc->sc_ledpin, !sc->sc_ledon);
1281b50c8bdeSSam Leffler 	}
12822fd9aabbSAdrian Chadd 
12832fd9aabbSAdrian Chadd 	/* XXX beacons ? */
12846b59f5e3SSam Leffler }
12855591b213SSam Leffler 
12865591b213SSam Leffler void
12875591b213SSam Leffler ath_shutdown(struct ath_softc *sc)
12885591b213SSam Leffler {
1289fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
12905591b213SSam Leffler 
1291c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1292c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
12935591b213SSam Leffler 
12945591b213SSam Leffler 	ath_stop(ifp);
1295d3ac945bSSam Leffler 	/* NB: no point powering down chip as we're about to reboot */
12965591b213SSam Leffler }
12975591b213SSam Leffler 
1298c42a7b7eSSam Leffler /*
1299c42a7b7eSSam Leffler  * Interrupt handler.  Most of the actual processing is deferred.
1300c42a7b7eSSam Leffler  */
13015591b213SSam Leffler void
13025591b213SSam Leffler ath_intr(void *arg)
13035591b213SSam Leffler {
13045591b213SSam Leffler 	struct ath_softc *sc = arg;
1305fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
13065591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
13076f5fe81eSAdrian Chadd 	HAL_INT status = 0;
13088f939e79SAdrian Chadd 	uint32_t txqs;
13095591b213SSam Leffler 
13105591b213SSam Leffler 	if (sc->sc_invalid) {
13115591b213SSam Leffler 		/*
1312b58b3803SSam Leffler 		 * The hardware is not ready/present, don't touch anything.
1313b58b3803SSam Leffler 		 * Note this can happen early on if the IRQ is shared.
13145591b213SSam Leffler 		 */
1315c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid; ignored\n", __func__);
13165591b213SSam Leffler 		return;
13175591b213SSam Leffler 	}
1318fdd758d4SSam Leffler 	if (!ath_hal_intrpend(ah))		/* shared irq, not for us */
1319fdd758d4SSam Leffler 		return;
132068e8e04eSSam Leffler 	if ((ifp->if_flags & IFF_UP) == 0 ||
132168e8e04eSSam Leffler 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
132268e8e04eSSam Leffler 		HAL_INT status;
132368e8e04eSSam Leffler 
1324c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
1325c42a7b7eSSam Leffler 			__func__, ifp->if_flags);
13265591b213SSam Leffler 		ath_hal_getisr(ah, &status);	/* clear ISR */
13275591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable further intr's */
13285591b213SSam Leffler 		return;
13295591b213SSam Leffler 	}
1330c42a7b7eSSam Leffler 	/*
1331c42a7b7eSSam Leffler 	 * Figure out the reason(s) for the interrupt.  Note
1332c42a7b7eSSam Leffler 	 * that the hal returns a pseudo-ISR that may include
1333c42a7b7eSSam Leffler 	 * bits we haven't explicitly enabled so we mask the
1334c42a7b7eSSam Leffler 	 * value to insure we only process bits we requested.
1335c42a7b7eSSam Leffler 	 */
13365591b213SSam Leffler 	ath_hal_getisr(ah, &status);		/* NB: clears ISR too */
1337c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_INTR, "%s: status 0x%x\n", __func__, status);
1338ecddff40SSam Leffler 	status &= sc->sc_imask;			/* discard unasked for bits */
13396f5fe81eSAdrian Chadd 
13406f5fe81eSAdrian Chadd 	/* Short-circuit un-handled interrupts */
13416f5fe81eSAdrian Chadd 	if (status == 0x0)
13426f5fe81eSAdrian Chadd 		return;
13436f5fe81eSAdrian Chadd 
13445591b213SSam Leffler 	if (status & HAL_INT_FATAL) {
13455591b213SSam Leffler 		sc->sc_stats.ast_hardware++;
13465591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
134716c8acaaSSam Leffler 		ath_fatal_proc(sc, 0);
13485591b213SSam Leffler 	} else {
1349c42a7b7eSSam Leffler 		if (status & HAL_INT_SWBA) {
1350c42a7b7eSSam Leffler 			/*
1351c42a7b7eSSam Leffler 			 * Software beacon alert--time to send a beacon.
1352c42a7b7eSSam Leffler 			 * Handle beacon transmission directly; deferring
1353c42a7b7eSSam Leffler 			 * this is too slow to meet timing constraints
1354c42a7b7eSSam Leffler 			 * under load.
1355c42a7b7eSSam Leffler 			 */
1356584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
135710ad9a77SSam Leffler 			if (sc->sc_tdma) {
135810ad9a77SSam Leffler 				if (sc->sc_tdmaswba == 0) {
135910ad9a77SSam Leffler 					struct ieee80211com *ic = ifp->if_l2com;
136010ad9a77SSam Leffler 					struct ieee80211vap *vap =
136110ad9a77SSam Leffler 					    TAILQ_FIRST(&ic->ic_vaps);
136210ad9a77SSam Leffler 					ath_tdma_beacon_send(sc, vap);
136310ad9a77SSam Leffler 					sc->sc_tdmaswba =
136410ad9a77SSam Leffler 					    vap->iv_tdma->tdma_bintval;
136510ad9a77SSam Leffler 				} else
136610ad9a77SSam Leffler 					sc->sc_tdmaswba--;
136710ad9a77SSam Leffler 			} else
136810ad9a77SSam Leffler #endif
1369339ccfb3SSam Leffler 			{
1370c42a7b7eSSam Leffler 				ath_beacon_proc(sc, 0);
1371339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
1372339ccfb3SSam Leffler 				/*
1373339ccfb3SSam Leffler 				 * Schedule the rx taskq in case there's no
1374339ccfb3SSam Leffler 				 * traffic so any frames held on the staging
1375339ccfb3SSam Leffler 				 * queue are aged and potentially flushed.
1376339ccfb3SSam Leffler 				 */
1377339ccfb3SSam Leffler 				taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
1378339ccfb3SSam Leffler #endif
1379339ccfb3SSam Leffler 			}
1380c42a7b7eSSam Leffler 		}
13815591b213SSam Leffler 		if (status & HAL_INT_RXEOL) {
13828f939e79SAdrian Chadd 			int imask;
13835591b213SSam Leffler 			/*
13845591b213SSam Leffler 			 * NB: the hardware should re-read the link when
13855591b213SSam Leffler 			 *     RXE bit is written, but it doesn't work at
13865591b213SSam Leffler 			 *     least on older hardware revs.
13875591b213SSam Leffler 			 */
13885591b213SSam Leffler 			sc->sc_stats.ast_rxeol++;
138973f895fcSAdrian Chadd 			/*
139073f895fcSAdrian Chadd 			 * Disable RXEOL/RXORN - prevent an interrupt
139173f895fcSAdrian Chadd 			 * storm until the PCU logic can be reset.
13921fdadc0fSAdrian Chadd 			 * In case the interface is reset some other
13931fdadc0fSAdrian Chadd 			 * way before "sc_kickpcu" is called, don't
13941fdadc0fSAdrian Chadd 			 * modify sc_imask - that way if it is reset
13951fdadc0fSAdrian Chadd 			 * by a call to ath_reset() somehow, the
13961fdadc0fSAdrian Chadd 			 * interrupt mask will be correctly reprogrammed.
139773f895fcSAdrian Chadd 			 */
13988f939e79SAdrian Chadd 			ATH_LOCK(sc);
13998f939e79SAdrian Chadd 			imask = sc->sc_imask;
14001fdadc0fSAdrian Chadd 			imask &= ~(HAL_INT_RXEOL | HAL_INT_RXORN);
14011fdadc0fSAdrian Chadd 			ath_hal_intrset(ah, imask);
14021fdadc0fSAdrian Chadd 			/*
14038f939e79SAdrian Chadd 			 * Only blank sc_rxlink if we've not yet kicked
14048f939e79SAdrian Chadd 			 * the PCU.
14058f939e79SAdrian Chadd 			 *
14068f939e79SAdrian Chadd 			 * This isn't entirely correct - the correct solution
14078f939e79SAdrian Chadd 			 * would be to have a PCU lock and engage that for
14088f939e79SAdrian Chadd 			 * the duration of the PCU fiddling; which would include
14098f939e79SAdrian Chadd 			 * running the RX process. Otherwise we could end up
14108f939e79SAdrian Chadd 			 * messing up the RX descriptor chain and making the
14118f939e79SAdrian Chadd 			 * RX desc list much shorter.
14128f939e79SAdrian Chadd 			 */
14138f939e79SAdrian Chadd 			if (! sc->sc_kickpcu)
14148f939e79SAdrian Chadd 				sc->sc_rxlink = NULL;
14158f939e79SAdrian Chadd 			sc->sc_kickpcu = 1;
14168f939e79SAdrian Chadd 			ATH_UNLOCK(sc);
14178f939e79SAdrian Chadd 			/*
14181fdadc0fSAdrian Chadd 			 * Enqueue an RX proc, to handled whatever
14191fdadc0fSAdrian Chadd 			 * is in the RX queue.
14201fdadc0fSAdrian Chadd 			 * This will then kick the PCU.
14211fdadc0fSAdrian Chadd 			 */
14221fdadc0fSAdrian Chadd 			taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
14235591b213SSam Leffler 		}
14245591b213SSam Leffler 		if (status & HAL_INT_TXURN) {
14255591b213SSam Leffler 			sc->sc_stats.ast_txurn++;
14265591b213SSam Leffler 			/* bump tx trigger level */
14275591b213SSam Leffler 			ath_hal_updatetxtriglevel(ah, AH_TRUE);
14285591b213SSam Leffler 		}
14298f939e79SAdrian Chadd 		if (status & HAL_INT_RX) {
14308f939e79SAdrian Chadd 			sc->sc_stats.ast_rx_intr++;
14310bbf5441SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
14328f939e79SAdrian Chadd 		}
14338f939e79SAdrian Chadd 		if (status & HAL_INT_TX) {
14348f939e79SAdrian Chadd 			sc->sc_stats.ast_tx_intr++;
14358f939e79SAdrian Chadd 			/*
14368f939e79SAdrian Chadd 			 * Grab all the currently set bits in the HAL txq bitmap
14378f939e79SAdrian Chadd 			 * and blank them. This is the only place we should be
14388f939e79SAdrian Chadd 			 * doing this.
14398f939e79SAdrian Chadd 			 */
14408f939e79SAdrian Chadd 			ATH_LOCK(sc);
14418f939e79SAdrian Chadd 			txqs = 0xffffffff;
14428f939e79SAdrian Chadd 			ath_hal_gettxintrtxqs(sc->sc_ah, &txqs);
14438f939e79SAdrian Chadd 			sc->sc_txq_active |= txqs;
14448f939e79SAdrian Chadd 			ATH_UNLOCK(sc);
14450bbf5441SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_txtask);
14468f939e79SAdrian Chadd 		}
14475591b213SSam Leffler 		if (status & HAL_INT_BMISS) {
14485591b213SSam Leffler 			sc->sc_stats.ast_bmiss++;
14490bbf5441SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_bmisstask);
14505591b213SSam Leffler 		}
14516ad02dbaSAdrian Chadd 		if (status & HAL_INT_GTT)
14526ad02dbaSAdrian Chadd 			sc->sc_stats.ast_tx_timeout++;
14535594f5c0SAdrian Chadd 		if (status & HAL_INT_CST)
14545594f5c0SAdrian Chadd 			sc->sc_stats.ast_tx_cst++;
1455c42a7b7eSSam Leffler 		if (status & HAL_INT_MIB) {
1456c42a7b7eSSam Leffler 			sc->sc_stats.ast_mib++;
1457c42a7b7eSSam Leffler 			/*
1458c42a7b7eSSam Leffler 			 * Disable interrupts until we service the MIB
1459c42a7b7eSSam Leffler 			 * interrupt; otherwise it will continue to fire.
1460c42a7b7eSSam Leffler 			 */
1461c42a7b7eSSam Leffler 			ath_hal_intrset(ah, 0);
1462c42a7b7eSSam Leffler 			/*
1463c42a7b7eSSam Leffler 			 * Let the hal handle the event.  We assume it will
1464c42a7b7eSSam Leffler 			 * clear whatever condition caused the interrupt.
1465c42a7b7eSSam Leffler 			 */
1466ffa2cab6SSam Leffler 			ath_hal_mibevent(ah, &sc->sc_halstats);
14678f939e79SAdrian Chadd 			/*
14688f939e79SAdrian Chadd 			 * Don't reset the interrupt if we've just
14698f939e79SAdrian Chadd 			 * kicked the PCU, or we may get a nested
14708f939e79SAdrian Chadd 			 * RXEOL before the rxproc has had a chance
14718f939e79SAdrian Chadd 			 * to run.
14728f939e79SAdrian Chadd 			 */
14738f939e79SAdrian Chadd 			ATH_LOCK(sc);
14748f939e79SAdrian Chadd 			if (sc->sc_kickpcu == 0)
1475c42a7b7eSSam Leffler 				ath_hal_intrset(ah, sc->sc_imask);
14768f939e79SAdrian Chadd 			ATH_UNLOCK(sc);
1477c42a7b7eSSam Leffler 		}
14789c4fc1e8SSam Leffler 		if (status & HAL_INT_RXORN) {
14799c4fc1e8SSam Leffler 			/* NB: hal marks HAL_INT_FATAL when RXORN is fatal */
14809c4fc1e8SSam Leffler 			sc->sc_stats.ast_rxorn++;
14819c4fc1e8SSam Leffler 		}
14825591b213SSam Leffler 	}
14835591b213SSam Leffler }
14845591b213SSam Leffler 
14855591b213SSam Leffler static void
14865591b213SSam Leffler ath_fatal_proc(void *arg, int pending)
14875591b213SSam Leffler {
14885591b213SSam Leffler 	struct ath_softc *sc = arg;
1489fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
149016c8acaaSSam Leffler 	u_int32_t *state;
149116c8acaaSSam Leffler 	u_int32_t len;
149268e8e04eSSam Leffler 	void *sp;
14935591b213SSam Leffler 
1494c42a7b7eSSam Leffler 	if_printf(ifp, "hardware error; resetting\n");
149516c8acaaSSam Leffler 	/*
149616c8acaaSSam Leffler 	 * Fatal errors are unrecoverable.  Typically these
149716c8acaaSSam Leffler 	 * are caused by DMA errors.  Collect h/w state from
149816c8acaaSSam Leffler 	 * the hal so we can diagnose what's going on.
149916c8acaaSSam Leffler 	 */
150068e8e04eSSam Leffler 	if (ath_hal_getfatalstate(sc->sc_ah, &sp, &len)) {
150116c8acaaSSam Leffler 		KASSERT(len >= 6*sizeof(u_int32_t), ("len %u bytes", len));
150268e8e04eSSam Leffler 		state = sp;
150316c8acaaSSam Leffler 		if_printf(ifp, "0x%08x 0x%08x 0x%08x, 0x%08x 0x%08x 0x%08x\n",
150416c8acaaSSam Leffler 		    state[0], state[1] , state[2], state[3],
150516c8acaaSSam Leffler 		    state[4], state[5]);
150616c8acaaSSam Leffler 	}
1507c42a7b7eSSam Leffler 	ath_reset(ifp);
15085591b213SSam Leffler }
15095591b213SSam Leffler 
15105591b213SSam Leffler static void
1511b032f27cSSam Leffler ath_bmiss_vap(struct ieee80211vap *vap)
15125591b213SSam Leffler {
151359fbb257SSam Leffler 	/*
151459fbb257SSam Leffler 	 * Workaround phantom bmiss interrupts by sanity-checking
151559fbb257SSam Leffler 	 * the time of our last rx'd frame.  If it is within the
151659fbb257SSam Leffler 	 * beacon miss interval then ignore the interrupt.  If it's
151759fbb257SSam Leffler 	 * truly a bmiss we'll get another interrupt soon and that'll
151859fbb257SSam Leffler 	 * be dispatched up for processing.  Note this applies only
151959fbb257SSam Leffler 	 * for h/w beacon miss events.
152059fbb257SSam Leffler 	 */
152159fbb257SSam Leffler 	if ((vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) == 0) {
1522a7ace843SSam Leffler 		struct ifnet *ifp = vap->iv_ic->ic_ifp;
1523a7ace843SSam Leffler 		struct ath_softc *sc = ifp->if_softc;
1524d7736e13SSam Leffler 		u_int64_t lastrx = sc->sc_lastrx;
1525d7736e13SSam Leffler 		u_int64_t tsf = ath_hal_gettsf64(sc->sc_ah);
1526d7736e13SSam Leffler 		u_int bmisstimeout =
1527b032f27cSSam Leffler 			vap->iv_bmissthreshold * vap->iv_bss->ni_intval * 1024;
1528d7736e13SSam Leffler 
1529d7736e13SSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
1530d7736e13SSam Leffler 		    "%s: tsf %llu lastrx %lld (%llu) bmiss %u\n",
1531d7736e13SSam Leffler 		    __func__, (unsigned long long) tsf,
1532d7736e13SSam Leffler 		    (unsigned long long)(tsf - lastrx),
1533d7736e13SSam Leffler 		    (unsigned long long) lastrx, bmisstimeout);
153459fbb257SSam Leffler 
153559fbb257SSam Leffler 		if (tsf - lastrx <= bmisstimeout) {
1536d7736e13SSam Leffler 			sc->sc_stats.ast_bmiss_phantom++;
153759fbb257SSam Leffler 			return;
153859fbb257SSam Leffler 		}
153959fbb257SSam Leffler 	}
154059fbb257SSam Leffler 	ATH_VAP(vap)->av_bmiss(vap);
1541e585d188SSam Leffler }
1542b032f27cSSam Leffler 
1543459bc4f0SSam Leffler static int
1544459bc4f0SSam Leffler ath_hal_gethangstate(struct ath_hal *ah, uint32_t mask, uint32_t *hangs)
1545459bc4f0SSam Leffler {
1546459bc4f0SSam Leffler 	uint32_t rsize;
1547459bc4f0SSam Leffler 	void *sp;
1548459bc4f0SSam Leffler 
154925c96056SAdrian Chadd 	if (!ath_hal_getdiagstate(ah, HAL_DIAG_CHECK_HANGS, &mask, sizeof(mask), &sp, &rsize))
1550459bc4f0SSam Leffler 		return 0;
1551459bc4f0SSam Leffler 	KASSERT(rsize == sizeof(uint32_t), ("resultsize %u", rsize));
1552459bc4f0SSam Leffler 	*hangs = *(uint32_t *)sp;
1553459bc4f0SSam Leffler 	return 1;
1554459bc4f0SSam Leffler }
1555459bc4f0SSam Leffler 
1556b032f27cSSam Leffler static void
1557b032f27cSSam Leffler ath_bmiss_proc(void *arg, int pending)
1558b032f27cSSam Leffler {
1559b032f27cSSam Leffler 	struct ath_softc *sc = arg;
1560b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
1561459bc4f0SSam Leffler 	uint32_t hangs;
1562b032f27cSSam Leffler 
1563b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: pending %u\n", __func__, pending);
1564459bc4f0SSam Leffler 
1565459bc4f0SSam Leffler 	if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0) {
15664fa8d4efSDaniel Eischen 		if_printf(ifp, "bb hang detected (0x%x), resetting\n", hangs);
1567459bc4f0SSam Leffler 		ath_reset(ifp);
1568459bc4f0SSam Leffler 	} else
1569b032f27cSSam Leffler 		ieee80211_beacon_miss(ifp->if_l2com);
15705591b213SSam Leffler }
15715591b213SSam Leffler 
1572724c193aSSam Leffler /*
1573b032f27cSSam Leffler  * Handle TKIP MIC setup to deal hardware that doesn't do MIC
1574b032f27cSSam Leffler  * calcs together with WME.  If necessary disable the crypto
1575b032f27cSSam Leffler  * hardware and mark the 802.11 state so keys will be setup
1576b032f27cSSam Leffler  * with the MIC work done in software.
1577b032f27cSSam Leffler  */
1578b032f27cSSam Leffler static void
1579b032f27cSSam Leffler ath_settkipmic(struct ath_softc *sc)
1580b032f27cSSam Leffler {
1581b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
1582b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
1583b032f27cSSam Leffler 
1584b032f27cSSam Leffler 	if ((ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIP) && !sc->sc_wmetkipmic) {
1585b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_WME) {
1586b032f27cSSam Leffler 			ath_hal_settkipmic(sc->sc_ah, AH_FALSE);
1587b032f27cSSam Leffler 			ic->ic_cryptocaps &= ~IEEE80211_CRYPTO_TKIPMIC;
1588b032f27cSSam Leffler 		} else {
1589b032f27cSSam Leffler 			ath_hal_settkipmic(sc->sc_ah, AH_TRUE);
1590b032f27cSSam Leffler 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
1591b032f27cSSam Leffler 		}
1592b032f27cSSam Leffler 	}
1593b032f27cSSam Leffler }
1594b032f27cSSam Leffler 
15955591b213SSam Leffler static void
15965591b213SSam Leffler ath_init(void *arg)
15975591b213SSam Leffler {
15985591b213SSam Leffler 	struct ath_softc *sc = (struct ath_softc *) arg;
1599fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
1600b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
16015591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
16025591b213SSam Leffler 	HAL_STATUS status;
16035591b213SSam Leffler 
1604c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
1605c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
16065591b213SSam Leffler 
1607f0b2a0beSSam Leffler 	ATH_LOCK(sc);
16085591b213SSam Leffler 	/*
16095591b213SSam Leffler 	 * Stop anything previously setup.  This is safe
16105591b213SSam Leffler 	 * whether this is the first time through or not.
16115591b213SSam Leffler 	 */
1612c42a7b7eSSam Leffler 	ath_stop_locked(ifp);
16135591b213SSam Leffler 
16145591b213SSam Leffler 	/*
16155591b213SSam Leffler 	 * The basic interface to setting the hardware in a good
16165591b213SSam Leffler 	 * state is ``reset''.  On return the hardware is known to
16175591b213SSam Leffler 	 * be powered up and with interrupts disabled.  This must
16185591b213SSam Leffler 	 * be followed by initialization of the appropriate bits
16195591b213SSam Leffler 	 * and then setup of the interrupt mask.
16205591b213SSam Leffler 	 */
1621b032f27cSSam Leffler 	ath_settkipmic(sc);
162259efa8b5SSam Leffler 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_FALSE, &status)) {
16235591b213SSam Leffler 		if_printf(ifp, "unable to reset hardware; hal status %u\n",
16245591b213SSam Leffler 			status);
1625b032f27cSSam Leffler 		ATH_UNLOCK(sc);
1626b032f27cSSam Leffler 		return;
16275591b213SSam Leffler 	}
1628b032f27cSSam Leffler 	ath_chan_change(sc, ic->ic_curchan);
16295591b213SSam Leffler 
163048237774SAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
163148237774SAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
163248237774SAdrian Chadd 
16335591b213SSam Leffler 	/*
1634c59005e9SSam Leffler 	 * Likewise this is set during reset so update
1635c59005e9SSam Leffler 	 * state cached in the driver.
1636c59005e9SSam Leffler 	 */
1637c59005e9SSam Leffler 	sc->sc_diversity = ath_hal_getdiversity(ah);
16382dc7fcc4SSam Leffler 	sc->sc_lastlongcal = 0;
16392dc7fcc4SSam Leffler 	sc->sc_resetcal = 1;
16402dc7fcc4SSam Leffler 	sc->sc_lastcalreset = 0;
1641a108ab63SAdrian Chadd 	sc->sc_lastani = 0;
1642a108ab63SAdrian Chadd 	sc->sc_lastshortcal = 0;
1643a108ab63SAdrian Chadd 	sc->sc_doresetcal = AH_FALSE;
16442fd9aabbSAdrian Chadd 	/*
16452fd9aabbSAdrian Chadd 	 * Beacon timers were cleared here; give ath_newstate()
16462fd9aabbSAdrian Chadd 	 * a hint that the beacon timers should be poked when
16472fd9aabbSAdrian Chadd 	 * things transition to the RUN state.
16482fd9aabbSAdrian Chadd 	 */
16492fd9aabbSAdrian Chadd 	sc->sc_beacons = 0;
1650c42a7b7eSSam Leffler 
1651c42a7b7eSSam Leffler 	/*
16528f939e79SAdrian Chadd 	 * Initial aggregation settings.
16538f939e79SAdrian Chadd 	 */
16548f939e79SAdrian Chadd 	sc->sc_hwq_limit = ATH_AGGR_MIN_QDEPTH;
16558f939e79SAdrian Chadd 	sc->sc_tid_hwq_lo = ATH_AGGR_SCHED_LOW;
16568f939e79SAdrian Chadd 	sc->sc_tid_hwq_hi = ATH_AGGR_SCHED_HIGH;
16578f939e79SAdrian Chadd 
16588f939e79SAdrian Chadd 	/*
16595591b213SSam Leffler 	 * Setup the hardware after reset: the key cache
16605591b213SSam Leffler 	 * is filled as needed and the receive engine is
16615591b213SSam Leffler 	 * set going.  Frame transmit is handled entirely
16625591b213SSam Leffler 	 * in the frame output path; there's nothing to do
16635591b213SSam Leffler 	 * here except setup the interrupt mask.
16645591b213SSam Leffler 	 */
16655591b213SSam Leffler 	if (ath_startrecv(sc) != 0) {
16665591b213SSam Leffler 		if_printf(ifp, "unable to start recv logic\n");
1667b032f27cSSam Leffler 		ATH_UNLOCK(sc);
1668b032f27cSSam Leffler 		return;
16695591b213SSam Leffler 	}
16705591b213SSam Leffler 
16715591b213SSam Leffler 	/*
16725591b213SSam Leffler 	 * Enable interrupts.
16735591b213SSam Leffler 	 */
16745591b213SSam Leffler 	sc->sc_imask = HAL_INT_RX | HAL_INT_TX
16755591b213SSam Leffler 		  | HAL_INT_RXEOL | HAL_INT_RXORN
16765591b213SSam Leffler 		  | HAL_INT_FATAL | HAL_INT_GLOBAL;
1677c42a7b7eSSam Leffler 	/*
1678c42a7b7eSSam Leffler 	 * Enable MIB interrupts when there are hardware phy counters.
1679c42a7b7eSSam Leffler 	 * Note we only do this (at the moment) for station mode.
1680c42a7b7eSSam Leffler 	 */
1681c42a7b7eSSam Leffler 	if (sc->sc_needmib && ic->ic_opmode == IEEE80211_M_STA)
1682c42a7b7eSSam Leffler 		sc->sc_imask |= HAL_INT_MIB;
16835591b213SSam Leffler 
16845594f5c0SAdrian Chadd 	/* Enable global TX timeout and carrier sense timeout if available */
16856ad02dbaSAdrian Chadd 	if (ath_hal_gtxto_supported(ah))
16863788ebedSAdrian Chadd 		sc->sc_imask |= HAL_INT_GTT;
1687d0a0ebc6SAdrian Chadd 
1688d0a0ebc6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: imask=0x%x\n",
1689d0a0ebc6SAdrian Chadd 		__func__, sc->sc_imask);
16906ad02dbaSAdrian Chadd 
169113f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
16922e986da5SSam Leffler 	callout_reset(&sc->sc_wd_ch, hz, ath_watchdog, sc);
1693b032f27cSSam Leffler 	ath_hal_intrset(ah, sc->sc_imask);
16945591b213SSam Leffler 
1695b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1696b032f27cSSam Leffler 
169786e07743SSam Leffler #ifdef ATH_TX99_DIAG
169886e07743SSam Leffler 	if (sc->sc_tx99 != NULL)
169986e07743SSam Leffler 		sc->sc_tx99->start(sc->sc_tx99);
170086e07743SSam Leffler 	else
170186e07743SSam Leffler #endif
1702b032f27cSSam Leffler 	ieee80211_start_all(ic);		/* start all vap's */
17035591b213SSam Leffler }
17045591b213SSam Leffler 
17055591b213SSam Leffler static void
1706c42a7b7eSSam Leffler ath_stop_locked(struct ifnet *ifp)
17075591b213SSam Leffler {
17085591b213SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
17095591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
17105591b213SSam Leffler 
1711c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid %u if_flags 0x%x\n",
1712c42a7b7eSSam Leffler 		__func__, sc->sc_invalid, ifp->if_flags);
17135591b213SSam Leffler 
1714c42a7b7eSSam Leffler 	ATH_LOCK_ASSERT(sc);
171513f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
17165591b213SSam Leffler 		/*
17175591b213SSam Leffler 		 * Shutdown the hardware and driver:
1718c42a7b7eSSam Leffler 		 *    reset 802.11 state machine
17195591b213SSam Leffler 		 *    turn off timers
1720c42a7b7eSSam Leffler 		 *    disable interrupts
1721c42a7b7eSSam Leffler 		 *    turn off the radio
17225591b213SSam Leffler 		 *    clear transmit machinery
17235591b213SSam Leffler 		 *    clear receive machinery
17245591b213SSam Leffler 		 *    drain and release tx queues
17255591b213SSam Leffler 		 *    reclaim beacon resources
17265591b213SSam Leffler 		 *    power down hardware
17275591b213SSam Leffler 		 *
17285591b213SSam Leffler 		 * Note that some of this work is not possible if the
17295591b213SSam Leffler 		 * hardware is gone (invalid).
17305591b213SSam Leffler 		 */
173186e07743SSam Leffler #ifdef ATH_TX99_DIAG
173286e07743SSam Leffler 		if (sc->sc_tx99 != NULL)
173386e07743SSam Leffler 			sc->sc_tx99->stop(sc->sc_tx99);
173486e07743SSam Leffler #endif
17352e986da5SSam Leffler 		callout_stop(&sc->sc_wd_ch);
17362e986da5SSam Leffler 		sc->sc_wd_timer = 0;
173713f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1738c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
17393e50ec2cSSam Leffler 			if (sc->sc_softled) {
17403e50ec2cSSam Leffler 				callout_stop(&sc->sc_ledtimer);
17413e50ec2cSSam Leffler 				ath_hal_gpioset(ah, sc->sc_ledpin,
17423e50ec2cSSam Leffler 					!sc->sc_ledon);
17433e50ec2cSSam Leffler 				sc->sc_blinking = 0;
17443e50ec2cSSam Leffler 			}
17455591b213SSam Leffler 			ath_hal_intrset(ah, 0);
1746c42a7b7eSSam Leffler 		}
17475591b213SSam Leffler 		ath_draintxq(sc);
1748c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
17495591b213SSam Leffler 			ath_stoprecv(sc);
1750c42a7b7eSSam Leffler 			ath_hal_phydisable(ah);
1751c42a7b7eSSam Leffler 		} else
17525591b213SSam Leffler 			sc->sc_rxlink = NULL;
1753b032f27cSSam Leffler 		ath_beacon_free(sc);	/* XXX not needed */
1754c42a7b7eSSam Leffler 	}
1755c42a7b7eSSam Leffler }
1756c42a7b7eSSam Leffler 
1757c42a7b7eSSam Leffler static void
1758c42a7b7eSSam Leffler ath_stop(struct ifnet *ifp)
1759c42a7b7eSSam Leffler {
1760c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
1761c42a7b7eSSam Leffler 
1762c42a7b7eSSam Leffler 	ATH_LOCK(sc);
1763c42a7b7eSSam Leffler 	ath_stop_locked(ifp);
1764f0b2a0beSSam Leffler 	ATH_UNLOCK(sc);
17655591b213SSam Leffler }
17665591b213SSam Leffler 
17675591b213SSam Leffler /*
17685591b213SSam Leffler  * Reset the hardware w/o losing operational state.  This is
17695591b213SSam Leffler  * basically a more efficient way of doing ath_stop, ath_init,
17705591b213SSam Leffler  * followed by state transitions to the current 802.11
1771c42a7b7eSSam Leffler  * operational state.  Used to recover from various errors and
1772c42a7b7eSSam Leffler  * to reset or reload hardware state.
17735591b213SSam Leffler  */
17746079fdbeSAdrian Chadd int
1775c42a7b7eSSam Leffler ath_reset(struct ifnet *ifp)
17765591b213SSam Leffler {
1777c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
1778b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
17795591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
17805591b213SSam Leffler 	HAL_STATUS status;
17815591b213SSam Leffler 
17825591b213SSam Leffler 	ath_hal_intrset(ah, 0);		/* disable interrupts */
17835591b213SSam Leffler 	ath_draintxq(sc);		/* stop xmit side */
17845591b213SSam Leffler 	ath_stoprecv(sc);		/* stop recv side */
1785b032f27cSSam Leffler 	ath_settkipmic(sc);		/* configure TKIP MIC handling */
17865591b213SSam Leffler 	/* NB: indicate channel change so we do a full reset */
178759efa8b5SSam Leffler 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_TRUE, &status))
17885591b213SSam Leffler 		if_printf(ifp, "%s: unable to reset hardware; hal status %u\n",
17895591b213SSam Leffler 			__func__, status);
1790c59005e9SSam Leffler 	sc->sc_diversity = ath_hal_getdiversity(ah);
179148237774SAdrian Chadd 
179248237774SAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
179348237774SAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
179448237774SAdrian Chadd 
179568e8e04eSSam Leffler 	if (ath_startrecv(sc) != 0)	/* restart recv */
179668e8e04eSSam Leffler 		if_printf(ifp, "%s: unable to start recv logic\n", __func__);
1797c42a7b7eSSam Leffler 	/*
1798c42a7b7eSSam Leffler 	 * We may be doing a reset in response to an ioctl
1799c42a7b7eSSam Leffler 	 * that changes the channel so update any state that
1800c42a7b7eSSam Leffler 	 * might change as a result.
1801c42a7b7eSSam Leffler 	 */
1802724c193aSSam Leffler 	ath_chan_change(sc, ic->ic_curchan);
1803c89b957aSSam Leffler 	if (sc->sc_beacons) {		/* restart beacons */
1804584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
180510ad9a77SSam Leffler 		if (sc->sc_tdma)
180610ad9a77SSam Leffler 			ath_tdma_config(sc, NULL);
180710ad9a77SSam Leffler 		else
180810ad9a77SSam Leffler #endif
1809c89b957aSSam Leffler 			ath_beacon_config(sc, NULL);
181010ad9a77SSam Leffler 	}
1811c42a7b7eSSam Leffler 	ath_hal_intrset(ah, sc->sc_imask);
1812c42a7b7eSSam Leffler 
1813c42a7b7eSSam Leffler 	ath_start(ifp);			/* restart xmit */
1814c42a7b7eSSam Leffler 	return 0;
18155591b213SSam Leffler }
18165591b213SSam Leffler 
181768e8e04eSSam Leffler static int
1818b032f27cSSam Leffler ath_reset_vap(struct ieee80211vap *vap, u_long cmd)
1819b032f27cSSam Leffler {
18204b54a231SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
18214b54a231SSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
18224b54a231SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
18234b54a231SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
18244b54a231SSam Leffler 
18254b54a231SSam Leffler 	switch (cmd) {
18264b54a231SSam Leffler 	case IEEE80211_IOC_TXPOWER:
18274b54a231SSam Leffler 		/*
18284b54a231SSam Leffler 		 * If per-packet TPC is enabled, then we have nothing
18294b54a231SSam Leffler 		 * to do; otherwise we need to force the global limit.
18304b54a231SSam Leffler 		 * All this can happen directly; no need to reset.
18314b54a231SSam Leffler 		 */
18324b54a231SSam Leffler 		if (!ath_hal_gettpc(ah))
18334b54a231SSam Leffler 			ath_hal_settxpowlimit(ah, ic->ic_txpowlimit);
18344b54a231SSam Leffler 		return 0;
18354b54a231SSam Leffler 	}
18364b54a231SSam Leffler 	return ath_reset(ifp);
1837b032f27cSSam Leffler }
1838b032f27cSSam Leffler 
1839b8e788a5SAdrian Chadd struct ath_buf *
184010ad9a77SSam Leffler _ath_getbuf_locked(struct ath_softc *sc)
184110ad9a77SSam Leffler {
184210ad9a77SSam Leffler 	struct ath_buf *bf;
184310ad9a77SSam Leffler 
184410ad9a77SSam Leffler 	ATH_TXBUF_LOCK_ASSERT(sc);
184510ad9a77SSam Leffler 
18466b349e5aSAdrian Chadd 	bf = TAILQ_FIRST(&sc->sc_txbuf);
184710ad9a77SSam Leffler 	if (bf != NULL && (bf->bf_flags & ATH_BUF_BUSY) == 0)
18486b349e5aSAdrian Chadd 		TAILQ_REMOVE(&sc->sc_txbuf, bf, bf_list);
184910ad9a77SSam Leffler 	else
185010ad9a77SSam Leffler 		bf = NULL;
185110ad9a77SSam Leffler 	if (bf == NULL) {
185210ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: %s\n", __func__,
18536b349e5aSAdrian Chadd 		    TAILQ_FIRST(&sc->sc_txbuf) == NULL ?
185410ad9a77SSam Leffler 			"out of xmit buffers" : "xmit buffer busy");
185510ad9a77SSam Leffler 	}
185610ad9a77SSam Leffler 	return bf;
185710ad9a77SSam Leffler }
185810ad9a77SSam Leffler 
1859b8e788a5SAdrian Chadd struct ath_buf *
186010ad9a77SSam Leffler ath_getbuf(struct ath_softc *sc)
186110ad9a77SSam Leffler {
186210ad9a77SSam Leffler 	struct ath_buf *bf;
186310ad9a77SSam Leffler 
186410ad9a77SSam Leffler 	ATH_TXBUF_LOCK(sc);
186510ad9a77SSam Leffler 	bf = _ath_getbuf_locked(sc);
186610ad9a77SSam Leffler 	if (bf == NULL) {
186710ad9a77SSam Leffler 		struct ifnet *ifp = sc->sc_ifp;
186810ad9a77SSam Leffler 
186910ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: stop queue\n", __func__);
187010ad9a77SSam Leffler 		sc->sc_stats.ast_tx_qstop++;
187110ad9a77SSam Leffler 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
187210ad9a77SSam Leffler 	}
187310ad9a77SSam Leffler 	ATH_TXBUF_UNLOCK(sc);
187410ad9a77SSam Leffler 	return bf;
187510ad9a77SSam Leffler }
187610ad9a77SSam Leffler 
18775591b213SSam Leffler static void
18785591b213SSam Leffler ath_start(struct ifnet *ifp)
18795591b213SSam Leffler {
18805591b213SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
18815591b213SSam Leffler 	struct ieee80211_node *ni;
18825591b213SSam Leffler 	struct ath_buf *bf;
188368e8e04eSSam Leffler 	struct mbuf *m, *next;
188468e8e04eSSam Leffler 	ath_bufhead frags;
18855591b213SSam Leffler 
188613f4c340SRobert Watson 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->sc_invalid)
18875591b213SSam Leffler 		return;
18885591b213SSam Leffler 	for (;;) {
18895591b213SSam Leffler 		/*
18905591b213SSam Leffler 		 * Grab a TX buffer and associated resources.
18915591b213SSam Leffler 		 */
189210ad9a77SSam Leffler 		bf = ath_getbuf(sc);
189310ad9a77SSam Leffler 		if (bf == NULL)
18945591b213SSam Leffler 			break;
18952b9411e2SSam Leffler 
1896b032f27cSSam Leffler 		IFQ_DEQUEUE(&ifp->if_snd, m);
1897b032f27cSSam Leffler 		if (m == NULL) {
1898b032f27cSSam Leffler 			ATH_TXBUF_LOCK(sc);
18996b349e5aSAdrian Chadd 			TAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
1900b032f27cSSam Leffler 			ATH_TXBUF_UNLOCK(sc);
1901b032f27cSSam Leffler 			break;
1902b032f27cSSam Leffler 		}
1903b032f27cSSam Leffler 		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
190468e8e04eSSam Leffler 		/*
190568e8e04eSSam Leffler 		 * Check for fragmentation.  If this frame
190668e8e04eSSam Leffler 		 * has been broken up verify we have enough
190768e8e04eSSam Leffler 		 * buffers to send all the fragments so all
190868e8e04eSSam Leffler 		 * go out or none...
190968e8e04eSSam Leffler 		 */
19106b349e5aSAdrian Chadd 		TAILQ_INIT(&frags);
191168e8e04eSSam Leffler 		if ((m->m_flags & M_FRAG) &&
191268e8e04eSSam Leffler 		    !ath_txfrag_setup(sc, &frags, m, ni)) {
191368e8e04eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_XMIT,
191468e8e04eSSam Leffler 			    "%s: out of txfrag buffers\n", __func__);
191536c6be9aSSam Leffler 			sc->sc_stats.ast_tx_nofrag++;
19169cb93076SSam Leffler 			ifp->if_oerrors++;
191768e8e04eSSam Leffler 			ath_freetx(m);
191868e8e04eSSam Leffler 			goto bad;
191968e8e04eSSam Leffler 		}
1920339ccfb3SSam Leffler 		ifp->if_opackets++;
192168e8e04eSSam Leffler 	nextfrag:
192268e8e04eSSam Leffler 		/*
192368e8e04eSSam Leffler 		 * Pass the frame to the h/w for transmission.
192468e8e04eSSam Leffler 		 * Fragmented frames have each frag chained together
192568e8e04eSSam Leffler 		 * with m_nextpkt.  We know there are sufficient ath_buf's
192668e8e04eSSam Leffler 		 * to send all the frags because of work done by
192768e8e04eSSam Leffler 		 * ath_txfrag_setup.  We leave m_nextpkt set while
192868e8e04eSSam Leffler 		 * calling ath_tx_start so it can use it to extend the
192968e8e04eSSam Leffler 		 * the tx duration to cover the subsequent frag and
193068e8e04eSSam Leffler 		 * so it can reclaim all the mbufs in case of an error;
193168e8e04eSSam Leffler 		 * ath_tx_start clears m_nextpkt once it commits to
193268e8e04eSSam Leffler 		 * handing the frame to the hardware.
193368e8e04eSSam Leffler 		 */
193468e8e04eSSam Leffler 		next = m->m_nextpkt;
19355591b213SSam Leffler 		if (ath_tx_start(sc, ni, bf, m)) {
19365591b213SSam Leffler 	bad:
19375591b213SSam Leffler 			ifp->if_oerrors++;
1938c42a7b7eSSam Leffler 	reclaim:
193968e8e04eSSam Leffler 			bf->bf_m = NULL;
194068e8e04eSSam Leffler 			bf->bf_node = NULL;
1941c42a7b7eSSam Leffler 			ATH_TXBUF_LOCK(sc);
19426b349e5aSAdrian Chadd 			TAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
194368e8e04eSSam Leffler 			ath_txfrag_cleanup(sc, &frags, ni);
1944c42a7b7eSSam Leffler 			ATH_TXBUF_UNLOCK(sc);
1945c42a7b7eSSam Leffler 			if (ni != NULL)
1946c42a7b7eSSam Leffler 				ieee80211_free_node(ni);
19475591b213SSam Leffler 			continue;
19485591b213SSam Leffler 		}
194968e8e04eSSam Leffler 		if (next != NULL) {
195068e8e04eSSam Leffler 			/*
195168e8e04eSSam Leffler 			 * Beware of state changing between frags.
195268e8e04eSSam Leffler 			 * XXX check sta power-save state?
195368e8e04eSSam Leffler 			 */
1954b032f27cSSam Leffler 			if (ni->ni_vap->iv_state != IEEE80211_S_RUN) {
195568e8e04eSSam Leffler 				DPRINTF(sc, ATH_DEBUG_XMIT,
195668e8e04eSSam Leffler 				    "%s: flush fragmented packet, state %s\n",
195768e8e04eSSam Leffler 				    __func__,
1958b032f27cSSam Leffler 				    ieee80211_state_name[ni->ni_vap->iv_state]);
195968e8e04eSSam Leffler 				ath_freetx(next);
196068e8e04eSSam Leffler 				goto reclaim;
196168e8e04eSSam Leffler 			}
196268e8e04eSSam Leffler 			m = next;
19636b349e5aSAdrian Chadd 			bf = TAILQ_FIRST(&frags);
196468e8e04eSSam Leffler 			KASSERT(bf != NULL, ("no buf for txfrag"));
19656b349e5aSAdrian Chadd 			TAILQ_REMOVE(&frags, bf, bf_list);
196668e8e04eSSam Leffler 			goto nextfrag;
196768e8e04eSSam Leffler 		}
19685591b213SSam Leffler 
19692e986da5SSam Leffler 		sc->sc_wd_timer = 5;
19705591b213SSam Leffler 	}
19715591b213SSam Leffler }
19725591b213SSam Leffler 
19735591b213SSam Leffler static int
19745591b213SSam Leffler ath_media_change(struct ifnet *ifp)
19755591b213SSam Leffler {
1976b032f27cSSam Leffler 	int error = ieee80211_media_change(ifp);
1977b032f27cSSam Leffler 	/* NB: only the fixed rate can change and that doesn't need a reset */
1978b032f27cSSam Leffler 	return (error == ENETRESET ? 0 : error);
19795591b213SSam Leffler }
19805591b213SSam Leffler 
1981c42a7b7eSSam Leffler /*
1982c42a7b7eSSam Leffler  * Block/unblock tx+rx processing while a key change is done.
1983c42a7b7eSSam Leffler  * We assume the caller serializes key management operations
1984c42a7b7eSSam Leffler  * so we only need to worry about synchronization with other
1985c42a7b7eSSam Leffler  * uses that originate in the driver.
1986c42a7b7eSSam Leffler  */
1987c42a7b7eSSam Leffler static void
1988b032f27cSSam Leffler ath_key_update_begin(struct ieee80211vap *vap)
1989c42a7b7eSSam Leffler {
1990b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ic->ic_ifp;
1991c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
1992c42a7b7eSSam Leffler 
1993c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
1994b032f27cSSam Leffler 	taskqueue_block(sc->sc_tq);
1995c42a7b7eSSam Leffler 	IF_LOCK(&ifp->if_snd);		/* NB: doesn't block mgmt frames */
1996c42a7b7eSSam Leffler }
1997c42a7b7eSSam Leffler 
1998c42a7b7eSSam Leffler static void
1999b032f27cSSam Leffler ath_key_update_end(struct ieee80211vap *vap)
2000c42a7b7eSSam Leffler {
2001b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ic->ic_ifp;
2002c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2003c42a7b7eSSam Leffler 
2004c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
2005c42a7b7eSSam Leffler 	IF_UNLOCK(&ifp->if_snd);
2006b032f27cSSam Leffler 	taskqueue_unblock(sc->sc_tq);
2007c42a7b7eSSam Leffler }
20085591b213SSam Leffler 
20094bc0e754SSam Leffler /*
20104bc0e754SSam Leffler  * Calculate the receive filter according to the
20114bc0e754SSam Leffler  * operating mode and state:
20124bc0e754SSam Leffler  *
20134bc0e754SSam Leffler  * o always accept unicast, broadcast, and multicast traffic
2014b032f27cSSam Leffler  * o accept PHY error frames when hardware doesn't have MIB support
2015411373ebSSam Leffler  *   to count and we need them for ANI (sta mode only until recently)
2016b032f27cSSam Leffler  *   and we are not scanning (ANI is disabled)
2017411373ebSSam Leffler  *   NB: older hal's add rx filter bits out of sight and we need to
2018411373ebSSam Leffler  *	 blindly preserve them
20194bc0e754SSam Leffler  * o probe request frames are accepted only when operating in
202059aa14a9SRui Paulo  *   hostap, adhoc, mesh, or monitor modes
2021b032f27cSSam Leffler  * o enable promiscuous mode
2022b032f27cSSam Leffler  *   - when in monitor mode
2023b032f27cSSam Leffler  *   - if interface marked PROMISC (assumes bridge setting is filtered)
20244bc0e754SSam Leffler  * o accept beacons:
20254bc0e754SSam Leffler  *   - when operating in station mode for collecting rssi data when
20264bc0e754SSam Leffler  *     the station is otherwise quiet, or
2027b032f27cSSam Leffler  *   - when operating in adhoc mode so the 802.11 layer creates
2028b032f27cSSam Leffler  *     node table entries for peers,
20294bc0e754SSam Leffler  *   - when scanning
2030b032f27cSSam Leffler  *   - when doing s/w beacon miss (e.g. for ap+sta)
2031b032f27cSSam Leffler  *   - when operating in ap mode in 11g to detect overlapping bss that
2032b032f27cSSam Leffler  *     require protection
203359aa14a9SRui Paulo  *   - when operating in mesh mode to detect neighbors
20346f48c956SSam Leffler  * o accept control frames:
20356f48c956SSam Leffler  *   - when in monitor mode
2036b032f27cSSam Leffler  * XXX HT protection for 11n
20374bc0e754SSam Leffler  */
20384bc0e754SSam Leffler static u_int32_t
203968e8e04eSSam Leffler ath_calcrxfilter(struct ath_softc *sc)
20404bc0e754SSam Leffler {
2041fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
2042b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
20434bc0e754SSam Leffler 	u_int32_t rfilt;
20444bc0e754SSam Leffler 
2045b032f27cSSam Leffler 	rfilt = HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST;
2046411373ebSSam Leffler 	if (!sc->sc_needmib && !sc->sc_scanning)
2047411373ebSSam Leffler 		rfilt |= HAL_RX_FILTER_PHYERR;
20484bc0e754SSam Leffler 	if (ic->ic_opmode != IEEE80211_M_STA)
20494bc0e754SSam Leffler 		rfilt |= HAL_RX_FILTER_PROBEREQ;
20505463c4a4SSam Leffler 	/* XXX ic->ic_monvaps != 0? */
2051b032f27cSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_MONITOR || (ifp->if_flags & IFF_PROMISC))
20524bc0e754SSam Leffler 		rfilt |= HAL_RX_FILTER_PROM;
20534bc0e754SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA ||
205447db982fSSam Leffler 	    ic->ic_opmode == IEEE80211_M_IBSS ||
2055b032f27cSSam Leffler 	    sc->sc_swbmiss || sc->sc_scanning)
2056b032f27cSSam Leffler 		rfilt |= HAL_RX_FILTER_BEACON;
2057b032f27cSSam Leffler 	/*
2058b032f27cSSam Leffler 	 * NB: We don't recalculate the rx filter when
2059b032f27cSSam Leffler 	 * ic_protmode changes; otherwise we could do
2060b032f27cSSam Leffler 	 * this only when ic_protmode != NONE.
2061b032f27cSSam Leffler 	 */
2062b032f27cSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
2063b032f27cSSam Leffler 	    IEEE80211_IS_CHAN_ANYG(ic->ic_curchan))
20644bc0e754SSam Leffler 		rfilt |= HAL_RX_FILTER_BEACON;
2065f378d4c8SAdrian Chadd 
2066f378d4c8SAdrian Chadd 	/*
20674aa18e9dSAdrian Chadd 	 * Enable hardware PS-POLL RX only for hostap mode;
2068f378d4c8SAdrian Chadd 	 * STA mode sends PS-POLL frames but never
20694aa18e9dSAdrian Chadd 	 * receives them.
2070f378d4c8SAdrian Chadd 	 */
2071dce0bccaSAdrian Chadd 	if (ath_hal_getcapability(sc->sc_ah, HAL_CAP_PSPOLL,
2072f378d4c8SAdrian Chadd 	    0, NULL) == HAL_OK &&
2073f378d4c8SAdrian Chadd 	    ic->ic_opmode == IEEE80211_M_HOSTAP)
2074f378d4c8SAdrian Chadd 		rfilt |= HAL_RX_FILTER_PSPOLL;
2075f378d4c8SAdrian Chadd 
2076fe0dd789SSam Leffler 	if (sc->sc_nmeshvaps) {
207759aa14a9SRui Paulo 		rfilt |= HAL_RX_FILTER_BEACON;
207859aa14a9SRui Paulo 		if (sc->sc_hasbmatch)
207959aa14a9SRui Paulo 			rfilt |= HAL_RX_FILTER_BSSID;
208059aa14a9SRui Paulo 		else
208159aa14a9SRui Paulo 			rfilt |= HAL_RX_FILTER_PROM;
208259aa14a9SRui Paulo 	}
20836f48c956SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
20846f48c956SSam Leffler 		rfilt |= HAL_RX_FILTER_CONTROL;
2085f378d4c8SAdrian Chadd 
2086f378d4c8SAdrian Chadd 	/*
2087f378d4c8SAdrian Chadd 	 * Enable RX of compressed BAR frames only when doing
2088f378d4c8SAdrian Chadd 	 * 802.11n. Required for A-MPDU.
2089f378d4c8SAdrian Chadd 	 */
2090a83df4d3SAdrian Chadd 	if (IEEE80211_IS_CHAN_HT(ic->ic_curchan))
2091a83df4d3SAdrian Chadd 		rfilt |= HAL_RX_FILTER_COMPBAR;
2092f378d4c8SAdrian Chadd 
2093b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x, %s if_flags 0x%x\n",
2094b032f27cSSam Leffler 	    __func__, rfilt, ieee80211_opmode_name[ic->ic_opmode], ifp->if_flags);
20954bc0e754SSam Leffler 	return rfilt;
2096b032f27cSSam Leffler }
2097b032f27cSSam Leffler 
2098b032f27cSSam Leffler static void
2099b032f27cSSam Leffler ath_update_promisc(struct ifnet *ifp)
2100b032f27cSSam Leffler {
2101b032f27cSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2102b032f27cSSam Leffler 	u_int32_t rfilt;
2103b032f27cSSam Leffler 
2104b032f27cSSam Leffler 	/* configure rx filter */
2105b032f27cSSam Leffler 	rfilt = ath_calcrxfilter(sc);
2106b032f27cSSam Leffler 	ath_hal_setrxfilter(sc->sc_ah, rfilt);
2107b032f27cSSam Leffler 
2108b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x\n", __func__, rfilt);
2109b032f27cSSam Leffler }
2110b032f27cSSam Leffler 
2111b032f27cSSam Leffler static void
2112b032f27cSSam Leffler ath_update_mcast(struct ifnet *ifp)
2113b032f27cSSam Leffler {
2114b032f27cSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2115b032f27cSSam Leffler 	u_int32_t mfilt[2];
2116b032f27cSSam Leffler 
2117b032f27cSSam Leffler 	/* calculate and install multicast filter */
2118b032f27cSSam Leffler 	if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
2119b032f27cSSam Leffler 		struct ifmultiaddr *ifma;
2120b032f27cSSam Leffler 		/*
2121b032f27cSSam Leffler 		 * Merge multicast addresses to form the hardware filter.
2122b032f27cSSam Leffler 		 */
2123b032f27cSSam Leffler 		mfilt[0] = mfilt[1] = 0;
2124eb956cd0SRobert Watson 		if_maddr_rlock(ifp);	/* XXX need some fiddling to remove? */
2125b032f27cSSam Leffler 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2126b032f27cSSam Leffler 			caddr_t dl;
2127b032f27cSSam Leffler 			u_int32_t val;
2128b032f27cSSam Leffler 			u_int8_t pos;
2129b032f27cSSam Leffler 
2130b032f27cSSam Leffler 			/* calculate XOR of eight 6bit values */
2131b032f27cSSam Leffler 			dl = LLADDR((struct sockaddr_dl *) ifma->ifma_addr);
2132b032f27cSSam Leffler 			val = LE_READ_4(dl + 0);
2133b032f27cSSam Leffler 			pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
2134b032f27cSSam Leffler 			val = LE_READ_4(dl + 3);
2135b032f27cSSam Leffler 			pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
2136b032f27cSSam Leffler 			pos &= 0x3f;
2137b032f27cSSam Leffler 			mfilt[pos / 32] |= (1 << (pos % 32));
2138b032f27cSSam Leffler 		}
2139eb956cd0SRobert Watson 		if_maddr_runlock(ifp);
2140b032f27cSSam Leffler 	} else
2141b032f27cSSam Leffler 		mfilt[0] = mfilt[1] = ~0;
2142b032f27cSSam Leffler 	ath_hal_setmcastfilter(sc->sc_ah, mfilt[0], mfilt[1]);
2143b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: MC filter %08x:%08x\n",
2144b032f27cSSam Leffler 		__func__, mfilt[0], mfilt[1]);
21454bc0e754SSam Leffler }
21464bc0e754SSam Leffler 
21475591b213SSam Leffler static void
21485591b213SSam Leffler ath_mode_init(struct ath_softc *sc)
21495591b213SSam Leffler {
2150fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
2151b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2152b032f27cSSam Leffler 	u_int32_t rfilt;
21535591b213SSam Leffler 
21544bc0e754SSam Leffler 	/* configure rx filter */
215568e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
21564bc0e754SSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
21574bc0e754SSam Leffler 
21585591b213SSam Leffler 	/* configure operational mode */
2159c42a7b7eSSam Leffler 	ath_hal_setopmode(ah);
2160c42a7b7eSSam Leffler 
216129aca940SSam Leffler 	/* handle any link-level address change */
216229aca940SSam Leffler 	ath_hal_setmac(ah, IF_LLADDR(ifp));
21635591b213SSam Leffler 
21645591b213SSam Leffler 	/* calculate and install multicast filter */
2165b032f27cSSam Leffler 	ath_update_mcast(ifp);
21665591b213SSam Leffler }
21675591b213SSam Leffler 
2168c42a7b7eSSam Leffler /*
2169c42a7b7eSSam Leffler  * Set the slot time based on the current setting.
2170c42a7b7eSSam Leffler  */
2171c42a7b7eSSam Leffler static void
2172c42a7b7eSSam Leffler ath_setslottime(struct ath_softc *sc)
2173c42a7b7eSSam Leffler {
2174b032f27cSSam Leffler 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2175c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2176aaa70f2fSSam Leffler 	u_int usec;
2177c42a7b7eSSam Leffler 
2178aaa70f2fSSam Leffler 	if (IEEE80211_IS_CHAN_HALF(ic->ic_curchan))
2179aaa70f2fSSam Leffler 		usec = 13;
2180aaa70f2fSSam Leffler 	else if (IEEE80211_IS_CHAN_QUARTER(ic->ic_curchan))
2181aaa70f2fSSam Leffler 		usec = 21;
2182724c193aSSam Leffler 	else if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan)) {
2183724c193aSSam Leffler 		/* honor short/long slot time only in 11g */
2184724c193aSSam Leffler 		/* XXX shouldn't honor on pure g or turbo g channel */
2185724c193aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
2186aaa70f2fSSam Leffler 			usec = HAL_SLOT_TIME_9;
2187aaa70f2fSSam Leffler 		else
2188aaa70f2fSSam Leffler 			usec = HAL_SLOT_TIME_20;
2189724c193aSSam Leffler 	} else
2190724c193aSSam Leffler 		usec = HAL_SLOT_TIME_9;
2191aaa70f2fSSam Leffler 
2192aaa70f2fSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET,
2193aaa70f2fSSam Leffler 	    "%s: chan %u MHz flags 0x%x %s slot, %u usec\n",
2194aaa70f2fSSam Leffler 	    __func__, ic->ic_curchan->ic_freq, ic->ic_curchan->ic_flags,
2195aaa70f2fSSam Leffler 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", usec);
2196aaa70f2fSSam Leffler 
2197aaa70f2fSSam Leffler 	ath_hal_setslottime(ah, usec);
2198c42a7b7eSSam Leffler 	sc->sc_updateslot = OK;
2199c42a7b7eSSam Leffler }
2200c42a7b7eSSam Leffler 
2201c42a7b7eSSam Leffler /*
2202c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update the
2203c42a7b7eSSam Leffler  * slot time based on the current setting.
2204c42a7b7eSSam Leffler  */
2205c42a7b7eSSam Leffler static void
2206c42a7b7eSSam Leffler ath_updateslot(struct ifnet *ifp)
2207c42a7b7eSSam Leffler {
2208c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2209b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
2210c42a7b7eSSam Leffler 
2211c42a7b7eSSam Leffler 	/*
2212c42a7b7eSSam Leffler 	 * When not coordinating the BSS, change the hardware
2213c42a7b7eSSam Leffler 	 * immediately.  For other operation we defer the change
2214c42a7b7eSSam Leffler 	 * until beacon updates have propagated to the stations.
2215c42a7b7eSSam Leffler 	 */
221659aa14a9SRui Paulo 	if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
221759aa14a9SRui Paulo 	    ic->ic_opmode == IEEE80211_M_MBSS)
2218c42a7b7eSSam Leffler 		sc->sc_updateslot = UPDATE;
2219c42a7b7eSSam Leffler 	else
2220c42a7b7eSSam Leffler 		ath_setslottime(sc);
2221c42a7b7eSSam Leffler }
2222c42a7b7eSSam Leffler 
2223c42a7b7eSSam Leffler /*
222480d2765fSSam Leffler  * Setup a h/w transmit queue for beacons.
222580d2765fSSam Leffler  */
222680d2765fSSam Leffler static int
222780d2765fSSam Leffler ath_beaconq_setup(struct ath_hal *ah)
222880d2765fSSam Leffler {
222980d2765fSSam Leffler 	HAL_TXQ_INFO qi;
223080d2765fSSam Leffler 
223180d2765fSSam Leffler 	memset(&qi, 0, sizeof(qi));
223280d2765fSSam Leffler 	qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
223380d2765fSSam Leffler 	qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
223480d2765fSSam Leffler 	qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
22350f2e86fbSSam Leffler 	/* NB: for dynamic turbo, don't enable any other interrupts */
2236bd5a9920SSam Leffler 	qi.tqi_qflags = HAL_TXQ_TXDESCINT_ENABLE;
223780d2765fSSam Leffler 	return ath_hal_setuptxqueue(ah, HAL_TX_QUEUE_BEACON, &qi);
223880d2765fSSam Leffler }
223980d2765fSSam Leffler 
224080d2765fSSam Leffler /*
22410f2e86fbSSam Leffler  * Setup the transmit queue parameters for the beacon queue.
22420f2e86fbSSam Leffler  */
22430f2e86fbSSam Leffler static int
22440f2e86fbSSam Leffler ath_beaconq_config(struct ath_softc *sc)
22450f2e86fbSSam Leffler {
22460f2e86fbSSam Leffler #define	ATH_EXPONENT_TO_VALUE(v)	((1<<(v))-1)
2247b032f27cSSam Leffler 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
22480f2e86fbSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
22490f2e86fbSSam Leffler 	HAL_TXQ_INFO qi;
22500f2e86fbSSam Leffler 
22510f2e86fbSSam Leffler 	ath_hal_gettxqueueprops(ah, sc->sc_bhalq, &qi);
225259aa14a9SRui Paulo 	if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
225359aa14a9SRui Paulo 	    ic->ic_opmode == IEEE80211_M_MBSS) {
22540f2e86fbSSam Leffler 		/*
22550f2e86fbSSam Leffler 		 * Always burst out beacon and CAB traffic.
22560f2e86fbSSam Leffler 		 */
22570f2e86fbSSam Leffler 		qi.tqi_aifs = ATH_BEACON_AIFS_DEFAULT;
22580f2e86fbSSam Leffler 		qi.tqi_cwmin = ATH_BEACON_CWMIN_DEFAULT;
22590f2e86fbSSam Leffler 		qi.tqi_cwmax = ATH_BEACON_CWMAX_DEFAULT;
22600f2e86fbSSam Leffler 	} else {
22610f2e86fbSSam Leffler 		struct wmeParams *wmep =
22620f2e86fbSSam Leffler 			&ic->ic_wme.wme_chanParams.cap_wmeParams[WME_AC_BE];
22630f2e86fbSSam Leffler 		/*
22640f2e86fbSSam Leffler 		 * Adhoc mode; important thing is to use 2x cwmin.
22650f2e86fbSSam Leffler 		 */
22660f2e86fbSSam Leffler 		qi.tqi_aifs = wmep->wmep_aifsn;
22670f2e86fbSSam Leffler 		qi.tqi_cwmin = 2*ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
22680f2e86fbSSam Leffler 		qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
22690f2e86fbSSam Leffler 	}
22700f2e86fbSSam Leffler 
22710f2e86fbSSam Leffler 	if (!ath_hal_settxqueueprops(ah, sc->sc_bhalq, &qi)) {
22720f2e86fbSSam Leffler 		device_printf(sc->sc_dev, "unable to update parameters for "
22730f2e86fbSSam Leffler 			"beacon hardware queue!\n");
22740f2e86fbSSam Leffler 		return 0;
22750f2e86fbSSam Leffler 	} else {
22760f2e86fbSSam Leffler 		ath_hal_resettxqueue(ah, sc->sc_bhalq); /* push to h/w */
22770f2e86fbSSam Leffler 		return 1;
22780f2e86fbSSam Leffler 	}
22790f2e86fbSSam Leffler #undef ATH_EXPONENT_TO_VALUE
22800f2e86fbSSam Leffler }
22810f2e86fbSSam Leffler 
22820f2e86fbSSam Leffler /*
2283c42a7b7eSSam Leffler  * Allocate and setup an initial beacon frame.
2284c42a7b7eSSam Leffler  */
22855591b213SSam Leffler static int
22865591b213SSam Leffler ath_beacon_alloc(struct ath_softc *sc, struct ieee80211_node *ni)
22875591b213SSam Leffler {
2288b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
2289b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
22905591b213SSam Leffler 	struct ath_buf *bf;
22915591b213SSam Leffler 	struct mbuf *m;
2292c42a7b7eSSam Leffler 	int error;
22935591b213SSam Leffler 
2294b032f27cSSam Leffler 	bf = avp->av_bcbuf;
2295b032f27cSSam Leffler 	if (bf->bf_m != NULL) {
2296b032f27cSSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
2297b032f27cSSam Leffler 		m_freem(bf->bf_m);
2298b032f27cSSam Leffler 		bf->bf_m = NULL;
2299c42a7b7eSSam Leffler 	}
2300b032f27cSSam Leffler 	if (bf->bf_node != NULL) {
2301b032f27cSSam Leffler 		ieee80211_free_node(bf->bf_node);
2302b032f27cSSam Leffler 		bf->bf_node = NULL;
2303b032f27cSSam Leffler 	}
2304b032f27cSSam Leffler 
23055591b213SSam Leffler 	/*
23065591b213SSam Leffler 	 * NB: the beacon data buffer must be 32-bit aligned;
23075591b213SSam Leffler 	 * we assume the mbuf routines will return us something
23085591b213SSam Leffler 	 * with this alignment (perhaps should assert).
23095591b213SSam Leffler 	 */
2310b032f27cSSam Leffler 	m = ieee80211_beacon_alloc(ni, &avp->av_boff);
23115591b213SSam Leffler 	if (m == NULL) {
2312b032f27cSSam Leffler 		device_printf(sc->sc_dev, "%s: cannot get mbuf\n", __func__);
23135591b213SSam Leffler 		sc->sc_stats.ast_be_nombuf++;
23145591b213SSam Leffler 		return ENOMEM;
23155591b213SSam Leffler 	}
2316f9e6219bSSam Leffler 	error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m,
2317f9e6219bSSam Leffler 				     bf->bf_segs, &bf->bf_nseg,
23185591b213SSam Leffler 				     BUS_DMA_NOWAIT);
2319b032f27cSSam Leffler 	if (error != 0) {
2320b032f27cSSam Leffler 		device_printf(sc->sc_dev,
2321b032f27cSSam Leffler 		    "%s: cannot map mbuf, bus_dmamap_load_mbuf_sg returns %d\n",
2322b032f27cSSam Leffler 		    __func__, error);
2323b032f27cSSam Leffler 		m_freem(m);
2324b032f27cSSam Leffler 		return error;
2325b032f27cSSam Leffler 	}
2326b032f27cSSam Leffler 
2327b032f27cSSam Leffler 	/*
2328b032f27cSSam Leffler 	 * Calculate a TSF adjustment factor required for staggered
2329b032f27cSSam Leffler 	 * beacons.  Note that we assume the format of the beacon
2330b032f27cSSam Leffler 	 * frame leaves the tstamp field immediately following the
2331b032f27cSSam Leffler 	 * header.
2332b032f27cSSam Leffler 	 */
2333b032f27cSSam Leffler 	if (sc->sc_stagbeacons && avp->av_bslot > 0) {
2334b032f27cSSam Leffler 		uint64_t tsfadjust;
2335b032f27cSSam Leffler 		struct ieee80211_frame *wh;
2336b032f27cSSam Leffler 
2337b032f27cSSam Leffler 		/*
2338b032f27cSSam Leffler 		 * The beacon interval is in TU's; the TSF is in usecs.
2339b032f27cSSam Leffler 		 * We figure out how many TU's to add to align the timestamp
2340b032f27cSSam Leffler 		 * then convert to TSF units and handle byte swapping before
2341b032f27cSSam Leffler 		 * inserting it in the frame.  The hardware will then add this
2342b032f27cSSam Leffler 		 * each time a beacon frame is sent.  Note that we align vap's
2343b032f27cSSam Leffler 		 * 1..N and leave vap 0 untouched.  This means vap 0 has a
2344b032f27cSSam Leffler 		 * timestamp in one beacon interval while the others get a
2345b032f27cSSam Leffler 		 * timstamp aligned to the next interval.
2346b032f27cSSam Leffler 		 */
2347b032f27cSSam Leffler 		tsfadjust = ni->ni_intval *
2348b032f27cSSam Leffler 		    (ATH_BCBUF - avp->av_bslot) / ATH_BCBUF;
2349b032f27cSSam Leffler 		tsfadjust = htole64(tsfadjust << 10);	/* TU -> TSF */
2350b032f27cSSam Leffler 
2351b032f27cSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
2352b032f27cSSam Leffler 		    "%s: %s beacons bslot %d intval %u tsfadjust %llu\n",
2353b032f27cSSam Leffler 		    __func__, sc->sc_stagbeacons ? "stagger" : "burst",
23543627e321SSam Leffler 		    avp->av_bslot, ni->ni_intval,
23553627e321SSam Leffler 		    (long long unsigned) le64toh(tsfadjust));
2356b032f27cSSam Leffler 
2357b032f27cSSam Leffler 		wh = mtod(m, struct ieee80211_frame *);
2358b032f27cSSam Leffler 		memcpy(&wh[1], &tsfadjust, sizeof(tsfadjust));
2359b032f27cSSam Leffler 	}
2360c42a7b7eSSam Leffler 	bf->bf_m = m;
2361f818612bSSam Leffler 	bf->bf_node = ieee80211_ref_node(ni);
2362b032f27cSSam Leffler 
2363b032f27cSSam Leffler 	return 0;
23645591b213SSam Leffler }
2365c42a7b7eSSam Leffler 
2366c42a7b7eSSam Leffler /*
2367c42a7b7eSSam Leffler  * Setup the beacon frame for transmit.
2368c42a7b7eSSam Leffler  */
2369c42a7b7eSSam Leffler static void
2370c42a7b7eSSam Leffler ath_beacon_setup(struct ath_softc *sc, struct ath_buf *bf)
2371c42a7b7eSSam Leffler {
2372c42a7b7eSSam Leffler #define	USE_SHPREAMBLE(_ic) \
2373c42a7b7eSSam Leffler 	(((_ic)->ic_flags & (IEEE80211_F_SHPREAMBLE | IEEE80211_F_USEBARKER))\
2374c42a7b7eSSam Leffler 		== IEEE80211_F_SHPREAMBLE)
2375c42a7b7eSSam Leffler 	struct ieee80211_node *ni = bf->bf_node;
2376c42a7b7eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2377c42a7b7eSSam Leffler 	struct mbuf *m = bf->bf_m;
2378c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2379c42a7b7eSSam Leffler 	struct ath_desc *ds;
2380c42a7b7eSSam Leffler 	int flags, antenna;
238155f63772SSam Leffler 	const HAL_RATE_TABLE *rt;
238255f63772SSam Leffler 	u_int8_t rix, rate;
2383c42a7b7eSSam Leffler 
23844a3ac3fcSSam Leffler 	DPRINTF(sc, ATH_DEBUG_BEACON_PROC, "%s: m %p len %u\n",
2385c42a7b7eSSam Leffler 		__func__, m, m->m_len);
23865591b213SSam Leffler 
23875591b213SSam Leffler 	/* setup descriptors */
23885591b213SSam Leffler 	ds = bf->bf_desc;
23895591b213SSam Leffler 
2390c42a7b7eSSam Leffler 	flags = HAL_TXDESC_NOACK;
2391c42a7b7eSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_hasveol) {
2392c42a7b7eSSam Leffler 		ds->ds_link = bf->bf_daddr;	/* self-linked */
2393c42a7b7eSSam Leffler 		flags |= HAL_TXDESC_VEOL;
2394c42a7b7eSSam Leffler 		/*
2395c42a7b7eSSam Leffler 		 * Let hardware handle antenna switching.
2396c42a7b7eSSam Leffler 		 */
23974866e6c2SSam Leffler 		antenna = sc->sc_txantenna;
2398c42a7b7eSSam Leffler 	} else {
23995591b213SSam Leffler 		ds->ds_link = 0;
2400c42a7b7eSSam Leffler 		/*
2401c42a7b7eSSam Leffler 		 * Switch antenna every 4 beacons.
2402c42a7b7eSSam Leffler 		 * XXX assumes two antenna
2403c42a7b7eSSam Leffler 		 */
2404b032f27cSSam Leffler 		if (sc->sc_txantenna != 0)
2405b032f27cSSam Leffler 			antenna = sc->sc_txantenna;
2406b032f27cSSam Leffler 		else if (sc->sc_stagbeacons && sc->sc_nbcnvaps != 0)
2407b032f27cSSam Leffler 			antenna = ((sc->sc_stats.ast_be_xmit / sc->sc_nbcnvaps) & 4 ? 2 : 1);
2408b032f27cSSam Leffler 		else
2409b032f27cSSam Leffler 			antenna = (sc->sc_stats.ast_be_xmit & 4 ? 2 : 1);
2410c42a7b7eSSam Leffler 	}
2411c42a7b7eSSam Leffler 
2412c42a7b7eSSam Leffler 	KASSERT(bf->bf_nseg == 1,
2413c42a7b7eSSam Leffler 		("multi-segment beacon frame; nseg %u", bf->bf_nseg));
24145591b213SSam Leffler 	ds->ds_data = bf->bf_segs[0].ds_addr;
24155591b213SSam Leffler 	/*
24165591b213SSam Leffler 	 * Calculate rate code.
24175591b213SSam Leffler 	 * XXX everything at min xmit rate
24185591b213SSam Leffler 	 */
2419b032f27cSSam Leffler 	rix = 0;
242055f63772SSam Leffler 	rt = sc->sc_currates;
242155f63772SSam Leffler 	rate = rt->info[rix].rateCode;
2422c42a7b7eSSam Leffler 	if (USE_SHPREAMBLE(ic))
242355f63772SSam Leffler 		rate |= rt->info[rix].shortPreamble;
24245591b213SSam Leffler 	ath_hal_setuptxdesc(ah, ds
2425c42a7b7eSSam Leffler 		, m->m_len + IEEE80211_CRC_LEN	/* frame length */
24265591b213SSam Leffler 		, sizeof(struct ieee80211_frame)/* header length */
24275591b213SSam Leffler 		, HAL_PKT_TYPE_BEACON		/* Atheros packet type */
2428c42a7b7eSSam Leffler 		, ni->ni_txpower		/* txpower XXX */
24295591b213SSam Leffler 		, rate, 1			/* series 0 rate/tries */
24305591b213SSam Leffler 		, HAL_TXKEYIX_INVALID		/* no encryption */
2431c42a7b7eSSam Leffler 		, antenna			/* antenna mode */
2432c42a7b7eSSam Leffler 		, flags				/* no ack, veol for beacons */
24335591b213SSam Leffler 		, 0				/* rts/cts rate */
24345591b213SSam Leffler 		, 0				/* rts/cts duration */
24355591b213SSam Leffler 	);
24365591b213SSam Leffler 	/* NB: beacon's BufLen must be a multiple of 4 bytes */
24375591b213SSam Leffler 	ath_hal_filltxdesc(ah, ds
2438c42a7b7eSSam Leffler 		, roundup(m->m_len, 4)		/* buffer length */
24395591b213SSam Leffler 		, AH_TRUE			/* first segment */
24405591b213SSam Leffler 		, AH_TRUE			/* last segment */
2441c42a7b7eSSam Leffler 		, ds				/* first descriptor */
24425591b213SSam Leffler 	);
2443b032f27cSSam Leffler #if 0
2444b032f27cSSam Leffler 	ath_desc_swap(ds);
2445b032f27cSSam Leffler #endif
2446c42a7b7eSSam Leffler #undef USE_SHPREAMBLE
24475591b213SSam Leffler }
24485591b213SSam Leffler 
2449b105a069SSam Leffler static void
2450b032f27cSSam Leffler ath_beacon_update(struct ieee80211vap *vap, int item)
2451b105a069SSam Leffler {
2452b032f27cSSam Leffler 	struct ieee80211_beacon_offsets *bo = &ATH_VAP(vap)->av_boff;
2453b105a069SSam Leffler 
2454b105a069SSam Leffler 	setbit(bo->bo_flags, item);
2455b105a069SSam Leffler }
2456b105a069SSam Leffler 
2457c42a7b7eSSam Leffler /*
2458622b3fd2SSam Leffler  * Append the contents of src to dst; both queues
2459622b3fd2SSam Leffler  * are assumed to be locked.
2460622b3fd2SSam Leffler  */
2461622b3fd2SSam Leffler static void
2462622b3fd2SSam Leffler ath_txqmove(struct ath_txq *dst, struct ath_txq *src)
2463622b3fd2SSam Leffler {
24646b349e5aSAdrian Chadd 	TAILQ_CONCAT(&dst->axq_q, &src->axq_q, bf_list);
2465622b3fd2SSam Leffler 	dst->axq_link = src->axq_link;
2466622b3fd2SSam Leffler 	src->axq_link = NULL;
2467622b3fd2SSam Leffler 	dst->axq_depth += src->axq_depth;
2468622b3fd2SSam Leffler 	src->axq_depth = 0;
2469622b3fd2SSam Leffler }
2470622b3fd2SSam Leffler 
2471622b3fd2SSam Leffler /*
2472c42a7b7eSSam Leffler  * Transmit a beacon frame at SWBA.  Dynamic updates to the
2473c42a7b7eSSam Leffler  * frame contents are done as needed and the slot time is
2474c42a7b7eSSam Leffler  * also adjusted based on current state.
2475c42a7b7eSSam Leffler  */
24765591b213SSam Leffler static void
24775591b213SSam Leffler ath_beacon_proc(void *arg, int pending)
24785591b213SSam Leffler {
24795591b213SSam Leffler 	struct ath_softc *sc = arg;
24805591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2481b032f27cSSam Leffler 	struct ieee80211vap *vap;
2482b032f27cSSam Leffler 	struct ath_buf *bf;
2483b032f27cSSam Leffler 	int slot, otherant;
2484b032f27cSSam Leffler 	uint32_t bfaddr;
24855591b213SSam Leffler 
2486c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_BEACON_PROC, "%s: pending %u\n",
2487c42a7b7eSSam Leffler 		__func__, pending);
2488c42a7b7eSSam Leffler 	/*
2489c42a7b7eSSam Leffler 	 * Check if the previous beacon has gone out.  If
2490c66c48cbSSam Leffler 	 * not don't try to post another, skip this period
2491c66c48cbSSam Leffler 	 * and wait for the next.  Missed beacons indicate
2492c66c48cbSSam Leffler 	 * a problem and should not occur.  If we miss too
2493c66c48cbSSam Leffler 	 * many consecutive beacons reset the device.
2494c42a7b7eSSam Leffler 	 */
2495c42a7b7eSSam Leffler 	if (ath_hal_numtxpending(ah, sc->sc_bhalq) != 0) {
2496c42a7b7eSSam Leffler 		sc->sc_bmisscount++;
24977ec4e6b8SAdrian Chadd 		sc->sc_stats.ast_be_missed++;
24984a3ac3fcSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
2499c42a7b7eSSam Leffler 			"%s: missed %u consecutive beacons\n",
2500c42a7b7eSSam Leffler 			__func__, sc->sc_bmisscount);
2501a32ac9d3SSam Leffler 		if (sc->sc_bmisscount >= ath_bstuck_threshold)
25020bbf5441SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_bstucktask);
2503c42a7b7eSSam Leffler 		return;
2504c42a7b7eSSam Leffler 	}
2505c42a7b7eSSam Leffler 	if (sc->sc_bmisscount != 0) {
2506c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
2507c42a7b7eSSam Leffler 			"%s: resume beacon xmit after %u misses\n",
2508c42a7b7eSSam Leffler 			__func__, sc->sc_bmisscount);
2509c42a7b7eSSam Leffler 		sc->sc_bmisscount = 0;
2510c42a7b7eSSam Leffler 	}
2511c42a7b7eSSam Leffler 
2512b032f27cSSam Leffler 	if (sc->sc_stagbeacons) {			/* staggered beacons */
2513b032f27cSSam Leffler 		struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2514b032f27cSSam Leffler 		uint32_t tsftu;
2515b032f27cSSam Leffler 
2516b032f27cSSam Leffler 		tsftu = ath_hal_gettsf32(ah) >> 10;
2517b032f27cSSam Leffler 		/* XXX lintval */
2518b032f27cSSam Leffler 		slot = ((tsftu % ic->ic_lintval) * ATH_BCBUF) / ic->ic_lintval;
2519b032f27cSSam Leffler 		vap = sc->sc_bslot[(slot+1) % ATH_BCBUF];
2520b032f27cSSam Leffler 		bfaddr = 0;
2521309a3e45SSam Leffler 		if (vap != NULL && vap->iv_state >= IEEE80211_S_RUN) {
2522b032f27cSSam Leffler 			bf = ath_beacon_generate(sc, vap);
2523b032f27cSSam Leffler 			if (bf != NULL)
2524b032f27cSSam Leffler 				bfaddr = bf->bf_daddr;
2525b032f27cSSam Leffler 		}
2526b032f27cSSam Leffler 	} else {					/* burst'd beacons */
2527b032f27cSSam Leffler 		uint32_t *bflink = &bfaddr;
2528b032f27cSSam Leffler 
2529b032f27cSSam Leffler 		for (slot = 0; slot < ATH_BCBUF; slot++) {
2530b032f27cSSam Leffler 			vap = sc->sc_bslot[slot];
2531309a3e45SSam Leffler 			if (vap != NULL && vap->iv_state >= IEEE80211_S_RUN) {
2532b032f27cSSam Leffler 				bf = ath_beacon_generate(sc, vap);
2533b032f27cSSam Leffler 				if (bf != NULL) {
2534b032f27cSSam Leffler 					*bflink = bf->bf_daddr;
2535b032f27cSSam Leffler 					bflink = &bf->bf_desc->ds_link;
2536c42a7b7eSSam Leffler 				}
2537c42a7b7eSSam Leffler 			}
2538b032f27cSSam Leffler 		}
2539b032f27cSSam Leffler 		*bflink = 0;				/* terminate list */
2540622b3fd2SSam Leffler 	}
2541c42a7b7eSSam Leffler 
2542c42a7b7eSSam Leffler 	/*
2543c42a7b7eSSam Leffler 	 * Handle slot time change when a non-ERP station joins/leaves
2544c42a7b7eSSam Leffler 	 * an 11g network.  The 802.11 layer notifies us via callback,
2545c42a7b7eSSam Leffler 	 * we mark updateslot, then wait one beacon before effecting
2546c42a7b7eSSam Leffler 	 * the change.  This gives associated stations at least one
2547c42a7b7eSSam Leffler 	 * beacon interval to note the state change.
2548c42a7b7eSSam Leffler 	 */
2549c42a7b7eSSam Leffler 	/* XXX locking */
2550b032f27cSSam Leffler 	if (sc->sc_updateslot == UPDATE) {
2551c42a7b7eSSam Leffler 		sc->sc_updateslot = COMMIT;	/* commit next beacon */
2552b032f27cSSam Leffler 		sc->sc_slotupdate = slot;
2553b032f27cSSam Leffler 	} else if (sc->sc_updateslot == COMMIT && sc->sc_slotupdate == slot)
2554c42a7b7eSSam Leffler 		ath_setslottime(sc);		/* commit change to h/w */
2555c42a7b7eSSam Leffler 
2556c42a7b7eSSam Leffler 	/*
2557c42a7b7eSSam Leffler 	 * Check recent per-antenna transmit statistics and flip
2558c42a7b7eSSam Leffler 	 * the default antenna if noticeably more frames went out
2559c42a7b7eSSam Leffler 	 * on the non-default antenna.
2560c42a7b7eSSam Leffler 	 * XXX assumes 2 anntenae
2561c42a7b7eSSam Leffler 	 */
2562b032f27cSSam Leffler 	if (!sc->sc_diversity && (!sc->sc_stagbeacons || slot == 0)) {
2563c42a7b7eSSam Leffler 		otherant = sc->sc_defant & 1 ? 2 : 1;
2564c42a7b7eSSam Leffler 		if (sc->sc_ant_tx[otherant] > sc->sc_ant_tx[sc->sc_defant] + 2)
2565c42a7b7eSSam Leffler 			ath_setdefantenna(sc, otherant);
2566c42a7b7eSSam Leffler 		sc->sc_ant_tx[1] = sc->sc_ant_tx[2] = 0;
2567b032f27cSSam Leffler 	}
2568c42a7b7eSSam Leffler 
2569b032f27cSSam Leffler 	if (bfaddr != 0) {
2570c42a7b7eSSam Leffler 		/*
2571c42a7b7eSSam Leffler 		 * Stop any current dma and put the new frame on the queue.
2572c42a7b7eSSam Leffler 		 * This should never fail since we check above that no frames
2573c42a7b7eSSam Leffler 		 * are still pending on the queue.
2574c42a7b7eSSam Leffler 		 */
25755591b213SSam Leffler 		if (!ath_hal_stoptxdma(ah, sc->sc_bhalq)) {
2576c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
2577c42a7b7eSSam Leffler 				"%s: beacon queue %u did not stop?\n",
2578c42a7b7eSSam Leffler 				__func__, sc->sc_bhalq);
25795591b213SSam Leffler 		}
2580b032f27cSSam Leffler 		/* NB: cabq traffic should already be queued and primed */
2581b032f27cSSam Leffler 		ath_hal_puttxbuf(ah, sc->sc_bhalq, bfaddr);
2582b032f27cSSam Leffler 		ath_hal_txstart(ah, sc->sc_bhalq);
2583b032f27cSSam Leffler 
2584b032f27cSSam Leffler 		sc->sc_stats.ast_be_xmit++;
2585b032f27cSSam Leffler 	}
2586b032f27cSSam Leffler }
2587b032f27cSSam Leffler 
2588b032f27cSSam Leffler static struct ath_buf *
2589b032f27cSSam Leffler ath_beacon_generate(struct ath_softc *sc, struct ieee80211vap *vap)
2590b032f27cSSam Leffler {
2591b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
2592b032f27cSSam Leffler 	struct ath_txq *cabq = sc->sc_cabq;
2593b032f27cSSam Leffler 	struct ath_buf *bf;
2594b032f27cSSam Leffler 	struct mbuf *m;
2595b032f27cSSam Leffler 	int nmcastq, error;
2596b032f27cSSam Leffler 
2597309a3e45SSam Leffler 	KASSERT(vap->iv_state >= IEEE80211_S_RUN,
2598b032f27cSSam Leffler 	    ("not running, state %d", vap->iv_state));
2599b032f27cSSam Leffler 	KASSERT(avp->av_bcbuf != NULL, ("no beacon buffer"));
2600b032f27cSSam Leffler 
2601b032f27cSSam Leffler 	/*
2602b032f27cSSam Leffler 	 * Update dynamic beacon contents.  If this returns
2603b032f27cSSam Leffler 	 * non-zero then we need to remap the memory because
2604b032f27cSSam Leffler 	 * the beacon frame changed size (probably because
2605b032f27cSSam Leffler 	 * of the TIM bitmap).
2606b032f27cSSam Leffler 	 */
2607b032f27cSSam Leffler 	bf = avp->av_bcbuf;
2608b032f27cSSam Leffler 	m = bf->bf_m;
2609b032f27cSSam Leffler 	nmcastq = avp->av_mcastq.axq_depth;
2610b032f27cSSam Leffler 	if (ieee80211_beacon_update(bf->bf_node, &avp->av_boff, m, nmcastq)) {
2611b032f27cSSam Leffler 		/* XXX too conservative? */
2612b032f27cSSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
2613b032f27cSSam Leffler 		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m,
2614b032f27cSSam Leffler 					     bf->bf_segs, &bf->bf_nseg,
2615b032f27cSSam Leffler 					     BUS_DMA_NOWAIT);
2616b032f27cSSam Leffler 		if (error != 0) {
2617b032f27cSSam Leffler 			if_printf(vap->iv_ifp,
2618b032f27cSSam Leffler 			    "%s: bus_dmamap_load_mbuf_sg failed, error %u\n",
2619b032f27cSSam Leffler 			    __func__, error);
2620b032f27cSSam Leffler 			return NULL;
2621b032f27cSSam Leffler 		}
2622b032f27cSSam Leffler 	}
2623b032f27cSSam Leffler 	if ((avp->av_boff.bo_tim[4] & 1) && cabq->axq_depth) {
2624b032f27cSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
2625b032f27cSSam Leffler 		    "%s: cabq did not drain, mcastq %u cabq %u\n",
2626b032f27cSSam Leffler 		    __func__, nmcastq, cabq->axq_depth);
2627b032f27cSSam Leffler 		sc->sc_stats.ast_cabq_busy++;
2628b032f27cSSam Leffler 		if (sc->sc_nvaps > 1 && sc->sc_stagbeacons) {
2629b032f27cSSam Leffler 			/*
2630b032f27cSSam Leffler 			 * CABQ traffic from a previous vap is still pending.
2631b032f27cSSam Leffler 			 * We must drain the q before this beacon frame goes
2632b032f27cSSam Leffler 			 * out as otherwise this vap's stations will get cab
2633b032f27cSSam Leffler 			 * frames from a different vap.
2634b032f27cSSam Leffler 			 * XXX could be slow causing us to miss DBA
2635b032f27cSSam Leffler 			 */
2636b032f27cSSam Leffler 			ath_tx_draintxq(sc, cabq);
2637b032f27cSSam Leffler 		}
2638b032f27cSSam Leffler 	}
2639b032f27cSSam Leffler 	ath_beacon_setup(sc, bf);
26405591b213SSam Leffler 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
26415591b213SSam Leffler 
2642c42a7b7eSSam Leffler 	/*
2643c42a7b7eSSam Leffler 	 * Enable the CAB queue before the beacon queue to
2644c42a7b7eSSam Leffler 	 * insure cab frames are triggered by this beacon.
2645c42a7b7eSSam Leffler 	 */
2646b032f27cSSam Leffler 	if (avp->av_boff.bo_tim[4] & 1) {
2647b032f27cSSam Leffler 		struct ath_hal *ah = sc->sc_ah;
2648b032f27cSSam Leffler 
2649f3af83f7SSam Leffler 		/* NB: only at DTIM */
2650622b3fd2SSam Leffler 		ATH_TXQ_LOCK(cabq);
2651b032f27cSSam Leffler 		ATH_TXQ_LOCK(&avp->av_mcastq);
2652622b3fd2SSam Leffler 		if (nmcastq) {
2653622b3fd2SSam Leffler 			struct ath_buf *bfm;
2654622b3fd2SSam Leffler 
2655622b3fd2SSam Leffler 			/*
2656622b3fd2SSam Leffler 			 * Move frames from the s/w mcast q to the h/w cab q.
2657b032f27cSSam Leffler 			 * XXX MORE_DATA bit
2658622b3fd2SSam Leffler 			 */
26596b349e5aSAdrian Chadd 			bfm = TAILQ_FIRST(&avp->av_mcastq.axq_q);
2660622b3fd2SSam Leffler 			if (cabq->axq_link != NULL) {
2661622b3fd2SSam Leffler 				*cabq->axq_link = bfm->bf_daddr;
2662622b3fd2SSam Leffler 			} else
2663622b3fd2SSam Leffler 				ath_hal_puttxbuf(ah, cabq->axq_qnum,
2664622b3fd2SSam Leffler 					bfm->bf_daddr);
2665b032f27cSSam Leffler 			ath_txqmove(cabq, &avp->av_mcastq);
2666622b3fd2SSam Leffler 
2667622b3fd2SSam Leffler 			sc->sc_stats.ast_cabq_xmit += nmcastq;
2668622b3fd2SSam Leffler 		}
2669622b3fd2SSam Leffler 		/* NB: gated by beacon so safe to start here */
26706b349e5aSAdrian Chadd 		if (! TAILQ_EMPTY(&(cabq->axq_q)))
2671622b3fd2SSam Leffler 			ath_hal_txstart(ah, cabq->axq_qnum);
2672b032f27cSSam Leffler 		ATH_TXQ_UNLOCK(&avp->av_mcastq);
26737b15790aSAdrian Chadd 		ATH_TXQ_UNLOCK(cabq);
2674622b3fd2SSam Leffler 	}
2675b032f27cSSam Leffler 	return bf;
2676b032f27cSSam Leffler }
2677b032f27cSSam Leffler 
2678b032f27cSSam Leffler static void
2679b032f27cSSam Leffler ath_beacon_start_adhoc(struct ath_softc *sc, struct ieee80211vap *vap)
2680b032f27cSSam Leffler {
2681b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
2682b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2683b032f27cSSam Leffler 	struct ath_buf *bf;
2684b032f27cSSam Leffler 	struct mbuf *m;
2685b032f27cSSam Leffler 	int error;
2686b032f27cSSam Leffler 
2687b032f27cSSam Leffler 	KASSERT(avp->av_bcbuf != NULL, ("no beacon buffer"));
2688b032f27cSSam Leffler 
2689b032f27cSSam Leffler 	/*
2690b032f27cSSam Leffler 	 * Update dynamic beacon contents.  If this returns
2691b032f27cSSam Leffler 	 * non-zero then we need to remap the memory because
2692b032f27cSSam Leffler 	 * the beacon frame changed size (probably because
2693b032f27cSSam Leffler 	 * of the TIM bitmap).
2694b032f27cSSam Leffler 	 */
2695b032f27cSSam Leffler 	bf = avp->av_bcbuf;
2696b032f27cSSam Leffler 	m = bf->bf_m;
2697b032f27cSSam Leffler 	if (ieee80211_beacon_update(bf->bf_node, &avp->av_boff, m, 0)) {
2698b032f27cSSam Leffler 		/* XXX too conservative? */
2699b032f27cSSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
2700b032f27cSSam Leffler 		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m,
2701b032f27cSSam Leffler 					     bf->bf_segs, &bf->bf_nseg,
2702b032f27cSSam Leffler 					     BUS_DMA_NOWAIT);
2703b032f27cSSam Leffler 		if (error != 0) {
2704b032f27cSSam Leffler 			if_printf(vap->iv_ifp,
2705b032f27cSSam Leffler 			    "%s: bus_dmamap_load_mbuf_sg failed, error %u\n",
2706b032f27cSSam Leffler 			    __func__, error);
2707b032f27cSSam Leffler 			return;
2708b032f27cSSam Leffler 		}
2709b032f27cSSam Leffler 	}
2710b032f27cSSam Leffler 	ath_beacon_setup(sc, bf);
2711b032f27cSSam Leffler 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
2712b032f27cSSam Leffler 
2713b032f27cSSam Leffler 	/* NB: caller is known to have already stopped tx dma */
27145591b213SSam Leffler 	ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr);
27155591b213SSam Leffler 	ath_hal_txstart(ah, sc->sc_bhalq);
27165591b213SSam Leffler }
27175591b213SSam Leffler 
2718c42a7b7eSSam Leffler /*
2719c42a7b7eSSam Leffler  * Reset the hardware after detecting beacons have stopped.
2720c42a7b7eSSam Leffler  */
2721c42a7b7eSSam Leffler static void
2722c42a7b7eSSam Leffler ath_bstuck_proc(void *arg, int pending)
2723c42a7b7eSSam Leffler {
2724c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
2725fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
2726c42a7b7eSSam Leffler 
2727c42a7b7eSSam Leffler 	if_printf(ifp, "stuck beacon; resetting (bmiss count %u)\n",
2728c42a7b7eSSam Leffler 		sc->sc_bmisscount);
2729c2e34459SSam Leffler 	sc->sc_stats.ast_bstuck++;
2730c42a7b7eSSam Leffler 	ath_reset(ifp);
2731c42a7b7eSSam Leffler }
2732c42a7b7eSSam Leffler 
2733c42a7b7eSSam Leffler /*
2734b032f27cSSam Leffler  * Reclaim beacon resources and return buffer to the pool.
2735b032f27cSSam Leffler  */
2736b032f27cSSam Leffler static void
2737b032f27cSSam Leffler ath_beacon_return(struct ath_softc *sc, struct ath_buf *bf)
2738b032f27cSSam Leffler {
2739b032f27cSSam Leffler 
2740b032f27cSSam Leffler 	if (bf->bf_m != NULL) {
2741b032f27cSSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
2742b032f27cSSam Leffler 		m_freem(bf->bf_m);
2743b032f27cSSam Leffler 		bf->bf_m = NULL;
2744b032f27cSSam Leffler 	}
2745b032f27cSSam Leffler 	if (bf->bf_node != NULL) {
2746b032f27cSSam Leffler 		ieee80211_free_node(bf->bf_node);
2747b032f27cSSam Leffler 		bf->bf_node = NULL;
2748b032f27cSSam Leffler 	}
27496b349e5aSAdrian Chadd 	TAILQ_INSERT_TAIL(&sc->sc_bbuf, bf, bf_list);
2750b032f27cSSam Leffler }
2751b032f27cSSam Leffler 
2752b032f27cSSam Leffler /*
2753c42a7b7eSSam Leffler  * Reclaim beacon resources.
2754c42a7b7eSSam Leffler  */
27555591b213SSam Leffler static void
27565591b213SSam Leffler ath_beacon_free(struct ath_softc *sc)
27575591b213SSam Leffler {
2758c42a7b7eSSam Leffler 	struct ath_buf *bf;
27595591b213SSam Leffler 
27606b349e5aSAdrian Chadd 	TAILQ_FOREACH(bf, &sc->sc_bbuf, bf_list) {
27615591b213SSam Leffler 		if (bf->bf_m != NULL) {
27625591b213SSam Leffler 			bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
27635591b213SSam Leffler 			m_freem(bf->bf_m);
27645591b213SSam Leffler 			bf->bf_m = NULL;
2765f818612bSSam Leffler 		}
2766f818612bSSam Leffler 		if (bf->bf_node != NULL) {
2767f818612bSSam Leffler 			ieee80211_free_node(bf->bf_node);
27685591b213SSam Leffler 			bf->bf_node = NULL;
27695591b213SSam Leffler 		}
27705591b213SSam Leffler 	}
2771f818612bSSam Leffler }
27725591b213SSam Leffler 
27735591b213SSam Leffler /*
27745591b213SSam Leffler  * Configure the beacon and sleep timers.
27755591b213SSam Leffler  *
27765591b213SSam Leffler  * When operating as an AP this resets the TSF and sets
27775591b213SSam Leffler  * up the hardware to notify us when we need to issue beacons.
27785591b213SSam Leffler  *
27795591b213SSam Leffler  * When operating in station mode this sets up the beacon
27805591b213SSam Leffler  * timers according to the timestamp of the last received
27815591b213SSam Leffler  * beacon and the current TSF, configures PCF and DTIM
27825591b213SSam Leffler  * handling, programs the sleep registers so the hardware
27835591b213SSam Leffler  * will wakeup in time to receive beacons, and configures
27845591b213SSam Leffler  * the beacon miss handling so we'll receive a BMISS
27855591b213SSam Leffler  * interrupt when we stop seeing beacons from the AP
27865591b213SSam Leffler  * we've associated with.
27875591b213SSam Leffler  */
27885591b213SSam Leffler static void
2789b032f27cSSam Leffler ath_beacon_config(struct ath_softc *sc, struct ieee80211vap *vap)
27905591b213SSam Leffler {
279180d939bfSSam Leffler #define	TSF_TO_TU(_h,_l) \
279280d939bfSSam Leffler 	((((u_int32_t)(_h)) << 22) | (((u_int32_t)(_l)) >> 10))
279380d939bfSSam Leffler #define	FUDGE	2
27945591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2795b032f27cSSam Leffler 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2796b032f27cSSam Leffler 	struct ieee80211_node *ni;
279780d939bfSSam Leffler 	u_int32_t nexttbtt, intval, tsftu;
279880d939bfSSam Leffler 	u_int64_t tsf;
27995591b213SSam Leffler 
2800b032f27cSSam Leffler 	if (vap == NULL)
2801b032f27cSSam Leffler 		vap = TAILQ_FIRST(&ic->ic_vaps);	/* XXX */
2802b032f27cSSam Leffler 	ni = vap->iv_bss;
2803b032f27cSSam Leffler 
28048371372bSSam Leffler 	/* extract tstamp from last beacon and convert to TU */
28058371372bSSam Leffler 	nexttbtt = TSF_TO_TU(LE_READ_4(ni->ni_tstamp.data + 4),
28068371372bSSam Leffler 			     LE_READ_4(ni->ni_tstamp.data));
280759aa14a9SRui Paulo 	if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
280859aa14a9SRui Paulo 	    ic->ic_opmode == IEEE80211_M_MBSS) {
2809b032f27cSSam Leffler 		/*
281059aa14a9SRui Paulo 		 * For multi-bss ap/mesh support beacons are either staggered
2811b032f27cSSam Leffler 		 * evenly over N slots or burst together.  For the former
2812b032f27cSSam Leffler 		 * arrange for the SWBA to be delivered for each slot.
2813b032f27cSSam Leffler 		 * Slots that are not occupied will generate nothing.
2814b032f27cSSam Leffler 		 */
28158371372bSSam Leffler 		/* NB: the beacon interval is kept internally in TU's */
28164bacf7c1SSam Leffler 		intval = ni->ni_intval & HAL_BEACON_PERIOD;
2817b032f27cSSam Leffler 		if (sc->sc_stagbeacons)
2818b032f27cSSam Leffler 			intval /= ATH_BCBUF;
2819b032f27cSSam Leffler 	} else {
2820b032f27cSSam Leffler 		/* NB: the beacon interval is kept internally in TU's */
2821b032f27cSSam Leffler 		intval = ni->ni_intval & HAL_BEACON_PERIOD;
2822b032f27cSSam Leffler 	}
2823a6c992f4SSam Leffler 	if (nexttbtt == 0)		/* e.g. for ap mode */
2824a6c992f4SSam Leffler 		nexttbtt = intval;
2825a6c992f4SSam Leffler 	else if (intval)		/* NB: can be 0 for monitor mode */
2826a6c992f4SSam Leffler 		nexttbtt = roundup(nexttbtt, intval);
2827a6c992f4SSam Leffler 	DPRINTF(sc, ATH_DEBUG_BEACON, "%s: nexttbtt %u intval %u (%u)\n",
2828a6c992f4SSam Leffler 		__func__, nexttbtt, intval, ni->ni_intval);
2829b032f27cSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA && !sc->sc_swbmiss) {
28305591b213SSam Leffler 		HAL_BEACON_STATE bs;
28318371372bSSam Leffler 		int dtimperiod, dtimcount;
28328371372bSSam Leffler 		int cfpperiod, cfpcount;
28335591b213SSam Leffler 
28348371372bSSam Leffler 		/*
28358371372bSSam Leffler 		 * Setup dtim and cfp parameters according to
28368371372bSSam Leffler 		 * last beacon we received (which may be none).
28378371372bSSam Leffler 		 */
28388371372bSSam Leffler 		dtimperiod = ni->ni_dtim_period;
28398371372bSSam Leffler 		if (dtimperiod <= 0)		/* NB: 0 if not known */
28408371372bSSam Leffler 			dtimperiod = 1;
28418371372bSSam Leffler 		dtimcount = ni->ni_dtim_count;
28428371372bSSam Leffler 		if (dtimcount >= dtimperiod)	/* NB: sanity check */
28438371372bSSam Leffler 			dtimcount = 0;		/* XXX? */
28448371372bSSam Leffler 		cfpperiod = 1;			/* NB: no PCF support yet */
28458371372bSSam Leffler 		cfpcount = 0;
28468371372bSSam Leffler 		/*
28478371372bSSam Leffler 		 * Pull nexttbtt forward to reflect the current
28488371372bSSam Leffler 		 * TSF and calculate dtim+cfp state for the result.
28498371372bSSam Leffler 		 */
28508371372bSSam Leffler 		tsf = ath_hal_gettsf64(ah);
285180d939bfSSam Leffler 		tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
28528371372bSSam Leffler 		do {
28538371372bSSam Leffler 			nexttbtt += intval;
28548371372bSSam Leffler 			if (--dtimcount < 0) {
28558371372bSSam Leffler 				dtimcount = dtimperiod - 1;
28568371372bSSam Leffler 				if (--cfpcount < 0)
28578371372bSSam Leffler 					cfpcount = cfpperiod - 1;
28588371372bSSam Leffler 			}
28598371372bSSam Leffler 		} while (nexttbtt < tsftu);
28605591b213SSam Leffler 		memset(&bs, 0, sizeof(bs));
2861a6c992f4SSam Leffler 		bs.bs_intval = intval;
28625591b213SSam Leffler 		bs.bs_nexttbtt = nexttbtt;
28638371372bSSam Leffler 		bs.bs_dtimperiod = dtimperiod*intval;
28648371372bSSam Leffler 		bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval;
28658371372bSSam Leffler 		bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod;
28668371372bSSam Leffler 		bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod;
28678371372bSSam Leffler 		bs.bs_cfpmaxduration = 0;
28688371372bSSam Leffler #if 0
28695591b213SSam Leffler 		/*
2870c42a7b7eSSam Leffler 		 * The 802.11 layer records the offset to the DTIM
2871c42a7b7eSSam Leffler 		 * bitmap while receiving beacons; use it here to
2872c42a7b7eSSam Leffler 		 * enable h/w detection of our AID being marked in
2873c42a7b7eSSam Leffler 		 * the bitmap vector (to indicate frames for us are
2874c42a7b7eSSam Leffler 		 * pending at the AP).
28758371372bSSam Leffler 		 * XXX do DTIM handling in s/w to WAR old h/w bugs
28768371372bSSam Leffler 		 * XXX enable based on h/w rev for newer chips
2877c42a7b7eSSam Leffler 		 */
2878c42a7b7eSSam Leffler 		bs.bs_timoffset = ni->ni_timoff;
28798371372bSSam Leffler #endif
2880c42a7b7eSSam Leffler 		/*
28815591b213SSam Leffler 		 * Calculate the number of consecutive beacons to miss
288268e8e04eSSam Leffler 		 * before taking a BMISS interrupt.
28835591b213SSam Leffler 		 * Note that we clamp the result to at most 10 beacons.
28845591b213SSam Leffler 		 */
2885b032f27cSSam Leffler 		bs.bs_bmissthreshold = vap->iv_bmissthreshold;
28865591b213SSam Leffler 		if (bs.bs_bmissthreshold > 10)
28875591b213SSam Leffler 			bs.bs_bmissthreshold = 10;
28885591b213SSam Leffler 		else if (bs.bs_bmissthreshold <= 0)
28895591b213SSam Leffler 			bs.bs_bmissthreshold = 1;
28905591b213SSam Leffler 
28915591b213SSam Leffler 		/*
28925591b213SSam Leffler 		 * Calculate sleep duration.  The configuration is
28935591b213SSam Leffler 		 * given in ms.  We insure a multiple of the beacon
28945591b213SSam Leffler 		 * period is used.  Also, if the sleep duration is
28955591b213SSam Leffler 		 * greater than the DTIM period then it makes senses
28965591b213SSam Leffler 		 * to make it a multiple of that.
28975591b213SSam Leffler 		 *
28985591b213SSam Leffler 		 * XXX fixed at 100ms
28995591b213SSam Leffler 		 */
29004bacf7c1SSam Leffler 		bs.bs_sleepduration =
29014bacf7c1SSam Leffler 			roundup(IEEE80211_MS_TO_TU(100), bs.bs_intval);
29025591b213SSam Leffler 		if (bs.bs_sleepduration > bs.bs_dtimperiod)
29035591b213SSam Leffler 			bs.bs_sleepduration = roundup(bs.bs_sleepduration, bs.bs_dtimperiod);
29045591b213SSam Leffler 
2905c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
29068371372bSSam 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"
29075591b213SSam Leffler 			, __func__
29088371372bSSam Leffler 			, tsf, tsftu
29095591b213SSam Leffler 			, bs.bs_intval
29105591b213SSam Leffler 			, bs.bs_nexttbtt
29115591b213SSam Leffler 			, bs.bs_dtimperiod
29125591b213SSam Leffler 			, bs.bs_nextdtim
29135591b213SSam Leffler 			, bs.bs_bmissthreshold
29145591b213SSam Leffler 			, bs.bs_sleepduration
2915c42a7b7eSSam Leffler 			, bs.bs_cfpperiod
2916c42a7b7eSSam Leffler 			, bs.bs_cfpmaxduration
2917c42a7b7eSSam Leffler 			, bs.bs_cfpnext
2918c42a7b7eSSam Leffler 			, bs.bs_timoffset
2919c42a7b7eSSam Leffler 		);
29205591b213SSam Leffler 		ath_hal_intrset(ah, 0);
2921c42a7b7eSSam Leffler 		ath_hal_beacontimers(ah, &bs);
29225591b213SSam Leffler 		sc->sc_imask |= HAL_INT_BMISS;
29235591b213SSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
29245591b213SSam Leffler 	} else {
29255591b213SSam Leffler 		ath_hal_intrset(ah, 0);
2926a6c992f4SSam Leffler 		if (nexttbtt == intval)
2927c42a7b7eSSam Leffler 			intval |= HAL_BEACON_RESET_TSF;
2928c42a7b7eSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS) {
2929c42a7b7eSSam Leffler 			/*
2930c42a7b7eSSam Leffler 			 * In IBSS mode enable the beacon timers but only
2931c42a7b7eSSam Leffler 			 * enable SWBA interrupts if we need to manually
2932c42a7b7eSSam Leffler 			 * prepare beacon frames.  Otherwise we use a
2933c42a7b7eSSam Leffler 			 * self-linked tx descriptor and let the hardware
2934c42a7b7eSSam Leffler 			 * deal with things.
2935c42a7b7eSSam Leffler 			 */
2936c42a7b7eSSam Leffler 			intval |= HAL_BEACON_ENA;
2937c42a7b7eSSam Leffler 			if (!sc->sc_hasveol)
2938c42a7b7eSSam Leffler 				sc->sc_imask |= HAL_INT_SWBA;
293980d939bfSSam Leffler 			if ((intval & HAL_BEACON_RESET_TSF) == 0) {
294080d939bfSSam Leffler 				/*
294180d939bfSSam Leffler 				 * Pull nexttbtt forward to reflect
294280d939bfSSam Leffler 				 * the current TSF.
294380d939bfSSam Leffler 				 */
294480d939bfSSam Leffler 				tsf = ath_hal_gettsf64(ah);
294580d939bfSSam Leffler 				tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
294680d939bfSSam Leffler 				do {
294780d939bfSSam Leffler 					nexttbtt += intval;
294880d939bfSSam Leffler 				} while (nexttbtt < tsftu);
294980d939bfSSam Leffler 			}
29500f2e86fbSSam Leffler 			ath_beaconq_config(sc);
295159aa14a9SRui Paulo 		} else if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
295259aa14a9SRui Paulo 		    ic->ic_opmode == IEEE80211_M_MBSS) {
2953c42a7b7eSSam Leffler 			/*
295459aa14a9SRui Paulo 			 * In AP/mesh mode we enable the beacon timers
295559aa14a9SRui Paulo 			 * and SWBA interrupts to prepare beacon frames.
2956c42a7b7eSSam Leffler 			 */
2957c42a7b7eSSam Leffler 			intval |= HAL_BEACON_ENA;
29585591b213SSam Leffler 			sc->sc_imask |= HAL_INT_SWBA;	/* beacon prepare */
29590f2e86fbSSam Leffler 			ath_beaconq_config(sc);
2960c42a7b7eSSam Leffler 		}
2961c42a7b7eSSam Leffler 		ath_hal_beaconinit(ah, nexttbtt, intval);
2962c42a7b7eSSam Leffler 		sc->sc_bmisscount = 0;
29635591b213SSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
2964c42a7b7eSSam Leffler 		/*
2965c42a7b7eSSam Leffler 		 * When using a self-linked beacon descriptor in
2966c42a7b7eSSam Leffler 		 * ibss mode load it once here.
2967c42a7b7eSSam Leffler 		 */
2968c42a7b7eSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_hasveol)
2969b032f27cSSam Leffler 			ath_beacon_start_adhoc(sc, vap);
29705591b213SSam Leffler 	}
297180d939bfSSam Leffler 	sc->sc_syncbeacon = 0;
297280d939bfSSam Leffler #undef FUDGE
29738371372bSSam Leffler #undef TSF_TO_TU
29745591b213SSam Leffler }
29755591b213SSam Leffler 
29765591b213SSam Leffler static void
29775591b213SSam Leffler ath_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
29785591b213SSam Leffler {
29795591b213SSam Leffler 	bus_addr_t *paddr = (bus_addr_t*) arg;
2980d77367bfSSam Leffler 	KASSERT(error == 0, ("error %u on bus_dma callback", error));
29815591b213SSam Leffler 	*paddr = segs->ds_addr;
29825591b213SSam Leffler }
29835591b213SSam Leffler 
29845591b213SSam Leffler static int
2985c42a7b7eSSam Leffler ath_descdma_setup(struct ath_softc *sc,
2986c42a7b7eSSam Leffler 	struct ath_descdma *dd, ath_bufhead *head,
2987c42a7b7eSSam Leffler 	const char *name, int nbuf, int ndesc)
2988c42a7b7eSSam Leffler {
2989c42a7b7eSSam Leffler #define	DS2PHYS(_dd, _ds) \
2990c42a7b7eSSam Leffler 	((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
299145abcd6cSAdrian Chadd #define	ATH_DESC_4KB_BOUND_CHECK(_daddr, _len) \
299245abcd6cSAdrian Chadd 	((((u_int32_t)(_daddr) & 0xFFF) > (0x1000 - (_len))) ? 1 : 0)
2993fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
299445abcd6cSAdrian Chadd 	uint8_t *ds;
2995c42a7b7eSSam Leffler 	struct ath_buf *bf;
2996c42a7b7eSSam Leffler 	int i, bsize, error;
299745abcd6cSAdrian Chadd 	int desc_len;
299845abcd6cSAdrian Chadd 
299945abcd6cSAdrian Chadd 	desc_len = sizeof(struct ath_desc);
3000c42a7b7eSSam Leffler 
3001c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA: %u buffers %u desc/buf\n",
3002c42a7b7eSSam Leffler 	    __func__, name, nbuf, ndesc);
3003c42a7b7eSSam Leffler 
3004c42a7b7eSSam Leffler 	dd->dd_name = name;
300545abcd6cSAdrian Chadd 	dd->dd_desc_len = desc_len * nbuf * ndesc;
300645abcd6cSAdrian Chadd 
300745abcd6cSAdrian Chadd 	/*
300845abcd6cSAdrian Chadd 	 * Merlin work-around:
300945abcd6cSAdrian Chadd 	 * Descriptors that cross the 4KB boundary can't be used.
301045abcd6cSAdrian Chadd 	 * Assume one skipped descriptor per 4KB page.
301145abcd6cSAdrian Chadd 	 */
301245abcd6cSAdrian Chadd 	if (! ath_hal_split4ktrans(sc->sc_ah)) {
301345abcd6cSAdrian Chadd 		int numdescpage = 4096 / (desc_len * ndesc);
301445abcd6cSAdrian Chadd 		dd->dd_desc_len = (nbuf / numdescpage + 1) * 4096;
301545abcd6cSAdrian Chadd 	}
3016c42a7b7eSSam Leffler 
3017c42a7b7eSSam Leffler 	/*
3018c42a7b7eSSam Leffler 	 * Setup DMA descriptor area.
3019c42a7b7eSSam Leffler 	 */
3020c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev),	/* parent */
3021c42a7b7eSSam Leffler 		       PAGE_SIZE, 0,		/* alignment, bounds */
3022c42a7b7eSSam Leffler 		       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
3023c42a7b7eSSam Leffler 		       BUS_SPACE_MAXADDR,	/* highaddr */
3024c42a7b7eSSam Leffler 		       NULL, NULL,		/* filter, filterarg */
3025c42a7b7eSSam Leffler 		       dd->dd_desc_len,		/* maxsize */
3026c42a7b7eSSam Leffler 		       1,			/* nsegments */
30276ccb8ea7SSam Leffler 		       dd->dd_desc_len,		/* maxsegsize */
3028c42a7b7eSSam Leffler 		       BUS_DMA_ALLOCNOW,	/* flags */
3029c42a7b7eSSam Leffler 		       NULL,			/* lockfunc */
3030c42a7b7eSSam Leffler 		       NULL,			/* lockarg */
3031c42a7b7eSSam Leffler 		       &dd->dd_dmat);
3032c42a7b7eSSam Leffler 	if (error != 0) {
3033c42a7b7eSSam Leffler 		if_printf(ifp, "cannot allocate %s DMA tag\n", dd->dd_name);
3034c42a7b7eSSam Leffler 		return error;
3035c42a7b7eSSam Leffler 	}
3036c42a7b7eSSam Leffler 
3037c42a7b7eSSam Leffler 	/* allocate descriptors */
3038c42a7b7eSSam Leffler 	error = bus_dmamap_create(dd->dd_dmat, BUS_DMA_NOWAIT, &dd->dd_dmamap);
3039c42a7b7eSSam Leffler 	if (error != 0) {
3040c42a7b7eSSam Leffler 		if_printf(ifp, "unable to create dmamap for %s descriptors, "
3041c42a7b7eSSam Leffler 			"error %u\n", dd->dd_name, error);
3042c42a7b7eSSam Leffler 		goto fail0;
3043c42a7b7eSSam Leffler 	}
3044c42a7b7eSSam Leffler 
3045c42a7b7eSSam Leffler 	error = bus_dmamem_alloc(dd->dd_dmat, (void**) &dd->dd_desc,
30460553a01fSSam Leffler 				 BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
30470553a01fSSam Leffler 				 &dd->dd_dmamap);
3048c42a7b7eSSam Leffler 	if (error != 0) {
3049c42a7b7eSSam Leffler 		if_printf(ifp, "unable to alloc memory for %u %s descriptors, "
3050c42a7b7eSSam Leffler 			"error %u\n", nbuf * ndesc, dd->dd_name, error);
3051c42a7b7eSSam Leffler 		goto fail1;
3052c42a7b7eSSam Leffler 	}
3053c42a7b7eSSam Leffler 
3054c42a7b7eSSam Leffler 	error = bus_dmamap_load(dd->dd_dmat, dd->dd_dmamap,
3055c42a7b7eSSam Leffler 				dd->dd_desc, dd->dd_desc_len,
3056c42a7b7eSSam Leffler 				ath_load_cb, &dd->dd_desc_paddr,
3057c42a7b7eSSam Leffler 				BUS_DMA_NOWAIT);
3058c42a7b7eSSam Leffler 	if (error != 0) {
3059c42a7b7eSSam Leffler 		if_printf(ifp, "unable to map %s descriptors, error %u\n",
3060c42a7b7eSSam Leffler 			dd->dd_name, error);
3061c42a7b7eSSam Leffler 		goto fail2;
3062c42a7b7eSSam Leffler 	}
3063c42a7b7eSSam Leffler 
306445abcd6cSAdrian Chadd 	ds = (uint8_t *) dd->dd_desc;
3065c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA map: %p (%lu) -> %p (%lu)\n",
3066c42a7b7eSSam Leffler 	    __func__, dd->dd_name, ds, (u_long) dd->dd_desc_len,
3067c42a7b7eSSam Leffler 	    (caddr_t) dd->dd_desc_paddr, /*XXX*/ (u_long) dd->dd_desc_len);
3068c42a7b7eSSam Leffler 
3069ebecf802SSam Leffler 	/* allocate rx buffers */
3070c42a7b7eSSam Leffler 	bsize = sizeof(struct ath_buf) * nbuf;
3071c42a7b7eSSam Leffler 	bf = malloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO);
3072c42a7b7eSSam Leffler 	if (bf == NULL) {
3073c42a7b7eSSam Leffler 		if_printf(ifp, "malloc of %s buffers failed, size %u\n",
3074c42a7b7eSSam Leffler 			dd->dd_name, bsize);
3075c42a7b7eSSam Leffler 		goto fail3;
3076c42a7b7eSSam Leffler 	}
3077c42a7b7eSSam Leffler 	dd->dd_bufptr = bf;
3078c42a7b7eSSam Leffler 
30796b349e5aSAdrian Chadd 	TAILQ_INIT(head);
308045abcd6cSAdrian Chadd 	for (i = 0; i < nbuf; i++, bf++, ds += (ndesc * desc_len)) {
308145abcd6cSAdrian Chadd 		bf->bf_desc = (struct ath_desc *) ds;
3082c42a7b7eSSam Leffler 		bf->bf_daddr = DS2PHYS(dd, ds);
308345abcd6cSAdrian Chadd 		if (! ath_hal_split4ktrans(sc->sc_ah)) {
308445abcd6cSAdrian Chadd 			/*
308545abcd6cSAdrian Chadd 			 * Merlin WAR: Skip descriptor addresses which
308645abcd6cSAdrian Chadd 			 * cause 4KB boundary crossing along any point
308745abcd6cSAdrian Chadd 			 * in the descriptor.
308845abcd6cSAdrian Chadd 			 */
308945abcd6cSAdrian Chadd 			 if (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr,
309045abcd6cSAdrian Chadd 			     desc_len * ndesc)) {
309145abcd6cSAdrian Chadd 				/* Start at the next page */
309245abcd6cSAdrian Chadd 				ds += 0x1000 - (bf->bf_daddr & 0xFFF);
309345abcd6cSAdrian Chadd 				bf->bf_desc = (struct ath_desc *) ds;
309445abcd6cSAdrian Chadd 				bf->bf_daddr = DS2PHYS(dd, ds);
309545abcd6cSAdrian Chadd 			}
309645abcd6cSAdrian Chadd 		}
3097c42a7b7eSSam Leffler 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
3098c42a7b7eSSam Leffler 				&bf->bf_dmamap);
3099c42a7b7eSSam Leffler 		if (error != 0) {
3100c42a7b7eSSam Leffler 			if_printf(ifp, "unable to create dmamap for %s "
3101c42a7b7eSSam Leffler 				"buffer %u, error %u\n", dd->dd_name, i, error);
3102c42a7b7eSSam Leffler 			ath_descdma_cleanup(sc, dd, head);
3103c42a7b7eSSam Leffler 			return error;
3104c42a7b7eSSam Leffler 		}
31056b349e5aSAdrian Chadd 		TAILQ_INSERT_TAIL(head, bf, bf_list);
3106c42a7b7eSSam Leffler 	}
3107c42a7b7eSSam Leffler 	return 0;
3108c42a7b7eSSam Leffler fail3:
3109c42a7b7eSSam Leffler 	bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
3110c42a7b7eSSam Leffler fail2:
3111c42a7b7eSSam Leffler 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3112c42a7b7eSSam Leffler fail1:
3113c42a7b7eSSam Leffler 	bus_dmamap_destroy(dd->dd_dmat, dd->dd_dmamap);
3114c42a7b7eSSam Leffler fail0:
3115c42a7b7eSSam Leffler 	bus_dma_tag_destroy(dd->dd_dmat);
3116c42a7b7eSSam Leffler 	memset(dd, 0, sizeof(*dd));
3117c42a7b7eSSam Leffler 	return error;
3118c42a7b7eSSam Leffler #undef DS2PHYS
311945abcd6cSAdrian Chadd #undef ATH_DESC_4KB_BOUND_CHECK
3120c42a7b7eSSam Leffler }
3121c42a7b7eSSam Leffler 
3122c42a7b7eSSam Leffler static void
3123c42a7b7eSSam Leffler ath_descdma_cleanup(struct ath_softc *sc,
3124c42a7b7eSSam Leffler 	struct ath_descdma *dd, ath_bufhead *head)
3125c42a7b7eSSam Leffler {
3126c42a7b7eSSam Leffler 	struct ath_buf *bf;
3127c42a7b7eSSam Leffler 	struct ieee80211_node *ni;
3128c42a7b7eSSam Leffler 
3129c42a7b7eSSam Leffler 	bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
3130c42a7b7eSSam Leffler 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3131c42a7b7eSSam Leffler 	bus_dmamap_destroy(dd->dd_dmat, dd->dd_dmamap);
3132c42a7b7eSSam Leffler 	bus_dma_tag_destroy(dd->dd_dmat);
3133c42a7b7eSSam Leffler 
31346b349e5aSAdrian Chadd 	TAILQ_FOREACH(bf, head, bf_list) {
3135c42a7b7eSSam Leffler 		if (bf->bf_m) {
3136c42a7b7eSSam Leffler 			m_freem(bf->bf_m);
3137c42a7b7eSSam Leffler 			bf->bf_m = NULL;
3138c42a7b7eSSam Leffler 		}
3139c42a7b7eSSam Leffler 		if (bf->bf_dmamap != NULL) {
3140c42a7b7eSSam Leffler 			bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
3141c42a7b7eSSam Leffler 			bf->bf_dmamap = NULL;
3142c42a7b7eSSam Leffler 		}
3143c42a7b7eSSam Leffler 		ni = bf->bf_node;
3144c42a7b7eSSam Leffler 		bf->bf_node = NULL;
3145c42a7b7eSSam Leffler 		if (ni != NULL) {
3146c42a7b7eSSam Leffler 			/*
3147c42a7b7eSSam Leffler 			 * Reclaim node reference.
3148c42a7b7eSSam Leffler 			 */
3149c42a7b7eSSam Leffler 			ieee80211_free_node(ni);
3150c42a7b7eSSam Leffler 		}
3151c42a7b7eSSam Leffler 	}
3152c42a7b7eSSam Leffler 
31536b349e5aSAdrian Chadd 	TAILQ_INIT(head);
3154c42a7b7eSSam Leffler 	free(dd->dd_bufptr, M_ATHDEV);
3155c42a7b7eSSam Leffler 	memset(dd, 0, sizeof(*dd));
3156c42a7b7eSSam Leffler }
3157c42a7b7eSSam Leffler 
3158c42a7b7eSSam Leffler static int
31595591b213SSam Leffler ath_desc_alloc(struct ath_softc *sc)
31605591b213SSam Leffler {
3161c42a7b7eSSam Leffler 	int error;
31625591b213SSam Leffler 
3163c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_rxdma, &sc->sc_rxbuf,
3164e2d787faSSam Leffler 			"rx", ath_rxbuf, 1);
31655591b213SSam Leffler 	if (error != 0)
31665591b213SSam Leffler 		return error;
31675591b213SSam Leffler 
3168c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_txdma, &sc->sc_txbuf,
3169e2d787faSSam Leffler 			"tx", ath_txbuf, ATH_TXDESC);
3170c42a7b7eSSam Leffler 	if (error != 0) {
3171c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
31725591b213SSam Leffler 		return error;
3173c42a7b7eSSam Leffler 	}
3174c42a7b7eSSam Leffler 
3175c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_bdma, &sc->sc_bbuf,
3176b032f27cSSam Leffler 			"beacon", ATH_BCBUF, 1);
3177c42a7b7eSSam Leffler 	if (error != 0) {
3178c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3179c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
3180c42a7b7eSSam Leffler 		return error;
3181c42a7b7eSSam Leffler 	}
31825591b213SSam Leffler 	return 0;
31835591b213SSam Leffler }
31845591b213SSam Leffler 
31855591b213SSam Leffler static void
31865591b213SSam Leffler ath_desc_free(struct ath_softc *sc)
31875591b213SSam Leffler {
31885591b213SSam Leffler 
3189c42a7b7eSSam Leffler 	if (sc->sc_bdma.dd_desc_len != 0)
3190c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_bdma, &sc->sc_bbuf);
3191c42a7b7eSSam Leffler 	if (sc->sc_txdma.dd_desc_len != 0)
3192c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3193c42a7b7eSSam Leffler 	if (sc->sc_rxdma.dd_desc_len != 0)
3194c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
31955591b213SSam Leffler }
31965591b213SSam Leffler 
31975591b213SSam Leffler static struct ieee80211_node *
319838c208f8SSam Leffler ath_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
31995591b213SSam Leffler {
320038c208f8SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
3201c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
3202c42a7b7eSSam Leffler 	const size_t space = sizeof(struct ath_node) + sc->sc_rc->arc_space;
3203c42a7b7eSSam Leffler 	struct ath_node *an;
3204c42a7b7eSSam Leffler 
3205c42a7b7eSSam Leffler 	an = malloc(space, M_80211_NODE, M_NOWAIT|M_ZERO);
3206c42a7b7eSSam Leffler 	if (an == NULL) {
3207c42a7b7eSSam Leffler 		/* XXX stat+msg */
3208de5af704SSam Leffler 		return NULL;
32095591b213SSam Leffler 	}
3210c42a7b7eSSam Leffler 	ath_rate_node_init(sc, an);
32115591b213SSam Leffler 
32123dd85b26SAdrian Chadd 	/* Setup the mutex - there's no associd yet so set the name to NULL */
32133dd85b26SAdrian Chadd 	snprintf(an->an_name, sizeof(an->an_name), "%s: node %p",
32143dd85b26SAdrian Chadd 	    device_get_nameunit(sc->sc_dev), an);
32153dd85b26SAdrian Chadd 	mtx_init(&an->an_mtx, an->an_name, NULL, MTX_DEF);
32163dd85b26SAdrian Chadd 
3217c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: an %p\n", __func__, an);
3218c42a7b7eSSam Leffler 	return &an->an_node;
3219c42a7b7eSSam Leffler }
3220c42a7b7eSSam Leffler 
32215591b213SSam Leffler static void
3222c42a7b7eSSam Leffler ath_node_free(struct ieee80211_node *ni)
32235591b213SSam Leffler {
3224c42a7b7eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
3225c42a7b7eSSam Leffler         struct ath_softc *sc = ic->ic_ifp->if_softc;
32261e774079SSam Leffler 
3227c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: ni %p\n", __func__, ni);
32283dd85b26SAdrian Chadd 	mtx_destroy(&ATH_NODE(ni)->an_mtx);
3229c42a7b7eSSam Leffler 	ath_rate_node_cleanup(sc, ATH_NODE(ni));
3230c42a7b7eSSam Leffler 	sc->sc_node_free(ni);
32315591b213SSam Leffler }
32325591b213SSam Leffler 
323368e8e04eSSam Leffler static void
323468e8e04eSSam Leffler ath_node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
323568e8e04eSSam Leffler {
323668e8e04eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
323768e8e04eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
323868e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
323968e8e04eSSam Leffler 
3240b032f27cSSam Leffler 	*rssi = ic->ic_node_getrssi(ni);
324159efa8b5SSam Leffler 	if (ni->ni_chan != IEEE80211_CHAN_ANYC)
324259efa8b5SSam Leffler 		*noise = ath_hal_getchannoise(ah, ni->ni_chan);
324359efa8b5SSam Leffler 	else
324468e8e04eSSam Leffler 		*noise = -95;		/* nominally correct */
324568e8e04eSSam Leffler }
324668e8e04eSSam Leffler 
32475591b213SSam Leffler static int
32485591b213SSam Leffler ath_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf)
32495591b213SSam Leffler {
32505591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
32515591b213SSam Leffler 	int error;
32525591b213SSam Leffler 	struct mbuf *m;
32535591b213SSam Leffler 	struct ath_desc *ds;
32545591b213SSam Leffler 
32555591b213SSam Leffler 	m = bf->bf_m;
32565591b213SSam Leffler 	if (m == NULL) {
32575591b213SSam Leffler 		/*
32585591b213SSam Leffler 		 * NB: by assigning a page to the rx dma buffer we
32595591b213SSam Leffler 		 * implicitly satisfy the Atheros requirement that
32605591b213SSam Leffler 		 * this buffer be cache-line-aligned and sized to be
32615591b213SSam Leffler 		 * multiple of the cache line size.  Not doing this
32625591b213SSam Leffler 		 * causes weird stuff to happen (for the 5210 at least).
32635591b213SSam Leffler 		 */
32645591b213SSam Leffler 		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
32655591b213SSam Leffler 		if (m == NULL) {
3266c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
3267c42a7b7eSSam Leffler 				"%s: no mbuf/cluster\n", __func__);
32685591b213SSam Leffler 			sc->sc_stats.ast_rx_nombuf++;
32695591b213SSam Leffler 			return ENOMEM;
32705591b213SSam Leffler 		}
32715591b213SSam Leffler 		m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
32725591b213SSam Leffler 
3273f9e6219bSSam Leffler 		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat,
3274c42a7b7eSSam Leffler 					     bf->bf_dmamap, m,
3275f9e6219bSSam Leffler 					     bf->bf_segs, &bf->bf_nseg,
32765591b213SSam Leffler 					     BUS_DMA_NOWAIT);
32775591b213SSam Leffler 		if (error != 0) {
3278c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
3279f9e6219bSSam Leffler 			    "%s: bus_dmamap_load_mbuf_sg failed; error %d\n",
3280c42a7b7eSSam Leffler 			    __func__, error);
32815591b213SSam Leffler 			sc->sc_stats.ast_rx_busdma++;
3282b2792ff6SSam Leffler 			m_freem(m);
32835591b213SSam Leffler 			return error;
32845591b213SSam Leffler 		}
3285d77367bfSSam Leffler 		KASSERT(bf->bf_nseg == 1,
3286d77367bfSSam Leffler 			("multi-segment packet; nseg %u", bf->bf_nseg));
3287b2792ff6SSam Leffler 		bf->bf_m = m;
32885591b213SSam Leffler 	}
32895591b213SSam Leffler 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREREAD);
32905591b213SSam Leffler 
329104e22a02SSam Leffler 	/*
329204e22a02SSam Leffler 	 * Setup descriptors.  For receive we always terminate
329304e22a02SSam Leffler 	 * the descriptor list with a self-linked entry so we'll
329404e22a02SSam Leffler 	 * not get overrun under high load (as can happen with a
3295c42a7b7eSSam Leffler 	 * 5212 when ANI processing enables PHY error frames).
329604e22a02SSam Leffler 	 *
329704e22a02SSam Leffler 	 * To insure the last descriptor is self-linked we create
329804e22a02SSam Leffler 	 * each descriptor as self-linked and add it to the end.  As
329904e22a02SSam Leffler 	 * each additional descriptor is added the previous self-linked
330004e22a02SSam Leffler 	 * entry is ``fixed'' naturally.  This should be safe even
330104e22a02SSam Leffler 	 * if DMA is happening.  When processing RX interrupts we
330204e22a02SSam Leffler 	 * never remove/process the last, self-linked, entry on the
330304e22a02SSam Leffler 	 * descriptor list.  This insures the hardware always has
330404e22a02SSam Leffler 	 * someplace to write a new frame.
330504e22a02SSam Leffler 	 */
33068a2a6beeSAdrian Chadd 	/*
33078a2a6beeSAdrian Chadd 	 * 11N: we can no longer afford to self link the last descriptor.
33088a2a6beeSAdrian Chadd 	 * MAC acknowledges BA status as long as it copies frames to host
33098a2a6beeSAdrian Chadd 	 * buffer (or rx fifo). This can incorrectly acknowledge packets
33108a2a6beeSAdrian Chadd 	 * to a sender if last desc is self-linked.
33118a2a6beeSAdrian Chadd 	 */
33125591b213SSam Leffler 	ds = bf->bf_desc;
33138a2a6beeSAdrian Chadd 	if (sc->sc_rxslink)
331404e22a02SSam Leffler 		ds->ds_link = bf->bf_daddr;	/* link to self */
33158a2a6beeSAdrian Chadd 	else
33168a2a6beeSAdrian Chadd 		ds->ds_link = 0;		/* terminate the list */
33175591b213SSam Leffler 	ds->ds_data = bf->bf_segs[0].ds_addr;
33185591b213SSam Leffler 	ath_hal_setuprxdesc(ah, ds
33195591b213SSam Leffler 		, m->m_len		/* buffer size */
33205591b213SSam Leffler 		, 0
33215591b213SSam Leffler 	);
33225591b213SSam Leffler 
33235591b213SSam Leffler 	if (sc->sc_rxlink != NULL)
33245591b213SSam Leffler 		*sc->sc_rxlink = bf->bf_daddr;
33255591b213SSam Leffler 	sc->sc_rxlink = &ds->ds_link;
33265591b213SSam Leffler 	return 0;
33275591b213SSam Leffler }
33285591b213SSam Leffler 
3329c42a7b7eSSam Leffler /*
333003ed599aSSam Leffler  * Extend 15-bit time stamp from rx descriptor to
33317b0c77ecSSam Leffler  * a full 64-bit TSF using the specified TSF.
333203ed599aSSam Leffler  */
333303ed599aSSam Leffler static __inline u_int64_t
3334fc4de9b7SAdrian Chadd ath_extend_tsf15(u_int32_t rstamp, u_int64_t tsf)
333503ed599aSSam Leffler {
333603ed599aSSam Leffler 	if ((tsf & 0x7fff) < rstamp)
333703ed599aSSam Leffler 		tsf -= 0x8000;
3338fc4de9b7SAdrian Chadd 
333903ed599aSSam Leffler 	return ((tsf &~ 0x7fff) | rstamp);
334003ed599aSSam Leffler }
334103ed599aSSam Leffler 
334203ed599aSSam Leffler /*
3343fc4de9b7SAdrian Chadd  * Extend 32-bit time stamp from rx descriptor to
3344fc4de9b7SAdrian Chadd  * a full 64-bit TSF using the specified TSF.
3345fc4de9b7SAdrian Chadd  */
3346fc4de9b7SAdrian Chadd static __inline u_int64_t
3347fc4de9b7SAdrian Chadd ath_extend_tsf32(u_int32_t rstamp, u_int64_t tsf)
3348fc4de9b7SAdrian Chadd {
3349fc4de9b7SAdrian Chadd 	u_int32_t tsf_low = tsf & 0xffffffff;
3350fc4de9b7SAdrian Chadd 	u_int64_t tsf64 = (tsf & ~0xffffffffULL) | rstamp;
3351fc4de9b7SAdrian Chadd 
3352fc4de9b7SAdrian Chadd 	if (rstamp > tsf_low && (rstamp - tsf_low > 0x10000000))
3353fc4de9b7SAdrian Chadd 		tsf64 -= 0x100000000ULL;
3354fc4de9b7SAdrian Chadd 
3355fc4de9b7SAdrian Chadd 	if (rstamp < tsf_low && (tsf_low - rstamp > 0x10000000))
3356fc4de9b7SAdrian Chadd 		tsf64 += 0x100000000ULL;
3357fc4de9b7SAdrian Chadd 
3358fc4de9b7SAdrian Chadd 	return tsf64;
3359fc4de9b7SAdrian Chadd }
3360fc4de9b7SAdrian Chadd 
3361fc4de9b7SAdrian Chadd /*
3362fc4de9b7SAdrian Chadd  * Extend the TSF from the RX descriptor to a full 64 bit TSF.
3363fc4de9b7SAdrian Chadd  * Earlier hardware versions only wrote the low 15 bits of the
3364fc4de9b7SAdrian Chadd  * TSF into the RX descriptor; later versions (AR5416 and up)
3365fc4de9b7SAdrian Chadd  * include the 32 bit TSF value.
3366fc4de9b7SAdrian Chadd  */
3367fc4de9b7SAdrian Chadd static __inline u_int64_t
3368fc4de9b7SAdrian Chadd ath_extend_tsf(struct ath_softc *sc, u_int32_t rstamp, u_int64_t tsf)
3369fc4de9b7SAdrian Chadd {
3370fc4de9b7SAdrian Chadd 	if (sc->sc_rxtsf32)
3371fc4de9b7SAdrian Chadd 		return ath_extend_tsf32(rstamp, tsf);
3372fc4de9b7SAdrian Chadd 	else
3373fc4de9b7SAdrian Chadd 		return ath_extend_tsf15(rstamp, tsf);
3374fc4de9b7SAdrian Chadd }
3375fc4de9b7SAdrian Chadd 
3376fc4de9b7SAdrian Chadd /*
3377c42a7b7eSSam Leffler  * Intercept management frames to collect beacon rssi data
3378c42a7b7eSSam Leffler  * and to do ibss merges.
3379c42a7b7eSSam Leffler  */
3380c42a7b7eSSam Leffler static void
3381b032f27cSSam Leffler ath_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
33825463c4a4SSam Leffler 	int subtype, int rssi, int nf)
3383c42a7b7eSSam Leffler {
3384b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
3385b032f27cSSam Leffler 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
3386c42a7b7eSSam Leffler 
3387c42a7b7eSSam Leffler 	/*
3388c42a7b7eSSam Leffler 	 * Call up first so subsequent work can use information
3389c42a7b7eSSam Leffler 	 * potentially stored in the node (e.g. for ibss merge).
3390c42a7b7eSSam Leffler 	 */
33915463c4a4SSam Leffler 	ATH_VAP(vap)->av_recv_mgmt(ni, m, subtype, rssi, nf);
3392c42a7b7eSSam Leffler 	switch (subtype) {
3393c42a7b7eSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON:
3394c42a7b7eSSam Leffler 		/* update rssi statistics for use by the hal */
3395ffa2cab6SSam Leffler 		ATH_RSSI_LPF(sc->sc_halstats.ns_avgbrssi, rssi);
339680d939bfSSam Leffler 		if (sc->sc_syncbeacon &&
3397b032f27cSSam Leffler 		    ni == vap->iv_bss && vap->iv_state == IEEE80211_S_RUN) {
339880d939bfSSam Leffler 			/*
339980d939bfSSam Leffler 			 * Resync beacon timers using the tsf of the beacon
340080d939bfSSam Leffler 			 * frame we just received.
340180d939bfSSam Leffler 			 */
3402b032f27cSSam Leffler 			ath_beacon_config(sc, vap);
340380d939bfSSam Leffler 		}
3404c42a7b7eSSam Leffler 		/* fall thru... */
3405c42a7b7eSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
3406b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_IBSS &&
3407b032f27cSSam Leffler 		    vap->iv_state == IEEE80211_S_RUN) {
34087041d50cSBernhard Schmidt 			uint32_t rstamp = sc->sc_lastrs->rs_tstamp;
3409fc4de9b7SAdrian Chadd 			uint64_t tsf = ath_extend_tsf(sc, rstamp,
34107b0c77ecSSam Leffler 				ath_hal_gettsf64(sc->sc_ah));
3411c42a7b7eSSam Leffler 			/*
3412c42a7b7eSSam Leffler 			 * Handle ibss merge as needed; check the tsf on the
3413c42a7b7eSSam Leffler 			 * frame before attempting the merge.  The 802.11 spec
3414c42a7b7eSSam Leffler 			 * says the station should change it's bssid to match
3415c42a7b7eSSam Leffler 			 * the oldest station with the same ssid, where oldest
3416f818612bSSam Leffler 			 * is determined by the tsf.  Note that hardware
3417f818612bSSam Leffler 			 * reconfiguration happens through callback to
341803ed599aSSam Leffler 			 * ath_newstate as the state machine will go from
341903ed599aSSam Leffler 			 * RUN -> RUN when this happens.
3420c42a7b7eSSam Leffler 			 */
342103ed599aSSam Leffler 			if (le64toh(ni->ni_tstamp.tsf) >= tsf) {
342203ed599aSSam Leffler 				DPRINTF(sc, ATH_DEBUG_STATE,
342333d7d80cSTai-hwa Liang 				    "ibss merge, rstamp %u tsf %ju "
342433d7d80cSTai-hwa Liang 				    "tstamp %ju\n", rstamp, (uintmax_t)tsf,
342533d7d80cSTai-hwa Liang 				    (uintmax_t)ni->ni_tstamp.tsf);
3426641b4d0bSSam Leffler 				(void) ieee80211_ibss_merge(ni);
3427c42a7b7eSSam Leffler 			}
342803ed599aSSam Leffler 		}
3429c42a7b7eSSam Leffler 		break;
3430c42a7b7eSSam Leffler 	}
3431c42a7b7eSSam Leffler }
3432c42a7b7eSSam Leffler 
3433c42a7b7eSSam Leffler /*
3434c42a7b7eSSam Leffler  * Set the default antenna.
3435c42a7b7eSSam Leffler  */
3436c42a7b7eSSam Leffler static void
3437c42a7b7eSSam Leffler ath_setdefantenna(struct ath_softc *sc, u_int antenna)
3438c42a7b7eSSam Leffler {
3439c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3440c42a7b7eSSam Leffler 
3441c42a7b7eSSam Leffler 	/* XXX block beacon interrupts */
3442c42a7b7eSSam Leffler 	ath_hal_setdefantenna(ah, antenna);
3443c42a7b7eSSam Leffler 	if (sc->sc_defant != antenna)
3444c42a7b7eSSam Leffler 		sc->sc_stats.ast_ant_defswitch++;
3445c42a7b7eSSam Leffler 	sc->sc_defant = antenna;
3446c42a7b7eSSam Leffler 	sc->sc_rxotherant = 0;
3447c42a7b7eSSam Leffler }
3448c42a7b7eSSam Leffler 
34495463c4a4SSam Leffler static void
3450b032f27cSSam Leffler ath_rx_tap(struct ifnet *ifp, struct mbuf *m,
345165f9edeeSSam Leffler 	const struct ath_rx_status *rs, u_int64_t tsf, int16_t nf)
34527b0c77ecSSam Leffler {
3453e387d629SSam Leffler #define	CHAN_HT20	htole32(IEEE80211_CHAN_HT20)
3454e387d629SSam Leffler #define	CHAN_HT40U	htole32(IEEE80211_CHAN_HT40U)
3455e387d629SSam Leffler #define	CHAN_HT40D	htole32(IEEE80211_CHAN_HT40D)
345646d4d74cSSam Leffler #define	CHAN_HT		(CHAN_HT20|CHAN_HT40U|CHAN_HT40D)
3457b032f27cSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
345846d4d74cSSam Leffler 	const HAL_RATE_TABLE *rt;
345946d4d74cSSam Leffler 	uint8_t rix;
34607b0c77ecSSam Leffler 
346146d4d74cSSam Leffler 	rt = sc->sc_currates;
346246d4d74cSSam Leffler 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
346346d4d74cSSam Leffler 	rix = rt->rateCodeToIndex[rs->rs_rate];
346468e8e04eSSam Leffler 	sc->sc_rx_th.wr_rate = sc->sc_hwmap[rix].ieeerate;
34657b0c77ecSSam Leffler 	sc->sc_rx_th.wr_flags = sc->sc_hwmap[rix].rxflags;
346646d4d74cSSam Leffler #ifdef AH_SUPPORT_AR5416
3467e387d629SSam Leffler 	sc->sc_rx_th.wr_chan_flags &= ~CHAN_HT;
346846d4d74cSSam Leffler 	if (sc->sc_rx_th.wr_rate & IEEE80211_RATE_MCS) {	/* HT rate */
346959efa8b5SSam Leffler 		struct ieee80211com *ic = ifp->if_l2com;
347059efa8b5SSam Leffler 
3471e387d629SSam Leffler 		if ((rs->rs_flags & HAL_RX_2040) == 0)
3472e387d629SSam Leffler 			sc->sc_rx_th.wr_chan_flags |= CHAN_HT20;
347359efa8b5SSam Leffler 		else if (IEEE80211_IS_CHAN_HT40U(ic->ic_curchan))
3474e387d629SSam Leffler 			sc->sc_rx_th.wr_chan_flags |= CHAN_HT40U;
3475e387d629SSam Leffler 		else
3476e387d629SSam Leffler 			sc->sc_rx_th.wr_chan_flags |= CHAN_HT40D;
347768e8e04eSSam Leffler 		if ((rs->rs_flags & HAL_RX_GI) == 0)
3478e387d629SSam Leffler 			sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
347968e8e04eSSam Leffler 	}
348068e8e04eSSam Leffler #endif
3481fc4de9b7SAdrian Chadd 	sc->sc_rx_th.wr_tsf = htole64(ath_extend_tsf(sc, rs->rs_tstamp, tsf));
348265f9edeeSSam Leffler 	if (rs->rs_status & HAL_RXERR_CRC)
34837b0c77ecSSam Leffler 		sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
34847b0c77ecSSam Leffler 	/* XXX propagate other error flags from descriptor */
34857b0c77ecSSam Leffler 	sc->sc_rx_th.wr_antnoise = nf;
34865463c4a4SSam Leffler 	sc->sc_rx_th.wr_antsignal = nf + rs->rs_rssi;
348765f9edeeSSam Leffler 	sc->sc_rx_th.wr_antenna = rs->rs_antenna;
348846d4d74cSSam Leffler #undef CHAN_HT
3489e387d629SSam Leffler #undef CHAN_HT20
3490e387d629SSam Leffler #undef CHAN_HT40U
3491e387d629SSam Leffler #undef CHAN_HT40D
34927b0c77ecSSam Leffler }
34937b0c77ecSSam Leffler 
34945591b213SSam Leffler static void
3495b032f27cSSam Leffler ath_handle_micerror(struct ieee80211com *ic,
3496b032f27cSSam Leffler 	struct ieee80211_frame *wh, int keyix)
3497b032f27cSSam Leffler {
3498b032f27cSSam Leffler 	struct ieee80211_node *ni;
3499b032f27cSSam Leffler 
3500b032f27cSSam Leffler 	/* XXX recheck MIC to deal w/ chips that lie */
3501b032f27cSSam Leffler 	/* XXX discard MIC errors on !data frames */
3502b032f27cSSam Leffler 	ni = ieee80211_find_rxnode(ic, (const struct ieee80211_frame_min *) wh);
3503b032f27cSSam Leffler 	if (ni != NULL) {
3504b032f27cSSam Leffler 		ieee80211_notify_michael_failure(ni->ni_vap, wh, keyix);
3505b032f27cSSam Leffler 		ieee80211_free_node(ni);
3506b032f27cSSam Leffler 	}
3507b032f27cSSam Leffler }
3508b032f27cSSam Leffler 
3509*96ff485dSAdrian Chadd /*
3510*96ff485dSAdrian Chadd  * Only run the RX proc if it's not already running.
3511*96ff485dSAdrian Chadd  * Since this may get run as part of the reset/flush path,
3512*96ff485dSAdrian Chadd  * the task can't clash with an existing, running tasklet.
3513*96ff485dSAdrian Chadd  */
3514b032f27cSSam Leffler static void
3515*96ff485dSAdrian Chadd ath_rx_tasklet(void *arg, int npending)
3516*96ff485dSAdrian Chadd {
3517*96ff485dSAdrian Chadd 	struct ath_softc *sc = arg;
3518*96ff485dSAdrian Chadd 
3519*96ff485dSAdrian Chadd 	CTR1(ATH_KTR_INTR, "ath_rx_proc: pending=%d", npending);
3520*96ff485dSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RX_PROC, "%s: pending %u\n", __func__, npending);
3521*96ff485dSAdrian Chadd 	ath_rx_proc(sc, 1);
3522*96ff485dSAdrian Chadd }
3523*96ff485dSAdrian Chadd 
3524*96ff485dSAdrian Chadd static void
3525*96ff485dSAdrian Chadd ath_rx_proc(struct ath_softc *sc, int resched)
35265591b213SSam Leffler {
35278cec0ab9SSam Leffler #define	PA2DESC(_sc, _pa) \
3528c42a7b7eSSam Leffler 	((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
3529c42a7b7eSSam Leffler 		((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
35305591b213SSam Leffler 	struct ath_buf *bf;
3531fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
3532b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
35335591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
35345591b213SSam Leffler 	struct ath_desc *ds;
353565f9edeeSSam Leffler 	struct ath_rx_status *rs;
35365591b213SSam Leffler 	struct mbuf *m;
35370a915fadSSam Leffler 	struct ieee80211_node *ni;
3538d7736e13SSam Leffler 	int len, type, ngood;
35395591b213SSam Leffler 	HAL_STATUS status;
35407b0c77ecSSam Leffler 	int16_t nf;
35417b0c77ecSSam Leffler 	u_int64_t tsf;
35428f939e79SAdrian Chadd 	int npkts = 0;
35435591b213SSam Leffler 
3544*96ff485dSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RX_PROC, "%s: called\n", __func__);
3545d7736e13SSam Leffler 	ngood = 0;
354659efa8b5SSam Leffler 	nf = ath_hal_getchannoise(ah, sc->sc_curchan);
354784784be1SSam Leffler 	sc->sc_stats.ast_rx_noise = nf;
35487b0c77ecSSam Leffler 	tsf = ath_hal_gettsf64(ah);
35495591b213SSam Leffler 	do {
35506b349e5aSAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_rxbuf);
35518a2a6beeSAdrian Chadd 		if (sc->sc_rxslink && bf == NULL) {	/* NB: shouldn't happen */
3552c42a7b7eSSam Leffler 			if_printf(ifp, "%s: no buffer!\n", __func__);
35535591b213SSam Leffler 			break;
35548a2a6beeSAdrian Chadd 		} else if (bf == NULL) {
35558a2a6beeSAdrian Chadd 			/*
35568a2a6beeSAdrian Chadd 			 * End of List:
35578a2a6beeSAdrian Chadd 			 * this can happen for non-self-linked RX chains
35588a2a6beeSAdrian Chadd 			 */
35598a2a6beeSAdrian Chadd 			sc->sc_stats.ast_rx_hitqueueend++;
35608a2a6beeSAdrian Chadd 			break;
35615591b213SSam Leffler 		}
3562b2792ff6SSam Leffler 		m = bf->bf_m;
3563b2792ff6SSam Leffler 		if (m == NULL) {		/* NB: shouldn't happen */
3564b2792ff6SSam Leffler 			/*
3565b2792ff6SSam Leffler 			 * If mbuf allocation failed previously there
3566b2792ff6SSam Leffler 			 * will be no mbuf; try again to re-populate it.
3567b2792ff6SSam Leffler 			 */
3568b2792ff6SSam Leffler 			/* XXX make debug msg */
3569b2792ff6SSam Leffler 			if_printf(ifp, "%s: no mbuf!\n", __func__);
35706b349e5aSAdrian Chadd 			TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
3571b2792ff6SSam Leffler 			goto rx_next;
3572b2792ff6SSam Leffler 		}
357304e22a02SSam Leffler 		ds = bf->bf_desc;
357404e22a02SSam Leffler 		if (ds->ds_link == bf->bf_daddr) {
357504e22a02SSam Leffler 			/* NB: never process the self-linked entry at the end */
3576f77057dbSAdrian Chadd 			sc->sc_stats.ast_rx_hitqueueend++;
357704e22a02SSam Leffler 			break;
357804e22a02SSam Leffler 		}
35798cec0ab9SSam Leffler 		/* XXX sync descriptor memory */
35808cec0ab9SSam Leffler 		/*
35818cec0ab9SSam Leffler 		 * Must provide the virtual address of the current
35828cec0ab9SSam Leffler 		 * descriptor, the physical address, and the virtual
35838cec0ab9SSam Leffler 		 * address of the next descriptor in the h/w chain.
35848cec0ab9SSam Leffler 		 * This allows the HAL to look ahead to see if the
35858cec0ab9SSam Leffler 		 * hardware is done with a descriptor by checking the
35868cec0ab9SSam Leffler 		 * done bit in the following descriptor and the address
35878cec0ab9SSam Leffler 		 * of the current descriptor the DMA engine is working
35888cec0ab9SSam Leffler 		 * on.  All this is necessary because of our use of
35898cec0ab9SSam Leffler 		 * a self-linked list to avoid rx overruns.
35908cec0ab9SSam Leffler 		 */
359165f9edeeSSam Leffler 		rs = &bf->bf_status.ds_rxstat;
35928cec0ab9SSam Leffler 		status = ath_hal_rxprocdesc(ah, ds,
359365f9edeeSSam Leffler 				bf->bf_daddr, PA2DESC(sc, ds->ds_link), rs);
3594a585a9a1SSam Leffler #ifdef ATH_DEBUG
3595c42a7b7eSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_RECV_DESC)
35966902009eSSam Leffler 			ath_printrxbuf(sc, bf, 0, status == HAL_OK);
35975591b213SSam Leffler #endif
35985591b213SSam Leffler 		if (status == HAL_EINPROGRESS)
35995591b213SSam Leffler 			break;
36006b349e5aSAdrian Chadd 
36016b349e5aSAdrian Chadd 		TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
36028f939e79SAdrian Chadd 		npkts++;
3603f9aa1d90SAdrian Chadd 
3604f9aa1d90SAdrian Chadd 		/* These aren't specifically errors */
3605f9aa1d90SAdrian Chadd 		if (rs->rs_flags & HAL_RX_GI)
3606f9aa1d90SAdrian Chadd 			sc->sc_stats.ast_rx_halfgi++;
3607f9aa1d90SAdrian Chadd 		if (rs->rs_flags & HAL_RX_2040)
3608f9aa1d90SAdrian Chadd 			sc->sc_stats.ast_rx_2040++;
3609f9aa1d90SAdrian Chadd 		if (rs->rs_flags & HAL_RX_DELIM_CRC_PRE)
3610f9aa1d90SAdrian Chadd 			sc->sc_stats.ast_rx_pre_crc_err++;
3611f9aa1d90SAdrian Chadd 		if (rs->rs_flags & HAL_RX_DELIM_CRC_POST)
3612f9aa1d90SAdrian Chadd 			sc->sc_stats.ast_rx_post_crc_err++;
3613f9aa1d90SAdrian Chadd 		if (rs->rs_flags & HAL_RX_DECRYPT_BUSY)
3614f9aa1d90SAdrian Chadd 			sc->sc_stats.ast_rx_decrypt_busy_err++;
3615f9aa1d90SAdrian Chadd 		if (rs->rs_flags & HAL_RX_HI_RX_CHAIN)
3616f9aa1d90SAdrian Chadd 			sc->sc_stats.ast_rx_hi_rx_chain++;
3617f9aa1d90SAdrian Chadd 
361868e8e04eSSam Leffler 		if (rs->rs_status != 0) {
361965f9edeeSSam Leffler 			if (rs->rs_status & HAL_RXERR_CRC)
36205591b213SSam Leffler 				sc->sc_stats.ast_rx_crcerr++;
362165f9edeeSSam Leffler 			if (rs->rs_status & HAL_RXERR_FIFO)
36225591b213SSam Leffler 				sc->sc_stats.ast_rx_fifoerr++;
362365f9edeeSSam Leffler 			if (rs->rs_status & HAL_RXERR_PHY) {
36245591b213SSam Leffler 				sc->sc_stats.ast_rx_phyerr++;
362548237774SAdrian Chadd 				/* Process DFS radar events */
3626373815efSAdrian Chadd 				if ((rs->rs_phyerr == HAL_PHYERR_RADAR) ||
3627373815efSAdrian Chadd 				    (rs->rs_phyerr == HAL_PHYERR_FALSE_RADAR_EXT)) {
3628373815efSAdrian Chadd 					/* Since we're touching the frame data, sync it */
3629373815efSAdrian Chadd 					bus_dmamap_sync(sc->sc_dmat,
3630373815efSAdrian Chadd 					    bf->bf_dmamap,
3631373815efSAdrian Chadd 					    BUS_DMASYNC_POSTREAD);
3632373815efSAdrian Chadd 					/* Now pass it to the radar processing code */
36337e5eb44dSAdrian Chadd 					ath_dfs_process_phy_err(sc, mtod(m, char *), tsf, rs);
3634373815efSAdrian Chadd 				}
363548237774SAdrian Chadd 
3636f9aa1d90SAdrian Chadd 				/* Be suitably paranoid about receiving phy errors out of the stats array bounds */
3637f9aa1d90SAdrian Chadd 				if (rs->rs_phyerr < 64)
3638f9aa1d90SAdrian Chadd 					sc->sc_stats.ast_rx_phy[rs->rs_phyerr]++;
363968e8e04eSSam Leffler 				goto rx_error;	/* NB: don't count in ierrors */
3640c42a7b7eSSam Leffler 			}
364165f9edeeSSam Leffler 			if (rs->rs_status & HAL_RXERR_DECRYPT) {
364285643802SSam Leffler 				/*
3643c42a7b7eSSam Leffler 				 * Decrypt error.  If the error occurred
3644c42a7b7eSSam Leffler 				 * because there was no hardware key, then
3645c42a7b7eSSam Leffler 				 * let the frame through so the upper layers
3646c42a7b7eSSam Leffler 				 * can process it.  This is necessary for 5210
3647c42a7b7eSSam Leffler 				 * parts which have no way to setup a ``clear''
3648c42a7b7eSSam Leffler 				 * key cache entry.
3649c42a7b7eSSam Leffler 				 *
3650c42a7b7eSSam Leffler 				 * XXX do key cache faulting
365185643802SSam Leffler 				 */
365265f9edeeSSam Leffler 				if (rs->rs_keyix == HAL_RXKEYIX_INVALID)
3653c42a7b7eSSam Leffler 					goto rx_accept;
3654c42a7b7eSSam Leffler 				sc->sc_stats.ast_rx_badcrypt++;
36555591b213SSam Leffler 			}
365665f9edeeSSam Leffler 			if (rs->rs_status & HAL_RXERR_MIC) {
3657c42a7b7eSSam Leffler 				sc->sc_stats.ast_rx_badmic++;
3658c42a7b7eSSam Leffler 				/*
3659c42a7b7eSSam Leffler 				 * Do minimal work required to hand off
36605463c4a4SSam Leffler 				 * the 802.11 header for notification.
3661c42a7b7eSSam Leffler 				 */
3662c42a7b7eSSam Leffler 				/* XXX frag's and qos frames */
366365f9edeeSSam Leffler 				len = rs->rs_datalen;
3664c42a7b7eSSam Leffler 				if (len >= sizeof (struct ieee80211_frame)) {
3665c42a7b7eSSam Leffler 					bus_dmamap_sync(sc->sc_dmat,
3666c42a7b7eSSam Leffler 					    bf->bf_dmamap,
3667c42a7b7eSSam Leffler 					    BUS_DMASYNC_POSTREAD);
3668b032f27cSSam Leffler 					ath_handle_micerror(ic,
3669c42a7b7eSSam Leffler 					    mtod(m, struct ieee80211_frame *),
36700ab4040aSSam Leffler 					    sc->sc_splitmic ?
3671b032f27cSSam Leffler 						rs->rs_keyix-32 : rs->rs_keyix);
3672c42a7b7eSSam Leffler 				}
3673c42a7b7eSSam Leffler 			}
3674c42a7b7eSSam Leffler 			ifp->if_ierrors++;
367568e8e04eSSam Leffler rx_error:
367668e8e04eSSam Leffler 			/*
367768e8e04eSSam Leffler 			 * Cleanup any pending partial frame.
367868e8e04eSSam Leffler 			 */
367968e8e04eSSam Leffler 			if (sc->sc_rxpending != NULL) {
368068e8e04eSSam Leffler 				m_freem(sc->sc_rxpending);
368168e8e04eSSam Leffler 				sc->sc_rxpending = NULL;
368268e8e04eSSam Leffler 			}
3683c42a7b7eSSam Leffler 			/*
36847b0c77ecSSam Leffler 			 * When a tap is present pass error frames
36857b0c77ecSSam Leffler 			 * that have been requested.  By default we
36867b0c77ecSSam Leffler 			 * pass decrypt+mic errors but others may be
36877b0c77ecSSam Leffler 			 * interesting (e.g. crc).
3688c42a7b7eSSam Leffler 			 */
36895463c4a4SSam Leffler 			if (ieee80211_radiotap_active(ic) &&
369065f9edeeSSam Leffler 			    (rs->rs_status & sc->sc_monpass)) {
36917b0c77ecSSam Leffler 				bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
36927b0c77ecSSam Leffler 				    BUS_DMASYNC_POSTREAD);
36937b0c77ecSSam Leffler 				/* NB: bpf needs the mbuf length setup */
369465f9edeeSSam Leffler 				len = rs->rs_datalen;
36957b0c77ecSSam Leffler 				m->m_pkthdr.len = m->m_len = len;
3696dcfd99a7SAdrian Chadd 				bf->bf_m = NULL;
36975463c4a4SSam Leffler 				ath_rx_tap(ifp, m, rs, tsf, nf);
36985463c4a4SSam Leffler 				ieee80211_radiotap_rx_all(ic, m);
3699dcfd99a7SAdrian Chadd 				m_freem(m);
37007b0c77ecSSam Leffler 			}
37017b0c77ecSSam Leffler 			/* XXX pass MIC errors up for s/w reclaculation */
37025591b213SSam Leffler 			goto rx_next;
37035591b213SSam Leffler 		}
3704c42a7b7eSSam Leffler rx_accept:
3705c42a7b7eSSam Leffler 		/*
3706c42a7b7eSSam Leffler 		 * Sync and unmap the frame.  At this point we're
3707c42a7b7eSSam Leffler 		 * committed to passing the mbuf somewhere so clear
3708c66c48cbSSam Leffler 		 * bf_m; this means a new mbuf must be allocated
3709c42a7b7eSSam Leffler 		 * when the rx descriptor is setup again to receive
3710c42a7b7eSSam Leffler 		 * another frame.
3711c42a7b7eSSam Leffler 		 */
37125591b213SSam Leffler 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
37135591b213SSam Leffler 		    BUS_DMASYNC_POSTREAD);
37145591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
37155591b213SSam Leffler 		bf->bf_m = NULL;
3716c42a7b7eSSam Leffler 
371765f9edeeSSam Leffler 		len = rs->rs_datalen;
371868e8e04eSSam Leffler 		m->m_len = len;
371968e8e04eSSam Leffler 
372068e8e04eSSam Leffler 		if (rs->rs_more) {
372168e8e04eSSam Leffler 			/*
372268e8e04eSSam Leffler 			 * Frame spans multiple descriptors; save
372368e8e04eSSam Leffler 			 * it for the next completed descriptor, it
372468e8e04eSSam Leffler 			 * will be used to construct a jumbogram.
372568e8e04eSSam Leffler 			 */
372668e8e04eSSam Leffler 			if (sc->sc_rxpending != NULL) {
372768e8e04eSSam Leffler 				/* NB: max frame size is currently 2 clusters */
372868e8e04eSSam Leffler 				sc->sc_stats.ast_rx_toobig++;
372968e8e04eSSam Leffler 				m_freem(sc->sc_rxpending);
373068e8e04eSSam Leffler 			}
373168e8e04eSSam Leffler 			m->m_pkthdr.rcvif = ifp;
373268e8e04eSSam Leffler 			m->m_pkthdr.len = len;
373368e8e04eSSam Leffler 			sc->sc_rxpending = m;
373468e8e04eSSam Leffler 			goto rx_next;
373568e8e04eSSam Leffler 		} else if (sc->sc_rxpending != NULL) {
373668e8e04eSSam Leffler 			/*
373768e8e04eSSam Leffler 			 * This is the second part of a jumbogram,
373868e8e04eSSam Leffler 			 * chain it to the first mbuf, adjust the
373968e8e04eSSam Leffler 			 * frame length, and clear the rxpending state.
374068e8e04eSSam Leffler 			 */
374168e8e04eSSam Leffler 			sc->sc_rxpending->m_next = m;
374268e8e04eSSam Leffler 			sc->sc_rxpending->m_pkthdr.len += len;
374368e8e04eSSam Leffler 			m = sc->sc_rxpending;
374468e8e04eSSam Leffler 			sc->sc_rxpending = NULL;
374568e8e04eSSam Leffler 		} else {
374668e8e04eSSam Leffler 			/*
374768e8e04eSSam Leffler 			 * Normal single-descriptor receive; setup
374868e8e04eSSam Leffler 			 * the rcvif and packet length.
374968e8e04eSSam Leffler 			 */
375068e8e04eSSam Leffler 			m->m_pkthdr.rcvif = ifp;
375168e8e04eSSam Leffler 			m->m_pkthdr.len = len;
375268e8e04eSSam Leffler 		}
375373454c73SSam Leffler 
3754b032f27cSSam Leffler 		ifp->if_ipackets++;
375565f9edeeSSam Leffler 		sc->sc_stats.ast_ant_rx[rs->rs_antenna]++;
3756c42a7b7eSSam Leffler 
37575463c4a4SSam Leffler 		/*
37585463c4a4SSam Leffler 		 * Populate the rx status block.  When there are bpf
37595463c4a4SSam Leffler 		 * listeners we do the additional work to provide
37605463c4a4SSam Leffler 		 * complete status.  Otherwise we fill in only the
37615463c4a4SSam Leffler 		 * material required by ieee80211_input.  Note that
37625463c4a4SSam Leffler 		 * noise setting is filled in above.
37635463c4a4SSam Leffler 		 */
37645463c4a4SSam Leffler 		if (ieee80211_radiotap_active(ic))
37655463c4a4SSam Leffler 			ath_rx_tap(ifp, m, rs, tsf, nf);
37660a915fadSSam Leffler 
37675591b213SSam Leffler 		/*
3768c42a7b7eSSam Leffler 		 * From this point on we assume the frame is at least
3769c42a7b7eSSam Leffler 		 * as large as ieee80211_frame_min; verify that.
37705591b213SSam Leffler 		 */
3771c42a7b7eSSam Leffler 		if (len < IEEE80211_MIN_LEN) {
37725463c4a4SSam Leffler 			if (!ieee80211_radiotap_active(ic)) {
37735463c4a4SSam Leffler 				DPRINTF(sc, ATH_DEBUG_RECV,
37745463c4a4SSam Leffler 				    "%s: short packet %d\n", __func__, len);
3775c42a7b7eSSam Leffler 				sc->sc_stats.ast_rx_tooshort++;
37765463c4a4SSam Leffler 			} else {
37775463c4a4SSam Leffler 				/* NB: in particular this captures ack's */
37785463c4a4SSam Leffler 				ieee80211_radiotap_rx_all(ic, m);
37795463c4a4SSam Leffler 			}
3780c42a7b7eSSam Leffler 			m_freem(m);
3781c42a7b7eSSam Leffler 			goto rx_next;
37825591b213SSam Leffler 		}
37830a915fadSSam Leffler 
3784c42a7b7eSSam Leffler 		if (IFF_DUMPPKTS(sc, ATH_DEBUG_RECV)) {
378546d4d74cSSam Leffler 			const HAL_RATE_TABLE *rt = sc->sc_currates;
378646d4d74cSSam Leffler 			uint8_t rix = rt->rateCodeToIndex[rs->rs_rate];
378746d4d74cSSam Leffler 
378868e8e04eSSam Leffler 			ieee80211_dump_pkt(ic, mtod(m, caddr_t), len,
378946d4d74cSSam Leffler 			    sc->sc_hwmap[rix].ieeerate, rs->rs_rssi);
3790c42a7b7eSSam Leffler 		}
3791c42a7b7eSSam Leffler 
3792c42a7b7eSSam Leffler 		m_adj(m, -IEEE80211_CRC_LEN);
3793de5af704SSam Leffler 
3794de5af704SSam Leffler 		/*
3795c42a7b7eSSam Leffler 		 * Locate the node for sender, track state, and then
3796c42a7b7eSSam Leffler 		 * pass the (referenced) node up to the 802.11 layer
3797c42a7b7eSSam Leffler 		 * for its use.
3798c42a7b7eSSam Leffler 		 */
3799c1225b52SSam Leffler 		ni = ieee80211_find_rxnode_withkey(ic,
3800c1225b52SSam Leffler 			mtod(m, const struct ieee80211_frame_min *),
380165f9edeeSSam Leffler 			rs->rs_keyix == HAL_RXKEYIX_INVALID ?
380265f9edeeSSam Leffler 				IEEE80211_KEYIX_NONE : rs->rs_keyix);
38037041d50cSBernhard Schmidt 		sc->sc_lastrs = rs;
3804a07e9ddbSAdrian Chadd 
3805a07e9ddbSAdrian Chadd 		if (rs->rs_isaggr)
3806a07e9ddbSAdrian Chadd 			sc->sc_stats.ast_rx_agg++;
3807a07e9ddbSAdrian Chadd 
3808a07e9ddbSAdrian Chadd 		if (ni != NULL) {
3809b032f27cSSam Leffler 			/*
3810e57539afSAdrian Chadd  			 * Only punt packets for ampdu reorder processing for
3811e57539afSAdrian Chadd 			 * 11n nodes; net80211 enforces that M_AMPDU is only
3812e57539afSAdrian Chadd 			 * set for 11n nodes.
381300fc8705SAdrian Chadd  			 */
381400fc8705SAdrian Chadd 			if (ni->ni_flags & IEEE80211_NODE_HT)
381500fc8705SAdrian Chadd 				m->m_flags |= M_AMPDU;
381600fc8705SAdrian Chadd 
381700fc8705SAdrian Chadd 			/*
3818b032f27cSSam Leffler 			 * Sending station is known, dispatch directly.
3819b032f27cSSam Leffler 			 */
38205463c4a4SSam Leffler 			type = ieee80211_input(ni, m, rs->rs_rssi, nf);
3821b032f27cSSam Leffler 			ieee80211_free_node(ni);
3822b032f27cSSam Leffler 			/*
3823b032f27cSSam Leffler 			 * Arrange to update the last rx timestamp only for
3824b032f27cSSam Leffler 			 * frames from our ap when operating in station mode.
3825b032f27cSSam Leffler 			 * This assumes the rx key is always setup when
3826b032f27cSSam Leffler 			 * associated.
3827b032f27cSSam Leffler 			 */
3828b032f27cSSam Leffler 			if (ic->ic_opmode == IEEE80211_M_STA &&
3829b032f27cSSam Leffler 			    rs->rs_keyix != HAL_RXKEYIX_INVALID)
3830b032f27cSSam Leffler 				ngood++;
3831b032f27cSSam Leffler 		} else {
38325463c4a4SSam Leffler 			type = ieee80211_input_all(ic, m, rs->rs_rssi, nf);
3833b032f27cSSam Leffler 		}
3834c42a7b7eSSam Leffler 		/*
3835c42a7b7eSSam Leffler 		 * Track rx rssi and do any rx antenna management.
3836de5af704SSam Leffler 		 */
383765f9edeeSSam Leffler 		ATH_RSSI_LPF(sc->sc_halstats.ns_avgrssi, rs->rs_rssi);
3838c42a7b7eSSam Leffler 		if (sc->sc_diversity) {
3839c42a7b7eSSam Leffler 			/*
3840c42a7b7eSSam Leffler 			 * When using fast diversity, change the default rx
3841c42a7b7eSSam Leffler 			 * antenna if diversity chooses the other antenna 3
3842c42a7b7eSSam Leffler 			 * times in a row.
3843c42a7b7eSSam Leffler 			 */
384465f9edeeSSam Leffler 			if (sc->sc_defant != rs->rs_antenna) {
3845c42a7b7eSSam Leffler 				if (++sc->sc_rxotherant >= 3)
384665f9edeeSSam Leffler 					ath_setdefantenna(sc, rs->rs_antenna);
3847c42a7b7eSSam Leffler 			} else
3848c42a7b7eSSam Leffler 				sc->sc_rxotherant = 0;
3849c42a7b7eSSam Leffler 		}
3850235ab70eSAdrian Chadd 
3851235ab70eSAdrian Chadd 		/* Newer school diversity - kite specific for now */
3852235ab70eSAdrian Chadd 		/* XXX perhaps migrate the normal diversity code to this? */
3853235ab70eSAdrian Chadd 		if ((ah)->ah_rxAntCombDiversity)
3854235ab70eSAdrian Chadd 			(*(ah)->ah_rxAntCombDiversity)(ah, rs, ticks, hz);
3855235ab70eSAdrian Chadd 
38563e50ec2cSSam Leffler 		if (sc->sc_softled) {
38573e50ec2cSSam Leffler 			/*
38583e50ec2cSSam Leffler 			 * Blink for any data frame.  Otherwise do a
38593e50ec2cSSam Leffler 			 * heartbeat-style blink when idle.  The latter
38603e50ec2cSSam Leffler 			 * is mainly for station mode where we depend on
38613e50ec2cSSam Leffler 			 * periodic beacon frames to trigger the poll event.
38623e50ec2cSSam Leffler 			 */
386331640eb7SSam Leffler 			if (type == IEEE80211_FC0_TYPE_DATA) {
386446d4d74cSSam Leffler 				const HAL_RATE_TABLE *rt = sc->sc_currates;
386546d4d74cSSam Leffler 				ath_led_event(sc,
386646d4d74cSSam Leffler 				    rt->rateCodeToIndex[rs->rs_rate]);
38673e50ec2cSSam Leffler 			} else if (ticks - sc->sc_ledevent >= sc->sc_ledidle)
386846d4d74cSSam Leffler 				ath_led_event(sc, 0);
38693e50ec2cSSam Leffler 		}
38705591b213SSam Leffler rx_next:
38716b349e5aSAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
38725591b213SSam Leffler 	} while (ath_rxbuf_init(sc, bf) == 0);
38735591b213SSam Leffler 
3874c42a7b7eSSam Leffler 	/* rx signal state monitoring */
387559efa8b5SSam Leffler 	ath_hal_rxmonitor(ah, &sc->sc_halstats, sc->sc_curchan);
3876d7736e13SSam Leffler 	if (ngood)
3877d7736e13SSam Leffler 		sc->sc_lastrx = tsf;
3878b5f4adb3SSam Leffler 
387948237774SAdrian Chadd 	/* Queue DFS tasklet if needed */
3880*96ff485dSAdrian Chadd 	if (resched && ath_dfs_tasklet_needed(sc, sc->sc_curchan))
388148237774SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_dfstask);
388248237774SAdrian Chadd 
38831fdadc0fSAdrian Chadd 	/*
38841fdadc0fSAdrian Chadd 	 * Now that all the RX frames were handled that
38851fdadc0fSAdrian Chadd 	 * need to be handled, kick the PCU if there's
38861fdadc0fSAdrian Chadd 	 * been an RXEOL condition.
38871fdadc0fSAdrian Chadd 	 */
3888*96ff485dSAdrian Chadd 	if (resched && sc->sc_kickpcu) {
38898f939e79SAdrian Chadd 		device_printf(sc->sc_dev, "%s: kickpcu; handled %d packets\n",
38908f939e79SAdrian Chadd 		    __func__, npkts);
38918f939e79SAdrian Chadd 
38928f939e79SAdrian Chadd 		/* XXX rxslink? */
38938f939e79SAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_rxbuf);
38948f939e79SAdrian Chadd 		ath_hal_putrxbuf(ah, bf->bf_daddr);
38958f939e79SAdrian Chadd 		ath_hal_rxena(ah);		/* enable recv descriptors */
38968f939e79SAdrian Chadd 		ath_mode_init(sc);		/* set filters, etc. */
38978f939e79SAdrian Chadd 		ath_hal_startpcurecv(ah);	/* re-enable PCU/DMA engine */
38988f939e79SAdrian Chadd 
38998f939e79SAdrian Chadd 		ATH_LOCK(sc);
39001fdadc0fSAdrian Chadd 		ath_hal_intrset(ah, sc->sc_imask);
39018f939e79SAdrian Chadd 		sc->sc_kickpcu = 0;
39028f939e79SAdrian Chadd 		ATH_UNLOCK(sc);
39031fdadc0fSAdrian Chadd 	}
39041fdadc0fSAdrian Chadd 
3905*96ff485dSAdrian Chadd 	if (resched && (ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0) {
3906339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
390704f19fd6SSam Leffler 		ieee80211_ff_age_all(ic, 100);
3908339ccfb3SSam Leffler #endif
3909339ccfb3SSam Leffler 		if (!IFQ_IS_EMPTY(&ifp->if_snd))
3910cd196bb2SSam Leffler 			ath_start(ifp);
3911339ccfb3SSam Leffler 	}
39128cec0ab9SSam Leffler #undef PA2DESC
39135591b213SSam Leffler }
39145591b213SSam Leffler 
3915622b3fd2SSam Leffler static void
3916622b3fd2SSam Leffler ath_txq_init(struct ath_softc *sc, struct ath_txq *txq, int qnum)
3917622b3fd2SSam Leffler {
3918622b3fd2SSam Leffler 	txq->axq_qnum = qnum;
3919339ccfb3SSam Leffler 	txq->axq_ac = 0;
3920622b3fd2SSam Leffler 	txq->axq_depth = 0;
3921622b3fd2SSam Leffler 	txq->axq_intrcnt = 0;
3922622b3fd2SSam Leffler 	txq->axq_link = NULL;
39236b349e5aSAdrian Chadd 	txq->axq_softc = sc;
39246b349e5aSAdrian Chadd 	TAILQ_INIT(&txq->axq_q);
39256b349e5aSAdrian Chadd 	TAILQ_INIT(&txq->axq_tidq);
3926622b3fd2SSam Leffler 	ATH_TXQ_LOCK_INIT(sc, txq);
3927622b3fd2SSam Leffler }
3928622b3fd2SSam Leffler 
39295591b213SSam Leffler /*
3930c42a7b7eSSam Leffler  * Setup a h/w transmit queue.
39315591b213SSam Leffler  */
3932c42a7b7eSSam Leffler static struct ath_txq *
3933c42a7b7eSSam Leffler ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
3934c42a7b7eSSam Leffler {
3935c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
3936c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3937c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
3938c42a7b7eSSam Leffler 	int qnum;
3939c42a7b7eSSam Leffler 
3940c42a7b7eSSam Leffler 	memset(&qi, 0, sizeof(qi));
3941c42a7b7eSSam Leffler 	qi.tqi_subtype = subtype;
3942c42a7b7eSSam Leffler 	qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
3943c42a7b7eSSam Leffler 	qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
3944c42a7b7eSSam Leffler 	qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
3945c42a7b7eSSam Leffler 	/*
3946c42a7b7eSSam Leffler 	 * Enable interrupts only for EOL and DESC conditions.
3947c42a7b7eSSam Leffler 	 * We mark tx descriptors to receive a DESC interrupt
3948c42a7b7eSSam Leffler 	 * when a tx queue gets deep; otherwise waiting for the
3949c42a7b7eSSam Leffler 	 * EOL to reap descriptors.  Note that this is done to
3950c42a7b7eSSam Leffler 	 * reduce interrupt load and this only defers reaping
3951c42a7b7eSSam Leffler 	 * descriptors, never transmitting frames.  Aside from
3952c42a7b7eSSam Leffler 	 * reducing interrupts this also permits more concurrency.
3953c42a7b7eSSam Leffler 	 * The only potential downside is if the tx queue backs
3954c42a7b7eSSam Leffler 	 * up in which case the top half of the kernel may backup
3955c42a7b7eSSam Leffler 	 * due to a lack of tx descriptors.
3956c42a7b7eSSam Leffler 	 */
3957bd5a9920SSam Leffler 	qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE | HAL_TXQ_TXDESCINT_ENABLE;
3958c42a7b7eSSam Leffler 	qnum = ath_hal_setuptxqueue(ah, qtype, &qi);
3959c42a7b7eSSam Leffler 	if (qnum == -1) {
3960c42a7b7eSSam Leffler 		/*
3961c42a7b7eSSam Leffler 		 * NB: don't print a message, this happens
3962a614e076SSam Leffler 		 * normally on parts with too few tx queues
3963c42a7b7eSSam Leffler 		 */
3964c42a7b7eSSam Leffler 		return NULL;
3965c42a7b7eSSam Leffler 	}
3966c42a7b7eSSam Leffler 	if (qnum >= N(sc->sc_txq)) {
39676891c875SPeter Wemm 		device_printf(sc->sc_dev,
39686891c875SPeter Wemm 			"hal qnum %u out of range, max %zu!\n",
3969c42a7b7eSSam Leffler 			qnum, N(sc->sc_txq));
3970c42a7b7eSSam Leffler 		ath_hal_releasetxqueue(ah, qnum);
3971c42a7b7eSSam Leffler 		return NULL;
3972c42a7b7eSSam Leffler 	}
3973c42a7b7eSSam Leffler 	if (!ATH_TXQ_SETUP(sc, qnum)) {
3974622b3fd2SSam Leffler 		ath_txq_init(sc, &sc->sc_txq[qnum], qnum);
3975c42a7b7eSSam Leffler 		sc->sc_txqsetup |= 1<<qnum;
3976c42a7b7eSSam Leffler 	}
3977c42a7b7eSSam Leffler 	return &sc->sc_txq[qnum];
3978c42a7b7eSSam Leffler #undef N
3979c42a7b7eSSam Leffler }
3980c42a7b7eSSam Leffler 
3981c42a7b7eSSam Leffler /*
3982c42a7b7eSSam Leffler  * Setup a hardware data transmit queue for the specified
3983c42a7b7eSSam Leffler  * access control.  The hal may not support all requested
3984c42a7b7eSSam Leffler  * queues in which case it will return a reference to a
3985c42a7b7eSSam Leffler  * previously setup queue.  We record the mapping from ac's
3986c42a7b7eSSam Leffler  * to h/w queues for use by ath_tx_start and also track
3987c42a7b7eSSam Leffler  * the set of h/w queues being used to optimize work in the
3988c42a7b7eSSam Leffler  * transmit interrupt handler and related routines.
3989c42a7b7eSSam Leffler  */
3990c42a7b7eSSam Leffler static int
3991c42a7b7eSSam Leffler ath_tx_setup(struct ath_softc *sc, int ac, int haltype)
3992c42a7b7eSSam Leffler {
3993c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
3994c42a7b7eSSam Leffler 	struct ath_txq *txq;
3995c42a7b7eSSam Leffler 
3996c42a7b7eSSam Leffler 	if (ac >= N(sc->sc_ac2q)) {
39976891c875SPeter Wemm 		device_printf(sc->sc_dev, "AC %u out of range, max %zu!\n",
3998c42a7b7eSSam Leffler 			ac, N(sc->sc_ac2q));
3999c42a7b7eSSam Leffler 		return 0;
4000c42a7b7eSSam Leffler 	}
4001c42a7b7eSSam Leffler 	txq = ath_txq_setup(sc, HAL_TX_QUEUE_DATA, haltype);
4002c42a7b7eSSam Leffler 	if (txq != NULL) {
4003339ccfb3SSam Leffler 		txq->axq_ac = ac;
4004c42a7b7eSSam Leffler 		sc->sc_ac2q[ac] = txq;
4005c42a7b7eSSam Leffler 		return 1;
4006c42a7b7eSSam Leffler 	} else
4007c42a7b7eSSam Leffler 		return 0;
4008c42a7b7eSSam Leffler #undef N
4009c42a7b7eSSam Leffler }
4010c42a7b7eSSam Leffler 
4011c42a7b7eSSam Leffler /*
4012c42a7b7eSSam Leffler  * Update WME parameters for a transmit queue.
4013c42a7b7eSSam Leffler  */
4014c42a7b7eSSam Leffler static int
4015c42a7b7eSSam Leffler ath_txq_update(struct ath_softc *sc, int ac)
4016c42a7b7eSSam Leffler {
4017c42a7b7eSSam Leffler #define	ATH_EXPONENT_TO_VALUE(v)	((1<<v)-1)
4018c42a7b7eSSam Leffler #define	ATH_TXOP_TO_US(v)		(v<<5)
4019b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
4020b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
4021c42a7b7eSSam Leffler 	struct ath_txq *txq = sc->sc_ac2q[ac];
4022c42a7b7eSSam Leffler 	struct wmeParams *wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
4023c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4024c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
4025c42a7b7eSSam Leffler 
4026c42a7b7eSSam Leffler 	ath_hal_gettxqueueprops(ah, txq->axq_qnum, &qi);
4027584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
402810ad9a77SSam Leffler 	if (sc->sc_tdma) {
402910ad9a77SSam Leffler 		/*
403010ad9a77SSam Leffler 		 * AIFS is zero so there's no pre-transmit wait.  The
403110ad9a77SSam Leffler 		 * burst time defines the slot duration and is configured
403209be6601SSam Leffler 		 * through net80211.  The QCU is setup to not do post-xmit
403310ad9a77SSam Leffler 		 * back off, lockout all lower-priority QCU's, and fire
403410ad9a77SSam Leffler 		 * off the DMA beacon alert timer which is setup based
403510ad9a77SSam Leffler 		 * on the slot configuration.
403610ad9a77SSam Leffler 		 */
403710ad9a77SSam Leffler 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
403810ad9a77SSam Leffler 			      | HAL_TXQ_TXERRINT_ENABLE
403910ad9a77SSam Leffler 			      | HAL_TXQ_TXURNINT_ENABLE
404010ad9a77SSam Leffler 			      | HAL_TXQ_TXEOLINT_ENABLE
404110ad9a77SSam Leffler 			      | HAL_TXQ_DBA_GATED
404210ad9a77SSam Leffler 			      | HAL_TXQ_BACKOFF_DISABLE
404310ad9a77SSam Leffler 			      | HAL_TXQ_ARB_LOCKOUT_GLOBAL
404410ad9a77SSam Leffler 			      ;
404510ad9a77SSam Leffler 		qi.tqi_aifs = 0;
404610ad9a77SSam Leffler 		/* XXX +dbaprep? */
404710ad9a77SSam Leffler 		qi.tqi_readyTime = sc->sc_tdmaslotlen;
404810ad9a77SSam Leffler 		qi.tqi_burstTime = qi.tqi_readyTime;
404910ad9a77SSam Leffler 	} else {
405010ad9a77SSam Leffler #endif
405110ad9a77SSam Leffler 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
405210ad9a77SSam Leffler 			      | HAL_TXQ_TXERRINT_ENABLE
405310ad9a77SSam Leffler 			      | HAL_TXQ_TXDESCINT_ENABLE
405410ad9a77SSam Leffler 			      | HAL_TXQ_TXURNINT_ENABLE
405510ad9a77SSam Leffler 			      ;
4056c42a7b7eSSam Leffler 		qi.tqi_aifs = wmep->wmep_aifsn;
4057c42a7b7eSSam Leffler 		qi.tqi_cwmin = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
4058c42a7b7eSSam Leffler 		qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
405910ad9a77SSam Leffler 		qi.tqi_readyTime = 0;
4060c42a7b7eSSam Leffler 		qi.tqi_burstTime = ATH_TXOP_TO_US(wmep->wmep_txopLimit);
4061584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
406210ad9a77SSam Leffler 	}
406310ad9a77SSam Leffler #endif
406410ad9a77SSam Leffler 
406510ad9a77SSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET,
406610ad9a77SSam Leffler 	    "%s: Q%u qflags 0x%x aifs %u cwmin %u cwmax %u burstTime %u\n",
406710ad9a77SSam Leffler 	    __func__, txq->axq_qnum, qi.tqi_qflags,
406810ad9a77SSam Leffler 	    qi.tqi_aifs, qi.tqi_cwmin, qi.tqi_cwmax, qi.tqi_burstTime);
4069c42a7b7eSSam Leffler 
4070c42a7b7eSSam Leffler 	if (!ath_hal_settxqueueprops(ah, txq->axq_qnum, &qi)) {
4071b032f27cSSam Leffler 		if_printf(ifp, "unable to update hardware queue "
4072c42a7b7eSSam Leffler 			"parameters for %s traffic!\n",
4073c42a7b7eSSam Leffler 			ieee80211_wme_acnames[ac]);
4074c42a7b7eSSam Leffler 		return 0;
4075c42a7b7eSSam Leffler 	} else {
4076c42a7b7eSSam Leffler 		ath_hal_resettxqueue(ah, txq->axq_qnum); /* push to h/w */
4077c42a7b7eSSam Leffler 		return 1;
4078c42a7b7eSSam Leffler 	}
4079c42a7b7eSSam Leffler #undef ATH_TXOP_TO_US
4080c42a7b7eSSam Leffler #undef ATH_EXPONENT_TO_VALUE
4081c42a7b7eSSam Leffler }
4082c42a7b7eSSam Leffler 
4083c42a7b7eSSam Leffler /*
4084c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update WME parameters.
4085c42a7b7eSSam Leffler  */
4086c42a7b7eSSam Leffler static int
4087c42a7b7eSSam Leffler ath_wme_update(struct ieee80211com *ic)
4088c42a7b7eSSam Leffler {
4089c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
4090c42a7b7eSSam Leffler 
4091c42a7b7eSSam Leffler 	return !ath_txq_update(sc, WME_AC_BE) ||
4092c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_BK) ||
4093c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VI) ||
4094c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VO) ? EIO : 0;
4095c42a7b7eSSam Leffler }
4096c42a7b7eSSam Leffler 
4097c42a7b7eSSam Leffler /*
4098c42a7b7eSSam Leffler  * Reclaim resources for a setup queue.
4099c42a7b7eSSam Leffler  */
4100c42a7b7eSSam Leffler static void
4101c42a7b7eSSam Leffler ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
4102c42a7b7eSSam Leffler {
4103c42a7b7eSSam Leffler 
4104c42a7b7eSSam Leffler 	ath_hal_releasetxqueue(sc->sc_ah, txq->axq_qnum);
4105c42a7b7eSSam Leffler 	ATH_TXQ_LOCK_DESTROY(txq);
4106c42a7b7eSSam Leffler 	sc->sc_txqsetup &= ~(1<<txq->axq_qnum);
4107c42a7b7eSSam Leffler }
4108c42a7b7eSSam Leffler 
4109c42a7b7eSSam Leffler /*
4110c42a7b7eSSam Leffler  * Reclaim all tx queue resources.
4111c42a7b7eSSam Leffler  */
4112c42a7b7eSSam Leffler static void
4113c42a7b7eSSam Leffler ath_tx_cleanup(struct ath_softc *sc)
4114c42a7b7eSSam Leffler {
4115c42a7b7eSSam Leffler 	int i;
4116c42a7b7eSSam Leffler 
4117c42a7b7eSSam Leffler 	ATH_TXBUF_LOCK_DESTROY(sc);
4118c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
4119c42a7b7eSSam Leffler 		if (ATH_TXQ_SETUP(sc, i))
4120c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, &sc->sc_txq[i]);
4121c42a7b7eSSam Leffler }
41225591b213SSam Leffler 
412399d258fdSSam Leffler /*
4124ab06fdf2SSam Leffler  * Return h/w rate index for an IEEE rate (w/o basic rate bit)
4125ab06fdf2SSam Leffler  * using the current rates in sc_rixmap.
41268b5341deSSam Leffler  */
4127b8e788a5SAdrian Chadd int
4128ab06fdf2SSam Leffler ath_tx_findrix(const struct ath_softc *sc, uint8_t rate)
41298b5341deSSam Leffler {
4130ab06fdf2SSam Leffler 	int rix = sc->sc_rixmap[rate];
4131ab06fdf2SSam Leffler 	/* NB: return lowest rix for invalid rate */
4132ab06fdf2SSam Leffler 	return (rix == 0xff ? 0 : rix);
41338b5341deSSam Leffler }
41348b5341deSSam Leffler 
413568e8e04eSSam Leffler /*
4136c42a7b7eSSam Leffler  * Process completed xmit descriptors from the specified queue.
4137c42a7b7eSSam Leffler  */
4138d7736e13SSam Leffler static int
4139*96ff485dSAdrian Chadd ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq, int dosched)
41405591b213SSam Leffler {
41415591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4142b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
4143b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
414410ad9a77SSam Leffler 	struct ath_buf *bf, *last;
4145c4c3cb46SSam Leffler 	struct ath_desc *ds, *ds0;
414665f9edeeSSam Leffler 	struct ath_tx_status *ts;
41475591b213SSam Leffler 	struct ieee80211_node *ni;
41485591b213SSam Leffler 	struct ath_node *an;
4149d7736e13SSam Leffler 	int sr, lr, pri, nacked;
41505591b213SSam Leffler 	HAL_STATUS status;
41515591b213SSam Leffler 
4152c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_TX_PROC, "%s: tx queue %u head %p link %p\n",
4153c42a7b7eSSam Leffler 		__func__, txq->axq_qnum,
4154c42a7b7eSSam Leffler 		(caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
4155c42a7b7eSSam Leffler 		txq->axq_link);
4156d7736e13SSam Leffler 	nacked = 0;
41575591b213SSam Leffler 	for (;;) {
4158c42a7b7eSSam Leffler 		ATH_TXQ_LOCK(txq);
4159c42a7b7eSSam Leffler 		txq->axq_intrcnt = 0;	/* reset periodic desc intr count */
41606b349e5aSAdrian Chadd 		bf = TAILQ_FIRST(&txq->axq_q);
41615591b213SSam Leffler 		if (bf == NULL) {
4162c42a7b7eSSam Leffler 			ATH_TXQ_UNLOCK(txq);
41635591b213SSam Leffler 			break;
41645591b213SSam Leffler 		}
4165c4c3cb46SSam Leffler 		ds0 = &bf->bf_desc[0];
41665591b213SSam Leffler 		ds = &bf->bf_desc[bf->bf_nseg - 1];
416765f9edeeSSam Leffler 		ts = &bf->bf_status.ds_txstat;
416865f9edeeSSam Leffler 		status = ath_hal_txprocdesc(ah, ds, ts);
4169a585a9a1SSam Leffler #ifdef ATH_DEBUG
4170c42a7b7eSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_XMIT_DESC)
41716902009eSSam Leffler 			ath_printtxbuf(sc, bf, txq->axq_qnum, 0,
41726902009eSSam Leffler 			    status == HAL_OK);
41735591b213SSam Leffler #endif
41745591b213SSam Leffler 		if (status == HAL_EINPROGRESS) {
4175c42a7b7eSSam Leffler 			ATH_TXQ_UNLOCK(txq);
41765591b213SSam Leffler 			break;
41775591b213SSam Leffler 		}
41786b349e5aSAdrian Chadd 		ATH_TXQ_REMOVE(txq, bf, bf_list);
4179584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
418010ad9a77SSam Leffler 		if (txq->axq_depth > 0) {
418110ad9a77SSam Leffler 			/*
418210ad9a77SSam Leffler 			 * More frames follow.  Mark the buffer busy
418310ad9a77SSam Leffler 			 * so it's not re-used while the hardware may
418410ad9a77SSam Leffler 			 * still re-read the link field in the descriptor.
418510ad9a77SSam Leffler 			 */
418610ad9a77SSam Leffler 			bf->bf_flags |= ATH_BUF_BUSY;
418710ad9a77SSam Leffler 		} else
418810ad9a77SSam Leffler #else
4189ebecf802SSam Leffler 		if (txq->axq_depth == 0)
419010ad9a77SSam Leffler #endif
41911539af1eSSam Leffler 			txq->axq_link = NULL;
4192c42a7b7eSSam Leffler 		ATH_TXQ_UNLOCK(txq);
41935591b213SSam Leffler 
41945591b213SSam Leffler 		ni = bf->bf_node;
41955591b213SSam Leffler 		if (ni != NULL) {
4196c42a7b7eSSam Leffler 			an = ATH_NODE(ni);
419765f9edeeSSam Leffler 			if (ts->ts_status == 0) {
419865f9edeeSSam Leffler 				u_int8_t txant = ts->ts_antenna;
4199c42a7b7eSSam Leffler 				sc->sc_stats.ast_ant_tx[txant]++;
4200c42a7b7eSSam Leffler 				sc->sc_ant_tx[txant]++;
4201f6cbf16aSSam Leffler 				if (ts->ts_finaltsi != 0)
4202c42a7b7eSSam Leffler 					sc->sc_stats.ast_tx_altrate++;
4203c42a7b7eSSam Leffler 				pri = M_WME_GETAC(bf->bf_m);
4204c42a7b7eSSam Leffler 				if (pri >= WME_AC_VO)
4205c42a7b7eSSam Leffler 					ic->ic_wme.wme_hipri_traffic++;
4206ad80c0aaSSam Leffler 				if ((bf->bf_txflags & HAL_TXDESC_NOACK) == 0)
4207c42a7b7eSSam Leffler 					ni->ni_inact = ni->ni_inact_reload;
42085591b213SSam Leffler 			} else {
420965f9edeeSSam Leffler 				if (ts->ts_status & HAL_TXERR_XRETRY)
42105591b213SSam Leffler 					sc->sc_stats.ast_tx_xretries++;
421165f9edeeSSam Leffler 				if (ts->ts_status & HAL_TXERR_FIFO)
42125591b213SSam Leffler 					sc->sc_stats.ast_tx_fifoerr++;
421365f9edeeSSam Leffler 				if (ts->ts_status & HAL_TXERR_FILT)
42145591b213SSam Leffler 					sc->sc_stats.ast_tx_filtered++;
4215e9d1191fSAdrian Chadd 				if (ts->ts_status & HAL_TXERR_XTXOP)
4216e9d1191fSAdrian Chadd 					sc->sc_stats.ast_tx_xtxop++;
4217e9d1191fSAdrian Chadd 				if (ts->ts_status & HAL_TXERR_TIMER_EXPIRED)
4218e9d1191fSAdrian Chadd 					sc->sc_stats.ast_tx_timerexpired++;
4219e9d1191fSAdrian Chadd 
4220e9d1191fSAdrian Chadd 				/* XXX HAL_TX_DATA_UNDERRUN */
4221e9d1191fSAdrian Chadd 				/* XXX HAL_TX_DELIM_UNDERRUN */
4222e9d1191fSAdrian Chadd 
422368e8e04eSSam Leffler 				if (bf->bf_m->m_flags & M_FF)
422468e8e04eSSam Leffler 					sc->sc_stats.ast_ff_txerr++;
42255591b213SSam Leffler 			}
4226e9d1191fSAdrian Chadd 			/* XXX when is this valid? */
4227e9d1191fSAdrian Chadd 			if (ts->ts_status & HAL_TX_DESC_CFG_ERR)
4228e9d1191fSAdrian Chadd 				sc->sc_stats.ast_tx_desccfgerr++;
4229e9d1191fSAdrian Chadd 
423065f9edeeSSam Leffler 			sr = ts->ts_shortretry;
423165f9edeeSSam Leffler 			lr = ts->ts_longretry;
42325591b213SSam Leffler 			sc->sc_stats.ast_tx_shortretry += sr;
42335591b213SSam Leffler 			sc->sc_stats.ast_tx_longretry += lr;
4234c42a7b7eSSam Leffler 			/*
4235c42a7b7eSSam Leffler 			 * Hand the descriptor to the rate control algorithm.
4236c42a7b7eSSam Leffler 			 */
423765f9edeeSSam Leffler 			if ((ts->ts_status & HAL_TXERR_FILT) == 0 &&
423880c07f23SSam Leffler 			    (bf->bf_txflags & HAL_TXDESC_NOACK) == 0) {
4239d7736e13SSam Leffler 				/*
424084784be1SSam Leffler 				 * If frame was ack'd update statistics,
424184784be1SSam Leffler 				 * including the last rx time used to
424284784be1SSam Leffler 				 * workaround phantom bmiss interrupts.
4243d7736e13SSam Leffler 				 */
424484784be1SSam Leffler 				if (ts->ts_status == 0) {
4245d7736e13SSam Leffler 					nacked++;
424684784be1SSam Leffler 					sc->sc_stats.ast_tx_rssi = ts->ts_rssi;
424784784be1SSam Leffler 					ATH_RSSI_LPF(sc->sc_halstats.ns_avgtxrssi,
424884784be1SSam Leffler 						ts->ts_rssi);
424984784be1SSam Leffler 				}
425065f9edeeSSam Leffler 				ath_rate_tx_complete(sc, an, bf);
4251d7736e13SSam Leffler 			}
42520a915fadSSam Leffler 			/*
425368e8e04eSSam Leffler 			 * Do any tx complete callback.  Note this must
425468e8e04eSSam Leffler 			 * be done before releasing the node reference.
425568e8e04eSSam Leffler 			 */
425668e8e04eSSam Leffler 			if (bf->bf_m->m_flags & M_TXCB)
425768e8e04eSSam Leffler 				ieee80211_process_callback(ni, bf->bf_m,
425874eca0c2SSam Leffler 				    (bf->bf_txflags & HAL_TXDESC_NOACK) == 0 ?
425974eca0c2SSam Leffler 				        ts->ts_status : HAL_TXERR_XRETRY);
4260c42a7b7eSSam Leffler 			ieee80211_free_node(ni);
42615591b213SSam Leffler 		}
42625591b213SSam Leffler 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
42635591b213SSam Leffler 		    BUS_DMASYNC_POSTWRITE);
42645591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
426568e8e04eSSam Leffler 
42665591b213SSam Leffler 		m_freem(bf->bf_m);
42675591b213SSam Leffler 		bf->bf_m = NULL;
42685591b213SSam Leffler 		bf->bf_node = NULL;
42695591b213SSam Leffler 
4270f0b2a0beSSam Leffler 		ATH_TXBUF_LOCK(sc);
42716b349e5aSAdrian Chadd 		last = TAILQ_LAST(&sc->sc_txbuf, ath_bufhead_s);
427210ad9a77SSam Leffler 		if (last != NULL)
427310ad9a77SSam Leffler 			last->bf_flags &= ~ATH_BUF_BUSY;
42746b349e5aSAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
4275f0b2a0beSSam Leffler 		ATH_TXBUF_UNLOCK(sc);
42765591b213SSam Leffler 	}
4277339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
427868e8e04eSSam Leffler 	/*
427968e8e04eSSam Leffler 	 * Flush fast-frame staging queue when traffic slows.
428068e8e04eSSam Leffler 	 */
428168e8e04eSSam Leffler 	if (txq->axq_depth <= 1)
428204f19fd6SSam Leffler 		ieee80211_ff_flush(ic, txq->axq_ac);
4283339ccfb3SSam Leffler #endif
4284d7736e13SSam Leffler 	return nacked;
4285d7736e13SSam Leffler }
4286d7736e13SSam Leffler 
42878f939e79SAdrian Chadd #define	TXQACTIVE(t, q)		( (t) & (1 << (q)))
4288c42a7b7eSSam Leffler 
4289c42a7b7eSSam Leffler /*
4290c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
4291c42a7b7eSSam Leffler  * for a single hardware transmit queue (e.g. 5210 and 5211).
4292c42a7b7eSSam Leffler  */
4293c42a7b7eSSam Leffler static void
4294c42a7b7eSSam Leffler ath_tx_proc_q0(void *arg, int npending)
4295c42a7b7eSSam Leffler {
4296c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4297fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
42988f939e79SAdrian Chadd 	uint32_t txqs;
4299c42a7b7eSSam Leffler 
43008f939e79SAdrian Chadd 	ATH_LOCK(sc);
43018f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
43028f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
43038f939e79SAdrian Chadd 	ATH_UNLOCK(sc);
43048f939e79SAdrian Chadd 
4305*96ff485dSAdrian Chadd 	if (TXQACTIVE(txqs, 0) && ath_tx_processq(sc, &sc->sc_txq[0], 1))
43068f939e79SAdrian Chadd 		/* XXX why is lastrx updated in tx code? */
4307d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
43088f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum))
4309*96ff485dSAdrian Chadd 		ath_tx_processq(sc, sc->sc_cabq, 1);
431013f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
43112e986da5SSam Leffler 	sc->sc_wd_timer = 0;
43125591b213SSam Leffler 
43133e50ec2cSSam Leffler 	if (sc->sc_softled)
431446d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
43153e50ec2cSSam Leffler 
43165591b213SSam Leffler 	ath_start(ifp);
43175591b213SSam Leffler }
43185591b213SSam Leffler 
43195591b213SSam Leffler /*
4320c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
4321c42a7b7eSSam Leffler  * for four hardware queues, 0-3 (e.g. 5212 w/ WME support).
43225591b213SSam Leffler  */
43235591b213SSam Leffler static void
4324c42a7b7eSSam Leffler ath_tx_proc_q0123(void *arg, int npending)
4325c42a7b7eSSam Leffler {
4326c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4327fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
4328d7736e13SSam Leffler 	int nacked;
43298f939e79SAdrian Chadd 	uint32_t txqs;
43308f939e79SAdrian Chadd 
43318f939e79SAdrian Chadd 	ATH_LOCK(sc);
43328f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
43338f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
43348f939e79SAdrian Chadd 	ATH_UNLOCK(sc);
4335c42a7b7eSSam Leffler 
4336c42a7b7eSSam Leffler 	/*
4337c42a7b7eSSam Leffler 	 * Process each active queue.
4338c42a7b7eSSam Leffler 	 */
4339d7736e13SSam Leffler 	nacked = 0;
43408f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 0))
4341*96ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[0], 1);
43428f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 1))
4343*96ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[1], 1);
43448f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 2))
4345*96ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[2], 1);
43468f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 3))
4347*96ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[3], 1);
43488f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum))
4349*96ff485dSAdrian Chadd 		ath_tx_processq(sc, sc->sc_cabq, 1);
4350d7736e13SSam Leffler 	if (nacked)
4351d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4352c42a7b7eSSam Leffler 
435313f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
43542e986da5SSam Leffler 	sc->sc_wd_timer = 0;
4355c42a7b7eSSam Leffler 
43563e50ec2cSSam Leffler 	if (sc->sc_softled)
435746d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
43583e50ec2cSSam Leffler 
4359c42a7b7eSSam Leffler 	ath_start(ifp);
4360c42a7b7eSSam Leffler }
4361c42a7b7eSSam Leffler 
4362c42a7b7eSSam Leffler /*
4363c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt.
4364c42a7b7eSSam Leffler  */
4365c42a7b7eSSam Leffler static void
4366c42a7b7eSSam Leffler ath_tx_proc(void *arg, int npending)
4367c42a7b7eSSam Leffler {
4368c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4369fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
4370d7736e13SSam Leffler 	int i, nacked;
43718f939e79SAdrian Chadd 	uint32_t txqs;
43728f939e79SAdrian Chadd 
43738f939e79SAdrian Chadd 	ATH_LOCK(sc);
43748f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
43758f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
43768f939e79SAdrian Chadd 	ATH_UNLOCK(sc);
4377c42a7b7eSSam Leffler 
4378c42a7b7eSSam Leffler 	/*
4379c42a7b7eSSam Leffler 	 * Process each active queue.
4380c42a7b7eSSam Leffler 	 */
4381d7736e13SSam Leffler 	nacked = 0;
4382c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
43838f939e79SAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i) && TXQACTIVE(txqs, i))
4384*96ff485dSAdrian Chadd 			nacked += ath_tx_processq(sc, &sc->sc_txq[i], 1);
4385d7736e13SSam Leffler 	if (nacked)
4386d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4387c42a7b7eSSam Leffler 
438813f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
43892e986da5SSam Leffler 	sc->sc_wd_timer = 0;
4390c42a7b7eSSam Leffler 
43913e50ec2cSSam Leffler 	if (sc->sc_softled)
439246d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
43933e50ec2cSSam Leffler 
4394c42a7b7eSSam Leffler 	ath_start(ifp);
4395c42a7b7eSSam Leffler }
4396c42a7b7eSSam Leffler 
4397c42a7b7eSSam Leffler static void
4398c42a7b7eSSam Leffler ath_tx_draintxq(struct ath_softc *sc, struct ath_txq *txq)
43995591b213SSam Leffler {
4400a585a9a1SSam Leffler #ifdef ATH_DEBUG
44015591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4402d2f6ed15SSam Leffler #endif
440323428eafSSam Leffler 	struct ieee80211_node *ni;
44045591b213SSam Leffler 	struct ath_buf *bf;
44057a4c5ed9SSam Leffler 	u_int ix;
44065591b213SSam Leffler 
4407c42a7b7eSSam Leffler 	/*
4408c42a7b7eSSam Leffler 	 * NB: this assumes output has been stopped and
44095d61b5e8SSam Leffler 	 *     we do not need to block ath_tx_proc
4410c42a7b7eSSam Leffler 	 */
441110ad9a77SSam Leffler 	ATH_TXBUF_LOCK(sc);
44126b349e5aSAdrian Chadd 	bf = TAILQ_LAST(&sc->sc_txbuf, ath_bufhead_s);
441310ad9a77SSam Leffler 	if (bf != NULL)
441410ad9a77SSam Leffler 		bf->bf_flags &= ~ATH_BUF_BUSY;
441510ad9a77SSam Leffler 	ATH_TXBUF_UNLOCK(sc);
44167a4c5ed9SSam Leffler 	for (ix = 0;; ix++) {
4417c42a7b7eSSam Leffler 		ATH_TXQ_LOCK(txq);
44186b349e5aSAdrian Chadd 		bf = TAILQ_FIRST(&txq->axq_q);
44195591b213SSam Leffler 		if (bf == NULL) {
4420ebecf802SSam Leffler 			txq->axq_link = NULL;
4421c42a7b7eSSam Leffler 			ATH_TXQ_UNLOCK(txq);
44225591b213SSam Leffler 			break;
44235591b213SSam Leffler 		}
44246b349e5aSAdrian Chadd 		ATH_TXQ_REMOVE(txq, bf, bf_list);
4425c42a7b7eSSam Leffler 		ATH_TXQ_UNLOCK(txq);
4426a585a9a1SSam Leffler #ifdef ATH_DEBUG
44274a3ac3fcSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_RESET) {
4428b032f27cSSam Leffler 			struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4429b032f27cSSam Leffler 
44306902009eSSam Leffler 			ath_printtxbuf(sc, bf, txq->axq_qnum, ix,
443165f9edeeSSam Leffler 				ath_hal_txprocdesc(ah, bf->bf_desc,
443265f9edeeSSam Leffler 				    &bf->bf_status.ds_txstat) == HAL_OK);
4433e40b6ab1SSam Leffler 			ieee80211_dump_pkt(ic, mtod(bf->bf_m, const uint8_t *),
44344a3ac3fcSSam Leffler 			    bf->bf_m->m_len, 0, -1);
44354a3ac3fcSSam Leffler 		}
4436a585a9a1SSam Leffler #endif /* ATH_DEBUG */
44375591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
443823428eafSSam Leffler 		ni = bf->bf_node;
44395591b213SSam Leffler 		bf->bf_node = NULL;
4440c42a7b7eSSam Leffler 		if (ni != NULL) {
444123428eafSSam Leffler 			/*
4442d50ea6acSSam Leffler 			 * Do any callback and reclaim the node reference.
444323428eafSSam Leffler 			 */
4444d50ea6acSSam Leffler 			if (bf->bf_m->m_flags & M_TXCB)
4445d50ea6acSSam Leffler 				ieee80211_process_callback(ni, bf->bf_m, -1);
4446c42a7b7eSSam Leffler 			ieee80211_free_node(ni);
444723428eafSSam Leffler 		}
444868e8e04eSSam Leffler 		m_freem(bf->bf_m);
444968e8e04eSSam Leffler 		bf->bf_m = NULL;
445010ad9a77SSam Leffler 		bf->bf_flags &= ~ATH_BUF_BUSY;
445168e8e04eSSam Leffler 
4452f0b2a0beSSam Leffler 		ATH_TXBUF_LOCK(sc);
44536b349e5aSAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
4454f0b2a0beSSam Leffler 		ATH_TXBUF_UNLOCK(sc);
44555591b213SSam Leffler 	}
4456c42a7b7eSSam Leffler }
4457c42a7b7eSSam Leffler 
4458c42a7b7eSSam Leffler static void
4459c42a7b7eSSam Leffler ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
4460c42a7b7eSSam Leffler {
4461c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4462c42a7b7eSSam Leffler 
4463c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n",
4464c42a7b7eSSam Leffler 	    __func__, txq->axq_qnum,
44656891c875SPeter Wemm 	    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, txq->axq_qnum),
44666891c875SPeter Wemm 	    txq->axq_link);
44674a3ac3fcSSam Leffler 	(void) ath_hal_stoptxdma(ah, txq->axq_qnum);
4468c42a7b7eSSam Leffler }
4469c42a7b7eSSam Leffler 
4470c42a7b7eSSam Leffler /*
4471c42a7b7eSSam Leffler  * Drain the transmit queues and reclaim resources.
4472c42a7b7eSSam Leffler  */
4473c42a7b7eSSam Leffler static void
4474c42a7b7eSSam Leffler ath_draintxq(struct ath_softc *sc)
4475c42a7b7eSSam Leffler {
4476c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4477fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
4478c42a7b7eSSam Leffler 	int i;
4479c42a7b7eSSam Leffler 
4480c42a7b7eSSam Leffler 	/* XXX return value */
4481c42a7b7eSSam Leffler 	if (!sc->sc_invalid) {
4482c42a7b7eSSam Leffler 		/* don't touch the hardware if marked invalid */
44834a3ac3fcSSam Leffler 		DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n",
44844a3ac3fcSSam Leffler 		    __func__, sc->sc_bhalq,
44854a3ac3fcSSam Leffler 		    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq),
44864a3ac3fcSSam Leffler 		    NULL);
4487c42a7b7eSSam Leffler 		(void) ath_hal_stoptxdma(ah, sc->sc_bhalq);
4488c42a7b7eSSam Leffler 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
4489c42a7b7eSSam Leffler 			if (ATH_TXQ_SETUP(sc, i))
4490c42a7b7eSSam Leffler 				ath_tx_stopdma(sc, &sc->sc_txq[i]);
4491c42a7b7eSSam Leffler 	}
4492c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
4493c42a7b7eSSam Leffler 		if (ATH_TXQ_SETUP(sc, i))
4494c42a7b7eSSam Leffler 			ath_tx_draintxq(sc, &sc->sc_txq[i]);
44954a3ac3fcSSam Leffler #ifdef ATH_DEBUG
44964a3ac3fcSSam Leffler 	if (sc->sc_debug & ATH_DEBUG_RESET) {
44976b349e5aSAdrian Chadd 		struct ath_buf *bf = TAILQ_FIRST(&sc->sc_bbuf);
44984a3ac3fcSSam Leffler 		if (bf != NULL && bf->bf_m != NULL) {
44996902009eSSam Leffler 			ath_printtxbuf(sc, bf, sc->sc_bhalq, 0,
450065f9edeeSSam Leffler 				ath_hal_txprocdesc(ah, bf->bf_desc,
450165f9edeeSSam Leffler 				    &bf->bf_status.ds_txstat) == HAL_OK);
4502e40b6ab1SSam Leffler 			ieee80211_dump_pkt(ifp->if_l2com,
4503e40b6ab1SSam Leffler 			    mtod(bf->bf_m, const uint8_t *), bf->bf_m->m_len,
4504e40b6ab1SSam Leffler 			    0, -1);
45054a3ac3fcSSam Leffler 		}
45064a3ac3fcSSam Leffler 	}
45074a3ac3fcSSam Leffler #endif /* ATH_DEBUG */
450813f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
45092e986da5SSam Leffler 	sc->sc_wd_timer = 0;
45105591b213SSam Leffler }
45115591b213SSam Leffler 
45125591b213SSam Leffler /*
45135591b213SSam Leffler  * Disable the receive h/w in preparation for a reset.
45145591b213SSam Leffler  */
45155591b213SSam Leffler static void
45165591b213SSam Leffler ath_stoprecv(struct ath_softc *sc)
45175591b213SSam Leffler {
45188cec0ab9SSam Leffler #define	PA2DESC(_sc, _pa) \
4519c42a7b7eSSam Leffler 	((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
4520c42a7b7eSSam Leffler 		((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
45215591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
45225591b213SSam Leffler 
45235591b213SSam Leffler 	ath_hal_stoppcurecv(ah);	/* disable PCU */
45245591b213SSam Leffler 	ath_hal_setrxfilter(ah, 0);	/* clear recv filter */
45255591b213SSam Leffler 	ath_hal_stopdmarecv(ah);	/* disable DMA engine */
4526c42a7b7eSSam Leffler 	DELAY(3000);			/* 3ms is long enough for 1 frame */
4527a585a9a1SSam Leffler #ifdef ATH_DEBUG
4528c42a7b7eSSam Leffler 	if (sc->sc_debug & (ATH_DEBUG_RESET | ATH_DEBUG_FATAL)) {
45295591b213SSam Leffler 		struct ath_buf *bf;
45307a4c5ed9SSam Leffler 		u_int ix;
45315591b213SSam Leffler 
4532e325e530SSam Leffler 		printf("%s: rx queue %p, link %p\n", __func__,
453330310634SPeter Wemm 			(caddr_t)(uintptr_t) ath_hal_getrxbuf(ah), sc->sc_rxlink);
45347a4c5ed9SSam Leffler 		ix = 0;
45356b349e5aSAdrian Chadd 		TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
45368cec0ab9SSam Leffler 			struct ath_desc *ds = bf->bf_desc;
453765f9edeeSSam Leffler 			struct ath_rx_status *rs = &bf->bf_status.ds_rxstat;
4538c42a7b7eSSam Leffler 			HAL_STATUS status = ath_hal_rxprocdesc(ah, ds,
453965f9edeeSSam Leffler 				bf->bf_daddr, PA2DESC(sc, ds->ds_link), rs);
4540c42a7b7eSSam Leffler 			if (status == HAL_OK || (sc->sc_debug & ATH_DEBUG_FATAL))
45416902009eSSam Leffler 				ath_printrxbuf(sc, bf, ix, status == HAL_OK);
45427a4c5ed9SSam Leffler 			ix++;
45435591b213SSam Leffler 		}
45445591b213SSam Leffler 	}
45455591b213SSam Leffler #endif
454668e8e04eSSam Leffler 	if (sc->sc_rxpending != NULL) {
454768e8e04eSSam Leffler 		m_freem(sc->sc_rxpending);
454868e8e04eSSam Leffler 		sc->sc_rxpending = NULL;
454968e8e04eSSam Leffler 	}
45505591b213SSam Leffler 	sc->sc_rxlink = NULL;		/* just in case */
45518cec0ab9SSam Leffler #undef PA2DESC
45525591b213SSam Leffler }
45535591b213SSam Leffler 
45545591b213SSam Leffler /*
45555591b213SSam Leffler  * Enable the receive h/w following a reset.
45565591b213SSam Leffler  */
45575591b213SSam Leffler static int
45585591b213SSam Leffler ath_startrecv(struct ath_softc *sc)
45595591b213SSam Leffler {
45605591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
45615591b213SSam Leffler 	struct ath_buf *bf;
45625591b213SSam Leffler 
45635591b213SSam Leffler 	sc->sc_rxlink = NULL;
456468e8e04eSSam Leffler 	sc->sc_rxpending = NULL;
45656b349e5aSAdrian Chadd 	TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
45665591b213SSam Leffler 		int error = ath_rxbuf_init(sc, bf);
45675591b213SSam Leffler 		if (error != 0) {
4568c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_RECV,
4569c42a7b7eSSam Leffler 				"%s: ath_rxbuf_init failed %d\n",
4570c42a7b7eSSam Leffler 				__func__, error);
45715591b213SSam Leffler 			return error;
45725591b213SSam Leffler 		}
45735591b213SSam Leffler 	}
45745591b213SSam Leffler 
45756b349e5aSAdrian Chadd 	bf = TAILQ_FIRST(&sc->sc_rxbuf);
45765591b213SSam Leffler 	ath_hal_putrxbuf(ah, bf->bf_daddr);
45775591b213SSam Leffler 	ath_hal_rxena(ah);		/* enable recv descriptors */
45785591b213SSam Leffler 	ath_mode_init(sc);		/* set filters, etc. */
45795591b213SSam Leffler 	ath_hal_startpcurecv(ah);	/* re-enable PCU/DMA engine */
45805591b213SSam Leffler 	return 0;
45815591b213SSam Leffler }
45825591b213SSam Leffler 
45835591b213SSam Leffler /*
4584c42a7b7eSSam Leffler  * Update internal state after a channel change.
4585c42a7b7eSSam Leffler  */
4586c42a7b7eSSam Leffler static void
4587c42a7b7eSSam Leffler ath_chan_change(struct ath_softc *sc, struct ieee80211_channel *chan)
4588c42a7b7eSSam Leffler {
4589c42a7b7eSSam Leffler 	enum ieee80211_phymode mode;
4590c42a7b7eSSam Leffler 
4591c42a7b7eSSam Leffler 	/*
4592c42a7b7eSSam Leffler 	 * Change channels and update the h/w rate map
4593c42a7b7eSSam Leffler 	 * if we're switching; e.g. 11a to 11b/g.
4594c42a7b7eSSam Leffler 	 */
459568e8e04eSSam Leffler 	mode = ieee80211_chan2mode(chan);
4596c42a7b7eSSam Leffler 	if (mode != sc->sc_curmode)
4597c42a7b7eSSam Leffler 		ath_setcurmode(sc, mode);
459859efa8b5SSam Leffler 	sc->sc_curchan = chan;
4599c42a7b7eSSam Leffler }
4600c42a7b7eSSam Leffler 
4601c42a7b7eSSam Leffler /*
46025591b213SSam Leffler  * Set/change channels.  If the channel is really being changed,
46034fa8d4efSDaniel Eischen  * it's done by resetting the chip.  To accomplish this we must
46045591b213SSam Leffler  * first cleanup any pending DMA, then restart stuff after a la
46055591b213SSam Leffler  * ath_init.
46065591b213SSam Leffler  */
46075591b213SSam Leffler static int
46085591b213SSam Leffler ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan)
46095591b213SSam Leffler {
4610b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
4611b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
46125591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4613c42a7b7eSSam Leffler 
461459efa8b5SSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %u (%u MHz, flags 0x%x)\n",
461559efa8b5SSam Leffler 	    __func__, ieee80211_chan2ieee(ic, chan),
461659efa8b5SSam Leffler 	    chan->ic_freq, chan->ic_flags);
461759efa8b5SSam Leffler 	if (chan != sc->sc_curchan) {
4618c42a7b7eSSam Leffler 		HAL_STATUS status;
46195591b213SSam Leffler 		/*
46205591b213SSam Leffler 		 * To switch channels clear any pending DMA operations;
46215591b213SSam Leffler 		 * wait long enough for the RX fifo to drain, reset the
46225591b213SSam Leffler 		 * hardware at the new frequency, and then re-enable
46235591b213SSam Leffler 		 * the relevant bits of the h/w.
46245591b213SSam Leffler 		 */
46255591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
46265591b213SSam Leffler 		ath_draintxq(sc);		/* clear pending tx frames */
46275591b213SSam Leffler 		ath_stoprecv(sc);		/* turn off frame recv */
462859efa8b5SSam Leffler 		if (!ath_hal_reset(ah, sc->sc_opmode, chan, AH_TRUE, &status)) {
4629b032f27cSSam Leffler 			if_printf(ifp, "%s: unable to reset "
463079649302SGavin Atkinson 			    "channel %u (%u MHz, flags 0x%x), hal status %u\n",
463159efa8b5SSam Leffler 			    __func__, ieee80211_chan2ieee(ic, chan),
463259efa8b5SSam Leffler 			    chan->ic_freq, chan->ic_flags, status);
46335591b213SSam Leffler 			return EIO;
46345591b213SSam Leffler 		}
4635c59005e9SSam Leffler 		sc->sc_diversity = ath_hal_getdiversity(ah);
4636c42a7b7eSSam Leffler 
463748237774SAdrian Chadd 		/* Let DFS at it in case it's a DFS channel */
463848237774SAdrian Chadd 		ath_dfs_radar_enable(sc, ic->ic_curchan);
463948237774SAdrian Chadd 
46405591b213SSam Leffler 		/*
46415591b213SSam Leffler 		 * Re-enable rx framework.
46425591b213SSam Leffler 		 */
46435591b213SSam Leffler 		if (ath_startrecv(sc) != 0) {
4644b032f27cSSam Leffler 			if_printf(ifp, "%s: unable to restart recv logic\n",
4645b032f27cSSam Leffler 			    __func__);
46465591b213SSam Leffler 			return EIO;
46475591b213SSam Leffler 		}
46485591b213SSam Leffler 
46495591b213SSam Leffler 		/*
46505591b213SSam Leffler 		 * Change channels and update the h/w rate map
46515591b213SSam Leffler 		 * if we're switching; e.g. 11a to 11b/g.
46525591b213SSam Leffler 		 */
4653c42a7b7eSSam Leffler 		ath_chan_change(sc, chan);
46540a915fadSSam Leffler 
46550a915fadSSam Leffler 		/*
46562fd9aabbSAdrian Chadd 		 * Reset clears the beacon timers; reset them
46572fd9aabbSAdrian Chadd 		 * here if needed.
46582fd9aabbSAdrian Chadd 		 */
46592fd9aabbSAdrian Chadd 		if (sc->sc_beacons) {		/* restart beacons */
46602fd9aabbSAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
46612fd9aabbSAdrian Chadd 			if (sc->sc_tdma)
46622fd9aabbSAdrian Chadd 				ath_tdma_config(sc, NULL);
46632fd9aabbSAdrian Chadd 			else
46642fd9aabbSAdrian Chadd #endif
46652fd9aabbSAdrian Chadd 			ath_beacon_config(sc, NULL);
46662fd9aabbSAdrian Chadd 		}
46672fd9aabbSAdrian Chadd 
46682fd9aabbSAdrian Chadd 		/*
46690a915fadSSam Leffler 		 * Re-enable interrupts.
46700a915fadSSam Leffler 		 */
46710a915fadSSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
46725591b213SSam Leffler 	}
46735591b213SSam Leffler 	return 0;
46745591b213SSam Leffler }
46755591b213SSam Leffler 
46765591b213SSam Leffler /*
46775591b213SSam Leffler  * Periodically recalibrate the PHY to account
46785591b213SSam Leffler  * for temperature/environment changes.
46795591b213SSam Leffler  */
46805591b213SSam Leffler static void
46815591b213SSam Leffler ath_calibrate(void *arg)
46825591b213SSam Leffler {
46835591b213SSam Leffler 	struct ath_softc *sc = arg;
46845591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
46852dc7fcc4SSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
46868d91de92SSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
46872dc7fcc4SSam Leffler 	HAL_BOOL longCal, isCalDone;
4688a108ab63SAdrian Chadd 	HAL_BOOL aniCal, shortCal = AH_FALSE;
46892dc7fcc4SSam Leffler 	int nextcal;
46905591b213SSam Leffler 
46918d91de92SSam Leffler 	if (ic->ic_flags & IEEE80211_F_SCAN)	/* defer, off channel */
46928d91de92SSam Leffler 		goto restart;
46932dc7fcc4SSam Leffler 	longCal = (ticks - sc->sc_lastlongcal >= ath_longcalinterval*hz);
4694a108ab63SAdrian Chadd 	aniCal = (ticks - sc->sc_lastani >= ath_anicalinterval*hz/1000);
4695a108ab63SAdrian Chadd 	if (sc->sc_doresetcal)
4696a108ab63SAdrian Chadd 		shortCal = (ticks - sc->sc_lastshortcal >= ath_shortcalinterval*hz/1000);
4697a108ab63SAdrian Chadd 
4698a108ab63SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: shortCal=%d; longCal=%d; aniCal=%d\n", __func__, shortCal, longCal, aniCal);
4699a108ab63SAdrian Chadd 	if (aniCal) {
4700a108ab63SAdrian Chadd 		sc->sc_stats.ast_ani_cal++;
4701a108ab63SAdrian Chadd 		sc->sc_lastani = ticks;
4702a108ab63SAdrian Chadd 		ath_hal_ani_poll(ah, sc->sc_curchan);
4703a108ab63SAdrian Chadd 	}
4704a108ab63SAdrian Chadd 
47052dc7fcc4SSam Leffler 	if (longCal) {
47065591b213SSam Leffler 		sc->sc_stats.ast_per_cal++;
47078197f57eSAdrian Chadd 		sc->sc_lastlongcal = ticks;
47085591b213SSam Leffler 		if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) {
47095591b213SSam Leffler 			/*
47105591b213SSam Leffler 			 * Rfgain is out of bounds, reset the chip
47115591b213SSam Leffler 			 * to load new gain values.
47125591b213SSam Leffler 			 */
4713370572d9SSam Leffler 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
4714370572d9SSam Leffler 				"%s: rfgain change\n", __func__);
47155591b213SSam Leffler 			sc->sc_stats.ast_per_rfgain++;
47162dc7fcc4SSam Leffler 			ath_reset(ifp);
47175591b213SSam Leffler 		}
47182dc7fcc4SSam Leffler 		/*
47192dc7fcc4SSam Leffler 		 * If this long cal is after an idle period, then
47202dc7fcc4SSam Leffler 		 * reset the data collection state so we start fresh.
47212dc7fcc4SSam Leffler 		 */
47222dc7fcc4SSam Leffler 		if (sc->sc_resetcal) {
472359efa8b5SSam Leffler 			(void) ath_hal_calreset(ah, sc->sc_curchan);
47242dc7fcc4SSam Leffler 			sc->sc_lastcalreset = ticks;
4725a108ab63SAdrian Chadd 			sc->sc_lastshortcal = ticks;
47262dc7fcc4SSam Leffler 			sc->sc_resetcal = 0;
4727a108ab63SAdrian Chadd 			sc->sc_doresetcal = AH_TRUE;
47282dc7fcc4SSam Leffler 		}
47292dc7fcc4SSam Leffler 	}
4730a108ab63SAdrian Chadd 
4731a108ab63SAdrian Chadd 	/* Only call if we're doing a short/long cal, not for ANI calibration */
4732a108ab63SAdrian Chadd 	if (shortCal || longCal) {
473359efa8b5SSam Leffler 		if (ath_hal_calibrateN(ah, sc->sc_curchan, longCal, &isCalDone)) {
47342dc7fcc4SSam Leffler 			if (longCal) {
47352dc7fcc4SSam Leffler 				/*
47362dc7fcc4SSam Leffler 				 * Calibrate noise floor data again in case of change.
47372dc7fcc4SSam Leffler 				 */
47382dc7fcc4SSam Leffler 				ath_hal_process_noisefloor(ah);
47392dc7fcc4SSam Leffler 			}
47402dc7fcc4SSam Leffler 		} else {
4741c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
4742c42a7b7eSSam Leffler 				"%s: calibration of channel %u failed\n",
474359efa8b5SSam Leffler 				__func__, sc->sc_curchan->ic_freq);
47445591b213SSam Leffler 			sc->sc_stats.ast_per_calfail++;
47455591b213SSam Leffler 		}
4746a108ab63SAdrian Chadd 		if (shortCal)
4747a108ab63SAdrian Chadd 			sc->sc_lastshortcal = ticks;
4748a108ab63SAdrian Chadd 	}
47492dc7fcc4SSam Leffler 	if (!isCalDone) {
47508d91de92SSam Leffler restart:
47517b0c77ecSSam Leffler 		/*
47522dc7fcc4SSam Leffler 		 * Use a shorter interval to potentially collect multiple
47532dc7fcc4SSam Leffler 		 * data samples required to complete calibration.  Once
47542dc7fcc4SSam Leffler 		 * we're told the work is done we drop back to a longer
47552dc7fcc4SSam Leffler 		 * interval between requests.  We're more aggressive doing
47562dc7fcc4SSam Leffler 		 * work when operating as an AP to improve operation right
47572dc7fcc4SSam Leffler 		 * after startup.
47587b0c77ecSSam Leffler 		 */
4759a108ab63SAdrian Chadd 		sc->sc_lastshortcal = ticks;
4760a108ab63SAdrian Chadd 		nextcal = ath_shortcalinterval*hz/1000;
47612dc7fcc4SSam Leffler 		if (sc->sc_opmode != HAL_M_HOSTAP)
47622dc7fcc4SSam Leffler 			nextcal *= 10;
4763a108ab63SAdrian Chadd 		sc->sc_doresetcal = AH_TRUE;
47642dc7fcc4SSam Leffler 	} else {
4765a108ab63SAdrian Chadd 		/* nextcal should be the shortest time for next event */
47662dc7fcc4SSam Leffler 		nextcal = ath_longcalinterval*hz;
47672dc7fcc4SSam Leffler 		if (sc->sc_lastcalreset == 0)
47682dc7fcc4SSam Leffler 			sc->sc_lastcalreset = sc->sc_lastlongcal;
47692dc7fcc4SSam Leffler 		else if (ticks - sc->sc_lastcalreset >= ath_resetcalinterval*hz)
47702dc7fcc4SSam Leffler 			sc->sc_resetcal = 1;	/* setup reset next trip */
4771a108ab63SAdrian Chadd 		sc->sc_doresetcal = AH_FALSE;
4772bd5a9920SSam Leffler 	}
4773a108ab63SAdrian Chadd 	/* ANI calibration may occur more often than short/long/resetcal */
4774a108ab63SAdrian Chadd 	if (ath_anicalinterval > 0)
4775a108ab63SAdrian Chadd 		nextcal = MIN(nextcal, ath_anicalinterval*hz/1000);
4776bd5a9920SSam Leffler 
47772dc7fcc4SSam Leffler 	if (nextcal != 0) {
47782dc7fcc4SSam Leffler 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: next +%u (%sisCalDone)\n",
47792dc7fcc4SSam Leffler 		    __func__, nextcal, isCalDone ? "" : "!");
47802dc7fcc4SSam Leffler 		callout_reset(&sc->sc_cal_ch, nextcal, ath_calibrate, sc);
47812dc7fcc4SSam Leffler 	} else {
47822dc7fcc4SSam Leffler 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: calibration disabled\n",
47832dc7fcc4SSam Leffler 		    __func__);
47842dc7fcc4SSam Leffler 		/* NB: don't rearm timer */
47852dc7fcc4SSam Leffler 	}
47865591b213SSam Leffler }
47875591b213SSam Leffler 
478868e8e04eSSam Leffler static void
478968e8e04eSSam Leffler ath_scan_start(struct ieee80211com *ic)
479068e8e04eSSam Leffler {
479168e8e04eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
479268e8e04eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
479368e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
479468e8e04eSSam Leffler 	u_int32_t rfilt;
479568e8e04eSSam Leffler 
479668e8e04eSSam Leffler 	/* XXX calibration timer? */
479768e8e04eSSam Leffler 
479868e8e04eSSam Leffler 	sc->sc_scanning = 1;
479968e8e04eSSam Leffler 	sc->sc_syncbeacon = 0;
480068e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
480168e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
480268e8e04eSSam Leffler 	ath_hal_setassocid(ah, ifp->if_broadcastaddr, 0);
480368e8e04eSSam Leffler 
480468e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0\n",
480568e8e04eSSam Leffler 		 __func__, rfilt, ether_sprintf(ifp->if_broadcastaddr));
480668e8e04eSSam Leffler }
480768e8e04eSSam Leffler 
480868e8e04eSSam Leffler static void
480968e8e04eSSam Leffler ath_scan_end(struct ieee80211com *ic)
481068e8e04eSSam Leffler {
481168e8e04eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
481268e8e04eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
481368e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
481468e8e04eSSam Leffler 	u_int32_t rfilt;
481568e8e04eSSam Leffler 
481668e8e04eSSam Leffler 	sc->sc_scanning = 0;
481768e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
481868e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
481968e8e04eSSam Leffler 	ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
482068e8e04eSSam Leffler 
482168e8e04eSSam Leffler 	ath_hal_process_noisefloor(ah);
482268e8e04eSSam Leffler 
482368e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
482468e8e04eSSam Leffler 		 __func__, rfilt, ether_sprintf(sc->sc_curbssid),
482568e8e04eSSam Leffler 		 sc->sc_curaid);
482668e8e04eSSam Leffler }
482768e8e04eSSam Leffler 
482868e8e04eSSam Leffler static void
482968e8e04eSSam Leffler ath_set_channel(struct ieee80211com *ic)
483068e8e04eSSam Leffler {
483168e8e04eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
483268e8e04eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
483368e8e04eSSam Leffler 
483468e8e04eSSam Leffler 	(void) ath_chan_set(sc, ic->ic_curchan);
483568e8e04eSSam Leffler 	/*
483668e8e04eSSam Leffler 	 * If we are returning to our bss channel then mark state
483768e8e04eSSam Leffler 	 * so the next recv'd beacon's tsf will be used to sync the
483868e8e04eSSam Leffler 	 * beacon timers.  Note that since we only hear beacons in
483968e8e04eSSam Leffler 	 * sta/ibss mode this has no effect in other operating modes.
484068e8e04eSSam Leffler 	 */
484168e8e04eSSam Leffler 	if (!sc->sc_scanning && ic->ic_curchan == ic->ic_bsschan)
484268e8e04eSSam Leffler 		sc->sc_syncbeacon = 1;
484368e8e04eSSam Leffler }
484468e8e04eSSam Leffler 
4845b032f27cSSam Leffler /*
4846b032f27cSSam Leffler  * Walk the vap list and check if there any vap's in RUN state.
4847b032f27cSSam Leffler  */
48485591b213SSam Leffler static int
4849b032f27cSSam Leffler ath_isanyrunningvaps(struct ieee80211vap *this)
48505591b213SSam Leffler {
4851b032f27cSSam Leffler 	struct ieee80211com *ic = this->iv_ic;
4852b032f27cSSam Leffler 	struct ieee80211vap *vap;
4853b032f27cSSam Leffler 
4854b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
4855b032f27cSSam Leffler 
4856b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
4857309a3e45SSam Leffler 		if (vap != this && vap->iv_state >= IEEE80211_S_RUN)
4858b032f27cSSam Leffler 			return 1;
4859b032f27cSSam Leffler 	}
4860b032f27cSSam Leffler 	return 0;
4861b032f27cSSam Leffler }
4862b032f27cSSam Leffler 
4863b032f27cSSam Leffler static int
4864b032f27cSSam Leffler ath_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
4865b032f27cSSam Leffler {
4866b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
4867b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
4868b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
486945bbf62fSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4870b032f27cSSam Leffler 	struct ieee80211_node *ni = NULL;
487168e8e04eSSam Leffler 	int i, error, stamode;
48725591b213SSam Leffler 	u_int32_t rfilt;
4873f52efb6dSAdrian Chadd 	int csa_run_transition = 0;
48745591b213SSam Leffler 	static const HAL_LED_STATE leds[] = {
48755591b213SSam Leffler 	    HAL_LED_INIT,	/* IEEE80211_S_INIT */
48765591b213SSam Leffler 	    HAL_LED_SCAN,	/* IEEE80211_S_SCAN */
48775591b213SSam Leffler 	    HAL_LED_AUTH,	/* IEEE80211_S_AUTH */
48785591b213SSam Leffler 	    HAL_LED_ASSOC, 	/* IEEE80211_S_ASSOC */
487977d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_CAC */
48805591b213SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_RUN */
488177d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_CSA */
488277d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_SLEEP */
48835591b213SSam Leffler 	};
48845591b213SSam Leffler 
4885c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: %s -> %s\n", __func__,
4886b032f27cSSam Leffler 		ieee80211_state_name[vap->iv_state],
4887c42a7b7eSSam Leffler 		ieee80211_state_name[nstate]);
48885591b213SSam Leffler 
4889f52efb6dSAdrian Chadd 	if (vap->iv_state == IEEE80211_S_CSA && nstate == IEEE80211_S_RUN)
4890f52efb6dSAdrian Chadd 		csa_run_transition = 1;
4891f52efb6dSAdrian Chadd 
48922e986da5SSam Leffler 	callout_drain(&sc->sc_cal_ch);
48935591b213SSam Leffler 	ath_hal_setledstate(ah, leds[nstate]);	/* set LED */
48945591b213SSam Leffler 
4895b032f27cSSam Leffler 	if (nstate == IEEE80211_S_SCAN) {
489658769f58SSam Leffler 		/*
4897b032f27cSSam Leffler 		 * Scanning: turn off beacon miss and don't beacon.
4898b032f27cSSam Leffler 		 * Mark beacon state so when we reach RUN state we'll
4899b032f27cSSam Leffler 		 * [re]setup beacons.  Unblock the task q thread so
4900b032f27cSSam Leffler 		 * deferred interrupt processing is done.
490158769f58SSam Leffler 		 */
4902b032f27cSSam Leffler 		ath_hal_intrset(ah,
4903b032f27cSSam Leffler 		    sc->sc_imask &~ (HAL_INT_SWBA | HAL_INT_BMISS));
49045591b213SSam Leffler 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
4905b032f27cSSam Leffler 		sc->sc_beacons = 0;
4906b032f27cSSam Leffler 		taskqueue_unblock(sc->sc_tq);
49075591b213SSam Leffler 	}
49085591b213SSam Leffler 
4909b032f27cSSam Leffler 	ni = vap->iv_bss;
491068e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
4911b032f27cSSam Leffler 	stamode = (vap->iv_opmode == IEEE80211_M_STA ||
49127b916f89SSam Leffler 		   vap->iv_opmode == IEEE80211_M_AHDEMO ||
4913b032f27cSSam Leffler 		   vap->iv_opmode == IEEE80211_M_IBSS);
491468e8e04eSSam Leffler 	if (stamode && nstate == IEEE80211_S_RUN) {
491568e8e04eSSam Leffler 		sc->sc_curaid = ni->ni_associd;
491668e8e04eSSam Leffler 		IEEE80211_ADDR_COPY(sc->sc_curbssid, ni->ni_bssid);
4917b032f27cSSam Leffler 		ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
4918b032f27cSSam Leffler 	}
491968e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
4920b032f27cSSam Leffler 	   __func__, rfilt, ether_sprintf(sc->sc_curbssid), sc->sc_curaid);
492168e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
492268e8e04eSSam Leffler 
4923b032f27cSSam Leffler 	/* XXX is this to restore keycache on resume? */
4924b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_STA &&
4925b032f27cSSam Leffler 	    (vap->iv_flags & IEEE80211_F_PRIVACY)) {
49265591b213SSam Leffler 		for (i = 0; i < IEEE80211_WEP_NKID; i++)
49275591b213SSam Leffler 			if (ath_hal_keyisvalid(ah, i))
492868e8e04eSSam Leffler 				ath_hal_keysetmac(ah, i, ni->ni_bssid);
49295591b213SSam Leffler 	}
4930b032f27cSSam Leffler 
4931b032f27cSSam Leffler 	/*
4932b032f27cSSam Leffler 	 * Invoke the parent method to do net80211 work.
4933b032f27cSSam Leffler 	 */
4934b032f27cSSam Leffler 	error = avp->av_newstate(vap, nstate, arg);
4935b032f27cSSam Leffler 	if (error != 0)
4936b032f27cSSam Leffler 		goto bad;
4937c42a7b7eSSam Leffler 
493868e8e04eSSam Leffler 	if (nstate == IEEE80211_S_RUN) {
4939b032f27cSSam Leffler 		/* NB: collect bss node again, it may have changed */
4940b032f27cSSam Leffler 		ni = vap->iv_bss;
49415591b213SSam Leffler 
4942b032f27cSSam Leffler 		DPRINTF(sc, ATH_DEBUG_STATE,
4943b032f27cSSam Leffler 		    "%s(RUN): iv_flags 0x%08x bintvl %d bssid %s "
4944b032f27cSSam Leffler 		    "capinfo 0x%04x chan %d\n", __func__,
4945b032f27cSSam Leffler 		    vap->iv_flags, ni->ni_intval, ether_sprintf(ni->ni_bssid),
4946b032f27cSSam Leffler 		    ni->ni_capinfo, ieee80211_chan2ieee(ic, ic->ic_curchan));
4947b032f27cSSam Leffler 
4948b032f27cSSam Leffler 		switch (vap->iv_opmode) {
4949584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
495010ad9a77SSam Leffler 		case IEEE80211_M_AHDEMO:
495110ad9a77SSam Leffler 			if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
495210ad9a77SSam Leffler 				break;
495310ad9a77SSam Leffler 			/* fall thru... */
495410ad9a77SSam Leffler #endif
4955e8fd88a3SSam Leffler 		case IEEE80211_M_HOSTAP:
4956e8fd88a3SSam Leffler 		case IEEE80211_M_IBSS:
495759aa14a9SRui Paulo 		case IEEE80211_M_MBSS:
49585591b213SSam Leffler 			/*
4959e8fd88a3SSam Leffler 			 * Allocate and setup the beacon frame.
4960e8fd88a3SSam Leffler 			 *
4961f818612bSSam Leffler 			 * Stop any previous beacon DMA.  This may be
4962f818612bSSam Leffler 			 * necessary, for example, when an ibss merge
4963f818612bSSam Leffler 			 * causes reconfiguration; there will be a state
4964f818612bSSam Leffler 			 * transition from RUN->RUN that means we may
4965f818612bSSam Leffler 			 * be called with beacon transmission active.
4966f818612bSSam Leffler 			 */
4967f818612bSSam Leffler 			ath_hal_stoptxdma(ah, sc->sc_bhalq);
4968b032f27cSSam Leffler 
49695591b213SSam Leffler 			error = ath_beacon_alloc(sc, ni);
49705591b213SSam Leffler 			if (error != 0)
49715591b213SSam Leffler 				goto bad;
49727a04dc27SSam Leffler 			/*
497380d939bfSSam Leffler 			 * If joining an adhoc network defer beacon timer
497480d939bfSSam Leffler 			 * configuration to the next beacon frame so we
497580d939bfSSam Leffler 			 * have a current TSF to use.  Otherwise we're
4976b032f27cSSam Leffler 			 * starting an ibss/bss so there's no need to delay;
4977b032f27cSSam Leffler 			 * if this is the first vap moving to RUN state, then
4978b032f27cSSam Leffler 			 * beacon state needs to be [re]configured.
49797a04dc27SSam Leffler 			 */
4980b032f27cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_IBSS &&
4981b032f27cSSam Leffler 			    ni->ni_tstamp.tsf != 0) {
498280d939bfSSam Leffler 				sc->sc_syncbeacon = 1;
4983b032f27cSSam Leffler 			} else if (!sc->sc_beacons) {
4984584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
498510ad9a77SSam Leffler 				if (vap->iv_caps & IEEE80211_C_TDMA)
498610ad9a77SSam Leffler 					ath_tdma_config(sc, vap);
498710ad9a77SSam Leffler 				else
498810ad9a77SSam Leffler #endif
4989b032f27cSSam Leffler 					ath_beacon_config(sc, vap);
4990b032f27cSSam Leffler 				sc->sc_beacons = 1;
4991b032f27cSSam Leffler 			}
4992e8fd88a3SSam Leffler 			break;
4993e8fd88a3SSam Leffler 		case IEEE80211_M_STA:
4994e8fd88a3SSam Leffler 			/*
499580d939bfSSam Leffler 			 * Defer beacon timer configuration to the next
499680d939bfSSam Leffler 			 * beacon frame so we have a current TSF to use
499780d939bfSSam Leffler 			 * (any TSF collected when scanning is likely old).
4998f52efb6dSAdrian Chadd 			 * However if it's due to a CSA -> RUN transition,
4999f52efb6dSAdrian Chadd 			 * force a beacon update so we pick up a lack of
5000f52efb6dSAdrian Chadd 			 * beacons from an AP in CAC and thus force a
5001f52efb6dSAdrian Chadd 			 * scan.
50027a04dc27SSam Leffler 			 */
500380d939bfSSam Leffler 			sc->sc_syncbeacon = 1;
5004f52efb6dSAdrian Chadd 			if (csa_run_transition)
5005f52efb6dSAdrian Chadd 				ath_beacon_config(sc, vap);
5006e8fd88a3SSam Leffler 			break;
5007b032f27cSSam Leffler 		case IEEE80211_M_MONITOR:
5008b032f27cSSam Leffler 			/*
5009b032f27cSSam Leffler 			 * Monitor mode vaps have only INIT->RUN and RUN->RUN
5010b032f27cSSam Leffler 			 * transitions so we must re-enable interrupts here to
5011b032f27cSSam Leffler 			 * handle the case of a single monitor mode vap.
5012b032f27cSSam Leffler 			 */
5013b032f27cSSam Leffler 			ath_hal_intrset(ah, sc->sc_imask);
5014b032f27cSSam Leffler 			break;
5015b032f27cSSam Leffler 		case IEEE80211_M_WDS:
5016b032f27cSSam Leffler 			break;
5017e8fd88a3SSam Leffler 		default:
5018e8fd88a3SSam Leffler 			break;
50195591b213SSam Leffler 		}
50205591b213SSam Leffler 		/*
50217b0c77ecSSam Leffler 		 * Let the hal process statistics collected during a
50227b0c77ecSSam Leffler 		 * scan so it can provide calibrated noise floor data.
50237b0c77ecSSam Leffler 		 */
50247b0c77ecSSam Leffler 		ath_hal_process_noisefloor(ah);
50257b0c77ecSSam Leffler 		/*
5026ffa2cab6SSam Leffler 		 * Reset rssi stats; maybe not the best place...
5027ffa2cab6SSam Leffler 		 */
5028ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
5029ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
5030ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
503145bbf62fSSam Leffler 		/*
5032b032f27cSSam Leffler 		 * Finally, start any timers and the task q thread
5033b032f27cSSam Leffler 		 * (in case we didn't go through SCAN state).
503445bbf62fSSam Leffler 		 */
50352dc7fcc4SSam Leffler 		if (ath_longcalinterval != 0) {
5036c42a7b7eSSam Leffler 			/* start periodic recalibration timer */
50372dc7fcc4SSam Leffler 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
50382dc7fcc4SSam Leffler 		} else {
50392dc7fcc4SSam Leffler 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
50402dc7fcc4SSam Leffler 			    "%s: calibration disabled\n", __func__);
5041c42a7b7eSSam Leffler 		}
5042b032f27cSSam Leffler 		taskqueue_unblock(sc->sc_tq);
5043b032f27cSSam Leffler 	} else if (nstate == IEEE80211_S_INIT) {
5044b032f27cSSam Leffler 		/*
5045b032f27cSSam Leffler 		 * If there are no vaps left in RUN state then
5046b032f27cSSam Leffler 		 * shutdown host/driver operation:
5047b032f27cSSam Leffler 		 * o disable interrupts
5048b032f27cSSam Leffler 		 * o disable the task queue thread
5049b032f27cSSam Leffler 		 * o mark beacon processing as stopped
5050b032f27cSSam Leffler 		 */
5051b032f27cSSam Leffler 		if (!ath_isanyrunningvaps(vap)) {
5052b032f27cSSam Leffler 			sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
5053b032f27cSSam Leffler 			/* disable interrupts  */
5054b032f27cSSam Leffler 			ath_hal_intrset(ah, sc->sc_imask &~ HAL_INT_GLOBAL);
5055b032f27cSSam Leffler 			taskqueue_block(sc->sc_tq);
5056b032f27cSSam Leffler 			sc->sc_beacons = 0;
5057b032f27cSSam Leffler 		}
5058584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
505910ad9a77SSam Leffler 		ath_hal_setcca(ah, AH_TRUE);
506010ad9a77SSam Leffler #endif
5061b032f27cSSam Leffler 	}
50625591b213SSam Leffler bad:
50635591b213SSam Leffler 	return error;
50645591b213SSam Leffler }
50655591b213SSam Leffler 
50665591b213SSam Leffler /*
5067e8fd88a3SSam Leffler  * Allocate a key cache slot to the station so we can
5068e8fd88a3SSam Leffler  * setup a mapping from key index to node. The key cache
5069e8fd88a3SSam Leffler  * slot is needed for managing antenna state and for
5070e8fd88a3SSam Leffler  * compression when stations do not use crypto.  We do
5071e8fd88a3SSam Leffler  * it uniliaterally here; if crypto is employed this slot
5072e8fd88a3SSam Leffler  * will be reassigned.
5073e8fd88a3SSam Leffler  */
5074e8fd88a3SSam Leffler static void
5075e8fd88a3SSam Leffler ath_setup_stationkey(struct ieee80211_node *ni)
5076e8fd88a3SSam Leffler {
5077b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
5078b032f27cSSam Leffler 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
5079c1225b52SSam Leffler 	ieee80211_keyix keyix, rxkeyix;
5080e8fd88a3SSam Leffler 
5081b032f27cSSam Leffler 	if (!ath_key_alloc(vap, &ni->ni_ucastkey, &keyix, &rxkeyix)) {
5082e8fd88a3SSam Leffler 		/*
5083e8fd88a3SSam Leffler 		 * Key cache is full; we'll fall back to doing
5084e8fd88a3SSam Leffler 		 * the more expensive lookup in software.  Note
5085e8fd88a3SSam Leffler 		 * this also means no h/w compression.
5086e8fd88a3SSam Leffler 		 */
5087e8fd88a3SSam Leffler 		/* XXX msg+statistic */
5088e8fd88a3SSam Leffler 	} else {
5089c1225b52SSam Leffler 		/* XXX locking? */
5090e8fd88a3SSam Leffler 		ni->ni_ucastkey.wk_keyix = keyix;
5091c1225b52SSam Leffler 		ni->ni_ucastkey.wk_rxkeyix = rxkeyix;
509233052833SSam Leffler 		/* NB: must mark device key to get called back on delete */
509333052833SSam Leffler 		ni->ni_ucastkey.wk_flags |= IEEE80211_KEY_DEVKEY;
5094d3ac945bSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_ucastkey.wk_macaddr, ni->ni_macaddr);
5095e8fd88a3SSam Leffler 		/* NB: this will create a pass-thru key entry */
5096d3ac945bSSam Leffler 		ath_keyset(sc, &ni->ni_ucastkey, vap->iv_bss);
5097e8fd88a3SSam Leffler 	}
5098e8fd88a3SSam Leffler }
5099e8fd88a3SSam Leffler 
5100e8fd88a3SSam Leffler /*
51015591b213SSam Leffler  * Setup driver-specific state for a newly associated node.
51025591b213SSam Leffler  * Note that we're called also on a re-associate, the isnew
51035591b213SSam Leffler  * param tells us if this is the first time or not.
51045591b213SSam Leffler  */
51055591b213SSam Leffler static void
5106e9962332SSam Leffler ath_newassoc(struct ieee80211_node *ni, int isnew)
51075591b213SSam Leffler {
5108b032f27cSSam Leffler 	struct ath_node *an = ATH_NODE(ni);
5109b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
5110b032f27cSSam Leffler 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
5111c62362cbSSam Leffler 	const struct ieee80211_txparam *tp = ni->ni_txparms;
51125591b213SSam Leffler 
5113ab06fdf2SSam Leffler 	an->an_mcastrix = ath_tx_findrix(sc, tp->mcastrate);
5114ab06fdf2SSam Leffler 	an->an_mgmtrix = ath_tx_findrix(sc, tp->mgmtrate);
5115b032f27cSSam Leffler 
5116b032f27cSSam Leffler 	ath_rate_newassoc(sc, an, isnew);
5117e8fd88a3SSam Leffler 	if (isnew &&
5118b032f27cSSam Leffler 	    (vap->iv_flags & IEEE80211_F_PRIVACY) == 0 && sc->sc_hasclrkey &&
5119b032f27cSSam Leffler 	    ni->ni_ucastkey.wk_keyix == IEEE80211_KEYIX_NONE)
5120e8fd88a3SSam Leffler 		ath_setup_stationkey(ni);
5121e8fd88a3SSam Leffler }
51225591b213SSam Leffler 
51235591b213SSam Leffler static int
512459efa8b5SSam Leffler ath_setregdomain(struct ieee80211com *ic, struct ieee80211_regdomain *reg,
5125b032f27cSSam Leffler 	int nchans, struct ieee80211_channel chans[])
5126b032f27cSSam Leffler {
5127b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
5128b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
512959efa8b5SSam Leffler 	HAL_STATUS status;
5130b032f27cSSam Leffler 
5131033022a9SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
513259efa8b5SSam Leffler 	    "%s: rd %u cc %u location %c%s\n",
513359efa8b5SSam Leffler 	    __func__, reg->regdomain, reg->country, reg->location,
513459efa8b5SSam Leffler 	    reg->ecm ? " ecm" : "");
5135033022a9SSam Leffler 
513659efa8b5SSam Leffler 	status = ath_hal_set_channels(ah, chans, nchans,
513759efa8b5SSam Leffler 	    reg->country, reg->regdomain);
513859efa8b5SSam Leffler 	if (status != HAL_OK) {
513959efa8b5SSam Leffler 		DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: failed, status %u\n",
514059efa8b5SSam Leffler 		    __func__, status);
514159efa8b5SSam Leffler 		return EINVAL;		/* XXX */
5142b032f27cSSam Leffler 	}
51438db87e40SAdrian Chadd 
5144b032f27cSSam Leffler 	return 0;
5145b032f27cSSam Leffler }
5146b032f27cSSam Leffler 
5147b032f27cSSam Leffler static void
5148b032f27cSSam Leffler ath_getradiocaps(struct ieee80211com *ic,
51495fe9f044SSam Leffler 	int maxchans, int *nchans, struct ieee80211_channel chans[])
5150b032f27cSSam Leffler {
5151b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
5152b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5153b032f27cSSam Leffler 
515459efa8b5SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: use rd %u cc %d\n",
515559efa8b5SSam Leffler 	    __func__, SKU_DEBUG, CTRY_DEFAULT);
5156033022a9SSam Leffler 
515759efa8b5SSam Leffler 	/* XXX check return */
515859efa8b5SSam Leffler 	(void) ath_hal_getchannels(ah, chans, maxchans, nchans,
515959efa8b5SSam Leffler 	    HAL_MODE_ALL, CTRY_DEFAULT, SKU_DEBUG, AH_TRUE);
5160033022a9SSam Leffler 
5161b032f27cSSam Leffler }
5162b032f27cSSam Leffler 
5163b032f27cSSam Leffler static int
5164b032f27cSSam Leffler ath_getchannels(struct ath_softc *sc)
5165b032f27cSSam Leffler {
5166b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
5167b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
5168b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
516959efa8b5SSam Leffler 	HAL_STATUS status;
5170b032f27cSSam Leffler 
5171b032f27cSSam Leffler 	/*
517259efa8b5SSam Leffler 	 * Collect channel set based on EEPROM contents.
5173b032f27cSSam Leffler 	 */
517459efa8b5SSam Leffler 	status = ath_hal_init_channels(ah, ic->ic_channels, IEEE80211_CHAN_MAX,
517559efa8b5SSam Leffler 	    &ic->ic_nchans, HAL_MODE_ALL, CTRY_DEFAULT, SKU_NONE, AH_TRUE);
517659efa8b5SSam Leffler 	if (status != HAL_OK) {
517759efa8b5SSam Leffler 		if_printf(ifp, "%s: unable to collect channel list from hal, "
517859efa8b5SSam Leffler 		    "status %d\n", __func__, status);
517959efa8b5SSam Leffler 		return EINVAL;
518059efa8b5SSam Leffler 	}
5181ca876918SSam Leffler 	(void) ath_hal_getregdomain(ah, &sc->sc_eerd);
5182ca876918SSam Leffler 	ath_hal_getcountrycode(ah, &sc->sc_eecc);	/* NB: cannot fail */
518359efa8b5SSam Leffler 	/* XXX map Atheros sku's to net80211 SKU's */
518459efa8b5SSam Leffler 	/* XXX net80211 types too small */
518559efa8b5SSam Leffler 	ic->ic_regdomain.regdomain = (uint16_t) sc->sc_eerd;
518659efa8b5SSam Leffler 	ic->ic_regdomain.country = (uint16_t) sc->sc_eecc;
518759efa8b5SSam Leffler 	ic->ic_regdomain.isocc[0] = ' ';	/* XXX don't know */
518859efa8b5SSam Leffler 	ic->ic_regdomain.isocc[1] = ' ';
518959efa8b5SSam Leffler 
5190b032f27cSSam Leffler 	ic->ic_regdomain.ecm = 1;
5191b032f27cSSam Leffler 	ic->ic_regdomain.location = 'I';
5192033022a9SSam Leffler 
5193033022a9SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
519459efa8b5SSam Leffler 	    "%s: eeprom rd %u cc %u (mapped rd %u cc %u) location %c%s\n",
5195033022a9SSam Leffler 	    __func__, sc->sc_eerd, sc->sc_eecc,
5196033022a9SSam Leffler 	    ic->ic_regdomain.regdomain, ic->ic_regdomain.country,
519759efa8b5SSam Leffler 	    ic->ic_regdomain.location, ic->ic_regdomain.ecm ? " ecm" : "");
51985591b213SSam Leffler 	return 0;
51995591b213SSam Leffler }
52005591b213SSam Leffler 
5201c42a7b7eSSam Leffler static void
52023e50ec2cSSam Leffler ath_led_done(void *arg)
5203c42a7b7eSSam Leffler {
52043e50ec2cSSam Leffler 	struct ath_softc *sc = arg;
52053e50ec2cSSam Leffler 
52063e50ec2cSSam Leffler 	sc->sc_blinking = 0;
52073e50ec2cSSam Leffler }
5208c42a7b7eSSam Leffler 
5209c42a7b7eSSam Leffler /*
52103e50ec2cSSam Leffler  * Turn the LED off: flip the pin and then set a timer so no
52113e50ec2cSSam Leffler  * update will happen for the specified duration.
5212c42a7b7eSSam Leffler  */
52133e50ec2cSSam Leffler static void
52143e50ec2cSSam Leffler ath_led_off(void *arg)
52153e50ec2cSSam Leffler {
52163e50ec2cSSam Leffler 	struct ath_softc *sc = arg;
52173e50ec2cSSam Leffler 
52183e50ec2cSSam Leffler 	ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, !sc->sc_ledon);
52193e50ec2cSSam Leffler 	callout_reset(&sc->sc_ledtimer, sc->sc_ledoff, ath_led_done, sc);
5220c42a7b7eSSam Leffler }
52213e50ec2cSSam Leffler 
52223e50ec2cSSam Leffler /*
52233e50ec2cSSam Leffler  * Blink the LED according to the specified on/off times.
52243e50ec2cSSam Leffler  */
52253e50ec2cSSam Leffler static void
52263e50ec2cSSam Leffler ath_led_blink(struct ath_softc *sc, int on, int off)
52273e50ec2cSSam Leffler {
52283e50ec2cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_LED, "%s: on %u off %u\n", __func__, on, off);
52293e50ec2cSSam Leffler 	ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, sc->sc_ledon);
52303e50ec2cSSam Leffler 	sc->sc_blinking = 1;
52313e50ec2cSSam Leffler 	sc->sc_ledoff = off;
52323e50ec2cSSam Leffler 	callout_reset(&sc->sc_ledtimer, on, ath_led_off, sc);
52333e50ec2cSSam Leffler }
52343e50ec2cSSam Leffler 
52353e50ec2cSSam Leffler static void
523646d4d74cSSam Leffler ath_led_event(struct ath_softc *sc, int rix)
52373e50ec2cSSam Leffler {
52383e50ec2cSSam Leffler 	sc->sc_ledevent = ticks;	/* time of last event */
52393e50ec2cSSam Leffler 	if (sc->sc_blinking)		/* don't interrupt active blink */
52403e50ec2cSSam Leffler 		return;
524146d4d74cSSam Leffler 	ath_led_blink(sc, sc->sc_hwmap[rix].ledon, sc->sc_hwmap[rix].ledoff);
5242c42a7b7eSSam Leffler }
5243c42a7b7eSSam Leffler 
52446c4612b9SSam Leffler static int
52456c4612b9SSam Leffler ath_rate_setup(struct ath_softc *sc, u_int mode)
52466c4612b9SSam Leffler {
52476c4612b9SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
52486c4612b9SSam Leffler 	const HAL_RATE_TABLE *rt;
52496c4612b9SSam Leffler 
52506c4612b9SSam Leffler 	switch (mode) {
52516c4612b9SSam Leffler 	case IEEE80211_MODE_11A:
52526c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A);
52536c4612b9SSam Leffler 		break;
5254724c193aSSam Leffler 	case IEEE80211_MODE_HALF:
5255aaa70f2fSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_HALF_RATE);
5256aaa70f2fSSam Leffler 		break;
5257724c193aSSam Leffler 	case IEEE80211_MODE_QUARTER:
5258aaa70f2fSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_QUARTER_RATE);
5259aaa70f2fSSam Leffler 		break;
52606c4612b9SSam Leffler 	case IEEE80211_MODE_11B:
52616c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11B);
52626c4612b9SSam Leffler 		break;
52636c4612b9SSam Leffler 	case IEEE80211_MODE_11G:
52646c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11G);
52656c4612b9SSam Leffler 		break;
52666c4612b9SSam Leffler 	case IEEE80211_MODE_TURBO_A:
526768e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_108A);
52686c4612b9SSam Leffler 		break;
52696c4612b9SSam Leffler 	case IEEE80211_MODE_TURBO_G:
52706c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_108G);
52716c4612b9SSam Leffler 		break;
527268e8e04eSSam Leffler 	case IEEE80211_MODE_STURBO_A:
527368e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_TURBO);
527468e8e04eSSam Leffler 		break;
527568e8e04eSSam Leffler 	case IEEE80211_MODE_11NA:
527668e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11NA_HT20);
527768e8e04eSSam Leffler 		break;
527868e8e04eSSam Leffler 	case IEEE80211_MODE_11NG:
527968e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11NG_HT20);
528068e8e04eSSam Leffler 		break;
52816c4612b9SSam Leffler 	default:
52826c4612b9SSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid mode %u\n",
52836c4612b9SSam Leffler 			__func__, mode);
52846c4612b9SSam Leffler 		return 0;
52856c4612b9SSam Leffler 	}
52866c4612b9SSam Leffler 	sc->sc_rates[mode] = rt;
5287aaa70f2fSSam Leffler 	return (rt != NULL);
52885591b213SSam Leffler }
52895591b213SSam Leffler 
52905591b213SSam Leffler static void
52915591b213SSam Leffler ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode)
52925591b213SSam Leffler {
52933e50ec2cSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
52943e50ec2cSSam Leffler 	/* NB: on/off times from the Atheros NDIS driver, w/ permission */
52953e50ec2cSSam Leffler 	static const struct {
52963e50ec2cSSam Leffler 		u_int		rate;		/* tx/rx 802.11 rate */
52973e50ec2cSSam Leffler 		u_int16_t	timeOn;		/* LED on time (ms) */
52983e50ec2cSSam Leffler 		u_int16_t	timeOff;	/* LED off time (ms) */
52993e50ec2cSSam Leffler 	} blinkrates[] = {
53003e50ec2cSSam Leffler 		{ 108,  40,  10 },
53013e50ec2cSSam Leffler 		{  96,  44,  11 },
53023e50ec2cSSam Leffler 		{  72,  50,  13 },
53033e50ec2cSSam Leffler 		{  48,  57,  14 },
53043e50ec2cSSam Leffler 		{  36,  67,  16 },
53053e50ec2cSSam Leffler 		{  24,  80,  20 },
53063e50ec2cSSam Leffler 		{  22, 100,  25 },
53073e50ec2cSSam Leffler 		{  18, 133,  34 },
53083e50ec2cSSam Leffler 		{  12, 160,  40 },
53093e50ec2cSSam Leffler 		{  10, 200,  50 },
53103e50ec2cSSam Leffler 		{   6, 240,  58 },
53113e50ec2cSSam Leffler 		{   4, 267,  66 },
53123e50ec2cSSam Leffler 		{   2, 400, 100 },
53133e50ec2cSSam Leffler 		{   0, 500, 130 },
5314724c193aSSam Leffler 		/* XXX half/quarter rates */
53153e50ec2cSSam Leffler 	};
53165591b213SSam Leffler 	const HAL_RATE_TABLE *rt;
53173e50ec2cSSam Leffler 	int i, j;
53185591b213SSam Leffler 
53195591b213SSam Leffler 	memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap));
53205591b213SSam Leffler 	rt = sc->sc_rates[mode];
53215591b213SSam Leffler 	KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode));
5322180f268dSSam Leffler 	for (i = 0; i < rt->rateCount; i++) {
5323180f268dSSam Leffler 		uint8_t ieeerate = rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
5324180f268dSSam Leffler 		if (rt->info[i].phy != IEEE80211_T_HT)
5325180f268dSSam Leffler 			sc->sc_rixmap[ieeerate] = i;
5326180f268dSSam Leffler 		else
5327180f268dSSam Leffler 			sc->sc_rixmap[ieeerate | IEEE80211_RATE_MCS] = i;
5328180f268dSSam Leffler 	}
53291b1a8e41SSam Leffler 	memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap));
533046d4d74cSSam Leffler 	for (i = 0; i < N(sc->sc_hwmap); i++) {
533146d4d74cSSam Leffler 		if (i >= rt->rateCount) {
53323e50ec2cSSam Leffler 			sc->sc_hwmap[i].ledon = (500 * hz) / 1000;
53333e50ec2cSSam Leffler 			sc->sc_hwmap[i].ledoff = (130 * hz) / 1000;
533416b4851aSSam Leffler 			continue;
53353e50ec2cSSam Leffler 		}
53363e50ec2cSSam Leffler 		sc->sc_hwmap[i].ieeerate =
533746d4d74cSSam Leffler 			rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
533846d4d74cSSam Leffler 		if (rt->info[i].phy == IEEE80211_T_HT)
533926041a14SSam Leffler 			sc->sc_hwmap[i].ieeerate |= IEEE80211_RATE_MCS;
5340d3be6f5bSSam Leffler 		sc->sc_hwmap[i].txflags = IEEE80211_RADIOTAP_F_DATAPAD;
534146d4d74cSSam Leffler 		if (rt->info[i].shortPreamble ||
534246d4d74cSSam Leffler 		    rt->info[i].phy == IEEE80211_T_OFDM)
5343d3be6f5bSSam Leffler 			sc->sc_hwmap[i].txflags |= IEEE80211_RADIOTAP_F_SHORTPRE;
53445463c4a4SSam Leffler 		sc->sc_hwmap[i].rxflags = sc->sc_hwmap[i].txflags;
53453e50ec2cSSam Leffler 		for (j = 0; j < N(blinkrates)-1; j++)
53463e50ec2cSSam Leffler 			if (blinkrates[j].rate == sc->sc_hwmap[i].ieeerate)
53473e50ec2cSSam Leffler 				break;
53483e50ec2cSSam Leffler 		/* NB: this uses the last entry if the rate isn't found */
53493e50ec2cSSam Leffler 		/* XXX beware of overlow */
53503e50ec2cSSam Leffler 		sc->sc_hwmap[i].ledon = (blinkrates[j].timeOn * hz) / 1000;
53513e50ec2cSSam Leffler 		sc->sc_hwmap[i].ledoff = (blinkrates[j].timeOff * hz) / 1000;
5352c42a7b7eSSam Leffler 	}
53535591b213SSam Leffler 	sc->sc_currates = rt;
53545591b213SSam Leffler 	sc->sc_curmode = mode;
53555591b213SSam Leffler 	/*
5356c42a7b7eSSam Leffler 	 * All protection frames are transmited at 2Mb/s for
5357c42a7b7eSSam Leffler 	 * 11g, otherwise at 1Mb/s.
53585591b213SSam Leffler 	 */
5359913a1ba1SSam Leffler 	if (mode == IEEE80211_MODE_11G)
5360ab06fdf2SSam Leffler 		sc->sc_protrix = ath_tx_findrix(sc, 2*2);
5361913a1ba1SSam Leffler 	else
5362ab06fdf2SSam Leffler 		sc->sc_protrix = ath_tx_findrix(sc, 2*1);
53634fa8d4efSDaniel Eischen 	/* NB: caller is responsible for resetting rate control state */
53643e50ec2cSSam Leffler #undef N
53655591b213SSam Leffler }
53665591b213SSam Leffler 
5367c42a7b7eSSam Leffler static void
53682e986da5SSam Leffler ath_watchdog(void *arg)
5369c42a7b7eSSam Leffler {
53702e986da5SSam Leffler 	struct ath_softc *sc = arg;
5371c42a7b7eSSam Leffler 
53722e986da5SSam Leffler 	if (sc->sc_wd_timer != 0 && --sc->sc_wd_timer == 0) {
53732e986da5SSam Leffler 		struct ifnet *ifp = sc->sc_ifp;
5374459bc4f0SSam Leffler 		uint32_t hangs;
5375459bc4f0SSam Leffler 
5376459bc4f0SSam Leffler 		if (ath_hal_gethangstate(sc->sc_ah, 0xffff, &hangs) &&
5377459bc4f0SSam Leffler 		    hangs != 0) {
5378459bc4f0SSam Leffler 			if_printf(ifp, "%s hang detected (0x%x)\n",
5379459bc4f0SSam Leffler 			    hangs & 0xff ? "bb" : "mac", hangs);
5380459bc4f0SSam Leffler 		} else
5381c42a7b7eSSam Leffler 			if_printf(ifp, "device timeout\n");
5382c42a7b7eSSam Leffler 		ath_reset(ifp);
5383c42a7b7eSSam Leffler 		ifp->if_oerrors++;
5384c42a7b7eSSam Leffler 		sc->sc_stats.ast_watchdog++;
5385c42a7b7eSSam Leffler 	}
53862e986da5SSam Leffler 	callout_schedule(&sc->sc_wd_ch, hz);
5387c42a7b7eSSam Leffler }
5388c42a7b7eSSam Leffler 
5389a585a9a1SSam Leffler #ifdef ATH_DIAGAPI
5390c42a7b7eSSam Leffler /*
5391c42a7b7eSSam Leffler  * Diagnostic interface to the HAL.  This is used by various
5392c42a7b7eSSam Leffler  * tools to do things like retrieve register contents for
5393c42a7b7eSSam Leffler  * debugging.  The mechanism is intentionally opaque so that
5394c42a7b7eSSam Leffler  * it can change frequently w/o concern for compatiblity.
5395c42a7b7eSSam Leffler  */
5396c42a7b7eSSam Leffler static int
5397c42a7b7eSSam Leffler ath_ioctl_diag(struct ath_softc *sc, struct ath_diag *ad)
5398c42a7b7eSSam Leffler {
5399c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5400c42a7b7eSSam Leffler 	u_int id = ad->ad_id & ATH_DIAG_ID;
5401c42a7b7eSSam Leffler 	void *indata = NULL;
5402c42a7b7eSSam Leffler 	void *outdata = NULL;
5403c42a7b7eSSam Leffler 	u_int32_t insize = ad->ad_in_size;
5404c42a7b7eSSam Leffler 	u_int32_t outsize = ad->ad_out_size;
5405c42a7b7eSSam Leffler 	int error = 0;
5406c42a7b7eSSam Leffler 
5407c42a7b7eSSam Leffler 	if (ad->ad_id & ATH_DIAG_IN) {
5408c42a7b7eSSam Leffler 		/*
5409c42a7b7eSSam Leffler 		 * Copy in data.
5410c42a7b7eSSam Leffler 		 */
5411c42a7b7eSSam Leffler 		indata = malloc(insize, M_TEMP, M_NOWAIT);
5412c42a7b7eSSam Leffler 		if (indata == NULL) {
5413c42a7b7eSSam Leffler 			error = ENOMEM;
5414c42a7b7eSSam Leffler 			goto bad;
5415c42a7b7eSSam Leffler 		}
5416c42a7b7eSSam Leffler 		error = copyin(ad->ad_in_data, indata, insize);
5417c42a7b7eSSam Leffler 		if (error)
5418c42a7b7eSSam Leffler 			goto bad;
5419c42a7b7eSSam Leffler 	}
5420c42a7b7eSSam Leffler 	if (ad->ad_id & ATH_DIAG_DYN) {
5421c42a7b7eSSam Leffler 		/*
5422c42a7b7eSSam Leffler 		 * Allocate a buffer for the results (otherwise the HAL
5423c42a7b7eSSam Leffler 		 * returns a pointer to a buffer where we can read the
5424c42a7b7eSSam Leffler 		 * results).  Note that we depend on the HAL leaving this
5425c42a7b7eSSam Leffler 		 * pointer for us to use below in reclaiming the buffer;
5426c42a7b7eSSam Leffler 		 * may want to be more defensive.
5427c42a7b7eSSam Leffler 		 */
5428c42a7b7eSSam Leffler 		outdata = malloc(outsize, M_TEMP, M_NOWAIT);
5429c42a7b7eSSam Leffler 		if (outdata == NULL) {
5430c42a7b7eSSam Leffler 			error = ENOMEM;
5431c42a7b7eSSam Leffler 			goto bad;
5432c42a7b7eSSam Leffler 		}
5433c42a7b7eSSam Leffler 	}
5434c42a7b7eSSam Leffler 	if (ath_hal_getdiagstate(ah, id, indata, insize, &outdata, &outsize)) {
5435c42a7b7eSSam Leffler 		if (outsize < ad->ad_out_size)
5436c42a7b7eSSam Leffler 			ad->ad_out_size = outsize;
5437c42a7b7eSSam Leffler 		if (outdata != NULL)
5438c42a7b7eSSam Leffler 			error = copyout(outdata, ad->ad_out_data,
5439c42a7b7eSSam Leffler 					ad->ad_out_size);
5440c42a7b7eSSam Leffler 	} else {
5441c42a7b7eSSam Leffler 		error = EINVAL;
5442c42a7b7eSSam Leffler 	}
5443c42a7b7eSSam Leffler bad:
5444c42a7b7eSSam Leffler 	if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
5445c42a7b7eSSam Leffler 		free(indata, M_TEMP);
5446c42a7b7eSSam Leffler 	if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
5447c42a7b7eSSam Leffler 		free(outdata, M_TEMP);
5448c42a7b7eSSam Leffler 	return error;
5449c42a7b7eSSam Leffler }
5450a585a9a1SSam Leffler #endif /* ATH_DIAGAPI */
5451c42a7b7eSSam Leffler 
5452c42a7b7eSSam Leffler static int
5453c42a7b7eSSam Leffler ath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
5454c42a7b7eSSam Leffler {
5455c42a7b7eSSam Leffler #define	IS_RUNNING(ifp) \
545613f4c340SRobert Watson 	((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
5457c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
5458b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
5459c42a7b7eSSam Leffler 	struct ifreq *ifr = (struct ifreq *)data;
546084784be1SSam Leffler 	const HAL_RATE_TABLE *rt;
5461c42a7b7eSSam Leffler 	int error = 0;
5462c42a7b7eSSam Leffler 
5463c42a7b7eSSam Leffler 	switch (cmd) {
5464c42a7b7eSSam Leffler 	case SIOCSIFFLAGS:
546531a8c1edSAndrew Thompson 		ATH_LOCK(sc);
5466c42a7b7eSSam Leffler 		if (IS_RUNNING(ifp)) {
5467c42a7b7eSSam Leffler 			/*
5468c42a7b7eSSam Leffler 			 * To avoid rescanning another access point,
5469c42a7b7eSSam Leffler 			 * do not call ath_init() here.  Instead,
5470c42a7b7eSSam Leffler 			 * only reflect promisc mode settings.
5471c42a7b7eSSam Leffler 			 */
5472c42a7b7eSSam Leffler 			ath_mode_init(sc);
5473c42a7b7eSSam Leffler 		} else if (ifp->if_flags & IFF_UP) {
5474c42a7b7eSSam Leffler 			/*
5475c42a7b7eSSam Leffler 			 * Beware of being called during attach/detach
5476c42a7b7eSSam Leffler 			 * to reset promiscuous mode.  In that case we
5477c42a7b7eSSam Leffler 			 * will still be marked UP but not RUNNING.
5478c42a7b7eSSam Leffler 			 * However trying to re-init the interface
5479c42a7b7eSSam Leffler 			 * is the wrong thing to do as we've already
5480c42a7b7eSSam Leffler 			 * torn down much of our state.  There's
5481c42a7b7eSSam Leffler 			 * probably a better way to deal with this.
5482c42a7b7eSSam Leffler 			 */
5483b032f27cSSam Leffler 			if (!sc->sc_invalid)
5484fc74a9f9SBrooks Davis 				ath_init(sc);	/* XXX lose error */
5485d3ac945bSSam Leffler 		} else {
5486c42a7b7eSSam Leffler 			ath_stop_locked(ifp);
5487d3ac945bSSam Leffler #ifdef notyet
5488d3ac945bSSam Leffler 			/* XXX must wakeup in places like ath_vap_delete */
5489d3ac945bSSam Leffler 			if (!sc->sc_invalid)
5490d3ac945bSSam Leffler 				ath_hal_setpower(sc->sc_ah, HAL_PM_FULL_SLEEP);
5491d3ac945bSSam Leffler #endif
5492d3ac945bSSam Leffler 		}
549331a8c1edSAndrew Thompson 		ATH_UNLOCK(sc);
5494c42a7b7eSSam Leffler 		break;
5495b032f27cSSam Leffler 	case SIOCGIFMEDIA:
5496b032f27cSSam Leffler 	case SIOCSIFMEDIA:
5497b032f27cSSam Leffler 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
5498b032f27cSSam Leffler 		break;
5499c42a7b7eSSam Leffler 	case SIOCGATHSTATS:
5500c42a7b7eSSam Leffler 		/* NB: embed these numbers to get a consistent view */
5501c42a7b7eSSam Leffler 		sc->sc_stats.ast_tx_packets = ifp->if_opackets;
5502c42a7b7eSSam Leffler 		sc->sc_stats.ast_rx_packets = ifp->if_ipackets;
550384784be1SSam Leffler 		sc->sc_stats.ast_tx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgtxrssi);
550484784be1SSam Leffler 		sc->sc_stats.ast_rx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgrssi);
5505584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
550610ad9a77SSam Leffler 		sc->sc_stats.ast_tdma_tsfadjp = TDMA_AVG(sc->sc_avgtsfdeltap);
550710ad9a77SSam Leffler 		sc->sc_stats.ast_tdma_tsfadjm = TDMA_AVG(sc->sc_avgtsfdeltam);
550810ad9a77SSam Leffler #endif
550984784be1SSam Leffler 		rt = sc->sc_currates;
551046d4d74cSSam Leffler 		sc->sc_stats.ast_tx_rate =
551146d4d74cSSam Leffler 		    rt->info[sc->sc_txrix].dot11Rate &~ IEEE80211_RATE_BASIC;
55126aa113fdSAdrian Chadd 		if (rt->info[sc->sc_txrix].phy & IEEE80211_T_HT)
55136aa113fdSAdrian Chadd 			sc->sc_stats.ast_tx_rate |= IEEE80211_RATE_MCS;
5514c42a7b7eSSam Leffler 		return copyout(&sc->sc_stats,
5515c42a7b7eSSam Leffler 		    ifr->ifr_data, sizeof (sc->sc_stats));
55163fc21fedSSam Leffler 	case SIOCZATHSTATS:
55173fc21fedSSam Leffler 		error = priv_check(curthread, PRIV_DRIVER);
55183fc21fedSSam Leffler 		if (error == 0)
55193fc21fedSSam Leffler 			memset(&sc->sc_stats, 0, sizeof(sc->sc_stats));
55203fc21fedSSam Leffler 		break;
5521a585a9a1SSam Leffler #ifdef ATH_DIAGAPI
5522c42a7b7eSSam Leffler 	case SIOCGATHDIAG:
5523c42a7b7eSSam Leffler 		error = ath_ioctl_diag(sc, (struct ath_diag *) ifr);
5524c42a7b7eSSam Leffler 		break;
5525f51c84eaSAdrian Chadd 	case SIOCGATHPHYERR:
5526f51c84eaSAdrian Chadd 		error = ath_ioctl_phyerr(sc,(struct ath_diag*) ifr);
5527f51c84eaSAdrian Chadd 		break;
5528a585a9a1SSam Leffler #endif
552931a8c1edSAndrew Thompson 	case SIOCGIFADDR:
5530b032f27cSSam Leffler 		error = ether_ioctl(ifp, cmd, data);
5531c42a7b7eSSam Leffler 		break;
553231a8c1edSAndrew Thompson 	default:
553331a8c1edSAndrew Thompson 		error = EINVAL;
553431a8c1edSAndrew Thompson 		break;
5535c42a7b7eSSam Leffler 	}
5536c42a7b7eSSam Leffler 	return error;
5537a614e076SSam Leffler #undef IS_RUNNING
5538c42a7b7eSSam Leffler }
5539c42a7b7eSSam Leffler 
5540c42a7b7eSSam Leffler /*
5541c42a7b7eSSam Leffler  * Announce various information on device/driver attach.
5542c42a7b7eSSam Leffler  */
5543c42a7b7eSSam Leffler static void
5544c42a7b7eSSam Leffler ath_announce(struct ath_softc *sc)
5545c42a7b7eSSam Leffler {
5546fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
5547c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5548c42a7b7eSSam Leffler 
5549498657cfSSam Leffler 	if_printf(ifp, "AR%s mac %d.%d RF%s phy %d.%d\n",
5550498657cfSSam Leffler 		ath_hal_mac_name(ah), ah->ah_macVersion, ah->ah_macRev,
5551498657cfSSam Leffler 		ath_hal_rf_name(ah), ah->ah_phyRev >> 4, ah->ah_phyRev & 0xf);
5552c42a7b7eSSam Leffler 	if (bootverbose) {
5553c42a7b7eSSam Leffler 		int i;
5554c42a7b7eSSam Leffler 		for (i = 0; i <= WME_AC_VO; i++) {
5555c42a7b7eSSam Leffler 			struct ath_txq *txq = sc->sc_ac2q[i];
5556c42a7b7eSSam Leffler 			if_printf(ifp, "Use hw queue %u for %s traffic\n",
5557c42a7b7eSSam Leffler 				txq->axq_qnum, ieee80211_wme_acnames[i]);
5558c42a7b7eSSam Leffler 		}
5559c42a7b7eSSam Leffler 		if_printf(ifp, "Use hw queue %u for CAB traffic\n",
5560c42a7b7eSSam Leffler 			sc->sc_cabq->axq_qnum);
5561c42a7b7eSSam Leffler 		if_printf(ifp, "Use hw queue %u for beacons\n", sc->sc_bhalq);
5562c42a7b7eSSam Leffler 	}
5563e2d787faSSam Leffler 	if (ath_rxbuf != ATH_RXBUF)
5564e2d787faSSam Leffler 		if_printf(ifp, "using %u rx buffers\n", ath_rxbuf);
5565e2d787faSSam Leffler 	if (ath_txbuf != ATH_TXBUF)
5566e2d787faSSam Leffler 		if_printf(ifp, "using %u tx buffers\n", ath_txbuf);
55679ac01d39SRui Paulo 	if (sc->sc_mcastkey && bootverbose)
55689ac01d39SRui Paulo 		if_printf(ifp, "using multicast key search\n");
5569c42a7b7eSSam Leffler }
557010ad9a77SSam Leffler 
5571584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
557210ad9a77SSam Leffler static void
557310ad9a77SSam Leffler ath_tdma_settimers(struct ath_softc *sc, u_int32_t nexttbtt, u_int32_t bintval)
557410ad9a77SSam Leffler {
557510ad9a77SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
557610ad9a77SSam Leffler 	HAL_BEACON_TIMERS bt;
557710ad9a77SSam Leffler 
557810ad9a77SSam Leffler 	bt.bt_intval = bintval | HAL_BEACON_ENA;
557910ad9a77SSam Leffler 	bt.bt_nexttbtt = nexttbtt;
558010ad9a77SSam Leffler 	bt.bt_nextdba = (nexttbtt<<3) - sc->sc_tdmadbaprep;
558110ad9a77SSam Leffler 	bt.bt_nextswba = (nexttbtt<<3) - sc->sc_tdmaswbaprep;
558210ad9a77SSam Leffler 	bt.bt_nextatim = nexttbtt+1;
5583f3fb1687SAdrian Chadd 	/* Enables TBTT, DBA, SWBA timers by default */
5584f3fb1687SAdrian Chadd 	bt.bt_flags = 0;
558510ad9a77SSam Leffler 	ath_hal_beaconsettimers(ah, &bt);
558610ad9a77SSam Leffler }
558710ad9a77SSam Leffler 
558810ad9a77SSam Leffler /*
558910ad9a77SSam Leffler  * Calculate the beacon interval.  This is periodic in the
559010ad9a77SSam Leffler  * superframe for the bss.  We assume each station is configured
559110ad9a77SSam Leffler  * identically wrt transmit rate so the guard time we calculate
559210ad9a77SSam Leffler  * above will be the same on all stations.  Note we need to
559310ad9a77SSam Leffler  * factor in the xmit time because the hardware will schedule
559410ad9a77SSam Leffler  * a frame for transmit if the start of the frame is within
559510ad9a77SSam Leffler  * the burst time.  When we get hardware that properly kills
559610ad9a77SSam Leffler  * frames in the PCU we can reduce/eliminate the guard time.
559710ad9a77SSam Leffler  *
559810ad9a77SSam Leffler  * Roundup to 1024 is so we have 1 TU buffer in the guard time
559910ad9a77SSam Leffler  * to deal with the granularity of the nexttbtt timer.  11n MAC's
560010ad9a77SSam Leffler  * with 1us timer granularity should allow us to reduce/eliminate
560110ad9a77SSam Leffler  * this.
560210ad9a77SSam Leffler  */
560310ad9a77SSam Leffler static void
560410ad9a77SSam Leffler ath_tdma_bintvalsetup(struct ath_softc *sc,
560510ad9a77SSam Leffler 	const struct ieee80211_tdma_state *tdma)
560610ad9a77SSam Leffler {
560710ad9a77SSam Leffler 	/* copy from vap state (XXX check all vaps have same value?) */
560810ad9a77SSam Leffler 	sc->sc_tdmaslotlen = tdma->tdma_slotlen;
560910ad9a77SSam Leffler 
561010ad9a77SSam Leffler 	sc->sc_tdmabintval = roundup((sc->sc_tdmaslotlen+sc->sc_tdmaguard) *
561110ad9a77SSam Leffler 		tdma->tdma_slotcnt, 1024);
561210ad9a77SSam Leffler 	sc->sc_tdmabintval >>= 10;		/* TSF -> TU */
561310ad9a77SSam Leffler 	if (sc->sc_tdmabintval & 1)
561410ad9a77SSam Leffler 		sc->sc_tdmabintval++;
561510ad9a77SSam Leffler 
561610ad9a77SSam Leffler 	if (tdma->tdma_slot == 0) {
561710ad9a77SSam Leffler 		/*
561810ad9a77SSam Leffler 		 * Only slot 0 beacons; other slots respond.
561910ad9a77SSam Leffler 		 */
562010ad9a77SSam Leffler 		sc->sc_imask |= HAL_INT_SWBA;
562110ad9a77SSam Leffler 		sc->sc_tdmaswba = 0;		/* beacon immediately */
562210ad9a77SSam Leffler 	} else {
562310ad9a77SSam Leffler 		/* XXX all vaps must be slot 0 or slot !0 */
562410ad9a77SSam Leffler 		sc->sc_imask &= ~HAL_INT_SWBA;
562510ad9a77SSam Leffler 	}
562610ad9a77SSam Leffler }
562710ad9a77SSam Leffler 
562810ad9a77SSam Leffler /*
562910ad9a77SSam Leffler  * Max 802.11 overhead.  This assumes no 4-address frames and
563010ad9a77SSam Leffler  * the encapsulation done by ieee80211_encap (llc).  We also
563110ad9a77SSam Leffler  * include potential crypto overhead.
563210ad9a77SSam Leffler  */
563310ad9a77SSam Leffler #define	IEEE80211_MAXOVERHEAD \
563410ad9a77SSam Leffler 	(sizeof(struct ieee80211_qosframe) \
563510ad9a77SSam Leffler 	 + sizeof(struct llc) \
563610ad9a77SSam Leffler 	 + IEEE80211_ADDR_LEN \
563710ad9a77SSam Leffler 	 + IEEE80211_WEP_IVLEN \
563810ad9a77SSam Leffler 	 + IEEE80211_WEP_KIDLEN \
563910ad9a77SSam Leffler 	 + IEEE80211_WEP_CRCLEN \
564010ad9a77SSam Leffler 	 + IEEE80211_WEP_MICLEN \
564110ad9a77SSam Leffler 	 + IEEE80211_CRC_LEN)
564210ad9a77SSam Leffler 
564310ad9a77SSam Leffler /*
564410ad9a77SSam Leffler  * Setup initially for tdma operation.  Start the beacon
564510ad9a77SSam Leffler  * timers and enable SWBA if we are slot 0.  Otherwise
564610ad9a77SSam Leffler  * we wait for slot 0 to arrive so we can sync up before
564710ad9a77SSam Leffler  * starting to transmit.
564810ad9a77SSam Leffler  */
564910ad9a77SSam Leffler static void
565010ad9a77SSam Leffler ath_tdma_config(struct ath_softc *sc, struct ieee80211vap *vap)
565110ad9a77SSam Leffler {
565210ad9a77SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
565310ad9a77SSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
565410ad9a77SSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
565510ad9a77SSam Leffler 	const struct ieee80211_txparam *tp;
565610ad9a77SSam Leffler 	const struct ieee80211_tdma_state *tdma = NULL;
565710ad9a77SSam Leffler 	int rix;
565810ad9a77SSam Leffler 
565910ad9a77SSam Leffler 	if (vap == NULL) {
566010ad9a77SSam Leffler 		vap = TAILQ_FIRST(&ic->ic_vaps);   /* XXX */
566110ad9a77SSam Leffler 		if (vap == NULL) {
566210ad9a77SSam Leffler 			if_printf(ifp, "%s: no vaps?\n", __func__);
566310ad9a77SSam Leffler 			return;
566410ad9a77SSam Leffler 		}
566510ad9a77SSam Leffler 	}
566610ad9a77SSam Leffler 	tp = vap->iv_bss->ni_txparms;
566710ad9a77SSam Leffler 	/*
566810ad9a77SSam Leffler 	 * Calculate the guard time for each slot.  This is the
566910ad9a77SSam Leffler 	 * time to send a maximal-size frame according to the
567010ad9a77SSam Leffler 	 * fixed/lowest transmit rate.  Note that the interface
567110ad9a77SSam Leffler 	 * mtu does not include the 802.11 overhead so we must
567210ad9a77SSam Leffler 	 * tack that on (ath_hal_computetxtime includes the
567310ad9a77SSam Leffler 	 * preamble and plcp in it's calculation).
567410ad9a77SSam Leffler 	 */
567510ad9a77SSam Leffler 	tdma = vap->iv_tdma;
567610ad9a77SSam Leffler 	if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
5677ab06fdf2SSam Leffler 		rix = ath_tx_findrix(sc, tp->ucastrate);
567810ad9a77SSam Leffler 	else
5679ab06fdf2SSam Leffler 		rix = ath_tx_findrix(sc, tp->mcastrate);
568010ad9a77SSam Leffler 	/* XXX short preamble assumed */
568110ad9a77SSam Leffler 	sc->sc_tdmaguard = ath_hal_computetxtime(ah, sc->sc_currates,
568210ad9a77SSam Leffler 		ifp->if_mtu + IEEE80211_MAXOVERHEAD, rix, AH_TRUE);
568310ad9a77SSam Leffler 
568410ad9a77SSam Leffler 	ath_hal_intrset(ah, 0);
568510ad9a77SSam Leffler 
568610ad9a77SSam Leffler 	ath_beaconq_config(sc);			/* setup h/w beacon q */
56879c859a04SSam Leffler 	if (sc->sc_setcca)
568810ad9a77SSam Leffler 		ath_hal_setcca(ah, AH_FALSE);	/* disable CCA */
568910ad9a77SSam Leffler 	ath_tdma_bintvalsetup(sc, tdma);	/* calculate beacon interval */
569010ad9a77SSam Leffler 	ath_tdma_settimers(sc, sc->sc_tdmabintval,
569110ad9a77SSam Leffler 		sc->sc_tdmabintval | HAL_BEACON_RESET_TSF);
569210ad9a77SSam Leffler 	sc->sc_syncbeacon = 0;
569310ad9a77SSam Leffler 
569410ad9a77SSam Leffler 	sc->sc_avgtsfdeltap = TDMA_DUMMY_MARKER;
569510ad9a77SSam Leffler 	sc->sc_avgtsfdeltam = TDMA_DUMMY_MARKER;
569610ad9a77SSam Leffler 
569710ad9a77SSam Leffler 	ath_hal_intrset(ah, sc->sc_imask);
569810ad9a77SSam Leffler 
569910ad9a77SSam Leffler 	DPRINTF(sc, ATH_DEBUG_TDMA, "%s: slot %u len %uus cnt %u "
570010ad9a77SSam Leffler 	    "bsched %u guard %uus bintval %u TU dba prep %u\n", __func__,
570110ad9a77SSam Leffler 	    tdma->tdma_slot, tdma->tdma_slotlen, tdma->tdma_slotcnt,
570210ad9a77SSam Leffler 	    tdma->tdma_bintval, sc->sc_tdmaguard, sc->sc_tdmabintval,
570310ad9a77SSam Leffler 	    sc->sc_tdmadbaprep);
570410ad9a77SSam Leffler }
570510ad9a77SSam Leffler 
570610ad9a77SSam Leffler /*
570710ad9a77SSam Leffler  * Update tdma operation.  Called from the 802.11 layer
570810ad9a77SSam Leffler  * when a beacon is received from the TDMA station operating
570910ad9a77SSam Leffler  * in the slot immediately preceding us in the bss.  Use
571010ad9a77SSam Leffler  * the rx timestamp for the beacon frame to update our
571110ad9a77SSam Leffler  * beacon timers so we follow their schedule.  Note that
571210ad9a77SSam Leffler  * by using the rx timestamp we implicitly include the
571310ad9a77SSam Leffler  * propagation delay in our schedule.
571410ad9a77SSam Leffler  */
571510ad9a77SSam Leffler static void
571610ad9a77SSam Leffler ath_tdma_update(struct ieee80211_node *ni,
57172bc3ce77SSam Leffler 	const struct ieee80211_tdma_param *tdma, int changed)
571810ad9a77SSam Leffler {
571910ad9a77SSam Leffler #define	TSF_TO_TU(_h,_l) \
572010ad9a77SSam Leffler 	((((u_int32_t)(_h)) << 22) | (((u_int32_t)(_l)) >> 10))
572110ad9a77SSam Leffler #define	TU_TO_TSF(_tu)	(((u_int64_t)(_tu)) << 10)
572210ad9a77SSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
572310ad9a77SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
572410ad9a77SSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
572510ad9a77SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
572610ad9a77SSam Leffler 	const HAL_RATE_TABLE *rt = sc->sc_currates;
5727fc4de9b7SAdrian Chadd 	u_int64_t tsf, rstamp, nextslot, nexttbtt;
5728fc4de9b7SAdrian Chadd 	u_int32_t txtime, nextslottu;
572910ad9a77SSam Leffler 	int32_t tudelta, tsfdelta;
573010ad9a77SSam Leffler 	const struct ath_rx_status *rs;
573110ad9a77SSam Leffler 	int rix;
573210ad9a77SSam Leffler 
573310ad9a77SSam Leffler 	sc->sc_stats.ast_tdma_update++;
573410ad9a77SSam Leffler 
573510ad9a77SSam Leffler 	/*
573610ad9a77SSam Leffler 	 * Check for and adopt configuration changes.
573710ad9a77SSam Leffler 	 */
57382bc3ce77SSam Leffler 	if (changed != 0) {
573910ad9a77SSam Leffler 		const struct ieee80211_tdma_state *ts = vap->iv_tdma;
574010ad9a77SSam Leffler 
574110ad9a77SSam Leffler 		ath_tdma_bintvalsetup(sc, ts);
5742040972a1SSam Leffler 		if (changed & TDMA_UPDATE_SLOTLEN)
5743040972a1SSam Leffler 			ath_wme_update(ic);
574410ad9a77SSam Leffler 
574510ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_TDMA,
574610ad9a77SSam Leffler 		    "%s: adopt slot %u slotcnt %u slotlen %u us "
574710ad9a77SSam Leffler 		    "bintval %u TU\n", __func__,
574810ad9a77SSam Leffler 		    ts->tdma_slot, ts->tdma_slotcnt, ts->tdma_slotlen,
574910ad9a77SSam Leffler 		    sc->sc_tdmabintval);
575010ad9a77SSam Leffler 
575110ad9a77SSam Leffler 		/* XXX right? */
575210ad9a77SSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
575310ad9a77SSam Leffler 		/* NB: beacon timers programmed below */
575410ad9a77SSam Leffler 	}
575510ad9a77SSam Leffler 
575610ad9a77SSam Leffler 	/* extend rx timestamp to 64 bits */
57575463c4a4SSam Leffler 	rs = sc->sc_lastrs;
575810ad9a77SSam Leffler 	tsf = ath_hal_gettsf64(ah);
5759fc4de9b7SAdrian Chadd 	rstamp = ath_extend_tsf(sc, rs->rs_tstamp, tsf);
576010ad9a77SSam Leffler 	/*
576110ad9a77SSam Leffler 	 * The rx timestamp is set by the hardware on completing
576210ad9a77SSam Leffler 	 * reception (at the point where the rx descriptor is DMA'd
576310ad9a77SSam Leffler 	 * to the host).  To find the start of our next slot we
576410ad9a77SSam Leffler 	 * must adjust this time by the time required to send
576510ad9a77SSam Leffler 	 * the packet just received.
576610ad9a77SSam Leffler 	 */
576710ad9a77SSam Leffler 	rix = rt->rateCodeToIndex[rs->rs_rate];
576810ad9a77SSam Leffler 	txtime = ath_hal_computetxtime(ah, rt, rs->rs_datalen, rix,
576910ad9a77SSam Leffler 	    rt->info[rix].shortPreamble);
577010ad9a77SSam Leffler 	/* NB: << 9 is to cvt to TU and /2 */
577110ad9a77SSam Leffler 	nextslot = (rstamp - txtime) + (sc->sc_tdmabintval << 9);
577210ad9a77SSam Leffler 	nextslottu = TSF_TO_TU(nextslot>>32, nextslot) & HAL_BEACON_PERIOD;
577310ad9a77SSam Leffler 
577410ad9a77SSam Leffler 	/*
5775fc4de9b7SAdrian Chadd 	 * Retrieve the hardware NextTBTT in usecs
5776fc4de9b7SAdrian Chadd 	 * and calculate the difference between what the
577710ad9a77SSam Leffler 	 * other station thinks and what we have programmed.  This
577810ad9a77SSam Leffler 	 * lets us figure how to adjust our timers to match.  The
577910ad9a77SSam Leffler 	 * adjustments are done by pulling the TSF forward and possibly
578010ad9a77SSam Leffler 	 * rewriting the beacon timers.
578110ad9a77SSam Leffler 	 */
5782fc4de9b7SAdrian Chadd 	nexttbtt = ath_hal_getnexttbtt(ah);
5783fc4de9b7SAdrian Chadd 	tsfdelta = (int32_t)((nextslot % TU_TO_TSF(HAL_BEACON_PERIOD + 1)) - nexttbtt);
578410ad9a77SSam Leffler 
578510ad9a77SSam Leffler 	DPRINTF(sc, ATH_DEBUG_TDMA_TIMER,
578610ad9a77SSam Leffler 	    "tsfdelta %d avg +%d/-%d\n", tsfdelta,
578710ad9a77SSam Leffler 	    TDMA_AVG(sc->sc_avgtsfdeltap), TDMA_AVG(sc->sc_avgtsfdeltam));
578810ad9a77SSam Leffler 
578910ad9a77SSam Leffler 	if (tsfdelta < 0) {
579010ad9a77SSam Leffler 		TDMA_SAMPLE(sc->sc_avgtsfdeltap, 0);
579110ad9a77SSam Leffler 		TDMA_SAMPLE(sc->sc_avgtsfdeltam, -tsfdelta);
579210ad9a77SSam Leffler 		tsfdelta = -tsfdelta % 1024;
579310ad9a77SSam Leffler 		nextslottu++;
579410ad9a77SSam Leffler 	} else if (tsfdelta > 0) {
579510ad9a77SSam Leffler 		TDMA_SAMPLE(sc->sc_avgtsfdeltap, tsfdelta);
579610ad9a77SSam Leffler 		TDMA_SAMPLE(sc->sc_avgtsfdeltam, 0);
579710ad9a77SSam Leffler 		tsfdelta = 1024 - (tsfdelta % 1024);
579810ad9a77SSam Leffler 		nextslottu++;
579910ad9a77SSam Leffler 	} else {
580010ad9a77SSam Leffler 		TDMA_SAMPLE(sc->sc_avgtsfdeltap, 0);
580110ad9a77SSam Leffler 		TDMA_SAMPLE(sc->sc_avgtsfdeltam, 0);
580210ad9a77SSam Leffler 	}
5803fc4de9b7SAdrian Chadd 	tudelta = nextslottu - TSF_TO_TU(nexttbtt >> 32, nexttbtt);
580410ad9a77SSam Leffler 
580510ad9a77SSam Leffler 	/*
580610ad9a77SSam Leffler 	 * Copy sender's timetstamp into tdma ie so they can
580710ad9a77SSam Leffler 	 * calculate roundtrip time.  We submit a beacon frame
580810ad9a77SSam Leffler 	 * below after any timer adjustment.  The frame goes out
580910ad9a77SSam Leffler 	 * at the next TBTT so the sender can calculate the
581010ad9a77SSam Leffler 	 * roundtrip by inspecting the tdma ie in our beacon frame.
581110ad9a77SSam Leffler 	 *
581210ad9a77SSam Leffler 	 * NB: This tstamp is subtlely preserved when
581310ad9a77SSam Leffler 	 *     IEEE80211_BEACON_TDMA is marked (e.g. when the
581410ad9a77SSam Leffler 	 *     slot position changes) because ieee80211_add_tdma
581510ad9a77SSam Leffler 	 *     skips over the data.
581610ad9a77SSam Leffler 	 */
581710ad9a77SSam Leffler 	memcpy(ATH_VAP(vap)->av_boff.bo_tdma +
581810ad9a77SSam Leffler 		__offsetof(struct ieee80211_tdma_param, tdma_tstamp),
581910ad9a77SSam Leffler 		&ni->ni_tstamp.data, 8);
582010ad9a77SSam Leffler #if 0
582110ad9a77SSam Leffler 	DPRINTF(sc, ATH_DEBUG_TDMA_TIMER,
5822fc4de9b7SAdrian Chadd 	    "tsf %llu nextslot %llu (%d, %d) nextslottu %u nexttbtt %llu (%d)\n",
582310ad9a77SSam Leffler 	    (unsigned long long) tsf, (unsigned long long) nextslot,
5824fc4de9b7SAdrian Chadd 	    (int)(nextslot - tsf), tsfdelta, nextslottu, nexttbtt, tudelta);
582510ad9a77SSam Leffler #endif
582610ad9a77SSam Leffler 	/*
582710ad9a77SSam Leffler 	 * Adjust the beacon timers only when pulling them forward
582810ad9a77SSam Leffler 	 * or when going back by less than the beacon interval.
582910ad9a77SSam Leffler 	 * Negative jumps larger than the beacon interval seem to
583010ad9a77SSam Leffler 	 * cause the timers to stop and generally cause instability.
583110ad9a77SSam Leffler 	 * This basically filters out jumps due to missed beacons.
583210ad9a77SSam Leffler 	 */
583310ad9a77SSam Leffler 	if (tudelta != 0 && (tudelta > 0 || -tudelta < sc->sc_tdmabintval)) {
583410ad9a77SSam Leffler 		ath_tdma_settimers(sc, nextslottu, sc->sc_tdmabintval);
583510ad9a77SSam Leffler 		sc->sc_stats.ast_tdma_timers++;
583610ad9a77SSam Leffler 	}
583710ad9a77SSam Leffler 	if (tsfdelta > 0) {
583810ad9a77SSam Leffler 		ath_hal_adjusttsf(ah, tsfdelta);
583910ad9a77SSam Leffler 		sc->sc_stats.ast_tdma_tsf++;
584010ad9a77SSam Leffler 	}
584110ad9a77SSam Leffler 	ath_tdma_beacon_send(sc, vap);		/* prepare response */
584210ad9a77SSam Leffler #undef TU_TO_TSF
584310ad9a77SSam Leffler #undef TSF_TO_TU
584410ad9a77SSam Leffler }
584510ad9a77SSam Leffler 
584610ad9a77SSam Leffler /*
584710ad9a77SSam Leffler  * Transmit a beacon frame at SWBA.  Dynamic updates
584810ad9a77SSam Leffler  * to the frame contents are done as needed.
584910ad9a77SSam Leffler  */
585010ad9a77SSam Leffler static void
585110ad9a77SSam Leffler ath_tdma_beacon_send(struct ath_softc *sc, struct ieee80211vap *vap)
585210ad9a77SSam Leffler {
585310ad9a77SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
585410ad9a77SSam Leffler 	struct ath_buf *bf;
585510ad9a77SSam Leffler 	int otherant;
585610ad9a77SSam Leffler 
585710ad9a77SSam Leffler 	/*
585810ad9a77SSam Leffler 	 * Check if the previous beacon has gone out.  If
585910ad9a77SSam Leffler 	 * not don't try to post another, skip this period
586010ad9a77SSam Leffler 	 * and wait for the next.  Missed beacons indicate
586110ad9a77SSam Leffler 	 * a problem and should not occur.  If we miss too
586210ad9a77SSam Leffler 	 * many consecutive beacons reset the device.
586310ad9a77SSam Leffler 	 */
586410ad9a77SSam Leffler 	if (ath_hal_numtxpending(ah, sc->sc_bhalq) != 0) {
586510ad9a77SSam Leffler 		sc->sc_bmisscount++;
586610ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
586710ad9a77SSam Leffler 			"%s: missed %u consecutive beacons\n",
586810ad9a77SSam Leffler 			__func__, sc->sc_bmisscount);
5869a32ac9d3SSam Leffler 		if (sc->sc_bmisscount >= ath_bstuck_threshold)
587010ad9a77SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_bstucktask);
587110ad9a77SSam Leffler 		return;
587210ad9a77SSam Leffler 	}
587310ad9a77SSam Leffler 	if (sc->sc_bmisscount != 0) {
587410ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
587510ad9a77SSam Leffler 			"%s: resume beacon xmit after %u misses\n",
587610ad9a77SSam Leffler 			__func__, sc->sc_bmisscount);
587710ad9a77SSam Leffler 		sc->sc_bmisscount = 0;
587810ad9a77SSam Leffler 	}
587910ad9a77SSam Leffler 
588010ad9a77SSam Leffler 	/*
588110ad9a77SSam Leffler 	 * Check recent per-antenna transmit statistics and flip
588210ad9a77SSam Leffler 	 * the default antenna if noticeably more frames went out
588310ad9a77SSam Leffler 	 * on the non-default antenna.
588410ad9a77SSam Leffler 	 * XXX assumes 2 anntenae
588510ad9a77SSam Leffler 	 */
588610ad9a77SSam Leffler 	if (!sc->sc_diversity) {
588710ad9a77SSam Leffler 		otherant = sc->sc_defant & 1 ? 2 : 1;
588810ad9a77SSam Leffler 		if (sc->sc_ant_tx[otherant] > sc->sc_ant_tx[sc->sc_defant] + 2)
588910ad9a77SSam Leffler 			ath_setdefantenna(sc, otherant);
589010ad9a77SSam Leffler 		sc->sc_ant_tx[1] = sc->sc_ant_tx[2] = 0;
589110ad9a77SSam Leffler 	}
589210ad9a77SSam Leffler 
589310ad9a77SSam Leffler 	bf = ath_beacon_generate(sc, vap);
589410ad9a77SSam Leffler 	if (bf != NULL) {
589510ad9a77SSam Leffler 		/*
589610ad9a77SSam Leffler 		 * Stop any current dma and put the new frame on the queue.
589710ad9a77SSam Leffler 		 * This should never fail since we check above that no frames
589810ad9a77SSam Leffler 		 * are still pending on the queue.
589910ad9a77SSam Leffler 		 */
590010ad9a77SSam Leffler 		if (!ath_hal_stoptxdma(ah, sc->sc_bhalq)) {
590110ad9a77SSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
590210ad9a77SSam Leffler 				"%s: beacon queue %u did not stop?\n",
590310ad9a77SSam Leffler 				__func__, sc->sc_bhalq);
590410ad9a77SSam Leffler 			/* NB: the HAL still stops DMA, so proceed */
590510ad9a77SSam Leffler 		}
590610ad9a77SSam Leffler 		ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr);
590710ad9a77SSam Leffler 		ath_hal_txstart(ah, sc->sc_bhalq);
590810ad9a77SSam Leffler 
590910ad9a77SSam Leffler 		sc->sc_stats.ast_be_xmit++;		/* XXX per-vap? */
591010ad9a77SSam Leffler 
591110ad9a77SSam Leffler 		/*
591210ad9a77SSam Leffler 		 * Record local TSF for our last send for use
591310ad9a77SSam Leffler 		 * in arbitrating slot collisions.
591410ad9a77SSam Leffler 		 */
591510ad9a77SSam Leffler 		vap->iv_bss->ni_tstamp.tsf = ath_hal_gettsf64(ah);
591610ad9a77SSam Leffler 	}
591710ad9a77SSam Leffler }
5918584f7327SSam Leffler #endif /* IEEE80211_SUPPORT_TDMA */
5919e8dabfbeSAdrian Chadd 
592048237774SAdrian Chadd static void
592148237774SAdrian Chadd ath_dfs_tasklet(void *p, int npending)
592248237774SAdrian Chadd {
592348237774SAdrian Chadd 	struct ath_softc *sc = (struct ath_softc *) p;
592448237774SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
592548237774SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
592648237774SAdrian Chadd 
592748237774SAdrian Chadd 	/*
592848237774SAdrian Chadd 	 * If previous processing has found a radar event,
592948237774SAdrian Chadd 	 * signal this to the net80211 layer to begin DFS
593048237774SAdrian Chadd 	 * processing.
593148237774SAdrian Chadd 	 */
593248237774SAdrian Chadd 	if (ath_dfs_process_radar_event(sc, sc->sc_curchan)) {
593348237774SAdrian Chadd 		/* DFS event found, initiate channel change */
593448237774SAdrian Chadd 		ieee80211_dfs_notify_radar(ic, sc->sc_curchan);
593548237774SAdrian Chadd 	}
593648237774SAdrian Chadd }
593748237774SAdrian Chadd 
5938dba9c859SAdrian Chadd MODULE_VERSION(if_ath, 1);
5939dba9c859SAdrian Chadd MODULE_DEPEND(if_ath, wlan, 1, 1, 1);          /* 802.11 media layer */
5940