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