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