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