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