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