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