xref: /freebsd/sys/dev/ath/if_ath.c (revision e325e5306941b8e6f205111c62b327de4fcae326)
15591b213SSam Leffler /*-
25591b213SSam Leffler  * Copyright (c) 2002, 2003 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>
855591b213SSam Leffler 
865591b213SSam Leffler /* unalligned little endian access */
875591b213SSam Leffler #define LE_READ_2(p)							\
885591b213SSam Leffler 	((u_int16_t)							\
895591b213SSam Leffler 	 ((((u_int8_t *)(p))[0]      ) | (((u_int8_t *)(p))[1] <<  8)))
905591b213SSam Leffler #define LE_READ_4(p)							\
915591b213SSam Leffler 	((u_int32_t)							\
925591b213SSam Leffler 	 ((((u_int8_t *)(p))[0]      ) | (((u_int8_t *)(p))[1] <<  8) |	\
935591b213SSam Leffler 	  (((u_int8_t *)(p))[2] << 16) | (((u_int8_t *)(p))[3] << 24)))
945591b213SSam Leffler 
955591b213SSam Leffler static void	ath_init(void *);
965591b213SSam Leffler static void	ath_stop(struct ifnet *);
975591b213SSam Leffler static void	ath_start(struct ifnet *);
985591b213SSam Leffler static void	ath_reset(struct ath_softc *);
995591b213SSam Leffler static int	ath_media_change(struct ifnet *);
1005591b213SSam Leffler static void	ath_watchdog(struct ifnet *);
1015591b213SSam Leffler static int	ath_ioctl(struct ifnet *, u_long, caddr_t);
1025591b213SSam Leffler static void	ath_fatal_proc(void *, int);
1035591b213SSam Leffler static void	ath_rxorn_proc(void *, int);
1045591b213SSam Leffler static void	ath_bmiss_proc(void *, int);
1055591b213SSam Leffler static void	ath_initkeytable(struct ath_softc *);
1065591b213SSam Leffler static void	ath_mode_init(struct ath_softc *);
1075591b213SSam Leffler static int	ath_beacon_alloc(struct ath_softc *, struct ieee80211_node *);
1085591b213SSam Leffler static void	ath_beacon_proc(void *, int);
1095591b213SSam Leffler static void	ath_beacon_free(struct ath_softc *);
1105591b213SSam Leffler static void	ath_beacon_config(struct ath_softc *);
1115591b213SSam Leffler static int	ath_desc_alloc(struct ath_softc *);
1125591b213SSam Leffler static void	ath_desc_free(struct ath_softc *);
1135591b213SSam Leffler static struct ieee80211_node *ath_node_alloc(struct ieee80211com *);
1145591b213SSam Leffler static void	ath_node_free(struct ieee80211com *, struct ieee80211_node *);
1155591b213SSam Leffler static void	ath_node_copy(struct ieee80211com *,
1165591b213SSam Leffler 			struct ieee80211_node *, const struct ieee80211_node *);
117de5af704SSam Leffler static u_int8_t	ath_node_getrssi(struct ieee80211com *,
118de5af704SSam Leffler 			struct ieee80211_node *);
1195591b213SSam Leffler static int	ath_rxbuf_init(struct ath_softc *, struct ath_buf *);
1205591b213SSam Leffler static void	ath_rx_proc(void *, int);
1215591b213SSam Leffler static int	ath_tx_start(struct ath_softc *, struct ieee80211_node *,
1225591b213SSam Leffler 			     struct ath_buf *, struct mbuf *);
1235591b213SSam Leffler static void	ath_tx_proc(void *, int);
1245591b213SSam Leffler static int	ath_chan_set(struct ath_softc *, struct ieee80211_channel *);
1255591b213SSam Leffler static void	ath_draintxq(struct ath_softc *);
1265591b213SSam Leffler static void	ath_stoprecv(struct ath_softc *);
1275591b213SSam Leffler static int	ath_startrecv(struct ath_softc *);
1285591b213SSam Leffler static void	ath_next_scan(void *);
1295591b213SSam Leffler static void	ath_calibrate(void *);
13045bbf62fSSam Leffler static int	ath_newstate(struct ieee80211com *, enum ieee80211_state, int);
1315591b213SSam Leffler static void	ath_newassoc(struct ieee80211com *,
1325591b213SSam Leffler 			struct ieee80211_node *, int);
1335591b213SSam Leffler static int	ath_getchannels(struct ath_softc *, u_int cc, HAL_BOOL outdoor);
1345591b213SSam Leffler 
1355591b213SSam Leffler static int	ath_rate_setup(struct ath_softc *sc, u_int mode);
1365591b213SSam Leffler static void	ath_setcurmode(struct ath_softc *, enum ieee80211_phymode);
1375591b213SSam Leffler static void	ath_rate_ctl_reset(struct ath_softc *, enum ieee80211_state);
1385591b213SSam Leffler static void	ath_rate_ctl(void *, struct ieee80211_node *);
1395591b213SSam Leffler 
1405591b213SSam Leffler SYSCTL_DECL(_hw_ath);
1415591b213SSam Leffler 
1425591b213SSam Leffler /* XXX validate sysctl values */
1435591b213SSam Leffler static	int ath_dwelltime = 200;		/* 5 channels/second */
1445591b213SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, dwell, CTLFLAG_RW, &ath_dwelltime,
1455591b213SSam Leffler 	    0, "channel dwell time (ms) for AP/station scanning");
1465591b213SSam Leffler static	int ath_calinterval = 30;		/* calibrate every 30 secs */
1475591b213SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, calibrate, CTLFLAG_RW, &ath_calinterval,
1485591b213SSam Leffler 	    0, "chip calibration interval (secs)");
14945cabbdcSSam Leffler static	int ath_outdoor = AH_TRUE;		/* outdoor operation */
15045cabbdcSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, outdoor, CTLFLAG_RD, &ath_outdoor,
15145cabbdcSSam Leffler 	    0, "enable/disable outdoor operation");
15245cabbdcSSam Leffler static	int ath_countrycode = CTRY_DEFAULT;	/* country code */
15345cabbdcSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, countrycode, CTLFLAG_RD, &ath_countrycode,
15445cabbdcSSam Leffler 	    0, "country code");
15545cabbdcSSam Leffler static	int ath_regdomain = 0;			/* regulatory domain */
15645cabbdcSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, regdomain, CTLFLAG_RD, &ath_regdomain,
15745cabbdcSSam Leffler 	    0, "regulatory domain");
1585591b213SSam Leffler 
1595591b213SSam Leffler #ifdef AR_DEBUG
1605591b213SSam Leffler int	ath_debug = 0;
1615591b213SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, debug, CTLFLAG_RW, &ath_debug,
1625591b213SSam Leffler 	    0, "control debugging printfs");
163e325e530SSam Leffler #define	IFF_DUMPPKTS(_ifp, _m) \
164e325e530SSam Leffler 	((ath_debug & _m) || \
1655591b213SSam Leffler 	    ((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
1665591b213SSam Leffler static	void ath_printrxbuf(struct ath_buf *bf, int);
1675591b213SSam Leffler static	void ath_printtxbuf(struct ath_buf *bf, int);
168e325e530SSam Leffler enum {
169e325e530SSam Leffler 	ATH_DEBUG_XMIT		= 0x00000001,	/* basic xmit operation */
170e325e530SSam Leffler 	ATH_DEBUG_XMIT_DESC	= 0x00000002,	/* xmit descriptors */
171e325e530SSam Leffler 	ATH_DEBUG_RECV		= 0x00000004,	/* basic recv operation */
172e325e530SSam Leffler 	ATH_DEBUG_RECV_DESC	= 0x00000008,	/* recv descriptors */
173e325e530SSam Leffler 	ATH_DEBUG_RATE		= 0x00000010,	/* rate control */
174e325e530SSam Leffler 	ATH_DEBUG_RESET		= 0x00000020,	/* reset processing */
175e325e530SSam Leffler 	ATH_DEBUG_MODE		= 0x00000040,	/* mode init/setup */
176e325e530SSam Leffler 	ATH_DEBUG_BEACON 	= 0x00000080,	/* beacon handling */
177e325e530SSam Leffler 	ATH_DEBUG_WATCHDOG 	= 0x00000100,	/* watchdog timeout */
178e325e530SSam Leffler 	ATH_DEBUG_INTR		= 0x00001000,	/* ISR */
179e325e530SSam Leffler 	ATH_DEBUG_TX_PROC	= 0x00002000,	/* tx ISR proc */
180e325e530SSam Leffler 	ATH_DEBUG_RX_PROC	= 0x00004000,	/* rx ISR proc */
181e325e530SSam Leffler 	ATH_DEBUG_BEACON_PROC	= 0x00008000,	/* beacon ISR proc */
182e325e530SSam Leffler 	ATH_DEBUG_CALIBRATE	= 0x00010000,	/* periodic calibration */
183e325e530SSam Leffler 	ATH_DEBUG_ANY		= 0xffffffff
184e325e530SSam Leffler };
185e325e530SSam Leffler #define	DPRINTF(_m,X)	if (ath_debug & _m) printf X
1865591b213SSam Leffler #else
187e325e530SSam Leffler #define	IFF_DUMPPKTS(_ifp, _m) \
1885591b213SSam Leffler 	(((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
189e325e530SSam Leffler #define	DPRINTF(_m, X)
1905591b213SSam Leffler #endif
1915591b213SSam Leffler 
1925591b213SSam Leffler int
1935591b213SSam Leffler ath_attach(u_int16_t devid, struct ath_softc *sc)
1945591b213SSam Leffler {
1955591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
1965591b213SSam Leffler 	struct ifnet *ifp = &ic->ic_if;
1975591b213SSam Leffler 	struct ath_hal *ah;
1985591b213SSam Leffler 	HAL_STATUS status;
1995591b213SSam Leffler 	int error = 0;
2005591b213SSam Leffler 
201e325e530SSam Leffler 	DPRINTF(ATH_DEBUG_ANY, ("%s: devid 0x%x\n", __func__, devid));
2025591b213SSam Leffler 
2035591b213SSam Leffler 	/* set these up early for if_printf use */
2049bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(sc->sc_dev),
2059bf40edeSBrooks Davis 	    device_get_unit(sc->sc_dev));
2065591b213SSam Leffler 
2075591b213SSam Leffler 	ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh, &status);
2085591b213SSam Leffler 	if (ah == NULL) {
2095591b213SSam Leffler 		if_printf(ifp, "unable to attach hardware; HAL status %u\n",
2105591b213SSam Leffler 			status);
2115591b213SSam Leffler 		error = ENXIO;
2125591b213SSam Leffler 		goto bad;
2135591b213SSam Leffler 	}
21485bdc65aSSam Leffler 	if (ah->ah_abi != HAL_ABI_VERSION) {
21585bdc65aSSam Leffler 		if_printf(ifp, "HAL ABI mismatch detected (0x%x != 0x%x)\n",
21685bdc65aSSam Leffler 			ah->ah_abi, HAL_ABI_VERSION);
21785bdc65aSSam Leffler 		error = ENXIO;
21885bdc65aSSam Leffler 		goto bad;
21985bdc65aSSam Leffler 	}
22085bdc65aSSam Leffler 	if_printf(ifp, "mac %d.%d phy %d.%d",
22185bdc65aSSam Leffler 		ah->ah_macVersion, ah->ah_macRev,
22285bdc65aSSam Leffler 		ah->ah_phyRev >> 4, ah->ah_phyRev & 0xf);
22385bdc65aSSam Leffler 	if (ah->ah_analog5GhzRev)
22485bdc65aSSam Leffler 		printf(" 5ghz radio %d.%d",
22585bdc65aSSam Leffler 			ah->ah_analog5GhzRev >> 4, ah->ah_analog5GhzRev & 0xf);
22685bdc65aSSam Leffler 	if (ah->ah_analog2GhzRev)
22785bdc65aSSam Leffler 		printf(" 2ghz radio %d.%d",
22885bdc65aSSam Leffler 			ah->ah_analog2GhzRev >> 4, ah->ah_analog2GhzRev & 0xf);
22985bdc65aSSam Leffler 	printf("\n");
2305591b213SSam Leffler 	sc->sc_ah = ah;
231b58b3803SSam Leffler 	sc->sc_invalid = 0;	/* ready to go, enable interrupt handling */
2325591b213SSam Leffler 
2335591b213SSam Leffler 	/*
2345591b213SSam Leffler 	 * Collect the channel list using the default country
2355591b213SSam Leffler 	 * code and including outdoor channels.  The 802.11 layer
23645cabbdcSSam Leffler 	 * is resposible for filtering this list based on settings
23745cabbdcSSam Leffler 	 * like the phy mode.
2385591b213SSam Leffler 	 */
23945cabbdcSSam Leffler 	error = ath_getchannels(sc, ath_countrycode, ath_outdoor);
2405591b213SSam Leffler 	if (error != 0)
2415591b213SSam Leffler 		goto bad;
24245cabbdcSSam Leffler 	/*
24345cabbdcSSam Leffler 	 * Copy these back; they are set as a side effect
24445cabbdcSSam Leffler 	 * of constructing the channel list.
24545cabbdcSSam Leffler 	 */
24645cabbdcSSam Leffler 	ath_regdomain = ath_hal_getregdomain(ah);
24745cabbdcSSam Leffler 	ath_countrycode = ath_hal_getcountrycode(ah);
2485591b213SSam Leffler 
2495591b213SSam Leffler 	/*
2505591b213SSam Leffler 	 * Setup rate tables for all potential media types.
2515591b213SSam Leffler 	 */
2525591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11A);
2535591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11B);
2545591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11G);
2555591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO);
2565591b213SSam Leffler 
2575591b213SSam Leffler 	error = ath_desc_alloc(sc);
2585591b213SSam Leffler 	if (error != 0) {
2595591b213SSam Leffler 		if_printf(ifp, "failed to allocate descriptors: %d\n", error);
2605591b213SSam Leffler 		goto bad;
2615591b213SSam Leffler 	}
2622274d8c8SSam Leffler 	callout_init(&sc->sc_scan_ch, CALLOUT_MPSAFE);
2632274d8c8SSam Leffler 	callout_init(&sc->sc_cal_ch, CALLOUT_MPSAFE);
2645591b213SSam Leffler 
265f0b2a0beSSam Leffler 	ATH_TXBUF_LOCK_INIT(sc);
266f0b2a0beSSam Leffler 	ATH_TXQ_LOCK_INIT(sc);
2675591b213SSam Leffler 
2685591b213SSam Leffler 	TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc, sc);
2695591b213SSam Leffler 	TASK_INIT(&sc->sc_rxtask, 0, ath_rx_proc, sc);
2705591b213SSam Leffler 	TASK_INIT(&sc->sc_swbatask, 0, ath_beacon_proc, sc);
2715591b213SSam Leffler 	TASK_INIT(&sc->sc_rxorntask, 0, ath_rxorn_proc, sc);
2725591b213SSam Leffler 	TASK_INIT(&sc->sc_fataltask, 0, ath_fatal_proc, sc);
2735591b213SSam Leffler 	TASK_INIT(&sc->sc_bmisstask, 0, ath_bmiss_proc, sc);
2745591b213SSam Leffler 
2755591b213SSam Leffler 	/*
2765591b213SSam Leffler 	 * For now just pre-allocate one data queue and one
2775591b213SSam Leffler 	 * beacon queue.  Note that the HAL handles resetting
2785591b213SSam Leffler 	 * them at the needed time.  Eventually we'll want to
2795591b213SSam Leffler 	 * allocate more tx queues for splitting management
2805591b213SSam Leffler 	 * frames and for QOS support.
2815591b213SSam Leffler 	 */
2825591b213SSam Leffler 	sc->sc_txhalq = ath_hal_setuptxqueue(ah,
2835591b213SSam Leffler 		HAL_TX_QUEUE_DATA,
2845591b213SSam Leffler 		AH_TRUE			/* enable interrupts */
2855591b213SSam Leffler 	);
2865591b213SSam Leffler 	if (sc->sc_txhalq == (u_int) -1) {
2875591b213SSam Leffler 		if_printf(ifp, "unable to setup a data xmit queue!\n");
2885591b213SSam Leffler 		goto bad;
2895591b213SSam Leffler 	}
2905591b213SSam Leffler 	sc->sc_bhalq = ath_hal_setuptxqueue(ah,
2915591b213SSam Leffler 		HAL_TX_QUEUE_BEACON,
2925591b213SSam Leffler 		AH_TRUE			/* enable interrupts */
2935591b213SSam Leffler 	);
2945591b213SSam Leffler 	if (sc->sc_bhalq == (u_int) -1) {
2955591b213SSam Leffler 		if_printf(ifp, "unable to setup a beacon xmit queue!\n");
2965591b213SSam Leffler 		goto bad;
2975591b213SSam Leffler 	}
2985591b213SSam Leffler 
2995591b213SSam Leffler 	ifp->if_softc = sc;
3005591b213SSam Leffler 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
3015591b213SSam Leffler 	ifp->if_start = ath_start;
3025591b213SSam Leffler 	ifp->if_watchdog = ath_watchdog;
3035591b213SSam Leffler 	ifp->if_ioctl = ath_ioctl;
3045591b213SSam Leffler 	ifp->if_init = ath_init;
3055591b213SSam Leffler 	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
3065591b213SSam Leffler 
3075591b213SSam Leffler 	ic->ic_softc = sc;
3085591b213SSam Leffler 	ic->ic_newassoc = ath_newassoc;
3095591b213SSam Leffler 	/* XXX not right but it's not used anywhere important */
3105591b213SSam Leffler 	ic->ic_phytype = IEEE80211_T_OFDM;
3115591b213SSam Leffler 	ic->ic_opmode = IEEE80211_M_STA;
312fe32c3efSSam Leffler 	ic->ic_caps = IEEE80211_C_WEP		/* wep supported */
313fe32c3efSSam Leffler 		| IEEE80211_C_IBSS		/* ibss, nee adhoc, mode */
314fe32c3efSSam Leffler 		| IEEE80211_C_HOSTAP		/* hostap mode */
315fe32c3efSSam Leffler 		| IEEE80211_C_MONITOR		/* monitor mode */
316fe32c3efSSam Leffler 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
317fe32c3efSSam Leffler 		| IEEE80211_C_RCVMGT;		/* recv management frames */
3185591b213SSam Leffler 
3195591b213SSam Leffler 	/* get mac address from hardware */
3205591b213SSam Leffler 	ath_hal_getmac(ah, ic->ic_myaddr);
3215591b213SSam Leffler 
3225591b213SSam Leffler 	/* call MI attach routine. */
3235591b213SSam Leffler 	ieee80211_ifattach(ifp);
3245591b213SSam Leffler 	/* override default methods */
3255591b213SSam Leffler 	ic->ic_node_alloc = ath_node_alloc;
3265591b213SSam Leffler 	ic->ic_node_free = ath_node_free;
3275591b213SSam Leffler 	ic->ic_node_copy = ath_node_copy;
328de5af704SSam Leffler 	ic->ic_node_getrssi = ath_node_getrssi;
32945bbf62fSSam Leffler 	sc->sc_newstate = ic->ic_newstate;
33045bbf62fSSam Leffler 	ic->ic_newstate = ath_newstate;
33145bbf62fSSam Leffler 	/* complete initialization */
3325591b213SSam Leffler 	ieee80211_media_init(ifp, ath_media_change, ieee80211_media_status);
3335591b213SSam Leffler 
33473454c73SSam Leffler 	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
33573454c73SSam Leffler 		sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th),
33673454c73SSam Leffler 		&sc->sc_drvbpf);
33773454c73SSam Leffler 	/*
33873454c73SSam Leffler 	 * Initialize constant fields.
33973454c73SSam Leffler 	 *
34073454c73SSam Leffler 	 * NB: the channel is setup each time we transition to the
34173454c73SSam Leffler 	 *     RUN state to avoid filling it in for each frame.
34273454c73SSam Leffler 	 */
34373454c73SSam Leffler 	sc->sc_tx_th.wt_ihdr.it_len = sizeof(sc->sc_tx_th);
34473454c73SSam Leffler 	sc->sc_tx_th.wt_ihdr.it_present = ATH_TX_RADIOTAP_PRESENT;
34573454c73SSam Leffler 
34673454c73SSam Leffler 	sc->sc_rx_th.wr_ihdr.it_len = sizeof(sc->sc_rx_th);
34773454c73SSam Leffler 	sc->sc_rx_th.wr_ihdr.it_present = ATH_RX_RADIOTAP_PRESENT;
34873454c73SSam Leffler 
3495591b213SSam Leffler 	if_printf(ifp, "802.11 address: %s\n", ether_sprintf(ic->ic_myaddr));
3505591b213SSam Leffler 
3515591b213SSam Leffler 	return 0;
3525591b213SSam Leffler bad:
3535591b213SSam Leffler 	if (ah)
3545591b213SSam Leffler 		ath_hal_detach(ah);
3555591b213SSam Leffler 	sc->sc_invalid = 1;
3565591b213SSam Leffler 	return error;
3575591b213SSam Leffler }
3585591b213SSam Leffler 
3595591b213SSam Leffler int
3605591b213SSam Leffler ath_detach(struct ath_softc *sc)
3615591b213SSam Leffler {
3625591b213SSam Leffler 	struct ifnet *ifp = &sc->sc_ic.ic_if;
3635591b213SSam Leffler 
364e325e530SSam Leffler 	DPRINTF(ATH_DEBUG_ANY, ("%s: if_flags %x\n", __func__, ifp->if_flags));
3655591b213SSam Leffler 
3665591b213SSam Leffler 	ath_stop(ifp);
36773454c73SSam Leffler 	bpfdetach(ifp);
3685591b213SSam Leffler 	ath_desc_free(sc);
3695591b213SSam Leffler 	ath_hal_detach(sc->sc_ah);
3705591b213SSam Leffler 	ieee80211_ifdetach(ifp);
371f0b2a0beSSam Leffler 
372f0b2a0beSSam Leffler 	ATH_TXBUF_LOCK_DESTROY(sc);
373f0b2a0beSSam Leffler 	ATH_TXQ_LOCK_DESTROY(sc);
374f0b2a0beSSam Leffler 
3755591b213SSam Leffler 	return 0;
3765591b213SSam Leffler }
3775591b213SSam Leffler 
3785591b213SSam Leffler void
3795591b213SSam Leffler ath_suspend(struct ath_softc *sc)
3805591b213SSam Leffler {
3815591b213SSam Leffler 	struct ifnet *ifp = &sc->sc_ic.ic_if;
3825591b213SSam Leffler 
383e325e530SSam Leffler 	DPRINTF(ATH_DEBUG_ANY, ("%s: if_flags %x\n", __func__, ifp->if_flags));
3845591b213SSam Leffler 
3855591b213SSam Leffler 	ath_stop(ifp);
3865591b213SSam Leffler }
3875591b213SSam Leffler 
3885591b213SSam Leffler void
3895591b213SSam Leffler ath_resume(struct ath_softc *sc)
3905591b213SSam Leffler {
3915591b213SSam Leffler 	struct ifnet *ifp = &sc->sc_ic.ic_if;
3925591b213SSam Leffler 
393e325e530SSam Leffler 	DPRINTF(ATH_DEBUG_ANY, ("%s: if_flags %x\n", __func__, ifp->if_flags));
3945591b213SSam Leffler 
3956b59f5e3SSam Leffler 	if (ifp->if_flags & IFF_UP) {
3965591b213SSam Leffler 		ath_init(ifp);
3976b59f5e3SSam Leffler 		if (ifp->if_flags & IFF_RUNNING)
3985591b213SSam Leffler 			ath_start(ifp);
3995591b213SSam Leffler 	}
4006b59f5e3SSam Leffler }
4015591b213SSam Leffler 
4025591b213SSam Leffler void
4035591b213SSam Leffler ath_shutdown(struct ath_softc *sc)
4045591b213SSam Leffler {
4055591b213SSam Leffler 	struct ifnet *ifp = &sc->sc_ic.ic_if;
4065591b213SSam Leffler 
407e325e530SSam Leffler 	DPRINTF(ATH_DEBUG_ANY, ("%s: if_flags %x\n", __func__, ifp->if_flags));
4085591b213SSam Leffler 
4095591b213SSam Leffler 	ath_stop(ifp);
4105591b213SSam Leffler }
4115591b213SSam Leffler 
4125591b213SSam Leffler void
4135591b213SSam Leffler ath_intr(void *arg)
4145591b213SSam Leffler {
4155591b213SSam Leffler 	struct ath_softc *sc = arg;
4165591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
4175591b213SSam Leffler 	struct ifnet *ifp = &ic->ic_if;
4185591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4195591b213SSam Leffler 	HAL_INT status;
4205591b213SSam Leffler 
4215591b213SSam Leffler 	if (sc->sc_invalid) {
4225591b213SSam Leffler 		/*
423b58b3803SSam Leffler 		 * The hardware is not ready/present, don't touch anything.
424b58b3803SSam Leffler 		 * Note this can happen early on if the IRQ is shared.
4255591b213SSam Leffler 		 */
426e325e530SSam Leffler 		DPRINTF(ATH_DEBUG_ANY, ("%s: invalid; ignored\n", __func__));
4275591b213SSam Leffler 		return;
4285591b213SSam Leffler 	}
4295591b213SSam Leffler 	if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) != (IFF_RUNNING|IFF_UP)) {
430e325e530SSam Leffler 		DPRINTF(ATH_DEBUG_ANY, ("%s: if_flags 0x%x\n",
431e325e530SSam Leffler 			__func__, ifp->if_flags));
4325591b213SSam Leffler 		ath_hal_getisr(ah, &status);	/* clear ISR */
4335591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable further intr's */
4345591b213SSam Leffler 		return;
4355591b213SSam Leffler 	}
4365591b213SSam Leffler 	ath_hal_getisr(ah, &status);		/* NB: clears ISR too */
437e325e530SSam Leffler 	DPRINTF(ATH_DEBUG_INTR, ("%s: status 0x%x\n", __func__, status));
4385591b213SSam Leffler #ifdef AR_DEBUG
4395591b213SSam Leffler 	if (ath_debug &&
4405591b213SSam Leffler 	    (status & (HAL_INT_FATAL|HAL_INT_RXORN|HAL_INT_BMISS))) {
4415591b213SSam Leffler 		if_printf(ifp, "ath_intr: status 0x%x\n", status);
4425591b213SSam Leffler 		ath_hal_dumpstate(ah);
4435591b213SSam Leffler 	}
4445591b213SSam Leffler #endif /* AR_DEBUG */
445ecddff40SSam Leffler 	status &= sc->sc_imask;			/* discard unasked for bits */
4465591b213SSam Leffler 	if (status & HAL_INT_FATAL) {
4475591b213SSam Leffler 		sc->sc_stats.ast_hardware++;
4485591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
4495591b213SSam Leffler 		taskqueue_enqueue(taskqueue_swi, &sc->sc_fataltask);
4505591b213SSam Leffler 	} else if (status & HAL_INT_RXORN) {
4515591b213SSam Leffler 		sc->sc_stats.ast_rxorn++;
4525591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
4535591b213SSam Leffler 		taskqueue_enqueue(taskqueue_swi, &sc->sc_rxorntask);
4545591b213SSam Leffler 	} else {
4555591b213SSam Leffler 		if (status & HAL_INT_RXEOL) {
4565591b213SSam Leffler 			/*
4575591b213SSam Leffler 			 * NB: the hardware should re-read the link when
4585591b213SSam Leffler 			 *     RXE bit is written, but it doesn't work at
4595591b213SSam Leffler 			 *     least on older hardware revs.
4605591b213SSam Leffler 			 */
4615591b213SSam Leffler 			sc->sc_stats.ast_rxeol++;
4625591b213SSam Leffler 			sc->sc_rxlink = NULL;
4635591b213SSam Leffler 		}
4645591b213SSam Leffler 		if (status & HAL_INT_TXURN) {
4655591b213SSam Leffler 			sc->sc_stats.ast_txurn++;
4665591b213SSam Leffler 			/* bump tx trigger level */
4675591b213SSam Leffler 			ath_hal_updatetxtriglevel(ah, AH_TRUE);
4685591b213SSam Leffler 		}
4695591b213SSam Leffler 		if (status & HAL_INT_RX)
4705591b213SSam Leffler 			taskqueue_enqueue(taskqueue_swi, &sc->sc_rxtask);
4715591b213SSam Leffler 		if (status & HAL_INT_TX)
4725591b213SSam Leffler 			taskqueue_enqueue(taskqueue_swi, &sc->sc_txtask);
4735591b213SSam Leffler 		if (status & HAL_INT_SWBA)
4745591b213SSam Leffler 			taskqueue_enqueue(taskqueue_swi, &sc->sc_swbatask);
4755591b213SSam Leffler 		if (status & HAL_INT_BMISS) {
4765591b213SSam Leffler 			sc->sc_stats.ast_bmiss++;
4775591b213SSam Leffler 			taskqueue_enqueue(taskqueue_swi, &sc->sc_bmisstask);
4785591b213SSam Leffler 		}
4795591b213SSam Leffler 	}
4805591b213SSam Leffler }
4815591b213SSam Leffler 
4825591b213SSam Leffler static void
4835591b213SSam Leffler ath_fatal_proc(void *arg, int pending)
4845591b213SSam Leffler {
4855591b213SSam Leffler 	struct ath_softc *sc = arg;
4865591b213SSam Leffler 
4875591b213SSam Leffler 	device_printf(sc->sc_dev, "hardware error; resetting\n");
4885591b213SSam Leffler 	ath_reset(sc);
4895591b213SSam Leffler }
4905591b213SSam Leffler 
4915591b213SSam Leffler static void
4925591b213SSam Leffler ath_rxorn_proc(void *arg, int pending)
4935591b213SSam Leffler {
4945591b213SSam Leffler 	struct ath_softc *sc = arg;
4955591b213SSam Leffler 
4965591b213SSam Leffler 	device_printf(sc->sc_dev, "rx FIFO overrun; resetting\n");
4975591b213SSam Leffler 	ath_reset(sc);
4985591b213SSam Leffler }
4995591b213SSam Leffler 
5005591b213SSam Leffler static void
5015591b213SSam Leffler ath_bmiss_proc(void *arg, int pending)
5025591b213SSam Leffler {
5035591b213SSam Leffler 	struct ath_softc *sc = arg;
5045591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
5055591b213SSam Leffler 
506e325e530SSam Leffler 	DPRINTF(ATH_DEBUG_ANY, ("%s: pending %u\n", __func__, pending));
5075591b213SSam Leffler 	KASSERT(ic->ic_opmode == IEEE80211_M_STA,
5085591b213SSam Leffler 		("unexpect operating mode %u", ic->ic_opmode));
509e585d188SSam Leffler 	if (ic->ic_state == IEEE80211_S_RUN) {
510e585d188SSam Leffler 		/*
511e585d188SSam Leffler 		 * Rather than go directly to scan state, try to
512e585d188SSam Leffler 		 * reassociate first.  If that fails then the state
513e585d188SSam Leffler 		 * machine will drop us into scanning after timing
514e585d188SSam Leffler 		 * out waiting for a probe response.
515e585d188SSam Leffler 		 */
516e585d188SSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1);
517e585d188SSam Leffler 	}
5185591b213SSam Leffler }
5195591b213SSam Leffler 
5205591b213SSam Leffler static u_int
5215591b213SSam Leffler ath_chan2flags(struct ieee80211com *ic, struct ieee80211_channel *chan)
5225591b213SSam Leffler {
5235591b213SSam Leffler 	static const u_int modeflags[] = {
5245591b213SSam Leffler 		0,			/* IEEE80211_MODE_AUTO */
5255591b213SSam Leffler 		CHANNEL_A,		/* IEEE80211_MODE_11A */
5265591b213SSam Leffler 		CHANNEL_B,		/* IEEE80211_MODE_11B */
5275591b213SSam Leffler 		CHANNEL_PUREG,		/* IEEE80211_MODE_11G */
5285591b213SSam Leffler 		CHANNEL_T		/* IEEE80211_MODE_TURBO */
5295591b213SSam Leffler 	};
5305591b213SSam Leffler 	return modeflags[ieee80211_chan2mode(ic, chan)];
5315591b213SSam Leffler }
5325591b213SSam Leffler 
5335591b213SSam Leffler static void
5345591b213SSam Leffler ath_init(void *arg)
5355591b213SSam Leffler {
5365591b213SSam Leffler 	struct ath_softc *sc = (struct ath_softc *) arg;
5375591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
5385591b213SSam Leffler 	struct ifnet *ifp = &ic->ic_if;
5395591b213SSam Leffler 	struct ieee80211_node *ni;
5405591b213SSam Leffler 	enum ieee80211_phymode mode;
5415591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5425591b213SSam Leffler 	HAL_STATUS status;
5435591b213SSam Leffler 	HAL_CHANNEL hchan;
5445591b213SSam Leffler 
545e325e530SSam Leffler 	DPRINTF(ATH_DEBUG_ANY, ("%s: if_flags 0x%x\n",
546e325e530SSam Leffler 		__func__, ifp->if_flags));
5475591b213SSam Leffler 
548f0b2a0beSSam Leffler 	ATH_LOCK(sc);
5495591b213SSam Leffler 	/*
5505591b213SSam Leffler 	 * Stop anything previously setup.  This is safe
5515591b213SSam Leffler 	 * whether this is the first time through or not.
5525591b213SSam Leffler 	 */
5535591b213SSam Leffler 	ath_stop(ifp);
5545591b213SSam Leffler 
5555591b213SSam Leffler 	/*
5565591b213SSam Leffler 	 * The basic interface to setting the hardware in a good
5575591b213SSam Leffler 	 * state is ``reset''.  On return the hardware is known to
5585591b213SSam Leffler 	 * be powered up and with interrupts disabled.  This must
5595591b213SSam Leffler 	 * be followed by initialization of the appropriate bits
5605591b213SSam Leffler 	 * and then setup of the interrupt mask.
5615591b213SSam Leffler 	 */
5625591b213SSam Leffler 	hchan.channel = ic->ic_ibss_chan->ic_freq;
5635591b213SSam Leffler 	hchan.channelFlags = ath_chan2flags(ic, ic->ic_ibss_chan);
5645591b213SSam Leffler 	if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_FALSE, &status)) {
5655591b213SSam Leffler 		if_printf(ifp, "unable to reset hardware; hal status %u\n",
5665591b213SSam Leffler 			status);
5675591b213SSam Leffler 		goto done;
5685591b213SSam Leffler 	}
5695591b213SSam Leffler 
5705591b213SSam Leffler 	/*
5715591b213SSam Leffler 	 * Setup the hardware after reset: the key cache
5725591b213SSam Leffler 	 * is filled as needed and the receive engine is
5735591b213SSam Leffler 	 * set going.  Frame transmit is handled entirely
5745591b213SSam Leffler 	 * in the frame output path; there's nothing to do
5755591b213SSam Leffler 	 * here except setup the interrupt mask.
5765591b213SSam Leffler 	 */
5775591b213SSam Leffler 	if (ic->ic_flags & IEEE80211_F_WEPON)
5785591b213SSam Leffler 		ath_initkeytable(sc);
5795591b213SSam Leffler 	if (ath_startrecv(sc) != 0) {
5805591b213SSam Leffler 		if_printf(ifp, "unable to start recv logic\n");
5815591b213SSam Leffler 		goto done;
5825591b213SSam Leffler 	}
5835591b213SSam Leffler 
5845591b213SSam Leffler 	/*
5855591b213SSam Leffler 	 * Enable interrupts.
5865591b213SSam Leffler 	 */
5875591b213SSam Leffler 	sc->sc_imask = HAL_INT_RX | HAL_INT_TX
5885591b213SSam Leffler 		  | HAL_INT_RXEOL | HAL_INT_RXORN
5895591b213SSam Leffler 		  | HAL_INT_FATAL | HAL_INT_GLOBAL;
5905591b213SSam Leffler 	ath_hal_intrset(ah, sc->sc_imask);
5915591b213SSam Leffler 
5925591b213SSam Leffler 	ifp->if_flags |= IFF_RUNNING;
5935591b213SSam Leffler 	ic->ic_state = IEEE80211_S_INIT;
5945591b213SSam Leffler 
5955591b213SSam Leffler 	/*
5965591b213SSam Leffler 	 * The hardware should be ready to go now so it's safe
5975591b213SSam Leffler 	 * to kick the 802.11 state machine as it's likely to
5985591b213SSam Leffler 	 * immediately call back to us to send mgmt frames.
5995591b213SSam Leffler 	 */
6005591b213SSam Leffler 	ni = ic->ic_bss;
6015591b213SSam Leffler 	ni->ni_chan = ic->ic_ibss_chan;
6025591b213SSam Leffler 	mode = ieee80211_chan2mode(ic, ni->ni_chan);
6035591b213SSam Leffler 	if (mode != sc->sc_curmode)
6045591b213SSam Leffler 		ath_setcurmode(sc, mode);
6056b59f5e3SSam Leffler 	if (ic->ic_opmode != IEEE80211_M_MONITOR)
60645bbf62fSSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
6076b59f5e3SSam Leffler 	else
6086b59f5e3SSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
6095591b213SSam Leffler done:
610f0b2a0beSSam Leffler 	ATH_UNLOCK(sc);
6115591b213SSam Leffler }
6125591b213SSam Leffler 
6135591b213SSam Leffler static void
6145591b213SSam Leffler ath_stop(struct ifnet *ifp)
6155591b213SSam Leffler {
61645bbf62fSSam Leffler 	struct ieee80211com *ic = (struct ieee80211com *) ifp;
6175591b213SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
6185591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
6195591b213SSam Leffler 
620e325e530SSam Leffler 	DPRINTF(ATH_DEBUG_ANY, ("%s: invalid %u if_flags 0x%x\n",
621e325e530SSam Leffler 		__func__, sc->sc_invalid, ifp->if_flags));
6225591b213SSam Leffler 
623f0b2a0beSSam Leffler 	ATH_LOCK(sc);
6245591b213SSam Leffler 	if (ifp->if_flags & IFF_RUNNING) {
6255591b213SSam Leffler 		/*
6265591b213SSam Leffler 		 * Shutdown the hardware and driver:
6275591b213SSam Leffler 		 *    disable interrupts
6285591b213SSam Leffler 		 *    turn off timers
6295591b213SSam Leffler 		 *    clear transmit machinery
6305591b213SSam Leffler 		 *    clear receive machinery
6315591b213SSam Leffler 		 *    drain and release tx queues
6325591b213SSam Leffler 		 *    reclaim beacon resources
6335591b213SSam Leffler 		 *    reset 802.11 state machine
6345591b213SSam Leffler 		 *    power down hardware
6355591b213SSam Leffler 		 *
6365591b213SSam Leffler 		 * Note that some of this work is not possible if the
6375591b213SSam Leffler 		 * hardware is gone (invalid).
6385591b213SSam Leffler 		 */
6395591b213SSam Leffler 		ifp->if_flags &= ~IFF_RUNNING;
6405591b213SSam Leffler 		ifp->if_timer = 0;
6415591b213SSam Leffler 		if (!sc->sc_invalid)
6425591b213SSam Leffler 			ath_hal_intrset(ah, 0);
6435591b213SSam Leffler 		ath_draintxq(sc);
6445591b213SSam Leffler 		if (!sc->sc_invalid)
6455591b213SSam Leffler 			ath_stoprecv(sc);
6465591b213SSam Leffler 		else
6475591b213SSam Leffler 			sc->sc_rxlink = NULL;
6485591b213SSam Leffler 		IF_DRAIN(&ifp->if_snd);
6495591b213SSam Leffler 		ath_beacon_free(sc);
65045bbf62fSSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
6515591b213SSam Leffler 		if (!sc->sc_invalid)
6525591b213SSam Leffler 			ath_hal_setpower(ah, HAL_PM_FULL_SLEEP, 0);
6535591b213SSam Leffler 	}
654f0b2a0beSSam Leffler 	ATH_UNLOCK(sc);
6555591b213SSam Leffler }
6565591b213SSam Leffler 
6575591b213SSam Leffler /*
6585591b213SSam Leffler  * Reset the hardware w/o losing operational state.  This is
6595591b213SSam Leffler  * basically a more efficient way of doing ath_stop, ath_init,
6605591b213SSam Leffler  * followed by state transitions to the current 802.11
6615591b213SSam Leffler  * operational state.  Used to recover from errors rx overrun
6625591b213SSam Leffler  * and to reset the hardware when rf gain settings must be reset.
6635591b213SSam Leffler  */
6645591b213SSam Leffler static void
6655591b213SSam Leffler ath_reset(struct ath_softc *sc)
6665591b213SSam Leffler {
6675591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
6685591b213SSam Leffler 	struct ifnet *ifp = &ic->ic_if;
6695591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
6705591b213SSam Leffler 	struct ieee80211_channel *c;
6715591b213SSam Leffler 	HAL_STATUS status;
6725591b213SSam Leffler 	HAL_CHANNEL hchan;
6735591b213SSam Leffler 
6745591b213SSam Leffler 	/*
6755591b213SSam Leffler 	 * Convert to a HAL channel description with the flags
6765591b213SSam Leffler 	 * constrained to reflect the current operating mode.
6775591b213SSam Leffler 	 */
6785591b213SSam Leffler 	c = ic->ic_ibss_chan;
6795591b213SSam Leffler 	hchan.channel = c->ic_freq;
6805591b213SSam Leffler 	hchan.channelFlags = ath_chan2flags(ic, c);
6815591b213SSam Leffler 
6825591b213SSam Leffler 	ath_hal_intrset(ah, 0);		/* disable interrupts */
6835591b213SSam Leffler 	ath_draintxq(sc);		/* stop xmit side */
6845591b213SSam Leffler 	ath_stoprecv(sc);		/* stop recv side */
6855591b213SSam Leffler 	/* NB: indicate channel change so we do a full reset */
6865591b213SSam Leffler 	if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_TRUE, &status))
6875591b213SSam Leffler 		if_printf(ifp, "%s: unable to reset hardware; hal status %u\n",
6885591b213SSam Leffler 			__func__, status);
6895591b213SSam Leffler 	ath_hal_intrset(ah, sc->sc_imask);
6905591b213SSam Leffler 	if (ath_startrecv(sc) != 0)	/* restart recv */
6915591b213SSam Leffler 		if_printf(ifp, "%s: unable to start recv logic\n", __func__);
6925591b213SSam Leffler 	ath_start(ifp);			/* restart xmit */
6935591b213SSam Leffler 	if (ic->ic_state == IEEE80211_S_RUN)
6945591b213SSam Leffler 		ath_beacon_config(sc);	/* restart beacons */
6955591b213SSam Leffler }
6965591b213SSam Leffler 
6975591b213SSam Leffler static void
6985591b213SSam Leffler ath_start(struct ifnet *ifp)
6995591b213SSam Leffler {
7005591b213SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
7015591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
7025591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
7035591b213SSam Leffler 	struct ieee80211_node *ni;
7045591b213SSam Leffler 	struct ath_buf *bf;
7055591b213SSam Leffler 	struct mbuf *m;
7065591b213SSam Leffler 	struct ieee80211_frame *wh;
7075591b213SSam Leffler 
7085591b213SSam Leffler 	if ((ifp->if_flags & IFF_RUNNING) == 0 || sc->sc_invalid)
7095591b213SSam Leffler 		return;
7105591b213SSam Leffler 	for (;;) {
7115591b213SSam Leffler 		/*
7125591b213SSam Leffler 		 * Grab a TX buffer and associated resources.
7135591b213SSam Leffler 		 */
714f0b2a0beSSam Leffler 		ATH_TXBUF_LOCK(sc);
7155591b213SSam Leffler 		bf = TAILQ_FIRST(&sc->sc_txbuf);
7165591b213SSam Leffler 		if (bf != NULL)
7175591b213SSam Leffler 			TAILQ_REMOVE(&sc->sc_txbuf, bf, bf_list);
718f0b2a0beSSam Leffler 		ATH_TXBUF_UNLOCK(sc);
7195591b213SSam Leffler 		if (bf == NULL) {
720e325e530SSam Leffler 			DPRINTF(ATH_DEBUG_ANY, ("%s: out of xmit buffers\n",
721e325e530SSam Leffler 				__func__));
7225591b213SSam Leffler 			sc->sc_stats.ast_tx_qstop++;
7235591b213SSam Leffler 			ifp->if_flags |= IFF_OACTIVE;
7245591b213SSam Leffler 			break;
7255591b213SSam Leffler 		}
7265591b213SSam Leffler 		/*
7275591b213SSam Leffler 		 * Poll the management queue for frames; they
7285591b213SSam Leffler 		 * have priority over normal data frames.
7295591b213SSam Leffler 		 */
7305591b213SSam Leffler 		IF_DEQUEUE(&ic->ic_mgtq, m);
7315591b213SSam Leffler 		if (m == NULL) {
7325591b213SSam Leffler 			/*
7335591b213SSam Leffler 			 * No data frames go out unless we're associated.
7345591b213SSam Leffler 			 */
7355591b213SSam Leffler 			if (ic->ic_state != IEEE80211_S_RUN) {
736e325e530SSam Leffler 				DPRINTF(ATH_DEBUG_ANY,
737e325e530SSam Leffler 					("%s: ignore data packet, state %u\n",
738e325e530SSam Leffler 					__func__, ic->ic_state));
7395591b213SSam Leffler 				sc->sc_stats.ast_tx_discard++;
740f0b2a0beSSam Leffler 				ATH_TXBUF_LOCK(sc);
7415591b213SSam Leffler 				TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
742f0b2a0beSSam Leffler 				ATH_TXBUF_UNLOCK(sc);
7435591b213SSam Leffler 				break;
7445591b213SSam Leffler 			}
7455591b213SSam Leffler 			IF_DEQUEUE(&ifp->if_snd, m);
7465591b213SSam Leffler 			if (m == NULL) {
747f0b2a0beSSam Leffler 				ATH_TXBUF_LOCK(sc);
7485591b213SSam Leffler 				TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
749f0b2a0beSSam Leffler 				ATH_TXBUF_UNLOCK(sc);
7505591b213SSam Leffler 				break;
7515591b213SSam Leffler 			}
7525591b213SSam Leffler 			ifp->if_opackets++;
7535591b213SSam Leffler 			BPF_MTAP(ifp, m);
7545591b213SSam Leffler 			/*
7555591b213SSam Leffler 			 * Encapsulate the packet in prep for transmission.
7565591b213SSam Leffler 			 */
7570a915fadSSam Leffler 			m = ieee80211_encap(ifp, m, &ni);
7585591b213SSam Leffler 			if (m == NULL) {
759e325e530SSam Leffler 				DPRINTF(ATH_DEBUG_ANY,
760e325e530SSam Leffler 					("%s: encapsulation failure\n",
761e325e530SSam Leffler 					__func__));
7625591b213SSam Leffler 				sc->sc_stats.ast_tx_encap++;
7635591b213SSam Leffler 				goto bad;
7645591b213SSam Leffler 			}
7655591b213SSam Leffler 			wh = mtod(m, struct ieee80211_frame *);
7665591b213SSam Leffler 			if (ic->ic_flags & IEEE80211_F_WEPON)
7675591b213SSam Leffler 				wh->i_fc[1] |= IEEE80211_FC1_WEP;
7685591b213SSam Leffler 		} else {
7690a915fadSSam Leffler 			/*
7700a915fadSSam Leffler 			 * Hack!  The referenced node pointer is in the
7710a915fadSSam Leffler 			 * rcvif field of the packet header.  This is
7720a915fadSSam Leffler 			 * placed there by ieee80211_mgmt_output because
7730a915fadSSam Leffler 			 * we need to hold the reference with the frame
7740a915fadSSam Leffler 			 * and there's no other way (other than packet
7750a915fadSSam Leffler 			 * tags which we consider too expensive to use)
7760a915fadSSam Leffler 			 * to pass it along.
7770a915fadSSam Leffler 			 */
7780a915fadSSam Leffler 			ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
7790a915fadSSam Leffler 			m->m_pkthdr.rcvif = NULL;
7800a915fadSSam Leffler 
7815591b213SSam Leffler 			wh = mtod(m, struct ieee80211_frame *);
7825591b213SSam Leffler 			if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
7835591b213SSam Leffler 			    IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
7845591b213SSam Leffler 				/* fill time stamp */
7855591b213SSam Leffler 				u_int64_t tsf;
7865591b213SSam Leffler 				u_int32_t *tstamp;
7875591b213SSam Leffler 
7885591b213SSam Leffler 				tsf = ath_hal_gettsf64(ah);
7895591b213SSam Leffler 				/* XXX: adjust 100us delay to xmit */
7905591b213SSam Leffler 				tsf += 100;
7915591b213SSam Leffler 				tstamp = (u_int32_t *)&wh[1];
7925591b213SSam Leffler 				tstamp[0] = htole32(tsf & 0xffffffff);
7935591b213SSam Leffler 				tstamp[1] = htole32(tsf >> 32);
7945591b213SSam Leffler 			}
7955591b213SSam Leffler 			sc->sc_stats.ast_tx_mgmt++;
7965591b213SSam Leffler 		}
79773454c73SSam Leffler 
7985591b213SSam Leffler 		if (ath_tx_start(sc, ni, bf, m)) {
7995591b213SSam Leffler 	bad:
800f0b2a0beSSam Leffler 			ATH_TXBUF_LOCK(sc);
8015591b213SSam Leffler 			TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
802f0b2a0beSSam Leffler 			ATH_TXBUF_UNLOCK(sc);
8035591b213SSam Leffler 			ifp->if_oerrors++;
8040a915fadSSam Leffler 			if (ni && ni != ic->ic_bss)
8050a915fadSSam Leffler 				ieee80211_free_node(ic, ni);
8065591b213SSam Leffler 			continue;
8075591b213SSam Leffler 		}
8085591b213SSam Leffler 
8095591b213SSam Leffler 		sc->sc_tx_timer = 5;
8105591b213SSam Leffler 		ifp->if_timer = 1;
8115591b213SSam Leffler 	}
8125591b213SSam Leffler }
8135591b213SSam Leffler 
8145591b213SSam Leffler static int
8155591b213SSam Leffler ath_media_change(struct ifnet *ifp)
8165591b213SSam Leffler {
8175591b213SSam Leffler 	int error;
8185591b213SSam Leffler 
8195591b213SSam Leffler 	error = ieee80211_media_change(ifp);
8205591b213SSam Leffler 	if (error == ENETRESET) {
8215591b213SSam Leffler 		if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) ==
8225591b213SSam Leffler 		    (IFF_RUNNING|IFF_UP))
8235591b213SSam Leffler 			ath_init(ifp);		/* XXX lose error */
8245591b213SSam Leffler 		error = 0;
8255591b213SSam Leffler 	}
8265591b213SSam Leffler 	return error;
8275591b213SSam Leffler }
8285591b213SSam Leffler 
8295591b213SSam Leffler static void
8305591b213SSam Leffler ath_watchdog(struct ifnet *ifp)
8315591b213SSam Leffler {
8325591b213SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
8335591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
8345591b213SSam Leffler 
8355591b213SSam Leffler 	ifp->if_timer = 0;
8365591b213SSam Leffler 	if ((ifp->if_flags & IFF_RUNNING) == 0 || sc->sc_invalid)
8375591b213SSam Leffler 		return;
8385591b213SSam Leffler 	if (sc->sc_tx_timer) {
8395591b213SSam Leffler 		if (--sc->sc_tx_timer == 0) {
8405591b213SSam Leffler 			if_printf(ifp, "device timeout\n");
8415591b213SSam Leffler #ifdef AR_DEBUG
842e325e530SSam Leffler 			if (ath_debug & ATH_DEBUG_WATCHDOG)
8435591b213SSam Leffler 				ath_hal_dumpstate(sc->sc_ah);
8445591b213SSam Leffler #endif /* AR_DEBUG */
8455591b213SSam Leffler 			ath_init(ifp);		/* XXX ath_reset??? */
8465591b213SSam Leffler 			ifp->if_oerrors++;
8475591b213SSam Leffler 			sc->sc_stats.ast_watchdog++;
8485591b213SSam Leffler 			return;
8495591b213SSam Leffler 		}
8505591b213SSam Leffler 		ifp->if_timer = 1;
8515591b213SSam Leffler 	}
8525591b213SSam Leffler 	if (ic->ic_fixed_rate == -1) {
8535591b213SSam Leffler 		/*
8545591b213SSam Leffler 		 * Run the rate control algorithm if we're not
8555591b213SSam Leffler 		 * locked at a fixed rate.
8565591b213SSam Leffler 		 */
8575591b213SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_STA)
8585591b213SSam Leffler 			ath_rate_ctl(sc, ic->ic_bss);
8595591b213SSam Leffler 		else
8605591b213SSam Leffler 			ieee80211_iterate_nodes(ic, ath_rate_ctl, sc);
8615591b213SSam Leffler 	}
8625591b213SSam Leffler 	ieee80211_watchdog(ifp);
8635591b213SSam Leffler }
8645591b213SSam Leffler 
8655591b213SSam Leffler static int
8665591b213SSam Leffler ath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
8675591b213SSam Leffler {
8685591b213SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
8695591b213SSam Leffler 	struct ifreq *ifr = (struct ifreq *)data;
8705591b213SSam Leffler 	int error = 0;
8715591b213SSam Leffler 
872f0b2a0beSSam Leffler 	ATH_LOCK(sc);
8735591b213SSam Leffler 	switch (cmd) {
8745591b213SSam Leffler 	case SIOCSIFFLAGS:
8755591b213SSam Leffler 		if (ifp->if_flags & IFF_UP) {
8765591b213SSam Leffler 			if (ifp->if_flags & IFF_RUNNING) {
8775591b213SSam Leffler 				/*
8785591b213SSam Leffler 				 * To avoid rescanning another access point,
8795591b213SSam Leffler 				 * do not call ath_init() here.  Instead,
8805591b213SSam Leffler 				 * only reflect promisc mode settings.
8815591b213SSam Leffler 				 */
8825591b213SSam Leffler 				ath_mode_init(sc);
8832075afbaSSam Leffler 			} else {
8842075afbaSSam Leffler 				/*
8852075afbaSSam Leffler 				 * Beware of being called during detach to
8862075afbaSSam Leffler 				 * reset promiscuous mode.  In that case we
8872075afbaSSam Leffler 				 * will still be marked UP but not RUNNING.
8882075afbaSSam Leffler 				 * However trying to re-init the interface
8892075afbaSSam Leffler 				 * is the wrong thing to do as we've already
8902075afbaSSam Leffler 				 * torn down much of our state.  There's
8912075afbaSSam Leffler 				 * probably a better way to deal with this.
8922075afbaSSam Leffler 				 */
8932075afbaSSam Leffler 				if (!sc->sc_invalid)
8945591b213SSam Leffler 					ath_init(ifp);	/* XXX lose error */
8952075afbaSSam Leffler 			}
8965591b213SSam Leffler 		} else
8975591b213SSam Leffler 			ath_stop(ifp);
8985591b213SSam Leffler 		break;
8995591b213SSam Leffler 	case SIOCADDMULTI:
9005591b213SSam Leffler 	case SIOCDELMULTI:
9015591b213SSam Leffler 		/*
9025591b213SSam Leffler 		 * The upper layer has already installed/removed
9035591b213SSam Leffler 		 * the multicast address(es), just recalculate the
9045591b213SSam Leffler 		 * multicast filter for the card.
9055591b213SSam Leffler 		 */
9065591b213SSam Leffler 		if (ifp->if_flags & IFF_RUNNING)
9075591b213SSam Leffler 			ath_mode_init(sc);
9085591b213SSam Leffler 		break;
9095591b213SSam Leffler 	case SIOCGATHSTATS:
9108cec0ab9SSam Leffler 		error = copyout(&sc->sc_stats,
9118cec0ab9SSam Leffler 				ifr->ifr_data, sizeof (sc->sc_stats));
9125591b213SSam Leffler 		break;
9138cec0ab9SSam Leffler 	case SIOCGATHDIAG: {
9148cec0ab9SSam Leffler 		struct ath_diag *ad = (struct ath_diag *)data;
9158cec0ab9SSam Leffler 		struct ath_hal *ah = sc->sc_ah;
9168cec0ab9SSam Leffler 		void *data;
9178cec0ab9SSam Leffler 		u_int size;
9188cec0ab9SSam Leffler 
9198cec0ab9SSam Leffler 		if (ath_hal_getdiagstate(ah, ad->ad_id, &data, &size)) {
9208cec0ab9SSam Leffler 			if (size < ad->ad_size)
9218cec0ab9SSam Leffler 				ad->ad_size = size;
9228cec0ab9SSam Leffler 			if (data)
9238cec0ab9SSam Leffler 				error = copyout(data, ad->ad_data, ad->ad_size);
9248cec0ab9SSam Leffler 		} else
9258cec0ab9SSam Leffler 			error = EINVAL;
9268cec0ab9SSam Leffler 		break;
9278cec0ab9SSam Leffler 	}
9285591b213SSam Leffler 	default:
9295591b213SSam Leffler 		error = ieee80211_ioctl(ifp, cmd, data);
9305591b213SSam Leffler 		if (error == ENETRESET) {
9315591b213SSam Leffler 			if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) ==
9325591b213SSam Leffler 			    (IFF_RUNNING|IFF_UP))
9335591b213SSam Leffler 				ath_init(ifp);		/* XXX lose error */
9345591b213SSam Leffler 			error = 0;
9355591b213SSam Leffler 		}
9365591b213SSam Leffler 		break;
9375591b213SSam Leffler 	}
938f0b2a0beSSam Leffler 	ATH_UNLOCK(sc);
9395591b213SSam Leffler 	return error;
9405591b213SSam Leffler }
9415591b213SSam Leffler 
9425591b213SSam Leffler /*
9435591b213SSam Leffler  * Fill the hardware key cache with key entries.
9445591b213SSam Leffler  */
9455591b213SSam Leffler static void
9465591b213SSam Leffler ath_initkeytable(struct ath_softc *sc)
9475591b213SSam Leffler {
9485591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
9495591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
9505591b213SSam Leffler 	int i;
9515591b213SSam Leffler 
9525591b213SSam Leffler 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
9535591b213SSam Leffler 		struct ieee80211_wepkey *k = &ic->ic_nw_keys[i];
9545591b213SSam Leffler 		if (k->wk_len == 0)
9555591b213SSam Leffler 			ath_hal_keyreset(ah, i);
9565591b213SSam Leffler 		else
9575591b213SSam Leffler 			/* XXX return value */
9585591b213SSam Leffler 			/* NB: this uses HAL_KEYVAL == ieee80211_wepkey */
9595591b213SSam Leffler 			ath_hal_keyset(ah, i, (const HAL_KEYVAL *) k);
9605591b213SSam Leffler 	}
9615591b213SSam Leffler }
9625591b213SSam Leffler 
9634bc0e754SSam Leffler /*
9644bc0e754SSam Leffler  * Calculate the receive filter according to the
9654bc0e754SSam Leffler  * operating mode and state:
9664bc0e754SSam Leffler  *
9674bc0e754SSam Leffler  * o always accept unicast, broadcast, and multicast traffic
9684bc0e754SSam Leffler  * o maintain current state of phy error reception
9694bc0e754SSam Leffler  * o probe request frames are accepted only when operating in
9704bc0e754SSam Leffler  *   hostap, adhoc, or monitor modes
9714bc0e754SSam Leffler  * o enable promiscuous mode according to the interface state
9724bc0e754SSam Leffler  * o accept beacons:
9734bc0e754SSam Leffler  *   - when operating in adhoc mode so the 802.11 layer creates
9744bc0e754SSam Leffler  *     node table entries for peers,
9754bc0e754SSam Leffler  *   - when operating in station mode for collecting rssi data when
9764bc0e754SSam Leffler  *     the station is otherwise quiet, or
9774bc0e754SSam Leffler  *   - when scanning
9784bc0e754SSam Leffler  */
9794bc0e754SSam Leffler static u_int32_t
9804bc0e754SSam Leffler ath_calcrxfilter(struct ath_softc *sc)
9814bc0e754SSam Leffler {
9824bc0e754SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
9834bc0e754SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
9844bc0e754SSam Leffler 	struct ifnet *ifp = &ic->ic_if;
9854bc0e754SSam Leffler 	u_int32_t rfilt;
9864bc0e754SSam Leffler 
9874bc0e754SSam Leffler 	rfilt = (ath_hal_getrxfilter(ah) & HAL_RX_FILTER_PHYERR)
9884bc0e754SSam Leffler 	      | HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST;
9894bc0e754SSam Leffler 	if (ic->ic_opmode != IEEE80211_M_STA)
9904bc0e754SSam Leffler 		rfilt |= HAL_RX_FILTER_PROBEREQ;
9914bc0e754SSam Leffler 	if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
9924bc0e754SSam Leffler 	    (ifp->if_flags & IFF_PROMISC))
9934bc0e754SSam Leffler 		rfilt |= HAL_RX_FILTER_PROM;
9944bc0e754SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA ||
9954bc0e754SSam Leffler 	    ic->ic_opmode == IEEE80211_M_IBSS ||
9964bc0e754SSam Leffler 	    ic->ic_state == IEEE80211_S_SCAN)
9974bc0e754SSam Leffler 		rfilt |= HAL_RX_FILTER_BEACON;
9984bc0e754SSam Leffler 	return rfilt;
9994bc0e754SSam Leffler }
10004bc0e754SSam Leffler 
10015591b213SSam Leffler static void
10025591b213SSam Leffler ath_mode_init(struct ath_softc *sc)
10035591b213SSam Leffler {
10045591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
10055591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
10065591b213SSam Leffler 	struct ifnet *ifp = &ic->ic_if;
10075591b213SSam Leffler 	u_int32_t rfilt, mfilt[2], val;
10085591b213SSam Leffler 	u_int8_t pos;
10095591b213SSam Leffler 	struct ifmultiaddr *ifma;
10105591b213SSam Leffler 
10114bc0e754SSam Leffler 	/* configure rx filter */
10124bc0e754SSam Leffler 	rfilt = ath_calcrxfilter(sc);
10134bc0e754SSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
10144bc0e754SSam Leffler 
10155591b213SSam Leffler 	/* configure operational mode */
10165591b213SSam Leffler 	ath_hal_setopmode(ah, ic->ic_opmode);
10175591b213SSam Leffler 
10185591b213SSam Leffler 	/* calculate and install multicast filter */
10195591b213SSam Leffler 	if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
10205591b213SSam Leffler 		mfilt[0] = mfilt[1] = 0;
10215591b213SSam Leffler 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
10225591b213SSam Leffler 			caddr_t dl;
10235591b213SSam Leffler 
10245591b213SSam Leffler 			/* calculate XOR of eight 6bit values */
10255591b213SSam Leffler 			dl = LLADDR((struct sockaddr_dl *) ifma->ifma_addr);
10265591b213SSam Leffler 			val = LE_READ_4(dl + 0);
10275591b213SSam Leffler 			pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
10285591b213SSam Leffler 			val = LE_READ_4(dl + 3);
10295591b213SSam Leffler 			pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
10305591b213SSam Leffler 			pos &= 0x3f;
10315591b213SSam Leffler 			mfilt[pos / 32] |= (1 << (pos % 32));
10325591b213SSam Leffler 		}
10335591b213SSam Leffler 	} else {
10345591b213SSam Leffler 		mfilt[0] = mfilt[1] = ~0;
10355591b213SSam Leffler 	}
10365591b213SSam Leffler 	ath_hal_setmcastfilter(ah, mfilt[0], mfilt[1]);
1037e325e530SSam Leffler 	DPRINTF(ATH_DEBUG_MODE, ("%s: RX filter 0x%x, MC filter %08x:%08x\n",
1038e325e530SSam Leffler 		__func__, rfilt, mfilt[0], mfilt[1]));
10395591b213SSam Leffler }
10405591b213SSam Leffler 
10415591b213SSam Leffler static void
10425591b213SSam Leffler ath_mbuf_load_cb(void *arg, bus_dma_segment_t *seg, int nseg, bus_size_t mapsize, int error)
10435591b213SSam Leffler {
10445591b213SSam Leffler 	struct ath_buf *bf = arg;
10455591b213SSam Leffler 
10465591b213SSam Leffler 	KASSERT(nseg <= ATH_MAX_SCATTER,
10475591b213SSam Leffler 		("ath_mbuf_load_cb: too many DMA segments %u", nseg));
10485591b213SSam Leffler 	bf->bf_mapsize = mapsize;
10495591b213SSam Leffler 	bf->bf_nseg = nseg;
10505591b213SSam Leffler 	bcopy(seg, bf->bf_segs, nseg * sizeof (seg[0]));
10515591b213SSam Leffler }
10525591b213SSam Leffler 
10535591b213SSam Leffler static int
10545591b213SSam Leffler ath_beacon_alloc(struct ath_softc *sc, struct ieee80211_node *ni)
10555591b213SSam Leffler {
10565591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
10575591b213SSam Leffler 	struct ifnet *ifp = &ic->ic_if;
10585591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
10595591b213SSam Leffler 	struct ieee80211_frame *wh;
10605591b213SSam Leffler 	struct ath_buf *bf;
10615591b213SSam Leffler 	struct ath_desc *ds;
10625591b213SSam Leffler 	struct mbuf *m;
10635591b213SSam Leffler 	int error, pktlen;
10645591b213SSam Leffler 	u_int8_t *frm, rate;
10655591b213SSam Leffler 	u_int16_t capinfo;
10665591b213SSam Leffler 	struct ieee80211_rateset *rs;
10675591b213SSam Leffler 	const HAL_RATE_TABLE *rt;
10685591b213SSam Leffler 
10695591b213SSam Leffler 	bf = sc->sc_bcbuf;
10705591b213SSam Leffler 	if (bf->bf_m != NULL) {
10715591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
10725591b213SSam Leffler 		m_freem(bf->bf_m);
10735591b213SSam Leffler 		bf->bf_m = NULL;
10745591b213SSam Leffler 		bf->bf_node = NULL;
10755591b213SSam Leffler 	}
10765591b213SSam Leffler 	/*
10775591b213SSam Leffler 	 * NB: the beacon data buffer must be 32-bit aligned;
10785591b213SSam Leffler 	 * we assume the mbuf routines will return us something
10795591b213SSam Leffler 	 * with this alignment (perhaps should assert).
10805591b213SSam Leffler 	 */
10815591b213SSam Leffler 	rs = &ni->ni_rates;
10829cccabebSSam Leffler 	pktlen = sizeof (struct ieee80211_frame)
1083abb62a41SSam Leffler 	       + 8 + 2 + 2 + 2+ni->ni_esslen + 2+rs->rs_nrates + 3 + 6;
10845591b213SSam Leffler 	if (rs->rs_nrates > IEEE80211_RATE_SIZE)
10855591b213SSam Leffler 		pktlen += 2;
10865591b213SSam Leffler 	if (pktlen <= MHLEN)
10875591b213SSam Leffler 		MGETHDR(m, M_DONTWAIT, MT_DATA);
10885591b213SSam Leffler 	else
10895591b213SSam Leffler 		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
10905591b213SSam Leffler 	if (m == NULL) {
1091e325e530SSam Leffler 		DPRINTF(ATH_DEBUG_BEACON,
1092e325e530SSam Leffler 			("%s: cannot get mbuf/cluster; size %u\n",
1093e325e530SSam Leffler 			__func__, pktlen));
10945591b213SSam Leffler 		sc->sc_stats.ast_be_nombuf++;
10955591b213SSam Leffler 		return ENOMEM;
10965591b213SSam Leffler 	}
10975591b213SSam Leffler 
10985591b213SSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
10995591b213SSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
11005591b213SSam Leffler 	    IEEE80211_FC0_SUBTYPE_BEACON;
11015591b213SSam Leffler 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
11025591b213SSam Leffler 	*(u_int16_t *)wh->i_dur = 0;
11035591b213SSam Leffler 	memcpy(wh->i_addr1, ifp->if_broadcastaddr, IEEE80211_ADDR_LEN);
11045591b213SSam Leffler 	memcpy(wh->i_addr2, ic->ic_myaddr, IEEE80211_ADDR_LEN);
11055591b213SSam Leffler 	memcpy(wh->i_addr3, ni->ni_bssid, IEEE80211_ADDR_LEN);
11065591b213SSam Leffler 	*(u_int16_t *)wh->i_seq = 0;
11075591b213SSam Leffler 
11085591b213SSam Leffler 	/*
11095591b213SSam Leffler 	 * beacon frame format
11105591b213SSam Leffler 	 *	[8] time stamp
11115591b213SSam Leffler 	 *	[2] beacon interval
11125591b213SSam Leffler 	 *	[2] cabability information
11135591b213SSam Leffler 	 *	[tlv] ssid
11145591b213SSam Leffler 	 *	[tlv] supported rates
11155591b213SSam Leffler 	 *	[tlv] parameter set (IBSS)
11165591b213SSam Leffler 	 *	[tlv] extended supported rates
11175591b213SSam Leffler 	 */
11185591b213SSam Leffler 	frm = (u_int8_t *)&wh[1];
11195591b213SSam Leffler 	memset(frm, 0, 8);	/* timestamp is set by hardware */
11205591b213SSam Leffler 	frm += 8;
11215591b213SSam Leffler 	*(u_int16_t *)frm = htole16(ni->ni_intval);
11225591b213SSam Leffler 	frm += 2;
11235591b213SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS)
11245591b213SSam Leffler 		capinfo = IEEE80211_CAPINFO_IBSS;
11255591b213SSam Leffler 	else
11265591b213SSam Leffler 		capinfo = IEEE80211_CAPINFO_ESS;
11275591b213SSam Leffler 	if (ic->ic_flags & IEEE80211_F_WEPON)
11285591b213SSam Leffler 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
11293065b96eSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
11303065b96eSSam Leffler 	    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
11315591b213SSam Leffler 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
11325591b213SSam Leffler 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
11335591b213SSam Leffler 		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
11345591b213SSam Leffler 	*(u_int16_t *)frm = htole16(capinfo);
11355591b213SSam Leffler 	frm += 2;
11365591b213SSam Leffler 	*frm++ = IEEE80211_ELEMID_SSID;
11375591b213SSam Leffler 	*frm++ = ni->ni_esslen;
11385591b213SSam Leffler 	memcpy(frm, ni->ni_essid, ni->ni_esslen);
11395591b213SSam Leffler 	frm += ni->ni_esslen;
11405591b213SSam Leffler 	frm = ieee80211_add_rates(frm, rs);
1141abb62a41SSam Leffler 	*frm++ = IEEE80211_ELEMID_DSPARMS;
1142abb62a41SSam Leffler 	*frm++ = 1;
1143abb62a41SSam Leffler 	*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
11445591b213SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
11455591b213SSam Leffler 		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
11465591b213SSam Leffler 		*frm++ = 2;
11475591b213SSam Leffler 		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
11485591b213SSam Leffler 	} else {
11495591b213SSam Leffler 		/* TODO: TIM */
11505591b213SSam Leffler 		*frm++ = IEEE80211_ELEMID_TIM;
11515591b213SSam Leffler 		*frm++ = 4;	/* length */
11525591b213SSam Leffler 		*frm++ = 0;	/* DTIM count */
11535591b213SSam Leffler 		*frm++ = 1;	/* DTIM period */
11545591b213SSam Leffler 		*frm++ = 0;	/* bitmap control */
11555591b213SSam Leffler 		*frm++ = 0;	/* Partial Virtual Bitmap (variable length) */
11565591b213SSam Leffler 	}
11575591b213SSam Leffler 	frm = ieee80211_add_xrates(frm, rs);
11585591b213SSam Leffler 	m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
11599cccabebSSam Leffler 	KASSERT(m->m_pkthdr.len <= pktlen,
11609cccabebSSam Leffler 		("beacon bigger than expected, len %u calculated %u",
11619cccabebSSam Leffler 		m->m_pkthdr.len, pktlen));
11625591b213SSam Leffler 
1163e325e530SSam Leffler 	DPRINTF(ATH_DEBUG_BEACON, ("%s: m %p len %u\n", __func__, m, m->m_len));
11645591b213SSam Leffler 	error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m,
11655591b213SSam Leffler 				     ath_mbuf_load_cb, bf,
11665591b213SSam Leffler 				     BUS_DMA_NOWAIT);
11675591b213SSam Leffler 	if (error != 0) {
11685591b213SSam Leffler 		m_freem(m);
11695591b213SSam Leffler 		return error;
11705591b213SSam Leffler 	}
11715591b213SSam Leffler 	KASSERT(bf->bf_nseg == 1,
1172e325e530SSam Leffler 		("%s: multi-segment packet; nseg %u", __func__, bf->bf_nseg));
11735591b213SSam Leffler 	bf->bf_m = m;
11745591b213SSam Leffler 
11755591b213SSam Leffler 	/* setup descriptors */
11765591b213SSam Leffler 	ds = bf->bf_desc;
11775591b213SSam Leffler 
11785591b213SSam Leffler 	ds->ds_link = 0;
11795591b213SSam Leffler 	ds->ds_data = bf->bf_segs[0].ds_addr;
11805591b213SSam Leffler 	/*
11815591b213SSam Leffler 	 * Calculate rate code.
11825591b213SSam Leffler 	 * XXX everything at min xmit rate
11835591b213SSam Leffler 	 */
11845591b213SSam Leffler 	rt = sc->sc_currates;
11855591b213SSam Leffler 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
11866b59f5e3SSam Leffler 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
11875591b213SSam Leffler 		rate = rt->info[0].rateCode | rt->info[0].shortPreamble;
11885591b213SSam Leffler 	else
11895591b213SSam Leffler 		rate = rt->info[0].rateCode;
11905591b213SSam Leffler 	ath_hal_setuptxdesc(ah, ds
11915591b213SSam Leffler 		, m->m_pkthdr.len + IEEE80211_CRC_LEN	/* packet length */
11925591b213SSam Leffler 		, sizeof(struct ieee80211_frame)	/* header length */
11935591b213SSam Leffler 		, HAL_PKT_TYPE_BEACON		/* Atheros packet type */
11945591b213SSam Leffler 		, 0x20				/* txpower XXX */
11955591b213SSam Leffler 		, rate, 1			/* series 0 rate/tries */
11965591b213SSam Leffler 		, HAL_TXKEYIX_INVALID		/* no encryption */
11975591b213SSam Leffler 		, 0				/* antenna mode */
11985591b213SSam Leffler 		, HAL_TXDESC_NOACK		/* no ack for beacons */
11995591b213SSam Leffler 		, 0				/* rts/cts rate */
12005591b213SSam Leffler 		, 0				/* rts/cts duration */
12015591b213SSam Leffler 	);
12025591b213SSam Leffler 	/* NB: beacon's BufLen must be a multiple of 4 bytes */
12039cccabebSSam Leffler 	/* XXX verify mbuf data area covers this roundup */
12045591b213SSam Leffler 	ath_hal_filltxdesc(ah, ds
12055591b213SSam Leffler 		, roundup(bf->bf_segs[0].ds_len, 4)	/* buffer length */
12065591b213SSam Leffler 		, AH_TRUE				/* first segment */
12075591b213SSam Leffler 		, AH_TRUE				/* last segment */
12085591b213SSam Leffler 	);
12095591b213SSam Leffler 
12105591b213SSam Leffler 	return 0;
12115591b213SSam Leffler }
12125591b213SSam Leffler 
12135591b213SSam Leffler static void
12145591b213SSam Leffler ath_beacon_proc(void *arg, int pending)
12155591b213SSam Leffler {
12165591b213SSam Leffler 	struct ath_softc *sc = arg;
12175591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
12185591b213SSam Leffler 	struct ath_buf *bf = sc->sc_bcbuf;
12195591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
12205591b213SSam Leffler 
1221e325e530SSam Leffler 	DPRINTF(ATH_DEBUG_BEACON_PROC, ("%s: pending %u\n", __func__, pending));
12220a915fadSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA ||
12230a915fadSSam Leffler 	    bf == NULL || bf->bf_m == NULL) {
1224e325e530SSam Leffler 		DPRINTF(ATH_DEBUG_ANY, ("%s: ic_flags=%x bf=%p bf_m=%p\n",
12255591b213SSam Leffler 			__func__, ic->ic_flags, bf, bf ? bf->bf_m : NULL));
12265591b213SSam Leffler 		return;
12275591b213SSam Leffler 	}
12280a915fadSSam Leffler 	/* TODO: update beacon to reflect PS poll state */
12295591b213SSam Leffler 	if (!ath_hal_stoptxdma(ah, sc->sc_bhalq)) {
1230e325e530SSam Leffler 		DPRINTF(ATH_DEBUG_ANY, ("%s: beacon queue %u did not stop?\n",
12315591b213SSam Leffler 			__func__, sc->sc_bhalq));
12325591b213SSam Leffler 		return;			/* busy, XXX is this right? */
12335591b213SSam Leffler 	}
12345591b213SSam Leffler 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
12355591b213SSam Leffler 
12365591b213SSam Leffler 	ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr);
12375591b213SSam Leffler 	ath_hal_txstart(ah, sc->sc_bhalq);
1238e325e530SSam Leffler 	DPRINTF(ATH_DEBUG_BEACON_PROC,
1239e325e530SSam Leffler 		("%s: TXDP%u = %p (%p)\n", __func__,
12405591b213SSam Leffler 		sc->sc_bhalq, (caddr_t)bf->bf_daddr, bf->bf_desc));
12415591b213SSam Leffler }
12425591b213SSam Leffler 
12435591b213SSam Leffler static void
12445591b213SSam Leffler ath_beacon_free(struct ath_softc *sc)
12455591b213SSam Leffler {
12465591b213SSam Leffler 	struct ath_buf *bf = sc->sc_bcbuf;
12475591b213SSam Leffler 
12485591b213SSam Leffler 	if (bf->bf_m != NULL) {
12495591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
12505591b213SSam Leffler 		m_freem(bf->bf_m);
12515591b213SSam Leffler 		bf->bf_m = NULL;
12525591b213SSam Leffler 		bf->bf_node = NULL;
12535591b213SSam Leffler 	}
12545591b213SSam Leffler }
12555591b213SSam Leffler 
12565591b213SSam Leffler /*
12575591b213SSam Leffler  * Configure the beacon and sleep timers.
12585591b213SSam Leffler  *
12595591b213SSam Leffler  * When operating as an AP this resets the TSF and sets
12605591b213SSam Leffler  * up the hardware to notify us when we need to issue beacons.
12615591b213SSam Leffler  *
12625591b213SSam Leffler  * When operating in station mode this sets up the beacon
12635591b213SSam Leffler  * timers according to the timestamp of the last received
12645591b213SSam Leffler  * beacon and the current TSF, configures PCF and DTIM
12655591b213SSam Leffler  * handling, programs the sleep registers so the hardware
12665591b213SSam Leffler  * will wakeup in time to receive beacons, and configures
12675591b213SSam Leffler  * the beacon miss handling so we'll receive a BMISS
12685591b213SSam Leffler  * interrupt when we stop seeing beacons from the AP
12695591b213SSam Leffler  * we've associated with.
12705591b213SSam Leffler  */
12715591b213SSam Leffler static void
12725591b213SSam Leffler ath_beacon_config(struct ath_softc *sc)
12735591b213SSam Leffler {
12745591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
12755591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
12765591b213SSam Leffler 	struct ieee80211_node *ni = ic->ic_bss;
12775591b213SSam Leffler 	u_int32_t nexttbtt;
12785591b213SSam Leffler 
12795591b213SSam Leffler 	nexttbtt = (LE_READ_4(ni->ni_tstamp + 4) << 22) |
12805591b213SSam Leffler 	    (LE_READ_4(ni->ni_tstamp) >> 10);
1281e325e530SSam Leffler 	DPRINTF(ATH_DEBUG_BEACON, ("%s: nexttbtt=%u\n", __func__, nexttbtt));
12825591b213SSam Leffler 	nexttbtt += ni->ni_intval;
12836b59f5e3SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA) {
12845591b213SSam Leffler 		HAL_BEACON_STATE bs;
12855591b213SSam Leffler 		u_int32_t bmisstime;
12865591b213SSam Leffler 
12875591b213SSam Leffler 		/* NB: no PCF support right now */
12885591b213SSam Leffler 		memset(&bs, 0, sizeof(bs));
12895591b213SSam Leffler 		bs.bs_intval = ni->ni_intval;
12905591b213SSam Leffler 		bs.bs_nexttbtt = nexttbtt;
12915591b213SSam Leffler 		bs.bs_dtimperiod = bs.bs_intval;
12925591b213SSam Leffler 		bs.bs_nextdtim = nexttbtt;
12935591b213SSam Leffler 		/*
12945591b213SSam Leffler 		 * Calculate the number of consecutive beacons to miss
12955591b213SSam Leffler 		 * before taking a BMISS interrupt.  The configuration
12965591b213SSam Leffler 		 * is specified in ms, so we need to convert that to
12975591b213SSam Leffler 		 * TU's and then calculate based on the beacon interval.
12985591b213SSam Leffler 		 * Note that we clamp the result to at most 10 beacons.
12995591b213SSam Leffler 		 */
13005591b213SSam Leffler 		bmisstime = (ic->ic_bmisstimeout * 1000) / 1024;
13015591b213SSam Leffler 		bs.bs_bmissthreshold = howmany(bmisstime,ni->ni_intval);
13025591b213SSam Leffler 		if (bs.bs_bmissthreshold > 10)
13035591b213SSam Leffler 			bs.bs_bmissthreshold = 10;
13045591b213SSam Leffler 		else if (bs.bs_bmissthreshold <= 0)
13055591b213SSam Leffler 			bs.bs_bmissthreshold = 1;
13065591b213SSam Leffler 
13075591b213SSam Leffler 		/*
13085591b213SSam Leffler 		 * Calculate sleep duration.  The configuration is
13095591b213SSam Leffler 		 * given in ms.  We insure a multiple of the beacon
13105591b213SSam Leffler 		 * period is used.  Also, if the sleep duration is
13115591b213SSam Leffler 		 * greater than the DTIM period then it makes senses
13125591b213SSam Leffler 		 * to make it a multiple of that.
13135591b213SSam Leffler 		 *
13145591b213SSam Leffler 		 * XXX fixed at 100ms
13155591b213SSam Leffler 		 */
13165591b213SSam Leffler 		bs.bs_sleepduration =
13175591b213SSam Leffler 			roundup((100 * 1000) / 1024, bs.bs_intval);
13185591b213SSam Leffler 		if (bs.bs_sleepduration > bs.bs_dtimperiod)
13195591b213SSam Leffler 			bs.bs_sleepduration = roundup(bs.bs_sleepduration, bs.bs_dtimperiod);
13205591b213SSam Leffler 
1321e325e530SSam Leffler 		DPRINTF(ATH_DEBUG_BEACON,
1322e325e530SSam Leffler 			("%s: intval %u nexttbtt %u dtim %u nextdtim %u bmiss %u sleep %u\n"
13235591b213SSam Leffler 			, __func__
13245591b213SSam Leffler 			, bs.bs_intval
13255591b213SSam Leffler 			, bs.bs_nexttbtt
13265591b213SSam Leffler 			, bs.bs_dtimperiod
13275591b213SSam Leffler 			, bs.bs_nextdtim
13285591b213SSam Leffler 			, bs.bs_bmissthreshold
13295591b213SSam Leffler 			, bs.bs_sleepduration
13305591b213SSam Leffler 		));
13315591b213SSam Leffler 		ath_hal_intrset(ah, 0);
13325591b213SSam Leffler 		/*
13335591b213SSam Leffler 		 * Reset our tsf so the hardware will update the
13345591b213SSam Leffler 		 * tsf register to reflect timestamps found in
13355591b213SSam Leffler 		 * received beacons.
13365591b213SSam Leffler 		 */
13375591b213SSam Leffler 		ath_hal_resettsf(ah);
13385591b213SSam Leffler 		ath_hal_beacontimers(ah, &bs, 0/*XXX*/, 0, 0);
13395591b213SSam Leffler 		sc->sc_imask |= HAL_INT_BMISS;
13405591b213SSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
13415591b213SSam Leffler 	} else {
1342e325e530SSam Leffler 		DPRINTF(ATH_DEBUG_BEACON, ("%s: intval %u nexttbtt %u\n",
13435591b213SSam Leffler 			__func__, ni->ni_intval, nexttbtt));
13445591b213SSam Leffler 		ath_hal_intrset(ah, 0);
13455591b213SSam Leffler 		ath_hal_beaconinit(ah, ic->ic_opmode,
13465591b213SSam Leffler 			nexttbtt, ni->ni_intval);
13476b59f5e3SSam Leffler 		if (ic->ic_opmode != IEEE80211_M_MONITOR)
13485591b213SSam Leffler 			sc->sc_imask |= HAL_INT_SWBA;	/* beacon prepare */
13495591b213SSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
13505591b213SSam Leffler 	}
13515591b213SSam Leffler }
13525591b213SSam Leffler 
13535591b213SSam Leffler static void
13545591b213SSam Leffler ath_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
13555591b213SSam Leffler {
13565591b213SSam Leffler 	bus_addr_t *paddr = (bus_addr_t*) arg;
13575591b213SSam Leffler 	*paddr = segs->ds_addr;
13585591b213SSam Leffler }
13595591b213SSam Leffler 
13605591b213SSam Leffler static int
13615591b213SSam Leffler ath_desc_alloc(struct ath_softc *sc)
13625591b213SSam Leffler {
13635591b213SSam Leffler 	int i, bsize, error;
13645591b213SSam Leffler 	struct ath_desc *ds;
13655591b213SSam Leffler 	struct ath_buf *bf;
13665591b213SSam Leffler 
13675591b213SSam Leffler 	/* allocate descriptors */
13685591b213SSam Leffler 	sc->sc_desc_len = sizeof(struct ath_desc) *
13695591b213SSam Leffler 				(ATH_TXBUF * ATH_TXDESC + ATH_RXBUF + 1);
13705591b213SSam Leffler 	error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT, &sc->sc_ddmamap);
13715591b213SSam Leffler 	if (error != 0)
13725591b213SSam Leffler 		return error;
13735591b213SSam Leffler 
13745591b213SSam Leffler 	error = bus_dmamem_alloc(sc->sc_dmat, (void**) &sc->sc_desc,
13755591b213SSam Leffler 				 BUS_DMA_NOWAIT, &sc->sc_ddmamap);
13765591b213SSam Leffler 	if (error != 0)
13775591b213SSam Leffler 		goto fail0;
13785591b213SSam Leffler 
13795591b213SSam Leffler 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_ddmamap,
13805591b213SSam Leffler 				sc->sc_desc, sc->sc_desc_len,
13815591b213SSam Leffler 				ath_load_cb, &sc->sc_desc_paddr,
13825591b213SSam Leffler 				BUS_DMA_NOWAIT);
13835591b213SSam Leffler 	if (error != 0)
13845591b213SSam Leffler 		goto fail1;
13855591b213SSam Leffler 
13865591b213SSam Leffler 	ds = sc->sc_desc;
1387e325e530SSam Leffler 	DPRINTF(ATH_DEBUG_ANY, ("%s: DMA map: %p (%d) -> %p (%lu)\n",
1388e325e530SSam Leffler 	    __func__, ds, sc->sc_desc_len, (caddr_t) sc->sc_desc_paddr,
1389e325e530SSam Leffler 	    /*XXX*/ (u_long) sc->sc_desc_len));
13905591b213SSam Leffler 
13915591b213SSam Leffler 	/* allocate buffers */
13925591b213SSam Leffler 	bsize = sizeof(struct ath_buf) * (ATH_TXBUF + ATH_RXBUF + 1);
13935591b213SSam Leffler 	bf = malloc(bsize, M_DEVBUF, M_NOWAIT | M_ZERO);
13945591b213SSam Leffler 	if (bf == NULL)
13955591b213SSam Leffler 		goto fail2;
13965591b213SSam Leffler 	sc->sc_bufptr = bf;
13975591b213SSam Leffler 
13985591b213SSam Leffler 	TAILQ_INIT(&sc->sc_rxbuf);
13995591b213SSam Leffler 	for (i = 0; i < ATH_RXBUF; i++, bf++, ds++) {
14005591b213SSam Leffler 		bf->bf_desc = ds;
14015591b213SSam Leffler 		bf->bf_daddr = sc->sc_desc_paddr +
14025591b213SSam Leffler 		    ((caddr_t)ds - (caddr_t)sc->sc_desc);
14035591b213SSam Leffler 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
14045591b213SSam Leffler 					  &bf->bf_dmamap);
14055591b213SSam Leffler 		if (error != 0)
14065591b213SSam Leffler 			break;
14075591b213SSam Leffler 		TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
14085591b213SSam Leffler 	}
14095591b213SSam Leffler 
14105591b213SSam Leffler 	TAILQ_INIT(&sc->sc_txbuf);
14115591b213SSam Leffler 	for (i = 0; i < ATH_TXBUF; i++, bf++, ds += ATH_TXDESC) {
14125591b213SSam Leffler 		bf->bf_desc = ds;
14135591b213SSam Leffler 		bf->bf_daddr = sc->sc_desc_paddr +
14145591b213SSam Leffler 		    ((caddr_t)ds - (caddr_t)sc->sc_desc);
14155591b213SSam Leffler 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
14165591b213SSam Leffler 					  &bf->bf_dmamap);
14175591b213SSam Leffler 		if (error != 0)
14185591b213SSam Leffler 			break;
14195591b213SSam Leffler 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
14205591b213SSam Leffler 	}
14215591b213SSam Leffler 	TAILQ_INIT(&sc->sc_txq);
14225591b213SSam Leffler 
14235591b213SSam Leffler 	/* beacon buffer */
14245591b213SSam Leffler 	bf->bf_desc = ds;
14255591b213SSam Leffler 	bf->bf_daddr = sc->sc_desc_paddr + ((caddr_t)ds - (caddr_t)sc->sc_desc);
14265591b213SSam Leffler 	error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT, &bf->bf_dmamap);
14275591b213SSam Leffler 	if (error != 0)
14285591b213SSam Leffler 		return error;
14295591b213SSam Leffler 	sc->sc_bcbuf = bf;
14305591b213SSam Leffler 	return 0;
14315591b213SSam Leffler 
14325591b213SSam Leffler fail2:
14335591b213SSam Leffler 	bus_dmamap_unload(sc->sc_dmat, sc->sc_ddmamap);
14345591b213SSam Leffler fail1:
14355591b213SSam Leffler 	bus_dmamem_free(sc->sc_dmat, sc->sc_desc, sc->sc_ddmamap);
14365591b213SSam Leffler fail0:
14375591b213SSam Leffler 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_ddmamap);
14385591b213SSam Leffler 	sc->sc_ddmamap = NULL;
14395591b213SSam Leffler 	return error;
14405591b213SSam Leffler }
14415591b213SSam Leffler 
14425591b213SSam Leffler static void
14435591b213SSam Leffler ath_desc_free(struct ath_softc *sc)
14445591b213SSam Leffler {
14455591b213SSam Leffler 	struct ath_buf *bf;
14465591b213SSam Leffler 
14475591b213SSam Leffler 	bus_dmamap_unload(sc->sc_dmat, sc->sc_ddmamap);
14485591b213SSam Leffler 	bus_dmamem_free(sc->sc_dmat, sc->sc_desc, sc->sc_ddmamap);
14495591b213SSam Leffler 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_ddmamap);
14505591b213SSam Leffler 
14515591b213SSam Leffler 	TAILQ_FOREACH(bf, &sc->sc_txq, bf_list) {
14525591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
14535591b213SSam Leffler 		bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
14545591b213SSam Leffler 		m_freem(bf->bf_m);
14555591b213SSam Leffler 	}
14565591b213SSam Leffler 	TAILQ_FOREACH(bf, &sc->sc_txbuf, bf_list)
14575591b213SSam Leffler 		bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
14585591b213SSam Leffler 	TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
14595591b213SSam Leffler 		if (bf->bf_m) {
14605591b213SSam Leffler 			bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
14615591b213SSam Leffler 			bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
14625591b213SSam Leffler 			m_freem(bf->bf_m);
14635591b213SSam Leffler 			bf->bf_m = NULL;
14645591b213SSam Leffler 		}
14655591b213SSam Leffler 	}
14665591b213SSam Leffler 	if (sc->sc_bcbuf != NULL) {
14675591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, sc->sc_bcbuf->bf_dmamap);
14685591b213SSam Leffler 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_bcbuf->bf_dmamap);
14695591b213SSam Leffler 		sc->sc_bcbuf = NULL;
14705591b213SSam Leffler 	}
14715591b213SSam Leffler 
14725591b213SSam Leffler 	TAILQ_INIT(&sc->sc_rxbuf);
14735591b213SSam Leffler 	TAILQ_INIT(&sc->sc_txbuf);
14745591b213SSam Leffler 	TAILQ_INIT(&sc->sc_txq);
14755591b213SSam Leffler 	free(sc->sc_bufptr, M_DEVBUF);
14765591b213SSam Leffler 	sc->sc_bufptr = NULL;
14775591b213SSam Leffler }
14785591b213SSam Leffler 
14795591b213SSam Leffler static struct ieee80211_node *
14805591b213SSam Leffler ath_node_alloc(struct ieee80211com *ic)
14815591b213SSam Leffler {
14825591b213SSam Leffler 	struct ath_node *an =
14835591b213SSam Leffler 		malloc(sizeof(struct ath_node), M_DEVBUF, M_NOWAIT | M_ZERO);
1484de5af704SSam Leffler 	if (an) {
1485de5af704SSam Leffler 		int i;
1486de5af704SSam Leffler 		for (i = 0; i < ATH_RHIST_SIZE; i++)
1487de5af704SSam Leffler 			an->an_rx_hist[i].arh_ticks = ATH_RHIST_NOTIME;
1488de5af704SSam Leffler 		an->an_rx_hist_next = ATH_RHIST_SIZE-1;
1489de5af704SSam Leffler 		return &an->an_node;
1490de5af704SSam Leffler 	} else
1491de5af704SSam Leffler 		return NULL;
14925591b213SSam Leffler }
14935591b213SSam Leffler 
14945591b213SSam Leffler static void
14955591b213SSam Leffler ath_node_free(struct ieee80211com *ic, struct ieee80211_node *ni)
14965591b213SSam Leffler {
14975591b213SSam Leffler         struct ath_softc *sc = ic->ic_if.if_softc;
14985591b213SSam Leffler 	struct ath_buf *bf;
14995591b213SSam Leffler 
15005591b213SSam Leffler 	TAILQ_FOREACH(bf, &sc->sc_txq, bf_list) {
15015591b213SSam Leffler 		if (bf->bf_node == ni)
15025591b213SSam Leffler 			bf->bf_node = NULL;
15035591b213SSam Leffler 	}
15045591b213SSam Leffler 	free(ni, M_DEVBUF);
15055591b213SSam Leffler }
15065591b213SSam Leffler 
15075591b213SSam Leffler static void
15085591b213SSam Leffler ath_node_copy(struct ieee80211com *ic,
15095591b213SSam Leffler 	struct ieee80211_node *dst, const struct ieee80211_node *src)
15105591b213SSam Leffler {
15115591b213SSam Leffler 	*(struct ath_node *)dst = *(const struct ath_node *)src;
15125591b213SSam Leffler }
15135591b213SSam Leffler 
1514de5af704SSam Leffler 
1515de5af704SSam Leffler static u_int8_t
1516de5af704SSam Leffler ath_node_getrssi(struct ieee80211com *ic, struct ieee80211_node *ni)
1517de5af704SSam Leffler {
1518de5af704SSam Leffler 	struct ath_node *an = ATH_NODE(ni);
1519de5af704SSam Leffler 	int i, now, nsamples, rssi;
1520de5af704SSam Leffler 
1521de5af704SSam Leffler 	/*
1522de5af704SSam Leffler 	 * Calculate the average over the last second of sampled data.
1523de5af704SSam Leffler 	 */
1524de5af704SSam Leffler 	now = ticks;
1525de5af704SSam Leffler 	nsamples = 0;
1526de5af704SSam Leffler 	rssi = 0;
1527de5af704SSam Leffler 	i = an->an_rx_hist_next;
1528de5af704SSam Leffler 	do {
1529de5af704SSam Leffler 		struct ath_recv_hist *rh = &an->an_rx_hist[i];
1530de5af704SSam Leffler 		if (rh->arh_ticks == ATH_RHIST_NOTIME)
1531de5af704SSam Leffler 			goto done;
1532de5af704SSam Leffler 		if (now - rh->arh_ticks > hz)
1533de5af704SSam Leffler 			goto done;
1534de5af704SSam Leffler 		rssi += rh->arh_rssi;
1535de5af704SSam Leffler 		nsamples++;
1536de5af704SSam Leffler 		if (i == 0)
1537de5af704SSam Leffler 			i = ATH_RHIST_SIZE-1;
1538de5af704SSam Leffler 		else
1539de5af704SSam Leffler 			i--;
1540de5af704SSam Leffler 	} while (i != an->an_rx_hist_next);
1541de5af704SSam Leffler done:
1542de5af704SSam Leffler 	/*
1543de5af704SSam Leffler 	 * Return either the average or the last known
1544de5af704SSam Leffler 	 * value if there is no recent data.
1545de5af704SSam Leffler 	 */
1546de5af704SSam Leffler 	return (nsamples ? rssi / nsamples : an->an_rx_hist[i].arh_rssi);
1547de5af704SSam Leffler }
1548de5af704SSam Leffler 
15495591b213SSam Leffler static int
15505591b213SSam Leffler ath_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf)
15515591b213SSam Leffler {
15525591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
15535591b213SSam Leffler 	int error;
15545591b213SSam Leffler 	struct mbuf *m;
15555591b213SSam Leffler 	struct ath_desc *ds;
15565591b213SSam Leffler 
15575591b213SSam Leffler 	m = bf->bf_m;
15585591b213SSam Leffler 	if (m == NULL) {
15595591b213SSam Leffler 		/*
15605591b213SSam Leffler 		 * NB: by assigning a page to the rx dma buffer we
15615591b213SSam Leffler 		 * implicitly satisfy the Atheros requirement that
15625591b213SSam Leffler 		 * this buffer be cache-line-aligned and sized to be
15635591b213SSam Leffler 		 * multiple of the cache line size.  Not doing this
15645591b213SSam Leffler 		 * causes weird stuff to happen (for the 5210 at least).
15655591b213SSam Leffler 		 */
15665591b213SSam Leffler 		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
15675591b213SSam Leffler 		if (m == NULL) {
1568e325e530SSam Leffler 			DPRINTF(ATH_DEBUG_ANY,
1569e325e530SSam Leffler 				("%s: no mbuf/cluster\n", __func__));
15705591b213SSam Leffler 			sc->sc_stats.ast_rx_nombuf++;
15715591b213SSam Leffler 			return ENOMEM;
15725591b213SSam Leffler 		}
15735591b213SSam Leffler 		bf->bf_m = m;
15745591b213SSam Leffler 		m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
15755591b213SSam Leffler 
15765591b213SSam Leffler 		error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m,
15775591b213SSam Leffler 					     ath_mbuf_load_cb, bf,
15785591b213SSam Leffler 					     BUS_DMA_NOWAIT);
15795591b213SSam Leffler 		if (error != 0) {
1580e325e530SSam Leffler 			DPRINTF(ATH_DEBUG_ANY,
1581e325e530SSam Leffler 				("%s: bus_dmamap_load_mbuf failed; error %d\n",
1582e325e530SSam Leffler 				__func__, error));
15835591b213SSam Leffler 			sc->sc_stats.ast_rx_busdma++;
15845591b213SSam Leffler 			return error;
15855591b213SSam Leffler 		}
15865591b213SSam Leffler 		KASSERT(bf->bf_nseg == 1,
15875591b213SSam Leffler 			("ath_rxbuf_init: multi-segment packet; nseg %u",
15885591b213SSam Leffler 			bf->bf_nseg));
15895591b213SSam Leffler 	}
15905591b213SSam Leffler 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREREAD);
15915591b213SSam Leffler 
159204e22a02SSam Leffler 	/*
159304e22a02SSam Leffler 	 * Setup descriptors.  For receive we always terminate
159404e22a02SSam Leffler 	 * the descriptor list with a self-linked entry so we'll
159504e22a02SSam Leffler 	 * not get overrun under high load (as can happen with a
159604e22a02SSam Leffler 	 * 5212 when ANI processing enables PHY errors).
159704e22a02SSam Leffler 	 *
159804e22a02SSam Leffler 	 * To insure the last descriptor is self-linked we create
159904e22a02SSam Leffler 	 * each descriptor as self-linked and add it to the end.  As
160004e22a02SSam Leffler 	 * each additional descriptor is added the previous self-linked
160104e22a02SSam Leffler 	 * entry is ``fixed'' naturally.  This should be safe even
160204e22a02SSam Leffler 	 * if DMA is happening.  When processing RX interrupts we
160304e22a02SSam Leffler 	 * never remove/process the last, self-linked, entry on the
160404e22a02SSam Leffler 	 * descriptor list.  This insures the hardware always has
160504e22a02SSam Leffler 	 * someplace to write a new frame.
160604e22a02SSam Leffler 	 */
16075591b213SSam Leffler 	ds = bf->bf_desc;
160804e22a02SSam Leffler 	ds->ds_link = bf->bf_daddr;	/* link to self */
16095591b213SSam Leffler 	ds->ds_data = bf->bf_segs[0].ds_addr;
16105591b213SSam Leffler 	ath_hal_setuprxdesc(ah, ds
16115591b213SSam Leffler 		, m->m_len		/* buffer size */
16125591b213SSam Leffler 		, 0
16135591b213SSam Leffler 	);
16145591b213SSam Leffler 
16155591b213SSam Leffler 	if (sc->sc_rxlink != NULL)
16165591b213SSam Leffler 		*sc->sc_rxlink = bf->bf_daddr;
16175591b213SSam Leffler 	sc->sc_rxlink = &ds->ds_link;
16185591b213SSam Leffler 	return 0;
16195591b213SSam Leffler }
16205591b213SSam Leffler 
16215591b213SSam Leffler static void
16225591b213SSam Leffler ath_rx_proc(void *arg, int npending)
16235591b213SSam Leffler {
16248cec0ab9SSam Leffler #define	PA2DESC(_sc, _pa) \
16258cec0ab9SSam Leffler 	((struct ath_desc *)((caddr_t)(_sc)->sc_desc + \
16268cec0ab9SSam Leffler 		((_pa) - (_sc)->sc_desc_paddr)))
16275591b213SSam Leffler 	struct ath_softc *sc = arg;
16285591b213SSam Leffler 	struct ath_buf *bf;
1629d1d0cf62SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
1630d1d0cf62SSam Leffler 	struct ifnet *ifp = &ic->ic_if;
16315591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
16325591b213SSam Leffler 	struct ath_desc *ds;
16335591b213SSam Leffler 	struct mbuf *m;
16345591b213SSam Leffler 	struct ieee80211_frame *wh, whbuf;
16350a915fadSSam Leffler 	struct ieee80211_node *ni;
1636de5af704SSam Leffler 	struct ath_node *an;
1637de5af704SSam Leffler 	struct ath_recv_hist *rh;
16385591b213SSam Leffler 	int len;
16395591b213SSam Leffler 	u_int phyerr;
16405591b213SSam Leffler 	HAL_STATUS status;
16415591b213SSam Leffler 
1642e325e530SSam Leffler 	DPRINTF(ATH_DEBUG_RX_PROC, ("%s: pending %u\n", __func__, npending));
16435591b213SSam Leffler 	do {
16445591b213SSam Leffler 		bf = TAILQ_FIRST(&sc->sc_rxbuf);
16455591b213SSam Leffler 		if (bf == NULL) {		/* NB: shouldn't happen */
16465591b213SSam Leffler 			if_printf(ifp, "ath_rx_proc: no buffer!\n");
16475591b213SSam Leffler 			break;
16485591b213SSam Leffler 		}
164904e22a02SSam Leffler 		ds = bf->bf_desc;
165004e22a02SSam Leffler 		if (ds->ds_link == bf->bf_daddr) {
165104e22a02SSam Leffler 			/* NB: never process the self-linked entry at the end */
165204e22a02SSam Leffler 			break;
165304e22a02SSam Leffler 		}
16545591b213SSam Leffler 		m = bf->bf_m;
16555591b213SSam Leffler 		if (m == NULL) {		/* NB: shouldn't happen */
16565591b213SSam Leffler 			if_printf(ifp, "ath_rx_proc: no mbuf!\n");
16575591b213SSam Leffler 			continue;
16585591b213SSam Leffler 		}
16598cec0ab9SSam Leffler 		/* XXX sync descriptor memory */
16608cec0ab9SSam Leffler 		/*
16618cec0ab9SSam Leffler 		 * Must provide the virtual address of the current
16628cec0ab9SSam Leffler 		 * descriptor, the physical address, and the virtual
16638cec0ab9SSam Leffler 		 * address of the next descriptor in the h/w chain.
16648cec0ab9SSam Leffler 		 * This allows the HAL to look ahead to see if the
16658cec0ab9SSam Leffler 		 * hardware is done with a descriptor by checking the
16668cec0ab9SSam Leffler 		 * done bit in the following descriptor and the address
16678cec0ab9SSam Leffler 		 * of the current descriptor the DMA engine is working
16688cec0ab9SSam Leffler 		 * on.  All this is necessary because of our use of
16698cec0ab9SSam Leffler 		 * a self-linked list to avoid rx overruns.
16708cec0ab9SSam Leffler 		 */
16718cec0ab9SSam Leffler 		status = ath_hal_rxprocdesc(ah, ds,
16728cec0ab9SSam Leffler 				bf->bf_daddr, PA2DESC(sc, ds->ds_link));
16735591b213SSam Leffler #ifdef AR_DEBUG
1674e325e530SSam Leffler 		if (ath_debug & ATH_DEBUG_RECV_DESC)
16755591b213SSam Leffler 			ath_printrxbuf(bf, status == HAL_OK);
16765591b213SSam Leffler #endif
16775591b213SSam Leffler 		if (status == HAL_EINPROGRESS)
16785591b213SSam Leffler 			break;
16795591b213SSam Leffler 		TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
16805591b213SSam Leffler 		if (ds->ds_rxstat.rs_status != 0) {
16815591b213SSam Leffler 			if (ds->ds_rxstat.rs_status & HAL_RXERR_CRC)
16825591b213SSam Leffler 				sc->sc_stats.ast_rx_crcerr++;
16835591b213SSam Leffler 			if (ds->ds_rxstat.rs_status & HAL_RXERR_FIFO)
16845591b213SSam Leffler 				sc->sc_stats.ast_rx_fifoerr++;
16855591b213SSam Leffler 			if (ds->ds_rxstat.rs_status & HAL_RXERR_DECRYPT)
16865591b213SSam Leffler 				sc->sc_stats.ast_rx_badcrypt++;
16875591b213SSam Leffler 			if (ds->ds_rxstat.rs_status & HAL_RXERR_PHY) {
16885591b213SSam Leffler 				sc->sc_stats.ast_rx_phyerr++;
16895591b213SSam Leffler 				phyerr = ds->ds_rxstat.rs_phyerr & 0x1f;
16905591b213SSam Leffler 				sc->sc_stats.ast_rx_phy[phyerr]++;
169185643802SSam Leffler 			} else {
169285643802SSam Leffler 				/*
169385643802SSam Leffler 				 * NB: don't count PHY errors as input errors;
169485643802SSam Leffler 				 * we enable them on the 5212 to collect info
169585643802SSam Leffler 				 * about environmental noise and, in that
169685643802SSam Leffler 				 * setting, they don't really reflect tx/rx
169785643802SSam Leffler 				 * errors.
169885643802SSam Leffler 				 */
169985643802SSam Leffler 				ifp->if_ierrors++;
17005591b213SSam Leffler 			}
17015591b213SSam Leffler 			goto rx_next;
17025591b213SSam Leffler 		}
17035591b213SSam Leffler 
17045591b213SSam Leffler 		len = ds->ds_rxstat.rs_datalen;
1705ecddff40SSam Leffler 		if (len < IEEE80211_MIN_LEN) {
1706e325e530SSam Leffler 			DPRINTF(ATH_DEBUG_RECV, ("%s: short packet %d\n",
1707e325e530SSam Leffler 				__func__, len));
17080a915fadSSam Leffler 			sc->sc_stats.ast_rx_tooshort++;
17095591b213SSam Leffler 			goto rx_next;
17105591b213SSam Leffler 		}
17115591b213SSam Leffler 
17125591b213SSam Leffler 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
17135591b213SSam Leffler 		    BUS_DMASYNC_POSTREAD);
17145591b213SSam Leffler 
17155591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
17165591b213SSam Leffler 		bf->bf_m = NULL;
17175591b213SSam Leffler 		m->m_pkthdr.rcvif = ifp;
17185591b213SSam Leffler 		m->m_pkthdr.len = m->m_len = len;
171973454c73SSam Leffler 
172073454c73SSam Leffler 		if (sc->sc_drvbpf) {
172173454c73SSam Leffler 			sc->sc_rx_th.wr_rate =
172273454c73SSam Leffler 				sc->sc_hwmap[ds->ds_rxstat.rs_rate];
1723437ffe18SSam Leffler 			sc->sc_rx_th.wr_antsignal = ds->ds_rxstat.rs_rssi;
1724437ffe18SSam Leffler 			sc->sc_rx_th.wr_antenna = ds->ds_rxstat.rs_antenna;
172573454c73SSam Leffler 			/* XXX TSF */
172673454c73SSam Leffler 
1727437ffe18SSam Leffler 			bpf_mtap2(sc->sc_drvbpf,
1728437ffe18SSam Leffler 				&sc->sc_rx_th, sizeof(sc->sc_rx_th), m);
17295591b213SSam Leffler 		}
17300a915fadSSam Leffler 
17315591b213SSam Leffler 		m_adj(m, -IEEE80211_CRC_LEN);
1732ecddff40SSam Leffler 		wh = mtod(m, struct ieee80211_frame *);
17335591b213SSam Leffler 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
17345591b213SSam Leffler 			/*
17355591b213SSam Leffler 			 * WEP is decrypted by hardware. Clear WEP bit
17365591b213SSam Leffler 			 * and trim WEP header for ieee80211_input().
17375591b213SSam Leffler 			 */
17385591b213SSam Leffler 			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
17395591b213SSam Leffler 			memcpy(&whbuf, wh, sizeof(whbuf));
17405591b213SSam Leffler 			m_adj(m, IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN);
1741f6dbcc49SSam Leffler 			wh = mtod(m, struct ieee80211_frame *);
1742f6dbcc49SSam Leffler 			memcpy(wh, &whbuf, sizeof(whbuf));
17435591b213SSam Leffler 			/*
17445591b213SSam Leffler 			 * Also trim WEP ICV from the tail.
17455591b213SSam Leffler 			 */
17465591b213SSam Leffler 			m_adj(m, -IEEE80211_WEP_CRCLEN);
17475591b213SSam Leffler 		}
17480a915fadSSam Leffler 
17490a915fadSSam Leffler 		/*
17500a915fadSSam Leffler 		 * Locate the node for sender, track state, and
17510a915fadSSam Leffler 		 * then pass this node (referenced) up to the 802.11
17520a915fadSSam Leffler 		 * layer for its use.  We are required to pass
17530a915fadSSam Leffler 		 * something so we fall back to ic_bss when this frame
17540a915fadSSam Leffler 		 * is from an unknown sender.
17550a915fadSSam Leffler 		 */
17560a915fadSSam Leffler 		if (ic->ic_opmode != IEEE80211_M_STA) {
17570a915fadSSam Leffler 			ni = ieee80211_find_node(ic, wh->i_addr2);
17580a915fadSSam Leffler 			if (ni == NULL)
17590a915fadSSam Leffler 				ni = ieee80211_ref_node(ic->ic_bss);
17600a915fadSSam Leffler 		} else
17610a915fadSSam Leffler 			ni = ieee80211_ref_node(ic->ic_bss);
1762de5af704SSam Leffler 
1763de5af704SSam Leffler 		/*
1764de5af704SSam Leffler 		 * Record driver-specific state.
1765de5af704SSam Leffler 		 */
1766de5af704SSam Leffler 		an = ATH_NODE(ni);
1767de5af704SSam Leffler 		if (++(an->an_rx_hist_next) == ATH_RHIST_SIZE)
1768de5af704SSam Leffler 			an->an_rx_hist_next = 0;
1769de5af704SSam Leffler 		rh = &an->an_rx_hist[an->an_rx_hist_next];
1770de5af704SSam Leffler 		rh->arh_ticks = ticks;
1771de5af704SSam Leffler 		rh->arh_rssi = ds->ds_rxstat.rs_rssi;
1772de5af704SSam Leffler 		rh->arh_antenna = ds->ds_rxstat.rs_antenna;
1773de5af704SSam Leffler 
17740a915fadSSam Leffler 		/*
17750a915fadSSam Leffler 		 * Send frame up for processing.
17760a915fadSSam Leffler 		 */
17770a915fadSSam Leffler 		ieee80211_input(ifp, m, ni,
17780a915fadSSam Leffler 			ds->ds_rxstat.rs_rssi, ds->ds_rxstat.rs_tstamp);
1779de5af704SSam Leffler 
17800a915fadSSam Leffler 		/*
17810a915fadSSam Leffler 		 * The frame may have caused the node to be marked for
17820a915fadSSam Leffler 		 * reclamation (e.g. in response to a DEAUTH message)
17830a915fadSSam Leffler 		 * so use free_node here instead of unref_node.
17840a915fadSSam Leffler 		 */
17850a915fadSSam Leffler 		if (ni == ic->ic_bss)
17860a915fadSSam Leffler 			ieee80211_unref_node(&ni);
17870a915fadSSam Leffler 		else
17880a915fadSSam Leffler 			ieee80211_free_node(ic, ni);
17895591b213SSam Leffler   rx_next:
17905591b213SSam Leffler 		TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
17915591b213SSam Leffler 	} while (ath_rxbuf_init(sc, bf) == 0);
17925591b213SSam Leffler 
17935591b213SSam Leffler 	ath_hal_rxmonitor(ah);			/* rx signal state monitoring */
17945591b213SSam Leffler 	ath_hal_rxena(ah);			/* in case of RXEOL */
17958cec0ab9SSam Leffler #undef PA2DESC
17965591b213SSam Leffler }
17975591b213SSam Leffler 
17985591b213SSam Leffler /*
17995591b213SSam Leffler  * XXX Size of an ACK control frame in bytes.
18005591b213SSam Leffler  */
18015591b213SSam Leffler #define	IEEE80211_ACK_SIZE	(2+2+IEEE80211_ADDR_LEN+4)
18025591b213SSam Leffler 
18035591b213SSam Leffler static int
18045591b213SSam Leffler ath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni, struct ath_buf *bf,
18055591b213SSam Leffler     struct mbuf *m0)
18065591b213SSam Leffler {
18075591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
18085591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
18095591b213SSam Leffler 	struct ifnet *ifp = &sc->sc_ic.ic_if;
18105591b213SSam Leffler 	int i, error, iswep, hdrlen, pktlen;
18115591b213SSam Leffler 	u_int8_t rix, cix, txrate, ctsrate;
18125591b213SSam Leffler 	struct ath_desc *ds;
18135591b213SSam Leffler 	struct mbuf *m;
18145591b213SSam Leffler 	struct ieee80211_frame *wh;
18155591b213SSam Leffler 	u_int32_t iv;
18165591b213SSam Leffler 	u_int8_t *ivp;
18175591b213SSam Leffler 	u_int8_t hdrbuf[sizeof(struct ieee80211_frame) +
18185591b213SSam Leffler 	    IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN];
18195591b213SSam Leffler 	u_int subtype, flags, ctsduration, antenna;
18205591b213SSam Leffler 	HAL_PKT_TYPE atype;
18215591b213SSam Leffler 	const HAL_RATE_TABLE *rt;
18225591b213SSam Leffler 	HAL_BOOL shortPreamble;
18235591b213SSam Leffler 	struct ath_node *an;
18245591b213SSam Leffler 
18255591b213SSam Leffler 	wh = mtod(m0, struct ieee80211_frame *);
18265591b213SSam Leffler 	iswep = wh->i_fc[1] & IEEE80211_FC1_WEP;
18275591b213SSam Leffler 	hdrlen = sizeof(struct ieee80211_frame);
18285591b213SSam Leffler 	pktlen = m0->m_pkthdr.len;
18295591b213SSam Leffler 
18305591b213SSam Leffler 	if (iswep) {
18315591b213SSam Leffler 		memcpy(hdrbuf, mtod(m0, caddr_t), hdrlen);
18325591b213SSam Leffler 		m_adj(m0, hdrlen);
18335591b213SSam Leffler 		M_PREPEND(m0, sizeof(hdrbuf), M_DONTWAIT);
18345591b213SSam Leffler 		if (m0 == NULL) {
18355591b213SSam Leffler 			sc->sc_stats.ast_tx_nombuf++;
18365591b213SSam Leffler 			return ENOMEM;
18375591b213SSam Leffler 		}
18385591b213SSam Leffler 		ivp = hdrbuf + hdrlen;
1839167ecdcaSSam Leffler 		wh = mtod(m0, struct ieee80211_frame *);
18405591b213SSam Leffler 		/*
18415591b213SSam Leffler 		 * XXX
18425591b213SSam Leffler 		 * IV must not duplicate during the lifetime of the key.
18435591b213SSam Leffler 		 * But no mechanism to renew keys is defined in IEEE 802.11
18445591b213SSam Leffler 		 * WEP.  And IV may be duplicated between other stations
18455591b213SSam Leffler 		 * because of the session key itself is shared.
18465591b213SSam Leffler 		 * So we use pseudo random IV for now, though it is not the
18475591b213SSam Leffler 		 * right way.
18485591b213SSam Leffler 		 */
184991101a2aSSam Leffler                 iv = ic->ic_iv;
185091101a2aSSam Leffler 		/*
185191101a2aSSam Leffler 		 * Skip 'bad' IVs from Fluhrer/Mantin/Shamir:
185291101a2aSSam Leffler 		 * (B, 255, N) with 3 <= B < 8
185391101a2aSSam Leffler 		 */
185491101a2aSSam Leffler 		if (iv >= 0x03ff00 && (iv & 0xf8ff00) == 0x00ff00)
185591101a2aSSam Leffler 			iv += 0x000100;
185691101a2aSSam Leffler 		ic->ic_iv = iv + 1;
18575591b213SSam Leffler 		for (i = 0; i < IEEE80211_WEP_IVLEN; i++) {
18585591b213SSam Leffler 			ivp[i] = iv;
18595591b213SSam Leffler 			iv >>= 8;
18605591b213SSam Leffler 		}
18615591b213SSam Leffler 		ivp[i] = sc->sc_ic.ic_wep_txkey << 6;	/* Key ID and pad */
18625591b213SSam Leffler 		memcpy(mtod(m0, caddr_t), hdrbuf, sizeof(hdrbuf));
18635591b213SSam Leffler 		/*
18645591b213SSam Leffler 		 * The ICV length must be included into hdrlen and pktlen.
18655591b213SSam Leffler 		 */
18665591b213SSam Leffler 		hdrlen = sizeof(hdrbuf) + IEEE80211_WEP_CRCLEN;
18675591b213SSam Leffler 		pktlen = m0->m_pkthdr.len + IEEE80211_WEP_CRCLEN;
18685591b213SSam Leffler 	}
18695591b213SSam Leffler 	pktlen += IEEE80211_CRC_LEN;
18705591b213SSam Leffler 
18715591b213SSam Leffler 	/*
18725591b213SSam Leffler 	 * Load the DMA map so any coalescing is done.  This
18735591b213SSam Leffler 	 * also calculates the number of descriptors we need.
18745591b213SSam Leffler 	 */
18755591b213SSam Leffler 	error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m0,
18765591b213SSam Leffler 				     ath_mbuf_load_cb, bf,
18775591b213SSam Leffler 				     BUS_DMA_NOWAIT);
187800a12f3aSSam Leffler 	if (error == EFBIG) {
187900a12f3aSSam Leffler 		/* XXX packet requires too many descriptors */
188000a12f3aSSam Leffler 		bf->bf_nseg = ATH_TXDESC+1;
188100a12f3aSSam Leffler 	} else if (error != 0) {
18825591b213SSam Leffler 		sc->sc_stats.ast_tx_busdma++;
18835591b213SSam Leffler 		m_freem(m0);
18845591b213SSam Leffler 		return error;
18855591b213SSam Leffler 	}
18865591b213SSam Leffler 	/*
18875591b213SSam Leffler 	 * Discard null packets and check for packets that
18885591b213SSam Leffler 	 * require too many TX descriptors.  We try to convert
18895591b213SSam Leffler 	 * the latter to a cluster.
18905591b213SSam Leffler 	 */
18915591b213SSam Leffler 	if (bf->bf_nseg > ATH_TXDESC) {		/* too many desc's, linearize */
18925591b213SSam Leffler 		sc->sc_stats.ast_tx_linear++;
18935591b213SSam Leffler 		MGETHDR(m, M_DONTWAIT, MT_DATA);
18945591b213SSam Leffler 		if (m == NULL) {
18955591b213SSam Leffler 			sc->sc_stats.ast_tx_nombuf++;
18965591b213SSam Leffler 			m_freem(m0);
18975591b213SSam Leffler 			return ENOMEM;
18985591b213SSam Leffler 		}
18995591b213SSam Leffler 		M_MOVE_PKTHDR(m, m0);
19005591b213SSam Leffler 		MCLGET(m, M_DONTWAIT);
19015591b213SSam Leffler 		if ((m->m_flags & M_EXT) == 0) {
19025591b213SSam Leffler 			sc->sc_stats.ast_tx_nomcl++;
19035591b213SSam Leffler 			m_freem(m0);
19045591b213SSam Leffler 			m_free(m);
19055591b213SSam Leffler 			return ENOMEM;
19065591b213SSam Leffler 		}
19075591b213SSam Leffler 		m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
19085591b213SSam Leffler 		m_freem(m0);
19095591b213SSam Leffler 		m->m_len = m->m_pkthdr.len;
19105591b213SSam Leffler 		m0 = m;
19115591b213SSam Leffler 		error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m0,
19125591b213SSam Leffler 					     ath_mbuf_load_cb, bf,
19135591b213SSam Leffler 					     BUS_DMA_NOWAIT);
19145591b213SSam Leffler 		if (error != 0) {
19155591b213SSam Leffler 			sc->sc_stats.ast_tx_busdma++;
19165591b213SSam Leffler 			m_freem(m0);
19175591b213SSam Leffler 			return error;
19185591b213SSam Leffler 		}
19195591b213SSam Leffler 		KASSERT(bf->bf_nseg == 1,
19205591b213SSam Leffler 			("ath_tx_start: packet not one segment; nseg %u",
19215591b213SSam Leffler 			bf->bf_nseg));
19225591b213SSam Leffler 	} else if (bf->bf_nseg == 0) {		/* null packet, discard */
19235591b213SSam Leffler 		sc->sc_stats.ast_tx_nodata++;
19245591b213SSam Leffler 		m_freem(m0);
19255591b213SSam Leffler 		return EIO;
19265591b213SSam Leffler 	}
1927e325e530SSam Leffler 	DPRINTF(ATH_DEBUG_XMIT, ("%s: m %p len %u\n", __func__, m0, pktlen));
19285591b213SSam Leffler 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
19295591b213SSam Leffler 	bf->bf_m = m0;
19300a915fadSSam Leffler 	bf->bf_node = ni;			/* NB: held reference */
19315591b213SSam Leffler 
19325591b213SSam Leffler 	/* setup descriptors */
19335591b213SSam Leffler 	ds = bf->bf_desc;
19345591b213SSam Leffler 	rt = sc->sc_currates;
19355591b213SSam Leffler 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
19365591b213SSam Leffler 
19375591b213SSam Leffler 	/*
19385591b213SSam Leffler 	 * Calculate Atheros packet type from IEEE80211 packet header
19395591b213SSam Leffler 	 * and setup for rate calculations.
19405591b213SSam Leffler 	 */
19415591b213SSam Leffler 	atype = HAL_PKT_TYPE_NORMAL;			/* default */
19425591b213SSam Leffler 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
19435591b213SSam Leffler 	case IEEE80211_FC0_TYPE_MGT:
19445591b213SSam Leffler 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
19455591b213SSam Leffler 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON)
19465591b213SSam Leffler 			atype = HAL_PKT_TYPE_BEACON;
19475591b213SSam Leffler 		else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
19485591b213SSam Leffler 			atype = HAL_PKT_TYPE_PROBE_RESP;
19495591b213SSam Leffler 		else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM)
19505591b213SSam Leffler 			atype = HAL_PKT_TYPE_ATIM;
19515591b213SSam Leffler 		rix = 0;			/* XXX lowest rate */
19525591b213SSam Leffler 		break;
19535591b213SSam Leffler 	case IEEE80211_FC0_TYPE_CTL:
19545591b213SSam Leffler 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
19555591b213SSam Leffler 		if (subtype == IEEE80211_FC0_SUBTYPE_PS_POLL)
19565591b213SSam Leffler 			atype = HAL_PKT_TYPE_PSPOLL;
19575591b213SSam Leffler 		rix = 0;			/* XXX lowest rate */
19585591b213SSam Leffler 		break;
19595591b213SSam Leffler 	default:
19605591b213SSam Leffler 		rix = sc->sc_rixmap[ni->ni_rates.rs_rates[ni->ni_txrate] &
19615591b213SSam Leffler 				IEEE80211_RATE_VAL];
19625591b213SSam Leffler 		if (rix == 0xff) {
19635591b213SSam Leffler 			if_printf(ifp, "bogus xmit rate 0x%x\n",
19645591b213SSam Leffler 				ni->ni_rates.rs_rates[ni->ni_txrate]);
19655591b213SSam Leffler 			sc->sc_stats.ast_tx_badrate++;
19665591b213SSam Leffler 			m_freem(m0);
19675591b213SSam Leffler 			return EIO;
19685591b213SSam Leffler 		}
19695591b213SSam Leffler 		break;
19705591b213SSam Leffler 	}
19715591b213SSam Leffler 	/*
19725591b213SSam Leffler 	 * NB: the 802.11 layer marks whether or not we should
19735591b213SSam Leffler 	 * use short preamble based on the current mode and
19745591b213SSam Leffler 	 * negotiated parameters.
19755591b213SSam Leffler 	 */
1976f6aa038bSSam Leffler 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
1977f6aa038bSSam Leffler 	    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
19785591b213SSam Leffler 		txrate = rt->info[rix].rateCode | rt->info[rix].shortPreamble;
19795591b213SSam Leffler 		shortPreamble = AH_TRUE;
19805591b213SSam Leffler 		sc->sc_stats.ast_tx_shortpre++;
19815591b213SSam Leffler 	} else {
19825591b213SSam Leffler 		txrate = rt->info[rix].rateCode;
19835591b213SSam Leffler 		shortPreamble = AH_FALSE;
19845591b213SSam Leffler 	}
19855591b213SSam Leffler 
19865591b213SSam Leffler 	/*
19875591b213SSam Leffler 	 * Calculate miscellaneous flags.
19885591b213SSam Leffler 	 */
19895591b213SSam Leffler 	flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for wep errors */
19905591b213SSam Leffler 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
19915591b213SSam Leffler 		flags |= HAL_TXDESC_NOACK;	/* no ack on broad/multicast */
19925591b213SSam Leffler 		sc->sc_stats.ast_tx_noack++;
19935591b213SSam Leffler 	} else if (pktlen > ic->ic_rtsthreshold) {
19945591b213SSam Leffler 		flags |= HAL_TXDESC_RTSENA;	/* RTS based on frame length */
19955591b213SSam Leffler 		sc->sc_stats.ast_tx_rts++;
19965591b213SSam Leffler 	}
19975591b213SSam Leffler 
19985591b213SSam Leffler 	/*
1999f6aa038bSSam Leffler 	 * Calculate duration.  This logically belongs in the 802.11
2000f6aa038bSSam Leffler 	 * layer but it lacks sufficient information to calculate it.
2001f6aa038bSSam Leffler 	 */
2002f6aa038bSSam Leffler 	if ((flags & HAL_TXDESC_NOACK) == 0 &&
2003f6aa038bSSam Leffler 	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL) {
2004f6aa038bSSam Leffler 		u_int16_t dur;
2005f6aa038bSSam Leffler 		/*
2006f6aa038bSSam Leffler 		 * XXX not right with fragmentation.
2007f6aa038bSSam Leffler 		 */
2008f6aa038bSSam Leffler 		dur = ath_hal_computetxtime(ah, rt, IEEE80211_ACK_SIZE,
2009f6aa038bSSam Leffler 				rix, shortPreamble);
2010f6aa038bSSam Leffler 		*((u_int16_t*) wh->i_dur) = htole16(dur);
2011f6aa038bSSam Leffler 	}
2012f6aa038bSSam Leffler 
2013f6aa038bSSam Leffler 	/*
20145591b213SSam Leffler 	 * Calculate RTS/CTS rate and duration if needed.
20155591b213SSam Leffler 	 */
20165591b213SSam Leffler 	ctsduration = 0;
20175591b213SSam Leffler 	if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA)) {
20185591b213SSam Leffler 		/*
20195591b213SSam Leffler 		 * CTS transmit rate is derived from the transmit rate
20205591b213SSam Leffler 		 * by looking in the h/w rate table.  We must also factor
20215591b213SSam Leffler 		 * in whether or not a short preamble is to be used.
20225591b213SSam Leffler 		 */
20235591b213SSam Leffler 		cix = rt->info[rix].controlRate;
20245591b213SSam Leffler 		ctsrate = rt->info[cix].rateCode;
20255591b213SSam Leffler 		if (shortPreamble)
20265591b213SSam Leffler 			ctsrate |= rt->info[cix].shortPreamble;
20275591b213SSam Leffler 		/*
20285591b213SSam Leffler 		 * Compute the transmit duration based on the size
20295591b213SSam Leffler 		 * of an ACK frame.  We call into the HAL to do the
20305591b213SSam Leffler 		 * computation since it depends on the characteristics
20315591b213SSam Leffler 		 * of the actual PHY being used.
20325591b213SSam Leffler 		 */
20335591b213SSam Leffler 		if (flags & HAL_TXDESC_RTSENA) {	/* SIFS + CTS */
20345591b213SSam Leffler 			ctsduration += ath_hal_computetxtime(ah,
20355591b213SSam Leffler 				rt, IEEE80211_ACK_SIZE, cix, shortPreamble);
20365591b213SSam Leffler 		}
20375591b213SSam Leffler 		/* SIFS + data */
20385591b213SSam Leffler 		ctsduration += ath_hal_computetxtime(ah,
20395591b213SSam Leffler 			rt, pktlen, rix, shortPreamble);
20405591b213SSam Leffler 		if ((flags & HAL_TXDESC_NOACK) == 0) {	/* SIFS + ACK */
20415591b213SSam Leffler 			ctsduration += ath_hal_computetxtime(ah,
20425591b213SSam Leffler 				rt, IEEE80211_ACK_SIZE, cix, shortPreamble);
20435591b213SSam Leffler 		}
20445591b213SSam Leffler 	} else
20455591b213SSam Leffler 		ctsrate = 0;
20465591b213SSam Leffler 
20475591b213SSam Leffler 	/*
20485591b213SSam Leffler 	 * For now use the antenna on which the last good
20495591b213SSam Leffler 	 * frame was received on.  We assume this field is
20505591b213SSam Leffler 	 * initialized to 0 which gives us ``auto'' or the
20515591b213SSam Leffler 	 * ``default'' antenna.
20525591b213SSam Leffler 	 */
20535591b213SSam Leffler 	an = (struct ath_node *) ni;
20545591b213SSam Leffler 	if (an->an_tx_antenna)
20555591b213SSam Leffler 		antenna = an->an_tx_antenna;
20565591b213SSam Leffler 	else
2057de5af704SSam Leffler 		antenna = an->an_rx_hist[an->an_rx_hist_next].arh_antenna;
20585591b213SSam Leffler 
2059eb2cdcb1SSam Leffler 	if (ic->ic_rawbpf)
2060eb2cdcb1SSam Leffler 		bpf_mtap(ic->ic_rawbpf, m0);
2061eb2cdcb1SSam Leffler 	if (sc->sc_drvbpf) {
2062eb2cdcb1SSam Leffler 		sc->sc_tx_th.wt_flags = 0;
2063eb2cdcb1SSam Leffler 		if (shortPreamble)
2064eb2cdcb1SSam Leffler 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2065eb2cdcb1SSam Leffler 		if (iswep)
2066eb2cdcb1SSam Leffler 			sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2067eb2cdcb1SSam Leffler 		sc->sc_tx_th.wt_rate = ni->ni_rates.rs_rates[ni->ni_txrate];
2068eb2cdcb1SSam Leffler 		sc->sc_tx_th.wt_txpower = 60/2;		/* XXX */
2069eb2cdcb1SSam Leffler 		sc->sc_tx_th.wt_antenna = antenna;
2070eb2cdcb1SSam Leffler 
2071eb2cdcb1SSam Leffler 		bpf_mtap2(sc->sc_drvbpf,
2072eb2cdcb1SSam Leffler 			&sc->sc_tx_th, sizeof(sc->sc_tx_th), m0);
2073eb2cdcb1SSam Leffler 	}
2074eb2cdcb1SSam Leffler 
20755591b213SSam Leffler 	/*
20765591b213SSam Leffler 	 * Formulate first tx descriptor with tx controls.
20775591b213SSam Leffler 	 */
20785591b213SSam Leffler 	/* XXX check return value? */
20795591b213SSam Leffler 	ath_hal_setuptxdesc(ah, ds
20805591b213SSam Leffler 		, pktlen		/* packet length */
20815591b213SSam Leffler 		, hdrlen		/* header length */
20825591b213SSam Leffler 		, atype			/* Atheros packet type */
20835591b213SSam Leffler 		, 60			/* txpower XXX */
20845591b213SSam Leffler 		, txrate, 1+10		/* series 0 rate/tries */
20855591b213SSam Leffler 		, iswep ? sc->sc_ic.ic_wep_txkey : HAL_TXKEYIX_INVALID
20865591b213SSam Leffler 		, antenna		/* antenna mode */
20875591b213SSam Leffler 		, flags			/* flags */
20885591b213SSam Leffler 		, ctsrate		/* rts/cts rate */
20895591b213SSam Leffler 		, ctsduration		/* rts/cts duration */
20905591b213SSam Leffler 	);
20915591b213SSam Leffler #ifdef notyet
20925591b213SSam Leffler 	ath_hal_setupxtxdesc(ah, ds
20935591b213SSam Leffler 		, AH_FALSE		/* short preamble */
20945591b213SSam Leffler 		, 0, 0			/* series 1 rate/tries */
20955591b213SSam Leffler 		, 0, 0			/* series 2 rate/tries */
20965591b213SSam Leffler 		, 0, 0			/* series 3 rate/tries */
20975591b213SSam Leffler 	);
20985591b213SSam Leffler #endif
20995591b213SSam Leffler 	/*
21005591b213SSam Leffler 	 * Fillin the remainder of the descriptor info.
21015591b213SSam Leffler 	 */
21025591b213SSam Leffler 	for (i = 0; i < bf->bf_nseg; i++, ds++) {
21035591b213SSam Leffler 		ds->ds_data = bf->bf_segs[i].ds_addr;
21045591b213SSam Leffler 		if (i == bf->bf_nseg - 1)
21055591b213SSam Leffler 			ds->ds_link = 0;
21065591b213SSam Leffler 		else
21075591b213SSam Leffler 			ds->ds_link = bf->bf_daddr + sizeof(*ds) * (i + 1);
21085591b213SSam Leffler 		ath_hal_filltxdesc(ah, ds
21095591b213SSam Leffler 			, bf->bf_segs[i].ds_len	/* segment length */
21105591b213SSam Leffler 			, i == 0		/* first segment */
21115591b213SSam Leffler 			, i == bf->bf_nseg - 1	/* last segment */
21125591b213SSam Leffler 		);
2113e325e530SSam Leffler 		DPRINTF(ATH_DEBUG_XMIT,
2114e325e530SSam Leffler 			("%s: %d: %08x %08x %08x %08x %08x %08x\n",
2115e325e530SSam Leffler 			__func__, i, ds->ds_link, ds->ds_data,
2116e325e530SSam Leffler 			ds->ds_ctl0, ds->ds_ctl1, ds->ds_hw[0], ds->ds_hw[1]));
21175591b213SSam Leffler 	}
21185591b213SSam Leffler 
21195591b213SSam Leffler 	/*
21205591b213SSam Leffler 	 * Insert the frame on the outbound list and
21215591b213SSam Leffler 	 * pass it on to the hardware.
21225591b213SSam Leffler 	 */
2123f0b2a0beSSam Leffler 	ATH_TXQ_LOCK(sc);
21245591b213SSam Leffler 	TAILQ_INSERT_TAIL(&sc->sc_txq, bf, bf_list);
21255591b213SSam Leffler 	if (sc->sc_txlink == NULL) {
21265591b213SSam Leffler 		ath_hal_puttxbuf(ah, sc->sc_txhalq, bf->bf_daddr);
2127e325e530SSam Leffler 		DPRINTF(ATH_DEBUG_XMIT, ("%s: TXDP0 = %p (%p)\n", __func__,
21285591b213SSam Leffler 		    (caddr_t)bf->bf_daddr, bf->bf_desc));
21295591b213SSam Leffler 	} else {
21305591b213SSam Leffler 		*sc->sc_txlink = bf->bf_daddr;
2131e325e530SSam Leffler 		DPRINTF(ATH_DEBUG_XMIT, ("%s: link(%p)=%p (%p)\n", __func__,
21325591b213SSam Leffler 		    sc->sc_txlink, (caddr_t)bf->bf_daddr, bf->bf_desc));
21335591b213SSam Leffler 	}
21345591b213SSam Leffler 	sc->sc_txlink = &bf->bf_desc[bf->bf_nseg - 1].ds_link;
2135f0b2a0beSSam Leffler 	ATH_TXQ_UNLOCK(sc);
21365591b213SSam Leffler 
21375591b213SSam Leffler 	ath_hal_txstart(ah, sc->sc_txhalq);
21385591b213SSam Leffler 	return 0;
21395591b213SSam Leffler }
21405591b213SSam Leffler 
21415591b213SSam Leffler static void
21425591b213SSam Leffler ath_tx_proc(void *arg, int npending)
21435591b213SSam Leffler {
21445591b213SSam Leffler 	struct ath_softc *sc = arg;
21455591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
21465591b213SSam Leffler 	struct ath_buf *bf;
21470a915fadSSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
21480a915fadSSam Leffler 	struct ifnet *ifp = &ic->ic_if;
21495591b213SSam Leffler 	struct ath_desc *ds;
21505591b213SSam Leffler 	struct ieee80211_node *ni;
21515591b213SSam Leffler 	struct ath_node *an;
21525591b213SSam Leffler 	int sr, lr;
21535591b213SSam Leffler 	HAL_STATUS status;
21545591b213SSam Leffler 
2155e325e530SSam Leffler 	DPRINTF(ATH_DEBUG_TX_PROC, ("%s: pending %u tx queue %p, link %p\n",
2156e325e530SSam Leffler 		__func__, npending,
2157e325e530SSam Leffler 		(caddr_t) ath_hal_gettxbuf(sc->sc_ah, sc->sc_txhalq),
21585591b213SSam Leffler 		sc->sc_txlink));
21595591b213SSam Leffler 	for (;;) {
2160f0b2a0beSSam Leffler 		ATH_TXQ_LOCK(sc);
21615591b213SSam Leffler 		bf = TAILQ_FIRST(&sc->sc_txq);
21625591b213SSam Leffler 		if (bf == NULL) {
21635591b213SSam Leffler 			sc->sc_txlink = NULL;
2164f0b2a0beSSam Leffler 			ATH_TXQ_UNLOCK(sc);
21655591b213SSam Leffler 			break;
21665591b213SSam Leffler 		}
21675591b213SSam Leffler 		/* only the last descriptor is needed */
21685591b213SSam Leffler 		ds = &bf->bf_desc[bf->bf_nseg - 1];
21695591b213SSam Leffler 		status = ath_hal_txprocdesc(ah, ds);
21705591b213SSam Leffler #ifdef AR_DEBUG
2171e325e530SSam Leffler 		if (ath_debug & ATH_DEBUG_XMIT_DESC)
21725591b213SSam Leffler 			ath_printtxbuf(bf, status == HAL_OK);
21735591b213SSam Leffler #endif
21745591b213SSam Leffler 		if (status == HAL_EINPROGRESS) {
2175f0b2a0beSSam Leffler 			ATH_TXQ_UNLOCK(sc);
21765591b213SSam Leffler 			break;
21775591b213SSam Leffler 		}
21785591b213SSam Leffler 		TAILQ_REMOVE(&sc->sc_txq, bf, bf_list);
2179f0b2a0beSSam Leffler 		ATH_TXQ_UNLOCK(sc);
21805591b213SSam Leffler 
21815591b213SSam Leffler 		ni = bf->bf_node;
21825591b213SSam Leffler 		if (ni != NULL) {
21835591b213SSam Leffler 			an = (struct ath_node *) ni;
21845591b213SSam Leffler 			if (ds->ds_txstat.ts_status == 0) {
21855591b213SSam Leffler 				an->an_tx_ok++;
21865591b213SSam Leffler 				an->an_tx_antenna = ds->ds_txstat.ts_antenna;
21875591b213SSam Leffler 			} else {
21885591b213SSam Leffler 				an->an_tx_err++;
21895591b213SSam Leffler 				ifp->if_oerrors++;
21905591b213SSam Leffler 				if (ds->ds_txstat.ts_status & HAL_TXERR_XRETRY)
21915591b213SSam Leffler 					sc->sc_stats.ast_tx_xretries++;
21925591b213SSam Leffler 				if (ds->ds_txstat.ts_status & HAL_TXERR_FIFO)
21935591b213SSam Leffler 					sc->sc_stats.ast_tx_fifoerr++;
21945591b213SSam Leffler 				if (ds->ds_txstat.ts_status & HAL_TXERR_FILT)
21955591b213SSam Leffler 					sc->sc_stats.ast_tx_filtered++;
21965591b213SSam Leffler 				an->an_tx_antenna = 0;	/* invalidate */
21975591b213SSam Leffler 			}
21985591b213SSam Leffler 			sr = ds->ds_txstat.ts_shortretry;
21995591b213SSam Leffler 			lr = ds->ds_txstat.ts_longretry;
22005591b213SSam Leffler 			sc->sc_stats.ast_tx_shortretry += sr;
22015591b213SSam Leffler 			sc->sc_stats.ast_tx_longretry += lr;
22025591b213SSam Leffler 			if (sr + lr)
22035591b213SSam Leffler 				an->an_tx_retr++;
22040a915fadSSam Leffler 			/*
22050a915fadSSam Leffler 			 * Reclaim reference to node.
22060a915fadSSam Leffler 			 *
22070a915fadSSam Leffler 			 * NB: the node may be reclaimed here if, for example
22080a915fadSSam Leffler 			 *     this is a DEAUTH message that was sent and the
22090a915fadSSam Leffler 			 *     node was timed out due to inactivity.
22100a915fadSSam Leffler 			 */
22110a915fadSSam Leffler 			if (ni != ic->ic_bss)
22120a915fadSSam Leffler 				ieee80211_free_node(ic, ni);
22135591b213SSam Leffler 		}
22145591b213SSam Leffler 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
22155591b213SSam Leffler 		    BUS_DMASYNC_POSTWRITE);
22165591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
22175591b213SSam Leffler 		m_freem(bf->bf_m);
22185591b213SSam Leffler 		bf->bf_m = NULL;
22195591b213SSam Leffler 		bf->bf_node = NULL;
22205591b213SSam Leffler 
2221f0b2a0beSSam Leffler 		ATH_TXBUF_LOCK(sc);
22225591b213SSam Leffler 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
2223f0b2a0beSSam Leffler 		ATH_TXBUF_UNLOCK(sc);
22245591b213SSam Leffler 	}
22255591b213SSam Leffler 	ifp->if_flags &= ~IFF_OACTIVE;
22265591b213SSam Leffler 	sc->sc_tx_timer = 0;
22275591b213SSam Leffler 
22285591b213SSam Leffler 	ath_start(ifp);
22295591b213SSam Leffler }
22305591b213SSam Leffler 
22315591b213SSam Leffler /*
22325591b213SSam Leffler  * Drain the transmit queue and reclaim resources.
22335591b213SSam Leffler  */
22345591b213SSam Leffler static void
22355591b213SSam Leffler ath_draintxq(struct ath_softc *sc)
22365591b213SSam Leffler {
22375591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
22385591b213SSam Leffler 	struct ifnet *ifp = &sc->sc_ic.ic_if;
22395591b213SSam Leffler 	struct ath_buf *bf;
22405591b213SSam Leffler 
22415591b213SSam Leffler 	/* XXX return value */
22425591b213SSam Leffler 	if (!sc->sc_invalid) {
22435591b213SSam Leffler 		/* don't touch the hardware if marked invalid */
22445591b213SSam Leffler 		(void) ath_hal_stoptxdma(ah, sc->sc_txhalq);
2245e325e530SSam Leffler 		DPRINTF(ATH_DEBUG_RESET,
2246e325e530SSam Leffler 		    ("%s: tx queue %p, link %p\n", __func__,
22475591b213SSam Leffler 		    (caddr_t) ath_hal_gettxbuf(ah, sc->sc_txhalq),
22485591b213SSam Leffler 		    sc->sc_txlink));
22495591b213SSam Leffler 		(void) ath_hal_stoptxdma(ah, sc->sc_bhalq);
2250e325e530SSam Leffler 		DPRINTF(ATH_DEBUG_RESET,
2251e325e530SSam Leffler 		    ("%s: beacon queue %p\n", __func__,
22525591b213SSam Leffler 		    (caddr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq)));
22535591b213SSam Leffler 	}
22545591b213SSam Leffler 	for (;;) {
2255f0b2a0beSSam Leffler 		ATH_TXQ_LOCK(sc);
22565591b213SSam Leffler 		bf = TAILQ_FIRST(&sc->sc_txq);
22575591b213SSam Leffler 		if (bf == NULL) {
22585591b213SSam Leffler 			sc->sc_txlink = NULL;
2259f0b2a0beSSam Leffler 			ATH_TXQ_UNLOCK(sc);
22605591b213SSam Leffler 			break;
22615591b213SSam Leffler 		}
22625591b213SSam Leffler 		TAILQ_REMOVE(&sc->sc_txq, bf, bf_list);
2263f0b2a0beSSam Leffler 		ATH_TXQ_UNLOCK(sc);
22645591b213SSam Leffler #ifdef AR_DEBUG
2265e325e530SSam Leffler 		if (ath_debug & ATH_DEBUG_RESET)
22665591b213SSam Leffler 			ath_printtxbuf(bf,
22675591b213SSam Leffler 				ath_hal_txprocdesc(ah, bf->bf_desc) == HAL_OK);
22685591b213SSam Leffler #endif /* AR_DEBUG */
22695591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
22705591b213SSam Leffler 		m_freem(bf->bf_m);
22715591b213SSam Leffler 		bf->bf_m = NULL;
22725591b213SSam Leffler 		bf->bf_node = NULL;
2273f0b2a0beSSam Leffler 		ATH_TXBUF_LOCK(sc);
22745591b213SSam Leffler 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
2275f0b2a0beSSam Leffler 		ATH_TXBUF_UNLOCK(sc);
22765591b213SSam Leffler 	}
22775591b213SSam Leffler 	ifp->if_flags &= ~IFF_OACTIVE;
22785591b213SSam Leffler 	sc->sc_tx_timer = 0;
22795591b213SSam Leffler }
22805591b213SSam Leffler 
22815591b213SSam Leffler /*
22825591b213SSam Leffler  * Disable the receive h/w in preparation for a reset.
22835591b213SSam Leffler  */
22845591b213SSam Leffler static void
22855591b213SSam Leffler ath_stoprecv(struct ath_softc *sc)
22865591b213SSam Leffler {
22878cec0ab9SSam Leffler #define	PA2DESC(_sc, _pa) \
22888cec0ab9SSam Leffler 	((struct ath_desc *)((caddr_t)(_sc)->sc_desc + \
22898cec0ab9SSam Leffler 		((_pa) - (_sc)->sc_desc_paddr)))
22905591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
22915591b213SSam Leffler 
22925591b213SSam Leffler 	ath_hal_stoppcurecv(ah);	/* disable PCU */
22935591b213SSam Leffler 	ath_hal_setrxfilter(ah, 0);	/* clear recv filter */
22945591b213SSam Leffler 	ath_hal_stopdmarecv(ah);	/* disable DMA engine */
22955591b213SSam Leffler 	DELAY(3000);			/* long enough for 1 frame */
22965591b213SSam Leffler #ifdef AR_DEBUG
2297e325e530SSam Leffler 	if (ath_debug & ATH_DEBUG_RESET) {
22985591b213SSam Leffler 		struct ath_buf *bf;
22995591b213SSam Leffler 
2300e325e530SSam Leffler 		printf("%s: rx queue %p, link %p\n", __func__,
2301e325e530SSam Leffler 			(caddr_t) ath_hal_getrxbuf(ah), sc->sc_rxlink);
23025591b213SSam Leffler 		TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
23038cec0ab9SSam Leffler 			struct ath_desc *ds = bf->bf_desc;
23048cec0ab9SSam Leffler 			if (ath_hal_rxprocdesc(ah, ds, bf->bf_daddr,
23058cec0ab9SSam Leffler 			    PA2DESC(sc, ds->ds_link)) == HAL_OK)
23065591b213SSam Leffler 				ath_printrxbuf(bf, 1);
23075591b213SSam Leffler 		}
23085591b213SSam Leffler 	}
23095591b213SSam Leffler #endif
23105591b213SSam Leffler 	sc->sc_rxlink = NULL;		/* just in case */
23118cec0ab9SSam Leffler #undef PA2DESC
23125591b213SSam Leffler }
23135591b213SSam Leffler 
23145591b213SSam Leffler /*
23155591b213SSam Leffler  * Enable the receive h/w following a reset.
23165591b213SSam Leffler  */
23175591b213SSam Leffler static int
23185591b213SSam Leffler ath_startrecv(struct ath_softc *sc)
23195591b213SSam Leffler {
23205591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
23215591b213SSam Leffler 	struct ath_buf *bf;
23225591b213SSam Leffler 
23235591b213SSam Leffler 	sc->sc_rxlink = NULL;
23245591b213SSam Leffler 	TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
23255591b213SSam Leffler 		int error = ath_rxbuf_init(sc, bf);
23265591b213SSam Leffler 		if (error != 0) {
2327e325e530SSam Leffler 			DPRINTF(ATH_DEBUG_RECV,
2328e325e530SSam Leffler 				("%s: ath_rxbuf_init failed %d\n",
2329e325e530SSam Leffler 				__func__, error));
23305591b213SSam Leffler 			return error;
23315591b213SSam Leffler 		}
23325591b213SSam Leffler 	}
23335591b213SSam Leffler 
23345591b213SSam Leffler 	bf = TAILQ_FIRST(&sc->sc_rxbuf);
23355591b213SSam Leffler 	ath_hal_putrxbuf(ah, bf->bf_daddr);
23365591b213SSam Leffler 	ath_hal_rxena(ah);		/* enable recv descriptors */
23375591b213SSam Leffler 	ath_mode_init(sc);		/* set filters, etc. */
23385591b213SSam Leffler 	ath_hal_startpcurecv(ah);	/* re-enable PCU/DMA engine */
23395591b213SSam Leffler 	return 0;
23405591b213SSam Leffler }
23415591b213SSam Leffler 
23425591b213SSam Leffler /*
23435591b213SSam Leffler  * Set/change channels.  If the channel is really being changed,
23445591b213SSam Leffler  * it's done by resetting the chip.  To accomplish this we must
23455591b213SSam Leffler  * first cleanup any pending DMA, then restart stuff after a la
23465591b213SSam Leffler  * ath_init.
23475591b213SSam Leffler  */
23485591b213SSam Leffler static int
23495591b213SSam Leffler ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan)
23505591b213SSam Leffler {
23515591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
23525591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
23535591b213SSam Leffler 
2354e325e530SSam Leffler 	DPRINTF(ATH_DEBUG_ANY, ("%s: %u (%u MHz) -> %u (%u MHz)\n", __func__,
23555591b213SSam Leffler 	    ieee80211_chan2ieee(ic, ic->ic_ibss_chan),
23565591b213SSam Leffler 		ic->ic_ibss_chan->ic_freq,
23575591b213SSam Leffler 	    ieee80211_chan2ieee(ic, chan), chan->ic_freq));
23585591b213SSam Leffler 	if (chan != ic->ic_ibss_chan) {
23595591b213SSam Leffler 		HAL_STATUS status;
23605591b213SSam Leffler 		HAL_CHANNEL hchan;
23615591b213SSam Leffler 		enum ieee80211_phymode mode;
23625591b213SSam Leffler 
23635591b213SSam Leffler 		/*
23645591b213SSam Leffler 		 * To switch channels clear any pending DMA operations;
23655591b213SSam Leffler 		 * wait long enough for the RX fifo to drain, reset the
23665591b213SSam Leffler 		 * hardware at the new frequency, and then re-enable
23675591b213SSam Leffler 		 * the relevant bits of the h/w.
23685591b213SSam Leffler 		 */
23695591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
23705591b213SSam Leffler 		ath_draintxq(sc);		/* clear pending tx frames */
23715591b213SSam Leffler 		ath_stoprecv(sc);		/* turn off frame recv */
23725591b213SSam Leffler 		/*
23735591b213SSam Leffler 		 * Convert to a HAL channel description with
23745591b213SSam Leffler 		 * the flags constrained to reflect the current
23755591b213SSam Leffler 		 * operating mode.
23765591b213SSam Leffler 		 */
23775591b213SSam Leffler 		hchan.channel = chan->ic_freq;
23785591b213SSam Leffler 		hchan.channelFlags = ath_chan2flags(ic, chan);
23795591b213SSam Leffler 		if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_TRUE, &status)) {
23805591b213SSam Leffler 			if_printf(&ic->ic_if, "ath_chan_set: unable to reset "
23815591b213SSam Leffler 				"channel %u (%u Mhz)\n",
23825591b213SSam Leffler 				ieee80211_chan2ieee(ic, chan), chan->ic_freq);
23835591b213SSam Leffler 			return EIO;
23845591b213SSam Leffler 		}
23855591b213SSam Leffler 		/*
23865591b213SSam Leffler 		 * Re-enable rx framework.
23875591b213SSam Leffler 		 */
23885591b213SSam Leffler 		if (ath_startrecv(sc) != 0) {
23895591b213SSam Leffler 			if_printf(&ic->ic_if,
23905591b213SSam Leffler 				"ath_chan_set: unable to restart recv logic\n");
23915591b213SSam Leffler 			return EIO;
23925591b213SSam Leffler 		}
23935591b213SSam Leffler 
23945591b213SSam Leffler 		/*
239573454c73SSam Leffler 		 * Update BPF state.
239673454c73SSam Leffler 		 */
239773454c73SSam Leffler 		sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
239873454c73SSam Leffler 			htole16(chan->ic_freq);
239973454c73SSam Leffler 		sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =
240073454c73SSam Leffler 			htole16(chan->ic_flags);
240173454c73SSam Leffler 
240273454c73SSam Leffler 		/*
24035591b213SSam Leffler 		 * Change channels and update the h/w rate map
24045591b213SSam Leffler 		 * if we're switching; e.g. 11a to 11b/g.
24055591b213SSam Leffler 		 */
24065591b213SSam Leffler 		ic->ic_ibss_chan = chan;
24075591b213SSam Leffler 		mode = ieee80211_chan2mode(ic, chan);
24085591b213SSam Leffler 		if (mode != sc->sc_curmode)
24095591b213SSam Leffler 			ath_setcurmode(sc, mode);
24100a915fadSSam Leffler 
24110a915fadSSam Leffler 		/*
24120a915fadSSam Leffler 		 * Re-enable interrupts.
24130a915fadSSam Leffler 		 */
24140a915fadSSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
24155591b213SSam Leffler 	}
24165591b213SSam Leffler 	return 0;
24175591b213SSam Leffler }
24185591b213SSam Leffler 
24195591b213SSam Leffler static void
24205591b213SSam Leffler ath_next_scan(void *arg)
24215591b213SSam Leffler {
24225591b213SSam Leffler 	struct ath_softc *sc = arg;
24235591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
24245591b213SSam Leffler 	struct ifnet *ifp = &ic->ic_if;
24255591b213SSam Leffler 
24265591b213SSam Leffler 	if (ic->ic_state == IEEE80211_S_SCAN)
24275591b213SSam Leffler 		ieee80211_next_scan(ifp);
24285591b213SSam Leffler }
24295591b213SSam Leffler 
24305591b213SSam Leffler /*
24315591b213SSam Leffler  * Periodically recalibrate the PHY to account
24325591b213SSam Leffler  * for temperature/environment changes.
24335591b213SSam Leffler  */
24345591b213SSam Leffler static void
24355591b213SSam Leffler ath_calibrate(void *arg)
24365591b213SSam Leffler {
24375591b213SSam Leffler 	struct ath_softc *sc = arg;
24385591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
24395591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
24405591b213SSam Leffler 	struct ieee80211_channel *c;
24415591b213SSam Leffler 	HAL_CHANNEL hchan;
24425591b213SSam Leffler 
24435591b213SSam Leffler 	sc->sc_stats.ast_per_cal++;
24445591b213SSam Leffler 
24455591b213SSam Leffler 	/*
24465591b213SSam Leffler 	 * Convert to a HAL channel description with the flags
24475591b213SSam Leffler 	 * constrained to reflect the current operating mode.
24485591b213SSam Leffler 	 */
24495591b213SSam Leffler 	c = ic->ic_ibss_chan;
24505591b213SSam Leffler 	hchan.channel = c->ic_freq;
24515591b213SSam Leffler 	hchan.channelFlags = ath_chan2flags(ic, c);
24525591b213SSam Leffler 
2453e325e530SSam Leffler 	DPRINTF(ATH_DEBUG_CALIBRATE,
2454e325e530SSam Leffler 		("%s: channel %u/%x\n", __func__, c->ic_freq, c->ic_flags));
24555591b213SSam Leffler 
24565591b213SSam Leffler 	if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) {
24575591b213SSam Leffler 		/*
24585591b213SSam Leffler 		 * Rfgain is out of bounds, reset the chip
24595591b213SSam Leffler 		 * to load new gain values.
24605591b213SSam Leffler 		 */
24615591b213SSam Leffler 		sc->sc_stats.ast_per_rfgain++;
24625591b213SSam Leffler 		ath_reset(sc);
24635591b213SSam Leffler 	}
24645591b213SSam Leffler 	if (!ath_hal_calibrate(ah, &hchan)) {
2465e325e530SSam Leffler 		DPRINTF(ATH_DEBUG_ANY,
2466e325e530SSam Leffler 			("%s: calibration of channel %u failed\n",
24675591b213SSam Leffler 			__func__, c->ic_freq));
24685591b213SSam Leffler 		sc->sc_stats.ast_per_calfail++;
24695591b213SSam Leffler 	}
24705591b213SSam Leffler 	callout_reset(&sc->sc_cal_ch, hz * ath_calinterval, ath_calibrate, sc);
24715591b213SSam Leffler }
24725591b213SSam Leffler 
24735591b213SSam Leffler static int
247445bbf62fSSam Leffler ath_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
24755591b213SSam Leffler {
24765591b213SSam Leffler 	struct ifnet *ifp = &ic->ic_if;
247745bbf62fSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
247845bbf62fSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
24795591b213SSam Leffler 	struct ieee80211_node *ni;
24805591b213SSam Leffler 	int i, error;
24818cec0ab9SSam Leffler 	const u_int8_t *bssid;
24825591b213SSam Leffler 	u_int32_t rfilt;
24835591b213SSam Leffler 	static const HAL_LED_STATE leds[] = {
24845591b213SSam Leffler 	    HAL_LED_INIT,	/* IEEE80211_S_INIT */
24855591b213SSam Leffler 	    HAL_LED_SCAN,	/* IEEE80211_S_SCAN */
24865591b213SSam Leffler 	    HAL_LED_AUTH,	/* IEEE80211_S_AUTH */
24875591b213SSam Leffler 	    HAL_LED_ASSOC, 	/* IEEE80211_S_ASSOC */
24885591b213SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_RUN */
24895591b213SSam Leffler 	};
24905591b213SSam Leffler 
2491e325e530SSam Leffler 	DPRINTF(ATH_DEBUG_ANY, ("%s: %s -> %s\n", __func__,
249245bbf62fSSam Leffler 		ieee80211_state_name[ic->ic_state],
249345bbf62fSSam Leffler 		ieee80211_state_name[nstate]));
24945591b213SSam Leffler 
24955591b213SSam Leffler 	ath_hal_setledstate(ah, leds[nstate]);	/* set LED */
24965591b213SSam Leffler 
24975591b213SSam Leffler 	if (nstate == IEEE80211_S_INIT) {
24985591b213SSam Leffler 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
24995591b213SSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
250045bbf62fSSam Leffler 		callout_stop(&sc->sc_scan_ch);
250145bbf62fSSam Leffler 		callout_stop(&sc->sc_cal_ch);
250245bbf62fSSam Leffler 		return (*sc->sc_newstate)(ic, nstate, arg);
25035591b213SSam Leffler 	}
25045591b213SSam Leffler 	ni = ic->ic_bss;
25055591b213SSam Leffler 	error = ath_chan_set(sc, ni->ni_chan);
25065591b213SSam Leffler 	if (error != 0)
25075591b213SSam Leffler 		goto bad;
25084bc0e754SSam Leffler 	rfilt = ath_calcrxfilter(sc);
25095591b213SSam Leffler 	if (nstate == IEEE80211_S_SCAN) {
25105591b213SSam Leffler 		callout_reset(&sc->sc_scan_ch, (hz * ath_dwelltime) / 1000,
25115591b213SSam Leffler 			ath_next_scan, sc);
25125591b213SSam Leffler 		bssid = ifp->if_broadcastaddr;
25135591b213SSam Leffler 	} else {
25145591b213SSam Leffler 		callout_stop(&sc->sc_scan_ch);
25155591b213SSam Leffler 		bssid = ni->ni_bssid;
25165591b213SSam Leffler 	}
25175591b213SSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
2518e325e530SSam Leffler 	DPRINTF(ATH_DEBUG_ANY, ("%s: RX filter 0x%x bssid %s\n",
25195591b213SSam Leffler 		 __func__, rfilt, ether_sprintf(bssid)));
25205591b213SSam Leffler 
25215591b213SSam Leffler 	if (nstate == IEEE80211_S_RUN && ic->ic_opmode == IEEE80211_M_STA)
25225591b213SSam Leffler 		ath_hal_setassocid(ah, bssid, ni->ni_associd);
25235591b213SSam Leffler 	else
25245591b213SSam Leffler 		ath_hal_setassocid(ah, bssid, 0);
25255591b213SSam Leffler 	if (ic->ic_flags & IEEE80211_F_WEPON) {
25265591b213SSam Leffler 		for (i = 0; i < IEEE80211_WEP_NKID; i++)
25275591b213SSam Leffler 			if (ath_hal_keyisvalid(ah, i))
25285591b213SSam Leffler 				ath_hal_keysetmac(ah, i, bssid);
25295591b213SSam Leffler 	}
25305591b213SSam Leffler 
25315591b213SSam Leffler 	if (nstate == IEEE80211_S_RUN) {
2532e325e530SSam Leffler 		DPRINTF(ATH_DEBUG_ANY, ("%s(RUN): ic_flags=0x%08x iv=%d bssid=%s "
25335591b213SSam Leffler 			"capinfo=0x%04x chan=%d\n"
25345591b213SSam Leffler 			 , __func__
25355591b213SSam Leffler 			 , ic->ic_flags
25365591b213SSam Leffler 			 , ni->ni_intval
25375591b213SSam Leffler 			 , ether_sprintf(ni->ni_bssid)
25385591b213SSam Leffler 			 , ni->ni_capinfo
25395591b213SSam Leffler 			 , ieee80211_chan2ieee(ic, ni->ni_chan)));
25405591b213SSam Leffler 
25415591b213SSam Leffler 		/*
25425591b213SSam Leffler 		 * Allocate and setup the beacon frame for AP or adhoc mode.
25435591b213SSam Leffler 		 */
25446b59f5e3SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
25456b59f5e3SSam Leffler 		    ic->ic_opmode == IEEE80211_M_IBSS) {
25465591b213SSam Leffler 			error = ath_beacon_alloc(sc, ni);
25475591b213SSam Leffler 			if (error != 0)
25485591b213SSam Leffler 				goto bad;
25495591b213SSam Leffler 		}
25505591b213SSam Leffler 
25515591b213SSam Leffler 		/*
25525591b213SSam Leffler 		 * Configure the beacon and sleep timers.
25535591b213SSam Leffler 		 */
25545591b213SSam Leffler 		ath_beacon_config(sc);
25555591b213SSam Leffler 
25565591b213SSam Leffler 		/* start periodic recalibration timer */
25575591b213SSam Leffler 		callout_reset(&sc->sc_cal_ch, hz * ath_calinterval,
25585591b213SSam Leffler 			ath_calibrate, sc);
25595591b213SSam Leffler 	} else {
25605591b213SSam Leffler 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
25615591b213SSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
25625591b213SSam Leffler 		callout_stop(&sc->sc_cal_ch);		/* no calibration */
25635591b213SSam Leffler 	}
25645591b213SSam Leffler 	/*
25655591b213SSam Leffler 	 * Reset the rate control state.
25665591b213SSam Leffler 	 */
25675591b213SSam Leffler 	ath_rate_ctl_reset(sc, nstate);
256845bbf62fSSam Leffler 	/*
256945bbf62fSSam Leffler 	 * Invoke the parent method to complete the work.
257045bbf62fSSam Leffler 	 */
257145bbf62fSSam Leffler 	return (*sc->sc_newstate)(ic, nstate, arg);
25725591b213SSam Leffler bad:
25735591b213SSam Leffler 	callout_stop(&sc->sc_scan_ch);
25745591b213SSam Leffler 	callout_stop(&sc->sc_cal_ch);
257545bbf62fSSam Leffler 	/* NB: do not invoke the parent */
25765591b213SSam Leffler 	return error;
25775591b213SSam Leffler }
25785591b213SSam Leffler 
25795591b213SSam Leffler /*
25805591b213SSam Leffler  * Setup driver-specific state for a newly associated node.
25815591b213SSam Leffler  * Note that we're called also on a re-associate, the isnew
25825591b213SSam Leffler  * param tells us if this is the first time or not.
25835591b213SSam Leffler  */
25845591b213SSam Leffler static void
25855591b213SSam Leffler ath_newassoc(struct ieee80211com *ic, struct ieee80211_node *ni, int isnew)
25865591b213SSam Leffler {
25875591b213SSam Leffler 	if (isnew) {
25885591b213SSam Leffler 		struct ath_node *an = (struct ath_node *) ni;
25895591b213SSam Leffler 
25905591b213SSam Leffler 		an->an_tx_ok = an->an_tx_err =
25915591b213SSam Leffler 			an->an_tx_retr = an->an_tx_upper = 0;
25925591b213SSam Leffler 		/* start with highest negotiated rate */
25935591b213SSam Leffler 		/*
25945591b213SSam Leffler 		 * XXX should do otherwise but only when
25955591b213SSam Leffler 		 * the rate control algorithm is better.
25965591b213SSam Leffler 		 */
25975591b213SSam Leffler 		KASSERT(ni->ni_rates.rs_nrates > 0,
25985591b213SSam Leffler 			("new association w/ no rates!"));
25995591b213SSam Leffler 		ni->ni_txrate = ni->ni_rates.rs_nrates - 1;
26005591b213SSam Leffler 	}
26015591b213SSam Leffler }
26025591b213SSam Leffler 
26035591b213SSam Leffler static int
26045591b213SSam Leffler ath_getchannels(struct ath_softc *sc, u_int cc, HAL_BOOL outdoor)
26055591b213SSam Leffler {
26065591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
26075591b213SSam Leffler 	struct ifnet *ifp = &ic->ic_if;
26085591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
26095591b213SSam Leffler 	HAL_CHANNEL *chans;
26105591b213SSam Leffler 	int i, ix, nchan;
26115591b213SSam Leffler 
26125591b213SSam Leffler 	chans = malloc(IEEE80211_CHAN_MAX * sizeof(HAL_CHANNEL),
26135591b213SSam Leffler 			M_TEMP, M_NOWAIT);
26145591b213SSam Leffler 	if (chans == NULL) {
26155591b213SSam Leffler 		if_printf(ifp, "unable to allocate channel table\n");
26165591b213SSam Leffler 		return ENOMEM;
26175591b213SSam Leffler 	}
26185591b213SSam Leffler 	if (!ath_hal_init_channels(ah, chans, IEEE80211_CHAN_MAX, &nchan,
261945cabbdcSSam Leffler 	    cc, HAL_MODE_ALL, outdoor)) {
26205591b213SSam Leffler 		if_printf(ifp, "unable to collect channel list from hal\n");
26215591b213SSam Leffler 		free(chans, M_TEMP);
26225591b213SSam Leffler 		return EINVAL;
26235591b213SSam Leffler 	}
26245591b213SSam Leffler 
26255591b213SSam Leffler 	/*
26265591b213SSam Leffler 	 * Convert HAL channels to ieee80211 ones and insert
26275591b213SSam Leffler 	 * them in the table according to their channel number.
26285591b213SSam Leffler 	 */
26295591b213SSam Leffler 	for (i = 0; i < nchan; i++) {
26305591b213SSam Leffler 		HAL_CHANNEL *c = &chans[i];
26315591b213SSam Leffler 		ix = ath_hal_mhz2ieee(c->channel, c->channelFlags);
26325591b213SSam Leffler 		if (ix > IEEE80211_CHAN_MAX) {
26335591b213SSam Leffler 			if_printf(ifp, "bad hal channel %u (%u/%x) ignored\n",
26345591b213SSam Leffler 				ix, c->channel, c->channelFlags);
26355591b213SSam Leffler 			continue;
26365591b213SSam Leffler 		}
26375591b213SSam Leffler 		/* NB: flags are known to be compatible */
26385591b213SSam Leffler 		if (ic->ic_channels[ix].ic_freq == 0) {
26395591b213SSam Leffler 			ic->ic_channels[ix].ic_freq = c->channel;
26405591b213SSam Leffler 			ic->ic_channels[ix].ic_flags = c->channelFlags;
26415591b213SSam Leffler 		} else {
26425591b213SSam Leffler 			/* channels overlap; e.g. 11g and 11b */
26435591b213SSam Leffler 			ic->ic_channels[ix].ic_flags |= c->channelFlags;
26445591b213SSam Leffler 		}
26455591b213SSam Leffler 	}
26465591b213SSam Leffler 	free(chans, M_TEMP);
26475591b213SSam Leffler 	return 0;
26485591b213SSam Leffler }
26495591b213SSam Leffler 
26505591b213SSam Leffler static int
26515591b213SSam Leffler ath_rate_setup(struct ath_softc *sc, u_int mode)
26525591b213SSam Leffler {
26535591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
26545591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
26555591b213SSam Leffler 	const HAL_RATE_TABLE *rt;
26565591b213SSam Leffler 	struct ieee80211_rateset *rs;
26575591b213SSam Leffler 	int i, maxrates;
26585591b213SSam Leffler 
26595591b213SSam Leffler 	switch (mode) {
26605591b213SSam Leffler 	case IEEE80211_MODE_11A:
26615591b213SSam Leffler 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11A);
26625591b213SSam Leffler 		break;
26635591b213SSam Leffler 	case IEEE80211_MODE_11B:
26645591b213SSam Leffler 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11B);
26655591b213SSam Leffler 		break;
26665591b213SSam Leffler 	case IEEE80211_MODE_11G:
26675591b213SSam Leffler 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11G);
26685591b213SSam Leffler 		break;
26695591b213SSam Leffler 	case IEEE80211_MODE_TURBO:
26705591b213SSam Leffler 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_TURBO);
26715591b213SSam Leffler 		break;
26725591b213SSam Leffler 	default:
2673e325e530SSam Leffler 		DPRINTF(ATH_DEBUG_ANY,
2674e325e530SSam Leffler 			("%s: invalid mode %u\n", __func__, mode));
26755591b213SSam Leffler 		return 0;
26765591b213SSam Leffler 	}
26775591b213SSam Leffler 	rt = sc->sc_rates[mode];
26785591b213SSam Leffler 	if (rt == NULL)
26795591b213SSam Leffler 		return 0;
26805591b213SSam Leffler 	if (rt->rateCount > IEEE80211_RATE_MAXSIZE) {
2681e325e530SSam Leffler 		DPRINTF(ATH_DEBUG_ANY,
2682e325e530SSam Leffler 			("%s: rate table too small (%u > %u)\n",
26835591b213SSam Leffler 			__func__, rt->rateCount, IEEE80211_RATE_MAXSIZE));
26845591b213SSam Leffler 		maxrates = IEEE80211_RATE_MAXSIZE;
26855591b213SSam Leffler 	} else
26865591b213SSam Leffler 		maxrates = rt->rateCount;
26875591b213SSam Leffler 	rs = &ic->ic_sup_rates[mode];
26885591b213SSam Leffler 	for (i = 0; i < maxrates; i++)
26895591b213SSam Leffler 		rs->rs_rates[i] = rt->info[i].dot11Rate;
26905591b213SSam Leffler 	rs->rs_nrates = maxrates;
26915591b213SSam Leffler 	return 1;
26925591b213SSam Leffler }
26935591b213SSam Leffler 
26945591b213SSam Leffler static void
26955591b213SSam Leffler ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode)
26965591b213SSam Leffler {
26975591b213SSam Leffler 	const HAL_RATE_TABLE *rt;
26985591b213SSam Leffler 	int i;
26995591b213SSam Leffler 
27005591b213SSam Leffler 	memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap));
27015591b213SSam Leffler 	rt = sc->sc_rates[mode];
27025591b213SSam Leffler 	KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode));
27035591b213SSam Leffler 	for (i = 0; i < rt->rateCount; i++)
27045591b213SSam Leffler 		sc->sc_rixmap[rt->info[i].dot11Rate & IEEE80211_RATE_VAL] = i;
27051b1a8e41SSam Leffler 	memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap));
27061b1a8e41SSam Leffler 	for (i = 0; i < 32; i++)
27071b1a8e41SSam Leffler 		sc->sc_hwmap[i] = rt->info[rt->rateCodeToIndex[i]].dot11Rate;
27085591b213SSam Leffler 	sc->sc_currates = rt;
27095591b213SSam Leffler 	sc->sc_curmode = mode;
27105591b213SSam Leffler }
27115591b213SSam Leffler 
27125591b213SSam Leffler /*
27135591b213SSam Leffler  * Reset the rate control state for each 802.11 state transition.
27145591b213SSam Leffler  */
27155591b213SSam Leffler static void
27165591b213SSam Leffler ath_rate_ctl_reset(struct ath_softc *sc, enum ieee80211_state state)
27175591b213SSam Leffler {
27185591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
27195591b213SSam Leffler 	struct ieee80211_node *ni;
27205591b213SSam Leffler 	struct ath_node *an;
27215591b213SSam Leffler 
2722310e4a4aSSam Leffler 	if (ic->ic_opmode != IEEE80211_M_STA) {
2723310e4a4aSSam Leffler 		/*
2724310e4a4aSSam Leffler 		 * When operating as a station the node table holds
2725310e4a4aSSam Leffler 		 * the AP's that were discovered during scanning.
2726310e4a4aSSam Leffler 		 * For any other operating mode we want to reset the
2727310e4a4aSSam Leffler 		 * tx rate state of each node.
2728310e4a4aSSam Leffler 		 */
2729310e4a4aSSam Leffler 		TAILQ_FOREACH(ni, &ic->ic_node, ni_list) {
2730310e4a4aSSam Leffler 			ni->ni_txrate = 0;		/* use lowest rate */
2731310e4a4aSSam Leffler 			an = (struct ath_node *) ni;
2732310e4a4aSSam Leffler 			an->an_tx_ok = an->an_tx_err = an->an_tx_retr =
2733310e4a4aSSam Leffler 			    an->an_tx_upper = 0;
2734310e4a4aSSam Leffler 		}
2735310e4a4aSSam Leffler 	}
2736310e4a4aSSam Leffler 	/*
2737310e4a4aSSam Leffler 	 * Reset local xmit state; this is really only meaningful
2738310e4a4aSSam Leffler 	 * when operating in station or adhoc mode.
2739310e4a4aSSam Leffler 	 */
27405591b213SSam Leffler 	ni = ic->ic_bss;
2741310e4a4aSSam Leffler 	an = (struct ath_node *) ni;
2742310e4a4aSSam Leffler 	an->an_tx_ok = an->an_tx_err = an->an_tx_retr = an->an_tx_upper = 0;
27435591b213SSam Leffler 	if (state == IEEE80211_S_RUN) {
27445591b213SSam Leffler 		/* start with highest negotiated rate */
27455591b213SSam Leffler 		KASSERT(ni->ni_rates.rs_nrates > 0,
27465591b213SSam Leffler 			("transition to RUN state w/ no rates!"));
27475591b213SSam Leffler 		ni->ni_txrate = ni->ni_rates.rs_nrates - 1;
27485591b213SSam Leffler 	} else {
27495591b213SSam Leffler 		/* use lowest rate */
27505591b213SSam Leffler 		ni->ni_txrate = 0;
27515591b213SSam Leffler 	}
27525591b213SSam Leffler }
27535591b213SSam Leffler 
27545591b213SSam Leffler /*
27555591b213SSam Leffler  * Examine and potentially adjust the transmit rate.
27565591b213SSam Leffler  */
27575591b213SSam Leffler static void
27585591b213SSam Leffler ath_rate_ctl(void *arg, struct ieee80211_node *ni)
27595591b213SSam Leffler {
27605591b213SSam Leffler 	struct ath_softc *sc = arg;
27615591b213SSam Leffler 	struct ath_node *an = (struct ath_node *) ni;
27625591b213SSam Leffler 	struct ieee80211_rateset *rs = &ni->ni_rates;
27635591b213SSam Leffler 	int mod = 0, orate, enough;
27645591b213SSam Leffler 
27655591b213SSam Leffler 	/*
27665591b213SSam Leffler 	 * Rate control
27675591b213SSam Leffler 	 * XXX: very primitive version.
27685591b213SSam Leffler 	 */
27695591b213SSam Leffler 	sc->sc_stats.ast_rate_calls++;
27705591b213SSam Leffler 
27715591b213SSam Leffler 	enough = (an->an_tx_ok + an->an_tx_err >= 10);
27725591b213SSam Leffler 
27735591b213SSam Leffler 	/* no packet reached -> down */
27745591b213SSam Leffler 	if (an->an_tx_err > 0 && an->an_tx_ok == 0)
27755591b213SSam Leffler 		mod = -1;
27765591b213SSam Leffler 
27775591b213SSam Leffler 	/* all packets needs retry in average -> down */
27785591b213SSam Leffler 	if (enough && an->an_tx_ok < an->an_tx_retr)
27795591b213SSam Leffler 		mod = -1;
27805591b213SSam Leffler 
27815591b213SSam Leffler 	/* no error and less than 10% of packets needs retry -> up */
27825591b213SSam Leffler 	if (enough && an->an_tx_err == 0 && an->an_tx_ok > an->an_tx_retr * 10)
27835591b213SSam Leffler 		mod = 1;
27845591b213SSam Leffler 
27855591b213SSam Leffler 	orate = ni->ni_txrate;
27865591b213SSam Leffler 	switch (mod) {
27875591b213SSam Leffler 	case 0:
27885591b213SSam Leffler 		if (enough && an->an_tx_upper > 0)
27895591b213SSam Leffler 			an->an_tx_upper--;
27905591b213SSam Leffler 		break;
27915591b213SSam Leffler 	case -1:
27925591b213SSam Leffler 		if (ni->ni_txrate > 0) {
27935591b213SSam Leffler 			ni->ni_txrate--;
27945591b213SSam Leffler 			sc->sc_stats.ast_rate_drop++;
27955591b213SSam Leffler 		}
27965591b213SSam Leffler 		an->an_tx_upper = 0;
27975591b213SSam Leffler 		break;
27985591b213SSam Leffler 	case 1:
27995591b213SSam Leffler 		if (++an->an_tx_upper < 2)
28005591b213SSam Leffler 			break;
28015591b213SSam Leffler 		an->an_tx_upper = 0;
28025591b213SSam Leffler 		if (ni->ni_txrate + 1 < rs->rs_nrates) {
28035591b213SSam Leffler 			ni->ni_txrate++;
28045591b213SSam Leffler 			sc->sc_stats.ast_rate_raise++;
28055591b213SSam Leffler 		}
28065591b213SSam Leffler 		break;
28075591b213SSam Leffler 	}
28085591b213SSam Leffler 
28095591b213SSam Leffler 	if (ni->ni_txrate != orate) {
2810e325e530SSam Leffler 		DPRINTF(ATH_DEBUG_RATE,
2811e325e530SSam Leffler 		    ("%s: %dM -> %dM (%d ok, %d err, %d retr)\n",
28125591b213SSam Leffler 		    __func__,
28135591b213SSam Leffler 		    (rs->rs_rates[orate] & IEEE80211_RATE_VAL) / 2,
28145591b213SSam Leffler 		    (rs->rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL) / 2,
281568025aebSSam Leffler 		    an->an_tx_ok, an->an_tx_err, an->an_tx_retr));
28165591b213SSam Leffler 	}
28175591b213SSam Leffler 	if (ni->ni_txrate != orate || enough)
28185591b213SSam Leffler 		an->an_tx_ok = an->an_tx_err = an->an_tx_retr = 0;
28195591b213SSam Leffler }
28205591b213SSam Leffler 
28215591b213SSam Leffler #ifdef AR_DEBUG
28225591b213SSam Leffler static int
28235591b213SSam Leffler sysctl_hw_ath_dump(SYSCTL_HANDLER_ARGS)
28245591b213SSam Leffler {
28255591b213SSam Leffler 	char dmode[64];
28265591b213SSam Leffler 	int error;
28275591b213SSam Leffler 
28285591b213SSam Leffler 	strncpy(dmode, "", sizeof(dmode) - 1);
28295591b213SSam Leffler 	dmode[sizeof(dmode) - 1] = '\0';
28305591b213SSam Leffler 	error = sysctl_handle_string(oidp, &dmode[0], sizeof(dmode), req);
28315591b213SSam Leffler 
28325591b213SSam Leffler 	if (error == 0 && req->newptr != NULL) {
28335591b213SSam Leffler 		struct ifnet *ifp;
28345591b213SSam Leffler 		struct ath_softc *sc;
28355591b213SSam Leffler 
28365591b213SSam Leffler 		ifp = ifunit("ath0");		/* XXX */
28375591b213SSam Leffler 		if (!ifp)
28385591b213SSam Leffler 			return EINVAL;
28395591b213SSam Leffler 		sc = ifp->if_softc;
28405591b213SSam Leffler 		if (strcmp(dmode, "hal") == 0)
28415591b213SSam Leffler 			ath_hal_dumpstate(sc->sc_ah);
28425591b213SSam Leffler 		else
28435591b213SSam Leffler 			return EINVAL;
28445591b213SSam Leffler 	}
28455591b213SSam Leffler 	return error;
28465591b213SSam Leffler }
28475591b213SSam Leffler SYSCTL_PROC(_hw_ath, OID_AUTO, dump, CTLTYPE_STRING | CTLFLAG_RW,
28485591b213SSam Leffler 	0, 0, sysctl_hw_ath_dump, "A", "Dump driver state");
28495591b213SSam Leffler 
28505591b213SSam Leffler static void
28515591b213SSam Leffler ath_printrxbuf(struct ath_buf *bf, int done)
28525591b213SSam Leffler {
28535591b213SSam Leffler 	struct ath_desc *ds;
28545591b213SSam Leffler 	int i;
28555591b213SSam Leffler 
28565591b213SSam Leffler 	for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) {
28575591b213SSam Leffler 		printf("R%d (%p %p) %08x %08x %08x %08x %08x %08x %c\n",
28585591b213SSam Leffler 		    i, ds, (struct ath_desc *)bf->bf_daddr + i,
28595591b213SSam Leffler 		    ds->ds_link, ds->ds_data,
28605591b213SSam Leffler 		    ds->ds_ctl0, ds->ds_ctl1,
28615591b213SSam Leffler 		    ds->ds_hw[0], ds->ds_hw[1],
28625591b213SSam Leffler 		    !done ? ' ' : (ds->ds_rxstat.rs_status == 0) ? '*' : '!');
28635591b213SSam Leffler 	}
28645591b213SSam Leffler }
28655591b213SSam Leffler 
28665591b213SSam Leffler static void
28675591b213SSam Leffler ath_printtxbuf(struct ath_buf *bf, int done)
28685591b213SSam Leffler {
28695591b213SSam Leffler 	struct ath_desc *ds;
28705591b213SSam Leffler 	int i;
28715591b213SSam Leffler 
28725591b213SSam Leffler 	for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) {
28735591b213SSam Leffler 		printf("T%d (%p %p) %08x %08x %08x %08x %08x %08x %08x %08x %c\n",
28745591b213SSam Leffler 		    i, ds, (struct ath_desc *)bf->bf_daddr + i,
28755591b213SSam Leffler 		    ds->ds_link, ds->ds_data,
28765591b213SSam Leffler 		    ds->ds_ctl0, ds->ds_ctl1,
28775591b213SSam Leffler 		    ds->ds_hw[0], ds->ds_hw[1], ds->ds_hw[2], ds->ds_hw[3],
28785591b213SSam Leffler 		    !done ? ' ' : (ds->ds_txstat.ts_status == 0) ? '*' : '!');
28795591b213SSam Leffler 	}
28805591b213SSam Leffler }
28815591b213SSam Leffler #endif /* AR_DEBUG */
2882