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