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