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