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