Home | History | Annotate | Line # | Download | only in ic
awi.c revision 1.43
      1 /*	$NetBSD: awi.c,v 1.43 2002/08/05 06:55:07 onoe Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999,2000,2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Bill Sommerfeld
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 /*
     39  * Driver for AMD 802.11 firmware.
     40  * Uses am79c930 chip driver to talk to firmware running on the am79c930.
     41  *
     42  * More-or-less a generic ethernet-like if driver, with 802.11 gorp added.
     43  */
     44 
     45 /*
     46  * todo:
     47  *	- flush tx queue on resynch.
     48  *	- clear oactive on "down".
     49  *	- rewrite copy-into-mbuf code
     50  *	- mgmt state machine gets stuck retransmitting assoc requests.
     51  *	- multicast filter.
     52  *	- fix device reset so it's more likely to work
     53  *	- show status goo through ifmedia.
     54  *
     55  * more todo:
     56  *	- deal with more 802.11 frames.
     57  *		- send reassoc request
     58  *		- deal with reassoc response
     59  *		- send/deal with disassociation
     60  *	- deal with "full" access points (no room for me).
     61  *	- power save mode
     62  *
     63  * later:
     64  *	- SSID preferences
     65  *	- need ioctls for poking at the MIBs
     66  *	- implement ad-hoc mode (including bss creation).
     67  *	- decide when to do "ad hoc" vs. infrastructure mode (IFF_LINK flags?)
     68  *		(focus on inf. mode since that will be needed for ietf)
     69  *	- deal with DH vs. FH versions of the card
     70  *	- deal with faster cards (2mb/s)
     71  *	- ?WEP goo (mmm, rc4) (it looks not particularly useful).
     72  *	- ifmedia revision.
     73  *	- common 802.11 mibish things.
     74  *	- common 802.11 media layer.
     75  */
     76 
     77 /*
     78  * Driver for AMD 802.11 PCnetMobile firmware.
     79  * Uses am79c930 chip driver to talk to firmware running on the am79c930.
     80  *
     81  * The initial version of the driver was written by
     82  * Bill Sommerfeld <sommerfeld (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.43 2002/08/05 06:55:07 onoe 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 (dowep) {
    657 			if ((m0 = ieee80211_wep_crypt(ifp, m0, 1)) == NULL) {
    658 				ifp->if_oerrors++;
    659 				continue;
    660 			}
    661 		}
    662 #ifdef DIAGNOSTIC
    663 		if (m0->m_pkthdr.len != len) {
    664 			printf("%s: length %d should be %d\n",
    665 			    ifp->if_xname, m0->m_pkthdr.len, len);
    666 			m_freem(m0);
    667 			ifp->if_oerrors++;
    668 			continue;
    669 		}
    670 #endif
    671 
    672 		if ((ifp->if_flags & IFF_DEBUG) && (ifp->if_flags & IFF_LINK2))
    673 			ieee80211_dump_pkt(m0->m_data, m0->m_len,
    674 			    ic->ic_bss.bs_rates[ic->ic_bss.bs_txrate] &
    675 			    IEEE80211_RATE_VAL, -1);
    676 
    677 		for (m = m0, len = 0; m != NULL; m = m->m_next) {
    678 			awi_write_bytes(sc, frame + len, mtod(m, u_int8_t *),
    679 			    m->m_len);
    680 			len += m->m_len;
    681 		}
    682 		m_freem(m0);
    683 		rate = (ic->ic_bss.bs_rates[ic->ic_bss.bs_txrate] &
    684 		    IEEE80211_RATE_VAL) * 5;
    685 		awi_write_1(sc, ntxd + AWI_TXD_STATE, 0);
    686 		awi_write_4(sc, txd + AWI_TXD_START, frame);
    687 		awi_write_4(sc, txd + AWI_TXD_NEXT, ntxd);
    688 		awi_write_4(sc, txd + AWI_TXD_LENGTH, len);
    689 		awi_write_1(sc, txd + AWI_TXD_RATE, rate);
    690 		awi_write_4(sc, txd + AWI_TXD_NDA, 0);
    691 		awi_write_4(sc, txd + AWI_TXD_NRA, 0);
    692 		awi_write_1(sc, txd + AWI_TXD_STATE, AWI_TXD_ST_OWN);
    693 		sc->sc_txnext = ntxd;
    694 
    695 		sc->sc_tx_timer = 5;
    696 		ifp->if_timer = 1;
    697 	}
    698 }
    699 
    700 static void
    701 awi_watchdog(struct ifnet *ifp)
    702 {
    703 	struct awi_softc *sc = ifp->if_softc;
    704 	u_int32_t prevdone;
    705 	int ocansleep;
    706 
    707 	ifp->if_timer = 0;
    708 	if (!sc->sc_enabled || sc->sc_invalid)
    709 		return;
    710 
    711 	ocansleep = sc->sc_cansleep;
    712 	sc->sc_cansleep = 0;
    713 	if (sc->sc_tx_timer) {
    714 		if (--sc->sc_tx_timer == 0) {
    715 			printf("%s: device timeout\n", ifp->if_xname);
    716 			prevdone = sc->sc_txdone;
    717 			awi_tx_int(sc);
    718 			if (sc->sc_txdone == prevdone) {
    719 				ifp->if_oerrors++;
    720 				awi_init(ifp);
    721 				goto out;
    722 			}
    723 		}
    724 		ifp->if_timer = 1;
    725 	}
    726 	if (sc->sc_rx_timer) {
    727 		if (--sc->sc_rx_timer == 0) {
    728 			if (sc->sc_ic.ic_state == IEEE80211_S_RUN) {
    729 				ieee80211_new_state(ifp, IEEE80211_S_SCAN, -1);
    730 				goto out;
    731 			}
    732 		} else
    733 			ifp->if_timer = 1;
    734 	}
    735 	/* TODO: rate control */
    736 	ieee80211_watchdog(ifp);
    737   out:
    738 	sc->sc_cansleep = ocansleep;
    739 }
    740 
    741 static int
    742 awi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    743 {
    744 	struct awi_softc *sc = ifp->if_softc;
    745 	struct ifreq *ifr = (struct ifreq *)data;
    746 	int s, error;
    747 
    748 	s = splnet();
    749 	/* serialize ioctl, since we may sleep */
    750 	if ((error = awi_lock(sc)) != 0)
    751 		goto cantlock;
    752 
    753 	switch (cmd) {
    754 	case SIOCSIFFLAGS:
    755 		if (ifp->if_flags & IFF_UP) {
    756 			if (sc->sc_enabled) {
    757 				/*
    758 				 * To avoid rescanning another access point,
    759 				 * do not call awi_init() here.  Instead,
    760 				 * only reflect promisc mode settings.
    761 				 */
    762 				error = awi_mode_init(sc);
    763 			} else
    764 				error = awi_init(ifp);
    765 		} else if (sc->sc_enabled)
    766 			awi_stop(ifp, 1);
    767 		break;
    768 	case SIOCSIFMEDIA:
    769 	case SIOCGIFMEDIA:
    770 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
    771 		break;
    772 	case SIOCADDMULTI:
    773 	case SIOCDELMULTI:
    774 		error = (cmd == SIOCADDMULTI) ?
    775 		    ether_addmulti(ifr, &sc->sc_ic.ic_ec) :
    776 		    ether_delmulti(ifr, &sc->sc_ic.ic_ec);
    777 		if (error == ENETRESET) {
    778 			/* do not rescan */
    779 			if (sc->sc_enabled)
    780 				error = awi_mode_init(sc);
    781 			else
    782 				error = 0;
    783 		}
    784 		break;
    785 	default:
    786 		error = ieee80211_ioctl(ifp, cmd, data);
    787 		if (error == ENETRESET) {
    788 			if (sc->sc_enabled)
    789 				error = awi_init(ifp);
    790 			else
    791 				error = 0;
    792 		}
    793 		break;
    794 	}
    795 	awi_unlock(sc);
    796   cantlock:
    797 	splx(s);
    798 	return error;
    799 }
    800 
    801 /*
    802  * Called from ifmedia_ioctl via awi_ioctl with lock obtained.
    803  */
    804 static int
    805 awi_media_change(struct ifnet *ifp)
    806 {
    807 	struct awi_softc *sc = ifp->if_softc;
    808 	struct ieee80211com *ic = &sc->sc_ic;
    809 	struct ifmedia_entry *ime;
    810 	int i, rate, error = 0;
    811 
    812 	ime = sc->sc_media.ifm_cur;
    813 	if (IFM_SUBTYPE(ime->ifm_media) == IFM_AUTO) {
    814 		ic->ic_fixed_rate = -1;
    815 	} else {
    816 		rate = awi_media_opt2rate(sc, ime->ifm_media);
    817 		if (rate == 0)
    818 			return EINVAL;
    819 		for (i = 0; i < IEEE80211_RATE_SIZE; i++) {
    820 			if ((ic->ic_sup_rates[i] & IEEE80211_RATE_VAL) == rate)
    821 				break;
    822 		}
    823 		if (i == IEEE80211_RATE_SIZE)
    824 			return EINVAL;
    825 		ic->ic_fixed_rate = i;
    826 	}
    827 
    828 	/*
    829 	 *  ADHOC,-FLAG0	ADHOC,  !no_bssid, !adhoc_ap	IBSS
    830 	 *  ADHOC, FLAG0	ADHOC    no_bssid, !adhoc_ap	WaveLAN adhoc
    831 	 * -ADHOC,-FLAG0	~ADHOC, !no_bssid, !adhoc_ap	Infra
    832 	 * -ADHOC, FLAG0	ADHOC,  !no_bssid,  adhoc_ap	Melco old AP
    833 	 *						also LINK0
    834 	 */
    835 	if (ime->ifm_media & IFM_IEEE80211_ADHOC) {
    836 		if ((ic->ic_flags & IEEE80211_F_ADHOC) == 0) {
    837 			ic->ic_flags |= IEEE80211_F_ADHOC;
    838 			error = ENETRESET;
    839 		}
    840 		ic->ic_flags |= IEEE80211_F_IBSSON;
    841 		if (sc->sc_mib_phy.IEEE_PHY_Type != AWI_PHY_TYPE_FH &&
    842 		    (ime->ifm_media & IFM_FLAG0)) {
    843 			if (sc->sc_no_bssid == 0) {
    844 				sc->sc_no_bssid = 1;
    845 				error = ENETRESET;
    846 			}
    847 		} else {
    848 			if (sc->sc_no_bssid) {
    849 				sc->sc_no_bssid = 0;
    850 				error = ENETRESET;
    851 			}
    852 		}
    853 		if (sc->sc_adhoc_ap) {
    854 			sc->sc_adhoc_ap = 0;
    855 			error = ENETRESET;
    856 		}
    857 	} else {
    858 		ic->ic_flags &= ~IEEE80211_F_IBSSON;
    859 		if (sc->sc_no_bssid) {
    860 			sc->sc_no_bssid = 0;
    861 			error = ENETRESET;
    862 		}
    863 		if (ime->ifm_media & IFM_FLAG0) {
    864 			if ((ic->ic_flags & IEEE80211_F_ADHOC) == 0) {
    865 				ic->ic_flags |= IEEE80211_F_ADHOC;
    866 				error = ENETRESET;
    867 			}
    868 			if (!sc->sc_adhoc_ap) {
    869 				sc->sc_adhoc_ap = 1;
    870 				error = ENETRESET;
    871 			}
    872 		} else {
    873 			if (ic->ic_flags & IEEE80211_F_ADHOC) {
    874 				ic->ic_flags &= ~IEEE80211_F_ADHOC;
    875 				error = ENETRESET;
    876 			}
    877 			if (sc->sc_adhoc_ap) {
    878 				sc->sc_adhoc_ap = 0;
    879 				error = ENETRESET;
    880 			}
    881 		}
    882 	}
    883 	if (error == ENETRESET) {
    884 		if (sc->sc_enabled)
    885 			error = awi_init(ifp);
    886 		else
    887 			error = 0;
    888 	}
    889 	return error;
    890 }
    891 
    892 static void
    893 awi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
    894 {
    895 	struct awi_softc *sc = ifp->if_softc;
    896 	struct ieee80211com *ic = &sc->sc_ic;
    897 	int rate;
    898 
    899 	imr->ifm_status = IFM_AVALID;
    900 	if (ic->ic_state == IEEE80211_S_RUN)
    901 		imr->ifm_status |= IFM_ACTIVE;
    902 	imr->ifm_active = IFM_IEEE80211;
    903 	if (ic->ic_state == IEEE80211_S_RUN)
    904 		rate = ic->ic_bss.bs_rates[ic->ic_bss.bs_txrate] &
    905 		    IEEE80211_RATE_VAL;
    906 	else {
    907 		if (ic->ic_fixed_rate == -1)
    908 			rate = 0;
    909 		else
    910 			rate = ic->ic_sup_rates[ic->ic_fixed_rate] &
    911 			    IEEE80211_RATE_VAL;
    912 	}
    913 	imr->ifm_active |= awi_media_rate2opt(sc, rate);
    914 	if (ic->ic_flags & IEEE80211_F_ADHOC) {
    915 		if (sc->sc_adhoc_ap)
    916 			imr->ifm_active |= IFM_FLAG0;
    917 		else {
    918 			imr->ifm_active |= IFM_IEEE80211_ADHOC;
    919 			if (sc->sc_no_bssid)
    920 				imr->ifm_active |= IFM_FLAG0;
    921 		}
    922 	}
    923 }
    924 
    925 static int
    926 awi_mode_init(struct awi_softc *sc)
    927 {
    928 	struct ifnet *ifp = &sc->sc_ic.ic_if;
    929 	int n, error;
    930 	struct ether_multi *enm;
    931 	struct ether_multistep step;
    932 
    933 	/* reinitialize muticast filter */
    934 	n = 0;
    935 	sc->sc_mib_local.Accept_All_Multicast_Dis = 0;
    936 	if (ifp->if_flags & IFF_PROMISC) {
    937 		sc->sc_mib_mac.aPromiscuous_Enable = 1;
    938 		goto set_mib;
    939 	}
    940 	sc->sc_mib_mac.aPromiscuous_Enable = 0;
    941 	ETHER_FIRST_MULTI(step, &sc->sc_ic.ic_ec, enm);
    942 	while (enm != NULL) {
    943 		if (n == AWI_GROUP_ADDR_SIZE ||
    944 		    memcmp(enm->enm_addrlo, enm->enm_addrhi, IEEE80211_ADDR_LEN)
    945 		    != 0)
    946 			goto set_mib;
    947 		memcpy(sc->sc_mib_addr.aGroup_Addresses[n], enm->enm_addrlo,
    948 		    IEEE80211_ADDR_LEN);
    949 		n++;
    950 		ETHER_NEXT_MULTI(step, enm);
    951 	}
    952 	for (; n < AWI_GROUP_ADDR_SIZE; n++)
    953 		memset(sc->sc_mib_addr.aGroup_Addresses[n], 0, IEEE80211_ADDR_LEN);
    954 	sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
    955 
    956   set_mib:
    957 	if (sc->sc_mib_local.Accept_All_Multicast_Dis)
    958 		ifp->if_flags &= ~IFF_ALLMULTI;
    959 	else
    960 		ifp->if_flags |= IFF_ALLMULTI;
    961 	sc->sc_mib_mgt.Wep_Required =
    962 	    (sc->sc_ic.ic_flags & IEEE80211_F_WEPON) ? AWI_WEP_ON : AWI_WEP_OFF;
    963 
    964 	if ((error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_LOCAL, AWI_WAIT)) ||
    965 	    (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_ADDR, AWI_WAIT)) ||
    966 	    (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MAC, AWI_WAIT)) ||
    967 	    (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT, AWI_WAIT)) ||
    968 	    (error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_PHY, AWI_WAIT))) {
    969 		DPRINTF(("awi_mode_init: MIB set failed: %d\n", error));
    970 		return error;
    971 	}
    972 	return 0;
    973 }
    974 
    975 /* XXX should be moved to if_ieee80211subr.c ? */
    976 static int
    977 awi_media_rate2opt(struct awi_softc *sc, int rate)
    978 {
    979 	int mword;
    980 
    981 	mword = 0;
    982 	switch (rate & IEEE80211_RATE_VAL) {
    983 	case 2:
    984 		if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
    985 			mword = IFM_IEEE80211_FH1;
    986 		else
    987 			mword = IFM_IEEE80211_DS1;
    988 		break;
    989 	case 4:
    990 		if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH)
    991 			mword = IFM_IEEE80211_FH2;
    992 		else
    993 			mword = IFM_IEEE80211_DS2;
    994 		break;
    995 	case 11:
    996 		if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_DS)
    997 			mword = IFM_IEEE80211_DS5;
    998 		break;
    999 	case 22:
   1000 		if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_DS)
   1001 			mword = IFM_IEEE80211_DS11;
   1002 		break;
   1003 	}
   1004 	return mword;
   1005 }
   1006 
   1007 static int
   1008 awi_media_opt2rate(struct awi_softc *sc, int opt)
   1009 {
   1010 	int rate;
   1011 
   1012 	rate = 0;
   1013 	switch (IFM_SUBTYPE(opt)) {
   1014 	case IFM_IEEE80211_FH1:
   1015 	case IFM_IEEE80211_FH2:
   1016 		if (sc->sc_mib_phy.IEEE_PHY_Type != AWI_PHY_TYPE_FH)
   1017 			return 0;
   1018 		break;
   1019 	case IFM_IEEE80211_DS1:
   1020 	case IFM_IEEE80211_DS2:
   1021 	case IFM_IEEE80211_DS5:
   1022 	case IFM_IEEE80211_DS11:
   1023 		if (sc->sc_mib_phy.IEEE_PHY_Type != AWI_PHY_TYPE_DS)
   1024 			return 0;
   1025 		break;
   1026 	}
   1027 
   1028 	switch (IFM_SUBTYPE(opt)) {
   1029 	case IFM_IEEE80211_FH1:
   1030 	case IFM_IEEE80211_DS1:
   1031 		rate = 2;
   1032 		break;
   1033 	case IFM_IEEE80211_FH2:
   1034 	case IFM_IEEE80211_DS2:
   1035 		rate = 4;
   1036 		break;
   1037 	case IFM_IEEE80211_DS5:
   1038 		rate = 11;
   1039 		break;
   1040 	case IFM_IEEE80211_DS11:
   1041 		rate = 22;
   1042 		break;
   1043 	}
   1044 	return rate;
   1045 }
   1046 
   1047 static void
   1048 awi_rx_int(struct awi_softc *sc)
   1049 {
   1050 	struct ifnet *ifp = &sc->sc_ic.ic_if;
   1051 	u_int8_t state, rate, rssi;
   1052 	u_int16_t len;
   1053 	u_int32_t frame, next, rstamp, rxoff;
   1054 	struct mbuf *m;
   1055 
   1056 	rxoff = sc->sc_rxdoff;
   1057 	for (;;) {
   1058 		state = awi_read_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE);
   1059 		if (state & AWI_RXD_ST_OWN)
   1060 			break;
   1061 		if (!(state & AWI_RXD_ST_CONSUMED)) {
   1062 			if (sc->sc_substate != AWI_ST_NONE)
   1063 				goto rx_next;
   1064 			if (state & AWI_RXD_ST_RXERROR) {
   1065 				ifp->if_ierrors++;
   1066 				goto rx_next;
   1067 			}
   1068 			len    = awi_read_2(sc, rxoff + AWI_RXD_LEN);
   1069 			rate   = awi_read_1(sc, rxoff + AWI_RXD_RATE);
   1070 			rssi   = awi_read_1(sc, rxoff + AWI_RXD_RSSI);
   1071 			frame  = awi_read_4(sc, rxoff + AWI_RXD_START_FRAME) &
   1072 			    0x7fff;
   1073 			rstamp = awi_read_4(sc, rxoff + AWI_RXD_LOCALTIME);
   1074 			m = awi_devget(sc, frame, len);
   1075 			if (m == NULL) {
   1076 				ifp->if_ierrors++;
   1077 				goto rx_next;
   1078 			}
   1079 			if (state & AWI_RXD_ST_LF) {
   1080 				/* TODO check my bss */
   1081 				if (!(sc->sc_ic.ic_flags & IEEE80211_F_SIBSS) &&
   1082 				    sc->sc_ic.ic_state == IEEE80211_S_RUN) {
   1083 					sc->sc_rx_timer = 10;
   1084 					ifp->if_timer = 1;
   1085 				}
   1086 				if ((ifp->if_flags & IFF_DEBUG) &&
   1087 				    (ifp->if_flags & IFF_LINK2))
   1088 					ieee80211_dump_pkt(m->m_data, m->m_len,
   1089 					    rate / 5, rssi);
   1090 				if ((ifp->if_flags & IFF_LINK0) ||
   1091 				    sc->sc_adhoc_ap)
   1092 					m = awi_ether_modcap(sc, m);
   1093 				if (m == NULL)
   1094 					ifp->if_ierrors++;
   1095 				else
   1096 					ieee80211_input(ifp, m, rssi, rstamp);
   1097 			} else
   1098 				sc->sc_rxpend = m;
   1099   rx_next:
   1100 			state |= AWI_RXD_ST_CONSUMED;
   1101 			awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
   1102 		}
   1103 		next = awi_read_4(sc, rxoff + AWI_RXD_NEXT);
   1104 		if (next & AWI_RXD_NEXT_LAST)
   1105 			break;
   1106 		/* make sure the next pointer is correct */
   1107 		if (next != awi_read_4(sc, rxoff + AWI_RXD_NEXT))
   1108 			break;
   1109 		state |= AWI_RXD_ST_OWN;
   1110 		awi_write_1(sc, rxoff + AWI_RXD_HOST_DESC_STATE, state);
   1111 		rxoff = next & 0x7fff;
   1112 	}
   1113 	sc->sc_rxdoff = rxoff;
   1114 }
   1115 
   1116 static void
   1117 awi_tx_int(struct awi_softc *sc)
   1118 {
   1119 	struct ifnet *ifp = &sc->sc_ic.ic_if;
   1120 	u_int8_t flags;
   1121 
   1122 	while (sc->sc_txdone != sc->sc_txnext) {
   1123 		flags = awi_read_1(sc, sc->sc_txdone + AWI_TXD_STATE);
   1124 		if ((flags & AWI_TXD_ST_OWN) || !(flags & AWI_TXD_ST_DONE))
   1125 			break;
   1126 		if (flags & AWI_TXD_ST_ERROR)
   1127 			ifp->if_oerrors++;
   1128 		sc->sc_txdone = awi_read_4(sc, sc->sc_txdone + AWI_TXD_NEXT) &
   1129 		    0x7fff;
   1130 	}
   1131 	DPRINTF2(("awi_txint: txdone %d txnext %d txbase %d txend %d\n",
   1132 	    sc->sc_txdone, sc->sc_txnext, sc->sc_txbase, sc->sc_txend));
   1133 	sc->sc_tx_timer = 0;
   1134 	ifp->if_flags &= ~IFF_OACTIVE;
   1135 	awi_start(ifp);
   1136 }
   1137 
   1138 static struct mbuf *
   1139 awi_devget(struct awi_softc *sc, u_int32_t off, u_int16_t len)
   1140 {
   1141 	struct ifnet *ifp = &sc->sc_ic.ic_if;
   1142 	struct mbuf *m;
   1143 	struct mbuf *top, **mp;
   1144 	u_int tlen;
   1145 
   1146 	top = sc->sc_rxpend;
   1147 	mp = &top;
   1148 	if (top != NULL) {
   1149 		sc->sc_rxpend = NULL;
   1150 		top->m_pkthdr.len += len;
   1151 		m = top;
   1152 		while (*mp != NULL) {
   1153 			m = *mp;
   1154 			mp = &m->m_next;
   1155 		}
   1156 		if (m->m_flags & M_EXT)
   1157 			tlen = m->m_ext.ext_size;
   1158 		else if (m->m_flags & M_PKTHDR)
   1159 			tlen = MHLEN;
   1160 		else
   1161 			tlen = MLEN;
   1162 		tlen -= m->m_len;
   1163 		if (tlen > len)
   1164 			tlen = len;
   1165 		awi_read_bytes(sc, off, mtod(m, u_int8_t *) + m->m_len, tlen);
   1166 		off += tlen;
   1167 		len -= tlen;
   1168 	}
   1169 
   1170 	while (len > 0) {
   1171 		if (top == NULL) {
   1172 			MGETHDR(m, M_DONTWAIT, MT_DATA);
   1173 			if (m == NULL)
   1174 				return NULL;
   1175 			m->m_pkthdr.rcvif = ifp;
   1176 			m->m_pkthdr.len = len;
   1177 			m->m_len = MHLEN;
   1178 			m->m_flags |= M_HASFCS;
   1179 		} else {
   1180 			MGET(m, M_DONTWAIT, MT_DATA);
   1181 			if (m == NULL) {
   1182 				m_freem(top);
   1183 				return NULL;
   1184 			}
   1185 			m->m_len = MLEN;
   1186 		}
   1187 		if (len >= MINCLSIZE) {
   1188 			MCLGET(m, M_DONTWAIT);
   1189 			if (m->m_flags & M_EXT)
   1190 				m->m_len = m->m_ext.ext_size;
   1191 		}
   1192 		if (top == NULL) {
   1193 			int hdrlen = sizeof(struct ieee80211_frame) +
   1194 			    sizeof(struct llc);
   1195 			caddr_t newdata = (caddr_t)
   1196 			    ALIGN(m->m_data + hdrlen) - hdrlen;
   1197 			m->m_len -= newdata - m->m_data;
   1198 			m->m_data = newdata;
   1199 		}
   1200 		if (m->m_len > len)
   1201 			m->m_len = len;
   1202 		awi_read_bytes(sc, off, mtod(m, u_int8_t *), m->m_len);
   1203 		off += m->m_len;
   1204 		len -= m->m_len;
   1205 		*mp = m;
   1206 		mp = &m->m_next;
   1207 	}
   1208 	return top;
   1209 }
   1210 
   1211 /*
   1212  * Initialize hardware and start firmware to accept commands.
   1213  * Called everytime after power on firmware.
   1214  */
   1215 
   1216 static int
   1217 awi_hw_init(struct awi_softc *sc)
   1218 {
   1219 	u_int8_t status;
   1220 	u_int16_t intmask;
   1221 	int i, error;
   1222 
   1223 	sc->sc_enab_intr = 0;
   1224 	sc->sc_invalid = 0;	/* XXX: really? */
   1225 	awi_drvstate(sc, AWI_DRV_RESET);
   1226 
   1227 	/* reset firmware */
   1228 	am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_CORESET);
   1229 	DELAY(100);
   1230 	awi_write_1(sc, AWI_SELFTEST, 0);
   1231 	awi_write_1(sc, AWI_CMD, 0);
   1232 	awi_write_1(sc, AWI_BANNER, 0);
   1233 	am79c930_gcr_clearbits(&sc->sc_chip, AM79C930_GCR_CORESET);
   1234 	DELAY(100);
   1235 
   1236 	/* wait for selftest completion */
   1237 	for (i = 0; ; i++) {
   1238 		if (i >= AWI_SELFTEST_TIMEOUT*hz/1000) {
   1239 			printf("%s: failed to complete selftest (timeout)\n",
   1240 			    sc->sc_dev.dv_xname);
   1241 			return ENXIO;
   1242 		}
   1243 		status = awi_read_1(sc, AWI_SELFTEST);
   1244 		if ((status & 0xf0) == 0xf0)
   1245 			break;
   1246 		if (sc->sc_cansleep) {
   1247 			sc->sc_sleep_cnt++;
   1248 			(void)tsleep(sc, PWAIT, "awitst", 1);
   1249 			sc->sc_sleep_cnt--;
   1250 		} else {
   1251 			DELAY(1000*1000/hz);
   1252 		}
   1253 	}
   1254 	if (status != AWI_SELFTEST_PASSED) {
   1255 		printf("%s: failed to complete selftest (code %x)\n",
   1256 		    sc->sc_dev.dv_xname, status);
   1257 		return ENXIO;
   1258 	}
   1259 
   1260 	/* check banner to confirm firmware write it */
   1261 	awi_read_bytes(sc, AWI_BANNER, sc->sc_banner, AWI_BANNER_LEN);
   1262 	if (memcmp(sc->sc_banner, "PCnetMobile:", 12) != 0) {
   1263 		printf("%s: failed to complete selftest (bad banner)\n",
   1264 		    sc->sc_dev.dv_xname);
   1265 		for (i = 0; i < AWI_BANNER_LEN; i++)
   1266 			printf("%s%02x", i ? ":" : "\t", sc->sc_banner[i]);
   1267 		printf("\n");
   1268 		return ENXIO;
   1269 	}
   1270 
   1271 	/* initializing interrupt */
   1272 	sc->sc_enab_intr = 1;
   1273 	error = awi_intr_lock(sc);
   1274 	if (error)
   1275 		return error;
   1276 	intmask = AWI_INT_GROGGY | AWI_INT_SCAN_CMPLT |
   1277 	    AWI_INT_TX | AWI_INT_RX | AWI_INT_CMD;
   1278 	awi_write_1(sc, AWI_INTMASK, ~intmask & 0xff);
   1279 	awi_write_1(sc, AWI_INTMASK2, 0);
   1280 	awi_write_1(sc, AWI_INTSTAT, 0);
   1281 	awi_write_1(sc, AWI_INTSTAT2, 0);
   1282 	awi_intr_unlock(sc);
   1283 	am79c930_gcr_setbits(&sc->sc_chip, AM79C930_GCR_ENECINT);
   1284 
   1285 	/* issuing interface test command */
   1286 	error = awi_cmd(sc, AWI_CMD_NOP, AWI_WAIT);
   1287 	if (error) {
   1288 		printf("%s: failed to complete selftest", sc->sc_dev.dv_xname);
   1289 		if (error == ENXIO)
   1290 			printf(" (no hardware)\n");
   1291 		else if (error != EWOULDBLOCK)
   1292 			printf(" (error %d)\n", error);
   1293 		else if (sc->sc_cansleep)
   1294 			printf(" (lost interrupt)\n");
   1295 		else
   1296 			printf(" (command timeout)\n");
   1297 	}
   1298 	return error;
   1299 }
   1300 
   1301 /*
   1302  * Extract the factory default MIB value from firmware and assign the driver
   1303  * default value.
   1304  * Called once at attaching the interface.
   1305  */
   1306 
   1307 static int
   1308 awi_init_mibs(struct awi_softc *sc)
   1309 {
   1310 	int i, error;
   1311 	struct awi_chanset *cs;
   1312 
   1313 	if ((error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_LOCAL, AWI_WAIT)) ||
   1314 	    (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_ADDR, AWI_WAIT)) ||
   1315 	    (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MAC, AWI_WAIT)) ||
   1316 	    (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_MGT, AWI_WAIT)) ||
   1317 	    (error = awi_mib(sc, AWI_CMD_GET_MIB, AWI_MIB_PHY, AWI_WAIT))) {
   1318 		printf("%s: failed to get default mib value (error %d)\n",
   1319 		    sc->sc_dev.dv_xname, error);
   1320 		return error;
   1321 	}
   1322 
   1323 	memset(&sc->sc_ic.ic_chan_avail, 0, sizeof(sc->sc_ic.ic_chan_avail));
   1324 	for (cs = awi_chanset; ; cs++) {
   1325 		if (cs->cs_type == 0) {
   1326 			printf("%s: failed to set available channel\n",
   1327 			    sc->sc_dev.dv_xname);
   1328 			return ENXIO;
   1329 		}
   1330 		if (cs->cs_type == sc->sc_mib_phy.IEEE_PHY_Type &&
   1331 		    cs->cs_region == sc->sc_mib_phy.aCurrent_Reg_Domain)
   1332 			break;
   1333 	}
   1334 	if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
   1335 		for (i = cs->cs_min; i <= cs->cs_max; i++) {
   1336 			setbit(sc->sc_ic.ic_chan_avail,
   1337 			    IEEE80211_FH_CHAN(i % 3 + 1, i));
   1338 			/*
   1339 			 * According to the IEEE 802.11 specification,
   1340 			 * hop pattern parameter for FH phy should be
   1341 			 * incremented by 3 for given hop chanset, i.e.,
   1342 			 * the chanset parameter is calculated for given
   1343 			 * hop patter.  However, BayStack 650 Access Points
   1344 			 * apparently use fixed hop chanset parameter value
   1345 			 * 1 for any hop pattern.  So we also try this
   1346 			 * combination of hop chanset and pattern.
   1347 			 */
   1348 			setbit(sc->sc_ic.ic_chan_avail,
   1349 			    IEEE80211_FH_CHAN(1, i));
   1350 		}
   1351 	} else {
   1352 		for (i = cs->cs_min; i <= cs->cs_max; i++)
   1353 			setbit(sc->sc_ic.ic_chan_avail, i);
   1354 	}
   1355 	sc->sc_cur_chan = cs->cs_def;
   1356 
   1357 	memset(&sc->sc_mib_mac.aDesired_ESS_ID, 0, AWI_ESS_ID_SIZE);
   1358 	sc->sc_mib_mac.aDesired_ESS_ID[0] = IEEE80211_ELEMID_SSID;
   1359 	sc->sc_mib_local.Fragmentation_Dis = 1;
   1360 	sc->sc_mib_local.Accept_All_Multicast_Dis = 1;
   1361 	sc->sc_mib_local.Power_Saving_Mode_Dis = 1;
   1362 
   1363 	/* allocate buffers */
   1364 	sc->sc_txbase = AWI_BUFFERS;
   1365 	sc->sc_txend = sc->sc_txbase +
   1366 	    (AWI_TXD_SIZE + sizeof(struct ieee80211_frame) +
   1367 	    sizeof(struct ether_header) + ETHERMTU) * AWI_NTXBUFS;
   1368 	LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Offset, sc->sc_txbase);
   1369 	LE_WRITE_4(&sc->sc_mib_local.Tx_Buffer_Size,
   1370 	    sc->sc_txend - sc->sc_txbase);
   1371 	LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Offset, sc->sc_txend);
   1372 	LE_WRITE_4(&sc->sc_mib_local.Rx_Buffer_Size,
   1373 	    AWI_BUFFERS_END - sc->sc_txend);
   1374 	sc->sc_mib_local.Network_Mode = 1;
   1375 	sc->sc_mib_local.Acting_as_AP = 0;
   1376 	return 0;
   1377 }
   1378 
   1379 static int
   1380 awi_chan_check(void *arg, u_char *chanreq)
   1381 {
   1382 	struct awi_softc *sc = arg;
   1383 	int i;
   1384 	struct awi_chanset *cs;
   1385 	u_char chanlist[(IEEE80211_CHAN_MAX+1)/NBBY];
   1386 
   1387 	for (cs = awi_chanset; cs->cs_type != 0; cs++) {
   1388 		if (cs->cs_type != sc->sc_mib_phy.IEEE_PHY_Type)
   1389 			continue;
   1390 		memset(chanlist, 0, sizeof(chanlist));
   1391 		for (i = 0; ; i++) {
   1392 			if (i == IEEE80211_CHAN_MAX) {
   1393 				sc->sc_mib_phy.aCurrent_Reg_Domain =
   1394 				    cs->cs_region;
   1395 				memcpy(sc->sc_ic.ic_chan_avail, chanlist,
   1396 				    sizeof(sc->sc_ic.ic_chan_avail));
   1397 				sc->sc_ic.ic_bss.bs_chan = cs->cs_def;
   1398 				sc->sc_cur_chan = cs->cs_def;
   1399 				return 0;
   1400 			}
   1401 			if (i >= cs->cs_min && i <= cs->cs_max)
   1402 				setbit(chanlist, i);
   1403 			else if (isset(chanreq, i))
   1404 				break;
   1405 		}
   1406 	}
   1407 	return EINVAL;
   1408 }
   1409 
   1410 static int
   1411 awi_mib(struct awi_softc *sc, u_int8_t cmd, u_int8_t mib, int wflag)
   1412 {
   1413 	int error;
   1414 	u_int8_t size, *ptr;
   1415 
   1416 	switch (mib) {
   1417 	case AWI_MIB_LOCAL:
   1418 		ptr = (u_int8_t *)&sc->sc_mib_local;
   1419 		size = sizeof(sc->sc_mib_local);
   1420 		break;
   1421 	case AWI_MIB_ADDR:
   1422 		ptr = (u_int8_t *)&sc->sc_mib_addr;
   1423 		size = sizeof(sc->sc_mib_addr);
   1424 		break;
   1425 	case AWI_MIB_MAC:
   1426 		ptr = (u_int8_t *)&sc->sc_mib_mac;
   1427 		size = sizeof(sc->sc_mib_mac);
   1428 		break;
   1429 	case AWI_MIB_STAT:
   1430 		ptr = (u_int8_t *)&sc->sc_mib_stat;
   1431 		size = sizeof(sc->sc_mib_stat);
   1432 		break;
   1433 	case AWI_MIB_MGT:
   1434 		ptr = (u_int8_t *)&sc->sc_mib_mgt;
   1435 		size = sizeof(sc->sc_mib_mgt);
   1436 		break;
   1437 	case AWI_MIB_PHY:
   1438 		ptr = (u_int8_t *)&sc->sc_mib_phy;
   1439 		size = sizeof(sc->sc_mib_phy);
   1440 		break;
   1441 	default:
   1442 		return EINVAL;
   1443 	}
   1444 	if (sc->sc_cmd_inprog) {
   1445 		if ((error = awi_cmd_wait(sc)) != 0) {
   1446 			if (error == EWOULDBLOCK)
   1447 				DPRINTF(("awi_mib: cmd %d inprog",
   1448 				    sc->sc_cmd_inprog));
   1449 			return error;
   1450 		}
   1451 	}
   1452 	sc->sc_cmd_inprog = cmd;
   1453 	if (cmd == AWI_CMD_SET_MIB)
   1454 		awi_write_bytes(sc, AWI_CA_MIB_DATA, ptr, size);
   1455 	awi_write_1(sc, AWI_CA_MIB_TYPE, mib);
   1456 	awi_write_1(sc, AWI_CA_MIB_SIZE, size);
   1457 	awi_write_1(sc, AWI_CA_MIB_INDEX, 0);
   1458 	if ((error = awi_cmd(sc, cmd, wflag)) != 0)
   1459 		return error;
   1460 	if (cmd == AWI_CMD_GET_MIB) {
   1461 		awi_read_bytes(sc, AWI_CA_MIB_DATA, ptr, size);
   1462 #ifdef AWI_DEBUG
   1463 		if (awi_debug) {
   1464 			int i;
   1465 
   1466 			printf("awi_mib: #%d:", mib);
   1467 			for (i = 0; i < size; i++)
   1468 				printf(" %02x", ptr[i]);
   1469 			printf("\n");
   1470 		}
   1471 #endif
   1472 	}
   1473 	return 0;
   1474 }
   1475 
   1476 static int
   1477 awi_cmd(struct awi_softc *sc, u_int8_t cmd, int wflag)
   1478 {
   1479 	u_int8_t status;
   1480 	int error = 0;
   1481 #ifdef AWI_DEBUG
   1482 	static const char *cmdname[] = {
   1483 	    "IDLE", "NOP", "SET_MIB", "INIT_TX", "FLUSH_TX", "INIT_RX",
   1484 	    "KILL_RX", "SLEEP", "WAKE", "GET_MIB", "SCAN", "SYNC", "RESUME"
   1485 	};
   1486 #endif
   1487 
   1488 #ifdef AWI_DEBUG
   1489 	if (awi_debug > 1) {
   1490 		if (cmd >= sizeof(cmdname)/sizeof(cmdname[0]))
   1491 			printf("awi_cmd: #%d", cmd);
   1492 		else
   1493 			printf("awi_cmd: %s", cmdname[cmd]);
   1494 		printf(" %s\n", wflag == AWI_NOWAIT ? "nowait" : "wait");
   1495 	}
   1496 #endif
   1497 	sc->sc_cmd_inprog = cmd;
   1498 	awi_write_1(sc, AWI_CMD_STATUS, AWI_STAT_IDLE);
   1499 	awi_write_1(sc, AWI_CMD, cmd);
   1500 	if (wflag == AWI_NOWAIT)
   1501 		return EINPROGRESS;
   1502 	if ((error = awi_cmd_wait(sc)) != 0)
   1503 		return error;
   1504 	status = awi_read_1(sc, AWI_CMD_STATUS);
   1505 	awi_write_1(sc, AWI_CMD, 0);
   1506 	switch (status) {
   1507 	case AWI_STAT_OK:
   1508 		break;
   1509 	case AWI_STAT_BADPARM:
   1510 		return EINVAL;
   1511 	default:
   1512 		printf("%s: command %d failed %x\n",
   1513 		    sc->sc_dev.dv_xname, cmd, status);
   1514 		return ENXIO;
   1515 	}
   1516 	return 0;
   1517 }
   1518 
   1519 static int
   1520 awi_cmd_wait(struct awi_softc *sc)
   1521 {
   1522 	int i, error = 0;
   1523 
   1524 	i = 0;
   1525 	while (sc->sc_cmd_inprog) {
   1526 		if (sc->sc_invalid)
   1527 			return ENXIO;
   1528 		if (awi_read_1(sc, AWI_CMD) != sc->sc_cmd_inprog) {
   1529 			printf("%s: failed to access hardware\n",
   1530 			    sc->sc_dev.dv_xname);
   1531 			sc->sc_invalid = 1;
   1532 			return ENXIO;
   1533 		}
   1534 		if (sc->sc_cansleep) {
   1535 			sc->sc_sleep_cnt++;
   1536 			error = tsleep(sc, PWAIT, "awicmd",
   1537 			    AWI_CMD_TIMEOUT*hz/1000);
   1538 			sc->sc_sleep_cnt--;
   1539 		} else {
   1540 			if (awi_read_1(sc, AWI_CMD_STATUS) != AWI_STAT_IDLE) {
   1541 				awi_cmd_done(sc);
   1542 				break;
   1543 			}
   1544 			if (i++ >= AWI_CMD_TIMEOUT*1000/10)
   1545 				error = EWOULDBLOCK;
   1546 			else
   1547 				DELAY(10);
   1548 		}
   1549 		if (error)
   1550 			break;
   1551 	}
   1552 	if (error) {
   1553 		DPRINTF(("awi_cmd_wait: cmd 0x%x, error %d\n",
   1554 		    sc->sc_cmd_inprog, error));
   1555 	}
   1556 	return error;
   1557 }
   1558 
   1559 static void
   1560 awi_cmd_done(struct awi_softc *sc)
   1561 {
   1562 	u_int8_t cmd, status;
   1563 
   1564 	status = awi_read_1(sc, AWI_CMD_STATUS);
   1565 	if (status == AWI_STAT_IDLE)
   1566 		return;		/* stray interrupt */
   1567 
   1568 	cmd = sc->sc_cmd_inprog;
   1569 	sc->sc_cmd_inprog = 0;
   1570 	wakeup(sc);
   1571 	awi_write_1(sc, AWI_CMD, 0);
   1572 
   1573 	if (status != AWI_STAT_OK) {
   1574 		printf("%s: command %d failed %x\n",
   1575 		    sc->sc_dev.dv_xname, cmd, status);
   1576 		sc->sc_substate = AWI_ST_NONE;
   1577 		return;
   1578 	}
   1579 	if (sc->sc_substate != AWI_ST_NONE)
   1580 		(void)ieee80211_new_state(&sc->sc_ic.ic_if, sc->sc_nstate, -1);
   1581 }
   1582 
   1583 static int
   1584 awi_next_txd(struct awi_softc *sc, int len, u_int32_t *framep, u_int32_t *ntxdp)
   1585 {
   1586 	u_int32_t txd, ntxd, frame;
   1587 
   1588 	txd = sc->sc_txnext;
   1589 	frame = txd + AWI_TXD_SIZE;
   1590 	if (frame + len > sc->sc_txend)
   1591 		frame = sc->sc_txbase;
   1592 	ntxd = frame + len;
   1593 	if (ntxd + AWI_TXD_SIZE > sc->sc_txend)
   1594 		ntxd = sc->sc_txbase;
   1595 	*framep = frame;
   1596 	*ntxdp = ntxd;
   1597 	/*
   1598 	 * Determine if there are any room in ring buffer.
   1599 	 *		--- send wait,  === new data,  +++ conflict (ENOBUFS)
   1600 	 *   base........................end
   1601 	 *	   done----txd=====ntxd		OK
   1602 	 *	 --txd=====done++++ntxd--	full
   1603 	 *	 --txd=====ntxd    done--	OK
   1604 	 *	 ==ntxd    done----txd===	OK
   1605 	 *	 ==done++++ntxd----txd===	full
   1606 	 *	 ++ntxd    txd=====done++	full
   1607 	 */
   1608 	if (txd < ntxd) {
   1609 		if (txd < sc->sc_txdone && ntxd + AWI_TXD_SIZE > sc->sc_txdone)
   1610 			return ENOBUFS;
   1611 	} else {
   1612 		if (txd < sc->sc_txdone || ntxd + AWI_TXD_SIZE > sc->sc_txdone)
   1613 			return ENOBUFS;
   1614 	}
   1615 	return 0;
   1616 }
   1617 
   1618 static int
   1619 awi_lock(struct awi_softc *sc)
   1620 {
   1621 	int error = 0;
   1622 
   1623 	if (curproc == NULL) {
   1624 		/*
   1625 		 * XXX
   1626 		 * Though driver ioctl should be called with context,
   1627 		 * KAME ipv6 stack calls ioctl in interrupt for now.
   1628 		 * We simply abort the request if there are other
   1629 		 * ioctl requests in progress.
   1630 		 */
   1631 		if (sc->sc_busy) {
   1632 			return EWOULDBLOCK;
   1633 			if (sc->sc_invalid)
   1634 				return ENXIO;
   1635 		}
   1636 		sc->sc_busy = 1;
   1637 		sc->sc_cansleep = 0;
   1638 		return 0;
   1639 	}
   1640 	while (sc->sc_busy) {
   1641 		if (sc->sc_invalid)
   1642 			return ENXIO;
   1643 		sc->sc_sleep_cnt++;
   1644 		error = tsleep(sc, PWAIT | PCATCH, "awilck", 0);
   1645 		sc->sc_sleep_cnt--;
   1646 		if (error)
   1647 			return error;
   1648 	}
   1649 	sc->sc_busy = 1;
   1650 	sc->sc_cansleep = 1;
   1651 	return 0;
   1652 }
   1653 
   1654 static void
   1655 awi_unlock(struct awi_softc *sc)
   1656 {
   1657 	sc->sc_busy = 0;
   1658 	sc->sc_cansleep = 0;
   1659 	if (sc->sc_sleep_cnt)
   1660 		wakeup(sc);
   1661 }
   1662 
   1663 static int
   1664 awi_intr_lock(struct awi_softc *sc)
   1665 {
   1666 	u_int8_t status;
   1667 	int i, retry;
   1668 
   1669 	status = 1;
   1670 	for (retry = 0; retry < 10; retry++) {
   1671 		for (i = 0; i < AWI_LOCKOUT_TIMEOUT*1000/5; i++) {
   1672 			if ((status = awi_read_1(sc, AWI_LOCKOUT_HOST)) == 0)
   1673 				break;
   1674 			DELAY(5);
   1675 		}
   1676 		if (status != 0)
   1677 			break;
   1678 		awi_write_1(sc, AWI_LOCKOUT_MAC, 1);
   1679 		if ((status = awi_read_1(sc, AWI_LOCKOUT_HOST)) == 0)
   1680 			break;
   1681 		awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
   1682 	}
   1683 	if (status != 0) {
   1684 		printf("%s: failed to lock interrupt\n",
   1685 		    sc->sc_dev.dv_xname);
   1686 		return ENXIO;
   1687 	}
   1688 	return 0;
   1689 }
   1690 
   1691 static void
   1692 awi_intr_unlock(struct awi_softc *sc)
   1693 {
   1694 
   1695 	awi_write_1(sc, AWI_LOCKOUT_MAC, 0);
   1696 }
   1697 
   1698 static int
   1699 awi_newstate(void *arg, enum ieee80211_state nstate)
   1700 {
   1701 	struct awi_softc *sc = arg;
   1702 	struct ieee80211com *ic = &sc->sc_ic;
   1703 	struct ieee80211_bss *bs = &ic->ic_bss;
   1704 	struct ifnet *ifp = &ic->ic_if;
   1705 	int error;
   1706 	u_int8_t newmode;
   1707 	enum ieee80211_state ostate;
   1708 #ifdef AWI_DEBUG
   1709 	static const char *stname[] =
   1710 	    { "INIT", "SCAN", "AUTH", "ASSOC", "RUN" };
   1711 	static const char *substname[] =
   1712 	    { "NONE", "SCAN_INIT", "SCAN_SETMIB", "SCAN_SCCMD",
   1713 	      "SUB_INIT", "SUB_SETSS", "SUB_SYNC" };
   1714 #endif /* AWI_DEBUG */
   1715 
   1716 	ostate = ic->ic_state;
   1717 	DPRINTF(("awi_newstate: %s (%s/%s) -> %s\n", stname[ostate],
   1718 	    stname[sc->sc_nstate], substname[sc->sc_substate], stname[nstate]));
   1719 
   1720 	/* set LED */
   1721 	switch (nstate) {
   1722 	case IEEE80211_S_INIT:
   1723 		awi_drvstate(sc, AWI_DRV_RESET);
   1724 		break;
   1725 	case IEEE80211_S_SCAN:
   1726 		if (ic->ic_flags & IEEE80211_F_ADHOC)
   1727 			awi_drvstate(sc, AWI_DRV_ADHSC);
   1728 		else
   1729 			awi_drvstate(sc, AWI_DRV_INFSY);
   1730 		break;
   1731 	case IEEE80211_S_AUTH:
   1732 		awi_drvstate(sc, AWI_DRV_INFSY);
   1733 		break;
   1734 	case IEEE80211_S_ASSOC:
   1735 		awi_drvstate(sc, AWI_DRV_INFAUTH);
   1736 		break;
   1737 	case IEEE80211_S_RUN:
   1738 		if (ic->ic_flags & IEEE80211_F_ADHOC)
   1739 			awi_drvstate(sc, AWI_DRV_ADHSY);
   1740 		else
   1741 			awi_drvstate(sc, AWI_DRV_INFASSOC);
   1742 		break;
   1743 	}
   1744 
   1745 	if (nstate == IEEE80211_S_INIT) {
   1746 		sc->sc_substate = AWI_ST_NONE;
   1747 		ic->ic_flags &= ~IEEE80211_F_SIBSS;
   1748 		return 0;
   1749 	}
   1750 
   1751 	/* state transition */
   1752 	if (nstate == IEEE80211_S_SCAN) {
   1753 		/* SCAN substate */
   1754 		if (sc->sc_substate == AWI_ST_NONE) {
   1755 			sc->sc_nstate = nstate;	/* next state in transition */
   1756 			sc->sc_substate = AWI_ST_SCAN_INIT;
   1757 		}
   1758 		switch (sc->sc_substate) {
   1759 		case AWI_ST_SCAN_INIT:
   1760 			sc->sc_substate = AWI_ST_SCAN_SETMIB;
   1761 			switch (ostate) {
   1762 			case IEEE80211_S_RUN:
   1763 				/* beacon miss */
   1764 				if (ifp->if_flags & IFF_DEBUG)
   1765 					printf("%s: no recent beacons from %s;"
   1766 					    " rescanning\n",
   1767 					    ifp->if_xname,
   1768 					    ether_sprintf(ic->ic_bss.bs_bssid));
   1769 				/* FALLTHRU */
   1770 			case IEEE80211_S_AUTH:
   1771 			case IEEE80211_S_ASSOC:
   1772 				/* timeout restart scan */
   1773 				ieee80211_free_scan(ifp);
   1774 				/* FALLTHRU */
   1775 			case IEEE80211_S_INIT:
   1776 				ic->ic_flags |= IEEE80211_F_ASCAN;
   1777 				ic->ic_scan_timer = 0;
   1778 				/* FALLTHRU */
   1779 			case IEEE80211_S_SCAN:
   1780 				/* scan next */
   1781 				break;
   1782 			}
   1783 			if (ic->ic_flags & IEEE80211_F_ASCAN)
   1784 				newmode = AWI_SCAN_ACTIVE;
   1785 			else
   1786 				newmode = AWI_SCAN_PASSIVE;
   1787 			if (sc->sc_mib_mgt.aScan_Mode != newmode) {
   1788 				sc->sc_mib_mgt.aScan_Mode = newmode;
   1789 				if ((error = awi_mib(sc, AWI_CMD_SET_MIB,
   1790 				    AWI_MIB_MGT, AWI_NOWAIT)) != 0)
   1791 					break;
   1792 			}
   1793 			/* FALLTHRU */
   1794 		case AWI_ST_SCAN_SETMIB:
   1795 			sc->sc_substate = AWI_ST_SCAN_SCCMD;
   1796 			if (sc->sc_cmd_inprog) {
   1797 				if ((error = awi_cmd_wait(sc)) != 0)
   1798 					break;
   1799 			}
   1800 			sc->sc_cmd_inprog = AWI_CMD_SCAN;
   1801 			awi_write_2(sc, AWI_CA_SCAN_DURATION,
   1802 			    (ic->ic_flags & IEEE80211_F_ASCAN) ?
   1803 			    AWI_ASCAN_DURATION : AWI_PSCAN_DURATION);
   1804 			if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
   1805 				awi_write_1(sc, AWI_CA_SCAN_SET,
   1806 				    IEEE80211_FH_CHANSET(bs->bs_chan));
   1807 				awi_write_1(sc, AWI_CA_SCAN_PATTERN,
   1808 				    IEEE80211_FH_CHANPAT(bs->bs_chan));
   1809 				awi_write_1(sc, AWI_CA_SCAN_IDX, 1);
   1810 			} else {
   1811 				awi_write_1(sc, AWI_CA_SCAN_SET, bs->bs_chan);
   1812 				awi_write_1(sc, AWI_CA_SCAN_PATTERN, 0);
   1813 				awi_write_1(sc, AWI_CA_SCAN_IDX, 0);
   1814 			}
   1815 			awi_write_1(sc, AWI_CA_SCAN_SUSP, 0);
   1816 			sc->sc_cur_chan = bs->bs_chan;
   1817 			if ((error = awi_cmd(sc, AWI_CMD_SCAN, AWI_NOWAIT))
   1818 			    != 0)
   1819 				break;
   1820 			/* FALLTHRU */
   1821 		case AWI_ST_SCAN_SCCMD:
   1822 			if (ic->ic_scan_timer == 0)
   1823 				ic->ic_scan_timer =
   1824 				    (ic->ic_flags & IEEE80211_F_ASCAN) ?
   1825 				    IEEE80211_ASCAN_WAIT : IEEE80211_PSCAN_WAIT;
   1826 			ifp->if_timer = 1;
   1827 			ic->ic_state = nstate;
   1828 			sc->sc_substate = AWI_ST_NONE;
   1829 			error = EINPROGRESS;
   1830 			break;
   1831 		default:
   1832 			DPRINTF(("awi_newstate: unexpected state %s/%s\n",
   1833 			    stname[nstate], substname[sc->sc_substate]));
   1834 			sc->sc_substate = AWI_ST_NONE;
   1835 			error = EIO;
   1836 			break;
   1837 		}
   1838 		return error;
   1839 	}
   1840 
   1841 	if (ostate == IEEE80211_S_SCAN) {
   1842 		/* set SSID and channel */
   1843 		/* substate */
   1844 		if (sc->sc_substate == AWI_ST_NONE) {
   1845 			sc->sc_nstate = nstate;	/* next state in transition */
   1846 			sc->sc_substate = AWI_ST_SUB_INIT;
   1847 		}
   1848 		switch (sc->sc_substate) {
   1849 		case AWI_ST_SUB_INIT:
   1850 			sc->sc_substate = AWI_ST_SUB_SETSS;
   1851 			memcpy(&sc->sc_mib_mgt.aCurrent_BSS_ID, bs->bs_bssid,
   1852 			    IEEE80211_ADDR_LEN);
   1853 			memset(&sc->sc_mib_mgt.aCurrent_ESS_ID, 0,
   1854 			    AWI_ESS_ID_SIZE);
   1855 			sc->sc_mib_mgt.aCurrent_ESS_ID[0] =
   1856 			    IEEE80211_ELEMID_SSID;
   1857 			sc->sc_mib_mgt.aCurrent_ESS_ID[1] = bs->bs_esslen;
   1858 			memcpy(&sc->sc_mib_mgt.aCurrent_ESS_ID[2],
   1859 			    bs->bs_essid, bs->bs_esslen);
   1860 			LE_WRITE_2(&sc->sc_mib_mgt.aBeacon_Period,
   1861 			    bs->bs_intval);
   1862 			if ((error = awi_mib(sc, AWI_CMD_SET_MIB, AWI_MIB_MGT,
   1863 			    AWI_NOWAIT)) != 0)
   1864 				break;
   1865 			/* FALLTHRU */
   1866 		case AWI_ST_SUB_SETSS:
   1867 			sc->sc_substate = AWI_ST_SUB_SYNC;
   1868 			if (sc->sc_cmd_inprog) {
   1869 				if ((error = awi_cmd_wait(sc)) != 0)
   1870 					break;
   1871 			}
   1872 			sc->sc_cmd_inprog = AWI_CMD_SYNC;
   1873 			if (sc->sc_mib_phy.IEEE_PHY_Type == AWI_PHY_TYPE_FH) {
   1874 				awi_write_1(sc, AWI_CA_SYNC_SET,
   1875 				    IEEE80211_FH_CHANSET(bs->bs_chan));
   1876 				awi_write_1(sc, AWI_CA_SYNC_PATTERN,
   1877 				    IEEE80211_FH_CHANPAT(bs->bs_chan));
   1878 				awi_write_1(sc, AWI_CA_SYNC_IDX,
   1879 				    bs->bs_fhindex);
   1880 				awi_write_2(sc, AWI_CA_SYNC_DWELL,
   1881 				    bs->bs_fhdwell);
   1882 			} else {
   1883 				awi_write_1(sc, AWI_CA_SYNC_SET, bs->bs_chan);
   1884 				awi_write_1(sc, AWI_CA_SYNC_PATTERN, 0);
   1885 				awi_write_1(sc, AWI_CA_SYNC_IDX, 0);
   1886 				awi_write_2(sc, AWI_CA_SYNC_DWELL, 0);
   1887 			}
   1888 			if ((ic->ic_flags & IEEE80211_F_SIBSS) &&
   1889 			    !sc->sc_no_bssid)
   1890 				awi_write_1(sc, AWI_CA_SYNC_STARTBSS, 1);
   1891 			else
   1892 				awi_write_1(sc, AWI_CA_SYNC_STARTBSS, 0);
   1893 			awi_write_2(sc, AWI_CA_SYNC_MBZ, 0);
   1894 			awi_write_bytes(sc, AWI_CA_SYNC_TIMESTAMP,
   1895 			    bs->bs_tstamp, 8);
   1896 			awi_write_4(sc, AWI_CA_SYNC_REFTIME, bs->bs_rstamp);
   1897 			sc->sc_cur_chan = bs->bs_chan;
   1898 			if ((error = awi_cmd(sc, AWI_CMD_SYNC, AWI_NOWAIT))
   1899 			    != 0)
   1900 				break;
   1901 			/* FALLTHRU */
   1902 		case AWI_ST_SUB_SYNC:
   1903 			sc->sc_substate = AWI_ST_NONE;
   1904 			if (ic->ic_flags & IEEE80211_F_SIBSS) {
   1905 				if ((error = awi_mib(sc, AWI_CMD_GET_MIB,
   1906 				    AWI_MIB_MGT, AWI_WAIT)) != 0)
   1907 					break;
   1908 				memcpy(bs->bs_bssid,
   1909 				    &sc->sc_mib_mgt.aCurrent_BSS_ID,
   1910 				    IEEE80211_ADDR_LEN);
   1911 			} else {
   1912 				if (nstate == IEEE80211_S_RUN) {
   1913 					sc->sc_rx_timer = 10;
   1914 					ifp->if_timer = 1;
   1915 				}
   1916 			}
   1917 			error = 0;
   1918 			break;
   1919 		default:
   1920 			DPRINTF(("awi_newstate: unexpected state %s/%s\n",
   1921 			    stname[nstate], substname[sc->sc_substate]));
   1922 			sc->sc_substate = AWI_ST_NONE;
   1923 			error = EIO;
   1924 			break;
   1925 		}
   1926 		return error;
   1927 	}
   1928 
   1929 	sc->sc_substate = AWI_ST_NONE;
   1930 
   1931 	return 0;
   1932 }
   1933 
   1934 static struct mbuf *
   1935 awi_ether_encap(struct awi_softc *sc, struct mbuf *m)
   1936 {
   1937 	struct ieee80211com *ic = &sc->sc_ic;
   1938 	struct ieee80211_bss *bs = &ic->ic_bss;
   1939 	struct ether_header *eh;
   1940 	struct ieee80211_frame *wh;
   1941 
   1942 	if (m->m_len < sizeof(struct ether_header)) {
   1943 		m = m_pullup(m, sizeof(struct ether_header));
   1944 		if (m == NULL)
   1945 			return NULL;
   1946 	}
   1947 	eh = mtod(m, struct ether_header *);
   1948 	M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
   1949 	if (m == NULL)
   1950 		return NULL;
   1951 	wh = mtod(m, struct ieee80211_frame *);
   1952 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
   1953 	*(u_int16_t *)wh->i_dur = 0;
   1954 	*(u_int16_t *)wh->i_seq =
   1955 	    htole16(bs->bs_txseq << IEEE80211_SEQ_SEQ_SHIFT);
   1956 	bs->bs_txseq++;
   1957 	if (ic->ic_flags & IEEE80211_F_ADHOC) {
   1958 		wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
   1959 		if (sc->sc_adhoc_ap)
   1960 			memcpy(wh->i_addr1, bs->bs_macaddr, IEEE80211_ADDR_LEN);
   1961 		else
   1962 			memcpy(wh->i_addr1, eh->ether_dhost,
   1963 			    IEEE80211_ADDR_LEN);
   1964 		memcpy(wh->i_addr2, eh->ether_shost, IEEE80211_ADDR_LEN);
   1965 		memcpy(wh->i_addr3, bs->bs_bssid, IEEE80211_ADDR_LEN);
   1966 	} else {
   1967 		wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
   1968 		memcpy(wh->i_addr1, bs->bs_bssid, IEEE80211_ADDR_LEN);
   1969 		memcpy(wh->i_addr2, eh->ether_shost, IEEE80211_ADDR_LEN);
   1970 		memcpy(wh->i_addr3, eh->ether_dhost, IEEE80211_ADDR_LEN);
   1971 	}
   1972 	return m;
   1973 }
   1974 
   1975 static struct mbuf *
   1976 awi_ether_modcap(struct awi_softc *sc, struct mbuf *m)
   1977 {
   1978 	struct ieee80211com *ic = &sc->sc_ic;
   1979 	struct ether_header eh;
   1980 	struct ieee80211_frame wh;
   1981 	struct llc *llc;
   1982 
   1983 	if (m->m_len < sizeof(wh) + sizeof(eh)) {
   1984 		m = m_pullup(m, sizeof(wh) + sizeof(eh));
   1985 		if (m == NULL)
   1986 			return NULL;
   1987 	}
   1988 	memcpy(&wh, mtod(m, caddr_t), sizeof(wh));
   1989 	if (wh.i_fc[0] != (IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA))
   1990 		return m;
   1991 	memcpy(&eh, mtod(m, caddr_t) + sizeof(wh), sizeof(eh));
   1992 	m_adj(m, sizeof(eh) - sizeof(*llc));
   1993 	if (ic->ic_flags & IEEE80211_F_ADHOC)
   1994 		memcpy(wh.i_addr2, eh.ether_shost, IEEE80211_ADDR_LEN);
   1995 	memcpy(mtod(m, caddr_t), &wh, sizeof(wh));
   1996 	llc = (struct llc *)(mtod(m, caddr_t) + sizeof(wh));
   1997 	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
   1998 	llc->llc_control = LLC_UI;
   1999 	llc->llc_snap.org_code[0] = 0;
   2000 	llc->llc_snap.org_code[1] = 0;
   2001 	llc->llc_snap.org_code[2] = 0;
   2002 	llc->llc_snap.ether_type = eh.ether_type;
   2003 	return m;
   2004 }
   2005