Home | History | Annotate | Line # | Download | only in ic
ath.c revision 1.15
      1 /*	$NetBSD: ath.c,v 1.15 2003/11/02 11:07:45 wiz Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer,
     12  *    without modification.
     13  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     14  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
     15  *    redistribution must be conditioned upon including a substantially
     16  *    similar Disclaimer requirement for further binary redistribution.
     17  * 3. Neither the names of the above-listed copyright holders nor the names
     18  *    of any contributors may be used to endorse or promote products derived
     19  *    from this software without specific prior written permission.
     20  *
     21  * Alternatively, this software may be distributed under the terms of the
     22  * GNU General Public License ("GPL") version 2 as published by the Free
     23  * Software Foundation.
     24  *
     25  * NO WARRANTY
     26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     28  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
     29  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
     30  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
     31  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
     34  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     36  * THE POSSIBILITY OF SUCH DAMAGES.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #ifdef __FreeBSD__
     41 __FBSDID("$FreeBSD: src/sys/dev/ath/if_ath.c,v 1.14 2003/09/05 22:22:49 sam Exp $");
     42 #endif
     43 #ifdef __NetBSD__
     44 __KERNEL_RCSID(0, "$NetBSD: ath.c,v 1.15 2003/11/02 11:07:45 wiz Exp $");
     45 #endif
     46 
     47 /*
     48  * Driver for the Atheros Wireless LAN controller.
     49  *
     50  * This software is derived from work of Atsushi Onoe; his contribution
     51  * is greatly appreciated.
     52  */
     53 
     54 #include "opt_inet.h"
     55 
     56 #ifdef __NetBSD__
     57 #include "bpfilter.h"
     58 #endif /* __NetBSD__ */
     59 
     60 #include <sys/param.h>
     61 #include <sys/systm.h>
     62 #include <sys/types.h>
     63 #include <sys/sysctl.h>
     64 #include <sys/mbuf.h>
     65 #include <sys/malloc.h>
     66 #include <sys/lock.h>
     67 #ifdef __FreeBSD__
     68 #include <sys/mutex.h>
     69 #endif
     70 #include <sys/kernel.h>
     71 #include <sys/socket.h>
     72 #include <sys/sockio.h>
     73 #include <sys/errno.h>
     74 #include <sys/callout.h>
     75 #ifdef __FreeBSD__
     76 #include <sys/bus.h>
     77 #else
     78 #include <machine/bus.h>
     79 #endif
     80 #include <sys/endian.h>
     81 
     82 #include <machine/bus.h>
     83 
     84 #include <net/if.h>
     85 #include <net/if_dl.h>
     86 #include <net/if_media.h>
     87 #include <net/if_arp.h>
     88 #ifdef __FreeBSD__
     89 #include <net/ethernet.h>
     90 #else
     91 #include <net/if_ether.h>
     92 #endif
     93 #include <net/if_llc.h>
     94 
     95 #include <net80211/ieee80211_var.h>
     96 #include <net80211/ieee80211_compat.h>
     97 
     98 #if NBPFILTER > 0
     99 #include <net/bpf.h>
    100 #endif
    101 
    102 #ifdef INET
    103 #include <netinet/in.h>
    104 #endif
    105 
    106 #include <dev/ic/athcompat.h>
    107 
    108 #define	AR_DEBUG
    109 #ifdef __FreeBSD__
    110 #include <dev/ath/if_athvar.h>
    111 #include <contrib/dev/ath/ah_desc.h>
    112 #else
    113 #include <dev/ic/athvar.h>
    114 #include <../contrib/sys/dev/ic/athhal_desc.h>
    115 #endif
    116 
    117 /* unaligned little endian access */
    118 #define LE_READ_2(p)							\
    119 	((u_int16_t)							\
    120 	 ((((u_int8_t *)(p))[0]      ) | (((u_int8_t *)(p))[1] <<  8)))
    121 #define LE_READ_4(p)							\
    122 	((u_int32_t)							\
    123 	 ((((u_int8_t *)(p))[0]      ) | (((u_int8_t *)(p))[1] <<  8) |	\
    124 	  (((u_int8_t *)(p))[2] << 16) | (((u_int8_t *)(p))[3] << 24)))
    125 
    126 #ifdef __FreeBSD__
    127 static void	ath_init(void *);
    128 #else
    129 static int	ath_init(struct ifnet *);
    130 #endif
    131 static int	ath_init1(struct ath_softc *);
    132 static int	ath_intr1(struct ath_softc *);
    133 static void	ath_stop(struct ifnet *);
    134 static void	ath_start(struct ifnet *);
    135 static void	ath_reset(struct ath_softc *);
    136 static int	ath_media_change(struct ifnet *);
    137 static void	ath_watchdog(struct ifnet *);
    138 static int	ath_ioctl(struct ifnet *, u_long, caddr_t);
    139 static void	ath_fatal_proc(void *, int);
    140 static void	ath_rxorn_proc(void *, int);
    141 static void	ath_bmiss_proc(void *, int);
    142 static void	ath_initkeytable(struct ath_softc *);
    143 static void	ath_mode_init(struct ath_softc *);
    144 static int	ath_beacon_alloc(struct ath_softc *, struct ieee80211_node *);
    145 static void	ath_beacon_proc(void *, int);
    146 static void	ath_beacon_free(struct ath_softc *);
    147 static void	ath_beacon_config(struct ath_softc *);
    148 static int	ath_desc_alloc(struct ath_softc *);
    149 static void	ath_desc_free(struct ath_softc *);
    150 static struct ieee80211_node *ath_node_alloc(struct ieee80211com *);
    151 static void	ath_node_free(struct ieee80211com *, struct ieee80211_node *);
    152 static void	ath_node_copy(struct ieee80211com *,
    153 			struct ieee80211_node *, const struct ieee80211_node *);
    154 static int	ath_rxbuf_init(struct ath_softc *, struct ath_buf *);
    155 static void	ath_rx_proc(void *, int);
    156 static int	ath_tx_start(struct ath_softc *, struct ieee80211_node *,
    157 			     struct ath_buf *, struct mbuf *);
    158 static void	ath_tx_proc(void *, int);
    159 static int	ath_chan_set(struct ath_softc *, struct ieee80211_channel *);
    160 static void	ath_draintxq(struct ath_softc *);
    161 static void	ath_stoprecv(struct ath_softc *);
    162 static int	ath_startrecv(struct ath_softc *);
    163 static void	ath_next_scan(void *);
    164 static void	ath_calibrate(void *);
    165 static int	ath_newstate(struct ieee80211com *, enum ieee80211_state, int);
    166 static void	ath_newassoc(struct ieee80211com *,
    167 			struct ieee80211_node *, int);
    168 static int	ath_getchannels(struct ath_softc *, u_int cc, HAL_BOOL outdoor);
    169 
    170 static int	ath_rate_setup(struct ath_softc *sc, u_int mode);
    171 static void	ath_setcurmode(struct ath_softc *, enum ieee80211_phymode);
    172 static void	ath_rate_ctl_reset(struct ath_softc *, enum ieee80211_state);
    173 static void	ath_rate_ctl(void *, struct ieee80211_node *);
    174 
    175 #ifdef __NetBSD__
    176 int	ath_enable(struct ath_softc *);
    177 void	ath_disable(struct ath_softc *);
    178 void	ath_power(int, void *);
    179 #endif
    180 
    181 #ifdef __FreeBSD__
    182 SYSCTL_DECL(_hw_ath);
    183 /* XXX validate sysctl values */
    184 SYSCTL_INT(_hw_ath, OID_AUTO, dwell, CTLFLAG_RW, &ath_dwelltime,
    185 	    0, "channel dwell time (ms) for AP/station scanning");
    186 SYSCTL_INT(_hw_ath, OID_AUTO, calibrate, CTLFLAG_RW, &ath_calinterval,
    187 	    0, "chip calibration interval (secs)");
    188 SYSCTL_INT(_hw_ath, OID_AUTO, outdoor, CTLFLAG_RD, &ath_outdoor,
    189 	    0, "enable/disable outdoor operation");
    190 SYSCTL_INT(_hw_ath, OID_AUTO, countrycode, CTLFLAG_RD, &ath_countrycode,
    191 	    0, "country code");
    192 SYSCTL_INT(_hw_ath, OID_AUTO, regdomain, CTLFLAG_RD, &ath_regdomain,
    193 	    0, "regulatory domain");
    194 #endif /* __FreeBSD__ */
    195 
    196 static	int ath_dwelltime = 200;		/* 5 channels/second */
    197 static	int ath_calinterval = 30;		/* calibrate every 30 secs */
    198 static	int ath_outdoor = AH_TRUE;		/* outdoor operation */
    199 static	int ath_countrycode = CTRY_DEFAULT;	/* country code */
    200 static	int ath_regdomain = 0;			/* regulatory domain */
    201 
    202 #ifdef AR_DEBUG
    203 int	ath_debug = 0;
    204 #ifdef __FreeBSD__
    205 SYSCTL_INT(_hw_ath, OID_AUTO, debug, CTLFLAG_RW, &ath_debug,
    206 	    0, "control debugging printfs");
    207 #endif /* __FreeBSD__ */
    208 #define	IFF_DUMPPKTS(_ifp) \
    209 	(ath_debug || \
    210 	    ((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
    211 static	void ath_printrxbuf(struct ath_buf *bf, int);
    212 static	void ath_printtxbuf(struct ath_buf *bf, int);
    213 #define	DPRINTF(X)	if (ath_debug) printf X
    214 #define	DPRINTF2(X)	if (ath_debug > 1) printf X
    215 #else
    216 #define	IFF_DUMPPKTS(_ifp) \
    217 	(((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
    218 #define	DPRINTF(X)
    219 #define	DPRINTF2(X)
    220 #endif
    221 
    222 #ifdef __NetBSD__
    223 int
    224 ath_activate(struct device *self, enum devact act)
    225 {
    226 	struct ath_softc *sc = (struct ath_softc *)self;
    227 	int rv = 0, s;
    228 
    229 	s = splnet();
    230 	switch (act) {
    231 	case DVACT_ACTIVATE:
    232 		rv = EOPNOTSUPP;
    233 		break;
    234 	case DVACT_DEACTIVATE:
    235 		if_deactivate(&sc->sc_ic.ic_if);
    236 		break;
    237 	}
    238 	splx(s);
    239 	return rv;
    240 }
    241 
    242 int
    243 ath_enable(struct ath_softc *sc)
    244 {
    245 	if (ATH_IS_ENABLED(sc) == 0) {
    246 		if (sc->sc_enable != NULL && (*sc->sc_enable)(sc) != 0) {
    247 			printf("%s: device enable failed\n",
    248 				sc->sc_dev.dv_xname);
    249 			return (EIO);
    250 		}
    251 		sc->sc_flags |= ATH_ENABLED;
    252 	}
    253 	return (0);
    254 }
    255 
    256 void
    257 ath_disable(struct ath_softc *sc)
    258 {
    259 	if (!ATH_IS_ENABLED(sc))
    260 		return;
    261 	if (sc->sc_disable != NULL)
    262 		(*sc->sc_disable)(sc);
    263 	sc->sc_flags &= ~ATH_ENABLED;
    264 }
    265 #endif	/* #ifdef __NetBSD__ */
    266 
    267 int
    268 ath_attach(u_int16_t devid, struct ath_softc *sc)
    269 {
    270 	struct ieee80211com *ic = &sc->sc_ic;
    271 	struct ifnet *ifp = &ic->ic_if;
    272 	struct ath_hal *ah;
    273 	HAL_STATUS status;
    274 	int error = 0;
    275 
    276 	DPRINTF(("ath_attach: devid 0x%x\n", devid));
    277 
    278 #ifdef __FreeBSD__
    279 	/* set these up early for if_printf use */
    280 	ifp->if_unit = device_get_unit(sc->sc_dev);
    281 	ifp->if_name = "ath";
    282 #else
    283 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
    284 #endif
    285 
    286 	ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh, &status);
    287 	if (ah == NULL) {
    288 		if_printf(ifp, "unable to attach hardware; HAL status %u\n",
    289 			status);
    290 		error = ENXIO;
    291 		goto bad;
    292 	}
    293 	sc->sc_ah = ah;
    294 	sc->sc_invalid = 0;	/* ready to go, enable interrupt handling */
    295 
    296 	/*
    297 	 * Collect the channel list using the default country
    298 	 * code and including outdoor channels.  The 802.11 layer
    299 	 * is resposible for filtering this list based on settings
    300 	 * like the phy mode.
    301 	 */
    302 	error = ath_getchannels(sc, ath_countrycode, ath_outdoor);
    303 	if (error != 0)
    304 		goto bad;
    305 	/*
    306 	 * Copy these back; they are set as a side effect
    307 	 * of constructing the channel list.
    308 	 */
    309 	ath_regdomain = ath_hal_getregdomain(ah);
    310 	ath_countrycode = ath_hal_getcountrycode(ah);
    311 
    312 	/*
    313 	 * Setup rate tables for all potential media types.
    314 	 */
    315 	ath_rate_setup(sc, IEEE80211_MODE_11A);
    316 	ath_rate_setup(sc, IEEE80211_MODE_11B);
    317 	ath_rate_setup(sc, IEEE80211_MODE_11G);
    318 	ath_rate_setup(sc, IEEE80211_MODE_TURBO);
    319 
    320 	error = ath_desc_alloc(sc);
    321 	if (error != 0) {
    322 		if_printf(ifp, "failed to allocate descriptors: %d\n", error);
    323 		goto bad;
    324 	}
    325 	ATH_CALLOUT_INIT(&sc->sc_scan_ch);
    326 	ATH_CALLOUT_INIT(&sc->sc_cal_ch);
    327 
    328 #ifdef __FreeBSD__
    329 	mtx_init(&sc->sc_txbuflock,
    330 		device_get_nameunit(sc->sc_dev), "xmit buf q", MTX_DEF);
    331 	mtx_init(&sc->sc_txqlock,
    332 		device_get_nameunit(sc->sc_dev), "xmit q", MTX_DEF);
    333 #endif
    334 
    335 	ATH_TASK_INIT(&sc->sc_txtask, ath_tx_proc, sc);
    336 	ATH_TASK_INIT(&sc->sc_rxtask, ath_rx_proc, sc);
    337 	ATH_TASK_INIT(&sc->sc_swbatask, ath_beacon_proc, sc);
    338 	ATH_TASK_INIT(&sc->sc_rxorntask, ath_rxorn_proc, sc);
    339 	ATH_TASK_INIT(&sc->sc_fataltask, ath_fatal_proc, sc);
    340 	ATH_TASK_INIT(&sc->sc_bmisstask, ath_bmiss_proc, sc);
    341 
    342 	/*
    343 	 * For now just pre-allocate one data queue and one
    344 	 * beacon queue.  Note that the HAL handles resetting
    345 	 * them at the needed time.  Eventually we'll want to
    346 	 * allocate more tx queues for splitting management
    347 	 * frames and for QOS support.
    348 	 */
    349 	sc->sc_txhalq = ath_hal_setuptxqueue(ah,
    350 		HAL_TX_QUEUE_DATA,
    351 		AH_TRUE			/* enable interrupts */
    352 	);
    353 	if (sc->sc_txhalq == (u_int) -1) {
    354 		if_printf(ifp, "unable to setup a data xmit queue!\n");
    355 		goto bad;
    356 	}
    357 	sc->sc_bhalq = ath_hal_setuptxqueue(ah,
    358 		HAL_TX_QUEUE_BEACON,
    359 		AH_TRUE			/* enable interrupts */
    360 	);
    361 	if (sc->sc_bhalq == (u_int) -1) {
    362 		if_printf(ifp, "unable to setup a beacon xmit queue!\n");
    363 		goto bad;
    364 	}
    365 
    366 	ifp->if_softc = sc;
    367 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
    368 	ifp->if_start = ath_start;
    369 	ifp->if_watchdog = ath_watchdog;
    370 	ifp->if_ioctl = ath_ioctl;
    371 	ifp->if_init = ath_init;
    372 #ifdef __FreeBSD__
    373 	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
    374 #else
    375 #if 0
    376 	ifp->if_stop = ath_stop;		/* XXX */
    377 #endif
    378 	IFQ_SET_READY(&ifp->if_snd);
    379 #endif
    380 
    381 	ic->ic_softc = sc;
    382 	ic->ic_newassoc = ath_newassoc;
    383 	/* XXX not right but it's not used anywhere important */
    384 	ic->ic_phytype = IEEE80211_T_OFDM;
    385 	ic->ic_opmode = IEEE80211_M_STA;
    386 	ic->ic_caps = IEEE80211_C_WEP | IEEE80211_C_IBSS | IEEE80211_C_HOSTAP
    387 		| IEEE80211_C_MONITOR;
    388 	/* NB: 11g support is identified when we fetch the channel set */
    389 	if (sc->sc_have11g)
    390 		ic->ic_caps |= IEEE80211_C_SHPREAMBLE;
    391 
    392 	/* get mac address from hardware */
    393 	ath_hal_getmac(ah, ic->ic_myaddr);
    394 
    395 #ifdef __NetBSD__
    396 	if_attach(ifp);
    397 #endif
    398 	/* call MI attach routine. */
    399 	ieee80211_ifattach(ifp);
    400 	/* override default methods */
    401 	ic->ic_node_alloc = ath_node_alloc;
    402 	ic->ic_node_free = ath_node_free;
    403 	ic->ic_node_copy = ath_node_copy;
    404 	sc->sc_newstate = ic->ic_newstate;
    405 	ic->ic_newstate = ath_newstate;
    406 	/* complete initialization */
    407 	ieee80211_media_init(ifp, ath_media_change, ieee80211_media_status);
    408 
    409 #if NBPFILTER > 0
    410 	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
    411 		sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th),
    412 		&sc->sc_drvbpf);
    413 #endif
    414 	/*
    415 	 * Initialize constant fields.
    416 	 *
    417 	 * NB: the channel is setup each time we transition to the
    418 	 *     RUN state to avoid filling it in for each frame.
    419 	 */
    420 	sc->sc_tx_th.wt_ihdr.it_len = sizeof(sc->sc_tx_th);
    421 	sc->sc_tx_th.wt_ihdr.it_present = ATH_TX_RADIOTAP_PRESENT;
    422 
    423 	sc->sc_rx_th.wr_ihdr.it_len = sizeof(sc->sc_rx_th);
    424 	sc->sc_rx_th.wr_ihdr.it_present = ATH_RX_RADIOTAP_PRESENT;
    425 
    426 	if_printf(ifp, "802.11 address: %s\n", ether_sprintf(ic->ic_myaddr));
    427 
    428 #ifdef __NetBSD__
    429 	sc->sc_flags |= ATH_ATTACHED;
    430 	/*
    431 	 * Make sure the interface is shutdown during reboot.
    432 	 */
    433 	sc->sc_sdhook = shutdownhook_establish(ath_shutdown, sc);
    434 	if (sc->sc_sdhook == NULL)
    435 		printf("%s: WARNING: unable to establish shutdown hook\n",
    436 			sc->sc_dev.dv_xname);
    437 	sc->sc_powerhook = powerhook_establish(ath_power, sc);
    438 	if (sc->sc_powerhook == NULL)
    439 		printf("%s: WARNING: unable to establish power hook\n",
    440 			sc->sc_dev.dv_xname);
    441 #endif
    442 	return 0;
    443 bad:
    444 	if (ah)
    445 		ath_hal_detach(ah);
    446 	sc->sc_invalid = 1;
    447 	return error;
    448 }
    449 
    450 int
    451 ath_detach(struct ath_softc *sc)
    452 {
    453 	struct ifnet *ifp = &sc->sc_ic.ic_if;
    454 	ath_softc_critsect_decl(s);
    455 
    456 	DPRINTF(("ath_detach: if_flags %x\n", ifp->if_flags));
    457 	if ((sc->sc_flags & ATH_ATTACHED) == 0)
    458 		return (0);
    459 
    460 	ath_softc_critsect_begin(sc, s);
    461 	ath_stop(ifp);
    462 #if NBPFILTER > 0
    463 	bpfdetach(ifp);
    464 #endif
    465 	ath_desc_free(sc);
    466 	ath_hal_detach(sc->sc_ah);
    467 	ieee80211_ifdetach(ifp);
    468 #ifdef __NetBSD__
    469 	if_detach(ifp);
    470 #endif
    471 	ath_softc_critsect_end(sc, s);
    472 #ifdef __NetBSD__
    473 	powerhook_disestablish(sc->sc_powerhook);
    474 	shutdownhook_disestablish(sc->sc_sdhook);
    475 #endif
    476 	return 0;
    477 }
    478 
    479 #ifdef __NetBSD__
    480 void
    481 ath_power(int why, void *arg)
    482 {
    483 	struct ath_softc *sc = arg;
    484 	int s;
    485 
    486 	DPRINTF(("ath_power(%d)\n", why));
    487 
    488 	s = splnet();
    489 	switch (why) {
    490 	case PWR_SUSPEND:
    491 	case PWR_STANDBY:
    492 		ath_suspend(sc, why);
    493 		break;
    494 	case PWR_RESUME:
    495 		ath_resume(sc, why);
    496 		break;
    497 	case PWR_SOFTSUSPEND:
    498 	case PWR_SOFTSTANDBY:
    499 	case PWR_SOFTRESUME:
    500 		break;
    501 	}
    502 	splx(s);
    503 }
    504 #endif
    505 
    506 void
    507 ath_suspend(struct ath_softc *sc, int why)
    508 {
    509 	struct ifnet *ifp = &sc->sc_ic.ic_if;
    510 
    511 	DPRINTF(("ath_suspend: if_flags %x\n", ifp->if_flags));
    512 
    513 	ath_stop(ifp);
    514 	if (sc->sc_power != NULL)
    515 		(*sc->sc_power)(sc, why);
    516 }
    517 
    518 void
    519 ath_resume(struct ath_softc *sc, int why)
    520 {
    521 	struct ifnet *ifp = &sc->sc_ic.ic_if;
    522 
    523 	DPRINTF(("ath_resume: if_flags %x\n", ifp->if_flags));
    524 
    525 	if (ifp->if_flags & IFF_UP) {
    526 		ath_init(ifp);
    527 #if 0
    528 		(void)ath_intr(sc);
    529 #endif
    530 		if (sc->sc_power != NULL)
    531 			(*sc->sc_power)(sc, why);
    532 		if (ifp->if_flags & IFF_RUNNING)
    533 			ath_start(ifp);
    534 	}
    535 }
    536 
    537 #ifdef __NetBSD__
    538 void
    539 ath_shutdown(void *arg)
    540 {
    541 	struct ath_softc *sc = arg;
    542 
    543 	ath_stop(&sc->sc_ic.ic_if);
    544 }
    545 #else
    546 void
    547 ath_shutdown(struct ath_softc *sc)
    548 {
    549 #if 1
    550 	return;
    551 #else
    552 	struct ifnet *ifp = &sc->sc_ic.ic_if;
    553 
    554 	DPRINTF(("ath_shutdown: if_flags %x\n", ifp->if_flags));
    555 
    556 	ath_stop(ifp);
    557 #endif
    558 }
    559 #endif
    560 
    561 #ifdef __NetBSD__
    562 int
    563 ath_intr(void *arg)
    564 {
    565 	return ath_intr1((struct ath_softc *)arg);
    566 }
    567 #else
    568 void
    569 ath_intr(void *arg)
    570 {
    571 	(void)ath_intr1((struct ath_softc *)arg);
    572 }
    573 #endif
    574 
    575 static int
    576 ath_intr1(struct ath_softc *sc)
    577 {
    578 	struct ieee80211com *ic = &sc->sc_ic;
    579 	struct ifnet *ifp = &ic->ic_if;
    580 	struct ath_hal *ah = sc->sc_ah;
    581 	HAL_INT status;
    582 
    583 	if (sc->sc_invalid) {
    584 		/*
    585 		 * The hardware is not ready/present, don't touch anything.
    586 		 * Note this can happen early on if the IRQ is shared.
    587 		 */
    588 		DPRINTF(("ath_intr: invalid; ignored\n"));
    589 		return 0;
    590 	}
    591 	if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) != (IFF_RUNNING|IFF_UP)) {
    592 		DPRINTF(("ath_intr: if_flags 0x%x\n", ifp->if_flags));
    593 		ath_hal_getisr(ah, &status);	/* clear ISR */
    594 		ath_hal_intrset(ah, 0);		/* disable further intr's */
    595 		return 1; /* XXX */
    596 	}
    597 	ath_hal_getisr(ah, &status);		/* NB: clears ISR too */
    598 	DPRINTF2(("ath_intr: status 0x%x\n", status));
    599 #ifdef AR_DEBUG
    600 	if (ath_debug &&
    601 	    (status & (HAL_INT_FATAL|HAL_INT_RXORN|HAL_INT_BMISS))) {
    602 		if_printf(ifp, "ath_intr: status 0x%x\n", status);
    603 		ath_hal_dumpstate(ah);
    604 	}
    605 #endif /* AR_DEBUG */
    606 	if (status & HAL_INT_FATAL) {
    607 		sc->sc_stats.ast_hardware++;
    608 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
    609 		ATH_TASK_RUN_OR_ENQUEUE(&sc->sc_fataltask);
    610 	} else if (status & HAL_INT_RXORN) {
    611 		sc->sc_stats.ast_rxorn++;
    612 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
    613 		ATH_TASK_RUN_OR_ENQUEUE(&sc->sc_rxorntask);
    614 	} else {
    615 		if (status & HAL_INT_RXEOL) {
    616 			/*
    617 			 * NB: the hardware should re-read the link when
    618 			 *     RXE bit is written, but it doesn't work at
    619 			 *     least on older hardware revs.
    620 			 */
    621 			sc->sc_stats.ast_rxeol++;
    622 			sc->sc_rxlink = NULL;
    623 		}
    624 		if (status & HAL_INT_TXURN) {
    625 			sc->sc_stats.ast_txurn++;
    626 			/* bump tx trigger level */
    627 			ath_hal_updatetxtriglevel(ah, AH_TRUE);
    628 		}
    629 		if (status & HAL_INT_RX)
    630 			ATH_TASK_RUN_OR_ENQUEUE(&sc->sc_rxtask);
    631 		if (status & HAL_INT_TX)
    632 			ATH_TASK_RUN_OR_ENQUEUE(&sc->sc_txtask);
    633 		if (status & HAL_INT_SWBA)
    634 			ATH_TASK_RUN_OR_ENQUEUE(&sc->sc_swbatask);
    635 		if (status & HAL_INT_BMISS) {
    636 			sc->sc_stats.ast_bmiss++;
    637 			ATH_TASK_RUN_OR_ENQUEUE(&sc->sc_bmisstask);
    638 		}
    639 	}
    640 	return 1;
    641 }
    642 
    643 static void
    644 ath_fatal_proc(void *arg, int pending)
    645 {
    646 	struct ath_softc *sc = arg;
    647 
    648 	device_printf(sc->sc_dev, "hardware error; resetting\n");
    649 	ath_reset(sc);
    650 }
    651 
    652 static void
    653 ath_rxorn_proc(void *arg, int pending)
    654 {
    655 	struct ath_softc *sc = arg;
    656 
    657 	device_printf(sc->sc_dev, "rx FIFO overrun; resetting\n");
    658 	ath_reset(sc);
    659 }
    660 
    661 static void
    662 ath_bmiss_proc(void *arg, int pending)
    663 {
    664 	struct ath_softc *sc = arg;
    665 	struct ieee80211com *ic = &sc->sc_ic;
    666 
    667 	DPRINTF(("ath_bmiss_proc: pending %u\n", pending));
    668 	KASSERT(ic->ic_opmode == IEEE80211_M_STA,
    669 		("unexpect operating mode %u", ic->ic_opmode));
    670 	if (ic->ic_state == IEEE80211_S_RUN)
    671 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
    672 }
    673 
    674 static u_int
    675 ath_chan2flags(struct ieee80211com *ic, struct ieee80211_channel *chan)
    676 {
    677 	enum ieee80211_phymode mode = ieee80211_chan2mode(ic, chan);
    678 
    679 	switch (mode) {
    680 	case IEEE80211_MODE_AUTO:
    681 		return 0;
    682 	case IEEE80211_MODE_11A:
    683 		return CHANNEL_A;
    684 	case IEEE80211_MODE_11B:
    685 		return CHANNEL_B;
    686 	case IEEE80211_MODE_11G:
    687 		return CHANNEL_PUREG;
    688 	case IEEE80211_MODE_TURBO:
    689 		return CHANNEL_T;
    690 	default:
    691 		panic("%s: unsupported mode %d\n", __func__, mode);
    692 		return 0;
    693 	}
    694 }
    695 
    696 #ifdef __NetBSD__
    697 static int
    698 ath_init(struct ifnet *ifp)
    699 {
    700 	return ath_init1((struct ath_softc *)ifp->if_softc);
    701 }
    702 #else
    703 static void
    704 ath_init(void *arg)
    705 {
    706 	(void)ath_init1((struct ath_softc *)arg);
    707 }
    708 #endif
    709 
    710 static int
    711 ath_init1(struct ath_softc *sc)
    712 {
    713 	struct ieee80211com *ic = &sc->sc_ic;
    714 	struct ifnet *ifp = &ic->ic_if;
    715 	struct ieee80211_node *ni;
    716 	enum ieee80211_phymode mode;
    717 	struct ath_hal *ah = sc->sc_ah;
    718 	HAL_STATUS status;
    719 	HAL_CHANNEL hchan;
    720 	int error = 0;
    721 	ath_softc_critsect_decl(s);
    722 
    723 	DPRINTF(("ath_init: if_flags 0x%x\n", ifp->if_flags));
    724 
    725 #ifdef __NetBSD__
    726 	if ((error = ath_enable(sc)) != 0)
    727 		return error;
    728 #endif
    729 
    730 	ath_softc_critsect_begin(sc, s);
    731 	/*
    732 	 * Stop anything previously setup.  This is safe
    733 	 * whether this is the first time through or not.
    734 	 */
    735 	ath_stop(ifp);
    736 
    737 	/*
    738 	 * The basic interface to setting the hardware in a good
    739 	 * state is ``reset''.  On return the hardware is known to
    740 	 * be powered up and with interrupts disabled.  This must
    741 	 * be followed by initialization of the appropriate bits
    742 	 * and then setup of the interrupt mask.
    743 	 */
    744 	hchan.channel = ic->ic_ibss_chan->ic_freq;
    745 	hchan.channelFlags = ath_chan2flags(ic, ic->ic_ibss_chan);
    746 	if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_FALSE, &status)) {
    747 		if_printf(ifp, "unable to reset hardware; hal status %u\n",
    748 			status);
    749 		error = -1;
    750 		goto done;
    751 	}
    752 
    753 	/*
    754 	 * Setup the hardware after reset: the key cache
    755 	 * is filled as needed and the receive engine is
    756 	 * set going.  Frame transmit is handled entirely
    757 	 * in the frame output path; there's nothing to do
    758 	 * here except setup the interrupt mask.
    759 	 */
    760 	if (ic->ic_flags & IEEE80211_F_WEPON)
    761 		ath_initkeytable(sc);
    762 	if ((error = ath_startrecv(sc)) != 0) {
    763 		if_printf(ifp, "unable to start recv logic\n");
    764 		goto done;
    765 	}
    766 
    767 	/*
    768 	 * Enable interrupts.
    769 	 */
    770 	sc->sc_imask = HAL_INT_RX | HAL_INT_TX
    771 		  | HAL_INT_RXEOL | HAL_INT_RXORN
    772 		  | HAL_INT_FATAL | HAL_INT_GLOBAL;
    773 	ath_hal_intrset(ah, sc->sc_imask);
    774 
    775 	ifp->if_flags |= IFF_RUNNING;
    776 	ic->ic_state = IEEE80211_S_INIT;
    777 
    778 	/*
    779 	 * The hardware should be ready to go now so it's safe
    780 	 * to kick the 802.11 state machine as it's likely to
    781 	 * immediately call back to us to send mgmt frames.
    782 	 */
    783 	ni = ic->ic_bss;
    784 	ni->ni_chan = ic->ic_ibss_chan;
    785 	mode = ieee80211_chan2mode(ic, ni->ni_chan);
    786 	if (mode != sc->sc_curmode)
    787 		ath_setcurmode(sc, mode);
    788 	if (ic->ic_opmode != IEEE80211_M_MONITOR)
    789 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
    790 	else
    791 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
    792 done:
    793 	ath_softc_critsect_end(sc, s);
    794 	return error;
    795 }
    796 
    797 static void
    798 ath_stop(struct ifnet *ifp)
    799 {
    800 	struct ieee80211com *ic = (struct ieee80211com *) ifp;
    801 	struct ath_softc *sc = ifp->if_softc;
    802 	struct ath_hal *ah = sc->sc_ah;
    803 	ath_softc_critsect_decl(s);
    804 
    805 	DPRINTF(("ath_stop: invalid %u if_flags 0x%x\n",
    806 		sc->sc_invalid, ifp->if_flags));
    807 
    808 	ath_softc_critsect_begin(sc, s);
    809 	if (ifp->if_flags & IFF_RUNNING) {
    810 		/*
    811 		 * Shutdown the hardware and driver:
    812 		 *    disable interrupts
    813 		 *    turn off timers
    814 		 *    clear transmit machinery
    815 		 *    clear receive machinery
    816 		 *    drain and release tx queues
    817 		 *    reclaim beacon resources
    818 		 *    reset 802.11 state machine
    819 		 *    power down hardware
    820 		 *
    821 		 * Note that some of this work is not possible if the
    822 		 * hardware is gone (invalid).
    823 		 */
    824 		ifp->if_flags &= ~IFF_RUNNING;
    825 		ifp->if_timer = 0;
    826 		if (!sc->sc_invalid)
    827 			ath_hal_intrset(ah, 0);
    828 		ath_draintxq(sc);
    829 		if (!sc->sc_invalid)
    830 			ath_stoprecv(sc);
    831 		else
    832 			sc->sc_rxlink = NULL;
    833 #ifdef __FreeBSD__
    834 		IF_DRAIN(&ifp->if_snd);
    835 #else
    836 		IF_PURGE(&ifp->if_snd);
    837 #endif
    838 		ath_beacon_free(sc);
    839 		ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
    840 		if (!sc->sc_invalid) {
    841 			ath_hal_setpower(ah, HAL_PM_FULL_SLEEP, 0);
    842 		}
    843 #ifdef __NetBSD__
    844 		ath_disable(sc);
    845 #endif
    846 	}
    847 	ath_softc_critsect_end(sc, s);
    848 }
    849 
    850 /*
    851  * Reset the hardware w/o losing operational state.  This is
    852  * basically a more efficient way of doing ath_stop, ath_init,
    853  * followed by state transitions to the current 802.11
    854  * operational state.  Used to recover from errors rx overrun
    855  * and to reset the hardware when rf gain settings must be reset.
    856  */
    857 static void
    858 ath_reset(struct ath_softc *sc)
    859 {
    860 	struct ieee80211com *ic = &sc->sc_ic;
    861 	struct ifnet *ifp = &ic->ic_if;
    862 	struct ath_hal *ah = sc->sc_ah;
    863 	struct ieee80211_channel *c;
    864 	HAL_STATUS status;
    865 	HAL_CHANNEL hchan;
    866 
    867 	/*
    868 	 * Convert to a HAL channel description with the flags
    869 	 * constrained to reflect the current operating mode.
    870 	 */
    871 	c = ic->ic_ibss_chan;
    872 	hchan.channel = c->ic_freq;
    873 	hchan.channelFlags = ath_chan2flags(ic, c);
    874 
    875 	ath_hal_intrset(ah, 0);		/* disable interrupts */
    876 	ath_draintxq(sc);		/* stop xmit side */
    877 	ath_stoprecv(sc);		/* stop recv side */
    878 	/* NB: indicate channel change so we do a full reset */
    879 	if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_TRUE, &status))
    880 		if_printf(ifp, "%s: unable to reset hardware; hal status %u\n",
    881 			__func__, status);
    882 	ath_hal_intrset(ah, sc->sc_imask);
    883 	if (ath_startrecv(sc) != 0)	/* restart recv */
    884 		if_printf(ifp, "%s: unable to start recv logic\n", __func__);
    885 	ath_start(ifp);			/* restart xmit */
    886 	if (ic->ic_state == IEEE80211_S_RUN)
    887 		ath_beacon_config(sc);	/* restart beacons */
    888 }
    889 
    890 static void
    891 ath_start(struct ifnet *ifp)
    892 {
    893 	struct ath_softc *sc = ifp->if_softc;
    894 	struct ath_hal *ah = sc->sc_ah;
    895 	struct ieee80211com *ic = &sc->sc_ic;
    896 	struct ieee80211_node *ni;
    897 	struct ath_buf *bf;
    898 	struct mbuf *m;
    899 	struct ieee80211_frame *wh;
    900 	ath_txbuf_critsect_decl(s);
    901 
    902 	if ((ifp->if_flags & IFF_RUNNING) == 0 || sc->sc_invalid)
    903 		return;
    904 	for (;;) {
    905 		/*
    906 		 * Grab a TX buffer and associated resources.
    907 		 */
    908 		ath_txbuf_critsect_begin(sc, s);
    909 		bf = TAILQ_FIRST(&sc->sc_txbuf);
    910 		if (bf != NULL)
    911 			TAILQ_REMOVE(&sc->sc_txbuf, bf, bf_list);
    912 		ath_txbuf_critsect_end(sc, s);
    913 		if (bf == NULL) {
    914 			DPRINTF(("ath_start: out of xmit buffers\n"));
    915 			sc->sc_stats.ast_tx_qstop++;
    916 			ifp->if_flags |= IFF_OACTIVE;
    917 			break;
    918 		}
    919 		/*
    920 		 * Poll the management queue for frames; they
    921 		 * have priority over normal data frames.
    922 		 */
    923 		IF_DEQUEUE(&ic->ic_mgtq, m);
    924 		if (m == NULL) {
    925 			/*
    926 			 * No data frames go out unless we're associated.
    927 			 */
    928 			if (ic->ic_state != IEEE80211_S_RUN) {
    929 				DPRINTF(("ath_start: ignore data packet, "
    930 					"state %u\n", ic->ic_state));
    931 				sc->sc_stats.ast_tx_discard++;
    932 				ath_txbuf_critsect_begin(sc, s);
    933 				TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
    934 				ath_txbuf_critsect_end(sc, s);
    935 				break;
    936 			}
    937 			IF_DEQUEUE(&ifp->if_snd, m);
    938 			if (m == NULL) {
    939 				ath_txbuf_critsect_begin(sc, s);
    940 				TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
    941 				ath_txbuf_critsect_end(sc, s);
    942 				break;
    943 			}
    944 			ifp->if_opackets++;
    945 
    946 #ifdef __NetBSD__
    947 #if NBPFILTER > 0
    948 			if (ifp->if_bpf)
    949 				bpf_mtap(ifp->if_bpf, m);
    950 #endif
    951 #endif
    952 #ifdef __FreeBSD__
    953 			BPF_MTAP(ifp, m);
    954 #endif
    955 			/*
    956 			 * Encapsulate the packet in prep for transmission.
    957 			 */
    958 			m = ieee80211_encap(ifp, m, &ni);
    959 			if (m == NULL) {
    960 				DPRINTF(("ath_start: encapsulation failure\n"));
    961 				sc->sc_stats.ast_tx_encap++;
    962 				goto bad;
    963 			}
    964 			wh = mtod(m, struct ieee80211_frame *);
    965 			if (ic->ic_flags & IEEE80211_F_WEPON)
    966 				wh->i_fc[1] |= IEEE80211_FC1_WEP;
    967 		} else {
    968 			/*
    969 			 * Hack!  The referenced node pointer is in the
    970 			 * rcvif field of the packet header.  This is
    971 			 * placed there by ieee80211_mgmt_output because
    972 			 * we need to hold the reference with the frame
    973 			 * and there's no other way (other than packet
    974 			 * tags which we consider too expensive to use)
    975 			 * to pass it along.
    976 			 */
    977 			ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
    978 			m->m_pkthdr.rcvif = NULL;
    979 
    980 			wh = mtod(m, struct ieee80211_frame *);
    981 			if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
    982 			    IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
    983 				/* fill time stamp */
    984 				u_int64_t tsf;
    985 				u_int32_t *tstamp;
    986 
    987 				tsf = ath_hal_gettsf64(ah);
    988 				/* XXX: adjust 100us delay to xmit */
    989 				tsf += 100;
    990 				tstamp = (u_int32_t *)&wh[1];
    991 				tstamp[0] = htole32(tsf & 0xffffffff);
    992 				tstamp[1] = htole32(tsf >> 32);
    993 			}
    994 			sc->sc_stats.ast_tx_mgmt++;
    995 		}
    996 #if NBPFILTER > 0
    997 		if (ic->ic_rawbpf)
    998 			bpf_mtap(ic->ic_rawbpf, m);
    999 #endif
   1000 
   1001 #if NBPFILTER > 0
   1002 		if (sc->sc_drvbpf) {
   1003 #ifdef __FreeBSD__
   1004 			struct mbuf *mb;
   1005 
   1006 			MGETHDR(mb, M_DONTWAIT, m->m_type);
   1007 			if (mb != NULL) {
   1008 				sc->sc_tx_th.wt_rate =
   1009 					ni->ni_rates.rs_rates[ni->ni_txrate];
   1010 
   1011 				mb->m_next = m;
   1012 				mb->m_data = (caddr_t)&sc->sc_tx_th;
   1013 				mb->m_len = sizeof(sc->sc_tx_th);
   1014 				mb->m_pkthdr.len += mb->m_len;
   1015 				bpf_mtap(sc->sc_drvbpf, mb);
   1016 				m_free(mb);
   1017 			}
   1018 #else
   1019 			struct mbuf mb;
   1020 
   1021 			M_COPY_PKTHDR(&mb, m);
   1022 			sc->sc_tx_th.wt_rate =
   1023 				ni->ni_rates.rs_rates[ni->ni_txrate];
   1024 
   1025 			mb.m_next = m;
   1026 			mb.m_data = (caddr_t)&sc->sc_tx_th;
   1027 			mb.m_len = sizeof(sc->sc_tx_th);
   1028 			mb.m_pkthdr.len += mb.m_len;
   1029 			bpf_mtap(sc->sc_drvbpf, &mb);
   1030 #endif
   1031 		}
   1032 #endif
   1033 
   1034 		/*
   1035 		 * TODO:
   1036 		 * The duration field of 802.11 header should be filled.
   1037 		 * XXX This may be done in the ieee80211 layer, but the upper
   1038 		 *     doesn't know the detail of parameters such as IFS
   1039 		 *     for now..
   1040 		 */
   1041 		if (ath_tx_start(sc, ni, bf, m)) {
   1042 	bad:
   1043 			ath_txbuf_critsect_begin(sc, s);
   1044 			TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
   1045 			ath_txbuf_critsect_end(sc, s);
   1046 			ifp->if_oerrors++;
   1047 			if (ni && ni != ic->ic_bss)
   1048 				ieee80211_free_node(ic, ni);
   1049 			continue;
   1050 		}
   1051 
   1052 		sc->sc_tx_timer = 5;
   1053 		ifp->if_timer = 1;
   1054 	}
   1055 }
   1056 
   1057 static int
   1058 ath_media_change(struct ifnet *ifp)
   1059 {
   1060 	int error;
   1061 
   1062 	error = ieee80211_media_change(ifp);
   1063 	if (error == ENETRESET) {
   1064 		if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) ==
   1065 		    (IFF_RUNNING|IFF_UP))
   1066 			ath_init(ifp);		/* XXX lose error */
   1067 		error = 0;
   1068 	}
   1069 	return error;
   1070 }
   1071 
   1072 static void
   1073 ath_watchdog(struct ifnet *ifp)
   1074 {
   1075 	struct ath_softc *sc = ifp->if_softc;
   1076 	struct ieee80211com *ic = &sc->sc_ic;
   1077 
   1078 	ifp->if_timer = 0;
   1079 	if ((ifp->if_flags & IFF_RUNNING) == 0 || sc->sc_invalid)
   1080 		return;
   1081 	if (sc->sc_tx_timer) {
   1082 		if (--sc->sc_tx_timer == 0) {
   1083 			if_printf(ifp, "device timeout\n");
   1084 #ifdef AR_DEBUG
   1085 			if (ath_debug)
   1086 				ath_hal_dumpstate(sc->sc_ah);
   1087 #endif /* AR_DEBUG */
   1088 			ath_init(ifp);		/* XXX ath_reset??? */
   1089 			ifp->if_oerrors++;
   1090 			sc->sc_stats.ast_watchdog++;
   1091 			return;
   1092 		}
   1093 		ifp->if_timer = 1;
   1094 	}
   1095 	if (ic->ic_fixed_rate == -1) {
   1096 		/*
   1097 		 * Run the rate control algorithm if we're not
   1098 		 * locked at a fixed rate.
   1099 		 */
   1100 		if (ic->ic_opmode == IEEE80211_M_STA)
   1101 			ath_rate_ctl(sc, ic->ic_bss);
   1102 		else
   1103 			ieee80211_iterate_nodes(ic, ath_rate_ctl, sc);
   1104 	}
   1105 	ieee80211_watchdog(ifp);
   1106 }
   1107 
   1108 static int
   1109 ath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
   1110 {
   1111 	struct ath_softc *sc = ifp->if_softc;
   1112 	struct ifreq *ifr = (struct ifreq *)data;
   1113 	int error = 0;
   1114 	ath_softc_critsect_decl(s);
   1115 
   1116 	ath_softc_critsect_begin(sc, s);
   1117 	switch (cmd) {
   1118 	case SIOCSIFFLAGS:
   1119 		if (ifp->if_flags & IFF_UP) {
   1120 			if (ifp->if_flags & IFF_RUNNING) {
   1121 				/*
   1122 				 * To avoid rescanning another access point,
   1123 				 * do not call ath_init() here.  Instead,
   1124 				 * only reflect promisc mode settings.
   1125 				 */
   1126 				ath_mode_init(sc);
   1127 			} else
   1128 				ath_init(ifp);		/* XXX lose error */
   1129 		} else
   1130 			ath_stop(ifp);
   1131 		break;
   1132 	case SIOCADDMULTI:
   1133 	case SIOCDELMULTI:
   1134 #ifdef __FreeBSD__
   1135 		/*
   1136 		 * The upper layer has already installed/removed
   1137 		 * the multicast address(es), just recalculate the
   1138 		 * multicast filter for the card.
   1139 		 */
   1140 		if (ifp->if_flags & IFF_RUNNING)
   1141 			ath_mode_init(sc);
   1142 #endif
   1143 #ifdef __NetBSD__
   1144 		error = (cmd == SIOCADDMULTI) ?
   1145 		    ether_addmulti(ifr, &sc->sc_ic.ic_ec) :
   1146 		    ether_delmulti(ifr, &sc->sc_ic.ic_ec);
   1147 		if (error == ENETRESET) {
   1148 			if (ifp->if_flags & IFF_RUNNING)
   1149 				ath_mode_init(sc);
   1150 			error = 0;
   1151 		}
   1152 #endif
   1153 		break;
   1154 	case SIOCGATHSTATS:
   1155 		copyout(&sc->sc_stats, ifr->ifr_data, sizeof (sc->sc_stats));
   1156 		break;
   1157 	default:
   1158 		error = ieee80211_ioctl(ifp, cmd, data);
   1159 		if (error == ENETRESET) {
   1160 			if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) ==
   1161 			    (IFF_RUNNING|IFF_UP))
   1162 				ath_init(ifp);		/* XXX lose error */
   1163 			error = 0;
   1164 		}
   1165 		break;
   1166 	}
   1167 	ath_softc_critsect_end(sc, s);
   1168 	return error;
   1169 }
   1170 
   1171 /*
   1172  * Fill the hardware key cache with key entries.
   1173  */
   1174 static void
   1175 ath_initkeytable(struct ath_softc *sc)
   1176 {
   1177 	struct ieee80211com *ic = &sc->sc_ic;
   1178 	struct ath_hal *ah = sc->sc_ah;
   1179 	int i;
   1180 
   1181 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   1182 		struct ieee80211_wepkey *k = &ic->ic_nw_keys[i];
   1183 		if (k->wk_len == 0)
   1184 			ath_hal_keyreset(ah, i);
   1185 		else
   1186 			/* XXX return value */
   1187 			/* NB: this uses HAL_KEYVAL == ieee80211_wepkey */
   1188 			ath_hal_keyset(ah, i, (const HAL_KEYVAL *) k);
   1189 	}
   1190 }
   1191 
   1192 static void
   1193 ath_mcastfilter_accum(caddr_t dl, u_int32_t (*mfilt)[2])
   1194 {
   1195 	u_int32_t val;
   1196 	u_int8_t pos;
   1197 
   1198 	val = LE_READ_4(dl + 0);
   1199 	pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
   1200 	val = LE_READ_4(dl + 3);
   1201 	pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
   1202 	pos &= 0x3f;
   1203 	(*mfilt)[pos / 32] |= (1 << (pos % 32));
   1204 }
   1205 
   1206 #ifdef __FreeBSD__
   1207 static void
   1208 ath_mcastfilter_compute(struct ath_softc *sc, u_int32_t (*mfilt)[2])
   1209 {
   1210 	struct ieee80211com *ic = &sc->sc_ic;
   1211 	struct ifnet *ifp = &ic->ic_if;
   1212 	struct ifmultiaddr *ifma;
   1213 
   1214 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
   1215 		caddr_t dl;
   1216 
   1217 		/* calculate XOR of eight 6bit values */
   1218 		dl = LLADDR((struct sockaddr_dl *) ifma->ifma_addr);
   1219 		ath_mcastfilter_accum(dl, &mfilt);
   1220 	}
   1221 }
   1222 #else
   1223 static void
   1224 ath_mcastfilter_compute(struct ath_softc *sc, u_int32_t (*mfilt)[2])
   1225 {
   1226 	struct ifnet *ifp = &sc->sc_ic.ic_if;
   1227 	struct ether_multi *enm;
   1228 	struct ether_multistep estep;
   1229 
   1230 	ETHER_FIRST_MULTI(estep, &sc->sc_ic.ic_ec, enm);
   1231 	while (enm != NULL) {
   1232 		/* XXX Punt on ranges. */
   1233 		if (!IEEE80211_ADDR_EQ(enm->enm_addrlo, enm->enm_addrhi)) {
   1234 			(*mfilt)[0] = (*mfilt)[1] = ~((u_int32_t)0);
   1235 			ifp->if_flags |= IFF_ALLMULTI;
   1236 			return;
   1237 		}
   1238 		ath_mcastfilter_accum(enm->enm_addrlo, mfilt);
   1239 		ETHER_NEXT_MULTI(estep, enm);
   1240 	}
   1241 	ifp->if_flags &= ~IFF_ALLMULTI;
   1242 }
   1243 #endif
   1244 
   1245 static void
   1246 ath_mode_init(struct ath_softc *sc)
   1247 {
   1248 	struct ieee80211com *ic = &sc->sc_ic;
   1249 	struct ath_hal *ah = sc->sc_ah;
   1250 	struct ifnet *ifp = &ic->ic_if;
   1251 	u_int32_t rfilt, mfilt[2];
   1252 
   1253 	/* configure operational mode */
   1254 	ath_hal_setopmode(ah, ic->ic_opmode);
   1255 
   1256 	/* receive filter */
   1257 	rfilt = (ath_hal_getrxfilter(ah) & HAL_RX_FILTER_PHYERR)
   1258 	      | HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST;
   1259 	if (ic->ic_opmode != IEEE80211_M_STA)
   1260 		rfilt |= HAL_RX_FILTER_PROBEREQ;
   1261 	if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
   1262 	    (ifp->if_flags & IFF_PROMISC))
   1263 		rfilt |= HAL_RX_FILTER_PROM;
   1264 	if (ic->ic_state == IEEE80211_S_SCAN)
   1265 		rfilt |= HAL_RX_FILTER_BEACON;
   1266 	ath_hal_setrxfilter(ah, rfilt);
   1267 
   1268 	/* calculate and install multicast filter */
   1269 #ifdef __FreeBSD__
   1270 	if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
   1271 		mfilt[0] = mfilt[1] = 0;
   1272 		ath_mcastfilter_compute(sc, &mfilt);
   1273 	} else {
   1274 		mfilt[0] = mfilt[1] = ~0;
   1275 	}
   1276 #endif
   1277 #ifdef __NetBSD__
   1278 	mfilt[0] = mfilt[1] = 0;
   1279 	ath_mcastfilter_compute(sc, &mfilt);
   1280 #endif
   1281 	ath_hal_setmcastfilter(ah, mfilt[0], mfilt[1]);
   1282 	DPRINTF(("ath_mode_init: RX filter 0x%x, MC filter %08x:%08x\n",
   1283 		rfilt, mfilt[0], mfilt[1]));
   1284 }
   1285 
   1286 #ifdef __FreeBSD__
   1287 static void
   1288 ath_mbuf_load_cb(void *arg, bus_dma_segment_t *seg, int nseg, bus_size_t mapsize, int error)
   1289 {
   1290 	struct ath_buf *bf = arg;
   1291 
   1292 	KASSERT(nseg <= ATH_MAX_SCATTER,
   1293 		("ath_mbuf_load_cb: too many DMA segments %u", nseg));
   1294 	bf->bf_mapsize = mapsize;
   1295 	bf->bf_nseg = nseg;
   1296 	bcopy(seg, bf->bf_segs, nseg * sizeof (seg[0]));
   1297 }
   1298 #endif /* __FreeBSD__ */
   1299 
   1300 static struct mbuf *
   1301 ath_getmbuf(int flags, int type, u_int pktlen)
   1302 {
   1303 	struct mbuf *m;
   1304 
   1305 	KASSERT(pktlen <= MCLBYTES, ("802.11 packet too large: %u", pktlen));
   1306 #ifdef __FreeBSD__
   1307 	if (pktlen <= MHLEN)
   1308 		MGETHDR(m, flags, type);
   1309 	else
   1310 		m = m_getcl(flags, type, M_PKTHDR);
   1311 #else
   1312 	MGETHDR(m, flags, type);
   1313 	if (m != NULL && pktlen > MHLEN)
   1314 		MCLGET(m, flags);
   1315 #endif
   1316 	return m;
   1317 }
   1318 
   1319 static int
   1320 ath_beacon_alloc(struct ath_softc *sc, struct ieee80211_node *ni)
   1321 {
   1322 	struct ieee80211com *ic = &sc->sc_ic;
   1323 	struct ifnet *ifp = &ic->ic_if;
   1324 	struct ath_hal *ah = sc->sc_ah;
   1325 	struct ieee80211_frame *wh;
   1326 	struct ath_buf *bf;
   1327 	struct ath_desc *ds;
   1328 	struct mbuf *m;
   1329 	int error, pktlen;
   1330 	u_int8_t *frm, rate;
   1331 	u_int16_t capinfo;
   1332 	struct ieee80211_rateset *rs;
   1333 	const HAL_RATE_TABLE *rt;
   1334 
   1335 	bf = sc->sc_bcbuf;
   1336 	if (bf->bf_m != NULL) {
   1337 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
   1338 		m_freem(bf->bf_m);
   1339 		bf->bf_m = NULL;
   1340 		bf->bf_node = NULL;
   1341 	}
   1342 	/*
   1343 	 * NB: the beacon data buffer must be 32-bit aligned;
   1344 	 * we assume the mbuf routines will return us something
   1345 	 * with this alignment (perhaps should assert).
   1346 	 */
   1347 	rs = &ni->ni_rates;
   1348 	pktlen = sizeof (struct ieee80211_frame)
   1349 	       + 8 + 2 + 2 + 2+ni->ni_esslen + 2+rs->rs_nrates + 6;
   1350 	if (rs->rs_nrates > IEEE80211_RATE_SIZE)
   1351 		pktlen += 2;
   1352 	m = ath_getmbuf(M_DONTWAIT, MT_DATA, pktlen);
   1353 	if (m == NULL) {
   1354 		DPRINTF(("ath_beacon_alloc: cannot get mbuf/cluster; size %u\n",
   1355 			pktlen));
   1356 		sc->sc_stats.ast_be_nombuf++;
   1357 		return ENOMEM;
   1358 	}
   1359 
   1360 	wh = mtod(m, struct ieee80211_frame *);
   1361 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
   1362 	    IEEE80211_FC0_SUBTYPE_BEACON;
   1363 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
   1364 	*(u_int16_t *)wh->i_dur = 0;
   1365 	memcpy(wh->i_addr1, ifp->if_broadcastaddr, IEEE80211_ADDR_LEN);
   1366 	memcpy(wh->i_addr2, ic->ic_myaddr, IEEE80211_ADDR_LEN);
   1367 	memcpy(wh->i_addr3, ni->ni_bssid, IEEE80211_ADDR_LEN);
   1368 	*(u_int16_t *)wh->i_seq = 0;
   1369 
   1370 	/*
   1371 	 * beacon frame format
   1372 	 *	[8] time stamp
   1373 	 *	[2] beacon interval
   1374 	 *	[2] cabability information
   1375 	 *	[tlv] ssid
   1376 	 *	[tlv] supported rates
   1377 	 *	[tlv] parameter set (IBSS)
   1378 	 *	[tlv] extended supported rates
   1379 	 */
   1380 	frm = (u_int8_t *)&wh[1];
   1381 	memset(frm, 0, 8);	/* timestamp is set by hardware */
   1382 	frm += 8;
   1383 	*(u_int16_t *)frm = htole16(ni->ni_intval);
   1384 	frm += 2;
   1385 	if (ic->ic_opmode == IEEE80211_M_IBSS)
   1386 		capinfo = IEEE80211_CAPINFO_IBSS;
   1387 	else
   1388 		capinfo = IEEE80211_CAPINFO_ESS;
   1389 	if (ic->ic_flags & IEEE80211_F_WEPON)
   1390 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
   1391 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
   1392 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
   1393 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
   1394 		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
   1395 	*(u_int16_t *)frm = htole16(capinfo);
   1396 	frm += 2;
   1397 	*frm++ = IEEE80211_ELEMID_SSID;
   1398 	*frm++ = ni->ni_esslen;
   1399 	memcpy(frm, ni->ni_essid, ni->ni_esslen);
   1400 	frm += ni->ni_esslen;
   1401 	frm = ieee80211_add_rates(frm, rs);
   1402 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
   1403 		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
   1404 		*frm++ = 2;
   1405 		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
   1406 	} else {
   1407 		/* TODO: TIM */
   1408 		*frm++ = IEEE80211_ELEMID_TIM;
   1409 		*frm++ = 4;	/* length */
   1410 		*frm++ = 0;	/* DTIM count */
   1411 		*frm++ = 1;	/* DTIM period */
   1412 		*frm++ = 0;	/* bitmap control */
   1413 		*frm++ = 0;	/* Partial Virtual Bitmap (variable length) */
   1414 	}
   1415 	frm = ieee80211_add_xrates(frm, rs);
   1416 	m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
   1417 	KASSERT(m->m_pkthdr.len <= pktlen,
   1418 		("beacon bigger than expected, len %u calculated %u",
   1419 		m->m_pkthdr.len, pktlen));
   1420 
   1421 	DPRINTF2(("ath_beacon_alloc: m %p len %u\n", m, m->m_len));
   1422 	error = ath_buf_dmamap_load_mbuf(sc->sc_dmat, bf, m, BUS_DMA_NOWAIT);
   1423 	if (error != 0) {
   1424 		m_freem(m);
   1425 		return error;
   1426 	}
   1427 	KASSERT(bf->bf_nseg == 1,
   1428 		("ath_beacon_alloc: multi-segment packet; nseg %u",
   1429 		bf->bf_nseg));
   1430 	bf->bf_m = m;
   1431 
   1432 	/* setup descriptors */
   1433 	ds = bf->bf_desc;
   1434 
   1435 	ds->ds_link = 0;
   1436 	ds->ds_data = bf->bf_segs[0].ds_addr;
   1437 
   1438 	DPRINTF2(("%s: segaddr %p seglen %u\n", __func__,
   1439 	    (caddr_t)bf->bf_segs[0].ds_addr, (u_int)bf->bf_segs[0].ds_len));
   1440 
   1441 	/*
   1442 	 * Calculate rate code.
   1443 	 * XXX everything at min xmit rate
   1444 	 */
   1445 	rt = sc->sc_currates;
   1446 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
   1447 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
   1448 		rate = rt->info[0].rateCode | rt->info[0].shortPreamble;
   1449 	else
   1450 		rate = rt->info[0].rateCode;
   1451 	if (!ath_hal_setuptxdesc(ah, ds
   1452 		, m->m_pkthdr.len + IEEE80211_CRC_LEN	/* packet length */
   1453 		, sizeof(struct ieee80211_frame)	/* header length */
   1454 		, HAL_PKT_TYPE_BEACON		/* Atheros packet type */
   1455 		, 0x20				/* txpower XXX */
   1456 		, rate, 1			/* series 0 rate/tries */
   1457 		, HAL_TXKEYIX_INVALID		/* no encryption */
   1458 		, 0				/* antenna mode */
   1459 		, HAL_TXDESC_NOACK		/* no ack for beacons */
   1460 		, 0				/* rts/cts rate */
   1461 		, 0				/* rts/cts duration */
   1462 	)) {
   1463 		printf("%s: ath_hal_setuptxdesc failed\n", __func__);
   1464 		return -1;
   1465 	}
   1466 	/* NB: beacon's BufLen must be a multiple of 4 bytes */
   1467 	/* XXX verify mbuf data area covers this roundup */
   1468 	if (!ath_hal_filltxdesc(ah, ds
   1469 		, roundup(bf->bf_segs[0].ds_len, 4)	/* buffer length */
   1470 		, AH_TRUE				/* first segment */
   1471 		, AH_TRUE				/* last segment */
   1472 	)) {
   1473 		printf("%s: ath_hal_filltxdesc failed\n", __func__);
   1474 		return -1;
   1475 	}
   1476 
   1477 	/* XXX it is not appropriate to bus_dmamap_sync? -dcy */
   1478 
   1479 	return 0;
   1480 }
   1481 
   1482 static void
   1483 ath_beacon_proc(void *arg, int pending)
   1484 {
   1485 	struct ath_softc *sc = arg;
   1486 	struct ieee80211com *ic = &sc->sc_ic;
   1487 	struct ath_buf *bf = sc->sc_bcbuf;
   1488 	struct ath_hal *ah = sc->sc_ah;
   1489 
   1490 	DPRINTF2(("%s: pending %u\n", __func__, pending));
   1491 	if (ic->ic_opmode == IEEE80211_M_STA ||
   1492 	    bf == NULL || bf->bf_m == NULL) {
   1493 		DPRINTF(("%s: ic_flags=%x bf=%p bf_m=%p\n",
   1494 			__func__, ic->ic_flags, bf, bf ? bf->bf_m : NULL));
   1495 		return;
   1496 	}
   1497 	/* TODO: update beacon to reflect PS poll state */
   1498 	if (!ath_hal_stoptxdma(ah, sc->sc_bhalq)) {
   1499 		DPRINTF(("%s: beacon queue %u did not stop?",
   1500 			__func__, sc->sc_bhalq));
   1501 		return;			/* busy, XXX is this right? */
   1502 	}
   1503 	ath_buf_dmamap_sync(sc->sc_dmat, bf, BUS_DMASYNC_PREWRITE);
   1504 
   1505 	ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr);
   1506 	ath_hal_txstart(ah, sc->sc_bhalq);
   1507 	DPRINTF2(("%s: BCDP%u = %p (%p)\n", __func__,
   1508 		sc->sc_bhalq, (caddr_t)bf->bf_daddr, bf->bf_desc));
   1509 }
   1510 
   1511 static void
   1512 ath_beacon_free(struct ath_softc *sc)
   1513 {
   1514 	struct ath_buf *bf = sc->sc_bcbuf;
   1515 
   1516 	if (bf->bf_m != NULL) {
   1517 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
   1518 		m_freem(bf->bf_m);
   1519 		bf->bf_m = NULL;
   1520 		bf->bf_node = NULL;
   1521 	}
   1522 }
   1523 
   1524 /*
   1525  * Configure the beacon and sleep timers.
   1526  *
   1527  * When operating as an AP this resets the TSF and sets
   1528  * up the hardware to notify us when we need to issue beacons.
   1529  *
   1530  * When operating in station mode this sets up the beacon
   1531  * timers according to the timestamp of the last received
   1532  * beacon and the current TSF, configures PCF and DTIM
   1533  * handling, programs the sleep registers so the hardware
   1534  * will wakeup in time to receive beacons, and configures
   1535  * the beacon miss handling so we'll receive a BMISS
   1536  * interrupt when we stop seeing beacons from the AP
   1537  * we've associated with.
   1538  */
   1539 static void
   1540 ath_beacon_config(struct ath_softc *sc)
   1541 {
   1542 	struct ath_hal *ah = sc->sc_ah;
   1543 	struct ieee80211com *ic = &sc->sc_ic;
   1544 	struct ieee80211_node *ni = ic->ic_bss;
   1545 	u_int32_t nexttbtt;
   1546 
   1547 	nexttbtt = (LE_READ_4(ni->ni_tstamp + 4) << 22) |
   1548 	    (LE_READ_4(ni->ni_tstamp) >> 10);
   1549 	DPRINTF(("%s: nexttbtt=%u\n", __func__, nexttbtt));
   1550 	nexttbtt += ni->ni_intval;
   1551 	if (ic->ic_opmode == IEEE80211_M_STA) {
   1552 		HAL_BEACON_STATE bs;
   1553 		u_int32_t bmisstime;
   1554 
   1555 		/* NB: no PCF support right now */
   1556 		memset(&bs, 0, sizeof(bs));
   1557 		bs.bs_intval = ni->ni_intval;
   1558 		bs.bs_nexttbtt = nexttbtt;
   1559 		bs.bs_dtimperiod = bs.bs_intval;
   1560 		bs.bs_nextdtim = nexttbtt;
   1561 		/*
   1562 		 * Calculate the number of consecutive beacons to miss
   1563 		 * before taking a BMISS interrupt.  The configuration
   1564 		 * is specified in ms, so we need to convert that to
   1565 		 * TU's and then calculate based on the beacon interval.
   1566 		 * Note that we clamp the result to at most 10 beacons.
   1567 		 */
   1568 		bmisstime = (ic->ic_bmisstimeout * 1000) / 1024;
   1569 		bs.bs_bmissthreshold = howmany(bmisstime,ni->ni_intval);
   1570 		if (bs.bs_bmissthreshold > 10)
   1571 			bs.bs_bmissthreshold = 10;
   1572 		else if (bs.bs_bmissthreshold <= 0)
   1573 			bs.bs_bmissthreshold = 1;
   1574 
   1575 		/*
   1576 		 * Calculate sleep duration.  The configuration is
   1577 		 * given in ms.  We insure a multiple of the beacon
   1578 		 * period is used.  Also, if the sleep duration is
   1579 		 * greater than the DTIM period then it makes senses
   1580 		 * to make it a multiple of that.
   1581 		 *
   1582 		 * XXX fixed at 100ms
   1583 		 */
   1584 		bs.bs_sleepduration =
   1585 			roundup((100 * 1000) / 1024, bs.bs_intval);
   1586 		if (bs.bs_sleepduration > bs.bs_dtimperiod)
   1587 			bs.bs_sleepduration = roundup(bs.bs_sleepduration, bs.bs_dtimperiod);
   1588 
   1589 		DPRINTF(("%s: intval %u nexttbtt %u dtim %u nextdtim %u bmiss %u sleep %u\n"
   1590 			, __func__
   1591 			, bs.bs_intval
   1592 			, bs.bs_nexttbtt
   1593 			, bs.bs_dtimperiod
   1594 			, bs.bs_nextdtim
   1595 			, bs.bs_bmissthreshold
   1596 			, bs.bs_sleepduration
   1597 		));
   1598 		ath_hal_intrset(ah, 0);
   1599 		/*
   1600 		 * Reset our tsf so the hardware will update the
   1601 		 * tsf register to reflect timestamps found in
   1602 		 * received beacons.
   1603 		 */
   1604 		ath_hal_resettsf(ah);
   1605 		ath_hal_beacontimers(ah, &bs, 0/*XXX*/, 0, 0);
   1606 		sc->sc_imask |= HAL_INT_BMISS;
   1607 		ath_hal_intrset(ah, sc->sc_imask);
   1608 	} else {
   1609 		DPRINTF(("%s: intval %u nexttbtt %u\n",
   1610 			__func__, ni->ni_intval, nexttbtt));
   1611 		ath_hal_intrset(ah, 0);
   1612 		ath_hal_beaconinit(ah, ic->ic_opmode,
   1613 			nexttbtt, ni->ni_intval);
   1614 		if (ic->ic_opmode != IEEE80211_M_MONITOR)
   1615 			sc->sc_imask |= HAL_INT_SWBA;	/* beacon prepare */
   1616 		ath_hal_intrset(ah, sc->sc_imask);
   1617 	}
   1618 }
   1619 
   1620 #ifdef __FreeBSD__
   1621 static void
   1622 ath_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
   1623 {
   1624 	bus_addr_t *paddr = (bus_addr_t*) arg;
   1625 	*paddr = segs->ds_addr;
   1626 }
   1627 #endif
   1628 
   1629 #ifdef __FreeBSD__
   1630 static int
   1631 ath_desc_alloc(struct ath_softc *sc)
   1632 {
   1633 	int i, bsize, error;
   1634 	struct ath_desc *ds;
   1635 	struct ath_buf *bf;
   1636 
   1637 	/* allocate descriptors */
   1638 	sc->sc_desc_len = sizeof(struct ath_desc) *
   1639 				(ATH_TXBUF * ATH_TXDESC + ATH_RXBUF + 1);
   1640 	error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT, &sc->sc_ddmamap);
   1641 	if (error != 0)
   1642 		return error;
   1643 
   1644 	error = bus_dmamem_alloc(sc->sc_dmat, (void**) &sc->sc_desc,
   1645 				 BUS_DMA_NOWAIT, &sc->sc_ddmamap);
   1646 
   1647 	if (error != 0)
   1648 		goto fail0;
   1649 
   1650 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_ddmamap,
   1651 				sc->sc_desc, sc->sc_desc_len,
   1652 				ath_load_cb, &sc->sc_desc_paddr,
   1653 				BUS_DMA_NOWAIT);
   1654 	if (error != 0)
   1655 		goto fail1;
   1656 
   1657 	ds = sc->sc_desc;
   1658 	DPRINTF(("ath_desc_alloc: DMA map: %p (%d) -> %p (%lu)\n",
   1659 	    ds, sc->sc_desc_len,
   1660 	    (caddr_t) sc->sc_desc_paddr, /*XXX*/ (u_long) sc->sc_desc_len));
   1661 
   1662 	/* allocate buffers */
   1663 	bsize = sizeof(struct ath_buf) * (ATH_TXBUF + ATH_RXBUF + 1);
   1664 	bf = malloc(bsize, M_DEVBUF, M_NOWAIT | M_ZERO);
   1665 	if (bf == NULL) {
   1666 		printf("%s: unable to allocate Tx/Rx buffers\n",
   1667 		    sc->sc_dev.dv_xname);
   1668 		error = -1;
   1669 		goto fail2;
   1670 	}
   1671 	sc->sc_bufptr = bf;
   1672 
   1673 	TAILQ_INIT(&sc->sc_rxbuf);
   1674 	for (i = 0; i < ATH_RXBUF; i++, bf++, ds++) {
   1675 		bf->bf_desc = ds;
   1676 		bf->bf_daddr = sc->sc_desc_paddr +
   1677 		    ((caddr_t)ds - (caddr_t)sc->sc_desc);
   1678 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
   1679 					  &bf->bf_dmamap);
   1680 		if (error != 0)
   1681 			break;
   1682 		TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
   1683 	}
   1684 
   1685 	TAILQ_INIT(&sc->sc_txbuf);
   1686 	for (i = 0; i < ATH_TXBUF; i++, bf++, ds += ATH_TXDESC) {
   1687 		bf->bf_desc = ds;
   1688 		bf->bf_daddr = sc->sc_desc_paddr +
   1689 		    ((caddr_t)ds - (caddr_t)sc->sc_desc);
   1690 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
   1691 					  &bf->bf_dmamap);
   1692 		if (error != 0)
   1693 			break;
   1694 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
   1695 	}
   1696 	TAILQ_INIT(&sc->sc_txq);
   1697 
   1698 	/* beacon buffer */
   1699 	bf->bf_desc = ds;
   1700 	bf->bf_daddr = sc->sc_desc_paddr + ((caddr_t)ds - (caddr_t)sc->sc_desc);
   1701 	error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT, &bf->bf_dmamap);
   1702 	if (error != 0)
   1703 		return error;
   1704 	sc->sc_bcbuf = bf;
   1705 	return 0;
   1706 
   1707 fail2:
   1708 	bus_dmamap_unload(sc->sc_dmat, sc->sc_ddmamap);
   1709 fail1:
   1710 	bus_dmamem_free(sc->sc_dmat, sc->sc_desc, sc->sc_ddmamap);
   1711 fail0:
   1712 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_ddmamap);
   1713 	sc->sc_ddmamap = NULL;
   1714 	return error;
   1715 }
   1716 #else
   1717 static int
   1718 ath_desc_alloc(struct ath_softc *sc)
   1719 {
   1720 	int i, bsize, error = -1;
   1721 	struct ath_desc *ds;
   1722 	struct ath_buf *bf;
   1723 
   1724 	/* allocate descriptors */
   1725 	sc->sc_desc_len = sizeof(struct ath_desc) *
   1726 				(ATH_TXBUF * ATH_TXDESC + ATH_RXBUF + 1);
   1727 	if ((error = bus_dmamem_alloc(sc->sc_dmat, sc->sc_desc_len, PAGE_SIZE,
   1728 	    0, &sc->sc_dseg, 1, &sc->sc_dnseg, 0)) != 0) {
   1729 		printf("%s: unable to allocate control data, error = %d\n",
   1730 		    sc->sc_dev.dv_xname, error);
   1731 		goto fail0;
   1732 	}
   1733 
   1734 	if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_dseg, sc->sc_dnseg,
   1735 	    sc->sc_desc_len, (caddr_t *)&sc->sc_desc, BUS_DMA_COHERENT)) != 0) {
   1736 		printf("%s: unable to map control data, error = %d\n",
   1737 		    sc->sc_dev.dv_xname, error);
   1738 		goto fail1;
   1739 	}
   1740 
   1741 	if ((error = bus_dmamap_create(sc->sc_dmat, sc->sc_desc_len, 1,
   1742 	    sc->sc_desc_len, 0, 0, &sc->sc_ddmamap)) != 0) {
   1743 		printf("%s: unable to create control data DMA map, "
   1744 		    "error = %d\n", sc->sc_dev.dv_xname, error);
   1745 		goto fail2;
   1746 	}
   1747 
   1748 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_ddmamap, sc->sc_desc,
   1749 	    sc->sc_desc_len, NULL, 0)) != 0) {
   1750 		printf("%s: unable to load control data DMA map, error = %d\n",
   1751 		    sc->sc_dev.dv_xname, error);
   1752 		goto fail3;
   1753 	}
   1754 
   1755 	ds = sc->sc_desc;
   1756 	sc->sc_desc_paddr = sc->sc_ddmamap->dm_segs[0].ds_addr;
   1757 
   1758 	DPRINTF(("ath_desc_alloc: DMA map: %p (%lu) -> %p (%lu)\n",
   1759 	    ds, (u_long)sc->sc_desc_len,
   1760 	    (caddr_t) sc->sc_desc_paddr, /*XXX*/ (u_long) sc->sc_desc_len));
   1761 
   1762 	/* allocate buffers */
   1763 	bsize = sizeof(struct ath_buf) * (ATH_TXBUF + ATH_RXBUF + 1);
   1764 	bf = malloc(bsize, M_DEVBUF, M_NOWAIT | M_ZERO);
   1765 	if (bf == NULL) {
   1766 		printf("%s: unable to allocate Tx/Rx buffers\n",
   1767 		    sc->sc_dev.dv_xname);
   1768 		error = ENOMEM;
   1769 		goto fail3;
   1770 	}
   1771 	sc->sc_bufptr = bf;
   1772 
   1773 	TAILQ_INIT(&sc->sc_rxbuf);
   1774 	for (i = 0; i < ATH_RXBUF; i++, bf++, ds++) {
   1775 		bf->bf_desc = ds;
   1776 		bf->bf_daddr = sc->sc_desc_paddr +
   1777 		    ((caddr_t)ds - (caddr_t)sc->sc_desc);
   1778 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
   1779 		    MCLBYTES, 0, 0, &bf->bf_dmamap)) != 0) {
   1780 			printf("%s: unable to create Rx dmamap, error = %d\n",
   1781 			    sc->sc_dev.dv_xname, error);
   1782 			goto fail4;
   1783 		}
   1784 		TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
   1785 	}
   1786 
   1787 	TAILQ_INIT(&sc->sc_txbuf);
   1788 	for (i = 0; i < ATH_TXBUF; i++, bf++, ds += ATH_TXDESC) {
   1789 		bf->bf_desc = ds;
   1790 		bf->bf_daddr = sc->sc_desc_paddr +
   1791 		    ((caddr_t)ds - (caddr_t)sc->sc_desc);
   1792 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
   1793 		    ATH_TXDESC, MCLBYTES, 0, 0, &bf->bf_dmamap)) != 0) {
   1794 			printf("%s: unable to create Tx dmamap, error = %d\n",
   1795 			    sc->sc_dev.dv_xname, error);
   1796 			goto fail5;
   1797 		}
   1798 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
   1799 	}
   1800 	TAILQ_INIT(&sc->sc_txq);
   1801 
   1802 	/* beacon buffer */
   1803 	bf->bf_desc = ds;
   1804 	bf->bf_daddr = sc->sc_desc_paddr + ((caddr_t)ds - (caddr_t)sc->sc_desc);
   1805 	if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, 0, 0,
   1806 	    &bf->bf_dmamap)) != 0) {
   1807 		printf("%s: unable to create beacon dmamap, error = %d\n",
   1808 		    sc->sc_dev.dv_xname, error);
   1809 		goto fail5;
   1810 	}
   1811 	sc->sc_bcbuf = bf;
   1812 	return 0;
   1813 
   1814 fail5:
   1815 	for (i = ATH_RXBUF; i < ATH_RXBUF + ATH_TXBUF; i++) {
   1816 		if (sc->sc_bufptr[i].bf_dmamap == NULL)
   1817 			continue;
   1818 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_bufptr[i].bf_dmamap);
   1819 	}
   1820 fail4:
   1821 	for (i = 0; i < ATH_RXBUF; i++) {
   1822 		if (sc->sc_bufptr[i].bf_dmamap == NULL)
   1823 			continue;
   1824 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_bufptr[i].bf_dmamap);
   1825 	}
   1826 fail3:
   1827 	bus_dmamap_unload(sc->sc_dmat, sc->sc_ddmamap);
   1828 fail2:
   1829 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_ddmamap);
   1830 	sc->sc_ddmamap = NULL;
   1831 fail1:
   1832 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_desc, sc->sc_desc_len);
   1833 fail0:
   1834 	bus_dmamem_free(sc->sc_dmat, &sc->sc_dseg, sc->sc_dnseg);
   1835 	return error;
   1836 }
   1837 #endif
   1838 
   1839 static void
   1840 ath_desc_free(struct ath_softc *sc)
   1841 {
   1842 	struct ath_buf *bf;
   1843 
   1844 #ifdef __FreeBSD__
   1845 	bus_dmamap_unload(sc->sc_dmat, sc->sc_ddmamap);
   1846 	bus_dmamem_free(sc->sc_dmat, sc->sc_desc, sc->sc_ddmamap);
   1847 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_ddmamap);
   1848 #else
   1849 	bus_dmamap_unload(sc->sc_dmat, sc->sc_ddmamap);
   1850 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_ddmamap);
   1851 	bus_dmamem_free(sc->sc_dmat, &sc->sc_dseg, sc->sc_dnseg);
   1852 #endif
   1853 
   1854 	TAILQ_FOREACH(bf, &sc->sc_txq, bf_list) {
   1855 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
   1856 		bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
   1857 		m_freem(bf->bf_m);
   1858 	}
   1859 	TAILQ_FOREACH(bf, &sc->sc_txbuf, bf_list)
   1860 		bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
   1861 	TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
   1862 		if (bf->bf_m) {
   1863 			bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
   1864 			bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
   1865 			m_freem(bf->bf_m);
   1866 			bf->bf_m = NULL;
   1867 		}
   1868 	}
   1869 	if (sc->sc_bcbuf != NULL) {
   1870 		bus_dmamap_unload(sc->sc_dmat, sc->sc_bcbuf->bf_dmamap);
   1871 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_bcbuf->bf_dmamap);
   1872 		sc->sc_bcbuf = NULL;
   1873 	}
   1874 
   1875 	TAILQ_INIT(&sc->sc_rxbuf);
   1876 	TAILQ_INIT(&sc->sc_txbuf);
   1877 	TAILQ_INIT(&sc->sc_txq);
   1878 	free(sc->sc_bufptr, M_DEVBUF);
   1879 	sc->sc_bufptr = NULL;
   1880 }
   1881 
   1882 static struct ieee80211_node *
   1883 ath_node_alloc(struct ieee80211com *ic)
   1884 {
   1885 	struct ath_node *an =
   1886 		malloc(sizeof(struct ath_node), M_DEVBUF, M_NOWAIT | M_ZERO);
   1887 	return an ? &an->an_node : NULL;
   1888 }
   1889 
   1890 static void
   1891 ath_node_free(struct ieee80211com *ic, struct ieee80211_node *ni)
   1892 {
   1893 	struct ath_softc *sc = ic->ic_if.if_softc;
   1894 	struct ath_buf *bf;
   1895 
   1896 	TAILQ_FOREACH(bf, &sc->sc_txq, bf_list) {
   1897 		if (bf->bf_node == ni)
   1898 			bf->bf_node = NULL;
   1899 	}
   1900 	free(ni, M_DEVBUF);
   1901 }
   1902 
   1903 static void
   1904 ath_node_copy(struct ieee80211com *ic,
   1905 	struct ieee80211_node *dst, const struct ieee80211_node *src)
   1906 {
   1907 	*(struct ath_node *)dst = *(const struct ath_node *)src;
   1908 }
   1909 
   1910 static int
   1911 ath_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf)
   1912 {
   1913 	struct ath_hal *ah = sc->sc_ah;
   1914 	int error;
   1915 	struct mbuf *m;
   1916 	struct ath_desc *ds;
   1917 
   1918 	m = bf->bf_m;
   1919 	if (m == NULL) {
   1920 		/*
   1921 		 * NB: by assigning a page to the rx dma buffer we
   1922 		 * implicitly satisfy the Atheros requirement that
   1923 		 * this buffer be cache-line-aligned and sized to be
   1924 		 * multiple of the cache line size.  Not doing this
   1925 		 * causes weird stuff to happen (for the 5210 at least).
   1926 		 */
   1927 		m = ath_getmbuf(M_DONTWAIT, MT_DATA, MCLBYTES);
   1928 		if (m == NULL) {
   1929 			DPRINTF(("ath_rxbuf_init: no mbuf/cluster\n"));
   1930 			sc->sc_stats.ast_rx_nombuf++;
   1931 			return ENOMEM;
   1932 		}
   1933 		bf->bf_m = m;
   1934 		m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
   1935 
   1936 		error = ath_buf_dmamap_load_mbuf(sc->sc_dmat, bf, m,
   1937 		                                 BUS_DMA_NOWAIT);
   1938 		if (error != 0) {
   1939 			DPRINTF(("ath_rxbuf_init: ath_buf_dmamap_load_mbuf failed;"
   1940 				" error %d\n", error));
   1941 			sc->sc_stats.ast_rx_busdma++;
   1942 			return error;
   1943 		}
   1944 		KASSERT(bf->bf_nseg == 1,
   1945 			("ath_rxbuf_init: multi-segment packet; nseg %u",
   1946 			bf->bf_nseg));
   1947 	}
   1948 	ath_buf_dmamap_sync(sc->sc_dmat, bf, BUS_DMASYNC_PREREAD);
   1949 
   1950 	/* setup descriptors */
   1951 	ds = bf->bf_desc;
   1952 	ds->ds_link = 0;
   1953 	ds->ds_data = bf->bf_segs[0].ds_addr;
   1954 	ath_hal_setuprxdesc(ah, ds
   1955 		, m->m_len		/* buffer size */
   1956 		, 0
   1957 	);
   1958 
   1959 	if (sc->sc_rxlink != NULL)
   1960 		*sc->sc_rxlink = bf->bf_daddr;
   1961 	sc->sc_rxlink = &ds->ds_link;
   1962 	return 0;
   1963 }
   1964 
   1965 static void
   1966 ath_rx_proc(void *arg, int npending)
   1967 {
   1968 	struct ath_softc *sc = arg;
   1969 	struct ath_buf *bf;
   1970 	struct ieee80211com *ic = &sc->sc_ic;
   1971 	struct ifnet *ifp = &ic->ic_if;
   1972 	struct ath_hal *ah = sc->sc_ah;
   1973 	struct ath_desc *ds;
   1974 	struct mbuf *m;
   1975 	struct ieee80211_frame *wh, whbuf;
   1976 	struct ieee80211_node *ni;
   1977 	int len;
   1978 	u_int phyerr;
   1979 	HAL_STATUS status;
   1980 
   1981 	DPRINTF2(("ath_rx_proc: pending %u\n", npending));
   1982 	do {
   1983 		bf = TAILQ_FIRST(&sc->sc_rxbuf);
   1984 		if (bf == NULL) {		/* NB: shouldn't happen */
   1985 			if_printf(ifp, "ath_rx_proc: no buffer!\n");
   1986 			break;
   1987 		}
   1988 		m = bf->bf_m;
   1989 		if (m == NULL) {		/* NB: shouldn't happen */
   1990 			if_printf(ifp, "ath_rx_proc: no mbuf!\n");
   1991 			continue;
   1992 		}
   1993 		ds = bf->bf_desc;
   1994 		status = ath_hal_rxprocdesc(ah, ds);
   1995 #ifdef AR_DEBUG
   1996 		if (ath_debug > 1)
   1997 			ath_printrxbuf(bf, status == HAL_OK);
   1998 #endif
   1999 		if (status == HAL_EINPROGRESS)
   2000 			break;
   2001 		TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
   2002 		if (ds->ds_rxstat.rs_status != 0) {
   2003 			ifp->if_ierrors++;
   2004 			if (ds->ds_rxstat.rs_status & HAL_RXERR_CRC)
   2005 				sc->sc_stats.ast_rx_crcerr++;
   2006 			if (ds->ds_rxstat.rs_status & HAL_RXERR_FIFO)
   2007 				sc->sc_stats.ast_rx_fifoerr++;
   2008 			if (ds->ds_rxstat.rs_status & HAL_RXERR_DECRYPT)
   2009 				sc->sc_stats.ast_rx_badcrypt++;
   2010 			if (ds->ds_rxstat.rs_status & HAL_RXERR_PHY) {
   2011 				sc->sc_stats.ast_rx_phyerr++;
   2012 				phyerr = ds->ds_rxstat.rs_phyerr & 0x1f;
   2013 				sc->sc_stats.ast_rx_phy[phyerr]++;
   2014 			}
   2015 			goto rx_next;
   2016 		}
   2017 
   2018 		len = ds->ds_rxstat.rs_datalen;
   2019 		if (len < sizeof(struct ieee80211_frame)) {
   2020 			DPRINTF(("ath_rx_proc: short packet %d\n", len));
   2021 			sc->sc_stats.ast_rx_tooshort++;
   2022 			goto rx_next;
   2023 		}
   2024 
   2025 		ath_buf_dmamap_sync(sc->sc_dmat, bf, BUS_DMASYNC_POSTREAD);
   2026 
   2027 		wh = mtod(m, struct ieee80211_frame *);
   2028 		if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
   2029 		    IEEE80211_FC0_TYPE_CTL &&
   2030 		    ic->ic_opmode != IEEE80211_M_MONITOR) {
   2031 			/*
   2032 			 * Discard control frame when not in monitor mode.
   2033 			 */
   2034 			DPRINTF(("ath_rx_proc: control frame\n"));
   2035 			sc->sc_stats.ast_rx_ctl++;
   2036 			goto rx_next;
   2037 		}
   2038 
   2039 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
   2040 		bf->bf_m = NULL;
   2041 		m->m_pkthdr.rcvif = ifp;
   2042 		m->m_pkthdr.len = m->m_len = len;
   2043 
   2044 #if NBPFILTER > 0
   2045 		if (sc->sc_drvbpf) {
   2046 #ifdef __FreeBSD__
   2047 			struct mbuf *mb;
   2048 
   2049 			/* XXX pre-allocate space when setting up recv's */
   2050 			MGETHDR(mb, M_DONTWAIT, m->m_type);
   2051 			if (mb != NULL) {
   2052 				sc->sc_rx_th.wr_rate =
   2053 					sc->sc_hwmap[ds->ds_rxstat.rs_rate];
   2054 				sc->sc_rx_th.wr_antsignal =
   2055 					ds->ds_rxstat.rs_rssi;
   2056 				sc->sc_rx_th.wr_antenna =
   2057 					ds->ds_rxstat.rs_antenna;
   2058 				/* XXX TSF */
   2059 
   2060 				(void) m_dup_pkthdr(mb, m, M_DONTWAIT);
   2061 				mb->m_next = m;
   2062 				mb->m_data = (caddr_t)&sc->sc_rx_th;
   2063 				mb->m_len = sizeof(sc->sc_rx_th);
   2064 				mb->m_pkthdr.len += mb->m_len;
   2065 				bpf_mtap(sc->sc_drvbpf, mb);
   2066 				m_free(mb);
   2067 			}
   2068 #else
   2069 			/* XXX pre-allocate space when setting up recv's */
   2070 			struct mbuf mb;
   2071 
   2072 			sc->sc_rx_th.wr_rate =
   2073 				sc->sc_hwmap[ds->ds_rxstat.rs_rate];
   2074 			sc->sc_rx_th.wr_antsignal =
   2075 				ds->ds_rxstat.rs_rssi;
   2076 			sc->sc_rx_th.wr_antenna =
   2077 				ds->ds_rxstat.rs_antenna;
   2078 			/* XXX TSF */
   2079 
   2080 			M_COPY_PKTHDR(&mb, m);
   2081 			mb.m_next = m;
   2082 			mb.m_data = (caddr_t)&sc->sc_rx_th;
   2083 			mb.m_len = sizeof(sc->sc_rx_th);
   2084 			mb.m_pkthdr.len += mb.m_len;
   2085 			bpf_mtap(sc->sc_drvbpf, &mb);
   2086 #endif
   2087 		}
   2088 #endif
   2089 
   2090 		m_adj(m, -IEEE80211_CRC_LEN);
   2091 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
   2092 			/*
   2093 			 * WEP is decrypted by hardware. Clear WEP bit
   2094 			 * and trim WEP header for ieee80211_input().
   2095 			 */
   2096 			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
   2097 			memcpy(&whbuf, wh, sizeof(whbuf));
   2098 			m_adj(m, IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN);
   2099 			memcpy(mtod(m, caddr_t), &whbuf, sizeof(whbuf));
   2100 			/*
   2101 			 * Also trim WEP ICV from the tail.
   2102 			 */
   2103 			m_adj(m, -IEEE80211_WEP_CRCLEN);
   2104 			/*
   2105 			 * The header has probably moved.
   2106 			 */
   2107 			wh = mtod(m, struct ieee80211_frame *);
   2108 		}
   2109 
   2110 		/*
   2111 		 * Locate the node for sender, track state, and
   2112 		 * then pass this node (referenced) up to the 802.11
   2113 		 * layer for its use.  We are required to pass
   2114 		 * something so we fall back to ic_bss when this frame
   2115 		 * is from an unknown sender.
   2116 		 */
   2117 		ni = ieee80211_find_rxnode(ic, wh);
   2118 		ATH_NODE(ni)->an_rx_antenna = ds->ds_rxstat.rs_antenna;
   2119 		/*
   2120 		 * Send frame up for processing.
   2121 		 */
   2122 		ieee80211_input(ifp, m, ni,
   2123 			ds->ds_rxstat.rs_rssi, ds->ds_rxstat.rs_tstamp);
   2124 		/*
   2125 		 * The frame may have caused the node to be marked for
   2126 		 * reclamation (e.g. in response to a DEAUTH message)
   2127 		 * so use free_node here instead of unref_node.
   2128 		 */
   2129 		if (ni == ic->ic_bss)
   2130 			ieee80211_unref_node(&ni);
   2131 		else
   2132 			ieee80211_free_node(ic, ni);
   2133   rx_next:
   2134 		TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
   2135 	} while (ath_rxbuf_init(sc, bf) == 0);
   2136 
   2137 	ath_hal_rxmonitor(ah);			/* rx signal state monitoring */
   2138 	ath_hal_rxena(ah);			/* in case of RXEOL */
   2139 }
   2140 
   2141 /*
   2142  * XXX Size of an ACK control frame in bytes.
   2143  */
   2144 #define	IEEE80211_ACK_SIZE	(2+2+IEEE80211_ADDR_LEN+4)
   2145 
   2146 static int
   2147 ath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni, struct ath_buf *bf,
   2148     struct mbuf *m0)
   2149 {
   2150 	struct ieee80211com *ic = &sc->sc_ic;
   2151 	struct ath_hal *ah = sc->sc_ah;
   2152 	struct ifnet *ifp = &sc->sc_ic.ic_if;
   2153 	int i, error, iswep, hdrlen, pktlen;
   2154 	u_int8_t rix, cix, txrate, ctsrate;
   2155 	struct ath_desc *ds;
   2156 	struct mbuf *m;
   2157 	struct ieee80211_frame *wh;
   2158 	u_int32_t iv;
   2159 	u_int8_t *ivp;
   2160 	u_int8_t hdrbuf[sizeof(struct ieee80211_frame) +
   2161 	    IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN];
   2162 	u_int subtype, flags, ctsduration, antenna;
   2163 	HAL_PKT_TYPE atype;
   2164 	const HAL_RATE_TABLE *rt;
   2165 	HAL_BOOL shortPreamble;
   2166 	struct ath_node *an;
   2167 	ath_txq_critsect_decl(s);
   2168 
   2169 	wh = mtod(m0, struct ieee80211_frame *);
   2170 	iswep = wh->i_fc[1] & IEEE80211_FC1_WEP;
   2171 	hdrlen = sizeof(struct ieee80211_frame);
   2172 	pktlen = m0->m_pkthdr.len;
   2173 
   2174 	if (iswep) {
   2175 		memcpy(hdrbuf, mtod(m0, caddr_t), hdrlen);
   2176 		m_adj(m0, hdrlen);
   2177 		M_PREPEND(m0, sizeof(hdrbuf), M_DONTWAIT);
   2178 		if (m0 == NULL) {
   2179 			sc->sc_stats.ast_tx_nombuf++;
   2180 			return ENOMEM;
   2181 		}
   2182 		ivp = hdrbuf + hdrlen;
   2183 		wh = mtod(m0, struct ieee80211_frame *);
   2184 		/*
   2185 		 * XXX
   2186 		 * IV must not duplicate during the lifetime of the key.
   2187 		 * But no mechanism to renew keys is defined in IEEE 802.11
   2188 		 * WEP.  And IV may be duplicated between other stations
   2189 		 * because of the session key itself is shared.
   2190 		 * So we use pseudo random IV for now, though it is not the
   2191 		 * right way.
   2192 		 */
   2193 		iv = arc4random();
   2194 		for (i = 0; i < IEEE80211_WEP_IVLEN; i++) {
   2195 			ivp[i] = iv;
   2196 			iv >>= 8;
   2197 		}
   2198 		ivp[i] = sc->sc_ic.ic_wep_txkey << 6;	/* Key ID and pad */
   2199 		memcpy(mtod(m0, caddr_t), hdrbuf, sizeof(hdrbuf));
   2200 		/*
   2201 		 * The ICV length must be included into hdrlen and pktlen.
   2202 		 */
   2203 		hdrlen = sizeof(hdrbuf) + IEEE80211_WEP_CRCLEN;
   2204 		pktlen = m0->m_pkthdr.len + IEEE80211_WEP_CRCLEN;
   2205 	}
   2206 	pktlen += IEEE80211_CRC_LEN;
   2207 
   2208 	/*
   2209 	 * Load the DMA map so any coalescing is done.  This
   2210 	 * also calculates the number of descriptors we need.
   2211 	 */
   2212 	error = ath_buf_dmamap_load_mbuf(sc->sc_dmat, bf, m0, BUS_DMA_NOWAIT);
   2213 	/*
   2214 	 * Discard null packets and check for packets that
   2215 	 * require too many TX descriptors.  We try to convert
   2216 	 * the latter to a cluster.
   2217 	 */
   2218 	if (error == EFBIG) {		/* too many desc's, linearize */
   2219 		sc->sc_stats.ast_tx_linear++;
   2220 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   2221 		if (m == NULL) {
   2222 			sc->sc_stats.ast_tx_nombuf++;
   2223 			m_freem(m0);
   2224 			return ENOMEM;
   2225 		}
   2226 #ifdef __FreeBSD__
   2227 		M_MOVE_PKTHDR(m, m0);
   2228 #else
   2229 		M_COPY_PKTHDR(m, m0);
   2230 #endif
   2231 		MCLGET(m, M_DONTWAIT);
   2232 		if ((m->m_flags & M_EXT) == 0) {
   2233 			sc->sc_stats.ast_tx_nomcl++;
   2234 			m_freem(m0);
   2235 			m_free(m);
   2236 			return ENOMEM;
   2237 		}
   2238 		m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
   2239 		m_freem(m0);
   2240 		m->m_len = m->m_pkthdr.len;
   2241 		m0 = m;
   2242 		error = ath_buf_dmamap_load_mbuf(sc->sc_dmat, bf, m0,
   2243 					         BUS_DMA_NOWAIT);
   2244 		if (error != 0) {
   2245 			sc->sc_stats.ast_tx_busdma++;
   2246 			m_freem(m0);
   2247 			return error;
   2248 		}
   2249 		KASSERT(bf->bf_nseg == 1,
   2250 			("ath_tx_start: packet not one segment; nseg %u",
   2251 			bf->bf_nseg));
   2252 	} else if (error != 0) {
   2253 		sc->sc_stats.ast_tx_busdma++;
   2254 		m_freem(m0);
   2255 		return error;
   2256 	} else if (bf->bf_nseg == 0) {		/* null packet, discard */
   2257 		sc->sc_stats.ast_tx_nodata++;
   2258 		m_freem(m0);
   2259 		return EIO;
   2260 	}
   2261 	DPRINTF2(("ath_tx_start: m %p len %u\n", m0, pktlen));
   2262 	ath_buf_dmamap_sync(sc->sc_dmat, bf, BUS_DMASYNC_PREWRITE);
   2263 	bf->bf_m = m0;
   2264 	bf->bf_node = ni;			/* NB: held reference */
   2265 
   2266 	/* setup descriptors */
   2267 	ds = bf->bf_desc;
   2268 	rt = sc->sc_currates;
   2269 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
   2270 
   2271 	/*
   2272 	 * Calculate Atheros packet type from IEEE80211 packet header
   2273 	 * and setup for rate calculations.
   2274 	 */
   2275 	atype = HAL_PKT_TYPE_NORMAL;			/* default */
   2276 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
   2277 	case IEEE80211_FC0_TYPE_MGT:
   2278 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
   2279 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON)
   2280 			atype = HAL_PKT_TYPE_BEACON;
   2281 		else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
   2282 			atype = HAL_PKT_TYPE_PROBE_RESP;
   2283 		else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM)
   2284 			atype = HAL_PKT_TYPE_ATIM;
   2285 		rix = 0;			/* XXX lowest rate */
   2286 		break;
   2287 	case IEEE80211_FC0_TYPE_CTL:
   2288 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
   2289 		if (subtype == IEEE80211_FC0_SUBTYPE_PS_POLL)
   2290 			atype = HAL_PKT_TYPE_PSPOLL;
   2291 		rix = 0;			/* XXX lowest rate */
   2292 		break;
   2293 	default:
   2294 		rix = sc->sc_rixmap[ni->ni_rates.rs_rates[ni->ni_txrate] &
   2295 				IEEE80211_RATE_VAL];
   2296 		if (rix == 0xff) {
   2297 			if_printf(ifp, "bogus xmit rate 0x%x\n",
   2298 				ni->ni_rates.rs_rates[ni->ni_txrate]);
   2299 			sc->sc_stats.ast_tx_badrate++;
   2300 			m_freem(m0);
   2301 			return EIO;
   2302 		}
   2303 		break;
   2304 	}
   2305 	/*
   2306 	 * NB: the 802.11 layer marks whether or not we should
   2307 	 * use short preamble based on the current mode and
   2308 	 * negotiated parameters.
   2309 	 */
   2310 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) {
   2311 		txrate = rt->info[rix].rateCode | rt->info[rix].shortPreamble;
   2312 		shortPreamble = AH_TRUE;
   2313 		sc->sc_stats.ast_tx_shortpre++;
   2314 	} else {
   2315 		txrate = rt->info[rix].rateCode;
   2316 		shortPreamble = AH_FALSE;
   2317 	}
   2318 
   2319 	/*
   2320 	 * Calculate miscellaneous flags.
   2321 	 */
   2322 	flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for wep errors */
   2323 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
   2324 		flags |= HAL_TXDESC_NOACK;	/* no ack on broad/multicast */
   2325 		sc->sc_stats.ast_tx_noack++;
   2326 	} else if (pktlen > ic->ic_rtsthreshold) {
   2327 		flags |= HAL_TXDESC_RTSENA;	/* RTS based on frame length */
   2328 		sc->sc_stats.ast_tx_rts++;
   2329 	}
   2330 
   2331 	/*
   2332 	 * Calculate RTS/CTS rate and duration if needed.
   2333 	 */
   2334 	ctsduration = 0;
   2335 	if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA)) {
   2336 		/*
   2337 		 * CTS transmit rate is derived from the transmit rate
   2338 		 * by looking in the h/w rate table.  We must also factor
   2339 		 * in whether or not a short preamble is to be used.
   2340 		 */
   2341 		cix = rt->info[rix].controlRate;
   2342 		ctsrate = rt->info[cix].rateCode;
   2343 		if (shortPreamble)
   2344 			ctsrate |= rt->info[cix].shortPreamble;
   2345 		/*
   2346 		 * Compute the transmit duration based on the size
   2347 		 * of an ACK frame.  We call into the HAL to do the
   2348 		 * computation since it depends on the characteristics
   2349 		 * of the actual PHY being used.
   2350 		 */
   2351 		if (flags & HAL_TXDESC_RTSENA) {	/* SIFS + CTS */
   2352 			ctsduration += ath_hal_computetxtime(ah,
   2353 				rt, IEEE80211_ACK_SIZE, cix, shortPreamble);
   2354 		}
   2355 		/* SIFS + data */
   2356 		ctsduration += ath_hal_computetxtime(ah,
   2357 			rt, pktlen, rix, shortPreamble);
   2358 		if ((flags & HAL_TXDESC_NOACK) == 0) {	/* SIFS + ACK */
   2359 			ctsduration += ath_hal_computetxtime(ah,
   2360 				rt, IEEE80211_ACK_SIZE, cix, shortPreamble);
   2361 		}
   2362 	} else
   2363 		ctsrate = 0;
   2364 
   2365 	/*
   2366 	 * For now use the antenna on which the last good
   2367 	 * frame was received on.  We assume this field is
   2368 	 * initialized to 0 which gives us ``auto'' or the
   2369 	 * ``default'' antenna.
   2370 	 */
   2371 	an = (struct ath_node *) ni;
   2372 	if (an->an_tx_antenna)
   2373 		antenna = an->an_tx_antenna;
   2374 	else
   2375 		antenna = an->an_rx_antenna;
   2376 
   2377 	/*
   2378 	 * Formulate first tx descriptor with tx controls.
   2379 	 */
   2380 	/* XXX check return value? */
   2381 	ath_hal_setuptxdesc(ah, ds
   2382 		, pktlen		/* packet length */
   2383 		, hdrlen		/* header length */
   2384 		, atype			/* Atheros packet type */
   2385 		, 60			/* txpower XXX */
   2386 		, txrate, 1+10		/* series 0 rate/tries */
   2387 		, iswep ? sc->sc_ic.ic_wep_txkey : HAL_TXKEYIX_INVALID
   2388 		, antenna		/* antenna mode */
   2389 		, flags			/* flags */
   2390 		, ctsrate		/* rts/cts rate */
   2391 		, ctsduration		/* rts/cts duration */
   2392 	);
   2393 #ifdef notyet
   2394 	ath_hal_setupxtxdesc(ah, ds
   2395 		, AH_FALSE		/* short preamble */
   2396 		, 0, 0			/* series 1 rate/tries */
   2397 		, 0, 0			/* series 2 rate/tries */
   2398 		, 0, 0			/* series 3 rate/tries */
   2399 	);
   2400 #endif
   2401 	/*
   2402 	 * Fillin the remainder of the descriptor info.
   2403 	 */
   2404 	for (i = 0; i < bf->bf_nseg; i++, ds++) {
   2405 		ds->ds_data = bf->bf_segs[i].ds_addr;
   2406 		if (i == bf->bf_nseg - 1)
   2407 			ds->ds_link = 0;
   2408 		else
   2409 			ds->ds_link = bf->bf_daddr + sizeof(*ds) * (i + 1);
   2410 		ath_hal_filltxdesc(ah, ds
   2411 			, bf->bf_segs[i].ds_len	/* segment length */
   2412 			, i == 0		/* first segment */
   2413 			, i == bf->bf_nseg - 1	/* last segment */
   2414 		);
   2415 		DPRINTF2(("ath_tx_start: %d: %08x %08x %08x %08x %08x %08x\n",
   2416 		    i, ds->ds_link, ds->ds_data, ds->ds_ctl0, ds->ds_ctl1,
   2417 		    ds->ds_hw[0], ds->ds_hw[1]));
   2418 	}
   2419 
   2420 	/*
   2421 	 * Insert the frame on the outbound list and
   2422 	 * pass it on to the hardware.
   2423 	 */
   2424 	ath_txq_critsect_begin(sc, s);
   2425 	TAILQ_INSERT_TAIL(&sc->sc_txq, bf, bf_list);
   2426 	if (sc->sc_txlink == NULL) {
   2427 		ath_hal_puttxbuf(ah, sc->sc_txhalq, bf->bf_daddr);
   2428 		DPRINTF2(("ath_tx_start: TXDP0 = %p (%p)\n",
   2429 		    (caddr_t)bf->bf_daddr, bf->bf_desc));
   2430 	} else {
   2431 		*sc->sc_txlink = bf->bf_daddr;
   2432 		DPRINTF2(("ath_tx_start: link(%p)=%p (%p)\n",
   2433 		    sc->sc_txlink, (caddr_t)bf->bf_daddr, bf->bf_desc));
   2434 	}
   2435 	sc->sc_txlink = &bf->bf_desc[bf->bf_nseg - 1].ds_link;
   2436 	ath_txq_critsect_end(sc, s);
   2437 
   2438 	ath_hal_txstart(ah, sc->sc_txhalq);
   2439 	return 0;
   2440 }
   2441 
   2442 static void
   2443 ath_tx_proc(void *arg, int npending)
   2444 {
   2445 	struct ath_softc *sc = arg;
   2446 	struct ath_hal *ah = sc->sc_ah;
   2447 	struct ath_buf *bf;
   2448 	struct ieee80211com *ic = &sc->sc_ic;
   2449 	struct ifnet *ifp = &ic->ic_if;
   2450 	struct ath_desc *ds;
   2451 	struct ieee80211_node *ni;
   2452 	struct ath_node *an;
   2453 	int sr, lr;
   2454 	HAL_STATUS status;
   2455 	ath_txq_critsect_decl(s);
   2456 	ath_txbuf_critsect_decl(s2);
   2457 
   2458 	DPRINTF2(("ath_tx_proc: pending %u tx queue %p, link %p\n",
   2459 		npending, (caddr_t) ath_hal_gettxbuf(sc->sc_ah, sc->sc_txhalq),
   2460 		sc->sc_txlink));
   2461 	for (;;) {
   2462 		ath_txq_critsect_begin(sc, s);
   2463 		bf = TAILQ_FIRST(&sc->sc_txq);
   2464 		if (bf == NULL) {
   2465 			sc->sc_txlink = NULL;
   2466 			ath_txq_critsect_end(sc, s);
   2467 			break;
   2468 		}
   2469 		/* only the last descriptor is needed */
   2470 		ds = &bf->bf_desc[bf->bf_nseg - 1];
   2471 		status = ath_hal_txprocdesc(ah, ds);
   2472 #ifdef AR_DEBUG
   2473 		if (ath_debug > 1)
   2474 			ath_printtxbuf(bf, status == HAL_OK);
   2475 #endif
   2476 		if (status == HAL_EINPROGRESS) {
   2477 			ath_txq_critsect_end(sc, s);
   2478 			break;
   2479 		}
   2480 		TAILQ_REMOVE(&sc->sc_txq, bf, bf_list);
   2481 		ath_txq_critsect_end(sc, s);
   2482 
   2483 		ni = bf->bf_node;
   2484 		if (ni != NULL) {
   2485 			an = (struct ath_node *) ni;
   2486 			if (ds->ds_txstat.ts_status == 0) {
   2487 				an->an_tx_ok++;
   2488 				an->an_tx_antenna = ds->ds_txstat.ts_antenna;
   2489 			} else {
   2490 				an->an_tx_err++;
   2491 				ifp->if_oerrors++;
   2492 				if (ds->ds_txstat.ts_status & HAL_TXERR_XRETRY)
   2493 					sc->sc_stats.ast_tx_xretries++;
   2494 				if (ds->ds_txstat.ts_status & HAL_TXERR_FIFO)
   2495 					sc->sc_stats.ast_tx_fifoerr++;
   2496 				if (ds->ds_txstat.ts_status & HAL_TXERR_FILT)
   2497 					sc->sc_stats.ast_tx_filtered++;
   2498 				an->an_tx_antenna = 0;	/* invalidate */
   2499 			}
   2500 			sr = ds->ds_txstat.ts_shortretry;
   2501 			lr = ds->ds_txstat.ts_longretry;
   2502 			sc->sc_stats.ast_tx_shortretry += sr;
   2503 			sc->sc_stats.ast_tx_longretry += lr;
   2504 			if (sr + lr)
   2505 				an->an_tx_retr++;
   2506 			/*
   2507 			 * Reclaim reference to node.
   2508 			 *
   2509 			 * NB: the node may be reclaimed here if, for example
   2510 			 *     this is a DEAUTH message that was sent and the
   2511 			 *     node was timed out due to inactivity.
   2512 			 */
   2513 			if (ni != ic->ic_bss)
   2514 				ieee80211_free_node(ic, ni);
   2515 		}
   2516 		ath_buf_dmamap_sync(sc->sc_dmat, bf, BUS_DMASYNC_POSTWRITE);
   2517 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
   2518 		m_freem(bf->bf_m);
   2519 		bf->bf_m = NULL;
   2520 		bf->bf_node = NULL;
   2521 
   2522 		ath_txbuf_critsect_begin(sc, s2);
   2523 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
   2524 		ath_txbuf_critsect_end(sc, s2);
   2525 	}
   2526 	ifp->if_flags &= ~IFF_OACTIVE;
   2527 	sc->sc_tx_timer = 0;
   2528 
   2529 	ath_start(ifp);
   2530 }
   2531 
   2532 /*
   2533  * Drain the transmit queue and reclaim resources.
   2534  */
   2535 static void
   2536 ath_draintxq(struct ath_softc *sc)
   2537 {
   2538 	struct ath_hal *ah = sc->sc_ah;
   2539 	struct ifnet *ifp = &sc->sc_ic.ic_if;
   2540 	struct ath_buf *bf;
   2541 	ath_txq_critsect_decl(s);
   2542 	ath_txbuf_critsect_decl(s2);
   2543 
   2544 	/* XXX return value */
   2545 	if (!sc->sc_invalid) {
   2546 		/* don't touch the hardware if marked invalid */
   2547 		(void) ath_hal_stoptxdma(ah, sc->sc_txhalq);
   2548 		DPRINTF(("ath_draintxq: tx queue %p, link %p\n",
   2549 		    (caddr_t) ath_hal_gettxbuf(ah, sc->sc_txhalq),
   2550 		    sc->sc_txlink));
   2551 		(void) ath_hal_stoptxdma(ah, sc->sc_bhalq);
   2552 		DPRINTF(("ath_draintxq: beacon queue %p\n",
   2553 		    (caddr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq)));
   2554 	}
   2555 	for (;;) {
   2556 		ath_txq_critsect_begin(sc, s);
   2557 		bf = TAILQ_FIRST(&sc->sc_txq);
   2558 		if (bf == NULL) {
   2559 			sc->sc_txlink = NULL;
   2560 			ath_txq_critsect_end(sc, s);
   2561 			break;
   2562 		}
   2563 		TAILQ_REMOVE(&sc->sc_txq, bf, bf_list);
   2564 		ath_txq_critsect_end(sc, s);
   2565 #ifdef AR_DEBUG
   2566 		if (ath_debug)
   2567 			ath_printtxbuf(bf,
   2568 				ath_hal_txprocdesc(ah, bf->bf_desc) == HAL_OK);
   2569 #endif /* AR_DEBUG */
   2570 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
   2571 		m_freem(bf->bf_m);
   2572 		bf->bf_m = NULL;
   2573 		bf->bf_node = NULL;
   2574 		ath_txbuf_critsect_begin(sc, s2);
   2575 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
   2576 		ath_txbuf_critsect_end(sc, s2);
   2577 	}
   2578 	ifp->if_flags &= ~IFF_OACTIVE;
   2579 	sc->sc_tx_timer = 0;
   2580 }
   2581 
   2582 /*
   2583  * Disable the receive h/w in preparation for a reset.
   2584  */
   2585 static void
   2586 ath_stoprecv(struct ath_softc *sc)
   2587 {
   2588 	struct ath_hal *ah = sc->sc_ah;
   2589 
   2590 	ath_hal_stoppcurecv(ah);	/* disable PCU */
   2591 	ath_hal_setrxfilter(ah, 0);	/* clear recv filter */
   2592 	ath_hal_stopdmarecv(ah);	/* disable DMA engine */
   2593 	DELAY(3000);			/* long enough for 1 frame */
   2594 #ifdef AR_DEBUG
   2595 	if (ath_debug) {
   2596 		struct ath_buf *bf;
   2597 
   2598 		DPRINTF(("ath_stoprecv: rx queue %p, link %p\n",
   2599 		    (caddr_t) ath_hal_getrxbuf(ah), sc->sc_rxlink));
   2600 		TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
   2601 			if (ath_hal_rxprocdesc(ah, bf->bf_desc) == HAL_OK)
   2602 				ath_printrxbuf(bf, 1);
   2603 		}
   2604 	}
   2605 #endif
   2606 	sc->sc_rxlink = NULL;		/* just in case */
   2607 }
   2608 
   2609 /*
   2610  * Enable the receive h/w following a reset.
   2611  */
   2612 static int
   2613 ath_startrecv(struct ath_softc *sc)
   2614 {
   2615 	struct ath_hal *ah = sc->sc_ah;
   2616 	struct ath_buf *bf;
   2617 
   2618 	sc->sc_rxlink = NULL;
   2619 	TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
   2620 		int error = ath_rxbuf_init(sc, bf);
   2621 		if (error != 0) {
   2622 			DPRINTF(("ath_startrecv: ath_rxbuf_init failed %d\n",
   2623 				error));
   2624 			return error;
   2625 		}
   2626 	}
   2627 
   2628 	bf = TAILQ_FIRST(&sc->sc_rxbuf);
   2629 	ath_hal_putrxbuf(ah, bf->bf_daddr);
   2630 	ath_hal_rxena(ah);		/* enable recv descriptors */
   2631 	ath_mode_init(sc);		/* set filters, etc. */
   2632 	ath_hal_startpcurecv(ah);	/* re-enable PCU/DMA engine */
   2633 	return 0;
   2634 }
   2635 
   2636 /*
   2637  * Set/change channels.  If the channel is really being changed,
   2638  * it's done by resetting the chip.  To accomplish this we must
   2639  * first cleanup any pending DMA, then restart stuff after a la
   2640  * ath_init.
   2641  */
   2642 static int
   2643 ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan)
   2644 {
   2645 	struct ath_hal *ah = sc->sc_ah;
   2646 	struct ieee80211com *ic = &sc->sc_ic;
   2647 
   2648 	DPRINTF(("ath_chan_set: %u (%u MHz) -> %u (%u MHz)\n",
   2649 	    ieee80211_chan2ieee(ic, ic->ic_ibss_chan),
   2650 		ic->ic_ibss_chan->ic_freq,
   2651 	    ieee80211_chan2ieee(ic, chan), chan->ic_freq));
   2652 	if (chan != ic->ic_ibss_chan) {
   2653 		HAL_STATUS status;
   2654 		HAL_CHANNEL hchan;
   2655 		enum ieee80211_phymode mode;
   2656 
   2657 		/*
   2658 		 * To switch channels clear any pending DMA operations;
   2659 		 * wait long enough for the RX fifo to drain, reset the
   2660 		 * hardware at the new frequency, and then re-enable
   2661 		 * the relevant bits of the h/w.
   2662 		 */
   2663 		ath_hal_intrset(ah, 0);		/* disable interrupts */
   2664 		ath_draintxq(sc);		/* clear pending tx frames */
   2665 		ath_stoprecv(sc);		/* turn off frame recv */
   2666 		/*
   2667 		 * Convert to a HAL channel description with
   2668 		 * the flags constrained to reflect the current
   2669 		 * operating mode.
   2670 		 */
   2671 		hchan.channel = chan->ic_freq;
   2672 		hchan.channelFlags = ath_chan2flags(ic, chan);
   2673 		if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_TRUE, &status)) {
   2674 			if_printf(&ic->ic_if, "ath_chan_set: unable to reset "
   2675 				"channel %u (%u Mhz)\n",
   2676 				ieee80211_chan2ieee(ic, chan), chan->ic_freq);
   2677 			return EIO;
   2678 		}
   2679 		/*
   2680 		 * Re-enable rx framework.
   2681 		 */
   2682 		if (ath_startrecv(sc) != 0) {
   2683 			if_printf(&ic->ic_if,
   2684 				"ath_chan_set: unable to restart recv logic\n");
   2685 			return EIO;
   2686 		}
   2687 
   2688 		/*
   2689 		 * Update BPF state.
   2690 		 */
   2691 		sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
   2692 			htole16(chan->ic_freq);
   2693 		sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =
   2694 			htole16(chan->ic_flags);
   2695 
   2696 		/*
   2697 		 * Change channels and update the h/w rate map
   2698 		 * if we're switching; e.g. 11a to 11b/g.
   2699 		 */
   2700 		ic->ic_ibss_chan = chan;
   2701 		mode = ieee80211_chan2mode(ic, chan);
   2702 		if (mode != sc->sc_curmode)
   2703 			ath_setcurmode(sc, mode);
   2704 
   2705 		/*
   2706 		 * Re-enable interrupts.
   2707 		 */
   2708 		ath_hal_intrset(ah, sc->sc_imask);
   2709 	}
   2710 	return 0;
   2711 }
   2712 
   2713 static void
   2714 ath_next_scan(void *arg)
   2715 {
   2716 	struct ath_softc *sc = arg;
   2717 	struct ieee80211com *ic = &sc->sc_ic;
   2718 	struct ifnet *ifp = &ic->ic_if;
   2719 	int s;
   2720 
   2721 	/* don't call ath_start w/o network interrupts blocked */
   2722 	s = splnet();
   2723 
   2724 	if (ic->ic_state == IEEE80211_S_SCAN)
   2725 		ieee80211_next_scan(ifp);
   2726 	splx(s);
   2727 }
   2728 
   2729 /*
   2730  * Periodically recalibrate the PHY to account
   2731  * for temperature/environment changes.
   2732  */
   2733 static void
   2734 ath_calibrate(void *arg)
   2735 {
   2736 	struct ath_softc *sc = arg;
   2737 	struct ath_hal *ah = sc->sc_ah;
   2738 	struct ieee80211com *ic = &sc->sc_ic;
   2739 	struct ieee80211_channel *c;
   2740 	HAL_CHANNEL hchan;
   2741 
   2742 	sc->sc_stats.ast_per_cal++;
   2743 
   2744 	/*
   2745 	 * Convert to a HAL channel description with the flags
   2746 	 * constrained to reflect the current operating mode.
   2747 	 */
   2748 	c = ic->ic_ibss_chan;
   2749 	hchan.channel = c->ic_freq;
   2750 	hchan.channelFlags = ath_chan2flags(ic, c);
   2751 
   2752 	DPRINTF(("%s: channel %u/%x\n", __func__, c->ic_freq, c->ic_flags));
   2753 
   2754 	if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) {
   2755 		/*
   2756 		 * Rfgain is out of bounds, reset the chip
   2757 		 * to load new gain values.
   2758 		 */
   2759 		sc->sc_stats.ast_per_rfgain++;
   2760 		ath_reset(sc);
   2761 	}
   2762 	if (!ath_hal_calibrate(ah, &hchan)) {
   2763 		DPRINTF(("%s: calibration of channel %u failed\n",
   2764 			__func__, c->ic_freq));
   2765 		sc->sc_stats.ast_per_calfail++;
   2766 	}
   2767 	callout_reset(&sc->sc_cal_ch, hz * ath_calinterval, ath_calibrate, sc);
   2768 }
   2769 
   2770 static HAL_LED_STATE
   2771 ath_state_to_led(enum ieee80211_state state)
   2772 {
   2773 	switch (state) {
   2774 	case IEEE80211_S_INIT:
   2775 		return HAL_LED_INIT;
   2776 	case IEEE80211_S_SCAN:
   2777 		return HAL_LED_SCAN;
   2778 	case IEEE80211_S_AUTH:
   2779 		return HAL_LED_AUTH;
   2780 	case IEEE80211_S_ASSOC:
   2781 		return HAL_LED_ASSOC;
   2782 	case IEEE80211_S_RUN:
   2783 		return HAL_LED_RUN;
   2784 	default:
   2785 		panic("%s: unknown 802.11 state %d\n", __func__, state);
   2786 		return HAL_LED_INIT;
   2787 	}
   2788 }
   2789 
   2790 static int
   2791 ath_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
   2792 {
   2793 	struct ifnet *ifp = &ic->ic_if;
   2794 	struct ath_softc *sc = ifp->if_softc;
   2795 	struct ath_hal *ah = sc->sc_ah;
   2796 	struct ieee80211_node *ni;
   2797 	int i, error;
   2798 	u_int8_t *bssid;
   2799 	u_int32_t rfilt;
   2800 
   2801 	DPRINTF(("%s: %s -> %s\n", __func__,
   2802 		ieee80211_state_name[ic->ic_state],
   2803 		ieee80211_state_name[nstate]));
   2804 
   2805 	ath_hal_setledstate(ah, ath_state_to_led(nstate));	/* set LED */
   2806 
   2807 	if (nstate == IEEE80211_S_INIT) {
   2808 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
   2809 		ath_hal_intrset(ah, sc->sc_imask);
   2810 		callout_stop(&sc->sc_scan_ch);
   2811 		callout_stop(&sc->sc_cal_ch);
   2812 		return (*sc->sc_newstate)(ic, nstate, arg);
   2813 	}
   2814 	ni = ic->ic_bss;
   2815 	error = ath_chan_set(sc, ni->ni_chan);
   2816 	if (error != 0)
   2817 		goto bad;
   2818 	rfilt = (ath_hal_getrxfilter(ah) & HAL_RX_FILTER_PHYERR)
   2819 	      | HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST;
   2820 	if (ic->ic_opmode != IEEE80211_M_STA)
   2821 		rfilt |= HAL_RX_FILTER_PROBEREQ;
   2822 	if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
   2823 	    (ifp->if_flags & IFF_PROMISC))
   2824 		rfilt |= HAL_RX_FILTER_PROM;
   2825 	if (nstate == IEEE80211_S_SCAN) {
   2826 		callout_reset(&sc->sc_scan_ch, (hz * ath_dwelltime) / 1000,
   2827 			ath_next_scan, sc);
   2828 		bssid = ifp->if_broadcastaddr;
   2829 		rfilt |= HAL_RX_FILTER_BEACON;
   2830 	} else {
   2831 		callout_stop(&sc->sc_scan_ch);
   2832 		bssid = ni->ni_bssid;
   2833 	}
   2834 	ath_hal_setrxfilter(ah, rfilt);
   2835 	DPRINTF(("%s: RX filter 0x%x bssid %s\n",
   2836 		 __func__, rfilt, ether_sprintf(bssid)));
   2837 
   2838 	if (nstate == IEEE80211_S_RUN && ic->ic_opmode == IEEE80211_M_STA)
   2839 		ath_hal_setassocid(ah, bssid, ni->ni_associd);
   2840 	else
   2841 		ath_hal_setassocid(ah, bssid, 0);
   2842 	if (ic->ic_flags & IEEE80211_F_WEPON) {
   2843 		for (i = 0; i < IEEE80211_WEP_NKID; i++)
   2844 			if (ath_hal_keyisvalid(ah, i))
   2845 				ath_hal_keysetmac(ah, i, bssid);
   2846 	}
   2847 
   2848 	if (nstate == IEEE80211_S_RUN) {
   2849 		DPRINTF(("%s(RUN): ic_flags=0x%08x iv=%d bssid=%s "
   2850 			"capinfo=0x%04x chan=%d\n"
   2851 			 , __func__
   2852 			 , ic->ic_flags
   2853 			 , ni->ni_intval
   2854 			 , ether_sprintf(ni->ni_bssid)
   2855 			 , ni->ni_capinfo
   2856 			 , ieee80211_chan2ieee(ic, ni->ni_chan)));
   2857 
   2858 		/*
   2859 		 * Allocate and setup the beacon frame for AP or adhoc mode.
   2860 		 */
   2861 		if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
   2862 		    ic->ic_opmode == IEEE80211_M_IBSS) {
   2863 			error = ath_beacon_alloc(sc, ni);
   2864 			if (error != 0)
   2865 				goto bad;
   2866 		}
   2867 
   2868 		/*
   2869 		 * Configure the beacon and sleep timers.
   2870 		 */
   2871 		ath_beacon_config(sc);
   2872 
   2873 		/* start periodic recalibration timer */
   2874 		callout_reset(&sc->sc_cal_ch, hz * ath_calinterval,
   2875 			ath_calibrate, sc);
   2876 	} else {
   2877 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
   2878 		ath_hal_intrset(ah, sc->sc_imask);
   2879 		callout_stop(&sc->sc_cal_ch);		/* no calibration */
   2880 	}
   2881 	/*
   2882 	 * Reset the rate control state.
   2883 	 */
   2884 	ath_rate_ctl_reset(sc, nstate);
   2885 	/*
   2886 	 * Invoke the parent method to complete the work.
   2887 	 */
   2888 	return (*sc->sc_newstate)(ic, nstate, arg);
   2889 bad:
   2890 	callout_stop(&sc->sc_scan_ch);
   2891 	callout_stop(&sc->sc_cal_ch);
   2892 	/* NB: do not invoke the parent */
   2893 	return error;
   2894 }
   2895 
   2896 /*
   2897  * Setup driver-specific state for a newly associated node.
   2898  * Note that we're called also on a re-associate, the isnew
   2899  * param tells us if this is the first time or not.
   2900  */
   2901 static void
   2902 ath_newassoc(struct ieee80211com *ic, struct ieee80211_node *ni, int isnew)
   2903 {
   2904 	if (isnew) {
   2905 		struct ath_node *an = (struct ath_node *) ni;
   2906 
   2907 		an->an_tx_ok = an->an_tx_err =
   2908 			an->an_tx_retr = an->an_tx_upper = 0;
   2909 		/* start with highest negotiated rate */
   2910 		/*
   2911 		 * XXX should do otherwise but only when
   2912 		 * the rate control algorithm is better.
   2913 		 */
   2914 		KASSERT(ni->ni_rates.rs_nrates > 0,
   2915 			("new association w/ no rates!"));
   2916 		ni->ni_txrate = ni->ni_rates.rs_nrates - 1;
   2917 	}
   2918 }
   2919 
   2920 static int
   2921 ath_getchannels(struct ath_softc *sc, u_int cc, HAL_BOOL outdoor)
   2922 {
   2923 	struct ieee80211com *ic = &sc->sc_ic;
   2924 	struct ifnet *ifp = &ic->ic_if;
   2925 	struct ath_hal *ah = sc->sc_ah;
   2926 	HAL_CHANNEL *chans;
   2927 	int i, ix, nchan;
   2928 
   2929 	sc->sc_have11g = 0;
   2930 	chans = malloc(IEEE80211_CHAN_MAX * sizeof(HAL_CHANNEL),
   2931 			M_TEMP, M_NOWAIT);
   2932 	if (chans == NULL) {
   2933 		if_printf(ifp, "unable to allocate channel table\n");
   2934 		return ENOMEM;
   2935 	}
   2936 	if (!ath_hal_init_channels(ah, chans, IEEE80211_CHAN_MAX, &nchan,
   2937 	    cc, HAL_MODE_ALL, outdoor)) {
   2938 		if_printf(ifp, "unable to collect channel list from hal\n");
   2939 		free(chans, M_TEMP);
   2940 		return EINVAL;
   2941 	}
   2942 
   2943 	/*
   2944 	 * Convert HAL channels to ieee80211 ones and insert
   2945 	 * them in the table according to their channel number.
   2946 	 */
   2947 	for (i = 0; i < nchan; i++) {
   2948 		HAL_CHANNEL *c = &chans[i];
   2949 		ix = ath_hal_mhz2ieee(c->channel, c->channelFlags);
   2950 		if (ix > IEEE80211_CHAN_MAX) {
   2951 			if_printf(ifp, "bad hal channel %u (%u/%x) ignored\n",
   2952 				ix, c->channel, c->channelFlags);
   2953 			continue;
   2954 		}
   2955 		/* NB: flags are known to be compatible */
   2956 		if (ic->ic_channels[ix].ic_freq == 0) {
   2957 			ic->ic_channels[ix].ic_freq = c->channel;
   2958 			ic->ic_channels[ix].ic_flags = c->channelFlags;
   2959 		} else {
   2960 			/* channels overlap; e.g. 11g and 11b */
   2961 			ic->ic_channels[ix].ic_flags |= c->channelFlags;
   2962 		}
   2963 		if ((c->channelFlags & CHANNEL_G) == CHANNEL_G)
   2964 			sc->sc_have11g = 1;
   2965 	}
   2966 	free(chans, M_TEMP);
   2967 	return 0;
   2968 }
   2969 
   2970 static int
   2971 ath_rate_setup(struct ath_softc *sc, u_int mode)
   2972 {
   2973 	struct ath_hal *ah = sc->sc_ah;
   2974 	struct ieee80211com *ic = &sc->sc_ic;
   2975 	const HAL_RATE_TABLE *rt;
   2976 	struct ieee80211_rateset *rs;
   2977 	int i, maxrates;
   2978 
   2979 	switch (mode) {
   2980 	case IEEE80211_MODE_11A:
   2981 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11A);
   2982 		break;
   2983 	case IEEE80211_MODE_11B:
   2984 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11B);
   2985 		break;
   2986 	case IEEE80211_MODE_11G:
   2987 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11G);
   2988 		break;
   2989 	case IEEE80211_MODE_TURBO:
   2990 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_TURBO);
   2991 		break;
   2992 	default:
   2993 		DPRINTF(("%s: invalid mode %u\n", __func__, mode));
   2994 		return 0;
   2995 	}
   2996 	rt = sc->sc_rates[mode];
   2997 	if (rt == NULL)
   2998 		return 0;
   2999 	if (rt->rateCount > IEEE80211_RATE_MAXSIZE) {
   3000 		DPRINTF(("%s: rate table too small (%u > %u)\n",
   3001 			__func__, rt->rateCount, IEEE80211_RATE_MAXSIZE));
   3002 		maxrates = IEEE80211_RATE_MAXSIZE;
   3003 	} else
   3004 		maxrates = rt->rateCount;
   3005 	rs = &ic->ic_sup_rates[mode];
   3006 	for (i = 0; i < maxrates; i++)
   3007 		rs->rs_rates[i] = rt->info[i].dot11Rate;
   3008 	rs->rs_nrates = maxrates;
   3009 	return 1;
   3010 }
   3011 
   3012 static void
   3013 ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode)
   3014 {
   3015 	const HAL_RATE_TABLE *rt;
   3016 	int i;
   3017 
   3018 	memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap));
   3019 	rt = sc->sc_rates[mode];
   3020 	KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode));
   3021 	for (i = 0; i < rt->rateCount; i++)
   3022 		sc->sc_rixmap[rt->info[i].dot11Rate & IEEE80211_RATE_VAL] = i;
   3023 	memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap));
   3024 	for (i = 0; i < 32; i++)
   3025 		sc->sc_hwmap[i] = rt->info[rt->rateCodeToIndex[i]].dot11Rate;
   3026 	sc->sc_currates = rt;
   3027 	sc->sc_curmode = mode;
   3028 }
   3029 
   3030 /*
   3031  * Reset the rate control state for each 802.11 state transition.
   3032  */
   3033 static void
   3034 ath_rate_ctl_reset(struct ath_softc *sc, enum ieee80211_state state)
   3035 {
   3036 	struct ieee80211com *ic = &sc->sc_ic;
   3037 	struct ieee80211_node *ni;
   3038 	struct ath_node *an;
   3039 
   3040 	an = (struct ath_node *) ic->ic_bss;
   3041 	an->an_tx_ok = an->an_tx_err = an->an_tx_retr = an->an_tx_upper = 0;
   3042 	if (ic->ic_opmode == IEEE80211_M_STA) {
   3043 		ni = ic->ic_bss;
   3044 		if (state == IEEE80211_S_RUN) {
   3045 			/* start with highest negotiated rate */
   3046 			KASSERT(ni->ni_rates.rs_nrates > 0,
   3047 				("transition to RUN state w/ no rates!"));
   3048 			ni->ni_txrate = ni->ni_rates.rs_nrates - 1;
   3049 		} else {
   3050 			/* use lowest rate */
   3051 			ni->ni_txrate = 0;
   3052 		}
   3053 	} else {
   3054 		TAILQ_FOREACH(ni, &ic->ic_node, ni_list) {
   3055 			ni->ni_txrate = 0;		/* use lowest rate */
   3056 			an = (struct ath_node *) ni;
   3057 			an->an_tx_ok = an->an_tx_err = an->an_tx_retr =
   3058 			    an->an_tx_upper = 0;
   3059 		}
   3060 	}
   3061 }
   3062 
   3063 /*
   3064  * Examine and potentially adjust the transmit rate.
   3065  */
   3066 static void
   3067 ath_rate_ctl(void *arg, struct ieee80211_node *ni)
   3068 {
   3069 	struct ath_softc *sc = arg;
   3070 	struct ath_node *an = (struct ath_node *) ni;
   3071 	struct ieee80211_rateset *rs = &ni->ni_rates;
   3072 	int mod = 0, orate, enough;
   3073 
   3074 	/*
   3075 	 * Rate control
   3076 	 * XXX: very primitive version.
   3077 	 */
   3078 	sc->sc_stats.ast_rate_calls++;
   3079 
   3080 	enough = (an->an_tx_ok + an->an_tx_err >= 10);
   3081 
   3082 	/* no packet reached -> down */
   3083 	if (an->an_tx_err > 0 && an->an_tx_ok == 0)
   3084 		mod = -1;
   3085 
   3086 	/* all packets needs retry in average -> down */
   3087 	if (enough && an->an_tx_ok < an->an_tx_retr)
   3088 		mod = -1;
   3089 
   3090 	/* no error and less than 10% of packets needs retry -> up */
   3091 	if (enough && an->an_tx_err == 0 && an->an_tx_ok > an->an_tx_retr * 10)
   3092 		mod = 1;
   3093 
   3094 	orate = ni->ni_txrate;
   3095 	switch (mod) {
   3096 	case 0:
   3097 		if (enough && an->an_tx_upper > 0)
   3098 			an->an_tx_upper--;
   3099 		break;
   3100 	case -1:
   3101 		if (ni->ni_txrate > 0) {
   3102 			ni->ni_txrate--;
   3103 			sc->sc_stats.ast_rate_drop++;
   3104 		}
   3105 		an->an_tx_upper = 0;
   3106 		break;
   3107 	case 1:
   3108 		if (++an->an_tx_upper < 2)
   3109 			break;
   3110 		an->an_tx_upper = 0;
   3111 		if (ni->ni_txrate + 1 < rs->rs_nrates) {
   3112 			ni->ni_txrate++;
   3113 			sc->sc_stats.ast_rate_raise++;
   3114 		}
   3115 		break;
   3116 	}
   3117 
   3118 	if (ni->ni_txrate != orate) {
   3119 		DPRINTF(("%s: %dM -> %dM (%d ok, %d err, %d retr)\n",
   3120 		    __func__,
   3121 		    (rs->rs_rates[orate] & IEEE80211_RATE_VAL) / 2,
   3122 		    (rs->rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL) / 2,
   3123 		    an->an_tx_ok, an->an_tx_err, an->an_tx_retr));
   3124 	}
   3125 	if (ni->ni_txrate != orate || enough)
   3126 		an->an_tx_ok = an->an_tx_err = an->an_tx_retr = 0;
   3127 }
   3128 
   3129 #ifdef AR_DEBUG
   3130 #ifdef __FreeBSD__
   3131 static int
   3132 sysctl_hw_ath_dump(SYSCTL_HANDLER_ARGS)
   3133 {
   3134 	char dmode[64];
   3135 	int error;
   3136 
   3137 	strncpy(dmode, "", sizeof(dmode) - 1);
   3138 	dmode[sizeof(dmode) - 1] = '\0';
   3139 	error = sysctl_handle_string(oidp, &dmode[0], sizeof(dmode), req);
   3140 
   3141 	if (error == 0 && req->newptr != NULL) {
   3142 		struct ifnet *ifp;
   3143 		struct ath_softc *sc;
   3144 
   3145 		ifp = ifunit("ath0");		/* XXX */
   3146 		if (!ifp)
   3147 			return EINVAL;
   3148 		sc = ifp->if_softc;
   3149 		if (strcmp(dmode, "hal") == 0)
   3150 			ath_hal_dumpstate(sc->sc_ah);
   3151 		else if (strcmp(dmode, "eeprom") == 0)
   3152 			ath_hal_dumpeeprom(sc->sc_ah);
   3153 		else if (strcmp(dmode, "rfgain") == 0)
   3154 			ath_hal_dumprfgain(sc->sc_ah);
   3155 		else if (strcmp(dmode, "ani") == 0)
   3156 			ath_hal_dumpani(sc->sc_ah);
   3157 		else
   3158 			return EINVAL;
   3159 	}
   3160 	return error;
   3161 }
   3162 SYSCTL_PROC(_hw_ath, OID_AUTO, dump, CTLTYPE_STRING | CTLFLAG_RW,
   3163 	0, 0, sysctl_hw_ath_dump, "A", "Dump driver state");
   3164 #endif /* __FreeBSD__ */
   3165 
   3166 static void
   3167 ath_printrxbuf(struct ath_buf *bf, int done)
   3168 {
   3169 	struct ath_desc *ds;
   3170 	int i;
   3171 
   3172 	for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) {
   3173 		printf("R%d (%p %p) %08x %08x %08x %08x %08x %08x %c\n",
   3174 		    i, ds, (struct ath_desc *)bf->bf_daddr + i,
   3175 		    ds->ds_link, ds->ds_data,
   3176 		    ds->ds_ctl0, ds->ds_ctl1,
   3177 		    ds->ds_hw[0], ds->ds_hw[1],
   3178 		    !done ? ' ' : (ds->ds_rxstat.rs_status == 0) ? '*' : '!');
   3179 	}
   3180 }
   3181 
   3182 static void
   3183 ath_printtxbuf(struct ath_buf *bf, int done)
   3184 {
   3185 	struct ath_desc *ds;
   3186 	int i;
   3187 
   3188 	for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) {
   3189 		printf("T%d (%p %p) %08x %08x %08x %08x %08x %08x %08x %08x %c\n",
   3190 		    i, ds, (struct ath_desc *)bf->bf_daddr + i,
   3191 		    ds->ds_link, ds->ds_data,
   3192 		    ds->ds_ctl0, ds->ds_ctl1,
   3193 		    ds->ds_hw[0], ds->ds_hw[1], ds->ds_hw[2], ds->ds_hw[3],
   3194 		    !done ? ' ' : (ds->ds_txstat.ts_status == 0) ? '*' : '!');
   3195 	}
   3196 }
   3197 #endif /* AR_DEBUG */
   3198