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