Home | History | Annotate | Line # | Download | only in ic
wi.c revision 1.162
      1 /*	$NetBSD: wi.c,v 1.162 2004/07/02 21:20:10 dyoung Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997, 1998, 1999
      5  *	Bill Paul <wpaul (at) ctr.columbia.edu>.  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  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Bill Paul.
     18  * 4. Neither the name of the author nor the names of any co-contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     32  * THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * Lucent WaveLAN/IEEE 802.11 PCMCIA driver for NetBSD.
     37  *
     38  * Original FreeBSD driver written by Bill Paul <wpaul (at) ctr.columbia.edu>
     39  * Electrical Engineering Department
     40  * Columbia University, New York City
     41  */
     42 
     43 /*
     44  * The WaveLAN/IEEE adapter is the second generation of the WaveLAN
     45  * from Lucent. Unlike the older cards, the new ones are programmed
     46  * entirely via a firmware-driven controller called the Hermes.
     47  * Unfortunately, Lucent will not release the Hermes programming manual
     48  * without an NDA (if at all). What they do release is an API library
     49  * called the HCF (Hardware Control Functions) which is supposed to
     50  * do the device-specific operations of a device driver for you. The
     51  * publically available version of the HCF library (the 'HCF Light') is
     52  * a) extremely gross, b) lacks certain features, particularly support
     53  * for 802.11 frames, and c) is contaminated by the GNU Public License.
     54  *
     55  * This driver does not use the HCF or HCF Light at all. Instead, it
     56  * programs the Hermes controller directly, using information gleaned
     57  * from the HCF Light code and corresponding documentation.
     58  *
     59  * This driver supports both the PCMCIA and ISA versions of the
     60  * WaveLAN/IEEE cards. Note however that the ISA card isn't really
     61  * anything of the sort: it's actually a PCMCIA bridge adapter
     62  * that fits into an ISA slot, into which a PCMCIA WaveLAN card is
     63  * inserted. Consequently, you need to use the pccard support for
     64  * both the ISA and PCMCIA adapters.
     65  */
     66 
     67 /*
     68  * FreeBSD driver ported to NetBSD by Bill Sommerfeld in the back of the
     69  * Oslo IETF plenary meeting.
     70  */
     71 
     72 #include <sys/cdefs.h>
     73 __KERNEL_RCSID(0, "$NetBSD: wi.c,v 1.162 2004/07/02 21:20:10 dyoung Exp $");
     74 
     75 #define WI_HERMES_AUTOINC_WAR	/* Work around data write autoinc bug. */
     76 #define WI_HERMES_STATS_WAR	/* Work around stats counter bug. */
     77 
     78 #include "bpfilter.h"
     79 
     80 #include <sys/param.h>
     81 #include <sys/systm.h>
     82 #include <sys/callout.h>
     83 #include <sys/device.h>
     84 #include <sys/socket.h>
     85 #include <sys/mbuf.h>
     86 #include <sys/ioctl.h>
     87 #include <sys/kernel.h>		/* for hz */
     88 #include <sys/proc.h>
     89 
     90 #include <net/if.h>
     91 #include <net/if_dl.h>
     92 #include <net/if_llc.h>
     93 #include <net/if_media.h>
     94 #include <net/if_ether.h>
     95 
     96 #include <net80211/ieee80211_var.h>
     97 #include <net80211/ieee80211_compat.h>
     98 #include <net80211/ieee80211_ioctl.h>
     99 #include <net80211/ieee80211_radiotap.h>
    100 #include <net80211/ieee80211_rssadapt.h>
    101 
    102 #if NBPFILTER > 0
    103 #include <net/bpf.h>
    104 #include <net/bpfdesc.h>
    105 #endif
    106 
    107 #include <machine/bus.h>
    108 
    109 #include <dev/ic/wi_ieee.h>
    110 #include <dev/ic/wireg.h>
    111 #include <dev/ic/wivar.h>
    112 
    113 static int  wi_init(struct ifnet *);
    114 static void wi_stop(struct ifnet *, int);
    115 static void wi_start(struct ifnet *);
    116 static int  wi_reset(struct wi_softc *);
    117 static void wi_watchdog(struct ifnet *);
    118 static int  wi_ioctl(struct ifnet *, u_long, caddr_t);
    119 static int  wi_media_change(struct ifnet *);
    120 static void wi_media_status(struct ifnet *, struct ifmediareq *);
    121 
    122 static struct ieee80211_node *wi_node_alloc(struct ieee80211com *);
    123 static void wi_node_copy(struct ieee80211com *, struct ieee80211_node *,
    124     const struct ieee80211_node *);
    125 static void wi_node_free(struct ieee80211com *, struct ieee80211_node *);
    126 
    127 static void wi_raise_rate(struct ieee80211com *, struct ieee80211_rssdesc *);
    128 static void wi_lower_rate(struct ieee80211com *, struct ieee80211_rssdesc *);
    129 static void wi_choose_rate(struct ieee80211com *, struct ieee80211_node *,
    130     struct ieee80211_frame *, u_int);
    131 static void wi_rssadapt_updatestats_cb(void *, struct ieee80211_node *);
    132 static void wi_rssadapt_updatestats(void *);
    133 
    134 static void wi_rx_intr(struct wi_softc *);
    135 static void wi_txalloc_intr(struct wi_softc *);
    136 static void wi_tx_intr(struct wi_softc *);
    137 static void wi_tx_ex_intr(struct wi_softc *);
    138 static void wi_info_intr(struct wi_softc *);
    139 
    140 static int  wi_get_cfg(struct ifnet *, u_long, caddr_t);
    141 static int  wi_set_cfg(struct ifnet *, u_long, caddr_t);
    142 static int  wi_cfg_txrate(struct wi_softc *);
    143 static int  wi_write_txrate(struct wi_softc *, int);
    144 static int  wi_write_wep(struct wi_softc *);
    145 static int  wi_write_multi(struct wi_softc *);
    146 static int  wi_alloc_fid(struct wi_softc *, int, int *);
    147 static void wi_read_nicid(struct wi_softc *);
    148 static int  wi_write_ssid(struct wi_softc *, int, u_int8_t *, int);
    149 
    150 static int  wi_cmd(struct wi_softc *, int, int, int, int);
    151 static int  wi_seek_bap(struct wi_softc *, int, int);
    152 static int  wi_read_bap(struct wi_softc *, int, int, void *, int);
    153 static int  wi_write_bap(struct wi_softc *, int, int, void *, int);
    154 static int  wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int);
    155 static int  wi_read_rid(struct wi_softc *, int, void *, int *);
    156 static int  wi_write_rid(struct wi_softc *, int, void *, int);
    157 
    158 static int  wi_newstate(struct ieee80211com *, enum ieee80211_state, int);
    159 static int  wi_set_tim(struct ieee80211com *, int, int);
    160 
    161 static int  wi_scan_ap(struct wi_softc *, u_int16_t, u_int16_t);
    162 static void wi_scan_result(struct wi_softc *, int, int);
    163 
    164 static void wi_dump_pkt(struct wi_frame *, struct ieee80211_node *, int rssi);
    165 
    166 static inline int
    167 wi_write_val(struct wi_softc *sc, int rid, u_int16_t val)
    168 {
    169 
    170 	val = htole16(val);
    171 	return wi_write_rid(sc, rid, &val, sizeof(val));
    172 }
    173 
    174 static	struct timeval lasttxerror;	/* time of last tx error msg */
    175 static	int curtxeps = 0;		/* current tx error msgs/sec */
    176 static	int wi_txerate = 0;		/* tx error rate: max msgs/sec */
    177 
    178 #ifdef WI_DEBUG
    179 int wi_debug = 0;
    180 
    181 #define	DPRINTF(X)	if (wi_debug) printf X
    182 #define	DPRINTF2(X)	if (wi_debug > 1) printf X
    183 #define	IFF_DUMPPKTS(_ifp) \
    184 	(((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
    185 #else
    186 #define	DPRINTF(X)
    187 #define	DPRINTF2(X)
    188 #define	IFF_DUMPPKTS(_ifp)	0
    189 #endif
    190 
    191 #define WI_INTRS	(WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO | \
    192 			 WI_EV_TX | WI_EV_TX_EXC)
    193 
    194 struct wi_card_ident
    195 wi_card_ident[] = {
    196 	/* CARD_ID			CARD_NAME		FIRM_TYPE */
    197 	{ WI_NIC_LUCENT_ID,		WI_NIC_LUCENT_STR,	WI_LUCENT },
    198 	{ WI_NIC_SONY_ID,		WI_NIC_SONY_STR,	WI_LUCENT },
    199 	{ WI_NIC_LUCENT_EMB_ID,		WI_NIC_LUCENT_EMB_STR,	WI_LUCENT },
    200 	{ WI_NIC_EVB2_ID,		WI_NIC_EVB2_STR,	WI_INTERSIL },
    201 	{ WI_NIC_HWB3763_ID,		WI_NIC_HWB3763_STR,	WI_INTERSIL },
    202 	{ WI_NIC_HWB3163_ID,		WI_NIC_HWB3163_STR,	WI_INTERSIL },
    203 	{ WI_NIC_HWB3163B_ID,		WI_NIC_HWB3163B_STR,	WI_INTERSIL },
    204 	{ WI_NIC_EVB3_ID,		WI_NIC_EVB3_STR,	WI_INTERSIL },
    205 	{ WI_NIC_HWB1153_ID,		WI_NIC_HWB1153_STR,	WI_INTERSIL },
    206 	{ WI_NIC_P2_SST_ID,		WI_NIC_P2_SST_STR,	WI_INTERSIL },
    207 	{ WI_NIC_EVB2_SST_ID,		WI_NIC_EVB2_SST_STR,	WI_INTERSIL },
    208 	{ WI_NIC_3842_EVA_ID,		WI_NIC_3842_EVA_STR,	WI_INTERSIL },
    209 	{ WI_NIC_3842_PCMCIA_AMD_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
    210 	{ WI_NIC_3842_PCMCIA_SST_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
    211 	{ WI_NIC_3842_PCMCIA_ATM_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
    212 	{ WI_NIC_3842_MINI_AMD_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
    213 	{ WI_NIC_3842_MINI_SST_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
    214 	{ WI_NIC_3842_MINI_ATM_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
    215 	{ WI_NIC_3842_PCI_AMD_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
    216 	{ WI_NIC_3842_PCI_SST_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
    217 	{ WI_NIC_3842_PCI_ATM_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
    218 	{ WI_NIC_P3_PCMCIA_AMD_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
    219 	{ WI_NIC_P3_PCMCIA_SST_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
    220 	{ WI_NIC_P3_MINI_AMD_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
    221 	{ WI_NIC_P3_MINI_SST_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
    222 	{ 0,	NULL,	0 },
    223 };
    224 
    225 int
    226 wi_attach(struct wi_softc *sc)
    227 {
    228 	struct ieee80211com *ic = &sc->sc_ic;
    229 	struct ifnet *ifp = &ic->ic_if;
    230 	int chan, nrate, buflen;
    231 	u_int16_t val, chanavail;
    232  	struct {
    233  		u_int16_t nrates;
    234  		char rates[IEEE80211_RATE_SIZE];
    235  	} ratebuf;
    236 	static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
    237 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    238 	};
    239 	int s;
    240 
    241 	s = splnet();
    242 
    243 	/* Make sure interrupts are disabled. */
    244 	CSR_WRITE_2(sc, WI_INT_EN, 0);
    245 	CSR_WRITE_2(sc, WI_EVENT_ACK, ~0);
    246 
    247 	sc->sc_invalid = 0;
    248 
    249 	/* Reset the NIC. */
    250 	if (wi_reset(sc) != 0) {
    251 		sc->sc_invalid = 1;
    252 		splx(s);
    253 		return 1;
    254 	}
    255 
    256 	buflen = IEEE80211_ADDR_LEN;
    257 	if (wi_read_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, &buflen) != 0 ||
    258 	    IEEE80211_ADDR_EQ(ic->ic_myaddr, empty_macaddr)) {
    259 		printf(" could not get mac address, attach failed\n");
    260 		splx(s);
    261 		return 1;
    262 	}
    263 
    264 	printf(" 802.11 address %s\n", ether_sprintf(ic->ic_myaddr));
    265 
    266 	/* Read NIC identification */
    267 	wi_read_nicid(sc);
    268 
    269 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
    270 	ifp->if_softc = sc;
    271 	ifp->if_start = wi_start;
    272 	ifp->if_ioctl = wi_ioctl;
    273 	ifp->if_watchdog = wi_watchdog;
    274 	ifp->if_init = wi_init;
    275 	ifp->if_stop = wi_stop;
    276 	ifp->if_flags =
    277 	    IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST | IFF_NOTRAILERS;
    278 	IFQ_SET_READY(&ifp->if_snd);
    279 
    280 	ic->ic_phytype = IEEE80211_T_DS;
    281 	ic->ic_opmode = IEEE80211_M_STA;
    282 	ic->ic_caps = IEEE80211_C_PMGT | IEEE80211_C_AHDEMO;
    283 	ic->ic_state = IEEE80211_S_INIT;
    284 	ic->ic_max_aid = WI_MAX_AID;
    285 
    286 	/* Find available channel */
    287 	buflen = sizeof(chanavail);
    288 	if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &chanavail, &buflen) != 0)
    289 		chanavail = htole16(0x1fff);	/* assume 1-11 */
    290 	for (chan = 16; chan > 0; chan--) {
    291 		if (!isset((u_int8_t*)&chanavail, chan - 1))
    292 			continue;
    293 		ic->ic_ibss_chan = &ic->ic_channels[chan];
    294 		ic->ic_channels[chan].ic_freq =
    295 		    ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ);
    296 		ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_B;
    297 	}
    298 
    299 	/* Find default IBSS channel */
    300 	buflen = sizeof(val);
    301 	if (wi_read_rid(sc, WI_RID_OWN_CHNL, &val, &buflen) == 0) {
    302 		chan = le16toh(val);
    303 		if (isset((u_int8_t*)&chanavail, chan - 1))
    304 			ic->ic_ibss_chan = &ic->ic_channels[chan];
    305 	}
    306 	if (ic->ic_ibss_chan == NULL)
    307 		panic("%s: no available channel\n", sc->sc_dev.dv_xname);
    308 
    309 	if (sc->sc_firmware_type == WI_LUCENT) {
    310 		sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET;
    311 	} else {
    312 		buflen = sizeof(val);
    313 		if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) &&
    314 		    wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0)
    315 			sc->sc_dbm_offset = le16toh(val);
    316 		else
    317 			sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
    318 	}
    319 
    320 	sc->sc_flags |= WI_FLAGS_RSSADAPTSTA;
    321 
    322 	/*
    323 	 * Set flags based on firmware version.
    324 	 */
    325 	switch (sc->sc_firmware_type) {
    326 	case WI_LUCENT:
    327 		sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
    328 #ifdef WI_HERMES_AUTOINC_WAR
    329 		/* XXX: not confirmed, but never seen for recent firmware */
    330 		if (sc->sc_sta_firmware_ver <  40000) {
    331 			sc->sc_flags |= WI_FLAGS_BUG_AUTOINC;
    332 		}
    333 #endif
    334 		if (sc->sc_sta_firmware_ver >= 60000)
    335 			sc->sc_flags |= WI_FLAGS_HAS_MOR;
    336 		if (sc->sc_sta_firmware_ver >= 60006) {
    337 			ic->ic_caps |= IEEE80211_C_IBSS;
    338 			ic->ic_caps |= IEEE80211_C_MONITOR;
    339 		}
    340 		sc->sc_ibss_port = 1;
    341 		break;
    342 
    343 	case WI_INTERSIL:
    344 		sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR;
    345 		sc->sc_flags |= WI_FLAGS_HAS_ROAMING;
    346 		sc->sc_flags |= WI_FLAGS_HAS_SYSSCALE;
    347 		if (sc->sc_sta_firmware_ver > 10101)
    348 			sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST;
    349 		if (sc->sc_sta_firmware_ver >= 800) {
    350 			if (sc->sc_sta_firmware_ver != 10402)
    351 				ic->ic_caps |= IEEE80211_C_HOSTAP;
    352 			ic->ic_caps |= IEEE80211_C_IBSS;
    353 			ic->ic_caps |= IEEE80211_C_MONITOR;
    354 		}
    355 		sc->sc_ibss_port = 0;
    356 		sc->sc_alt_retry = 2;
    357 		break;
    358 
    359 	case WI_SYMBOL:
    360 		sc->sc_flags |= WI_FLAGS_HAS_DIVERSITY;
    361 		if (sc->sc_sta_firmware_ver >= 20000)
    362 			ic->ic_caps |= IEEE80211_C_IBSS;
    363 		sc->sc_ibss_port = 4;
    364 		break;
    365 	}
    366 
    367 	/*
    368 	 * Find out if we support WEP on this card.
    369 	 */
    370 	buflen = sizeof(val);
    371 	if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 &&
    372 	    val != htole16(0))
    373 		ic->ic_caps |= IEEE80211_C_WEP;
    374 
    375 	/* Find supported rates. */
    376 	buflen = sizeof(ratebuf);
    377 	if (wi_read_rid(sc, WI_RID_DATA_RATES, &ratebuf, &buflen) == 0) {
    378 		nrate = le16toh(ratebuf.nrates);
    379 		if (nrate > IEEE80211_RATE_SIZE)
    380 			nrate = IEEE80211_RATE_SIZE;
    381 		memcpy(ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates,
    382 		    &ratebuf.rates[0], nrate);
    383 		ic->ic_sup_rates[IEEE80211_MODE_11B].rs_nrates = nrate;
    384 	}
    385 	buflen = sizeof(val);
    386 
    387 	sc->sc_max_datalen = 2304;
    388 	sc->sc_rts_thresh = 2347;
    389 	sc->sc_frag_thresh = 2346;
    390 	sc->sc_system_scale = 1;
    391 	sc->sc_cnfauthmode = IEEE80211_AUTH_OPEN;
    392 	sc->sc_roaming_mode = 1;
    393 
    394 	callout_init(&sc->sc_rssadapt_ch);
    395 
    396 	/*
    397 	 * Call MI attach routines.
    398 	 */
    399 	if_attach(ifp);
    400 	ieee80211_ifattach(ifp);
    401 
    402 	sc->sc_newstate = ic->ic_newstate;
    403 	ic->ic_newstate = wi_newstate;
    404 	ic->ic_node_alloc = wi_node_alloc;
    405 	ic->ic_node_free = wi_node_free;
    406 	ic->ic_node_copy = wi_node_copy;
    407 	ic->ic_set_tim = wi_set_tim;
    408 
    409 	ieee80211_media_init(ifp, wi_media_change, wi_media_status);
    410 
    411 #if NBPFILTER > 0
    412 	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
    413 	    sizeof(struct ieee80211_frame) + 64, &sc->sc_drvbpf);
    414 #endif
    415 
    416 	memset(&sc->sc_rxtapu, 0, sizeof(sc->sc_rxtapu));
    417 	sc->sc_rxtap.wr_ihdr.it_len = sizeof(sc->sc_rxtapu);
    418 	sc->sc_rxtap.wr_ihdr.it_present = WI_RX_RADIOTAP_PRESENT;
    419 
    420 	memset(&sc->sc_txtapu, 0, sizeof(sc->sc_txtapu));
    421 	sc->sc_txtap.wt_ihdr.it_len = sizeof(sc->sc_txtapu);
    422 	sc->sc_txtap.wt_ihdr.it_present = WI_TX_RADIOTAP_PRESENT;
    423 
    424 	/* Attach is successful. */
    425 	sc->sc_attached = 1;
    426 
    427 	splx(s);
    428 	return 0;
    429 }
    430 
    431 int
    432 wi_detach(struct wi_softc *sc)
    433 {
    434 	struct ifnet *ifp = &sc->sc_ic.ic_if;
    435 	int s;
    436 
    437 	if (!sc->sc_attached)
    438 		return 0;
    439 
    440 	s = splnet();
    441 
    442 	sc->sc_invalid = 1;
    443 	wi_stop(ifp, 1);
    444 
    445 	/* Delete all remaining media. */
    446 	ifmedia_delete_instance(&sc->sc_ic.ic_media, IFM_INST_ANY);
    447 
    448 	ieee80211_ifdetach(ifp);
    449 	if_detach(ifp);
    450 	splx(s);
    451 	return 0;
    452 }
    453 
    454 #ifdef __NetBSD__
    455 int
    456 wi_activate(struct device *self, enum devact act)
    457 {
    458 	struct wi_softc *sc = (struct wi_softc *)self;
    459 	int rv = 0, s;
    460 
    461 	s = splnet();
    462 	switch (act) {
    463 	case DVACT_ACTIVATE:
    464 		rv = EOPNOTSUPP;
    465 		break;
    466 
    467 	case DVACT_DEACTIVATE:
    468 		if_deactivate(&sc->sc_ic.ic_if);
    469 		break;
    470 	}
    471 	splx(s);
    472 	return rv;
    473 }
    474 
    475 void
    476 wi_power(struct wi_softc *sc, int why)
    477 {
    478 	struct ifnet *ifp = &sc->sc_ic.ic_if;
    479 	int s;
    480 
    481 	s = splnet();
    482 	switch (why) {
    483 	case PWR_SUSPEND:
    484 	case PWR_STANDBY:
    485 		wi_stop(ifp, 1);
    486 		break;
    487 	case PWR_RESUME:
    488 		if (ifp->if_flags & IFF_UP) {
    489 			wi_init(ifp);
    490 			(void)wi_intr(sc);
    491 		}
    492 		break;
    493 	case PWR_SOFTSUSPEND:
    494 	case PWR_SOFTSTANDBY:
    495 	case PWR_SOFTRESUME:
    496 		break;
    497 	}
    498 	splx(s);
    499 }
    500 #endif /* __NetBSD__ */
    501 
    502 void
    503 wi_shutdown(struct wi_softc *sc)
    504 {
    505 	struct ifnet *ifp = &sc->sc_ic.ic_if;
    506 
    507 	if (sc->sc_attached)
    508 		wi_stop(ifp, 1);
    509 }
    510 
    511 int
    512 wi_intr(void *arg)
    513 {
    514 	int i;
    515 	struct wi_softc	*sc = arg;
    516 	struct ifnet *ifp = &sc->sc_ic.ic_if;
    517 	u_int16_t status;
    518 
    519 	if (sc->sc_enabled == 0 ||
    520 	    (sc->sc_dev.dv_flags & DVF_ACTIVE) == 0 ||
    521 	    (ifp->if_flags & IFF_RUNNING) == 0)
    522 		return 0;
    523 
    524 	if ((ifp->if_flags & IFF_UP) == 0) {
    525 		CSR_WRITE_2(sc, WI_INT_EN, 0);
    526 		CSR_WRITE_2(sc, WI_EVENT_ACK, ~0);
    527 		return 1;
    528 	}
    529 
    530 	/* This is superfluous on Prism, but Lucent breaks if we
    531 	 * do not disable interrupts.
    532 	 */
    533 	CSR_WRITE_2(sc, WI_INT_EN, 0);
    534 
    535 	/* maximum 10 loops per interrupt */
    536 	for (i = 0; i < 10; i++) {
    537 		/*
    538 		 * Only believe a status bit when we enter wi_intr, or when
    539 		 * the bit was "off" the last time through the loop. This is
    540 		 * my strategy to avoid racing the hardware/firmware if I
    541 		 * can re-read the event status register more quickly than
    542 		 * it is updated.
    543 		 */
    544 		status = CSR_READ_2(sc, WI_EVENT_STAT);
    545 		if ((status & WI_INTRS) == 0)
    546 			break;
    547 
    548 		if (status & WI_EV_RX)
    549 			wi_rx_intr(sc);
    550 
    551 		if (status & WI_EV_ALLOC)
    552 			wi_txalloc_intr(sc);
    553 
    554 		if (status & WI_EV_TX)
    555 			wi_tx_intr(sc);
    556 
    557 		if (status & WI_EV_TX_EXC)
    558 			wi_tx_ex_intr(sc);
    559 
    560 		if (status & WI_EV_INFO)
    561 			wi_info_intr(sc);
    562 
    563 		if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
    564 		    (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0 &&
    565 		    !IFQ_IS_EMPTY(&ifp->if_snd))
    566 			wi_start(ifp);
    567 	}
    568 
    569 	/* re-enable interrupts */
    570 	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
    571 
    572 	return 1;
    573 }
    574 
    575 #define arraylen(a) (sizeof(a) / sizeof((a)[0]))
    576 
    577 static void
    578 wi_rssdescs_init(struct wi_rssdesc (*rssd)[WI_NTXRSS], wi_rssdescq_t *rssdfree)
    579 {
    580 	int i;
    581 	SLIST_INIT(rssdfree);
    582 	for (i = 0; i < arraylen(*rssd); i++) {
    583 		SLIST_INSERT_HEAD(rssdfree, &(*rssd)[i], rd_next);
    584 	}
    585 }
    586 
    587 static void
    588 wi_rssdescs_reset(struct ieee80211com *ic, struct wi_rssdesc (*rssd)[WI_NTXRSS],
    589     wi_rssdescq_t *rssdfree, u_int8_t (*txpending)[IEEE80211_RATE_MAXSIZE])
    590 {
    591 	struct ieee80211_node *ni;
    592 	int i;
    593 	for (i = 0; i < arraylen(*rssd); i++) {
    594 		ni = (*rssd)[i].rd_desc.id_node;
    595 		(*rssd)[i].rd_desc.id_node = NULL;
    596 		if (ni != NULL && (ic->ic_if.if_flags & IFF_DEBUG) != 0)
    597 			printf("%s: cleaning outstanding rssadapt "
    598 			    "descriptor for %s\n",
    599 			    ic->ic_if.if_xname, ether_sprintf(ni->ni_macaddr));
    600 		if (ni != NULL && ni != ic->ic_bss)
    601 			ieee80211_free_node(ic, ni);
    602 	}
    603 	memset(*txpending, 0, sizeof(*txpending));
    604 	wi_rssdescs_init(rssd, rssdfree);
    605 }
    606 
    607 static int
    608 wi_init(struct ifnet *ifp)
    609 {
    610 	struct wi_softc *sc = ifp->if_softc;
    611 	struct ieee80211com *ic = &sc->sc_ic;
    612 	struct wi_joinreq join;
    613 	int i;
    614 	int error = 0, wasenabled;
    615 
    616 	DPRINTF(("wi_init: enabled %d\n", sc->sc_enabled));
    617 	wasenabled = sc->sc_enabled;
    618 	if (!sc->sc_enabled) {
    619 		if ((error = (*sc->sc_enable)(sc)) != 0)
    620 			goto out;
    621 		sc->sc_enabled = 1;
    622 	} else
    623 		wi_stop(ifp, 0);
    624 
    625 	/* Symbol firmware cannot be initialized more than once */
    626 	if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled)
    627 		if ((error = wi_reset(sc)) != 0)
    628 			goto out;
    629 
    630 	/* common 802.11 configuration */
    631 	ic->ic_flags &= ~IEEE80211_F_IBSSON;
    632 	sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
    633 	switch (ic->ic_opmode) {
    634 	case IEEE80211_M_STA:
    635 		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_BSS);
    636 		break;
    637 	case IEEE80211_M_IBSS:
    638 		wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_ibss_port);
    639 		ic->ic_flags |= IEEE80211_F_IBSSON;
    640 		sc->sc_syn_timer = 5;
    641 		ifp->if_timer = 1;
    642 		break;
    643 	case IEEE80211_M_AHDEMO:
    644 		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
    645 		break;
    646 	case IEEE80211_M_HOSTAP:
    647 		wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_HOSTAP);
    648 		break;
    649 	case IEEE80211_M_MONITOR:
    650 		if (sc->sc_firmware_type == WI_LUCENT)
    651 			wi_write_val(sc, WI_RID_PORTTYPE, WI_PORTTYPE_ADHOC);
    652 		wi_cmd(sc, WI_CMD_TEST | (WI_TEST_MONITOR << 8), 0, 0, 0);
    653 		break;
    654 	}
    655 
    656 	/* Intersil interprets this RID as joining ESS even in IBSS mode */
    657 	if (sc->sc_firmware_type == WI_LUCENT &&
    658 	    (ic->ic_flags & IEEE80211_F_IBSSON) && ic->ic_des_esslen > 0)
    659 		wi_write_val(sc, WI_RID_CREATE_IBSS, 1);
    660 	else
    661 		wi_write_val(sc, WI_RID_CREATE_IBSS, 0);
    662 	wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
    663 	wi_write_ssid(sc, WI_RID_DESIRED_SSID, ic->ic_des_essid,
    664 	    ic->ic_des_esslen);
    665 	wi_write_val(sc, WI_RID_OWN_CHNL,
    666 	    ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
    667 	wi_write_ssid(sc, WI_RID_OWN_SSID, ic->ic_des_essid, ic->ic_des_esslen);
    668 	IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
    669 	wi_write_rid(sc, WI_RID_MAC_NODE, ic->ic_myaddr, IEEE80211_ADDR_LEN);
    670 	wi_write_val(sc, WI_RID_PM_ENABLED,
    671 	    (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0);
    672 
    673 	/* not yet common 802.11 configuration */
    674 	wi_write_val(sc, WI_RID_MAX_DATALEN, sc->sc_max_datalen);
    675 	wi_write_val(sc, WI_RID_RTS_THRESH, sc->sc_rts_thresh);
    676 	if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
    677 		wi_write_val(sc, WI_RID_FRAG_THRESH, sc->sc_frag_thresh);
    678 
    679 	/* driver specific 802.11 configuration */
    680 	if (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)
    681 		wi_write_val(sc, WI_RID_SYSTEM_SCALE, sc->sc_system_scale);
    682 	if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
    683 		wi_write_val(sc, WI_RID_ROAMING_MODE, sc->sc_roaming_mode);
    684 	if (sc->sc_flags & WI_FLAGS_HAS_MOR)
    685 		wi_write_val(sc, WI_RID_MICROWAVE_OVEN, sc->sc_microwave_oven);
    686 	wi_cfg_txrate(sc);
    687 	wi_write_ssid(sc, WI_RID_NODENAME, sc->sc_nodename, sc->sc_nodelen);
    688 
    689 	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
    690 	    sc->sc_firmware_type == WI_INTERSIL) {
    691 		wi_write_val(sc, WI_RID_OWN_BEACON_INT, ic->ic_lintval);
    692 		wi_write_val(sc, WI_RID_BASIC_RATE, 0x03);   /* 1, 2 */
    693 		wi_write_val(sc, WI_RID_SUPPORT_RATE, 0x0f); /* 1, 2, 5.5, 11 */
    694 		wi_write_val(sc, WI_RID_DTIM_PERIOD, 1);
    695 	}
    696 
    697 	if (sc->sc_firmware_type == WI_INTERSIL)
    698 		wi_write_val(sc, WI_RID_ALT_RETRY_COUNT, sc->sc_alt_retry);
    699 
    700 	/*
    701 	 * Initialize promisc mode.
    702 	 *	Being in Host-AP mode causes a great
    703 	 *	deal of pain if promiscuous mode is set.
    704 	 *	Therefore we avoid confusing the firmware
    705 	 *	and always reset promisc mode in Host-AP
    706 	 *	mode.  Host-AP sees all the packets anyway.
    707 	 */
    708 	if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
    709 	    (ifp->if_flags & IFF_PROMISC) != 0) {
    710 		wi_write_val(sc, WI_RID_PROMISC, 1);
    711 	} else {
    712 		wi_write_val(sc, WI_RID_PROMISC, 0);
    713 	}
    714 
    715 	/* Configure WEP. */
    716 	if (ic->ic_caps & IEEE80211_C_WEP)
    717 		wi_write_wep(sc);
    718 
    719 	/* Set multicast filter. */
    720 	wi_write_multi(sc);
    721 
    722 	if (sc->sc_firmware_type != WI_SYMBOL || !wasenabled) {
    723 		sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
    724 		if (sc->sc_firmware_type == WI_SYMBOL)
    725 			sc->sc_buflen = 1585;	/* XXX */
    726 		for (i = 0; i < WI_NTXBUF; i++) {
    727 			error = wi_alloc_fid(sc, sc->sc_buflen,
    728 			    &sc->sc_txd[i].d_fid);
    729 			if (error) {
    730 				printf("%s: tx buffer allocation failed\n",
    731 				    sc->sc_dev.dv_xname);
    732 				goto out;
    733 			}
    734 			DPRINTF2(("wi_init: txbuf %d allocated %x\n", i,
    735 			    sc->sc_txd[i].d_fid));
    736 			sc->sc_txd[i].d_len = 0;
    737 		}
    738 	}
    739 	sc->sc_txcur = sc->sc_txnext = 0;
    740 
    741 	wi_rssdescs_init(&sc->sc_rssd, &sc->sc_rssdfree);
    742 
    743 	/* Enable desired port */
    744 	wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
    745 	ifp->if_flags |= IFF_RUNNING;
    746 	ifp->if_flags &= ~IFF_OACTIVE;
    747 	ic->ic_state = IEEE80211_S_INIT;
    748 
    749 	if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
    750 	    ic->ic_opmode == IEEE80211_M_MONITOR ||
    751 	    ic->ic_opmode == IEEE80211_M_HOSTAP)
    752 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
    753 
    754 	/* Enable interrupts */
    755 	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
    756 
    757 	if (!wasenabled &&
    758 	    ic->ic_opmode == IEEE80211_M_HOSTAP &&
    759 	    sc->sc_firmware_type == WI_INTERSIL) {
    760 		/* XXX: some card need to be re-enabled for hostap */
    761 		wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
    762 		wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
    763 	}
    764 
    765 	if (ic->ic_opmode == IEEE80211_M_STA &&
    766 	    ((ic->ic_flags & IEEE80211_F_DESBSSID) ||
    767 	    ic->ic_des_chan != IEEE80211_CHAN_ANYC)) {
    768 		memset(&join, 0, sizeof(join));
    769 		if (ic->ic_flags & IEEE80211_F_DESBSSID)
    770 			IEEE80211_ADDR_COPY(&join.wi_bssid, ic->ic_des_bssid);
    771 		if (ic->ic_des_chan != IEEE80211_CHAN_ANYC)
    772 			join.wi_chan =
    773 			    htole16(ieee80211_chan2ieee(ic, ic->ic_des_chan));
    774 		/* Lucent firmware does not support the JOIN RID. */
    775 		if (sc->sc_firmware_type != WI_LUCENT)
    776 			wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
    777 	}
    778 
    779  out:
    780 	if (error) {
    781 		printf("%s: interface not running\n", sc->sc_dev.dv_xname);
    782 		wi_stop(ifp, 0);
    783 	}
    784 	DPRINTF(("wi_init: return %d\n", error));
    785 	return error;
    786 }
    787 
    788 static void
    789 wi_stop(struct ifnet *ifp, int disable)
    790 {
    791 	struct wi_softc	*sc = ifp->if_softc;
    792 	struct ieee80211com *ic = &sc->sc_ic;
    793 	int s;
    794 
    795 	if (!sc->sc_enabled)
    796 		return;
    797 
    798 	s = splnet();
    799 
    800 	DPRINTF(("wi_stop: disable %d\n", disable));
    801 
    802 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
    803 	if (!sc->sc_invalid) {
    804 		CSR_WRITE_2(sc, WI_INT_EN, 0);
    805 		wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
    806 	}
    807 
    808 	wi_rssdescs_reset(ic, &sc->sc_rssd, &sc->sc_rssdfree,
    809 	    &sc->sc_txpending);
    810 
    811 	sc->sc_tx_timer = 0;
    812 	sc->sc_scan_timer = 0;
    813 	sc->sc_syn_timer = 0;
    814 	sc->sc_false_syns = 0;
    815 	sc->sc_naps = 0;
    816 	ifp->if_flags &= ~(IFF_OACTIVE | IFF_RUNNING);
    817 	ifp->if_timer = 0;
    818 
    819 	if (disable) {
    820 		if (sc->sc_disable)
    821 			(*sc->sc_disable)(sc);
    822 		sc->sc_enabled = 0;
    823 	}
    824 	splx(s);
    825 }
    826 
    827 /*
    828  * Choose a data rate for a packet len bytes long that suits the packet
    829  * type and the wireless conditions.
    830  *
    831  * TBD Adapt fragmentation threshold.
    832  */
    833 static void
    834 wi_choose_rate(struct ieee80211com *ic, struct ieee80211_node *ni,
    835     struct ieee80211_frame *wh, u_int len)
    836 {
    837 	struct wi_softc	*sc = ic->ic_if.if_softc;
    838 	struct wi_node *wn = (void*)ni;
    839 	struct ieee80211_rssadapt *ra = &wn->wn_rssadapt;
    840 	int do_not_adapt, i, rateidx, s;
    841 
    842 	do_not_adapt = (ic->ic_opmode != IEEE80211_M_HOSTAP) &&
    843 	    (sc->sc_flags & WI_FLAGS_RSSADAPTSTA) == 0;
    844 
    845 	s = splnet();
    846 
    847 	rateidx = ieee80211_rssadapt_choose(ra, &ni->ni_rates, wh, len,
    848 	    ic->ic_fixed_rate,
    849 	    ((ic->ic_if.if_flags & IFF_DEBUG) == 0) ? NULL : ic->ic_if.if_xname,
    850 	    do_not_adapt);
    851 
    852 	if (ic->ic_opmode != IEEE80211_M_HOSTAP) {
    853 		/* choose the slowest pending rate so that we don't
    854 		 * accidentally send a packet on the MAC's queue
    855 		 * too fast. TBD find out if the MAC labels Tx
    856 		 * packets w/ rate when enqueued or dequeued.
    857 		 */
    858 		for (i = 0; i < rateidx && sc->sc_txpending[i] == 0; i++);
    859 		ni->ni_txrate = i;
    860 	} else
    861 		ni->ni_txrate = rateidx;
    862 	splx(s);
    863 	return;
    864 }
    865 
    866 static void
    867 wi_raise_rate(struct ieee80211com *ic, struct ieee80211_rssdesc *id)
    868 {
    869 	struct wi_node *wn;
    870 	if (id->id_node == NULL)
    871 		return;
    872 
    873 	wn = (void*)id->id_node;
    874 	ieee80211_rssadapt_raise_rate(ic, &wn->wn_rssadapt, id);
    875 }
    876 
    877 static void
    878 wi_lower_rate(struct ieee80211com *ic, struct ieee80211_rssdesc *id)
    879 {
    880 	struct ieee80211_node *ni;
    881 	struct wi_node *wn;
    882 	int s;
    883 
    884 	s = splnet();
    885 
    886 	if ((ni = id->id_node) == NULL) {
    887 		DPRINTF(("wi_lower_rate: missing node\n"));
    888 		goto out;
    889 	}
    890 
    891 	wn = (void *)ni;
    892 
    893 	ieee80211_rssadapt_lower_rate(ic, ni, &wn->wn_rssadapt, id);
    894 out:
    895 	splx(s);
    896 	return;
    897 }
    898 
    899 static void
    900 wi_start(struct ifnet *ifp)
    901 {
    902 	struct wi_softc	*sc = ifp->if_softc;
    903 	struct ieee80211com *ic = &sc->sc_ic;
    904 	struct ieee80211_node *ni;
    905 	struct ieee80211_frame *wh;
    906 	struct ieee80211_rateset *rs;
    907 	struct wi_rssdesc *rd;
    908 	struct ieee80211_rssdesc *id;
    909 	struct mbuf *m0;
    910 	struct wi_frame frmhdr;
    911 	int cur, fid, off;
    912 
    913 	if (!sc->sc_enabled || sc->sc_invalid)
    914 		return;
    915 	if (sc->sc_flags & WI_FLAGS_OUTRANGE)
    916 		return;
    917 
    918 	memset(&frmhdr, 0, sizeof(frmhdr));
    919 	cur = sc->sc_txnext;
    920 	for (;;) {
    921 		ni = ic->ic_bss;
    922 		if (sc->sc_txd[cur].d_len != 0 ||
    923 		    SLIST_EMPTY(&sc->sc_rssdfree)) {
    924 			ifp->if_flags |= IFF_OACTIVE;
    925 			break;
    926 		}
    927 		if (!IF_IS_EMPTY(&ic->ic_mgtq)) {
    928 			IF_DEQUEUE(&ic->ic_mgtq, m0);
    929 			m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
    930 			    (caddr_t)&frmhdr.wi_ehdr);
    931 			frmhdr.wi_ehdr.ether_type = 0;
    932                         wh = mtod(m0, struct ieee80211_frame *);
    933 			ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
    934 			m0->m_pkthdr.rcvif = NULL;
    935 		} else if (ic->ic_state != IEEE80211_S_RUN)
    936                                 break;
    937 		else if (!IF_IS_EMPTY(&ic->ic_pwrsaveq)) {
    938 			struct llc *llc;
    939 
    940 			/*
    941 			 * Should these packets be processed after the
    942 			 * regular packets or before?  Since they are being
    943 			 * probed for, they are probably less time critical
    944 			 * than other packets, but, on the other hand,
    945 			 * we want the power saving nodes to go back to
    946 			 * sleep as quickly as possible to save power...
    947 			 */
    948 
    949 			IF_DEQUEUE(&ic->ic_pwrsaveq, m0);
    950                         wh = mtod(m0, struct ieee80211_frame *);
    951 			llc = (struct llc *) (wh + 1);
    952 			m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
    953 			    (caddr_t)&frmhdr.wi_ehdr);
    954 			frmhdr.wi_ehdr.ether_type = llc->llc_snap.ether_type;
    955 			ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
    956 			m0->m_pkthdr.rcvif = NULL;
    957 		} else {
    958 			IFQ_POLL(&ifp->if_snd, m0);
    959 			if (m0 == NULL) {
    960 				break;
    961 			}
    962 			IFQ_DEQUEUE(&ifp->if_snd, m0);
    963 			ifp->if_opackets++;
    964 			m_copydata(m0, 0, ETHER_HDR_LEN,
    965 			    (caddr_t)&frmhdr.wi_ehdr);
    966 #if NBPFILTER > 0
    967 			if (ifp->if_bpf)
    968 				bpf_mtap(ifp->if_bpf, m0);
    969 #endif
    970 
    971 			if ((m0 = ieee80211_encap(ifp, m0, &ni)) == NULL) {
    972 				ifp->if_oerrors++;
    973 				continue;
    974 			}
    975                         wh = mtod(m0, struct ieee80211_frame *);
    976 			if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
    977 			    !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
    978 			    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
    979 			    IEEE80211_FC0_TYPE_DATA) {
    980 				if (ni->ni_associd == 0) {
    981 					m_freem(m0);
    982 					ifp->if_oerrors++;
    983 					goto next;
    984 				}
    985 				if (ni->ni_pwrsave & IEEE80211_PS_SLEEP) {
    986 					ieee80211_pwrsave(ic, ni, m0);
    987 					continue; /* don't free node. */
    988 				}
    989 			}
    990 		}
    991 #if NBPFILTER > 0
    992 		if (ic->ic_rawbpf)
    993 			bpf_mtap(ic->ic_rawbpf, m0);
    994 #endif
    995 		frmhdr.wi_tx_ctl =
    996 		    htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX|WI_TXCNTL_TX_OK);
    997 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
    998 			frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_ALTRTRY);
    999 		if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
   1000 		    (wh->i_fc[1] & IEEE80211_FC1_WEP)) {
   1001 			if ((m0 = ieee80211_wep_crypt(ifp, m0, 1)) == NULL) {
   1002 				ifp->if_oerrors++;
   1003 				goto next;
   1004 			}
   1005 			frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
   1006 		}
   1007 
   1008 		wi_choose_rate(ic, ni, wh, m0->m_pkthdr.len);
   1009 
   1010 #if NBPFILTER > 0
   1011 		if (sc->sc_drvbpf) {
   1012 			struct mbuf mb;
   1013 
   1014 			struct wi_tx_radiotap_header *tap = &sc->sc_txtap;
   1015 
   1016 			tap->wt_rate = ni->ni_rates.rs_rates[ni->ni_txrate];
   1017 			tap->wt_chan_freq =
   1018 			    htole16(ic->ic_bss->ni_chan->ic_freq);
   1019 			tap->wt_chan_flags =
   1020 			    htole16(ic->ic_bss->ni_chan->ic_flags);
   1021 
   1022 			/* TBD tap->wt_flags */
   1023 
   1024 			M_COPY_PKTHDR(&mb, m0);
   1025 			mb.m_data = (caddr_t)tap;
   1026 			mb.m_len = tap->wt_ihdr.it_len;
   1027 			mb.m_next = m0;
   1028 			mb.m_pkthdr.len += mb.m_len;
   1029 			bpf_mtap(sc->sc_drvbpf, &mb);
   1030 		}
   1031 #endif
   1032 		rs = &ni->ni_rates;
   1033 		rd = SLIST_FIRST(&sc->sc_rssdfree);
   1034 		id = &rd->rd_desc;
   1035 		id->id_len = m0->m_pkthdr.len;
   1036 		sc->sc_txd[cur].d_rate = id->id_rateidx = ni->ni_txrate;
   1037 		id->id_rssi = ni->ni_rssi;
   1038 
   1039 		frmhdr.wi_tx_idx = rd - sc->sc_rssd;
   1040 
   1041 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
   1042 			frmhdr.wi_tx_rate = 5 * (rs->rs_rates[ni->ni_txrate] &
   1043 			    IEEE80211_RATE_VAL);
   1044 		else if (sc->sc_flags & WI_FLAGS_RSSADAPTSTA)
   1045 			(void)wi_write_txrate(sc, rs->rs_rates[ni->ni_txrate]);
   1046 
   1047 		m_copydata(m0, 0, sizeof(struct ieee80211_frame),
   1048 		    (caddr_t)&frmhdr.wi_whdr);
   1049 		m_adj(m0, sizeof(struct ieee80211_frame));
   1050 		frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
   1051 		if (IFF_DUMPPKTS(ifp))
   1052 			wi_dump_pkt(&frmhdr, ni, -1);
   1053 		fid = sc->sc_txd[cur].d_fid;
   1054 		off = sizeof(frmhdr);
   1055 		if (wi_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0 ||
   1056 		    wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0) {
   1057 			ifp->if_oerrors++;
   1058 			m_freem(m0);
   1059 			goto next;
   1060 		}
   1061 		m_freem(m0);
   1062 		sc->sc_txd[cur].d_len = off;
   1063 		if (sc->sc_txcur == cur) {
   1064 			if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
   1065 				printf("%s: xmit failed\n",
   1066 				    sc->sc_dev.dv_xname);
   1067 				sc->sc_txd[cur].d_len = 0;
   1068 				goto next;
   1069 			}
   1070 			sc->sc_txpending[ni->ni_txrate]++;
   1071 			sc->sc_tx_timer = 5;
   1072 			ifp->if_timer = 1;
   1073 		}
   1074 		sc->sc_txnext = cur = (cur + 1) % WI_NTXBUF;
   1075 		SLIST_REMOVE_HEAD(&sc->sc_rssdfree, rd_next);
   1076 		id->id_node = ni;
   1077 		continue;
   1078 next:
   1079 		if (ni != NULL && ni != ic->ic_bss)
   1080 			ieee80211_free_node(ic, ni);
   1081 	}
   1082 }
   1083 
   1084 
   1085 static int
   1086 wi_reset(struct wi_softc *sc)
   1087 {
   1088 	int i, error;
   1089 
   1090 	DPRINTF(("wi_reset\n"));
   1091 
   1092 	if (sc->sc_reset)
   1093 		(*sc->sc_reset)(sc);
   1094 
   1095 	error = 0;
   1096 	for (i = 0; i < 5; i++) {
   1097 		DELAY(20*1000);	/* XXX: way too long! */
   1098 		if ((error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0)) == 0)
   1099 			break;
   1100 	}
   1101 	if (error) {
   1102 		printf("%s: init failed\n", sc->sc_dev.dv_xname);
   1103 		return error;
   1104 	}
   1105 	CSR_WRITE_2(sc, WI_INT_EN, 0);
   1106 	CSR_WRITE_2(sc, WI_EVENT_ACK, ~0);
   1107 
   1108 	/* Calibrate timer. */
   1109 	wi_write_val(sc, WI_RID_TICK_TIME, 0);
   1110 	return 0;
   1111 }
   1112 
   1113 static void
   1114 wi_watchdog(struct ifnet *ifp)
   1115 {
   1116 	struct wi_softc *sc = ifp->if_softc;
   1117 	struct ieee80211com *ic = &sc->sc_ic;
   1118 
   1119 	ifp->if_timer = 0;
   1120 	if (!sc->sc_enabled)
   1121 		return;
   1122 
   1123 	if (sc->sc_tx_timer) {
   1124 		if (--sc->sc_tx_timer == 0) {
   1125 			printf("%s: device timeout\n", ifp->if_xname);
   1126 			ifp->if_oerrors++;
   1127 			wi_init(ifp);
   1128 			return;
   1129 		}
   1130 		ifp->if_timer = 1;
   1131 	}
   1132 
   1133 	if (sc->sc_scan_timer) {
   1134 		if (--sc->sc_scan_timer <= WI_SCAN_WAIT - WI_SCAN_INQWAIT &&
   1135 		    sc->sc_firmware_type == WI_INTERSIL) {
   1136 			DPRINTF(("wi_watchdog: inquire scan\n"));
   1137 			wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
   1138 		}
   1139 		if (sc->sc_scan_timer)
   1140 			ifp->if_timer = 1;
   1141 	}
   1142 
   1143 	if (sc->sc_syn_timer) {
   1144 		if (--sc->sc_syn_timer == 0) {
   1145 			DPRINTF2(("%s: %d false syns\n",
   1146 			    sc->sc_dev.dv_xname, sc->sc_false_syns));
   1147 			sc->sc_false_syns = 0;
   1148 			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
   1149 			sc->sc_syn_timer = 5;
   1150 		}
   1151 		ifp->if_timer = 1;
   1152 	}
   1153 
   1154 	/* TODO: rate control */
   1155 	ieee80211_watchdog(ifp);
   1156 }
   1157 
   1158 static int
   1159 wi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
   1160 {
   1161 	struct wi_softc *sc = ifp->if_softc;
   1162 	struct ieee80211com *ic = &sc->sc_ic;
   1163 	struct ifreq *ifr = (struct ifreq *)data;
   1164 	int s, error = 0;
   1165 
   1166 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
   1167 		return ENXIO;
   1168 
   1169 	s = splnet();
   1170 
   1171 	switch (cmd) {
   1172 	case SIOCSIFFLAGS:
   1173 		/*
   1174 		 * Can't do promisc and hostap at the same time.  If all that's
   1175 		 * changing is the promisc flag, try to short-circuit a call to
   1176 		 * wi_init() by just setting PROMISC in the hardware.
   1177 		 */
   1178 		if (ifp->if_flags & IFF_UP) {
   1179 			if (sc->sc_enabled) {
   1180 				if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
   1181 				    (ifp->if_flags & IFF_PROMISC) != 0)
   1182 					wi_write_val(sc, WI_RID_PROMISC, 1);
   1183 				else
   1184 					wi_write_val(sc, WI_RID_PROMISC, 0);
   1185 			} else
   1186 				error = wi_init(ifp);
   1187 		} else if (sc->sc_enabled)
   1188 			wi_stop(ifp, 1);
   1189 		break;
   1190 	case SIOCSIFMEDIA:
   1191 	case SIOCGIFMEDIA:
   1192 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
   1193 		break;
   1194 	case SIOCADDMULTI:
   1195 	case SIOCDELMULTI:
   1196 		error = (cmd == SIOCADDMULTI) ?
   1197 		    ether_addmulti(ifr, &sc->sc_ic.ic_ec) :
   1198 		    ether_delmulti(ifr, &sc->sc_ic.ic_ec);
   1199 		if (error == ENETRESET) {
   1200 			if (sc->sc_enabled) {
   1201 				/* do not rescan */
   1202 				error = wi_write_multi(sc);
   1203 			} else
   1204 				error = 0;
   1205 		}
   1206 		break;
   1207 	case SIOCGIFGENERIC:
   1208 		error = wi_get_cfg(ifp, cmd, data);
   1209 		break;
   1210 	case SIOCSIFGENERIC:
   1211 		error = suser(curproc->p_ucred, &curproc->p_acflag);
   1212 		if (error)
   1213 			break;
   1214 		error = wi_set_cfg(ifp, cmd, data);
   1215 		if (error == ENETRESET) {
   1216 			if (sc->sc_enabled)
   1217 				error = wi_init(ifp);
   1218 			else
   1219 				error = 0;
   1220 		}
   1221 		break;
   1222 	case SIOCS80211BSSID:
   1223 		if (sc->sc_firmware_type == WI_LUCENT) {
   1224 			error = ENODEV;
   1225 			break;
   1226 		}
   1227 		/* fall through */
   1228 	default:
   1229 		error = ieee80211_ioctl(ifp, cmd, data);
   1230 		if (error == ENETRESET) {
   1231 			if (sc->sc_enabled)
   1232 				error = wi_init(ifp);
   1233 			else
   1234 				error = 0;
   1235 		}
   1236 		break;
   1237 	}
   1238 	splx(s);
   1239 	return error;
   1240 }
   1241 
   1242 static int
   1243 wi_media_change(struct ifnet *ifp)
   1244 {
   1245 	struct wi_softc *sc = ifp->if_softc;
   1246 	struct ieee80211com *ic = &sc->sc_ic;
   1247 	int error;
   1248 
   1249 	error = ieee80211_media_change(ifp);
   1250 	if (error == ENETRESET) {
   1251 		if (sc->sc_enabled)
   1252 			error = wi_init(ifp);
   1253 		else
   1254 			error = 0;
   1255 	}
   1256 	ifp->if_baudrate = ifmedia_baudrate(ic->ic_media.ifm_cur->ifm_media);
   1257 
   1258 	return error;
   1259 }
   1260 
   1261 static void
   1262 wi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
   1263 {
   1264 	struct wi_softc *sc = ifp->if_softc;
   1265 	struct ieee80211com *ic = &sc->sc_ic;
   1266 	u_int16_t val;
   1267 	int rate, len;
   1268 
   1269 	if (sc->sc_enabled == 0) {
   1270 		imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
   1271 		imr->ifm_status = 0;
   1272 		return;
   1273 	}
   1274 
   1275 	imr->ifm_status = IFM_AVALID;
   1276 	imr->ifm_active = IFM_IEEE80211;
   1277 	if (ic->ic_state == IEEE80211_S_RUN &&
   1278 	    (sc->sc_flags & WI_FLAGS_OUTRANGE) == 0)
   1279 		imr->ifm_status |= IFM_ACTIVE;
   1280 	len = sizeof(val);
   1281 	if (wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) != 0)
   1282 		rate = 0;
   1283 	else {
   1284 		/* convert to 802.11 rate */
   1285 		val = le16toh(val);
   1286 		rate = val * 2;
   1287 		if (sc->sc_firmware_type == WI_LUCENT) {
   1288 			if (rate == 10)
   1289 				rate = 11;	/* 5.5Mbps */
   1290 		} else {
   1291 			if (rate == 4*2)
   1292 				rate = 11;	/* 5.5Mbps */
   1293 			else if (rate == 8*2)
   1294 				rate = 22;	/* 11Mbps */
   1295 		}
   1296 	}
   1297 	imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
   1298 	switch (ic->ic_opmode) {
   1299 	case IEEE80211_M_STA:
   1300 		break;
   1301 	case IEEE80211_M_IBSS:
   1302 		imr->ifm_active |= IFM_IEEE80211_ADHOC;
   1303 		break;
   1304 	case IEEE80211_M_AHDEMO:
   1305 		imr->ifm_active |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
   1306 		break;
   1307 	case IEEE80211_M_HOSTAP:
   1308 		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
   1309 		break;
   1310 	case IEEE80211_M_MONITOR:
   1311 		imr->ifm_active |= IFM_IEEE80211_MONITOR;
   1312 		break;
   1313 	}
   1314 }
   1315 
   1316 static struct ieee80211_node *
   1317 wi_node_alloc(struct ieee80211com *ic)
   1318 {
   1319 	struct wi_node *wn =
   1320 	    malloc(sizeof(struct wi_node), M_DEVBUF, M_NOWAIT | M_ZERO);
   1321 	return wn ? &wn->wn_node : NULL;
   1322 }
   1323 
   1324 static void
   1325 wi_node_free(struct ieee80211com *ic, struct ieee80211_node *ni)
   1326 {
   1327 	struct wi_softc *sc = ic->ic_if.if_softc;
   1328 	int i;
   1329 
   1330 	for (i = 0; i < WI_NTXRSS; i++) {
   1331 		if (sc->sc_rssd[i].rd_desc.id_node == ni)
   1332 			sc->sc_rssd[i].rd_desc.id_node = NULL;
   1333 	}
   1334 	free(ni, M_DEVBUF);
   1335 }
   1336 
   1337 static void
   1338 wi_node_copy(struct ieee80211com *ic, struct ieee80211_node *dst,
   1339     const struct ieee80211_node *src)
   1340 {
   1341 	*(struct wi_node *)dst = *(const struct wi_node *)src;
   1342 }
   1343 
   1344 static void
   1345 wi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
   1346 {
   1347 	struct ieee80211com *ic = &sc->sc_ic;
   1348 	struct ieee80211_node *ni = ic->ic_bss;
   1349 	struct ifnet *ifp = &ic->ic_if;
   1350 
   1351 	if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
   1352 		return;
   1353 
   1354 	DPRINTF(("wi_sync_bssid: bssid %s -> ", ether_sprintf(ni->ni_bssid)));
   1355 	DPRINTF(("%s ?\n", ether_sprintf(new_bssid)));
   1356 
   1357 	/* In promiscuous mode, the BSSID field is not a reliable
   1358 	 * indicator of the firmware's BSSID. Damp spurious
   1359 	 * change-of-BSSID indications.
   1360 	 */
   1361 	if ((ifp->if_flags & IFF_PROMISC) != 0 &&
   1362 	    sc->sc_false_syns >= WI_MAX_FALSE_SYNS)
   1363 		return;
   1364 
   1365 	ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
   1366 }
   1367 
   1368 static __inline void
   1369 wi_rssadapt_input(struct ieee80211com *ic, struct ieee80211_node *ni,
   1370     struct ieee80211_frame *wh, int rssi)
   1371 {
   1372 	struct wi_node *wn;
   1373 
   1374 	if (ni == NULL) {
   1375 		printf("%s: null node", __func__);
   1376 		return;
   1377 	}
   1378 
   1379 	wn = (void*)ni;
   1380 	ieee80211_rssadapt_input(ic, ni, &wn->wn_rssadapt, rssi);
   1381 }
   1382 
   1383 static void
   1384 wi_rx_intr(struct wi_softc *sc)
   1385 {
   1386 	struct ieee80211com *ic = &sc->sc_ic;
   1387 	struct ifnet *ifp = &ic->ic_if;
   1388 	struct ieee80211_node *ni;
   1389 	struct wi_frame frmhdr;
   1390 	struct mbuf *m;
   1391 	struct ieee80211_frame *wh;
   1392 	int fid, len, off, rssi;
   1393 	u_int8_t dir;
   1394 	u_int16_t status;
   1395 	u_int32_t rstamp;
   1396 
   1397 	fid = CSR_READ_2(sc, WI_RX_FID);
   1398 
   1399 	/* First read in the frame header */
   1400 	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
   1401 		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
   1402 		ifp->if_ierrors++;
   1403 		DPRINTF(("wi_rx_intr: read fid %x failed\n", fid));
   1404 		return;
   1405 	}
   1406 
   1407 	if (IFF_DUMPPKTS(ifp))
   1408 		wi_dump_pkt(&frmhdr, NULL, frmhdr.wi_rx_signal);
   1409 
   1410 	/*
   1411 	 * Drop undecryptable or packets with receive errors here
   1412 	 */
   1413 	status = le16toh(frmhdr.wi_status);
   1414 	if ((status & WI_STAT_ERRSTAT) != 0 &&
   1415 	    ic->ic_opmode != IEEE80211_M_MONITOR) {
   1416 		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
   1417 		ifp->if_ierrors++;
   1418 		DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
   1419 		return;
   1420 	}
   1421 	rssi = frmhdr.wi_rx_signal;
   1422 	rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
   1423 	    le16toh(frmhdr.wi_rx_tstamp1);
   1424 
   1425 	len = le16toh(frmhdr.wi_dat_len);
   1426 	off = ALIGN(sizeof(struct ieee80211_frame));
   1427 
   1428 	/* Sometimes the PRISM2.x returns bogusly large frames. Except
   1429 	 * in monitor mode, just throw them away.
   1430 	 */
   1431 	if (off + len > MCLBYTES) {
   1432 		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
   1433 			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
   1434 			ifp->if_ierrors++;
   1435 			DPRINTF(("wi_rx_intr: oversized packet\n"));
   1436 			return;
   1437 		} else
   1438 			len = 0;
   1439 	}
   1440 
   1441 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1442 	if (m == NULL) {
   1443 		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
   1444 		ifp->if_ierrors++;
   1445 		DPRINTF(("wi_rx_intr: MGET failed\n"));
   1446 		return;
   1447 	}
   1448 	if (off + len > MHLEN) {
   1449 		MCLGET(m, M_DONTWAIT);
   1450 		if ((m->m_flags & M_EXT) == 0) {
   1451 			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
   1452 			m_freem(m);
   1453 			ifp->if_ierrors++;
   1454 			DPRINTF(("wi_rx_intr: MCLGET failed\n"));
   1455 			return;
   1456 		}
   1457 	}
   1458 
   1459 	m->m_data += off - sizeof(struct ieee80211_frame);
   1460 	memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
   1461 	wi_read_bap(sc, fid, sizeof(frmhdr),
   1462 	    m->m_data + sizeof(struct ieee80211_frame), len);
   1463 	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
   1464 	m->m_pkthdr.rcvif = ifp;
   1465 
   1466 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
   1467 
   1468 #if NBPFILTER > 0
   1469 	if (sc->sc_drvbpf) {
   1470 		struct mbuf mb;
   1471 		struct wi_rx_radiotap_header *tap = &sc->sc_rxtap;
   1472 
   1473 		tap->wr_rate = frmhdr.wi_rx_rate / 5;
   1474 		tap->wr_antsignal = WI_RSSI_TO_DBM(sc, frmhdr.wi_rx_signal);
   1475 		tap->wr_antnoise = WI_RSSI_TO_DBM(sc, frmhdr.wi_rx_silence);
   1476 
   1477 		tap->wr_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
   1478 		tap->wr_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
   1479 		if (frmhdr.wi_status & WI_STAT_PCF)
   1480 			tap->wr_flags |= IEEE80211_RADIOTAP_F_CFP;
   1481 
   1482 		M_COPY_PKTHDR(&mb, m);
   1483 		mb.m_data = (caddr_t)tap;
   1484 		mb.m_len = tap->wr_ihdr.it_len;
   1485 		mb.m_next = m;
   1486 		mb.m_pkthdr.len += mb.m_len;
   1487 		bpf_mtap(sc->sc_drvbpf, &mb);
   1488 	}
   1489 #endif
   1490 	wh = mtod(m, struct ieee80211_frame *);
   1491 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
   1492 		/*
   1493 		 * WEP is decrypted by hardware. Clear WEP bit
   1494 		 * header for ieee80211_input().
   1495 		 */
   1496 		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
   1497 	}
   1498 
   1499 	/* synchronize driver's BSSID with firmware's BSSID */
   1500 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
   1501 	if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
   1502 		wi_sync_bssid(sc, wh->i_addr3);
   1503 
   1504 	ni = ieee80211_find_rxnode(ic, wh);
   1505 
   1506 	ieee80211_input(ifp, m, ni, rssi, rstamp);
   1507 
   1508 	wi_rssadapt_input(ic, ni, wh, rssi);
   1509 
   1510 	/*
   1511 	 * The frame may have caused the node to be marked for
   1512 	 * reclamation (e.g. in response to a DEAUTH message)
   1513 	 * so use free_node here instead of unref_node.
   1514 	 */
   1515 	if (ni == ic->ic_bss)
   1516 		ieee80211_unref_node(&ni);
   1517 	else
   1518 		ieee80211_free_node(ic, ni);
   1519 }
   1520 
   1521 static void
   1522 wi_tx_ex_intr(struct wi_softc *sc)
   1523 {
   1524 	struct ieee80211com *ic = &sc->sc_ic;
   1525 	struct ifnet *ifp = &ic->ic_if;
   1526 	struct ieee80211_node *ni;
   1527 	struct ieee80211_rssdesc *id;
   1528 	struct wi_rssdesc *rssd;
   1529 	struct wi_frame frmhdr;
   1530 	int fid;
   1531 	u_int16_t status;
   1532 
   1533 	fid = CSR_READ_2(sc, WI_TX_CMP_FID);
   1534 	/* Read in the frame header */
   1535 	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0) {
   1536 		printf("%s: %s read fid %x failed\n", sc->sc_dev.dv_xname,
   1537 		    __func__, fid);
   1538 		wi_rssdescs_reset(ic, &sc->sc_rssd, &sc->sc_rssdfree,
   1539 		    &sc->sc_txpending);
   1540 		goto out;
   1541 	}
   1542 
   1543 	if (frmhdr.wi_tx_idx >= WI_NTXRSS) {
   1544 		printf("%s: %s bad idx %02x\n",
   1545 		    sc->sc_dev.dv_xname, __func__, frmhdr.wi_tx_idx);
   1546 		wi_rssdescs_reset(ic, &sc->sc_rssd, &sc->sc_rssdfree,
   1547 		    &sc->sc_txpending);
   1548 		goto out;
   1549 	}
   1550 
   1551 	status = le16toh(frmhdr.wi_status);
   1552 
   1553 	/*
   1554 	 * Spontaneous station disconnects appear as xmit
   1555 	 * errors.  Don't announce them and/or count them
   1556 	 * as an output error.
   1557 	 */
   1558 	if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) {
   1559 		printf("%s: tx failed", sc->sc_dev.dv_xname);
   1560 		if (status & WI_TXSTAT_RET_ERR)
   1561 			printf(", retry limit exceeded");
   1562 		if (status & WI_TXSTAT_AGED_ERR)
   1563 			printf(", max transmit lifetime exceeded");
   1564 		if (status & WI_TXSTAT_DISCONNECT)
   1565 			printf(", port disconnected");
   1566 		if (status & WI_TXSTAT_FORM_ERR)
   1567 			printf(", invalid format (data len %u src %s)",
   1568 				le16toh(frmhdr.wi_dat_len),
   1569 				ether_sprintf(frmhdr.wi_ehdr.ether_shost));
   1570 		if (status & ~0xf)
   1571 			printf(", status=0x%x", status);
   1572 		printf("\n");
   1573 	}
   1574 	ifp->if_oerrors++;
   1575 	rssd = &sc->sc_rssd[frmhdr.wi_tx_idx];
   1576 	id = &rssd->rd_desc;
   1577 	if ((status & WI_TXSTAT_RET_ERR) != 0)
   1578 		wi_lower_rate(ic, id);
   1579 
   1580 	ni = id->id_node;
   1581 	id->id_node = NULL;
   1582 
   1583 	if (ni == NULL) {
   1584 		printf("%s: %s null node, rssdesc %02x\n",
   1585 		    sc->sc_dev.dv_xname, __func__, frmhdr.wi_tx_idx);
   1586 		goto out;
   1587 	}
   1588 
   1589 	if (sc->sc_txpending[id->id_rateidx]-- == 0) {
   1590 	        printf("%s: %s txpending[%i] wraparound", sc->sc_dev.dv_xname,
   1591 		    __func__, id->id_rateidx);
   1592 		sc->sc_txpending[id->id_rateidx] = 0;
   1593 	}
   1594 	if (ni != NULL && ni != ic->ic_bss)
   1595 		ieee80211_free_node(ic, ni);
   1596 	SLIST_INSERT_HEAD(&sc->sc_rssdfree, rssd, rd_next);
   1597 out:
   1598 	ifp->if_flags &= ~IFF_OACTIVE;
   1599 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
   1600 }
   1601 
   1602 static void
   1603 wi_txalloc_intr(struct wi_softc *sc)
   1604 {
   1605 	struct ieee80211com *ic = &sc->sc_ic;
   1606 	struct ifnet *ifp = &ic->ic_if;
   1607 	int fid, cur;
   1608 
   1609 	fid = CSR_READ_2(sc, WI_ALLOC_FID);
   1610 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
   1611 
   1612 	cur = sc->sc_txcur;
   1613 	if (sc->sc_txd[cur].d_fid != fid) {
   1614 		printf("%s: bad alloc %x != %x, cur %d nxt %d\n",
   1615 		    sc->sc_dev.dv_xname, fid, sc->sc_txd[cur].d_fid, cur,
   1616 		    sc->sc_txnext);
   1617 		return;
   1618 	}
   1619 	sc->sc_tx_timer = 0;
   1620 	sc->sc_txd[cur].d_len = 0;
   1621 	sc->sc_txcur = cur = (cur + 1) % WI_NTXBUF;
   1622 	if (sc->sc_txd[cur].d_len == 0)
   1623 		ifp->if_flags &= ~IFF_OACTIVE;
   1624 	else {
   1625 		if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
   1626 		    0, 0)) {
   1627 			printf("%s: xmit failed\n", sc->sc_dev.dv_xname);
   1628 			sc->sc_txd[cur].d_len = 0;
   1629 		} else {
   1630 			sc->sc_txpending[sc->sc_txd[cur].d_rate]++;
   1631 			sc->sc_tx_timer = 5;
   1632 			ifp->if_timer = 1;
   1633 		}
   1634 	}
   1635 }
   1636 
   1637 static void
   1638 wi_tx_intr(struct wi_softc *sc)
   1639 {
   1640 	struct ieee80211com *ic = &sc->sc_ic;
   1641 	struct ifnet *ifp = &ic->ic_if;
   1642 	struct ieee80211_node *ni;
   1643 	struct ieee80211_rssdesc *id;
   1644 	struct wi_rssdesc *rssd;
   1645 	struct wi_frame frmhdr;
   1646 	int fid;
   1647 
   1648 	fid = CSR_READ_2(sc, WI_TX_CMP_FID);
   1649 	/* Read in the frame header */
   1650 	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0) {
   1651 		printf("%s: %s read fid %x failed\n", sc->sc_dev.dv_xname,
   1652 		    __func__, fid);
   1653 		wi_rssdescs_reset(ic, &sc->sc_rssd, &sc->sc_rssdfree,
   1654 		    &sc->sc_txpending);
   1655 		goto out;
   1656 	}
   1657 
   1658 	if (frmhdr.wi_tx_idx >= WI_NTXRSS) {
   1659 		printf("%s: %s bad idx %02x\n",
   1660 		    sc->sc_dev.dv_xname, __func__, frmhdr.wi_tx_idx);
   1661 		wi_rssdescs_reset(ic, &sc->sc_rssd, &sc->sc_rssdfree,
   1662 		    &sc->sc_txpending);
   1663 		goto out;
   1664 	}
   1665 
   1666 	rssd = &sc->sc_rssd[frmhdr.wi_tx_idx];
   1667 	id = &rssd->rd_desc;
   1668 	wi_raise_rate(ic, id);
   1669 
   1670 	ni = id->id_node;
   1671 	id->id_node = NULL;
   1672 
   1673 	if (ni == NULL) {
   1674 		printf("%s: %s null node, rssdesc %02x\n",
   1675 		    sc->sc_dev.dv_xname, __func__, frmhdr.wi_tx_idx);
   1676 		goto out;
   1677 	}
   1678 
   1679 	if (sc->sc_txpending[id->id_rateidx]-- == 0) {
   1680 	        printf("%s: %s txpending[%i] wraparound", sc->sc_dev.dv_xname,
   1681 		    __func__, id->id_rateidx);
   1682 		sc->sc_txpending[id->id_rateidx] = 0;
   1683 	}
   1684 	if (ni != NULL && ni != ic->ic_bss)
   1685 		ieee80211_free_node(ic, ni);
   1686 	SLIST_INSERT_HEAD(&sc->sc_rssdfree, rssd, rd_next);
   1687 out:
   1688 	ifp->if_flags &= ~IFF_OACTIVE;
   1689 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX);
   1690 }
   1691 
   1692 static void
   1693 wi_info_intr(struct wi_softc *sc)
   1694 {
   1695 	struct ieee80211com *ic = &sc->sc_ic;
   1696 	struct ifnet *ifp = &ic->ic_if;
   1697 	int i, fid, len, off;
   1698 	u_int16_t ltbuf[2];
   1699 	u_int16_t stat;
   1700 	u_int32_t *ptr;
   1701 
   1702 	fid = CSR_READ_2(sc, WI_INFO_FID);
   1703 	wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
   1704 
   1705 	switch (le16toh(ltbuf[1])) {
   1706 
   1707 	case WI_INFO_LINK_STAT:
   1708 		wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
   1709 		DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
   1710 		switch (le16toh(stat)) {
   1711 		case CONNECTED:
   1712 			sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
   1713 			if (ic->ic_state == IEEE80211_S_RUN &&
   1714 			    ic->ic_opmode != IEEE80211_M_IBSS)
   1715 				break;
   1716 			/* FALLTHROUGH */
   1717 		case AP_CHANGE:
   1718 			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
   1719 			break;
   1720 		case AP_IN_RANGE:
   1721 			sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
   1722 			break;
   1723 		case AP_OUT_OF_RANGE:
   1724 			if (sc->sc_firmware_type == WI_SYMBOL &&
   1725 			    sc->sc_scan_timer > 0) {
   1726 				if (wi_cmd(sc, WI_CMD_INQUIRE,
   1727 				    WI_INFO_HOST_SCAN_RESULTS, 0, 0) != 0)
   1728 					sc->sc_scan_timer = 0;
   1729 				break;
   1730 			}
   1731 			if (ic->ic_opmode == IEEE80211_M_STA)
   1732 				sc->sc_flags |= WI_FLAGS_OUTRANGE;
   1733 			break;
   1734 		case DISCONNECTED:
   1735 		case ASSOC_FAILED:
   1736 			if (ic->ic_opmode == IEEE80211_M_STA)
   1737 				ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
   1738 			break;
   1739 		}
   1740 		break;
   1741 
   1742 	case WI_INFO_COUNTERS:
   1743 		/* some card versions have a larger stats structure */
   1744 		len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
   1745 		ptr = (u_int32_t *)&sc->sc_stats;
   1746 		off = sizeof(ltbuf);
   1747 		for (i = 0; i < len; i++, off += 2, ptr++) {
   1748 			wi_read_bap(sc, fid, off, &stat, sizeof(stat));
   1749 			stat = le16toh(stat);
   1750 #ifdef WI_HERMES_STATS_WAR
   1751 			if (stat & 0xf000)
   1752 				stat = ~stat;
   1753 #endif
   1754 			*ptr += stat;
   1755 		}
   1756 		ifp->if_collisions = sc->sc_stats.wi_tx_single_retries +
   1757 		    sc->sc_stats.wi_tx_multi_retries +
   1758 		    sc->sc_stats.wi_tx_retry_limit;
   1759 		break;
   1760 
   1761 	case WI_INFO_SCAN_RESULTS:
   1762 	case WI_INFO_HOST_SCAN_RESULTS:
   1763 		wi_scan_result(sc, fid, le16toh(ltbuf[0]));
   1764 		break;
   1765 
   1766 	default:
   1767 		DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
   1768 		    le16toh(ltbuf[1]), le16toh(ltbuf[0])));
   1769 		break;
   1770 	}
   1771 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
   1772 }
   1773 
   1774 static int
   1775 wi_write_multi(struct wi_softc *sc)
   1776 {
   1777 	struct ifnet *ifp = &sc->sc_ic.ic_if;
   1778 	int n;
   1779 	struct wi_mcast mlist;
   1780 	struct ether_multi *enm;
   1781 	struct ether_multistep estep;
   1782 
   1783 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
   1784 allmulti:
   1785 		ifp->if_flags |= IFF_ALLMULTI;
   1786 		memset(&mlist, 0, sizeof(mlist));
   1787 		return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
   1788 		    sizeof(mlist));
   1789 	}
   1790 
   1791 	n = 0;
   1792 	ETHER_FIRST_MULTI(estep, &sc->sc_ic.ic_ec, enm);
   1793 	while (enm != NULL) {
   1794 		/* Punt on ranges or too many multicast addresses. */
   1795 		if (!IEEE80211_ADDR_EQ(enm->enm_addrlo, enm->enm_addrhi) ||
   1796 		    n >= sizeof(mlist) / sizeof(mlist.wi_mcast[0]))
   1797 			goto allmulti;
   1798 
   1799 		IEEE80211_ADDR_COPY(&mlist.wi_mcast[n], enm->enm_addrlo);
   1800 		n++;
   1801 		ETHER_NEXT_MULTI(estep, enm);
   1802 	}
   1803 	ifp->if_flags &= ~IFF_ALLMULTI;
   1804 	return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
   1805 	    IEEE80211_ADDR_LEN * n);
   1806 }
   1807 
   1808 
   1809 static void
   1810 wi_read_nicid(struct wi_softc *sc)
   1811 {
   1812 	struct wi_card_ident *id;
   1813 	char *p;
   1814 	int len;
   1815 	u_int16_t ver[4];
   1816 
   1817 	/* getting chip identity */
   1818 	memset(ver, 0, sizeof(ver));
   1819 	len = sizeof(ver);
   1820 	wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
   1821 	printf("%s: using ", sc->sc_dev.dv_xname);
   1822 DPRINTF2(("wi_read_nicid: CARD_ID: %x %x %x %x\n", le16toh(ver[0]), le16toh(ver[1]), le16toh(ver[2]), le16toh(ver[3])));
   1823 
   1824 	sc->sc_firmware_type = WI_NOTYPE;
   1825 	for (id = wi_card_ident; id->card_name != NULL; id++) {
   1826 		if (le16toh(ver[0]) == id->card_id) {
   1827 			printf("%s", id->card_name);
   1828 			sc->sc_firmware_type = id->firm_type;
   1829 			break;
   1830 		}
   1831 	}
   1832 	if (sc->sc_firmware_type == WI_NOTYPE) {
   1833 		if (le16toh(ver[0]) & 0x8000) {
   1834 			printf("Unknown PRISM2 chip");
   1835 			sc->sc_firmware_type = WI_INTERSIL;
   1836 		} else {
   1837 			printf("Unknown Lucent chip");
   1838 			sc->sc_firmware_type = WI_LUCENT;
   1839 		}
   1840 	}
   1841 
   1842 	/* get primary firmware version (Only Prism chips) */
   1843 	if (sc->sc_firmware_type != WI_LUCENT) {
   1844 		memset(ver, 0, sizeof(ver));
   1845 		len = sizeof(ver);
   1846 		wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
   1847 		sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
   1848 		    le16toh(ver[3]) * 100 + le16toh(ver[1]);
   1849 	}
   1850 
   1851 	/* get station firmware version */
   1852 	memset(ver, 0, sizeof(ver));
   1853 	len = sizeof(ver);
   1854 	wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
   1855 	sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
   1856 	    le16toh(ver[3]) * 100 + le16toh(ver[1]);
   1857 	if (sc->sc_firmware_type == WI_INTERSIL &&
   1858 	    (sc->sc_sta_firmware_ver == 10102 ||
   1859 	     sc->sc_sta_firmware_ver == 20102)) {
   1860 		char ident[12];
   1861 		memset(ident, 0, sizeof(ident));
   1862 		len = sizeof(ident);
   1863 		/* value should be the format like "V2.00-11" */
   1864 		if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
   1865 		    *(p = (char *)ident) >= 'A' &&
   1866 		    p[2] == '.' && p[5] == '-' && p[8] == '\0') {
   1867 			sc->sc_firmware_type = WI_SYMBOL;
   1868 			sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
   1869 			    (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
   1870 			    (p[6] - '0') * 10 + (p[7] - '0');
   1871 		}
   1872 	}
   1873 
   1874 	printf("\n%s: %s Firmware: ", sc->sc_dev.dv_xname,
   1875 	     sc->sc_firmware_type == WI_LUCENT ? "Lucent" :
   1876 	    (sc->sc_firmware_type == WI_SYMBOL ? "Symbol" : "Intersil"));
   1877 	if (sc->sc_firmware_type != WI_LUCENT)	/* XXX */
   1878 		printf("Primary (%u.%u.%u), ",
   1879 		    sc->sc_pri_firmware_ver / 10000,
   1880 		    (sc->sc_pri_firmware_ver % 10000) / 100,
   1881 		    sc->sc_pri_firmware_ver % 100);
   1882 	printf("Station (%u.%u.%u)\n",
   1883 	    sc->sc_sta_firmware_ver / 10000,
   1884 	    (sc->sc_sta_firmware_ver % 10000) / 100,
   1885 	    sc->sc_sta_firmware_ver % 100);
   1886 }
   1887 
   1888 static int
   1889 wi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
   1890 {
   1891 	struct wi_ssid ssid;
   1892 
   1893 	if (buflen > IEEE80211_NWID_LEN)
   1894 		return ENOBUFS;
   1895 	memset(&ssid, 0, sizeof(ssid));
   1896 	ssid.wi_len = htole16(buflen);
   1897 	memcpy(ssid.wi_ssid, buf, buflen);
   1898 	return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
   1899 }
   1900 
   1901 static int
   1902 wi_get_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
   1903 {
   1904 	struct wi_softc *sc = ifp->if_softc;
   1905 	struct ieee80211com *ic = &sc->sc_ic;
   1906 	struct ifreq *ifr = (struct ifreq *)data;
   1907 	struct wi_req wreq;
   1908 	int len, n, error;
   1909 
   1910 	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
   1911 	if (error)
   1912 		return error;
   1913 	len = (wreq.wi_len - 1) * 2;
   1914 	if (len < sizeof(u_int16_t))
   1915 		return ENOSPC;
   1916 	if (len > sizeof(wreq.wi_val))
   1917 		len = sizeof(wreq.wi_val);
   1918 
   1919 	switch (wreq.wi_type) {
   1920 
   1921 	case WI_RID_IFACE_STATS:
   1922 		memcpy(wreq.wi_val, &sc->sc_stats, sizeof(sc->sc_stats));
   1923 		if (len < sizeof(sc->sc_stats))
   1924 			error = ENOSPC;
   1925 		else
   1926 			len = sizeof(sc->sc_stats);
   1927 		break;
   1928 
   1929 	case WI_RID_ENCRYPTION:
   1930 	case WI_RID_TX_CRYPT_KEY:
   1931 	case WI_RID_DEFLT_CRYPT_KEYS:
   1932 	case WI_RID_TX_RATE:
   1933 		return ieee80211_cfgget(ifp, cmd, data);
   1934 
   1935 	case WI_RID_MICROWAVE_OVEN:
   1936 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_MOR)) {
   1937 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   1938 			    &len);
   1939 			break;
   1940 		}
   1941 		wreq.wi_val[0] = htole16(sc->sc_microwave_oven);
   1942 		len = sizeof(u_int16_t);
   1943 		break;
   1944 
   1945 	case WI_RID_DBM_ADJUST:
   1946 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_DBMADJUST)) {
   1947 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   1948 			    &len);
   1949 			break;
   1950 		}
   1951 		wreq.wi_val[0] = htole16(sc->sc_dbm_offset);
   1952 		len = sizeof(u_int16_t);
   1953 		break;
   1954 
   1955 	case WI_RID_ROAMING_MODE:
   1956 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_ROAMING)) {
   1957 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   1958 			    &len);
   1959 			break;
   1960 		}
   1961 		wreq.wi_val[0] = htole16(sc->sc_roaming_mode);
   1962 		len = sizeof(u_int16_t);
   1963 		break;
   1964 
   1965 	case WI_RID_SYSTEM_SCALE:
   1966 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE)) {
   1967 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   1968 			    &len);
   1969 			break;
   1970 		}
   1971 		wreq.wi_val[0] = htole16(sc->sc_system_scale);
   1972 		len = sizeof(u_int16_t);
   1973 		break;
   1974 
   1975 	case WI_RID_FRAG_THRESH:
   1976 		if (sc->sc_enabled && (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)) {
   1977 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   1978 			    &len);
   1979 			break;
   1980 		}
   1981 		wreq.wi_val[0] = htole16(sc->sc_frag_thresh);
   1982 		len = sizeof(u_int16_t);
   1983 		break;
   1984 
   1985 	case WI_RID_READ_APS:
   1986 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
   1987 			return ieee80211_cfgget(ifp, cmd, data);
   1988 		if (sc->sc_scan_timer > 0) {
   1989 			error = EINPROGRESS;
   1990 			break;
   1991 		}
   1992 		n = sc->sc_naps;
   1993 		if (len < sizeof(n)) {
   1994 			error = ENOSPC;
   1995 			break;
   1996 		}
   1997 		if (len < sizeof(n) + sizeof(struct wi_apinfo) * n)
   1998 			n = (len - sizeof(n)) / sizeof(struct wi_apinfo);
   1999 		len = sizeof(n) + sizeof(struct wi_apinfo) * n;
   2000 		memcpy(wreq.wi_val, &n, sizeof(n));
   2001 		memcpy((caddr_t)wreq.wi_val + sizeof(n), sc->sc_aps,
   2002 		    sizeof(struct wi_apinfo) * n);
   2003 		break;
   2004 
   2005 	default:
   2006 		if (sc->sc_enabled) {
   2007 			error = wi_read_rid(sc, wreq.wi_type, wreq.wi_val,
   2008 			    &len);
   2009 			break;
   2010 		}
   2011 		switch (wreq.wi_type) {
   2012 		case WI_RID_MAX_DATALEN:
   2013 			wreq.wi_val[0] = htole16(sc->sc_max_datalen);
   2014 			len = sizeof(u_int16_t);
   2015 			break;
   2016 		case WI_RID_FRAG_THRESH:
   2017 			wreq.wi_val[0] = htole16(sc->sc_frag_thresh);
   2018 			len = sizeof(u_int16_t);
   2019 			break;
   2020 		case WI_RID_RTS_THRESH:
   2021 			wreq.wi_val[0] = htole16(sc->sc_rts_thresh);
   2022 			len = sizeof(u_int16_t);
   2023 			break;
   2024 		case WI_RID_CNFAUTHMODE:
   2025 			wreq.wi_val[0] = htole16(sc->sc_cnfauthmode);
   2026 			len = sizeof(u_int16_t);
   2027 			break;
   2028 		case WI_RID_NODENAME:
   2029 			if (len < sc->sc_nodelen + sizeof(u_int16_t)) {
   2030 				error = ENOSPC;
   2031 				break;
   2032 			}
   2033 			len = sc->sc_nodelen + sizeof(u_int16_t);
   2034 			wreq.wi_val[0] = htole16((sc->sc_nodelen + 1) / 2);
   2035 			memcpy(&wreq.wi_val[1], sc->sc_nodename,
   2036 			    sc->sc_nodelen);
   2037 			break;
   2038 		default:
   2039 			return ieee80211_cfgget(ifp, cmd, data);
   2040 		}
   2041 		break;
   2042 	}
   2043 	if (error)
   2044 		return error;
   2045 	wreq.wi_len = (len + 1) / 2 + 1;
   2046 	return copyout(&wreq, ifr->ifr_data, (wreq.wi_len + 1) * 2);
   2047 }
   2048 
   2049 static int
   2050 wi_set_cfg(struct ifnet *ifp, u_long cmd, caddr_t data)
   2051 {
   2052 	struct wi_softc *sc = ifp->if_softc;
   2053 	struct ieee80211com *ic = &sc->sc_ic;
   2054 	struct ifreq *ifr = (struct ifreq *)data;
   2055 	struct ieee80211_rateset *rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
   2056 	struct wi_req wreq;
   2057 	struct mbuf *m;
   2058 	int i, len, error;
   2059 
   2060 	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
   2061 	if (error)
   2062 		return error;
   2063 	len = (wreq.wi_len - 1) * 2;
   2064 	switch (wreq.wi_type) {
   2065 	case WI_RID_DBM_ADJUST:
   2066 		return ENODEV;
   2067 
   2068 	case WI_RID_NODENAME:
   2069 		if (le16toh(wreq.wi_val[0]) * 2 > len ||
   2070 		    le16toh(wreq.wi_val[0]) > sizeof(sc->sc_nodename)) {
   2071 			error = ENOSPC;
   2072 			break;
   2073 		}
   2074 		if (sc->sc_enabled) {
   2075 			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
   2076 			    len);
   2077 			if (error)
   2078 				break;
   2079 		}
   2080 		sc->sc_nodelen = le16toh(wreq.wi_val[0]) * 2;
   2081 		memcpy(sc->sc_nodename, &wreq.wi_val[1], sc->sc_nodelen);
   2082 		break;
   2083 
   2084 	case WI_RID_MICROWAVE_OVEN:
   2085 	case WI_RID_ROAMING_MODE:
   2086 	case WI_RID_SYSTEM_SCALE:
   2087 	case WI_RID_FRAG_THRESH:
   2088 		if (wreq.wi_type == WI_RID_MICROWAVE_OVEN &&
   2089 		    (sc->sc_flags & WI_FLAGS_HAS_MOR) == 0)
   2090 			break;
   2091 		if (wreq.wi_type == WI_RID_ROAMING_MODE &&
   2092 		    (sc->sc_flags & WI_FLAGS_HAS_ROAMING) == 0)
   2093 			break;
   2094 		if (wreq.wi_type == WI_RID_SYSTEM_SCALE &&
   2095 		    (sc->sc_flags & WI_FLAGS_HAS_SYSSCALE) == 0)
   2096 			break;
   2097 		if (wreq.wi_type == WI_RID_FRAG_THRESH &&
   2098 		    (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR) == 0)
   2099 			break;
   2100 		/* FALLTHROUGH */
   2101 	case WI_RID_RTS_THRESH:
   2102 	case WI_RID_CNFAUTHMODE:
   2103 	case WI_RID_MAX_DATALEN:
   2104 		if (sc->sc_enabled) {
   2105 			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
   2106 			    sizeof(u_int16_t));
   2107 			if (error)
   2108 				break;
   2109 		}
   2110 		switch (wreq.wi_type) {
   2111 		case WI_RID_FRAG_THRESH:
   2112 			sc->sc_frag_thresh = le16toh(wreq.wi_val[0]);
   2113 			break;
   2114 		case WI_RID_RTS_THRESH:
   2115 			sc->sc_rts_thresh = le16toh(wreq.wi_val[0]);
   2116 			break;
   2117 		case WI_RID_MICROWAVE_OVEN:
   2118 			sc->sc_microwave_oven = le16toh(wreq.wi_val[0]);
   2119 			break;
   2120 		case WI_RID_ROAMING_MODE:
   2121 			sc->sc_roaming_mode = le16toh(wreq.wi_val[0]);
   2122 			break;
   2123 		case WI_RID_SYSTEM_SCALE:
   2124 			sc->sc_system_scale = le16toh(wreq.wi_val[0]);
   2125 			break;
   2126 		case WI_RID_CNFAUTHMODE:
   2127 			sc->sc_cnfauthmode = le16toh(wreq.wi_val[0]);
   2128 			break;
   2129 		case WI_RID_MAX_DATALEN:
   2130 			sc->sc_max_datalen = le16toh(wreq.wi_val[0]);
   2131 			break;
   2132 		}
   2133 		break;
   2134 
   2135 	case WI_RID_TX_RATE:
   2136 		switch (le16toh(wreq.wi_val[0])) {
   2137 		case 3:
   2138 			ic->ic_fixed_rate = -1;
   2139 			break;
   2140 		default:
   2141 			for (i = 0; i < IEEE80211_RATE_SIZE; i++) {
   2142 				if ((rs->rs_rates[i] & IEEE80211_RATE_VAL)
   2143 				    / 2 == le16toh(wreq.wi_val[0]))
   2144 					break;
   2145 			}
   2146 			if (i == IEEE80211_RATE_SIZE)
   2147 				return EINVAL;
   2148 			ic->ic_fixed_rate = i;
   2149 		}
   2150 		if (sc->sc_enabled)
   2151 			error = wi_cfg_txrate(sc);
   2152 		break;
   2153 
   2154 	case WI_RID_SCAN_APS:
   2155 		if (sc->sc_enabled && ic->ic_opmode != IEEE80211_M_HOSTAP)
   2156 			error = wi_scan_ap(sc, 0x3fff, 0x000f);
   2157 		break;
   2158 
   2159 	case WI_RID_MGMT_XMIT:
   2160 		if (!sc->sc_enabled) {
   2161 			error = ENETDOWN;
   2162 			break;
   2163 		}
   2164 		if (ic->ic_mgtq.ifq_len > 5) {
   2165 			error = EAGAIN;
   2166 			break;
   2167 		}
   2168 		/* XXX wi_len looks in u_int8_t, not in u_int16_t */
   2169 		m = m_devget((char *)&wreq.wi_val, wreq.wi_len, 0, ifp, NULL);
   2170 		if (m == NULL) {
   2171 			error = ENOMEM;
   2172 			break;
   2173 		}
   2174 		IF_ENQUEUE(&ic->ic_mgtq, m);
   2175 		break;
   2176 
   2177 	default:
   2178 		if (sc->sc_enabled) {
   2179 			error = wi_write_rid(sc, wreq.wi_type, wreq.wi_val,
   2180 			    len);
   2181 			if (error)
   2182 				break;
   2183 		}
   2184 		error = ieee80211_cfgset(ifp, cmd, data);
   2185 		break;
   2186 	}
   2187 	return error;
   2188 }
   2189 
   2190 /* Rate is 0 for hardware auto-select, otherwise rate is
   2191  * 2, 4, 11, or 22 (units of 500Kbps).
   2192  */
   2193 static int
   2194 wi_write_txrate(struct wi_softc *sc, int rate)
   2195 {
   2196 	u_int16_t hwrate;
   2197 	int i;
   2198 
   2199 	rate = (rate & IEEE80211_RATE_VAL) / 2;
   2200 
   2201 	/* rate: 0, 1, 2, 5, 11 */
   2202 	switch (sc->sc_firmware_type) {
   2203 	case WI_LUCENT:
   2204 		switch (rate) {
   2205 		case 0:
   2206 			hwrate = 3;	/* auto */
   2207 			break;
   2208 		case 5:
   2209 			hwrate = 4;
   2210 			break;
   2211 		case 11:
   2212 			hwrate = 5;
   2213 			break;
   2214 		default:
   2215 			hwrate = rate;
   2216 			break;
   2217 		}
   2218 		break;
   2219 	default:
   2220 		/* Choose a bit according to this table.
   2221 		 *
   2222 		 * bit | data rate
   2223 		 * ----+-------------------
   2224 		 * 0   | 1Mbps
   2225 		 * 1   | 2Mbps
   2226 		 * 2   | 5.5Mbps
   2227 		 * 3   | 11Mbps
   2228 		 */
   2229 		for (i = 8; i > 0; i >>= 1) {
   2230 			if (rate >= i)
   2231 				break;
   2232 		}
   2233 		if (i == 0)
   2234 			hwrate = 0xf;	/* auto */
   2235 		else
   2236 			hwrate = i;
   2237 		break;
   2238 	}
   2239 
   2240 	if (sc->sc_tx_rate == hwrate)
   2241 		return 0;
   2242 
   2243 	if (sc->sc_if.if_flags & IFF_DEBUG)
   2244 		printf("%s: tx rate %d -> %d (%d)\n", __func__, sc->sc_tx_rate,
   2245 		    hwrate, rate);
   2246 
   2247 	sc->sc_tx_rate = hwrate;
   2248 
   2249 	return wi_write_val(sc, WI_RID_TX_RATE, sc->sc_tx_rate);
   2250 }
   2251 
   2252 static int
   2253 wi_cfg_txrate(struct wi_softc *sc)
   2254 {
   2255 	struct ieee80211com *ic = &sc->sc_ic;
   2256 	struct ieee80211_rateset *rs;
   2257 	int rate;
   2258 
   2259 	rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
   2260 
   2261 	sc->sc_tx_rate = 0; /* force write to RID */
   2262 
   2263 	if (ic->ic_fixed_rate < 0)
   2264 		rate = 0;	/* auto */
   2265 	else
   2266 		rate = rs->rs_rates[ic->ic_fixed_rate];
   2267 
   2268 	return wi_write_txrate(sc, rate);
   2269 }
   2270 
   2271 static int
   2272 wi_write_wep(struct wi_softc *sc)
   2273 {
   2274 	struct ieee80211com *ic = &sc->sc_ic;
   2275 	int error = 0;
   2276 	int i, keylen;
   2277 	u_int16_t val;
   2278 	struct wi_key wkey[IEEE80211_WEP_NKID];
   2279 
   2280 	switch (sc->sc_firmware_type) {
   2281 	case WI_LUCENT:
   2282 		val = (ic->ic_flags & IEEE80211_F_WEPON) ? 1 : 0;
   2283 		error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
   2284 		if (error)
   2285 			break;
   2286 		error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, ic->ic_wep_txkey);
   2287 		if (error)
   2288 			break;
   2289 		memset(wkey, 0, sizeof(wkey));
   2290 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   2291 			keylen = ic->ic_nw_keys[i].wk_len;
   2292 			wkey[i].wi_keylen = htole16(keylen);
   2293 			memcpy(wkey[i].wi_keydat, ic->ic_nw_keys[i].wk_key,
   2294 			    keylen);
   2295 		}
   2296 		error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
   2297 		    wkey, sizeof(wkey));
   2298 		break;
   2299 
   2300 	case WI_INTERSIL:
   2301 	case WI_SYMBOL:
   2302 		if (ic->ic_flags & IEEE80211_F_WEPON) {
   2303 			/*
   2304 			 * ONLY HWB3163 EVAL-CARD Firmware version
   2305 			 * less than 0.8 variant2
   2306 			 *
   2307 			 *   If promiscuous mode disable, Prism2 chip
   2308 			 *  does not work with WEP .
   2309 			 * It is under investigation for details.
   2310 			 * (ichiro (at) NetBSD.org)
   2311 			 */
   2312 			if (sc->sc_firmware_type == WI_INTERSIL &&
   2313 			    sc->sc_sta_firmware_ver < 802 ) {
   2314 				/* firm ver < 0.8 variant 2 */
   2315 				wi_write_val(sc, WI_RID_PROMISC, 1);
   2316 			}
   2317 			wi_write_val(sc, WI_RID_CNFAUTHMODE,
   2318 			    sc->sc_cnfauthmode);
   2319 			val = PRIVACY_INVOKED | EXCLUDE_UNENCRYPTED;
   2320 			/*
   2321 			 * Encryption firmware has a bug for HostAP mode.
   2322 			 */
   2323 			if (sc->sc_firmware_type == WI_INTERSIL &&
   2324 			    ic->ic_opmode == IEEE80211_M_HOSTAP)
   2325 				val |= HOST_ENCRYPT;
   2326 		} else {
   2327 			wi_write_val(sc, WI_RID_CNFAUTHMODE,
   2328 			    IEEE80211_AUTH_OPEN);
   2329 			val = HOST_ENCRYPT | HOST_DECRYPT;
   2330 		}
   2331 		error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
   2332 		if (error)
   2333 			break;
   2334 		error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY,
   2335 		    ic->ic_wep_txkey);
   2336 		if (error)
   2337 			break;
   2338 		/*
   2339 		 * It seems that the firmware accept 104bit key only if
   2340 		 * all the keys have 104bit length.  We get the length of
   2341 		 * the transmit key and use it for all other keys.
   2342 		 * Perhaps we should use software WEP for such situation.
   2343 		 */
   2344 		keylen = ic->ic_nw_keys[ic->ic_wep_txkey].wk_len;
   2345 		if (keylen > IEEE80211_WEP_KEYLEN)
   2346 			keylen = 13;	/* 104bit keys */
   2347 		else
   2348 			keylen = IEEE80211_WEP_KEYLEN;
   2349 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   2350 			error = wi_write_rid(sc, WI_RID_P2_CRYPT_KEY0 + i,
   2351 			    ic->ic_nw_keys[i].wk_key, keylen);
   2352 			if (error)
   2353 				break;
   2354 		}
   2355 		break;
   2356 	}
   2357 	return error;
   2358 }
   2359 
   2360 /* Must be called at proper protection level! */
   2361 static int
   2362 wi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
   2363 {
   2364 	int i, status;
   2365 
   2366 	/* wait for the busy bit to clear */
   2367 	for (i = 500; i > 0; i--) {	/* 5s */
   2368 		if ((CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY) == 0)
   2369 			break;
   2370 		DELAY(10*1000);	/* 10 m sec */
   2371 	}
   2372 	if (i == 0) {
   2373 		printf("%s: wi_cmd: busy bit won't clear.\n",
   2374 		    sc->sc_dev.dv_xname);
   2375 		return(ETIMEDOUT);
   2376   	}
   2377 	CSR_WRITE_2(sc, WI_PARAM0, val0);
   2378 	CSR_WRITE_2(sc, WI_PARAM1, val1);
   2379 	CSR_WRITE_2(sc, WI_PARAM2, val2);
   2380 	CSR_WRITE_2(sc, WI_COMMAND, cmd);
   2381 
   2382 	if (cmd == WI_CMD_INI) {
   2383 		/* XXX: should sleep here. */
   2384 		DELAY(100*1000);
   2385 	}
   2386 	/* wait for the cmd completed bit */
   2387 	for (i = 0; i < WI_TIMEOUT; i++) {
   2388 		if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_CMD)
   2389 			break;
   2390 		DELAY(WI_DELAY);
   2391 	}
   2392 
   2393 	status = CSR_READ_2(sc, WI_STATUS);
   2394 
   2395 	/* Ack the command */
   2396 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
   2397 
   2398 	if (i == WI_TIMEOUT) {
   2399 		printf("%s: command timed out, cmd=0x%x, arg=0x%x\n",
   2400 		    sc->sc_dev.dv_xname, cmd, val0);
   2401 		return ETIMEDOUT;
   2402 	}
   2403 
   2404 	if (status & WI_STAT_CMD_RESULT) {
   2405 		printf("%s: command failed, cmd=0x%x, arg=0x%x\n",
   2406 		    sc->sc_dev.dv_xname, cmd, val0);
   2407 		return EIO;
   2408 	}
   2409 	return 0;
   2410 }
   2411 
   2412 static int
   2413 wi_seek_bap(struct wi_softc *sc, int id, int off)
   2414 {
   2415 	int i, status;
   2416 
   2417 	CSR_WRITE_2(sc, WI_SEL0, id);
   2418 	CSR_WRITE_2(sc, WI_OFF0, off);
   2419 
   2420 	for (i = 0; ; i++) {
   2421 		status = CSR_READ_2(sc, WI_OFF0);
   2422 		if ((status & WI_OFF_BUSY) == 0)
   2423 			break;
   2424 		if (i == WI_TIMEOUT) {
   2425 			printf("%s: timeout in wi_seek to %x/%x\n",
   2426 			    sc->sc_dev.dv_xname, id, off);
   2427 			sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
   2428 			return ETIMEDOUT;
   2429 		}
   2430 		DELAY(1);
   2431 	}
   2432 	if (status & WI_OFF_ERR) {
   2433 		printf("%s: failed in wi_seek to %x/%x\n",
   2434 		    sc->sc_dev.dv_xname, id, off);
   2435 		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
   2436 		return EIO;
   2437 	}
   2438 	sc->sc_bap_id = id;
   2439 	sc->sc_bap_off = off;
   2440 	return 0;
   2441 }
   2442 
   2443 static int
   2444 wi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
   2445 {
   2446 	int error, cnt;
   2447 
   2448 	if (buflen == 0)
   2449 		return 0;
   2450 	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
   2451 		if ((error = wi_seek_bap(sc, id, off)) != 0)
   2452 			return error;
   2453 	}
   2454 	cnt = (buflen + 1) / 2;
   2455 	CSR_READ_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt);
   2456 	sc->sc_bap_off += cnt * 2;
   2457 	return 0;
   2458 }
   2459 
   2460 static int
   2461 wi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
   2462 {
   2463 	int error, cnt;
   2464 
   2465 	if (buflen == 0)
   2466 		return 0;
   2467 
   2468 #ifdef WI_HERMES_AUTOINC_WAR
   2469   again:
   2470 #endif
   2471 	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
   2472 		if ((error = wi_seek_bap(sc, id, off)) != 0)
   2473 			return error;
   2474 	}
   2475 	cnt = (buflen + 1) / 2;
   2476 	CSR_WRITE_MULTI_STREAM_2(sc, WI_DATA0, (u_int16_t *)buf, cnt);
   2477 	sc->sc_bap_off += cnt * 2;
   2478 
   2479 #ifdef WI_HERMES_AUTOINC_WAR
   2480 	/*
   2481 	 * According to the comments in the HCF Light code, there is a bug
   2482 	 * in the Hermes (or possibly in certain Hermes firmware revisions)
   2483 	 * where the chip's internal autoincrement counter gets thrown off
   2484 	 * during data writes:  the autoincrement is missed, causing one
   2485 	 * data word to be overwritten and subsequent words to be written to
   2486 	 * the wrong memory locations. The end result is that we could end
   2487 	 * up transmitting bogus frames without realizing it. The workaround
   2488 	 * for this is to write a couple of extra guard words after the end
   2489 	 * of the transfer, then attempt to read then back. If we fail to
   2490 	 * locate the guard words where we expect them, we preform the
   2491 	 * transfer over again.
   2492 	 */
   2493 	if ((sc->sc_flags & WI_FLAGS_BUG_AUTOINC) && (id & 0xf000) == 0) {
   2494 		CSR_WRITE_2(sc, WI_DATA0, 0x1234);
   2495 		CSR_WRITE_2(sc, WI_DATA0, 0x5678);
   2496 		wi_seek_bap(sc, id, sc->sc_bap_off);
   2497 		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
   2498 		if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
   2499 		    CSR_READ_2(sc, WI_DATA0) != 0x5678) {
   2500 			printf("%s: detect auto increment bug, try again\n",
   2501 			    sc->sc_dev.dv_xname);
   2502 			goto again;
   2503 		}
   2504 	}
   2505 #endif
   2506 	return 0;
   2507 }
   2508 
   2509 static int
   2510 wi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
   2511 {
   2512 	int error, len;
   2513 	struct mbuf *m;
   2514 
   2515 	for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
   2516 		if (m->m_len == 0)
   2517 			continue;
   2518 
   2519 		len = min(m->m_len, totlen);
   2520 
   2521 		if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
   2522 			m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
   2523 			return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
   2524 			    totlen);
   2525 		}
   2526 
   2527 		if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
   2528 			return error;
   2529 
   2530 		off += m->m_len;
   2531 		totlen -= len;
   2532 	}
   2533 	return 0;
   2534 }
   2535 
   2536 static int
   2537 wi_alloc_fid(struct wi_softc *sc, int len, int *idp)
   2538 {
   2539 	int i;
   2540 
   2541 	if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
   2542 		printf("%s: failed to allocate %d bytes on NIC\n",
   2543 		    sc->sc_dev.dv_xname, len);
   2544 		return ENOMEM;
   2545 	}
   2546 
   2547 	for (i = 0; i < WI_TIMEOUT; i++) {
   2548 		if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
   2549 			break;
   2550 		if (i == WI_TIMEOUT) {
   2551 			printf("%s: timeout in alloc\n", sc->sc_dev.dv_xname);
   2552 			return ETIMEDOUT;
   2553 		}
   2554 		DELAY(1);
   2555 	}
   2556 	*idp = CSR_READ_2(sc, WI_ALLOC_FID);
   2557 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
   2558 	return 0;
   2559 }
   2560 
   2561 static int
   2562 wi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
   2563 {
   2564 	int error, len;
   2565 	u_int16_t ltbuf[2];
   2566 
   2567 	/* Tell the NIC to enter record read mode. */
   2568 	error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
   2569 	if (error)
   2570 		return error;
   2571 
   2572 	error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
   2573 	if (error)
   2574 		return error;
   2575 
   2576 	if (le16toh(ltbuf[1]) != rid) {
   2577 		printf("%s: record read mismatch, rid=%x, got=%x\n",
   2578 		    sc->sc_dev.dv_xname, rid, le16toh(ltbuf[1]));
   2579 		return EIO;
   2580 	}
   2581 	len = max(0, le16toh(ltbuf[0]) - 1) * 2;	 /* already got rid */
   2582 	if (*buflenp < len) {
   2583 		printf("%s: record buffer is too small, "
   2584 		    "rid=%x, size=%d, len=%d\n",
   2585 		    sc->sc_dev.dv_xname, rid, *buflenp, len);
   2586 		return ENOSPC;
   2587 	}
   2588 	*buflenp = len;
   2589 	return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
   2590 }
   2591 
   2592 static int
   2593 wi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
   2594 {
   2595 	int error;
   2596 	u_int16_t ltbuf[2];
   2597 
   2598 	ltbuf[0] = htole16((buflen + 1) / 2 + 1);	 /* includes rid */
   2599 	ltbuf[1] = htole16(rid);
   2600 
   2601 	error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
   2602 	if (error)
   2603 		return error;
   2604 	error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
   2605 	if (error)
   2606 		return error;
   2607 
   2608 	return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
   2609 }
   2610 
   2611 static void
   2612 wi_rssadapt_updatestats_cb(void *arg, struct ieee80211_node *ni)
   2613 {
   2614 	struct wi_node *wn = (void*)ni;
   2615 	ieee80211_rssadapt_updatestats(&wn->wn_rssadapt);
   2616 }
   2617 
   2618 static void
   2619 wi_rssadapt_updatestats(void *arg)
   2620 {
   2621 	struct wi_softc *sc = arg;
   2622 	struct ieee80211com *ic = &sc->sc_ic;
   2623 	ieee80211_iterate_nodes(ic, wi_rssadapt_updatestats_cb, arg);
   2624 	if (ic->ic_opmode != IEEE80211_M_MONITOR &&
   2625 	    ic->ic_state == IEEE80211_S_RUN)
   2626 		callout_reset(&sc->sc_rssadapt_ch, hz / 10,
   2627 		    wi_rssadapt_updatestats, arg);
   2628 }
   2629 
   2630 static int
   2631 wi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
   2632 {
   2633 	struct wi_softc *sc = ic->ic_softc;
   2634 	struct ieee80211_node *ni = ic->ic_bss;
   2635 	int buflen;
   2636 	u_int16_t val;
   2637 	struct wi_ssid ssid;
   2638 	struct wi_macaddr bssid, old_bssid;
   2639 	enum ieee80211_state ostate;
   2640 #ifdef WI_DEBUG
   2641 	static const char *stname[] =
   2642 	    { "INIT", "SCAN", "AUTH", "ASSOC", "RUN" };
   2643 #endif /* WI_DEBUG */
   2644 
   2645 	ostate = ic->ic_state;
   2646 	DPRINTF(("wi_newstate: %s -> %s\n", stname[ostate], stname[nstate]));
   2647 
   2648 	switch (nstate) {
   2649 	case IEEE80211_S_INIT:
   2650 		if (ic->ic_opmode != IEEE80211_M_MONITOR)
   2651 			callout_stop(&sc->sc_rssadapt_ch);
   2652 		ic->ic_flags &= ~IEEE80211_F_SIBSS;
   2653 		sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
   2654 		return (*sc->sc_newstate)(ic, nstate, arg);
   2655 
   2656 	case IEEE80211_S_RUN:
   2657 		sc->sc_flags &= ~WI_FLAGS_OUTRANGE;
   2658 		buflen = IEEE80211_ADDR_LEN;
   2659 		IEEE80211_ADDR_COPY(old_bssid.wi_mac_addr, ni->ni_bssid);
   2660 		wi_read_rid(sc, WI_RID_CURRENT_BSSID, &bssid, &buflen);
   2661 		IEEE80211_ADDR_COPY(ni->ni_bssid, &bssid);
   2662 		IEEE80211_ADDR_COPY(ni->ni_macaddr, &bssid);
   2663 		buflen = sizeof(val);
   2664 		wi_read_rid(sc, WI_RID_CURRENT_CHAN, &val, &buflen);
   2665 		if (!isset(ic->ic_chan_avail, le16toh(val)))
   2666 			panic("%s: invalid channel %d\n", sc->sc_dev.dv_xname,
   2667 			    le16toh(val));
   2668 		ni->ni_chan = &ic->ic_channels[le16toh(val)];
   2669 
   2670 		if (IEEE80211_ADDR_EQ(old_bssid.wi_mac_addr, ni->ni_bssid))
   2671 			sc->sc_false_syns++;
   2672 		else
   2673 			sc->sc_false_syns = 0;
   2674 
   2675 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
   2676 			ni->ni_esslen = ic->ic_des_esslen;
   2677 			memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
   2678 			ni->ni_rates = ic->ic_sup_rates[
   2679 			    ieee80211_chan2mode(ic, ni->ni_chan)];
   2680 			ni->ni_intval = ic->ic_lintval;
   2681 			ni->ni_capinfo = IEEE80211_CAPINFO_ESS;
   2682 			if (ic->ic_flags & IEEE80211_F_WEPON)
   2683 				ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
   2684 		} else {
   2685 			buflen = sizeof(ssid);
   2686 			wi_read_rid(sc, WI_RID_CURRENT_SSID, &ssid, &buflen);
   2687 			ni->ni_esslen = le16toh(ssid.wi_len);
   2688 			if (ni->ni_esslen > IEEE80211_NWID_LEN)
   2689 				ni->ni_esslen = IEEE80211_NWID_LEN;	/*XXX*/
   2690 			memcpy(ni->ni_essid, ssid.wi_ssid, ni->ni_esslen);
   2691 			ni->ni_rates = ic->ic_sup_rates[
   2692 			    ieee80211_chan2mode(ic, ni->ni_chan)]; /*XXX*/
   2693 		}
   2694 		if (ic->ic_opmode != IEEE80211_M_MONITOR)
   2695 			callout_reset(&sc->sc_rssadapt_ch, hz / 10,
   2696 			    wi_rssadapt_updatestats, sc);
   2697 		break;
   2698 
   2699 	case IEEE80211_S_SCAN:
   2700 	case IEEE80211_S_AUTH:
   2701 	case IEEE80211_S_ASSOC:
   2702 		break;
   2703 	}
   2704 
   2705 	ic->ic_state = nstate;
   2706 	/* skip standard ieee80211 handling */
   2707 	return 0;
   2708 }
   2709 
   2710 static int
   2711 wi_set_tim(struct ieee80211com *ic, int aid, int which)
   2712 {
   2713 	struct wi_softc *sc = ic->ic_softc;
   2714 
   2715 	aid &= ~0xc000;
   2716 	if (which)
   2717 		aid |= 0x8000;
   2718 
   2719 	return wi_write_val(sc, WI_RID_SET_TIM, aid);
   2720 }
   2721 
   2722 static int
   2723 wi_scan_ap(struct wi_softc *sc, u_int16_t chanmask, u_int16_t txrate)
   2724 {
   2725 	int error = 0;
   2726 	u_int16_t val[2];
   2727 
   2728 	if (!sc->sc_enabled)
   2729 		return ENXIO;
   2730 	switch (sc->sc_firmware_type) {
   2731 	case WI_LUCENT:
   2732 		(void)wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_SCAN_RESULTS, 0, 0);
   2733 		break;
   2734 	case WI_INTERSIL:
   2735 		val[0] = htole16(chanmask);	/* channel */
   2736 		val[1] = htole16(txrate);	/* tx rate */
   2737 		error = wi_write_rid(sc, WI_RID_SCAN_REQ, val, sizeof(val));
   2738 		break;
   2739 	case WI_SYMBOL:
   2740 		/*
   2741 		 * XXX only supported on 3.x ?
   2742 		 */
   2743 		val[0] = BSCAN_BCAST | BSCAN_ONETIME;
   2744 		error = wi_write_rid(sc, WI_RID_BCAST_SCAN_REQ,
   2745 		    val, sizeof(val[0]));
   2746 		break;
   2747 	}
   2748 	if (error == 0) {
   2749 		sc->sc_scan_timer = WI_SCAN_WAIT;
   2750 		sc->sc_ic.ic_if.if_timer = 1;
   2751 		DPRINTF(("wi_scan_ap: start scanning, "
   2752 			"chanmask 0x%x txrate 0x%x\n", chanmask, txrate));
   2753 	}
   2754 	return error;
   2755 }
   2756 
   2757 static void
   2758 wi_scan_result(struct wi_softc *sc, int fid, int cnt)
   2759 {
   2760 #define	N(a)	(sizeof (a) / sizeof (a[0]))
   2761 	int i, naps, off, szbuf;
   2762 	struct wi_scan_header ws_hdr;	/* Prism2 header */
   2763 	struct wi_scan_data_p2 ws_dat;	/* Prism2 scantable*/
   2764 	struct wi_apinfo *ap;
   2765 
   2766 	off = sizeof(u_int16_t) * 2;
   2767 	memset(&ws_hdr, 0, sizeof(ws_hdr));
   2768 	switch (sc->sc_firmware_type) {
   2769 	case WI_INTERSIL:
   2770 		wi_read_bap(sc, fid, off, &ws_hdr, sizeof(ws_hdr));
   2771 		off += sizeof(ws_hdr);
   2772 		szbuf = sizeof(struct wi_scan_data_p2);
   2773 		break;
   2774 	case WI_SYMBOL:
   2775 		szbuf = sizeof(struct wi_scan_data_p2) + 6;
   2776 		break;
   2777 	case WI_LUCENT:
   2778 		szbuf = sizeof(struct wi_scan_data);
   2779 		break;
   2780 	default:
   2781 		printf("%s: wi_scan_result: unknown firmware type %u\n",
   2782 		    sc->sc_dev.dv_xname, sc->sc_firmware_type);
   2783 		naps = 0;
   2784 		goto done;
   2785 	}
   2786 	naps = (cnt * 2 + 2 - off) / szbuf;
   2787 	if (naps > N(sc->sc_aps))
   2788 		naps = N(sc->sc_aps);
   2789 	sc->sc_naps = naps;
   2790 	/* Read Data */
   2791 	ap = sc->sc_aps;
   2792 	memset(&ws_dat, 0, sizeof(ws_dat));
   2793 	for (i = 0; i < naps; i++, ap++) {
   2794 		wi_read_bap(sc, fid, off, &ws_dat,
   2795 		    (sizeof(ws_dat) < szbuf ? sizeof(ws_dat) : szbuf));
   2796 		DPRINTF2(("wi_scan_result: #%d: off %d bssid %s\n", i, off,
   2797 		    ether_sprintf(ws_dat.wi_bssid)));
   2798 		off += szbuf;
   2799 		ap->scanreason = le16toh(ws_hdr.wi_reason);
   2800 		memcpy(ap->bssid, ws_dat.wi_bssid, sizeof(ap->bssid));
   2801 		ap->channel = le16toh(ws_dat.wi_chid);
   2802 		ap->signal  = le16toh(ws_dat.wi_signal);
   2803 		ap->noise   = le16toh(ws_dat.wi_noise);
   2804 		ap->quality = ap->signal - ap->noise;
   2805 		ap->capinfo = le16toh(ws_dat.wi_capinfo);
   2806 		ap->interval = le16toh(ws_dat.wi_interval);
   2807 		ap->rate    = le16toh(ws_dat.wi_rate);
   2808 		ap->namelen = le16toh(ws_dat.wi_namelen);
   2809 		if (ap->namelen > sizeof(ap->name))
   2810 			ap->namelen = sizeof(ap->name);
   2811 		memcpy(ap->name, ws_dat.wi_name, ap->namelen);
   2812 	}
   2813 done:
   2814 	/* Done scanning */
   2815 	sc->sc_scan_timer = 0;
   2816 	DPRINTF(("wi_scan_result: scan complete: ap %d\n", naps));
   2817 #undef N
   2818 }
   2819 
   2820 static void
   2821 wi_dump_pkt(struct wi_frame *wh, struct ieee80211_node *ni, int rssi)
   2822 {
   2823 	ieee80211_dump_pkt((u_int8_t *) &wh->wi_whdr, sizeof(wh->wi_whdr),
   2824 	    ni	? ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL
   2825 		: -1,
   2826 	    rssi);
   2827 	printf(" status 0x%x rx_tstamp1 %u rx_tstamp0 0x%u rx_silence %u\n",
   2828 		le16toh(wh->wi_status), le16toh(wh->wi_rx_tstamp1),
   2829 		le16toh(wh->wi_rx_tstamp0), wh->wi_rx_silence);
   2830 	printf(" rx_signal %u rx_rate %u rx_flow %u\n",
   2831 		wh->wi_rx_signal, wh->wi_rx_rate, wh->wi_rx_flow);
   2832 	printf(" tx_rtry %u tx_rate %u tx_ctl 0x%x dat_len %u\n",
   2833 		wh->wi_tx_rtry, wh->wi_tx_rate,
   2834 		le16toh(wh->wi_tx_ctl), le16toh(wh->wi_dat_len));
   2835 	printf(" ehdr dst %s src %s type 0x%x\n",
   2836 		ether_sprintf(wh->wi_ehdr.ether_dhost),
   2837 		ether_sprintf(wh->wi_ehdr.ether_shost),
   2838 		wh->wi_ehdr.ether_type);
   2839 }
   2840