xref: /netbsd-src/sys/dev/ic/an.c (revision eb961d0e02b7a46a9acfa877b02df48df6637278)
1 /*	$NetBSD: an.c,v 1.40 2006/02/20 16:50:37 thorpej 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.40 2006/02/20 16:50:37 thorpej 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 = sizeof(sc->sc_rxtapu);
333 	sc->sc_rxtap.ar_ihdr.it_present = AN_RX_RADIOTAP_PRESENT;
334 
335 	memset(&sc->sc_txtapu, 0, sizeof(sc->sc_txtapu));
336 	sc->sc_txtap.at_ihdr.it_len = sizeof(sc->sc_txtapu);
337 	sc->sc_txtap.at_ihdr.it_present = 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 = frmhdr.an_tx_rate;
823 			tap->at_rate = ic->ic_bss->ni_rates.rs_rates[ic->ic_bss->ni_txrate];
824 			tap->at_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
825 			tap->at_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
826                         /* TBD tap->wt_flags */
827                         bpf_mtap2(sc->sc_drvbpf, tap, tap->at_ihdr.it_len, m);
828                 }
829 #endif
830 
831 #ifdef AN_DEBUG
832 		if ((ifp->if_flags & (IFF_DEBUG|IFF_LINK2)) ==
833 		    (IFF_DEBUG|IFF_LINK2)) {
834 			ieee80211_dump_pkt((u_int8_t *)&frmhdr.an_whdr,
835 			    sizeof(struct ieee80211_frame), -1, 0);
836 			printf(" txctl 0x%x plen %u\n",
837 			    le16toh(frmhdr.an_tx_ctl),
838 			    le16toh(frmhdr.an_tx_payload_len));
839 		}
840 #endif
841 		if (sizeof(frmhdr) + AN_TXGAP_802_11 + sizeof(len) +
842 		    m->m_pkthdr.len > AN_TX_MAX_LEN)
843 			goto bad;
844 
845 		fid = sc->sc_txd[cur].d_fid;
846 		if (an_write_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0)
847 			goto bad;
848 		/* dummy write to avoid seek. */
849 		an_write_bap(sc, fid, -1, &frmhdr, AN_TXGAP_802_11);
850 		an_mwrite_bap(sc, fid, -1, m, m->m_pkthdr.len);
851 		m_freem(m);
852 
853 		DPRINTF2(("an_start: send %d byte via %x/%d\n",
854 		    ntohs(len) + sizeof(struct ieee80211_frame),
855 		    fid, cur));
856 		sc->sc_txd[cur].d_inuse = 1;
857 		if (an_cmd(sc, AN_CMD_TX, fid)) {
858 			printf("%s: xmit failed\n", ifp->if_xname);
859 			sc->sc_txd[cur].d_inuse = 0;
860 			continue;
861 		}
862 		sc->sc_tx_timer = 5;
863 		ifp->if_timer = 1;
864 		AN_INC(cur, AN_TX_RING_CNT);
865 		sc->sc_txnext = cur;
866 		continue;
867 bad:
868 		ifp->if_oerrors++;
869 		m_freem(m);
870 	}
871 }
872 
873 static int
874 an_reset(struct an_softc *sc)
875 {
876 
877 	DPRINTF(("an_reset\n"));
878 
879 	if (!sc->sc_enabled)
880 		return ENXIO;
881 
882 	an_cmd(sc, AN_CMD_ENABLE, 0);
883 	an_cmd(sc, AN_CMD_FW_RESTART, 0);
884 	an_cmd(sc, AN_CMD_NOOP2, 0);
885 
886 	if (an_cmd(sc, AN_CMD_FORCE_SYNCLOSS, 0) == ETIMEDOUT) {
887 		printf("%s: reset failed\n", sc->sc_dev.dv_xname);
888 		return ETIMEDOUT;
889 	}
890 
891 	an_cmd(sc, AN_CMD_DISABLE, 0);
892 	return 0;
893 }
894 
895 static void
896 an_watchdog(struct ifnet *ifp)
897 {
898 	struct an_softc *sc = ifp->if_softc;
899 
900 	if (!sc->sc_enabled)
901 		return;
902 
903 	if (sc->sc_tx_timer) {
904 		if (--sc->sc_tx_timer == 0) {
905 			printf("%s: device timeout\n", ifp->if_xname);
906 			ifp->if_oerrors++;
907 			an_init(ifp);
908 			return;
909 		}
910 		ifp->if_timer = 1;
911 	}
912 	ieee80211_watchdog(&sc->sc_ic);
913 }
914 
915 static int
916 an_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
917 {
918 	struct an_softc *sc = ifp->if_softc;
919 	int s, error = 0;
920 
921 	if (!device_is_active(&sc->sc_dev))
922 		return ENXIO;
923 
924 	s = splnet();
925 
926 	switch (command) {
927 	case SIOCSIFFLAGS:
928 		if (ifp->if_flags & IFF_UP) {
929 			if (sc->sc_enabled) {
930 				/*
931 				 * To avoid rescanning another access point,
932 				 * do not call an_init() here.  Instead, only
933 				 * reflect promisc mode settings.
934 				 */
935 				error = an_cmd(sc, AN_CMD_SET_MODE,
936 				    (ifp->if_flags & IFF_PROMISC) ? 0xffff : 0);
937 			} else
938 				error = an_init(ifp);
939 		} else if (sc->sc_enabled)
940 			an_stop(ifp, 1);
941 		break;
942 	case SIOCADDMULTI:
943 	case SIOCDELMULTI:
944 		error = ether_ioctl(ifp, command, data);
945 		if (error == ENETRESET) {
946 			/* we don't have multicast filter. */
947 			error = 0;
948 		}
949 		break;
950 	case SIOCS80211NWKEY:
951 		error = an_set_nwkey(sc, (struct ieee80211_nwkey *)data);
952 			break;
953 	case SIOCG80211NWKEY:
954 		error = an_get_nwkey(sc, (struct ieee80211_nwkey *)data);
955 		break;
956 	default:
957 		error = ieee80211_ioctl(&sc->sc_ic, command, data);
958 		break;
959 	}
960 	if (error == ENETRESET) {
961 		if (sc->sc_enabled)
962 			error = an_init(ifp);
963 		else
964 			error = 0;
965 	}
966 	splx(s);
967 	return error;
968 }
969 
970 /* TBD factor with ieee80211_media_change */
971 static int
972 an_media_change(struct ifnet *ifp)
973 {
974 	struct an_softc *sc = ifp->if_softc;
975 	struct ieee80211com *ic = &sc->sc_ic;
976 	struct ifmedia_entry *ime;
977 	enum ieee80211_opmode newmode;
978 	int i, rate, error = 0;
979 
980 	ime = ic->ic_media.ifm_cur;
981 	if (IFM_SUBTYPE(ime->ifm_media) == IFM_AUTO) {
982 		i = -1;
983 	} else {
984 		struct ieee80211_rateset *rs =
985 		    &ic->ic_sup_rates[IEEE80211_MODE_11B];
986 		rate = ieee80211_media2rate(ime->ifm_media);
987 		if (rate == 0)
988 			return EINVAL;
989 		for (i = 0; i < rs->rs_nrates; i++) {
990 			if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == rate)
991 				break;
992 		}
993 		if (i == rs->rs_nrates)
994 			return EINVAL;
995 	}
996 	if (ic->ic_fixed_rate != i) {
997 		ic->ic_fixed_rate = i;
998 		error = ENETRESET;
999 	}
1000 
1001 	if (ime->ifm_media & IFM_IEEE80211_ADHOC)
1002 		newmode = IEEE80211_M_IBSS;
1003 	else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
1004 		newmode = IEEE80211_M_HOSTAP;
1005 	else if (ime->ifm_media & IFM_IEEE80211_MONITOR)
1006 		newmode = IEEE80211_M_MONITOR;
1007 	else
1008 		newmode = IEEE80211_M_STA;
1009 	if (ic->ic_opmode != newmode) {
1010 		ic->ic_opmode = newmode;
1011 		error = ENETRESET;
1012 	}
1013 	if (error == ENETRESET) {
1014 		if (sc->sc_enabled)
1015 			error = an_init(ifp);
1016 		else
1017 			error = 0;
1018 	}
1019 	ifp->if_baudrate = ifmedia_baudrate(ic->ic_media.ifm_cur->ifm_media);
1020 
1021 	return error;
1022 }
1023 
1024 static void
1025 an_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1026 {
1027 	struct an_softc *sc = ifp->if_softc;
1028 	struct ieee80211com *ic = &sc->sc_ic;
1029 	int rate, buflen;
1030 
1031 	if (sc->sc_enabled == 0) {
1032 		imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
1033 		imr->ifm_status = 0;
1034 		return;
1035 	}
1036 
1037 	imr->ifm_status = IFM_AVALID;
1038 	imr->ifm_active = IFM_IEEE80211;
1039 	if (ic->ic_state == IEEE80211_S_RUN)
1040 		imr->ifm_status |= IFM_ACTIVE;
1041 	buflen = sizeof(sc->sc_buf);
1042 	if (ic->ic_fixed_rate != -1)
1043 		rate = ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[
1044 		    ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
1045 	else if (an_read_rid(sc, AN_RID_STATUS, &sc->sc_buf, &buflen) != 0)
1046 		rate = 0;
1047 	else
1048 		rate = le16toh(sc->sc_buf.sc_status.an_current_tx_rate);
1049 	imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
1050 	switch (ic->ic_opmode) {
1051 	case IEEE80211_M_STA:
1052 		break;
1053 	case IEEE80211_M_IBSS:
1054 		imr->ifm_active |= IFM_IEEE80211_ADHOC;
1055 		break;
1056 	case IEEE80211_M_HOSTAP:
1057 		imr->ifm_active |= IFM_IEEE80211_HOSTAP;
1058 		break;
1059 	case IEEE80211_M_MONITOR:
1060 		imr->ifm_active |= IFM_IEEE80211_MONITOR;
1061 		break;
1062 	default:
1063 		break;
1064 	}
1065 }
1066 
1067 static int
1068 an_set_nwkey(struct an_softc *sc, struct ieee80211_nwkey *nwkey)
1069 {
1070 	int error;
1071 	struct ieee80211com *ic = &sc->sc_ic;
1072 	u_int16_t prevauth;
1073 
1074 	error = 0;
1075 	prevauth = sc->sc_config.an_authtype;
1076 
1077 	switch (nwkey->i_wepon) {
1078 	case IEEE80211_NWKEY_OPEN:
1079 		sc->sc_config.an_authtype = AN_AUTHTYPE_OPEN;
1080 		ic->ic_flags &= ~IEEE80211_F_PRIVACY;
1081 		break;
1082 
1083 	case IEEE80211_NWKEY_WEP:
1084 	case IEEE80211_NWKEY_WEP | IEEE80211_NWKEY_PERSIST:
1085 		error = an_set_nwkey_wep(sc, nwkey);
1086 		if (error == 0 || error == ENETRESET) {
1087 			sc->sc_config.an_authtype =
1088 			    AN_AUTHTYPE_OPEN | AN_AUTHTYPE_PRIVACY_IN_USE;
1089 			ic->ic_flags |= IEEE80211_F_PRIVACY;
1090 		}
1091 		break;
1092 
1093 	case IEEE80211_NWKEY_EAP:
1094 		error = an_set_nwkey_eap(sc, nwkey);
1095 		if (error == 0 || error == ENETRESET) {
1096 			sc->sc_config.an_authtype = AN_AUTHTYPE_OPEN |
1097 			    AN_AUTHTYPE_PRIVACY_IN_USE | AN_AUTHTYPE_LEAP;
1098 			ic->ic_flags |= IEEE80211_F_PRIVACY;
1099 		}
1100 		break;
1101 	default:
1102 		error = EINVAL;
1103 		break;
1104 	}
1105 	if (error == 0 && prevauth != sc->sc_config.an_authtype)
1106 		error = ENETRESET;
1107 	return error;
1108 }
1109 
1110 static int
1111 an_set_nwkey_wep(struct an_softc *sc, struct ieee80211_nwkey *nwkey)
1112 {
1113 	int i, txkey, anysetkey, needreset, error;
1114 	struct an_wepkey keys[IEEE80211_WEP_NKID];
1115 
1116 	error = 0;
1117 	memset(keys, 0, sizeof(keys));
1118 	anysetkey = needreset = 0;
1119 
1120 	/* load argument and sanity check */
1121 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1122 		keys[i].an_wep_keylen = nwkey->i_key[i].i_keylen;
1123 		if (keys[i].an_wep_keylen < 0)
1124 			continue;
1125 		if (keys[i].an_wep_keylen != 0 &&
1126 		    keys[i].an_wep_keylen < IEEE80211_WEP_KEYLEN)
1127 			return EINVAL;
1128 		if (keys[i].an_wep_keylen > sizeof(keys[i].an_wep_key))
1129 			return EINVAL;
1130 		if ((error = copyin(nwkey->i_key[i].i_keydat,
1131 		    keys[i].an_wep_key, keys[i].an_wep_keylen)) != 0)
1132 			return error;
1133 		anysetkey++;
1134 	}
1135 	txkey = nwkey->i_defkid - 1;
1136 	if (txkey >= 0) {
1137 		if (txkey >= IEEE80211_WEP_NKID)
1138 			return EINVAL;
1139 		/* default key must have a valid value */
1140 		if (keys[txkey].an_wep_keylen == 0 ||
1141 		    (keys[txkey].an_wep_keylen < 0 &&
1142 		    sc->sc_perskeylen[txkey] == 0))
1143 			return EINVAL;
1144 		anysetkey++;
1145 	}
1146 	DPRINTF(("an_set_nwkey_wep: %s: %sold(%d:%d,%d,%d,%d) "
1147 	    "pers(%d:%d,%d,%d,%d) new(%d:%d,%d,%d,%d)\n",
1148 	    sc->sc_dev.dv_xname,
1149 	    ((nwkey->i_wepon & IEEE80211_NWKEY_PERSIST) ? "persist: " : ""),
1150 	    sc->sc_tx_key,
1151 	    sc->sc_wepkeys[0].an_wep_keylen, sc->sc_wepkeys[1].an_wep_keylen,
1152 	    sc->sc_wepkeys[2].an_wep_keylen, sc->sc_wepkeys[3].an_wep_keylen,
1153 	    sc->sc_tx_perskey,
1154 	    sc->sc_perskeylen[0], sc->sc_perskeylen[1],
1155 	    sc->sc_perskeylen[2], sc->sc_perskeylen[3],
1156 	    txkey,
1157 	    keys[0].an_wep_keylen, keys[1].an_wep_keylen,
1158 	    keys[2].an_wep_keylen, keys[3].an_wep_keylen));
1159 	if (!(nwkey->i_wepon & IEEE80211_NWKEY_PERSIST)) {
1160 		/* set temporary keys */
1161 		sc->sc_tx_key = txkey;
1162 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1163 			if (keys[i].an_wep_keylen < 0)
1164 				continue;
1165 			memcpy(&sc->sc_wepkeys[i], &keys[i], sizeof(keys[i]));
1166 		}
1167 	} else {
1168 		/* set persist keys */
1169 		if (anysetkey) {
1170 			/* prepare to write nvram */
1171 			if (!sc->sc_enabled) {
1172 				if (sc->sc_enable)
1173 					(*sc->sc_enable)(sc);
1174 				an_wait(sc);
1175 				sc->sc_enabled = 1;
1176 				error = an_write_wepkey(sc,
1177 				    AN_RID_WEP_PERSISTENT, keys, txkey);
1178 				if (sc->sc_disable)
1179 					(*sc->sc_disable)(sc);
1180 				sc->sc_enabled = 0;
1181 			} else {
1182 				an_cmd(sc, AN_CMD_DISABLE, 0);
1183 				error = an_write_wepkey(sc,
1184 				    AN_RID_WEP_PERSISTENT, keys, txkey);
1185 				an_cmd(sc, AN_CMD_ENABLE, 0);
1186 			}
1187 			if (error)
1188 				return error;
1189 		}
1190 		if (txkey >= 0)
1191 			sc->sc_tx_perskey = txkey;
1192 		if (sc->sc_tx_key >= 0) {
1193 			sc->sc_tx_key = -1;
1194 			needreset++;
1195 		}
1196 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1197 			if (sc->sc_wepkeys[i].an_wep_keylen >= 0) {
1198 				memset(&sc->sc_wepkeys[i].an_wep_key, 0,
1199 				    sizeof(sc->sc_wepkeys[i].an_wep_key));
1200 				sc->sc_wepkeys[i].an_wep_keylen = -1;
1201 				needreset++;
1202 			}
1203 			if (keys[i].an_wep_keylen >= 0)
1204 				sc->sc_perskeylen[i] = keys[i].an_wep_keylen;
1205 		}
1206 	}
1207 	if (needreset) {
1208 		/* firmware restart to reload persistent key */
1209 		an_reset(sc);
1210 	}
1211 	if (anysetkey || needreset)
1212 		error = ENETRESET;
1213 	return error;
1214 }
1215 
1216 static int
1217 an_set_nwkey_eap(struct an_softc *sc, struct ieee80211_nwkey *nwkey)
1218 {
1219 	int i, error, len;
1220 	struct ifnet *ifp = &sc->sc_if;
1221 	struct an_rid_leapkey *key;
1222 	u_int16_t unibuf[sizeof(key->an_key)];
1223 	static const int leap_rid[] = { AN_RID_LEAP_PASS, AN_RID_LEAP_USER };
1224 	MD4_CTX ctx;
1225 
1226 	error = 0;
1227 
1228 	if (nwkey->i_key[0].i_keydat == NULL &&
1229 	    nwkey->i_key[1].i_keydat == NULL)
1230 		return 0;
1231 	if (!sc->sc_enabled)
1232 		return ENXIO;
1233 	an_cmd(sc, AN_CMD_DISABLE, 0);
1234 	key = &sc->sc_buf.sc_leapkey;
1235 	for (i = 0; i < 2; i++) {
1236 		if (nwkey->i_key[i].i_keydat == NULL)
1237 			continue;
1238 		len = nwkey->i_key[i].i_keylen;
1239 		if (len > sizeof(key->an_key))
1240 			return EINVAL;
1241 		memset(key, 0, sizeof(*key));
1242 		key->an_key_len = htole16(len);
1243 		if ((error = copyin(nwkey->i_key[i].i_keydat, key->an_key,
1244 		    len)) != 0)
1245 			return error;
1246 		if (i == 1) {
1247 			/*
1248 			 * Cisco seems to use PasswordHash and PasswordHashHash
1249 			 * in RFC-2759 (MS-CHAP-V2).
1250 			 */
1251 			memset(unibuf, 0, sizeof(unibuf));
1252 			/* XXX: convert password to unicode */
1253 			for (i = 0; i < len; i++)
1254 				unibuf[i] = key->an_key[i];
1255 			/* set PasswordHash */
1256 			MD4Init(&ctx);
1257 			MD4Update(&ctx, (u_int8_t *)unibuf, len * 2);
1258 			MD4Final(key->an_key, &ctx);
1259 			/* set PasswordHashHash */
1260 			MD4Init(&ctx);
1261 			MD4Update(&ctx, key->an_key, 16);
1262 			MD4Final(key->an_key + 16, &ctx);
1263 			key->an_key_len = htole16(32);
1264 		}
1265 		if ((error = an_write_rid(sc, leap_rid[i], key,
1266 		    sizeof(*key))) != 0) {
1267 			printf("%s: LEAP set failed\n", ifp->if_xname);
1268 			return error;
1269 		}
1270 	}
1271 	error = an_cmd(sc, AN_CMD_ENABLE, 0);
1272 	if (error)
1273 		printf("%s: an_set_nwkey: failed to enable MAC\n",
1274 		    ifp->if_xname);
1275 	else
1276 		error = ENETRESET;
1277 	return error;
1278 }
1279 
1280 static int
1281 an_get_nwkey(struct an_softc *sc, struct ieee80211_nwkey *nwkey)
1282 {
1283 	int i, error;
1284 
1285 	error = 0;
1286 	if (sc->sc_config.an_authtype & AN_AUTHTYPE_LEAP)
1287 		nwkey->i_wepon = IEEE80211_NWKEY_EAP;
1288 	else if (sc->sc_config.an_authtype & AN_AUTHTYPE_PRIVACY_IN_USE)
1289 		nwkey->i_wepon = IEEE80211_NWKEY_WEP;
1290 	else
1291 		nwkey->i_wepon = IEEE80211_NWKEY_OPEN;
1292 	if (sc->sc_tx_key == -1)
1293 		nwkey->i_defkid = sc->sc_tx_perskey + 1;
1294 	else
1295 		nwkey->i_defkid = sc->sc_tx_key + 1;
1296 	if (nwkey->i_key[0].i_keydat == NULL)
1297 		return 0;
1298 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1299 		if (nwkey->i_key[i].i_keydat == NULL)
1300 			continue;
1301 		/* do not show any keys to non-root user */
1302 		if ((error = suser(curproc->p_ucred, &curproc->p_acflag)) != 0)
1303 			break;
1304 		nwkey->i_key[i].i_keylen = sc->sc_wepkeys[i].an_wep_keylen;
1305 		if (nwkey->i_key[i].i_keylen < 0) {
1306 			if (sc->sc_perskeylen[i] == 0)
1307 				nwkey->i_key[i].i_keylen = 0;
1308 			continue;
1309 		}
1310 		if ((error = copyout(sc->sc_wepkeys[i].an_wep_key,
1311 		    nwkey->i_key[i].i_keydat,
1312 		    sc->sc_wepkeys[i].an_wep_keylen)) != 0)
1313 			break;
1314 	}
1315 	return error;
1316 }
1317 
1318 static int
1319 an_write_wepkey(struct an_softc *sc, int type, struct an_wepkey *keys, int kid)
1320 {
1321 	int i, error;
1322 	struct an_rid_wepkey *akey;
1323 
1324 	error = 0;
1325 	akey = &sc->sc_buf.sc_wepkey;
1326 	memset(akey, 0, sizeof(struct an_rid_wepkey));
1327 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1328 		if (keys[i].an_wep_keylen < 0 ||
1329 		    keys[i].an_wep_keylen > sizeof(akey->an_key))
1330 			continue;
1331 		akey->an_key_len = htole16(keys[i].an_wep_keylen);
1332 		akey->an_key_index = htole16(i);
1333 		akey->an_mac_addr[0] = 1;	/* default mac */
1334 		memcpy(akey->an_key, keys[i].an_wep_key, keys[i].an_wep_keylen);
1335 		if ((error = an_write_rid(sc, type, akey, sizeof(*akey))) != 0)
1336 			return error;
1337 	}
1338 	if (kid >= 0) {
1339 		akey->an_key_index = htole16(0xffff);
1340 		akey->an_mac_addr[0] = kid;
1341 		akey->an_key_len = htole16(0);
1342 		memset(akey->an_key, 0, sizeof(akey->an_key));
1343 		error = an_write_rid(sc, type, akey, sizeof(*akey));
1344 	}
1345 	return error;
1346 }
1347 
1348 #ifdef AN_DEBUG
1349 static void
1350 an_dump_pkt(const char *devname, struct mbuf *m)
1351 {
1352 	int col, col0, i;
1353 	uint8_t *pkt = mtod(m, uint8_t *);
1354 	const char *delim = "";
1355 	int delimw = 0;
1356 
1357 	printf("%s: pkt ", devname);
1358 	col = col0 = strlen(devname) + strlen(": pkt ");
1359 	for (i = 0; i < m->m_len; i++) {
1360 		printf("%s%02x", delim, pkt[i]);
1361 		delim = ":";
1362 		delimw = 1;
1363 		col += delimw + 2;
1364 		if (col >= 72) {
1365 			printf("\n%*s", col0, "");
1366 			col = col0;
1367 			delim = "";
1368 			delimw = 0;
1369 		}
1370 	}
1371 	if (col != 0)
1372 		printf("\n");
1373 }
1374 #endif /* AN_DEBUG */
1375 
1376 /*
1377  * Low level functions
1378  */
1379 
1380 static void
1381 an_rx_intr(struct an_softc *sc)
1382 {
1383 	struct ieee80211com *ic = &sc->sc_ic;
1384 	struct ifnet *ifp = &sc->sc_if;
1385 	struct ieee80211_frame_min *wh;
1386 	struct ieee80211_node *ni;
1387 	struct an_rxframe frmhdr;
1388 	struct mbuf *m;
1389 	u_int16_t status;
1390 	int fid, gaplen, len, off;
1391 	uint8_t *gap;
1392 
1393 	fid = CSR_READ_2(sc, AN_RX_FID);
1394 
1395 	/* First read in the frame header */
1396 	if (an_read_bap(sc, fid, 0, &frmhdr, sizeof(frmhdr)) != 0) {
1397 		CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
1398 		ifp->if_ierrors++;
1399 		DPRINTF(("an_rx_intr: read fid %x failed\n", fid));
1400 		return;
1401 	}
1402 
1403 #ifdef AN_DEBUG
1404 	if ((ifp->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2)) {
1405 		ieee80211_dump_pkt((u_int8_t *)&frmhdr.an_whdr,
1406 		    sizeof(struct ieee80211_frame), frmhdr.an_rx_rate,
1407 		    frmhdr.an_rx_signal_strength);
1408 		printf(" time 0x%x status 0x%x plen %u chan %u"
1409 		    " plcp %02x %02x %02x %02x gap %u\n",
1410 		    le32toh(frmhdr.an_rx_time), le16toh(frmhdr.an_rx_status),
1411 		    le16toh(frmhdr.an_rx_payload_len), frmhdr.an_rx_chan,
1412 		    frmhdr.an_plcp_hdr[0], frmhdr.an_plcp_hdr[1],
1413 		    frmhdr.an_plcp_hdr[2], frmhdr.an_plcp_hdr[3],
1414 		    le16toh(frmhdr.an_gaplen));
1415 	}
1416 #endif
1417 
1418 	status = le16toh(frmhdr.an_rx_status);
1419 	if ((status & AN_STAT_ERRSTAT) != 0 &&
1420 	    ic->ic_opmode != IEEE80211_M_MONITOR) {
1421 		CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
1422 		ifp->if_ierrors++;
1423 		DPRINTF(("an_rx_intr: fid %x status %x\n", fid, status));
1424 		return;
1425 	}
1426 
1427 	/* the payload length field includes a 16-bit "mystery field" */
1428 	len = le16toh(frmhdr.an_rx_payload_len) - sizeof(uint16_t);
1429 	off = ALIGN(sizeof(struct ieee80211_frame));
1430 
1431 	if (off + len > MCLBYTES) {
1432 		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1433 			CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
1434 			ifp->if_ierrors++;
1435 			DPRINTF(("an_rx_intr: oversized packet %d\n", len));
1436 			return;
1437 		}
1438 		len = 0;
1439 	}
1440 
1441 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1442 	if (m == NULL) {
1443 		CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
1444 		ifp->if_ierrors++;
1445 		DPRINTF(("an_rx_intr: MGET failed\n"));
1446 		return;
1447 	}
1448 	if (off + len + AN_GAPLEN_MAX > MHLEN) {
1449 		MCLGET(m, M_DONTWAIT);
1450 		if ((m->m_flags & M_EXT) == 0) {
1451 			CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
1452 			m_freem(m);
1453 			ifp->if_ierrors++;
1454 			DPRINTF(("an_rx_intr: MCLGET failed\n"));
1455 			return;
1456 		}
1457 	}
1458 	m->m_data += off - sizeof(struct ieee80211_frame);
1459 
1460 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1461 		gaplen = le16toh(frmhdr.an_gaplen);
1462 		if (gaplen > AN_GAPLEN_MAX) {
1463 			CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
1464 			m_freem(m);
1465 			ifp->if_ierrors++;
1466 			DPRINTF(("%s: gap too long\n", __func__));
1467 			return;
1468 		}
1469 		/*
1470 		 * We don't need the 16-bit mystery field (payload length?),
1471 		 * so read it into the region reserved for the 802.11 header.
1472 		 *
1473 		 * When Cisco Aironet 350 cards w/ firmware version 5 or
1474 		 * greater operate with certain Cisco 350 APs,
1475 		 * the "gap" is filled with the SNAP header.  Read
1476 		 * it in after the 802.11 header.
1477 		 */
1478 		gap = m->m_data + sizeof(struct ieee80211_frame) -
1479 		    sizeof(uint16_t);
1480 		an_read_bap(sc, fid, -1, gap, gaplen + sizeof(u_int16_t));
1481 #ifdef AN_DEBUG
1482 		if ((ifp->if_flags & (IFF_DEBUG|IFF_LINK2)) ==
1483 		    (IFF_DEBUG|IFF_LINK2)) {
1484 			int i;
1485 			printf(" gap&len");
1486 			for (i = 0; i < gaplen + sizeof(u_int16_t); i++)
1487 				printf(" %02x", gap[i]);
1488 			printf("\n");
1489 		}
1490 #endif
1491 	} else
1492 		gaplen = 0;
1493 
1494 	an_read_bap(sc, fid, -1,
1495 	    m->m_data + sizeof(struct ieee80211_frame) + gaplen, len);
1496 	m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame) + gaplen +
1497 	    len;
1498 
1499 	memcpy(m->m_data, &frmhdr.an_whdr, sizeof(struct ieee80211_frame));
1500 	m->m_pkthdr.rcvif = ifp;
1501 	CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_RX);
1502 
1503 #if NBPFILTER > 0
1504 	if (sc->sc_drvbpf) {
1505 		struct an_rx_radiotap_header *tap = &sc->sc_rxtap;
1506 
1507 		tap->ar_rate = frmhdr.an_rx_rate;
1508 		tap->ar_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
1509 		tap->ar_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
1510 		tap->ar_antsignal = frmhdr.an_rx_signal_strength;
1511 		if ((le16toh(frmhdr.an_rx_status) & AN_STAT_BADCRC) ||
1512 		    (le16toh(frmhdr.an_rx_status) & AN_STAT_ERRSTAT) ||
1513 		    (le16toh(frmhdr.an_rx_status) & AN_STAT_UNDECRYPTABLE))
1514 		    tap->ar_flags |= IEEE80211_RADIOTAP_F_BADFCS;
1515 
1516 		bpf_mtap2(sc->sc_drvbpf, tap, tap->ar_ihdr.it_len, m);
1517         }
1518 #endif
1519 	wh = mtod(m, struct ieee80211_frame_min *);
1520 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1521 		/*
1522 		 * WEP is decrypted by hardware. Clear WEP bit
1523 		 * header for ieee80211_input().
1524 		 */
1525 		wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1526 	}
1527 
1528 #ifdef AN_DEBUG
1529 	if (an_debug > 1)
1530 		an_dump_pkt(sc->sc_dev.dv_xname, m);
1531 #endif /* AN_DEBUG */
1532 
1533 	ni = ieee80211_find_rxnode(ic, wh);
1534 	ieee80211_input(ic, m, ni, frmhdr.an_rx_signal_strength,
1535 	    le32toh(frmhdr.an_rx_time));
1536 	ieee80211_free_node(ni);
1537 }
1538 
1539 static void
1540 an_tx_intr(struct an_softc *sc, int status)
1541 {
1542 	struct ifnet *ifp = &sc->sc_if;
1543 	int cur, fid;
1544 
1545 	sc->sc_tx_timer = 0;
1546 	ifp->if_flags &= ~IFF_OACTIVE;
1547 
1548 	fid = CSR_READ_2(sc, AN_TX_CMP_FID);
1549 	CSR_WRITE_2(sc, AN_EVENT_ACK, status & (AN_EV_TX | AN_EV_TX_EXC));
1550 
1551 	if (status & AN_EV_TX_EXC)
1552 		ifp->if_oerrors++;
1553 	else
1554 		ifp->if_opackets++;
1555 
1556 	cur = sc->sc_txcur;
1557 	if (sc->sc_txd[cur].d_fid == fid) {
1558 		sc->sc_txd[cur].d_inuse = 0;
1559 		DPRINTF2(("an_tx_intr: sent %x/%d\n", fid, cur));
1560 		AN_INC(cur, AN_TX_RING_CNT);
1561 		sc->sc_txcur = cur;
1562 	} else {
1563 		for (cur = 0; cur < AN_TX_RING_CNT; cur++) {
1564 			if (fid == sc->sc_txd[cur].d_fid) {
1565 				sc->sc_txd[cur].d_inuse = 0;
1566 				break;
1567 			}
1568 		}
1569 		if (ifp->if_flags & IFF_DEBUG)
1570 			printf("%s: tx mismatch: "
1571 			    "expected %x(%d), actual %x(%d)\n",
1572 			    sc->sc_dev.dv_xname,
1573 			    sc->sc_txd[sc->sc_txcur].d_fid, sc->sc_txcur,
1574 			    fid, cur);
1575 	}
1576 
1577 	return;
1578 }
1579 
1580 static void
1581 an_linkstat_intr(struct an_softc *sc)
1582 {
1583 	struct ieee80211com *ic = &sc->sc_ic;
1584 	u_int16_t status;
1585 
1586 	status = CSR_READ_2(sc, AN_LINKSTAT);
1587 	CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_LINKSTAT);
1588 	DPRINTF(("an_linkstat_intr: status 0x%x\n", status));
1589 
1590 	if (status == AN_LINKSTAT_ASSOCIATED) {
1591 		if (ic->ic_state != IEEE80211_S_RUN ||
1592 		    ic->ic_opmode == IEEE80211_M_IBSS)
1593 			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1594 	} else {
1595 		if (ic->ic_opmode == IEEE80211_M_STA)
1596 			ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1597 	}
1598 }
1599 
1600 /* Must be called at proper protection level! */
1601 static int
1602 an_cmd(struct an_softc *sc, int cmd, int val)
1603 {
1604 	int i, status;
1605 
1606 	/* make sure that previous command completed */
1607 	if (CSR_READ_2(sc, AN_COMMAND) & AN_CMD_BUSY) {
1608 		if (sc->sc_if.if_flags & IFF_DEBUG)
1609 			printf("%s: command 0x%x busy\n", sc->sc_dev.dv_xname,
1610 			    CSR_READ_2(sc, AN_COMMAND));
1611 		CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CLR_STUCK_BUSY);
1612 	}
1613 
1614 	CSR_WRITE_2(sc, AN_PARAM0, val);
1615 	CSR_WRITE_2(sc, AN_PARAM1, 0);
1616 	CSR_WRITE_2(sc, AN_PARAM2, 0);
1617 	CSR_WRITE_2(sc, AN_COMMAND, cmd);
1618 
1619 	if (cmd == AN_CMD_FW_RESTART) {
1620 		/* XXX: should sleep here */
1621 		DELAY(100*1000);
1622 	}
1623 
1624 	for (i = 0; i < AN_TIMEOUT; i++) {
1625 		if (CSR_READ_2(sc, AN_EVENT_STAT) & AN_EV_CMD)
1626 			break;
1627 		DELAY(10);
1628 	}
1629 
1630 	status = CSR_READ_2(sc, AN_STATUS);
1631 
1632 	/* clear stuck command busy if necessary */
1633 	if (CSR_READ_2(sc, AN_COMMAND) & AN_CMD_BUSY)
1634 		CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CLR_STUCK_BUSY);
1635 
1636 	/* Ack the command */
1637 	CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CMD);
1638 
1639 	if (i == AN_TIMEOUT) {
1640 		if (sc->sc_if.if_flags & IFF_DEBUG)
1641 			printf("%s: command 0x%x param 0x%x timeout\n",
1642 			    sc->sc_dev.dv_xname, cmd, val);
1643 		return ETIMEDOUT;
1644 	}
1645 	if (status & AN_STAT_CMD_RESULT) {
1646 		if (sc->sc_if.if_flags & IFF_DEBUG)
1647 			printf("%s: command 0x%x param 0x%x status 0x%x "
1648 			    "resp 0x%x 0x%x 0x%x\n",
1649 			    sc->sc_dev.dv_xname, cmd, val, status,
1650 			    CSR_READ_2(sc, AN_RESP0), CSR_READ_2(sc, AN_RESP1),
1651 			    CSR_READ_2(sc, AN_RESP2));
1652 		return EIO;
1653 	}
1654 
1655 	return 0;
1656 }
1657 
1658 
1659 /*
1660  * Wait for firmware come up after power enabled.
1661  */
1662 static void
1663 an_wait(struct an_softc *sc)
1664 {
1665 	int i;
1666 
1667 	CSR_WRITE_2(sc, AN_COMMAND, AN_CMD_NOOP2);
1668 	for (i = 0; i < 3*hz; i++) {
1669 		if (CSR_READ_2(sc, AN_EVENT_STAT) & AN_EV_CMD)
1670 			break;
1671 		(void)tsleep(sc, PWAIT, "anatch", 1);
1672 	}
1673 	CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_CMD);
1674 }
1675 
1676 static int
1677 an_seek_bap(struct an_softc *sc, int id, int off)
1678 {
1679 	int i, status;
1680 
1681 	CSR_WRITE_2(sc, AN_SEL0, id);
1682 	CSR_WRITE_2(sc, AN_OFF0, off);
1683 
1684 	for (i = 0; ; i++) {
1685 		status = CSR_READ_2(sc, AN_OFF0);
1686 		if ((status & AN_OFF_BUSY) == 0)
1687 			break;
1688 		if (i == AN_TIMEOUT) {
1689 			printf("%s: timeout in an_seek_bap to 0x%x/0x%x\n",
1690 			    sc->sc_dev.dv_xname, id, off);
1691 			sc->sc_bap_off = AN_OFF_ERR;	/* invalidate */
1692 			return ETIMEDOUT;
1693 		}
1694 		DELAY(10);
1695 	}
1696 	if (status & AN_OFF_ERR) {
1697 		printf("%s: failed in an_seek_bap to 0x%x/0x%x\n",
1698 		    sc->sc_dev.dv_xname, id, off);
1699 		sc->sc_bap_off = AN_OFF_ERR;	/* invalidate */
1700 		return EIO;
1701 	}
1702 	sc->sc_bap_id = id;
1703 	sc->sc_bap_off = off;
1704 	return 0;
1705 }
1706 
1707 static int
1708 an_read_bap(struct an_softc *sc, int id, int off, void *buf, int buflen)
1709 {
1710 	int error, cnt;
1711 
1712 	if (buflen == 0)
1713 		return 0;
1714 	if (off == -1)
1715 		off = sc->sc_bap_off;
1716 	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1717 		if ((error = an_seek_bap(sc, id, off)) != 0)
1718 			return EIO;
1719 	}
1720 
1721 	cnt = (buflen + 1) / 2;
1722 	CSR_READ_MULTI_STREAM_2(sc, AN_DATA0, (u_int16_t *)buf, cnt);
1723 	sc->sc_bap_off += cnt * 2;
1724 	return 0;
1725 }
1726 
1727 static int
1728 an_write_bap(struct an_softc *sc, int id, int off, void *buf, int buflen)
1729 {
1730 	int error, cnt;
1731 
1732 	if (buflen == 0)
1733 		return 0;
1734 	if (off == -1)
1735 		off = sc->sc_bap_off;
1736 	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1737 		if ((error = an_seek_bap(sc, id, off)) != 0)
1738 			return EIO;
1739 	}
1740 
1741 	cnt = (buflen + 1) / 2;
1742 	CSR_WRITE_MULTI_STREAM_2(sc, AN_DATA0, (u_int16_t *)buf, cnt);
1743 	sc->sc_bap_off += cnt * 2;
1744 	return 0;
1745 }
1746 
1747 static int
1748 an_mwrite_bap(struct an_softc *sc, int id, int off, struct mbuf *m, int totlen)
1749 {
1750 	int error, len, cnt;
1751 
1752 	if (off == -1)
1753 		off = sc->sc_bap_off;
1754 	if (id != sc->sc_bap_id || off != sc->sc_bap_off) {
1755 		if ((error = an_seek_bap(sc, id, off)) != 0)
1756 			return EIO;
1757 	}
1758 
1759 	for (len = 0; m != NULL; m = m->m_next) {
1760 		if (m->m_len == 0)
1761 			continue;
1762 		len = min(m->m_len, totlen);
1763 
1764 		if ((mtod(m, u_long) & 0x1) || (len & 0x1)) {
1765 			m_copydata(m, 0, totlen, (caddr_t)&sc->sc_buf.sc_txbuf);
1766 			cnt = (totlen + 1) / 2;
1767 			CSR_WRITE_MULTI_STREAM_2(sc, AN_DATA0,
1768 			    sc->sc_buf.sc_val, cnt);
1769 			off += cnt * 2;
1770 			break;
1771 		}
1772 		cnt = len / 2;
1773 		CSR_WRITE_MULTI_STREAM_2(sc, AN_DATA0, mtod(m, u_int16_t *),
1774 		    cnt);
1775 		off += len;
1776 		totlen -= len;
1777 	}
1778 	sc->sc_bap_off = off;
1779 	return 0;
1780 }
1781 
1782 static int
1783 an_alloc_fid(struct an_softc *sc, int len, int *idp)
1784 {
1785 	int i;
1786 
1787 	if (an_cmd(sc, AN_CMD_ALLOC_MEM, len)) {
1788 		printf("%s: failed to allocate %d bytes on NIC\n",
1789 		    sc->sc_dev.dv_xname, len);
1790 		return ENOMEM;
1791 	}
1792 
1793 	for (i = 0; i < AN_TIMEOUT; i++) {
1794 		if (CSR_READ_2(sc, AN_EVENT_STAT) & AN_EV_ALLOC)
1795 			break;
1796 		if (i == AN_TIMEOUT) {
1797 			printf("%s: timeout in alloc\n", sc->sc_dev.dv_xname);
1798 			return ETIMEDOUT;
1799 		}
1800 		DELAY(10);
1801 	}
1802 
1803 	*idp = CSR_READ_2(sc, AN_ALLOC_FID);
1804 	CSR_WRITE_2(sc, AN_EVENT_ACK, AN_EV_ALLOC);
1805 	return 0;
1806 }
1807 
1808 static int
1809 an_read_rid(struct an_softc *sc, int rid, void *buf, int *buflenp)
1810 {
1811 	int error;
1812 	u_int16_t len;
1813 
1814 	/* Tell the NIC to enter record read mode. */
1815 	error = an_cmd(sc, AN_CMD_ACCESS | AN_ACCESS_READ, rid);
1816 	if (error)
1817 		return error;
1818 
1819 	/* length in byte, including length itself */
1820 	error = an_read_bap(sc, rid, 0, &len, sizeof(len));
1821 	if (error)
1822 		return error;
1823 
1824 	len = le16toh(len) - 2;
1825 	if (*buflenp < len) {
1826 		printf("%s: record buffer is too small, "
1827 		    "rid=%x, size=%d, len=%d\n",
1828 		    sc->sc_dev.dv_xname, rid, *buflenp, len);
1829 		return ENOSPC;
1830 	}
1831 	*buflenp = len;
1832 	return an_read_bap(sc, rid, sizeof(len), buf, len);
1833 }
1834 
1835 static int
1836 an_write_rid(struct an_softc *sc, int rid, void *buf, int buflen)
1837 {
1838 	int error;
1839 	u_int16_t len;
1840 
1841 	/* length in byte, including length itself */
1842 	len = htole16(buflen + 2);
1843 
1844 	error = an_write_bap(sc, rid, 0, &len, sizeof(len));
1845 	if (error)
1846 		return error;
1847 	error = an_write_bap(sc, rid, sizeof(len), buf, buflen);
1848 	if (error)
1849 		return error;
1850 
1851 	return an_cmd(sc, AN_CMD_ACCESS | AN_ACCESS_WRITE, rid);
1852 }
1853 
1854 static int
1855 an_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
1856 {
1857 	struct an_softc *sc = (struct an_softc *)ic->ic_ifp->if_softc;
1858 	struct ieee80211_node *ni = ic->ic_bss;
1859 	enum ieee80211_state ostate;
1860 	int buflen;
1861 
1862 	ostate = ic->ic_state;
1863 	DPRINTF(("an_newstate: %s -> %s\n", ieee80211_state_name[ostate],
1864 	    ieee80211_state_name[nstate]));
1865 
1866 	switch (nstate) {
1867 	case IEEE80211_S_INIT:
1868 		ic->ic_flags &= ~IEEE80211_F_IBSSON;
1869 		return (*sc->sc_newstate)(ic, nstate, arg);
1870 
1871 	case IEEE80211_S_SCAN:
1872 	case IEEE80211_S_AUTH:
1873 	case IEEE80211_S_ASSOC:
1874 		ic->ic_state = nstate; /* NB: skip normal ieee80211 handling */
1875 		return 0;
1876 
1877 	case IEEE80211_S_RUN:
1878 		buflen = sizeof(sc->sc_buf);
1879 		an_read_rid(sc, AN_RID_STATUS, &sc->sc_buf, &buflen);
1880 		IEEE80211_ADDR_COPY(ni->ni_bssid,
1881 		    sc->sc_buf.sc_status.an_cur_bssid);
1882 		IEEE80211_ADDR_COPY(ni->ni_macaddr, ni->ni_bssid);
1883 		ni->ni_chan = &ic->ic_channels[
1884 		    le16toh(sc->sc_buf.sc_status.an_cur_channel)];
1885 		ni->ni_esslen = le16toh(sc->sc_buf.sc_status.an_ssidlen);
1886 		if (ni->ni_esslen > IEEE80211_NWID_LEN)
1887 			ni->ni_esslen = IEEE80211_NWID_LEN;	/*XXX*/
1888 		memcpy(ni->ni_essid, sc->sc_buf.sc_status.an_ssid,
1889 		    ni->ni_esslen);
1890 		ni->ni_rates = ic->ic_sup_rates[IEEE80211_MODE_11B];	/*XXX*/
1891 		if (ic->ic_ifp->if_flags & IFF_DEBUG) {
1892 			printf("%s: ", sc->sc_dev.dv_xname);
1893 			if (ic->ic_opmode == IEEE80211_M_STA)
1894 				printf("associated ");
1895 			else
1896 				printf("synchronized ");
1897 			printf("with %s ssid ", ether_sprintf(ni->ni_bssid));
1898 			ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
1899 			printf(" channel %u start %uMb\n",
1900 			    le16toh(sc->sc_buf.sc_status.an_cur_channel),
1901 			    le16toh(sc->sc_buf.sc_status.an_current_tx_rate)/2);
1902 		}
1903 		break;
1904 
1905 	default:
1906 		break;
1907 	}
1908 	return (*sc->sc_newstate)(ic, nstate, arg);
1909 }
1910