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