xref: /dragonfly/sys/dev/netif/wi/if_wi.c (revision 517258aa)
1 /*-
2  * Copyright (c) 1997, 1998, 1999
3  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * $FreeBSD: head/sys/dev/wi/if_wi.c 196970 2009-09-08 13:19:05Z phk $
33  */
34 
35 /*
36  * Lucent WaveLAN/IEEE 802.11 PCMCIA driver.
37  *
38  * Original FreeBSD driver written by Bill Paul <wpaul@ctr.columbia.edu>
39  * Electrical Engineering Department
40  * Columbia University, New York City
41  */
42 
43 /*
44  * The WaveLAN/IEEE adapter is the second generation of the WaveLAN
45  * from Lucent. Unlike the older cards, the new ones are programmed
46  * entirely via a firmware-driven controller called the Hermes.
47  * Unfortunately, Lucent will not release the Hermes programming manual
48  * without an NDA (if at all). What they do release is an API library
49  * called the HCF (Hardware Control Functions) which is supposed to
50  * do the device-specific operations of a device driver for you. The
51  * publically available version of the HCF library (the 'HCF Light') is
52  * a) extremely gross, b) lacks certain features, particularly support
53  * for 802.11 frames, and c) is contaminated by the GNU Public License.
54  *
55  * This driver does not use the HCF or HCF Light at all. Instead, it
56  * programs the Hermes controller directly, using information gleaned
57  * from the HCF Light code and corresponding documentation.
58  *
59  * This driver supports the ISA, PCMCIA and PCI versions of the Lucent
60  * WaveLan cards (based on the Hermes chipset), as well as the newer
61  * Prism 2 chipsets with firmware from Intersil and Symbol.
62  */
63 
64 
65 #define WI_HERMES_STATS_WAR	/* Work around stats counter bug. */
66 
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/endian.h>
70 #include <sys/sockio.h>
71 #include <sys/mbuf.h>
72 #include <sys/priv.h>
73 #include <sys/proc.h>
74 #include <sys/kernel.h>
75 #include <sys/socket.h>
76 #include <sys/module.h>
77 #include <sys/bus.h>
78 #include <sys/random.h>
79 #include <sys/syslog.h>
80 #include <sys/sysctl.h>
81 
82 #include <machine/atomic.h>
83 #include <sys/rman.h>
84 
85 #include <net/if.h>
86 #include <net/if_arp.h>
87 #include <net/ethernet.h>
88 #include <net/if_dl.h>
89 #include <net/if_llc.h>
90 #include <net/if_media.h>
91 #include <net/if_types.h>
92 #include <net/ifq_var.h>
93 
94 #include <netproto/802_11/ieee80211_var.h>
95 #include <netproto/802_11/ieee80211_ioctl.h>
96 #include <netproto/802_11/ieee80211_radiotap.h>
97 
98 #include <netinet/in.h>
99 #include <netinet/in_systm.h>
100 #include <netinet/in_var.h>
101 #include <netinet/ip.h>
102 #include <netinet/if_ether.h>
103 
104 #include <net/bpf.h>
105 
106 #include <dev/netif/wi/if_wavelan_ieee.h>
107 #include <dev/netif/wi/if_wireg.h>
108 #include <dev/netif/wi/if_wivar.h>
109 
110 static struct ieee80211vap *wi_vap_create(struct ieee80211com *ic,
111 		const char name[IFNAMSIZ], int unit, int opmode, int flags,
112 		const uint8_t bssid[IEEE80211_ADDR_LEN],
113 		const uint8_t mac[IEEE80211_ADDR_LEN]);
114 static void wi_vap_delete(struct ieee80211vap *vap);
115 static void wi_stop_locked(struct wi_softc *sc, int disable);
116 static void wi_start_locked(struct ifnet *);
117 static void wi_start(struct ifnet *, struct ifaltq_subque *);
118 static int  wi_start_tx(struct ifnet *ifp, struct wi_frame *frmhdr,
119 		struct mbuf *m0);
120 static int  wi_raw_xmit(struct ieee80211_node *, struct mbuf *,
121 		const struct ieee80211_bpf_params *);
122 static int  wi_newstate_sta(struct ieee80211vap *, enum ieee80211_state, int);
123 static int  wi_newstate_hostap(struct ieee80211vap *, enum ieee80211_state,
124 		int);
125 static void wi_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
126 		int subtype, int rssi, int nf);
127 static int  wi_reset(struct wi_softc *);
128 static void wi_watchdog_callout(void *);
129 static int  wi_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
130 static void wi_media_status(struct ifnet *, struct ifmediareq *);
131 
132 static void wi_rx_intr(struct wi_softc *);
133 static void wi_tx_intr(struct wi_softc *);
134 static void wi_tx_ex_intr(struct wi_softc *);
135 
136 static void wi_info_intr(struct wi_softc *);
137 
138 static int  wi_write_txrate(struct wi_softc *, struct ieee80211vap *);
139 static int  wi_write_wep(struct wi_softc *, struct ieee80211vap *);
140 static int  wi_write_multi(struct wi_softc *);
141 static void wi_update_mcast(struct ifnet *);
142 static void wi_update_promisc(struct ifnet *);
143 static int  wi_alloc_fid(struct wi_softc *, int, int *);
144 static void wi_read_nicid(struct wi_softc *);
145 static int  wi_write_ssid(struct wi_softc *, int, u_int8_t *, int);
146 
147 static int  wi_cmd(struct wi_softc *, int, int, int, int);
148 static int  wi_seek_bap(struct wi_softc *, int, int);
149 static int  wi_read_bap(struct wi_softc *, int, int, void *, int);
150 static int  wi_write_bap(struct wi_softc *, int, int, void *, int);
151 static int  wi_mwrite_bap(struct wi_softc *, int, int, struct mbuf *, int);
152 static int  wi_read_rid(struct wi_softc *, int, void *, int *);
153 static int  wi_write_rid(struct wi_softc *, int, void *, int);
154 static int  wi_write_appie(struct wi_softc *, int, const struct ieee80211_appie *);
155 
156 static void wi_scan_start(struct ieee80211com *);
157 static void wi_scan_end(struct ieee80211com *);
158 static void wi_set_channel(struct ieee80211com *);
159 
160 static __inline int
161 wi_write_val(struct wi_softc *sc, int rid, u_int16_t val)
162 {
163 
164 	val = htole16(val);
165 	return wi_write_rid(sc, rid, &val, sizeof(val));
166 }
167 
168 SYSCTL_NODE(_hw, OID_AUTO, wi, CTLFLAG_RD, 0, "Wireless driver parameters");
169 
170 static	struct timeval lasttxerror;	/* time of last tx error msg */
171 static	int curtxeps;			/* current tx error msgs/sec */
172 static	int wi_txerate = 0;		/* tx error rate: max msgs/sec */
173 SYSCTL_INT(_hw_wi, OID_AUTO, txerate, CTLFLAG_RW, &wi_txerate,
174 	    0, "max tx error msgs/sec; 0 to disable msgs");
175 
176 #define	WI_DEBUG
177 #ifdef WI_DEBUG
178 static	int wi_debug = 0;
179 SYSCTL_INT(_hw_wi, OID_AUTO, debug, CTLFLAG_RW, &wi_debug,
180 	    0, "control debugging printfs");
181 #define	DPRINTF(X)	if (wi_debug) kprintf X
182 #else
183 #define	DPRINTF(X)
184 #endif
185 
186 #define WI_INTRS	(WI_EV_RX | WI_EV_ALLOC | WI_EV_INFO)
187 
188 struct wi_card_ident wi_card_ident[] = {
189 	/* CARD_ID			CARD_NAME		FIRM_TYPE */
190 	{ WI_NIC_LUCENT_ID,		WI_NIC_LUCENT_STR,	WI_LUCENT },
191 	{ WI_NIC_SONY_ID,		WI_NIC_SONY_STR,	WI_LUCENT },
192 	{ WI_NIC_LUCENT_EMB_ID,		WI_NIC_LUCENT_EMB_STR,	WI_LUCENT },
193 	{ WI_NIC_EVB2_ID,		WI_NIC_EVB2_STR,	WI_INTERSIL },
194 	{ WI_NIC_HWB3763_ID,		WI_NIC_HWB3763_STR,	WI_INTERSIL },
195 	{ WI_NIC_HWB3163_ID,		WI_NIC_HWB3163_STR,	WI_INTERSIL },
196 	{ WI_NIC_HWB3163B_ID,		WI_NIC_HWB3163B_STR,	WI_INTERSIL },
197 	{ WI_NIC_EVB3_ID,		WI_NIC_EVB3_STR,	WI_INTERSIL },
198 	{ WI_NIC_HWB1153_ID,		WI_NIC_HWB1153_STR,	WI_INTERSIL },
199 	{ WI_NIC_P2_SST_ID,		WI_NIC_P2_SST_STR,	WI_INTERSIL },
200 	{ WI_NIC_EVB2_SST_ID,		WI_NIC_EVB2_SST_STR,	WI_INTERSIL },
201 	{ WI_NIC_3842_EVA_ID,		WI_NIC_3842_EVA_STR,	WI_INTERSIL },
202 	{ WI_NIC_3842_PCMCIA_AMD_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
203 	{ WI_NIC_3842_PCMCIA_SST_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
204 	{ WI_NIC_3842_PCMCIA_ATL_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
205 	{ WI_NIC_3842_PCMCIA_ATS_ID,	WI_NIC_3842_PCMCIA_STR,	WI_INTERSIL },
206 	{ WI_NIC_3842_MINI_AMD_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
207 	{ WI_NIC_3842_MINI_SST_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
208 	{ WI_NIC_3842_MINI_ATL_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
209 	{ WI_NIC_3842_MINI_ATS_ID,	WI_NIC_3842_MINI_STR,	WI_INTERSIL },
210 	{ WI_NIC_3842_PCI_AMD_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
211 	{ WI_NIC_3842_PCI_SST_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
212 	{ WI_NIC_3842_PCI_ATS_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
213 	{ WI_NIC_3842_PCI_ATL_ID,	WI_NIC_3842_PCI_STR,	WI_INTERSIL },
214 	{ WI_NIC_P3_PCMCIA_AMD_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
215 	{ WI_NIC_P3_PCMCIA_SST_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
216 	{ WI_NIC_P3_PCMCIA_ATL_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
217 	{ WI_NIC_P3_PCMCIA_ATS_ID,	WI_NIC_P3_PCMCIA_STR,	WI_INTERSIL },
218 	{ WI_NIC_P3_MINI_AMD_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
219 	{ WI_NIC_P3_MINI_SST_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
220 	{ WI_NIC_P3_MINI_ATL_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
221 	{ WI_NIC_P3_MINI_ATS_ID,	WI_NIC_P3_MINI_STR,	WI_INTERSIL },
222 	{ 0,	NULL,	0 },
223 };
224 
225 static char *wi_firmware_names[] = { "none", "Hermes", "Intersil", "Symbol" };
226 
227 devclass_t wi_devclass;
228 
229 int
230 wi_attach(device_t dev)
231 {
232 	struct wi_softc	*sc = device_get_softc(dev);
233 	struct ieee80211com *ic;
234 	struct ifnet *ifp;
235 	int i, nrates, buflen;
236 	u_int16_t val;
237 	u_int8_t ratebuf[2 + IEEE80211_RATE_SIZE];
238 	struct ieee80211_rateset *rs;
239 	struct sysctl_ctx_list *sctx;
240 	struct sysctl_oid *soid;
241 	static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
242 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00
243 	};
244 	int error;
245 	uint8_t macaddr[IEEE80211_ADDR_LEN];
246 
247 	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
248 	if (ifp == NULL) {
249 		device_printf(dev, "can not if_alloc\n");
250 		wi_free(dev);
251 		return ENOSPC;
252 	}
253 	ic = ifp->if_l2com;
254 
255 	sc->sc_firmware_type = WI_NOTYPE;
256 	sc->wi_cmd_count = 500;
257 	/* Reset the NIC. */
258 	if (wi_reset(sc) != 0) {
259 		wi_free(dev);
260 		return ENXIO;		/* XXX */
261 	}
262 
263 	/* Read NIC identification */
264 	wi_read_nicid(sc);
265 	switch (sc->sc_firmware_type) {
266 	case WI_LUCENT:
267 		if (sc->sc_sta_firmware_ver < 60006)
268 			goto reject;
269 		break;
270 	case WI_INTERSIL:
271 		if (sc->sc_sta_firmware_ver < 800)
272 			goto reject;
273 		break;
274 	default:
275 	reject:
276 		device_printf(dev, "Sorry, this card is not supported "
277 		    "(type %d, firmware ver %d)\n",
278 		    sc->sc_firmware_type, sc->sc_sta_firmware_ver);
279 		wi_free(dev);
280 		return EOPNOTSUPP;
281 	}
282 
283 	/* Export info about the device via sysctl */
284 	sctx = &sc->sc_sysctl_ctx;
285         sysctl_ctx_init(sctx);
286         soid = SYSCTL_ADD_NODE(sctx, SYSCTL_STATIC_CHILDREN(_hw),
287                                OID_AUTO,
288                                device_get_nameunit(sc->sc_dev),
289                                CTLFLAG_RD, 0, "");
290 	if (soid == NULL) {
291 		device_printf(sc->sc_dev, "can't add sysctl node\n");
292 		return ENXIO;
293 	}
294 
295 	SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
296 	    "firmware_type", CTLFLAG_RD,
297 	    wi_firmware_names[sc->sc_firmware_type], 0,
298 	    "Firmware type string");
299 	SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "sta_version",
300 	    CTLFLAG_RD, &sc->sc_sta_firmware_ver, 0,
301 	    "Station Firmware version");
302 	if (sc->sc_firmware_type == WI_INTERSIL)
303 		SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO,
304 		    "pri_version", CTLFLAG_RD, &sc->sc_pri_firmware_ver, 0,
305 		    "Primary Firmware version");
306 	SYSCTL_ADD_INT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "nic_id",
307 	    CTLFLAG_RD, &sc->sc_nic_id, 0, "NIC id");
308 	SYSCTL_ADD_STRING(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "nic_name",
309 	    CTLFLAG_RD, sc->sc_nic_name, 0, "NIC name");
310 
311 	callout_init(&sc->sc_watchdog);
312 
313 	/*
314 	 * Read the station address.
315 	 * And do it twice. I've seen PRISM-based cards that return
316 	 * an error when trying to read it the first time, which causes
317 	 * the probe to fail.
318 	 */
319 	buflen = IEEE80211_ADDR_LEN;
320 	error = wi_read_rid(sc, WI_RID_MAC_NODE, macaddr, &buflen);
321 	if (error != 0) {
322 		buflen = IEEE80211_ADDR_LEN;
323 		error = wi_read_rid(sc, WI_RID_MAC_NODE, macaddr, &buflen);
324 	}
325 	if (error || IEEE80211_ADDR_EQ(macaddr, empty_macaddr)) {
326 		if (error != 0)
327 			device_printf(dev, "mac read failed %d\n", error);
328 		else {
329 			device_printf(dev, "mac read failed (all zeros)\n");
330 			error = ENXIO;
331 		}
332 		wi_free(dev);
333 		return (error);
334 	}
335 
336 	ifp->if_softc = sc;
337 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
338 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
339 	ifp->if_ioctl = wi_ioctl;
340 	ifp->if_start = wi_start;
341 	ifp->if_init = wi_init;
342 	ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
343 #ifdef notyet
344 	ifq_set_ready(&ifp->if_snd);
345 #endif
346 
347 	ic->ic_ifp = ifp;
348 	ic->ic_phytype = IEEE80211_T_DS;
349 	ic->ic_opmode = IEEE80211_M_STA;
350 	ic->ic_caps = IEEE80211_C_STA
351 		    | IEEE80211_C_PMGT
352 		    | IEEE80211_C_MONITOR
353 		    ;
354 
355 	/*
356 	 * Query the card for available channels and setup the
357 	 * channel table.  We assume these are all 11b channels.
358 	 */
359 	buflen = sizeof(val);
360 	if (wi_read_rid(sc, WI_RID_CHANNEL_LIST, &val, &buflen) != 0)
361 		val = htole16(0x1fff);	/* assume 1-11 */
362 	KASSERT(val != 0, ("wi_attach: no available channels listed!"));
363 
364 	val <<= 1;			/* shift for base 1 indices */
365 	for (i = 1; i < 16; i++) {
366 		struct ieee80211_channel *c;
367 
368 		if (!isset((u_int8_t*)&val, i))
369 			continue;
370 		c = &ic->ic_channels[ic->ic_nchans++];
371 		c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_B);
372 		c->ic_flags = IEEE80211_CHAN_B;
373 		c->ic_ieee = i;
374 		/* XXX txpowers? */
375 	}
376 
377 	/*
378 	 * Set flags based on firmware version.
379 	 */
380 	switch (sc->sc_firmware_type) {
381 	case WI_LUCENT:
382 		sc->sc_ntxbuf = 1;
383 		ic->ic_caps |= IEEE80211_C_IBSS;
384 
385 		sc->sc_ibss_port = WI_PORTTYPE_BSS;
386 		sc->sc_monitor_port = WI_PORTTYPE_ADHOC;
387 		sc->sc_min_rssi = WI_LUCENT_MIN_RSSI;
388 		sc->sc_max_rssi = WI_LUCENT_MAX_RSSI;
389 		sc->sc_dbm_offset = WI_LUCENT_DBM_OFFSET;
390 		break;
391 	case WI_INTERSIL:
392 		sc->sc_ntxbuf = WI_NTXBUF;
393 		sc->sc_flags |= WI_FLAGS_HAS_FRAGTHR
394 			     |  WI_FLAGS_HAS_ROAMING;
395 		/*
396 		 * Old firmware are slow, so give peace a chance.
397 		 */
398 		if (sc->sc_sta_firmware_ver < 10000)
399 			sc->wi_cmd_count = 5000;
400 		if (sc->sc_sta_firmware_ver > 10101)
401 			sc->sc_flags |= WI_FLAGS_HAS_DBMADJUST;
402 		ic->ic_caps |= IEEE80211_C_IBSS;
403 		/*
404 		 * version 0.8.3 and newer are the only ones that are known
405 		 * to currently work.  Earlier versions can be made to work,
406 		 * at least according to the Linux driver but we require
407 		 * monitor mode so this is irrelevant.
408 		 */
409 		ic->ic_caps |= IEEE80211_C_HOSTAP;
410 		if (sc->sc_sta_firmware_ver >= 10603)
411 			sc->sc_flags |= WI_FLAGS_HAS_ENHSECURITY;
412 		if (sc->sc_sta_firmware_ver >= 10700) {
413 			/*
414 			 * 1.7.0+ have the necessary support for sta mode WPA.
415 			 */
416 			sc->sc_flags |= WI_FLAGS_HAS_WPASUPPORT;
417 			ic->ic_caps |= IEEE80211_C_WPA;
418 		}
419 
420 		sc->sc_ibss_port = WI_PORTTYPE_IBSS;
421 		sc->sc_monitor_port = WI_PORTTYPE_APSILENT;
422 		sc->sc_min_rssi = WI_PRISM_MIN_RSSI;
423 		sc->sc_max_rssi = WI_PRISM_MAX_RSSI;
424 		sc->sc_dbm_offset = WI_PRISM_DBM_OFFSET;
425 		break;
426 	}
427 
428 	/*
429 	 * Find out if we support WEP on this card.
430 	 */
431 	buflen = sizeof(val);
432 	if (wi_read_rid(sc, WI_RID_WEP_AVAIL, &val, &buflen) == 0 &&
433 	    val != htole16(0))
434 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP;
435 
436 	/* Find supported rates. */
437 	buflen = sizeof(ratebuf);
438 	rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
439 	if (wi_read_rid(sc, WI_RID_DATA_RATES, ratebuf, &buflen) == 0) {
440 		nrates = le16toh(*(u_int16_t *)ratebuf);
441 		if (nrates > IEEE80211_RATE_MAXSIZE)
442 			nrates = IEEE80211_RATE_MAXSIZE;
443 		rs->rs_nrates = 0;
444 		for (i = 0; i < nrates; i++)
445 			if (ratebuf[2+i])
446 				rs->rs_rates[rs->rs_nrates++] = ratebuf[2+i];
447 	} else {
448 		/* XXX fallback on error? */
449 	}
450 
451 	buflen = sizeof(val);
452 	if ((sc->sc_flags & WI_FLAGS_HAS_DBMADJUST) &&
453 	    wi_read_rid(sc, WI_RID_DBM_ADJUST, &val, &buflen) == 0) {
454 		sc->sc_dbm_offset = le16toh(val);
455 	}
456 
457 	sc->sc_portnum = WI_DEFAULT_PORT;
458 
459 	ieee80211_ifattach(ic, macaddr);
460 	ic->ic_raw_xmit = wi_raw_xmit;
461 	ic->ic_scan_start = wi_scan_start;
462 	ic->ic_scan_end = wi_scan_end;
463 	ic->ic_set_channel = wi_set_channel;
464 
465 	ic->ic_vap_create = wi_vap_create;
466 	ic->ic_vap_delete = wi_vap_delete;
467 	ic->ic_update_mcast = wi_update_mcast;
468 	ic->ic_update_promisc = wi_update_promisc;
469 
470 	ieee80211_radiotap_attach(ic,
471 	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
472 		WI_TX_RADIOTAP_PRESENT,
473 	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
474 		WI_RX_RADIOTAP_PRESENT);
475 
476 	if (bootverbose)
477 		ieee80211_announce(ic);
478 
479 	error = bus_setup_intr(dev, sc->irq, INTR_MPSAFE,
480 	    wi_intr, sc, &sc->wi_intrhand, NULL);
481 	if (error) {
482 		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
483 		ieee80211_ifdetach(ic);
484 		if_free(sc->sc_ifp);
485 		wi_free(dev);
486 		return error;
487 	}
488 
489 	return (0);
490 }
491 
492 int
493 wi_detach(device_t dev)
494 {
495 	struct wi_softc	*sc = device_get_softc(dev);
496 	struct ifnet *ifp = sc->sc_ifp;
497 	struct ieee80211com *ic = ifp->if_l2com;
498 
499 	/* check if device was removed */
500 	sc->wi_gone |= !bus_child_present(dev);
501 
502 	wi_stop_locked(sc, 0);
503 	ieee80211_ifdetach(ic);
504 
505 	bus_teardown_intr(dev, sc->irq, sc->wi_intrhand);
506 	if_free(sc->sc_ifp);
507 	wi_free(dev);
508 	return (0);
509 }
510 
511 static struct ieee80211vap *
512 wi_vap_create(struct ieee80211com *ic,
513 	const char name[IFNAMSIZ], int unit, int opmode, int flags,
514 	const uint8_t bssid[IEEE80211_ADDR_LEN],
515 	const uint8_t mac[IEEE80211_ADDR_LEN])
516 {
517 	struct wi_softc *sc = ic->ic_ifp->if_softc;
518 	struct wi_vap *wvp;
519 	struct ieee80211vap *vap;
520 
521 	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
522 		return NULL;
523 	wvp = (struct wi_vap *) kmalloc(sizeof(struct wi_vap),
524 	    M_80211_VAP, M_NOWAIT | M_ZERO);
525 	if (wvp == NULL)
526 		return NULL;
527 
528 	vap = &wvp->wv_vap;
529 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
530 
531 	vap->iv_max_aid = WI_MAX_AID;
532 
533 	switch (opmode) {
534 	case IEEE80211_M_STA:
535 		sc->sc_porttype = WI_PORTTYPE_BSS;
536 		wvp->wv_newstate = vap->iv_newstate;
537 		vap->iv_newstate = wi_newstate_sta;
538 		/* need to filter mgt frames to avoid confusing state machine */
539 		wvp->wv_recv_mgmt = vap->iv_recv_mgmt;
540 		vap->iv_recv_mgmt = wi_recv_mgmt;
541 		break;
542 	case IEEE80211_M_IBSS:
543 		sc->sc_porttype = sc->sc_ibss_port;
544 		wvp->wv_newstate = vap->iv_newstate;
545 		vap->iv_newstate = wi_newstate_sta;
546 		break;
547 	case IEEE80211_M_AHDEMO:
548 		sc->sc_porttype = WI_PORTTYPE_ADHOC;
549 		break;
550 	case IEEE80211_M_HOSTAP:
551 		sc->sc_porttype = WI_PORTTYPE_HOSTAP;
552 		wvp->wv_newstate = vap->iv_newstate;
553 		vap->iv_newstate = wi_newstate_hostap;
554 		break;
555 	case IEEE80211_M_MONITOR:
556 		sc->sc_porttype = sc->sc_monitor_port;
557 		break;
558 	default:
559 		break;
560 	}
561 
562 	/* complete setup */
563 	ieee80211_vap_attach(vap, ieee80211_media_change, wi_media_status);
564 	ic->ic_opmode = opmode;
565 	return vap;
566 }
567 
568 static void
569 wi_vap_delete(struct ieee80211vap *vap)
570 {
571 	struct wi_vap *wvp = WI_VAP(vap);
572 
573 	ieee80211_vap_detach(vap);
574 	kfree(wvp, M_80211_VAP);
575 }
576 
577 int
578 wi_shutdown(device_t dev)
579 {
580 	struct wi_softc *sc = device_get_softc(dev);
581 
582 	wi_stop(sc, 1);
583 	return (0);
584 }
585 
586 void
587 wi_intr(void *arg)
588 {
589 	struct wi_softc *sc = arg;
590 	struct ifnet *ifp = sc->sc_ifp;
591 	u_int16_t status;
592 
593 	if (sc->wi_gone || !sc->sc_enabled || (ifp->if_flags & IFF_UP) == 0) {
594 		CSR_WRITE_2(sc, WI_INT_EN, 0);
595 		CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
596 		return;
597 	}
598 
599 	/* Disable interrupts. */
600 	CSR_WRITE_2(sc, WI_INT_EN, 0);
601 
602 	status = CSR_READ_2(sc, WI_EVENT_STAT);
603 	if (status & WI_EV_RX)
604 		wi_rx_intr(sc);
605 	if (status & WI_EV_ALLOC)
606 		wi_tx_intr(sc);
607 	if (status & WI_EV_TX_EXC)
608 		wi_tx_ex_intr(sc);
609 	if (status & WI_EV_INFO)
610 		wi_info_intr(sc);
611 	if (!ifq_is_oactive(&ifp->if_snd) && !ifq_is_empty(&ifp->if_snd))
612 		wi_start_locked(ifp);
613 
614 	/* Re-enable interrupts. */
615 	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
616 
617 	return;
618 }
619 
620 static void
621 wi_enable(struct wi_softc *sc)
622 {
623 	/* Enable interrupts */
624 	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
625 
626 	/* enable port */
627 	wi_cmd(sc, WI_CMD_ENABLE | sc->sc_portnum, 0, 0, 0);
628 	sc->sc_enabled = 1;
629 }
630 
631 static int
632 wi_setup_locked(struct wi_softc *sc, int porttype, int mode,
633 	uint8_t mac[IEEE80211_ADDR_LEN])
634 {
635 	int i;
636 
637 	wi_reset(sc);
638 
639 	wi_write_val(sc, WI_RID_PORTTYPE, porttype);
640 	wi_write_val(sc, WI_RID_CREATE_IBSS, mode);
641 	wi_write_val(sc, WI_RID_MAX_DATALEN, 2304);
642 	/* XXX IEEE80211_BPF_NOACK wants 0 */
643 	wi_write_val(sc, WI_RID_ALT_RETRY_CNT, 2);
644 	if (sc->sc_flags & WI_FLAGS_HAS_ROAMING)
645 		wi_write_val(sc, WI_RID_ROAMING_MODE, 3); /* NB: disabled */
646 
647 	wi_write_rid(sc, WI_RID_MAC_NODE, mac, IEEE80211_ADDR_LEN);
648 
649 	/* Allocate fids for the card */
650 	sc->sc_buflen = IEEE80211_MAX_LEN + sizeof(struct wi_frame);
651 	for (i = 0; i < sc->sc_ntxbuf; i++) {
652 		int error = wi_alloc_fid(sc, sc->sc_buflen,
653 		    &sc->sc_txd[i].d_fid);
654 		if (error) {
655 			device_printf(sc->sc_dev,
656 			    "tx buffer allocation failed (error %u)\n",
657 			    error);
658 			return error;
659 		}
660 		sc->sc_txd[i].d_len = 0;
661 	}
662 	sc->sc_txcur = sc->sc_txnext = 0;
663 
664 	return 0;
665 }
666 
667 static void
668 wi_init_locked(struct wi_softc *sc)
669 {
670 	struct ifnet *ifp = sc->sc_ifp;
671 	int wasenabled;
672 
673 	wasenabled = sc->sc_enabled;
674 	if (wasenabled)
675 		wi_stop_locked(sc, 1);
676 
677 	if (wi_setup_locked(sc, sc->sc_porttype, 3, IF_LLADDR(ifp)) != 0) {
678 		if_printf(ifp, "interface not running\n");
679 		wi_stop_locked(sc, 1);
680 		return;
681 	}
682 
683 	ifp->if_flags |= IFF_RUNNING;
684 	ifq_clr_oactive(&ifp->if_snd);
685 
686 	callout_reset(&sc->sc_watchdog, hz, wi_watchdog_callout, sc);
687 
688 	wi_enable(sc);			/* Enable desired port */
689 }
690 
691 void
692 wi_init(void *arg)
693 {
694 	struct wi_softc *sc = arg;
695 	struct ifnet *ifp = sc->sc_ifp;
696 	struct ieee80211com *ic = ifp->if_l2com;
697 
698 	wi_init_locked(sc);
699 
700 	if (ifp->if_flags & IFF_RUNNING)
701 		ieee80211_start_all(ic);		/* start all vap's */
702 }
703 
704 static void
705 wi_stop_locked(struct wi_softc *sc, int disable)
706 {
707 	struct ifnet *ifp = sc->sc_ifp;
708 
709 	if (sc->sc_enabled && !sc->wi_gone) {
710 		CSR_WRITE_2(sc, WI_INT_EN, 0);
711 		wi_cmd(sc, WI_CMD_DISABLE | sc->sc_portnum, 0, 0, 0);
712 		if (disable)
713 			sc->sc_enabled = 0;
714 	} else if (sc->wi_gone && disable)	/* gone --> not enabled */
715 		sc->sc_enabled = 0;
716 
717 	callout_stop(&sc->sc_watchdog);
718 	sc->sc_tx_timer = 0;
719 	sc->sc_false_syns = 0;
720 
721 	ifp->if_flags &= ~IFF_RUNNING;
722 	ifq_clr_oactive(&ifp->if_snd);
723 }
724 
725 void
726 wi_stop(struct wi_softc *sc, int disable)
727 {
728 	wi_stop_locked(sc, disable);
729 }
730 
731 static void
732 wi_set_channel(struct ieee80211com *ic)
733 {
734 	struct ifnet *ifp = ic->ic_ifp;
735 	struct wi_softc *sc = ifp->if_softc;
736 
737 	DPRINTF(("%s: channel %d, %sscanning\n", __func__,
738 	    ieee80211_chan2ieee(ic, ic->ic_curchan),
739 	    ic->ic_flags & IEEE80211_F_SCAN ? "" : "!"));
740 
741 	wi_write_val(sc, WI_RID_OWN_CHNL,
742 	    ieee80211_chan2ieee(ic, ic->ic_curchan));
743 }
744 
745 static void
746 wi_scan_start(struct ieee80211com *ic)
747 {
748 	struct ifnet *ifp = ic->ic_ifp;
749 	struct wi_softc *sc = ifp->if_softc;
750 	struct ieee80211_scan_state *ss = ic->ic_scan;
751 
752 	DPRINTF(("%s\n", __func__));
753 
754 	/*
755 	 * Switch device to monitor mode.
756 	 */
757 	wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_monitor_port);
758 	if (sc->sc_firmware_type == WI_INTERSIL) {
759 		wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
760 		wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
761 	}
762 	/* force full dwell time to compensate for firmware overhead */
763 	ss->ss_mindwell = ss->ss_maxdwell = msecs_to_ticks(400);
764 
765 }
766 
767 static void
768 wi_scan_end(struct ieee80211com *ic)
769 {
770 	struct ifnet *ifp = ic->ic_ifp;
771 	struct wi_softc *sc = ifp->if_softc;
772 
773 	DPRINTF(("%s: restore port type %d\n", __func__, sc->sc_porttype));
774 
775 	wi_write_val(sc, WI_RID_PORTTYPE, sc->sc_porttype);
776 	if (sc->sc_firmware_type == WI_INTERSIL) {
777 		wi_cmd(sc, WI_CMD_DISABLE | WI_PORT0, 0, 0, 0);
778 		wi_cmd(sc, WI_CMD_ENABLE | WI_PORT0, 0, 0, 0);
779 	}
780 }
781 
782 static void
783 wi_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
784 	int subtype, int rssi, int nf)
785 {
786 	struct ieee80211vap *vap = ni->ni_vap;
787 
788 	switch (subtype) {
789 	case IEEE80211_FC0_SUBTYPE_AUTH:
790 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
791 	case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
792 		/* NB: filter frames that trigger state changes */
793 		return;
794 	}
795 	WI_VAP(vap)->wv_recv_mgmt(ni, m, subtype, rssi, nf);
796 }
797 
798 static int
799 wi_newstate_sta(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
800 {
801 	struct ieee80211com *ic = vap->iv_ic;
802 	struct ifnet *ifp = ic->ic_ifp;
803 	struct ieee80211_node *bss;
804 	struct wi_softc *sc = ifp->if_softc;
805 
806 	DPRINTF(("%s: %s -> %s\n", __func__,
807 		ieee80211_state_name[vap->iv_state],
808 		ieee80211_state_name[nstate]));
809 
810 	if (nstate == IEEE80211_S_AUTH) {
811 		wi_setup_locked(sc, WI_PORTTYPE_BSS, 3, vap->iv_myaddr);
812 
813 		if (vap->iv_flags & IEEE80211_F_PMGTON) {
814 			wi_write_val(sc, WI_RID_MAX_SLEEP, ic->ic_lintval);
815 			wi_write_val(sc, WI_RID_PM_ENABLED, 1);
816 		}
817 		wi_write_val(sc, WI_RID_RTS_THRESH, vap->iv_rtsthreshold);
818 		if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
819 			wi_write_val(sc, WI_RID_FRAG_THRESH,
820 			    vap->iv_fragthreshold);
821 		wi_write_txrate(sc, vap);
822 
823 		bss = vap->iv_bss;
824 		wi_write_ssid(sc, WI_RID_DESIRED_SSID, bss->ni_essid, bss->ni_esslen);
825 		wi_write_val(sc, WI_RID_OWN_CHNL,
826 		    ieee80211_chan2ieee(ic, bss->ni_chan));
827 
828 		/* Configure WEP. */
829 		if (ic->ic_cryptocaps & IEEE80211_CRYPTO_WEP)
830 			wi_write_wep(sc, vap);
831 		else
832 			sc->sc_encryption = 0;
833 
834 		if ((sc->sc_flags & WI_FLAGS_HAS_WPASUPPORT) &&
835 		    (vap->iv_flags & IEEE80211_F_WPA)) {
836 			wi_write_val(sc, WI_RID_WPA_HANDLING, 1);
837 			if (vap->iv_appie_wpa != NULL)
838 				wi_write_appie(sc, WI_RID_WPA_DATA,
839 				    vap->iv_appie_wpa);
840 		}
841 
842 		wi_enable(sc);		/* enable port */
843 
844 		/* Lucent firmware does not support the JOIN RID. */
845 		if (sc->sc_firmware_type == WI_INTERSIL) {
846 			struct wi_joinreq join;
847 
848 			memset(&join, 0, sizeof(join));
849 			IEEE80211_ADDR_COPY(&join.wi_bssid, bss->ni_bssid);
850 			join.wi_chan = htole16(
851 			    ieee80211_chan2ieee(ic, bss->ni_chan));
852 			wi_write_rid(sc, WI_RID_JOIN_REQ, &join, sizeof(join));
853 		}
854 
855 		/*
856 		 * NB: don't go through 802.11 layer, it'll send auth frame;
857 		 * instead we drive the state machine from the link status
858 		 * notification we get on association.
859 		 */
860 		vap->iv_state = nstate;
861 		return (0);
862 	}
863 	return WI_VAP(vap)->wv_newstate(vap, nstate, arg);
864 }
865 
866 static int
867 wi_newstate_hostap(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
868 {
869 	struct ieee80211com *ic = vap->iv_ic;
870 	struct ifnet *ifp = ic->ic_ifp;
871 	struct ieee80211_node *bss;
872 	struct wi_softc *sc = ifp->if_softc;
873 	int error;
874 
875 	DPRINTF(("%s: %s -> %s\n", __func__,
876 		ieee80211_state_name[vap->iv_state],
877 		ieee80211_state_name[nstate]));
878 
879 	error = WI_VAP(vap)->wv_newstate(vap, nstate, arg);
880 	if (error == 0 && nstate == IEEE80211_S_RUN) {
881 		wi_setup_locked(sc, WI_PORTTYPE_HOSTAP, 0, vap->iv_myaddr);
882 
883 		bss = vap->iv_bss;
884 		wi_write_ssid(sc, WI_RID_OWN_SSID,
885 		    bss->ni_essid, bss->ni_esslen);
886 		wi_write_val(sc, WI_RID_OWN_CHNL,
887 		    ieee80211_chan2ieee(ic, bss->ni_chan));
888 		wi_write_val(sc, WI_RID_BASIC_RATE, 0x3);
889 		wi_write_val(sc, WI_RID_SUPPORT_RATE, 0xf);
890 		wi_write_txrate(sc, vap);
891 
892 		wi_write_val(sc, WI_RID_OWN_BEACON_INT, bss->ni_intval);
893 		wi_write_val(sc, WI_RID_DTIM_PERIOD, vap->iv_dtim_period);
894 
895 		wi_write_val(sc, WI_RID_RTS_THRESH, vap->iv_rtsthreshold);
896 		if (sc->sc_flags & WI_FLAGS_HAS_FRAGTHR)
897 			wi_write_val(sc, WI_RID_FRAG_THRESH,
898 			    vap->iv_fragthreshold);
899 
900 		if ((sc->sc_flags & WI_FLAGS_HAS_ENHSECURITY) &&
901 		    (vap->iv_flags & IEEE80211_F_HIDESSID)) {
902 			/*
903 			 * bit 0 means hide SSID in beacons,
904 			 * bit 1 means don't respond to bcast probe req
905 			 */
906 			wi_write_val(sc, WI_RID_ENH_SECURITY, 0x3);
907 		}
908 
909 		if ((sc->sc_flags & WI_FLAGS_HAS_WPASUPPORT) &&
910 		    (vap->iv_flags & IEEE80211_F_WPA) &&
911 		    vap->iv_appie_wpa != NULL)
912 			wi_write_appie(sc, WI_RID_WPA_DATA, vap->iv_appie_wpa);
913 
914 		wi_write_val(sc, WI_RID_PROMISC, 0);
915 
916 		/* Configure WEP. */
917 		if (ic->ic_cryptocaps & IEEE80211_CRYPTO_WEP)
918 			wi_write_wep(sc, vap);
919 		else
920 			sc->sc_encryption = 0;
921 
922 		wi_enable(sc);		/* enable port */
923 	}
924 	return error;
925 }
926 
927 static void
928 wi_start_locked(struct ifnet *ifp)
929 {
930 	struct wi_softc	*sc = ifp->if_softc;
931 	struct ieee80211_node *ni;
932 	struct ieee80211_frame *wh;
933 	struct mbuf *m0;
934 	struct ieee80211_key *k;
935 	struct wi_frame frmhdr;
936 	const struct llc *llc;
937 	int cur;
938 
939 	if (sc->wi_gone)
940 		return;
941 
942 	memset(&frmhdr, 0, sizeof(frmhdr));
943 	cur = sc->sc_txnext;
944 	for (;;) {
945 		m0 = ifq_dequeue(&ifp->if_snd);
946 		if (m0 == NULL)
947 			break;
948 		if (sc->sc_txd[cur].d_len != 0) {
949 			ifq_prepend(&ifp->if_snd, m0);
950 			ifq_set_oactive(&ifp->if_snd);
951 			break;
952 		}
953 		ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif;
954 
955 		/* reconstruct 802.3 header */
956 		wh = mtod(m0, struct ieee80211_frame *);
957 		switch (wh->i_fc[1]) {
958 		case IEEE80211_FC1_DIR_TODS:
959 			IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost,
960 			    wh->i_addr2);
961 			IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost,
962 			    wh->i_addr3);
963 			break;
964 		case IEEE80211_FC1_DIR_NODS:
965 			IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost,
966 			    wh->i_addr2);
967 			IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost,
968 			    wh->i_addr1);
969 			break;
970 		case IEEE80211_FC1_DIR_FROMDS:
971 			IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_shost,
972 			    wh->i_addr3);
973 			IEEE80211_ADDR_COPY(frmhdr.wi_ehdr.ether_dhost,
974 			    wh->i_addr1);
975 			break;
976 		}
977 		llc = (const struct llc *)(
978 		    mtod(m0, const uint8_t *) + ieee80211_hdrsize(wh));
979 		frmhdr.wi_ehdr.ether_type = llc->llc_snap.ether_type;
980 		frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
981 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
982 			k = ieee80211_crypto_encap(ni, m0);
983 			if (k == NULL) {
984 				ieee80211_free_node(ni);
985 				m_freem(m0);
986 				continue;
987 			}
988 			frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
989 		}
990 
991 		if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
992 			sc->sc_tx_th.wt_rate = ni->ni_txrate;
993 			ieee80211_radiotap_tx(ni->ni_vap, m0);
994 		}
995 
996 		m_copydata(m0, 0, sizeof(struct ieee80211_frame),
997 		    (caddr_t)&frmhdr.wi_whdr);
998 		m_adj(m0, sizeof(struct ieee80211_frame));
999 		frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
1000 		ieee80211_free_node(ni);
1001 		if (wi_start_tx(ifp, &frmhdr, m0))
1002 			continue;
1003 
1004 		sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
1005 		IFNET_STAT_INC(ifp, opackets, 1);
1006 	}
1007 }
1008 
1009 static void
1010 wi_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
1011 {
1012 	ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
1013 	wi_start_locked(ifp);
1014 }
1015 
1016 static int
1017 wi_start_tx(struct ifnet *ifp, struct wi_frame *frmhdr, struct mbuf *m0)
1018 {
1019 	struct wi_softc	*sc = ifp->if_softc;
1020 	int cur = sc->sc_txnext;
1021 	int fid, off, error;
1022 
1023 	fid = sc->sc_txd[cur].d_fid;
1024 	off = sizeof(*frmhdr);
1025 	error = wi_write_bap(sc, fid, 0, frmhdr, sizeof(*frmhdr)) != 0
1026 	     || wi_mwrite_bap(sc, fid, off, m0, m0->m_pkthdr.len) != 0;
1027 	m_freem(m0);
1028 	if (error) {
1029 		IFNET_STAT_INC(ifp, oerrors, 1);
1030 		return -1;
1031 	}
1032 	sc->sc_txd[cur].d_len = off;
1033 	if (sc->sc_txcur == cur) {
1034 		if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, fid, 0, 0)) {
1035 			if_printf(ifp, "xmit failed\n");
1036 			sc->sc_txd[cur].d_len = 0;
1037 			return -1;
1038 		}
1039 		sc->sc_tx_timer = 5;
1040 	}
1041 	return 0;
1042 }
1043 
1044 static int
1045 wi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m0,
1046 	    const struct ieee80211_bpf_params *params)
1047 {
1048 	struct ieee80211com *ic = ni->ni_ic;
1049 	struct ifnet *ifp = ic->ic_ifp;
1050 	struct ieee80211vap *vap = ni->ni_vap;
1051 	struct wi_softc	*sc = ifp->if_softc;
1052 	struct ieee80211_key *k;
1053 	struct ieee80211_frame *wh;
1054 	struct wi_frame frmhdr;
1055 	int cur;
1056 	int rc = 0;
1057 
1058 	if (sc->wi_gone) {
1059 		rc = ENETDOWN;
1060 		goto out;
1061 	}
1062 	memset(&frmhdr, 0, sizeof(frmhdr));
1063 	cur = sc->sc_txnext;
1064 	if (sc->sc_txd[cur].d_len != 0) {
1065 		ifq_set_oactive(&ifp->if_snd);
1066 		rc = ENOBUFS;
1067 		goto out;
1068 	}
1069 	m0->m_pkthdr.rcvif = NULL;
1070 
1071 	m_copydata(m0, 4, ETHER_ADDR_LEN * 2,
1072 	    (caddr_t)&frmhdr.wi_ehdr);
1073 	frmhdr.wi_ehdr.ether_type = 0;
1074 	wh = mtod(m0, struct ieee80211_frame *);
1075 
1076 	frmhdr.wi_tx_ctl = htole16(WI_ENC_TX_802_11|WI_TXCNTL_TX_EX);
1077 	if (params && (params->ibp_flags & IEEE80211_BPF_NOACK))
1078 		frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_ALTRTRY);
1079 	if ((wh->i_fc[1] & IEEE80211_FC1_WEP) &&
1080 	    (!params || (params && (params->ibp_flags & IEEE80211_BPF_CRYPTO)))) {
1081 		k = ieee80211_crypto_encap(ni, m0);
1082 		if (k == NULL) {
1083 			rc = ENOMEM;
1084 			goto out;
1085 		}
1086 		frmhdr.wi_tx_ctl |= htole16(WI_TXCNTL_NOCRYPT);
1087 	}
1088 	if (ieee80211_radiotap_active_vap(vap)) {
1089 		sc->sc_tx_th.wt_rate = ni->ni_txrate;
1090 		ieee80211_radiotap_tx(vap, m0);
1091 	}
1092 	m_copydata(m0, 0, sizeof(struct ieee80211_frame),
1093 	    (caddr_t)&frmhdr.wi_whdr);
1094 	m_adj(m0, sizeof(struct ieee80211_frame));
1095 	frmhdr.wi_dat_len = htole16(m0->m_pkthdr.len);
1096 	if (wi_start_tx(ifp, &frmhdr, m0) < 0) {
1097 		m0 = NULL;
1098 		rc = EIO;
1099 		goto out;
1100 	}
1101 	m0 = NULL;
1102 
1103 	sc->sc_txnext = cur = (cur + 1) % sc->sc_ntxbuf;
1104 out:
1105 
1106 	if (m0 != NULL)
1107 		m_freem(m0);
1108 	ieee80211_free_node(ni);
1109 	return rc;
1110 }
1111 
1112 static int
1113 wi_reset(struct wi_softc *sc)
1114 {
1115 #define WI_INIT_TRIES 3
1116 	int i, error = 0;
1117 
1118 	for (i = 0; i < WI_INIT_TRIES; i++) {
1119 		error = wi_cmd(sc, WI_CMD_INI, 0, 0, 0);
1120 		if (error == 0)
1121 			break;
1122 		DELAY(WI_DELAY * 1000);
1123 	}
1124 	sc->sc_reset = 1;
1125 	if (i == WI_INIT_TRIES) {
1126 		if_printf(sc->sc_ifp, "reset failed\n");
1127 		return error;
1128 	}
1129 
1130 	CSR_WRITE_2(sc, WI_INT_EN, 0);
1131 	CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
1132 
1133 	/* Calibrate timer. */
1134 	wi_write_val(sc, WI_RID_TICK_TIME, 8);
1135 
1136 	return 0;
1137 #undef WI_INIT_TRIES
1138 }
1139 
1140 static void
1141 wi_watchdog_callout(void *arg)
1142 {
1143 	struct wi_softc	*sc = arg;
1144 	struct ifnet *ifp = sc->sc_ifp;
1145 
1146 	if (!sc->sc_enabled)
1147 		return;
1148 
1149 	if (sc->sc_tx_timer && --sc->sc_tx_timer == 0) {
1150 		if_printf(ifp, "device timeout\n");
1151 		IFNET_STAT_INC(ifp, oerrors, 1);
1152 		wi_init_locked(ifp->if_softc);
1153 		return;
1154 	}
1155 	callout_reset(&sc->sc_watchdog, hz, wi_watchdog_callout, sc);
1156 }
1157 
1158 static int
1159 wi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *ucred)
1160 {
1161 	struct wi_softc *sc = ifp->if_softc;
1162 	struct ieee80211com *ic = ifp->if_l2com;
1163 	struct ifreq *ifr = (struct ifreq *) data;
1164 	int error = 0, startall = 0;
1165 
1166 	switch (cmd) {
1167 	case SIOCSIFFLAGS:
1168 		/*
1169 		 * Can't do promisc and hostap at the same time.  If all that's
1170 		 * changing is the promisc flag, try to short-circuit a call to
1171 		 * wi_init() by just setting PROMISC in the hardware.
1172 		 */
1173 		if (ifp->if_flags & IFF_UP) {
1174 			if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
1175 			    ifp->if_flags & IFF_RUNNING) {
1176 				if ((ifp->if_flags ^ sc->sc_if_flags) & IFF_PROMISC) {
1177 					wi_write_val(sc, WI_RID_PROMISC,
1178 					    (ifp->if_flags & IFF_PROMISC) != 0);
1179 				} else {
1180 					wi_init_locked(sc);
1181 					startall = 1;
1182 				}
1183 			} else {
1184 				wi_init_locked(sc);
1185 				startall = 1;
1186 			}
1187 		} else {
1188 			if (ifp->if_flags & IFF_RUNNING)
1189 				wi_stop_locked(sc, 1);
1190 			sc->wi_gone = 0;
1191 		}
1192 		sc->sc_if_flags = ifp->if_flags;
1193 		if (startall)
1194 			ieee80211_start_all(ic);
1195 		break;
1196 	case SIOCGIFMEDIA:
1197 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1198 		break;
1199 	case SIOCGIFADDR:
1200 		error = ether_ioctl(ifp, cmd, data);
1201 		break;
1202 	default:
1203 		error = EINVAL;
1204 		break;
1205 	}
1206 	return error;
1207 }
1208 
1209 static void
1210 wi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1211 {
1212 	struct ieee80211vap *vap = ifp->if_softc;
1213 	struct ieee80211com *ic = vap->iv_ic;
1214 	struct wi_softc *sc = ic->ic_ifp->if_softc;
1215 	u_int16_t val;
1216 	int rate, len;
1217 
1218 	len = sizeof(val);
1219 	if (sc->sc_enabled &&
1220 	    wi_read_rid(sc, WI_RID_CUR_TX_RATE, &val, &len) == 0 &&
1221 	    len == sizeof(val)) {
1222 		/* convert to 802.11 rate */
1223 		val = le16toh(val);
1224 		rate = val * 2;
1225 		if (sc->sc_firmware_type == WI_LUCENT) {
1226 			if (rate == 10)
1227 				rate = 11;	/* 5.5Mbps */
1228 		} else {
1229 			if (rate == 4*2)
1230 				rate = 11;	/* 5.5Mbps */
1231 			else if (rate == 8*2)
1232 				rate = 22;	/* 11Mbps */
1233 		}
1234 		vap->iv_bss->ni_txrate = rate;
1235 	}
1236 	ieee80211_media_status(ifp, imr);
1237 }
1238 
1239 static void
1240 wi_sync_bssid(struct wi_softc *sc, u_int8_t new_bssid[IEEE80211_ADDR_LEN])
1241 {
1242 	struct ifnet *ifp = sc->sc_ifp;
1243 	struct ieee80211com *ic = ifp->if_l2com;
1244 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1245 	struct ieee80211_node *ni = vap->iv_bss;
1246 	char ethstr[ETHER_ADDRSTRLEN + 1];
1247 
1248 	if (IEEE80211_ADDR_EQ(new_bssid, ni->ni_bssid))
1249 		return;
1250 
1251 	DPRINTF(("wi_sync_bssid: bssid %s -> ", kether_ntoa(ni->ni_bssid, ethstr)));
1252 	DPRINTF(("%s ?\n", kether_ntoa(new_bssid, ethstr)));
1253 
1254 	/* In promiscuous mode, the BSSID field is not a reliable
1255 	 * indicator of the firmware's BSSID. Damp spurious
1256 	 * change-of-BSSID indications.
1257 	 */
1258 	if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1259 	    !ppsratecheck(&sc->sc_last_syn, &sc->sc_false_syns,
1260 	                 WI_MAX_FALSE_SYNS))
1261 		return;
1262 
1263 	sc->sc_false_syns = MAX(0, sc->sc_false_syns - 1);
1264 #if 0
1265 	/*
1266 	 * XXX hack; we should create a new node with the new bssid
1267 	 * and replace the existing ic_bss with it but since we don't
1268 	 * process management frames to collect state we cheat by
1269 	 * reusing the existing node as we know wi_newstate will be
1270 	 * called and it will overwrite the node state.
1271 	 */
1272 	ieee80211_sta_join(ic, ieee80211_ref_node(ni));
1273 #endif
1274 }
1275 
1276 static __noinline void
1277 wi_rx_intr(struct wi_softc *sc)
1278 {
1279 	struct ifnet *ifp = sc->sc_ifp;
1280 	struct ieee80211com *ic = ifp->if_l2com;
1281 	struct wi_frame frmhdr;
1282 	struct mbuf *m;
1283 	struct ieee80211_frame *wh;
1284 	struct ieee80211_node *ni;
1285 	int fid, len, off;
1286 	u_int8_t dir;
1287 	u_int16_t status;
1288 	int8_t rssi, nf;
1289 
1290 	fid = CSR_READ_2(sc, WI_RX_FID);
1291 
1292 	/* First read in the frame header */
1293 	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr))) {
1294 		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1295 		IFNET_STAT_INC(ifp, ierrors, 1);
1296 		DPRINTF(("wi_rx_intr: read fid %x failed\n", fid));
1297 		return;
1298 	}
1299 
1300 	/*
1301 	 * Drop undecryptable or packets with receive errors here
1302 	 */
1303 	status = le16toh(frmhdr.wi_status);
1304 	if (status & WI_STAT_ERRSTAT) {
1305 		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1306 		IFNET_STAT_INC(ifp, ierrors, 1);
1307 		DPRINTF(("wi_rx_intr: fid %x error status %x\n", fid, status));
1308 		return;
1309 	}
1310 
1311 	len = le16toh(frmhdr.wi_dat_len);
1312 	off = ALIGN(sizeof(struct ieee80211_frame));
1313 
1314 	/*
1315 	 * Sometimes the PRISM2.x returns bogusly large frames. Except
1316 	 * in monitor mode, just throw them away.
1317 	 */
1318 	if (off + len > MCLBYTES) {
1319 		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1320 			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1321 			IFNET_STAT_INC(ifp, ierrors, 1);
1322 			DPRINTF(("wi_rx_intr: oversized packet\n"));
1323 			return;
1324 		} else
1325 			len = 0;
1326 	}
1327 
1328 	if (off + len > MHLEN)
1329 		m = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1330 	else
1331 		m = m_gethdr(MB_DONTWAIT, MT_DATA);
1332 	if (m == NULL) {
1333 		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1334 		IFNET_STAT_INC(ifp, ierrors, 1);
1335 		DPRINTF(("wi_rx_intr: MGET failed\n"));
1336 		return;
1337 	}
1338 	m->m_data += off - sizeof(struct ieee80211_frame);
1339 	memcpy(m->m_data, &frmhdr.wi_whdr, sizeof(struct ieee80211_frame));
1340 	wi_read_bap(sc, fid, sizeof(frmhdr),
1341 	    m->m_data + sizeof(struct ieee80211_frame), len);
1342 	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + len;
1343 	m->m_pkthdr.rcvif = ifp;
1344 
1345 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
1346 
1347 	rssi = frmhdr.wi_rx_signal;
1348 	nf = frmhdr.wi_rx_silence;
1349 	if (ieee80211_radiotap_active(ic)) {
1350 		struct wi_rx_radiotap_header *tap = &sc->sc_rx_th;
1351 		uint32_t rstamp;
1352 
1353 		rstamp = (le16toh(frmhdr.wi_rx_tstamp0) << 16) |
1354 		    le16toh(frmhdr.wi_rx_tstamp1);
1355 		tap->wr_tsf = htole64((uint64_t)rstamp);
1356 		/* XXX replace divide by table */
1357 		tap->wr_rate = frmhdr.wi_rx_rate / 5;
1358 		tap->wr_flags = 0;
1359 		if (frmhdr.wi_status & WI_STAT_PCF)
1360 			tap->wr_flags |= IEEE80211_RADIOTAP_F_CFP;
1361 		if (m->m_flags & M_WEP)
1362 			tap->wr_flags |= IEEE80211_RADIOTAP_F_WEP;
1363 		tap->wr_antsignal = rssi;
1364 		tap->wr_antnoise = nf;
1365 	}
1366 
1367 	/* synchronize driver's BSSID with firmware's BSSID */
1368 	wh = mtod(m, struct ieee80211_frame *);
1369 	dir = wh->i_fc[1] & IEEE80211_FC1_DIR_MASK;
1370 	if (ic->ic_opmode == IEEE80211_M_IBSS && dir == IEEE80211_FC1_DIR_NODS)
1371 		wi_sync_bssid(sc, wh->i_addr3);
1372 
1373 	ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1374 	if (ni != NULL) {
1375 		(void) ieee80211_input(ni, m, rssi, nf);
1376 		ieee80211_free_node(ni);
1377 	} else
1378 		(void) ieee80211_input_all(ic, m, rssi, nf);
1379 
1380 }
1381 
1382 static __noinline void
1383 wi_tx_ex_intr(struct wi_softc *sc)
1384 {
1385 	struct ifnet *ifp = sc->sc_ifp;
1386 	struct wi_frame frmhdr;
1387 	int fid;
1388 	char ethstr[ETHER_ADDRSTRLEN + 1];
1389 
1390 	fid = CSR_READ_2(sc, WI_TX_CMP_FID);
1391 	/* Read in the frame header */
1392 	if (wi_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) == 0) {
1393 		u_int16_t status = le16toh(frmhdr.wi_status);
1394 		/*
1395 		 * Spontaneous station disconnects appear as xmit
1396 		 * errors.  Don't announce them and/or count them
1397 		 * as an output error.
1398 		 */
1399 		if ((status & WI_TXSTAT_DISCONNECT) == 0) {
1400 			if (ppsratecheck(&lasttxerror, &curtxeps, wi_txerate)) {
1401 				if_printf(ifp, "tx failed");
1402 				if (status & WI_TXSTAT_RET_ERR)
1403 					kprintf(", retry limit exceeded");
1404 				if (status & WI_TXSTAT_AGED_ERR)
1405 					kprintf(", max transmit lifetime exceeded");
1406 				if (status & WI_TXSTAT_DISCONNECT)
1407 					kprintf(", port disconnected");
1408 				if (status & WI_TXSTAT_FORM_ERR)
1409 					kprintf(", invalid format (data len %u src %s)",
1410 						le16toh(frmhdr.wi_dat_len),
1411 					    kether_ntoa(frmhdr.wi_ehdr.ether_shost, ethstr));
1412 				if (status & ~0xf)
1413 					kprintf(", status=0x%x", status);
1414 				kprintf("\n");
1415 			}
1416 			IFNET_STAT_INC(ifp, oerrors, 1);
1417 		} else {
1418 			DPRINTF(("port disconnected\n"));
1419 			IFNET_STAT_INC(ifp, collisions, 1);	/* XXX */
1420 		}
1421 	} else
1422 		DPRINTF(("wi_tx_ex_intr: read fid %x failed\n", fid));
1423 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
1424 }
1425 
1426 static __noinline void
1427 wi_tx_intr(struct wi_softc *sc)
1428 {
1429 	struct ifnet *ifp = sc->sc_ifp;
1430 	int fid, cur;
1431 
1432 	if (sc->wi_gone)
1433 		return;
1434 
1435 	fid = CSR_READ_2(sc, WI_ALLOC_FID);
1436 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
1437 
1438 	cur = sc->sc_txcur;
1439 	if (sc->sc_txd[cur].d_fid != fid) {
1440 		if_printf(ifp, "bad alloc %x != %x, cur %d nxt %d\n",
1441 		    fid, sc->sc_txd[cur].d_fid, cur, sc->sc_txnext);
1442 		return;
1443 	}
1444 	sc->sc_tx_timer = 0;
1445 	sc->sc_txd[cur].d_len = 0;
1446 	sc->sc_txcur = cur = (cur + 1) % sc->sc_ntxbuf;
1447 	if (sc->sc_txd[cur].d_len == 0)
1448 		ifq_clr_oactive(&ifp->if_snd);
1449 	else {
1450 		if (wi_cmd(sc, WI_CMD_TX | WI_RECLAIM, sc->sc_txd[cur].d_fid,
1451 		    0, 0)) {
1452 			if_printf(ifp, "xmit failed\n");
1453 			sc->sc_txd[cur].d_len = 0;
1454 		} else {
1455 			sc->sc_tx_timer = 5;
1456 		}
1457 	}
1458 }
1459 
1460 static __noinline void
1461 wi_info_intr(struct wi_softc *sc)
1462 {
1463 	struct ifnet *ifp = sc->sc_ifp;
1464 	struct ieee80211com *ic = ifp->if_l2com;
1465 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1466 	int i, fid, len, off;
1467 	u_int16_t ltbuf[2];
1468 	u_int16_t stat;
1469 	u_int32_t *ptr;
1470 
1471 	fid = CSR_READ_2(sc, WI_INFO_FID);
1472 	wi_read_bap(sc, fid, 0, ltbuf, sizeof(ltbuf));
1473 
1474 	switch (le16toh(ltbuf[1])) {
1475 	case WI_INFO_LINK_STAT:
1476 		wi_read_bap(sc, fid, sizeof(ltbuf), &stat, sizeof(stat));
1477 		DPRINTF(("wi_info_intr: LINK_STAT 0x%x\n", le16toh(stat)));
1478 		switch (le16toh(stat)) {
1479 		case WI_INFO_LINK_STAT_CONNECTED:
1480 			if (vap->iv_state == IEEE80211_S_RUN &&
1481 			    vap->iv_opmode != IEEE80211_M_IBSS)
1482 				break;
1483 			/* fall thru... */
1484 		case WI_INFO_LINK_STAT_AP_CHG:
1485 			vap->iv_bss->ni_associd = 1 | 0xc000;	/* NB: anything will do */
1486 			ieee80211_new_state(vap, IEEE80211_S_RUN, 0);
1487 			break;
1488 		case WI_INFO_LINK_STAT_AP_INR:
1489 			break;
1490 		case WI_INFO_LINK_STAT_DISCONNECTED:
1491 			/* we dropped off the net; e.g. due to deauth/disassoc */
1492 			vap->iv_bss->ni_associd = 0;
1493 			vap->iv_stats.is_rx_deauth++;
1494 			ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
1495 			break;
1496 		case WI_INFO_LINK_STAT_AP_OOR:
1497 			/* XXX does this need to be per-vap? */
1498 			ieee80211_beacon_miss(ic);
1499 			break;
1500 		case WI_INFO_LINK_STAT_ASSOC_FAILED:
1501 			if (vap->iv_opmode == IEEE80211_M_STA)
1502 				ieee80211_new_state(vap, IEEE80211_S_SCAN,
1503 				    IEEE80211_SCAN_FAIL_TIMEOUT);
1504 			break;
1505 		}
1506 		break;
1507 	case WI_INFO_COUNTERS:
1508 		/* some card versions have a larger stats structure */
1509 		len = min(le16toh(ltbuf[0]) - 1, sizeof(sc->sc_stats) / 4);
1510 		ptr = (u_int32_t *)&sc->sc_stats;
1511 		off = sizeof(ltbuf);
1512 		for (i = 0; i < len; i++, off += 2, ptr++) {
1513 			wi_read_bap(sc, fid, off, &stat, sizeof(stat));
1514 #ifdef WI_HERMES_STATS_WAR
1515 			if (stat & 0xf000)
1516 				stat = ~stat;
1517 #endif
1518 			*ptr += stat;
1519 		}
1520 		IFNET_STAT_SET(ifp, collisions,
1521 		    sc->sc_stats.wi_tx_single_retries +
1522 		    sc->sc_stats.wi_tx_multi_retries +
1523 		    sc->sc_stats.wi_tx_retry_limit);
1524 		break;
1525 	default:
1526 		DPRINTF(("wi_info_intr: got fid %x type %x len %d\n", fid,
1527 		    le16toh(ltbuf[1]), le16toh(ltbuf[0])));
1528 		break;
1529 	}
1530 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
1531 }
1532 
1533 static int
1534 wi_write_multi(struct wi_softc *sc)
1535 {
1536 	struct ifnet *ifp = sc->sc_ifp;
1537 	int n;
1538 	struct ifmultiaddr *ifma;
1539 	struct wi_mcast mlist;
1540 
1541 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1542 allmulti:
1543 		memset(&mlist, 0, sizeof(mlist));
1544 		return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1545 		    sizeof(mlist));
1546 	}
1547 
1548 	n = 0;
1549 #ifdef __FreeBSD__
1550 	if_maddr_rlock(ifp);
1551 #endif
1552 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1553 		if (ifma->ifma_addr->sa_family != AF_LINK)
1554 			continue;
1555 		if (n >= 16)
1556 			goto allmulti;
1557 		IEEE80211_ADDR_COPY(&mlist.wi_mcast[n],
1558 		    (LLADDR((struct sockaddr_dl *)ifma->ifma_addr)));
1559 		n++;
1560 	}
1561 #ifdef __FreeBSD__
1562 	if_maddr_runlock(ifp);
1563 #endif
1564 	return wi_write_rid(sc, WI_RID_MCAST_LIST, &mlist,
1565 	    IEEE80211_ADDR_LEN * n);
1566 }
1567 
1568 static void
1569 wi_update_mcast(struct ifnet *ifp)
1570 {
1571 	wi_write_multi(ifp->if_softc);
1572 }
1573 
1574 static void
1575 wi_update_promisc(struct ifnet *ifp)
1576 {
1577 	struct wi_softc *sc = ifp->if_softc;
1578 	struct ieee80211com *ic = ifp->if_l2com;
1579 
1580 	/* XXX handle WEP special case handling? */
1581 	wi_write_val(sc, WI_RID_PROMISC,
1582 	    (ic->ic_opmode == IEEE80211_M_MONITOR ||
1583 	     (ifp->if_flags & IFF_PROMISC)));
1584 }
1585 
1586 static void
1587 wi_read_nicid(struct wi_softc *sc)
1588 {
1589 	struct wi_card_ident *id;
1590 	char *p;
1591 	int len;
1592 	u_int16_t ver[4];
1593 
1594 	/* getting chip identity */
1595 	memset(ver, 0, sizeof(ver));
1596 	len = sizeof(ver);
1597 	wi_read_rid(sc, WI_RID_CARD_ID, ver, &len);
1598 
1599 	sc->sc_firmware_type = WI_NOTYPE;
1600 	sc->sc_nic_id = le16toh(ver[0]);
1601 	for (id = wi_card_ident; id->card_name != NULL; id++) {
1602 		if (sc->sc_nic_id == id->card_id) {
1603 			sc->sc_nic_name = id->card_name;
1604 			sc->sc_firmware_type = id->firm_type;
1605 			break;
1606 		}
1607 	}
1608 	if (sc->sc_firmware_type == WI_NOTYPE) {
1609 		if (sc->sc_nic_id & 0x8000) {
1610 			sc->sc_firmware_type = WI_INTERSIL;
1611 			sc->sc_nic_name = "Unknown Prism chip";
1612 		} else {
1613 			sc->sc_firmware_type = WI_LUCENT;
1614 			sc->sc_nic_name = "Unknown Lucent chip";
1615 		}
1616 	}
1617 	if (bootverbose)
1618 		device_printf(sc->sc_dev, "using %s\n", sc->sc_nic_name);
1619 
1620 	/* get primary firmware version (Only Prism chips) */
1621 	if (sc->sc_firmware_type != WI_LUCENT) {
1622 		memset(ver, 0, sizeof(ver));
1623 		len = sizeof(ver);
1624 		wi_read_rid(sc, WI_RID_PRI_IDENTITY, ver, &len);
1625 		sc->sc_pri_firmware_ver = le16toh(ver[2]) * 10000 +
1626 		    le16toh(ver[3]) * 100 + le16toh(ver[1]);
1627 	}
1628 
1629 	/* get station firmware version */
1630 	memset(ver, 0, sizeof(ver));
1631 	len = sizeof(ver);
1632 	wi_read_rid(sc, WI_RID_STA_IDENTITY, ver, &len);
1633 	sc->sc_sta_firmware_ver = le16toh(ver[2]) * 10000 +
1634 	    le16toh(ver[3]) * 100 + le16toh(ver[1]);
1635 	if (sc->sc_firmware_type == WI_INTERSIL &&
1636 	    (sc->sc_sta_firmware_ver == 10102 ||
1637 	     sc->sc_sta_firmware_ver == 20102)) {
1638 		char ident[12];
1639 		memset(ident, 0, sizeof(ident));
1640 		len = sizeof(ident);
1641 		/* value should be the format like "V2.00-11" */
1642 		if (wi_read_rid(sc, WI_RID_SYMBOL_IDENTITY, ident, &len) == 0 &&
1643 		    *(p = (char *)ident) >= 'A' &&
1644 		    p[2] == '.' && p[5] == '-' && p[8] == '\0') {
1645 			sc->sc_firmware_type = WI_SYMBOL;
1646 			sc->sc_sta_firmware_ver = (p[1] - '0') * 10000 +
1647 			    (p[3] - '0') * 1000 + (p[4] - '0') * 100 +
1648 			    (p[6] - '0') * 10 + (p[7] - '0');
1649 		}
1650 	}
1651 	if (bootverbose) {
1652 		device_printf(sc->sc_dev, "%s Firmware: ",
1653 		    wi_firmware_names[sc->sc_firmware_type]);
1654 		if (sc->sc_firmware_type != WI_LUCENT)	/* XXX */
1655 			kprintf("Primary (%u.%u.%u), ",
1656 			    sc->sc_pri_firmware_ver / 10000,
1657 			    (sc->sc_pri_firmware_ver % 10000) / 100,
1658 			    sc->sc_pri_firmware_ver % 100);
1659 		kprintf("Station (%u.%u.%u)\n",
1660 		    sc->sc_sta_firmware_ver / 10000,
1661 		    (sc->sc_sta_firmware_ver % 10000) / 100,
1662 		    sc->sc_sta_firmware_ver % 100);
1663 	}
1664 }
1665 
1666 static int
1667 wi_write_ssid(struct wi_softc *sc, int rid, u_int8_t *buf, int buflen)
1668 {
1669 	struct wi_ssid ssid;
1670 
1671 	if (buflen > IEEE80211_NWID_LEN)
1672 		return ENOBUFS;
1673 	memset(&ssid, 0, sizeof(ssid));
1674 	ssid.wi_len = htole16(buflen);
1675 	memcpy(ssid.wi_ssid, buf, buflen);
1676 	return wi_write_rid(sc, rid, &ssid, sizeof(ssid));
1677 }
1678 
1679 static int
1680 wi_write_txrate(struct wi_softc *sc, struct ieee80211vap *vap)
1681 {
1682 	static const uint16_t lucent_rates[12] = {
1683 	    [ 0] = 3,	/* auto */
1684 	    [ 1] = 1,	/* 1Mb/s */
1685 	    [ 2] = 2,	/* 2Mb/s */
1686 	    [ 5] = 4,	/* 5.5Mb/s */
1687 	    [11] = 5	/* 11Mb/s */
1688 	};
1689 	static const uint16_t intersil_rates[12] = {
1690 	    [ 0] = 0xf,	/* auto */
1691 	    [ 1] = 0,	/* 1Mb/s */
1692 	    [ 2] = 1,	/* 2Mb/s */
1693 	    [ 5] = 2,	/* 5.5Mb/s */
1694 	    [11] = 3,	/* 11Mb/s */
1695 	};
1696 	const uint16_t *rates = sc->sc_firmware_type == WI_LUCENT ?
1697 	    lucent_rates : intersil_rates;
1698 	struct ieee80211com *ic = vap->iv_ic;
1699 	const struct ieee80211_txparam *tp;
1700 
1701 	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
1702 	return wi_write_val(sc, WI_RID_TX_RATE,
1703 	    (tp->ucastrate == IEEE80211_FIXED_RATE_NONE ?
1704 		rates[0] : rates[tp->ucastrate / 2]));
1705 }
1706 
1707 static int
1708 wi_write_wep(struct wi_softc *sc, struct ieee80211vap *vap)
1709 {
1710 	int error = 0;
1711 	int i, keylen;
1712 	u_int16_t val;
1713 	struct wi_key wkey[IEEE80211_WEP_NKID];
1714 
1715 	switch (sc->sc_firmware_type) {
1716 	case WI_LUCENT:
1717 		val = (vap->iv_flags & IEEE80211_F_PRIVACY) ? 1 : 0;
1718 		error = wi_write_val(sc, WI_RID_ENCRYPTION, val);
1719 		if (error)
1720 			break;
1721 		if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0)
1722 			break;
1723 		error = wi_write_val(sc, WI_RID_TX_CRYPT_KEY, vap->iv_def_txkey);
1724 		if (error)
1725 			break;
1726 		memset(wkey, 0, sizeof(wkey));
1727 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1728 			keylen = vap->iv_nw_keys[i].wk_keylen;
1729 			wkey[i].wi_keylen = htole16(keylen);
1730 			memcpy(wkey[i].wi_keydat, vap->iv_nw_keys[i].wk_key,
1731 			    keylen);
1732 		}
1733 		error = wi_write_rid(sc, WI_RID_DEFLT_CRYPT_KEYS,
1734 		    wkey, sizeof(wkey));
1735 		sc->sc_encryption = 0;
1736 		break;
1737 
1738 	case WI_INTERSIL:
1739 		val = HOST_ENCRYPT | HOST_DECRYPT;
1740 		if (vap->iv_flags & IEEE80211_F_PRIVACY) {
1741 			/*
1742 			 * ONLY HWB3163 EVAL-CARD Firmware version
1743 			 * less than 0.8 variant2
1744 			 *
1745 			 *   If promiscuous mode disable, Prism2 chip
1746 			 *  does not work with WEP .
1747 			 * It is under investigation for details.
1748 			 * (ichiro@netbsd.org)
1749 			 */
1750 			if (sc->sc_sta_firmware_ver < 802 ) {
1751 				/* firm ver < 0.8 variant 2 */
1752 				wi_write_val(sc, WI_RID_PROMISC, 1);
1753 			}
1754 			wi_write_val(sc, WI_RID_CNFAUTHMODE,
1755 			    vap->iv_bss->ni_authmode);
1756 			val |= PRIVACY_INVOKED;
1757 		} else {
1758 			wi_write_val(sc, WI_RID_CNFAUTHMODE, IEEE80211_AUTH_OPEN);
1759 		}
1760 		error = wi_write_val(sc, WI_RID_P2_ENCRYPTION, val);
1761 		if (error)
1762 			break;
1763 		sc->sc_encryption = val;
1764 		if ((val & PRIVACY_INVOKED) == 0)
1765 			break;
1766 		error = wi_write_val(sc, WI_RID_P2_TX_CRYPT_KEY, vap->iv_def_txkey);
1767 		break;
1768 	}
1769 	return error;
1770 }
1771 
1772 static int
1773 wi_cmd(struct wi_softc *sc, int cmd, int val0, int val1, int val2)
1774 {
1775 	int i, s = 0;
1776 
1777 	if (sc->wi_gone)
1778 		return (ENODEV);
1779 
1780 	/* wait for the busy bit to clear */
1781 	for (i = sc->wi_cmd_count; i > 0; i--) {	/* 500ms */
1782 		if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
1783 			break;
1784 		DELAY(1*1000);	/* 1ms */
1785 	}
1786 	if (i == 0) {
1787 		device_printf(sc->sc_dev, "%s: busy bit won't clear, cmd 0x%x\n",
1788 		   __func__, cmd);
1789 		sc->wi_gone = 1;
1790 		return(ETIMEDOUT);
1791 	}
1792 
1793 	CSR_WRITE_2(sc, WI_PARAM0, val0);
1794 	CSR_WRITE_2(sc, WI_PARAM1, val1);
1795 	CSR_WRITE_2(sc, WI_PARAM2, val2);
1796 	CSR_WRITE_2(sc, WI_COMMAND, cmd);
1797 
1798 	if (cmd == WI_CMD_INI) {
1799 		/* XXX: should sleep here. */
1800 		DELAY(100*1000);		/* 100ms delay for init */
1801 	}
1802 	for (i = 0; i < WI_TIMEOUT; i++) {
1803 		/*
1804 		 * Wait for 'command complete' bit to be
1805 		 * set in the event status register.
1806 		 */
1807 		s = CSR_READ_2(sc, WI_EVENT_STAT);
1808 		if (s & WI_EV_CMD) {
1809 			/* Ack the event and read result code. */
1810 			s = CSR_READ_2(sc, WI_STATUS);
1811 			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
1812 			if (s & WI_STAT_CMD_RESULT) {
1813 				return(EIO);
1814 			}
1815 			break;
1816 		}
1817 		DELAY(WI_DELAY);
1818 	}
1819 
1820 	if (i == WI_TIMEOUT) {
1821 		device_printf(sc->sc_dev, "%s: timeout on cmd 0x%04x; "
1822 		    "event status 0x%04x\n", __func__, cmd, s);
1823 		if (s == 0xffff)
1824 			sc->wi_gone = 1;
1825 		return(ETIMEDOUT);
1826 	}
1827 	return (0);
1828 }
1829 
1830 static int
1831 wi_seek_bap(struct wi_softc *sc, int id, int off)
1832 {
1833 	int i, status;
1834 
1835 	CSR_WRITE_2(sc, WI_SEL0, id);
1836 	CSR_WRITE_2(sc, WI_OFF0, off);
1837 
1838 	for (i = 0; ; i++) {
1839 		status = CSR_READ_2(sc, WI_OFF0);
1840 		if ((status & WI_OFF_BUSY) == 0)
1841 			break;
1842 		if (i == WI_TIMEOUT) {
1843 			device_printf(sc->sc_dev, "%s: timeout, id %x off %x\n",
1844 			    __func__, id, off);
1845 			sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
1846 			if (status == 0xffff)
1847 				sc->wi_gone = 1;
1848 			return ETIMEDOUT;
1849 		}
1850 		DELAY(1);
1851 	}
1852 	if (status & WI_OFF_ERR) {
1853 		device_printf(sc->sc_dev, "%s: error, id %x off %x\n",
1854 		    __func__, id, off);
1855 		sc->sc_bap_off = WI_OFF_ERR;	/* invalidate */
1856 		return EIO;
1857 	}
1858 	sc->sc_bap_id = id;
1859 	sc->sc_bap_off = off;
1860 	return 0;
1861 }
1862 
1863 static int
1864 wi_read_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
1865 {
1866 	u_int16_t *ptr;
1867 	int i, error, cnt;
1868 
1869 	if (buflen == 0)
1870 		return 0;
1871 	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1872 		if ((error = wi_seek_bap(sc, id, off)) != 0)
1873 			return error;
1874 	}
1875 	cnt = (buflen + 1) / 2;
1876 	ptr = (u_int16_t *)buf;
1877 	for (i = 0; i < cnt; i++)
1878 		*ptr++ = CSR_READ_2(sc, WI_DATA0);
1879 	sc->sc_bap_off += cnt * 2;
1880 	return 0;
1881 }
1882 
1883 static int
1884 wi_write_bap(struct wi_softc *sc, int id, int off, void *buf, int buflen)
1885 {
1886 	u_int16_t *ptr;
1887 	int i, error, cnt;
1888 
1889 	if (buflen == 0)
1890 		return 0;
1891 
1892 	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1893 		if ((error = wi_seek_bap(sc, id, off)) != 0)
1894 			return error;
1895 	}
1896 	cnt = (buflen + 1) / 2;
1897 	ptr = (u_int16_t *)buf;
1898 	for (i = 0; i < cnt; i++)
1899 		CSR_WRITE_2(sc, WI_DATA0, ptr[i]);
1900 	sc->sc_bap_off += cnt * 2;
1901 
1902 	return 0;
1903 }
1904 
1905 static int
1906 wi_mwrite_bap(struct wi_softc *sc, int id, int off, struct mbuf *m0, int totlen)
1907 {
1908 	int error, len;
1909 	struct mbuf *m;
1910 
1911 	for (m = m0; m != NULL && totlen > 0; m = m->m_next) {
1912 		if (m->m_len == 0)
1913 			continue;
1914 
1915 		len = min(m->m_len, totlen);
1916 
1917 		if (((u_long)m->m_data) % 2 != 0 || len % 2 != 0) {
1918 			m_copydata(m, 0, totlen, (caddr_t)&sc->sc_txbuf);
1919 			return wi_write_bap(sc, id, off, (caddr_t)&sc->sc_txbuf,
1920 			    totlen);
1921 		}
1922 
1923 		if ((error = wi_write_bap(sc, id, off, m->m_data, len)) != 0)
1924 			return error;
1925 
1926 		off += m->m_len;
1927 		totlen -= len;
1928 	}
1929 	return 0;
1930 }
1931 
1932 static int
1933 wi_alloc_fid(struct wi_softc *sc, int len, int *idp)
1934 {
1935 	int i;
1936 
1937 	if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len, 0, 0)) {
1938 		device_printf(sc->sc_dev, "%s: failed to allocate %d bytes on NIC\n",
1939 		    __func__, len);
1940 		return ENOMEM;
1941 	}
1942 
1943 	for (i = 0; i < WI_TIMEOUT; i++) {
1944 		if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
1945 			break;
1946 		DELAY(1);
1947 	}
1948 	if (i == WI_TIMEOUT) {
1949 		device_printf(sc->sc_dev, "%s: timeout in alloc\n", __func__);
1950 		return ETIMEDOUT;
1951 	}
1952 	*idp = CSR_READ_2(sc, WI_ALLOC_FID);
1953 	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
1954 	return 0;
1955 }
1956 
1957 static int
1958 wi_read_rid(struct wi_softc *sc, int rid, void *buf, int *buflenp)
1959 {
1960 	int error, len;
1961 	u_int16_t ltbuf[2];
1962 
1963 	/* Tell the NIC to enter record read mode. */
1964 	error = wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_READ, rid, 0, 0);
1965 	if (error)
1966 		return error;
1967 
1968 	error = wi_read_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
1969 	if (error)
1970 		return error;
1971 
1972 	if (le16toh(ltbuf[1]) != rid) {
1973 		device_printf(sc->sc_dev, "record read mismatch, rid=%x, got=%x\n",
1974 		    rid, le16toh(ltbuf[1]));
1975 		return EIO;
1976 	}
1977 	len = (le16toh(ltbuf[0]) - 1) * 2;	 /* already got rid */
1978 	if (*buflenp < len) {
1979 		device_printf(sc->sc_dev, "record buffer is too small, "
1980 		    "rid=%x, size=%d, len=%d\n",
1981 		    rid, *buflenp, len);
1982 		return ENOSPC;
1983 	}
1984 	*buflenp = len;
1985 	return wi_read_bap(sc, rid, sizeof(ltbuf), buf, len);
1986 }
1987 
1988 static int
1989 wi_write_rid(struct wi_softc *sc, int rid, void *buf, int buflen)
1990 {
1991 	int error;
1992 	u_int16_t ltbuf[2];
1993 
1994 	ltbuf[0] = htole16((buflen + 1) / 2 + 1);	 /* includes rid */
1995 	ltbuf[1] = htole16(rid);
1996 
1997 	error = wi_write_bap(sc, rid, 0, ltbuf, sizeof(ltbuf));
1998 	if (error) {
1999 		device_printf(sc->sc_dev, "%s: bap0 write failure, rid 0x%x\n",
2000 		    __func__, rid);
2001 		return error;
2002 	}
2003 	error = wi_write_bap(sc, rid, sizeof(ltbuf), buf, buflen);
2004 	if (error) {
2005 		device_printf(sc->sc_dev, "%s: bap1 write failure, rid 0x%x\n",
2006 		    __func__, rid);
2007 		return error;
2008 	}
2009 
2010 	return wi_cmd(sc, WI_CMD_ACCESS | WI_ACCESS_WRITE, rid, 0, 0);
2011 }
2012 
2013 static int
2014 wi_write_appie(struct wi_softc *sc, int rid, const struct ieee80211_appie *ie)
2015 {
2016 	/* NB: 42 bytes is probably ok to have on the stack */
2017 	char buf[sizeof(uint16_t) + 40];
2018 
2019 	if (ie->ie_len > 40)
2020 		return EINVAL;
2021 	/* NB: firmware requires 16-bit ie length before ie data */
2022 	*(uint16_t *) buf = htole16(ie->ie_len);
2023 	memcpy(buf + sizeof(uint16_t), ie->ie_data, ie->ie_len);
2024 	return wi_write_rid(sc, rid, buf, ie->ie_len + sizeof(uint16_t));
2025 }
2026 
2027 int
2028 wi_alloc(device_t dev, int rid)
2029 {
2030 	struct wi_softc	*sc = device_get_softc(dev);
2031 
2032 	if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
2033 		sc->iobase_rid = rid;
2034 		sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT,
2035 		    &sc->iobase_rid, 0, ~0, (1 << 6),
2036 		    rman_make_alignment_flags(1 << 6) | RF_ACTIVE);
2037 		if (sc->iobase == NULL) {
2038 			device_printf(dev, "No I/O space?!\n");
2039 			return ENXIO;
2040 		}
2041 
2042 		sc->wi_io_addr = rman_get_start(sc->iobase);
2043 		sc->wi_btag = rman_get_bustag(sc->iobase);
2044 		sc->wi_bhandle = rman_get_bushandle(sc->iobase);
2045 	} else {
2046 		sc->mem_rid = rid;
2047 		sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
2048 		    &sc->mem_rid, RF_ACTIVE);
2049 		if (sc->mem == NULL) {
2050 			device_printf(dev, "No Mem space on prism2.5?\n");
2051 			return ENXIO;
2052 		}
2053 
2054 		sc->wi_btag = rman_get_bustag(sc->mem);
2055 		sc->wi_bhandle = rman_get_bushandle(sc->mem);
2056 	}
2057 
2058 	sc->irq_rid = 0;
2059 	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
2060 	    RF_ACTIVE |
2061 	    ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE));
2062 	if (sc->irq == NULL) {
2063 		wi_free(dev);
2064 		device_printf(dev, "No irq?!\n");
2065 		return ENXIO;
2066 	}
2067 
2068 	sc->sc_dev = dev;
2069 	sc->sc_unit = device_get_unit(dev);
2070 	return 0;
2071 }
2072 
2073 void
2074 wi_free(device_t dev)
2075 {
2076 	struct wi_softc	*sc = device_get_softc(dev);
2077 
2078 	if (sc->iobase != NULL) {
2079 		bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase);
2080 		sc->iobase = NULL;
2081 	}
2082 	if (sc->irq != NULL) {
2083 		bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
2084 		sc->irq = NULL;
2085 	}
2086 	if (sc->mem != NULL) {
2087 		bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
2088 		sc->mem = NULL;
2089 	}
2090 }
2091