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