xref: /freebsd/sys/dev/ath/if_ath.c (revision 6b349e5a86b1d7fc255e195f6e544caa2f791ad2)
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);
1735591b213SSam Leffler static void	ath_rx_proc(void *, int);
174622b3fd2SSam Leffler static void	ath_txq_init(struct ath_softc *sc, struct ath_txq *, int);
175c42a7b7eSSam Leffler static struct ath_txq *ath_txq_setup(struct ath_softc*, int qtype, int subtype);
176c42a7b7eSSam Leffler static int	ath_tx_setup(struct ath_softc *, int, int);
177c42a7b7eSSam Leffler static int	ath_wme_update(struct ieee80211com *);
178c42a7b7eSSam Leffler static void	ath_tx_cleanupq(struct ath_softc *, struct ath_txq *);
179c42a7b7eSSam Leffler static void	ath_tx_cleanup(struct ath_softc *);
180c42a7b7eSSam Leffler static void	ath_tx_proc_q0(void *, int);
181c42a7b7eSSam Leffler static void	ath_tx_proc_q0123(void *, int);
1825591b213SSam Leffler static void	ath_tx_proc(void *, int);
183b032f27cSSam Leffler static void	ath_tx_draintxq(struct ath_softc *, struct ath_txq *);
1845591b213SSam Leffler static int	ath_chan_set(struct ath_softc *, struct ieee80211_channel *);
1855591b213SSam Leffler static void	ath_draintxq(struct ath_softc *);
1865591b213SSam Leffler static void	ath_stoprecv(struct ath_softc *);
1875591b213SSam Leffler static int	ath_startrecv(struct ath_softc *);
188c42a7b7eSSam Leffler static void	ath_chan_change(struct ath_softc *, struct ieee80211_channel *);
18968e8e04eSSam Leffler static void	ath_scan_start(struct ieee80211com *);
19068e8e04eSSam Leffler static void	ath_scan_end(struct ieee80211com *);
19168e8e04eSSam Leffler static void	ath_set_channel(struct ieee80211com *);
1925591b213SSam Leffler static void	ath_calibrate(void *);
193b032f27cSSam Leffler static int	ath_newstate(struct ieee80211vap *, enum ieee80211_state, int);
194e8fd88a3SSam Leffler static void	ath_setup_stationkey(struct ieee80211_node *);
195e9962332SSam Leffler static void	ath_newassoc(struct ieee80211_node *, int);
196b032f27cSSam Leffler static int	ath_setregdomain(struct ieee80211com *,
197b032f27cSSam Leffler 		    struct ieee80211_regdomain *, int,
198b032f27cSSam Leffler 		    struct ieee80211_channel []);
1995fe9f044SSam Leffler static void	ath_getradiocaps(struct ieee80211com *, int, int *,
200b032f27cSSam Leffler 		    struct ieee80211_channel []);
201b032f27cSSam Leffler static int	ath_getchannels(struct ath_softc *);
2023e50ec2cSSam Leffler static void	ath_led_event(struct ath_softc *, int);
2035591b213SSam Leffler 
204c42a7b7eSSam Leffler static int	ath_rate_setup(struct ath_softc *, u_int mode);
2055591b213SSam Leffler static void	ath_setcurmode(struct ath_softc *, enum ieee80211_phymode);
206c42a7b7eSSam Leffler 
207c42a7b7eSSam Leffler static void	ath_announce(struct ath_softc *);
2085591b213SSam Leffler 
20948237774SAdrian Chadd static void	ath_dfs_tasklet(void *, int);
21048237774SAdrian Chadd 
211584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
21210ad9a77SSam Leffler static void	ath_tdma_settimers(struct ath_softc *sc, u_int32_t nexttbtt,
21310ad9a77SSam Leffler 		    u_int32_t bintval);
21410ad9a77SSam Leffler static void	ath_tdma_bintvalsetup(struct ath_softc *sc,
21510ad9a77SSam Leffler 		    const struct ieee80211_tdma_state *tdma);
21610ad9a77SSam Leffler static void	ath_tdma_config(struct ath_softc *sc, struct ieee80211vap *vap);
21710ad9a77SSam Leffler static void	ath_tdma_update(struct ieee80211_node *ni,
2182bc3ce77SSam Leffler 		    const struct ieee80211_tdma_param *tdma, int);
21910ad9a77SSam Leffler static void	ath_tdma_beacon_send(struct ath_softc *sc,
22010ad9a77SSam Leffler 		    struct ieee80211vap *vap);
22110ad9a77SSam Leffler 
22210ad9a77SSam Leffler #define	TDMA_EP_MULTIPLIER	(1<<10) /* pow2 to optimize out * and / */
22310ad9a77SSam Leffler #define	TDMA_LPF_LEN		6
22410ad9a77SSam Leffler #define	TDMA_DUMMY_MARKER	0x127
22510ad9a77SSam Leffler #define	TDMA_EP_MUL(x, mul)	((x) * (mul))
22610ad9a77SSam Leffler #define	TDMA_IN(x)		(TDMA_EP_MUL((x), TDMA_EP_MULTIPLIER))
22710ad9a77SSam Leffler #define	TDMA_LPF(x, y, len) \
22810ad9a77SSam Leffler     ((x != TDMA_DUMMY_MARKER) ? (((x) * ((len)-1) + (y)) / (len)) : (y))
22910ad9a77SSam Leffler #define	TDMA_SAMPLE(x, y) do {					\
23010ad9a77SSam Leffler 	x = TDMA_LPF((x), TDMA_IN(y), TDMA_LPF_LEN);		\
23110ad9a77SSam Leffler } while (0)
23210ad9a77SSam Leffler #define	TDMA_EP_RND(x,mul) \
23310ad9a77SSam Leffler 	((((x)%(mul)) >= ((mul)/2)) ? ((x) + ((mul) - 1)) / (mul) : (x)/(mul))
23410ad9a77SSam Leffler #define	TDMA_AVG(x)		TDMA_EP_RND(x, TDMA_EP_MULTIPLIER)
235584f7327SSam Leffler #endif /* IEEE80211_SUPPORT_TDMA */
23610ad9a77SSam Leffler 
2375591b213SSam Leffler SYSCTL_DECL(_hw_ath);
2385591b213SSam Leffler 
2395591b213SSam Leffler /* XXX validate sysctl values */
2402dc7fcc4SSam Leffler static	int ath_longcalinterval = 30;		/* long cals every 30 secs */
2412dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, longcal, CTLFLAG_RW, &ath_longcalinterval,
2422dc7fcc4SSam Leffler 	    0, "long chip calibration interval (secs)");
2432dc7fcc4SSam Leffler static	int ath_shortcalinterval = 100;		/* short cals every 100 ms */
2442dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, shortcal, CTLFLAG_RW, &ath_shortcalinterval,
2452dc7fcc4SSam Leffler 	    0, "short chip calibration interval (msecs)");
2462dc7fcc4SSam Leffler static	int ath_resetcalinterval = 20*60;	/* reset cal state 20 mins */
2472dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, resetcal, CTLFLAG_RW, &ath_resetcalinterval,
2482dc7fcc4SSam Leffler 	    0, "reset chip calibration results (secs)");
249a108ab63SAdrian Chadd static	int ath_anicalinterval = 100;		/* ANI calibration - 100 msec */
250a108ab63SAdrian Chadd SYSCTL_INT(_hw_ath, OID_AUTO, anical, CTLFLAG_RW, &ath_anicalinterval,
251a108ab63SAdrian Chadd 	    0, "ANI calibration (msecs)");
2525591b213SSam Leffler 
253e2d787faSSam Leffler static	int ath_rxbuf = ATH_RXBUF;		/* # rx buffers to allocate */
254aaa70f2fSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, rxbuf, CTLFLAG_RW, &ath_rxbuf,
255e2d787faSSam Leffler 	    0, "rx buffers allocated");
256e2d787faSSam Leffler TUNABLE_INT("hw.ath.rxbuf", &ath_rxbuf);
257e2d787faSSam Leffler static	int ath_txbuf = ATH_TXBUF;		/* # tx buffers to allocate */
258aaa70f2fSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, txbuf, CTLFLAG_RW, &ath_txbuf,
259e2d787faSSam Leffler 	    0, "tx buffers allocated");
260e2d787faSSam Leffler TUNABLE_INT("hw.ath.txbuf", &ath_txbuf);
261e2d787faSSam Leffler 
262a32ac9d3SSam Leffler static	int ath_bstuck_threshold = 4;		/* max missed beacons */
263a32ac9d3SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, bstuck, CTLFLAG_RW, &ath_bstuck_threshold,
264a32ac9d3SSam Leffler 	    0, "max missed beacon xmits before chip reset");
265a32ac9d3SSam Leffler 
266*6b349e5aSAdrian Chadd MALLOC_DEFINE(M_ATHDEV, "athdev", "ath driver dma buffers");
267c42a7b7eSSam Leffler 
26867397d39SAdrian Chadd #define	HAL_MODE_HT20 (HAL_MODE_11NG_HT20 | HAL_MODE_11NA_HT20)
26967397d39SAdrian Chadd #define	HAL_MODE_HT40 \
27067397d39SAdrian Chadd 	(HAL_MODE_11NG_HT40PLUS | HAL_MODE_11NG_HT40MINUS | \
27167397d39SAdrian Chadd 	HAL_MODE_11NA_HT40PLUS | HAL_MODE_11NA_HT40MINUS)
2725591b213SSam Leffler int
2735591b213SSam Leffler ath_attach(u_int16_t devid, struct ath_softc *sc)
2745591b213SSam Leffler {
275fc74a9f9SBrooks Davis 	struct ifnet *ifp;
276b032f27cSSam Leffler 	struct ieee80211com *ic;
277fc74a9f9SBrooks Davis 	struct ath_hal *ah = NULL;
2785591b213SSam Leffler 	HAL_STATUS status;
279c42a7b7eSSam Leffler 	int error = 0, i;
280411373ebSSam Leffler 	u_int wmodes;
28129aca940SSam Leffler 	uint8_t macaddr[IEEE80211_ADDR_LEN];
2825591b213SSam Leffler 
283c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: devid 0x%x\n", __func__, devid);
2845591b213SSam Leffler 
285b032f27cSSam Leffler 	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
286fc74a9f9SBrooks Davis 	if (ifp == NULL) {
287fc74a9f9SBrooks Davis 		device_printf(sc->sc_dev, "can not if_alloc()\n");
288fc74a9f9SBrooks Davis 		error = ENOSPC;
289fc74a9f9SBrooks Davis 		goto bad;
290fc74a9f9SBrooks Davis 	}
291b032f27cSSam Leffler 	ic = ifp->if_l2com;
292fc74a9f9SBrooks Davis 
2935591b213SSam Leffler 	/* set these up early for if_printf use */
2949bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(sc->sc_dev),
2959bf40edeSBrooks Davis 		device_get_unit(sc->sc_dev));
2965591b213SSam Leffler 
29788117a53SAdrian Chadd 	ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh, sc->sc_eepromdata, &status);
2985591b213SSam Leffler 	if (ah == NULL) {
2995591b213SSam Leffler 		if_printf(ifp, "unable to attach hardware; HAL status %u\n",
3005591b213SSam Leffler 			status);
3015591b213SSam Leffler 		error = ENXIO;
3025591b213SSam Leffler 		goto bad;
3035591b213SSam Leffler 	}
3045591b213SSam Leffler 	sc->sc_ah = ah;
305b58b3803SSam Leffler 	sc->sc_invalid = 0;	/* ready to go, enable interrupt handling */
3063297be13SSam Leffler #ifdef	ATH_DEBUG
3073297be13SSam Leffler 	sc->sc_debug = ath_debug;
3083297be13SSam Leffler #endif
3095591b213SSam Leffler 
3105591b213SSam Leffler 	/*
311c42a7b7eSSam Leffler 	 * Check if the MAC has multi-rate retry support.
312c42a7b7eSSam Leffler 	 * We do this by trying to setup a fake extended
313c42a7b7eSSam Leffler 	 * descriptor.  MAC's that don't have support will
314c42a7b7eSSam Leffler 	 * return false w/o doing anything.  MAC's that do
315c42a7b7eSSam Leffler 	 * support it will return true w/o doing anything.
316c42a7b7eSSam Leffler 	 */
317c42a7b7eSSam Leffler 	sc->sc_mrretry = ath_hal_setupxtxdesc(ah, NULL, 0,0, 0,0, 0,0);
318c42a7b7eSSam Leffler 
319c42a7b7eSSam Leffler 	/*
320c42a7b7eSSam Leffler 	 * Check if the device has hardware counters for PHY
321c42a7b7eSSam Leffler 	 * errors.  If so we need to enable the MIB interrupt
322c42a7b7eSSam Leffler 	 * so we can act on stat triggers.
323c42a7b7eSSam Leffler 	 */
324c42a7b7eSSam Leffler 	if (ath_hal_hwphycounters(ah))
325c42a7b7eSSam Leffler 		sc->sc_needmib = 1;
326c42a7b7eSSam Leffler 
327c42a7b7eSSam Leffler 	/*
328c42a7b7eSSam Leffler 	 * Get the hardware key cache size.
329c42a7b7eSSam Leffler 	 */
330c42a7b7eSSam Leffler 	sc->sc_keymax = ath_hal_keycachesize(ah);
331e8fd88a3SSam Leffler 	if (sc->sc_keymax > ATH_KEYMAX) {
332e8fd88a3SSam Leffler 		if_printf(ifp, "Warning, using only %u of %u key cache slots\n",
333e8fd88a3SSam Leffler 			ATH_KEYMAX, sc->sc_keymax);
334e8fd88a3SSam Leffler 		sc->sc_keymax = ATH_KEYMAX;
335c42a7b7eSSam Leffler 	}
336c42a7b7eSSam Leffler 	/*
337c42a7b7eSSam Leffler 	 * Reset the key cache since some parts do not
338c42a7b7eSSam Leffler 	 * reset the contents on initial power up.
339c42a7b7eSSam Leffler 	 */
340c42a7b7eSSam Leffler 	for (i = 0; i < sc->sc_keymax; i++)
341c42a7b7eSSam Leffler 		ath_hal_keyreset(ah, i);
342c42a7b7eSSam Leffler 
343c42a7b7eSSam Leffler 	/*
344b032f27cSSam Leffler 	 * Collect the default channel list.
3455591b213SSam Leffler 	 */
346b032f27cSSam Leffler 	error = ath_getchannels(sc);
3475591b213SSam Leffler 	if (error != 0)
3485591b213SSam Leffler 		goto bad;
3495591b213SSam Leffler 
3505591b213SSam Leffler 	/*
3515591b213SSam Leffler 	 * Setup rate tables for all potential media types.
3525591b213SSam Leffler 	 */
3535591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11A);
3545591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11B);
3555591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11G);
356c42a7b7eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_A);
357c42a7b7eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_G);
35868e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_STURBO_A);
35968e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11NA);
36068e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11NG);
361724c193aSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_HALF);
362724c193aSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_QUARTER);
363aaa70f2fSSam Leffler 
364c42a7b7eSSam Leffler 	/* NB: setup here so ath_rate_update is happy */
365c42a7b7eSSam Leffler 	ath_setcurmode(sc, IEEE80211_MODE_11A);
3665591b213SSam Leffler 
367c42a7b7eSSam Leffler 	/*
368c42a7b7eSSam Leffler 	 * Allocate tx+rx descriptors and populate the lists.
369c42a7b7eSSam Leffler 	 */
3705591b213SSam Leffler 	error = ath_desc_alloc(sc);
3715591b213SSam Leffler 	if (error != 0) {
3725591b213SSam Leffler 		if_printf(ifp, "failed to allocate descriptors: %d\n", error);
3735591b213SSam Leffler 		goto bad;
3745591b213SSam Leffler 	}
3752e986da5SSam Leffler 	callout_init_mtx(&sc->sc_cal_ch, &sc->sc_mtx, 0);
3762e986da5SSam Leffler 	callout_init_mtx(&sc->sc_wd_ch, &sc->sc_mtx, 0);
3775591b213SSam Leffler 
378f0b2a0beSSam Leffler 	ATH_TXBUF_LOCK_INIT(sc);
3795591b213SSam Leffler 
3800bbf5441SSam Leffler 	sc->sc_tq = taskqueue_create("ath_taskq", M_NOWAIT,
3810bbf5441SSam Leffler 		taskqueue_thread_enqueue, &sc->sc_tq);
3820bbf5441SSam Leffler 	taskqueue_start_threads(&sc->sc_tq, 1, PI_NET,
3830bbf5441SSam Leffler 		"%s taskq", ifp->if_xname);
3840bbf5441SSam Leffler 
3855591b213SSam Leffler 	TASK_INIT(&sc->sc_rxtask, 0, ath_rx_proc, sc);
3865591b213SSam Leffler 	TASK_INIT(&sc->sc_bmisstask, 0, ath_bmiss_proc, sc);
387c42a7b7eSSam Leffler 	TASK_INIT(&sc->sc_bstucktask,0, ath_bstuck_proc, sc);
3885591b213SSam Leffler 
3895591b213SSam Leffler 	/*
390c42a7b7eSSam Leffler 	 * Allocate hardware transmit queues: one queue for
391c42a7b7eSSam Leffler 	 * beacon frames and one data queue for each QoS
3924fa8d4efSDaniel Eischen 	 * priority.  Note that the hal handles resetting
393c42a7b7eSSam Leffler 	 * these queues at the needed time.
394c42a7b7eSSam Leffler 	 *
395c42a7b7eSSam Leffler 	 * XXX PS-Poll
3965591b213SSam Leffler 	 */
39780d2765fSSam Leffler 	sc->sc_bhalq = ath_beaconq_setup(ah);
3985591b213SSam Leffler 	if (sc->sc_bhalq == (u_int) -1) {
3995591b213SSam Leffler 		if_printf(ifp, "unable to setup a beacon xmit queue!\n");
400c42a7b7eSSam Leffler 		error = EIO;
401b28b4653SSam Leffler 		goto bad2;
4025591b213SSam Leffler 	}
403c42a7b7eSSam Leffler 	sc->sc_cabq = ath_txq_setup(sc, HAL_TX_QUEUE_CAB, 0);
404c42a7b7eSSam Leffler 	if (sc->sc_cabq == NULL) {
405c42a7b7eSSam Leffler 		if_printf(ifp, "unable to setup CAB xmit queue!\n");
406c42a7b7eSSam Leffler 		error = EIO;
407c42a7b7eSSam Leffler 		goto bad2;
408c42a7b7eSSam Leffler 	}
409c42a7b7eSSam Leffler 	/* NB: insure BK queue is the lowest priority h/w queue */
410c42a7b7eSSam Leffler 	if (!ath_tx_setup(sc, WME_AC_BK, HAL_WME_AC_BK)) {
411c42a7b7eSSam Leffler 		if_printf(ifp, "unable to setup xmit queue for %s traffic!\n",
412c42a7b7eSSam Leffler 			ieee80211_wme_acnames[WME_AC_BK]);
413c42a7b7eSSam Leffler 		error = EIO;
414c42a7b7eSSam Leffler 		goto bad2;
415c42a7b7eSSam Leffler 	}
416c42a7b7eSSam Leffler 	if (!ath_tx_setup(sc, WME_AC_BE, HAL_WME_AC_BE) ||
417c42a7b7eSSam Leffler 	    !ath_tx_setup(sc, WME_AC_VI, HAL_WME_AC_VI) ||
418c42a7b7eSSam Leffler 	    !ath_tx_setup(sc, WME_AC_VO, HAL_WME_AC_VO)) {
419c42a7b7eSSam Leffler 		/*
420c42a7b7eSSam Leffler 		 * Not enough hardware tx queues to properly do WME;
421c42a7b7eSSam Leffler 		 * just punt and assign them all to the same h/w queue.
422c42a7b7eSSam Leffler 		 * We could do a better job of this if, for example,
423c42a7b7eSSam Leffler 		 * we allocate queues when we switch from station to
424c42a7b7eSSam Leffler 		 * AP mode.
425c42a7b7eSSam Leffler 		 */
426c42a7b7eSSam Leffler 		if (sc->sc_ac2q[WME_AC_VI] != NULL)
427c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_VI]);
428c42a7b7eSSam Leffler 		if (sc->sc_ac2q[WME_AC_BE] != NULL)
429c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_BE]);
430c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_BE] = sc->sc_ac2q[WME_AC_BK];
431c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_VI] = sc->sc_ac2q[WME_AC_BK];
432c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_VO] = sc->sc_ac2q[WME_AC_BK];
433c42a7b7eSSam Leffler 	}
434c42a7b7eSSam Leffler 
435c42a7b7eSSam Leffler 	/*
436c42a7b7eSSam Leffler 	 * Special case certain configurations.  Note the
437c42a7b7eSSam Leffler 	 * CAB queue is handled by these specially so don't
438c42a7b7eSSam Leffler 	 * include them when checking the txq setup mask.
439c42a7b7eSSam Leffler 	 */
440c42a7b7eSSam Leffler 	switch (sc->sc_txqsetup &~ (1<<sc->sc_cabq->axq_qnum)) {
441c42a7b7eSSam Leffler 	case 0x01:
442c42a7b7eSSam Leffler 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0, sc);
443c42a7b7eSSam Leffler 		break;
444c42a7b7eSSam Leffler 	case 0x0f:
445c42a7b7eSSam Leffler 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0123, sc);
446c42a7b7eSSam Leffler 		break;
447c42a7b7eSSam Leffler 	default:
448c42a7b7eSSam Leffler 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc, sc);
449c42a7b7eSSam Leffler 		break;
450c42a7b7eSSam Leffler 	}
451c42a7b7eSSam Leffler 
452c42a7b7eSSam Leffler 	/*
453c42a7b7eSSam Leffler 	 * Setup rate control.  Some rate control modules
454c42a7b7eSSam Leffler 	 * call back to change the anntena state so expose
455c42a7b7eSSam Leffler 	 * the necessary entry points.
456c42a7b7eSSam Leffler 	 * XXX maybe belongs in struct ath_ratectrl?
457c42a7b7eSSam Leffler 	 */
458c42a7b7eSSam Leffler 	sc->sc_setdefantenna = ath_setdefantenna;
459c42a7b7eSSam Leffler 	sc->sc_rc = ath_rate_attach(sc);
460c42a7b7eSSam Leffler 	if (sc->sc_rc == NULL) {
461c42a7b7eSSam Leffler 		error = EIO;
462c42a7b7eSSam Leffler 		goto bad2;
463c42a7b7eSSam Leffler 	}
464c42a7b7eSSam Leffler 
46548237774SAdrian Chadd 	/* Attach DFS module */
46648237774SAdrian Chadd 	if (! ath_dfs_attach(sc)) {
46748237774SAdrian Chadd 		device_printf(sc->sc_dev, "%s: unable to attach DFS\n", __func__);
46848237774SAdrian Chadd 		error = EIO;
46948237774SAdrian Chadd 		goto bad2;
47048237774SAdrian Chadd 	}
47148237774SAdrian Chadd 
47248237774SAdrian Chadd 	/* Start DFS processing tasklet */
47348237774SAdrian Chadd 	TASK_INIT(&sc->sc_dfstask, 0, ath_dfs_tasklet, sc);
47448237774SAdrian Chadd 
4753e50ec2cSSam Leffler 	sc->sc_blinking = 0;
476c42a7b7eSSam Leffler 	sc->sc_ledstate = 1;
4773e50ec2cSSam Leffler 	sc->sc_ledon = 0;			/* low true */
4783e50ec2cSSam Leffler 	sc->sc_ledidle = (2700*hz)/1000;	/* 2.7sec */
4793e50ec2cSSam Leffler 	callout_init(&sc->sc_ledtimer, CALLOUT_MPSAFE);
480c42a7b7eSSam Leffler 	/*
481c42a7b7eSSam Leffler 	 * Auto-enable soft led processing for IBM cards and for
482c42a7b7eSSam Leffler 	 * 5211 minipci cards.  Users can also manually enable/disable
483c42a7b7eSSam Leffler 	 * support with a sysctl.
484c42a7b7eSSam Leffler 	 */
485c42a7b7eSSam Leffler 	sc->sc_softled = (devid == AR5212_DEVID_IBM || devid == AR5211_DEVID);
486c42a7b7eSSam Leffler 	if (sc->sc_softled) {
487869ff02eSSam Leffler 		ath_hal_gpioCfgOutput(ah, sc->sc_ledpin,
488869ff02eSSam Leffler 		    HAL_GPIO_MUX_MAC_NETWORK_LED);
4893e50ec2cSSam Leffler 		ath_hal_gpioset(ah, sc->sc_ledpin, !sc->sc_ledon);
490c42a7b7eSSam Leffler 	}
4915591b213SSam Leffler 
4925591b213SSam Leffler 	ifp->if_softc = sc;
4935591b213SSam Leffler 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
4945591b213SSam Leffler 	ifp->if_start = ath_start;
4955591b213SSam Leffler 	ifp->if_ioctl = ath_ioctl;
4965591b213SSam Leffler 	ifp->if_init = ath_init;
497e50d35e6SMaxim Sobolev 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
498e50d35e6SMaxim Sobolev 	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
499154b8df2SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
5005591b213SSam Leffler 
501c42a7b7eSSam Leffler 	ic->ic_ifp = ifp;
5025591b213SSam Leffler 	/* XXX not right but it's not used anywhere important */
5035591b213SSam Leffler 	ic->ic_phytype = IEEE80211_T_OFDM;
5045591b213SSam Leffler 	ic->ic_opmode = IEEE80211_M_STA;
505c42a7b7eSSam Leffler 	ic->ic_caps =
506c43feedeSSam Leffler 		  IEEE80211_C_STA		/* station mode */
507c43feedeSSam Leffler 		| IEEE80211_C_IBSS		/* ibss, nee adhoc, mode */
508fe32c3efSSam Leffler 		| IEEE80211_C_HOSTAP		/* hostap mode */
509fe32c3efSSam Leffler 		| IEEE80211_C_MONITOR		/* monitor mode */
5107a04dc27SSam Leffler 		| IEEE80211_C_AHDEMO		/* adhoc demo mode */
511b032f27cSSam Leffler 		| IEEE80211_C_WDS		/* 4-address traffic works */
51259aa14a9SRui Paulo 		| IEEE80211_C_MBSS		/* mesh point link mode */
513fe32c3efSSam Leffler 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
514c42a7b7eSSam Leffler 		| IEEE80211_C_SHSLOT		/* short slot time supported */
515c42a7b7eSSam Leffler 		| IEEE80211_C_WPA		/* capable of WPA1+WPA2 */
51668e8e04eSSam Leffler 		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
51768e8e04eSSam Leffler 		| IEEE80211_C_TXFRAG		/* handle tx frags */
51810dc8de4SAdrian Chadd #ifdef	ATH_ENABLE_DFS
51910dc8de4SAdrian Chadd 		| IEEE80211_C_DFS		/* Enable DFS radar detection */
52010dc8de4SAdrian Chadd #endif
52101e7e035SSam Leffler 		;
522c42a7b7eSSam Leffler 	/*
523c42a7b7eSSam Leffler 	 * Query the hal to figure out h/w crypto support.
524c42a7b7eSSam Leffler 	 */
525c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_WEP))
526b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP;
527c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_OCB))
528b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_OCB;
529c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_CCM))
530b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_CCM;
531c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_CKIP))
532b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_CKIP;
533c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_TKIP)) {
534b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIP;
535c42a7b7eSSam Leffler 		/*
536c42a7b7eSSam Leffler 		 * Check if h/w does the MIC and/or whether the
537c42a7b7eSSam Leffler 		 * separate key cache entries are required to
538c42a7b7eSSam Leffler 		 * handle both tx+rx MIC keys.
539c42a7b7eSSam Leffler 		 */
540c42a7b7eSSam Leffler 		if (ath_hal_ciphersupported(ah, HAL_CIPHER_MIC))
541b032f27cSSam Leffler 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
5425901d2d3SSam Leffler 		/*
5435901d2d3SSam Leffler 		 * If the h/w supports storing tx+rx MIC keys
5445901d2d3SSam Leffler 		 * in one cache slot automatically enable use.
5455901d2d3SSam Leffler 		 */
5465901d2d3SSam Leffler 		if (ath_hal_hastkipsplit(ah) ||
5475901d2d3SSam Leffler 		    !ath_hal_settkipsplit(ah, AH_FALSE))
548c42a7b7eSSam Leffler 			sc->sc_splitmic = 1;
549b032f27cSSam Leffler 		/*
550b032f27cSSam Leffler 		 * If the h/w can do TKIP MIC together with WME then
551b032f27cSSam Leffler 		 * we use it; otherwise we force the MIC to be done
552b032f27cSSam Leffler 		 * in software by the net80211 layer.
553b032f27cSSam Leffler 		 */
554b032f27cSSam Leffler 		if (ath_hal_haswmetkipmic(ah))
555b032f27cSSam Leffler 			sc->sc_wmetkipmic = 1;
556c42a7b7eSSam Leffler 	}
557e8fd88a3SSam Leffler 	sc->sc_hasclrkey = ath_hal_ciphersupported(ah, HAL_CIPHER_CLR);
5589ac01d39SRui Paulo 	/*
5591ac5dac2SRui Paulo 	 * Check for multicast key search support.
5609ac01d39SRui Paulo 	 */
5619ac01d39SRui Paulo 	if (ath_hal_hasmcastkeysearch(sc->sc_ah) &&
5629ac01d39SRui Paulo 	    !ath_hal_getmcastkeysearch(sc->sc_ah)) {
5639ac01d39SRui Paulo 		ath_hal_setmcastkeysearch(sc->sc_ah, 1);
5649ac01d39SRui Paulo 	}
565e8fd88a3SSam Leffler 	sc->sc_mcastkey = ath_hal_getmcastkeysearch(ah);
566c42a7b7eSSam Leffler 	/*
5675901d2d3SSam Leffler 	 * Mark key cache slots associated with global keys
5685901d2d3SSam Leffler 	 * as in use.  If we knew TKIP was not to be used we
5695901d2d3SSam Leffler 	 * could leave the +32, +64, and +32+64 slots free.
5705901d2d3SSam Leffler 	 */
5715901d2d3SSam Leffler 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
5725901d2d3SSam Leffler 		setbit(sc->sc_keymap, i);
5735901d2d3SSam Leffler 		setbit(sc->sc_keymap, i+64);
5745901d2d3SSam Leffler 		if (sc->sc_splitmic) {
5755901d2d3SSam Leffler 			setbit(sc->sc_keymap, i+32);
5765901d2d3SSam Leffler 			setbit(sc->sc_keymap, i+32+64);
5775901d2d3SSam Leffler 		}
5785901d2d3SSam Leffler 	}
5795901d2d3SSam Leffler 	/*
580c42a7b7eSSam Leffler 	 * TPC support can be done either with a global cap or
581c42a7b7eSSam Leffler 	 * per-packet support.  The latter is not available on
582c42a7b7eSSam Leffler 	 * all parts.  We're a bit pedantic here as all parts
583c42a7b7eSSam Leffler 	 * support a global cap.
584c42a7b7eSSam Leffler 	 */
585c59005e9SSam Leffler 	if (ath_hal_hastpc(ah) || ath_hal_hastxpowlimit(ah))
586c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TXPMGT;
587c42a7b7eSSam Leffler 
588c42a7b7eSSam Leffler 	/*
589c42a7b7eSSam Leffler 	 * Mark WME capability only if we have sufficient
590c42a7b7eSSam Leffler 	 * hardware queues to do proper priority scheduling.
591c42a7b7eSSam Leffler 	 */
592c42a7b7eSSam Leffler 	if (sc->sc_ac2q[WME_AC_BE] != sc->sc_ac2q[WME_AC_BK])
593c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_WME;
594c42a7b7eSSam Leffler 	/*
595e8fd88a3SSam Leffler 	 * Check for misc other capabilities.
596c42a7b7eSSam Leffler 	 */
597c42a7b7eSSam Leffler 	if (ath_hal_hasbursting(ah))
598c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_BURST;
599b032f27cSSam Leffler 	sc->sc_hasbmask = ath_hal_hasbssidmask(ah);
60059aa14a9SRui Paulo 	sc->sc_hasbmatch = ath_hal_hasbssidmatch(ah);
601b032f27cSSam Leffler 	sc->sc_hastsfadd = ath_hal_hastsfadjust(ah);
6028a2a6beeSAdrian Chadd 	sc->sc_rxslink = ath_hal_self_linked_final_rxdesc(ah);
603fc4de9b7SAdrian Chadd 	sc->sc_rxtsf32 = ath_hal_has_long_rxdesc_tsf(ah);
60468e8e04eSSam Leffler 	if (ath_hal_hasfastframes(ah))
60568e8e04eSSam Leffler 		ic->ic_caps |= IEEE80211_C_FF;
60659efa8b5SSam Leffler 	wmodes = ath_hal_getwirelessmodes(ah);
607411373ebSSam Leffler 	if (wmodes & (HAL_MODE_108G|HAL_MODE_TURBO))
60868e8e04eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TURBOP;
609584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
61010ad9a77SSam Leffler 	if (ath_hal_macversion(ah) > 0x78) {
61110ad9a77SSam Leffler 		ic->ic_caps |= IEEE80211_C_TDMA; /* capable of TDMA */
61210ad9a77SSam Leffler 		ic->ic_tdma_update = ath_tdma_update;
61310ad9a77SSam Leffler 	}
61410ad9a77SSam Leffler #endif
61567397d39SAdrian Chadd 
61667397d39SAdrian Chadd 	/*
61767397d39SAdrian Chadd 	 * The if_ath 11n support is completely not ready for normal use.
61867397d39SAdrian Chadd 	 * Enabling this option will likely break everything and everything.
61967397d39SAdrian Chadd 	 * Don't think of doing that unless you know what you're doing.
62067397d39SAdrian Chadd 	 */
62167397d39SAdrian Chadd 
6228fd67f92SAdrian Chadd #ifdef	ATH_ENABLE_11N
62367397d39SAdrian Chadd 	/*
62467397d39SAdrian Chadd 	 * Query HT capabilities
62567397d39SAdrian Chadd 	 */
62667397d39SAdrian Chadd 	if (ath_hal_getcapability(ah, HAL_CAP_HT, 0, NULL) == HAL_OK &&
62767397d39SAdrian Chadd 	    (wmodes & (HAL_MODE_HT20 | HAL_MODE_HT40))) {
62867397d39SAdrian Chadd 		int rxs, txs;
62967397d39SAdrian Chadd 
63067397d39SAdrian Chadd 		device_printf(sc->sc_dev, "[HT] enabling HT modes\n");
63167397d39SAdrian Chadd 		ic->ic_htcaps = IEEE80211_HTC_HT		/* HT operation */
63267397d39SAdrian Chadd 			    | IEEE80211_HTC_AMPDU		/* A-MPDU tx/rx */
63367397d39SAdrian Chadd 			    | IEEE80211_HTC_AMSDU		/* A-MSDU tx/rx */
63467397d39SAdrian Chadd 			    | IEEE80211_HTCAP_MAXAMSDU_3839	/* max A-MSDU length */
63567397d39SAdrian Chadd 			    | IEEE80211_HTCAP_SMPS_OFF;		/* SM power save off */
63667397d39SAdrian Chadd 			;
63767397d39SAdrian Chadd 
63876355edbSAdrian Chadd 		/*
63976355edbSAdrian Chadd 		 * Enable short-GI for HT20 only if the hardware
64076355edbSAdrian Chadd 		 * advertises support.
64176355edbSAdrian Chadd 		 * Notably, anything earlier than the AR9287 doesn't.
64276355edbSAdrian Chadd 		 */
64376355edbSAdrian Chadd 		if ((ath_hal_getcapability(ah,
64476355edbSAdrian Chadd 		    HAL_CAP_HT20_SGI, 0, NULL) == HAL_OK) &&
64576355edbSAdrian Chadd 		    (wmodes & HAL_MODE_HT20)) {
64676355edbSAdrian Chadd 			device_printf(sc->sc_dev,
64776355edbSAdrian Chadd 			    "[HT] enabling short-GI in 20MHz mode\n");
64876355edbSAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_SHORTGI20;
64976355edbSAdrian Chadd 		}
65076355edbSAdrian Chadd 
65167397d39SAdrian Chadd 		if (wmodes & HAL_MODE_HT40)
65267397d39SAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40
65367397d39SAdrian Chadd 			    |  IEEE80211_HTCAP_SHORTGI40;
65467397d39SAdrian Chadd 
65567397d39SAdrian Chadd 		/*
65667397d39SAdrian Chadd 		 * rx/tx stream is not currently used anywhere; it needs to be taken
65767397d39SAdrian Chadd 		 * into account when negotiating which MCS rates it'll receive and
65867397d39SAdrian Chadd 		 * what MCS rates are available for TX.
65967397d39SAdrian Chadd 		 */
66067397d39SAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 0, &rxs);
66167397d39SAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 1, &txs);
66267397d39SAdrian Chadd 
66367397d39SAdrian Chadd 		ath_hal_getrxchainmask(ah, &sc->sc_rxchainmask);
66467397d39SAdrian Chadd 		ath_hal_gettxchainmask(ah, &sc->sc_txchainmask);
66567397d39SAdrian Chadd 
66667397d39SAdrian Chadd 		ic->ic_txstream = txs;
66767397d39SAdrian Chadd 		ic->ic_rxstream = rxs;
66867397d39SAdrian Chadd 
66967397d39SAdrian Chadd 		device_printf(sc->sc_dev, "[HT] %d RX streams; %d TX streams\n", rxs, txs);
67067397d39SAdrian Chadd 	}
67167397d39SAdrian Chadd #endif
67267397d39SAdrian Chadd 
673c42a7b7eSSam Leffler 	/*
674c42a7b7eSSam Leffler 	 * Indicate we need the 802.11 header padded to a
675c42a7b7eSSam Leffler 	 * 32-bit boundary for 4-address and QoS frames.
676c42a7b7eSSam Leffler 	 */
677c42a7b7eSSam Leffler 	ic->ic_flags |= IEEE80211_F_DATAPAD;
678c42a7b7eSSam Leffler 
679c42a7b7eSSam Leffler 	/*
680c42a7b7eSSam Leffler 	 * Query the hal about antenna support.
681c42a7b7eSSam Leffler 	 */
682c42a7b7eSSam Leffler 	sc->sc_defant = ath_hal_getdefantenna(ah);
683c42a7b7eSSam Leffler 
684c42a7b7eSSam Leffler 	/*
685c42a7b7eSSam Leffler 	 * Not all chips have the VEOL support we want to
686c42a7b7eSSam Leffler 	 * use with IBSS beacons; check here for it.
687c42a7b7eSSam Leffler 	 */
688c42a7b7eSSam Leffler 	sc->sc_hasveol = ath_hal_hasveol(ah);
6895591b213SSam Leffler 
6905591b213SSam Leffler 	/* get mac address from hardware */
69129aca940SSam Leffler 	ath_hal_getmac(ah, macaddr);
692b032f27cSSam Leffler 	if (sc->sc_hasbmask)
693b032f27cSSam Leffler 		ath_hal_getbssidmask(ah, sc->sc_hwbssidmask);
6945591b213SSam Leffler 
695b032f27cSSam Leffler 	/* NB: used to size node table key mapping array */
696b032f27cSSam Leffler 	ic->ic_max_keyix = sc->sc_keymax;
6975591b213SSam Leffler 	/* call MI attach routine. */
69829aca940SSam Leffler 	ieee80211_ifattach(ic, macaddr);
699b032f27cSSam Leffler 	ic->ic_setregdomain = ath_setregdomain;
700b032f27cSSam Leffler 	ic->ic_getradiocaps = ath_getradiocaps;
701b032f27cSSam Leffler 	sc->sc_opmode = HAL_M_STA;
702b032f27cSSam Leffler 
7035591b213SSam Leffler 	/* override default methods */
704b032f27cSSam Leffler 	ic->ic_newassoc = ath_newassoc;
705b032f27cSSam Leffler 	ic->ic_updateslot = ath_updateslot;
706b032f27cSSam Leffler 	ic->ic_wme.wme_update = ath_wme_update;
707b032f27cSSam Leffler 	ic->ic_vap_create = ath_vap_create;
708b032f27cSSam Leffler 	ic->ic_vap_delete = ath_vap_delete;
709b032f27cSSam Leffler 	ic->ic_raw_xmit = ath_raw_xmit;
710b032f27cSSam Leffler 	ic->ic_update_mcast = ath_update_mcast;
711b032f27cSSam Leffler 	ic->ic_update_promisc = ath_update_promisc;
7125591b213SSam Leffler 	ic->ic_node_alloc = ath_node_alloc;
7131e774079SSam Leffler 	sc->sc_node_free = ic->ic_node_free;
7145591b213SSam Leffler 	ic->ic_node_free = ath_node_free;
71568e8e04eSSam Leffler 	ic->ic_node_getsignal = ath_node_getsignal;
71668e8e04eSSam Leffler 	ic->ic_scan_start = ath_scan_start;
71768e8e04eSSam Leffler 	ic->ic_scan_end = ath_scan_end;
71868e8e04eSSam Leffler 	ic->ic_set_channel = ath_set_channel;
7195591b213SSam Leffler 
7205463c4a4SSam Leffler 	ieee80211_radiotap_attach(ic,
7215463c4a4SSam Leffler 	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
7225463c4a4SSam Leffler 		ATH_TX_RADIOTAP_PRESENT,
7235463c4a4SSam Leffler 	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
7245463c4a4SSam Leffler 		ATH_RX_RADIOTAP_PRESENT);
7255463c4a4SSam Leffler 
7264866e6c2SSam Leffler 	/*
7274866e6c2SSam Leffler 	 * Setup dynamic sysctl's now that country code and
7284866e6c2SSam Leffler 	 * regdomain are available from the hal.
7294866e6c2SSam Leffler 	 */
7304866e6c2SSam Leffler 	ath_sysctlattach(sc);
731e8dabfbeSAdrian Chadd 	ath_sysctl_stats_attach(sc);
73237931a35SAdrian Chadd 	ath_sysctl_hal_attach(sc);
73373454c73SSam Leffler 
734c42a7b7eSSam Leffler 	if (bootverbose)
735c42a7b7eSSam Leffler 		ieee80211_announce(ic);
736c42a7b7eSSam Leffler 	ath_announce(sc);
7375591b213SSam Leffler 	return 0;
738b28b4653SSam Leffler bad2:
739c42a7b7eSSam Leffler 	ath_tx_cleanup(sc);
740b28b4653SSam Leffler 	ath_desc_free(sc);
7415591b213SSam Leffler bad:
7425591b213SSam Leffler 	if (ah)
7435591b213SSam Leffler 		ath_hal_detach(ah);
744fc74a9f9SBrooks Davis 	if (ifp != NULL)
745fc74a9f9SBrooks Davis 		if_free(ifp);
7465591b213SSam Leffler 	sc->sc_invalid = 1;
7475591b213SSam Leffler 	return error;
7485591b213SSam Leffler }
7495591b213SSam Leffler 
7505591b213SSam Leffler int
7515591b213SSam Leffler ath_detach(struct ath_softc *sc)
7525591b213SSam Leffler {
753fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
7545591b213SSam Leffler 
755c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
756c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
7575591b213SSam Leffler 
758c42a7b7eSSam Leffler 	/*
759c42a7b7eSSam Leffler 	 * NB: the order of these is important:
76071b85077SSam Leffler 	 * o stop the chip so no more interrupts will fire
761c42a7b7eSSam Leffler 	 * o call the 802.11 layer before detaching the hal to
762c42a7b7eSSam Leffler 	 *   insure callbacks into the driver to delete global
763c42a7b7eSSam Leffler 	 *   key cache entries can be handled
76471b85077SSam Leffler 	 * o free the taskqueue which drains any pending tasks
765c42a7b7eSSam Leffler 	 * o reclaim the tx queue data structures after calling
766c42a7b7eSSam Leffler 	 *   the 802.11 layer as we'll get called back to reclaim
767c42a7b7eSSam Leffler 	 *   node state and potentially want to use them
768c42a7b7eSSam Leffler 	 * o to cleanup the tx queues the hal is called, so detach
769c42a7b7eSSam Leffler 	 *   it last
770c42a7b7eSSam Leffler 	 * Other than that, it's straightforward...
771c42a7b7eSSam Leffler 	 */
77271b85077SSam Leffler 	ath_stop(ifp);
773b032f27cSSam Leffler 	ieee80211_ifdetach(ifp->if_l2com);
77471b85077SSam Leffler 	taskqueue_free(sc->sc_tq);
77586e07743SSam Leffler #ifdef ATH_TX99_DIAG
77686e07743SSam Leffler 	if (sc->sc_tx99 != NULL)
77786e07743SSam Leffler 		sc->sc_tx99->detach(sc->sc_tx99);
77886e07743SSam Leffler #endif
779c42a7b7eSSam Leffler 	ath_rate_detach(sc->sc_rc);
78048237774SAdrian Chadd 
78148237774SAdrian Chadd 	ath_dfs_detach(sc);
7825591b213SSam Leffler 	ath_desc_free(sc);
783c42a7b7eSSam Leffler 	ath_tx_cleanup(sc);
78471b85077SSam Leffler 	ath_hal_detach(sc->sc_ah);	/* NB: sets chip in full sleep */
785c4c6f08fSRuslan Ermilov 	if_free(ifp);
786f0b2a0beSSam Leffler 
7875591b213SSam Leffler 	return 0;
7885591b213SSam Leffler }
7895591b213SSam Leffler 
790b032f27cSSam Leffler /*
791b032f27cSSam Leffler  * MAC address handling for multiple BSS on the same radio.
792b032f27cSSam Leffler  * The first vap uses the MAC address from the EEPROM.  For
793b032f27cSSam Leffler  * subsequent vap's we set the U/L bit (bit 1) in the MAC
794b032f27cSSam Leffler  * address and use the next six bits as an index.
795b032f27cSSam Leffler  */
796b032f27cSSam Leffler static void
797b032f27cSSam Leffler assign_address(struct ath_softc *sc, uint8_t mac[IEEE80211_ADDR_LEN], int clone)
798b032f27cSSam Leffler {
799b032f27cSSam Leffler 	int i;
800b032f27cSSam Leffler 
801b032f27cSSam Leffler 	if (clone && sc->sc_hasbmask) {
802b032f27cSSam Leffler 		/* NB: we only do this if h/w supports multiple bssid */
803b032f27cSSam Leffler 		for (i = 0; i < 8; i++)
804b032f27cSSam Leffler 			if ((sc->sc_bssidmask & (1<<i)) == 0)
805b032f27cSSam Leffler 				break;
806b032f27cSSam Leffler 		if (i != 0)
807b032f27cSSam Leffler 			mac[0] |= (i << 2)|0x2;
808b032f27cSSam Leffler 	} else
809b032f27cSSam Leffler 		i = 0;
810b032f27cSSam Leffler 	sc->sc_bssidmask |= 1<<i;
811b032f27cSSam Leffler 	sc->sc_hwbssidmask[0] &= ~mac[0];
812b032f27cSSam Leffler 	if (i == 0)
813b032f27cSSam Leffler 		sc->sc_nbssid0++;
814b032f27cSSam Leffler }
815b032f27cSSam Leffler 
816b032f27cSSam Leffler static void
817b032f27cSSam Leffler reclaim_address(struct ath_softc *sc, const uint8_t mac[IEEE80211_ADDR_LEN])
818b032f27cSSam Leffler {
819b032f27cSSam Leffler 	int i = mac[0] >> 2;
820b032f27cSSam Leffler 	uint8_t mask;
821b032f27cSSam Leffler 
822b032f27cSSam Leffler 	if (i != 0 || --sc->sc_nbssid0 == 0) {
823b032f27cSSam Leffler 		sc->sc_bssidmask &= ~(1<<i);
824b032f27cSSam Leffler 		/* recalculate bssid mask from remaining addresses */
825b032f27cSSam Leffler 		mask = 0xff;
826b032f27cSSam Leffler 		for (i = 1; i < 8; i++)
827b032f27cSSam Leffler 			if (sc->sc_bssidmask & (1<<i))
828b032f27cSSam Leffler 				mask &= ~((i<<2)|0x2);
829b032f27cSSam Leffler 		sc->sc_hwbssidmask[0] |= mask;
830b032f27cSSam Leffler 	}
831b032f27cSSam Leffler }
832b032f27cSSam Leffler 
833b032f27cSSam Leffler /*
834b032f27cSSam Leffler  * Assign a beacon xmit slot.  We try to space out
835b032f27cSSam Leffler  * assignments so when beacons are staggered the
836b032f27cSSam Leffler  * traffic coming out of the cab q has maximal time
837b032f27cSSam Leffler  * to go out before the next beacon is scheduled.
838b032f27cSSam Leffler  */
839b032f27cSSam Leffler static int
840b032f27cSSam Leffler assign_bslot(struct ath_softc *sc)
841b032f27cSSam Leffler {
842b032f27cSSam Leffler 	u_int slot, free;
843b032f27cSSam Leffler 
844b032f27cSSam Leffler 	free = 0;
845b032f27cSSam Leffler 	for (slot = 0; slot < ATH_BCBUF; slot++)
846b032f27cSSam Leffler 		if (sc->sc_bslot[slot] == NULL) {
847b032f27cSSam Leffler 			if (sc->sc_bslot[(slot+1)%ATH_BCBUF] == NULL &&
848b032f27cSSam Leffler 			    sc->sc_bslot[(slot-1)%ATH_BCBUF] == NULL)
849b032f27cSSam Leffler 				return slot;
850b032f27cSSam Leffler 			free = slot;
851b032f27cSSam Leffler 			/* NB: keep looking for a double slot */
852b032f27cSSam Leffler 		}
853b032f27cSSam Leffler 	return free;
854b032f27cSSam Leffler }
855b032f27cSSam Leffler 
856b032f27cSSam Leffler static struct ieee80211vap *
857b032f27cSSam Leffler ath_vap_create(struct ieee80211com *ic,
858b032f27cSSam Leffler 	const char name[IFNAMSIZ], int unit, int opmode, int flags,
859b032f27cSSam Leffler 	const uint8_t bssid[IEEE80211_ADDR_LEN],
860b032f27cSSam Leffler 	const uint8_t mac0[IEEE80211_ADDR_LEN])
861b032f27cSSam Leffler {
862b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
863b032f27cSSam Leffler 	struct ath_vap *avp;
864b032f27cSSam Leffler 	struct ieee80211vap *vap;
865b032f27cSSam Leffler 	uint8_t mac[IEEE80211_ADDR_LEN];
866b032f27cSSam Leffler 	int ic_opmode, needbeacon, error;
867b032f27cSSam Leffler 
868b032f27cSSam Leffler 	avp = (struct ath_vap *) malloc(sizeof(struct ath_vap),
869b032f27cSSam Leffler 	    M_80211_VAP, M_WAITOK | M_ZERO);
870b032f27cSSam Leffler 	needbeacon = 0;
871b032f27cSSam Leffler 	IEEE80211_ADDR_COPY(mac, mac0);
872b032f27cSSam Leffler 
873b032f27cSSam Leffler 	ATH_LOCK(sc);
874a8962181SSam Leffler 	ic_opmode = opmode;		/* default to opmode of new vap */
875b032f27cSSam Leffler 	switch (opmode) {
876b032f27cSSam Leffler 	case IEEE80211_M_STA:
877a8962181SSam Leffler 		if (sc->sc_nstavaps != 0) {	/* XXX only 1 for now */
878b032f27cSSam Leffler 			device_printf(sc->sc_dev, "only 1 sta vap supported\n");
879b032f27cSSam Leffler 			goto bad;
880b032f27cSSam Leffler 		}
881b032f27cSSam Leffler 		if (sc->sc_nvaps) {
882b032f27cSSam Leffler 			/*
883a8962181SSam Leffler 			 * With multiple vaps we must fall back
884a8962181SSam Leffler 			 * to s/w beacon miss handling.
885b032f27cSSam Leffler 			 */
886b032f27cSSam Leffler 			flags |= IEEE80211_CLONE_NOBEACONS;
887b032f27cSSam Leffler 		}
888a8962181SSam Leffler 		if (flags & IEEE80211_CLONE_NOBEACONS) {
889a8962181SSam Leffler 			/*
890a8962181SSam Leffler 			 * Station mode w/o beacons are implemented w/ AP mode.
891a8962181SSam Leffler 			 */
892b032f27cSSam Leffler 			ic_opmode = IEEE80211_M_HOSTAP;
893a8962181SSam Leffler 		}
894b032f27cSSam Leffler 		break;
895b032f27cSSam Leffler 	case IEEE80211_M_IBSS:
896b032f27cSSam Leffler 		if (sc->sc_nvaps != 0) {	/* XXX only 1 for now */
897b032f27cSSam Leffler 			device_printf(sc->sc_dev,
898b032f27cSSam Leffler 			    "only 1 ibss vap supported\n");
899b032f27cSSam Leffler 			goto bad;
900b032f27cSSam Leffler 		}
901b032f27cSSam Leffler 		needbeacon = 1;
902b032f27cSSam Leffler 		break;
903b032f27cSSam Leffler 	case IEEE80211_M_AHDEMO:
904584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
90510ad9a77SSam Leffler 		if (flags & IEEE80211_CLONE_TDMA) {
906a8962181SSam Leffler 			if (sc->sc_nvaps != 0) {
907a8962181SSam Leffler 				device_printf(sc->sc_dev,
908a8962181SSam Leffler 				    "only 1 tdma vap supported\n");
909a8962181SSam Leffler 				goto bad;
910a8962181SSam Leffler 			}
91110ad9a77SSam Leffler 			needbeacon = 1;
91210ad9a77SSam Leffler 			flags |= IEEE80211_CLONE_NOBEACONS;
91310ad9a77SSam Leffler 		}
914b032f27cSSam Leffler 		/* fall thru... */
91510ad9a77SSam Leffler #endif
916b032f27cSSam Leffler 	case IEEE80211_M_MONITOR:
917b032f27cSSam Leffler 		if (sc->sc_nvaps != 0 && ic->ic_opmode != opmode) {
918a8962181SSam Leffler 			/*
919a8962181SSam Leffler 			 * Adopt existing mode.  Adding a monitor or ahdemo
920a8962181SSam Leffler 			 * vap to an existing configuration is of dubious
921a8962181SSam Leffler 			 * value but should be ok.
922a8962181SSam Leffler 			 */
923b032f27cSSam Leffler 			/* XXX not right for monitor mode */
924b032f27cSSam Leffler 			ic_opmode = ic->ic_opmode;
925a8962181SSam Leffler 		}
926b032f27cSSam Leffler 		break;
927b032f27cSSam Leffler 	case IEEE80211_M_HOSTAP:
92859aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
929b032f27cSSam Leffler 		needbeacon = 1;
930a8962181SSam Leffler 		break;
931b032f27cSSam Leffler 	case IEEE80211_M_WDS:
932a8962181SSam Leffler 		if (sc->sc_nvaps != 0 && ic->ic_opmode == IEEE80211_M_STA) {
933b032f27cSSam Leffler 			device_printf(sc->sc_dev,
934b032f27cSSam Leffler 			    "wds not supported in sta mode\n");
935b032f27cSSam Leffler 			goto bad;
936b032f27cSSam Leffler 		}
937b032f27cSSam Leffler 		/*
938b032f27cSSam Leffler 		 * Silently remove any request for a unique
939b032f27cSSam Leffler 		 * bssid; WDS vap's always share the local
940b032f27cSSam Leffler 		 * mac address.
941b032f27cSSam Leffler 		 */
942b032f27cSSam Leffler 		flags &= ~IEEE80211_CLONE_BSSID;
943a8962181SSam Leffler 		if (sc->sc_nvaps == 0)
944b032f27cSSam Leffler 			ic_opmode = IEEE80211_M_HOSTAP;
945a8962181SSam Leffler 		else
946a8962181SSam Leffler 			ic_opmode = ic->ic_opmode;
9477d261891SRui Paulo 		break;
948b032f27cSSam Leffler 	default:
949b032f27cSSam Leffler 		device_printf(sc->sc_dev, "unknown opmode %d\n", opmode);
950b032f27cSSam Leffler 		goto bad;
951b032f27cSSam Leffler 	}
952b032f27cSSam Leffler 	/*
953b032f27cSSam Leffler 	 * Check that a beacon buffer is available; the code below assumes it.
954b032f27cSSam Leffler 	 */
955*6b349e5aSAdrian Chadd 	if (needbeacon & TAILQ_EMPTY(&sc->sc_bbuf)) {
956b032f27cSSam Leffler 		device_printf(sc->sc_dev, "no beacon buffer available\n");
957b032f27cSSam Leffler 		goto bad;
958b032f27cSSam Leffler 	}
959b032f27cSSam Leffler 
960b032f27cSSam Leffler 	/* STA, AHDEMO? */
96159aa14a9SRui Paulo 	if (opmode == IEEE80211_M_HOSTAP || opmode == IEEE80211_M_MBSS) {
962b032f27cSSam Leffler 		assign_address(sc, mac, flags & IEEE80211_CLONE_BSSID);
963b032f27cSSam Leffler 		ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
964b032f27cSSam Leffler 	}
965b032f27cSSam Leffler 
966b032f27cSSam Leffler 	vap = &avp->av_vap;
967b032f27cSSam Leffler 	/* XXX can't hold mutex across if_alloc */
968b032f27cSSam Leffler 	ATH_UNLOCK(sc);
969b032f27cSSam Leffler 	error = ieee80211_vap_setup(ic, vap, name, unit, opmode, flags,
970b032f27cSSam Leffler 	    bssid, mac);
971b032f27cSSam Leffler 	ATH_LOCK(sc);
972b032f27cSSam Leffler 	if (error != 0) {
973b032f27cSSam Leffler 		device_printf(sc->sc_dev, "%s: error %d creating vap\n",
974b032f27cSSam Leffler 		    __func__, error);
975b032f27cSSam Leffler 		goto bad2;
976b032f27cSSam Leffler 	}
977b032f27cSSam Leffler 
978b032f27cSSam Leffler 	/* h/w crypto support */
979b032f27cSSam Leffler 	vap->iv_key_alloc = ath_key_alloc;
980b032f27cSSam Leffler 	vap->iv_key_delete = ath_key_delete;
981b032f27cSSam Leffler 	vap->iv_key_set = ath_key_set;
982b032f27cSSam Leffler 	vap->iv_key_update_begin = ath_key_update_begin;
983b032f27cSSam Leffler 	vap->iv_key_update_end = ath_key_update_end;
984b032f27cSSam Leffler 
985b032f27cSSam Leffler 	/* override various methods */
986b032f27cSSam Leffler 	avp->av_recv_mgmt = vap->iv_recv_mgmt;
987b032f27cSSam Leffler 	vap->iv_recv_mgmt = ath_recv_mgmt;
988b032f27cSSam Leffler 	vap->iv_reset = ath_reset_vap;
989b032f27cSSam Leffler 	vap->iv_update_beacon = ath_beacon_update;
990b032f27cSSam Leffler 	avp->av_newstate = vap->iv_newstate;
991b032f27cSSam Leffler 	vap->iv_newstate = ath_newstate;
992b032f27cSSam Leffler 	avp->av_bmiss = vap->iv_bmiss;
993b032f27cSSam Leffler 	vap->iv_bmiss = ath_bmiss_vap;
994b032f27cSSam Leffler 
9959be25f4aSAdrian Chadd 	/* Set default parameters */
9969be25f4aSAdrian Chadd 
9979be25f4aSAdrian Chadd 	/*
9989be25f4aSAdrian Chadd 	 * Anything earlier than some AR9300 series MACs don't
9999be25f4aSAdrian Chadd 	 * support a smaller MPDU density.
10009be25f4aSAdrian Chadd 	 */
10019be25f4aSAdrian Chadd 	vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_8;
10029be25f4aSAdrian Chadd 	/*
10039be25f4aSAdrian Chadd 	 * All NICs can handle the maximum size, however
10049be25f4aSAdrian Chadd 	 * AR5416 based MACs can only TX aggregates w/ RTS
10059be25f4aSAdrian Chadd 	 * protection when the total aggregate size is <= 8k.
10069be25f4aSAdrian Chadd 	 * However, for now that's enforced by the TX path.
10079be25f4aSAdrian Chadd 	 */
10089be25f4aSAdrian Chadd 	vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_64K;
10099be25f4aSAdrian Chadd 
1010b032f27cSSam Leffler 	avp->av_bslot = -1;
1011b032f27cSSam Leffler 	if (needbeacon) {
1012b032f27cSSam Leffler 		/*
1013b032f27cSSam Leffler 		 * Allocate beacon state and setup the q for buffered
1014b032f27cSSam Leffler 		 * multicast frames.  We know a beacon buffer is
1015b032f27cSSam Leffler 		 * available because we checked above.
1016b032f27cSSam Leffler 		 */
1017*6b349e5aSAdrian Chadd 		avp->av_bcbuf = TAILQ_FIRST(&sc->sc_bbuf);
1018*6b349e5aSAdrian Chadd 		TAILQ_REMOVE(&sc->sc_bbuf, avp->av_bcbuf, bf_list);
1019b032f27cSSam Leffler 		if (opmode != IEEE80211_M_IBSS || !sc->sc_hasveol) {
1020b032f27cSSam Leffler 			/*
1021b032f27cSSam Leffler 			 * Assign the vap to a beacon xmit slot.  As above
1022b032f27cSSam Leffler 			 * this cannot fail to find a free one.
1023b032f27cSSam Leffler 			 */
1024b032f27cSSam Leffler 			avp->av_bslot = assign_bslot(sc);
1025b032f27cSSam Leffler 			KASSERT(sc->sc_bslot[avp->av_bslot] == NULL,
1026b032f27cSSam Leffler 			    ("beacon slot %u not empty", avp->av_bslot));
1027b032f27cSSam Leffler 			sc->sc_bslot[avp->av_bslot] = vap;
1028b032f27cSSam Leffler 			sc->sc_nbcnvaps++;
1029b032f27cSSam Leffler 		}
1030b032f27cSSam Leffler 		if (sc->sc_hastsfadd && sc->sc_nbcnvaps > 0) {
1031b032f27cSSam Leffler 			/*
1032b032f27cSSam Leffler 			 * Multple vaps are to transmit beacons and we
1033b032f27cSSam Leffler 			 * have h/w support for TSF adjusting; enable
1034b032f27cSSam Leffler 			 * use of staggered beacons.
1035b032f27cSSam Leffler 			 */
1036b032f27cSSam Leffler 			sc->sc_stagbeacons = 1;
1037b032f27cSSam Leffler 		}
1038b032f27cSSam Leffler 		ath_txq_init(sc, &avp->av_mcastq, ATH_TXQ_SWQ);
1039b032f27cSSam Leffler 	}
1040b032f27cSSam Leffler 
1041b032f27cSSam Leffler 	ic->ic_opmode = ic_opmode;
1042b032f27cSSam Leffler 	if (opmode != IEEE80211_M_WDS) {
1043b032f27cSSam Leffler 		sc->sc_nvaps++;
1044b032f27cSSam Leffler 		if (opmode == IEEE80211_M_STA)
1045b032f27cSSam Leffler 			sc->sc_nstavaps++;
1046fe0dd789SSam Leffler 		if (opmode == IEEE80211_M_MBSS)
1047fe0dd789SSam Leffler 			sc->sc_nmeshvaps++;
1048b032f27cSSam Leffler 	}
1049b032f27cSSam Leffler 	switch (ic_opmode) {
1050b032f27cSSam Leffler 	case IEEE80211_M_IBSS:
1051b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_IBSS;
1052b032f27cSSam Leffler 		break;
1053b032f27cSSam Leffler 	case IEEE80211_M_STA:
1054b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_STA;
1055b032f27cSSam Leffler 		break;
1056b032f27cSSam Leffler 	case IEEE80211_M_AHDEMO:
1057584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
105810ad9a77SSam Leffler 		if (vap->iv_caps & IEEE80211_C_TDMA) {
105910ad9a77SSam Leffler 			sc->sc_tdma = 1;
106010ad9a77SSam Leffler 			/* NB: disable tsf adjust */
106110ad9a77SSam Leffler 			sc->sc_stagbeacons = 0;
106210ad9a77SSam Leffler 		}
106310ad9a77SSam Leffler 		/*
106410ad9a77SSam Leffler 		 * NB: adhoc demo mode is a pseudo mode; to the hal it's
106510ad9a77SSam Leffler 		 * just ap mode.
106610ad9a77SSam Leffler 		 */
106710ad9a77SSam Leffler 		/* fall thru... */
106810ad9a77SSam Leffler #endif
1069b032f27cSSam Leffler 	case IEEE80211_M_HOSTAP:
107059aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
1071b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_HOSTAP;
1072b032f27cSSam Leffler 		break;
1073b032f27cSSam Leffler 	case IEEE80211_M_MONITOR:
1074b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_MONITOR;
1075b032f27cSSam Leffler 		break;
1076b032f27cSSam Leffler 	default:
1077b032f27cSSam Leffler 		/* XXX should not happen */
1078b032f27cSSam Leffler 		break;
1079b032f27cSSam Leffler 	}
1080b032f27cSSam Leffler 	if (sc->sc_hastsfadd) {
1081b032f27cSSam Leffler 		/*
1082b032f27cSSam Leffler 		 * Configure whether or not TSF adjust should be done.
1083b032f27cSSam Leffler 		 */
1084b032f27cSSam Leffler 		ath_hal_settsfadjust(sc->sc_ah, sc->sc_stagbeacons);
1085b032f27cSSam Leffler 	}
108610ad9a77SSam Leffler 	if (flags & IEEE80211_CLONE_NOBEACONS) {
108710ad9a77SSam Leffler 		/*
108810ad9a77SSam Leffler 		 * Enable s/w beacon miss handling.
108910ad9a77SSam Leffler 		 */
109010ad9a77SSam Leffler 		sc->sc_swbmiss = 1;
109110ad9a77SSam Leffler 	}
1092b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1093b032f27cSSam Leffler 
1094b032f27cSSam Leffler 	/* complete setup */
1095b032f27cSSam Leffler 	ieee80211_vap_attach(vap, ath_media_change, ieee80211_media_status);
1096b032f27cSSam Leffler 	return vap;
1097b032f27cSSam Leffler bad2:
1098b032f27cSSam Leffler 	reclaim_address(sc, mac);
1099b032f27cSSam Leffler 	ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
1100b032f27cSSam Leffler bad:
1101b032f27cSSam Leffler 	free(avp, M_80211_VAP);
1102b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1103b032f27cSSam Leffler 	return NULL;
1104b032f27cSSam Leffler }
1105b032f27cSSam Leffler 
1106b032f27cSSam Leffler static void
1107b032f27cSSam Leffler ath_vap_delete(struct ieee80211vap *vap)
1108b032f27cSSam Leffler {
1109b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1110b032f27cSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
1111b032f27cSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
1112b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1113b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
1114b032f27cSSam Leffler 
1115b032f27cSSam Leffler 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1116b032f27cSSam Leffler 		/*
1117b032f27cSSam Leffler 		 * Quiesce the hardware while we remove the vap.  In
1118b032f27cSSam Leffler 		 * particular we need to reclaim all references to
1119b032f27cSSam Leffler 		 * the vap state by any frames pending on the tx queues.
1120b032f27cSSam Leffler 		 */
1121b032f27cSSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
1122b032f27cSSam Leffler 		ath_draintxq(sc);		/* stop xmit side */
1123b032f27cSSam Leffler 		ath_stoprecv(sc);		/* stop recv side */
1124b032f27cSSam Leffler 	}
1125b032f27cSSam Leffler 
1126b032f27cSSam Leffler 	ieee80211_vap_detach(vap);
1127b032f27cSSam Leffler 	ATH_LOCK(sc);
1128b032f27cSSam Leffler 	/*
1129b032f27cSSam Leffler 	 * Reclaim beacon state.  Note this must be done before
1130b032f27cSSam Leffler 	 * the vap instance is reclaimed as we may have a reference
1131b032f27cSSam Leffler 	 * to it in the buffer for the beacon frame.
1132b032f27cSSam Leffler 	 */
1133b032f27cSSam Leffler 	if (avp->av_bcbuf != NULL) {
1134b032f27cSSam Leffler 		if (avp->av_bslot != -1) {
1135b032f27cSSam Leffler 			sc->sc_bslot[avp->av_bslot] = NULL;
1136b032f27cSSam Leffler 			sc->sc_nbcnvaps--;
1137b032f27cSSam Leffler 		}
1138b032f27cSSam Leffler 		ath_beacon_return(sc, avp->av_bcbuf);
1139b032f27cSSam Leffler 		avp->av_bcbuf = NULL;
1140b032f27cSSam Leffler 		if (sc->sc_nbcnvaps == 0) {
1141b032f27cSSam Leffler 			sc->sc_stagbeacons = 0;
1142b032f27cSSam Leffler 			if (sc->sc_hastsfadd)
1143b032f27cSSam Leffler 				ath_hal_settsfadjust(sc->sc_ah, 0);
1144b032f27cSSam Leffler 		}
1145b032f27cSSam Leffler 		/*
1146b032f27cSSam Leffler 		 * Reclaim any pending mcast frames for the vap.
1147b032f27cSSam Leffler 		 */
1148b032f27cSSam Leffler 		ath_tx_draintxq(sc, &avp->av_mcastq);
1149b032f27cSSam Leffler 		ATH_TXQ_LOCK_DESTROY(&avp->av_mcastq);
1150b032f27cSSam Leffler 	}
1151b032f27cSSam Leffler 	/*
1152b032f27cSSam Leffler 	 * Update bookkeeping.
1153b032f27cSSam Leffler 	 */
1154b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA) {
1155b032f27cSSam Leffler 		sc->sc_nstavaps--;
1156b032f27cSSam Leffler 		if (sc->sc_nstavaps == 0 && sc->sc_swbmiss)
1157b032f27cSSam Leffler 			sc->sc_swbmiss = 0;
115859aa14a9SRui Paulo 	} else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
115959aa14a9SRui Paulo 	    vap->iv_opmode == IEEE80211_M_MBSS) {
1160b032f27cSSam Leffler 		reclaim_address(sc, vap->iv_myaddr);
1161b032f27cSSam Leffler 		ath_hal_setbssidmask(ah, sc->sc_hwbssidmask);
1162fe0dd789SSam Leffler 		if (vap->iv_opmode == IEEE80211_M_MBSS)
1163fe0dd789SSam Leffler 			sc->sc_nmeshvaps--;
1164b032f27cSSam Leffler 	}
1165b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_WDS)
1166b032f27cSSam Leffler 		sc->sc_nvaps--;
1167584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
116810ad9a77SSam Leffler 	/* TDMA operation ceases when the last vap is destroyed */
116910ad9a77SSam Leffler 	if (sc->sc_tdma && sc->sc_nvaps == 0) {
117010ad9a77SSam Leffler 		sc->sc_tdma = 0;
117110ad9a77SSam Leffler 		sc->sc_swbmiss = 0;
117210ad9a77SSam Leffler 	}
117310ad9a77SSam Leffler #endif
1174b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1175b032f27cSSam Leffler 	free(avp, M_80211_VAP);
1176b032f27cSSam Leffler 
1177b032f27cSSam Leffler 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1178b032f27cSSam Leffler 		/*
1179b032f27cSSam Leffler 		 * Restart rx+tx machines if still running (RUNNING will
1180b032f27cSSam Leffler 		 * be reset if we just destroyed the last vap).
1181b032f27cSSam Leffler 		 */
1182b032f27cSSam Leffler 		if (ath_startrecv(sc) != 0)
1183b032f27cSSam Leffler 			if_printf(ifp, "%s: unable to restart recv logic\n",
1184b032f27cSSam Leffler 			    __func__);
1185c89b957aSSam Leffler 		if (sc->sc_beacons) {		/* restart beacons */
1186c89b957aSSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
1187c89b957aSSam Leffler 			if (sc->sc_tdma)
1188c89b957aSSam Leffler 				ath_tdma_config(sc, NULL);
1189c89b957aSSam Leffler 			else
1190c89b957aSSam Leffler #endif
1191b032f27cSSam Leffler 				ath_beacon_config(sc, NULL);
1192c89b957aSSam Leffler 		}
1193b032f27cSSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
1194b032f27cSSam Leffler 	}
1195b032f27cSSam Leffler }
1196b032f27cSSam Leffler 
11975591b213SSam Leffler void
11985591b213SSam Leffler ath_suspend(struct ath_softc *sc)
11995591b213SSam Leffler {
1200fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
1201d3ac945bSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
12025591b213SSam Leffler 
1203c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1204c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
12055591b213SSam Leffler 
1206d3ac945bSSam Leffler 	sc->sc_resume_up = (ifp->if_flags & IFF_UP) != 0;
1207d3ac945bSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA)
12085591b213SSam Leffler 		ath_stop(ifp);
1209d3ac945bSSam Leffler 	else
1210d3ac945bSSam Leffler 		ieee80211_suspend_all(ic);
1211d3ac945bSSam Leffler 	/*
1212d3ac945bSSam Leffler 	 * NB: don't worry about putting the chip in low power
1213d3ac945bSSam Leffler 	 * mode; pci will power off our socket on suspend and
1214f29b8b7fSWarner Losh 	 * CardBus detaches the device.
1215d3ac945bSSam Leffler 	 */
1216d3ac945bSSam Leffler }
1217d3ac945bSSam Leffler 
1218d3ac945bSSam Leffler /*
1219d3ac945bSSam Leffler  * Reset the key cache since some parts do not reset the
1220d3ac945bSSam Leffler  * contents on resume.  First we clear all entries, then
1221d3ac945bSSam Leffler  * re-load keys that the 802.11 layer assumes are setup
1222d3ac945bSSam Leffler  * in h/w.
1223d3ac945bSSam Leffler  */
1224d3ac945bSSam Leffler static void
1225d3ac945bSSam Leffler ath_reset_keycache(struct ath_softc *sc)
1226d3ac945bSSam Leffler {
1227d3ac945bSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
1228d3ac945bSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
1229d3ac945bSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1230d3ac945bSSam Leffler 	int i;
1231d3ac945bSSam Leffler 
1232d3ac945bSSam Leffler 	for (i = 0; i < sc->sc_keymax; i++)
1233d3ac945bSSam Leffler 		ath_hal_keyreset(ah, i);
1234d3ac945bSSam Leffler 	ieee80211_crypto_reload_keys(ic);
12355591b213SSam Leffler }
12365591b213SSam Leffler 
12375591b213SSam Leffler void
12385591b213SSam Leffler ath_resume(struct ath_softc *sc)
12395591b213SSam Leffler {
1240fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
1241d3ac945bSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
1242d3ac945bSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1243d3ac945bSSam Leffler 	HAL_STATUS status;
12445591b213SSam Leffler 
1245c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1246c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
12475591b213SSam Leffler 
1248d3ac945bSSam Leffler 	/*
1249d3ac945bSSam Leffler 	 * Must reset the chip before we reload the
1250d3ac945bSSam Leffler 	 * keycache as we were powered down on suspend.
1251d3ac945bSSam Leffler 	 */
1252054d7b69SSam Leffler 	ath_hal_reset(ah, sc->sc_opmode,
1253054d7b69SSam Leffler 	    sc->sc_curchan != NULL ? sc->sc_curchan : ic->ic_curchan,
1254054d7b69SSam Leffler 	    AH_FALSE, &status);
1255d3ac945bSSam Leffler 	ath_reset_keycache(sc);
12567e5eb44dSAdrian Chadd 
12577e5eb44dSAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
12587e5eb44dSAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
12597e5eb44dSAdrian Chadd 
1260d3ac945bSSam Leffler 	if (sc->sc_resume_up) {
1261d3ac945bSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_STA) {
1262fc74a9f9SBrooks Davis 			ath_init(sc);
1263394f34a5SSam Leffler 			/*
1264394f34a5SSam Leffler 			 * Program the beacon registers using the last rx'd
1265394f34a5SSam Leffler 			 * beacon frame and enable sync on the next beacon
1266394f34a5SSam Leffler 			 * we see.  This should handle the case where we
1267394f34a5SSam Leffler 			 * wakeup and find the same AP and also the case where
1268394f34a5SSam Leffler 			 * we wakeup and need to roam.  For the latter we
1269394f34a5SSam Leffler 			 * should get bmiss events that trigger a roam.
1270394f34a5SSam Leffler 			 */
1271394f34a5SSam Leffler 			ath_beacon_config(sc, NULL);
1272394f34a5SSam Leffler 			sc->sc_syncbeacon = 1;
1273d3ac945bSSam Leffler 		} else
1274d3ac945bSSam Leffler 			ieee80211_resume_all(ic);
12755591b213SSam Leffler 	}
1276b50c8bdeSSam Leffler 	if (sc->sc_softled) {
1277869ff02eSSam Leffler 		ath_hal_gpioCfgOutput(ah, sc->sc_ledpin,
1278869ff02eSSam Leffler 		    HAL_GPIO_MUX_MAC_NETWORK_LED);
1279d3ac945bSSam Leffler 		ath_hal_gpioset(ah, sc->sc_ledpin, !sc->sc_ledon);
1280b50c8bdeSSam Leffler 	}
12812fd9aabbSAdrian Chadd 
12822fd9aabbSAdrian Chadd 	/* XXX beacons ? */
12836b59f5e3SSam Leffler }
12845591b213SSam Leffler 
12855591b213SSam Leffler void
12865591b213SSam Leffler ath_shutdown(struct ath_softc *sc)
12875591b213SSam Leffler {
1288fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
12895591b213SSam Leffler 
1290c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1291c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
12925591b213SSam Leffler 
12935591b213SSam Leffler 	ath_stop(ifp);
1294d3ac945bSSam Leffler 	/* NB: no point powering down chip as we're about to reboot */
12955591b213SSam Leffler }
12965591b213SSam Leffler 
1297c42a7b7eSSam Leffler /*
1298c42a7b7eSSam Leffler  * Interrupt handler.  Most of the actual processing is deferred.
1299c42a7b7eSSam Leffler  */
13005591b213SSam Leffler void
13015591b213SSam Leffler ath_intr(void *arg)
13025591b213SSam Leffler {
13035591b213SSam Leffler 	struct ath_softc *sc = arg;
1304fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
13055591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
13066f5fe81eSAdrian Chadd 	HAL_INT status = 0;
13075591b213SSam Leffler 
13085591b213SSam Leffler 	if (sc->sc_invalid) {
13095591b213SSam Leffler 		/*
1310b58b3803SSam Leffler 		 * The hardware is not ready/present, don't touch anything.
1311b58b3803SSam Leffler 		 * Note this can happen early on if the IRQ is shared.
13125591b213SSam Leffler 		 */
1313c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid; ignored\n", __func__);
13145591b213SSam Leffler 		return;
13155591b213SSam Leffler 	}
1316fdd758d4SSam Leffler 	if (!ath_hal_intrpend(ah))		/* shared irq, not for us */
1317fdd758d4SSam Leffler 		return;
131868e8e04eSSam Leffler 	if ((ifp->if_flags & IFF_UP) == 0 ||
131968e8e04eSSam Leffler 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
132068e8e04eSSam Leffler 		HAL_INT status;
132168e8e04eSSam Leffler 
1322c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
1323c42a7b7eSSam Leffler 			__func__, ifp->if_flags);
13245591b213SSam Leffler 		ath_hal_getisr(ah, &status);	/* clear ISR */
13255591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable further intr's */
13265591b213SSam Leffler 		return;
13275591b213SSam Leffler 	}
1328c42a7b7eSSam Leffler 	/*
1329c42a7b7eSSam Leffler 	 * Figure out the reason(s) for the interrupt.  Note
1330c42a7b7eSSam Leffler 	 * that the hal returns a pseudo-ISR that may include
1331c42a7b7eSSam Leffler 	 * bits we haven't explicitly enabled so we mask the
1332c42a7b7eSSam Leffler 	 * value to insure we only process bits we requested.
1333c42a7b7eSSam Leffler 	 */
13345591b213SSam Leffler 	ath_hal_getisr(ah, &status);		/* NB: clears ISR too */
1335c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_INTR, "%s: status 0x%x\n", __func__, status);
1336ecddff40SSam Leffler 	status &= sc->sc_imask;			/* discard unasked for bits */
13376f5fe81eSAdrian Chadd 
13386f5fe81eSAdrian Chadd 	/* Short-circuit un-handled interrupts */
13396f5fe81eSAdrian Chadd 	if (status == 0x0)
13406f5fe81eSAdrian Chadd 		return;
13416f5fe81eSAdrian Chadd 
13425591b213SSam Leffler 	if (status & HAL_INT_FATAL) {
13435591b213SSam Leffler 		sc->sc_stats.ast_hardware++;
13445591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
134516c8acaaSSam Leffler 		ath_fatal_proc(sc, 0);
13465591b213SSam Leffler 	} else {
1347c42a7b7eSSam Leffler 		if (status & HAL_INT_SWBA) {
1348c42a7b7eSSam Leffler 			/*
1349c42a7b7eSSam Leffler 			 * Software beacon alert--time to send a beacon.
1350c42a7b7eSSam Leffler 			 * Handle beacon transmission directly; deferring
1351c42a7b7eSSam Leffler 			 * this is too slow to meet timing constraints
1352c42a7b7eSSam Leffler 			 * under load.
1353c42a7b7eSSam Leffler 			 */
1354584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
135510ad9a77SSam Leffler 			if (sc->sc_tdma) {
135610ad9a77SSam Leffler 				if (sc->sc_tdmaswba == 0) {
135710ad9a77SSam Leffler 					struct ieee80211com *ic = ifp->if_l2com;
135810ad9a77SSam Leffler 					struct ieee80211vap *vap =
135910ad9a77SSam Leffler 					    TAILQ_FIRST(&ic->ic_vaps);
136010ad9a77SSam Leffler 					ath_tdma_beacon_send(sc, vap);
136110ad9a77SSam Leffler 					sc->sc_tdmaswba =
136210ad9a77SSam Leffler 					    vap->iv_tdma->tdma_bintval;
136310ad9a77SSam Leffler 				} else
136410ad9a77SSam Leffler 					sc->sc_tdmaswba--;
136510ad9a77SSam Leffler 			} else
136610ad9a77SSam Leffler #endif
1367339ccfb3SSam Leffler 			{
1368c42a7b7eSSam Leffler 				ath_beacon_proc(sc, 0);
1369339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
1370339ccfb3SSam Leffler 				/*
1371339ccfb3SSam Leffler 				 * Schedule the rx taskq in case there's no
1372339ccfb3SSam Leffler 				 * traffic so any frames held on the staging
1373339ccfb3SSam Leffler 				 * queue are aged and potentially flushed.
1374339ccfb3SSam Leffler 				 */
1375339ccfb3SSam Leffler 				taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
1376339ccfb3SSam Leffler #endif
1377339ccfb3SSam Leffler 			}
1378c42a7b7eSSam Leffler 		}
13795591b213SSam Leffler 		if (status & HAL_INT_RXEOL) {
13801fdadc0fSAdrian Chadd 			int imask = sc->sc_imask;
13815591b213SSam Leffler 			/*
13825591b213SSam Leffler 			 * NB: the hardware should re-read the link when
13835591b213SSam Leffler 			 *     RXE bit is written, but it doesn't work at
13845591b213SSam Leffler 			 *     least on older hardware revs.
13855591b213SSam Leffler 			 */
13865591b213SSam Leffler 			sc->sc_stats.ast_rxeol++;
138773f895fcSAdrian Chadd 			/*
138873f895fcSAdrian Chadd 			 * Disable RXEOL/RXORN - prevent an interrupt
138973f895fcSAdrian Chadd 			 * storm until the PCU logic can be reset.
13901fdadc0fSAdrian Chadd 			 * In case the interface is reset some other
13911fdadc0fSAdrian Chadd 			 * way before "sc_kickpcu" is called, don't
13921fdadc0fSAdrian Chadd 			 * modify sc_imask - that way if it is reset
13931fdadc0fSAdrian Chadd 			 * by a call to ath_reset() somehow, the
13941fdadc0fSAdrian Chadd 			 * interrupt mask will be correctly reprogrammed.
139573f895fcSAdrian Chadd 			 */
13961fdadc0fSAdrian Chadd 			imask &= ~(HAL_INT_RXEOL | HAL_INT_RXORN);
13971fdadc0fSAdrian Chadd 			ath_hal_intrset(ah, imask);
13981fdadc0fSAdrian Chadd 			/*
13991fdadc0fSAdrian Chadd 			 * Enqueue an RX proc, to handled whatever
14001fdadc0fSAdrian Chadd 			 * is in the RX queue.
14011fdadc0fSAdrian Chadd 			 * This will then kick the PCU.
14021fdadc0fSAdrian Chadd 			 */
14031fdadc0fSAdrian Chadd 			taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
14045591b213SSam Leffler 			sc->sc_rxlink = NULL;
14051fdadc0fSAdrian Chadd 			sc->sc_kickpcu = 1;
14065591b213SSam Leffler 		}
14075591b213SSam Leffler 		if (status & HAL_INT_TXURN) {
14085591b213SSam Leffler 			sc->sc_stats.ast_txurn++;
14095591b213SSam Leffler 			/* bump tx trigger level */
14105591b213SSam Leffler 			ath_hal_updatetxtriglevel(ah, AH_TRUE);
14115591b213SSam Leffler 		}
14125591b213SSam Leffler 		if (status & HAL_INT_RX)
14130bbf5441SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
14145591b213SSam Leffler 		if (status & HAL_INT_TX)
14150bbf5441SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_txtask);
14165591b213SSam Leffler 		if (status & HAL_INT_BMISS) {
14175591b213SSam Leffler 			sc->sc_stats.ast_bmiss++;
14180bbf5441SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_bmisstask);
14195591b213SSam Leffler 		}
14206ad02dbaSAdrian Chadd 		if (status & HAL_INT_GTT)
14216ad02dbaSAdrian Chadd 			sc->sc_stats.ast_tx_timeout++;
14225594f5c0SAdrian Chadd 		if (status & HAL_INT_CST)
14235594f5c0SAdrian Chadd 			sc->sc_stats.ast_tx_cst++;
1424c42a7b7eSSam Leffler 		if (status & HAL_INT_MIB) {
1425c42a7b7eSSam Leffler 			sc->sc_stats.ast_mib++;
1426c42a7b7eSSam Leffler 			/*
1427c42a7b7eSSam Leffler 			 * Disable interrupts until we service the MIB
1428c42a7b7eSSam Leffler 			 * interrupt; otherwise it will continue to fire.
1429c42a7b7eSSam Leffler 			 */
1430c42a7b7eSSam Leffler 			ath_hal_intrset(ah, 0);
1431c42a7b7eSSam Leffler 			/*
1432c42a7b7eSSam Leffler 			 * Let the hal handle the event.  We assume it will
1433c42a7b7eSSam Leffler 			 * clear whatever condition caused the interrupt.
1434c42a7b7eSSam Leffler 			 */
1435ffa2cab6SSam Leffler 			ath_hal_mibevent(ah, &sc->sc_halstats);
1436c42a7b7eSSam Leffler 			ath_hal_intrset(ah, sc->sc_imask);
1437c42a7b7eSSam Leffler 		}
14389c4fc1e8SSam Leffler 		if (status & HAL_INT_RXORN) {
14399c4fc1e8SSam Leffler 			/* NB: hal marks HAL_INT_FATAL when RXORN is fatal */
14409c4fc1e8SSam Leffler 			sc->sc_stats.ast_rxorn++;
14419c4fc1e8SSam Leffler 		}
14425591b213SSam Leffler 	}
14435591b213SSam Leffler }
14445591b213SSam Leffler 
14455591b213SSam Leffler static void
14465591b213SSam Leffler ath_fatal_proc(void *arg, int pending)
14475591b213SSam Leffler {
14485591b213SSam Leffler 	struct ath_softc *sc = arg;
1449fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
145016c8acaaSSam Leffler 	u_int32_t *state;
145116c8acaaSSam Leffler 	u_int32_t len;
145268e8e04eSSam Leffler 	void *sp;
14535591b213SSam Leffler 
1454c42a7b7eSSam Leffler 	if_printf(ifp, "hardware error; resetting\n");
145516c8acaaSSam Leffler 	/*
145616c8acaaSSam Leffler 	 * Fatal errors are unrecoverable.  Typically these
145716c8acaaSSam Leffler 	 * are caused by DMA errors.  Collect h/w state from
145816c8acaaSSam Leffler 	 * the hal so we can diagnose what's going on.
145916c8acaaSSam Leffler 	 */
146068e8e04eSSam Leffler 	if (ath_hal_getfatalstate(sc->sc_ah, &sp, &len)) {
146116c8acaaSSam Leffler 		KASSERT(len >= 6*sizeof(u_int32_t), ("len %u bytes", len));
146268e8e04eSSam Leffler 		state = sp;
146316c8acaaSSam Leffler 		if_printf(ifp, "0x%08x 0x%08x 0x%08x, 0x%08x 0x%08x 0x%08x\n",
146416c8acaaSSam Leffler 		    state[0], state[1] , state[2], state[3],
146516c8acaaSSam Leffler 		    state[4], state[5]);
146616c8acaaSSam Leffler 	}
1467c42a7b7eSSam Leffler 	ath_reset(ifp);
14685591b213SSam Leffler }
14695591b213SSam Leffler 
14705591b213SSam Leffler static void
1471b032f27cSSam Leffler ath_bmiss_vap(struct ieee80211vap *vap)
14725591b213SSam Leffler {
147359fbb257SSam Leffler 	/*
147459fbb257SSam Leffler 	 * Workaround phantom bmiss interrupts by sanity-checking
147559fbb257SSam Leffler 	 * the time of our last rx'd frame.  If it is within the
147659fbb257SSam Leffler 	 * beacon miss interval then ignore the interrupt.  If it's
147759fbb257SSam Leffler 	 * truly a bmiss we'll get another interrupt soon and that'll
147859fbb257SSam Leffler 	 * be dispatched up for processing.  Note this applies only
147959fbb257SSam Leffler 	 * for h/w beacon miss events.
148059fbb257SSam Leffler 	 */
148159fbb257SSam Leffler 	if ((vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) == 0) {
1482a7ace843SSam Leffler 		struct ifnet *ifp = vap->iv_ic->ic_ifp;
1483a7ace843SSam Leffler 		struct ath_softc *sc = ifp->if_softc;
1484d7736e13SSam Leffler 		u_int64_t lastrx = sc->sc_lastrx;
1485d7736e13SSam Leffler 		u_int64_t tsf = ath_hal_gettsf64(sc->sc_ah);
1486d7736e13SSam Leffler 		u_int bmisstimeout =
1487b032f27cSSam Leffler 			vap->iv_bmissthreshold * vap->iv_bss->ni_intval * 1024;
1488d7736e13SSam Leffler 
1489d7736e13SSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
1490d7736e13SSam Leffler 		    "%s: tsf %llu lastrx %lld (%llu) bmiss %u\n",
1491d7736e13SSam Leffler 		    __func__, (unsigned long long) tsf,
1492d7736e13SSam Leffler 		    (unsigned long long)(tsf - lastrx),
1493d7736e13SSam Leffler 		    (unsigned long long) lastrx, bmisstimeout);
149459fbb257SSam Leffler 
149559fbb257SSam Leffler 		if (tsf - lastrx <= bmisstimeout) {
1496d7736e13SSam Leffler 			sc->sc_stats.ast_bmiss_phantom++;
149759fbb257SSam Leffler 			return;
149859fbb257SSam Leffler 		}
149959fbb257SSam Leffler 	}
150059fbb257SSam Leffler 	ATH_VAP(vap)->av_bmiss(vap);
1501e585d188SSam Leffler }
1502b032f27cSSam Leffler 
1503459bc4f0SSam Leffler static int
1504459bc4f0SSam Leffler ath_hal_gethangstate(struct ath_hal *ah, uint32_t mask, uint32_t *hangs)
1505459bc4f0SSam Leffler {
1506459bc4f0SSam Leffler 	uint32_t rsize;
1507459bc4f0SSam Leffler 	void *sp;
1508459bc4f0SSam Leffler 
150925c96056SAdrian Chadd 	if (!ath_hal_getdiagstate(ah, HAL_DIAG_CHECK_HANGS, &mask, sizeof(mask), &sp, &rsize))
1510459bc4f0SSam Leffler 		return 0;
1511459bc4f0SSam Leffler 	KASSERT(rsize == sizeof(uint32_t), ("resultsize %u", rsize));
1512459bc4f0SSam Leffler 	*hangs = *(uint32_t *)sp;
1513459bc4f0SSam Leffler 	return 1;
1514459bc4f0SSam Leffler }
1515459bc4f0SSam Leffler 
1516b032f27cSSam Leffler static void
1517b032f27cSSam Leffler ath_bmiss_proc(void *arg, int pending)
1518b032f27cSSam Leffler {
1519b032f27cSSam Leffler 	struct ath_softc *sc = arg;
1520b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
1521459bc4f0SSam Leffler 	uint32_t hangs;
1522b032f27cSSam Leffler 
1523b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: pending %u\n", __func__, pending);
1524459bc4f0SSam Leffler 
1525459bc4f0SSam Leffler 	if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0) {
15264fa8d4efSDaniel Eischen 		if_printf(ifp, "bb hang detected (0x%x), resetting\n", hangs);
1527459bc4f0SSam Leffler 		ath_reset(ifp);
1528459bc4f0SSam Leffler 	} else
1529b032f27cSSam Leffler 		ieee80211_beacon_miss(ifp->if_l2com);
15305591b213SSam Leffler }
15315591b213SSam Leffler 
1532724c193aSSam Leffler /*
1533b032f27cSSam Leffler  * Handle TKIP MIC setup to deal hardware that doesn't do MIC
1534b032f27cSSam Leffler  * calcs together with WME.  If necessary disable the crypto
1535b032f27cSSam Leffler  * hardware and mark the 802.11 state so keys will be setup
1536b032f27cSSam Leffler  * with the MIC work done in software.
1537b032f27cSSam Leffler  */
1538b032f27cSSam Leffler static void
1539b032f27cSSam Leffler ath_settkipmic(struct ath_softc *sc)
1540b032f27cSSam Leffler {
1541b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
1542b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
1543b032f27cSSam Leffler 
1544b032f27cSSam Leffler 	if ((ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIP) && !sc->sc_wmetkipmic) {
1545b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_WME) {
1546b032f27cSSam Leffler 			ath_hal_settkipmic(sc->sc_ah, AH_FALSE);
1547b032f27cSSam Leffler 			ic->ic_cryptocaps &= ~IEEE80211_CRYPTO_TKIPMIC;
1548b032f27cSSam Leffler 		} else {
1549b032f27cSSam Leffler 			ath_hal_settkipmic(sc->sc_ah, AH_TRUE);
1550b032f27cSSam Leffler 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
1551b032f27cSSam Leffler 		}
1552b032f27cSSam Leffler 	}
1553b032f27cSSam Leffler }
1554b032f27cSSam Leffler 
15555591b213SSam Leffler static void
15565591b213SSam Leffler ath_init(void *arg)
15575591b213SSam Leffler {
15585591b213SSam Leffler 	struct ath_softc *sc = (struct ath_softc *) arg;
1559fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
1560b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
15615591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
15625591b213SSam Leffler 	HAL_STATUS status;
15635591b213SSam Leffler 
1564c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
1565c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
15665591b213SSam Leffler 
1567f0b2a0beSSam Leffler 	ATH_LOCK(sc);
15685591b213SSam Leffler 	/*
15695591b213SSam Leffler 	 * Stop anything previously setup.  This is safe
15705591b213SSam Leffler 	 * whether this is the first time through or not.
15715591b213SSam Leffler 	 */
1572c42a7b7eSSam Leffler 	ath_stop_locked(ifp);
15735591b213SSam Leffler 
15745591b213SSam Leffler 	/*
15755591b213SSam Leffler 	 * The basic interface to setting the hardware in a good
15765591b213SSam Leffler 	 * state is ``reset''.  On return the hardware is known to
15775591b213SSam Leffler 	 * be powered up and with interrupts disabled.  This must
15785591b213SSam Leffler 	 * be followed by initialization of the appropriate bits
15795591b213SSam Leffler 	 * and then setup of the interrupt mask.
15805591b213SSam Leffler 	 */
1581b032f27cSSam Leffler 	ath_settkipmic(sc);
158259efa8b5SSam Leffler 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_FALSE, &status)) {
15835591b213SSam Leffler 		if_printf(ifp, "unable to reset hardware; hal status %u\n",
15845591b213SSam Leffler 			status);
1585b032f27cSSam Leffler 		ATH_UNLOCK(sc);
1586b032f27cSSam Leffler 		return;
15875591b213SSam Leffler 	}
1588b032f27cSSam Leffler 	ath_chan_change(sc, ic->ic_curchan);
15895591b213SSam Leffler 
159048237774SAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
159148237774SAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
159248237774SAdrian Chadd 
15935591b213SSam Leffler 	/*
1594c59005e9SSam Leffler 	 * Likewise this is set during reset so update
1595c59005e9SSam Leffler 	 * state cached in the driver.
1596c59005e9SSam Leffler 	 */
1597c59005e9SSam Leffler 	sc->sc_diversity = ath_hal_getdiversity(ah);
15982dc7fcc4SSam Leffler 	sc->sc_lastlongcal = 0;
15992dc7fcc4SSam Leffler 	sc->sc_resetcal = 1;
16002dc7fcc4SSam Leffler 	sc->sc_lastcalreset = 0;
1601a108ab63SAdrian Chadd 	sc->sc_lastani = 0;
1602a108ab63SAdrian Chadd 	sc->sc_lastshortcal = 0;
1603a108ab63SAdrian Chadd 	sc->sc_doresetcal = AH_FALSE;
16042fd9aabbSAdrian Chadd 	/*
16052fd9aabbSAdrian Chadd 	 * Beacon timers were cleared here; give ath_newstate()
16062fd9aabbSAdrian Chadd 	 * a hint that the beacon timers should be poked when
16072fd9aabbSAdrian Chadd 	 * things transition to the RUN state.
16082fd9aabbSAdrian Chadd 	 */
16092fd9aabbSAdrian Chadd 	sc->sc_beacons = 0;
1610c42a7b7eSSam Leffler 
1611c42a7b7eSSam Leffler 	/*
16125591b213SSam Leffler 	 * Setup the hardware after reset: the key cache
16135591b213SSam Leffler 	 * is filled as needed and the receive engine is
16145591b213SSam Leffler 	 * set going.  Frame transmit is handled entirely
16155591b213SSam Leffler 	 * in the frame output path; there's nothing to do
16165591b213SSam Leffler 	 * here except setup the interrupt mask.
16175591b213SSam Leffler 	 */
16185591b213SSam Leffler 	if (ath_startrecv(sc) != 0) {
16195591b213SSam Leffler 		if_printf(ifp, "unable to start recv logic\n");
1620b032f27cSSam Leffler 		ATH_UNLOCK(sc);
1621b032f27cSSam Leffler 		return;
16225591b213SSam Leffler 	}
16235591b213SSam Leffler 
16245591b213SSam Leffler 	/*
16255591b213SSam Leffler 	 * Enable interrupts.
16265591b213SSam Leffler 	 */
16275591b213SSam Leffler 	sc->sc_imask = HAL_INT_RX | HAL_INT_TX
16285591b213SSam Leffler 		  | HAL_INT_RXEOL | HAL_INT_RXORN
16295591b213SSam Leffler 		  | HAL_INT_FATAL | HAL_INT_GLOBAL;
1630c42a7b7eSSam Leffler 	/*
1631c42a7b7eSSam Leffler 	 * Enable MIB interrupts when there are hardware phy counters.
1632c42a7b7eSSam Leffler 	 * Note we only do this (at the moment) for station mode.
1633c42a7b7eSSam Leffler 	 */
1634c42a7b7eSSam Leffler 	if (sc->sc_needmib && ic->ic_opmode == IEEE80211_M_STA)
1635c42a7b7eSSam Leffler 		sc->sc_imask |= HAL_INT_MIB;
16365591b213SSam Leffler 
16375594f5c0SAdrian Chadd 	/* Enable global TX timeout and carrier sense timeout if available */
16386ad02dbaSAdrian Chadd 	if (ath_hal_gtxto_supported(ah))
16393788ebedSAdrian Chadd 		sc->sc_imask |= HAL_INT_GTT;
1640d0a0ebc6SAdrian Chadd 
1641d0a0ebc6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: imask=0x%x\n",
1642d0a0ebc6SAdrian Chadd 		__func__, sc->sc_imask);
16436ad02dbaSAdrian Chadd 
164413f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
16452e986da5SSam Leffler 	callout_reset(&sc->sc_wd_ch, hz, ath_watchdog, sc);
1646b032f27cSSam Leffler 	ath_hal_intrset(ah, sc->sc_imask);
16475591b213SSam Leffler 
1648b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1649b032f27cSSam Leffler 
165086e07743SSam Leffler #ifdef ATH_TX99_DIAG
165186e07743SSam Leffler 	if (sc->sc_tx99 != NULL)
165286e07743SSam Leffler 		sc->sc_tx99->start(sc->sc_tx99);
165386e07743SSam Leffler 	else
165486e07743SSam Leffler #endif
1655b032f27cSSam Leffler 	ieee80211_start_all(ic);		/* start all vap's */
16565591b213SSam Leffler }
16575591b213SSam Leffler 
16585591b213SSam Leffler static void
1659c42a7b7eSSam Leffler ath_stop_locked(struct ifnet *ifp)
16605591b213SSam Leffler {
16615591b213SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
16625591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
16635591b213SSam Leffler 
1664c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid %u if_flags 0x%x\n",
1665c42a7b7eSSam Leffler 		__func__, sc->sc_invalid, ifp->if_flags);
16665591b213SSam Leffler 
1667c42a7b7eSSam Leffler 	ATH_LOCK_ASSERT(sc);
166813f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
16695591b213SSam Leffler 		/*
16705591b213SSam Leffler 		 * Shutdown the hardware and driver:
1671c42a7b7eSSam Leffler 		 *    reset 802.11 state machine
16725591b213SSam Leffler 		 *    turn off timers
1673c42a7b7eSSam Leffler 		 *    disable interrupts
1674c42a7b7eSSam Leffler 		 *    turn off the radio
16755591b213SSam Leffler 		 *    clear transmit machinery
16765591b213SSam Leffler 		 *    clear receive machinery
16775591b213SSam Leffler 		 *    drain and release tx queues
16785591b213SSam Leffler 		 *    reclaim beacon resources
16795591b213SSam Leffler 		 *    power down hardware
16805591b213SSam Leffler 		 *
16815591b213SSam Leffler 		 * Note that some of this work is not possible if the
16825591b213SSam Leffler 		 * hardware is gone (invalid).
16835591b213SSam Leffler 		 */
168486e07743SSam Leffler #ifdef ATH_TX99_DIAG
168586e07743SSam Leffler 		if (sc->sc_tx99 != NULL)
168686e07743SSam Leffler 			sc->sc_tx99->stop(sc->sc_tx99);
168786e07743SSam Leffler #endif
16882e986da5SSam Leffler 		callout_stop(&sc->sc_wd_ch);
16892e986da5SSam Leffler 		sc->sc_wd_timer = 0;
169013f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1691c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
16923e50ec2cSSam Leffler 			if (sc->sc_softled) {
16933e50ec2cSSam Leffler 				callout_stop(&sc->sc_ledtimer);
16943e50ec2cSSam Leffler 				ath_hal_gpioset(ah, sc->sc_ledpin,
16953e50ec2cSSam Leffler 					!sc->sc_ledon);
16963e50ec2cSSam Leffler 				sc->sc_blinking = 0;
16973e50ec2cSSam Leffler 			}
16985591b213SSam Leffler 			ath_hal_intrset(ah, 0);
1699c42a7b7eSSam Leffler 		}
17005591b213SSam Leffler 		ath_draintxq(sc);
1701c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
17025591b213SSam Leffler 			ath_stoprecv(sc);
1703c42a7b7eSSam Leffler 			ath_hal_phydisable(ah);
1704c42a7b7eSSam Leffler 		} else
17055591b213SSam Leffler 			sc->sc_rxlink = NULL;
1706b032f27cSSam Leffler 		ath_beacon_free(sc);	/* XXX not needed */
1707c42a7b7eSSam Leffler 	}
1708c42a7b7eSSam Leffler }
1709c42a7b7eSSam Leffler 
1710c42a7b7eSSam Leffler static void
1711c42a7b7eSSam Leffler ath_stop(struct ifnet *ifp)
1712c42a7b7eSSam Leffler {
1713c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
1714c42a7b7eSSam Leffler 
1715c42a7b7eSSam Leffler 	ATH_LOCK(sc);
1716c42a7b7eSSam Leffler 	ath_stop_locked(ifp);
1717f0b2a0beSSam Leffler 	ATH_UNLOCK(sc);
17185591b213SSam Leffler }
17195591b213SSam Leffler 
17205591b213SSam Leffler /*
17215591b213SSam Leffler  * Reset the hardware w/o losing operational state.  This is
17225591b213SSam Leffler  * basically a more efficient way of doing ath_stop, ath_init,
17235591b213SSam Leffler  * followed by state transitions to the current 802.11
1724c42a7b7eSSam Leffler  * operational state.  Used to recover from various errors and
1725c42a7b7eSSam Leffler  * to reset or reload hardware state.
17265591b213SSam Leffler  */
17276079fdbeSAdrian Chadd int
1728c42a7b7eSSam Leffler ath_reset(struct ifnet *ifp)
17295591b213SSam Leffler {
1730c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
1731b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
17325591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
17335591b213SSam Leffler 	HAL_STATUS status;
17345591b213SSam Leffler 
17355591b213SSam Leffler 	ath_hal_intrset(ah, 0);		/* disable interrupts */
17365591b213SSam Leffler 	ath_draintxq(sc);		/* stop xmit side */
17375591b213SSam Leffler 	ath_stoprecv(sc);		/* stop recv side */
1738b032f27cSSam Leffler 	ath_settkipmic(sc);		/* configure TKIP MIC handling */
17395591b213SSam Leffler 	/* NB: indicate channel change so we do a full reset */
174059efa8b5SSam Leffler 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_TRUE, &status))
17415591b213SSam Leffler 		if_printf(ifp, "%s: unable to reset hardware; hal status %u\n",
17425591b213SSam Leffler 			__func__, status);
1743c59005e9SSam Leffler 	sc->sc_diversity = ath_hal_getdiversity(ah);
174448237774SAdrian Chadd 
174548237774SAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
174648237774SAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
174748237774SAdrian Chadd 
174868e8e04eSSam Leffler 	if (ath_startrecv(sc) != 0)	/* restart recv */
174968e8e04eSSam Leffler 		if_printf(ifp, "%s: unable to start recv logic\n", __func__);
1750c42a7b7eSSam Leffler 	/*
1751c42a7b7eSSam Leffler 	 * We may be doing a reset in response to an ioctl
1752c42a7b7eSSam Leffler 	 * that changes the channel so update any state that
1753c42a7b7eSSam Leffler 	 * might change as a result.
1754c42a7b7eSSam Leffler 	 */
1755724c193aSSam Leffler 	ath_chan_change(sc, ic->ic_curchan);
1756c89b957aSSam Leffler 	if (sc->sc_beacons) {		/* restart beacons */
1757584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
175810ad9a77SSam Leffler 		if (sc->sc_tdma)
175910ad9a77SSam Leffler 			ath_tdma_config(sc, NULL);
176010ad9a77SSam Leffler 		else
176110ad9a77SSam Leffler #endif
1762c89b957aSSam Leffler 			ath_beacon_config(sc, NULL);
176310ad9a77SSam Leffler 	}
1764c42a7b7eSSam Leffler 	ath_hal_intrset(ah, sc->sc_imask);
1765c42a7b7eSSam Leffler 
1766c42a7b7eSSam Leffler 	ath_start(ifp);			/* restart xmit */
1767c42a7b7eSSam Leffler 	return 0;
17685591b213SSam Leffler }
17695591b213SSam Leffler 
177068e8e04eSSam Leffler static int
1771b032f27cSSam Leffler ath_reset_vap(struct ieee80211vap *vap, u_long cmd)
1772b032f27cSSam Leffler {
17734b54a231SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
17744b54a231SSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
17754b54a231SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
17764b54a231SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
17774b54a231SSam Leffler 
17784b54a231SSam Leffler 	switch (cmd) {
17794b54a231SSam Leffler 	case IEEE80211_IOC_TXPOWER:
17804b54a231SSam Leffler 		/*
17814b54a231SSam Leffler 		 * If per-packet TPC is enabled, then we have nothing
17824b54a231SSam Leffler 		 * to do; otherwise we need to force the global limit.
17834b54a231SSam Leffler 		 * All this can happen directly; no need to reset.
17844b54a231SSam Leffler 		 */
17854b54a231SSam Leffler 		if (!ath_hal_gettpc(ah))
17864b54a231SSam Leffler 			ath_hal_settxpowlimit(ah, ic->ic_txpowlimit);
17874b54a231SSam Leffler 		return 0;
17884b54a231SSam Leffler 	}
17894b54a231SSam Leffler 	return ath_reset(ifp);
1790b032f27cSSam Leffler }
1791b032f27cSSam Leffler 
1792b8e788a5SAdrian Chadd struct ath_buf *
179310ad9a77SSam Leffler _ath_getbuf_locked(struct ath_softc *sc)
179410ad9a77SSam Leffler {
179510ad9a77SSam Leffler 	struct ath_buf *bf;
179610ad9a77SSam Leffler 
179710ad9a77SSam Leffler 	ATH_TXBUF_LOCK_ASSERT(sc);
179810ad9a77SSam Leffler 
1799*6b349e5aSAdrian Chadd 	bf = TAILQ_FIRST(&sc->sc_txbuf);
180010ad9a77SSam Leffler 	if (bf != NULL && (bf->bf_flags & ATH_BUF_BUSY) == 0)
1801*6b349e5aSAdrian Chadd 		TAILQ_REMOVE(&sc->sc_txbuf, bf, bf_list);
180210ad9a77SSam Leffler 	else
180310ad9a77SSam Leffler 		bf = NULL;
180410ad9a77SSam Leffler 	if (bf == NULL) {
180510ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: %s\n", __func__,
1806*6b349e5aSAdrian Chadd 		    TAILQ_FIRST(&sc->sc_txbuf) == NULL ?
180710ad9a77SSam Leffler 			"out of xmit buffers" : "xmit buffer busy");
180810ad9a77SSam Leffler 	}
180910ad9a77SSam Leffler 	return bf;
181010ad9a77SSam Leffler }
181110ad9a77SSam Leffler 
1812b8e788a5SAdrian Chadd struct ath_buf *
181310ad9a77SSam Leffler ath_getbuf(struct ath_softc *sc)
181410ad9a77SSam Leffler {
181510ad9a77SSam Leffler 	struct ath_buf *bf;
181610ad9a77SSam Leffler 
181710ad9a77SSam Leffler 	ATH_TXBUF_LOCK(sc);
181810ad9a77SSam Leffler 	bf = _ath_getbuf_locked(sc);
181910ad9a77SSam Leffler 	if (bf == NULL) {
182010ad9a77SSam Leffler 		struct ifnet *ifp = sc->sc_ifp;
182110ad9a77SSam Leffler 
182210ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: stop queue\n", __func__);
182310ad9a77SSam Leffler 		sc->sc_stats.ast_tx_qstop++;
182410ad9a77SSam Leffler 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
182510ad9a77SSam Leffler 	}
182610ad9a77SSam Leffler 	ATH_TXBUF_UNLOCK(sc);
182710ad9a77SSam Leffler 	return bf;
182810ad9a77SSam Leffler }
182910ad9a77SSam Leffler 
18305591b213SSam Leffler static void
18315591b213SSam Leffler ath_start(struct ifnet *ifp)
18325591b213SSam Leffler {
18335591b213SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
18345591b213SSam Leffler 	struct ieee80211_node *ni;
18355591b213SSam Leffler 	struct ath_buf *bf;
183668e8e04eSSam Leffler 	struct mbuf *m, *next;
183768e8e04eSSam Leffler 	ath_bufhead frags;
18385591b213SSam Leffler 
183913f4c340SRobert Watson 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->sc_invalid)
18405591b213SSam Leffler 		return;
18415591b213SSam Leffler 	for (;;) {
18425591b213SSam Leffler 		/*
18435591b213SSam Leffler 		 * Grab a TX buffer and associated resources.
18445591b213SSam Leffler 		 */
184510ad9a77SSam Leffler 		bf = ath_getbuf(sc);
184610ad9a77SSam Leffler 		if (bf == NULL)
18475591b213SSam Leffler 			break;
18482b9411e2SSam Leffler 
1849b032f27cSSam Leffler 		IFQ_DEQUEUE(&ifp->if_snd, m);
1850b032f27cSSam Leffler 		if (m == NULL) {
1851b032f27cSSam Leffler 			ATH_TXBUF_LOCK(sc);
1852*6b349e5aSAdrian Chadd 			TAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
1853b032f27cSSam Leffler 			ATH_TXBUF_UNLOCK(sc);
1854b032f27cSSam Leffler 			break;
1855b032f27cSSam Leffler 		}
1856b032f27cSSam Leffler 		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
185768e8e04eSSam Leffler 		/*
185868e8e04eSSam Leffler 		 * Check for fragmentation.  If this frame
185968e8e04eSSam Leffler 		 * has been broken up verify we have enough
186068e8e04eSSam Leffler 		 * buffers to send all the fragments so all
186168e8e04eSSam Leffler 		 * go out or none...
186268e8e04eSSam Leffler 		 */
1863*6b349e5aSAdrian Chadd 		TAILQ_INIT(&frags);
186468e8e04eSSam Leffler 		if ((m->m_flags & M_FRAG) &&
186568e8e04eSSam Leffler 		    !ath_txfrag_setup(sc, &frags, m, ni)) {
186668e8e04eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_XMIT,
186768e8e04eSSam Leffler 			    "%s: out of txfrag buffers\n", __func__);
186836c6be9aSSam Leffler 			sc->sc_stats.ast_tx_nofrag++;
18699cb93076SSam Leffler 			ifp->if_oerrors++;
187068e8e04eSSam Leffler 			ath_freetx(m);
187168e8e04eSSam Leffler 			goto bad;
187268e8e04eSSam Leffler 		}
1873339ccfb3SSam Leffler 		ifp->if_opackets++;
187468e8e04eSSam Leffler 	nextfrag:
187568e8e04eSSam Leffler 		/*
187668e8e04eSSam Leffler 		 * Pass the frame to the h/w for transmission.
187768e8e04eSSam Leffler 		 * Fragmented frames have each frag chained together
187868e8e04eSSam Leffler 		 * with m_nextpkt.  We know there are sufficient ath_buf's
187968e8e04eSSam Leffler 		 * to send all the frags because of work done by
188068e8e04eSSam Leffler 		 * ath_txfrag_setup.  We leave m_nextpkt set while
188168e8e04eSSam Leffler 		 * calling ath_tx_start so it can use it to extend the
188268e8e04eSSam Leffler 		 * the tx duration to cover the subsequent frag and
188368e8e04eSSam Leffler 		 * so it can reclaim all the mbufs in case of an error;
188468e8e04eSSam Leffler 		 * ath_tx_start clears m_nextpkt once it commits to
188568e8e04eSSam Leffler 		 * handing the frame to the hardware.
188668e8e04eSSam Leffler 		 */
188768e8e04eSSam Leffler 		next = m->m_nextpkt;
18885591b213SSam Leffler 		if (ath_tx_start(sc, ni, bf, m)) {
18895591b213SSam Leffler 	bad:
18905591b213SSam Leffler 			ifp->if_oerrors++;
1891c42a7b7eSSam Leffler 	reclaim:
189268e8e04eSSam Leffler 			bf->bf_m = NULL;
189368e8e04eSSam Leffler 			bf->bf_node = NULL;
1894c42a7b7eSSam Leffler 			ATH_TXBUF_LOCK(sc);
1895*6b349e5aSAdrian Chadd 			TAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
189668e8e04eSSam Leffler 			ath_txfrag_cleanup(sc, &frags, ni);
1897c42a7b7eSSam Leffler 			ATH_TXBUF_UNLOCK(sc);
1898c42a7b7eSSam Leffler 			if (ni != NULL)
1899c42a7b7eSSam Leffler 				ieee80211_free_node(ni);
19005591b213SSam Leffler 			continue;
19015591b213SSam Leffler 		}
190268e8e04eSSam Leffler 		if (next != NULL) {
190368e8e04eSSam Leffler 			/*
190468e8e04eSSam Leffler 			 * Beware of state changing between frags.
190568e8e04eSSam Leffler 			 * XXX check sta power-save state?
190668e8e04eSSam Leffler 			 */
1907b032f27cSSam Leffler 			if (ni->ni_vap->iv_state != IEEE80211_S_RUN) {
190868e8e04eSSam Leffler 				DPRINTF(sc, ATH_DEBUG_XMIT,
190968e8e04eSSam Leffler 				    "%s: flush fragmented packet, state %s\n",
191068e8e04eSSam Leffler 				    __func__,
1911b032f27cSSam Leffler 				    ieee80211_state_name[ni->ni_vap->iv_state]);
191268e8e04eSSam Leffler 				ath_freetx(next);
191368e8e04eSSam Leffler 				goto reclaim;
191468e8e04eSSam Leffler 			}
191568e8e04eSSam Leffler 			m = next;
1916*6b349e5aSAdrian Chadd 			bf = TAILQ_FIRST(&frags);
191768e8e04eSSam Leffler 			KASSERT(bf != NULL, ("no buf for txfrag"));
1918*6b349e5aSAdrian Chadd 			TAILQ_REMOVE(&frags, bf, bf_list);
191968e8e04eSSam Leffler 			goto nextfrag;
192068e8e04eSSam Leffler 		}
19215591b213SSam Leffler 
19222e986da5SSam Leffler 		sc->sc_wd_timer = 5;
19235591b213SSam Leffler 	}
19245591b213SSam Leffler }
19255591b213SSam Leffler 
19265591b213SSam Leffler static int
19275591b213SSam Leffler ath_media_change(struct ifnet *ifp)
19285591b213SSam Leffler {
1929b032f27cSSam Leffler 	int error = ieee80211_media_change(ifp);
1930b032f27cSSam Leffler 	/* NB: only the fixed rate can change and that doesn't need a reset */
1931b032f27cSSam Leffler 	return (error == ENETRESET ? 0 : error);
19325591b213SSam Leffler }
19335591b213SSam Leffler 
1934c42a7b7eSSam Leffler /*
1935c42a7b7eSSam Leffler  * Block/unblock tx+rx processing while a key change is done.
1936c42a7b7eSSam Leffler  * We assume the caller serializes key management operations
1937c42a7b7eSSam Leffler  * so we only need to worry about synchronization with other
1938c42a7b7eSSam Leffler  * uses that originate in the driver.
1939c42a7b7eSSam Leffler  */
1940c42a7b7eSSam Leffler static void
1941b032f27cSSam Leffler ath_key_update_begin(struct ieee80211vap *vap)
1942c42a7b7eSSam Leffler {
1943b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ic->ic_ifp;
1944c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
1945c42a7b7eSSam Leffler 
1946c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
1947b032f27cSSam Leffler 	taskqueue_block(sc->sc_tq);
1948c42a7b7eSSam Leffler 	IF_LOCK(&ifp->if_snd);		/* NB: doesn't block mgmt frames */
1949c42a7b7eSSam Leffler }
1950c42a7b7eSSam Leffler 
1951c42a7b7eSSam Leffler static void
1952b032f27cSSam Leffler ath_key_update_end(struct ieee80211vap *vap)
1953c42a7b7eSSam Leffler {
1954b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ic->ic_ifp;
1955c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
1956c42a7b7eSSam Leffler 
1957c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
1958c42a7b7eSSam Leffler 	IF_UNLOCK(&ifp->if_snd);
1959b032f27cSSam Leffler 	taskqueue_unblock(sc->sc_tq);
1960c42a7b7eSSam Leffler }
19615591b213SSam Leffler 
19624bc0e754SSam Leffler /*
19634bc0e754SSam Leffler  * Calculate the receive filter according to the
19644bc0e754SSam Leffler  * operating mode and state:
19654bc0e754SSam Leffler  *
19664bc0e754SSam Leffler  * o always accept unicast, broadcast, and multicast traffic
1967b032f27cSSam Leffler  * o accept PHY error frames when hardware doesn't have MIB support
1968411373ebSSam Leffler  *   to count and we need them for ANI (sta mode only until recently)
1969b032f27cSSam Leffler  *   and we are not scanning (ANI is disabled)
1970411373ebSSam Leffler  *   NB: older hal's add rx filter bits out of sight and we need to
1971411373ebSSam Leffler  *	 blindly preserve them
19724bc0e754SSam Leffler  * o probe request frames are accepted only when operating in
197359aa14a9SRui Paulo  *   hostap, adhoc, mesh, or monitor modes
1974b032f27cSSam Leffler  * o enable promiscuous mode
1975b032f27cSSam Leffler  *   - when in monitor mode
1976b032f27cSSam Leffler  *   - if interface marked PROMISC (assumes bridge setting is filtered)
19774bc0e754SSam Leffler  * o accept beacons:
19784bc0e754SSam Leffler  *   - when operating in station mode for collecting rssi data when
19794bc0e754SSam Leffler  *     the station is otherwise quiet, or
1980b032f27cSSam Leffler  *   - when operating in adhoc mode so the 802.11 layer creates
1981b032f27cSSam Leffler  *     node table entries for peers,
19824bc0e754SSam Leffler  *   - when scanning
1983b032f27cSSam Leffler  *   - when doing s/w beacon miss (e.g. for ap+sta)
1984b032f27cSSam Leffler  *   - when operating in ap mode in 11g to detect overlapping bss that
1985b032f27cSSam Leffler  *     require protection
198659aa14a9SRui Paulo  *   - when operating in mesh mode to detect neighbors
19876f48c956SSam Leffler  * o accept control frames:
19886f48c956SSam Leffler  *   - when in monitor mode
1989b032f27cSSam Leffler  * XXX HT protection for 11n
19904bc0e754SSam Leffler  */
19914bc0e754SSam Leffler static u_int32_t
199268e8e04eSSam Leffler ath_calcrxfilter(struct ath_softc *sc)
19934bc0e754SSam Leffler {
1994fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
1995b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
19964bc0e754SSam Leffler 	u_int32_t rfilt;
19974bc0e754SSam Leffler 
1998b032f27cSSam Leffler 	rfilt = HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST;
1999411373ebSSam Leffler 	if (!sc->sc_needmib && !sc->sc_scanning)
2000411373ebSSam Leffler 		rfilt |= HAL_RX_FILTER_PHYERR;
20014bc0e754SSam Leffler 	if (ic->ic_opmode != IEEE80211_M_STA)
20024bc0e754SSam Leffler 		rfilt |= HAL_RX_FILTER_PROBEREQ;
20035463c4a4SSam Leffler 	/* XXX ic->ic_monvaps != 0? */
2004b032f27cSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_MONITOR || (ifp->if_flags & IFF_PROMISC))
20054bc0e754SSam Leffler 		rfilt |= HAL_RX_FILTER_PROM;
20064bc0e754SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA ||
200747db982fSSam Leffler 	    ic->ic_opmode == IEEE80211_M_IBSS ||
2008b032f27cSSam Leffler 	    sc->sc_swbmiss || sc->sc_scanning)
2009b032f27cSSam Leffler 		rfilt |= HAL_RX_FILTER_BEACON;
2010b032f27cSSam Leffler 	/*
2011b032f27cSSam Leffler 	 * NB: We don't recalculate the rx filter when
2012b032f27cSSam Leffler 	 * ic_protmode changes; otherwise we could do
2013b032f27cSSam Leffler 	 * this only when ic_protmode != NONE.
2014b032f27cSSam Leffler 	 */
2015b032f27cSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
2016b032f27cSSam Leffler 	    IEEE80211_IS_CHAN_ANYG(ic->ic_curchan))
20174bc0e754SSam Leffler 		rfilt |= HAL_RX_FILTER_BEACON;
2018f378d4c8SAdrian Chadd 
2019f378d4c8SAdrian Chadd 	/*
20204aa18e9dSAdrian Chadd 	 * Enable hardware PS-POLL RX only for hostap mode;
2021f378d4c8SAdrian Chadd 	 * STA mode sends PS-POLL frames but never
20224aa18e9dSAdrian Chadd 	 * receives them.
2023f378d4c8SAdrian Chadd 	 */
2024dce0bccaSAdrian Chadd 	if (ath_hal_getcapability(sc->sc_ah, HAL_CAP_PSPOLL,
2025f378d4c8SAdrian Chadd 	    0, NULL) == HAL_OK &&
2026f378d4c8SAdrian Chadd 	    ic->ic_opmode == IEEE80211_M_HOSTAP)
2027f378d4c8SAdrian Chadd 		rfilt |= HAL_RX_FILTER_PSPOLL;
2028f378d4c8SAdrian Chadd 
2029fe0dd789SSam Leffler 	if (sc->sc_nmeshvaps) {
203059aa14a9SRui Paulo 		rfilt |= HAL_RX_FILTER_BEACON;
203159aa14a9SRui Paulo 		if (sc->sc_hasbmatch)
203259aa14a9SRui Paulo 			rfilt |= HAL_RX_FILTER_BSSID;
203359aa14a9SRui Paulo 		else
203459aa14a9SRui Paulo 			rfilt |= HAL_RX_FILTER_PROM;
203559aa14a9SRui Paulo 	}
20366f48c956SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
20376f48c956SSam Leffler 		rfilt |= HAL_RX_FILTER_CONTROL;
2038f378d4c8SAdrian Chadd 
2039f378d4c8SAdrian Chadd 	/*
2040f378d4c8SAdrian Chadd 	 * Enable RX of compressed BAR frames only when doing
2041f378d4c8SAdrian Chadd 	 * 802.11n. Required for A-MPDU.
2042f378d4c8SAdrian Chadd 	 */
2043a83df4d3SAdrian Chadd 	if (IEEE80211_IS_CHAN_HT(ic->ic_curchan))
2044a83df4d3SAdrian Chadd 		rfilt |= HAL_RX_FILTER_COMPBAR;
2045f378d4c8SAdrian Chadd 
2046b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x, %s if_flags 0x%x\n",
2047b032f27cSSam Leffler 	    __func__, rfilt, ieee80211_opmode_name[ic->ic_opmode], ifp->if_flags);
20484bc0e754SSam Leffler 	return rfilt;
2049b032f27cSSam Leffler }
2050b032f27cSSam Leffler 
2051b032f27cSSam Leffler static void
2052b032f27cSSam Leffler ath_update_promisc(struct ifnet *ifp)
2053b032f27cSSam Leffler {
2054b032f27cSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2055b032f27cSSam Leffler 	u_int32_t rfilt;
2056b032f27cSSam Leffler 
2057b032f27cSSam Leffler 	/* configure rx filter */
2058b032f27cSSam Leffler 	rfilt = ath_calcrxfilter(sc);
2059b032f27cSSam Leffler 	ath_hal_setrxfilter(sc->sc_ah, rfilt);
2060b032f27cSSam Leffler 
2061b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x\n", __func__, rfilt);
2062b032f27cSSam Leffler }
2063b032f27cSSam Leffler 
2064b032f27cSSam Leffler static void
2065b032f27cSSam Leffler ath_update_mcast(struct ifnet *ifp)
2066b032f27cSSam Leffler {
2067b032f27cSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2068b032f27cSSam Leffler 	u_int32_t mfilt[2];
2069b032f27cSSam Leffler 
2070b032f27cSSam Leffler 	/* calculate and install multicast filter */
2071b032f27cSSam Leffler 	if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
2072b032f27cSSam Leffler 		struct ifmultiaddr *ifma;
2073b032f27cSSam Leffler 		/*
2074b032f27cSSam Leffler 		 * Merge multicast addresses to form the hardware filter.
2075b032f27cSSam Leffler 		 */
2076b032f27cSSam Leffler 		mfilt[0] = mfilt[1] = 0;
2077eb956cd0SRobert Watson 		if_maddr_rlock(ifp);	/* XXX need some fiddling to remove? */
2078b032f27cSSam Leffler 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2079b032f27cSSam Leffler 			caddr_t dl;
2080b032f27cSSam Leffler 			u_int32_t val;
2081b032f27cSSam Leffler 			u_int8_t pos;
2082b032f27cSSam Leffler 
2083b032f27cSSam Leffler 			/* calculate XOR of eight 6bit values */
2084b032f27cSSam Leffler 			dl = LLADDR((struct sockaddr_dl *) ifma->ifma_addr);
2085b032f27cSSam Leffler 			val = LE_READ_4(dl + 0);
2086b032f27cSSam Leffler 			pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
2087b032f27cSSam Leffler 			val = LE_READ_4(dl + 3);
2088b032f27cSSam Leffler 			pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
2089b032f27cSSam Leffler 			pos &= 0x3f;
2090b032f27cSSam Leffler 			mfilt[pos / 32] |= (1 << (pos % 32));
2091b032f27cSSam Leffler 		}
2092eb956cd0SRobert Watson 		if_maddr_runlock(ifp);
2093b032f27cSSam Leffler 	} else
2094b032f27cSSam Leffler 		mfilt[0] = mfilt[1] = ~0;
2095b032f27cSSam Leffler 	ath_hal_setmcastfilter(sc->sc_ah, mfilt[0], mfilt[1]);
2096b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: MC filter %08x:%08x\n",
2097b032f27cSSam Leffler 		__func__, mfilt[0], mfilt[1]);
20984bc0e754SSam Leffler }
20994bc0e754SSam Leffler 
21005591b213SSam Leffler static void
21015591b213SSam Leffler ath_mode_init(struct ath_softc *sc)
21025591b213SSam Leffler {
2103fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
2104b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2105b032f27cSSam Leffler 	u_int32_t rfilt;
21065591b213SSam Leffler 
21074bc0e754SSam Leffler 	/* configure rx filter */
210868e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
21094bc0e754SSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
21104bc0e754SSam Leffler 
21115591b213SSam Leffler 	/* configure operational mode */
2112c42a7b7eSSam Leffler 	ath_hal_setopmode(ah);
2113c42a7b7eSSam Leffler 
211429aca940SSam Leffler 	/* handle any link-level address change */
211529aca940SSam Leffler 	ath_hal_setmac(ah, IF_LLADDR(ifp));
21165591b213SSam Leffler 
21175591b213SSam Leffler 	/* calculate and install multicast filter */
2118b032f27cSSam Leffler 	ath_update_mcast(ifp);
21195591b213SSam Leffler }
21205591b213SSam Leffler 
2121c42a7b7eSSam Leffler /*
2122c42a7b7eSSam Leffler  * Set the slot time based on the current setting.
2123c42a7b7eSSam Leffler  */
2124c42a7b7eSSam Leffler static void
2125c42a7b7eSSam Leffler ath_setslottime(struct ath_softc *sc)
2126c42a7b7eSSam Leffler {
2127b032f27cSSam Leffler 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2128c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2129aaa70f2fSSam Leffler 	u_int usec;
2130c42a7b7eSSam Leffler 
2131aaa70f2fSSam Leffler 	if (IEEE80211_IS_CHAN_HALF(ic->ic_curchan))
2132aaa70f2fSSam Leffler 		usec = 13;
2133aaa70f2fSSam Leffler 	else if (IEEE80211_IS_CHAN_QUARTER(ic->ic_curchan))
2134aaa70f2fSSam Leffler 		usec = 21;
2135724c193aSSam Leffler 	else if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan)) {
2136724c193aSSam Leffler 		/* honor short/long slot time only in 11g */
2137724c193aSSam Leffler 		/* XXX shouldn't honor on pure g or turbo g channel */
2138724c193aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
2139aaa70f2fSSam Leffler 			usec = HAL_SLOT_TIME_9;
2140aaa70f2fSSam Leffler 		else
2141aaa70f2fSSam Leffler 			usec = HAL_SLOT_TIME_20;
2142724c193aSSam Leffler 	} else
2143724c193aSSam Leffler 		usec = HAL_SLOT_TIME_9;
2144aaa70f2fSSam Leffler 
2145aaa70f2fSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET,
2146aaa70f2fSSam Leffler 	    "%s: chan %u MHz flags 0x%x %s slot, %u usec\n",
2147aaa70f2fSSam Leffler 	    __func__, ic->ic_curchan->ic_freq, ic->ic_curchan->ic_flags,
2148aaa70f2fSSam Leffler 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", usec);
2149aaa70f2fSSam Leffler 
2150aaa70f2fSSam Leffler 	ath_hal_setslottime(ah, usec);
2151c42a7b7eSSam Leffler 	sc->sc_updateslot = OK;
2152c42a7b7eSSam Leffler }
2153c42a7b7eSSam Leffler 
2154c42a7b7eSSam Leffler /*
2155c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update the
2156c42a7b7eSSam Leffler  * slot time based on the current setting.
2157c42a7b7eSSam Leffler  */
2158c42a7b7eSSam Leffler static void
2159c42a7b7eSSam Leffler ath_updateslot(struct ifnet *ifp)
2160c42a7b7eSSam Leffler {
2161c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2162b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
2163c42a7b7eSSam Leffler 
2164c42a7b7eSSam Leffler 	/*
2165c42a7b7eSSam Leffler 	 * When not coordinating the BSS, change the hardware
2166c42a7b7eSSam Leffler 	 * immediately.  For other operation we defer the change
2167c42a7b7eSSam Leffler 	 * until beacon updates have propagated to the stations.
2168c42a7b7eSSam Leffler 	 */
216959aa14a9SRui Paulo 	if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
217059aa14a9SRui Paulo 	    ic->ic_opmode == IEEE80211_M_MBSS)
2171c42a7b7eSSam Leffler 		sc->sc_updateslot = UPDATE;
2172c42a7b7eSSam Leffler 	else
2173c42a7b7eSSam Leffler 		ath_setslottime(sc);
2174c42a7b7eSSam Leffler }
2175c42a7b7eSSam Leffler 
2176c42a7b7eSSam Leffler /*
217780d2765fSSam Leffler  * Setup a h/w transmit queue for beacons.
217880d2765fSSam Leffler  */
217980d2765fSSam Leffler static int
218080d2765fSSam Leffler ath_beaconq_setup(struct ath_hal *ah)
218180d2765fSSam Leffler {
218280d2765fSSam Leffler 	HAL_TXQ_INFO qi;
218380d2765fSSam Leffler 
218480d2765fSSam Leffler 	memset(&qi, 0, sizeof(qi));
218580d2765fSSam Leffler 	qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
218680d2765fSSam Leffler 	qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
218780d2765fSSam Leffler 	qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
21880f2e86fbSSam Leffler 	/* NB: for dynamic turbo, don't enable any other interrupts */
2189bd5a9920SSam Leffler 	qi.tqi_qflags = HAL_TXQ_TXDESCINT_ENABLE;
219080d2765fSSam Leffler 	return ath_hal_setuptxqueue(ah, HAL_TX_QUEUE_BEACON, &qi);
219180d2765fSSam Leffler }
219280d2765fSSam Leffler 
219380d2765fSSam Leffler /*
21940f2e86fbSSam Leffler  * Setup the transmit queue parameters for the beacon queue.
21950f2e86fbSSam Leffler  */
21960f2e86fbSSam Leffler static int
21970f2e86fbSSam Leffler ath_beaconq_config(struct ath_softc *sc)
21980f2e86fbSSam Leffler {
21990f2e86fbSSam Leffler #define	ATH_EXPONENT_TO_VALUE(v)	((1<<(v))-1)
2200b032f27cSSam Leffler 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
22010f2e86fbSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
22020f2e86fbSSam Leffler 	HAL_TXQ_INFO qi;
22030f2e86fbSSam Leffler 
22040f2e86fbSSam Leffler 	ath_hal_gettxqueueprops(ah, sc->sc_bhalq, &qi);
220559aa14a9SRui Paulo 	if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
220659aa14a9SRui Paulo 	    ic->ic_opmode == IEEE80211_M_MBSS) {
22070f2e86fbSSam Leffler 		/*
22080f2e86fbSSam Leffler 		 * Always burst out beacon and CAB traffic.
22090f2e86fbSSam Leffler 		 */
22100f2e86fbSSam Leffler 		qi.tqi_aifs = ATH_BEACON_AIFS_DEFAULT;
22110f2e86fbSSam Leffler 		qi.tqi_cwmin = ATH_BEACON_CWMIN_DEFAULT;
22120f2e86fbSSam Leffler 		qi.tqi_cwmax = ATH_BEACON_CWMAX_DEFAULT;
22130f2e86fbSSam Leffler 	} else {
22140f2e86fbSSam Leffler 		struct wmeParams *wmep =
22150f2e86fbSSam Leffler 			&ic->ic_wme.wme_chanParams.cap_wmeParams[WME_AC_BE];
22160f2e86fbSSam Leffler 		/*
22170f2e86fbSSam Leffler 		 * Adhoc mode; important thing is to use 2x cwmin.
22180f2e86fbSSam Leffler 		 */
22190f2e86fbSSam Leffler 		qi.tqi_aifs = wmep->wmep_aifsn;
22200f2e86fbSSam Leffler 		qi.tqi_cwmin = 2*ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
22210f2e86fbSSam Leffler 		qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
22220f2e86fbSSam Leffler 	}
22230f2e86fbSSam Leffler 
22240f2e86fbSSam Leffler 	if (!ath_hal_settxqueueprops(ah, sc->sc_bhalq, &qi)) {
22250f2e86fbSSam Leffler 		device_printf(sc->sc_dev, "unable to update parameters for "
22260f2e86fbSSam Leffler 			"beacon hardware queue!\n");
22270f2e86fbSSam Leffler 		return 0;
22280f2e86fbSSam Leffler 	} else {
22290f2e86fbSSam Leffler 		ath_hal_resettxqueue(ah, sc->sc_bhalq); /* push to h/w */
22300f2e86fbSSam Leffler 		return 1;
22310f2e86fbSSam Leffler 	}
22320f2e86fbSSam Leffler #undef ATH_EXPONENT_TO_VALUE
22330f2e86fbSSam Leffler }
22340f2e86fbSSam Leffler 
22350f2e86fbSSam Leffler /*
2236c42a7b7eSSam Leffler  * Allocate and setup an initial beacon frame.
2237c42a7b7eSSam Leffler  */
22385591b213SSam Leffler static int
22395591b213SSam Leffler ath_beacon_alloc(struct ath_softc *sc, struct ieee80211_node *ni)
22405591b213SSam Leffler {
2241b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
2242b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
22435591b213SSam Leffler 	struct ath_buf *bf;
22445591b213SSam Leffler 	struct mbuf *m;
2245c42a7b7eSSam Leffler 	int error;
22465591b213SSam Leffler 
2247b032f27cSSam Leffler 	bf = avp->av_bcbuf;
2248b032f27cSSam Leffler 	if (bf->bf_m != NULL) {
2249b032f27cSSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
2250b032f27cSSam Leffler 		m_freem(bf->bf_m);
2251b032f27cSSam Leffler 		bf->bf_m = NULL;
2252c42a7b7eSSam Leffler 	}
2253b032f27cSSam Leffler 	if (bf->bf_node != NULL) {
2254b032f27cSSam Leffler 		ieee80211_free_node(bf->bf_node);
2255b032f27cSSam Leffler 		bf->bf_node = NULL;
2256b032f27cSSam Leffler 	}
2257b032f27cSSam Leffler 
22585591b213SSam Leffler 	/*
22595591b213SSam Leffler 	 * NB: the beacon data buffer must be 32-bit aligned;
22605591b213SSam Leffler 	 * we assume the mbuf routines will return us something
22615591b213SSam Leffler 	 * with this alignment (perhaps should assert).
22625591b213SSam Leffler 	 */
2263b032f27cSSam Leffler 	m = ieee80211_beacon_alloc(ni, &avp->av_boff);
22645591b213SSam Leffler 	if (m == NULL) {
2265b032f27cSSam Leffler 		device_printf(sc->sc_dev, "%s: cannot get mbuf\n", __func__);
22665591b213SSam Leffler 		sc->sc_stats.ast_be_nombuf++;
22675591b213SSam Leffler 		return ENOMEM;
22685591b213SSam Leffler 	}
2269f9e6219bSSam Leffler 	error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m,
2270f9e6219bSSam Leffler 				     bf->bf_segs, &bf->bf_nseg,
22715591b213SSam Leffler 				     BUS_DMA_NOWAIT);
2272b032f27cSSam Leffler 	if (error != 0) {
2273b032f27cSSam Leffler 		device_printf(sc->sc_dev,
2274b032f27cSSam Leffler 		    "%s: cannot map mbuf, bus_dmamap_load_mbuf_sg returns %d\n",
2275b032f27cSSam Leffler 		    __func__, error);
2276b032f27cSSam Leffler 		m_freem(m);
2277b032f27cSSam Leffler 		return error;
2278b032f27cSSam Leffler 	}
2279b032f27cSSam Leffler 
2280b032f27cSSam Leffler 	/*
2281b032f27cSSam Leffler 	 * Calculate a TSF adjustment factor required for staggered
2282b032f27cSSam Leffler 	 * beacons.  Note that we assume the format of the beacon
2283b032f27cSSam Leffler 	 * frame leaves the tstamp field immediately following the
2284b032f27cSSam Leffler 	 * header.
2285b032f27cSSam Leffler 	 */
2286b032f27cSSam Leffler 	if (sc->sc_stagbeacons && avp->av_bslot > 0) {
2287b032f27cSSam Leffler 		uint64_t tsfadjust;
2288b032f27cSSam Leffler 		struct ieee80211_frame *wh;
2289b032f27cSSam Leffler 
2290b032f27cSSam Leffler 		/*
2291b032f27cSSam Leffler 		 * The beacon interval is in TU's; the TSF is in usecs.
2292b032f27cSSam Leffler 		 * We figure out how many TU's to add to align the timestamp
2293b032f27cSSam Leffler 		 * then convert to TSF units and handle byte swapping before
2294b032f27cSSam Leffler 		 * inserting it in the frame.  The hardware will then add this
2295b032f27cSSam Leffler 		 * each time a beacon frame is sent.  Note that we align vap's
2296b032f27cSSam Leffler 		 * 1..N and leave vap 0 untouched.  This means vap 0 has a
2297b032f27cSSam Leffler 		 * timestamp in one beacon interval while the others get a
2298b032f27cSSam Leffler 		 * timstamp aligned to the next interval.
2299b032f27cSSam Leffler 		 */
2300b032f27cSSam Leffler 		tsfadjust = ni->ni_intval *
2301b032f27cSSam Leffler 		    (ATH_BCBUF - avp->av_bslot) / ATH_BCBUF;
2302b032f27cSSam Leffler 		tsfadjust = htole64(tsfadjust << 10);	/* TU -> TSF */
2303b032f27cSSam Leffler 
2304b032f27cSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
2305b032f27cSSam Leffler 		    "%s: %s beacons bslot %d intval %u tsfadjust %llu\n",
2306b032f27cSSam Leffler 		    __func__, sc->sc_stagbeacons ? "stagger" : "burst",
23073627e321SSam Leffler 		    avp->av_bslot, ni->ni_intval,
23083627e321SSam Leffler 		    (long long unsigned) le64toh(tsfadjust));
2309b032f27cSSam Leffler 
2310b032f27cSSam Leffler 		wh = mtod(m, struct ieee80211_frame *);
2311b032f27cSSam Leffler 		memcpy(&wh[1], &tsfadjust, sizeof(tsfadjust));
2312b032f27cSSam Leffler 	}
2313c42a7b7eSSam Leffler 	bf->bf_m = m;
2314f818612bSSam Leffler 	bf->bf_node = ieee80211_ref_node(ni);
2315b032f27cSSam Leffler 
2316b032f27cSSam Leffler 	return 0;
23175591b213SSam Leffler }
2318c42a7b7eSSam Leffler 
2319c42a7b7eSSam Leffler /*
2320c42a7b7eSSam Leffler  * Setup the beacon frame for transmit.
2321c42a7b7eSSam Leffler  */
2322c42a7b7eSSam Leffler static void
2323c42a7b7eSSam Leffler ath_beacon_setup(struct ath_softc *sc, struct ath_buf *bf)
2324c42a7b7eSSam Leffler {
2325c42a7b7eSSam Leffler #define	USE_SHPREAMBLE(_ic) \
2326c42a7b7eSSam Leffler 	(((_ic)->ic_flags & (IEEE80211_F_SHPREAMBLE | IEEE80211_F_USEBARKER))\
2327c42a7b7eSSam Leffler 		== IEEE80211_F_SHPREAMBLE)
2328c42a7b7eSSam Leffler 	struct ieee80211_node *ni = bf->bf_node;
2329c42a7b7eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2330c42a7b7eSSam Leffler 	struct mbuf *m = bf->bf_m;
2331c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2332c42a7b7eSSam Leffler 	struct ath_desc *ds;
2333c42a7b7eSSam Leffler 	int flags, antenna;
233455f63772SSam Leffler 	const HAL_RATE_TABLE *rt;
233555f63772SSam Leffler 	u_int8_t rix, rate;
2336c42a7b7eSSam Leffler 
23374a3ac3fcSSam Leffler 	DPRINTF(sc, ATH_DEBUG_BEACON_PROC, "%s: m %p len %u\n",
2338c42a7b7eSSam Leffler 		__func__, m, m->m_len);
23395591b213SSam Leffler 
23405591b213SSam Leffler 	/* setup descriptors */
23415591b213SSam Leffler 	ds = bf->bf_desc;
23425591b213SSam Leffler 
2343c42a7b7eSSam Leffler 	flags = HAL_TXDESC_NOACK;
2344c42a7b7eSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_hasveol) {
2345c42a7b7eSSam Leffler 		ds->ds_link = bf->bf_daddr;	/* self-linked */
2346c42a7b7eSSam Leffler 		flags |= HAL_TXDESC_VEOL;
2347c42a7b7eSSam Leffler 		/*
2348c42a7b7eSSam Leffler 		 * Let hardware handle antenna switching.
2349c42a7b7eSSam Leffler 		 */
23504866e6c2SSam Leffler 		antenna = sc->sc_txantenna;
2351c42a7b7eSSam Leffler 	} else {
23525591b213SSam Leffler 		ds->ds_link = 0;
2353c42a7b7eSSam Leffler 		/*
2354c42a7b7eSSam Leffler 		 * Switch antenna every 4 beacons.
2355c42a7b7eSSam Leffler 		 * XXX assumes two antenna
2356c42a7b7eSSam Leffler 		 */
2357b032f27cSSam Leffler 		if (sc->sc_txantenna != 0)
2358b032f27cSSam Leffler 			antenna = sc->sc_txantenna;
2359b032f27cSSam Leffler 		else if (sc->sc_stagbeacons && sc->sc_nbcnvaps != 0)
2360b032f27cSSam Leffler 			antenna = ((sc->sc_stats.ast_be_xmit / sc->sc_nbcnvaps) & 4 ? 2 : 1);
2361b032f27cSSam Leffler 		else
2362b032f27cSSam Leffler 			antenna = (sc->sc_stats.ast_be_xmit & 4 ? 2 : 1);
2363c42a7b7eSSam Leffler 	}
2364c42a7b7eSSam Leffler 
2365c42a7b7eSSam Leffler 	KASSERT(bf->bf_nseg == 1,
2366c42a7b7eSSam Leffler 		("multi-segment beacon frame; nseg %u", bf->bf_nseg));
23675591b213SSam Leffler 	ds->ds_data = bf->bf_segs[0].ds_addr;
23685591b213SSam Leffler 	/*
23695591b213SSam Leffler 	 * Calculate rate code.
23705591b213SSam Leffler 	 * XXX everything at min xmit rate
23715591b213SSam Leffler 	 */
2372b032f27cSSam Leffler 	rix = 0;
237355f63772SSam Leffler 	rt = sc->sc_currates;
237455f63772SSam Leffler 	rate = rt->info[rix].rateCode;
2375c42a7b7eSSam Leffler 	if (USE_SHPREAMBLE(ic))
237655f63772SSam Leffler 		rate |= rt->info[rix].shortPreamble;
23775591b213SSam Leffler 	ath_hal_setuptxdesc(ah, ds
2378c42a7b7eSSam Leffler 		, m->m_len + IEEE80211_CRC_LEN	/* frame length */
23795591b213SSam Leffler 		, sizeof(struct ieee80211_frame)/* header length */
23805591b213SSam Leffler 		, HAL_PKT_TYPE_BEACON		/* Atheros packet type */
2381c42a7b7eSSam Leffler 		, ni->ni_txpower		/* txpower XXX */
23825591b213SSam Leffler 		, rate, 1			/* series 0 rate/tries */
23835591b213SSam Leffler 		, HAL_TXKEYIX_INVALID		/* no encryption */
2384c42a7b7eSSam Leffler 		, antenna			/* antenna mode */
2385c42a7b7eSSam Leffler 		, flags				/* no ack, veol for beacons */
23865591b213SSam Leffler 		, 0				/* rts/cts rate */
23875591b213SSam Leffler 		, 0				/* rts/cts duration */
23885591b213SSam Leffler 	);
23895591b213SSam Leffler 	/* NB: beacon's BufLen must be a multiple of 4 bytes */
23905591b213SSam Leffler 	ath_hal_filltxdesc(ah, ds
2391c42a7b7eSSam Leffler 		, roundup(m->m_len, 4)		/* buffer length */
23925591b213SSam Leffler 		, AH_TRUE			/* first segment */
23935591b213SSam Leffler 		, AH_TRUE			/* last segment */
2394c42a7b7eSSam Leffler 		, ds				/* first descriptor */
23955591b213SSam Leffler 	);
2396b032f27cSSam Leffler #if 0
2397b032f27cSSam Leffler 	ath_desc_swap(ds);
2398b032f27cSSam Leffler #endif
2399c42a7b7eSSam Leffler #undef USE_SHPREAMBLE
24005591b213SSam Leffler }
24015591b213SSam Leffler 
2402b105a069SSam Leffler static void
2403b032f27cSSam Leffler ath_beacon_update(struct ieee80211vap *vap, int item)
2404b105a069SSam Leffler {
2405b032f27cSSam Leffler 	struct ieee80211_beacon_offsets *bo = &ATH_VAP(vap)->av_boff;
2406b105a069SSam Leffler 
2407b105a069SSam Leffler 	setbit(bo->bo_flags, item);
2408b105a069SSam Leffler }
2409b105a069SSam Leffler 
2410c42a7b7eSSam Leffler /*
2411622b3fd2SSam Leffler  * Append the contents of src to dst; both queues
2412622b3fd2SSam Leffler  * are assumed to be locked.
2413622b3fd2SSam Leffler  */
2414622b3fd2SSam Leffler static void
2415622b3fd2SSam Leffler ath_txqmove(struct ath_txq *dst, struct ath_txq *src)
2416622b3fd2SSam Leffler {
2417*6b349e5aSAdrian Chadd 	TAILQ_CONCAT(&dst->axq_q, &src->axq_q, bf_list);
2418622b3fd2SSam Leffler 	dst->axq_link = src->axq_link;
2419622b3fd2SSam Leffler 	src->axq_link = NULL;
2420622b3fd2SSam Leffler 	dst->axq_depth += src->axq_depth;
2421622b3fd2SSam Leffler 	src->axq_depth = 0;
2422622b3fd2SSam Leffler }
2423622b3fd2SSam Leffler 
2424622b3fd2SSam Leffler /*
2425c42a7b7eSSam Leffler  * Transmit a beacon frame at SWBA.  Dynamic updates to the
2426c42a7b7eSSam Leffler  * frame contents are done as needed and the slot time is
2427c42a7b7eSSam Leffler  * also adjusted based on current state.
2428c42a7b7eSSam Leffler  */
24295591b213SSam Leffler static void
24305591b213SSam Leffler ath_beacon_proc(void *arg, int pending)
24315591b213SSam Leffler {
24325591b213SSam Leffler 	struct ath_softc *sc = arg;
24335591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2434b032f27cSSam Leffler 	struct ieee80211vap *vap;
2435b032f27cSSam Leffler 	struct ath_buf *bf;
2436b032f27cSSam Leffler 	int slot, otherant;
2437b032f27cSSam Leffler 	uint32_t bfaddr;
24385591b213SSam Leffler 
2439c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_BEACON_PROC, "%s: pending %u\n",
2440c42a7b7eSSam Leffler 		__func__, pending);
2441c42a7b7eSSam Leffler 	/*
2442c42a7b7eSSam Leffler 	 * Check if the previous beacon has gone out.  If
2443c66c48cbSSam Leffler 	 * not don't try to post another, skip this period
2444c66c48cbSSam Leffler 	 * and wait for the next.  Missed beacons indicate
2445c66c48cbSSam Leffler 	 * a problem and should not occur.  If we miss too
2446c66c48cbSSam Leffler 	 * many consecutive beacons reset the device.
2447c42a7b7eSSam Leffler 	 */
2448c42a7b7eSSam Leffler 	if (ath_hal_numtxpending(ah, sc->sc_bhalq) != 0) {
2449c42a7b7eSSam Leffler 		sc->sc_bmisscount++;
24507ec4e6b8SAdrian Chadd 		sc->sc_stats.ast_be_missed++;
24514a3ac3fcSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
2452c42a7b7eSSam Leffler 			"%s: missed %u consecutive beacons\n",
2453c42a7b7eSSam Leffler 			__func__, sc->sc_bmisscount);
2454a32ac9d3SSam Leffler 		if (sc->sc_bmisscount >= ath_bstuck_threshold)
24550bbf5441SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_bstucktask);
2456c42a7b7eSSam Leffler 		return;
2457c42a7b7eSSam Leffler 	}
2458c42a7b7eSSam Leffler 	if (sc->sc_bmisscount != 0) {
2459c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
2460c42a7b7eSSam Leffler 			"%s: resume beacon xmit after %u misses\n",
2461c42a7b7eSSam Leffler 			__func__, sc->sc_bmisscount);
2462c42a7b7eSSam Leffler 		sc->sc_bmisscount = 0;
2463c42a7b7eSSam Leffler 	}
2464c42a7b7eSSam Leffler 
2465b032f27cSSam Leffler 	if (sc->sc_stagbeacons) {			/* staggered beacons */
2466b032f27cSSam Leffler 		struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2467b032f27cSSam Leffler 		uint32_t tsftu;
2468b032f27cSSam Leffler 
2469b032f27cSSam Leffler 		tsftu = ath_hal_gettsf32(ah) >> 10;
2470b032f27cSSam Leffler 		/* XXX lintval */
2471b032f27cSSam Leffler 		slot = ((tsftu % ic->ic_lintval) * ATH_BCBUF) / ic->ic_lintval;
2472b032f27cSSam Leffler 		vap = sc->sc_bslot[(slot+1) % ATH_BCBUF];
2473b032f27cSSam Leffler 		bfaddr = 0;
2474309a3e45SSam Leffler 		if (vap != NULL && vap->iv_state >= IEEE80211_S_RUN) {
2475b032f27cSSam Leffler 			bf = ath_beacon_generate(sc, vap);
2476b032f27cSSam Leffler 			if (bf != NULL)
2477b032f27cSSam Leffler 				bfaddr = bf->bf_daddr;
2478b032f27cSSam Leffler 		}
2479b032f27cSSam Leffler 	} else {					/* burst'd beacons */
2480b032f27cSSam Leffler 		uint32_t *bflink = &bfaddr;
2481b032f27cSSam Leffler 
2482b032f27cSSam Leffler 		for (slot = 0; slot < ATH_BCBUF; slot++) {
2483b032f27cSSam Leffler 			vap = sc->sc_bslot[slot];
2484309a3e45SSam Leffler 			if (vap != NULL && vap->iv_state >= IEEE80211_S_RUN) {
2485b032f27cSSam Leffler 				bf = ath_beacon_generate(sc, vap);
2486b032f27cSSam Leffler 				if (bf != NULL) {
2487b032f27cSSam Leffler 					*bflink = bf->bf_daddr;
2488b032f27cSSam Leffler 					bflink = &bf->bf_desc->ds_link;
2489c42a7b7eSSam Leffler 				}
2490c42a7b7eSSam Leffler 			}
2491b032f27cSSam Leffler 		}
2492b032f27cSSam Leffler 		*bflink = 0;				/* terminate list */
2493622b3fd2SSam Leffler 	}
2494c42a7b7eSSam Leffler 
2495c42a7b7eSSam Leffler 	/*
2496c42a7b7eSSam Leffler 	 * Handle slot time change when a non-ERP station joins/leaves
2497c42a7b7eSSam Leffler 	 * an 11g network.  The 802.11 layer notifies us via callback,
2498c42a7b7eSSam Leffler 	 * we mark updateslot, then wait one beacon before effecting
2499c42a7b7eSSam Leffler 	 * the change.  This gives associated stations at least one
2500c42a7b7eSSam Leffler 	 * beacon interval to note the state change.
2501c42a7b7eSSam Leffler 	 */
2502c42a7b7eSSam Leffler 	/* XXX locking */
2503b032f27cSSam Leffler 	if (sc->sc_updateslot == UPDATE) {
2504c42a7b7eSSam Leffler 		sc->sc_updateslot = COMMIT;	/* commit next beacon */
2505b032f27cSSam Leffler 		sc->sc_slotupdate = slot;
2506b032f27cSSam Leffler 	} else if (sc->sc_updateslot == COMMIT && sc->sc_slotupdate == slot)
2507c42a7b7eSSam Leffler 		ath_setslottime(sc);		/* commit change to h/w */
2508c42a7b7eSSam Leffler 
2509c42a7b7eSSam Leffler 	/*
2510c42a7b7eSSam Leffler 	 * Check recent per-antenna transmit statistics and flip
2511c42a7b7eSSam Leffler 	 * the default antenna if noticeably more frames went out
2512c42a7b7eSSam Leffler 	 * on the non-default antenna.
2513c42a7b7eSSam Leffler 	 * XXX assumes 2 anntenae
2514c42a7b7eSSam Leffler 	 */
2515b032f27cSSam Leffler 	if (!sc->sc_diversity && (!sc->sc_stagbeacons || slot == 0)) {
2516c42a7b7eSSam Leffler 		otherant = sc->sc_defant & 1 ? 2 : 1;
2517c42a7b7eSSam Leffler 		if (sc->sc_ant_tx[otherant] > sc->sc_ant_tx[sc->sc_defant] + 2)
2518c42a7b7eSSam Leffler 			ath_setdefantenna(sc, otherant);
2519c42a7b7eSSam Leffler 		sc->sc_ant_tx[1] = sc->sc_ant_tx[2] = 0;
2520b032f27cSSam Leffler 	}
2521c42a7b7eSSam Leffler 
2522b032f27cSSam Leffler 	if (bfaddr != 0) {
2523c42a7b7eSSam Leffler 		/*
2524c42a7b7eSSam Leffler 		 * Stop any current dma and put the new frame on the queue.
2525c42a7b7eSSam Leffler 		 * This should never fail since we check above that no frames
2526c42a7b7eSSam Leffler 		 * are still pending on the queue.
2527c42a7b7eSSam Leffler 		 */
25285591b213SSam Leffler 		if (!ath_hal_stoptxdma(ah, sc->sc_bhalq)) {
2529c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
2530c42a7b7eSSam Leffler 				"%s: beacon queue %u did not stop?\n",
2531c42a7b7eSSam Leffler 				__func__, sc->sc_bhalq);
25325591b213SSam Leffler 		}
2533b032f27cSSam Leffler 		/* NB: cabq traffic should already be queued and primed */
2534b032f27cSSam Leffler 		ath_hal_puttxbuf(ah, sc->sc_bhalq, bfaddr);
2535b032f27cSSam Leffler 		ath_hal_txstart(ah, sc->sc_bhalq);
2536b032f27cSSam Leffler 
2537b032f27cSSam Leffler 		sc->sc_stats.ast_be_xmit++;
2538b032f27cSSam Leffler 	}
2539b032f27cSSam Leffler }
2540b032f27cSSam Leffler 
2541b032f27cSSam Leffler static struct ath_buf *
2542b032f27cSSam Leffler ath_beacon_generate(struct ath_softc *sc, struct ieee80211vap *vap)
2543b032f27cSSam Leffler {
2544b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
2545b032f27cSSam Leffler 	struct ath_txq *cabq = sc->sc_cabq;
2546b032f27cSSam Leffler 	struct ath_buf *bf;
2547b032f27cSSam Leffler 	struct mbuf *m;
2548b032f27cSSam Leffler 	int nmcastq, error;
2549b032f27cSSam Leffler 
2550309a3e45SSam Leffler 	KASSERT(vap->iv_state >= IEEE80211_S_RUN,
2551b032f27cSSam Leffler 	    ("not running, state %d", vap->iv_state));
2552b032f27cSSam Leffler 	KASSERT(avp->av_bcbuf != NULL, ("no beacon buffer"));
2553b032f27cSSam Leffler 
2554b032f27cSSam Leffler 	/*
2555b032f27cSSam Leffler 	 * Update dynamic beacon contents.  If this returns
2556b032f27cSSam Leffler 	 * non-zero then we need to remap the memory because
2557b032f27cSSam Leffler 	 * the beacon frame changed size (probably because
2558b032f27cSSam Leffler 	 * of the TIM bitmap).
2559b032f27cSSam Leffler 	 */
2560b032f27cSSam Leffler 	bf = avp->av_bcbuf;
2561b032f27cSSam Leffler 	m = bf->bf_m;
2562b032f27cSSam Leffler 	nmcastq = avp->av_mcastq.axq_depth;
2563b032f27cSSam Leffler 	if (ieee80211_beacon_update(bf->bf_node, &avp->av_boff, m, nmcastq)) {
2564b032f27cSSam Leffler 		/* XXX too conservative? */
2565b032f27cSSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
2566b032f27cSSam Leffler 		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m,
2567b032f27cSSam Leffler 					     bf->bf_segs, &bf->bf_nseg,
2568b032f27cSSam Leffler 					     BUS_DMA_NOWAIT);
2569b032f27cSSam Leffler 		if (error != 0) {
2570b032f27cSSam Leffler 			if_printf(vap->iv_ifp,
2571b032f27cSSam Leffler 			    "%s: bus_dmamap_load_mbuf_sg failed, error %u\n",
2572b032f27cSSam Leffler 			    __func__, error);
2573b032f27cSSam Leffler 			return NULL;
2574b032f27cSSam Leffler 		}
2575b032f27cSSam Leffler 	}
2576b032f27cSSam Leffler 	if ((avp->av_boff.bo_tim[4] & 1) && cabq->axq_depth) {
2577b032f27cSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
2578b032f27cSSam Leffler 		    "%s: cabq did not drain, mcastq %u cabq %u\n",
2579b032f27cSSam Leffler 		    __func__, nmcastq, cabq->axq_depth);
2580b032f27cSSam Leffler 		sc->sc_stats.ast_cabq_busy++;
2581b032f27cSSam Leffler 		if (sc->sc_nvaps > 1 && sc->sc_stagbeacons) {
2582b032f27cSSam Leffler 			/*
2583b032f27cSSam Leffler 			 * CABQ traffic from a previous vap is still pending.
2584b032f27cSSam Leffler 			 * We must drain the q before this beacon frame goes
2585b032f27cSSam Leffler 			 * out as otherwise this vap's stations will get cab
2586b032f27cSSam Leffler 			 * frames from a different vap.
2587b032f27cSSam Leffler 			 * XXX could be slow causing us to miss DBA
2588b032f27cSSam Leffler 			 */
2589b032f27cSSam Leffler 			ath_tx_draintxq(sc, cabq);
2590b032f27cSSam Leffler 		}
2591b032f27cSSam Leffler 	}
2592b032f27cSSam Leffler 	ath_beacon_setup(sc, bf);
25935591b213SSam Leffler 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
25945591b213SSam Leffler 
2595c42a7b7eSSam Leffler 	/*
2596c42a7b7eSSam Leffler 	 * Enable the CAB queue before the beacon queue to
2597c42a7b7eSSam Leffler 	 * insure cab frames are triggered by this beacon.
2598c42a7b7eSSam Leffler 	 */
2599b032f27cSSam Leffler 	if (avp->av_boff.bo_tim[4] & 1) {
2600b032f27cSSam Leffler 		struct ath_hal *ah = sc->sc_ah;
2601b032f27cSSam Leffler 
2602f3af83f7SSam Leffler 		/* NB: only at DTIM */
2603622b3fd2SSam Leffler 		ATH_TXQ_LOCK(cabq);
2604b032f27cSSam Leffler 		ATH_TXQ_LOCK(&avp->av_mcastq);
2605622b3fd2SSam Leffler 		if (nmcastq) {
2606622b3fd2SSam Leffler 			struct ath_buf *bfm;
2607622b3fd2SSam Leffler 
2608622b3fd2SSam Leffler 			/*
2609622b3fd2SSam Leffler 			 * Move frames from the s/w mcast q to the h/w cab q.
2610b032f27cSSam Leffler 			 * XXX MORE_DATA bit
2611622b3fd2SSam Leffler 			 */
2612*6b349e5aSAdrian Chadd 			bfm = TAILQ_FIRST(&avp->av_mcastq.axq_q);
2613622b3fd2SSam Leffler 			if (cabq->axq_link != NULL) {
2614622b3fd2SSam Leffler 				*cabq->axq_link = bfm->bf_daddr;
2615622b3fd2SSam Leffler 			} else
2616622b3fd2SSam Leffler 				ath_hal_puttxbuf(ah, cabq->axq_qnum,
2617622b3fd2SSam Leffler 					bfm->bf_daddr);
2618b032f27cSSam Leffler 			ath_txqmove(cabq, &avp->av_mcastq);
2619622b3fd2SSam Leffler 
2620622b3fd2SSam Leffler 			sc->sc_stats.ast_cabq_xmit += nmcastq;
2621622b3fd2SSam Leffler 		}
2622622b3fd2SSam Leffler 		/* NB: gated by beacon so safe to start here */
2623*6b349e5aSAdrian Chadd 		if (! TAILQ_EMPTY(&(cabq->axq_q)))
2624622b3fd2SSam Leffler 			ath_hal_txstart(ah, cabq->axq_qnum);
2625b032f27cSSam Leffler 		ATH_TXQ_UNLOCK(&avp->av_mcastq);
26267b15790aSAdrian Chadd 		ATH_TXQ_UNLOCK(cabq);
2627622b3fd2SSam Leffler 	}
2628b032f27cSSam Leffler 	return bf;
2629b032f27cSSam Leffler }
2630b032f27cSSam Leffler 
2631b032f27cSSam Leffler static void
2632b032f27cSSam Leffler ath_beacon_start_adhoc(struct ath_softc *sc, struct ieee80211vap *vap)
2633b032f27cSSam Leffler {
2634b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
2635b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2636b032f27cSSam Leffler 	struct ath_buf *bf;
2637b032f27cSSam Leffler 	struct mbuf *m;
2638b032f27cSSam Leffler 	int error;
2639b032f27cSSam Leffler 
2640b032f27cSSam Leffler 	KASSERT(avp->av_bcbuf != NULL, ("no beacon buffer"));
2641b032f27cSSam Leffler 
2642b032f27cSSam Leffler 	/*
2643b032f27cSSam Leffler 	 * Update dynamic beacon contents.  If this returns
2644b032f27cSSam Leffler 	 * non-zero then we need to remap the memory because
2645b032f27cSSam Leffler 	 * the beacon frame changed size (probably because
2646b032f27cSSam Leffler 	 * of the TIM bitmap).
2647b032f27cSSam Leffler 	 */
2648b032f27cSSam Leffler 	bf = avp->av_bcbuf;
2649b032f27cSSam Leffler 	m = bf->bf_m;
2650b032f27cSSam Leffler 	if (ieee80211_beacon_update(bf->bf_node, &avp->av_boff, m, 0)) {
2651b032f27cSSam Leffler 		/* XXX too conservative? */
2652b032f27cSSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
2653b032f27cSSam Leffler 		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m,
2654b032f27cSSam Leffler 					     bf->bf_segs, &bf->bf_nseg,
2655b032f27cSSam Leffler 					     BUS_DMA_NOWAIT);
2656b032f27cSSam Leffler 		if (error != 0) {
2657b032f27cSSam Leffler 			if_printf(vap->iv_ifp,
2658b032f27cSSam Leffler 			    "%s: bus_dmamap_load_mbuf_sg failed, error %u\n",
2659b032f27cSSam Leffler 			    __func__, error);
2660b032f27cSSam Leffler 			return;
2661b032f27cSSam Leffler 		}
2662b032f27cSSam Leffler 	}
2663b032f27cSSam Leffler 	ath_beacon_setup(sc, bf);
2664b032f27cSSam Leffler 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
2665b032f27cSSam Leffler 
2666b032f27cSSam Leffler 	/* NB: caller is known to have already stopped tx dma */
26675591b213SSam Leffler 	ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr);
26685591b213SSam Leffler 	ath_hal_txstart(ah, sc->sc_bhalq);
26695591b213SSam Leffler }
26705591b213SSam Leffler 
2671c42a7b7eSSam Leffler /*
2672c42a7b7eSSam Leffler  * Reset the hardware after detecting beacons have stopped.
2673c42a7b7eSSam Leffler  */
2674c42a7b7eSSam Leffler static void
2675c42a7b7eSSam Leffler ath_bstuck_proc(void *arg, int pending)
2676c42a7b7eSSam Leffler {
2677c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
2678fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
2679c42a7b7eSSam Leffler 
2680c42a7b7eSSam Leffler 	if_printf(ifp, "stuck beacon; resetting (bmiss count %u)\n",
2681c42a7b7eSSam Leffler 		sc->sc_bmisscount);
2682c2e34459SSam Leffler 	sc->sc_stats.ast_bstuck++;
2683c42a7b7eSSam Leffler 	ath_reset(ifp);
2684c42a7b7eSSam Leffler }
2685c42a7b7eSSam Leffler 
2686c42a7b7eSSam Leffler /*
2687b032f27cSSam Leffler  * Reclaim beacon resources and return buffer to the pool.
2688b032f27cSSam Leffler  */
2689b032f27cSSam Leffler static void
2690b032f27cSSam Leffler ath_beacon_return(struct ath_softc *sc, struct ath_buf *bf)
2691b032f27cSSam Leffler {
2692b032f27cSSam Leffler 
2693b032f27cSSam Leffler 	if (bf->bf_m != NULL) {
2694b032f27cSSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
2695b032f27cSSam Leffler 		m_freem(bf->bf_m);
2696b032f27cSSam Leffler 		bf->bf_m = NULL;
2697b032f27cSSam Leffler 	}
2698b032f27cSSam Leffler 	if (bf->bf_node != NULL) {
2699b032f27cSSam Leffler 		ieee80211_free_node(bf->bf_node);
2700b032f27cSSam Leffler 		bf->bf_node = NULL;
2701b032f27cSSam Leffler 	}
2702*6b349e5aSAdrian Chadd 	TAILQ_INSERT_TAIL(&sc->sc_bbuf, bf, bf_list);
2703b032f27cSSam Leffler }
2704b032f27cSSam Leffler 
2705b032f27cSSam Leffler /*
2706c42a7b7eSSam Leffler  * Reclaim beacon resources.
2707c42a7b7eSSam Leffler  */
27085591b213SSam Leffler static void
27095591b213SSam Leffler ath_beacon_free(struct ath_softc *sc)
27105591b213SSam Leffler {
2711c42a7b7eSSam Leffler 	struct ath_buf *bf;
27125591b213SSam Leffler 
2713*6b349e5aSAdrian Chadd 	TAILQ_FOREACH(bf, &sc->sc_bbuf, bf_list) {
27145591b213SSam Leffler 		if (bf->bf_m != NULL) {
27155591b213SSam Leffler 			bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
27165591b213SSam Leffler 			m_freem(bf->bf_m);
27175591b213SSam Leffler 			bf->bf_m = NULL;
2718f818612bSSam Leffler 		}
2719f818612bSSam Leffler 		if (bf->bf_node != NULL) {
2720f818612bSSam Leffler 			ieee80211_free_node(bf->bf_node);
27215591b213SSam Leffler 			bf->bf_node = NULL;
27225591b213SSam Leffler 		}
27235591b213SSam Leffler 	}
2724f818612bSSam Leffler }
27255591b213SSam Leffler 
27265591b213SSam Leffler /*
27275591b213SSam Leffler  * Configure the beacon and sleep timers.
27285591b213SSam Leffler  *
27295591b213SSam Leffler  * When operating as an AP this resets the TSF and sets
27305591b213SSam Leffler  * up the hardware to notify us when we need to issue beacons.
27315591b213SSam Leffler  *
27325591b213SSam Leffler  * When operating in station mode this sets up the beacon
27335591b213SSam Leffler  * timers according to the timestamp of the last received
27345591b213SSam Leffler  * beacon and the current TSF, configures PCF and DTIM
27355591b213SSam Leffler  * handling, programs the sleep registers so the hardware
27365591b213SSam Leffler  * will wakeup in time to receive beacons, and configures
27375591b213SSam Leffler  * the beacon miss handling so we'll receive a BMISS
27385591b213SSam Leffler  * interrupt when we stop seeing beacons from the AP
27395591b213SSam Leffler  * we've associated with.
27405591b213SSam Leffler  */
27415591b213SSam Leffler static void
2742b032f27cSSam Leffler ath_beacon_config(struct ath_softc *sc, struct ieee80211vap *vap)
27435591b213SSam Leffler {
274480d939bfSSam Leffler #define	TSF_TO_TU(_h,_l) \
274580d939bfSSam Leffler 	((((u_int32_t)(_h)) << 22) | (((u_int32_t)(_l)) >> 10))
274680d939bfSSam Leffler #define	FUDGE	2
27475591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2748b032f27cSSam Leffler 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2749b032f27cSSam Leffler 	struct ieee80211_node *ni;
275080d939bfSSam Leffler 	u_int32_t nexttbtt, intval, tsftu;
275180d939bfSSam Leffler 	u_int64_t tsf;
27525591b213SSam Leffler 
2753b032f27cSSam Leffler 	if (vap == NULL)
2754b032f27cSSam Leffler 		vap = TAILQ_FIRST(&ic->ic_vaps);	/* XXX */
2755b032f27cSSam Leffler 	ni = vap->iv_bss;
2756b032f27cSSam Leffler 
27578371372bSSam Leffler 	/* extract tstamp from last beacon and convert to TU */
27588371372bSSam Leffler 	nexttbtt = TSF_TO_TU(LE_READ_4(ni->ni_tstamp.data + 4),
27598371372bSSam Leffler 			     LE_READ_4(ni->ni_tstamp.data));
276059aa14a9SRui Paulo 	if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
276159aa14a9SRui Paulo 	    ic->ic_opmode == IEEE80211_M_MBSS) {
2762b032f27cSSam Leffler 		/*
276359aa14a9SRui Paulo 		 * For multi-bss ap/mesh support beacons are either staggered
2764b032f27cSSam Leffler 		 * evenly over N slots or burst together.  For the former
2765b032f27cSSam Leffler 		 * arrange for the SWBA to be delivered for each slot.
2766b032f27cSSam Leffler 		 * Slots that are not occupied will generate nothing.
2767b032f27cSSam Leffler 		 */
27688371372bSSam Leffler 		/* NB: the beacon interval is kept internally in TU's */
27694bacf7c1SSam Leffler 		intval = ni->ni_intval & HAL_BEACON_PERIOD;
2770b032f27cSSam Leffler 		if (sc->sc_stagbeacons)
2771b032f27cSSam Leffler 			intval /= ATH_BCBUF;
2772b032f27cSSam Leffler 	} else {
2773b032f27cSSam Leffler 		/* NB: the beacon interval is kept internally in TU's */
2774b032f27cSSam Leffler 		intval = ni->ni_intval & HAL_BEACON_PERIOD;
2775b032f27cSSam Leffler 	}
2776a6c992f4SSam Leffler 	if (nexttbtt == 0)		/* e.g. for ap mode */
2777a6c992f4SSam Leffler 		nexttbtt = intval;
2778a6c992f4SSam Leffler 	else if (intval)		/* NB: can be 0 for monitor mode */
2779a6c992f4SSam Leffler 		nexttbtt = roundup(nexttbtt, intval);
2780a6c992f4SSam Leffler 	DPRINTF(sc, ATH_DEBUG_BEACON, "%s: nexttbtt %u intval %u (%u)\n",
2781a6c992f4SSam Leffler 		__func__, nexttbtt, intval, ni->ni_intval);
2782b032f27cSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA && !sc->sc_swbmiss) {
27835591b213SSam Leffler 		HAL_BEACON_STATE bs;
27848371372bSSam Leffler 		int dtimperiod, dtimcount;
27858371372bSSam Leffler 		int cfpperiod, cfpcount;
27865591b213SSam Leffler 
27878371372bSSam Leffler 		/*
27888371372bSSam Leffler 		 * Setup dtim and cfp parameters according to
27898371372bSSam Leffler 		 * last beacon we received (which may be none).
27908371372bSSam Leffler 		 */
27918371372bSSam Leffler 		dtimperiod = ni->ni_dtim_period;
27928371372bSSam Leffler 		if (dtimperiod <= 0)		/* NB: 0 if not known */
27938371372bSSam Leffler 			dtimperiod = 1;
27948371372bSSam Leffler 		dtimcount = ni->ni_dtim_count;
27958371372bSSam Leffler 		if (dtimcount >= dtimperiod)	/* NB: sanity check */
27968371372bSSam Leffler 			dtimcount = 0;		/* XXX? */
27978371372bSSam Leffler 		cfpperiod = 1;			/* NB: no PCF support yet */
27988371372bSSam Leffler 		cfpcount = 0;
27998371372bSSam Leffler 		/*
28008371372bSSam Leffler 		 * Pull nexttbtt forward to reflect the current
28018371372bSSam Leffler 		 * TSF and calculate dtim+cfp state for the result.
28028371372bSSam Leffler 		 */
28038371372bSSam Leffler 		tsf = ath_hal_gettsf64(ah);
280480d939bfSSam Leffler 		tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
28058371372bSSam Leffler 		do {
28068371372bSSam Leffler 			nexttbtt += intval;
28078371372bSSam Leffler 			if (--dtimcount < 0) {
28088371372bSSam Leffler 				dtimcount = dtimperiod - 1;
28098371372bSSam Leffler 				if (--cfpcount < 0)
28108371372bSSam Leffler 					cfpcount = cfpperiod - 1;
28118371372bSSam Leffler 			}
28128371372bSSam Leffler 		} while (nexttbtt < tsftu);
28135591b213SSam Leffler 		memset(&bs, 0, sizeof(bs));
2814a6c992f4SSam Leffler 		bs.bs_intval = intval;
28155591b213SSam Leffler 		bs.bs_nexttbtt = nexttbtt;
28168371372bSSam Leffler 		bs.bs_dtimperiod = dtimperiod*intval;
28178371372bSSam Leffler 		bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval;
28188371372bSSam Leffler 		bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod;
28198371372bSSam Leffler 		bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod;
28208371372bSSam Leffler 		bs.bs_cfpmaxduration = 0;
28218371372bSSam Leffler #if 0
28225591b213SSam Leffler 		/*
2823c42a7b7eSSam Leffler 		 * The 802.11 layer records the offset to the DTIM
2824c42a7b7eSSam Leffler 		 * bitmap while receiving beacons; use it here to
2825c42a7b7eSSam Leffler 		 * enable h/w detection of our AID being marked in
2826c42a7b7eSSam Leffler 		 * the bitmap vector (to indicate frames for us are
2827c42a7b7eSSam Leffler 		 * pending at the AP).
28288371372bSSam Leffler 		 * XXX do DTIM handling in s/w to WAR old h/w bugs
28298371372bSSam Leffler 		 * XXX enable based on h/w rev for newer chips
2830c42a7b7eSSam Leffler 		 */
2831c42a7b7eSSam Leffler 		bs.bs_timoffset = ni->ni_timoff;
28328371372bSSam Leffler #endif
2833c42a7b7eSSam Leffler 		/*
28345591b213SSam Leffler 		 * Calculate the number of consecutive beacons to miss
283568e8e04eSSam Leffler 		 * before taking a BMISS interrupt.
28365591b213SSam Leffler 		 * Note that we clamp the result to at most 10 beacons.
28375591b213SSam Leffler 		 */
2838b032f27cSSam Leffler 		bs.bs_bmissthreshold = vap->iv_bmissthreshold;
28395591b213SSam Leffler 		if (bs.bs_bmissthreshold > 10)
28405591b213SSam Leffler 			bs.bs_bmissthreshold = 10;
28415591b213SSam Leffler 		else if (bs.bs_bmissthreshold <= 0)
28425591b213SSam Leffler 			bs.bs_bmissthreshold = 1;
28435591b213SSam Leffler 
28445591b213SSam Leffler 		/*
28455591b213SSam Leffler 		 * Calculate sleep duration.  The configuration is
28465591b213SSam Leffler 		 * given in ms.  We insure a multiple of the beacon
28475591b213SSam Leffler 		 * period is used.  Also, if the sleep duration is
28485591b213SSam Leffler 		 * greater than the DTIM period then it makes senses
28495591b213SSam Leffler 		 * to make it a multiple of that.
28505591b213SSam Leffler 		 *
28515591b213SSam Leffler 		 * XXX fixed at 100ms
28525591b213SSam Leffler 		 */
28534bacf7c1SSam Leffler 		bs.bs_sleepduration =
28544bacf7c1SSam Leffler 			roundup(IEEE80211_MS_TO_TU(100), bs.bs_intval);
28555591b213SSam Leffler 		if (bs.bs_sleepduration > bs.bs_dtimperiod)
28565591b213SSam Leffler 			bs.bs_sleepduration = roundup(bs.bs_sleepduration, bs.bs_dtimperiod);
28575591b213SSam Leffler 
2858c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
28598371372bSSam 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"
28605591b213SSam Leffler 			, __func__
28618371372bSSam Leffler 			, tsf, tsftu
28625591b213SSam Leffler 			, bs.bs_intval
28635591b213SSam Leffler 			, bs.bs_nexttbtt
28645591b213SSam Leffler 			, bs.bs_dtimperiod
28655591b213SSam Leffler 			, bs.bs_nextdtim
28665591b213SSam Leffler 			, bs.bs_bmissthreshold
28675591b213SSam Leffler 			, bs.bs_sleepduration
2868c42a7b7eSSam Leffler 			, bs.bs_cfpperiod
2869c42a7b7eSSam Leffler 			, bs.bs_cfpmaxduration
2870c42a7b7eSSam Leffler 			, bs.bs_cfpnext
2871c42a7b7eSSam Leffler 			, bs.bs_timoffset
2872c42a7b7eSSam Leffler 		);
28735591b213SSam Leffler 		ath_hal_intrset(ah, 0);
2874c42a7b7eSSam Leffler 		ath_hal_beacontimers(ah, &bs);
28755591b213SSam Leffler 		sc->sc_imask |= HAL_INT_BMISS;
28765591b213SSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
28775591b213SSam Leffler 	} else {
28785591b213SSam Leffler 		ath_hal_intrset(ah, 0);
2879a6c992f4SSam Leffler 		if (nexttbtt == intval)
2880c42a7b7eSSam Leffler 			intval |= HAL_BEACON_RESET_TSF;
2881c42a7b7eSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS) {
2882c42a7b7eSSam Leffler 			/*
2883c42a7b7eSSam Leffler 			 * In IBSS mode enable the beacon timers but only
2884c42a7b7eSSam Leffler 			 * enable SWBA interrupts if we need to manually
2885c42a7b7eSSam Leffler 			 * prepare beacon frames.  Otherwise we use a
2886c42a7b7eSSam Leffler 			 * self-linked tx descriptor and let the hardware
2887c42a7b7eSSam Leffler 			 * deal with things.
2888c42a7b7eSSam Leffler 			 */
2889c42a7b7eSSam Leffler 			intval |= HAL_BEACON_ENA;
2890c42a7b7eSSam Leffler 			if (!sc->sc_hasveol)
2891c42a7b7eSSam Leffler 				sc->sc_imask |= HAL_INT_SWBA;
289280d939bfSSam Leffler 			if ((intval & HAL_BEACON_RESET_TSF) == 0) {
289380d939bfSSam Leffler 				/*
289480d939bfSSam Leffler 				 * Pull nexttbtt forward to reflect
289580d939bfSSam Leffler 				 * the current TSF.
289680d939bfSSam Leffler 				 */
289780d939bfSSam Leffler 				tsf = ath_hal_gettsf64(ah);
289880d939bfSSam Leffler 				tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
289980d939bfSSam Leffler 				do {
290080d939bfSSam Leffler 					nexttbtt += intval;
290180d939bfSSam Leffler 				} while (nexttbtt < tsftu);
290280d939bfSSam Leffler 			}
29030f2e86fbSSam Leffler 			ath_beaconq_config(sc);
290459aa14a9SRui Paulo 		} else if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
290559aa14a9SRui Paulo 		    ic->ic_opmode == IEEE80211_M_MBSS) {
2906c42a7b7eSSam Leffler 			/*
290759aa14a9SRui Paulo 			 * In AP/mesh mode we enable the beacon timers
290859aa14a9SRui Paulo 			 * and SWBA interrupts to prepare beacon frames.
2909c42a7b7eSSam Leffler 			 */
2910c42a7b7eSSam Leffler 			intval |= HAL_BEACON_ENA;
29115591b213SSam Leffler 			sc->sc_imask |= HAL_INT_SWBA;	/* beacon prepare */
29120f2e86fbSSam Leffler 			ath_beaconq_config(sc);
2913c42a7b7eSSam Leffler 		}
2914c42a7b7eSSam Leffler 		ath_hal_beaconinit(ah, nexttbtt, intval);
2915c42a7b7eSSam Leffler 		sc->sc_bmisscount = 0;
29165591b213SSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
2917c42a7b7eSSam Leffler 		/*
2918c42a7b7eSSam Leffler 		 * When using a self-linked beacon descriptor in
2919c42a7b7eSSam Leffler 		 * ibss mode load it once here.
2920c42a7b7eSSam Leffler 		 */
2921c42a7b7eSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_hasveol)
2922b032f27cSSam Leffler 			ath_beacon_start_adhoc(sc, vap);
29235591b213SSam Leffler 	}
292480d939bfSSam Leffler 	sc->sc_syncbeacon = 0;
292580d939bfSSam Leffler #undef FUDGE
29268371372bSSam Leffler #undef TSF_TO_TU
29275591b213SSam Leffler }
29285591b213SSam Leffler 
29295591b213SSam Leffler static void
29305591b213SSam Leffler ath_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
29315591b213SSam Leffler {
29325591b213SSam Leffler 	bus_addr_t *paddr = (bus_addr_t*) arg;
2933d77367bfSSam Leffler 	KASSERT(error == 0, ("error %u on bus_dma callback", error));
29345591b213SSam Leffler 	*paddr = segs->ds_addr;
29355591b213SSam Leffler }
29365591b213SSam Leffler 
29375591b213SSam Leffler static int
2938c42a7b7eSSam Leffler ath_descdma_setup(struct ath_softc *sc,
2939c42a7b7eSSam Leffler 	struct ath_descdma *dd, ath_bufhead *head,
2940c42a7b7eSSam Leffler 	const char *name, int nbuf, int ndesc)
2941c42a7b7eSSam Leffler {
2942c42a7b7eSSam Leffler #define	DS2PHYS(_dd, _ds) \
2943c42a7b7eSSam Leffler 	((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
294445abcd6cSAdrian Chadd #define	ATH_DESC_4KB_BOUND_CHECK(_daddr, _len) \
294545abcd6cSAdrian Chadd 	((((u_int32_t)(_daddr) & 0xFFF) > (0x1000 - (_len))) ? 1 : 0)
2946fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
294745abcd6cSAdrian Chadd 	uint8_t *ds;
2948c42a7b7eSSam Leffler 	struct ath_buf *bf;
2949c42a7b7eSSam Leffler 	int i, bsize, error;
295045abcd6cSAdrian Chadd 	int desc_len;
295145abcd6cSAdrian Chadd 
295245abcd6cSAdrian Chadd 	desc_len = sizeof(struct ath_desc);
2953c42a7b7eSSam Leffler 
2954c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA: %u buffers %u desc/buf\n",
2955c42a7b7eSSam Leffler 	    __func__, name, nbuf, ndesc);
2956c42a7b7eSSam Leffler 
2957c42a7b7eSSam Leffler 	dd->dd_name = name;
295845abcd6cSAdrian Chadd 	dd->dd_desc_len = desc_len * nbuf * ndesc;
295945abcd6cSAdrian Chadd 
296045abcd6cSAdrian Chadd 	/*
296145abcd6cSAdrian Chadd 	 * Merlin work-around:
296245abcd6cSAdrian Chadd 	 * Descriptors that cross the 4KB boundary can't be used.
296345abcd6cSAdrian Chadd 	 * Assume one skipped descriptor per 4KB page.
296445abcd6cSAdrian Chadd 	 */
296545abcd6cSAdrian Chadd 	if (! ath_hal_split4ktrans(sc->sc_ah)) {
296645abcd6cSAdrian Chadd 		int numdescpage = 4096 / (desc_len * ndesc);
296745abcd6cSAdrian Chadd 		dd->dd_desc_len = (nbuf / numdescpage + 1) * 4096;
296845abcd6cSAdrian Chadd 	}
2969c42a7b7eSSam Leffler 
2970c42a7b7eSSam Leffler 	/*
2971c42a7b7eSSam Leffler 	 * Setup DMA descriptor area.
2972c42a7b7eSSam Leffler 	 */
2973c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev),	/* parent */
2974c42a7b7eSSam Leffler 		       PAGE_SIZE, 0,		/* alignment, bounds */
2975c42a7b7eSSam Leffler 		       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
2976c42a7b7eSSam Leffler 		       BUS_SPACE_MAXADDR,	/* highaddr */
2977c42a7b7eSSam Leffler 		       NULL, NULL,		/* filter, filterarg */
2978c42a7b7eSSam Leffler 		       dd->dd_desc_len,		/* maxsize */
2979c42a7b7eSSam Leffler 		       1,			/* nsegments */
29806ccb8ea7SSam Leffler 		       dd->dd_desc_len,		/* maxsegsize */
2981c42a7b7eSSam Leffler 		       BUS_DMA_ALLOCNOW,	/* flags */
2982c42a7b7eSSam Leffler 		       NULL,			/* lockfunc */
2983c42a7b7eSSam Leffler 		       NULL,			/* lockarg */
2984c42a7b7eSSam Leffler 		       &dd->dd_dmat);
2985c42a7b7eSSam Leffler 	if (error != 0) {
2986c42a7b7eSSam Leffler 		if_printf(ifp, "cannot allocate %s DMA tag\n", dd->dd_name);
2987c42a7b7eSSam Leffler 		return error;
2988c42a7b7eSSam Leffler 	}
2989c42a7b7eSSam Leffler 
2990c42a7b7eSSam Leffler 	/* allocate descriptors */
2991c42a7b7eSSam Leffler 	error = bus_dmamap_create(dd->dd_dmat, BUS_DMA_NOWAIT, &dd->dd_dmamap);
2992c42a7b7eSSam Leffler 	if (error != 0) {
2993c42a7b7eSSam Leffler 		if_printf(ifp, "unable to create dmamap for %s descriptors, "
2994c42a7b7eSSam Leffler 			"error %u\n", dd->dd_name, error);
2995c42a7b7eSSam Leffler 		goto fail0;
2996c42a7b7eSSam Leffler 	}
2997c42a7b7eSSam Leffler 
2998c42a7b7eSSam Leffler 	error = bus_dmamem_alloc(dd->dd_dmat, (void**) &dd->dd_desc,
29990553a01fSSam Leffler 				 BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
30000553a01fSSam Leffler 				 &dd->dd_dmamap);
3001c42a7b7eSSam Leffler 	if (error != 0) {
3002c42a7b7eSSam Leffler 		if_printf(ifp, "unable to alloc memory for %u %s descriptors, "
3003c42a7b7eSSam Leffler 			"error %u\n", nbuf * ndesc, dd->dd_name, error);
3004c42a7b7eSSam Leffler 		goto fail1;
3005c42a7b7eSSam Leffler 	}
3006c42a7b7eSSam Leffler 
3007c42a7b7eSSam Leffler 	error = bus_dmamap_load(dd->dd_dmat, dd->dd_dmamap,
3008c42a7b7eSSam Leffler 				dd->dd_desc, dd->dd_desc_len,
3009c42a7b7eSSam Leffler 				ath_load_cb, &dd->dd_desc_paddr,
3010c42a7b7eSSam Leffler 				BUS_DMA_NOWAIT);
3011c42a7b7eSSam Leffler 	if (error != 0) {
3012c42a7b7eSSam Leffler 		if_printf(ifp, "unable to map %s descriptors, error %u\n",
3013c42a7b7eSSam Leffler 			dd->dd_name, error);
3014c42a7b7eSSam Leffler 		goto fail2;
3015c42a7b7eSSam Leffler 	}
3016c42a7b7eSSam Leffler 
301745abcd6cSAdrian Chadd 	ds = (uint8_t *) dd->dd_desc;
3018c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA map: %p (%lu) -> %p (%lu)\n",
3019c42a7b7eSSam Leffler 	    __func__, dd->dd_name, ds, (u_long) dd->dd_desc_len,
3020c42a7b7eSSam Leffler 	    (caddr_t) dd->dd_desc_paddr, /*XXX*/ (u_long) dd->dd_desc_len);
3021c42a7b7eSSam Leffler 
3022ebecf802SSam Leffler 	/* allocate rx buffers */
3023c42a7b7eSSam Leffler 	bsize = sizeof(struct ath_buf) * nbuf;
3024c42a7b7eSSam Leffler 	bf = malloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO);
3025c42a7b7eSSam Leffler 	if (bf == NULL) {
3026c42a7b7eSSam Leffler 		if_printf(ifp, "malloc of %s buffers failed, size %u\n",
3027c42a7b7eSSam Leffler 			dd->dd_name, bsize);
3028c42a7b7eSSam Leffler 		goto fail3;
3029c42a7b7eSSam Leffler 	}
3030c42a7b7eSSam Leffler 	dd->dd_bufptr = bf;
3031c42a7b7eSSam Leffler 
3032*6b349e5aSAdrian Chadd 	TAILQ_INIT(head);
303345abcd6cSAdrian Chadd 	for (i = 0; i < nbuf; i++, bf++, ds += (ndesc * desc_len)) {
303445abcd6cSAdrian Chadd 		bf->bf_desc = (struct ath_desc *) ds;
3035c42a7b7eSSam Leffler 		bf->bf_daddr = DS2PHYS(dd, ds);
303645abcd6cSAdrian Chadd 		if (! ath_hal_split4ktrans(sc->sc_ah)) {
303745abcd6cSAdrian Chadd 			/*
303845abcd6cSAdrian Chadd 			 * Merlin WAR: Skip descriptor addresses which
303945abcd6cSAdrian Chadd 			 * cause 4KB boundary crossing along any point
304045abcd6cSAdrian Chadd 			 * in the descriptor.
304145abcd6cSAdrian Chadd 			 */
304245abcd6cSAdrian Chadd 			 if (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr,
304345abcd6cSAdrian Chadd 			     desc_len * ndesc)) {
304445abcd6cSAdrian Chadd 				/* Start at the next page */
304545abcd6cSAdrian Chadd 				ds += 0x1000 - (bf->bf_daddr & 0xFFF);
304645abcd6cSAdrian Chadd 				bf->bf_desc = (struct ath_desc *) ds;
304745abcd6cSAdrian Chadd 				bf->bf_daddr = DS2PHYS(dd, ds);
304845abcd6cSAdrian Chadd 			}
304945abcd6cSAdrian Chadd 		}
3050c42a7b7eSSam Leffler 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
3051c42a7b7eSSam Leffler 				&bf->bf_dmamap);
3052c42a7b7eSSam Leffler 		if (error != 0) {
3053c42a7b7eSSam Leffler 			if_printf(ifp, "unable to create dmamap for %s "
3054c42a7b7eSSam Leffler 				"buffer %u, error %u\n", dd->dd_name, i, error);
3055c42a7b7eSSam Leffler 			ath_descdma_cleanup(sc, dd, head);
3056c42a7b7eSSam Leffler 			return error;
3057c42a7b7eSSam Leffler 		}
3058*6b349e5aSAdrian Chadd 		TAILQ_INSERT_TAIL(head, bf, bf_list);
3059c42a7b7eSSam Leffler 	}
3060c42a7b7eSSam Leffler 	return 0;
3061c42a7b7eSSam Leffler fail3:
3062c42a7b7eSSam Leffler 	bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
3063c42a7b7eSSam Leffler fail2:
3064c42a7b7eSSam Leffler 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3065c42a7b7eSSam Leffler fail1:
3066c42a7b7eSSam Leffler 	bus_dmamap_destroy(dd->dd_dmat, dd->dd_dmamap);
3067c42a7b7eSSam Leffler fail0:
3068c42a7b7eSSam Leffler 	bus_dma_tag_destroy(dd->dd_dmat);
3069c42a7b7eSSam Leffler 	memset(dd, 0, sizeof(*dd));
3070c42a7b7eSSam Leffler 	return error;
3071c42a7b7eSSam Leffler #undef DS2PHYS
307245abcd6cSAdrian Chadd #undef ATH_DESC_4KB_BOUND_CHECK
3073c42a7b7eSSam Leffler }
3074c42a7b7eSSam Leffler 
3075c42a7b7eSSam Leffler static void
3076c42a7b7eSSam Leffler ath_descdma_cleanup(struct ath_softc *sc,
3077c42a7b7eSSam Leffler 	struct ath_descdma *dd, ath_bufhead *head)
3078c42a7b7eSSam Leffler {
3079c42a7b7eSSam Leffler 	struct ath_buf *bf;
3080c42a7b7eSSam Leffler 	struct ieee80211_node *ni;
3081c42a7b7eSSam Leffler 
3082c42a7b7eSSam Leffler 	bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
3083c42a7b7eSSam Leffler 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3084c42a7b7eSSam Leffler 	bus_dmamap_destroy(dd->dd_dmat, dd->dd_dmamap);
3085c42a7b7eSSam Leffler 	bus_dma_tag_destroy(dd->dd_dmat);
3086c42a7b7eSSam Leffler 
3087*6b349e5aSAdrian Chadd 	TAILQ_FOREACH(bf, head, bf_list) {
3088c42a7b7eSSam Leffler 		if (bf->bf_m) {
3089c42a7b7eSSam Leffler 			m_freem(bf->bf_m);
3090c42a7b7eSSam Leffler 			bf->bf_m = NULL;
3091c42a7b7eSSam Leffler 		}
3092c42a7b7eSSam Leffler 		if (bf->bf_dmamap != NULL) {
3093c42a7b7eSSam Leffler 			bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
3094c42a7b7eSSam Leffler 			bf->bf_dmamap = NULL;
3095c42a7b7eSSam Leffler 		}
3096c42a7b7eSSam Leffler 		ni = bf->bf_node;
3097c42a7b7eSSam Leffler 		bf->bf_node = NULL;
3098c42a7b7eSSam Leffler 		if (ni != NULL) {
3099c42a7b7eSSam Leffler 			/*
3100c42a7b7eSSam Leffler 			 * Reclaim node reference.
3101c42a7b7eSSam Leffler 			 */
3102c42a7b7eSSam Leffler 			ieee80211_free_node(ni);
3103c42a7b7eSSam Leffler 		}
3104c42a7b7eSSam Leffler 	}
3105c42a7b7eSSam Leffler 
3106*6b349e5aSAdrian Chadd 	TAILQ_INIT(head);
3107c42a7b7eSSam Leffler 	free(dd->dd_bufptr, M_ATHDEV);
3108c42a7b7eSSam Leffler 	memset(dd, 0, sizeof(*dd));
3109c42a7b7eSSam Leffler }
3110c42a7b7eSSam Leffler 
3111c42a7b7eSSam Leffler static int
31125591b213SSam Leffler ath_desc_alloc(struct ath_softc *sc)
31135591b213SSam Leffler {
3114c42a7b7eSSam Leffler 	int error;
31155591b213SSam Leffler 
3116c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_rxdma, &sc->sc_rxbuf,
3117e2d787faSSam Leffler 			"rx", ath_rxbuf, 1);
31185591b213SSam Leffler 	if (error != 0)
31195591b213SSam Leffler 		return error;
31205591b213SSam Leffler 
3121c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_txdma, &sc->sc_txbuf,
3122e2d787faSSam Leffler 			"tx", ath_txbuf, ATH_TXDESC);
3123c42a7b7eSSam Leffler 	if (error != 0) {
3124c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
31255591b213SSam Leffler 		return error;
3126c42a7b7eSSam Leffler 	}
3127c42a7b7eSSam Leffler 
3128c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_bdma, &sc->sc_bbuf,
3129b032f27cSSam Leffler 			"beacon", ATH_BCBUF, 1);
3130c42a7b7eSSam Leffler 	if (error != 0) {
3131c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3132c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
3133c42a7b7eSSam Leffler 		return error;
3134c42a7b7eSSam Leffler 	}
31355591b213SSam Leffler 	return 0;
31365591b213SSam Leffler }
31375591b213SSam Leffler 
31385591b213SSam Leffler static void
31395591b213SSam Leffler ath_desc_free(struct ath_softc *sc)
31405591b213SSam Leffler {
31415591b213SSam Leffler 
3142c42a7b7eSSam Leffler 	if (sc->sc_bdma.dd_desc_len != 0)
3143c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_bdma, &sc->sc_bbuf);
3144c42a7b7eSSam Leffler 	if (sc->sc_txdma.dd_desc_len != 0)
3145c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3146c42a7b7eSSam Leffler 	if (sc->sc_rxdma.dd_desc_len != 0)
3147c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
31485591b213SSam Leffler }
31495591b213SSam Leffler 
31505591b213SSam Leffler static struct ieee80211_node *
315138c208f8SSam Leffler ath_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
31525591b213SSam Leffler {
315338c208f8SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
3154c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
3155c42a7b7eSSam Leffler 	const size_t space = sizeof(struct ath_node) + sc->sc_rc->arc_space;
3156c42a7b7eSSam Leffler 	struct ath_node *an;
3157c42a7b7eSSam Leffler 
3158c42a7b7eSSam Leffler 	an = malloc(space, M_80211_NODE, M_NOWAIT|M_ZERO);
3159c42a7b7eSSam Leffler 	if (an == NULL) {
3160c42a7b7eSSam Leffler 		/* XXX stat+msg */
3161de5af704SSam Leffler 		return NULL;
31625591b213SSam Leffler 	}
3163c42a7b7eSSam Leffler 	ath_rate_node_init(sc, an);
31645591b213SSam Leffler 
31653dd85b26SAdrian Chadd 	/* Setup the mutex - there's no associd yet so set the name to NULL */
31663dd85b26SAdrian Chadd 	snprintf(an->an_name, sizeof(an->an_name), "%s: node %p",
31673dd85b26SAdrian Chadd 	    device_get_nameunit(sc->sc_dev), an);
31683dd85b26SAdrian Chadd 	mtx_init(&an->an_mtx, an->an_name, NULL, MTX_DEF);
31693dd85b26SAdrian Chadd 
3170c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: an %p\n", __func__, an);
3171c42a7b7eSSam Leffler 	return &an->an_node;
3172c42a7b7eSSam Leffler }
3173c42a7b7eSSam Leffler 
31745591b213SSam Leffler static void
3175c42a7b7eSSam Leffler ath_node_free(struct ieee80211_node *ni)
31765591b213SSam Leffler {
3177c42a7b7eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
3178c42a7b7eSSam Leffler         struct ath_softc *sc = ic->ic_ifp->if_softc;
31791e774079SSam Leffler 
3180c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: ni %p\n", __func__, ni);
31813dd85b26SAdrian Chadd 	mtx_destroy(&ATH_NODE(ni)->an_mtx);
3182c42a7b7eSSam Leffler 	ath_rate_node_cleanup(sc, ATH_NODE(ni));
3183c42a7b7eSSam Leffler 	sc->sc_node_free(ni);
31845591b213SSam Leffler }
31855591b213SSam Leffler 
318668e8e04eSSam Leffler static void
318768e8e04eSSam Leffler ath_node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
318868e8e04eSSam Leffler {
318968e8e04eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
319068e8e04eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
319168e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
319268e8e04eSSam Leffler 
3193b032f27cSSam Leffler 	*rssi = ic->ic_node_getrssi(ni);
319459efa8b5SSam Leffler 	if (ni->ni_chan != IEEE80211_CHAN_ANYC)
319559efa8b5SSam Leffler 		*noise = ath_hal_getchannoise(ah, ni->ni_chan);
319659efa8b5SSam Leffler 	else
319768e8e04eSSam Leffler 		*noise = -95;		/* nominally correct */
319868e8e04eSSam Leffler }
319968e8e04eSSam Leffler 
32005591b213SSam Leffler static int
32015591b213SSam Leffler ath_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf)
32025591b213SSam Leffler {
32035591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
32045591b213SSam Leffler 	int error;
32055591b213SSam Leffler 	struct mbuf *m;
32065591b213SSam Leffler 	struct ath_desc *ds;
32075591b213SSam Leffler 
32085591b213SSam Leffler 	m = bf->bf_m;
32095591b213SSam Leffler 	if (m == NULL) {
32105591b213SSam Leffler 		/*
32115591b213SSam Leffler 		 * NB: by assigning a page to the rx dma buffer we
32125591b213SSam Leffler 		 * implicitly satisfy the Atheros requirement that
32135591b213SSam Leffler 		 * this buffer be cache-line-aligned and sized to be
32145591b213SSam Leffler 		 * multiple of the cache line size.  Not doing this
32155591b213SSam Leffler 		 * causes weird stuff to happen (for the 5210 at least).
32165591b213SSam Leffler 		 */
32175591b213SSam Leffler 		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
32185591b213SSam Leffler 		if (m == NULL) {
3219c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
3220c42a7b7eSSam Leffler 				"%s: no mbuf/cluster\n", __func__);
32215591b213SSam Leffler 			sc->sc_stats.ast_rx_nombuf++;
32225591b213SSam Leffler 			return ENOMEM;
32235591b213SSam Leffler 		}
32245591b213SSam Leffler 		m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
32255591b213SSam Leffler 
3226f9e6219bSSam Leffler 		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat,
3227c42a7b7eSSam Leffler 					     bf->bf_dmamap, m,
3228f9e6219bSSam Leffler 					     bf->bf_segs, &bf->bf_nseg,
32295591b213SSam Leffler 					     BUS_DMA_NOWAIT);
32305591b213SSam Leffler 		if (error != 0) {
3231c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
3232f9e6219bSSam Leffler 			    "%s: bus_dmamap_load_mbuf_sg failed; error %d\n",
3233c42a7b7eSSam Leffler 			    __func__, error);
32345591b213SSam Leffler 			sc->sc_stats.ast_rx_busdma++;
3235b2792ff6SSam Leffler 			m_freem(m);
32365591b213SSam Leffler 			return error;
32375591b213SSam Leffler 		}
3238d77367bfSSam Leffler 		KASSERT(bf->bf_nseg == 1,
3239d77367bfSSam Leffler 			("multi-segment packet; nseg %u", bf->bf_nseg));
3240b2792ff6SSam Leffler 		bf->bf_m = m;
32415591b213SSam Leffler 	}
32425591b213SSam Leffler 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREREAD);
32435591b213SSam Leffler 
324404e22a02SSam Leffler 	/*
324504e22a02SSam Leffler 	 * Setup descriptors.  For receive we always terminate
324604e22a02SSam Leffler 	 * the descriptor list with a self-linked entry so we'll
324704e22a02SSam Leffler 	 * not get overrun under high load (as can happen with a
3248c42a7b7eSSam Leffler 	 * 5212 when ANI processing enables PHY error frames).
324904e22a02SSam Leffler 	 *
325004e22a02SSam Leffler 	 * To insure the last descriptor is self-linked we create
325104e22a02SSam Leffler 	 * each descriptor as self-linked and add it to the end.  As
325204e22a02SSam Leffler 	 * each additional descriptor is added the previous self-linked
325304e22a02SSam Leffler 	 * entry is ``fixed'' naturally.  This should be safe even
325404e22a02SSam Leffler 	 * if DMA is happening.  When processing RX interrupts we
325504e22a02SSam Leffler 	 * never remove/process the last, self-linked, entry on the
325604e22a02SSam Leffler 	 * descriptor list.  This insures the hardware always has
325704e22a02SSam Leffler 	 * someplace to write a new frame.
325804e22a02SSam Leffler 	 */
32598a2a6beeSAdrian Chadd 	/*
32608a2a6beeSAdrian Chadd 	 * 11N: we can no longer afford to self link the last descriptor.
32618a2a6beeSAdrian Chadd 	 * MAC acknowledges BA status as long as it copies frames to host
32628a2a6beeSAdrian Chadd 	 * buffer (or rx fifo). This can incorrectly acknowledge packets
32638a2a6beeSAdrian Chadd 	 * to a sender if last desc is self-linked.
32648a2a6beeSAdrian Chadd 	 */
32655591b213SSam Leffler 	ds = bf->bf_desc;
32668a2a6beeSAdrian Chadd 	if (sc->sc_rxslink)
326704e22a02SSam Leffler 		ds->ds_link = bf->bf_daddr;	/* link to self */
32688a2a6beeSAdrian Chadd 	else
32698a2a6beeSAdrian Chadd 		ds->ds_link = 0;		/* terminate the list */
32705591b213SSam Leffler 	ds->ds_data = bf->bf_segs[0].ds_addr;
32715591b213SSam Leffler 	ath_hal_setuprxdesc(ah, ds
32725591b213SSam Leffler 		, m->m_len		/* buffer size */
32735591b213SSam Leffler 		, 0
32745591b213SSam Leffler 	);
32755591b213SSam Leffler 
32765591b213SSam Leffler 	if (sc->sc_rxlink != NULL)
32775591b213SSam Leffler 		*sc->sc_rxlink = bf->bf_daddr;
32785591b213SSam Leffler 	sc->sc_rxlink = &ds->ds_link;
32795591b213SSam Leffler 	return 0;
32805591b213SSam Leffler }
32815591b213SSam Leffler 
3282c42a7b7eSSam Leffler /*
328303ed599aSSam Leffler  * Extend 15-bit time stamp from rx descriptor to
32847b0c77ecSSam Leffler  * a full 64-bit TSF using the specified TSF.
328503ed599aSSam Leffler  */
328603ed599aSSam Leffler static __inline u_int64_t
3287fc4de9b7SAdrian Chadd ath_extend_tsf15(u_int32_t rstamp, u_int64_t tsf)
328803ed599aSSam Leffler {
328903ed599aSSam Leffler 	if ((tsf & 0x7fff) < rstamp)
329003ed599aSSam Leffler 		tsf -= 0x8000;
3291fc4de9b7SAdrian Chadd 
329203ed599aSSam Leffler 	return ((tsf &~ 0x7fff) | rstamp);
329303ed599aSSam Leffler }
329403ed599aSSam Leffler 
329503ed599aSSam Leffler /*
3296fc4de9b7SAdrian Chadd  * Extend 32-bit time stamp from rx descriptor to
3297fc4de9b7SAdrian Chadd  * a full 64-bit TSF using the specified TSF.
3298fc4de9b7SAdrian Chadd  */
3299fc4de9b7SAdrian Chadd static __inline u_int64_t
3300fc4de9b7SAdrian Chadd ath_extend_tsf32(u_int32_t rstamp, u_int64_t tsf)
3301fc4de9b7SAdrian Chadd {
3302fc4de9b7SAdrian Chadd 	u_int32_t tsf_low = tsf & 0xffffffff;
3303fc4de9b7SAdrian Chadd 	u_int64_t tsf64 = (tsf & ~0xffffffffULL) | rstamp;
3304fc4de9b7SAdrian Chadd 
3305fc4de9b7SAdrian Chadd 	if (rstamp > tsf_low && (rstamp - tsf_low > 0x10000000))
3306fc4de9b7SAdrian Chadd 		tsf64 -= 0x100000000ULL;
3307fc4de9b7SAdrian Chadd 
3308fc4de9b7SAdrian Chadd 	if (rstamp < tsf_low && (tsf_low - rstamp > 0x10000000))
3309fc4de9b7SAdrian Chadd 		tsf64 += 0x100000000ULL;
3310fc4de9b7SAdrian Chadd 
3311fc4de9b7SAdrian Chadd 	return tsf64;
3312fc4de9b7SAdrian Chadd }
3313fc4de9b7SAdrian Chadd 
3314fc4de9b7SAdrian Chadd /*
3315fc4de9b7SAdrian Chadd  * Extend the TSF from the RX descriptor to a full 64 bit TSF.
3316fc4de9b7SAdrian Chadd  * Earlier hardware versions only wrote the low 15 bits of the
3317fc4de9b7SAdrian Chadd  * TSF into the RX descriptor; later versions (AR5416 and up)
3318fc4de9b7SAdrian Chadd  * include the 32 bit TSF value.
3319fc4de9b7SAdrian Chadd  */
3320fc4de9b7SAdrian Chadd static __inline u_int64_t
3321fc4de9b7SAdrian Chadd ath_extend_tsf(struct ath_softc *sc, u_int32_t rstamp, u_int64_t tsf)
3322fc4de9b7SAdrian Chadd {
3323fc4de9b7SAdrian Chadd 	if (sc->sc_rxtsf32)
3324fc4de9b7SAdrian Chadd 		return ath_extend_tsf32(rstamp, tsf);
3325fc4de9b7SAdrian Chadd 	else
3326fc4de9b7SAdrian Chadd 		return ath_extend_tsf15(rstamp, tsf);
3327fc4de9b7SAdrian Chadd }
3328fc4de9b7SAdrian Chadd 
3329fc4de9b7SAdrian Chadd /*
3330c42a7b7eSSam Leffler  * Intercept management frames to collect beacon rssi data
3331c42a7b7eSSam Leffler  * and to do ibss merges.
3332c42a7b7eSSam Leffler  */
3333c42a7b7eSSam Leffler static void
3334b032f27cSSam Leffler ath_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
33355463c4a4SSam Leffler 	int subtype, int rssi, int nf)
3336c42a7b7eSSam Leffler {
3337b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
3338b032f27cSSam Leffler 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
3339c42a7b7eSSam Leffler 
3340c42a7b7eSSam Leffler 	/*
3341c42a7b7eSSam Leffler 	 * Call up first so subsequent work can use information
3342c42a7b7eSSam Leffler 	 * potentially stored in the node (e.g. for ibss merge).
3343c42a7b7eSSam Leffler 	 */
33445463c4a4SSam Leffler 	ATH_VAP(vap)->av_recv_mgmt(ni, m, subtype, rssi, nf);
3345c42a7b7eSSam Leffler 	switch (subtype) {
3346c42a7b7eSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON:
3347c42a7b7eSSam Leffler 		/* update rssi statistics for use by the hal */
3348ffa2cab6SSam Leffler 		ATH_RSSI_LPF(sc->sc_halstats.ns_avgbrssi, rssi);
334980d939bfSSam Leffler 		if (sc->sc_syncbeacon &&
3350b032f27cSSam Leffler 		    ni == vap->iv_bss && vap->iv_state == IEEE80211_S_RUN) {
335180d939bfSSam Leffler 			/*
335280d939bfSSam Leffler 			 * Resync beacon timers using the tsf of the beacon
335380d939bfSSam Leffler 			 * frame we just received.
335480d939bfSSam Leffler 			 */
3355b032f27cSSam Leffler 			ath_beacon_config(sc, vap);
335680d939bfSSam Leffler 		}
3357c42a7b7eSSam Leffler 		/* fall thru... */
3358c42a7b7eSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
3359b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_IBSS &&
3360b032f27cSSam Leffler 		    vap->iv_state == IEEE80211_S_RUN) {
33617041d50cSBernhard Schmidt 			uint32_t rstamp = sc->sc_lastrs->rs_tstamp;
3362fc4de9b7SAdrian Chadd 			uint64_t tsf = ath_extend_tsf(sc, rstamp,
33637b0c77ecSSam Leffler 				ath_hal_gettsf64(sc->sc_ah));
3364c42a7b7eSSam Leffler 			/*
3365c42a7b7eSSam Leffler 			 * Handle ibss merge as needed; check the tsf on the
3366c42a7b7eSSam Leffler 			 * frame before attempting the merge.  The 802.11 spec
3367c42a7b7eSSam Leffler 			 * says the station should change it's bssid to match
3368c42a7b7eSSam Leffler 			 * the oldest station with the same ssid, where oldest
3369f818612bSSam Leffler 			 * is determined by the tsf.  Note that hardware
3370f818612bSSam Leffler 			 * reconfiguration happens through callback to
337103ed599aSSam Leffler 			 * ath_newstate as the state machine will go from
337203ed599aSSam Leffler 			 * RUN -> RUN when this happens.
3373c42a7b7eSSam Leffler 			 */
337403ed599aSSam Leffler 			if (le64toh(ni->ni_tstamp.tsf) >= tsf) {
337503ed599aSSam Leffler 				DPRINTF(sc, ATH_DEBUG_STATE,
337633d7d80cSTai-hwa Liang 				    "ibss merge, rstamp %u tsf %ju "
337733d7d80cSTai-hwa Liang 				    "tstamp %ju\n", rstamp, (uintmax_t)tsf,
337833d7d80cSTai-hwa Liang 				    (uintmax_t)ni->ni_tstamp.tsf);
3379641b4d0bSSam Leffler 				(void) ieee80211_ibss_merge(ni);
3380c42a7b7eSSam Leffler 			}
338103ed599aSSam Leffler 		}
3382c42a7b7eSSam Leffler 		break;
3383c42a7b7eSSam Leffler 	}
3384c42a7b7eSSam Leffler }
3385c42a7b7eSSam Leffler 
3386c42a7b7eSSam Leffler /*
3387c42a7b7eSSam Leffler  * Set the default antenna.
3388c42a7b7eSSam Leffler  */
3389c42a7b7eSSam Leffler static void
3390c42a7b7eSSam Leffler ath_setdefantenna(struct ath_softc *sc, u_int antenna)
3391c42a7b7eSSam Leffler {
3392c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3393c42a7b7eSSam Leffler 
3394c42a7b7eSSam Leffler 	/* XXX block beacon interrupts */
3395c42a7b7eSSam Leffler 	ath_hal_setdefantenna(ah, antenna);
3396c42a7b7eSSam Leffler 	if (sc->sc_defant != antenna)
3397c42a7b7eSSam Leffler 		sc->sc_stats.ast_ant_defswitch++;
3398c42a7b7eSSam Leffler 	sc->sc_defant = antenna;
3399c42a7b7eSSam Leffler 	sc->sc_rxotherant = 0;
3400c42a7b7eSSam Leffler }
3401c42a7b7eSSam Leffler 
34025463c4a4SSam Leffler static void
3403b032f27cSSam Leffler ath_rx_tap(struct ifnet *ifp, struct mbuf *m,
340465f9edeeSSam Leffler 	const struct ath_rx_status *rs, u_int64_t tsf, int16_t nf)
34057b0c77ecSSam Leffler {
3406e387d629SSam Leffler #define	CHAN_HT20	htole32(IEEE80211_CHAN_HT20)
3407e387d629SSam Leffler #define	CHAN_HT40U	htole32(IEEE80211_CHAN_HT40U)
3408e387d629SSam Leffler #define	CHAN_HT40D	htole32(IEEE80211_CHAN_HT40D)
340946d4d74cSSam Leffler #define	CHAN_HT		(CHAN_HT20|CHAN_HT40U|CHAN_HT40D)
3410b032f27cSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
341146d4d74cSSam Leffler 	const HAL_RATE_TABLE *rt;
341246d4d74cSSam Leffler 	uint8_t rix;
34137b0c77ecSSam Leffler 
341446d4d74cSSam Leffler 	rt = sc->sc_currates;
341546d4d74cSSam Leffler 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
341646d4d74cSSam Leffler 	rix = rt->rateCodeToIndex[rs->rs_rate];
341768e8e04eSSam Leffler 	sc->sc_rx_th.wr_rate = sc->sc_hwmap[rix].ieeerate;
34187b0c77ecSSam Leffler 	sc->sc_rx_th.wr_flags = sc->sc_hwmap[rix].rxflags;
341946d4d74cSSam Leffler #ifdef AH_SUPPORT_AR5416
3420e387d629SSam Leffler 	sc->sc_rx_th.wr_chan_flags &= ~CHAN_HT;
342146d4d74cSSam Leffler 	if (sc->sc_rx_th.wr_rate & IEEE80211_RATE_MCS) {	/* HT rate */
342259efa8b5SSam Leffler 		struct ieee80211com *ic = ifp->if_l2com;
342359efa8b5SSam Leffler 
3424e387d629SSam Leffler 		if ((rs->rs_flags & HAL_RX_2040) == 0)
3425e387d629SSam Leffler 			sc->sc_rx_th.wr_chan_flags |= CHAN_HT20;
342659efa8b5SSam Leffler 		else if (IEEE80211_IS_CHAN_HT40U(ic->ic_curchan))
3427e387d629SSam Leffler 			sc->sc_rx_th.wr_chan_flags |= CHAN_HT40U;
3428e387d629SSam Leffler 		else
3429e387d629SSam Leffler 			sc->sc_rx_th.wr_chan_flags |= CHAN_HT40D;
343068e8e04eSSam Leffler 		if ((rs->rs_flags & HAL_RX_GI) == 0)
3431e387d629SSam Leffler 			sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
343268e8e04eSSam Leffler 	}
343368e8e04eSSam Leffler #endif
3434fc4de9b7SAdrian Chadd 	sc->sc_rx_th.wr_tsf = htole64(ath_extend_tsf(sc, rs->rs_tstamp, tsf));
343565f9edeeSSam Leffler 	if (rs->rs_status & HAL_RXERR_CRC)
34367b0c77ecSSam Leffler 		sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
34377b0c77ecSSam Leffler 	/* XXX propagate other error flags from descriptor */
34387b0c77ecSSam Leffler 	sc->sc_rx_th.wr_antnoise = nf;
34395463c4a4SSam Leffler 	sc->sc_rx_th.wr_antsignal = nf + rs->rs_rssi;
344065f9edeeSSam Leffler 	sc->sc_rx_th.wr_antenna = rs->rs_antenna;
344146d4d74cSSam Leffler #undef CHAN_HT
3442e387d629SSam Leffler #undef CHAN_HT20
3443e387d629SSam Leffler #undef CHAN_HT40U
3444e387d629SSam Leffler #undef CHAN_HT40D
34457b0c77ecSSam Leffler }
34467b0c77ecSSam Leffler 
34475591b213SSam Leffler static void
3448b032f27cSSam Leffler ath_handle_micerror(struct ieee80211com *ic,
3449b032f27cSSam Leffler 	struct ieee80211_frame *wh, int keyix)
3450b032f27cSSam Leffler {
3451b032f27cSSam Leffler 	struct ieee80211_node *ni;
3452b032f27cSSam Leffler 
3453b032f27cSSam Leffler 	/* XXX recheck MIC to deal w/ chips that lie */
3454b032f27cSSam Leffler 	/* XXX discard MIC errors on !data frames */
3455b032f27cSSam Leffler 	ni = ieee80211_find_rxnode(ic, (const struct ieee80211_frame_min *) wh);
3456b032f27cSSam Leffler 	if (ni != NULL) {
3457b032f27cSSam Leffler 		ieee80211_notify_michael_failure(ni->ni_vap, wh, keyix);
3458b032f27cSSam Leffler 		ieee80211_free_node(ni);
3459b032f27cSSam Leffler 	}
3460b032f27cSSam Leffler }
3461b032f27cSSam Leffler 
3462b032f27cSSam Leffler static void
34635591b213SSam Leffler ath_rx_proc(void *arg, int npending)
34645591b213SSam Leffler {
34658cec0ab9SSam Leffler #define	PA2DESC(_sc, _pa) \
3466c42a7b7eSSam Leffler 	((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
3467c42a7b7eSSam Leffler 		((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
34685591b213SSam Leffler 	struct ath_softc *sc = arg;
34695591b213SSam Leffler 	struct ath_buf *bf;
3470fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
3471b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
34725591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
34735591b213SSam Leffler 	struct ath_desc *ds;
347465f9edeeSSam Leffler 	struct ath_rx_status *rs;
34755591b213SSam Leffler 	struct mbuf *m;
34760a915fadSSam Leffler 	struct ieee80211_node *ni;
3477d7736e13SSam Leffler 	int len, type, ngood;
34785591b213SSam Leffler 	HAL_STATUS status;
34797b0c77ecSSam Leffler 	int16_t nf;
34807b0c77ecSSam Leffler 	u_int64_t tsf;
34815591b213SSam Leffler 
3482c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RX_PROC, "%s: pending %u\n", __func__, npending);
3483d7736e13SSam Leffler 	ngood = 0;
348459efa8b5SSam Leffler 	nf = ath_hal_getchannoise(ah, sc->sc_curchan);
348584784be1SSam Leffler 	sc->sc_stats.ast_rx_noise = nf;
34867b0c77ecSSam Leffler 	tsf = ath_hal_gettsf64(ah);
34875591b213SSam Leffler 	do {
3488*6b349e5aSAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_rxbuf);
34898a2a6beeSAdrian Chadd 		if (sc->sc_rxslink && bf == NULL) {	/* NB: shouldn't happen */
3490c42a7b7eSSam Leffler 			if_printf(ifp, "%s: no buffer!\n", __func__);
34915591b213SSam Leffler 			break;
34928a2a6beeSAdrian Chadd 		} else if (bf == NULL) {
34938a2a6beeSAdrian Chadd 			/*
34948a2a6beeSAdrian Chadd 			 * End of List:
34958a2a6beeSAdrian Chadd 			 * this can happen for non-self-linked RX chains
34968a2a6beeSAdrian Chadd 			 */
34978a2a6beeSAdrian Chadd 			sc->sc_stats.ast_rx_hitqueueend++;
34988a2a6beeSAdrian Chadd 			break;
34995591b213SSam Leffler 		}
3500b2792ff6SSam Leffler 		m = bf->bf_m;
3501b2792ff6SSam Leffler 		if (m == NULL) {		/* NB: shouldn't happen */
3502b2792ff6SSam Leffler 			/*
3503b2792ff6SSam Leffler 			 * If mbuf allocation failed previously there
3504b2792ff6SSam Leffler 			 * will be no mbuf; try again to re-populate it.
3505b2792ff6SSam Leffler 			 */
3506b2792ff6SSam Leffler 			/* XXX make debug msg */
3507b2792ff6SSam Leffler 			if_printf(ifp, "%s: no mbuf!\n", __func__);
3508*6b349e5aSAdrian Chadd 			TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
3509b2792ff6SSam Leffler 			goto rx_next;
3510b2792ff6SSam Leffler 		}
351104e22a02SSam Leffler 		ds = bf->bf_desc;
351204e22a02SSam Leffler 		if (ds->ds_link == bf->bf_daddr) {
351304e22a02SSam Leffler 			/* NB: never process the self-linked entry at the end */
3514f77057dbSAdrian Chadd 			sc->sc_stats.ast_rx_hitqueueend++;
351504e22a02SSam Leffler 			break;
351604e22a02SSam Leffler 		}
35178cec0ab9SSam Leffler 		/* XXX sync descriptor memory */
35188cec0ab9SSam Leffler 		/*
35198cec0ab9SSam Leffler 		 * Must provide the virtual address of the current
35208cec0ab9SSam Leffler 		 * descriptor, the physical address, and the virtual
35218cec0ab9SSam Leffler 		 * address of the next descriptor in the h/w chain.
35228cec0ab9SSam Leffler 		 * This allows the HAL to look ahead to see if the
35238cec0ab9SSam Leffler 		 * hardware is done with a descriptor by checking the
35248cec0ab9SSam Leffler 		 * done bit in the following descriptor and the address
35258cec0ab9SSam Leffler 		 * of the current descriptor the DMA engine is working
35268cec0ab9SSam Leffler 		 * on.  All this is necessary because of our use of
35278cec0ab9SSam Leffler 		 * a self-linked list to avoid rx overruns.
35288cec0ab9SSam Leffler 		 */
352965f9edeeSSam Leffler 		rs = &bf->bf_status.ds_rxstat;
35308cec0ab9SSam Leffler 		status = ath_hal_rxprocdesc(ah, ds,
353165f9edeeSSam Leffler 				bf->bf_daddr, PA2DESC(sc, ds->ds_link), rs);
3532a585a9a1SSam Leffler #ifdef ATH_DEBUG
3533c42a7b7eSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_RECV_DESC)
35346902009eSSam Leffler 			ath_printrxbuf(sc, bf, 0, status == HAL_OK);
35355591b213SSam Leffler #endif
35365591b213SSam Leffler 		if (status == HAL_EINPROGRESS)
35375591b213SSam Leffler 			break;
3538*6b349e5aSAdrian Chadd 
3539*6b349e5aSAdrian Chadd 		TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
3540f9aa1d90SAdrian Chadd 
3541f9aa1d90SAdrian Chadd 		/* These aren't specifically errors */
3542f9aa1d90SAdrian Chadd 		if (rs->rs_flags & HAL_RX_GI)
3543f9aa1d90SAdrian Chadd 			sc->sc_stats.ast_rx_halfgi++;
3544f9aa1d90SAdrian Chadd 		if (rs->rs_flags & HAL_RX_2040)
3545f9aa1d90SAdrian Chadd 			sc->sc_stats.ast_rx_2040++;
3546f9aa1d90SAdrian Chadd 		if (rs->rs_flags & HAL_RX_DELIM_CRC_PRE)
3547f9aa1d90SAdrian Chadd 			sc->sc_stats.ast_rx_pre_crc_err++;
3548f9aa1d90SAdrian Chadd 		if (rs->rs_flags & HAL_RX_DELIM_CRC_POST)
3549f9aa1d90SAdrian Chadd 			sc->sc_stats.ast_rx_post_crc_err++;
3550f9aa1d90SAdrian Chadd 		if (rs->rs_flags & HAL_RX_DECRYPT_BUSY)
3551f9aa1d90SAdrian Chadd 			sc->sc_stats.ast_rx_decrypt_busy_err++;
3552f9aa1d90SAdrian Chadd 		if (rs->rs_flags & HAL_RX_HI_RX_CHAIN)
3553f9aa1d90SAdrian Chadd 			sc->sc_stats.ast_rx_hi_rx_chain++;
3554f9aa1d90SAdrian Chadd 
355568e8e04eSSam Leffler 		if (rs->rs_status != 0) {
355665f9edeeSSam Leffler 			if (rs->rs_status & HAL_RXERR_CRC)
35575591b213SSam Leffler 				sc->sc_stats.ast_rx_crcerr++;
355865f9edeeSSam Leffler 			if (rs->rs_status & HAL_RXERR_FIFO)
35595591b213SSam Leffler 				sc->sc_stats.ast_rx_fifoerr++;
356065f9edeeSSam Leffler 			if (rs->rs_status & HAL_RXERR_PHY) {
35615591b213SSam Leffler 				sc->sc_stats.ast_rx_phyerr++;
356248237774SAdrian Chadd 				/* Process DFS radar events */
3563373815efSAdrian Chadd 				if ((rs->rs_phyerr == HAL_PHYERR_RADAR) ||
3564373815efSAdrian Chadd 				    (rs->rs_phyerr == HAL_PHYERR_FALSE_RADAR_EXT)) {
3565373815efSAdrian Chadd 					/* Since we're touching the frame data, sync it */
3566373815efSAdrian Chadd 					bus_dmamap_sync(sc->sc_dmat,
3567373815efSAdrian Chadd 					    bf->bf_dmamap,
3568373815efSAdrian Chadd 					    BUS_DMASYNC_POSTREAD);
3569373815efSAdrian Chadd 					/* Now pass it to the radar processing code */
35707e5eb44dSAdrian Chadd 					ath_dfs_process_phy_err(sc, mtod(m, char *), tsf, rs);
3571373815efSAdrian Chadd 				}
357248237774SAdrian Chadd 
3573f9aa1d90SAdrian Chadd 				/* Be suitably paranoid about receiving phy errors out of the stats array bounds */
3574f9aa1d90SAdrian Chadd 				if (rs->rs_phyerr < 64)
3575f9aa1d90SAdrian Chadd 					sc->sc_stats.ast_rx_phy[rs->rs_phyerr]++;
357668e8e04eSSam Leffler 				goto rx_error;	/* NB: don't count in ierrors */
3577c42a7b7eSSam Leffler 			}
357865f9edeeSSam Leffler 			if (rs->rs_status & HAL_RXERR_DECRYPT) {
357985643802SSam Leffler 				/*
3580c42a7b7eSSam Leffler 				 * Decrypt error.  If the error occurred
3581c42a7b7eSSam Leffler 				 * because there was no hardware key, then
3582c42a7b7eSSam Leffler 				 * let the frame through so the upper layers
3583c42a7b7eSSam Leffler 				 * can process it.  This is necessary for 5210
3584c42a7b7eSSam Leffler 				 * parts which have no way to setup a ``clear''
3585c42a7b7eSSam Leffler 				 * key cache entry.
3586c42a7b7eSSam Leffler 				 *
3587c42a7b7eSSam Leffler 				 * XXX do key cache faulting
358885643802SSam Leffler 				 */
358965f9edeeSSam Leffler 				if (rs->rs_keyix == HAL_RXKEYIX_INVALID)
3590c42a7b7eSSam Leffler 					goto rx_accept;
3591c42a7b7eSSam Leffler 				sc->sc_stats.ast_rx_badcrypt++;
35925591b213SSam Leffler 			}
359365f9edeeSSam Leffler 			if (rs->rs_status & HAL_RXERR_MIC) {
3594c42a7b7eSSam Leffler 				sc->sc_stats.ast_rx_badmic++;
3595c42a7b7eSSam Leffler 				/*
3596c42a7b7eSSam Leffler 				 * Do minimal work required to hand off
35975463c4a4SSam Leffler 				 * the 802.11 header for notification.
3598c42a7b7eSSam Leffler 				 */
3599c42a7b7eSSam Leffler 				/* XXX frag's and qos frames */
360065f9edeeSSam Leffler 				len = rs->rs_datalen;
3601c42a7b7eSSam Leffler 				if (len >= sizeof (struct ieee80211_frame)) {
3602c42a7b7eSSam Leffler 					bus_dmamap_sync(sc->sc_dmat,
3603c42a7b7eSSam Leffler 					    bf->bf_dmamap,
3604c42a7b7eSSam Leffler 					    BUS_DMASYNC_POSTREAD);
3605b032f27cSSam Leffler 					ath_handle_micerror(ic,
3606c42a7b7eSSam Leffler 					    mtod(m, struct ieee80211_frame *),
36070ab4040aSSam Leffler 					    sc->sc_splitmic ?
3608b032f27cSSam Leffler 						rs->rs_keyix-32 : rs->rs_keyix);
3609c42a7b7eSSam Leffler 				}
3610c42a7b7eSSam Leffler 			}
3611c42a7b7eSSam Leffler 			ifp->if_ierrors++;
361268e8e04eSSam Leffler rx_error:
361368e8e04eSSam Leffler 			/*
361468e8e04eSSam Leffler 			 * Cleanup any pending partial frame.
361568e8e04eSSam Leffler 			 */
361668e8e04eSSam Leffler 			if (sc->sc_rxpending != NULL) {
361768e8e04eSSam Leffler 				m_freem(sc->sc_rxpending);
361868e8e04eSSam Leffler 				sc->sc_rxpending = NULL;
361968e8e04eSSam Leffler 			}
3620c42a7b7eSSam Leffler 			/*
36217b0c77ecSSam Leffler 			 * When a tap is present pass error frames
36227b0c77ecSSam Leffler 			 * that have been requested.  By default we
36237b0c77ecSSam Leffler 			 * pass decrypt+mic errors but others may be
36247b0c77ecSSam Leffler 			 * interesting (e.g. crc).
3625c42a7b7eSSam Leffler 			 */
36265463c4a4SSam Leffler 			if (ieee80211_radiotap_active(ic) &&
362765f9edeeSSam Leffler 			    (rs->rs_status & sc->sc_monpass)) {
36287b0c77ecSSam Leffler 				bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
36297b0c77ecSSam Leffler 				    BUS_DMASYNC_POSTREAD);
36307b0c77ecSSam Leffler 				/* NB: bpf needs the mbuf length setup */
363165f9edeeSSam Leffler 				len = rs->rs_datalen;
36327b0c77ecSSam Leffler 				m->m_pkthdr.len = m->m_len = len;
3633dcfd99a7SAdrian Chadd 				bf->bf_m = NULL;
36345463c4a4SSam Leffler 				ath_rx_tap(ifp, m, rs, tsf, nf);
36355463c4a4SSam Leffler 				ieee80211_radiotap_rx_all(ic, m);
3636dcfd99a7SAdrian Chadd 				m_freem(m);
36377b0c77ecSSam Leffler 			}
36387b0c77ecSSam Leffler 			/* XXX pass MIC errors up for s/w reclaculation */
36395591b213SSam Leffler 			goto rx_next;
36405591b213SSam Leffler 		}
3641c42a7b7eSSam Leffler rx_accept:
3642c42a7b7eSSam Leffler 		/*
3643c42a7b7eSSam Leffler 		 * Sync and unmap the frame.  At this point we're
3644c42a7b7eSSam Leffler 		 * committed to passing the mbuf somewhere so clear
3645c66c48cbSSam Leffler 		 * bf_m; this means a new mbuf must be allocated
3646c42a7b7eSSam Leffler 		 * when the rx descriptor is setup again to receive
3647c42a7b7eSSam Leffler 		 * another frame.
3648c42a7b7eSSam Leffler 		 */
36495591b213SSam Leffler 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
36505591b213SSam Leffler 		    BUS_DMASYNC_POSTREAD);
36515591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
36525591b213SSam Leffler 		bf->bf_m = NULL;
3653c42a7b7eSSam Leffler 
365465f9edeeSSam Leffler 		len = rs->rs_datalen;
365568e8e04eSSam Leffler 		m->m_len = len;
365668e8e04eSSam Leffler 
365768e8e04eSSam Leffler 		if (rs->rs_more) {
365868e8e04eSSam Leffler 			/*
365968e8e04eSSam Leffler 			 * Frame spans multiple descriptors; save
366068e8e04eSSam Leffler 			 * it for the next completed descriptor, it
366168e8e04eSSam Leffler 			 * will be used to construct a jumbogram.
366268e8e04eSSam Leffler 			 */
366368e8e04eSSam Leffler 			if (sc->sc_rxpending != NULL) {
366468e8e04eSSam Leffler 				/* NB: max frame size is currently 2 clusters */
366568e8e04eSSam Leffler 				sc->sc_stats.ast_rx_toobig++;
366668e8e04eSSam Leffler 				m_freem(sc->sc_rxpending);
366768e8e04eSSam Leffler 			}
366868e8e04eSSam Leffler 			m->m_pkthdr.rcvif = ifp;
366968e8e04eSSam Leffler 			m->m_pkthdr.len = len;
367068e8e04eSSam Leffler 			sc->sc_rxpending = m;
367168e8e04eSSam Leffler 			goto rx_next;
367268e8e04eSSam Leffler 		} else if (sc->sc_rxpending != NULL) {
367368e8e04eSSam Leffler 			/*
367468e8e04eSSam Leffler 			 * This is the second part of a jumbogram,
367568e8e04eSSam Leffler 			 * chain it to the first mbuf, adjust the
367668e8e04eSSam Leffler 			 * frame length, and clear the rxpending state.
367768e8e04eSSam Leffler 			 */
367868e8e04eSSam Leffler 			sc->sc_rxpending->m_next = m;
367968e8e04eSSam Leffler 			sc->sc_rxpending->m_pkthdr.len += len;
368068e8e04eSSam Leffler 			m = sc->sc_rxpending;
368168e8e04eSSam Leffler 			sc->sc_rxpending = NULL;
368268e8e04eSSam Leffler 		} else {
368368e8e04eSSam Leffler 			/*
368468e8e04eSSam Leffler 			 * Normal single-descriptor receive; setup
368568e8e04eSSam Leffler 			 * the rcvif and packet length.
368668e8e04eSSam Leffler 			 */
368768e8e04eSSam Leffler 			m->m_pkthdr.rcvif = ifp;
368868e8e04eSSam Leffler 			m->m_pkthdr.len = len;
368968e8e04eSSam Leffler 		}
369073454c73SSam Leffler 
3691b032f27cSSam Leffler 		ifp->if_ipackets++;
369265f9edeeSSam Leffler 		sc->sc_stats.ast_ant_rx[rs->rs_antenna]++;
3693c42a7b7eSSam Leffler 
36945463c4a4SSam Leffler 		/*
36955463c4a4SSam Leffler 		 * Populate the rx status block.  When there are bpf
36965463c4a4SSam Leffler 		 * listeners we do the additional work to provide
36975463c4a4SSam Leffler 		 * complete status.  Otherwise we fill in only the
36985463c4a4SSam Leffler 		 * material required by ieee80211_input.  Note that
36995463c4a4SSam Leffler 		 * noise setting is filled in above.
37005463c4a4SSam Leffler 		 */
37015463c4a4SSam Leffler 		if (ieee80211_radiotap_active(ic))
37025463c4a4SSam Leffler 			ath_rx_tap(ifp, m, rs, tsf, nf);
37030a915fadSSam Leffler 
37045591b213SSam Leffler 		/*
3705c42a7b7eSSam Leffler 		 * From this point on we assume the frame is at least
3706c42a7b7eSSam Leffler 		 * as large as ieee80211_frame_min; verify that.
37075591b213SSam Leffler 		 */
3708c42a7b7eSSam Leffler 		if (len < IEEE80211_MIN_LEN) {
37095463c4a4SSam Leffler 			if (!ieee80211_radiotap_active(ic)) {
37105463c4a4SSam Leffler 				DPRINTF(sc, ATH_DEBUG_RECV,
37115463c4a4SSam Leffler 				    "%s: short packet %d\n", __func__, len);
3712c42a7b7eSSam Leffler 				sc->sc_stats.ast_rx_tooshort++;
37135463c4a4SSam Leffler 			} else {
37145463c4a4SSam Leffler 				/* NB: in particular this captures ack's */
37155463c4a4SSam Leffler 				ieee80211_radiotap_rx_all(ic, m);
37165463c4a4SSam Leffler 			}
3717c42a7b7eSSam Leffler 			m_freem(m);
3718c42a7b7eSSam Leffler 			goto rx_next;
37195591b213SSam Leffler 		}
37200a915fadSSam Leffler 
3721c42a7b7eSSam Leffler 		if (IFF_DUMPPKTS(sc, ATH_DEBUG_RECV)) {
372246d4d74cSSam Leffler 			const HAL_RATE_TABLE *rt = sc->sc_currates;
372346d4d74cSSam Leffler 			uint8_t rix = rt->rateCodeToIndex[rs->rs_rate];
372446d4d74cSSam Leffler 
372568e8e04eSSam Leffler 			ieee80211_dump_pkt(ic, mtod(m, caddr_t), len,
372646d4d74cSSam Leffler 			    sc->sc_hwmap[rix].ieeerate, rs->rs_rssi);
3727c42a7b7eSSam Leffler 		}
3728c42a7b7eSSam Leffler 
3729c42a7b7eSSam Leffler 		m_adj(m, -IEEE80211_CRC_LEN);
3730de5af704SSam Leffler 
3731de5af704SSam Leffler 		/*
3732c42a7b7eSSam Leffler 		 * Locate the node for sender, track state, and then
3733c42a7b7eSSam Leffler 		 * pass the (referenced) node up to the 802.11 layer
3734c42a7b7eSSam Leffler 		 * for its use.
3735c42a7b7eSSam Leffler 		 */
3736c1225b52SSam Leffler 		ni = ieee80211_find_rxnode_withkey(ic,
3737c1225b52SSam Leffler 			mtod(m, const struct ieee80211_frame_min *),
373865f9edeeSSam Leffler 			rs->rs_keyix == HAL_RXKEYIX_INVALID ?
373965f9edeeSSam Leffler 				IEEE80211_KEYIX_NONE : rs->rs_keyix);
37407041d50cSBernhard Schmidt 		sc->sc_lastrs = rs;
3741a07e9ddbSAdrian Chadd 
3742a07e9ddbSAdrian Chadd 		if (rs->rs_isaggr)
3743a07e9ddbSAdrian Chadd 			sc->sc_stats.ast_rx_agg++;
3744a07e9ddbSAdrian Chadd 
3745a07e9ddbSAdrian Chadd 		if (ni != NULL) {
3746b032f27cSSam Leffler 			/*
3747e57539afSAdrian Chadd  			 * Only punt packets for ampdu reorder processing for
3748e57539afSAdrian Chadd 			 * 11n nodes; net80211 enforces that M_AMPDU is only
3749e57539afSAdrian Chadd 			 * set for 11n nodes.
375000fc8705SAdrian Chadd  			 */
375100fc8705SAdrian Chadd 			if (ni->ni_flags & IEEE80211_NODE_HT)
375200fc8705SAdrian Chadd 				m->m_flags |= M_AMPDU;
375300fc8705SAdrian Chadd 
375400fc8705SAdrian Chadd 			/*
3755b032f27cSSam Leffler 			 * Sending station is known, dispatch directly.
3756b032f27cSSam Leffler 			 */
37575463c4a4SSam Leffler 			type = ieee80211_input(ni, m, rs->rs_rssi, nf);
3758b032f27cSSam Leffler 			ieee80211_free_node(ni);
3759b032f27cSSam Leffler 			/*
3760b032f27cSSam Leffler 			 * Arrange to update the last rx timestamp only for
3761b032f27cSSam Leffler 			 * frames from our ap when operating in station mode.
3762b032f27cSSam Leffler 			 * This assumes the rx key is always setup when
3763b032f27cSSam Leffler 			 * associated.
3764b032f27cSSam Leffler 			 */
3765b032f27cSSam Leffler 			if (ic->ic_opmode == IEEE80211_M_STA &&
3766b032f27cSSam Leffler 			    rs->rs_keyix != HAL_RXKEYIX_INVALID)
3767b032f27cSSam Leffler 				ngood++;
3768b032f27cSSam Leffler 		} else {
37695463c4a4SSam Leffler 			type = ieee80211_input_all(ic, m, rs->rs_rssi, nf);
3770b032f27cSSam Leffler 		}
3771c42a7b7eSSam Leffler 		/*
3772c42a7b7eSSam Leffler 		 * Track rx rssi and do any rx antenna management.
3773de5af704SSam Leffler 		 */
377465f9edeeSSam Leffler 		ATH_RSSI_LPF(sc->sc_halstats.ns_avgrssi, rs->rs_rssi);
3775c42a7b7eSSam Leffler 		if (sc->sc_diversity) {
3776c42a7b7eSSam Leffler 			/*
3777c42a7b7eSSam Leffler 			 * When using fast diversity, change the default rx
3778c42a7b7eSSam Leffler 			 * antenna if diversity chooses the other antenna 3
3779c42a7b7eSSam Leffler 			 * times in a row.
3780c42a7b7eSSam Leffler 			 */
378165f9edeeSSam Leffler 			if (sc->sc_defant != rs->rs_antenna) {
3782c42a7b7eSSam Leffler 				if (++sc->sc_rxotherant >= 3)
378365f9edeeSSam Leffler 					ath_setdefantenna(sc, rs->rs_antenna);
3784c42a7b7eSSam Leffler 			} else
3785c42a7b7eSSam Leffler 				sc->sc_rxotherant = 0;
3786c42a7b7eSSam Leffler 		}
3787235ab70eSAdrian Chadd 
3788235ab70eSAdrian Chadd 		/* Newer school diversity - kite specific for now */
3789235ab70eSAdrian Chadd 		/* XXX perhaps migrate the normal diversity code to this? */
3790235ab70eSAdrian Chadd 		if ((ah)->ah_rxAntCombDiversity)
3791235ab70eSAdrian Chadd 			(*(ah)->ah_rxAntCombDiversity)(ah, rs, ticks, hz);
3792235ab70eSAdrian Chadd 
37933e50ec2cSSam Leffler 		if (sc->sc_softled) {
37943e50ec2cSSam Leffler 			/*
37953e50ec2cSSam Leffler 			 * Blink for any data frame.  Otherwise do a
37963e50ec2cSSam Leffler 			 * heartbeat-style blink when idle.  The latter
37973e50ec2cSSam Leffler 			 * is mainly for station mode where we depend on
37983e50ec2cSSam Leffler 			 * periodic beacon frames to trigger the poll event.
37993e50ec2cSSam Leffler 			 */
380031640eb7SSam Leffler 			if (type == IEEE80211_FC0_TYPE_DATA) {
380146d4d74cSSam Leffler 				const HAL_RATE_TABLE *rt = sc->sc_currates;
380246d4d74cSSam Leffler 				ath_led_event(sc,
380346d4d74cSSam Leffler 				    rt->rateCodeToIndex[rs->rs_rate]);
38043e50ec2cSSam Leffler 			} else if (ticks - sc->sc_ledevent >= sc->sc_ledidle)
380546d4d74cSSam Leffler 				ath_led_event(sc, 0);
38063e50ec2cSSam Leffler 		}
38075591b213SSam Leffler rx_next:
3808*6b349e5aSAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
38095591b213SSam Leffler 	} while (ath_rxbuf_init(sc, bf) == 0);
38105591b213SSam Leffler 
3811c42a7b7eSSam Leffler 	/* rx signal state monitoring */
381259efa8b5SSam Leffler 	ath_hal_rxmonitor(ah, &sc->sc_halstats, sc->sc_curchan);
3813d7736e13SSam Leffler 	if (ngood)
3814d7736e13SSam Leffler 		sc->sc_lastrx = tsf;
3815b5f4adb3SSam Leffler 
381648237774SAdrian Chadd 	/* Queue DFS tasklet if needed */
381748237774SAdrian Chadd 	if (ath_dfs_tasklet_needed(sc, sc->sc_curchan))
381848237774SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_dfstask);
381948237774SAdrian Chadd 
38201fdadc0fSAdrian Chadd 	/*
38211fdadc0fSAdrian Chadd 	 * Now that all the RX frames were handled that
38221fdadc0fSAdrian Chadd 	 * need to be handled, kick the PCU if there's
38231fdadc0fSAdrian Chadd 	 * been an RXEOL condition.
38241fdadc0fSAdrian Chadd 	 */
38251fdadc0fSAdrian Chadd 	if (sc->sc_kickpcu) {
38261fdadc0fSAdrian Chadd 		sc->sc_kickpcu = 0;
38271fdadc0fSAdrian Chadd 		ath_stoprecv(sc);
38281fdadc0fSAdrian Chadd 		sc->sc_imask |= (HAL_INT_RXEOL | HAL_INT_RXORN);
38291fdadc0fSAdrian Chadd 		if (ath_startrecv(sc) != 0) {
38301fdadc0fSAdrian Chadd 			if_printf(ifp,
38311fdadc0fSAdrian Chadd 			    "%s: couldn't restart RX after RXEOL; resetting\n",
38321fdadc0fSAdrian Chadd 			    __func__);
38331fdadc0fSAdrian Chadd 			ath_reset(ifp);
38341fdadc0fSAdrian Chadd 			return;
38351fdadc0fSAdrian Chadd 		}
38361fdadc0fSAdrian Chadd 		ath_hal_intrset(ah, sc->sc_imask);
38371fdadc0fSAdrian Chadd 	}
38381fdadc0fSAdrian Chadd 
3839339ccfb3SSam Leffler 	if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0) {
3840339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
384104f19fd6SSam Leffler 		ieee80211_ff_age_all(ic, 100);
3842339ccfb3SSam Leffler #endif
3843339ccfb3SSam Leffler 		if (!IFQ_IS_EMPTY(&ifp->if_snd))
3844cd196bb2SSam Leffler 			ath_start(ifp);
3845339ccfb3SSam Leffler 	}
38468cec0ab9SSam Leffler #undef PA2DESC
38475591b213SSam Leffler }
38485591b213SSam Leffler 
3849622b3fd2SSam Leffler static void
3850622b3fd2SSam Leffler ath_txq_init(struct ath_softc *sc, struct ath_txq *txq, int qnum)
3851622b3fd2SSam Leffler {
3852622b3fd2SSam Leffler 	txq->axq_qnum = qnum;
3853339ccfb3SSam Leffler 	txq->axq_ac = 0;
3854622b3fd2SSam Leffler 	txq->axq_depth = 0;
3855622b3fd2SSam Leffler 	txq->axq_intrcnt = 0;
3856622b3fd2SSam Leffler 	txq->axq_link = NULL;
3857*6b349e5aSAdrian Chadd 	txq->axq_softc = sc;
3858*6b349e5aSAdrian Chadd 	TAILQ_INIT(&txq->axq_q);
3859*6b349e5aSAdrian Chadd 	TAILQ_INIT(&txq->axq_tidq);
3860622b3fd2SSam Leffler 	ATH_TXQ_LOCK_INIT(sc, txq);
3861622b3fd2SSam Leffler }
3862622b3fd2SSam Leffler 
38635591b213SSam Leffler /*
3864c42a7b7eSSam Leffler  * Setup a h/w transmit queue.
38655591b213SSam Leffler  */
3866c42a7b7eSSam Leffler static struct ath_txq *
3867c42a7b7eSSam Leffler ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
3868c42a7b7eSSam Leffler {
3869c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
3870c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3871c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
3872c42a7b7eSSam Leffler 	int qnum;
3873c42a7b7eSSam Leffler 
3874c42a7b7eSSam Leffler 	memset(&qi, 0, sizeof(qi));
3875c42a7b7eSSam Leffler 	qi.tqi_subtype = subtype;
3876c42a7b7eSSam Leffler 	qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
3877c42a7b7eSSam Leffler 	qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
3878c42a7b7eSSam Leffler 	qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
3879c42a7b7eSSam Leffler 	/*
3880c42a7b7eSSam Leffler 	 * Enable interrupts only for EOL and DESC conditions.
3881c42a7b7eSSam Leffler 	 * We mark tx descriptors to receive a DESC interrupt
3882c42a7b7eSSam Leffler 	 * when a tx queue gets deep; otherwise waiting for the
3883c42a7b7eSSam Leffler 	 * EOL to reap descriptors.  Note that this is done to
3884c42a7b7eSSam Leffler 	 * reduce interrupt load and this only defers reaping
3885c42a7b7eSSam Leffler 	 * descriptors, never transmitting frames.  Aside from
3886c42a7b7eSSam Leffler 	 * reducing interrupts this also permits more concurrency.
3887c42a7b7eSSam Leffler 	 * The only potential downside is if the tx queue backs
3888c42a7b7eSSam Leffler 	 * up in which case the top half of the kernel may backup
3889c42a7b7eSSam Leffler 	 * due to a lack of tx descriptors.
3890c42a7b7eSSam Leffler 	 */
3891bd5a9920SSam Leffler 	qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE | HAL_TXQ_TXDESCINT_ENABLE;
3892c42a7b7eSSam Leffler 	qnum = ath_hal_setuptxqueue(ah, qtype, &qi);
3893c42a7b7eSSam Leffler 	if (qnum == -1) {
3894c42a7b7eSSam Leffler 		/*
3895c42a7b7eSSam Leffler 		 * NB: don't print a message, this happens
3896a614e076SSam Leffler 		 * normally on parts with too few tx queues
3897c42a7b7eSSam Leffler 		 */
3898c42a7b7eSSam Leffler 		return NULL;
3899c42a7b7eSSam Leffler 	}
3900c42a7b7eSSam Leffler 	if (qnum >= N(sc->sc_txq)) {
39016891c875SPeter Wemm 		device_printf(sc->sc_dev,
39026891c875SPeter Wemm 			"hal qnum %u out of range, max %zu!\n",
3903c42a7b7eSSam Leffler 			qnum, N(sc->sc_txq));
3904c42a7b7eSSam Leffler 		ath_hal_releasetxqueue(ah, qnum);
3905c42a7b7eSSam Leffler 		return NULL;
3906c42a7b7eSSam Leffler 	}
3907c42a7b7eSSam Leffler 	if (!ATH_TXQ_SETUP(sc, qnum)) {
3908622b3fd2SSam Leffler 		ath_txq_init(sc, &sc->sc_txq[qnum], qnum);
3909c42a7b7eSSam Leffler 		sc->sc_txqsetup |= 1<<qnum;
3910c42a7b7eSSam Leffler 	}
3911c42a7b7eSSam Leffler 	return &sc->sc_txq[qnum];
3912c42a7b7eSSam Leffler #undef N
3913c42a7b7eSSam Leffler }
3914c42a7b7eSSam Leffler 
3915c42a7b7eSSam Leffler /*
3916c42a7b7eSSam Leffler  * Setup a hardware data transmit queue for the specified
3917c42a7b7eSSam Leffler  * access control.  The hal may not support all requested
3918c42a7b7eSSam Leffler  * queues in which case it will return a reference to a
3919c42a7b7eSSam Leffler  * previously setup queue.  We record the mapping from ac's
3920c42a7b7eSSam Leffler  * to h/w queues for use by ath_tx_start and also track
3921c42a7b7eSSam Leffler  * the set of h/w queues being used to optimize work in the
3922c42a7b7eSSam Leffler  * transmit interrupt handler and related routines.
3923c42a7b7eSSam Leffler  */
3924c42a7b7eSSam Leffler static int
3925c42a7b7eSSam Leffler ath_tx_setup(struct ath_softc *sc, int ac, int haltype)
3926c42a7b7eSSam Leffler {
3927c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
3928c42a7b7eSSam Leffler 	struct ath_txq *txq;
3929c42a7b7eSSam Leffler 
3930c42a7b7eSSam Leffler 	if (ac >= N(sc->sc_ac2q)) {
39316891c875SPeter Wemm 		device_printf(sc->sc_dev, "AC %u out of range, max %zu!\n",
3932c42a7b7eSSam Leffler 			ac, N(sc->sc_ac2q));
3933c42a7b7eSSam Leffler 		return 0;
3934c42a7b7eSSam Leffler 	}
3935c42a7b7eSSam Leffler 	txq = ath_txq_setup(sc, HAL_TX_QUEUE_DATA, haltype);
3936c42a7b7eSSam Leffler 	if (txq != NULL) {
3937339ccfb3SSam Leffler 		txq->axq_ac = ac;
3938c42a7b7eSSam Leffler 		sc->sc_ac2q[ac] = txq;
3939c42a7b7eSSam Leffler 		return 1;
3940c42a7b7eSSam Leffler 	} else
3941c42a7b7eSSam Leffler 		return 0;
3942c42a7b7eSSam Leffler #undef N
3943c42a7b7eSSam Leffler }
3944c42a7b7eSSam Leffler 
3945c42a7b7eSSam Leffler /*
3946c42a7b7eSSam Leffler  * Update WME parameters for a transmit queue.
3947c42a7b7eSSam Leffler  */
3948c42a7b7eSSam Leffler static int
3949c42a7b7eSSam Leffler ath_txq_update(struct ath_softc *sc, int ac)
3950c42a7b7eSSam Leffler {
3951c42a7b7eSSam Leffler #define	ATH_EXPONENT_TO_VALUE(v)	((1<<v)-1)
3952c42a7b7eSSam Leffler #define	ATH_TXOP_TO_US(v)		(v<<5)
3953b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
3954b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
3955c42a7b7eSSam Leffler 	struct ath_txq *txq = sc->sc_ac2q[ac];
3956c42a7b7eSSam Leffler 	struct wmeParams *wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
3957c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3958c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
3959c42a7b7eSSam Leffler 
3960c42a7b7eSSam Leffler 	ath_hal_gettxqueueprops(ah, txq->axq_qnum, &qi);
3961584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
396210ad9a77SSam Leffler 	if (sc->sc_tdma) {
396310ad9a77SSam Leffler 		/*
396410ad9a77SSam Leffler 		 * AIFS is zero so there's no pre-transmit wait.  The
396510ad9a77SSam Leffler 		 * burst time defines the slot duration and is configured
396609be6601SSam Leffler 		 * through net80211.  The QCU is setup to not do post-xmit
396710ad9a77SSam Leffler 		 * back off, lockout all lower-priority QCU's, and fire
396810ad9a77SSam Leffler 		 * off the DMA beacon alert timer which is setup based
396910ad9a77SSam Leffler 		 * on the slot configuration.
397010ad9a77SSam Leffler 		 */
397110ad9a77SSam Leffler 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
397210ad9a77SSam Leffler 			      | HAL_TXQ_TXERRINT_ENABLE
397310ad9a77SSam Leffler 			      | HAL_TXQ_TXURNINT_ENABLE
397410ad9a77SSam Leffler 			      | HAL_TXQ_TXEOLINT_ENABLE
397510ad9a77SSam Leffler 			      | HAL_TXQ_DBA_GATED
397610ad9a77SSam Leffler 			      | HAL_TXQ_BACKOFF_DISABLE
397710ad9a77SSam Leffler 			      | HAL_TXQ_ARB_LOCKOUT_GLOBAL
397810ad9a77SSam Leffler 			      ;
397910ad9a77SSam Leffler 		qi.tqi_aifs = 0;
398010ad9a77SSam Leffler 		/* XXX +dbaprep? */
398110ad9a77SSam Leffler 		qi.tqi_readyTime = sc->sc_tdmaslotlen;
398210ad9a77SSam Leffler 		qi.tqi_burstTime = qi.tqi_readyTime;
398310ad9a77SSam Leffler 	} else {
398410ad9a77SSam Leffler #endif
398510ad9a77SSam Leffler 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
398610ad9a77SSam Leffler 			      | HAL_TXQ_TXERRINT_ENABLE
398710ad9a77SSam Leffler 			      | HAL_TXQ_TXDESCINT_ENABLE
398810ad9a77SSam Leffler 			      | HAL_TXQ_TXURNINT_ENABLE
398910ad9a77SSam Leffler 			      ;
3990c42a7b7eSSam Leffler 		qi.tqi_aifs = wmep->wmep_aifsn;
3991c42a7b7eSSam Leffler 		qi.tqi_cwmin = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
3992c42a7b7eSSam Leffler 		qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
399310ad9a77SSam Leffler 		qi.tqi_readyTime = 0;
3994c42a7b7eSSam Leffler 		qi.tqi_burstTime = ATH_TXOP_TO_US(wmep->wmep_txopLimit);
3995584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
399610ad9a77SSam Leffler 	}
399710ad9a77SSam Leffler #endif
399810ad9a77SSam Leffler 
399910ad9a77SSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET,
400010ad9a77SSam Leffler 	    "%s: Q%u qflags 0x%x aifs %u cwmin %u cwmax %u burstTime %u\n",
400110ad9a77SSam Leffler 	    __func__, txq->axq_qnum, qi.tqi_qflags,
400210ad9a77SSam Leffler 	    qi.tqi_aifs, qi.tqi_cwmin, qi.tqi_cwmax, qi.tqi_burstTime);
4003c42a7b7eSSam Leffler 
4004c42a7b7eSSam Leffler 	if (!ath_hal_settxqueueprops(ah, txq->axq_qnum, &qi)) {
4005b032f27cSSam Leffler 		if_printf(ifp, "unable to update hardware queue "
4006c42a7b7eSSam Leffler 			"parameters for %s traffic!\n",
4007c42a7b7eSSam Leffler 			ieee80211_wme_acnames[ac]);
4008c42a7b7eSSam Leffler 		return 0;
4009c42a7b7eSSam Leffler 	} else {
4010c42a7b7eSSam Leffler 		ath_hal_resettxqueue(ah, txq->axq_qnum); /* push to h/w */
4011c42a7b7eSSam Leffler 		return 1;
4012c42a7b7eSSam Leffler 	}
4013c42a7b7eSSam Leffler #undef ATH_TXOP_TO_US
4014c42a7b7eSSam Leffler #undef ATH_EXPONENT_TO_VALUE
4015c42a7b7eSSam Leffler }
4016c42a7b7eSSam Leffler 
4017c42a7b7eSSam Leffler /*
4018c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update WME parameters.
4019c42a7b7eSSam Leffler  */
4020c42a7b7eSSam Leffler static int
4021c42a7b7eSSam Leffler ath_wme_update(struct ieee80211com *ic)
4022c42a7b7eSSam Leffler {
4023c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
4024c42a7b7eSSam Leffler 
4025c42a7b7eSSam Leffler 	return !ath_txq_update(sc, WME_AC_BE) ||
4026c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_BK) ||
4027c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VI) ||
4028c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VO) ? EIO : 0;
4029c42a7b7eSSam Leffler }
4030c42a7b7eSSam Leffler 
4031c42a7b7eSSam Leffler /*
4032c42a7b7eSSam Leffler  * Reclaim resources for a setup queue.
4033c42a7b7eSSam Leffler  */
4034c42a7b7eSSam Leffler static void
4035c42a7b7eSSam Leffler ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
4036c42a7b7eSSam Leffler {
4037c42a7b7eSSam Leffler 
4038c42a7b7eSSam Leffler 	ath_hal_releasetxqueue(sc->sc_ah, txq->axq_qnum);
4039c42a7b7eSSam Leffler 	ATH_TXQ_LOCK_DESTROY(txq);
4040c42a7b7eSSam Leffler 	sc->sc_txqsetup &= ~(1<<txq->axq_qnum);
4041c42a7b7eSSam Leffler }
4042c42a7b7eSSam Leffler 
4043c42a7b7eSSam Leffler /*
4044c42a7b7eSSam Leffler  * Reclaim all tx queue resources.
4045c42a7b7eSSam Leffler  */
4046c42a7b7eSSam Leffler static void
4047c42a7b7eSSam Leffler ath_tx_cleanup(struct ath_softc *sc)
4048c42a7b7eSSam Leffler {
4049c42a7b7eSSam Leffler 	int i;
4050c42a7b7eSSam Leffler 
4051c42a7b7eSSam Leffler 	ATH_TXBUF_LOCK_DESTROY(sc);
4052c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
4053c42a7b7eSSam Leffler 		if (ATH_TXQ_SETUP(sc, i))
4054c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, &sc->sc_txq[i]);
4055c42a7b7eSSam Leffler }
40565591b213SSam Leffler 
405799d258fdSSam Leffler /*
4058ab06fdf2SSam Leffler  * Return h/w rate index for an IEEE rate (w/o basic rate bit)
4059ab06fdf2SSam Leffler  * using the current rates in sc_rixmap.
40608b5341deSSam Leffler  */
4061b8e788a5SAdrian Chadd int
4062ab06fdf2SSam Leffler ath_tx_findrix(const struct ath_softc *sc, uint8_t rate)
40638b5341deSSam Leffler {
4064ab06fdf2SSam Leffler 	int rix = sc->sc_rixmap[rate];
4065ab06fdf2SSam Leffler 	/* NB: return lowest rix for invalid rate */
4066ab06fdf2SSam Leffler 	return (rix == 0xff ? 0 : rix);
40678b5341deSSam Leffler }
40688b5341deSSam Leffler 
406968e8e04eSSam Leffler /*
4070c42a7b7eSSam Leffler  * Process completed xmit descriptors from the specified queue.
4071c42a7b7eSSam Leffler  */
4072d7736e13SSam Leffler static int
4073c42a7b7eSSam Leffler ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq)
40745591b213SSam Leffler {
40755591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4076b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
4077b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
407810ad9a77SSam Leffler 	struct ath_buf *bf, *last;
4079c4c3cb46SSam Leffler 	struct ath_desc *ds, *ds0;
408065f9edeeSSam Leffler 	struct ath_tx_status *ts;
40815591b213SSam Leffler 	struct ieee80211_node *ni;
40825591b213SSam Leffler 	struct ath_node *an;
4083d7736e13SSam Leffler 	int sr, lr, pri, nacked;
40845591b213SSam Leffler 	HAL_STATUS status;
40855591b213SSam Leffler 
4086c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_TX_PROC, "%s: tx queue %u head %p link %p\n",
4087c42a7b7eSSam Leffler 		__func__, txq->axq_qnum,
4088c42a7b7eSSam Leffler 		(caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
4089c42a7b7eSSam Leffler 		txq->axq_link);
4090d7736e13SSam Leffler 	nacked = 0;
40915591b213SSam Leffler 	for (;;) {
4092c42a7b7eSSam Leffler 		ATH_TXQ_LOCK(txq);
4093c42a7b7eSSam Leffler 		txq->axq_intrcnt = 0;	/* reset periodic desc intr count */
4094*6b349e5aSAdrian Chadd 		bf = TAILQ_FIRST(&txq->axq_q);
40955591b213SSam Leffler 		if (bf == NULL) {
4096c42a7b7eSSam Leffler 			ATH_TXQ_UNLOCK(txq);
40975591b213SSam Leffler 			break;
40985591b213SSam Leffler 		}
4099c4c3cb46SSam Leffler 		ds0 = &bf->bf_desc[0];
41005591b213SSam Leffler 		ds = &bf->bf_desc[bf->bf_nseg - 1];
410165f9edeeSSam Leffler 		ts = &bf->bf_status.ds_txstat;
410265f9edeeSSam Leffler 		status = ath_hal_txprocdesc(ah, ds, ts);
4103a585a9a1SSam Leffler #ifdef ATH_DEBUG
4104c42a7b7eSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_XMIT_DESC)
41056902009eSSam Leffler 			ath_printtxbuf(sc, bf, txq->axq_qnum, 0,
41066902009eSSam Leffler 			    status == HAL_OK);
41075591b213SSam Leffler #endif
41085591b213SSam Leffler 		if (status == HAL_EINPROGRESS) {
4109c42a7b7eSSam Leffler 			ATH_TXQ_UNLOCK(txq);
41105591b213SSam Leffler 			break;
41115591b213SSam Leffler 		}
4112*6b349e5aSAdrian Chadd 		ATH_TXQ_REMOVE(txq, bf, bf_list);
4113584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
411410ad9a77SSam Leffler 		if (txq->axq_depth > 0) {
411510ad9a77SSam Leffler 			/*
411610ad9a77SSam Leffler 			 * More frames follow.  Mark the buffer busy
411710ad9a77SSam Leffler 			 * so it's not re-used while the hardware may
411810ad9a77SSam Leffler 			 * still re-read the link field in the descriptor.
411910ad9a77SSam Leffler 			 */
412010ad9a77SSam Leffler 			bf->bf_flags |= ATH_BUF_BUSY;
412110ad9a77SSam Leffler 		} else
412210ad9a77SSam Leffler #else
4123ebecf802SSam Leffler 		if (txq->axq_depth == 0)
412410ad9a77SSam Leffler #endif
41251539af1eSSam Leffler 			txq->axq_link = NULL;
4126c42a7b7eSSam Leffler 		ATH_TXQ_UNLOCK(txq);
41275591b213SSam Leffler 
41285591b213SSam Leffler 		ni = bf->bf_node;
41295591b213SSam Leffler 		if (ni != NULL) {
4130c42a7b7eSSam Leffler 			an = ATH_NODE(ni);
413165f9edeeSSam Leffler 			if (ts->ts_status == 0) {
413265f9edeeSSam Leffler 				u_int8_t txant = ts->ts_antenna;
4133c42a7b7eSSam Leffler 				sc->sc_stats.ast_ant_tx[txant]++;
4134c42a7b7eSSam Leffler 				sc->sc_ant_tx[txant]++;
4135f6cbf16aSSam Leffler 				if (ts->ts_finaltsi != 0)
4136c42a7b7eSSam Leffler 					sc->sc_stats.ast_tx_altrate++;
4137c42a7b7eSSam Leffler 				pri = M_WME_GETAC(bf->bf_m);
4138c42a7b7eSSam Leffler 				if (pri >= WME_AC_VO)
4139c42a7b7eSSam Leffler 					ic->ic_wme.wme_hipri_traffic++;
4140ad80c0aaSSam Leffler 				if ((bf->bf_txflags & HAL_TXDESC_NOACK) == 0)
4141c42a7b7eSSam Leffler 					ni->ni_inact = ni->ni_inact_reload;
41425591b213SSam Leffler 			} else {
414365f9edeeSSam Leffler 				if (ts->ts_status & HAL_TXERR_XRETRY)
41445591b213SSam Leffler 					sc->sc_stats.ast_tx_xretries++;
414565f9edeeSSam Leffler 				if (ts->ts_status & HAL_TXERR_FIFO)
41465591b213SSam Leffler 					sc->sc_stats.ast_tx_fifoerr++;
414765f9edeeSSam Leffler 				if (ts->ts_status & HAL_TXERR_FILT)
41485591b213SSam Leffler 					sc->sc_stats.ast_tx_filtered++;
4149e9d1191fSAdrian Chadd 				if (ts->ts_status & HAL_TXERR_XTXOP)
4150e9d1191fSAdrian Chadd 					sc->sc_stats.ast_tx_xtxop++;
4151e9d1191fSAdrian Chadd 				if (ts->ts_status & HAL_TXERR_TIMER_EXPIRED)
4152e9d1191fSAdrian Chadd 					sc->sc_stats.ast_tx_timerexpired++;
4153e9d1191fSAdrian Chadd 
4154e9d1191fSAdrian Chadd 				/* XXX HAL_TX_DATA_UNDERRUN */
4155e9d1191fSAdrian Chadd 				/* XXX HAL_TX_DELIM_UNDERRUN */
4156e9d1191fSAdrian Chadd 
415768e8e04eSSam Leffler 				if (bf->bf_m->m_flags & M_FF)
415868e8e04eSSam Leffler 					sc->sc_stats.ast_ff_txerr++;
41595591b213SSam Leffler 			}
4160e9d1191fSAdrian Chadd 			/* XXX when is this valid? */
4161e9d1191fSAdrian Chadd 			if (ts->ts_status & HAL_TX_DESC_CFG_ERR)
4162e9d1191fSAdrian Chadd 				sc->sc_stats.ast_tx_desccfgerr++;
4163e9d1191fSAdrian Chadd 
416465f9edeeSSam Leffler 			sr = ts->ts_shortretry;
416565f9edeeSSam Leffler 			lr = ts->ts_longretry;
41665591b213SSam Leffler 			sc->sc_stats.ast_tx_shortretry += sr;
41675591b213SSam Leffler 			sc->sc_stats.ast_tx_longretry += lr;
4168c42a7b7eSSam Leffler 			/*
4169c42a7b7eSSam Leffler 			 * Hand the descriptor to the rate control algorithm.
4170c42a7b7eSSam Leffler 			 */
417165f9edeeSSam Leffler 			if ((ts->ts_status & HAL_TXERR_FILT) == 0 &&
417280c07f23SSam Leffler 			    (bf->bf_txflags & HAL_TXDESC_NOACK) == 0) {
4173d7736e13SSam Leffler 				/*
417484784be1SSam Leffler 				 * If frame was ack'd update statistics,
417584784be1SSam Leffler 				 * including the last rx time used to
417684784be1SSam Leffler 				 * workaround phantom bmiss interrupts.
4177d7736e13SSam Leffler 				 */
417884784be1SSam Leffler 				if (ts->ts_status == 0) {
4179d7736e13SSam Leffler 					nacked++;
418084784be1SSam Leffler 					sc->sc_stats.ast_tx_rssi = ts->ts_rssi;
418184784be1SSam Leffler 					ATH_RSSI_LPF(sc->sc_halstats.ns_avgtxrssi,
418284784be1SSam Leffler 						ts->ts_rssi);
418384784be1SSam Leffler 				}
418465f9edeeSSam Leffler 				ath_rate_tx_complete(sc, an, bf);
4185d7736e13SSam Leffler 			}
41860a915fadSSam Leffler 			/*
418768e8e04eSSam Leffler 			 * Do any tx complete callback.  Note this must
418868e8e04eSSam Leffler 			 * be done before releasing the node reference.
418968e8e04eSSam Leffler 			 */
419068e8e04eSSam Leffler 			if (bf->bf_m->m_flags & M_TXCB)
419168e8e04eSSam Leffler 				ieee80211_process_callback(ni, bf->bf_m,
419274eca0c2SSam Leffler 				    (bf->bf_txflags & HAL_TXDESC_NOACK) == 0 ?
419374eca0c2SSam Leffler 				        ts->ts_status : HAL_TXERR_XRETRY);
4194c42a7b7eSSam Leffler 			ieee80211_free_node(ni);
41955591b213SSam Leffler 		}
41965591b213SSam Leffler 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
41975591b213SSam Leffler 		    BUS_DMASYNC_POSTWRITE);
41985591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
419968e8e04eSSam Leffler 
42005591b213SSam Leffler 		m_freem(bf->bf_m);
42015591b213SSam Leffler 		bf->bf_m = NULL;
42025591b213SSam Leffler 		bf->bf_node = NULL;
42035591b213SSam Leffler 
4204f0b2a0beSSam Leffler 		ATH_TXBUF_LOCK(sc);
4205*6b349e5aSAdrian Chadd 		last = TAILQ_LAST(&sc->sc_txbuf, ath_bufhead_s);
420610ad9a77SSam Leffler 		if (last != NULL)
420710ad9a77SSam Leffler 			last->bf_flags &= ~ATH_BUF_BUSY;
4208*6b349e5aSAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
4209f0b2a0beSSam Leffler 		ATH_TXBUF_UNLOCK(sc);
42105591b213SSam Leffler 	}
4211339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
421268e8e04eSSam Leffler 	/*
421368e8e04eSSam Leffler 	 * Flush fast-frame staging queue when traffic slows.
421468e8e04eSSam Leffler 	 */
421568e8e04eSSam Leffler 	if (txq->axq_depth <= 1)
421604f19fd6SSam Leffler 		ieee80211_ff_flush(ic, txq->axq_ac);
4217339ccfb3SSam Leffler #endif
4218d7736e13SSam Leffler 	return nacked;
4219d7736e13SSam Leffler }
4220d7736e13SSam Leffler 
4221d7736e13SSam Leffler static __inline int
4222d7736e13SSam Leffler txqactive(struct ath_hal *ah, int qnum)
4223d7736e13SSam Leffler {
4224e2815d69SSam Leffler 	u_int32_t txqs = 1<<qnum;
4225e2815d69SSam Leffler 	ath_hal_gettxintrtxqs(ah, &txqs);
42269760f8aeSSam Leffler 	return (txqs & (1<<qnum));
4227c42a7b7eSSam Leffler }
4228c42a7b7eSSam Leffler 
4229c42a7b7eSSam Leffler /*
4230c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
4231c42a7b7eSSam Leffler  * for a single hardware transmit queue (e.g. 5210 and 5211).
4232c42a7b7eSSam Leffler  */
4233c42a7b7eSSam Leffler static void
4234c42a7b7eSSam Leffler ath_tx_proc_q0(void *arg, int npending)
4235c42a7b7eSSam Leffler {
4236c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4237fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
4238c42a7b7eSSam Leffler 
4239d7736e13SSam Leffler 	if (txqactive(sc->sc_ah, 0) && ath_tx_processq(sc, &sc->sc_txq[0]))
4240d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4241d7736e13SSam Leffler 	if (txqactive(sc->sc_ah, sc->sc_cabq->axq_qnum))
4242d7736e13SSam Leffler 		ath_tx_processq(sc, sc->sc_cabq);
424313f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
42442e986da5SSam Leffler 	sc->sc_wd_timer = 0;
42455591b213SSam Leffler 
42463e50ec2cSSam Leffler 	if (sc->sc_softled)
424746d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
42483e50ec2cSSam Leffler 
42495591b213SSam Leffler 	ath_start(ifp);
42505591b213SSam Leffler }
42515591b213SSam Leffler 
42525591b213SSam Leffler /*
4253c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
4254c42a7b7eSSam Leffler  * for four hardware queues, 0-3 (e.g. 5212 w/ WME support).
42555591b213SSam Leffler  */
42565591b213SSam Leffler static void
4257c42a7b7eSSam Leffler ath_tx_proc_q0123(void *arg, int npending)
4258c42a7b7eSSam Leffler {
4259c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4260fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
4261d7736e13SSam Leffler 	int nacked;
4262c42a7b7eSSam Leffler 
4263c42a7b7eSSam Leffler 	/*
4264c42a7b7eSSam Leffler 	 * Process each active queue.
4265c42a7b7eSSam Leffler 	 */
4266d7736e13SSam Leffler 	nacked = 0;
4267d7736e13SSam Leffler 	if (txqactive(sc->sc_ah, 0))
4268d7736e13SSam Leffler 		nacked += ath_tx_processq(sc, &sc->sc_txq[0]);
4269d7736e13SSam Leffler 	if (txqactive(sc->sc_ah, 1))
4270d7736e13SSam Leffler 		nacked += ath_tx_processq(sc, &sc->sc_txq[1]);
4271d7736e13SSam Leffler 	if (txqactive(sc->sc_ah, 2))
4272d7736e13SSam Leffler 		nacked += ath_tx_processq(sc, &sc->sc_txq[2]);
4273d7736e13SSam Leffler 	if (txqactive(sc->sc_ah, 3))
4274d7736e13SSam Leffler 		nacked += ath_tx_processq(sc, &sc->sc_txq[3]);
4275d7736e13SSam Leffler 	if (txqactive(sc->sc_ah, sc->sc_cabq->axq_qnum))
4276c42a7b7eSSam Leffler 		ath_tx_processq(sc, sc->sc_cabq);
4277d7736e13SSam Leffler 	if (nacked)
4278d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4279c42a7b7eSSam Leffler 
428013f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
42812e986da5SSam Leffler 	sc->sc_wd_timer = 0;
4282c42a7b7eSSam Leffler 
42833e50ec2cSSam Leffler 	if (sc->sc_softled)
428446d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
42853e50ec2cSSam Leffler 
4286c42a7b7eSSam Leffler 	ath_start(ifp);
4287c42a7b7eSSam Leffler }
4288c42a7b7eSSam Leffler 
4289c42a7b7eSSam Leffler /*
4290c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt.
4291c42a7b7eSSam Leffler  */
4292c42a7b7eSSam Leffler static void
4293c42a7b7eSSam Leffler ath_tx_proc(void *arg, int npending)
4294c42a7b7eSSam Leffler {
4295c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4296fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
4297d7736e13SSam Leffler 	int i, nacked;
4298c42a7b7eSSam Leffler 
4299c42a7b7eSSam Leffler 	/*
4300c42a7b7eSSam Leffler 	 * Process each active queue.
4301c42a7b7eSSam Leffler 	 */
4302d7736e13SSam Leffler 	nacked = 0;
4303c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
4304d7736e13SSam Leffler 		if (ATH_TXQ_SETUP(sc, i) && txqactive(sc->sc_ah, i))
4305d7736e13SSam Leffler 			nacked += ath_tx_processq(sc, &sc->sc_txq[i]);
4306d7736e13SSam Leffler 	if (nacked)
4307d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4308c42a7b7eSSam Leffler 
430913f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
43102e986da5SSam Leffler 	sc->sc_wd_timer = 0;
4311c42a7b7eSSam Leffler 
43123e50ec2cSSam Leffler 	if (sc->sc_softled)
431346d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
43143e50ec2cSSam Leffler 
4315c42a7b7eSSam Leffler 	ath_start(ifp);
4316c42a7b7eSSam Leffler }
4317c42a7b7eSSam Leffler 
4318c42a7b7eSSam Leffler static void
4319c42a7b7eSSam Leffler ath_tx_draintxq(struct ath_softc *sc, struct ath_txq *txq)
43205591b213SSam Leffler {
4321a585a9a1SSam Leffler #ifdef ATH_DEBUG
43225591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4323d2f6ed15SSam Leffler #endif
432423428eafSSam Leffler 	struct ieee80211_node *ni;
43255591b213SSam Leffler 	struct ath_buf *bf;
43267a4c5ed9SSam Leffler 	u_int ix;
43275591b213SSam Leffler 
4328c42a7b7eSSam Leffler 	/*
4329c42a7b7eSSam Leffler 	 * NB: this assumes output has been stopped and
43305d61b5e8SSam Leffler 	 *     we do not need to block ath_tx_proc
4331c42a7b7eSSam Leffler 	 */
433210ad9a77SSam Leffler 	ATH_TXBUF_LOCK(sc);
4333*6b349e5aSAdrian Chadd 	bf = TAILQ_LAST(&sc->sc_txbuf, ath_bufhead_s);
433410ad9a77SSam Leffler 	if (bf != NULL)
433510ad9a77SSam Leffler 		bf->bf_flags &= ~ATH_BUF_BUSY;
433610ad9a77SSam Leffler 	ATH_TXBUF_UNLOCK(sc);
43377a4c5ed9SSam Leffler 	for (ix = 0;; ix++) {
4338c42a7b7eSSam Leffler 		ATH_TXQ_LOCK(txq);
4339*6b349e5aSAdrian Chadd 		bf = TAILQ_FIRST(&txq->axq_q);
43405591b213SSam Leffler 		if (bf == NULL) {
4341ebecf802SSam Leffler 			txq->axq_link = NULL;
4342c42a7b7eSSam Leffler 			ATH_TXQ_UNLOCK(txq);
43435591b213SSam Leffler 			break;
43445591b213SSam Leffler 		}
4345*6b349e5aSAdrian Chadd 		ATH_TXQ_REMOVE(txq, bf, bf_list);
4346c42a7b7eSSam Leffler 		ATH_TXQ_UNLOCK(txq);
4347a585a9a1SSam Leffler #ifdef ATH_DEBUG
43484a3ac3fcSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_RESET) {
4349b032f27cSSam Leffler 			struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4350b032f27cSSam Leffler 
43516902009eSSam Leffler 			ath_printtxbuf(sc, bf, txq->axq_qnum, ix,
435265f9edeeSSam Leffler 				ath_hal_txprocdesc(ah, bf->bf_desc,
435365f9edeeSSam Leffler 				    &bf->bf_status.ds_txstat) == HAL_OK);
4354e40b6ab1SSam Leffler 			ieee80211_dump_pkt(ic, mtod(bf->bf_m, const uint8_t *),
43554a3ac3fcSSam Leffler 			    bf->bf_m->m_len, 0, -1);
43564a3ac3fcSSam Leffler 		}
4357a585a9a1SSam Leffler #endif /* ATH_DEBUG */
43585591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
435923428eafSSam Leffler 		ni = bf->bf_node;
43605591b213SSam Leffler 		bf->bf_node = NULL;
4361c42a7b7eSSam Leffler 		if (ni != NULL) {
436223428eafSSam Leffler 			/*
4363d50ea6acSSam Leffler 			 * Do any callback and reclaim the node reference.
436423428eafSSam Leffler 			 */
4365d50ea6acSSam Leffler 			if (bf->bf_m->m_flags & M_TXCB)
4366d50ea6acSSam Leffler 				ieee80211_process_callback(ni, bf->bf_m, -1);
4367c42a7b7eSSam Leffler 			ieee80211_free_node(ni);
436823428eafSSam Leffler 		}
436968e8e04eSSam Leffler 		m_freem(bf->bf_m);
437068e8e04eSSam Leffler 		bf->bf_m = NULL;
437110ad9a77SSam Leffler 		bf->bf_flags &= ~ATH_BUF_BUSY;
437268e8e04eSSam Leffler 
4373f0b2a0beSSam Leffler 		ATH_TXBUF_LOCK(sc);
4374*6b349e5aSAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
4375f0b2a0beSSam Leffler 		ATH_TXBUF_UNLOCK(sc);
43765591b213SSam Leffler 	}
4377c42a7b7eSSam Leffler }
4378c42a7b7eSSam Leffler 
4379c42a7b7eSSam Leffler static void
4380c42a7b7eSSam Leffler ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
4381c42a7b7eSSam Leffler {
4382c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4383c42a7b7eSSam Leffler 
4384c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n",
4385c42a7b7eSSam Leffler 	    __func__, txq->axq_qnum,
43866891c875SPeter Wemm 	    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, txq->axq_qnum),
43876891c875SPeter Wemm 	    txq->axq_link);
43884a3ac3fcSSam Leffler 	(void) ath_hal_stoptxdma(ah, txq->axq_qnum);
4389c42a7b7eSSam Leffler }
4390c42a7b7eSSam Leffler 
4391c42a7b7eSSam Leffler /*
4392c42a7b7eSSam Leffler  * Drain the transmit queues and reclaim resources.
4393c42a7b7eSSam Leffler  */
4394c42a7b7eSSam Leffler static void
4395c42a7b7eSSam Leffler ath_draintxq(struct ath_softc *sc)
4396c42a7b7eSSam Leffler {
4397c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4398fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
4399c42a7b7eSSam Leffler 	int i;
4400c42a7b7eSSam Leffler 
4401c42a7b7eSSam Leffler 	/* XXX return value */
4402c42a7b7eSSam Leffler 	if (!sc->sc_invalid) {
4403c42a7b7eSSam Leffler 		/* don't touch the hardware if marked invalid */
44044a3ac3fcSSam Leffler 		DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n",
44054a3ac3fcSSam Leffler 		    __func__, sc->sc_bhalq,
44064a3ac3fcSSam Leffler 		    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq),
44074a3ac3fcSSam Leffler 		    NULL);
4408c42a7b7eSSam Leffler 		(void) ath_hal_stoptxdma(ah, sc->sc_bhalq);
4409c42a7b7eSSam Leffler 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
4410c42a7b7eSSam Leffler 			if (ATH_TXQ_SETUP(sc, i))
4411c42a7b7eSSam Leffler 				ath_tx_stopdma(sc, &sc->sc_txq[i]);
4412c42a7b7eSSam Leffler 	}
4413c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
4414c42a7b7eSSam Leffler 		if (ATH_TXQ_SETUP(sc, i))
4415c42a7b7eSSam Leffler 			ath_tx_draintxq(sc, &sc->sc_txq[i]);
44164a3ac3fcSSam Leffler #ifdef ATH_DEBUG
44174a3ac3fcSSam Leffler 	if (sc->sc_debug & ATH_DEBUG_RESET) {
4418*6b349e5aSAdrian Chadd 		struct ath_buf *bf = TAILQ_FIRST(&sc->sc_bbuf);
44194a3ac3fcSSam Leffler 		if (bf != NULL && bf->bf_m != NULL) {
44206902009eSSam Leffler 			ath_printtxbuf(sc, bf, sc->sc_bhalq, 0,
442165f9edeeSSam Leffler 				ath_hal_txprocdesc(ah, bf->bf_desc,
442265f9edeeSSam Leffler 				    &bf->bf_status.ds_txstat) == HAL_OK);
4423e40b6ab1SSam Leffler 			ieee80211_dump_pkt(ifp->if_l2com,
4424e40b6ab1SSam Leffler 			    mtod(bf->bf_m, const uint8_t *), bf->bf_m->m_len,
4425e40b6ab1SSam Leffler 			    0, -1);
44264a3ac3fcSSam Leffler 		}
44274a3ac3fcSSam Leffler 	}
44284a3ac3fcSSam Leffler #endif /* ATH_DEBUG */
442913f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
44302e986da5SSam Leffler 	sc->sc_wd_timer = 0;
44315591b213SSam Leffler }
44325591b213SSam Leffler 
44335591b213SSam Leffler /*
44345591b213SSam Leffler  * Disable the receive h/w in preparation for a reset.
44355591b213SSam Leffler  */
44365591b213SSam Leffler static void
44375591b213SSam Leffler ath_stoprecv(struct ath_softc *sc)
44385591b213SSam Leffler {
44398cec0ab9SSam Leffler #define	PA2DESC(_sc, _pa) \
4440c42a7b7eSSam Leffler 	((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
4441c42a7b7eSSam Leffler 		((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
44425591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
44435591b213SSam Leffler 
44445591b213SSam Leffler 	ath_hal_stoppcurecv(ah);	/* disable PCU */
44455591b213SSam Leffler 	ath_hal_setrxfilter(ah, 0);	/* clear recv filter */
44465591b213SSam Leffler 	ath_hal_stopdmarecv(ah);	/* disable DMA engine */
4447c42a7b7eSSam Leffler 	DELAY(3000);			/* 3ms is long enough for 1 frame */
4448a585a9a1SSam Leffler #ifdef ATH_DEBUG
4449c42a7b7eSSam Leffler 	if (sc->sc_debug & (ATH_DEBUG_RESET | ATH_DEBUG_FATAL)) {
44505591b213SSam Leffler 		struct ath_buf *bf;
44517a4c5ed9SSam Leffler 		u_int ix;
44525591b213SSam Leffler 
4453e325e530SSam Leffler 		printf("%s: rx queue %p, link %p\n", __func__,
445430310634SPeter Wemm 			(caddr_t)(uintptr_t) ath_hal_getrxbuf(ah), sc->sc_rxlink);
44557a4c5ed9SSam Leffler 		ix = 0;
4456*6b349e5aSAdrian Chadd 		TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
44578cec0ab9SSam Leffler 			struct ath_desc *ds = bf->bf_desc;
445865f9edeeSSam Leffler 			struct ath_rx_status *rs = &bf->bf_status.ds_rxstat;
4459c42a7b7eSSam Leffler 			HAL_STATUS status = ath_hal_rxprocdesc(ah, ds,
446065f9edeeSSam Leffler 				bf->bf_daddr, PA2DESC(sc, ds->ds_link), rs);
4461c42a7b7eSSam Leffler 			if (status == HAL_OK || (sc->sc_debug & ATH_DEBUG_FATAL))
44626902009eSSam Leffler 				ath_printrxbuf(sc, bf, ix, status == HAL_OK);
44637a4c5ed9SSam Leffler 			ix++;
44645591b213SSam Leffler 		}
44655591b213SSam Leffler 	}
44665591b213SSam Leffler #endif
446768e8e04eSSam Leffler 	if (sc->sc_rxpending != NULL) {
446868e8e04eSSam Leffler 		m_freem(sc->sc_rxpending);
446968e8e04eSSam Leffler 		sc->sc_rxpending = NULL;
447068e8e04eSSam Leffler 	}
44715591b213SSam Leffler 	sc->sc_rxlink = NULL;		/* just in case */
44728cec0ab9SSam Leffler #undef PA2DESC
44735591b213SSam Leffler }
44745591b213SSam Leffler 
44755591b213SSam Leffler /*
44765591b213SSam Leffler  * Enable the receive h/w following a reset.
44775591b213SSam Leffler  */
44785591b213SSam Leffler static int
44795591b213SSam Leffler ath_startrecv(struct ath_softc *sc)
44805591b213SSam Leffler {
44815591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
44825591b213SSam Leffler 	struct ath_buf *bf;
44835591b213SSam Leffler 
44845591b213SSam Leffler 	sc->sc_rxlink = NULL;
448568e8e04eSSam Leffler 	sc->sc_rxpending = NULL;
4486*6b349e5aSAdrian Chadd 	TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
44875591b213SSam Leffler 		int error = ath_rxbuf_init(sc, bf);
44885591b213SSam Leffler 		if (error != 0) {
4489c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_RECV,
4490c42a7b7eSSam Leffler 				"%s: ath_rxbuf_init failed %d\n",
4491c42a7b7eSSam Leffler 				__func__, error);
44925591b213SSam Leffler 			return error;
44935591b213SSam Leffler 		}
44945591b213SSam Leffler 	}
44955591b213SSam Leffler 
4496*6b349e5aSAdrian Chadd 	bf = TAILQ_FIRST(&sc->sc_rxbuf);
44975591b213SSam Leffler 	ath_hal_putrxbuf(ah, bf->bf_daddr);
44985591b213SSam Leffler 	ath_hal_rxena(ah);		/* enable recv descriptors */
44995591b213SSam Leffler 	ath_mode_init(sc);		/* set filters, etc. */
45005591b213SSam Leffler 	ath_hal_startpcurecv(ah);	/* re-enable PCU/DMA engine */
45015591b213SSam Leffler 	return 0;
45025591b213SSam Leffler }
45035591b213SSam Leffler 
45045591b213SSam Leffler /*
4505c42a7b7eSSam Leffler  * Update internal state after a channel change.
4506c42a7b7eSSam Leffler  */
4507c42a7b7eSSam Leffler static void
4508c42a7b7eSSam Leffler ath_chan_change(struct ath_softc *sc, struct ieee80211_channel *chan)
4509c42a7b7eSSam Leffler {
4510c42a7b7eSSam Leffler 	enum ieee80211_phymode mode;
4511c42a7b7eSSam Leffler 
4512c42a7b7eSSam Leffler 	/*
4513c42a7b7eSSam Leffler 	 * Change channels and update the h/w rate map
4514c42a7b7eSSam Leffler 	 * if we're switching; e.g. 11a to 11b/g.
4515c42a7b7eSSam Leffler 	 */
451668e8e04eSSam Leffler 	mode = ieee80211_chan2mode(chan);
4517c42a7b7eSSam Leffler 	if (mode != sc->sc_curmode)
4518c42a7b7eSSam Leffler 		ath_setcurmode(sc, mode);
451959efa8b5SSam Leffler 	sc->sc_curchan = chan;
4520c42a7b7eSSam Leffler }
4521c42a7b7eSSam Leffler 
4522c42a7b7eSSam Leffler /*
45235591b213SSam Leffler  * Set/change channels.  If the channel is really being changed,
45244fa8d4efSDaniel Eischen  * it's done by resetting the chip.  To accomplish this we must
45255591b213SSam Leffler  * first cleanup any pending DMA, then restart stuff after a la
45265591b213SSam Leffler  * ath_init.
45275591b213SSam Leffler  */
45285591b213SSam Leffler static int
45295591b213SSam Leffler ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan)
45305591b213SSam Leffler {
4531b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
4532b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
45335591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4534c42a7b7eSSam Leffler 
453559efa8b5SSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %u (%u MHz, flags 0x%x)\n",
453659efa8b5SSam Leffler 	    __func__, ieee80211_chan2ieee(ic, chan),
453759efa8b5SSam Leffler 	    chan->ic_freq, chan->ic_flags);
453859efa8b5SSam Leffler 	if (chan != sc->sc_curchan) {
4539c42a7b7eSSam Leffler 		HAL_STATUS status;
45405591b213SSam Leffler 		/*
45415591b213SSam Leffler 		 * To switch channels clear any pending DMA operations;
45425591b213SSam Leffler 		 * wait long enough for the RX fifo to drain, reset the
45435591b213SSam Leffler 		 * hardware at the new frequency, and then re-enable
45445591b213SSam Leffler 		 * the relevant bits of the h/w.
45455591b213SSam Leffler 		 */
45465591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
45475591b213SSam Leffler 		ath_draintxq(sc);		/* clear pending tx frames */
45485591b213SSam Leffler 		ath_stoprecv(sc);		/* turn off frame recv */
454959efa8b5SSam Leffler 		if (!ath_hal_reset(ah, sc->sc_opmode, chan, AH_TRUE, &status)) {
4550b032f27cSSam Leffler 			if_printf(ifp, "%s: unable to reset "
455179649302SGavin Atkinson 			    "channel %u (%u MHz, flags 0x%x), hal status %u\n",
455259efa8b5SSam Leffler 			    __func__, ieee80211_chan2ieee(ic, chan),
455359efa8b5SSam Leffler 			    chan->ic_freq, chan->ic_flags, status);
45545591b213SSam Leffler 			return EIO;
45555591b213SSam Leffler 		}
4556c59005e9SSam Leffler 		sc->sc_diversity = ath_hal_getdiversity(ah);
4557c42a7b7eSSam Leffler 
455848237774SAdrian Chadd 		/* Let DFS at it in case it's a DFS channel */
455948237774SAdrian Chadd 		ath_dfs_radar_enable(sc, ic->ic_curchan);
456048237774SAdrian Chadd 
45615591b213SSam Leffler 		/*
45625591b213SSam Leffler 		 * Re-enable rx framework.
45635591b213SSam Leffler 		 */
45645591b213SSam Leffler 		if (ath_startrecv(sc) != 0) {
4565b032f27cSSam Leffler 			if_printf(ifp, "%s: unable to restart recv logic\n",
4566b032f27cSSam Leffler 			    __func__);
45675591b213SSam Leffler 			return EIO;
45685591b213SSam Leffler 		}
45695591b213SSam Leffler 
45705591b213SSam Leffler 		/*
45715591b213SSam Leffler 		 * Change channels and update the h/w rate map
45725591b213SSam Leffler 		 * if we're switching; e.g. 11a to 11b/g.
45735591b213SSam Leffler 		 */
4574c42a7b7eSSam Leffler 		ath_chan_change(sc, chan);
45750a915fadSSam Leffler 
45760a915fadSSam Leffler 		/*
45772fd9aabbSAdrian Chadd 		 * Reset clears the beacon timers; reset them
45782fd9aabbSAdrian Chadd 		 * here if needed.
45792fd9aabbSAdrian Chadd 		 */
45802fd9aabbSAdrian Chadd 		if (sc->sc_beacons) {		/* restart beacons */
45812fd9aabbSAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
45822fd9aabbSAdrian Chadd 			if (sc->sc_tdma)
45832fd9aabbSAdrian Chadd 				ath_tdma_config(sc, NULL);
45842fd9aabbSAdrian Chadd 			else
45852fd9aabbSAdrian Chadd #endif
45862fd9aabbSAdrian Chadd 			ath_beacon_config(sc, NULL);
45872fd9aabbSAdrian Chadd 		}
45882fd9aabbSAdrian Chadd 
45892fd9aabbSAdrian Chadd 		/*
45900a915fadSSam Leffler 		 * Re-enable interrupts.
45910a915fadSSam Leffler 		 */
45920a915fadSSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
45935591b213SSam Leffler 	}
45945591b213SSam Leffler 	return 0;
45955591b213SSam Leffler }
45965591b213SSam Leffler 
45975591b213SSam Leffler /*
45985591b213SSam Leffler  * Periodically recalibrate the PHY to account
45995591b213SSam Leffler  * for temperature/environment changes.
46005591b213SSam Leffler  */
46015591b213SSam Leffler static void
46025591b213SSam Leffler ath_calibrate(void *arg)
46035591b213SSam Leffler {
46045591b213SSam Leffler 	struct ath_softc *sc = arg;
46055591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
46062dc7fcc4SSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
46078d91de92SSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
46082dc7fcc4SSam Leffler 	HAL_BOOL longCal, isCalDone;
4609a108ab63SAdrian Chadd 	HAL_BOOL aniCal, shortCal = AH_FALSE;
46102dc7fcc4SSam Leffler 	int nextcal;
46115591b213SSam Leffler 
46128d91de92SSam Leffler 	if (ic->ic_flags & IEEE80211_F_SCAN)	/* defer, off channel */
46138d91de92SSam Leffler 		goto restart;
46142dc7fcc4SSam Leffler 	longCal = (ticks - sc->sc_lastlongcal >= ath_longcalinterval*hz);
4615a108ab63SAdrian Chadd 	aniCal = (ticks - sc->sc_lastani >= ath_anicalinterval*hz/1000);
4616a108ab63SAdrian Chadd 	if (sc->sc_doresetcal)
4617a108ab63SAdrian Chadd 		shortCal = (ticks - sc->sc_lastshortcal >= ath_shortcalinterval*hz/1000);
4618a108ab63SAdrian Chadd 
4619a108ab63SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: shortCal=%d; longCal=%d; aniCal=%d\n", __func__, shortCal, longCal, aniCal);
4620a108ab63SAdrian Chadd 	if (aniCal) {
4621a108ab63SAdrian Chadd 		sc->sc_stats.ast_ani_cal++;
4622a108ab63SAdrian Chadd 		sc->sc_lastani = ticks;
4623a108ab63SAdrian Chadd 		ath_hal_ani_poll(ah, sc->sc_curchan);
4624a108ab63SAdrian Chadd 	}
4625a108ab63SAdrian Chadd 
46262dc7fcc4SSam Leffler 	if (longCal) {
46275591b213SSam Leffler 		sc->sc_stats.ast_per_cal++;
46288197f57eSAdrian Chadd 		sc->sc_lastlongcal = ticks;
46295591b213SSam Leffler 		if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) {
46305591b213SSam Leffler 			/*
46315591b213SSam Leffler 			 * Rfgain is out of bounds, reset the chip
46325591b213SSam Leffler 			 * to load new gain values.
46335591b213SSam Leffler 			 */
4634370572d9SSam Leffler 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
4635370572d9SSam Leffler 				"%s: rfgain change\n", __func__);
46365591b213SSam Leffler 			sc->sc_stats.ast_per_rfgain++;
46372dc7fcc4SSam Leffler 			ath_reset(ifp);
46385591b213SSam Leffler 		}
46392dc7fcc4SSam Leffler 		/*
46402dc7fcc4SSam Leffler 		 * If this long cal is after an idle period, then
46412dc7fcc4SSam Leffler 		 * reset the data collection state so we start fresh.
46422dc7fcc4SSam Leffler 		 */
46432dc7fcc4SSam Leffler 		if (sc->sc_resetcal) {
464459efa8b5SSam Leffler 			(void) ath_hal_calreset(ah, sc->sc_curchan);
46452dc7fcc4SSam Leffler 			sc->sc_lastcalreset = ticks;
4646a108ab63SAdrian Chadd 			sc->sc_lastshortcal = ticks;
46472dc7fcc4SSam Leffler 			sc->sc_resetcal = 0;
4648a108ab63SAdrian Chadd 			sc->sc_doresetcal = AH_TRUE;
46492dc7fcc4SSam Leffler 		}
46502dc7fcc4SSam Leffler 	}
4651a108ab63SAdrian Chadd 
4652a108ab63SAdrian Chadd 	/* Only call if we're doing a short/long cal, not for ANI calibration */
4653a108ab63SAdrian Chadd 	if (shortCal || longCal) {
465459efa8b5SSam Leffler 		if (ath_hal_calibrateN(ah, sc->sc_curchan, longCal, &isCalDone)) {
46552dc7fcc4SSam Leffler 			if (longCal) {
46562dc7fcc4SSam Leffler 				/*
46572dc7fcc4SSam Leffler 				 * Calibrate noise floor data again in case of change.
46582dc7fcc4SSam Leffler 				 */
46592dc7fcc4SSam Leffler 				ath_hal_process_noisefloor(ah);
46602dc7fcc4SSam Leffler 			}
46612dc7fcc4SSam Leffler 		} else {
4662c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
4663c42a7b7eSSam Leffler 				"%s: calibration of channel %u failed\n",
466459efa8b5SSam Leffler 				__func__, sc->sc_curchan->ic_freq);
46655591b213SSam Leffler 			sc->sc_stats.ast_per_calfail++;
46665591b213SSam Leffler 		}
4667a108ab63SAdrian Chadd 		if (shortCal)
4668a108ab63SAdrian Chadd 			sc->sc_lastshortcal = ticks;
4669a108ab63SAdrian Chadd 	}
46702dc7fcc4SSam Leffler 	if (!isCalDone) {
46718d91de92SSam Leffler restart:
46727b0c77ecSSam Leffler 		/*
46732dc7fcc4SSam Leffler 		 * Use a shorter interval to potentially collect multiple
46742dc7fcc4SSam Leffler 		 * data samples required to complete calibration.  Once
46752dc7fcc4SSam Leffler 		 * we're told the work is done we drop back to a longer
46762dc7fcc4SSam Leffler 		 * interval between requests.  We're more aggressive doing
46772dc7fcc4SSam Leffler 		 * work when operating as an AP to improve operation right
46782dc7fcc4SSam Leffler 		 * after startup.
46797b0c77ecSSam Leffler 		 */
4680a108ab63SAdrian Chadd 		sc->sc_lastshortcal = ticks;
4681a108ab63SAdrian Chadd 		nextcal = ath_shortcalinterval*hz/1000;
46822dc7fcc4SSam Leffler 		if (sc->sc_opmode != HAL_M_HOSTAP)
46832dc7fcc4SSam Leffler 			nextcal *= 10;
4684a108ab63SAdrian Chadd 		sc->sc_doresetcal = AH_TRUE;
46852dc7fcc4SSam Leffler 	} else {
4686a108ab63SAdrian Chadd 		/* nextcal should be the shortest time for next event */
46872dc7fcc4SSam Leffler 		nextcal = ath_longcalinterval*hz;
46882dc7fcc4SSam Leffler 		if (sc->sc_lastcalreset == 0)
46892dc7fcc4SSam Leffler 			sc->sc_lastcalreset = sc->sc_lastlongcal;
46902dc7fcc4SSam Leffler 		else if (ticks - sc->sc_lastcalreset >= ath_resetcalinterval*hz)
46912dc7fcc4SSam Leffler 			sc->sc_resetcal = 1;	/* setup reset next trip */
4692a108ab63SAdrian Chadd 		sc->sc_doresetcal = AH_FALSE;
4693bd5a9920SSam Leffler 	}
4694a108ab63SAdrian Chadd 	/* ANI calibration may occur more often than short/long/resetcal */
4695a108ab63SAdrian Chadd 	if (ath_anicalinterval > 0)
4696a108ab63SAdrian Chadd 		nextcal = MIN(nextcal, ath_anicalinterval*hz/1000);
4697bd5a9920SSam Leffler 
46982dc7fcc4SSam Leffler 	if (nextcal != 0) {
46992dc7fcc4SSam Leffler 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: next +%u (%sisCalDone)\n",
47002dc7fcc4SSam Leffler 		    __func__, nextcal, isCalDone ? "" : "!");
47012dc7fcc4SSam Leffler 		callout_reset(&sc->sc_cal_ch, nextcal, ath_calibrate, sc);
47022dc7fcc4SSam Leffler 	} else {
47032dc7fcc4SSam Leffler 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: calibration disabled\n",
47042dc7fcc4SSam Leffler 		    __func__);
47052dc7fcc4SSam Leffler 		/* NB: don't rearm timer */
47062dc7fcc4SSam Leffler 	}
47075591b213SSam Leffler }
47085591b213SSam Leffler 
470968e8e04eSSam Leffler static void
471068e8e04eSSam Leffler ath_scan_start(struct ieee80211com *ic)
471168e8e04eSSam Leffler {
471268e8e04eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
471368e8e04eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
471468e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
471568e8e04eSSam Leffler 	u_int32_t rfilt;
471668e8e04eSSam Leffler 
471768e8e04eSSam Leffler 	/* XXX calibration timer? */
471868e8e04eSSam Leffler 
471968e8e04eSSam Leffler 	sc->sc_scanning = 1;
472068e8e04eSSam Leffler 	sc->sc_syncbeacon = 0;
472168e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
472268e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
472368e8e04eSSam Leffler 	ath_hal_setassocid(ah, ifp->if_broadcastaddr, 0);
472468e8e04eSSam Leffler 
472568e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0\n",
472668e8e04eSSam Leffler 		 __func__, rfilt, ether_sprintf(ifp->if_broadcastaddr));
472768e8e04eSSam Leffler }
472868e8e04eSSam Leffler 
472968e8e04eSSam Leffler static void
473068e8e04eSSam Leffler ath_scan_end(struct ieee80211com *ic)
473168e8e04eSSam Leffler {
473268e8e04eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
473368e8e04eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
473468e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
473568e8e04eSSam Leffler 	u_int32_t rfilt;
473668e8e04eSSam Leffler 
473768e8e04eSSam Leffler 	sc->sc_scanning = 0;
473868e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
473968e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
474068e8e04eSSam Leffler 	ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
474168e8e04eSSam Leffler 
474268e8e04eSSam Leffler 	ath_hal_process_noisefloor(ah);
474368e8e04eSSam Leffler 
474468e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
474568e8e04eSSam Leffler 		 __func__, rfilt, ether_sprintf(sc->sc_curbssid),
474668e8e04eSSam Leffler 		 sc->sc_curaid);
474768e8e04eSSam Leffler }
474868e8e04eSSam Leffler 
474968e8e04eSSam Leffler static void
475068e8e04eSSam Leffler ath_set_channel(struct ieee80211com *ic)
475168e8e04eSSam Leffler {
475268e8e04eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
475368e8e04eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
475468e8e04eSSam Leffler 
475568e8e04eSSam Leffler 	(void) ath_chan_set(sc, ic->ic_curchan);
475668e8e04eSSam Leffler 	/*
475768e8e04eSSam Leffler 	 * If we are returning to our bss channel then mark state
475868e8e04eSSam Leffler 	 * so the next recv'd beacon's tsf will be used to sync the
475968e8e04eSSam Leffler 	 * beacon timers.  Note that since we only hear beacons in
476068e8e04eSSam Leffler 	 * sta/ibss mode this has no effect in other operating modes.
476168e8e04eSSam Leffler 	 */
476268e8e04eSSam Leffler 	if (!sc->sc_scanning && ic->ic_curchan == ic->ic_bsschan)
476368e8e04eSSam Leffler 		sc->sc_syncbeacon = 1;
476468e8e04eSSam Leffler }
476568e8e04eSSam Leffler 
4766b032f27cSSam Leffler /*
4767b032f27cSSam Leffler  * Walk the vap list and check if there any vap's in RUN state.
4768b032f27cSSam Leffler  */
47695591b213SSam Leffler static int
4770b032f27cSSam Leffler ath_isanyrunningvaps(struct ieee80211vap *this)
47715591b213SSam Leffler {
4772b032f27cSSam Leffler 	struct ieee80211com *ic = this->iv_ic;
4773b032f27cSSam Leffler 	struct ieee80211vap *vap;
4774b032f27cSSam Leffler 
4775b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
4776b032f27cSSam Leffler 
4777b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
4778309a3e45SSam Leffler 		if (vap != this && vap->iv_state >= IEEE80211_S_RUN)
4779b032f27cSSam Leffler 			return 1;
4780b032f27cSSam Leffler 	}
4781b032f27cSSam Leffler 	return 0;
4782b032f27cSSam Leffler }
4783b032f27cSSam Leffler 
4784b032f27cSSam Leffler static int
4785b032f27cSSam Leffler ath_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
4786b032f27cSSam Leffler {
4787b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
4788b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
4789b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
479045bbf62fSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4791b032f27cSSam Leffler 	struct ieee80211_node *ni = NULL;
479268e8e04eSSam Leffler 	int i, error, stamode;
47935591b213SSam Leffler 	u_int32_t rfilt;
4794f52efb6dSAdrian Chadd 	int csa_run_transition = 0;
47955591b213SSam Leffler 	static const HAL_LED_STATE leds[] = {
47965591b213SSam Leffler 	    HAL_LED_INIT,	/* IEEE80211_S_INIT */
47975591b213SSam Leffler 	    HAL_LED_SCAN,	/* IEEE80211_S_SCAN */
47985591b213SSam Leffler 	    HAL_LED_AUTH,	/* IEEE80211_S_AUTH */
47995591b213SSam Leffler 	    HAL_LED_ASSOC, 	/* IEEE80211_S_ASSOC */
480077d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_CAC */
48015591b213SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_RUN */
480277d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_CSA */
480377d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_SLEEP */
48045591b213SSam Leffler 	};
48055591b213SSam Leffler 
4806c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: %s -> %s\n", __func__,
4807b032f27cSSam Leffler 		ieee80211_state_name[vap->iv_state],
4808c42a7b7eSSam Leffler 		ieee80211_state_name[nstate]);
48095591b213SSam Leffler 
4810f52efb6dSAdrian Chadd 	if (vap->iv_state == IEEE80211_S_CSA && nstate == IEEE80211_S_RUN)
4811f52efb6dSAdrian Chadd 		csa_run_transition = 1;
4812f52efb6dSAdrian Chadd 
48132e986da5SSam Leffler 	callout_drain(&sc->sc_cal_ch);
48145591b213SSam Leffler 	ath_hal_setledstate(ah, leds[nstate]);	/* set LED */
48155591b213SSam Leffler 
4816b032f27cSSam Leffler 	if (nstate == IEEE80211_S_SCAN) {
481758769f58SSam Leffler 		/*
4818b032f27cSSam Leffler 		 * Scanning: turn off beacon miss and don't beacon.
4819b032f27cSSam Leffler 		 * Mark beacon state so when we reach RUN state we'll
4820b032f27cSSam Leffler 		 * [re]setup beacons.  Unblock the task q thread so
4821b032f27cSSam Leffler 		 * deferred interrupt processing is done.
482258769f58SSam Leffler 		 */
4823b032f27cSSam Leffler 		ath_hal_intrset(ah,
4824b032f27cSSam Leffler 		    sc->sc_imask &~ (HAL_INT_SWBA | HAL_INT_BMISS));
48255591b213SSam Leffler 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
4826b032f27cSSam Leffler 		sc->sc_beacons = 0;
4827b032f27cSSam Leffler 		taskqueue_unblock(sc->sc_tq);
48285591b213SSam Leffler 	}
48295591b213SSam Leffler 
4830b032f27cSSam Leffler 	ni = vap->iv_bss;
483168e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
4832b032f27cSSam Leffler 	stamode = (vap->iv_opmode == IEEE80211_M_STA ||
48337b916f89SSam Leffler 		   vap->iv_opmode == IEEE80211_M_AHDEMO ||
4834b032f27cSSam Leffler 		   vap->iv_opmode == IEEE80211_M_IBSS);
483568e8e04eSSam Leffler 	if (stamode && nstate == IEEE80211_S_RUN) {
483668e8e04eSSam Leffler 		sc->sc_curaid = ni->ni_associd;
483768e8e04eSSam Leffler 		IEEE80211_ADDR_COPY(sc->sc_curbssid, ni->ni_bssid);
4838b032f27cSSam Leffler 		ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
4839b032f27cSSam Leffler 	}
484068e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
4841b032f27cSSam Leffler 	   __func__, rfilt, ether_sprintf(sc->sc_curbssid), sc->sc_curaid);
484268e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
484368e8e04eSSam Leffler 
4844b032f27cSSam Leffler 	/* XXX is this to restore keycache on resume? */
4845b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_STA &&
4846b032f27cSSam Leffler 	    (vap->iv_flags & IEEE80211_F_PRIVACY)) {
48475591b213SSam Leffler 		for (i = 0; i < IEEE80211_WEP_NKID; i++)
48485591b213SSam Leffler 			if (ath_hal_keyisvalid(ah, i))
484968e8e04eSSam Leffler 				ath_hal_keysetmac(ah, i, ni->ni_bssid);
48505591b213SSam Leffler 	}
4851b032f27cSSam Leffler 
4852b032f27cSSam Leffler 	/*
4853b032f27cSSam Leffler 	 * Invoke the parent method to do net80211 work.
4854b032f27cSSam Leffler 	 */
4855b032f27cSSam Leffler 	error = avp->av_newstate(vap, nstate, arg);
4856b032f27cSSam Leffler 	if (error != 0)
4857b032f27cSSam Leffler 		goto bad;
4858c42a7b7eSSam Leffler 
485968e8e04eSSam Leffler 	if (nstate == IEEE80211_S_RUN) {
4860b032f27cSSam Leffler 		/* NB: collect bss node again, it may have changed */
4861b032f27cSSam Leffler 		ni = vap->iv_bss;
48625591b213SSam Leffler 
4863b032f27cSSam Leffler 		DPRINTF(sc, ATH_DEBUG_STATE,
4864b032f27cSSam Leffler 		    "%s(RUN): iv_flags 0x%08x bintvl %d bssid %s "
4865b032f27cSSam Leffler 		    "capinfo 0x%04x chan %d\n", __func__,
4866b032f27cSSam Leffler 		    vap->iv_flags, ni->ni_intval, ether_sprintf(ni->ni_bssid),
4867b032f27cSSam Leffler 		    ni->ni_capinfo, ieee80211_chan2ieee(ic, ic->ic_curchan));
4868b032f27cSSam Leffler 
4869b032f27cSSam Leffler 		switch (vap->iv_opmode) {
4870584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
487110ad9a77SSam Leffler 		case IEEE80211_M_AHDEMO:
487210ad9a77SSam Leffler 			if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
487310ad9a77SSam Leffler 				break;
487410ad9a77SSam Leffler 			/* fall thru... */
487510ad9a77SSam Leffler #endif
4876e8fd88a3SSam Leffler 		case IEEE80211_M_HOSTAP:
4877e8fd88a3SSam Leffler 		case IEEE80211_M_IBSS:
487859aa14a9SRui Paulo 		case IEEE80211_M_MBSS:
48795591b213SSam Leffler 			/*
4880e8fd88a3SSam Leffler 			 * Allocate and setup the beacon frame.
4881e8fd88a3SSam Leffler 			 *
4882f818612bSSam Leffler 			 * Stop any previous beacon DMA.  This may be
4883f818612bSSam Leffler 			 * necessary, for example, when an ibss merge
4884f818612bSSam Leffler 			 * causes reconfiguration; there will be a state
4885f818612bSSam Leffler 			 * transition from RUN->RUN that means we may
4886f818612bSSam Leffler 			 * be called with beacon transmission active.
4887f818612bSSam Leffler 			 */
4888f818612bSSam Leffler 			ath_hal_stoptxdma(ah, sc->sc_bhalq);
4889b032f27cSSam Leffler 
48905591b213SSam Leffler 			error = ath_beacon_alloc(sc, ni);
48915591b213SSam Leffler 			if (error != 0)
48925591b213SSam Leffler 				goto bad;
48937a04dc27SSam Leffler 			/*
489480d939bfSSam Leffler 			 * If joining an adhoc network defer beacon timer
489580d939bfSSam Leffler 			 * configuration to the next beacon frame so we
489680d939bfSSam Leffler 			 * have a current TSF to use.  Otherwise we're
4897b032f27cSSam Leffler 			 * starting an ibss/bss so there's no need to delay;
4898b032f27cSSam Leffler 			 * if this is the first vap moving to RUN state, then
4899b032f27cSSam Leffler 			 * beacon state needs to be [re]configured.
49007a04dc27SSam Leffler 			 */
4901b032f27cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_IBSS &&
4902b032f27cSSam Leffler 			    ni->ni_tstamp.tsf != 0) {
490380d939bfSSam Leffler 				sc->sc_syncbeacon = 1;
4904b032f27cSSam Leffler 			} else if (!sc->sc_beacons) {
4905584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
490610ad9a77SSam Leffler 				if (vap->iv_caps & IEEE80211_C_TDMA)
490710ad9a77SSam Leffler 					ath_tdma_config(sc, vap);
490810ad9a77SSam Leffler 				else
490910ad9a77SSam Leffler #endif
4910b032f27cSSam Leffler 					ath_beacon_config(sc, vap);
4911b032f27cSSam Leffler 				sc->sc_beacons = 1;
4912b032f27cSSam Leffler 			}
4913e8fd88a3SSam Leffler 			break;
4914e8fd88a3SSam Leffler 		case IEEE80211_M_STA:
4915e8fd88a3SSam Leffler 			/*
491680d939bfSSam Leffler 			 * Defer beacon timer configuration to the next
491780d939bfSSam Leffler 			 * beacon frame so we have a current TSF to use
491880d939bfSSam Leffler 			 * (any TSF collected when scanning is likely old).
4919f52efb6dSAdrian Chadd 			 * However if it's due to a CSA -> RUN transition,
4920f52efb6dSAdrian Chadd 			 * force a beacon update so we pick up a lack of
4921f52efb6dSAdrian Chadd 			 * beacons from an AP in CAC and thus force a
4922f52efb6dSAdrian Chadd 			 * scan.
49237a04dc27SSam Leffler 			 */
492480d939bfSSam Leffler 			sc->sc_syncbeacon = 1;
4925f52efb6dSAdrian Chadd 			if (csa_run_transition)
4926f52efb6dSAdrian Chadd 				ath_beacon_config(sc, vap);
4927e8fd88a3SSam Leffler 			break;
4928b032f27cSSam Leffler 		case IEEE80211_M_MONITOR:
4929b032f27cSSam Leffler 			/*
4930b032f27cSSam Leffler 			 * Monitor mode vaps have only INIT->RUN and RUN->RUN
4931b032f27cSSam Leffler 			 * transitions so we must re-enable interrupts here to
4932b032f27cSSam Leffler 			 * handle the case of a single monitor mode vap.
4933b032f27cSSam Leffler 			 */
4934b032f27cSSam Leffler 			ath_hal_intrset(ah, sc->sc_imask);
4935b032f27cSSam Leffler 			break;
4936b032f27cSSam Leffler 		case IEEE80211_M_WDS:
4937b032f27cSSam Leffler 			break;
4938e8fd88a3SSam Leffler 		default:
4939e8fd88a3SSam Leffler 			break;
49405591b213SSam Leffler 		}
49415591b213SSam Leffler 		/*
49427b0c77ecSSam Leffler 		 * Let the hal process statistics collected during a
49437b0c77ecSSam Leffler 		 * scan so it can provide calibrated noise floor data.
49447b0c77ecSSam Leffler 		 */
49457b0c77ecSSam Leffler 		ath_hal_process_noisefloor(ah);
49467b0c77ecSSam Leffler 		/*
4947ffa2cab6SSam Leffler 		 * Reset rssi stats; maybe not the best place...
4948ffa2cab6SSam Leffler 		 */
4949ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
4950ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
4951ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
495245bbf62fSSam Leffler 		/*
4953b032f27cSSam Leffler 		 * Finally, start any timers and the task q thread
4954b032f27cSSam Leffler 		 * (in case we didn't go through SCAN state).
495545bbf62fSSam Leffler 		 */
49562dc7fcc4SSam Leffler 		if (ath_longcalinterval != 0) {
4957c42a7b7eSSam Leffler 			/* start periodic recalibration timer */
49582dc7fcc4SSam Leffler 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
49592dc7fcc4SSam Leffler 		} else {
49602dc7fcc4SSam Leffler 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
49612dc7fcc4SSam Leffler 			    "%s: calibration disabled\n", __func__);
4962c42a7b7eSSam Leffler 		}
4963b032f27cSSam Leffler 		taskqueue_unblock(sc->sc_tq);
4964b032f27cSSam Leffler 	} else if (nstate == IEEE80211_S_INIT) {
4965b032f27cSSam Leffler 		/*
4966b032f27cSSam Leffler 		 * If there are no vaps left in RUN state then
4967b032f27cSSam Leffler 		 * shutdown host/driver operation:
4968b032f27cSSam Leffler 		 * o disable interrupts
4969b032f27cSSam Leffler 		 * o disable the task queue thread
4970b032f27cSSam Leffler 		 * o mark beacon processing as stopped
4971b032f27cSSam Leffler 		 */
4972b032f27cSSam Leffler 		if (!ath_isanyrunningvaps(vap)) {
4973b032f27cSSam Leffler 			sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
4974b032f27cSSam Leffler 			/* disable interrupts  */
4975b032f27cSSam Leffler 			ath_hal_intrset(ah, sc->sc_imask &~ HAL_INT_GLOBAL);
4976b032f27cSSam Leffler 			taskqueue_block(sc->sc_tq);
4977b032f27cSSam Leffler 			sc->sc_beacons = 0;
4978b032f27cSSam Leffler 		}
4979584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
498010ad9a77SSam Leffler 		ath_hal_setcca(ah, AH_TRUE);
498110ad9a77SSam Leffler #endif
4982b032f27cSSam Leffler 	}
49835591b213SSam Leffler bad:
49845591b213SSam Leffler 	return error;
49855591b213SSam Leffler }
49865591b213SSam Leffler 
49875591b213SSam Leffler /*
4988e8fd88a3SSam Leffler  * Allocate a key cache slot to the station so we can
4989e8fd88a3SSam Leffler  * setup a mapping from key index to node. The key cache
4990e8fd88a3SSam Leffler  * slot is needed for managing antenna state and for
4991e8fd88a3SSam Leffler  * compression when stations do not use crypto.  We do
4992e8fd88a3SSam Leffler  * it uniliaterally here; if crypto is employed this slot
4993e8fd88a3SSam Leffler  * will be reassigned.
4994e8fd88a3SSam Leffler  */
4995e8fd88a3SSam Leffler static void
4996e8fd88a3SSam Leffler ath_setup_stationkey(struct ieee80211_node *ni)
4997e8fd88a3SSam Leffler {
4998b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
4999b032f27cSSam Leffler 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
5000c1225b52SSam Leffler 	ieee80211_keyix keyix, rxkeyix;
5001e8fd88a3SSam Leffler 
5002b032f27cSSam Leffler 	if (!ath_key_alloc(vap, &ni->ni_ucastkey, &keyix, &rxkeyix)) {
5003e8fd88a3SSam Leffler 		/*
5004e8fd88a3SSam Leffler 		 * Key cache is full; we'll fall back to doing
5005e8fd88a3SSam Leffler 		 * the more expensive lookup in software.  Note
5006e8fd88a3SSam Leffler 		 * this also means no h/w compression.
5007e8fd88a3SSam Leffler 		 */
5008e8fd88a3SSam Leffler 		/* XXX msg+statistic */
5009e8fd88a3SSam Leffler 	} else {
5010c1225b52SSam Leffler 		/* XXX locking? */
5011e8fd88a3SSam Leffler 		ni->ni_ucastkey.wk_keyix = keyix;
5012c1225b52SSam Leffler 		ni->ni_ucastkey.wk_rxkeyix = rxkeyix;
501333052833SSam Leffler 		/* NB: must mark device key to get called back on delete */
501433052833SSam Leffler 		ni->ni_ucastkey.wk_flags |= IEEE80211_KEY_DEVKEY;
5015d3ac945bSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_ucastkey.wk_macaddr, ni->ni_macaddr);
5016e8fd88a3SSam Leffler 		/* NB: this will create a pass-thru key entry */
5017d3ac945bSSam Leffler 		ath_keyset(sc, &ni->ni_ucastkey, vap->iv_bss);
5018e8fd88a3SSam Leffler 	}
5019e8fd88a3SSam Leffler }
5020e8fd88a3SSam Leffler 
5021e8fd88a3SSam Leffler /*
50225591b213SSam Leffler  * Setup driver-specific state for a newly associated node.
50235591b213SSam Leffler  * Note that we're called also on a re-associate, the isnew
50245591b213SSam Leffler  * param tells us if this is the first time or not.
50255591b213SSam Leffler  */
50265591b213SSam Leffler static void
5027e9962332SSam Leffler ath_newassoc(struct ieee80211_node *ni, int isnew)
50285591b213SSam Leffler {
5029b032f27cSSam Leffler 	struct ath_node *an = ATH_NODE(ni);
5030b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
5031b032f27cSSam Leffler 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
5032c62362cbSSam Leffler 	const struct ieee80211_txparam *tp = ni->ni_txparms;
50335591b213SSam Leffler 
5034ab06fdf2SSam Leffler 	an->an_mcastrix = ath_tx_findrix(sc, tp->mcastrate);
5035ab06fdf2SSam Leffler 	an->an_mgmtrix = ath_tx_findrix(sc, tp->mgmtrate);
5036b032f27cSSam Leffler 
5037b032f27cSSam Leffler 	ath_rate_newassoc(sc, an, isnew);
5038e8fd88a3SSam Leffler 	if (isnew &&
5039b032f27cSSam Leffler 	    (vap->iv_flags & IEEE80211_F_PRIVACY) == 0 && sc->sc_hasclrkey &&
5040b032f27cSSam Leffler 	    ni->ni_ucastkey.wk_keyix == IEEE80211_KEYIX_NONE)
5041e8fd88a3SSam Leffler 		ath_setup_stationkey(ni);
5042e8fd88a3SSam Leffler }
50435591b213SSam Leffler 
50445591b213SSam Leffler static int
504559efa8b5SSam Leffler ath_setregdomain(struct ieee80211com *ic, struct ieee80211_regdomain *reg,
5046b032f27cSSam Leffler 	int nchans, struct ieee80211_channel chans[])
5047b032f27cSSam Leffler {
5048b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
5049b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
505059efa8b5SSam Leffler 	HAL_STATUS status;
5051b032f27cSSam Leffler 
5052033022a9SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
505359efa8b5SSam Leffler 	    "%s: rd %u cc %u location %c%s\n",
505459efa8b5SSam Leffler 	    __func__, reg->regdomain, reg->country, reg->location,
505559efa8b5SSam Leffler 	    reg->ecm ? " ecm" : "");
5056033022a9SSam Leffler 
505759efa8b5SSam Leffler 	status = ath_hal_set_channels(ah, chans, nchans,
505859efa8b5SSam Leffler 	    reg->country, reg->regdomain);
505959efa8b5SSam Leffler 	if (status != HAL_OK) {
506059efa8b5SSam Leffler 		DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: failed, status %u\n",
506159efa8b5SSam Leffler 		    __func__, status);
506259efa8b5SSam Leffler 		return EINVAL;		/* XXX */
5063b032f27cSSam Leffler 	}
50648db87e40SAdrian Chadd 
5065b032f27cSSam Leffler 	return 0;
5066b032f27cSSam Leffler }
5067b032f27cSSam Leffler 
5068b032f27cSSam Leffler static void
5069b032f27cSSam Leffler ath_getradiocaps(struct ieee80211com *ic,
50705fe9f044SSam Leffler 	int maxchans, int *nchans, struct ieee80211_channel chans[])
5071b032f27cSSam Leffler {
5072b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
5073b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5074b032f27cSSam Leffler 
507559efa8b5SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: use rd %u cc %d\n",
507659efa8b5SSam Leffler 	    __func__, SKU_DEBUG, CTRY_DEFAULT);
5077033022a9SSam Leffler 
507859efa8b5SSam Leffler 	/* XXX check return */
507959efa8b5SSam Leffler 	(void) ath_hal_getchannels(ah, chans, maxchans, nchans,
508059efa8b5SSam Leffler 	    HAL_MODE_ALL, CTRY_DEFAULT, SKU_DEBUG, AH_TRUE);
5081033022a9SSam Leffler 
5082b032f27cSSam Leffler }
5083b032f27cSSam Leffler 
5084b032f27cSSam Leffler static int
5085b032f27cSSam Leffler ath_getchannels(struct ath_softc *sc)
5086b032f27cSSam Leffler {
5087b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
5088b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
5089b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
509059efa8b5SSam Leffler 	HAL_STATUS status;
5091b032f27cSSam Leffler 
5092b032f27cSSam Leffler 	/*
509359efa8b5SSam Leffler 	 * Collect channel set based on EEPROM contents.
5094b032f27cSSam Leffler 	 */
509559efa8b5SSam Leffler 	status = ath_hal_init_channels(ah, ic->ic_channels, IEEE80211_CHAN_MAX,
509659efa8b5SSam Leffler 	    &ic->ic_nchans, HAL_MODE_ALL, CTRY_DEFAULT, SKU_NONE, AH_TRUE);
509759efa8b5SSam Leffler 	if (status != HAL_OK) {
509859efa8b5SSam Leffler 		if_printf(ifp, "%s: unable to collect channel list from hal, "
509959efa8b5SSam Leffler 		    "status %d\n", __func__, status);
510059efa8b5SSam Leffler 		return EINVAL;
510159efa8b5SSam Leffler 	}
5102ca876918SSam Leffler 	(void) ath_hal_getregdomain(ah, &sc->sc_eerd);
5103ca876918SSam Leffler 	ath_hal_getcountrycode(ah, &sc->sc_eecc);	/* NB: cannot fail */
510459efa8b5SSam Leffler 	/* XXX map Atheros sku's to net80211 SKU's */
510559efa8b5SSam Leffler 	/* XXX net80211 types too small */
510659efa8b5SSam Leffler 	ic->ic_regdomain.regdomain = (uint16_t) sc->sc_eerd;
510759efa8b5SSam Leffler 	ic->ic_regdomain.country = (uint16_t) sc->sc_eecc;
510859efa8b5SSam Leffler 	ic->ic_regdomain.isocc[0] = ' ';	/* XXX don't know */
510959efa8b5SSam Leffler 	ic->ic_regdomain.isocc[1] = ' ';
511059efa8b5SSam Leffler 
5111b032f27cSSam Leffler 	ic->ic_regdomain.ecm = 1;
5112b032f27cSSam Leffler 	ic->ic_regdomain.location = 'I';
5113033022a9SSam Leffler 
5114033022a9SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
511559efa8b5SSam Leffler 	    "%s: eeprom rd %u cc %u (mapped rd %u cc %u) location %c%s\n",
5116033022a9SSam Leffler 	    __func__, sc->sc_eerd, sc->sc_eecc,
5117033022a9SSam Leffler 	    ic->ic_regdomain.regdomain, ic->ic_regdomain.country,
511859efa8b5SSam Leffler 	    ic->ic_regdomain.location, ic->ic_regdomain.ecm ? " ecm" : "");
51195591b213SSam Leffler 	return 0;
51205591b213SSam Leffler }
51215591b213SSam Leffler 
5122c42a7b7eSSam Leffler static void
51233e50ec2cSSam Leffler ath_led_done(void *arg)
5124c42a7b7eSSam Leffler {
51253e50ec2cSSam Leffler 	struct ath_softc *sc = arg;
51263e50ec2cSSam Leffler 
51273e50ec2cSSam Leffler 	sc->sc_blinking = 0;
51283e50ec2cSSam Leffler }
5129c42a7b7eSSam Leffler 
5130c42a7b7eSSam Leffler /*
51313e50ec2cSSam Leffler  * Turn the LED off: flip the pin and then set a timer so no
51323e50ec2cSSam Leffler  * update will happen for the specified duration.
5133c42a7b7eSSam Leffler  */
51343e50ec2cSSam Leffler static void
51353e50ec2cSSam Leffler ath_led_off(void *arg)
51363e50ec2cSSam Leffler {
51373e50ec2cSSam Leffler 	struct ath_softc *sc = arg;
51383e50ec2cSSam Leffler 
51393e50ec2cSSam Leffler 	ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, !sc->sc_ledon);
51403e50ec2cSSam Leffler 	callout_reset(&sc->sc_ledtimer, sc->sc_ledoff, ath_led_done, sc);
5141c42a7b7eSSam Leffler }
51423e50ec2cSSam Leffler 
51433e50ec2cSSam Leffler /*
51443e50ec2cSSam Leffler  * Blink the LED according to the specified on/off times.
51453e50ec2cSSam Leffler  */
51463e50ec2cSSam Leffler static void
51473e50ec2cSSam Leffler ath_led_blink(struct ath_softc *sc, int on, int off)
51483e50ec2cSSam Leffler {
51493e50ec2cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_LED, "%s: on %u off %u\n", __func__, on, off);
51503e50ec2cSSam Leffler 	ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, sc->sc_ledon);
51513e50ec2cSSam Leffler 	sc->sc_blinking = 1;
51523e50ec2cSSam Leffler 	sc->sc_ledoff = off;
51533e50ec2cSSam Leffler 	callout_reset(&sc->sc_ledtimer, on, ath_led_off, sc);
51543e50ec2cSSam Leffler }
51553e50ec2cSSam Leffler 
51563e50ec2cSSam Leffler static void
515746d4d74cSSam Leffler ath_led_event(struct ath_softc *sc, int rix)
51583e50ec2cSSam Leffler {
51593e50ec2cSSam Leffler 	sc->sc_ledevent = ticks;	/* time of last event */
51603e50ec2cSSam Leffler 	if (sc->sc_blinking)		/* don't interrupt active blink */
51613e50ec2cSSam Leffler 		return;
516246d4d74cSSam Leffler 	ath_led_blink(sc, sc->sc_hwmap[rix].ledon, sc->sc_hwmap[rix].ledoff);
5163c42a7b7eSSam Leffler }
5164c42a7b7eSSam Leffler 
51656c4612b9SSam Leffler static int
51666c4612b9SSam Leffler ath_rate_setup(struct ath_softc *sc, u_int mode)
51676c4612b9SSam Leffler {
51686c4612b9SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
51696c4612b9SSam Leffler 	const HAL_RATE_TABLE *rt;
51706c4612b9SSam Leffler 
51716c4612b9SSam Leffler 	switch (mode) {
51726c4612b9SSam Leffler 	case IEEE80211_MODE_11A:
51736c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A);
51746c4612b9SSam Leffler 		break;
5175724c193aSSam Leffler 	case IEEE80211_MODE_HALF:
5176aaa70f2fSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_HALF_RATE);
5177aaa70f2fSSam Leffler 		break;
5178724c193aSSam Leffler 	case IEEE80211_MODE_QUARTER:
5179aaa70f2fSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_QUARTER_RATE);
5180aaa70f2fSSam Leffler 		break;
51816c4612b9SSam Leffler 	case IEEE80211_MODE_11B:
51826c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11B);
51836c4612b9SSam Leffler 		break;
51846c4612b9SSam Leffler 	case IEEE80211_MODE_11G:
51856c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11G);
51866c4612b9SSam Leffler 		break;
51876c4612b9SSam Leffler 	case IEEE80211_MODE_TURBO_A:
518868e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_108A);
51896c4612b9SSam Leffler 		break;
51906c4612b9SSam Leffler 	case IEEE80211_MODE_TURBO_G:
51916c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_108G);
51926c4612b9SSam Leffler 		break;
519368e8e04eSSam Leffler 	case IEEE80211_MODE_STURBO_A:
519468e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_TURBO);
519568e8e04eSSam Leffler 		break;
519668e8e04eSSam Leffler 	case IEEE80211_MODE_11NA:
519768e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11NA_HT20);
519868e8e04eSSam Leffler 		break;
519968e8e04eSSam Leffler 	case IEEE80211_MODE_11NG:
520068e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11NG_HT20);
520168e8e04eSSam Leffler 		break;
52026c4612b9SSam Leffler 	default:
52036c4612b9SSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid mode %u\n",
52046c4612b9SSam Leffler 			__func__, mode);
52056c4612b9SSam Leffler 		return 0;
52066c4612b9SSam Leffler 	}
52076c4612b9SSam Leffler 	sc->sc_rates[mode] = rt;
5208aaa70f2fSSam Leffler 	return (rt != NULL);
52095591b213SSam Leffler }
52105591b213SSam Leffler 
52115591b213SSam Leffler static void
52125591b213SSam Leffler ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode)
52135591b213SSam Leffler {
52143e50ec2cSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
52153e50ec2cSSam Leffler 	/* NB: on/off times from the Atheros NDIS driver, w/ permission */
52163e50ec2cSSam Leffler 	static const struct {
52173e50ec2cSSam Leffler 		u_int		rate;		/* tx/rx 802.11 rate */
52183e50ec2cSSam Leffler 		u_int16_t	timeOn;		/* LED on time (ms) */
52193e50ec2cSSam Leffler 		u_int16_t	timeOff;	/* LED off time (ms) */
52203e50ec2cSSam Leffler 	} blinkrates[] = {
52213e50ec2cSSam Leffler 		{ 108,  40,  10 },
52223e50ec2cSSam Leffler 		{  96,  44,  11 },
52233e50ec2cSSam Leffler 		{  72,  50,  13 },
52243e50ec2cSSam Leffler 		{  48,  57,  14 },
52253e50ec2cSSam Leffler 		{  36,  67,  16 },
52263e50ec2cSSam Leffler 		{  24,  80,  20 },
52273e50ec2cSSam Leffler 		{  22, 100,  25 },
52283e50ec2cSSam Leffler 		{  18, 133,  34 },
52293e50ec2cSSam Leffler 		{  12, 160,  40 },
52303e50ec2cSSam Leffler 		{  10, 200,  50 },
52313e50ec2cSSam Leffler 		{   6, 240,  58 },
52323e50ec2cSSam Leffler 		{   4, 267,  66 },
52333e50ec2cSSam Leffler 		{   2, 400, 100 },
52343e50ec2cSSam Leffler 		{   0, 500, 130 },
5235724c193aSSam Leffler 		/* XXX half/quarter rates */
52363e50ec2cSSam Leffler 	};
52375591b213SSam Leffler 	const HAL_RATE_TABLE *rt;
52383e50ec2cSSam Leffler 	int i, j;
52395591b213SSam Leffler 
52405591b213SSam Leffler 	memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap));
52415591b213SSam Leffler 	rt = sc->sc_rates[mode];
52425591b213SSam Leffler 	KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode));
5243180f268dSSam Leffler 	for (i = 0; i < rt->rateCount; i++) {
5244180f268dSSam Leffler 		uint8_t ieeerate = rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
5245180f268dSSam Leffler 		if (rt->info[i].phy != IEEE80211_T_HT)
5246180f268dSSam Leffler 			sc->sc_rixmap[ieeerate] = i;
5247180f268dSSam Leffler 		else
5248180f268dSSam Leffler 			sc->sc_rixmap[ieeerate | IEEE80211_RATE_MCS] = i;
5249180f268dSSam Leffler 	}
52501b1a8e41SSam Leffler 	memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap));
525146d4d74cSSam Leffler 	for (i = 0; i < N(sc->sc_hwmap); i++) {
525246d4d74cSSam Leffler 		if (i >= rt->rateCount) {
52533e50ec2cSSam Leffler 			sc->sc_hwmap[i].ledon = (500 * hz) / 1000;
52543e50ec2cSSam Leffler 			sc->sc_hwmap[i].ledoff = (130 * hz) / 1000;
525516b4851aSSam Leffler 			continue;
52563e50ec2cSSam Leffler 		}
52573e50ec2cSSam Leffler 		sc->sc_hwmap[i].ieeerate =
525846d4d74cSSam Leffler 			rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
525946d4d74cSSam Leffler 		if (rt->info[i].phy == IEEE80211_T_HT)
526026041a14SSam Leffler 			sc->sc_hwmap[i].ieeerate |= IEEE80211_RATE_MCS;
5261d3be6f5bSSam Leffler 		sc->sc_hwmap[i].txflags = IEEE80211_RADIOTAP_F_DATAPAD;
526246d4d74cSSam Leffler 		if (rt->info[i].shortPreamble ||
526346d4d74cSSam Leffler 		    rt->info[i].phy == IEEE80211_T_OFDM)
5264d3be6f5bSSam Leffler 			sc->sc_hwmap[i].txflags |= IEEE80211_RADIOTAP_F_SHORTPRE;
52655463c4a4SSam Leffler 		sc->sc_hwmap[i].rxflags = sc->sc_hwmap[i].txflags;
52663e50ec2cSSam Leffler 		for (j = 0; j < N(blinkrates)-1; j++)
52673e50ec2cSSam Leffler 			if (blinkrates[j].rate == sc->sc_hwmap[i].ieeerate)
52683e50ec2cSSam Leffler 				break;
52693e50ec2cSSam Leffler 		/* NB: this uses the last entry if the rate isn't found */
52703e50ec2cSSam Leffler 		/* XXX beware of overlow */
52713e50ec2cSSam Leffler 		sc->sc_hwmap[i].ledon = (blinkrates[j].timeOn * hz) / 1000;
52723e50ec2cSSam Leffler 		sc->sc_hwmap[i].ledoff = (blinkrates[j].timeOff * hz) / 1000;
5273c42a7b7eSSam Leffler 	}
52745591b213SSam Leffler 	sc->sc_currates = rt;
52755591b213SSam Leffler 	sc->sc_curmode = mode;
52765591b213SSam Leffler 	/*
5277c42a7b7eSSam Leffler 	 * All protection frames are transmited at 2Mb/s for
5278c42a7b7eSSam Leffler 	 * 11g, otherwise at 1Mb/s.
52795591b213SSam Leffler 	 */
5280913a1ba1SSam Leffler 	if (mode == IEEE80211_MODE_11G)
5281ab06fdf2SSam Leffler 		sc->sc_protrix = ath_tx_findrix(sc, 2*2);
5282913a1ba1SSam Leffler 	else
5283ab06fdf2SSam Leffler 		sc->sc_protrix = ath_tx_findrix(sc, 2*1);
52844fa8d4efSDaniel Eischen 	/* NB: caller is responsible for resetting rate control state */
52853e50ec2cSSam Leffler #undef N
52865591b213SSam Leffler }
52875591b213SSam Leffler 
5288c42a7b7eSSam Leffler static void
52892e986da5SSam Leffler ath_watchdog(void *arg)
5290c42a7b7eSSam Leffler {
52912e986da5SSam Leffler 	struct ath_softc *sc = arg;
5292c42a7b7eSSam Leffler 
52932e986da5SSam Leffler 	if (sc->sc_wd_timer != 0 && --sc->sc_wd_timer == 0) {
52942e986da5SSam Leffler 		struct ifnet *ifp = sc->sc_ifp;
5295459bc4f0SSam Leffler 		uint32_t hangs;
5296459bc4f0SSam Leffler 
5297459bc4f0SSam Leffler 		if (ath_hal_gethangstate(sc->sc_ah, 0xffff, &hangs) &&
5298459bc4f0SSam Leffler 		    hangs != 0) {
5299459bc4f0SSam Leffler 			if_printf(ifp, "%s hang detected (0x%x)\n",
5300459bc4f0SSam Leffler 			    hangs & 0xff ? "bb" : "mac", hangs);
5301459bc4f0SSam Leffler 		} else
5302c42a7b7eSSam Leffler 			if_printf(ifp, "device timeout\n");
5303c42a7b7eSSam Leffler 		ath_reset(ifp);
5304c42a7b7eSSam Leffler 		ifp->if_oerrors++;
5305c42a7b7eSSam Leffler 		sc->sc_stats.ast_watchdog++;
5306c42a7b7eSSam Leffler 	}
53072e986da5SSam Leffler 	callout_schedule(&sc->sc_wd_ch, hz);
5308c42a7b7eSSam Leffler }
5309c42a7b7eSSam Leffler 
5310a585a9a1SSam Leffler #ifdef ATH_DIAGAPI
5311c42a7b7eSSam Leffler /*
5312c42a7b7eSSam Leffler  * Diagnostic interface to the HAL.  This is used by various
5313c42a7b7eSSam Leffler  * tools to do things like retrieve register contents for
5314c42a7b7eSSam Leffler  * debugging.  The mechanism is intentionally opaque so that
5315c42a7b7eSSam Leffler  * it can change frequently w/o concern for compatiblity.
5316c42a7b7eSSam Leffler  */
5317c42a7b7eSSam Leffler static int
5318c42a7b7eSSam Leffler ath_ioctl_diag(struct ath_softc *sc, struct ath_diag *ad)
5319c42a7b7eSSam Leffler {
5320c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5321c42a7b7eSSam Leffler 	u_int id = ad->ad_id & ATH_DIAG_ID;
5322c42a7b7eSSam Leffler 	void *indata = NULL;
5323c42a7b7eSSam Leffler 	void *outdata = NULL;
5324c42a7b7eSSam Leffler 	u_int32_t insize = ad->ad_in_size;
5325c42a7b7eSSam Leffler 	u_int32_t outsize = ad->ad_out_size;
5326c42a7b7eSSam Leffler 	int error = 0;
5327c42a7b7eSSam Leffler 
5328c42a7b7eSSam Leffler 	if (ad->ad_id & ATH_DIAG_IN) {
5329c42a7b7eSSam Leffler 		/*
5330c42a7b7eSSam Leffler 		 * Copy in data.
5331c42a7b7eSSam Leffler 		 */
5332c42a7b7eSSam Leffler 		indata = malloc(insize, M_TEMP, M_NOWAIT);
5333c42a7b7eSSam Leffler 		if (indata == NULL) {
5334c42a7b7eSSam Leffler 			error = ENOMEM;
5335c42a7b7eSSam Leffler 			goto bad;
5336c42a7b7eSSam Leffler 		}
5337c42a7b7eSSam Leffler 		error = copyin(ad->ad_in_data, indata, insize);
5338c42a7b7eSSam Leffler 		if (error)
5339c42a7b7eSSam Leffler 			goto bad;
5340c42a7b7eSSam Leffler 	}
5341c42a7b7eSSam Leffler 	if (ad->ad_id & ATH_DIAG_DYN) {
5342c42a7b7eSSam Leffler 		/*
5343c42a7b7eSSam Leffler 		 * Allocate a buffer for the results (otherwise the HAL
5344c42a7b7eSSam Leffler 		 * returns a pointer to a buffer where we can read the
5345c42a7b7eSSam Leffler 		 * results).  Note that we depend on the HAL leaving this
5346c42a7b7eSSam Leffler 		 * pointer for us to use below in reclaiming the buffer;
5347c42a7b7eSSam Leffler 		 * may want to be more defensive.
5348c42a7b7eSSam Leffler 		 */
5349c42a7b7eSSam Leffler 		outdata = malloc(outsize, M_TEMP, M_NOWAIT);
5350c42a7b7eSSam Leffler 		if (outdata == NULL) {
5351c42a7b7eSSam Leffler 			error = ENOMEM;
5352c42a7b7eSSam Leffler 			goto bad;
5353c42a7b7eSSam Leffler 		}
5354c42a7b7eSSam Leffler 	}
5355c42a7b7eSSam Leffler 	if (ath_hal_getdiagstate(ah, id, indata, insize, &outdata, &outsize)) {
5356c42a7b7eSSam Leffler 		if (outsize < ad->ad_out_size)
5357c42a7b7eSSam Leffler 			ad->ad_out_size = outsize;
5358c42a7b7eSSam Leffler 		if (outdata != NULL)
5359c42a7b7eSSam Leffler 			error = copyout(outdata, ad->ad_out_data,
5360c42a7b7eSSam Leffler 					ad->ad_out_size);
5361c42a7b7eSSam Leffler 	} else {
5362c42a7b7eSSam Leffler 		error = EINVAL;
5363c42a7b7eSSam Leffler 	}
5364c42a7b7eSSam Leffler bad:
5365c42a7b7eSSam Leffler 	if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
5366c42a7b7eSSam Leffler 		free(indata, M_TEMP);
5367c42a7b7eSSam Leffler 	if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
5368c42a7b7eSSam Leffler 		free(outdata, M_TEMP);
5369c42a7b7eSSam Leffler 	return error;
5370c42a7b7eSSam Leffler }
5371a585a9a1SSam Leffler #endif /* ATH_DIAGAPI */
5372c42a7b7eSSam Leffler 
5373c42a7b7eSSam Leffler static int
5374c42a7b7eSSam Leffler ath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
5375c42a7b7eSSam Leffler {
5376c42a7b7eSSam Leffler #define	IS_RUNNING(ifp) \
537713f4c340SRobert Watson 	((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
5378c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
5379b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
5380c42a7b7eSSam Leffler 	struct ifreq *ifr = (struct ifreq *)data;
538184784be1SSam Leffler 	const HAL_RATE_TABLE *rt;
5382c42a7b7eSSam Leffler 	int error = 0;
5383c42a7b7eSSam Leffler 
5384c42a7b7eSSam Leffler 	switch (cmd) {
5385c42a7b7eSSam Leffler 	case SIOCSIFFLAGS:
538631a8c1edSAndrew Thompson 		ATH_LOCK(sc);
5387c42a7b7eSSam Leffler 		if (IS_RUNNING(ifp)) {
5388c42a7b7eSSam Leffler 			/*
5389c42a7b7eSSam Leffler 			 * To avoid rescanning another access point,
5390c42a7b7eSSam Leffler 			 * do not call ath_init() here.  Instead,
5391c42a7b7eSSam Leffler 			 * only reflect promisc mode settings.
5392c42a7b7eSSam Leffler 			 */
5393c42a7b7eSSam Leffler 			ath_mode_init(sc);
5394c42a7b7eSSam Leffler 		} else if (ifp->if_flags & IFF_UP) {
5395c42a7b7eSSam Leffler 			/*
5396c42a7b7eSSam Leffler 			 * Beware of being called during attach/detach
5397c42a7b7eSSam Leffler 			 * to reset promiscuous mode.  In that case we
5398c42a7b7eSSam Leffler 			 * will still be marked UP but not RUNNING.
5399c42a7b7eSSam Leffler 			 * However trying to re-init the interface
5400c42a7b7eSSam Leffler 			 * is the wrong thing to do as we've already
5401c42a7b7eSSam Leffler 			 * torn down much of our state.  There's
5402c42a7b7eSSam Leffler 			 * probably a better way to deal with this.
5403c42a7b7eSSam Leffler 			 */
5404b032f27cSSam Leffler 			if (!sc->sc_invalid)
5405fc74a9f9SBrooks Davis 				ath_init(sc);	/* XXX lose error */
5406d3ac945bSSam Leffler 		} else {
5407c42a7b7eSSam Leffler 			ath_stop_locked(ifp);
5408d3ac945bSSam Leffler #ifdef notyet
5409d3ac945bSSam Leffler 			/* XXX must wakeup in places like ath_vap_delete */
5410d3ac945bSSam Leffler 			if (!sc->sc_invalid)
5411d3ac945bSSam Leffler 				ath_hal_setpower(sc->sc_ah, HAL_PM_FULL_SLEEP);
5412d3ac945bSSam Leffler #endif
5413d3ac945bSSam Leffler 		}
541431a8c1edSAndrew Thompson 		ATH_UNLOCK(sc);
5415c42a7b7eSSam Leffler 		break;
5416b032f27cSSam Leffler 	case SIOCGIFMEDIA:
5417b032f27cSSam Leffler 	case SIOCSIFMEDIA:
5418b032f27cSSam Leffler 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
5419b032f27cSSam Leffler 		break;
5420c42a7b7eSSam Leffler 	case SIOCGATHSTATS:
5421c42a7b7eSSam Leffler 		/* NB: embed these numbers to get a consistent view */
5422c42a7b7eSSam Leffler 		sc->sc_stats.ast_tx_packets = ifp->if_opackets;
5423c42a7b7eSSam Leffler 		sc->sc_stats.ast_rx_packets = ifp->if_ipackets;
542484784be1SSam Leffler 		sc->sc_stats.ast_tx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgtxrssi);
542584784be1SSam Leffler 		sc->sc_stats.ast_rx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgrssi);
5426584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
542710ad9a77SSam Leffler 		sc->sc_stats.ast_tdma_tsfadjp = TDMA_AVG(sc->sc_avgtsfdeltap);
542810ad9a77SSam Leffler 		sc->sc_stats.ast_tdma_tsfadjm = TDMA_AVG(sc->sc_avgtsfdeltam);
542910ad9a77SSam Leffler #endif
543084784be1SSam Leffler 		rt = sc->sc_currates;
543146d4d74cSSam Leffler 		sc->sc_stats.ast_tx_rate =
543246d4d74cSSam Leffler 		    rt->info[sc->sc_txrix].dot11Rate &~ IEEE80211_RATE_BASIC;
54336aa113fdSAdrian Chadd 		if (rt->info[sc->sc_txrix].phy & IEEE80211_T_HT)
54346aa113fdSAdrian Chadd 			sc->sc_stats.ast_tx_rate |= IEEE80211_RATE_MCS;
5435c42a7b7eSSam Leffler 		return copyout(&sc->sc_stats,
5436c42a7b7eSSam Leffler 		    ifr->ifr_data, sizeof (sc->sc_stats));
54373fc21fedSSam Leffler 	case SIOCZATHSTATS:
54383fc21fedSSam Leffler 		error = priv_check(curthread, PRIV_DRIVER);
54393fc21fedSSam Leffler 		if (error == 0)
54403fc21fedSSam Leffler 			memset(&sc->sc_stats, 0, sizeof(sc->sc_stats));
54413fc21fedSSam Leffler 		break;
5442a585a9a1SSam Leffler #ifdef ATH_DIAGAPI
5443c42a7b7eSSam Leffler 	case SIOCGATHDIAG:
5444c42a7b7eSSam Leffler 		error = ath_ioctl_diag(sc, (struct ath_diag *) ifr);
5445c42a7b7eSSam Leffler 		break;
5446f51c84eaSAdrian Chadd 	case SIOCGATHPHYERR:
5447f51c84eaSAdrian Chadd 		error = ath_ioctl_phyerr(sc,(struct ath_diag*) ifr);
5448f51c84eaSAdrian Chadd 		break;
5449a585a9a1SSam Leffler #endif
545031a8c1edSAndrew Thompson 	case SIOCGIFADDR:
5451b032f27cSSam Leffler 		error = ether_ioctl(ifp, cmd, data);
5452c42a7b7eSSam Leffler 		break;
545331a8c1edSAndrew Thompson 	default:
545431a8c1edSAndrew Thompson 		error = EINVAL;
545531a8c1edSAndrew Thompson 		break;
5456c42a7b7eSSam Leffler 	}
5457c42a7b7eSSam Leffler 	return error;
5458a614e076SSam Leffler #undef IS_RUNNING
5459c42a7b7eSSam Leffler }
5460c42a7b7eSSam Leffler 
5461c42a7b7eSSam Leffler /*
5462c42a7b7eSSam Leffler  * Announce various information on device/driver attach.
5463c42a7b7eSSam Leffler  */
5464c42a7b7eSSam Leffler static void
5465c42a7b7eSSam Leffler ath_announce(struct ath_softc *sc)
5466c42a7b7eSSam Leffler {
5467fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
5468c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5469c42a7b7eSSam Leffler 
5470498657cfSSam Leffler 	if_printf(ifp, "AR%s mac %d.%d RF%s phy %d.%d\n",
5471498657cfSSam Leffler 		ath_hal_mac_name(ah), ah->ah_macVersion, ah->ah_macRev,
5472498657cfSSam Leffler 		ath_hal_rf_name(ah), ah->ah_phyRev >> 4, ah->ah_phyRev & 0xf);
5473c42a7b7eSSam Leffler 	if (bootverbose) {
5474c42a7b7eSSam Leffler 		int i;
5475c42a7b7eSSam Leffler 		for (i = 0; i <= WME_AC_VO; i++) {
5476c42a7b7eSSam Leffler 			struct ath_txq *txq = sc->sc_ac2q[i];
5477c42a7b7eSSam Leffler 			if_printf(ifp, "Use hw queue %u for %s traffic\n",
5478c42a7b7eSSam Leffler 				txq->axq_qnum, ieee80211_wme_acnames[i]);
5479c42a7b7eSSam Leffler 		}
5480c42a7b7eSSam Leffler 		if_printf(ifp, "Use hw queue %u for CAB traffic\n",
5481c42a7b7eSSam Leffler 			sc->sc_cabq->axq_qnum);
5482c42a7b7eSSam Leffler 		if_printf(ifp, "Use hw queue %u for beacons\n", sc->sc_bhalq);
5483c42a7b7eSSam Leffler 	}
5484e2d787faSSam Leffler 	if (ath_rxbuf != ATH_RXBUF)
5485e2d787faSSam Leffler 		if_printf(ifp, "using %u rx buffers\n", ath_rxbuf);
5486e2d787faSSam Leffler 	if (ath_txbuf != ATH_TXBUF)
5487e2d787faSSam Leffler 		if_printf(ifp, "using %u tx buffers\n", ath_txbuf);
54889ac01d39SRui Paulo 	if (sc->sc_mcastkey && bootverbose)
54899ac01d39SRui Paulo 		if_printf(ifp, "using multicast key search\n");
5490c42a7b7eSSam Leffler }
549110ad9a77SSam Leffler 
5492584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
549310ad9a77SSam Leffler static void
549410ad9a77SSam Leffler ath_tdma_settimers(struct ath_softc *sc, u_int32_t nexttbtt, u_int32_t bintval)
549510ad9a77SSam Leffler {
549610ad9a77SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
549710ad9a77SSam Leffler 	HAL_BEACON_TIMERS bt;
549810ad9a77SSam Leffler 
549910ad9a77SSam Leffler 	bt.bt_intval = bintval | HAL_BEACON_ENA;
550010ad9a77SSam Leffler 	bt.bt_nexttbtt = nexttbtt;
550110ad9a77SSam Leffler 	bt.bt_nextdba = (nexttbtt<<3) - sc->sc_tdmadbaprep;
550210ad9a77SSam Leffler 	bt.bt_nextswba = (nexttbtt<<3) - sc->sc_tdmaswbaprep;
550310ad9a77SSam Leffler 	bt.bt_nextatim = nexttbtt+1;
5504f3fb1687SAdrian Chadd 	/* Enables TBTT, DBA, SWBA timers by default */
5505f3fb1687SAdrian Chadd 	bt.bt_flags = 0;
550610ad9a77SSam Leffler 	ath_hal_beaconsettimers(ah, &bt);
550710ad9a77SSam Leffler }
550810ad9a77SSam Leffler 
550910ad9a77SSam Leffler /*
551010ad9a77SSam Leffler  * Calculate the beacon interval.  This is periodic in the
551110ad9a77SSam Leffler  * superframe for the bss.  We assume each station is configured
551210ad9a77SSam Leffler  * identically wrt transmit rate so the guard time we calculate
551310ad9a77SSam Leffler  * above will be the same on all stations.  Note we need to
551410ad9a77SSam Leffler  * factor in the xmit time because the hardware will schedule
551510ad9a77SSam Leffler  * a frame for transmit if the start of the frame is within
551610ad9a77SSam Leffler  * the burst time.  When we get hardware that properly kills
551710ad9a77SSam Leffler  * frames in the PCU we can reduce/eliminate the guard time.
551810ad9a77SSam Leffler  *
551910ad9a77SSam Leffler  * Roundup to 1024 is so we have 1 TU buffer in the guard time
552010ad9a77SSam Leffler  * to deal with the granularity of the nexttbtt timer.  11n MAC's
552110ad9a77SSam Leffler  * with 1us timer granularity should allow us to reduce/eliminate
552210ad9a77SSam Leffler  * this.
552310ad9a77SSam Leffler  */
552410ad9a77SSam Leffler static void
552510ad9a77SSam Leffler ath_tdma_bintvalsetup(struct ath_softc *sc,
552610ad9a77SSam Leffler 	const struct ieee80211_tdma_state *tdma)
552710ad9a77SSam Leffler {
552810ad9a77SSam Leffler 	/* copy from vap state (XXX check all vaps have same value?) */
552910ad9a77SSam Leffler 	sc->sc_tdmaslotlen = tdma->tdma_slotlen;
553010ad9a77SSam Leffler 
553110ad9a77SSam Leffler 	sc->sc_tdmabintval = roundup((sc->sc_tdmaslotlen+sc->sc_tdmaguard) *
553210ad9a77SSam Leffler 		tdma->tdma_slotcnt, 1024);
553310ad9a77SSam Leffler 	sc->sc_tdmabintval >>= 10;		/* TSF -> TU */
553410ad9a77SSam Leffler 	if (sc->sc_tdmabintval & 1)
553510ad9a77SSam Leffler 		sc->sc_tdmabintval++;
553610ad9a77SSam Leffler 
553710ad9a77SSam Leffler 	if (tdma->tdma_slot == 0) {
553810ad9a77SSam Leffler 		/*
553910ad9a77SSam Leffler 		 * Only slot 0 beacons; other slots respond.
554010ad9a77SSam Leffler 		 */
554110ad9a77SSam Leffler 		sc->sc_imask |= HAL_INT_SWBA;
554210ad9a77SSam Leffler 		sc->sc_tdmaswba = 0;		/* beacon immediately */
554310ad9a77SSam Leffler 	} else {
554410ad9a77SSam Leffler 		/* XXX all vaps must be slot 0 or slot !0 */
554510ad9a77SSam Leffler 		sc->sc_imask &= ~HAL_INT_SWBA;
554610ad9a77SSam Leffler 	}
554710ad9a77SSam Leffler }
554810ad9a77SSam Leffler 
554910ad9a77SSam Leffler /*
555010ad9a77SSam Leffler  * Max 802.11 overhead.  This assumes no 4-address frames and
555110ad9a77SSam Leffler  * the encapsulation done by ieee80211_encap (llc).  We also
555210ad9a77SSam Leffler  * include potential crypto overhead.
555310ad9a77SSam Leffler  */
555410ad9a77SSam Leffler #define	IEEE80211_MAXOVERHEAD \
555510ad9a77SSam Leffler 	(sizeof(struct ieee80211_qosframe) \
555610ad9a77SSam Leffler 	 + sizeof(struct llc) \
555710ad9a77SSam Leffler 	 + IEEE80211_ADDR_LEN \
555810ad9a77SSam Leffler 	 + IEEE80211_WEP_IVLEN \
555910ad9a77SSam Leffler 	 + IEEE80211_WEP_KIDLEN \
556010ad9a77SSam Leffler 	 + IEEE80211_WEP_CRCLEN \
556110ad9a77SSam Leffler 	 + IEEE80211_WEP_MICLEN \
556210ad9a77SSam Leffler 	 + IEEE80211_CRC_LEN)
556310ad9a77SSam Leffler 
556410ad9a77SSam Leffler /*
556510ad9a77SSam Leffler  * Setup initially for tdma operation.  Start the beacon
556610ad9a77SSam Leffler  * timers and enable SWBA if we are slot 0.  Otherwise
556710ad9a77SSam Leffler  * we wait for slot 0 to arrive so we can sync up before
556810ad9a77SSam Leffler  * starting to transmit.
556910ad9a77SSam Leffler  */
557010ad9a77SSam Leffler static void
557110ad9a77SSam Leffler ath_tdma_config(struct ath_softc *sc, struct ieee80211vap *vap)
557210ad9a77SSam Leffler {
557310ad9a77SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
557410ad9a77SSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
557510ad9a77SSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
557610ad9a77SSam Leffler 	const struct ieee80211_txparam *tp;
557710ad9a77SSam Leffler 	const struct ieee80211_tdma_state *tdma = NULL;
557810ad9a77SSam Leffler 	int rix;
557910ad9a77SSam Leffler 
558010ad9a77SSam Leffler 	if (vap == NULL) {
558110ad9a77SSam Leffler 		vap = TAILQ_FIRST(&ic->ic_vaps);   /* XXX */
558210ad9a77SSam Leffler 		if (vap == NULL) {
558310ad9a77SSam Leffler 			if_printf(ifp, "%s: no vaps?\n", __func__);
558410ad9a77SSam Leffler 			return;
558510ad9a77SSam Leffler 		}
558610ad9a77SSam Leffler 	}
558710ad9a77SSam Leffler 	tp = vap->iv_bss->ni_txparms;
558810ad9a77SSam Leffler 	/*
558910ad9a77SSam Leffler 	 * Calculate the guard time for each slot.  This is the
559010ad9a77SSam Leffler 	 * time to send a maximal-size frame according to the
559110ad9a77SSam Leffler 	 * fixed/lowest transmit rate.  Note that the interface
559210ad9a77SSam Leffler 	 * mtu does not include the 802.11 overhead so we must
559310ad9a77SSam Leffler 	 * tack that on (ath_hal_computetxtime includes the
559410ad9a77SSam Leffler 	 * preamble and plcp in it's calculation).
559510ad9a77SSam Leffler 	 */
559610ad9a77SSam Leffler 	tdma = vap->iv_tdma;
559710ad9a77SSam Leffler 	if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
5598ab06fdf2SSam Leffler 		rix = ath_tx_findrix(sc, tp->ucastrate);
559910ad9a77SSam Leffler 	else
5600ab06fdf2SSam Leffler 		rix = ath_tx_findrix(sc, tp->mcastrate);
560110ad9a77SSam Leffler 	/* XXX short preamble assumed */
560210ad9a77SSam Leffler 	sc->sc_tdmaguard = ath_hal_computetxtime(ah, sc->sc_currates,
560310ad9a77SSam Leffler 		ifp->if_mtu + IEEE80211_MAXOVERHEAD, rix, AH_TRUE);
560410ad9a77SSam Leffler 
560510ad9a77SSam Leffler 	ath_hal_intrset(ah, 0);
560610ad9a77SSam Leffler 
560710ad9a77SSam Leffler 	ath_beaconq_config(sc);			/* setup h/w beacon q */
56089c859a04SSam Leffler 	if (sc->sc_setcca)
560910ad9a77SSam Leffler 		ath_hal_setcca(ah, AH_FALSE);	/* disable CCA */
561010ad9a77SSam Leffler 	ath_tdma_bintvalsetup(sc, tdma);	/* calculate beacon interval */
561110ad9a77SSam Leffler 	ath_tdma_settimers(sc, sc->sc_tdmabintval,
561210ad9a77SSam Leffler 		sc->sc_tdmabintval | HAL_BEACON_RESET_TSF);
561310ad9a77SSam Leffler 	sc->sc_syncbeacon = 0;
561410ad9a77SSam Leffler 
561510ad9a77SSam Leffler 	sc->sc_avgtsfdeltap = TDMA_DUMMY_MARKER;
561610ad9a77SSam Leffler 	sc->sc_avgtsfdeltam = TDMA_DUMMY_MARKER;
561710ad9a77SSam Leffler 
561810ad9a77SSam Leffler 	ath_hal_intrset(ah, sc->sc_imask);
561910ad9a77SSam Leffler 
562010ad9a77SSam Leffler 	DPRINTF(sc, ATH_DEBUG_TDMA, "%s: slot %u len %uus cnt %u "
562110ad9a77SSam Leffler 	    "bsched %u guard %uus bintval %u TU dba prep %u\n", __func__,
562210ad9a77SSam Leffler 	    tdma->tdma_slot, tdma->tdma_slotlen, tdma->tdma_slotcnt,
562310ad9a77SSam Leffler 	    tdma->tdma_bintval, sc->sc_tdmaguard, sc->sc_tdmabintval,
562410ad9a77SSam Leffler 	    sc->sc_tdmadbaprep);
562510ad9a77SSam Leffler }
562610ad9a77SSam Leffler 
562710ad9a77SSam Leffler /*
562810ad9a77SSam Leffler  * Update tdma operation.  Called from the 802.11 layer
562910ad9a77SSam Leffler  * when a beacon is received from the TDMA station operating
563010ad9a77SSam Leffler  * in the slot immediately preceding us in the bss.  Use
563110ad9a77SSam Leffler  * the rx timestamp for the beacon frame to update our
563210ad9a77SSam Leffler  * beacon timers so we follow their schedule.  Note that
563310ad9a77SSam Leffler  * by using the rx timestamp we implicitly include the
563410ad9a77SSam Leffler  * propagation delay in our schedule.
563510ad9a77SSam Leffler  */
563610ad9a77SSam Leffler static void
563710ad9a77SSam Leffler ath_tdma_update(struct ieee80211_node *ni,
56382bc3ce77SSam Leffler 	const struct ieee80211_tdma_param *tdma, int changed)
563910ad9a77SSam Leffler {
564010ad9a77SSam Leffler #define	TSF_TO_TU(_h,_l) \
564110ad9a77SSam Leffler 	((((u_int32_t)(_h)) << 22) | (((u_int32_t)(_l)) >> 10))
564210ad9a77SSam Leffler #define	TU_TO_TSF(_tu)	(((u_int64_t)(_tu)) << 10)
564310ad9a77SSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
564410ad9a77SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
564510ad9a77SSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
564610ad9a77SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
564710ad9a77SSam Leffler 	const HAL_RATE_TABLE *rt = sc->sc_currates;
5648fc4de9b7SAdrian Chadd 	u_int64_t tsf, rstamp, nextslot, nexttbtt;
5649fc4de9b7SAdrian Chadd 	u_int32_t txtime, nextslottu;
565010ad9a77SSam Leffler 	int32_t tudelta, tsfdelta;
565110ad9a77SSam Leffler 	const struct ath_rx_status *rs;
565210ad9a77SSam Leffler 	int rix;
565310ad9a77SSam Leffler 
565410ad9a77SSam Leffler 	sc->sc_stats.ast_tdma_update++;
565510ad9a77SSam Leffler 
565610ad9a77SSam Leffler 	/*
565710ad9a77SSam Leffler 	 * Check for and adopt configuration changes.
565810ad9a77SSam Leffler 	 */
56592bc3ce77SSam Leffler 	if (changed != 0) {
566010ad9a77SSam Leffler 		const struct ieee80211_tdma_state *ts = vap->iv_tdma;
566110ad9a77SSam Leffler 
566210ad9a77SSam Leffler 		ath_tdma_bintvalsetup(sc, ts);
5663040972a1SSam Leffler 		if (changed & TDMA_UPDATE_SLOTLEN)
5664040972a1SSam Leffler 			ath_wme_update(ic);
566510ad9a77SSam Leffler 
566610ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_TDMA,
566710ad9a77SSam Leffler 		    "%s: adopt slot %u slotcnt %u slotlen %u us "
566810ad9a77SSam Leffler 		    "bintval %u TU\n", __func__,
566910ad9a77SSam Leffler 		    ts->tdma_slot, ts->tdma_slotcnt, ts->tdma_slotlen,
567010ad9a77SSam Leffler 		    sc->sc_tdmabintval);
567110ad9a77SSam Leffler 
567210ad9a77SSam Leffler 		/* XXX right? */
567310ad9a77SSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
567410ad9a77SSam Leffler 		/* NB: beacon timers programmed below */
567510ad9a77SSam Leffler 	}
567610ad9a77SSam Leffler 
567710ad9a77SSam Leffler 	/* extend rx timestamp to 64 bits */
56785463c4a4SSam Leffler 	rs = sc->sc_lastrs;
567910ad9a77SSam Leffler 	tsf = ath_hal_gettsf64(ah);
5680fc4de9b7SAdrian Chadd 	rstamp = ath_extend_tsf(sc, rs->rs_tstamp, tsf);
568110ad9a77SSam Leffler 	/*
568210ad9a77SSam Leffler 	 * The rx timestamp is set by the hardware on completing
568310ad9a77SSam Leffler 	 * reception (at the point where the rx descriptor is DMA'd
568410ad9a77SSam Leffler 	 * to the host).  To find the start of our next slot we
568510ad9a77SSam Leffler 	 * must adjust this time by the time required to send
568610ad9a77SSam Leffler 	 * the packet just received.
568710ad9a77SSam Leffler 	 */
568810ad9a77SSam Leffler 	rix = rt->rateCodeToIndex[rs->rs_rate];
568910ad9a77SSam Leffler 	txtime = ath_hal_computetxtime(ah, rt, rs->rs_datalen, rix,
569010ad9a77SSam Leffler 	    rt->info[rix].shortPreamble);
569110ad9a77SSam Leffler 	/* NB: << 9 is to cvt to TU and /2 */
569210ad9a77SSam Leffler 	nextslot = (rstamp - txtime) + (sc->sc_tdmabintval << 9);
569310ad9a77SSam Leffler 	nextslottu = TSF_TO_TU(nextslot>>32, nextslot) & HAL_BEACON_PERIOD;
569410ad9a77SSam Leffler 
569510ad9a77SSam Leffler 	/*
5696fc4de9b7SAdrian Chadd 	 * Retrieve the hardware NextTBTT in usecs
5697fc4de9b7SAdrian Chadd 	 * and calculate the difference between what the
569810ad9a77SSam Leffler 	 * other station thinks and what we have programmed.  This
569910ad9a77SSam Leffler 	 * lets us figure how to adjust our timers to match.  The
570010ad9a77SSam Leffler 	 * adjustments are done by pulling the TSF forward and possibly
570110ad9a77SSam Leffler 	 * rewriting the beacon timers.
570210ad9a77SSam Leffler 	 */
5703fc4de9b7SAdrian Chadd 	nexttbtt = ath_hal_getnexttbtt(ah);
5704fc4de9b7SAdrian Chadd 	tsfdelta = (int32_t)((nextslot % TU_TO_TSF(HAL_BEACON_PERIOD + 1)) - nexttbtt);
570510ad9a77SSam Leffler 
570610ad9a77SSam Leffler 	DPRINTF(sc, ATH_DEBUG_TDMA_TIMER,
570710ad9a77SSam Leffler 	    "tsfdelta %d avg +%d/-%d\n", tsfdelta,
570810ad9a77SSam Leffler 	    TDMA_AVG(sc->sc_avgtsfdeltap), TDMA_AVG(sc->sc_avgtsfdeltam));
570910ad9a77SSam Leffler 
571010ad9a77SSam Leffler 	if (tsfdelta < 0) {
571110ad9a77SSam Leffler 		TDMA_SAMPLE(sc->sc_avgtsfdeltap, 0);
571210ad9a77SSam Leffler 		TDMA_SAMPLE(sc->sc_avgtsfdeltam, -tsfdelta);
571310ad9a77SSam Leffler 		tsfdelta = -tsfdelta % 1024;
571410ad9a77SSam Leffler 		nextslottu++;
571510ad9a77SSam Leffler 	} else if (tsfdelta > 0) {
571610ad9a77SSam Leffler 		TDMA_SAMPLE(sc->sc_avgtsfdeltap, tsfdelta);
571710ad9a77SSam Leffler 		TDMA_SAMPLE(sc->sc_avgtsfdeltam, 0);
571810ad9a77SSam Leffler 		tsfdelta = 1024 - (tsfdelta % 1024);
571910ad9a77SSam Leffler 		nextslottu++;
572010ad9a77SSam Leffler 	} else {
572110ad9a77SSam Leffler 		TDMA_SAMPLE(sc->sc_avgtsfdeltap, 0);
572210ad9a77SSam Leffler 		TDMA_SAMPLE(sc->sc_avgtsfdeltam, 0);
572310ad9a77SSam Leffler 	}
5724fc4de9b7SAdrian Chadd 	tudelta = nextslottu - TSF_TO_TU(nexttbtt >> 32, nexttbtt);
572510ad9a77SSam Leffler 
572610ad9a77SSam Leffler 	/*
572710ad9a77SSam Leffler 	 * Copy sender's timetstamp into tdma ie so they can
572810ad9a77SSam Leffler 	 * calculate roundtrip time.  We submit a beacon frame
572910ad9a77SSam Leffler 	 * below after any timer adjustment.  The frame goes out
573010ad9a77SSam Leffler 	 * at the next TBTT so the sender can calculate the
573110ad9a77SSam Leffler 	 * roundtrip by inspecting the tdma ie in our beacon frame.
573210ad9a77SSam Leffler 	 *
573310ad9a77SSam Leffler 	 * NB: This tstamp is subtlely preserved when
573410ad9a77SSam Leffler 	 *     IEEE80211_BEACON_TDMA is marked (e.g. when the
573510ad9a77SSam Leffler 	 *     slot position changes) because ieee80211_add_tdma
573610ad9a77SSam Leffler 	 *     skips over the data.
573710ad9a77SSam Leffler 	 */
573810ad9a77SSam Leffler 	memcpy(ATH_VAP(vap)->av_boff.bo_tdma +
573910ad9a77SSam Leffler 		__offsetof(struct ieee80211_tdma_param, tdma_tstamp),
574010ad9a77SSam Leffler 		&ni->ni_tstamp.data, 8);
574110ad9a77SSam Leffler #if 0
574210ad9a77SSam Leffler 	DPRINTF(sc, ATH_DEBUG_TDMA_TIMER,
5743fc4de9b7SAdrian Chadd 	    "tsf %llu nextslot %llu (%d, %d) nextslottu %u nexttbtt %llu (%d)\n",
574410ad9a77SSam Leffler 	    (unsigned long long) tsf, (unsigned long long) nextslot,
5745fc4de9b7SAdrian Chadd 	    (int)(nextslot - tsf), tsfdelta, nextslottu, nexttbtt, tudelta);
574610ad9a77SSam Leffler #endif
574710ad9a77SSam Leffler 	/*
574810ad9a77SSam Leffler 	 * Adjust the beacon timers only when pulling them forward
574910ad9a77SSam Leffler 	 * or when going back by less than the beacon interval.
575010ad9a77SSam Leffler 	 * Negative jumps larger than the beacon interval seem to
575110ad9a77SSam Leffler 	 * cause the timers to stop and generally cause instability.
575210ad9a77SSam Leffler 	 * This basically filters out jumps due to missed beacons.
575310ad9a77SSam Leffler 	 */
575410ad9a77SSam Leffler 	if (tudelta != 0 && (tudelta > 0 || -tudelta < sc->sc_tdmabintval)) {
575510ad9a77SSam Leffler 		ath_tdma_settimers(sc, nextslottu, sc->sc_tdmabintval);
575610ad9a77SSam Leffler 		sc->sc_stats.ast_tdma_timers++;
575710ad9a77SSam Leffler 	}
575810ad9a77SSam Leffler 	if (tsfdelta > 0) {
575910ad9a77SSam Leffler 		ath_hal_adjusttsf(ah, tsfdelta);
576010ad9a77SSam Leffler 		sc->sc_stats.ast_tdma_tsf++;
576110ad9a77SSam Leffler 	}
576210ad9a77SSam Leffler 	ath_tdma_beacon_send(sc, vap);		/* prepare response */
576310ad9a77SSam Leffler #undef TU_TO_TSF
576410ad9a77SSam Leffler #undef TSF_TO_TU
576510ad9a77SSam Leffler }
576610ad9a77SSam Leffler 
576710ad9a77SSam Leffler /*
576810ad9a77SSam Leffler  * Transmit a beacon frame at SWBA.  Dynamic updates
576910ad9a77SSam Leffler  * to the frame contents are done as needed.
577010ad9a77SSam Leffler  */
577110ad9a77SSam Leffler static void
577210ad9a77SSam Leffler ath_tdma_beacon_send(struct ath_softc *sc, struct ieee80211vap *vap)
577310ad9a77SSam Leffler {
577410ad9a77SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
577510ad9a77SSam Leffler 	struct ath_buf *bf;
577610ad9a77SSam Leffler 	int otherant;
577710ad9a77SSam Leffler 
577810ad9a77SSam Leffler 	/*
577910ad9a77SSam Leffler 	 * Check if the previous beacon has gone out.  If
578010ad9a77SSam Leffler 	 * not don't try to post another, skip this period
578110ad9a77SSam Leffler 	 * and wait for the next.  Missed beacons indicate
578210ad9a77SSam Leffler 	 * a problem and should not occur.  If we miss too
578310ad9a77SSam Leffler 	 * many consecutive beacons reset the device.
578410ad9a77SSam Leffler 	 */
578510ad9a77SSam Leffler 	if (ath_hal_numtxpending(ah, sc->sc_bhalq) != 0) {
578610ad9a77SSam Leffler 		sc->sc_bmisscount++;
578710ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
578810ad9a77SSam Leffler 			"%s: missed %u consecutive beacons\n",
578910ad9a77SSam Leffler 			__func__, sc->sc_bmisscount);
5790a32ac9d3SSam Leffler 		if (sc->sc_bmisscount >= ath_bstuck_threshold)
579110ad9a77SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_bstucktask);
579210ad9a77SSam Leffler 		return;
579310ad9a77SSam Leffler 	}
579410ad9a77SSam Leffler 	if (sc->sc_bmisscount != 0) {
579510ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
579610ad9a77SSam Leffler 			"%s: resume beacon xmit after %u misses\n",
579710ad9a77SSam Leffler 			__func__, sc->sc_bmisscount);
579810ad9a77SSam Leffler 		sc->sc_bmisscount = 0;
579910ad9a77SSam Leffler 	}
580010ad9a77SSam Leffler 
580110ad9a77SSam Leffler 	/*
580210ad9a77SSam Leffler 	 * Check recent per-antenna transmit statistics and flip
580310ad9a77SSam Leffler 	 * the default antenna if noticeably more frames went out
580410ad9a77SSam Leffler 	 * on the non-default antenna.
580510ad9a77SSam Leffler 	 * XXX assumes 2 anntenae
580610ad9a77SSam Leffler 	 */
580710ad9a77SSam Leffler 	if (!sc->sc_diversity) {
580810ad9a77SSam Leffler 		otherant = sc->sc_defant & 1 ? 2 : 1;
580910ad9a77SSam Leffler 		if (sc->sc_ant_tx[otherant] > sc->sc_ant_tx[sc->sc_defant] + 2)
581010ad9a77SSam Leffler 			ath_setdefantenna(sc, otherant);
581110ad9a77SSam Leffler 		sc->sc_ant_tx[1] = sc->sc_ant_tx[2] = 0;
581210ad9a77SSam Leffler 	}
581310ad9a77SSam Leffler 
581410ad9a77SSam Leffler 	bf = ath_beacon_generate(sc, vap);
581510ad9a77SSam Leffler 	if (bf != NULL) {
581610ad9a77SSam Leffler 		/*
581710ad9a77SSam Leffler 		 * Stop any current dma and put the new frame on the queue.
581810ad9a77SSam Leffler 		 * This should never fail since we check above that no frames
581910ad9a77SSam Leffler 		 * are still pending on the queue.
582010ad9a77SSam Leffler 		 */
582110ad9a77SSam Leffler 		if (!ath_hal_stoptxdma(ah, sc->sc_bhalq)) {
582210ad9a77SSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
582310ad9a77SSam Leffler 				"%s: beacon queue %u did not stop?\n",
582410ad9a77SSam Leffler 				__func__, sc->sc_bhalq);
582510ad9a77SSam Leffler 			/* NB: the HAL still stops DMA, so proceed */
582610ad9a77SSam Leffler 		}
582710ad9a77SSam Leffler 		ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr);
582810ad9a77SSam Leffler 		ath_hal_txstart(ah, sc->sc_bhalq);
582910ad9a77SSam Leffler 
583010ad9a77SSam Leffler 		sc->sc_stats.ast_be_xmit++;		/* XXX per-vap? */
583110ad9a77SSam Leffler 
583210ad9a77SSam Leffler 		/*
583310ad9a77SSam Leffler 		 * Record local TSF for our last send for use
583410ad9a77SSam Leffler 		 * in arbitrating slot collisions.
583510ad9a77SSam Leffler 		 */
583610ad9a77SSam Leffler 		vap->iv_bss->ni_tstamp.tsf = ath_hal_gettsf64(ah);
583710ad9a77SSam Leffler 	}
583810ad9a77SSam Leffler }
5839584f7327SSam Leffler #endif /* IEEE80211_SUPPORT_TDMA */
5840e8dabfbeSAdrian Chadd 
584148237774SAdrian Chadd static void
584248237774SAdrian Chadd ath_dfs_tasklet(void *p, int npending)
584348237774SAdrian Chadd {
584448237774SAdrian Chadd 	struct ath_softc *sc = (struct ath_softc *) p;
584548237774SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
584648237774SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
584748237774SAdrian Chadd 
584848237774SAdrian Chadd 	/*
584948237774SAdrian Chadd 	 * If previous processing has found a radar event,
585048237774SAdrian Chadd 	 * signal this to the net80211 layer to begin DFS
585148237774SAdrian Chadd 	 * processing.
585248237774SAdrian Chadd 	 */
585348237774SAdrian Chadd 	if (ath_dfs_process_radar_event(sc, sc->sc_curchan)) {
585448237774SAdrian Chadd 		/* DFS event found, initiate channel change */
585548237774SAdrian Chadd 		ieee80211_dfs_notify_radar(ic, sc->sc_curchan);
585648237774SAdrian Chadd 	}
585748237774SAdrian Chadd }
585848237774SAdrian Chadd 
5859dba9c859SAdrian Chadd MODULE_VERSION(if_ath, 1);
5860dba9c859SAdrian Chadd MODULE_DEPEND(if_ath, wlan, 1, 1, 1);          /* 802.11 media layer */
5861