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