Home | History | Annotate | Line # | Download | only in ic
rtw.c revision 1.29
      1 /* $NetBSD: rtw.c,v 1.29 2004/12/27 19:49:16 dyoung Exp $ */
      2 /*-
      3  * Copyright (c) 2004, 2005 David Young.  All rights reserved.
      4  *
      5  * Programmed for NetBSD by David Young.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of David Young may not be used to endorse or promote
     16  *    products derived from this software without specific prior
     17  *    written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY
     20  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     22  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL David
     23  * Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
     25  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     27  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
     30  * OF SUCH DAMAGE.
     31  */
     32 /*
     33  * Device driver for the Realtek RTL8180 802.11 MAC/BBP.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: rtw.c,v 1.29 2004/12/27 19:49:16 dyoung Exp $");
     38 
     39 #include "bpfilter.h"
     40 
     41 #include <sys/param.h>
     42 #include <sys/sysctl.h>
     43 #include <sys/systm.h>
     44 #include <sys/callout.h>
     45 #include <sys/mbuf.h>
     46 #include <sys/malloc.h>
     47 #include <sys/kernel.h>
     48 #if 0
     49 #include <sys/socket.h>
     50 #include <sys/ioctl.h>
     51 #include <sys/errno.h>
     52 #include <sys/device.h>
     53 #endif
     54 #include <sys/time.h>
     55 #include <sys/types.h>
     56 
     57 #include <machine/endian.h>
     58 #include <machine/bus.h>
     59 #include <machine/intr.h>	/* splnet */
     60 
     61 #include <uvm/uvm_extern.h>
     62 
     63 #include <net/if.h>
     64 #include <net/if_media.h>
     65 #include <net/if_ether.h>
     66 
     67 #include <net80211/ieee80211_var.h>
     68 #include <net80211/ieee80211_compat.h>
     69 #include <net80211/ieee80211_radiotap.h>
     70 
     71 #if NBPFILTER > 0
     72 #include <net/bpf.h>
     73 #endif
     74 
     75 #include <dev/ic/rtwreg.h>
     76 #include <dev/ic/rtwvar.h>
     77 #include <dev/ic/rtwphyio.h>
     78 #include <dev/ic/rtwphy.h>
     79 
     80 #include <dev/ic/smc93cx6var.h>
     81 
     82 #define	KASSERT2(__cond, __msg)		\
     83 	do {				\
     84 		if (!(__cond))		\
     85 			panic __msg ;	\
     86 	} while (0)
     87 
     88 int rtw_rfprog_fallback = 0;
     89 int rtw_host_rfio = 0;
     90 
     91 #ifdef RTW_DEBUG
     92 int rtw_debug = 0;
     93 #endif /* RTW_DEBUG */
     94 
     95 #define NEXT_ATTACH_STATE(sc, state) do {			\
     96 	DPRINTF(sc, RTW_DEBUG_ATTACH,				\
     97 	    ("%s: attach state %s\n", __func__, #state));	\
     98 	sc->sc_attach_state = state;				\
     99 } while (0)
    100 
    101 int rtw_dwelltime = 200;	/* milliseconds */
    102 
    103 static void rtw_start(struct ifnet *);
    104 
    105 static int rtw_sysctl_verify_rfio(SYSCTLFN_PROTO);
    106 static int rtw_sysctl_verify_rfprog(SYSCTLFN_PROTO);
    107 #ifdef RTW_DEBUG
    108 static void rtw_print_txdesc(struct rtw_softc *, const char *,
    109     struct rtw_txctl *, struct rtw_txdesc_blk *, int);
    110 static int rtw_sysctl_verify_debug(SYSCTLFN_PROTO);
    111 #endif /* RTW_DEBUG */
    112 
    113 /*
    114  * Setup sysctl(3) MIB, hw.rtw.*
    115  *
    116  * TBD condition CTLFLAG_PERMANENT on being an LKM or not
    117  */
    118 SYSCTL_SETUP(sysctl_rtw, "sysctl rtw(4) subtree setup")
    119 {
    120 	int rc;
    121 	struct sysctlnode *cnode, *rnode;
    122 
    123 	if ((rc = sysctl_createv(clog, 0, NULL, &rnode,
    124 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
    125 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0)
    126 		goto err;
    127 
    128 	if ((rc = sysctl_createv(clog, 0, &rnode, &rnode,
    129 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "rtw",
    130 	    "Realtek RTL818x 802.11 controls",
    131 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
    132 		goto err;
    133 
    134 #ifdef RTW_DEBUG
    135 	/* control debugging printfs */
    136 	if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
    137 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    138 	    "debug", SYSCTL_DESCR("Enable RTL818x debugging output"),
    139 	    rtw_sysctl_verify_debug, 0, &rtw_debug, 0,
    140 	    CTL_CREATE, CTL_EOL)) != 0)
    141 		goto err;
    142 #endif /* RTW_DEBUG */
    143 	/* set fallback RF programming method */
    144 	if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
    145 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    146 	    "rfprog_fallback",
    147 	    SYSCTL_DESCR("Set fallback RF programming method"),
    148 	    rtw_sysctl_verify_rfprog, 0, &rtw_rfprog_fallback, 0,
    149 	    CTL_CREATE, CTL_EOL)) != 0)
    150 		goto err;
    151 
    152 	/* force host to control RF I/O bus */
    153 	if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
    154 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    155 	    "host_rfio", SYSCTL_DESCR("Enable host control of RF I/O"),
    156 	    rtw_sysctl_verify_rfio, 0, &rtw_host_rfio, 0,
    157 	    CTL_CREATE, CTL_EOL)) != 0)
    158 		goto err;
    159 
    160 	return;
    161 err:
    162 	printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
    163 }
    164 
    165 static int
    166 rtw_sysctl_verify(SYSCTLFN_ARGS, int lower, int upper)
    167 {
    168 	int error, t;
    169 	struct sysctlnode node;
    170 
    171 	node = *rnode;
    172 	t = *(int*)rnode->sysctl_data;
    173 	node.sysctl_data = &t;
    174 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    175 	if (error || newp == NULL)
    176 		return (error);
    177 
    178 	if (t < lower || t > upper)
    179 		return (EINVAL);
    180 
    181 	*(int*)rnode->sysctl_data = t;
    182 
    183 	return (0);
    184 }
    185 
    186 static int
    187 rtw_sysctl_verify_rfprog(SYSCTLFN_ARGS)
    188 {
    189 	return rtw_sysctl_verify(SYSCTLFN_CALL(rnode), 0,
    190 	    MASK_AND_RSHIFT(RTW_CONFIG4_RFTYPE_MASK, RTW_CONFIG4_RFTYPE_MASK));
    191 }
    192 
    193 static int
    194 rtw_sysctl_verify_rfio(SYSCTLFN_ARGS)
    195 {
    196 	return rtw_sysctl_verify(SYSCTLFN_CALL(rnode), 0, 1);
    197 }
    198 
    199 #ifdef RTW_DEBUG
    200 static int
    201 rtw_sysctl_verify_debug(SYSCTLFN_ARGS)
    202 {
    203 	return rtw_sysctl_verify(SYSCTLFN_CALL(rnode), 0, RTW_DEBUG_MAX);
    204 }
    205 
    206 static void
    207 rtw_print_regs(struct rtw_regs *regs, const char *dvname, const char *where)
    208 {
    209 #define PRINTREG32(sc, reg)				\
    210 	RTW_DPRINTF(RTW_DEBUG_REGDUMP,			\
    211 	    ("%s: reg[ " #reg " / %03x ] = %08x\n",	\
    212 	    dvname, reg, RTW_READ(regs, reg)))
    213 
    214 #define PRINTREG16(sc, reg)				\
    215 	RTW_DPRINTF(RTW_DEBUG_REGDUMP,			\
    216 	    ("%s: reg[ " #reg " / %03x ] = %04x\n",	\
    217 	    dvname, reg, RTW_READ16(regs, reg)))
    218 
    219 #define PRINTREG8(sc, reg)				\
    220 	RTW_DPRINTF(RTW_DEBUG_REGDUMP,			\
    221 	    ("%s: reg[ " #reg " / %03x ] = %02x\n",	\
    222 	    dvname, reg, RTW_READ8(regs, reg)))
    223 
    224 	RTW_DPRINTF(RTW_DEBUG_REGDUMP, ("%s: %s\n", dvname, where));
    225 
    226 	PRINTREG32(regs, RTW_IDR0);
    227 	PRINTREG32(regs, RTW_IDR1);
    228 	PRINTREG32(regs, RTW_MAR0);
    229 	PRINTREG32(regs, RTW_MAR1);
    230 	PRINTREG32(regs, RTW_TSFTRL);
    231 	PRINTREG32(regs, RTW_TSFTRH);
    232 	PRINTREG32(regs, RTW_TLPDA);
    233 	PRINTREG32(regs, RTW_TNPDA);
    234 	PRINTREG32(regs, RTW_THPDA);
    235 	PRINTREG32(regs, RTW_TCR);
    236 	PRINTREG32(regs, RTW_RCR);
    237 	PRINTREG32(regs, RTW_TINT);
    238 	PRINTREG32(regs, RTW_TBDA);
    239 	PRINTREG32(regs, RTW_ANAPARM);
    240 	PRINTREG32(regs, RTW_BB);
    241 	PRINTREG32(regs, RTW_PHYCFG);
    242 	PRINTREG32(regs, RTW_WAKEUP0L);
    243 	PRINTREG32(regs, RTW_WAKEUP0H);
    244 	PRINTREG32(regs, RTW_WAKEUP1L);
    245 	PRINTREG32(regs, RTW_WAKEUP1H);
    246 	PRINTREG32(regs, RTW_WAKEUP2LL);
    247 	PRINTREG32(regs, RTW_WAKEUP2LH);
    248 	PRINTREG32(regs, RTW_WAKEUP2HL);
    249 	PRINTREG32(regs, RTW_WAKEUP2HH);
    250 	PRINTREG32(regs, RTW_WAKEUP3LL);
    251 	PRINTREG32(regs, RTW_WAKEUP3LH);
    252 	PRINTREG32(regs, RTW_WAKEUP3HL);
    253 	PRINTREG32(regs, RTW_WAKEUP3HH);
    254 	PRINTREG32(regs, RTW_WAKEUP4LL);
    255 	PRINTREG32(regs, RTW_WAKEUP4LH);
    256 	PRINTREG32(regs, RTW_WAKEUP4HL);
    257 	PRINTREG32(regs, RTW_WAKEUP4HH);
    258 	PRINTREG32(regs, RTW_DK0);
    259 	PRINTREG32(regs, RTW_DK1);
    260 	PRINTREG32(regs, RTW_DK2);
    261 	PRINTREG32(regs, RTW_DK3);
    262 	PRINTREG32(regs, RTW_RETRYCTR);
    263 	PRINTREG32(regs, RTW_RDSAR);
    264 	PRINTREG32(regs, RTW_FER);
    265 	PRINTREG32(regs, RTW_FEMR);
    266 	PRINTREG32(regs, RTW_FPSR);
    267 	PRINTREG32(regs, RTW_FFER);
    268 
    269 	/* 16-bit registers */
    270 	PRINTREG16(regs, RTW_BRSR);
    271 	PRINTREG16(regs, RTW_IMR);
    272 	PRINTREG16(regs, RTW_ISR);
    273 	PRINTREG16(regs, RTW_BCNITV);
    274 	PRINTREG16(regs, RTW_ATIMWND);
    275 	PRINTREG16(regs, RTW_BINTRITV);
    276 	PRINTREG16(regs, RTW_ATIMTRITV);
    277 	PRINTREG16(regs, RTW_CRC16ERR);
    278 	PRINTREG16(regs, RTW_CRC0);
    279 	PRINTREG16(regs, RTW_CRC1);
    280 	PRINTREG16(regs, RTW_CRC2);
    281 	PRINTREG16(regs, RTW_CRC3);
    282 	PRINTREG16(regs, RTW_CRC4);
    283 	PRINTREG16(regs, RTW_CWR);
    284 
    285 	/* 8-bit registers */
    286 	PRINTREG8(regs, RTW_CR);
    287 	PRINTREG8(regs, RTW_9346CR);
    288 	PRINTREG8(regs, RTW_CONFIG0);
    289 	PRINTREG8(regs, RTW_CONFIG1);
    290 	PRINTREG8(regs, RTW_CONFIG2);
    291 	PRINTREG8(regs, RTW_MSR);
    292 	PRINTREG8(regs, RTW_CONFIG3);
    293 	PRINTREG8(regs, RTW_CONFIG4);
    294 	PRINTREG8(regs, RTW_TESTR);
    295 	PRINTREG8(regs, RTW_PSR);
    296 	PRINTREG8(regs, RTW_SCR);
    297 	PRINTREG8(regs, RTW_PHYDELAY);
    298 	PRINTREG8(regs, RTW_CRCOUNT);
    299 	PRINTREG8(regs, RTW_PHYADDR);
    300 	PRINTREG8(regs, RTW_PHYDATAW);
    301 	PRINTREG8(regs, RTW_PHYDATAR);
    302 	PRINTREG8(regs, RTW_CONFIG5);
    303 	PRINTREG8(regs, RTW_TPPOLL);
    304 
    305 	PRINTREG16(regs, RTW_BSSID16);
    306 	PRINTREG32(regs, RTW_BSSID32);
    307 #undef PRINTREG32
    308 #undef PRINTREG16
    309 #undef PRINTREG8
    310 }
    311 #endif /* RTW_DEBUG */
    312 
    313 void
    314 rtw_continuous_tx_enable(struct rtw_softc *sc, int enable)
    315 {
    316 	struct rtw_regs *regs = &sc->sc_regs;
    317 
    318 	u_int32_t tcr;
    319 	tcr = RTW_READ(regs, RTW_TCR);
    320 	tcr &= ~RTW_TCR_LBK_MASK;
    321 	if (enable)
    322 		tcr |= RTW_TCR_LBK_CONT;
    323 	else
    324 		tcr |= RTW_TCR_LBK_NORMAL;
    325 	RTW_WRITE(regs, RTW_TCR, tcr);
    326 	RTW_SYNC(regs, RTW_TCR, RTW_TCR);
    327 	rtw_set_access(sc, RTW_ACCESS_ANAPARM);
    328 	rtw_txdac_enable(sc, !enable);
    329 	rtw_set_access(sc, RTW_ACCESS_ANAPARM);	/* XXX Voodoo from Linux. */
    330 	rtw_set_access(sc, RTW_ACCESS_NONE);
    331 }
    332 
    333 #ifdef RTW_DEBUG
    334 static const char *
    335 rtw_access_string(enum rtw_access access)
    336 {
    337 	switch (access) {
    338 	case RTW_ACCESS_NONE:
    339 		return "none";
    340 	case RTW_ACCESS_CONFIG:
    341 		return "config";
    342 	case RTW_ACCESS_ANAPARM:
    343 		return "anaparm";
    344 	default:
    345 		return "unknown";
    346 	}
    347 }
    348 #endif /* RTW_DEBUG */
    349 
    350 static void
    351 rtw_set_access1(struct rtw_regs *regs,
    352     enum rtw_access oaccess, enum rtw_access naccess)
    353 {
    354 	KASSERT(naccess >= RTW_ACCESS_NONE && naccess <= RTW_ACCESS_ANAPARM);
    355 	KASSERT(oaccess >= RTW_ACCESS_NONE && oaccess <= RTW_ACCESS_ANAPARM);
    356 
    357 	if (naccess == oaccess)
    358 		return;
    359 
    360 	switch (naccess) {
    361 	case RTW_ACCESS_NONE:
    362 		switch (oaccess) {
    363 		case RTW_ACCESS_ANAPARM:
    364 			rtw_anaparm_enable(regs, 0);
    365 			/*FALLTHROUGH*/
    366 		case RTW_ACCESS_CONFIG:
    367 			rtw_config0123_enable(regs, 0);
    368 			/*FALLTHROUGH*/
    369 		case RTW_ACCESS_NONE:
    370 			break;
    371 		}
    372 		break;
    373 	case RTW_ACCESS_CONFIG:
    374 		switch (oaccess) {
    375 		case RTW_ACCESS_NONE:
    376 			rtw_config0123_enable(regs, 1);
    377 			/*FALLTHROUGH*/
    378 		case RTW_ACCESS_CONFIG:
    379 			break;
    380 		case RTW_ACCESS_ANAPARM:
    381 			rtw_anaparm_enable(regs, 0);
    382 			break;
    383 		}
    384 		break;
    385 	case RTW_ACCESS_ANAPARM:
    386 		switch (oaccess) {
    387 		case RTW_ACCESS_NONE:
    388 			rtw_config0123_enable(regs, 1);
    389 			/*FALLTHROUGH*/
    390 		case RTW_ACCESS_CONFIG:
    391 			rtw_anaparm_enable(regs, 1);
    392 			/*FALLTHROUGH*/
    393 		case RTW_ACCESS_ANAPARM:
    394 			break;
    395 		}
    396 		break;
    397 	}
    398 }
    399 
    400 void
    401 rtw_set_access(struct rtw_softc *sc, enum rtw_access access)
    402 {
    403 	rtw_set_access1(&sc->sc_regs, sc->sc_access, access);
    404 	RTW_DPRINTF(RTW_DEBUG_ACCESS,
    405 	    ("%s: access %s -> %s\n", sc->sc_dev.dv_xname,
    406 	    rtw_access_string(sc->sc_access),
    407 	    rtw_access_string(access)));
    408 	sc->sc_access = access;
    409 }
    410 
    411 /*
    412  * Enable registers, switch register banks.
    413  */
    414 void
    415 rtw_config0123_enable(struct rtw_regs *regs, int enable)
    416 {
    417 	u_int8_t ecr;
    418 	ecr = RTW_READ8(regs, RTW_9346CR);
    419 	ecr &= ~(RTW_9346CR_EEM_MASK | RTW_9346CR_EECS | RTW_9346CR_EESK);
    420 	if (enable)
    421 		ecr |= RTW_9346CR_EEM_CONFIG;
    422 	else {
    423 		RTW_WBW(regs, RTW_9346CR, MAX(RTW_CONFIG0, RTW_CONFIG3));
    424 		ecr |= RTW_9346CR_EEM_NORMAL;
    425 	}
    426 	RTW_WRITE8(regs, RTW_9346CR, ecr);
    427 	RTW_SYNC(regs, RTW_9346CR, RTW_9346CR);
    428 }
    429 
    430 /* requires rtw_config0123_enable(, 1) */
    431 void
    432 rtw_anaparm_enable(struct rtw_regs *regs, int enable)
    433 {
    434 	u_int8_t cfg3;
    435 
    436 	cfg3 = RTW_READ8(regs, RTW_CONFIG3);
    437 	cfg3 |= RTW_CONFIG3_CLKRUNEN;
    438 	if (enable)
    439 		cfg3 |= RTW_CONFIG3_PARMEN;
    440 	else
    441 		cfg3 &= ~RTW_CONFIG3_PARMEN;
    442 	RTW_WRITE8(regs, RTW_CONFIG3, cfg3);
    443 	RTW_SYNC(regs, RTW_CONFIG3, RTW_CONFIG3);
    444 }
    445 
    446 /* requires rtw_anaparm_enable(, 1) */
    447 void
    448 rtw_txdac_enable(struct rtw_softc *sc, int enable)
    449 {
    450 	u_int32_t anaparm;
    451 	struct rtw_regs *regs = &sc->sc_regs;
    452 
    453 	anaparm = RTW_READ(regs, RTW_ANAPARM);
    454 	if (enable)
    455 		anaparm &= ~RTW_ANAPARM_TXDACOFF;
    456 	else
    457 		anaparm |= RTW_ANAPARM_TXDACOFF;
    458 	RTW_WRITE(regs, RTW_ANAPARM, anaparm);
    459 	RTW_SYNC(regs, RTW_ANAPARM, RTW_ANAPARM);
    460 }
    461 
    462 static __inline int
    463 rtw_chip_reset1(struct rtw_regs *regs, const char *dvname)
    464 {
    465 	u_int8_t cr;
    466 	int i;
    467 
    468 	RTW_WRITE8(regs, RTW_CR, RTW_CR_RST);
    469 
    470 	RTW_WBR(regs, RTW_CR, RTW_CR);
    471 
    472 	for (i = 0; i < 1000; i++) {
    473 		if ((cr = RTW_READ8(regs, RTW_CR) & RTW_CR_RST) == 0) {
    474 			RTW_DPRINTF(RTW_DEBUG_RESET,
    475 			    ("%s: reset in %dus\n", dvname, i));
    476 			return 0;
    477 		}
    478 		RTW_RBR(regs, RTW_CR, RTW_CR);
    479 		DELAY(10); /* 10us */
    480 	}
    481 
    482 	printf("%s: reset failed\n", dvname);
    483 	return ETIMEDOUT;
    484 }
    485 
    486 static __inline int
    487 rtw_chip_reset(struct rtw_regs *regs, const char *dvname)
    488 {
    489 	uint32_t tcr;
    490 
    491 	/* from Linux driver */
    492 	tcr = RTW_TCR_CWMIN | RTW_TCR_MXDMA_2048 |
    493 	      LSHIFT(7, RTW_TCR_SRL_MASK) | LSHIFT(7, RTW_TCR_LRL_MASK);
    494 
    495 	RTW_WRITE(regs, RTW_TCR, tcr);
    496 
    497 	RTW_WBW(regs, RTW_CR, RTW_TCR);
    498 
    499 	return rtw_chip_reset1(regs, dvname);
    500 }
    501 
    502 static __inline int
    503 rtw_recall_eeprom(struct rtw_regs *regs, const char *dvname)
    504 {
    505 	int i;
    506 	u_int8_t ecr;
    507 
    508 	ecr = RTW_READ8(regs, RTW_9346CR);
    509 	ecr = (ecr & ~RTW_9346CR_EEM_MASK) | RTW_9346CR_EEM_AUTOLOAD;
    510 	RTW_WRITE8(regs, RTW_9346CR, ecr);
    511 
    512 	RTW_WBR(regs, RTW_9346CR, RTW_9346CR);
    513 
    514 	/* wait 2.5ms for completion */
    515 	for (i = 0; i < 25; i++) {
    516 		ecr = RTW_READ8(regs, RTW_9346CR);
    517 		if ((ecr & RTW_9346CR_EEM_MASK) == RTW_9346CR_EEM_NORMAL) {
    518 			RTW_DPRINTF(RTW_DEBUG_RESET,
    519 			    ("%s: recall EEPROM in %dus\n", dvname, i * 100));
    520 			return 0;
    521 		}
    522 		RTW_RBR(regs, RTW_9346CR, RTW_9346CR);
    523 		DELAY(100);
    524 	}
    525 	printf("%s: recall EEPROM failed\n", dvname);
    526 	return ETIMEDOUT;
    527 }
    528 
    529 static __inline int
    530 rtw_reset(struct rtw_softc *sc)
    531 {
    532 	int rc;
    533 	uint8_t config1;
    534 
    535 	if ((rc = rtw_chip_reset(&sc->sc_regs, sc->sc_dev.dv_xname)) != 0)
    536 		return rc;
    537 
    538 	if ((rc = rtw_recall_eeprom(&sc->sc_regs, sc->sc_dev.dv_xname)) != 0)
    539 		;
    540 
    541 	config1 = RTW_READ8(&sc->sc_regs, RTW_CONFIG1);
    542 	RTW_WRITE8(&sc->sc_regs, RTW_CONFIG1, config1 & ~RTW_CONFIG1_PMEN);
    543 	/* TBD turn off maximum power saving? */
    544 
    545 	return 0;
    546 }
    547 
    548 static __inline int
    549 rtw_txdesc_dmamaps_create(bus_dma_tag_t dmat, struct rtw_txctl *descs,
    550     u_int ndescs)
    551 {
    552 	int i, rc = 0;
    553 	for (i = 0; i < ndescs; i++) {
    554 		rc = bus_dmamap_create(dmat, MCLBYTES, RTW_MAXPKTSEGS, MCLBYTES,
    555 		    0, 0, &descs[i].stx_dmamap);
    556 		if (rc != 0)
    557 			break;
    558 	}
    559 	return rc;
    560 }
    561 
    562 static __inline int
    563 rtw_rxdesc_dmamaps_create(bus_dma_tag_t dmat, struct rtw_rxctl *descs,
    564     u_int ndescs)
    565 {
    566 	int i, rc = 0;
    567 	for (i = 0; i < ndescs; i++) {
    568 		rc = bus_dmamap_create(dmat, MCLBYTES, 1, MCLBYTES, 0, 0,
    569 		    &descs[i].srx_dmamap);
    570 		if (rc != 0)
    571 			break;
    572 	}
    573 	return rc;
    574 }
    575 
    576 static __inline void
    577 rtw_rxctls_setup(struct rtw_rxctl *descs)
    578 {
    579 	int i;
    580 	for (i = 0; i < RTW_RXQLEN; i++)
    581 		descs[i].srx_mbuf = NULL;
    582 }
    583 
    584 static __inline void
    585 rtw_rxdesc_dmamaps_destroy(bus_dma_tag_t dmat, struct rtw_rxctl *descs,
    586     u_int ndescs)
    587 {
    588 	int i;
    589 	for (i = 0; i < ndescs; i++) {
    590 		if (descs[i].srx_dmamap != NULL)
    591 			bus_dmamap_destroy(dmat, descs[i].srx_dmamap);
    592 	}
    593 }
    594 
    595 static __inline void
    596 rtw_txdesc_dmamaps_destroy(bus_dma_tag_t dmat, struct rtw_txctl *descs,
    597     u_int ndescs)
    598 {
    599 	int i;
    600 	for (i = 0; i < ndescs; i++) {
    601 		if (descs[i].stx_dmamap != NULL)
    602 			bus_dmamap_destroy(dmat, descs[i].stx_dmamap);
    603 	}
    604 }
    605 
    606 static __inline void
    607 rtw_srom_free(struct rtw_srom *sr)
    608 {
    609 	sr->sr_size = 0;
    610 	if (sr->sr_content == NULL)
    611 		return;
    612 	free(sr->sr_content, M_DEVBUF);
    613 	sr->sr_content = NULL;
    614 }
    615 
    616 static void
    617 rtw_srom_defaults(struct rtw_srom *sr, u_int32_t *flags, u_int8_t *cs_threshold,
    618     enum rtw_rfchipid *rfchipid, u_int32_t *rcr)
    619 {
    620 	*flags |= (RTW_F_DIGPHY|RTW_F_ANTDIV);
    621 	*cs_threshold = RTW_SR_ENERGYDETTHR_DEFAULT;
    622 	*rcr |= RTW_RCR_ENCS1;
    623 	*rfchipid = RTW_RFCHIPID_PHILIPS;
    624 }
    625 
    626 static int
    627 rtw_srom_parse(struct rtw_srom *sr, u_int32_t *flags, u_int8_t *cs_threshold,
    628     enum rtw_rfchipid *rfchipid, u_int32_t *rcr, enum rtw_locale *locale,
    629     const char *dvname)
    630 {
    631 	int i;
    632 	const char *rfname, *paname;
    633 	char scratch[sizeof("unknown 0xXX")];
    634 	u_int16_t version;
    635 	u_int8_t mac[IEEE80211_ADDR_LEN];
    636 
    637 	*flags &= ~(RTW_F_DIGPHY|RTW_F_DFLANTB|RTW_F_ANTDIV);
    638 	*rcr &= ~(RTW_RCR_ENCS1 | RTW_RCR_ENCS2);
    639 
    640 	version = RTW_SR_GET16(sr, RTW_SR_VERSION);
    641 	printf("%s: SROM version %d.%d", dvname, version >> 8, version & 0xff);
    642 
    643 	if (version <= 0x0101) {
    644 		printf(" is not understood, limping along with defaults\n");
    645 		rtw_srom_defaults(sr, flags, cs_threshold, rfchipid, rcr);
    646 		return 0;
    647 	}
    648 	printf("\n");
    649 
    650 	for (i = 0; i < IEEE80211_ADDR_LEN; i++)
    651 		mac[i] = RTW_SR_GET(sr, RTW_SR_MAC + i);
    652 
    653 	RTW_DPRINTF(RTW_DEBUG_ATTACH,
    654 	    ("%s: EEPROM MAC %s\n", dvname, ether_sprintf(mac)));
    655 
    656 	*cs_threshold = RTW_SR_GET(sr, RTW_SR_ENERGYDETTHR);
    657 
    658 	if ((RTW_SR_GET(sr, RTW_SR_CONFIG2) & RTW_CONFIG2_ANT) != 0)
    659 		*flags |= RTW_F_ANTDIV;
    660 
    661 	/* Note well: the sense of the RTW_SR_RFPARM_DIGPHY bit seems
    662 	 * to be reversed.
    663 	 */
    664 	if ((RTW_SR_GET(sr, RTW_SR_RFPARM) & RTW_SR_RFPARM_DIGPHY) == 0)
    665 		*flags |= RTW_F_DIGPHY;
    666 	if ((RTW_SR_GET(sr, RTW_SR_RFPARM) & RTW_SR_RFPARM_DFLANTB) != 0)
    667 		*flags |= RTW_F_DFLANTB;
    668 
    669 	*rcr |= LSHIFT(MASK_AND_RSHIFT(RTW_SR_GET(sr, RTW_SR_RFPARM),
    670 	    RTW_SR_RFPARM_CS_MASK), RTW_RCR_ENCS1);
    671 
    672 	*rfchipid = RTW_SR_GET(sr, RTW_SR_RFCHIPID);
    673 	switch (*rfchipid) {
    674 	case RTW_RFCHIPID_GCT:		/* this combo seen in the wild */
    675 		rfname = "GCT GRF5101";
    676 		paname = "Winspring WS9901";
    677 		break;
    678 	case RTW_RFCHIPID_MAXIM:
    679 		rfname = "Maxim MAX2820";	/* guess */
    680 		paname = "Maxim MAX2422";	/* guess */
    681 		break;
    682 	case RTW_RFCHIPID_INTERSIL:
    683 		rfname = "Intersil HFA3873";	/* guess */
    684 		paname = "Intersil <unknown>";
    685 		break;
    686 	case RTW_RFCHIPID_PHILIPS:	/* this combo seen in the wild */
    687 		rfname = "Philips SA2400A";
    688 		paname = "Philips SA2411";
    689 		break;
    690 	case RTW_RFCHIPID_RFMD:
    691 		/* this is the same front-end as an atw(4)! */
    692 		rfname = "RFMD RF2948B, "	/* mentioned in Realtek docs */
    693 			 "LNA: RFMD RF2494, "	/* mentioned in Realtek docs */
    694 			 "SYN: Silicon Labs Si4126";	/* inferred from
    695 			 				 * reference driver
    696 							 */
    697 		paname = "RFMD RF2189";		/* mentioned in Realtek docs */
    698 		break;
    699 	case RTW_RFCHIPID_RESERVED:
    700 		rfname = paname = "reserved";
    701 		break;
    702 	default:
    703 		snprintf(scratch, sizeof(scratch), "unknown 0x%02x", *rfchipid);
    704 		rfname = paname = scratch;
    705 	}
    706 	printf("%s: RF: %s, PA: %s\n", dvname, rfname, paname);
    707 
    708 	switch (RTW_SR_GET(sr, RTW_SR_CONFIG0) & RTW_CONFIG0_GL_MASK) {
    709 	case RTW_CONFIG0_GL_USA:
    710 		*locale = RTW_LOCALE_USA;
    711 		break;
    712 	case RTW_CONFIG0_GL_EUROPE:
    713 		*locale = RTW_LOCALE_EUROPE;
    714 		break;
    715 	case RTW_CONFIG0_GL_JAPAN:
    716 		*locale = RTW_LOCALE_JAPAN;
    717 		break;
    718 	default:
    719 		*locale = RTW_LOCALE_UNKNOWN;
    720 		break;
    721 	}
    722 	return 0;
    723 }
    724 
    725 /* Returns -1 on failure. */
    726 static int
    727 rtw_srom_read(struct rtw_regs *regs, u_int32_t flags, struct rtw_srom *sr,
    728     const char *dvname)
    729 {
    730 	int rc;
    731 	struct seeprom_descriptor sd;
    732 	u_int8_t ecr;
    733 
    734 	(void)memset(&sd, 0, sizeof(sd));
    735 
    736 	ecr = RTW_READ8(regs, RTW_9346CR);
    737 
    738 	if ((flags & RTW_F_9356SROM) != 0) {
    739 		RTW_DPRINTF(RTW_DEBUG_ATTACH, ("%s: 93c56 SROM\n", dvname));
    740 		sr->sr_size = 256;
    741 		sd.sd_chip = C56_66;
    742 	} else {
    743 		RTW_DPRINTF(RTW_DEBUG_ATTACH, ("%s: 93c46 SROM\n", dvname));
    744 		sr->sr_size = 128;
    745 		sd.sd_chip = C46;
    746 	}
    747 
    748 	ecr &= ~(RTW_9346CR_EEDI | RTW_9346CR_EEDO | RTW_9346CR_EESK |
    749 	    RTW_9346CR_EEM_MASK);
    750 	ecr |= RTW_9346CR_EEM_PROGRAM;
    751 
    752 	RTW_WRITE8(regs, RTW_9346CR, ecr);
    753 
    754 	sr->sr_content = malloc(sr->sr_size, M_DEVBUF, M_NOWAIT);
    755 
    756 	if (sr->sr_content == NULL) {
    757 		printf("%s: unable to allocate SROM buffer\n", dvname);
    758 		return ENOMEM;
    759 	}
    760 
    761 	(void)memset(sr->sr_content, 0, sr->sr_size);
    762 
    763 	/* RTL8180 has a single 8-bit register for controlling the
    764 	 * 93cx6 SROM.  There is no "ready" bit. The RTL8180
    765 	 * input/output sense is the reverse of read_seeprom's.
    766 	 */
    767 	sd.sd_tag = regs->r_bt;
    768 	sd.sd_bsh = regs->r_bh;
    769 	sd.sd_regsize = 1;
    770 	sd.sd_control_offset = RTW_9346CR;
    771 	sd.sd_status_offset = RTW_9346CR;
    772 	sd.sd_dataout_offset = RTW_9346CR;
    773 	sd.sd_CK = RTW_9346CR_EESK;
    774 	sd.sd_CS = RTW_9346CR_EECS;
    775 	sd.sd_DI = RTW_9346CR_EEDO;
    776 	sd.sd_DO = RTW_9346CR_EEDI;
    777 	/* make read_seeprom enter EEPROM read/write mode */
    778 	sd.sd_MS = ecr;
    779 	sd.sd_RDY = 0;
    780 #if 0
    781 	sd.sd_clkdelay = 50;
    782 #endif
    783 
    784 	/* TBD bus barriers */
    785 	if (!read_seeprom(&sd, sr->sr_content, 0, sr->sr_size/2)) {
    786 		printf("%s: could not read SROM\n", dvname);
    787 		free(sr->sr_content, M_DEVBUF);
    788 		sr->sr_content = NULL;
    789 		return -1;	/* XXX */
    790 	}
    791 
    792 	/* end EEPROM read/write mode */
    793 	RTW_WRITE8(regs, RTW_9346CR,
    794 	    (ecr & ~RTW_9346CR_EEM_MASK) | RTW_9346CR_EEM_NORMAL);
    795 	RTW_WBRW(regs, RTW_9346CR, RTW_9346CR);
    796 
    797 	if ((rc = rtw_recall_eeprom(regs, dvname)) != 0)
    798 		return rc;
    799 
    800 #ifdef RTW_DEBUG
    801 	{
    802 		int i;
    803 		RTW_DPRINTF(RTW_DEBUG_ATTACH,
    804 		    ("\n%s: serial ROM:\n\t", dvname));
    805 		for (i = 0; i < sr->sr_size/2; i++) {
    806 			if (((i % 8) == 0) && (i != 0))
    807 				RTW_DPRINTF(RTW_DEBUG_ATTACH, ("\n\t"));
    808 			RTW_DPRINTF(RTW_DEBUG_ATTACH,
    809 			    (" %04x", sr->sr_content[i]));
    810 		}
    811 		RTW_DPRINTF(RTW_DEBUG_ATTACH, ("\n"));
    812 	}
    813 #endif /* RTW_DEBUG */
    814 	return 0;
    815 }
    816 
    817 static void
    818 rtw_set_rfprog(struct rtw_regs *regs, enum rtw_rfchipid rfchipid,
    819     const char *dvname)
    820 {
    821 	u_int8_t cfg4;
    822 	const char *method;
    823 
    824 	cfg4 = RTW_READ8(regs, RTW_CONFIG4) & ~RTW_CONFIG4_RFTYPE_MASK;
    825 
    826 	switch (rfchipid) {
    827 	default:
    828 		cfg4 |= LSHIFT(rtw_rfprog_fallback, RTW_CONFIG4_RFTYPE_MASK);
    829 		method = "fallback";
    830 		break;
    831 	case RTW_RFCHIPID_INTERSIL:
    832 		cfg4 |= RTW_CONFIG4_RFTYPE_INTERSIL;
    833 		method = "Intersil";
    834 		break;
    835 	case RTW_RFCHIPID_PHILIPS:
    836 		cfg4 |= RTW_CONFIG4_RFTYPE_PHILIPS;
    837 		method = "Philips";
    838 		break;
    839 	case RTW_RFCHIPID_RFMD:
    840 		cfg4 |= RTW_CONFIG4_RFTYPE_RFMD;
    841 		method = "RFMD";
    842 		break;
    843 	}
    844 
    845 	RTW_WRITE8(regs, RTW_CONFIG4, cfg4);
    846 
    847 	RTW_WBR(regs, RTW_CONFIG4, RTW_CONFIG4);
    848 
    849 	RTW_DPRINTF(RTW_DEBUG_INIT,
    850 	    ("%s: %s RF programming method, %#02x\n", dvname, method,
    851 	    RTW_READ8(regs, RTW_CONFIG4)));
    852 }
    853 
    854 #if 0
    855 static __inline int
    856 rtw_identify_rf(struct rtw_regs *regs, enum rtw_rftype *rftype,
    857     const char *dvname)
    858 {
    859 	u_int8_t cfg4;
    860 	const char *name;
    861 
    862 	cfg4 = RTW_READ8(regs, RTW_CONFIG4);
    863 
    864 	switch (cfg4 & RTW_CONFIG4_RFTYPE_MASK) {
    865 	case RTW_CONFIG4_RFTYPE_PHILIPS:
    866 		*rftype = RTW_RFTYPE_PHILIPS;
    867 		name = "Philips";
    868 		break;
    869 	case RTW_CONFIG4_RFTYPE_INTERSIL:
    870 		*rftype = RTW_RFTYPE_INTERSIL;
    871 		name = "Intersil";
    872 		break;
    873 	case RTW_CONFIG4_RFTYPE_RFMD:
    874 		*rftype = RTW_RFTYPE_RFMD;
    875 		name = "RFMD";
    876 		break;
    877 	default:
    878 		name = "<unknown>";
    879 		return ENXIO;
    880 	}
    881 
    882 	printf("%s: RF prog type %s\n", dvname, name);
    883 	return 0;
    884 }
    885 #endif
    886 
    887 static __inline void
    888 rtw_init_channels(enum rtw_locale locale,
    889     struct ieee80211_channel (*chans)[IEEE80211_CHAN_MAX+1],
    890     const char *dvname)
    891 {
    892 	int i;
    893 	const char *name = NULL;
    894 #define ADD_CHANNEL(_chans, _chan) do {			\
    895 	(*_chans)[_chan].ic_flags = IEEE80211_CHAN_B;		\
    896 	(*_chans)[_chan].ic_freq =				\
    897 	    ieee80211_ieee2mhz(_chan, (*_chans)[_chan].ic_flags);\
    898 } while (0)
    899 
    900 	switch (locale) {
    901 	case RTW_LOCALE_USA:	/* 1-11 */
    902 		name = "USA";
    903 		for (i = 1; i <= 11; i++)
    904 			ADD_CHANNEL(chans, i);
    905 		break;
    906 	case RTW_LOCALE_JAPAN:	/* 1-14 */
    907 		name = "Japan";
    908 		ADD_CHANNEL(chans, 14);
    909 		for (i = 1; i <= 14; i++)
    910 			ADD_CHANNEL(chans, i);
    911 		break;
    912 	case RTW_LOCALE_EUROPE:	/* 1-13 */
    913 		name = "Europe";
    914 		for (i = 1; i <= 13; i++)
    915 			ADD_CHANNEL(chans, i);
    916 		break;
    917 	default:			/* 10-11 allowed by most countries */
    918 		name = "<unknown>";
    919 		for (i = 10; i <= 11; i++)
    920 			ADD_CHANNEL(chans, i);
    921 		break;
    922 	}
    923 	printf("%s: Geographic Location %s\n", dvname, name);
    924 #undef ADD_CHANNEL
    925 }
    926 
    927 static __inline void
    928 rtw_identify_country(struct rtw_regs *regs, enum rtw_locale *locale,
    929     const char *dvname)
    930 {
    931 	u_int8_t cfg0 = RTW_READ8(regs, RTW_CONFIG0);
    932 
    933 	switch (cfg0 & RTW_CONFIG0_GL_MASK) {
    934 	case RTW_CONFIG0_GL_USA:
    935 		*locale = RTW_LOCALE_USA;
    936 		break;
    937 	case RTW_CONFIG0_GL_JAPAN:
    938 		*locale = RTW_LOCALE_JAPAN;
    939 		break;
    940 	case RTW_CONFIG0_GL_EUROPE:
    941 		*locale = RTW_LOCALE_EUROPE;
    942 		break;
    943 	default:
    944 		*locale = RTW_LOCALE_UNKNOWN;
    945 		break;
    946 	}
    947 }
    948 
    949 static __inline int
    950 rtw_identify_sta(struct rtw_regs *regs, u_int8_t (*addr)[IEEE80211_ADDR_LEN],
    951     const char *dvname)
    952 {
    953 	static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
    954 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    955 	};
    956 	u_int32_t idr0 = RTW_READ(regs, RTW_IDR0),
    957 	          idr1 = RTW_READ(regs, RTW_IDR1);
    958 
    959 	(*addr)[0] = MASK_AND_RSHIFT(idr0, BITS(0,  7));
    960 	(*addr)[1] = MASK_AND_RSHIFT(idr0, BITS(8,  15));
    961 	(*addr)[2] = MASK_AND_RSHIFT(idr0, BITS(16, 23));
    962 	(*addr)[3] = MASK_AND_RSHIFT(idr0, BITS(24 ,31));
    963 
    964 	(*addr)[4] = MASK_AND_RSHIFT(idr1, BITS(0,  7));
    965 	(*addr)[5] = MASK_AND_RSHIFT(idr1, BITS(8, 15));
    966 
    967 	if (IEEE80211_ADDR_EQ(addr, empty_macaddr)) {
    968 		printf("%s: could not get mac address, attach failed\n",
    969 		    dvname);
    970 		return ENXIO;
    971 	}
    972 
    973 	printf("%s: 802.11 address %s\n", dvname, ether_sprintf(*addr));
    974 
    975 	return 0;
    976 }
    977 
    978 static u_int8_t
    979 rtw_chan2txpower(struct rtw_srom *sr, struct ieee80211com *ic,
    980     struct ieee80211_channel *chan)
    981 {
    982 	u_int idx = RTW_SR_TXPOWER1 + ieee80211_chan2ieee(ic, chan) - 1;
    983 	KASSERT2(idx >= RTW_SR_TXPOWER1 && idx <= RTW_SR_TXPOWER14,
    984 	    ("%s: channel %d out of range", __func__,
    985 	     idx - RTW_SR_TXPOWER1 + 1));
    986 	return RTW_SR_GET(sr, idx);
    987 }
    988 
    989 static void
    990 rtw_txdesc_blk_init_all(struct rtw_txdesc_blk *htcs)
    991 {
    992 	int pri;
    993 	u_int ndesc[RTW_NTXPRI] =
    994 	    {RTW_NTXDESCLO, RTW_NTXDESCMD, RTW_NTXDESCHI, RTW_NTXDESCBCN};
    995 
    996 	for (pri = 0; pri < RTW_NTXPRI; pri++) {
    997 		htcs[pri].htc_nfree = ndesc[pri];
    998 		htcs[pri].htc_next = 0;
    999 	}
   1000 }
   1001 
   1002 static int
   1003 rtw_txctl_blk_init(struct rtw_txctl_blk *stc)
   1004 {
   1005 	int i;
   1006 	struct rtw_txctl *stx;
   1007 
   1008 	SIMPLEQ_INIT(&stc->stc_dirtyq);
   1009 	SIMPLEQ_INIT(&stc->stc_freeq);
   1010 	for (i = 0; i < stc->stc_ndesc; i++) {
   1011 		stx = &stc->stc_desc[i];
   1012 		stx->stx_mbuf = NULL;
   1013 		SIMPLEQ_INSERT_TAIL(&stc->stc_freeq, stx, stx_q);
   1014 	}
   1015 	return 0;
   1016 }
   1017 
   1018 static void
   1019 rtw_txctl_blk_init_all(struct rtw_txctl_blk *stcs)
   1020 {
   1021 	int pri;
   1022 	for (pri = 0; pri < RTW_NTXPRI; pri++)
   1023 		rtw_txctl_blk_init(&stcs[pri]);
   1024 }
   1025 
   1026 static __inline void
   1027 rtw_rxdescs_sync(bus_dma_tag_t dmat, bus_dmamap_t dmap, u_int desc0, u_int
   1028     nsync, int ops)
   1029 {
   1030 	KASSERT(nsync <= RTW_RXQLEN);
   1031 	/* sync to end of ring */
   1032 	if (desc0 + nsync > RTW_RXQLEN) {
   1033 		bus_dmamap_sync(dmat, dmap,
   1034 		    offsetof(struct rtw_descs, hd_rx[desc0]),
   1035 		    sizeof(struct rtw_rxdesc) * (RTW_RXQLEN - desc0), ops);
   1036 		nsync -= (RTW_RXQLEN - desc0);
   1037 		desc0 = 0;
   1038 	}
   1039 
   1040 	KASSERT(desc0 < RTW_RXQLEN);
   1041 	KASSERT(nsync <= RTW_RXQLEN);
   1042 	KASSERT(desc0 + nsync <= RTW_RXQLEN);
   1043 
   1044 	/* sync what remains */
   1045 	bus_dmamap_sync(dmat, dmap,
   1046 	    offsetof(struct rtw_descs, hd_rx[desc0]),
   1047 	    sizeof(struct rtw_rxdesc) * nsync, ops);
   1048 }
   1049 
   1050 static void
   1051 rtw_txdescs_sync(bus_dma_tag_t dmat, bus_dmamap_t dmap,
   1052     struct rtw_txdesc_blk *htc, u_int desc0, u_int nsync, int ops)
   1053 {
   1054 	/* sync to end of ring */
   1055 	if (desc0 + nsync > htc->htc_ndesc) {
   1056 		bus_dmamap_sync(dmat, dmap,
   1057 		    htc->htc_ofs + sizeof(struct rtw_txdesc) * desc0,
   1058 		    sizeof(struct rtw_txdesc) * (htc->htc_ndesc - desc0),
   1059 		    ops);
   1060 		nsync -= (htc->htc_ndesc - desc0);
   1061 		desc0 = 0;
   1062 	}
   1063 
   1064 	/* sync what remains */
   1065 	bus_dmamap_sync(dmat, dmap,
   1066 	    htc->htc_ofs + sizeof(struct rtw_txdesc) * desc0,
   1067 	    sizeof(struct rtw_txdesc) * nsync, ops);
   1068 }
   1069 
   1070 static void
   1071 rtw_txdescs_sync_all(bus_dma_tag_t dmat, bus_dmamap_t dmap,
   1072     struct rtw_txdesc_blk *htcs)
   1073 {
   1074 	int pri;
   1075 	for (pri = 0; pri < RTW_NTXPRI; pri++) {
   1076 		rtw_txdescs_sync(dmat, dmap,
   1077 		    &htcs[pri], 0, htcs[pri].htc_ndesc,
   1078 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1079 	}
   1080 }
   1081 
   1082 static void
   1083 rtw_rxbufs_release(bus_dma_tag_t dmat, struct rtw_rxctl *desc)
   1084 {
   1085 	int i;
   1086 	struct rtw_rxctl *srx;
   1087 
   1088 	for (i = 0; i < RTW_RXQLEN; i++) {
   1089 		srx = &desc[i];
   1090 		bus_dmamap_sync(dmat, srx->srx_dmamap, 0,
   1091 		    srx->srx_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1092 		bus_dmamap_unload(dmat, srx->srx_dmamap);
   1093 		m_freem(srx->srx_mbuf);
   1094 		srx->srx_mbuf = NULL;
   1095 	}
   1096 }
   1097 
   1098 static __inline int
   1099 rtw_rxbuf_alloc(bus_dma_tag_t dmat, struct rtw_rxctl *srx)
   1100 {
   1101 	int rc;
   1102 	struct mbuf *m;
   1103 
   1104 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1105 	if (m == NULL)
   1106 		return ENOBUFS;
   1107 
   1108 	MCLGET(m, M_DONTWAIT);
   1109 	if (m == NULL)
   1110 		return ENOBUFS;
   1111 
   1112 	m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
   1113 
   1114 	if (srx->srx_mbuf != NULL)
   1115 		bus_dmamap_unload(dmat, srx->srx_dmamap);
   1116 
   1117 	srx->srx_mbuf = NULL;
   1118 
   1119 	rc = bus_dmamap_load_mbuf(dmat, srx->srx_dmamap, m, BUS_DMA_NOWAIT);
   1120 	if (rc != 0) {
   1121 		m_freem(m);
   1122 		return -1;
   1123 	}
   1124 
   1125 	srx->srx_mbuf = m;
   1126 
   1127 	return 0;
   1128 }
   1129 
   1130 static int
   1131 rtw_rxctl_init_all(bus_dma_tag_t dmat, struct rtw_rxctl *desc,
   1132     u_int *next, const char *dvname)
   1133 {
   1134 	int i, rc;
   1135 	struct rtw_rxctl *srx;
   1136 
   1137 	for (i = 0; i < RTW_RXQLEN; i++) {
   1138 		srx = &desc[i];
   1139 		if ((rc = rtw_rxbuf_alloc(dmat, srx)) == 0)
   1140 			continue;
   1141 		printf("%s: failed rtw_rxbuf_alloc after %d buffers, rc = %d\n",
   1142 		    dvname, i, rc);
   1143 		if (i == 0) {
   1144 			rtw_rxbufs_release(dmat, desc);
   1145 			return rc;
   1146 		}
   1147 	}
   1148 	*next = 0;
   1149 	return 0;
   1150 }
   1151 
   1152 static __inline void
   1153 rtw_rxdesc_init(bus_dma_tag_t dmat, bus_dmamap_t dmam,
   1154     struct rtw_rxdesc *hrx, struct rtw_rxctl *srx, int idx, int kick)
   1155 {
   1156 	int is_last = (idx == RTW_RXQLEN - 1);
   1157 	uint32_t ctl, octl, obuf;
   1158 
   1159 	obuf = hrx->hrx_buf;
   1160 	hrx->hrx_buf = htole32(srx->srx_dmamap->dm_segs[0].ds_addr);
   1161 
   1162 	ctl = LSHIFT(srx->srx_mbuf->m_len, RTW_RXCTL_LENGTH_MASK) |
   1163 	    RTW_RXCTL_OWN | RTW_RXCTL_FS | RTW_RXCTL_LS;
   1164 
   1165 	if (is_last)
   1166 		ctl |= RTW_RXCTL_EOR;
   1167 
   1168 	octl = hrx->hrx_ctl;
   1169 	hrx->hrx_ctl = htole32(ctl);
   1170 
   1171 	RTW_DPRINTF(
   1172 	    kick ? (RTW_DEBUG_RECV_DESC | RTW_DEBUG_IO_KICK)
   1173 	         : RTW_DEBUG_RECV_DESC,
   1174 	    ("%s: hrx %p buf %08x -> %08x ctl %08x -> %08x\n", __func__, hrx,
   1175 	     le32toh(obuf), le32toh(hrx->hrx_buf), le32toh(octl),
   1176 	     le32toh(hrx->hrx_ctl)));
   1177 
   1178 	/* sync the mbuf */
   1179 	bus_dmamap_sync(dmat, srx->srx_dmamap, 0, srx->srx_dmamap->dm_mapsize,
   1180 	    BUS_DMASYNC_PREREAD);
   1181 
   1182 	/* sync the descriptor */
   1183 	bus_dmamap_sync(dmat, dmam, RTW_DESC_OFFSET(hd_rx, idx),
   1184 	    sizeof(struct rtw_rxdesc),
   1185 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1186 }
   1187 
   1188 static void
   1189 rtw_rxdesc_init_all(bus_dma_tag_t dmat, bus_dmamap_t dmam,
   1190     struct rtw_rxdesc *desc, struct rtw_rxctl *ctl, int kick)
   1191 {
   1192 	int i;
   1193 	struct rtw_rxdesc *hrx;
   1194 	struct rtw_rxctl *srx;
   1195 
   1196 	for (i = 0; i < RTW_RXQLEN; i++) {
   1197 		hrx = &desc[i];
   1198 		srx = &ctl[i];
   1199 		rtw_rxdesc_init(dmat, dmam, hrx, srx, i, kick);
   1200 	}
   1201 }
   1202 
   1203 static void
   1204 rtw_io_enable(struct rtw_regs *regs, u_int8_t flags, int enable)
   1205 {
   1206 	u_int8_t cr;
   1207 
   1208 	RTW_DPRINTF(RTW_DEBUG_IOSTATE, ("%s: %s 0x%02x\n", __func__,
   1209 	    enable ? "enable" : "disable", flags));
   1210 
   1211 	cr = RTW_READ8(regs, RTW_CR);
   1212 
   1213 	/* XXX reference source does not enable MULRW */
   1214 #if 0
   1215 	/* enable PCI Read/Write Multiple */
   1216 	cr |= RTW_CR_MULRW;
   1217 #endif
   1218 
   1219 	RTW_RBW(regs, RTW_CR, RTW_CR);	/* XXX paranoia? */
   1220 	if (enable)
   1221 		cr |= flags;
   1222 	else
   1223 		cr &= ~flags;
   1224 	RTW_WRITE8(regs, RTW_CR, cr);
   1225 	RTW_SYNC(regs, RTW_CR, RTW_CR);
   1226 }
   1227 
   1228 static void
   1229 rtw_intr_rx(struct rtw_softc *sc, u_int16_t isr)
   1230 {
   1231 	u_int next, nproc = 0;
   1232 	int len, rate, rssi;
   1233 	u_int32_t hrssi, hstat, htsfth, htsftl;
   1234 	struct rtw_rxdesc *hrx;
   1235 	struct rtw_rxctl *srx;
   1236 	struct mbuf *m;
   1237 
   1238 	struct ieee80211_node *ni;
   1239 	struct ieee80211_frame *wh;
   1240 
   1241 	KASSERT(sc->sc_rxnext < RTW_RXQLEN);
   1242 
   1243 	for (next = sc->sc_rxnext; ; next = (next + 1) % RTW_RXQLEN) {
   1244 		rtw_rxdescs_sync(sc->sc_dmat, sc->sc_desc_dmamap,
   1245 		    next, 1, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1246 		hrx = &sc->sc_rxdesc[next];
   1247 		srx = &sc->sc_rxctl[next];
   1248 
   1249 		hstat = le32toh(hrx->hrx_stat);
   1250 		hrssi = le32toh(hrx->hrx_rssi);
   1251 		htsfth = le32toh(hrx->hrx_tsfth);
   1252 		htsftl = le32toh(hrx->hrx_tsftl);
   1253 
   1254 		RTW_DPRINTF(RTW_DEBUG_RECV_DESC,
   1255 		    ("%s: rxdesc[%d] hstat %08x hrssi %08x htsft %08x%08x\n",
   1256 		    __func__, next, hstat, hrssi, htsfth, htsftl));
   1257 
   1258 		KASSERT((hstat & (RTW_RXSTAT_FS|RTW_RXSTAT_LS)) ==
   1259 		    (RTW_RXSTAT_FS|RTW_RXSTAT_LS));
   1260 
   1261 		++nproc;
   1262 
   1263 		/* still belongs to NIC */
   1264 		if ((hstat & RTW_RXSTAT_OWN) != 0) {
   1265 			if (nproc > 1)
   1266 				break;
   1267 
   1268 			/* sometimes the NIC skips to the 0th descriptor */
   1269 			rtw_rxdescs_sync(sc->sc_dmat, sc->sc_desc_dmamap,
   1270 			    0, 1, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1271 			hrx = &sc->sc_rxdesc[0];
   1272 			if ((hrx->hrx_stat & htole32(RTW_RXSTAT_OWN)) != 0)
   1273 				break;
   1274 			RTW_DPRINTF(RTW_DEBUG_BUGS,
   1275 			    ("%s: NIC skipped to rxdesc[0]\n",
   1276 			     sc->sc_dev.dv_xname));
   1277 			next = 0;
   1278 			continue;
   1279 		}
   1280 
   1281 		if ((hstat & RTW_RXSTAT_IOERROR) != 0) {
   1282 			printf("%s: DMA error/FIFO overflow %08x, "
   1283 			    "rx descriptor %d\n", sc->sc_dev.dv_xname,
   1284 			    hstat & RTW_RXSTAT_IOERROR, next);
   1285 			goto next;
   1286 		}
   1287 
   1288 		len = MASK_AND_RSHIFT(hstat, RTW_RXSTAT_LENGTH_MASK);
   1289 		if (len < IEEE80211_MIN_LEN) {
   1290 			sc->sc_ic.ic_stats.is_rx_tooshort++;
   1291 			goto next;
   1292 		}
   1293 
   1294 		switch (hstat & RTW_RXSTAT_RATE_MASK) {
   1295 		case RTW_RXSTAT_RATE_1MBPS:
   1296 			rate = 2;
   1297 			break;
   1298 		case RTW_RXSTAT_RATE_2MBPS:
   1299 			rate = 4;
   1300 			break;
   1301 		case RTW_RXSTAT_RATE_5MBPS:
   1302 			rate = 11;
   1303 			break;
   1304 		case RTW_RXSTAT_RATE_11MBPS:
   1305 			rate = 22;
   1306 			break;
   1307 		default:
   1308 			printf("%s: unknown rate #%d\n", sc->sc_dev.dv_xname,
   1309 			    MASK_AND_RSHIFT(hstat, RTW_RXSTAT_RATE_MASK));
   1310 			goto next;
   1311 		}
   1312 
   1313 #ifdef RTW_DEBUG
   1314 #define PRINTSTAT(flag) do { \
   1315 	if ((hstat & flag) != 0) { \
   1316 		printf("%s" #flag, delim); \
   1317 		delim = ","; \
   1318 	} \
   1319 } while (0)
   1320 		if ((rtw_debug & RTW_DEBUG_RECV_DESC) != 0) {
   1321 			const char *delim = "<";
   1322 			printf("%s: ", sc->sc_dev.dv_xname);
   1323 			if ((hstat & RTW_RXSTAT_DEBUG) != 0) {
   1324 				printf("status %08x", hstat);
   1325 				PRINTSTAT(RTW_RXSTAT_SPLCP);
   1326 				PRINTSTAT(RTW_RXSTAT_MAR);
   1327 				PRINTSTAT(RTW_RXSTAT_PAR);
   1328 				PRINTSTAT(RTW_RXSTAT_BAR);
   1329 				PRINTSTAT(RTW_RXSTAT_PWRMGT);
   1330 				PRINTSTAT(RTW_RXSTAT_CRC32);
   1331 				PRINTSTAT(RTW_RXSTAT_ICV);
   1332 				printf(">, ");
   1333 			}
   1334 			printf("rate %d.%d Mb/s, time %08x%08x\n",
   1335 			    (rate * 5) / 10, (rate * 5) % 10, htsfth, htsftl);
   1336 		}
   1337 #endif /* RTW_DEBUG */
   1338 
   1339 		if ((hstat & RTW_RXSTAT_RES) != 0 &&
   1340 		    sc->sc_ic.ic_opmode != IEEE80211_M_MONITOR)
   1341 			goto next;
   1342 
   1343 		/* if bad flags, skip descriptor */
   1344 		if ((hstat & RTW_RXSTAT_ONESEG) != RTW_RXSTAT_ONESEG) {
   1345 			printf("%s: too many rx segments\n",
   1346 			    sc->sc_dev.dv_xname);
   1347 			goto next;
   1348 		}
   1349 
   1350 		bus_dmamap_sync(sc->sc_dmat, srx->srx_dmamap, 0,
   1351 		    srx->srx_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1352 
   1353 		m = srx->srx_mbuf;
   1354 
   1355 		/* if temporarily out of memory, re-use mbuf */
   1356 		switch (rtw_rxbuf_alloc(sc->sc_dmat, srx)) {
   1357 		case 0:
   1358 			break;
   1359 		case ENOBUFS:
   1360 			printf("%s: rtw_rxbuf_alloc(, %d) failed, "
   1361 			    "dropping this packet\n", sc->sc_dev.dv_xname,
   1362 			    next);
   1363 			goto next;
   1364 		default:
   1365 			/* XXX shorten rx ring, instead? */
   1366 			panic("%s: could not load DMA map\n",
   1367 			    sc->sc_dev.dv_xname);
   1368 		}
   1369 
   1370 		if (sc->sc_rfchipid == RTW_RFCHIPID_PHILIPS)
   1371 			rssi = MASK_AND_RSHIFT(hrssi, RTW_RXRSSI_RSSI);
   1372 		else {
   1373 			rssi = MASK_AND_RSHIFT(hrssi, RTW_RXRSSI_IMR_RSSI);
   1374 			/* TBD find out each front-end's LNA gain in the
   1375 			 * front-end's units
   1376 			 */
   1377 			if ((hrssi & RTW_RXRSSI_IMR_LNA) == 0)
   1378 				rssi |= 0x80;
   1379 		}
   1380 
   1381 		m->m_pkthdr.rcvif = &sc->sc_if;
   1382 		m->m_pkthdr.len = m->m_len = len;
   1383 		m->m_flags |= M_HASFCS;
   1384 
   1385 		wh = mtod(m, struct ieee80211_frame *);
   1386 		/* TBD use _MAR, _BAR, _PAR flags as hints to _find_rxnode? */
   1387 		ni = ieee80211_find_rxnode(&sc->sc_ic, wh);
   1388 
   1389 		sc->sc_tsfth = htsfth;
   1390 
   1391 #ifdef RTW_DEBUG
   1392 		if ((sc->sc_if.if_flags & (IFF_DEBUG|IFF_LINK2)) ==
   1393 		    (IFF_DEBUG|IFF_LINK2)) {
   1394 			ieee80211_dump_pkt(mtod(m, uint8_t *), m->m_pkthdr.len,
   1395 			    rate, rssi);
   1396 		}
   1397 #endif /* RTW_DEBUG */
   1398 		ieee80211_input(&sc->sc_if, m, ni, rssi, htsftl);
   1399 		ieee80211_release_node(&sc->sc_ic, ni);
   1400 next:
   1401 		rtw_rxdesc_init(sc->sc_dmat, sc->sc_desc_dmamap,
   1402 		    hrx, srx, next, 0);
   1403 	}
   1404 	KASSERT(sc->sc_rxnext < RTW_RXQLEN);
   1405 
   1406 	sc->sc_rxnext = next;
   1407 
   1408 	return;
   1409 }
   1410 
   1411 static void
   1412 rtw_txbuf_release(bus_dma_tag_t dmat, struct ieee80211com *ic,
   1413     struct rtw_txctl *stx)
   1414 {
   1415 	struct mbuf *m;
   1416 	struct ieee80211_node *ni;
   1417 
   1418 	m = stx->stx_mbuf;
   1419 	ni = stx->stx_ni;
   1420 	KASSERT(m != NULL);
   1421 	KASSERT(ni != NULL);
   1422 	stx->stx_mbuf = NULL;
   1423 	stx->stx_ni = NULL;
   1424 
   1425 	bus_dmamap_sync(dmat, stx->stx_dmamap, 0, stx->stx_dmamap->dm_mapsize,
   1426 	    BUS_DMASYNC_POSTWRITE);
   1427 	bus_dmamap_unload(dmat, stx->stx_dmamap);
   1428 	m_freem(m);
   1429 	ieee80211_release_node(ic, ni);
   1430 }
   1431 
   1432 static void
   1433 rtw_txbufs_release(bus_dma_tag_t dmat, bus_dmamap_t desc_dmamap,
   1434     struct ieee80211com *ic, struct rtw_txctl_blk *stc)
   1435 {
   1436 	struct rtw_txctl *stx;
   1437 
   1438 	while ((stx = SIMPLEQ_FIRST(&stc->stc_dirtyq)) != NULL) {
   1439 		rtw_txbuf_release(dmat, ic, stx);
   1440 		SIMPLEQ_REMOVE_HEAD(&stc->stc_dirtyq, stx_q);
   1441 		SIMPLEQ_INSERT_TAIL(&stc->stc_freeq, stx, stx_q);
   1442 	}
   1443 }
   1444 
   1445 static __inline void
   1446 rtw_collect_txpkt(struct rtw_softc *sc, struct rtw_txdesc_blk *htc,
   1447     struct rtw_txctl *stx, int ndesc)
   1448 {
   1449 	uint32_t hstat;
   1450 	int data_retry, rts_retry;
   1451 	struct rtw_txdesc *htxn;
   1452 	const char *condstring;
   1453 
   1454 	rtw_txbuf_release(sc->sc_dmat, &sc->sc_ic, stx);
   1455 
   1456 	htc->htc_nfree += ndesc;
   1457 
   1458 	htxn = &htc->htc_desc[stx->stx_last];
   1459 
   1460 	hstat = le32toh(htxn->htx_stat);
   1461 	rts_retry = MASK_AND_RSHIFT(hstat, RTW_TXSTAT_RTSRETRY_MASK);
   1462 	data_retry = MASK_AND_RSHIFT(hstat, RTW_TXSTAT_DRC_MASK);
   1463 
   1464 	sc->sc_if.if_collisions += rts_retry + data_retry;
   1465 
   1466 	if ((hstat & RTW_TXSTAT_TOK) != 0)
   1467 		condstring = "ok";
   1468 	else {
   1469 		sc->sc_if.if_oerrors++;
   1470 		condstring = "error";
   1471 	}
   1472 
   1473 	DPRINTF(sc, RTW_DEBUG_XMIT_DESC,
   1474 	    ("%s: stx %p txdesc[%d, %d] %s tries rts %u data %u\n",
   1475 	    sc->sc_dev.dv_xname, stx, stx->stx_first, stx->stx_last,
   1476 	    condstring, rts_retry, data_retry));
   1477 }
   1478 
   1479 /* Collect transmitted packets. */
   1480 static __inline void
   1481 rtw_collect_txring(struct rtw_softc *sc, struct rtw_txctl_blk *stc,
   1482     struct rtw_txdesc_blk *htc)
   1483 {
   1484 	int ndesc;
   1485 	struct rtw_txctl *stx;
   1486 
   1487 	while ((stx = SIMPLEQ_FIRST(&stc->stc_dirtyq)) != NULL) {
   1488 		ndesc = 1 + stx->stx_last - stx->stx_first;
   1489 		if (stx->stx_last < stx->stx_first)
   1490 			ndesc += htc->htc_ndesc;
   1491 
   1492 		KASSERT(ndesc > 0);
   1493 
   1494 		rtw_txdescs_sync(sc->sc_dmat, sc->sc_desc_dmamap, htc,
   1495 		    stx->stx_first, ndesc,
   1496 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1497 
   1498 		if ((htc->htc_desc[stx->stx_last].htx_stat &
   1499 		    htole32(RTW_TXSTAT_OWN)) != 0)
   1500 			break;
   1501 
   1502 		rtw_collect_txpkt(sc, htc, stx, ndesc);
   1503 		SIMPLEQ_REMOVE_HEAD(&stc->stc_dirtyq, stx_q);
   1504 		SIMPLEQ_INSERT_TAIL(&stc->stc_freeq, stx, stx_q);
   1505 		sc->sc_if.if_flags &= ~IFF_OACTIVE;
   1506 	}
   1507 	if (stx == NULL)
   1508 		stc->stc_tx_timer = 0;
   1509 }
   1510 
   1511 static void
   1512 rtw_intr_tx(struct rtw_softc *sc, u_int16_t isr)
   1513 {
   1514 	int pri;
   1515 	struct rtw_txctl_blk	*stc;
   1516 	struct rtw_txdesc_blk	*htc;
   1517 
   1518 	for (pri = 0; pri < RTW_NTXPRI; pri++) {
   1519 		stc = &sc->sc_txctl_blk[pri];
   1520 		htc = &sc->sc_txdesc_blk[pri];
   1521 
   1522 		rtw_collect_txring(sc, stc, htc);
   1523 
   1524 		if ((isr & RTW_INTR_TX) != 0)
   1525 			rtw_start(&sc->sc_if);
   1526 	}
   1527 
   1528 	/* TBD */
   1529 	return;
   1530 }
   1531 
   1532 static void
   1533 rtw_intr_beacon(struct rtw_softc *sc, u_int16_t isr)
   1534 {
   1535 	/* TBD */
   1536 	return;
   1537 }
   1538 
   1539 static void
   1540 rtw_intr_atim(struct rtw_softc *sc)
   1541 {
   1542 	/* TBD */
   1543 	return;
   1544 }
   1545 
   1546 #ifdef RTW_DEBUG
   1547 static void
   1548 rtw_dump_rings(struct rtw_softc *sc)
   1549 {
   1550 	struct rtw_txdesc_blk *htc;
   1551 	struct rtw_rxdesc *hrx;
   1552 	int desc, pri;
   1553 
   1554 	if ((rtw_debug & RTW_DEBUG_IO_KICK) == 0)
   1555 		return;
   1556 
   1557 	for (pri = 0; pri < RTW_NTXPRI; pri++) {
   1558 		htc = &sc->sc_txdesc_blk[pri];
   1559 		printf("%s: txpri %d ndesc %d nfree %d\n", __func__, pri,
   1560 		    htc->htc_ndesc, htc->htc_nfree);
   1561 		for (desc = 0; desc < htc->htc_ndesc; desc++)
   1562 			rtw_print_txdesc(sc, ".", NULL, htc, desc);
   1563 	}
   1564 
   1565 	for (desc = 0; desc < RTW_RXQLEN; desc++) {
   1566 		hrx = &sc->sc_rxdesc[desc];
   1567 		printf("%s: ctl %08x rsvd0/rssi %08x buf/tsftl %08x "
   1568 		    "rsvd1/tsfth %08x\n", __func__,
   1569 		    le32toh(hrx->hrx_ctl), le32toh(hrx->hrx_rssi),
   1570 		    le32toh(hrx->hrx_buf), le32toh(hrx->hrx_tsfth));
   1571 	}
   1572 }
   1573 #endif /* RTW_DEBUG */
   1574 
   1575 static void
   1576 rtw_hwring_setup(struct rtw_softc *sc)
   1577 {
   1578 	struct rtw_regs *regs = &sc->sc_regs;
   1579 	RTW_WRITE(regs, RTW_RDSAR, RTW_RING_BASE(sc, hd_rx));
   1580 	RTW_WRITE(regs, RTW_TLPDA, RTW_RING_BASE(sc, hd_txlo));
   1581 	RTW_WRITE(regs, RTW_TNPDA, RTW_RING_BASE(sc, hd_txmd));
   1582 	RTW_WRITE(regs, RTW_THPDA, RTW_RING_BASE(sc, hd_txhi));
   1583 	RTW_WRITE(regs, RTW_TBDA, RTW_RING_BASE(sc, hd_bcn));
   1584 	RTW_SYNC(regs, RTW_TLPDA, RTW_RDSAR);
   1585 	RTW_DPRINTF(RTW_DEBUG_XMIT_DESC,
   1586 	    ("%s: reg[TLPDA] <- %" PRIxPTR "\n", __func__,
   1587 	     (uintptr_t)RTW_RING_BASE(sc, hd_txlo)));
   1588 	RTW_DPRINTF(RTW_DEBUG_XMIT_DESC,
   1589 	    ("%s: reg[TNPDA] <- %" PRIxPTR "\n", __func__,
   1590 	     (uintptr_t)RTW_RING_BASE(sc, hd_txmd)));
   1591 	RTW_DPRINTF(RTW_DEBUG_XMIT_DESC,
   1592 	    ("%s: reg[THPDA] <- %" PRIxPTR "\n", __func__,
   1593 	     (uintptr_t)RTW_RING_BASE(sc, hd_txhi)));
   1594 	RTW_DPRINTF(RTW_DEBUG_XMIT_DESC,
   1595 	    ("%s: reg[TBDA] <- %" PRIxPTR "\n", __func__,
   1596 	     (uintptr_t)RTW_RING_BASE(sc, hd_bcn)));
   1597 	RTW_DPRINTF(RTW_DEBUG_RECV_DESC,
   1598 	    ("%s: reg[RDSAR] <- %" PRIxPTR "\n", __func__,
   1599 	     (uintptr_t)RTW_RING_BASE(sc, hd_rx)));
   1600 }
   1601 
   1602 static void
   1603 rtw_swring_setup(struct rtw_softc *sc)
   1604 {
   1605 	rtw_txdesc_blk_init_all(&sc->sc_txdesc_blk[0]);
   1606 
   1607 	rtw_txctl_blk_init_all(&sc->sc_txctl_blk[0]);
   1608 
   1609 	rtw_rxdescs_sync(sc->sc_dmat, sc->sc_desc_dmamap,
   1610 	    0, RTW_RXQLEN, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1611 	rtw_rxctl_init_all(sc->sc_dmat, sc->sc_rxctl, &sc->sc_rxnext,
   1612 	    sc->sc_dev.dv_xname);
   1613 	rtw_rxdesc_init_all(sc->sc_dmat, sc->sc_desc_dmamap,
   1614 	    sc->sc_rxdesc, sc->sc_rxctl, 1);
   1615 
   1616 	rtw_txdescs_sync_all(sc->sc_dmat, sc->sc_desc_dmamap,
   1617 	    &sc->sc_txdesc_blk[0]);
   1618 #if 0	/* redundant with rtw_rxdesc_init_all */
   1619 	rtw_rxdescs_sync(sc->sc_dmat, sc->sc_desc_dmamap,
   1620 	    0, RTW_RXQLEN, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1621 #endif
   1622 }
   1623 
   1624 static void
   1625 rtw_txdesc_blk_reset(struct rtw_txdesc_blk *htc)
   1626 {
   1627 	int i;
   1628 
   1629 	(void)memset(htc->htc_desc, 0,
   1630 	    sizeof(htc->htc_desc[0]) * htc->htc_ndesc);
   1631 	for (i = 0; i < htc->htc_ndesc; i++)
   1632 		htc->htc_desc[i].htx_next = htole32(RTW_NEXT_DESC(htc, i));
   1633 	htc->htc_nfree = htc->htc_ndesc;
   1634 	htc->htc_next = 0;
   1635 }
   1636 
   1637 static void
   1638 rtw_txdescs_reset(struct rtw_softc *sc)
   1639 {
   1640 	int pri;
   1641 	struct rtw_txdesc_blk *htc;
   1642 
   1643 	for (pri = 0; pri < RTW_NTXPRI; pri++) {
   1644 		htc = &sc->sc_txdesc_blk[pri];
   1645 		rtw_txbufs_release(sc->sc_dmat, sc->sc_desc_dmamap, &sc->sc_ic,
   1646 		    &sc->sc_txctl_blk[pri]);
   1647 		rtw_txdesc_blk_reset(htc);
   1648 		rtw_txdescs_sync(sc->sc_dmat, sc->sc_desc_dmamap, htc,
   1649 		    0, htc->htc_ndesc,
   1650 		    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
   1651 	}
   1652 }
   1653 
   1654 static void
   1655 rtw_rxdescs_reset(struct rtw_softc *sc)
   1656 {
   1657 	/* Re-initialize descriptors, just in case. */
   1658 	rtw_rxdesc_init_all(sc->sc_dmat, sc->sc_desc_dmamap, sc->sc_rxdesc,
   1659 	    &sc->sc_rxctl[0], 1);
   1660 
   1661 	/* Reset to start of ring. */
   1662 	sc->sc_rxnext = 0;
   1663 }
   1664 
   1665 static void
   1666 rtw_intr_ioerror(struct rtw_softc *sc, uint16_t isr)
   1667 {
   1668 	struct rtw_regs *regs = &sc->sc_regs;
   1669 
   1670 	if ((isr & RTW_INTR_TXFOVW) != 0)
   1671 		printf("%s: tx fifo overflow\n", sc->sc_dev.dv_xname);
   1672 
   1673 	if ((isr & (RTW_INTR_RDU|RTW_INTR_RXFOVW)) == 0)
   1674 		return;
   1675 
   1676 	RTW_DPRINTF(RTW_DEBUG_BUGS, ("%s: restarting xmit/recv\n",
   1677 	    sc->sc_dev.dv_xname));
   1678 
   1679 #ifdef RTW_DEBUG
   1680 	rtw_dump_rings(sc);
   1681 #endif /* RTW_DEBUG */
   1682 
   1683 	rtw_io_enable(regs, RTW_CR_RE | RTW_CR_TE, 0);
   1684 
   1685 	/* Collect rx'd packets.  Refresh rx buffers. */
   1686 	rtw_intr_rx(sc, 0);
   1687 	/* Collect tx'd packets. */
   1688 	rtw_intr_tx(sc, 0);
   1689 
   1690 	RTW_WRITE16(regs, RTW_IMR, 0);
   1691 	RTW_SYNC(regs, RTW_IMR, RTW_IMR);
   1692 
   1693 	rtw_chip_reset1(regs, sc->sc_dev.dv_xname);
   1694 
   1695 	rtw_rxdescs_reset(sc);
   1696 	rtw_txdescs_reset(sc);
   1697 
   1698 	rtw_hwring_setup(sc);
   1699 
   1700 #ifdef RTW_DEBUG
   1701 	rtw_dump_rings(sc);
   1702 #endif /* RTW_DEBUG */
   1703 
   1704 	RTW_WRITE16(regs, RTW_IMR, sc->sc_inten);
   1705 	RTW_SYNC(regs, RTW_IMR, RTW_IMR);
   1706 	rtw_io_enable(regs, RTW_CR_RE | RTW_CR_TE, 1);
   1707 }
   1708 
   1709 static __inline void
   1710 rtw_suspend_ticks(struct rtw_softc *sc)
   1711 {
   1712 	RTW_DPRINTF(RTW_DEBUG_TIMEOUT,
   1713 	    ("%s: suspending ticks\n", sc->sc_dev.dv_xname));
   1714 	sc->sc_do_tick = 0;
   1715 }
   1716 
   1717 static __inline void
   1718 rtw_resume_ticks(struct rtw_softc *sc)
   1719 {
   1720 	u_int32_t tsftrl0, tsftrl1, next_tick;
   1721 
   1722 	tsftrl0 = RTW_READ(&sc->sc_regs, RTW_TSFTRL);
   1723 
   1724 	tsftrl1 = RTW_READ(&sc->sc_regs, RTW_TSFTRL);
   1725 	next_tick = tsftrl1 + 1000000;
   1726 	RTW_WRITE(&sc->sc_regs, RTW_TINT, next_tick);
   1727 
   1728 	sc->sc_do_tick = 1;
   1729 
   1730 	RTW_DPRINTF(RTW_DEBUG_TIMEOUT,
   1731 	    ("%s: resume ticks delta %#08x now %#08x next %#08x\n",
   1732 	    sc->sc_dev.dv_xname, tsftrl1 - tsftrl0, tsftrl1, next_tick));
   1733 }
   1734 
   1735 static void
   1736 rtw_intr_timeout(struct rtw_softc *sc)
   1737 {
   1738 	RTW_DPRINTF(RTW_DEBUG_TIMEOUT, ("%s: timeout\n", sc->sc_dev.dv_xname));
   1739 	if (sc->sc_do_tick)
   1740 		rtw_resume_ticks(sc);
   1741 	return;
   1742 }
   1743 
   1744 int
   1745 rtw_intr(void *arg)
   1746 {
   1747 	int i;
   1748 	struct rtw_softc *sc = arg;
   1749 	struct rtw_regs *regs = &sc->sc_regs;
   1750 	u_int16_t isr;
   1751 
   1752 	/*
   1753 	 * If the interface isn't running, the interrupt couldn't
   1754 	 * possibly have come from us.
   1755 	 */
   1756 	if ((sc->sc_flags & RTW_F_ENABLED) == 0 ||
   1757 	    (sc->sc_if.if_flags & IFF_RUNNING) == 0 ||
   1758 	    (sc->sc_dev.dv_flags & DVF_ACTIVE) == 0) {
   1759 		RTW_DPRINTF(RTW_DEBUG_INTR, ("%s: stray interrupt\n", sc->sc_dev.dv_xname));
   1760 		return (0);
   1761 	}
   1762 
   1763 	for (i = 0; i < 10; i++) {
   1764 		isr = RTW_READ16(regs, RTW_ISR);
   1765 
   1766 		RTW_WRITE16(regs, RTW_ISR, isr);
   1767 		RTW_WBR(regs, RTW_ISR, RTW_ISR);
   1768 
   1769 		if (sc->sc_intr_ack != NULL)
   1770 			(*sc->sc_intr_ack)(regs);
   1771 
   1772 		if (isr == 0)
   1773 			break;
   1774 
   1775 #ifdef RTW_DEBUG
   1776 #define PRINTINTR(flag) do { \
   1777 	if ((isr & flag) != 0) { \
   1778 		printf("%s" #flag, delim); \
   1779 		delim = ","; \
   1780 	} \
   1781 } while (0)
   1782 
   1783 		if ((rtw_debug & RTW_DEBUG_INTR) != 0 && isr != 0) {
   1784 			const char *delim = "<";
   1785 
   1786 			printf("%s: reg[ISR] = %x", sc->sc_dev.dv_xname, isr);
   1787 
   1788 			PRINTINTR(RTW_INTR_TXFOVW);
   1789 			PRINTINTR(RTW_INTR_TIMEOUT);
   1790 			PRINTINTR(RTW_INTR_BCNINT);
   1791 			PRINTINTR(RTW_INTR_ATIMINT);
   1792 			PRINTINTR(RTW_INTR_TBDER);
   1793 			PRINTINTR(RTW_INTR_TBDOK);
   1794 			PRINTINTR(RTW_INTR_THPDER);
   1795 			PRINTINTR(RTW_INTR_THPDOK);
   1796 			PRINTINTR(RTW_INTR_TNPDER);
   1797 			PRINTINTR(RTW_INTR_TNPDOK);
   1798 			PRINTINTR(RTW_INTR_RXFOVW);
   1799 			PRINTINTR(RTW_INTR_RDU);
   1800 			PRINTINTR(RTW_INTR_TLPDER);
   1801 			PRINTINTR(RTW_INTR_TLPDOK);
   1802 			PRINTINTR(RTW_INTR_RER);
   1803 			PRINTINTR(RTW_INTR_ROK);
   1804 
   1805 			printf(">\n");
   1806 		}
   1807 #undef PRINTINTR
   1808 #endif /* RTW_DEBUG */
   1809 
   1810 		if ((isr & RTW_INTR_RX) != 0)
   1811 			rtw_intr_rx(sc, isr & RTW_INTR_RX);
   1812 		if ((isr & RTW_INTR_TX) != 0)
   1813 			rtw_intr_tx(sc, isr & RTW_INTR_TX);
   1814 		if ((isr & RTW_INTR_BEACON) != 0)
   1815 			rtw_intr_beacon(sc, isr & RTW_INTR_BEACON);
   1816 		if ((isr & RTW_INTR_ATIMINT) != 0)
   1817 			rtw_intr_atim(sc);
   1818 		if ((isr & RTW_INTR_IOERROR) != 0)
   1819 			rtw_intr_ioerror(sc, isr & RTW_INTR_IOERROR);
   1820 		if ((isr & RTW_INTR_TIMEOUT) != 0)
   1821 			rtw_intr_timeout(sc);
   1822 	}
   1823 
   1824 	return 1;
   1825 }
   1826 
   1827 /* Must be called at splnet. */
   1828 static void
   1829 rtw_stop(struct ifnet *ifp, int disable)
   1830 {
   1831 	int pri;
   1832 	struct rtw_softc *sc = (struct rtw_softc *)ifp->if_softc;
   1833 	struct ieee80211com *ic = &sc->sc_ic;
   1834 	struct rtw_regs *regs = &sc->sc_regs;
   1835 
   1836 	if ((sc->sc_flags & RTW_F_ENABLED) == 0)
   1837 		return;
   1838 
   1839 	rtw_suspend_ticks(sc);
   1840 
   1841 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
   1842 
   1843 	if ((sc->sc_flags & RTW_F_INVALID) == 0) {
   1844 		/* Disable interrupts. */
   1845 		RTW_WRITE16(regs, RTW_IMR, 0);
   1846 
   1847 		RTW_WBW(regs, RTW_TPPOLL, RTW_IMR);
   1848 
   1849 		/* Stop the transmit and receive processes. First stop DMA,
   1850 		 * then disable receiver and transmitter.
   1851 		 */
   1852 		RTW_WRITE8(regs, RTW_TPPOLL,
   1853 		    RTW_TPPOLL_SBQ|RTW_TPPOLL_SHPQ|RTW_TPPOLL_SNPQ|
   1854 		    RTW_TPPOLL_SLPQ);
   1855 
   1856 		RTW_SYNC(regs, RTW_TPPOLL, RTW_IMR);
   1857 
   1858 		rtw_io_enable(&sc->sc_regs, RTW_CR_RE|RTW_CR_TE, 0);
   1859 	}
   1860 
   1861 	for (pri = 0; pri < RTW_NTXPRI; pri++) {
   1862 		rtw_txbufs_release(sc->sc_dmat, sc->sc_desc_dmamap, &sc->sc_ic,
   1863 		    &sc->sc_txctl_blk[pri]);
   1864 	}
   1865 
   1866 	if (disable) {
   1867 		rtw_disable(sc);
   1868 		rtw_rxbufs_release(sc->sc_dmat, &sc->sc_rxctl[0]);
   1869 	}
   1870 
   1871 	/* Mark the interface as not running.  Cancel the watchdog timer. */
   1872 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1873 	ifp->if_timer = 0;
   1874 
   1875 	return;
   1876 }
   1877 
   1878 const char *
   1879 rtw_pwrstate_string(enum rtw_pwrstate power)
   1880 {
   1881 	switch (power) {
   1882 	case RTW_ON:
   1883 		return "on";
   1884 	case RTW_SLEEP:
   1885 		return "sleep";
   1886 	case RTW_OFF:
   1887 		return "off";
   1888 	default:
   1889 		return "unknown";
   1890 	}
   1891 }
   1892 
   1893 /* XXX For Maxim, I am using the RFMD settings gleaned from the
   1894  * reference driver, plus a magic Maxim "ON" value that comes from
   1895  * the Realtek document "Windows PG for Rtl8180."
   1896  */
   1897 static void
   1898 rtw_maxim_pwrstate(struct rtw_regs *regs, enum rtw_pwrstate power,
   1899     int before_rf, int digphy)
   1900 {
   1901 	u_int32_t anaparm;
   1902 
   1903 	anaparm = RTW_READ(regs, RTW_ANAPARM);
   1904 	anaparm &= ~(RTW_ANAPARM_RFPOW_MASK | RTW_ANAPARM_TXDACOFF);
   1905 
   1906 	switch (power) {
   1907 	case RTW_OFF:
   1908 		if (before_rf)
   1909 			return;
   1910 		anaparm |= RTW_ANAPARM_RFPOW_MAXIM_OFF;
   1911 		anaparm |= RTW_ANAPARM_TXDACOFF;
   1912 		break;
   1913 	case RTW_SLEEP:
   1914 		if (!before_rf)
   1915 			return;
   1916 		anaparm |= RTW_ANAPARM_RFPOW_MAXIM_SLEEP;
   1917 		anaparm |= RTW_ANAPARM_TXDACOFF;
   1918 		break;
   1919 	case RTW_ON:
   1920 		if (!before_rf)
   1921 			return;
   1922 		anaparm |= RTW_ANAPARM_RFPOW_MAXIM_ON;
   1923 		break;
   1924 	}
   1925 	RTW_DPRINTF(RTW_DEBUG_PWR,
   1926 	    ("%s: power state %s, %s RF, reg[ANAPARM] <- %08x\n",
   1927 	    __func__, rtw_pwrstate_string(power),
   1928 	    (before_rf) ? "before" : "after", anaparm));
   1929 
   1930 	RTW_WRITE(regs, RTW_ANAPARM, anaparm);
   1931 	RTW_SYNC(regs, RTW_ANAPARM, RTW_ANAPARM);
   1932 }
   1933 
   1934 /* XXX I am using the RFMD settings gleaned from the reference
   1935  * driver.  They agree
   1936  */
   1937 static void
   1938 rtw_rfmd_pwrstate(struct rtw_regs *regs, enum rtw_pwrstate power,
   1939     int before_rf, int digphy)
   1940 {
   1941 	u_int32_t anaparm;
   1942 
   1943 	anaparm = RTW_READ(regs, RTW_ANAPARM);
   1944 	anaparm &= ~(RTW_ANAPARM_RFPOW_MASK | RTW_ANAPARM_TXDACOFF);
   1945 
   1946 	switch (power) {
   1947 	case RTW_OFF:
   1948 		if (before_rf)
   1949 			return;
   1950 		anaparm |= RTW_ANAPARM_RFPOW_RFMD_OFF;
   1951 		anaparm |= RTW_ANAPARM_TXDACOFF;
   1952 		break;
   1953 	case RTW_SLEEP:
   1954 		if (!before_rf)
   1955 			return;
   1956 		anaparm |= RTW_ANAPARM_RFPOW_RFMD_SLEEP;
   1957 		anaparm |= RTW_ANAPARM_TXDACOFF;
   1958 		break;
   1959 	case RTW_ON:
   1960 		if (!before_rf)
   1961 			return;
   1962 		anaparm |= RTW_ANAPARM_RFPOW_RFMD_ON;
   1963 		break;
   1964 	}
   1965 	RTW_DPRINTF(RTW_DEBUG_PWR,
   1966 	    ("%s: power state %s, %s RF, reg[ANAPARM] <- %08x\n",
   1967 	    __func__, rtw_pwrstate_string(power),
   1968 	    (before_rf) ? "before" : "after", anaparm));
   1969 
   1970 	RTW_WRITE(regs, RTW_ANAPARM, anaparm);
   1971 	RTW_SYNC(regs, RTW_ANAPARM, RTW_ANAPARM);
   1972 }
   1973 
   1974 static void
   1975 rtw_philips_pwrstate(struct rtw_regs *regs, enum rtw_pwrstate power,
   1976     int before_rf, int digphy)
   1977 {
   1978 	u_int32_t anaparm;
   1979 
   1980 	anaparm = RTW_READ(regs, RTW_ANAPARM);
   1981 	anaparm &= ~(RTW_ANAPARM_RFPOW_MASK | RTW_ANAPARM_TXDACOFF);
   1982 
   1983 	switch (power) {
   1984 	case RTW_OFF:
   1985 		if (before_rf)
   1986 			return;
   1987 		anaparm |= RTW_ANAPARM_RFPOW_PHILIPS_OFF;
   1988 		anaparm |= RTW_ANAPARM_TXDACOFF;
   1989 		break;
   1990 	case RTW_SLEEP:
   1991 		if (!before_rf)
   1992 			return;
   1993 		anaparm |= RTW_ANAPARM_RFPOW_PHILIPS_SLEEP;
   1994 		anaparm |= RTW_ANAPARM_TXDACOFF;
   1995 		break;
   1996 	case RTW_ON:
   1997 		if (!before_rf)
   1998 			return;
   1999 		if (digphy) {
   2000 			anaparm |= RTW_ANAPARM_RFPOW_DIG_PHILIPS_ON;
   2001 			/* XXX guess */
   2002 			anaparm |= RTW_ANAPARM_TXDACOFF;
   2003 		} else
   2004 			anaparm |= RTW_ANAPARM_RFPOW_ANA_PHILIPS_ON;
   2005 		break;
   2006 	}
   2007 	RTW_DPRINTF(RTW_DEBUG_PWR,
   2008 	    ("%s: power state %s, %s RF, reg[ANAPARM] <- %08x\n",
   2009 	    __func__, rtw_pwrstate_string(power),
   2010 	    (before_rf) ? "before" : "after", anaparm));
   2011 
   2012 	RTW_WRITE(regs, RTW_ANAPARM, anaparm);
   2013 	RTW_SYNC(regs, RTW_ANAPARM, RTW_ANAPARM);
   2014 }
   2015 
   2016 static void
   2017 rtw_pwrstate0(struct rtw_softc *sc, enum rtw_pwrstate power, int before_rf,
   2018     int digphy)
   2019 {
   2020 	struct rtw_regs *regs = &sc->sc_regs;
   2021 
   2022 	rtw_set_access(sc, RTW_ACCESS_ANAPARM);
   2023 
   2024 	(*sc->sc_pwrstate_cb)(regs, power, before_rf, digphy);
   2025 
   2026 	rtw_set_access(sc, RTW_ACCESS_NONE);
   2027 
   2028 	return;
   2029 }
   2030 
   2031 static int
   2032 rtw_pwrstate(struct rtw_softc *sc, enum rtw_pwrstate power)
   2033 {
   2034 	int rc;
   2035 
   2036 	RTW_DPRINTF(RTW_DEBUG_PWR,
   2037 	    ("%s: %s->%s\n", __func__,
   2038 	    rtw_pwrstate_string(sc->sc_pwrstate), rtw_pwrstate_string(power)));
   2039 
   2040 	if (sc->sc_pwrstate == power)
   2041 		return 0;
   2042 
   2043 	rtw_pwrstate0(sc, power, 1, sc->sc_flags & RTW_F_DIGPHY);
   2044 	rc = rtw_rf_pwrstate(sc->sc_rf, power);
   2045 	rtw_pwrstate0(sc, power, 0, sc->sc_flags & RTW_F_DIGPHY);
   2046 
   2047 	switch (power) {
   2048 	case RTW_ON:
   2049 		/* TBD set LEDs */
   2050 		break;
   2051 	case RTW_SLEEP:
   2052 		/* TBD */
   2053 		break;
   2054 	case RTW_OFF:
   2055 		/* TBD */
   2056 		break;
   2057 	}
   2058 	if (rc == 0)
   2059 		sc->sc_pwrstate = power;
   2060 	else
   2061 		sc->sc_pwrstate = RTW_OFF;
   2062 	return rc;
   2063 }
   2064 
   2065 static int
   2066 rtw_tune(struct rtw_softc *sc)
   2067 {
   2068 	struct ieee80211com *ic = &sc->sc_ic;
   2069 	u_int chan;
   2070 	int rc;
   2071 	int antdiv = sc->sc_flags & RTW_F_ANTDIV,
   2072 	    dflantb = sc->sc_flags & RTW_F_DFLANTB;
   2073 
   2074 	KASSERT(ic->ic_bss->ni_chan != NULL);
   2075 
   2076 	chan = ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan);
   2077 	if (chan == IEEE80211_CHAN_ANY)
   2078 		panic("%s: chan == IEEE80211_CHAN_ANY\n", __func__);
   2079 
   2080 	if (chan == sc->sc_cur_chan) {
   2081 		RTW_DPRINTF(RTW_DEBUG_TUNE,
   2082 		    ("%s: already tuned chan #%d\n", __func__, chan));
   2083 		return 0;
   2084 	}
   2085 
   2086 	rtw_suspend_ticks(sc);
   2087 
   2088 	rtw_io_enable(&sc->sc_regs, RTW_CR_RE | RTW_CR_TE, 0);
   2089 
   2090 	/* TBD wait for Tx to complete */
   2091 
   2092 	KASSERT((sc->sc_flags & RTW_F_ENABLED) != 0);
   2093 
   2094 	if ((rc = rtw_phy_init(&sc->sc_regs, sc->sc_rf,
   2095 	    rtw_chan2txpower(&sc->sc_srom, ic, ic->ic_bss->ni_chan),
   2096 	    sc->sc_csthr, ic->ic_bss->ni_chan->ic_freq, antdiv,
   2097 	    dflantb, RTW_ON)) != 0) {
   2098 		/* XXX condition on powersaving */
   2099 		printf("%s: phy init failed\n", sc->sc_dev.dv_xname);
   2100 	}
   2101 
   2102 	sc->sc_cur_chan = chan;
   2103 
   2104 	rtw_io_enable(&sc->sc_regs, RTW_CR_RE | RTW_CR_TE, 1);
   2105 
   2106 	rtw_resume_ticks(sc);
   2107 
   2108 	return rc;
   2109 }
   2110 
   2111 void
   2112 rtw_disable(struct rtw_softc *sc)
   2113 {
   2114 	int rc;
   2115 
   2116 	if ((sc->sc_flags & RTW_F_ENABLED) == 0)
   2117 		return;
   2118 
   2119 	/* turn off PHY */
   2120 	if ((rc = rtw_pwrstate(sc, RTW_OFF)) != 0)
   2121 		printf("%s: failed to turn off PHY (%d)\n",
   2122 		    sc->sc_dev.dv_xname, rc);
   2123 
   2124 	if (sc->sc_disable != NULL)
   2125 		(*sc->sc_disable)(sc);
   2126 
   2127 	sc->sc_flags &= ~RTW_F_ENABLED;
   2128 }
   2129 
   2130 int
   2131 rtw_enable(struct rtw_softc *sc)
   2132 {
   2133 	if ((sc->sc_flags & RTW_F_ENABLED) == 0) {
   2134 		if (sc->sc_enable != NULL && (*sc->sc_enable)(sc) != 0) {
   2135 			printf("%s: device enable failed\n",
   2136 			    sc->sc_dev.dv_xname);
   2137 			return (EIO);
   2138 		}
   2139 		sc->sc_flags |= RTW_F_ENABLED;
   2140 	}
   2141 	return (0);
   2142 }
   2143 
   2144 static void
   2145 rtw_transmit_config(struct rtw_regs *regs)
   2146 {
   2147 	u_int32_t tcr;
   2148 
   2149 	tcr = RTW_READ(regs, RTW_TCR);
   2150 
   2151 	tcr |= RTW_TCR_CWMIN;
   2152 	tcr &= ~RTW_TCR_MXDMA_MASK;
   2153 	tcr |= RTW_TCR_MXDMA_256;
   2154 	tcr |= RTW_TCR_SAT;		/* send ACK as fast as possible */
   2155 	tcr &= ~RTW_TCR_LBK_MASK;
   2156 	tcr |= RTW_TCR_LBK_NORMAL;	/* normal operating mode */
   2157 
   2158 	/* set short/long retry limits */
   2159 	tcr &= ~(RTW_TCR_SRL_MASK|RTW_TCR_LRL_MASK);
   2160 	tcr |= LSHIFT(4, RTW_TCR_SRL_MASK) | LSHIFT(4, RTW_TCR_LRL_MASK);
   2161 
   2162 	tcr &= ~RTW_TCR_CRC;	/* NIC appends CRC32 */
   2163 
   2164 	RTW_WRITE(regs, RTW_TCR, tcr);
   2165 	RTW_SYNC(regs, RTW_TCR, RTW_TCR);
   2166 }
   2167 
   2168 static __inline void
   2169 rtw_enable_interrupts(struct rtw_softc *sc)
   2170 {
   2171 	struct rtw_regs *regs = &sc->sc_regs;
   2172 
   2173 	sc->sc_inten = RTW_INTR_RX|RTW_INTR_TX|RTW_INTR_BEACON|RTW_INTR_ATIMINT;
   2174 	sc->sc_inten |= RTW_INTR_IOERROR|RTW_INTR_TIMEOUT;
   2175 
   2176 	RTW_WRITE16(regs, RTW_IMR, sc->sc_inten);
   2177 	RTW_WBW(regs, RTW_IMR, RTW_ISR);
   2178 	RTW_WRITE16(regs, RTW_ISR, 0xffff);
   2179 	RTW_SYNC(regs, RTW_IMR, RTW_ISR);
   2180 
   2181 	/* XXX necessary? */
   2182 	if (sc->sc_intr_ack != NULL)
   2183 		(*sc->sc_intr_ack)(regs);
   2184 }
   2185 
   2186 static void
   2187 rtw_set_nettype(struct rtw_softc *sc, enum ieee80211_opmode opmode)
   2188 {
   2189 	uint8_t msr;
   2190 
   2191 	/* I'm guessing that MSR is protected as CONFIG[0123] are. */
   2192 	rtw_set_access(sc, RTW_ACCESS_CONFIG);
   2193 
   2194 	msr = RTW_READ8(&sc->sc_regs, RTW_MSR) & ~RTW_MSR_NETYPE_MASK;
   2195 
   2196 	switch (opmode) {
   2197 	case IEEE80211_M_AHDEMO:
   2198 	case IEEE80211_M_IBSS:
   2199 		msr |= RTW_MSR_NETYPE_ADHOC_OK;
   2200 		break;
   2201 	case IEEE80211_M_HOSTAP:
   2202 		msr |= RTW_MSR_NETYPE_AP_OK;
   2203 		break;
   2204 	case IEEE80211_M_MONITOR:
   2205 		/* XXX */
   2206 		msr |= RTW_MSR_NETYPE_NOLINK;
   2207 		break;
   2208 	case IEEE80211_M_STA:
   2209 		msr |= RTW_MSR_NETYPE_INFRA_OK;
   2210 		break;
   2211 	}
   2212 	RTW_WRITE8(&sc->sc_regs, RTW_MSR, msr);
   2213 
   2214 	rtw_set_access(sc, RTW_ACCESS_NONE);
   2215 }
   2216 
   2217 /* XXX is the endianness correct? test. */
   2218 #define	rtw_calchash(addr) \
   2219 	(ether_crc32_le((addr), IEEE80211_ADDR_LEN) & BITS(5, 0))
   2220 
   2221 static void
   2222 rtw_pktfilt_load(struct rtw_softc *sc)
   2223 {
   2224 	struct rtw_regs *regs = &sc->sc_regs;
   2225 	struct ieee80211com *ic = &sc->sc_ic;
   2226 	struct ethercom *ec = &ic->ic_ec;
   2227 	struct ifnet *ifp = &sc->sc_ic.ic_if;
   2228 	int hash;
   2229 	u_int32_t hashes[2] = { 0, 0 };
   2230 	struct ether_multi *enm;
   2231 	struct ether_multistep step;
   2232 
   2233 	/* XXX might be necessary to stop Rx/Tx engines while setting filters */
   2234 
   2235 #define RTW_RCR_MONITOR (RTW_RCR_ACRC32|RTW_RCR_APM|RTW_RCR_AAP|RTW_RCR_AB|RTW_RCR_ACF | RTW_RCR_AICV | RTW_RCR_ACRC32)
   2236 
   2237 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
   2238 		sc->sc_rcr |= RTW_RCR_MONITOR;
   2239 	else
   2240 		sc->sc_rcr &= ~RTW_RCR_MONITOR;
   2241 
   2242 	/* XXX reference sources BEGIN */
   2243 	sc->sc_rcr |= RTW_RCR_ENMARP;
   2244 	sc->sc_rcr |= RTW_RCR_AB | RTW_RCR_AM | RTW_RCR_APM;
   2245 #if 0
   2246 	/* receive broadcasts in our BSS */
   2247 	sc->sc_rcr |= RTW_RCR_ADD3;
   2248 #endif
   2249 	/* XXX reference sources END */
   2250 
   2251 	/* receive pwrmgmt frames. */
   2252 	sc->sc_rcr |= RTW_RCR_APWRMGT;
   2253 	/* receive mgmt/ctrl/data frames. */
   2254 	sc->sc_rcr |= RTW_RCR_ADF | RTW_RCR_AMF;
   2255 	/* initialize Rx DMA threshold, Tx DMA burst size */
   2256 	sc->sc_rcr |= RTW_RCR_RXFTH_WHOLE | RTW_RCR_MXDMA_1024;
   2257 
   2258 	ifp->if_flags &= ~IFF_ALLMULTI;
   2259 
   2260 	if (ifp->if_flags & IFF_PROMISC) {
   2261 		sc->sc_rcr |= RTW_RCR_AB;	/* accept all broadcast */
   2262 allmulti:
   2263 		ifp->if_flags |= IFF_ALLMULTI;
   2264 		goto setit;
   2265 	}
   2266 
   2267 	/*
   2268 	 * Program the 64-bit multicast hash filter.
   2269 	 */
   2270 	ETHER_FIRST_MULTI(step, ec, enm);
   2271 	while (enm != NULL) {
   2272 		/* XXX */
   2273 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
   2274 		    ETHER_ADDR_LEN) != 0)
   2275 			goto allmulti;
   2276 
   2277 		hash = rtw_calchash(enm->enm_addrlo);
   2278 		hashes[hash >> 5] |= 1 << (hash & 0x1f);
   2279 		ETHER_NEXT_MULTI(step, enm);
   2280 	}
   2281 
   2282 	if (ifp->if_flags & IFF_BROADCAST) {
   2283 		hash = rtw_calchash(etherbroadcastaddr);
   2284 		hashes[hash >> 5] |= 1 << (hash & 0x1f);
   2285 	}
   2286 
   2287 	/* all bits set => hash is useless */
   2288 	if (~(hashes[0] & hashes[1]) == 0)
   2289 		goto allmulti;
   2290 
   2291  setit:
   2292 	if (ifp->if_flags & IFF_ALLMULTI)
   2293 		sc->sc_rcr |= RTW_RCR_AM;	/* accept all multicast */
   2294 
   2295 	if (ic->ic_state == IEEE80211_S_SCAN)
   2296 		sc->sc_rcr |= RTW_RCR_AB;	/* accept all broadcast */
   2297 
   2298 	hashes[0] = hashes[1] = 0xffffffff;
   2299 
   2300 	RTW_WRITE(regs, RTW_MAR0, hashes[0]);
   2301 	RTW_WRITE(regs, RTW_MAR1, hashes[1]);
   2302 	RTW_WRITE(regs, RTW_RCR, sc->sc_rcr);
   2303 	RTW_SYNC(regs, RTW_MAR0, RTW_RCR); /* RTW_MAR0 < RTW_MAR1 < RTW_RCR */
   2304 
   2305 	DPRINTF(sc, RTW_DEBUG_PKTFILT,
   2306 	    ("%s: RTW_MAR0 %08x RTW_MAR1 %08x RTW_RCR %08x\n",
   2307 	    sc->sc_dev.dv_xname, RTW_READ(regs, RTW_MAR0),
   2308 	    RTW_READ(regs, RTW_MAR1), RTW_READ(regs, RTW_RCR)));
   2309 
   2310 	return;
   2311 }
   2312 
   2313 /* Must be called at splnet. */
   2314 static int
   2315 rtw_init(struct ifnet *ifp)
   2316 {
   2317 	struct rtw_softc *sc = (struct rtw_softc *)ifp->if_softc;
   2318 	struct ieee80211com *ic = &sc->sc_ic;
   2319 	struct rtw_regs *regs = &sc->sc_regs;
   2320 	int rc = 0;
   2321 
   2322 	if ((rc = rtw_enable(sc)) != 0)
   2323 		goto out;
   2324 
   2325 	/* Cancel pending I/O and reset. */
   2326 	rtw_stop(ifp, 0);
   2327 
   2328 	ic->ic_bss->ni_chan = ic->ic_ibss_chan;
   2329 	DPRINTF(sc, RTW_DEBUG_TUNE, ("%s: channel %d freq %d flags 0x%04x\n",
   2330 	    __func__, ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan),
   2331 	    ic->ic_bss->ni_chan->ic_freq, ic->ic_bss->ni_chan->ic_flags));
   2332 
   2333 	if ((rc = rtw_pwrstate(sc, RTW_OFF)) != 0)
   2334 		goto out;
   2335 
   2336 	rtw_swring_setup(sc);
   2337 
   2338 	rtw_transmit_config(regs);
   2339 
   2340 	rtw_set_access(sc, RTW_ACCESS_CONFIG);
   2341 
   2342 	RTW_WRITE8(regs, RTW_MSR, 0x0);	/* no link */
   2343 	RTW_WBW(regs, RTW_MSR, RTW_BRSR);
   2344 
   2345 	/* long PLCP header, 1Mb/2Mb basic rate */
   2346 	RTW_WRITE16(regs, RTW_BRSR, RTW_BRSR_MBR8180_2MBPS);
   2347 	RTW_SYNC(regs, RTW_BRSR, RTW_BRSR);
   2348 
   2349 	rtw_set_access(sc, RTW_ACCESS_ANAPARM);
   2350 	rtw_set_access(sc, RTW_ACCESS_NONE);
   2351 
   2352 #if 0
   2353 	RTW_WRITE(regs, RTW_FEMR, RTW_FEMR_GWAKE|RTW_FEMR_WKUP|RTW_FEMR_INTR);
   2354 #endif
   2355 	/* XXX from reference sources */
   2356 	RTW_WRITE(regs, RTW_FEMR, 0xffff);
   2357 	RTW_SYNC(regs, RTW_FEMR, RTW_FEMR);
   2358 
   2359 	rtw_set_rfprog(regs, sc->sc_rfchipid, sc->sc_dev.dv_xname);
   2360 
   2361 	RTW_WRITE8(regs, RTW_PHYDELAY, sc->sc_phydelay);
   2362 	/* from Linux driver */
   2363 	RTW_WRITE8(regs, RTW_CRCOUNT, RTW_CRCOUNT_MAGIC);
   2364 
   2365 	RTW_SYNC(regs, RTW_PHYDELAY, RTW_CRCOUNT);
   2366 
   2367 	rtw_enable_interrupts(sc);
   2368 
   2369 	rtw_pktfilt_load(sc);
   2370 
   2371 	rtw_hwring_setup(sc);
   2372 
   2373 	rtw_io_enable(regs, RTW_CR_RE|RTW_CR_TE, 1);
   2374 
   2375 	ifp->if_flags |= IFF_RUNNING;
   2376 	ic->ic_state = IEEE80211_S_INIT;
   2377 
   2378 	RTW_WRITE16(regs, RTW_BSSID16, 0x0);
   2379 	RTW_WRITE(regs, RTW_BSSID32, 0x0);
   2380 
   2381 	rtw_resume_ticks(sc);
   2382 
   2383 	rtw_set_nettype(sc, IEEE80211_M_MONITOR);
   2384 
   2385 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
   2386 		return ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
   2387 	else
   2388 		return ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
   2389 
   2390 out:
   2391 	return rc;
   2392 }
   2393 
   2394 static int
   2395 rtw_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
   2396 {
   2397 	int rc = 0, s;
   2398 	struct rtw_softc *sc = ifp->if_softc;
   2399 	struct ifreq *ifr = (struct ifreq *)data;
   2400 
   2401 	s = splnet();
   2402 	switch (cmd) {
   2403 	case SIOCSIFFLAGS:
   2404 		if ((ifp->if_flags & IFF_UP) != 0) {
   2405 			if (0 && (sc->sc_flags & RTW_F_ENABLED) != 0) {
   2406 				rtw_pktfilt_load(sc);
   2407 			} else
   2408 				rc = rtw_init(ifp);
   2409 #ifdef RTW_DEBUG
   2410 			rtw_print_regs(&sc->sc_regs, ifp->if_xname, __func__);
   2411 #endif /* RTW_DEBUG */
   2412 		} else if ((sc->sc_flags & RTW_F_ENABLED) != 0) {
   2413 #ifdef RTW_DEBUG
   2414 			rtw_print_regs(&sc->sc_regs, ifp->if_xname, __func__);
   2415 #endif /* RTW_DEBUG */
   2416 			rtw_stop(ifp, 1);
   2417 		}
   2418 		break;
   2419 	case SIOCADDMULTI:
   2420 	case SIOCDELMULTI:
   2421 		if (cmd == SIOCADDMULTI)
   2422 			rc = ether_addmulti(ifr, &sc->sc_ic.ic_ec);
   2423 		else
   2424 			rc = ether_delmulti(ifr, &sc->sc_ic.ic_ec);
   2425 		if (rc == ENETRESET) {
   2426 			if (ifp->if_flags & IFF_RUNNING)
   2427 				rtw_pktfilt_load(sc);
   2428 			rc = 0;
   2429 		}
   2430 		break;
   2431 	default:
   2432 		if ((rc = ieee80211_ioctl(ifp, cmd, data)) == ENETRESET) {
   2433 			if ((sc->sc_flags & RTW_F_ENABLED) != 0)
   2434 				rc = rtw_init(ifp);
   2435 			else
   2436 				rc = 0;
   2437 		}
   2438 		break;
   2439 	}
   2440 	splx(s);
   2441 	return rc;
   2442 }
   2443 
   2444 /* Point *mp at the next 802.11 frame to transmit.  Point *stcp
   2445  * at the driver's selection of transmit control block for the packet.
   2446  */
   2447 static __inline int
   2448 rtw_dequeue(struct ifnet *ifp, struct rtw_txctl_blk **stcp,
   2449     struct rtw_txdesc_blk **htcp, struct mbuf **mp,
   2450     struct ieee80211_node **nip)
   2451 {
   2452 	struct rtw_txctl_blk *stc;
   2453 	struct rtw_txdesc_blk *htc;
   2454 	struct mbuf *m0;
   2455 	struct rtw_softc *sc;
   2456 	struct ieee80211com *ic;
   2457 
   2458 	sc = (struct rtw_softc *)ifp->if_softc;
   2459 
   2460 	DPRINTF(sc, RTW_DEBUG_XMIT,
   2461 	    ("%s: enter %s\n", sc->sc_dev.dv_xname, __func__));
   2462 	*mp = NULL;
   2463 
   2464 	stc = &sc->sc_txctl_blk[RTW_TXPRIMD];
   2465 	htc = &sc->sc_txdesc_blk[RTW_TXPRIMD];
   2466 
   2467 	if (SIMPLEQ_EMPTY(&stc->stc_freeq) || htc->htc_nfree == 0) {
   2468 		DPRINTF(sc, RTW_DEBUG_XMIT,
   2469 		    ("%s: out of descriptors\n", __func__));
   2470 		ifp->if_flags |= IFF_OACTIVE;
   2471 		return 0;
   2472 	}
   2473 
   2474 	ic = &sc->sc_ic;
   2475 
   2476 	if (!IF_IS_EMPTY(&ic->ic_mgtq)) {
   2477 		IF_DEQUEUE(&ic->ic_mgtq, m0);
   2478 		*nip = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
   2479 		m0->m_pkthdr.rcvif = NULL;
   2480 		DPRINTF(sc, RTW_DEBUG_XMIT,
   2481 		    ("%s: dequeue mgt frame\n", __func__));
   2482 	} else if (ic->ic_state != IEEE80211_S_RUN) {
   2483 		DPRINTF(sc, RTW_DEBUG_XMIT, ("%s: not running\n", __func__));
   2484 		return 0;
   2485 	} else if (!IF_IS_EMPTY(&ic->ic_pwrsaveq)) {
   2486 		IF_DEQUEUE(&ic->ic_pwrsaveq, m0);
   2487 		*nip = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
   2488 		m0->m_pkthdr.rcvif = NULL;
   2489 		DPRINTF(sc, RTW_DEBUG_XMIT,
   2490 		    ("%s: dequeue pwrsave frame\n", __func__));
   2491 	} else {
   2492 		IFQ_DEQUEUE(&ifp->if_snd, m0);
   2493 		if (m0 == NULL) {
   2494 			DPRINTF(sc, RTW_DEBUG_XMIT,
   2495 			    ("%s: no frame\n", __func__));
   2496 			return 0;
   2497 		}
   2498 		DPRINTF(sc, RTW_DEBUG_XMIT,
   2499 		    ("%s: dequeue data frame\n", __func__));
   2500 		ifp->if_opackets++;
   2501 #if NBPFILTER > 0
   2502 		if (ifp->if_bpf)
   2503 			bpf_mtap(ifp->if_bpf, m0);
   2504 #endif
   2505 		if ((m0 = ieee80211_encap(ifp, m0, nip)) == NULL) {
   2506 			DPRINTF(sc, RTW_DEBUG_XMIT,
   2507 			    ("%s: encap error\n", __func__));
   2508 			ifp->if_oerrors++;
   2509 			return -1;
   2510 		}
   2511 	}
   2512 	DPRINTF(sc, RTW_DEBUG_XMIT, ("%s: leave\n", __func__));
   2513 	*stcp = stc;
   2514 	*htcp = htc;
   2515 	*mp = m0;
   2516 	return 0;
   2517 }
   2518 
   2519 static int
   2520 rtw_seg_too_short(bus_dmamap_t dmamap)
   2521 {
   2522 	int i;
   2523 	for (i = 0; i < dmamap->dm_nsegs; i++) {
   2524 		if (dmamap->dm_segs[i].ds_len < 4) {
   2525 			printf("%s: segment too short\n", __func__);
   2526 			return 1;
   2527 		}
   2528 	}
   2529 	return 0;
   2530 }
   2531 
   2532 /* TBD factor with atw_start */
   2533 static struct mbuf *
   2534 rtw_dmamap_load_txbuf(bus_dma_tag_t dmat, bus_dmamap_t dmam, struct mbuf *chain,
   2535     u_int ndescfree, short *ifflagsp, const char *dvname)
   2536 {
   2537 	int first, rc;
   2538 	struct mbuf *m, *m0;
   2539 
   2540 	m0 = chain;
   2541 
   2542 	/*
   2543 	 * Load the DMA map.  Copy and try (once) again if the packet
   2544 	 * didn't fit in the alloted number of segments.
   2545 	 */
   2546 	for (first = 1;
   2547 	     ((rc = bus_dmamap_load_mbuf(dmat, dmam, m0,
   2548 			  BUS_DMA_WRITE|BUS_DMA_NOWAIT)) != 0 ||
   2549 	      dmam->dm_nsegs > ndescfree || rtw_seg_too_short(dmam)) && first;
   2550 	     first = 0) {
   2551 		if (rc == 0)
   2552 			bus_dmamap_unload(dmat, dmam);
   2553 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   2554 		if (m == NULL) {
   2555 			printf("%s: unable to allocate Tx mbuf\n",
   2556 			    dvname);
   2557 			break;
   2558 		}
   2559 		if (m0->m_pkthdr.len > MHLEN) {
   2560 			MCLGET(m, M_DONTWAIT);
   2561 			if ((m->m_flags & M_EXT) == 0) {
   2562 				printf("%s: cannot allocate Tx cluster\n",
   2563 				    dvname);
   2564 				m_freem(m);
   2565 				break;
   2566 			}
   2567 		}
   2568 		m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
   2569 		m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
   2570 		m_freem(m0);
   2571 		m0 = m;
   2572 		m = NULL;
   2573 	}
   2574 	if (rc != 0) {
   2575 		printf("%s: cannot load Tx buffer, rc = %d\n", dvname, rc);
   2576 		m_freem(m0);
   2577 		return NULL;
   2578 	} else if (rtw_seg_too_short(dmam)) {
   2579 		printf("%s: cannot load Tx buffer, segment too short\n",
   2580 		    dvname);
   2581 		bus_dmamap_unload(dmat, dmam);
   2582 		m_freem(m0);
   2583 		return NULL;
   2584 	} else if (dmam->dm_nsegs > ndescfree) {
   2585 		*ifflagsp |= IFF_OACTIVE;
   2586 		bus_dmamap_unload(dmat, dmam);
   2587 		m_freem(m0);
   2588 		return NULL;
   2589 	}
   2590 	return m0;
   2591 }
   2592 
   2593 #ifdef RTW_DEBUG
   2594 static void
   2595 rtw_print_txdesc(struct rtw_softc *sc, const char *action,
   2596     struct rtw_txctl *stx, struct rtw_txdesc_blk *htc, int desc)
   2597 {
   2598 	struct rtw_txdesc *htx = &htc->htc_desc[desc];
   2599 	DPRINTF(sc, RTW_DEBUG_XMIT_DESC, ("%s: %p %s txdesc[%d] ctl0 %#08x "
   2600 	    "ctl1 %#08x buf %#08x len %#08x\n",
   2601 	    sc->sc_dev.dv_xname, stx, action, desc,
   2602 	    le32toh(htx->htx_ctl0),
   2603 	    le32toh(htx->htx_ctl1), le32toh(htx->htx_buf),
   2604 	    le32toh(htx->htx_len)));
   2605 }
   2606 #endif /* RTW_DEBUG */
   2607 
   2608 static void
   2609 rtw_start(struct ifnet *ifp)
   2610 {
   2611 	uint8_t tppoll;
   2612 	int desc, i, lastdesc, npkt, rate;
   2613 	uint32_t proto_ctl0, ctl0, ctl1;
   2614 	bus_dmamap_t		dmamap;
   2615 	struct ieee80211com	*ic;
   2616 	struct ieee80211_duration *d0;
   2617 	struct ieee80211_frame	*wh;
   2618 	struct ieee80211_node	*ni;
   2619 	struct mbuf		*m0;
   2620 	struct rtw_softc	*sc;
   2621 	struct rtw_txctl_blk	*stc;
   2622 	struct rtw_txdesc_blk	*htc;
   2623 	struct rtw_txctl	*stx;
   2624 	struct rtw_txdesc	*htx;
   2625 
   2626 	sc = (struct rtw_softc *)ifp->if_softc;
   2627 	ic = &sc->sc_ic;
   2628 
   2629 	DPRINTF(sc, RTW_DEBUG_XMIT,
   2630 	    ("%s: enter %s\n", sc->sc_dev.dv_xname, __func__));
   2631 
   2632 	/* XXX do real rate control */
   2633 	proto_ctl0 = RTW_TXCTL0_RTSRATE_1MBPS;
   2634 
   2635 	switch (rate = MAX(2, ieee80211_get_rate(ic))) {
   2636 	case 2:
   2637 		proto_ctl0 |= RTW_TXCTL0_RATE_1MBPS;
   2638 		break;
   2639 	case 4:
   2640 		proto_ctl0 |= RTW_TXCTL0_RATE_2MBPS;
   2641 		break;
   2642 	case 11:
   2643 		proto_ctl0 |= RTW_TXCTL0_RATE_5MBPS;
   2644 		break;
   2645 	case 22:
   2646 		proto_ctl0 |= RTW_TXCTL0_RATE_11MBPS;
   2647 		break;
   2648 	}
   2649 
   2650 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0)
   2651 		proto_ctl0 |= RTW_TXCTL0_SPLCP;
   2652 
   2653 	for (;;) {
   2654 		if (rtw_dequeue(ifp, &stc, &htc, &m0, &ni) == -1)
   2655 			continue;
   2656 		if (m0 == NULL)
   2657 			break;
   2658 		stx = SIMPLEQ_FIRST(&stc->stc_freeq);
   2659 
   2660 		dmamap = stx->stx_dmamap;
   2661 
   2662 		m0 = rtw_dmamap_load_txbuf(sc->sc_dmat, dmamap, m0,
   2663 		    htc->htc_nfree, &ifp->if_flags, sc->sc_dev.dv_xname);
   2664 
   2665 		if (m0 == NULL || dmamap->dm_nsegs == 0) {
   2666 			DPRINTF(sc, RTW_DEBUG_XMIT,
   2667 			    ("%s: fail dmamap load\n", __func__));
   2668 			goto post_dequeue_err;
   2669 		}
   2670 
   2671 #ifdef RTW_DEBUG
   2672 		if ((sc->sc_if.if_flags & (IFF_DEBUG|IFF_LINK2)) ==
   2673 		    (IFF_DEBUG|IFF_LINK2)) {
   2674 			ieee80211_dump_pkt(mtod(m0, uint8_t *),
   2675 			    (dmamap->dm_nsegs == 1) ? m0->m_pkthdr.len
   2676 			                            : sizeof(wh),
   2677 			    rate, 0);
   2678 		}
   2679 #endif /* RTW_DEBUG */
   2680 		ctl0 = proto_ctl0 |
   2681 		    LSHIFT(m0->m_pkthdr.len, RTW_TXCTL0_TPKTSIZE_MASK);
   2682 
   2683 		wh = mtod(m0, struct ieee80211_frame *);
   2684 
   2685 		if (ieee80211_compute_duration(wh, m0->m_pkthdr.len,
   2686 		    ic->ic_flags, ic->ic_fragthreshold,
   2687 		    rate, &stx->stx_d0, &stx->stx_dn, &npkt,
   2688 		    (sc->sc_if.if_flags & (IFF_DEBUG|IFF_LINK2)) ==
   2689 		    (IFF_DEBUG|IFF_LINK2)) == -1) {
   2690 			DPRINTF(sc, RTW_DEBUG_XMIT,
   2691 			    ("%s: fail compute duration\n", __func__));
   2692 			goto post_load_err;
   2693 		}
   2694 
   2695 		/* XXX >= ? */
   2696 		if (m0->m_pkthdr.len > ic->ic_rtsthreshold)
   2697 			ctl0 |= RTW_TXCTL0_RTSEN;
   2698 
   2699 		d0 = &stx->stx_d0;
   2700 
   2701 		*(uint16_t*)wh->i_dur = htole16(d0->d_data_dur);
   2702 
   2703 		ctl1 = LSHIFT(d0->d_plcp_len, RTW_TXCTL1_LENGTH_MASK) |
   2704 		    LSHIFT(d0->d_rts_dur, RTW_TXCTL1_RTSDUR_MASK);
   2705 
   2706 		if (d0->d_residue)
   2707 			ctl1 |= RTW_TXCTL1_LENGEXT;
   2708 
   2709 		/* TBD fragmentation */
   2710 
   2711 		stx->stx_first = htc->htc_next;
   2712 
   2713 		rtw_txdescs_sync(sc->sc_dmat, sc->sc_desc_dmamap,
   2714 		    htc, stx->stx_first, dmamap->dm_nsegs,
   2715 		    BUS_DMASYNC_PREWRITE);
   2716 
   2717 		KASSERT(stx->stx_first < htc->htc_ndesc);
   2718 
   2719 		for (i = 0, lastdesc = desc = stx->stx_first;
   2720 		     i < dmamap->dm_nsegs;
   2721 		     i++, desc = RTW_NEXT_IDX(htc, desc)) {
   2722 			if (dmamap->dm_segs[i].ds_len > RTW_TXLEN_LENGTH_MASK) {
   2723 				DPRINTF(sc, RTW_DEBUG_XMIT_DESC,
   2724 				    ("%s: seg too long\n", __func__));
   2725 				goto post_load_err;
   2726 			}
   2727 			htx = &htc->htc_desc[desc];
   2728 			htx->htx_ctl0 = htole32(ctl0);
   2729 			if (i != 0)
   2730 				htx->htx_ctl0 |= htole32(RTW_TXCTL0_OWN);
   2731 			htx->htx_ctl1 = htole32(ctl1);
   2732 			htx->htx_buf = htole32(dmamap->dm_segs[i].ds_addr);
   2733 			htx->htx_len = htole32(dmamap->dm_segs[i].ds_len);
   2734 			lastdesc = desc;
   2735 #ifdef RTW_DEBUG
   2736 			rtw_print_txdesc(sc, "load", stx, htc, desc);
   2737 #endif /* RTW_DEBUG */
   2738 		}
   2739 
   2740 		KASSERT(desc < htc->htc_ndesc);
   2741 
   2742 		stx->stx_ni = ni;
   2743 		stx->stx_mbuf = m0;
   2744 		stx->stx_last = lastdesc;
   2745 		htc->htc_desc[stx->stx_last].htx_ctl0 |= htole32(RTW_TXCTL0_LS);
   2746 		htc->htc_desc[stx->stx_first].htx_ctl0 |=
   2747 		   htole32(RTW_TXCTL0_FS);
   2748 
   2749 #ifdef RTW_DEBUG
   2750 		rtw_print_txdesc(sc, "FS on", stx, htc, stx->stx_first);
   2751 		rtw_print_txdesc(sc, "LS on", stx, htc, stx->stx_last);
   2752 #endif /* RTW_DEBUG */
   2753 
   2754 		htc->htc_nfree -= dmamap->dm_nsegs;
   2755 		htc->htc_next = desc;
   2756 
   2757 		rtw_txdescs_sync(sc->sc_dmat, sc->sc_desc_dmamap,
   2758 		    htc, stx->stx_first, dmamap->dm_nsegs,
   2759 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   2760 
   2761 		htc->htc_desc[stx->stx_first].htx_ctl0 |=
   2762 		    htole32(RTW_TXCTL0_OWN);
   2763 
   2764 #ifdef RTW_DEBUG
   2765 		rtw_print_txdesc(sc, "OWN on", stx, htc, stx->stx_first);
   2766 #endif /* RTW_DEBUG */
   2767 
   2768 		rtw_txdescs_sync(sc->sc_dmat, sc->sc_desc_dmamap,
   2769 		    htc, stx->stx_first, 1,
   2770 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   2771 
   2772 		SIMPLEQ_REMOVE_HEAD(&stc->stc_freeq, stx_q);
   2773 		SIMPLEQ_INSERT_TAIL(&stc->stc_dirtyq, stx, stx_q);
   2774 
   2775 		stc->stc_tx_timer = 5;
   2776 		ifp->if_timer = 1;
   2777 
   2778 		tppoll = RTW_READ8(&sc->sc_regs, RTW_TPPOLL);
   2779 
   2780 		/* TBD poke other queues. */
   2781 		RTW_WRITE8(&sc->sc_regs, RTW_TPPOLL, tppoll | RTW_TPPOLL_NPQ);
   2782 		RTW_SYNC(&sc->sc_regs, RTW_TPPOLL, RTW_TPPOLL);
   2783 	}
   2784 	DPRINTF(sc, RTW_DEBUG_XMIT, ("%s: leave\n", __func__));
   2785 	return;
   2786 post_load_err:
   2787 	bus_dmamap_unload(sc->sc_dmat, dmamap);
   2788 	m_freem(m0);
   2789 post_dequeue_err:
   2790 	ieee80211_release_node(&sc->sc_ic, ni);
   2791 	return;
   2792 }
   2793 
   2794 static void
   2795 rtw_watchdog(struct ifnet *ifp)
   2796 {
   2797 	int pri;
   2798 	struct rtw_softc *sc;
   2799 	struct rtw_txctl_blk *stc;
   2800 
   2801 	sc = ifp->if_softc;
   2802 
   2803 	ifp->if_timer = 0;
   2804 
   2805 	if ((sc->sc_flags & RTW_F_ENABLED) == 0)
   2806 		return;
   2807 
   2808 	for (pri = 0; pri < RTW_NTXPRI; pri++) {
   2809 		stc = &sc->sc_txctl_blk[pri];
   2810 
   2811 		if (stc->stc_tx_timer == 0)
   2812 			continue;
   2813 
   2814 		if (--stc->stc_tx_timer == 0) {
   2815 			if (SIMPLEQ_EMPTY(&stc->stc_dirtyq))
   2816 				continue;
   2817 			printf("%s: transmit timeout, priority %d\n",
   2818 			    ifp->if_xname, pri);
   2819 			ifp->if_oerrors++;
   2820 			/* Stop Tx DMA, disable transmitter, clear
   2821 			 * Tx rings, and restart.
   2822 			 */
   2823 			RTW_WRITE8(&sc->sc_regs, RTW_TPPOLL, RTW_TPPOLL_SNPQ);
   2824 			RTW_SYNC(&sc->sc_regs, RTW_TPPOLL, RTW_TPPOLL);
   2825 			rtw_io_enable(&sc->sc_regs, RTW_CR_TE, 0);
   2826 			rtw_txdescs_reset(sc);
   2827 			rtw_io_enable(&sc->sc_regs, RTW_CR_TE, 1);
   2828 			rtw_start(ifp);
   2829 		} else
   2830 			ifp->if_timer = 1;
   2831 	}
   2832 	ieee80211_watchdog(ifp);
   2833 	return;
   2834 }
   2835 
   2836 static void
   2837 rtw_start_beacon(struct rtw_softc *sc, int enable)
   2838 {
   2839 	/* TBD */
   2840 	return;
   2841 }
   2842 
   2843 static void
   2844 rtw_next_scan(void *arg)
   2845 {
   2846 	struct ieee80211com *ic = arg;
   2847 	int s;
   2848 
   2849 	/* don't call rtw_start w/o network interrupts blocked */
   2850 	s = splnet();
   2851 	if (ic->ic_state == IEEE80211_S_SCAN)
   2852 		ieee80211_next_scan(ic);
   2853 	splx(s);
   2854 }
   2855 
   2856 static void
   2857 rtw_join_bss(struct rtw_softc *sc, uint8_t *bssid, enum ieee80211_opmode opmode,
   2858     uint16_t intval0)
   2859 {
   2860 	uint16_t bcnitv, intval;
   2861 	int i;
   2862 	struct rtw_regs *regs = &sc->sc_regs;
   2863 
   2864 	for (i = 0; i < IEEE80211_ADDR_LEN; i++)
   2865 		RTW_WRITE8(regs, RTW_BSSID + i, bssid[i]);
   2866 
   2867 	RTW_SYNC(regs, RTW_BSSID16, RTW_BSSID32);
   2868 
   2869 	rtw_set_access(sc, RTW_ACCESS_CONFIG);
   2870 
   2871 	intval = MIN(intval0, PRESHIFT(RTW_BCNITV_BCNITV_MASK));
   2872 
   2873 	bcnitv = RTW_READ16(regs, RTW_BCNITV) & ~RTW_BCNITV_BCNITV_MASK;
   2874 	bcnitv |= LSHIFT(intval, RTW_BCNITV_BCNITV_MASK);
   2875 	RTW_WRITE16(regs, RTW_BCNITV, bcnitv);
   2876 	/* magic from Linux */
   2877 	RTW_WRITE16(regs, RTW_ATIMWND, LSHIFT(1, RTW_ATIMWND_ATIMWND));
   2878 	RTW_WRITE16(regs, RTW_ATIMTRITV, LSHIFT(2, RTW_ATIMTRITV_ATIMTRITV));
   2879 
   2880 	rtw_set_nettype(sc, opmode);
   2881 
   2882 	rtw_set_access(sc, RTW_ACCESS_NONE);
   2883 
   2884 	/* TBD WEP */
   2885 	RTW_WRITE8(regs, RTW_SCR, 0);
   2886 
   2887 	rtw_io_enable(regs, RTW_CR_RE | RTW_CR_TE, 1);
   2888 }
   2889 
   2890 /* Synchronize the hardware state with the software state. */
   2891 static int
   2892 rtw_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
   2893 {
   2894 	struct ifnet *ifp = &ic->ic_if;
   2895 	struct rtw_softc *sc = ifp->if_softc;
   2896 	enum ieee80211_state ostate;
   2897 	int error;
   2898 
   2899 	ostate = ic->ic_state;
   2900 
   2901 	if (nstate == IEEE80211_S_INIT) {
   2902 		callout_stop(&sc->sc_scan_ch);
   2903 		sc->sc_cur_chan = IEEE80211_CHAN_ANY;
   2904 		rtw_start_beacon(sc, 0);
   2905 		return (*sc->sc_mtbl.mt_newstate)(ic, nstate, arg);
   2906 	}
   2907 
   2908 	if (ostate == IEEE80211_S_INIT && nstate != IEEE80211_S_INIT)
   2909 		rtw_pwrstate(sc, RTW_ON);
   2910 
   2911 	if ((error = rtw_tune(sc)) != 0)
   2912 		return error;
   2913 
   2914 	switch (nstate) {
   2915 	case IEEE80211_S_ASSOC:
   2916 		rtw_join_bss(sc, ic->ic_bss->ni_bssid, ic->ic_opmode,
   2917 		    ic->ic_bss->ni_intval);
   2918 		break;
   2919 	case IEEE80211_S_INIT:
   2920 		panic("%s: unexpected state IEEE80211_S_INIT\n", __func__);
   2921 		break;
   2922 	case IEEE80211_S_SCAN:
   2923 		if (ostate != IEEE80211_S_SCAN) {
   2924 			(void)memset(ic->ic_bss->ni_bssid, 0,
   2925 			    IEEE80211_ADDR_LEN);
   2926 			rtw_join_bss(sc, ic->ic_bss->ni_bssid, ic->ic_opmode,
   2927 			    ic->ic_bss->ni_intval);
   2928 		}
   2929 
   2930 		callout_reset(&sc->sc_scan_ch, rtw_dwelltime * hz / 1000,
   2931 		    rtw_next_scan, ic);
   2932 
   2933 		break;
   2934 	case IEEE80211_S_RUN:
   2935 		if (ic->ic_opmode == IEEE80211_M_STA)
   2936 			break;
   2937 		/*FALLTHROUGH*/
   2938 	case IEEE80211_S_AUTH:
   2939 #if 0
   2940 		rtw_write_bcn_thresh(sc);
   2941 		rtw_write_ssid(sc);
   2942 		rtw_write_sup_rates(sc);
   2943 #endif
   2944 		if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
   2945 		    ic->ic_opmode == IEEE80211_M_MONITOR)
   2946 			break;
   2947 
   2948 		/* TBD set listen interval */
   2949 
   2950 #if 0
   2951 		rtw_tsf(sc);
   2952 #endif
   2953 		break;
   2954 	}
   2955 
   2956 	if (nstate != IEEE80211_S_SCAN)
   2957 		callout_stop(&sc->sc_scan_ch);
   2958 
   2959 	if (nstate == IEEE80211_S_RUN &&
   2960 	    (ic->ic_opmode == IEEE80211_M_HOSTAP ||
   2961 	     ic->ic_opmode == IEEE80211_M_IBSS))
   2962 		rtw_start_beacon(sc, 1);
   2963 	else
   2964 		rtw_start_beacon(sc, 0);
   2965 
   2966 	return (*sc->sc_mtbl.mt_newstate)(ic, nstate, arg);
   2967 }
   2968 
   2969 static void
   2970 rtw_recv_beacon(struct rtw_softc *sc, struct mbuf *m,
   2971     struct ieee80211_node *ni, int subtype, int rssi, u_int32_t rstamp)
   2972 {
   2973 	(*sc->sc_mtbl.mt_recv_mgmt)(&sc->sc_ic, m, ni, subtype, rssi, rstamp);
   2974 	return;
   2975 }
   2976 
   2977 static void
   2978 rtw_recv_mgmt(struct ieee80211com *ic, struct mbuf *m,
   2979     struct ieee80211_node *ni, int subtype, int rssi, u_int32_t rstamp)
   2980 {
   2981 	struct rtw_softc *sc = (struct rtw_softc*)ic->ic_softc;
   2982 
   2983 	switch (subtype) {
   2984 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
   2985 		/* do nothing: hardware answers probe request XXX */
   2986 		break;
   2987 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
   2988 	case IEEE80211_FC0_SUBTYPE_BEACON:
   2989 		rtw_recv_beacon(sc, m, ni, subtype, rssi, rstamp);
   2990 		break;
   2991 	default:
   2992 		(*sc->sc_mtbl.mt_recv_mgmt)(ic, m, ni, subtype, rssi, rstamp);
   2993 		break;
   2994 	}
   2995 	return;
   2996 }
   2997 
   2998 static struct ieee80211_node *
   2999 rtw_node_alloc(struct ieee80211com *ic)
   3000 {
   3001 	struct rtw_softc *sc = (struct rtw_softc *)ic->ic_if.if_softc;
   3002 	struct ieee80211_node *ni = (*sc->sc_mtbl.mt_node_alloc)(ic);
   3003 
   3004 	DPRINTF(sc, RTW_DEBUG_NODE,
   3005 	    ("%s: alloc node %p\n", sc->sc_dev.dv_xname, ni));
   3006 	return ni;
   3007 }
   3008 
   3009 static void
   3010 rtw_node_free(struct ieee80211com *ic, struct ieee80211_node *ni)
   3011 {
   3012 	struct rtw_softc *sc = (struct rtw_softc *)ic->ic_if.if_softc;
   3013 
   3014 	DPRINTF(sc, RTW_DEBUG_NODE,
   3015 	    ("%s: freeing node %p %s\n", sc->sc_dev.dv_xname, ni,
   3016 	    ether_sprintf(ni->ni_bssid)));
   3017 	(*sc->sc_mtbl.mt_node_free)(ic, ni);
   3018 }
   3019 
   3020 static int
   3021 rtw_media_change(struct ifnet *ifp)
   3022 {
   3023 	int error;
   3024 
   3025 	error = ieee80211_media_change(ifp);
   3026 	if (error == ENETRESET) {
   3027 		if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) ==
   3028 		    (IFF_RUNNING|IFF_UP))
   3029 			rtw_init(ifp);		/* XXX lose error */
   3030 		error = 0;
   3031 	}
   3032 	return error;
   3033 }
   3034 
   3035 static void
   3036 rtw_media_status(struct ifnet *ifp, struct ifmediareq *imr)
   3037 {
   3038 	struct rtw_softc *sc = ifp->if_softc;
   3039 
   3040 	if ((sc->sc_flags & RTW_F_ENABLED) == 0) {
   3041 		imr->ifm_active = IFM_IEEE80211 | IFM_NONE;
   3042 		imr->ifm_status = 0;
   3043 		return;
   3044 	}
   3045 	ieee80211_media_status(ifp, imr);
   3046 }
   3047 
   3048 void
   3049 rtw_power(int why, void *arg)
   3050 {
   3051 	struct rtw_softc *sc = arg;
   3052 	struct ifnet *ifp = &sc->sc_ic.ic_if;
   3053 	int s;
   3054 
   3055 	DPRINTF(sc, RTW_DEBUG_PWR,
   3056 	    ("%s: rtw_power(%d,)\n", sc->sc_dev.dv_xname, why));
   3057 
   3058 	s = splnet();
   3059 	switch (why) {
   3060 	case PWR_STANDBY:
   3061 		/* XXX do nothing. */
   3062 		break;
   3063 	case PWR_SUSPEND:
   3064 		rtw_stop(ifp, 0);
   3065 		if (sc->sc_power != NULL)
   3066 			(*sc->sc_power)(sc, why);
   3067 		break;
   3068 	case PWR_RESUME:
   3069 		if (ifp->if_flags & IFF_UP) {
   3070 			if (sc->sc_power != NULL)
   3071 				(*sc->sc_power)(sc, why);
   3072 			rtw_init(ifp);
   3073 		}
   3074 		break;
   3075 	case PWR_SOFTSUSPEND:
   3076 	case PWR_SOFTSTANDBY:
   3077 	case PWR_SOFTRESUME:
   3078 		break;
   3079 	}
   3080 	splx(s);
   3081 }
   3082 
   3083 /* rtw_shutdown: make sure the interface is stopped at reboot time. */
   3084 void
   3085 rtw_shutdown(void *arg)
   3086 {
   3087 	struct rtw_softc *sc = arg;
   3088 
   3089 	rtw_stop(&sc->sc_ic.ic_if, 1);
   3090 }
   3091 
   3092 static __inline void
   3093 rtw_setifprops(struct ifnet *ifp, const char *dvname, void *softc)
   3094 {
   3095 	(void)memcpy(ifp->if_xname, dvname, IFNAMSIZ);
   3096 	ifp->if_softc = softc;
   3097 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST |
   3098 	    IFF_NOTRAILERS;
   3099 	ifp->if_ioctl = rtw_ioctl;
   3100 	ifp->if_start = rtw_start;
   3101 	ifp->if_watchdog = rtw_watchdog;
   3102 	ifp->if_init = rtw_init;
   3103 	ifp->if_stop = rtw_stop;
   3104 }
   3105 
   3106 static __inline void
   3107 rtw_set80211props(struct ieee80211com *ic)
   3108 {
   3109 	int nrate;
   3110 	ic->ic_phytype = IEEE80211_T_DS;
   3111 	ic->ic_opmode = IEEE80211_M_STA;
   3112 	ic->ic_caps = IEEE80211_C_PMGT | IEEE80211_C_IBSS |
   3113 	    IEEE80211_C_HOSTAP | IEEE80211_C_MONITOR | IEEE80211_C_WEP;
   3114 
   3115 	nrate = 0;
   3116 	ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[nrate++] =
   3117 	    IEEE80211_RATE_BASIC | 2;
   3118 	ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[nrate++] =
   3119 	    IEEE80211_RATE_BASIC | 4;
   3120 	ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[nrate++] = 11;
   3121 	ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[nrate++] = 22;
   3122 	ic->ic_sup_rates[IEEE80211_MODE_11B].rs_nrates = nrate;
   3123 }
   3124 
   3125 static __inline void
   3126 rtw_set80211methods(struct rtw_mtbl *mtbl, struct ieee80211com *ic)
   3127 {
   3128 	mtbl->mt_newstate = ic->ic_newstate;
   3129 	ic->ic_newstate = rtw_newstate;
   3130 
   3131 	mtbl->mt_recv_mgmt = ic->ic_recv_mgmt;
   3132 	ic->ic_recv_mgmt = rtw_recv_mgmt;
   3133 
   3134 	mtbl->mt_node_free = ic->ic_node_free;
   3135 	ic->ic_node_free = rtw_node_free;
   3136 
   3137 	mtbl->mt_node_alloc = ic->ic_node_alloc;
   3138 	ic->ic_node_alloc = rtw_node_alloc;
   3139 }
   3140 
   3141 static __inline void
   3142 rtw_establish_hooks(struct rtw_hooks *hooks, const char *dvname,
   3143     void *arg)
   3144 {
   3145 	/*
   3146 	 * Make sure the interface is shutdown during reboot.
   3147 	 */
   3148 	hooks->rh_shutdown = shutdownhook_establish(rtw_shutdown, arg);
   3149 	if (hooks->rh_shutdown == NULL)
   3150 		printf("%s: WARNING: unable to establish shutdown hook\n",
   3151 		    dvname);
   3152 
   3153 	/*
   3154 	 * Add a suspend hook to make sure we come back up after a
   3155 	 * resume.
   3156 	 */
   3157 	hooks->rh_power = powerhook_establish(rtw_power, arg);
   3158 	if (hooks->rh_power == NULL)
   3159 		printf("%s: WARNING: unable to establish power hook\n",
   3160 		    dvname);
   3161 }
   3162 
   3163 static __inline void
   3164 rtw_disestablish_hooks(struct rtw_hooks *hooks, const char *dvname,
   3165     void *arg)
   3166 {
   3167 	if (hooks->rh_shutdown != NULL)
   3168 		shutdownhook_disestablish(hooks->rh_shutdown);
   3169 
   3170 	if (hooks->rh_power != NULL)
   3171 		powerhook_disestablish(hooks->rh_power);
   3172 }
   3173 
   3174 static __inline void
   3175 rtw_init_radiotap(struct rtw_softc *sc)
   3176 {
   3177 	memset(&sc->sc_rxtapu, 0, sizeof(sc->sc_rxtapu));
   3178 	sc->sc_rxtap.rr_ihdr.it_len = sizeof(sc->sc_rxtapu);
   3179 	sc->sc_rxtap.rr_ihdr.it_present = RTW_RX_RADIOTAP_PRESENT;
   3180 
   3181 	memset(&sc->sc_txtapu, 0, sizeof(sc->sc_txtapu));
   3182 	sc->sc_txtap.rt_ihdr.it_len = sizeof(sc->sc_txtapu);
   3183 	sc->sc_txtap.rt_ihdr.it_present = RTW_TX_RADIOTAP_PRESENT;
   3184 }
   3185 
   3186 static int
   3187 rtw_txctl_blk_setup(struct rtw_txctl_blk *stc, u_int qlen)
   3188 {
   3189 	SIMPLEQ_INIT(&stc->stc_dirtyq);
   3190 	SIMPLEQ_INIT(&stc->stc_freeq);
   3191 	stc->stc_ndesc = qlen;
   3192 	stc->stc_desc = malloc(qlen * sizeof(*stc->stc_desc), M_DEVBUF,
   3193 	    M_NOWAIT);
   3194 	if (stc->stc_desc == NULL)
   3195 		return ENOMEM;
   3196 	return 0;
   3197 }
   3198 
   3199 static void
   3200 rtw_txctl_blk_cleanup_all(struct rtw_softc *sc)
   3201 {
   3202 	int pri;
   3203 	struct rtw_txctl_blk *stc;
   3204 
   3205 	for (pri = 0; pri < RTW_NTXPRI; pri++) {
   3206 		stc = &sc->sc_txctl_blk[pri];
   3207 		free(stc->stc_desc, M_DEVBUF);
   3208 		stc->stc_desc = NULL;
   3209 	}
   3210 }
   3211 
   3212 static int
   3213 rtw_txctl_blk_setup_all(struct rtw_softc *sc)
   3214 {
   3215 	int pri, rc = 0;
   3216 	int qlen[RTW_NTXPRI] =
   3217 	     {RTW_TXQLENLO, RTW_TXQLENMD, RTW_TXQLENHI, RTW_TXQLENBCN};
   3218 
   3219 	for (pri = 0; pri < RTW_NTXPRI; pri++) {
   3220 		rc = rtw_txctl_blk_setup(&sc->sc_txctl_blk[pri], qlen[pri]);
   3221 		if (rc != 0)
   3222 			break;
   3223 	}
   3224 	return rc;
   3225 }
   3226 
   3227 static void
   3228 rtw_txdesc_blk_setup(struct rtw_txdesc_blk *htc, struct rtw_txdesc *desc,
   3229     u_int ndesc, bus_addr_t ofs, bus_addr_t physbase)
   3230 {
   3231 	htc->htc_ndesc = ndesc;
   3232 	htc->htc_desc = desc;
   3233 	htc->htc_physbase = physbase;
   3234 	htc->htc_ofs = ofs;
   3235 
   3236 	(void)memset(htc->htc_desc, 0,
   3237 	    sizeof(htc->htc_desc[0]) * htc->htc_ndesc);
   3238 
   3239 	rtw_txdesc_blk_reset(htc);
   3240 }
   3241 
   3242 static void
   3243 rtw_txdesc_blk_setup_all(struct rtw_softc *sc)
   3244 {
   3245 	rtw_txdesc_blk_setup(&sc->sc_txdesc_blk[RTW_TXPRILO],
   3246 	    &sc->sc_descs->hd_txlo[0], RTW_NTXDESCLO,
   3247 	    RTW_RING_OFFSET(hd_txlo), RTW_RING_BASE(sc, hd_txlo));
   3248 
   3249 	rtw_txdesc_blk_setup(&sc->sc_txdesc_blk[RTW_TXPRIMD],
   3250 	    &sc->sc_descs->hd_txmd[0], RTW_NTXDESCMD,
   3251 	    RTW_RING_OFFSET(hd_txmd), RTW_RING_BASE(sc, hd_txmd));
   3252 
   3253 	rtw_txdesc_blk_setup(&sc->sc_txdesc_blk[RTW_TXPRIHI],
   3254 	    &sc->sc_descs->hd_txhi[0], RTW_NTXDESCHI,
   3255 	    RTW_RING_OFFSET(hd_txhi), RTW_RING_BASE(sc, hd_txhi));
   3256 
   3257 	rtw_txdesc_blk_setup(&sc->sc_txdesc_blk[RTW_TXPRIBCN],
   3258 	    &sc->sc_descs->hd_bcn[0], RTW_NTXDESCBCN,
   3259 	    RTW_RING_OFFSET(hd_bcn), RTW_RING_BASE(sc, hd_bcn));
   3260 }
   3261 
   3262 static struct rtw_rf *
   3263 rtw_rf_attach(struct rtw_softc *sc, enum rtw_rfchipid rfchipid,
   3264     rtw_rf_write_t rf_write, int digphy)
   3265 {
   3266 	struct rtw_rf *rf;
   3267 
   3268 	switch (rfchipid) {
   3269 	case RTW_RFCHIPID_MAXIM:
   3270 		rf = rtw_max2820_create(&sc->sc_regs, rf_write, 0);
   3271 		sc->sc_pwrstate_cb = rtw_maxim_pwrstate;
   3272 		break;
   3273 	case RTW_RFCHIPID_PHILIPS:
   3274 		rf = rtw_sa2400_create(&sc->sc_regs, rf_write, digphy);
   3275 		sc->sc_pwrstate_cb = rtw_philips_pwrstate;
   3276 		break;
   3277 	case RTW_RFCHIPID_RFMD:
   3278 		/* XXX RFMD has no RF constructor */
   3279 		sc->sc_pwrstate_cb = rtw_rfmd_pwrstate;
   3280 		/*FALLTHROUGH*/
   3281 	default:
   3282 		return NULL;
   3283 	}
   3284 	rf->rf_continuous_tx_cb =
   3285 	    (rtw_continuous_tx_cb_t)rtw_continuous_tx_enable;
   3286 	rf->rf_continuous_tx_arg = (void *)sc;
   3287 	return rf;
   3288 }
   3289 
   3290 /* Revision C and later use a different PHY delay setting than
   3291  * revisions A and B.
   3292  */
   3293 static u_int8_t
   3294 rtw_check_phydelay(struct rtw_regs *regs, u_int32_t rcr0)
   3295 {
   3296 #define REVAB (RTW_RCR_MXDMA_UNLIMITED | RTW_RCR_AICV)
   3297 #define REVC (REVAB | RTW_RCR_RXFTH_WHOLE)
   3298 
   3299 	u_int8_t phydelay = LSHIFT(0x6, RTW_PHYDELAY_PHYDELAY);
   3300 
   3301 	RTW_WRITE(regs, RTW_RCR, REVAB);
   3302 	RTW_WBW(regs, RTW_RCR, RTW_RCR);
   3303 	RTW_WRITE(regs, RTW_RCR, REVC);
   3304 
   3305 	RTW_WBR(regs, RTW_RCR, RTW_RCR);
   3306 	if ((RTW_READ(regs, RTW_RCR) & REVC) == REVC)
   3307 		phydelay |= RTW_PHYDELAY_REVC_MAGIC;
   3308 
   3309 	RTW_WRITE(regs, RTW_RCR, rcr0);	/* restore RCR */
   3310 	RTW_SYNC(regs, RTW_RCR, RTW_RCR);
   3311 
   3312 	return phydelay;
   3313 #undef REVC
   3314 }
   3315 
   3316 void
   3317 rtw_attach(struct rtw_softc *sc)
   3318 {
   3319 	rtw_rf_write_t rf_write;
   3320 	struct rtw_txctl_blk *stc;
   3321 	int pri, rc, vers;
   3322 
   3323 #if 0
   3324 	CASSERT(RTW_DESC_ALIGNMENT % sizeof(struct rtw_txdesc) == 0,
   3325 	    "RTW_DESC_ALIGNMENT is not a multiple of "
   3326 	    "sizeof(struct rtw_txdesc)");
   3327 
   3328 	CASSERT(RTW_DESC_ALIGNMENT % sizeof(struct rtw_rxdesc) == 0,
   3329 	    "RTW_DESC_ALIGNMENT is not a multiple of "
   3330 	    "sizeof(struct rtw_rxdesc)");
   3331 
   3332 	CASSERT(RTW_DESC_ALIGNMENT % RTW_MAXPKTSEGS == 0,
   3333 	    "RTW_DESC_ALIGNMENT is not a multiple of RTW_MAXPKTSEGS");
   3334 #endif
   3335 
   3336 	NEXT_ATTACH_STATE(sc, DETACHED);
   3337 
   3338 	switch (RTW_READ(&sc->sc_regs, RTW_TCR) & RTW_TCR_HWVERID_MASK) {
   3339 	case RTW_TCR_HWVERID_F:
   3340 		vers = 'F';
   3341 		rf_write = rtw_rf_hostwrite;
   3342 		break;
   3343 	case RTW_TCR_HWVERID_D:
   3344 		vers = 'D';
   3345 		if (rtw_host_rfio)
   3346 			rf_write = rtw_rf_hostwrite;
   3347 		else
   3348 			rf_write = rtw_rf_macwrite;
   3349 		break;
   3350 	default:
   3351 		vers = '?';
   3352 		rf_write = rtw_rf_macwrite;
   3353 		break;
   3354 	}
   3355 	printf("%s: hardware version %c\n", sc->sc_dev.dv_xname, vers);
   3356 
   3357 	rc = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct rtw_descs),
   3358 	    RTW_DESC_ALIGNMENT, 0, &sc->sc_desc_segs, 1, &sc->sc_desc_nsegs,
   3359 	    0);
   3360 
   3361 	if (rc != 0) {
   3362 		printf("%s: could not allocate hw descriptors, error %d\n",
   3363 		     sc->sc_dev.dv_xname, rc);
   3364 		goto err;
   3365 	}
   3366 
   3367 	NEXT_ATTACH_STATE(sc, FINISH_DESC_ALLOC);
   3368 
   3369 	rc = bus_dmamem_map(sc->sc_dmat, &sc->sc_desc_segs,
   3370 	    sc->sc_desc_nsegs, sizeof(struct rtw_descs),
   3371 	    (caddr_t*)&sc->sc_descs, BUS_DMA_COHERENT);
   3372 
   3373 	if (rc != 0) {
   3374 		printf("%s: could not map hw descriptors, error %d\n",
   3375 		    sc->sc_dev.dv_xname, rc);
   3376 		goto err;
   3377 	}
   3378 	NEXT_ATTACH_STATE(sc, FINISH_DESC_MAP);
   3379 
   3380 	rc = bus_dmamap_create(sc->sc_dmat, sizeof(struct rtw_descs), 1,
   3381 	    sizeof(struct rtw_descs), 0, 0, &sc->sc_desc_dmamap);
   3382 
   3383 	if (rc != 0) {
   3384 		printf("%s: could not create DMA map for hw descriptors, "
   3385 		    "error %d\n", sc->sc_dev.dv_xname, rc);
   3386 		goto err;
   3387 	}
   3388 	NEXT_ATTACH_STATE(sc, FINISH_DESCMAP_CREATE);
   3389 
   3390 	rc = bus_dmamap_load(sc->sc_dmat, sc->sc_desc_dmamap, sc->sc_descs,
   3391 	    sizeof(struct rtw_descs), NULL, 0);
   3392 
   3393 	if (rc != 0) {
   3394 		printf("%s: could not load DMA map for hw descriptors, "
   3395 		    "error %d\n", sc->sc_dev.dv_xname, rc);
   3396 		goto err;
   3397 	}
   3398 	NEXT_ATTACH_STATE(sc, FINISH_DESCMAP_LOAD);
   3399 
   3400 	if (rtw_txctl_blk_setup_all(sc) != 0)
   3401 		goto err;
   3402 	NEXT_ATTACH_STATE(sc, FINISH_TXCTLBLK_SETUP);
   3403 
   3404 	rtw_txdesc_blk_setup_all(sc);
   3405 
   3406 	NEXT_ATTACH_STATE(sc, FINISH_TXDESCBLK_SETUP);
   3407 
   3408 	sc->sc_rxdesc = &sc->sc_descs->hd_rx[0];
   3409 
   3410 	rtw_rxctls_setup(&sc->sc_rxctl[0]);
   3411 
   3412 	for (pri = 0; pri < RTW_NTXPRI; pri++) {
   3413 		stc = &sc->sc_txctl_blk[pri];
   3414 
   3415 		if ((rc = rtw_txdesc_dmamaps_create(sc->sc_dmat,
   3416 		    &stc->stc_desc[0], stc->stc_ndesc)) != 0) {
   3417 			printf("%s: could not load DMA map for "
   3418 			    "hw tx descriptors, error %d\n",
   3419 			    sc->sc_dev.dv_xname, rc);
   3420 			goto err;
   3421 		}
   3422 	}
   3423 
   3424 	NEXT_ATTACH_STATE(sc, FINISH_TXMAPS_CREATE);
   3425 	if ((rc = rtw_rxdesc_dmamaps_create(sc->sc_dmat, &sc->sc_rxctl[0],
   3426 	                                    RTW_RXQLEN)) != 0) {
   3427 		printf("%s: could not load DMA map for hw rx descriptors, "
   3428 		    "error %d\n", sc->sc_dev.dv_xname, rc);
   3429 		goto err;
   3430 	}
   3431 	NEXT_ATTACH_STATE(sc, FINISH_RXMAPS_CREATE);
   3432 
   3433 	/* Reset the chip to a known state. */
   3434 	if (rtw_reset(sc) != 0)
   3435 		goto err;
   3436 	NEXT_ATTACH_STATE(sc, FINISH_RESET);
   3437 
   3438 	sc->sc_rcr = RTW_READ(&sc->sc_regs, RTW_RCR);
   3439 
   3440 	if ((sc->sc_rcr & RTW_RCR_9356SEL) != 0)
   3441 		sc->sc_flags |= RTW_F_9356SROM;
   3442 
   3443 	if (rtw_srom_read(&sc->sc_regs, sc->sc_flags, &sc->sc_srom,
   3444 	    sc->sc_dev.dv_xname) != 0)
   3445 		goto err;
   3446 
   3447 	NEXT_ATTACH_STATE(sc, FINISH_READ_SROM);
   3448 
   3449 	if (rtw_srom_parse(&sc->sc_srom, &sc->sc_flags, &sc->sc_csthr,
   3450 	    &sc->sc_rfchipid, &sc->sc_rcr, &sc->sc_locale,
   3451 	    sc->sc_dev.dv_xname) != 0) {
   3452 		printf("%s: attach failed, malformed serial ROM\n",
   3453 		    sc->sc_dev.dv_xname);
   3454 		goto err;
   3455 	}
   3456 
   3457 	printf("%s: %s PHY\n", sc->sc_dev.dv_xname,
   3458 	    ((sc->sc_flags & RTW_F_DIGPHY) != 0) ? "digital" : "analog");
   3459 
   3460 	printf("%s: CS threshold %u\n", sc->sc_dev.dv_xname, sc->sc_csthr);
   3461 
   3462 	NEXT_ATTACH_STATE(sc, FINISH_PARSE_SROM);
   3463 
   3464 	sc->sc_rf = rtw_rf_attach(sc, sc->sc_rfchipid, rf_write,
   3465 	    sc->sc_flags & RTW_F_DIGPHY);
   3466 
   3467 	if (sc->sc_rf == NULL) {
   3468 		printf("%s: attach failed, could not attach RF\n",
   3469 		    sc->sc_dev.dv_xname);
   3470 		goto err;
   3471 	}
   3472 
   3473 #if 0
   3474 	if (rtw_identify_rf(&sc->sc_regs, &sc->sc_rftype,
   3475 	    sc->sc_dev.dv_xname) != 0) {
   3476 		printf("%s: attach failed, unknown RF unidentified\n",
   3477 		    sc->sc_dev.dv_xname);
   3478 		goto err;
   3479 	}
   3480 #endif
   3481 
   3482 	NEXT_ATTACH_STATE(sc, FINISH_RF_ATTACH);
   3483 
   3484 	sc->sc_phydelay = rtw_check_phydelay(&sc->sc_regs, sc->sc_rcr);
   3485 
   3486 	RTW_DPRINTF(RTW_DEBUG_ATTACH,
   3487 	    ("%s: PHY delay %d\n", sc->sc_dev.dv_xname, sc->sc_phydelay));
   3488 
   3489 	if (sc->sc_locale == RTW_LOCALE_UNKNOWN)
   3490 		rtw_identify_country(&sc->sc_regs, &sc->sc_locale,
   3491 		    sc->sc_dev.dv_xname);
   3492 
   3493 	rtw_init_channels(sc->sc_locale, &sc->sc_ic.ic_channels,
   3494 	    sc->sc_dev.dv_xname);
   3495 
   3496 	if (rtw_identify_sta(&sc->sc_regs, &sc->sc_ic.ic_myaddr,
   3497 	    sc->sc_dev.dv_xname) != 0)
   3498 		goto err;
   3499 	NEXT_ATTACH_STATE(sc, FINISH_ID_STA);
   3500 
   3501 	rtw_setifprops(&sc->sc_if, sc->sc_dev.dv_xname, (void*)sc);
   3502 
   3503 	IFQ_SET_READY(&sc->sc_if.if_snd);
   3504 
   3505 	rtw_set80211props(&sc->sc_ic);
   3506 
   3507 	/*
   3508 	 * Call MI attach routines.
   3509 	 */
   3510 	if_attach(&sc->sc_if);
   3511 	ieee80211_ifattach(&sc->sc_if);
   3512 
   3513 	rtw_set80211methods(&sc->sc_mtbl, &sc->sc_ic);
   3514 
   3515 	/* possibly we should fill in our own sc_send_prresp, since
   3516 	 * the RTL8180 is probably sending probe responses in ad hoc
   3517 	 * mode.
   3518 	 */
   3519 
   3520 	/* complete initialization */
   3521 	ieee80211_media_init(&sc->sc_if, rtw_media_change, rtw_media_status);
   3522 	callout_init(&sc->sc_scan_ch);
   3523 
   3524 #if NBPFILTER > 0
   3525 	bpfattach2(&sc->sc_if, DLT_IEEE802_11_RADIO,
   3526 	    sizeof(struct ieee80211_frame) + 64, &sc->sc_radiobpf);
   3527 #endif
   3528 
   3529 	rtw_establish_hooks(&sc->sc_hooks, sc->sc_dev.dv_xname, (void*)sc);
   3530 
   3531 	rtw_init_radiotap(sc);
   3532 
   3533 	NEXT_ATTACH_STATE(sc, FINISHED);
   3534 
   3535 	return;
   3536 err:
   3537 	rtw_detach(sc);
   3538 	return;
   3539 }
   3540 
   3541 int
   3542 rtw_detach(struct rtw_softc *sc)
   3543 {
   3544 	int pri;
   3545 
   3546 	switch (sc->sc_attach_state) {
   3547 	case FINISHED:
   3548 		rtw_stop(&sc->sc_if, 1);
   3549 
   3550 		rtw_disestablish_hooks(&sc->sc_hooks, sc->sc_dev.dv_xname,
   3551 		    (void*)sc);
   3552 		callout_stop(&sc->sc_scan_ch);
   3553 		ieee80211_ifdetach(&sc->sc_if);
   3554 		if_detach(&sc->sc_if);
   3555 		break;
   3556 	case FINISH_ID_STA:
   3557 	case FINISH_RF_ATTACH:
   3558 		rtw_rf_destroy(sc->sc_rf);
   3559 		sc->sc_rf = NULL;
   3560 		/*FALLTHROUGH*/
   3561 	case FINISH_PARSE_SROM:
   3562 	case FINISH_READ_SROM:
   3563 		rtw_srom_free(&sc->sc_srom);
   3564 		/*FALLTHROUGH*/
   3565 	case FINISH_RESET:
   3566 	case FINISH_RXMAPS_CREATE:
   3567 		rtw_rxdesc_dmamaps_destroy(sc->sc_dmat, &sc->sc_rxctl[0],
   3568 		    RTW_RXQLEN);
   3569 		/*FALLTHROUGH*/
   3570 	case FINISH_TXMAPS_CREATE:
   3571 		for (pri = 0; pri < RTW_NTXPRI; pri++) {
   3572 			rtw_txdesc_dmamaps_destroy(sc->sc_dmat,
   3573 			    sc->sc_txctl_blk[pri].stc_desc,
   3574 			    sc->sc_txctl_blk[pri].stc_ndesc);
   3575 		}
   3576 		/*FALLTHROUGH*/
   3577 	case FINISH_TXDESCBLK_SETUP:
   3578 	case FINISH_TXCTLBLK_SETUP:
   3579 		rtw_txctl_blk_cleanup_all(sc);
   3580 		/*FALLTHROUGH*/
   3581 	case FINISH_DESCMAP_LOAD:
   3582 		bus_dmamap_unload(sc->sc_dmat, sc->sc_desc_dmamap);
   3583 		/*FALLTHROUGH*/
   3584 	case FINISH_DESCMAP_CREATE:
   3585 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_desc_dmamap);
   3586 		/*FALLTHROUGH*/
   3587 	case FINISH_DESC_MAP:
   3588 		bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_descs,
   3589 		    sizeof(struct rtw_descs));
   3590 		/*FALLTHROUGH*/
   3591 	case FINISH_DESC_ALLOC:
   3592 		bus_dmamem_free(sc->sc_dmat, &sc->sc_desc_segs,
   3593 		    sc->sc_desc_nsegs);
   3594 		/*FALLTHROUGH*/
   3595 	case DETACHED:
   3596 		NEXT_ATTACH_STATE(sc, DETACHED);
   3597 		break;
   3598 	}
   3599 	return 0;
   3600 }
   3601 
   3602 int
   3603 rtw_activate(struct device *self, enum devact act)
   3604 {
   3605 	struct rtw_softc *sc = (struct rtw_softc *)self;
   3606 	int rc = 0, s;
   3607 
   3608 	s = splnet();
   3609 	switch (act) {
   3610 	case DVACT_ACTIVATE:
   3611 		rc = EOPNOTSUPP;
   3612 		break;
   3613 
   3614 	case DVACT_DEACTIVATE:
   3615 		if_deactivate(&sc->sc_ic.ic_if);
   3616 		break;
   3617 	}
   3618 	splx(s);
   3619 	return rc;
   3620 }
   3621