xref: /freebsd/sys/dev/ath/if_ath.c (revision a614e0762d6b33ff205d53551865439d5cd2bdd1)
15591b213SSam Leffler /*-
2cb344d95SSam Leffler  * Copyright (c) 2002-2004 Sam Leffler, Errno Consulting
35591b213SSam Leffler  * All rights reserved.
45591b213SSam Leffler  *
55591b213SSam Leffler  * Redistribution and use in source and binary forms, with or without
65591b213SSam Leffler  * modification, are permitted provided that the following conditions
75591b213SSam Leffler  * are met:
85591b213SSam Leffler  * 1. Redistributions of source code must retain the above copyright
95591b213SSam Leffler  *    notice, this list of conditions and the following disclaimer,
105591b213SSam Leffler  *    without modification.
115591b213SSam Leffler  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
125591b213SSam Leffler  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
135591b213SSam Leffler  *    redistribution must be conditioned upon including a substantially
145591b213SSam Leffler  *    similar Disclaimer requirement for further binary redistribution.
155591b213SSam Leffler  * 3. Neither the names of the above-listed copyright holders nor the names
165591b213SSam Leffler  *    of any contributors may be used to endorse or promote products derived
175591b213SSam Leffler  *    from this software without specific prior written permission.
185591b213SSam Leffler  *
195591b213SSam Leffler  * Alternatively, this software may be distributed under the terms of the
205591b213SSam Leffler  * GNU General Public License ("GPL") version 2 as published by the Free
215591b213SSam Leffler  * Software Foundation.
225591b213SSam Leffler  *
235591b213SSam Leffler  * NO WARRANTY
245591b213SSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
255591b213SSam Leffler  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
265591b213SSam Leffler  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
275591b213SSam Leffler  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
285591b213SSam Leffler  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
295591b213SSam Leffler  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
305591b213SSam Leffler  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
315591b213SSam Leffler  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
325591b213SSam Leffler  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
335591b213SSam Leffler  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
345591b213SSam Leffler  * THE POSSIBILITY OF SUCH DAMAGES.
355591b213SSam Leffler  */
365591b213SSam Leffler 
375591b213SSam Leffler #include <sys/cdefs.h>
385591b213SSam Leffler __FBSDID("$FreeBSD$");
395591b213SSam Leffler 
405591b213SSam Leffler /*
415591b213SSam Leffler  * Driver for the Atheros Wireless LAN controller.
425f3721d5SSam Leffler  *
435f3721d5SSam Leffler  * This software is derived from work of Atsushi Onoe; his contribution
445f3721d5SSam Leffler  * is greatly appreciated.
455591b213SSam Leffler  */
465591b213SSam Leffler 
475591b213SSam Leffler #include "opt_inet.h"
485591b213SSam Leffler 
495591b213SSam Leffler #include <sys/param.h>
505591b213SSam Leffler #include <sys/systm.h>
515591b213SSam Leffler #include <sys/sysctl.h>
525591b213SSam Leffler #include <sys/mbuf.h>
535591b213SSam Leffler #include <sys/malloc.h>
545591b213SSam Leffler #include <sys/lock.h>
555591b213SSam Leffler #include <sys/mutex.h>
565591b213SSam Leffler #include <sys/kernel.h>
575591b213SSam Leffler #include <sys/socket.h>
585591b213SSam Leffler #include <sys/sockio.h>
595591b213SSam Leffler #include <sys/errno.h>
605591b213SSam Leffler #include <sys/callout.h>
615591b213SSam Leffler #include <sys/bus.h>
625591b213SSam Leffler #include <sys/endian.h>
635591b213SSam Leffler 
645591b213SSam Leffler #include <machine/bus.h>
655591b213SSam Leffler 
665591b213SSam Leffler #include <net/if.h>
675591b213SSam Leffler #include <net/if_dl.h>
685591b213SSam Leffler #include <net/if_media.h>
695591b213SSam Leffler #include <net/if_arp.h>
705591b213SSam Leffler #include <net/ethernet.h>
715591b213SSam Leffler #include <net/if_llc.h>
725591b213SSam Leffler 
735591b213SSam Leffler #include <net80211/ieee80211_var.h>
745591b213SSam Leffler 
755591b213SSam Leffler #include <net/bpf.h>
765591b213SSam Leffler 
775591b213SSam Leffler #ifdef INET
785591b213SSam Leffler #include <netinet/in.h>
795591b213SSam Leffler #include <netinet/if_ether.h>
805591b213SSam Leffler #endif
815591b213SSam Leffler 
825591b213SSam Leffler #define	AR_DEBUG
835591b213SSam Leffler #include <dev/ath/if_athvar.h>
845591b213SSam Leffler #include <contrib/dev/ath/ah_desc.h>
85c42a7b7eSSam Leffler #include <contrib/dev/ath/ah_devid.h>		/* XXX for softled */
865591b213SSam Leffler 
875591b213SSam Leffler /* unalligned little endian access */
885591b213SSam Leffler #define LE_READ_2(p)							\
895591b213SSam Leffler 	((u_int16_t)							\
905591b213SSam Leffler 	 ((((u_int8_t *)(p))[0]      ) | (((u_int8_t *)(p))[1] <<  8)))
915591b213SSam Leffler #define LE_READ_4(p)							\
925591b213SSam Leffler 	((u_int32_t)							\
935591b213SSam Leffler 	 ((((u_int8_t *)(p))[0]      ) | (((u_int8_t *)(p))[1] <<  8) |	\
945591b213SSam Leffler 	  (((u_int8_t *)(p))[2] << 16) | (((u_int8_t *)(p))[3] << 24)))
955591b213SSam Leffler 
965591b213SSam Leffler static void	ath_init(void *);
97c42a7b7eSSam Leffler static void	ath_stop_locked(struct ifnet *);
985591b213SSam Leffler static void	ath_stop(struct ifnet *);
995591b213SSam Leffler static void	ath_start(struct ifnet *);
100c42a7b7eSSam Leffler static int	ath_reset(struct ifnet *);
1015591b213SSam Leffler static int	ath_media_change(struct ifnet *);
1025591b213SSam Leffler static void	ath_watchdog(struct ifnet *);
1035591b213SSam Leffler static int	ath_ioctl(struct ifnet *, u_long, caddr_t);
1045591b213SSam Leffler static void	ath_fatal_proc(void *, int);
1055591b213SSam Leffler static void	ath_rxorn_proc(void *, int);
1065591b213SSam Leffler static void	ath_bmiss_proc(void *, int);
1075591b213SSam Leffler static void	ath_initkeytable(struct ath_softc *);
108c42a7b7eSSam Leffler static int	ath_key_alloc(struct ieee80211com *,
109c42a7b7eSSam Leffler 			const struct ieee80211_key *);
110c42a7b7eSSam Leffler static int	ath_key_delete(struct ieee80211com *,
111c42a7b7eSSam Leffler 			const struct ieee80211_key *);
112c42a7b7eSSam Leffler static int	ath_key_set(struct ieee80211com *, const struct ieee80211_key *,
113c42a7b7eSSam Leffler 			const u_int8_t mac[IEEE80211_ADDR_LEN]);
114c42a7b7eSSam Leffler static void	ath_key_update_begin(struct ieee80211com *);
115c42a7b7eSSam Leffler static void	ath_key_update_end(struct ieee80211com *);
1165591b213SSam Leffler static void	ath_mode_init(struct ath_softc *);
117c42a7b7eSSam Leffler static void	ath_setslottime(struct ath_softc *);
118c42a7b7eSSam Leffler static void	ath_updateslot(struct ifnet *);
1195591b213SSam Leffler static int	ath_beacon_alloc(struct ath_softc *, struct ieee80211_node *);
120c42a7b7eSSam Leffler static void	ath_beacon_setup(struct ath_softc *, struct ath_buf *);
1215591b213SSam Leffler static void	ath_beacon_proc(void *, int);
122c42a7b7eSSam Leffler static void	ath_bstuck_proc(void *, int);
1235591b213SSam Leffler static void	ath_beacon_free(struct ath_softc *);
1245591b213SSam Leffler static void	ath_beacon_config(struct ath_softc *);
125c42a7b7eSSam Leffler static void	ath_descdma_cleanup(struct ath_softc *sc,
126c42a7b7eSSam Leffler 			struct ath_descdma *, ath_bufhead *);
1275591b213SSam Leffler static int	ath_desc_alloc(struct ath_softc *);
1285591b213SSam Leffler static void	ath_desc_free(struct ath_softc *);
129c42a7b7eSSam Leffler static struct ieee80211_node *ath_node_alloc(struct ieee80211_node_table *);
130c42a7b7eSSam Leffler static void	ath_node_free(struct ieee80211_node *);
131c42a7b7eSSam Leffler static u_int8_t	ath_node_getrssi(const struct ieee80211_node *);
1325591b213SSam Leffler static int	ath_rxbuf_init(struct ath_softc *, struct ath_buf *);
133c42a7b7eSSam Leffler static void	ath_recv_mgmt(struct ieee80211com *ic, struct mbuf *m,
134c42a7b7eSSam Leffler 			struct ieee80211_node *ni,
135c42a7b7eSSam Leffler 			int subtype, int rssi, u_int32_t rstamp);
136c42a7b7eSSam Leffler static void	ath_setdefantenna(struct ath_softc *, u_int);
1375591b213SSam Leffler static void	ath_rx_proc(void *, int);
138c42a7b7eSSam Leffler static struct ath_txq *ath_txq_setup(struct ath_softc*, int qtype, int subtype);
139c42a7b7eSSam Leffler static int	ath_tx_setup(struct ath_softc *, int, int);
140c42a7b7eSSam Leffler static int	ath_wme_update(struct ieee80211com *);
141c42a7b7eSSam Leffler static void	ath_tx_cleanupq(struct ath_softc *, struct ath_txq *);
142c42a7b7eSSam Leffler static void	ath_tx_cleanup(struct ath_softc *);
1435591b213SSam Leffler static int	ath_tx_start(struct ath_softc *, struct ieee80211_node *,
1445591b213SSam Leffler 			     struct ath_buf *, struct mbuf *);
145c42a7b7eSSam Leffler static void	ath_tx_proc_q0(void *, int);
146c42a7b7eSSam Leffler static void	ath_tx_proc_q0123(void *, int);
1475591b213SSam Leffler static void	ath_tx_proc(void *, int);
1485591b213SSam Leffler static int	ath_chan_set(struct ath_softc *, struct ieee80211_channel *);
1495591b213SSam Leffler static void	ath_draintxq(struct ath_softc *);
1505591b213SSam Leffler static void	ath_stoprecv(struct ath_softc *);
1515591b213SSam Leffler static int	ath_startrecv(struct ath_softc *);
152c42a7b7eSSam Leffler static void	ath_chan_change(struct ath_softc *, struct ieee80211_channel *);
1535591b213SSam Leffler static void	ath_next_scan(void *);
1545591b213SSam Leffler static void	ath_calibrate(void *);
15545bbf62fSSam Leffler static int	ath_newstate(struct ieee80211com *, enum ieee80211_state, int);
1565591b213SSam Leffler static void	ath_newassoc(struct ieee80211com *,
1575591b213SSam Leffler 			struct ieee80211_node *, int);
158c42a7b7eSSam Leffler static int	ath_getchannels(struct ath_softc *, u_int cc,
159c42a7b7eSSam Leffler 			HAL_BOOL outdoor, HAL_BOOL xchanmode);
160c42a7b7eSSam Leffler static void	ath_update_led(struct ath_softc *);
161c42a7b7eSSam Leffler static void	ath_update_txpow(struct ath_softc *);
1625591b213SSam Leffler 
163c42a7b7eSSam Leffler static int	ath_rate_setup(struct ath_softc *, u_int mode);
1645591b213SSam Leffler static void	ath_setcurmode(struct ath_softc *, enum ieee80211_phymode);
165c42a7b7eSSam Leffler 
166c42a7b7eSSam Leffler static void	ath_sysctlattach(struct ath_softc *);
167c42a7b7eSSam Leffler static void	ath_bpfattach(struct ath_softc *);
168c42a7b7eSSam Leffler static void	ath_announce(struct ath_softc *);
1695591b213SSam Leffler 
1705591b213SSam Leffler SYSCTL_DECL(_hw_ath);
1715591b213SSam Leffler 
1725591b213SSam Leffler /* XXX validate sysctl values */
1735591b213SSam Leffler static	int ath_dwelltime = 200;		/* 5 channels/second */
1745591b213SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, dwell, CTLFLAG_RW, &ath_dwelltime,
1755591b213SSam Leffler 	    0, "channel dwell time (ms) for AP/station scanning");
1765591b213SSam Leffler static	int ath_calinterval = 30;		/* calibrate every 30 secs */
1775591b213SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, calibrate, CTLFLAG_RW, &ath_calinterval,
1785591b213SSam Leffler 	    0, "chip calibration interval (secs)");
17945cabbdcSSam Leffler static	int ath_outdoor = AH_TRUE;		/* outdoor operation */
18045cabbdcSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, outdoor, CTLFLAG_RD, &ath_outdoor,
181c42a7b7eSSam Leffler 	    0, "outdoor operation");
1828c0370b7SSam Leffler TUNABLE_INT("hw.ath.outdoor", &ath_outdoor);
183c42a7b7eSSam Leffler static	int ath_xchanmode = AH_TRUE;		/* extended channel use */
184c42a7b7eSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, xchanmode, CTLFLAG_RD, &ath_xchanmode,
185c42a7b7eSSam Leffler 	    0, "extended channel mode");
186c42a7b7eSSam Leffler TUNABLE_INT("hw.ath.xchanmode", &ath_xchanmode);
18745cabbdcSSam Leffler static	int ath_countrycode = CTRY_DEFAULT;	/* country code */
18845cabbdcSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, countrycode, CTLFLAG_RD, &ath_countrycode,
18945cabbdcSSam Leffler 	    0, "country code");
1908c0370b7SSam Leffler TUNABLE_INT("hw.ath.countrycode", &ath_countrycode);
19145cabbdcSSam Leffler static	int ath_regdomain = 0;			/* regulatory domain */
19245cabbdcSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, regdomain, CTLFLAG_RD, &ath_regdomain,
19345cabbdcSSam Leffler 	    0, "regulatory domain");
1945591b213SSam Leffler 
1955591b213SSam Leffler #ifdef AR_DEBUG
196c42a7b7eSSam Leffler static	int ath_debug = 0;
1975591b213SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, debug, CTLFLAG_RW, &ath_debug,
1985591b213SSam Leffler 	    0, "control debugging printfs");
199f3be7956SSam Leffler TUNABLE_INT("hw.ath.debug", &ath_debug);
200e325e530SSam Leffler enum {
201e325e530SSam Leffler 	ATH_DEBUG_XMIT		= 0x00000001,	/* basic xmit operation */
202e325e530SSam Leffler 	ATH_DEBUG_XMIT_DESC	= 0x00000002,	/* xmit descriptors */
203e325e530SSam Leffler 	ATH_DEBUG_RECV		= 0x00000004,	/* basic recv operation */
204e325e530SSam Leffler 	ATH_DEBUG_RECV_DESC	= 0x00000008,	/* recv descriptors */
205e325e530SSam Leffler 	ATH_DEBUG_RATE		= 0x00000010,	/* rate control */
206e325e530SSam Leffler 	ATH_DEBUG_RESET		= 0x00000020,	/* reset processing */
207e325e530SSam Leffler 	ATH_DEBUG_MODE		= 0x00000040,	/* mode init/setup */
208e325e530SSam Leffler 	ATH_DEBUG_BEACON 	= 0x00000080,	/* beacon handling */
209e325e530SSam Leffler 	ATH_DEBUG_WATCHDOG 	= 0x00000100,	/* watchdog timeout */
210e325e530SSam Leffler 	ATH_DEBUG_INTR		= 0x00001000,	/* ISR */
211e325e530SSam Leffler 	ATH_DEBUG_TX_PROC	= 0x00002000,	/* tx ISR proc */
212e325e530SSam Leffler 	ATH_DEBUG_RX_PROC	= 0x00004000,	/* rx ISR proc */
213e325e530SSam Leffler 	ATH_DEBUG_BEACON_PROC	= 0x00008000,	/* beacon ISR proc */
214e325e530SSam Leffler 	ATH_DEBUG_CALIBRATE	= 0x00010000,	/* periodic calibration */
215c42a7b7eSSam Leffler 	ATH_DEBUG_KEYCACHE	= 0x00020000,	/* key cache management */
216c42a7b7eSSam Leffler 	ATH_DEBUG_STATE		= 0x00040000,	/* 802.11 state transitions */
217c42a7b7eSSam Leffler 	ATH_DEBUG_NODE		= 0x00080000,	/* node management */
218c42a7b7eSSam Leffler 	ATH_DEBUG_FATAL		= 0x80000000,	/* fatal errors */
219e325e530SSam Leffler 	ATH_DEBUG_ANY		= 0xffffffff
220e325e530SSam Leffler };
221c42a7b7eSSam Leffler #define	IFF_DUMPPKTS(sc, m) \
222c42a7b7eSSam Leffler 	((sc->sc_debug & m) || \
223c42a7b7eSSam Leffler 	    (sc->sc_if.if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
224c42a7b7eSSam Leffler #define	DPRINTF(sc, m, fmt, ...) do {				\
225c42a7b7eSSam Leffler 	if (sc->sc_debug & m)					\
226c42a7b7eSSam Leffler 		printf(fmt, __VA_ARGS__);			\
227c42a7b7eSSam Leffler } while (0)
228c42a7b7eSSam Leffler #define	KEYPRINTF(sc, ix, hk, mac) do {				\
229c42a7b7eSSam Leffler 	if (sc->sc_debug & ATH_DEBUG_KEYCACHE)			\
230c42a7b7eSSam Leffler 		ath_keyprint(__func__, ix, hk, mac);		\
231c42a7b7eSSam Leffler } while (0)
232c42a7b7eSSam Leffler static	void ath_printrxbuf(struct ath_buf *bf, int);
233c42a7b7eSSam Leffler static	void ath_printtxbuf(struct ath_buf *bf, int);
2345591b213SSam Leffler #else
235c42a7b7eSSam Leffler #define	IFF_DUMPPKTS(sc, m) \
236c42a7b7eSSam Leffler 	((sc->sc_if.if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
237c42a7b7eSSam Leffler #define	DPRINTF(m, fmt, ...)
238c42a7b7eSSam Leffler #define	KEYPRINTF(sc, k, ix, mac)
2395591b213SSam Leffler #endif
2405591b213SSam Leffler 
241c42a7b7eSSam Leffler MALLOC_DEFINE(M_ATHDEV, "athdev", "ath driver dma buffers");
242c42a7b7eSSam Leffler 
2435591b213SSam Leffler int
2445591b213SSam Leffler ath_attach(u_int16_t devid, struct ath_softc *sc)
2455591b213SSam Leffler {
246c42a7b7eSSam Leffler 	struct ifnet *ifp = &sc->sc_if;
2475591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
2485591b213SSam Leffler 	struct ath_hal *ah;
2495591b213SSam Leffler 	HAL_STATUS status;
250c42a7b7eSSam Leffler 	int error = 0, i;
2515591b213SSam Leffler 
252c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: devid 0x%x\n", __func__, devid);
2535591b213SSam Leffler 
2545591b213SSam Leffler 	/* set these up early for if_printf use */
2559bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(sc->sc_dev),
2569bf40edeSBrooks Davis 		device_get_unit(sc->sc_dev));
2575591b213SSam Leffler 
2585591b213SSam Leffler 	ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh, &status);
2595591b213SSam Leffler 	if (ah == NULL) {
2605591b213SSam Leffler 		if_printf(ifp, "unable to attach hardware; HAL status %u\n",
2615591b213SSam Leffler 			status);
2625591b213SSam Leffler 		error = ENXIO;
2635591b213SSam Leffler 		goto bad;
2645591b213SSam Leffler 	}
26585bdc65aSSam Leffler 	if (ah->ah_abi != HAL_ABI_VERSION) {
266c42a7b7eSSam Leffler 		if_printf(ifp, "HAL ABI mismatch detected "
267c42a7b7eSSam Leffler 			"(HAL:0x%x != driver:0x%x)\n",
26885bdc65aSSam Leffler 			ah->ah_abi, HAL_ABI_VERSION);
26985bdc65aSSam Leffler 		error = ENXIO;
27085bdc65aSSam Leffler 		goto bad;
27185bdc65aSSam Leffler 	}
2725591b213SSam Leffler 	sc->sc_ah = ah;
273b58b3803SSam Leffler 	sc->sc_invalid = 0;	/* ready to go, enable interrupt handling */
2745591b213SSam Leffler 
2755591b213SSam Leffler 	/*
276c42a7b7eSSam Leffler 	 * Check if the MAC has multi-rate retry support.
277c42a7b7eSSam Leffler 	 * We do this by trying to setup a fake extended
278c42a7b7eSSam Leffler 	 * descriptor.  MAC's that don't have support will
279c42a7b7eSSam Leffler 	 * return false w/o doing anything.  MAC's that do
280c42a7b7eSSam Leffler 	 * support it will return true w/o doing anything.
281c42a7b7eSSam Leffler 	 */
282c42a7b7eSSam Leffler 	sc->sc_mrretry = ath_hal_setupxtxdesc(ah, NULL, 0,0, 0,0, 0,0);
283c42a7b7eSSam Leffler 
284c42a7b7eSSam Leffler 	/*
285c42a7b7eSSam Leffler 	 * Check if the device has hardware counters for PHY
286c42a7b7eSSam Leffler 	 * errors.  If so we need to enable the MIB interrupt
287c42a7b7eSSam Leffler 	 * so we can act on stat triggers.
288c42a7b7eSSam Leffler 	 */
289c42a7b7eSSam Leffler 	if (ath_hal_hwphycounters(ah))
290c42a7b7eSSam Leffler 		sc->sc_needmib = 1;
291c42a7b7eSSam Leffler 
292c42a7b7eSSam Leffler 	/*
293c42a7b7eSSam Leffler 	 * Get the hardware key cache size.
294c42a7b7eSSam Leffler 	 */
295c42a7b7eSSam Leffler 	sc->sc_keymax = ath_hal_keycachesize(ah);
296c42a7b7eSSam Leffler 	if (sc->sc_keymax > sizeof(sc->sc_keymap) * NBBY) {
2976891c875SPeter Wemm 		if_printf(ifp,
2986891c875SPeter Wemm 			"Warning, using only %zu of %u key cache slots\n",
299c42a7b7eSSam Leffler 			sizeof(sc->sc_keymap) * NBBY, sc->sc_keymax);
300c42a7b7eSSam Leffler 		sc->sc_keymax = sizeof(sc->sc_keymap) * NBBY;
301c42a7b7eSSam Leffler 	}
302c42a7b7eSSam Leffler 	/*
303c42a7b7eSSam Leffler 	 * Reset the key cache since some parts do not
304c42a7b7eSSam Leffler 	 * reset the contents on initial power up.
305c42a7b7eSSam Leffler 	 */
306c42a7b7eSSam Leffler 	for (i = 0; i < sc->sc_keymax; i++)
307c42a7b7eSSam Leffler 		ath_hal_keyreset(ah, i);
308c42a7b7eSSam Leffler 	/*
309c42a7b7eSSam Leffler 	 * Mark key cache slots associated with global keys
310c42a7b7eSSam Leffler 	 * as in use.  If we knew TKIP was not to be used we
311c42a7b7eSSam Leffler 	 * could leave the +32, +64, and +32+64 slots free.
312c42a7b7eSSam Leffler 	 * XXX only for splitmic.
313c42a7b7eSSam Leffler 	 */
314c42a7b7eSSam Leffler 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
315c42a7b7eSSam Leffler 		setbit(sc->sc_keymap, i);
316c42a7b7eSSam Leffler 		setbit(sc->sc_keymap, i+32);
317c42a7b7eSSam Leffler 		setbit(sc->sc_keymap, i+64);
318c42a7b7eSSam Leffler 		setbit(sc->sc_keymap, i+32+64);
319c42a7b7eSSam Leffler 	}
320c42a7b7eSSam Leffler 
321c42a7b7eSSam Leffler 	/*
3225591b213SSam Leffler 	 * Collect the channel list using the default country
3235591b213SSam Leffler 	 * code and including outdoor channels.  The 802.11 layer
32445cabbdcSSam Leffler 	 * is resposible for filtering this list based on settings
32545cabbdcSSam Leffler 	 * like the phy mode.
3265591b213SSam Leffler 	 */
327c42a7b7eSSam Leffler 	error = ath_getchannels(sc, ath_countrycode,
328c42a7b7eSSam Leffler 			ath_outdoor, ath_xchanmode);
3295591b213SSam Leffler 	if (error != 0)
3305591b213SSam Leffler 		goto bad;
33145cabbdcSSam Leffler 	/*
332c42a7b7eSSam Leffler 	 * Setup dynamic sysctl's now that country code and
333c42a7b7eSSam Leffler 	 * regdomain are available from the hal.
33445cabbdcSSam Leffler 	 */
335c42a7b7eSSam Leffler 	ath_sysctlattach(sc);
3365591b213SSam Leffler 
3375591b213SSam Leffler 	/*
3385591b213SSam Leffler 	 * Setup rate tables for all potential media types.
3395591b213SSam Leffler 	 */
3405591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11A);
3415591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11B);
3425591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11G);
343c42a7b7eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_A);
344c42a7b7eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_G);
345c42a7b7eSSam Leffler 	/* NB: setup here so ath_rate_update is happy */
346c42a7b7eSSam Leffler 	ath_setcurmode(sc, IEEE80211_MODE_11A);
3475591b213SSam Leffler 
348c42a7b7eSSam Leffler 	/*
349c42a7b7eSSam Leffler 	 * Allocate tx+rx descriptors and populate the lists.
350c42a7b7eSSam Leffler 	 */
3515591b213SSam Leffler 	error = ath_desc_alloc(sc);
3525591b213SSam Leffler 	if (error != 0) {
3535591b213SSam Leffler 		if_printf(ifp, "failed to allocate descriptors: %d\n", error);
3545591b213SSam Leffler 		goto bad;
3555591b213SSam Leffler 	}
356e383b240SSam Leffler 	callout_init(&sc->sc_scan_ch, debug_mpsafenet ? CALLOUT_MPSAFE : 0);
3572274d8c8SSam Leffler 	callout_init(&sc->sc_cal_ch, CALLOUT_MPSAFE);
3585591b213SSam Leffler 
359f0b2a0beSSam Leffler 	ATH_TXBUF_LOCK_INIT(sc);
3605591b213SSam Leffler 
3615591b213SSam Leffler 	TASK_INIT(&sc->sc_rxtask, 0, ath_rx_proc, sc);
3625591b213SSam Leffler 	TASK_INIT(&sc->sc_rxorntask, 0, ath_rxorn_proc, sc);
3635591b213SSam Leffler 	TASK_INIT(&sc->sc_fataltask, 0, ath_fatal_proc, sc);
3645591b213SSam Leffler 	TASK_INIT(&sc->sc_bmisstask, 0, ath_bmiss_proc, sc);
365c42a7b7eSSam Leffler 	TASK_INIT(&sc->sc_bstucktask, 0, ath_bstuck_proc, sc);
3665591b213SSam Leffler 
3675591b213SSam Leffler 	/*
368c42a7b7eSSam Leffler 	 * Allocate hardware transmit queues: one queue for
369c42a7b7eSSam Leffler 	 * beacon frames and one data queue for each QoS
370c42a7b7eSSam Leffler 	 * priority.  Note that the hal handles reseting
371c42a7b7eSSam Leffler 	 * these queues at the needed time.
372c42a7b7eSSam Leffler 	 *
373c42a7b7eSSam Leffler 	 * XXX PS-Poll
3745591b213SSam Leffler 	 */
375c42a7b7eSSam Leffler 	sc->sc_bhalq = ath_hal_setuptxqueue(ah, HAL_TX_QUEUE_BEACON, NULL);
3765591b213SSam Leffler 	if (sc->sc_bhalq == (u_int) -1) {
3775591b213SSam Leffler 		if_printf(ifp, "unable to setup a beacon xmit queue!\n");
378c42a7b7eSSam Leffler 		error = EIO;
379b28b4653SSam Leffler 		goto bad2;
3805591b213SSam Leffler 	}
381c42a7b7eSSam Leffler 	sc->sc_cabq = ath_txq_setup(sc, HAL_TX_QUEUE_CAB, 0);
382c42a7b7eSSam Leffler 	if (sc->sc_cabq == NULL) {
383c42a7b7eSSam Leffler 		if_printf(ifp, "unable to setup CAB xmit queue!\n");
384c42a7b7eSSam Leffler 		error = EIO;
385c42a7b7eSSam Leffler 		goto bad2;
386c42a7b7eSSam Leffler 	}
387c42a7b7eSSam Leffler 	/* NB: insure BK queue is the lowest priority h/w queue */
388c42a7b7eSSam Leffler 	if (!ath_tx_setup(sc, WME_AC_BK, HAL_WME_AC_BK)) {
389c42a7b7eSSam Leffler 		if_printf(ifp, "unable to setup xmit queue for %s traffic!\n",
390c42a7b7eSSam Leffler 			ieee80211_wme_acnames[WME_AC_BK]);
391c42a7b7eSSam Leffler 		error = EIO;
392c42a7b7eSSam Leffler 		goto bad2;
393c42a7b7eSSam Leffler 	}
394c42a7b7eSSam Leffler 	if (!ath_tx_setup(sc, WME_AC_BE, HAL_WME_AC_BE) ||
395c42a7b7eSSam Leffler 	    !ath_tx_setup(sc, WME_AC_VI, HAL_WME_AC_VI) ||
396c42a7b7eSSam Leffler 	    !ath_tx_setup(sc, WME_AC_VO, HAL_WME_AC_VO)) {
397c42a7b7eSSam Leffler 		/*
398c42a7b7eSSam Leffler 		 * Not enough hardware tx queues to properly do WME;
399c42a7b7eSSam Leffler 		 * just punt and assign them all to the same h/w queue.
400c42a7b7eSSam Leffler 		 * We could do a better job of this if, for example,
401c42a7b7eSSam Leffler 		 * we allocate queues when we switch from station to
402c42a7b7eSSam Leffler 		 * AP mode.
403c42a7b7eSSam Leffler 		 */
404c42a7b7eSSam Leffler 		if (sc->sc_ac2q[WME_AC_VI] != NULL)
405c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_VI]);
406c42a7b7eSSam Leffler 		if (sc->sc_ac2q[WME_AC_BE] != NULL)
407c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_BE]);
408c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_BE] = sc->sc_ac2q[WME_AC_BK];
409c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_VI] = sc->sc_ac2q[WME_AC_BK];
410c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_VO] = sc->sc_ac2q[WME_AC_BK];
411c42a7b7eSSam Leffler 	}
412c42a7b7eSSam Leffler 
413c42a7b7eSSam Leffler 	/*
414c42a7b7eSSam Leffler 	 * Special case certain configurations.  Note the
415c42a7b7eSSam Leffler 	 * CAB queue is handled by these specially so don't
416c42a7b7eSSam Leffler 	 * include them when checking the txq setup mask.
417c42a7b7eSSam Leffler 	 */
418c42a7b7eSSam Leffler 	switch (sc->sc_txqsetup &~ (1<<sc->sc_cabq->axq_qnum)) {
419c42a7b7eSSam Leffler 	case 0x01:
420c42a7b7eSSam Leffler 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0, sc);
421c42a7b7eSSam Leffler 		break;
422c42a7b7eSSam Leffler 	case 0x0f:
423c42a7b7eSSam Leffler 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0123, sc);
424c42a7b7eSSam Leffler 		break;
425c42a7b7eSSam Leffler 	default:
426c42a7b7eSSam Leffler 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc, sc);
427c42a7b7eSSam Leffler 		break;
428c42a7b7eSSam Leffler 	}
429c42a7b7eSSam Leffler 
430c42a7b7eSSam Leffler 	/*
431c42a7b7eSSam Leffler 	 * Setup rate control.  Some rate control modules
432c42a7b7eSSam Leffler 	 * call back to change the anntena state so expose
433c42a7b7eSSam Leffler 	 * the necessary entry points.
434c42a7b7eSSam Leffler 	 * XXX maybe belongs in struct ath_ratectrl?
435c42a7b7eSSam Leffler 	 */
436c42a7b7eSSam Leffler 	sc->sc_setdefantenna = ath_setdefantenna;
437c42a7b7eSSam Leffler 	sc->sc_rc = ath_rate_attach(sc);
438c42a7b7eSSam Leffler 	if (sc->sc_rc == NULL) {
439c42a7b7eSSam Leffler 		error = EIO;
440c42a7b7eSSam Leffler 		goto bad2;
441c42a7b7eSSam Leffler 	}
442c42a7b7eSSam Leffler 
443c42a7b7eSSam Leffler 	sc->sc_ledstate = 1;
444c42a7b7eSSam Leffler 	/*
445c42a7b7eSSam Leffler 	 * Auto-enable soft led processing for IBM cards and for
446c42a7b7eSSam Leffler 	 * 5211 minipci cards.  Users can also manually enable/disable
447c42a7b7eSSam Leffler 	 * support with a sysctl.
448c42a7b7eSSam Leffler 	 */
449c42a7b7eSSam Leffler 	sc->sc_softled = (devid == AR5212_DEVID_IBM || devid == AR5211_DEVID);
450c42a7b7eSSam Leffler 	if (sc->sc_softled) {
451c42a7b7eSSam Leffler 		ath_hal_gpioCfgOutput(ah, sc->sc_ledpin);
452c42a7b7eSSam Leffler 		ath_hal_gpioset(ah, sc->sc_ledpin, 0);
453c42a7b7eSSam Leffler 	}
4545591b213SSam Leffler 
4555591b213SSam Leffler 	ifp->if_softc = sc;
4565591b213SSam Leffler 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
4575591b213SSam Leffler 	ifp->if_start = ath_start;
4585591b213SSam Leffler 	ifp->if_watchdog = ath_watchdog;
4595591b213SSam Leffler 	ifp->if_ioctl = ath_ioctl;
4605591b213SSam Leffler 	ifp->if_init = ath_init;
461154b8df2SMax Laier 	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
462154b8df2SMax Laier 	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
463154b8df2SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
4645591b213SSam Leffler 
465c42a7b7eSSam Leffler 	ic->ic_ifp = ifp;
466c42a7b7eSSam Leffler 	ic->ic_reset = ath_reset;
4675591b213SSam Leffler 	ic->ic_newassoc = ath_newassoc;
468c42a7b7eSSam Leffler 	ic->ic_updateslot = ath_updateslot;
469c42a7b7eSSam Leffler 	ic->ic_wme.wme_update = ath_wme_update;
4705591b213SSam Leffler 	/* XXX not right but it's not used anywhere important */
4715591b213SSam Leffler 	ic->ic_phytype = IEEE80211_T_OFDM;
4725591b213SSam Leffler 	ic->ic_opmode = IEEE80211_M_STA;
473c42a7b7eSSam Leffler 	ic->ic_caps =
474c42a7b7eSSam Leffler 		  IEEE80211_C_IBSS		/* ibss, nee adhoc, mode */
475fe32c3efSSam Leffler 		| IEEE80211_C_HOSTAP		/* hostap mode */
476fe32c3efSSam Leffler 		| IEEE80211_C_MONITOR		/* monitor mode */
477fe32c3efSSam Leffler 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
478c42a7b7eSSam Leffler 		| IEEE80211_C_SHSLOT		/* short slot time supported */
479c42a7b7eSSam Leffler 		| IEEE80211_C_WPA		/* capable of WPA1+WPA2 */
48001e7e035SSam Leffler 		;
481c42a7b7eSSam Leffler 	/*
482c42a7b7eSSam Leffler 	 * Query the hal to figure out h/w crypto support.
483c42a7b7eSSam Leffler 	 */
484c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_WEP))
485c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_WEP;
486c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_OCB))
487c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_AES;
488c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_CCM))
489c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_AES_CCM;
490c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_CKIP))
491c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_CKIP;
492c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_TKIP)) {
493c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TKIP;
494c42a7b7eSSam Leffler 		/*
495c42a7b7eSSam Leffler 		 * Check if h/w does the MIC and/or whether the
496c42a7b7eSSam Leffler 		 * separate key cache entries are required to
497c42a7b7eSSam Leffler 		 * handle both tx+rx MIC keys.
498c42a7b7eSSam Leffler 		 */
499c42a7b7eSSam Leffler 		if (ath_hal_ciphersupported(ah, HAL_CIPHER_MIC))
500c42a7b7eSSam Leffler 			ic->ic_caps |= IEEE80211_C_TKIPMIC;
501c42a7b7eSSam Leffler 		if (ath_hal_tkipsplit(ah))
502c42a7b7eSSam Leffler 			sc->sc_splitmic = 1;
503c42a7b7eSSam Leffler 	}
504c42a7b7eSSam Leffler 	/*
505c42a7b7eSSam Leffler 	 * TPC support can be done either with a global cap or
506c42a7b7eSSam Leffler 	 * per-packet support.  The latter is not available on
507c42a7b7eSSam Leffler 	 * all parts.  We're a bit pedantic here as all parts
508c42a7b7eSSam Leffler 	 * support a global cap.
509c42a7b7eSSam Leffler 	 */
510c42a7b7eSSam Leffler 	sc->sc_hastpc = ath_hal_hastpc(ah);
511c42a7b7eSSam Leffler 	if (sc->sc_hastpc || ath_hal_hastxpowlimit(ah))
512c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TXPMGT;
513c42a7b7eSSam Leffler 
514c42a7b7eSSam Leffler 	/*
515c42a7b7eSSam Leffler 	 * Mark WME capability only if we have sufficient
516c42a7b7eSSam Leffler 	 * hardware queues to do proper priority scheduling.
517c42a7b7eSSam Leffler 	 */
518c42a7b7eSSam Leffler 	if (sc->sc_ac2q[WME_AC_BE] != sc->sc_ac2q[WME_AC_BK])
519c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_WME;
520c42a7b7eSSam Leffler 	/*
521c42a7b7eSSam Leffler 	 * Check for frame bursting capability.
522c42a7b7eSSam Leffler 	 */
523c42a7b7eSSam Leffler 	if (ath_hal_hasbursting(ah))
524c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_BURST;
525c42a7b7eSSam Leffler 
526c42a7b7eSSam Leffler 	/*
527c42a7b7eSSam Leffler 	 * Indicate we need the 802.11 header padded to a
528c42a7b7eSSam Leffler 	 * 32-bit boundary for 4-address and QoS frames.
529c42a7b7eSSam Leffler 	 */
530c42a7b7eSSam Leffler 	ic->ic_flags |= IEEE80211_F_DATAPAD;
531c42a7b7eSSam Leffler 
532c42a7b7eSSam Leffler 	/*
533c42a7b7eSSam Leffler 	 * Query the hal about antenna support.
534c42a7b7eSSam Leffler 	 */
535c42a7b7eSSam Leffler 	if (ath_hal_hasdiversity(ah)) {
536c42a7b7eSSam Leffler 		sc->sc_hasdiversity = 1;
537c42a7b7eSSam Leffler 		sc->sc_diversity = ath_hal_getdiversity(ah);
538c42a7b7eSSam Leffler 	}
539c42a7b7eSSam Leffler 	sc->sc_defant = ath_hal_getdefantenna(ah);
540c42a7b7eSSam Leffler 
541c42a7b7eSSam Leffler 	/*
542c42a7b7eSSam Leffler 	 * Not all chips have the VEOL support we want to
543c42a7b7eSSam Leffler 	 * use with IBSS beacons; check here for it.
544c42a7b7eSSam Leffler 	 */
545c42a7b7eSSam Leffler 	sc->sc_hasveol = ath_hal_hasveol(ah);
5465591b213SSam Leffler 
5475591b213SSam Leffler 	/* get mac address from hardware */
5485591b213SSam Leffler 	ath_hal_getmac(ah, ic->ic_myaddr);
5495591b213SSam Leffler 
5505591b213SSam Leffler 	/* call MI attach routine. */
551c42a7b7eSSam Leffler 	ieee80211_ifattach(ic);
5525591b213SSam Leffler 	/* override default methods */
5535591b213SSam Leffler 	ic->ic_node_alloc = ath_node_alloc;
5541e774079SSam Leffler 	sc->sc_node_free = ic->ic_node_free;
5555591b213SSam Leffler 	ic->ic_node_free = ath_node_free;
556de5af704SSam Leffler 	ic->ic_node_getrssi = ath_node_getrssi;
557c42a7b7eSSam Leffler 	sc->sc_recv_mgmt = ic->ic_recv_mgmt;
558c42a7b7eSSam Leffler 	ic->ic_recv_mgmt = ath_recv_mgmt;
55945bbf62fSSam Leffler 	sc->sc_newstate = ic->ic_newstate;
56045bbf62fSSam Leffler 	ic->ic_newstate = ath_newstate;
561c42a7b7eSSam Leffler 	ic->ic_crypto.cs_key_alloc = ath_key_alloc;
562c42a7b7eSSam Leffler 	ic->ic_crypto.cs_key_delete = ath_key_delete;
563c42a7b7eSSam Leffler 	ic->ic_crypto.cs_key_set = ath_key_set;
564c42a7b7eSSam Leffler 	ic->ic_crypto.cs_key_update_begin = ath_key_update_begin;
565c42a7b7eSSam Leffler 	ic->ic_crypto.cs_key_update_end = ath_key_update_end;
56645bbf62fSSam Leffler 	/* complete initialization */
567c42a7b7eSSam Leffler 	ieee80211_media_init(ic, ath_media_change, ieee80211_media_status);
5685591b213SSam Leffler 
569c42a7b7eSSam Leffler 	ath_bpfattach(sc);
57073454c73SSam Leffler 
571c42a7b7eSSam Leffler 	if (bootverbose)
572c42a7b7eSSam Leffler 		ieee80211_announce(ic);
573c42a7b7eSSam Leffler 	ath_announce(sc);
5745591b213SSam Leffler 	return 0;
575b28b4653SSam Leffler bad2:
576c42a7b7eSSam Leffler 	ath_tx_cleanup(sc);
577b28b4653SSam Leffler 	ath_desc_free(sc);
5785591b213SSam Leffler bad:
5795591b213SSam Leffler 	if (ah)
5805591b213SSam Leffler 		ath_hal_detach(ah);
5815591b213SSam Leffler 	sc->sc_invalid = 1;
5825591b213SSam Leffler 	return error;
5835591b213SSam Leffler }
5845591b213SSam Leffler 
5855591b213SSam Leffler int
5865591b213SSam Leffler ath_detach(struct ath_softc *sc)
5875591b213SSam Leffler {
588c42a7b7eSSam Leffler 	struct ifnet *ifp = &sc->sc_if;
5895591b213SSam Leffler 
590c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
591c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
5925591b213SSam Leffler 
5935591b213SSam Leffler 	ath_stop(ifp);
59473454c73SSam Leffler 	bpfdetach(ifp);
595c42a7b7eSSam Leffler 	/*
596c42a7b7eSSam Leffler 	 * NB: the order of these is important:
597c42a7b7eSSam Leffler 	 * o call the 802.11 layer before detaching the hal to
598c42a7b7eSSam Leffler 	 *   insure callbacks into the driver to delete global
599c42a7b7eSSam Leffler 	 *   key cache entries can be handled
600c42a7b7eSSam Leffler 	 * o reclaim the tx queue data structures after calling
601c42a7b7eSSam Leffler 	 *   the 802.11 layer as we'll get called back to reclaim
602c42a7b7eSSam Leffler 	 *   node state and potentially want to use them
603c42a7b7eSSam Leffler 	 * o to cleanup the tx queues the hal is called, so detach
604c42a7b7eSSam Leffler 	 *   it last
605c42a7b7eSSam Leffler 	 * Other than that, it's straightforward...
606c42a7b7eSSam Leffler 	 */
607c42a7b7eSSam Leffler 	ieee80211_ifdetach(&sc->sc_ic);
608c42a7b7eSSam Leffler 	ath_rate_detach(sc->sc_rc);
6095591b213SSam Leffler 	ath_desc_free(sc);
610c42a7b7eSSam Leffler 	ath_tx_cleanup(sc);
6115591b213SSam Leffler 	ath_hal_detach(sc->sc_ah);
612f0b2a0beSSam Leffler 
6135591b213SSam Leffler 	return 0;
6145591b213SSam Leffler }
6155591b213SSam Leffler 
6165591b213SSam Leffler void
6175591b213SSam Leffler ath_suspend(struct ath_softc *sc)
6185591b213SSam Leffler {
619c42a7b7eSSam Leffler 	struct ifnet *ifp = &sc->sc_if;
6205591b213SSam Leffler 
621c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
622c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
6235591b213SSam Leffler 
6245591b213SSam Leffler 	ath_stop(ifp);
6255591b213SSam Leffler }
6265591b213SSam Leffler 
6275591b213SSam Leffler void
6285591b213SSam Leffler ath_resume(struct ath_softc *sc)
6295591b213SSam Leffler {
630c42a7b7eSSam Leffler 	struct ifnet *ifp = &sc->sc_if;
6315591b213SSam Leffler 
632c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
633c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
6345591b213SSam Leffler 
6356b59f5e3SSam Leffler 	if (ifp->if_flags & IFF_UP) {
6365591b213SSam Leffler 		ath_init(ifp);
6376b59f5e3SSam Leffler 		if (ifp->if_flags & IFF_RUNNING)
6385591b213SSam Leffler 			ath_start(ifp);
6395591b213SSam Leffler 	}
6406b59f5e3SSam Leffler }
6415591b213SSam Leffler 
6425591b213SSam Leffler void
6435591b213SSam Leffler ath_shutdown(struct ath_softc *sc)
6445591b213SSam Leffler {
645c42a7b7eSSam Leffler 	struct ifnet *ifp = &sc->sc_if;
6465591b213SSam Leffler 
647c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
648c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
6495591b213SSam Leffler 
6505591b213SSam Leffler 	ath_stop(ifp);
6515591b213SSam Leffler }
6525591b213SSam Leffler 
653c42a7b7eSSam Leffler /*
654c42a7b7eSSam Leffler  * Interrupt handler.  Most of the actual processing is deferred.
655c42a7b7eSSam Leffler  */
6565591b213SSam Leffler void
6575591b213SSam Leffler ath_intr(void *arg)
6585591b213SSam Leffler {
6595591b213SSam Leffler 	struct ath_softc *sc = arg;
660c42a7b7eSSam Leffler 	struct ifnet *ifp = &sc->sc_if;
6615591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
6625591b213SSam Leffler 	HAL_INT status;
6635591b213SSam Leffler 
6645591b213SSam Leffler 	if (sc->sc_invalid) {
6655591b213SSam Leffler 		/*
666b58b3803SSam Leffler 		 * The hardware is not ready/present, don't touch anything.
667b58b3803SSam Leffler 		 * Note this can happen early on if the IRQ is shared.
6685591b213SSam Leffler 		 */
669c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid; ignored\n", __func__);
6705591b213SSam Leffler 		return;
6715591b213SSam Leffler 	}
672fdd758d4SSam Leffler 	if (!ath_hal_intrpend(ah))		/* shared irq, not for us */
673fdd758d4SSam Leffler 		return;
6745591b213SSam Leffler 	if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) != (IFF_RUNNING|IFF_UP)) {
675c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
676c42a7b7eSSam Leffler 			__func__, ifp->if_flags);
6775591b213SSam Leffler 		ath_hal_getisr(ah, &status);	/* clear ISR */
6785591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable further intr's */
6795591b213SSam Leffler 		return;
6805591b213SSam Leffler 	}
681c42a7b7eSSam Leffler 	/*
682c42a7b7eSSam Leffler 	 * Figure out the reason(s) for the interrupt.  Note
683c42a7b7eSSam Leffler 	 * that the hal returns a pseudo-ISR that may include
684c42a7b7eSSam Leffler 	 * bits we haven't explicitly enabled so we mask the
685c42a7b7eSSam Leffler 	 * value to insure we only process bits we requested.
686c42a7b7eSSam Leffler 	 */
6875591b213SSam Leffler 	ath_hal_getisr(ah, &status);		/* NB: clears ISR too */
688c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_INTR, "%s: status 0x%x\n", __func__, status);
689ecddff40SSam Leffler 	status &= sc->sc_imask;			/* discard unasked for bits */
6905591b213SSam Leffler 	if (status & HAL_INT_FATAL) {
691c42a7b7eSSam Leffler 		/*
692c42a7b7eSSam Leffler 		 * Fatal errors are unrecoverable.  Typically
693c42a7b7eSSam Leffler 		 * these are caused by DMA errors.  Unfortunately
694c42a7b7eSSam Leffler 		 * the exact reason is not (presently) returned
695c42a7b7eSSam Leffler 		 * by the hal.
696c42a7b7eSSam Leffler 		 */
6975591b213SSam Leffler 		sc->sc_stats.ast_hardware++;
6985591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
6995591b213SSam Leffler 		taskqueue_enqueue(taskqueue_swi, &sc->sc_fataltask);
7005591b213SSam Leffler 	} else if (status & HAL_INT_RXORN) {
7015591b213SSam Leffler 		sc->sc_stats.ast_rxorn++;
7025591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
7035591b213SSam Leffler 		taskqueue_enqueue(taskqueue_swi, &sc->sc_rxorntask);
7045591b213SSam Leffler 	} else {
705c42a7b7eSSam Leffler 		if (status & HAL_INT_SWBA) {
706c42a7b7eSSam Leffler 			/*
707c42a7b7eSSam Leffler 			 * Software beacon alert--time to send a beacon.
708c42a7b7eSSam Leffler 			 * Handle beacon transmission directly; deferring
709c42a7b7eSSam Leffler 			 * this is too slow to meet timing constraints
710c42a7b7eSSam Leffler 			 * under load.
711c42a7b7eSSam Leffler 			 */
712c42a7b7eSSam Leffler 			ath_beacon_proc(sc, 0);
713c42a7b7eSSam Leffler 		}
7145591b213SSam Leffler 		if (status & HAL_INT_RXEOL) {
7155591b213SSam Leffler 			/*
7165591b213SSam Leffler 			 * NB: the hardware should re-read the link when
7175591b213SSam Leffler 			 *     RXE bit is written, but it doesn't work at
7185591b213SSam Leffler 			 *     least on older hardware revs.
7195591b213SSam Leffler 			 */
7205591b213SSam Leffler 			sc->sc_stats.ast_rxeol++;
7215591b213SSam Leffler 			sc->sc_rxlink = NULL;
7225591b213SSam Leffler 		}
7235591b213SSam Leffler 		if (status & HAL_INT_TXURN) {
7245591b213SSam Leffler 			sc->sc_stats.ast_txurn++;
7255591b213SSam Leffler 			/* bump tx trigger level */
7265591b213SSam Leffler 			ath_hal_updatetxtriglevel(ah, AH_TRUE);
7275591b213SSam Leffler 		}
7285591b213SSam Leffler 		if (status & HAL_INT_RX)
7295591b213SSam Leffler 			taskqueue_enqueue(taskqueue_swi, &sc->sc_rxtask);
7305591b213SSam Leffler 		if (status & HAL_INT_TX)
7315591b213SSam Leffler 			taskqueue_enqueue(taskqueue_swi, &sc->sc_txtask);
7325591b213SSam Leffler 		if (status & HAL_INT_BMISS) {
7335591b213SSam Leffler 			sc->sc_stats.ast_bmiss++;
7345591b213SSam Leffler 			taskqueue_enqueue(taskqueue_swi, &sc->sc_bmisstask);
7355591b213SSam Leffler 		}
736c42a7b7eSSam Leffler 		if (status & HAL_INT_MIB) {
737c42a7b7eSSam Leffler 			sc->sc_stats.ast_mib++;
738c42a7b7eSSam Leffler 			/*
739c42a7b7eSSam Leffler 			 * Disable interrupts until we service the MIB
740c42a7b7eSSam Leffler 			 * interrupt; otherwise it will continue to fire.
741c42a7b7eSSam Leffler 			 */
742c42a7b7eSSam Leffler 			ath_hal_intrset(ah, 0);
743c42a7b7eSSam Leffler 			/*
744c42a7b7eSSam Leffler 			 * Let the hal handle the event.  We assume it will
745c42a7b7eSSam Leffler 			 * clear whatever condition caused the interrupt.
746c42a7b7eSSam Leffler 			 */
747c42a7b7eSSam Leffler 			ath_hal_mibevent(ah,
748c42a7b7eSSam Leffler 				&ATH_NODE(sc->sc_ic.ic_bss)->an_halstats);
749c42a7b7eSSam Leffler 			ath_hal_intrset(ah, sc->sc_imask);
750c42a7b7eSSam Leffler 		}
7515591b213SSam Leffler 	}
7525591b213SSam Leffler }
7535591b213SSam Leffler 
7545591b213SSam Leffler static void
7555591b213SSam Leffler ath_fatal_proc(void *arg, int pending)
7565591b213SSam Leffler {
7575591b213SSam Leffler 	struct ath_softc *sc = arg;
758c42a7b7eSSam Leffler 	struct ifnet *ifp = &sc->sc_if;
7595591b213SSam Leffler 
760c42a7b7eSSam Leffler 	if_printf(ifp, "hardware error; resetting\n");
761c42a7b7eSSam Leffler 	ath_reset(ifp);
7625591b213SSam Leffler }
7635591b213SSam Leffler 
7645591b213SSam Leffler static void
7655591b213SSam Leffler ath_rxorn_proc(void *arg, int pending)
7665591b213SSam Leffler {
7675591b213SSam Leffler 	struct ath_softc *sc = arg;
768c42a7b7eSSam Leffler 	struct ifnet *ifp = &sc->sc_if;
7695591b213SSam Leffler 
770c42a7b7eSSam Leffler 	if_printf(ifp, "rx FIFO overrun; resetting\n");
771c42a7b7eSSam Leffler 	ath_reset(ifp);
7725591b213SSam Leffler }
7735591b213SSam Leffler 
7745591b213SSam Leffler static void
7755591b213SSam Leffler ath_bmiss_proc(void *arg, int pending)
7765591b213SSam Leffler {
7775591b213SSam Leffler 	struct ath_softc *sc = arg;
7785591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
7795591b213SSam Leffler 
780c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: pending %u\n", __func__, pending);
7815591b213SSam Leffler 	KASSERT(ic->ic_opmode == IEEE80211_M_STA,
7825591b213SSam Leffler 		("unexpect operating mode %u", ic->ic_opmode));
783e585d188SSam Leffler 	if (ic->ic_state == IEEE80211_S_RUN) {
784e585d188SSam Leffler 		/*
785e585d188SSam Leffler 		 * Rather than go directly to scan state, try to
786e585d188SSam Leffler 		 * reassociate first.  If that fails then the state
787e585d188SSam Leffler 		 * machine will drop us into scanning after timing
788e585d188SSam Leffler 		 * out waiting for a probe response.
789e585d188SSam Leffler 		 */
790b5f4adb3SSam Leffler 		NET_LOCK_GIANT();
791e585d188SSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1);
792b5f4adb3SSam Leffler 		NET_UNLOCK_GIANT();
793e585d188SSam Leffler 	}
7945591b213SSam Leffler }
7955591b213SSam Leffler 
7965591b213SSam Leffler static u_int
7975591b213SSam Leffler ath_chan2flags(struct ieee80211com *ic, struct ieee80211_channel *chan)
7985591b213SSam Leffler {
799c42a7b7eSSam Leffler #define	N(a)	(sizeof(a) / sizeof(a[0]))
8005591b213SSam Leffler 	static const u_int modeflags[] = {
8015591b213SSam Leffler 		0,			/* IEEE80211_MODE_AUTO */
8025591b213SSam Leffler 		CHANNEL_A,		/* IEEE80211_MODE_11A */
8035591b213SSam Leffler 		CHANNEL_B,		/* IEEE80211_MODE_11B */
8045591b213SSam Leffler 		CHANNEL_PUREG,		/* IEEE80211_MODE_11G */
805c42a7b7eSSam Leffler 		0,			/* IEEE80211_MODE_FH */
806c42a7b7eSSam Leffler 		CHANNEL_T,		/* IEEE80211_MODE_TURBO_A */
807c42a7b7eSSam Leffler 		CHANNEL_108G		/* IEEE80211_MODE_TURBO_G */
8085591b213SSam Leffler 	};
809c42a7b7eSSam Leffler 	enum ieee80211_phymode mode = ieee80211_chan2mode(ic, chan);
810c42a7b7eSSam Leffler 
811c42a7b7eSSam Leffler 	KASSERT(mode < N(modeflags), ("unexpected phy mode %u", mode));
812c42a7b7eSSam Leffler 	KASSERT(modeflags[mode] != 0, ("mode %u undefined", mode));
813c42a7b7eSSam Leffler 	return modeflags[mode];
814c42a7b7eSSam Leffler #undef N
8155591b213SSam Leffler }
8165591b213SSam Leffler 
8175591b213SSam Leffler static void
8185591b213SSam Leffler ath_init(void *arg)
8195591b213SSam Leffler {
8205591b213SSam Leffler 	struct ath_softc *sc = (struct ath_softc *) arg;
8215591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
822c42a7b7eSSam Leffler 	struct ifnet *ifp = &sc->sc_if;
8235591b213SSam Leffler 	struct ieee80211_node *ni;
8245591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
8255591b213SSam Leffler 	HAL_STATUS status;
8265591b213SSam Leffler 
827c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
828c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
8295591b213SSam Leffler 
830f0b2a0beSSam Leffler 	ATH_LOCK(sc);
8315591b213SSam Leffler 	/*
8325591b213SSam Leffler 	 * Stop anything previously setup.  This is safe
8335591b213SSam Leffler 	 * whether this is the first time through or not.
8345591b213SSam Leffler 	 */
835c42a7b7eSSam Leffler 	ath_stop_locked(ifp);
8365591b213SSam Leffler 
8375591b213SSam Leffler 	/*
8385591b213SSam Leffler 	 * The basic interface to setting the hardware in a good
8395591b213SSam Leffler 	 * state is ``reset''.  On return the hardware is known to
8405591b213SSam Leffler 	 * be powered up and with interrupts disabled.  This must
8415591b213SSam Leffler 	 * be followed by initialization of the appropriate bits
8425591b213SSam Leffler 	 * and then setup of the interrupt mask.
8435591b213SSam Leffler 	 */
844c42a7b7eSSam Leffler 	sc->sc_curchan.channel = ic->ic_ibss_chan->ic_freq;
845c42a7b7eSSam Leffler 	sc->sc_curchan.channelFlags = ath_chan2flags(ic, ic->ic_ibss_chan);
846c42a7b7eSSam Leffler 	if (!ath_hal_reset(ah, ic->ic_opmode, &sc->sc_curchan, AH_FALSE, &status)) {
8475591b213SSam Leffler 		if_printf(ifp, "unable to reset hardware; hal status %u\n",
8485591b213SSam Leffler 			status);
8495591b213SSam Leffler 		goto done;
8505591b213SSam Leffler 	}
8515591b213SSam Leffler 
8525591b213SSam Leffler 	/*
853c42a7b7eSSam Leffler 	 * This is needed only to setup initial state
854c42a7b7eSSam Leffler 	 * but it's best done after a reset.
855c42a7b7eSSam Leffler 	 */
856c42a7b7eSSam Leffler 	ath_update_txpow(sc);
857c42a7b7eSSam Leffler 
858c42a7b7eSSam Leffler 	/*
8595591b213SSam Leffler 	 * Setup the hardware after reset: the key cache
8605591b213SSam Leffler 	 * is filled as needed and the receive engine is
8615591b213SSam Leffler 	 * set going.  Frame transmit is handled entirely
8625591b213SSam Leffler 	 * in the frame output path; there's nothing to do
8635591b213SSam Leffler 	 * here except setup the interrupt mask.
8645591b213SSam Leffler 	 */
865c42a7b7eSSam Leffler 	ath_initkeytable(sc);		/* XXX still needed? */
8665591b213SSam Leffler 	if (ath_startrecv(sc) != 0) {
8675591b213SSam Leffler 		if_printf(ifp, "unable to start recv logic\n");
8685591b213SSam Leffler 		goto done;
8695591b213SSam Leffler 	}
8705591b213SSam Leffler 
8715591b213SSam Leffler 	/*
8725591b213SSam Leffler 	 * Enable interrupts.
8735591b213SSam Leffler 	 */
8745591b213SSam Leffler 	sc->sc_imask = HAL_INT_RX | HAL_INT_TX
8755591b213SSam Leffler 		  | HAL_INT_RXEOL | HAL_INT_RXORN
8765591b213SSam Leffler 		  | HAL_INT_FATAL | HAL_INT_GLOBAL;
877c42a7b7eSSam Leffler 	/*
878c42a7b7eSSam Leffler 	 * Enable MIB interrupts when there are hardware phy counters.
879c42a7b7eSSam Leffler 	 * Note we only do this (at the moment) for station mode.
880c42a7b7eSSam Leffler 	 */
881c42a7b7eSSam Leffler 	if (sc->sc_needmib && ic->ic_opmode == IEEE80211_M_STA)
882c42a7b7eSSam Leffler 		sc->sc_imask |= HAL_INT_MIB;
8835591b213SSam Leffler 	ath_hal_intrset(ah, sc->sc_imask);
8845591b213SSam Leffler 
8855591b213SSam Leffler 	ifp->if_flags |= IFF_RUNNING;
8865591b213SSam Leffler 	ic->ic_state = IEEE80211_S_INIT;
8875591b213SSam Leffler 
8885591b213SSam Leffler 	/*
8895591b213SSam Leffler 	 * The hardware should be ready to go now so it's safe
8905591b213SSam Leffler 	 * to kick the 802.11 state machine as it's likely to
8915591b213SSam Leffler 	 * immediately call back to us to send mgmt frames.
8925591b213SSam Leffler 	 */
8935591b213SSam Leffler 	ni = ic->ic_bss;
8945591b213SSam Leffler 	ni->ni_chan = ic->ic_ibss_chan;
89516b4851aSSam Leffler 	ath_chan_change(sc, ni->ni_chan);
896c42a7b7eSSam Leffler 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
897c42a7b7eSSam Leffler 		if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
89845bbf62fSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
899c42a7b7eSSam Leffler 	} else
9006b59f5e3SSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
9015591b213SSam Leffler done:
902f0b2a0beSSam Leffler 	ATH_UNLOCK(sc);
9035591b213SSam Leffler }
9045591b213SSam Leffler 
9055591b213SSam Leffler static void
906c42a7b7eSSam Leffler ath_stop_locked(struct ifnet *ifp)
9075591b213SSam Leffler {
9085591b213SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
909c42a7b7eSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
9105591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
9115591b213SSam Leffler 
912c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid %u if_flags 0x%x\n",
913c42a7b7eSSam Leffler 		__func__, sc->sc_invalid, ifp->if_flags);
9145591b213SSam Leffler 
915c42a7b7eSSam Leffler 	ATH_LOCK_ASSERT(sc);
9165591b213SSam Leffler 	if (ifp->if_flags & IFF_RUNNING) {
9175591b213SSam Leffler 		/*
9185591b213SSam Leffler 		 * Shutdown the hardware and driver:
919c42a7b7eSSam Leffler 		 *    reset 802.11 state machine
9205591b213SSam Leffler 		 *    turn off timers
921c42a7b7eSSam Leffler 		 *    disable interrupts
922c42a7b7eSSam Leffler 		 *    turn off the radio
9235591b213SSam Leffler 		 *    clear transmit machinery
9245591b213SSam Leffler 		 *    clear receive machinery
9255591b213SSam Leffler 		 *    drain and release tx queues
9265591b213SSam Leffler 		 *    reclaim beacon resources
9275591b213SSam Leffler 		 *    power down hardware
9285591b213SSam Leffler 		 *
9295591b213SSam Leffler 		 * Note that some of this work is not possible if the
9305591b213SSam Leffler 		 * hardware is gone (invalid).
9315591b213SSam Leffler 		 */
932c42a7b7eSSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
9335591b213SSam Leffler 		ifp->if_flags &= ~IFF_RUNNING;
9345591b213SSam Leffler 		ifp->if_timer = 0;
935c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
936c42a7b7eSSam Leffler 			if (sc->sc_softled)
937c42a7b7eSSam Leffler 				ath_hal_gpioset(ah, sc->sc_ledpin, 1);
9385591b213SSam Leffler 			ath_hal_intrset(ah, 0);
939c42a7b7eSSam Leffler 		}
9405591b213SSam Leffler 		ath_draintxq(sc);
941c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
9425591b213SSam Leffler 			ath_stoprecv(sc);
943c42a7b7eSSam Leffler 			ath_hal_phydisable(ah);
944c42a7b7eSSam Leffler 		} else
9455591b213SSam Leffler 			sc->sc_rxlink = NULL;
946154b8df2SMax Laier 		IFQ_DRV_PURGE(&ifp->if_snd);
9475591b213SSam Leffler 		ath_beacon_free(sc);
948c42a7b7eSSam Leffler 	}
949c42a7b7eSSam Leffler }
950c42a7b7eSSam Leffler 
951c42a7b7eSSam Leffler static void
952c42a7b7eSSam Leffler ath_stop(struct ifnet *ifp)
953c42a7b7eSSam Leffler {
954c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
955c42a7b7eSSam Leffler 
956c42a7b7eSSam Leffler 	ATH_LOCK(sc);
957c42a7b7eSSam Leffler 	ath_stop_locked(ifp);
958c42a7b7eSSam Leffler 	if (!sc->sc_invalid) {
959c42a7b7eSSam Leffler 		/*
960c42a7b7eSSam Leffler 		 * Set the chip in full sleep mode.  Note that we are
961c42a7b7eSSam Leffler 		 * careful to do this only when bringing the interface
962c42a7b7eSSam Leffler 		 * completely to a stop.  When the chip is in this state
963c42a7b7eSSam Leffler 		 * it must be carefully woken up or references to
964c42a7b7eSSam Leffler 		 * registers in the PCI clock domain may freeze the bus
965c42a7b7eSSam Leffler 		 * (and system).  This varies by chip and is mostly an
966c42a7b7eSSam Leffler 		 * issue with newer parts that go to sleep more quickly.
967c42a7b7eSSam Leffler 		 */
968c42a7b7eSSam Leffler 		ath_hal_setpower(sc->sc_ah, HAL_PM_FULL_SLEEP, 0);
9695591b213SSam Leffler 	}
970f0b2a0beSSam Leffler 	ATH_UNLOCK(sc);
9715591b213SSam Leffler }
9725591b213SSam Leffler 
9735591b213SSam Leffler /*
9745591b213SSam Leffler  * Reset the hardware w/o losing operational state.  This is
9755591b213SSam Leffler  * basically a more efficient way of doing ath_stop, ath_init,
9765591b213SSam Leffler  * followed by state transitions to the current 802.11
977c42a7b7eSSam Leffler  * operational state.  Used to recover from various errors and
978c42a7b7eSSam Leffler  * to reset or reload hardware state.
9795591b213SSam Leffler  */
980c42a7b7eSSam Leffler static int
981c42a7b7eSSam Leffler ath_reset(struct ifnet *ifp)
9825591b213SSam Leffler {
983c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
9845591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
9855591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
9865591b213SSam Leffler 	struct ieee80211_channel *c;
9875591b213SSam Leffler 	HAL_STATUS status;
9885591b213SSam Leffler 
9895591b213SSam Leffler 	/*
9905591b213SSam Leffler 	 * Convert to a HAL channel description with the flags
9915591b213SSam Leffler 	 * constrained to reflect the current operating mode.
9925591b213SSam Leffler 	 */
9935591b213SSam Leffler 	c = ic->ic_ibss_chan;
994c42a7b7eSSam Leffler 	sc->sc_curchan.channel = c->ic_freq;
995c42a7b7eSSam Leffler 	sc->sc_curchan.channelFlags = ath_chan2flags(ic, c);
9965591b213SSam Leffler 
9975591b213SSam Leffler 	ath_hal_intrset(ah, 0);		/* disable interrupts */
9985591b213SSam Leffler 	ath_draintxq(sc);		/* stop xmit side */
9995591b213SSam Leffler 	ath_stoprecv(sc);		/* stop recv side */
10005591b213SSam Leffler 	/* NB: indicate channel change so we do a full reset */
1001c42a7b7eSSam Leffler 	if (!ath_hal_reset(ah, ic->ic_opmode, &sc->sc_curchan, AH_TRUE, &status))
10025591b213SSam Leffler 		if_printf(ifp, "%s: unable to reset hardware; hal status %u\n",
10035591b213SSam Leffler 			__func__, status);
1004c42a7b7eSSam Leffler 	ath_update_txpow(sc);		/* update tx power state */
10055591b213SSam Leffler 	if (ath_startrecv(sc) != 0)	/* restart recv */
10065591b213SSam Leffler 		if_printf(ifp, "%s: unable to start recv logic\n", __func__);
1007c42a7b7eSSam Leffler 	/*
1008c42a7b7eSSam Leffler 	 * We may be doing a reset in response to an ioctl
1009c42a7b7eSSam Leffler 	 * that changes the channel so update any state that
1010c42a7b7eSSam Leffler 	 * might change as a result.
1011c42a7b7eSSam Leffler 	 */
1012c42a7b7eSSam Leffler 	ath_chan_change(sc, c);
10135591b213SSam Leffler 	if (ic->ic_state == IEEE80211_S_RUN)
10145591b213SSam Leffler 		ath_beacon_config(sc);	/* restart beacons */
1015c42a7b7eSSam Leffler 	ath_hal_intrset(ah, sc->sc_imask);
1016c42a7b7eSSam Leffler 
1017c42a7b7eSSam Leffler 	ath_start(ifp);			/* restart xmit */
1018c42a7b7eSSam Leffler 	return 0;
10195591b213SSam Leffler }
10205591b213SSam Leffler 
10215591b213SSam Leffler static void
10225591b213SSam Leffler ath_start(struct ifnet *ifp)
10235591b213SSam Leffler {
10245591b213SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
10255591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
10265591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
10275591b213SSam Leffler 	struct ieee80211_node *ni;
10285591b213SSam Leffler 	struct ath_buf *bf;
10295591b213SSam Leffler 	struct mbuf *m;
10305591b213SSam Leffler 	struct ieee80211_frame *wh;
1031c42a7b7eSSam Leffler 	struct ether_header *eh;
10325591b213SSam Leffler 
10335591b213SSam Leffler 	if ((ifp->if_flags & IFF_RUNNING) == 0 || sc->sc_invalid)
10345591b213SSam Leffler 		return;
10355591b213SSam Leffler 	for (;;) {
10365591b213SSam Leffler 		/*
10375591b213SSam Leffler 		 * Grab a TX buffer and associated resources.
10385591b213SSam Leffler 		 */
1039f0b2a0beSSam Leffler 		ATH_TXBUF_LOCK(sc);
1040c42a7b7eSSam Leffler 		bf = STAILQ_FIRST(&sc->sc_txbuf);
10415591b213SSam Leffler 		if (bf != NULL)
1042c42a7b7eSSam Leffler 			STAILQ_REMOVE_HEAD(&sc->sc_txbuf, bf_list);
1043f0b2a0beSSam Leffler 		ATH_TXBUF_UNLOCK(sc);
10445591b213SSam Leffler 		if (bf == NULL) {
1045c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY, "%s: out of xmit buffers\n",
1046c42a7b7eSSam Leffler 				__func__);
10475591b213SSam Leffler 			sc->sc_stats.ast_tx_qstop++;
10485591b213SSam Leffler 			ifp->if_flags |= IFF_OACTIVE;
10495591b213SSam Leffler 			break;
10505591b213SSam Leffler 		}
10515591b213SSam Leffler 		/*
10525591b213SSam Leffler 		 * Poll the management queue for frames; they
10535591b213SSam Leffler 		 * have priority over normal data frames.
10545591b213SSam Leffler 		 */
10555591b213SSam Leffler 		IF_DEQUEUE(&ic->ic_mgtq, m);
10565591b213SSam Leffler 		if (m == NULL) {
10575591b213SSam Leffler 			/*
10585591b213SSam Leffler 			 * No data frames go out unless we're associated.
10595591b213SSam Leffler 			 */
10605591b213SSam Leffler 			if (ic->ic_state != IEEE80211_S_RUN) {
1061c42a7b7eSSam Leffler 				DPRINTF(sc, ATH_DEBUG_ANY,
1062c42a7b7eSSam Leffler 					"%s: ignore data packet, state %u\n",
1063c42a7b7eSSam Leffler 					__func__, ic->ic_state);
10645591b213SSam Leffler 				sc->sc_stats.ast_tx_discard++;
1065f0b2a0beSSam Leffler 				ATH_TXBUF_LOCK(sc);
1066c42a7b7eSSam Leffler 				STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
1067f0b2a0beSSam Leffler 				ATH_TXBUF_UNLOCK(sc);
10685591b213SSam Leffler 				break;
10695591b213SSam Leffler 			}
1070154b8df2SMax Laier 			IFQ_DRV_DEQUEUE(&ifp->if_snd, m);	/* XXX: LOCK */
10715591b213SSam Leffler 			if (m == NULL) {
1072f0b2a0beSSam Leffler 				ATH_TXBUF_LOCK(sc);
1073c42a7b7eSSam Leffler 				STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
1074f0b2a0beSSam Leffler 				ATH_TXBUF_UNLOCK(sc);
10755591b213SSam Leffler 				break;
10765591b213SSam Leffler 			}
1077c42a7b7eSSam Leffler 			/*
1078c42a7b7eSSam Leffler 			 * Find the node for the destination so we can do
1079c42a7b7eSSam Leffler 			 * things like power save and fast frames aggregation.
1080c42a7b7eSSam Leffler 			 */
1081c42a7b7eSSam Leffler 			if (m->m_len < sizeof(struct ether_header) &&
1082c42a7b7eSSam Leffler 			   (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
1083c42a7b7eSSam Leffler 				ic->ic_stats.is_tx_nobuf++;	/* XXX */
1084c42a7b7eSSam Leffler 				ni = NULL;
1085c42a7b7eSSam Leffler 				goto bad;
1086c42a7b7eSSam Leffler 			}
1087c42a7b7eSSam Leffler 			eh = mtod(m, struct ether_header *);
1088c42a7b7eSSam Leffler 			ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1089c42a7b7eSSam Leffler 			if (ni == NULL) {
1090c42a7b7eSSam Leffler 				/* NB: ieee80211_find_txnode does stat+msg */
1091c42a7b7eSSam Leffler 				goto bad;
1092c42a7b7eSSam Leffler 			}
1093c42a7b7eSSam Leffler 			if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
1094c42a7b7eSSam Leffler 			    (m->m_flags & M_PWR_SAV) == 0) {
1095c42a7b7eSSam Leffler 				/*
1096c42a7b7eSSam Leffler 				 * Station in power save mode; pass the frame
1097c42a7b7eSSam Leffler 				 * to the 802.11 layer and continue.  We'll get
1098c42a7b7eSSam Leffler 				 * the frame back when the time is right.
1099c42a7b7eSSam Leffler 				 */
1100c42a7b7eSSam Leffler 				ieee80211_pwrsave(ic, ni, m);
1101c42a7b7eSSam Leffler 				goto reclaim;
1102c42a7b7eSSam Leffler 			}
1103c42a7b7eSSam Leffler 			/* calculate priority so we can find the tx queue */
1104c42a7b7eSSam Leffler 			if (ieee80211_classify(ic, m, ni)) {
1105c42a7b7eSSam Leffler 				DPRINTF(sc, ATH_DEBUG_XMIT,
1106c42a7b7eSSam Leffler 					"%s: discard, classification failure\n",
1107c42a7b7eSSam Leffler 					__func__);
1108c42a7b7eSSam Leffler 				goto bad;
1109c42a7b7eSSam Leffler 			}
11105591b213SSam Leffler 			ifp->if_opackets++;
11115591b213SSam Leffler 			BPF_MTAP(ifp, m);
11125591b213SSam Leffler 			/*
11135591b213SSam Leffler 			 * Encapsulate the packet in prep for transmission.
11145591b213SSam Leffler 			 */
1115c42a7b7eSSam Leffler 			m = ieee80211_encap(ic, m, ni);
11165591b213SSam Leffler 			if (m == NULL) {
1117c42a7b7eSSam Leffler 				DPRINTF(sc, ATH_DEBUG_ANY,
1118c42a7b7eSSam Leffler 					"%s: encapsulation failure\n",
1119c42a7b7eSSam Leffler 					__func__);
11205591b213SSam Leffler 				sc->sc_stats.ast_tx_encap++;
11215591b213SSam Leffler 				goto bad;
11225591b213SSam Leffler 			}
11235591b213SSam Leffler 		} else {
11240a915fadSSam Leffler 			/*
11250a915fadSSam Leffler 			 * Hack!  The referenced node pointer is in the
11260a915fadSSam Leffler 			 * rcvif field of the packet header.  This is
11270a915fadSSam Leffler 			 * placed there by ieee80211_mgmt_output because
11280a915fadSSam Leffler 			 * we need to hold the reference with the frame
11290a915fadSSam Leffler 			 * and there's no other way (other than packet
11300a915fadSSam Leffler 			 * tags which we consider too expensive to use)
11310a915fadSSam Leffler 			 * to pass it along.
11320a915fadSSam Leffler 			 */
11330a915fadSSam Leffler 			ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
11340a915fadSSam Leffler 			m->m_pkthdr.rcvif = NULL;
11350a915fadSSam Leffler 
11365591b213SSam Leffler 			wh = mtod(m, struct ieee80211_frame *);
11375591b213SSam Leffler 			if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
11385591b213SSam Leffler 			    IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
11395591b213SSam Leffler 				/* fill time stamp */
11405591b213SSam Leffler 				u_int64_t tsf;
11415591b213SSam Leffler 				u_int32_t *tstamp;
11425591b213SSam Leffler 
11435591b213SSam Leffler 				tsf = ath_hal_gettsf64(ah);
11445591b213SSam Leffler 				/* XXX: adjust 100us delay to xmit */
11455591b213SSam Leffler 				tsf += 100;
11465591b213SSam Leffler 				tstamp = (u_int32_t *)&wh[1];
11475591b213SSam Leffler 				tstamp[0] = htole32(tsf & 0xffffffff);
11485591b213SSam Leffler 				tstamp[1] = htole32(tsf >> 32);
11495591b213SSam Leffler 			}
11505591b213SSam Leffler 			sc->sc_stats.ast_tx_mgmt++;
11515591b213SSam Leffler 		}
115273454c73SSam Leffler 
11535591b213SSam Leffler 		if (ath_tx_start(sc, ni, bf, m)) {
11545591b213SSam Leffler 	bad:
11555591b213SSam Leffler 			ifp->if_oerrors++;
1156c42a7b7eSSam Leffler 	reclaim:
1157c42a7b7eSSam Leffler 			ATH_TXBUF_LOCK(sc);
1158c42a7b7eSSam Leffler 			STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
1159c42a7b7eSSam Leffler 			ATH_TXBUF_UNLOCK(sc);
1160c42a7b7eSSam Leffler 			if (ni != NULL)
1161c42a7b7eSSam Leffler 				ieee80211_free_node(ni);
11625591b213SSam Leffler 			continue;
11635591b213SSam Leffler 		}
11645591b213SSam Leffler 
11655591b213SSam Leffler 		sc->sc_tx_timer = 5;
11665591b213SSam Leffler 		ifp->if_timer = 1;
11675591b213SSam Leffler 	}
11685591b213SSam Leffler }
11695591b213SSam Leffler 
11705591b213SSam Leffler static int
11715591b213SSam Leffler ath_media_change(struct ifnet *ifp)
11725591b213SSam Leffler {
1173c42a7b7eSSam Leffler #define	IS_UP(ifp) \
1174c42a7b7eSSam Leffler 	((ifp->if_flags & (IFF_RUNNING|IFF_UP)) == (IFF_RUNNING|IFF_UP))
11755591b213SSam Leffler 	int error;
11765591b213SSam Leffler 
11775591b213SSam Leffler 	error = ieee80211_media_change(ifp);
11785591b213SSam Leffler 	if (error == ENETRESET) {
1179c42a7b7eSSam Leffler 		if (IS_UP(ifp))
11805591b213SSam Leffler 			ath_init(ifp);		/* XXX lose error */
11815591b213SSam Leffler 		error = 0;
11825591b213SSam Leffler 	}
11835591b213SSam Leffler 	return error;
1184c42a7b7eSSam Leffler #undef IS_UP
11855591b213SSam Leffler }
11865591b213SSam Leffler 
11875591b213SSam Leffler #ifdef AR_DEBUG
1188c42a7b7eSSam Leffler static void
1189c42a7b7eSSam Leffler ath_keyprint(const char *tag, u_int ix,
1190c42a7b7eSSam Leffler 	const HAL_KEYVAL *hk, const u_int8_t mac[IEEE80211_ADDR_LEN])
11915591b213SSam Leffler {
1192c42a7b7eSSam Leffler 	static const char *ciphers[] = {
1193c42a7b7eSSam Leffler 		"WEP",
1194c42a7b7eSSam Leffler 		"AES-OCB",
1195c42a7b7eSSam Leffler 		"AES-CCM",
1196c42a7b7eSSam Leffler 		"CKIP",
1197c42a7b7eSSam Leffler 		"TKIP",
1198c42a7b7eSSam Leffler 		"CLR",
1199c42a7b7eSSam Leffler 	};
1200c42a7b7eSSam Leffler 	int i, n;
12015591b213SSam Leffler 
1202c42a7b7eSSam Leffler 	printf("%s: [%02u] %-7s ", tag, ix, ciphers[hk->kv_type]);
1203c42a7b7eSSam Leffler 	for (i = 0, n = hk->kv_len; i < n; i++)
1204c42a7b7eSSam Leffler 		printf("%02x", hk->kv_val[i]);
1205c42a7b7eSSam Leffler 	printf(" mac %s", ether_sprintf(mac));
1206c42a7b7eSSam Leffler 	if (hk->kv_type == HAL_CIPHER_TKIP) {
1207c42a7b7eSSam Leffler 		printf(" mic ");
1208c42a7b7eSSam Leffler 		for (i = 0; i < sizeof(hk->kv_mic); i++)
1209c42a7b7eSSam Leffler 			printf("%02x", hk->kv_mic[i]);
12102075afbaSSam Leffler 	}
1211c42a7b7eSSam Leffler 	printf("\n");
1212c42a7b7eSSam Leffler }
1213c42a7b7eSSam Leffler #endif
1214c42a7b7eSSam Leffler 
12155591b213SSam Leffler /*
1216c42a7b7eSSam Leffler  * Set a TKIP key into the hardware.  This handles the
1217c42a7b7eSSam Leffler  * potential distribution of key state to multiple key
1218c42a7b7eSSam Leffler  * cache slots for TKIP.
12195591b213SSam Leffler  */
1220c42a7b7eSSam Leffler static int
1221c42a7b7eSSam Leffler ath_keyset_tkip(struct ath_softc *sc, const struct ieee80211_key *k,
1222c42a7b7eSSam Leffler 	HAL_KEYVAL *hk, const u_int8_t mac[IEEE80211_ADDR_LEN])
1223c42a7b7eSSam Leffler {
1224c42a7b7eSSam Leffler #define	IEEE80211_KEY_XR	(IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV)
1225c42a7b7eSSam Leffler 	static const u_int8_t zerobssid[IEEE80211_ADDR_LEN];
12268cec0ab9SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
12278cec0ab9SSam Leffler 
1228c42a7b7eSSam Leffler 	KASSERT(k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP,
1229c42a7b7eSSam Leffler 		("got a non-TKIP key, cipher %u", k->wk_cipher->ic_cipher));
1230c42a7b7eSSam Leffler 	KASSERT(sc->sc_splitmic, ("key cache !split"));
1231c42a7b7eSSam Leffler 	if ((k->wk_flags & IEEE80211_KEY_XR) == IEEE80211_KEY_XR) {
1232c42a7b7eSSam Leffler 		/*
1233c42a7b7eSSam Leffler 		 * TX key goes at first index, RX key at +32.
1234c42a7b7eSSam Leffler 		 * The hal handles the MIC keys at index+64.
1235c42a7b7eSSam Leffler 		 */
1236c42a7b7eSSam Leffler 		memcpy(hk->kv_mic, k->wk_txmic, sizeof(hk->kv_mic));
1237c42a7b7eSSam Leffler 		KEYPRINTF(sc, k->wk_keyix, hk, zerobssid);
1238c42a7b7eSSam Leffler 		if (!ath_hal_keyset(ah, k->wk_keyix, hk, zerobssid))
1239c42a7b7eSSam Leffler 			return 0;
1240c42a7b7eSSam Leffler 
1241c42a7b7eSSam Leffler 		memcpy(hk->kv_mic, k->wk_rxmic, sizeof(hk->kv_mic));
1242c42a7b7eSSam Leffler 		KEYPRINTF(sc, k->wk_keyix+32, hk, mac);
1243c42a7b7eSSam Leffler 		/* XXX delete tx key on failure? */
1244c42a7b7eSSam Leffler 		return ath_hal_keyset(ah, k->wk_keyix+32, hk, mac);
1245c42a7b7eSSam Leffler 	} else if (k->wk_flags & IEEE80211_KEY_XR) {
1246c42a7b7eSSam Leffler 		/*
1247c42a7b7eSSam Leffler 		 * TX/RX key goes at first index.
1248c42a7b7eSSam Leffler 		 * The hal handles the MIC keys are index+64.
1249c42a7b7eSSam Leffler 		 */
1250c42a7b7eSSam Leffler 		KASSERT(k->wk_keyix < IEEE80211_WEP_NKID,
1251c42a7b7eSSam Leffler 			("group key at index %u", k->wk_keyix));
1252c42a7b7eSSam Leffler 		memcpy(hk->kv_mic, k->wk_flags & IEEE80211_KEY_XMIT ?
1253c42a7b7eSSam Leffler 			k->wk_txmic : k->wk_rxmic, sizeof(hk->kv_mic));
1254c42a7b7eSSam Leffler 		KEYPRINTF(sc, k->wk_keyix, hk, zerobssid);
1255c42a7b7eSSam Leffler 		return ath_hal_keyset(ah, k->wk_keyix, hk, zerobssid);
1256c42a7b7eSSam Leffler 	}
1257c42a7b7eSSam Leffler 	/* XXX key w/o xmit/recv; need this for compression? */
1258c42a7b7eSSam Leffler 	return 0;
1259c42a7b7eSSam Leffler #undef IEEE80211_KEY_XR
1260c42a7b7eSSam Leffler }
1261c42a7b7eSSam Leffler 
1262c42a7b7eSSam Leffler /*
1263c42a7b7eSSam Leffler  * Set a net80211 key into the hardware.  This handles the
1264c42a7b7eSSam Leffler  * potential distribution of key state to multiple key
1265c42a7b7eSSam Leffler  * cache slots for TKIP with hardware MIC support.
1266c42a7b7eSSam Leffler  */
1267c42a7b7eSSam Leffler static int
1268c42a7b7eSSam Leffler ath_keyset(struct ath_softc *sc, const struct ieee80211_key *k,
1269c42a7b7eSSam Leffler 	const u_int8_t mac[IEEE80211_ADDR_LEN])
1270c42a7b7eSSam Leffler {
1271c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
1272c42a7b7eSSam Leffler 	static const u_int8_t ciphermap[] = {
1273c42a7b7eSSam Leffler 		HAL_CIPHER_WEP,		/* IEEE80211_CIPHER_WEP */
1274c42a7b7eSSam Leffler 		HAL_CIPHER_TKIP,	/* IEEE80211_CIPHER_TKIP */
1275c42a7b7eSSam Leffler 		HAL_CIPHER_AES_OCB,	/* IEEE80211_CIPHER_AES_OCB */
1276c42a7b7eSSam Leffler 		HAL_CIPHER_AES_CCM,	/* IEEE80211_CIPHER_AES_CCM */
1277c42a7b7eSSam Leffler 		(u_int8_t) -1,		/* 4 is not allocated */
1278c42a7b7eSSam Leffler 		HAL_CIPHER_CKIP,	/* IEEE80211_CIPHER_CKIP */
1279c42a7b7eSSam Leffler 		HAL_CIPHER_CLR,		/* IEEE80211_CIPHER_NONE */
1280c42a7b7eSSam Leffler 	};
1281c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1282c42a7b7eSSam Leffler 	const struct ieee80211_cipher *cip = k->wk_cipher;
1283c42a7b7eSSam Leffler 	HAL_KEYVAL hk;
1284c42a7b7eSSam Leffler 
1285c42a7b7eSSam Leffler 	memset(&hk, 0, sizeof(hk));
1286c42a7b7eSSam Leffler 	/*
1287c42a7b7eSSam Leffler 	 * Software crypto uses a "clear key" so non-crypto
1288c42a7b7eSSam Leffler 	 * state kept in the key cache are maintained and
1289c42a7b7eSSam Leffler 	 * so that rx frames have an entry to match.
1290c42a7b7eSSam Leffler 	 */
1291c42a7b7eSSam Leffler 	if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) == 0) {
1292c42a7b7eSSam Leffler 		KASSERT(cip->ic_cipher < N(ciphermap),
1293c42a7b7eSSam Leffler 			("invalid cipher type %u", cip->ic_cipher));
1294c42a7b7eSSam Leffler 		hk.kv_type = ciphermap[cip->ic_cipher];
1295c42a7b7eSSam Leffler 		hk.kv_len = k->wk_keylen;
1296c42a7b7eSSam Leffler 		memcpy(hk.kv_val, k->wk_key, k->wk_keylen);
12978cec0ab9SSam Leffler 	} else
1298c42a7b7eSSam Leffler 		hk.kv_type = HAL_CIPHER_CLR;
1299c42a7b7eSSam Leffler 
1300c42a7b7eSSam Leffler 	if (hk.kv_type == HAL_CIPHER_TKIP &&
1301c42a7b7eSSam Leffler 	    (k->wk_flags & IEEE80211_KEY_SWMIC) == 0 &&
1302c42a7b7eSSam Leffler 	    sc->sc_splitmic) {
1303c42a7b7eSSam Leffler 		return ath_keyset_tkip(sc, k, &hk, mac);
1304c42a7b7eSSam Leffler 	} else {
1305c42a7b7eSSam Leffler 		KEYPRINTF(sc, k->wk_keyix, &hk, mac);
1306c42a7b7eSSam Leffler 		return ath_hal_keyset(ah, k->wk_keyix, &hk, mac);
13078cec0ab9SSam Leffler 	}
1308c42a7b7eSSam Leffler #undef N
13095591b213SSam Leffler }
13105591b213SSam Leffler 
13115591b213SSam Leffler /*
13125591b213SSam Leffler  * Fill the hardware key cache with key entries.
13135591b213SSam Leffler  */
13145591b213SSam Leffler static void
13155591b213SSam Leffler ath_initkeytable(struct ath_softc *sc)
13165591b213SSam Leffler {
13175591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
1318c42a7b7eSSam Leffler 	struct ifnet *ifp = &sc->sc_if;
13195591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1320c42a7b7eSSam Leffler 	const u_int8_t *bssid;
13215591b213SSam Leffler 	int i;
13225591b213SSam Leffler 
1323c42a7b7eSSam Leffler 	/* XXX maybe should reset all keys when !PRIVACY */
1324c42a7b7eSSam Leffler 	if (ic->ic_state == IEEE80211_S_SCAN)
1325c42a7b7eSSam Leffler 		bssid = ifp->if_broadcastaddr;
13265591b213SSam Leffler 	else
1327c42a7b7eSSam Leffler 		bssid = ic->ic_bss->ni_bssid;
1328c42a7b7eSSam Leffler 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1329c42a7b7eSSam Leffler 		struct ieee80211_key *k = &ic->ic_nw_keys[i];
1330c42a7b7eSSam Leffler 
1331c42a7b7eSSam Leffler 		if (k->wk_keylen == 0) {
1332c42a7b7eSSam Leffler 			ath_hal_keyreset(ah, i);
1333c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: reset key %u\n",
1334c42a7b7eSSam Leffler 				__func__, i);
1335c42a7b7eSSam Leffler 		} else {
1336c42a7b7eSSam Leffler 			ath_keyset(sc, k, bssid);
13375591b213SSam Leffler 		}
13385591b213SSam Leffler 	}
1339c42a7b7eSSam Leffler }
1340c42a7b7eSSam Leffler 
1341c42a7b7eSSam Leffler /*
1342c42a7b7eSSam Leffler  * Allocate tx/rx key slots for TKIP.  We allocate two slots for
1343c42a7b7eSSam Leffler  * each key, one for decrypt/encrypt and the other for the MIC.
1344c42a7b7eSSam Leffler  */
1345c42a7b7eSSam Leffler static u_int16_t
1346c42a7b7eSSam Leffler key_alloc_2pair(struct ath_softc *sc)
1347c42a7b7eSSam Leffler {
1348c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
1349c42a7b7eSSam Leffler 	u_int i, keyix;
1350c42a7b7eSSam Leffler 
1351c42a7b7eSSam Leffler 	KASSERT(sc->sc_splitmic, ("key cache !split"));
1352c42a7b7eSSam Leffler 	/* XXX could optimize */
1353c42a7b7eSSam Leffler 	for (i = 0; i < N(sc->sc_keymap)/4; i++) {
1354c42a7b7eSSam Leffler 		u_int8_t b = sc->sc_keymap[i];
1355c42a7b7eSSam Leffler 		if (b != 0xff) {
1356c42a7b7eSSam Leffler 			/*
1357c42a7b7eSSam Leffler 			 * One or more slots in this byte are free.
1358c42a7b7eSSam Leffler 			 */
1359c42a7b7eSSam Leffler 			keyix = i*NBBY;
1360c42a7b7eSSam Leffler 			while (b & 1) {
1361c42a7b7eSSam Leffler 		again:
1362c42a7b7eSSam Leffler 				keyix++;
1363c42a7b7eSSam Leffler 				b >>= 1;
1364c42a7b7eSSam Leffler 			}
1365c42a7b7eSSam Leffler 			/* XXX IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV */
1366c42a7b7eSSam Leffler 			if (isset(sc->sc_keymap, keyix+32) ||
1367c42a7b7eSSam Leffler 			    isset(sc->sc_keymap, keyix+64) ||
1368c42a7b7eSSam Leffler 			    isset(sc->sc_keymap, keyix+32+64)) {
1369c42a7b7eSSam Leffler 				/* full pair unavailable */
1370c42a7b7eSSam Leffler 				/* XXX statistic */
1371c42a7b7eSSam Leffler 				if (keyix == (i+1)*NBBY) {
1372c42a7b7eSSam Leffler 					/* no slots were appropriate, advance */
1373c42a7b7eSSam Leffler 					continue;
1374c42a7b7eSSam Leffler 				}
1375c42a7b7eSSam Leffler 				goto again;
1376c42a7b7eSSam Leffler 			}
1377c42a7b7eSSam Leffler 			setbit(sc->sc_keymap, keyix);
1378c42a7b7eSSam Leffler 			setbit(sc->sc_keymap, keyix+64);
1379c42a7b7eSSam Leffler 			setbit(sc->sc_keymap, keyix+32);
1380c42a7b7eSSam Leffler 			setbit(sc->sc_keymap, keyix+32+64);
1381c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_KEYCACHE,
1382c42a7b7eSSam Leffler 				"%s: key pair %u,%u %u,%u\n",
1383c42a7b7eSSam Leffler 				__func__, keyix, keyix+64,
1384c42a7b7eSSam Leffler 				keyix+32, keyix+32+64);
1385c42a7b7eSSam Leffler 			return keyix;
1386c42a7b7eSSam Leffler 		}
1387c42a7b7eSSam Leffler 	}
1388c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of pair space\n", __func__);
1389c42a7b7eSSam Leffler 	return IEEE80211_KEYIX_NONE;
1390c42a7b7eSSam Leffler #undef N
1391c42a7b7eSSam Leffler }
1392c42a7b7eSSam Leffler 
1393c42a7b7eSSam Leffler /*
1394c42a7b7eSSam Leffler  * Allocate a single key cache slot.
1395c42a7b7eSSam Leffler  */
1396c42a7b7eSSam Leffler static u_int16_t
1397c42a7b7eSSam Leffler key_alloc_single(struct ath_softc *sc)
1398c42a7b7eSSam Leffler {
1399c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
1400c42a7b7eSSam Leffler 	u_int i, keyix;
1401c42a7b7eSSam Leffler 
1402c42a7b7eSSam Leffler 	/* XXX try i,i+32,i+64,i+32+64 to minimize key pair conflicts */
1403c42a7b7eSSam Leffler 	for (i = 0; i < N(sc->sc_keymap); i++) {
1404c42a7b7eSSam Leffler 		u_int8_t b = sc->sc_keymap[i];
1405c42a7b7eSSam Leffler 		if (b != 0xff) {
1406c42a7b7eSSam Leffler 			/*
1407c42a7b7eSSam Leffler 			 * One or more slots are free.
1408c42a7b7eSSam Leffler 			 */
1409c42a7b7eSSam Leffler 			keyix = i*NBBY;
1410c42a7b7eSSam Leffler 			while (b & 1)
1411c42a7b7eSSam Leffler 				keyix++, b >>= 1;
1412c42a7b7eSSam Leffler 			setbit(sc->sc_keymap, keyix);
1413c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: key %u\n",
1414c42a7b7eSSam Leffler 				__func__, keyix);
1415c42a7b7eSSam Leffler 			return keyix;
1416c42a7b7eSSam Leffler 		}
1417c42a7b7eSSam Leffler 	}
1418c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of space\n", __func__);
1419c42a7b7eSSam Leffler 	return IEEE80211_KEYIX_NONE;
1420c42a7b7eSSam Leffler #undef N
1421c42a7b7eSSam Leffler }
1422c42a7b7eSSam Leffler 
1423c42a7b7eSSam Leffler /*
1424c42a7b7eSSam Leffler  * Allocate one or more key cache slots for a uniacst key.  The
1425c42a7b7eSSam Leffler  * key itself is needed only to identify the cipher.  For hardware
1426c42a7b7eSSam Leffler  * TKIP with split cipher+MIC keys we allocate two key cache slot
1427c42a7b7eSSam Leffler  * pairs so that we can setup separate TX and RX MIC keys.  Note
1428c42a7b7eSSam Leffler  * that the MIC key for a TKIP key at slot i is assumed by the
1429c42a7b7eSSam Leffler  * hardware to be at slot i+64.  This limits TKIP keys to the first
1430c42a7b7eSSam Leffler  * 64 entries.
1431c42a7b7eSSam Leffler  */
1432c42a7b7eSSam Leffler static int
1433c42a7b7eSSam Leffler ath_key_alloc(struct ieee80211com *ic, const struct ieee80211_key *k)
1434c42a7b7eSSam Leffler {
1435c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
1436c42a7b7eSSam Leffler 
1437c42a7b7eSSam Leffler 	/*
1438c42a7b7eSSam Leffler 	 * We allocate two pair for TKIP when using the h/w to do
1439c42a7b7eSSam Leffler 	 * the MIC.  For everything else, including software crypto,
1440c42a7b7eSSam Leffler 	 * we allocate a single entry.  Note that s/w crypto requires
1441c42a7b7eSSam Leffler 	 * a pass-through slot on the 5211 and 5212.  The 5210 does
1442c42a7b7eSSam Leffler 	 * not support pass-through cache entries and we map all
1443c42a7b7eSSam Leffler 	 * those requests to slot 0.
1444c42a7b7eSSam Leffler 	 */
1445c42a7b7eSSam Leffler 	if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
1446c42a7b7eSSam Leffler 		return key_alloc_single(sc);
1447c42a7b7eSSam Leffler 	} else if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP &&
1448c42a7b7eSSam Leffler 	    (k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && sc->sc_splitmic) {
1449c42a7b7eSSam Leffler 		return key_alloc_2pair(sc);
1450c42a7b7eSSam Leffler 	} else {
1451c42a7b7eSSam Leffler 		return key_alloc_single(sc);
1452c42a7b7eSSam Leffler 	}
1453c42a7b7eSSam Leffler }
1454c42a7b7eSSam Leffler 
1455c42a7b7eSSam Leffler /*
1456c42a7b7eSSam Leffler  * Delete an entry in the key cache allocated by ath_key_alloc.
1457c42a7b7eSSam Leffler  */
1458c42a7b7eSSam Leffler static int
1459c42a7b7eSSam Leffler ath_key_delete(struct ieee80211com *ic, const struct ieee80211_key *k)
1460c42a7b7eSSam Leffler {
1461c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
1462c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1463c42a7b7eSSam Leffler 	const struct ieee80211_cipher *cip = k->wk_cipher;
1464c42a7b7eSSam Leffler 	u_int keyix = k->wk_keyix;
1465c42a7b7eSSam Leffler 
1466c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: delete key %u\n", __func__, keyix);
1467c42a7b7eSSam Leffler 
1468c42a7b7eSSam Leffler 	ath_hal_keyreset(ah, keyix);
1469c42a7b7eSSam Leffler 	/*
1470c42a7b7eSSam Leffler 	 * Handle split tx/rx keying required for TKIP with h/w MIC.
1471c42a7b7eSSam Leffler 	 */
1472c42a7b7eSSam Leffler 	if (cip->ic_cipher == IEEE80211_CIPHER_TKIP &&
1473c42a7b7eSSam Leffler 	    (k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && sc->sc_splitmic)
1474c42a7b7eSSam Leffler 		ath_hal_keyreset(ah, keyix+32);		/* RX key */
1475c42a7b7eSSam Leffler 	if (keyix >= IEEE80211_WEP_NKID) {
1476c42a7b7eSSam Leffler 		/*
1477c42a7b7eSSam Leffler 		 * Don't touch keymap entries for global keys so
1478c42a7b7eSSam Leffler 		 * they are never considered for dynamic allocation.
1479c42a7b7eSSam Leffler 		 */
1480c42a7b7eSSam Leffler 		clrbit(sc->sc_keymap, keyix);
1481c42a7b7eSSam Leffler 		if (cip->ic_cipher == IEEE80211_CIPHER_TKIP &&
1482c42a7b7eSSam Leffler 		    (k->wk_flags & IEEE80211_KEY_SWMIC) == 0 &&
1483c42a7b7eSSam Leffler 		    sc->sc_splitmic) {
1484c42a7b7eSSam Leffler 			clrbit(sc->sc_keymap, keyix+64);	/* TX key MIC */
1485c42a7b7eSSam Leffler 			clrbit(sc->sc_keymap, keyix+32);	/* RX key */
1486c42a7b7eSSam Leffler 			clrbit(sc->sc_keymap, keyix+32+64);	/* RX key MIC */
1487c42a7b7eSSam Leffler 		}
1488c42a7b7eSSam Leffler 	}
1489c42a7b7eSSam Leffler 	return 1;
1490c42a7b7eSSam Leffler }
1491c42a7b7eSSam Leffler 
1492c42a7b7eSSam Leffler /*
1493c42a7b7eSSam Leffler  * Set the key cache contents for the specified key.  Key cache
1494c42a7b7eSSam Leffler  * slot(s) must already have been allocated by ath_key_alloc.
1495c42a7b7eSSam Leffler  */
1496c42a7b7eSSam Leffler static int
1497c42a7b7eSSam Leffler ath_key_set(struct ieee80211com *ic, const struct ieee80211_key *k,
1498c42a7b7eSSam Leffler 	const u_int8_t mac[IEEE80211_ADDR_LEN])
1499c42a7b7eSSam Leffler {
1500c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
1501c42a7b7eSSam Leffler 
1502c42a7b7eSSam Leffler 	return ath_keyset(sc, k, mac);
1503c42a7b7eSSam Leffler }
1504c42a7b7eSSam Leffler 
1505c42a7b7eSSam Leffler /*
1506c42a7b7eSSam Leffler  * Block/unblock tx+rx processing while a key change is done.
1507c42a7b7eSSam Leffler  * We assume the caller serializes key management operations
1508c42a7b7eSSam Leffler  * so we only need to worry about synchronization with other
1509c42a7b7eSSam Leffler  * uses that originate in the driver.
1510c42a7b7eSSam Leffler  */
1511c42a7b7eSSam Leffler static void
1512c42a7b7eSSam Leffler ath_key_update_begin(struct ieee80211com *ic)
1513c42a7b7eSSam Leffler {
1514c42a7b7eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
1515c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
1516c42a7b7eSSam Leffler 
1517c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
1518c42a7b7eSSam Leffler #if 0
1519c42a7b7eSSam Leffler 	tasklet_disable(&sc->sc_rxtq);
1520c42a7b7eSSam Leffler #endif
1521c42a7b7eSSam Leffler 	IF_LOCK(&ifp->if_snd);		/* NB: doesn't block mgmt frames */
1522c42a7b7eSSam Leffler }
1523c42a7b7eSSam Leffler 
1524c42a7b7eSSam Leffler static void
1525c42a7b7eSSam Leffler ath_key_update_end(struct ieee80211com *ic)
1526c42a7b7eSSam Leffler {
1527c42a7b7eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
1528c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
1529c42a7b7eSSam Leffler 
1530c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
1531c42a7b7eSSam Leffler 	IF_UNLOCK(&ifp->if_snd);
1532c42a7b7eSSam Leffler #if 0
1533c42a7b7eSSam Leffler 	tasklet_enable(&sc->sc_rxtq);
1534c42a7b7eSSam Leffler #endif
1535c42a7b7eSSam Leffler }
15365591b213SSam Leffler 
15374bc0e754SSam Leffler /*
15384bc0e754SSam Leffler  * Calculate the receive filter according to the
15394bc0e754SSam Leffler  * operating mode and state:
15404bc0e754SSam Leffler  *
15414bc0e754SSam Leffler  * o always accept unicast, broadcast, and multicast traffic
1542c42a7b7eSSam Leffler  * o maintain current state of phy error reception (the hal
1543c42a7b7eSSam Leffler  *   may enable phy error frames for noise immunity work)
15444bc0e754SSam Leffler  * o probe request frames are accepted only when operating in
15454bc0e754SSam Leffler  *   hostap, adhoc, or monitor modes
15464bc0e754SSam Leffler  * o enable promiscuous mode according to the interface state
15474bc0e754SSam Leffler  * o accept beacons:
15484bc0e754SSam Leffler  *   - when operating in adhoc mode so the 802.11 layer creates
15494bc0e754SSam Leffler  *     node table entries for peers,
15504bc0e754SSam Leffler  *   - when operating in station mode for collecting rssi data when
15514bc0e754SSam Leffler  *     the station is otherwise quiet, or
15524bc0e754SSam Leffler  *   - when scanning
15534bc0e754SSam Leffler  */
15544bc0e754SSam Leffler static u_int32_t
1555c42a7b7eSSam Leffler ath_calcrxfilter(struct ath_softc *sc, enum ieee80211_state state)
15564bc0e754SSam Leffler {
15574bc0e754SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
15584bc0e754SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1559c42a7b7eSSam Leffler 	struct ifnet *ifp = &sc->sc_if;
15604bc0e754SSam Leffler 	u_int32_t rfilt;
15614bc0e754SSam Leffler 
15624bc0e754SSam Leffler 	rfilt = (ath_hal_getrxfilter(ah) & HAL_RX_FILTER_PHYERR)
15634bc0e754SSam Leffler 	      | HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST;
15644bc0e754SSam Leffler 	if (ic->ic_opmode != IEEE80211_M_STA)
15654bc0e754SSam Leffler 		rfilt |= HAL_RX_FILTER_PROBEREQ;
15664bc0e754SSam Leffler 	if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
15674bc0e754SSam Leffler 	    (ifp->if_flags & IFF_PROMISC))
15684bc0e754SSam Leffler 		rfilt |= HAL_RX_FILTER_PROM;
15694bc0e754SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA ||
15704bc0e754SSam Leffler 	    ic->ic_opmode == IEEE80211_M_IBSS ||
1571c42a7b7eSSam Leffler 	    state == IEEE80211_S_SCAN)
15724bc0e754SSam Leffler 		rfilt |= HAL_RX_FILTER_BEACON;
15734bc0e754SSam Leffler 	return rfilt;
15744bc0e754SSam Leffler }
15754bc0e754SSam Leffler 
15765591b213SSam Leffler static void
15775591b213SSam Leffler ath_mode_init(struct ath_softc *sc)
15785591b213SSam Leffler {
15795591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
15805591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1581c42a7b7eSSam Leffler 	struct ifnet *ifp = &sc->sc_if;
15825591b213SSam Leffler 	u_int32_t rfilt, mfilt[2], val;
15835591b213SSam Leffler 	u_int8_t pos;
15845591b213SSam Leffler 	struct ifmultiaddr *ifma;
15855591b213SSam Leffler 
15864bc0e754SSam Leffler 	/* configure rx filter */
1587c42a7b7eSSam Leffler 	rfilt = ath_calcrxfilter(sc, ic->ic_state);
15884bc0e754SSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
15894bc0e754SSam Leffler 
15905591b213SSam Leffler 	/* configure operational mode */
1591c42a7b7eSSam Leffler 	ath_hal_setopmode(ah);
1592c42a7b7eSSam Leffler 
1593c42a7b7eSSam Leffler 	/*
1594c42a7b7eSSam Leffler 	 * Handle any link-level address change.  Note that we only
1595c42a7b7eSSam Leffler 	 * need to force ic_myaddr; any other addresses are handled
1596c42a7b7eSSam Leffler 	 * as a byproduct of the ifnet code marking the interface
1597c42a7b7eSSam Leffler 	 * down then up.
1598c42a7b7eSSam Leffler 	 *
1599c42a7b7eSSam Leffler 	 * XXX should get from lladdr instead of arpcom but that's more work
1600c42a7b7eSSam Leffler 	 */
1601c42a7b7eSSam Leffler 	IEEE80211_ADDR_COPY(ic->ic_myaddr, IFP2AC(ifp)->ac_enaddr);
1602c42a7b7eSSam Leffler 	ath_hal_setmac(ah, ic->ic_myaddr);
16035591b213SSam Leffler 
16045591b213SSam Leffler 	/* calculate and install multicast filter */
16055591b213SSam Leffler 	if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
16065591b213SSam Leffler 		mfilt[0] = mfilt[1] = 0;
16075591b213SSam Leffler 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
16085591b213SSam Leffler 			caddr_t dl;
16095591b213SSam Leffler 
16105591b213SSam Leffler 			/* calculate XOR of eight 6bit values */
16115591b213SSam Leffler 			dl = LLADDR((struct sockaddr_dl *) ifma->ifma_addr);
16125591b213SSam Leffler 			val = LE_READ_4(dl + 0);
16135591b213SSam Leffler 			pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
16145591b213SSam Leffler 			val = LE_READ_4(dl + 3);
16155591b213SSam Leffler 			pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
16165591b213SSam Leffler 			pos &= 0x3f;
16175591b213SSam Leffler 			mfilt[pos / 32] |= (1 << (pos % 32));
16185591b213SSam Leffler 		}
16195591b213SSam Leffler 	} else {
16205591b213SSam Leffler 		mfilt[0] = mfilt[1] = ~0;
16215591b213SSam Leffler 	}
16225591b213SSam Leffler 	ath_hal_setmcastfilter(ah, mfilt[0], mfilt[1]);
1623c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x, MC filter %08x:%08x\n",
1624c42a7b7eSSam Leffler 		__func__, rfilt, mfilt[0], mfilt[1]);
16255591b213SSam Leffler }
16265591b213SSam Leffler 
16275591b213SSam Leffler static void
16285591b213SSam Leffler ath_mbuf_load_cb(void *arg, bus_dma_segment_t *seg, int nseg, bus_size_t mapsize, int error)
16295591b213SSam Leffler {
16305591b213SSam Leffler 	struct ath_buf *bf = arg;
16315591b213SSam Leffler 
1632d77367bfSSam Leffler 	KASSERT(nseg <= ATH_MAX_SCATTER, ("too many DMA segments %u", nseg));
1633d77367bfSSam Leffler 	KASSERT(error == 0, ("error %u on bus_dma callback", error));
16345591b213SSam Leffler 	bf->bf_mapsize = mapsize;
16355591b213SSam Leffler 	bf->bf_nseg = nseg;
16365591b213SSam Leffler 	bcopy(seg, bf->bf_segs, nseg * sizeof (seg[0]));
16375591b213SSam Leffler }
16385591b213SSam Leffler 
1639c42a7b7eSSam Leffler /*
1640c42a7b7eSSam Leffler  * Set the slot time based on the current setting.
1641c42a7b7eSSam Leffler  */
1642c42a7b7eSSam Leffler static void
1643c42a7b7eSSam Leffler ath_setslottime(struct ath_softc *sc)
1644c42a7b7eSSam Leffler {
1645c42a7b7eSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
1646c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1647c42a7b7eSSam Leffler 
1648c42a7b7eSSam Leffler 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
1649c42a7b7eSSam Leffler 		ath_hal_setslottime(ah, HAL_SLOT_TIME_9);
1650c42a7b7eSSam Leffler 	else
1651c42a7b7eSSam Leffler 		ath_hal_setslottime(ah, HAL_SLOT_TIME_20);
1652c42a7b7eSSam Leffler 	sc->sc_updateslot = OK;
1653c42a7b7eSSam Leffler }
1654c42a7b7eSSam Leffler 
1655c42a7b7eSSam Leffler /*
1656c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update the
1657c42a7b7eSSam Leffler  * slot time based on the current setting.
1658c42a7b7eSSam Leffler  */
1659c42a7b7eSSam Leffler static void
1660c42a7b7eSSam Leffler ath_updateslot(struct ifnet *ifp)
1661c42a7b7eSSam Leffler {
1662c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
1663c42a7b7eSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
1664c42a7b7eSSam Leffler 
1665c42a7b7eSSam Leffler 	/*
1666c42a7b7eSSam Leffler 	 * When not coordinating the BSS, change the hardware
1667c42a7b7eSSam Leffler 	 * immediately.  For other operation we defer the change
1668c42a7b7eSSam Leffler 	 * until beacon updates have propagated to the stations.
1669c42a7b7eSSam Leffler 	 */
1670c42a7b7eSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1671c42a7b7eSSam Leffler 		sc->sc_updateslot = UPDATE;
1672c42a7b7eSSam Leffler 	else
1673c42a7b7eSSam Leffler 		ath_setslottime(sc);
1674c42a7b7eSSam Leffler }
1675c42a7b7eSSam Leffler 
1676c42a7b7eSSam Leffler /*
1677c42a7b7eSSam Leffler  * Allocate and setup an initial beacon frame.
1678c42a7b7eSSam Leffler  */
16795591b213SSam Leffler static int
16805591b213SSam Leffler ath_beacon_alloc(struct ath_softc *sc, struct ieee80211_node *ni)
16815591b213SSam Leffler {
1682c42a7b7eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
16835591b213SSam Leffler 	struct ath_buf *bf;
16845591b213SSam Leffler 	struct mbuf *m;
1685c42a7b7eSSam Leffler 	int error;
16865591b213SSam Leffler 
1687c42a7b7eSSam Leffler 	bf = STAILQ_FIRST(&sc->sc_bbuf);
1688c42a7b7eSSam Leffler 	if (bf == NULL) {
1689c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON, "%s: no dma buffers\n", __func__);
1690c42a7b7eSSam Leffler 		sc->sc_stats.ast_be_nombuf++;	/* XXX */
1691c42a7b7eSSam Leffler 		return ENOMEM;			/* XXX */
1692c42a7b7eSSam Leffler 	}
16935591b213SSam Leffler 	if (bf->bf_m != NULL) {
16945591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
16955591b213SSam Leffler 		m_freem(bf->bf_m);
16965591b213SSam Leffler 		bf->bf_m = NULL;
16975591b213SSam Leffler 		bf->bf_node = NULL;
16985591b213SSam Leffler 	}
16995591b213SSam Leffler 	/*
17005591b213SSam Leffler 	 * NB: the beacon data buffer must be 32-bit aligned;
17015591b213SSam Leffler 	 * we assume the mbuf routines will return us something
17025591b213SSam Leffler 	 * with this alignment (perhaps should assert).
17035591b213SSam Leffler 	 */
1704c42a7b7eSSam Leffler 	m = ieee80211_beacon_alloc(ic, ni, &sc->sc_boff);
17055591b213SSam Leffler 	if (m == NULL) {
1706c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON, "%s: cannot get mbuf\n",
1707c42a7b7eSSam Leffler 			__func__);
17085591b213SSam Leffler 		sc->sc_stats.ast_be_nombuf++;
17095591b213SSam Leffler 		return ENOMEM;
17105591b213SSam Leffler 	}
17115591b213SSam Leffler 	error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m,
17125591b213SSam Leffler 				     ath_mbuf_load_cb, bf,
17135591b213SSam Leffler 				     BUS_DMA_NOWAIT);
1714c42a7b7eSSam Leffler 	if (error == 0) {
1715c42a7b7eSSam Leffler 		bf->bf_m = m;
1716c42a7b7eSSam Leffler 		bf->bf_node = ni;		/* NB: no held reference */
1717c42a7b7eSSam Leffler 	} else {
17185591b213SSam Leffler 		m_freem(m);
1719c42a7b7eSSam Leffler 	}
17205591b213SSam Leffler 	return error;
17215591b213SSam Leffler }
1722c42a7b7eSSam Leffler 
1723c42a7b7eSSam Leffler /*
1724c42a7b7eSSam Leffler  * Setup the beacon frame for transmit.
1725c42a7b7eSSam Leffler  */
1726c42a7b7eSSam Leffler static void
1727c42a7b7eSSam Leffler ath_beacon_setup(struct ath_softc *sc, struct ath_buf *bf)
1728c42a7b7eSSam Leffler {
1729c42a7b7eSSam Leffler #define	USE_SHPREAMBLE(_ic) \
1730c42a7b7eSSam Leffler 	(((_ic)->ic_flags & (IEEE80211_F_SHPREAMBLE | IEEE80211_F_USEBARKER))\
1731c42a7b7eSSam Leffler 		== IEEE80211_F_SHPREAMBLE)
1732c42a7b7eSSam Leffler 	struct ieee80211_node *ni = bf->bf_node;
1733c42a7b7eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
1734c42a7b7eSSam Leffler 	struct mbuf *m = bf->bf_m;
1735c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1736c42a7b7eSSam Leffler 	struct ath_node *an = ATH_NODE(ni);
1737c42a7b7eSSam Leffler 	struct ath_desc *ds;
1738c42a7b7eSSam Leffler 	int flags, antenna;
1739c42a7b7eSSam Leffler 	u_int8_t rate;
1740c42a7b7eSSam Leffler 
1741c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_BEACON, "%s: m %p len %u\n",
1742c42a7b7eSSam Leffler 		__func__, m, m->m_len);
17435591b213SSam Leffler 
17445591b213SSam Leffler 	/* setup descriptors */
17455591b213SSam Leffler 	ds = bf->bf_desc;
17465591b213SSam Leffler 
1747c42a7b7eSSam Leffler 	flags = HAL_TXDESC_NOACK;
1748c42a7b7eSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_hasveol) {
1749c42a7b7eSSam Leffler 		ds->ds_link = bf->bf_daddr;	/* self-linked */
1750c42a7b7eSSam Leffler 		flags |= HAL_TXDESC_VEOL;
1751c42a7b7eSSam Leffler 		/*
1752c42a7b7eSSam Leffler 		 * Let hardware handle antenna switching.
1753c42a7b7eSSam Leffler 		 */
1754c42a7b7eSSam Leffler 		antenna = 0;
1755c42a7b7eSSam Leffler 	} else {
17565591b213SSam Leffler 		ds->ds_link = 0;
1757c42a7b7eSSam Leffler 		/*
1758c42a7b7eSSam Leffler 		 * Switch antenna every 4 beacons.
1759c42a7b7eSSam Leffler 		 * XXX assumes two antenna
1760c42a7b7eSSam Leffler 		 */
1761c42a7b7eSSam Leffler 		antenna = (sc->sc_stats.ast_be_xmit & 4 ? 2 : 1);
1762c42a7b7eSSam Leffler 	}
1763c42a7b7eSSam Leffler 
1764c42a7b7eSSam Leffler 	KASSERT(bf->bf_nseg == 1,
1765c42a7b7eSSam Leffler 		("multi-segment beacon frame; nseg %u", bf->bf_nseg));
17665591b213SSam Leffler 	ds->ds_data = bf->bf_segs[0].ds_addr;
17675591b213SSam Leffler 	/*
17685591b213SSam Leffler 	 * Calculate rate code.
17695591b213SSam Leffler 	 * XXX everything at min xmit rate
17705591b213SSam Leffler 	 */
1771c42a7b7eSSam Leffler 	if (USE_SHPREAMBLE(ic))
1772c42a7b7eSSam Leffler 		rate = an->an_tx_mgtratesp;
17735591b213SSam Leffler 	else
1774c42a7b7eSSam Leffler 		rate = an->an_tx_mgtrate;
17755591b213SSam Leffler 	ath_hal_setuptxdesc(ah, ds
1776c42a7b7eSSam Leffler 		, m->m_len + IEEE80211_CRC_LEN	/* frame length */
17775591b213SSam Leffler 		, sizeof(struct ieee80211_frame)/* header length */
17785591b213SSam Leffler 		, HAL_PKT_TYPE_BEACON		/* Atheros packet type */
1779c42a7b7eSSam Leffler 		, ni->ni_txpower		/* txpower XXX */
17805591b213SSam Leffler 		, rate, 1			/* series 0 rate/tries */
17815591b213SSam Leffler 		, HAL_TXKEYIX_INVALID		/* no encryption */
1782c42a7b7eSSam Leffler 		, antenna			/* antenna mode */
1783c42a7b7eSSam Leffler 		, flags				/* no ack, veol for beacons */
17845591b213SSam Leffler 		, 0				/* rts/cts rate */
17855591b213SSam Leffler 		, 0				/* rts/cts duration */
17865591b213SSam Leffler 	);
17875591b213SSam Leffler 	/* NB: beacon's BufLen must be a multiple of 4 bytes */
17885591b213SSam Leffler 	ath_hal_filltxdesc(ah, ds
1789c42a7b7eSSam Leffler 		, roundup(m->m_len, 4)		/* buffer length */
17905591b213SSam Leffler 		, AH_TRUE			/* first segment */
17915591b213SSam Leffler 		, AH_TRUE			/* last segment */
1792c42a7b7eSSam Leffler 		, ds				/* first descriptor */
17935591b213SSam Leffler 	);
1794c42a7b7eSSam Leffler #undef USE_SHPREAMBLE
17955591b213SSam Leffler }
17965591b213SSam Leffler 
1797c42a7b7eSSam Leffler /*
1798c42a7b7eSSam Leffler  * Transmit a beacon frame at SWBA.  Dynamic updates to the
1799c42a7b7eSSam Leffler  * frame contents are done as needed and the slot time is
1800c42a7b7eSSam Leffler  * also adjusted based on current state.
1801c42a7b7eSSam Leffler  */
18025591b213SSam Leffler static void
18035591b213SSam Leffler ath_beacon_proc(void *arg, int pending)
18045591b213SSam Leffler {
18055591b213SSam Leffler 	struct ath_softc *sc = arg;
1806c42a7b7eSSam Leffler 	struct ath_buf *bf = STAILQ_FIRST(&sc->sc_bbuf);
1807c42a7b7eSSam Leffler 	struct ieee80211_node *ni = bf->bf_node;
1808c42a7b7eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
18095591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1810c42a7b7eSSam Leffler 	struct mbuf *m;
1811c42a7b7eSSam Leffler 	int ncabq, error, otherant;
18125591b213SSam Leffler 
1813c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_BEACON_PROC, "%s: pending %u\n",
1814c42a7b7eSSam Leffler 		__func__, pending);
1815c42a7b7eSSam Leffler 
18160a915fadSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA ||
1817c42a7b7eSSam Leffler 	    ic->ic_opmode == IEEE80211_M_MONITOR ||
18180a915fadSSam Leffler 	    bf == NULL || bf->bf_m == NULL) {
1819c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: ic_flags=%x bf=%p bf_m=%p\n",
1820c42a7b7eSSam Leffler 			__func__, ic->ic_flags, bf, bf ? bf->bf_m : NULL);
18215591b213SSam Leffler 		return;
18225591b213SSam Leffler 	}
1823c42a7b7eSSam Leffler 	/*
1824c42a7b7eSSam Leffler 	 * Check if the previous beacon has gone out.  If
1825c42a7b7eSSam Leffler 	 * not don't don't try to post another, skip this
1826c42a7b7eSSam Leffler 	 * period and wait for the next.  Missed beacons
1827c42a7b7eSSam Leffler 	 * indicate a problem and should not occur.  If we
1828c42a7b7eSSam Leffler 	 * miss too many consecutive beacons reset the device.
1829c42a7b7eSSam Leffler 	 */
1830c42a7b7eSSam Leffler 	if (ath_hal_numtxpending(ah, sc->sc_bhalq) != 0) {
1831c42a7b7eSSam Leffler 		sc->sc_bmisscount++;
1832c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON_PROC,
1833c42a7b7eSSam Leffler 			"%s: missed %u consecutive beacons\n",
1834c42a7b7eSSam Leffler 			__func__, sc->sc_bmisscount);
1835c42a7b7eSSam Leffler 		if (sc->sc_bmisscount > 3)		/* NB: 3 is a guess */
1836c42a7b7eSSam Leffler 			taskqueue_enqueue(taskqueue_swi, &sc->sc_bstucktask);
1837c42a7b7eSSam Leffler 		return;
1838c42a7b7eSSam Leffler 	}
1839c42a7b7eSSam Leffler 	if (sc->sc_bmisscount != 0) {
1840c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
1841c42a7b7eSSam Leffler 			"%s: resume beacon xmit after %u misses\n",
1842c42a7b7eSSam Leffler 			__func__, sc->sc_bmisscount);
1843c42a7b7eSSam Leffler 		sc->sc_bmisscount = 0;
1844c42a7b7eSSam Leffler 	}
1845c42a7b7eSSam Leffler 
1846c42a7b7eSSam Leffler 	/*
1847c42a7b7eSSam Leffler 	 * Update dynamic beacon contents.  If this returns
1848c42a7b7eSSam Leffler 	 * non-zero then we need to remap the memory because
1849c42a7b7eSSam Leffler 	 * the beacon frame changed size (probably because
1850c42a7b7eSSam Leffler 	 * of the TIM bitmap).
1851c42a7b7eSSam Leffler 	 */
1852c42a7b7eSSam Leffler 	m = bf->bf_m;
1853c42a7b7eSSam Leffler 	ncabq = ath_hal_numtxpending(ah, sc->sc_cabq->axq_qnum);
1854c42a7b7eSSam Leffler 	if (ieee80211_beacon_update(ic, bf->bf_node, &sc->sc_boff, m, ncabq)) {
1855c42a7b7eSSam Leffler 		/* XXX too conservative? */
1856c42a7b7eSSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
1857c42a7b7eSSam Leffler 		error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m,
1858c42a7b7eSSam Leffler 					     ath_mbuf_load_cb, bf,
1859c42a7b7eSSam Leffler 					     BUS_DMA_NOWAIT);
1860c42a7b7eSSam Leffler 		if (error != 0) {
1861c42a7b7eSSam Leffler 			if_printf(ic->ic_ifp,
1862c42a7b7eSSam Leffler 			    "%s: bus_dmamap_load_mbuf failed, error %u\n",
1863c42a7b7eSSam Leffler 			    __func__, error);
1864c42a7b7eSSam Leffler 			return;
1865c42a7b7eSSam Leffler 		}
1866c42a7b7eSSam Leffler 	}
1867c42a7b7eSSam Leffler 
1868c42a7b7eSSam Leffler 	/*
1869c42a7b7eSSam Leffler 	 * Handle slot time change when a non-ERP station joins/leaves
1870c42a7b7eSSam Leffler 	 * an 11g network.  The 802.11 layer notifies us via callback,
1871c42a7b7eSSam Leffler 	 * we mark updateslot, then wait one beacon before effecting
1872c42a7b7eSSam Leffler 	 * the change.  This gives associated stations at least one
1873c42a7b7eSSam Leffler 	 * beacon interval to note the state change.
1874c42a7b7eSSam Leffler 	 */
1875c42a7b7eSSam Leffler 	/* XXX locking */
1876c42a7b7eSSam Leffler 	if (sc->sc_updateslot == UPDATE)
1877c42a7b7eSSam Leffler 		sc->sc_updateslot = COMMIT;	/* commit next beacon */
1878c42a7b7eSSam Leffler 	else if (sc->sc_updateslot == COMMIT)
1879c42a7b7eSSam Leffler 		ath_setslottime(sc);		/* commit change to h/w */
1880c42a7b7eSSam Leffler 
1881c42a7b7eSSam Leffler 	/*
1882c42a7b7eSSam Leffler 	 * Check recent per-antenna transmit statistics and flip
1883c42a7b7eSSam Leffler 	 * the default antenna if noticeably more frames went out
1884c42a7b7eSSam Leffler 	 * on the non-default antenna.
1885c42a7b7eSSam Leffler 	 * XXX assumes 2 anntenae
1886c42a7b7eSSam Leffler 	 */
1887c42a7b7eSSam Leffler 	otherant = sc->sc_defant & 1 ? 2 : 1;
1888c42a7b7eSSam Leffler 	if (sc->sc_ant_tx[otherant] > sc->sc_ant_tx[sc->sc_defant] + 2)
1889c42a7b7eSSam Leffler 		ath_setdefantenna(sc, otherant);
1890c42a7b7eSSam Leffler 	sc->sc_ant_tx[1] = sc->sc_ant_tx[2] = 0;
1891c42a7b7eSSam Leffler 
1892c42a7b7eSSam Leffler 	/*
1893c42a7b7eSSam Leffler 	 * Construct tx descriptor.
1894c42a7b7eSSam Leffler 	 */
1895c42a7b7eSSam Leffler 	ath_beacon_setup(sc, bf);
1896c42a7b7eSSam Leffler 
1897c42a7b7eSSam Leffler 	/*
1898c42a7b7eSSam Leffler 	 * Stop any current dma and put the new frame on the queue.
1899c42a7b7eSSam Leffler 	 * This should never fail since we check above that no frames
1900c42a7b7eSSam Leffler 	 * are still pending on the queue.
1901c42a7b7eSSam Leffler 	 */
19025591b213SSam Leffler 	if (!ath_hal_stoptxdma(ah, sc->sc_bhalq)) {
1903c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY,
1904c42a7b7eSSam Leffler 			"%s: beacon queue %u did not stop?\n",
1905c42a7b7eSSam Leffler 			__func__, sc->sc_bhalq);
19065591b213SSam Leffler 	}
19075591b213SSam Leffler 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
19085591b213SSam Leffler 
1909c42a7b7eSSam Leffler 	/*
1910c42a7b7eSSam Leffler 	 * Enable the CAB queue before the beacon queue to
1911c42a7b7eSSam Leffler 	 * insure cab frames are triggered by this beacon.
1912c42a7b7eSSam Leffler 	 */
1913c42a7b7eSSam Leffler 	if (sc->sc_boff.bo_tim[4] & 1)		/* NB: only at DTIM */
1914c42a7b7eSSam Leffler 		ath_hal_txstart(ah, sc->sc_cabq->axq_qnum);
19155591b213SSam Leffler 	ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr);
19165591b213SSam Leffler 	ath_hal_txstart(ah, sc->sc_bhalq);
1917c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_BEACON_PROC,
1918c42a7b7eSSam Leffler 		"%s: TXDP[%u] = %p (%p)\n", __func__,
1919c42a7b7eSSam Leffler 		sc->sc_bhalq, (caddr_t)bf->bf_daddr, bf->bf_desc);
1920c42a7b7eSSam Leffler 
1921c42a7b7eSSam Leffler 	sc->sc_stats.ast_be_xmit++;
19225591b213SSam Leffler }
19235591b213SSam Leffler 
1924c42a7b7eSSam Leffler /*
1925c42a7b7eSSam Leffler  * Reset the hardware after detecting beacons have stopped.
1926c42a7b7eSSam Leffler  */
1927c42a7b7eSSam Leffler static void
1928c42a7b7eSSam Leffler ath_bstuck_proc(void *arg, int pending)
1929c42a7b7eSSam Leffler {
1930c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
1931c42a7b7eSSam Leffler 	struct ifnet *ifp = &sc->sc_if;
1932c42a7b7eSSam Leffler 
1933c42a7b7eSSam Leffler 	if_printf(ifp, "stuck beacon; resetting (bmiss count %u)\n",
1934c42a7b7eSSam Leffler 		sc->sc_bmisscount);
1935c42a7b7eSSam Leffler 	ath_reset(ifp);
1936c42a7b7eSSam Leffler }
1937c42a7b7eSSam Leffler 
1938c42a7b7eSSam Leffler /*
1939c42a7b7eSSam Leffler  * Reclaim beacon resources.
1940c42a7b7eSSam Leffler  */
19415591b213SSam Leffler static void
19425591b213SSam Leffler ath_beacon_free(struct ath_softc *sc)
19435591b213SSam Leffler {
1944c42a7b7eSSam Leffler 	struct ath_buf *bf;
19455591b213SSam Leffler 
1946c42a7b7eSSam Leffler 	STAILQ_FOREACH(bf, &sc->sc_bbuf, bf_list)
19475591b213SSam Leffler 		if (bf->bf_m != NULL) {
19485591b213SSam Leffler 			bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
19495591b213SSam Leffler 			m_freem(bf->bf_m);
19505591b213SSam Leffler 			bf->bf_m = NULL;
19515591b213SSam Leffler 			bf->bf_node = NULL;
19525591b213SSam Leffler 		}
19535591b213SSam Leffler }
19545591b213SSam Leffler 
19555591b213SSam Leffler /*
19565591b213SSam Leffler  * Configure the beacon and sleep timers.
19575591b213SSam Leffler  *
19585591b213SSam Leffler  * When operating as an AP this resets the TSF and sets
19595591b213SSam Leffler  * up the hardware to notify us when we need to issue beacons.
19605591b213SSam Leffler  *
19615591b213SSam Leffler  * When operating in station mode this sets up the beacon
19625591b213SSam Leffler  * timers according to the timestamp of the last received
19635591b213SSam Leffler  * beacon and the current TSF, configures PCF and DTIM
19645591b213SSam Leffler  * handling, programs the sleep registers so the hardware
19655591b213SSam Leffler  * will wakeup in time to receive beacons, and configures
19665591b213SSam Leffler  * the beacon miss handling so we'll receive a BMISS
19675591b213SSam Leffler  * interrupt when we stop seeing beacons from the AP
19685591b213SSam Leffler  * we've associated with.
19695591b213SSam Leffler  */
19705591b213SSam Leffler static void
19715591b213SSam Leffler ath_beacon_config(struct ath_softc *sc)
19725591b213SSam Leffler {
1973a6c992f4SSam Leffler #define	MS_TO_TU(x)	(((x) * 1000) / 1024)
19745591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
19755591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
19765591b213SSam Leffler 	struct ieee80211_node *ni = ic->ic_bss;
1977c42a7b7eSSam Leffler 	u_int32_t nexttbtt, intval;
19785591b213SSam Leffler 
1979c42a7b7eSSam Leffler 	nexttbtt = (LE_READ_4(ni->ni_tstamp.data + 4) << 22) |
1980c42a7b7eSSam Leffler 	    (LE_READ_4(ni->ni_tstamp.data) >> 10);
1981a6c992f4SSam Leffler 	intval = MS_TO_TU(ni->ni_intval) & HAL_BEACON_PERIOD;
1982a6c992f4SSam Leffler 	if (nexttbtt == 0)		/* e.g. for ap mode */
1983a6c992f4SSam Leffler 		nexttbtt = intval;
1984a6c992f4SSam Leffler 	else if (intval)		/* NB: can be 0 for monitor mode */
1985a6c992f4SSam Leffler 		nexttbtt = roundup(nexttbtt, intval);
1986a6c992f4SSam Leffler 	DPRINTF(sc, ATH_DEBUG_BEACON, "%s: nexttbtt %u intval %u (%u)\n",
1987a6c992f4SSam Leffler 		__func__, nexttbtt, intval, ni->ni_intval);
19886b59f5e3SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA) {
19895591b213SSam Leffler 		HAL_BEACON_STATE bs;
19905591b213SSam Leffler 		u_int32_t bmisstime;
19915591b213SSam Leffler 
19925591b213SSam Leffler 		/* NB: no PCF support right now */
19935591b213SSam Leffler 		memset(&bs, 0, sizeof(bs));
1994a6c992f4SSam Leffler 		bs.bs_intval = intval;
19955591b213SSam Leffler 		bs.bs_nexttbtt = nexttbtt;
19965591b213SSam Leffler 		bs.bs_dtimperiod = bs.bs_intval;
19975591b213SSam Leffler 		bs.bs_nextdtim = nexttbtt;
19985591b213SSam Leffler 		/*
1999c42a7b7eSSam Leffler 		 * The 802.11 layer records the offset to the DTIM
2000c42a7b7eSSam Leffler 		 * bitmap while receiving beacons; use it here to
2001c42a7b7eSSam Leffler 		 * enable h/w detection of our AID being marked in
2002c42a7b7eSSam Leffler 		 * the bitmap vector (to indicate frames for us are
2003c42a7b7eSSam Leffler 		 * pending at the AP).
2004c42a7b7eSSam Leffler 		 */
2005c42a7b7eSSam Leffler 		bs.bs_timoffset = ni->ni_timoff;
2006c42a7b7eSSam Leffler 		/*
20075591b213SSam Leffler 		 * Calculate the number of consecutive beacons to miss
20085591b213SSam Leffler 		 * before taking a BMISS interrupt.  The configuration
20095591b213SSam Leffler 		 * is specified in ms, so we need to convert that to
20105591b213SSam Leffler 		 * TU's and then calculate based on the beacon interval.
20115591b213SSam Leffler 		 * Note that we clamp the result to at most 10 beacons.
20125591b213SSam Leffler 		 */
2013a6c992f4SSam Leffler 		bmisstime = MS_TO_TU(ic->ic_bmisstimeout);
2014a6c992f4SSam Leffler 		bs.bs_bmissthreshold = howmany(bmisstime, intval);
20155591b213SSam Leffler 		if (bs.bs_bmissthreshold > 10)
20165591b213SSam Leffler 			bs.bs_bmissthreshold = 10;
20175591b213SSam Leffler 		else if (bs.bs_bmissthreshold <= 0)
20185591b213SSam Leffler 			bs.bs_bmissthreshold = 1;
20195591b213SSam Leffler 
20205591b213SSam Leffler 		/*
20215591b213SSam Leffler 		 * Calculate sleep duration.  The configuration is
20225591b213SSam Leffler 		 * given in ms.  We insure a multiple of the beacon
20235591b213SSam Leffler 		 * period is used.  Also, if the sleep duration is
20245591b213SSam Leffler 		 * greater than the DTIM period then it makes senses
20255591b213SSam Leffler 		 * to make it a multiple of that.
20265591b213SSam Leffler 		 *
20275591b213SSam Leffler 		 * XXX fixed at 100ms
20285591b213SSam Leffler 		 */
2029a6c992f4SSam Leffler 		bs.bs_sleepduration = roundup(MS_TO_TU(100), bs.bs_intval);
20305591b213SSam Leffler 		if (bs.bs_sleepduration > bs.bs_dtimperiod)
20315591b213SSam Leffler 			bs.bs_sleepduration = roundup(bs.bs_sleepduration, bs.bs_dtimperiod);
20325591b213SSam Leffler 
2033c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
2034c42a7b7eSSam Leffler 			"%s: intval %u nexttbtt %u dtim %u nextdtim %u bmiss %u sleep %u cfp:period %u maxdur %u next %u timoffset %u\n"
20355591b213SSam Leffler 			, __func__
20365591b213SSam Leffler 			, bs.bs_intval
20375591b213SSam Leffler 			, bs.bs_nexttbtt
20385591b213SSam Leffler 			, bs.bs_dtimperiod
20395591b213SSam Leffler 			, bs.bs_nextdtim
20405591b213SSam Leffler 			, bs.bs_bmissthreshold
20415591b213SSam Leffler 			, bs.bs_sleepduration
2042c42a7b7eSSam Leffler 			, bs.bs_cfpperiod
2043c42a7b7eSSam Leffler 			, bs.bs_cfpmaxduration
2044c42a7b7eSSam Leffler 			, bs.bs_cfpnext
2045c42a7b7eSSam Leffler 			, bs.bs_timoffset
2046c42a7b7eSSam Leffler 		);
20475591b213SSam Leffler 		ath_hal_intrset(ah, 0);
2048c42a7b7eSSam Leffler 		ath_hal_beacontimers(ah, &bs);
20495591b213SSam Leffler 		sc->sc_imask |= HAL_INT_BMISS;
20505591b213SSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
20515591b213SSam Leffler 	} else {
20525591b213SSam Leffler 		ath_hal_intrset(ah, 0);
2053a6c992f4SSam Leffler 		if (nexttbtt == intval)
2054c42a7b7eSSam Leffler 			intval |= HAL_BEACON_RESET_TSF;
2055c42a7b7eSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS) {
2056c42a7b7eSSam Leffler 			/*
2057c42a7b7eSSam Leffler 			 * In IBSS mode enable the beacon timers but only
2058c42a7b7eSSam Leffler 			 * enable SWBA interrupts if we need to manually
2059c42a7b7eSSam Leffler 			 * prepare beacon frames.  Otherwise we use a
2060c42a7b7eSSam Leffler 			 * self-linked tx descriptor and let the hardware
2061c42a7b7eSSam Leffler 			 * deal with things.
2062c42a7b7eSSam Leffler 			 */
2063c42a7b7eSSam Leffler 			intval |= HAL_BEACON_ENA;
2064c42a7b7eSSam Leffler 			if (!sc->sc_hasveol)
2065c42a7b7eSSam Leffler 				sc->sc_imask |= HAL_INT_SWBA;
2066c42a7b7eSSam Leffler 		} else if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
2067c42a7b7eSSam Leffler 			/*
2068c42a7b7eSSam Leffler 			 * In AP mode we enable the beacon timers and
2069c42a7b7eSSam Leffler 			 * SWBA interrupts to prepare beacon frames.
2070c42a7b7eSSam Leffler 			 */
2071c42a7b7eSSam Leffler 			intval |= HAL_BEACON_ENA;
20725591b213SSam Leffler 			sc->sc_imask |= HAL_INT_SWBA;	/* beacon prepare */
2073c42a7b7eSSam Leffler 		}
2074c42a7b7eSSam Leffler 		ath_hal_beaconinit(ah, nexttbtt, intval);
2075c42a7b7eSSam Leffler 		sc->sc_bmisscount = 0;
20765591b213SSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
2077c42a7b7eSSam Leffler 		/*
2078c42a7b7eSSam Leffler 		 * When using a self-linked beacon descriptor in
2079c42a7b7eSSam Leffler 		 * ibss mode load it once here.
2080c42a7b7eSSam Leffler 		 */
2081c42a7b7eSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_hasveol)
2082c42a7b7eSSam Leffler 			ath_beacon_proc(sc, 0);
20835591b213SSam Leffler 	}
2084a6c992f4SSam Leffler #undef MS_TO_TU
20855591b213SSam Leffler }
20865591b213SSam Leffler 
20875591b213SSam Leffler static void
20885591b213SSam Leffler ath_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
20895591b213SSam Leffler {
20905591b213SSam Leffler 	bus_addr_t *paddr = (bus_addr_t*) arg;
2091d77367bfSSam Leffler 	KASSERT(error == 0, ("error %u on bus_dma callback", error));
20925591b213SSam Leffler 	*paddr = segs->ds_addr;
20935591b213SSam Leffler }
20945591b213SSam Leffler 
20955591b213SSam Leffler static int
2096c42a7b7eSSam Leffler ath_descdma_setup(struct ath_softc *sc,
2097c42a7b7eSSam Leffler 	struct ath_descdma *dd, ath_bufhead *head,
2098c42a7b7eSSam Leffler 	const char *name, int nbuf, int ndesc)
2099c42a7b7eSSam Leffler {
2100c42a7b7eSSam Leffler #define	DS2PHYS(_dd, _ds) \
2101c42a7b7eSSam Leffler 	((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
2102c42a7b7eSSam Leffler 	struct ifnet *ifp = &sc->sc_if;
2103c42a7b7eSSam Leffler 	struct ath_desc *ds;
2104c42a7b7eSSam Leffler 	struct ath_buf *bf;
2105c42a7b7eSSam Leffler 	int i, bsize, error;
2106c42a7b7eSSam Leffler 
2107c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA: %u buffers %u desc/buf\n",
2108c42a7b7eSSam Leffler 	    __func__, name, nbuf, ndesc);
2109c42a7b7eSSam Leffler 
2110c42a7b7eSSam Leffler 	dd->dd_name = name;
2111c42a7b7eSSam Leffler 	dd->dd_desc_len = sizeof(struct ath_desc) * nbuf * ndesc;
2112c42a7b7eSSam Leffler 
2113c42a7b7eSSam Leffler 	/*
2114c42a7b7eSSam Leffler 	 * Setup DMA descriptor area.
2115c42a7b7eSSam Leffler 	 */
2116c42a7b7eSSam Leffler 	error = bus_dma_tag_create(NULL,	/* parent */
2117c42a7b7eSSam Leffler 		       PAGE_SIZE, 0,		/* alignment, bounds */
2118c42a7b7eSSam Leffler 		       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
2119c42a7b7eSSam Leffler 		       BUS_SPACE_MAXADDR,	/* highaddr */
2120c42a7b7eSSam Leffler 		       NULL, NULL,		/* filter, filterarg */
2121c42a7b7eSSam Leffler 		       dd->dd_desc_len,		/* maxsize */
2122c42a7b7eSSam Leffler 		       1,			/* nsegments */
2123c42a7b7eSSam Leffler 		       BUS_SPACE_MAXADDR,	/* maxsegsize */
2124c42a7b7eSSam Leffler 		       BUS_DMA_ALLOCNOW,	/* flags */
2125c42a7b7eSSam Leffler 		       NULL,			/* lockfunc */
2126c42a7b7eSSam Leffler 		       NULL,			/* lockarg */
2127c42a7b7eSSam Leffler 		       &dd->dd_dmat);
2128c42a7b7eSSam Leffler 	if (error != 0) {
2129c42a7b7eSSam Leffler 		if_printf(ifp, "cannot allocate %s DMA tag\n", dd->dd_name);
2130c42a7b7eSSam Leffler 		return error;
2131c42a7b7eSSam Leffler 	}
2132c42a7b7eSSam Leffler 
2133c42a7b7eSSam Leffler 	/* allocate descriptors */
2134c42a7b7eSSam Leffler 	error = bus_dmamap_create(dd->dd_dmat, BUS_DMA_NOWAIT, &dd->dd_dmamap);
2135c42a7b7eSSam Leffler 	if (error != 0) {
2136c42a7b7eSSam Leffler 		if_printf(ifp, "unable to create dmamap for %s descriptors, "
2137c42a7b7eSSam Leffler 			"error %u\n", dd->dd_name, error);
2138c42a7b7eSSam Leffler 		goto fail0;
2139c42a7b7eSSam Leffler 	}
2140c42a7b7eSSam Leffler 
2141c42a7b7eSSam Leffler 	error = bus_dmamem_alloc(dd->dd_dmat, (void**) &dd->dd_desc,
2142c42a7b7eSSam Leffler 				 BUS_DMA_NOWAIT, &dd->dd_dmamap);
2143c42a7b7eSSam Leffler 	if (error != 0) {
2144c42a7b7eSSam Leffler 		if_printf(ifp, "unable to alloc memory for %u %s descriptors, "
2145c42a7b7eSSam Leffler 			"error %u\n", nbuf * ndesc, dd->dd_name, error);
2146c42a7b7eSSam Leffler 		goto fail1;
2147c42a7b7eSSam Leffler 	}
2148c42a7b7eSSam Leffler 
2149c42a7b7eSSam Leffler 	error = bus_dmamap_load(dd->dd_dmat, dd->dd_dmamap,
2150c42a7b7eSSam Leffler 				dd->dd_desc, dd->dd_desc_len,
2151c42a7b7eSSam Leffler 				ath_load_cb, &dd->dd_desc_paddr,
2152c42a7b7eSSam Leffler 				BUS_DMA_NOWAIT);
2153c42a7b7eSSam Leffler 	if (error != 0) {
2154c42a7b7eSSam Leffler 		if_printf(ifp, "unable to map %s descriptors, error %u\n",
2155c42a7b7eSSam Leffler 			dd->dd_name, error);
2156c42a7b7eSSam Leffler 		goto fail2;
2157c42a7b7eSSam Leffler 	}
2158c42a7b7eSSam Leffler 
2159c42a7b7eSSam Leffler 	ds = dd->dd_desc;
2160c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA map: %p (%lu) -> %p (%lu)\n",
2161c42a7b7eSSam Leffler 	    __func__, dd->dd_name, ds, (u_long) dd->dd_desc_len,
2162c42a7b7eSSam Leffler 	    (caddr_t) dd->dd_desc_paddr, /*XXX*/ (u_long) dd->dd_desc_len);
2163c42a7b7eSSam Leffler 
2164c42a7b7eSSam Leffler 	/* allocate rx buffers */
2165c42a7b7eSSam Leffler 	bsize = sizeof(struct ath_buf) * nbuf;
2166c42a7b7eSSam Leffler 	bf = malloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO);
2167c42a7b7eSSam Leffler 	if (bf == NULL) {
2168c42a7b7eSSam Leffler 		if_printf(ifp, "malloc of %s buffers failed, size %u\n",
2169c42a7b7eSSam Leffler 			dd->dd_name, bsize);
2170c42a7b7eSSam Leffler 		goto fail3;
2171c42a7b7eSSam Leffler 	}
2172c42a7b7eSSam Leffler 	dd->dd_bufptr = bf;
2173c42a7b7eSSam Leffler 
2174c42a7b7eSSam Leffler 	STAILQ_INIT(head);
2175c42a7b7eSSam Leffler 	for (i = 0; i < nbuf; i++, bf++, ds += ndesc) {
2176c42a7b7eSSam Leffler 		bf->bf_desc = ds;
2177c42a7b7eSSam Leffler 		bf->bf_daddr = DS2PHYS(dd, ds);
2178c42a7b7eSSam Leffler 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
2179c42a7b7eSSam Leffler 				&bf->bf_dmamap);
2180c42a7b7eSSam Leffler 		if (error != 0) {
2181c42a7b7eSSam Leffler 			if_printf(ifp, "unable to create dmamap for %s "
2182c42a7b7eSSam Leffler 				"buffer %u, error %u\n", dd->dd_name, i, error);
2183c42a7b7eSSam Leffler 			ath_descdma_cleanup(sc, dd, head);
2184c42a7b7eSSam Leffler 			return error;
2185c42a7b7eSSam Leffler 		}
2186c42a7b7eSSam Leffler 		STAILQ_INSERT_TAIL(head, bf, bf_list);
2187c42a7b7eSSam Leffler 	}
2188c42a7b7eSSam Leffler 	return 0;
2189c42a7b7eSSam Leffler fail3:
2190c42a7b7eSSam Leffler 	bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
2191c42a7b7eSSam Leffler fail2:
2192c42a7b7eSSam Leffler 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
2193c42a7b7eSSam Leffler fail1:
2194c42a7b7eSSam Leffler 	bus_dmamap_destroy(dd->dd_dmat, dd->dd_dmamap);
2195c42a7b7eSSam Leffler fail0:
2196c42a7b7eSSam Leffler 	bus_dma_tag_destroy(dd->dd_dmat);
2197c42a7b7eSSam Leffler 	memset(dd, 0, sizeof(*dd));
2198c42a7b7eSSam Leffler 	return error;
2199c42a7b7eSSam Leffler #undef DS2PHYS
2200c42a7b7eSSam Leffler }
2201c42a7b7eSSam Leffler 
2202c42a7b7eSSam Leffler static void
2203c42a7b7eSSam Leffler ath_descdma_cleanup(struct ath_softc *sc,
2204c42a7b7eSSam Leffler 	struct ath_descdma *dd, ath_bufhead *head)
2205c42a7b7eSSam Leffler {
2206c42a7b7eSSam Leffler 	struct ath_buf *bf;
2207c42a7b7eSSam Leffler 	struct ieee80211_node *ni;
2208c42a7b7eSSam Leffler 
2209c42a7b7eSSam Leffler 	bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
2210c42a7b7eSSam Leffler 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
2211c42a7b7eSSam Leffler 	bus_dmamap_destroy(dd->dd_dmat, dd->dd_dmamap);
2212c42a7b7eSSam Leffler 	bus_dma_tag_destroy(dd->dd_dmat);
2213c42a7b7eSSam Leffler 
2214c42a7b7eSSam Leffler 	STAILQ_FOREACH(bf, head, bf_list) {
2215c42a7b7eSSam Leffler 		if (bf->bf_m) {
2216c42a7b7eSSam Leffler 			m_freem(bf->bf_m);
2217c42a7b7eSSam Leffler 			bf->bf_m = NULL;
2218c42a7b7eSSam Leffler 		}
2219c42a7b7eSSam Leffler 		if (bf->bf_dmamap != NULL) {
2220c42a7b7eSSam Leffler 			bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
2221c42a7b7eSSam Leffler 			bf->bf_dmamap = NULL;
2222c42a7b7eSSam Leffler 		}
2223c42a7b7eSSam Leffler 		ni = bf->bf_node;
2224c42a7b7eSSam Leffler 		bf->bf_node = NULL;
2225c42a7b7eSSam Leffler 		if (ni != NULL) {
2226c42a7b7eSSam Leffler 			/*
2227c42a7b7eSSam Leffler 			 * Reclaim node reference.
2228c42a7b7eSSam Leffler 			 */
2229c42a7b7eSSam Leffler 			ieee80211_free_node(ni);
2230c42a7b7eSSam Leffler 		}
2231c42a7b7eSSam Leffler 	}
2232c42a7b7eSSam Leffler 
2233c42a7b7eSSam Leffler 	STAILQ_INIT(head);
2234c42a7b7eSSam Leffler 	free(dd->dd_bufptr, M_ATHDEV);
2235c42a7b7eSSam Leffler 	memset(dd, 0, sizeof(*dd));
2236c42a7b7eSSam Leffler }
2237c42a7b7eSSam Leffler 
2238c42a7b7eSSam Leffler static int
22395591b213SSam Leffler ath_desc_alloc(struct ath_softc *sc)
22405591b213SSam Leffler {
2241c42a7b7eSSam Leffler 	int error;
22425591b213SSam Leffler 
2243c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_rxdma, &sc->sc_rxbuf,
2244c42a7b7eSSam Leffler 			"rx", ATH_RXBUF, 1);
22455591b213SSam Leffler 	if (error != 0)
22465591b213SSam Leffler 		return error;
22475591b213SSam Leffler 
2248c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_txdma, &sc->sc_txbuf,
2249c42a7b7eSSam Leffler 			"tx", ATH_TXBUF, ATH_TXDESC);
2250c42a7b7eSSam Leffler 	if (error != 0) {
2251c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
22525591b213SSam Leffler 		return error;
2253c42a7b7eSSam Leffler 	}
2254c42a7b7eSSam Leffler 
2255c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_bdma, &sc->sc_bbuf,
2256c42a7b7eSSam Leffler 			"beacon", 1, 1);
2257c42a7b7eSSam Leffler 	if (error != 0) {
2258c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
2259c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
2260c42a7b7eSSam Leffler 		return error;
2261c42a7b7eSSam Leffler 	}
22625591b213SSam Leffler 	return 0;
22635591b213SSam Leffler }
22645591b213SSam Leffler 
22655591b213SSam Leffler static void
22665591b213SSam Leffler ath_desc_free(struct ath_softc *sc)
22675591b213SSam Leffler {
22685591b213SSam Leffler 
2269c42a7b7eSSam Leffler 	if (sc->sc_bdma.dd_desc_len != 0)
2270c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_bdma, &sc->sc_bbuf);
2271c42a7b7eSSam Leffler 	if (sc->sc_txdma.dd_desc_len != 0)
2272c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
2273c42a7b7eSSam Leffler 	if (sc->sc_rxdma.dd_desc_len != 0)
2274c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
22755591b213SSam Leffler }
22765591b213SSam Leffler 
22775591b213SSam Leffler static struct ieee80211_node *
2278c42a7b7eSSam Leffler ath_node_alloc(struct ieee80211_node_table *nt)
22795591b213SSam Leffler {
2280c42a7b7eSSam Leffler 	struct ieee80211com *ic = nt->nt_ic;
2281c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
2282c42a7b7eSSam Leffler 	const size_t space = sizeof(struct ath_node) + sc->sc_rc->arc_space;
2283c42a7b7eSSam Leffler 	struct ath_node *an;
2284c42a7b7eSSam Leffler 
2285c42a7b7eSSam Leffler 	an = malloc(space, M_80211_NODE, M_NOWAIT|M_ZERO);
2286c42a7b7eSSam Leffler 	if (an == NULL) {
2287c42a7b7eSSam Leffler 		/* XXX stat+msg */
2288de5af704SSam Leffler 		return NULL;
22895591b213SSam Leffler 	}
2290c42a7b7eSSam Leffler 	an->an_avgrssi = ATH_RSSI_DUMMY_MARKER;
2291c42a7b7eSSam Leffler 	an->an_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
2292c42a7b7eSSam Leffler 	an->an_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
2293c42a7b7eSSam Leffler 	an->an_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
2294c42a7b7eSSam Leffler 	ath_rate_node_init(sc, an);
22955591b213SSam Leffler 
2296c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: an %p\n", __func__, an);
2297c42a7b7eSSam Leffler 	return &an->an_node;
2298c42a7b7eSSam Leffler }
2299c42a7b7eSSam Leffler 
23005591b213SSam Leffler static void
2301c42a7b7eSSam Leffler ath_node_free(struct ieee80211_node *ni)
23025591b213SSam Leffler {
2303c42a7b7eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
2304c42a7b7eSSam Leffler         struct ath_softc *sc = ic->ic_ifp->if_softc;
23051e774079SSam Leffler 
2306c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: ni %p\n", __func__, ni);
2307c42a7b7eSSam Leffler 
2308c42a7b7eSSam Leffler 	ath_rate_node_cleanup(sc, ATH_NODE(ni));
2309c42a7b7eSSam Leffler 	sc->sc_node_free(ni);
23105591b213SSam Leffler }
23115591b213SSam Leffler 
2312de5af704SSam Leffler static u_int8_t
2313c42a7b7eSSam Leffler ath_node_getrssi(const struct ieee80211_node *ni)
2314de5af704SSam Leffler {
2315c42a7b7eSSam Leffler #define	HAL_EP_RND(x, mul) \
2316c42a7b7eSSam Leffler 	((((x)%(mul)) >= ((mul)/2)) ? ((x) + ((mul) - 1)) / (mul) : (x)/(mul))
2317c42a7b7eSSam Leffler 	u_int32_t avgrssi = ATH_NODE_CONST(ni)->an_avgrssi;
2318c42a7b7eSSam Leffler 	int32_t rssi;
2319de5af704SSam Leffler 
2320de5af704SSam Leffler 	/*
2321c42a7b7eSSam Leffler 	 * When only one frame is received there will be no state in
2322c42a7b7eSSam Leffler 	 * avgrssi so fallback on the value recorded by the 802.11 layer.
2323de5af704SSam Leffler 	 */
2324c42a7b7eSSam Leffler 	if (avgrssi != ATH_RSSI_DUMMY_MARKER)
2325c42a7b7eSSam Leffler 		rssi = HAL_EP_RND(avgrssi, HAL_RSSI_EP_MULTIPLIER);
2326de5af704SSam Leffler 	else
2327c42a7b7eSSam Leffler 		rssi = ni->ni_rssi;
2328c42a7b7eSSam Leffler 	/* NB: theoretically we shouldn't need this, but be paranoid */
2329c42a7b7eSSam Leffler 	return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
2330c42a7b7eSSam Leffler #undef HAL_EP_RND
2331de5af704SSam Leffler }
2332de5af704SSam Leffler 
23335591b213SSam Leffler static int
23345591b213SSam Leffler ath_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf)
23355591b213SSam Leffler {
23365591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
23375591b213SSam Leffler 	int error;
23385591b213SSam Leffler 	struct mbuf *m;
23395591b213SSam Leffler 	struct ath_desc *ds;
23405591b213SSam Leffler 
23415591b213SSam Leffler 	m = bf->bf_m;
23425591b213SSam Leffler 	if (m == NULL) {
23435591b213SSam Leffler 		/*
23445591b213SSam Leffler 		 * NB: by assigning a page to the rx dma buffer we
23455591b213SSam Leffler 		 * implicitly satisfy the Atheros requirement that
23465591b213SSam Leffler 		 * this buffer be cache-line-aligned and sized to be
23475591b213SSam Leffler 		 * multiple of the cache line size.  Not doing this
23485591b213SSam Leffler 		 * causes weird stuff to happen (for the 5210 at least).
23495591b213SSam Leffler 		 */
23505591b213SSam Leffler 		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
23515591b213SSam Leffler 		if (m == NULL) {
2352c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
2353c42a7b7eSSam Leffler 				"%s: no mbuf/cluster\n", __func__);
23545591b213SSam Leffler 			sc->sc_stats.ast_rx_nombuf++;
23555591b213SSam Leffler 			return ENOMEM;
23565591b213SSam Leffler 		}
23575591b213SSam Leffler 		bf->bf_m = m;
23585591b213SSam Leffler 		m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
23595591b213SSam Leffler 
2360c42a7b7eSSam Leffler 		error = bus_dmamap_load_mbuf(sc->sc_dmat,
2361c42a7b7eSSam Leffler 					     bf->bf_dmamap, m,
23625591b213SSam Leffler 					     ath_mbuf_load_cb, bf,
23635591b213SSam Leffler 					     BUS_DMA_NOWAIT);
23645591b213SSam Leffler 		if (error != 0) {
2365c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
2366c42a7b7eSSam Leffler 				"%s: bus_dmamap_load_mbuf failed; error %d\n",
2367c42a7b7eSSam Leffler 				__func__, error);
23685591b213SSam Leffler 			sc->sc_stats.ast_rx_busdma++;
23695591b213SSam Leffler 			return error;
23705591b213SSam Leffler 		}
2371d77367bfSSam Leffler 		KASSERT(bf->bf_nseg == 1,
2372d77367bfSSam Leffler 			("multi-segment packet; nseg %u", bf->bf_nseg));
23735591b213SSam Leffler 	}
23745591b213SSam Leffler 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREREAD);
23755591b213SSam Leffler 
237604e22a02SSam Leffler 	/*
237704e22a02SSam Leffler 	 * Setup descriptors.  For receive we always terminate
237804e22a02SSam Leffler 	 * the descriptor list with a self-linked entry so we'll
237904e22a02SSam Leffler 	 * not get overrun under high load (as can happen with a
2380c42a7b7eSSam Leffler 	 * 5212 when ANI processing enables PHY error frames).
238104e22a02SSam Leffler 	 *
238204e22a02SSam Leffler 	 * To insure the last descriptor is self-linked we create
238304e22a02SSam Leffler 	 * each descriptor as self-linked and add it to the end.  As
238404e22a02SSam Leffler 	 * each additional descriptor is added the previous self-linked
238504e22a02SSam Leffler 	 * entry is ``fixed'' naturally.  This should be safe even
238604e22a02SSam Leffler 	 * if DMA is happening.  When processing RX interrupts we
238704e22a02SSam Leffler 	 * never remove/process the last, self-linked, entry on the
238804e22a02SSam Leffler 	 * descriptor list.  This insures the hardware always has
238904e22a02SSam Leffler 	 * someplace to write a new frame.
239004e22a02SSam Leffler 	 */
23915591b213SSam Leffler 	ds = bf->bf_desc;
239204e22a02SSam Leffler 	ds->ds_link = bf->bf_daddr;	/* link to self */
23935591b213SSam Leffler 	ds->ds_data = bf->bf_segs[0].ds_addr;
23945591b213SSam Leffler 	ath_hal_setuprxdesc(ah, ds
23955591b213SSam Leffler 		, m->m_len		/* buffer size */
23965591b213SSam Leffler 		, 0
23975591b213SSam Leffler 	);
23985591b213SSam Leffler 
23995591b213SSam Leffler 	if (sc->sc_rxlink != NULL)
24005591b213SSam Leffler 		*sc->sc_rxlink = bf->bf_daddr;
24015591b213SSam Leffler 	sc->sc_rxlink = &ds->ds_link;
24025591b213SSam Leffler 	return 0;
24035591b213SSam Leffler }
24045591b213SSam Leffler 
2405c42a7b7eSSam Leffler /*
2406c42a7b7eSSam Leffler  * Intercept management frames to collect beacon rssi data
2407c42a7b7eSSam Leffler  * and to do ibss merges.
2408c42a7b7eSSam Leffler  */
2409c42a7b7eSSam Leffler static void
2410c42a7b7eSSam Leffler ath_recv_mgmt(struct ieee80211com *ic, struct mbuf *m,
2411c42a7b7eSSam Leffler 	struct ieee80211_node *ni,
2412c42a7b7eSSam Leffler 	int subtype, int rssi, u_int32_t rstamp)
2413c42a7b7eSSam Leffler {
2414c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
2415c42a7b7eSSam Leffler 
2416c42a7b7eSSam Leffler 	/*
2417c42a7b7eSSam Leffler 	 * Call up first so subsequent work can use information
2418c42a7b7eSSam Leffler 	 * potentially stored in the node (e.g. for ibss merge).
2419c42a7b7eSSam Leffler 	 */
2420c42a7b7eSSam Leffler 	sc->sc_recv_mgmt(ic, m, ni, subtype, rssi, rstamp);
2421c42a7b7eSSam Leffler 	switch (subtype) {
2422c42a7b7eSSam Leffler 	case IEEE80211_FC0_SUBTYPE_BEACON:
2423c42a7b7eSSam Leffler 		/* update rssi statistics for use by the hal */
2424c42a7b7eSSam Leffler 		ATH_RSSI_LPF(ATH_NODE(ni)->an_halstats.ns_avgbrssi, rssi);
2425c42a7b7eSSam Leffler 		/* fall thru... */
2426c42a7b7eSSam Leffler 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
2427c42a7b7eSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_IBSS &&
2428c42a7b7eSSam Leffler 		    ic->ic_state == IEEE80211_S_RUN) {
2429c42a7b7eSSam Leffler 			struct ath_hal *ah = sc->sc_ah;
2430c42a7b7eSSam Leffler 			/* XXX extend rstamp */
2431c42a7b7eSSam Leffler 			u_int64_t tsf = ath_hal_gettsf64(ah);
2432c42a7b7eSSam Leffler 
2433c42a7b7eSSam Leffler 			/*
2434c42a7b7eSSam Leffler 			 * Handle ibss merge as needed; check the tsf on the
2435c42a7b7eSSam Leffler 			 * frame before attempting the merge.  The 802.11 spec
2436c42a7b7eSSam Leffler 			 * says the station should change it's bssid to match
2437c42a7b7eSSam Leffler 			 * the oldest station with the same ssid, where oldest
2438c42a7b7eSSam Leffler 			 * is determined by the tsf.
2439c42a7b7eSSam Leffler 			 */
2440c42a7b7eSSam Leffler 			if (le64toh(ni->ni_tstamp.tsf) >= tsf &&
2441c42a7b7eSSam Leffler 			    ieee80211_ibss_merge(ic, ni))
2442c42a7b7eSSam Leffler 				ath_hal_setassocid(ah, ic->ic_bss->ni_bssid, 0);
2443c42a7b7eSSam Leffler 		}
2444c42a7b7eSSam Leffler 		break;
2445c42a7b7eSSam Leffler 	}
2446c42a7b7eSSam Leffler }
2447c42a7b7eSSam Leffler 
2448c42a7b7eSSam Leffler /*
2449c42a7b7eSSam Leffler  * Set the default antenna.
2450c42a7b7eSSam Leffler  */
2451c42a7b7eSSam Leffler static void
2452c42a7b7eSSam Leffler ath_setdefantenna(struct ath_softc *sc, u_int antenna)
2453c42a7b7eSSam Leffler {
2454c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2455c42a7b7eSSam Leffler 
2456c42a7b7eSSam Leffler 	/* XXX block beacon interrupts */
2457c42a7b7eSSam Leffler 	ath_hal_setdefantenna(ah, antenna);
2458c42a7b7eSSam Leffler 	if (sc->sc_defant != antenna)
2459c42a7b7eSSam Leffler 		sc->sc_stats.ast_ant_defswitch++;
2460c42a7b7eSSam Leffler 	sc->sc_defant = antenna;
2461c42a7b7eSSam Leffler 	sc->sc_rxotherant = 0;
2462c42a7b7eSSam Leffler }
2463c42a7b7eSSam Leffler 
24645591b213SSam Leffler static void
24655591b213SSam Leffler ath_rx_proc(void *arg, int npending)
24665591b213SSam Leffler {
24678cec0ab9SSam Leffler #define	PA2DESC(_sc, _pa) \
2468c42a7b7eSSam Leffler 	((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
2469c42a7b7eSSam Leffler 		((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
24705591b213SSam Leffler 	struct ath_softc *sc = arg;
24715591b213SSam Leffler 	struct ath_buf *bf;
2472d1d0cf62SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
2473c42a7b7eSSam Leffler 	struct ifnet *ifp = &sc->sc_if;
24745591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
24755591b213SSam Leffler 	struct ath_desc *ds;
24765591b213SSam Leffler 	struct mbuf *m;
24770a915fadSSam Leffler 	struct ieee80211_node *ni;
2478de5af704SSam Leffler 	struct ath_node *an;
24795591b213SSam Leffler 	int len;
24805591b213SSam Leffler 	u_int phyerr;
24815591b213SSam Leffler 	HAL_STATUS status;
24825591b213SSam Leffler 
2483b5f4adb3SSam Leffler 	NET_LOCK_GIANT();		/* XXX */
2484b5f4adb3SSam Leffler 
2485c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RX_PROC, "%s: pending %u\n", __func__, npending);
24865591b213SSam Leffler 	do {
2487c42a7b7eSSam Leffler 		bf = STAILQ_FIRST(&sc->sc_rxbuf);
24885591b213SSam Leffler 		if (bf == NULL) {		/* NB: shouldn't happen */
2489c42a7b7eSSam Leffler 			if_printf(ifp, "%s: no buffer!\n", __func__);
24905591b213SSam Leffler 			break;
24915591b213SSam Leffler 		}
249204e22a02SSam Leffler 		ds = bf->bf_desc;
249304e22a02SSam Leffler 		if (ds->ds_link == bf->bf_daddr) {
249404e22a02SSam Leffler 			/* NB: never process the self-linked entry at the end */
249504e22a02SSam Leffler 			break;
249604e22a02SSam Leffler 		}
24975591b213SSam Leffler 		m = bf->bf_m;
24985591b213SSam Leffler 		if (m == NULL) {		/* NB: shouldn't happen */
2499c42a7b7eSSam Leffler 			if_printf(ifp, "%s: no mbuf!\n", __func__);
25005591b213SSam Leffler 			continue;
25015591b213SSam Leffler 		}
25028cec0ab9SSam Leffler 		/* XXX sync descriptor memory */
25038cec0ab9SSam Leffler 		/*
25048cec0ab9SSam Leffler 		 * Must provide the virtual address of the current
25058cec0ab9SSam Leffler 		 * descriptor, the physical address, and the virtual
25068cec0ab9SSam Leffler 		 * address of the next descriptor in the h/w chain.
25078cec0ab9SSam Leffler 		 * This allows the HAL to look ahead to see if the
25088cec0ab9SSam Leffler 		 * hardware is done with a descriptor by checking the
25098cec0ab9SSam Leffler 		 * done bit in the following descriptor and the address
25108cec0ab9SSam Leffler 		 * of the current descriptor the DMA engine is working
25118cec0ab9SSam Leffler 		 * on.  All this is necessary because of our use of
25128cec0ab9SSam Leffler 		 * a self-linked list to avoid rx overruns.
25138cec0ab9SSam Leffler 		 */
25148cec0ab9SSam Leffler 		status = ath_hal_rxprocdesc(ah, ds,
25158cec0ab9SSam Leffler 				bf->bf_daddr, PA2DESC(sc, ds->ds_link));
25165591b213SSam Leffler #ifdef AR_DEBUG
2517c42a7b7eSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_RECV_DESC)
25185591b213SSam Leffler 			ath_printrxbuf(bf, status == HAL_OK);
25195591b213SSam Leffler #endif
25205591b213SSam Leffler 		if (status == HAL_EINPROGRESS)
25215591b213SSam Leffler 			break;
2522c42a7b7eSSam Leffler 		STAILQ_REMOVE_HEAD(&sc->sc_rxbuf, bf_list);
2523c42a7b7eSSam Leffler 		if (ds->ds_rxstat.rs_more) {
2524c42a7b7eSSam Leffler 			/*
2525c42a7b7eSSam Leffler 			 * Frame spans multiple descriptors; this
2526c42a7b7eSSam Leffler 			 * cannot happen yet as we don't support
2527c42a7b7eSSam Leffler 			 * jumbograms.  If not in monitor mode,
2528c42a7b7eSSam Leffler 			 * discard the frame.
2529c42a7b7eSSam Leffler 			 */
2530c42a7b7eSSam Leffler 			if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2531c42a7b7eSSam Leffler 				sc->sc_stats.ast_rx_toobig++;
2532c42a7b7eSSam Leffler 				goto rx_next;
2533c42a7b7eSSam Leffler 			}
2534c42a7b7eSSam Leffler 			/* fall thru for monitor mode handling... */
2535c42a7b7eSSam Leffler 		} else if (ds->ds_rxstat.rs_status != 0) {
25365591b213SSam Leffler 			if (ds->ds_rxstat.rs_status & HAL_RXERR_CRC)
25375591b213SSam Leffler 				sc->sc_stats.ast_rx_crcerr++;
25385591b213SSam Leffler 			if (ds->ds_rxstat.rs_status & HAL_RXERR_FIFO)
25395591b213SSam Leffler 				sc->sc_stats.ast_rx_fifoerr++;
25405591b213SSam Leffler 			if (ds->ds_rxstat.rs_status & HAL_RXERR_PHY) {
25415591b213SSam Leffler 				sc->sc_stats.ast_rx_phyerr++;
25425591b213SSam Leffler 				phyerr = ds->ds_rxstat.rs_phyerr & 0x1f;
25435591b213SSam Leffler 				sc->sc_stats.ast_rx_phy[phyerr]++;
2544c42a7b7eSSam Leffler 				goto rx_next;
2545c42a7b7eSSam Leffler 			}
2546c42a7b7eSSam Leffler 			if (ds->ds_rxstat.rs_status & HAL_RXERR_DECRYPT) {
254785643802SSam Leffler 				/*
2548c42a7b7eSSam Leffler 				 * Decrypt error.  If the error occurred
2549c42a7b7eSSam Leffler 				 * because there was no hardware key, then
2550c42a7b7eSSam Leffler 				 * let the frame through so the upper layers
2551c42a7b7eSSam Leffler 				 * can process it.  This is necessary for 5210
2552c42a7b7eSSam Leffler 				 * parts which have no way to setup a ``clear''
2553c42a7b7eSSam Leffler 				 * key cache entry.
2554c42a7b7eSSam Leffler 				 *
2555c42a7b7eSSam Leffler 				 * XXX do key cache faulting
255685643802SSam Leffler 				 */
2557c42a7b7eSSam Leffler 				if (ds->ds_rxstat.rs_keyix == HAL_RXKEYIX_INVALID)
2558c42a7b7eSSam Leffler 					goto rx_accept;
2559c42a7b7eSSam Leffler 				sc->sc_stats.ast_rx_badcrypt++;
25605591b213SSam Leffler 			}
2561c42a7b7eSSam Leffler 			if (ds->ds_rxstat.rs_status & HAL_RXERR_MIC) {
2562c42a7b7eSSam Leffler 				sc->sc_stats.ast_rx_badmic++;
2563c42a7b7eSSam Leffler 				/*
2564c42a7b7eSSam Leffler 				 * Do minimal work required to hand off
2565c42a7b7eSSam Leffler 				 * the 802.11 header for notifcation.
2566c42a7b7eSSam Leffler 				 */
2567c42a7b7eSSam Leffler 				/* XXX frag's and qos frames */
25685591b213SSam Leffler 				len = ds->ds_rxstat.rs_datalen;
2569c42a7b7eSSam Leffler 				if (len >= sizeof (struct ieee80211_frame)) {
2570c42a7b7eSSam Leffler 					bus_dmamap_sync(sc->sc_dmat,
2571c42a7b7eSSam Leffler 					    bf->bf_dmamap,
2572c42a7b7eSSam Leffler 					    BUS_DMASYNC_POSTREAD);
2573c42a7b7eSSam Leffler 					ieee80211_notify_michael_failure(ic,
2574c42a7b7eSSam Leffler 					    mtod(m, struct ieee80211_frame *),
25750ab4040aSSam Leffler 					    sc->sc_splitmic ?
25760ab4040aSSam Leffler 					        ds->ds_rxstat.rs_keyix-32 :
25770ab4040aSSam Leffler 					        ds->ds_rxstat.rs_keyix
25780ab4040aSSam Leffler 					);
2579c42a7b7eSSam Leffler 				}
2580c42a7b7eSSam Leffler 			}
2581c42a7b7eSSam Leffler 			ifp->if_ierrors++;
2582c42a7b7eSSam Leffler 			/*
2583c42a7b7eSSam Leffler 			 * Reject error frames, we normally don't want
2584c42a7b7eSSam Leffler 			 * to see them in monitor mode (in monitor mode
2585c42a7b7eSSam Leffler 			 * allow through packets that have crypto problems).
2586c42a7b7eSSam Leffler 			 */
2587c42a7b7eSSam Leffler 			if ((ds->ds_rxstat.rs_status &~
2588c42a7b7eSSam Leffler 				(HAL_RXERR_DECRYPT|HAL_RXERR_MIC)) ||
2589c42a7b7eSSam Leffler 			    sc->sc_ic.ic_opmode != IEEE80211_M_MONITOR)
25905591b213SSam Leffler 				goto rx_next;
25915591b213SSam Leffler 		}
2592c42a7b7eSSam Leffler rx_accept:
2593c42a7b7eSSam Leffler 		/*
2594c42a7b7eSSam Leffler 		 * Sync and unmap the frame.  At this point we're
2595c42a7b7eSSam Leffler 		 * committed to passing the mbuf somewhere so clear
2596c42a7b7eSSam Leffler 		 * bf_m; this means a new sk_buff must be allocated
2597c42a7b7eSSam Leffler 		 * when the rx descriptor is setup again to receive
2598c42a7b7eSSam Leffler 		 * another frame.
2599c42a7b7eSSam Leffler 		 */
26005591b213SSam Leffler 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
26015591b213SSam Leffler 		    BUS_DMASYNC_POSTREAD);
26025591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
26035591b213SSam Leffler 		bf->bf_m = NULL;
2604c42a7b7eSSam Leffler 
26055591b213SSam Leffler 		m->m_pkthdr.rcvif = ifp;
2606c42a7b7eSSam Leffler 		len = ds->ds_rxstat.rs_datalen;
26075591b213SSam Leffler 		m->m_pkthdr.len = m->m_len = len;
260873454c73SSam Leffler 
2609c42a7b7eSSam Leffler 		if (sc->sc_softled)
2610c42a7b7eSSam Leffler 			ath_update_led(sc);
2611c42a7b7eSSam Leffler 		sc->sc_stats.ast_ant_rx[ds->ds_rxstat.rs_antenna]++;
2612c42a7b7eSSam Leffler 
261373454c73SSam Leffler 		if (sc->sc_drvbpf) {
261416b4851aSSam Leffler 			const void *data;
261516b4851aSSam Leffler 			int hdrsize, hdrspace;
261616b4851aSSam Leffler 			u_int8_t rix;
261716b4851aSSam Leffler 
2618c42a7b7eSSam Leffler 			/*
2619c42a7b7eSSam Leffler 			 * Discard anything shorter than an ack or cts.
2620c42a7b7eSSam Leffler 			 */
2621c42a7b7eSSam Leffler 			if (len < IEEE80211_ACK_LEN) {
2622c42a7b7eSSam Leffler 				DPRINTF(sc, ATH_DEBUG_RECV,
2623c42a7b7eSSam Leffler 					"%s: runt packet %d\n",
2624c42a7b7eSSam Leffler 					__func__, len);
2625c42a7b7eSSam Leffler 				sc->sc_stats.ast_rx_tooshort++;
2626c42a7b7eSSam Leffler 				m_freem(m);
2627c42a7b7eSSam Leffler 				goto rx_next;
2628c42a7b7eSSam Leffler 			}
262916b4851aSSam Leffler 			rix = ds->ds_rxstat.rs_rate;
263016b4851aSSam Leffler 			sc->sc_rx_th.wr_flags = sc->sc_hwflags[rix];
263116b4851aSSam Leffler 			sc->sc_rx_th.wr_rate = sc->sc_hwmap[rix];
2632437ffe18SSam Leffler 			sc->sc_rx_th.wr_antsignal = ds->ds_rxstat.rs_rssi;
2633437ffe18SSam Leffler 			sc->sc_rx_th.wr_antenna = ds->ds_rxstat.rs_antenna;
263473454c73SSam Leffler 			/* XXX TSF */
263573454c73SSam Leffler 
263616b4851aSSam Leffler 			/*
263716b4851aSSam Leffler 			 * Gag, deal with hardware padding of headers. This
263816b4851aSSam Leffler 			 * only happens for QoS frames.  We copy the 802.11
263916b4851aSSam Leffler 			 * header out-of-line and supply it separately, then
264016b4851aSSam Leffler 			 * adjust the mbuf chain.  It would be better if we
264116b4851aSSam Leffler 			 * could just flag the packet in the radiotap header
264216b4851aSSam Leffler 			 * and have applications DTRT.
264316b4851aSSam Leffler 			 */
264416b4851aSSam Leffler 			if (len > sizeof(struct ieee80211_qosframe)) {
264516b4851aSSam Leffler 				data = mtod(m, const void *);
264616b4851aSSam Leffler 				hdrsize = ieee80211_anyhdrsize(data);
264716b4851aSSam Leffler 				if (hdrsize & 3) {
264816b4851aSSam Leffler 					bcopy(data, &sc->sc_rx_wh, hdrsize);
264916b4851aSSam Leffler 					hdrspace = roundup(hdrsize,
265016b4851aSSam Leffler 						sizeof(u_int32_t));
265116b4851aSSam Leffler 					m->m_data += hdrspace;
265216b4851aSSam Leffler 					m->m_len -= hdrspace;
265316b4851aSSam Leffler 					bpf_mtap2(sc->sc_drvbpf, &sc->sc_rx,
265416b4851aSSam Leffler 						sc->sc_rx_rt_len + hdrsize, m);
265516b4851aSSam Leffler 					m->m_data -= hdrspace;
265616b4851aSSam Leffler 					m->m_len += hdrspace;
265716b4851aSSam Leffler 				} else
2658437ffe18SSam Leffler 					bpf_mtap2(sc->sc_drvbpf,
265916b4851aSSam Leffler 					     &sc->sc_rx, sc->sc_rx_rt_len, m);
266016b4851aSSam Leffler 			} else
266116b4851aSSam Leffler 				bpf_mtap2(sc->sc_drvbpf,
266216b4851aSSam Leffler 				    &sc->sc_rx, sc->sc_rx_rt_len, m);
26635591b213SSam Leffler 		}
26640a915fadSSam Leffler 
26655591b213SSam Leffler 		/*
2666c42a7b7eSSam Leffler 		 * From this point on we assume the frame is at least
2667c42a7b7eSSam Leffler 		 * as large as ieee80211_frame_min; verify that.
26685591b213SSam Leffler 		 */
2669c42a7b7eSSam Leffler 		if (len < IEEE80211_MIN_LEN) {
2670c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_RECV, "%s: short packet %d\n",
2671c42a7b7eSSam Leffler 				__func__, len);
2672c42a7b7eSSam Leffler 			sc->sc_stats.ast_rx_tooshort++;
2673c42a7b7eSSam Leffler 			m_freem(m);
2674c42a7b7eSSam Leffler 			goto rx_next;
26755591b213SSam Leffler 		}
26760a915fadSSam Leffler 
2677c42a7b7eSSam Leffler 		if (IFF_DUMPPKTS(sc, ATH_DEBUG_RECV)) {
2678c42a7b7eSSam Leffler 			ieee80211_dump_pkt(mtod(m, caddr_t), len,
2679c42a7b7eSSam Leffler 				   sc->sc_hwmap[ds->ds_rxstat.rs_rate],
2680c42a7b7eSSam Leffler 				   ds->ds_rxstat.rs_rssi);
2681c42a7b7eSSam Leffler 		}
2682c42a7b7eSSam Leffler 
2683c42a7b7eSSam Leffler 		m_adj(m, -IEEE80211_CRC_LEN);
2684de5af704SSam Leffler 
2685de5af704SSam Leffler 		/*
2686c42a7b7eSSam Leffler 		 * Locate the node for sender, track state, and then
2687c42a7b7eSSam Leffler 		 * pass the (referenced) node up to the 802.11 layer
2688c42a7b7eSSam Leffler 		 * for its use.
2689c42a7b7eSSam Leffler 		 */
2690c42a7b7eSSam Leffler 		ni = ieee80211_find_rxnode(ic,
2691c42a7b7eSSam Leffler 			mtod(m, const struct ieee80211_frame_min *));
2692c42a7b7eSSam Leffler 
2693c42a7b7eSSam Leffler 		/*
2694c42a7b7eSSam Leffler 		 * Track rx rssi and do any rx antenna management.
2695de5af704SSam Leffler 		 */
2696de5af704SSam Leffler 		an = ATH_NODE(ni);
2697c42a7b7eSSam Leffler 		ATH_RSSI_LPF(an->an_avgrssi, ds->ds_rxstat.rs_rssi);
2698c42a7b7eSSam Leffler 		if (sc->sc_diversity) {
2699c42a7b7eSSam Leffler 			/*
2700c42a7b7eSSam Leffler 			 * When using fast diversity, change the default rx
2701c42a7b7eSSam Leffler 			 * antenna if diversity chooses the other antenna 3
2702c42a7b7eSSam Leffler 			 * times in a row.
2703c42a7b7eSSam Leffler 			 */
2704c42a7b7eSSam Leffler 			if (sc->sc_defant != ds->ds_rxstat.rs_antenna) {
2705c42a7b7eSSam Leffler 				if (++sc->sc_rxotherant >= 3)
2706c42a7b7eSSam Leffler 					ath_setdefantenna(sc,
2707c42a7b7eSSam Leffler 						ds->ds_rxstat.rs_antenna);
2708c42a7b7eSSam Leffler 			} else
2709c42a7b7eSSam Leffler 				sc->sc_rxotherant = 0;
2710c42a7b7eSSam Leffler 		}
2711de5af704SSam Leffler 
27120a915fadSSam Leffler 		/*
27130a915fadSSam Leffler 		 * Send frame up for processing.
27140a915fadSSam Leffler 		 */
2715c42a7b7eSSam Leffler 		ieee80211_input(ic, m, ni,
27160a915fadSSam Leffler 			ds->ds_rxstat.rs_rssi, ds->ds_rxstat.rs_tstamp);
2717de5af704SSam Leffler 
27180a915fadSSam Leffler 		/*
2719c42a7b7eSSam Leffler 		 * Reclaim node reference.
27200a915fadSSam Leffler 		 */
2721c42a7b7eSSam Leffler 		ieee80211_free_node(ni);
27225591b213SSam Leffler rx_next:
2723c42a7b7eSSam Leffler 		STAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
27245591b213SSam Leffler 	} while (ath_rxbuf_init(sc, bf) == 0);
27255591b213SSam Leffler 
2726c42a7b7eSSam Leffler 	/* rx signal state monitoring */
2727c42a7b7eSSam Leffler 	ath_hal_rxmonitor(ah, &ATH_NODE(ic->ic_bss)->an_halstats);
2728b5f4adb3SSam Leffler 
2729b5f4adb3SSam Leffler 	NET_UNLOCK_GIANT();		/* XXX */
27308cec0ab9SSam Leffler #undef PA2DESC
27315591b213SSam Leffler }
27325591b213SSam Leffler 
27335591b213SSam Leffler /*
2734c42a7b7eSSam Leffler  * Setup a h/w transmit queue.
27355591b213SSam Leffler  */
2736c42a7b7eSSam Leffler static struct ath_txq *
2737c42a7b7eSSam Leffler ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
2738c42a7b7eSSam Leffler {
2739c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
2740c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2741c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
2742c42a7b7eSSam Leffler 	int qnum;
2743c42a7b7eSSam Leffler 
2744c42a7b7eSSam Leffler 	memset(&qi, 0, sizeof(qi));
2745c42a7b7eSSam Leffler 	qi.tqi_subtype = subtype;
2746c42a7b7eSSam Leffler 	qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
2747c42a7b7eSSam Leffler 	qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
2748c42a7b7eSSam Leffler 	qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
2749c42a7b7eSSam Leffler 	/*
2750c42a7b7eSSam Leffler 	 * Enable interrupts only for EOL and DESC conditions.
2751c42a7b7eSSam Leffler 	 * We mark tx descriptors to receive a DESC interrupt
2752c42a7b7eSSam Leffler 	 * when a tx queue gets deep; otherwise waiting for the
2753c42a7b7eSSam Leffler 	 * EOL to reap descriptors.  Note that this is done to
2754c42a7b7eSSam Leffler 	 * reduce interrupt load and this only defers reaping
2755c42a7b7eSSam Leffler 	 * descriptors, never transmitting frames.  Aside from
2756c42a7b7eSSam Leffler 	 * reducing interrupts this also permits more concurrency.
2757c42a7b7eSSam Leffler 	 * The only potential downside is if the tx queue backs
2758c42a7b7eSSam Leffler 	 * up in which case the top half of the kernel may backup
2759c42a7b7eSSam Leffler 	 * due to a lack of tx descriptors.
2760c42a7b7eSSam Leffler 	 */
2761c42a7b7eSSam Leffler 	qi.tqi_qflags = TXQ_FLAG_TXEOLINT_ENABLE | TXQ_FLAG_TXDESCINT_ENABLE;
2762c42a7b7eSSam Leffler 	qnum = ath_hal_setuptxqueue(ah, qtype, &qi);
2763c42a7b7eSSam Leffler 	if (qnum == -1) {
2764c42a7b7eSSam Leffler 		/*
2765c42a7b7eSSam Leffler 		 * NB: don't print a message, this happens
2766a614e076SSam Leffler 		 * normally on parts with too few tx queues
2767c42a7b7eSSam Leffler 		 */
2768c42a7b7eSSam Leffler 		return NULL;
2769c42a7b7eSSam Leffler 	}
2770c42a7b7eSSam Leffler 	if (qnum >= N(sc->sc_txq)) {
27716891c875SPeter Wemm 		device_printf(sc->sc_dev,
27726891c875SPeter Wemm 			"hal qnum %u out of range, max %zu!\n",
2773c42a7b7eSSam Leffler 			qnum, N(sc->sc_txq));
2774c42a7b7eSSam Leffler 		ath_hal_releasetxqueue(ah, qnum);
2775c42a7b7eSSam Leffler 		return NULL;
2776c42a7b7eSSam Leffler 	}
2777c42a7b7eSSam Leffler 	if (!ATH_TXQ_SETUP(sc, qnum)) {
2778c42a7b7eSSam Leffler 		struct ath_txq *txq = &sc->sc_txq[qnum];
2779c42a7b7eSSam Leffler 
2780c42a7b7eSSam Leffler 		txq->axq_qnum = qnum;
2781c42a7b7eSSam Leffler 		txq->axq_depth = 0;
2782c42a7b7eSSam Leffler 		txq->axq_intrcnt = 0;
2783c42a7b7eSSam Leffler 		txq->axq_link = NULL;
2784c42a7b7eSSam Leffler 		STAILQ_INIT(&txq->axq_q);
2785c42a7b7eSSam Leffler 		ATH_TXQ_LOCK_INIT(sc, txq);
2786c42a7b7eSSam Leffler 		sc->sc_txqsetup |= 1<<qnum;
2787c42a7b7eSSam Leffler 	}
2788c42a7b7eSSam Leffler 	return &sc->sc_txq[qnum];
2789c42a7b7eSSam Leffler #undef N
2790c42a7b7eSSam Leffler }
2791c42a7b7eSSam Leffler 
2792c42a7b7eSSam Leffler /*
2793c42a7b7eSSam Leffler  * Setup a hardware data transmit queue for the specified
2794c42a7b7eSSam Leffler  * access control.  The hal may not support all requested
2795c42a7b7eSSam Leffler  * queues in which case it will return a reference to a
2796c42a7b7eSSam Leffler  * previously setup queue.  We record the mapping from ac's
2797c42a7b7eSSam Leffler  * to h/w queues for use by ath_tx_start and also track
2798c42a7b7eSSam Leffler  * the set of h/w queues being used to optimize work in the
2799c42a7b7eSSam Leffler  * transmit interrupt handler and related routines.
2800c42a7b7eSSam Leffler  */
2801c42a7b7eSSam Leffler static int
2802c42a7b7eSSam Leffler ath_tx_setup(struct ath_softc *sc, int ac, int haltype)
2803c42a7b7eSSam Leffler {
2804c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
2805c42a7b7eSSam Leffler 	struct ath_txq *txq;
2806c42a7b7eSSam Leffler 
2807c42a7b7eSSam Leffler 	if (ac >= N(sc->sc_ac2q)) {
28086891c875SPeter Wemm 		device_printf(sc->sc_dev, "AC %u out of range, max %zu!\n",
2809c42a7b7eSSam Leffler 			ac, N(sc->sc_ac2q));
2810c42a7b7eSSam Leffler 		return 0;
2811c42a7b7eSSam Leffler 	}
2812c42a7b7eSSam Leffler 	txq = ath_txq_setup(sc, HAL_TX_QUEUE_DATA, haltype);
2813c42a7b7eSSam Leffler 	if (txq != NULL) {
2814c42a7b7eSSam Leffler 		sc->sc_ac2q[ac] = txq;
2815c42a7b7eSSam Leffler 		return 1;
2816c42a7b7eSSam Leffler 	} else
2817c42a7b7eSSam Leffler 		return 0;
2818c42a7b7eSSam Leffler #undef N
2819c42a7b7eSSam Leffler }
2820c42a7b7eSSam Leffler 
2821c42a7b7eSSam Leffler /*
2822c42a7b7eSSam Leffler  * Update WME parameters for a transmit queue.
2823c42a7b7eSSam Leffler  */
2824c42a7b7eSSam Leffler static int
2825c42a7b7eSSam Leffler ath_txq_update(struct ath_softc *sc, int ac)
2826c42a7b7eSSam Leffler {
2827c42a7b7eSSam Leffler #define	ATH_EXPONENT_TO_VALUE(v)	((1<<v)-1)
2828c42a7b7eSSam Leffler #define	ATH_TXOP_TO_US(v)		(v<<5)
2829c42a7b7eSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
2830c42a7b7eSSam Leffler 	struct ath_txq *txq = sc->sc_ac2q[ac];
2831c42a7b7eSSam Leffler 	struct wmeParams *wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
2832c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2833c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
2834c42a7b7eSSam Leffler 
2835c42a7b7eSSam Leffler 	ath_hal_gettxqueueprops(ah, txq->axq_qnum, &qi);
2836c42a7b7eSSam Leffler 	qi.tqi_aifs = wmep->wmep_aifsn;
2837c42a7b7eSSam Leffler 	qi.tqi_cwmin = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
2838c42a7b7eSSam Leffler 	qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
2839c42a7b7eSSam Leffler 	qi.tqi_burstTime = ATH_TXOP_TO_US(wmep->wmep_txopLimit);
2840c42a7b7eSSam Leffler 
2841c42a7b7eSSam Leffler 	if (!ath_hal_settxqueueprops(ah, txq->axq_qnum, &qi)) {
2842c42a7b7eSSam Leffler 		device_printf(sc->sc_dev, "unable to update hardware queue "
2843c42a7b7eSSam Leffler 			"parameters for %s traffic!\n",
2844c42a7b7eSSam Leffler 			ieee80211_wme_acnames[ac]);
2845c42a7b7eSSam Leffler 		return 0;
2846c42a7b7eSSam Leffler 	} else {
2847c42a7b7eSSam Leffler 		ath_hal_resettxqueue(ah, txq->axq_qnum); /* push to h/w */
2848c42a7b7eSSam Leffler 		return 1;
2849c42a7b7eSSam Leffler 	}
2850c42a7b7eSSam Leffler #undef ATH_TXOP_TO_US
2851c42a7b7eSSam Leffler #undef ATH_EXPONENT_TO_VALUE
2852c42a7b7eSSam Leffler }
2853c42a7b7eSSam Leffler 
2854c42a7b7eSSam Leffler /*
2855c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update WME parameters.
2856c42a7b7eSSam Leffler  */
2857c42a7b7eSSam Leffler static int
2858c42a7b7eSSam Leffler ath_wme_update(struct ieee80211com *ic)
2859c42a7b7eSSam Leffler {
2860c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
2861c42a7b7eSSam Leffler 
2862c42a7b7eSSam Leffler 	return !ath_txq_update(sc, WME_AC_BE) ||
2863c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_BK) ||
2864c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VI) ||
2865c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VO) ? EIO : 0;
2866c42a7b7eSSam Leffler }
2867c42a7b7eSSam Leffler 
2868c42a7b7eSSam Leffler /*
2869c42a7b7eSSam Leffler  * Reclaim resources for a setup queue.
2870c42a7b7eSSam Leffler  */
2871c42a7b7eSSam Leffler static void
2872c42a7b7eSSam Leffler ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
2873c42a7b7eSSam Leffler {
2874c42a7b7eSSam Leffler 
2875c42a7b7eSSam Leffler 	ath_hal_releasetxqueue(sc->sc_ah, txq->axq_qnum);
2876c42a7b7eSSam Leffler 	ATH_TXQ_LOCK_DESTROY(txq);
2877c42a7b7eSSam Leffler 	sc->sc_txqsetup &= ~(1<<txq->axq_qnum);
2878c42a7b7eSSam Leffler }
2879c42a7b7eSSam Leffler 
2880c42a7b7eSSam Leffler /*
2881c42a7b7eSSam Leffler  * Reclaim all tx queue resources.
2882c42a7b7eSSam Leffler  */
2883c42a7b7eSSam Leffler static void
2884c42a7b7eSSam Leffler ath_tx_cleanup(struct ath_softc *sc)
2885c42a7b7eSSam Leffler {
2886c42a7b7eSSam Leffler 	int i;
2887c42a7b7eSSam Leffler 
2888c42a7b7eSSam Leffler 	ATH_TXBUF_LOCK_DESTROY(sc);
2889c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
2890c42a7b7eSSam Leffler 		if (ATH_TXQ_SETUP(sc, i))
2891c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, &sc->sc_txq[i]);
2892c42a7b7eSSam Leffler }
28935591b213SSam Leffler 
28945591b213SSam Leffler static int
28955591b213SSam Leffler ath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni, struct ath_buf *bf,
28965591b213SSam Leffler     struct mbuf *m0)
28975591b213SSam Leffler {
28985591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
28995591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2900c42a7b7eSSam Leffler 	struct ifnet *ifp = &sc->sc_if;
2901c42a7b7eSSam Leffler 	int i, error, iswep, ismcast, keyix, hdrlen, pktlen, try0;
2902c42a7b7eSSam Leffler 	u_int8_t rix, txrate, ctsrate;
2903c42a7b7eSSam Leffler 	u_int8_t cix = 0xff;		/* NB: silence compiler */
2904c42a7b7eSSam Leffler 	struct ath_desc *ds, *ds0;
2905c42a7b7eSSam Leffler 	struct ath_txq *txq;
29065591b213SSam Leffler 	struct mbuf *m;
29075591b213SSam Leffler 	struct ieee80211_frame *wh;
2908c42a7b7eSSam Leffler 	u_int subtype, flags, ctsduration;
29095591b213SSam Leffler 	HAL_PKT_TYPE atype;
29105591b213SSam Leffler 	const HAL_RATE_TABLE *rt;
29115591b213SSam Leffler 	HAL_BOOL shortPreamble;
29125591b213SSam Leffler 	struct ath_node *an;
29135591b213SSam Leffler 
29145591b213SSam Leffler 	wh = mtod(m0, struct ieee80211_frame *);
29155591b213SSam Leffler 	iswep = wh->i_fc[1] & IEEE80211_FC1_WEP;
2916c42a7b7eSSam Leffler 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
2917c42a7b7eSSam Leffler 	hdrlen = ieee80211_anyhdrsize(wh);
2918c42a7b7eSSam Leffler 	/*
2919a614e076SSam Leffler 	 * Packet length must not include any
2920a614e076SSam Leffler 	 * pad bytes; deduct them here.
2921c42a7b7eSSam Leffler 	 */
2922c42a7b7eSSam Leffler 	pktlen = m0->m_pkthdr.len - (hdrlen & 3);
29235591b213SSam Leffler 
29245591b213SSam Leffler 	if (iswep) {
2925c42a7b7eSSam Leffler 		const struct ieee80211_cipher *cip;
2926c42a7b7eSSam Leffler 		struct ieee80211_key *k;
2927c42a7b7eSSam Leffler 
2928c42a7b7eSSam Leffler 		/*
2929c42a7b7eSSam Leffler 		 * Construct the 802.11 header+trailer for an encrypted
2930c42a7b7eSSam Leffler 		 * frame. The only reason this can fail is because of an
2931c42a7b7eSSam Leffler 		 * unknown or unsupported cipher/key type.
2932c42a7b7eSSam Leffler 		 */
2933c42a7b7eSSam Leffler 		k = ieee80211_crypto_encap(ic, ni, m0);
2934c42a7b7eSSam Leffler 		if (k == NULL) {
2935c42a7b7eSSam Leffler 			/*
2936c42a7b7eSSam Leffler 			 * This can happen when the key is yanked after the
2937c42a7b7eSSam Leffler 			 * frame was queued.  Just discard the frame; the
2938c42a7b7eSSam Leffler 			 * 802.11 layer counts failures and provides
2939c42a7b7eSSam Leffler 			 * debugging/diagnostics.
2940c42a7b7eSSam Leffler 			 */
2941c42a7b7eSSam Leffler 			return EIO;
29425591b213SSam Leffler 		}
2943c42a7b7eSSam Leffler 		/*
2944c42a7b7eSSam Leffler 		 * Adjust the packet + header lengths for the crypto
2945c42a7b7eSSam Leffler 		 * additions and calculate the h/w key index.  When
2946c42a7b7eSSam Leffler 		 * a s/w mic is done the frame will have had any mic
2947c42a7b7eSSam Leffler 		 * added to it prior to entry so skb->len above will
2948c42a7b7eSSam Leffler 		 * account for it. Otherwise we need to add it to the
2949c42a7b7eSSam Leffler 		 * packet length.
2950c42a7b7eSSam Leffler 		 */
2951c42a7b7eSSam Leffler 		cip = k->wk_cipher;
2952c42a7b7eSSam Leffler 		hdrlen += cip->ic_header;
2953c42a7b7eSSam Leffler 		pktlen += cip->ic_header + cip->ic_trailer;
2954c42a7b7eSSam Leffler 		if ((k->wk_flags & IEEE80211_KEY_SWMIC) == 0)
2955c42a7b7eSSam Leffler 			pktlen += cip->ic_miclen;
2956c42a7b7eSSam Leffler 		keyix = k->wk_keyix;
2957c42a7b7eSSam Leffler 
2958c42a7b7eSSam Leffler 		/* packet header may have moved, reset our local pointer */
2959167ecdcaSSam Leffler 		wh = mtod(m0, struct ieee80211_frame *);
2960c42a7b7eSSam Leffler 	} else
2961c42a7b7eSSam Leffler 		keyix = HAL_TXKEYIX_INVALID;
2962c42a7b7eSSam Leffler 
29635591b213SSam Leffler 	pktlen += IEEE80211_CRC_LEN;
29645591b213SSam Leffler 
29655591b213SSam Leffler 	/*
29665591b213SSam Leffler 	 * Load the DMA map so any coalescing is done.  This
29675591b213SSam Leffler 	 * also calculates the number of descriptors we need.
29685591b213SSam Leffler 	 */
29695591b213SSam Leffler 	error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m0,
29705591b213SSam Leffler 				     ath_mbuf_load_cb, bf,
29715591b213SSam Leffler 				     BUS_DMA_NOWAIT);
297200a12f3aSSam Leffler 	if (error == EFBIG) {
297300a12f3aSSam Leffler 		/* XXX packet requires too many descriptors */
297400a12f3aSSam Leffler 		bf->bf_nseg = ATH_TXDESC+1;
297500a12f3aSSam Leffler 	} else if (error != 0) {
29765591b213SSam Leffler 		sc->sc_stats.ast_tx_busdma++;
29775591b213SSam Leffler 		m_freem(m0);
29785591b213SSam Leffler 		return error;
29795591b213SSam Leffler 	}
29805591b213SSam Leffler 	/*
29815591b213SSam Leffler 	 * Discard null packets and check for packets that
29825591b213SSam Leffler 	 * require too many TX descriptors.  We try to convert
29835591b213SSam Leffler 	 * the latter to a cluster.
29845591b213SSam Leffler 	 */
29855591b213SSam Leffler 	if (bf->bf_nseg > ATH_TXDESC) {		/* too many desc's, linearize */
29865591b213SSam Leffler 		sc->sc_stats.ast_tx_linear++;
29875591b213SSam Leffler 		MGETHDR(m, M_DONTWAIT, MT_DATA);
29885591b213SSam Leffler 		if (m == NULL) {
29895591b213SSam Leffler 			sc->sc_stats.ast_tx_nombuf++;
29905591b213SSam Leffler 			m_freem(m0);
29915591b213SSam Leffler 			return ENOMEM;
29925591b213SSam Leffler 		}
29935591b213SSam Leffler 		M_MOVE_PKTHDR(m, m0);
29945591b213SSam Leffler 		MCLGET(m, M_DONTWAIT);
29955591b213SSam Leffler 		if ((m->m_flags & M_EXT) == 0) {
29965591b213SSam Leffler 			sc->sc_stats.ast_tx_nomcl++;
29975591b213SSam Leffler 			m_freem(m0);
29985591b213SSam Leffler 			m_free(m);
29995591b213SSam Leffler 			return ENOMEM;
30005591b213SSam Leffler 		}
30015591b213SSam Leffler 		m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
30025591b213SSam Leffler 		m_freem(m0);
30035591b213SSam Leffler 		m->m_len = m->m_pkthdr.len;
30045591b213SSam Leffler 		m0 = m;
30055591b213SSam Leffler 		error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m0,
30065591b213SSam Leffler 					     ath_mbuf_load_cb, bf,
30075591b213SSam Leffler 					     BUS_DMA_NOWAIT);
30085591b213SSam Leffler 		if (error != 0) {
30095591b213SSam Leffler 			sc->sc_stats.ast_tx_busdma++;
30105591b213SSam Leffler 			m_freem(m0);
30115591b213SSam Leffler 			return error;
30125591b213SSam Leffler 		}
30135591b213SSam Leffler 		KASSERT(bf->bf_nseg == 1,
3014d77367bfSSam Leffler 			("packet not one segment; nseg %u", bf->bf_nseg));
30155591b213SSam Leffler 	} else if (bf->bf_nseg == 0) {		/* null packet, discard */
30165591b213SSam Leffler 		sc->sc_stats.ast_tx_nodata++;
30175591b213SSam Leffler 		m_freem(m0);
30185591b213SSam Leffler 		return EIO;
30195591b213SSam Leffler 	}
3020c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_XMIT, "%s: m %p len %u\n", __func__, m0, pktlen);
30215591b213SSam Leffler 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
30225591b213SSam Leffler 	bf->bf_m = m0;
30230a915fadSSam Leffler 	bf->bf_node = ni;			/* NB: held reference */
30245591b213SSam Leffler 
30255591b213SSam Leffler 	/* setup descriptors */
30265591b213SSam Leffler 	ds = bf->bf_desc;
30275591b213SSam Leffler 	rt = sc->sc_currates;
30285591b213SSam Leffler 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
30295591b213SSam Leffler 
30305591b213SSam Leffler 	/*
3031c42a7b7eSSam Leffler 	 * NB: the 802.11 layer marks whether or not we should
3032c42a7b7eSSam Leffler 	 * use short preamble based on the current mode and
3033c42a7b7eSSam Leffler 	 * negotiated parameters.
30345591b213SSam Leffler 	 */
3035c42a7b7eSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
3036c42a7b7eSSam Leffler 	    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
3037c42a7b7eSSam Leffler 		shortPreamble = AH_TRUE;
3038c42a7b7eSSam Leffler 		sc->sc_stats.ast_tx_shortpre++;
3039c42a7b7eSSam Leffler 	} else {
3040c42a7b7eSSam Leffler 		shortPreamble = AH_FALSE;
3041c42a7b7eSSam Leffler 	}
3042c42a7b7eSSam Leffler 
3043c42a7b7eSSam Leffler 	an = ATH_NODE(ni);
3044c42a7b7eSSam Leffler 	flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for crypto errs */
3045c42a7b7eSSam Leffler 	/*
3046c42a7b7eSSam Leffler 	 * Calculate Atheros packet type from IEEE80211 packet header,
3047c42a7b7eSSam Leffler 	 * setup for rate calculations, and select h/w transmit queue.
3048c42a7b7eSSam Leffler 	 */
30495591b213SSam Leffler 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
30505591b213SSam Leffler 	case IEEE80211_FC0_TYPE_MGT:
30515591b213SSam Leffler 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
30525591b213SSam Leffler 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON)
30535591b213SSam Leffler 			atype = HAL_PKT_TYPE_BEACON;
30545591b213SSam Leffler 		else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
30555591b213SSam Leffler 			atype = HAL_PKT_TYPE_PROBE_RESP;
30565591b213SSam Leffler 		else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM)
30575591b213SSam Leffler 			atype = HAL_PKT_TYPE_ATIM;
3058c42a7b7eSSam Leffler 		else
3059c42a7b7eSSam Leffler 			atype = HAL_PKT_TYPE_NORMAL;	/* XXX */
30605591b213SSam Leffler 		rix = 0;			/* XXX lowest rate */
3061c42a7b7eSSam Leffler 		try0 = ATH_TXMAXTRY;
3062c42a7b7eSSam Leffler 		if (shortPreamble)
3063c42a7b7eSSam Leffler 			txrate = an->an_tx_mgtratesp;
3064c42a7b7eSSam Leffler 		else
3065c42a7b7eSSam Leffler 			txrate = an->an_tx_mgtrate;
3066c42a7b7eSSam Leffler 		/* NB: force all management frames to highest queue */
3067c42a7b7eSSam Leffler 		if (ni->ni_flags & IEEE80211_NODE_QOS) {
3068c42a7b7eSSam Leffler 			/* NB: force all management frames to highest queue */
3069c42a7b7eSSam Leffler 			txq = sc->sc_ac2q[WME_AC_VO];
3070c42a7b7eSSam Leffler 		} else
3071c42a7b7eSSam Leffler 			txq = sc->sc_ac2q[WME_AC_BE];
3072c42a7b7eSSam Leffler 		flags |= HAL_TXDESC_INTREQ;	/* force interrupt */
30735591b213SSam Leffler 		break;
30745591b213SSam Leffler 	case IEEE80211_FC0_TYPE_CTL:
3075c42a7b7eSSam Leffler 		atype = HAL_PKT_TYPE_PSPOLL;	/* stop setting of duration */
30765591b213SSam Leffler 		rix = 0;			/* XXX lowest rate */
3077c42a7b7eSSam Leffler 		try0 = ATH_TXMAXTRY;
3078c42a7b7eSSam Leffler 		if (shortPreamble)
3079c42a7b7eSSam Leffler 			txrate = an->an_tx_mgtratesp;
3080c42a7b7eSSam Leffler 		else
3081c42a7b7eSSam Leffler 			txrate = an->an_tx_mgtrate;
3082c42a7b7eSSam Leffler 		/* NB: force all ctl frames to highest queue */
3083c42a7b7eSSam Leffler 		if (ni->ni_flags & IEEE80211_NODE_QOS) {
3084c42a7b7eSSam Leffler 			/* NB: force all ctl frames to highest queue */
3085c42a7b7eSSam Leffler 			txq = sc->sc_ac2q[WME_AC_VO];
3086c42a7b7eSSam Leffler 		} else
3087c42a7b7eSSam Leffler 			txq = sc->sc_ac2q[WME_AC_BE];
3088c42a7b7eSSam Leffler 		flags |= HAL_TXDESC_INTREQ;	/* force interrupt */
3089c42a7b7eSSam Leffler 		break;
3090c42a7b7eSSam Leffler 	case IEEE80211_FC0_TYPE_DATA:
3091c42a7b7eSSam Leffler 		atype = HAL_PKT_TYPE_NORMAL;		/* default */
3092c42a7b7eSSam Leffler 		/*
3093c42a7b7eSSam Leffler 		 * Data frames; consult the rate control module.
3094c42a7b7eSSam Leffler 		 */
3095c42a7b7eSSam Leffler 		ath_rate_findrate(sc, an, shortPreamble, pktlen,
3096c42a7b7eSSam Leffler 			&rix, &try0, &txrate);
3097c42a7b7eSSam Leffler 		/*
3098c42a7b7eSSam Leffler 		 * Default all non-QoS traffic to the background queue.
3099c42a7b7eSSam Leffler 		 */
3100c42a7b7eSSam Leffler 		if (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_QOS) {
3101c42a7b7eSSam Leffler 			u_int pri = M_WME_GETAC(m0);
3102c42a7b7eSSam Leffler 			txq = sc->sc_ac2q[pri];
3103c42a7b7eSSam Leffler 			if (ic->ic_wme.wme_wmeChanParams.cap_wmeParams[pri].wmep_noackPolicy)
3104c42a7b7eSSam Leffler 				 flags |= HAL_TXDESC_NOACK;
3105c42a7b7eSSam Leffler 		} else
3106c42a7b7eSSam Leffler 			txq = sc->sc_ac2q[WME_AC_BE];
31075591b213SSam Leffler 		break;
31085591b213SSam Leffler 	default:
3109c42a7b7eSSam Leffler 		if_printf(ifp, "bogus frame type 0x%x (%s)\n",
3110c42a7b7eSSam Leffler 			wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__);
3111c42a7b7eSSam Leffler 		/* XXX statistic */
31125591b213SSam Leffler 		m_freem(m0);
31135591b213SSam Leffler 		return EIO;
31145591b213SSam Leffler 	}
3115c42a7b7eSSam Leffler 
31165591b213SSam Leffler 	/*
3117c42a7b7eSSam Leffler 	 * When servicing one or more stations in power-save mode
3118c42a7b7eSSam Leffler 	 * multicast frames must be buffered until after the beacon.
3119c42a7b7eSSam Leffler 	 * We use the CAB queue for that.
31205591b213SSam Leffler 	 */
3121c42a7b7eSSam Leffler 	if (ismcast && ic->ic_ps_sta) {
3122c42a7b7eSSam Leffler 		txq = sc->sc_cabq;
3123c42a7b7eSSam Leffler 		/* XXX? more bit in 802.11 frame header */
31245591b213SSam Leffler 	}
31255591b213SSam Leffler 
31265591b213SSam Leffler 	/*
31275591b213SSam Leffler 	 * Calculate miscellaneous flags.
31285591b213SSam Leffler 	 */
3129c42a7b7eSSam Leffler 	if (ismcast) {
31305591b213SSam Leffler 		flags |= HAL_TXDESC_NOACK;	/* no ack on broad/multicast */
31315591b213SSam Leffler 		sc->sc_stats.ast_tx_noack++;
31325591b213SSam Leffler 	} else if (pktlen > ic->ic_rtsthreshold) {
31335591b213SSam Leffler 		flags |= HAL_TXDESC_RTSENA;	/* RTS based on frame length */
3134c42a7b7eSSam Leffler 		cix = rt->info[rix].controlRate;
31355591b213SSam Leffler 		sc->sc_stats.ast_tx_rts++;
31365591b213SSam Leffler 	}
31375591b213SSam Leffler 
31385591b213SSam Leffler 	/*
3139c42a7b7eSSam Leffler 	 * If 802.11g protection is enabled, determine whether
3140c42a7b7eSSam Leffler 	 * to use RTS/CTS or just CTS.  Note that this is only
3141c42a7b7eSSam Leffler 	 * done for OFDM unicast frames.
3142c42a7b7eSSam Leffler 	 */
3143c42a7b7eSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
3144c42a7b7eSSam Leffler 	    rt->info[rix].phy == IEEE80211_T_OFDM &&
3145c42a7b7eSSam Leffler 	    (flags & HAL_TXDESC_NOACK) == 0) {
3146c42a7b7eSSam Leffler 		/* XXX fragments must use CCK rates w/ protection */
3147c42a7b7eSSam Leffler 		if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
3148c42a7b7eSSam Leffler 			flags |= HAL_TXDESC_RTSENA;
3149c42a7b7eSSam Leffler 		else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
3150c42a7b7eSSam Leffler 			flags |= HAL_TXDESC_CTSENA;
3151c42a7b7eSSam Leffler 		cix = rt->info[sc->sc_protrix].controlRate;
3152c42a7b7eSSam Leffler 		sc->sc_stats.ast_tx_protect++;
3153c42a7b7eSSam Leffler 	}
3154c42a7b7eSSam Leffler 
3155c42a7b7eSSam Leffler 	/*
3156f6aa038bSSam Leffler 	 * Calculate duration.  This logically belongs in the 802.11
3157f6aa038bSSam Leffler 	 * layer but it lacks sufficient information to calculate it.
3158f6aa038bSSam Leffler 	 */
3159f6aa038bSSam Leffler 	if ((flags & HAL_TXDESC_NOACK) == 0 &&
3160f6aa038bSSam Leffler 	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL) {
3161f6aa038bSSam Leffler 		u_int16_t dur;
3162f6aa038bSSam Leffler 		/*
3163f6aa038bSSam Leffler 		 * XXX not right with fragmentation.
3164f6aa038bSSam Leffler 		 */
3165c42a7b7eSSam Leffler 		if (shortPreamble)
3166c42a7b7eSSam Leffler 			dur = rt->info[rix].spAckDuration;
3167c42a7b7eSSam Leffler 		else
3168c42a7b7eSSam Leffler 			dur = rt->info[rix].lpAckDuration;
3169c42a7b7eSSam Leffler 		*(u_int16_t *)wh->i_dur = htole16(dur);
3170f6aa038bSSam Leffler 	}
3171f6aa038bSSam Leffler 
3172f6aa038bSSam Leffler 	/*
31735591b213SSam Leffler 	 * Calculate RTS/CTS rate and duration if needed.
31745591b213SSam Leffler 	 */
31755591b213SSam Leffler 	ctsduration = 0;
31765591b213SSam Leffler 	if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA)) {
31775591b213SSam Leffler 		/*
31785591b213SSam Leffler 		 * CTS transmit rate is derived from the transmit rate
31795591b213SSam Leffler 		 * by looking in the h/w rate table.  We must also factor
31805591b213SSam Leffler 		 * in whether or not a short preamble is to be used.
31815591b213SSam Leffler 		 */
3182c42a7b7eSSam Leffler 		/* NB: cix is set above where RTS/CTS is enabled */
3183c42a7b7eSSam Leffler 		KASSERT(cix != 0xff, ("cix not setup"));
31845591b213SSam Leffler 		ctsrate = rt->info[cix].rateCode;
31855591b213SSam Leffler 		/*
3186c42a7b7eSSam Leffler 		 * Compute the transmit duration based on the frame
3187c42a7b7eSSam Leffler 		 * size and the size of an ACK frame.  We call into the
3188c42a7b7eSSam Leffler 		 * HAL to do the computation since it depends on the
3189c42a7b7eSSam Leffler 		 * characteristics of the actual PHY being used.
3190c42a7b7eSSam Leffler 		 *
3191c42a7b7eSSam Leffler 		 * NB: CTS is assumed the same size as an ACK so we can
3192c42a7b7eSSam Leffler 		 *     use the precalculated ACK durations.
31935591b213SSam Leffler 		 */
3194c42a7b7eSSam Leffler 		if (shortPreamble) {
3195c42a7b7eSSam Leffler 			ctsrate |= rt->info[cix].shortPreamble;
3196c42a7b7eSSam Leffler 			if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
3197c42a7b7eSSam Leffler 				ctsduration += rt->info[cix].spAckDuration;
31985591b213SSam Leffler 			ctsduration += ath_hal_computetxtime(ah,
3199c42a7b7eSSam Leffler 				rt, pktlen, rix, AH_TRUE);
3200c42a7b7eSSam Leffler 			if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
3201c42a7b7eSSam Leffler 				ctsduration += rt->info[cix].spAckDuration;
3202c42a7b7eSSam Leffler 		} else {
3203c42a7b7eSSam Leffler 			if (flags & HAL_TXDESC_RTSENA)		/* SIFS + CTS */
3204c42a7b7eSSam Leffler 				ctsduration += rt->info[cix].lpAckDuration;
3205c42a7b7eSSam Leffler 			ctsduration += ath_hal_computetxtime(ah,
3206c42a7b7eSSam Leffler 				rt, pktlen, rix, AH_FALSE);
3207c42a7b7eSSam Leffler 			if ((flags & HAL_TXDESC_NOACK) == 0)	/* SIFS + ACK */
3208c42a7b7eSSam Leffler 				ctsduration += rt->info[cix].lpAckDuration;
32095591b213SSam Leffler 		}
3210c42a7b7eSSam Leffler 		/*
3211c42a7b7eSSam Leffler 		 * Must disable multi-rate retry when using RTS/CTS.
3212c42a7b7eSSam Leffler 		 */
3213c42a7b7eSSam Leffler 		try0 = ATH_TXMAXTRY;
32145591b213SSam Leffler 	} else
32155591b213SSam Leffler 		ctsrate = 0;
32165591b213SSam Leffler 
3217c42a7b7eSSam Leffler 	if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
3218c42a7b7eSSam Leffler 		ieee80211_dump_pkt(mtod(m0, caddr_t), m0->m_len,
3219c42a7b7eSSam Leffler 			sc->sc_hwmap[txrate], -1);
32205591b213SSam Leffler 
3221eb2cdcb1SSam Leffler 	if (ic->ic_rawbpf)
3222eb2cdcb1SSam Leffler 		bpf_mtap(ic->ic_rawbpf, m0);
3223eb2cdcb1SSam Leffler 	if (sc->sc_drvbpf) {
322416b4851aSSam Leffler 		sc->sc_tx_th.wt_flags = sc->sc_hwflags[txrate];
3225eb2cdcb1SSam Leffler 		if (iswep)
3226eb2cdcb1SSam Leffler 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
322716b4851aSSam Leffler 		sc->sc_tx_th.wt_rate = sc->sc_hwmap[txrate];
3228c42a7b7eSSam Leffler 		sc->sc_tx_th.wt_txpower = ni->ni_txpower;
3229c42a7b7eSSam Leffler 		sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
3230eb2cdcb1SSam Leffler 
3231eb2cdcb1SSam Leffler 		bpf_mtap2(sc->sc_drvbpf,
32322f1ad18bSSam Leffler 			&sc->sc_tx_th, sc->sc_tx_th_len, m0);
3233eb2cdcb1SSam Leffler 	}
3234eb2cdcb1SSam Leffler 
32355591b213SSam Leffler 	/*
3236c42a7b7eSSam Leffler 	 * Determine if a tx interrupt should be generated for
3237c42a7b7eSSam Leffler 	 * this descriptor.  We take a tx interrupt to reap
3238c42a7b7eSSam Leffler 	 * descriptors when the h/w hits an EOL condition or
3239c42a7b7eSSam Leffler 	 * when the descriptor is specifically marked to generate
3240c42a7b7eSSam Leffler 	 * an interrupt.  We periodically mark descriptors in this
3241c42a7b7eSSam Leffler 	 * way to insure timely replenishing of the supply needed
3242c42a7b7eSSam Leffler 	 * for sending frames.  Defering interrupts reduces system
3243c42a7b7eSSam Leffler 	 * load and potentially allows more concurrent work to be
3244c42a7b7eSSam Leffler 	 * done but if done to aggressively can cause senders to
3245c42a7b7eSSam Leffler 	 * backup.
3246c42a7b7eSSam Leffler 	 *
3247c42a7b7eSSam Leffler 	 * NB: use >= to deal with sc_txintrperiod changing
3248c42a7b7eSSam Leffler 	 *     dynamically through sysctl.
3249c42a7b7eSSam Leffler 	 */
3250c42a7b7eSSam Leffler 	if (flags & HAL_TXDESC_INTREQ) {
3251c42a7b7eSSam Leffler 		txq->axq_intrcnt = 0;
3252c42a7b7eSSam Leffler 	} else if (++txq->axq_intrcnt >= sc->sc_txintrperiod) {
3253c42a7b7eSSam Leffler 		flags |= HAL_TXDESC_INTREQ;
3254c42a7b7eSSam Leffler 		txq->axq_intrcnt = 0;
3255c42a7b7eSSam Leffler 	}
3256c42a7b7eSSam Leffler 
3257c42a7b7eSSam Leffler 	/*
32585591b213SSam Leffler 	 * Formulate first tx descriptor with tx controls.
32595591b213SSam Leffler 	 */
32605591b213SSam Leffler 	/* XXX check return value? */
32615591b213SSam Leffler 	ath_hal_setuptxdesc(ah, ds
32625591b213SSam Leffler 		, pktlen		/* packet length */
32635591b213SSam Leffler 		, hdrlen		/* header length */
32645591b213SSam Leffler 		, atype			/* Atheros packet type */
3265c42a7b7eSSam Leffler 		, ni->ni_txpower	/* txpower */
3266c42a7b7eSSam Leffler 		, txrate, try0		/* series 0 rate/tries */
3267c42a7b7eSSam Leffler 		, keyix			/* key cache index */
3268c42a7b7eSSam Leffler 		, sc->sc_txantenna	/* antenna mode */
32695591b213SSam Leffler 		, flags			/* flags */
32705591b213SSam Leffler 		, ctsrate		/* rts/cts rate */
32715591b213SSam Leffler 		, ctsduration		/* rts/cts duration */
32725591b213SSam Leffler 	);
3273c42a7b7eSSam Leffler 	/*
3274c42a7b7eSSam Leffler 	 * Setup the multi-rate retry state only when we're
3275c42a7b7eSSam Leffler 	 * going to use it.  This assumes ath_hal_setuptxdesc
3276c42a7b7eSSam Leffler 	 * initializes the descriptors (so we don't have to)
3277c42a7b7eSSam Leffler 	 * when the hardware supports multi-rate retry and
3278c42a7b7eSSam Leffler 	 * we don't use it.
3279c42a7b7eSSam Leffler 	 */
3280c42a7b7eSSam Leffler 	if (try0 != ATH_TXMAXTRY)
3281c42a7b7eSSam Leffler 		ath_rate_setupxtxdesc(sc, an, ds, shortPreamble, rix);
3282c42a7b7eSSam Leffler 
32835591b213SSam Leffler 	/*
32845591b213SSam Leffler 	 * Fillin the remainder of the descriptor info.
32855591b213SSam Leffler 	 */
3286c42a7b7eSSam Leffler 	ds0 = ds;
32875591b213SSam Leffler 	for (i = 0; i < bf->bf_nseg; i++, ds++) {
32885591b213SSam Leffler 		ds->ds_data = bf->bf_segs[i].ds_addr;
32895591b213SSam Leffler 		if (i == bf->bf_nseg - 1)
32905591b213SSam Leffler 			ds->ds_link = 0;
32915591b213SSam Leffler 		else
32925591b213SSam Leffler 			ds->ds_link = bf->bf_daddr + sizeof(*ds) * (i + 1);
32935591b213SSam Leffler 		ath_hal_filltxdesc(ah, ds
32945591b213SSam Leffler 			, bf->bf_segs[i].ds_len	/* segment length */
32955591b213SSam Leffler 			, i == 0		/* first segment */
32965591b213SSam Leffler 			, i == bf->bf_nseg - 1	/* last segment */
3297c42a7b7eSSam Leffler 			, ds0			/* first descriptor */
32985591b213SSam Leffler 		);
3299c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT,
3300c42a7b7eSSam Leffler 			"%s: %d: %08x %08x %08x %08x %08x %08x\n",
3301e325e530SSam Leffler 			__func__, i, ds->ds_link, ds->ds_data,
3302c42a7b7eSSam Leffler 			ds->ds_ctl0, ds->ds_ctl1, ds->ds_hw[0], ds->ds_hw[1]);
33035591b213SSam Leffler 	}
3304c42a7b7eSSam Leffler #if 0
3305c42a7b7eSSam Leffler 	if ((flags & (HAL_TXDESC_RTSENA | HAL_TXDESC_CTSENA)) &&
3306c42a7b7eSSam Leffler   	    !ath_hal_updateCTSForBursting(ah, ds
3307c42a7b7eSSam Leffler 		     , txq->axq_linkbuf != NULL ?
3308c42a7b7eSSam Leffler 			txq->axq_linkbuf->bf_desc : NULL
3309c42a7b7eSSam Leffler 		     , txq->axq_lastdsWithCTS
3310c42a7b7eSSam Leffler 		     , txq->axq_gatingds
3311c42a7b7eSSam Leffler 		     , IEEE80211_TXOP_TO_US(ic->ic_chanParams.cap_wmeParams[skb->priority].wmep_txopLimit)
3312c42a7b7eSSam Leffler 		     , ath_hal_computetxtime(ah, rt, IEEE80211_ACK_LEN, cix, AH_TRUE))) {
3313c42a7b7eSSam Leffler 		ATH_TXQ_LOCK(txq);
3314c42a7b7eSSam Leffler 		txq->axq_lastdsWithCTS = ds;
3315c42a7b7eSSam Leffler 		/* set gating Desc to final desc */
3316c42a7b7eSSam Leffler 		txq->axq_gatingds = (struct ath_desc *)txq->axq_link;
3317c42a7b7eSSam Leffler 		ATH_TXQ_UNLOCK(txq);
3318c42a7b7eSSam Leffler 	}
3319c42a7b7eSSam Leffler #endif
33205591b213SSam Leffler 	/*
33215591b213SSam Leffler 	 * Insert the frame on the outbound list and
33225591b213SSam Leffler 	 * pass it on to the hardware.
33235591b213SSam Leffler 	 */
3324c42a7b7eSSam Leffler 	ATH_TXQ_LOCK(txq);
3325c42a7b7eSSam Leffler 	ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
3326c42a7b7eSSam Leffler 	if (txq->axq_link == NULL) {
3327c42a7b7eSSam Leffler 		ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
3328c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT,
3329c42a7b7eSSam Leffler 			"%s: TXDP[%u] = %p (%p) depth %d\n", __func__,
3330c42a7b7eSSam Leffler 			txq->axq_qnum, (caddr_t)bf->bf_daddr, bf->bf_desc,
3331c42a7b7eSSam Leffler 			txq->axq_depth);
33325591b213SSam Leffler 	} else {
3333c42a7b7eSSam Leffler 		*txq->axq_link = bf->bf_daddr;
3334c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT,
3335c42a7b7eSSam Leffler 			"%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
3336c42a7b7eSSam Leffler 			txq->axq_qnum, txq->axq_link,
3337c42a7b7eSSam Leffler 			(caddr_t)bf->bf_daddr, bf->bf_desc, txq->axq_depth);
33385591b213SSam Leffler 	}
3339c42a7b7eSSam Leffler 	txq->axq_link = &bf->bf_desc[bf->bf_nseg - 1].ds_link;
3340c42a7b7eSSam Leffler 	ATH_TXQ_UNLOCK(txq);
33415591b213SSam Leffler 
3342c42a7b7eSSam Leffler 	if (sc->sc_softled)
3343c42a7b7eSSam Leffler 		ath_update_led(sc);
3344c42a7b7eSSam Leffler 
3345c42a7b7eSSam Leffler 	/*
3346c42a7b7eSSam Leffler 	 * The CAB queue is started from the SWBA handler since
3347c42a7b7eSSam Leffler 	 * frames only go out on DTIM and to avoid possible races.
3348c42a7b7eSSam Leffler 	 */
3349c42a7b7eSSam Leffler 	if (txq != sc->sc_cabq)
3350c42a7b7eSSam Leffler 		ath_hal_txstart(ah, txq->axq_qnum);
33515591b213SSam Leffler 	return 0;
33525591b213SSam Leffler }
33535591b213SSam Leffler 
3354c42a7b7eSSam Leffler /*
3355c42a7b7eSSam Leffler  * Process completed xmit descriptors from the specified queue.
3356c42a7b7eSSam Leffler  */
33575591b213SSam Leffler static void
3358c42a7b7eSSam Leffler ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq)
33595591b213SSam Leffler {
33605591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
33610a915fadSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
3362c42a7b7eSSam Leffler 	struct ath_buf *bf;
33635591b213SSam Leffler 	struct ath_desc *ds;
33645591b213SSam Leffler 	struct ieee80211_node *ni;
33655591b213SSam Leffler 	struct ath_node *an;
3366c42a7b7eSSam Leffler 	int sr, lr, pri;
33675591b213SSam Leffler 	HAL_STATUS status;
33685591b213SSam Leffler 
3369c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_TX_PROC, "%s: tx queue %u head %p link %p\n",
3370c42a7b7eSSam Leffler 		__func__, txq->axq_qnum,
3371c42a7b7eSSam Leffler 		(caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
3372c42a7b7eSSam Leffler 		txq->axq_link);
33735591b213SSam Leffler 	for (;;) {
3374c42a7b7eSSam Leffler 		ATH_TXQ_LOCK(txq);
3375c42a7b7eSSam Leffler 		txq->axq_intrcnt = 0;	/* reset periodic desc intr count */
3376c42a7b7eSSam Leffler 		bf = STAILQ_FIRST(&txq->axq_q);
33775591b213SSam Leffler 		if (bf == NULL) {
3378c42a7b7eSSam Leffler 			txq->axq_link = NULL;
3379c42a7b7eSSam Leffler 			ATH_TXQ_UNLOCK(txq);
33805591b213SSam Leffler 			break;
33815591b213SSam Leffler 		}
33825591b213SSam Leffler 		/* only the last descriptor is needed */
33835591b213SSam Leffler 		ds = &bf->bf_desc[bf->bf_nseg - 1];
33845591b213SSam Leffler 		status = ath_hal_txprocdesc(ah, ds);
33855591b213SSam Leffler #ifdef AR_DEBUG
3386c42a7b7eSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_XMIT_DESC)
33875591b213SSam Leffler 			ath_printtxbuf(bf, status == HAL_OK);
33885591b213SSam Leffler #endif
33895591b213SSam Leffler 		if (status == HAL_EINPROGRESS) {
3390c42a7b7eSSam Leffler 			ATH_TXQ_UNLOCK(txq);
33915591b213SSam Leffler 			break;
33925591b213SSam Leffler 		}
3393c42a7b7eSSam Leffler #if 0
3394c42a7b7eSSam Leffler 		if (bf->bf_desc == txq->axq_lastdsWithCTS)
3395c42a7b7eSSam Leffler 			txq->axq_lastdsWithCTS = NULL;
3396c42a7b7eSSam Leffler 		if (ds == txq->axq_gatingds)
3397c42a7b7eSSam Leffler 			txq->axq_gatingds = NULL;
3398c42a7b7eSSam Leffler #endif
3399c42a7b7eSSam Leffler 		ATH_TXQ_REMOVE_HEAD(txq, bf_list);
3400c42a7b7eSSam Leffler 		ATH_TXQ_UNLOCK(txq);
34015591b213SSam Leffler 
34025591b213SSam Leffler 		ni = bf->bf_node;
34035591b213SSam Leffler 		if (ni != NULL) {
3404c42a7b7eSSam Leffler 			an = ATH_NODE(ni);
34055591b213SSam Leffler 			if (ds->ds_txstat.ts_status == 0) {
3406c42a7b7eSSam Leffler 				u_int8_t txant = ds->ds_txstat.ts_antenna;
3407c42a7b7eSSam Leffler 				sc->sc_stats.ast_ant_tx[txant]++;
3408c42a7b7eSSam Leffler 				sc->sc_ant_tx[txant]++;
3409c42a7b7eSSam Leffler 				if (ds->ds_txstat.ts_rate & HAL_TXSTAT_ALTRATE)
3410c42a7b7eSSam Leffler 					sc->sc_stats.ast_tx_altrate++;
3411c42a7b7eSSam Leffler 				sc->sc_stats.ast_tx_rssi =
3412c42a7b7eSSam Leffler 					ds->ds_txstat.ts_rssi;
3413c42a7b7eSSam Leffler 				ATH_RSSI_LPF(an->an_halstats.ns_avgtxrssi,
3414c42a7b7eSSam Leffler 					ds->ds_txstat.ts_rssi);
3415c42a7b7eSSam Leffler 				pri = M_WME_GETAC(bf->bf_m);
3416c42a7b7eSSam Leffler 				if (pri >= WME_AC_VO)
3417c42a7b7eSSam Leffler 					ic->ic_wme.wme_hipri_traffic++;
3418c42a7b7eSSam Leffler 				ni->ni_inact = ni->ni_inact_reload;
34195591b213SSam Leffler 			} else {
34205591b213SSam Leffler 				if (ds->ds_txstat.ts_status & HAL_TXERR_XRETRY)
34215591b213SSam Leffler 					sc->sc_stats.ast_tx_xretries++;
34225591b213SSam Leffler 				if (ds->ds_txstat.ts_status & HAL_TXERR_FIFO)
34235591b213SSam Leffler 					sc->sc_stats.ast_tx_fifoerr++;
34245591b213SSam Leffler 				if (ds->ds_txstat.ts_status & HAL_TXERR_FILT)
34255591b213SSam Leffler 					sc->sc_stats.ast_tx_filtered++;
34265591b213SSam Leffler 			}
34275591b213SSam Leffler 			sr = ds->ds_txstat.ts_shortretry;
34285591b213SSam Leffler 			lr = ds->ds_txstat.ts_longretry;
34295591b213SSam Leffler 			sc->sc_stats.ast_tx_shortretry += sr;
34305591b213SSam Leffler 			sc->sc_stats.ast_tx_longretry += lr;
3431c42a7b7eSSam Leffler 			/*
3432c42a7b7eSSam Leffler 			 * Hand the descriptor to the rate control algorithm.
3433c42a7b7eSSam Leffler 			 */
3434c42a7b7eSSam Leffler 			ath_rate_tx_complete(sc, an, ds);
34350a915fadSSam Leffler 			/*
34360a915fadSSam Leffler 			 * Reclaim reference to node.
34370a915fadSSam Leffler 			 *
34380a915fadSSam Leffler 			 * NB: the node may be reclaimed here if, for example
34390a915fadSSam Leffler 			 *     this is a DEAUTH message that was sent and the
34400a915fadSSam Leffler 			 *     node was timed out due to inactivity.
34410a915fadSSam Leffler 			 */
3442c42a7b7eSSam Leffler 			ieee80211_free_node(ni);
34435591b213SSam Leffler 		}
34445591b213SSam Leffler 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
34455591b213SSam Leffler 		    BUS_DMASYNC_POSTWRITE);
34465591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
34475591b213SSam Leffler 		m_freem(bf->bf_m);
34485591b213SSam Leffler 		bf->bf_m = NULL;
34495591b213SSam Leffler 		bf->bf_node = NULL;
34505591b213SSam Leffler 
3451f0b2a0beSSam Leffler 		ATH_TXBUF_LOCK(sc);
3452c42a7b7eSSam Leffler 		STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
3453f0b2a0beSSam Leffler 		ATH_TXBUF_UNLOCK(sc);
34545591b213SSam Leffler 	}
3455c42a7b7eSSam Leffler }
3456c42a7b7eSSam Leffler 
3457c42a7b7eSSam Leffler /*
3458c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
3459c42a7b7eSSam Leffler  * for a single hardware transmit queue (e.g. 5210 and 5211).
3460c42a7b7eSSam Leffler  */
3461c42a7b7eSSam Leffler static void
3462c42a7b7eSSam Leffler ath_tx_proc_q0(void *arg, int npending)
3463c42a7b7eSSam Leffler {
3464c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
3465c42a7b7eSSam Leffler 	struct ifnet *ifp = &sc->sc_if;
3466c42a7b7eSSam Leffler 
3467c42a7b7eSSam Leffler 	ath_tx_processq(sc, &sc->sc_txq[0]);
3468c42a7b7eSSam Leffler 	ath_tx_processq(sc, sc->sc_cabq);
34695591b213SSam Leffler 	ifp->if_flags &= ~IFF_OACTIVE;
34705591b213SSam Leffler 	sc->sc_tx_timer = 0;
34715591b213SSam Leffler 
34725591b213SSam Leffler 	ath_start(ifp);
34735591b213SSam Leffler }
34745591b213SSam Leffler 
34755591b213SSam Leffler /*
3476c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
3477c42a7b7eSSam Leffler  * for four hardware queues, 0-3 (e.g. 5212 w/ WME support).
34785591b213SSam Leffler  */
34795591b213SSam Leffler static void
3480c42a7b7eSSam Leffler ath_tx_proc_q0123(void *arg, int npending)
3481c42a7b7eSSam Leffler {
3482c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
3483c42a7b7eSSam Leffler 	struct ifnet *ifp = &sc->sc_if;
3484c42a7b7eSSam Leffler 
3485c42a7b7eSSam Leffler 	/*
3486c42a7b7eSSam Leffler 	 * Process each active queue.
3487c42a7b7eSSam Leffler 	 */
3488c42a7b7eSSam Leffler 	ath_tx_processq(sc, &sc->sc_txq[0]);
3489c42a7b7eSSam Leffler 	ath_tx_processq(sc, &sc->sc_txq[1]);
3490c42a7b7eSSam Leffler 	ath_tx_processq(sc, &sc->sc_txq[2]);
3491c42a7b7eSSam Leffler 	ath_tx_processq(sc, &sc->sc_txq[3]);
3492c42a7b7eSSam Leffler 	ath_tx_processq(sc, sc->sc_cabq);
3493c42a7b7eSSam Leffler 
3494c42a7b7eSSam Leffler 	ifp->if_flags &= ~IFF_OACTIVE;
3495c42a7b7eSSam Leffler 	sc->sc_tx_timer = 0;
3496c42a7b7eSSam Leffler 
3497c42a7b7eSSam Leffler 	ath_start(ifp);
3498c42a7b7eSSam Leffler }
3499c42a7b7eSSam Leffler 
3500c42a7b7eSSam Leffler /*
3501c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt.
3502c42a7b7eSSam Leffler  */
3503c42a7b7eSSam Leffler static void
3504c42a7b7eSSam Leffler ath_tx_proc(void *arg, int npending)
3505c42a7b7eSSam Leffler {
3506c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
3507c42a7b7eSSam Leffler 	struct ifnet *ifp = &sc->sc_if;
3508c42a7b7eSSam Leffler 	int i;
3509c42a7b7eSSam Leffler 
3510c42a7b7eSSam Leffler 	/*
3511c42a7b7eSSam Leffler 	 * Process each active queue.
3512c42a7b7eSSam Leffler 	 */
3513c42a7b7eSSam Leffler 	/* XXX faster to read ISR_S0_S and ISR_S1_S to determine q's? */
3514c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
3515c42a7b7eSSam Leffler 		if (ATH_TXQ_SETUP(sc, i))
3516c42a7b7eSSam Leffler 			ath_tx_processq(sc, &sc->sc_txq[i]);
3517c42a7b7eSSam Leffler 
3518c42a7b7eSSam Leffler 	ifp->if_flags &= ~IFF_OACTIVE;
3519c42a7b7eSSam Leffler 	sc->sc_tx_timer = 0;
3520c42a7b7eSSam Leffler 
3521c42a7b7eSSam Leffler 	ath_start(ifp);
3522c42a7b7eSSam Leffler }
3523c42a7b7eSSam Leffler 
3524c42a7b7eSSam Leffler static void
3525c42a7b7eSSam Leffler ath_tx_draintxq(struct ath_softc *sc, struct ath_txq *txq)
35265591b213SSam Leffler {
35275591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
352823428eafSSam Leffler 	struct ieee80211_node *ni;
35295591b213SSam Leffler 	struct ath_buf *bf;
35305591b213SSam Leffler 
3531c42a7b7eSSam Leffler 	/*
3532c42a7b7eSSam Leffler 	 * NB: this assumes output has been stopped and
3533c42a7b7eSSam Leffler 	 *     we do not need to block ath_tx_tasklet
3534c42a7b7eSSam Leffler 	 */
35355591b213SSam Leffler 	for (;;) {
3536c42a7b7eSSam Leffler 		ATH_TXQ_LOCK(txq);
3537c42a7b7eSSam Leffler 		bf = STAILQ_FIRST(&txq->axq_q);
35385591b213SSam Leffler 		if (bf == NULL) {
3539c42a7b7eSSam Leffler 			txq->axq_link = NULL;
3540c42a7b7eSSam Leffler 			ATH_TXQ_UNLOCK(txq);
35415591b213SSam Leffler 			break;
35425591b213SSam Leffler 		}
3543c42a7b7eSSam Leffler 		ATH_TXQ_REMOVE_HEAD(txq, bf_list);
3544c42a7b7eSSam Leffler 		ATH_TXQ_UNLOCK(txq);
35455591b213SSam Leffler #ifdef AR_DEBUG
3546c42a7b7eSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_RESET)
35475591b213SSam Leffler 			ath_printtxbuf(bf,
35485591b213SSam Leffler 				ath_hal_txprocdesc(ah, bf->bf_desc) == HAL_OK);
35495591b213SSam Leffler #endif /* AR_DEBUG */
35505591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
35515591b213SSam Leffler 		m_freem(bf->bf_m);
35525591b213SSam Leffler 		bf->bf_m = NULL;
355323428eafSSam Leffler 		ni = bf->bf_node;
35545591b213SSam Leffler 		bf->bf_node = NULL;
3555c42a7b7eSSam Leffler 		if (ni != NULL) {
355623428eafSSam Leffler 			/*
355723428eafSSam Leffler 			 * Reclaim node reference.
355823428eafSSam Leffler 			 */
3559c42a7b7eSSam Leffler 			ieee80211_free_node(ni);
356023428eafSSam Leffler 		}
3561f0b2a0beSSam Leffler 		ATH_TXBUF_LOCK(sc);
3562c42a7b7eSSam Leffler 		STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
3563f0b2a0beSSam Leffler 		ATH_TXBUF_UNLOCK(sc);
35645591b213SSam Leffler 	}
3565c42a7b7eSSam Leffler }
3566c42a7b7eSSam Leffler 
3567c42a7b7eSSam Leffler static void
3568c42a7b7eSSam Leffler ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
3569c42a7b7eSSam Leffler {
3570c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3571c42a7b7eSSam Leffler 
3572c42a7b7eSSam Leffler 	(void) ath_hal_stoptxdma(ah, txq->axq_qnum);
3573c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n",
3574c42a7b7eSSam Leffler 	    __func__, txq->axq_qnum,
35756891c875SPeter Wemm 	    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, txq->axq_qnum),
35766891c875SPeter Wemm 	    txq->axq_link);
3577c42a7b7eSSam Leffler }
3578c42a7b7eSSam Leffler 
3579c42a7b7eSSam Leffler /*
3580c42a7b7eSSam Leffler  * Drain the transmit queues and reclaim resources.
3581c42a7b7eSSam Leffler  */
3582c42a7b7eSSam Leffler static void
3583c42a7b7eSSam Leffler ath_draintxq(struct ath_softc *sc)
3584c42a7b7eSSam Leffler {
3585c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3586c42a7b7eSSam Leffler 	struct ifnet *ifp = &sc->sc_if;
3587c42a7b7eSSam Leffler 	int i;
3588c42a7b7eSSam Leffler 
3589c42a7b7eSSam Leffler 	/* XXX return value */
3590c42a7b7eSSam Leffler 	if (!sc->sc_invalid) {
3591c42a7b7eSSam Leffler 		/* don't touch the hardware if marked invalid */
3592c42a7b7eSSam Leffler 		(void) ath_hal_stoptxdma(ah, sc->sc_bhalq);
3593c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_RESET,
3594c42a7b7eSSam Leffler 		    "%s: beacon queue %p\n", __func__,
3595c42a7b7eSSam Leffler 		    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq));
3596c42a7b7eSSam Leffler 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
3597c42a7b7eSSam Leffler 			if (ATH_TXQ_SETUP(sc, i))
3598c42a7b7eSSam Leffler 				ath_tx_stopdma(sc, &sc->sc_txq[i]);
3599c42a7b7eSSam Leffler 	}
3600c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
3601c42a7b7eSSam Leffler 		if (ATH_TXQ_SETUP(sc, i))
3602c42a7b7eSSam Leffler 			ath_tx_draintxq(sc, &sc->sc_txq[i]);
36035591b213SSam Leffler 	ifp->if_flags &= ~IFF_OACTIVE;
36045591b213SSam Leffler 	sc->sc_tx_timer = 0;
36055591b213SSam Leffler }
36065591b213SSam Leffler 
36075591b213SSam Leffler /*
36085591b213SSam Leffler  * Disable the receive h/w in preparation for a reset.
36095591b213SSam Leffler  */
36105591b213SSam Leffler static void
36115591b213SSam Leffler ath_stoprecv(struct ath_softc *sc)
36125591b213SSam Leffler {
36138cec0ab9SSam Leffler #define	PA2DESC(_sc, _pa) \
3614c42a7b7eSSam Leffler 	((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
3615c42a7b7eSSam Leffler 		((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
36165591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
36175591b213SSam Leffler 
36185591b213SSam Leffler 	ath_hal_stoppcurecv(ah);	/* disable PCU */
36195591b213SSam Leffler 	ath_hal_setrxfilter(ah, 0);	/* clear recv filter */
36205591b213SSam Leffler 	ath_hal_stopdmarecv(ah);	/* disable DMA engine */
3621c42a7b7eSSam Leffler 	DELAY(3000);			/* 3ms is long enough for 1 frame */
36225591b213SSam Leffler #ifdef AR_DEBUG
3623c42a7b7eSSam Leffler 	if (sc->sc_debug & (ATH_DEBUG_RESET | ATH_DEBUG_FATAL)) {
36245591b213SSam Leffler 		struct ath_buf *bf;
36255591b213SSam Leffler 
3626e325e530SSam Leffler 		printf("%s: rx queue %p, link %p\n", __func__,
362730310634SPeter Wemm 			(caddr_t)(uintptr_t) ath_hal_getrxbuf(ah), sc->sc_rxlink);
3628c42a7b7eSSam Leffler 		STAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
36298cec0ab9SSam Leffler 			struct ath_desc *ds = bf->bf_desc;
3630c42a7b7eSSam Leffler 			HAL_STATUS status = ath_hal_rxprocdesc(ah, ds,
3631c42a7b7eSSam Leffler 				bf->bf_daddr, PA2DESC(sc, ds->ds_link));
3632c42a7b7eSSam Leffler 			if (status == HAL_OK || (sc->sc_debug & ATH_DEBUG_FATAL))
3633c42a7b7eSSam Leffler 				ath_printrxbuf(bf, status == HAL_OK);
36345591b213SSam Leffler 		}
36355591b213SSam Leffler 	}
36365591b213SSam Leffler #endif
36375591b213SSam Leffler 	sc->sc_rxlink = NULL;		/* just in case */
36388cec0ab9SSam Leffler #undef PA2DESC
36395591b213SSam Leffler }
36405591b213SSam Leffler 
36415591b213SSam Leffler /*
36425591b213SSam Leffler  * Enable the receive h/w following a reset.
36435591b213SSam Leffler  */
36445591b213SSam Leffler static int
36455591b213SSam Leffler ath_startrecv(struct ath_softc *sc)
36465591b213SSam Leffler {
36475591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
36485591b213SSam Leffler 	struct ath_buf *bf;
36495591b213SSam Leffler 
36505591b213SSam Leffler 	sc->sc_rxlink = NULL;
3651c42a7b7eSSam Leffler 	STAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
36525591b213SSam Leffler 		int error = ath_rxbuf_init(sc, bf);
36535591b213SSam Leffler 		if (error != 0) {
3654c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_RECV,
3655c42a7b7eSSam Leffler 				"%s: ath_rxbuf_init failed %d\n",
3656c42a7b7eSSam Leffler 				__func__, error);
36575591b213SSam Leffler 			return error;
36585591b213SSam Leffler 		}
36595591b213SSam Leffler 	}
36605591b213SSam Leffler 
3661c42a7b7eSSam Leffler 	bf = STAILQ_FIRST(&sc->sc_rxbuf);
36625591b213SSam Leffler 	ath_hal_putrxbuf(ah, bf->bf_daddr);
36635591b213SSam Leffler 	ath_hal_rxena(ah);		/* enable recv descriptors */
36645591b213SSam Leffler 	ath_mode_init(sc);		/* set filters, etc. */
36655591b213SSam Leffler 	ath_hal_startpcurecv(ah);	/* re-enable PCU/DMA engine */
36665591b213SSam Leffler 	return 0;
36675591b213SSam Leffler }
36685591b213SSam Leffler 
36695591b213SSam Leffler /*
3670c42a7b7eSSam Leffler  * Update internal state after a channel change.
3671c42a7b7eSSam Leffler  */
3672c42a7b7eSSam Leffler static void
3673c42a7b7eSSam Leffler ath_chan_change(struct ath_softc *sc, struct ieee80211_channel *chan)
3674c42a7b7eSSam Leffler {
3675c42a7b7eSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
3676c42a7b7eSSam Leffler 	enum ieee80211_phymode mode;
367716b4851aSSam Leffler 	u_int16_t flags;
3678c42a7b7eSSam Leffler 
3679c42a7b7eSSam Leffler 	/*
3680c42a7b7eSSam Leffler 	 * Change channels and update the h/w rate map
3681c42a7b7eSSam Leffler 	 * if we're switching; e.g. 11a to 11b/g.
3682c42a7b7eSSam Leffler 	 */
3683c42a7b7eSSam Leffler 	mode = ieee80211_chan2mode(ic, chan);
3684c42a7b7eSSam Leffler 	if (mode != sc->sc_curmode)
3685c42a7b7eSSam Leffler 		ath_setcurmode(sc, mode);
3686c42a7b7eSSam Leffler 	/*
368716b4851aSSam Leffler 	 * Update BPF state.  NB: ethereal et. al. don't handle
368816b4851aSSam Leffler 	 * merged flags well so pick a unique mode for their use.
3689c42a7b7eSSam Leffler 	 */
369016b4851aSSam Leffler 	if (IEEE80211_IS_CHAN_A(chan))
369116b4851aSSam Leffler 		flags = IEEE80211_CHAN_A;
369216b4851aSSam Leffler 	/* XXX 11g schizophrenia */
369316b4851aSSam Leffler 	else if (IEEE80211_IS_CHAN_G(chan) ||
369416b4851aSSam Leffler 	    IEEE80211_IS_CHAN_PUREG(chan))
369516b4851aSSam Leffler 		flags = IEEE80211_CHAN_G;
369616b4851aSSam Leffler 	else
369716b4851aSSam Leffler 		flags = IEEE80211_CHAN_B;
369816b4851aSSam Leffler 	if (IEEE80211_IS_CHAN_T(chan))
369916b4851aSSam Leffler 		flags |= IEEE80211_CHAN_TURBO;
3700c42a7b7eSSam Leffler 	sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
3701c42a7b7eSSam Leffler 		htole16(chan->ic_freq);
3702c42a7b7eSSam Leffler 	sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =
370316b4851aSSam Leffler 		htole16(flags);
3704c42a7b7eSSam Leffler }
3705c42a7b7eSSam Leffler 
3706c42a7b7eSSam Leffler /*
37075591b213SSam Leffler  * Set/change channels.  If the channel is really being changed,
3708c42a7b7eSSam Leffler  * it's done by reseting the chip.  To accomplish this we must
37095591b213SSam Leffler  * first cleanup any pending DMA, then restart stuff after a la
37105591b213SSam Leffler  * ath_init.
37115591b213SSam Leffler  */
37125591b213SSam Leffler static int
37135591b213SSam Leffler ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan)
37145591b213SSam Leffler {
37155591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
37165591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
37175591b213SSam Leffler 	HAL_CHANNEL hchan;
3718c42a7b7eSSam Leffler 
3719c42a7b7eSSam Leffler 	/*
3720c42a7b7eSSam Leffler 	 * Convert to a HAL channel description with
3721c42a7b7eSSam Leffler 	 * the flags constrained to reflect the current
3722c42a7b7eSSam Leffler 	 * operating mode.
3723c42a7b7eSSam Leffler 	 */
3724c42a7b7eSSam Leffler 	hchan.channel = chan->ic_freq;
3725c42a7b7eSSam Leffler 	hchan.channelFlags = ath_chan2flags(ic, chan);
3726c42a7b7eSSam Leffler 
3727c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %u (%u MHz) -> %u (%u MHz)\n",
3728c42a7b7eSSam Leffler 	    __func__,
3729c42a7b7eSSam Leffler 	    ath_hal_mhz2ieee(sc->sc_curchan.channel,
3730c42a7b7eSSam Leffler 		sc->sc_curchan.channelFlags),
3731c42a7b7eSSam Leffler 	    	sc->sc_curchan.channel,
3732c42a7b7eSSam Leffler 	    ath_hal_mhz2ieee(hchan.channel, hchan.channelFlags), hchan.channel);
3733c42a7b7eSSam Leffler 	if (hchan.channel != sc->sc_curchan.channel ||
3734c42a7b7eSSam Leffler 	    hchan.channelFlags != sc->sc_curchan.channelFlags) {
3735c42a7b7eSSam Leffler 		HAL_STATUS status;
37365591b213SSam Leffler 
37375591b213SSam Leffler 		/*
37385591b213SSam Leffler 		 * To switch channels clear any pending DMA operations;
37395591b213SSam Leffler 		 * wait long enough for the RX fifo to drain, reset the
37405591b213SSam Leffler 		 * hardware at the new frequency, and then re-enable
37415591b213SSam Leffler 		 * the relevant bits of the h/w.
37425591b213SSam Leffler 		 */
37435591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
37445591b213SSam Leffler 		ath_draintxq(sc);		/* clear pending tx frames */
37455591b213SSam Leffler 		ath_stoprecv(sc);		/* turn off frame recv */
37465591b213SSam Leffler 		if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_TRUE, &status)) {
3747c42a7b7eSSam Leffler 			if_printf(ic->ic_ifp, "ath_chan_set: unable to reset "
37485591b213SSam Leffler 				"channel %u (%u Mhz)\n",
37495591b213SSam Leffler 				ieee80211_chan2ieee(ic, chan), chan->ic_freq);
37505591b213SSam Leffler 			return EIO;
37515591b213SSam Leffler 		}
3752c42a7b7eSSam Leffler 		sc->sc_curchan = hchan;
3753c42a7b7eSSam Leffler 		ath_update_txpow(sc);		/* update tx power state */
3754c42a7b7eSSam Leffler 
37555591b213SSam Leffler 		/*
37565591b213SSam Leffler 		 * Re-enable rx framework.
37575591b213SSam Leffler 		 */
37585591b213SSam Leffler 		if (ath_startrecv(sc) != 0) {
3759c42a7b7eSSam Leffler 			if_printf(ic->ic_ifp,
37605591b213SSam Leffler 				"ath_chan_set: unable to restart recv logic\n");
37615591b213SSam Leffler 			return EIO;
37625591b213SSam Leffler 		}
37635591b213SSam Leffler 
37645591b213SSam Leffler 		/*
37655591b213SSam Leffler 		 * Change channels and update the h/w rate map
37665591b213SSam Leffler 		 * if we're switching; e.g. 11a to 11b/g.
37675591b213SSam Leffler 		 */
37685591b213SSam Leffler 		ic->ic_ibss_chan = chan;
3769c42a7b7eSSam Leffler 		ath_chan_change(sc, chan);
37700a915fadSSam Leffler 
37710a915fadSSam Leffler 		/*
37720a915fadSSam Leffler 		 * Re-enable interrupts.
37730a915fadSSam Leffler 		 */
37740a915fadSSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
37755591b213SSam Leffler 	}
37765591b213SSam Leffler 	return 0;
37775591b213SSam Leffler }
37785591b213SSam Leffler 
37795591b213SSam Leffler static void
37805591b213SSam Leffler ath_next_scan(void *arg)
37815591b213SSam Leffler {
37825591b213SSam Leffler 	struct ath_softc *sc = arg;
37835591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
37845591b213SSam Leffler 
37855591b213SSam Leffler 	if (ic->ic_state == IEEE80211_S_SCAN)
3786c42a7b7eSSam Leffler 		ieee80211_next_scan(ic);
37875591b213SSam Leffler }
37885591b213SSam Leffler 
37895591b213SSam Leffler /*
37905591b213SSam Leffler  * Periodically recalibrate the PHY to account
37915591b213SSam Leffler  * for temperature/environment changes.
37925591b213SSam Leffler  */
37935591b213SSam Leffler static void
37945591b213SSam Leffler ath_calibrate(void *arg)
37955591b213SSam Leffler {
37965591b213SSam Leffler 	struct ath_softc *sc = arg;
37975591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
37985591b213SSam Leffler 
37995591b213SSam Leffler 	sc->sc_stats.ast_per_cal++;
38005591b213SSam Leffler 
3801c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: channel %u/%x\n",
3802c42a7b7eSSam Leffler 		__func__, sc->sc_curchan.channel, sc->sc_curchan.channelFlags);
38035591b213SSam Leffler 
38045591b213SSam Leffler 	if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) {
38055591b213SSam Leffler 		/*
38065591b213SSam Leffler 		 * Rfgain is out of bounds, reset the chip
38075591b213SSam Leffler 		 * to load new gain values.
38085591b213SSam Leffler 		 */
38095591b213SSam Leffler 		sc->sc_stats.ast_per_rfgain++;
3810c42a7b7eSSam Leffler 		ath_reset(&sc->sc_if);
38115591b213SSam Leffler 	}
3812c42a7b7eSSam Leffler 	if (!ath_hal_calibrate(ah, &sc->sc_curchan)) {
3813c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY,
3814c42a7b7eSSam Leffler 			"%s: calibration of channel %u failed\n",
3815c42a7b7eSSam Leffler 			__func__, sc->sc_curchan.channel);
38165591b213SSam Leffler 		sc->sc_stats.ast_per_calfail++;
38175591b213SSam Leffler 	}
3818c42a7b7eSSam Leffler 	callout_reset(&sc->sc_cal_ch, ath_calinterval * hz, ath_calibrate, sc);
38195591b213SSam Leffler }
38205591b213SSam Leffler 
38215591b213SSam Leffler static int
382245bbf62fSSam Leffler ath_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
38235591b213SSam Leffler {
3824c42a7b7eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
382545bbf62fSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
382645bbf62fSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
38275591b213SSam Leffler 	struct ieee80211_node *ni;
38285591b213SSam Leffler 	int i, error;
38298cec0ab9SSam Leffler 	const u_int8_t *bssid;
38305591b213SSam Leffler 	u_int32_t rfilt;
38315591b213SSam Leffler 	static const HAL_LED_STATE leds[] = {
38325591b213SSam Leffler 	    HAL_LED_INIT,	/* IEEE80211_S_INIT */
38335591b213SSam Leffler 	    HAL_LED_SCAN,	/* IEEE80211_S_SCAN */
38345591b213SSam Leffler 	    HAL_LED_AUTH,	/* IEEE80211_S_AUTH */
38355591b213SSam Leffler 	    HAL_LED_ASSOC, 	/* IEEE80211_S_ASSOC */
38365591b213SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_RUN */
38375591b213SSam Leffler 	};
38385591b213SSam Leffler 
3839c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: %s -> %s\n", __func__,
384045bbf62fSSam Leffler 		ieee80211_state_name[ic->ic_state],
3841c42a7b7eSSam Leffler 		ieee80211_state_name[nstate]);
38425591b213SSam Leffler 
3843c42a7b7eSSam Leffler 	callout_stop(&sc->sc_scan_ch);
3844c42a7b7eSSam Leffler 	callout_stop(&sc->sc_cal_ch);
38455591b213SSam Leffler 	ath_hal_setledstate(ah, leds[nstate]);	/* set LED */
38465591b213SSam Leffler 
38475591b213SSam Leffler 	if (nstate == IEEE80211_S_INIT) {
38485591b213SSam Leffler 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
38495591b213SSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
3850c42a7b7eSSam Leffler 		/*
3851c42a7b7eSSam Leffler 		 * Notify the rate control algorithm.
3852c42a7b7eSSam Leffler 		 */
3853c42a7b7eSSam Leffler 		ath_rate_newstate(sc, nstate);
3854c42a7b7eSSam Leffler 		goto done;
38555591b213SSam Leffler 	}
38565591b213SSam Leffler 	ni = ic->ic_bss;
38575591b213SSam Leffler 	error = ath_chan_set(sc, ni->ni_chan);
38585591b213SSam Leffler 	if (error != 0)
38595591b213SSam Leffler 		goto bad;
3860c42a7b7eSSam Leffler 	rfilt = ath_calcrxfilter(sc, nstate);
3861c42a7b7eSSam Leffler 	if (nstate == IEEE80211_S_SCAN)
38625591b213SSam Leffler 		bssid = ifp->if_broadcastaddr;
3863c42a7b7eSSam Leffler 	else
38645591b213SSam Leffler 		bssid = ni->ni_bssid;
38655591b213SSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
3866c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s\n",
3867c42a7b7eSSam Leffler 		 __func__, rfilt, ether_sprintf(bssid));
38685591b213SSam Leffler 
38695591b213SSam Leffler 	if (nstate == IEEE80211_S_RUN && ic->ic_opmode == IEEE80211_M_STA)
38705591b213SSam Leffler 		ath_hal_setassocid(ah, bssid, ni->ni_associd);
38715591b213SSam Leffler 	else
38725591b213SSam Leffler 		ath_hal_setassocid(ah, bssid, 0);
3873c42a7b7eSSam Leffler 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
38745591b213SSam Leffler 		for (i = 0; i < IEEE80211_WEP_NKID; i++)
38755591b213SSam Leffler 			if (ath_hal_keyisvalid(ah, i))
38765591b213SSam Leffler 				ath_hal_keysetmac(ah, i, bssid);
38775591b213SSam Leffler 	}
38785591b213SSam Leffler 
3879c42a7b7eSSam Leffler 	/*
3880c42a7b7eSSam Leffler 	 * Notify the rate control algorithm so rates
3881c42a7b7eSSam Leffler 	 * are setup should ath_beacon_alloc be called.
3882c42a7b7eSSam Leffler 	 */
3883c42a7b7eSSam Leffler 	ath_rate_newstate(sc, nstate);
3884c42a7b7eSSam Leffler 
3885c42a7b7eSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_MONITOR) {
3886c42a7b7eSSam Leffler 		/* nothing to do */;
3887c42a7b7eSSam Leffler 	} else if (nstate == IEEE80211_S_RUN) {
3888c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_STATE,
3889c42a7b7eSSam Leffler 			"%s(RUN): ic_flags=0x%08x iv=%d bssid=%s "
38905591b213SSam Leffler 			"capinfo=0x%04x chan=%d\n"
38915591b213SSam Leffler 			 , __func__
38925591b213SSam Leffler 			 , ic->ic_flags
38935591b213SSam Leffler 			 , ni->ni_intval
38945591b213SSam Leffler 			 , ether_sprintf(ni->ni_bssid)
38955591b213SSam Leffler 			 , ni->ni_capinfo
3896c42a7b7eSSam Leffler 			 , ieee80211_chan2ieee(ic, ni->ni_chan));
38975591b213SSam Leffler 
38985591b213SSam Leffler 		/*
38995591b213SSam Leffler 		 * Allocate and setup the beacon frame for AP or adhoc mode.
39005591b213SSam Leffler 		 */
39016b59f5e3SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
39026b59f5e3SSam Leffler 		    ic->ic_opmode == IEEE80211_M_IBSS) {
39035591b213SSam Leffler 			error = ath_beacon_alloc(sc, ni);
39045591b213SSam Leffler 			if (error != 0)
39055591b213SSam Leffler 				goto bad;
39065591b213SSam Leffler 		}
39075591b213SSam Leffler 
39085591b213SSam Leffler 		/*
39095591b213SSam Leffler 		 * Configure the beacon and sleep timers.
39105591b213SSam Leffler 		 */
39115591b213SSam Leffler 		ath_beacon_config(sc);
39125591b213SSam Leffler 	} else {
3913c42a7b7eSSam Leffler 		ath_hal_intrset(ah,
3914c42a7b7eSSam Leffler 			sc->sc_imask &~ (HAL_INT_SWBA | HAL_INT_BMISS));
39155591b213SSam Leffler 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
39165591b213SSam Leffler 	}
3917c42a7b7eSSam Leffler done:
391845bbf62fSSam Leffler 	/*
391945bbf62fSSam Leffler 	 * Invoke the parent method to complete the work.
392045bbf62fSSam Leffler 	 */
3921c42a7b7eSSam Leffler 	error = sc->sc_newstate(ic, nstate, arg);
3922c42a7b7eSSam Leffler 	/*
3923c42a7b7eSSam Leffler 	 * Finally, start any timers.
3924c42a7b7eSSam Leffler 	 */
3925c42a7b7eSSam Leffler 	if (nstate == IEEE80211_S_RUN) {
3926c42a7b7eSSam Leffler 		/* start periodic recalibration timer */
3927c42a7b7eSSam Leffler 		callout_reset(&sc->sc_cal_ch, ath_calinterval * hz,
3928c42a7b7eSSam Leffler 			ath_calibrate, sc);
3929c42a7b7eSSam Leffler 	} else if (nstate == IEEE80211_S_SCAN) {
3930c42a7b7eSSam Leffler 		/* start ap/neighbor scan timer */
3931c42a7b7eSSam Leffler 		callout_reset(&sc->sc_scan_ch, (ath_dwelltime * hz) / 1000,
3932c42a7b7eSSam Leffler 			ath_next_scan, sc);
3933c42a7b7eSSam Leffler 	}
39345591b213SSam Leffler bad:
39355591b213SSam Leffler 	return error;
39365591b213SSam Leffler }
39375591b213SSam Leffler 
39385591b213SSam Leffler /*
39395591b213SSam Leffler  * Setup driver-specific state for a newly associated node.
39405591b213SSam Leffler  * Note that we're called also on a re-associate, the isnew
39415591b213SSam Leffler  * param tells us if this is the first time or not.
39425591b213SSam Leffler  */
39435591b213SSam Leffler static void
39445591b213SSam Leffler ath_newassoc(struct ieee80211com *ic, struct ieee80211_node *ni, int isnew)
39455591b213SSam Leffler {
3946c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
39475591b213SSam Leffler 
3948c42a7b7eSSam Leffler 	ath_rate_newassoc(sc, ATH_NODE(ni), isnew);
39495591b213SSam Leffler }
39505591b213SSam Leffler 
39515591b213SSam Leffler static int
3952c42a7b7eSSam Leffler ath_getchannels(struct ath_softc *sc, u_int cc,
3953c42a7b7eSSam Leffler 	HAL_BOOL outdoor, HAL_BOOL xchanmode)
39545591b213SSam Leffler {
39555591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
3956c42a7b7eSSam Leffler 	struct ifnet *ifp = &sc->sc_if;
39575591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
39585591b213SSam Leffler 	HAL_CHANNEL *chans;
39595591b213SSam Leffler 	int i, ix, nchan;
39605591b213SSam Leffler 
39615591b213SSam Leffler 	chans = malloc(IEEE80211_CHAN_MAX * sizeof(HAL_CHANNEL),
39625591b213SSam Leffler 			M_TEMP, M_NOWAIT);
39635591b213SSam Leffler 	if (chans == NULL) {
39645591b213SSam Leffler 		if_printf(ifp, "unable to allocate channel table\n");
39655591b213SSam Leffler 		return ENOMEM;
39665591b213SSam Leffler 	}
39675591b213SSam Leffler 	if (!ath_hal_init_channels(ah, chans, IEEE80211_CHAN_MAX, &nchan,
3968c42a7b7eSSam Leffler 	    cc, HAL_MODE_ALL, outdoor, xchanmode)) {
3969c42a7b7eSSam Leffler 		u_int32_t rd;
3970c42a7b7eSSam Leffler 
3971c42a7b7eSSam Leffler 		ath_hal_getregdomain(ah, &rd);
3972c42a7b7eSSam Leffler 		if_printf(ifp, "unable to collect channel list from hal; "
3973c42a7b7eSSam Leffler 			"regdomain likely %u country code %u\n", rd, cc);
39745591b213SSam Leffler 		free(chans, M_TEMP);
39755591b213SSam Leffler 		return EINVAL;
39765591b213SSam Leffler 	}
39775591b213SSam Leffler 
39785591b213SSam Leffler 	/*
39795591b213SSam Leffler 	 * Convert HAL channels to ieee80211 ones and insert
39805591b213SSam Leffler 	 * them in the table according to their channel number.
39815591b213SSam Leffler 	 */
39825591b213SSam Leffler 	for (i = 0; i < nchan; i++) {
39835591b213SSam Leffler 		HAL_CHANNEL *c = &chans[i];
39845591b213SSam Leffler 		ix = ath_hal_mhz2ieee(c->channel, c->channelFlags);
39855591b213SSam Leffler 		if (ix > IEEE80211_CHAN_MAX) {
39865591b213SSam Leffler 			if_printf(ifp, "bad hal channel %u (%u/%x) ignored\n",
39875591b213SSam Leffler 				ix, c->channel, c->channelFlags);
39885591b213SSam Leffler 			continue;
39895591b213SSam Leffler 		}
39905591b213SSam Leffler 		/* NB: flags are known to be compatible */
39915591b213SSam Leffler 		if (ic->ic_channels[ix].ic_freq == 0) {
39925591b213SSam Leffler 			ic->ic_channels[ix].ic_freq = c->channel;
39935591b213SSam Leffler 			ic->ic_channels[ix].ic_flags = c->channelFlags;
39945591b213SSam Leffler 		} else {
39955591b213SSam Leffler 			/* channels overlap; e.g. 11g and 11b */
39965591b213SSam Leffler 			ic->ic_channels[ix].ic_flags |= c->channelFlags;
39975591b213SSam Leffler 		}
39985591b213SSam Leffler 	}
39995591b213SSam Leffler 	free(chans, M_TEMP);
40005591b213SSam Leffler 	return 0;
40015591b213SSam Leffler }
40025591b213SSam Leffler 
4003c42a7b7eSSam Leffler static void
4004c42a7b7eSSam Leffler ath_update_led(struct ath_softc *sc)
4005c42a7b7eSSam Leffler {
4006c42a7b7eSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
4007c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4008c42a7b7eSSam Leffler 	u_int32_t threshold;
4009c42a7b7eSSam Leffler 
4010c42a7b7eSSam Leffler 	/*
4011c42a7b7eSSam Leffler 	 * When not associated, flash LED on for 5s, off for 200ms.
4012c42a7b7eSSam Leffler 	 * XXX this assumes 100ms beacon interval.
4013c42a7b7eSSam Leffler 	 */
4014c42a7b7eSSam Leffler 	if (ic->ic_state != IEEE80211_S_RUN) {
4015c42a7b7eSSam Leffler 		threshold = 2 + sc->sc_ledstate * 48;
4016c42a7b7eSSam Leffler 	} else {
4017c42a7b7eSSam Leffler 		threshold = 2 + sc->sc_ledstate * 18;
4018c42a7b7eSSam Leffler 	}
4019c42a7b7eSSam Leffler 	if (ic->ic_stats.is_rx_beacon - sc->sc_beacons >= threshold) {
4020c42a7b7eSSam Leffler 		ath_hal_gpioCfgOutput(ah, sc->sc_ledpin);
4021c42a7b7eSSam Leffler 		ath_hal_gpioset(ah, sc->sc_ledpin, sc->sc_ledstate);
4022c42a7b7eSSam Leffler 		sc->sc_ledstate ^= 1;
4023c42a7b7eSSam Leffler 		sc->sc_beacons = ic->ic_stats.is_rx_beacon;
4024c42a7b7eSSam Leffler 	}
4025c42a7b7eSSam Leffler }
4026c42a7b7eSSam Leffler 
4027c42a7b7eSSam Leffler static void
4028c42a7b7eSSam Leffler ath_update_txpow(struct ath_softc *sc)
4029c42a7b7eSSam Leffler {
4030c42a7b7eSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
4031c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4032c42a7b7eSSam Leffler 	u_int32_t txpow;
4033c42a7b7eSSam Leffler 
4034c42a7b7eSSam Leffler 	if (sc->sc_curtxpow != ic->ic_txpowlimit) {
4035c42a7b7eSSam Leffler 		ath_hal_settxpowlimit(ah, ic->ic_txpowlimit);
4036c42a7b7eSSam Leffler 		/* read back in case value is clamped */
4037c42a7b7eSSam Leffler 		ath_hal_gettxpowlimit(ah, &txpow);
4038c42a7b7eSSam Leffler 		ic->ic_txpowlimit = sc->sc_curtxpow = txpow;
4039c42a7b7eSSam Leffler 	}
4040c42a7b7eSSam Leffler 	/*
4041c42a7b7eSSam Leffler 	 * Fetch max tx power level for status requests.
4042c42a7b7eSSam Leffler 	 */
4043c42a7b7eSSam Leffler 	ath_hal_getmaxtxpow(sc->sc_ah, &txpow);
4044c42a7b7eSSam Leffler 	ic->ic_bss->ni_txpower = txpow;
4045c42a7b7eSSam Leffler }
4046c42a7b7eSSam Leffler 
40475591b213SSam Leffler static int
40485591b213SSam Leffler ath_rate_setup(struct ath_softc *sc, u_int mode)
40495591b213SSam Leffler {
40505591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
40515591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
40525591b213SSam Leffler 	const HAL_RATE_TABLE *rt;
40535591b213SSam Leffler 	struct ieee80211_rateset *rs;
40545591b213SSam Leffler 	int i, maxrates;
40555591b213SSam Leffler 
40565591b213SSam Leffler 	switch (mode) {
40575591b213SSam Leffler 	case IEEE80211_MODE_11A:
40585591b213SSam Leffler 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11A);
40595591b213SSam Leffler 		break;
40605591b213SSam Leffler 	case IEEE80211_MODE_11B:
40615591b213SSam Leffler 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11B);
40625591b213SSam Leffler 		break;
40635591b213SSam Leffler 	case IEEE80211_MODE_11G:
40645591b213SSam Leffler 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11G);
40655591b213SSam Leffler 		break;
4066c42a7b7eSSam Leffler 	case IEEE80211_MODE_TURBO_A:
40675591b213SSam Leffler 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_TURBO);
40685591b213SSam Leffler 		break;
4069c42a7b7eSSam Leffler 	case IEEE80211_MODE_TURBO_G:
4070c42a7b7eSSam Leffler 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_108G);
4071c42a7b7eSSam Leffler 		break;
40725591b213SSam Leffler 	default:
4073c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid mode %u\n",
4074c42a7b7eSSam Leffler 			__func__, mode);
40755591b213SSam Leffler 		return 0;
40765591b213SSam Leffler 	}
40775591b213SSam Leffler 	rt = sc->sc_rates[mode];
40785591b213SSam Leffler 	if (rt == NULL)
40795591b213SSam Leffler 		return 0;
40805591b213SSam Leffler 	if (rt->rateCount > IEEE80211_RATE_MAXSIZE) {
4081c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY,
4082c42a7b7eSSam Leffler 			"%s: rate table too small (%u > %u)\n",
4083c42a7b7eSSam Leffler 			__func__, rt->rateCount, IEEE80211_RATE_MAXSIZE);
40845591b213SSam Leffler 		maxrates = IEEE80211_RATE_MAXSIZE;
40855591b213SSam Leffler 	} else
40865591b213SSam Leffler 		maxrates = rt->rateCount;
40875591b213SSam Leffler 	rs = &ic->ic_sup_rates[mode];
40885591b213SSam Leffler 	for (i = 0; i < maxrates; i++)
40895591b213SSam Leffler 		rs->rs_rates[i] = rt->info[i].dot11Rate;
40905591b213SSam Leffler 	rs->rs_nrates = maxrates;
40915591b213SSam Leffler 	return 1;
40925591b213SSam Leffler }
40935591b213SSam Leffler 
40945591b213SSam Leffler static void
40955591b213SSam Leffler ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode)
40965591b213SSam Leffler {
40975591b213SSam Leffler 	const HAL_RATE_TABLE *rt;
40985591b213SSam Leffler 	int i;
40995591b213SSam Leffler 
41005591b213SSam Leffler 	memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap));
41015591b213SSam Leffler 	rt = sc->sc_rates[mode];
41025591b213SSam Leffler 	KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode));
41035591b213SSam Leffler 	for (i = 0; i < rt->rateCount; i++)
41045591b213SSam Leffler 		sc->sc_rixmap[rt->info[i].dot11Rate & IEEE80211_RATE_VAL] = i;
41051b1a8e41SSam Leffler 	memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap));
410616b4851aSSam Leffler 	memset(sc->sc_hwflags, 0, sizeof(sc->sc_hwflags));
4107c42a7b7eSSam Leffler 	for (i = 0; i < 32; i++) {
4108c42a7b7eSSam Leffler 		u_int8_t ix = rt->rateCodeToIndex[i];
410916b4851aSSam Leffler 		if (ix == 0xff)
411016b4851aSSam Leffler 			continue;
4111c42a7b7eSSam Leffler 		sc->sc_hwmap[i] = rt->info[ix].dot11Rate & IEEE80211_RATE_VAL;
411216b4851aSSam Leffler 		if (rt->info[ix].shortPreamble ||
411316b4851aSSam Leffler 		    rt->info[ix].phy == IEEE80211_T_OFDM)
411416b4851aSSam Leffler 			sc->sc_hwflags[i] |= IEEE80211_RADIOTAP_F_SHORTPRE;
4115c42a7b7eSSam Leffler 	}
41165591b213SSam Leffler 	sc->sc_currates = rt;
41175591b213SSam Leffler 	sc->sc_curmode = mode;
41185591b213SSam Leffler 	/*
4119c42a7b7eSSam Leffler 	 * All protection frames are transmited at 2Mb/s for
4120c42a7b7eSSam Leffler 	 * 11g, otherwise at 1Mb/s.
4121c42a7b7eSSam Leffler 	 * XXX select protection rate index from rate table.
41225591b213SSam Leffler 	 */
4123c42a7b7eSSam Leffler 	sc->sc_protrix = (mode == IEEE80211_MODE_11G ? 1 : 0);
4124c42a7b7eSSam Leffler 	/* NB: caller is responsible for reseting rate control state */
41255591b213SSam Leffler }
41265591b213SSam Leffler 
41275591b213SSam Leffler #ifdef AR_DEBUG
41285591b213SSam Leffler static void
41295591b213SSam Leffler ath_printrxbuf(struct ath_buf *bf, int done)
41305591b213SSam Leffler {
41315591b213SSam Leffler 	struct ath_desc *ds;
41325591b213SSam Leffler 	int i;
41335591b213SSam Leffler 
41345591b213SSam Leffler 	for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) {
41355591b213SSam Leffler 		printf("R%d (%p %p) %08x %08x %08x %08x %08x %08x %c\n",
41365591b213SSam Leffler 		    i, ds, (struct ath_desc *)bf->bf_daddr + i,
41375591b213SSam Leffler 		    ds->ds_link, ds->ds_data,
41385591b213SSam Leffler 		    ds->ds_ctl0, ds->ds_ctl1,
41395591b213SSam Leffler 		    ds->ds_hw[0], ds->ds_hw[1],
41405591b213SSam Leffler 		    !done ? ' ' : (ds->ds_rxstat.rs_status == 0) ? '*' : '!');
41415591b213SSam Leffler 	}
41425591b213SSam Leffler }
41435591b213SSam Leffler 
41445591b213SSam Leffler static void
41455591b213SSam Leffler ath_printtxbuf(struct ath_buf *bf, int done)
41465591b213SSam Leffler {
41475591b213SSam Leffler 	struct ath_desc *ds;
41485591b213SSam Leffler 	int i;
41495591b213SSam Leffler 
41505591b213SSam Leffler 	for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) {
41515591b213SSam Leffler 		printf("T%d (%p %p) %08x %08x %08x %08x %08x %08x %08x %08x %c\n",
41525591b213SSam Leffler 		    i, ds, (struct ath_desc *)bf->bf_daddr + i,
41535591b213SSam Leffler 		    ds->ds_link, ds->ds_data,
41545591b213SSam Leffler 		    ds->ds_ctl0, ds->ds_ctl1,
41555591b213SSam Leffler 		    ds->ds_hw[0], ds->ds_hw[1], ds->ds_hw[2], ds->ds_hw[3],
41565591b213SSam Leffler 		    !done ? ' ' : (ds->ds_txstat.ts_status == 0) ? '*' : '!');
41575591b213SSam Leffler 	}
41585591b213SSam Leffler }
41595591b213SSam Leffler #endif /* AR_DEBUG */
4160c42a7b7eSSam Leffler 
4161c42a7b7eSSam Leffler static void
4162c42a7b7eSSam Leffler ath_watchdog(struct ifnet *ifp)
4163c42a7b7eSSam Leffler {
4164c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
4165c42a7b7eSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
4166c42a7b7eSSam Leffler 
4167c42a7b7eSSam Leffler 	ifp->if_timer = 0;
4168c42a7b7eSSam Leffler 	if ((ifp->if_flags & IFF_RUNNING) == 0 || sc->sc_invalid)
4169c42a7b7eSSam Leffler 		return;
4170c42a7b7eSSam Leffler 	if (sc->sc_tx_timer) {
4171c42a7b7eSSam Leffler 		if (--sc->sc_tx_timer == 0) {
4172c42a7b7eSSam Leffler 			if_printf(ifp, "device timeout\n");
4173c42a7b7eSSam Leffler 			ath_reset(ifp);
4174c42a7b7eSSam Leffler 			ifp->if_oerrors++;
4175c42a7b7eSSam Leffler 			sc->sc_stats.ast_watchdog++;
4176c42a7b7eSSam Leffler 		} else
4177c42a7b7eSSam Leffler 			ifp->if_timer = 1;
4178c42a7b7eSSam Leffler 	}
4179c42a7b7eSSam Leffler 	ieee80211_watchdog(ic);
4180c42a7b7eSSam Leffler }
4181c42a7b7eSSam Leffler 
4182c42a7b7eSSam Leffler /*
4183c42a7b7eSSam Leffler  * Diagnostic interface to the HAL.  This is used by various
4184c42a7b7eSSam Leffler  * tools to do things like retrieve register contents for
4185c42a7b7eSSam Leffler  * debugging.  The mechanism is intentionally opaque so that
4186c42a7b7eSSam Leffler  * it can change frequently w/o concern for compatiblity.
4187c42a7b7eSSam Leffler  */
4188c42a7b7eSSam Leffler static int
4189c42a7b7eSSam Leffler ath_ioctl_diag(struct ath_softc *sc, struct ath_diag *ad)
4190c42a7b7eSSam Leffler {
4191c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4192c42a7b7eSSam Leffler 	u_int id = ad->ad_id & ATH_DIAG_ID;
4193c42a7b7eSSam Leffler 	void *indata = NULL;
4194c42a7b7eSSam Leffler 	void *outdata = NULL;
4195c42a7b7eSSam Leffler 	u_int32_t insize = ad->ad_in_size;
4196c42a7b7eSSam Leffler 	u_int32_t outsize = ad->ad_out_size;
4197c42a7b7eSSam Leffler 	int error = 0;
4198c42a7b7eSSam Leffler 
4199c42a7b7eSSam Leffler 	if (ad->ad_id & ATH_DIAG_IN) {
4200c42a7b7eSSam Leffler 		/*
4201c42a7b7eSSam Leffler 		 * Copy in data.
4202c42a7b7eSSam Leffler 		 */
4203c42a7b7eSSam Leffler 		indata = malloc(insize, M_TEMP, M_NOWAIT);
4204c42a7b7eSSam Leffler 		if (indata == NULL) {
4205c42a7b7eSSam Leffler 			error = ENOMEM;
4206c42a7b7eSSam Leffler 			goto bad;
4207c42a7b7eSSam Leffler 		}
4208c42a7b7eSSam Leffler 		error = copyin(ad->ad_in_data, indata, insize);
4209c42a7b7eSSam Leffler 		if (error)
4210c42a7b7eSSam Leffler 			goto bad;
4211c42a7b7eSSam Leffler 	}
4212c42a7b7eSSam Leffler 	if (ad->ad_id & ATH_DIAG_DYN) {
4213c42a7b7eSSam Leffler 		/*
4214c42a7b7eSSam Leffler 		 * Allocate a buffer for the results (otherwise the HAL
4215c42a7b7eSSam Leffler 		 * returns a pointer to a buffer where we can read the
4216c42a7b7eSSam Leffler 		 * results).  Note that we depend on the HAL leaving this
4217c42a7b7eSSam Leffler 		 * pointer for us to use below in reclaiming the buffer;
4218c42a7b7eSSam Leffler 		 * may want to be more defensive.
4219c42a7b7eSSam Leffler 		 */
4220c42a7b7eSSam Leffler 		outdata = malloc(outsize, M_TEMP, M_NOWAIT);
4221c42a7b7eSSam Leffler 		if (outdata == NULL) {
4222c42a7b7eSSam Leffler 			error = ENOMEM;
4223c42a7b7eSSam Leffler 			goto bad;
4224c42a7b7eSSam Leffler 		}
4225c42a7b7eSSam Leffler 	}
4226c42a7b7eSSam Leffler 	if (ath_hal_getdiagstate(ah, id, indata, insize, &outdata, &outsize)) {
4227c42a7b7eSSam Leffler 		if (outsize < ad->ad_out_size)
4228c42a7b7eSSam Leffler 			ad->ad_out_size = outsize;
4229c42a7b7eSSam Leffler 		if (outdata != NULL)
4230c42a7b7eSSam Leffler 			error = copyout(outdata, ad->ad_out_data,
4231c42a7b7eSSam Leffler 					ad->ad_out_size);
4232c42a7b7eSSam Leffler 	} else {
4233c42a7b7eSSam Leffler 		error = EINVAL;
4234c42a7b7eSSam Leffler 	}
4235c42a7b7eSSam Leffler bad:
4236c42a7b7eSSam Leffler 	if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
4237c42a7b7eSSam Leffler 		free(indata, M_TEMP);
4238c42a7b7eSSam Leffler 	if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
4239c42a7b7eSSam Leffler 		free(outdata, M_TEMP);
4240c42a7b7eSSam Leffler 	return error;
4241c42a7b7eSSam Leffler }
4242c42a7b7eSSam Leffler 
4243c42a7b7eSSam Leffler static int
4244c42a7b7eSSam Leffler ath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
4245c42a7b7eSSam Leffler {
4246c42a7b7eSSam Leffler #define	IS_RUNNING(ifp) \
4247c42a7b7eSSam Leffler 	((ifp->if_flags & (IFF_RUNNING|IFF_UP)) == (IFF_RUNNING|IFF_UP))
4248c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
4249c42a7b7eSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
4250c42a7b7eSSam Leffler 	struct ifreq *ifr = (struct ifreq *)data;
4251c42a7b7eSSam Leffler 	int error = 0;
4252c42a7b7eSSam Leffler 
4253c42a7b7eSSam Leffler 	ATH_LOCK(sc);
4254c42a7b7eSSam Leffler 	switch (cmd) {
4255c42a7b7eSSam Leffler 	case SIOCSIFFLAGS:
4256c42a7b7eSSam Leffler 		if (IS_RUNNING(ifp)) {
4257c42a7b7eSSam Leffler 			/*
4258c42a7b7eSSam Leffler 			 * To avoid rescanning another access point,
4259c42a7b7eSSam Leffler 			 * do not call ath_init() here.  Instead,
4260c42a7b7eSSam Leffler 			 * only reflect promisc mode settings.
4261c42a7b7eSSam Leffler 			 */
4262c42a7b7eSSam Leffler 			ath_mode_init(sc);
4263c42a7b7eSSam Leffler 		} else if (ifp->if_flags & IFF_UP) {
4264c42a7b7eSSam Leffler 			/*
4265c42a7b7eSSam Leffler 			 * Beware of being called during attach/detach
4266c42a7b7eSSam Leffler 			 * to reset promiscuous mode.  In that case we
4267c42a7b7eSSam Leffler 			 * will still be marked UP but not RUNNING.
4268c42a7b7eSSam Leffler 			 * However trying to re-init the interface
4269c42a7b7eSSam Leffler 			 * is the wrong thing to do as we've already
4270c42a7b7eSSam Leffler 			 * torn down much of our state.  There's
4271c42a7b7eSSam Leffler 			 * probably a better way to deal with this.
4272c42a7b7eSSam Leffler 			 */
4273c42a7b7eSSam Leffler 			if (!sc->sc_invalid && ic->ic_bss != NULL)
4274c42a7b7eSSam Leffler 				ath_init(ifp);	/* XXX lose error */
4275c42a7b7eSSam Leffler 		} else
4276c42a7b7eSSam Leffler 			ath_stop_locked(ifp);
4277c42a7b7eSSam Leffler 		break;
4278c42a7b7eSSam Leffler 	case SIOCADDMULTI:
4279c42a7b7eSSam Leffler 	case SIOCDELMULTI:
4280c42a7b7eSSam Leffler 		/*
4281c42a7b7eSSam Leffler 		 * The upper layer has already installed/removed
4282c42a7b7eSSam Leffler 		 * the multicast address(es), just recalculate the
4283c42a7b7eSSam Leffler 		 * multicast filter for the card.
4284c42a7b7eSSam Leffler 		 */
4285c42a7b7eSSam Leffler 		if (ifp->if_flags & IFF_RUNNING)
4286c42a7b7eSSam Leffler 			ath_mode_init(sc);
4287c42a7b7eSSam Leffler 		break;
4288c42a7b7eSSam Leffler 	case SIOCGATHSTATS:
4289c42a7b7eSSam Leffler 		/* NB: embed these numbers to get a consistent view */
4290c42a7b7eSSam Leffler 		sc->sc_stats.ast_tx_packets = ifp->if_opackets;
4291c42a7b7eSSam Leffler 		sc->sc_stats.ast_rx_packets = ifp->if_ipackets;
4292c42a7b7eSSam Leffler 		sc->sc_stats.ast_rx_rssi = ieee80211_getrssi(ic);
4293c42a7b7eSSam Leffler 		ATH_UNLOCK(sc);
4294c42a7b7eSSam Leffler 		/*
4295c42a7b7eSSam Leffler 		 * NB: Drop the softc lock in case of a page fault;
4296c42a7b7eSSam Leffler 		 * we'll accept any potential inconsisentcy in the
4297c42a7b7eSSam Leffler 		 * statistics.  The alternative is to copy the data
4298c42a7b7eSSam Leffler 		 * to a local structure.
4299c42a7b7eSSam Leffler 		 */
4300c42a7b7eSSam Leffler 		return copyout(&sc->sc_stats,
4301c42a7b7eSSam Leffler 				ifr->ifr_data, sizeof (sc->sc_stats));
4302c42a7b7eSSam Leffler 	case SIOCGATHDIAG:
4303c42a7b7eSSam Leffler 		error = ath_ioctl_diag(sc, (struct ath_diag *) ifr);
4304c42a7b7eSSam Leffler 		break;
4305c42a7b7eSSam Leffler 	default:
4306c42a7b7eSSam Leffler 		error = ieee80211_ioctl(ic, cmd, data);
4307c42a7b7eSSam Leffler 		if (error == ENETRESET) {
4308c42a7b7eSSam Leffler 			if (IS_RUNNING(ifp) &&
4309c42a7b7eSSam Leffler 			    ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
4310c42a7b7eSSam Leffler 				ath_init(ifp);	/* XXX lose error */
4311c42a7b7eSSam Leffler 			error = 0;
4312c42a7b7eSSam Leffler 		}
4313c42a7b7eSSam Leffler 		if (error == ERESTART)
4314c42a7b7eSSam Leffler 			error = IS_RUNNING(ifp) ? ath_reset(ifp) : 0;
4315c42a7b7eSSam Leffler 		break;
4316c42a7b7eSSam Leffler 	}
4317c42a7b7eSSam Leffler 	ATH_UNLOCK(sc);
4318c42a7b7eSSam Leffler 	return error;
4319a614e076SSam Leffler #undef IS_RUNNING
4320c42a7b7eSSam Leffler }
4321c42a7b7eSSam Leffler 
4322c42a7b7eSSam Leffler static int
4323c42a7b7eSSam Leffler ath_sysctl_slottime(SYSCTL_HANDLER_ARGS)
4324c42a7b7eSSam Leffler {
4325c42a7b7eSSam Leffler 	struct ath_softc *sc = arg1;
4326c42a7b7eSSam Leffler 	u_int slottime = ath_hal_getslottime(sc->sc_ah);
4327c42a7b7eSSam Leffler 	int error;
4328c42a7b7eSSam Leffler 
4329c42a7b7eSSam Leffler 	error = sysctl_handle_int(oidp, &slottime, 0, req);
4330c42a7b7eSSam Leffler 	if (error || !req->newptr)
4331c42a7b7eSSam Leffler 		return error;
4332c42a7b7eSSam Leffler 	return !ath_hal_setslottime(sc->sc_ah, slottime) ? EINVAL : 0;
4333c42a7b7eSSam Leffler }
4334c42a7b7eSSam Leffler 
4335c42a7b7eSSam Leffler static int
4336c42a7b7eSSam Leffler ath_sysctl_acktimeout(SYSCTL_HANDLER_ARGS)
4337c42a7b7eSSam Leffler {
4338c42a7b7eSSam Leffler 	struct ath_softc *sc = arg1;
4339c42a7b7eSSam Leffler 	u_int acktimeout = ath_hal_getacktimeout(sc->sc_ah);
4340c42a7b7eSSam Leffler 	int error;
4341c42a7b7eSSam Leffler 
4342c42a7b7eSSam Leffler 	error = sysctl_handle_int(oidp, &acktimeout, 0, req);
4343c42a7b7eSSam Leffler 	if (error || !req->newptr)
4344c42a7b7eSSam Leffler 		return error;
4345c42a7b7eSSam Leffler 	return !ath_hal_setacktimeout(sc->sc_ah, acktimeout) ? EINVAL : 0;
4346c42a7b7eSSam Leffler }
4347c42a7b7eSSam Leffler 
4348c42a7b7eSSam Leffler static int
4349c42a7b7eSSam Leffler ath_sysctl_ctstimeout(SYSCTL_HANDLER_ARGS)
4350c42a7b7eSSam Leffler {
4351c42a7b7eSSam Leffler 	struct ath_softc *sc = arg1;
4352c42a7b7eSSam Leffler 	u_int ctstimeout = ath_hal_getctstimeout(sc->sc_ah);
4353c42a7b7eSSam Leffler 	int error;
4354c42a7b7eSSam Leffler 
4355c42a7b7eSSam Leffler 	error = sysctl_handle_int(oidp, &ctstimeout, 0, req);
4356c42a7b7eSSam Leffler 	if (error || !req->newptr)
4357c42a7b7eSSam Leffler 		return error;
4358c42a7b7eSSam Leffler 	return !ath_hal_setctstimeout(sc->sc_ah, ctstimeout) ? EINVAL : 0;
4359c42a7b7eSSam Leffler }
4360c42a7b7eSSam Leffler 
4361c42a7b7eSSam Leffler static int
4362c42a7b7eSSam Leffler ath_sysctl_softled(SYSCTL_HANDLER_ARGS)
4363c42a7b7eSSam Leffler {
4364c42a7b7eSSam Leffler 	struct ath_softc *sc = arg1;
4365c42a7b7eSSam Leffler 	int softled = sc->sc_softled;
4366c42a7b7eSSam Leffler 	int error;
4367c42a7b7eSSam Leffler 
4368c42a7b7eSSam Leffler 	error = sysctl_handle_int(oidp, &softled, 0, req);
4369c42a7b7eSSam Leffler 	if (error || !req->newptr)
4370c42a7b7eSSam Leffler 		return error;
4371c42a7b7eSSam Leffler 	if (softled > 1)
4372c42a7b7eSSam Leffler 		softled = 1;
4373c42a7b7eSSam Leffler 	if (softled != sc->sc_softled) {
4374c42a7b7eSSam Leffler 		if (softled)
4375c42a7b7eSSam Leffler 			ath_hal_gpioCfgOutput(sc->sc_ah, sc->sc_ledpin);
4376c42a7b7eSSam Leffler 		ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, !softled);
4377c42a7b7eSSam Leffler 		sc->sc_softled = softled;
4378c42a7b7eSSam Leffler 	}
4379c42a7b7eSSam Leffler 	return 0;
4380c42a7b7eSSam Leffler }
4381c42a7b7eSSam Leffler 
4382c42a7b7eSSam Leffler static int
4383c42a7b7eSSam Leffler ath_sysctl_rxantenna(SYSCTL_HANDLER_ARGS)
4384c42a7b7eSSam Leffler {
4385c42a7b7eSSam Leffler 	struct ath_softc *sc = arg1;
4386c42a7b7eSSam Leffler 	u_int defantenna = ath_hal_getdefantenna(sc->sc_ah);
4387c42a7b7eSSam Leffler 	int error;
4388c42a7b7eSSam Leffler 
4389c42a7b7eSSam Leffler 	error = sysctl_handle_int(oidp, &defantenna, 0, req);
4390c42a7b7eSSam Leffler 	if (!error && req->newptr)
4391c42a7b7eSSam Leffler 		ath_hal_setdefantenna(sc->sc_ah, defantenna);
4392c42a7b7eSSam Leffler 	return error;
4393c42a7b7eSSam Leffler }
4394c42a7b7eSSam Leffler 
4395c42a7b7eSSam Leffler static int
4396c42a7b7eSSam Leffler ath_sysctl_diversity(SYSCTL_HANDLER_ARGS)
4397c42a7b7eSSam Leffler {
4398c42a7b7eSSam Leffler 	struct ath_softc *sc = arg1;
4399c42a7b7eSSam Leffler 	u_int diversity = sc->sc_diversity;
4400c42a7b7eSSam Leffler 	int error;
4401c42a7b7eSSam Leffler 
4402c42a7b7eSSam Leffler 	error = sysctl_handle_int(oidp, &diversity, 0, req);
4403c42a7b7eSSam Leffler 	if (error || !req->newptr)
4404c42a7b7eSSam Leffler 		return error;
4405c42a7b7eSSam Leffler 	sc->sc_diversity = diversity;
4406c42a7b7eSSam Leffler 	return !ath_hal_setdiversity(sc->sc_ah, diversity) ? EINVAL : 0;
4407c42a7b7eSSam Leffler }
4408c42a7b7eSSam Leffler 
4409c42a7b7eSSam Leffler static int
4410c42a7b7eSSam Leffler ath_sysctl_diag(SYSCTL_HANDLER_ARGS)
4411c42a7b7eSSam Leffler {
4412c42a7b7eSSam Leffler 	struct ath_softc *sc = arg1;
4413c42a7b7eSSam Leffler 	u_int32_t diag;
4414c42a7b7eSSam Leffler 	int error;
4415c42a7b7eSSam Leffler 
4416c42a7b7eSSam Leffler 	if (!ath_hal_getdiag(sc->sc_ah, &diag))
4417c42a7b7eSSam Leffler 		return EINVAL;
4418c42a7b7eSSam Leffler 	error = sysctl_handle_int(oidp, &diag, 0, req);
4419c42a7b7eSSam Leffler 	if (error || !req->newptr)
4420c42a7b7eSSam Leffler 		return error;
4421c42a7b7eSSam Leffler 	return !ath_hal_setdiag(sc->sc_ah, diag) ? EINVAL : 0;
4422c42a7b7eSSam Leffler }
4423c42a7b7eSSam Leffler 
4424c42a7b7eSSam Leffler static int
4425c42a7b7eSSam Leffler ath_sysctl_tpscale(SYSCTL_HANDLER_ARGS)
4426c42a7b7eSSam Leffler {
4427c42a7b7eSSam Leffler 	struct ath_softc *sc = arg1;
4428c42a7b7eSSam Leffler 	struct ifnet *ifp = &sc->sc_if;
4429c42a7b7eSSam Leffler 	u_int32_t scale;
4430c42a7b7eSSam Leffler 	int error;
4431c42a7b7eSSam Leffler 
4432c42a7b7eSSam Leffler 	ath_hal_gettpscale(sc->sc_ah, &scale);
4433c42a7b7eSSam Leffler 	error = sysctl_handle_int(oidp, &scale, 0, req);
4434c42a7b7eSSam Leffler 	if (error || !req->newptr)
4435c42a7b7eSSam Leffler 		return error;
4436c42a7b7eSSam Leffler 	return !ath_hal_settpscale(sc->sc_ah, scale) ? EINVAL : ath_reset(ifp);
4437c42a7b7eSSam Leffler }
4438c42a7b7eSSam Leffler 
4439c42a7b7eSSam Leffler static int
4440c42a7b7eSSam Leffler ath_sysctl_tpc(SYSCTL_HANDLER_ARGS)
4441c42a7b7eSSam Leffler {
4442c42a7b7eSSam Leffler 	struct ath_softc *sc = arg1;
4443c42a7b7eSSam Leffler 	u_int tpc = ath_hal_gettpc(sc->sc_ah);
4444c42a7b7eSSam Leffler 	int error;
4445c42a7b7eSSam Leffler 
4446c42a7b7eSSam Leffler 	error = sysctl_handle_int(oidp, &tpc, 0, req);
4447c42a7b7eSSam Leffler 	if (error || !req->newptr)
4448c42a7b7eSSam Leffler 		return error;
4449c42a7b7eSSam Leffler 	return !ath_hal_settpc(sc->sc_ah, tpc) ? EINVAL : 0;
4450c42a7b7eSSam Leffler }
4451c42a7b7eSSam Leffler 
4452c42a7b7eSSam Leffler static void
4453c42a7b7eSSam Leffler ath_sysctlattach(struct ath_softc *sc)
4454c42a7b7eSSam Leffler {
4455c42a7b7eSSam Leffler 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
4456c42a7b7eSSam Leffler 	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
4457c42a7b7eSSam Leffler 
4458c42a7b7eSSam Leffler 	ath_hal_getcountrycode(sc->sc_ah, &sc->sc_countrycode);
4459c42a7b7eSSam Leffler 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4460c42a7b7eSSam Leffler 		"countrycode", CTLFLAG_RD, &sc->sc_countrycode, 0,
4461c42a7b7eSSam Leffler 		"EEPROM country code");
4462c42a7b7eSSam Leffler 	ath_hal_getregdomain(sc->sc_ah, &sc->sc_regdomain);
4463c42a7b7eSSam Leffler 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4464c42a7b7eSSam Leffler 		"regdomain", CTLFLAG_RD, &sc->sc_regdomain, 0,
4465c42a7b7eSSam Leffler 		"EEPROM regdomain code");
4466c42a7b7eSSam Leffler 	sc->sc_debug = ath_debug;
4467c42a7b7eSSam Leffler 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4468c42a7b7eSSam Leffler 		"debug", CTLFLAG_RW, &sc->sc_debug, 0,
4469c42a7b7eSSam Leffler 		"control debugging printfs");
4470c42a7b7eSSam Leffler 
4471c42a7b7eSSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4472c42a7b7eSSam Leffler 		"slottime", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
4473c42a7b7eSSam Leffler 		ath_sysctl_slottime, "I", "802.11 slot time (us)");
4474c42a7b7eSSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4475c42a7b7eSSam Leffler 		"acktimeout", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
4476c42a7b7eSSam Leffler 		ath_sysctl_acktimeout, "I", "802.11 ACK timeout (us)");
4477c42a7b7eSSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4478c42a7b7eSSam Leffler 		"ctstimeout", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
4479c42a7b7eSSam Leffler 		ath_sysctl_ctstimeout, "I", "802.11 CTS timeout (us)");
4480c42a7b7eSSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4481c42a7b7eSSam Leffler 		"softled", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
4482c42a7b7eSSam Leffler 		ath_sysctl_softled, "I", "enable/disable software LED support");
4483c42a7b7eSSam Leffler 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4484c42a7b7eSSam Leffler 		"ledpin", CTLFLAG_RW, &sc->sc_ledpin, 0,
4485c42a7b7eSSam Leffler 		"GPIO pin connected to LED");
4486c42a7b7eSSam Leffler 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4487c42a7b7eSSam Leffler 		"txantenna", CTLFLAG_RW, &sc->sc_txantenna, 0,
4488c42a7b7eSSam Leffler 		"tx antenna (0=auto)");
4489c42a7b7eSSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4490c42a7b7eSSam Leffler 		"rxantenna", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
4491c42a7b7eSSam Leffler 		ath_sysctl_rxantenna, "I", "default/rx antenna");
4492c42a7b7eSSam Leffler 	if (sc->sc_hasdiversity)
4493c42a7b7eSSam Leffler 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4494c42a7b7eSSam Leffler 			"diversity", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
4495c42a7b7eSSam Leffler 			ath_sysctl_diversity, "I", "antenna diversity");
4496c42a7b7eSSam Leffler 	sc->sc_txintrperiod = ATH_TXINTR_PERIOD;
4497c42a7b7eSSam Leffler 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4498c42a7b7eSSam Leffler 		"txintrperiod", CTLFLAG_RW, &sc->sc_txintrperiod, 0,
4499c42a7b7eSSam Leffler 		"tx descriptor batching");
4500c42a7b7eSSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4501c42a7b7eSSam Leffler 		"diag", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
4502c42a7b7eSSam Leffler 		ath_sysctl_diag, "I", "h/w diagnostic control");
4503c42a7b7eSSam Leffler 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4504c42a7b7eSSam Leffler 		"tpscale", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
4505c42a7b7eSSam Leffler 		ath_sysctl_tpscale, "I", "tx power scaling");
4506c42a7b7eSSam Leffler 	if (sc->sc_hastpc)
4507c42a7b7eSSam Leffler 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
4508c42a7b7eSSam Leffler 			"tpc", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
4509c42a7b7eSSam Leffler 			ath_sysctl_tpc, "I", "enable/disable per-packet TPC");
4510c42a7b7eSSam Leffler }
4511c42a7b7eSSam Leffler 
4512c42a7b7eSSam Leffler static void
4513c42a7b7eSSam Leffler ath_bpfattach(struct ath_softc *sc)
4514c42a7b7eSSam Leffler {
4515c42a7b7eSSam Leffler 	struct ifnet *ifp = &sc->sc_if;
4516c42a7b7eSSam Leffler 
4517c42a7b7eSSam Leffler 	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
4518c42a7b7eSSam Leffler 		sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th),
4519c42a7b7eSSam Leffler 		&sc->sc_drvbpf);
4520c42a7b7eSSam Leffler 	/*
4521c42a7b7eSSam Leffler 	 * Initialize constant fields.
4522c42a7b7eSSam Leffler 	 * XXX make header lengths a multiple of 32-bits so subsequent
4523c42a7b7eSSam Leffler 	 *     headers are properly aligned; this is a kludge to keep
4524c42a7b7eSSam Leffler 	 *     certain applications happy.
4525c42a7b7eSSam Leffler 	 *
4526c42a7b7eSSam Leffler 	 * NB: the channel is setup each time we transition to the
4527c42a7b7eSSam Leffler 	 *     RUN state to avoid filling it in for each frame.
4528c42a7b7eSSam Leffler 	 */
4529c42a7b7eSSam Leffler 	sc->sc_tx_th_len = roundup(sizeof(sc->sc_tx_th), sizeof(u_int32_t));
4530c42a7b7eSSam Leffler 	sc->sc_tx_th.wt_ihdr.it_len = htole16(sc->sc_tx_th_len);
4531c42a7b7eSSam Leffler 	sc->sc_tx_th.wt_ihdr.it_present = htole32(ATH_TX_RADIOTAP_PRESENT);
4532c42a7b7eSSam Leffler 
453316b4851aSSam Leffler 	sc->sc_rx_rt_len = roundup(sizeof(sc->sc_rx_th), sizeof(u_int32_t));
453416b4851aSSam Leffler 	sc->sc_rx_th.wr_ihdr.it_len = htole16(sc->sc_rx_rt_len);
4535c42a7b7eSSam Leffler 	sc->sc_rx_th.wr_ihdr.it_present = htole32(ATH_RX_RADIOTAP_PRESENT);
4536c42a7b7eSSam Leffler }
4537c42a7b7eSSam Leffler 
4538c42a7b7eSSam Leffler /*
4539c42a7b7eSSam Leffler  * Announce various information on device/driver attach.
4540c42a7b7eSSam Leffler  */
4541c42a7b7eSSam Leffler static void
4542c42a7b7eSSam Leffler ath_announce(struct ath_softc *sc)
4543c42a7b7eSSam Leffler {
4544c42a7b7eSSam Leffler #define	HAL_MODE_DUALBAND	(HAL_MODE_11A|HAL_MODE_11B)
4545c42a7b7eSSam Leffler 	struct ifnet *ifp = &sc->sc_if;
4546c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4547c42a7b7eSSam Leffler 	u_int modes, cc;
4548c42a7b7eSSam Leffler 
4549c42a7b7eSSam Leffler 	if_printf(ifp, "mac %d.%d phy %d.%d",
4550c42a7b7eSSam Leffler 		ah->ah_macVersion, ah->ah_macRev,
4551c42a7b7eSSam Leffler 		ah->ah_phyRev >> 4, ah->ah_phyRev & 0xf);
4552c42a7b7eSSam Leffler 	/*
4553c42a7b7eSSam Leffler 	 * Print radio revision(s).  We check the wireless modes
4554c42a7b7eSSam Leffler 	 * to avoid falsely printing revs for inoperable parts.
4555c42a7b7eSSam Leffler 	 * Dual-band radio revs are returned in the 5Ghz rev number.
4556c42a7b7eSSam Leffler 	 */
4557c42a7b7eSSam Leffler 	ath_hal_getcountrycode(ah, &cc);
4558c42a7b7eSSam Leffler 	modes = ath_hal_getwirelessmodes(ah, cc);
4559c42a7b7eSSam Leffler 	if ((modes & HAL_MODE_DUALBAND) == HAL_MODE_DUALBAND) {
4560c42a7b7eSSam Leffler 		if (ah->ah_analog5GhzRev && ah->ah_analog2GhzRev)
4561c42a7b7eSSam Leffler 			printf(" 5ghz radio %d.%d 2ghz radio %d.%d",
4562c42a7b7eSSam Leffler 				ah->ah_analog5GhzRev >> 4,
4563c42a7b7eSSam Leffler 				ah->ah_analog5GhzRev & 0xf,
4564c42a7b7eSSam Leffler 				ah->ah_analog2GhzRev >> 4,
4565c42a7b7eSSam Leffler 				ah->ah_analog2GhzRev & 0xf);
4566c42a7b7eSSam Leffler 		else
4567c42a7b7eSSam Leffler 			printf(" radio %d.%d", ah->ah_analog5GhzRev >> 4,
4568c42a7b7eSSam Leffler 				ah->ah_analog5GhzRev & 0xf);
4569c42a7b7eSSam Leffler 	} else
4570c42a7b7eSSam Leffler 		printf(" radio %d.%d", ah->ah_analog5GhzRev >> 4,
4571c42a7b7eSSam Leffler 			ah->ah_analog5GhzRev & 0xf);
4572c42a7b7eSSam Leffler 	printf("\n");
4573c42a7b7eSSam Leffler 	if (bootverbose) {
4574c42a7b7eSSam Leffler 		int i;
4575c42a7b7eSSam Leffler 		for (i = 0; i <= WME_AC_VO; i++) {
4576c42a7b7eSSam Leffler 			struct ath_txq *txq = sc->sc_ac2q[i];
4577c42a7b7eSSam Leffler 			if_printf(ifp, "Use hw queue %u for %s traffic\n",
4578c42a7b7eSSam Leffler 				txq->axq_qnum, ieee80211_wme_acnames[i]);
4579c42a7b7eSSam Leffler 		}
4580c42a7b7eSSam Leffler 		if_printf(ifp, "Use hw queue %u for CAB traffic\n",
4581c42a7b7eSSam Leffler 			sc->sc_cabq->axq_qnum);
4582c42a7b7eSSam Leffler 		if_printf(ifp, "Use hw queue %u for beacons\n", sc->sc_bhalq);
4583c42a7b7eSSam Leffler 	}
4584c42a7b7eSSam Leffler #undef HAL_MODE_DUALBAND
4585c42a7b7eSSam Leffler }
4586