if_wi.c revision 91394
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 for FreeBSD.
35 *
36 * 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 both the PCMCIA and ISA versions of the
58 * WaveLAN/IEEE cards. Note however that the ISA card isn't really
59 * anything of the sort: it's actually a PCMCIA bridge adapter
60 * that fits into an ISA slot, into which a PCMCIA WaveLAN card is
61 * inserted. Consequently, you need to use the pccard support for
62 * both the ISA and PCMCIA adapters.
63 */
64
65#define WI_HERMES_AUTOINC_WAR	/* Work around data write autoinc bug. */
66#define WI_HERMES_STATS_WAR	/* Work around stats counter bug. */
67#define WICACHE			/* turn on signal strength cache code */
68
69#include "pci.h"
70
71#include <sys/param.h>
72#include <sys/systm.h>
73#include <sys/sockio.h>
74#include <sys/mbuf.h>
75#include <sys/proc.h>
76#include <sys/kernel.h>
77#include <sys/socket.h>
78#include <sys/module.h>
79#include <sys/bus.h>
80#include <sys/syslog.h>
81#include <sys/sysctl.h>
82
83#include <machine/bus.h>
84#include <machine/resource.h>
85#include <machine/md_var.h>
86#include <machine/bus_pio.h>
87#include <sys/rman.h>
88
89#if NPCI > 0
90#include <pci/pcireg.h>
91#include <pci/pcivar.h>
92#endif
93
94#include <net/if.h>
95#include <net/if_arp.h>
96#include <net/ethernet.h>
97#include <net/if_dl.h>
98#include <net/if_media.h>
99#include <net/if_types.h>
100#include <net/if_ieee80211.h>
101
102#include <netinet/in.h>
103#include <netinet/in_systm.h>
104#include <netinet/in_var.h>
105#include <netinet/ip.h>
106#include <netinet/if_ether.h>
107
108#include <net/bpf.h>
109
110#include <dev/pccard/pccardvar.h>
111#include <dev/pccard/pccarddevs.h>
112
113#include <dev/wi/if_wavelan_ieee.h>
114#include <dev/wi/if_wireg.h>
115
116#include "card_if.h"
117
118#if !defined(lint)
119static const char rcsid[] =
120  "$FreeBSD: head/sys/dev/wi/if_wi.c 91394 2002-02-27 17:16:18Z tmm $";
121#endif
122
123#ifdef foo
124static u_int8_t	wi_mcast_addr[6] = { 0x01, 0x60, 0x1D, 0x00, 0x01, 0x00 };
125#endif
126
127/*
128 * The following is for compatibility with NetBSD.
129 */
130#define LE16TOH(a)	((a) = le16toh((a)))
131
132static void wi_intr		__P((void *));
133static void wi_reset		__P((struct wi_softc *));
134static int wi_ioctl		__P((struct ifnet *, u_long, caddr_t));
135static void wi_init		__P((void *));
136static void wi_start		__P((struct ifnet *));
137static void wi_stop		__P((struct wi_softc *));
138static void wi_watchdog		__P((struct ifnet *));
139static void wi_rxeof		__P((struct wi_softc *));
140static void wi_txeof		__P((struct wi_softc *, int));
141static void wi_update_stats	__P((struct wi_softc *));
142static void wi_setmulti		__P((struct wi_softc *));
143
144static int wi_cmd		__P((struct wi_softc *, int, int));
145static int wi_read_record	__P((struct wi_softc *, struct wi_ltv_gen *));
146static int wi_write_record	__P((struct wi_softc *, struct wi_ltv_gen *));
147static int wi_read_data		__P((struct wi_softc *, int,
148					int, caddr_t, int));
149static int wi_write_data	__P((struct wi_softc *, int,
150					int, caddr_t, int));
151static int wi_seek		__P((struct wi_softc *, int, int, int));
152static int wi_alloc_nicmem	__P((struct wi_softc *, int, int *));
153static void wi_inquire		__P((void *));
154static void wi_setdef		__P((struct wi_softc *, struct wi_req *));
155static int wi_mgmt_xmit		__P((struct wi_softc *, caddr_t, int));
156
157#ifdef WICACHE
158static
159void wi_cache_store __P((struct wi_softc *, struct ether_header *,
160	struct mbuf *, unsigned short));
161#endif
162
163static int wi_generic_attach	__P((device_t));
164static int wi_pccard_match	__P((device_t));
165static int wi_pccard_probe	__P((device_t));
166static int wi_pccard_attach	__P((device_t));
167#if NPCI > 0
168static int wi_pci_probe		__P((device_t));
169static int wi_pci_attach	__P((device_t));
170#endif
171static int wi_pccard_detach	__P((device_t));
172static void wi_shutdown		__P((device_t));
173
174static int wi_alloc		__P((device_t, int));
175static void wi_free		__P((device_t));
176
177static int wi_get_cur_ssid	__P((struct wi_softc *, char *, int *));
178static void wi_get_id		__P((struct wi_softc *, device_t));
179static int wi_media_change	__P((struct ifnet *));
180static void wi_media_status	__P((struct ifnet *, struct ifmediareq *));
181
182static device_method_t wi_pccard_methods[] = {
183	/* Device interface */
184	DEVMETHOD(device_probe,		pccard_compat_probe),
185	DEVMETHOD(device_attach,	pccard_compat_attach),
186	DEVMETHOD(device_detach,	wi_pccard_detach),
187	DEVMETHOD(device_shutdown,	wi_shutdown),
188
189	/* Card interface */
190	DEVMETHOD(card_compat_match,	wi_pccard_match),
191	DEVMETHOD(card_compat_probe,	wi_pccard_probe),
192	DEVMETHOD(card_compat_attach,	wi_pccard_attach),
193
194	{ 0, 0 }
195};
196
197#if NPCI > 0
198static device_method_t wi_pci_methods[] = {
199	/* Device interface */
200	DEVMETHOD(device_probe,		wi_pci_probe),
201	DEVMETHOD(device_attach,	wi_pci_attach),
202	DEVMETHOD(device_detach,	wi_pccard_detach),
203	DEVMETHOD(device_shutdown,	wi_shutdown),
204
205	{ 0, 0 }
206};
207#endif
208
209static driver_t wi_pccard_driver = {
210	"wi",
211	wi_pccard_methods,
212	sizeof(struct wi_softc)
213};
214
215#if NPCI > 0
216static driver_t wi_pci_driver = {
217	"wi",
218	wi_pci_methods,
219	sizeof(struct wi_softc)
220};
221
222static struct {
223	unsigned int vendor,device;
224	int bus_type;
225	char *desc;
226} pci_ids[] = {
227	{0x1638, 0x1100, WI_BUS_PCI_PLX, "PRISM2STA PCI WaveLAN/IEEE 802.11"},
228	{0x1385, 0x4100, WI_BUS_PCI_PLX, "Netgear MA301 PCI IEEE 802.11b"},
229	{0x16ab, 0x1101, WI_BUS_PCI_PLX, "GLPRISM2 PCI WaveLAN/IEEE 802.11"},
230	{0x16ab, 0x1102, WI_BUS_PCI_PLX, "Linksys WDT11 PCI IEEE 802.11b"},
231	{0x1260, 0x3873, WI_BUS_PCI_NATIVE, "Linksys WMP11 PCI Prism2.5"},
232	{0, 0, 0, NULL}
233};
234#endif
235
236static devclass_t wi_devclass;
237
238DRIVER_MODULE(if_wi, pccard, wi_pccard_driver, wi_devclass, 0, 0);
239#if NPCI > 0
240DRIVER_MODULE(if_wi, pci, wi_pci_driver, wi_devclass, 0, 0);
241#endif
242
243static const struct pccard_product wi_pccard_products[] = {
244	PCMCIA_CARD(3COM, 3CRWE737A, 0),
245	PCMCIA_CARD(BUFFALO, WLI_PCM_S11, 0),
246	PCMCIA_CARD(BUFFALO, WLI_CF_S11G, 0),
247	PCMCIA_CARD(COMPAQ, NC5004, 0),
248	PCMCIA_CARD(CONTEC, FX_DS110_PCC, 0),
249	PCMCIA_CARD(COREGA, WIRELESS_LAN_PCC_11, 0),
250	PCMCIA_CARD(COREGA, WIRELESS_LAN_PCCA_11, 0),
251	PCMCIA_CARD(COREGA, WIRELESS_LAN_PCCB_11, 0),
252	PCMCIA_CARD(ELSA, XI300_IEEE, 0),
253	PCMCIA_CARD(ELSA, XI800_IEEE, 0),
254	PCMCIA_CARD(EMTAC, WLAN, 0),
255	PCMCIA_CARD(GEMTEK, WLAN, 0),
256	PCMCIA_CARD(INTEL, PRO_WLAN_2011, 0),
257	PCMCIA_CARD(INTERSIL, PRISM2, 0),
258	PCMCIA_CARD(IODATA2, WNB11PCM, 0),
259	PCMCIA_CARD2(LUCENT, WAVELAN_IEEE, NANOSPEED_PRISM2, 0),
260	PCMCIA_CARD2(LUCENT, WAVELAN_IEEE, NEC_CMZ_RT_WP, 0),
261	PCMCIA_CARD2(LUCENT, WAVELAN_IEEE, NTT_ME_WLAN, 0),
262	PCMCIA_CARD2(LUCENT, WAVELAN_IEEE, SMC_2632W, 0),
263	/* Must be after other LUCENT ones because it is less specific */
264	PCMCIA_CARD(LUCENT, WAVELAN_IEEE, 0),
265	PCMCIA_CARD(LINKSYS2, IWN, 0),
266	PCMCIA_CARD(SAMSUNG, SWL_2000N, 0),
267	PCMCIA_CARD(TDK, LAK_CD011WL, 0),
268	{ NULL }
269};
270
271static int
272wi_pccard_match(dev)
273	device_t	dev;
274{
275	const struct pccard_product *pp;
276
277	if ((pp = pccard_product_lookup(dev, wi_pccard_products,
278	    sizeof(wi_pccard_products[0]), NULL)) != NULL) {
279		device_set_desc(dev, pp->pp_name);
280		return 0;
281	}
282	return ENXIO;
283}
284
285static int
286wi_pccard_probe(dev)
287	device_t	dev;
288{
289	struct wi_softc	*sc;
290	int		error;
291
292	sc = device_get_softc(dev);
293	sc->wi_gone = 0;
294	sc->wi_bus_type = WI_BUS_PCCARD;
295
296	error = wi_alloc(dev, 0);
297	if (error)
298		return (error);
299
300	wi_free(dev);
301
302	/* Make sure interrupts are disabled. */
303	CSR_WRITE_2(sc, WI_INT_EN, 0);
304	CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
305
306	return (0);
307}
308
309#if NPCI > 0
310static int
311wi_pci_probe(dev)
312	device_t	dev;
313{
314	struct wi_softc		*sc;
315	int i;
316
317	sc = device_get_softc(dev);
318	for(i=0; pci_ids[i].vendor != 0; i++) {
319		if ((pci_get_vendor(dev) == pci_ids[i].vendor) &&
320			(pci_get_device(dev) == pci_ids[i].device)) {
321			sc->wi_prism2 = 1;
322			sc->wi_bus_type = pci_ids[i].bus_type;
323			device_set_desc(dev, pci_ids[i].desc);
324			return (0);
325		}
326	}
327	return(ENXIO);
328}
329#endif
330
331static int
332wi_pccard_detach(dev)
333	device_t		dev;
334{
335	struct wi_softc		*sc;
336	struct ifnet		*ifp;
337
338	sc = device_get_softc(dev);
339	WI_LOCK(sc);
340	ifp = &sc->arpcom.ac_if;
341
342	if (sc->wi_gone) {
343		device_printf(dev, "already unloaded\n");
344		WI_UNLOCK(sc);
345		return(ENODEV);
346	}
347
348	wi_stop(sc);
349
350	/* Delete all remaining media. */
351	ifmedia_removeall(&sc->ifmedia);
352
353	ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
354	bus_teardown_intr(dev, sc->irq, sc->wi_intrhand);
355	wi_free(dev);
356	sc->wi_gone = 1;
357
358	WI_UNLOCK(sc);
359	mtx_destroy(&sc->wi_mtx);
360
361	return(0);
362}
363
364static int
365wi_pccard_attach(device_t dev)
366{
367	struct wi_softc		*sc;
368	int			error;
369
370	sc = device_get_softc(dev);
371
372	error = wi_alloc(dev, 0);
373	if (error) {
374		device_printf(dev, "wi_alloc() failed! (%d)\n", error);
375		return (error);
376	}
377	return (wi_generic_attach(dev));
378}
379
380#if NPCI > 0
381static int
382wi_pci_attach(device_t dev)
383{
384	struct wi_softc		*sc;
385	u_int32_t		command, wanted;
386	u_int16_t		reg;
387	int			error;
388	int			timeout;
389
390	sc = device_get_softc(dev);
391
392	command = pci_read_config(dev, PCIR_COMMAND, 4);
393	wanted = PCIM_CMD_PORTEN|PCIM_CMD_MEMEN;
394	command |= wanted;
395	pci_write_config(dev, PCIR_COMMAND, command, 4);
396	command = pci_read_config(dev, PCIR_COMMAND, 4);
397	if ((command & wanted) != wanted) {
398		device_printf(dev, "wi_pci_attach() failed to enable pci!\n");
399		return (ENXIO);
400	}
401
402	if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
403		error = wi_alloc(dev, WI_PCI_IORES);
404		if (error)
405			return (error);
406
407		/* Make sure interrupts are disabled. */
408		CSR_WRITE_2(sc, WI_INT_EN, 0);
409		CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
410
411		/* We have to do a magic PLX poke to enable interrupts */
412		sc->local_rid = WI_PCI_LOCALRES;
413		sc->local = bus_alloc_resource(dev, SYS_RES_IOPORT,
414		    &sc->local_rid, 0, ~0, 1, RF_ACTIVE);
415		sc->wi_localtag = rman_get_bustag(sc->local);
416		sc->wi_localhandle = rman_get_bushandle(sc->local);
417		command = bus_space_read_4(sc->wi_localtag, sc->wi_localhandle,
418		    WI_LOCAL_INTCSR);
419		command |= WI_LOCAL_INTEN;
420		bus_space_write_4(sc->wi_localtag, sc->wi_localhandle,
421		    WI_LOCAL_INTCSR, command);
422		bus_release_resource(dev, SYS_RES_IOPORT, sc->local_rid,
423		    sc->local);
424		sc->local = NULL;
425
426		sc->mem_rid = WI_PCI_MEMRES;
427		sc->mem = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid,
428					0, ~0, 1, RF_ACTIVE);
429		if (sc->mem == NULL) {
430			device_printf(dev, "couldn't allocate memory\n");
431			wi_free(dev);
432			return (ENXIO);
433		}
434		sc->wi_bmemtag = rman_get_bustag(sc->mem);
435		sc->wi_bmemhandle = rman_get_bushandle(sc->mem);
436
437		/*
438		 * From Linux driver:
439		 * Write COR to enable PC card
440		 * This is a subset of the protocol that the pccard bus code
441		 * would do.
442		 */
443		CSM_WRITE_1(sc, WI_COR_OFFSET, WI_COR_VALUE);
444		reg = CSM_READ_1(sc, WI_COR_OFFSET);
445		if (reg != WI_COR_VALUE) {
446			device_printf(dev, "CSM_READ_1(WI_COR_OFFSET) "
447			    "wanted %d, got %d\n", WI_COR_VALUE, reg);
448			wi_free(dev);
449			return (ENXIO);
450		}
451	} else {
452		error = wi_alloc(dev, WI_PCI_LMEMRES);
453		if (error)
454			return (error);
455
456		CSR_WRITE_2(sc, WI_HFA384X_PCICOR_OFF, 0x0080);
457		DELAY(250000);
458
459		CSR_WRITE_2(sc, WI_HFA384X_PCICOR_OFF, 0x0000);
460		DELAY(500000);
461
462		timeout=2000000;
463		while ((--timeout > 0) &&
464		    (CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
465			DELAY(10);
466
467		if (timeout == 0) {
468			device_printf(dev, "couldn't reset prism2.5 core.\n");
469			wi_free(dev);
470			return(ENXIO);
471		}
472	}
473
474	CSR_WRITE_2(sc, WI_HFA384X_SWSUPPORT0_OFF, WI_PRISM2STA_MAGIC);
475	reg = CSR_READ_2(sc, WI_HFA384X_SWSUPPORT0_OFF);
476	if (reg != WI_PRISM2STA_MAGIC) {
477		device_printf(dev,
478		    "CSR_READ_2(WI_HFA384X_SWSUPPORT0_OFF) "
479		    "wanted %d, got %d\n", WI_PRISM2STA_MAGIC, reg);
480		wi_free(dev);
481		return (ENXIO);
482	}
483
484	error = wi_generic_attach(dev);
485	if (error != 0)
486		return (error);
487
488	return (0);
489}
490#endif
491
492static int
493wi_generic_attach(device_t dev)
494{
495	struct wi_softc		*sc;
496	struct wi_ltv_macaddr	mac;
497	struct wi_ltv_gen	gen;
498	struct ifnet		*ifp;
499	int			error;
500
501	sc = device_get_softc(dev);
502	ifp = &sc->arpcom.ac_if;
503
504	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET,
505	    wi_intr, sc, &sc->wi_intrhand);
506
507	if (error) {
508		device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
509		wi_free(dev);
510		return (error);
511	}
512
513	mtx_init(&sc->wi_mtx, device_get_nameunit(dev), MTX_DEF | MTX_RECURSE);
514	WI_LOCK(sc);
515
516	/* Reset the NIC. */
517	wi_reset(sc);
518
519	/*
520	 * Read the station address.
521	 * And do it twice. I've seen PRISM-based cards that return
522	 * an error when trying to read it the first time, which causes
523	 * the probe to fail.
524	 */
525	mac.wi_type = WI_RID_MAC_NODE;
526	mac.wi_len = 4;
527	wi_read_record(sc, (struct wi_ltv_gen *)&mac);
528	if ((error = wi_read_record(sc, (struct wi_ltv_gen *)&mac)) != 0) {
529		device_printf(dev, "mac read failed %d\n", error);
530		wi_free(dev);
531		return (error);
532	}
533	bcopy((char *)&mac.wi_mac_addr,
534	   (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
535
536	device_printf(dev, "802.11 address: %6D\n", sc->arpcom.ac_enaddr, ":");
537
538	wi_get_id(sc, dev);
539
540	ifp->if_softc = sc;
541	ifp->if_unit = sc->wi_unit;
542	ifp->if_name = "wi";
543	ifp->if_mtu = ETHERMTU;
544	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
545	ifp->if_ioctl = wi_ioctl;
546	ifp->if_output = ether_output;
547	ifp->if_start = wi_start;
548	ifp->if_watchdog = wi_watchdog;
549	ifp->if_init = wi_init;
550	ifp->if_baudrate = 10000000;
551	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
552
553	bzero(sc->wi_node_name, sizeof(sc->wi_node_name));
554	bcopy(WI_DEFAULT_NODENAME, sc->wi_node_name,
555	    sizeof(WI_DEFAULT_NODENAME) - 1);
556
557	bzero(sc->wi_net_name, sizeof(sc->wi_net_name));
558	bcopy(WI_DEFAULT_NETNAME, sc->wi_net_name,
559	    sizeof(WI_DEFAULT_NETNAME) - 1);
560
561	bzero(sc->wi_ibss_name, sizeof(sc->wi_ibss_name));
562	bcopy(WI_DEFAULT_IBSS, sc->wi_ibss_name,
563	    sizeof(WI_DEFAULT_IBSS) - 1);
564
565	sc->wi_portnum = WI_DEFAULT_PORT;
566	sc->wi_ptype = WI_PORTTYPE_BSS;
567	sc->wi_ap_density = WI_DEFAULT_AP_DENSITY;
568	sc->wi_rts_thresh = WI_DEFAULT_RTS_THRESH;
569	sc->wi_tx_rate = WI_DEFAULT_TX_RATE;
570	sc->wi_max_data_len = WI_DEFAULT_DATALEN;
571	sc->wi_create_ibss = WI_DEFAULT_CREATE_IBSS;
572	sc->wi_pm_enabled = WI_DEFAULT_PM_ENABLED;
573	sc->wi_max_sleep = WI_DEFAULT_MAX_SLEEP;
574
575	/*
576	 * Read the default channel from the NIC. This may vary
577	 * depending on the country where the NIC was purchased, so
578	 * we can't hard-code a default and expect it to work for
579	 * everyone.
580	 */
581	gen.wi_type = WI_RID_OWN_CHNL;
582	gen.wi_len = 2;
583	wi_read_record(sc, &gen);
584	sc->wi_channel = gen.wi_val;
585
586	/*
587	 * Find out if we support WEP on this card.
588	 */
589	gen.wi_type = WI_RID_WEP_AVAIL;
590	gen.wi_len = 2;
591	wi_read_record(sc, &gen);
592	sc->wi_has_wep = gen.wi_val;
593
594	if (bootverbose) {
595		device_printf(sc->dev,
596				"%s:wi_has_wep = %d\n",
597				__func__, sc->wi_has_wep);
598	}
599
600	bzero((char *)&sc->wi_stats, sizeof(sc->wi_stats));
601
602	wi_init(sc);
603	wi_stop(sc);
604
605	ifmedia_init(&sc->ifmedia, 0, wi_media_change, wi_media_status);
606	/* XXX: Should read from card capabilities */
607#define ADD(m, c)       ifmedia_add(&sc->ifmedia, (m), (c), NULL)
608	ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS1,
609	    IFM_IEEE80211_ADHOC, 0), 0);
610	ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS1, 0, 0), 0);
611	ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS2,
612	    IFM_IEEE80211_ADHOC, 0), 0);
613	ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS2, 0, 0), 0);
614	ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS5,
615	    IFM_IEEE80211_ADHOC, 0), 0);
616	ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS5, 0, 0), 0);
617	ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS11,
618	    IFM_IEEE80211_ADHOC, 0), 0);
619	ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_DS11, 0, 0), 0);
620	ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_AUTO,
621		IFM_IEEE80211_ADHOC, 0), 0);
622	ADD(IFM_MAKEWORD(IFM_IEEE80211, IFM_AUTO, 0, 0), 0);
623#undef	ADD
624	ifmedia_set(&sc->ifmedia, IFM_MAKEWORD(IFM_IEEE80211, IFM_AUTO,
625	    0, 0));
626
627
628	/*
629	 * Call MI attach routine.
630	 */
631	ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
632	callout_handle_init(&sc->wi_stat_ch);
633	WI_UNLOCK(sc);
634
635	return(0);
636}
637
638static void
639wi_get_id(sc, dev)
640	struct wi_softc *sc;
641	device_t dev;
642{
643	struct wi_ltv_ver       ver;
644
645	/* getting chip identity */
646	memset(&ver, 0, sizeof(ver));
647	ver.wi_type = WI_RID_CARDID;
648	ver.wi_len = 5;
649	wi_read_record(sc, (struct wi_ltv_gen *)&ver);
650	device_printf(dev, "using ");
651	switch (le16toh(ver.wi_ver[0])) {
652	case WI_NIC_EVB2:
653		printf("RF:PRISM2 MAC:HFA3841");
654		sc->wi_prism2 = 1;
655		break;
656	case WI_NIC_HWB3763:
657		printf("RF:PRISM2 MAC:HFA3841 CARD:HWB3763 rev.B");
658		sc->wi_prism2 = 1;
659		break;
660	case WI_NIC_HWB3163:
661		printf("RF:PRISM2 MAC:HFA3841 CARD:HWB3163 rev.A");
662		sc->wi_prism2 = 1;
663		break;
664	case WI_NIC_HWB3163B:
665		printf("RF:PRISM2 MAC:HFA3841 CARD:HWB3163 rev.B");
666		sc->wi_prism2 = 1;
667		break;
668	case WI_NIC_EVB3:
669		printf("RF:PRISM2 MAC:HFA3842");
670		sc->wi_prism2 = 1;
671		break;
672	case WI_NIC_HWB1153:
673		printf("RF:PRISM1 MAC:HFA3841 CARD:HWB1153");
674		sc->wi_prism2 = 1;
675		break;
676	case WI_NIC_P2_SST:
677		printf("RF:PRISM2 MAC:HFA3841 CARD:HWB3163-SST-flash");
678		sc->wi_prism2 = 1;
679		break;
680	case WI_NIC_PRISM2_5:
681		printf("RF:PRISM2.5 MAC:ISL3873");
682		sc->wi_prism2 = 1;
683		break;
684	case WI_NIC_3874A:
685		printf("RF:PRISM2.5 MAC:ISL3874A(PCI)");
686		sc->wi_prism2 = 1;
687		break;
688	default:
689		printf("Lucent chip or unknown chip\n");
690		sc->wi_prism2 = 0;
691		break;
692	}
693
694	if (sc->wi_prism2) {
695		/* try to get prism2 firm version */
696		memset(&ver, 0, sizeof(ver));
697		ver.wi_type = WI_RID_IDENT;
698		ver.wi_len = 5;
699		wi_read_record(sc, (struct wi_ltv_gen *)&ver);
700		LE16TOH(ver.wi_ver[1]);
701		LE16TOH(ver.wi_ver[2]);
702		LE16TOH(ver.wi_ver[3]);
703		printf(", Firmware: %d.%d variant %d\n", ver.wi_ver[2],
704		       ver.wi_ver[3], ver.wi_ver[1]);
705		sc->wi_prism2_ver = ver.wi_ver[2] * 100 +
706				    ver.wi_ver[3] *  10 + ver.wi_ver[1];
707	}
708
709	return;
710}
711
712static void
713wi_rxeof(sc)
714	struct wi_softc		*sc;
715{
716	struct ifnet		*ifp;
717	struct ether_header	*eh;
718	struct wi_frame		rx_frame;
719	struct mbuf		*m;
720	int			id;
721
722	ifp = &sc->arpcom.ac_if;
723
724	id = CSR_READ_2(sc, WI_RX_FID);
725
726	/* First read in the frame header */
727	if (wi_read_data(sc, id, 0, (caddr_t)&rx_frame, sizeof(rx_frame))) {
728		ifp->if_ierrors++;
729		return;
730	}
731
732	if (rx_frame.wi_status & WI_STAT_ERRSTAT) {
733		ifp->if_ierrors++;
734		return;
735	}
736
737	MGETHDR(m, M_DONTWAIT, MT_DATA);
738	if (m == NULL) {
739		ifp->if_ierrors++;
740		return;
741	}
742	MCLGET(m, M_DONTWAIT);
743	if (!(m->m_flags & M_EXT)) {
744		m_freem(m);
745		ifp->if_ierrors++;
746		return;
747	}
748
749	eh = mtod(m, struct ether_header *);
750	m->m_pkthdr.rcvif = ifp;
751
752	if (rx_frame.wi_status == WI_STAT_1042 ||
753	    rx_frame.wi_status == WI_STAT_TUNNEL ||
754	    rx_frame.wi_status == WI_STAT_WMP_MSG) {
755		if((rx_frame.wi_dat_len + WI_SNAPHDR_LEN) > MCLBYTES) {
756			device_printf(sc->dev, "oversized packet received "
757			    "(wi_dat_len=%d, wi_status=0x%x)\n",
758			    rx_frame.wi_dat_len, rx_frame.wi_status);
759			m_freem(m);
760			ifp->if_ierrors++;
761			return;
762		}
763		m->m_pkthdr.len = m->m_len =
764		    rx_frame.wi_dat_len + WI_SNAPHDR_LEN;
765
766#if 0
767		bcopy((char *)&rx_frame.wi_addr1,
768		    (char *)&eh->ether_dhost, ETHER_ADDR_LEN);
769		if (sc->wi_ptype == WI_PORTTYPE_ADHOC) {
770			bcopy((char *)&rx_frame.wi_addr2,
771			    (char *)&eh->ether_shost, ETHER_ADDR_LEN);
772		} else {
773			bcopy((char *)&rx_frame.wi_addr3,
774			    (char *)&eh->ether_shost, ETHER_ADDR_LEN);
775		}
776#else
777		bcopy((char *)&rx_frame.wi_dst_addr,
778			(char *)&eh->ether_dhost, ETHER_ADDR_LEN);
779		bcopy((char *)&rx_frame.wi_src_addr,
780			(char *)&eh->ether_shost, ETHER_ADDR_LEN);
781#endif
782
783		bcopy((char *)&rx_frame.wi_type,
784		    (char *)&eh->ether_type, ETHER_TYPE_LEN);
785
786		if (wi_read_data(sc, id, WI_802_11_OFFSET,
787		    mtod(m, caddr_t) + sizeof(struct ether_header),
788		    m->m_len + 2)) {
789			m_freem(m);
790			ifp->if_ierrors++;
791			return;
792		}
793	} else {
794		if((rx_frame.wi_dat_len +
795		    sizeof(struct ether_header)) > MCLBYTES) {
796			device_printf(sc->dev, "oversized packet received "
797			    "(wi_dat_len=%d, wi_status=0x%x)\n",
798			    rx_frame.wi_dat_len, rx_frame.wi_status);
799			m_freem(m);
800			ifp->if_ierrors++;
801			return;
802		}
803		m->m_pkthdr.len = m->m_len =
804		    rx_frame.wi_dat_len + sizeof(struct ether_header);
805
806		if (wi_read_data(sc, id, WI_802_3_OFFSET,
807		    mtod(m, caddr_t), m->m_len + 2)) {
808			m_freem(m);
809			ifp->if_ierrors++;
810			return;
811		}
812	}
813
814	ifp->if_ipackets++;
815
816	/* Receive packet. */
817	m_adj(m, sizeof(struct ether_header));
818#ifdef WICACHE
819	wi_cache_store(sc, eh, m, rx_frame.wi_q_info);
820#endif
821	ether_input(ifp, eh, m);
822}
823
824static void
825wi_txeof(sc, status)
826	struct wi_softc		*sc;
827	int			status;
828{
829	struct ifnet		*ifp;
830
831	ifp = &sc->arpcom.ac_if;
832
833	ifp->if_timer = 0;
834	ifp->if_flags &= ~IFF_OACTIVE;
835
836	if (status & WI_EV_TX_EXC)
837		ifp->if_oerrors++;
838	else
839		ifp->if_opackets++;
840
841	return;
842}
843
844void
845wi_inquire(xsc)
846	void			*xsc;
847{
848	struct wi_softc		*sc;
849	struct ifnet		*ifp;
850
851	sc = xsc;
852	ifp = &sc->arpcom.ac_if;
853
854	sc->wi_stat_ch = timeout(wi_inquire, sc, hz * 60);
855
856	/* Don't do this while we're transmitting */
857	if (ifp->if_flags & IFF_OACTIVE)
858		return;
859
860	wi_cmd(sc, WI_CMD_INQUIRE, WI_INFO_COUNTERS);
861
862	return;
863}
864
865void
866wi_update_stats(sc)
867	struct wi_softc		*sc;
868{
869	struct wi_ltv_gen	gen;
870	u_int16_t		id;
871	struct ifnet		*ifp;
872	u_int32_t		*ptr;
873	int			len, i;
874	u_int16_t		t;
875
876	ifp = &sc->arpcom.ac_if;
877
878	id = CSR_READ_2(sc, WI_INFO_FID);
879
880	wi_read_data(sc, id, 0, (char *)&gen, 4);
881
882	if (gen.wi_type != WI_INFO_COUNTERS)
883		return;
884
885	len = (gen.wi_len - 1 < sizeof(sc->wi_stats) / 4) ?
886		gen.wi_len - 1 : sizeof(sc->wi_stats) / 4;
887	ptr = (u_int32_t *)&sc->wi_stats;
888
889	for (i = 0; i < len - 1; i++) {
890		t = CSR_READ_2(sc, WI_DATA1);
891#ifdef WI_HERMES_STATS_WAR
892		if (t > 0xF000)
893			t = ~t & 0xFFFF;
894#endif
895		ptr[i] += t;
896	}
897
898	ifp->if_collisions = sc->wi_stats.wi_tx_single_retries +
899	    sc->wi_stats.wi_tx_multi_retries +
900	    sc->wi_stats.wi_tx_retry_limit;
901
902	return;
903}
904
905static void
906wi_intr(xsc)
907	void		*xsc;
908{
909	struct wi_softc		*sc = xsc;
910	struct ifnet		*ifp;
911	u_int16_t		status;
912
913	WI_LOCK(sc);
914
915	ifp = &sc->arpcom.ac_if;
916
917	if (sc->wi_gone || !(ifp->if_flags & IFF_UP)) {
918		CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
919		CSR_WRITE_2(sc, WI_INT_EN, 0);
920		WI_UNLOCK(sc);
921		return;
922	}
923
924	/* Disable interrupts. */
925	CSR_WRITE_2(sc, WI_INT_EN, 0);
926
927	status = CSR_READ_2(sc, WI_EVENT_STAT);
928	CSR_WRITE_2(sc, WI_EVENT_ACK, ~WI_INTRS);
929
930	if (status & WI_EV_RX) {
931		wi_rxeof(sc);
932		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_RX);
933	}
934
935	if (status & WI_EV_TX) {
936		wi_txeof(sc, status);
937		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX);
938	}
939
940	if (status & WI_EV_ALLOC) {
941		int			id;
942
943		id = CSR_READ_2(sc, WI_ALLOC_FID);
944		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
945		if (id == sc->wi_tx_data_id)
946			wi_txeof(sc, status);
947	}
948
949	if (status & WI_EV_INFO) {
950		wi_update_stats(sc);
951		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO);
952	}
953
954	if (status & WI_EV_TX_EXC) {
955		wi_txeof(sc, status);
956		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_TX_EXC);
957	}
958
959	if (status & WI_EV_INFO_DROP) {
960		CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_INFO_DROP);
961	}
962
963	/* Re-enable interrupts. */
964	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
965
966	if (ifp->if_snd.ifq_head != NULL) {
967		wi_start(ifp);
968	}
969
970	WI_UNLOCK(sc);
971
972	return;
973}
974
975static int
976wi_cmd(sc, cmd, val)
977	struct wi_softc		*sc;
978	int			cmd;
979	int			val;
980{
981	int			i, s = 0;
982
983	/* wait for the busy bit to clear */
984	for (i = 500; i > 0; i--) {	/* 5s */
985		if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY)) {
986			break;
987		}
988		DELAY(10*1000);	/* 10 m sec */
989	}
990	if (i == 0) {
991		device_printf(sc->dev, "wi_cmd: busy bit won't clear.\n" );
992		return(ETIMEDOUT);
993	}
994
995	CSR_WRITE_2(sc, WI_PARAM0, val);
996	CSR_WRITE_2(sc, WI_PARAM1, 0);
997	CSR_WRITE_2(sc, WI_PARAM2, 0);
998	CSR_WRITE_2(sc, WI_COMMAND, cmd);
999
1000	for (i = 0; i < WI_TIMEOUT; i++) {
1001		/*
1002		 * Wait for 'command complete' bit to be
1003		 * set in the event status register.
1004		 */
1005		s = CSR_READ_2(sc, WI_EVENT_STAT);
1006		if (s & WI_EV_CMD) {
1007			/* Ack the event and read result code. */
1008			s = CSR_READ_2(sc, WI_STATUS);
1009			CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_CMD);
1010#ifdef foo
1011			if ((s & WI_CMD_CODE_MASK) != (cmd & WI_CMD_CODE_MASK))
1012				return(EIO);
1013#endif
1014			if (s & WI_STAT_CMD_RESULT)
1015				return(EIO);
1016			break;
1017		}
1018		DELAY(WI_DELAY);
1019	}
1020
1021	if (i == WI_TIMEOUT) {
1022		device_printf(sc->dev,
1023		    "timeout in wi_cmd %x; event status %x\n", cmd, s);
1024		return(ETIMEDOUT);
1025	}
1026
1027	return(0);
1028}
1029
1030static void
1031wi_reset(sc)
1032	struct wi_softc		*sc;
1033{
1034#define WI_INIT_TRIES 5
1035	int i;
1036
1037	for (i = 0; i < WI_INIT_TRIES; i++) {
1038		if (wi_cmd(sc, WI_CMD_INI, 0) == 0)
1039			break;
1040		DELAY(WI_DELAY * 1000);
1041	}
1042	if (i == WI_INIT_TRIES)
1043		device_printf(sc->dev, "init failed\n");
1044
1045	CSR_WRITE_2(sc, WI_INT_EN, 0);
1046	CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
1047
1048	/* Calibrate timer. */
1049	WI_SETVAL(WI_RID_TICK_TIME, 8);
1050
1051	return;
1052}
1053
1054/*
1055 * Read an LTV record from the NIC.
1056 */
1057static int
1058wi_read_record(sc, ltv)
1059	struct wi_softc		*sc;
1060	struct wi_ltv_gen	*ltv;
1061{
1062	u_int16_t		*ptr;
1063	int			i, len, code;
1064	struct wi_ltv_gen	*oltv, p2ltv;
1065
1066	oltv = ltv;
1067	if (sc->wi_prism2) {
1068		switch (ltv->wi_type) {
1069		case WI_RID_ENCRYPTION:
1070			p2ltv.wi_type = WI_RID_P2_ENCRYPTION;
1071			p2ltv.wi_len = 2;
1072			ltv = &p2ltv;
1073			break;
1074		case WI_RID_TX_CRYPT_KEY:
1075			p2ltv.wi_type = WI_RID_P2_TX_CRYPT_KEY;
1076			p2ltv.wi_len = 2;
1077			ltv = &p2ltv;
1078			break;
1079		}
1080	}
1081
1082	/* Tell the NIC to enter record read mode. */
1083	if (wi_cmd(sc, WI_CMD_ACCESS|WI_ACCESS_READ, ltv->wi_type))
1084		return(EIO);
1085
1086	/* Seek to the record. */
1087	if (wi_seek(sc, ltv->wi_type, 0, WI_BAP1))
1088		return(EIO);
1089
1090	/*
1091	 * Read the length and record type and make sure they
1092	 * match what we expect (this verifies that we have enough
1093	 * room to hold all of the returned data).
1094	 */
1095	len = CSR_READ_2(sc, WI_DATA1);
1096	if (len > ltv->wi_len)
1097		return(ENOSPC);
1098	code = CSR_READ_2(sc, WI_DATA1);
1099	if (code != ltv->wi_type)
1100		return(EIO);
1101
1102	ltv->wi_len = len;
1103	ltv->wi_type = code;
1104
1105	/* Now read the data. */
1106	ptr = &ltv->wi_val;
1107	for (i = 0; i < ltv->wi_len - 1; i++)
1108		ptr[i] = CSR_READ_2(sc, WI_DATA1);
1109
1110	if (sc->wi_prism2) {
1111		switch (oltv->wi_type) {
1112		case WI_RID_TX_RATE:
1113		case WI_RID_CUR_TX_RATE:
1114			switch (ltv->wi_val) {
1115			case 1: oltv->wi_val = 1; break;
1116			case 2: oltv->wi_val = 2; break;
1117			case 3:	oltv->wi_val = 6; break;
1118			case 4: oltv->wi_val = 5; break;
1119			case 7: oltv->wi_val = 7; break;
1120			case 8: oltv->wi_val = 11; break;
1121			case 15: oltv->wi_val = 3; break;
1122			default: oltv->wi_val = 0x100 + ltv->wi_val; break;
1123			}
1124			break;
1125		case WI_RID_ENCRYPTION:
1126			oltv->wi_len = 2;
1127			if (ltv->wi_val & 0x01)
1128				oltv->wi_val = 1;
1129			else
1130				oltv->wi_val = 0;
1131			break;
1132		case WI_RID_TX_CRYPT_KEY:
1133			oltv->wi_len = 2;
1134			oltv->wi_val = ltv->wi_val;
1135			break;
1136		}
1137	}
1138
1139	return(0);
1140}
1141
1142/*
1143 * Same as read, except we inject data instead of reading it.
1144 */
1145static int
1146wi_write_record(sc, ltv)
1147	struct wi_softc		*sc;
1148	struct wi_ltv_gen	*ltv;
1149{
1150	u_int16_t		*ptr;
1151	int			i;
1152	struct wi_ltv_gen	p2ltv;
1153
1154	if (sc->wi_prism2) {
1155		switch (ltv->wi_type) {
1156		case WI_RID_TX_RATE:
1157			p2ltv.wi_type = WI_RID_TX_RATE;
1158			p2ltv.wi_len = 2;
1159			switch (ltv->wi_val) {
1160			case 1: p2ltv.wi_val = 1; break;
1161			case 2: p2ltv.wi_val = 2; break;
1162			case 3:	p2ltv.wi_val = 15; break;
1163			case 5: p2ltv.wi_val = 4; break;
1164			case 6: p2ltv.wi_val = 3; break;
1165			case 7: p2ltv.wi_val = 7; break;
1166			case 11: p2ltv.wi_val = 8; break;
1167			default: return EINVAL;
1168			}
1169			ltv = &p2ltv;
1170			break;
1171		case WI_RID_ENCRYPTION:
1172			p2ltv.wi_type = WI_RID_P2_ENCRYPTION;
1173			p2ltv.wi_len = 2;
1174			if (ltv->wi_val)
1175				p2ltv.wi_val = 0x03;
1176			else
1177				p2ltv.wi_val = 0x90;
1178			ltv = &p2ltv;
1179			break;
1180		case WI_RID_TX_CRYPT_KEY:
1181			p2ltv.wi_type = WI_RID_P2_TX_CRYPT_KEY;
1182			p2ltv.wi_len = 2;
1183			p2ltv.wi_val = ltv->wi_val;
1184			ltv = &p2ltv;
1185			break;
1186		case WI_RID_DEFLT_CRYPT_KEYS:
1187		    {
1188			int error;
1189			struct wi_ltv_str	ws;
1190			struct wi_ltv_keys	*wk =
1191			    (struct wi_ltv_keys *)ltv;
1192
1193			for (i = 0; i < 4; i++) {
1194				ws.wi_len = 4;
1195				ws.wi_type = WI_RID_P2_CRYPT_KEY0 + i;
1196				memcpy(ws.wi_str,
1197				    &wk->wi_keys[i].wi_keydat, 5);
1198				ws.wi_str[5] = '\0';
1199				error = wi_write_record(sc,
1200				    (struct wi_ltv_gen *)&ws);
1201				if (error)
1202					return error;
1203			}
1204			return 0;
1205		    }
1206		}
1207	}
1208
1209	if (wi_seek(sc, ltv->wi_type, 0, WI_BAP1))
1210		return(EIO);
1211
1212	CSR_WRITE_2(sc, WI_DATA1, ltv->wi_len);
1213	CSR_WRITE_2(sc, WI_DATA1, ltv->wi_type);
1214
1215	ptr = &ltv->wi_val;
1216	for (i = 0; i < ltv->wi_len - 1; i++)
1217		CSR_WRITE_2(sc, WI_DATA1, ptr[i]);
1218
1219	if (wi_cmd(sc, WI_CMD_ACCESS|WI_ACCESS_WRITE, ltv->wi_type))
1220		return(EIO);
1221
1222	return(0);
1223}
1224
1225static int
1226wi_seek(sc, id, off, chan)
1227	struct wi_softc		*sc;
1228	int			id, off, chan;
1229{
1230	int			i;
1231	int			selreg, offreg;
1232	int			status;
1233
1234	switch (chan) {
1235	case WI_BAP0:
1236		selreg = WI_SEL0;
1237		offreg = WI_OFF0;
1238		break;
1239	case WI_BAP1:
1240		selreg = WI_SEL1;
1241		offreg = WI_OFF1;
1242		break;
1243	default:
1244		device_printf(sc->dev, "invalid data path: %x\n", chan);
1245		return(EIO);
1246	}
1247
1248	CSR_WRITE_2(sc, selreg, id);
1249	CSR_WRITE_2(sc, offreg, off);
1250
1251	for (i = 0; i < WI_TIMEOUT; i++) {
1252		status = CSR_READ_2(sc, offreg);
1253		if (!(status & (WI_OFF_BUSY|WI_OFF_ERR)))
1254			break;
1255		DELAY(WI_DELAY);
1256	}
1257
1258	if (i == WI_TIMEOUT) {
1259		device_printf(sc->dev, "timeout in wi_seek to %x/%x; last status %x\n",
1260			id, off, status);
1261		return(ETIMEDOUT);
1262	}
1263
1264	return(0);
1265}
1266
1267static int
1268wi_read_data(sc, id, off, buf, len)
1269	struct wi_softc		*sc;
1270	int			id, off;
1271	caddr_t			buf;
1272	int			len;
1273{
1274	int			i;
1275	u_int16_t		*ptr;
1276
1277	if (wi_seek(sc, id, off, WI_BAP1))
1278		return(EIO);
1279
1280	ptr = (u_int16_t *)buf;
1281	for (i = 0; i < len / 2; i++)
1282		ptr[i] = CSR_READ_2(sc, WI_DATA1);
1283
1284	return(0);
1285}
1286
1287/*
1288 * According to the comments in the HCF Light code, there is a bug in
1289 * the Hermes (or possibly in certain Hermes firmware revisions) where
1290 * the chip's internal autoincrement counter gets thrown off during
1291 * data writes: the autoincrement is missed, causing one data word to
1292 * be overwritten and subsequent words to be written to the wrong memory
1293 * locations. The end result is that we could end up transmitting bogus
1294 * frames without realizing it. The workaround for this is to write a
1295 * couple of extra guard words after the end of the transfer, then
1296 * attempt to read then back. If we fail to locate the guard words where
1297 * we expect them, we preform the transfer over again.
1298 */
1299static int
1300wi_write_data(sc, id, off, buf, len)
1301	struct wi_softc		*sc;
1302	int			id, off;
1303	caddr_t			buf;
1304	int			len;
1305{
1306	int			i;
1307	u_int16_t		*ptr;
1308#ifdef WI_HERMES_AUTOINC_WAR
1309	int			retries;
1310
1311	retries = 512;
1312again:
1313#endif
1314
1315	if (wi_seek(sc, id, off, WI_BAP0))
1316		return(EIO);
1317
1318	ptr = (u_int16_t *)buf;
1319	for (i = 0; i < (len / 2); i++)
1320		CSR_WRITE_2(sc, WI_DATA0, ptr[i]);
1321
1322#ifdef WI_HERMES_AUTOINC_WAR
1323	CSR_WRITE_2(sc, WI_DATA0, 0x1234);
1324	CSR_WRITE_2(sc, WI_DATA0, 0x5678);
1325
1326	if (wi_seek(sc, id, off + len, WI_BAP0))
1327		return(EIO);
1328
1329	if (CSR_READ_2(sc, WI_DATA0) != 0x1234 ||
1330	    CSR_READ_2(sc, WI_DATA0) != 0x5678) {
1331		if (--retries >= 0)
1332			goto again;
1333		device_printf(sc->dev, "wi_write_data device timeout\n");
1334		return (EIO);
1335	}
1336#endif
1337
1338	return(0);
1339}
1340
1341/*
1342 * Allocate a region of memory inside the NIC and zero
1343 * it out.
1344 */
1345static int
1346wi_alloc_nicmem(sc, len, id)
1347	struct wi_softc		*sc;
1348	int			len;
1349	int			*id;
1350{
1351	int			i;
1352
1353	if (wi_cmd(sc, WI_CMD_ALLOC_MEM, len)) {
1354		device_printf(sc->dev,
1355		    "failed to allocate %d bytes on NIC\n", len);
1356		return(ENOMEM);
1357	}
1358
1359	for (i = 0; i < WI_TIMEOUT; i++) {
1360		if (CSR_READ_2(sc, WI_EVENT_STAT) & WI_EV_ALLOC)
1361			break;
1362		DELAY(WI_DELAY);
1363	}
1364
1365	if (i == WI_TIMEOUT) {
1366		device_printf(sc->dev, "time out allocating memory on card\n");
1367		return(ETIMEDOUT);
1368	}
1369
1370	CSR_WRITE_2(sc, WI_EVENT_ACK, WI_EV_ALLOC);
1371	*id = CSR_READ_2(sc, WI_ALLOC_FID);
1372
1373	if (wi_seek(sc, *id, 0, WI_BAP0)) {
1374		device_printf(sc->dev, "seek failed while allocating memory on card\n");
1375		return(EIO);
1376	}
1377
1378	for (i = 0; i < len / 2; i++)
1379		CSR_WRITE_2(sc, WI_DATA0, 0);
1380
1381	return(0);
1382}
1383
1384static void
1385wi_setmulti(sc)
1386	struct wi_softc		*sc;
1387{
1388	struct ifnet		*ifp;
1389	int			i = 0;
1390	struct ifmultiaddr	*ifma;
1391	struct wi_ltv_mcast	mcast;
1392
1393	ifp = &sc->arpcom.ac_if;
1394
1395	bzero((char *)&mcast, sizeof(mcast));
1396
1397	mcast.wi_type = WI_RID_MCAST;
1398	mcast.wi_len = (3 * 16) + 1;
1399
1400	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1401		wi_write_record(sc, (struct wi_ltv_gen *)&mcast);
1402		return;
1403	}
1404
1405	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1406		if (ifma->ifma_addr->sa_family != AF_LINK)
1407			continue;
1408		if (i < 16) {
1409			bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1410			    (char *)&mcast.wi_mcast[i], ETHER_ADDR_LEN);
1411			i++;
1412		} else {
1413			bzero((char *)&mcast, sizeof(mcast));
1414			break;
1415		}
1416	}
1417
1418	mcast.wi_len = (i * 3) + 1;
1419	wi_write_record(sc, (struct wi_ltv_gen *)&mcast);
1420
1421	return;
1422}
1423
1424static void
1425wi_setdef(sc, wreq)
1426	struct wi_softc		*sc;
1427	struct wi_req		*wreq;
1428{
1429	struct sockaddr_dl	*sdl;
1430	struct ifaddr		*ifa;
1431	struct ifnet		*ifp;
1432
1433	ifp = &sc->arpcom.ac_if;
1434
1435	switch(wreq->wi_type) {
1436	case WI_RID_MAC_NODE:
1437		ifa = ifaddr_byindex(ifp->if_index);
1438		sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1439		bcopy((char *)&wreq->wi_val, (char *)&sc->arpcom.ac_enaddr,
1440		   ETHER_ADDR_LEN);
1441		bcopy((char *)&wreq->wi_val, LLADDR(sdl), ETHER_ADDR_LEN);
1442		break;
1443	case WI_RID_PORTTYPE:
1444		sc->wi_ptype = wreq->wi_val[0];
1445		break;
1446	case WI_RID_TX_RATE:
1447		sc->wi_tx_rate = wreq->wi_val[0];
1448		break;
1449	case WI_RID_MAX_DATALEN:
1450		sc->wi_max_data_len = wreq->wi_val[0];
1451		break;
1452	case WI_RID_RTS_THRESH:
1453		sc->wi_rts_thresh = wreq->wi_val[0];
1454		break;
1455	case WI_RID_SYSTEM_SCALE:
1456		sc->wi_ap_density = wreq->wi_val[0];
1457		break;
1458	case WI_RID_CREATE_IBSS:
1459		sc->wi_create_ibss = wreq->wi_val[0];
1460		break;
1461	case WI_RID_OWN_CHNL:
1462		sc->wi_channel = wreq->wi_val[0];
1463		break;
1464	case WI_RID_NODENAME:
1465		bzero(sc->wi_node_name, sizeof(sc->wi_node_name));
1466		bcopy((char *)&wreq->wi_val[1], sc->wi_node_name, 30);
1467		break;
1468	case WI_RID_DESIRED_SSID:
1469		bzero(sc->wi_net_name, sizeof(sc->wi_net_name));
1470		bcopy((char *)&wreq->wi_val[1], sc->wi_net_name, 30);
1471		break;
1472	case WI_RID_OWN_SSID:
1473		bzero(sc->wi_ibss_name, sizeof(sc->wi_ibss_name));
1474		bcopy((char *)&wreq->wi_val[1], sc->wi_ibss_name, 30);
1475		break;
1476	case WI_RID_PM_ENABLED:
1477		sc->wi_pm_enabled = wreq->wi_val[0];
1478		break;
1479	case WI_RID_MAX_SLEEP:
1480		sc->wi_max_sleep = wreq->wi_val[0];
1481		break;
1482	case WI_RID_ENCRYPTION:
1483		sc->wi_use_wep = wreq->wi_val[0];
1484		break;
1485	case WI_RID_TX_CRYPT_KEY:
1486		sc->wi_tx_key = wreq->wi_val[0];
1487		break;
1488	case WI_RID_DEFLT_CRYPT_KEYS:
1489		bcopy((char *)wreq, (char *)&sc->wi_keys,
1490		    sizeof(struct wi_ltv_keys));
1491		break;
1492	default:
1493		break;
1494	}
1495
1496	/* Reinitialize WaveLAN. */
1497	wi_init(sc);
1498
1499	return;
1500}
1501
1502static int
1503wi_ioctl(ifp, command, data)
1504	struct ifnet		*ifp;
1505	u_long			command;
1506	caddr_t			data;
1507{
1508	int			error = 0;
1509	int			len;
1510	u_int8_t		tmpkey[14];
1511	char			tmpssid[IEEE80211_NWID_LEN];
1512	struct wi_softc		*sc;
1513	struct wi_req		wreq;
1514	struct ifreq		*ifr;
1515	struct ieee80211req	*ireq;
1516	struct proc		*p = curproc;
1517
1518	sc = ifp->if_softc;
1519	WI_LOCK(sc);
1520	ifr = (struct ifreq *)data;
1521	ireq = (struct ieee80211req *)data;
1522
1523	if (sc->wi_gone) {
1524		error = ENODEV;
1525		goto out;
1526	}
1527
1528	switch(command) {
1529	case SIOCSIFADDR:
1530	case SIOCGIFADDR:
1531	case SIOCSIFMTU:
1532		error = ether_ioctl(ifp, command, data);
1533		break;
1534	case SIOCSIFFLAGS:
1535		if (ifp->if_flags & IFF_UP) {
1536			if (ifp->if_flags & IFF_RUNNING &&
1537			    ifp->if_flags & IFF_PROMISC &&
1538			    !(sc->wi_if_flags & IFF_PROMISC)) {
1539				WI_SETVAL(WI_RID_PROMISC, 1);
1540			} else if (ifp->if_flags & IFF_RUNNING &&
1541			    !(ifp->if_flags & IFF_PROMISC) &&
1542			    sc->wi_if_flags & IFF_PROMISC) {
1543				WI_SETVAL(WI_RID_PROMISC, 0);
1544			} else
1545				wi_init(sc);
1546		} else {
1547			if (ifp->if_flags & IFF_RUNNING) {
1548				wi_stop(sc);
1549			}
1550		}
1551		sc->wi_if_flags = ifp->if_flags;
1552		error = 0;
1553		break;
1554	case SIOCSIFMEDIA:
1555	case SIOCGIFMEDIA:
1556		error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
1557		break;
1558	case SIOCADDMULTI:
1559	case SIOCDELMULTI:
1560		wi_setmulti(sc);
1561		error = 0;
1562		break;
1563	case SIOCGWAVELAN:
1564		error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1565		if (error)
1566			break;
1567		/* Don't show WEP keys to non-root users. */
1568		if (wreq.wi_type == WI_RID_DEFLT_CRYPT_KEYS && suser(p))
1569			break;
1570		if (wreq.wi_type == WI_RID_IFACE_STATS) {
1571			bcopy((char *)&sc->wi_stats, (char *)&wreq.wi_val,
1572			    sizeof(sc->wi_stats));
1573			wreq.wi_len = (sizeof(sc->wi_stats) / 2) + 1;
1574		} else if (wreq.wi_type == WI_RID_DEFLT_CRYPT_KEYS) {
1575			bcopy((char *)&sc->wi_keys, (char *)&wreq,
1576			    sizeof(struct wi_ltv_keys));
1577		}
1578#ifdef WICACHE
1579		else if (wreq.wi_type == WI_RID_ZERO_CACHE) {
1580			sc->wi_sigitems = sc->wi_nextitem = 0;
1581		} else if (wreq.wi_type == WI_RID_READ_CACHE) {
1582			char *pt = (char *)&wreq.wi_val;
1583			bcopy((char *)&sc->wi_sigitems,
1584			    (char *)pt, sizeof(int));
1585			pt += (sizeof (int));
1586			wreq.wi_len = sizeof(int) / 2;
1587			bcopy((char *)&sc->wi_sigcache, (char *)pt,
1588			    sizeof(struct wi_sigcache) * sc->wi_sigitems);
1589			wreq.wi_len += ((sizeof(struct wi_sigcache) *
1590			    sc->wi_sigitems) / 2) + 1;
1591		}
1592#endif
1593		else {
1594			if (wi_read_record(sc, (struct wi_ltv_gen *)&wreq)) {
1595				error = EINVAL;
1596				break;
1597			}
1598		}
1599		error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
1600		break;
1601	case SIOCSWAVELAN:
1602		if ((error = suser(p)))
1603			goto out;
1604		error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1605		if (error)
1606			break;
1607		if (wreq.wi_type == WI_RID_IFACE_STATS) {
1608			error = EINVAL;
1609			break;
1610		} else if (wreq.wi_type == WI_RID_MGMT_XMIT) {
1611			error = wi_mgmt_xmit(sc, (caddr_t)&wreq.wi_val,
1612			    wreq.wi_len);
1613		} else {
1614			error = wi_write_record(sc, (struct wi_ltv_gen *)&wreq);
1615			if (!error)
1616				wi_setdef(sc, &wreq);
1617		}
1618		break;
1619	case SIOCG80211:
1620		switch(ireq->i_type) {
1621		case IEEE80211_IOC_SSID:
1622			if(ireq->i_val == -1) {
1623				bzero(tmpssid, IEEE80211_NWID_LEN);
1624				error = wi_get_cur_ssid(sc, tmpssid, &len);
1625				if (error != 0)
1626					break;
1627				error = copyout(tmpssid, ireq->i_data,
1628					IEEE80211_NWID_LEN);
1629				ireq->i_len = len;
1630			} else if (ireq->i_val == 0) {
1631				error = copyout(sc->wi_net_name,
1632				    ireq->i_data,
1633				    IEEE80211_NWID_LEN);
1634				ireq->i_len = IEEE80211_NWID_LEN;
1635			} else
1636				error = EINVAL;
1637			break;
1638		case IEEE80211_IOC_NUMSSIDS:
1639			ireq->i_val = 1;
1640			break;
1641		case IEEE80211_IOC_WEP:
1642			if(!sc->wi_has_wep) {
1643				ireq->i_val = IEEE80211_WEP_NOSUP;
1644			} else {
1645				if(sc->wi_use_wep) {
1646					ireq->i_val =
1647					    IEEE80211_WEP_MIXED;
1648				} else {
1649					ireq->i_val =
1650					    IEEE80211_WEP_OFF;
1651				}
1652			}
1653			break;
1654		case IEEE80211_IOC_WEPKEY:
1655			if(!sc->wi_has_wep ||
1656			    ireq->i_val < 0 || ireq->i_val > 3) {
1657				error = EINVAL;
1658				break;
1659			}
1660			len = sc->wi_keys.wi_keys[ireq->i_val].wi_keylen;
1661			if (suser(p))
1662				bcopy(sc->wi_keys.wi_keys[ireq->i_val].wi_keydat,
1663				    tmpkey, len);
1664			else
1665				bzero(tmpkey, len);
1666
1667			ireq->i_len = len;
1668			error = copyout(tmpkey, ireq->i_data, len);
1669
1670			break;
1671		case IEEE80211_IOC_NUMWEPKEYS:
1672			if(!sc->wi_has_wep)
1673				error = EINVAL;
1674			else
1675				ireq->i_val = 4;
1676			break;
1677		case IEEE80211_IOC_WEPTXKEY:
1678			if(!sc->wi_has_wep)
1679				error = EINVAL;
1680			else
1681				ireq->i_val = sc->wi_tx_key;
1682			break;
1683		case IEEE80211_IOC_AUTHMODE:
1684			ireq->i_val = IEEE80211_AUTH_NONE;
1685			break;
1686		case IEEE80211_IOC_STATIONNAME:
1687			error = copyout(sc->wi_node_name,
1688			    ireq->i_data, IEEE80211_NWID_LEN);
1689			ireq->i_len = IEEE80211_NWID_LEN;
1690			break;
1691		case IEEE80211_IOC_CHANNEL:
1692			wreq.wi_type = WI_RID_CURRENT_CHAN;
1693			wreq.wi_len = WI_MAX_DATALEN;
1694			if (wi_read_record(sc, (struct wi_ltv_gen *)&wreq))
1695				error = EINVAL;
1696			else {
1697				ireq->i_val = wreq.wi_val[0];
1698			}
1699			break;
1700		case IEEE80211_IOC_POWERSAVE:
1701			if(sc->wi_pm_enabled)
1702				ireq->i_val = IEEE80211_POWERSAVE_ON;
1703			else
1704				ireq->i_val = IEEE80211_POWERSAVE_OFF;
1705			break;
1706		case IEEE80211_IOC_POWERSAVESLEEP:
1707			ireq->i_val = sc->wi_max_sleep;
1708			break;
1709		default:
1710			error = EINVAL;
1711		}
1712		break;
1713	case SIOCS80211:
1714		if ((error = suser(p)))
1715			goto out;
1716		switch(ireq->i_type) {
1717		case IEEE80211_IOC_SSID:
1718			if (ireq->i_val != 0 ||
1719			    ireq->i_len > IEEE80211_NWID_LEN) {
1720				error = EINVAL;
1721				break;
1722			}
1723			/* We set both of them */
1724			bzero(sc->wi_net_name, IEEE80211_NWID_LEN);
1725			error = copyin(ireq->i_data,
1726			    sc->wi_net_name, ireq->i_len);
1727			bcopy(sc->wi_net_name, sc->wi_ibss_name, IEEE80211_NWID_LEN);
1728			break;
1729		case IEEE80211_IOC_WEP:
1730			/*
1731			 * These cards only support one mode so
1732			 * we just turn wep on what ever is
1733			 * passed in if it's not OFF.
1734			 */
1735			if (ireq->i_val == IEEE80211_WEP_OFF) {
1736				sc->wi_use_wep = 0;
1737			} else {
1738				sc->wi_use_wep = 1;
1739			}
1740			break;
1741		case IEEE80211_IOC_WEPKEY:
1742			if (ireq->i_val < 0 || ireq->i_val > 3 ||
1743				ireq->i_len > 13) {
1744				error = EINVAL;
1745				break;
1746			}
1747			bzero(sc->wi_keys.wi_keys[ireq->i_val].wi_keydat, 13);
1748			error = copyin(ireq->i_data,
1749			    sc->wi_keys.wi_keys[ireq->i_val].wi_keydat,
1750			    ireq->i_len);
1751			if(error)
1752				break;
1753			sc->wi_keys.wi_keys[ireq->i_val].wi_keylen =
1754				    ireq->i_len;
1755			break;
1756		case IEEE80211_IOC_WEPTXKEY:
1757			if (ireq->i_val < 0 || ireq->i_val > 3) {
1758				error = EINVAL;
1759				break;
1760			}
1761			sc->wi_tx_key = ireq->i_val;
1762			break;
1763		case IEEE80211_IOC_AUTHMODE:
1764			error = EINVAL;
1765			break;
1766		case IEEE80211_IOC_STATIONNAME:
1767			if (ireq->i_len > 32) {
1768				error = EINVAL;
1769				break;
1770			}
1771			bzero(sc->wi_node_name, 32);
1772			error = copyin(ireq->i_data,
1773			    sc->wi_node_name, ireq->i_len);
1774			break;
1775		case IEEE80211_IOC_CHANNEL:
1776			/*
1777			 * The actual range is 1-14, but if you
1778			 * set it to 0 you get the default. So
1779			 * we let that work too.
1780			 */
1781			if (ireq->i_val < 0 || ireq->i_val > 14) {
1782				error = EINVAL;
1783				break;
1784			}
1785			sc->wi_channel = ireq->i_val;
1786			break;
1787		case IEEE80211_IOC_POWERSAVE:
1788			switch (ireq->i_val) {
1789			case IEEE80211_POWERSAVE_OFF:
1790				sc->wi_pm_enabled = 0;
1791				break;
1792			case IEEE80211_POWERSAVE_ON:
1793				sc->wi_pm_enabled = 1;
1794				break;
1795			default:
1796				error = EINVAL;
1797				break;
1798			}
1799			break;
1800		case IEEE80211_IOC_POWERSAVESLEEP:
1801			if (ireq->i_val < 0) {
1802				error = EINVAL;
1803				break;
1804			}
1805			sc->wi_max_sleep = ireq->i_val;
1806			break;
1807		default:
1808			error = EINVAL;
1809			break;
1810		}
1811
1812		/* Reinitialize WaveLAN. */
1813		wi_init(sc);
1814
1815		break;
1816	default:
1817		error = EINVAL;
1818		break;
1819	}
1820out:
1821	WI_UNLOCK(sc);
1822
1823	return(error);
1824}
1825
1826static void
1827wi_init(xsc)
1828	void			*xsc;
1829{
1830	struct wi_softc		*sc = xsc;
1831	struct ifnet		*ifp = &sc->arpcom.ac_if;
1832	struct wi_ltv_macaddr	mac;
1833	int			id = 0;
1834
1835	WI_LOCK(sc);
1836
1837	if (sc->wi_gone) {
1838		WI_UNLOCK(sc);
1839		return;
1840	}
1841
1842	if (ifp->if_flags & IFF_RUNNING)
1843		wi_stop(sc);
1844
1845	wi_reset(sc);
1846
1847	/* Program max data length. */
1848	WI_SETVAL(WI_RID_MAX_DATALEN, sc->wi_max_data_len);
1849
1850	/* Enable/disable IBSS creation. */
1851	WI_SETVAL(WI_RID_CREATE_IBSS, sc->wi_create_ibss);
1852
1853	/* Set the port type. */
1854	WI_SETVAL(WI_RID_PORTTYPE, sc->wi_ptype);
1855
1856	/* Program the RTS/CTS threshold. */
1857	WI_SETVAL(WI_RID_RTS_THRESH, sc->wi_rts_thresh);
1858
1859	/* Program the TX rate */
1860	WI_SETVAL(WI_RID_TX_RATE, sc->wi_tx_rate);
1861
1862	/* Access point density */
1863	WI_SETVAL(WI_RID_SYSTEM_SCALE, sc->wi_ap_density);
1864
1865	/* Power Management Enabled */
1866	WI_SETVAL(WI_RID_PM_ENABLED, sc->wi_pm_enabled);
1867
1868	/* Power Managment Max Sleep */
1869	WI_SETVAL(WI_RID_MAX_SLEEP, sc->wi_max_sleep);
1870
1871	/* Specify the IBSS name */
1872	WI_SETSTR(WI_RID_OWN_SSID, sc->wi_ibss_name);
1873
1874	/* Specify the network name */
1875	WI_SETSTR(WI_RID_DESIRED_SSID, sc->wi_net_name);
1876
1877	/* Specify the frequency to use */
1878	WI_SETVAL(WI_RID_OWN_CHNL, sc->wi_channel);
1879
1880	/* Program the nodename. */
1881	WI_SETSTR(WI_RID_NODENAME, sc->wi_node_name);
1882
1883	/* Set our MAC address. */
1884	mac.wi_len = 4;
1885	mac.wi_type = WI_RID_MAC_NODE;
1886	bcopy((char *)&sc->arpcom.ac_enaddr,
1887	   (char *)&mac.wi_mac_addr, ETHER_ADDR_LEN);
1888	wi_write_record(sc, (struct wi_ltv_gen *)&mac);
1889
1890	/* Configure WEP. */
1891	if (sc->wi_has_wep) {
1892		WI_SETVAL(WI_RID_ENCRYPTION, sc->wi_use_wep);
1893		WI_SETVAL(WI_RID_TX_CRYPT_KEY, sc->wi_tx_key);
1894		sc->wi_keys.wi_len = (sizeof(struct wi_ltv_keys) / 2) + 1;
1895		sc->wi_keys.wi_type = WI_RID_DEFLT_CRYPT_KEYS;
1896		wi_write_record(sc, (struct wi_ltv_gen *)&sc->wi_keys);
1897	}
1898
1899	/* Initialize promisc mode. */
1900	if (ifp->if_flags & IFF_PROMISC) {
1901		WI_SETVAL(WI_RID_PROMISC, 1);
1902	} else {
1903		WI_SETVAL(WI_RID_PROMISC, 0);
1904	}
1905
1906	/* Set multicast filter. */
1907	wi_setmulti(sc);
1908
1909	/* Enable desired port */
1910	wi_cmd(sc, WI_CMD_ENABLE|sc->wi_portnum, 0);
1911
1912	if (wi_alloc_nicmem(sc, ETHER_MAX_LEN + sizeof(struct wi_frame) + 8, &id))
1913		device_printf(sc->dev, "tx buffer allocation failed\n");
1914	sc->wi_tx_data_id = id;
1915
1916	if (wi_alloc_nicmem(sc, ETHER_MAX_LEN + sizeof(struct wi_frame) + 8, &id))
1917		device_printf(sc->dev, "mgmt. buffer allocation failed\n");
1918	sc->wi_tx_mgmt_id = id;
1919
1920	/* enable interrupts */
1921	CSR_WRITE_2(sc, WI_INT_EN, WI_INTRS);
1922
1923	ifp->if_flags |= IFF_RUNNING;
1924	ifp->if_flags &= ~IFF_OACTIVE;
1925
1926	sc->wi_stat_ch = timeout(wi_inquire, sc, hz * 60);
1927	WI_UNLOCK(sc);
1928
1929	return;
1930}
1931
1932static void
1933wi_start(ifp)
1934	struct ifnet		*ifp;
1935{
1936	struct wi_softc		*sc;
1937	struct mbuf		*m0;
1938	struct wi_frame		tx_frame;
1939	struct ether_header	*eh;
1940	int			id;
1941
1942	sc = ifp->if_softc;
1943	WI_LOCK(sc);
1944
1945	if (sc->wi_gone) {
1946		WI_UNLOCK(sc);
1947		return;
1948	}
1949
1950	if (ifp->if_flags & IFF_OACTIVE) {
1951		WI_UNLOCK(sc);
1952		return;
1953	}
1954
1955	IF_DEQUEUE(&ifp->if_snd, m0);
1956	if (m0 == NULL) {
1957		WI_UNLOCK(sc);
1958		return;
1959	}
1960
1961	bzero((char *)&tx_frame, sizeof(tx_frame));
1962	id = sc->wi_tx_data_id;
1963	eh = mtod(m0, struct ether_header *);
1964
1965	/*
1966	 * Use RFC1042 encoding for IP and ARP datagrams,
1967	 * 802.3 for anything else.
1968	 */
1969	if (ntohs(eh->ether_type) > ETHER_MAX_LEN) {
1970		bcopy((char *)&eh->ether_dhost,
1971		    (char *)&tx_frame.wi_addr1, ETHER_ADDR_LEN);
1972		bcopy((char *)&eh->ether_shost,
1973		    (char *)&tx_frame.wi_addr2, ETHER_ADDR_LEN);
1974		bcopy((char *)&eh->ether_dhost,
1975		    (char *)&tx_frame.wi_dst_addr, ETHER_ADDR_LEN);
1976		bcopy((char *)&eh->ether_shost,
1977		    (char *)&tx_frame.wi_src_addr, ETHER_ADDR_LEN);
1978
1979		tx_frame.wi_dat_len = m0->m_pkthdr.len - WI_SNAPHDR_LEN;
1980		tx_frame.wi_frame_ctl = WI_FTYPE_DATA;
1981		tx_frame.wi_dat[0] = htons(WI_SNAP_WORD0);
1982		tx_frame.wi_dat[1] = htons(WI_SNAP_WORD1);
1983		tx_frame.wi_len = htons(m0->m_pkthdr.len - WI_SNAPHDR_LEN);
1984		tx_frame.wi_type = eh->ether_type;
1985
1986		m_copydata(m0, sizeof(struct ether_header),
1987		    m0->m_pkthdr.len - sizeof(struct ether_header),
1988		    (caddr_t)&sc->wi_txbuf);
1989
1990		wi_write_data(sc, id, 0, (caddr_t)&tx_frame,
1991		    sizeof(struct wi_frame));
1992		wi_write_data(sc, id, WI_802_11_OFFSET, (caddr_t)&sc->wi_txbuf,
1993		    (m0->m_pkthdr.len - sizeof(struct ether_header)) + 2);
1994	} else {
1995		tx_frame.wi_dat_len = m0->m_pkthdr.len;
1996
1997		eh->ether_type = htons(m0->m_pkthdr.len - WI_SNAPHDR_LEN);
1998		m_copydata(m0, 0, m0->m_pkthdr.len, (caddr_t)&sc->wi_txbuf);
1999
2000		wi_write_data(sc, id, 0, (caddr_t)&tx_frame,
2001		    sizeof(struct wi_frame));
2002		wi_write_data(sc, id, WI_802_3_OFFSET, (caddr_t)&sc->wi_txbuf,
2003		    m0->m_pkthdr.len + 2);
2004	}
2005
2006	/*
2007	 * If there's a BPF listner, bounce a copy of
2008	 * this frame to him.
2009	 */
2010	if (ifp->if_bpf)
2011		bpf_mtap(ifp, m0);
2012
2013	m_freem(m0);
2014
2015	if (wi_cmd(sc, WI_CMD_TX|WI_RECLAIM, id))
2016		device_printf(sc->dev, "xmit failed\n");
2017
2018	ifp->if_flags |= IFF_OACTIVE;
2019
2020	/*
2021	 * Set a timeout in case the chip goes out to lunch.
2022	 */
2023	ifp->if_timer = 5;
2024
2025	WI_UNLOCK(sc);
2026	return;
2027}
2028
2029static int
2030wi_mgmt_xmit(sc, data, len)
2031	struct wi_softc		*sc;
2032	caddr_t			data;
2033	int			len;
2034{
2035	struct wi_frame		tx_frame;
2036	int			id;
2037	struct wi_80211_hdr	*hdr;
2038	caddr_t			dptr;
2039
2040	if (sc->wi_gone)
2041		return(ENODEV);
2042
2043	hdr = (struct wi_80211_hdr *)data;
2044	dptr = data + sizeof(struct wi_80211_hdr);
2045
2046	bzero((char *)&tx_frame, sizeof(tx_frame));
2047	id = sc->wi_tx_mgmt_id;
2048
2049	bcopy((char *)hdr, (char *)&tx_frame.wi_frame_ctl,
2050	   sizeof(struct wi_80211_hdr));
2051
2052	tx_frame.wi_dat_len = len - WI_SNAPHDR_LEN;
2053	tx_frame.wi_len = htons(len - WI_SNAPHDR_LEN);
2054
2055	wi_write_data(sc, id, 0, (caddr_t)&tx_frame, sizeof(struct wi_frame));
2056	wi_write_data(sc, id, WI_802_11_OFFSET_RAW, dptr,
2057	    (len - sizeof(struct wi_80211_hdr)) + 2);
2058
2059	if (wi_cmd(sc, WI_CMD_TX|WI_RECLAIM, id)) {
2060		device_printf(sc->dev, "xmit failed\n");
2061		return(EIO);
2062	}
2063
2064	return(0);
2065}
2066
2067static void
2068wi_stop(sc)
2069	struct wi_softc		*sc;
2070{
2071	struct ifnet		*ifp;
2072
2073	WI_LOCK(sc);
2074
2075	if (sc->wi_gone) {
2076		WI_UNLOCK(sc);
2077		return;
2078	}
2079
2080	ifp = &sc->arpcom.ac_if;
2081
2082	/*
2083	 * If the card is gone and the memory port isn't mapped, we will
2084	 * (hopefully) get 0xffff back from the status read, which is not
2085	 * a valid status value.
2086	 */
2087	if (CSR_READ_2(sc, WI_STATUS) != 0xffff) {
2088		CSR_WRITE_2(sc, WI_INT_EN, 0);
2089		wi_cmd(sc, WI_CMD_DISABLE|sc->wi_portnum, 0);
2090	}
2091
2092	untimeout(wi_inquire, sc, sc->wi_stat_ch);
2093
2094	ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
2095
2096	WI_UNLOCK(sc);
2097	return;
2098}
2099
2100static void
2101wi_watchdog(ifp)
2102	struct ifnet		*ifp;
2103{
2104	struct wi_softc		*sc;
2105
2106	sc = ifp->if_softc;
2107
2108	device_printf(sc->dev, "watchdog timeout\n");
2109
2110	wi_init(sc);
2111
2112	ifp->if_oerrors++;
2113
2114	return;
2115}
2116
2117static int
2118wi_alloc(dev, rid)
2119	device_t		dev;
2120	int			rid;
2121{
2122	struct wi_softc		*sc = device_get_softc(dev);
2123
2124	if (sc->wi_bus_type != WI_BUS_PCI_NATIVE) {
2125		sc->iobase_rid = rid;
2126		sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT,
2127		    &sc->iobase_rid, 0, ~0, (1 << 6),
2128		    rman_make_alignment_flags(1 << 6) | RF_ACTIVE);
2129		if (!sc->iobase) {
2130			device_printf(dev, "No I/O space?!\n");
2131			return (ENXIO);
2132		}
2133
2134		sc->wi_io_addr = rman_get_start(sc->iobase);
2135		sc->wi_btag = rman_get_bustag(sc->iobase);
2136		sc->wi_bhandle = rman_get_bushandle(sc->iobase);
2137	} else {
2138		sc->mem_rid = rid;
2139		sc->mem = bus_alloc_resource(dev, SYS_RES_MEMORY,
2140		    &sc->mem_rid, 0, ~0, 1, RF_ACTIVE);
2141
2142		if (!sc->mem) {
2143			device_printf(dev, "No Mem space on prism2.5?\n");
2144			return (ENXIO);
2145		}
2146
2147		sc->wi_btag = rman_get_bustag(sc->mem);
2148		sc->wi_bhandle = rman_get_bushandle(sc->mem);
2149	}
2150
2151
2152	sc->irq_rid = 0;
2153	sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
2154	    0, ~0, 1, RF_ACTIVE |
2155	    ((sc->wi_bus_type == WI_BUS_PCCARD) ? 0 : RF_SHAREABLE));
2156
2157	if (!sc->irq) {
2158		wi_free(dev);
2159		device_printf(dev, "No irq?!\n");
2160		return (ENXIO);
2161	}
2162
2163	sc->dev = dev;
2164	sc->wi_unit = device_get_unit(dev);
2165
2166	return (0);
2167}
2168
2169static void
2170wi_free(dev)
2171	device_t		dev;
2172{
2173	struct wi_softc		*sc = device_get_softc(dev);
2174
2175	if (sc->iobase != NULL) {
2176		bus_release_resource(dev, SYS_RES_IOPORT, sc->iobase_rid, sc->iobase);
2177		sc->iobase = NULL;
2178	}
2179	if (sc->irq != NULL) {
2180		bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
2181		sc->irq = NULL;
2182	}
2183	if (sc->mem != NULL) {
2184		bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
2185		sc->mem = NULL;
2186	}
2187
2188	return;
2189}
2190
2191static void
2192wi_shutdown(dev)
2193	device_t		dev;
2194{
2195	struct wi_softc		*sc;
2196
2197	sc = device_get_softc(dev);
2198	wi_stop(sc);
2199
2200	return;
2201}
2202
2203#ifdef WICACHE
2204/* wavelan signal strength cache code.
2205 * store signal/noise/quality on per MAC src basis in
2206 * a small fixed cache.  The cache wraps if > MAX slots
2207 * used.  The cache may be zeroed out to start over.
2208 * Two simple filters exist to reduce computation:
2209 * 1. ip only (literally 0x800) which may be used
2210 * to ignore some packets.  It defaults to ip only.
2211 * it could be used to focus on broadcast, non-IP 802.11 beacons.
2212 * 2. multicast/broadcast only.  This may be used to
2213 * ignore unicast packets and only cache signal strength
2214 * for multicast/broadcast packets (beacons); e.g., Mobile-IP
2215 * beacons and not unicast traffic.
2216 *
2217 * The cache stores (MAC src(index), IP src (major clue), signal,
2218 *	quality, noise)
2219 *
2220 * No apologies for storing IP src here.  It's easy and saves much
2221 * trouble elsewhere.  The cache is assumed to be INET dependent,
2222 * although it need not be.
2223 */
2224
2225#ifdef documentation
2226
2227int wi_sigitems;                                /* number of cached entries */
2228struct wi_sigcache wi_sigcache[MAXWICACHE];  /*  array of cache entries */
2229int wi_nextitem;                                /*  index/# of entries */
2230
2231
2232#endif
2233
2234/* control variables for cache filtering.  Basic idea is
2235 * to reduce cost (e.g., to only Mobile-IP agent beacons
2236 * which are broadcast or multicast).  Still you might
2237 * want to measure signal strength with unicast ping packets
2238 * on a pt. to pt. ant. setup.
2239 */
2240/* set true if you want to limit cache items to broadcast/mcast
2241 * only packets (not unicast).  Useful for mobile-ip beacons which
2242 * are broadcast/multicast at network layer.  Default is all packets
2243 * so ping/unicast will work say with pt. to pt. antennae setup.
2244 */
2245static int wi_cache_mcastonly = 0;
2246SYSCTL_INT(_machdep, OID_AUTO, wi_cache_mcastonly, CTLFLAG_RW,
2247	&wi_cache_mcastonly, 0, "");
2248
2249/* set true if you want to limit cache items to IP packets only
2250*/
2251static int wi_cache_iponly = 1;
2252SYSCTL_INT(_machdep, OID_AUTO, wi_cache_iponly, CTLFLAG_RW,
2253	&wi_cache_iponly, 0, "");
2254
2255/*
2256 * Original comments:
2257 * -----------------
2258 * wi_cache_store, per rx packet store signal
2259 * strength in MAC (src) indexed cache.
2260 *
2261 * follows linux driver in how signal strength is computed.
2262 * In ad hoc mode, we use the rx_quality field.
2263 * signal and noise are trimmed to fit in the range from 47..138.
2264 * rx_quality field MSB is signal strength.
2265 * rx_quality field LSB is noise.
2266 * "quality" is (signal - noise) as is log value.
2267 * note: quality CAN be negative.
2268 *
2269 * In BSS mode, we use the RID for communication quality.
2270 * TBD:  BSS mode is currently untested.
2271 *
2272 * Bill's comments:
2273 * ---------------
2274 * Actually, we use the rx_quality field all the time for both "ad-hoc"
2275 * and BSS modes. Why? Because reading an RID is really, really expensive:
2276 * there's a bunch of PIO operations that have to be done to read a record
2277 * from the NIC, and reading the comms quality RID each time a packet is
2278 * received can really hurt performance. We don't have to do this anyway:
2279 * the comms quality field only reflects the values in the rx_quality field
2280 * anyway. The comms quality RID is only meaningful in infrastructure mode,
2281 * but the values it contains are updated based on the rx_quality from
2282 * frames received from the access point.
2283 *
2284 * Also, according to Lucent, the signal strength and noise level values
2285 * can be converted to dBms by subtracting 149, so I've modified the code
2286 * to do that instead of the scaling it did originally.
2287 */
2288static void
2289wi_cache_store(struct wi_softc *sc, struct ether_header *eh,
2290                     struct mbuf *m, unsigned short rx_quality)
2291{
2292	struct ip *ip = 0;
2293	int i;
2294	static int cache_slot = 0; 	/* use this cache entry */
2295	static int wrapindex = 0;       /* next "free" cache entry */
2296	int sig, noise;
2297	int sawip=0;
2298
2299	/* filters:
2300	 * 1. ip only
2301	 * 2. configurable filter to throw out unicast packets,
2302	 * keep multicast only.
2303	 */
2304
2305	if ((ntohs(eh->ether_type) == ETHERTYPE_IP)) {
2306		sawip = 1;
2307	}
2308
2309	/* filter for ip packets only
2310	*/
2311	if (wi_cache_iponly && !sawip) {
2312		return;
2313	}
2314
2315	/* filter for broadcast/multicast only
2316	 */
2317	if (wi_cache_mcastonly && ((eh->ether_dhost[0] & 1) == 0)) {
2318		return;
2319	}
2320
2321#ifdef SIGDEBUG
2322	printf("wi%d: q value %x (MSB=0x%x, LSB=0x%x) \n", sc->wi_unit,
2323	    rx_quality & 0xffff, rx_quality >> 8, rx_quality & 0xff);
2324#endif
2325
2326	/* find the ip header.  we want to store the ip_src
2327	 * address.
2328	 */
2329	if (sawip) {
2330		ip = mtod(m, struct ip *);
2331	}
2332
2333	/* do a linear search for a matching MAC address
2334	 * in the cache table
2335	 * . MAC address is 6 bytes,
2336	 * . var w_nextitem holds total number of entries already cached
2337	 */
2338	for(i = 0; i < sc->wi_nextitem; i++) {
2339		if (! bcmp(eh->ether_shost , sc->wi_sigcache[i].macsrc,  6 )) {
2340			/* Match!,
2341			 * so we already have this entry,
2342			 * update the data
2343			 */
2344			break;
2345		}
2346	}
2347
2348	/* did we find a matching mac address?
2349	 * if yes, then overwrite a previously existing cache entry
2350	 */
2351	if (i < sc->wi_nextitem )   {
2352		cache_slot = i;
2353	}
2354	/* else, have a new address entry,so
2355	 * add this new entry,
2356	 * if table full, then we need to replace LRU entry
2357	 */
2358	else    {
2359
2360		/* check for space in cache table
2361		 * note: wi_nextitem also holds number of entries
2362		 * added in the cache table
2363		 */
2364		if ( sc->wi_nextitem < MAXWICACHE ) {
2365			cache_slot = sc->wi_nextitem;
2366			sc->wi_nextitem++;
2367			sc->wi_sigitems = sc->wi_nextitem;
2368		}
2369        	/* no space found, so simply wrap with wrap index
2370		 * and "zap" the next entry
2371		 */
2372		else {
2373			if (wrapindex == MAXWICACHE) {
2374				wrapindex = 0;
2375			}
2376			cache_slot = wrapindex++;
2377		}
2378	}
2379
2380	/* invariant: cache_slot now points at some slot
2381	 * in cache.
2382	 */
2383	if (cache_slot < 0 || cache_slot >= MAXWICACHE) {
2384		log(LOG_ERR, "wi_cache_store, bad index: %d of "
2385		    "[0..%d], gross cache error\n",
2386		    cache_slot, MAXWICACHE);
2387		return;
2388	}
2389
2390	/*  store items in cache
2391	 *  .ip source address
2392	 *  .mac src
2393	 *  .signal, etc.
2394	 */
2395	if (sawip) {
2396		sc->wi_sigcache[cache_slot].ipsrc = ip->ip_src.s_addr;
2397	}
2398	bcopy( eh->ether_shost, sc->wi_sigcache[cache_slot].macsrc,  6);
2399
2400	sig = (rx_quality >> 8) & 0xFF;
2401	noise = rx_quality & 0xFF;
2402	sc->wi_sigcache[cache_slot].signal = sig - 149;
2403	sc->wi_sigcache[cache_slot].noise = noise - 149;
2404	sc->wi_sigcache[cache_slot].quality = sig - noise;
2405
2406	return;
2407}
2408#endif
2409
2410static int
2411wi_get_cur_ssid(sc, ssid, len)
2412	struct wi_softc		*sc;
2413	char			*ssid;
2414	int			*len;
2415{
2416	int			error = 0;
2417	struct wi_req		wreq;
2418
2419	wreq.wi_len = WI_MAX_DATALEN;
2420	switch (sc->wi_ptype) {
2421	case WI_PORTTYPE_ADHOC:
2422		wreq.wi_type = WI_RID_CURRENT_SSID;
2423		error = wi_read_record(sc, (struct wi_ltv_gen *)&wreq);
2424		if (error != 0)
2425			break;
2426		if (wreq.wi_val[0] > IEEE80211_NWID_LEN) {
2427			error = EINVAL;
2428			break;
2429		}
2430		*len = wreq.wi_val[0];
2431		bcopy(&wreq.wi_val[1], ssid, IEEE80211_NWID_LEN);
2432		break;
2433	case WI_PORTTYPE_BSS:
2434		wreq.wi_type = WI_RID_COMMQUAL;
2435		error = wi_read_record(sc, (struct wi_ltv_gen *)&wreq);
2436		if (error != 0)
2437			break;
2438		if (wreq.wi_val[0] != 0) /* associated */ {
2439			wreq.wi_type = WI_RID_CURRENT_SSID;
2440			wreq.wi_len = WI_MAX_DATALEN;
2441			error = wi_read_record(sc, (struct wi_ltv_gen *)&wreq);
2442			if (error != 0)
2443				break;
2444			if (wreq.wi_val[0] > IEEE80211_NWID_LEN) {
2445				error = EINVAL;
2446				break;
2447			}
2448			*len = wreq.wi_val[0];
2449			bcopy(&wreq.wi_val[1], ssid, IEEE80211_NWID_LEN);
2450		} else {
2451			*len = IEEE80211_NWID_LEN;
2452			bcopy(sc->wi_net_name, ssid, IEEE80211_NWID_LEN);
2453		}
2454		break;
2455	default:
2456		error = EINVAL;
2457		break;
2458	}
2459
2460	return error;
2461}
2462
2463static int
2464wi_media_change(ifp)
2465	struct ifnet		*ifp;
2466{
2467	struct wi_softc		*sc = ifp->if_softc;
2468	int			otype = sc->wi_ptype;
2469	int			orate = sc->wi_tx_rate;
2470
2471	if ((sc->ifmedia.ifm_cur->ifm_media & IFM_IEEE80211_ADHOC) != 0)
2472		sc->wi_ptype = WI_PORTTYPE_ADHOC;
2473	else
2474		sc->wi_ptype = WI_PORTTYPE_BSS;
2475
2476	switch (IFM_SUBTYPE(sc->ifmedia.ifm_cur->ifm_media)) {
2477	case IFM_IEEE80211_DS1:
2478		sc->wi_tx_rate = 1;
2479		break;
2480	case IFM_IEEE80211_DS2:
2481		sc->wi_tx_rate = 2;
2482		break;
2483	case IFM_IEEE80211_DS5:
2484		sc->wi_tx_rate = 5;
2485		break;
2486	case IFM_IEEE80211_DS11:
2487		sc->wi_tx_rate = 11;
2488		break;
2489	case IFM_AUTO:
2490		sc->wi_tx_rate = 3;
2491		break;
2492	}
2493
2494	if (otype != sc->wi_ptype ||
2495	    orate != sc->wi_tx_rate)
2496		wi_init(sc);
2497
2498	return(0);
2499}
2500
2501static void
2502wi_media_status(ifp, imr)
2503	struct ifnet		*ifp;
2504	struct ifmediareq	*imr;
2505{
2506	struct wi_req		wreq;
2507	struct wi_softc		*sc = ifp->if_softc;
2508
2509	if (sc->wi_tx_rate == 3) {
2510		imr->ifm_active = IFM_IEEE80211|IFM_AUTO;
2511		if (sc->wi_ptype == WI_PORTTYPE_ADHOC)
2512			imr->ifm_active |= IFM_IEEE80211_ADHOC;
2513		wreq.wi_type = WI_RID_CUR_TX_RATE;
2514		wreq.wi_len = WI_MAX_DATALEN;
2515		if (wi_read_record(sc, (struct wi_ltv_gen *)&wreq) == 0) {
2516			switch(wreq.wi_val[0]) {
2517			case 1:
2518				imr->ifm_active |= IFM_IEEE80211_DS1;
2519				break;
2520			case 2:
2521				imr->ifm_active |= IFM_IEEE80211_DS2;
2522				break;
2523			case 6:
2524				imr->ifm_active |= IFM_IEEE80211_DS5;
2525				break;
2526			case 11:
2527				imr->ifm_active |= IFM_IEEE80211_DS11;
2528				break;
2529				}
2530		}
2531	} else {
2532		imr->ifm_active = sc->ifmedia.ifm_cur->ifm_media;
2533	}
2534
2535	imr->ifm_status = IFM_AVALID;
2536	if (sc->wi_ptype == WI_PORTTYPE_ADHOC)
2537		/*
2538		 * XXX: It would be nice if we could give some actually
2539		 * useful status like whether we joined another IBSS or
2540		 * created one ourselves.
2541		 */
2542		imr->ifm_status |= IFM_ACTIVE;
2543	else {
2544		wreq.wi_type = WI_RID_COMMQUAL;
2545		wreq.wi_len = WI_MAX_DATALEN;
2546		if (wi_read_record(sc, (struct wi_ltv_gen *)&wreq) == 0 &&
2547		    wreq.wi_val[0] != 0)
2548			imr->ifm_status |= IFM_ACTIVE;
2549	}
2550}
2551