Home | History | Annotate | Line # | Download | only in pci
if_wpi.c revision 1.49.4.1
      1 /*  $NetBSD: if_wpi.c,v 1.49.4.1 2012/04/17 00:07:50 yamt Exp $    */
      2 
      3 /*-
      4  * Copyright (c) 2006, 2007
      5  *	Damien Bergamini <damien.bergamini (at) free.fr>
      6  *
      7  * Permission to use, copy, modify, and distribute this software for any
      8  * purpose with or without fee is hereby granted, provided that the above
      9  * copyright notice and this permission notice appear in all copies.
     10  *
     11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  */
     19 
     20 #include <sys/cdefs.h>
     21 __KERNEL_RCSID(0, "$NetBSD: if_wpi.c,v 1.49.4.1 2012/04/17 00:07:50 yamt Exp $");
     22 
     23 /*
     24  * Driver for Intel PRO/Wireless 3945ABG 802.11 network adapters.
     25  */
     26 
     27 
     28 #include <sys/param.h>
     29 #include <sys/sockio.h>
     30 #include <sys/sysctl.h>
     31 #include <sys/mbuf.h>
     32 #include <sys/kernel.h>
     33 #include <sys/socket.h>
     34 #include <sys/systm.h>
     35 #include <sys/malloc.h>
     36 #include <sys/mutex.h>
     37 #include <sys/once.h>
     38 #include <sys/conf.h>
     39 #include <sys/kauth.h>
     40 #include <sys/callout.h>
     41 #include <sys/proc.h>
     42 
     43 #include <sys/bus.h>
     44 #include <machine/endian.h>
     45 #include <sys/intr.h>
     46 
     47 #include <dev/pci/pcireg.h>
     48 #include <dev/pci/pcivar.h>
     49 #include <dev/pci/pcidevs.h>
     50 
     51 #include <net/bpf.h>
     52 #include <net/if.h>
     53 #include <net/if_arp.h>
     54 #include <net/if_dl.h>
     55 #include <net/if_ether.h>
     56 #include <net/if_media.h>
     57 #include <net/if_types.h>
     58 
     59 #include <net80211/ieee80211_var.h>
     60 #include <net80211/ieee80211_amrr.h>
     61 #include <net80211/ieee80211_radiotap.h>
     62 
     63 #include <netinet/in.h>
     64 #include <netinet/in_systm.h>
     65 #include <netinet/in_var.h>
     66 #include <netinet/ip.h>
     67 
     68 #include <dev/firmload.h>
     69 
     70 #include <dev/pci/if_wpireg.h>
     71 #include <dev/pci/if_wpivar.h>
     72 
     73 #ifdef WPI_DEBUG
     74 #define DPRINTF(x)	if (wpi_debug > 0) printf x
     75 #define DPRINTFN(n, x)	if (wpi_debug >= (n)) printf x
     76 int wpi_debug = 1;
     77 #else
     78 #define DPRINTF(x)
     79 #define DPRINTFN(n, x)
     80 #endif
     81 
     82 /*
     83  * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
     84  */
     85 static const struct ieee80211_rateset wpi_rateset_11a =
     86 	{ 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
     87 
     88 static const struct ieee80211_rateset wpi_rateset_11b =
     89 	{ 4, { 2, 4, 11, 22 } };
     90 
     91 static const struct ieee80211_rateset wpi_rateset_11g =
     92 	{ 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
     93 
     94 static once_t wpi_firmware_init;
     95 static kmutex_t wpi_firmware_mutex;
     96 static size_t wpi_firmware_users;
     97 static uint8_t *wpi_firmware_image;
     98 static size_t wpi_firmware_size;
     99 
    100 static int  wpi_match(device_t, cfdata_t, void *);
    101 static void wpi_attach(device_t, device_t, void *);
    102 static int  wpi_detach(device_t , int);
    103 static int  wpi_dma_contig_alloc(bus_dma_tag_t, struct wpi_dma_info *,
    104 	void **, bus_size_t, bus_size_t, int);
    105 static void wpi_dma_contig_free(struct wpi_dma_info *);
    106 static int  wpi_alloc_shared(struct wpi_softc *);
    107 static void wpi_free_shared(struct wpi_softc *);
    108 static int  wpi_alloc_fwmem(struct wpi_softc *);
    109 static void wpi_free_fwmem(struct wpi_softc *);
    110 static struct wpi_rbuf *wpi_alloc_rbuf(struct wpi_softc *);
    111 static void wpi_free_rbuf(struct mbuf *, void *, size_t, void *);
    112 static int  wpi_alloc_rpool(struct wpi_softc *);
    113 static void wpi_free_rpool(struct wpi_softc *);
    114 static int  wpi_alloc_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
    115 static void wpi_reset_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
    116 static void wpi_free_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
    117 static int  wpi_alloc_tx_ring(struct wpi_softc *, struct wpi_tx_ring *, int,
    118 	int);
    119 static void wpi_reset_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
    120 static void wpi_free_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
    121 static struct ieee80211_node * wpi_node_alloc(struct ieee80211_node_table *);
    122 static void wpi_newassoc(struct ieee80211_node *, int);
    123 static int  wpi_media_change(struct ifnet *);
    124 static int  wpi_newstate(struct ieee80211com *, enum ieee80211_state, int);
    125 static void	wpi_fix_channel(struct ieee80211com *, struct mbuf *);
    126 static void wpi_mem_lock(struct wpi_softc *);
    127 static void wpi_mem_unlock(struct wpi_softc *);
    128 static uint32_t wpi_mem_read(struct wpi_softc *, uint16_t);
    129 static void wpi_mem_write(struct wpi_softc *, uint16_t, uint32_t);
    130 static void wpi_mem_write_region_4(struct wpi_softc *, uint16_t,
    131 								   const uint32_t *, int);
    132 static int  wpi_read_prom_data(struct wpi_softc *, uint32_t, void *, int);
    133 static int  wpi_load_microcode(struct wpi_softc *,  const uint8_t *, int);
    134 static int  wpi_load_firmware(struct wpi_softc *);
    135 static void wpi_calib_timeout(void *);
    136 static void wpi_iter_func(void *, struct ieee80211_node *);
    137 static void wpi_power_calibration(struct wpi_softc *, int);
    138 static void wpi_rx_intr(struct wpi_softc *, struct wpi_rx_desc *,
    139 	struct wpi_rx_data *);
    140 static void wpi_tx_intr(struct wpi_softc *, struct wpi_rx_desc *);
    141 static void wpi_cmd_intr(struct wpi_softc *, struct wpi_rx_desc *);
    142 static void wpi_notif_intr(struct wpi_softc *);
    143 static int  wpi_intr(void *);
    144 static void wpi_read_eeprom(struct wpi_softc *);
    145 static void wpi_read_eeprom_channels(struct wpi_softc *, int);
    146 static void wpi_read_eeprom_group(struct wpi_softc *, int);
    147 static uint8_t wpi_plcp_signal(int);
    148 static int  wpi_tx_data(struct wpi_softc *, struct mbuf *,
    149 	struct ieee80211_node *, int);
    150 static void wpi_start(struct ifnet *);
    151 static void wpi_watchdog(struct ifnet *);
    152 static int  wpi_ioctl(struct ifnet *, u_long, void *);
    153 static int  wpi_cmd(struct wpi_softc *, int, const void *, int, int);
    154 static int  wpi_wme_update(struct ieee80211com *);
    155 static int  wpi_mrr_setup(struct wpi_softc *);
    156 static void wpi_set_led(struct wpi_softc *, uint8_t, uint8_t, uint8_t);
    157 static void wpi_enable_tsf(struct wpi_softc *, struct ieee80211_node *);
    158 static int  wpi_set_txpower(struct wpi_softc *,
    159 			    struct ieee80211_channel *, int);
    160 static int  wpi_get_power_index(struct wpi_softc *,
    161 		struct wpi_power_group *, struct ieee80211_channel *, int);
    162 static int  wpi_setup_beacon(struct wpi_softc *, struct ieee80211_node *);
    163 static int  wpi_auth(struct wpi_softc *);
    164 static int  wpi_scan(struct wpi_softc *, uint16_t);
    165 static int  wpi_config(struct wpi_softc *);
    166 static void wpi_stop_master(struct wpi_softc *);
    167 static int  wpi_power_up(struct wpi_softc *);
    168 static int  wpi_reset(struct wpi_softc *);
    169 static void wpi_hw_config(struct wpi_softc *);
    170 static int  wpi_init(struct ifnet *);
    171 static void wpi_stop(struct ifnet *, int);
    172 static bool wpi_resume(device_t, const pmf_qual_t *);
    173 static int	wpi_getrfkill(struct wpi_softc *);
    174 static void wpi_sysctlattach(struct wpi_softc *);
    175 
    176 CFATTACH_DECL_NEW(wpi, sizeof (struct wpi_softc), wpi_match, wpi_attach,
    177 	wpi_detach, NULL);
    178 
    179 static int
    180 wpi_match(device_t parent, cfdata_t match __unused, void *aux)
    181 {
    182 	struct pci_attach_args *pa = aux;
    183 
    184 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
    185 		return 0;
    186 
    187 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_3945ABG_1 ||
    188 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_3945ABG_2)
    189 		return 1;
    190 
    191 	return 0;
    192 }
    193 
    194 /* Base Address Register */
    195 #define WPI_PCI_BAR0	0x10
    196 
    197 static int
    198 wpi_attach_once(void)
    199 {
    200 	mutex_init(&wpi_firmware_mutex, MUTEX_DEFAULT, IPL_NONE);
    201 	return 0;
    202 }
    203 
    204 static void
    205 wpi_attach(device_t parent __unused, device_t self, void *aux)
    206 {
    207 	struct wpi_softc *sc = device_private(self);
    208 	struct ieee80211com *ic = &sc->sc_ic;
    209 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    210 	struct pci_attach_args *pa = aux;
    211 	const char *intrstr;
    212 	bus_space_tag_t memt;
    213 	bus_space_handle_t memh;
    214 	pci_intr_handle_t ih;
    215 	pcireg_t data;
    216 	int error, ac;
    217 
    218 	RUN_ONCE(&wpi_firmware_init, wpi_attach_once);
    219 	sc->fw_used = false;
    220 
    221 	sc->sc_dev = self;
    222 	sc->sc_pct = pa->pa_pc;
    223 	sc->sc_pcitag = pa->pa_tag;
    224 
    225 	callout_init(&sc->calib_to, 0);
    226 	callout_setfunc(&sc->calib_to, wpi_calib_timeout, sc);
    227 
    228 	pci_aprint_devinfo(pa, NULL);
    229 
    230 	/* enable bus-mastering */
    231 	data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
    232 	data |= PCI_COMMAND_MASTER_ENABLE;
    233 	pci_conf_write(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG, data);
    234 
    235 	/* map the register window */
    236 	error = pci_mapreg_map(pa, WPI_PCI_BAR0, PCI_MAPREG_TYPE_MEM |
    237 		PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh, NULL, &sc->sc_sz);
    238 	if (error != 0) {
    239 		aprint_error_dev(self, "could not map memory space\n");
    240 		return;
    241 	}
    242 
    243 	sc->sc_st = memt;
    244 	sc->sc_sh = memh;
    245 	sc->sc_dmat = pa->pa_dmat;
    246 
    247 	if (pci_intr_map(pa, &ih) != 0) {
    248 		aprint_error_dev(self, "could not map interrupt\n");
    249 		return;
    250 	}
    251 
    252 	intrstr = pci_intr_string(sc->sc_pct, ih);
    253 	sc->sc_ih = pci_intr_establish(sc->sc_pct, ih, IPL_NET, wpi_intr, sc);
    254 	if (sc->sc_ih == NULL) {
    255 		aprint_error_dev(self, "could not establish interrupt");
    256 		if (intrstr != NULL)
    257 			aprint_error(" at %s", intrstr);
    258 		aprint_error("\n");
    259 		return;
    260 	}
    261 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
    262 
    263 	if (wpi_reset(sc) != 0) {
    264 		aprint_error_dev(self, "could not reset adapter\n");
    265 		return;
    266 	}
    267 
    268  	/*
    269 	 * Allocate DMA memory for firmware transfers.
    270 	 */
    271 	if ((error = wpi_alloc_fwmem(sc)) != 0)
    272 		return;
    273 
    274 	/*
    275 	 * Allocate shared page and Tx/Rx rings.
    276 	 */
    277 	if ((error = wpi_alloc_shared(sc)) != 0) {
    278 		aprint_error_dev(self, "could not allocate shared area\n");
    279 		goto fail1;
    280 	}
    281 
    282 	if ((error = wpi_alloc_rpool(sc)) != 0) {
    283 		aprint_error_dev(self, "could not allocate Rx buffers\n");
    284 		goto fail2;
    285 	}
    286 
    287 	for (ac = 0; ac < 4; ac++) {
    288 		error = wpi_alloc_tx_ring(sc, &sc->txq[ac], WPI_TX_RING_COUNT, ac);
    289 		if (error != 0) {
    290 			aprint_error_dev(self, "could not allocate Tx ring %d\n", ac);
    291 			goto fail3;
    292 		}
    293 	}
    294 
    295 	error = wpi_alloc_tx_ring(sc, &sc->cmdq, WPI_CMD_RING_COUNT, 4);
    296 	if (error != 0) {
    297 		aprint_error_dev(self, "could not allocate command ring\n");
    298 		goto fail3;
    299 	}
    300 
    301 	if (wpi_alloc_rx_ring(sc, &sc->rxq) != 0) {
    302 		aprint_error_dev(self, "could not allocate Rx ring\n");
    303 		goto fail4;
    304 	}
    305 
    306 	ic->ic_ifp = ifp;
    307 	ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
    308 	ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
    309 	ic->ic_state = IEEE80211_S_INIT;
    310 
    311 	/* set device capabilities */
    312 	ic->ic_caps =
    313 		IEEE80211_C_IBSS |       /* IBSS mode support */
    314 		IEEE80211_C_WPA |        /* 802.11i */
    315 		IEEE80211_C_MONITOR |    /* monitor mode supported */
    316 		IEEE80211_C_TXPMGT |     /* tx power management */
    317 		IEEE80211_C_SHSLOT |     /* short slot time supported */
    318 		IEEE80211_C_SHPREAMBLE | /* short preamble supported */
    319 		IEEE80211_C_WME;         /* 802.11e */
    320 
    321 	/* read supported channels and MAC address from EEPROM */
    322 	wpi_read_eeprom(sc);
    323 
    324 	/* set supported .11a, .11b, .11g rates */
    325 	ic->ic_sup_rates[IEEE80211_MODE_11A] = wpi_rateset_11a;
    326 	ic->ic_sup_rates[IEEE80211_MODE_11B] = wpi_rateset_11b;
    327 	ic->ic_sup_rates[IEEE80211_MODE_11G] = wpi_rateset_11g;
    328 
    329 	ic->ic_ibss_chan = &ic->ic_channels[0];
    330 
    331 	ifp->if_softc = sc;
    332 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    333 	ifp->if_init = wpi_init;
    334 	ifp->if_stop = wpi_stop;
    335 	ifp->if_ioctl = wpi_ioctl;
    336 	ifp->if_start = wpi_start;
    337 	ifp->if_watchdog = wpi_watchdog;
    338 	IFQ_SET_READY(&ifp->if_snd);
    339 	memcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
    340 
    341 	if_attach(ifp);
    342 	ieee80211_ifattach(ic);
    343 	/* override default methods */
    344 	ic->ic_node_alloc = wpi_node_alloc;
    345 	ic->ic_newassoc = wpi_newassoc;
    346 	ic->ic_wme.wme_update = wpi_wme_update;
    347 
    348 	/* override state transition machine */
    349 	sc->sc_newstate = ic->ic_newstate;
    350 	ic->ic_newstate = wpi_newstate;
    351 	ieee80211_media_init(ic, wpi_media_change, ieee80211_media_status);
    352 
    353 	sc->amrr.amrr_min_success_threshold = 1;
    354 	sc->amrr.amrr_max_success_threshold = 15;
    355 
    356 	wpi_sysctlattach(sc);
    357 
    358 	if (pmf_device_register(self, NULL, wpi_resume))
    359 		pmf_class_network_register(self, ifp);
    360 	else
    361 		aprint_error_dev(self, "couldn't establish power handler\n");
    362 
    363 	bpf_attach2(ifp, DLT_IEEE802_11_RADIO,
    364 	    sizeof(struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
    365 	    &sc->sc_drvbpf);
    366 
    367 	sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
    368 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
    369 	sc->sc_rxtap.wr_ihdr.it_present = htole32(WPI_RX_RADIOTAP_PRESENT);
    370 
    371 	sc->sc_txtap_len = sizeof sc->sc_txtapu;
    372 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
    373 	sc->sc_txtap.wt_ihdr.it_present = htole32(WPI_TX_RADIOTAP_PRESENT);
    374 
    375 	ieee80211_announce(ic);
    376 
    377 	return;
    378 
    379 fail4:  wpi_free_tx_ring(sc, &sc->cmdq);
    380 fail3:  while (--ac >= 0)
    381 			wpi_free_tx_ring(sc, &sc->txq[ac]);
    382 	wpi_free_rpool(sc);
    383 fail2:	wpi_free_shared(sc);
    384 fail1:	wpi_free_fwmem(sc);
    385 }
    386 
    387 static int
    388 wpi_detach(device_t self, int flags __unused)
    389 {
    390 	struct wpi_softc *sc = device_private(self);
    391 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
    392 	int ac;
    393 
    394 	wpi_stop(ifp, 1);
    395 
    396 	if (ifp != NULL)
    397 		bpf_detach(ifp);
    398 	ieee80211_ifdetach(&sc->sc_ic);
    399 	if (ifp != NULL)
    400 		if_detach(ifp);
    401 
    402 	for (ac = 0; ac < 4; ac++)
    403 		wpi_free_tx_ring(sc, &sc->txq[ac]);
    404 	wpi_free_tx_ring(sc, &sc->cmdq);
    405 	wpi_free_rx_ring(sc, &sc->rxq);
    406 	wpi_free_rpool(sc);
    407 	wpi_free_shared(sc);
    408 
    409 	if (sc->sc_ih != NULL) {
    410 		pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
    411 		sc->sc_ih = NULL;
    412 	}
    413 
    414 	bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz);
    415 
    416 	if (sc->fw_used) {
    417 		mutex_enter(&wpi_firmware_mutex);
    418 		if (--wpi_firmware_users == 0)
    419 			firmware_free(wpi_firmware_image, wpi_firmware_size);
    420 		mutex_exit(&wpi_firmware_mutex);
    421 	}
    422 
    423 	return 0;
    424 }
    425 
    426 static int
    427 wpi_dma_contig_alloc(bus_dma_tag_t tag, struct wpi_dma_info *dma,
    428 	void **kvap, bus_size_t size, bus_size_t alignment, int flags)
    429 {
    430 	int nsegs, error;
    431 
    432 	dma->tag = tag;
    433 	dma->size = size;
    434 
    435 	error = bus_dmamap_create(tag, size, 1, size, 0, flags, &dma->map);
    436 	if (error != 0)
    437 		goto fail;
    438 
    439 	error = bus_dmamem_alloc(tag, size, alignment, 0, &dma->seg, 1, &nsegs,
    440 	    flags);
    441 	if (error != 0)
    442 		goto fail;
    443 
    444 	error = bus_dmamem_map(tag, &dma->seg, 1, size, &dma->vaddr, flags);
    445 	if (error != 0)
    446 		goto fail;
    447 
    448 	error = bus_dmamap_load(tag, dma->map, dma->vaddr, size, NULL, flags);
    449 	if (error != 0)
    450 		goto fail;
    451 
    452 	memset(dma->vaddr, 0, size);
    453 
    454 	dma->paddr = dma->map->dm_segs[0].ds_addr;
    455 	if (kvap != NULL)
    456 		*kvap = dma->vaddr;
    457 
    458 	return 0;
    459 
    460 fail:   wpi_dma_contig_free(dma);
    461 	return error;
    462 }
    463 
    464 static void
    465 wpi_dma_contig_free(struct wpi_dma_info *dma)
    466 {
    467 	if (dma->map != NULL) {
    468 		if (dma->vaddr != NULL) {
    469 			bus_dmamap_unload(dma->tag, dma->map);
    470 			bus_dmamem_unmap(dma->tag, dma->vaddr, dma->size);
    471 			bus_dmamem_free(dma->tag, &dma->seg, 1);
    472 			dma->vaddr = NULL;
    473 		}
    474 		bus_dmamap_destroy(dma->tag, dma->map);
    475 		dma->map = NULL;
    476 	}
    477 }
    478 
    479 /*
    480  * Allocate a shared page between host and NIC.
    481  */
    482 static int
    483 wpi_alloc_shared(struct wpi_softc *sc)
    484 {
    485 	int error;
    486 	/* must be aligned on a 4K-page boundary */
    487 	error = wpi_dma_contig_alloc(sc->sc_dmat, &sc->shared_dma,
    488 			(void **)&sc->shared, sizeof (struct wpi_shared),
    489 			WPI_BUF_ALIGN,BUS_DMA_NOWAIT);
    490 	if (error != 0)
    491 		aprint_error_dev(sc->sc_dev,
    492 				"could not allocate shared area DMA memory\n");
    493 
    494 	return error;
    495 }
    496 
    497 static void
    498 wpi_free_shared(struct wpi_softc *sc)
    499 {
    500 	wpi_dma_contig_free(&sc->shared_dma);
    501 }
    502 
    503 /*
    504  * Allocate DMA-safe memory for firmware transfer.
    505  */
    506 static int
    507 wpi_alloc_fwmem(struct wpi_softc *sc)
    508 {
    509 	int error;
    510 	/* allocate enough contiguous space to store text and data */
    511 	error = wpi_dma_contig_alloc(sc->sc_dmat, &sc->fw_dma, NULL,
    512 	    WPI_FW_MAIN_TEXT_MAXSZ + WPI_FW_MAIN_DATA_MAXSZ, 0,
    513 	    BUS_DMA_NOWAIT);
    514 
    515 	if (error != 0)
    516 		aprint_error_dev(sc->sc_dev,
    517 			"could not allocate firmware transfer area"
    518 			"DMA memory\n");
    519 	return error;
    520 }
    521 
    522 static void
    523 wpi_free_fwmem(struct wpi_softc *sc)
    524 {
    525 	wpi_dma_contig_free(&sc->fw_dma);
    526 }
    527 
    528 
    529 static struct wpi_rbuf *
    530 wpi_alloc_rbuf(struct wpi_softc *sc)
    531 {
    532 	struct wpi_rbuf *rbuf;
    533 
    534 	mutex_enter(&sc->rxq.freelist_mtx);
    535 	rbuf = SLIST_FIRST(&sc->rxq.freelist);
    536 	if (rbuf != NULL) {
    537 		SLIST_REMOVE_HEAD(&sc->rxq.freelist, next);
    538 		sc->rxq.nb_free_entries --;
    539 	}
    540 	mutex_exit(&sc->rxq.freelist_mtx);
    541 
    542 	return rbuf;
    543 }
    544 
    545 /*
    546  * This is called automatically by the network stack when the mbuf to which our
    547  * Rx buffer is attached is freed.
    548  */
    549 static void
    550 wpi_free_rbuf(struct mbuf* m, void *buf, size_t size, void *arg)
    551 {
    552 	struct wpi_rbuf *rbuf = arg;
    553 	struct wpi_softc *sc = rbuf->sc;
    554 
    555 	/* put the buffer back in the free list */
    556 
    557 	mutex_enter(&sc->rxq.freelist_mtx);
    558 	SLIST_INSERT_HEAD(&sc->rxq.freelist, rbuf, next);
    559 	mutex_exit(&sc->rxq.freelist_mtx);
    560 	/* No need to protect this with a mutex, see wpi_rx_intr */
    561 	sc->rxq.nb_free_entries ++;
    562 
    563 	if (__predict_true(m != NULL))
    564 		pool_cache_put(mb_cache, m);
    565 }
    566 
    567 static int
    568 wpi_alloc_rpool(struct wpi_softc *sc)
    569 {
    570 	struct wpi_rx_ring *ring = &sc->rxq;
    571 	struct wpi_rbuf *rbuf;
    572 	int i, error;
    573 
    574 	/* allocate a big chunk of DMA'able memory.. */
    575 	error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->buf_dma, NULL,
    576 	    WPI_RBUF_COUNT * WPI_RBUF_SIZE, WPI_BUF_ALIGN, BUS_DMA_NOWAIT);
    577 	if (error != 0) {
    578 		aprint_normal_dev(sc->sc_dev,
    579 						  "could not allocate Rx buffers DMA memory\n");
    580 		return error;
    581 	}
    582 
    583 	/* ..and split it into 3KB chunks */
    584 	mutex_init(&ring->freelist_mtx, MUTEX_DEFAULT, IPL_NET);
    585 	SLIST_INIT(&ring->freelist);
    586 	for (i = 0; i < WPI_RBUF_COUNT; i++) {
    587 		rbuf = &ring->rbuf[i];
    588 		rbuf->sc = sc;	/* backpointer for callbacks */
    589 		rbuf->vaddr = (char *)ring->buf_dma.vaddr + i * WPI_RBUF_SIZE;
    590 		rbuf->paddr = ring->buf_dma.paddr + i * WPI_RBUF_SIZE;
    591 
    592 		SLIST_INSERT_HEAD(&ring->freelist, rbuf, next);
    593 	}
    594 
    595 	ring->nb_free_entries = WPI_RBUF_COUNT;
    596 	return 0;
    597 }
    598 
    599 static void
    600 wpi_free_rpool(struct wpi_softc *sc)
    601 {
    602 	wpi_dma_contig_free(&sc->rxq.buf_dma);
    603 }
    604 
    605 static int
    606 wpi_alloc_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
    607 {
    608 	struct wpi_rx_data *data;
    609 	struct wpi_rbuf *rbuf;
    610 	int i, error;
    611 
    612 	ring->cur = 0;
    613 
    614 	error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->desc_dma,
    615 		(void **)&ring->desc,
    616 		WPI_RX_RING_COUNT * sizeof (struct wpi_rx_desc),
    617 		WPI_RING_DMA_ALIGN, BUS_DMA_NOWAIT);
    618 	if (error != 0) {
    619 		aprint_error_dev(sc->sc_dev, "could not allocate rx ring DMA memory\n");
    620 		goto fail;
    621 	}
    622 
    623 	/*
    624 	 * Setup Rx buffers.
    625 	 */
    626 	for (i = 0; i < WPI_RX_RING_COUNT; i++) {
    627 		data = &ring->data[i];
    628 
    629 		MGETHDR(data->m, M_DONTWAIT, MT_DATA);
    630 		if (data->m == NULL) {
    631 			aprint_error_dev(sc->sc_dev, "could not allocate rx mbuf\n");
    632 			error = ENOMEM;
    633 			goto fail;
    634 		}
    635 		if ((rbuf = wpi_alloc_rbuf(sc)) == NULL) {
    636 			m_freem(data->m);
    637 			data->m = NULL;
    638 			aprint_error_dev(sc->sc_dev, "could not allocate rx cluster\n");
    639 			error = ENOMEM;
    640 			goto fail;
    641 		}
    642 		/* attach Rx buffer to mbuf */
    643 		MEXTADD(data->m, rbuf->vaddr, WPI_RBUF_SIZE, 0, wpi_free_rbuf,
    644 		    rbuf);
    645 		data->m->m_flags |= M_EXT_RW;
    646 
    647 		ring->desc[i] = htole32(rbuf->paddr);
    648 	}
    649 
    650 	return 0;
    651 
    652 fail:	wpi_free_rx_ring(sc, ring);
    653 	return error;
    654 }
    655 
    656 static void
    657 wpi_reset_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
    658 {
    659 	int ntries;
    660 
    661 	wpi_mem_lock(sc);
    662 
    663 	WPI_WRITE(sc, WPI_RX_CONFIG, 0);
    664 	for (ntries = 0; ntries < 100; ntries++) {
    665 		if (WPI_READ(sc, WPI_RX_STATUS) & WPI_RX_IDLE)
    666 			break;
    667 		DELAY(10);
    668 	}
    669 #ifdef WPI_DEBUG
    670 	if (ntries == 100 && wpi_debug > 0)
    671 		aprint_error_dev(sc->sc_dev, "timeout resetting Rx ring\n");
    672 #endif
    673 	wpi_mem_unlock(sc);
    674 
    675 	ring->cur = 0;
    676 }
    677 
    678 static void
    679 wpi_free_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
    680 {
    681 	int i;
    682 
    683 	wpi_dma_contig_free(&ring->desc_dma);
    684 
    685 	for (i = 0; i < WPI_RX_RING_COUNT; i++) {
    686 		if (ring->data[i].m != NULL)
    687 			m_freem(ring->data[i].m);
    688 	}
    689 }
    690 
    691 static int
    692 wpi_alloc_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring, int count,
    693 	int qid)
    694 {
    695 	struct wpi_tx_data *data;
    696 	int i, error;
    697 
    698 	ring->qid = qid;
    699 	ring->count = count;
    700 	ring->queued = 0;
    701 	ring->cur = 0;
    702 
    703 	error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->desc_dma,
    704 		(void **)&ring->desc, count * sizeof (struct wpi_tx_desc),
    705 		WPI_RING_DMA_ALIGN, BUS_DMA_NOWAIT);
    706 	if (error != 0) {
    707 		aprint_error_dev(sc->sc_dev, "could not allocate tx ring DMA memory\n");
    708 		goto fail;
    709 	}
    710 
    711 	/* update shared page with ring's base address */
    712 	sc->shared->txbase[qid] = htole32(ring->desc_dma.paddr);
    713 
    714 	error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->cmd_dma,
    715 		(void **)&ring->cmd,
    716 		count * sizeof (struct wpi_tx_cmd), 4, BUS_DMA_NOWAIT);
    717 	if (error != 0) {
    718 		aprint_error_dev(sc->sc_dev, "could not allocate tx cmd DMA memory\n");
    719 		goto fail;
    720 	}
    721 
    722 	ring->data = malloc(count * sizeof (struct wpi_tx_data), M_DEVBUF,
    723 		M_NOWAIT);
    724 	if (ring->data == NULL) {
    725 		aprint_error_dev(sc->sc_dev, "could not allocate tx data slots\n");
    726 		goto fail;
    727 	}
    728 
    729 	memset(ring->data, 0, count * sizeof (struct wpi_tx_data));
    730 
    731 	for (i = 0; i < count; i++) {
    732 		data = &ring->data[i];
    733 
    734 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    735 			WPI_MAX_SCATTER - 1, MCLBYTES, 0, BUS_DMA_NOWAIT,
    736 			&data->map);
    737 		if (error != 0) {
    738 			aprint_error_dev(sc->sc_dev, "could not create tx buf DMA map\n");
    739 			goto fail;
    740 		}
    741 	}
    742 
    743 	return 0;
    744 
    745 fail:	wpi_free_tx_ring(sc, ring);
    746 	return error;
    747 }
    748 
    749 static void
    750 wpi_reset_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
    751 {
    752 	struct wpi_tx_data *data;
    753 	int i, ntries;
    754 
    755 	wpi_mem_lock(sc);
    756 
    757 	WPI_WRITE(sc, WPI_TX_CONFIG(ring->qid), 0);
    758 	for (ntries = 0; ntries < 100; ntries++) {
    759 		if (WPI_READ(sc, WPI_TX_STATUS) & WPI_TX_IDLE(ring->qid))
    760 			break;
    761 		DELAY(10);
    762 	}
    763 #ifdef WPI_DEBUG
    764 	if (ntries == 100 && wpi_debug > 0) {
    765 		aprint_error_dev(sc->sc_dev, "timeout resetting Tx ring %d\n",
    766 									   ring->qid);
    767 	}
    768 #endif
    769 	wpi_mem_unlock(sc);
    770 
    771 	for (i = 0; i < ring->count; i++) {
    772 		data = &ring->data[i];
    773 
    774 		if (data->m != NULL) {
    775 			bus_dmamap_unload(sc->sc_dmat, data->map);
    776 			m_freem(data->m);
    777 			data->m = NULL;
    778 		}
    779 	}
    780 
    781 	ring->queued = 0;
    782 	ring->cur = 0;
    783 }
    784 
    785 static void
    786 wpi_free_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
    787 {
    788 	struct wpi_tx_data *data;
    789 	int i;
    790 
    791 	wpi_dma_contig_free(&ring->desc_dma);
    792 	wpi_dma_contig_free(&ring->cmd_dma);
    793 
    794 	if (ring->data != NULL) {
    795 		for (i = 0; i < ring->count; i++) {
    796 			data = &ring->data[i];
    797 
    798 			if (data->m != NULL) {
    799 				bus_dmamap_unload(sc->sc_dmat, data->map);
    800 				m_freem(data->m);
    801 			}
    802 		}
    803 		free(ring->data, M_DEVBUF);
    804 	}
    805 }
    806 
    807 /*ARGUSED*/
    808 static struct ieee80211_node *
    809 wpi_node_alloc(struct ieee80211_node_table *nt __unused)
    810 {
    811 	struct wpi_node *wn;
    812 
    813 	wn = malloc(sizeof (struct wpi_node), M_80211_NODE, M_NOWAIT | M_ZERO);
    814 
    815 	return (struct ieee80211_node *)wn;
    816 }
    817 
    818 static void
    819 wpi_newassoc(struct ieee80211_node *ni, int isnew)
    820 {
    821 	struct wpi_softc *sc = ni->ni_ic->ic_ifp->if_softc;
    822 	int i;
    823 
    824 	ieee80211_amrr_node_init(&sc->amrr, &((struct wpi_node *)ni)->amn);
    825 
    826 	/* set rate to some reasonable initial value */
    827 	for (i = ni->ni_rates.rs_nrates - 1;
    828 	     i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72;
    829 	     i--);
    830 	ni->ni_txrate = i;
    831 }
    832 
    833 static int
    834 wpi_media_change(struct ifnet *ifp)
    835 {
    836 	int error;
    837 
    838 	error = ieee80211_media_change(ifp);
    839 	if (error != ENETRESET)
    840 		return error;
    841 
    842 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
    843 		wpi_init(ifp);
    844 
    845 	return 0;
    846 }
    847 
    848 static int
    849 wpi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
    850 {
    851 	struct ifnet *ifp = ic->ic_ifp;
    852 	struct wpi_softc *sc = ifp->if_softc;
    853 	struct ieee80211_node *ni;
    854 	int error;
    855 
    856 	callout_stop(&sc->calib_to);
    857 
    858 	switch (nstate) {
    859 	case IEEE80211_S_SCAN:
    860 
    861 		if (sc->is_scanning)
    862 			break;
    863 
    864 		sc->is_scanning = true;
    865 		ieee80211_node_table_reset(&ic->ic_scan);
    866 		ic->ic_flags |= IEEE80211_F_SCAN | IEEE80211_F_ASCAN;
    867 
    868 		/* make the link LED blink while we're scanning */
    869 		wpi_set_led(sc, WPI_LED_LINK, 20, 2);
    870 
    871 		if ((error = wpi_scan(sc, IEEE80211_CHAN_G)) != 0) {
    872 			aprint_error_dev(sc->sc_dev, "could not initiate scan\n");
    873 			ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN);
    874 			return error;
    875 		}
    876 
    877 		ic->ic_state = nstate;
    878 		return 0;
    879 
    880 	case IEEE80211_S_ASSOC:
    881 		if (ic->ic_state != IEEE80211_S_RUN)
    882 			break;
    883 		/* FALLTHROUGH */
    884 	case IEEE80211_S_AUTH:
    885 		sc->config.associd = 0;
    886 		sc->config.filter &= ~htole32(WPI_FILTER_BSS);
    887 		if ((error = wpi_auth(sc)) != 0) {
    888 			aprint_error_dev(sc->sc_dev,
    889 							"could not send authentication request\n");
    890 			return error;
    891 		}
    892 		break;
    893 
    894 	case IEEE80211_S_RUN:
    895 		if (ic->ic_opmode == IEEE80211_M_MONITOR) {
    896 			/* link LED blinks while monitoring */
    897 			wpi_set_led(sc, WPI_LED_LINK, 5, 5);
    898 			break;
    899 		}
    900 
    901 		ni = ic->ic_bss;
    902 
    903 		if (ic->ic_opmode != IEEE80211_M_STA) {
    904 			(void) wpi_auth(sc);    /* XXX */
    905 			wpi_setup_beacon(sc, ni);
    906 		}
    907 
    908 		wpi_enable_tsf(sc, ni);
    909 
    910 		/* update adapter's configuration */
    911 		sc->config.associd = htole16(ni->ni_associd & ~0xc000);
    912 		/* short preamble/slot time are negotiated when associating */
    913 		sc->config.flags &= ~htole32(WPI_CONFIG_SHPREAMBLE |
    914 			WPI_CONFIG_SHSLOT);
    915 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
    916 			sc->config.flags |= htole32(WPI_CONFIG_SHSLOT);
    917 		if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
    918 			sc->config.flags |= htole32(WPI_CONFIG_SHPREAMBLE);
    919 		sc->config.filter |= htole32(WPI_FILTER_BSS);
    920 		if (ic->ic_opmode != IEEE80211_M_STA)
    921 			sc->config.filter |= htole32(WPI_FILTER_BEACON);
    922 
    923 /* XXX put somewhere HC_QOS_SUPPORT_ASSOC + HC_IBSS_START */
    924 
    925 		DPRINTF(("config chan %d flags %x\n", sc->config.chan,
    926 			sc->config.flags));
    927 		error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config,
    928 			sizeof (struct wpi_config), 1);
    929 		if (error != 0) {
    930 			aprint_error_dev(sc->sc_dev, "could not update configuration\n");
    931 			return error;
    932 		}
    933 
    934 		/* configuration has changed, set Tx power accordingly */
    935 		if ((error = wpi_set_txpower(sc, ni->ni_chan, 1)) != 0) {
    936 			aprint_error_dev(sc->sc_dev, "could not set Tx power\n");
    937 			return error;
    938 		}
    939 
    940 		if (ic->ic_opmode == IEEE80211_M_STA) {
    941 			/* fake a join to init the tx rate */
    942 			wpi_newassoc(ni, 1);
    943 		}
    944 
    945 		/* start periodic calibration timer */
    946 		sc->calib_cnt = 0;
    947 		callout_schedule(&sc->calib_to, hz/2);
    948 
    949 		/* link LED always on while associated */
    950 		wpi_set_led(sc, WPI_LED_LINK, 0, 1);
    951 		break;
    952 
    953 	case IEEE80211_S_INIT:
    954 		sc->is_scanning = false;
    955 		break;
    956 	}
    957 
    958 	return sc->sc_newstate(ic, nstate, arg);
    959 }
    960 
    961 /*
    962  * XXX: Hack to set the current channel to the value advertised in beacons or
    963  * probe responses. Only used during AP detection.
    964  * XXX: Duplicated from if_iwi.c
    965  */
    966 static void
    967 wpi_fix_channel(struct ieee80211com *ic, struct mbuf *m)
    968 {
    969 	struct ieee80211_frame *wh;
    970 	uint8_t subtype;
    971 	uint8_t *frm, *efrm;
    972 
    973 	wh = mtod(m, struct ieee80211_frame *);
    974 
    975 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
    976 		return;
    977 
    978 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
    979 
    980 	if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
    981 	    subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
    982 		return;
    983 
    984 	frm = (uint8_t *)(wh + 1);
    985 	efrm = mtod(m, uint8_t *) + m->m_len;
    986 
    987 	frm += 12;	/* skip tstamp, bintval and capinfo fields */
    988 	while (frm < efrm) {
    989 		if (*frm == IEEE80211_ELEMID_DSPARMS)
    990 #if IEEE80211_CHAN_MAX < 255
    991 		if (frm[2] <= IEEE80211_CHAN_MAX)
    992 #endif
    993 			ic->ic_curchan = &ic->ic_channels[frm[2]];
    994 
    995 		frm += frm[1] + 2;
    996 	}
    997 }
    998 
    999 /*
   1000  * Grab exclusive access to NIC memory.
   1001  */
   1002 static void
   1003 wpi_mem_lock(struct wpi_softc *sc)
   1004 {
   1005 	uint32_t tmp;
   1006 	int ntries;
   1007 
   1008 	tmp = WPI_READ(sc, WPI_GPIO_CTL);
   1009 	WPI_WRITE(sc, WPI_GPIO_CTL, tmp | WPI_GPIO_MAC);
   1010 
   1011 	/* spin until we actually get the lock */
   1012 	for (ntries = 0; ntries < 1000; ntries++) {
   1013 		if ((WPI_READ(sc, WPI_GPIO_CTL) &
   1014 			(WPI_GPIO_CLOCK | WPI_GPIO_SLEEP)) == WPI_GPIO_CLOCK)
   1015 			break;
   1016 		DELAY(10);
   1017 	}
   1018 	if (ntries == 1000)
   1019 		aprint_error_dev(sc->sc_dev, "could not lock memory\n");
   1020 }
   1021 
   1022 /*
   1023  * Release lock on NIC memory.
   1024  */
   1025 static void
   1026 wpi_mem_unlock(struct wpi_softc *sc)
   1027 {
   1028 	uint32_t tmp = WPI_READ(sc, WPI_GPIO_CTL);
   1029 	WPI_WRITE(sc, WPI_GPIO_CTL, tmp & ~WPI_GPIO_MAC);
   1030 }
   1031 
   1032 static uint32_t
   1033 wpi_mem_read(struct wpi_softc *sc, uint16_t addr)
   1034 {
   1035 	WPI_WRITE(sc, WPI_READ_MEM_ADDR, WPI_MEM_4 | addr);
   1036 	return WPI_READ(sc, WPI_READ_MEM_DATA);
   1037 }
   1038 
   1039 static void
   1040 wpi_mem_write(struct wpi_softc *sc, uint16_t addr, uint32_t data)
   1041 {
   1042 	WPI_WRITE(sc, WPI_WRITE_MEM_ADDR, WPI_MEM_4 | addr);
   1043 	WPI_WRITE(sc, WPI_WRITE_MEM_DATA, data);
   1044 }
   1045 
   1046 static void
   1047 wpi_mem_write_region_4(struct wpi_softc *sc, uint16_t addr,
   1048 						const uint32_t *data, int wlen)
   1049 {
   1050 	for (; wlen > 0; wlen--, data++, addr += 4)
   1051 		wpi_mem_write(sc, addr, *data);
   1052 }
   1053 
   1054 
   1055 /*
   1056  * Read `len' bytes from the EEPROM.  We access the EEPROM through the MAC
   1057  * instead of using the traditional bit-bang method.
   1058  */
   1059 static int
   1060 wpi_read_prom_data(struct wpi_softc *sc, uint32_t addr, void *data, int len)
   1061 {
   1062 	uint8_t *out = data;
   1063 	uint32_t val;
   1064 	int ntries;
   1065 
   1066 	wpi_mem_lock(sc);
   1067 	for (; len > 0; len -= 2, addr++) {
   1068 		WPI_WRITE(sc, WPI_EEPROM_CTL, addr << 2);
   1069 
   1070 		for (ntries = 0; ntries < 10; ntries++) {
   1071 			if ((val = WPI_READ(sc, WPI_EEPROM_CTL)) &
   1072 			    WPI_EEPROM_READY)
   1073 				break;
   1074 			DELAY(5);
   1075 		}
   1076 		if (ntries == 10) {
   1077 			aprint_error_dev(sc->sc_dev, "could not read EEPROM\n");
   1078 			return ETIMEDOUT;
   1079 		}
   1080 		*out++ = val >> 16;
   1081 		if (len > 1)
   1082 			*out++ = val >> 24;
   1083 	}
   1084 	wpi_mem_unlock(sc);
   1085 
   1086 	return 0;
   1087 }
   1088 
   1089 /*
   1090  * The firmware boot code is small and is intended to be copied directly into
   1091  * the NIC internal memory.
   1092  */
   1093 int
   1094 wpi_load_microcode(struct wpi_softc *sc, const uint8_t *ucode, int size)
   1095 {
   1096 	int ntries;
   1097 
   1098 	size /= sizeof (uint32_t);
   1099 
   1100 	wpi_mem_lock(sc);
   1101 
   1102 	/* copy microcode image into NIC memory */
   1103 	wpi_mem_write_region_4(sc, WPI_MEM_UCODE_BASE,
   1104 	    (const uint32_t *)ucode, size);
   1105 
   1106 	wpi_mem_write(sc, WPI_MEM_UCODE_SRC, 0);
   1107 	wpi_mem_write(sc, WPI_MEM_UCODE_DST, WPI_FW_TEXT);
   1108 	wpi_mem_write(sc, WPI_MEM_UCODE_SIZE, size);
   1109 
   1110 	/* run microcode */
   1111 	wpi_mem_write(sc, WPI_MEM_UCODE_CTL, WPI_UC_RUN);
   1112 
   1113 	/* wait for transfer to complete */
   1114 	for (ntries = 0; ntries < 1000; ntries++) {
   1115 		if (!(wpi_mem_read(sc, WPI_MEM_UCODE_CTL) & WPI_UC_RUN))
   1116 			break;
   1117 		DELAY(10);
   1118 	}
   1119 	if (ntries == 1000) {
   1120 		wpi_mem_unlock(sc);
   1121 		aprint_error_dev(sc->sc_dev, "could not load boot firmware\n");
   1122 		return ETIMEDOUT;
   1123 	}
   1124 	wpi_mem_write(sc, WPI_MEM_UCODE_CTL, WPI_UC_ENABLE);
   1125 
   1126 	wpi_mem_unlock(sc);
   1127 
   1128 	return 0;
   1129 }
   1130 
   1131 static int
   1132 wpi_cache_firmware(struct wpi_softc *sc)
   1133 {
   1134 	firmware_handle_t fw;
   1135 	int error;
   1136 
   1137 	if (sc->fw_used)
   1138 		return 0;
   1139 
   1140 	mutex_enter(&wpi_firmware_mutex);
   1141 	if (wpi_firmware_users++) {
   1142 		mutex_exit(&wpi_firmware_mutex);
   1143 		return 0;
   1144 	}
   1145 
   1146 	/* load firmware image from disk */
   1147 	if ((error = firmware_open("if_wpi","iwlwifi-3945.ucode", &fw)) != 0) {
   1148 		aprint_error_dev(sc->sc_dev, "could not read firmware file\n");
   1149 		goto fail1;
   1150 	}
   1151 
   1152 	wpi_firmware_size = firmware_get_size(fw);
   1153 
   1154 	if (wpi_firmware_size > sizeof (struct wpi_firmware_hdr) +
   1155 	    WPI_FW_MAIN_TEXT_MAXSZ + WPI_FW_MAIN_DATA_MAXSZ +
   1156 	    WPI_FW_INIT_TEXT_MAXSZ + WPI_FW_INIT_DATA_MAXSZ +
   1157 	    WPI_FW_BOOT_TEXT_MAXSZ) {
   1158 		aprint_error_dev(sc->sc_dev, "invalid firmware file\n");
   1159 		error = EFBIG;
   1160 		goto fail1;
   1161 	}
   1162 
   1163 	if (wpi_firmware_size < sizeof (struct wpi_firmware_hdr)) {
   1164 		aprint_error_dev(sc->sc_dev,
   1165 		    "truncated firmware header: %zu bytes\n",
   1166 		    wpi_firmware_size);
   1167 		error = EINVAL;
   1168 		goto fail2;
   1169 	}
   1170 
   1171 	wpi_firmware_image = firmware_malloc(wpi_firmware_size);
   1172 	if (wpi_firmware_image == NULL) {
   1173 		aprint_error_dev(sc->sc_dev, "not enough memory to stock firmware\n");
   1174 		error = ENOMEM;
   1175 		goto fail1;
   1176 	}
   1177 
   1178 	if ((error = firmware_read(fw, 0, wpi_firmware_image, wpi_firmware_size)) != 0) {
   1179 		aprint_error_dev(sc->sc_dev, "can't get firmware\n");
   1180 		goto fail2;
   1181 	}
   1182 
   1183 	sc->fw_used = true;
   1184 	firmware_close(fw);
   1185 	mutex_exit(&wpi_firmware_mutex);
   1186 
   1187 	return 0;
   1188 
   1189 fail2:
   1190 	firmware_free(wpi_firmware_image, wpi_firmware_size);
   1191 fail1:
   1192 	firmware_close(fw);
   1193 	if (--wpi_firmware_users == 0)
   1194 		firmware_free(wpi_firmware_image, wpi_firmware_size);
   1195 	mutex_exit(&wpi_firmware_mutex);
   1196 	return error;
   1197 }
   1198 
   1199 static int
   1200 wpi_load_firmware(struct wpi_softc *sc)
   1201 {
   1202 	struct wpi_dma_info *dma = &sc->fw_dma;
   1203 	struct wpi_firmware_hdr hdr;
   1204 	const uint8_t *init_text, *init_data, *main_text, *main_data;
   1205 	const uint8_t *boot_text;
   1206 	uint32_t init_textsz, init_datasz, main_textsz, main_datasz;
   1207 	uint32_t boot_textsz;
   1208 	int error;
   1209 
   1210 	if ((error = wpi_cache_firmware(sc)) != 0)
   1211 		return error;
   1212 
   1213 	memcpy(&hdr, wpi_firmware_image, sizeof(hdr));
   1214 
   1215 	main_textsz = le32toh(hdr.main_textsz);
   1216 	main_datasz = le32toh(hdr.main_datasz);
   1217 	init_textsz = le32toh(hdr.init_textsz);
   1218 	init_datasz = le32toh(hdr.init_datasz);
   1219 	boot_textsz = le32toh(hdr.boot_textsz);
   1220 
   1221 	/* sanity-check firmware segments sizes */
   1222 	if (main_textsz > WPI_FW_MAIN_TEXT_MAXSZ ||
   1223 	    main_datasz > WPI_FW_MAIN_DATA_MAXSZ ||
   1224 	    init_textsz > WPI_FW_INIT_TEXT_MAXSZ ||
   1225 	    init_datasz > WPI_FW_INIT_DATA_MAXSZ ||
   1226 	    boot_textsz > WPI_FW_BOOT_TEXT_MAXSZ ||
   1227 	    (boot_textsz & 3) != 0) {
   1228 		aprint_error_dev(sc->sc_dev, "invalid firmware header\n");
   1229 		error = EINVAL;
   1230 		goto free_firmware;
   1231 	}
   1232 
   1233 	/* check that all firmware segments are present */
   1234 	if (wpi_firmware_size <
   1235 	    sizeof (struct wpi_firmware_hdr) + main_textsz +
   1236 	    main_datasz + init_textsz + init_datasz + boot_textsz) {
   1237 		aprint_error_dev(sc->sc_dev,
   1238 		    "firmware file too short: %zu bytes\n", wpi_firmware_size);
   1239 		error = EINVAL;
   1240 		goto free_firmware;
   1241 	}
   1242 
   1243 	/* get pointers to firmware segments */
   1244 	main_text = wpi_firmware_image + sizeof (struct wpi_firmware_hdr);
   1245 	main_data = main_text + main_textsz;
   1246 	init_text = main_data + main_datasz;
   1247 	init_data = init_text + init_textsz;
   1248 	boot_text = init_data + init_datasz;
   1249 
   1250 	/* copy initialization images into pre-allocated DMA-safe memory */
   1251 	memcpy(dma->vaddr, init_data, init_datasz);
   1252 	memcpy((char*)dma->vaddr + WPI_FW_INIT_DATA_MAXSZ, init_text, init_textsz);
   1253 
   1254 	/* tell adapter where to find initialization images */
   1255 	wpi_mem_lock(sc);
   1256 	wpi_mem_write(sc, WPI_MEM_DATA_BASE, dma->paddr);
   1257 	wpi_mem_write(sc, WPI_MEM_DATA_SIZE, init_datasz);
   1258 	wpi_mem_write(sc, WPI_MEM_TEXT_BASE,
   1259 	    dma->paddr + WPI_FW_INIT_DATA_MAXSZ);
   1260 	wpi_mem_write(sc, WPI_MEM_TEXT_SIZE, init_textsz);
   1261 	wpi_mem_unlock(sc);
   1262 
   1263 	/* load firmware boot code */
   1264 	if ((error = wpi_load_microcode(sc, boot_text, boot_textsz)) != 0) {
   1265 		aprint_error_dev(sc->sc_dev, "could not load boot firmware\n");
   1266 		return error;
   1267 	}
   1268 
   1269 	/* now press "execute" ;-) */
   1270 	WPI_WRITE(sc, WPI_RESET, 0);
   1271 
   1272 	/* ..and wait at most one second for adapter to initialize */
   1273 	if ((error = tsleep(sc, PCATCH, "wpiinit", hz)) != 0) {
   1274 		/* this isn't what was supposed to happen.. */
   1275 		aprint_error_dev(sc->sc_dev,
   1276 		    "timeout waiting for adapter to initialize\n");
   1277 	}
   1278 
   1279 	/* copy runtime images into pre-allocated DMA-safe memory */
   1280 	memcpy(dma->vaddr, main_data, main_datasz);
   1281 	memcpy((char*)dma->vaddr + WPI_FW_MAIN_DATA_MAXSZ, main_text, main_textsz);
   1282 
   1283 	/* tell adapter where to find runtime images */
   1284 	wpi_mem_lock(sc);
   1285 	wpi_mem_write(sc, WPI_MEM_DATA_BASE, dma->paddr);
   1286 	wpi_mem_write(sc, WPI_MEM_DATA_SIZE, main_datasz);
   1287 	wpi_mem_write(sc, WPI_MEM_TEXT_BASE,
   1288 	    dma->paddr + WPI_FW_MAIN_DATA_MAXSZ);
   1289 	wpi_mem_write(sc, WPI_MEM_TEXT_SIZE, WPI_FW_UPDATED | main_textsz);
   1290 	wpi_mem_unlock(sc);
   1291 
   1292 	/* wait at most one second for second alive notification */
   1293 	if ((error = tsleep(sc, PCATCH, "wpiinit", hz)) != 0) {
   1294 		/* this isn't what was supposed to happen.. */
   1295 		aprint_error_dev(sc->sc_dev,
   1296 		    "timeout waiting for adapter to initialize\n");
   1297 	}
   1298 
   1299 	return error;
   1300 
   1301 free_firmware:
   1302 	mutex_enter(&wpi_firmware_mutex);
   1303 	sc->fw_used = false;
   1304 	--wpi_firmware_users;
   1305 	mutex_exit(&wpi_firmware_mutex);
   1306 	return error;
   1307 }
   1308 
   1309 static void
   1310 wpi_calib_timeout(void *arg)
   1311 {
   1312 	struct wpi_softc *sc = arg;
   1313 	struct ieee80211com *ic = &sc->sc_ic;
   1314 	int temp, s;
   1315 
   1316 	/* automatic rate control triggered every 500ms */
   1317 	if (ic->ic_fixed_rate == -1) {
   1318 		s = splnet();
   1319 		if (ic->ic_opmode == IEEE80211_M_STA)
   1320 			wpi_iter_func(sc, ic->ic_bss);
   1321 		else
   1322                 	ieee80211_iterate_nodes(&ic->ic_sta, wpi_iter_func, sc);
   1323 		splx(s);
   1324 	}
   1325 
   1326 	/* update sensor data */
   1327 	temp = (int)WPI_READ(sc, WPI_TEMPERATURE);
   1328 
   1329 	/* automatic power calibration every 60s */
   1330 	if (++sc->calib_cnt >= 120) {
   1331 		wpi_power_calibration(sc, temp);
   1332 		sc->calib_cnt = 0;
   1333 	}
   1334 
   1335 	callout_schedule(&sc->calib_to, hz/2);
   1336 }
   1337 
   1338 static void
   1339 wpi_iter_func(void *arg, struct ieee80211_node *ni)
   1340 {
   1341 	struct wpi_softc *sc = arg;
   1342 	struct wpi_node *wn = (struct wpi_node *)ni;
   1343 
   1344 	ieee80211_amrr_choose(&sc->amrr, ni, &wn->amn);
   1345 }
   1346 
   1347 /*
   1348  * This function is called periodically (every 60 seconds) to adjust output
   1349  * power to temperature changes.
   1350  */
   1351 void
   1352 wpi_power_calibration(struct wpi_softc *sc, int temp)
   1353 {
   1354 	/* sanity-check read value */
   1355 	if (temp < -260 || temp > 25) {
   1356 		/* this can't be correct, ignore */
   1357 		DPRINTF(("out-of-range temperature reported: %d\n", temp));
   1358 		return;
   1359 	}
   1360 
   1361 	DPRINTF(("temperature %d->%d\n", sc->temp, temp));
   1362 
   1363 	/* adjust Tx power if need be */
   1364 	if (abs(temp - sc->temp) <= 6)
   1365 		return;
   1366 
   1367 	sc->temp = temp;
   1368 
   1369 	if (wpi_set_txpower(sc, sc->sc_ic.ic_bss->ni_chan, 1) != 0) {
   1370 		/* just warn, too bad for the automatic calibration... */
   1371 		aprint_error_dev(sc->sc_dev, "could not adjust Tx power\n");
   1372 	}
   1373 }
   1374 
   1375 static void
   1376 wpi_rx_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc,
   1377 	struct wpi_rx_data *data)
   1378 {
   1379 	struct ieee80211com *ic = &sc->sc_ic;
   1380 	struct ifnet *ifp = ic->ic_ifp;
   1381 	struct wpi_rx_ring *ring = &sc->rxq;
   1382 	struct wpi_rx_stat *stat;
   1383 	struct wpi_rx_head *head;
   1384 	struct wpi_rx_tail *tail;
   1385 	struct wpi_rbuf *rbuf;
   1386 	struct ieee80211_frame *wh;
   1387 	struct ieee80211_node *ni;
   1388 	struct mbuf *m, *mnew;
   1389 	int data_off ;
   1390 
   1391 	stat = (struct wpi_rx_stat *)(desc + 1);
   1392 
   1393 	if (stat->len > WPI_STAT_MAXLEN) {
   1394 		aprint_error_dev(sc->sc_dev, "invalid rx statistic header\n");
   1395 		ifp->if_ierrors++;
   1396 		return;
   1397 	}
   1398 
   1399 	head = (struct wpi_rx_head *)((char *)(stat + 1) + stat->len);
   1400 	tail = (struct wpi_rx_tail *)((char *)(head + 1) + le16toh(head->len));
   1401 
   1402 	DPRINTFN(4, ("rx intr: idx=%d len=%d stat len=%d rssi=%d rate=%x "
   1403 		"chan=%d tstamp=%" PRId64 "\n", ring->cur, le32toh(desc->len),
   1404 		le16toh(head->len), (int8_t)stat->rssi, head->rate, head->chan,
   1405 		le64toh(tail->tstamp)));
   1406 
   1407 	/*
   1408 	 * Discard Rx frames with bad CRC early (XXX we may want to pass them
   1409 	 * to radiotap in monitor mode).
   1410 	 */
   1411 	if ((le32toh(tail->flags) & WPI_RX_NOERROR) != WPI_RX_NOERROR) {
   1412 		DPRINTF(("rx tail flags error %x\n", le32toh(tail->flags)));
   1413 		ifp->if_ierrors++;
   1414 		return;
   1415 	}
   1416 
   1417 	/* Compute where are the useful datas */
   1418 	data_off = (char*)(head + 1) - mtod(data->m, char*);
   1419 
   1420 	/*
   1421 	 * If the number of free entry is too low
   1422 	 * just dup the data->m socket and reuse the same rbuf entry
   1423 	 * Note that thi test is not protected by a mutex because the
   1424 	 * only path that causes nb_free_entries to decrease is through
   1425 	 * this interrupt routine, which is not re-entrent.
   1426 	 * What may not be obvious is that the safe path is if that test
   1427 	 * evaluates as true, so nb_free_entries can grow any time.
   1428 	 */
   1429 	if (sc->rxq.nb_free_entries <= WPI_RBUF_LOW_LIMIT) {
   1430 
   1431 		/* Prepare the mbuf for the m_dup */
   1432 		data->m->m_pkthdr.len = data->m->m_len = le16toh(head->len);
   1433 		data->m->m_data = (char*) data->m->m_data + data_off;
   1434 
   1435 		m = m_dup(data->m,0,M_COPYALL,M_DONTWAIT);
   1436 
   1437 		/* Restore the m_data pointer for future use */
   1438 		data->m->m_data = (char*) data->m->m_data - data_off;
   1439 
   1440 		if (m == NULL) {
   1441 			ifp->if_ierrors++;
   1442 			return;
   1443 		}
   1444 	} else {
   1445 
   1446 		MGETHDR(mnew, M_DONTWAIT, MT_DATA);
   1447 		if (mnew == NULL) {
   1448 			ifp->if_ierrors++;
   1449 			return;
   1450 		}
   1451 
   1452 		rbuf = wpi_alloc_rbuf(sc);
   1453 		KASSERT(rbuf != NULL);
   1454 
   1455  		/* attach Rx buffer to mbuf */
   1456 		MEXTADD(mnew, rbuf->vaddr, WPI_RBUF_SIZE, 0, wpi_free_rbuf,
   1457 		 	rbuf);
   1458 		mnew->m_flags |= M_EXT_RW;
   1459 
   1460 		m = data->m;
   1461 		data->m = mnew;
   1462 
   1463 		/* update Rx descriptor */
   1464 		ring->desc[ring->cur] = htole32(rbuf->paddr);
   1465 
   1466 		m->m_data = (char*)m->m_data + data_off;
   1467 		m->m_pkthdr.len = m->m_len = le16toh(head->len);
   1468 	}
   1469 
   1470 	/* finalize mbuf */
   1471 	m->m_pkthdr.rcvif = ifp;
   1472 
   1473 	if (ic->ic_state == IEEE80211_S_SCAN)
   1474 		wpi_fix_channel(ic, m);
   1475 
   1476 	if (sc->sc_drvbpf != NULL) {
   1477 		struct wpi_rx_radiotap_header *tap = &sc->sc_rxtap;
   1478 
   1479 		tap->wr_flags = 0;
   1480 		tap->wr_chan_freq =
   1481 			htole16(ic->ic_channels[head->chan].ic_freq);
   1482 		tap->wr_chan_flags =
   1483 			htole16(ic->ic_channels[head->chan].ic_flags);
   1484 		tap->wr_dbm_antsignal = (int8_t)(stat->rssi - WPI_RSSI_OFFSET);
   1485 		tap->wr_dbm_antnoise = (int8_t)le16toh(stat->noise);
   1486 		tap->wr_tsft = tail->tstamp;
   1487 		tap->wr_antenna = (le16toh(head->flags) >> 4) & 0xf;
   1488 		switch (head->rate) {
   1489 		/* CCK rates */
   1490 		case  10: tap->wr_rate =   2; break;
   1491 		case  20: tap->wr_rate =   4; break;
   1492 		case  55: tap->wr_rate =  11; break;
   1493 		case 110: tap->wr_rate =  22; break;
   1494 		/* OFDM rates */
   1495 		case 0xd: tap->wr_rate =  12; break;
   1496 		case 0xf: tap->wr_rate =  18; break;
   1497 		case 0x5: tap->wr_rate =  24; break;
   1498 		case 0x7: tap->wr_rate =  36; break;
   1499 		case 0x9: tap->wr_rate =  48; break;
   1500 		case 0xb: tap->wr_rate =  72; break;
   1501 		case 0x1: tap->wr_rate =  96; break;
   1502 		case 0x3: tap->wr_rate = 108; break;
   1503 		/* unknown rate: should not happen */
   1504 		default:  tap->wr_rate =   0;
   1505 		}
   1506 		if (le16toh(head->flags) & 0x4)
   1507 			tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
   1508 
   1509 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
   1510 	}
   1511 
   1512 	/* grab a reference to the source node */
   1513 	wh = mtod(m, struct ieee80211_frame *);
   1514 	ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
   1515 
   1516 	/* send the frame to the 802.11 layer */
   1517 	ieee80211_input(ic, m, ni, stat->rssi, 0);
   1518 
   1519 	/* release node reference */
   1520 	ieee80211_free_node(ni);
   1521 }
   1522 
   1523 static void
   1524 wpi_tx_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc)
   1525 {
   1526 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
   1527 	struct wpi_tx_ring *ring = &sc->txq[desc->qid & 0x3];
   1528 	struct wpi_tx_data *txdata = &ring->data[desc->idx];
   1529 	struct wpi_tx_stat *stat = (struct wpi_tx_stat *)(desc + 1);
   1530 	struct wpi_node *wn = (struct wpi_node *)txdata->ni;
   1531 
   1532 	DPRINTFN(4, ("tx done: qid=%d idx=%d retries=%d nkill=%d rate=%x "
   1533 		"duration=%d status=%x\n", desc->qid, desc->idx, stat->ntries,
   1534 		stat->nkill, stat->rate, le32toh(stat->duration),
   1535 		le32toh(stat->status)));
   1536 
   1537 	/*
   1538 	 * Update rate control statistics for the node.
   1539 	 * XXX we should not count mgmt frames since they're always sent at
   1540 	 * the lowest available bit-rate.
   1541 	 */
   1542 	wn->amn.amn_txcnt++;
   1543 	if (stat->ntries > 0) {
   1544 		DPRINTFN(3, ("tx intr ntries %d\n", stat->ntries));
   1545 		wn->amn.amn_retrycnt++;
   1546 	}
   1547 
   1548 	if ((le32toh(stat->status) & 0xff) != 1)
   1549 		ifp->if_oerrors++;
   1550 	else
   1551 		ifp->if_opackets++;
   1552 
   1553 	bus_dmamap_unload(sc->sc_dmat, txdata->map);
   1554 	m_freem(txdata->m);
   1555 	txdata->m = NULL;
   1556 	ieee80211_free_node(txdata->ni);
   1557 	txdata->ni = NULL;
   1558 
   1559 	ring->queued--;
   1560 
   1561 	sc->sc_tx_timer = 0;
   1562 	ifp->if_flags &= ~IFF_OACTIVE;
   1563 	wpi_start(ifp);
   1564 }
   1565 
   1566 static void
   1567 wpi_cmd_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc)
   1568 {
   1569 	struct wpi_tx_ring *ring = &sc->cmdq;
   1570 	struct wpi_tx_data *data;
   1571 
   1572 	if ((desc->qid & 7) != 4)
   1573 		return;	/* not a command ack */
   1574 
   1575 	data = &ring->data[desc->idx];
   1576 
   1577 	/* if the command was mapped in a mbuf, free it */
   1578 	if (data->m != NULL) {
   1579 		bus_dmamap_unload(sc->sc_dmat, data->map);
   1580 		m_freem(data->m);
   1581 		data->m = NULL;
   1582 	}
   1583 
   1584 	wakeup(&ring->cmd[desc->idx]);
   1585 }
   1586 
   1587 static void
   1588 wpi_notif_intr(struct wpi_softc *sc)
   1589 {
   1590 	struct ieee80211com *ic = &sc->sc_ic;
   1591 	struct ifnet *ifp =  ic->ic_ifp;
   1592 	struct wpi_rx_desc *desc;
   1593 	struct wpi_rx_data *data;
   1594 	uint32_t hw;
   1595 
   1596 	hw = le32toh(sc->shared->next);
   1597 	while (sc->rxq.cur != hw) {
   1598 		data = &sc->rxq.data[sc->rxq.cur];
   1599 
   1600 		desc = mtod(data->m, struct wpi_rx_desc *);
   1601 
   1602 		DPRINTFN(4, ("rx notification qid=%x idx=%d flags=%x type=%d "
   1603 			"len=%d\n", desc->qid, desc->idx, desc->flags,
   1604 			desc->type, le32toh(desc->len)));
   1605 
   1606 		if (!(desc->qid & 0x80))	/* reply to a command */
   1607 			wpi_cmd_intr(sc, desc);
   1608 
   1609 		switch (desc->type) {
   1610 		case WPI_RX_DONE:
   1611 			/* a 802.11 frame was received */
   1612 			wpi_rx_intr(sc, desc, data);
   1613 			break;
   1614 
   1615 		case WPI_TX_DONE:
   1616 			/* a 802.11 frame has been transmitted */
   1617 			wpi_tx_intr(sc, desc);
   1618 			break;
   1619 
   1620 		case WPI_UC_READY:
   1621 		{
   1622 			struct wpi_ucode_info *uc =
   1623 				(struct wpi_ucode_info *)(desc + 1);
   1624 
   1625 			/* the microcontroller is ready */
   1626 			DPRINTF(("microcode alive notification version %x "
   1627 				"alive %x\n", le32toh(uc->version),
   1628 				le32toh(uc->valid)));
   1629 
   1630 			if (le32toh(uc->valid) != 1) {
   1631 				aprint_error_dev(sc->sc_dev,
   1632 					"microcontroller initialization failed\n");
   1633 			}
   1634 			break;
   1635 		}
   1636 		case WPI_STATE_CHANGED:
   1637 		{
   1638 			uint32_t *status = (uint32_t *)(desc + 1);
   1639 
   1640 			/* enabled/disabled notification */
   1641 			DPRINTF(("state changed to %x\n", le32toh(*status)));
   1642 
   1643 			if (le32toh(*status) & 1) {
   1644 				/* the radio button has to be pushed */
   1645 				aprint_error_dev(sc->sc_dev, "Radio transmitter is off\n");
   1646 				/* turn the interface down */
   1647 				ifp->if_flags &= ~IFF_UP;
   1648 				wpi_stop(ifp, 1);
   1649 				return;	/* no further processing */
   1650 			}
   1651 			break;
   1652 		}
   1653 		case WPI_START_SCAN:
   1654 		{
   1655 			struct wpi_start_scan *scan =
   1656 				(struct wpi_start_scan *)(desc + 1);
   1657 
   1658 			DPRINTFN(2, ("scanning channel %d status %x\n",
   1659 				scan->chan, le32toh(scan->status)));
   1660 
   1661 			/* fix current channel */
   1662 			ic->ic_bss->ni_chan = &ic->ic_channels[scan->chan];
   1663 			break;
   1664 		}
   1665 		case WPI_STOP_SCAN:
   1666 		{
   1667 			struct wpi_stop_scan *scan =
   1668 				(struct wpi_stop_scan *)(desc + 1);
   1669 
   1670 			DPRINTF(("scan finished nchan=%d status=%d chan=%d\n",
   1671 				scan->nchan, scan->status, scan->chan));
   1672 
   1673 			if (scan->status == 1 && scan->chan <= 14) {
   1674 				/*
   1675 				 * We just finished scanning 802.11g channels,
   1676 				 * start scanning 802.11a ones.
   1677 				 */
   1678 				if (wpi_scan(sc, IEEE80211_CHAN_A) == 0)
   1679 					break;
   1680 			}
   1681 			sc->is_scanning = false;
   1682 			ieee80211_end_scan(ic);
   1683 			break;
   1684 		}
   1685 		}
   1686 
   1687 		sc->rxq.cur = (sc->rxq.cur + 1) % WPI_RX_RING_COUNT;
   1688 	}
   1689 
   1690 	/* tell the firmware what we have processed */
   1691 	hw = (hw == 0) ? WPI_RX_RING_COUNT - 1 : hw - 1;
   1692 	WPI_WRITE(sc, WPI_RX_WIDX, hw & ~7);
   1693 }
   1694 
   1695 static int
   1696 wpi_intr(void *arg)
   1697 {
   1698 	struct wpi_softc *sc = arg;
   1699 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
   1700 	uint32_t r;
   1701 
   1702 	r = WPI_READ(sc, WPI_INTR);
   1703 	if (r == 0 || r == 0xffffffff)
   1704 		return 0;	/* not for us */
   1705 
   1706 	DPRINTFN(5, ("interrupt reg %x\n", r));
   1707 
   1708 	/* disable interrupts */
   1709 	WPI_WRITE(sc, WPI_MASK, 0);
   1710 	/* ack interrupts */
   1711 	WPI_WRITE(sc, WPI_INTR, r);
   1712 
   1713 	if (r & (WPI_SW_ERROR | WPI_HW_ERROR)) {
   1714 		aprint_error_dev(sc->sc_dev, "fatal firmware error\n");
   1715 		sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP;
   1716 		wpi_stop(sc->sc_ic.ic_ifp, 1);
   1717 		return 1;
   1718 	}
   1719 
   1720 	if (r & WPI_RX_INTR)
   1721 		wpi_notif_intr(sc);
   1722 
   1723 	if (r & WPI_ALIVE_INTR)	/* firmware initialized */
   1724 		wakeup(sc);
   1725 
   1726 	/* re-enable interrupts */
   1727 	if (ifp->if_flags & IFF_UP)
   1728 		WPI_WRITE(sc, WPI_MASK, WPI_INTR_MASK);
   1729 
   1730 	return 1;
   1731 }
   1732 
   1733 static uint8_t
   1734 wpi_plcp_signal(int rate)
   1735 {
   1736 	switch (rate) {
   1737 	/* CCK rates (returned values are device-dependent) */
   1738 	case 2:		return 10;
   1739 	case 4:		return 20;
   1740 	case 11:	return 55;
   1741 	case 22:	return 110;
   1742 
   1743 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
   1744 	/* R1-R4, (u)ral is R4-R1 */
   1745 	case 12:	return 0xd;
   1746 	case 18:	return 0xf;
   1747 	case 24:	return 0x5;
   1748 	case 36:	return 0x7;
   1749 	case 48:	return 0x9;
   1750 	case 72:	return 0xb;
   1751 	case 96:	return 0x1;
   1752 	case 108:	return 0x3;
   1753 
   1754 	/* unsupported rates (should not get there) */
   1755 	default:	return 0;
   1756 	}
   1757 }
   1758 
   1759 /* quickly determine if a given rate is CCK or OFDM */
   1760 #define WPI_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
   1761 
   1762 static int
   1763 wpi_tx_data(struct wpi_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
   1764 	int ac)
   1765 {
   1766 	struct ieee80211com *ic = &sc->sc_ic;
   1767 	struct wpi_tx_ring *ring = &sc->txq[ac];
   1768 	struct wpi_tx_desc *desc;
   1769 	struct wpi_tx_data *data;
   1770 	struct wpi_tx_cmd *cmd;
   1771 	struct wpi_cmd_data *tx;
   1772 	struct ieee80211_frame *wh;
   1773 	struct ieee80211_key *k;
   1774 	const struct chanAccParams *cap;
   1775 	struct mbuf *mnew;
   1776 	int i, error, rate, hdrlen, noack = 0;
   1777 
   1778 	desc = &ring->desc[ring->cur];
   1779 	data = &ring->data[ring->cur];
   1780 
   1781 	wh = mtod(m0, struct ieee80211_frame *);
   1782 
   1783 	if (IEEE80211_QOS_HAS_SEQ(wh)) {
   1784 		cap = &ic->ic_wme.wme_chanParams;
   1785 		noack = cap->cap_wmeParams[ac].wmep_noackPolicy;
   1786 	}
   1787 
   1788 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
   1789 		k = ieee80211_crypto_encap(ic, ni, m0);
   1790 		if (k == NULL) {
   1791 			m_freem(m0);
   1792 			return ENOBUFS;
   1793 		}
   1794 
   1795 		/* packet header may have moved, reset our local pointer */
   1796 		wh = mtod(m0, struct ieee80211_frame *);
   1797 	}
   1798 
   1799 	hdrlen = ieee80211_anyhdrsize(wh);
   1800 
   1801 	/* pickup a rate */
   1802 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
   1803 		IEEE80211_FC0_TYPE_MGT) {
   1804 		/* mgmt frames are sent at the lowest available bit-rate */
   1805 		rate = ni->ni_rates.rs_rates[0];
   1806 	} else {
   1807 		if (ic->ic_fixed_rate != -1) {
   1808 			rate = ic->ic_sup_rates[ic->ic_curmode].
   1809 				rs_rates[ic->ic_fixed_rate];
   1810 		} else
   1811 			rate = ni->ni_rates.rs_rates[ni->ni_txrate];
   1812 	}
   1813 	rate &= IEEE80211_RATE_VAL;
   1814 
   1815 
   1816 	if (sc->sc_drvbpf != NULL) {
   1817 		struct wpi_tx_radiotap_header *tap = &sc->sc_txtap;
   1818 
   1819 		tap->wt_flags = 0;
   1820 		tap->wt_chan_freq = htole16(ni->ni_chan->ic_freq);
   1821 		tap->wt_chan_flags = htole16(ni->ni_chan->ic_flags);
   1822 		tap->wt_rate = rate;
   1823 		tap->wt_hwqueue = ac;
   1824 		if (wh->i_fc[1] & IEEE80211_FC1_WEP)
   1825 			tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
   1826 
   1827 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
   1828 	}
   1829 
   1830 	cmd = &ring->cmd[ring->cur];
   1831 	cmd->code = WPI_CMD_TX_DATA;
   1832 	cmd->flags = 0;
   1833 	cmd->qid = ring->qid;
   1834 	cmd->idx = ring->cur;
   1835 
   1836 	tx = (struct wpi_cmd_data *)cmd->data;
   1837 	tx->flags = 0;
   1838 
   1839 	if (!noack && !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
   1840 		tx->flags |= htole32(WPI_TX_NEED_ACK);
   1841 	} else if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > ic->ic_rtsthreshold)
   1842 		tx->flags |= htole32(WPI_TX_NEED_RTS | WPI_TX_FULL_TXOP);
   1843 
   1844 	tx->flags |= htole32(WPI_TX_AUTO_SEQ);
   1845 
   1846 	/* retrieve destination node's id */
   1847 	tx->id = IEEE80211_IS_MULTICAST(wh->i_addr1) ? WPI_ID_BROADCAST :
   1848 		WPI_ID_BSS;
   1849 
   1850 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
   1851 		IEEE80211_FC0_TYPE_MGT) {
   1852 		/* tell h/w to set timestamp in probe responses */
   1853 		if ((wh->i_fc[0] &
   1854 		    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
   1855 		    (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
   1856 			tx->flags |= htole32(WPI_TX_INSERT_TSTAMP);
   1857 
   1858 		if (((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
   1859 			 IEEE80211_FC0_SUBTYPE_ASSOC_REQ) ||
   1860 			((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
   1861 			 IEEE80211_FC0_SUBTYPE_REASSOC_REQ))
   1862 			tx->timeout = htole16(3);
   1863 		else
   1864 			tx->timeout = htole16(2);
   1865 	} else
   1866 		tx->timeout = htole16(0);
   1867 
   1868 	tx->rate = wpi_plcp_signal(rate);
   1869 
   1870 	/* be very persistant at sending frames out */
   1871 	tx->rts_ntries = 7;
   1872 	tx->data_ntries = 15;
   1873 
   1874 	tx->ofdm_mask = 0xff;
   1875 	tx->cck_mask = 0xf;
   1876 	tx->lifetime = htole32(WPI_LIFETIME_INFINITE);
   1877 
   1878 	tx->len = htole16(m0->m_pkthdr.len);
   1879 
   1880 	/* save and trim IEEE802.11 header */
   1881 	memcpy((uint8_t *)(tx + 1), wh, hdrlen);
   1882 	m_adj(m0, hdrlen);
   1883 
   1884 	error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
   1885 		BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   1886 	if (error != 0 && error != EFBIG) {
   1887 		aprint_error_dev(sc->sc_dev, "could not map mbuf (error %d)\n", error);
   1888 		m_freem(m0);
   1889 		return error;
   1890 	}
   1891 	if (error != 0) {
   1892 		/* too many fragments, linearize */
   1893 		MGETHDR(mnew, M_DONTWAIT, MT_DATA);
   1894 		if (mnew == NULL) {
   1895 			m_freem(m0);
   1896 			return ENOMEM;
   1897 		}
   1898 
   1899 		M_COPY_PKTHDR(mnew, m0);
   1900 		if (m0->m_pkthdr.len > MHLEN) {
   1901 			MCLGET(mnew, M_DONTWAIT);
   1902 			if (!(mnew->m_flags & M_EXT)) {
   1903 				m_freem(m0);
   1904 				m_freem(mnew);
   1905 				return ENOMEM;
   1906 			}
   1907 		}
   1908 
   1909 		m_copydata(m0, 0, m0->m_pkthdr.len, mtod(mnew, void *));
   1910 		m_freem(m0);
   1911 		mnew->m_len = mnew->m_pkthdr.len;
   1912 		m0 = mnew;
   1913 
   1914 		error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
   1915 			BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   1916 		if (error != 0) {
   1917 			aprint_error_dev(sc->sc_dev, "could not map mbuf (error %d)\n",
   1918 							 error);
   1919 			m_freem(m0);
   1920 			return error;
   1921 		}
   1922 	}
   1923 
   1924 	data->m = m0;
   1925 	data->ni = ni;
   1926 
   1927 	DPRINTFN(4, ("sending data: qid=%d idx=%d len=%d nsegs=%d\n",
   1928 		ring->qid, ring->cur, m0->m_pkthdr.len, data->map->dm_nsegs));
   1929 
   1930 	/* first scatter/gather segment is used by the tx data command */
   1931 	desc->flags = htole32(WPI_PAD32(m0->m_pkthdr.len) << 28 |
   1932 		(1 + data->map->dm_nsegs) << 24);
   1933 	desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
   1934 		ring->cur * sizeof (struct wpi_tx_cmd));
   1935 	desc->segs[0].len  = htole32(4 + sizeof (struct wpi_cmd_data) +
   1936 						 ((hdrlen + 3) & ~3));
   1937 
   1938 	for (i = 1; i <= data->map->dm_nsegs; i++) {
   1939 		desc->segs[i].addr =
   1940 			htole32(data->map->dm_segs[i - 1].ds_addr);
   1941 		desc->segs[i].len  =
   1942 			htole32(data->map->dm_segs[i - 1].ds_len);
   1943 	}
   1944 
   1945 	ring->queued++;
   1946 
   1947 	/* kick ring */
   1948 	ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT;
   1949 	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
   1950 
   1951 	return 0;
   1952 }
   1953 
   1954 static void
   1955 wpi_start(struct ifnet *ifp)
   1956 {
   1957 	struct wpi_softc *sc = ifp->if_softc;
   1958 	struct ieee80211com *ic = &sc->sc_ic;
   1959 	struct ieee80211_node *ni;
   1960 	struct ether_header *eh;
   1961 	struct mbuf *m0;
   1962 	int ac;
   1963 
   1964 	/*
   1965 	 * net80211 may still try to send management frames even if the
   1966 	 * IFF_RUNNING flag is not set...
   1967 	 */
   1968 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
   1969 		return;
   1970 
   1971 	for (;;) {
   1972 		IF_DEQUEUE(&ic->ic_mgtq, m0);
   1973 		if (m0 != NULL) {
   1974 
   1975 			ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
   1976 			m0->m_pkthdr.rcvif = NULL;
   1977 
   1978 			/* management frames go into ring 0 */
   1979 			if (sc->txq[0].queued > sc->txq[0].count - 8) {
   1980 				ifp->if_oerrors++;
   1981 				continue;
   1982 			}
   1983 			bpf_mtap3(ic->ic_rawbpf, m0);
   1984 			if (wpi_tx_data(sc, m0, ni, 0) != 0) {
   1985 				ifp->if_oerrors++;
   1986 				break;
   1987 			}
   1988 		} else {
   1989 			if (ic->ic_state != IEEE80211_S_RUN)
   1990 				break;
   1991 			IFQ_POLL(&ifp->if_snd, m0);
   1992 			if (m0 == NULL)
   1993 				break;
   1994 
   1995 			if (m0->m_len < sizeof (*eh) &&
   1996 			    (m0 = m_pullup(m0, sizeof (*eh))) == NULL) {
   1997 				ifp->if_oerrors++;
   1998 				continue;
   1999 			}
   2000 			eh = mtod(m0, struct ether_header *);
   2001 			ni = ieee80211_find_txnode(ic, eh->ether_dhost);
   2002 			if (ni == NULL) {
   2003 				m_freem(m0);
   2004 				ifp->if_oerrors++;
   2005 				continue;
   2006 			}
   2007 
   2008 			/* classify mbuf so we can find which tx ring to use */
   2009 			if (ieee80211_classify(ic, m0, ni) != 0) {
   2010 				m_freem(m0);
   2011 				ieee80211_free_node(ni);
   2012 				ifp->if_oerrors++;
   2013 				continue;
   2014 			}
   2015 
   2016 			/* no QoS encapsulation for EAPOL frames */
   2017 			ac = (eh->ether_type != htons(ETHERTYPE_PAE)) ?
   2018 			    M_WME_GETAC(m0) : WME_AC_BE;
   2019 
   2020 			if (sc->txq[ac].queued > sc->txq[ac].count - 8) {
   2021 				/* there is no place left in this ring */
   2022 				ifp->if_flags |= IFF_OACTIVE;
   2023 				break;
   2024 			}
   2025 			IFQ_DEQUEUE(&ifp->if_snd, m0);
   2026 			bpf_mtap(ifp, m0);
   2027 			m0 = ieee80211_encap(ic, m0, ni);
   2028 			if (m0 == NULL) {
   2029 				ieee80211_free_node(ni);
   2030 				ifp->if_oerrors++;
   2031 				continue;
   2032 			}
   2033 			bpf_mtap3(ic->ic_rawbpf, m0);
   2034 			if (wpi_tx_data(sc, m0, ni, ac) != 0) {
   2035 				ieee80211_free_node(ni);
   2036 				ifp->if_oerrors++;
   2037 				break;
   2038 			}
   2039 		}
   2040 
   2041 		sc->sc_tx_timer = 5;
   2042 		ifp->if_timer = 1;
   2043 	}
   2044 }
   2045 
   2046 static void
   2047 wpi_watchdog(struct ifnet *ifp)
   2048 {
   2049 	struct wpi_softc *sc = ifp->if_softc;
   2050 
   2051 	ifp->if_timer = 0;
   2052 
   2053 	if (sc->sc_tx_timer > 0) {
   2054 		if (--sc->sc_tx_timer == 0) {
   2055 			aprint_error_dev(sc->sc_dev, "device timeout\n");
   2056 			ifp->if_oerrors++;
   2057 			ifp->if_flags &= ~IFF_UP;
   2058 			wpi_stop(ifp, 1);
   2059 			return;
   2060 		}
   2061 		ifp->if_timer = 1;
   2062 	}
   2063 
   2064 	ieee80211_watchdog(&sc->sc_ic);
   2065 }
   2066 
   2067 static int
   2068 wpi_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   2069 {
   2070 #define IS_RUNNING(ifp) \
   2071 	((ifp->if_flags & IFF_UP) && (ifp->if_flags & IFF_RUNNING))
   2072 
   2073 	struct wpi_softc *sc = ifp->if_softc;
   2074 	struct ieee80211com *ic = &sc->sc_ic;
   2075 	int s, error = 0;
   2076 
   2077 	s = splnet();
   2078 
   2079 	switch (cmd) {
   2080 	case SIOCSIFFLAGS:
   2081 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   2082 			break;
   2083 		if (ifp->if_flags & IFF_UP) {
   2084 			if (!(ifp->if_flags & IFF_RUNNING))
   2085 				wpi_init(ifp);
   2086 		} else {
   2087 			if (ifp->if_flags & IFF_RUNNING)
   2088 				wpi_stop(ifp, 1);
   2089 		}
   2090 		break;
   2091 
   2092 	case SIOCADDMULTI:
   2093 	case SIOCDELMULTI:
   2094 		/* XXX no h/w multicast filter? --dyoung */
   2095 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
   2096 			/* setup multicast filter, etc */
   2097 			error = 0;
   2098 		}
   2099 		break;
   2100 
   2101 	default:
   2102 		error = ieee80211_ioctl(ic, cmd, data);
   2103 	}
   2104 
   2105 	if (error == ENETRESET) {
   2106 		if (IS_RUNNING(ifp) &&
   2107 			(ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
   2108 			wpi_init(ifp);
   2109 		error = 0;
   2110 	}
   2111 
   2112 	splx(s);
   2113 	return error;
   2114 
   2115 #undef IS_RUNNING
   2116 }
   2117 
   2118 /*
   2119  * Extract various information from EEPROM.
   2120  */
   2121 static void
   2122 wpi_read_eeprom(struct wpi_softc *sc)
   2123 {
   2124 	struct ieee80211com *ic = &sc->sc_ic;
   2125 	char domain[4];
   2126 	int i;
   2127 
   2128 	wpi_read_prom_data(sc, WPI_EEPROM_CAPABILITIES, &sc->cap, 1);
   2129 	wpi_read_prom_data(sc, WPI_EEPROM_REVISION, &sc->rev, 2);
   2130 	wpi_read_prom_data(sc, WPI_EEPROM_TYPE, &sc->type, 1);
   2131 
   2132 	DPRINTF(("cap=%x rev=%x type=%x\n", sc->cap, le16toh(sc->rev),
   2133 	    sc->type));
   2134 
   2135 	/* read and print regulatory domain */
   2136 	wpi_read_prom_data(sc, WPI_EEPROM_DOMAIN, domain, 4);
   2137 	aprint_normal_dev(sc->sc_dev, "%.4s", domain);
   2138 
   2139 	/* read and print MAC address */
   2140 	wpi_read_prom_data(sc, WPI_EEPROM_MAC, ic->ic_myaddr, 6);
   2141 	aprint_normal(", address %s\n", ether_sprintf(ic->ic_myaddr));
   2142 
   2143 	/* read the list of authorized channels */
   2144 	for (i = 0; i < WPI_CHAN_BANDS_COUNT; i++)
   2145 		wpi_read_eeprom_channels(sc, i);
   2146 
   2147 	/* read the list of power groups */
   2148 	for (i = 0; i < WPI_POWER_GROUPS_COUNT; i++)
   2149 		wpi_read_eeprom_group(sc, i);
   2150 }
   2151 
   2152 static void
   2153 wpi_read_eeprom_channels(struct wpi_softc *sc, int n)
   2154 {
   2155 	struct ieee80211com *ic = &sc->sc_ic;
   2156 	const struct wpi_chan_band *band = &wpi_bands[n];
   2157 	struct wpi_eeprom_chan channels[WPI_MAX_CHAN_PER_BAND];
   2158 	int chan, i;
   2159 
   2160 	wpi_read_prom_data(sc, band->addr, channels,
   2161 	    band->nchan * sizeof (struct wpi_eeprom_chan));
   2162 
   2163 	for (i = 0; i < band->nchan; i++) {
   2164 		if (!(channels[i].flags & WPI_EEPROM_CHAN_VALID))
   2165 			continue;
   2166 
   2167 		chan = band->chan[i];
   2168 
   2169 		if (n == 0) {	/* 2GHz band */
   2170 			ic->ic_channels[chan].ic_freq =
   2171 			    ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ);
   2172 			ic->ic_channels[chan].ic_flags =
   2173 			    IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
   2174 			    IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
   2175 
   2176 		} else {	/* 5GHz band */
   2177 			/*
   2178 			 * Some 3945abg adapters support channels 7, 8, 11
   2179 			 * and 12 in the 2GHz *and* 5GHz bands.
   2180 			 * Because of limitations in our net80211(9) stack,
   2181 			 * we can't support these channels in 5GHz band.
   2182 			 */
   2183 			if (chan <= 14)
   2184 				continue;
   2185 
   2186 			ic->ic_channels[chan].ic_freq =
   2187 			    ieee80211_ieee2mhz(chan, IEEE80211_CHAN_5GHZ);
   2188 			ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_A;
   2189 		}
   2190 
   2191 		/* is active scan allowed on this channel? */
   2192 		if (!(channels[i].flags & WPI_EEPROM_CHAN_ACTIVE)) {
   2193 			ic->ic_channels[chan].ic_flags |=
   2194 			    IEEE80211_CHAN_PASSIVE;
   2195 		}
   2196 
   2197 		/* save maximum allowed power for this channel */
   2198 		sc->maxpwr[chan] = channels[i].maxpwr;
   2199 
   2200 		DPRINTF(("adding chan %d flags=0x%x maxpwr=%d\n",
   2201 		    chan, channels[i].flags, sc->maxpwr[chan]));
   2202 	}
   2203 }
   2204 
   2205 static void
   2206 wpi_read_eeprom_group(struct wpi_softc *sc, int n)
   2207 {
   2208 	struct wpi_power_group *group = &sc->groups[n];
   2209 	struct wpi_eeprom_group rgroup;
   2210 	int i;
   2211 
   2212 	wpi_read_prom_data(sc, WPI_EEPROM_POWER_GRP + n * 32, &rgroup,
   2213 	    sizeof rgroup);
   2214 
   2215 	/* save power group information */
   2216 	group->chan   = rgroup.chan;
   2217 	group->maxpwr = rgroup.maxpwr;
   2218 	/* temperature at which the samples were taken */
   2219 	group->temp   = (int16_t)le16toh(rgroup.temp);
   2220 
   2221 	DPRINTF(("power group %d: chan=%d maxpwr=%d temp=%d\n", n,
   2222 	    group->chan, group->maxpwr, group->temp));
   2223 
   2224 	for (i = 0; i < WPI_SAMPLES_COUNT; i++) {
   2225 		group->samples[i].index = rgroup.samples[i].index;
   2226 		group->samples[i].power = rgroup.samples[i].power;
   2227 
   2228 		DPRINTF(("\tsample %d: index=%d power=%d\n", i,
   2229 		    group->samples[i].index, group->samples[i].power));
   2230 	}
   2231 }
   2232 
   2233 /*
   2234  * Send a command to the firmware.
   2235  */
   2236 static int
   2237 wpi_cmd(struct wpi_softc *sc, int code, const void *buf, int size, int async)
   2238 {
   2239 	struct wpi_tx_ring *ring = &sc->cmdq;
   2240 	struct wpi_tx_desc *desc;
   2241 	struct wpi_tx_cmd *cmd;
   2242 
   2243 	KASSERT(size <= sizeof cmd->data);
   2244 
   2245 	desc = &ring->desc[ring->cur];
   2246 	cmd = &ring->cmd[ring->cur];
   2247 
   2248 	cmd->code = code;
   2249 	cmd->flags = 0;
   2250 	cmd->qid = ring->qid;
   2251 	cmd->idx = ring->cur;
   2252 	memcpy(cmd->data, buf, size);
   2253 
   2254 	desc->flags = htole32(WPI_PAD32(size) << 28 | 1 << 24);
   2255 	desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
   2256 		ring->cur * sizeof (struct wpi_tx_cmd));
   2257 	desc->segs[0].len  = htole32(4 + size);
   2258 
   2259 	/* kick cmd ring */
   2260 	ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
   2261 	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
   2262 
   2263 	return async ? 0 : tsleep(cmd, PCATCH, "wpicmd", hz);
   2264 }
   2265 
   2266 static int
   2267 wpi_wme_update(struct ieee80211com *ic)
   2268 {
   2269 #define WPI_EXP2(v)	htole16((1 << (v)) - 1)
   2270 #define WPI_USEC(v)	htole16(IEEE80211_TXOP_TO_US(v))
   2271 	struct wpi_softc *sc = ic->ic_ifp->if_softc;
   2272 	const struct wmeParams *wmep;
   2273 	struct wpi_wme_setup wme;
   2274 	int ac;
   2275 
   2276 	/* don't override default WME values if WME is not actually enabled */
   2277 	if (!(ic->ic_flags & IEEE80211_F_WME))
   2278 		return 0;
   2279 
   2280 	wme.flags = 0;
   2281 	for (ac = 0; ac < WME_NUM_AC; ac++) {
   2282 		wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
   2283 		wme.ac[ac].aifsn = wmep->wmep_aifsn;
   2284 		wme.ac[ac].cwmin = WPI_EXP2(wmep->wmep_logcwmin);
   2285 		wme.ac[ac].cwmax = WPI_EXP2(wmep->wmep_logcwmax);
   2286 		wme.ac[ac].txop  = WPI_USEC(wmep->wmep_txopLimit);
   2287 
   2288 		DPRINTF(("setting WME for queue %d aifsn=%d cwmin=%d cwmax=%d "
   2289 		    "txop=%d\n", ac, wme.ac[ac].aifsn, wme.ac[ac].cwmin,
   2290 		    wme.ac[ac].cwmax, wme.ac[ac].txop));
   2291 	}
   2292 
   2293 	return wpi_cmd(sc, WPI_CMD_SET_WME, &wme, sizeof wme, 1);
   2294 #undef WPI_USEC
   2295 #undef WPI_EXP2
   2296 }
   2297 
   2298 /*
   2299  * Configure h/w multi-rate retries.
   2300  */
   2301 static int
   2302 wpi_mrr_setup(struct wpi_softc *sc)
   2303 {
   2304 	struct ieee80211com *ic = &sc->sc_ic;
   2305 	struct wpi_mrr_setup mrr;
   2306 	int i, error;
   2307 
   2308 	/* CCK rates (not used with 802.11a) */
   2309 	for (i = WPI_CCK1; i <= WPI_CCK11; i++) {
   2310 		mrr.rates[i].flags = 0;
   2311 		mrr.rates[i].plcp = wpi_ridx_to_plcp[i];
   2312 		/* fallback to the immediate lower CCK rate (if any) */
   2313 		mrr.rates[i].next = (i == WPI_CCK1) ? WPI_CCK1 : i - 1;
   2314 		/* try one time at this rate before falling back to "next" */
   2315 		mrr.rates[i].ntries = 1;
   2316 	}
   2317 
   2318 	/* OFDM rates (not used with 802.11b) */
   2319 	for (i = WPI_OFDM6; i <= WPI_OFDM54; i++) {
   2320 		mrr.rates[i].flags = 0;
   2321 		mrr.rates[i].plcp = wpi_ridx_to_plcp[i];
   2322 		/* fallback to the immediate lower rate (if any) */
   2323 		/* we allow fallback from OFDM/6 to CCK/2 in 11b/g mode */
   2324 		mrr.rates[i].next = (i == WPI_OFDM6) ?
   2325 		    ((ic->ic_curmode == IEEE80211_MODE_11A) ?
   2326 			WPI_OFDM6 : WPI_CCK2) :
   2327 		    i - 1;
   2328 		/* try one time at this rate before falling back to "next" */
   2329 		mrr.rates[i].ntries = 1;
   2330 	}
   2331 
   2332 	/* setup MRR for control frames */
   2333 	mrr.which = htole32(WPI_MRR_CTL);
   2334 	error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
   2335 	if (error != 0) {
   2336 		aprint_error_dev(sc->sc_dev, "could not setup MRR for control frames\n");
   2337 		return error;
   2338 	}
   2339 
   2340 	/* setup MRR for data frames */
   2341 	mrr.which = htole32(WPI_MRR_DATA);
   2342 	error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
   2343 	if (error != 0) {
   2344 		aprint_error_dev(sc->sc_dev, "could not setup MRR for data frames\n");
   2345 		return error;
   2346 	}
   2347 
   2348 	return 0;
   2349 }
   2350 
   2351 static void
   2352 wpi_set_led(struct wpi_softc *sc, uint8_t which, uint8_t off, uint8_t on)
   2353 {
   2354 	struct wpi_cmd_led led;
   2355 
   2356 	led.which = which;
   2357 	led.unit = htole32(100000);	/* on/off in unit of 100ms */
   2358 	led.off = off;
   2359 	led.on = on;
   2360 
   2361 	(void)wpi_cmd(sc, WPI_CMD_SET_LED, &led, sizeof led, 1);
   2362 }
   2363 
   2364 static void
   2365 wpi_enable_tsf(struct wpi_softc *sc, struct ieee80211_node *ni)
   2366 {
   2367 	struct wpi_cmd_tsf tsf;
   2368 	uint64_t val, mod;
   2369 
   2370 	memset(&tsf, 0, sizeof tsf);
   2371 	memcpy(&tsf.tstamp, ni->ni_tstamp.data, 8);
   2372 	tsf.bintval = htole16(ni->ni_intval);
   2373 	tsf.lintval = htole16(10);
   2374 
   2375 	/* compute remaining time until next beacon */
   2376 	val = (uint64_t)ni->ni_intval  * 1024;	/* msecs -> usecs */
   2377 	mod = le64toh(tsf.tstamp) % val;
   2378 	tsf.binitval = htole32((uint32_t)(val - mod));
   2379 
   2380 	DPRINTF(("TSF bintval=%u tstamp=%" PRId64 ", init=%u\n",
   2381 	    ni->ni_intval, le64toh(tsf.tstamp), (uint32_t)(val - mod)));
   2382 
   2383 	if (wpi_cmd(sc, WPI_CMD_TSF, &tsf, sizeof tsf, 1) != 0)
   2384 		aprint_error_dev(sc->sc_dev, "could not enable TSF\n");
   2385 }
   2386 
   2387 /*
   2388  * Update Tx power to match what is defined for channel `c'.
   2389  */
   2390 static int
   2391 wpi_set_txpower(struct wpi_softc *sc, struct ieee80211_channel *c, int async)
   2392 {
   2393 	struct ieee80211com *ic = &sc->sc_ic;
   2394 	struct wpi_power_group *group;
   2395 	struct wpi_cmd_txpower txpower;
   2396 	u_int chan;
   2397 	int i;
   2398 
   2399 	/* get channel number */
   2400 	chan = ieee80211_chan2ieee(ic, c);
   2401 
   2402 	/* find the power group to which this channel belongs */
   2403 	if (IEEE80211_IS_CHAN_5GHZ(c)) {
   2404 		for (group = &sc->groups[1]; group < &sc->groups[4]; group++)
   2405 			if (chan <= group->chan)
   2406 				break;
   2407 	} else
   2408 		group = &sc->groups[0];
   2409 
   2410 	memset(&txpower, 0, sizeof txpower);
   2411 	txpower.band = IEEE80211_IS_CHAN_5GHZ(c) ? 0 : 1;
   2412 	txpower.chan = htole16(chan);
   2413 
   2414 	/* set Tx power for all OFDM and CCK rates */
   2415 	for (i = 0; i <= 11 ; i++) {
   2416 		/* retrieve Tx power for this channel/rate combination */
   2417 		int idx = wpi_get_power_index(sc, group, c,
   2418 		    wpi_ridx_to_rate[i]);
   2419 
   2420 		txpower.rates[i].plcp = wpi_ridx_to_plcp[i];
   2421 
   2422 		if (IEEE80211_IS_CHAN_5GHZ(c)) {
   2423 			txpower.rates[i].rf_gain = wpi_rf_gain_5ghz[idx];
   2424 			txpower.rates[i].dsp_gain = wpi_dsp_gain_5ghz[idx];
   2425 		} else {
   2426 			txpower.rates[i].rf_gain = wpi_rf_gain_2ghz[idx];
   2427 			txpower.rates[i].dsp_gain = wpi_dsp_gain_2ghz[idx];
   2428 		}
   2429 		DPRINTF(("chan %d/rate %d: power index %d\n", chan,
   2430 		    wpi_ridx_to_rate[i], idx));
   2431 	}
   2432 
   2433 	return wpi_cmd(sc, WPI_CMD_TXPOWER, &txpower, sizeof txpower, async);
   2434 }
   2435 
   2436 /*
   2437  * Determine Tx power index for a given channel/rate combination.
   2438  * This takes into account the regulatory information from EEPROM and the
   2439  * current temperature.
   2440  */
   2441 static int
   2442 wpi_get_power_index(struct wpi_softc *sc, struct wpi_power_group *group,
   2443     struct ieee80211_channel *c, int rate)
   2444 {
   2445 /* fixed-point arithmetic division using a n-bit fractional part */
   2446 #define fdivround(a, b, n)	\
   2447 	((((1 << n) * (a)) / (b) + (1 << n) / 2) / (1 << n))
   2448 
   2449 /* linear interpolation */
   2450 #define interpolate(x, x1, y1, x2, y2, n)	\
   2451 	((y1) + fdivround(((x) - (x1)) * ((y2) - (y1)), (x2) - (x1), n))
   2452 
   2453 	struct ieee80211com *ic = &sc->sc_ic;
   2454 	struct wpi_power_sample *sample;
   2455 	int pwr, idx;
   2456 	u_int chan;
   2457 
   2458 	/* get channel number */
   2459 	chan = ieee80211_chan2ieee(ic, c);
   2460 
   2461 	/* default power is group's maximum power - 3dB */
   2462 	pwr = group->maxpwr / 2;
   2463 
   2464 	/* decrease power for highest OFDM rates to reduce distortion */
   2465 	switch (rate) {
   2466 	case 72:	/* 36Mb/s */
   2467 		pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 0 :  5;
   2468 		break;
   2469 	case 96:	/* 48Mb/s */
   2470 		pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 7 : 10;
   2471 		break;
   2472 	case 108:	/* 54Mb/s */
   2473 		pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 9 : 12;
   2474 		break;
   2475 	}
   2476 
   2477 	/* never exceed channel's maximum allowed Tx power */
   2478 	pwr = min(pwr, sc->maxpwr[chan]);
   2479 
   2480 	/* retrieve power index into gain tables from samples */
   2481 	for (sample = group->samples; sample < &group->samples[3]; sample++)
   2482 		if (pwr > sample[1].power)
   2483 			break;
   2484 	/* fixed-point linear interpolation using a 19-bit fractional part */
   2485 	idx = interpolate(pwr, sample[0].power, sample[0].index,
   2486 	    sample[1].power, sample[1].index, 19);
   2487 
   2488 	/*
   2489 	 * Adjust power index based on current temperature:
   2490 	 * - if cooler than factory-calibrated: decrease output power
   2491 	 * - if warmer than factory-calibrated: increase output power
   2492 	 */
   2493 	idx -= (sc->temp - group->temp) * 11 / 100;
   2494 
   2495 	/* decrease power for CCK rates (-5dB) */
   2496 	if (!WPI_RATE_IS_OFDM(rate))
   2497 		idx += 10;
   2498 
   2499 	/* keep power index in a valid range */
   2500 	if (idx < 0)
   2501 		return 0;
   2502 	if (idx > WPI_MAX_PWR_INDEX)
   2503 		return WPI_MAX_PWR_INDEX;
   2504 	return idx;
   2505 
   2506 #undef interpolate
   2507 #undef fdivround
   2508 }
   2509 
   2510 /*
   2511  * Build a beacon frame that the firmware will broadcast periodically in
   2512  * IBSS or HostAP modes.
   2513  */
   2514 static int
   2515 wpi_setup_beacon(struct wpi_softc *sc, struct ieee80211_node *ni)
   2516 {
   2517 	struct ieee80211com *ic = &sc->sc_ic;
   2518 	struct wpi_tx_ring *ring = &sc->cmdq;
   2519 	struct wpi_tx_desc *desc;
   2520 	struct wpi_tx_data *data;
   2521 	struct wpi_tx_cmd *cmd;
   2522 	struct wpi_cmd_beacon *bcn;
   2523 	struct ieee80211_beacon_offsets bo;
   2524 	struct mbuf *m0;
   2525 	int error;
   2526 
   2527 	desc = &ring->desc[ring->cur];
   2528 	data = &ring->data[ring->cur];
   2529 
   2530 	m0 = ieee80211_beacon_alloc(ic, ni, &bo);
   2531 	if (m0 == NULL) {
   2532 		aprint_error_dev(sc->sc_dev, "could not allocate beacon frame\n");
   2533 		return ENOMEM;
   2534 	}
   2535 
   2536 	cmd = &ring->cmd[ring->cur];
   2537 	cmd->code = WPI_CMD_SET_BEACON;
   2538 	cmd->flags = 0;
   2539 	cmd->qid = ring->qid;
   2540 	cmd->idx = ring->cur;
   2541 
   2542 	bcn = (struct wpi_cmd_beacon *)cmd->data;
   2543 	memset(bcn, 0, sizeof (struct wpi_cmd_beacon));
   2544 	bcn->id = WPI_ID_BROADCAST;
   2545 	bcn->ofdm_mask = 0xff;
   2546 	bcn->cck_mask = 0x0f;
   2547 	bcn->lifetime = htole32(WPI_LIFETIME_INFINITE);
   2548 	bcn->len = htole16(m0->m_pkthdr.len);
   2549 	bcn->rate = (ic->ic_curmode == IEEE80211_MODE_11A) ?
   2550 		wpi_plcp_signal(12) : wpi_plcp_signal(2);
   2551 	bcn->flags = htole32(WPI_TX_AUTO_SEQ | WPI_TX_INSERT_TSTAMP);
   2552 
   2553 	/* save and trim IEEE802.11 header */
   2554 	m_copydata(m0, 0, sizeof (struct ieee80211_frame), (void *)&bcn->wh);
   2555 	m_adj(m0, sizeof (struct ieee80211_frame));
   2556 
   2557 	/* assume beacon frame is contiguous */
   2558 	error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
   2559 		BUS_DMA_READ | BUS_DMA_NOWAIT);
   2560 	if (error) {
   2561 		aprint_error_dev(sc->sc_dev, "could not map beacon\n");
   2562 		m_freem(m0);
   2563 		return error;
   2564 	}
   2565 
   2566 	data->m = m0;
   2567 
   2568 	/* first scatter/gather segment is used by the beacon command */
   2569 	desc->flags = htole32(WPI_PAD32(m0->m_pkthdr.len) << 28 | 2 << 24);
   2570 	desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
   2571 		ring->cur * sizeof (struct wpi_tx_cmd));
   2572 	desc->segs[0].len  = htole32(4 + sizeof (struct wpi_cmd_beacon));
   2573 	desc->segs[1].addr = htole32(data->map->dm_segs[0].ds_addr);
   2574 	desc->segs[1].len  = htole32(data->map->dm_segs[0].ds_len);
   2575 
   2576 	/* kick cmd ring */
   2577 	ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
   2578 	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
   2579 
   2580 	return 0;
   2581 }
   2582 
   2583 static int
   2584 wpi_auth(struct wpi_softc *sc)
   2585 {
   2586 	struct ieee80211com *ic = &sc->sc_ic;
   2587 	struct ieee80211_node *ni = ic->ic_bss;
   2588 	struct wpi_node_info node;
   2589 	int error;
   2590 
   2591 	/* update adapter's configuration */
   2592 	IEEE80211_ADDR_COPY(sc->config.bssid, ni->ni_bssid);
   2593 	sc->config.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
   2594 	sc->config.flags = htole32(WPI_CONFIG_TSF);
   2595 	if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
   2596 		sc->config.flags |= htole32(WPI_CONFIG_AUTO |
   2597 		    WPI_CONFIG_24GHZ);
   2598 	}
   2599 	switch (ic->ic_curmode) {
   2600 	case IEEE80211_MODE_11A:
   2601 		sc->config.cck_mask  = 0;
   2602 		sc->config.ofdm_mask = 0x15;
   2603 		break;
   2604 	case IEEE80211_MODE_11B:
   2605 		sc->config.cck_mask  = 0x03;
   2606 		sc->config.ofdm_mask = 0;
   2607 		break;
   2608 	default:	/* assume 802.11b/g */
   2609 		sc->config.cck_mask  = 0x0f;
   2610 		sc->config.ofdm_mask = 0x15;
   2611 	}
   2612 
   2613 	DPRINTF(("config chan %d flags %x cck %x ofdm %x\n", sc->config.chan,
   2614 		sc->config.flags, sc->config.cck_mask, sc->config.ofdm_mask));
   2615 	error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config,
   2616 		sizeof (struct wpi_config), 1);
   2617 	if (error != 0) {
   2618 		aprint_error_dev(sc->sc_dev, "could not configure\n");
   2619 		return error;
   2620 	}
   2621 
   2622 	/* configuration has changed, set Tx power accordingly */
   2623 	if ((error = wpi_set_txpower(sc, ni->ni_chan, 1)) != 0) {
   2624 		aprint_error_dev(sc->sc_dev, "could not set Tx power\n");
   2625 		return error;
   2626 	}
   2627 
   2628 	/* add default node */
   2629 	memset(&node, 0, sizeof node);
   2630 	IEEE80211_ADDR_COPY(node.bssid, ni->ni_bssid);
   2631 	node.id = WPI_ID_BSS;
   2632 	node.rate = (ic->ic_curmode == IEEE80211_MODE_11A) ?
   2633 	    wpi_plcp_signal(12) : wpi_plcp_signal(2);
   2634 	node.action = htole32(WPI_ACTION_SET_RATE);
   2635 	node.antenna = WPI_ANTENNA_BOTH;
   2636 	error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
   2637 	if (error != 0) {
   2638 		aprint_error_dev(sc->sc_dev, "could not add BSS node\n");
   2639 		return error;
   2640 	}
   2641 
   2642 	return 0;
   2643 }
   2644 
   2645 /*
   2646  * Send a scan request to the firmware.  Since this command is huge, we map it
   2647  * into a mbuf instead of using the pre-allocated set of commands.
   2648  */
   2649 static int
   2650 wpi_scan(struct wpi_softc *sc, uint16_t flags)
   2651 {
   2652 	struct ieee80211com *ic = &sc->sc_ic;
   2653 	struct wpi_tx_ring *ring = &sc->cmdq;
   2654 	struct wpi_tx_desc *desc;
   2655 	struct wpi_tx_data *data;
   2656 	struct wpi_tx_cmd *cmd;
   2657 	struct wpi_scan_hdr *hdr;
   2658 	struct wpi_scan_chan *chan;
   2659 	struct ieee80211_frame *wh;
   2660 	struct ieee80211_rateset *rs;
   2661 	struct ieee80211_channel *c;
   2662 	enum ieee80211_phymode mode;
   2663 	uint8_t *frm;
   2664 	int nrates, pktlen, error;
   2665 
   2666 	desc = &ring->desc[ring->cur];
   2667 	data = &ring->data[ring->cur];
   2668 
   2669 	MGETHDR(data->m, M_DONTWAIT, MT_DATA);
   2670 	if (data->m == NULL) {
   2671 		aprint_error_dev(sc->sc_dev,
   2672 						"could not allocate mbuf for scan command\n");
   2673 		return ENOMEM;
   2674 	}
   2675 
   2676 	MCLGET(data->m, M_DONTWAIT);
   2677 	if (!(data->m->m_flags & M_EXT)) {
   2678 		m_freem(data->m);
   2679 		data->m = NULL;
   2680 		aprint_error_dev(sc->sc_dev,
   2681 						 "could not allocate mbuf for scan command\n");
   2682 		return ENOMEM;
   2683 	}
   2684 
   2685 	cmd = mtod(data->m, struct wpi_tx_cmd *);
   2686 	cmd->code = WPI_CMD_SCAN;
   2687 	cmd->flags = 0;
   2688 	cmd->qid = ring->qid;
   2689 	cmd->idx = ring->cur;
   2690 
   2691 	hdr = (struct wpi_scan_hdr *)cmd->data;
   2692 	memset(hdr, 0, sizeof (struct wpi_scan_hdr));
   2693 	hdr->txflags = htole32(WPI_TX_AUTO_SEQ);
   2694 	hdr->id = WPI_ID_BROADCAST;
   2695 	hdr->lifetime = htole32(WPI_LIFETIME_INFINITE);
   2696 
   2697 	/*
   2698 	 * Move to the next channel if no packets are received within 5 msecs
   2699 	 * after sending the probe request (this helps to reduce the duration
   2700 	 * of active scans).
   2701 	 */
   2702 	hdr->quiet = htole16(5);        /* timeout in milliseconds */
   2703 	hdr->plcp_threshold = htole16(1);	/* min # of packets */
   2704 
   2705 	if (flags & IEEE80211_CHAN_A) {
   2706 		hdr->crc_threshold = htole16(1);
   2707 		/* send probe requests at 6Mbps */
   2708 		hdr->rate = wpi_plcp_signal(12);
   2709 	} else {
   2710 		hdr->flags = htole32(WPI_CONFIG_24GHZ | WPI_CONFIG_AUTO);
   2711 		/* send probe requests at 1Mbps */
   2712 		hdr->rate = wpi_plcp_signal(2);
   2713 	}
   2714 
   2715 	/* for directed scans, firmware inserts the essid IE itself */
   2716 	hdr->essid[0].id  = IEEE80211_ELEMID_SSID;
   2717 	hdr->essid[0].len = ic->ic_des_esslen;
   2718 	memcpy(hdr->essid[0].data, ic->ic_des_essid, ic->ic_des_esslen);
   2719 
   2720 	/*
   2721 	 * Build a probe request frame.  Most of the following code is a
   2722 	 * copy & paste of what is done in net80211.
   2723 	 */
   2724 	wh = (struct ieee80211_frame *)(hdr + 1);
   2725 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
   2726 		IEEE80211_FC0_SUBTYPE_PROBE_REQ;
   2727 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
   2728 	IEEE80211_ADDR_COPY(wh->i_addr1, etherbroadcastaddr);
   2729 	IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
   2730 	IEEE80211_ADDR_COPY(wh->i_addr3, etherbroadcastaddr);
   2731 	*(u_int16_t *)&wh->i_dur[0] = 0;	/* filled by h/w */
   2732 	*(u_int16_t *)&wh->i_seq[0] = 0;	/* filled by h/w */
   2733 
   2734 	frm = (uint8_t *)(wh + 1);
   2735 
   2736 	/* add empty essid IE (firmware generates it for directed scans) */
   2737 	*frm++ = IEEE80211_ELEMID_SSID;
   2738 	*frm++ = 0;
   2739 
   2740 	mode = ieee80211_chan2mode(ic, ic->ic_ibss_chan);
   2741 	rs = &ic->ic_sup_rates[mode];
   2742 
   2743 	/* add supported rates IE */
   2744 	*frm++ = IEEE80211_ELEMID_RATES;
   2745 	nrates = rs->rs_nrates;
   2746 	if (nrates > IEEE80211_RATE_SIZE)
   2747 		nrates = IEEE80211_RATE_SIZE;
   2748 	*frm++ = nrates;
   2749 	memcpy(frm, rs->rs_rates, nrates);
   2750 	frm += nrates;
   2751 
   2752 	/* add supported xrates IE */
   2753 	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
   2754 		nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
   2755 		*frm++ = IEEE80211_ELEMID_XRATES;
   2756 		*frm++ = nrates;
   2757 		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
   2758 		frm += nrates;
   2759 	}
   2760 
   2761 	/* setup length of probe request */
   2762 	hdr->paylen = htole16(frm - (uint8_t *)wh);
   2763 
   2764 	chan = (struct wpi_scan_chan *)frm;
   2765 	for (c  = &ic->ic_channels[1];
   2766 	     c <= &ic->ic_channels[IEEE80211_CHAN_MAX]; c++) {
   2767 		if ((c->ic_flags & flags) != flags)
   2768 			continue;
   2769 
   2770 		chan->chan = ieee80211_chan2ieee(ic, c);
   2771 		chan->flags = 0;
   2772 		if (!(c->ic_flags & IEEE80211_CHAN_PASSIVE)) {
   2773 			chan->flags |= WPI_CHAN_ACTIVE;
   2774 			if (ic->ic_des_esslen != 0)
   2775 				chan->flags |= WPI_CHAN_DIRECT;
   2776 		}
   2777 		chan->dsp_gain = 0x6e;
   2778 		if (IEEE80211_IS_CHAN_5GHZ(c)) {
   2779 			chan->rf_gain = 0x3b;
   2780 			chan->active = htole16(10);
   2781 			chan->passive = htole16(110);
   2782 		} else {
   2783 			chan->rf_gain = 0x28;
   2784 			chan->active = htole16(20);
   2785 			chan->passive = htole16(120);
   2786 		}
   2787 		hdr->nchan++;
   2788 		chan++;
   2789 
   2790 		frm += sizeof (struct wpi_scan_chan);
   2791 	}
   2792 	hdr->len = htole16(frm - (uint8_t *)hdr);
   2793 	pktlen = frm - (uint8_t *)cmd;
   2794 
   2795 	error = bus_dmamap_load(sc->sc_dmat, data->map, cmd, pktlen,
   2796 		NULL, BUS_DMA_NOWAIT);
   2797 	if (error) {
   2798 		aprint_error_dev(sc->sc_dev, "could not map scan command\n");
   2799 		m_freem(data->m);
   2800 		data->m = NULL;
   2801 		return error;
   2802 	}
   2803 
   2804 	desc->flags = htole32(WPI_PAD32(pktlen) << 28 | 1 << 24);
   2805 	desc->segs[0].addr = htole32(data->map->dm_segs[0].ds_addr);
   2806 	desc->segs[0].len  = htole32(data->map->dm_segs[0].ds_len);
   2807 
   2808 	/* kick cmd ring */
   2809 	ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
   2810 	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
   2811 
   2812 	return 0;	/* will be notified async. of failure/success */
   2813 }
   2814 
   2815 static int
   2816 wpi_config(struct wpi_softc *sc)
   2817 {
   2818 	struct ieee80211com *ic = &sc->sc_ic;
   2819 	struct ifnet *ifp = ic->ic_ifp;
   2820 	struct wpi_power power;
   2821 	struct wpi_bluetooth bluetooth;
   2822 	struct wpi_node_info node;
   2823 	int error;
   2824 
   2825 	memset(&power, 0, sizeof power);
   2826 	power.flags = htole32(WPI_POWER_CAM | 0x8);
   2827 	error = wpi_cmd(sc, WPI_CMD_SET_POWER_MODE, &power, sizeof power, 0);
   2828 	if (error != 0) {
   2829 		aprint_error_dev(sc->sc_dev, "could not set power mode\n");
   2830 		return error;
   2831 	}
   2832 
   2833 	/* configure bluetooth coexistence */
   2834 	memset(&bluetooth, 0, sizeof bluetooth);
   2835 	bluetooth.flags = 3;
   2836 	bluetooth.lead = 0xaa;
   2837 	bluetooth.kill = 1;
   2838 	error = wpi_cmd(sc, WPI_CMD_BLUETOOTH, &bluetooth, sizeof bluetooth,
   2839 		0);
   2840 	if (error != 0) {
   2841 		aprint_error_dev(sc->sc_dev,
   2842 			"could not configure bluetooth coexistence\n");
   2843 		return error;
   2844 	}
   2845 
   2846 	/* configure adapter */
   2847 	memset(&sc->config, 0, sizeof (struct wpi_config));
   2848 	IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl));
   2849 	IEEE80211_ADDR_COPY(sc->config.myaddr, ic->ic_myaddr);
   2850 	/*set default channel*/
   2851 	sc->config.chan = ieee80211_chan2ieee(ic, ic->ic_ibss_chan);
   2852 	sc->config.flags = htole32(WPI_CONFIG_TSF);
   2853 	if (IEEE80211_IS_CHAN_2GHZ(ic->ic_ibss_chan)) {
   2854 		sc->config.flags |= htole32(WPI_CONFIG_AUTO |
   2855 		    WPI_CONFIG_24GHZ);
   2856 	}
   2857 	sc->config.filter = 0;
   2858 	switch (ic->ic_opmode) {
   2859 	case IEEE80211_M_STA:
   2860 		sc->config.mode = WPI_MODE_STA;
   2861 		sc->config.filter |= htole32(WPI_FILTER_MULTICAST);
   2862 		break;
   2863 	case IEEE80211_M_IBSS:
   2864 	case IEEE80211_M_AHDEMO:
   2865 		sc->config.mode = WPI_MODE_IBSS;
   2866 		break;
   2867 	case IEEE80211_M_HOSTAP:
   2868 		sc->config.mode = WPI_MODE_HOSTAP;
   2869 		break;
   2870 	case IEEE80211_M_MONITOR:
   2871 		sc->config.mode = WPI_MODE_MONITOR;
   2872 		sc->config.filter |= htole32(WPI_FILTER_MULTICAST |
   2873 			WPI_FILTER_CTL | WPI_FILTER_PROMISC);
   2874 		break;
   2875 	}
   2876 	sc->config.cck_mask  = 0x0f;	/* not yet negotiated */
   2877 	sc->config.ofdm_mask = 0xff;	/* not yet negotiated */
   2878 	error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config,
   2879 		sizeof (struct wpi_config), 0);
   2880 	if (error != 0) {
   2881 		aprint_error_dev(sc->sc_dev, "configure command failed\n");
   2882 		return error;
   2883 	}
   2884 
   2885 	/* configuration has changed, set Tx power accordingly */
   2886 	if ((error = wpi_set_txpower(sc, ic->ic_ibss_chan, 0)) != 0) {
   2887 		aprint_error_dev(sc->sc_dev, "could not set Tx power\n");
   2888 		return error;
   2889 	}
   2890 
   2891 	/* add broadcast node */
   2892 	memset(&node, 0, sizeof node);
   2893 	IEEE80211_ADDR_COPY(node.bssid, etherbroadcastaddr);
   2894 	node.id = WPI_ID_BROADCAST;
   2895 	node.rate = wpi_plcp_signal(2);
   2896 	node.action = htole32(WPI_ACTION_SET_RATE);
   2897 	node.antenna = WPI_ANTENNA_BOTH;
   2898 	error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 0);
   2899 	if (error != 0) {
   2900 		aprint_error_dev(sc->sc_dev, "could not add broadcast node\n");
   2901 		return error;
   2902 	}
   2903 
   2904 	if ((error = wpi_mrr_setup(sc)) != 0) {
   2905 		aprint_error_dev(sc->sc_dev, "could not setup MRR\n");
   2906 		return error;
   2907 	}
   2908 
   2909 	return 0;
   2910 }
   2911 
   2912 static void
   2913 wpi_stop_master(struct wpi_softc *sc)
   2914 {
   2915 	uint32_t tmp;
   2916 	int ntries;
   2917 
   2918 	tmp = WPI_READ(sc, WPI_RESET);
   2919 	WPI_WRITE(sc, WPI_RESET, tmp | WPI_STOP_MASTER);
   2920 
   2921 	tmp = WPI_READ(sc, WPI_GPIO_CTL);
   2922 	if ((tmp & WPI_GPIO_PWR_STATUS) == WPI_GPIO_PWR_SLEEP)
   2923 		return;	/* already asleep */
   2924 
   2925 	for (ntries = 0; ntries < 100; ntries++) {
   2926 		if (WPI_READ(sc, WPI_RESET) & WPI_MASTER_DISABLED)
   2927 			break;
   2928 		DELAY(10);
   2929 	}
   2930 	if (ntries == 100) {
   2931 		aprint_error_dev(sc->sc_dev, "timeout waiting for master\n");
   2932 	}
   2933 }
   2934 
   2935 static int
   2936 wpi_power_up(struct wpi_softc *sc)
   2937 {
   2938 	uint32_t tmp;
   2939 	int ntries;
   2940 
   2941 	wpi_mem_lock(sc);
   2942 	tmp = wpi_mem_read(sc, WPI_MEM_POWER);
   2943 	wpi_mem_write(sc, WPI_MEM_POWER, tmp & ~0x03000000);
   2944 	wpi_mem_unlock(sc);
   2945 
   2946 	for (ntries = 0; ntries < 5000; ntries++) {
   2947 		if (WPI_READ(sc, WPI_GPIO_STATUS) & WPI_POWERED)
   2948 			break;
   2949 		DELAY(10);
   2950 	}
   2951 	if (ntries == 5000) {
   2952 		aprint_error_dev(sc->sc_dev, "timeout waiting for NIC to power up\n");
   2953 		return ETIMEDOUT;
   2954 	}
   2955 	return 0;
   2956 }
   2957 
   2958 static int
   2959 wpi_reset(struct wpi_softc *sc)
   2960 {
   2961 	uint32_t tmp;
   2962 	int ntries;
   2963 
   2964 	/* clear any pending interrupts */
   2965 	WPI_WRITE(sc, WPI_INTR, 0xffffffff);
   2966 
   2967 	tmp = WPI_READ(sc, WPI_PLL_CTL);
   2968 	WPI_WRITE(sc, WPI_PLL_CTL, tmp | WPI_PLL_INIT);
   2969 
   2970 	tmp = WPI_READ(sc, WPI_CHICKEN);
   2971 	WPI_WRITE(sc, WPI_CHICKEN, tmp | WPI_CHICKEN_RXNOLOS);
   2972 
   2973 	tmp = WPI_READ(sc, WPI_GPIO_CTL);
   2974 	WPI_WRITE(sc, WPI_GPIO_CTL, tmp | WPI_GPIO_INIT);
   2975 
   2976 	/* wait for clock stabilization */
   2977 	for (ntries = 0; ntries < 1000; ntries++) {
   2978 		if (WPI_READ(sc, WPI_GPIO_CTL) & WPI_GPIO_CLOCK)
   2979 			break;
   2980 		DELAY(10);
   2981 	}
   2982 	if (ntries == 1000) {
   2983 		aprint_error_dev(sc->sc_dev,
   2984 						 "timeout waiting for clock stabilization\n");
   2985 		return ETIMEDOUT;
   2986 	}
   2987 
   2988 	/* initialize EEPROM */
   2989 	tmp = WPI_READ(sc, WPI_EEPROM_STATUS);
   2990 	if ((tmp & WPI_EEPROM_VERSION) == 0) {
   2991 		aprint_error_dev(sc->sc_dev, "EEPROM not found\n");
   2992 		return EIO;
   2993 	}
   2994 	WPI_WRITE(sc, WPI_EEPROM_STATUS, tmp & ~WPI_EEPROM_LOCKED);
   2995 
   2996 	return 0;
   2997 }
   2998 
   2999 static void
   3000 wpi_hw_config(struct wpi_softc *sc)
   3001 {
   3002 	uint32_t rev, hw;
   3003 
   3004 	/* voodoo from the reference driver */
   3005 	hw = WPI_READ(sc, WPI_HWCONFIG);
   3006 
   3007 	rev = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_CLASS_REG);
   3008 	rev = PCI_REVISION(rev);
   3009 	if ((rev & 0xc0) == 0x40)
   3010 		hw |= WPI_HW_ALM_MB;
   3011 	else if (!(rev & 0x80))
   3012 		hw |= WPI_HW_ALM_MM;
   3013 
   3014 	if (sc->cap == 0x80)
   3015 		hw |= WPI_HW_SKU_MRC;
   3016 
   3017 	hw &= ~WPI_HW_REV_D;
   3018 	if ((le16toh(sc->rev) & 0xf0) == 0xd0)
   3019 		hw |= WPI_HW_REV_D;
   3020 
   3021 	if (sc->type > 1)
   3022 		hw |= WPI_HW_TYPE_B;
   3023 
   3024 	DPRINTF(("setting h/w config %x\n", hw));
   3025 	WPI_WRITE(sc, WPI_HWCONFIG, hw);
   3026 }
   3027 
   3028 static int
   3029 wpi_init(struct ifnet *ifp)
   3030 {
   3031 	struct wpi_softc *sc = ifp->if_softc;
   3032 	struct ieee80211com *ic = &sc->sc_ic;
   3033 	uint32_t tmp;
   3034 	int qid, ntries, error;
   3035 
   3036 	wpi_stop(ifp,1);
   3037 	(void)wpi_reset(sc);
   3038 
   3039 	wpi_mem_lock(sc);
   3040 	wpi_mem_write(sc, WPI_MEM_CLOCK1, 0xa00);
   3041 	DELAY(20);
   3042 	tmp = wpi_mem_read(sc, WPI_MEM_PCIDEV);
   3043 	wpi_mem_write(sc, WPI_MEM_PCIDEV, tmp | 0x800);
   3044 	wpi_mem_unlock(sc);
   3045 
   3046 	(void)wpi_power_up(sc);
   3047 	wpi_hw_config(sc);
   3048 
   3049 	/* init Rx ring */
   3050 	wpi_mem_lock(sc);
   3051 	WPI_WRITE(sc, WPI_RX_BASE, sc->rxq.desc_dma.paddr);
   3052 	WPI_WRITE(sc, WPI_RX_RIDX_PTR, sc->shared_dma.paddr +
   3053 	    offsetof(struct wpi_shared, next));
   3054 	WPI_WRITE(sc, WPI_RX_WIDX, (WPI_RX_RING_COUNT - 1) & ~7);
   3055 	WPI_WRITE(sc, WPI_RX_CONFIG, 0xa9601010);
   3056 	wpi_mem_unlock(sc);
   3057 
   3058 	/* init Tx rings */
   3059 	wpi_mem_lock(sc);
   3060 	wpi_mem_write(sc, WPI_MEM_MODE, 2); /* bypass mode */
   3061 	wpi_mem_write(sc, WPI_MEM_RA, 1);   /* enable RA0 */
   3062 	wpi_mem_write(sc, WPI_MEM_TXCFG, 0x3f); /* enable all 6 Tx rings */
   3063 	wpi_mem_write(sc, WPI_MEM_BYPASS1, 0x10000);
   3064 	wpi_mem_write(sc, WPI_MEM_BYPASS2, 0x30002);
   3065 	wpi_mem_write(sc, WPI_MEM_MAGIC4, 4);
   3066 	wpi_mem_write(sc, WPI_MEM_MAGIC5, 5);
   3067 
   3068 	WPI_WRITE(sc, WPI_TX_BASE_PTR, sc->shared_dma.paddr);
   3069 	WPI_WRITE(sc, WPI_MSG_CONFIG, 0xffff05a5);
   3070 
   3071 	for (qid = 0; qid < 6; qid++) {
   3072 		WPI_WRITE(sc, WPI_TX_CTL(qid), 0);
   3073 		WPI_WRITE(sc, WPI_TX_BASE(qid), 0);
   3074 		WPI_WRITE(sc, WPI_TX_CONFIG(qid), 0x80200008);
   3075 	}
   3076 	wpi_mem_unlock(sc);
   3077 
   3078 	/* clear "radio off" and "disable command" bits (reversed logic) */
   3079 	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
   3080 	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_DISABLE_CMD);
   3081 
   3082 	/* clear any pending interrupts */
   3083 	WPI_WRITE(sc, WPI_INTR, 0xffffffff);
   3084 	/* enable interrupts */
   3085 	WPI_WRITE(sc, WPI_MASK, WPI_INTR_MASK);
   3086 
   3087 	/* not sure why/if this is necessary... */
   3088 	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
   3089 	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
   3090 
   3091 	if ((error = wpi_load_firmware(sc)) != 0) {
   3092 		aprint_error_dev(sc->sc_dev, "could not load firmware\n");
   3093 		goto fail1;
   3094 	}
   3095 
   3096 	/* Check the status of the radio switch */
   3097 	if (wpi_getrfkill(sc)) {
   3098 		aprint_error_dev(sc->sc_dev, "Radio is disabled by hardware switch\n");
   3099 		error = EBUSY;
   3100 		goto fail1;
   3101 	}
   3102 
   3103 	/* wait for thermal sensors to calibrate */
   3104 	for (ntries = 0; ntries < 1000; ntries++) {
   3105 		if ((sc->temp = (int)WPI_READ(sc, WPI_TEMPERATURE)) != 0)
   3106 			break;
   3107 		DELAY(10);
   3108 	}
   3109 	if (ntries == 1000) {
   3110 		aprint_error_dev(sc->sc_dev,
   3111 						 "timeout waiting for thermal sensors calibration\n");
   3112 		error = ETIMEDOUT;
   3113 		goto fail1;
   3114 	}
   3115 
   3116 	DPRINTF(("temperature %d\n", sc->temp));
   3117 
   3118 	if ((error = wpi_config(sc)) != 0) {
   3119 		aprint_error_dev(sc->sc_dev, "could not configure device\n");
   3120 		goto fail1;
   3121 	}
   3122 
   3123 	ifp->if_flags &= ~IFF_OACTIVE;
   3124 	ifp->if_flags |= IFF_RUNNING;
   3125 
   3126 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
   3127 		if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
   3128 			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
   3129 	}
   3130 	else
   3131 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
   3132 
   3133 	return 0;
   3134 
   3135 fail1:	wpi_stop(ifp, 1);
   3136 	return error;
   3137 }
   3138 
   3139 
   3140 static void
   3141 wpi_stop(struct ifnet *ifp, int disable)
   3142 {
   3143 	struct wpi_softc *sc = ifp->if_softc;
   3144 	struct ieee80211com *ic = &sc->sc_ic;
   3145 	uint32_t tmp;
   3146 	int ac;
   3147 
   3148 	ifp->if_timer = sc->sc_tx_timer = 0;
   3149 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   3150 
   3151 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
   3152 
   3153 	/* disable interrupts */
   3154 	WPI_WRITE(sc, WPI_MASK, 0);
   3155 	WPI_WRITE(sc, WPI_INTR, WPI_INTR_MASK);
   3156 	WPI_WRITE(sc, WPI_INTR_STATUS, 0xff);
   3157 	WPI_WRITE(sc, WPI_INTR_STATUS, 0x00070000);
   3158 
   3159 	wpi_mem_lock(sc);
   3160 	wpi_mem_write(sc, WPI_MEM_MODE, 0);
   3161 	wpi_mem_unlock(sc);
   3162 
   3163 	/* reset all Tx rings */
   3164 	for (ac = 0; ac < 4; ac++)
   3165 		wpi_reset_tx_ring(sc, &sc->txq[ac]);
   3166 	wpi_reset_tx_ring(sc, &sc->cmdq);
   3167 
   3168 	/* reset Rx ring */
   3169 	wpi_reset_rx_ring(sc, &sc->rxq);
   3170 
   3171 	wpi_mem_lock(sc);
   3172 	wpi_mem_write(sc, WPI_MEM_CLOCK2, 0x200);
   3173 	wpi_mem_unlock(sc);
   3174 
   3175 	DELAY(5);
   3176 
   3177 	wpi_stop_master(sc);
   3178 
   3179 	tmp = WPI_READ(sc, WPI_RESET);
   3180 	WPI_WRITE(sc, WPI_RESET, tmp | WPI_SW_RESET);
   3181 }
   3182 
   3183 static bool
   3184 wpi_resume(device_t dv, const pmf_qual_t *qual)
   3185 {
   3186 	struct wpi_softc *sc = device_private(dv);
   3187 
   3188 	(void)wpi_reset(sc);
   3189 
   3190 	return true;
   3191 }
   3192 
   3193 /*
   3194  * Return whether or not the radio is enabled in hardware
   3195  * (i.e. the rfkill switch is "off").
   3196  */
   3197 static int
   3198 wpi_getrfkill(struct wpi_softc *sc)
   3199 {
   3200 	uint32_t tmp;
   3201 
   3202 	wpi_mem_lock(sc);
   3203 	tmp = wpi_mem_read(sc, WPI_MEM_RFKILL);
   3204 	wpi_mem_unlock(sc);
   3205 
   3206 	return !(tmp & 0x01);
   3207 }
   3208 
   3209 static int
   3210 wpi_sysctl_radio(SYSCTLFN_ARGS)
   3211 {
   3212 	struct sysctlnode node;
   3213 	struct wpi_softc *sc;
   3214 	int val, error;
   3215 
   3216 	node = *rnode;
   3217 	sc = (struct wpi_softc *)node.sysctl_data;
   3218 
   3219 	val = !wpi_getrfkill(sc);
   3220 
   3221 	node.sysctl_data = &val;
   3222 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   3223 
   3224 	if (error || newp == NULL)
   3225 		return error;
   3226 
   3227 	return 0;
   3228 }
   3229 
   3230 static void
   3231 wpi_sysctlattach(struct wpi_softc *sc)
   3232 {
   3233 	int rc;
   3234 	const struct sysctlnode *rnode;
   3235 	const struct sysctlnode *cnode;
   3236 
   3237 	struct sysctllog **clog = &sc->sc_sysctllog;
   3238 
   3239 	if ((rc = sysctl_createv(clog, 0, NULL, &rnode,
   3240 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
   3241 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0)
   3242 		goto err;
   3243 
   3244 	if ((rc = sysctl_createv(clog, 0, &rnode, &rnode,
   3245 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, device_xname(sc->sc_dev),
   3246 	    SYSCTL_DESCR("wpi controls and statistics"),
   3247 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
   3248 		goto err;
   3249 
   3250 	if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
   3251 	    CTLFLAG_PERMANENT, CTLTYPE_INT, "radio",
   3252 	    SYSCTL_DESCR("radio transmitter switch state (0=off, 1=on)"),
   3253 	    wpi_sysctl_radio, 0, sc, 0, CTL_CREATE, CTL_EOL)) != 0)
   3254 		goto err;
   3255 
   3256 #ifdef WPI_DEBUG
   3257 	/* control debugging printfs */
   3258 	if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
   3259 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
   3260 	    "debug", SYSCTL_DESCR("Enable debugging output"),
   3261 	    NULL, 0, &wpi_debug, 0, CTL_CREATE, CTL_EOL)) != 0)
   3262 		goto err;
   3263 #endif
   3264 
   3265 	return;
   3266 err:
   3267 	aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
   3268 }
   3269