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