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