xref: /netbsd-src/sys/dev/ic/awi.c (revision 8a8f936f250a330d54f8a24ed0e92aadf9743a7b)
1 /*	$NetBSD: awi.c,v 1.40 2001/09/20 13:54:44 onoe Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999,2000,2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Bill Sommerfeld
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 /*
39  * Driver for AMD 802.11 firmware.
40  * Uses am79c930 chip driver to talk to firmware running on the am79c930.
41  *
42  * More-or-less a generic ethernet-like if driver, with 802.11 gorp added.
43  */
44 
45 /*
46  * todo:
47  *	- flush tx queue on resynch.
48  *	- clear oactive on "down".
49  *	- rewrite copy-into-mbuf code
50  *	- mgmt state machine gets stuck retransmitting assoc requests.
51  *	- multicast filter.
52  *	- fix device reset so it's more likely to work
53  *	- show status goo through ifmedia.
54  *
55  * more todo:
56  *	- deal with more 802.11 frames.
57  *		- send reassoc request
58  *		- deal with reassoc response
59  *		- send/deal with disassociation
60  *	- deal with "full" access points (no room for me).
61  *	- power save mode
62  *
63  * later:
64  *	- SSID preferences
65  *	- need ioctls for poking at the MIBs
66  *	- implement ad-hoc mode (including bss creation).
67  *	- decide when to do "ad hoc" vs. infrastructure mode (IFF_LINK flags?)
68  *		(focus on inf. mode since that will be needed for ietf)
69  *	- deal with DH vs. FH versions of the card
70  *	- deal with faster cards (2mb/s)
71  *	- ?WEP goo (mmm, rc4) (it looks not particularly useful).
72  *	- ifmedia revision.
73  *	- common 802.11 mibish things.
74  *	- common 802.11 media layer.
75  */
76 
77 /*
78  * Driver for AMD 802.11 PCnetMobile firmware.
79  * Uses am79c930 chip driver to talk to firmware running on the am79c930.
80  *
81  * The initial version of the driver was written by
82  * Bill Sommerfeld <sommerfeld@netbsd.org>.
83  * Then the driver module completely rewritten to support cards with DS phy
84  * and to support adhoc mode by Atsushi Onoe <onoe@netbsd.org>
85  */
86 
87 #include "opt_inet.h"
88 #include "bpfilter.h"
89 
90 #include <sys/param.h>
91 #include <sys/systm.h>
92 #include <sys/kernel.h>
93 #include <sys/mbuf.h>
94 #include <sys/malloc.h>
95 #include <sys/proc.h>
96 #include <sys/socket.h>
97 #include <sys/sockio.h>
98 #include <sys/errno.h>
99 #include <sys/device.h>
100 
101 #include <net/if.h>
102 #include <net/if_dl.h>
103 #include <net/if_ether.h>
104 #include <net/if_media.h>
105 #include <net/if_llc.h>
106 #include <net/if_ieee80211.h>
107 
108 #ifdef INET
109 #include <netinet/in.h>
110 #include <netinet/in_systm.h>
111 #ifdef __NetBSD__
112 #include <netinet/if_inarp.h>
113 #else
114 #include <netinet/if_ether.h>
115 #endif
116 #endif
117 
118 #if NBPFILTER > 0
119 #include <net/bpf.h>
120 #endif
121 
122 #include <machine/cpu.h>
123 #include <machine/bus.h>
124 #include <machine/intr.h>
125 
126 #include <dev/ic/am79c930reg.h>
127 #include <dev/ic/am79c930var.h>
128 #include <dev/ic/awireg.h>
129 #include <dev/ic/awivar.h>
130 
131 static int  awi_init(struct ifnet *);
132 static void awi_stop(struct ifnet *, int);
133 static void awi_start(struct ifnet *);
134 static void awi_watchdog(struct ifnet *);
135 static int  awi_ioctl(struct ifnet *, u_long, caddr_t);
136 static int  awi_media_change(struct ifnet *);
137 static void awi_media_status(struct ifnet *, struct ifmediareq *);
138 static int  awi_mode_init(struct awi_softc *);
139 static int  awi_media_rate2opt(struct awi_softc *, int);
140 static int  awi_media_opt2rate(struct awi_softc *, int);
141 static void awi_rx_int(struct awi_softc *);
142 static void awi_tx_int(struct awi_softc *);
143 static struct mbuf *awi_devget(struct awi_softc *, u_int32_t, u_int16_t);
144 static int  awi_hw_init(struct awi_softc *);
145 static int  awi_init_mibs(struct awi_softc *);
146 static int  awi_chan_check(void *, u_char *);
147 static int  awi_mib(struct awi_softc *, u_int8_t, u_int8_t, int);
148 static int  awi_cmd(struct awi_softc *, u_int8_t, int);
149 static int  awi_cmd_wait(struct awi_softc *);
150 static void awi_cmd_done(struct awi_softc *);
151 static int  awi_next_txd(struct awi_softc *, int, u_int32_t *, u_int32_t *);
152 static int  awi_lock(struct awi_softc *);
153 static void awi_unlock(struct awi_softc *);
154 static int  awi_intr_lock(struct awi_softc *);
155 static void awi_intr_unlock(struct awi_softc *);
156 static int  awi_newstate(void *, enum ieee80211_state);
157 static struct mbuf *awi_ether_encap(struct awi_softc *, struct mbuf *);
158 static struct mbuf *awi_ether_modcap(struct awi_softc *, struct mbuf *);
159 
160 /* unalligned little endian access */
161 #define LE_READ_2(p)							\
162 	((((u_int8_t *)(p))[0]      ) | (((u_int8_t *)(p))[1] <<  8))
163 #define LE_READ_4(p)							\
164 	((((u_int8_t *)(p))[0]      ) | (((u_int8_t *)(p))[1] <<  8) |	\
165 	 (((u_int8_t *)(p))[2] << 16) | (((u_int8_t *)(p))[3] << 24))
166 #define LE_WRITE_2(p, v)						\
167 	((((u_int8_t *)(p))[0] = (((u_int32_t)(v)      ) & 0xff)),	\
168 	 (((u_int8_t *)(p))[1] = (((u_int32_t)(v) >>  8) & 0xff)))
169 #define LE_WRITE_4(p, v)						\
170 	((((u_int8_t *)(p))[0] = (((u_int32_t)(v)      ) & 0xff)),	\
171 	 (((u_int8_t *)(p))[1] = (((u_int32_t)(v) >>  8) & 0xff)),	\
172 	 (((u_int8_t *)(p))[2] = (((u_int32_t)(v) >> 16) & 0xff)),	\
173 	 (((u_int8_t *)(p))[3] = (((u_int32_t)(v) >> 24) & 0xff)))
174 
175 struct awi_chanset awi_chanset[] = {
176     /* PHY type        domain            min max def */
177     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_JP,  6, 17,  6 },
178     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_ES,  0, 26,  1 },
179     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_FR,  0, 32,  1 },
180     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_US,  0, 77,  1 },
181     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_CA,  0, 77,  1 },
182     { AWI_PHY_TYPE_FH, AWI_REG_DOMAIN_EU,  0, 77,  1 },
183     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_JP, 14, 14, 14 },
184     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_ES, 10, 11, 10 },
185     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_FR, 10, 13, 10 },
186     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_US,  1, 11,  3 },
187     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_CA,  1, 11,  3 },
188     { AWI_PHY_TYPE_DS, AWI_REG_DOMAIN_EU,  1, 13,  3 },
189     { 0, 0 }
190 };
191 
192 #ifdef AWI_DEBUG
193 int awi_debug;
194 
195 #define	DPRINTF(X)	if (awi_debug) printf X
196 #define	DPRINTF2(X)	if (awi_debug > 1) printf X
197 #else
198 #define	DPRINTF(X)
199 #define	DPRINTF2(X)
200 #endif
201 
202 int
203 awi_attach(struct awi_softc *sc)
204 {
205 	struct ieee80211com *ic = &sc->sc_ic;
206 	struct ifnet *ifp = &ic->ic_if;
207 	int s, i, error, nrate;
208 	int mword;
209 	struct ifmediareq imr;
210 
211 	s = splnet();
212 	sc->sc_busy = 1;
213 	ic->ic_state = IEEE80211_S_INIT;
214 	sc->sc_substate = AWI_ST_NONE;
215 	if ((error = awi_hw_init(sc)) != 0) {
216 		sc->sc_invalid = 1;
217 		splx(s);
218 		return error;
219 	}
220 	error = awi_init_mibs(sc);
221 	if (error != 0) {
222 		sc->sc_invalid = 1;
223 		splx(s);
224 		return error;
225 	}
226 	ifp->if_softc = sc;
227 	ifp->if_flags =
228 	    IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST | IFF_NOTRAILERS;
229 	ifp->if_ioctl = awi_ioctl;
230 	ifp->if_start = awi_start;
231 	ifp->if_init = awi_init;
232 	ifp->if_stop = awi_stop;
233 	ifp->if_watchdog = awi_watchdog;
234 	IFQ_SET_READY(&ifp->if_snd);
235 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
236 
237 	ic->ic_flags = IEEE80211_F_HASWEP | IEEE80211_F_HASIBSS;
238 	ic->ic_newstate = awi_newstate;
239 	ic->ic_chancheck = awi_chan_check;
240 	nrate = sc->sc_mib_phy.aSuprt_Data_Rates[1];
241 	memcpy(ic->ic_sup_rates, sc->sc_mib_phy.aSuprt_Data_Rates + 2, nrate);
242 	memcpy(ic->ic_myaddr, sc->sc_mib_addr.aMAC_Address, IEEE80211_ADDR_LEN);
243 
244 	printf("%s: IEEE802.11 %s %dMbps (firmware %s)\n",
245 	    sc->sc_dev.dv_xname,
246 	    sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH ? "FH" : "DS",
247 	    (ic->ic_sup_rates[nrate - 1] & IEEE80211_RATE_VAL) / 2,
248 	    sc->sc_banner);
249 	printf("%s: 802.11 address: %s\n", sc->sc_dev.dv_xname,
250 	    ether_sprintf(ic->ic_myaddr));
251 
252 	if_attach(ifp);
253 	ieee80211_ifattach(ifp);
254 
255 	ifmedia_init(&sc->sc_media, 0, awi_media_change, awi_media_status);
256 	mword = IFM_MAKEWORD(IFM_IEEE80211, IFM_AUTO, 0, 0);
257 	ifmedia_add(&sc->sc_media, mword, 0, NULL);
258 	ifmedia_add(&sc->sc_media, mword | IFM_FLAG0, 0, NULL);
259 	mword |= IFM_IEEE80211_ADHOC;
260 	ifmedia_add(&sc->sc_media, mword, 0, NULL);
261 	ifmedia_add(&sc->sc_media, mword | IFM_FLAG0, 0, NULL);
262 	for (i = 0; i < nrate; i++) {
263 		mword = awi_media_rate2opt(sc, ic->ic_sup_rates[i]);
264 		if (mword == 0)
265 			continue;
266 		mword |= IFM_IEEE80211;
267 		ifmedia_add(&sc->sc_media, mword, 0, NULL);
268 		ifmedia_add(&sc->sc_media, mword | IFM_FLAG0, 0, NULL);
269 		mword |= IFM_IEEE80211_ADHOC;
270 		ifmedia_add(&sc->sc_media, mword, 0, NULL);
271 		if (sc->sc_mib_phy.IEEE_PHY_Type != AWI_PHY_TYPE_FH)
272 			ifmedia_add(&sc->sc_media, mword | IFM_FLAG0, 0, NULL);
273 	}
274 	awi_media_status(ifp, &imr);
275 	ifmedia_set(&sc->sc_media, imr.ifm_active);
276 
277 	if ((sc->sc_sdhook = shutdownhook_establish(awi_shutdown, sc)) == NULL)
278 		printf("%s: WARNING: unable to establish shutdown hook\n",
279 		    sc->sc_dev.dv_xname);
280 	if ((sc->sc_powerhook = powerhook_establish(awi_power, sc)) == NULL)
281 		printf("%s: WARNING: unable to establish power hook\n",
282 		    sc->sc_dev.dv_xname);
283 	sc->sc_attached = 1;
284 	splx(s);
285 
286 	/* ready to accept ioctl */
287 	awi_unlock(sc);
288 
289 	return 0;
290 }
291 
292 int
293 awi_detach(struct awi_softc *sc)
294 {
295 	struct ifnet *ifp = &sc->sc_ic.ic_if;
296 	int s;
297 
298 	if (!sc->sc_attached)
299 		return 0;
300 
301 	s = splnet();
302 	sc->sc_invalid = 1;
303 	awi_stop(ifp, 1);
304 	while (sc->sc_sleep_cnt > 0) {
305 		wakeup(sc);
306 		(void)tsleep(sc, PWAIT, "awidet", 1);
307 	}
308 	ifmedia_delete_instance(&sc->sc_media, IFM_INST_ANY);
309 	ieee80211_ifdetach(ifp);
310 	if_detach(ifp);
311 	shutdownhook_disestablish(sc->sc_sdhook);
312 	powerhook_disestablish(sc->sc_powerhook);
313 	splx(s);
314 	return 0;
315 }
316 
317 int
318 awi_activate(struct device *self, enum devact act)
319 {
320 	struct awi_softc *sc = (struct awi_softc *)self;
321 	struct ifnet *ifp = &sc->sc_ic.ic_if;
322 	int s, error = 0;
323 
324 	s = splnet();
325 	switch (act) {
326 	case DVACT_ACTIVATE:
327 		error = EOPNOTSUPP;
328 		break;
329 	case DVACT_DEACTIVATE:
330 		sc->sc_invalid = 1;
331 		if_deactivate(ifp);
332 		break;
333 	}
334 	splx(s);
335 	return error;
336 }
337 
338 void
339 awi_power(int why, void *arg)
340 {
341 	struct awi_softc *sc = arg;
342 	struct ifnet *ifp = &sc->sc_ic.ic_if;
343 	int s;
344 	int ocansleep;
345 
346 	DPRINTF(("awi_power: %d\n", why));
347 	s = splnet();
348 	ocansleep = sc->sc_cansleep;
349 	sc->sc_cansleep = 0;
350 	switch (why) {
351 	case PWR_SUSPEND:
352 	case PWR_STANDBY:
353 		awi_stop(ifp, 1);
354 		break;
355 	case PWR_RESUME:
356 		if (ifp->if_flags & IFF_UP) {
357 			awi_init(ifp);
358 			(void)awi_intr(sc);	/* make sure */
359 		}
360 		break;
361 	case PWR_SOFTSUSPEND:
362 	case PWR_SOFTSTANDBY:
363 	case PWR_SOFTRESUME:
364 		break;
365 	}
366 	sc->sc_cansleep = ocansleep;
367 	splx(s);
368 }
369 
370 void
371 awi_shutdown(void *arg)
372 {
373 	struct awi_softc *sc = arg;
374 	struct ifnet *ifp = &sc->sc_ic.ic_if;
375 
376 	if (sc->sc_attached)
377 		awi_stop(ifp, 1);
378 }
379 
380 int
381 awi_intr(void *arg)
382 {
383 	struct awi_softc *sc = arg;
384 	u_int16_t status;
385 	int error, handled = 0, ocansleep;
386 #ifdef AWI_DEBUG
387 	static const char *intname[] = {
388 	    "CMD", "RX", "TX", "SCAN_CMPLT",
389 	    "CFP_START", "DTIM", "CFP_ENDING", "GROGGY",
390 	    "TXDATA", "TXBCAST", "TXPS", "TXCF",
391 	    "TXMGT", "#13", "RXDATA", "RXMGT"
392 	};
393 #endif
394 
395 	if (!sc->sc_enabled || !sc->sc_enab_intr || sc->sc_invalid)
396 		return 0;
397 
398 	am79c930_gcr_setbits(&sc->sc_chip,
399 	    AM79C930_GCR_DISPWDN | AM79C930_GCR_ECINT);
400 	awi_write_1(sc, AWI_DIS_PWRDN, 1);
401 	ocansleep = sc->sc_cansleep;
402 	sc->sc_cansleep = 0;
403 
404 	for (;;) {
405 		if ((error = awi_intr_lock(sc)) != 0)
406 			break;
407 		status = awi_read_1(sc, AWI_INTSTAT);
408 		awi_write_1(sc, AWI_INTSTAT, 0);
409 		awi_write_1(sc, AWI_INTSTAT, 0);
410 		status |= awi_read_1(sc, AWI_INTSTAT2) << 8;
411 		awi_write_1(sc, AWI_INTSTAT2, 0);
412 		DELAY(10);
413 		awi_intr_unlock(sc);
414 		if (!sc->sc_cmd_inprog)
415 			status &= ~AWI_INT_CMD;	/* make sure */
416 		if (status == 0)
417 			break;
418 #ifdef AWI_DEBUG
419 		if (awi_debug > 1) {
420 			int i;
421 
422 			printf("awi_intr: status 0x%04x", status);
423 			for (i = 0; i < sizeof(intname)/sizeof(intname[0]);
424 			    i++) {
425 				if (status & (1 << i))
426 					printf(" %s", intname[i]);
427 			}
428 			printf("\n");
429 		}
430 #endif
431 		handled = 1;
432 		if (status & AWI_INT_RX)
433 			awi_rx_int(sc);
434 		if (status & AWI_INT_TX)
435 			awi_tx_int(sc);
436 		if (status & AWI_INT_CMD)
437 			awi_cmd_done(sc);
438 		if (status & AWI_INT_SCAN_CMPLT) {
439 			if (sc->sc_ic.ic_state == IEEE80211_S_SCAN)
440 				ieee80211_next_scan(&sc->sc_ic.ic_if);
441 		}
442 	}
443 	sc->sc_cansleep = ocansleep;
444 	am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_DISPWDN);
445 	awi_write_1(sc, AWI_DIS_PWRDN, 0);
446 	return handled;
447 }
448 
449 static int
450 awi_init(struct ifnet *ifp)
451 {
452 	struct awi_softc *sc = ifp->if_softc;
453 	struct ieee80211com *ic = &sc->sc_ic;
454 	struct ieee80211_bss *bs = &ic->ic_bss;
455 	int i, error;
456 
457 	DPRINTF(("awi_init: enabled=%d\n", sc->sc_enabled));
458 	if (sc->sc_enabled) {
459 		awi_stop(ifp, 0);
460 	} else {
461 		if (sc->sc_enable)
462 			(*sc->sc_enable)(sc);
463 		sc->sc_enabled = 1;
464 		if ((error = awi_hw_init(sc)) != 0) {
465 			awi_stop(ifp, 1);
466 			return error;
467 		}
468 	}
469 	ic->ic_state = IEEE80211_S_INIT;
470 
471 	sc->sc_mib_local.Network_Mode =
472 	    (ic->ic_flags & IEEE80211_F_ADHOC) ? 0 : 1;
473 
474 	if ((error = awi_mode_init(sc)) != 0) {
475 		DPRINTF(("awi_init: awi_mode_init failed %d\n", error));
476 		awi_stop(ifp, 1);
477 		return error;
478 	}
479 
480 	/* start transmitter */
481 	sc->sc_txdone = sc->sc_txnext = sc->sc_txbase;
482 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_START, 0);
483 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_NEXT, 0);
484 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_LENGTH, 0);
485 	awi_write_1(sc, sc->sc_txbase + AWI_TXD_RATE, 0);
486 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_NDA, 0);
487 	awi_write_4(sc, sc->sc_txbase + AWI_TXD_NRA, 0);
488 	awi_write_1(sc, sc->sc_txbase + AWI_TXD_STATE, 0);
489 	awi_write_4(sc, AWI_CA_TX_DATA, sc->sc_txbase);
490 	awi_write_4(sc, AWI_CA_TX_MGT, 0);
491 	awi_write_4(sc, AWI_CA_TX_BCAST, 0);
492 	awi_write_4(sc, AWI_CA_TX_PS, 0);
493 	awi_write_4(sc, AWI_CA_TX_CF, 0);
494 	if ((error = awi_cmd(sc, AWI_CMD_INIT_TX, AWI_WAIT)) != 0) {
495 		DPRINTF(("awi_init: failed to start transmitter: %d\n", error));
496 		awi_stop(ifp, 1);
497 		return error;
498 	}
499 
500 	/* start receiver */
501 	if ((error = awi_cmd(sc, AWI_CMD_INIT_RX, AWI_WAIT)) != 0) {
502 		DPRINTF(("awi_init: failed to start receiver: %d\n", error));
503 		awi_stop(ifp, 1);
504 		return error;
505 	}
506 	sc->sc_rxdoff = awi_read_4(sc, AWI_CA_IRX_DATA_DESC);
507 	sc->sc_rxmoff = awi_read_4(sc, AWI_CA_IRX_PS_DESC);
508 
509 	ifp->if_flags |= IFF_RUNNING;
510 	ifp->if_flags &= ~IFF_OACTIVE;
511 
512 	if ((sc->sc_ic.ic_flags & IEEE80211_F_ADHOC) && sc->sc_no_bssid) {
513 		bs->bs_chan = ic->ic_ibss_chan;
514 		bs->bs_intval = ic->ic_lintval;
515 		bs->bs_nrate = 0;
516 		for (i = 0; i < IEEE80211_RATE_SIZE; i++) {
517 			if (ic->ic_sup_rates[i])
518 				bs->bs_rates[bs->bs_nrate++] =
519 				    ic->ic_sup_rates[i];
520 		}
521 		memcpy(bs->bs_macaddr, ic->ic_myaddr, IEEE80211_ADDR_LEN);
522 		memset(bs->bs_bssid, 0, IEEE80211_ADDR_LEN);
523 		bs->bs_esslen = 0;
524 		ic->ic_flags |= IEEE80211_F_SIBSS;
525 		ic->ic_state = IEEE80211_S_SCAN;	/*XXX*/
526 		sc->sc_substate = AWI_ST_NONE;
527 		ieee80211_new_state(&ic->ic_if, IEEE80211_S_RUN, -1);
528 	} else {
529 		bs->bs_chan = sc->sc_cur_chan;
530 		ieee80211_new_state(&ic->ic_if, IEEE80211_S_SCAN, -1);
531 	}
532 	return 0;
533 }
534 
535 static void
536 awi_stop(struct ifnet *ifp, int disable)
537 {
538 	struct awi_softc *sc = ifp->if_softc;
539 
540 	if (!sc->sc_enabled)
541 		return;
542 
543 	DPRINTF(("awi_stop(%d)\n", disable));
544 
545 	ieee80211_new_state(&sc->sc_ic.ic_if, IEEE80211_S_INIT, -1);
546 
547 	if (!sc->sc_invalid) {
548 		if (sc->sc_cmd_inprog)
549 			(void)awi_cmd_wait(sc);
550 		(void)awi_cmd(sc, AWI_CMD_KILL_RX, AWI_WAIT);
551 		sc->sc_cmd_inprog = AWI_CMD_FLUSH_TX;
552 		awi_write_1(sc, AWI_CA_FTX_DATA, 1);
553 		awi_write_1(sc, AWI_CA_FTX_MGT, 0);
554 		awi_write_1(sc, AWI_CA_FTX_BCAST, 0);
555 		awi_write_1(sc, AWI_CA_FTX_PS, 0);
556 		awi_write_1(sc, AWI_CA_FTX_CF, 0);
557 		(void)awi_cmd(sc, AWI_CMD_FLUSH_TX, AWI_WAIT);
558 	}
559 	ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
560 	ifp->if_timer = 0;
561 	sc->sc_tx_timer = sc->sc_rx_timer = 0;
562 	if (sc->sc_rxpend != NULL) {
563 		m_freem(sc->sc_rxpend);
564 		sc->sc_rxpend = NULL;
565 	}
566 	IFQ_PURGE(&ifp->if_snd);
567 
568 	if (disable) {
569 		if (sc->sc_disable)
570 			(*sc->sc_disable)(sc);
571 		sc->sc_enabled = 0;
572 	}
573 }
574 
575 static void
576 awi_start(struct ifnet *ifp)
577 {
578 	struct awi_softc *sc = ifp->if_softc;
579 	struct ieee80211com *ic = &sc->sc_ic;
580 	struct mbuf *m, *m0;
581 	int len;
582 	u_int32_t txd, frame, ntxd;
583 	u_int8_t rate;
584 
585 	if (!sc->sc_enabled || sc->sc_invalid)
586 		return;
587 
588 	for (;;) {
589 		txd = sc->sc_txnext;
590 		IF_POLL(&ic->ic_mgtq, m0);
591 		if (m0 != NULL) {
592 			if (awi_next_txd(sc, m0->m_pkthdr.len, &frame, &ntxd)) {
593 				ifp->if_flags |= IFF_OACTIVE;
594 				break;
595 			}
596 			IF_DEQUEUE(&ic->ic_mgtq, m0);
597 		} else {
598 			if (ic->ic_state != IEEE80211_S_RUN)
599 				break;
600 			IFQ_POLL(&ifp->if_snd, m0);
601 			if (m0 == NULL)
602 				break;
603 			/*
604 			 * Need to calculate the real length to determine
605 			 * if the transmit buffer has a room for the packet.
606 			 */
607 			len = m0->m_pkthdr.len + sizeof(struct ieee80211_frame);
608 			if (!(ifp->if_flags & IFF_LINK0) && !sc->sc_adhoc_ap)
609 				len += sizeof(struct llc) -
610 				    sizeof(struct ether_header);
611 			if (ic->ic_flags & IEEE80211_F_WEPON)
612 				len += IEEE80211_WEP_IVLEN +
613 				    IEEE80211_WEP_KIDLEN + IEEE80211_WEP_CRCLEN;
614 			if (awi_next_txd(sc, len, &frame, &ntxd)) {
615 				ifp->if_flags |= IFF_OACTIVE;
616 				break;
617 			}
618 			IFQ_DEQUEUE(&ifp->if_snd, m0);
619 			ifp->if_opackets++;
620 #if NBPFILTER > 0
621 			if (ifp->if_bpf)
622 				bpf_mtap(ifp->if_bpf, m0);
623 #endif
624 			if ((ifp->if_flags & IFF_LINK0) || sc->sc_adhoc_ap)
625 				m0 = awi_ether_encap(sc, m0);
626 			else
627 				m0 = ieee80211_encap(ifp, m0);
628 			if ((ic->ic_flags & IEEE80211_F_WEPON) && m0 != NULL)
629 				m0 = ieee80211_wep_crypt(ifp, m0, 1);
630 			if (m0 == NULL) {
631 				ifp->if_oerrors++;
632 				continue;
633 			}
634 #ifdef DIAGNOSTIC
635 			if (m0->m_pkthdr.len != len) {
636 				printf("%s: length %d should be %d\n",
637 				    ifp->if_xname, m0->m_pkthdr.len, len);
638 				m_freem(m0);
639 				ifp->if_oerrors++;
640 				continue;
641 			}
642 #endif
643 		}
644 
645 		if ((ifp->if_flags & IFF_DEBUG) && (ifp->if_flags & IFF_LINK2))
646 			ieee80211_dump_pkt(m0->m_data, m0->m_len,
647 			    ic->ic_bss.bs_rates[ic->ic_bss.bs_txrate] &
648 			    IEEE80211_RATE_VAL, -1);
649 
650 		for (m = m0, len = 0; m != NULL; m = m->m_next) {
651 			awi_write_bytes(sc, frame + len, mtod(m, u_int8_t *),
652 			    m->m_len);
653 			len += m->m_len;
654 		}
655 		m_freem(m0);
656 		rate = (ic->ic_bss.bs_rates[ic->ic_bss.bs_txrate] &
657 		    IEEE80211_RATE_VAL) * 5;
658 		awi_write_1(sc, ntxd + AWI_TXD_STATE, 0);
659 		awi_write_4(sc, txd + AWI_TXD_START, frame);
660 		awi_write_4(sc, txd + AWI_TXD_NEXT, ntxd);
661 		awi_write_4(sc, txd + AWI_TXD_LENGTH, len);
662 		awi_write_1(sc, txd + AWI_TXD_RATE, rate);
663 		awi_write_4(sc, txd + AWI_TXD_NDA, 0);
664 		awi_write_4(sc, txd + AWI_TXD_NRA, 0);
665 		awi_write_1(sc, txd + AWI_TXD_STATE, AWI_TXD_ST_OWN);
666 		sc->sc_txnext = ntxd;
667 
668 		sc->sc_tx_timer = 5;
669 		ifp->if_timer = 1;
670 	}
671 }
672 
673 static void
674 awi_watchdog(struct ifnet *ifp)
675 {
676 	struct awi_softc *sc = ifp->if_softc;
677 	u_int32_t prevdone;
678 	int ocansleep;
679 
680 	ifp->if_timer = 0;
681 	if (!sc->sc_enabled || sc->sc_invalid)
682 		return;
683 
684 	ocansleep = sc->sc_cansleep;
685 	sc->sc_cansleep = 0;
686 	if (sc->sc_tx_timer) {
687 		if (--sc->sc_tx_timer == 0) {
688 			printf("%s: device timeout\n", ifp->if_xname);
689 			prevdone = sc->sc_txdone;
690 			awi_tx_int(sc);
691 			if (sc->sc_txdone == prevdone) {
692 				ifp->if_oerrors++;
693 				awi_init(ifp);
694 				goto out;
695 			}
696 		}
697 		ifp->if_timer = 1;
698 	}
699 	if (sc->sc_rx_timer) {
700 		if (--sc->sc_rx_timer == 0) {
701 			if (sc->sc_ic.ic_state == IEEE80211_S_RUN) {
702 				ieee80211_new_state(ifp, IEEE80211_S_SCAN, -1);
703 				goto out;
704 			}
705 		} else
706 			ifp->if_timer = 1;
707 	}
708 	/* TODO: rate control */
709 	ieee80211_watchdog(ifp);
710   out:
711 	sc->sc_cansleep = ocansleep;
712 }
713 
714 static int
715 awi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
716 {
717 	struct awi_softc *sc = ifp->if_softc;
718 	struct ifreq *ifr = (struct ifreq *)data;
719 	int s, error;
720 
721 	s = splnet();
722 	/* serialize ioctl, since we may sleep */
723 	if ((error = awi_lock(sc)) != 0)
724 		goto cantlock;
725 
726 	switch (cmd) {
727 	case SIOCSIFFLAGS:
728 		if (ifp->if_flags & IFF_UP) {
729 			if (sc->sc_enabled) {
730 				/*
731 				 * To avoid rescanning another access point,
732 				 * do not call awi_init() here.  Instead,
733 				 * only reflect promisc mode settings.
734 				 */
735 				error = awi_mode_init(sc);
736 			} else
737 				error = awi_init(ifp);
738 		} else if (sc->sc_enabled)
739 			awi_stop(ifp, 1);
740 		break;
741 	case SIOCSIFMEDIA:
742 	case SIOCGIFMEDIA:
743 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
744 		break;
745 	case SIOCADDMULTI:
746 	case SIOCDELMULTI:
747 		error = (cmd == SIOCADDMULTI) ?
748 		    ether_addmulti(ifr, &sc->sc_ic.ic_ec) :
749 		    ether_delmulti(ifr, &sc->sc_ic.ic_ec);
750 		if (error == ENETRESET) {
751 			/* do not rescan */
752 			if (sc->sc_enabled)
753 				error = awi_mode_init(sc);
754 			else
755 				error = 0;
756 		}
757 		break;
758 	default:
759 		error = ieee80211_ioctl(ifp, cmd, data);
760 		if (error == ENETRESET) {
761 			if (sc->sc_enabled)
762 				error = awi_init(ifp);
763 			else
764 				error = 0;
765 		}
766 		break;
767 	}
768 	awi_unlock(sc);
769   cantlock:
770 	splx(s);
771 	return error;
772 }
773 
774 /*
775  * Called from ifmedia_ioctl via awi_ioctl with lock obtained.
776  */
777 static int
778 awi_media_change(struct ifnet *ifp)
779 {
780 	struct awi_softc *sc = ifp->if_softc;
781 	struct ieee80211com *ic = &sc->sc_ic;
782 	struct ifmedia_entry *ime;
783 	int i, rate, error = 0;
784 
785 	ime = sc->sc_media.ifm_cur;
786 	if (IFM_SUBTYPE(ime->ifm_media) == IFM_AUTO) {
787 		ic->ic_fixed_rate = -1;
788 	} else {
789 		rate = awi_media_opt2rate(sc, ime->ifm_media);
790 		if (rate == 0)
791 			return EINVAL;
792 		for (i = 0; i < IEEE80211_RATE_SIZE; i++) {
793 			if ((ic->ic_sup_rates[i] & IEEE80211_RATE_VAL) == rate)
794 				break;
795 		}
796 		if (i == IEEE80211_RATE_SIZE)
797 			return EINVAL;
798 		ic->ic_fixed_rate = i;
799 	}
800 
801 	/*
802 	 *  ADHOC,-FLAG0	ADHOC,  !no_bssid, !adhoc_ap	IBSS
803 	 *  ADHOC, FLAG0	ADHOC    no_bssid, !adhoc_ap	WaveLAN adhoc
804 	 * -ADHOC,-FLAG0	~ADHOC, !no_bssid, !adhoc_ap	Infra
805 	 * -ADHOC, FLAG0	ADHOC,  !no_bssid,  adhoc_ap	Melco old AP
806 	 *						also LINK0
807 	 */
808 	if (ime->ifm_media & IFM_IEEE80211_ADHOC) {
809 		if ((ic->ic_flags & IEEE80211_F_ADHOC) == 0) {
810 			ic->ic_flags |= IEEE80211_F_ADHOC;
811 			error = ENETRESET;
812 		}
813 		ic->ic_flags |= IEEE80211_F_IBSSON;
814 		if (sc->sc_mib_phy.IEEE_PHY_Type != AWI_PHY_TYPE_FH &&
815 		    (ime->ifm_media & IFM_FLAG0)) {
816 			if (sc->sc_no_bssid == 0) {
817 				sc->sc_no_bssid = 1;
818 				error = ENETRESET;
819 			}
820 		} else {
821 			if (sc->sc_no_bssid) {
822 				sc->sc_no_bssid = 0;
823 				error = ENETRESET;
824 			}
825 		}
826 		if (sc->sc_adhoc_ap) {
827 			sc->sc_adhoc_ap = 0;
828 			error = ENETRESET;
829 		}
830 	} else {
831 		ic->ic_flags &= ~IEEE80211_F_IBSSON;
832 		if (sc->sc_no_bssid) {
833 			sc->sc_no_bssid = 0;
834 			error = ENETRESET;
835 		}
836 		if (ime->ifm_media & IFM_FLAG0) {
837 			if ((ic->ic_flags & IEEE80211_F_ADHOC) == 0) {
838 				ic->ic_flags |= IEEE80211_F_ADHOC;
839 				error = ENETRESET;
840 			}
841 			if (!sc->sc_adhoc_ap) {
842 				sc->sc_adhoc_ap = 1;
843 				error = ENETRESET;
844 			}
845 		} else {
846 			if (ic->ic_flags & IEEE80211_F_ADHOC) {
847 				ic->ic_flags &= ~IEEE80211_F_ADHOC;
848 				error = ENETRESET;
849 			}
850 			if (sc->sc_adhoc_ap) {
851 				sc->sc_adhoc_ap = 0;
852 				error = ENETRESET;
853 			}
854 		}
855 	}
856 	if (error == ENETRESET) {
857 		if (sc->sc_enabled)
858 			error = awi_init(ifp);
859 		else
860 			error = 0;
861 	}
862 	return error;
863 }
864 
865 static void
866 awi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
867 {
868 	struct awi_softc *sc = ifp->if_softc;
869 	struct ieee80211com *ic = &sc->sc_ic;
870 	int rate;
871 
872 	imr->ifm_status = IFM_AVALID;
873 	if (ic->ic_state == IEEE80211_S_RUN)
874 		imr->ifm_status |= IFM_ACTIVE;
875 	imr->ifm_active = IFM_IEEE80211;
876 	if (ic->ic_state == IEEE80211_S_RUN)
877 		rate = ic->ic_bss.bs_rates[ic->ic_bss.bs_txrate] &
878 		    IEEE80211_RATE_VAL;
879 	else {
880 		if (ic->ic_fixed_rate == -1)
881 			rate = 0;
882 		else
883 			rate = ic->ic_sup_rates[ic->ic_fixed_rate] &
884 			    IEEE80211_RATE_VAL;
885 	}
886 	imr->ifm_active |= awi_media_rate2opt(sc, rate);
887 	if (ic->ic_flags & IEEE80211_F_ADHOC) {
888 		if (sc->sc_adhoc_ap)
889 			imr->ifm_active |= IFM_FLAG0;
890 		else {
891 			imr->ifm_active |= IFM_IEEE80211_ADHOC;
892 			if (sc->sc_no_bssid)
893 				imr->ifm_active |= IFM_FLAG0;
894 		}
895 	}
896 }
897 
898 static int
899 awi_mode_init(struct awi_softc *sc)
900 {
901 	struct ifnet *ifp = &sc->sc_ic.ic_if;
902 	int n, error;
903 	struct ether_multi *enm;
904 	struct ether_multistep step;
905 
906 	/* reinitialize muticast filter */
907 	n = 0;
908 	sc->sc_mib_local.Accept_All_Multicast_Dis = 0;
909 	if (ifp->if_flags & IFF_PROMISC) {
910 		sc->sc_mib_mac.aPromiscuous_Enable = 1;
911 		goto set_mib;
912 	}
913 	sc->sc_mib_mac.aPromiscuous_Enable = 0;
914 	ETHER_FIRST_MULTI(step, &sc->sc_ic.ic_ec, enm);
915 	while (enm != NULL) {
916 		if (n == AWI_GROUP_ADDR_SIZE ||
917 		    memcmp(enm->enm_addrlo, enm->enm_addrhi, IEEE80211_ADDR_LEN)
918 		    != 0)
919 			goto set_mib;
920 		memcpy(sc->sc_mib_addr.aGroup_Addresses[n], enm->enm_addrlo,
921 		    IEEE80211_ADDR_LEN);
922 		n++;
923 		ETHER_NEXT_MULTI(step, enm);
924 	}
925 	for (; n < AWI_GROUP_ADDR_SIZE; n++)
926 		memset(sc->sc_mib_addr.aGroup_Addresses[n], 0, IEEE80211_ADDR_LEN);
927 	sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
928 
929   set_mib:
930 	if (sc->sc_mib_local.Accept_All_Multicast_Dis)
931 		ifp->if_flags &= ~IFF_ALLMULTI;
932 	else
933 		ifp->if_flags |= IFF_ALLMULTI;
934 	sc->sc_mib_mgt.Wep_Required =
935 	    (sc->sc_ic.ic_flags & IEEE80211_F_WEPON) ? 1 : 0;
936 
937 	if ((error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_LOCAL, AWI_WAIT)) ||
938 	    (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_ADDR, AWI_WAIT)) ||
939 	    (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MAC, AWI_WAIT)) ||
940 	    (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT, AWI_WAIT)) ||
941 	    (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_PHY, AWI_WAIT))) {
942 		DPRINTF(("awi_mode_init: MIB set failed: %d\n", error));
943 		return error;
944 	}
945 	return 0;
946 }
947 
948 /* XXX should be moved to if_ieee80211subr.c ? */
949 static int
950 awi_media_rate2opt(struct awi_softc *sc, int rate)
951 {
952 	int mword;
953 
954 	mword = 0;
955 	switch (rate & IEEE80211_RATE_VAL) {
956 	case 2:
957 		if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
958 			mword = IFM_IEEE80211_FH1;
959 		else
960 			mword = IFM_IEEE80211_DS1;
961 		break;
962 	case 4:
963 		if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
964 			mword = IFM_IEEE80211_FH2;
965 		else
966 			mword = IFM_IEEE80211_DS2;
967 		break;
968 	case 11:
969 		if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_DS)
970 			mword = IFM_IEEE80211_DS5;
971 		break;
972 	case 22:
973 		if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_DS)
974 			mword = IFM_IEEE80211_DS11;
975 		break;
976 	}
977 	return mword;
978 }
979 
980 static int
981 awi_media_opt2rate(struct awi_softc *sc, int opt)
982 {
983 	int rate;
984 
985 	rate = 0;
986 	switch (IFM_SUBTYPE(opt)) {
987 	case IFM_IEEE80211_FH1:
988 	case IFM_IEEE80211_FH2:
989 		if (sc->sc_mib_phy.IEEE_PHY_Type != AWI_PHY_TYPE_FH)
990 			return 0;
991 		break;
992 	case IFM_IEEE80211_DS1:
993 	case IFM_IEEE80211_DS2:
994 	case IFM_IEEE80211_DS5:
995 	case IFM_IEEE80211_DS11:
996 		if (sc->sc_mib_phy.IEEE_PHY_Type != AWI_PHY_TYPE_DS)
997 			return 0;
998 		break;
999 	}
1000 
1001 	switch (IFM_SUBTYPE(opt)) {
1002 	case IFM_IEEE80211_FH1:
1003 	case IFM_IEEE80211_DS1:
1004 		rate = 2;
1005 		break;
1006 	case IFM_IEEE80211_FH2:
1007 	case IFM_IEEE80211_DS2:
1008 		rate = 4;
1009 		break;
1010 	case IFM_IEEE80211_DS5:
1011 		rate = 11;
1012 		break;
1013 	case IFM_IEEE80211_DS11:
1014 		rate = 22;
1015 		break;
1016 	}
1017 	return rate;
1018 }
1019 
1020 static void
1021 awi_rx_int(struct awi_softc *sc)
1022 {
1023 	struct ifnet *ifp = &sc->sc_ic.ic_if;
1024 	u_int8_t state, rate, rssi;
1025 	u_int16_t len;
1026 	u_int32_t frame, next, timoff, rxoff;
1027 	struct mbuf *m;
1028 
1029 	rxoff = sc->sc_rxdoff;
1030 	for (;;) {
1031 		state = awi_read_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE);
1032 		if (state & AWI_RXD_ST_OWN)
1033 			break;
1034 		if (!(state & AWI_RXD_ST_CONSUMED)) {
1035 			if (state & AWI_RXD_ST_RXERROR) {
1036 				ifp->if_ierrors++;
1037 				goto rx_next;
1038 			}
1039 			len    = awi_read_2(sc, rxoff + AWI_RXD_LEN);
1040 			rate   = awi_read_1(sc, rxoff + AWI_RXD_RATE);
1041 			rssi   = awi_read_1(sc, rxoff + AWI_RXD_RSSI);
1042 			frame  = awi_read_4(sc, rxoff + AWI_RXD_START_FRAME) &
1043 			    0x7fff;
1044 			timoff = awi_read_4(sc, rxoff + AWI_RXD_LOCALTIME);
1045 			m = awi_devget(sc, frame, len);
1046 			if (m == NULL) {
1047 				ifp->if_ierrors++;
1048 				goto rx_next;
1049 			}
1050 			if (state & AWI_RXD_ST_LF) {
1051 				/* TODO check my bss */
1052 				if (!(sc->sc_ic.ic_flags & IEEE80211_F_SIBSS) &&
1053 				    sc->sc_ic.ic_state == IEEE80211_S_RUN) {
1054 					sc->sc_rx_timer = 10;
1055 					ifp->if_timer = 1;
1056 				}
1057 				if ((ifp->if_flags & IFF_DEBUG) &&
1058 				    (ifp->if_flags & IFF_LINK2))
1059 					ieee80211_dump_pkt(m->m_data, m->m_len,
1060 					    rate / 5, rssi);
1061 				if ((ifp->if_flags & IFF_LINK0) ||
1062 				    sc->sc_adhoc_ap)
1063 					m = awi_ether_modcap(sc, m);
1064 				if (m == NULL)
1065 					ifp->if_ierrors++;
1066 				else
1067 					ieee80211_input(ifp, m, rssi, timoff);
1068 			} else
1069 				sc->sc_rxpend = m;
1070   rx_next:
1071 			state |= AWI_RXD_ST_CONSUMED;
1072 			awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
1073 		}
1074 		next = awi_read_4(sc, rxoff + AWI_RXD_NEXT);
1075 		if (next & AWI_RXD_NEXT_LAST)
1076 			break;
1077 		/* make sure the next pointer is correct */
1078 		if (next != awi_read_4(sc, rxoff + AWI_RXD_NEXT))
1079 			break;
1080 		state |= AWI_RXD_ST_OWN;
1081 		awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
1082 		rxoff = next & 0x7fff;
1083 	}
1084 	sc->sc_rxdoff = rxoff;
1085 }
1086 
1087 static void
1088 awi_tx_int(struct awi_softc *sc)
1089 {
1090 	struct ifnet *ifp = &sc->sc_ic.ic_if;
1091 	u_int8_t flags;
1092 
1093 	while (sc->sc_txdone != sc->sc_txnext) {
1094 		flags = awi_read_1(sc, sc->sc_txdone + AWI_TXD_STATE);
1095 		if ((flags & AWI_TXD_ST_OWN) || !(flags & AWI_TXD_ST_DONE))
1096 			break;
1097 		if (flags & AWI_TXD_ST_ERROR)
1098 			ifp->if_oerrors++;
1099 		sc->sc_txdone = awi_read_4(sc, sc->sc_txdone + AWI_TXD_NEXT) &
1100 		    0x7fff;
1101 	}
1102 	DPRINTF2(("awi_txint: txdone %d txnext %d txbase %d txend %d\n",
1103 	    sc->sc_txdone, sc->sc_txnext, sc->sc_txbase, sc->sc_txend));
1104 	sc->sc_tx_timer = 0;
1105 	ifp->if_flags &= ~IFF_OACTIVE;
1106 	awi_start(ifp);
1107 }
1108 
1109 static struct mbuf *
1110 awi_devget(struct awi_softc *sc, u_int32_t off, u_int16_t len)
1111 {
1112 	struct ifnet *ifp = &sc->sc_ic.ic_if;
1113 	struct mbuf *m;
1114 	struct mbuf *top, **mp;
1115 	u_int tlen;
1116 
1117 	top = sc->sc_rxpend;
1118 	mp = &top;
1119 	if (top != NULL) {
1120 		sc->sc_rxpend = NULL;
1121 		top->m_pkthdr.len += len;
1122 		m = top;
1123 		while (*mp != NULL) {
1124 			m = *mp;
1125 			mp = &m->m_next;
1126 		}
1127 		if (m->m_flags & M_EXT)
1128 			tlen = m->m_ext.ext_size;
1129 		else if (m->m_flags & M_PKTHDR)
1130 			tlen = MHLEN;
1131 		else
1132 			tlen = MLEN;
1133 		tlen -= m->m_len;
1134 		if (tlen > len)
1135 			tlen = len;
1136 		awi_read_bytes(sc, off, mtod(m, u_int8_t *) + m->m_len, tlen);
1137 		off += tlen;
1138 		len -= tlen;
1139 	}
1140 
1141 	while (len > 0) {
1142 		if (top == NULL) {
1143 			MGETHDR(m, M_DONTWAIT, MT_DATA);
1144 			if (m == NULL)
1145 				return NULL;
1146 			m->m_pkthdr.rcvif = ifp;
1147 			m->m_pkthdr.len = len;
1148 			m->m_len = MHLEN;
1149 			m->m_flags |= M_HASFCS;
1150 		} else {
1151 			MGET(m, M_DONTWAIT, MT_DATA);
1152 			if (m == NULL) {
1153 				m_freem(top);
1154 				return NULL;
1155 			}
1156 			m->m_len = MLEN;
1157 		}
1158 		if (len >= MINCLSIZE) {
1159 			MCLGET(m, M_DONTWAIT);
1160 			if (m->m_flags & M_EXT)
1161 				m->m_len = m->m_ext.ext_size;
1162 		}
1163 		if (top == NULL) {
1164 			int hdrlen = sizeof(struct ieee80211_frame) +
1165 			    sizeof(struct llc);
1166 			caddr_t newdata = (caddr_t)
1167 			    ALIGN(m->m_data + hdrlen) - hdrlen;
1168 			m->m_len -= newdata - m->m_data;
1169 			m->m_data = newdata;
1170 		}
1171 		if (m->m_len > len)
1172 			m->m_len = len;
1173 		awi_read_bytes(sc, off, mtod(m, u_int8_t *), m->m_len);
1174 		off += m->m_len;
1175 		len -= m->m_len;
1176 		*mp = m;
1177 		mp = &m->m_next;
1178 	}
1179 	return top;
1180 }
1181 
1182 /*
1183  * Initialize hardware and start firmware to accept commands.
1184  * Called everytime after power on firmware.
1185  */
1186 
1187 static int
1188 awi_hw_init(struct awi_softc *sc)
1189 {
1190 	u_int8_t status;
1191 	u_int16_t intmask;
1192 	int i, error;
1193 
1194 	sc->sc_enab_intr = 0;
1195 	sc->sc_invalid = 0;	/* XXX: really? */
1196 	awi_drvstate(sc, AWI_DRV_RESET);
1197 
1198 	/* reset firmware */
1199 	am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_CORESET);
1200 	DELAY(100);
1201 	awi_write_1(sc, AWI_SELFTEST, 0);
1202 	awi_write_1(sc, AWI_CMD, 0);
1203 	awi_write_1(sc, AWI_BANNER, 0);
1204 	am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_CORESET);
1205 	DELAY(100);
1206 
1207 	/* wait for selftest completion */
1208 	for (i = 0; ; i++) {
1209 		if (i >= AWI_SELFTEST_TIMEOUT*hz/1000) {
1210 			printf("%s: failed to complete selftest (timeout)\n",
1211 			    sc->sc_dev.dv_xname);
1212 			return ENXIO;
1213 		}
1214 		status = awi_read_1(sc, AWI_SELFTEST);
1215 		if ((status & 0xf0) == 0xf0)
1216 			break;
1217 		if (sc->sc_cansleep) {
1218 			sc->sc_sleep_cnt++;
1219 			(void)tsleep(sc, PWAIT, "awitst", 1);
1220 			sc->sc_sleep_cnt--;
1221 		} else {
1222 			DELAY(1000*1000/hz);
1223 		}
1224 	}
1225 	if (status != AWI_SELFTEST_PASSED) {
1226 		printf("%s: failed to complete selftest (code %x)\n",
1227 		    sc->sc_dev.dv_xname, status);
1228 		return ENXIO;
1229 	}
1230 
1231 	/* check banner to confirm firmware write it */
1232 	awi_read_bytes(sc, AWI_BANNER, sc->sc_banner, AWI_BANNER_LEN);
1233 	if (memcmp(sc->sc_banner, "PCnetMobile:", 12) != 0) {
1234 		printf("%s: failed to complete selftest (bad banner)\n",
1235 		    sc->sc_dev.dv_xname);
1236 		for (i = 0; i < AWI_BANNER_LEN; i++)
1237 			printf("%s%02x", i ? ":" : "\t", sc->sc_banner[i]);
1238 		printf("\n");
1239 		return ENXIO;
1240 	}
1241 
1242 	/* initializing interrupt */
1243 	sc->sc_enab_intr = 1;
1244 	error = awi_intr_lock(sc);
1245 	if (error)
1246 		return error;
1247 	intmask = AWI_INT_GROGGY | AWI_INT_SCAN_CMPLT |
1248 	    AWI_INT_TX | AWI_INT_RX | AWI_INT_CMD;
1249 	awi_write_1(sc, AWI_INTMASK, ~intmask & 0xff);
1250 	awi_write_1(sc, AWI_INTMASK2, 0);
1251 	awi_write_1(sc, AWI_INTSTAT, 0);
1252 	awi_write_1(sc, AWI_INTSTAT2, 0);
1253 	awi_intr_unlock(sc);
1254 	am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_ENECINT);
1255 
1256 	/* issuing interface test command */
1257 	error = awi_cmd(sc, AWI_CMD_NOP, AWI_WAIT);
1258 	if (error) {
1259 		printf("%s: failed to complete selftest", sc->sc_dev.dv_xname);
1260 		if (error == ENXIO)
1261 			printf(" (no hardware)\n");
1262 		else if (error != EWOULDBLOCK)
1263 			printf(" (error %d)\n", error);
1264 		else if (sc->sc_cansleep)
1265 			printf(" (lost interrupt)\n");
1266 		else
1267 			printf(" (command timeout)\n");
1268 	}
1269 	return error;
1270 }
1271 
1272 /*
1273  * Extract the factory default MIB value from firmware and assign the driver
1274  * default value.
1275  * Called once at attaching the interface.
1276  */
1277 
1278 static int
1279 awi_init_mibs(struct awi_softc *sc)
1280 {
1281 	int i, error;
1282 	struct awi_chanset *cs;
1283 
1284 	if ((error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_LOCAL, AWI_WAIT)) ||
1285 	    (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_ADDR, AWI_WAIT)) ||
1286 	    (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MAC, AWI_WAIT)) ||
1287 	    (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MGT, AWI_WAIT)) ||
1288 	    (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_PHY, AWI_WAIT))) {
1289 		printf("%s: failed to get default mib value (error %d)\n",
1290 		    sc->sc_dev.dv_xname, error);
1291 		return error;
1292 	}
1293 
1294 	memset(&sc->sc_ic.ic_chan_avail, 0, sizeof(sc->sc_ic.ic_chan_avail));
1295 	for (cs = awi_chanset; ; cs++) {
1296 		if (cs->cs_type == 0) {
1297 			printf("%s: failed to set available channel\n",
1298 			    sc->sc_dev.dv_xname);
1299 			return ENXIO;
1300 		}
1301 		if (cs->cs_type == sc->sc_mib_phy.IEEE_PHY_Type &&
1302 		    cs->cs_region == sc->sc_mib_phy.aCurrent_Reg_Domain)
1303 			break;
1304 	}
1305 	if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1306 		for (i = cs->cs_min; i <= cs->cs_max; i++) {
1307 			setbit(sc->sc_ic.ic_chan_avail,
1308 			    IEEE80211_FH_CHAN(i % 3 + 1, i));
1309 			/*
1310 			 * According to the IEEE 802.11 specification,
1311 			 * hop pattern parameter for FH phy should be
1312 			 * incremented by 3 for given hop chanset, i.e.,
1313 			 * the chanset parameter is calculated for given
1314 			 * hop patter.  However, BayStack 650 Access Points
1315 			 * apparently use fixed hop chanset parameter value
1316 			 * 1 for any hop pattern.  So we also try this
1317 			 * combination of hop chanset and pattern.
1318 			 */
1319 			setbit(sc->sc_ic.ic_chan_avail,
1320 			    IEEE80211_FH_CHAN(1, i));
1321 		}
1322 	} else {
1323 		for (i = cs->cs_min; i <= cs->cs_max; i++)
1324 			setbit(sc->sc_ic.ic_chan_avail, i);
1325 	}
1326 	sc->sc_cur_chan = cs->cs_def;
1327 
1328 	memset(&sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
1329 	sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
1330 	sc->sc_mib_local.Fragmentation_Dis = 1;
1331 	sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
1332 	sc->sc_mib_local.Power_Saving_Mode_Dis = 1;
1333 
1334 	/* allocate buffers */
1335 	sc->sc_txbase = AWI_BUFFERS;
1336 	sc->sc_txend = sc->sc_txbase +
1337 	    (AWI_TXD_SIZE + sizeof(struct ieee80211_frame) +
1338 	    sizeof(struct ether_header) + ETHERMTU) * AWI_NTXBUFS;
1339 	LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Offset, sc->sc_txbase);
1340 	LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Size,
1341 	    sc->sc_txend - sc->sc_txbase);
1342 	LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Offset, sc->sc_txend);
1343 	LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Size,
1344 	    AWI_BUFFERS_END - sc->sc_txend);
1345 	sc->sc_mib_local.Network_Mode = 1;
1346 	sc->sc_mib_local.Acting_as_AP = 0;
1347 	return 0;
1348 }
1349 
1350 static int
1351 awi_chan_check(void *arg, u_char *chanreq)
1352 {
1353 	struct awi_softc *sc = arg;
1354 	int i;
1355 	struct awi_chanset *cs;
1356 	u_char chanlist[(IEEE80211_CHAN_MAX+1)/NBBY];
1357 
1358 	for (cs = awi_chanset; cs->cs_type != 0; cs++) {
1359 		if (cs->cs_type != sc->sc_mib_phy.IEEE_PHY_Type)
1360 			continue;
1361 		memset(chanlist, 0, sizeof(chanlist));
1362 		for (i = 0; ; i++) {
1363 			if (i == IEEE80211_CHAN_MAX) {
1364 				sc->sc_mib_phy.aCurrent_Reg_Domain =
1365 				    cs->cs_region;
1366 				memcpy(sc->sc_ic.ic_chan_avail, chanlist,
1367 				    sizeof(sc->sc_ic.ic_chan_avail));
1368 				sc->sc_cur_chan = cs->cs_def;
1369 				return 0;
1370 			}
1371 			if (i >= cs->cs_min && i <= cs->cs_max)
1372 				setbit(chanlist, i);
1373 			else if (isset(chanreq, i))
1374 				break;
1375 		}
1376 	}
1377 	return EINVAL;
1378 }
1379 
1380 static int
1381 awi_mib(struct awi_softc *sc, u_int8_t cmd, u_int8_t mib, int wflag)
1382 {
1383 	int error;
1384 	u_int8_t size, *ptr;
1385 
1386 	switch (mib) {
1387 	case AWI_MIB_LOCAL:
1388 		ptr = (u_int8_t *)&sc->sc_mib_local;
1389 		size = sizeof(sc->sc_mib_local);
1390 		break;
1391 	case AWI_MIB_ADDR:
1392 		ptr = (u_int8_t *)&sc->sc_mib_addr;
1393 		size = sizeof(sc->sc_mib_addr);
1394 		break;
1395 	case AWI_MIB_MAC:
1396 		ptr = (u_int8_t *)&sc->sc_mib_mac;
1397 		size = sizeof(sc->sc_mib_mac);
1398 		break;
1399 	case AWI_MIB_STAT:
1400 		ptr = (u_int8_t *)&sc->sc_mib_stat;
1401 		size = sizeof(sc->sc_mib_stat);
1402 		break;
1403 	case AWI_MIB_MGT:
1404 		ptr = (u_int8_t *)&sc->sc_mib_mgt;
1405 		size = sizeof(sc->sc_mib_mgt);
1406 		break;
1407 	case AWI_MIB_PHY:
1408 		ptr = (u_int8_t *)&sc->sc_mib_phy;
1409 		size = sizeof(sc->sc_mib_phy);
1410 		break;
1411 	default:
1412 		return EINVAL;
1413 	}
1414 	if (sc->sc_cmd_inprog) {
1415 		if ((error = awi_cmd_wait(sc)) != 0) {
1416 			if (error == EWOULDBLOCK)
1417 				DPRINTF(("awi_mib: cmd %d inprog",
1418 				    sc->sc_cmd_inprog));
1419 			return error;
1420 		}
1421 	}
1422 	sc->sc_cmd_inprog = cmd;
1423 	if (cmd == AWI_CMD_SET_MIB)
1424 		awi_write_bytes(sc, AWI_CA_MIB_DATA, ptr, size);
1425 	awi_write_1(sc, AWI_CA_MIB_TYPE, mib);
1426 	awi_write_1(sc, AWI_CA_MIB_SIZE, size);
1427 	awi_write_1(sc, AWI_CA_MIB_INDEX, 0);
1428 	if ((error = awi_cmd(sc, cmd, wflag)) != 0)
1429 		return error;
1430 	if (cmd == AWI_CMD_GET_MIB) {
1431 		awi_read_bytes(sc, AWI_CA_MIB_DATA, ptr, size);
1432 #ifdef AWI_DEBUG
1433 		if (awi_debug) {
1434 			int i;
1435 
1436 			printf("awi_mib: #%d:", mib);
1437 			for (i = 0; i < size; i++)
1438 				printf(" %02x", ptr[i]);
1439 			printf("\n");
1440 		}
1441 #endif
1442 	}
1443 	return 0;
1444 }
1445 
1446 static int
1447 awi_cmd(struct awi_softc *sc, u_int8_t cmd, int wflag)
1448 {
1449 	u_int8_t status;
1450 	int error = 0;
1451 #ifdef AWI_DEBUG
1452 	static const char *cmdname[] = {
1453 	    "IDLE", "NOP", "SET_MIB", "INIT_TX", "FLUSH_TX", "INIT_RX",
1454 	    "KILL_RX", "SLEEP", "WAKE", "GET_MIB", "SCAN", "SYNC", "RESUME"
1455 	};
1456 #endif
1457 
1458 #ifdef AWI_DEBUG
1459 	if (awi_debug > 1) {
1460 		if (cmd >= sizeof(cmdname)/sizeof(cmdname[0]))
1461 			printf("awi_cmd: #%d", cmd);
1462 		else
1463 			printf("awi_cmd: %s", cmdname[cmd]);
1464 		printf(" %s\n", wflag == AWI_NOWAIT ? "nowait" : "wait");
1465 	}
1466 #endif
1467 	sc->sc_cmd_inprog = cmd;
1468 	awi_write_1(sc, AWI_CMD_STATUS, AWI_STAT_IDLE);
1469 	awi_write_1(sc, AWI_CMD, cmd);
1470 	if (wflag == AWI_NOWAIT)
1471 		return EINPROGRESS;
1472 	if ((error = awi_cmd_wait(sc)) != 0)
1473 		return error;
1474 	status = awi_read_1(sc, AWI_CMD_STATUS);
1475 	awi_write_1(sc, AWI_CMD, 0);
1476 	switch (status) {
1477 	case AWI_STAT_OK:
1478 		break;
1479 	case AWI_STAT_BADPARM:
1480 		return EINVAL;
1481 	default:
1482 		printf("%s: command %d failed %x\n",
1483 		    sc->sc_dev.dv_xname, cmd, status);
1484 		return ENXIO;
1485 	}
1486 	return 0;
1487 }
1488 
1489 static int
1490 awi_cmd_wait(struct awi_softc *sc)
1491 {
1492 	int i, error = 0;
1493 
1494 	i = 0;
1495 	while (sc->sc_cmd_inprog) {
1496 		if (sc->sc_invalid)
1497 			return ENXIO;
1498 		if (awi_read_1(sc, AWI_CMD) != sc->sc_cmd_inprog) {
1499 			printf("%s: failed to access hardware\n",
1500 			    sc->sc_dev.dv_xname);
1501 			sc->sc_invalid = 1;
1502 			return ENXIO;
1503 		}
1504 		if (sc->sc_cansleep) {
1505 			sc->sc_sleep_cnt++;
1506 			error = tsleep(sc, PWAIT, "awicmd",
1507 			    AWI_CMD_TIMEOUT*hz/1000);
1508 			sc->sc_sleep_cnt--;
1509 		} else {
1510 			if (awi_read_1(sc, AWI_CMD_STATUS) != AWI_STAT_IDLE) {
1511 				awi_cmd_done(sc);
1512 				break;
1513 			}
1514 			if (i++ >= AWI_CMD_TIMEOUT*1000/10)
1515 				error = EWOULDBLOCK;
1516 			else
1517 				DELAY(10);
1518 		}
1519 		if (error)
1520 			break;
1521 	}
1522 	if (error) {
1523 		DPRINTF(("awi_cmd_wait: cmd 0x%x, error %d\n",
1524 		    sc->sc_cmd_inprog, error));
1525 	}
1526 	return error;
1527 }
1528 
1529 static void
1530 awi_cmd_done(struct awi_softc *sc)
1531 {
1532 	u_int8_t cmd, status;
1533 
1534 	status = awi_read_1(sc, AWI_CMD_STATUS);
1535 	if (status == AWI_STAT_IDLE)
1536 		return;		/* stray interrupt */
1537 
1538 	cmd = sc->sc_cmd_inprog;
1539 	sc->sc_cmd_inprog = 0;
1540 	wakeup(sc);
1541 	awi_write_1(sc, AWI_CMD, 0);
1542 
1543 	if (status != AWI_STAT_OK) {
1544 		printf("%s: command %d failed %x\n",
1545 		    sc->sc_dev.dv_xname, cmd, status);
1546 		sc->sc_substate = AWI_ST_NONE;
1547 		return;
1548 	}
1549 	if (sc->sc_substate != AWI_ST_NONE)
1550 		(void)ieee80211_new_state(&sc->sc_ic.ic_if, sc->sc_nstate, -1);
1551 }
1552 
1553 static int
1554 awi_next_txd(struct awi_softc *sc, int len, u_int32_t *framep, u_int32_t *ntxdp)
1555 {
1556 	u_int32_t txd, ntxd, frame;
1557 
1558 	txd = sc->sc_txnext;
1559 	frame = txd + AWI_TXD_SIZE;
1560 	if (frame + len > sc->sc_txend)
1561 		frame = sc->sc_txbase;
1562 	ntxd = frame + len;
1563 	if (ntxd + AWI_TXD_SIZE > sc->sc_txend)
1564 		ntxd = sc->sc_txbase;
1565 	*framep = frame;
1566 	*ntxdp = ntxd;
1567 	/*
1568 	 * Determine if there are any room in ring buffer.
1569 	 *		--- send wait,  === new data,  +++ conflict (ENOBUFS)
1570 	 *   base........................end
1571 	 *	   done----txd=====ntxd		OK
1572 	 *	 --txd=====done++++ntxd--	full
1573 	 *	 --txd=====ntxd    done--	OK
1574 	 *	 ==ntxd    done----txd===	OK
1575 	 *	 ==done++++ntxd----txd===	full
1576 	 *	 ++ntxd    txd=====done++	full
1577 	 */
1578 	if (txd < ntxd) {
1579 		if (txd < sc->sc_txdone && ntxd + AWI_TXD_SIZE > sc->sc_txdone)
1580 			return ENOBUFS;
1581 	} else {
1582 		if (txd < sc->sc_txdone || ntxd + AWI_TXD_SIZE > sc->sc_txdone)
1583 			return ENOBUFS;
1584 	}
1585 	return 0;
1586 }
1587 
1588 static int
1589 awi_lock(struct awi_softc *sc)
1590 {
1591 	int error = 0;
1592 
1593 	if (curproc == NULL) {
1594 		/*
1595 		 * XXX
1596 		 * Though driver ioctl should be called with context,
1597 		 * KAME ipv6 stack calls ioctl in interrupt for now.
1598 		 * We simply abort the request if there are other
1599 		 * ioctl requests in progress.
1600 		 */
1601 		if (sc->sc_busy) {
1602 			return EWOULDBLOCK;
1603 			if (sc->sc_invalid)
1604 				return ENXIO;
1605 		}
1606 		sc->sc_busy = 1;
1607 		sc->sc_cansleep = 0;
1608 		return 0;
1609 	}
1610 	while (sc->sc_busy) {
1611 		if (sc->sc_invalid)
1612 			return ENXIO;
1613 		sc->sc_sleep_cnt++;
1614 		error = tsleep(sc, PWAIT | PCATCH, "awilck", 0);
1615 		sc->sc_sleep_cnt--;
1616 		if (error)
1617 			return error;
1618 	}
1619 	sc->sc_busy = 1;
1620 	sc->sc_cansleep = 1;
1621 	return 0;
1622 }
1623 
1624 static void
1625 awi_unlock(struct awi_softc *sc)
1626 {
1627 	sc->sc_busy = 0;
1628 	sc->sc_cansleep = 0;
1629 	if (sc->sc_sleep_cnt)
1630 		wakeup(sc);
1631 }
1632 
1633 static int
1634 awi_intr_lock(struct awi_softc *sc)
1635 {
1636 	u_int8_t status;
1637 	int i, retry;
1638 
1639 	status = 1;
1640 	for (retry = 0; retry < 10; retry++) {
1641 		for (i = 0; i < AWI_LOCKOUT_TIMEOUT*1000/5; i++) {
1642 			if ((status = awi_read_1(sc, AWI_LOCKOUT_HOST)) == 0)
1643 				break;
1644 			DELAY(5);
1645 		}
1646 		if (status != 0)
1647 			break;
1648 		awi_write_1(sc, AWI_LOCKOUT_MAC, 1);
1649 		if ((status = awi_read_1(sc, AWI_LOCKOUT_HOST)) == 0)
1650 			break;
1651 		awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
1652 	}
1653 	if (status != 0) {
1654 		printf("%s: failed to lock interrupt\n",
1655 		    sc->sc_dev.dv_xname);
1656 		return ENXIO;
1657 	}
1658 	return 0;
1659 }
1660 
1661 static void
1662 awi_intr_unlock(struct awi_softc *sc)
1663 {
1664 
1665 	awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
1666 }
1667 
1668 static int
1669 awi_newstate(void *arg, enum ieee80211_state nstate)
1670 {
1671 	struct awi_softc *sc = arg;
1672 	struct ieee80211com *ic = &sc->sc_ic;
1673 	struct ieee80211_bss *bs = &ic->ic_bss;
1674 	struct ifnet *ifp = &ic->ic_if;
1675 	int error;
1676 	u_int8_t newmode;
1677 	enum ieee80211_state ostate;
1678 #ifdef AWI_DEBUG
1679 	static const char *stname[] =
1680 	    { "INIT", "SCAN", "AUTH", "ASSOC", "RUN" };
1681 	static const char *substname[] =
1682 	    { "NONE", "SCAN_INIT", "SCAN_SETMIB", "SCAN_SCCMD",
1683 	      "SUB_INIT", "SUB_SETSS", "SUB_SYNC" };
1684 #endif /* AWI_DEBUG */
1685 
1686 	ostate = ic->ic_state;
1687 	DPRINTF(("awi_newstate: %s (%s/%s) -> %s\n", stname[ostate],
1688 	    stname[sc->sc_nstate], substname[sc->sc_substate], stname[nstate]));
1689 
1690 	/* set LED */
1691 	switch (nstate) {
1692 	case IEEE80211_S_INIT:
1693 		awi_drvstate(sc, AWI_DRV_RESET);
1694 		break;
1695 	case IEEE80211_S_SCAN:
1696 		if (ic->ic_flags & IEEE80211_F_ADHOC)
1697 			awi_drvstate(sc, AWI_DRV_ADHSC);
1698 		else
1699 			awi_drvstate(sc, AWI_DRV_INFSY);
1700 		break;
1701 	case IEEE80211_S_AUTH:
1702 		awi_drvstate(sc, AWI_DRV_INFSY);
1703 		break;
1704 	case IEEE80211_S_ASSOC:
1705 		awi_drvstate(sc, AWI_DRV_INFAUTH);
1706 		break;
1707 	case IEEE80211_S_RUN:
1708 		if (ic->ic_flags & IEEE80211_F_ADHOC)
1709 			awi_drvstate(sc, AWI_DRV_ADHSY);
1710 		else
1711 			awi_drvstate(sc, AWI_DRV_INFASSOC);
1712 		break;
1713 	}
1714 
1715 	if (nstate == IEEE80211_S_INIT) {
1716 		sc->sc_substate = AWI_ST_NONE;
1717 		ic->ic_flags &= ~IEEE80211_F_SIBSS;
1718 		return 0;
1719 	}
1720 
1721 	/* state transition */
1722 	if (nstate == IEEE80211_S_SCAN) {
1723 		/* SCAN substate */
1724 		if (sc->sc_substate == AWI_ST_NONE) {
1725 			sc->sc_nstate = nstate;	/* next state in transition */
1726 			sc->sc_substate = AWI_ST_SCAN_INIT;
1727 		}
1728 		switch (sc->sc_substate) {
1729 		case AWI_ST_SCAN_INIT:
1730 			sc->sc_substate = AWI_ST_SCAN_SETMIB;
1731 			switch (ostate) {
1732 			case IEEE80211_S_RUN:
1733 				/* beacon miss */
1734 				if (ifp->if_flags & IFF_DEBUG)
1735 					printf("%s: no recent beacons from %s;"
1736 					    " rescanning\n",
1737 					    ifp->if_xname,
1738 					    ether_sprintf(ic->ic_bss.bs_bssid));
1739 				/* FALLTHRU */
1740 			case IEEE80211_S_AUTH:
1741 			case IEEE80211_S_ASSOC:
1742 				/* timeout restart scan */
1743 				ieee80211_free_scan(ifp);
1744 				/* FALLTHRU */
1745 			case IEEE80211_S_INIT:
1746 				ic->ic_flags |= IEEE80211_F_ASCAN;
1747 				ic->ic_scan_timer = 0;
1748 				/* FALLTHRU */
1749 			case IEEE80211_S_SCAN:
1750 				/* scan next */
1751 				break;
1752 			}
1753 			if (ic->ic_flags & IEEE80211_F_ASCAN)
1754 				newmode = AWI_SCAN_ACTIVE;
1755 			else
1756 				newmode = AWI_SCAN_PASSIVE;
1757 			if (sc->sc_mib_mgt.aScan_Mode != newmode) {
1758 				sc->sc_mib_mgt.aScan_Mode = newmode;
1759 				if ((error = awi_mib(sc, AWI_CMD_SET_MIB,
1760 				    AWI_MIB_MGT, AWI_NOWAIT)) != 0)
1761 					break;
1762 			}
1763 			/* FALLTHRU */
1764 		case AWI_ST_SCAN_SETMIB:
1765 			sc->sc_substate = AWI_ST_SCAN_SCCMD;
1766 			if (sc->sc_cmd_inprog) {
1767 				if ((error = awi_cmd_wait(sc)) != 0)
1768 					break;
1769 			}
1770 			sc->sc_cmd_inprog = AWI_CMD_SCAN;
1771 			awi_write_2(sc, AWI_CA_SCAN_DURATION,
1772 			    (ic->ic_flags & IEEE80211_F_ASCAN) ?
1773 			    AWI_ASCAN_DURATION : AWI_PSCAN_DURATION);
1774 			if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1775 				awi_write_1(sc, AWI_CA_SCAN_SET,
1776 				    IEEE80211_FH_CHANSET(bs->bs_chan));
1777 				awi_write_1(sc, AWI_CA_SCAN_PATTERN,
1778 				    IEEE80211_FH_CHANPAT(bs->bs_chan));
1779 				awi_write_1(sc, AWI_CA_SCAN_IDX, 1);
1780 			} else {
1781 				awi_write_1(sc, AWI_CA_SCAN_SET, bs->bs_chan);
1782 				awi_write_1(sc, AWI_CA_SCAN_PATTERN, 0);
1783 				awi_write_1(sc, AWI_CA_SCAN_IDX, 0);
1784 			}
1785 			awi_write_1(sc, AWI_CA_SCAN_SUSP, 0);
1786 			sc->sc_cur_chan = bs->bs_chan;
1787 			if ((error = awi_cmd(sc, AWI_CMD_SCAN, AWI_NOWAIT))
1788 			    != 0)
1789 				break;
1790 			/* FALLTHRU */
1791 		case AWI_ST_SCAN_SCCMD:
1792 			if (ic->ic_scan_timer == 0)
1793 				ic->ic_scan_timer =
1794 				    (ic->ic_flags & IEEE80211_F_ASCAN) ?
1795 				    IEEE80211_ASCAN_WAIT : IEEE80211_PSCAN_WAIT;
1796 			ifp->if_timer = 1;
1797 			ic->ic_state = nstate;
1798 			sc->sc_substate = AWI_ST_NONE;
1799 			error = EINPROGRESS;
1800 			break;
1801 		default:
1802 			DPRINTF(("awi_newstate: unexpected state %s/%s\n",
1803 			    stname[nstate], substname[sc->sc_substate]));
1804 			sc->sc_substate = AWI_ST_NONE;
1805 			error = EIO;
1806 			break;
1807 		}
1808 		return error;
1809 	}
1810 
1811 	if (ostate == IEEE80211_S_SCAN) {
1812 		/* set SSID and channel */
1813 		/* substate */
1814 		if (sc->sc_substate == AWI_ST_NONE) {
1815 			sc->sc_nstate = nstate;	/* next state in transition */
1816 			sc->sc_substate = AWI_ST_SUB_INIT;
1817 		}
1818 		switch (sc->sc_substate) {
1819 		case AWI_ST_SUB_INIT:
1820 			sc->sc_substate = AWI_ST_SUB_SETSS;
1821 			memcpy(&sc->sc_mib_mgt.aCurrent_BSS_ID, bs->bs_bssid,
1822 			    IEEE80211_ADDR_LEN);
1823 			memset(&sc->sc_mib_mgt.aCurrent_ESS_ID, 0,
1824 			    AWI_ESS_ID_SIZE);
1825 			sc->sc_mib_mgt.aCurrent_ESS_ID[0] =
1826 			    IEEE80211_ELEMID_SSID;
1827 			sc->sc_mib_mgt.aCurrent_ESS_ID[1] = bs->bs_esslen;
1828 			memcpy(&sc->sc_mib_mgt.aCurrent_ESS_ID[2],
1829 			    bs->bs_essid, bs->bs_esslen);
1830 			LE_WRITE_2(&sc->sc_mib_mgt.aBeacon_Period,
1831 			    bs->bs_intval);
1832 			if ((error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT,
1833 			    AWI_NOWAIT)) != 0)
1834 				break;
1835 			/* FALLTHRU */
1836 		case AWI_ST_SUB_SETSS:
1837 			sc->sc_substate = AWI_ST_SUB_SYNC;
1838 			if (sc->sc_cmd_inprog) {
1839 				if (awi_cmd_wait(sc))
1840 					break;
1841 			}
1842 			sc->sc_cmd_inprog = AWI_CMD_SYNC;
1843 			if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
1844 				awi_write_1(sc, AWI_CA_SYNC_SET,
1845 				    IEEE80211_FH_CHANSET(bs->bs_chan));
1846 				awi_write_1(sc, AWI_CA_SYNC_PATTERN,
1847 				    IEEE80211_FH_CHANPAT(bs->bs_chan));
1848 				awi_write_1(sc, AWI_CA_SYNC_IDX,
1849 				    bs->bs_fhindex);
1850 				awi_write_2(sc, AWI_CA_SYNC_DWELL,
1851 				    bs->bs_fhdwell);
1852 			} else {
1853 				awi_write_1(sc, AWI_CA_SYNC_SET, bs->bs_chan);
1854 				awi_write_1(sc, AWI_CA_SYNC_PATTERN, 0);
1855 				awi_write_1(sc, AWI_CA_SYNC_IDX, 0);
1856 				awi_write_2(sc, AWI_CA_SYNC_DWELL, 0);
1857 			}
1858 			if ((ic->ic_flags & IEEE80211_F_SIBSS) &&
1859 			    !sc->sc_no_bssid)
1860 				awi_write_1(sc, AWI_CA_SYNC_STARTBSS, 1);
1861 			else
1862 				awi_write_1(sc, AWI_CA_SYNC_STARTBSS, 0);
1863 			awi_write_2(sc, AWI_CA_SYNC_MBZ, 0);
1864 			awi_write_bytes(sc, AWI_CA_SYNC_TIMESTAMP,
1865 			    bs->bs_tstamp, 8);
1866 			awi_write_4(sc, AWI_CA_SYNC_REFTIME, bs->bs_timoff);
1867 			sc->sc_cur_chan = bs->bs_chan;
1868 			if ((error = awi_cmd(sc, AWI_CMD_SYNC, AWI_NOWAIT))
1869 			    != 0)
1870 				break;
1871 			/* FALLTHRU */
1872 		case AWI_ST_SUB_SYNC:
1873 			sc->sc_substate = AWI_ST_NONE;
1874 			if (ic->ic_flags & IEEE80211_F_SIBSS) {
1875 				if ((error = awi_mib(sc, AWI_CMD_GET_MIB,
1876 				    AWI_MIB_MGT, AWI_WAIT)) != 0)
1877 					break;
1878 				memcpy(bs->bs_bssid,
1879 				    &sc->sc_mib_mgt.aCurrent_BSS_ID,
1880 				    IEEE80211_ADDR_LEN);
1881 			} else {
1882 				if (nstate == IEEE80211_S_RUN) {
1883 					sc->sc_rx_timer = 10;
1884 					ifp->if_timer = 1;
1885 				}
1886 			}
1887 			error = 0;
1888 			break;
1889 		default:
1890 			DPRINTF(("awi_newstate: unexpected state %s/%s\n",
1891 			    stname[nstate], substname[sc->sc_substate]));
1892 			sc->sc_substate = AWI_ST_NONE;
1893 			error = EIO;
1894 			break;
1895 		}
1896 		return error;
1897 	}
1898 
1899 	sc->sc_substate = AWI_ST_NONE;
1900 
1901 	return 0;
1902 }
1903 
1904 static struct mbuf *
1905 awi_ether_encap(struct awi_softc *sc, struct mbuf *m)
1906 {
1907 	struct ieee80211com *ic = &sc->sc_ic;
1908 	struct ieee80211_bss *bs = &ic->ic_bss;
1909 	struct ether_header *eh;
1910 	struct ieee80211_frame *wh;
1911 
1912 	if (m->m_len < sizeof(struct ether_header)) {
1913 		m = m_pullup(m, sizeof(struct ether_header));
1914 		if (m == NULL)
1915 			return NULL;
1916 	}
1917 	eh = mtod(m, struct ether_header *);
1918 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
1919 	if (m == NULL)
1920 		return NULL;
1921 	wh = mtod(m, struct ieee80211_frame *);
1922 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
1923 	*(u_int16_t *)wh->i_dur = 0;
1924 	*(u_int16_t *)wh->i_seq =
1925 	    htole16(bs->bs_txseq << IEEE80211_SEQ_SEQ_SHIFT);
1926 	bs->bs_txseq++;
1927 	if (ic->ic_flags & IEEE80211_F_ADHOC) {
1928 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1929 		if (sc->sc_adhoc_ap)
1930 			memcpy(wh->i_addr1, bs->bs_macaddr, IEEE80211_ADDR_LEN);
1931 		else
1932 			memcpy(wh->i_addr1, eh->ether_dhost,
1933 			    IEEE80211_ADDR_LEN);
1934 		memcpy(wh->i_addr2, eh->ether_shost, IEEE80211_ADDR_LEN);
1935 		memcpy(wh->i_addr3, bs->bs_bssid, IEEE80211_ADDR_LEN);
1936 	} else {
1937 		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
1938 		memcpy(wh->i_addr1, bs->bs_bssid, IEEE80211_ADDR_LEN);
1939 		memcpy(wh->i_addr2, eh->ether_shost, IEEE80211_ADDR_LEN);
1940 		memcpy(wh->i_addr3, eh->ether_dhost, IEEE80211_ADDR_LEN);
1941 	}
1942 	return m;
1943 }
1944 
1945 static struct mbuf *
1946 awi_ether_modcap(struct awi_softc *sc, struct mbuf *m)
1947 {
1948 	struct ieee80211com *ic = &sc->sc_ic;
1949 	struct ether_header eh;
1950 	struct ieee80211_frame wh;
1951 	struct llc *llc;
1952 
1953 	if (m->m_len < sizeof(wh) + sizeof(eh)) {
1954 		m = m_pullup(m, sizeof(wh) + sizeof(eh));
1955 		if (m == NULL)
1956 			return NULL;
1957 	}
1958 	memcpy(&wh, mtod(m, caddr_t), sizeof(wh));
1959 	if (wh.i_fc[0] != (IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA))
1960 		return m;
1961 	memcpy(&eh, mtod(m, caddr_t) + sizeof(wh), sizeof(eh));
1962 	m_adj(m, sizeof(eh) - sizeof(*llc));
1963 	if (ic->ic_flags & IEEE80211_F_ADHOC)
1964 		memcpy(wh.i_addr2, eh.ether_shost, IEEE80211_ADDR_LEN);
1965 	memcpy(mtod(m, caddr_t), &wh, sizeof(wh));
1966 	llc = (struct llc *)(mtod(m, caddr_t) + sizeof(wh));
1967 	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
1968 	llc->llc_control = LLC_UI;
1969 	llc->llc_snap.org_code[0] = 0;
1970 	llc->llc_snap.org_code[1] = 0;
1971 	llc->llc_snap.org_code[2] = 0;
1972 	llc->llc_snap.ether_type = eh.ether_type;
1973 	return m;
1974 }
1975