xref: /freebsd/sys/dev/ath/if_ath.c (revision 9cccabebd778a60157b313f05f2029c900b971d4)
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 *);
1175591b213SSam Leffler static int	ath_rxbuf_init(struct ath_softc *, struct ath_buf *);
1185591b213SSam Leffler static void	ath_rx_proc(void *, int);
1195591b213SSam Leffler static int	ath_tx_start(struct ath_softc *, struct ieee80211_node *,
1205591b213SSam Leffler 			     struct ath_buf *, struct mbuf *);
1215591b213SSam Leffler static void	ath_tx_proc(void *, int);
1225591b213SSam Leffler static int	ath_chan_set(struct ath_softc *, struct ieee80211_channel *);
1235591b213SSam Leffler static void	ath_draintxq(struct ath_softc *);
1245591b213SSam Leffler static void	ath_stoprecv(struct ath_softc *);
1255591b213SSam Leffler static int	ath_startrecv(struct ath_softc *);
1265591b213SSam Leffler static void	ath_next_scan(void *);
1275591b213SSam Leffler static void	ath_calibrate(void *);
12845bbf62fSSam Leffler static int	ath_newstate(struct ieee80211com *, enum ieee80211_state, int);
1295591b213SSam Leffler static void	ath_newassoc(struct ieee80211com *,
1305591b213SSam Leffler 			struct ieee80211_node *, int);
1315591b213SSam Leffler static int	ath_getchannels(struct ath_softc *, u_int cc, HAL_BOOL outdoor);
1325591b213SSam Leffler 
1335591b213SSam Leffler static int	ath_rate_setup(struct ath_softc *sc, u_int mode);
1345591b213SSam Leffler static void	ath_setcurmode(struct ath_softc *, enum ieee80211_phymode);
1355591b213SSam Leffler static void	ath_rate_ctl_reset(struct ath_softc *, enum ieee80211_state);
1365591b213SSam Leffler static void	ath_rate_ctl(void *, struct ieee80211_node *);
1375591b213SSam Leffler 
1385591b213SSam Leffler SYSCTL_DECL(_hw_ath);
1395591b213SSam Leffler 
1405591b213SSam Leffler /* XXX validate sysctl values */
1415591b213SSam Leffler static	int ath_dwelltime = 200;		/* 5 channels/second */
1425591b213SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, dwell, CTLFLAG_RW, &ath_dwelltime,
1435591b213SSam Leffler 	    0, "channel dwell time (ms) for AP/station scanning");
1445591b213SSam Leffler static	int ath_calinterval = 30;		/* calibrate every 30 secs */
1455591b213SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, calibrate, CTLFLAG_RW, &ath_calinterval,
1465591b213SSam Leffler 	    0, "chip calibration interval (secs)");
14745cabbdcSSam Leffler static	int ath_outdoor = AH_TRUE;		/* outdoor operation */
14845cabbdcSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, outdoor, CTLFLAG_RD, &ath_outdoor,
14945cabbdcSSam Leffler 	    0, "enable/disable outdoor operation");
15045cabbdcSSam Leffler static	int ath_countrycode = CTRY_DEFAULT;	/* country code */
15145cabbdcSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, countrycode, CTLFLAG_RD, &ath_countrycode,
15245cabbdcSSam Leffler 	    0, "country code");
15345cabbdcSSam Leffler static	int ath_regdomain = 0;			/* regulatory domain */
15445cabbdcSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, regdomain, CTLFLAG_RD, &ath_regdomain,
15545cabbdcSSam Leffler 	    0, "regulatory domain");
1565591b213SSam Leffler 
1575591b213SSam Leffler #ifdef AR_DEBUG
1585591b213SSam Leffler int	ath_debug = 0;
1595591b213SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, debug, CTLFLAG_RW, &ath_debug,
1605591b213SSam Leffler 	    0, "control debugging printfs");
1615591b213SSam Leffler #define	IFF_DUMPPKTS(_ifp) \
1625591b213SSam Leffler 	(ath_debug || \
1635591b213SSam Leffler 	    ((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
1645591b213SSam Leffler static	void ath_printrxbuf(struct ath_buf *bf, int);
1655591b213SSam Leffler static	void ath_printtxbuf(struct ath_buf *bf, int);
1665591b213SSam Leffler #define	DPRINTF(X)	if (ath_debug) printf X
1675591b213SSam Leffler #define	DPRINTF2(X)	if (ath_debug > 1) printf X
1685591b213SSam Leffler #else
1695591b213SSam Leffler #define	IFF_DUMPPKTS(_ifp) \
1705591b213SSam Leffler 	(((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
1715591b213SSam Leffler #define	DPRINTF(X)
1725591b213SSam Leffler #define	DPRINTF2(X)
1735591b213SSam Leffler #endif
1745591b213SSam Leffler 
1755591b213SSam Leffler int
1765591b213SSam Leffler ath_attach(u_int16_t devid, struct ath_softc *sc)
1775591b213SSam Leffler {
1785591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
1795591b213SSam Leffler 	struct ifnet *ifp = &ic->ic_if;
1805591b213SSam Leffler 	struct ath_hal *ah;
1815591b213SSam Leffler 	HAL_STATUS status;
1825591b213SSam Leffler 	int error = 0;
1835591b213SSam Leffler 
1845591b213SSam Leffler 	DPRINTF(("ath_attach: devid 0x%x\n", devid));
1855591b213SSam Leffler 
1865591b213SSam Leffler 	/* set these up early for if_printf use */
1875591b213SSam Leffler 	ifp->if_unit = device_get_unit(sc->sc_dev);
1885591b213SSam Leffler 	ifp->if_name = "ath";
1895591b213SSam Leffler 
1905591b213SSam Leffler 	ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh, &status);
1915591b213SSam Leffler 	if (ah == NULL) {
1925591b213SSam Leffler 		if_printf(ifp, "unable to attach hardware; HAL status %u\n",
1935591b213SSam Leffler 			status);
1945591b213SSam Leffler 		error = ENXIO;
1955591b213SSam Leffler 		goto bad;
1965591b213SSam Leffler 	}
1975591b213SSam Leffler 	sc->sc_ah = ah;
198b58b3803SSam Leffler 	sc->sc_invalid = 0;	/* ready to go, enable interrupt handling */
1995591b213SSam Leffler 
2005591b213SSam Leffler 	/*
2015591b213SSam Leffler 	 * Collect the channel list using the default country
2025591b213SSam Leffler 	 * code and including outdoor channels.  The 802.11 layer
20345cabbdcSSam Leffler 	 * is resposible for filtering this list based on settings
20445cabbdcSSam Leffler 	 * like the phy mode.
2055591b213SSam Leffler 	 */
20645cabbdcSSam Leffler 	error = ath_getchannels(sc, ath_countrycode, ath_outdoor);
2075591b213SSam Leffler 	if (error != 0)
2085591b213SSam Leffler 		goto bad;
20945cabbdcSSam Leffler 	/*
21045cabbdcSSam Leffler 	 * Copy these back; they are set as a side effect
21145cabbdcSSam Leffler 	 * of constructing the channel list.
21245cabbdcSSam Leffler 	 */
21345cabbdcSSam Leffler 	ath_regdomain = ath_hal_getregdomain(ah);
21445cabbdcSSam Leffler 	ath_countrycode = ath_hal_getcountrycode(ah);
2155591b213SSam Leffler 
2165591b213SSam Leffler 	/*
2175591b213SSam Leffler 	 * Setup rate tables for all potential media types.
2185591b213SSam Leffler 	 */
2195591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11A);
2205591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11B);
2215591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11G);
2225591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO);
2235591b213SSam Leffler 
2245591b213SSam Leffler 	error = ath_desc_alloc(sc);
2255591b213SSam Leffler 	if (error != 0) {
2265591b213SSam Leffler 		if_printf(ifp, "failed to allocate descriptors: %d\n", error);
2275591b213SSam Leffler 		goto bad;
2285591b213SSam Leffler 	}
2292274d8c8SSam Leffler 	callout_init(&sc->sc_scan_ch, CALLOUT_MPSAFE);
2302274d8c8SSam Leffler 	callout_init(&sc->sc_cal_ch, CALLOUT_MPSAFE);
2315591b213SSam Leffler 
2325591b213SSam Leffler 	mtx_init(&sc->sc_txbuflock,
2335591b213SSam Leffler 		device_get_nameunit(sc->sc_dev), "xmit buf q", MTX_DEF);
2345591b213SSam Leffler 	mtx_init(&sc->sc_txqlock,
2355591b213SSam Leffler 		device_get_nameunit(sc->sc_dev), "xmit q", MTX_DEF);
2365591b213SSam Leffler 
2375591b213SSam Leffler 	TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc, sc);
2385591b213SSam Leffler 	TASK_INIT(&sc->sc_rxtask, 0, ath_rx_proc, sc);
2395591b213SSam Leffler 	TASK_INIT(&sc->sc_swbatask, 0, ath_beacon_proc, sc);
2405591b213SSam Leffler 	TASK_INIT(&sc->sc_rxorntask, 0, ath_rxorn_proc, sc);
2415591b213SSam Leffler 	TASK_INIT(&sc->sc_fataltask, 0, ath_fatal_proc, sc);
2425591b213SSam Leffler 	TASK_INIT(&sc->sc_bmisstask, 0, ath_bmiss_proc, sc);
2435591b213SSam Leffler 
2445591b213SSam Leffler 	/*
2455591b213SSam Leffler 	 * For now just pre-allocate one data queue and one
2465591b213SSam Leffler 	 * beacon queue.  Note that the HAL handles resetting
2475591b213SSam Leffler 	 * them at the needed time.  Eventually we'll want to
2485591b213SSam Leffler 	 * allocate more tx queues for splitting management
2495591b213SSam Leffler 	 * frames and for QOS support.
2505591b213SSam Leffler 	 */
2515591b213SSam Leffler 	sc->sc_txhalq = ath_hal_setuptxqueue(ah,
2525591b213SSam Leffler 		HAL_TX_QUEUE_DATA,
2535591b213SSam Leffler 		AH_TRUE			/* enable interrupts */
2545591b213SSam Leffler 	);
2555591b213SSam Leffler 	if (sc->sc_txhalq == (u_int) -1) {
2565591b213SSam Leffler 		if_printf(ifp, "unable to setup a data xmit queue!\n");
2575591b213SSam Leffler 		goto bad;
2585591b213SSam Leffler 	}
2595591b213SSam Leffler 	sc->sc_bhalq = ath_hal_setuptxqueue(ah,
2605591b213SSam Leffler 		HAL_TX_QUEUE_BEACON,
2615591b213SSam Leffler 		AH_TRUE			/* enable interrupts */
2625591b213SSam Leffler 	);
2635591b213SSam Leffler 	if (sc->sc_bhalq == (u_int) -1) {
2645591b213SSam Leffler 		if_printf(ifp, "unable to setup a beacon xmit queue!\n");
2655591b213SSam Leffler 		goto bad;
2665591b213SSam Leffler 	}
2675591b213SSam Leffler 
2685591b213SSam Leffler 	ifp->if_softc = sc;
2695591b213SSam Leffler 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
2705591b213SSam Leffler 	ifp->if_start = ath_start;
2715591b213SSam Leffler 	ifp->if_watchdog = ath_watchdog;
2725591b213SSam Leffler 	ifp->if_ioctl = ath_ioctl;
2735591b213SSam Leffler 	ifp->if_init = ath_init;
2745591b213SSam Leffler 	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
2755591b213SSam Leffler 
2765591b213SSam Leffler 	ic->ic_softc = sc;
2775591b213SSam Leffler 	ic->ic_newassoc = ath_newassoc;
2785591b213SSam Leffler 	/* XXX not right but it's not used anywhere important */
2795591b213SSam Leffler 	ic->ic_phytype = IEEE80211_T_OFDM;
2805591b213SSam Leffler 	ic->ic_opmode = IEEE80211_M_STA;
2816b59f5e3SSam Leffler 	ic->ic_caps = IEEE80211_C_WEP | IEEE80211_C_IBSS | IEEE80211_C_HOSTAP
2826b59f5e3SSam Leffler 		| IEEE80211_C_MONITOR;
2835591b213SSam Leffler 	/* NB: 11g support is identified when we fetch the channel set */
2845591b213SSam Leffler 	if (sc->sc_have11g)
2855591b213SSam Leffler 		ic->ic_caps |= IEEE80211_C_SHPREAMBLE;
2865591b213SSam Leffler 
2875591b213SSam Leffler 	/* get mac address from hardware */
2885591b213SSam Leffler 	ath_hal_getmac(ah, ic->ic_myaddr);
2895591b213SSam Leffler 
2905591b213SSam Leffler 	/* call MI attach routine. */
2915591b213SSam Leffler 	ieee80211_ifattach(ifp);
2925591b213SSam Leffler 	/* override default methods */
2935591b213SSam Leffler 	ic->ic_node_alloc = ath_node_alloc;
2945591b213SSam Leffler 	ic->ic_node_free = ath_node_free;
2955591b213SSam Leffler 	ic->ic_node_copy = ath_node_copy;
29645bbf62fSSam Leffler 	sc->sc_newstate = ic->ic_newstate;
29745bbf62fSSam Leffler 	ic->ic_newstate = ath_newstate;
29845bbf62fSSam Leffler 	/* complete initialization */
2995591b213SSam Leffler 	ieee80211_media_init(ifp, ath_media_change, ieee80211_media_status);
3005591b213SSam Leffler 
3015591b213SSam Leffler 	if_printf(ifp, "802.11 address: %s\n", ether_sprintf(ic->ic_myaddr));
3025591b213SSam Leffler 
3035591b213SSam Leffler 	return 0;
3045591b213SSam Leffler bad:
3055591b213SSam Leffler 	if (ah)
3065591b213SSam Leffler 		ath_hal_detach(ah);
3075591b213SSam Leffler 	sc->sc_invalid = 1;
3085591b213SSam Leffler 	return error;
3095591b213SSam Leffler }
3105591b213SSam Leffler 
3115591b213SSam Leffler int
3125591b213SSam Leffler ath_detach(struct ath_softc *sc)
3135591b213SSam Leffler {
3145591b213SSam Leffler 	struct ifnet *ifp = &sc->sc_ic.ic_if;
3155591b213SSam Leffler 
3165591b213SSam Leffler 	DPRINTF(("ath_detach: if_flags %x\n", ifp->if_flags));
3175591b213SSam Leffler 
3185591b213SSam Leffler 	mtx_lock(&sc->sc_mtx);
3195591b213SSam Leffler 	ath_stop(ifp);
3205591b213SSam Leffler 	ath_desc_free(sc);
3215591b213SSam Leffler 	ath_hal_detach(sc->sc_ah);
3225591b213SSam Leffler 	ieee80211_ifdetach(ifp);
3235591b213SSam Leffler 	mtx_unlock(&sc->sc_mtx);
3245591b213SSam Leffler 	return 0;
3255591b213SSam Leffler }
3265591b213SSam Leffler 
3275591b213SSam Leffler void
3285591b213SSam Leffler ath_suspend(struct ath_softc *sc)
3295591b213SSam Leffler {
3305591b213SSam Leffler 	struct ifnet *ifp = &sc->sc_ic.ic_if;
3315591b213SSam Leffler 
3325591b213SSam Leffler 	DPRINTF(("ath_suspend: if_flags %x\n", ifp->if_flags));
3335591b213SSam Leffler 
3345591b213SSam Leffler 	ath_stop(ifp);
3355591b213SSam Leffler }
3365591b213SSam Leffler 
3375591b213SSam Leffler void
3385591b213SSam Leffler ath_resume(struct ath_softc *sc)
3395591b213SSam Leffler {
3405591b213SSam Leffler 	struct ifnet *ifp = &sc->sc_ic.ic_if;
3415591b213SSam Leffler 
3425591b213SSam Leffler 	DPRINTF(("ath_resume: if_flags %x\n", ifp->if_flags));
3435591b213SSam Leffler 
3446b59f5e3SSam Leffler 	if (ifp->if_flags & IFF_UP) {
3455591b213SSam Leffler 		ath_init(ifp);
3466b59f5e3SSam Leffler 		if (ifp->if_flags & IFF_RUNNING)
3475591b213SSam Leffler 			ath_start(ifp);
3485591b213SSam Leffler 	}
3496b59f5e3SSam Leffler }
3505591b213SSam Leffler 
3515591b213SSam Leffler void
3525591b213SSam Leffler ath_shutdown(struct ath_softc *sc)
3535591b213SSam Leffler {
3545591b213SSam Leffler 	struct ifnet *ifp = &sc->sc_ic.ic_if;
3555591b213SSam Leffler 
3565591b213SSam Leffler 	DPRINTF(("ath_shutdown: if_flags %x\n", ifp->if_flags));
3575591b213SSam Leffler 
3585591b213SSam Leffler 	ath_stop(ifp);
3595591b213SSam Leffler }
3605591b213SSam Leffler 
3615591b213SSam Leffler void
3625591b213SSam Leffler ath_intr(void *arg)
3635591b213SSam Leffler {
3645591b213SSam Leffler 	struct ath_softc *sc = arg;
3655591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
3665591b213SSam Leffler 	struct ifnet *ifp = &ic->ic_if;
3675591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3685591b213SSam Leffler 	HAL_INT status;
3695591b213SSam Leffler 
3705591b213SSam Leffler 	if (sc->sc_invalid) {
3715591b213SSam Leffler 		/*
372b58b3803SSam Leffler 		 * The hardware is not ready/present, don't touch anything.
373b58b3803SSam Leffler 		 * Note this can happen early on if the IRQ is shared.
3745591b213SSam Leffler 		 */
3755591b213SSam Leffler 		DPRINTF(("ath_intr: invalid; ignored\n"));
3765591b213SSam Leffler 		return;
3775591b213SSam Leffler 	}
3785591b213SSam Leffler 	if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) != (IFF_RUNNING|IFF_UP)) {
3795591b213SSam Leffler 		DPRINTF(("ath_intr: if_flags 0x%x\n", ifp->if_flags));
3805591b213SSam Leffler 		ath_hal_getisr(ah, &status);	/* clear ISR */
3815591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable further intr's */
3825591b213SSam Leffler 		return;
3835591b213SSam Leffler 	}
3845591b213SSam Leffler 	ath_hal_getisr(ah, &status);		/* NB: clears ISR too */
3855591b213SSam Leffler 	DPRINTF2(("ath_intr: status 0x%x\n", status));
3865591b213SSam Leffler #ifdef AR_DEBUG
3875591b213SSam Leffler 	if (ath_debug &&
3885591b213SSam Leffler 	    (status & (HAL_INT_FATAL|HAL_INT_RXORN|HAL_INT_BMISS))) {
3895591b213SSam Leffler 		if_printf(ifp, "ath_intr: status 0x%x\n", status);
3905591b213SSam Leffler 		ath_hal_dumpstate(ah);
3915591b213SSam Leffler 	}
3925591b213SSam Leffler #endif /* AR_DEBUG */
3935591b213SSam Leffler 	if (status & HAL_INT_FATAL) {
3945591b213SSam Leffler 		sc->sc_stats.ast_hardware++;
3955591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
3965591b213SSam Leffler 		taskqueue_enqueue(taskqueue_swi, &sc->sc_fataltask);
3975591b213SSam Leffler 	} else if (status & HAL_INT_RXORN) {
3985591b213SSam Leffler 		sc->sc_stats.ast_rxorn++;
3995591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
4005591b213SSam Leffler 		taskqueue_enqueue(taskqueue_swi, &sc->sc_rxorntask);
4015591b213SSam Leffler 	} else {
4025591b213SSam Leffler 		if (status & HAL_INT_RXEOL) {
4035591b213SSam Leffler 			/*
4045591b213SSam Leffler 			 * NB: the hardware should re-read the link when
4055591b213SSam Leffler 			 *     RXE bit is written, but it doesn't work at
4065591b213SSam Leffler 			 *     least on older hardware revs.
4075591b213SSam Leffler 			 */
4085591b213SSam Leffler 			sc->sc_stats.ast_rxeol++;
4095591b213SSam Leffler 			sc->sc_rxlink = NULL;
4105591b213SSam Leffler 		}
4115591b213SSam Leffler 		if (status & HAL_INT_TXURN) {
4125591b213SSam Leffler 			sc->sc_stats.ast_txurn++;
4135591b213SSam Leffler 			/* bump tx trigger level */
4145591b213SSam Leffler 			ath_hal_updatetxtriglevel(ah, AH_TRUE);
4155591b213SSam Leffler 		}
4165591b213SSam Leffler 		if (status & HAL_INT_RX)
4175591b213SSam Leffler 			taskqueue_enqueue(taskqueue_swi, &sc->sc_rxtask);
4185591b213SSam Leffler 		if (status & HAL_INT_TX)
4195591b213SSam Leffler 			taskqueue_enqueue(taskqueue_swi, &sc->sc_txtask);
4205591b213SSam Leffler 		if (status & HAL_INT_SWBA)
4215591b213SSam Leffler 			taskqueue_enqueue(taskqueue_swi, &sc->sc_swbatask);
4225591b213SSam Leffler 		if (status & HAL_INT_BMISS) {
4235591b213SSam Leffler 			sc->sc_stats.ast_bmiss++;
4245591b213SSam Leffler 			taskqueue_enqueue(taskqueue_swi, &sc->sc_bmisstask);
4255591b213SSam Leffler 		}
4265591b213SSam Leffler 	}
4275591b213SSam Leffler }
4285591b213SSam Leffler 
4295591b213SSam Leffler static void
4305591b213SSam Leffler ath_fatal_proc(void *arg, int pending)
4315591b213SSam Leffler {
4325591b213SSam Leffler 	struct ath_softc *sc = arg;
4335591b213SSam Leffler 
4345591b213SSam Leffler 	device_printf(sc->sc_dev, "hardware error; resetting\n");
4355591b213SSam Leffler 	ath_reset(sc);
4365591b213SSam Leffler }
4375591b213SSam Leffler 
4385591b213SSam Leffler static void
4395591b213SSam Leffler ath_rxorn_proc(void *arg, int pending)
4405591b213SSam Leffler {
4415591b213SSam Leffler 	struct ath_softc *sc = arg;
4425591b213SSam Leffler 
4435591b213SSam Leffler 	device_printf(sc->sc_dev, "rx FIFO overrun; resetting\n");
4445591b213SSam Leffler 	ath_reset(sc);
4455591b213SSam Leffler }
4465591b213SSam Leffler 
4475591b213SSam Leffler static void
4485591b213SSam Leffler ath_bmiss_proc(void *arg, int pending)
4495591b213SSam Leffler {
4505591b213SSam Leffler 	struct ath_softc *sc = arg;
4515591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
4525591b213SSam Leffler 
4535591b213SSam Leffler 	DPRINTF(("ath_bmiss_proc: pending %u\n", pending));
4545591b213SSam Leffler 	KASSERT(ic->ic_opmode == IEEE80211_M_STA,
4555591b213SSam Leffler 		("unexpect operating mode %u", ic->ic_opmode));
4565591b213SSam Leffler 	if (ic->ic_state == IEEE80211_S_RUN)
45745bbf62fSSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
4585591b213SSam Leffler }
4595591b213SSam Leffler 
4605591b213SSam Leffler static u_int
4615591b213SSam Leffler ath_chan2flags(struct ieee80211com *ic, struct ieee80211_channel *chan)
4625591b213SSam Leffler {
4635591b213SSam Leffler 	static const u_int modeflags[] = {
4645591b213SSam Leffler 		0,			/* IEEE80211_MODE_AUTO */
4655591b213SSam Leffler 		CHANNEL_A,		/* IEEE80211_MODE_11A */
4665591b213SSam Leffler 		CHANNEL_B,		/* IEEE80211_MODE_11B */
4675591b213SSam Leffler 		CHANNEL_PUREG,		/* IEEE80211_MODE_11G */
4685591b213SSam Leffler 		CHANNEL_T		/* IEEE80211_MODE_TURBO */
4695591b213SSam Leffler 	};
4705591b213SSam Leffler 	return modeflags[ieee80211_chan2mode(ic, chan)];
4715591b213SSam Leffler }
4725591b213SSam Leffler 
4735591b213SSam Leffler static void
4745591b213SSam Leffler ath_init(void *arg)
4755591b213SSam Leffler {
4765591b213SSam Leffler 	struct ath_softc *sc = (struct ath_softc *) arg;
4775591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
4785591b213SSam Leffler 	struct ifnet *ifp = &ic->ic_if;
4795591b213SSam Leffler 	struct ieee80211_node *ni;
4805591b213SSam Leffler 	enum ieee80211_phymode mode;
4815591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4825591b213SSam Leffler 	HAL_STATUS status;
4835591b213SSam Leffler 	HAL_CHANNEL hchan;
4845591b213SSam Leffler 
4855591b213SSam Leffler 	DPRINTF(("ath_init: if_flags 0x%x\n", ifp->if_flags));
4865591b213SSam Leffler 
4875591b213SSam Leffler 	mtx_lock(&sc->sc_mtx);
4885591b213SSam Leffler 	/*
4895591b213SSam Leffler 	 * Stop anything previously setup.  This is safe
4905591b213SSam Leffler 	 * whether this is the first time through or not.
4915591b213SSam Leffler 	 */
4925591b213SSam Leffler 	ath_stop(ifp);
4935591b213SSam Leffler 
4945591b213SSam Leffler 	/*
4955591b213SSam Leffler 	 * The basic interface to setting the hardware in a good
4965591b213SSam Leffler 	 * state is ``reset''.  On return the hardware is known to
4975591b213SSam Leffler 	 * be powered up and with interrupts disabled.  This must
4985591b213SSam Leffler 	 * be followed by initialization of the appropriate bits
4995591b213SSam Leffler 	 * and then setup of the interrupt mask.
5005591b213SSam Leffler 	 */
5015591b213SSam Leffler 	hchan.channel = ic->ic_ibss_chan->ic_freq;
5025591b213SSam Leffler 	hchan.channelFlags = ath_chan2flags(ic, ic->ic_ibss_chan);
5035591b213SSam Leffler 	if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_FALSE, &status)) {
5045591b213SSam Leffler 		if_printf(ifp, "unable to reset hardware; hal status %u\n",
5055591b213SSam Leffler 			status);
5065591b213SSam Leffler 		goto done;
5075591b213SSam Leffler 	}
5085591b213SSam Leffler 
5095591b213SSam Leffler 	/*
5105591b213SSam Leffler 	 * Setup the hardware after reset: the key cache
5115591b213SSam Leffler 	 * is filled as needed and the receive engine is
5125591b213SSam Leffler 	 * set going.  Frame transmit is handled entirely
5135591b213SSam Leffler 	 * in the frame output path; there's nothing to do
5145591b213SSam Leffler 	 * here except setup the interrupt mask.
5155591b213SSam Leffler 	 */
5165591b213SSam Leffler 	if (ic->ic_flags & IEEE80211_F_WEPON)
5175591b213SSam Leffler 		ath_initkeytable(sc);
5185591b213SSam Leffler 	if (ath_startrecv(sc) != 0) {
5195591b213SSam Leffler 		if_printf(ifp, "unable to start recv logic\n");
5205591b213SSam Leffler 		goto done;
5215591b213SSam Leffler 	}
5225591b213SSam Leffler 
5235591b213SSam Leffler 	/*
5245591b213SSam Leffler 	 * Enable interrupts.
5255591b213SSam Leffler 	 */
5265591b213SSam Leffler 	sc->sc_imask = HAL_INT_RX | HAL_INT_TX
5275591b213SSam Leffler 		  | HAL_INT_RXEOL | HAL_INT_RXORN
5285591b213SSam Leffler 		  | HAL_INT_FATAL | HAL_INT_GLOBAL;
5295591b213SSam Leffler 	ath_hal_intrset(ah, sc->sc_imask);
5305591b213SSam Leffler 
5315591b213SSam Leffler 	ifp->if_flags |= IFF_RUNNING;
5325591b213SSam Leffler 	ic->ic_state = IEEE80211_S_INIT;
5335591b213SSam Leffler 
5345591b213SSam Leffler 	/*
5355591b213SSam Leffler 	 * The hardware should be ready to go now so it's safe
5365591b213SSam Leffler 	 * to kick the 802.11 state machine as it's likely to
5375591b213SSam Leffler 	 * immediately call back to us to send mgmt frames.
5385591b213SSam Leffler 	 */
5395591b213SSam Leffler 	ni = ic->ic_bss;
5405591b213SSam Leffler 	ni->ni_chan = ic->ic_ibss_chan;
5415591b213SSam Leffler 	mode = ieee80211_chan2mode(ic, ni->ni_chan);
5425591b213SSam Leffler 	if (mode != sc->sc_curmode)
5435591b213SSam Leffler 		ath_setcurmode(sc, mode);
5446b59f5e3SSam Leffler 	if (ic->ic_opmode != IEEE80211_M_MONITOR)
54545bbf62fSSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
5466b59f5e3SSam Leffler 	else
5476b59f5e3SSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
5485591b213SSam Leffler done:
5495591b213SSam Leffler 	mtx_unlock(&sc->sc_mtx);
5505591b213SSam Leffler }
5515591b213SSam Leffler 
5525591b213SSam Leffler static void
5535591b213SSam Leffler ath_stop(struct ifnet *ifp)
5545591b213SSam Leffler {
55545bbf62fSSam Leffler 	struct ieee80211com *ic = (struct ieee80211com *) ifp;
5565591b213SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
5575591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5585591b213SSam Leffler 
5595591b213SSam Leffler 	DPRINTF(("ath_stop: invalid %u if_flags 0x%x\n",
5605591b213SSam Leffler 		sc->sc_invalid, ifp->if_flags));
5615591b213SSam Leffler 
5625591b213SSam Leffler 	mtx_lock(&sc->sc_mtx);
5635591b213SSam Leffler 	if (ifp->if_flags & IFF_RUNNING) {
5645591b213SSam Leffler 		/*
5655591b213SSam Leffler 		 * Shutdown the hardware and driver:
5665591b213SSam Leffler 		 *    disable interrupts
5675591b213SSam Leffler 		 *    turn off timers
5685591b213SSam Leffler 		 *    clear transmit machinery
5695591b213SSam Leffler 		 *    clear receive machinery
5705591b213SSam Leffler 		 *    drain and release tx queues
5715591b213SSam Leffler 		 *    reclaim beacon resources
5725591b213SSam Leffler 		 *    reset 802.11 state machine
5735591b213SSam Leffler 		 *    power down hardware
5745591b213SSam Leffler 		 *
5755591b213SSam Leffler 		 * Note that some of this work is not possible if the
5765591b213SSam Leffler 		 * hardware is gone (invalid).
5775591b213SSam Leffler 		 */
5785591b213SSam Leffler 		ifp->if_flags &= ~IFF_RUNNING;
5795591b213SSam Leffler 		ifp->if_timer = 0;
5805591b213SSam Leffler 		if (!sc->sc_invalid)
5815591b213SSam Leffler 			ath_hal_intrset(ah, 0);
5825591b213SSam Leffler 		ath_draintxq(sc);
5835591b213SSam Leffler 		if (!sc->sc_invalid)
5845591b213SSam Leffler 			ath_stoprecv(sc);
5855591b213SSam Leffler 		else
5865591b213SSam Leffler 			sc->sc_rxlink = NULL;
5875591b213SSam Leffler 		IF_DRAIN(&ifp->if_snd);
5885591b213SSam Leffler 		ath_beacon_free(sc);
58945bbf62fSSam Leffler 		ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
5905591b213SSam Leffler 		if (!sc->sc_invalid)
5915591b213SSam Leffler 			ath_hal_setpower(ah, HAL_PM_FULL_SLEEP, 0);
5925591b213SSam Leffler 	}
5935591b213SSam Leffler 	mtx_unlock(&sc->sc_mtx);
5945591b213SSam Leffler }
5955591b213SSam Leffler 
5965591b213SSam Leffler /*
5975591b213SSam Leffler  * Reset the hardware w/o losing operational state.  This is
5985591b213SSam Leffler  * basically a more efficient way of doing ath_stop, ath_init,
5995591b213SSam Leffler  * followed by state transitions to the current 802.11
6005591b213SSam Leffler  * operational state.  Used to recover from errors rx overrun
6015591b213SSam Leffler  * and to reset the hardware when rf gain settings must be reset.
6025591b213SSam Leffler  */
6035591b213SSam Leffler static void
6045591b213SSam Leffler ath_reset(struct ath_softc *sc)
6055591b213SSam Leffler {
6065591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
6075591b213SSam Leffler 	struct ifnet *ifp = &ic->ic_if;
6085591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
6095591b213SSam Leffler 	struct ieee80211_channel *c;
6105591b213SSam Leffler 	HAL_STATUS status;
6115591b213SSam Leffler 	HAL_CHANNEL hchan;
6125591b213SSam Leffler 
6135591b213SSam Leffler 	/*
6145591b213SSam Leffler 	 * Convert to a HAL channel description with the flags
6155591b213SSam Leffler 	 * constrained to reflect the current operating mode.
6165591b213SSam Leffler 	 */
6175591b213SSam Leffler 	c = ic->ic_ibss_chan;
6185591b213SSam Leffler 	hchan.channel = c->ic_freq;
6195591b213SSam Leffler 	hchan.channelFlags = ath_chan2flags(ic, c);
6205591b213SSam Leffler 
6215591b213SSam Leffler 	ath_hal_intrset(ah, 0);		/* disable interrupts */
6225591b213SSam Leffler 	ath_draintxq(sc);		/* stop xmit side */
6235591b213SSam Leffler 	ath_stoprecv(sc);		/* stop recv side */
6245591b213SSam Leffler 	/* NB: indicate channel change so we do a full reset */
6255591b213SSam Leffler 	if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_TRUE, &status))
6265591b213SSam Leffler 		if_printf(ifp, "%s: unable to reset hardware; hal status %u\n",
6275591b213SSam Leffler 			__func__, status);
6285591b213SSam Leffler 	ath_hal_intrset(ah, sc->sc_imask);
6295591b213SSam Leffler 	if (ath_startrecv(sc) != 0)	/* restart recv */
6305591b213SSam Leffler 		if_printf(ifp, "%s: unable to start recv logic\n", __func__);
6315591b213SSam Leffler 	ath_start(ifp);			/* restart xmit */
6325591b213SSam Leffler 	if (ic->ic_state == IEEE80211_S_RUN)
6335591b213SSam Leffler 		ath_beacon_config(sc);	/* restart beacons */
6345591b213SSam Leffler }
6355591b213SSam Leffler 
6365591b213SSam Leffler static void
6375591b213SSam Leffler ath_start(struct ifnet *ifp)
6385591b213SSam Leffler {
6395591b213SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
6405591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
6415591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
6425591b213SSam Leffler 	struct ieee80211_node *ni;
6435591b213SSam Leffler 	struct ath_buf *bf;
6445591b213SSam Leffler 	struct mbuf *m;
6455591b213SSam Leffler 	struct ieee80211_frame *wh;
6465591b213SSam Leffler 
6475591b213SSam Leffler 	if ((ifp->if_flags & IFF_RUNNING) == 0 || sc->sc_invalid)
6485591b213SSam Leffler 		return;
6495591b213SSam Leffler 	for (;;) {
6505591b213SSam Leffler 		/*
6515591b213SSam Leffler 		 * Grab a TX buffer and associated resources.
6525591b213SSam Leffler 		 */
6535591b213SSam Leffler 		mtx_lock(&sc->sc_txbuflock);
6545591b213SSam Leffler 		bf = TAILQ_FIRST(&sc->sc_txbuf);
6555591b213SSam Leffler 		if (bf != NULL)
6565591b213SSam Leffler 			TAILQ_REMOVE(&sc->sc_txbuf, bf, bf_list);
6575591b213SSam Leffler 		mtx_unlock(&sc->sc_txbuflock);
6585591b213SSam Leffler 		if (bf == NULL) {
6595591b213SSam Leffler 			DPRINTF(("ath_start: out of xmit buffers\n"));
6605591b213SSam Leffler 			sc->sc_stats.ast_tx_qstop++;
6615591b213SSam Leffler 			ifp->if_flags |= IFF_OACTIVE;
6625591b213SSam Leffler 			break;
6635591b213SSam Leffler 		}
6645591b213SSam Leffler 		/*
6655591b213SSam Leffler 		 * Poll the management queue for frames; they
6665591b213SSam Leffler 		 * have priority over normal data frames.
6675591b213SSam Leffler 		 */
6685591b213SSam Leffler 		IF_DEQUEUE(&ic->ic_mgtq, m);
6695591b213SSam Leffler 		if (m == NULL) {
6705591b213SSam Leffler 			/*
6715591b213SSam Leffler 			 * No data frames go out unless we're associated.
6725591b213SSam Leffler 			 */
6735591b213SSam Leffler 			if (ic->ic_state != IEEE80211_S_RUN) {
6745591b213SSam Leffler 				DPRINTF(("ath_start: ignore data packet, "
6755591b213SSam Leffler 					"state %u\n", ic->ic_state));
6765591b213SSam Leffler 				sc->sc_stats.ast_tx_discard++;
6775591b213SSam Leffler 				mtx_lock(&sc->sc_txbuflock);
6785591b213SSam Leffler 				TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
6795591b213SSam Leffler 				mtx_unlock(&sc->sc_txbuflock);
6805591b213SSam Leffler 				break;
6815591b213SSam Leffler 			}
6825591b213SSam Leffler 			IF_DEQUEUE(&ifp->if_snd, m);
6835591b213SSam Leffler 			if (m == NULL) {
6845591b213SSam Leffler 				mtx_lock(&sc->sc_txbuflock);
6855591b213SSam Leffler 				TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
6865591b213SSam Leffler 				mtx_unlock(&sc->sc_txbuflock);
6875591b213SSam Leffler 				break;
6885591b213SSam Leffler 			}
6895591b213SSam Leffler 			ifp->if_opackets++;
6905591b213SSam Leffler 			BPF_MTAP(ifp, m);
6915591b213SSam Leffler 			/*
6925591b213SSam Leffler 			 * Encapsulate the packet in prep for transmission.
6935591b213SSam Leffler 			 */
6945591b213SSam Leffler 			m = ieee80211_encap(ifp, m);
6955591b213SSam Leffler 			if (m == NULL) {
6965591b213SSam Leffler 				DPRINTF(("ath_start: encapsulation failure\n"));
6975591b213SSam Leffler 				sc->sc_stats.ast_tx_encap++;
6985591b213SSam Leffler 				goto bad;
6995591b213SSam Leffler 			}
7005591b213SSam Leffler 			wh = mtod(m, struct ieee80211_frame *);
7015591b213SSam Leffler 			if (ic->ic_flags & IEEE80211_F_WEPON)
7025591b213SSam Leffler 				wh->i_fc[1] |= IEEE80211_FC1_WEP;
7035591b213SSam Leffler 		} else {
7045591b213SSam Leffler 			wh = mtod(m, struct ieee80211_frame *);
7055591b213SSam Leffler 			if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
7065591b213SSam Leffler 			    IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
7075591b213SSam Leffler 				/* fill time stamp */
7085591b213SSam Leffler 				u_int64_t tsf;
7095591b213SSam Leffler 				u_int32_t *tstamp;
7105591b213SSam Leffler 
7115591b213SSam Leffler 				tsf = ath_hal_gettsf64(ah);
7125591b213SSam Leffler 				/* XXX: adjust 100us delay to xmit */
7135591b213SSam Leffler 				tsf += 100;
7145591b213SSam Leffler 				tstamp = (u_int32_t *)&wh[1];
7155591b213SSam Leffler 				tstamp[0] = htole32(tsf & 0xffffffff);
7165591b213SSam Leffler 				tstamp[1] = htole32(tsf >> 32);
7175591b213SSam Leffler 			}
7185591b213SSam Leffler 			sc->sc_stats.ast_tx_mgmt++;
7195591b213SSam Leffler 		}
7205591b213SSam Leffler 		if (ic->ic_rawbpf)
7215591b213SSam Leffler 			bpf_mtap(ic->ic_rawbpf, m);
7225591b213SSam Leffler 
7235591b213SSam Leffler 		if (ic->ic_opmode != IEEE80211_M_STA) {
7245591b213SSam Leffler 			ni = ieee80211_find_node(ic, wh->i_addr1);
7255591b213SSam Leffler 			if (ni == NULL) {
7265591b213SSam Leffler 				/*
7275591b213SSam Leffler 				 * When not in station mode the destination
7285591b213SSam Leffler 				 * address should always be in the node table
7295591b213SSam Leffler 				 * unless this is a multicast/broadcast frame.
7305591b213SSam Leffler 				 */
7315591b213SSam Leffler 				if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
7325591b213SSam Leffler 				    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
7335591b213SSam Leffler 				    IEEE80211_FC0_TYPE_DATA) {
7345591b213SSam Leffler 					m_freem(m);
7355591b213SSam Leffler 					sc->sc_stats.ast_tx_nonode++;
7365591b213SSam Leffler 					goto bad;
7375591b213SSam Leffler 				}
7385591b213SSam Leffler 				ni = ic->ic_bss;
7395591b213SSam Leffler 			}
7405591b213SSam Leffler 		} else
7415591b213SSam Leffler 			ni = ic->ic_bss;
7425591b213SSam Leffler 
7435591b213SSam Leffler 		/*
7445591b213SSam Leffler 		 * TODO:
7455591b213SSam Leffler 		 * The duration field of 802.11 header should be filled.
7465591b213SSam Leffler 		 * XXX This may be done in the ieee80211 layer, but the upper
7475591b213SSam Leffler 		 *     doesn't know the detail of parameters such as IFS
7485591b213SSam Leffler 		 *     for now..
7495591b213SSam Leffler 		 */
7505591b213SSam Leffler 
7515591b213SSam Leffler 		if (IFF_DUMPPKTS(ifp))
7525591b213SSam Leffler 			ieee80211_dump_pkt(mtod(m, u_int8_t *), m->m_len,
7535591b213SSam Leffler 			    ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL,
7545591b213SSam Leffler 			    -1);
7555591b213SSam Leffler 
7565591b213SSam Leffler 		if (ath_tx_start(sc, ni, bf, m)) {
7575591b213SSam Leffler 	bad:
7585591b213SSam Leffler 			mtx_lock(&sc->sc_txbuflock);
7595591b213SSam Leffler 			TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
7605591b213SSam Leffler 			mtx_unlock(&sc->sc_txbuflock);
7615591b213SSam Leffler 			ifp->if_oerrors++;
7625591b213SSam Leffler 			continue;
7635591b213SSam Leffler 		}
7645591b213SSam Leffler 
7655591b213SSam Leffler 		sc->sc_tx_timer = 5;
7665591b213SSam Leffler 		ifp->if_timer = 1;
7675591b213SSam Leffler 	}
7685591b213SSam Leffler }
7695591b213SSam Leffler 
7705591b213SSam Leffler static int
7715591b213SSam Leffler ath_media_change(struct ifnet *ifp)
7725591b213SSam Leffler {
7735591b213SSam Leffler 	int error;
7745591b213SSam Leffler 
7755591b213SSam Leffler 	error = ieee80211_media_change(ifp);
7765591b213SSam Leffler 	if (error == ENETRESET) {
7775591b213SSam Leffler 		if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) ==
7785591b213SSam Leffler 		    (IFF_RUNNING|IFF_UP))
7795591b213SSam Leffler 			ath_init(ifp);		/* XXX lose error */
7805591b213SSam Leffler 		error = 0;
7815591b213SSam Leffler 	}
7825591b213SSam Leffler 	return error;
7835591b213SSam Leffler }
7845591b213SSam Leffler 
7855591b213SSam Leffler static void
7865591b213SSam Leffler ath_watchdog(struct ifnet *ifp)
7875591b213SSam Leffler {
7885591b213SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
7895591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
7905591b213SSam Leffler 
7915591b213SSam Leffler 	ifp->if_timer = 0;
7925591b213SSam Leffler 	if ((ifp->if_flags & IFF_RUNNING) == 0 || sc->sc_invalid)
7935591b213SSam Leffler 		return;
7945591b213SSam Leffler 	if (sc->sc_tx_timer) {
7955591b213SSam Leffler 		if (--sc->sc_tx_timer == 0) {
7965591b213SSam Leffler 			if_printf(ifp, "device timeout\n");
7975591b213SSam Leffler #ifdef AR_DEBUG
7985591b213SSam Leffler 			if (ath_debug)
7995591b213SSam Leffler 				ath_hal_dumpstate(sc->sc_ah);
8005591b213SSam Leffler #endif /* AR_DEBUG */
8015591b213SSam Leffler 			ath_init(ifp);		/* XXX ath_reset??? */
8025591b213SSam Leffler 			ifp->if_oerrors++;
8035591b213SSam Leffler 			sc->sc_stats.ast_watchdog++;
8045591b213SSam Leffler 			return;
8055591b213SSam Leffler 		}
8065591b213SSam Leffler 		ifp->if_timer = 1;
8075591b213SSam Leffler 	}
8085591b213SSam Leffler 	if (ic->ic_fixed_rate == -1) {
8095591b213SSam Leffler 		/*
8105591b213SSam Leffler 		 * Run the rate control algorithm if we're not
8115591b213SSam Leffler 		 * locked at a fixed rate.
8125591b213SSam Leffler 		 */
8135591b213SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_STA)
8145591b213SSam Leffler 			ath_rate_ctl(sc, ic->ic_bss);
8155591b213SSam Leffler 		else
8165591b213SSam Leffler 			ieee80211_iterate_nodes(ic, ath_rate_ctl, sc);
8175591b213SSam Leffler 	}
8185591b213SSam Leffler 	ieee80211_watchdog(ifp);
8195591b213SSam Leffler }
8205591b213SSam Leffler 
8215591b213SSam Leffler static int
8225591b213SSam Leffler ath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
8235591b213SSam Leffler {
8245591b213SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
8255591b213SSam Leffler 	struct ifreq *ifr = (struct ifreq *)data;
8265591b213SSam Leffler 	int error = 0;
8275591b213SSam Leffler 
8285591b213SSam Leffler 	mtx_lock(&sc->sc_mtx);
8295591b213SSam Leffler 	switch (cmd) {
8305591b213SSam Leffler 	case SIOCSIFFLAGS:
8315591b213SSam Leffler 		if (ifp->if_flags & IFF_UP) {
8325591b213SSam Leffler 			if (ifp->if_flags & IFF_RUNNING) {
8335591b213SSam Leffler 				/*
8345591b213SSam Leffler 				 * To avoid rescanning another access point,
8355591b213SSam Leffler 				 * do not call ath_init() here.  Instead,
8365591b213SSam Leffler 				 * only reflect promisc mode settings.
8375591b213SSam Leffler 				 */
8385591b213SSam Leffler 				ath_mode_init(sc);
8395591b213SSam Leffler 			} else
8405591b213SSam Leffler 				ath_init(ifp);		/* XXX lose error */
8415591b213SSam Leffler 		} else
8425591b213SSam Leffler 			ath_stop(ifp);
8435591b213SSam Leffler 		break;
8445591b213SSam Leffler 	case SIOCADDMULTI:
8455591b213SSam Leffler 	case SIOCDELMULTI:
8465591b213SSam Leffler 		/*
8475591b213SSam Leffler 		 * The upper layer has already installed/removed
8485591b213SSam Leffler 		 * the multicast address(es), just recalculate the
8495591b213SSam Leffler 		 * multicast filter for the card.
8505591b213SSam Leffler 		 */
8515591b213SSam Leffler 		if (ifp->if_flags & IFF_RUNNING)
8525591b213SSam Leffler 			ath_mode_init(sc);
8535591b213SSam Leffler 		break;
8545591b213SSam Leffler 	case SIOCGATHSTATS:
8555591b213SSam Leffler 		copyout(&sc->sc_stats, ifr->ifr_data, sizeof (sc->sc_stats));
8565591b213SSam Leffler 		break;
8575591b213SSam Leffler 	default:
8585591b213SSam Leffler 		error = ieee80211_ioctl(ifp, cmd, data);
8595591b213SSam Leffler 		if (error == ENETRESET) {
8605591b213SSam Leffler 			if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) ==
8615591b213SSam Leffler 			    (IFF_RUNNING|IFF_UP))
8625591b213SSam Leffler 				ath_init(ifp);		/* XXX lose error */
8635591b213SSam Leffler 			error = 0;
8645591b213SSam Leffler 		}
8655591b213SSam Leffler 		break;
8665591b213SSam Leffler 	}
8675591b213SSam Leffler 	mtx_unlock(&sc->sc_mtx);
8685591b213SSam Leffler 	return error;
8695591b213SSam Leffler }
8705591b213SSam Leffler 
8715591b213SSam Leffler /*
8725591b213SSam Leffler  * Fill the hardware key cache with key entries.
8735591b213SSam Leffler  */
8745591b213SSam Leffler static void
8755591b213SSam Leffler ath_initkeytable(struct ath_softc *sc)
8765591b213SSam Leffler {
8775591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
8785591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
8795591b213SSam Leffler 	int i;
8805591b213SSam Leffler 
8815591b213SSam Leffler 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
8825591b213SSam Leffler 		struct ieee80211_wepkey *k = &ic->ic_nw_keys[i];
8835591b213SSam Leffler 		if (k->wk_len == 0)
8845591b213SSam Leffler 			ath_hal_keyreset(ah, i);
8855591b213SSam Leffler 		else
8865591b213SSam Leffler 			/* XXX return value */
8875591b213SSam Leffler 			/* NB: this uses HAL_KEYVAL == ieee80211_wepkey */
8885591b213SSam Leffler 			ath_hal_keyset(ah, i, (const HAL_KEYVAL *) k);
8895591b213SSam Leffler 	}
8905591b213SSam Leffler }
8915591b213SSam Leffler 
8925591b213SSam Leffler static void
8935591b213SSam Leffler ath_mode_init(struct ath_softc *sc)
8945591b213SSam Leffler {
8955591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
8965591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
8975591b213SSam Leffler 	struct ifnet *ifp = &ic->ic_if;
8985591b213SSam Leffler 	u_int32_t rfilt, mfilt[2], val;
8995591b213SSam Leffler 	u_int8_t pos;
9005591b213SSam Leffler 	struct ifmultiaddr *ifma;
9015591b213SSam Leffler 
9025591b213SSam Leffler 	/* configure operational mode */
9035591b213SSam Leffler 	ath_hal_setopmode(ah, ic->ic_opmode);
9045591b213SSam Leffler 
9055591b213SSam Leffler 	/* receive filter */
9065591b213SSam Leffler 	rfilt = (ath_hal_getrxfilter(ah) & HAL_RX_FILTER_PHYERR)
9075591b213SSam Leffler 	      | HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST;
9085591b213SSam Leffler 	if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
9095591b213SSam Leffler 	    (ifp->if_flags & IFF_PROMISC))
9105591b213SSam Leffler 		rfilt |= HAL_RX_FILTER_PROM;
9115591b213SSam Leffler 	if (ic->ic_state == IEEE80211_S_SCAN)
9125591b213SSam Leffler 		rfilt |= HAL_RX_FILTER_BEACON;
9135591b213SSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
9145591b213SSam Leffler 
9155591b213SSam Leffler 	/* calculate and install multicast filter */
9165591b213SSam Leffler 	if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
9175591b213SSam Leffler 		mfilt[0] = mfilt[1] = 0;
9185591b213SSam Leffler 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
9195591b213SSam Leffler 			caddr_t dl;
9205591b213SSam Leffler 
9215591b213SSam Leffler 			/* calculate XOR of eight 6bit values */
9225591b213SSam Leffler 			dl = LLADDR((struct sockaddr_dl *) ifma->ifma_addr);
9235591b213SSam Leffler 			val = LE_READ_4(dl + 0);
9245591b213SSam Leffler 			pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
9255591b213SSam Leffler 			val = LE_READ_4(dl + 3);
9265591b213SSam Leffler 			pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
9275591b213SSam Leffler 			pos &= 0x3f;
9285591b213SSam Leffler 			mfilt[pos / 32] |= (1 << (pos % 32));
9295591b213SSam Leffler 		}
9305591b213SSam Leffler 	} else {
9315591b213SSam Leffler 		mfilt[0] = mfilt[1] = ~0;
9325591b213SSam Leffler 	}
9335591b213SSam Leffler 	ath_hal_setmcastfilter(ah, mfilt[0], mfilt[1]);
9345591b213SSam Leffler 	DPRINTF(("ath_mode_init: RX filter 0x%x, MC filter %08x:%08x\n",
9355591b213SSam Leffler 		rfilt, mfilt[0], mfilt[1]));
9365591b213SSam Leffler }
9375591b213SSam Leffler 
9385591b213SSam Leffler static void
9395591b213SSam Leffler ath_mbuf_load_cb(void *arg, bus_dma_segment_t *seg, int nseg, bus_size_t mapsize, int error)
9405591b213SSam Leffler {
9415591b213SSam Leffler 	struct ath_buf *bf = arg;
9425591b213SSam Leffler 
9435591b213SSam Leffler 	KASSERT(nseg <= ATH_MAX_SCATTER,
9445591b213SSam Leffler 		("ath_mbuf_load_cb: too many DMA segments %u", nseg));
9455591b213SSam Leffler 	bf->bf_mapsize = mapsize;
9465591b213SSam Leffler 	bf->bf_nseg = nseg;
9475591b213SSam Leffler 	bcopy(seg, bf->bf_segs, nseg * sizeof (seg[0]));
9485591b213SSam Leffler }
9495591b213SSam Leffler 
9505591b213SSam Leffler static int
9515591b213SSam Leffler ath_beacon_alloc(struct ath_softc *sc, struct ieee80211_node *ni)
9525591b213SSam Leffler {
9535591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
9545591b213SSam Leffler 	struct ifnet *ifp = &ic->ic_if;
9555591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
9565591b213SSam Leffler 	struct ieee80211_frame *wh;
9575591b213SSam Leffler 	struct ath_buf *bf;
9585591b213SSam Leffler 	struct ath_desc *ds;
9595591b213SSam Leffler 	struct mbuf *m;
9605591b213SSam Leffler 	int error, pktlen;
9615591b213SSam Leffler 	u_int8_t *frm, rate;
9625591b213SSam Leffler 	u_int16_t capinfo;
9635591b213SSam Leffler 	struct ieee80211_rateset *rs;
9645591b213SSam Leffler 	const HAL_RATE_TABLE *rt;
9655591b213SSam Leffler 
9665591b213SSam Leffler 	bf = sc->sc_bcbuf;
9675591b213SSam Leffler 	if (bf->bf_m != NULL) {
9685591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
9695591b213SSam Leffler 		m_freem(bf->bf_m);
9705591b213SSam Leffler 		bf->bf_m = NULL;
9715591b213SSam Leffler 		bf->bf_node = NULL;
9725591b213SSam Leffler 	}
9735591b213SSam Leffler 	/*
9745591b213SSam Leffler 	 * NB: the beacon data buffer must be 32-bit aligned;
9755591b213SSam Leffler 	 * we assume the mbuf routines will return us something
9765591b213SSam Leffler 	 * with this alignment (perhaps should assert).
9775591b213SSam Leffler 	 */
9785591b213SSam Leffler 	rs = &ni->ni_rates;
9799cccabebSSam Leffler 	pktlen = sizeof (struct ieee80211_frame)
9809cccabebSSam Leffler 	       + 8 + 2 + 2 + 2+ni->ni_esslen + 2+rs->rs_nrates + 6;
9815591b213SSam Leffler 	if (rs->rs_nrates > IEEE80211_RATE_SIZE)
9825591b213SSam Leffler 		pktlen += 2;
9835591b213SSam Leffler 	if (pktlen <= MHLEN)
9845591b213SSam Leffler 		MGETHDR(m, M_DONTWAIT, MT_DATA);
9855591b213SSam Leffler 	else
9865591b213SSam Leffler 		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
9875591b213SSam Leffler 	if (m == NULL) {
9885591b213SSam Leffler 		DPRINTF(("ath_beacon_alloc: cannot get mbuf/cluster; size %u\n",
9895591b213SSam Leffler 			pktlen));
9905591b213SSam Leffler 		sc->sc_stats.ast_be_nombuf++;
9915591b213SSam Leffler 		return ENOMEM;
9925591b213SSam Leffler 	}
9935591b213SSam Leffler 
9945591b213SSam Leffler 	wh = mtod(m, struct ieee80211_frame *);
9955591b213SSam Leffler 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
9965591b213SSam Leffler 	    IEEE80211_FC0_SUBTYPE_BEACON;
9975591b213SSam Leffler 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
9985591b213SSam Leffler 	*(u_int16_t *)wh->i_dur = 0;
9995591b213SSam Leffler 	memcpy(wh->i_addr1, ifp->if_broadcastaddr, IEEE80211_ADDR_LEN);
10005591b213SSam Leffler 	memcpy(wh->i_addr2, ic->ic_myaddr, IEEE80211_ADDR_LEN);
10015591b213SSam Leffler 	memcpy(wh->i_addr3, ni->ni_bssid, IEEE80211_ADDR_LEN);
10025591b213SSam Leffler 	*(u_int16_t *)wh->i_seq = 0;
10035591b213SSam Leffler 
10045591b213SSam Leffler 	/*
10055591b213SSam Leffler 	 * beacon frame format
10065591b213SSam Leffler 	 *	[8] time stamp
10075591b213SSam Leffler 	 *	[2] beacon interval
10085591b213SSam Leffler 	 *	[2] cabability information
10095591b213SSam Leffler 	 *	[tlv] ssid
10105591b213SSam Leffler 	 *	[tlv] supported rates
10115591b213SSam Leffler 	 *	[tlv] parameter set (IBSS)
10125591b213SSam Leffler 	 *	[tlv] extended supported rates
10135591b213SSam Leffler 	 */
10145591b213SSam Leffler 	frm = (u_int8_t *)&wh[1];
10155591b213SSam Leffler 	memset(frm, 0, 8);	/* timestamp is set by hardware */
10165591b213SSam Leffler 	frm += 8;
10175591b213SSam Leffler 	*(u_int16_t *)frm = htole16(ni->ni_intval);
10185591b213SSam Leffler 	frm += 2;
10195591b213SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS)
10205591b213SSam Leffler 		capinfo = IEEE80211_CAPINFO_IBSS;
10215591b213SSam Leffler 	else
10225591b213SSam Leffler 		capinfo = IEEE80211_CAPINFO_ESS;
10235591b213SSam Leffler 	if (ic->ic_flags & IEEE80211_F_WEPON)
10245591b213SSam Leffler 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
10255591b213SSam Leffler 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
10265591b213SSam Leffler 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
10275591b213SSam Leffler 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
10285591b213SSam Leffler 		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
10295591b213SSam Leffler 	*(u_int16_t *)frm = htole16(capinfo);
10305591b213SSam Leffler 	frm += 2;
10315591b213SSam Leffler 	*frm++ = IEEE80211_ELEMID_SSID;
10325591b213SSam Leffler 	*frm++ = ni->ni_esslen;
10335591b213SSam Leffler 	memcpy(frm, ni->ni_essid, ni->ni_esslen);
10345591b213SSam Leffler 	frm += ni->ni_esslen;
10355591b213SSam Leffler 	frm = ieee80211_add_rates(frm, rs);
10365591b213SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
10375591b213SSam Leffler 		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
10385591b213SSam Leffler 		*frm++ = 2;
10395591b213SSam Leffler 		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
10405591b213SSam Leffler 	} else {
10415591b213SSam Leffler 		/* TODO: TIM */
10425591b213SSam Leffler 		*frm++ = IEEE80211_ELEMID_TIM;
10435591b213SSam Leffler 		*frm++ = 4;	/* length */
10445591b213SSam Leffler 		*frm++ = 0;	/* DTIM count */
10455591b213SSam Leffler 		*frm++ = 1;	/* DTIM period */
10465591b213SSam Leffler 		*frm++ = 0;	/* bitmap control */
10475591b213SSam Leffler 		*frm++ = 0;	/* Partial Virtual Bitmap (variable length) */
10485591b213SSam Leffler 	}
10495591b213SSam Leffler 	frm = ieee80211_add_xrates(frm, rs);
10505591b213SSam Leffler 	m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
10519cccabebSSam Leffler 	KASSERT(m->m_pkthdr.len <= pktlen,
10529cccabebSSam Leffler 		("beacon bigger than expected, len %u calculated %u",
10539cccabebSSam Leffler 		m->m_pkthdr.len, pktlen));
10545591b213SSam Leffler 
10555591b213SSam Leffler 	DPRINTF2(("ath_beacon_alloc: m %p len %u\n", m, m->m_len));
10565591b213SSam Leffler 	error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m,
10575591b213SSam Leffler 				     ath_mbuf_load_cb, bf,
10585591b213SSam Leffler 				     BUS_DMA_NOWAIT);
10595591b213SSam Leffler 	if (error != 0) {
10605591b213SSam Leffler 		m_freem(m);
10615591b213SSam Leffler 		return error;
10625591b213SSam Leffler 	}
10635591b213SSam Leffler 	KASSERT(bf->bf_nseg == 1,
10645591b213SSam Leffler 		("ath_beacon_alloc: multi-segment packet; nseg %u",
10655591b213SSam Leffler 		bf->bf_nseg));
10665591b213SSam Leffler 	bf->bf_m = m;
10675591b213SSam Leffler 
10685591b213SSam Leffler 	/* setup descriptors */
10695591b213SSam Leffler 	ds = bf->bf_desc;
10705591b213SSam Leffler 
10715591b213SSam Leffler 	ds->ds_link = 0;
10725591b213SSam Leffler 	ds->ds_data = bf->bf_segs[0].ds_addr;
10735591b213SSam Leffler 	/*
10745591b213SSam Leffler 	 * Calculate rate code.
10755591b213SSam Leffler 	 * XXX everything at min xmit rate
10765591b213SSam Leffler 	 */
10775591b213SSam Leffler 	rt = sc->sc_currates;
10785591b213SSam Leffler 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
10796b59f5e3SSam Leffler 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
10805591b213SSam Leffler 		rate = rt->info[0].rateCode | rt->info[0].shortPreamble;
10815591b213SSam Leffler 	else
10825591b213SSam Leffler 		rate = rt->info[0].rateCode;
10835591b213SSam Leffler 	ath_hal_setuptxdesc(ah, ds
10845591b213SSam Leffler 		, m->m_pkthdr.len + IEEE80211_CRC_LEN	/* packet length */
10855591b213SSam Leffler 		, sizeof(struct ieee80211_frame)	/* header length */
10865591b213SSam Leffler 		, HAL_PKT_TYPE_BEACON		/* Atheros packet type */
10875591b213SSam Leffler 		, 0x20				/* txpower XXX */
10885591b213SSam Leffler 		, rate, 1			/* series 0 rate/tries */
10895591b213SSam Leffler 		, HAL_TXKEYIX_INVALID		/* no encryption */
10905591b213SSam Leffler 		, 0				/* antenna mode */
10915591b213SSam Leffler 		, HAL_TXDESC_NOACK		/* no ack for beacons */
10925591b213SSam Leffler 		, 0				/* rts/cts rate */
10935591b213SSam Leffler 		, 0				/* rts/cts duration */
10945591b213SSam Leffler 	);
10955591b213SSam Leffler 	/* NB: beacon's BufLen must be a multiple of 4 bytes */
10969cccabebSSam Leffler 	/* XXX verify mbuf data area covers this roundup */
10975591b213SSam Leffler 	ath_hal_filltxdesc(ah, ds
10985591b213SSam Leffler 		, roundup(bf->bf_segs[0].ds_len, 4)	/* buffer length */
10995591b213SSam Leffler 		, AH_TRUE				/* first segment */
11005591b213SSam Leffler 		, AH_TRUE				/* last segment */
11015591b213SSam Leffler 	);
11025591b213SSam Leffler 
11035591b213SSam Leffler 	return 0;
11045591b213SSam Leffler }
11055591b213SSam Leffler 
11065591b213SSam Leffler static void
11075591b213SSam Leffler ath_beacon_proc(void *arg, int pending)
11085591b213SSam Leffler {
11095591b213SSam Leffler 	struct ath_softc *sc = arg;
11105591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
11115591b213SSam Leffler 	struct ath_buf *bf = sc->sc_bcbuf;
11125591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
11135591b213SSam Leffler 
11145591b213SSam Leffler 	DPRINTF2(("%s: pending %u\n", __func__, pending));
11155591b213SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA || bf == NULL || bf->bf_m == NULL) {
11165591b213SSam Leffler 		DPRINTF(("%s: ic_flags=%x bf=%p bf_m=%p\n",
11175591b213SSam Leffler 			__func__, ic->ic_flags, bf, bf ? bf->bf_m : NULL));
11185591b213SSam Leffler 		return;
11195591b213SSam Leffler 	}
11205591b213SSam Leffler 	/* update beacon to reflect PS poll state */
11215591b213SSam Leffler 	if (!ath_hal_stoptxdma(ah, sc->sc_bhalq)) {
11225591b213SSam Leffler 		DPRINTF(("%s: beacon queue %u did not stop?",
11235591b213SSam Leffler 			__func__, sc->sc_bhalq));
11245591b213SSam Leffler 		return;			/* busy, XXX is this right? */
11255591b213SSam Leffler 	}
11265591b213SSam Leffler 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
11275591b213SSam Leffler 
11285591b213SSam Leffler 	ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr);
11295591b213SSam Leffler 	ath_hal_txstart(ah, sc->sc_bhalq);
11305591b213SSam Leffler 	DPRINTF2(("%s: TXDP%u = %p (%p)\n", __func__,
11315591b213SSam Leffler 		sc->sc_bhalq, (caddr_t)bf->bf_daddr, bf->bf_desc));
11325591b213SSam Leffler }
11335591b213SSam Leffler 
11345591b213SSam Leffler static void
11355591b213SSam Leffler ath_beacon_free(struct ath_softc *sc)
11365591b213SSam Leffler {
11375591b213SSam Leffler 	struct ath_buf *bf = sc->sc_bcbuf;
11385591b213SSam Leffler 
11395591b213SSam Leffler 	if (bf->bf_m != NULL) {
11405591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
11415591b213SSam Leffler 		m_freem(bf->bf_m);
11425591b213SSam Leffler 		bf->bf_m = NULL;
11435591b213SSam Leffler 		bf->bf_node = NULL;
11445591b213SSam Leffler 	}
11455591b213SSam Leffler }
11465591b213SSam Leffler 
11475591b213SSam Leffler /*
11485591b213SSam Leffler  * Configure the beacon and sleep timers.
11495591b213SSam Leffler  *
11505591b213SSam Leffler  * When operating as an AP this resets the TSF and sets
11515591b213SSam Leffler  * up the hardware to notify us when we need to issue beacons.
11525591b213SSam Leffler  *
11535591b213SSam Leffler  * When operating in station mode this sets up the beacon
11545591b213SSam Leffler  * timers according to the timestamp of the last received
11555591b213SSam Leffler  * beacon and the current TSF, configures PCF and DTIM
11565591b213SSam Leffler  * handling, programs the sleep registers so the hardware
11575591b213SSam Leffler  * will wakeup in time to receive beacons, and configures
11585591b213SSam Leffler  * the beacon miss handling so we'll receive a BMISS
11595591b213SSam Leffler  * interrupt when we stop seeing beacons from the AP
11605591b213SSam Leffler  * we've associated with.
11615591b213SSam Leffler  */
11625591b213SSam Leffler static void
11635591b213SSam Leffler ath_beacon_config(struct ath_softc *sc)
11645591b213SSam Leffler {
11655591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
11665591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
11675591b213SSam Leffler 	struct ieee80211_node *ni = ic->ic_bss;
11685591b213SSam Leffler 	u_int32_t nexttbtt;
11695591b213SSam Leffler 
11705591b213SSam Leffler 	nexttbtt = (LE_READ_4(ni->ni_tstamp + 4) << 22) |
11715591b213SSam Leffler 	    (LE_READ_4(ni->ni_tstamp) >> 10);
11725591b213SSam Leffler 	DPRINTF(("%s: nexttbtt=%u\n", __func__, nexttbtt));
11735591b213SSam Leffler 	nexttbtt += ni->ni_intval;
11746b59f5e3SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA) {
11755591b213SSam Leffler 		HAL_BEACON_STATE bs;
11765591b213SSam Leffler 		u_int32_t bmisstime;
11775591b213SSam Leffler 
11785591b213SSam Leffler 		/* NB: no PCF support right now */
11795591b213SSam Leffler 		memset(&bs, 0, sizeof(bs));
11805591b213SSam Leffler 		bs.bs_intval = ni->ni_intval;
11815591b213SSam Leffler 		bs.bs_nexttbtt = nexttbtt;
11825591b213SSam Leffler 		bs.bs_dtimperiod = bs.bs_intval;
11835591b213SSam Leffler 		bs.bs_nextdtim = nexttbtt;
11845591b213SSam Leffler 		/*
11855591b213SSam Leffler 		 * Calculate the number of consecutive beacons to miss
11865591b213SSam Leffler 		 * before taking a BMISS interrupt.  The configuration
11875591b213SSam Leffler 		 * is specified in ms, so we need to convert that to
11885591b213SSam Leffler 		 * TU's and then calculate based on the beacon interval.
11895591b213SSam Leffler 		 * Note that we clamp the result to at most 10 beacons.
11905591b213SSam Leffler 		 */
11915591b213SSam Leffler 		bmisstime = (ic->ic_bmisstimeout * 1000) / 1024;
11925591b213SSam Leffler 		bs.bs_bmissthreshold = howmany(bmisstime,ni->ni_intval);
11935591b213SSam Leffler 		if (bs.bs_bmissthreshold > 10)
11945591b213SSam Leffler 			bs.bs_bmissthreshold = 10;
11955591b213SSam Leffler 		else if (bs.bs_bmissthreshold <= 0)
11965591b213SSam Leffler 			bs.bs_bmissthreshold = 1;
11975591b213SSam Leffler 
11985591b213SSam Leffler 		/*
11995591b213SSam Leffler 		 * Calculate sleep duration.  The configuration is
12005591b213SSam Leffler 		 * given in ms.  We insure a multiple of the beacon
12015591b213SSam Leffler 		 * period is used.  Also, if the sleep duration is
12025591b213SSam Leffler 		 * greater than the DTIM period then it makes senses
12035591b213SSam Leffler 		 * to make it a multiple of that.
12045591b213SSam Leffler 		 *
12055591b213SSam Leffler 		 * XXX fixed at 100ms
12065591b213SSam Leffler 		 */
12075591b213SSam Leffler 		bs.bs_sleepduration =
12085591b213SSam Leffler 			roundup((100 * 1000) / 1024, bs.bs_intval);
12095591b213SSam Leffler 		if (bs.bs_sleepduration > bs.bs_dtimperiod)
12105591b213SSam Leffler 			bs.bs_sleepduration = roundup(bs.bs_sleepduration, bs.bs_dtimperiod);
12115591b213SSam Leffler 
12125591b213SSam Leffler 		DPRINTF(("%s: intval %u nexttbtt %u dtim %u nextdtim %u bmiss %u sleep %u\n"
12135591b213SSam Leffler 			, __func__
12145591b213SSam Leffler 			, bs.bs_intval
12155591b213SSam Leffler 			, bs.bs_nexttbtt
12165591b213SSam Leffler 			, bs.bs_dtimperiod
12175591b213SSam Leffler 			, bs.bs_nextdtim
12185591b213SSam Leffler 			, bs.bs_bmissthreshold
12195591b213SSam Leffler 			, bs.bs_sleepduration
12205591b213SSam Leffler 		));
12215591b213SSam Leffler 		ath_hal_intrset(ah, 0);
12225591b213SSam Leffler 		/*
12235591b213SSam Leffler 		 * Reset our tsf so the hardware will update the
12245591b213SSam Leffler 		 * tsf register to reflect timestamps found in
12255591b213SSam Leffler 		 * received beacons.
12265591b213SSam Leffler 		 */
12275591b213SSam Leffler 		ath_hal_resettsf(ah);
12285591b213SSam Leffler 		ath_hal_beacontimers(ah, &bs, 0/*XXX*/, 0, 0);
12295591b213SSam Leffler 		sc->sc_imask |= HAL_INT_BMISS;
12305591b213SSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
12315591b213SSam Leffler 	} else {
12325591b213SSam Leffler 		DPRINTF(("%s: intval %u nexttbtt %u\n",
12335591b213SSam Leffler 			__func__, ni->ni_intval, nexttbtt));
12345591b213SSam Leffler 		ath_hal_intrset(ah, 0);
12355591b213SSam Leffler 		ath_hal_beaconinit(ah, ic->ic_opmode,
12365591b213SSam Leffler 			nexttbtt, ni->ni_intval);
12376b59f5e3SSam Leffler 		if (ic->ic_opmode != IEEE80211_M_MONITOR)
12385591b213SSam Leffler 			sc->sc_imask |= HAL_INT_SWBA;	/* beacon prepare */
12395591b213SSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
12405591b213SSam Leffler 	}
12415591b213SSam Leffler }
12425591b213SSam Leffler 
12435591b213SSam Leffler static void
12445591b213SSam Leffler ath_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
12455591b213SSam Leffler {
12465591b213SSam Leffler 	bus_addr_t *paddr = (bus_addr_t*) arg;
12475591b213SSam Leffler 	*paddr = segs->ds_addr;
12485591b213SSam Leffler }
12495591b213SSam Leffler 
12505591b213SSam Leffler static int
12515591b213SSam Leffler ath_desc_alloc(struct ath_softc *sc)
12525591b213SSam Leffler {
12535591b213SSam Leffler 	int i, bsize, error;
12545591b213SSam Leffler 	struct ath_desc *ds;
12555591b213SSam Leffler 	struct ath_buf *bf;
12565591b213SSam Leffler 
12575591b213SSam Leffler 	/* allocate descriptors */
12585591b213SSam Leffler 	sc->sc_desc_len = sizeof(struct ath_desc) *
12595591b213SSam Leffler 				(ATH_TXBUF * ATH_TXDESC + ATH_RXBUF + 1);
12605591b213SSam Leffler 	error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT, &sc->sc_ddmamap);
12615591b213SSam Leffler 	if (error != 0)
12625591b213SSam Leffler 		return error;
12635591b213SSam Leffler 
12645591b213SSam Leffler 	error = bus_dmamem_alloc(sc->sc_dmat, (void**) &sc->sc_desc,
12655591b213SSam Leffler 				 BUS_DMA_NOWAIT, &sc->sc_ddmamap);
12665591b213SSam Leffler 	if (error != 0)
12675591b213SSam Leffler 		goto fail0;
12685591b213SSam Leffler 
12695591b213SSam Leffler 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_ddmamap,
12705591b213SSam Leffler 				sc->sc_desc, sc->sc_desc_len,
12715591b213SSam Leffler 				ath_load_cb, &sc->sc_desc_paddr,
12725591b213SSam Leffler 				BUS_DMA_NOWAIT);
12735591b213SSam Leffler 	if (error != 0)
12745591b213SSam Leffler 		goto fail1;
12755591b213SSam Leffler 
12765591b213SSam Leffler 	ds = sc->sc_desc;
12775591b213SSam Leffler 	DPRINTF(("ath_desc_alloc: DMA map: %p (%d) -> %p (%lu)\n",
12785591b213SSam Leffler 	    ds, sc->sc_desc_len,
12795591b213SSam Leffler 	    (caddr_t) sc->sc_desc_paddr, /*XXX*/ (u_long) sc->sc_desc_len));
12805591b213SSam Leffler 
12815591b213SSam Leffler 	/* allocate buffers */
12825591b213SSam Leffler 	bsize = sizeof(struct ath_buf) * (ATH_TXBUF + ATH_RXBUF + 1);
12835591b213SSam Leffler 	bf = malloc(bsize, M_DEVBUF, M_NOWAIT | M_ZERO);
12845591b213SSam Leffler 	if (bf == NULL)
12855591b213SSam Leffler 		goto fail2;
12865591b213SSam Leffler 	sc->sc_bufptr = bf;
12875591b213SSam Leffler 
12885591b213SSam Leffler 	TAILQ_INIT(&sc->sc_rxbuf);
12895591b213SSam Leffler 	for (i = 0; i < ATH_RXBUF; i++, bf++, ds++) {
12905591b213SSam Leffler 		bf->bf_desc = ds;
12915591b213SSam Leffler 		bf->bf_daddr = sc->sc_desc_paddr +
12925591b213SSam Leffler 		    ((caddr_t)ds - (caddr_t)sc->sc_desc);
12935591b213SSam Leffler 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
12945591b213SSam Leffler 					  &bf->bf_dmamap);
12955591b213SSam Leffler 		if (error != 0)
12965591b213SSam Leffler 			break;
12975591b213SSam Leffler 		TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
12985591b213SSam Leffler 	}
12995591b213SSam Leffler 
13005591b213SSam Leffler 	TAILQ_INIT(&sc->sc_txbuf);
13015591b213SSam Leffler 	for (i = 0; i < ATH_TXBUF; i++, bf++, ds += ATH_TXDESC) {
13025591b213SSam Leffler 		bf->bf_desc = ds;
13035591b213SSam Leffler 		bf->bf_daddr = sc->sc_desc_paddr +
13045591b213SSam Leffler 		    ((caddr_t)ds - (caddr_t)sc->sc_desc);
13055591b213SSam Leffler 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
13065591b213SSam Leffler 					  &bf->bf_dmamap);
13075591b213SSam Leffler 		if (error != 0)
13085591b213SSam Leffler 			break;
13095591b213SSam Leffler 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
13105591b213SSam Leffler 	}
13115591b213SSam Leffler 	TAILQ_INIT(&sc->sc_txq);
13125591b213SSam Leffler 
13135591b213SSam Leffler 	/* beacon buffer */
13145591b213SSam Leffler 	bf->bf_desc = ds;
13155591b213SSam Leffler 	bf->bf_daddr = sc->sc_desc_paddr + ((caddr_t)ds - (caddr_t)sc->sc_desc);
13165591b213SSam Leffler 	error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT, &bf->bf_dmamap);
13175591b213SSam Leffler 	if (error != 0)
13185591b213SSam Leffler 		return error;
13195591b213SSam Leffler 	sc->sc_bcbuf = bf;
13205591b213SSam Leffler 	return 0;
13215591b213SSam Leffler 
13225591b213SSam Leffler fail2:
13235591b213SSam Leffler 	bus_dmamap_unload(sc->sc_dmat, sc->sc_ddmamap);
13245591b213SSam Leffler fail1:
13255591b213SSam Leffler 	bus_dmamem_free(sc->sc_dmat, sc->sc_desc, sc->sc_ddmamap);
13265591b213SSam Leffler fail0:
13275591b213SSam Leffler 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_ddmamap);
13285591b213SSam Leffler 	sc->sc_ddmamap = NULL;
13295591b213SSam Leffler 	return error;
13305591b213SSam Leffler }
13315591b213SSam Leffler 
13325591b213SSam Leffler static void
13335591b213SSam Leffler ath_desc_free(struct ath_softc *sc)
13345591b213SSam Leffler {
13355591b213SSam Leffler 	struct ath_buf *bf;
13365591b213SSam Leffler 
13375591b213SSam Leffler 	bus_dmamap_unload(sc->sc_dmat, sc->sc_ddmamap);
13385591b213SSam Leffler 	bus_dmamem_free(sc->sc_dmat, sc->sc_desc, sc->sc_ddmamap);
13395591b213SSam Leffler 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_ddmamap);
13405591b213SSam Leffler 
13415591b213SSam Leffler 	TAILQ_FOREACH(bf, &sc->sc_txq, bf_list) {
13425591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
13435591b213SSam Leffler 		bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
13445591b213SSam Leffler 		m_freem(bf->bf_m);
13455591b213SSam Leffler 	}
13465591b213SSam Leffler 	TAILQ_FOREACH(bf, &sc->sc_txbuf, bf_list)
13475591b213SSam Leffler 		bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
13485591b213SSam Leffler 	TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
13495591b213SSam Leffler 		if (bf->bf_m) {
13505591b213SSam Leffler 			bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
13515591b213SSam Leffler 			bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
13525591b213SSam Leffler 			m_freem(bf->bf_m);
13535591b213SSam Leffler 			bf->bf_m = NULL;
13545591b213SSam Leffler 		}
13555591b213SSam Leffler 	}
13565591b213SSam Leffler 	if (sc->sc_bcbuf != NULL) {
13575591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, sc->sc_bcbuf->bf_dmamap);
13585591b213SSam Leffler 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_bcbuf->bf_dmamap);
13595591b213SSam Leffler 		sc->sc_bcbuf = NULL;
13605591b213SSam Leffler 	}
13615591b213SSam Leffler 
13625591b213SSam Leffler 	TAILQ_INIT(&sc->sc_rxbuf);
13635591b213SSam Leffler 	TAILQ_INIT(&sc->sc_txbuf);
13645591b213SSam Leffler 	TAILQ_INIT(&sc->sc_txq);
13655591b213SSam Leffler 	free(sc->sc_bufptr, M_DEVBUF);
13665591b213SSam Leffler 	sc->sc_bufptr = NULL;
13675591b213SSam Leffler }
13685591b213SSam Leffler 
13695591b213SSam Leffler static struct ieee80211_node *
13705591b213SSam Leffler ath_node_alloc(struct ieee80211com *ic)
13715591b213SSam Leffler {
13725591b213SSam Leffler 	struct ath_node *an =
13735591b213SSam Leffler 		malloc(sizeof(struct ath_node), M_DEVBUF, M_NOWAIT | M_ZERO);
13745591b213SSam Leffler 	return an ? &an->st_node : NULL;
13755591b213SSam Leffler }
13765591b213SSam Leffler 
13775591b213SSam Leffler static void
13785591b213SSam Leffler ath_node_free(struct ieee80211com *ic, struct ieee80211_node *ni)
13795591b213SSam Leffler {
13805591b213SSam Leffler         struct ath_softc *sc = ic->ic_if.if_softc;
13815591b213SSam Leffler 	struct ath_buf *bf;
13825591b213SSam Leffler 
13835591b213SSam Leffler 	TAILQ_FOREACH(bf, &sc->sc_txq, bf_list) {
13845591b213SSam Leffler 		if (bf->bf_node == ni)
13855591b213SSam Leffler 			bf->bf_node = NULL;
13865591b213SSam Leffler 	}
13875591b213SSam Leffler 	free(ni, M_DEVBUF);
13885591b213SSam Leffler }
13895591b213SSam Leffler 
13905591b213SSam Leffler static void
13915591b213SSam Leffler ath_node_copy(struct ieee80211com *ic,
13925591b213SSam Leffler 	struct ieee80211_node *dst, const struct ieee80211_node *src)
13935591b213SSam Leffler {
13945591b213SSam Leffler 	*(struct ath_node *)dst = *(const struct ath_node *)src;
13955591b213SSam Leffler }
13965591b213SSam Leffler 
13975591b213SSam Leffler static int
13985591b213SSam Leffler ath_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf)
13995591b213SSam Leffler {
14005591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
14015591b213SSam Leffler 	int error;
14025591b213SSam Leffler 	struct mbuf *m;
14035591b213SSam Leffler 	struct ath_desc *ds;
14045591b213SSam Leffler 
14055591b213SSam Leffler 	m = bf->bf_m;
14065591b213SSam Leffler 	if (m == NULL) {
14075591b213SSam Leffler 		/*
14085591b213SSam Leffler 		 * NB: by assigning a page to the rx dma buffer we
14095591b213SSam Leffler 		 * implicitly satisfy the Atheros requirement that
14105591b213SSam Leffler 		 * this buffer be cache-line-aligned and sized to be
14115591b213SSam Leffler 		 * multiple of the cache line size.  Not doing this
14125591b213SSam Leffler 		 * causes weird stuff to happen (for the 5210 at least).
14135591b213SSam Leffler 		 */
14145591b213SSam Leffler 		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
14155591b213SSam Leffler 		if (m == NULL) {
14165591b213SSam Leffler 			DPRINTF(("ath_rxbuf_init: no mbuf/cluster\n"));
14175591b213SSam Leffler 			sc->sc_stats.ast_rx_nombuf++;
14185591b213SSam Leffler 			return ENOMEM;
14195591b213SSam Leffler 		}
14205591b213SSam Leffler 		bf->bf_m = m;
14215591b213SSam Leffler 		m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
14225591b213SSam Leffler 
14235591b213SSam Leffler 		error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m,
14245591b213SSam Leffler 					     ath_mbuf_load_cb, bf,
14255591b213SSam Leffler 					     BUS_DMA_NOWAIT);
14265591b213SSam Leffler 		if (error != 0) {
14275591b213SSam Leffler 			DPRINTF(("ath_rxbuf_init: bus_dmamap_load_mbuf failed;"
14285591b213SSam Leffler 				" error %d\n", error));
14295591b213SSam Leffler 			sc->sc_stats.ast_rx_busdma++;
14305591b213SSam Leffler 			return error;
14315591b213SSam Leffler 		}
14325591b213SSam Leffler 		KASSERT(bf->bf_nseg == 1,
14335591b213SSam Leffler 			("ath_rxbuf_init: multi-segment packet; nseg %u",
14345591b213SSam Leffler 			bf->bf_nseg));
14355591b213SSam Leffler 	}
14365591b213SSam Leffler 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREREAD);
14375591b213SSam Leffler 
14385591b213SSam Leffler 	/* setup descriptors */
14395591b213SSam Leffler 	ds = bf->bf_desc;
14405591b213SSam Leffler 	ds->ds_link = 0;
14415591b213SSam Leffler 	ds->ds_data = bf->bf_segs[0].ds_addr;
14425591b213SSam Leffler 	ath_hal_setuprxdesc(ah, ds
14435591b213SSam Leffler 		, m->m_len		/* buffer size */
14445591b213SSam Leffler 		, 0
14455591b213SSam Leffler 	);
14465591b213SSam Leffler 
14475591b213SSam Leffler 	if (sc->sc_rxlink != NULL)
14485591b213SSam Leffler 		*sc->sc_rxlink = bf->bf_daddr;
14495591b213SSam Leffler 	sc->sc_rxlink = &ds->ds_link;
14505591b213SSam Leffler 	return 0;
14515591b213SSam Leffler }
14525591b213SSam Leffler 
14535591b213SSam Leffler static void
14545591b213SSam Leffler ath_rx_proc(void *arg, int npending)
14555591b213SSam Leffler {
14565591b213SSam Leffler 	struct ath_softc *sc = arg;
14575591b213SSam Leffler 	struct ath_buf *bf;
14585591b213SSam Leffler 	struct ifnet *ifp = &sc->sc_ic.ic_if;
14595591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
14605591b213SSam Leffler 	struct ath_desc *ds;
14615591b213SSam Leffler 	struct mbuf *m;
14625591b213SSam Leffler 	struct ieee80211_frame *wh, whbuf;
14635591b213SSam Leffler 	int len;
14645591b213SSam Leffler 	u_int phyerr;
14655591b213SSam Leffler 	HAL_STATUS status;
14665591b213SSam Leffler 
14675591b213SSam Leffler 	DPRINTF2(("ath_rx_proc: pending %u\n", npending));
14685591b213SSam Leffler 	do {
14695591b213SSam Leffler 		bf = TAILQ_FIRST(&sc->sc_rxbuf);
14705591b213SSam Leffler 		if (bf == NULL) {		/* NB: shouldn't happen */
14715591b213SSam Leffler 			if_printf(ifp, "ath_rx_proc: no buffer!\n");
14725591b213SSam Leffler 			break;
14735591b213SSam Leffler 		}
14745591b213SSam Leffler 		m = bf->bf_m;
14755591b213SSam Leffler 		if (m == NULL) {		/* NB: shouldn't happen */
14765591b213SSam Leffler 			if_printf(ifp, "ath_rx_proc: no mbuf!\n");
14775591b213SSam Leffler 			continue;
14785591b213SSam Leffler 		}
14795591b213SSam Leffler 		ds = bf->bf_desc;
14805591b213SSam Leffler 		status = ath_hal_rxprocdesc(ah, ds);
14815591b213SSam Leffler #ifdef AR_DEBUG
14825591b213SSam Leffler 		if (ath_debug > 1)
14835591b213SSam Leffler 			ath_printrxbuf(bf, status == HAL_OK);
14845591b213SSam Leffler #endif
14855591b213SSam Leffler 		if (status == HAL_EINPROGRESS)
14865591b213SSam Leffler 			break;
14875591b213SSam Leffler 		TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
14885591b213SSam Leffler 		if (ds->ds_rxstat.rs_status != 0) {
14895591b213SSam Leffler 			ifp->if_ierrors++;
14905591b213SSam Leffler 			if (ds->ds_rxstat.rs_status & HAL_RXERR_CRC)
14915591b213SSam Leffler 				sc->sc_stats.ast_rx_crcerr++;
14925591b213SSam Leffler 			if (ds->ds_rxstat.rs_status & HAL_RXERR_FIFO)
14935591b213SSam Leffler 				sc->sc_stats.ast_rx_fifoerr++;
14945591b213SSam Leffler 			if (ds->ds_rxstat.rs_status & HAL_RXERR_DECRYPT)
14955591b213SSam Leffler 				sc->sc_stats.ast_rx_badcrypt++;
14965591b213SSam Leffler 			if (ds->ds_rxstat.rs_status & HAL_RXERR_PHY) {
14975591b213SSam Leffler 				sc->sc_stats.ast_rx_phyerr++;
14985591b213SSam Leffler 				phyerr = ds->ds_rxstat.rs_phyerr & 0x1f;
14995591b213SSam Leffler 				sc->sc_stats.ast_rx_phy[phyerr]++;
15005591b213SSam Leffler 			}
15015591b213SSam Leffler 			goto rx_next;
15025591b213SSam Leffler 		}
15035591b213SSam Leffler 
15045591b213SSam Leffler 		len = ds->ds_rxstat.rs_datalen;
15055591b213SSam Leffler 		if (len < sizeof(struct ieee80211_frame)) {
15065591b213SSam Leffler 			DPRINTF(("ath_rx_proc: short packet %d\n", len));
15075591b213SSam Leffler 			goto rx_next;
15085591b213SSam Leffler 		}
15095591b213SSam Leffler 
15105591b213SSam Leffler 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
15115591b213SSam Leffler 		    BUS_DMASYNC_POSTREAD);
15125591b213SSam Leffler 
15135591b213SSam Leffler 		wh = mtod(m, struct ieee80211_frame *);
15145591b213SSam Leffler 		if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
15155591b213SSam Leffler 		    IEEE80211_FC0_TYPE_CTL) {
15165591b213SSam Leffler 			/*
15175591b213SSam Leffler 			 * Ignore control frame received in promisc mode.
15185591b213SSam Leffler 			 */
15195591b213SSam Leffler 			DPRINTF(("ath_rx_proc: control frame\n"));
15205591b213SSam Leffler 			goto rx_next;
15215591b213SSam Leffler 		}
15225591b213SSam Leffler 
15235591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
15245591b213SSam Leffler 		bf->bf_m = NULL;
15255591b213SSam Leffler 		m->m_pkthdr.rcvif = ifp;
15265591b213SSam Leffler 		m->m_pkthdr.len = m->m_len = len;
15275591b213SSam Leffler 		if (IFF_DUMPPKTS(ifp)) {
15285591b213SSam Leffler 			ieee80211_dump_pkt(mtod(m, u_int8_t *), len,
15291b1a8e41SSam Leffler 				sc->sc_hwmap[ds->ds_rxstat.rs_rate] &
15301b1a8e41SSam Leffler 					IEEE80211_RATE_VAL,
15315591b213SSam Leffler 				ds->ds_rxstat.rs_rssi);
15325591b213SSam Leffler 		}
15335591b213SSam Leffler 		m_adj(m, -IEEE80211_CRC_LEN);
15345591b213SSam Leffler 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
15355591b213SSam Leffler 			/*
15365591b213SSam Leffler 			 * WEP is decrypted by hardware. Clear WEP bit
15375591b213SSam Leffler 			 * and trim WEP header for ieee80211_input().
15385591b213SSam Leffler 			 */
15395591b213SSam Leffler 			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
15405591b213SSam Leffler 			memcpy(&whbuf, wh, sizeof(whbuf));
15415591b213SSam Leffler 			m_adj(m, IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN);
15425591b213SSam Leffler 			memcpy(mtod(m, caddr_t), &whbuf, sizeof(whbuf));
15435591b213SSam Leffler 			/*
15445591b213SSam Leffler 			 * Also trim WEP ICV from the tail.
15455591b213SSam Leffler 			 */
15465591b213SSam Leffler 			m_adj(m, -IEEE80211_WEP_CRCLEN);
15475591b213SSam Leffler 		}
15485591b213SSam Leffler 		ieee80211_input(ifp, m,
15495591b213SSam Leffler 			ds->ds_rxstat.rs_rssi,
15505591b213SSam Leffler 			ds->ds_rxstat.rs_tstamp,
15515591b213SSam Leffler 			ds->ds_rxstat.rs_antenna);
15525591b213SSam Leffler   rx_next:
15535591b213SSam Leffler 		TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
15545591b213SSam Leffler 	} while (ath_rxbuf_init(sc, bf) == 0);
15555591b213SSam Leffler 
15565591b213SSam Leffler 	ath_hal_rxmonitor(ah);			/* rx signal state monitoring */
15575591b213SSam Leffler 	ath_hal_rxena(ah);			/* in case of RXEOL */
15585591b213SSam Leffler }
15595591b213SSam Leffler 
15605591b213SSam Leffler /*
15615591b213SSam Leffler  * XXX Size of an ACK control frame in bytes.
15625591b213SSam Leffler  */
15635591b213SSam Leffler #define	IEEE80211_ACK_SIZE	(2+2+IEEE80211_ADDR_LEN+4)
15645591b213SSam Leffler 
15655591b213SSam Leffler static int
15665591b213SSam Leffler ath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni, struct ath_buf *bf,
15675591b213SSam Leffler     struct mbuf *m0)
15685591b213SSam Leffler {
15695591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
15705591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
15715591b213SSam Leffler 	struct ifnet *ifp = &sc->sc_ic.ic_if;
15725591b213SSam Leffler 	int i, error, iswep, hdrlen, pktlen;
15735591b213SSam Leffler 	u_int8_t rix, cix, txrate, ctsrate;
15745591b213SSam Leffler 	struct ath_desc *ds;
15755591b213SSam Leffler 	struct mbuf *m;
15765591b213SSam Leffler 	struct ieee80211_frame *wh;
15775591b213SSam Leffler 	u_int32_t iv;
15785591b213SSam Leffler 	u_int8_t *ivp;
15795591b213SSam Leffler 	u_int8_t hdrbuf[sizeof(struct ieee80211_frame) +
15805591b213SSam Leffler 	    IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN];
15815591b213SSam Leffler 	u_int subtype, flags, ctsduration, antenna;
15825591b213SSam Leffler 	HAL_PKT_TYPE atype;
15835591b213SSam Leffler 	const HAL_RATE_TABLE *rt;
15845591b213SSam Leffler 	HAL_BOOL shortPreamble;
15855591b213SSam Leffler 	struct ath_node *an;
15865591b213SSam Leffler 
15875591b213SSam Leffler 	wh = mtod(m0, struct ieee80211_frame *);
15885591b213SSam Leffler 	iswep = wh->i_fc[1] & IEEE80211_FC1_WEP;
15895591b213SSam Leffler 	hdrlen = sizeof(struct ieee80211_frame);
15905591b213SSam Leffler 	pktlen = m0->m_pkthdr.len;
15915591b213SSam Leffler 
15925591b213SSam Leffler 	if (iswep) {
15935591b213SSam Leffler 		memcpy(hdrbuf, mtod(m0, caddr_t), hdrlen);
15945591b213SSam Leffler 		m_adj(m0, hdrlen);
15955591b213SSam Leffler 		M_PREPEND(m0, sizeof(hdrbuf), M_DONTWAIT);
15965591b213SSam Leffler 		if (m0 == NULL) {
15975591b213SSam Leffler 			sc->sc_stats.ast_tx_nombuf++;
15985591b213SSam Leffler 			return ENOMEM;
15995591b213SSam Leffler 		}
16005591b213SSam Leffler 		ivp = hdrbuf + hdrlen;
16015591b213SSam Leffler 		/*
16025591b213SSam Leffler 		 * XXX
16035591b213SSam Leffler 		 * IV must not duplicate during the lifetime of the key.
16045591b213SSam Leffler 		 * But no mechanism to renew keys is defined in IEEE 802.11
16055591b213SSam Leffler 		 * WEP.  And IV may be duplicated between other stations
16065591b213SSam Leffler 		 * because of the session key itself is shared.
16075591b213SSam Leffler 		 * So we use pseudo random IV for now, though it is not the
16085591b213SSam Leffler 		 * right way.
16095591b213SSam Leffler 		 */
16105591b213SSam Leffler 		iv = arc4random();
16115591b213SSam Leffler 		for (i = 0; i < IEEE80211_WEP_IVLEN; i++) {
16125591b213SSam Leffler 			ivp[i] = iv;
16135591b213SSam Leffler 			iv >>= 8;
16145591b213SSam Leffler 		}
16155591b213SSam Leffler 		ivp[i] = sc->sc_ic.ic_wep_txkey << 6;	/* Key ID and pad */
16165591b213SSam Leffler 		memcpy(mtod(m0, caddr_t), hdrbuf, sizeof(hdrbuf));
16175591b213SSam Leffler 		/*
16185591b213SSam Leffler 		 * The ICV length must be included into hdrlen and pktlen.
16195591b213SSam Leffler 		 */
16205591b213SSam Leffler 		hdrlen = sizeof(hdrbuf) + IEEE80211_WEP_CRCLEN;
16215591b213SSam Leffler 		pktlen = m0->m_pkthdr.len + IEEE80211_WEP_CRCLEN;
16225591b213SSam Leffler 	}
16235591b213SSam Leffler 	pktlen += IEEE80211_CRC_LEN;
16245591b213SSam Leffler 
16255591b213SSam Leffler 	/*
16265591b213SSam Leffler 	 * Load the DMA map so any coalescing is done.  This
16275591b213SSam Leffler 	 * also calculates the number of descriptors we need.
16285591b213SSam Leffler 	 */
16295591b213SSam Leffler 	error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m0,
16305591b213SSam Leffler 				     ath_mbuf_load_cb, bf,
16315591b213SSam Leffler 				     BUS_DMA_NOWAIT);
16325591b213SSam Leffler 	if (error != 0) {
16335591b213SSam Leffler 		sc->sc_stats.ast_tx_busdma++;
16345591b213SSam Leffler 		m_freem(m0);
16355591b213SSam Leffler 		return error;
16365591b213SSam Leffler 	}
16375591b213SSam Leffler 	/*
16385591b213SSam Leffler 	 * Discard null packets and check for packets that
16395591b213SSam Leffler 	 * require too many TX descriptors.  We try to convert
16405591b213SSam Leffler 	 * the latter to a cluster.
16415591b213SSam Leffler 	 */
16425591b213SSam Leffler 	if (bf->bf_nseg > ATH_TXDESC) {		/* too many desc's, linearize */
16435591b213SSam Leffler 		sc->sc_stats.ast_tx_linear++;
16445591b213SSam Leffler 		MGETHDR(m, M_DONTWAIT, MT_DATA);
16455591b213SSam Leffler 		if (m == NULL) {
16465591b213SSam Leffler 			sc->sc_stats.ast_tx_nombuf++;
16475591b213SSam Leffler 			m_freem(m0);
16485591b213SSam Leffler 			return ENOMEM;
16495591b213SSam Leffler 		}
16505591b213SSam Leffler 		M_MOVE_PKTHDR(m, m0);
16515591b213SSam Leffler 		MCLGET(m, M_DONTWAIT);
16525591b213SSam Leffler 		if ((m->m_flags & M_EXT) == 0) {
16535591b213SSam Leffler 			sc->sc_stats.ast_tx_nomcl++;
16545591b213SSam Leffler 			m_freem(m0);
16555591b213SSam Leffler 			m_free(m);
16565591b213SSam Leffler 			return ENOMEM;
16575591b213SSam Leffler 		}
16585591b213SSam Leffler 		m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
16595591b213SSam Leffler 		m_freem(m0);
16605591b213SSam Leffler 		m->m_len = m->m_pkthdr.len;
16615591b213SSam Leffler 		m0 = m;
16625591b213SSam Leffler 		error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m0,
16635591b213SSam Leffler 					     ath_mbuf_load_cb, bf,
16645591b213SSam Leffler 					     BUS_DMA_NOWAIT);
16655591b213SSam Leffler 		if (error != 0) {
16665591b213SSam Leffler 			sc->sc_stats.ast_tx_busdma++;
16675591b213SSam Leffler 			m_freem(m0);
16685591b213SSam Leffler 			return error;
16695591b213SSam Leffler 		}
16705591b213SSam Leffler 		KASSERT(bf->bf_nseg == 1,
16715591b213SSam Leffler 			("ath_tx_start: packet not one segment; nseg %u",
16725591b213SSam Leffler 			bf->bf_nseg));
16735591b213SSam Leffler 	} else if (bf->bf_nseg == 0) {		/* null packet, discard */
16745591b213SSam Leffler 		sc->sc_stats.ast_tx_nodata++;
16755591b213SSam Leffler 		m_freem(m0);
16765591b213SSam Leffler 		return EIO;
16775591b213SSam Leffler 	}
16785591b213SSam Leffler 	DPRINTF2(("ath_tx_start: m %p len %u\n", m0, pktlen));
16795591b213SSam Leffler 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
16805591b213SSam Leffler 	bf->bf_m = m0;
16815591b213SSam Leffler 	bf->bf_node = ni;
16825591b213SSam Leffler 
16835591b213SSam Leffler 	/* setup descriptors */
16845591b213SSam Leffler 	ds = bf->bf_desc;
16855591b213SSam Leffler 	rt = sc->sc_currates;
16865591b213SSam Leffler 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
16875591b213SSam Leffler 
16885591b213SSam Leffler 	/*
16895591b213SSam Leffler 	 * Calculate Atheros packet type from IEEE80211 packet header
16905591b213SSam Leffler 	 * and setup for rate calculations.
16915591b213SSam Leffler 	 */
16925591b213SSam Leffler 	atype = HAL_PKT_TYPE_NORMAL;			/* default */
16935591b213SSam Leffler 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
16945591b213SSam Leffler 	case IEEE80211_FC0_TYPE_MGT:
16955591b213SSam Leffler 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
16965591b213SSam Leffler 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON)
16975591b213SSam Leffler 			atype = HAL_PKT_TYPE_BEACON;
16985591b213SSam Leffler 		else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
16995591b213SSam Leffler 			atype = HAL_PKT_TYPE_PROBE_RESP;
17005591b213SSam Leffler 		else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM)
17015591b213SSam Leffler 			atype = HAL_PKT_TYPE_ATIM;
17025591b213SSam Leffler 		rix = 0;			/* XXX lowest rate */
17035591b213SSam Leffler 		break;
17045591b213SSam Leffler 	case IEEE80211_FC0_TYPE_CTL:
17055591b213SSam Leffler 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
17065591b213SSam Leffler 		if (subtype == IEEE80211_FC0_SUBTYPE_PS_POLL)
17075591b213SSam Leffler 			atype = HAL_PKT_TYPE_PSPOLL;
17085591b213SSam Leffler 		rix = 0;			/* XXX lowest rate */
17095591b213SSam Leffler 		break;
17105591b213SSam Leffler 	default:
17115591b213SSam Leffler 		rix = sc->sc_rixmap[ni->ni_rates.rs_rates[ni->ni_txrate] &
17125591b213SSam Leffler 				IEEE80211_RATE_VAL];
17135591b213SSam Leffler 		if (rix == 0xff) {
17145591b213SSam Leffler 			if_printf(ifp, "bogus xmit rate 0x%x\n",
17155591b213SSam Leffler 				ni->ni_rates.rs_rates[ni->ni_txrate]);
17165591b213SSam Leffler 			sc->sc_stats.ast_tx_badrate++;
17175591b213SSam Leffler 			m_freem(m0);
17185591b213SSam Leffler 			return EIO;
17195591b213SSam Leffler 		}
17205591b213SSam Leffler 		break;
17215591b213SSam Leffler 	}
17225591b213SSam Leffler 	/*
17235591b213SSam Leffler 	 * NB: the 802.11 layer marks whether or not we should
17245591b213SSam Leffler 	 * use short preamble based on the current mode and
17255591b213SSam Leffler 	 * negotiated parameters.
17265591b213SSam Leffler 	 */
17275591b213SSam Leffler 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) {
17285591b213SSam Leffler 		txrate = rt->info[rix].rateCode | rt->info[rix].shortPreamble;
17295591b213SSam Leffler 		shortPreamble = AH_TRUE;
17305591b213SSam Leffler 		sc->sc_stats.ast_tx_shortpre++;
17315591b213SSam Leffler 	} else {
17325591b213SSam Leffler 		txrate = rt->info[rix].rateCode;
17335591b213SSam Leffler 		shortPreamble = AH_FALSE;
17345591b213SSam Leffler 	}
17355591b213SSam Leffler 
17365591b213SSam Leffler 	/*
17375591b213SSam Leffler 	 * Calculate miscellaneous flags.
17385591b213SSam Leffler 	 */
17395591b213SSam Leffler 	flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for wep errors */
17405591b213SSam Leffler 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
17415591b213SSam Leffler 		flags |= HAL_TXDESC_NOACK;	/* no ack on broad/multicast */
17425591b213SSam Leffler 		sc->sc_stats.ast_tx_noack++;
17435591b213SSam Leffler 	} else if (pktlen > ic->ic_rtsthreshold) {
17445591b213SSam Leffler 		flags |= HAL_TXDESC_RTSENA;	/* RTS based on frame length */
17455591b213SSam Leffler 		sc->sc_stats.ast_tx_rts++;
17465591b213SSam Leffler 	}
17475591b213SSam Leffler 
17485591b213SSam Leffler 	/*
17495591b213SSam Leffler 	 * Calculate RTS/CTS rate and duration if needed.
17505591b213SSam Leffler 	 */
17515591b213SSam Leffler 	ctsduration = 0;
17525591b213SSam Leffler 	if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA)) {
17535591b213SSam Leffler 		/*
17545591b213SSam Leffler 		 * CTS transmit rate is derived from the transmit rate
17555591b213SSam Leffler 		 * by looking in the h/w rate table.  We must also factor
17565591b213SSam Leffler 		 * in whether or not a short preamble is to be used.
17575591b213SSam Leffler 		 */
17585591b213SSam Leffler 		cix = rt->info[rix].controlRate;
17595591b213SSam Leffler 		ctsrate = rt->info[cix].rateCode;
17605591b213SSam Leffler 		if (shortPreamble)
17615591b213SSam Leffler 			ctsrate |= rt->info[cix].shortPreamble;
17625591b213SSam Leffler 		/*
17635591b213SSam Leffler 		 * Compute the transmit duration based on the size
17645591b213SSam Leffler 		 * of an ACK frame.  We call into the HAL to do the
17655591b213SSam Leffler 		 * computation since it depends on the characteristics
17665591b213SSam Leffler 		 * of the actual PHY being used.
17675591b213SSam Leffler 		 */
17685591b213SSam Leffler 		if (flags & HAL_TXDESC_RTSENA) {	/* SIFS + CTS */
17695591b213SSam Leffler 			ctsduration += ath_hal_computetxtime(ah,
17705591b213SSam Leffler 				rt, IEEE80211_ACK_SIZE, cix, shortPreamble);
17715591b213SSam Leffler 		}
17725591b213SSam Leffler 		/* SIFS + data */
17735591b213SSam Leffler 		ctsduration += ath_hal_computetxtime(ah,
17745591b213SSam Leffler 			rt, pktlen, rix, shortPreamble);
17755591b213SSam Leffler 		if ((flags & HAL_TXDESC_NOACK) == 0) {	/* SIFS + ACK */
17765591b213SSam Leffler 			ctsduration += ath_hal_computetxtime(ah,
17775591b213SSam Leffler 				rt, IEEE80211_ACK_SIZE, cix, shortPreamble);
17785591b213SSam Leffler 		}
17795591b213SSam Leffler 	} else
17805591b213SSam Leffler 		ctsrate = 0;
17815591b213SSam Leffler 
17825591b213SSam Leffler 	/*
17835591b213SSam Leffler 	 * For now use the antenna on which the last good
17845591b213SSam Leffler 	 * frame was received on.  We assume this field is
17855591b213SSam Leffler 	 * initialized to 0 which gives us ``auto'' or the
17865591b213SSam Leffler 	 * ``default'' antenna.
17875591b213SSam Leffler 	 */
17885591b213SSam Leffler 	an = (struct ath_node *) ni;
17895591b213SSam Leffler 	if (an->an_tx_antenna)
17905591b213SSam Leffler 		antenna = an->an_tx_antenna;
17915591b213SSam Leffler 	else
17925591b213SSam Leffler 		antenna = ni->ni_rantenna;
17935591b213SSam Leffler 
17945591b213SSam Leffler 	/*
17955591b213SSam Leffler 	 * Formulate first tx descriptor with tx controls.
17965591b213SSam Leffler 	 */
17975591b213SSam Leffler 	/* XXX check return value? */
17985591b213SSam Leffler 	ath_hal_setuptxdesc(ah, ds
17995591b213SSam Leffler 		, pktlen		/* packet length */
18005591b213SSam Leffler 		, hdrlen		/* header length */
18015591b213SSam Leffler 		, atype			/* Atheros packet type */
18025591b213SSam Leffler 		, 60			/* txpower XXX */
18035591b213SSam Leffler 		, txrate, 1+10		/* series 0 rate/tries */
18045591b213SSam Leffler 		, iswep ? sc->sc_ic.ic_wep_txkey : HAL_TXKEYIX_INVALID
18055591b213SSam Leffler 		, antenna		/* antenna mode */
18065591b213SSam Leffler 		, flags			/* flags */
18075591b213SSam Leffler 		, ctsrate		/* rts/cts rate */
18085591b213SSam Leffler 		, ctsduration		/* rts/cts duration */
18095591b213SSam Leffler 	);
18105591b213SSam Leffler #ifdef notyet
18115591b213SSam Leffler 	ath_hal_setupxtxdesc(ah, ds
18125591b213SSam Leffler 		, AH_FALSE		/* short preamble */
18135591b213SSam Leffler 		, 0, 0			/* series 1 rate/tries */
18145591b213SSam Leffler 		, 0, 0			/* series 2 rate/tries */
18155591b213SSam Leffler 		, 0, 0			/* series 3 rate/tries */
18165591b213SSam Leffler 	);
18175591b213SSam Leffler #endif
18185591b213SSam Leffler 	/*
18195591b213SSam Leffler 	 * Fillin the remainder of the descriptor info.
18205591b213SSam Leffler 	 */
18215591b213SSam Leffler 	for (i = 0; i < bf->bf_nseg; i++, ds++) {
18225591b213SSam Leffler 		ds->ds_data = bf->bf_segs[i].ds_addr;
18235591b213SSam Leffler 		if (i == bf->bf_nseg - 1)
18245591b213SSam Leffler 			ds->ds_link = 0;
18255591b213SSam Leffler 		else
18265591b213SSam Leffler 			ds->ds_link = bf->bf_daddr + sizeof(*ds) * (i + 1);
18275591b213SSam Leffler 		ath_hal_filltxdesc(ah, ds
18285591b213SSam Leffler 			, bf->bf_segs[i].ds_len	/* segment length */
18295591b213SSam Leffler 			, i == 0		/* first segment */
18305591b213SSam Leffler 			, i == bf->bf_nseg - 1	/* last segment */
18315591b213SSam Leffler 		);
18325591b213SSam Leffler 		DPRINTF2(("ath_tx_start: %d: %08x %08x %08x %08x %08x %08x\n",
18335591b213SSam Leffler 		    i, ds->ds_link, ds->ds_data, ds->ds_ctl0, ds->ds_ctl1,
18345591b213SSam Leffler 		    ds->ds_hw[0], ds->ds_hw[1]));
18355591b213SSam Leffler 	}
18365591b213SSam Leffler 
18375591b213SSam Leffler 	/*
18385591b213SSam Leffler 	 * Insert the frame on the outbound list and
18395591b213SSam Leffler 	 * pass it on to the hardware.
18405591b213SSam Leffler 	 */
18415591b213SSam Leffler 	mtx_lock(&sc->sc_txqlock);
18425591b213SSam Leffler 	TAILQ_INSERT_TAIL(&sc->sc_txq, bf, bf_list);
18435591b213SSam Leffler 	if (sc->sc_txlink == NULL) {
18445591b213SSam Leffler 		ath_hal_puttxbuf(ah, sc->sc_txhalq, bf->bf_daddr);
18455591b213SSam Leffler 		DPRINTF2(("ath_tx_start: TXDP0 = %p (%p)\n",
18465591b213SSam Leffler 		    (caddr_t)bf->bf_daddr, bf->bf_desc));
18475591b213SSam Leffler 	} else {
18485591b213SSam Leffler 		*sc->sc_txlink = bf->bf_daddr;
18495591b213SSam Leffler 		DPRINTF2(("ath_tx_start: link(%p)=%p (%p)\n",
18505591b213SSam Leffler 		    sc->sc_txlink, (caddr_t)bf->bf_daddr, bf->bf_desc));
18515591b213SSam Leffler 	}
18525591b213SSam Leffler 	sc->sc_txlink = &bf->bf_desc[bf->bf_nseg - 1].ds_link;
18535591b213SSam Leffler 	mtx_unlock(&sc->sc_txqlock);
18545591b213SSam Leffler 
18555591b213SSam Leffler 	ath_hal_txstart(ah, sc->sc_txhalq);
18565591b213SSam Leffler 	return 0;
18575591b213SSam Leffler }
18585591b213SSam Leffler 
18595591b213SSam Leffler static void
18605591b213SSam Leffler ath_tx_proc(void *arg, int npending)
18615591b213SSam Leffler {
18625591b213SSam Leffler 	struct ath_softc *sc = arg;
18635591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
18645591b213SSam Leffler 	struct ath_buf *bf;
18655591b213SSam Leffler 	struct ifnet *ifp = &sc->sc_ic.ic_if;
18665591b213SSam Leffler 	struct ath_desc *ds;
18675591b213SSam Leffler 	struct ieee80211_node *ni;
18685591b213SSam Leffler 	struct ath_node *an;
18695591b213SSam Leffler 	int sr, lr;
18705591b213SSam Leffler 	HAL_STATUS status;
18715591b213SSam Leffler 
18725591b213SSam Leffler 	DPRINTF2(("ath_tx_proc: pending %u tx queue %p, link %p\n",
18735591b213SSam Leffler 		npending, (caddr_t) ath_hal_gettxbuf(sc->sc_ah, sc->sc_txhalq),
18745591b213SSam Leffler 		sc->sc_txlink));
18755591b213SSam Leffler 	for (;;) {
18765591b213SSam Leffler 		mtx_lock(&sc->sc_txqlock);
18775591b213SSam Leffler 		bf = TAILQ_FIRST(&sc->sc_txq);
18785591b213SSam Leffler 		if (bf == NULL) {
18795591b213SSam Leffler 			sc->sc_txlink = NULL;
18805591b213SSam Leffler 			mtx_unlock(&sc->sc_txqlock);
18815591b213SSam Leffler 			break;
18825591b213SSam Leffler 		}
18835591b213SSam Leffler 		/* only the last descriptor is needed */
18845591b213SSam Leffler 		ds = &bf->bf_desc[bf->bf_nseg - 1];
18855591b213SSam Leffler 		status = ath_hal_txprocdesc(ah, ds);
18865591b213SSam Leffler #ifdef AR_DEBUG
18875591b213SSam Leffler 		if (ath_debug > 1)
18885591b213SSam Leffler 			ath_printtxbuf(bf, status == HAL_OK);
18895591b213SSam Leffler #endif
18905591b213SSam Leffler 		if (status == HAL_EINPROGRESS) {
18915591b213SSam Leffler 			mtx_unlock(&sc->sc_txqlock);
18925591b213SSam Leffler 			break;
18935591b213SSam Leffler 		}
18945591b213SSam Leffler 		TAILQ_REMOVE(&sc->sc_txq, bf, bf_list);
18955591b213SSam Leffler 		mtx_unlock(&sc->sc_txqlock);
18965591b213SSam Leffler 
18975591b213SSam Leffler 		ni = bf->bf_node;
18985591b213SSam Leffler 		if (ni != NULL) {
18995591b213SSam Leffler 			an = (struct ath_node *) ni;
19005591b213SSam Leffler 			if (ds->ds_txstat.ts_status == 0) {
19015591b213SSam Leffler 				an->an_tx_ok++;
19025591b213SSam Leffler 				an->an_tx_antenna = ds->ds_txstat.ts_antenna;
19035591b213SSam Leffler 			} else {
19045591b213SSam Leffler 				an->an_tx_err++;
19055591b213SSam Leffler 				ifp->if_oerrors++;
19065591b213SSam Leffler 				if (ds->ds_txstat.ts_status & HAL_TXERR_XRETRY)
19075591b213SSam Leffler 					sc->sc_stats.ast_tx_xretries++;
19085591b213SSam Leffler 				if (ds->ds_txstat.ts_status & HAL_TXERR_FIFO)
19095591b213SSam Leffler 					sc->sc_stats.ast_tx_fifoerr++;
19105591b213SSam Leffler 				if (ds->ds_txstat.ts_status & HAL_TXERR_FILT)
19115591b213SSam Leffler 					sc->sc_stats.ast_tx_filtered++;
19125591b213SSam Leffler 				an->an_tx_antenna = 0;	/* invalidate */
19135591b213SSam Leffler 			}
19145591b213SSam Leffler 			sr = ds->ds_txstat.ts_shortretry;
19155591b213SSam Leffler 			lr = ds->ds_txstat.ts_longretry;
19165591b213SSam Leffler 			sc->sc_stats.ast_tx_shortretry += sr;
19175591b213SSam Leffler 			sc->sc_stats.ast_tx_longretry += lr;
19185591b213SSam Leffler 			if (sr + lr)
19195591b213SSam Leffler 				an->an_tx_retr++;
19205591b213SSam Leffler 		}
19215591b213SSam Leffler 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
19225591b213SSam Leffler 		    BUS_DMASYNC_POSTWRITE);
19235591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
19245591b213SSam Leffler 		m_freem(bf->bf_m);
19255591b213SSam Leffler 		bf->bf_m = NULL;
19265591b213SSam Leffler 		bf->bf_node = NULL;
19275591b213SSam Leffler 
19285591b213SSam Leffler 		mtx_lock(&sc->sc_txbuflock);
19295591b213SSam Leffler 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
19305591b213SSam Leffler 		mtx_unlock(&sc->sc_txbuflock);
19315591b213SSam Leffler 	}
19325591b213SSam Leffler 	ifp->if_flags &= ~IFF_OACTIVE;
19335591b213SSam Leffler 	sc->sc_tx_timer = 0;
19345591b213SSam Leffler 
19355591b213SSam Leffler 	ath_start(ifp);
19365591b213SSam Leffler }
19375591b213SSam Leffler 
19385591b213SSam Leffler /*
19395591b213SSam Leffler  * Drain the transmit queue and reclaim resources.
19405591b213SSam Leffler  */
19415591b213SSam Leffler static void
19425591b213SSam Leffler ath_draintxq(struct ath_softc *sc)
19435591b213SSam Leffler {
19445591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
19455591b213SSam Leffler 	struct ifnet *ifp = &sc->sc_ic.ic_if;
19465591b213SSam Leffler 	struct ath_buf *bf;
19475591b213SSam Leffler 
19485591b213SSam Leffler 	/* XXX return value */
19495591b213SSam Leffler 	if (!sc->sc_invalid) {
19505591b213SSam Leffler 		/* don't touch the hardware if marked invalid */
19515591b213SSam Leffler 		(void) ath_hal_stoptxdma(ah, sc->sc_txhalq);
19525591b213SSam Leffler 		DPRINTF(("ath_draintxq: tx queue %p, link %p\n",
19535591b213SSam Leffler 		    (caddr_t) ath_hal_gettxbuf(ah, sc->sc_txhalq),
19545591b213SSam Leffler 		    sc->sc_txlink));
19555591b213SSam Leffler 		(void) ath_hal_stoptxdma(ah, sc->sc_bhalq);
19565591b213SSam Leffler 		DPRINTF(("ath_draintxq: beacon queue %p\n",
19575591b213SSam Leffler 		    (caddr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq)));
19585591b213SSam Leffler 	}
19595591b213SSam Leffler 	for (;;) {
19605591b213SSam Leffler 		mtx_lock(&sc->sc_txqlock);
19615591b213SSam Leffler 		bf = TAILQ_FIRST(&sc->sc_txq);
19625591b213SSam Leffler 		if (bf == NULL) {
19635591b213SSam Leffler 			sc->sc_txlink = NULL;
19645591b213SSam Leffler 			mtx_unlock(&sc->sc_txqlock);
19655591b213SSam Leffler 			break;
19665591b213SSam Leffler 		}
19675591b213SSam Leffler 		TAILQ_REMOVE(&sc->sc_txq, bf, bf_list);
19685591b213SSam Leffler 		mtx_unlock(&sc->sc_txqlock);
19695591b213SSam Leffler #ifdef AR_DEBUG
19705591b213SSam Leffler 		if (ath_debug)
19715591b213SSam Leffler 			ath_printtxbuf(bf,
19725591b213SSam Leffler 				ath_hal_txprocdesc(ah, bf->bf_desc) == HAL_OK);
19735591b213SSam Leffler #endif /* AR_DEBUG */
19745591b213SSam Leffler 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
19755591b213SSam Leffler 		m_freem(bf->bf_m);
19765591b213SSam Leffler 		bf->bf_m = NULL;
19775591b213SSam Leffler 		bf->bf_node = NULL;
19785591b213SSam Leffler 		mtx_lock(&sc->sc_txbuflock);
19795591b213SSam Leffler 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
19805591b213SSam Leffler 		mtx_unlock(&sc->sc_txbuflock);
19815591b213SSam Leffler 	}
19825591b213SSam Leffler 	ifp->if_flags &= ~IFF_OACTIVE;
19835591b213SSam Leffler 	sc->sc_tx_timer = 0;
19845591b213SSam Leffler }
19855591b213SSam Leffler 
19865591b213SSam Leffler /*
19875591b213SSam Leffler  * Disable the receive h/w in preparation for a reset.
19885591b213SSam Leffler  */
19895591b213SSam Leffler static void
19905591b213SSam Leffler ath_stoprecv(struct ath_softc *sc)
19915591b213SSam Leffler {
19925591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
19935591b213SSam Leffler 
19945591b213SSam Leffler 	ath_hal_stoppcurecv(ah);	/* disable PCU */
19955591b213SSam Leffler 	ath_hal_setrxfilter(ah, 0);	/* clear recv filter */
19965591b213SSam Leffler 	ath_hal_stopdmarecv(ah);	/* disable DMA engine */
19975591b213SSam Leffler 	DELAY(3000);			/* long enough for 1 frame */
19985591b213SSam Leffler #ifdef AR_DEBUG
19995591b213SSam Leffler 	if (ath_debug) {
20005591b213SSam Leffler 		struct ath_buf *bf;
20015591b213SSam Leffler 
20025591b213SSam Leffler 		DPRINTF(("ath_stoprecv: rx queue %p, link %p\n",
20035591b213SSam Leffler 		    (caddr_t) ath_hal_getrxbuf(ah), sc->sc_rxlink));
20045591b213SSam Leffler 		TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
20055591b213SSam Leffler 			if (ath_hal_rxprocdesc(ah, bf->bf_desc) == HAL_OK)
20065591b213SSam Leffler 				ath_printrxbuf(bf, 1);
20075591b213SSam Leffler 		}
20085591b213SSam Leffler 	}
20095591b213SSam Leffler #endif
20105591b213SSam Leffler 	sc->sc_rxlink = NULL;		/* just in case */
20115591b213SSam Leffler }
20125591b213SSam Leffler 
20135591b213SSam Leffler /*
20145591b213SSam Leffler  * Enable the receive h/w following a reset.
20155591b213SSam Leffler  */
20165591b213SSam Leffler static int
20175591b213SSam Leffler ath_startrecv(struct ath_softc *sc)
20185591b213SSam Leffler {
20195591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
20205591b213SSam Leffler 	struct ath_buf *bf;
20215591b213SSam Leffler 
20225591b213SSam Leffler 	sc->sc_rxlink = NULL;
20235591b213SSam Leffler 	TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
20245591b213SSam Leffler 		int error = ath_rxbuf_init(sc, bf);
20255591b213SSam Leffler 		if (error != 0) {
20265591b213SSam Leffler 			DPRINTF(("ath_startrecv: ath_rxbuf_init failed %d\n",
20275591b213SSam Leffler 				error));
20285591b213SSam Leffler 			return error;
20295591b213SSam Leffler 		}
20305591b213SSam Leffler 	}
20315591b213SSam Leffler 
20325591b213SSam Leffler 	bf = TAILQ_FIRST(&sc->sc_rxbuf);
20335591b213SSam Leffler 	ath_hal_putrxbuf(ah, bf->bf_daddr);
20345591b213SSam Leffler 	ath_hal_rxena(ah);		/* enable recv descriptors */
20355591b213SSam Leffler 	ath_mode_init(sc);		/* set filters, etc. */
20365591b213SSam Leffler 	ath_hal_startpcurecv(ah);	/* re-enable PCU/DMA engine */
20375591b213SSam Leffler 	return 0;
20385591b213SSam Leffler }
20395591b213SSam Leffler 
20405591b213SSam Leffler /*
20415591b213SSam Leffler  * Set/change channels.  If the channel is really being changed,
20425591b213SSam Leffler  * it's done by resetting the chip.  To accomplish this we must
20435591b213SSam Leffler  * first cleanup any pending DMA, then restart stuff after a la
20445591b213SSam Leffler  * ath_init.
20455591b213SSam Leffler  */
20465591b213SSam Leffler static int
20475591b213SSam Leffler ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan)
20485591b213SSam Leffler {
20495591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
20505591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
20515591b213SSam Leffler 
20525591b213SSam Leffler 	DPRINTF(("ath_chan_set: %u (%u MHz) -> %u (%u MHz)\n",
20535591b213SSam Leffler 	    ieee80211_chan2ieee(ic, ic->ic_ibss_chan),
20545591b213SSam Leffler 		ic->ic_ibss_chan->ic_freq,
20555591b213SSam Leffler 	    ieee80211_chan2ieee(ic, chan), chan->ic_freq));
20565591b213SSam Leffler 	if (chan != ic->ic_ibss_chan) {
20575591b213SSam Leffler 		HAL_STATUS status;
20585591b213SSam Leffler 		HAL_CHANNEL hchan;
20595591b213SSam Leffler 		enum ieee80211_phymode mode;
20605591b213SSam Leffler 
20615591b213SSam Leffler 		/*
20625591b213SSam Leffler 		 * To switch channels clear any pending DMA operations;
20635591b213SSam Leffler 		 * wait long enough for the RX fifo to drain, reset the
20645591b213SSam Leffler 		 * hardware at the new frequency, and then re-enable
20655591b213SSam Leffler 		 * the relevant bits of the h/w.
20665591b213SSam Leffler 		 */
20675591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
20685591b213SSam Leffler 		ath_draintxq(sc);		/* clear pending tx frames */
20695591b213SSam Leffler 		ath_stoprecv(sc);		/* turn off frame recv */
20705591b213SSam Leffler 		/*
20715591b213SSam Leffler 		 * Convert to a HAL channel description with
20725591b213SSam Leffler 		 * the flags constrained to reflect the current
20735591b213SSam Leffler 		 * operating mode.
20745591b213SSam Leffler 		 */
20755591b213SSam Leffler 		hchan.channel = chan->ic_freq;
20765591b213SSam Leffler 		hchan.channelFlags = ath_chan2flags(ic, chan);
20775591b213SSam Leffler 		if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_TRUE, &status)) {
20785591b213SSam Leffler 			if_printf(&ic->ic_if, "ath_chan_set: unable to reset "
20795591b213SSam Leffler 				"channel %u (%u Mhz)\n",
20805591b213SSam Leffler 				ieee80211_chan2ieee(ic, chan), chan->ic_freq);
20815591b213SSam Leffler 			return EIO;
20825591b213SSam Leffler 		}
20835591b213SSam Leffler 		/*
20845591b213SSam Leffler 		 * Re-enable rx framework.
20855591b213SSam Leffler 		 */
20865591b213SSam Leffler 		if (ath_startrecv(sc) != 0) {
20875591b213SSam Leffler 			if_printf(&ic->ic_if,
20885591b213SSam Leffler 				"ath_chan_set: unable to restart recv logic\n");
20895591b213SSam Leffler 			return EIO;
20905591b213SSam Leffler 		}
20915591b213SSam Leffler 
20925591b213SSam Leffler 		/*
20935591b213SSam Leffler 		 * Re-enable interrupts.
20945591b213SSam Leffler 		 */
20955591b213SSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
20965591b213SSam Leffler 
20975591b213SSam Leffler 		/*
20985591b213SSam Leffler 		 * Change channels and update the h/w rate map
20995591b213SSam Leffler 		 * if we're switching; e.g. 11a to 11b/g.
21005591b213SSam Leffler 		 */
21015591b213SSam Leffler 		ic->ic_ibss_chan = chan;
21025591b213SSam Leffler 		mode = ieee80211_chan2mode(ic, chan);
21035591b213SSam Leffler 		if (mode != sc->sc_curmode)
21045591b213SSam Leffler 			ath_setcurmode(sc, mode);
21055591b213SSam Leffler 	}
21065591b213SSam Leffler 	return 0;
21075591b213SSam Leffler }
21085591b213SSam Leffler 
21095591b213SSam Leffler static void
21105591b213SSam Leffler ath_next_scan(void *arg)
21115591b213SSam Leffler {
21125591b213SSam Leffler 	struct ath_softc *sc = arg;
21135591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
21145591b213SSam Leffler 	struct ifnet *ifp = &ic->ic_if;
21155591b213SSam Leffler 
21165591b213SSam Leffler 	if (ic->ic_state == IEEE80211_S_SCAN)
21175591b213SSam Leffler 		ieee80211_next_scan(ifp);
21185591b213SSam Leffler }
21195591b213SSam Leffler 
21205591b213SSam Leffler /*
21215591b213SSam Leffler  * Periodically recalibrate the PHY to account
21225591b213SSam Leffler  * for temperature/environment changes.
21235591b213SSam Leffler  */
21245591b213SSam Leffler static void
21255591b213SSam Leffler ath_calibrate(void *arg)
21265591b213SSam Leffler {
21275591b213SSam Leffler 	struct ath_softc *sc = arg;
21285591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
21295591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
21305591b213SSam Leffler 	struct ieee80211_channel *c;
21315591b213SSam Leffler 	HAL_CHANNEL hchan;
21325591b213SSam Leffler 
21335591b213SSam Leffler 	sc->sc_stats.ast_per_cal++;
21345591b213SSam Leffler 
21355591b213SSam Leffler 	/*
21365591b213SSam Leffler 	 * Convert to a HAL channel description with the flags
21375591b213SSam Leffler 	 * constrained to reflect the current operating mode.
21385591b213SSam Leffler 	 */
21395591b213SSam Leffler 	c = ic->ic_ibss_chan;
21405591b213SSam Leffler 	hchan.channel = c->ic_freq;
21415591b213SSam Leffler 	hchan.channelFlags = ath_chan2flags(ic, c);
21425591b213SSam Leffler 
21435591b213SSam Leffler 	DPRINTF(("%s: channel %u/%x\n", __func__, c->ic_freq, c->ic_flags));
21445591b213SSam Leffler 
21455591b213SSam Leffler 	if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) {
21465591b213SSam Leffler 		/*
21475591b213SSam Leffler 		 * Rfgain is out of bounds, reset the chip
21485591b213SSam Leffler 		 * to load new gain values.
21495591b213SSam Leffler 		 */
21505591b213SSam Leffler 		sc->sc_stats.ast_per_rfgain++;
21515591b213SSam Leffler 		ath_reset(sc);
21525591b213SSam Leffler 	}
21535591b213SSam Leffler 	if (!ath_hal_calibrate(ah, &hchan)) {
21545591b213SSam Leffler 		DPRINTF(("%s: calibration of channel %u failed\n",
21555591b213SSam Leffler 			__func__, c->ic_freq));
21565591b213SSam Leffler 		sc->sc_stats.ast_per_calfail++;
21575591b213SSam Leffler 	}
21585591b213SSam Leffler 	callout_reset(&sc->sc_cal_ch, hz * ath_calinterval, ath_calibrate, sc);
21595591b213SSam Leffler }
21605591b213SSam Leffler 
21615591b213SSam Leffler static int
216245bbf62fSSam Leffler ath_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
21635591b213SSam Leffler {
21645591b213SSam Leffler 	struct ifnet *ifp = &ic->ic_if;
216545bbf62fSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
216645bbf62fSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
21675591b213SSam Leffler 	struct ieee80211_node *ni;
21685591b213SSam Leffler 	int i, error;
21695591b213SSam Leffler 	u_int8_t *bssid;
21705591b213SSam Leffler 	u_int32_t rfilt;
21715591b213SSam Leffler 	static const HAL_LED_STATE leds[] = {
21725591b213SSam Leffler 	    HAL_LED_INIT,	/* IEEE80211_S_INIT */
21735591b213SSam Leffler 	    HAL_LED_SCAN,	/* IEEE80211_S_SCAN */
21745591b213SSam Leffler 	    HAL_LED_AUTH,	/* IEEE80211_S_AUTH */
21755591b213SSam Leffler 	    HAL_LED_ASSOC, 	/* IEEE80211_S_ASSOC */
21765591b213SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_RUN */
21775591b213SSam Leffler 	};
21785591b213SSam Leffler 
217945bbf62fSSam Leffler 	DPRINTF(("%s: %s -> %s\n", __func__,
218045bbf62fSSam Leffler 		ieee80211_state_name[ic->ic_state],
218145bbf62fSSam Leffler 		ieee80211_state_name[nstate]));
21825591b213SSam Leffler 
21835591b213SSam Leffler 	ath_hal_setledstate(ah, leds[nstate]);	/* set LED */
21845591b213SSam Leffler 
21855591b213SSam Leffler 	if (nstate == IEEE80211_S_INIT) {
21865591b213SSam Leffler 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
21875591b213SSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
218845bbf62fSSam Leffler 		callout_stop(&sc->sc_scan_ch);
218945bbf62fSSam Leffler 		callout_stop(&sc->sc_cal_ch);
219045bbf62fSSam Leffler 		return (*sc->sc_newstate)(ic, nstate, arg);
21915591b213SSam Leffler 	}
21925591b213SSam Leffler 	ni = ic->ic_bss;
21935591b213SSam Leffler 	error = ath_chan_set(sc, ni->ni_chan);
21945591b213SSam Leffler 	if (error != 0)
21955591b213SSam Leffler 		goto bad;
21965591b213SSam Leffler 	rfilt = (ath_hal_getrxfilter(ah) & HAL_RX_FILTER_PHYERR)
21975591b213SSam Leffler 	      | HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST;
21985591b213SSam Leffler 	if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
21995591b213SSam Leffler 	    (ifp->if_flags & IFF_PROMISC))
22005591b213SSam Leffler 		rfilt |= HAL_RX_FILTER_PROM;
22015591b213SSam Leffler 	if (nstate == IEEE80211_S_SCAN) {
22025591b213SSam Leffler 		callout_reset(&sc->sc_scan_ch, (hz * ath_dwelltime) / 1000,
22035591b213SSam Leffler 			ath_next_scan, sc);
22045591b213SSam Leffler 		bssid = ifp->if_broadcastaddr;
22055591b213SSam Leffler 		rfilt |= HAL_RX_FILTER_BEACON;
22065591b213SSam Leffler 	} else {
22075591b213SSam Leffler 		callout_stop(&sc->sc_scan_ch);
22085591b213SSam Leffler 		bssid = ni->ni_bssid;
22095591b213SSam Leffler 	}
22105591b213SSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
22115591b213SSam Leffler 	DPRINTF(("%s: RX filter 0x%x bssid %s\n",
22125591b213SSam Leffler 		 __func__, rfilt, ether_sprintf(bssid)));
22135591b213SSam Leffler 
22145591b213SSam Leffler 	if (nstate == IEEE80211_S_RUN && ic->ic_opmode == IEEE80211_M_STA)
22155591b213SSam Leffler 		ath_hal_setassocid(ah, bssid, ni->ni_associd);
22165591b213SSam Leffler 	else
22175591b213SSam Leffler 		ath_hal_setassocid(ah, bssid, 0);
22185591b213SSam Leffler 	if (ic->ic_flags & IEEE80211_F_WEPON) {
22195591b213SSam Leffler 		for (i = 0; i < IEEE80211_WEP_NKID; i++)
22205591b213SSam Leffler 			if (ath_hal_keyisvalid(ah, i))
22215591b213SSam Leffler 				ath_hal_keysetmac(ah, i, bssid);
22225591b213SSam Leffler 	}
22235591b213SSam Leffler 
22245591b213SSam Leffler 	if (nstate == IEEE80211_S_RUN) {
22255591b213SSam Leffler 		DPRINTF(("%s(RUN): ic_flags=0x%08x iv=%d bssid=%s "
22265591b213SSam Leffler 			"capinfo=0x%04x chan=%d\n"
22275591b213SSam Leffler 			 , __func__
22285591b213SSam Leffler 			 , ic->ic_flags
22295591b213SSam Leffler 			 , ni->ni_intval
22305591b213SSam Leffler 			 , ether_sprintf(ni->ni_bssid)
22315591b213SSam Leffler 			 , ni->ni_capinfo
22325591b213SSam Leffler 			 , ieee80211_chan2ieee(ic, ni->ni_chan)));
22335591b213SSam Leffler 
22345591b213SSam Leffler 		/*
22355591b213SSam Leffler 		 * Allocate and setup the beacon frame for AP or adhoc mode.
22365591b213SSam Leffler 		 */
22376b59f5e3SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
22386b59f5e3SSam Leffler 		    ic->ic_opmode == IEEE80211_M_IBSS) {
22395591b213SSam Leffler 			error = ath_beacon_alloc(sc, ni);
22405591b213SSam Leffler 			if (error != 0)
22415591b213SSam Leffler 				goto bad;
22425591b213SSam Leffler 		}
22435591b213SSam Leffler 
22445591b213SSam Leffler 		/*
22455591b213SSam Leffler 		 * Configure the beacon and sleep timers.
22465591b213SSam Leffler 		 */
22475591b213SSam Leffler 		ath_beacon_config(sc);
22485591b213SSam Leffler 
22495591b213SSam Leffler 		/* start periodic recalibration timer */
22505591b213SSam Leffler 		callout_reset(&sc->sc_cal_ch, hz * ath_calinterval,
22515591b213SSam Leffler 			ath_calibrate, sc);
22525591b213SSam Leffler 	} else {
22535591b213SSam Leffler 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
22545591b213SSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
22555591b213SSam Leffler 		callout_stop(&sc->sc_cal_ch);		/* no calibration */
22565591b213SSam Leffler 	}
22575591b213SSam Leffler 	/*
22585591b213SSam Leffler 	 * Reset the rate control state.
22595591b213SSam Leffler 	 */
22605591b213SSam Leffler 	ath_rate_ctl_reset(sc, nstate);
226145bbf62fSSam Leffler 	/*
226245bbf62fSSam Leffler 	 * Invoke the parent method to complete the work.
226345bbf62fSSam Leffler 	 */
226445bbf62fSSam Leffler 	return (*sc->sc_newstate)(ic, nstate, arg);
22655591b213SSam Leffler bad:
22665591b213SSam Leffler 	callout_stop(&sc->sc_scan_ch);
22675591b213SSam Leffler 	callout_stop(&sc->sc_cal_ch);
226845bbf62fSSam Leffler 	/* NB: do not invoke the parent */
22695591b213SSam Leffler 	return error;
22705591b213SSam Leffler }
22715591b213SSam Leffler 
22725591b213SSam Leffler /*
22735591b213SSam Leffler  * Setup driver-specific state for a newly associated node.
22745591b213SSam Leffler  * Note that we're called also on a re-associate, the isnew
22755591b213SSam Leffler  * param tells us if this is the first time or not.
22765591b213SSam Leffler  */
22775591b213SSam Leffler static void
22785591b213SSam Leffler ath_newassoc(struct ieee80211com *ic, struct ieee80211_node *ni, int isnew)
22795591b213SSam Leffler {
22805591b213SSam Leffler 	if (isnew) {
22815591b213SSam Leffler 		struct ath_node *an = (struct ath_node *) ni;
22825591b213SSam Leffler 
22835591b213SSam Leffler 		an->an_tx_ok = an->an_tx_err =
22845591b213SSam Leffler 			an->an_tx_retr = an->an_tx_upper = 0;
22855591b213SSam Leffler 		/* start with highest negotiated rate */
22865591b213SSam Leffler 		/*
22875591b213SSam Leffler 		 * XXX should do otherwise but only when
22885591b213SSam Leffler 		 * the rate control algorithm is better.
22895591b213SSam Leffler 		 */
22905591b213SSam Leffler 		KASSERT(ni->ni_rates.rs_nrates > 0,
22915591b213SSam Leffler 			("new association w/ no rates!"));
22925591b213SSam Leffler 		ni->ni_txrate = ni->ni_rates.rs_nrates - 1;
22935591b213SSam Leffler 	}
22945591b213SSam Leffler }
22955591b213SSam Leffler 
22965591b213SSam Leffler static int
22975591b213SSam Leffler ath_getchannels(struct ath_softc *sc, u_int cc, HAL_BOOL outdoor)
22985591b213SSam Leffler {
22995591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
23005591b213SSam Leffler 	struct ifnet *ifp = &ic->ic_if;
23015591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
23025591b213SSam Leffler 	HAL_CHANNEL *chans;
23035591b213SSam Leffler 	int i, ix, nchan;
23045591b213SSam Leffler 
23055591b213SSam Leffler 	sc->sc_have11g = 0;
23065591b213SSam Leffler 	chans = malloc(IEEE80211_CHAN_MAX * sizeof(HAL_CHANNEL),
23075591b213SSam Leffler 			M_TEMP, M_NOWAIT);
23085591b213SSam Leffler 	if (chans == NULL) {
23095591b213SSam Leffler 		if_printf(ifp, "unable to allocate channel table\n");
23105591b213SSam Leffler 		return ENOMEM;
23115591b213SSam Leffler 	}
23125591b213SSam Leffler 	if (!ath_hal_init_channels(ah, chans, IEEE80211_CHAN_MAX, &nchan,
231345cabbdcSSam Leffler 	    cc, HAL_MODE_ALL, outdoor)) {
23145591b213SSam Leffler 		if_printf(ifp, "unable to collect channel list from hal\n");
23155591b213SSam Leffler 		free(chans, M_TEMP);
23165591b213SSam Leffler 		return EINVAL;
23175591b213SSam Leffler 	}
23185591b213SSam Leffler 
23195591b213SSam Leffler 	/*
23205591b213SSam Leffler 	 * Convert HAL channels to ieee80211 ones and insert
23215591b213SSam Leffler 	 * them in the table according to their channel number.
23225591b213SSam Leffler 	 */
23235591b213SSam Leffler 	for (i = 0; i < nchan; i++) {
23245591b213SSam Leffler 		HAL_CHANNEL *c = &chans[i];
23255591b213SSam Leffler 		ix = ath_hal_mhz2ieee(c->channel, c->channelFlags);
23265591b213SSam Leffler 		if (ix > IEEE80211_CHAN_MAX) {
23275591b213SSam Leffler 			if_printf(ifp, "bad hal channel %u (%u/%x) ignored\n",
23285591b213SSam Leffler 				ix, c->channel, c->channelFlags);
23295591b213SSam Leffler 			continue;
23305591b213SSam Leffler 		}
23315591b213SSam Leffler 		/* NB: flags are known to be compatible */
23325591b213SSam Leffler 		if (ic->ic_channels[ix].ic_freq == 0) {
23335591b213SSam Leffler 			ic->ic_channels[ix].ic_freq = c->channel;
23345591b213SSam Leffler 			ic->ic_channels[ix].ic_flags = c->channelFlags;
23355591b213SSam Leffler 		} else {
23365591b213SSam Leffler 			/* channels overlap; e.g. 11g and 11b */
23375591b213SSam Leffler 			ic->ic_channels[ix].ic_flags |= c->channelFlags;
23385591b213SSam Leffler 		}
23395591b213SSam Leffler 		if ((c->channelFlags & CHANNEL_G) == CHANNEL_G)
23405591b213SSam Leffler 			sc->sc_have11g = 1;
23415591b213SSam Leffler 	}
23425591b213SSam Leffler 	free(chans, M_TEMP);
23435591b213SSam Leffler 	return 0;
23445591b213SSam Leffler }
23455591b213SSam Leffler 
23465591b213SSam Leffler static int
23475591b213SSam Leffler ath_rate_setup(struct ath_softc *sc, u_int mode)
23485591b213SSam Leffler {
23495591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
23505591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
23515591b213SSam Leffler 	const HAL_RATE_TABLE *rt;
23525591b213SSam Leffler 	struct ieee80211_rateset *rs;
23535591b213SSam Leffler 	int i, maxrates;
23545591b213SSam Leffler 
23555591b213SSam Leffler 	switch (mode) {
23565591b213SSam Leffler 	case IEEE80211_MODE_11A:
23575591b213SSam Leffler 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11A);
23585591b213SSam Leffler 		break;
23595591b213SSam Leffler 	case IEEE80211_MODE_11B:
23605591b213SSam Leffler 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11B);
23615591b213SSam Leffler 		break;
23625591b213SSam Leffler 	case IEEE80211_MODE_11G:
23635591b213SSam Leffler 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11G);
23645591b213SSam Leffler 		break;
23655591b213SSam Leffler 	case IEEE80211_MODE_TURBO:
23665591b213SSam Leffler 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_TURBO);
23675591b213SSam Leffler 		break;
23685591b213SSam Leffler 	default:
23695591b213SSam Leffler 		DPRINTF(("%s: invalid mode %u\n", __func__, mode));
23705591b213SSam Leffler 		return 0;
23715591b213SSam Leffler 	}
23725591b213SSam Leffler 	rt = sc->sc_rates[mode];
23735591b213SSam Leffler 	if (rt == NULL)
23745591b213SSam Leffler 		return 0;
23755591b213SSam Leffler 	if (rt->rateCount > IEEE80211_RATE_MAXSIZE) {
23765591b213SSam Leffler 		DPRINTF(("%s: rate table too small (%u > %u)\n",
23775591b213SSam Leffler 			__func__, rt->rateCount, IEEE80211_RATE_MAXSIZE));
23785591b213SSam Leffler 		maxrates = IEEE80211_RATE_MAXSIZE;
23795591b213SSam Leffler 	} else
23805591b213SSam Leffler 		maxrates = rt->rateCount;
23815591b213SSam Leffler 	rs = &ic->ic_sup_rates[mode];
23825591b213SSam Leffler 	for (i = 0; i < maxrates; i++)
23835591b213SSam Leffler 		rs->rs_rates[i] = rt->info[i].dot11Rate;
23845591b213SSam Leffler 	rs->rs_nrates = maxrates;
23855591b213SSam Leffler 	return 1;
23865591b213SSam Leffler }
23875591b213SSam Leffler 
23885591b213SSam Leffler static void
23895591b213SSam Leffler ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode)
23905591b213SSam Leffler {
23915591b213SSam Leffler 	const HAL_RATE_TABLE *rt;
23925591b213SSam Leffler 	int i;
23935591b213SSam Leffler 
23945591b213SSam Leffler 	memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap));
23955591b213SSam Leffler 	rt = sc->sc_rates[mode];
23965591b213SSam Leffler 	KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode));
23975591b213SSam Leffler 	for (i = 0; i < rt->rateCount; i++)
23985591b213SSam Leffler 		sc->sc_rixmap[rt->info[i].dot11Rate & IEEE80211_RATE_VAL] = i;
23991b1a8e41SSam Leffler 	memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap));
24001b1a8e41SSam Leffler 	for (i = 0; i < 32; i++)
24011b1a8e41SSam Leffler 		sc->sc_hwmap[i] = rt->info[rt->rateCodeToIndex[i]].dot11Rate;
24025591b213SSam Leffler 	sc->sc_currates = rt;
24035591b213SSam Leffler 	sc->sc_curmode = mode;
24045591b213SSam Leffler }
24055591b213SSam Leffler 
24065591b213SSam Leffler /*
24075591b213SSam Leffler  * Reset the rate control state for each 802.11 state transition.
24085591b213SSam Leffler  */
24095591b213SSam Leffler static void
24105591b213SSam Leffler ath_rate_ctl_reset(struct ath_softc *sc, enum ieee80211_state state)
24115591b213SSam Leffler {
24125591b213SSam Leffler 	struct ieee80211com *ic = &sc->sc_ic;
24135591b213SSam Leffler 	struct ieee80211_node *ni;
24145591b213SSam Leffler 	struct ath_node *an;
24155591b213SSam Leffler 
24165591b213SSam Leffler 	an = (struct ath_node *) ic->ic_bss;
24175591b213SSam Leffler 	an->an_tx_ok = an->an_tx_err = an->an_tx_retr = an->an_tx_upper = 0;
24185591b213SSam Leffler 	if (ic->ic_opmode == IEEE80211_M_STA) {
24195591b213SSam Leffler 		ni = ic->ic_bss;
24205591b213SSam Leffler 		if (state == IEEE80211_S_RUN) {
24215591b213SSam Leffler 			/* start with highest negotiated rate */
24225591b213SSam Leffler 			KASSERT(ni->ni_rates.rs_nrates > 0,
24235591b213SSam Leffler 				("transition to RUN state w/ no rates!"));
24245591b213SSam Leffler 			ni->ni_txrate = ni->ni_rates.rs_nrates - 1;
24255591b213SSam Leffler 		} else {
24265591b213SSam Leffler 			/* use lowest rate */
24275591b213SSam Leffler 			ni->ni_txrate = 0;
24285591b213SSam Leffler 		}
24295591b213SSam Leffler 	} else {
24305591b213SSam Leffler 		TAILQ_FOREACH(ni, &ic->ic_node, ni_list) {
24315591b213SSam Leffler 			ni->ni_txrate = 0;		/* use lowest rate */
24325591b213SSam Leffler 			an = (struct ath_node *) ni;
24335591b213SSam Leffler 			an->an_tx_ok = an->an_tx_err = an->an_tx_retr =
24345591b213SSam Leffler 			    an->an_tx_upper = 0;
24355591b213SSam Leffler 		}
24365591b213SSam Leffler 	}
24375591b213SSam Leffler }
24385591b213SSam Leffler 
24395591b213SSam Leffler /*
24405591b213SSam Leffler  * Examine and potentially adjust the transmit rate.
24415591b213SSam Leffler  */
24425591b213SSam Leffler static void
24435591b213SSam Leffler ath_rate_ctl(void *arg, struct ieee80211_node *ni)
24445591b213SSam Leffler {
24455591b213SSam Leffler 	struct ath_softc *sc = arg;
24465591b213SSam Leffler 	struct ath_node *an = (struct ath_node *) ni;
24475591b213SSam Leffler 	struct ieee80211_rateset *rs = &ni->ni_rates;
24485591b213SSam Leffler 	int mod = 0, orate, enough;
24495591b213SSam Leffler 
24505591b213SSam Leffler 	/*
24515591b213SSam Leffler 	 * Rate control
24525591b213SSam Leffler 	 * XXX: very primitive version.
24535591b213SSam Leffler 	 */
24545591b213SSam Leffler 	sc->sc_stats.ast_rate_calls++;
24555591b213SSam Leffler 
24565591b213SSam Leffler 	enough = (an->an_tx_ok + an->an_tx_err >= 10);
24575591b213SSam Leffler 
24585591b213SSam Leffler 	/* no packet reached -> down */
24595591b213SSam Leffler 	if (an->an_tx_err > 0 && an->an_tx_ok == 0)
24605591b213SSam Leffler 		mod = -1;
24615591b213SSam Leffler 
24625591b213SSam Leffler 	/* all packets needs retry in average -> down */
24635591b213SSam Leffler 	if (enough && an->an_tx_ok < an->an_tx_retr)
24645591b213SSam Leffler 		mod = -1;
24655591b213SSam Leffler 
24665591b213SSam Leffler 	/* no error and less than 10% of packets needs retry -> up */
24675591b213SSam Leffler 	if (enough && an->an_tx_err == 0 && an->an_tx_ok > an->an_tx_retr * 10)
24685591b213SSam Leffler 		mod = 1;
24695591b213SSam Leffler 
24705591b213SSam Leffler 	orate = ni->ni_txrate;
24715591b213SSam Leffler 	switch (mod) {
24725591b213SSam Leffler 	case 0:
24735591b213SSam Leffler 		if (enough && an->an_tx_upper > 0)
24745591b213SSam Leffler 			an->an_tx_upper--;
24755591b213SSam Leffler 		break;
24765591b213SSam Leffler 	case -1:
24775591b213SSam Leffler 		if (ni->ni_txrate > 0) {
24785591b213SSam Leffler 			ni->ni_txrate--;
24795591b213SSam Leffler 			sc->sc_stats.ast_rate_drop++;
24805591b213SSam Leffler 		}
24815591b213SSam Leffler 		an->an_tx_upper = 0;
24825591b213SSam Leffler 		break;
24835591b213SSam Leffler 	case 1:
24845591b213SSam Leffler 		if (++an->an_tx_upper < 2)
24855591b213SSam Leffler 			break;
24865591b213SSam Leffler 		an->an_tx_upper = 0;
24875591b213SSam Leffler 		if (ni->ni_txrate + 1 < rs->rs_nrates) {
24885591b213SSam Leffler 			ni->ni_txrate++;
24895591b213SSam Leffler 			sc->sc_stats.ast_rate_raise++;
24905591b213SSam Leffler 		}
24915591b213SSam Leffler 		break;
24925591b213SSam Leffler 	}
24935591b213SSam Leffler 
24945591b213SSam Leffler 	if (ni->ni_txrate != orate) {
24955591b213SSam Leffler 		printf("%s: %dM -> %dM (%d ok, %d err, %d retr)\n",
24965591b213SSam Leffler 		    __func__,
24975591b213SSam Leffler 		    (rs->rs_rates[orate] & IEEE80211_RATE_VAL) / 2,
24985591b213SSam Leffler 		    (rs->rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL) / 2,
24995591b213SSam Leffler 		    an->an_tx_ok, an->an_tx_err, an->an_tx_retr);
25005591b213SSam Leffler 	}
25015591b213SSam Leffler 	if (ni->ni_txrate != orate || enough)
25025591b213SSam Leffler 		an->an_tx_ok = an->an_tx_err = an->an_tx_retr = 0;
25035591b213SSam Leffler }
25045591b213SSam Leffler 
25055591b213SSam Leffler #ifdef AR_DEBUG
25065591b213SSam Leffler static int
25075591b213SSam Leffler sysctl_hw_ath_dump(SYSCTL_HANDLER_ARGS)
25085591b213SSam Leffler {
25095591b213SSam Leffler 	char dmode[64];
25105591b213SSam Leffler 	int error;
25115591b213SSam Leffler 
25125591b213SSam Leffler 	strncpy(dmode, "", sizeof(dmode) - 1);
25135591b213SSam Leffler 	dmode[sizeof(dmode) - 1] = '\0';
25145591b213SSam Leffler 	error = sysctl_handle_string(oidp, &dmode[0], sizeof(dmode), req);
25155591b213SSam Leffler 
25165591b213SSam Leffler 	if (error == 0 && req->newptr != NULL) {
25175591b213SSam Leffler 		struct ifnet *ifp;
25185591b213SSam Leffler 		struct ath_softc *sc;
25195591b213SSam Leffler 
25205591b213SSam Leffler 		ifp = ifunit("ath0");		/* XXX */
25215591b213SSam Leffler 		if (!ifp)
25225591b213SSam Leffler 			return EINVAL;
25235591b213SSam Leffler 		sc = ifp->if_softc;
25245591b213SSam Leffler 		if (strcmp(dmode, "hal") == 0)
25255591b213SSam Leffler 			ath_hal_dumpstate(sc->sc_ah);
25265591b213SSam Leffler 		else if (strcmp(dmode, "eeprom") == 0)
25275591b213SSam Leffler 			ath_hal_dumpeeprom(sc->sc_ah);
25285591b213SSam Leffler 		else if (strcmp(dmode, "rfgain") == 0)
25295591b213SSam Leffler 			ath_hal_dumprfgain(sc->sc_ah);
25305591b213SSam Leffler 		else if (strcmp(dmode, "ani") == 0)
25315591b213SSam Leffler 			ath_hal_dumpani(sc->sc_ah);
25325591b213SSam Leffler 		else
25335591b213SSam Leffler 			return EINVAL;
25345591b213SSam Leffler 	}
25355591b213SSam Leffler 	return error;
25365591b213SSam Leffler }
25375591b213SSam Leffler SYSCTL_PROC(_hw_ath, OID_AUTO, dump, CTLTYPE_STRING | CTLFLAG_RW,
25385591b213SSam Leffler 	0, 0, sysctl_hw_ath_dump, "A", "Dump driver state");
25395591b213SSam Leffler 
25405591b213SSam Leffler static void
25415591b213SSam Leffler ath_printrxbuf(struct ath_buf *bf, int done)
25425591b213SSam Leffler {
25435591b213SSam Leffler 	struct ath_desc *ds;
25445591b213SSam Leffler 	int i;
25455591b213SSam Leffler 
25465591b213SSam Leffler 	for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) {
25475591b213SSam Leffler 		printf("R%d (%p %p) %08x %08x %08x %08x %08x %08x %c\n",
25485591b213SSam Leffler 		    i, ds, (struct ath_desc *)bf->bf_daddr + i,
25495591b213SSam Leffler 		    ds->ds_link, ds->ds_data,
25505591b213SSam Leffler 		    ds->ds_ctl0, ds->ds_ctl1,
25515591b213SSam Leffler 		    ds->ds_hw[0], ds->ds_hw[1],
25525591b213SSam Leffler 		    !done ? ' ' : (ds->ds_rxstat.rs_status == 0) ? '*' : '!');
25535591b213SSam Leffler 	}
25545591b213SSam Leffler }
25555591b213SSam Leffler 
25565591b213SSam Leffler static void
25575591b213SSam Leffler ath_printtxbuf(struct ath_buf *bf, int done)
25585591b213SSam Leffler {
25595591b213SSam Leffler 	struct ath_desc *ds;
25605591b213SSam Leffler 	int i;
25615591b213SSam Leffler 
25625591b213SSam Leffler 	for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) {
25635591b213SSam Leffler 		printf("T%d (%p %p) %08x %08x %08x %08x %08x %08x %08x %08x %c\n",
25645591b213SSam Leffler 		    i, ds, (struct ath_desc *)bf->bf_daddr + i,
25655591b213SSam Leffler 		    ds->ds_link, ds->ds_data,
25665591b213SSam Leffler 		    ds->ds_ctl0, ds->ds_ctl1,
25675591b213SSam Leffler 		    ds->ds_hw[0], ds->ds_hw[1], ds->ds_hw[2], ds->ds_hw[3],
25685591b213SSam Leffler 		    !done ? ' ' : (ds->ds_txstat.ts_status == 0) ? '*' : '!');
25695591b213SSam Leffler 	}
25705591b213SSam Leffler }
25715591b213SSam Leffler #endif /* AR_DEBUG */
2572