Home | History | Annotate | Line # | Download | only in ic
atw.c revision 1.98
      1 /*	$NetBSD: atw.c,v 1.98 2005/12/29 21:44:33 dyoung Exp $  */
      2 
      3 /*-
      4  * Copyright (c) 1998, 1999, 2000, 2002, 2003, 2004 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by David Young, by Jason R. Thorpe, and by Charles M. Hannum.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Device driver for the ADMtek ADM8211 802.11 MAC/BBP.
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: atw.c,v 1.98 2005/12/29 21:44:33 dyoung Exp $");
     45 
     46 #include "bpfilter.h"
     47 
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/callout.h>
     51 #include <sys/mbuf.h>
     52 #include <sys/malloc.h>
     53 #include <sys/kernel.h>
     54 #include <sys/socket.h>
     55 #include <sys/ioctl.h>
     56 #include <sys/errno.h>
     57 #include <sys/device.h>
     58 #include <sys/time.h>
     59 
     60 #include <machine/endian.h>
     61 
     62 #include <uvm/uvm_extern.h>
     63 
     64 #include <net/if.h>
     65 #include <net/if_dl.h>
     66 #include <net/if_media.h>
     67 #include <net/if_ether.h>
     68 
     69 #include <net80211/ieee80211_netbsd.h>
     70 #include <net80211/ieee80211_var.h>
     71 #include <net80211/ieee80211_radiotap.h>
     72 
     73 #if NBPFILTER > 0
     74 #include <net/bpf.h>
     75 #endif
     76 
     77 #include <machine/bus.h>
     78 #include <machine/intr.h>
     79 
     80 #include <dev/ic/atwreg.h>
     81 #include <dev/ic/rf3000reg.h>
     82 #include <dev/ic/si4136reg.h>
     83 #include <dev/ic/atwvar.h>
     84 #include <dev/ic/smc93cx6var.h>
     85 
     86 /* XXX TBD open questions
     87  *
     88  *
     89  * When should I set DSSS PAD in reg 0x15 of RF3000? In 1-2Mbps
     90  * modes only, or all modes (5.5-11 Mbps CCK modes, too?) Does the MAC
     91  * handle this for me?
     92  *
     93  */
     94 /* device attachment
     95  *
     96  *    print TOFS[012]
     97  *
     98  * device initialization
     99  *
    100  *    clear ATW_FRCTL_MAXPSP to disable max power saving
    101  *    set ATW_TXBR_ALCUPDATE to enable ALC
    102  *    set TOFS[012]? (hope not)
    103  *    disable rx/tx
    104  *    set ATW_PAR_SWR (software reset)
    105  *    wait for ATW_PAR_SWR clear
    106  *    disable interrupts
    107  *    ack status register
    108  *    enable interrupts
    109  *
    110  * rx/tx initialization
    111  *
    112  *    disable rx/tx w/ ATW_NAR_SR, ATW_NAR_ST
    113  *    allocate and init descriptor rings
    114  *    write ATW_PAR_DSL (descriptor skip length)
    115  *    write descriptor base addrs: ATW_TDBD, ATW_TDBP, write ATW_RDB
    116  *    write ATW_NAR_SQ for one/both transmit descriptor rings
    117  *    write ATW_NAR_SQ for one/both transmit descriptor rings
    118  *    enable rx/tx w/ ATW_NAR_SR, ATW_NAR_ST
    119  *
    120  * rx/tx end
    121  *
    122  *    stop DMA
    123  *    disable rx/tx w/ ATW_NAR_SR, ATW_NAR_ST
    124  *    flush tx w/ ATW_NAR_HF
    125  *
    126  * scan
    127  *
    128  *    initialize rx/tx
    129  *
    130  * BSS join: (re)association response
    131  *
    132  *    set ATW_FRCTL_AID
    133  *
    134  * optimizations ???
    135  *
    136  */
    137 
    138 #define ATW_REFSLAVE	/* slavishly do what the reference driver does */
    139 
    140 #define	VOODOO_DUR_11_ROUNDING		0x01 /* necessary */
    141 #define	VOODOO_DUR_2_4_SPECIALCASE	0x02 /* NOT necessary */
    142 int atw_voodoo = VOODOO_DUR_11_ROUNDING;
    143 
    144 int atw_pseudo_milli = 1;
    145 int atw_magic_delay1 = 100 * 1000;
    146 int atw_magic_delay2 = 100 * 1000;
    147 /* more magic multi-millisecond delays (units: microseconds) */
    148 int atw_nar_delay = 20 * 1000;
    149 int atw_magic_delay4 = 10 * 1000;
    150 int atw_rf_delay1 = 10 * 1000;
    151 int atw_rf_delay2 = 5 * 1000;
    152 int atw_plcphd_delay = 2 * 1000;
    153 int atw_bbp_io_enable_delay = 20 * 1000;
    154 int atw_bbp_io_disable_delay = 2 * 1000;
    155 int atw_writewep_delay = 1000;
    156 int atw_beacon_len_adjust = 4;
    157 int atw_dwelltime = 200;
    158 int atw_xindiv2 = 0;
    159 
    160 #ifdef ATW_DEBUG
    161 int atw_debug = 0;
    162 
    163 #define ATW_DPRINTF(x)	if (atw_debug > 0) printf x
    164 #define ATW_DPRINTF2(x)	if (atw_debug > 1) printf x
    165 #define ATW_DPRINTF3(x)	if (atw_debug > 2) printf x
    166 #define	DPRINTF(sc, x)	if ((sc)->sc_if.if_flags & IFF_DEBUG) printf x
    167 #define	DPRINTF2(sc, x)	if ((sc)->sc_if.if_flags & IFF_DEBUG) ATW_DPRINTF2(x)
    168 #define	DPRINTF3(sc, x)	if ((sc)->sc_if.if_flags & IFF_DEBUG) ATW_DPRINTF3(x)
    169 
    170 static void	atw_dump_pkt(struct ifnet *, struct mbuf *);
    171 static void	atw_print_regs(struct atw_softc *, const char *);
    172 
    173 /* Note well: I never got atw_rf3000_read or atw_si4126_read to work. */
    174 #	ifdef ATW_BBPDEBUG
    175 static void	atw_rf3000_print(struct atw_softc *);
    176 static int	atw_rf3000_read(struct atw_softc *sc, u_int, u_int *);
    177 #	endif /* ATW_BBPDEBUG */
    178 
    179 #	ifdef ATW_SYNDEBUG
    180 static void	atw_si4126_print(struct atw_softc *);
    181 static int	atw_si4126_read(struct atw_softc *, u_int, u_int *);
    182 #	endif /* ATW_SYNDEBUG */
    183 
    184 #else
    185 #define ATW_DPRINTF(x)
    186 #define ATW_DPRINTF2(x)
    187 #define ATW_DPRINTF3(x)
    188 #define	DPRINTF(sc, x)	/* nothing */
    189 #define	DPRINTF2(sc, x)	/* nothing */
    190 #define	DPRINTF3(sc, x)	/* nothing */
    191 #endif
    192 
    193 /* ifnet methods */
    194 int	atw_init(struct ifnet *);
    195 int	atw_ioctl(struct ifnet *, u_long, caddr_t);
    196 void	atw_start(struct ifnet *);
    197 void	atw_stop(struct ifnet *, int);
    198 void	atw_watchdog(struct ifnet *);
    199 
    200 /* Device attachment */
    201 void	atw_attach(struct atw_softc *);
    202 int	atw_detach(struct atw_softc *);
    203 
    204 /* Rx/Tx process */
    205 int	atw_add_rxbuf(struct atw_softc *, int);
    206 void	atw_idle(struct atw_softc *, u_int32_t);
    207 void	atw_rxdrain(struct atw_softc *);
    208 void	atw_txdrain(struct atw_softc *);
    209 
    210 /* Device (de)activation and power state */
    211 void	atw_disable(struct atw_softc *);
    212 int	atw_enable(struct atw_softc *);
    213 void	atw_power(int, void *);
    214 void	atw_reset(struct atw_softc *);
    215 void	atw_shutdown(void *);
    216 
    217 /* Interrupt handlers */
    218 void	atw_linkintr(struct atw_softc *, u_int32_t);
    219 void	atw_rxintr(struct atw_softc *);
    220 void	atw_txintr(struct atw_softc *);
    221 
    222 /* 802.11 state machine */
    223 static int	atw_newstate(struct ieee80211com *, enum ieee80211_state, int);
    224 static void	atw_next_scan(void *);
    225 static void	atw_recv_mgmt(struct ieee80211com *, struct mbuf *,
    226 		              struct ieee80211_node *, int, int, u_int32_t);
    227 static int	atw_tune(struct atw_softc *);
    228 
    229 /* Device initialization */
    230 static void	atw_bbp_io_init(struct atw_softc *);
    231 static void	atw_cfp_init(struct atw_softc *);
    232 static void	atw_cmdr_init(struct atw_softc *);
    233 static void	atw_ifs_init(struct atw_softc *);
    234 static void	atw_nar_init(struct atw_softc *);
    235 static void	atw_response_times_init(struct atw_softc *);
    236 static void	atw_rf_reset(struct atw_softc *);
    237 static void	atw_test1_init(struct atw_softc *);
    238 static void	atw_tofs0_init(struct atw_softc *);
    239 static void	atw_tofs2_init(struct atw_softc *);
    240 static void	atw_txlmt_init(struct atw_softc *);
    241 static void	atw_wcsr_init(struct atw_softc *);
    242 
    243 /* Key management */
    244 static int atw_key_delete(struct ieee80211com *, const struct ieee80211_key *);
    245 static int atw_key_set(struct ieee80211com *, const struct ieee80211_key *,
    246 	const u_int8_t[IEEE80211_ADDR_LEN]);
    247 static void atw_key_update_begin(struct ieee80211com *);
    248 static void atw_key_update_end(struct ieee80211com *);
    249 
    250 /* RAM/ROM utilities */
    251 static void	atw_clear_sram(struct atw_softc *);
    252 static void	atw_write_sram(struct atw_softc *, u_int, u_int8_t *, u_int);
    253 static int	atw_read_srom(struct atw_softc *);
    254 
    255 /* BSS setup */
    256 static void	atw_predict_beacon(struct atw_softc *);
    257 static void	atw_start_beacon(struct atw_softc *, int);
    258 static void	atw_write_bssid(struct atw_softc *);
    259 static void	atw_write_ssid(struct atw_softc *);
    260 static void	atw_write_sup_rates(struct atw_softc *);
    261 static void	atw_write_wep(struct atw_softc *);
    262 
    263 /* Media */
    264 static int	atw_media_change(struct ifnet *);
    265 
    266 static void	atw_filter_setup(struct atw_softc *);
    267 
    268 /* 802.11 utilities */
    269 static uint64_t			atw_get_tsft(struct atw_softc *);
    270 static inline uint32_t	atw_last_even_tsft(uint32_t, uint32_t,
    271 				                   uint32_t);
    272 static struct ieee80211_node	*atw_node_alloc(struct ieee80211_node_table *);
    273 static void			atw_node_free(struct ieee80211_node *);
    274 static void			atw_change_ibss(struct atw_softc *);
    275 
    276 /*
    277  * Tuner/transceiver/modem
    278  */
    279 static void	atw_bbp_io_enable(struct atw_softc *, int);
    280 
    281 /* RFMD RF3000 Baseband Processor */
    282 static int	atw_rf3000_init(struct atw_softc *);
    283 static int	atw_rf3000_tune(struct atw_softc *, u_int);
    284 static int	atw_rf3000_write(struct atw_softc *, u_int, u_int);
    285 
    286 /* Silicon Laboratories Si4126 RF/IF Synthesizer */
    287 static void	atw_si4126_tune(struct atw_softc *, u_int);
    288 static void	atw_si4126_write(struct atw_softc *, u_int, u_int);
    289 
    290 const struct atw_txthresh_tab atw_txthresh_tab_lo[] = ATW_TXTHRESH_TAB_LO_RATE;
    291 const struct atw_txthresh_tab atw_txthresh_tab_hi[] = ATW_TXTHRESH_TAB_HI_RATE;
    292 
    293 const char *atw_tx_state[] = {
    294 	"STOPPED",
    295 	"RUNNING - read descriptor",
    296 	"RUNNING - transmitting",
    297 	"RUNNING - filling fifo",	/* XXX */
    298 	"SUSPENDED",
    299 	"RUNNING -- write descriptor",
    300 	"RUNNING -- write last descriptor",
    301 	"RUNNING - fifo full"
    302 };
    303 
    304 const char *atw_rx_state[] = {
    305 	"STOPPED",
    306 	"RUNNING - read descriptor",
    307 	"RUNNING - check this packet, pre-fetch next",
    308 	"RUNNING - wait for reception",
    309 	"SUSPENDED",
    310 	"RUNNING - write descriptor",
    311 	"RUNNING - flush fifo",
    312 	"RUNNING - fifo drain"
    313 };
    314 
    315 int
    316 atw_activate(struct device *self, enum devact act)
    317 {
    318 	struct atw_softc *sc = (struct atw_softc *)self;
    319 	int rv = 0, s;
    320 
    321 	s = splnet();
    322 	switch (act) {
    323 	case DVACT_ACTIVATE:
    324 		rv = EOPNOTSUPP;
    325 		break;
    326 
    327 	case DVACT_DEACTIVATE:
    328 		if_deactivate(&sc->sc_if);
    329 		break;
    330 	}
    331 	splx(s);
    332 	return rv;
    333 }
    334 
    335 /*
    336  * atw_enable:
    337  *
    338  *	Enable the ADM8211 chip.
    339  */
    340 int
    341 atw_enable(struct atw_softc *sc)
    342 {
    343 
    344 	if (ATW_IS_ENABLED(sc) == 0) {
    345 		if (sc->sc_enable != NULL && (*sc->sc_enable)(sc) != 0) {
    346 			printf("%s: device enable failed\n",
    347 			    sc->sc_dev.dv_xname);
    348 			return (EIO);
    349 		}
    350 		sc->sc_flags |= ATWF_ENABLED;
    351 	}
    352 	return (0);
    353 }
    354 
    355 /*
    356  * atw_disable:
    357  *
    358  *	Disable the ADM8211 chip.
    359  */
    360 void
    361 atw_disable(struct atw_softc *sc)
    362 {
    363 	if (!ATW_IS_ENABLED(sc))
    364 		return;
    365 	if (sc->sc_disable != NULL)
    366 		(*sc->sc_disable)(sc);
    367 	sc->sc_flags &= ~ATWF_ENABLED;
    368 }
    369 
    370 /* Returns -1 on failure. */
    371 static int
    372 atw_read_srom(struct atw_softc *sc)
    373 {
    374 	struct seeprom_descriptor sd;
    375 	uint32_t test0, fail_bits;
    376 
    377 	(void)memset(&sd, 0, sizeof(sd));
    378 
    379 	test0 = ATW_READ(sc, ATW_TEST0);
    380 
    381 	switch (sc->sc_rev) {
    382 	case ATW_REVISION_BA:
    383 	case ATW_REVISION_CA:
    384 		fail_bits = ATW_TEST0_EPNE;
    385 		break;
    386 	default:
    387 		fail_bits = ATW_TEST0_EPNE|ATW_TEST0_EPSNM;
    388 		break;
    389 	}
    390 	if ((test0 & fail_bits) != 0) {
    391 		printf("%s: bad or missing/bad SROM\n", sc->sc_dev.dv_xname);
    392 		return -1;
    393 	}
    394 
    395 	switch (test0 & ATW_TEST0_EPTYP_MASK) {
    396 	case ATW_TEST0_EPTYP_93c66:
    397 		ATW_DPRINTF(("%s: 93c66 SROM\n", sc->sc_dev.dv_xname));
    398 		sc->sc_sromsz = 512;
    399 		sd.sd_chip = C56_66;
    400 		break;
    401 	case ATW_TEST0_EPTYP_93c46:
    402 		ATW_DPRINTF(("%s: 93c46 SROM\n", sc->sc_dev.dv_xname));
    403 		sc->sc_sromsz = 128;
    404 		sd.sd_chip = C46;
    405 		break;
    406 	default:
    407 		printf("%s: unknown SROM type %d\n", sc->sc_dev.dv_xname,
    408 		    MASK_AND_RSHIFT(test0, ATW_TEST0_EPTYP_MASK));
    409 		return -1;
    410 	}
    411 
    412 	sc->sc_srom = malloc(sc->sc_sromsz, M_DEVBUF, M_NOWAIT);
    413 
    414 	if (sc->sc_srom == NULL) {
    415 		printf("%s: unable to allocate SROM buffer\n",
    416 		    sc->sc_dev.dv_xname);
    417 		return -1;
    418 	}
    419 
    420 	(void)memset(sc->sc_srom, 0, sc->sc_sromsz);
    421 
    422 	/* ADM8211 has a single 32-bit register for controlling the
    423 	 * 93cx6 SROM.  Bit SRS enables the serial port. There is no
    424 	 * "ready" bit. The ADM8211 input/output sense is the reverse
    425 	 * of read_seeprom's.
    426 	 */
    427 	sd.sd_tag = sc->sc_st;
    428 	sd.sd_bsh = sc->sc_sh;
    429 	sd.sd_regsize = 4;
    430 	sd.sd_control_offset = ATW_SPR;
    431 	sd.sd_status_offset = ATW_SPR;
    432 	sd.sd_dataout_offset = ATW_SPR;
    433 	sd.sd_CK = ATW_SPR_SCLK;
    434 	sd.sd_CS = ATW_SPR_SCS;
    435 	sd.sd_DI = ATW_SPR_SDO;
    436 	sd.sd_DO = ATW_SPR_SDI;
    437 	sd.sd_MS = ATW_SPR_SRS;
    438 	sd.sd_RDY = 0;
    439 
    440 	if (!read_seeprom(&sd, sc->sc_srom, 0, sc->sc_sromsz/2)) {
    441 		printf("%s: could not read SROM\n", sc->sc_dev.dv_xname);
    442 		free(sc->sc_srom, M_DEVBUF);
    443 		return -1;
    444 	}
    445 #ifdef ATW_DEBUG
    446 	{
    447 		int i;
    448 		ATW_DPRINTF(("\nSerial EEPROM:\n\t"));
    449 		for (i = 0; i < sc->sc_sromsz/2; i = i + 1) {
    450 			if (((i % 8) == 0) && (i != 0)) {
    451 				ATW_DPRINTF(("\n\t"));
    452 			}
    453 			ATW_DPRINTF((" 0x%x", sc->sc_srom[i]));
    454 		}
    455 		ATW_DPRINTF(("\n"));
    456 	}
    457 #endif /* ATW_DEBUG */
    458 	return 0;
    459 }
    460 
    461 #ifdef ATW_DEBUG
    462 static void
    463 atw_print_regs(struct atw_softc *sc, const char *where)
    464 {
    465 #define PRINTREG(sc, reg) \
    466 	ATW_DPRINTF2(("%s: reg[ " #reg " / %03x ] = %08x\n", \
    467 	    sc->sc_dev.dv_xname, reg, ATW_READ(sc, reg)))
    468 
    469 	ATW_DPRINTF2(("%s: %s\n", sc->sc_dev.dv_xname, where));
    470 
    471 	PRINTREG(sc, ATW_PAR);
    472 	PRINTREG(sc, ATW_FRCTL);
    473 	PRINTREG(sc, ATW_TDR);
    474 	PRINTREG(sc, ATW_WTDP);
    475 	PRINTREG(sc, ATW_RDR);
    476 	PRINTREG(sc, ATW_WRDP);
    477 	PRINTREG(sc, ATW_RDB);
    478 	PRINTREG(sc, ATW_CSR3A);
    479 	PRINTREG(sc, ATW_TDBD);
    480 	PRINTREG(sc, ATW_TDBP);
    481 	PRINTREG(sc, ATW_STSR);
    482 	PRINTREG(sc, ATW_CSR5A);
    483 	PRINTREG(sc, ATW_NAR);
    484 	PRINTREG(sc, ATW_CSR6A);
    485 	PRINTREG(sc, ATW_IER);
    486 	PRINTREG(sc, ATW_CSR7A);
    487 	PRINTREG(sc, ATW_LPC);
    488 	PRINTREG(sc, ATW_TEST1);
    489 	PRINTREG(sc, ATW_SPR);
    490 	PRINTREG(sc, ATW_TEST0);
    491 	PRINTREG(sc, ATW_WCSR);
    492 	PRINTREG(sc, ATW_WPDR);
    493 	PRINTREG(sc, ATW_GPTMR);
    494 	PRINTREG(sc, ATW_GPIO);
    495 	PRINTREG(sc, ATW_BBPCTL);
    496 	PRINTREG(sc, ATW_SYNCTL);
    497 	PRINTREG(sc, ATW_PLCPHD);
    498 	PRINTREG(sc, ATW_MMIWADDR);
    499 	PRINTREG(sc, ATW_MMIRADDR1);
    500 	PRINTREG(sc, ATW_MMIRADDR2);
    501 	PRINTREG(sc, ATW_TXBR);
    502 	PRINTREG(sc, ATW_CSR15A);
    503 	PRINTREG(sc, ATW_ALCSTAT);
    504 	PRINTREG(sc, ATW_TOFS2);
    505 	PRINTREG(sc, ATW_CMDR);
    506 	PRINTREG(sc, ATW_PCIC);
    507 	PRINTREG(sc, ATW_PMCSR);
    508 	PRINTREG(sc, ATW_PAR0);
    509 	PRINTREG(sc, ATW_PAR1);
    510 	PRINTREG(sc, ATW_MAR0);
    511 	PRINTREG(sc, ATW_MAR1);
    512 	PRINTREG(sc, ATW_ATIMDA0);
    513 	PRINTREG(sc, ATW_ABDA1);
    514 	PRINTREG(sc, ATW_BSSID0);
    515 	PRINTREG(sc, ATW_TXLMT);
    516 	PRINTREG(sc, ATW_MIBCNT);
    517 	PRINTREG(sc, ATW_BCNT);
    518 	PRINTREG(sc, ATW_TSFTH);
    519 	PRINTREG(sc, ATW_TSC);
    520 	PRINTREG(sc, ATW_SYNRF);
    521 	PRINTREG(sc, ATW_BPLI);
    522 	PRINTREG(sc, ATW_CAP0);
    523 	PRINTREG(sc, ATW_CAP1);
    524 	PRINTREG(sc, ATW_RMD);
    525 	PRINTREG(sc, ATW_CFPP);
    526 	PRINTREG(sc, ATW_TOFS0);
    527 	PRINTREG(sc, ATW_TOFS1);
    528 	PRINTREG(sc, ATW_IFST);
    529 	PRINTREG(sc, ATW_RSPT);
    530 	PRINTREG(sc, ATW_TSFTL);
    531 	PRINTREG(sc, ATW_WEPCTL);
    532 	PRINTREG(sc, ATW_WESK);
    533 	PRINTREG(sc, ATW_WEPCNT);
    534 	PRINTREG(sc, ATW_MACTEST);
    535 	PRINTREG(sc, ATW_FER);
    536 	PRINTREG(sc, ATW_FEMR);
    537 	PRINTREG(sc, ATW_FPSR);
    538 	PRINTREG(sc, ATW_FFER);
    539 #undef PRINTREG
    540 }
    541 #endif /* ATW_DEBUG */
    542 
    543 /*
    544  * Finish attaching an ADMtek ADM8211 MAC.  Called by bus-specific front-end.
    545  */
    546 void
    547 atw_attach(struct atw_softc *sc)
    548 {
    549 	static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
    550 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    551 	};
    552 	struct ieee80211com *ic = &sc->sc_ic;
    553 	struct ifnet *ifp = &sc->sc_if;
    554 	int country_code, error, i, nrate, srom_major;
    555 	u_int32_t reg;
    556 	static const char *type_strings[] = {"Intersil (not supported)",
    557 	    "RFMD", "Marvel (not supported)"};
    558 
    559 	sc->sc_txth = atw_txthresh_tab_lo;
    560 
    561 	SIMPLEQ_INIT(&sc->sc_txfreeq);
    562 	SIMPLEQ_INIT(&sc->sc_txdirtyq);
    563 
    564 #ifdef ATW_DEBUG
    565 	atw_print_regs(sc, "atw_attach");
    566 #endif /* ATW_DEBUG */
    567 
    568 	/*
    569 	 * Allocate the control data structures, and create and load the
    570 	 * DMA map for it.
    571 	 */
    572 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    573 	    sizeof(struct atw_control_data), PAGE_SIZE, 0, &sc->sc_cdseg,
    574 	    1, &sc->sc_cdnseg, 0)) != 0) {
    575 		printf("%s: unable to allocate control data, error = %d\n",
    576 		    sc->sc_dev.dv_xname, error);
    577 		goto fail_0;
    578 	}
    579 
    580 	if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg,
    581 	    sizeof(struct atw_control_data), (caddr_t *)&sc->sc_control_data,
    582 	    BUS_DMA_COHERENT)) != 0) {
    583 		printf("%s: unable to map control data, error = %d\n",
    584 		    sc->sc_dev.dv_xname, error);
    585 		goto fail_1;
    586 	}
    587 
    588 	if ((error = bus_dmamap_create(sc->sc_dmat,
    589 	    sizeof(struct atw_control_data), 1,
    590 	    sizeof(struct atw_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
    591 		printf("%s: unable to create control data DMA map, "
    592 		    "error = %d\n", sc->sc_dev.dv_xname, error);
    593 		goto fail_2;
    594 	}
    595 
    596 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
    597 	    sc->sc_control_data, sizeof(struct atw_control_data), NULL,
    598 	    0)) != 0) {
    599 		printf("%s: unable to load control data DMA map, error = %d\n",
    600 		    sc->sc_dev.dv_xname, error);
    601 		goto fail_3;
    602 	}
    603 
    604 	/*
    605 	 * Create the transmit buffer DMA maps.
    606 	 */
    607 	sc->sc_ntxsegs = ATW_NTXSEGS;
    608 	for (i = 0; i < ATW_TXQUEUELEN; i++) {
    609 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    610 		    sc->sc_ntxsegs, MCLBYTES, 0, 0,
    611 		    &sc->sc_txsoft[i].txs_dmamap)) != 0) {
    612 			printf("%s: unable to create tx DMA map %d, "
    613 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    614 			goto fail_4;
    615 		}
    616 	}
    617 
    618 	/*
    619 	 * Create the receive buffer DMA maps.
    620 	 */
    621 	for (i = 0; i < ATW_NRXDESC; i++) {
    622 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
    623 		    MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
    624 			printf("%s: unable to create rx DMA map %d, "
    625 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    626 			goto fail_5;
    627 		}
    628 	}
    629 	for (i = 0; i < ATW_NRXDESC; i++) {
    630 		sc->sc_rxsoft[i].rxs_mbuf = NULL;
    631 	}
    632 
    633 	switch (sc->sc_rev) {
    634 	case ATW_REVISION_AB:
    635 	case ATW_REVISION_AF:
    636 		sc->sc_sramlen = ATW_SRAM_A_SIZE;
    637 		break;
    638 	case ATW_REVISION_BA:
    639 	case ATW_REVISION_CA:
    640 		sc->sc_sramlen = ATW_SRAM_B_SIZE;
    641 		break;
    642 	}
    643 
    644 	/* Reset the chip to a known state. */
    645 	atw_reset(sc);
    646 
    647 	if (atw_read_srom(sc) == -1)
    648 		return;
    649 
    650 	sc->sc_rftype = MASK_AND_RSHIFT(sc->sc_srom[ATW_SR_CSR20],
    651 	    ATW_SR_RFTYPE_MASK);
    652 
    653 	sc->sc_bbptype = MASK_AND_RSHIFT(sc->sc_srom[ATW_SR_CSR20],
    654 	    ATW_SR_BBPTYPE_MASK);
    655 
    656 	if (sc->sc_rftype > sizeof(type_strings)/sizeof(type_strings[0])) {
    657 		printf("%s: unknown RF\n", sc->sc_dev.dv_xname);
    658 		return;
    659 	}
    660 	if (sc->sc_bbptype > sizeof(type_strings)/sizeof(type_strings[0])) {
    661 		printf("%s: unknown BBP\n", sc->sc_dev.dv_xname);
    662 		return;
    663 	}
    664 
    665 	printf("%s: %s RF, %s BBP", sc->sc_dev.dv_xname,
    666 	    type_strings[sc->sc_rftype], type_strings[sc->sc_bbptype]);
    667 
    668 	/* XXX There exists a Linux driver which seems to use RFType = 0 for
    669 	 * MARVEL. My bug, or theirs?
    670 	 */
    671 
    672 	reg = LSHIFT(sc->sc_rftype, ATW_SYNCTL_RFTYPE_MASK);
    673 
    674 	switch (sc->sc_rftype) {
    675 	case ATW_RFTYPE_INTERSIL:
    676 		reg |= ATW_SYNCTL_CS1;
    677 		break;
    678 	case ATW_RFTYPE_RFMD:
    679 		reg |= ATW_SYNCTL_CS0;
    680 		break;
    681 	case ATW_RFTYPE_MARVEL:
    682 		break;
    683 	}
    684 
    685 	sc->sc_synctl_rd = reg | ATW_SYNCTL_RD;
    686 	sc->sc_synctl_wr = reg | ATW_SYNCTL_WR;
    687 
    688 	reg = LSHIFT(sc->sc_bbptype, ATW_BBPCTL_TYPE_MASK);
    689 
    690 	switch (sc->sc_bbptype) {
    691 	case ATW_BBPTYPE_INTERSIL:
    692 		reg |= ATW_BBPCTL_TWI;
    693 		break;
    694 	case ATW_BBPTYPE_RFMD:
    695 		reg |= ATW_BBPCTL_RF3KADDR_ADDR | ATW_BBPCTL_NEGEDGE_DO |
    696 		    ATW_BBPCTL_CCA_ACTLO;
    697 		break;
    698 	case ATW_BBPTYPE_MARVEL:
    699 		break;
    700 	case ATW_C_BBPTYPE_RFMD:
    701 		printf("%s: ADM8211C MAC/RFMD BBP not supported yet.\n",
    702 		    sc->sc_dev.dv_xname);
    703 		break;
    704 	}
    705 
    706 	sc->sc_bbpctl_wr = reg | ATW_BBPCTL_WR;
    707 	sc->sc_bbpctl_rd = reg | ATW_BBPCTL_RD;
    708 
    709 	/*
    710 	 * From this point forward, the attachment cannot fail.  A failure
    711 	 * before this point releases all resources that may have been
    712 	 * allocated.
    713 	 */
    714 	sc->sc_flags |= ATWF_ATTACHED /* | ATWF_RTSCTS */;
    715 
    716 	ATW_DPRINTF((" SROM MAC %04x%04x%04x",
    717 	    htole16(sc->sc_srom[ATW_SR_MAC00]),
    718 	    htole16(sc->sc_srom[ATW_SR_MAC01]),
    719 	    htole16(sc->sc_srom[ATW_SR_MAC10])));
    720 
    721 	srom_major = MASK_AND_RSHIFT(sc->sc_srom[ATW_SR_FORMAT_VERSION],
    722 	    ATW_SR_MAJOR_MASK);
    723 
    724 	if (srom_major < 2)
    725 		sc->sc_rf3000_options1 = 0;
    726 	else if (sc->sc_rev == ATW_REVISION_BA) {
    727 		sc->sc_rf3000_options1 =
    728 		    MASK_AND_RSHIFT(sc->sc_srom[ATW_SR_CR28_CR03],
    729 		    ATW_SR_CR28_MASK);
    730 	} else
    731 		sc->sc_rf3000_options1 = 0;
    732 
    733 	sc->sc_rf3000_options2 = MASK_AND_RSHIFT(sc->sc_srom[ATW_SR_CTRY_CR29],
    734 	    ATW_SR_CR29_MASK);
    735 
    736 	country_code = MASK_AND_RSHIFT(sc->sc_srom[ATW_SR_CTRY_CR29],
    737 	    ATW_SR_CTRY_MASK);
    738 
    739 #define ADD_CHANNEL(_ic, _chan) do {					\
    740 	_ic->ic_channels[_chan].ic_flags = IEEE80211_CHAN_B;		\
    741 	_ic->ic_channels[_chan].ic_freq =				\
    742 	    ieee80211_ieee2mhz(_chan, _ic->ic_channels[_chan].ic_flags);\
    743 } while (0)
    744 
    745 	/* Find available channels */
    746 	switch (country_code) {
    747 	case COUNTRY_MMK2:	/* 1-14 */
    748 		ADD_CHANNEL(ic, 14);
    749 		/*FALLTHROUGH*/
    750 	case COUNTRY_ETSI:	/* 1-13 */
    751 		for (i = 1; i <= 13; i++)
    752 			ADD_CHANNEL(ic, i);
    753 		break;
    754 	case COUNTRY_FCC:	/* 1-11 */
    755 	case COUNTRY_IC:	/* 1-11 */
    756 		for (i = 1; i <= 11; i++)
    757 			ADD_CHANNEL(ic, i);
    758 		break;
    759 	case COUNTRY_MMK:	/* 14 */
    760 		ADD_CHANNEL(ic, 14);
    761 		break;
    762 	case COUNTRY_FRANCE:	/* 10-13 */
    763 		for (i = 10; i <= 13; i++)
    764 			ADD_CHANNEL(ic, i);
    765 		break;
    766 	default:	/* assume channels 10-11 */
    767 	case COUNTRY_SPAIN:	/* 10-11 */
    768 		for (i = 10; i <= 11; i++)
    769 			ADD_CHANNEL(ic, i);
    770 		break;
    771 	}
    772 
    773 	/* Read the MAC address. */
    774 	reg = ATW_READ(sc, ATW_PAR0);
    775 	ic->ic_myaddr[0] = MASK_AND_RSHIFT(reg, ATW_PAR0_PAB0_MASK);
    776 	ic->ic_myaddr[1] = MASK_AND_RSHIFT(reg, ATW_PAR0_PAB1_MASK);
    777 	ic->ic_myaddr[2] = MASK_AND_RSHIFT(reg, ATW_PAR0_PAB2_MASK);
    778 	ic->ic_myaddr[3] = MASK_AND_RSHIFT(reg, ATW_PAR0_PAB3_MASK);
    779 	reg = ATW_READ(sc, ATW_PAR1);
    780 	ic->ic_myaddr[4] = MASK_AND_RSHIFT(reg, ATW_PAR1_PAB4_MASK);
    781 	ic->ic_myaddr[5] = MASK_AND_RSHIFT(reg, ATW_PAR1_PAB5_MASK);
    782 
    783 	if (IEEE80211_ADDR_EQ(ic->ic_myaddr, empty_macaddr)) {
    784 		printf(" could not get mac address, attach failed\n");
    785 		return;
    786 	}
    787 
    788 	printf(" 802.11 address %s\n", ether_sprintf(ic->ic_myaddr));
    789 
    790 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
    791 	ifp->if_softc = sc;
    792 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST |
    793 	    IFF_NOTRAILERS;
    794 	ifp->if_ioctl = atw_ioctl;
    795 	ifp->if_start = atw_start;
    796 	ifp->if_watchdog = atw_watchdog;
    797 	ifp->if_init = atw_init;
    798 	ifp->if_stop = atw_stop;
    799 	IFQ_SET_READY(&ifp->if_snd);
    800 
    801 	ic->ic_ifp = ifp;
    802 	ic->ic_phytype = IEEE80211_T_DS;
    803 	ic->ic_opmode = IEEE80211_M_STA;
    804 	ic->ic_caps = IEEE80211_C_PMGT | IEEE80211_C_IBSS |
    805 	    IEEE80211_C_HOSTAP | IEEE80211_C_MONITOR;
    806 
    807 	nrate = 0;
    808 	ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[nrate++] = 2;
    809 	ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[nrate++] = 4;
    810 	ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[nrate++] = 11;
    811 	ic->ic_sup_rates[IEEE80211_MODE_11B].rs_rates[nrate++] = 22;
    812 	ic->ic_sup_rates[IEEE80211_MODE_11B].rs_nrates = nrate;
    813 
    814 	/*
    815 	 * Call MI attach routines.
    816 	 */
    817 
    818 	if_attach(ifp);
    819 	ieee80211_ifattach(ic);
    820 
    821 	sc->sc_newstate = ic->ic_newstate;
    822 	ic->ic_newstate = atw_newstate;
    823 
    824 	sc->sc_recv_mgmt = ic->ic_recv_mgmt;
    825 	ic->ic_recv_mgmt = atw_recv_mgmt;
    826 
    827 	sc->sc_node_free = ic->ic_node_free;
    828 	ic->ic_node_free = atw_node_free;
    829 
    830 	sc->sc_node_alloc = ic->ic_node_alloc;
    831 	ic->ic_node_alloc = atw_node_alloc;
    832 
    833 	ic->ic_crypto.cs_key_delete = atw_key_delete;
    834 	ic->ic_crypto.cs_key_set = atw_key_set;
    835 	ic->ic_crypto.cs_key_update_begin = atw_key_update_begin;
    836 	ic->ic_crypto.cs_key_update_end = atw_key_update_end;
    837 
    838 	/* possibly we should fill in our own sc_send_prresp, since
    839 	 * the ADM8211 is probably sending probe responses in ad hoc
    840 	 * mode.
    841 	 */
    842 
    843 	/* complete initialization */
    844 	ieee80211_media_init(ic, atw_media_change, ieee80211_media_status);
    845 	callout_init(&sc->sc_scan_ch);
    846 
    847 #if NBPFILTER > 0
    848 	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
    849 	    sizeof(struct ieee80211_frame) + 64, &sc->sc_radiobpf);
    850 #endif
    851 
    852 	/*
    853 	 * Make sure the interface is shutdown during reboot.
    854 	 */
    855 	sc->sc_sdhook = shutdownhook_establish(atw_shutdown, sc);
    856 	if (sc->sc_sdhook == NULL)
    857 		printf("%s: WARNING: unable to establish shutdown hook\n",
    858 		    sc->sc_dev.dv_xname);
    859 
    860 	/*
    861 	 * Add a suspend hook to make sure we come back up after a
    862 	 * resume.
    863 	 */
    864 	sc->sc_powerhook = powerhook_establish(atw_power, sc);
    865 	if (sc->sc_powerhook == NULL)
    866 		printf("%s: WARNING: unable to establish power hook\n",
    867 		    sc->sc_dev.dv_xname);
    868 
    869 	memset(&sc->sc_rxtapu, 0, sizeof(sc->sc_rxtapu));
    870 	sc->sc_rxtap.ar_ihdr.it_len = sizeof(sc->sc_rxtapu);
    871 	sc->sc_rxtap.ar_ihdr.it_present = ATW_RX_RADIOTAP_PRESENT;
    872 
    873 	memset(&sc->sc_txtapu, 0, sizeof(sc->sc_txtapu));
    874 	sc->sc_txtap.at_ihdr.it_len = sizeof(sc->sc_txtapu);
    875 	sc->sc_txtap.at_ihdr.it_present = ATW_TX_RADIOTAP_PRESENT;
    876 
    877 	ieee80211_announce(ic);
    878 	return;
    879 
    880 	/*
    881 	 * Free any resources we've allocated during the failed attach
    882 	 * attempt.  Do this in reverse order and fall through.
    883 	 */
    884  fail_5:
    885 	for (i = 0; i < ATW_NRXDESC; i++) {
    886 		if (sc->sc_rxsoft[i].rxs_dmamap == NULL)
    887 			continue;
    888 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_rxsoft[i].rxs_dmamap);
    889 	}
    890  fail_4:
    891 	for (i = 0; i < ATW_TXQUEUELEN; i++) {
    892 		if (sc->sc_txsoft[i].txs_dmamap == NULL)
    893 			continue;
    894 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_txsoft[i].txs_dmamap);
    895 	}
    896 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
    897  fail_3:
    898 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
    899  fail_2:
    900 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
    901 	    sizeof(struct atw_control_data));
    902  fail_1:
    903 	bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg);
    904  fail_0:
    905 	return;
    906 }
    907 
    908 static struct ieee80211_node *
    909 atw_node_alloc(struct ieee80211_node_table *nt)
    910 {
    911 	struct atw_softc *sc = (struct atw_softc *)nt->nt_ic->ic_ifp->if_softc;
    912 	struct ieee80211_node *ni = (*sc->sc_node_alloc)(nt);
    913 
    914 	DPRINTF(sc, ("%s: alloc node %p\n", sc->sc_dev.dv_xname, ni));
    915 	return ni;
    916 }
    917 
    918 static void
    919 atw_node_free(struct ieee80211_node *ni)
    920 {
    921 	struct atw_softc *sc = (struct atw_softc *)ni->ni_ic->ic_ifp->if_softc;
    922 
    923 	DPRINTF(sc, ("%s: freeing node %p %s\n", sc->sc_dev.dv_xname, ni,
    924 	    ether_sprintf(ni->ni_bssid)));
    925 	(*sc->sc_node_free)(ni);
    926 }
    927 
    928 
    929 static void
    930 atw_test1_reset(struct atw_softc *sc)
    931 {
    932 	switch (sc->sc_rev) {
    933 	case ATW_REVISION_BA:
    934 		if (1 /* XXX condition on transceiver type */) {
    935 			ATW_SET(sc, ATW_TEST1, ATW_TEST1_TESTMODE_MONITOR);
    936 		}
    937 		break;
    938 	case ATW_REVISION_CA:
    939 		ATW_CLR(sc, ATW_TEST1, ATW_TEST1_TESTMODE_MASK);
    940 		break;
    941 	default:
    942 		break;
    943 	}
    944 }
    945 
    946 /*
    947  * atw_reset:
    948  *
    949  *	Perform a soft reset on the ADM8211.
    950  */
    951 void
    952 atw_reset(struct atw_softc *sc)
    953 {
    954 	int i;
    955 	uint32_t lpc;
    956 
    957 	ATW_WRITE(sc, ATW_NAR, 0x0);
    958 	DELAY(atw_nar_delay);
    959 
    960 	/* Reference driver has a cryptic remark indicating that this might
    961 	 * power-on the chip.  I know that it turns off power-saving....
    962 	 */
    963 	ATW_WRITE(sc, ATW_FRCTL, 0x0);
    964 
    965 	ATW_WRITE(sc, ATW_PAR, ATW_PAR_SWR);
    966 
    967 	for (i = 0; i < 50000 / atw_pseudo_milli; i++) {
    968 		if (ATW_READ(sc, ATW_PAR) == 0)
    969 			break;
    970 		DELAY(atw_pseudo_milli);
    971 	}
    972 
    973 	/* ... and then pause 100ms longer for good measure. */
    974 	DELAY(atw_magic_delay1);
    975 
    976 	DPRINTF2(sc, ("%s: atw_reset %d iterations\n", sc->sc_dev.dv_xname, i));
    977 
    978 	if (ATW_ISSET(sc, ATW_PAR, ATW_PAR_SWR))
    979 		printf("%s: reset failed to complete\n", sc->sc_dev.dv_xname);
    980 
    981 	atw_test1_reset(sc);
    982 	/*
    983 	 * Initialize the PCI Access Register.
    984 	 */
    985 	sc->sc_busmode = ATW_PAR_PBL_8DW;
    986 
    987 	ATW_WRITE(sc, ATW_PAR, sc->sc_busmode);
    988 	DPRINTF(sc, ("%s: ATW_PAR %08x busmode %08x\n", sc->sc_dev.dv_xname,
    989 	    ATW_READ(sc, ATW_PAR), sc->sc_busmode));
    990 
    991 	/* Turn off maximum power saving, etc.
    992 	 *
    993 	 * XXX Following example of reference driver, should I set
    994 	 * an AID of 1?  It didn't seem to help....
    995 	 */
    996 	ATW_WRITE(sc, ATW_FRCTL, 0x0);
    997 
    998 	DELAY(atw_magic_delay2);
    999 
   1000 	/* Recall EEPROM. */
   1001 	ATW_SET(sc, ATW_TEST0, ATW_TEST0_EPRLD);
   1002 
   1003 	DELAY(atw_magic_delay4);
   1004 
   1005 	lpc = ATW_READ(sc, ATW_LPC);
   1006 
   1007 	DPRINTF(sc, ("%s: ATW_LPC %#08x\n", __func__, lpc));
   1008 
   1009 	/* A reset seems to affect the SRAM contents, so put them into
   1010 	 * a known state.
   1011 	 */
   1012 	atw_clear_sram(sc);
   1013 
   1014 	memset(sc->sc_bssid, 0xff, sizeof(sc->sc_bssid));
   1015 }
   1016 
   1017 static void
   1018 atw_clear_sram(struct atw_softc *sc)
   1019 {
   1020 	memset(sc->sc_sram, 0, sizeof(sc->sc_sram));
   1021 	sc->sc_flags &= ~ATWF_WEP_SRAM_VALID;
   1022 	/* XXX not for revision 0x20. */
   1023 	atw_write_sram(sc, 0, sc->sc_sram, sc->sc_sramlen);
   1024 }
   1025 
   1026 /* TBD atw_init
   1027  *
   1028  * set MAC based on ic->ic_bss->myaddr
   1029  * write WEP keys
   1030  * set TX rate
   1031  */
   1032 
   1033 /* Tell the ADM8211 to raise ATW_INTR_LINKOFF if 7 beacon intervals pass
   1034  * without receiving a beacon with the preferred BSSID & SSID.
   1035  * atw_write_bssid & atw_write_ssid set the BSSID & SSID.
   1036  */
   1037 static void
   1038 atw_wcsr_init(struct atw_softc *sc)
   1039 {
   1040 	uint32_t wcsr;
   1041 
   1042 	wcsr = ATW_READ(sc, ATW_WCSR);
   1043 	wcsr &= ~(ATW_WCSR_BLN_MASK|ATW_WCSR_LSOE|ATW_WCSR_MPRE|ATW_WCSR_LSOE);
   1044 	wcsr |= LSHIFT(7, ATW_WCSR_BLN_MASK);
   1045 	ATW_WRITE(sc, ATW_WCSR, wcsr);	/* XXX resets wake-up status bits */
   1046 
   1047 	DPRINTF(sc, ("%s: %s reg[WCSR] = %08x\n",
   1048 	    sc->sc_dev.dv_xname, __func__, ATW_READ(sc, ATW_WCSR)));
   1049 }
   1050 
   1051 /* Turn off power management.  Set Rx store-and-forward mode. */
   1052 static void
   1053 atw_cmdr_init(struct atw_softc *sc)
   1054 {
   1055 	uint32_t cmdr;
   1056 	cmdr = ATW_READ(sc, ATW_CMDR);
   1057 	cmdr &= ~ATW_CMDR_APM;
   1058 	cmdr |= ATW_CMDR_RTE;
   1059 	cmdr &= ~ATW_CMDR_DRT_MASK;
   1060 	cmdr |= ATW_CMDR_DRT_SF;
   1061 
   1062 	ATW_WRITE(sc, ATW_CMDR, cmdr);
   1063 }
   1064 
   1065 static void
   1066 atw_tofs2_init(struct atw_softc *sc)
   1067 {
   1068 	uint32_t tofs2;
   1069 	/* XXX this magic can probably be figured out from the RFMD docs */
   1070 #ifndef ATW_REFSLAVE
   1071 	tofs2 = LSHIFT(4, ATW_TOFS2_PWR1UP_MASK)    | /* 8 ms = 4 * 2 ms */
   1072 	      LSHIFT(13, ATW_TOFS2_PWR0PAPE_MASK) | /* 13 us */
   1073 	      LSHIFT(8, ATW_TOFS2_PWR1PAPE_MASK)  | /* 8 us */
   1074 	      LSHIFT(5, ATW_TOFS2_PWR0TRSW_MASK)  | /* 5 us */
   1075 	      LSHIFT(12, ATW_TOFS2_PWR1TRSW_MASK) | /* 12 us */
   1076 	      LSHIFT(13, ATW_TOFS2_PWR0PE2_MASK)  | /* 13 us */
   1077 	      LSHIFT(4, ATW_TOFS2_PWR1PE2_MASK)   | /* 4 us */
   1078 	      LSHIFT(5, ATW_TOFS2_PWR0TXPE_MASK);  /* 5 us */
   1079 #else
   1080 	/* XXX new magic from reference driver source */
   1081 	tofs2 = LSHIFT(8, ATW_TOFS2_PWR1UP_MASK)    | /* 8 ms = 4 * 2 ms */
   1082 	      LSHIFT(8, ATW_TOFS2_PWR0PAPE_MASK) | /* 13 us */
   1083 	      LSHIFT(1, ATW_TOFS2_PWR1PAPE_MASK)  | /* 8 us */
   1084 	      LSHIFT(5, ATW_TOFS2_PWR0TRSW_MASK)  | /* 5 us */
   1085 	      LSHIFT(12, ATW_TOFS2_PWR1TRSW_MASK) | /* 12 us */
   1086 	      LSHIFT(13, ATW_TOFS2_PWR0PE2_MASK)  | /* 13 us */
   1087 	      LSHIFT(1, ATW_TOFS2_PWR1PE2_MASK)   | /* 4 us */
   1088 	      LSHIFT(8, ATW_TOFS2_PWR0TXPE_MASK);  /* 5 us */
   1089 #endif
   1090 	ATW_WRITE(sc, ATW_TOFS2, tofs2);
   1091 }
   1092 
   1093 static void
   1094 atw_nar_init(struct atw_softc *sc)
   1095 {
   1096 	ATW_WRITE(sc, ATW_NAR, ATW_NAR_SF|ATW_NAR_PB);
   1097 }
   1098 
   1099 static void
   1100 atw_txlmt_init(struct atw_softc *sc)
   1101 {
   1102 	ATW_WRITE(sc, ATW_TXLMT, LSHIFT(512, ATW_TXLMT_MTMLT_MASK) |
   1103 	                         LSHIFT(1, ATW_TXLMT_SRTYLIM_MASK));
   1104 }
   1105 
   1106 static void
   1107 atw_test1_init(struct atw_softc *sc)
   1108 {
   1109 	uint32_t test1;
   1110 
   1111 	test1 = ATW_READ(sc, ATW_TEST1);
   1112 	test1 &= ~(ATW_TEST1_DBGREAD_MASK|ATW_TEST1_CONTROL);
   1113 	/* XXX magic 0x1 */
   1114 	test1 |= LSHIFT(0x1, ATW_TEST1_DBGREAD_MASK) | ATW_TEST1_CONTROL;
   1115 	ATW_WRITE(sc, ATW_TEST1, test1);
   1116 }
   1117 
   1118 static void
   1119 atw_rf_reset(struct atw_softc *sc)
   1120 {
   1121 	/* XXX this resets an Intersil RF front-end? */
   1122 	/* TBD condition on Intersil RFType? */
   1123 	ATW_WRITE(sc, ATW_SYNRF, ATW_SYNRF_INTERSIL_EN);
   1124 	DELAY(atw_rf_delay1);
   1125 	ATW_WRITE(sc, ATW_SYNRF, 0);
   1126 	DELAY(atw_rf_delay2);
   1127 }
   1128 
   1129 /* Set 16 TU max duration for the contention-free period (CFP). */
   1130 static void
   1131 atw_cfp_init(struct atw_softc *sc)
   1132 {
   1133 	uint32_t cfpp;
   1134 
   1135 	cfpp = ATW_READ(sc, ATW_CFPP);
   1136 	cfpp &= ~ATW_CFPP_CFPMD;
   1137 	cfpp |= LSHIFT(16, ATW_CFPP_CFPMD);
   1138 	ATW_WRITE(sc, ATW_CFPP, cfpp);
   1139 }
   1140 
   1141 static void
   1142 atw_tofs0_init(struct atw_softc *sc)
   1143 {
   1144 	/* XXX I guess that the Cardbus clock is 22MHz?
   1145 	 * I am assuming that the role of ATW_TOFS0_USCNT is
   1146 	 * to divide the bus clock to get a 1MHz clock---the datasheet is not
   1147 	 * very clear on this point. It says in the datasheet that it is
   1148 	 * possible for the ADM8211 to accomodate bus speeds between 22MHz
   1149 	 * and 33MHz; maybe this is the way? I see a binary-only driver write
   1150 	 * these values. These values are also the power-on default.
   1151 	 */
   1152 	ATW_WRITE(sc, ATW_TOFS0,
   1153 	    LSHIFT(22, ATW_TOFS0_USCNT_MASK) |
   1154 	    ATW_TOFS0_TUCNT_MASK /* set all bits in TUCNT */);
   1155 }
   1156 
   1157 /* Initialize interframe spacing: 802.11b slot time, SIFS, DIFS, EIFS. */
   1158 static void
   1159 atw_ifs_init(struct atw_softc *sc)
   1160 {
   1161 	uint32_t ifst;
   1162 	/* XXX EIFS=0x64, SIFS=110 are used by the reference driver.
   1163 	 * Go figure.
   1164 	 */
   1165 	ifst = LSHIFT(IEEE80211_DUR_DS_SLOT, ATW_IFST_SLOT_MASK) |
   1166 	      LSHIFT(22 * 5 /* IEEE80211_DUR_DS_SIFS */ /* # of 22MHz cycles */,
   1167 	             ATW_IFST_SIFS_MASK) |
   1168 	      LSHIFT(IEEE80211_DUR_DS_DIFS, ATW_IFST_DIFS_MASK) |
   1169 	      LSHIFT(0x64 /* IEEE80211_DUR_DS_EIFS */, ATW_IFST_EIFS_MASK);
   1170 
   1171 	ATW_WRITE(sc, ATW_IFST, ifst);
   1172 }
   1173 
   1174 static void
   1175 atw_response_times_init(struct atw_softc *sc)
   1176 {
   1177 	/* XXX More magic. Relates to ACK timing?  The datasheet seems to
   1178 	 * indicate that the MAC expects at least SIFS + MIRT microseconds
   1179 	 * to pass after it transmits a frame that requires a response;
   1180 	 * it waits at most SIFS + MART microseconds for the response.
   1181 	 * Surely this is not the ACK timeout?
   1182 	 */
   1183 	ATW_WRITE(sc, ATW_RSPT, LSHIFT(0xffff, ATW_RSPT_MART_MASK) |
   1184 	    LSHIFT(0xff, ATW_RSPT_MIRT_MASK));
   1185 }
   1186 
   1187 /* Set up the MMI read/write addresses for the baseband. The Tx/Rx
   1188  * engines read and write baseband registers after Rx and before
   1189  * Tx, respectively.
   1190  */
   1191 static void
   1192 atw_bbp_io_init(struct atw_softc *sc)
   1193 {
   1194 	uint32_t mmiraddr2;
   1195 
   1196 	/* XXX The reference driver does this, but is it *really*
   1197 	 * necessary?
   1198 	 */
   1199 	switch (sc->sc_rev) {
   1200 	case ATW_REVISION_AB:
   1201 	case ATW_REVISION_AF:
   1202 		mmiraddr2 = 0x0;
   1203 		break;
   1204 	default:
   1205 		mmiraddr2 = ATW_READ(sc, ATW_MMIRADDR2);
   1206 		mmiraddr2 &=
   1207 		    ~(ATW_MMIRADDR2_PROREXT|ATW_MMIRADDR2_PRORLEN_MASK);
   1208 		break;
   1209 	}
   1210 
   1211 	switch (sc->sc_bbptype) {
   1212 	case ATW_BBPTYPE_INTERSIL:
   1213 		ATW_WRITE(sc, ATW_MMIWADDR, ATW_MMIWADDR_INTERSIL);
   1214 		ATW_WRITE(sc, ATW_MMIRADDR1, ATW_MMIRADDR1_INTERSIL);
   1215 		mmiraddr2 |= ATW_MMIRADDR2_INTERSIL;
   1216 		break;
   1217 	case ATW_BBPTYPE_MARVEL:
   1218 		/* TBD find out the Marvel settings. */
   1219 		break;
   1220 	case ATW_BBPTYPE_RFMD:
   1221 	default:
   1222 		ATW_WRITE(sc, ATW_MMIWADDR, ATW_MMIWADDR_RFMD);
   1223 		ATW_WRITE(sc, ATW_MMIRADDR1, ATW_MMIRADDR1_RFMD);
   1224 		mmiraddr2 |= ATW_MMIRADDR2_RFMD;
   1225 		break;
   1226 	}
   1227 	ATW_WRITE(sc, ATW_MMIRADDR2, mmiraddr2);
   1228 	ATW_WRITE(sc, ATW_MACTEST, ATW_MACTEST_MMI_USETXCLK);
   1229 }
   1230 
   1231 /*
   1232  * atw_init:		[ ifnet interface function ]
   1233  *
   1234  *	Initialize the interface.  Must be called at splnet().
   1235  */
   1236 int
   1237 atw_init(struct ifnet *ifp)
   1238 {
   1239 	struct atw_softc *sc = ifp->if_softc;
   1240 	struct ieee80211com *ic = &sc->sc_ic;
   1241 	struct atw_txsoft *txs;
   1242 	struct atw_rxsoft *rxs;
   1243 	int i, error = 0;
   1244 
   1245 	if ((error = atw_enable(sc)) != 0)
   1246 		goto out;
   1247 
   1248 	/*
   1249 	 * Cancel any pending I/O. This also resets.
   1250 	 */
   1251 	atw_stop(ifp, 0);
   1252 
   1253 	DPRINTF(sc, ("%s: channel %d freq %d flags 0x%04x\n",
   1254 	    __func__, ieee80211_chan2ieee(ic, ic->ic_curchan),
   1255 	    ic->ic_curchan->ic_freq, ic->ic_curchan->ic_flags));
   1256 
   1257 	atw_wcsr_init(sc);
   1258 
   1259 	atw_cmdr_init(sc);
   1260 
   1261 	/* Set data rate for PLCP Signal field, 1Mbps = 10 x 100Kb/s.
   1262 	 *
   1263 	 * XXX Set transmit power for ATIM, RTS, Beacon.
   1264 	 */
   1265 	ATW_WRITE(sc, ATW_PLCPHD, LSHIFT(10, ATW_PLCPHD_SIGNAL_MASK) |
   1266 	    LSHIFT(0xb0, ATW_PLCPHD_SERVICE_MASK));
   1267 
   1268 	atw_tofs2_init(sc);
   1269 
   1270 	atw_nar_init(sc);
   1271 
   1272 	atw_txlmt_init(sc);
   1273 
   1274 	atw_test1_init(sc);
   1275 
   1276 	atw_rf_reset(sc);
   1277 
   1278 	atw_cfp_init(sc);
   1279 
   1280 	atw_tofs0_init(sc);
   1281 
   1282 	atw_ifs_init(sc);
   1283 
   1284 	/* XXX Fall asleep after one second of inactivity.
   1285 	 * XXX A frame may only dribble in for 65536us.
   1286 	 */
   1287 	ATW_WRITE(sc, ATW_RMD,
   1288 	    LSHIFT(1, ATW_RMD_PCNT) | LSHIFT(0xffff, ATW_RMD_RMRD_MASK));
   1289 
   1290 	atw_response_times_init(sc);
   1291 
   1292 	atw_bbp_io_init(sc);
   1293 
   1294 	ATW_WRITE(sc, ATW_STSR, 0xffffffff);
   1295 
   1296 	if ((error = atw_rf3000_init(sc)) != 0)
   1297 		goto out;
   1298 
   1299 	ATW_WRITE(sc, ATW_PAR, sc->sc_busmode);
   1300 	DPRINTF(sc, ("%s: ATW_PAR %08x busmode %08x\n", sc->sc_dev.dv_xname,
   1301 	    ATW_READ(sc, ATW_PAR), sc->sc_busmode));
   1302 
   1303 	/*
   1304 	 * Initialize the transmit descriptor ring.
   1305 	 */
   1306 	memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
   1307 	for (i = 0; i < ATW_NTXDESC; i++) {
   1308 		sc->sc_txdescs[i].at_ctl = 0;
   1309 		/* no transmit chaining */
   1310 		sc->sc_txdescs[i].at_flags = 0 /* ATW_TXFLAG_TCH */;
   1311 		sc->sc_txdescs[i].at_buf2 =
   1312 		    htole32(ATW_CDTXADDR(sc, ATW_NEXTTX(i)));
   1313 	}
   1314 	/* use ring mode */
   1315 	sc->sc_txdescs[ATW_NTXDESC - 1].at_flags |= htole32(ATW_TXFLAG_TER);
   1316 	ATW_CDTXSYNC(sc, 0, ATW_NTXDESC,
   1317 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1318 	sc->sc_txfree = ATW_NTXDESC;
   1319 	sc->sc_txnext = 0;
   1320 
   1321 	/*
   1322 	 * Initialize the transmit job descriptors.
   1323 	 */
   1324 	SIMPLEQ_INIT(&sc->sc_txfreeq);
   1325 	SIMPLEQ_INIT(&sc->sc_txdirtyq);
   1326 	for (i = 0; i < ATW_TXQUEUELEN; i++) {
   1327 		txs = &sc->sc_txsoft[i];
   1328 		txs->txs_mbuf = NULL;
   1329 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1330 	}
   1331 
   1332 	/*
   1333 	 * Initialize the receive descriptor and receive job
   1334 	 * descriptor rings.
   1335 	 */
   1336 	for (i = 0; i < ATW_NRXDESC; i++) {
   1337 		rxs = &sc->sc_rxsoft[i];
   1338 		if (rxs->rxs_mbuf == NULL) {
   1339 			if ((error = atw_add_rxbuf(sc, i)) != 0) {
   1340 				printf("%s: unable to allocate or map rx "
   1341 				    "buffer %d, error = %d\n",
   1342 				    sc->sc_dev.dv_xname, i, error);
   1343 				/*
   1344 				 * XXX Should attempt to run with fewer receive
   1345 				 * XXX buffers instead of just failing.
   1346 				 */
   1347 				atw_rxdrain(sc);
   1348 				goto out;
   1349 			}
   1350 		} else
   1351 			ATW_INIT_RXDESC(sc, i);
   1352 	}
   1353 	sc->sc_rxptr = 0;
   1354 
   1355 	/*
   1356 	 * Initialize the interrupt mask and enable interrupts.
   1357 	 */
   1358 	/* normal interrupts */
   1359 	sc->sc_inten =  ATW_INTR_TCI | ATW_INTR_TDU | ATW_INTR_RCI |
   1360 	    ATW_INTR_NISS | ATW_INTR_LINKON | ATW_INTR_BCNTC;
   1361 
   1362 	/* abnormal interrupts */
   1363 	sc->sc_inten |= ATW_INTR_TPS | ATW_INTR_TLT | ATW_INTR_TRT |
   1364 	    ATW_INTR_TUF | ATW_INTR_RDU | ATW_INTR_RPS | ATW_INTR_AISS |
   1365 	    ATW_INTR_FBE | ATW_INTR_LINKOFF | ATW_INTR_TSFTF | ATW_INTR_TSCZ;
   1366 
   1367 	sc->sc_linkint_mask = ATW_INTR_LINKON | ATW_INTR_LINKOFF |
   1368 	    ATW_INTR_BCNTC | ATW_INTR_TSFTF | ATW_INTR_TSCZ;
   1369 	sc->sc_rxint_mask = ATW_INTR_RCI | ATW_INTR_RDU;
   1370 	sc->sc_txint_mask = ATW_INTR_TCI | ATW_INTR_TUF | ATW_INTR_TLT |
   1371 	    ATW_INTR_TRT;
   1372 
   1373 	sc->sc_linkint_mask &= sc->sc_inten;
   1374 	sc->sc_rxint_mask &= sc->sc_inten;
   1375 	sc->sc_txint_mask &= sc->sc_inten;
   1376 
   1377 	ATW_WRITE(sc, ATW_IER, sc->sc_inten);
   1378 	ATW_WRITE(sc, ATW_STSR, 0xffffffff);
   1379 
   1380 	DPRINTF(sc, ("%s: ATW_IER %08x, inten %08x\n",
   1381 	    sc->sc_dev.dv_xname, ATW_READ(sc, ATW_IER), sc->sc_inten));
   1382 
   1383 	/*
   1384 	 * Give the transmit and receive rings to the ADM8211.
   1385 	 */
   1386 	ATW_WRITE(sc, ATW_RDB, ATW_CDRXADDR(sc, sc->sc_rxptr));
   1387 	ATW_WRITE(sc, ATW_TDBD, ATW_CDTXADDR(sc, sc->sc_txnext));
   1388 
   1389 	sc->sc_txthresh = 0;
   1390 	sc->sc_opmode = ATW_NAR_SR | ATW_NAR_ST |
   1391 	    sc->sc_txth[sc->sc_txthresh].txth_opmode;
   1392 
   1393 	/* common 802.11 configuration */
   1394 	ic->ic_flags &= ~IEEE80211_F_IBSSON;
   1395 	switch (ic->ic_opmode) {
   1396 	case IEEE80211_M_STA:
   1397 		break;
   1398 	case IEEE80211_M_AHDEMO: /* XXX */
   1399 	case IEEE80211_M_IBSS:
   1400 		ic->ic_flags |= IEEE80211_F_IBSSON;
   1401 		/*FALLTHROUGH*/
   1402 	case IEEE80211_M_HOSTAP: /* XXX */
   1403 		break;
   1404 	case IEEE80211_M_MONITOR: /* XXX */
   1405 		break;
   1406 	}
   1407 
   1408 	switch (ic->ic_opmode) {
   1409 	case IEEE80211_M_AHDEMO:
   1410 	case IEEE80211_M_HOSTAP:
   1411 #ifndef IEEE80211_NO_HOSTAP
   1412 		ic->ic_bss->ni_intval = ic->ic_lintval;
   1413 		ic->ic_bss->ni_rssi = 0;
   1414 		ic->ic_bss->ni_rstamp = 0;
   1415 #endif /* !IEEE80211_NO_HOSTAP */
   1416 		break;
   1417 	default:					/* XXX */
   1418 		break;
   1419 	}
   1420 
   1421 	sc->sc_wepctl = 0;
   1422 
   1423 	atw_write_ssid(sc);
   1424 	atw_write_sup_rates(sc);
   1425 	atw_write_wep(sc);
   1426 
   1427 	ic->ic_state = IEEE80211_S_INIT;
   1428 
   1429 	/*
   1430 	 * Set the receive filter.  This will start the transmit and
   1431 	 * receive processes.
   1432 	 */
   1433 	atw_filter_setup(sc);
   1434 
   1435 	/*
   1436 	 * Start the receive process.
   1437 	 */
   1438 	ATW_WRITE(sc, ATW_RDR, 0x1);
   1439 
   1440 	/*
   1441 	 * Note that the interface is now running.
   1442 	 */
   1443 	ifp->if_flags |= IFF_RUNNING;
   1444 	ifp->if_flags &= ~IFF_OACTIVE;
   1445 
   1446 	/* send no beacons, yet. */
   1447 	atw_start_beacon(sc, 0);
   1448 
   1449 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
   1450 		error = ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
   1451 	else
   1452 		error = ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
   1453  out:
   1454 	if (error) {
   1455 		ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1456 		sc->sc_tx_timer = 0;
   1457 		ifp->if_timer = 0;
   1458 		printf("%s: interface not running\n", sc->sc_dev.dv_xname);
   1459 	}
   1460 #ifdef ATW_DEBUG
   1461 	atw_print_regs(sc, "end of init");
   1462 #endif /* ATW_DEBUG */
   1463 
   1464 	return (error);
   1465 }
   1466 
   1467 /* enable == 1: host control of RF3000/Si4126 through ATW_SYNCTL.
   1468  *           0: MAC control of RF3000/Si4126.
   1469  *
   1470  * Applies power, or selects RF front-end? Sets reset condition.
   1471  *
   1472  * TBD support non-RFMD BBP, non-SiLabs synth.
   1473  */
   1474 static void
   1475 atw_bbp_io_enable(struct atw_softc *sc, int enable)
   1476 {
   1477 	if (enable) {
   1478 		ATW_WRITE(sc, ATW_SYNRF,
   1479 		    ATW_SYNRF_SELRF|ATW_SYNRF_PE1|ATW_SYNRF_PHYRST);
   1480 		DELAY(atw_bbp_io_enable_delay);
   1481 	} else {
   1482 		ATW_WRITE(sc, ATW_SYNRF, 0);
   1483 		DELAY(atw_bbp_io_disable_delay); /* shorter for some reason */
   1484 	}
   1485 }
   1486 
   1487 static int
   1488 atw_tune(struct atw_softc *sc)
   1489 {
   1490 	int rc;
   1491 	u_int chan;
   1492 	struct ieee80211com *ic = &sc->sc_ic;
   1493 
   1494 	chan = ieee80211_chan2ieee(ic, ic->ic_curchan);
   1495 	if (chan == IEEE80211_CHAN_ANY)
   1496 		panic("%s: chan == IEEE80211_CHAN_ANY\n", __func__);
   1497 
   1498 	if (chan == sc->sc_cur_chan)
   1499 		return 0;
   1500 
   1501 	DPRINTF(sc, ("%s: chan %d -> %d\n", sc->sc_dev.dv_xname,
   1502 	    sc->sc_cur_chan, chan));
   1503 
   1504 	atw_idle(sc, ATW_NAR_SR|ATW_NAR_ST);
   1505 
   1506 	atw_si4126_tune(sc, chan);
   1507 	if ((rc = atw_rf3000_tune(sc, chan)) != 0)
   1508 		printf("%s: failed to tune channel %d\n", sc->sc_dev.dv_xname,
   1509 		    chan);
   1510 
   1511 	ATW_WRITE(sc, ATW_NAR, sc->sc_opmode);
   1512 	DELAY(atw_nar_delay);
   1513 	ATW_WRITE(sc, ATW_RDR, 0x1);
   1514 
   1515 	if (rc == 0)
   1516 		sc->sc_cur_chan = chan;
   1517 
   1518 	return rc;
   1519 }
   1520 
   1521 #ifdef ATW_SYNDEBUG
   1522 static void
   1523 atw_si4126_print(struct atw_softc *sc)
   1524 {
   1525 	struct ifnet *ifp = &sc->sc_if;
   1526 	u_int addr, val;
   1527 
   1528 	if (atw_debug < 3 || (ifp->if_flags & IFF_DEBUG) == 0)
   1529 		return;
   1530 
   1531 	for (addr = 0; addr <= 8; addr++) {
   1532 		printf("%s: synth[%d] = ", sc->sc_dev.dv_xname, addr);
   1533 		if (atw_si4126_read(sc, addr, &val) == 0) {
   1534 			printf("<unknown> (quitting print-out)\n");
   1535 			break;
   1536 		}
   1537 		printf("%05x\n", val);
   1538 	}
   1539 }
   1540 #endif /* ATW_SYNDEBUG */
   1541 
   1542 /* Tune to channel chan by adjusting the Si4126 RF/IF synthesizer.
   1543  *
   1544  * The RF/IF synthesizer produces two reference frequencies for
   1545  * the RF2948B transceiver.  The first frequency the RF2948B requires
   1546  * is two times the so-called "intermediate frequency" (IF). Since
   1547  * a SAW filter on the radio fixes the IF at 374MHz, I program the
   1548  * Si4126 to generate IF LO = 374MHz x 2 = 748MHz.  The second
   1549  * frequency required by the transceiver is the radio frequency
   1550  * (RF). This is a superheterodyne transceiver; for f(chan) the
   1551  * center frequency of the channel we are tuning, RF = f(chan) -
   1552  * IF.
   1553  *
   1554  * XXX I am told by SiLabs that the Si4126 will accept a broader range
   1555  * of XIN than the 2-25MHz mentioned by the datasheet, even *without*
   1556  * XINDIV2 = 1.  I've tried this (it is necessary to double R) and it
   1557  * works, but I have still programmed for XINDIV2 = 1 to be safe.
   1558  */
   1559 static void
   1560 atw_si4126_tune(struct atw_softc *sc, u_int chan)
   1561 {
   1562 	u_int mhz;
   1563 	u_int R;
   1564 	u_int32_t gpio;
   1565 	u_int16_t gain;
   1566 
   1567 #ifdef ATW_SYNDEBUG
   1568 	atw_si4126_print(sc);
   1569 #endif /* ATW_SYNDEBUG */
   1570 
   1571 	if (chan == 14)
   1572 		mhz = 2484;
   1573 	else
   1574 		mhz = 2412 + 5 * (chan - 1);
   1575 
   1576 	/* Tune IF to 748MHz to suit the IF LO input of the
   1577 	 * RF2494B, which is 2 x IF. No need to set an IF divider
   1578          * because an IF in 526MHz - 952MHz is allowed.
   1579 	 *
   1580 	 * XIN is 44.000MHz, so divide it by two to get allowable
   1581 	 * range of 2-25MHz. SiLabs tells me that this is not
   1582 	 * strictly necessary.
   1583 	 */
   1584 
   1585 	if (atw_xindiv2)
   1586 		R = 44;
   1587 	else
   1588 		R = 88;
   1589 
   1590 	/* Power-up RF, IF synthesizers. */
   1591 	atw_si4126_write(sc, SI4126_POWER,
   1592 	    SI4126_POWER_PDIB|SI4126_POWER_PDRB);
   1593 
   1594 	/* set LPWR, too? */
   1595 	atw_si4126_write(sc, SI4126_MAIN,
   1596 	    (atw_xindiv2) ? SI4126_MAIN_XINDIV2 : 0);
   1597 
   1598 	/* Set the phase-locked loop gain.  If RF2 N > 2047, then
   1599 	 * set KP2 to 1.
   1600 	 *
   1601 	 * REFDIF This is different from the reference driver, which
   1602 	 * always sets SI4126_GAIN to 0.
   1603 	 */
   1604 	gain = LSHIFT(((mhz - 374) > 2047) ? 1 : 0, SI4126_GAIN_KP2_MASK);
   1605 
   1606 	atw_si4126_write(sc, SI4126_GAIN, gain);
   1607 
   1608 	/* XIN = 44MHz.
   1609 	 *
   1610 	 * If XINDIV2 = 1, IF = N/(2 * R) * XIN.  I choose N = 1496,
   1611 	 * R = 44 so that 1496/(2 * 44) * 44MHz = 748MHz.
   1612 	 *
   1613 	 * If XINDIV2 = 0, IF = N/R * XIN.  I choose N = 1496, R = 88
   1614 	 * so that 1496/88 * 44MHz = 748MHz.
   1615 	 */
   1616 	atw_si4126_write(sc, SI4126_IFN, 1496);
   1617 
   1618 	atw_si4126_write(sc, SI4126_IFR, R);
   1619 
   1620 #ifndef ATW_REFSLAVE
   1621 	/* Set RF1 arbitrarily. DO NOT configure RF1 after RF2, because
   1622 	 * then RF1 becomes the active RF synthesizer, even on the Si4126,
   1623 	 * which has no RF1!
   1624 	 */
   1625 	atw_si4126_write(sc, SI4126_RF1R, R);
   1626 
   1627 	atw_si4126_write(sc, SI4126_RF1N, mhz - 374);
   1628 #endif
   1629 
   1630 	/* N/R * XIN = RF. XIN = 44MHz. We desire RF = mhz - IF,
   1631 	 * where IF = 374MHz.  Let's divide XIN to 1MHz. So R = 44.
   1632 	 * Now let's multiply it to mhz. So mhz - IF = N.
   1633 	 */
   1634 	atw_si4126_write(sc, SI4126_RF2R, R);
   1635 
   1636 	atw_si4126_write(sc, SI4126_RF2N, mhz - 374);
   1637 
   1638 	/* wait 100us from power-up for RF, IF to settle */
   1639 	DELAY(100);
   1640 
   1641 	gpio = ATW_READ(sc, ATW_GPIO);
   1642 	gpio &= ~(ATW_GPIO_EN_MASK|ATW_GPIO_O_MASK|ATW_GPIO_I_MASK);
   1643 	gpio |= LSHIFT(1, ATW_GPIO_EN_MASK);
   1644 
   1645 	if ((sc->sc_if.if_flags & IFF_LINK1) != 0 && chan != 14) {
   1646 		/* Set a Prism RF front-end to a special mode for channel 14?
   1647 		 *
   1648 		 * Apparently the SMC2635W needs this, although I don't think
   1649 		 * it has a Prism RF.
   1650 		 */
   1651 		gpio |= LSHIFT(1, ATW_GPIO_O_MASK);
   1652 	}
   1653 	ATW_WRITE(sc, ATW_GPIO, gpio);
   1654 
   1655 #ifdef ATW_SYNDEBUG
   1656 	atw_si4126_print(sc);
   1657 #endif /* ATW_SYNDEBUG */
   1658 }
   1659 
   1660 /* Baseline initialization of RF3000 BBP: set CCA mode and enable antenna
   1661  * diversity.
   1662  *
   1663  * !!!
   1664  * !!! Call this w/ Tx/Rx suspended, atw_idle(, ATW_NAR_ST|ATW_NAR_SR).
   1665  * !!!
   1666  */
   1667 static int
   1668 atw_rf3000_init(struct atw_softc *sc)
   1669 {
   1670 	int rc = 0;
   1671 
   1672 	atw_bbp_io_enable(sc, 1);
   1673 
   1674 	/* CCA is acquisition sensitive */
   1675 	rc = atw_rf3000_write(sc, RF3000_CCACTL,
   1676 	    LSHIFT(RF3000_CCACTL_MODE_BOTH, RF3000_CCACTL_MODE_MASK));
   1677 
   1678 	if (rc != 0)
   1679 		goto out;
   1680 
   1681 	/* enable diversity */
   1682 	rc = atw_rf3000_write(sc, RF3000_DIVCTL, RF3000_DIVCTL_ENABLE);
   1683 
   1684 	if (rc != 0)
   1685 		goto out;
   1686 
   1687 	/* sensible setting from a binary-only driver */
   1688 	rc = atw_rf3000_write(sc, RF3000_GAINCTL,
   1689 	    LSHIFT(0x1d, RF3000_GAINCTL_TXVGC_MASK));
   1690 
   1691 	if (rc != 0)
   1692 		goto out;
   1693 
   1694 	/* magic from a binary-only driver */
   1695 	rc = atw_rf3000_write(sc, RF3000_LOGAINCAL,
   1696 	    LSHIFT(0x38, RF3000_LOGAINCAL_CAL_MASK));
   1697 
   1698 	if (rc != 0)
   1699 		goto out;
   1700 
   1701 	rc = atw_rf3000_write(sc, RF3000_HIGAINCAL, RF3000_HIGAINCAL_DSSSPAD);
   1702 
   1703 	if (rc != 0)
   1704 		goto out;
   1705 
   1706 	/* XXX Reference driver remarks that Abocom sets this to 50.
   1707 	 * Meaning 0x50, I think....  50 = 0x32, which would set a bit
   1708 	 * in the "reserved" area of register RF3000_OPTIONS1.
   1709 	 */
   1710 	rc = atw_rf3000_write(sc, RF3000_OPTIONS1, sc->sc_rf3000_options1);
   1711 
   1712 	if (rc != 0)
   1713 		goto out;
   1714 
   1715 	rc = atw_rf3000_write(sc, RF3000_OPTIONS2, sc->sc_rf3000_options2);
   1716 
   1717 	if (rc != 0)
   1718 		goto out;
   1719 
   1720 out:
   1721 	atw_bbp_io_enable(sc, 0);
   1722 	return rc;
   1723 }
   1724 
   1725 #ifdef ATW_BBPDEBUG
   1726 static void
   1727 atw_rf3000_print(struct atw_softc *sc)
   1728 {
   1729 	struct ifnet *ifp = &sc->sc_if;
   1730 	u_int addr, val;
   1731 
   1732 	if (atw_debug < 3 || (ifp->if_flags & IFF_DEBUG) == 0)
   1733 		return;
   1734 
   1735 	for (addr = 0x01; addr <= 0x15; addr++) {
   1736 		printf("%s: bbp[%d] = \n", sc->sc_dev.dv_xname, addr);
   1737 		if (atw_rf3000_read(sc, addr, &val) != 0) {
   1738 			printf("<unknown> (quitting print-out)\n");
   1739 			break;
   1740 		}
   1741 		printf("%08x\n", val);
   1742 	}
   1743 }
   1744 #endif /* ATW_BBPDEBUG */
   1745 
   1746 /* Set the power settings on the BBP for channel `chan'. */
   1747 static int
   1748 atw_rf3000_tune(struct atw_softc *sc, u_int chan)
   1749 {
   1750 	int rc = 0;
   1751 	u_int32_t reg;
   1752 	u_int16_t txpower, lpf_cutoff, lna_gs_thresh;
   1753 
   1754 	txpower = sc->sc_srom[ATW_SR_TXPOWER(chan)];
   1755 	lpf_cutoff = sc->sc_srom[ATW_SR_LPF_CUTOFF(chan)];
   1756 	lna_gs_thresh = sc->sc_srom[ATW_SR_LNA_GS_THRESH(chan)];
   1757 
   1758 	/* odd channels: LSB, even channels: MSB */
   1759 	if (chan % 2 == 1) {
   1760 		txpower &= 0xFF;
   1761 		lpf_cutoff &= 0xFF;
   1762 		lna_gs_thresh &= 0xFF;
   1763 	} else {
   1764 		txpower >>= 8;
   1765 		lpf_cutoff >>= 8;
   1766 		lna_gs_thresh >>= 8;
   1767 	}
   1768 
   1769 #ifdef ATW_BBPDEBUG
   1770 	atw_rf3000_print(sc);
   1771 #endif /* ATW_BBPDEBUG */
   1772 
   1773 	DPRINTF(sc, ("%s: chan %d txpower %02x, lpf_cutoff %02x, "
   1774 	    "lna_gs_thresh %02x\n",
   1775 	    sc->sc_dev.dv_xname, chan, txpower, lpf_cutoff, lna_gs_thresh));
   1776 
   1777 	atw_bbp_io_enable(sc, 1);
   1778 
   1779 	if ((rc = atw_rf3000_write(sc, RF3000_GAINCTL,
   1780 	    LSHIFT(txpower, RF3000_GAINCTL_TXVGC_MASK))) != 0)
   1781 		goto out;
   1782 
   1783 	if ((rc = atw_rf3000_write(sc, RF3000_LOGAINCAL, lpf_cutoff)) != 0)
   1784 		goto out;
   1785 
   1786 	if ((rc = atw_rf3000_write(sc, RF3000_HIGAINCAL, lna_gs_thresh)) != 0)
   1787 		goto out;
   1788 
   1789 	rc = atw_rf3000_write(sc, RF3000_OPTIONS1, 0x0);
   1790 
   1791 	if (rc != 0)
   1792 		goto out;
   1793 
   1794 	rc = atw_rf3000_write(sc, RF3000_OPTIONS2, RF3000_OPTIONS2_LNAGS_DELAY);
   1795 
   1796 	if (rc != 0)
   1797 		goto out;
   1798 
   1799 #ifdef ATW_BBPDEBUG
   1800 	atw_rf3000_print(sc);
   1801 #endif /* ATW_BBPDEBUG */
   1802 
   1803 out:
   1804 	atw_bbp_io_enable(sc, 0);
   1805 
   1806 	/* set beacon, rts, atim transmit power */
   1807 	reg = ATW_READ(sc, ATW_PLCPHD);
   1808 	reg &= ~ATW_PLCPHD_SERVICE_MASK;
   1809 	reg |= LSHIFT(LSHIFT(txpower, RF3000_GAINCTL_TXVGC_MASK),
   1810 	    ATW_PLCPHD_SERVICE_MASK);
   1811 	ATW_WRITE(sc, ATW_PLCPHD, reg);
   1812 	DELAY(atw_plcphd_delay);
   1813 
   1814 	return rc;
   1815 }
   1816 
   1817 /* Write a register on the RF3000 baseband processor using the
   1818  * registers provided by the ADM8211 for this purpose.
   1819  *
   1820  * Return 0 on success.
   1821  */
   1822 static int
   1823 atw_rf3000_write(struct atw_softc *sc, u_int addr, u_int val)
   1824 {
   1825 	u_int32_t reg;
   1826 	int i;
   1827 
   1828 	reg = sc->sc_bbpctl_wr |
   1829 	     LSHIFT(val & 0xff, ATW_BBPCTL_DATA_MASK) |
   1830 	     LSHIFT(addr & 0x7f, ATW_BBPCTL_ADDR_MASK);
   1831 
   1832 	for (i = 20000 / atw_pseudo_milli; --i >= 0; ) {
   1833 		ATW_WRITE(sc, ATW_BBPCTL, reg);
   1834 		DELAY(2 * atw_pseudo_milli);
   1835 		if (ATW_ISSET(sc, ATW_BBPCTL, ATW_BBPCTL_WR) == 0)
   1836 			break;
   1837 	}
   1838 
   1839 	if (i < 0) {
   1840 		printf("%s: BBPCTL still busy\n", sc->sc_dev.dv_xname);
   1841 		return ETIMEDOUT;
   1842 	}
   1843 	return 0;
   1844 }
   1845 
   1846 /* Read a register on the RF3000 baseband processor using the registers
   1847  * the ADM8211 provides for this purpose.
   1848  *
   1849  * The 7-bit register address is addr.  Record the 8-bit data in the register
   1850  * in *val.
   1851  *
   1852  * Return 0 on success.
   1853  *
   1854  * XXX This does not seem to work. The ADM8211 must require more or
   1855  * different magic to read the chip than to write it. Possibly some
   1856  * of the magic I have derived from a binary-only driver concerns
   1857  * the "chip address" (see the RF3000 manual).
   1858  */
   1859 #ifdef ATW_BBPDEBUG
   1860 static int
   1861 atw_rf3000_read(struct atw_softc *sc, u_int addr, u_int *val)
   1862 {
   1863 	u_int32_t reg;
   1864 	int i;
   1865 
   1866 	for (i = 1000; --i >= 0; ) {
   1867 		if (ATW_ISSET(sc, ATW_BBPCTL, ATW_BBPCTL_RD|ATW_BBPCTL_WR) == 0)
   1868 			break;
   1869 		DELAY(100);
   1870 	}
   1871 
   1872 	if (i < 0) {
   1873 		printf("%s: start atw_rf3000_read, BBPCTL busy\n",
   1874 		    sc->sc_dev.dv_xname);
   1875 		return ETIMEDOUT;
   1876 	}
   1877 
   1878 	reg = sc->sc_bbpctl_rd | LSHIFT(addr & 0x7f, ATW_BBPCTL_ADDR_MASK);
   1879 
   1880 	ATW_WRITE(sc, ATW_BBPCTL, reg);
   1881 
   1882 	for (i = 1000; --i >= 0; ) {
   1883 		DELAY(100);
   1884 		if (ATW_ISSET(sc, ATW_BBPCTL, ATW_BBPCTL_RD) == 0)
   1885 			break;
   1886 	}
   1887 
   1888 	ATW_CLR(sc, ATW_BBPCTL, ATW_BBPCTL_RD);
   1889 
   1890 	if (i < 0) {
   1891 		printf("%s: atw_rf3000_read wrote %08x; BBPCTL still busy\n",
   1892 		    sc->sc_dev.dv_xname, reg);
   1893 		return ETIMEDOUT;
   1894 	}
   1895 	if (val != NULL)
   1896 		*val = MASK_AND_RSHIFT(reg, ATW_BBPCTL_DATA_MASK);
   1897 	return 0;
   1898 }
   1899 #endif /* ATW_BBPDEBUG */
   1900 
   1901 /* Write a register on the Si4126 RF/IF synthesizer using the registers
   1902  * provided by the ADM8211 for that purpose.
   1903  *
   1904  * val is 18 bits of data, and val is the 4-bit address of the register.
   1905  *
   1906  * Return 0 on success.
   1907  */
   1908 static void
   1909 atw_si4126_write(struct atw_softc *sc, u_int addr, u_int val)
   1910 {
   1911 	uint32_t bits, mask, reg;
   1912 	const int nbits = 22;
   1913 
   1914 	KASSERT((addr & ~PRESHIFT(SI4126_TWI_ADDR_MASK)) == 0);
   1915 	KASSERT((val & ~PRESHIFT(SI4126_TWI_DATA_MASK)) == 0);
   1916 
   1917 	bits = LSHIFT(val, SI4126_TWI_DATA_MASK) |
   1918 	       LSHIFT(addr, SI4126_TWI_ADDR_MASK);
   1919 
   1920 	reg = ATW_SYNRF_SELSYN;
   1921 	/* reference driver: reset Si4126 serial bus to initial
   1922 	 * conditions?
   1923 	 */
   1924 	ATW_WRITE(sc, ATW_SYNRF, reg | ATW_SYNRF_LEIF);
   1925 	ATW_WRITE(sc, ATW_SYNRF, reg);
   1926 
   1927 	for (mask = BIT(nbits - 1); mask != 0; mask >>= 1) {
   1928 		if ((bits & mask) != 0)
   1929 			reg |= ATW_SYNRF_SYNDATA;
   1930 		else
   1931 			reg &= ~ATW_SYNRF_SYNDATA;
   1932 		ATW_WRITE(sc, ATW_SYNRF, reg);
   1933 		ATW_WRITE(sc, ATW_SYNRF, reg | ATW_SYNRF_SYNCLK);
   1934 		ATW_WRITE(sc, ATW_SYNRF, reg);
   1935 	}
   1936 	ATW_WRITE(sc, ATW_SYNRF, reg | ATW_SYNRF_LEIF);
   1937 	ATW_WRITE(sc, ATW_SYNRF, 0x0);
   1938 }
   1939 
   1940 /* Read 18-bit data from the 4-bit address addr in Si4126
   1941  * RF synthesizer and write the data to *val. Return 0 on success.
   1942  *
   1943  * XXX This does not seem to work. The ADM8211 must require more or
   1944  * different magic to read the chip than to write it.
   1945  */
   1946 #ifdef ATW_SYNDEBUG
   1947 static int
   1948 atw_si4126_read(struct atw_softc *sc, u_int addr, u_int *val)
   1949 {
   1950 	u_int32_t reg;
   1951 	int i;
   1952 
   1953 	KASSERT((addr & ~PRESHIFT(SI4126_TWI_ADDR_MASK)) == 0);
   1954 
   1955 	for (i = 1000; --i >= 0; ) {
   1956 		if (ATW_ISSET(sc, ATW_SYNCTL, ATW_SYNCTL_RD|ATW_SYNCTL_WR) == 0)
   1957 			break;
   1958 		DELAY(100);
   1959 	}
   1960 
   1961 	if (i < 0) {
   1962 		printf("%s: start atw_si4126_read, SYNCTL busy\n",
   1963 		    sc->sc_dev.dv_xname);
   1964 		return ETIMEDOUT;
   1965 	}
   1966 
   1967 	reg = sc->sc_synctl_rd | LSHIFT(addr, ATW_SYNCTL_DATA_MASK);
   1968 
   1969 	ATW_WRITE(sc, ATW_SYNCTL, reg);
   1970 
   1971 	for (i = 1000; --i >= 0; ) {
   1972 		DELAY(100);
   1973 		if (ATW_ISSET(sc, ATW_SYNCTL, ATW_SYNCTL_RD) == 0)
   1974 			break;
   1975 	}
   1976 
   1977 	ATW_CLR(sc, ATW_SYNCTL, ATW_SYNCTL_RD);
   1978 
   1979 	if (i < 0) {
   1980 		printf("%s: atw_si4126_read wrote %#08x, SYNCTL still busy\n",
   1981 		    sc->sc_dev.dv_xname, reg);
   1982 		return ETIMEDOUT;
   1983 	}
   1984 	if (val != NULL)
   1985 		*val = MASK_AND_RSHIFT(ATW_READ(sc, ATW_SYNCTL),
   1986 		                       ATW_SYNCTL_DATA_MASK);
   1987 	return 0;
   1988 }
   1989 #endif /* ATW_SYNDEBUG */
   1990 
   1991 /* XXX is the endianness correct? test. */
   1992 #define	atw_calchash(addr) \
   1993 	(ether_crc32_le((addr), IEEE80211_ADDR_LEN) & BITS(5, 0))
   1994 
   1995 /*
   1996  * atw_filter_setup:
   1997  *
   1998  *	Set the ADM8211's receive filter.
   1999  */
   2000 static void
   2001 atw_filter_setup(struct atw_softc *sc)
   2002 {
   2003 	struct ieee80211com *ic = &sc->sc_ic;
   2004 	struct ethercom *ec = &sc->sc_ec;
   2005 	struct ifnet *ifp = &sc->sc_if;
   2006 	int hash;
   2007 	u_int32_t hashes[2];
   2008 	struct ether_multi *enm;
   2009 	struct ether_multistep step;
   2010 
   2011 	/* According to comments in tlp_al981_filter_setup
   2012 	 * (dev/ic/tulip.c) the ADMtek AL981 does not like for its
   2013 	 * multicast filter to be set while it is running.  Hopefully
   2014 	 * the ADM8211 is not the same!
   2015 	 */
   2016 	if ((ifp->if_flags & IFF_RUNNING) != 0)
   2017 		atw_idle(sc, ATW_NAR_SR);
   2018 
   2019 	sc->sc_opmode &= ~(ATW_NAR_PR|ATW_NAR_MM);
   2020 	ifp->if_flags &= ~IFF_ALLMULTI;
   2021 
   2022 	/* XXX in scan mode, do not filter packets.  Maybe this is
   2023 	 * unnecessary.
   2024 	 */
   2025 	if (ic->ic_state == IEEE80211_S_SCAN ||
   2026 	    (ifp->if_flags & IFF_PROMISC) != 0) {
   2027 		sc->sc_opmode |= ATW_NAR_PR;
   2028 		goto allmulti;
   2029 	}
   2030 
   2031 	hashes[0] = hashes[1] = 0x0;
   2032 
   2033 	/*
   2034 	 * Program the 64-bit multicast hash filter.
   2035 	 */
   2036 	ETHER_FIRST_MULTI(step, ec, enm);
   2037 	while (enm != NULL) {
   2038 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
   2039 		    ETHER_ADDR_LEN) != 0)
   2040 			goto allmulti;
   2041 
   2042 		hash = atw_calchash(enm->enm_addrlo);
   2043 		hashes[hash >> 5] |= 1 << (hash & 0x1f);
   2044 		ETHER_NEXT_MULTI(step, enm);
   2045 		sc->sc_opmode |= ATW_NAR_MM;
   2046 	}
   2047 	ifp->if_flags &= ~IFF_ALLMULTI;
   2048 	goto setit;
   2049 
   2050 allmulti:
   2051 	sc->sc_opmode |= ATW_NAR_MM;
   2052 	ifp->if_flags |= IFF_ALLMULTI;
   2053 	hashes[0] = hashes[1] = 0xffffffff;
   2054 
   2055 setit:
   2056 	ATW_WRITE(sc, ATW_MAR0, hashes[0]);
   2057 	ATW_WRITE(sc, ATW_MAR1, hashes[1]);
   2058 	ATW_WRITE(sc, ATW_NAR, sc->sc_opmode);
   2059 	DELAY(atw_nar_delay);
   2060 
   2061 	DPRINTF(sc, ("%s: ATW_NAR %08x opmode %08x\n", sc->sc_dev.dv_xname,
   2062 	    ATW_READ(sc, ATW_NAR), sc->sc_opmode));
   2063 }
   2064 
   2065 /* Tell the ADM8211 our preferred BSSID. The ADM8211 must match
   2066  * a beacon's BSSID and SSID against the preferred BSSID and SSID
   2067  * before it will raise ATW_INTR_LINKON. When the ADM8211 receives
   2068  * no beacon with the preferred BSSID and SSID in the number of
   2069  * beacon intervals given in ATW_BPLI, then it raises ATW_INTR_LINKOFF.
   2070  */
   2071 static void
   2072 atw_write_bssid(struct atw_softc *sc)
   2073 {
   2074 	struct ieee80211com *ic = &sc->sc_ic;
   2075 	u_int8_t *bssid;
   2076 
   2077 	bssid = ic->ic_bss->ni_bssid;
   2078 
   2079 	ATW_WRITE(sc, ATW_BSSID0,
   2080 	    LSHIFT(bssid[0], ATW_BSSID0_BSSIDB0_MASK) |
   2081 	    LSHIFT(bssid[1], ATW_BSSID0_BSSIDB1_MASK) |
   2082 	    LSHIFT(bssid[2], ATW_BSSID0_BSSIDB2_MASK) |
   2083 	    LSHIFT(bssid[3], ATW_BSSID0_BSSIDB3_MASK));
   2084 
   2085 	ATW_WRITE(sc, ATW_ABDA1,
   2086 	    (ATW_READ(sc, ATW_ABDA1) &
   2087 	    ~(ATW_ABDA1_BSSIDB4_MASK|ATW_ABDA1_BSSIDB5_MASK)) |
   2088 	    LSHIFT(bssid[4], ATW_ABDA1_BSSIDB4_MASK) |
   2089 	    LSHIFT(bssid[5], ATW_ABDA1_BSSIDB5_MASK));
   2090 
   2091 	DPRINTF(sc, ("%s: BSSID %s -> ", sc->sc_dev.dv_xname,
   2092 	    ether_sprintf(sc->sc_bssid)));
   2093 	DPRINTF(sc, ("%s\n", ether_sprintf(bssid)));
   2094 
   2095 	memcpy(sc->sc_bssid, bssid, sizeof(sc->sc_bssid));
   2096 }
   2097 
   2098 /* Write buflen bytes from buf to SRAM starting at the SRAM's ofs'th
   2099  * 16-bit word.
   2100  */
   2101 static void
   2102 atw_write_sram(struct atw_softc *sc, u_int ofs, u_int8_t *buf, u_int buflen)
   2103 {
   2104 	u_int i;
   2105 	u_int8_t *ptr;
   2106 
   2107 	memcpy(&sc->sc_sram[ofs], buf, buflen);
   2108 
   2109 	KASSERT(ofs % 2 == 0 && buflen % 2 == 0);
   2110 
   2111 	KASSERT(buflen + ofs <= sc->sc_sramlen);
   2112 
   2113 	ptr = &sc->sc_sram[ofs];
   2114 
   2115 	for (i = 0; i < buflen; i += 2) {
   2116 		ATW_WRITE(sc, ATW_WEPCTL, ATW_WEPCTL_WR |
   2117 		    LSHIFT((ofs + i) / 2, ATW_WEPCTL_TBLADD_MASK));
   2118 		DELAY(atw_writewep_delay);
   2119 
   2120 		ATW_WRITE(sc, ATW_WESK,
   2121 		    LSHIFT((ptr[i + 1] << 8) | ptr[i], ATW_WESK_DATA_MASK));
   2122 		DELAY(atw_writewep_delay);
   2123 	}
   2124 	ATW_WRITE(sc, ATW_WEPCTL, sc->sc_wepctl); /* restore WEP condition */
   2125 
   2126 	if (sc->sc_if.if_flags & IFF_DEBUG) {
   2127 		int n_octets = 0;
   2128 		printf("%s: wrote %d bytes at 0x%x wepctl 0x%08x\n",
   2129 		    sc->sc_dev.dv_xname, buflen, ofs, sc->sc_wepctl);
   2130 		for (i = 0; i < buflen; i++) {
   2131 			printf(" %02x", ptr[i]);
   2132 			if (++n_octets % 24 == 0)
   2133 				printf("\n");
   2134 		}
   2135 		if (n_octets % 24 != 0)
   2136 			printf("\n");
   2137 	}
   2138 }
   2139 
   2140 static int
   2141 atw_key_delete(struct ieee80211com *ic, const struct ieee80211_key *k)
   2142 {
   2143 	struct atw_softc *sc = ic->ic_ifp->if_softc;
   2144 	u_int keyix = k->wk_keyix;
   2145 
   2146 	DPRINTF(sc, ("%s: delete key %u\n", __func__, keyix));
   2147 
   2148 	if (keyix >= IEEE80211_WEP_NKID)
   2149 		return 0;
   2150 	if (k->wk_keylen != 0)
   2151 		sc->sc_flags &= ~ATWF_WEP_SRAM_VALID;
   2152 
   2153 	return 1;
   2154 }
   2155 
   2156 static int
   2157 atw_key_set(struct ieee80211com *ic, const struct ieee80211_key *k,
   2158 	const u_int8_t mac[IEEE80211_ADDR_LEN])
   2159 {
   2160 	struct atw_softc *sc = ic->ic_ifp->if_softc;
   2161 
   2162 	DPRINTF(sc, ("%s: set key %u\n", __func__, k->wk_keyix));
   2163 
   2164 	if (k->wk_keyix >= IEEE80211_WEP_NKID)
   2165 		return 0;
   2166 
   2167 	sc->sc_flags &= ~ATWF_WEP_SRAM_VALID;
   2168 
   2169 	return 1;
   2170 }
   2171 
   2172 static void
   2173 atw_key_update_begin(struct ieee80211com *ic)
   2174 {
   2175 #ifdef ATW_DEBUG
   2176 	struct ifnet *ifp = ic->ic_ifp;
   2177 	struct atw_softc *sc = ifp->if_softc;
   2178 #endif
   2179 
   2180 	DPRINTF(sc, ("%s:\n", __func__));
   2181 }
   2182 
   2183 static void
   2184 atw_key_update_end(struct ieee80211com *ic)
   2185 {
   2186 	struct ifnet *ifp = ic->ic_ifp;
   2187 	struct atw_softc *sc = ifp->if_softc;
   2188 
   2189 	DPRINTF(sc, ("%s:\n", __func__));
   2190 
   2191 	if ((sc->sc_flags & ATWF_WEP_SRAM_VALID) != 0)
   2192 		return;
   2193 	if (ATW_IS_ENABLED(sc) == 0)
   2194 		return;
   2195 	atw_idle(sc, ATW_NAR_SR | ATW_NAR_ST);
   2196 	atw_write_wep(sc);
   2197 	ATW_WRITE(sc, ATW_NAR, sc->sc_opmode);
   2198 }
   2199 
   2200 /* Write WEP keys from the ieee80211com to the ADM8211's SRAM. */
   2201 static void
   2202 atw_write_wep(struct atw_softc *sc)
   2203 {
   2204 	struct ieee80211com *ic = &sc->sc_ic;
   2205 	/* SRAM shared-key record format: key0 flags key1 ... key12 */
   2206 	u_int8_t buf[IEEE80211_WEP_NKID]
   2207 	            [1 /* key[0] */ + 1 /* flags */ + 12 /* key[1 .. 12] */];
   2208 	u_int32_t reg;
   2209 	int i;
   2210 
   2211 	sc->sc_wepctl = 0;
   2212 	ATW_WRITE(sc, ATW_WEPCTL, sc->sc_wepctl);
   2213 
   2214 	memset(&buf[0][0], 0, sizeof(buf));
   2215 
   2216 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   2217 		if (ic->ic_nw_keys[i].wk_keylen > 5) {
   2218 			buf[i][1] = ATW_WEP_ENABLED | ATW_WEP_104BIT;
   2219 		} else if (ic->ic_nw_keys[i].wk_keylen != 0) {
   2220 			buf[i][1] = ATW_WEP_ENABLED;
   2221 		} else {
   2222 			buf[i][1] = 0;
   2223 			continue;
   2224 		}
   2225 		buf[i][0] = ic->ic_nw_keys[i].wk_key[0];
   2226 		memcpy(&buf[i][2], &ic->ic_nw_keys[i].wk_key[1],
   2227 		    ic->ic_nw_keys[i].wk_keylen - 1);
   2228 	}
   2229 
   2230 	reg = ATW_READ(sc, ATW_MACTEST);
   2231 	reg |= ATW_MACTEST_MMI_USETXCLK | ATW_MACTEST_FORCE_KEYID;
   2232 	reg &= ~ATW_MACTEST_KEYID_MASK;
   2233 	reg |= LSHIFT(ic->ic_def_txkey, ATW_MACTEST_KEYID_MASK);
   2234 	ATW_WRITE(sc, ATW_MACTEST, reg);
   2235 
   2236 	if ((ic->ic_flags & IEEE80211_F_PRIVACY) != 0)
   2237 		sc->sc_wepctl |= ATW_WEPCTL_WEPENABLE;
   2238 
   2239 	switch (sc->sc_rev) {
   2240 	case ATW_REVISION_AB:
   2241 	case ATW_REVISION_AF:
   2242 		/* Bypass WEP on Rx. */
   2243 		sc->sc_wepctl |= ATW_WEPCTL_WEPRXBYP;
   2244 		break;
   2245 	default:
   2246 		break;
   2247 	}
   2248 
   2249 	atw_write_sram(sc, ATW_SRAM_ADDR_SHARED_KEY, (u_int8_t*)&buf[0][0],
   2250 	    sizeof(buf));
   2251 
   2252 	sc->sc_flags |= ATWF_WEP_SRAM_VALID;
   2253 }
   2254 
   2255 static void
   2256 atw_recv_mgmt(struct ieee80211com *ic, struct mbuf *m,
   2257     struct ieee80211_node *ni, int subtype, int rssi, u_int32_t rstamp)
   2258 {
   2259 	struct atw_softc *sc = (struct atw_softc *)ic->ic_ifp->if_softc;
   2260 
   2261 	/* The ADM8211A answers probe requests. TBD ADM8211B/C. */
   2262 	if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_REQ)
   2263 		return;
   2264 
   2265 	(*sc->sc_recv_mgmt)(ic, m, ni, subtype, rssi, rstamp);
   2266 
   2267 	switch (subtype) {
   2268 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
   2269 	case IEEE80211_FC0_SUBTYPE_BEACON:
   2270 		if (ic->ic_opmode == IEEE80211_M_IBSS &&
   2271 		    ic->ic_state == IEEE80211_S_RUN) {
   2272 			if (le64toh(ni->ni_tstamp.tsf) >= atw_get_tsft(sc))
   2273 				(void)ieee80211_ibss_merge(ni);
   2274 		}
   2275 		break;
   2276 	default:
   2277 		break;
   2278 	}
   2279 	return;
   2280 }
   2281 
   2282 /* Write the SSID in the ieee80211com to the SRAM on the ADM8211.
   2283  * In ad hoc mode, the SSID is written to the beacons sent by the
   2284  * ADM8211. In both ad hoc and infrastructure mode, beacons received
   2285  * with matching SSID affect ATW_INTR_LINKON/ATW_INTR_LINKOFF
   2286  * indications.
   2287  */
   2288 static void
   2289 atw_write_ssid(struct atw_softc *sc)
   2290 {
   2291 	struct ieee80211com *ic = &sc->sc_ic;
   2292 	/* 34 bytes are reserved in ADM8211 SRAM for the SSID, but
   2293 	 * it only expects the element length, not its ID.
   2294 	 */
   2295 	u_int8_t buf[roundup(1 /* length */ + IEEE80211_NWID_LEN, 2)];
   2296 
   2297 	memset(buf, 0, sizeof(buf));
   2298 	buf[0] = ic->ic_bss->ni_esslen;
   2299 	memcpy(&buf[1], ic->ic_bss->ni_essid, ic->ic_bss->ni_esslen);
   2300 
   2301 	atw_write_sram(sc, ATW_SRAM_ADDR_SSID, buf,
   2302 	    roundup(1 + ic->ic_bss->ni_esslen, 2));
   2303 }
   2304 
   2305 /* Write the supported rates in the ieee80211com to the SRAM of the ADM8211.
   2306  * In ad hoc mode, the supported rates are written to beacons sent by the
   2307  * ADM8211.
   2308  */
   2309 static void
   2310 atw_write_sup_rates(struct atw_softc *sc)
   2311 {
   2312 	struct ieee80211com *ic = &sc->sc_ic;
   2313 	/* 14 bytes are probably (XXX) reserved in the ADM8211 SRAM for
   2314 	 * supported rates
   2315 	 */
   2316 	u_int8_t buf[roundup(1 /* length */ + IEEE80211_RATE_SIZE, 2)];
   2317 
   2318 	memset(buf, 0, sizeof(buf));
   2319 
   2320 	buf[0] = ic->ic_bss->ni_rates.rs_nrates;
   2321 
   2322 	memcpy(&buf[1], ic->ic_bss->ni_rates.rs_rates,
   2323 	    ic->ic_bss->ni_rates.rs_nrates);
   2324 
   2325 	atw_write_sram(sc, ATW_SRAM_ADDR_SUPRATES, buf, sizeof(buf));
   2326 }
   2327 
   2328 /* Start/stop sending beacons. */
   2329 void
   2330 atw_start_beacon(struct atw_softc *sc, int start)
   2331 {
   2332 	struct ieee80211com *ic = &sc->sc_ic;
   2333 	uint16_t chan;
   2334 	uint32_t bcnt, bpli, cap0, cap1, capinfo;
   2335 	size_t len;
   2336 
   2337 	if (ATW_IS_ENABLED(sc) == 0)
   2338 		return;
   2339 
   2340 	/* start beacons */
   2341 	len = sizeof(struct ieee80211_frame) +
   2342 	    8 /* timestamp */ + 2 /* beacon interval */ +
   2343 	    2 /* capability info */ +
   2344 	    2 + ic->ic_bss->ni_esslen /* SSID element */ +
   2345 	    2 + ic->ic_bss->ni_rates.rs_nrates /* rates element */ +
   2346 	    3 /* DS parameters */ +
   2347 	    IEEE80211_CRC_LEN;
   2348 
   2349 	bcnt = ATW_READ(sc, ATW_BCNT) & ~ATW_BCNT_BCNT_MASK;
   2350 	cap0 = ATW_READ(sc, ATW_CAP0) & ~ATW_CAP0_CHN_MASK;
   2351 	cap1 = ATW_READ(sc, ATW_CAP1) & ~ATW_CAP1_CAPI_MASK;
   2352 
   2353 	ATW_WRITE(sc, ATW_BCNT, bcnt);
   2354 	ATW_WRITE(sc, ATW_CAP1, cap1);
   2355 
   2356 	if (!start)
   2357 		return;
   2358 
   2359 	/* TBD use ni_capinfo */
   2360 
   2361 	capinfo = 0;
   2362 	if (sc->sc_flags & ATWF_SHORT_PREAMBLE)
   2363 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
   2364 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
   2365 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
   2366 
   2367 	switch (ic->ic_opmode) {
   2368 	case IEEE80211_M_IBSS:
   2369 		len += 4; /* IBSS parameters */
   2370 		capinfo |= IEEE80211_CAPINFO_IBSS;
   2371 		break;
   2372 	case IEEE80211_M_HOSTAP:
   2373 		/* XXX 6-byte minimum TIM */
   2374 		len += atw_beacon_len_adjust;
   2375 		capinfo |= IEEE80211_CAPINFO_ESS;
   2376 		break;
   2377 	default:
   2378 		return;
   2379 	}
   2380 
   2381 	/* set listen interval
   2382 	 * XXX do software units agree w/ hardware?
   2383 	 */
   2384 	bpli = LSHIFT(ic->ic_bss->ni_intval, ATW_BPLI_BP_MASK) |
   2385 	    LSHIFT(ic->ic_lintval / ic->ic_bss->ni_intval, ATW_BPLI_LI_MASK);
   2386 
   2387 	chan = ieee80211_chan2ieee(ic, ic->ic_curchan);
   2388 
   2389 	bcnt |= LSHIFT(len, ATW_BCNT_BCNT_MASK);
   2390 	cap0 |= LSHIFT(chan, ATW_CAP0_CHN_MASK);
   2391 	cap1 |= LSHIFT(capinfo, ATW_CAP1_CAPI_MASK);
   2392 
   2393 	ATW_WRITE(sc, ATW_BCNT, bcnt);
   2394 	ATW_WRITE(sc, ATW_BPLI, bpli);
   2395 	ATW_WRITE(sc, ATW_CAP0, cap0);
   2396 	ATW_WRITE(sc, ATW_CAP1, cap1);
   2397 
   2398 	DPRINTF(sc, ("%s: atw_start_beacon reg[ATW_BCNT] = %08x\n",
   2399 	    sc->sc_dev.dv_xname, bcnt));
   2400 
   2401 	DPRINTF(sc, ("%s: atw_start_beacon reg[ATW_CAP1] = %08x\n",
   2402 	    sc->sc_dev.dv_xname, cap1));
   2403 }
   2404 
   2405 /* Return the 32 lsb of the last TSFT divisible by ival. */
   2406 static inline uint32_t
   2407 atw_last_even_tsft(uint32_t tsfth, uint32_t tsftl, uint32_t ival)
   2408 {
   2409 	/* Following the reference driver's lead, I compute
   2410 	 *
   2411 	 *   (uint32_t)((((uint64_t)tsfth << 32) | tsftl) % ival)
   2412 	 *
   2413 	 * without using 64-bit arithmetic, using the following
   2414 	 * relationship:
   2415 	 *
   2416 	 *     (0x100000000 * H + L) % m
   2417 	 *   = ((0x100000000 % m) * H + L) % m
   2418 	 *   = (((0xffffffff + 1) % m) * H + L) % m
   2419 	 *   = ((0xffffffff % m + 1 % m) * H + L) % m
   2420 	 *   = ((0xffffffff % m + 1) * H + L) % m
   2421 	 */
   2422 	return ((0xFFFFFFFF % ival + 1) * tsfth + tsftl) % ival;
   2423 }
   2424 
   2425 static uint64_t
   2426 atw_get_tsft(struct atw_softc *sc)
   2427 {
   2428 	int i;
   2429 	uint32_t tsfth, tsftl;
   2430 	for (i = 0; i < 2; i++) {
   2431 		tsfth = ATW_READ(sc, ATW_TSFTH);
   2432 		tsftl = ATW_READ(sc, ATW_TSFTL);
   2433 		if (ATW_READ(sc, ATW_TSFTH) == tsfth)
   2434 			break;
   2435 	}
   2436 	return ((uint64_t)tsfth << 32) | tsftl;
   2437 }
   2438 
   2439 /* If we've created an IBSS, write the TSF time in the ADM8211 to
   2440  * the ieee80211com.
   2441  *
   2442  * Predict the next target beacon transmission time (TBTT) and
   2443  * write it to the ADM8211.
   2444  */
   2445 static void
   2446 atw_predict_beacon(struct atw_softc *sc)
   2447 {
   2448 #define TBTTOFS 20 /* TU */
   2449 
   2450 	struct ieee80211com *ic = &sc->sc_ic;
   2451 	uint64_t tsft;
   2452 	uint32_t ival, past_even, tbtt, tsfth, tsftl;
   2453 	union {
   2454 		uint64_t	word;
   2455 		uint8_t		tstamp[8];
   2456 	} u;
   2457 
   2458 	if ((ic->ic_opmode == IEEE80211_M_HOSTAP) ||
   2459 	    ((ic->ic_opmode == IEEE80211_M_IBSS) &&
   2460 	     (ic->ic_flags & IEEE80211_F_SIBSS))) {
   2461 		tsft = atw_get_tsft(sc);
   2462 		u.word = htole64(tsft);
   2463 		(void)memcpy(&ic->ic_bss->ni_tstamp, &u.tstamp[0],
   2464 		    sizeof(ic->ic_bss->ni_tstamp));
   2465 	} else
   2466 		tsft = le64toh(ic->ic_bss->ni_tstamp.tsf);
   2467 
   2468 	ival = ic->ic_bss->ni_intval * IEEE80211_DUR_TU;
   2469 
   2470 	tsftl = tsft & 0xFFFFFFFF;
   2471 	tsfth = tsft >> 32;
   2472 
   2473 	/* We sent/received the last beacon `past' microseconds
   2474 	 * after the interval divided the TSF timer.
   2475 	 */
   2476 	past_even = tsftl - atw_last_even_tsft(tsfth, tsftl, ival);
   2477 
   2478 	/* Skip ten beacons so that the TBTT cannot pass before
   2479 	 * we've programmed it.  Ten is an arbitrary number.
   2480 	 */
   2481 	tbtt = past_even + ival * 10;
   2482 
   2483 	ATW_WRITE(sc, ATW_TOFS1,
   2484 	    LSHIFT(1, ATW_TOFS1_TSFTOFSR_MASK) |
   2485 	    LSHIFT(TBTTOFS, ATW_TOFS1_TBTTOFS_MASK) |
   2486 	    LSHIFT(MASK_AND_RSHIFT(tbtt - TBTTOFS * IEEE80211_DUR_TU,
   2487 	        ATW_TBTTPRE_MASK), ATW_TOFS1_TBTTPRE_MASK));
   2488 #undef TBTTOFS
   2489 }
   2490 
   2491 static void
   2492 atw_next_scan(void *arg)
   2493 {
   2494 	struct atw_softc *sc = arg;
   2495 	struct ieee80211com *ic = &sc->sc_ic;
   2496 	int s;
   2497 
   2498 	/* don't call atw_start w/o network interrupts blocked */
   2499 	s = splnet();
   2500 	if (ic->ic_state == IEEE80211_S_SCAN)
   2501 		ieee80211_next_scan(ic);
   2502 	splx(s);
   2503 }
   2504 
   2505 /* Synchronize the hardware state with the software state. */
   2506 static int
   2507 atw_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
   2508 {
   2509 	struct ifnet *ifp = ic->ic_ifp;
   2510 	struct atw_softc *sc = ifp->if_softc;
   2511 	enum ieee80211_state ostate;
   2512 	int error = 0;
   2513 
   2514 	ostate = ic->ic_state;
   2515 	callout_stop(&sc->sc_scan_ch);
   2516 
   2517 	switch (nstate) {
   2518 	case IEEE80211_S_AUTH:
   2519 	case IEEE80211_S_ASSOC:
   2520 		error = atw_tune(sc);
   2521 		break;
   2522 	case IEEE80211_S_INIT:
   2523 		callout_stop(&sc->sc_scan_ch);
   2524 		sc->sc_cur_chan = IEEE80211_CHAN_ANY;
   2525 		atw_start_beacon(sc, 0);
   2526 		break;
   2527 	case IEEE80211_S_SCAN:
   2528 		error = atw_tune(sc);
   2529 		callout_reset(&sc->sc_scan_ch, atw_dwelltime * hz / 1000,
   2530 		    atw_next_scan, sc);
   2531 		break;
   2532 	case IEEE80211_S_RUN:
   2533 		error = atw_tune(sc);
   2534 		atw_write_bssid(sc);
   2535 		atw_write_ssid(sc);
   2536 		atw_write_sup_rates(sc);
   2537 
   2538 		if (ic->ic_opmode == IEEE80211_M_AHDEMO ||
   2539 		    ic->ic_opmode == IEEE80211_M_MONITOR)
   2540 			break;
   2541 
   2542 		/* set listen interval
   2543 		 * XXX do software units agree w/ hardware?
   2544 		 */
   2545 		ATW_WRITE(sc, ATW_BPLI,
   2546 		    LSHIFT(ic->ic_bss->ni_intval, ATW_BPLI_BP_MASK) |
   2547 		    LSHIFT(ic->ic_lintval / ic->ic_bss->ni_intval,
   2548 			   ATW_BPLI_LI_MASK));
   2549 
   2550 		DPRINTF(sc, ("%s: reg[ATW_BPLI] = %08x\n", sc->sc_dev.dv_xname,
   2551 		    ATW_READ(sc, ATW_BPLI)));
   2552 
   2553 		atw_predict_beacon(sc);
   2554 
   2555 		switch (ic->ic_opmode) {
   2556 		case IEEE80211_M_AHDEMO:
   2557 		case IEEE80211_M_HOSTAP:
   2558 		case IEEE80211_M_IBSS:
   2559 			atw_start_beacon(sc, 1);
   2560 			break;
   2561 		case IEEE80211_M_MONITOR:
   2562 		case IEEE80211_M_STA:
   2563 			break;
   2564 		}
   2565 
   2566 		break;
   2567 	}
   2568 	return (error != 0) ? error : (*sc->sc_newstate)(ic, nstate, arg);
   2569 }
   2570 
   2571 /*
   2572  * atw_add_rxbuf:
   2573  *
   2574  *	Add a receive buffer to the indicated descriptor.
   2575  */
   2576 int
   2577 atw_add_rxbuf(struct atw_softc *sc, int idx)
   2578 {
   2579 	struct atw_rxsoft *rxs = &sc->sc_rxsoft[idx];
   2580 	struct mbuf *m;
   2581 	int error;
   2582 
   2583 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   2584 	if (m == NULL)
   2585 		return (ENOBUFS);
   2586 
   2587 	MCLGET(m, M_DONTWAIT);
   2588 	if ((m->m_flags & M_EXT) == 0) {
   2589 		m_freem(m);
   2590 		return (ENOBUFS);
   2591 	}
   2592 
   2593 	if (rxs->rxs_mbuf != NULL)
   2594 		bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   2595 
   2596 	rxs->rxs_mbuf = m;
   2597 
   2598 	error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
   2599 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
   2600 	    BUS_DMA_READ|BUS_DMA_NOWAIT);
   2601 	if (error) {
   2602 		printf("%s: can't load rx DMA map %d, error = %d\n",
   2603 		    sc->sc_dev.dv_xname, idx, error);
   2604 		panic("atw_add_rxbuf");	/* XXX */
   2605 	}
   2606 
   2607 	bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   2608 	    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   2609 
   2610 	ATW_INIT_RXDESC(sc, idx);
   2611 
   2612 	return (0);
   2613 }
   2614 
   2615 /*
   2616  * Release any queued transmit buffers.
   2617  */
   2618 void
   2619 atw_txdrain(struct atw_softc *sc)
   2620 {
   2621 	struct atw_txsoft *txs;
   2622 
   2623 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
   2624 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
   2625 		if (txs->txs_mbuf != NULL) {
   2626 			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   2627 			m_freem(txs->txs_mbuf);
   2628 			txs->txs_mbuf = NULL;
   2629 		}
   2630 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   2631 		sc->sc_txfree += txs->txs_ndescs;
   2632 	}
   2633 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
   2634 	sc->sc_tx_timer = 0;
   2635 }
   2636 
   2637 /*
   2638  * atw_stop:		[ ifnet interface function ]
   2639  *
   2640  *	Stop transmission on the interface.
   2641  */
   2642 void
   2643 atw_stop(struct ifnet *ifp, int disable)
   2644 {
   2645 	struct atw_softc *sc = ifp->if_softc;
   2646 	struct ieee80211com *ic = &sc->sc_ic;
   2647 
   2648 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
   2649 
   2650 	/* Disable interrupts. */
   2651 	ATW_WRITE(sc, ATW_IER, 0);
   2652 
   2653 	/* Stop the transmit and receive processes. */
   2654 	sc->sc_opmode = 0;
   2655 	ATW_WRITE(sc, ATW_NAR, 0);
   2656 	DELAY(atw_nar_delay);
   2657 	ATW_WRITE(sc, ATW_TDBD, 0);
   2658 	ATW_WRITE(sc, ATW_TDBP, 0);
   2659 	ATW_WRITE(sc, ATW_RDB, 0);
   2660 
   2661 	atw_txdrain(sc);
   2662 
   2663 	if (disable) {
   2664 		atw_rxdrain(sc);
   2665 		atw_disable(sc);
   2666 	}
   2667 
   2668 	/*
   2669 	 * Mark the interface down and cancel the watchdog timer.
   2670 	 */
   2671 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   2672 	sc->sc_tx_timer = 0;
   2673 	ifp->if_timer = 0;
   2674 
   2675 	if (!disable)
   2676 		atw_reset(sc);
   2677 }
   2678 
   2679 /*
   2680  * atw_rxdrain:
   2681  *
   2682  *	Drain the receive queue.
   2683  */
   2684 void
   2685 atw_rxdrain(struct atw_softc *sc)
   2686 {
   2687 	struct atw_rxsoft *rxs;
   2688 	int i;
   2689 
   2690 	for (i = 0; i < ATW_NRXDESC; i++) {
   2691 		rxs = &sc->sc_rxsoft[i];
   2692 		if (rxs->rxs_mbuf == NULL)
   2693 			continue;
   2694 		bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   2695 		m_freem(rxs->rxs_mbuf);
   2696 		rxs->rxs_mbuf = NULL;
   2697 	}
   2698 }
   2699 
   2700 /*
   2701  * atw_detach:
   2702  *
   2703  *	Detach an ADM8211 interface.
   2704  */
   2705 int
   2706 atw_detach(struct atw_softc *sc)
   2707 {
   2708 	struct ifnet *ifp = &sc->sc_if;
   2709 	struct atw_rxsoft *rxs;
   2710 	struct atw_txsoft *txs;
   2711 	int i;
   2712 
   2713 	/*
   2714 	 * Succeed now if there isn't any work to do.
   2715 	 */
   2716 	if ((sc->sc_flags & ATWF_ATTACHED) == 0)
   2717 		return (0);
   2718 
   2719 	callout_stop(&sc->sc_scan_ch);
   2720 
   2721 	ieee80211_ifdetach(&sc->sc_ic);
   2722 	if_detach(ifp);
   2723 
   2724 	for (i = 0; i < ATW_NRXDESC; i++) {
   2725 		rxs = &sc->sc_rxsoft[i];
   2726 		if (rxs->rxs_mbuf != NULL) {
   2727 			bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   2728 			m_freem(rxs->rxs_mbuf);
   2729 			rxs->rxs_mbuf = NULL;
   2730 		}
   2731 		bus_dmamap_destroy(sc->sc_dmat, rxs->rxs_dmamap);
   2732 	}
   2733 	for (i = 0; i < ATW_TXQUEUELEN; i++) {
   2734 		txs = &sc->sc_txsoft[i];
   2735 		if (txs->txs_mbuf != NULL) {
   2736 			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   2737 			m_freem(txs->txs_mbuf);
   2738 			txs->txs_mbuf = NULL;
   2739 		}
   2740 		bus_dmamap_destroy(sc->sc_dmat, txs->txs_dmamap);
   2741 	}
   2742 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
   2743 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
   2744 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
   2745 	    sizeof(struct atw_control_data));
   2746 	bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg);
   2747 
   2748 	shutdownhook_disestablish(sc->sc_sdhook);
   2749 	powerhook_disestablish(sc->sc_powerhook);
   2750 
   2751 	if (sc->sc_srom)
   2752 		free(sc->sc_srom, M_DEVBUF);
   2753 
   2754 	return (0);
   2755 }
   2756 
   2757 /* atw_shutdown: make sure the interface is stopped at reboot time. */
   2758 void
   2759 atw_shutdown(void *arg)
   2760 {
   2761 	struct atw_softc *sc = arg;
   2762 
   2763 	atw_stop(&sc->sc_if, 1);
   2764 }
   2765 
   2766 int
   2767 atw_intr(void *arg)
   2768 {
   2769 	struct atw_softc *sc = arg;
   2770 	struct ifnet *ifp = &sc->sc_if;
   2771 	u_int32_t status, rxstatus, txstatus, linkstatus;
   2772 	int handled = 0, txthresh;
   2773 
   2774 #ifdef DEBUG
   2775 	if (ATW_IS_ENABLED(sc) == 0)
   2776 		panic("%s: atw_intr: not enabled", sc->sc_dev.dv_xname);
   2777 #endif
   2778 
   2779 	/*
   2780 	 * If the interface isn't running, the interrupt couldn't
   2781 	 * possibly have come from us.
   2782 	 */
   2783 	if ((ifp->if_flags & IFF_RUNNING) == 0 ||
   2784 	    (sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
   2785 		return (0);
   2786 
   2787 	for (;;) {
   2788 		status = ATW_READ(sc, ATW_STSR);
   2789 
   2790 		if (status)
   2791 			ATW_WRITE(sc, ATW_STSR, status);
   2792 
   2793 #ifdef ATW_DEBUG
   2794 #define PRINTINTR(flag) do { \
   2795 	if ((status & flag) != 0) { \
   2796 		printf("%s" #flag, delim); \
   2797 		delim = ","; \
   2798 	} \
   2799 } while (0)
   2800 
   2801 		if (atw_debug > 1 && status) {
   2802 			const char *delim = "<";
   2803 
   2804 			printf("%s: reg[STSR] = %x",
   2805 			    sc->sc_dev.dv_xname, status);
   2806 
   2807 			PRINTINTR(ATW_INTR_FBE);
   2808 			PRINTINTR(ATW_INTR_LINKOFF);
   2809 			PRINTINTR(ATW_INTR_LINKON);
   2810 			PRINTINTR(ATW_INTR_RCI);
   2811 			PRINTINTR(ATW_INTR_RDU);
   2812 			PRINTINTR(ATW_INTR_REIS);
   2813 			PRINTINTR(ATW_INTR_RPS);
   2814 			PRINTINTR(ATW_INTR_TCI);
   2815 			PRINTINTR(ATW_INTR_TDU);
   2816 			PRINTINTR(ATW_INTR_TLT);
   2817 			PRINTINTR(ATW_INTR_TPS);
   2818 			PRINTINTR(ATW_INTR_TRT);
   2819 			PRINTINTR(ATW_INTR_TUF);
   2820 			PRINTINTR(ATW_INTR_BCNTC);
   2821 			PRINTINTR(ATW_INTR_ATIME);
   2822 			PRINTINTR(ATW_INTR_TBTT);
   2823 			PRINTINTR(ATW_INTR_TSCZ);
   2824 			PRINTINTR(ATW_INTR_TSFTF);
   2825 			printf(">\n");
   2826 		}
   2827 #undef PRINTINTR
   2828 #endif /* ATW_DEBUG */
   2829 
   2830 		if ((status & sc->sc_inten) == 0)
   2831 			break;
   2832 
   2833 		handled = 1;
   2834 
   2835 		rxstatus = status & sc->sc_rxint_mask;
   2836 		txstatus = status & sc->sc_txint_mask;
   2837 		linkstatus = status & sc->sc_linkint_mask;
   2838 
   2839 		if (linkstatus) {
   2840 			atw_linkintr(sc, linkstatus);
   2841 		}
   2842 
   2843 		if (rxstatus) {
   2844 			/* Grab any new packets. */
   2845 			atw_rxintr(sc);
   2846 
   2847 			if (rxstatus & ATW_INTR_RDU) {
   2848 				printf("%s: receive ring overrun\n",
   2849 				    sc->sc_dev.dv_xname);
   2850 				/* Get the receive process going again. */
   2851 				ATW_WRITE(sc, ATW_RDR, 0x1);
   2852 				break;
   2853 			}
   2854 		}
   2855 
   2856 		if (txstatus) {
   2857 			/* Sweep up transmit descriptors. */
   2858 			atw_txintr(sc);
   2859 
   2860 			if (txstatus & ATW_INTR_TLT)
   2861 				DPRINTF(sc, ("%s: tx lifetime exceeded\n",
   2862 				    sc->sc_dev.dv_xname));
   2863 
   2864 			if (txstatus & ATW_INTR_TRT)
   2865 				DPRINTF(sc, ("%s: tx retry limit exceeded\n",
   2866 				    sc->sc_dev.dv_xname));
   2867 
   2868 			/* If Tx under-run, increase our transmit threshold
   2869 			 * if another is available.
   2870 			 */
   2871 			txthresh = sc->sc_txthresh + 1;
   2872 			if ((txstatus & ATW_INTR_TUF) &&
   2873 			    sc->sc_txth[txthresh].txth_name != NULL) {
   2874 				/* Idle the transmit process. */
   2875 				atw_idle(sc, ATW_NAR_ST);
   2876 
   2877 				sc->sc_txthresh = txthresh;
   2878 				sc->sc_opmode &= ~(ATW_NAR_TR_MASK|ATW_NAR_SF);
   2879 				sc->sc_opmode |=
   2880 				    sc->sc_txth[txthresh].txth_opmode;
   2881 				printf("%s: transmit underrun; new "
   2882 				    "threshold: %s\n", sc->sc_dev.dv_xname,
   2883 				    sc->sc_txth[txthresh].txth_name);
   2884 
   2885 				/* Set the new threshold and restart
   2886 				 * the transmit process.
   2887 				 */
   2888 				ATW_WRITE(sc, ATW_NAR, sc->sc_opmode);
   2889 				DELAY(atw_nar_delay);
   2890 				ATW_WRITE(sc, ATW_RDR, 0x1);
   2891 				/* XXX Log every Nth underrun from
   2892 				 * XXX now on?
   2893 				 */
   2894 			}
   2895 		}
   2896 
   2897 		if (status & (ATW_INTR_TPS|ATW_INTR_RPS)) {
   2898 			if (status & ATW_INTR_TPS)
   2899 				printf("%s: transmit process stopped\n",
   2900 				    sc->sc_dev.dv_xname);
   2901 			if (status & ATW_INTR_RPS)
   2902 				printf("%s: receive process stopped\n",
   2903 				    sc->sc_dev.dv_xname);
   2904 			(void)atw_init(ifp);
   2905 			break;
   2906 		}
   2907 
   2908 		if (status & ATW_INTR_FBE) {
   2909 			printf("%s: fatal bus error\n", sc->sc_dev.dv_xname);
   2910 			(void)atw_init(ifp);
   2911 			break;
   2912 		}
   2913 
   2914 		/*
   2915 		 * Not handled:
   2916 		 *
   2917 		 *	Transmit buffer unavailable -- normal
   2918 		 *	condition, nothing to do, really.
   2919 		 *
   2920 		 *	Early receive interrupt -- not available on
   2921 		 *	all chips, we just use RI.  We also only
   2922 		 *	use single-segment receive DMA, so this
   2923 		 *	is mostly useless.
   2924 		 *
   2925 		 *      TBD others
   2926 		 */
   2927 	}
   2928 
   2929 	/* Try to get more packets going. */
   2930 	atw_start(ifp);
   2931 
   2932 	return (handled);
   2933 }
   2934 
   2935 /*
   2936  * atw_idle:
   2937  *
   2938  *	Cause the transmit and/or receive processes to go idle.
   2939  *
   2940  *      XXX It seems that the ADM8211 will not signal the end of the Rx/Tx
   2941  *	process in STSR if I clear SR or ST after the process has already
   2942  *	ceased. Fair enough. But the Rx process status bits in ATW_TEST0
   2943  *      do not seem to be too reliable. Perhaps I have the sense of the
   2944  *	Rx bits switched with the Tx bits?
   2945  */
   2946 void
   2947 atw_idle(struct atw_softc *sc, u_int32_t bits)
   2948 {
   2949 	u_int32_t ackmask = 0, opmode, stsr, test0;
   2950 	int i, s;
   2951 
   2952 	s = splnet();
   2953 
   2954 	opmode = sc->sc_opmode & ~bits;
   2955 
   2956 	if (bits & ATW_NAR_SR)
   2957 		ackmask |= ATW_INTR_RPS;
   2958 
   2959 	if (bits & ATW_NAR_ST) {
   2960 		ackmask |= ATW_INTR_TPS;
   2961 		/* set ATW_NAR_HF to flush TX FIFO. */
   2962 		opmode |= ATW_NAR_HF;
   2963 	}
   2964 
   2965 	ATW_WRITE(sc, ATW_NAR, opmode);
   2966 	DELAY(atw_nar_delay);
   2967 
   2968 	for (i = 0; i < 1000; i++) {
   2969 		stsr = ATW_READ(sc, ATW_STSR);
   2970 		if ((stsr & ackmask) == ackmask)
   2971 			break;
   2972 		DELAY(10);
   2973 	}
   2974 
   2975 	ATW_WRITE(sc, ATW_STSR, stsr & ackmask);
   2976 
   2977 	if ((stsr & ackmask) == ackmask)
   2978 		goto out;
   2979 
   2980 	test0 = ATW_READ(sc, ATW_TEST0);
   2981 
   2982 	if ((bits & ATW_NAR_ST) != 0 && (stsr & ATW_INTR_TPS) == 0 &&
   2983 	    (test0 & ATW_TEST0_TS_MASK) != ATW_TEST0_TS_STOPPED) {
   2984 		printf("%s: transmit process not idle [%s]\n",
   2985 		    sc->sc_dev.dv_xname,
   2986 		    atw_tx_state[MASK_AND_RSHIFT(test0, ATW_TEST0_TS_MASK)]);
   2987 		printf("%s: bits %08x test0 %08x stsr %08x\n",
   2988 		    sc->sc_dev.dv_xname, bits, test0, stsr);
   2989 	}
   2990 
   2991 	if ((bits & ATW_NAR_SR) != 0 && (stsr & ATW_INTR_RPS) == 0 &&
   2992 	    (test0 & ATW_TEST0_RS_MASK) != ATW_TEST0_RS_STOPPED) {
   2993 		DPRINTF2(sc, ("%s: receive process not idle [%s]\n",
   2994 		    sc->sc_dev.dv_xname,
   2995 		    atw_rx_state[MASK_AND_RSHIFT(test0, ATW_TEST0_RS_MASK)]));
   2996 		DPRINTF2(sc, ("%s: bits %08x test0 %08x stsr %08x\n",
   2997 		    sc->sc_dev.dv_xname, bits, test0, stsr));
   2998 	}
   2999 out:
   3000 	if ((bits & ATW_NAR_ST) != 0)
   3001 		atw_txdrain(sc);
   3002 	splx(s);
   3003 	return;
   3004 }
   3005 
   3006 /*
   3007  * atw_linkintr:
   3008  *
   3009  *	Helper; handle link-status interrupts.
   3010  */
   3011 void
   3012 atw_linkintr(struct atw_softc *sc, u_int32_t linkstatus)
   3013 {
   3014 	struct ieee80211com *ic = &sc->sc_ic;
   3015 
   3016 	if (ic->ic_state != IEEE80211_S_RUN)
   3017 		return;
   3018 
   3019 	if (linkstatus & ATW_INTR_LINKON) {
   3020 		DPRINTF(sc, ("%s: link on\n", sc->sc_dev.dv_xname));
   3021 		sc->sc_rescan_timer = 0;
   3022 	} else if (linkstatus & ATW_INTR_LINKOFF) {
   3023 		DPRINTF(sc, ("%s: link off\n", sc->sc_dev.dv_xname));
   3024 		if (ic->ic_opmode != IEEE80211_M_STA)
   3025 			return;
   3026 		sc->sc_rescan_timer = 3;
   3027 		sc->sc_if.if_timer = 1;
   3028 	}
   3029 }
   3030 
   3031 static inline int
   3032 atw_hw_decrypted(struct atw_softc *sc, struct ieee80211_frame_min *wh)
   3033 {
   3034 	if ((sc->sc_ic.ic_flags & IEEE80211_F_PRIVACY) == 0)
   3035 		return 0;
   3036 	if ((wh->i_fc[1] & IEEE80211_FC1_WEP) == 0)
   3037 		return 0;
   3038 	return (sc->sc_wepctl & ATW_WEPCTL_WEPRXBYP) == 0;
   3039 }
   3040 
   3041 /*
   3042  * atw_rxintr:
   3043  *
   3044  *	Helper; handle receive interrupts.
   3045  */
   3046 void
   3047 atw_rxintr(struct atw_softc *sc)
   3048 {
   3049 	static int rate_tbl[] = {2, 4, 11, 22, 44};
   3050 	struct ieee80211com *ic = &sc->sc_ic;
   3051 	struct ieee80211_node *ni;
   3052 	struct ieee80211_frame_min *wh;
   3053 	struct ifnet *ifp = &sc->sc_if;
   3054 	struct atw_rxsoft *rxs;
   3055 	struct mbuf *m;
   3056 	u_int32_t rxstat;
   3057 	int i, len, rate, rate0;
   3058 	u_int32_t rssi, rssi0;
   3059 
   3060 	for (i = sc->sc_rxptr;; i = ATW_NEXTRX(i)) {
   3061 		rxs = &sc->sc_rxsoft[i];
   3062 
   3063 		ATW_CDRXSYNC(sc, i, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   3064 
   3065 		rxstat = le32toh(sc->sc_rxdescs[i].ar_stat);
   3066 		rssi0 = le32toh(sc->sc_rxdescs[i].ar_rssi);
   3067 		rate0 = MASK_AND_RSHIFT(rxstat, ATW_RXSTAT_RXDR_MASK);
   3068 
   3069 		if (rxstat & ATW_RXSTAT_OWN)
   3070 			break; /* We have processed all receive buffers. */
   3071 
   3072 		DPRINTF3(sc,
   3073 		    ("%s: rx stat %08x rssi0 %08x buf1 %08x buf2 %08x\n",
   3074 		    sc->sc_dev.dv_xname,
   3075 		    rxstat, rssi0,
   3076 		    le32toh(sc->sc_rxdescs[i].ar_buf1),
   3077 		    le32toh(sc->sc_rxdescs[i].ar_buf2)));
   3078 
   3079 		/*
   3080 		 * Make sure the packet fits in one buffer.  This should
   3081 		 * always be the case.
   3082 		 */
   3083 		if ((rxstat & (ATW_RXSTAT_FS|ATW_RXSTAT_LS)) !=
   3084 		    (ATW_RXSTAT_FS|ATW_RXSTAT_LS)) {
   3085 			printf("%s: incoming packet spilled, resetting\n",
   3086 			    sc->sc_dev.dv_xname);
   3087 			(void)atw_init(ifp);
   3088 			return;
   3089 		}
   3090 
   3091 		/*
   3092 		 * If an error occurred, update stats, clear the status
   3093 		 * word, and leave the packet buffer in place.  It will
   3094 		 * simply be reused the next time the ring comes around.
   3095 	 	 * If 802.1Q VLAN MTU is enabled, ignore the Frame Too Long
   3096 		 * error.
   3097 		 */
   3098 
   3099 		if ((rxstat & ATW_RXSTAT_ES) != 0 &&
   3100 		    ((sc->sc_ec.ec_capenable & ETHERCAP_VLAN_MTU) == 0 ||
   3101 		     (rxstat & (ATW_RXSTAT_DE | ATW_RXSTAT_SFDE |
   3102 		                ATW_RXSTAT_SIGE | ATW_RXSTAT_CRC16E |
   3103 				ATW_RXSTAT_RXTOE | ATW_RXSTAT_CRC32E |
   3104 				ATW_RXSTAT_ICVE)) != 0)) {
   3105 #define	PRINTERR(bit, str)						\
   3106 			if (rxstat & (bit))				\
   3107 				printf("%s: receive error: %s\n",	\
   3108 				    sc->sc_dev.dv_xname, str)
   3109 			ifp->if_ierrors++;
   3110 			PRINTERR(ATW_RXSTAT_DE, "descriptor error");
   3111 			PRINTERR(ATW_RXSTAT_SFDE, "PLCP SFD error");
   3112 			PRINTERR(ATW_RXSTAT_SIGE, "PLCP signal error");
   3113 			PRINTERR(ATW_RXSTAT_CRC16E, "PLCP CRC16 error");
   3114 			PRINTERR(ATW_RXSTAT_RXTOE, "time-out");
   3115 			PRINTERR(ATW_RXSTAT_CRC32E, "FCS error");
   3116 			PRINTERR(ATW_RXSTAT_ICVE, "WEP ICV error");
   3117 #undef PRINTERR
   3118 			ATW_INIT_RXDESC(sc, i);
   3119 			continue;
   3120 		}
   3121 
   3122 		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   3123 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   3124 
   3125 		/*
   3126 		 * No errors; receive the packet.  Note the ADM8211
   3127 		 * includes the CRC in promiscuous mode.
   3128 		 */
   3129 		len = MASK_AND_RSHIFT(rxstat, ATW_RXSTAT_FL_MASK);
   3130 
   3131 		/*
   3132 		 * Allocate a new mbuf cluster.  If that fails, we are
   3133 		 * out of memory, and must drop the packet and recycle
   3134 		 * the buffer that's already attached to this descriptor.
   3135 		 */
   3136 		m = rxs->rxs_mbuf;
   3137 		if (atw_add_rxbuf(sc, i) != 0) {
   3138 			ifp->if_ierrors++;
   3139 			ATW_INIT_RXDESC(sc, i);
   3140 			bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   3141 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   3142 			continue;
   3143 		}
   3144 
   3145 		ifp->if_ipackets++;
   3146 		if (sc->sc_opmode & ATW_NAR_PR)
   3147 			len -= IEEE80211_CRC_LEN;
   3148 		m->m_pkthdr.rcvif = ifp;
   3149 		m->m_pkthdr.len = m->m_len = MIN(m->m_ext.ext_size, len);
   3150 
   3151 		if (rate0 >= sizeof(rate_tbl) / sizeof(rate_tbl[0]))
   3152 			rate = 0;
   3153 		else
   3154 			rate = rate_tbl[rate0];
   3155 
   3156 		/* The RSSI comes straight from a register in the
   3157 		 * baseband processor.  I know that for the RF3000,
   3158 		 * the RSSI register also contains the antenna-selection
   3159 		 * bits.  Mask those off.
   3160 		 *
   3161 		 * TBD Treat other basebands.
   3162 		 */
   3163 		if (sc->sc_bbptype == ATW_BBPTYPE_RFMD)
   3164 			rssi = rssi0 & RF3000_RSSI_MASK;
   3165 		else
   3166 			rssi = rssi0;
   3167 
   3168  #if NBPFILTER > 0
   3169 		/* Pass this up to any BPF listeners. */
   3170 		if (sc->sc_radiobpf != NULL) {
   3171 			struct atw_rx_radiotap_header *tap = &sc->sc_rxtap;
   3172 
   3173 			tap->ar_rate = rate;
   3174 			tap->ar_chan_freq = ic->ic_curchan->ic_freq;
   3175 			tap->ar_chan_flags = ic->ic_curchan->ic_flags;
   3176 
   3177 			/* TBD verify units are dB */
   3178 			tap->ar_antsignal = (int)rssi;
   3179 			/* TBD tap->ar_flags */
   3180 
   3181 			bpf_mtap2(sc->sc_radiobpf, (caddr_t)tap,
   3182 			    tap->ar_ihdr.it_len, m);
   3183  		}
   3184  #endif /* NPBFILTER > 0 */
   3185 
   3186 		wh = mtod(m, struct ieee80211_frame_min *);
   3187 		ni = ieee80211_find_rxnode(ic, wh);
   3188 		if (atw_hw_decrypted(sc, wh)) {
   3189 			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
   3190 			DPRINTF(sc, ("%s: hw decrypted\n", __func__));
   3191 		}
   3192 		ieee80211_input(ic, m, ni, (int)rssi, 0);
   3193 		ieee80211_free_node(ni);
   3194 	}
   3195 
   3196 	/* Update the receive pointer. */
   3197 	sc->sc_rxptr = i;
   3198 }
   3199 
   3200 /*
   3201  * atw_txintr:
   3202  *
   3203  *	Helper; handle transmit interrupts.
   3204  */
   3205 void
   3206 atw_txintr(struct atw_softc *sc)
   3207 {
   3208 #define TXSTAT_ERRMASK (ATW_TXSTAT_TUF | ATW_TXSTAT_TLT | ATW_TXSTAT_TRT | \
   3209     ATW_TXSTAT_TRO | ATW_TXSTAT_SOFBR)
   3210 #define TXSTAT_FMT "\20\31ATW_TXSTAT_SOFBR\32ATW_TXSTAT_TRO\33ATW_TXSTAT_TUF" \
   3211     "\34ATW_TXSTAT_TRT\35ATW_TXSTAT_TLT"
   3212 
   3213 	static char txstat_buf[sizeof("ffffffff<>" TXSTAT_FMT)];
   3214 	struct ifnet *ifp = &sc->sc_if;
   3215 	struct atw_txsoft *txs;
   3216 	u_int32_t txstat;
   3217 
   3218 	DPRINTF3(sc, ("%s: atw_txintr: sc_flags 0x%08x\n",
   3219 	    sc->sc_dev.dv_xname, sc->sc_flags));
   3220 
   3221 	/*
   3222 	 * Go through our Tx list and free mbufs for those
   3223 	 * frames that have been transmitted.
   3224 	 */
   3225 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
   3226 		ATW_CDTXSYNC(sc, txs->txs_lastdesc, 1,
   3227 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   3228 
   3229 #ifdef ATW_DEBUG
   3230 		if ((ifp->if_flags & IFF_DEBUG) != 0 && atw_debug > 2) {
   3231 			int i;
   3232 			printf("    txsoft %p transmit chain:\n", txs);
   3233 			ATW_CDTXSYNC(sc, txs->txs_firstdesc,
   3234 			    txs->txs_ndescs - 1,
   3235 			    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   3236 			for (i = txs->txs_firstdesc;; i = ATW_NEXTTX(i)) {
   3237 				printf("     descriptor %d:\n", i);
   3238 				printf("       at_status:   0x%08x\n",
   3239 				    le32toh(sc->sc_txdescs[i].at_stat));
   3240 				printf("       at_flags:      0x%08x\n",
   3241 				    le32toh(sc->sc_txdescs[i].at_flags));
   3242 				printf("       at_buf1: 0x%08x\n",
   3243 				    le32toh(sc->sc_txdescs[i].at_buf1));
   3244 				printf("       at_buf2: 0x%08x\n",
   3245 				    le32toh(sc->sc_txdescs[i].at_buf2));
   3246 				if (i == txs->txs_lastdesc)
   3247 					break;
   3248 			}
   3249 		}
   3250 #endif
   3251 
   3252 		txstat = le32toh(sc->sc_txdescs[txs->txs_lastdesc].at_stat);
   3253 		if (txstat & ATW_TXSTAT_OWN)
   3254 			break;
   3255 
   3256 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
   3257 
   3258 		sc->sc_txfree += txs->txs_ndescs;
   3259 
   3260 		bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
   3261 		    0, txs->txs_dmamap->dm_mapsize,
   3262 		    BUS_DMASYNC_POSTWRITE);
   3263 		bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   3264 		m_freem(txs->txs_mbuf);
   3265 		txs->txs_mbuf = NULL;
   3266 
   3267 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   3268 
   3269 		ifp->if_flags &= ~IFF_OACTIVE;
   3270 
   3271 		if ((ifp->if_flags & IFF_DEBUG) != 0 &&
   3272 		    (txstat & TXSTAT_ERRMASK) != 0) {
   3273 			bitmask_snprintf(txstat & TXSTAT_ERRMASK, TXSTAT_FMT,
   3274 			    txstat_buf, sizeof(txstat_buf));
   3275 			printf("%s: txstat %s %d\n", sc->sc_dev.dv_xname,
   3276 			    txstat_buf,
   3277 			    MASK_AND_RSHIFT(txstat, ATW_TXSTAT_ARC_MASK));
   3278 		}
   3279 
   3280 		/*
   3281 		 * Check for errors and collisions.
   3282 		 */
   3283 		if (txstat & ATW_TXSTAT_TUF)
   3284 			sc->sc_stats.ts_tx_tuf++;
   3285 		if (txstat & ATW_TXSTAT_TLT)
   3286 			sc->sc_stats.ts_tx_tlt++;
   3287 		if (txstat & ATW_TXSTAT_TRT)
   3288 			sc->sc_stats.ts_tx_trt++;
   3289 		if (txstat & ATW_TXSTAT_TRO)
   3290 			sc->sc_stats.ts_tx_tro++;
   3291 		if (txstat & ATW_TXSTAT_SOFBR) {
   3292 			sc->sc_stats.ts_tx_sofbr++;
   3293 		}
   3294 
   3295 		if ((txstat & ATW_TXSTAT_ES) == 0)
   3296 			ifp->if_collisions +=
   3297 			    MASK_AND_RSHIFT(txstat, ATW_TXSTAT_ARC_MASK);
   3298 		else
   3299 			ifp->if_oerrors++;
   3300 
   3301 		ifp->if_opackets++;
   3302 	}
   3303 
   3304 	/*
   3305 	 * If there are no more pending transmissions, cancel the watchdog
   3306 	 * timer.
   3307 	 */
   3308 	if (txs == NULL)
   3309 		sc->sc_tx_timer = 0;
   3310 #undef TXSTAT_ERRMASK
   3311 #undef TXSTAT_FMT
   3312 }
   3313 
   3314 /*
   3315  * atw_watchdog:	[ifnet interface function]
   3316  *
   3317  *	Watchdog timer handler.
   3318  */
   3319 void
   3320 atw_watchdog(struct ifnet *ifp)
   3321 {
   3322 	struct atw_softc *sc = ifp->if_softc;
   3323 	struct ieee80211com *ic = &sc->sc_ic;
   3324 
   3325 	ifp->if_timer = 0;
   3326 	if (ATW_IS_ENABLED(sc) == 0)
   3327 		return;
   3328 
   3329 	if (sc->sc_rescan_timer) {
   3330 		if (--sc->sc_rescan_timer == 0)
   3331 			(void)ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
   3332 	}
   3333 	if (sc->sc_tx_timer) {
   3334 		if (--sc->sc_tx_timer == 0 &&
   3335 		    !SIMPLEQ_EMPTY(&sc->sc_txdirtyq)) {
   3336 			printf("%s: transmit timeout\n", ifp->if_xname);
   3337 			ifp->if_oerrors++;
   3338 			(void)atw_init(ifp);
   3339 			atw_start(ifp);
   3340 		}
   3341 	}
   3342 	if (sc->sc_tx_timer != 0 || sc->sc_rescan_timer != 0)
   3343 		ifp->if_timer = 1;
   3344 	ieee80211_watchdog(ic);
   3345 }
   3346 
   3347 #ifdef ATW_DEBUG
   3348 static void
   3349 atw_dump_pkt(struct ifnet *ifp, struct mbuf *m0)
   3350 {
   3351 	struct atw_softc *sc = ifp->if_softc;
   3352 	struct mbuf *m;
   3353 	int i, noctets = 0;
   3354 
   3355 	printf("%s: %d-byte packet\n", sc->sc_dev.dv_xname,
   3356 	    m0->m_pkthdr.len);
   3357 
   3358 	for (m = m0; m; m = m->m_next) {
   3359 		if (m->m_len == 0)
   3360 			continue;
   3361 		for (i = 0; i < m->m_len; i++) {
   3362 			printf(" %02x", ((u_int8_t*)m->m_data)[i]);
   3363 			if (++noctets % 24 == 0)
   3364 				printf("\n");
   3365 		}
   3366 	}
   3367 	printf("%s%s: %d bytes emitted\n",
   3368 	    (noctets % 24 != 0) ? "\n" : "", sc->sc_dev.dv_xname, noctets);
   3369 }
   3370 #endif /* ATW_DEBUG */
   3371 
   3372 /*
   3373  * atw_start:		[ifnet interface function]
   3374  *
   3375  *	Start packet transmission on the interface.
   3376  */
   3377 void
   3378 atw_start(struct ifnet *ifp)
   3379 {
   3380 	struct atw_softc *sc = ifp->if_softc;
   3381 	struct ieee80211_key *k;
   3382 	struct ieee80211com *ic = &sc->sc_ic;
   3383 	struct ieee80211_node *ni;
   3384 	struct ieee80211_frame_min *whm;
   3385 	struct ieee80211_frame *wh;
   3386 	struct atw_frame *hh;
   3387 	struct mbuf *m0, *m;
   3388 	struct atw_txsoft *txs, *last_txs;
   3389 	struct atw_txdesc *txd;
   3390 	int do_encrypt, npkt, rate;
   3391 	bus_dmamap_t dmamap;
   3392 	int ctl, error, firsttx, nexttx, lasttx = -1, first, ofree, seg;
   3393 
   3394 	DPRINTF2(sc, ("%s: atw_start: sc_flags 0x%08x, if_flags 0x%08x\n",
   3395 	    sc->sc_dev.dv_xname, sc->sc_flags, ifp->if_flags));
   3396 
   3397 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
   3398 		return;
   3399 
   3400 	/*
   3401 	 * Remember the previous number of free descriptors and
   3402 	 * the first descriptor we'll use.
   3403 	 */
   3404 	ofree = sc->sc_txfree;
   3405 	firsttx = sc->sc_txnext;
   3406 
   3407 	DPRINTF2(sc, ("%s: atw_start: txfree %d, txnext %d\n",
   3408 	    sc->sc_dev.dv_xname, ofree, firsttx));
   3409 
   3410 	/*
   3411 	 * Loop through the send queue, setting up transmit descriptors
   3412 	 * until we drain the queue, or use up all available transmit
   3413 	 * descriptors.
   3414 	 */
   3415 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txfreeq)) != NULL &&
   3416 	       sc->sc_txfree != 0) {
   3417 
   3418 		/*
   3419 		 * Grab a packet off the management queue, if it
   3420 		 * is not empty. Otherwise, from the data queue.
   3421 		 */
   3422 		IF_DEQUEUE(&ic->ic_mgtq, m0);
   3423 		if (m0 != NULL) {
   3424 			ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
   3425 			m0->m_pkthdr.rcvif = NULL;
   3426 		} else {
   3427 			/* send no data packets until we are associated */
   3428 			if (ic->ic_state != IEEE80211_S_RUN)
   3429 				break;
   3430 			IFQ_DEQUEUE(&ifp->if_snd, m0);
   3431 			if (m0 == NULL)
   3432 				break;
   3433 #if NBPFILTER > 0
   3434 			if (ifp->if_bpf != NULL)
   3435 				bpf_mtap(ifp->if_bpf, m0);
   3436 #endif /* NBPFILTER > 0 */
   3437 			ni = ieee80211_find_txnode(ic,
   3438 			    mtod(m0, struct ether_header *)->ether_dhost);
   3439 			if (ni == NULL) {
   3440 				ifp->if_oerrors++;
   3441 				break;
   3442 			}
   3443 			if ((m0 = ieee80211_encap(ic, m0, ni)) == NULL) {
   3444 				ieee80211_free_node(ni);
   3445 				ifp->if_oerrors++;
   3446 				break;
   3447 			}
   3448 		}
   3449 
   3450 		rate = MAX(ieee80211_get_rate(ic), 2);
   3451 
   3452 		whm = mtod(m0, struct ieee80211_frame_min *);
   3453 
   3454 		do_encrypt = ((whm->i_fc[1] & IEEE80211_FC1_WEP) != 0) ? 1 : 0;
   3455 		if (do_encrypt)
   3456 			k = &ic->ic_nw_keys[ic->ic_def_txkey];
   3457 		else
   3458 			k = NULL;
   3459 
   3460 		if (ieee80211_compute_duration(whm, k, m0->m_pkthdr.len,
   3461 		    ic->ic_flags, ic->ic_fragthreshold, rate,
   3462 		    &txs->txs_d0, &txs->txs_dn, &npkt, 0) == -1) {
   3463 			DPRINTF2(sc, ("%s: fail compute duration\n", __func__));
   3464 			m_freem(m0);
   3465 			break;
   3466 		}
   3467 
   3468 		/* XXX Misleading if fragmentation is enabled.  Better
   3469 		 * to fragment in software?
   3470 		 */
   3471 		*(uint16_t *)whm->i_dur = htole16(txs->txs_d0.d_rts_dur);
   3472 
   3473 #if NBPFILTER > 0
   3474 		/*
   3475 		 * Pass the packet to any BPF listeners.
   3476 		 */
   3477 		if (ic->ic_rawbpf != NULL)
   3478 			bpf_mtap((caddr_t)ic->ic_rawbpf, m0);
   3479 
   3480 		if (sc->sc_radiobpf != NULL) {
   3481 			struct atw_tx_radiotap_header *tap = &sc->sc_txtap;
   3482 
   3483 			tap->at_rate = rate;
   3484 			tap->at_chan_freq = ic->ic_curchan->ic_freq;
   3485 			tap->at_chan_flags = ic->ic_curchan->ic_flags;
   3486 
   3487 			/* TBD tap->at_flags */
   3488 
   3489 			bpf_mtap2(sc->sc_radiobpf, (caddr_t)tap,
   3490 			    tap->at_ihdr.it_len, m0);
   3491 		}
   3492 #endif /* NBPFILTER > 0 */
   3493 
   3494 		M_PREPEND(m0, offsetof(struct atw_frame, atw_ihdr), M_DONTWAIT);
   3495 
   3496 		if (ni != NULL)
   3497 			ieee80211_free_node(ni);
   3498 
   3499 		if (m0 == NULL) {
   3500 			ifp->if_oerrors++;
   3501 			break;
   3502 		}
   3503 
   3504 		/* just to make sure. */
   3505 		m0 = m_pullup(m0, sizeof(struct atw_frame));
   3506 
   3507 		if (m0 == NULL) {
   3508 			ifp->if_oerrors++;
   3509 			break;
   3510 		}
   3511 
   3512 		hh = mtod(m0, struct atw_frame *);
   3513 		wh = &hh->atw_ihdr;
   3514 
   3515 		/* Copy everything we need from the 802.11 header:
   3516 		 * Frame Control; address 1, address 3, or addresses
   3517 		 * 3 and 4. NIC fills in BSSID, SA.
   3518 		 */
   3519 		if (wh->i_fc[1] & IEEE80211_FC1_DIR_TODS) {
   3520 			if (wh->i_fc[1] & IEEE80211_FC1_DIR_FROMDS)
   3521 				panic("%s: illegal WDS frame",
   3522 				    sc->sc_dev.dv_xname);
   3523 			memcpy(hh->atw_dst, wh->i_addr3, IEEE80211_ADDR_LEN);
   3524 		} else
   3525 			memcpy(hh->atw_dst, wh->i_addr1, IEEE80211_ADDR_LEN);
   3526 
   3527 		*(u_int16_t*)hh->atw_fc = *(u_int16_t*)wh->i_fc;
   3528 
   3529 		/* initialize remaining Tx parameters */
   3530 		memset(&hh->u, 0, sizeof(hh->u));
   3531 
   3532 		hh->atw_rate = rate * 5;
   3533 		/* XXX this could be incorrect if M_FCS. _encap should
   3534 		 * probably strip FCS just in case it sticks around in
   3535 		 * bridged packets.
   3536 		 */
   3537 		hh->atw_service = 0x00; /* XXX guess */
   3538 		hh->atw_paylen = htole16(m0->m_pkthdr.len -
   3539 		    sizeof(struct atw_frame));
   3540 
   3541 		hh->atw_fragthr = htole16(ic->ic_fragthreshold);
   3542 		hh->atw_rtylmt = 3;
   3543 		hh->atw_hdrctl = htole16(ATW_HDRCTL_UNKNOWN1);
   3544 		if (do_encrypt) {
   3545 			hh->atw_hdrctl |= htole16(ATW_HDRCTL_WEP);
   3546 			hh->atw_keyid = ic->ic_def_txkey;
   3547 		}
   3548 
   3549 		hh->atw_head_plcplen = htole16(txs->txs_d0.d_plcp_len);
   3550 		hh->atw_tail_plcplen = htole16(txs->txs_dn.d_plcp_len);
   3551 		if (txs->txs_d0.d_residue)
   3552 			hh->atw_head_plcplen |= htole16(0x8000);
   3553 		if (txs->txs_dn.d_residue)
   3554 			hh->atw_tail_plcplen |= htole16(0x8000);
   3555 		hh->atw_head_dur = htole16(txs->txs_d0.d_rts_dur);
   3556 		hh->atw_tail_dur = htole16(txs->txs_dn.d_rts_dur);
   3557 
   3558 		/* never fragment multicast frames */
   3559 		if (IEEE80211_IS_MULTICAST(hh->atw_dst)) {
   3560 			hh->atw_fragthr = htole16(ic->ic_fragthreshold);
   3561 		} else if (sc->sc_flags & ATWF_RTSCTS) {
   3562 			hh->atw_hdrctl |= htole16(ATW_HDRCTL_RTSCTS);
   3563 		}
   3564 
   3565 #ifdef ATW_DEBUG
   3566 		hh->atw_fragnum = 0;
   3567 
   3568 		if ((ifp->if_flags & IFF_DEBUG) != 0 && atw_debug > 2) {
   3569 			printf("%s: dst = %s, rate = 0x%02x, "
   3570 			    "service = 0x%02x, paylen = 0x%04x\n",
   3571 			    sc->sc_dev.dv_xname, ether_sprintf(hh->atw_dst),
   3572 			    hh->atw_rate, hh->atw_service, hh->atw_paylen);
   3573 
   3574 			printf("%s: fc[0] = 0x%02x, fc[1] = 0x%02x, "
   3575 			    "dur1 = 0x%04x, dur2 = 0x%04x, "
   3576 			    "dur3 = 0x%04x, rts_dur = 0x%04x\n",
   3577 			    sc->sc_dev.dv_xname, hh->atw_fc[0], hh->atw_fc[1],
   3578 			    hh->atw_tail_plcplen, hh->atw_head_plcplen,
   3579 			    hh->atw_tail_dur, hh->atw_head_dur);
   3580 
   3581 			printf("%s: hdrctl = 0x%04x, fragthr = 0x%04x, "
   3582 			    "fragnum = 0x%02x, rtylmt = 0x%04x\n",
   3583 			    sc->sc_dev.dv_xname, hh->atw_hdrctl,
   3584 			    hh->atw_fragthr, hh->atw_fragnum, hh->atw_rtylmt);
   3585 
   3586 			printf("%s: keyid = %d\n",
   3587 			    sc->sc_dev.dv_xname, hh->atw_keyid);
   3588 
   3589 			atw_dump_pkt(ifp, m0);
   3590 		}
   3591 #endif /* ATW_DEBUG */
   3592 
   3593 		dmamap = txs->txs_dmamap;
   3594 
   3595 		/*
   3596 		 * Load the DMA map.  Copy and try (once) again if the packet
   3597 		 * didn't fit in the alloted number of segments.
   3598 		 */
   3599 		for (first = 1;
   3600 		     (error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
   3601 		                  BUS_DMA_WRITE|BUS_DMA_NOWAIT)) != 0 && first;
   3602 		     first = 0) {
   3603 			MGETHDR(m, M_DONTWAIT, MT_DATA);
   3604 			if (m == NULL) {
   3605 				printf("%s: unable to allocate Tx mbuf\n",
   3606 				    sc->sc_dev.dv_xname);
   3607 				break;
   3608 			}
   3609 			if (m0->m_pkthdr.len > MHLEN) {
   3610 				MCLGET(m, M_DONTWAIT);
   3611 				if ((m->m_flags & M_EXT) == 0) {
   3612 					printf("%s: unable to allocate Tx "
   3613 					    "cluster\n", sc->sc_dev.dv_xname);
   3614 					m_freem(m);
   3615 					break;
   3616 				}
   3617 			}
   3618 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
   3619 			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
   3620 			m_freem(m0);
   3621 			m0 = m;
   3622 			m = NULL;
   3623 		}
   3624 		if (error != 0) {
   3625 			printf("%s: unable to load Tx buffer, "
   3626 			    "error = %d\n", sc->sc_dev.dv_xname, error);
   3627 			m_freem(m0);
   3628 			break;
   3629 		}
   3630 
   3631 		/*
   3632 		 * Ensure we have enough descriptors free to describe
   3633 		 * the packet.
   3634 		 */
   3635 		if (dmamap->dm_nsegs > sc->sc_txfree) {
   3636 			/*
   3637 			 * Not enough free descriptors to transmit
   3638 			 * this packet.  Unload the DMA map and
   3639 			 * drop the packet.  Notify the upper layer
   3640 			 * that there are no more slots left.
   3641 			 *
   3642 			 * XXX We could allocate an mbuf and copy, but
   3643 			 * XXX it is worth it?
   3644 			 */
   3645 			bus_dmamap_unload(sc->sc_dmat, dmamap);
   3646 			m_freem(m0);
   3647 			break;
   3648 		}
   3649 
   3650 		/*
   3651 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
   3652 		 */
   3653 
   3654 		/* Sync the DMA map. */
   3655 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
   3656 		    BUS_DMASYNC_PREWRITE);
   3657 
   3658 		/* XXX arbitrary retry limit; 8 because I have seen it in
   3659 		 * use already and maybe 0 means "no tries" !
   3660 		 */
   3661 		ctl = htole32(LSHIFT(8, ATW_TXCTL_TL_MASK));
   3662 
   3663 		DPRINTF2(sc, ("%s: TXDR <- max(10, %d)\n",
   3664 		    sc->sc_dev.dv_xname, rate * 5));
   3665 		ctl |= htole32(LSHIFT(MAX(10, rate * 5), ATW_TXCTL_TXDR_MASK));
   3666 
   3667 		/*
   3668 		 * Initialize the transmit descriptors.
   3669 		 */
   3670 		for (nexttx = sc->sc_txnext, seg = 0;
   3671 		     seg < dmamap->dm_nsegs;
   3672 		     seg++, nexttx = ATW_NEXTTX(nexttx)) {
   3673 			/*
   3674 			 * If this is the first descriptor we're
   3675 			 * enqueueing, don't set the OWN bit just
   3676 			 * yet.  That could cause a race condition.
   3677 			 * We'll do it below.
   3678 			 */
   3679 			txd = &sc->sc_txdescs[nexttx];
   3680 			txd->at_ctl = ctl |
   3681 			    ((nexttx == firsttx) ? 0 : htole32(ATW_TXCTL_OWN));
   3682 
   3683 			txd->at_buf1 = htole32(dmamap->dm_segs[seg].ds_addr);
   3684 			txd->at_flags =
   3685 			    htole32(LSHIFT(dmamap->dm_segs[seg].ds_len,
   3686 			                   ATW_TXFLAG_TBS1_MASK)) |
   3687 			    ((nexttx == (ATW_NTXDESC - 1))
   3688 			        ? htole32(ATW_TXFLAG_TER) : 0);
   3689 			lasttx = nexttx;
   3690 		}
   3691 
   3692 		IASSERT(lasttx != -1, ("bad lastx"));
   3693 		/* Set `first segment' and `last segment' appropriately. */
   3694 		sc->sc_txdescs[sc->sc_txnext].at_flags |=
   3695 		    htole32(ATW_TXFLAG_FS);
   3696 		sc->sc_txdescs[lasttx].at_flags |= htole32(ATW_TXFLAG_LS);
   3697 
   3698 #ifdef ATW_DEBUG
   3699 		if ((ifp->if_flags & IFF_DEBUG) != 0 && atw_debug > 2) {
   3700 			printf("     txsoft %p transmit chain:\n", txs);
   3701 			for (seg = sc->sc_txnext;; seg = ATW_NEXTTX(seg)) {
   3702 				printf("     descriptor %d:\n", seg);
   3703 				printf("       at_ctl:   0x%08x\n",
   3704 				    le32toh(sc->sc_txdescs[seg].at_ctl));
   3705 				printf("       at_flags:      0x%08x\n",
   3706 				    le32toh(sc->sc_txdescs[seg].at_flags));
   3707 				printf("       at_buf1: 0x%08x\n",
   3708 				    le32toh(sc->sc_txdescs[seg].at_buf1));
   3709 				printf("       at_buf2: 0x%08x\n",
   3710 				    le32toh(sc->sc_txdescs[seg].at_buf2));
   3711 				if (seg == lasttx)
   3712 					break;
   3713 			}
   3714 		}
   3715 #endif
   3716 
   3717 		/* Sync the descriptors we're using. */
   3718 		ATW_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
   3719 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   3720 
   3721 		/*
   3722 		 * Store a pointer to the packet so we can free it later,
   3723 		 * and remember what txdirty will be once the packet is
   3724 		 * done.
   3725 		 */
   3726 		txs->txs_mbuf = m0;
   3727 		txs->txs_firstdesc = sc->sc_txnext;
   3728 		txs->txs_lastdesc = lasttx;
   3729 		txs->txs_ndescs = dmamap->dm_nsegs;
   3730 
   3731 		/* Advance the tx pointer. */
   3732 		sc->sc_txfree -= dmamap->dm_nsegs;
   3733 		sc->sc_txnext = nexttx;
   3734 
   3735 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q);
   3736 		SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
   3737 
   3738 		last_txs = txs;
   3739 	}
   3740 
   3741 	if (txs == NULL || sc->sc_txfree == 0) {
   3742 		/* No more slots left; notify upper layer. */
   3743 		ifp->if_flags |= IFF_OACTIVE;
   3744 	}
   3745 
   3746 	if (sc->sc_txfree != ofree) {
   3747 		DPRINTF2(sc, ("%s: packets enqueued, IC on %d, OWN on %d\n",
   3748 		    sc->sc_dev.dv_xname, lasttx, firsttx));
   3749 		/*
   3750 		 * Cause a transmit interrupt to happen on the
   3751 		 * last packet we enqueued.
   3752 		 */
   3753 		sc->sc_txdescs[lasttx].at_flags |= htole32(ATW_TXFLAG_IC);
   3754 		ATW_CDTXSYNC(sc, lasttx, 1,
   3755 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   3756 
   3757 		/*
   3758 		 * The entire packet chain is set up.  Give the
   3759 		 * first descriptor to the chip now.
   3760 		 */
   3761 		sc->sc_txdescs[firsttx].at_ctl |= htole32(ATW_TXCTL_OWN);
   3762 		ATW_CDTXSYNC(sc, firsttx, 1,
   3763 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   3764 
   3765 		/* Wake up the transmitter. */
   3766 		ATW_WRITE(sc, ATW_TDR, 0x1);
   3767 
   3768 		/* Set a watchdog timer in case the chip flakes out. */
   3769 		sc->sc_tx_timer = 5;
   3770 		ifp->if_timer = 1;
   3771 	}
   3772 }
   3773 
   3774 /*
   3775  * atw_power:
   3776  *
   3777  *	Power management (suspend/resume) hook.
   3778  */
   3779 void
   3780 atw_power(int why, void *arg)
   3781 {
   3782 	struct atw_softc *sc = arg;
   3783 	struct ifnet *ifp = &sc->sc_if;
   3784 	int s;
   3785 
   3786 	DPRINTF(sc, ("%s: atw_power(%d,)\n", sc->sc_dev.dv_xname, why));
   3787 
   3788 	s = splnet();
   3789 	switch (why) {
   3790 	case PWR_STANDBY:
   3791 		/* XXX do nothing. */
   3792 		break;
   3793 	case PWR_SUSPEND:
   3794 		atw_stop(ifp, 0);
   3795 		if (sc->sc_power != NULL)
   3796 			(*sc->sc_power)(sc, why);
   3797 		break;
   3798 	case PWR_RESUME:
   3799 		if (ifp->if_flags & IFF_UP) {
   3800 			if (sc->sc_power != NULL)
   3801 				(*sc->sc_power)(sc, why);
   3802 			atw_init(ifp);
   3803 		}
   3804 		break;
   3805 	case PWR_SOFTSUSPEND:
   3806 	case PWR_SOFTSTANDBY:
   3807 	case PWR_SOFTRESUME:
   3808 		break;
   3809 	}
   3810 	splx(s);
   3811 }
   3812 
   3813 /*
   3814  * atw_ioctl:		[ifnet interface function]
   3815  *
   3816  *	Handle control requests from the operator.
   3817  */
   3818 int
   3819 atw_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
   3820 {
   3821 	struct atw_softc *sc = ifp->if_softc;
   3822 	struct ifreq *ifr = (struct ifreq *)data;
   3823 	int s, error = 0;
   3824 
   3825 	/* XXX monkey see, monkey do. comes from wi_ioctl. */
   3826 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
   3827 		return ENXIO;
   3828 
   3829 	s = splnet();
   3830 
   3831 	switch (cmd) {
   3832 	case SIOCSIFFLAGS:
   3833 		if (ifp->if_flags & IFF_UP) {
   3834 			if (ATW_IS_ENABLED(sc)) {
   3835 				/*
   3836 				 * To avoid rescanning another access point,
   3837 				 * do not call atw_init() here.  Instead,
   3838 				 * only reflect media settings.
   3839 				 */
   3840 				atw_filter_setup(sc);
   3841 			} else
   3842 				error = atw_init(ifp);
   3843 		} else if (ATW_IS_ENABLED(sc))
   3844 			atw_stop(ifp, 1);
   3845 		break;
   3846 	case SIOCADDMULTI:
   3847 	case SIOCDELMULTI:
   3848 		error = (cmd == SIOCADDMULTI) ?
   3849 		    ether_addmulti(ifr, &sc->sc_ec) :
   3850 		    ether_delmulti(ifr, &sc->sc_ec);
   3851 		if (error == ENETRESET) {
   3852 			if (ifp->if_flags & IFF_RUNNING)
   3853 				atw_filter_setup(sc); /* do not rescan */
   3854 			error = 0;
   3855 		}
   3856 		break;
   3857 	default:
   3858 		error = ieee80211_ioctl(&sc->sc_ic, cmd, data);
   3859 		if (error == ENETRESET) {
   3860 			if (ATW_IS_ENABLED(sc))
   3861 				error = atw_init(ifp);
   3862 			else
   3863 				error = 0;
   3864 		}
   3865 		break;
   3866 	}
   3867 
   3868 	/* Try to get more packets going. */
   3869 	if (ATW_IS_ENABLED(sc))
   3870 		atw_start(ifp);
   3871 
   3872 	splx(s);
   3873 	return (error);
   3874 }
   3875 
   3876 static int
   3877 atw_media_change(struct ifnet *ifp)
   3878 {
   3879 	int error;
   3880 
   3881 	error = ieee80211_media_change(ifp);
   3882 	if (error == ENETRESET) {
   3883 		if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) ==
   3884 		    (IFF_RUNNING|IFF_UP))
   3885 			atw_init(ifp);		/* XXX lose error */
   3886 		error = 0;
   3887 	}
   3888 	return error;
   3889 }
   3890