Home | History | Annotate | Line # | Download | only in ic
rtw.c revision 1.128
      1  1.128   msaitoh /* $NetBSD: rtw.c,v 1.128 2018/06/26 06:48:00 msaitoh Exp $ */
      2    1.1    dyoung /*-
      3   1.93    dyoung  * Copyright (c) 2004, 2005, 2006, 2007 David Young.  All rights
      4   1.93    dyoung  * reserved.
      5    1.1    dyoung  *
      6    1.1    dyoung  * Programmed for NetBSD by David Young.
      7    1.1    dyoung  *
      8    1.1    dyoung  * Redistribution and use in source and binary forms, with or without
      9    1.1    dyoung  * modification, are permitted provided that the following conditions
     10    1.1    dyoung  * are met:
     11    1.1    dyoung  * 1. Redistributions of source code must retain the above copyright
     12    1.1    dyoung  *    notice, this list of conditions and the following disclaimer.
     13    1.1    dyoung  * 2. Redistributions in binary form must reproduce the above copyright
     14    1.1    dyoung  *    notice, this list of conditions and the following disclaimer in the
     15    1.1    dyoung  *    documentation and/or other materials provided with the distribution.
     16    1.1    dyoung  *
     17    1.1    dyoung  * THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY
     18    1.1    dyoung  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     19    1.1    dyoung  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     20    1.1    dyoung  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL David
     21    1.1    dyoung  * Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     22    1.1    dyoung  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
     23    1.1    dyoung  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24    1.1    dyoung  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     25    1.1    dyoung  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26    1.1    dyoung  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27    1.1    dyoung  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
     28    1.1    dyoung  * OF SUCH DAMAGE.
     29    1.1    dyoung  */
     30    1.1    dyoung /*
     31    1.1    dyoung  * Device driver for the Realtek RTL8180 802.11 MAC/BBP.
     32    1.1    dyoung  */
     33    1.1    dyoung 
     34    1.1    dyoung #include <sys/cdefs.h>
     35  1.128   msaitoh __KERNEL_RCSID(0, "$NetBSD: rtw.c,v 1.128 2018/06/26 06:48:00 msaitoh Exp $");
     36    1.1    dyoung 
     37    1.1    dyoung 
     38    1.1    dyoung #include <sys/param.h>
     39    1.4    dyoung #include <sys/sysctl.h>
     40   1.44     perry #include <sys/systm.h>
     41    1.1    dyoung #include <sys/callout.h>
     42   1.44     perry #include <sys/mbuf.h>
     43    1.1    dyoung #include <sys/malloc.h>
     44    1.1    dyoung #include <sys/kernel.h>
     45    1.1    dyoung #include <sys/time.h>
     46    1.1    dyoung #include <sys/types.h>
     47   1.99   tsutsui #include <sys/device.h>
     48  1.118  uebayasi #include <sys/sockio.h>
     49    1.1    dyoung 
     50    1.1    dyoung #include <machine/endian.h>
     51   1.91        ad #include <sys/bus.h>
     52   1.91        ad #include <sys/intr.h>	/* splnet */
     53    1.1    dyoung 
     54    1.1    dyoung #include <net/if.h>
     55    1.1    dyoung #include <net/if_media.h>
     56    1.1    dyoung #include <net/if_ether.h>
     57    1.1    dyoung 
     58   1.48    dyoung #include <net80211/ieee80211_netbsd.h>
     59    1.1    dyoung #include <net80211/ieee80211_var.h>
     60    1.1    dyoung #include <net80211/ieee80211_radiotap.h>
     61    1.1    dyoung 
     62    1.1    dyoung #include <net/bpf.h>
     63    1.1    dyoung 
     64    1.1    dyoung #include <dev/ic/rtwreg.h>
     65    1.1    dyoung #include <dev/ic/rtwvar.h>
     66    1.1    dyoung #include <dev/ic/rtwphyio.h>
     67    1.1    dyoung #include <dev/ic/rtwphy.h>
     68    1.1    dyoung 
     69    1.1    dyoung #include <dev/ic/smc93cx6var.h>
     70    1.1    dyoung 
     71   1.58    dyoung static int rtw_rfprog_fallback = 0;
     72   1.58    dyoung static int rtw_host_rfio = 0;
     73    1.4    dyoung 
     74    1.1    dyoung #ifdef RTW_DEBUG
     75   1.21    dyoung int rtw_debug = 0;
     76   1.58    dyoung static int rtw_rxbufs_limit = RTW_RXQLEN;
     77    1.1    dyoung #endif /* RTW_DEBUG */
     78    1.1    dyoung 
     79   1.21    dyoung #define NEXT_ATTACH_STATE(sc, state) do {			\
     80   1.21    dyoung 	DPRINTF(sc, RTW_DEBUG_ATTACH,				\
     81   1.21    dyoung 	    ("%s: attach state %s\n", __func__, #state));	\
     82   1.21    dyoung 	sc->sc_attach_state = state;				\
     83    1.1    dyoung } while (0)
     84    1.1    dyoung 
     85   1.26    dyoung int rtw_dwelltime = 200;	/* milliseconds */
     86   1.50    dyoung static struct ieee80211_cipher rtw_cipher_wep;
     87    1.1    dyoung 
     88  1.101    dyoung static void rtw_disable_interrupts(struct rtw_regs *);
     89  1.101    dyoung static void rtw_enable_interrupts(struct rtw_softc *);
     90  1.101    dyoung 
     91  1.101    dyoung static int rtw_init(struct ifnet *);
     92  1.125    nonaka static void rtw_softintr(void *);
     93  1.101    dyoung 
     94    1.5    dyoung static void rtw_start(struct ifnet *);
     95   1.58    dyoung static void rtw_reset_oactive(struct rtw_softc *);
     96   1.58    dyoung static struct mbuf *rtw_beacon_alloc(struct rtw_softc *,
     97   1.58    dyoung     struct ieee80211_node *);
     98   1.58    dyoung static u_int rtw_txring_next(struct rtw_regs *, struct rtw_txdesc_blk *);
     99    1.5    dyoung 
    100   1.83    dyoung static void rtw_io_enable(struct rtw_softc *, uint8_t, int);
    101   1.49    dyoung static int rtw_key_delete(struct ieee80211com *, const struct ieee80211_key *);
    102   1.49    dyoung static int rtw_key_set(struct ieee80211com *, const struct ieee80211_key *,
    103   1.49    dyoung     const u_int8_t[IEEE80211_ADDR_LEN]);
    104   1.49    dyoung static void rtw_key_update_end(struct ieee80211com *);
    105   1.49    dyoung static void rtw_key_update_begin(struct ieee80211com *);
    106   1.54    dogcow static int rtw_wep_decap(struct ieee80211_key *, struct mbuf *, int);
    107   1.49    dyoung static void rtw_wep_setkeys(struct rtw_softc *, struct ieee80211_key *, int);
    108   1.49    dyoung 
    109   1.45    dyoung static void rtw_led_attach(struct rtw_led_state *, void *);
    110  1.101    dyoung static void rtw_led_detach(struct rtw_led_state *);
    111   1.42    dyoung static void rtw_led_init(struct rtw_regs *);
    112   1.42    dyoung static void rtw_led_slowblink(void *);
    113   1.42    dyoung static void rtw_led_fastblink(void *);
    114   1.42    dyoung static void rtw_led_set(struct rtw_led_state *, struct rtw_regs *, int);
    115   1.42    dyoung 
    116    1.4    dyoung static int rtw_sysctl_verify_rfio(SYSCTLFN_PROTO);
    117    1.4    dyoung static int rtw_sysctl_verify_rfprog(SYSCTLFN_PROTO);
    118    1.4    dyoung #ifdef RTW_DEBUG
    119   1.83    dyoung static void rtw_dump_rings(struct rtw_softc *sc);
    120   1.21    dyoung static void rtw_print_txdesc(struct rtw_softc *, const char *,
    121   1.34    dyoung     struct rtw_txsoft *, struct rtw_txdesc_blk *, int);
    122    1.4    dyoung static int rtw_sysctl_verify_debug(SYSCTLFN_PROTO);
    123   1.31    dyoung static int rtw_sysctl_verify_rxbufs_limit(SYSCTLFN_PROTO);
    124    1.4    dyoung #endif /* RTW_DEBUG */
    125   1.83    dyoung #ifdef RTW_DIAG
    126   1.83    dyoung static void rtw_txring_fixup(struct rtw_softc *sc, const char *fn, int ln);
    127   1.83    dyoung #endif /* RTW_DIAG */
    128    1.4    dyoung 
    129    1.4    dyoung /*
    130    1.4    dyoung  * Setup sysctl(3) MIB, hw.rtw.*
    131    1.4    dyoung  *
    132  1.106        ad  * TBD condition CTLFLAG_PERMANENT on being a module or not
    133    1.4    dyoung  */
    134    1.4    dyoung SYSCTL_SETUP(sysctl_rtw, "sysctl rtw(4) subtree setup")
    135    1.4    dyoung {
    136    1.4    dyoung 	int rc;
    137   1.47    atatat 	const struct sysctlnode *cnode, *rnode;
    138    1.4    dyoung 
    139    1.4    dyoung 	if ((rc = sysctl_createv(clog, 0, NULL, &rnode,
    140    1.4    dyoung 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "rtw",
    141    1.4    dyoung 	    "Realtek RTL818x 802.11 controls",
    142  1.121     pooka 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
    143    1.4    dyoung 		goto err;
    144    1.4    dyoung 
    145    1.4    dyoung #ifdef RTW_DEBUG
    146    1.4    dyoung 	/* control debugging printfs */
    147    1.4    dyoung 	if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
    148    1.4    dyoung 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    149    1.4    dyoung 	    "debug", SYSCTL_DESCR("Enable RTL818x debugging output"),
    150    1.4    dyoung 	    rtw_sysctl_verify_debug, 0, &rtw_debug, 0,
    151    1.4    dyoung 	    CTL_CREATE, CTL_EOL)) != 0)
    152    1.4    dyoung 		goto err;
    153   1.31    dyoung 
    154   1.31    dyoung 	/* Limit rx buffers, for simulating resource exhaustion. */
    155   1.31    dyoung 	if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
    156   1.31    dyoung 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    157   1.31    dyoung 	    "rxbufs_limit",
    158   1.31    dyoung 	    SYSCTL_DESCR("Set rx buffers limit"),
    159   1.31    dyoung 	    rtw_sysctl_verify_rxbufs_limit, 0, &rtw_rxbufs_limit, 0,
    160   1.31    dyoung 	    CTL_CREATE, CTL_EOL)) != 0)
    161   1.31    dyoung 		goto err;
    162   1.31    dyoung 
    163    1.4    dyoung #endif /* RTW_DEBUG */
    164    1.4    dyoung 	/* set fallback RF programming method */
    165    1.4    dyoung 	if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
    166    1.4    dyoung 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    167    1.4    dyoung 	    "rfprog_fallback",
    168    1.4    dyoung 	    SYSCTL_DESCR("Set fallback RF programming method"),
    169    1.4    dyoung 	    rtw_sysctl_verify_rfprog, 0, &rtw_rfprog_fallback, 0,
    170    1.4    dyoung 	    CTL_CREATE, CTL_EOL)) != 0)
    171    1.4    dyoung 		goto err;
    172    1.4    dyoung 
    173    1.4    dyoung 	/* force host to control RF I/O bus */
    174    1.4    dyoung 	if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
    175    1.4    dyoung 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    176    1.4    dyoung 	    "host_rfio", SYSCTL_DESCR("Enable host control of RF I/O"),
    177    1.4    dyoung 	    rtw_sysctl_verify_rfio, 0, &rtw_host_rfio, 0,
    178    1.4    dyoung 	    CTL_CREATE, CTL_EOL)) != 0)
    179    1.4    dyoung 		goto err;
    180    1.4    dyoung 
    181    1.4    dyoung 	return;
    182    1.4    dyoung err:
    183    1.4    dyoung 	printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
    184    1.4    dyoung }
    185    1.4    dyoung 
    186    1.4    dyoung static int
    187    1.4    dyoung rtw_sysctl_verify(SYSCTLFN_ARGS, int lower, int upper)
    188    1.4    dyoung {
    189    1.4    dyoung 	int error, t;
    190    1.4    dyoung 	struct sysctlnode node;
    191    1.4    dyoung 
    192    1.4    dyoung 	node = *rnode;
    193    1.4    dyoung 	t = *(int*)rnode->sysctl_data;
    194    1.4    dyoung 	node.sysctl_data = &t;
    195    1.4    dyoung 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    196    1.4    dyoung 	if (error || newp == NULL)
    197    1.4    dyoung 		return (error);
    198    1.4    dyoung 
    199    1.4    dyoung 	if (t < lower || t > upper)
    200    1.4    dyoung 		return (EINVAL);
    201    1.4    dyoung 
    202    1.4    dyoung 	*(int*)rnode->sysctl_data = t;
    203    1.4    dyoung 
    204    1.4    dyoung 	return (0);
    205    1.4    dyoung }
    206    1.4    dyoung 
    207    1.4    dyoung static int
    208    1.4    dyoung rtw_sysctl_verify_rfprog(SYSCTLFN_ARGS)
    209    1.4    dyoung {
    210   1.46    dyoung 	return rtw_sysctl_verify(SYSCTLFN_CALL(__UNCONST(rnode)), 0,
    211   1.75    dyoung 	    __SHIFTOUT(RTW_CONFIG4_RFTYPE_MASK, RTW_CONFIG4_RFTYPE_MASK));
    212    1.4    dyoung }
    213    1.4    dyoung 
    214    1.4    dyoung static int
    215    1.4    dyoung rtw_sysctl_verify_rfio(SYSCTLFN_ARGS)
    216    1.4    dyoung {
    217   1.46    dyoung 	return rtw_sysctl_verify(SYSCTLFN_CALL(__UNCONST(rnode)), 0, 1);
    218    1.4    dyoung }
    219    1.4    dyoung 
    220    1.1    dyoung #ifdef RTW_DEBUG
    221    1.4    dyoung static int
    222    1.4    dyoung rtw_sysctl_verify_debug(SYSCTLFN_ARGS)
    223    1.4    dyoung {
    224   1.46    dyoung 	return rtw_sysctl_verify(SYSCTLFN_CALL(__UNCONST(rnode)),
    225   1.46    dyoung 	    0, RTW_DEBUG_MAX);
    226    1.4    dyoung }
    227    1.4    dyoung 
    228   1.31    dyoung static int
    229   1.31    dyoung rtw_sysctl_verify_rxbufs_limit(SYSCTLFN_ARGS)
    230   1.31    dyoung {
    231   1.46    dyoung 	return rtw_sysctl_verify(SYSCTLFN_CALL(__UNCONST(rnode)),
    232   1.46    dyoung 	    0, RTW_RXQLEN);
    233   1.31    dyoung }
    234   1.31    dyoung 
    235    1.1    dyoung static void
    236    1.1    dyoung rtw_print_regs(struct rtw_regs *regs, const char *dvname, const char *where)
    237    1.1    dyoung {
    238   1.21    dyoung #define PRINTREG32(sc, reg)				\
    239   1.21    dyoung 	RTW_DPRINTF(RTW_DEBUG_REGDUMP,			\
    240   1.21    dyoung 	    ("%s: reg[ " #reg " / %03x ] = %08x\n",	\
    241    1.1    dyoung 	    dvname, reg, RTW_READ(regs, reg)))
    242    1.1    dyoung 
    243   1.21    dyoung #define PRINTREG16(sc, reg)				\
    244   1.21    dyoung 	RTW_DPRINTF(RTW_DEBUG_REGDUMP,			\
    245   1.21    dyoung 	    ("%s: reg[ " #reg " / %03x ] = %04x\n",	\
    246    1.1    dyoung 	    dvname, reg, RTW_READ16(regs, reg)))
    247    1.1    dyoung 
    248   1.21    dyoung #define PRINTREG8(sc, reg)				\
    249   1.21    dyoung 	RTW_DPRINTF(RTW_DEBUG_REGDUMP,			\
    250   1.21    dyoung 	    ("%s: reg[ " #reg " / %03x ] = %02x\n",	\
    251    1.1    dyoung 	    dvname, reg, RTW_READ8(regs, reg)))
    252    1.1    dyoung 
    253   1.21    dyoung 	RTW_DPRINTF(RTW_DEBUG_REGDUMP, ("%s: %s\n", dvname, where));
    254    1.1    dyoung 
    255    1.1    dyoung 	PRINTREG32(regs, RTW_IDR0);
    256    1.1    dyoung 	PRINTREG32(regs, RTW_IDR1);
    257    1.1    dyoung 	PRINTREG32(regs, RTW_MAR0);
    258    1.1    dyoung 	PRINTREG32(regs, RTW_MAR1);
    259    1.1    dyoung 	PRINTREG32(regs, RTW_TSFTRL);
    260    1.1    dyoung 	PRINTREG32(regs, RTW_TSFTRH);
    261    1.1    dyoung 	PRINTREG32(regs, RTW_TLPDA);
    262    1.1    dyoung 	PRINTREG32(regs, RTW_TNPDA);
    263    1.1    dyoung 	PRINTREG32(regs, RTW_THPDA);
    264    1.1    dyoung 	PRINTREG32(regs, RTW_TCR);
    265    1.1    dyoung 	PRINTREG32(regs, RTW_RCR);
    266    1.1    dyoung 	PRINTREG32(regs, RTW_TINT);
    267    1.1    dyoung 	PRINTREG32(regs, RTW_TBDA);
    268    1.1    dyoung 	PRINTREG32(regs, RTW_ANAPARM);
    269    1.1    dyoung 	PRINTREG32(regs, RTW_BB);
    270    1.1    dyoung 	PRINTREG32(regs, RTW_PHYCFG);
    271    1.1    dyoung 	PRINTREG32(regs, RTW_WAKEUP0L);
    272    1.1    dyoung 	PRINTREG32(regs, RTW_WAKEUP0H);
    273    1.1    dyoung 	PRINTREG32(regs, RTW_WAKEUP1L);
    274    1.1    dyoung 	PRINTREG32(regs, RTW_WAKEUP1H);
    275    1.1    dyoung 	PRINTREG32(regs, RTW_WAKEUP2LL);
    276    1.1    dyoung 	PRINTREG32(regs, RTW_WAKEUP2LH);
    277    1.1    dyoung 	PRINTREG32(regs, RTW_WAKEUP2HL);
    278    1.1    dyoung 	PRINTREG32(regs, RTW_WAKEUP2HH);
    279    1.1    dyoung 	PRINTREG32(regs, RTW_WAKEUP3LL);
    280    1.1    dyoung 	PRINTREG32(regs, RTW_WAKEUP3LH);
    281    1.1    dyoung 	PRINTREG32(regs, RTW_WAKEUP3HL);
    282    1.1    dyoung 	PRINTREG32(regs, RTW_WAKEUP3HH);
    283    1.1    dyoung 	PRINTREG32(regs, RTW_WAKEUP4LL);
    284    1.1    dyoung 	PRINTREG32(regs, RTW_WAKEUP4LH);
    285    1.1    dyoung 	PRINTREG32(regs, RTW_WAKEUP4HL);
    286    1.1    dyoung 	PRINTREG32(regs, RTW_WAKEUP4HH);
    287    1.1    dyoung 	PRINTREG32(regs, RTW_DK0);
    288    1.1    dyoung 	PRINTREG32(regs, RTW_DK1);
    289    1.1    dyoung 	PRINTREG32(regs, RTW_DK2);
    290    1.1    dyoung 	PRINTREG32(regs, RTW_DK3);
    291    1.1    dyoung 	PRINTREG32(regs, RTW_RETRYCTR);
    292    1.1    dyoung 	PRINTREG32(regs, RTW_RDSAR);
    293    1.1    dyoung 	PRINTREG32(regs, RTW_FER);
    294    1.1    dyoung 	PRINTREG32(regs, RTW_FEMR);
    295    1.1    dyoung 	PRINTREG32(regs, RTW_FPSR);
    296    1.1    dyoung 	PRINTREG32(regs, RTW_FFER);
    297    1.1    dyoung 
    298    1.1    dyoung 	/* 16-bit registers */
    299    1.1    dyoung 	PRINTREG16(regs, RTW_BRSR);
    300    1.1    dyoung 	PRINTREG16(regs, RTW_IMR);
    301    1.1    dyoung 	PRINTREG16(regs, RTW_ISR);
    302    1.1    dyoung 	PRINTREG16(regs, RTW_BCNITV);
    303    1.1    dyoung 	PRINTREG16(regs, RTW_ATIMWND);
    304    1.1    dyoung 	PRINTREG16(regs, RTW_BINTRITV);
    305    1.1    dyoung 	PRINTREG16(regs, RTW_ATIMTRITV);
    306    1.1    dyoung 	PRINTREG16(regs, RTW_CRC16ERR);
    307    1.1    dyoung 	PRINTREG16(regs, RTW_CRC0);
    308    1.1    dyoung 	PRINTREG16(regs, RTW_CRC1);
    309    1.1    dyoung 	PRINTREG16(regs, RTW_CRC2);
    310    1.1    dyoung 	PRINTREG16(regs, RTW_CRC3);
    311    1.1    dyoung 	PRINTREG16(regs, RTW_CRC4);
    312    1.1    dyoung 	PRINTREG16(regs, RTW_CWR);
    313    1.1    dyoung 
    314    1.1    dyoung 	/* 8-bit registers */
    315    1.1    dyoung 	PRINTREG8(regs, RTW_CR);
    316    1.1    dyoung 	PRINTREG8(regs, RTW_9346CR);
    317    1.1    dyoung 	PRINTREG8(regs, RTW_CONFIG0);
    318    1.1    dyoung 	PRINTREG8(regs, RTW_CONFIG1);
    319    1.1    dyoung 	PRINTREG8(regs, RTW_CONFIG2);
    320    1.1    dyoung 	PRINTREG8(regs, RTW_MSR);
    321    1.1    dyoung 	PRINTREG8(regs, RTW_CONFIG3);
    322    1.1    dyoung 	PRINTREG8(regs, RTW_CONFIG4);
    323    1.1    dyoung 	PRINTREG8(regs, RTW_TESTR);
    324    1.1    dyoung 	PRINTREG8(regs, RTW_PSR);
    325    1.1    dyoung 	PRINTREG8(regs, RTW_SCR);
    326    1.1    dyoung 	PRINTREG8(regs, RTW_PHYDELAY);
    327    1.1    dyoung 	PRINTREG8(regs, RTW_CRCOUNT);
    328    1.1    dyoung 	PRINTREG8(regs, RTW_PHYADDR);
    329    1.1    dyoung 	PRINTREG8(regs, RTW_PHYDATAW);
    330    1.1    dyoung 	PRINTREG8(regs, RTW_PHYDATAR);
    331    1.1    dyoung 	PRINTREG8(regs, RTW_CONFIG5);
    332    1.1    dyoung 	PRINTREG8(regs, RTW_TPPOLL);
    333    1.1    dyoung 
    334    1.1    dyoung 	PRINTREG16(regs, RTW_BSSID16);
    335    1.1    dyoung 	PRINTREG32(regs, RTW_BSSID32);
    336    1.1    dyoung #undef PRINTREG32
    337    1.1    dyoung #undef PRINTREG16
    338    1.1    dyoung #undef PRINTREG8
    339    1.1    dyoung }
    340    1.1    dyoung #endif /* RTW_DEBUG */
    341    1.1    dyoung 
    342    1.1    dyoung void
    343    1.3    dyoung rtw_continuous_tx_enable(struct rtw_softc *sc, int enable)
    344    1.1    dyoung {
    345    1.3    dyoung 	struct rtw_regs *regs = &sc->sc_regs;
    346    1.3    dyoung 
    347   1.37    dyoung 	uint32_t tcr;
    348    1.1    dyoung 	tcr = RTW_READ(regs, RTW_TCR);
    349    1.1    dyoung 	tcr &= ~RTW_TCR_LBK_MASK;
    350    1.1    dyoung 	if (enable)
    351    1.1    dyoung 		tcr |= RTW_TCR_LBK_CONT;
    352    1.1    dyoung 	else
    353    1.1    dyoung 		tcr |= RTW_TCR_LBK_NORMAL;
    354    1.1    dyoung 	RTW_WRITE(regs, RTW_TCR, tcr);
    355    1.1    dyoung 	RTW_SYNC(regs, RTW_TCR, RTW_TCR);
    356   1.42    dyoung 	rtw_set_access(regs, RTW_ACCESS_ANAPARM);
    357    1.4    dyoung 	rtw_txdac_enable(sc, !enable);
    358   1.42    dyoung 	rtw_set_access(regs, RTW_ACCESS_ANAPARM);/* XXX Voodoo from Linux. */
    359   1.42    dyoung 	rtw_set_access(regs, RTW_ACCESS_NONE);
    360    1.3    dyoung }
    361    1.3    dyoung 
    362   1.24    dyoung #ifdef RTW_DEBUG
    363    1.3    dyoung static const char *
    364    1.3    dyoung rtw_access_string(enum rtw_access access)
    365    1.3    dyoung {
    366    1.3    dyoung 	switch (access) {
    367    1.3    dyoung 	case RTW_ACCESS_NONE:
    368    1.3    dyoung 		return "none";
    369    1.3    dyoung 	case RTW_ACCESS_CONFIG:
    370    1.3    dyoung 		return "config";
    371    1.3    dyoung 	case RTW_ACCESS_ANAPARM:
    372    1.3    dyoung 		return "anaparm";
    373    1.3    dyoung 	default:
    374    1.3    dyoung 		return "unknown";
    375    1.3    dyoung 	}
    376    1.3    dyoung }
    377   1.24    dyoung #endif /* RTW_DEBUG */
    378    1.3    dyoung 
    379    1.3    dyoung static void
    380   1.42    dyoung rtw_set_access1(struct rtw_regs *regs, enum rtw_access naccess)
    381    1.3    dyoung {
    382   1.76  christos 	KASSERT(/* naccess >= RTW_ACCESS_NONE && */
    383   1.76  christos 	    naccess <= RTW_ACCESS_ANAPARM);
    384   1.76  christos 	KASSERT(/* regs->r_access >= RTW_ACCESS_NONE && */
    385   1.76  christos 	    regs->r_access <= RTW_ACCESS_ANAPARM);
    386    1.3    dyoung 
    387   1.42    dyoung 	if (naccess == regs->r_access)
    388    1.3    dyoung 		return;
    389    1.3    dyoung 
    390    1.3    dyoung 	switch (naccess) {
    391    1.3    dyoung 	case RTW_ACCESS_NONE:
    392   1.42    dyoung 		switch (regs->r_access) {
    393    1.3    dyoung 		case RTW_ACCESS_ANAPARM:
    394    1.3    dyoung 			rtw_anaparm_enable(regs, 0);
    395    1.3    dyoung 			/*FALLTHROUGH*/
    396    1.3    dyoung 		case RTW_ACCESS_CONFIG:
    397    1.3    dyoung 			rtw_config0123_enable(regs, 0);
    398    1.3    dyoung 			/*FALLTHROUGH*/
    399    1.3    dyoung 		case RTW_ACCESS_NONE:
    400    1.3    dyoung 			break;
    401    1.3    dyoung 		}
    402    1.3    dyoung 		break;
    403    1.3    dyoung 	case RTW_ACCESS_CONFIG:
    404   1.42    dyoung 		switch (regs->r_access) {
    405    1.3    dyoung 		case RTW_ACCESS_NONE:
    406    1.3    dyoung 			rtw_config0123_enable(regs, 1);
    407    1.3    dyoung 			/*FALLTHROUGH*/
    408    1.3    dyoung 		case RTW_ACCESS_CONFIG:
    409    1.3    dyoung 			break;
    410    1.3    dyoung 		case RTW_ACCESS_ANAPARM:
    411    1.3    dyoung 			rtw_anaparm_enable(regs, 0);
    412    1.3    dyoung 			break;
    413    1.3    dyoung 		}
    414    1.3    dyoung 		break;
    415    1.3    dyoung 	case RTW_ACCESS_ANAPARM:
    416   1.42    dyoung 		switch (regs->r_access) {
    417    1.3    dyoung 		case RTW_ACCESS_NONE:
    418    1.3    dyoung 			rtw_config0123_enable(regs, 1);
    419    1.3    dyoung 			/*FALLTHROUGH*/
    420    1.3    dyoung 		case RTW_ACCESS_CONFIG:
    421    1.3    dyoung 			rtw_anaparm_enable(regs, 1);
    422    1.3    dyoung 			/*FALLTHROUGH*/
    423    1.3    dyoung 		case RTW_ACCESS_ANAPARM:
    424    1.3    dyoung 			break;
    425    1.3    dyoung 		}
    426    1.3    dyoung 		break;
    427    1.1    dyoung 	}
    428    1.1    dyoung }
    429    1.1    dyoung 
    430    1.3    dyoung void
    431   1.42    dyoung rtw_set_access(struct rtw_regs *regs, enum rtw_access access)
    432    1.3    dyoung {
    433   1.42    dyoung 	rtw_set_access1(regs, access);
    434   1.21    dyoung 	RTW_DPRINTF(RTW_DEBUG_ACCESS,
    435   1.42    dyoung 	    ("%s: access %s -> %s\n", __func__,
    436   1.42    dyoung 	    rtw_access_string(regs->r_access),
    437    1.3    dyoung 	    rtw_access_string(access)));
    438   1.42    dyoung 	regs->r_access = access;
    439    1.3    dyoung }
    440    1.3    dyoung 
    441    1.1    dyoung /*
    442    1.1    dyoung  * Enable registers, switch register banks.
    443    1.1    dyoung  */
    444    1.1    dyoung void
    445    1.1    dyoung rtw_config0123_enable(struct rtw_regs *regs, int enable)
    446    1.1    dyoung {
    447   1.37    dyoung 	uint8_t ecr;
    448    1.1    dyoung 	ecr = RTW_READ8(regs, RTW_9346CR);
    449    1.1    dyoung 	ecr &= ~(RTW_9346CR_EEM_MASK | RTW_9346CR_EECS | RTW_9346CR_EESK);
    450    1.1    dyoung 	if (enable)
    451    1.1    dyoung 		ecr |= RTW_9346CR_EEM_CONFIG;
    452    1.8    dyoung 	else {
    453    1.8    dyoung 		RTW_WBW(regs, RTW_9346CR, MAX(RTW_CONFIG0, RTW_CONFIG3));
    454    1.1    dyoung 		ecr |= RTW_9346CR_EEM_NORMAL;
    455    1.8    dyoung 	}
    456    1.1    dyoung 	RTW_WRITE8(regs, RTW_9346CR, ecr);
    457    1.1    dyoung 	RTW_SYNC(regs, RTW_9346CR, RTW_9346CR);
    458    1.1    dyoung }
    459    1.1    dyoung 
    460    1.1    dyoung /* requires rtw_config0123_enable(, 1) */
    461    1.1    dyoung void
    462    1.1    dyoung rtw_anaparm_enable(struct rtw_regs *regs, int enable)
    463    1.1    dyoung {
    464   1.37    dyoung 	uint8_t cfg3;
    465    1.1    dyoung 
    466    1.1    dyoung 	cfg3 = RTW_READ8(regs, RTW_CONFIG3);
    467    1.3    dyoung 	cfg3 |= RTW_CONFIG3_CLKRUNEN;
    468    1.3    dyoung 	if (enable)
    469    1.3    dyoung 		cfg3 |= RTW_CONFIG3_PARMEN;
    470    1.3    dyoung 	else
    471    1.1    dyoung 		cfg3 &= ~RTW_CONFIG3_PARMEN;
    472    1.1    dyoung 	RTW_WRITE8(regs, RTW_CONFIG3, cfg3);
    473    1.1    dyoung 	RTW_SYNC(regs, RTW_CONFIG3, RTW_CONFIG3);
    474    1.1    dyoung }
    475    1.1    dyoung 
    476    1.1    dyoung /* requires rtw_anaparm_enable(, 1) */
    477    1.1    dyoung void
    478    1.4    dyoung rtw_txdac_enable(struct rtw_softc *sc, int enable)
    479    1.1    dyoung {
    480   1.37    dyoung 	uint32_t anaparm;
    481    1.4    dyoung 	struct rtw_regs *regs = &sc->sc_regs;
    482    1.1    dyoung 
    483    1.1    dyoung 	anaparm = RTW_READ(regs, RTW_ANAPARM);
    484    1.1    dyoung 	if (enable)
    485    1.1    dyoung 		anaparm &= ~RTW_ANAPARM_TXDACOFF;
    486    1.1    dyoung 	else
    487    1.1    dyoung 		anaparm |= RTW_ANAPARM_TXDACOFF;
    488    1.1    dyoung 	RTW_WRITE(regs, RTW_ANAPARM, anaparm);
    489    1.1    dyoung 	RTW_SYNC(regs, RTW_ANAPARM, RTW_ANAPARM);
    490    1.1    dyoung }
    491    1.1    dyoung 
    492   1.61     perry static inline int
    493   1.98    dyoung rtw_chip_reset1(struct rtw_regs *regs, device_t dev)
    494    1.1    dyoung {
    495   1.37    dyoung 	uint8_t cr;
    496    1.1    dyoung 	int i;
    497    1.1    dyoung 
    498    1.1    dyoung 	RTW_WRITE8(regs, RTW_CR, RTW_CR_RST);
    499    1.1    dyoung 
    500    1.1    dyoung 	RTW_WBR(regs, RTW_CR, RTW_CR);
    501    1.1    dyoung 
    502   1.21    dyoung 	for (i = 0; i < 1000; i++) {
    503    1.1    dyoung 		if ((cr = RTW_READ8(regs, RTW_CR) & RTW_CR_RST) == 0) {
    504   1.21    dyoung 			RTW_DPRINTF(RTW_DEBUG_RESET,
    505   1.98    dyoung 			    ("%s: reset in %dus\n", device_xname(dev), i));
    506    1.1    dyoung 			return 0;
    507    1.1    dyoung 		}
    508    1.1    dyoung 		RTW_RBR(regs, RTW_CR, RTW_CR);
    509   1.21    dyoung 		DELAY(10); /* 10us */
    510    1.1    dyoung 	}
    511    1.1    dyoung 
    512   1.98    dyoung 	aprint_error_dev(dev, "reset failed\n");
    513    1.1    dyoung 	return ETIMEDOUT;
    514    1.1    dyoung }
    515    1.1    dyoung 
    516   1.61     perry static inline int
    517   1.98    dyoung rtw_chip_reset(struct rtw_regs *regs, device_t dev)
    518    1.3    dyoung {
    519    1.3    dyoung 	uint32_t tcr;
    520    1.3    dyoung 
    521    1.3    dyoung 	/* from Linux driver */
    522    1.3    dyoung 	tcr = RTW_TCR_CWMIN | RTW_TCR_MXDMA_2048 |
    523   1.75    dyoung 	      __SHIFTIN(7, RTW_TCR_SRL_MASK) | __SHIFTIN(7, RTW_TCR_LRL_MASK);
    524    1.3    dyoung 
    525    1.3    dyoung 	RTW_WRITE(regs, RTW_TCR, tcr);
    526    1.3    dyoung 
    527    1.3    dyoung 	RTW_WBW(regs, RTW_CR, RTW_TCR);
    528    1.3    dyoung 
    529   1.98    dyoung 	return rtw_chip_reset1(regs, dev);
    530    1.3    dyoung }
    531    1.3    dyoung 
    532   1.49    dyoung static int
    533   1.58    dyoung rtw_wep_decap(struct ieee80211_key *k, struct mbuf *m, int hdrlen)
    534   1.50    dyoung {
    535   1.50    dyoung 	struct ieee80211_key keycopy;
    536   1.50    dyoung 
    537   1.50    dyoung 	RTW_DPRINTF(RTW_DEBUG_KEY, ("%s:\n", __func__));
    538   1.50    dyoung 
    539   1.50    dyoung 	keycopy = *k;
    540   1.50    dyoung 	keycopy.wk_flags &= ~IEEE80211_KEY_SWCRYPT;
    541   1.50    dyoung 
    542   1.58    dyoung 	return (*ieee80211_cipher_wep.ic_decap)(&keycopy, m, hdrlen);
    543   1.50    dyoung }
    544   1.50    dyoung 
    545   1.50    dyoung static int
    546   1.49    dyoung rtw_key_delete(struct ieee80211com *ic, const struct ieee80211_key *k)
    547   1.49    dyoung {
    548   1.49    dyoung 	struct rtw_softc *sc = ic->ic_ifp->if_softc;
    549   1.49    dyoung 
    550  1.102    dyoung 	DPRINTF(sc, RTW_DEBUG_KEY, ("%s: delete key %u\n", __func__,
    551  1.102    dyoung 	    k->wk_keyix));
    552   1.49    dyoung 
    553  1.102    dyoung 	KASSERT(k->wk_keyix < IEEE80211_WEP_NKID);
    554  1.102    dyoung 
    555  1.102    dyoung 	if (k->wk_keylen != 0 &&
    556  1.102    dyoung 	    k->wk_cipher->ic_cipher == IEEE80211_CIPHER_WEP)
    557   1.49    dyoung 		sc->sc_flags &= ~RTW_F_DK_VALID;
    558   1.49    dyoung 
    559   1.49    dyoung 	return 1;
    560   1.49    dyoung }
    561   1.49    dyoung 
    562   1.49    dyoung static int
    563   1.49    dyoung rtw_key_set(struct ieee80211com *ic, const struct ieee80211_key *k,
    564   1.81  christos     const u_int8_t mac[IEEE80211_ADDR_LEN])
    565   1.49    dyoung {
    566   1.49    dyoung 	struct rtw_softc *sc = ic->ic_ifp->if_softc;
    567   1.49    dyoung 
    568   1.49    dyoung 	DPRINTF(sc, RTW_DEBUG_KEY, ("%s: set key %u\n", __func__, k->wk_keyix));
    569   1.49    dyoung 
    570  1.102    dyoung 	KASSERT(k->wk_keyix < IEEE80211_WEP_NKID);
    571   1.49    dyoung 
    572   1.49    dyoung 	sc->sc_flags &= ~RTW_F_DK_VALID;
    573   1.49    dyoung 
    574   1.49    dyoung 	return 1;
    575   1.49    dyoung }
    576   1.49    dyoung 
    577   1.49    dyoung static void
    578   1.81  christos rtw_key_update_begin(struct ieee80211com *ic)
    579   1.49    dyoung {
    580   1.55    dogcow #ifdef RTW_DEBUG
    581   1.49    dyoung 	struct ifnet *ifp = ic->ic_ifp;
    582   1.49    dyoung 	struct rtw_softc *sc = ifp->if_softc;
    583   1.49    dyoung #endif
    584   1.49    dyoung 
    585   1.49    dyoung 	DPRINTF(sc, RTW_DEBUG_KEY, ("%s:\n", __func__));
    586   1.49    dyoung }
    587   1.49    dyoung 
    588   1.49    dyoung static void
    589  1.103    dyoung rtw_tx_kick(struct rtw_regs *regs, uint8_t ringsel)
    590  1.103    dyoung {
    591  1.103    dyoung 	uint8_t tppoll;
    592  1.103    dyoung 
    593  1.103    dyoung 	tppoll = RTW_READ8(regs, RTW_TPPOLL);
    594  1.103    dyoung 	tppoll &= ~RTW_TPPOLL_SALL;
    595  1.103    dyoung 	tppoll |= ringsel & RTW_TPPOLL_ALL;
    596  1.103    dyoung 	RTW_WRITE8(regs, RTW_TPPOLL, tppoll);
    597  1.103    dyoung 	RTW_SYNC(regs, RTW_TPPOLL, RTW_TPPOLL);
    598  1.103    dyoung }
    599  1.103    dyoung 
    600  1.103    dyoung static void
    601   1.49    dyoung rtw_key_update_end(struct ieee80211com *ic)
    602   1.49    dyoung {
    603   1.49    dyoung 	struct ifnet *ifp = ic->ic_ifp;
    604   1.49    dyoung 	struct rtw_softc *sc = ifp->if_softc;
    605   1.49    dyoung 
    606   1.49    dyoung 	DPRINTF(sc, RTW_DEBUG_KEY, ("%s:\n", __func__));
    607   1.49    dyoung 
    608   1.58    dyoung 	if ((sc->sc_flags & RTW_F_DK_VALID) != 0 ||
    609  1.101    dyoung 	    !device_is_active(sc->sc_dev))
    610   1.49    dyoung 		return;
    611   1.49    dyoung 
    612   1.83    dyoung 	rtw_io_enable(sc, RTW_CR_RE | RTW_CR_TE, 0);
    613   1.49    dyoung 	rtw_wep_setkeys(sc, ic->ic_nw_keys, ic->ic_def_txkey);
    614   1.83    dyoung 	rtw_io_enable(sc, RTW_CR_RE | RTW_CR_TE,
    615   1.49    dyoung 	    (ifp->if_flags & IFF_RUNNING) != 0);
    616   1.49    dyoung }
    617   1.49    dyoung 
    618  1.104    dyoung static bool
    619   1.58    dyoung rtw_key_hwsupp(uint32_t flags, const struct ieee80211_key *k)
    620   1.58    dyoung {
    621   1.58    dyoung 	if (k->wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP)
    622  1.104    dyoung 		return false;
    623   1.58    dyoung 
    624   1.58    dyoung 	return	((flags & RTW_C_RXWEP_40) != 0 && k->wk_keylen == 5) ||
    625   1.58    dyoung 		((flags & RTW_C_RXWEP_104) != 0 && k->wk_keylen == 13);
    626   1.58    dyoung }
    627   1.58    dyoung 
    628   1.42    dyoung static void
    629   1.48    dyoung rtw_wep_setkeys(struct rtw_softc *sc, struct ieee80211_key *wk, int txkey)
    630   1.42    dyoung {
    631   1.58    dyoung 	uint8_t psr, scr;
    632  1.104    dyoung 	int i, keylen = 0;
    633   1.42    dyoung 	struct rtw_regs *regs;
    634   1.42    dyoung 	union rtw_keys *rk;
    635   1.42    dyoung 
    636   1.42    dyoung 	regs = &sc->sc_regs;
    637   1.42    dyoung 	rk = &sc->sc_keys;
    638   1.42    dyoung 
    639  1.119     joerg 	(void)memset(rk, 0, sizeof(*rk));
    640   1.42    dyoung 
    641   1.58    dyoung 	/* Temporarily use software crypto for all keys. */
    642   1.58    dyoung 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    643   1.58    dyoung 		if (wk[i].wk_cipher == &rtw_cipher_wep)
    644   1.58    dyoung 			wk[i].wk_cipher = &ieee80211_cipher_wep;
    645   1.58    dyoung 	}
    646   1.58    dyoung 
    647   1.50    dyoung 	rtw_set_access(regs, RTW_ACCESS_CONFIG);
    648   1.50    dyoung 
    649   1.50    dyoung 	psr = RTW_READ8(regs, RTW_PSR);
    650   1.42    dyoung 	scr = RTW_READ8(regs, RTW_SCR);
    651   1.42    dyoung 	scr &= ~(RTW_SCR_KM_MASK | RTW_SCR_TXSECON | RTW_SCR_RXSECON);
    652   1.42    dyoung 
    653   1.42    dyoung 	if ((sc->sc_ic.ic_flags & IEEE80211_F_PRIVACY) == 0)
    654   1.42    dyoung 		goto out;
    655   1.42    dyoung 
    656  1.104    dyoung 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    657   1.66    dyoung 		if (!rtw_key_hwsupp(sc->sc_flags, &wk[i]))
    658   1.66    dyoung 			continue;
    659   1.66    dyoung 		if (i == txkey) {
    660   1.66    dyoung 			keylen = wk[i].wk_keylen;
    661   1.66    dyoung 			break;
    662   1.66    dyoung 		}
    663   1.66    dyoung 		keylen = MAX(keylen, wk[i].wk_keylen);
    664   1.42    dyoung 	}
    665   1.42    dyoung 
    666   1.66    dyoung 	if (keylen == 5)
    667   1.66    dyoung 		scr |= RTW_SCR_KM_WEP40 | RTW_SCR_RXSECON;
    668   1.66    dyoung 	else if (keylen == 13)
    669   1.66    dyoung 		scr |= RTW_SCR_KM_WEP104 | RTW_SCR_RXSECON;
    670   1.42    dyoung 
    671   1.49    dyoung 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    672   1.66    dyoung 		if (wk[i].wk_keylen != keylen ||
    673   1.58    dyoung 		    wk[i].wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP)
    674   1.42    dyoung 			continue;
    675   1.58    dyoung 		/* h/w will decrypt, s/w still strips headers */
    676   1.58    dyoung 		wk[i].wk_cipher = &rtw_cipher_wep;
    677   1.49    dyoung 		(void)memcpy(rk->rk_keys[i], wk[i].wk_key, wk[i].wk_keylen);
    678   1.42    dyoung 	}
    679   1.42    dyoung 
    680   1.42    dyoung out:
    681   1.50    dyoung 	RTW_WRITE8(regs, RTW_PSR, psr & ~RTW_PSR_PSEN);
    682   1.50    dyoung 
    683   1.88    dyoung 	bus_space_write_region_stream_4(regs->r_bt, regs->r_bh,
    684   1.87    dyoung 	    RTW_DK0, rk->rk_words, __arraycount(rk->rk_words));
    685   1.42    dyoung 
    686   1.50    dyoung 	bus_space_barrier(regs->r_bt, regs->r_bh, RTW_DK0, sizeof(rk->rk_words),
    687  1.124  jdolecek 	    BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE);
    688   1.42    dyoung 
    689  1.104    dyoung 	RTW_DPRINTF(RTW_DEBUG_KEY,
    690  1.104    dyoung 	    ("%s.%d: scr %02" PRIx8 ", keylen %d\n", __func__, __LINE__, scr,
    691  1.104    dyoung 	     keylen));
    692  1.104    dyoung 
    693   1.58    dyoung 	RTW_WBW(regs, RTW_DK0, RTW_PSR);
    694   1.50    dyoung 	RTW_WRITE8(regs, RTW_PSR, psr);
    695   1.58    dyoung 	RTW_WBW(regs, RTW_PSR, RTW_SCR);
    696   1.42    dyoung 	RTW_WRITE8(regs, RTW_SCR, scr);
    697   1.42    dyoung 	RTW_SYNC(regs, RTW_SCR, RTW_SCR);
    698   1.42    dyoung 	rtw_set_access(regs, RTW_ACCESS_NONE);
    699   1.49    dyoung 	sc->sc_flags |= RTW_F_DK_VALID;
    700   1.42    dyoung }
    701   1.42    dyoung 
    702   1.61     perry static inline int
    703   1.98    dyoung rtw_recall_eeprom(struct rtw_regs *regs, device_t dev)
    704    1.1    dyoung {
    705    1.1    dyoung 	int i;
    706   1.37    dyoung 	uint8_t ecr;
    707    1.1    dyoung 
    708    1.1    dyoung 	ecr = RTW_READ8(regs, RTW_9346CR);
    709    1.1    dyoung 	ecr = (ecr & ~RTW_9346CR_EEM_MASK) | RTW_9346CR_EEM_AUTOLOAD;
    710    1.1    dyoung 	RTW_WRITE8(regs, RTW_9346CR, ecr);
    711    1.1    dyoung 
    712    1.1    dyoung 	RTW_WBR(regs, RTW_9346CR, RTW_9346CR);
    713    1.1    dyoung 
    714   1.55    dogcow 	/* wait 25ms for completion */
    715   1.55    dogcow 	for (i = 0; i < 250; i++) {
    716    1.1    dyoung 		ecr = RTW_READ8(regs, RTW_9346CR);
    717    1.1    dyoung 		if ((ecr & RTW_9346CR_EEM_MASK) == RTW_9346CR_EEM_NORMAL) {
    718   1.21    dyoung 			RTW_DPRINTF(RTW_DEBUG_RESET,
    719   1.98    dyoung 			    ("%s: recall EEPROM in %dus\n", device_xname(dev),
    720   1.98    dyoung 			    i * 100));
    721    1.1    dyoung 			return 0;
    722    1.1    dyoung 		}
    723    1.1    dyoung 		RTW_RBR(regs, RTW_9346CR, RTW_9346CR);
    724    1.1    dyoung 		DELAY(100);
    725    1.1    dyoung 	}
    726   1.98    dyoung 	aprint_error_dev(dev, "recall EEPROM failed\n");
    727    1.1    dyoung 	return ETIMEDOUT;
    728    1.1    dyoung }
    729    1.1    dyoung 
    730   1.61     perry static inline int
    731    1.1    dyoung rtw_reset(struct rtw_softc *sc)
    732    1.1    dyoung {
    733    1.1    dyoung 	int rc;
    734    1.4    dyoung 	uint8_t config1;
    735    1.1    dyoung 
    736   1.49    dyoung 	sc->sc_flags &= ~RTW_F_DK_VALID;
    737   1.49    dyoung 
    738   1.98    dyoung 	if ((rc = rtw_chip_reset(&sc->sc_regs, sc->sc_dev)) != 0)
    739    1.1    dyoung 		return rc;
    740    1.1    dyoung 
    741   1.98    dyoung 	rc = rtw_recall_eeprom(&sc->sc_regs, sc->sc_dev);
    742    1.1    dyoung 
    743    1.4    dyoung 	config1 = RTW_READ8(&sc->sc_regs, RTW_CONFIG1);
    744    1.4    dyoung 	RTW_WRITE8(&sc->sc_regs, RTW_CONFIG1, config1 & ~RTW_CONFIG1_PMEN);
    745    1.1    dyoung 	/* TBD turn off maximum power saving? */
    746    1.1    dyoung 
    747    1.1    dyoung 	return 0;
    748    1.1    dyoung }
    749    1.1    dyoung 
    750   1.61     perry static inline int
    751   1.34    dyoung rtw_txdesc_dmamaps_create(bus_dma_tag_t dmat, struct rtw_txsoft *descs,
    752    1.1    dyoung     u_int ndescs)
    753    1.1    dyoung {
    754    1.1    dyoung 	int i, rc = 0;
    755    1.1    dyoung 	for (i = 0; i < ndescs; i++) {
    756    1.1    dyoung 		rc = bus_dmamap_create(dmat, MCLBYTES, RTW_MAXPKTSEGS, MCLBYTES,
    757   1.34    dyoung 		    0, 0, &descs[i].ts_dmamap);
    758    1.1    dyoung 		if (rc != 0)
    759    1.1    dyoung 			break;
    760    1.1    dyoung 	}
    761    1.1    dyoung 	return rc;
    762    1.1    dyoung }
    763    1.1    dyoung 
    764   1.61     perry static inline int
    765   1.34    dyoung rtw_rxdesc_dmamaps_create(bus_dma_tag_t dmat, struct rtw_rxsoft *descs,
    766    1.1    dyoung     u_int ndescs)
    767    1.1    dyoung {
    768    1.1    dyoung 	int i, rc = 0;
    769    1.1    dyoung 	for (i = 0; i < ndescs; i++) {
    770    1.1    dyoung 		rc = bus_dmamap_create(dmat, MCLBYTES, 1, MCLBYTES, 0, 0,
    771   1.34    dyoung 		    &descs[i].rs_dmamap);
    772    1.1    dyoung 		if (rc != 0)
    773    1.1    dyoung 			break;
    774    1.1    dyoung 	}
    775    1.1    dyoung 	return rc;
    776    1.1    dyoung }
    777    1.1    dyoung 
    778   1.61     perry static inline void
    779   1.34    dyoung rtw_rxdesc_dmamaps_destroy(bus_dma_tag_t dmat, struct rtw_rxsoft *descs,
    780    1.1    dyoung     u_int ndescs)
    781    1.1    dyoung {
    782    1.1    dyoung 	int i;
    783    1.1    dyoung 	for (i = 0; i < ndescs; i++) {
    784   1.34    dyoung 		if (descs[i].rs_dmamap != NULL)
    785   1.34    dyoung 			bus_dmamap_destroy(dmat, descs[i].rs_dmamap);
    786    1.1    dyoung 	}
    787    1.1    dyoung }
    788    1.1    dyoung 
    789   1.61     perry static inline void
    790   1.34    dyoung rtw_txdesc_dmamaps_destroy(bus_dma_tag_t dmat, struct rtw_txsoft *descs,
    791    1.1    dyoung     u_int ndescs)
    792    1.1    dyoung {
    793    1.1    dyoung 	int i;
    794    1.1    dyoung 	for (i = 0; i < ndescs; i++) {
    795   1.34    dyoung 		if (descs[i].ts_dmamap != NULL)
    796   1.34    dyoung 			bus_dmamap_destroy(dmat, descs[i].ts_dmamap);
    797    1.1    dyoung 	}
    798    1.1    dyoung }
    799    1.1    dyoung 
    800   1.61     perry static inline void
    801    1.1    dyoung rtw_srom_free(struct rtw_srom *sr)
    802    1.1    dyoung {
    803    1.1    dyoung 	sr->sr_size = 0;
    804    1.1    dyoung 	if (sr->sr_content == NULL)
    805    1.1    dyoung 		return;
    806    1.1    dyoung 	free(sr->sr_content, M_DEVBUF);
    807    1.1    dyoung 	sr->sr_content = NULL;
    808    1.1    dyoung }
    809    1.1    dyoung 
    810    1.1    dyoung static void
    811   1.81  christos rtw_srom_defaults(struct rtw_srom *sr, uint32_t *flags,
    812   1.78  christos     uint8_t *cs_threshold, enum rtw_rfchipid *rfchipid, uint32_t *rcr)
    813    1.1    dyoung {
    814    1.1    dyoung 	*flags |= (RTW_F_DIGPHY|RTW_F_ANTDIV);
    815    1.1    dyoung 	*cs_threshold = RTW_SR_ENERGYDETTHR_DEFAULT;
    816    1.1    dyoung 	*rcr |= RTW_RCR_ENCS1;
    817    1.1    dyoung 	*rfchipid = RTW_RFCHIPID_PHILIPS;
    818    1.1    dyoung }
    819    1.1    dyoung 
    820    1.1    dyoung static int
    821   1.37    dyoung rtw_srom_parse(struct rtw_srom *sr, uint32_t *flags, uint8_t *cs_threshold,
    822   1.37    dyoung     enum rtw_rfchipid *rfchipid, uint32_t *rcr, enum rtw_locale *locale,
    823   1.98    dyoung     device_t dev)
    824    1.1    dyoung {
    825    1.1    dyoung 	int i;
    826    1.1    dyoung 	const char *rfname, *paname;
    827    1.1    dyoung 	char scratch[sizeof("unknown 0xXX")];
    828   1.46    dyoung 	uint16_t srom_version;
    829    1.1    dyoung 
    830    1.1    dyoung 	*flags &= ~(RTW_F_DIGPHY|RTW_F_DFLANTB|RTW_F_ANTDIV);
    831    1.1    dyoung 	*rcr &= ~(RTW_RCR_ENCS1 | RTW_RCR_ENCS2);
    832    1.1    dyoung 
    833   1.46    dyoung 	srom_version = RTW_SR_GET16(sr, RTW_SR_VERSION);
    834    1.1    dyoung 
    835   1.46    dyoung 	if (srom_version <= 0x0101) {
    836   1.98    dyoung 		aprint_error_dev(dev,
    837   1.98    dyoung 		    "SROM version %d.%d is not understood, "
    838   1.98    dyoung 		    "limping along with defaults\n",
    839   1.98    dyoung 		    srom_version >> 8, srom_version & 0xff);
    840    1.7    dyoung 		rtw_srom_defaults(sr, flags, cs_threshold, rfchipid, rcr);
    841    1.1    dyoung 		return 0;
    842   1.98    dyoung 	} else {
    843  1.113    dyoung 		aprint_verbose_dev(dev, "SROM version %d.%d\n",
    844   1.98    dyoung 		    srom_version >> 8, srom_version & 0xff);
    845    1.1    dyoung 	}
    846    1.1    dyoung 
    847  1.120  christos 	uint8_t mac[IEEE80211_ADDR_LEN];
    848    1.1    dyoung 	for (i = 0; i < IEEE80211_ADDR_LEN; i++)
    849    1.1    dyoung 		mac[i] = RTW_SR_GET(sr, RTW_SR_MAC + i);
    850  1.120  christos 	__USE(mac);
    851    1.1    dyoung 
    852   1.21    dyoung 	RTW_DPRINTF(RTW_DEBUG_ATTACH,
    853   1.98    dyoung 	    ("%s: EEPROM MAC %s\n", device_xname(dev), ether_sprintf(mac)));
    854    1.1    dyoung 
    855    1.1    dyoung 	*cs_threshold = RTW_SR_GET(sr, RTW_SR_ENERGYDETTHR);
    856    1.1    dyoung 
    857    1.1    dyoung 	if ((RTW_SR_GET(sr, RTW_SR_CONFIG2) & RTW_CONFIG2_ANT) != 0)
    858    1.1    dyoung 		*flags |= RTW_F_ANTDIV;
    859    1.1    dyoung 
    860   1.10    dyoung 	/* Note well: the sense of the RTW_SR_RFPARM_DIGPHY bit seems
    861   1.10    dyoung 	 * to be reversed.
    862   1.10    dyoung 	 */
    863   1.10    dyoung 	if ((RTW_SR_GET(sr, RTW_SR_RFPARM) & RTW_SR_RFPARM_DIGPHY) == 0)
    864    1.1    dyoung 		*flags |= RTW_F_DIGPHY;
    865    1.1    dyoung 	if ((RTW_SR_GET(sr, RTW_SR_RFPARM) & RTW_SR_RFPARM_DFLANTB) != 0)
    866    1.1    dyoung 		*flags |= RTW_F_DFLANTB;
    867    1.1    dyoung 
    868   1.75    dyoung 	*rcr |= __SHIFTIN(__SHIFTOUT(RTW_SR_GET(sr, RTW_SR_RFPARM),
    869    1.1    dyoung 	    RTW_SR_RFPARM_CS_MASK), RTW_RCR_ENCS1);
    870    1.1    dyoung 
    871   1.58    dyoung 	if ((RTW_SR_GET(sr, RTW_SR_CONFIG0) & RTW_CONFIG0_WEP104) != 0)
    872   1.58    dyoung 		*flags |= RTW_C_RXWEP_104;
    873   1.58    dyoung 
    874   1.58    dyoung 	*flags |= RTW_C_RXWEP_40;	/* XXX */
    875   1.58    dyoung 
    876    1.1    dyoung 	*rfchipid = RTW_SR_GET(sr, RTW_SR_RFCHIPID);
    877    1.1    dyoung 	switch (*rfchipid) {
    878    1.1    dyoung 	case RTW_RFCHIPID_GCT:		/* this combo seen in the wild */
    879    1.1    dyoung 		rfname = "GCT GRF5101";
    880    1.1    dyoung 		paname = "Winspring WS9901";
    881    1.1    dyoung 		break;
    882    1.1    dyoung 	case RTW_RFCHIPID_MAXIM:
    883    1.1    dyoung 		rfname = "Maxim MAX2820";	/* guess */
    884    1.1    dyoung 		paname = "Maxim MAX2422";	/* guess */
    885    1.1    dyoung 		break;
    886    1.1    dyoung 	case RTW_RFCHIPID_INTERSIL:
    887    1.1    dyoung 		rfname = "Intersil HFA3873";	/* guess */
    888    1.1    dyoung 		paname = "Intersil <unknown>";
    889    1.1    dyoung 		break;
    890    1.1    dyoung 	case RTW_RFCHIPID_PHILIPS:	/* this combo seen in the wild */
    891    1.1    dyoung 		rfname = "Philips SA2400A";
    892    1.1    dyoung 		paname = "Philips SA2411";
    893    1.1    dyoung 		break;
    894    1.1    dyoung 	case RTW_RFCHIPID_RFMD:
    895    1.1    dyoung 		/* this is the same front-end as an atw(4)! */
    896    1.1    dyoung 		rfname = "RFMD RF2948B, "	/* mentioned in Realtek docs */
    897    1.1    dyoung 			 "LNA: RFMD RF2494, "	/* mentioned in Realtek docs */
    898    1.1    dyoung 			 "SYN: Silicon Labs Si4126";	/* inferred from
    899    1.1    dyoung 			 				 * reference driver
    900    1.1    dyoung 							 */
    901    1.1    dyoung 		paname = "RFMD RF2189";		/* mentioned in Realtek docs */
    902    1.1    dyoung 		break;
    903    1.1    dyoung 	case RTW_RFCHIPID_RESERVED:
    904    1.1    dyoung 		rfname = paname = "reserved";
    905    1.1    dyoung 		break;
    906    1.1    dyoung 	default:
    907    1.1    dyoung 		snprintf(scratch, sizeof(scratch), "unknown 0x%02x", *rfchipid);
    908    1.1    dyoung 		rfname = paname = scratch;
    909    1.1    dyoung 	}
    910   1.98    dyoung 	aprint_normal_dev(dev, "RF: %s, PA: %s\n", rfname, paname);
    911    1.1    dyoung 
    912    1.1    dyoung 	switch (RTW_SR_GET(sr, RTW_SR_CONFIG0) & RTW_CONFIG0_GL_MASK) {
    913    1.1    dyoung 	case RTW_CONFIG0_GL_USA:
    914   1.55    dogcow 	case _RTW_CONFIG0_GL_USA:
    915    1.1    dyoung 		*locale = RTW_LOCALE_USA;
    916    1.1    dyoung 		break;
    917    1.1    dyoung 	case RTW_CONFIG0_GL_EUROPE:
    918    1.1    dyoung 		*locale = RTW_LOCALE_EUROPE;
    919    1.1    dyoung 		break;
    920    1.1    dyoung 	case RTW_CONFIG0_GL_JAPAN:
    921    1.1    dyoung 		*locale = RTW_LOCALE_JAPAN;
    922    1.1    dyoung 		break;
    923    1.1    dyoung 	default:
    924    1.1    dyoung 		*locale = RTW_LOCALE_UNKNOWN;
    925    1.1    dyoung 		break;
    926    1.1    dyoung 	}
    927    1.1    dyoung 	return 0;
    928    1.1    dyoung }
    929    1.1    dyoung 
    930    1.1    dyoung /* Returns -1 on failure. */
    931    1.1    dyoung static int
    932   1.37    dyoung rtw_srom_read(struct rtw_regs *regs, uint32_t flags, struct rtw_srom *sr,
    933   1.98    dyoung     device_t dev)
    934    1.1    dyoung {
    935    1.1    dyoung 	int rc;
    936    1.1    dyoung 	struct seeprom_descriptor sd;
    937   1.37    dyoung 	uint8_t ecr;
    938    1.1    dyoung 
    939    1.1    dyoung 	(void)memset(&sd, 0, sizeof(sd));
    940    1.1    dyoung 
    941    1.1    dyoung 	ecr = RTW_READ8(regs, RTW_9346CR);
    942    1.1    dyoung 
    943    1.1    dyoung 	if ((flags & RTW_F_9356SROM) != 0) {
    944   1.98    dyoung 		RTW_DPRINTF(RTW_DEBUG_ATTACH, ("%s: 93c56 SROM\n",
    945   1.98    dyoung 		    device_xname(dev)));
    946    1.1    dyoung 		sr->sr_size = 256;
    947    1.1    dyoung 		sd.sd_chip = C56_66;
    948    1.1    dyoung 	} else {
    949   1.98    dyoung 		RTW_DPRINTF(RTW_DEBUG_ATTACH, ("%s: 93c46 SROM\n",
    950   1.98    dyoung 		    device_xname(dev)));
    951    1.1    dyoung 		sr->sr_size = 128;
    952    1.1    dyoung 		sd.sd_chip = C46;
    953    1.1    dyoung 	}
    954    1.1    dyoung 
    955    1.1    dyoung 	ecr &= ~(RTW_9346CR_EEDI | RTW_9346CR_EEDO | RTW_9346CR_EESK |
    956   1.41    dyoung 	    RTW_9346CR_EEM_MASK | RTW_9346CR_EECS);
    957    1.1    dyoung 	ecr |= RTW_9346CR_EEM_PROGRAM;
    958    1.1    dyoung 
    959    1.1    dyoung 	RTW_WRITE8(regs, RTW_9346CR, ecr);
    960    1.1    dyoung 
    961    1.1    dyoung 	sr->sr_content = malloc(sr->sr_size, M_DEVBUF, M_NOWAIT);
    962    1.1    dyoung 
    963    1.1    dyoung 	if (sr->sr_content == NULL) {
    964   1.98    dyoung 		aprint_error_dev(dev, "unable to allocate SROM buffer\n");
    965    1.1    dyoung 		return ENOMEM;
    966    1.1    dyoung 	}
    967    1.1    dyoung 
    968    1.1    dyoung 	(void)memset(sr->sr_content, 0, sr->sr_size);
    969    1.1    dyoung 
    970    1.1    dyoung 	/* RTL8180 has a single 8-bit register for controlling the
    971    1.1    dyoung 	 * 93cx6 SROM.  There is no "ready" bit. The RTL8180
    972    1.1    dyoung 	 * input/output sense is the reverse of read_seeprom's.
    973    1.1    dyoung 	 */
    974    1.1    dyoung 	sd.sd_tag = regs->r_bt;
    975    1.1    dyoung 	sd.sd_bsh = regs->r_bh;
    976    1.1    dyoung 	sd.sd_regsize = 1;
    977    1.1    dyoung 	sd.sd_control_offset = RTW_9346CR;
    978    1.1    dyoung 	sd.sd_status_offset = RTW_9346CR;
    979    1.1    dyoung 	sd.sd_dataout_offset = RTW_9346CR;
    980    1.1    dyoung 	sd.sd_CK = RTW_9346CR_EESK;
    981    1.1    dyoung 	sd.sd_CS = RTW_9346CR_EECS;
    982    1.1    dyoung 	sd.sd_DI = RTW_9346CR_EEDO;
    983    1.1    dyoung 	sd.sd_DO = RTW_9346CR_EEDI;
    984   1.44     perry 	/* make read_seeprom enter EEPROM read/write mode */
    985    1.1    dyoung 	sd.sd_MS = ecr;
    986    1.1    dyoung 	sd.sd_RDY = 0;
    987    1.1    dyoung 
    988    1.8    dyoung 	/* TBD bus barriers */
    989    1.1    dyoung 	if (!read_seeprom(&sd, sr->sr_content, 0, sr->sr_size/2)) {
    990   1.98    dyoung 		aprint_error_dev(dev, "could not read SROM\n");
    991    1.1    dyoung 		free(sr->sr_content, M_DEVBUF);
    992    1.1    dyoung 		sr->sr_content = NULL;
    993    1.1    dyoung 		return -1;	/* XXX */
    994    1.1    dyoung 	}
    995    1.1    dyoung 
    996   1.44     perry 	/* end EEPROM read/write mode */
    997    1.1    dyoung 	RTW_WRITE8(regs, RTW_9346CR,
    998    1.1    dyoung 	    (ecr & ~RTW_9346CR_EEM_MASK) | RTW_9346CR_EEM_NORMAL);
    999    1.1    dyoung 	RTW_WBRW(regs, RTW_9346CR, RTW_9346CR);
   1000    1.1    dyoung 
   1001   1.98    dyoung 	if ((rc = rtw_recall_eeprom(regs, dev)) != 0)
   1002    1.1    dyoung 		return rc;
   1003    1.1    dyoung 
   1004    1.1    dyoung #ifdef RTW_DEBUG
   1005    1.1    dyoung 	{
   1006    1.1    dyoung 		int i;
   1007   1.21    dyoung 		RTW_DPRINTF(RTW_DEBUG_ATTACH,
   1008   1.98    dyoung 		    ("\n%s: serial ROM:\n\t", device_xname(dev)));
   1009    1.1    dyoung 		for (i = 0; i < sr->sr_size/2; i++) {
   1010    1.1    dyoung 			if (((i % 8) == 0) && (i != 0))
   1011   1.21    dyoung 				RTW_DPRINTF(RTW_DEBUG_ATTACH, ("\n\t"));
   1012   1.21    dyoung 			RTW_DPRINTF(RTW_DEBUG_ATTACH,
   1013   1.21    dyoung 			    (" %04x", sr->sr_content[i]));
   1014    1.1    dyoung 		}
   1015   1.21    dyoung 		RTW_DPRINTF(RTW_DEBUG_ATTACH, ("\n"));
   1016    1.1    dyoung 	}
   1017    1.1    dyoung #endif /* RTW_DEBUG */
   1018    1.1    dyoung 	return 0;
   1019    1.1    dyoung }
   1020    1.1    dyoung 
   1021    1.4    dyoung static void
   1022    1.4    dyoung rtw_set_rfprog(struct rtw_regs *regs, enum rtw_rfchipid rfchipid,
   1023   1.98    dyoung     device_t dev)
   1024    1.4    dyoung {
   1025   1.37    dyoung 	uint8_t cfg4;
   1026    1.4    dyoung 	const char *method;
   1027    1.4    dyoung 
   1028    1.4    dyoung 	cfg4 = RTW_READ8(regs, RTW_CONFIG4) & ~RTW_CONFIG4_RFTYPE_MASK;
   1029    1.4    dyoung 
   1030    1.4    dyoung 	switch (rfchipid) {
   1031    1.4    dyoung 	default:
   1032   1.75    dyoung 		cfg4 |= __SHIFTIN(rtw_rfprog_fallback, RTW_CONFIG4_RFTYPE_MASK);
   1033    1.4    dyoung 		method = "fallback";
   1034    1.4    dyoung 		break;
   1035    1.4    dyoung 	case RTW_RFCHIPID_INTERSIL:
   1036    1.4    dyoung 		cfg4 |= RTW_CONFIG4_RFTYPE_INTERSIL;
   1037    1.4    dyoung 		method = "Intersil";
   1038    1.4    dyoung 		break;
   1039    1.4    dyoung 	case RTW_RFCHIPID_PHILIPS:
   1040    1.4    dyoung 		cfg4 |= RTW_CONFIG4_RFTYPE_PHILIPS;
   1041    1.4    dyoung 		method = "Philips";
   1042    1.4    dyoung 		break;
   1043   1.42    dyoung 	case RTW_RFCHIPID_GCT:	/* XXX a guess */
   1044    1.4    dyoung 	case RTW_RFCHIPID_RFMD:
   1045    1.4    dyoung 		cfg4 |= RTW_CONFIG4_RFTYPE_RFMD;
   1046    1.4    dyoung 		method = "RFMD";
   1047    1.4    dyoung 		break;
   1048    1.4    dyoung 	}
   1049    1.4    dyoung 
   1050    1.4    dyoung 	RTW_WRITE8(regs, RTW_CONFIG4, cfg4);
   1051    1.4    dyoung 
   1052    1.8    dyoung 	RTW_WBR(regs, RTW_CONFIG4, RTW_CONFIG4);
   1053    1.8    dyoung 
   1054  1.120  christos #ifdef RTW_DEBUG
   1055   1.21    dyoung 	RTW_DPRINTF(RTW_DEBUG_INIT,
   1056   1.98    dyoung 	    ("%s: %s RF programming method, %#02x\n", device_xname(dev), method,
   1057   1.10    dyoung 	    RTW_READ8(regs, RTW_CONFIG4)));
   1058  1.120  christos #else
   1059  1.120  christos 	__USE(method);
   1060  1.120  christos #endif
   1061    1.4    dyoung }
   1062    1.4    dyoung 
   1063   1.61     perry static inline void
   1064    1.1    dyoung rtw_init_channels(enum rtw_locale locale,
   1065   1.98    dyoung     struct ieee80211_channel (*chans)[IEEE80211_CHAN_MAX+1], device_t dev)
   1066    1.1    dyoung {
   1067    1.1    dyoung 	int i;
   1068    1.1    dyoung 	const char *name = NULL;
   1069    1.1    dyoung #define ADD_CHANNEL(_chans, _chan) do {			\
   1070    1.1    dyoung 	(*_chans)[_chan].ic_flags = IEEE80211_CHAN_B;		\
   1071    1.1    dyoung 	(*_chans)[_chan].ic_freq =				\
   1072    1.1    dyoung 	    ieee80211_ieee2mhz(_chan, (*_chans)[_chan].ic_flags);\
   1073    1.1    dyoung } while (0)
   1074    1.1    dyoung 
   1075    1.1    dyoung 	switch (locale) {
   1076    1.1    dyoung 	case RTW_LOCALE_USA:	/* 1-11 */
   1077    1.1    dyoung 		name = "USA";
   1078    1.1    dyoung 		for (i = 1; i <= 11; i++)
   1079    1.1    dyoung 			ADD_CHANNEL(chans, i);
   1080    1.1    dyoung 		break;
   1081    1.1    dyoung 	case RTW_LOCALE_JAPAN:	/* 1-14 */
   1082    1.1    dyoung 		name = "Japan";
   1083    1.1    dyoung 		ADD_CHANNEL(chans, 14);
   1084    1.1    dyoung 		for (i = 1; i <= 14; i++)
   1085    1.1    dyoung 			ADD_CHANNEL(chans, i);
   1086    1.1    dyoung 		break;
   1087    1.1    dyoung 	case RTW_LOCALE_EUROPE:	/* 1-13 */
   1088    1.1    dyoung 		name = "Europe";
   1089    1.1    dyoung 		for (i = 1; i <= 13; i++)
   1090    1.1    dyoung 			ADD_CHANNEL(chans, i);
   1091    1.1    dyoung 		break;
   1092    1.1    dyoung 	default:			/* 10-11 allowed by most countries */
   1093    1.1    dyoung 		name = "<unknown>";
   1094    1.1    dyoung 		for (i = 10; i <= 11; i++)
   1095    1.1    dyoung 			ADD_CHANNEL(chans, i);
   1096    1.1    dyoung 		break;
   1097    1.1    dyoung 	}
   1098   1.98    dyoung 	aprint_normal_dev(dev, "Geographic Location %s\n", name);
   1099    1.1    dyoung #undef ADD_CHANNEL
   1100    1.1    dyoung }
   1101    1.1    dyoung 
   1102   1.58    dyoung 
   1103   1.61     perry static inline void
   1104   1.58    dyoung rtw_identify_country(struct rtw_regs *regs, enum rtw_locale *locale)
   1105    1.1    dyoung {
   1106   1.37    dyoung 	uint8_t cfg0 = RTW_READ8(regs, RTW_CONFIG0);
   1107    1.1    dyoung 
   1108    1.1    dyoung 	switch (cfg0 & RTW_CONFIG0_GL_MASK) {
   1109    1.1    dyoung 	case RTW_CONFIG0_GL_USA:
   1110   1.55    dogcow 	case _RTW_CONFIG0_GL_USA:
   1111    1.1    dyoung 		*locale = RTW_LOCALE_USA;
   1112    1.1    dyoung 		break;
   1113    1.1    dyoung 	case RTW_CONFIG0_GL_JAPAN:
   1114    1.1    dyoung 		*locale = RTW_LOCALE_JAPAN;
   1115    1.1    dyoung 		break;
   1116    1.1    dyoung 	case RTW_CONFIG0_GL_EUROPE:
   1117    1.1    dyoung 		*locale = RTW_LOCALE_EUROPE;
   1118    1.1    dyoung 		break;
   1119    1.1    dyoung 	default:
   1120    1.1    dyoung 		*locale = RTW_LOCALE_UNKNOWN;
   1121    1.1    dyoung 		break;
   1122    1.1    dyoung 	}
   1123    1.1    dyoung }
   1124    1.1    dyoung 
   1125   1.61     perry static inline int
   1126   1.37    dyoung rtw_identify_sta(struct rtw_regs *regs, uint8_t (*addr)[IEEE80211_ADDR_LEN],
   1127   1.98    dyoung     device_t dev)
   1128    1.1    dyoung {
   1129   1.37    dyoung 	static const uint8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
   1130    1.1    dyoung 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00
   1131    1.1    dyoung 	};
   1132   1.37    dyoung 	uint32_t idr0 = RTW_READ(regs, RTW_IDR0),
   1133    1.1    dyoung 	          idr1 = RTW_READ(regs, RTW_IDR1);
   1134    1.1    dyoung 
   1135   1.75    dyoung 	(*addr)[0] = __SHIFTOUT(idr0, __BITS(0,  7));
   1136   1.75    dyoung 	(*addr)[1] = __SHIFTOUT(idr0, __BITS(8,  15));
   1137   1.75    dyoung 	(*addr)[2] = __SHIFTOUT(idr0, __BITS(16, 23));
   1138   1.75    dyoung 	(*addr)[3] = __SHIFTOUT(idr0, __BITS(24 ,31));
   1139    1.1    dyoung 
   1140   1.75    dyoung 	(*addr)[4] = __SHIFTOUT(idr1, __BITS(0,  7));
   1141   1.75    dyoung 	(*addr)[5] = __SHIFTOUT(idr1, __BITS(8, 15));
   1142    1.1    dyoung 
   1143    1.1    dyoung 	if (IEEE80211_ADDR_EQ(addr, empty_macaddr)) {
   1144   1.98    dyoung 		aprint_error_dev(dev,
   1145   1.98    dyoung 		    "could not get mac address, attach failed\n");
   1146    1.1    dyoung 		return ENXIO;
   1147    1.1    dyoung 	}
   1148    1.1    dyoung 
   1149   1.98    dyoung 	aprint_normal_dev(dev, "802.11 address %s\n", ether_sprintf(*addr));
   1150    1.1    dyoung 
   1151    1.1    dyoung 	return 0;
   1152    1.1    dyoung }
   1153    1.1    dyoung 
   1154   1.37    dyoung static uint8_t
   1155    1.1    dyoung rtw_chan2txpower(struct rtw_srom *sr, struct ieee80211com *ic,
   1156    1.1    dyoung     struct ieee80211_channel *chan)
   1157    1.1    dyoung {
   1158    1.1    dyoung 	u_int idx = RTW_SR_TXPOWER1 + ieee80211_chan2ieee(ic, chan) - 1;
   1159   1.97    dyoung 	KASSERT(idx >= RTW_SR_TXPOWER1 && idx <= RTW_SR_TXPOWER14);
   1160    1.1    dyoung 	return RTW_SR_GET(sr, idx);
   1161    1.1    dyoung }
   1162    1.1    dyoung 
   1163    1.1    dyoung static void
   1164   1.34    dyoung rtw_txdesc_blk_init_all(struct rtw_txdesc_blk *tdb)
   1165    1.1    dyoung {
   1166    1.1    dyoung 	int pri;
   1167   1.58    dyoung 	/* nfree: the number of free descriptors in each ring.
   1168   1.58    dyoung 	 * The beacon ring is a special case: I do not let the
   1169   1.58    dyoung 	 * driver use all of the descriptors on the beacon ring.
   1170   1.58    dyoung 	 * The reasons are two-fold:
   1171   1.58    dyoung 	 *
   1172   1.58    dyoung 	 * (1) A BEACON descriptor's OWN bit is (apparently) not
   1173   1.58    dyoung 	 * updated, so the driver cannot easily know if the descriptor
   1174   1.58    dyoung 	 * belongs to it, or if it is racing the NIC.  If the NIC
   1175   1.58    dyoung 	 * does not OWN every descriptor, then the driver can safely
   1176   1.58    dyoung 	 * update the descriptors when RTW_TBDA points at tdb_next.
   1177   1.58    dyoung 	 *
   1178   1.58    dyoung 	 * (2) I hope that the NIC will process more than one BEACON
   1179   1.58    dyoung 	 * descriptor in a single beacon interval, since that will
   1180   1.58    dyoung 	 * enable multiple-BSS support.  Since the NIC does not
   1181   1.58    dyoung 	 * clear the OWN bit, there is no natural place for it to
   1182   1.58    dyoung 	 * stop processing BEACON desciptors.  Maybe it will *not*
   1183   1.58    dyoung 	 * stop processing them!  I do not want to chance the NIC
   1184   1.58    dyoung 	 * looping around and around a saturated beacon ring, so
   1185   1.58    dyoung 	 * I will leave one descriptor unOWNed at all times.
   1186   1.58    dyoung 	 */
   1187   1.58    dyoung 	u_int nfree[RTW_NTXPRI] =
   1188   1.58    dyoung 	    {RTW_NTXDESCLO, RTW_NTXDESCMD, RTW_NTXDESCHI,
   1189   1.58    dyoung 	     RTW_NTXDESCBCN - 1};
   1190    1.1    dyoung 
   1191    1.1    dyoung 	for (pri = 0; pri < RTW_NTXPRI; pri++) {
   1192   1.58    dyoung 		tdb[pri].tdb_nfree = nfree[pri];
   1193   1.34    dyoung 		tdb[pri].tdb_next = 0;
   1194    1.1    dyoung 	}
   1195    1.1    dyoung }
   1196    1.1    dyoung 
   1197    1.1    dyoung static int
   1198   1.34    dyoung rtw_txsoft_blk_init(struct rtw_txsoft_blk *tsb)
   1199    1.1    dyoung {
   1200    1.1    dyoung 	int i;
   1201   1.34    dyoung 	struct rtw_txsoft *ts;
   1202    1.1    dyoung 
   1203   1.34    dyoung 	SIMPLEQ_INIT(&tsb->tsb_dirtyq);
   1204   1.34    dyoung 	SIMPLEQ_INIT(&tsb->tsb_freeq);
   1205   1.34    dyoung 	for (i = 0; i < tsb->tsb_ndesc; i++) {
   1206   1.34    dyoung 		ts = &tsb->tsb_desc[i];
   1207   1.34    dyoung 		ts->ts_mbuf = NULL;
   1208   1.34    dyoung 		SIMPLEQ_INSERT_TAIL(&tsb->tsb_freeq, ts, ts_q);
   1209    1.1    dyoung 	}
   1210   1.58    dyoung 	tsb->tsb_tx_timer = 0;
   1211    1.1    dyoung 	return 0;
   1212    1.1    dyoung }
   1213    1.1    dyoung 
   1214    1.1    dyoung static void
   1215   1.34    dyoung rtw_txsoft_blk_init_all(struct rtw_txsoft_blk *tsb)
   1216    1.1    dyoung {
   1217    1.1    dyoung 	int pri;
   1218    1.3    dyoung 	for (pri = 0; pri < RTW_NTXPRI; pri++)
   1219   1.34    dyoung 		rtw_txsoft_blk_init(&tsb[pri]);
   1220    1.1    dyoung }
   1221    1.1    dyoung 
   1222   1.61     perry static inline void
   1223   1.34    dyoung rtw_rxdescs_sync(struct rtw_rxdesc_blk *rdb, int desc0, int nsync, int ops)
   1224    1.1    dyoung {
   1225   1.34    dyoung 	KASSERT(nsync <= rdb->rdb_ndesc);
   1226    1.1    dyoung 	/* sync to end of ring */
   1227   1.34    dyoung 	if (desc0 + nsync > rdb->rdb_ndesc) {
   1228   1.34    dyoung 		bus_dmamap_sync(rdb->rdb_dmat, rdb->rdb_dmamap,
   1229    1.1    dyoung 		    offsetof(struct rtw_descs, hd_rx[desc0]),
   1230   1.34    dyoung 		    sizeof(struct rtw_rxdesc) * (rdb->rdb_ndesc - desc0), ops);
   1231   1.34    dyoung 		nsync -= (rdb->rdb_ndesc - desc0);
   1232    1.1    dyoung 		desc0 = 0;
   1233    1.1    dyoung 	}
   1234    1.1    dyoung 
   1235   1.34    dyoung 	KASSERT(desc0 < rdb->rdb_ndesc);
   1236   1.34    dyoung 	KASSERT(nsync <= rdb->rdb_ndesc);
   1237   1.34    dyoung 	KASSERT(desc0 + nsync <= rdb->rdb_ndesc);
   1238   1.21    dyoung 
   1239    1.1    dyoung 	/* sync what remains */
   1240   1.34    dyoung 	bus_dmamap_sync(rdb->rdb_dmat, rdb->rdb_dmamap,
   1241    1.1    dyoung 	    offsetof(struct rtw_descs, hd_rx[desc0]),
   1242    1.1    dyoung 	    sizeof(struct rtw_rxdesc) * nsync, ops);
   1243    1.1    dyoung }
   1244    1.1    dyoung 
   1245    1.1    dyoung static void
   1246   1.34    dyoung rtw_txdescs_sync(struct rtw_txdesc_blk *tdb, u_int desc0, u_int nsync, int ops)
   1247    1.1    dyoung {
   1248    1.1    dyoung 	/* sync to end of ring */
   1249   1.34    dyoung 	if (desc0 + nsync > tdb->tdb_ndesc) {
   1250   1.34    dyoung 		bus_dmamap_sync(tdb->tdb_dmat, tdb->tdb_dmamap,
   1251   1.34    dyoung 		    tdb->tdb_ofs + sizeof(struct rtw_txdesc) * desc0,
   1252   1.34    dyoung 		    sizeof(struct rtw_txdesc) * (tdb->tdb_ndesc - desc0),
   1253    1.1    dyoung 		    ops);
   1254   1.34    dyoung 		nsync -= (tdb->tdb_ndesc - desc0);
   1255    1.1    dyoung 		desc0 = 0;
   1256    1.1    dyoung 	}
   1257    1.1    dyoung 
   1258    1.1    dyoung 	/* sync what remains */
   1259   1.34    dyoung 	bus_dmamap_sync(tdb->tdb_dmat, tdb->tdb_dmamap,
   1260   1.34    dyoung 	    tdb->tdb_ofs + sizeof(struct rtw_txdesc) * desc0,
   1261    1.1    dyoung 	    sizeof(struct rtw_txdesc) * nsync, ops);
   1262    1.1    dyoung }
   1263    1.1    dyoung 
   1264    1.1    dyoung static void
   1265   1.34    dyoung rtw_txdescs_sync_all(struct rtw_txdesc_blk *tdb)
   1266    1.1    dyoung {
   1267    1.1    dyoung 	int pri;
   1268    1.1    dyoung 	for (pri = 0; pri < RTW_NTXPRI; pri++) {
   1269   1.34    dyoung 		rtw_txdescs_sync(&tdb[pri], 0, tdb[pri].tdb_ndesc,
   1270    1.1    dyoung 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1271    1.1    dyoung 	}
   1272    1.1    dyoung }
   1273    1.1    dyoung 
   1274    1.1    dyoung static void
   1275   1.34    dyoung rtw_rxbufs_release(bus_dma_tag_t dmat, struct rtw_rxsoft *desc)
   1276    1.1    dyoung {
   1277    1.1    dyoung 	int i;
   1278   1.34    dyoung 	struct rtw_rxsoft *rs;
   1279    1.1    dyoung 
   1280   1.21    dyoung 	for (i = 0; i < RTW_RXQLEN; i++) {
   1281   1.34    dyoung 		rs = &desc[i];
   1282   1.34    dyoung 		if (rs->rs_mbuf == NULL)
   1283   1.31    dyoung 			continue;
   1284   1.34    dyoung 		bus_dmamap_sync(dmat, rs->rs_dmamap, 0,
   1285   1.34    dyoung 		    rs->rs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1286   1.34    dyoung 		bus_dmamap_unload(dmat, rs->rs_dmamap);
   1287   1.34    dyoung 		m_freem(rs->rs_mbuf);
   1288   1.34    dyoung 		rs->rs_mbuf = NULL;
   1289    1.1    dyoung 	}
   1290    1.1    dyoung }
   1291    1.1    dyoung 
   1292   1.61     perry static inline int
   1293   1.34    dyoung rtw_rxsoft_alloc(bus_dma_tag_t dmat, struct rtw_rxsoft *rs)
   1294    1.1    dyoung {
   1295    1.1    dyoung 	int rc;
   1296    1.1    dyoung 	struct mbuf *m;
   1297    1.1    dyoung 
   1298   1.44     perry 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1299    1.1    dyoung 	if (m == NULL)
   1300   1.18    dyoung 		return ENOBUFS;
   1301    1.1    dyoung 
   1302   1.44     perry 	MCLGET(m, M_DONTWAIT);
   1303   1.31    dyoung 	if ((m->m_flags & M_EXT) == 0) {
   1304   1.31    dyoung 		m_freem(m);
   1305   1.18    dyoung 		return ENOBUFS;
   1306   1.31    dyoung 	}
   1307    1.1    dyoung 
   1308    1.1    dyoung 	m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
   1309    1.1    dyoung 
   1310   1.34    dyoung 	if (rs->rs_mbuf != NULL)
   1311   1.34    dyoung 		bus_dmamap_unload(dmat, rs->rs_dmamap);
   1312   1.18    dyoung 
   1313   1.34    dyoung 	rs->rs_mbuf = NULL;
   1314   1.18    dyoung 
   1315   1.34    dyoung 	rc = bus_dmamap_load_mbuf(dmat, rs->rs_dmamap, m, BUS_DMA_NOWAIT);
   1316   1.18    dyoung 	if (rc != 0) {
   1317   1.18    dyoung 		m_freem(m);
   1318   1.18    dyoung 		return -1;
   1319   1.18    dyoung 	}
   1320    1.1    dyoung 
   1321   1.34    dyoung 	rs->rs_mbuf = m;
   1322    1.1    dyoung 
   1323    1.1    dyoung 	return 0;
   1324    1.1    dyoung }
   1325    1.1    dyoung 
   1326    1.1    dyoung static int
   1327   1.34    dyoung rtw_rxsoft_init_all(bus_dma_tag_t dmat, struct rtw_rxsoft *desc,
   1328   1.98    dyoung     int *ndesc, device_t dev)
   1329    1.1    dyoung {
   1330   1.31    dyoung 	int i, rc = 0;
   1331   1.34    dyoung 	struct rtw_rxsoft *rs;
   1332    1.1    dyoung 
   1333   1.21    dyoung 	for (i = 0; i < RTW_RXQLEN; i++) {
   1334   1.34    dyoung 		rs = &desc[i];
   1335   1.31    dyoung 		/* we're in rtw_init, so there should be no mbufs allocated */
   1336   1.34    dyoung 		KASSERT(rs->rs_mbuf == NULL);
   1337   1.31    dyoung #ifdef RTW_DEBUG
   1338   1.31    dyoung 		if (i == rtw_rxbufs_limit) {
   1339   1.98    dyoung 			aprint_error_dev(dev, "TEST hit %d-buffer limit\n", i);
   1340   1.31    dyoung 			rc = ENOBUFS;
   1341   1.31    dyoung 			break;
   1342   1.31    dyoung 		}
   1343   1.31    dyoung #endif /* RTW_DEBUG */
   1344   1.34    dyoung 		if ((rc = rtw_rxsoft_alloc(dmat, rs)) != 0) {
   1345   1.98    dyoung 			aprint_error_dev(dev,
   1346   1.98    dyoung 			    "rtw_rxsoft_alloc failed, %d buffers, rc %d\n",
   1347   1.98    dyoung 			    i, rc);
   1348   1.31    dyoung 			break;
   1349    1.1    dyoung 		}
   1350    1.1    dyoung 	}
   1351   1.31    dyoung 	*ndesc = i;
   1352   1.31    dyoung 	return rc;
   1353    1.1    dyoung }
   1354    1.1    dyoung 
   1355   1.61     perry static inline void
   1356   1.34    dyoung rtw_rxdesc_init(struct rtw_rxdesc_blk *rdb, struct rtw_rxsoft *rs,
   1357   1.81  christos     int idx, int kick)
   1358    1.1    dyoung {
   1359   1.34    dyoung 	int is_last = (idx == rdb->rdb_ndesc - 1);
   1360   1.21    dyoung 	uint32_t ctl, octl, obuf;
   1361   1.34    dyoung 	struct rtw_rxdesc *rd = &rdb->rdb_desc[idx];
   1362    1.1    dyoung 
   1363   1.92    dyoung 	/* sync the mbuf before the descriptor */
   1364   1.92    dyoung 	bus_dmamap_sync(rdb->rdb_dmat, rs->rs_dmamap, 0,
   1365   1.92    dyoung 	    rs->rs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1366   1.92    dyoung 
   1367   1.34    dyoung 	obuf = rd->rd_buf;
   1368   1.34    dyoung 	rd->rd_buf = htole32(rs->rs_dmamap->dm_segs[0].ds_addr);
   1369    1.1    dyoung 
   1370   1.75    dyoung 	ctl = __SHIFTIN(rs->rs_mbuf->m_len, RTW_RXCTL_LENGTH_MASK) |
   1371    1.1    dyoung 	    RTW_RXCTL_OWN | RTW_RXCTL_FS | RTW_RXCTL_LS;
   1372    1.1    dyoung 
   1373    1.1    dyoung 	if (is_last)
   1374    1.1    dyoung 		ctl |= RTW_RXCTL_EOR;
   1375    1.1    dyoung 
   1376   1.34    dyoung 	octl = rd->rd_ctl;
   1377   1.34    dyoung 	rd->rd_ctl = htole32(ctl);
   1378    1.1    dyoung 
   1379  1.120  christos #ifdef RTW_DEBUG
   1380   1.24    dyoung 	RTW_DPRINTF(
   1381   1.24    dyoung 	    kick ? (RTW_DEBUG_RECV_DESC | RTW_DEBUG_IO_KICK)
   1382   1.24    dyoung 	         : RTW_DEBUG_RECV_DESC,
   1383   1.34    dyoung 	    ("%s: rd %p buf %08x -> %08x ctl %08x -> %08x\n", __func__, rd,
   1384   1.34    dyoung 	     le32toh(obuf), le32toh(rd->rd_buf), le32toh(octl),
   1385   1.34    dyoung 	     le32toh(rd->rd_ctl)));
   1386  1.120  christos #else
   1387  1.120  christos 	__USE(octl);
   1388  1.120  christos 	__USE(obuf);
   1389  1.120  christos #endif
   1390   1.21    dyoung 
   1391    1.1    dyoung 	/* sync the descriptor */
   1392   1.34    dyoung 	bus_dmamap_sync(rdb->rdb_dmat, rdb->rdb_dmamap,
   1393   1.33    dyoung 	    RTW_DESC_OFFSET(hd_rx, idx), sizeof(struct rtw_rxdesc),
   1394    1.1    dyoung 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1395    1.1    dyoung }
   1396    1.1    dyoung 
   1397    1.1    dyoung static void
   1398   1.34    dyoung rtw_rxdesc_init_all(struct rtw_rxdesc_blk *rdb, struct rtw_rxsoft *ctl, int kick)
   1399    1.1    dyoung {
   1400    1.1    dyoung 	int i;
   1401   1.34    dyoung 	struct rtw_rxsoft *rs;
   1402    1.1    dyoung 
   1403   1.34    dyoung 	for (i = 0; i < rdb->rdb_ndesc; i++) {
   1404   1.34    dyoung 		rs = &ctl[i];
   1405   1.34    dyoung 		rtw_rxdesc_init(rdb, rs, i, kick);
   1406    1.1    dyoung 	}
   1407    1.1    dyoung }
   1408    1.1    dyoung 
   1409    1.1    dyoung static void
   1410   1.83    dyoung rtw_io_enable(struct rtw_softc *sc, uint8_t flags, int enable)
   1411    1.1    dyoung {
   1412   1.83    dyoung 	struct rtw_regs *regs = &sc->sc_regs;
   1413   1.37    dyoung 	uint8_t cr;
   1414    1.1    dyoung 
   1415   1.21    dyoung 	RTW_DPRINTF(RTW_DEBUG_IOSTATE, ("%s: %s 0x%02x\n", __func__,
   1416    1.1    dyoung 	    enable ? "enable" : "disable", flags));
   1417    1.1    dyoung 
   1418    1.1    dyoung 	cr = RTW_READ8(regs, RTW_CR);
   1419    1.1    dyoung 
   1420    1.1    dyoung 	/* XXX reference source does not enable MULRW */
   1421    1.1    dyoung 	/* enable PCI Read/Write Multiple */
   1422    1.1    dyoung 	cr |= RTW_CR_MULRW;
   1423   1.83    dyoung 
   1424   1.83    dyoung 	/* The receive engine will always start at RDSAR.  */
   1425   1.83    dyoung 	if (enable && (flags & ~cr & RTW_CR_RE)) {
   1426   1.83    dyoung 		struct rtw_rxdesc_blk *rdb;
   1427   1.83    dyoung 		rdb = &sc->sc_rxdesc_blk;
   1428   1.83    dyoung 		rdb->rdb_next = 0;
   1429   1.83    dyoung 	}
   1430    1.1    dyoung 
   1431    1.1    dyoung 	RTW_RBW(regs, RTW_CR, RTW_CR);	/* XXX paranoia? */
   1432    1.1    dyoung 	if (enable)
   1433    1.1    dyoung 		cr |= flags;
   1434    1.1    dyoung 	else
   1435    1.1    dyoung 		cr &= ~flags;
   1436    1.1    dyoung 	RTW_WRITE8(regs, RTW_CR, cr);
   1437    1.1    dyoung 	RTW_SYNC(regs, RTW_CR, RTW_CR);
   1438   1.83    dyoung 
   1439   1.83    dyoung #ifdef RTW_DIAG
   1440   1.83    dyoung 	if (cr & RTW_CR_TE)
   1441   1.83    dyoung 		rtw_txring_fixup(sc, __func__, __LINE__);
   1442   1.83    dyoung #endif
   1443  1.103    dyoung 	if (cr & RTW_CR_TE) {
   1444  1.103    dyoung 		rtw_tx_kick(&sc->sc_regs,
   1445  1.103    dyoung 		    RTW_TPPOLL_HPQ | RTW_TPPOLL_NPQ | RTW_TPPOLL_LPQ);
   1446  1.103    dyoung 	}
   1447    1.1    dyoung }
   1448    1.1    dyoung 
   1449    1.1    dyoung static void
   1450   1.81  christos rtw_intr_rx(struct rtw_softc *sc, uint16_t isr)
   1451    1.1    dyoung {
   1452   1.42    dyoung #define	IS_BEACON(__fc0)						\
   1453   1.42    dyoung     ((__fc0 & (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==\
   1454   1.42    dyoung      (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_BEACON))
   1455   1.42    dyoung 
   1456   1.30    dyoung 	static const int ratetbl[4] = {2, 4, 11, 22};	/* convert rates:
   1457   1.30    dyoung 							 * hardware -> net80211
   1458   1.30    dyoung 							 */
   1459   1.21    dyoung 	u_int next, nproc = 0;
   1460  1.125    nonaka 	int hwrate, len, rate, rssi, sq, s;
   1461   1.37    dyoung 	uint32_t hrssi, hstat, htsfth, htsftl;
   1462   1.34    dyoung 	struct rtw_rxdesc *rd;
   1463   1.34    dyoung 	struct rtw_rxsoft *rs;
   1464   1.34    dyoung 	struct rtw_rxdesc_blk *rdb;
   1465    1.1    dyoung 	struct mbuf *m;
   1466   1.48    dyoung 	struct ifnet *ifp = &sc->sc_if;
   1467    1.1    dyoung 
   1468    1.1    dyoung 	struct ieee80211_node *ni;
   1469   1.48    dyoung 	struct ieee80211_frame_min *wh;
   1470    1.1    dyoung 
   1471   1.34    dyoung 	rdb = &sc->sc_rxdesc_blk;
   1472   1.21    dyoung 
   1473   1.83    dyoung 	for (next = rdb->rdb_next; ; next = rdb->rdb_next) {
   1474   1.83    dyoung 		KASSERT(next < rdb->rdb_ndesc);
   1475   1.93    dyoung 
   1476   1.34    dyoung 		rtw_rxdescs_sync(rdb, next, 1,
   1477   1.31    dyoung 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1478   1.34    dyoung 		rd = &rdb->rdb_desc[next];
   1479   1.34    dyoung 		rs = &sc->sc_rxsoft[next];
   1480    1.1    dyoung 
   1481   1.34    dyoung 		hstat = le32toh(rd->rd_stat);
   1482   1.34    dyoung 		hrssi = le32toh(rd->rd_rssi);
   1483   1.34    dyoung 		htsfth = le32toh(rd->rd_tsfth);
   1484   1.34    dyoung 		htsftl = le32toh(rd->rd_tsftl);
   1485    1.1    dyoung 
   1486   1.21    dyoung 		RTW_DPRINTF(RTW_DEBUG_RECV_DESC,
   1487   1.21    dyoung 		    ("%s: rxdesc[%d] hstat %08x hrssi %08x htsft %08x%08x\n",
   1488   1.21    dyoung 		    __func__, next, hstat, hrssi, htsfth, htsftl));
   1489   1.21    dyoung 
   1490   1.21    dyoung 		++nproc;
   1491   1.21    dyoung 
   1492   1.21    dyoung 		/* still belongs to NIC */
   1493   1.21    dyoung 		if ((hstat & RTW_RXSTAT_OWN) != 0) {
   1494   1.82    dyoung 			rtw_rxdescs_sync(rdb, next, 1, BUS_DMASYNC_PREREAD);
   1495   1.83    dyoung 			break;
   1496   1.83    dyoung 		}
   1497    1.1    dyoung 
   1498   1.83    dyoung                 /* ieee80211_input() might reset the receive engine
   1499   1.83    dyoung                  * (e.g. by indirectly calling rtw_tune()), so save
   1500   1.83    dyoung                  * the next pointer here and retrieve it again on
   1501   1.83    dyoung                  * the next round.
   1502   1.83    dyoung 		 */
   1503   1.83    dyoung 		rdb->rdb_next = (next + 1) % rdb->rdb_ndesc;
   1504    1.1    dyoung 
   1505   1.45    dyoung #ifdef RTW_DEBUG
   1506   1.45    dyoung #define PRINTSTAT(flag) do { \
   1507   1.45    dyoung 	if ((hstat & flag) != 0) { \
   1508   1.45    dyoung 		printf("%s" #flag, delim); \
   1509   1.45    dyoung 		delim = ","; \
   1510   1.45    dyoung 	} \
   1511   1.45    dyoung } while (0)
   1512   1.45    dyoung 		if ((rtw_debug & RTW_DEBUG_RECV_DESC) != 0) {
   1513   1.45    dyoung 			const char *delim = "<";
   1514   1.98    dyoung 			printf("%s: ", device_xname(sc->sc_dev));
   1515   1.45    dyoung 			if ((hstat & RTW_RXSTAT_DEBUG) != 0) {
   1516   1.45    dyoung 				printf("status %08x", hstat);
   1517   1.45    dyoung 				PRINTSTAT(RTW_RXSTAT_SPLCP);
   1518   1.45    dyoung 				PRINTSTAT(RTW_RXSTAT_MAR);
   1519   1.45    dyoung 				PRINTSTAT(RTW_RXSTAT_PAR);
   1520   1.45    dyoung 				PRINTSTAT(RTW_RXSTAT_BAR);
   1521   1.45    dyoung 				PRINTSTAT(RTW_RXSTAT_PWRMGT);
   1522   1.45    dyoung 				PRINTSTAT(RTW_RXSTAT_CRC32);
   1523   1.45    dyoung 				PRINTSTAT(RTW_RXSTAT_ICV);
   1524   1.45    dyoung 				printf(">, ");
   1525   1.45    dyoung 			}
   1526   1.45    dyoung 		}
   1527   1.45    dyoung #endif /* RTW_DEBUG */
   1528   1.45    dyoung 
   1529    1.1    dyoung 		if ((hstat & RTW_RXSTAT_IOERROR) != 0) {
   1530   1.98    dyoung 			aprint_error_dev(sc->sc_dev,
   1531   1.98    dyoung 			    "DMA error/FIFO overflow %08" PRIx32 ", "
   1532   1.98    dyoung 			    "rx descriptor %d\n", hstat, next);
   1533   1.48    dyoung 			ifp->if_ierrors++;
   1534    1.1    dyoung 			goto next;
   1535    1.1    dyoung 		}
   1536    1.1    dyoung 
   1537   1.75    dyoung 		len = __SHIFTOUT(hstat, RTW_RXSTAT_LENGTH_MASK);
   1538   1.22    dyoung 		if (len < IEEE80211_MIN_LEN) {
   1539   1.22    dyoung 			sc->sc_ic.ic_stats.is_rx_tooshort++;
   1540   1.22    dyoung 			goto next;
   1541   1.22    dyoung 		}
   1542   1.98    dyoung 		if (len > rs->rs_mbuf->m_len) {
   1543  1.101    dyoung 			aprint_error_dev(sc->sc_dev,
   1544  1.101    dyoung 			    "rx frame too long, %d > %d, %08" PRIx32
   1545  1.101    dyoung 			    ", desc %d\n",
   1546  1.101    dyoung 			    len, rs->rs_mbuf->m_len, hstat, next);
   1547   1.98    dyoung 			ifp->if_ierrors++;
   1548   1.98    dyoung 			goto next;
   1549   1.98    dyoung 		}
   1550   1.43   thorpej 
   1551   1.75    dyoung 		hwrate = __SHIFTOUT(hstat, RTW_RXSTAT_RATE_MASK);
   1552   1.87    dyoung 		if (hwrate >= __arraycount(ratetbl)) {
   1553   1.98    dyoung 			aprint_error_dev(sc->sc_dev,
   1554   1.98    dyoung 			    "unknown rate #%" __PRIuBITS "\n",
   1555   1.75    dyoung 			    __SHIFTOUT(hstat, RTW_RXSTAT_RATE_MASK));
   1556   1.48    dyoung 			ifp->if_ierrors++;
   1557   1.22    dyoung 			goto next;
   1558    1.1    dyoung 		}
   1559   1.30    dyoung 		rate = ratetbl[hwrate];
   1560    1.1    dyoung 
   1561    1.1    dyoung #ifdef RTW_DEBUG
   1562   1.45    dyoung 		RTW_DPRINTF(RTW_DEBUG_RECV_DESC,
   1563   1.45    dyoung 		    ("rate %d.%d Mb/s, time %08x%08x\n", (rate * 5) / 10,
   1564   1.45    dyoung 		     (rate * 5) % 10, htsfth, htsftl));
   1565    1.1    dyoung #endif /* RTW_DEBUG */
   1566    1.1    dyoung 
   1567    1.1    dyoung 		/* if bad flags, skip descriptor */
   1568    1.1    dyoung 		if ((hstat & RTW_RXSTAT_ONESEG) != RTW_RXSTAT_ONESEG) {
   1569   1.98    dyoung 			aprint_error_dev(sc->sc_dev, "too many rx segments, "
   1570   1.98    dyoung 			    "next=%d, %08" PRIx32 "\n", next, hstat);
   1571    1.1    dyoung 			goto next;
   1572    1.1    dyoung 		}
   1573    1.1    dyoung 
   1574   1.34    dyoung 		bus_dmamap_sync(sc->sc_dmat, rs->rs_dmamap, 0,
   1575   1.34    dyoung 		    rs->rs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1576   1.18    dyoung 
   1577   1.34    dyoung 		m = rs->rs_mbuf;
   1578    1.1    dyoung 
   1579    1.1    dyoung 		/* if temporarily out of memory, re-use mbuf */
   1580   1.34    dyoung 		switch (rtw_rxsoft_alloc(sc->sc_dmat, rs)) {
   1581   1.18    dyoung 		case 0:
   1582   1.18    dyoung 			break;
   1583   1.18    dyoung 		case ENOBUFS:
   1584   1.98    dyoung 			aprint_error_dev(sc->sc_dev,
   1585   1.98    dyoung 			    "rtw_rxsoft_alloc(, %d) failed, dropping packet\n",
   1586   1.98    dyoung 			    next);
   1587    1.1    dyoung 			goto next;
   1588   1.18    dyoung 		default:
   1589   1.18    dyoung 			/* XXX shorten rx ring, instead? */
   1590   1.98    dyoung 			aprint_error_dev(sc->sc_dev,
   1591   1.98    dyoung 			    "could not load DMA map\n");
   1592    1.1    dyoung 		}
   1593    1.1    dyoung 
   1594   1.93    dyoung 		sq = __SHIFTOUT(hrssi, RTW_RXRSSI_SQ);
   1595   1.93    dyoung 
   1596    1.1    dyoung 		if (sc->sc_rfchipid == RTW_RFCHIPID_PHILIPS)
   1597   1.93    dyoung 			rssi = UINT8_MAX - sq;
   1598    1.1    dyoung 		else {
   1599   1.75    dyoung 			rssi = __SHIFTOUT(hrssi, RTW_RXRSSI_IMR_RSSI);
   1600    1.1    dyoung 			/* TBD find out each front-end's LNA gain in the
   1601    1.1    dyoung 			 * front-end's units
   1602    1.1    dyoung 			 */
   1603    1.1    dyoung 			if ((hrssi & RTW_RXRSSI_IMR_LNA) == 0)
   1604    1.1    dyoung 				rssi |= 0x80;
   1605    1.1    dyoung 		}
   1606    1.1    dyoung 
   1607   1.34    dyoung 		/* Note well: now we cannot recycle the rs_mbuf unless
   1608   1.32    dyoung 		 * we restore its original length.
   1609   1.32    dyoung 		 */
   1610  1.123     ozaki 		m_set_rcvif(m, ifp);
   1611   1.22    dyoung 		m->m_pkthdr.len = m->m_len = len;
   1612    1.1    dyoung 
   1613   1.48    dyoung 		wh = mtod(m, struct ieee80211_frame_min *);
   1614   1.42    dyoung 
   1615  1.125    nonaka 		s = splnet();
   1616  1.125    nonaka 
   1617   1.42    dyoung 		if (!IS_BEACON(wh->i_fc[0]))
   1618   1.42    dyoung 			sc->sc_led_state.ls_event |= RTW_LED_S_RX;
   1619    1.1    dyoung 
   1620    1.1    dyoung 		sc->sc_tsfth = htsfth;
   1621    1.1    dyoung 
   1622   1.10    dyoung #ifdef RTW_DEBUG
   1623   1.48    dyoung 		if ((ifp->if_flags & (IFF_DEBUG|IFF_LINK2)) ==
   1624   1.10    dyoung 		    (IFF_DEBUG|IFF_LINK2)) {
   1625   1.10    dyoung 			ieee80211_dump_pkt(mtod(m, uint8_t *), m->m_pkthdr.len,
   1626   1.10    dyoung 			    rate, rssi);
   1627   1.10    dyoung 		}
   1628   1.10    dyoung #endif /* RTW_DEBUG */
   1629   1.32    dyoung 
   1630   1.32    dyoung 		if (sc->sc_radiobpf != NULL) {
   1631   1.32    dyoung 			struct rtw_rx_radiotap_header *rr = &sc->sc_rxtap;
   1632   1.32    dyoung 
   1633   1.32    dyoung 			rr->rr_tsft =
   1634   1.32    dyoung 			    htole64(((uint64_t)htsfth << 32) | htsftl);
   1635   1.32    dyoung 
   1636   1.93    dyoung 			rr->rr_flags = IEEE80211_RADIOTAP_F_FCS;
   1637   1.93    dyoung 
   1638   1.32    dyoung 			if ((hstat & RTW_RXSTAT_SPLCP) != 0)
   1639   1.93    dyoung 				rr->rr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
   1640   1.93    dyoung 			if ((hstat & RTW_RXSTAT_CRC32) != 0)
   1641   1.93    dyoung 				rr->rr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
   1642   1.32    dyoung 
   1643   1.32    dyoung 			rr->rr_rate = rate;
   1644   1.32    dyoung 
   1645   1.93    dyoung 			if (sc->sc_rfchipid == RTW_RFCHIPID_PHILIPS)
   1646   1.93    dyoung 				rr->rr_u.u_philips.p_antsignal = rssi;
   1647   1.93    dyoung 			else {
   1648   1.93    dyoung 				rr->rr_u.u_other.o_antsignal = rssi;
   1649   1.93    dyoung 				rr->rr_u.u_other.o_barker_lock =
   1650   1.93    dyoung 				    htole16(UINT8_MAX - sq);
   1651   1.93    dyoung 			}
   1652   1.93    dyoung 
   1653  1.116     joerg 			bpf_mtap2(sc->sc_radiobpf,
   1654  1.128   msaitoh 			    rr, sizeof(sc->sc_rxtapu), m, BPF_D_IN);
   1655   1.32    dyoung 		}
   1656   1.32    dyoung 
   1657   1.93    dyoung 		if ((hstat & RTW_RXSTAT_RES) != 0) {
   1658   1.93    dyoung 			m_freem(m);
   1659  1.125    nonaka 			splx(s);
   1660   1.93    dyoung 			goto next;
   1661   1.93    dyoung 		}
   1662   1.93    dyoung 
   1663   1.93    dyoung 		/* CRC is included with the packet; trim it off. */
   1664   1.93    dyoung 		m_adj(m, -IEEE80211_CRC_LEN);
   1665   1.93    dyoung 
   1666   1.93    dyoung 		/* TBD use _MAR, _BAR, _PAR flags as hints to _find_rxnode? */
   1667   1.93    dyoung 		ni = ieee80211_find_rxnode(&sc->sc_ic, wh);
   1668   1.48    dyoung 		ieee80211_input(&sc->sc_ic, m, ni, rssi, htsftl);
   1669   1.48    dyoung 		ieee80211_free_node(ni);
   1670  1.125    nonaka 		splx(s);
   1671    1.1    dyoung next:
   1672   1.34    dyoung 		rtw_rxdesc_init(rdb, rs, next, 0);
   1673    1.1    dyoung 	}
   1674   1.42    dyoung #undef IS_BEACON
   1675    1.1    dyoung }
   1676    1.1    dyoung 
   1677    1.1    dyoung static void
   1678   1.81  christos rtw_txsoft_release(bus_dma_tag_t dmat, struct ieee80211com *ic,
   1679   1.34    dyoung     struct rtw_txsoft *ts)
   1680    1.5    dyoung {
   1681    1.5    dyoung 	struct mbuf *m;
   1682    1.5    dyoung 	struct ieee80211_node *ni;
   1683    1.5    dyoung 
   1684   1.34    dyoung 	m = ts->ts_mbuf;
   1685   1.34    dyoung 	ni = ts->ts_ni;
   1686   1.21    dyoung 	KASSERT(m != NULL);
   1687   1.21    dyoung 	KASSERT(ni != NULL);
   1688   1.34    dyoung 	ts->ts_mbuf = NULL;
   1689   1.34    dyoung 	ts->ts_ni = NULL;
   1690    1.5    dyoung 
   1691   1.34    dyoung 	bus_dmamap_sync(dmat, ts->ts_dmamap, 0, ts->ts_dmamap->dm_mapsize,
   1692    1.5    dyoung 	    BUS_DMASYNC_POSTWRITE);
   1693   1.34    dyoung 	bus_dmamap_unload(dmat, ts->ts_dmamap);
   1694    1.5    dyoung 	m_freem(m);
   1695   1.48    dyoung 	ieee80211_free_node(ni);
   1696    1.5    dyoung }
   1697    1.5    dyoung 
   1698    1.5    dyoung static void
   1699   1.34    dyoung rtw_txsofts_release(bus_dma_tag_t dmat, struct ieee80211com *ic,
   1700   1.34    dyoung     struct rtw_txsoft_blk *tsb)
   1701    1.5    dyoung {
   1702   1.34    dyoung 	struct rtw_txsoft *ts;
   1703    1.5    dyoung 
   1704   1.34    dyoung 	while ((ts = SIMPLEQ_FIRST(&tsb->tsb_dirtyq)) != NULL) {
   1705   1.34    dyoung 		rtw_txsoft_release(dmat, ic, ts);
   1706   1.34    dyoung 		SIMPLEQ_REMOVE_HEAD(&tsb->tsb_dirtyq, ts_q);
   1707   1.34    dyoung 		SIMPLEQ_INSERT_TAIL(&tsb->tsb_freeq, ts, ts_q);
   1708    1.5    dyoung 	}
   1709   1.58    dyoung 	tsb->tsb_tx_timer = 0;
   1710    1.5    dyoung }
   1711    1.5    dyoung 
   1712   1.61     perry static inline void
   1713   1.34    dyoung rtw_collect_txpkt(struct rtw_softc *sc, struct rtw_txdesc_blk *tdb,
   1714   1.34    dyoung     struct rtw_txsoft *ts, int ndesc)
   1715    1.5    dyoung {
   1716   1.11    dyoung 	uint32_t hstat;
   1717    1.5    dyoung 	int data_retry, rts_retry;
   1718   1.34    dyoung 	struct rtw_txdesc *tdn;
   1719    1.5    dyoung 	const char *condstring;
   1720   1.48    dyoung 	struct ifnet *ifp = &sc->sc_if;
   1721    1.5    dyoung 
   1722   1.34    dyoung 	rtw_txsoft_release(sc->sc_dmat, &sc->sc_ic, ts);
   1723    1.5    dyoung 
   1724   1.34    dyoung 	tdb->tdb_nfree += ndesc;
   1725    1.5    dyoung 
   1726   1.34    dyoung 	tdn = &tdb->tdb_desc[ts->ts_last];
   1727    1.5    dyoung 
   1728   1.34    dyoung 	hstat = le32toh(tdn->td_stat);
   1729   1.75    dyoung 	rts_retry = __SHIFTOUT(hstat, RTW_TXSTAT_RTSRETRY_MASK);
   1730   1.75    dyoung 	data_retry = __SHIFTOUT(hstat, RTW_TXSTAT_DRC_MASK);
   1731    1.5    dyoung 
   1732   1.48    dyoung 	ifp->if_collisions += rts_retry + data_retry;
   1733    1.5    dyoung 
   1734   1.11    dyoung 	if ((hstat & RTW_TXSTAT_TOK) != 0)
   1735    1.5    dyoung 		condstring = "ok";
   1736    1.5    dyoung 	else {
   1737   1.48    dyoung 		ifp->if_oerrors++;
   1738    1.5    dyoung 		condstring = "error";
   1739    1.5    dyoung 	}
   1740    1.5    dyoung 
   1741  1.120  christos #ifdef RTW_DEBUG
   1742   1.21    dyoung 	DPRINTF(sc, RTW_DEBUG_XMIT_DESC,
   1743   1.34    dyoung 	    ("%s: ts %p txdesc[%d, %d] %s tries rts %u data %u\n",
   1744   1.98    dyoung 	    device_xname(sc->sc_dev), ts, ts->ts_first, ts->ts_last,
   1745    1.5    dyoung 	    condstring, rts_retry, data_retry));
   1746  1.120  christos #else
   1747  1.120  christos 	__USE(condstring);
   1748  1.120  christos #endif
   1749    1.5    dyoung }
   1750    1.5    dyoung 
   1751   1.58    dyoung static void
   1752   1.58    dyoung rtw_reset_oactive(struct rtw_softc *sc)
   1753   1.58    dyoung {
   1754   1.58    dyoung 	short oflags;
   1755   1.58    dyoung 	int pri;
   1756   1.58    dyoung 	struct rtw_txsoft_blk *tsb;
   1757   1.58    dyoung 	struct rtw_txdesc_blk *tdb;
   1758   1.58    dyoung 	oflags = sc->sc_if.if_flags;
   1759   1.58    dyoung 	for (pri = 0; pri < RTW_NTXPRI; pri++) {
   1760   1.58    dyoung 		tsb = &sc->sc_txsoft_blk[pri];
   1761   1.58    dyoung 		tdb = &sc->sc_txdesc_blk[pri];
   1762   1.58    dyoung 		if (!SIMPLEQ_EMPTY(&tsb->tsb_freeq) && tdb->tdb_nfree > 0)
   1763   1.58    dyoung 			sc->sc_if.if_flags &= ~IFF_OACTIVE;
   1764   1.58    dyoung 	}
   1765   1.58    dyoung 	if (oflags != sc->sc_if.if_flags) {
   1766   1.58    dyoung 		DPRINTF(sc, RTW_DEBUG_OACTIVE,
   1767   1.58    dyoung 		    ("%s: reset OACTIVE\n", __func__));
   1768   1.58    dyoung 	}
   1769   1.58    dyoung }
   1770   1.58    dyoung 
   1771    1.5    dyoung /* Collect transmitted packets. */
   1772  1.107    dyoung static bool
   1773   1.34    dyoung rtw_collect_txring(struct rtw_softc *sc, struct rtw_txsoft_blk *tsb,
   1774   1.58    dyoung     struct rtw_txdesc_blk *tdb, int force)
   1775    1.5    dyoung {
   1776  1.107    dyoung 	bool collected = false;
   1777    1.5    dyoung 	int ndesc;
   1778   1.34    dyoung 	struct rtw_txsoft *ts;
   1779    1.5    dyoung 
   1780   1.83    dyoung #ifdef RTW_DEBUG
   1781   1.83    dyoung 	rtw_dump_rings(sc);
   1782   1.83    dyoung #endif
   1783   1.83    dyoung 
   1784   1.34    dyoung 	while ((ts = SIMPLEQ_FIRST(&tsb->tsb_dirtyq)) != NULL) {
   1785   1.83    dyoung 		/* If we're clearing a failed transmission, only clear
   1786   1.83    dyoung 		   up to the last packet the hardware has processed.  */
   1787   1.83    dyoung 		if (ts->ts_first == rtw_txring_next(&sc->sc_regs, tdb))
   1788   1.83    dyoung 			break;
   1789   1.83    dyoung 
   1790   1.34    dyoung 		ndesc = 1 + ts->ts_last - ts->ts_first;
   1791   1.34    dyoung 		if (ts->ts_last < ts->ts_first)
   1792   1.34    dyoung 			ndesc += tdb->tdb_ndesc;
   1793    1.5    dyoung 
   1794    1.6    dyoung 		KASSERT(ndesc > 0);
   1795    1.6    dyoung 
   1796   1.34    dyoung 		rtw_txdescs_sync(tdb, ts->ts_first, ndesc,
   1797    1.5    dyoung 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1798    1.5    dyoung 
   1799   1.58    dyoung 		if (force) {
   1800   1.83    dyoung 			int next;
   1801   1.83    dyoung #ifdef RTW_DIAG
   1802   1.83    dyoung 			printf("%s: clearing packet, stats", __func__);
   1803   1.83    dyoung #endif
   1804   1.83    dyoung 			for (next = ts->ts_first; ;
   1805   1.83    dyoung 			    next = RTW_NEXT_IDX(tdb, next)) {
   1806   1.83    dyoung #ifdef RTW_DIAG
   1807   1.83    dyoung 				printf(" %" PRIx32 "/%" PRIx32 "/%" PRIx32 "/%" PRIu32 "/%" PRIx32, le32toh(tdb->tdb_desc[next].td_stat), le32toh(tdb->tdb_desc[next].td_ctl1), le32toh(tdb->tdb_desc[next].td_buf), le32toh(tdb->tdb_desc[next].td_len), le32toh(tdb->tdb_desc[next].td_next));
   1808   1.83    dyoung #endif
   1809   1.83    dyoung 				tdb->tdb_desc[next].td_stat &=
   1810   1.58    dyoung 				    ~htole32(RTW_TXSTAT_OWN);
   1811   1.83    dyoung 				if (next == ts->ts_last)
   1812   1.58    dyoung 					break;
   1813   1.58    dyoung 			}
   1814   1.58    dyoung 			rtw_txdescs_sync(tdb, ts->ts_first, ndesc,
   1815   1.58    dyoung 			    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1816   1.83    dyoung #ifdef RTW_DIAG
   1817   1.83    dyoung 			next = RTW_NEXT_IDX(tdb, next);
   1818   1.83    dyoung 			printf(" -> end %u stat %" PRIx32 ", was %u\n", next,
   1819   1.83    dyoung 			    le32toh(tdb->tdb_desc[next].td_stat),
   1820   1.83    dyoung 			    rtw_txring_next(&sc->sc_regs, tdb));
   1821   1.83    dyoung #endif
   1822   1.58    dyoung 		} else if ((tdb->tdb_desc[ts->ts_last].td_stat &
   1823   1.82    dyoung 		    htole32(RTW_TXSTAT_OWN)) != 0) {
   1824   1.82    dyoung 			rtw_txdescs_sync(tdb, ts->ts_last, 1,
   1825   1.82    dyoung 			    BUS_DMASYNC_PREREAD);
   1826    1.5    dyoung 			break;
   1827   1.82    dyoung 		}
   1828    1.5    dyoung 
   1829  1.107    dyoung 		collected = true;
   1830  1.107    dyoung 
   1831   1.34    dyoung 		rtw_collect_txpkt(sc, tdb, ts, ndesc);
   1832   1.34    dyoung 		SIMPLEQ_REMOVE_HEAD(&tsb->tsb_dirtyq, ts_q);
   1833   1.34    dyoung 		SIMPLEQ_INSERT_TAIL(&tsb->tsb_freeq, ts, ts_q);
   1834    1.5    dyoung 	}
   1835   1.83    dyoung 
   1836   1.74     blymn 	/* no more pending transmissions, cancel watchdog */
   1837   1.34    dyoung 	if (ts == NULL)
   1838   1.34    dyoung 		tsb->tsb_tx_timer = 0;
   1839   1.58    dyoung 	rtw_reset_oactive(sc);
   1840  1.107    dyoung 
   1841  1.107    dyoung 	return collected;
   1842    1.5    dyoung }
   1843    1.5    dyoung 
   1844    1.5    dyoung static void
   1845   1.37    dyoung rtw_intr_tx(struct rtw_softc *sc, uint16_t isr)
   1846    1.1    dyoung {
   1847  1.125    nonaka 	int pri, s;
   1848   1.34    dyoung 	struct rtw_txsoft_blk	*tsb;
   1849   1.34    dyoung 	struct rtw_txdesc_blk	*tdb;
   1850   1.48    dyoung 	struct ifnet *ifp = &sc->sc_if;
   1851    1.5    dyoung 
   1852  1.125    nonaka 	s = splnet();
   1853  1.125    nonaka 
   1854    1.5    dyoung 	for (pri = 0; pri < RTW_NTXPRI; pri++) {
   1855   1.34    dyoung 		tsb = &sc->sc_txsoft_blk[pri];
   1856   1.34    dyoung 		tdb = &sc->sc_txdesc_blk[pri];
   1857   1.58    dyoung 		rtw_collect_txring(sc, tsb, tdb, 0);
   1858   1.58    dyoung 	}
   1859    1.5    dyoung 
   1860   1.58    dyoung 	if ((isr & RTW_INTR_TX) != 0)
   1861  1.126     ozaki 		rtw_start(ifp); /* in softint */
   1862    1.5    dyoung 
   1863  1.125    nonaka 	splx(s);
   1864    1.1    dyoung }
   1865    1.1    dyoung 
   1866    1.1    dyoung static void
   1867   1.37    dyoung rtw_intr_beacon(struct rtw_softc *sc, uint16_t isr)
   1868    1.1    dyoung {
   1869   1.58    dyoung 	u_int next;
   1870   1.58    dyoung 	uint32_t tsfth, tsftl;
   1871   1.58    dyoung 	struct ieee80211com *ic;
   1872   1.58    dyoung 	struct rtw_txdesc_blk *tdb = &sc->sc_txdesc_blk[RTW_TXPRIBCN];
   1873   1.58    dyoung 	struct rtw_txsoft_blk *tsb = &sc->sc_txsoft_blk[RTW_TXPRIBCN];
   1874   1.58    dyoung 	struct mbuf *m;
   1875  1.125    nonaka 	int s;
   1876  1.125    nonaka 
   1877  1.125    nonaka 	s = splnet();
   1878   1.58    dyoung 
   1879   1.58    dyoung 	tsfth = RTW_READ(&sc->sc_regs, RTW_TSFTRH);
   1880   1.58    dyoung 	tsftl = RTW_READ(&sc->sc_regs, RTW_TSFTRL);
   1881   1.58    dyoung 
   1882   1.58    dyoung 	if ((isr & (RTW_INTR_TBDOK|RTW_INTR_TBDER)) != 0) {
   1883   1.58    dyoung 		next = rtw_txring_next(&sc->sc_regs, tdb);
   1884  1.120  christos #ifdef RTW_DEBUG
   1885   1.58    dyoung 		RTW_DPRINTF(RTW_DEBUG_BEACON,
   1886   1.58    dyoung 		    ("%s: beacon ring %sprocessed, isr = %#04" PRIx16
   1887   1.58    dyoung 		     ", next %u expected %u, %" PRIu64 "\n", __func__,
   1888   1.58    dyoung 		     (next == tdb->tdb_next) ? "" : "un", isr, next,
   1889   1.58    dyoung 		     tdb->tdb_next, (uint64_t)tsfth << 32 | tsftl));
   1890  1.120  christos #else
   1891  1.120  christos 		__USE(next);
   1892  1.120  christos 		__USE(tsfth);
   1893  1.120  christos 		__USE(tsftl);
   1894  1.120  christos #endif
   1895   1.83    dyoung 		if ((RTW_READ8(&sc->sc_regs, RTW_TPPOLL) & RTW_TPPOLL_BQ) == 0)
   1896   1.58    dyoung 			rtw_collect_txring(sc, tsb, tdb, 1);
   1897   1.58    dyoung 	}
   1898   1.58    dyoung 	/* Start beacon transmission. */
   1899   1.58    dyoung 
   1900   1.58    dyoung 	if ((isr & RTW_INTR_BCNINT) != 0 &&
   1901   1.58    dyoung 	    sc->sc_ic.ic_state == IEEE80211_S_RUN &&
   1902   1.58    dyoung 	    SIMPLEQ_EMPTY(&tsb->tsb_dirtyq)) {
   1903   1.58    dyoung 		RTW_DPRINTF(RTW_DEBUG_BEACON,
   1904   1.58    dyoung 		    ("%s: beacon prep. time, isr = %#04" PRIx16
   1905   1.58    dyoung 		     ", %16" PRIu64 "\n", __func__, isr,
   1906   1.58    dyoung 		     (uint64_t)tsfth << 32 | tsftl));
   1907   1.58    dyoung 		ic = &sc->sc_ic;
   1908   1.58    dyoung 		m = rtw_beacon_alloc(sc, ic->ic_bss);
   1909   1.58    dyoung 
   1910   1.58    dyoung 		if (m == NULL) {
   1911   1.98    dyoung 			aprint_error_dev(sc->sc_dev,
   1912   1.98    dyoung 			    "could not allocate beacon\n");
   1913  1.125    nonaka 			splx(s);
   1914   1.58    dyoung 			return;
   1915   1.58    dyoung 		}
   1916  1.122     ozaki 		M_SETCTX(m, ieee80211_ref_node(ic->ic_bss));
   1917   1.58    dyoung 		IF_ENQUEUE(&sc->sc_beaconq, m);
   1918  1.126     ozaki 		rtw_start(&sc->sc_if); /* in softint */
   1919   1.58    dyoung 	}
   1920  1.125    nonaka 
   1921  1.125    nonaka 	splx(s);
   1922    1.1    dyoung }
   1923    1.1    dyoung 
   1924    1.1    dyoung static void
   1925   1.81  christos rtw_intr_atim(struct rtw_softc *sc)
   1926    1.1    dyoung {
   1927    1.1    dyoung 	/* TBD */
   1928    1.1    dyoung 	return;
   1929    1.1    dyoung }
   1930    1.1    dyoung 
   1931   1.21    dyoung #ifdef RTW_DEBUG
   1932   1.21    dyoung static void
   1933   1.21    dyoung rtw_dump_rings(struct rtw_softc *sc)
   1934   1.21    dyoung {
   1935   1.34    dyoung 	struct rtw_txdesc_blk *tdb;
   1936   1.34    dyoung 	struct rtw_rxdesc *rd;
   1937   1.34    dyoung 	struct rtw_rxdesc_blk *rdb;
   1938   1.21    dyoung 	int desc, pri;
   1939   1.21    dyoung 
   1940   1.21    dyoung 	if ((rtw_debug & RTW_DEBUG_IO_KICK) == 0)
   1941   1.21    dyoung 		return;
   1942   1.21    dyoung 
   1943   1.21    dyoung 	for (pri = 0; pri < RTW_NTXPRI; pri++) {
   1944   1.34    dyoung 		tdb = &sc->sc_txdesc_blk[pri];
   1945   1.21    dyoung 		printf("%s: txpri %d ndesc %d nfree %d\n", __func__, pri,
   1946   1.34    dyoung 		    tdb->tdb_ndesc, tdb->tdb_nfree);
   1947   1.34    dyoung 		for (desc = 0; desc < tdb->tdb_ndesc; desc++)
   1948   1.34    dyoung 			rtw_print_txdesc(sc, ".", NULL, tdb, desc);
   1949   1.21    dyoung 	}
   1950   1.21    dyoung 
   1951   1.34    dyoung 	rdb = &sc->sc_rxdesc_blk;
   1952   1.33    dyoung 
   1953   1.21    dyoung 	for (desc = 0; desc < RTW_RXQLEN; desc++) {
   1954   1.34    dyoung 		rd = &rdb->rdb_desc[desc];
   1955   1.31    dyoung 		printf("%s: %sctl %08x rsvd0/rssi %08x buf/tsftl %08x "
   1956   1.21    dyoung 		    "rsvd1/tsfth %08x\n", __func__,
   1957   1.34    dyoung 		    (desc >= rdb->rdb_ndesc) ? "UNUSED " : "",
   1958   1.34    dyoung 		    le32toh(rd->rd_ctl), le32toh(rd->rd_rssi),
   1959   1.34    dyoung 		    le32toh(rd->rd_buf), le32toh(rd->rd_tsfth));
   1960   1.21    dyoung 	}
   1961   1.21    dyoung }
   1962   1.21    dyoung #endif /* RTW_DEBUG */
   1963   1.21    dyoung 
   1964    1.1    dyoung static void
   1965    1.3    dyoung rtw_hwring_setup(struct rtw_softc *sc)
   1966    1.3    dyoung {
   1967   1.58    dyoung 	int pri;
   1968    1.3    dyoung 	struct rtw_regs *regs = &sc->sc_regs;
   1969   1.58    dyoung 	struct rtw_txdesc_blk *tdb;
   1970   1.58    dyoung 
   1971   1.58    dyoung 	sc->sc_txdesc_blk[RTW_TXPRILO].tdb_basereg = RTW_TLPDA;
   1972   1.58    dyoung 	sc->sc_txdesc_blk[RTW_TXPRILO].tdb_base = RTW_RING_BASE(sc, hd_txlo);
   1973   1.58    dyoung 	sc->sc_txdesc_blk[RTW_TXPRIMD].tdb_basereg = RTW_TNPDA;
   1974   1.58    dyoung 	sc->sc_txdesc_blk[RTW_TXPRIMD].tdb_base = RTW_RING_BASE(sc, hd_txmd);
   1975   1.58    dyoung 	sc->sc_txdesc_blk[RTW_TXPRIHI].tdb_basereg = RTW_THPDA;
   1976   1.58    dyoung 	sc->sc_txdesc_blk[RTW_TXPRIHI].tdb_base = RTW_RING_BASE(sc, hd_txhi);
   1977   1.58    dyoung 	sc->sc_txdesc_blk[RTW_TXPRIBCN].tdb_basereg = RTW_TBDA;
   1978   1.58    dyoung 	sc->sc_txdesc_blk[RTW_TXPRIBCN].tdb_base = RTW_RING_BASE(sc, hd_bcn);
   1979   1.58    dyoung 
   1980   1.58    dyoung 	for (pri = 0; pri < RTW_NTXPRI; pri++) {
   1981   1.58    dyoung 		tdb = &sc->sc_txdesc_blk[pri];
   1982   1.58    dyoung 		RTW_WRITE(regs, tdb->tdb_basereg, tdb->tdb_base);
   1983   1.58    dyoung 		RTW_DPRINTF(RTW_DEBUG_XMIT_DESC,
   1984   1.58    dyoung 		    ("%s: reg[tdb->tdb_basereg] <- %" PRIxPTR "\n", __func__,
   1985   1.58    dyoung 		     (uintptr_t)tdb->tdb_base));
   1986   1.58    dyoung 	}
   1987   1.58    dyoung 
   1988    1.3    dyoung 	RTW_WRITE(regs, RTW_RDSAR, RTW_RING_BASE(sc, hd_rx));
   1989   1.58    dyoung 
   1990   1.21    dyoung 	RTW_DPRINTF(RTW_DEBUG_RECV_DESC,
   1991   1.21    dyoung 	    ("%s: reg[RDSAR] <- %" PRIxPTR "\n", __func__,
   1992   1.21    dyoung 	     (uintptr_t)RTW_RING_BASE(sc, hd_rx)));
   1993   1.58    dyoung 
   1994   1.58    dyoung 	RTW_SYNC(regs, RTW_TLPDA, RTW_RDSAR);
   1995   1.58    dyoung 
   1996    1.3    dyoung }
   1997    1.3    dyoung 
   1998   1.31    dyoung static int
   1999    1.3    dyoung rtw_swring_setup(struct rtw_softc *sc)
   2000    1.3    dyoung {
   2001   1.31    dyoung 	int rc;
   2002   1.34    dyoung 	struct rtw_rxdesc_blk *rdb;
   2003   1.33    dyoung 
   2004    1.3    dyoung 	rtw_txdesc_blk_init_all(&sc->sc_txdesc_blk[0]);
   2005    1.3    dyoung 
   2006   1.34    dyoung 	rtw_txsoft_blk_init_all(&sc->sc_txsoft_blk[0]);
   2007    1.3    dyoung 
   2008   1.34    dyoung 	rdb = &sc->sc_rxdesc_blk;
   2009   1.34    dyoung 	if ((rc = rtw_rxsoft_init_all(sc->sc_dmat, sc->sc_rxsoft, &rdb->rdb_ndesc,
   2010   1.98    dyoung 	     sc->sc_dev)) != 0 && rdb->rdb_ndesc == 0) {
   2011   1.98    dyoung 		aprint_error_dev(sc->sc_dev, "could not allocate rx buffers\n");
   2012   1.31    dyoung 		return rc;
   2013   1.31    dyoung 	}
   2014   1.44     perry 
   2015   1.34    dyoung 	rdb = &sc->sc_rxdesc_blk;
   2016   1.34    dyoung 	rtw_rxdescs_sync(rdb, 0, rdb->rdb_ndesc,
   2017   1.31    dyoung 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   2018   1.34    dyoung 	rtw_rxdesc_init_all(rdb, sc->sc_rxsoft, 1);
   2019   1.58    dyoung 	rdb->rdb_next = 0;
   2020    1.3    dyoung 
   2021   1.33    dyoung 	rtw_txdescs_sync_all(&sc->sc_txdesc_blk[0]);
   2022   1.31    dyoung 	return 0;
   2023    1.3    dyoung }
   2024    1.3    dyoung 
   2025    1.3    dyoung static void
   2026   1.58    dyoung rtw_txdesc_blk_init(struct rtw_txdesc_blk *tdb)
   2027   1.21    dyoung {
   2028   1.21    dyoung 	int i;
   2029   1.21    dyoung 
   2030   1.34    dyoung 	(void)memset(tdb->tdb_desc, 0,
   2031   1.34    dyoung 	    sizeof(tdb->tdb_desc[0]) * tdb->tdb_ndesc);
   2032   1.34    dyoung 	for (i = 0; i < tdb->tdb_ndesc; i++)
   2033   1.34    dyoung 		tdb->tdb_desc[i].td_next = htole32(RTW_NEXT_DESC(tdb, i));
   2034   1.58    dyoung }
   2035   1.58    dyoung 
   2036   1.58    dyoung static u_int
   2037   1.58    dyoung rtw_txring_next(struct rtw_regs *regs, struct rtw_txdesc_blk *tdb)
   2038   1.58    dyoung {
   2039   1.58    dyoung 	return (le32toh(RTW_READ(regs, tdb->tdb_basereg)) - tdb->tdb_base) /
   2040   1.58    dyoung 	    sizeof(struct rtw_txdesc);
   2041   1.21    dyoung }
   2042   1.21    dyoung 
   2043   1.83    dyoung #ifdef RTW_DIAG
   2044   1.21    dyoung static void
   2045   1.83    dyoung rtw_txring_fixup(struct rtw_softc *sc, const char *fn, int ln)
   2046    1.3    dyoung {
   2047    1.5    dyoung 	int pri;
   2048   1.58    dyoung 	u_int next;
   2049   1.34    dyoung 	struct rtw_txdesc_blk *tdb;
   2050   1.58    dyoung 	struct rtw_regs *regs = &sc->sc_regs;
   2051   1.21    dyoung 
   2052   1.21    dyoung 	for (pri = 0; pri < RTW_NTXPRI; pri++) {
   2053   1.83    dyoung 		int i;
   2054   1.34    dyoung 		tdb = &sc->sc_txdesc_blk[pri];
   2055   1.58    dyoung 		next = rtw_txring_next(regs, tdb);
   2056   1.58    dyoung 		if (tdb->tdb_next == next)
   2057   1.58    dyoung 			continue;
   2058   1.83    dyoung 		for (i = 0; next != tdb->tdb_next;
   2059   1.83    dyoung 		    next = RTW_NEXT_IDX(tdb, next), i++) {
   2060   1.83    dyoung 			if ((tdb->tdb_desc[next].td_stat & htole32(RTW_TXSTAT_OWN)) == 0)
   2061   1.83    dyoung 				break;
   2062   1.83    dyoung 		}
   2063   1.83    dyoung 		printf("%s:%d: tx-ring %d expected next %u, read %u+%d -> %s\n", fn,
   2064   1.83    dyoung 		    ln, pri, tdb->tdb_next, next, i, tdb->tdb_next == next ? "okay" : "BAD");
   2065   1.83    dyoung 		if (tdb->tdb_next == next)
   2066   1.83    dyoung 			continue;
   2067   1.65    dyoung 		tdb->tdb_next = MIN(next, tdb->tdb_ndesc - 1);
   2068   1.58    dyoung 	}
   2069   1.58    dyoung }
   2070   1.83    dyoung #endif
   2071   1.21    dyoung 
   2072   1.21    dyoung static void
   2073   1.58    dyoung rtw_txdescs_reset(struct rtw_softc *sc)
   2074   1.21    dyoung {
   2075   1.58    dyoung 	int pri;
   2076   1.83    dyoung 	struct rtw_txsoft_blk	*tsb;
   2077   1.83    dyoung 	struct rtw_txdesc_blk	*tdb;
   2078   1.58    dyoung 
   2079   1.58    dyoung 	for (pri = 0; pri < RTW_NTXPRI; pri++) {
   2080   1.83    dyoung 		tsb = &sc->sc_txsoft_blk[pri];
   2081   1.83    dyoung 		tdb = &sc->sc_txdesc_blk[pri];
   2082   1.83    dyoung 		rtw_collect_txring(sc, tsb, tdb, 1);
   2083   1.83    dyoung #ifdef RTW_DIAG
   2084   1.83    dyoung 		if (!SIMPLEQ_EMPTY(&tsb->tsb_dirtyq))
   2085   1.83    dyoung 			printf("%s: packets left in ring %d\n", __func__, pri);
   2086   1.83    dyoung #endif
   2087   1.58    dyoung 	}
   2088   1.21    dyoung }
   2089   1.21    dyoung 
   2090   1.21    dyoung static void
   2091   1.21    dyoung rtw_intr_ioerror(struct rtw_softc *sc, uint16_t isr)
   2092   1.21    dyoung {
   2093  1.125    nonaka 	int s;
   2094  1.125    nonaka 
   2095   1.98    dyoung 	aprint_error_dev(sc->sc_dev, "tx fifo underflow\n");
   2096    1.5    dyoung 
   2097   1.83    dyoung 	RTW_DPRINTF(RTW_DEBUG_BUGS, ("%s: cleaning up xmit, isr %" PRIx16
   2098   1.98    dyoung 	    "\n", device_xname(sc->sc_dev), isr));
   2099   1.15    dyoung 
   2100  1.125    nonaka 	s = splnet();
   2101  1.125    nonaka 
   2102   1.24    dyoung #ifdef RTW_DEBUG
   2103   1.21    dyoung 	rtw_dump_rings(sc);
   2104   1.24    dyoung #endif /* RTW_DEBUG */
   2105   1.15    dyoung 
   2106   1.58    dyoung 	/* Collect tx'd packets.  XXX let's hope this stops the transmit
   2107   1.58    dyoung 	 * timeouts.
   2108   1.58    dyoung 	 */
   2109   1.83    dyoung 	rtw_txdescs_reset(sc);
   2110   1.21    dyoung 
   2111   1.24    dyoung #ifdef RTW_DEBUG
   2112   1.21    dyoung 	rtw_dump_rings(sc);
   2113   1.24    dyoung #endif /* RTW_DEBUG */
   2114  1.125    nonaka 
   2115  1.125    nonaka 	splx(s);
   2116    1.3    dyoung }
   2117    1.3    dyoung 
   2118   1.61     perry static inline void
   2119    1.1    dyoung rtw_suspend_ticks(struct rtw_softc *sc)
   2120    1.1    dyoung {
   2121   1.21    dyoung 	RTW_DPRINTF(RTW_DEBUG_TIMEOUT,
   2122   1.98    dyoung 	    ("%s: suspending ticks\n", device_xname(sc->sc_dev)));
   2123    1.1    dyoung 	sc->sc_do_tick = 0;
   2124    1.1    dyoung }
   2125    1.1    dyoung 
   2126   1.61     perry static inline void
   2127    1.1    dyoung rtw_resume_ticks(struct rtw_softc *sc)
   2128    1.1    dyoung {
   2129  1.115    dyoung 	uint32_t tsftrl0, tsftrl1, next_tint;
   2130    1.1    dyoung 
   2131    1.1    dyoung 	tsftrl0 = RTW_READ(&sc->sc_regs, RTW_TSFTRL);
   2132    1.1    dyoung 
   2133    1.1    dyoung 	tsftrl1 = RTW_READ(&sc->sc_regs, RTW_TSFTRL);
   2134  1.115    dyoung 	next_tint = tsftrl1 + 1000000;
   2135  1.115    dyoung 	RTW_WRITE(&sc->sc_regs, RTW_TINT, next_tint);
   2136    1.1    dyoung 
   2137    1.1    dyoung 	sc->sc_do_tick = 1;
   2138    1.1    dyoung 
   2139  1.120  christos #ifdef RTW_DEBUG
   2140   1.21    dyoung 	RTW_DPRINTF(RTW_DEBUG_TIMEOUT,
   2141   1.21    dyoung 	    ("%s: resume ticks delta %#08x now %#08x next %#08x\n",
   2142  1.115    dyoung 	    device_xname(sc->sc_dev), tsftrl1 - tsftrl0, tsftrl1, next_tint));
   2143  1.120  christos #else
   2144  1.120  christos 	__USE(tsftrl0);
   2145  1.120  christos #endif
   2146    1.1    dyoung }
   2147    1.1    dyoung 
   2148    1.1    dyoung static void
   2149    1.1    dyoung rtw_intr_timeout(struct rtw_softc *sc)
   2150    1.1    dyoung {
   2151  1.125    nonaka 	int s;
   2152  1.125    nonaka 
   2153  1.125    nonaka 	s = splnet();
   2154   1.98    dyoung 	RTW_DPRINTF(RTW_DEBUG_TIMEOUT, ("%s: timeout\n", device_xname(sc->sc_dev)));
   2155    1.1    dyoung 	if (sc->sc_do_tick)
   2156    1.1    dyoung 		rtw_resume_ticks(sc);
   2157  1.125    nonaka 	splx(s);
   2158    1.1    dyoung }
   2159    1.1    dyoung 
   2160    1.1    dyoung int
   2161    1.1    dyoung rtw_intr(void *arg)
   2162    1.1    dyoung {
   2163    1.1    dyoung 	struct rtw_softc *sc = arg;
   2164    1.1    dyoung 	struct rtw_regs *regs = &sc->sc_regs;
   2165   1.37    dyoung 	uint16_t isr;
   2166   1.48    dyoung 	struct ifnet *ifp = &sc->sc_if;
   2167    1.1    dyoung 
   2168    1.1    dyoung 	/*
   2169    1.1    dyoung 	 * If the interface isn't running, the interrupt couldn't
   2170    1.1    dyoung 	 * possibly have come from us.
   2171    1.1    dyoung 	 */
   2172  1.101    dyoung 	if ((ifp->if_flags & IFF_RUNNING) == 0 ||
   2173  1.109    dyoung 	    !device_activation(sc->sc_dev, DEVACT_LEVEL_DRIVER)) {
   2174   1.98    dyoung 		RTW_DPRINTF(RTW_DEBUG_INTR, ("%s: stray interrupt\n",
   2175   1.98    dyoung 		    device_xname(sc->sc_dev)));
   2176    1.1    dyoung 		return (0);
   2177    1.1    dyoung 	}
   2178    1.1    dyoung 
   2179  1.125    nonaka 	isr = RTW_READ16(regs, RTW_ISR);
   2180  1.125    nonaka 	if (isr == 0)
   2181  1.125    nonaka 		return (0);
   2182  1.125    nonaka 
   2183  1.125    nonaka 	/* Disable interrupts. */
   2184  1.125    nonaka 	RTW_WRITE16(regs, RTW_IMR, 0);
   2185  1.125    nonaka 	RTW_WBW(regs, RTW_IMR, RTW_IMR);
   2186  1.125    nonaka 
   2187  1.125    nonaka 	softint_schedule(sc->sc_soft_ih);
   2188  1.125    nonaka 	return (1);
   2189  1.125    nonaka }
   2190  1.125    nonaka 
   2191  1.125    nonaka static void
   2192  1.125    nonaka rtw_softintr(void *arg)
   2193  1.125    nonaka {
   2194  1.125    nonaka 	int i;
   2195  1.125    nonaka 	struct rtw_softc *sc = arg;
   2196  1.125    nonaka 	struct rtw_regs *regs = &sc->sc_regs;
   2197  1.125    nonaka 	uint16_t isr;
   2198  1.125    nonaka 	struct ifnet *ifp = &sc->sc_if;
   2199  1.125    nonaka 
   2200  1.125    nonaka 	if ((ifp->if_flags & IFF_RUNNING) == 0 ||
   2201  1.125    nonaka 	    !device_activation(sc->sc_dev, DEVACT_LEVEL_DRIVER)) {
   2202  1.125    nonaka 		RTW_DPRINTF(RTW_DEBUG_INTR, ("%s: stray interrupt\n",
   2203  1.125    nonaka 		    device_xname(sc->sc_dev)));
   2204  1.125    nonaka 		return;
   2205  1.125    nonaka 	}
   2206  1.125    nonaka 
   2207    1.3    dyoung 	for (i = 0; i < 10; i++) {
   2208    1.1    dyoung 		isr = RTW_READ16(regs, RTW_ISR);
   2209    1.1    dyoung 
   2210    1.1    dyoung 		RTW_WRITE16(regs, RTW_ISR, isr);
   2211    1.8    dyoung 		RTW_WBR(regs, RTW_ISR, RTW_ISR);
   2212    1.1    dyoung 
   2213    1.1    dyoung 		if (sc->sc_intr_ack != NULL)
   2214    1.1    dyoung 			(*sc->sc_intr_ack)(regs);
   2215    1.1    dyoung 
   2216    1.1    dyoung 		if (isr == 0)
   2217    1.1    dyoung 			break;
   2218    1.1    dyoung 
   2219    1.1    dyoung #ifdef RTW_DEBUG
   2220    1.1    dyoung #define PRINTINTR(flag) do { \
   2221    1.1    dyoung 	if ((isr & flag) != 0) { \
   2222    1.1    dyoung 		printf("%s" #flag, delim); \
   2223    1.1    dyoung 		delim = ","; \
   2224    1.1    dyoung 	} \
   2225    1.1    dyoung } while (0)
   2226    1.1    dyoung 
   2227   1.21    dyoung 		if ((rtw_debug & RTW_DEBUG_INTR) != 0 && isr != 0) {
   2228    1.1    dyoung 			const char *delim = "<";
   2229    1.1    dyoung 
   2230   1.98    dyoung 			printf("%s: reg[ISR] = %x", device_xname(sc->sc_dev),
   2231   1.98    dyoung 			    isr);
   2232    1.1    dyoung 
   2233    1.1    dyoung 			PRINTINTR(RTW_INTR_TXFOVW);
   2234    1.1    dyoung 			PRINTINTR(RTW_INTR_TIMEOUT);
   2235    1.1    dyoung 			PRINTINTR(RTW_INTR_BCNINT);
   2236    1.1    dyoung 			PRINTINTR(RTW_INTR_ATIMINT);
   2237    1.1    dyoung 			PRINTINTR(RTW_INTR_TBDER);
   2238    1.1    dyoung 			PRINTINTR(RTW_INTR_TBDOK);
   2239    1.1    dyoung 			PRINTINTR(RTW_INTR_THPDER);
   2240    1.1    dyoung 			PRINTINTR(RTW_INTR_THPDOK);
   2241    1.1    dyoung 			PRINTINTR(RTW_INTR_TNPDER);
   2242    1.1    dyoung 			PRINTINTR(RTW_INTR_TNPDOK);
   2243    1.1    dyoung 			PRINTINTR(RTW_INTR_RXFOVW);
   2244    1.1    dyoung 			PRINTINTR(RTW_INTR_RDU);
   2245    1.1    dyoung 			PRINTINTR(RTW_INTR_TLPDER);
   2246    1.1    dyoung 			PRINTINTR(RTW_INTR_TLPDOK);
   2247    1.1    dyoung 			PRINTINTR(RTW_INTR_RER);
   2248    1.1    dyoung 			PRINTINTR(RTW_INTR_ROK);
   2249    1.1    dyoung 
   2250    1.1    dyoung 			printf(">\n");
   2251    1.1    dyoung 		}
   2252    1.1    dyoung #undef PRINTINTR
   2253    1.1    dyoung #endif /* RTW_DEBUG */
   2254    1.1    dyoung 
   2255    1.1    dyoung 		if ((isr & RTW_INTR_RX) != 0)
   2256   1.83    dyoung 			rtw_intr_rx(sc, isr);
   2257    1.1    dyoung 		if ((isr & RTW_INTR_TX) != 0)
   2258   1.83    dyoung 			rtw_intr_tx(sc, isr);
   2259    1.1    dyoung 		if ((isr & RTW_INTR_BEACON) != 0)
   2260   1.83    dyoung 			rtw_intr_beacon(sc, isr);
   2261    1.1    dyoung 		if ((isr & RTW_INTR_ATIMINT) != 0)
   2262    1.1    dyoung 			rtw_intr_atim(sc);
   2263    1.1    dyoung 		if ((isr & RTW_INTR_IOERROR) != 0)
   2264   1.83    dyoung 			rtw_intr_ioerror(sc, isr);
   2265    1.1    dyoung 		if ((isr & RTW_INTR_TIMEOUT) != 0)
   2266    1.1    dyoung 			rtw_intr_timeout(sc);
   2267    1.1    dyoung 	}
   2268  1.125    nonaka 	if (i == 10)
   2269  1.125    nonaka 		softint_schedule(sc->sc_soft_ih);
   2270    1.1    dyoung 
   2271  1.125    nonaka 	/* Re-enable interrupts */
   2272  1.125    nonaka 	RTW_WRITE16(regs, RTW_IMR, sc->sc_inten);
   2273  1.125    nonaka 	RTW_WBW(regs, RTW_IMR, RTW_IMR);
   2274    1.1    dyoung }
   2275    1.1    dyoung 
   2276   1.21    dyoung /* Must be called at splnet. */
   2277    1.1    dyoung static void
   2278    1.1    dyoung rtw_stop(struct ifnet *ifp, int disable)
   2279    1.1    dyoung {
   2280   1.21    dyoung 	int pri;
   2281    1.1    dyoung 	struct rtw_softc *sc = (struct rtw_softc *)ifp->if_softc;
   2282    1.1    dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   2283    1.1    dyoung 	struct rtw_regs *regs = &sc->sc_regs;
   2284    1.1    dyoung 
   2285    1.1    dyoung 	rtw_suspend_ticks(sc);
   2286    1.1    dyoung 
   2287    1.1    dyoung 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
   2288    1.1    dyoung 
   2289  1.101    dyoung 	if (device_has_power(sc->sc_dev)) {
   2290    1.3    dyoung 		/* Disable interrupts. */
   2291    1.3    dyoung 		RTW_WRITE16(regs, RTW_IMR, 0);
   2292    1.3    dyoung 
   2293    1.8    dyoung 		RTW_WBW(regs, RTW_TPPOLL, RTW_IMR);
   2294    1.8    dyoung 
   2295    1.3    dyoung 		/* Stop the transmit and receive processes. First stop DMA,
   2296    1.3    dyoung 		 * then disable receiver and transmitter.
   2297    1.3    dyoung 		 */
   2298   1.42    dyoung 		RTW_WRITE8(regs, RTW_TPPOLL, RTW_TPPOLL_SALL);
   2299    1.1    dyoung 
   2300    1.8    dyoung 		RTW_SYNC(regs, RTW_TPPOLL, RTW_IMR);
   2301    1.8    dyoung 
   2302   1.83    dyoung 		rtw_io_enable(sc, RTW_CR_RE | RTW_CR_TE, 0);
   2303    1.3    dyoung 	}
   2304    1.1    dyoung 
   2305    1.5    dyoung 	for (pri = 0; pri < RTW_NTXPRI; pri++) {
   2306   1.34    dyoung 		rtw_txsofts_release(sc->sc_dmat, &sc->sc_ic,
   2307   1.34    dyoung 		    &sc->sc_txsoft_blk[pri]);
   2308    1.5    dyoung 	}
   2309    1.1    dyoung 
   2310   1.34    dyoung 	rtw_rxbufs_release(sc->sc_dmat, &sc->sc_rxsoft[0]);
   2311   1.31    dyoung 
   2312    1.1    dyoung 	/* Mark the interface as not running.  Cancel the watchdog timer. */
   2313   1.21    dyoung 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   2314    1.1    dyoung 	ifp->if_timer = 0;
   2315    1.3    dyoung 
   2316  1.101    dyoung 	if (disable)
   2317  1.109    dyoung 		pmf_device_suspend(sc->sc_dev, &sc->sc_qual);
   2318  1.101    dyoung 
   2319    1.1    dyoung 	return;
   2320    1.1    dyoung }
   2321    1.1    dyoung 
   2322    1.1    dyoung const char *
   2323    1.1    dyoung rtw_pwrstate_string(enum rtw_pwrstate power)
   2324    1.1    dyoung {
   2325    1.1    dyoung 	switch (power) {
   2326    1.1    dyoung 	case RTW_ON:
   2327    1.1    dyoung 		return "on";
   2328    1.1    dyoung 	case RTW_SLEEP:
   2329    1.1    dyoung 		return "sleep";
   2330    1.1    dyoung 	case RTW_OFF:
   2331    1.1    dyoung 		return "off";
   2332    1.1    dyoung 	default:
   2333    1.1    dyoung 		return "unknown";
   2334    1.1    dyoung 	}
   2335    1.1    dyoung }
   2336    1.1    dyoung 
   2337   1.10    dyoung /* XXX For Maxim, I am using the RFMD settings gleaned from the
   2338   1.10    dyoung  * reference driver, plus a magic Maxim "ON" value that comes from
   2339   1.10    dyoung  * the Realtek document "Windows PG for Rtl8180."
   2340    1.1    dyoung  */
   2341    1.1    dyoung static void
   2342    1.1    dyoung rtw_maxim_pwrstate(struct rtw_regs *regs, enum rtw_pwrstate power,
   2343   1.81  christos     int before_rf, int digphy)
   2344    1.1    dyoung {
   2345   1.37    dyoung 	uint32_t anaparm;
   2346    1.1    dyoung 
   2347   1.10    dyoung 	anaparm = RTW_READ(regs, RTW_ANAPARM);
   2348   1.10    dyoung 	anaparm &= ~(RTW_ANAPARM_RFPOW_MASK | RTW_ANAPARM_TXDACOFF);
   2349   1.10    dyoung 
   2350   1.10    dyoung 	switch (power) {
   2351   1.10    dyoung 	case RTW_OFF:
   2352   1.10    dyoung 		if (before_rf)
   2353   1.10    dyoung 			return;
   2354   1.10    dyoung 		anaparm |= RTW_ANAPARM_RFPOW_MAXIM_OFF;
   2355   1.10    dyoung 		anaparm |= RTW_ANAPARM_TXDACOFF;
   2356   1.10    dyoung 		break;
   2357   1.10    dyoung 	case RTW_SLEEP:
   2358   1.10    dyoung 		if (!before_rf)
   2359   1.10    dyoung 			return;
   2360   1.10    dyoung 		anaparm |= RTW_ANAPARM_RFPOW_MAXIM_SLEEP;
   2361   1.10    dyoung 		anaparm |= RTW_ANAPARM_TXDACOFF;
   2362   1.10    dyoung 		break;
   2363   1.10    dyoung 	case RTW_ON:
   2364   1.10    dyoung 		if (!before_rf)
   2365   1.10    dyoung 			return;
   2366   1.10    dyoung 		anaparm |= RTW_ANAPARM_RFPOW_MAXIM_ON;
   2367   1.10    dyoung 		break;
   2368   1.10    dyoung 	}
   2369   1.21    dyoung 	RTW_DPRINTF(RTW_DEBUG_PWR,
   2370   1.21    dyoung 	    ("%s: power state %s, %s RF, reg[ANAPARM] <- %08x\n",
   2371   1.10    dyoung 	    __func__, rtw_pwrstate_string(power),
   2372   1.10    dyoung 	    (before_rf) ? "before" : "after", anaparm));
   2373   1.10    dyoung 
   2374   1.10    dyoung 	RTW_WRITE(regs, RTW_ANAPARM, anaparm);
   2375   1.10    dyoung 	RTW_SYNC(regs, RTW_ANAPARM, RTW_ANAPARM);
   2376   1.10    dyoung }
   2377   1.10    dyoung 
   2378   1.10    dyoung /* XXX I am using the RFMD settings gleaned from the reference
   2379   1.44     perry  * driver.  They agree
   2380   1.10    dyoung  */
   2381   1.10    dyoung static void
   2382   1.10    dyoung rtw_rfmd_pwrstate(struct rtw_regs *regs, enum rtw_pwrstate power,
   2383   1.81  christos     int before_rf, int digphy)
   2384   1.10    dyoung {
   2385   1.37    dyoung 	uint32_t anaparm;
   2386    1.1    dyoung 
   2387    1.1    dyoung 	anaparm = RTW_READ(regs, RTW_ANAPARM);
   2388   1.10    dyoung 	anaparm &= ~(RTW_ANAPARM_RFPOW_MASK | RTW_ANAPARM_TXDACOFF);
   2389    1.1    dyoung 
   2390    1.1    dyoung 	switch (power) {
   2391    1.1    dyoung 	case RTW_OFF:
   2392    1.1    dyoung 		if (before_rf)
   2393    1.1    dyoung 			return;
   2394   1.10    dyoung 		anaparm |= RTW_ANAPARM_RFPOW_RFMD_OFF;
   2395    1.1    dyoung 		anaparm |= RTW_ANAPARM_TXDACOFF;
   2396    1.1    dyoung 		break;
   2397    1.1    dyoung 	case RTW_SLEEP:
   2398    1.1    dyoung 		if (!before_rf)
   2399    1.1    dyoung 			return;
   2400   1.10    dyoung 		anaparm |= RTW_ANAPARM_RFPOW_RFMD_SLEEP;
   2401    1.1    dyoung 		anaparm |= RTW_ANAPARM_TXDACOFF;
   2402    1.1    dyoung 		break;
   2403    1.1    dyoung 	case RTW_ON:
   2404    1.1    dyoung 		if (!before_rf)
   2405    1.1    dyoung 			return;
   2406   1.10    dyoung 		anaparm |= RTW_ANAPARM_RFPOW_RFMD_ON;
   2407    1.1    dyoung 		break;
   2408    1.1    dyoung 	}
   2409   1.21    dyoung 	RTW_DPRINTF(RTW_DEBUG_PWR,
   2410   1.21    dyoung 	    ("%s: power state %s, %s RF, reg[ANAPARM] <- %08x\n",
   2411   1.10    dyoung 	    __func__, rtw_pwrstate_string(power),
   2412   1.10    dyoung 	    (before_rf) ? "before" : "after", anaparm));
   2413   1.10    dyoung 
   2414    1.1    dyoung 	RTW_WRITE(regs, RTW_ANAPARM, anaparm);
   2415    1.1    dyoung 	RTW_SYNC(regs, RTW_ANAPARM, RTW_ANAPARM);
   2416    1.1    dyoung }
   2417    1.1    dyoung 
   2418    1.1    dyoung static void
   2419    1.1    dyoung rtw_philips_pwrstate(struct rtw_regs *regs, enum rtw_pwrstate power,
   2420   1.10    dyoung     int before_rf, int digphy)
   2421    1.1    dyoung {
   2422   1.37    dyoung 	uint32_t anaparm;
   2423    1.1    dyoung 
   2424    1.1    dyoung 	anaparm = RTW_READ(regs, RTW_ANAPARM);
   2425   1.10    dyoung 	anaparm &= ~(RTW_ANAPARM_RFPOW_MASK | RTW_ANAPARM_TXDACOFF);
   2426    1.1    dyoung 
   2427    1.1    dyoung 	switch (power) {
   2428    1.1    dyoung 	case RTW_OFF:
   2429    1.1    dyoung 		if (before_rf)
   2430    1.1    dyoung 			return;
   2431   1.10    dyoung 		anaparm |= RTW_ANAPARM_RFPOW_PHILIPS_OFF;
   2432    1.1    dyoung 		anaparm |= RTW_ANAPARM_TXDACOFF;
   2433    1.1    dyoung 		break;
   2434    1.1    dyoung 	case RTW_SLEEP:
   2435    1.1    dyoung 		if (!before_rf)
   2436    1.1    dyoung 			return;
   2437   1.10    dyoung 		anaparm |= RTW_ANAPARM_RFPOW_PHILIPS_SLEEP;
   2438    1.1    dyoung 		anaparm |= RTW_ANAPARM_TXDACOFF;
   2439    1.1    dyoung 		break;
   2440    1.1    dyoung 	case RTW_ON:
   2441    1.1    dyoung 		if (!before_rf)
   2442    1.1    dyoung 			return;
   2443   1.10    dyoung 		if (digphy) {
   2444   1.10    dyoung 			anaparm |= RTW_ANAPARM_RFPOW_DIG_PHILIPS_ON;
   2445   1.10    dyoung 			/* XXX guess */
   2446   1.10    dyoung 			anaparm |= RTW_ANAPARM_TXDACOFF;
   2447   1.10    dyoung 		} else
   2448   1.10    dyoung 			anaparm |= RTW_ANAPARM_RFPOW_ANA_PHILIPS_ON;
   2449    1.1    dyoung 		break;
   2450    1.1    dyoung 	}
   2451   1.21    dyoung 	RTW_DPRINTF(RTW_DEBUG_PWR,
   2452   1.21    dyoung 	    ("%s: power state %s, %s RF, reg[ANAPARM] <- %08x\n",
   2453   1.10    dyoung 	    __func__, rtw_pwrstate_string(power),
   2454   1.10    dyoung 	    (before_rf) ? "before" : "after", anaparm));
   2455   1.10    dyoung 
   2456    1.1    dyoung 	RTW_WRITE(regs, RTW_ANAPARM, anaparm);
   2457    1.1    dyoung 	RTW_SYNC(regs, RTW_ANAPARM, RTW_ANAPARM);
   2458    1.1    dyoung }
   2459    1.1    dyoung 
   2460    1.1    dyoung static void
   2461   1.10    dyoung rtw_pwrstate0(struct rtw_softc *sc, enum rtw_pwrstate power, int before_rf,
   2462   1.10    dyoung     int digphy)
   2463    1.1    dyoung {
   2464    1.1    dyoung 	struct rtw_regs *regs = &sc->sc_regs;
   2465    1.1    dyoung 
   2466   1.42    dyoung 	rtw_set_access(regs, RTW_ACCESS_ANAPARM);
   2467    1.1    dyoung 
   2468   1.10    dyoung 	(*sc->sc_pwrstate_cb)(regs, power, before_rf, digphy);
   2469    1.1    dyoung 
   2470   1.42    dyoung 	rtw_set_access(regs, RTW_ACCESS_NONE);
   2471    1.1    dyoung 
   2472    1.1    dyoung 	return;
   2473    1.1    dyoung }
   2474    1.1    dyoung 
   2475    1.1    dyoung static int
   2476    1.1    dyoung rtw_pwrstate(struct rtw_softc *sc, enum rtw_pwrstate power)
   2477    1.1    dyoung {
   2478    1.1    dyoung 	int rc;
   2479    1.1    dyoung 
   2480   1.21    dyoung 	RTW_DPRINTF(RTW_DEBUG_PWR,
   2481   1.21    dyoung 	    ("%s: %s->%s\n", __func__,
   2482    1.1    dyoung 	    rtw_pwrstate_string(sc->sc_pwrstate), rtw_pwrstate_string(power)));
   2483    1.1    dyoung 
   2484    1.1    dyoung 	if (sc->sc_pwrstate == power)
   2485    1.1    dyoung 		return 0;
   2486    1.1    dyoung 
   2487   1.10    dyoung 	rtw_pwrstate0(sc, power, 1, sc->sc_flags & RTW_F_DIGPHY);
   2488    1.1    dyoung 	rc = rtw_rf_pwrstate(sc->sc_rf, power);
   2489   1.10    dyoung 	rtw_pwrstate0(sc, power, 0, sc->sc_flags & RTW_F_DIGPHY);
   2490    1.1    dyoung 
   2491    1.1    dyoung 	switch (power) {
   2492    1.1    dyoung 	case RTW_ON:
   2493    1.4    dyoung 		/* TBD set LEDs */
   2494    1.1    dyoung 		break;
   2495    1.1    dyoung 	case RTW_SLEEP:
   2496    1.1    dyoung 		/* TBD */
   2497    1.1    dyoung 		break;
   2498    1.1    dyoung 	case RTW_OFF:
   2499    1.1    dyoung 		/* TBD */
   2500    1.1    dyoung 		break;
   2501    1.1    dyoung 	}
   2502    1.1    dyoung 	if (rc == 0)
   2503    1.1    dyoung 		sc->sc_pwrstate = power;
   2504    1.1    dyoung 	else
   2505    1.1    dyoung 		sc->sc_pwrstate = RTW_OFF;
   2506    1.1    dyoung 	return rc;
   2507    1.1    dyoung }
   2508    1.1    dyoung 
   2509    1.1    dyoung static int
   2510    1.1    dyoung rtw_tune(struct rtw_softc *sc)
   2511    1.1    dyoung {
   2512    1.1    dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   2513   1.71    dyoung 	struct rtw_tx_radiotap_header *rt = &sc->sc_txtap;
   2514   1.71    dyoung 	struct rtw_rx_radiotap_header *rr = &sc->sc_rxtap;
   2515    1.1    dyoung 	u_int chan;
   2516    1.1    dyoung 	int rc;
   2517    1.1    dyoung 	int antdiv = sc->sc_flags & RTW_F_ANTDIV,
   2518    1.1    dyoung 	    dflantb = sc->sc_flags & RTW_F_DFLANTB;
   2519    1.1    dyoung 
   2520   1.57     skrll 	chan = ieee80211_chan2ieee(ic, ic->ic_curchan);
   2521   1.97    dyoung 	KASSERT(chan != IEEE80211_CHAN_ANY);
   2522    1.1    dyoung 
   2523   1.71    dyoung 	rt->rt_chan_freq = htole16(ic->ic_curchan->ic_freq);
   2524   1.71    dyoung 	rt->rt_chan_flags = htole16(ic->ic_curchan->ic_flags);
   2525   1.71    dyoung 
   2526   1.71    dyoung 	rr->rr_chan_freq = htole16(ic->ic_curchan->ic_freq);
   2527   1.71    dyoung 	rr->rr_chan_flags = htole16(ic->ic_curchan->ic_flags);
   2528   1.71    dyoung 
   2529    1.1    dyoung 	if (chan == sc->sc_cur_chan) {
   2530   1.21    dyoung 		RTW_DPRINTF(RTW_DEBUG_TUNE,
   2531   1.44     perry 		    ("%s: already tuned chan #%d\n", __func__, chan));
   2532    1.1    dyoung 		return 0;
   2533    1.1    dyoung 	}
   2534    1.1    dyoung 
   2535    1.1    dyoung 	rtw_suspend_ticks(sc);
   2536    1.1    dyoung 
   2537   1.83    dyoung 	rtw_io_enable(sc, RTW_CR_RE | RTW_CR_TE, 0);
   2538    1.1    dyoung 
   2539    1.1    dyoung 	/* TBD wait for Tx to complete */
   2540    1.1    dyoung 
   2541  1.101    dyoung 	KASSERT(device_has_power(sc->sc_dev));
   2542    1.1    dyoung 
   2543    1.1    dyoung 	if ((rc = rtw_phy_init(&sc->sc_regs, sc->sc_rf,
   2544   1.57     skrll 	    rtw_chan2txpower(&sc->sc_srom, ic, ic->ic_curchan), sc->sc_csthr,
   2545   1.57     skrll 	        ic->ic_curchan->ic_freq, antdiv, dflantb, RTW_ON)) != 0) {
   2546    1.1    dyoung 		/* XXX condition on powersaving */
   2547   1.98    dyoung 		aprint_error_dev(sc->sc_dev, "phy init failed\n");
   2548    1.1    dyoung 	}
   2549    1.1    dyoung 
   2550    1.1    dyoung 	sc->sc_cur_chan = chan;
   2551    1.1    dyoung 
   2552   1.83    dyoung 	rtw_io_enable(sc, RTW_CR_RE | RTW_CR_TE, 1);
   2553    1.1    dyoung 
   2554    1.1    dyoung 	rtw_resume_ticks(sc);
   2555    1.1    dyoung 
   2556    1.1    dyoung 	return rc;
   2557    1.1    dyoung }
   2558    1.1    dyoung 
   2559  1.101    dyoung bool
   2560  1.114    dyoung rtw_suspend(device_t self, const pmf_qual_t *qual)
   2561    1.1    dyoung {
   2562    1.1    dyoung 	int rc;
   2563  1.101    dyoung 	struct rtw_softc *sc = device_private(self);
   2564  1.101    dyoung 
   2565  1.101    dyoung 	sc->sc_flags &= ~RTW_F_DK_VALID;
   2566    1.1    dyoung 
   2567  1.101    dyoung 	if (!device_has_power(self))
   2568  1.101    dyoung 		return false;
   2569    1.1    dyoung 
   2570    1.1    dyoung 	/* turn off PHY */
   2571  1.101    dyoung 	if ((rc = rtw_pwrstate(sc, RTW_OFF)) != 0) {
   2572  1.101    dyoung 		aprint_error_dev(self, "failed to turn off PHY (%d)\n", rc);
   2573  1.101    dyoung 		return false;
   2574   1.36    dyoung 	}
   2575    1.1    dyoung 
   2576  1.101    dyoung 	rtw_disable_interrupts(&sc->sc_regs);
   2577    1.1    dyoung 
   2578  1.101    dyoung 	return true;
   2579    1.1    dyoung }
   2580    1.1    dyoung 
   2581  1.101    dyoung bool
   2582  1.114    dyoung rtw_resume(device_t self, const pmf_qual_t *qual)
   2583    1.1    dyoung {
   2584  1.101    dyoung 	struct rtw_softc *sc = device_private(self);
   2585  1.101    dyoung 
   2586  1.101    dyoung 	/* Power may have been removed, resetting WEP keys.
   2587  1.101    dyoung 	 */
   2588  1.101    dyoung 	sc->sc_flags &= ~RTW_F_DK_VALID;
   2589  1.101    dyoung 	rtw_enable_interrupts(sc);
   2590  1.101    dyoung 
   2591  1.101    dyoung 	return true;
   2592    1.1    dyoung }
   2593    1.1    dyoung 
   2594    1.1    dyoung static void
   2595    1.1    dyoung rtw_transmit_config(struct rtw_regs *regs)
   2596    1.1    dyoung {
   2597   1.37    dyoung 	uint32_t tcr;
   2598    1.1    dyoung 
   2599    1.1    dyoung 	tcr = RTW_READ(regs, RTW_TCR);
   2600    1.1    dyoung 
   2601   1.10    dyoung 	tcr |= RTW_TCR_CWMIN;
   2602   1.10    dyoung 	tcr &= ~RTW_TCR_MXDMA_MASK;
   2603   1.10    dyoung 	tcr |= RTW_TCR_MXDMA_256;
   2604    1.1    dyoung 	tcr |= RTW_TCR_SAT;		/* send ACK as fast as possible */
   2605    1.1    dyoung 	tcr &= ~RTW_TCR_LBK_MASK;
   2606    1.1    dyoung 	tcr |= RTW_TCR_LBK_NORMAL;	/* normal operating mode */
   2607    1.1    dyoung 
   2608    1.1    dyoung 	/* set short/long retry limits */
   2609    1.1    dyoung 	tcr &= ~(RTW_TCR_SRL_MASK|RTW_TCR_LRL_MASK);
   2610   1.75    dyoung 	tcr |= __SHIFTIN(4, RTW_TCR_SRL_MASK) | __SHIFTIN(4, RTW_TCR_LRL_MASK);
   2611    1.1    dyoung 
   2612   1.13    dyoung 	tcr &= ~RTW_TCR_CRC;	/* NIC appends CRC32 */
   2613    1.1    dyoung 
   2614    1.1    dyoung 	RTW_WRITE(regs, RTW_TCR, tcr);
   2615    1.8    dyoung 	RTW_SYNC(regs, RTW_TCR, RTW_TCR);
   2616    1.1    dyoung }
   2617    1.1    dyoung 
   2618  1.101    dyoung static void
   2619  1.101    dyoung rtw_disable_interrupts(struct rtw_regs *regs)
   2620  1.101    dyoung {
   2621  1.101    dyoung 	RTW_WRITE16(regs, RTW_IMR, 0);
   2622  1.101    dyoung 	RTW_WBW(regs, RTW_IMR, RTW_ISR);
   2623  1.101    dyoung 	RTW_WRITE16(regs, RTW_ISR, 0xffff);
   2624  1.101    dyoung 	RTW_SYNC(regs, RTW_IMR, RTW_ISR);
   2625  1.101    dyoung }
   2626  1.101    dyoung 
   2627  1.101    dyoung static void
   2628    1.1    dyoung rtw_enable_interrupts(struct rtw_softc *sc)
   2629    1.1    dyoung {
   2630    1.1    dyoung 	struct rtw_regs *regs = &sc->sc_regs;
   2631    1.1    dyoung 
   2632    1.1    dyoung 	sc->sc_inten = RTW_INTR_RX|RTW_INTR_TX|RTW_INTR_BEACON|RTW_INTR_ATIMINT;
   2633    1.1    dyoung 	sc->sc_inten |= RTW_INTR_IOERROR|RTW_INTR_TIMEOUT;
   2634    1.1    dyoung 
   2635    1.1    dyoung 	RTW_WRITE16(regs, RTW_IMR, sc->sc_inten);
   2636    1.8    dyoung 	RTW_WBW(regs, RTW_IMR, RTW_ISR);
   2637    1.1    dyoung 	RTW_WRITE16(regs, RTW_ISR, 0xffff);
   2638    1.8    dyoung 	RTW_SYNC(regs, RTW_IMR, RTW_ISR);
   2639    1.1    dyoung 
   2640    1.1    dyoung 	/* XXX necessary? */
   2641    1.1    dyoung 	if (sc->sc_intr_ack != NULL)
   2642    1.1    dyoung 		(*sc->sc_intr_ack)(regs);
   2643    1.1    dyoung }
   2644    1.1    dyoung 
   2645   1.10    dyoung static void
   2646   1.10    dyoung rtw_set_nettype(struct rtw_softc *sc, enum ieee80211_opmode opmode)
   2647   1.10    dyoung {
   2648   1.10    dyoung 	uint8_t msr;
   2649   1.10    dyoung 
   2650   1.10    dyoung 	/* I'm guessing that MSR is protected as CONFIG[0123] are. */
   2651   1.42    dyoung 	rtw_set_access(&sc->sc_regs, RTW_ACCESS_CONFIG);
   2652   1.10    dyoung 
   2653   1.10    dyoung 	msr = RTW_READ8(&sc->sc_regs, RTW_MSR) & ~RTW_MSR_NETYPE_MASK;
   2654   1.10    dyoung 
   2655   1.10    dyoung 	switch (opmode) {
   2656   1.10    dyoung 	case IEEE80211_M_AHDEMO:
   2657   1.10    dyoung 	case IEEE80211_M_IBSS:
   2658   1.10    dyoung 		msr |= RTW_MSR_NETYPE_ADHOC_OK;
   2659   1.10    dyoung 		break;
   2660   1.10    dyoung 	case IEEE80211_M_HOSTAP:
   2661   1.10    dyoung 		msr |= RTW_MSR_NETYPE_AP_OK;
   2662   1.10    dyoung 		break;
   2663   1.10    dyoung 	case IEEE80211_M_MONITOR:
   2664   1.10    dyoung 		/* XXX */
   2665   1.10    dyoung 		msr |= RTW_MSR_NETYPE_NOLINK;
   2666   1.10    dyoung 		break;
   2667   1.10    dyoung 	case IEEE80211_M_STA:
   2668   1.10    dyoung 		msr |= RTW_MSR_NETYPE_INFRA_OK;
   2669   1.10    dyoung 		break;
   2670   1.10    dyoung 	}
   2671   1.10    dyoung 	RTW_WRITE8(&sc->sc_regs, RTW_MSR, msr);
   2672   1.10    dyoung 
   2673   1.42    dyoung 	rtw_set_access(&sc->sc_regs, RTW_ACCESS_NONE);
   2674   1.10    dyoung }
   2675   1.10    dyoung 
   2676    1.1    dyoung #define	rtw_calchash(addr) \
   2677   1.38    dyoung 	(ether_crc32_be((addr), IEEE80211_ADDR_LEN) >> 26)
   2678    1.1    dyoung 
   2679    1.1    dyoung static void
   2680    1.1    dyoung rtw_pktfilt_load(struct rtw_softc *sc)
   2681    1.1    dyoung {
   2682    1.1    dyoung 	struct rtw_regs *regs = &sc->sc_regs;
   2683    1.1    dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   2684   1.48    dyoung 	struct ethercom *ec = &sc->sc_ec;
   2685   1.48    dyoung 	struct ifnet *ifp = &sc->sc_if;
   2686    1.1    dyoung 	int hash;
   2687   1.37    dyoung 	uint32_t hashes[2] = { 0, 0 };
   2688    1.1    dyoung 	struct ether_multi *enm;
   2689    1.1    dyoung 	struct ether_multistep step;
   2690    1.1    dyoung 
   2691    1.1    dyoung 	/* XXX might be necessary to stop Rx/Tx engines while setting filters */
   2692    1.1    dyoung 
   2693   1.38    dyoung 	sc->sc_rcr &= ~RTW_RCR_PKTFILTER_MASK;
   2694   1.38    dyoung 	sc->sc_rcr &= ~(RTW_RCR_MXDMA_MASK | RTW_RCR_RXFTH_MASK);
   2695    1.1    dyoung 
   2696   1.38    dyoung 	sc->sc_rcr |= RTW_RCR_PKTFILTER_DEFAULT;
   2697   1.38    dyoung 	/* MAC auto-reset PHY (huh?) */
   2698   1.10    dyoung 	sc->sc_rcr |= RTW_RCR_ENMARP;
   2699   1.38    dyoung 	/* DMA whole Rx packets, only.  Set Tx DMA burst size to 1024 bytes. */
   2700   1.38    dyoung 	sc->sc_rcr |= RTW_RCR_MXDMA_1024 | RTW_RCR_RXFTH_WHOLE;
   2701    1.1    dyoung 
   2702   1.38    dyoung 	switch (ic->ic_opmode) {
   2703   1.38    dyoung 	case IEEE80211_M_MONITOR:
   2704   1.38    dyoung 		sc->sc_rcr |= RTW_RCR_MONITOR;
   2705   1.38    dyoung 		break;
   2706   1.38    dyoung 	case IEEE80211_M_AHDEMO:
   2707   1.38    dyoung 	case IEEE80211_M_IBSS:
   2708   1.38    dyoung 		/* receive broadcasts in our BSS */
   2709   1.38    dyoung 		sc->sc_rcr |= RTW_RCR_ADD3;
   2710   1.38    dyoung 		break;
   2711   1.38    dyoung 	default:
   2712   1.38    dyoung 		break;
   2713   1.38    dyoung 	}
   2714    1.1    dyoung 
   2715    1.1    dyoung 	ifp->if_flags &= ~IFF_ALLMULTI;
   2716    1.1    dyoung 
   2717    1.1    dyoung 	/*
   2718    1.1    dyoung 	 * Program the 64-bit multicast hash filter.
   2719    1.1    dyoung 	 */
   2720    1.1    dyoung 	ETHER_FIRST_MULTI(step, ec, enm);
   2721    1.1    dyoung 	while (enm != NULL) {
   2722    1.1    dyoung 		/* XXX */
   2723    1.1    dyoung 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
   2724   1.86    dyoung 		    ETHER_ADDR_LEN) != 0) {
   2725   1.86    dyoung 			ifp->if_flags |= IFF_ALLMULTI;
   2726   1.86    dyoung 			break;
   2727   1.86    dyoung 		}
   2728    1.1    dyoung 
   2729    1.1    dyoung 		hash = rtw_calchash(enm->enm_addrlo);
   2730   1.38    dyoung 		hashes[hash >> 5] |= (1 << (hash & 0x1f));
   2731    1.1    dyoung 		ETHER_NEXT_MULTI(step, enm);
   2732    1.1    dyoung 	}
   2733    1.1    dyoung 
   2734   1.86    dyoung 	/* XXX accept all broadcast if scanning */
   2735   1.86    dyoung 	if ((ifp->if_flags & IFF_BROADCAST) != 0)
   2736   1.86    dyoung 		sc->sc_rcr |= RTW_RCR_AB;	/* accept all broadcast */
   2737   1.86    dyoung 
   2738   1.86    dyoung 	if (ifp->if_flags & IFF_PROMISC) {
   2739   1.86    dyoung 		sc->sc_rcr |= RTW_RCR_AB;	/* accept all broadcast */
   2740   1.93    dyoung 		sc->sc_rcr |= RTW_RCR_ACRC32;	/* accept frames failing CRC */
   2741   1.93    dyoung 		sc->sc_rcr |= RTW_RCR_AICV;	/* accept frames failing ICV */
   2742   1.86    dyoung 		ifp->if_flags |= IFF_ALLMULTI;
   2743   1.86    dyoung 	}
   2744   1.86    dyoung 
   2745   1.86    dyoung 	if (ifp->if_flags & IFF_ALLMULTI)
   2746   1.38    dyoung 		hashes[0] = hashes[1] = 0xffffffff;
   2747   1.86    dyoung 
   2748   1.86    dyoung 	if ((hashes[0] | hashes[1]) != 0)
   2749   1.86    dyoung 		sc->sc_rcr |= RTW_RCR_AM;	/* accept multicast */
   2750    1.1    dyoung 
   2751    1.1    dyoung 	RTW_WRITE(regs, RTW_MAR0, hashes[0]);
   2752    1.1    dyoung 	RTW_WRITE(regs, RTW_MAR1, hashes[1]);
   2753    1.1    dyoung 	RTW_WRITE(regs, RTW_RCR, sc->sc_rcr);
   2754    1.1    dyoung 	RTW_SYNC(regs, RTW_MAR0, RTW_RCR); /* RTW_MAR0 < RTW_MAR1 < RTW_RCR */
   2755    1.1    dyoung 
   2756   1.21    dyoung 	DPRINTF(sc, RTW_DEBUG_PKTFILT,
   2757   1.21    dyoung 	    ("%s: RTW_MAR0 %08x RTW_MAR1 %08x RTW_RCR %08x\n",
   2758   1.98    dyoung 	    device_xname(sc->sc_dev), RTW_READ(regs, RTW_MAR0),
   2759    1.1    dyoung 	    RTW_READ(regs, RTW_MAR1), RTW_READ(regs, RTW_RCR)));
   2760    1.1    dyoung }
   2761    1.1    dyoung 
   2762   1.42    dyoung static struct mbuf *
   2763   1.42    dyoung rtw_beacon_alloc(struct rtw_softc *sc, struct ieee80211_node *ni)
   2764   1.42    dyoung {
   2765   1.42    dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   2766   1.42    dyoung 	struct mbuf *m;
   2767   1.48    dyoung 	struct ieee80211_beacon_offsets	boff;
   2768   1.42    dyoung 
   2769   1.58    dyoung 	if ((m = ieee80211_beacon_alloc(ic, ni, &boff)) != NULL) {
   2770   1.58    dyoung 		RTW_DPRINTF(RTW_DEBUG_BEACON,
   2771   1.58    dyoung 		    ("%s: m %p len %u\n", __func__, m, m->m_len));
   2772   1.58    dyoung 	}
   2773   1.42    dyoung 	return m;
   2774   1.42    dyoung }
   2775   1.42    dyoung 
   2776   1.21    dyoung /* Must be called at splnet. */
   2777    1.1    dyoung static int
   2778    1.1    dyoung rtw_init(struct ifnet *ifp)
   2779    1.1    dyoung {
   2780    1.1    dyoung 	struct rtw_softc *sc = (struct rtw_softc *)ifp->if_softc;
   2781    1.1    dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   2782    1.1    dyoung 	struct rtw_regs *regs = &sc->sc_regs;
   2783  1.101    dyoung 	int rc;
   2784    1.1    dyoung 
   2785  1.101    dyoung 	if (device_is_active(sc->sc_dev)) {
   2786  1.101    dyoung 		/* Cancel pending I/O and reset. */
   2787  1.101    dyoung 		rtw_stop(ifp, 0);
   2788  1.109    dyoung 	} else if (!pmf_device_resume(sc->sc_dev, &sc->sc_qual) ||
   2789  1.109    dyoung 	           !device_is_active(sc->sc_dev))
   2790  1.109    dyoung 		return 0;
   2791    1.1    dyoung 
   2792   1.21    dyoung 	DPRINTF(sc, RTW_DEBUG_TUNE, ("%s: channel %d freq %d flags 0x%04x\n",
   2793   1.57     skrll 	    __func__, ieee80211_chan2ieee(ic, ic->ic_curchan),
   2794   1.57     skrll 	    ic->ic_curchan->ic_freq, ic->ic_curchan->ic_flags));
   2795    1.1    dyoung 
   2796    1.1    dyoung 	if ((rc = rtw_pwrstate(sc, RTW_OFF)) != 0)
   2797    1.1    dyoung 		goto out;
   2798    1.1    dyoung 
   2799   1.31    dyoung 	if ((rc = rtw_swring_setup(sc)) != 0)
   2800   1.31    dyoung 		goto out;
   2801    1.1    dyoung 
   2802    1.1    dyoung 	rtw_transmit_config(regs);
   2803    1.1    dyoung 
   2804   1.42    dyoung 	rtw_set_access(regs, RTW_ACCESS_CONFIG);
   2805    1.1    dyoung 
   2806    1.4    dyoung 	RTW_WRITE8(regs, RTW_MSR, 0x0);	/* no link */
   2807    1.8    dyoung 	RTW_WBW(regs, RTW_MSR, RTW_BRSR);
   2808    1.1    dyoung 
   2809   1.27   mycroft 	/* long PLCP header, 1Mb/2Mb basic rate */
   2810   1.27   mycroft 	RTW_WRITE16(regs, RTW_BRSR, RTW_BRSR_MBR8180_2MBPS);
   2811    1.8    dyoung 	RTW_SYNC(regs, RTW_BRSR, RTW_BRSR);
   2812    1.1    dyoung 
   2813   1.42    dyoung 	rtw_set_access(regs, RTW_ACCESS_ANAPARM);
   2814   1.42    dyoung 	rtw_set_access(regs, RTW_ACCESS_NONE);
   2815    1.1    dyoung 
   2816    1.1    dyoung 	/* XXX from reference sources */
   2817    1.1    dyoung 	RTW_WRITE(regs, RTW_FEMR, 0xffff);
   2818    1.8    dyoung 	RTW_SYNC(regs, RTW_FEMR, RTW_FEMR);
   2819    1.1    dyoung 
   2820   1.98    dyoung 	rtw_set_rfprog(regs, sc->sc_rfchipid, sc->sc_dev);
   2821    1.4    dyoung 
   2822    1.4    dyoung 	RTW_WRITE8(regs, RTW_PHYDELAY, sc->sc_phydelay);
   2823    1.1    dyoung 	/* from Linux driver */
   2824    1.4    dyoung 	RTW_WRITE8(regs, RTW_CRCOUNT, RTW_CRCOUNT_MAGIC);
   2825    1.1    dyoung 
   2826    1.8    dyoung 	RTW_SYNC(regs, RTW_PHYDELAY, RTW_CRCOUNT);
   2827    1.8    dyoung 
   2828    1.1    dyoung 	rtw_enable_interrupts(sc);
   2829    1.1    dyoung 
   2830    1.1    dyoung 	rtw_pktfilt_load(sc);
   2831    1.1    dyoung 
   2832    1.3    dyoung 	rtw_hwring_setup(sc);
   2833    1.1    dyoung 
   2834   1.48    dyoung 	rtw_wep_setkeys(sc, ic->ic_nw_keys, ic->ic_def_txkey);
   2835   1.42    dyoung 
   2836   1.83    dyoung 	rtw_io_enable(sc, RTW_CR_RE | RTW_CR_TE, 1);
   2837    1.1    dyoung 
   2838    1.1    dyoung 	ifp->if_flags |= IFF_RUNNING;
   2839    1.1    dyoung 	ic->ic_state = IEEE80211_S_INIT;
   2840    1.1    dyoung 
   2841    1.1    dyoung 	RTW_WRITE16(regs, RTW_BSSID16, 0x0);
   2842    1.1    dyoung 	RTW_WRITE(regs, RTW_BSSID32, 0x0);
   2843    1.1    dyoung 
   2844    1.4    dyoung 	rtw_resume_ticks(sc);
   2845    1.1    dyoung 
   2846   1.10    dyoung 	rtw_set_nettype(sc, IEEE80211_M_MONITOR);
   2847    1.4    dyoung 
   2848    1.4    dyoung 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
   2849    1.4    dyoung 		return ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
   2850    1.4    dyoung 	else
   2851    1.4    dyoung 		return ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
   2852    1.4    dyoung 
   2853    1.1    dyoung out:
   2854   1.98    dyoung 	aprint_error_dev(sc->sc_dev, "interface not running\n");
   2855    1.1    dyoung 	return rc;
   2856    1.1    dyoung }
   2857    1.1    dyoung 
   2858   1.61     perry static inline void
   2859   1.42    dyoung rtw_led_init(struct rtw_regs *regs)
   2860   1.42    dyoung {
   2861   1.42    dyoung 	uint8_t cfg0, cfg1;
   2862   1.42    dyoung 
   2863   1.42    dyoung 	rtw_set_access(regs, RTW_ACCESS_CONFIG);
   2864   1.42    dyoung 
   2865   1.42    dyoung 	cfg0 = RTW_READ8(regs, RTW_CONFIG0);
   2866   1.42    dyoung 	cfg0 |= RTW_CONFIG0_LEDGPOEN;
   2867   1.42    dyoung 	RTW_WRITE8(regs, RTW_CONFIG0, cfg0);
   2868   1.42    dyoung 
   2869   1.42    dyoung 	cfg1 = RTW_READ8(regs, RTW_CONFIG1);
   2870   1.42    dyoung 	RTW_DPRINTF(RTW_DEBUG_LED,
   2871   1.42    dyoung 	    ("%s: read %" PRIx8 " from reg[CONFIG1]\n", __func__, cfg1));
   2872   1.42    dyoung 
   2873   1.42    dyoung 	cfg1 &= ~RTW_CONFIG1_LEDS_MASK;
   2874   1.42    dyoung 	cfg1 |= RTW_CONFIG1_LEDS_TX_RX;
   2875   1.42    dyoung 	RTW_WRITE8(regs, RTW_CONFIG1, cfg1);
   2876   1.42    dyoung 
   2877   1.42    dyoung 	rtw_set_access(regs, RTW_ACCESS_NONE);
   2878   1.42    dyoung }
   2879   1.42    dyoung 
   2880   1.44     perry /*
   2881   1.42    dyoung  * IEEE80211_S_INIT: 		LED1 off
   2882   1.42    dyoung  *
   2883   1.42    dyoung  * IEEE80211_S_AUTH,
   2884   1.42    dyoung  * IEEE80211_S_ASSOC,
   2885   1.42    dyoung  * IEEE80211_S_SCAN: 		LED1 blinks @ 1 Hz, blinks at 5Hz for tx/rx
   2886   1.42    dyoung  *
   2887   1.42    dyoung  * IEEE80211_S_RUN: 		LED1 on, blinks @ 5Hz for tx/rx
   2888   1.42    dyoung  */
   2889   1.42    dyoung static void
   2890   1.42    dyoung rtw_led_newstate(struct rtw_softc *sc, enum ieee80211_state nstate)
   2891   1.42    dyoung {
   2892   1.42    dyoung 	struct rtw_led_state *ls;
   2893   1.42    dyoung 
   2894   1.42    dyoung 	ls = &sc->sc_led_state;
   2895   1.42    dyoung 
   2896   1.42    dyoung 	switch (nstate) {
   2897   1.42    dyoung 	case IEEE80211_S_INIT:
   2898   1.42    dyoung 		rtw_led_init(&sc->sc_regs);
   2899  1.101    dyoung 		aprint_debug_dev(sc->sc_dev, "stopping blink\n");
   2900   1.42    dyoung 		callout_stop(&ls->ls_slow_ch);
   2901   1.42    dyoung 		callout_stop(&ls->ls_fast_ch);
   2902   1.42    dyoung 		ls->ls_slowblink = 0;
   2903   1.42    dyoung 		ls->ls_actblink = 0;
   2904   1.42    dyoung 		ls->ls_default = 0;
   2905   1.42    dyoung 		break;
   2906   1.42    dyoung 	case IEEE80211_S_SCAN:
   2907  1.101    dyoung 		aprint_debug_dev(sc->sc_dev, "scheduling blink\n");
   2908   1.42    dyoung 		callout_schedule(&ls->ls_slow_ch, RTW_LED_SLOW_TICKS);
   2909   1.42    dyoung 		callout_schedule(&ls->ls_fast_ch, RTW_LED_FAST_TICKS);
   2910   1.42    dyoung 		/*FALLTHROUGH*/
   2911   1.42    dyoung 	case IEEE80211_S_AUTH:
   2912   1.42    dyoung 	case IEEE80211_S_ASSOC:
   2913   1.42    dyoung 		ls->ls_default = RTW_LED1;
   2914   1.42    dyoung 		ls->ls_actblink = RTW_LED1;
   2915   1.42    dyoung 		ls->ls_slowblink = RTW_LED1;
   2916   1.42    dyoung 		break;
   2917   1.42    dyoung 	case IEEE80211_S_RUN:
   2918   1.42    dyoung 		ls->ls_slowblink = 0;
   2919   1.42    dyoung 		break;
   2920   1.42    dyoung 	}
   2921   1.42    dyoung 	rtw_led_set(ls, &sc->sc_regs, sc->sc_hwverid);
   2922   1.42    dyoung }
   2923   1.42    dyoung 
   2924   1.42    dyoung static void
   2925   1.42    dyoung rtw_led_set(struct rtw_led_state *ls, struct rtw_regs *regs, int hwverid)
   2926   1.42    dyoung {
   2927   1.42    dyoung 	uint8_t led_condition;
   2928   1.42    dyoung 	bus_size_t ofs;
   2929   1.42    dyoung 	uint8_t mask, newval, val;
   2930   1.42    dyoung 
   2931   1.42    dyoung 	led_condition = ls->ls_default;
   2932   1.42    dyoung 
   2933   1.42    dyoung 	if (ls->ls_state & RTW_LED_S_SLOW)
   2934   1.42    dyoung 		led_condition ^= ls->ls_slowblink;
   2935   1.42    dyoung 	if (ls->ls_state & (RTW_LED_S_RX|RTW_LED_S_TX))
   2936   1.42    dyoung 		led_condition ^= ls->ls_actblink;
   2937   1.42    dyoung 
   2938   1.42    dyoung 	RTW_DPRINTF(RTW_DEBUG_LED,
   2939   1.42    dyoung 	    ("%s: LED condition %" PRIx8 "\n", __func__, led_condition));
   2940   1.42    dyoung 
   2941   1.42    dyoung 	switch (hwverid) {
   2942   1.42    dyoung 	default:
   2943   1.42    dyoung 	case 'F':
   2944   1.42    dyoung 		ofs = RTW_PSR;
   2945   1.42    dyoung 		newval = mask = RTW_PSR_LEDGPO0 | RTW_PSR_LEDGPO1;
   2946   1.42    dyoung 		if (led_condition & RTW_LED0)
   2947   1.42    dyoung 			newval &= ~RTW_PSR_LEDGPO0;
   2948   1.42    dyoung 		if (led_condition & RTW_LED1)
   2949   1.42    dyoung 			newval &= ~RTW_PSR_LEDGPO1;
   2950   1.42    dyoung 		break;
   2951   1.42    dyoung 	case 'D':
   2952   1.42    dyoung 		ofs = RTW_9346CR;
   2953   1.42    dyoung 		mask = RTW_9346CR_EEM_MASK | RTW_9346CR_EEDI | RTW_9346CR_EECS;
   2954   1.42    dyoung 		newval = RTW_9346CR_EEM_PROGRAM;
   2955   1.42    dyoung 		if (led_condition & RTW_LED0)
   2956   1.42    dyoung 			newval |= RTW_9346CR_EEDI;
   2957   1.42    dyoung 		if (led_condition & RTW_LED1)
   2958   1.42    dyoung 			newval |= RTW_9346CR_EECS;
   2959   1.42    dyoung 		break;
   2960   1.42    dyoung 	}
   2961   1.42    dyoung 	val = RTW_READ8(regs, ofs);
   2962   1.42    dyoung 	RTW_DPRINTF(RTW_DEBUG_LED,
   2963   1.45    dyoung 	    ("%s: read %" PRIx8 " from reg[%#02" PRIxPTR "]\n", __func__, val,
   2964   1.45    dyoung 	     (uintptr_t)ofs));
   2965   1.42    dyoung 	val &= ~mask;
   2966   1.42    dyoung 	val |= newval;
   2967   1.42    dyoung 	RTW_WRITE8(regs, ofs, val);
   2968   1.42    dyoung 	RTW_DPRINTF(RTW_DEBUG_LED,
   2969   1.45    dyoung 	    ("%s: wrote %" PRIx8 " to reg[%#02" PRIxPTR "]\n", __func__, val,
   2970   1.45    dyoung 	     (uintptr_t)ofs));
   2971   1.42    dyoung 	RTW_SYNC(regs, ofs, ofs);
   2972   1.42    dyoung }
   2973   1.42    dyoung 
   2974   1.42    dyoung static void
   2975   1.42    dyoung rtw_led_fastblink(void *arg)
   2976   1.42    dyoung {
   2977   1.42    dyoung 	int ostate, s;
   2978   1.42    dyoung 	struct rtw_softc *sc = (struct rtw_softc *)arg;
   2979   1.42    dyoung 	struct rtw_led_state *ls = &sc->sc_led_state;
   2980   1.42    dyoung 
   2981   1.42    dyoung 	s = splnet();
   2982   1.42    dyoung 	ostate = ls->ls_state;
   2983   1.42    dyoung 	ls->ls_state ^= ls->ls_event;
   2984   1.42    dyoung 
   2985   1.42    dyoung 	if ((ls->ls_event & RTW_LED_S_TX) == 0)
   2986   1.42    dyoung 		ls->ls_state &= ~RTW_LED_S_TX;
   2987   1.42    dyoung 
   2988   1.42    dyoung 	if ((ls->ls_event & RTW_LED_S_RX) == 0)
   2989   1.42    dyoung 		ls->ls_state &= ~RTW_LED_S_RX;
   2990   1.42    dyoung 
   2991   1.42    dyoung 	ls->ls_event = 0;
   2992   1.42    dyoung 
   2993   1.42    dyoung 	if (ostate != ls->ls_state)
   2994   1.42    dyoung 		rtw_led_set(ls, &sc->sc_regs, sc->sc_hwverid);
   2995   1.42    dyoung 	splx(s);
   2996   1.42    dyoung 
   2997  1.101    dyoung 	aprint_debug_dev(sc->sc_dev, "scheduling fast blink\n");
   2998   1.42    dyoung 	callout_schedule(&ls->ls_fast_ch, RTW_LED_FAST_TICKS);
   2999   1.42    dyoung }
   3000   1.42    dyoung 
   3001   1.42    dyoung static void
   3002   1.42    dyoung rtw_led_slowblink(void *arg)
   3003   1.42    dyoung {
   3004   1.42    dyoung 	int s;
   3005   1.42    dyoung 	struct rtw_softc *sc = (struct rtw_softc *)arg;
   3006   1.42    dyoung 	struct rtw_led_state *ls = &sc->sc_led_state;
   3007   1.42    dyoung 
   3008   1.42    dyoung 	s = splnet();
   3009   1.42    dyoung 	ls->ls_state ^= RTW_LED_S_SLOW;
   3010   1.42    dyoung 	rtw_led_set(ls, &sc->sc_regs, sc->sc_hwverid);
   3011   1.42    dyoung 	splx(s);
   3012  1.101    dyoung 	aprint_debug_dev(sc->sc_dev, "scheduling slow blink\n");
   3013   1.42    dyoung 	callout_schedule(&ls->ls_slow_ch, RTW_LED_SLOW_TICKS);
   3014   1.42    dyoung }
   3015   1.42    dyoung 
   3016  1.101    dyoung static void
   3017  1.101    dyoung rtw_led_detach(struct rtw_led_state *ls)
   3018  1.101    dyoung {
   3019  1.101    dyoung 	callout_destroy(&ls->ls_fast_ch);
   3020  1.101    dyoung 	callout_destroy(&ls->ls_slow_ch);
   3021  1.101    dyoung }
   3022  1.101    dyoung 
   3023  1.101    dyoung static void
   3024   1.45    dyoung rtw_led_attach(struct rtw_led_state *ls, void *arg)
   3025   1.42    dyoung {
   3026   1.89        ad 	callout_init(&ls->ls_fast_ch, 0);
   3027   1.89        ad 	callout_init(&ls->ls_slow_ch, 0);
   3028   1.45    dyoung 	callout_setfunc(&ls->ls_fast_ch, rtw_led_fastblink, arg);
   3029   1.45    dyoung 	callout_setfunc(&ls->ls_slow_ch, rtw_led_slowblink, arg);
   3030   1.42    dyoung }
   3031   1.42    dyoung 
   3032    1.1    dyoung static int
   3033   1.85  christos rtw_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   3034    1.1    dyoung {
   3035   1.21    dyoung 	int rc = 0, s;
   3036    1.1    dyoung 	struct rtw_softc *sc = ifp->if_softc;
   3037    1.1    dyoung 
   3038   1.21    dyoung 	s = splnet();
   3039   1.86    dyoung 	if (cmd == SIOCSIFFLAGS) {
   3040  1.105    dyoung 		if ((rc = ifioctl_common(ifp, cmd, data)) != 0)
   3041  1.105    dyoung 			;
   3042  1.109    dyoung 		else switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
   3043  1.109    dyoung 		case IFF_UP:
   3044  1.109    dyoung 			rc = rtw_init(ifp);
   3045  1.109    dyoung 			RTW_PRINT_REGS(&sc->sc_regs, ifp->if_xname, __func__);
   3046  1.109    dyoung 			break;
   3047  1.109    dyoung 		case IFF_UP|IFF_RUNNING:
   3048  1.109    dyoung 			if (device_activation(sc->sc_dev, DEVACT_LEVEL_DRIVER))
   3049    1.1    dyoung 				rtw_pktfilt_load(sc);
   3050   1.39    dyoung 			RTW_PRINT_REGS(&sc->sc_regs, ifp->if_xname, __func__);
   3051  1.109    dyoung 			break;
   3052  1.109    dyoung 		case IFF_RUNNING:
   3053   1.39    dyoung 			RTW_PRINT_REGS(&sc->sc_regs, ifp->if_xname, __func__);
   3054    1.1    dyoung 			rtw_stop(ifp, 1);
   3055  1.109    dyoung 			break;
   3056  1.109    dyoung 		default:
   3057  1.109    dyoung 			break;
   3058    1.1    dyoung 		}
   3059   1.86    dyoung 	} else if ((rc = ieee80211_ioctl(&sc->sc_ic, cmd, data)) != ENETRESET)
   3060   1.86    dyoung 		;	/* nothing to do */
   3061   1.86    dyoung 	else if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI) {
   3062   1.86    dyoung 		/* reload packet filter if running */
   3063   1.39    dyoung 		if (ifp->if_flags & IFF_RUNNING)
   3064   1.39    dyoung 			rtw_pktfilt_load(sc);
   3065   1.39    dyoung 		rc = 0;
   3066  1.101    dyoung 	} else if ((ifp->if_flags & IFF_UP) != 0)
   3067   1.86    dyoung 		rc = rtw_init(ifp);
   3068   1.86    dyoung 	else
   3069   1.86    dyoung 		rc = 0;
   3070   1.21    dyoung 	splx(s);
   3071    1.1    dyoung 	return rc;
   3072    1.1    dyoung }
   3073    1.1    dyoung 
   3074   1.42    dyoung /* Select a transmit ring with at least one h/w and s/w descriptor free.
   3075   1.42    dyoung  * Return 0 on success, -1 on failure.
   3076   1.42    dyoung  */
   3077   1.61     perry static inline int
   3078   1.42    dyoung rtw_txring_choose(struct rtw_softc *sc, struct rtw_txsoft_blk **tsbp,
   3079   1.42    dyoung     struct rtw_txdesc_blk **tdbp, int pri)
   3080   1.42    dyoung {
   3081   1.42    dyoung 	struct rtw_txsoft_blk *tsb;
   3082   1.42    dyoung 	struct rtw_txdesc_blk *tdb;
   3083   1.42    dyoung 
   3084   1.42    dyoung 	KASSERT(pri >= 0 && pri < RTW_NTXPRI);
   3085   1.42    dyoung 
   3086   1.42    dyoung 	tsb = &sc->sc_txsoft_blk[pri];
   3087   1.42    dyoung 	tdb = &sc->sc_txdesc_blk[pri];
   3088   1.42    dyoung 
   3089   1.42    dyoung 	if (SIMPLEQ_EMPTY(&tsb->tsb_freeq) || tdb->tdb_nfree == 0) {
   3090   1.58    dyoung 		if (tsb->tsb_tx_timer == 0)
   3091   1.58    dyoung 			tsb->tsb_tx_timer = 5;
   3092   1.42    dyoung 		*tsbp = NULL;
   3093   1.42    dyoung 		*tdbp = NULL;
   3094   1.42    dyoung 		return -1;
   3095   1.42    dyoung 	}
   3096   1.42    dyoung 	*tsbp = tsb;
   3097   1.42    dyoung 	*tdbp = tdb;
   3098   1.42    dyoung 	return 0;
   3099   1.42    dyoung }
   3100   1.42    dyoung 
   3101   1.61     perry static inline struct mbuf *
   3102   1.42    dyoung rtw_80211_dequeue(struct rtw_softc *sc, struct ifqueue *ifq, int pri,
   3103   1.42    dyoung     struct rtw_txsoft_blk **tsbp, struct rtw_txdesc_blk **tdbp,
   3104   1.42    dyoung     struct ieee80211_node **nip, short *if_flagsp)
   3105   1.42    dyoung {
   3106   1.42    dyoung 	struct mbuf *m;
   3107   1.42    dyoung 
   3108   1.42    dyoung 	if (IF_IS_EMPTY(ifq))
   3109   1.42    dyoung 		return NULL;
   3110   1.42    dyoung 	if (rtw_txring_choose(sc, tsbp, tdbp, pri) == -1) {
   3111   1.58    dyoung 		DPRINTF(sc, RTW_DEBUG_XMIT_RSRC, ("%s: no ring %d descriptor\n",
   3112   1.58    dyoung 		    __func__, pri));
   3113   1.42    dyoung 		*if_flagsp |= IFF_OACTIVE;
   3114   1.58    dyoung 		sc->sc_if.if_timer = 1;
   3115   1.42    dyoung 		return NULL;
   3116   1.42    dyoung 	}
   3117   1.42    dyoung 	IF_DEQUEUE(ifq, m);
   3118  1.122     ozaki 	*nip = M_GETCTX(m, struct ieee80211_node *);
   3119  1.122     ozaki 	M_SETCTX(m, NULL);
   3120   1.48    dyoung 	KASSERT(*nip != NULL);
   3121   1.42    dyoung 	return m;
   3122   1.42    dyoung }
   3123   1.42    dyoung 
   3124   1.34    dyoung /* Point *mp at the next 802.11 frame to transmit.  Point *tsbp
   3125    1.1    dyoung  * at the driver's selection of transmit control block for the packet.
   3126    1.1    dyoung  */
   3127   1.61     perry static inline int
   3128   1.34    dyoung rtw_dequeue(struct ifnet *ifp, struct rtw_txsoft_blk **tsbp,
   3129   1.34    dyoung     struct rtw_txdesc_blk **tdbp, struct mbuf **mp,
   3130    1.1    dyoung     struct ieee80211_node **nip)
   3131    1.1    dyoung {
   3132   1.48    dyoung 	int pri;
   3133   1.48    dyoung 	struct ether_header *eh;
   3134    1.1    dyoung 	struct mbuf *m0;
   3135    1.1    dyoung 	struct rtw_softc *sc;
   3136   1.42    dyoung 	short *if_flagsp;
   3137    1.1    dyoung 
   3138   1.84    dyoung 	*mp = NULL;
   3139   1.84    dyoung 
   3140    1.1    dyoung 	sc = (struct rtw_softc *)ifp->if_softc;
   3141    1.1    dyoung 
   3142   1.21    dyoung 	DPRINTF(sc, RTW_DEBUG_XMIT,
   3143   1.98    dyoung 	    ("%s: enter %s\n", device_xname(sc->sc_dev), __func__));
   3144    1.1    dyoung 
   3145   1.42    dyoung 	if_flagsp = &ifp->if_flags;
   3146    1.5    dyoung 
   3147   1.42    dyoung 	if (sc->sc_ic.ic_state == IEEE80211_S_RUN &&
   3148   1.42    dyoung 	    (*mp = rtw_80211_dequeue(sc, &sc->sc_beaconq, RTW_TXPRIBCN, tsbp,
   3149   1.42    dyoung 		                     tdbp, nip, if_flagsp)) != NULL) {
   3150   1.42    dyoung 		DPRINTF(sc, RTW_DEBUG_XMIT, ("%s: dequeue beacon frame\n",
   3151   1.42    dyoung 		    __func__));
   3152    1.5    dyoung 		return 0;
   3153    1.5    dyoung 	}
   3154    1.5    dyoung 
   3155   1.42    dyoung 	if ((*mp = rtw_80211_dequeue(sc, &sc->sc_ic.ic_mgtq, RTW_TXPRIMD, tsbp,
   3156   1.42    dyoung 		                     tdbp, nip, if_flagsp)) != NULL) {
   3157   1.42    dyoung 		DPRINTF(sc, RTW_DEBUG_XMIT, ("%s: dequeue mgt frame\n",
   3158   1.42    dyoung 		    __func__));
   3159   1.42    dyoung 		return 0;
   3160   1.42    dyoung 	}
   3161    1.5    dyoung 
   3162   1.42    dyoung 	if (sc->sc_ic.ic_state != IEEE80211_S_RUN) {
   3163   1.21    dyoung 		DPRINTF(sc, RTW_DEBUG_XMIT, ("%s: not running\n", __func__));
   3164    1.1    dyoung 		return 0;
   3165   1.42    dyoung 	}
   3166   1.42    dyoung 
   3167   1.48    dyoung 	IFQ_POLL(&ifp->if_snd, m0);
   3168   1.48    dyoung 	if (m0 == NULL) {
   3169   1.48    dyoung 		DPRINTF(sc, RTW_DEBUG_XMIT, ("%s: no frame ready\n",
   3170   1.42    dyoung 		    __func__));
   3171   1.42    dyoung 		return 0;
   3172   1.42    dyoung 	}
   3173   1.42    dyoung 
   3174   1.48    dyoung 	pri = ((m0->m_flags & M_PWR_SAV) != 0) ? RTW_TXPRIHI : RTW_TXPRIMD;
   3175   1.48    dyoung 
   3176   1.48    dyoung 	if (rtw_txring_choose(sc, tsbp, tdbp, pri) == -1) {
   3177   1.58    dyoung 		DPRINTF(sc, RTW_DEBUG_XMIT_RSRC, ("%s: no ring %d descriptor\n",
   3178   1.58    dyoung 		    __func__, pri));
   3179   1.42    dyoung 		*if_flagsp |= IFF_OACTIVE;
   3180   1.58    dyoung 		sc->sc_if.if_timer = 1;
   3181   1.42    dyoung 		return 0;
   3182   1.42    dyoung 	}
   3183   1.42    dyoung 
   3184   1.42    dyoung 	IFQ_DEQUEUE(&ifp->if_snd, m0);
   3185   1.42    dyoung 	if (m0 == NULL) {
   3186   1.48    dyoung 		DPRINTF(sc, RTW_DEBUG_XMIT, ("%s: no frame ready\n",
   3187   1.42    dyoung 		    __func__));
   3188   1.42    dyoung 		return 0;
   3189   1.42    dyoung 	}
   3190   1.42    dyoung 	DPRINTF(sc, RTW_DEBUG_XMIT, ("%s: dequeue data frame\n", __func__));
   3191   1.42    dyoung 	ifp->if_opackets++;
   3192  1.128   msaitoh 	bpf_mtap(ifp, m0, BPF_D_OUT);
   3193   1.48    dyoung 	eh = mtod(m0, struct ether_header *);
   3194   1.48    dyoung 	*nip = ieee80211_find_txnode(&sc->sc_ic, eh->ether_dhost);
   3195   1.48    dyoung 	if (*nip == NULL) {
   3196   1.48    dyoung 		/* NB: ieee80211_find_txnode does stat+msg */
   3197   1.48    dyoung 		m_freem(m0);
   3198   1.48    dyoung 		return -1;
   3199   1.48    dyoung 	}
   3200   1.48    dyoung 	if ((m0 = ieee80211_encap(&sc->sc_ic, m0, *nip)) == NULL) {
   3201   1.48    dyoung 		DPRINTF(sc, RTW_DEBUG_XMIT, ("%s: encap error\n", __func__));
   3202   1.42    dyoung 		ifp->if_oerrors++;
   3203   1.42    dyoung 		return -1;
   3204    1.1    dyoung 	}
   3205   1.21    dyoung 	DPRINTF(sc, RTW_DEBUG_XMIT, ("%s: leave\n", __func__));
   3206    1.1    dyoung 	*mp = m0;
   3207    1.1    dyoung 	return 0;
   3208    1.1    dyoung }
   3209    1.1    dyoung 
   3210   1.21    dyoung static int
   3211   1.21    dyoung rtw_seg_too_short(bus_dmamap_t dmamap)
   3212   1.21    dyoung {
   3213   1.21    dyoung 	int i;
   3214   1.21    dyoung 	for (i = 0; i < dmamap->dm_nsegs; i++) {
   3215   1.83    dyoung 		if (dmamap->dm_segs[i].ds_len < 4)
   3216   1.21    dyoung 			return 1;
   3217   1.21    dyoung 	}
   3218   1.21    dyoung 	return 0;
   3219   1.21    dyoung }
   3220   1.21    dyoung 
   3221    1.5    dyoung /* TBD factor with atw_start */
   3222    1.5    dyoung static struct mbuf *
   3223    1.5    dyoung rtw_dmamap_load_txbuf(bus_dma_tag_t dmat, bus_dmamap_t dmam, struct mbuf *chain,
   3224   1.98    dyoung     u_int ndescfree, device_t dev)
   3225    1.5    dyoung {
   3226    1.5    dyoung 	int first, rc;
   3227    1.5    dyoung 	struct mbuf *m, *m0;
   3228    1.5    dyoung 
   3229    1.5    dyoung 	m0 = chain;
   3230    1.5    dyoung 
   3231    1.5    dyoung 	/*
   3232    1.5    dyoung 	 * Load the DMA map.  Copy and try (once) again if the packet
   3233    1.5    dyoung 	 * didn't fit in the alloted number of segments.
   3234    1.5    dyoung 	 */
   3235    1.5    dyoung 	for (first = 1;
   3236    1.5    dyoung 	     ((rc = bus_dmamap_load_mbuf(dmat, dmam, m0,
   3237    1.5    dyoung 			  BUS_DMA_WRITE|BUS_DMA_NOWAIT)) != 0 ||
   3238   1.21    dyoung 	      dmam->dm_nsegs > ndescfree || rtw_seg_too_short(dmam)) && first;
   3239    1.5    dyoung 	     first = 0) {
   3240   1.83    dyoung 		if (rc == 0) {
   3241   1.83    dyoung #ifdef RTW_DIAGxxx
   3242   1.83    dyoung 			if (rtw_seg_too_short(dmam)) {
   3243   1.83    dyoung 				printf("%s: short segment, mbuf lengths:", __func__);
   3244   1.83    dyoung 				for (m = m0; m; m = m->m_next)
   3245   1.83    dyoung 					printf(" %d", m->m_len);
   3246   1.83    dyoung 				printf("\n");
   3247   1.83    dyoung 			}
   3248   1.83    dyoung #endif
   3249    1.5    dyoung 			bus_dmamap_unload(dmat, dmam);
   3250   1.83    dyoung 		}
   3251    1.5    dyoung 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   3252    1.5    dyoung 		if (m == NULL) {
   3253   1.98    dyoung 			aprint_error_dev(dev, "unable to allocate Tx mbuf\n");
   3254    1.5    dyoung 			break;
   3255    1.5    dyoung 		}
   3256    1.5    dyoung 		if (m0->m_pkthdr.len > MHLEN) {
   3257    1.5    dyoung 			MCLGET(m, M_DONTWAIT);
   3258    1.5    dyoung 			if ((m->m_flags & M_EXT) == 0) {
   3259   1.98    dyoung 				aprint_error_dev(dev,
   3260   1.98    dyoung 				    "cannot allocate Tx cluster\n");
   3261    1.5    dyoung 				m_freem(m);
   3262    1.5    dyoung 				break;
   3263    1.5    dyoung 			}
   3264    1.5    dyoung 		}
   3265   1.85  christos 		m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, void *));
   3266    1.5    dyoung 		m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
   3267    1.5    dyoung 		m_freem(m0);
   3268    1.5    dyoung 		m0 = m;
   3269    1.5    dyoung 		m = NULL;
   3270    1.5    dyoung 	}
   3271    1.5    dyoung 	if (rc != 0) {
   3272   1.98    dyoung 		aprint_error_dev(dev, "cannot load Tx buffer, rc = %d\n", rc);
   3273    1.5    dyoung 		m_freem(m0);
   3274    1.5    dyoung 		return NULL;
   3275   1.21    dyoung 	} else if (rtw_seg_too_short(dmam)) {
   3276   1.98    dyoung 		aprint_error_dev(dev,
   3277   1.98    dyoung 		    "cannot load Tx buffer, segment too short\n");
   3278   1.21    dyoung 		bus_dmamap_unload(dmat, dmam);
   3279   1.21    dyoung 		m_freem(m0);
   3280   1.21    dyoung 		return NULL;
   3281    1.5    dyoung 	} else if (dmam->dm_nsegs > ndescfree) {
   3282   1.98    dyoung 		aprint_error_dev(dev, "too many tx segments\n");
   3283    1.5    dyoung 		bus_dmamap_unload(dmat, dmam);
   3284    1.5    dyoung 		m_freem(m0);
   3285    1.5    dyoung 		return NULL;
   3286    1.5    dyoung 	}
   3287    1.5    dyoung 	return m0;
   3288    1.5    dyoung }
   3289    1.5    dyoung 
   3290   1.21    dyoung #ifdef RTW_DEBUG
   3291    1.1    dyoung static void
   3292   1.16    dyoung rtw_print_txdesc(struct rtw_softc *sc, const char *action,
   3293   1.34    dyoung     struct rtw_txsoft *ts, struct rtw_txdesc_blk *tdb, int desc)
   3294   1.16    dyoung {
   3295   1.34    dyoung 	struct rtw_txdesc *td = &tdb->tdb_desc[desc];
   3296   1.58    dyoung 	DPRINTF(sc, RTW_DEBUG_XMIT_DESC, ("%s: %p %s txdesc[%d] next %#08x "
   3297   1.58    dyoung 	    "buf %#08x ctl0 %#08x ctl1 %#08x len %#08x\n",
   3298   1.98    dyoung 	    device_xname(sc->sc_dev), ts, action, desc,
   3299   1.58    dyoung 	    le32toh(td->td_buf), le32toh(td->td_next),
   3300   1.58    dyoung 	    le32toh(td->td_ctl0), le32toh(td->td_ctl1),
   3301   1.34    dyoung 	    le32toh(td->td_len)));
   3302   1.16    dyoung }
   3303   1.21    dyoung #endif /* RTW_DEBUG */
   3304   1.16    dyoung 
   3305   1.16    dyoung static void
   3306    1.1    dyoung rtw_start(struct ifnet *ifp)
   3307    1.1    dyoung {
   3308    1.5    dyoung 	int desc, i, lastdesc, npkt, rate;
   3309   1.14    dyoung 	uint32_t proto_ctl0, ctl0, ctl1;
   3310    1.5    dyoung 	bus_dmamap_t		dmamap;
   3311    1.5    dyoung 	struct ieee80211com	*ic;
   3312    1.5    dyoung 	struct ieee80211_duration *d0;
   3313   1.49    dyoung 	struct ieee80211_frame_min	*wh;
   3314   1.73  christos 	struct ieee80211_node	*ni = NULL;	/* XXX: GCC */
   3315    1.5    dyoung 	struct mbuf		*m0;
   3316    1.5    dyoung 	struct rtw_softc	*sc;
   3317   1.73  christos 	struct rtw_txsoft_blk	*tsb = NULL;	/* XXX: GCC */
   3318   1.73  christos 	struct rtw_txdesc_blk	*tdb = NULL;	/* XXX: GCC */
   3319   1.34    dyoung 	struct rtw_txsoft	*ts;
   3320   1.34    dyoung 	struct rtw_txdesc	*td;
   3321   1.49    dyoung 	struct ieee80211_key	*k;
   3322    1.1    dyoung 
   3323    1.1    dyoung 	sc = (struct rtw_softc *)ifp->if_softc;
   3324    1.5    dyoung 	ic = &sc->sc_ic;
   3325    1.1    dyoung 
   3326   1.21    dyoung 	DPRINTF(sc, RTW_DEBUG_XMIT,
   3327   1.98    dyoung 	    ("%s: enter %s\n", device_xname(sc->sc_dev), __func__));
   3328    1.5    dyoung 
   3329   1.31    dyoung 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
   3330   1.31    dyoung 		goto out;
   3331   1.31    dyoung 
   3332    1.5    dyoung 	/* XXX do real rate control */
   3333   1.14    dyoung 	proto_ctl0 = RTW_TXCTL0_RTSRATE_1MBPS;
   3334    1.5    dyoung 
   3335    1.5    dyoung 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0)
   3336   1.14    dyoung 		proto_ctl0 |= RTW_TXCTL0_SPLCP;
   3337    1.5    dyoung 
   3338    1.5    dyoung 	for (;;) {
   3339   1.34    dyoung 		if (rtw_dequeue(ifp, &tsb, &tdb, &m0, &ni) == -1)
   3340    1.1    dyoung 			continue;
   3341    1.1    dyoung 		if (m0 == NULL)
   3342    1.1    dyoung 			break;
   3343   1.49    dyoung 
   3344   1.49    dyoung 		wh = mtod(m0, struct ieee80211_frame_min *);
   3345   1.49    dyoung 
   3346   1.49    dyoung 		if ((wh->i_fc[1] & IEEE80211_FC1_WEP) != 0 &&
   3347   1.49    dyoung 		    (k = ieee80211_crypto_encap(ic, ni, m0)) == NULL) {
   3348   1.51    dyoung 			m_freem(m0);
   3349   1.49    dyoung 			break;
   3350   1.49    dyoung 		} else
   3351   1.49    dyoung 			k = NULL;
   3352   1.49    dyoung 
   3353   1.34    dyoung 		ts = SIMPLEQ_FIRST(&tsb->tsb_freeq);
   3354    1.5    dyoung 
   3355   1.34    dyoung 		dmamap = ts->ts_dmamap;
   3356    1.5    dyoung 
   3357    1.5    dyoung 		m0 = rtw_dmamap_load_txbuf(sc->sc_dmat, dmamap, m0,
   3358   1.98    dyoung 		    tdb->tdb_nfree, sc->sc_dev);
   3359    1.5    dyoung 
   3360    1.5    dyoung 		if (m0 == NULL || dmamap->dm_nsegs == 0) {
   3361   1.21    dyoung 			DPRINTF(sc, RTW_DEBUG_XMIT,
   3362   1.21    dyoung 			    ("%s: fail dmamap load\n", __func__));
   3363    1.5    dyoung 			goto post_dequeue_err;
   3364    1.5    dyoung 		}
   3365    1.5    dyoung 
   3366   1.49    dyoung 		/* Note well: rtw_dmamap_load_txbuf may have created
   3367   1.49    dyoung 		 * a new chain, so we must find the header once
   3368   1.49    dyoung 		 * more.
   3369   1.49    dyoung 		 */
   3370   1.49    dyoung 		wh = mtod(m0, struct ieee80211_frame_min *);
   3371   1.45    dyoung 
   3372   1.45    dyoung 		/* XXX do real rate control */
   3373   1.45    dyoung 		if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
   3374   1.45    dyoung 		    IEEE80211_FC0_TYPE_MGT)
   3375   1.45    dyoung 			rate = 2;
   3376   1.45    dyoung 		else
   3377   1.72    dyoung 			rate = MAX(2, ieee80211_get_rate(ni));
   3378   1.45    dyoung 
   3379   1.16    dyoung #ifdef RTW_DEBUG
   3380   1.48    dyoung 		if ((ifp->if_flags & (IFF_DEBUG|IFF_LINK2)) ==
   3381   1.16    dyoung 		    (IFF_DEBUG|IFF_LINK2)) {
   3382   1.16    dyoung 			ieee80211_dump_pkt(mtod(m0, uint8_t *),
   3383   1.16    dyoung 			    (dmamap->dm_nsegs == 1) ? m0->m_pkthdr.len
   3384   1.16    dyoung 			                            : sizeof(wh),
   3385   1.16    dyoung 			    rate, 0);
   3386   1.16    dyoung 		}
   3387   1.16    dyoung #endif /* RTW_DEBUG */
   3388   1.14    dyoung 		ctl0 = proto_ctl0 |
   3389   1.75    dyoung 		    __SHIFTIN(m0->m_pkthdr.len, RTW_TXCTL0_TPKTSIZE_MASK);
   3390    1.5    dyoung 
   3391   1.45    dyoung 		switch (rate) {
   3392   1.42    dyoung 		default:
   3393   1.42    dyoung 		case 2:
   3394   1.42    dyoung 			ctl0 |= RTW_TXCTL0_RATE_1MBPS;
   3395   1.42    dyoung 			break;
   3396   1.42    dyoung 		case 4:
   3397   1.42    dyoung 			ctl0 |= RTW_TXCTL0_RATE_2MBPS;
   3398   1.42    dyoung 			break;
   3399   1.42    dyoung 		case 11:
   3400   1.42    dyoung 			ctl0 |= RTW_TXCTL0_RATE_5MBPS;
   3401   1.42    dyoung 			break;
   3402   1.42    dyoung 		case 22:
   3403   1.42    dyoung 			ctl0 |= RTW_TXCTL0_RATE_11MBPS;
   3404   1.42    dyoung 			break;
   3405   1.42    dyoung 		}
   3406   1.45    dyoung 		/* XXX >= ? Compare after fragmentation? */
   3407   1.45    dyoung 		if (m0->m_pkthdr.len > ic->ic_rtsthreshold)
   3408   1.45    dyoung 			ctl0 |= RTW_TXCTL0_RTSEN;
   3409   1.45    dyoung 
   3410   1.66    dyoung                 /* XXX Sometimes writes a bogus keyid; h/w doesn't
   3411   1.66    dyoung                  * seem to care, since we don't activate h/w Tx
   3412   1.66    dyoung                  * encryption.
   3413   1.66    dyoung 		 */
   3414  1.107    dyoung 		if (k != NULL &&
   3415  1.107    dyoung 		    k->wk_cipher->ic_cipher == IEEE80211_CIPHER_WEP) {
   3416   1.75    dyoung 			ctl0 |= __SHIFTIN(k->wk_keyix, RTW_TXCTL0_KEYID_MASK) &
   3417   1.49    dyoung 			    RTW_TXCTL0_KEYID_MASK;
   3418   1.49    dyoung 		}
   3419   1.42    dyoung 
   3420   1.45    dyoung 		if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
   3421   1.45    dyoung 		    IEEE80211_FC0_TYPE_MGT) {
   3422   1.45    dyoung 			ctl0 &= ~(RTW_TXCTL0_SPLCP | RTW_TXCTL0_RTSEN);
   3423   1.45    dyoung 			if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
   3424   1.45    dyoung 			    IEEE80211_FC0_SUBTYPE_BEACON)
   3425   1.45    dyoung 				ctl0 |= RTW_TXCTL0_BEACON;
   3426   1.45    dyoung 		}
   3427   1.45    dyoung 
   3428   1.62    dyoung 		if (ieee80211_compute_duration(wh, k, m0->m_pkthdr.len,
   3429    1.5    dyoung 		    ic->ic_flags, ic->ic_fragthreshold,
   3430   1.34    dyoung 		    rate, &ts->ts_d0, &ts->ts_dn, &npkt,
   3431   1.48    dyoung 		    (ifp->if_flags & (IFF_DEBUG|IFF_LINK2)) ==
   3432   1.19    dyoung 		    (IFF_DEBUG|IFF_LINK2)) == -1) {
   3433   1.21    dyoung 			DPRINTF(sc, RTW_DEBUG_XMIT,
   3434   1.21    dyoung 			    ("%s: fail compute duration\n", __func__));
   3435    1.5    dyoung 			goto post_load_err;
   3436    1.5    dyoung 		}
   3437    1.5    dyoung 
   3438   1.34    dyoung 		d0 = &ts->ts_d0;
   3439    1.5    dyoung 
   3440   1.20    dyoung 		*(uint16_t*)wh->i_dur = htole16(d0->d_data_dur);
   3441   1.20    dyoung 
   3442   1.75    dyoung 		ctl1 = __SHIFTIN(d0->d_plcp_len, RTW_TXCTL1_LENGTH_MASK) |
   3443   1.75    dyoung 		    __SHIFTIN(d0->d_rts_dur, RTW_TXCTL1_RTSDUR_MASK);
   3444    1.5    dyoung 
   3445   1.25   mycroft 		if (d0->d_residue)
   3446   1.14    dyoung 			ctl1 |= RTW_TXCTL1_LENGEXT;
   3447    1.5    dyoung 
   3448    1.5    dyoung 		/* TBD fragmentation */
   3449    1.5    dyoung 
   3450   1.34    dyoung 		ts->ts_first = tdb->tdb_next;
   3451    1.5    dyoung 
   3452   1.34    dyoung 		rtw_txdescs_sync(tdb, ts->ts_first, dmamap->dm_nsegs,
   3453    1.5    dyoung 		    BUS_DMASYNC_PREWRITE);
   3454    1.5    dyoung 
   3455   1.34    dyoung 		KASSERT(ts->ts_first < tdb->tdb_ndesc);
   3456   1.21    dyoung 
   3457  1.128   msaitoh 		bpf_mtap3(ic->ic_rawbpf, m0, BPF_D_OUT);
   3458   1.32    dyoung 
   3459   1.32    dyoung 		if (sc->sc_radiobpf != NULL) {
   3460   1.32    dyoung 			struct rtw_tx_radiotap_header *rt = &sc->sc_txtap;
   3461   1.32    dyoung 
   3462   1.32    dyoung 			rt->rt_rate = rate;
   3463   1.32    dyoung 
   3464  1.116     joerg 			bpf_mtap2(sc->sc_radiobpf, rt, sizeof(sc->sc_txtapu),
   3465  1.128   msaitoh 			    m0, BPF_D_OUT);
   3466   1.32    dyoung 		}
   3467   1.32    dyoung 
   3468   1.34    dyoung 		for (i = 0, lastdesc = desc = ts->ts_first;
   3469    1.5    dyoung 		     i < dmamap->dm_nsegs;
   3470   1.34    dyoung 		     i++, desc = RTW_NEXT_IDX(tdb, desc)) {
   3471    1.5    dyoung 			if (dmamap->dm_segs[i].ds_len > RTW_TXLEN_LENGTH_MASK) {
   3472   1.21    dyoung 				DPRINTF(sc, RTW_DEBUG_XMIT_DESC,
   3473   1.21    dyoung 				    ("%s: seg too long\n", __func__));
   3474    1.5    dyoung 				goto post_load_err;
   3475    1.5    dyoung 			}
   3476   1.34    dyoung 			td = &tdb->tdb_desc[desc];
   3477   1.34    dyoung 			td->td_ctl0 = htole32(ctl0);
   3478   1.34    dyoung 			td->td_ctl1 = htole32(ctl1);
   3479   1.34    dyoung 			td->td_buf = htole32(dmamap->dm_segs[i].ds_addr);
   3480   1.34    dyoung 			td->td_len = htole32(dmamap->dm_segs[i].ds_len);
   3481   1.92    dyoung 			td->td_next = htole32(RTW_NEXT_DESC(tdb, desc));
   3482   1.92    dyoung 			if (i != 0)
   3483   1.92    dyoung 				td->td_ctl0 |= htole32(RTW_TXCTL0_OWN);
   3484    1.5    dyoung 			lastdesc = desc;
   3485   1.16    dyoung #ifdef RTW_DEBUG
   3486   1.34    dyoung 			rtw_print_txdesc(sc, "load", ts, tdb, desc);
   3487   1.16    dyoung #endif /* RTW_DEBUG */
   3488    1.5    dyoung 		}
   3489    1.5    dyoung 
   3490   1.34    dyoung 		KASSERT(desc < tdb->tdb_ndesc);
   3491   1.21    dyoung 
   3492   1.34    dyoung 		ts->ts_ni = ni;
   3493   1.48    dyoung 		KASSERT(ni != NULL);
   3494   1.34    dyoung 		ts->ts_mbuf = m0;
   3495   1.34    dyoung 		ts->ts_last = lastdesc;
   3496   1.34    dyoung 		tdb->tdb_desc[ts->ts_last].td_ctl0 |= htole32(RTW_TXCTL0_LS);
   3497   1.34    dyoung 		tdb->tdb_desc[ts->ts_first].td_ctl0 |=
   3498    1.5    dyoung 		   htole32(RTW_TXCTL0_FS);
   3499    1.5    dyoung 
   3500   1.16    dyoung #ifdef RTW_DEBUG
   3501   1.34    dyoung 		rtw_print_txdesc(sc, "FS on", ts, tdb, ts->ts_first);
   3502   1.34    dyoung 		rtw_print_txdesc(sc, "LS on", ts, tdb, ts->ts_last);
   3503   1.16    dyoung #endif /* RTW_DEBUG */
   3504    1.5    dyoung 
   3505   1.34    dyoung 		tdb->tdb_nfree -= dmamap->dm_nsegs;
   3506   1.34    dyoung 		tdb->tdb_next = desc;
   3507    1.5    dyoung 
   3508   1.34    dyoung 		rtw_txdescs_sync(tdb, ts->ts_first, dmamap->dm_nsegs,
   3509    1.5    dyoung 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   3510    1.5    dyoung 
   3511   1.34    dyoung 		tdb->tdb_desc[ts->ts_first].td_ctl0 |=
   3512    1.5    dyoung 		    htole32(RTW_TXCTL0_OWN);
   3513    1.5    dyoung 
   3514   1.16    dyoung #ifdef RTW_DEBUG
   3515   1.34    dyoung 		rtw_print_txdesc(sc, "OWN on", ts, tdb, ts->ts_first);
   3516   1.16    dyoung #endif /* RTW_DEBUG */
   3517    1.5    dyoung 
   3518   1.34    dyoung 		rtw_txdescs_sync(tdb, ts->ts_first, 1,
   3519    1.5    dyoung 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   3520    1.5    dyoung 
   3521   1.34    dyoung 		SIMPLEQ_REMOVE_HEAD(&tsb->tsb_freeq, ts_q);
   3522   1.34    dyoung 		SIMPLEQ_INSERT_TAIL(&tsb->tsb_dirtyq, ts, ts_q);
   3523    1.5    dyoung 
   3524   1.58    dyoung 		if (tsb != &sc->sc_txsoft_blk[RTW_TXPRIBCN])
   3525   1.42    dyoung 			sc->sc_led_state.ls_event |= RTW_LED_S_TX;
   3526   1.58    dyoung 		tsb->tsb_tx_timer = 5;
   3527   1.58    dyoung 		ifp->if_timer = 1;
   3528  1.103    dyoung 		rtw_tx_kick(&sc->sc_regs, tsb->tsb_poll);
   3529    1.1    dyoung 	}
   3530   1.31    dyoung out:
   3531   1.21    dyoung 	DPRINTF(sc, RTW_DEBUG_XMIT, ("%s: leave\n", __func__));
   3532    1.5    dyoung 	return;
   3533    1.5    dyoung post_load_err:
   3534    1.5    dyoung 	bus_dmamap_unload(sc->sc_dmat, dmamap);
   3535    1.5    dyoung 	m_freem(m0);
   3536    1.5    dyoung post_dequeue_err:
   3537   1.48    dyoung 	ieee80211_free_node(ni);
   3538    1.1    dyoung 	return;
   3539    1.1    dyoung }
   3540    1.1    dyoung 
   3541    1.1    dyoung static void
   3542   1.58    dyoung rtw_idle(struct rtw_regs *regs)
   3543   1.58    dyoung {
   3544   1.58    dyoung 	int active;
   3545  1.100    dyoung 	uint8_t tppoll;
   3546   1.58    dyoung 
   3547   1.58    dyoung 	/* request stop DMA; wait for packets to stop transmitting. */
   3548   1.58    dyoung 
   3549   1.58    dyoung 	RTW_WRITE8(regs, RTW_TPPOLL, RTW_TPPOLL_SALL);
   3550   1.58    dyoung 	RTW_WBR(regs, RTW_TPPOLL, RTW_TPPOLL);
   3551   1.58    dyoung 
   3552   1.58    dyoung 	for (active = 0; active < 300 &&
   3553  1.100    dyoung 	     (tppoll = RTW_READ8(regs, RTW_TPPOLL) & RTW_TPPOLL_ACTIVE) != 0;
   3554  1.100    dyoung 	     active++)
   3555   1.58    dyoung 		DELAY(10);
   3556  1.100    dyoung 	printf("%s: transmit DMA idle in %dus, tppoll %02" PRIx8 "\n", __func__,
   3557  1.100    dyoung 	    active * 10, tppoll);
   3558   1.58    dyoung }
   3559   1.58    dyoung 
   3560   1.58    dyoung static void
   3561    1.1    dyoung rtw_watchdog(struct ifnet *ifp)
   3562    1.1    dyoung {
   3563   1.58    dyoung 	int pri, tx_timeouts = 0;
   3564    1.5    dyoung 	struct rtw_softc *sc;
   3565   1.34    dyoung 	struct rtw_txsoft_blk *tsb;
   3566    1.5    dyoung 
   3567    1.5    dyoung 	sc = ifp->if_softc;
   3568    1.5    dyoung 
   3569    1.5    dyoung 	ifp->if_timer = 0;
   3570    1.5    dyoung 
   3571  1.101    dyoung 	if (!device_is_active(sc->sc_dev))
   3572    1.5    dyoung 		return;
   3573    1.5    dyoung 
   3574    1.5    dyoung 	for (pri = 0; pri < RTW_NTXPRI; pri++) {
   3575   1.34    dyoung 		tsb = &sc->sc_txsoft_blk[pri];
   3576    1.5    dyoung 
   3577   1.34    dyoung 		if (tsb->tsb_tx_timer == 0)
   3578    1.5    dyoung 			continue;
   3579   1.58    dyoung 		else if (--tsb->tsb_tx_timer == 0) {
   3580   1.34    dyoung 			if (SIMPLEQ_EMPTY(&tsb->tsb_dirtyq))
   3581    1.5    dyoung 				continue;
   3582  1.107    dyoung 			else if (rtw_collect_txring(sc, tsb,
   3583  1.107    dyoung 			    &sc->sc_txdesc_blk[pri], 0))
   3584  1.107    dyoung 				continue;
   3585    1.5    dyoung 			printf("%s: transmit timeout, priority %d\n",
   3586    1.5    dyoung 			    ifp->if_xname, pri);
   3587    1.5    dyoung 			ifp->if_oerrors++;
   3588   1.83    dyoung 			if (pri != RTW_TXPRIBCN)
   3589   1.83    dyoung 				tx_timeouts++;
   3590    1.5    dyoung 		} else
   3591    1.5    dyoung 			ifp->if_timer = 1;
   3592    1.5    dyoung 	}
   3593   1.58    dyoung 
   3594   1.58    dyoung 	if (tx_timeouts > 0) {
   3595   1.58    dyoung 		/* Stop Tx DMA, disable xmtr, flush Tx rings, enable xmtr,
   3596   1.58    dyoung 		 * reset s/w tx-ring pointers, and start transmission.
   3597   1.58    dyoung 		 *
   3598   1.58    dyoung 		 * TBD Stop/restart just the broken rings?
   3599   1.58    dyoung 		 */
   3600   1.58    dyoung 		rtw_idle(&sc->sc_regs);
   3601  1.107    dyoung 		rtw_io_enable(sc, RTW_CR_RE | RTW_CR_TE, 0);
   3602   1.58    dyoung 		rtw_txdescs_reset(sc);
   3603  1.107    dyoung 		rtw_io_enable(sc, RTW_CR_RE | RTW_CR_TE, 1);
   3604   1.58    dyoung 		rtw_start(ifp);
   3605   1.58    dyoung 	}
   3606   1.48    dyoung 	ieee80211_watchdog(&sc->sc_ic);
   3607    1.1    dyoung 	return;
   3608    1.1    dyoung }
   3609    1.1    dyoung 
   3610    1.1    dyoung static void
   3611    1.1    dyoung rtw_next_scan(void *arg)
   3612    1.1    dyoung {
   3613    1.1    dyoung 	struct ieee80211com *ic = arg;
   3614    1.1    dyoung 	int s;
   3615    1.1    dyoung 
   3616    1.1    dyoung 	/* don't call rtw_start w/o network interrupts blocked */
   3617    1.1    dyoung 	s = splnet();
   3618    1.1    dyoung 	if (ic->ic_state == IEEE80211_S_SCAN)
   3619    1.1    dyoung 		ieee80211_next_scan(ic);
   3620    1.1    dyoung 	splx(s);
   3621    1.1    dyoung }
   3622    1.1    dyoung 
   3623   1.10    dyoung static void
   3624   1.45    dyoung rtw_join_bss(struct rtw_softc *sc, uint8_t *bssid, uint16_t intval0)
   3625   1.10    dyoung {
   3626   1.58    dyoung 	uint16_t bcnitv, bintritv, intval;
   3627   1.10    dyoung 	int i;
   3628   1.10    dyoung 	struct rtw_regs *regs = &sc->sc_regs;
   3629   1.10    dyoung 
   3630   1.10    dyoung 	for (i = 0; i < IEEE80211_ADDR_LEN; i++)
   3631   1.10    dyoung 		RTW_WRITE8(regs, RTW_BSSID + i, bssid[i]);
   3632   1.10    dyoung 
   3633   1.10    dyoung 	RTW_SYNC(regs, RTW_BSSID16, RTW_BSSID32);
   3634   1.10    dyoung 
   3635   1.42    dyoung 	rtw_set_access(regs, RTW_ACCESS_CONFIG);
   3636   1.10    dyoung 
   3637   1.75    dyoung 	intval = MIN(intval0, __SHIFTOUT_MASK(RTW_BCNITV_BCNITV_MASK));
   3638   1.10    dyoung 
   3639   1.10    dyoung 	bcnitv = RTW_READ16(regs, RTW_BCNITV) & ~RTW_BCNITV_BCNITV_MASK;
   3640   1.75    dyoung 	bcnitv |= __SHIFTIN(intval, RTW_BCNITV_BCNITV_MASK);
   3641   1.10    dyoung 	RTW_WRITE16(regs, RTW_BCNITV, bcnitv);
   3642   1.58    dyoung 	/* interrupt host 1ms before the TBTT */
   3643   1.58    dyoung 	bintritv = RTW_READ16(regs, RTW_BINTRITV) & ~RTW_BINTRITV_BINTRITV;
   3644   1.75    dyoung 	bintritv |= __SHIFTIN(1000, RTW_BINTRITV_BINTRITV);
   3645   1.58    dyoung 	RTW_WRITE16(regs, RTW_BINTRITV, bintritv);
   3646   1.10    dyoung 	/* magic from Linux */
   3647   1.75    dyoung 	RTW_WRITE16(regs, RTW_ATIMWND, __SHIFTIN(1, RTW_ATIMWND_ATIMWND));
   3648   1.75    dyoung 	RTW_WRITE16(regs, RTW_ATIMTRITV, __SHIFTIN(2, RTW_ATIMTRITV_ATIMTRITV));
   3649   1.42    dyoung 	rtw_set_access(regs, RTW_ACCESS_NONE);
   3650   1.10    dyoung 
   3651   1.83    dyoung 	rtw_io_enable(sc, RTW_CR_RE | RTW_CR_TE, 1);
   3652   1.10    dyoung }
   3653   1.10    dyoung 
   3654    1.1    dyoung /* Synchronize the hardware state with the software state. */
   3655    1.1    dyoung static int
   3656    1.1    dyoung rtw_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
   3657    1.1    dyoung {
   3658   1.48    dyoung 	struct ifnet *ifp = ic->ic_ifp;
   3659   1.48    dyoung 	struct rtw_softc *sc = (struct rtw_softc *)ifp->if_softc;
   3660    1.1    dyoung 	enum ieee80211_state ostate;
   3661    1.1    dyoung 	int error;
   3662    1.1    dyoung 
   3663    1.1    dyoung 	ostate = ic->ic_state;
   3664    1.1    dyoung 
   3665  1.101    dyoung 	aprint_debug_dev(sc->sc_dev, "%s: l.%d\n", __func__, __LINE__);
   3666   1.42    dyoung 	rtw_led_newstate(sc, nstate);
   3667   1.42    dyoung 
   3668  1.101    dyoung 	aprint_debug_dev(sc->sc_dev, "%s: l.%d\n", __func__, __LINE__);
   3669    1.1    dyoung 	if (nstate == IEEE80211_S_INIT) {
   3670    1.1    dyoung 		callout_stop(&sc->sc_scan_ch);
   3671    1.1    dyoung 		sc->sc_cur_chan = IEEE80211_CHAN_ANY;
   3672    1.1    dyoung 		return (*sc->sc_mtbl.mt_newstate)(ic, nstate, arg);
   3673    1.1    dyoung 	}
   3674    1.1    dyoung 
   3675    1.1    dyoung 	if (ostate == IEEE80211_S_INIT && nstate != IEEE80211_S_INIT)
   3676    1.1    dyoung 		rtw_pwrstate(sc, RTW_ON);
   3677    1.1    dyoung 
   3678    1.1    dyoung 	if ((error = rtw_tune(sc)) != 0)
   3679    1.1    dyoung 		return error;
   3680    1.1    dyoung 
   3681    1.1    dyoung 	switch (nstate) {
   3682    1.1    dyoung 	case IEEE80211_S_INIT:
   3683    1.1    dyoung 		panic("%s: unexpected state IEEE80211_S_INIT\n", __func__);
   3684    1.1    dyoung 		break;
   3685    1.1    dyoung 	case IEEE80211_S_SCAN:
   3686   1.21    dyoung 		if (ostate != IEEE80211_S_SCAN) {
   3687   1.21    dyoung 			(void)memset(ic->ic_bss->ni_bssid, 0,
   3688   1.21    dyoung 			    IEEE80211_ADDR_LEN);
   3689   1.45    dyoung 			rtw_set_nettype(sc, IEEE80211_M_MONITOR);
   3690   1.21    dyoung 		}
   3691    1.1    dyoung 
   3692    1.1    dyoung 		callout_reset(&sc->sc_scan_ch, rtw_dwelltime * hz / 1000,
   3693    1.1    dyoung 		    rtw_next_scan, ic);
   3694    1.1    dyoung 
   3695    1.1    dyoung 		break;
   3696    1.1    dyoung 	case IEEE80211_S_RUN:
   3697   1.38    dyoung 		switch (ic->ic_opmode) {
   3698   1.38    dyoung 		case IEEE80211_M_HOSTAP:
   3699   1.38    dyoung 		case IEEE80211_M_IBSS:
   3700   1.45    dyoung 			rtw_set_nettype(sc, IEEE80211_M_MONITOR);
   3701   1.42    dyoung 			/*FALLTHROUGH*/
   3702   1.42    dyoung 		case IEEE80211_M_AHDEMO:
   3703   1.45    dyoung 		case IEEE80211_M_STA:
   3704   1.45    dyoung 			rtw_join_bss(sc, ic->ic_bss->ni_bssid,
   3705   1.38    dyoung 			    ic->ic_bss->ni_intval);
   3706    1.1    dyoung 			break;
   3707   1.38    dyoung 		case IEEE80211_M_MONITOR:
   3708   1.38    dyoung 			break;
   3709   1.38    dyoung 		}
   3710   1.45    dyoung 		rtw_set_nettype(sc, ic->ic_opmode);
   3711   1.38    dyoung 		break;
   3712   1.45    dyoung 	case IEEE80211_S_ASSOC:
   3713    1.1    dyoung 	case IEEE80211_S_AUTH:
   3714    1.1    dyoung 		break;
   3715    1.1    dyoung 	}
   3716    1.1    dyoung 
   3717    1.1    dyoung 	if (nstate != IEEE80211_S_SCAN)
   3718    1.1    dyoung 		callout_stop(&sc->sc_scan_ch);
   3719    1.1    dyoung 
   3720    1.1    dyoung 	return (*sc->sc_mtbl.mt_newstate)(ic, nstate, arg);
   3721    1.1    dyoung }
   3722    1.1    dyoung 
   3723   1.40    dyoung /* Extend a 32-bit TSF timestamp to a 64-bit timestamp. */
   3724   1.40    dyoung static uint64_t
   3725   1.40    dyoung rtw_tsf_extend(struct rtw_regs *regs, uint32_t rstamp)
   3726   1.40    dyoung {
   3727   1.40    dyoung 	uint32_t tsftl, tsfth;
   3728   1.40    dyoung 
   3729   1.40    dyoung 	tsfth = RTW_READ(regs, RTW_TSFTRH);
   3730   1.40    dyoung 	tsftl = RTW_READ(regs, RTW_TSFTRL);
   3731   1.40    dyoung 	if (tsftl < rstamp)	/* Compensate for rollover. */
   3732   1.40    dyoung 		tsfth--;
   3733   1.40    dyoung 	return ((uint64_t)tsfth << 32) | rstamp;
   3734   1.40    dyoung }
   3735   1.40    dyoung 
   3736    1.1    dyoung static void
   3737    1.1    dyoung rtw_recv_mgmt(struct ieee80211com *ic, struct mbuf *m,
   3738   1.37    dyoung     struct ieee80211_node *ni, int subtype, int rssi, uint32_t rstamp)
   3739    1.1    dyoung {
   3740   1.48    dyoung 	struct ifnet *ifp = ic->ic_ifp;
   3741   1.48    dyoung 	struct rtw_softc *sc = (struct rtw_softc *)ifp->if_softc;
   3742    1.1    dyoung 
   3743   1.40    dyoung 	(*sc->sc_mtbl.mt_recv_mgmt)(ic, m, ni, subtype, rssi, rstamp);
   3744   1.40    dyoung 
   3745    1.1    dyoung 	switch (subtype) {
   3746    1.1    dyoung 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
   3747    1.1    dyoung 	case IEEE80211_FC0_SUBTYPE_BEACON:
   3748   1.63    dyoung 		if (ic->ic_opmode == IEEE80211_M_IBSS &&
   3749  1.101    dyoung 		    ic->ic_state == IEEE80211_S_RUN &&
   3750  1.101    dyoung 		    device_is_active(sc->sc_dev)) {
   3751   1.63    dyoung 			uint64_t tsf = rtw_tsf_extend(&sc->sc_regs, rstamp);
   3752   1.63    dyoung 			if (le64toh(ni->ni_tstamp.tsf) >= tsf)
   3753   1.63    dyoung 				(void)ieee80211_ibss_merge(ni);
   3754   1.63    dyoung 		}
   3755    1.1    dyoung 		break;
   3756    1.1    dyoung 	default:
   3757    1.1    dyoung 		break;
   3758    1.1    dyoung 	}
   3759    1.1    dyoung 	return;
   3760    1.1    dyoung }
   3761    1.1    dyoung 
   3762    1.1    dyoung static struct ieee80211_node *
   3763   1.48    dyoung rtw_node_alloc(struct ieee80211_node_table *nt)
   3764    1.1    dyoung {
   3765   1.48    dyoung 	struct ifnet *ifp = nt->nt_ic->ic_ifp;
   3766   1.48    dyoung 	struct rtw_softc *sc = (struct rtw_softc *)ifp->if_softc;
   3767   1.48    dyoung 	struct ieee80211_node *ni = (*sc->sc_mtbl.mt_node_alloc)(nt);
   3768    1.1    dyoung 
   3769   1.21    dyoung 	DPRINTF(sc, RTW_DEBUG_NODE,
   3770   1.98    dyoung 	    ("%s: alloc node %p\n", device_xname(sc->sc_dev), ni));
   3771    1.1    dyoung 	return ni;
   3772    1.1    dyoung }
   3773    1.1    dyoung 
   3774    1.1    dyoung static void
   3775   1.48    dyoung rtw_node_free(struct ieee80211_node *ni)
   3776    1.1    dyoung {
   3777   1.48    dyoung 	struct ieee80211com *ic = ni->ni_ic;
   3778   1.48    dyoung 	struct ifnet *ifp = ic->ic_ifp;
   3779   1.48    dyoung 	struct rtw_softc *sc = (struct rtw_softc *)ifp->if_softc;
   3780    1.1    dyoung 
   3781   1.21    dyoung 	DPRINTF(sc, RTW_DEBUG_NODE,
   3782   1.98    dyoung 	    ("%s: freeing node %p %s\n", device_xname(sc->sc_dev), ni,
   3783    1.1    dyoung 	    ether_sprintf(ni->ni_bssid)));
   3784   1.48    dyoung 	(*sc->sc_mtbl.mt_node_free)(ni);
   3785    1.1    dyoung }
   3786    1.1    dyoung 
   3787    1.1    dyoung static int
   3788    1.1    dyoung rtw_media_change(struct ifnet *ifp)
   3789    1.1    dyoung {
   3790    1.1    dyoung 	int error;
   3791    1.1    dyoung 
   3792    1.1    dyoung 	error = ieee80211_media_change(ifp);
   3793    1.1    dyoung 	if (error == ENETRESET) {
   3794    1.1    dyoung 		if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) ==
   3795    1.1    dyoung 		    (IFF_RUNNING|IFF_UP))
   3796    1.1    dyoung 			rtw_init(ifp);		/* XXX lose error */
   3797    1.1    dyoung 		error = 0;
   3798    1.1    dyoung 	}
   3799    1.1    dyoung 	return error;
   3800    1.1    dyoung }
   3801    1.1    dyoung 
   3802    1.1    dyoung static void
   3803    1.1    dyoung rtw_media_status(struct ifnet *ifp, struct ifmediareq *imr)
   3804    1.1    dyoung {
   3805    1.1    dyoung 	struct rtw_softc *sc = ifp->if_softc;
   3806    1.1    dyoung 
   3807  1.101    dyoung 	if (!device_is_active(sc->sc_dev)) {
   3808    1.1    dyoung 		imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
   3809    1.1    dyoung 		imr->ifm_status = 0;
   3810    1.1    dyoung 		return;
   3811    1.1    dyoung 	}
   3812    1.1    dyoung 	ieee80211_media_status(ifp, imr);
   3813    1.1    dyoung }
   3814    1.1    dyoung 
   3815   1.61     perry static inline void
   3816    1.7    dyoung rtw_setifprops(struct ifnet *ifp, const char *dvname, void *softc)
   3817    1.1    dyoung {
   3818   1.98    dyoung 	(void)strlcpy(ifp->if_xname, dvname, IFNAMSIZ);
   3819    1.1    dyoung 	ifp->if_softc = softc;
   3820    1.1    dyoung 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST |
   3821    1.1    dyoung 	    IFF_NOTRAILERS;
   3822    1.1    dyoung 	ifp->if_ioctl = rtw_ioctl;
   3823    1.1    dyoung 	ifp->if_start = rtw_start;
   3824    1.1    dyoung 	ifp->if_watchdog = rtw_watchdog;
   3825    1.1    dyoung 	ifp->if_init = rtw_init;
   3826    1.1    dyoung 	ifp->if_stop = rtw_stop;
   3827    1.1    dyoung }
   3828    1.1    dyoung 
   3829   1.61     perry static inline void
   3830    1.1    dyoung rtw_set80211props(struct ieee80211com *ic)
   3831    1.1    dyoung {
   3832    1.1    dyoung 	int nrate;
   3833    1.1    dyoung 	ic->ic_phytype = IEEE80211_T_DS;
   3834    1.1    dyoung 	ic->ic_opmode = IEEE80211_M_STA;
   3835    1.1    dyoung 	ic->ic_caps = IEEE80211_C_PMGT | IEEE80211_C_IBSS |
   3836  1.107    dyoung 	    IEEE80211_C_HOSTAP | IEEE80211_C_MONITOR | IEEE80211_C_WEP;
   3837    1.1    dyoung 
   3838    1.1    dyoung 	nrate = 0;
   3839   1.12    dyoung 	ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[nrate++] =
   3840   1.12    dyoung 	    IEEE80211_RATE_BASIC | 2;
   3841   1.12    dyoung 	ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[nrate++] =
   3842   1.12    dyoung 	    IEEE80211_RATE_BASIC | 4;
   3843    1.1    dyoung 	ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[nrate++] = 11;
   3844    1.1    dyoung 	ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[nrate++] = 22;
   3845    1.1    dyoung 	ic->ic_sup_rates[IEEE80211_MODE_11B].rs_nrates = nrate;
   3846    1.1    dyoung }
   3847    1.1    dyoung 
   3848   1.61     perry static inline void
   3849    1.1    dyoung rtw_set80211methods(struct rtw_mtbl *mtbl, struct ieee80211com *ic)
   3850    1.1    dyoung {
   3851    1.1    dyoung 	mtbl->mt_newstate = ic->ic_newstate;
   3852    1.1    dyoung 	ic->ic_newstate = rtw_newstate;
   3853    1.1    dyoung 
   3854    1.1    dyoung 	mtbl->mt_recv_mgmt = ic->ic_recv_mgmt;
   3855    1.1    dyoung 	ic->ic_recv_mgmt = rtw_recv_mgmt;
   3856    1.1    dyoung 
   3857    1.1    dyoung 	mtbl->mt_node_free = ic->ic_node_free;
   3858    1.1    dyoung 	ic->ic_node_free = rtw_node_free;
   3859    1.1    dyoung 
   3860    1.1    dyoung 	mtbl->mt_node_alloc = ic->ic_node_alloc;
   3861    1.1    dyoung 	ic->ic_node_alloc = rtw_node_alloc;
   3862   1.49    dyoung 
   3863   1.49    dyoung 	ic->ic_crypto.cs_key_delete = rtw_key_delete;
   3864   1.49    dyoung 	ic->ic_crypto.cs_key_set = rtw_key_set;
   3865   1.49    dyoung 	ic->ic_crypto.cs_key_update_begin = rtw_key_update_begin;
   3866   1.49    dyoung 	ic->ic_crypto.cs_key_update_end = rtw_key_update_end;
   3867    1.1    dyoung }
   3868    1.1    dyoung 
   3869   1.61     perry static inline void
   3870    1.1    dyoung rtw_init_radiotap(struct rtw_softc *sc)
   3871    1.1    dyoung {
   3872   1.93    dyoung 	uint32_t present;
   3873   1.93    dyoung 
   3874    1.1    dyoung 	memset(&sc->sc_rxtapu, 0, sizeof(sc->sc_rxtapu));
   3875   1.32    dyoung 	sc->sc_rxtap.rr_ihdr.it_len = htole16(sizeof(sc->sc_rxtapu));
   3876   1.93    dyoung 
   3877   1.93    dyoung 	if (sc->sc_rfchipid == RTW_RFCHIPID_PHILIPS)
   3878   1.93    dyoung 		present = htole32(RTW_PHILIPS_RX_RADIOTAP_PRESENT);
   3879   1.93    dyoung 	else
   3880   1.93    dyoung 		present = htole32(RTW_RX_RADIOTAP_PRESENT);
   3881   1.93    dyoung 	sc->sc_rxtap.rr_ihdr.it_present = present;
   3882    1.1    dyoung 
   3883    1.1    dyoung 	memset(&sc->sc_txtapu, 0, sizeof(sc->sc_txtapu));
   3884   1.32    dyoung 	sc->sc_txtap.rt_ihdr.it_len = htole16(sizeof(sc->sc_txtapu));
   3885   1.32    dyoung 	sc->sc_txtap.rt_ihdr.it_present = htole32(RTW_TX_RADIOTAP_PRESENT);
   3886    1.1    dyoung }
   3887    1.1    dyoung 
   3888    1.1    dyoung static int
   3889   1.34    dyoung rtw_txsoft_blk_setup(struct rtw_txsoft_blk *tsb, u_int qlen)
   3890    1.1    dyoung {
   3891   1.34    dyoung 	SIMPLEQ_INIT(&tsb->tsb_dirtyq);
   3892   1.34    dyoung 	SIMPLEQ_INIT(&tsb->tsb_freeq);
   3893   1.34    dyoung 	tsb->tsb_ndesc = qlen;
   3894   1.34    dyoung 	tsb->tsb_desc = malloc(qlen * sizeof(*tsb->tsb_desc), M_DEVBUF,
   3895    1.1    dyoung 	    M_NOWAIT);
   3896   1.34    dyoung 	if (tsb->tsb_desc == NULL)
   3897    1.1    dyoung 		return ENOMEM;
   3898    1.1    dyoung 	return 0;
   3899    1.1    dyoung }
   3900    1.1    dyoung 
   3901    1.1    dyoung static void
   3902   1.34    dyoung rtw_txsoft_blk_cleanup_all(struct rtw_softc *sc)
   3903    1.1    dyoung {
   3904   1.21    dyoung 	int pri;
   3905   1.34    dyoung 	struct rtw_txsoft_blk *tsb;
   3906    1.1    dyoung 
   3907   1.21    dyoung 	for (pri = 0; pri < RTW_NTXPRI; pri++) {
   3908   1.34    dyoung 		tsb = &sc->sc_txsoft_blk[pri];
   3909   1.34    dyoung 		free(tsb->tsb_desc, M_DEVBUF);
   3910   1.34    dyoung 		tsb->tsb_desc = NULL;
   3911    1.1    dyoung 	}
   3912    1.1    dyoung }
   3913    1.1    dyoung 
   3914    1.1    dyoung static int
   3915   1.34    dyoung rtw_txsoft_blk_setup_all(struct rtw_softc *sc)
   3916    1.1    dyoung {
   3917    1.1    dyoung 	int pri, rc = 0;
   3918    1.1    dyoung 	int qlen[RTW_NTXPRI] =
   3919    1.1    dyoung 	     {RTW_TXQLENLO, RTW_TXQLENMD, RTW_TXQLENHI, RTW_TXQLENBCN};
   3920   1.42    dyoung 	struct rtw_txsoft_blk *tsbs;
   3921   1.42    dyoung 
   3922   1.42    dyoung 	tsbs = sc->sc_txsoft_blk;
   3923    1.1    dyoung 
   3924   1.21    dyoung 	for (pri = 0; pri < RTW_NTXPRI; pri++) {
   3925   1.42    dyoung 		rc = rtw_txsoft_blk_setup(&tsbs[pri], qlen[pri]);
   3926    1.1    dyoung 		if (rc != 0)
   3927    1.1    dyoung 			break;
   3928    1.1    dyoung 	}
   3929   1.42    dyoung 	tsbs[RTW_TXPRILO].tsb_poll = RTW_TPPOLL_LPQ | RTW_TPPOLL_SLPQ;
   3930   1.42    dyoung 	tsbs[RTW_TXPRIMD].tsb_poll = RTW_TPPOLL_NPQ | RTW_TPPOLL_SNPQ;
   3931   1.42    dyoung 	tsbs[RTW_TXPRIHI].tsb_poll = RTW_TPPOLL_HPQ | RTW_TPPOLL_SHPQ;
   3932   1.42    dyoung 	tsbs[RTW_TXPRIBCN].tsb_poll = RTW_TPPOLL_BQ | RTW_TPPOLL_SBQ;
   3933    1.1    dyoung 	return rc;
   3934    1.1    dyoung }
   3935    1.1    dyoung 
   3936    1.1    dyoung static void
   3937   1.34    dyoung rtw_txdesc_blk_setup(struct rtw_txdesc_blk *tdb, struct rtw_txdesc *desc,
   3938    1.1    dyoung     u_int ndesc, bus_addr_t ofs, bus_addr_t physbase)
   3939    1.1    dyoung {
   3940   1.34    dyoung 	tdb->tdb_ndesc = ndesc;
   3941   1.34    dyoung 	tdb->tdb_desc = desc;
   3942   1.34    dyoung 	tdb->tdb_physbase = physbase;
   3943   1.34    dyoung 	tdb->tdb_ofs = ofs;
   3944    1.1    dyoung 
   3945   1.34    dyoung 	(void)memset(tdb->tdb_desc, 0,
   3946   1.34    dyoung 	    sizeof(tdb->tdb_desc[0]) * tdb->tdb_ndesc);
   3947    1.1    dyoung 
   3948   1.58    dyoung 	rtw_txdesc_blk_init(tdb);
   3949   1.58    dyoung 	tdb->tdb_next = 0;
   3950    1.1    dyoung }
   3951    1.1    dyoung 
   3952    1.1    dyoung static void
   3953    1.1    dyoung rtw_txdesc_blk_setup_all(struct rtw_softc *sc)
   3954    1.1    dyoung {
   3955    1.1    dyoung 	rtw_txdesc_blk_setup(&sc->sc_txdesc_blk[RTW_TXPRILO],
   3956    1.1    dyoung 	    &sc->sc_descs->hd_txlo[0], RTW_NTXDESCLO,
   3957    1.1    dyoung 	    RTW_RING_OFFSET(hd_txlo), RTW_RING_BASE(sc, hd_txlo));
   3958    1.1    dyoung 
   3959    1.1    dyoung 	rtw_txdesc_blk_setup(&sc->sc_txdesc_blk[RTW_TXPRIMD],
   3960    1.1    dyoung 	    &sc->sc_descs->hd_txmd[0], RTW_NTXDESCMD,
   3961    1.1    dyoung 	    RTW_RING_OFFSET(hd_txmd), RTW_RING_BASE(sc, hd_txmd));
   3962    1.1    dyoung 
   3963    1.1    dyoung 	rtw_txdesc_blk_setup(&sc->sc_txdesc_blk[RTW_TXPRIHI],
   3964    1.1    dyoung 	    &sc->sc_descs->hd_txhi[0], RTW_NTXDESCHI,
   3965    1.1    dyoung 	    RTW_RING_OFFSET(hd_txhi), RTW_RING_BASE(sc, hd_txhi));
   3966    1.1    dyoung 
   3967    1.1    dyoung 	rtw_txdesc_blk_setup(&sc->sc_txdesc_blk[RTW_TXPRIBCN],
   3968    1.1    dyoung 	    &sc->sc_descs->hd_bcn[0], RTW_NTXDESCBCN,
   3969    1.1    dyoung 	    RTW_RING_OFFSET(hd_bcn), RTW_RING_BASE(sc, hd_bcn));
   3970    1.1    dyoung }
   3971    1.1    dyoung 
   3972    1.1    dyoung static struct rtw_rf *
   3973   1.42    dyoung rtw_rf_attach(struct rtw_softc *sc, enum rtw_rfchipid rfchipid, int digphy)
   3974    1.1    dyoung {
   3975   1.42    dyoung 	rtw_rf_write_t rf_write;
   3976    1.1    dyoung 	struct rtw_rf *rf;
   3977    1.1    dyoung 
   3978    1.1    dyoung 	switch (rfchipid) {
   3979   1.42    dyoung 	default:
   3980   1.42    dyoung 		rf_write = rtw_rf_hostwrite;
   3981   1.42    dyoung 		break;
   3982   1.42    dyoung 	case RTW_RFCHIPID_INTERSIL:
   3983   1.42    dyoung 	case RTW_RFCHIPID_PHILIPS:
   3984   1.42    dyoung 	case RTW_RFCHIPID_GCT:	/* XXX a guess */
   3985   1.42    dyoung 	case RTW_RFCHIPID_RFMD:
   3986   1.42    dyoung 		rf_write = (rtw_host_rfio) ? rtw_rf_hostwrite : rtw_rf_macwrite;
   3987   1.42    dyoung 		break;
   3988   1.42    dyoung 	}
   3989   1.42    dyoung 
   3990   1.42    dyoung 	switch (rfchipid) {
   3991   1.64    dyoung 	case RTW_RFCHIPID_GCT:
   3992   1.64    dyoung 		rf = rtw_grf5101_create(&sc->sc_regs, rf_write, 0);
   3993   1.64    dyoung 		sc->sc_pwrstate_cb = rtw_maxim_pwrstate;
   3994   1.64    dyoung 		break;
   3995    1.1    dyoung 	case RTW_RFCHIPID_MAXIM:
   3996    1.1    dyoung 		rf = rtw_max2820_create(&sc->sc_regs, rf_write, 0);
   3997    1.1    dyoung 		sc->sc_pwrstate_cb = rtw_maxim_pwrstate;
   3998    1.1    dyoung 		break;
   3999    1.1    dyoung 	case RTW_RFCHIPID_PHILIPS:
   4000    1.1    dyoung 		rf = rtw_sa2400_create(&sc->sc_regs, rf_write, digphy);
   4001    1.1    dyoung 		sc->sc_pwrstate_cb = rtw_philips_pwrstate;
   4002    1.1    dyoung 		break;
   4003   1.10    dyoung 	case RTW_RFCHIPID_RFMD:
   4004   1.10    dyoung 		/* XXX RFMD has no RF constructor */
   4005   1.10    dyoung 		sc->sc_pwrstate_cb = rtw_rfmd_pwrstate;
   4006   1.10    dyoung 		/*FALLTHROUGH*/
   4007    1.1    dyoung 	default:
   4008    1.1    dyoung 		return NULL;
   4009    1.1    dyoung 	}
   4010    1.1    dyoung 	rf->rf_continuous_tx_cb =
   4011    1.1    dyoung 	    (rtw_continuous_tx_cb_t)rtw_continuous_tx_enable;
   4012    1.1    dyoung 	rf->rf_continuous_tx_arg = (void *)sc;
   4013    1.1    dyoung 	return rf;
   4014    1.1    dyoung }
   4015    1.1    dyoung 
   4016    1.1    dyoung /* Revision C and later use a different PHY delay setting than
   4017    1.1    dyoung  * revisions A and B.
   4018    1.1    dyoung  */
   4019   1.37    dyoung static uint8_t
   4020   1.46    dyoung rtw_check_phydelay(struct rtw_regs *regs, uint32_t old_rcr)
   4021    1.1    dyoung {
   4022    1.1    dyoung #define REVAB (RTW_RCR_MXDMA_UNLIMITED | RTW_RCR_AICV)
   4023    1.1    dyoung #define REVC (REVAB | RTW_RCR_RXFTH_WHOLE)
   4024    1.1    dyoung 
   4025   1.75    dyoung 	uint8_t phydelay = __SHIFTIN(0x6, RTW_PHYDELAY_PHYDELAY);
   4026    1.1    dyoung 
   4027    1.1    dyoung 	RTW_WRITE(regs, RTW_RCR, REVAB);
   4028    1.8    dyoung 	RTW_WBW(regs, RTW_RCR, RTW_RCR);
   4029    1.1    dyoung 	RTW_WRITE(regs, RTW_RCR, REVC);
   4030    1.1    dyoung 
   4031    1.1    dyoung 	RTW_WBR(regs, RTW_RCR, RTW_RCR);
   4032    1.1    dyoung 	if ((RTW_READ(regs, RTW_RCR) & REVC) == REVC)
   4033    1.1    dyoung 		phydelay |= RTW_PHYDELAY_REVC_MAGIC;
   4034    1.1    dyoung 
   4035   1.46    dyoung 	RTW_WRITE(regs, RTW_RCR, old_rcr);	/* restore RCR */
   4036    1.8    dyoung 	RTW_SYNC(regs, RTW_RCR, RTW_RCR);
   4037    1.1    dyoung 
   4038    1.1    dyoung 	return phydelay;
   4039    1.1    dyoung #undef REVC
   4040    1.1    dyoung }
   4041    1.1    dyoung 
   4042    1.1    dyoung void
   4043    1.1    dyoung rtw_attach(struct rtw_softc *sc)
   4044    1.1    dyoung {
   4045   1.48    dyoung 	struct ifnet *ifp = &sc->sc_if;
   4046   1.53    dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   4047   1.34    dyoung 	struct rtw_txsoft_blk *tsb;
   4048   1.42    dyoung 	int pri, rc;
   4049    1.1    dyoung 
   4050  1.109    dyoung 	pmf_self_suspensor_init(sc->sc_dev, &sc->sc_suspensor, &sc->sc_qual);
   4051  1.109    dyoung 
   4052   1.58    dyoung 	rtw_cipher_wep = ieee80211_cipher_wep;
   4053   1.58    dyoung 	rtw_cipher_wep.ic_decap = rtw_wep_decap;
   4054   1.58    dyoung 
   4055    1.1    dyoung 	NEXT_ATTACH_STATE(sc, DETACHED);
   4056    1.1    dyoung 
   4057  1.125    nonaka 	sc->sc_soft_ih = softint_establish(SOFTINT_NET, rtw_softintr, sc);
   4058  1.125    nonaka 	if (sc->sc_soft_ih == NULL) {
   4059  1.125    nonaka 		aprint_error_dev(sc->sc_dev, "could not establish softint\n");
   4060  1.125    nonaka 		goto err;
   4061  1.125    nonaka 	}
   4062  1.125    nonaka 
   4063    1.1    dyoung 	switch (RTW_READ(&sc->sc_regs, RTW_TCR) & RTW_TCR_HWVERID_MASK) {
   4064    1.1    dyoung 	case RTW_TCR_HWVERID_F:
   4065   1.42    dyoung 		sc->sc_hwverid = 'F';
   4066    1.1    dyoung 		break;
   4067    1.1    dyoung 	case RTW_TCR_HWVERID_D:
   4068   1.42    dyoung 		sc->sc_hwverid = 'D';
   4069    1.1    dyoung 		break;
   4070    1.1    dyoung 	default:
   4071   1.42    dyoung 		sc->sc_hwverid = '?';
   4072    1.1    dyoung 		break;
   4073    1.1    dyoung 	}
   4074   1.98    dyoung 	aprint_verbose_dev(sc->sc_dev, "hardware version %c\n",
   4075   1.42    dyoung 	    sc->sc_hwverid);
   4076    1.1    dyoung 
   4077    1.1    dyoung 	rc = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct rtw_descs),
   4078    1.1    dyoung 	    RTW_DESC_ALIGNMENT, 0, &sc->sc_desc_segs, 1, &sc->sc_desc_nsegs,
   4079    1.1    dyoung 	    0);
   4080    1.1    dyoung 
   4081    1.1    dyoung 	if (rc != 0) {
   4082   1.98    dyoung 		aprint_error_dev(sc->sc_dev,
   4083   1.98    dyoung 		    "could not allocate hw descriptors, error %d\n", rc);
   4084    1.1    dyoung 		goto err;
   4085    1.1    dyoung 	}
   4086    1.1    dyoung 
   4087    1.1    dyoung 	NEXT_ATTACH_STATE(sc, FINISH_DESC_ALLOC);
   4088    1.1    dyoung 
   4089    1.1    dyoung 	rc = bus_dmamem_map(sc->sc_dmat, &sc->sc_desc_segs,
   4090    1.1    dyoung 	    sc->sc_desc_nsegs, sizeof(struct rtw_descs),
   4091   1.85  christos 	    (void **)&sc->sc_descs, BUS_DMA_COHERENT);
   4092    1.1    dyoung 
   4093    1.1    dyoung 	if (rc != 0) {
   4094   1.98    dyoung 		aprint_error_dev(sc->sc_dev,
   4095   1.98    dyoung 		    "could not map hw descriptors, error %d\n", rc);
   4096    1.1    dyoung 		goto err;
   4097    1.1    dyoung 	}
   4098    1.1    dyoung 	NEXT_ATTACH_STATE(sc, FINISH_DESC_MAP);
   4099    1.1    dyoung 
   4100    1.1    dyoung 	rc = bus_dmamap_create(sc->sc_dmat, sizeof(struct rtw_descs), 1,
   4101    1.1    dyoung 	    sizeof(struct rtw_descs), 0, 0, &sc->sc_desc_dmamap);
   4102    1.1    dyoung 
   4103    1.1    dyoung 	if (rc != 0) {
   4104   1.98    dyoung 		aprint_error_dev(sc->sc_dev,
   4105   1.98    dyoung 		    "could not create DMA map for hw descriptors, error %d\n",
   4106   1.98    dyoung 		    rc);
   4107    1.1    dyoung 		goto err;
   4108    1.1    dyoung 	}
   4109    1.1    dyoung 	NEXT_ATTACH_STATE(sc, FINISH_DESCMAP_CREATE);
   4110    1.1    dyoung 
   4111   1.34    dyoung 	sc->sc_rxdesc_blk.rdb_dmat = sc->sc_dmat;
   4112   1.34    dyoung 	sc->sc_rxdesc_blk.rdb_dmamap = sc->sc_desc_dmamap;
   4113   1.33    dyoung 
   4114   1.33    dyoung 	for (pri = 0; pri < RTW_NTXPRI; pri++) {
   4115   1.34    dyoung 		sc->sc_txdesc_blk[pri].tdb_dmat = sc->sc_dmat;
   4116   1.34    dyoung 		sc->sc_txdesc_blk[pri].tdb_dmamap = sc->sc_desc_dmamap;
   4117   1.33    dyoung 	}
   4118   1.33    dyoung 
   4119    1.1    dyoung 	rc = bus_dmamap_load(sc->sc_dmat, sc->sc_desc_dmamap, sc->sc_descs,
   4120    1.1    dyoung 	    sizeof(struct rtw_descs), NULL, 0);
   4121    1.1    dyoung 
   4122    1.1    dyoung 	if (rc != 0) {
   4123   1.98    dyoung 		aprint_error_dev(sc->sc_dev,
   4124   1.98    dyoung 		    "could not load DMA map for hw descriptors, error %d\n",
   4125   1.98    dyoung 		    rc);
   4126    1.1    dyoung 		goto err;
   4127    1.1    dyoung 	}
   4128    1.1    dyoung 	NEXT_ATTACH_STATE(sc, FINISH_DESCMAP_LOAD);
   4129    1.1    dyoung 
   4130   1.34    dyoung 	if (rtw_txsoft_blk_setup_all(sc) != 0)
   4131    1.1    dyoung 		goto err;
   4132    1.1    dyoung 	NEXT_ATTACH_STATE(sc, FINISH_TXCTLBLK_SETUP);
   4133    1.1    dyoung 
   4134    1.1    dyoung 	rtw_txdesc_blk_setup_all(sc);
   4135    1.1    dyoung 
   4136    1.1    dyoung 	NEXT_ATTACH_STATE(sc, FINISH_TXDESCBLK_SETUP);
   4137    1.1    dyoung 
   4138   1.34    dyoung 	sc->sc_rxdesc_blk.rdb_desc = &sc->sc_descs->hd_rx[0];
   4139    1.1    dyoung 
   4140    1.1    dyoung 	for (pri = 0; pri < RTW_NTXPRI; pri++) {
   4141   1.34    dyoung 		tsb = &sc->sc_txsoft_blk[pri];
   4142    1.1    dyoung 
   4143    1.1    dyoung 		if ((rc = rtw_txdesc_dmamaps_create(sc->sc_dmat,
   4144   1.34    dyoung 		    &tsb->tsb_desc[0], tsb->tsb_ndesc)) != 0) {
   4145   1.98    dyoung 			aprint_error_dev(sc->sc_dev,
   4146   1.98    dyoung 			    "could not load DMA map for hw tx descriptors, "
   4147   1.98    dyoung 			    "error %d\n", rc);
   4148    1.1    dyoung 			goto err;
   4149    1.1    dyoung 		}
   4150    1.1    dyoung 	}
   4151    1.1    dyoung 
   4152    1.1    dyoung 	NEXT_ATTACH_STATE(sc, FINISH_TXMAPS_CREATE);
   4153   1.34    dyoung 	if ((rc = rtw_rxdesc_dmamaps_create(sc->sc_dmat, &sc->sc_rxsoft[0],
   4154    1.1    dyoung 	                                    RTW_RXQLEN)) != 0) {
   4155   1.98    dyoung 		aprint_error_dev(sc->sc_dev,
   4156   1.98    dyoung 		    "could not load DMA map for hw rx descriptors, error %d\n",
   4157   1.98    dyoung 		    rc);
   4158    1.1    dyoung 		goto err;
   4159    1.1    dyoung 	}
   4160    1.1    dyoung 	NEXT_ATTACH_STATE(sc, FINISH_RXMAPS_CREATE);
   4161    1.1    dyoung 
   4162    1.1    dyoung 	/* Reset the chip to a known state. */
   4163    1.1    dyoung 	if (rtw_reset(sc) != 0)
   4164    1.1    dyoung 		goto err;
   4165    1.1    dyoung 	NEXT_ATTACH_STATE(sc, FINISH_RESET);
   4166    1.1    dyoung 
   4167    1.1    dyoung 	sc->sc_rcr = RTW_READ(&sc->sc_regs, RTW_RCR);
   4168    1.1    dyoung 
   4169    1.1    dyoung 	if ((sc->sc_rcr & RTW_RCR_9356SEL) != 0)
   4170    1.1    dyoung 		sc->sc_flags |= RTW_F_9356SROM;
   4171    1.1    dyoung 
   4172    1.1    dyoung 	if (rtw_srom_read(&sc->sc_regs, sc->sc_flags, &sc->sc_srom,
   4173   1.98    dyoung 	    sc->sc_dev) != 0)
   4174    1.1    dyoung 		goto err;
   4175    1.1    dyoung 
   4176    1.1    dyoung 	NEXT_ATTACH_STATE(sc, FINISH_READ_SROM);
   4177    1.1    dyoung 
   4178    1.1    dyoung 	if (rtw_srom_parse(&sc->sc_srom, &sc->sc_flags, &sc->sc_csthr,
   4179    1.1    dyoung 	    &sc->sc_rfchipid, &sc->sc_rcr, &sc->sc_locale,
   4180   1.98    dyoung 	    sc->sc_dev) != 0) {
   4181   1.98    dyoung 		aprint_error_dev(sc->sc_dev,
   4182   1.98    dyoung 		    "attach failed, malformed serial ROM\n");
   4183    1.1    dyoung 		goto err;
   4184    1.1    dyoung 	}
   4185    1.1    dyoung 
   4186   1.98    dyoung 	aprint_verbose_dev(sc->sc_dev, "%s PHY\n",
   4187   1.10    dyoung 	    ((sc->sc_flags & RTW_F_DIGPHY) != 0) ? "digital" : "analog");
   4188   1.10    dyoung 
   4189   1.98    dyoung 	aprint_verbose_dev(sc->sc_dev, "carrier-sense threshold %u\n",
   4190   1.98    dyoung 	    sc->sc_csthr);
   4191    1.1    dyoung 
   4192    1.1    dyoung 	NEXT_ATTACH_STATE(sc, FINISH_PARSE_SROM);
   4193    1.1    dyoung 
   4194   1.42    dyoung 	sc->sc_rf = rtw_rf_attach(sc, sc->sc_rfchipid,
   4195    1.1    dyoung 	    sc->sc_flags & RTW_F_DIGPHY);
   4196    1.1    dyoung 
   4197    1.1    dyoung 	if (sc->sc_rf == NULL) {
   4198   1.98    dyoung 		aprint_verbose_dev(sc->sc_dev,
   4199   1.98    dyoung 		    "attach failed, could not attach RF\n");
   4200    1.1    dyoung 		goto err;
   4201    1.1    dyoung 	}
   4202    1.1    dyoung 
   4203    1.1    dyoung 	NEXT_ATTACH_STATE(sc, FINISH_RF_ATTACH);
   4204    1.1    dyoung 
   4205    1.1    dyoung 	sc->sc_phydelay = rtw_check_phydelay(&sc->sc_regs, sc->sc_rcr);
   4206    1.1    dyoung 
   4207   1.21    dyoung 	RTW_DPRINTF(RTW_DEBUG_ATTACH,
   4208   1.98    dyoung 	    ("%s: PHY delay %d\n", device_xname(sc->sc_dev), sc->sc_phydelay));
   4209    1.1    dyoung 
   4210    1.1    dyoung 	if (sc->sc_locale == RTW_LOCALE_UNKNOWN)
   4211   1.58    dyoung 		rtw_identify_country(&sc->sc_regs, &sc->sc_locale);
   4212    1.1    dyoung 
   4213   1.98    dyoung 	rtw_init_channels(sc->sc_locale, &sc->sc_ic.ic_channels, sc->sc_dev);
   4214    1.1    dyoung 
   4215    1.1    dyoung 	if (rtw_identify_sta(&sc->sc_regs, &sc->sc_ic.ic_myaddr,
   4216   1.98    dyoung 	    sc->sc_dev) != 0)
   4217    1.1    dyoung 		goto err;
   4218    1.1    dyoung 	NEXT_ATTACH_STATE(sc, FINISH_ID_STA);
   4219    1.1    dyoung 
   4220   1.98    dyoung 	rtw_setifprops(ifp, device_xname(sc->sc_dev), (void*)sc);
   4221    1.1    dyoung 
   4222   1.56       gdt 	IFQ_SET_READY(&ifp->if_snd);
   4223    1.1    dyoung 
   4224   1.48    dyoung 	sc->sc_ic.ic_ifp = ifp;
   4225    1.1    dyoung 	rtw_set80211props(&sc->sc_ic);
   4226    1.1    dyoung 
   4227   1.45    dyoung 	rtw_led_attach(&sc->sc_led_state, (void *)sc);
   4228  1.127   msaitoh 	NEXT_ATTACH_STATE(sc, FINISH_LED_ATTACH);
   4229   1.42    dyoung 
   4230    1.1    dyoung 	/*
   4231    1.1    dyoung 	 * Call MI attach routines.
   4232    1.1    dyoung 	 */
   4233  1.127   msaitoh 	rc = if_initialize(ifp);
   4234  1.127   msaitoh 	if (rc != 0) {
   4235  1.127   msaitoh 		aprint_error_dev(sc->sc_dev, "if_initialize failed(%d)\n", rc);
   4236  1.127   msaitoh 		goto err;
   4237  1.127   msaitoh 	}
   4238  1.125    nonaka 	ieee80211_ifattach(ic);
   4239  1.125    nonaka 	/* Use common softint-based if_input */
   4240  1.125    nonaka 	ifp->if_percpuq = if_percpuq_create(ifp);
   4241  1.125    nonaka 	if_register(ifp);
   4242    1.1    dyoung 
   4243    1.1    dyoung 	rtw_set80211methods(&sc->sc_mtbl, &sc->sc_ic);
   4244    1.1    dyoung 
   4245    1.1    dyoung 	/* possibly we should fill in our own sc_send_prresp, since
   4246    1.1    dyoung 	 * the RTL8180 is probably sending probe responses in ad hoc
   4247    1.1    dyoung 	 * mode.
   4248    1.1    dyoung 	 */
   4249    1.1    dyoung 
   4250    1.1    dyoung 	/* complete initialization */
   4251   1.48    dyoung 	ieee80211_media_init(&sc->sc_ic, rtw_media_change, rtw_media_status);
   4252   1.89        ad 	callout_init(&sc->sc_scan_ch, 0);
   4253    1.1    dyoung 
   4254   1.32    dyoung 	rtw_init_radiotap(sc);
   4255   1.32    dyoung 
   4256  1.116     joerg 	bpf_attach2(ifp, DLT_IEEE802_11_RADIO,
   4257    1.1    dyoung 	    sizeof(struct ieee80211_frame) + 64, &sc->sc_radiobpf);
   4258    1.1    dyoung 
   4259    1.1    dyoung 	NEXT_ATTACH_STATE(sc, FINISHED);
   4260    1.1    dyoung 
   4261   1.52    dyoung 	ieee80211_announce(ic);
   4262    1.1    dyoung 	return;
   4263    1.1    dyoung err:
   4264    1.1    dyoung 	rtw_detach(sc);
   4265    1.1    dyoung 	return;
   4266    1.1    dyoung }
   4267    1.1    dyoung 
   4268    1.1    dyoung int
   4269    1.1    dyoung rtw_detach(struct rtw_softc *sc)
   4270    1.1    dyoung {
   4271   1.48    dyoung 	struct ifnet *ifp = &sc->sc_if;
   4272   1.95    dyoung 	int pri, s;
   4273    1.1    dyoung 
   4274   1.95    dyoung 	s = splnet();
   4275   1.36    dyoung 
   4276    1.1    dyoung 	switch (sc->sc_attach_state) {
   4277    1.1    dyoung 	case FINISHED:
   4278   1.48    dyoung 		rtw_stop(ifp, 1);
   4279    1.3    dyoung 
   4280   1.98    dyoung 		pmf_device_deregister(sc->sc_dev);
   4281    1.1    dyoung 		callout_stop(&sc->sc_scan_ch);
   4282   1.48    dyoung 		ieee80211_ifdetach(&sc->sc_ic);
   4283   1.48    dyoung 		if_detach(ifp);
   4284  1.127   msaitoh 	case FINISH_LED_ATTACH:
   4285  1.101    dyoung 		rtw_led_detach(&sc->sc_led_state);
   4286   1.96    dyoung 		/*FALLTHROUGH*/
   4287    1.1    dyoung 	case FINISH_ID_STA:
   4288    1.1    dyoung 	case FINISH_RF_ATTACH:
   4289    1.1    dyoung 		rtw_rf_destroy(sc->sc_rf);
   4290    1.1    dyoung 		sc->sc_rf = NULL;
   4291    1.1    dyoung 		/*FALLTHROUGH*/
   4292    1.1    dyoung 	case FINISH_PARSE_SROM:
   4293    1.1    dyoung 	case FINISH_READ_SROM:
   4294    1.1    dyoung 		rtw_srom_free(&sc->sc_srom);
   4295    1.1    dyoung 		/*FALLTHROUGH*/
   4296    1.1    dyoung 	case FINISH_RESET:
   4297    1.1    dyoung 	case FINISH_RXMAPS_CREATE:
   4298   1.34    dyoung 		rtw_rxdesc_dmamaps_destroy(sc->sc_dmat, &sc->sc_rxsoft[0],
   4299    1.1    dyoung 		    RTW_RXQLEN);
   4300    1.1    dyoung 		/*FALLTHROUGH*/
   4301    1.1    dyoung 	case FINISH_TXMAPS_CREATE:
   4302    1.1    dyoung 		for (pri = 0; pri < RTW_NTXPRI; pri++) {
   4303    1.1    dyoung 			rtw_txdesc_dmamaps_destroy(sc->sc_dmat,
   4304   1.34    dyoung 			    sc->sc_txsoft_blk[pri].tsb_desc,
   4305   1.34    dyoung 			    sc->sc_txsoft_blk[pri].tsb_ndesc);
   4306    1.1    dyoung 		}
   4307    1.1    dyoung 		/*FALLTHROUGH*/
   4308    1.1    dyoung 	case FINISH_TXDESCBLK_SETUP:
   4309    1.1    dyoung 	case FINISH_TXCTLBLK_SETUP:
   4310   1.34    dyoung 		rtw_txsoft_blk_cleanup_all(sc);
   4311    1.1    dyoung 		/*FALLTHROUGH*/
   4312    1.1    dyoung 	case FINISH_DESCMAP_LOAD:
   4313    1.1    dyoung 		bus_dmamap_unload(sc->sc_dmat, sc->sc_desc_dmamap);
   4314    1.1    dyoung 		/*FALLTHROUGH*/
   4315    1.1    dyoung 	case FINISH_DESCMAP_CREATE:
   4316    1.1    dyoung 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_desc_dmamap);
   4317    1.1    dyoung 		/*FALLTHROUGH*/
   4318    1.1    dyoung 	case FINISH_DESC_MAP:
   4319   1.85  christos 		bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_descs,
   4320    1.1    dyoung 		    sizeof(struct rtw_descs));
   4321    1.1    dyoung 		/*FALLTHROUGH*/
   4322    1.1    dyoung 	case FINISH_DESC_ALLOC:
   4323    1.1    dyoung 		bus_dmamem_free(sc->sc_dmat, &sc->sc_desc_segs,
   4324    1.1    dyoung 		    sc->sc_desc_nsegs);
   4325    1.1    dyoung 		/*FALLTHROUGH*/
   4326    1.1    dyoung 	case DETACHED:
   4327  1.125    nonaka 		if (sc->sc_soft_ih != NULL) {
   4328  1.125    nonaka 			softint_disestablish(sc->sc_soft_ih);
   4329  1.125    nonaka 			sc->sc_soft_ih = NULL;
   4330  1.125    nonaka 		}
   4331    1.1    dyoung 		NEXT_ATTACH_STATE(sc, DETACHED);
   4332    1.1    dyoung 		break;
   4333    1.1    dyoung 	}
   4334   1.95    dyoung 	splx(s);
   4335    1.1    dyoung 	return 0;
   4336    1.1    dyoung }
   4337