xref: /netbsd-src/sys/dev/ic/an.c (revision c71562d660be5e4ad22016bce45e96f08af190cc)
1 /*	$NetBSD: an.c,v 1.42 2006/03/12 03:34:37 dyoung Exp $	*/
2 /*
3  * Copyright (c) 1997, 1998, 1999
4  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Bill Paul.
17  * 4. Neither the name of the author nor the names of any co-contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31  * THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * $FreeBSD: src/sys/dev/an/if_an.c,v 1.12 2000/11/13 23:04:12 wpaul Exp $
34  */
35 /*
36  * Copyright (c) 2004, 2005 David Young.  All rights reserved.
37  * Copyright (c) 2004, 2005 OJC Technologies.  All rights reserved.
38  * Copyright (c) 2004, 2005 Dayton Data Center Services, LLC.  All
39  *     rights reserved.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  * 3. Neither the name of the author nor the names of any co-contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY David Young AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL David Young AND CONTRIBUTORS
57  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
58  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
59  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
60  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
61  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
63  * THE POSSIBILITY OF SUCH DAMAGE.
64  */
65 
66 /*
67  * Aironet 4500/4800 802.11 PCMCIA/ISA/PCI driver for FreeBSD.
68  *
69  * Written by Bill Paul <wpaul@ctr.columbia.edu>
70  * Electrical Engineering Department
71  * Columbia University, New York City
72  */
73 
74 /*
75  * Ported to NetBSD from FreeBSD by Atsushi Onoe at the San Diego
76  * IETF meeting.
77  */
78 
79 #include <sys/cdefs.h>
80 __KERNEL_RCSID(0, "$NetBSD: an.c,v 1.42 2006/03/12 03:34:37 dyoung Exp $");
81 
82 #include "bpfilter.h"
83 
84 #include <sys/param.h>
85 #include <sys/callout.h>
86 #include <sys/sysctl.h>
87 #include <sys/systm.h>
88 #include <sys/sockio.h>
89 #include <sys/mbuf.h>
90 #include <sys/kernel.h>
91 #include <sys/ucred.h>
92 #include <sys/socket.h>
93 #include <sys/device.h>
94 #include <sys/proc.h>
95 #include <sys/md4.h>
96 #include <sys/endian.h>
97 
98 #include <machine/bus.h>
99 
100 #include <net/if.h>
101 #include <net/if_dl.h>
102 #include <net/if_ether.h>
103 #include <net/if_llc.h>
104 #include <net/if_media.h>
105 #include <net/if_types.h>
106 
107 #include <net80211/ieee80211_netbsd.h>
108 #include <net80211/ieee80211_var.h>
109 #include <net80211/ieee80211_radiotap.h>
110 
111 #if NBPFILTER > 0
112 #include <net/bpf.h>
113 #include <net/bpfdesc.h>
114 #endif
115 
116 #include <dev/ic/anreg.h>
117 #include <dev/ic/anvar.h>
118 
119 static int	an_reset(struct an_softc *);
120 static void	an_wait(struct an_softc *);
121 static int	an_init(struct ifnet *);
122 static void	an_stop(struct ifnet *, int);
123 static void	an_start(struct ifnet *);
124 static void	an_watchdog(struct ifnet *);
125 static int	an_ioctl(struct ifnet *, u_long, caddr_t);
126 static int	an_media_change(struct ifnet *);
127 static void	an_media_status(struct ifnet *, struct ifmediareq *);
128 
129 static int	an_set_nwkey(struct an_softc *, struct ieee80211_nwkey *);
130 static int	an_set_nwkey_wep(struct an_softc *, struct ieee80211_nwkey *);
131 static int	an_set_nwkey_eap(struct an_softc *, struct ieee80211_nwkey *);
132 static int	an_get_nwkey(struct an_softc *, struct ieee80211_nwkey *);
133 static int	an_write_wepkey(struct an_softc *, int, struct an_wepkey *,
134 				int);
135 
136 static void	an_rx_intr(struct an_softc *);
137 static void	an_tx_intr(struct an_softc *, int);
138 static void	an_linkstat_intr(struct an_softc *);
139 
140 static int	an_cmd(struct an_softc *, int, int);
141 static int	an_seek_bap(struct an_softc *, int, int);
142 static int	an_read_bap(struct an_softc *, int, int, void *, int);
143 static int	an_write_bap(struct an_softc *, int, int, void *, int);
144 static int	an_mwrite_bap(struct an_softc *, int, int, struct mbuf *, int);
145 static int	an_read_rid(struct an_softc *, int, void *, int *);
146 static int	an_write_rid(struct an_softc *, int, void *, int);
147 
148 static int	an_alloc_fid(struct an_softc *, int, int *);
149 
150 static int	an_newstate(struct ieee80211com *, enum ieee80211_state, int);
151 
152 #ifdef AN_DEBUG
153 int an_debug = 0;
154 
155 #define	DPRINTF(X)	if (an_debug) printf X
156 #define	DPRINTF2(X)	if (an_debug > 1) printf X
157 static int an_sysctl_verify(SYSCTLFN_PROTO, int lower, int upper);
158 static int an_sysctl_verify_debug(SYSCTLFN_PROTO);
159 #else
160 #define	DPRINTF(X)
161 #define	DPRINTF2(X)
162 #endif
163 
164 int
165 an_attach(struct an_softc *sc)
166 {
167 	struct ieee80211com *ic = &sc->sc_ic;
168 	struct ifnet *ifp = &sc->sc_if;
169 	int i, s;
170 	struct an_rid_wepkey *akey;
171 	int buflen, kid, rid;
172 	int chan, chan_min, chan_max;
173 
174 	s = splnet();
175 	sc->sc_invalid = 0;
176 
177 	an_wait(sc);
178 	if (an_reset(sc) != 0) {
179 		sc->sc_invalid = 1;
180 		splx(s);
181 		return 1;
182 	}
183 
184 	/* Load factory config */
185 	if (an_cmd(sc, AN_CMD_READCFG, 0) != 0) {
186 		splx(s);
187 		aprint_error("%s: failed to load config data\n",
188 		    sc->sc_dev.dv_xname);
189 		return 1;
190 	}
191 
192 	/* Read the current configuration */
193 	buflen = sizeof(sc->sc_config);
194 	if (an_read_rid(sc, AN_RID_GENCONFIG, &sc->sc_config, &buflen) != 0) {
195 		splx(s);
196 		aprint_error("%s: read config failed\n", sc->sc_dev.dv_xname);
197 		return 1;
198 	}
199 
200 	/* Read the card capabilities */
201 	buflen = sizeof(sc->sc_caps);
202 	if (an_read_rid(sc, AN_RID_CAPABILITIES, &sc->sc_caps, &buflen) != 0) {
203 		splx(s);
204 		aprint_error("%s: read caps failed\n", sc->sc_dev.dv_xname);
205 		return 1;
206 	}
207 
208 #ifdef AN_DEBUG
209 	if (an_debug) {
210 		static const int dumprid[] = {
211 		    AN_RID_GENCONFIG, AN_RID_CAPABILITIES, AN_RID_SSIDLIST,
212 		    AN_RID_APLIST, AN_RID_STATUS, AN_RID_ENCAP
213 		};
214 
215 		for (rid = 0; rid < sizeof(dumprid)/sizeof(dumprid[0]); rid++) {
216 			buflen = sizeof(sc->sc_buf);
217 			if (an_read_rid(sc, dumprid[rid], &sc->sc_buf, &buflen)
218 			    != 0)
219 				continue;
220 			printf("%04x (%d):\n", dumprid[rid], buflen);
221 			for (i = 0; i < (buflen + 1) / 2; i++)
222 				printf(" %04x", sc->sc_buf.sc_val[i]);
223 			printf("\n");
224 		}
225 	}
226 #endif
227 
228 	/* Read WEP settings from persistent memory */
229 	akey = &sc->sc_buf.sc_wepkey;
230 	buflen = sizeof(struct an_rid_wepkey);
231 	rid = AN_RID_WEP_VOLATILE;	/* first persistent key */
232 	while (an_read_rid(sc, rid, akey, &buflen) == 0) {
233 		kid = le16toh(akey->an_key_index);
234 		DPRINTF(("an_attach: wep rid=0x%x len=%d(%d) index=0x%04x "
235 		    "mac[0]=%02x keylen=%d\n",
236 		    rid, buflen, sizeof(*akey), kid,
237 		    akey->an_mac_addr[0], le16toh(akey->an_key_len)));
238 		if (kid == 0xffff) {
239 			sc->sc_tx_perskey = akey->an_mac_addr[0];
240 			sc->sc_tx_key = -1;
241 			break;
242 		}
243 		if (kid >= IEEE80211_WEP_NKID)
244 			break;
245 		sc->sc_perskeylen[kid] = le16toh(akey->an_key_len);
246 		sc->sc_wepkeys[kid].an_wep_keylen = -1;
247 		rid = AN_RID_WEP_PERSISTENT;	/* for next key */
248 		buflen = sizeof(struct an_rid_wepkey);
249 	}
250 
251 	aprint_normal("%s: %s %s (firmware %s)\n", sc->sc_dev.dv_xname,
252 	    sc->sc_caps.an_manufname, sc->sc_caps.an_prodname,
253 	    sc->sc_caps.an_prodvers);
254 
255 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
256 
257 	ifp->if_softc = sc;
258 	ifp->if_flags = IFF_BROADCAST | IFF_NOTRAILERS | IFF_SIMPLEX |
259 	    IFF_MULTICAST | IFF_ALLMULTI;
260 	ifp->if_ioctl = an_ioctl;
261 	ifp->if_start = an_start;
262 	ifp->if_init = an_init;
263 	ifp->if_stop = an_stop;
264 	ifp->if_watchdog = an_watchdog;
265 	IFQ_SET_READY(&ifp->if_snd);
266 
267 	ic->ic_ifp = ifp;
268 	ic->ic_phytype = IEEE80211_T_DS;
269 	ic->ic_opmode = IEEE80211_M_STA;
270 	ic->ic_caps = IEEE80211_C_WEP | IEEE80211_C_PMGT | IEEE80211_C_IBSS |
271 	    IEEE80211_C_MONITOR;
272 	ic->ic_state = IEEE80211_S_INIT;
273 	IEEE80211_ADDR_COPY(ic->ic_myaddr, sc->sc_caps.an_oemaddr);
274 
275 	switch (le16toh(sc->sc_caps.an_regdomain)) {
276 	default:
277 	case AN_REGDOMAIN_USA:
278 	case AN_REGDOMAIN_CANADA:
279 		chan_min = 1; chan_max = 11; break;
280 	case AN_REGDOMAIN_EUROPE:
281 	case AN_REGDOMAIN_AUSTRALIA:
282 		chan_min = 1; chan_max = 13; break;
283 	case AN_REGDOMAIN_JAPAN:
284 		chan_min = 14; chan_max = 14; break;
285 	case AN_REGDOMAIN_SPAIN:
286 		chan_min = 10; chan_max = 11; break;
287 	case AN_REGDOMAIN_FRANCE:
288 		chan_min = 10; chan_max = 13; break;
289 	case AN_REGDOMAIN_JAPANWIDE:
290 		chan_min = 1; chan_max = 14; break;
291 	}
292 
293 	for (chan = chan_min; chan <= chan_max; chan++) {
294 		ic->ic_channels[chan].ic_freq =
295 		    ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ);
296 		ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_B;
297 	}
298 	ic->ic_ibss_chan = &ic->ic_channels[chan_min];
299 
300 	aprint_normal("%s: 802.11 address: %s, channel: %d-%d\n",
301 	    ifp->if_xname, ether_sprintf(ic->ic_myaddr), chan_min, chan_max);
302 
303 	/* Find supported rate */
304 	for (i = 0; i < sizeof(sc->sc_caps.an_rates); i++) {
305 		if (sc->sc_caps.an_rates[i] == 0)
306 			continue;
307 		ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[
308 		    ic->ic_sup_rates[IEEE80211_MODE_11B].rs_nrates++] =
309 		    sc->sc_caps.an_rates[i];
310 	}
311 
312 	/*
313 	 * Call MI attach routine.
314 	 */
315 	if_attach(ifp);
316 	ieee80211_ifattach(ic);
317 
318 	sc->sc_newstate = ic->ic_newstate;
319 	ic->ic_newstate = an_newstate;
320 
321 	ieee80211_media_init(ic, an_media_change, an_media_status);
322 
323 	/*
324 	 * radiotap BPF device
325 	 */
326 #if NBPFILTER > 0
327 	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
328 	    sizeof(struct ieee80211_frame) + 64, &sc->sc_drvbpf);
329 #endif
330 
331 	memset(&sc->sc_rxtapu, 0, sizeof(sc->sc_rxtapu));
332 	sc->sc_rxtap.ar_ihdr.it_len = htole16(sizeof(sc->sc_rxtapu));
333 	sc->sc_rxtap.ar_ihdr.it_present = htole32(AN_RX_RADIOTAP_PRESENT);
334 
335 	memset(&sc->sc_txtapu, 0, sizeof(sc->sc_txtapu));
336 	sc->sc_txtap.at_ihdr.it_len = htole16(sizeof(sc->sc_txtapu));
337 	sc->sc_txtap.at_ihdr.it_present = htole32(AN_TX_RADIOTAP_PRESENT);
338 
339 	sc->sc_attached = 1;
340 	splx(s);
341 
342 	ieee80211_announce(ic);
343 	return 0;
344 }
345 
346 #ifdef AN_DEBUG
347 /*
348  * Setup sysctl(3) MIB, hw.an.*
349  *
350  * TBD condition CTLFLAG_PERMANENT on being an LKM or not
351  */
352 SYSCTL_SETUP(sysctl_an, "sysctl an(4) subtree setup")
353 {
354 	int rc;
355 	const struct sysctlnode *cnode, *rnode;
356 
357 	if ((rc = sysctl_createv(clog, 0, NULL, &rnode,
358 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
359 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0)
360 		goto err;
361 
362 	if ((rc = sysctl_createv(clog, 0, &rnode, &rnode,
363 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "an",
364 	    "Cisco/Aironet 802.11 controls",
365 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
366 		goto err;
367 
368 	/* control debugging printfs */
369 	if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
370 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
371 	    "debug", SYSCTL_DESCR("Enable Cisco/Aironet debugging output"),
372 	    an_sysctl_verify_debug, 0, &an_debug, 0,
373 	    CTL_CREATE, CTL_EOL)) != 0)
374 		goto err;
375 
376 	return;
377 err:
378 	printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
379 }
380 
381 static int
382 an_sysctl_verify(SYSCTLFN_ARGS, int lower, int upper)
383 {
384 	int error, t;
385 	struct sysctlnode node;
386 
387 	node = *rnode;
388 	t = *(int*)rnode->sysctl_data;
389 	node.sysctl_data = &t;
390 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
391 	if (error || newp == NULL)
392 		return (error);
393 
394 	if (t < lower || t > upper)
395 		return (EINVAL);
396 
397 	*(int*)rnode->sysctl_data = t;
398 
399 	return (0);
400 }
401 
402 static int
403 an_sysctl_verify_debug(SYSCTLFN_ARGS)
404 {
405 	return an_sysctl_verify(SYSCTLFN_CALL(rnode), 0, 2);
406 }
407 #endif /* AN_DEBUG */
408 
409 int
410 an_detach(struct an_softc *sc)
411 {
412 	struct ieee80211com *ic = &sc->sc_ic;
413 	struct ifnet *ifp = &sc->sc_if;
414 	int s;
415 
416 	if (!sc->sc_attached)
417 		return 0;
418 
419 	s = splnet();
420 	sc->sc_invalid = 1;
421 	an_stop(ifp, 1);
422 	ieee80211_ifdetach(ic);
423 	if_detach(ifp);
424 	splx(s);
425 	return 0;
426 }
427 
428 int
429 an_activate(struct device *self, enum devact act)
430 {
431 	struct an_softc *sc = (struct an_softc *)self;
432 	int s, error = 0;
433 
434 	s = splnet();
435 	switch (act) {
436 	case DVACT_ACTIVATE:
437 		error = EOPNOTSUPP;
438 		break;
439 
440 	case DVACT_DEACTIVATE:
441 		sc->sc_invalid = 1;
442 		if_deactivate(&sc->sc_if);
443 		break;
444 	}
445 	splx(s);
446 
447 	return error;
448 }
449 
450 void
451 an_power(int why, void *arg)
452 {
453 	int s;
454 	struct an_softc *sc = arg;
455 	struct ifnet *ifp = &sc->sc_if;
456 
457 	s = splnet();
458 	switch (why) {
459 	case PWR_SUSPEND:
460 	case PWR_STANDBY:
461 		an_stop(ifp, 1);
462 		break;
463 	case PWR_RESUME:
464 		if (ifp->if_flags & IFF_UP) {
465 			an_init(ifp);
466 			(void)an_intr(sc);
467 		}
468 		break;
469 	case PWR_SOFTSUSPEND:
470 	case PWR_SOFTSTANDBY:
471 	case PWR_SOFTRESUME:
472 		break;
473 	}
474 	splx(s);
475 }
476 
477 void
478 an_shutdown(struct an_softc *sc)
479 {
480 
481 	if (sc->sc_attached)
482 		an_stop(&sc->sc_if, 1);
483 }
484 
485 int
486 an_intr(void *arg)
487 {
488 	struct an_softc *sc = arg;
489 	struct ifnet *ifp = &sc->sc_if;
490 	int i;
491 	u_int16_t status;
492 
493 	if (!sc->sc_enabled || sc->sc_invalid ||
494 	    !device_is_active(&sc->sc_dev) ||
495 	    (ifp->if_flags & IFF_RUNNING) == 0)
496 		return 0;
497 
498 	if ((ifp->if_flags & IFF_UP) == 0) {
499 		CSR_WRITE_2(sc, AN_INT_EN, 0);
500 		CSR_WRITE_2(sc, AN_EVENT_ACK, ~0);
501 		return 1;
502 	}
503 
504 	/* maximum 10 loops per interrupt */
505 	for (i = 0; i < 10; i++) {
506 		if (!sc->sc_enabled || sc->sc_invalid)
507 			return 1;
508 		if (CSR_READ_2(sc, AN_SW0) != AN_MAGIC) {
509 			DPRINTF(("an_intr: magic number changed: %x\n",
510 			    CSR_READ_2(sc, AN_SW0)));
511 			sc->sc_invalid = 1;
512 			return 1;
513 		}
514 		status = CSR_READ_2(sc, AN_EVENT_STAT);
515 		CSR_WRITE_2(sc, AN_EVENT_ACK, status & ~(AN_INTRS));
516 		if ((status & AN_INTRS) == 0)
517 			break;
518 
519 		if (status & AN_EV_RX)
520 			an_rx_intr(sc);
521 
522 		if (status & (AN_EV_TX | AN_EV_TX_EXC))
523 			an_tx_intr(sc, status);
524 
525 		if (status & AN_EV_LINKSTAT)
526 			an_linkstat_intr(sc);
527 
528 		if ((ifp->if_flags & IFF_OACTIVE) == 0 &&
529 		    sc->sc_ic.ic_state == IEEE80211_S_RUN &&
530 		    !IFQ_IS_EMPTY(&ifp->if_snd))
531 			an_start(ifp);
532 	}
533 
534 	return 1;
535 }
536 
537 static int
538 an_init(struct ifnet *ifp)
539 {
540 	struct an_softc *sc = ifp->if_softc;
541 	struct ieee80211com *ic = &sc->sc_ic;
542 	int i, error, fid;
543 
544 	DPRINTF(("an_init: enabled %d\n", sc->sc_enabled));
545 	if (!sc->sc_enabled) {
546 		if (sc->sc_enable)
547 			(*sc->sc_enable)(sc);
548 		an_wait(sc);
549 		sc->sc_enabled = 1;
550 	} else {
551 		an_stop(ifp, 0);
552 		if ((error = an_reset(sc)) != 0) {
553 			printf("%s: failed to reset\n", ifp->if_xname);
554 			an_stop(ifp, 1);
555 			return error;
556 		}
557 	}
558 	CSR_WRITE_2(sc, AN_SW0, AN_MAGIC);
559 
560 	/* Allocate the TX buffers */
561 	for (i = 0; i < AN_TX_RING_CNT; i++) {
562 		if ((error = an_alloc_fid(sc, AN_TX_MAX_LEN, &fid)) != 0) {
563 			printf("%s: failed to allocate nic memory\n",
564 			    ifp->if_xname);
565 			an_stop(ifp, 1);
566 			return error;
567 		}
568 		DPRINTF2(("an_init: txbuf %d allocated %x\n", i, fid));
569 		sc->sc_txd[i].d_fid = fid;
570 		sc->sc_txd[i].d_inuse = 0;
571 	}
572 	sc->sc_txcur = sc->sc_txnext = 0;
573 
574 	IEEE80211_ADDR_COPY(sc->sc_config.an_macaddr, ic->ic_myaddr);
575 	sc->sc_config.an_scanmode = htole16(AN_SCANMODE_ACTIVE);
576 	sc->sc_config.an_authtype = htole16(AN_AUTHTYPE_OPEN);	/*XXX*/
577 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
578 		sc->sc_config.an_authtype |=
579 		    htole16(AN_AUTHTYPE_PRIVACY_IN_USE);
580 		if (sc->sc_use_leap)
581 			sc->sc_config.an_authtype |=
582 			    htole16(AN_AUTHTYPE_LEAP);
583 	}
584 	sc->sc_config.an_listen_interval = htole16(ic->ic_lintval);
585 	sc->sc_config.an_beacon_period = htole16(ic->ic_lintval);
586 	if (ic->ic_flags & IEEE80211_F_PMGTON)
587 		sc->sc_config.an_psave_mode = htole16(AN_PSAVE_PSP);
588 	else
589 		sc->sc_config.an_psave_mode = htole16(AN_PSAVE_CAM);
590 	sc->sc_config.an_ds_channel =
591 	    htole16(ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
592 
593 	switch (ic->ic_opmode) {
594 	case IEEE80211_M_STA:
595 		sc->sc_config.an_opmode =
596 		    htole16(AN_OPMODE_INFRASTRUCTURE_STATION);
597 		sc->sc_config.an_rxmode = htole16(AN_RXMODE_BC_MC_ADDR);
598 		break;
599 	case IEEE80211_M_IBSS:
600 		sc->sc_config.an_opmode = htole16(AN_OPMODE_IBSS_ADHOC);
601 		sc->sc_config.an_rxmode = htole16(AN_RXMODE_BC_MC_ADDR);
602 		break;
603 	case IEEE80211_M_MONITOR:
604 		sc->sc_config.an_opmode =
605 		    htole16(AN_OPMODE_INFRASTRUCTURE_STATION);
606 		sc->sc_config.an_rxmode =
607 		    htole16(AN_RXMODE_80211_MONITOR_ANYBSS);
608 		sc->sc_config.an_authtype = htole16(AN_AUTHTYPE_NONE);
609 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
610 			sc->sc_config.an_authtype |=
611 			    htole16(AN_AUTHTYPE_PRIVACY_IN_USE |
612 		            AN_AUTHTYPE_ALLOW_UNENCRYPTED);
613 		break;
614 	default:
615 		printf("%s: bad opmode %d\n", ifp->if_xname, ic->ic_opmode);
616 		an_stop(ifp, 1);
617 		return EIO;
618 	}
619 	sc->sc_config.an_rxmode |= htole16(AN_RXMODE_NO_8023_HEADER);
620 
621 	/* Set the ssid list */
622 	memset(&sc->sc_buf, 0, sizeof(sc->sc_buf.sc_ssidlist));
623 	sc->sc_buf.sc_ssidlist.an_entry[0].an_ssid_len =
624 	    htole16(ic->ic_des_esslen);
625 	if (ic->ic_des_esslen)
626 		memcpy(sc->sc_buf.sc_ssidlist.an_entry[0].an_ssid,
627 		    ic->ic_des_essid, ic->ic_des_esslen);
628 	if (an_write_rid(sc, AN_RID_SSIDLIST, &sc->sc_buf,
629 	    sizeof(sc->sc_buf.sc_ssidlist)) != 0) {
630 		printf("%s: failed to write ssid list\n", ifp->if_xname);
631 		an_stop(ifp, 1);
632 		return error;
633 	}
634 
635 	/* Set the AP list */
636 	memset(&sc->sc_buf, 0, sizeof(sc->sc_buf.sc_aplist));
637 	(void)an_write_rid(sc, AN_RID_APLIST, &sc->sc_buf,
638 	    sizeof(sc->sc_buf.sc_aplist));
639 
640 	/* Set the encapsulation */
641 	for (i = 0; i < AN_ENCAP_NENTS; i++) {
642 		sc->sc_buf.sc_encap.an_entry[i].an_ethertype = htole16(0);
643 		sc->sc_buf.sc_encap.an_entry[i].an_action =
644 		    htole16(AN_RXENCAP_RFC1024 | AN_TXENCAP_RFC1024);
645 	}
646 	(void)an_write_rid(sc, AN_RID_ENCAP, &sc->sc_buf,
647 	    sizeof(sc->sc_buf.sc_encap));
648 
649 	/* Set the WEP Keys */
650 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
651 		an_write_wepkey(sc, AN_RID_WEP_VOLATILE, sc->sc_wepkeys,
652 		    sc->sc_tx_key);
653 
654 	/* Set the configuration */
655 #ifdef AN_DEBUG
656 	if (an_debug) {
657 		printf("write config:\n");
658 		for (i = 0; i < sizeof(sc->sc_config) / 2; i++)
659 			printf(" %04x", ((u_int16_t *)&sc->sc_config)[i]);
660 		printf("\n");
661 	}
662 #endif
663 	if (an_write_rid(sc, AN_RID_GENCONFIG, &sc->sc_config,
664 	    sizeof(sc->sc_config)) != 0) {
665 		printf("%s: failed to write config\n", ifp->if_xname);
666 		an_stop(ifp, 1);
667 		return error;
668 	}
669 
670 	/* Enable the MAC */
671 	if (an_cmd(sc, AN_CMD_ENABLE, 0)) {
672 		printf("%s: failed to enable MAC\n", sc->sc_dev.dv_xname);
673 		an_stop(ifp, 1);
674 		return ENXIO;
675 	}
676 	if (ifp->if_flags & IFF_PROMISC)
677 		an_cmd(sc, AN_CMD_SET_MODE, 0xffff);
678 
679 	ifp->if_flags |= IFF_RUNNING;
680 	ifp->if_flags &= ~IFF_OACTIVE;
681 	ic->ic_state = IEEE80211_S_INIT;
682 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
683 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
684 
685 	/* enable interrupts */
686 	CSR_WRITE_2(sc, AN_INT_EN, AN_INTRS);
687 	return 0;
688 }
689 
690 static void
691 an_stop(struct ifnet *ifp, int disable)
692 {
693 	struct an_softc *sc = ifp->if_softc;
694 	int i, s;
695 
696 	if (!sc->sc_enabled)
697 		return;
698 
699 	DPRINTF(("an_stop: disable %d\n", disable));
700 
701 	s = splnet();
702 	ieee80211_new_state(&sc->sc_ic, IEEE80211_S_INIT, -1);
703 	if (!sc->sc_invalid) {
704 		an_cmd(sc, AN_CMD_FORCE_SYNCLOSS, 0);
705 		CSR_WRITE_2(sc, AN_INT_EN, 0);
706 		an_cmd(sc, AN_CMD_DISABLE, 0);
707 
708 		for (i = 0; i < AN_TX_RING_CNT; i++)
709 			an_cmd(sc, AN_CMD_DEALLOC_MEM, sc->sc_txd[i].d_fid);
710 	}
711 
712 	sc->sc_tx_timer = 0;
713 	ifp->if_timer = 0;
714 	ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
715 
716 	if (disable) {
717 		if (sc->sc_disable)
718 			(*sc->sc_disable)(sc);
719 		sc->sc_enabled = 0;
720 	}
721 	splx(s);
722 }
723 
724 static void
725 an_start(struct ifnet *ifp)
726 {
727 	struct an_softc *sc = (struct an_softc *)ifp->if_softc;
728 	struct ieee80211com *ic = &sc->sc_ic;
729 	struct ieee80211_node *ni;
730 	struct ieee80211_frame *wh;
731 	struct an_txframe frmhdr;
732 	struct ether_header *eh;
733 	struct mbuf *m;
734 	u_int16_t len;
735 	int cur, fid;
736 
737 	if (!sc->sc_enabled || sc->sc_invalid) {
738 		DPRINTF(("an_start: noop: enabled %d invalid %d\n",
739 		    sc->sc_enabled, sc->sc_invalid));
740 		return;
741 	}
742 
743 	memset(&frmhdr, 0, sizeof(frmhdr));
744 	cur = sc->sc_txnext;
745 	for (;;) {
746 		if (ic->ic_state != IEEE80211_S_RUN) {
747 			DPRINTF(("an_start: not running %d\n", ic->ic_state));
748 			break;
749 		}
750 		IFQ_POLL(&ifp->if_snd, m);
751 		if (m == NULL) {
752 			DPRINTF2(("an_start: no pending mbuf\n"));
753 			break;
754 		}
755 		if (sc->sc_txd[cur].d_inuse) {
756 			DPRINTF2(("an_start: %x/%d busy\n",
757 			    sc->sc_txd[cur].d_fid, cur));
758 			ifp->if_flags |= IFF_OACTIVE;
759 			break;
760 		}
761 		IFQ_DEQUEUE(&ifp->if_snd, m);
762 		ifp->if_opackets++;
763 #if NBPFILTER > 0
764 		if (ifp->if_bpf)
765 			bpf_mtap(ifp->if_bpf, m);
766 #endif
767 		eh = mtod(m, struct ether_header *);
768 		ni = ieee80211_find_txnode(ic, eh->ether_dhost);
769 		if (ni == NULL) {
770 			/* NB: ieee80211_find_txnode does stat+msg */
771 			goto bad;
772 		}
773 		if ((m = ieee80211_encap(ic, m, ni)) == NULL)
774 			goto bad;
775 		ieee80211_free_node(ni);
776 #if NBPFILTER > 0
777 		if (ic->ic_rawbpf)
778 			bpf_mtap(ic->ic_rawbpf, m);
779 #endif
780 
781 		wh = mtod(m, struct ieee80211_frame *);
782 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
783 			wh->i_fc[1] |= IEEE80211_FC1_WEP;
784 		m_copydata(m, 0, sizeof(struct ieee80211_frame),
785 		    (caddr_t)&frmhdr.an_whdr);
786 
787 		/* insert payload length in front of llc/snap */
788 		len = htons(m->m_pkthdr.len - sizeof(struct ieee80211_frame));
789 		m_adj(m, sizeof(struct ieee80211_frame) - sizeof(len));
790 		if (mtod(m, u_long) & 0x01)
791 			memcpy(mtod(m, caddr_t), &len, sizeof(len));
792 		else
793 			*mtod(m, u_int16_t *) = len;
794 
795 		/*
796 		 * XXX Aironet firmware apparently convert the packet
797 		 * with longer than 1500 bytes in length into LLC/SNAP.
798 		 * If we have 1500 bytes in ethernet payload, it is
799 		 * 1508 bytes including LLC/SNAP and will be inserted
800 		 * additional LLC/SNAP header with 1501-1508 in its
801 		 * ethertype !!
802 		 * So we skip LLC/SNAP header and force firmware to
803 		 * convert it to LLC/SNAP again.
804 		 */
805 		m_adj(m, sizeof(struct llc));
806 
807 		frmhdr.an_tx_ctl = htole16(AN_TXCTL_80211);
808 		frmhdr.an_tx_payload_len = htole16(m->m_pkthdr.len);
809 		frmhdr.an_gaplen = htole16(AN_TXGAP_802_11);
810 
811 		if (ic->ic_fixed_rate != -1)
812 			frmhdr.an_tx_rate =
813 			    ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[
814 			    ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
815 		else
816 			frmhdr.an_tx_rate = 0;
817 
818 		/* XXX radiotap for tx must be completed */
819 #if NBPFILTER > 0
820 		if (sc->sc_drvbpf) {
821 			struct an_tx_radiotap_header *tap = &sc->sc_txtap;
822 			tap->at_rate = ic->ic_bss->ni_rates.rs_rates[ic->ic_bss->ni_txrate];
823 			tap->at_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
824 			tap->at_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
825 			/* TBD tap->wt_flags */
826 			bpf_mtap2(sc->sc_drvbpf, tap, tap->at_ihdr.it_len, m);
827 		}
828 #endif
829 
830 #ifdef AN_DEBUG
831 		if ((ifp->if_flags & (IFF_DEBUG|IFF_LINK2)) ==
832 		    (IFF_DEBUG|IFF_LINK2)) {
833 			ieee80211_dump_pkt((u_int8_t *)&frmhdr.an_whdr,
834 			    sizeof(struct ieee80211_frame), -1, 0);
835 			printf(" txctl 0x%x plen %u\n",
836 			    le16toh(frmhdr.an_tx_ctl),
837 			    le16toh(frmhdr.an_tx_payload_len));
838 		}
839 #endif
840 		if (sizeof(frmhdr) + AN_TXGAP_802_11 + sizeof(len) +
841 		    m->m_pkthdr.len > AN_TX_MAX_LEN)
842 			goto bad;
843 
844 		fid = sc->sc_txd[cur].d_fid;
845 		if (an_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0)
846 			goto bad;
847 		/* dummy write to avoid seek. */
848 		an_write_bap(sc, fid, -1, &frmhdr, AN_TXGAP_802_11);
849 		an_mwrite_bap(sc, fid, -1, m, m->m_pkthdr.len);
850 		m_freem(m);
851 
852 		DPRINTF2(("an_start: send %d byte via %x/%d\n",
853 		    ntohs(len) + sizeof(struct ieee80211_frame),
854 		    fid, cur));
855 		sc->sc_txd[cur].d_inuse = 1;
856 		if (an_cmd(sc, AN_CMD_TX, fid)) {
857 			printf("%s: xmit failed\n", ifp->if_xname);
858 			sc->sc_txd[cur].d_inuse = 0;
859 			continue;
860 		}
861 		sc->sc_tx_timer = 5;
862 		ifp->if_timer = 1;
863 		AN_INC(cur, AN_TX_RING_CNT);
864 		sc->sc_txnext = cur;
865 		continue;
866 bad:
867 		ifp->if_oerrors++;
868 		m_freem(m);
869 	}
870 }
871 
872 static int
873 an_reset(struct an_softc *sc)
874 {
875 
876 	DPRINTF(("an_reset\n"));
877 
878 	if (!sc->sc_enabled)
879 		return ENXIO;
880 
881 	an_cmd(sc, AN_CMD_ENABLE, 0);
882 	an_cmd(sc, AN_CMD_FW_RESTART, 0);
883 	an_cmd(sc, AN_CMD_NOOP2, 0);
884 
885 	if (an_cmd(sc, AN_CMD_FORCE_SYNCLOSS, 0) == ETIMEDOUT) {
886 		printf("%s: reset failed\n", sc->sc_dev.dv_xname);
887 		return ETIMEDOUT;
888 	}
889 
890 	an_cmd(sc, AN_CMD_DISABLE, 0);
891 	return 0;
892 }
893 
894 static void
895 an_watchdog(struct ifnet *ifp)
896 {
897 	struct an_softc *sc = ifp->if_softc;
898 
899 	if (!sc->sc_enabled)
900 		return;
901 
902 	if (sc->sc_tx_timer) {
903 		if (--sc->sc_tx_timer == 0) {
904 			printf("%s: device timeout\n", ifp->if_xname);
905 			ifp->if_oerrors++;
906 			an_init(ifp);
907 			return;
908 		}
909 		ifp->if_timer = 1;
910 	}
911 	ieee80211_watchdog(&sc->sc_ic);
912 }
913 
914 static int
915 an_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
916 {
917 	struct an_softc *sc = ifp->if_softc;
918 	int s, error = 0;
919 
920 	if (!device_is_active(&sc->sc_dev))
921 		return ENXIO;
922 
923 	s = splnet();
924 
925 	switch (command) {
926 	case SIOCSIFFLAGS:
927 		if (ifp->if_flags & IFF_UP) {
928 			if (sc->sc_enabled) {
929 				/*
930 				 * To avoid rescanning another access point,
931 				 * do not call an_init() here.  Instead, only
932 				 * reflect promisc mode settings.
933 				 */
934 				error = an_cmd(sc, AN_CMD_SET_MODE,
935 				    (ifp->if_flags & IFF_PROMISC) ? 0xffff : 0);
936 			} else
937 				error = an_init(ifp);
938 		} else if (sc->sc_enabled)
939 			an_stop(ifp, 1);
940 		break;
941 	case SIOCADDMULTI:
942 	case SIOCDELMULTI:
943 		error = ether_ioctl(ifp, command, data);
944 		if (error == ENETRESET) {
945 			/* we don't have multicast filter. */
946 			error = 0;
947 		}
948 		break;
949 	case SIOCS80211NWKEY:
950 		error = an_set_nwkey(sc, (struct ieee80211_nwkey *)data);
951 			break;
952 	case SIOCG80211NWKEY:
953 		error = an_get_nwkey(sc, (struct ieee80211_nwkey *)data);
954 		break;
955 	default:
956 		error = ieee80211_ioctl(&sc->sc_ic, command, data);
957 		break;
958 	}
959 	if (error == ENETRESET) {
960 		if (sc->sc_enabled)
961 			error = an_init(ifp);
962 		else
963 			error = 0;
964 	}
965 	splx(s);
966 	return error;
967 }
968 
969 /* TBD factor with ieee80211_media_change */
970 static int
971 an_media_change(struct ifnet *ifp)
972 {
973 	struct an_softc *sc = ifp->if_softc;
974 	struct ieee80211com *ic = &sc->sc_ic;
975 	struct ifmedia_entry *ime;
976 	enum ieee80211_opmode newmode;
977 	int i, rate, error = 0;
978 
979 	ime = ic->ic_media.ifm_cur;
980 	if (IFM_SUBTYPE(ime->ifm_media) == IFM_AUTO) {
981 		i = -1;
982 	} else {
983 		struct ieee80211_rateset *rs =
984 		    &ic->ic_sup_rates[IEEE80211_MODE_11B];
985 		rate = ieee80211_media2rate(ime->ifm_media);
986 		if (rate == 0)
987 			return EINVAL;
988 		for (i = 0; i < rs->rs_nrates; i++) {
989 			if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == rate)
990 				break;
991 		}
992 		if (i == rs->rs_nrates)
993 			return EINVAL;
994 	}
995 	if (ic->ic_fixed_rate != i) {
996 		ic->ic_fixed_rate = i;
997 		error = ENETRESET;
998 	}
999 
1000 	if (ime->ifm_media & IFM_IEEE80211_ADHOC)
1001 		newmode = IEEE80211_M_IBSS;
1002 	else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
1003 		newmode = IEEE80211_M_HOSTAP;
1004 	else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
1005 		newmode = IEEE80211_M_MONITOR;
1006 	else
1007 		newmode = IEEE80211_M_STA;
1008 	if (ic->ic_opmode != newmode) {
1009 		ic->ic_opmode = newmode;
1010 		error = ENETRESET;
1011 	}
1012 	if (error == ENETRESET) {
1013 		if (sc->sc_enabled)
1014 			error = an_init(ifp);
1015 		else
1016 			error = 0;
1017 	}
1018 	ifp->if_baudrate = ifmedia_baudrate(ic->ic_media.ifm_cur->ifm_media);
1019 
1020 	return error;
1021 }
1022 
1023 static void
1024 an_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1025 {
1026 	struct an_softc *sc = ifp->if_softc;
1027 	struct ieee80211com *ic = &sc->sc_ic;
1028 	int rate, buflen;
1029 
1030 	if (sc->sc_enabled == 0) {
1031 		imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
1032 		imr->ifm_status = 0;
1033 		return;
1034 	}
1035 
1036 	imr->ifm_status = IFM_AVALID;
1037 	imr->ifm_active = IFM_IEEE80211;
1038 	if (ic->ic_state == IEEE80211_S_RUN)
1039 		imr->ifm_status |= IFM_ACTIVE;
1040 	buflen = sizeof(sc->sc_buf);
1041 	if (ic->ic_fixed_rate != -1)
1042 		rate = ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[
1043 		    ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
1044 	else if (an_read_rid(sc, AN_RID_STATUS, &sc->sc_buf, &buflen) != 0)
1045 		rate = 0;
1046 	else
1047 		rate = le16toh(sc->sc_buf.sc_status.an_current_tx_rate);
1048 	imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
1049 	switch (ic->ic_opmode) {
1050 	case IEEE80211_M_STA:
1051 		break;
1052 	case IEEE80211_M_IBSS:
1053 		imr->ifm_active |= IFM_IEEE80211_ADHOC;
1054 		break;
1055 	case IEEE80211_M_HOSTAP:
1056 		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1057 		break;
1058 	case IEEE80211_M_MONITOR:
1059 		imr->ifm_active |= IFM_IEEE80211_MONITOR;
1060 		break;
1061 	default:
1062 		break;
1063 	}
1064 }
1065 
1066 static int
1067 an_set_nwkey(struct an_softc *sc, struct ieee80211_nwkey *nwkey)
1068 {
1069 	int error;
1070 	struct ieee80211com *ic = &sc->sc_ic;
1071 	u_int16_t prevauth;
1072 
1073 	error = 0;
1074 	prevauth = sc->sc_config.an_authtype;
1075 
1076 	switch (nwkey->i_wepon) {
1077 	case IEEE80211_NWKEY_OPEN:
1078 		sc->sc_config.an_authtype = AN_AUTHTYPE_OPEN;
1079 		ic->ic_flags &= ~IEEE80211_F_PRIVACY;
1080 		break;
1081 
1082 	case IEEE80211_NWKEY_WEP:
1083 	case IEEE80211_NWKEY_WEP | IEEE80211_NWKEY_PERSIST:
1084 		error = an_set_nwkey_wep(sc, nwkey);
1085 		if (error == 0 || error == ENETRESET) {
1086 			sc->sc_config.an_authtype =
1087 			    AN_AUTHTYPE_OPEN | AN_AUTHTYPE_PRIVACY_IN_USE;
1088 			ic->ic_flags |= IEEE80211_F_PRIVACY;
1089 		}
1090 		break;
1091 
1092 	case IEEE80211_NWKEY_EAP:
1093 		error = an_set_nwkey_eap(sc, nwkey);
1094 		if (error == 0 || error == ENETRESET) {
1095 			sc->sc_config.an_authtype = AN_AUTHTYPE_OPEN |
1096 			    AN_AUTHTYPE_PRIVACY_IN_USE | AN_AUTHTYPE_LEAP;
1097 			ic->ic_flags |= IEEE80211_F_PRIVACY;
1098 		}
1099 		break;
1100 	default:
1101 		error = EINVAL;
1102 		break;
1103 	}
1104 	if (error == 0 && prevauth != sc->sc_config.an_authtype)
1105 		error = ENETRESET;
1106 	return error;
1107 }
1108 
1109 static int
1110 an_set_nwkey_wep(struct an_softc *sc, struct ieee80211_nwkey *nwkey)
1111 {
1112 	int i, txkey, anysetkey, needreset, error;
1113 	struct an_wepkey keys[IEEE80211_WEP_NKID];
1114 
1115 	error = 0;
1116 	memset(keys, 0, sizeof(keys));
1117 	anysetkey = needreset = 0;
1118 
1119 	/* load argument and sanity check */
1120 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1121 		keys[i].an_wep_keylen = nwkey->i_key[i].i_keylen;
1122 		if (keys[i].an_wep_keylen < 0)
1123 			continue;
1124 		if (keys[i].an_wep_keylen != 0 &&
1125 		    keys[i].an_wep_keylen < IEEE80211_WEP_KEYLEN)
1126 			return EINVAL;
1127 		if (keys[i].an_wep_keylen > sizeof(keys[i].an_wep_key))
1128 			return EINVAL;
1129 		if ((error = copyin(nwkey->i_key[i].i_keydat,
1130 		    keys[i].an_wep_key, keys[i].an_wep_keylen)) != 0)
1131 			return error;
1132 		anysetkey++;
1133 	}
1134 	txkey = nwkey->i_defkid - 1;
1135 	if (txkey >= 0) {
1136 		if (txkey >= IEEE80211_WEP_NKID)
1137 			return EINVAL;
1138 		/* default key must have a valid value */
1139 		if (keys[txkey].an_wep_keylen == 0 ||
1140 		    (keys[txkey].an_wep_keylen < 0 &&
1141 		    sc->sc_perskeylen[txkey] == 0))
1142 			return EINVAL;
1143 		anysetkey++;
1144 	}
1145 	DPRINTF(("an_set_nwkey_wep: %s: %sold(%d:%d,%d,%d,%d) "
1146 	    "pers(%d:%d,%d,%d,%d) new(%d:%d,%d,%d,%d)\n",
1147 	    sc->sc_dev.dv_xname,
1148 	    ((nwkey->i_wepon & IEEE80211_NWKEY_PERSIST) ? "persist: " : ""),
1149 	    sc->sc_tx_key,
1150 	    sc->sc_wepkeys[0].an_wep_keylen, sc->sc_wepkeys[1].an_wep_keylen,
1151 	    sc->sc_wepkeys[2].an_wep_keylen, sc->sc_wepkeys[3].an_wep_keylen,
1152 	    sc->sc_tx_perskey,
1153 	    sc->sc_perskeylen[0], sc->sc_perskeylen[1],
1154 	    sc->sc_perskeylen[2], sc->sc_perskeylen[3],
1155 	    txkey,
1156 	    keys[0].an_wep_keylen, keys[1].an_wep_keylen,
1157 	    keys[2].an_wep_keylen, keys[3].an_wep_keylen));
1158 	if (!(nwkey->i_wepon & IEEE80211_NWKEY_PERSIST)) {
1159 		/* set temporary keys */
1160 		sc->sc_tx_key = txkey;
1161 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1162 			if (keys[i].an_wep_keylen < 0)
1163 				continue;
1164 			memcpy(&sc->sc_wepkeys[i], &keys[i], sizeof(keys[i]));
1165 		}
1166 	} else {
1167 		/* set persist keys */
1168 		if (anysetkey) {
1169 			/* prepare to write nvram */
1170 			if (!sc->sc_enabled) {
1171 				if (sc->sc_enable)
1172 					(*sc->sc_enable)(sc);
1173 				an_wait(sc);
1174 				sc->sc_enabled = 1;
1175 				error = an_write_wepkey(sc,
1176 				    AN_RID_WEP_PERSISTENT, keys, txkey);
1177 				if (sc->sc_disable)
1178 					(*sc->sc_disable)(sc);
1179 				sc->sc_enabled = 0;
1180 			} else {
1181 				an_cmd(sc, AN_CMD_DISABLE, 0);
1182 				error = an_write_wepkey(sc,
1183 				    AN_RID_WEP_PERSISTENT, keys, txkey);
1184 				an_cmd(sc, AN_CMD_ENABLE, 0);
1185 			}
1186 			if (error)
1187 				return error;
1188 		}
1189 		if (txkey >= 0)
1190 			sc->sc_tx_perskey = txkey;
1191 		if (sc->sc_tx_key >= 0) {
1192 			sc->sc_tx_key = -1;
1193 			needreset++;
1194 		}
1195 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1196 			if (sc->sc_wepkeys[i].an_wep_keylen >= 0) {
1197 				memset(&sc->sc_wepkeys[i].an_wep_key, 0,
1198 				    sizeof(sc->sc_wepkeys[i].an_wep_key));
1199 				sc->sc_wepkeys[i].an_wep_keylen = -1;
1200 				needreset++;
1201 			}
1202 			if (keys[i].an_wep_keylen >= 0)
1203 				sc->sc_perskeylen[i] = keys[i].an_wep_keylen;
1204 		}
1205 	}
1206 	if (needreset) {
1207 		/* firmware restart to reload persistent key */
1208 		an_reset(sc);
1209 	}
1210 	if (anysetkey || needreset)
1211 		error = ENETRESET;
1212 	return error;
1213 }
1214 
1215 static int
1216 an_set_nwkey_eap(struct an_softc *sc, struct ieee80211_nwkey *nwkey)
1217 {
1218 	int i, error, len;
1219 	struct ifnet *ifp = &sc->sc_if;
1220 	struct an_rid_leapkey *key;
1221 	u_int16_t unibuf[sizeof(key->an_key)];
1222 	static const int leap_rid[] = { AN_RID_LEAP_PASS, AN_RID_LEAP_USER };
1223 	MD4_CTX ctx;
1224 
1225 	error = 0;
1226 
1227 	if (nwkey->i_key[0].i_keydat == NULL &&
1228 	    nwkey->i_key[1].i_keydat == NULL)
1229 		return 0;
1230 	if (!sc->sc_enabled)
1231 		return ENXIO;
1232 	an_cmd(sc, AN_CMD_DISABLE, 0);
1233 	key = &sc->sc_buf.sc_leapkey;
1234 	for (i = 0; i < 2; i++) {
1235 		if (nwkey->i_key[i].i_keydat == NULL)
1236 			continue;
1237 		len = nwkey->i_key[i].i_keylen;
1238 		if (len > sizeof(key->an_key))
1239 			return EINVAL;
1240 		memset(key, 0, sizeof(*key));
1241 		key->an_key_len = htole16(len);
1242 		if ((error = copyin(nwkey->i_key[i].i_keydat, key->an_key,
1243 		    len)) != 0)
1244 			return error;
1245 		if (i == 1) {
1246 			/*
1247 			 * Cisco seems to use PasswordHash and PasswordHashHash
1248 			 * in RFC-2759 (MS-CHAP-V2).
1249 			 */
1250 			memset(unibuf, 0, sizeof(unibuf));
1251 			/* XXX: convert password to unicode */
1252 			for (i = 0; i < len; i++)
1253 				unibuf[i] = key->an_key[i];
1254 			/* set PasswordHash */
1255 			MD4Init(&ctx);
1256 			MD4Update(&ctx, (u_int8_t *)unibuf, len * 2);
1257 			MD4Final(key->an_key, &ctx);
1258 			/* set PasswordHashHash */
1259 			MD4Init(&ctx);
1260 			MD4Update(&ctx, key->an_key, 16);
1261 			MD4Final(key->an_key + 16, &ctx);
1262 			key->an_key_len = htole16(32);
1263 		}
1264 		if ((error = an_write_rid(sc, leap_rid[i], key,
1265 		    sizeof(*key))) != 0) {
1266 			printf("%s: LEAP set failed\n", ifp->if_xname);
1267 			return error;
1268 		}
1269 	}
1270 	error = an_cmd(sc, AN_CMD_ENABLE, 0);
1271 	if (error)
1272 		printf("%s: an_set_nwkey: failed to enable MAC\n",
1273 		    ifp->if_xname);
1274 	else
1275 		error = ENETRESET;
1276 	return error;
1277 }
1278 
1279 static int
1280 an_get_nwkey(struct an_softc *sc, struct ieee80211_nwkey *nwkey)
1281 {
1282 	int i, error;
1283 
1284 	error = 0;
1285 	if (sc->sc_config.an_authtype & AN_AUTHTYPE_LEAP)
1286 		nwkey->i_wepon = IEEE80211_NWKEY_EAP;
1287 	else if (sc->sc_config.an_authtype & AN_AUTHTYPE_PRIVACY_IN_USE)
1288 		nwkey->i_wepon = IEEE80211_NWKEY_WEP;
1289 	else
1290 		nwkey->i_wepon = IEEE80211_NWKEY_OPEN;
1291 	if (sc->sc_tx_key == -1)
1292 		nwkey->i_defkid = sc->sc_tx_perskey + 1;
1293 	else
1294 		nwkey->i_defkid = sc->sc_tx_key + 1;
1295 	if (nwkey->i_key[0].i_keydat == NULL)
1296 		return 0;
1297 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1298 		if (nwkey->i_key[i].i_keydat == NULL)
1299 			continue;
1300 		/* do not show any keys to non-root user */
1301 		if ((error = suser(curproc->p_ucred, &curproc->p_acflag)) != 0)
1302 			break;
1303 		nwkey->i_key[i].i_keylen = sc->sc_wepkeys[i].an_wep_keylen;
1304 		if (nwkey->i_key[i].i_keylen < 0) {
1305 			if (sc->sc_perskeylen[i] == 0)
1306 				nwkey->i_key[i].i_keylen = 0;
1307 			continue;
1308 		}
1309 		if ((error = copyout(sc->sc_wepkeys[i].an_wep_key,
1310 		    nwkey->i_key[i].i_keydat,
1311 		    sc->sc_wepkeys[i].an_wep_keylen)) != 0)
1312 			break;
1313 	}
1314 	return error;
1315 }
1316 
1317 static int
1318 an_write_wepkey(struct an_softc *sc, int type, struct an_wepkey *keys, int kid)
1319 {
1320 	int i, error;
1321 	struct an_rid_wepkey *akey;
1322 
1323 	error = 0;
1324 	akey = &sc->sc_buf.sc_wepkey;
1325 	memset(akey, 0, sizeof(struct an_rid_wepkey));
1326 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1327 		if (keys[i].an_wep_keylen < 0 ||
1328 		    keys[i].an_wep_keylen > sizeof(akey->an_key))
1329 			continue;
1330 		akey->an_key_len = htole16(keys[i].an_wep_keylen);
1331 		akey->an_key_index = htole16(i);
1332 		akey->an_mac_addr[0] = 1;	/* default mac */
1333 		memcpy(akey->an_key, keys[i].an_wep_key, keys[i].an_wep_keylen);
1334 		if ((error = an_write_rid(sc, type, akey, sizeof(*akey))) != 0)
1335 			return error;
1336 	}
1337 	if (kid >= 0) {
1338 		akey->an_key_index = htole16(0xffff);
1339 		akey->an_mac_addr[0] = kid;
1340 		akey->an_key_len = htole16(0);
1341 		memset(akey->an_key, 0, sizeof(akey->an_key));
1342 		error = an_write_rid(sc, type, akey, sizeof(*akey));
1343 	}
1344 	return error;
1345 }
1346 
1347 #ifdef AN_DEBUG
1348 static void
1349 an_dump_pkt(const char *devname, struct mbuf *m)
1350 {
1351 	int col, col0, i;
1352 	uint8_t *pkt = mtod(m, uint8_t *);
1353 	const char *delim = "";
1354 	int delimw = 0;
1355 
1356 	printf("%s: pkt ", devname);
1357 	col = col0 = strlen(devname) + strlen(": pkt ");
1358 	for (i = 0; i < m->m_len; i++) {
1359 		printf("%s%02x", delim, pkt[i]);
1360 		delim = ":";
1361 		delimw = 1;
1362 		col += delimw + 2;
1363 		if (col >= 72) {
1364 			printf("\n%*s", col0, "");
1365 			col = col0;
1366 			delim = "";
1367 			delimw = 0;
1368 		}
1369 	}
1370 	if (col != 0)
1371 		printf("\n");
1372 }
1373 #endif /* AN_DEBUG */
1374 
1375 /*
1376  * Low level functions
1377  */
1378 
1379 static void
1380 an_rx_intr(struct an_softc *sc)
1381 {
1382 	struct ieee80211com *ic = &sc->sc_ic;
1383 	struct ifnet *ifp = &sc->sc_if;
1384 	struct ieee80211_frame_min *wh;
1385 	struct ieee80211_node *ni;
1386 	struct an_rxframe frmhdr;
1387 	struct mbuf *m;
1388 	u_int16_t status;
1389 	int fid, gaplen, len, off;
1390 	uint8_t *gap;
1391 
1392 	fid = CSR_READ_2(sc, AN_RX_FID);
1393 
1394 	/* First read in the frame header */
1395 	if (an_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0) {
1396 		CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
1397 		ifp->if_ierrors++;
1398 		DPRINTF(("an_rx_intr: read fid %x failed\n", fid));
1399 		return;
1400 	}
1401 
1402 #ifdef AN_DEBUG
1403 	if ((ifp->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2)) {
1404 		ieee80211_dump_pkt((u_int8_t *)&frmhdr.an_whdr,
1405 		    sizeof(struct ieee80211_frame), frmhdr.an_rx_rate,
1406 		    frmhdr.an_rx_signal_strength);
1407 		printf(" time 0x%x status 0x%x plen %u chan %u"
1408 		    " plcp %02x %02x %02x %02x gap %u\n",
1409 		    le32toh(frmhdr.an_rx_time), le16toh(frmhdr.an_rx_status),
1410 		    le16toh(frmhdr.an_rx_payload_len), frmhdr.an_rx_chan,
1411 		    frmhdr.an_plcp_hdr[0], frmhdr.an_plcp_hdr[1],
1412 		    frmhdr.an_plcp_hdr[2], frmhdr.an_plcp_hdr[3],
1413 		    le16toh(frmhdr.an_gaplen));
1414 	}
1415 #endif
1416 
1417 	status = le16toh(frmhdr.an_rx_status);
1418 	if ((status & AN_STAT_ERRSTAT) != 0 &&
1419 	    ic->ic_opmode != IEEE80211_M_MONITOR) {
1420 		CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
1421 		ifp->if_ierrors++;
1422 		DPRINTF(("an_rx_intr: fid %x status %x\n", fid, status));
1423 		return;
1424 	}
1425 
1426 	/* the payload length field includes a 16-bit "mystery field" */
1427 	len = le16toh(frmhdr.an_rx_payload_len) - sizeof(uint16_t);
1428 	off = ALIGN(sizeof(struct ieee80211_frame));
1429 
1430 	if (off + len > MCLBYTES) {
1431 		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1432 			CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
1433 			ifp->if_ierrors++;
1434 			DPRINTF(("an_rx_intr: oversized packet %d\n", len));
1435 			return;
1436 		}
1437 		len = 0;
1438 	}
1439 
1440 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1441 	if (m == NULL) {
1442 		CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
1443 		ifp->if_ierrors++;
1444 		DPRINTF(("an_rx_intr: MGET failed\n"));
1445 		return;
1446 	}
1447 	if (off + len + AN_GAPLEN_MAX > MHLEN) {
1448 		MCLGET(m, M_DONTWAIT);
1449 		if ((m->m_flags & M_EXT) == 0) {
1450 			CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
1451 			m_freem(m);
1452 			ifp->if_ierrors++;
1453 			DPRINTF(("an_rx_intr: MCLGET failed\n"));
1454 			return;
1455 		}
1456 	}
1457 	m->m_data += off - sizeof(struct ieee80211_frame);
1458 
1459 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1460 		gaplen = le16toh(frmhdr.an_gaplen);
1461 		if (gaplen > AN_GAPLEN_MAX) {
1462 			CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
1463 			m_freem(m);
1464 			ifp->if_ierrors++;
1465 			DPRINTF(("%s: gap too long\n", __func__));
1466 			return;
1467 		}
1468 		/*
1469 		 * We don't need the 16-bit mystery field (payload length?),
1470 		 * so read it into the region reserved for the 802.11 header.
1471 		 *
1472 		 * When Cisco Aironet 350 cards w/ firmware version 5 or
1473 		 * greater operate with certain Cisco 350 APs,
1474 		 * the "gap" is filled with the SNAP header.  Read
1475 		 * it in after the 802.11 header.
1476 		 */
1477 		gap = m->m_data + sizeof(struct ieee80211_frame) -
1478 		    sizeof(uint16_t);
1479 		an_read_bap(sc, fid, -1, gap, gaplen + sizeof(u_int16_t));
1480 #ifdef AN_DEBUG
1481 		if ((ifp->if_flags & (IFF_DEBUG|IFF_LINK2)) ==
1482 		    (IFF_DEBUG|IFF_LINK2)) {
1483 			int i;
1484 			printf(" gap&len");
1485 			for (i = 0; i < gaplen + sizeof(u_int16_t); i++)
1486 				printf(" %02x", gap[i]);
1487 			printf("\n");
1488 		}
1489 #endif
1490 	} else
1491 		gaplen = 0;
1492 
1493 	an_read_bap(sc, fid, -1,
1494 	    m->m_data + sizeof(struct ieee80211_frame) + gaplen, len);
1495 	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + gaplen +
1496 	    len;
1497 
1498 	memcpy(m->m_data, &frmhdr.an_whdr, sizeof(struct ieee80211_frame));
1499 	m->m_pkthdr.rcvif = ifp;
1500 	CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
1501 
1502 #if NBPFILTER > 0
1503 	if (sc->sc_drvbpf) {
1504 		struct an_rx_radiotap_header *tap = &sc->sc_rxtap;
1505 
1506 		tap->ar_rate = frmhdr.an_rx_rate;
1507 		tap->ar_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
1508 		tap->ar_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
1509 		tap->ar_antsignal = frmhdr.an_rx_signal_strength;
1510 		if ((le16toh(frmhdr.an_rx_status) & AN_STAT_BADCRC) ||
1511 		    (le16toh(frmhdr.an_rx_status) & AN_STAT_ERRSTAT) ||
1512 		    (le16toh(frmhdr.an_rx_status) & AN_STAT_UNDECRYPTABLE))
1513 		    tap->ar_flags |= IEEE80211_RADIOTAP_F_BADFCS;
1514 
1515 		bpf_mtap2(sc->sc_drvbpf, tap, tap->ar_ihdr.it_len, m);
1516 	}
1517 #endif
1518 	wh = mtod(m, struct ieee80211_frame_min *);
1519 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1520 		/*
1521 		 * WEP is decrypted by hardware. Clear WEP bit
1522 		 * header for ieee80211_input().
1523 		 */
1524 		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1525 	}
1526 
1527 #ifdef AN_DEBUG
1528 	if (an_debug > 1)
1529 		an_dump_pkt(sc->sc_dev.dv_xname, m);
1530 #endif /* AN_DEBUG */
1531 
1532 	ni = ieee80211_find_rxnode(ic, wh);
1533 	ieee80211_input(ic, m, ni, frmhdr.an_rx_signal_strength,
1534 	    le32toh(frmhdr.an_rx_time));
1535 	ieee80211_free_node(ni);
1536 }
1537 
1538 static void
1539 an_tx_intr(struct an_softc *sc, int status)
1540 {
1541 	struct ifnet *ifp = &sc->sc_if;
1542 	int cur, fid;
1543 
1544 	sc->sc_tx_timer = 0;
1545 	ifp->if_flags &= ~IFF_OACTIVE;
1546 
1547 	fid = CSR_READ_2(sc, AN_TX_CMP_FID);
1548 	CSR_WRITE_2(sc, AN_EVENT_ACK, status & (AN_EV_TX | AN_EV_TX_EXC));
1549 
1550 	if (status & AN_EV_TX_EXC)
1551 		ifp->if_oerrors++;
1552 	else
1553 		ifp->if_opackets++;
1554 
1555 	cur = sc->sc_txcur;
1556 	if (sc->sc_txd[cur].d_fid == fid) {
1557 		sc->sc_txd[cur].d_inuse = 0;
1558 		DPRINTF2(("an_tx_intr: sent %x/%d\n", fid, cur));
1559 		AN_INC(cur, AN_TX_RING_CNT);
1560 		sc->sc_txcur = cur;
1561 	} else {
1562 		for (cur = 0; cur < AN_TX_RING_CNT; cur++) {
1563 			if (fid == sc->sc_txd[cur].d_fid) {
1564 				sc->sc_txd[cur].d_inuse = 0;
1565 				break;
1566 			}
1567 		}
1568 		if (ifp->if_flags & IFF_DEBUG)
1569 			printf("%s: tx mismatch: "
1570 			    "expected %x(%d), actual %x(%d)\n",
1571 			    sc->sc_dev.dv_xname,
1572 			    sc->sc_txd[sc->sc_txcur].d_fid, sc->sc_txcur,
1573 			    fid, cur);
1574 	}
1575 
1576 	return;
1577 }
1578 
1579 static void
1580 an_linkstat_intr(struct an_softc *sc)
1581 {
1582 	struct ieee80211com *ic = &sc->sc_ic;
1583 	u_int16_t status;
1584 
1585 	status = CSR_READ_2(sc, AN_LINKSTAT);
1586 	CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_LINKSTAT);
1587 	DPRINTF(("an_linkstat_intr: status 0x%x\n", status));
1588 
1589 	if (status == AN_LINKSTAT_ASSOCIATED) {
1590 		if (ic->ic_state != IEEE80211_S_RUN ||
1591 		    ic->ic_opmode == IEEE80211_M_IBSS)
1592 			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1593 	} else {
1594 		if (ic->ic_opmode == IEEE80211_M_STA)
1595 			ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1596 	}
1597 }
1598 
1599 /* Must be called at proper protection level! */
1600 static int
1601 an_cmd(struct an_softc *sc, int cmd, int val)
1602 {
1603 	int i, status;
1604 
1605 	/* make sure that previous command completed */
1606 	if (CSR_READ_2(sc, AN_COMMAND) & AN_CMD_BUSY) {
1607 		if (sc->sc_if.if_flags & IFF_DEBUG)
1608 			printf("%s: command 0x%x busy\n", sc->sc_dev.dv_xname,
1609 			    CSR_READ_2(sc, AN_COMMAND));
1610 		CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CLR_STUCK_BUSY);
1611 	}
1612 
1613 	CSR_WRITE_2(sc, AN_PARAM0, val);
1614 	CSR_WRITE_2(sc, AN_PARAM1, 0);
1615 	CSR_WRITE_2(sc, AN_PARAM2, 0);
1616 	CSR_WRITE_2(sc, AN_COMMAND, cmd);
1617 
1618 	if (cmd == AN_CMD_FW_RESTART) {
1619 		/* XXX: should sleep here */
1620 		DELAY(100*1000);
1621 	}
1622 
1623 	for (i = 0; i < AN_TIMEOUT; i++) {
1624 		if (CSR_READ_2(sc, AN_EVENT_STAT) & AN_EV_CMD)
1625 			break;
1626 		DELAY(10);
1627 	}
1628 
1629 	status = CSR_READ_2(sc, AN_STATUS);
1630 
1631 	/* clear stuck command busy if necessary */
1632 	if (CSR_READ_2(sc, AN_COMMAND) & AN_CMD_BUSY)
1633 		CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CLR_STUCK_BUSY);
1634 
1635 	/* Ack the command */
1636 	CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CMD);
1637 
1638 	if (i == AN_TIMEOUT) {
1639 		if (sc->sc_if.if_flags & IFF_DEBUG)
1640 			printf("%s: command 0x%x param 0x%x timeout\n",
1641 			    sc->sc_dev.dv_xname, cmd, val);
1642 		return ETIMEDOUT;
1643 	}
1644 	if (status & AN_STAT_CMD_RESULT) {
1645 		if (sc->sc_if.if_flags & IFF_DEBUG)
1646 			printf("%s: command 0x%x param 0x%x status 0x%x "
1647 			    "resp 0x%x 0x%x 0x%x\n",
1648 			    sc->sc_dev.dv_xname, cmd, val, status,
1649 			    CSR_READ_2(sc, AN_RESP0), CSR_READ_2(sc, AN_RESP1),
1650 			    CSR_READ_2(sc, AN_RESP2));
1651 		return EIO;
1652 	}
1653 
1654 	return 0;
1655 }
1656 
1657 
1658 /*
1659  * Wait for firmware come up after power enabled.
1660  */
1661 static void
1662 an_wait(struct an_softc *sc)
1663 {
1664 	int i;
1665 
1666 	CSR_WRITE_2(sc, AN_COMMAND, AN_CMD_NOOP2);
1667 	for (i = 0; i < 3*hz; i++) {
1668 		if (CSR_READ_2(sc, AN_EVENT_STAT) & AN_EV_CMD)
1669 			break;
1670 		(void)tsleep(sc, PWAIT, "anatch", 1);
1671 	}
1672 	CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CMD);
1673 }
1674 
1675 static int
1676 an_seek_bap(struct an_softc *sc, int id, int off)
1677 {
1678 	int i, status;
1679 
1680 	CSR_WRITE_2(sc, AN_SEL0, id);
1681 	CSR_WRITE_2(sc, AN_OFF0, off);
1682 
1683 	for (i = 0; ; i++) {
1684 		status = CSR_READ_2(sc, AN_OFF0);
1685 		if ((status & AN_OFF_BUSY) == 0)
1686 			break;
1687 		if (i == AN_TIMEOUT) {
1688 			printf("%s: timeout in an_seek_bap to 0x%x/0x%x\n",
1689 			    sc->sc_dev.dv_xname, id, off);
1690 			sc->sc_bap_off = AN_OFF_ERR;	/* invalidate */
1691 			return ETIMEDOUT;
1692 		}
1693 		DELAY(10);
1694 	}
1695 	if (status & AN_OFF_ERR) {
1696 		printf("%s: failed in an_seek_bap to 0x%x/0x%x\n",
1697 		    sc->sc_dev.dv_xname, id, off);
1698 		sc->sc_bap_off = AN_OFF_ERR;	/* invalidate */
1699 		return EIO;
1700 	}
1701 	sc->sc_bap_id = id;
1702 	sc->sc_bap_off = off;
1703 	return 0;
1704 }
1705 
1706 static int
1707 an_read_bap(struct an_softc *sc, int id, int off, void *buf, int buflen)
1708 {
1709 	int error, cnt;
1710 
1711 	if (buflen == 0)
1712 		return 0;
1713 	if (off == -1)
1714 		off = sc->sc_bap_off;
1715 	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1716 		if ((error = an_seek_bap(sc, id, off)) != 0)
1717 			return EIO;
1718 	}
1719 
1720 	cnt = (buflen + 1) / 2;
1721 	CSR_READ_MULTI_STREAM_2(sc, AN_DATA0, (u_int16_t *)buf, cnt);
1722 	sc->sc_bap_off += cnt * 2;
1723 	return 0;
1724 }
1725 
1726 static int
1727 an_write_bap(struct an_softc *sc, int id, int off, void *buf, int buflen)
1728 {
1729 	int error, cnt;
1730 
1731 	if (buflen == 0)
1732 		return 0;
1733 	if (off == -1)
1734 		off = sc->sc_bap_off;
1735 	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1736 		if ((error = an_seek_bap(sc, id, off)) != 0)
1737 			return EIO;
1738 	}
1739 
1740 	cnt = (buflen + 1) / 2;
1741 	CSR_WRITE_MULTI_STREAM_2(sc, AN_DATA0, (u_int16_t *)buf, cnt);
1742 	sc->sc_bap_off += cnt * 2;
1743 	return 0;
1744 }
1745 
1746 static int
1747 an_mwrite_bap(struct an_softc *sc, int id, int off, struct mbuf *m, int totlen)
1748 {
1749 	int error, len, cnt;
1750 
1751 	if (off == -1)
1752 		off = sc->sc_bap_off;
1753 	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1754 		if ((error = an_seek_bap(sc, id, off)) != 0)
1755 			return EIO;
1756 	}
1757 
1758 	for (len = 0; m != NULL; m = m->m_next) {
1759 		if (m->m_len == 0)
1760 			continue;
1761 		len = min(m->m_len, totlen);
1762 
1763 		if ((mtod(m, u_long) & 0x1) || (len & 0x1)) {
1764 			m_copydata(m, 0, totlen, (caddr_t)&sc->sc_buf.sc_txbuf);
1765 			cnt = (totlen + 1) / 2;
1766 			CSR_WRITE_MULTI_STREAM_2(sc, AN_DATA0,
1767 			    sc->sc_buf.sc_val, cnt);
1768 			off += cnt * 2;
1769 			break;
1770 		}
1771 		cnt = len / 2;
1772 		CSR_WRITE_MULTI_STREAM_2(sc, AN_DATA0, mtod(m, u_int16_t *),
1773 		    cnt);
1774 		off += len;
1775 		totlen -= len;
1776 	}
1777 	sc->sc_bap_off = off;
1778 	return 0;
1779 }
1780 
1781 static int
1782 an_alloc_fid(struct an_softc *sc, int len, int *idp)
1783 {
1784 	int i;
1785 
1786 	if (an_cmd(sc, AN_CMD_ALLOC_MEM, len)) {
1787 		printf("%s: failed to allocate %d bytes on NIC\n",
1788 		    sc->sc_dev.dv_xname, len);
1789 		return ENOMEM;
1790 	}
1791 
1792 	for (i = 0; i < AN_TIMEOUT; i++) {
1793 		if (CSR_READ_2(sc, AN_EVENT_STAT) & AN_EV_ALLOC)
1794 			break;
1795 		if (i == AN_TIMEOUT) {
1796 			printf("%s: timeout in alloc\n", sc->sc_dev.dv_xname);
1797 			return ETIMEDOUT;
1798 		}
1799 		DELAY(10);
1800 	}
1801 
1802 	*idp = CSR_READ_2(sc, AN_ALLOC_FID);
1803 	CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_ALLOC);
1804 	return 0;
1805 }
1806 
1807 static int
1808 an_read_rid(struct an_softc *sc, int rid, void *buf, int *buflenp)
1809 {
1810 	int error;
1811 	u_int16_t len;
1812 
1813 	/* Tell the NIC to enter record read mode. */
1814 	error = an_cmd(sc, AN_CMD_ACCESS | AN_ACCESS_READ, rid);
1815 	if (error)
1816 		return error;
1817 
1818 	/* length in byte, including length itself */
1819 	error = an_read_bap(sc, rid, 0, &len, sizeof(len));
1820 	if (error)
1821 		return error;
1822 
1823 	len = le16toh(len) - 2;
1824 	if (*buflenp < len) {
1825 		printf("%s: record buffer is too small, "
1826 		    "rid=%x, size=%d, len=%d\n",
1827 		    sc->sc_dev.dv_xname, rid, *buflenp, len);
1828 		return ENOSPC;
1829 	}
1830 	*buflenp = len;
1831 	return an_read_bap(sc, rid, sizeof(len), buf, len);
1832 }
1833 
1834 static int
1835 an_write_rid(struct an_softc *sc, int rid, void *buf, int buflen)
1836 {
1837 	int error;
1838 	u_int16_t len;
1839 
1840 	/* length in byte, including length itself */
1841 	len = htole16(buflen + 2);
1842 
1843 	error = an_write_bap(sc, rid, 0, &len, sizeof(len));
1844 	if (error)
1845 		return error;
1846 	error = an_write_bap(sc, rid, sizeof(len), buf, buflen);
1847 	if (error)
1848 		return error;
1849 
1850 	return an_cmd(sc, AN_CMD_ACCESS | AN_ACCESS_WRITE, rid);
1851 }
1852 
1853 static int
1854 an_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
1855 {
1856 	struct an_softc *sc = (struct an_softc *)ic->ic_ifp->if_softc;
1857 	struct ieee80211_node *ni = ic->ic_bss;
1858 	enum ieee80211_state ostate;
1859 	int buflen;
1860 
1861 	ostate = ic->ic_state;
1862 	DPRINTF(("an_newstate: %s -> %s\n", ieee80211_state_name[ostate],
1863 	    ieee80211_state_name[nstate]));
1864 
1865 	switch (nstate) {
1866 	case IEEE80211_S_INIT:
1867 		ic->ic_flags &= ~IEEE80211_F_IBSSON;
1868 		return (*sc->sc_newstate)(ic, nstate, arg);
1869 
1870 	case IEEE80211_S_SCAN:
1871 	case IEEE80211_S_AUTH:
1872 	case IEEE80211_S_ASSOC:
1873 		ic->ic_state = nstate; /* NB: skip normal ieee80211 handling */
1874 		return 0;
1875 
1876 	case IEEE80211_S_RUN:
1877 		buflen = sizeof(sc->sc_buf);
1878 		an_read_rid(sc, AN_RID_STATUS, &sc->sc_buf, &buflen);
1879 		IEEE80211_ADDR_COPY(ni->ni_bssid,
1880 		    sc->sc_buf.sc_status.an_cur_bssid);
1881 		IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid);
1882 		ni->ni_chan = &ic->ic_channels[
1883 		    le16toh(sc->sc_buf.sc_status.an_cur_channel)];
1884 		ni->ni_esslen = le16toh(sc->sc_buf.sc_status.an_ssidlen);
1885 		if (ni->ni_esslen > IEEE80211_NWID_LEN)
1886 			ni->ni_esslen = IEEE80211_NWID_LEN;	/*XXX*/
1887 		memcpy(ni->ni_essid, sc->sc_buf.sc_status.an_ssid,
1888 		    ni->ni_esslen);
1889 		ni->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11B];	/*XXX*/
1890 		if (ic->ic_ifp->if_flags & IFF_DEBUG) {
1891 			printf("%s: ", sc->sc_dev.dv_xname);
1892 			if (ic->ic_opmode == IEEE80211_M_STA)
1893 				printf("associated ");
1894 			else
1895 				printf("synchronized ");
1896 			printf("with %s ssid ", ether_sprintf(ni->ni_bssid));
1897 			ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
1898 			printf(" channel %u start %uMb\n",
1899 			    le16toh(sc->sc_buf.sc_status.an_cur_channel),
1900 			    le16toh(sc->sc_buf.sc_status.an_current_tx_rate)/2);
1901 		}
1902 		break;
1903 
1904 	default:
1905 		break;
1906 	}
1907 	return (*sc->sc_newstate)(ic, nstate, arg);
1908 }
1909