Home | History | Annotate | Line # | Download | only in pci
if_iwi.c revision 1.17
      1 /*	$NetBSD: if_iwi.c,v 1.17 2005/08/30 21:05:47 skrll Exp $  */
      2 
      3 /*-
      4  * Copyright (c) 2004, 2005
      5  *      Damien Bergamini <damien.bergamini (at) free.fr>. All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice unmodified, this list of conditions, and the following
     12  *    disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: if_iwi.c,v 1.17 2005/08/30 21:05:47 skrll Exp $");
     32 
     33 /*-
     34  * Intel(R) PRO/Wireless 2200BG/2225BG/2915ABG driver
     35  * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
     36  */
     37 
     38 #include "bpfilter.h"
     39 
     40 #include <sys/param.h>
     41 #include <sys/sockio.h>
     42 #include <sys/sysctl.h>
     43 #include <sys/mbuf.h>
     44 #include <sys/kernel.h>
     45 #include <sys/socket.h>
     46 #include <sys/systm.h>
     47 #include <sys/malloc.h>
     48 #include <sys/conf.h>
     49 
     50 #include <machine/bus.h>
     51 #include <machine/endian.h>
     52 #include <machine/intr.h>
     53 
     54 #include <dev/pci/pcireg.h>
     55 #include <dev/pci/pcivar.h>
     56 #include <dev/pci/pcidevs.h>
     57 
     58 #if NBPFILTER > 0
     59 #include <net/bpf.h>
     60 #endif
     61 #include <net/if.h>
     62 #include <net/if_arp.h>
     63 #include <net/if_dl.h>
     64 #include <net/if_ether.h>
     65 #include <net/if_media.h>
     66 #include <net/if_types.h>
     67 
     68 #include <net80211/ieee80211_var.h>
     69 #include <net80211/ieee80211_radiotap.h>
     70 
     71 #include <netinet/in.h>
     72 #include <netinet/in_systm.h>
     73 #include <netinet/in_var.h>
     74 #include <netinet/ip.h>
     75 
     76 #include <crypto/arc4/arc4.h>
     77 
     78 #include <dev/pci/if_iwireg.h>
     79 #include <dev/pci/if_iwivar.h>
     80 
     81 static const struct ieee80211_rateset iwi_rateset_11a =
     82 	{ 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
     83 
     84 static const struct ieee80211_rateset iwi_rateset_11b =
     85 	{ 4, { 2, 4, 11, 22 } };
     86 
     87 static const struct ieee80211_rateset iwi_rateset_11g =
     88 	{ 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
     89 
     90 static int iwi_match(struct device *, struct cfdata *, void *);
     91 static void iwi_attach(struct device *, struct device *, void *);
     92 static int iwi_detach(struct device *, int);
     93 
     94 static void iwi_shutdown(void *);
     95 static int iwi_suspend(struct iwi_softc *);
     96 static int iwi_resume(struct iwi_softc *);
     97 static void iwi_powerhook(int, void *);
     98 
     99 static int iwi_alloc_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *,
    100     int);
    101 static void iwi_reset_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
    102 static void iwi_free_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
    103 static int iwi_alloc_tx_ring(struct iwi_softc *, struct iwi_tx_ring *,
    104     int);
    105 static void iwi_reset_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
    106 static void iwi_free_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
    107 static int iwi_alloc_rx_ring(struct iwi_softc *, struct iwi_rx_ring *,
    108     int);
    109 static void iwi_reset_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
    110 static void iwi_free_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
    111 
    112 static int iwi_media_change(struct ifnet *);
    113 static void iwi_media_status(struct ifnet *, struct ifmediareq *);
    114 static u_int16_t iwi_read_prom_word(struct iwi_softc *, u_int8_t);
    115 static int iwi_newstate(struct ieee80211com *, enum ieee80211_state, int);
    116 static void iwi_fix_channel(struct ieee80211com *, struct mbuf *);
    117 static void iwi_frame_intr(struct iwi_softc *, struct iwi_rx_data *, int,
    118     struct iwi_frame *);
    119 static void iwi_notification_intr(struct iwi_softc *, struct iwi_rx_data *,
    120     struct iwi_notif *);
    121 static void iwi_rx_intr(struct iwi_softc *);
    122 static void iwi_tx_intr(struct iwi_softc *);
    123 static int iwi_intr(void *);
    124 static int iwi_cmd(struct iwi_softc *, u_int8_t, void *, u_int8_t, int);
    125 static int iwi_tx_start(struct ifnet *, struct mbuf *, struct ieee80211_node *);
    126 static void iwi_start(struct ifnet *);
    127 static void iwi_watchdog(struct ifnet *);
    128 static int iwi_get_table0(struct iwi_softc *, u_int32_t *);
    129 static int iwi_get_radio(struct iwi_softc *, int *);
    130 static int iwi_ioctl(struct ifnet *, u_long, caddr_t);
    131 static void iwi_stop_master(struct iwi_softc *);
    132 static int iwi_reset(struct iwi_softc *);
    133 static int iwi_load_ucode(struct iwi_softc *, void *, int);
    134 static int iwi_load_firmware(struct iwi_softc *, void *, int);
    135 static int iwi_cache_firmware(struct iwi_softc *, void *);
    136 static void iwi_free_firmware(struct iwi_softc *);
    137 static int iwi_config(struct iwi_softc *);
    138 static int iwi_set_chan(struct iwi_softc *, struct ieee80211_channel *);
    139 static int iwi_scan(struct iwi_softc *);
    140 static int iwi_auth_and_assoc(struct iwi_softc *);
    141 static int iwi_init(struct ifnet *);
    142 static void iwi_stop(struct ifnet *, int);
    143 
    144 static __inline u_int8_t
    145 MEM_READ_1(struct iwi_softc *sc, u_int32_t addr)
    146 {
    147 	CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
    148 	return CSR_READ_1(sc, IWI_CSR_INDIRECT_DATA);
    149 }
    150 
    151 static __inline u_int32_t
    152 MEM_READ_4(struct iwi_softc *sc, u_int32_t addr)
    153 {
    154 	CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
    155 	return CSR_READ_4(sc, IWI_CSR_INDIRECT_DATA);
    156 }
    157 
    158 #ifdef IWI_DEBUG
    159 #define DPRINTF(x)	if (iwi_debug > 0) printf x
    160 #define DPRINTFN(n, x)	if (iwi_debug >= (n)) printf x
    161 int iwi_debug = 0;
    162 #else
    163 #define DPRINTF(x)
    164 #define DPRINTFN(n, x)
    165 #endif
    166 
    167 CFATTACH_DECL(iwi, sizeof (struct iwi_softc), iwi_match, iwi_attach,
    168     iwi_detach, NULL);
    169 
    170 static int
    171 iwi_match(struct device *parent, struct cfdata *match, void *aux)
    172 {
    173 	struct pci_attach_args *pa = aux;
    174 
    175 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
    176 		return 0;
    177 
    178 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2200BG ||
    179 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2225BG ||
    180 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_1 ||
    181 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_2)
    182 		return 1;
    183 
    184 	return 0;
    185 }
    186 
    187 /* Base Address Register */
    188 #define IWI_PCI_BAR0	0x10
    189 
    190 static void
    191 iwi_attach(struct device *parent, struct device *self, void *aux)
    192 {
    193 	struct iwi_softc *sc = (struct iwi_softc *)self;
    194 	struct ieee80211com *ic = &sc->sc_ic;
    195 	struct ifnet *ifp = &sc->sc_if;
    196 	struct pci_attach_args *pa = aux;
    197 	const char *intrstr;
    198 	char devinfo[256];
    199 	bus_space_tag_t memt;
    200 	bus_space_handle_t memh;
    201 	bus_addr_t base;
    202 	pci_intr_handle_t ih;
    203 	pcireg_t data;
    204 	u_int16_t val;
    205 	int error, revision, i;
    206 
    207 	sc->sc_pct = pa->pa_pc;
    208 	sc->sc_pcitag = pa->pa_tag;
    209 
    210 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof devinfo);
    211 	revision = PCI_REVISION(pa->pa_class);
    212 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo, revision);
    213 
    214 	/* clear device specific PCI configuration register 0x41 */
    215 	data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
    216 	data &= ~0x0000ff00;
    217 	pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data);
    218 
    219 	/* enable bus-mastering */
    220 	data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
    221 	data |= PCI_COMMAND_MASTER_ENABLE;
    222 	pci_conf_write(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG, data);
    223 
    224 	/* map the register window */
    225 	error = pci_mapreg_map(pa, IWI_PCI_BAR0, PCI_MAPREG_TYPE_MEM |
    226 	    PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh, &base, &sc->sc_sz);
    227 	if (error != 0) {
    228 		aprint_error("%s: could not map memory space\n",
    229 		    sc->sc_dev.dv_xname);
    230 		return;
    231 	}
    232 
    233 	sc->sc_st = memt;
    234 	sc->sc_sh = memh;
    235 	sc->sc_dmat = pa->pa_dmat;
    236 
    237 	/* disable interrupts */
    238 	CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
    239 
    240 	if (pci_intr_map(pa, &ih) != 0) {
    241 		aprint_error("%s: could not map interrupt\n",
    242 		    sc->sc_dev.dv_xname);
    243 		return;
    244 	}
    245 
    246 	intrstr = pci_intr_string(sc->sc_pct, ih);
    247 	sc->sc_ih = pci_intr_establish(sc->sc_pct, ih, IPL_NET, iwi_intr, sc);
    248 	if (sc->sc_ih == NULL) {
    249 		aprint_error("%s: could not establish interrupt",
    250 		    sc->sc_dev.dv_xname);
    251 		if (intrstr != NULL)
    252 			aprint_error(" at %s", intrstr);
    253 		aprint_error("\n");
    254 		return;
    255 	}
    256 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    257 
    258 	if (iwi_reset(sc) != 0) {
    259 		aprint_error("%s: could not reset adapter\n",
    260 		    sc->sc_dev.dv_xname);
    261 		return;
    262 	}
    263 
    264 	/*
    265 	 * Allocate rings.
    266 	 */
    267 	if (iwi_alloc_cmd_ring(sc, &sc->cmdq, IWI_CMD_RING_COUNT) != 0) {
    268 		aprint_error("%s: could not allocate command ring\n",
    269 		    sc->sc_dev.dv_xname);
    270 		goto fail;
    271 	}
    272 
    273 	if (iwi_alloc_tx_ring(sc, &sc->txq, IWI_TX_RING_COUNT) != 0) {
    274 		aprint_error("%s: could not allocate Tx ring\n",
    275 		    sc->sc_dev.dv_xname);
    276 		goto fail;
    277 	}
    278 
    279 	if (iwi_alloc_rx_ring(sc, &sc->rxq, IWI_RX_RING_COUNT) != 0) {
    280 		aprint_error("%s: could not allocate Rx ring\n",
    281 		    sc->sc_dev.dv_xname);
    282 		goto fail;
    283 	}
    284 
    285 	ic->ic_ifp = ifp;
    286 	ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
    287 	ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
    288 	ic->ic_state = IEEE80211_S_INIT;
    289 
    290 	/* set device capabilities */
    291 	ic->ic_caps = IEEE80211_C_WPA | IEEE80211_C_PMGT | IEEE80211_C_TXPMGT |
    292 	    IEEE80211_C_SHPREAMBLE | IEEE80211_C_MONITOR;
    293 
    294 	/* read MAC address from EEPROM */
    295 	val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 0);
    296 	ic->ic_myaddr[0] = val >> 8;
    297 	ic->ic_myaddr[1] = val & 0xff;
    298 	val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 1);
    299 	ic->ic_myaddr[2] = val >> 8;
    300 	ic->ic_myaddr[3] = val & 0xff;
    301 	val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 2);
    302 	ic->ic_myaddr[4] = val >> 8;
    303 	ic->ic_myaddr[5] = val & 0xff;
    304 
    305 	aprint_normal("%s: 802.11 address %s\n", sc->sc_dev.dv_xname,
    306 	    ether_sprintf(ic->ic_myaddr));
    307 
    308 
    309 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_1 ||
    310 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_2) {
    311 		/* set supported .11a rates (2915ABG only) */
    312 		ic->ic_sup_rates[IEEE80211_MODE_11A] = iwi_rateset_11a;
    313 
    314 		/* set supported .11a channels */
    315 		for (i = 36; i <= 64; i += 4) {
    316 			ic->ic_channels[i].ic_freq =
    317 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
    318 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
    319 		}
    320 		for (i = 149; i <= 165; i += 4) {
    321 			ic->ic_channels[i].ic_freq =
    322 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
    323 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
    324 		}
    325 	}
    326 
    327 	/* set supported .11b and .11g rates */
    328 	ic->ic_sup_rates[IEEE80211_MODE_11B] = iwi_rateset_11b;
    329 	ic->ic_sup_rates[IEEE80211_MODE_11G] = iwi_rateset_11g;
    330 
    331 	/* set supported .11b and .11g channels (1 through 14) */
    332 	for (i = 1; i <= 14; i++) {
    333 		ic->ic_channels[i].ic_freq =
    334 		    ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
    335 		ic->ic_channels[i].ic_flags =
    336 		    IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
    337 		    IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
    338 	}
    339 
    340 	ifp->if_softc = sc;
    341 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    342 	ifp->if_init = iwi_init;
    343 	ifp->if_stop = iwi_stop;
    344 	ifp->if_ioctl = iwi_ioctl;
    345 	ifp->if_start = iwi_start;
    346 	ifp->if_watchdog = iwi_watchdog;
    347 	IFQ_SET_READY(&ifp->if_snd);
    348 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
    349 
    350 	if_attach(ifp);
    351 	ieee80211_ifattach(ic);
    352 	/* override state transition machine */
    353 	sc->sc_newstate = ic->ic_newstate;
    354 	ic->ic_newstate = iwi_newstate;
    355 	ieee80211_media_init(ic, iwi_media_change, iwi_media_status);
    356 
    357 #if NBPFILTER > 0
    358 	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
    359 	    sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf);
    360 
    361 	sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
    362 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
    363 	sc->sc_rxtap.wr_ihdr.it_present = htole32(IWI_RX_RADIOTAP_PRESENT);
    364 
    365 	sc->sc_txtap_len = sizeof sc->sc_txtapu;
    366 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
    367 	sc->sc_txtap.wt_ihdr.it_present = htole32(IWI_TX_RADIOTAP_PRESENT);
    368 #endif
    369 
    370 	/*
    371 	 * Make sure the interface is shutdown during reboot.
    372 	 */
    373 	sc->sc_sdhook = shutdownhook_establish(iwi_shutdown, sc);
    374 	if (sc->sc_sdhook == NULL)
    375 		aprint_error("%s: WARNING: unable to establish shutdown hook\n",
    376 		    sc->sc_dev.dv_xname);
    377 	sc->sc_powerhook = powerhook_establish(iwi_powerhook, sc);
    378 	if (sc->sc_powerhook == NULL)
    379 		printf("%s: WARNING: unable to establish power hook\n",
    380 		    sc->sc_dev.dv_xname);
    381 
    382 	ieee80211_announce(ic);
    383 	/*
    384 	 * Add a few sysctl knobs.
    385 	 * XXX: Not yet.
    386 	 */
    387 	sc->dwelltime = 100;
    388 	sc->bluetooth = 1;
    389 	sc->antenna = 0;
    390 
    391 	return;
    392 
    393 fail:	iwi_detach(self, 0);
    394 }
    395 
    396 static int
    397 iwi_detach(struct device* self, int flags)
    398 {
    399 	struct iwi_softc *sc = (struct iwi_softc *)self;
    400 	struct ifnet *ifp = &sc->sc_if;
    401 
    402 	iwi_stop(ifp, 1);
    403 	iwi_free_firmware(sc);
    404 
    405 #if NBPFILTER > 0
    406 	bpfdetach(ifp);
    407 #endif
    408 	ieee80211_ifdetach(&sc->sc_ic);
    409 	if_detach(ifp);
    410 
    411 	iwi_free_cmd_ring(sc, &sc->cmdq);
    412 	iwi_free_tx_ring(sc, &sc->txq);
    413 	iwi_free_rx_ring(sc, &sc->rxq);
    414 
    415 	if (sc->sc_ih != NULL) {
    416 		pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
    417 		sc->sc_ih = NULL;
    418 	}
    419 
    420 	bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz);
    421 
    422 	return 0;
    423 }
    424 
    425 static int
    426 iwi_alloc_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring,
    427     int count)
    428 {
    429 	int error, nsegs;
    430 
    431 	ring->count = count;
    432 	ring->queued = 0;
    433 	ring->cur = ring->next = 0;
    434 
    435 	/*
    436 	 * Allocate and map command ring
    437 	 */
    438 	error = bus_dmamap_create(sc->sc_dmat,
    439 	    sizeof (struct iwi_cmd_desc) * count, 1,
    440 	    sizeof (struct iwi_cmd_desc) * count, 0,
    441 	    BUS_DMA_NOWAIT, &ring->desc_map);
    442 	if (error != 0) {
    443 		aprint_error("%s: could not create command ring DMA map\n",
    444 		    sc->sc_dev.dv_xname);
    445 		goto fail;
    446 	}
    447 
    448 	error = bus_dmamem_alloc(sc->sc_dmat,
    449 	    sizeof (struct iwi_cmd_desc) * count, PAGE_SIZE, 0,
    450 	    &sc->cmdq.desc_seg, 1, &nsegs, BUS_DMA_NOWAIT);
    451 	if (error != 0) {
    452 		aprint_error("%s: could not allocate command ring DMA memory\n",
    453 		    sc->sc_dev.dv_xname);
    454 		goto fail;
    455 	}
    456 
    457 	error = bus_dmamem_map(sc->sc_dmat, &sc->cmdq.desc_seg, nsegs,
    458 	    sizeof (struct iwi_cmd_desc) * count,
    459 	    (caddr_t *)&sc->cmdq.desc, BUS_DMA_NOWAIT);
    460 	if (error != 0) {
    461 		aprint_error("%s: could not map command ring DMA memory\n",
    462 		    sc->sc_dev.dv_xname);
    463 		goto fail;
    464 	}
    465 
    466 	error = bus_dmamap_load(sc->sc_dmat, sc->cmdq.desc_map, sc->cmdq.desc,
    467 	    sizeof (struct iwi_cmd_desc) * count, NULL,
    468 	    BUS_DMA_NOWAIT);
    469 	if (error != 0) {
    470 		aprint_error("%s: could not load command ring DMA map\n",
    471 		    sc->sc_dev.dv_xname);
    472 		goto fail;
    473 	}
    474 
    475 	memset(sc->cmdq.desc, 0,
    476 	    sizeof (struct iwi_cmd_desc) * count);
    477 
    478 	return 0;
    479 
    480 fail:	iwi_free_cmd_ring(sc, ring);
    481 	return error;
    482 }
    483 
    484 static void
    485 iwi_reset_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
    486 {
    487 	ring->queued = 0;
    488 	ring->cur = ring->next = 0;
    489 }
    490 
    491 static void
    492 iwi_free_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
    493 {
    494 	if (ring->desc_map != NULL) {
    495 		if (ring->desc != NULL) {
    496 			bus_dmamap_unload(sc->sc_dmat, ring->desc_map);
    497 			bus_dmamem_unmap(sc->sc_dmat, (caddr_t)ring->desc,
    498 			    sizeof (struct iwi_cmd_desc) * ring->count);
    499 			bus_dmamem_free(sc->sc_dmat, &ring->desc_seg, 1);
    500 		}
    501 		bus_dmamap_destroy(sc->sc_dmat, ring->desc_map);
    502 	}
    503 }
    504 
    505 static int
    506 iwi_alloc_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring,
    507     int count)
    508 {
    509 	int i, error, nsegs;
    510 
    511 	ring->count = count;
    512 	ring->queued = 0;
    513 	ring->cur = ring->next = 0;
    514 
    515 	/*
    516 	 * Allocate and map Tx ring
    517 	 */
    518 	error = bus_dmamap_create(sc->sc_dmat,
    519 	    sizeof (struct iwi_tx_desc) * count, 1,
    520 	    sizeof (struct iwi_tx_desc) * count, 0, BUS_DMA_NOWAIT,
    521 	    &ring->desc_map);
    522 	if (error != 0) {
    523 		aprint_error("%s: could not create tx ring DMA map\n",
    524 		    sc->sc_dev.dv_xname);
    525 		goto fail;
    526 	}
    527 
    528 	error = bus_dmamem_alloc(sc->sc_dmat,
    529 	    sizeof (struct iwi_tx_desc) * count, PAGE_SIZE, 0,
    530 	    &ring->desc_seg, 1, &nsegs, BUS_DMA_NOWAIT);
    531 	if (error != 0) {
    532 		aprint_error("%s: could not allocate tx ring DMA memory\n",
    533 		    sc->sc_dev.dv_xname);
    534 		goto fail;
    535 	}
    536 
    537 	error = bus_dmamem_map(sc->sc_dmat, &ring->desc_seg, nsegs,
    538 	    sizeof (struct iwi_tx_desc) * count,
    539 	    (caddr_t *)&ring->desc, BUS_DMA_NOWAIT);
    540 	if (error != 0) {
    541 		aprint_error("%s: could not map tx ring DMA memory\n",
    542 		    sc->sc_dev.dv_xname);
    543 		goto fail;
    544 	}
    545 
    546 	error = bus_dmamap_load(sc->sc_dmat, ring->desc_map, ring->desc,
    547 	    sizeof (struct iwi_tx_desc) * count, NULL,
    548 	    BUS_DMA_NOWAIT);
    549 	if (error != 0) {
    550 		aprint_error("%s: could not load tx ring DMA map\n",
    551 		    sc->sc_dev.dv_xname);
    552 		goto fail;
    553 	}
    554 
    555 	memset(ring->desc, 0, sizeof (struct iwi_tx_desc) * count);
    556 
    557 	ring->data = malloc(count * sizeof (struct iwi_tx_data), M_DEVBUF,
    558 	    M_NOWAIT | M_ZERO);
    559 	if (ring->data == NULL) {
    560 		aprint_error("%s: could not allocate soft data\n",
    561 		    sc->sc_dev.dv_xname);
    562 		error = ENOMEM;
    563 		goto fail;
    564 	}
    565 
    566 	/*
    567 	 * Allocate Tx buffers DMA maps
    568 	 */
    569 	for (i = 0; i < count; i++) {
    570 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, IWI_MAX_NSEG,
    571 		    MCLBYTES, 0, BUS_DMA_NOWAIT, &ring->data[i].map);
    572 		if (error != 0) {
    573 			aprint_error("%s: could not create tx buf DMA map",
    574 			    sc->sc_dev.dv_xname);
    575 			goto fail;
    576 		}
    577 	}
    578 	return 0;
    579 
    580 fail:	iwi_free_tx_ring(sc, ring);
    581 	return error;
    582 }
    583 
    584 static void
    585 iwi_reset_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
    586 {
    587 	struct iwi_tx_data *data;
    588 	int i;
    589 
    590 	for (i = 0; i < ring->count; i++) {
    591 		data = &ring->data[i];
    592 
    593 		if (data->m != NULL) {
    594 			bus_dmamap_sync(sc->sc_dmat, data->map, 0,
    595 			    MCLBYTES, BUS_DMASYNC_POSTWRITE);
    596 			bus_dmamap_unload(sc->sc_dmat, data->map);
    597 			m_freem(data->m);
    598 			data->m = NULL;
    599 		}
    600 
    601 		if (data->ni != NULL) {
    602 			ieee80211_free_node(data->ni);
    603 			data->ni = NULL;
    604 		}
    605 	}
    606 
    607 	ring->queued = 0;
    608 	ring->cur = ring->next = 0;
    609 }
    610 
    611 static void
    612 iwi_free_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
    613 {
    614 	int i;
    615 
    616 	if (ring->desc_map != NULL) {
    617 		if (ring->desc != NULL) {
    618 			bus_dmamap_unload(sc->sc_dmat, ring->desc_map);
    619 			bus_dmamem_unmap(sc->sc_dmat, (caddr_t)ring->desc,
    620 			    sizeof (struct iwi_tx_desc) * ring->count);
    621 			bus_dmamem_free(sc->sc_dmat, &ring->desc_seg, 1);
    622 		}
    623 		bus_dmamap_destroy(sc->sc_dmat, ring->desc_map);
    624 	}
    625 
    626 	for (i = 0; i < ring->count; i++) {
    627 		if (ring->data[i].m != NULL) {
    628 			bus_dmamap_unload(sc->sc_dmat, ring->data[i].map);
    629 			m_freem(ring->data[i].m);
    630 		}
    631 		bus_dmamap_destroy(sc->sc_dmat, ring->data[i].map);
    632 	}
    633 }
    634 
    635 static int
    636 iwi_alloc_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring,
    637     int count)
    638 {
    639 	int i, error;
    640 
    641 	ring->count = count;
    642 	ring->cur = 0;
    643 
    644 	ring->data = malloc(count * sizeof (struct iwi_rx_data), M_DEVBUF,
    645 	    M_NOWAIT | M_ZERO);
    646 	if (ring->data == NULL) {
    647 		aprint_error("%s: could not allocate soft data\n",
    648 		    sc->sc_dev.dv_xname);
    649 		error = ENOMEM;
    650 		goto fail;
    651 	}
    652 
    653 	/*
    654 	 * Allocate and map Rx buffers
    655 	 */
    656 	for (i = 0; i < count; i++) {
    657 
    658 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
    659 		    0, BUS_DMA_NOWAIT, &ring->data[i].map);
    660 		if (error != 0) {
    661 			aprint_error("%s: could not create rx buf DMA map",
    662 			    sc->sc_dev.dv_xname);
    663 			goto fail;
    664 		}
    665 
    666 		MGETHDR(ring->data[i].m, M_DONTWAIT, MT_DATA);
    667 		if (ring->data[i].m == NULL) {
    668 			aprint_error("%s: could not allocate rx mbuf\n",
    669 			    sc->sc_dev.dv_xname);
    670 			error = ENOMEM;
    671 			goto fail;
    672 		}
    673 
    674 		MCLGET(ring->data[i].m, M_DONTWAIT);
    675 		if (!(ring->data[i].m->m_flags & M_EXT)) {
    676 			m_freem(ring->data[i].m);
    677 			aprint_error("%s: could not allocate rx mbuf cluster\n",
    678 			    sc->sc_dev.dv_xname);
    679 			error = ENOMEM;
    680 			goto fail;
    681 		}
    682 
    683 		error = bus_dmamap_load(sc->sc_dmat, ring->data[i].map,
    684 		    mtod(ring->data[i].m, void *), MCLBYTES, NULL,
    685 		    BUS_DMA_NOWAIT);
    686 		if (error != 0) {
    687 			aprint_error("%s: could not load rx buffer DMA map\n",
    688 			    sc->sc_dev.dv_xname);
    689 			goto fail;
    690 		}
    691 	}
    692 
    693 	return 0;
    694 
    695 fail:	iwi_free_rx_ring(sc, ring);
    696 	return error;
    697 }
    698 
    699 static void
    700 iwi_reset_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
    701 {
    702 	ring->cur = 0;
    703 }
    704 
    705 static void
    706 iwi_free_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
    707 {
    708 	int i;
    709 
    710 	for (i = 0; i < ring->count; i++) {
    711 		if (ring->data[i].m != NULL) {
    712 			bus_dmamap_unload(sc->sc_dmat, ring->data[i].map);
    713 			m_freem(ring->data[i].m);
    714 		}
    715 		bus_dmamap_destroy(sc->sc_dmat, ring->data[i].map);
    716 	}
    717 }
    718 
    719 static void
    720 iwi_shutdown(void *arg)
    721 {
    722 	struct iwi_softc *sc = (struct iwi_softc *)arg;
    723 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
    724 
    725 	iwi_stop(ifp, 1);
    726 }
    727 
    728 static int
    729 iwi_suspend(struct iwi_softc *sc)
    730 {
    731 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
    732 
    733 	iwi_stop(ifp, 1);
    734 
    735 	return 0;
    736 }
    737 
    738 static int
    739 iwi_resume(struct iwi_softc *sc)
    740 {
    741 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
    742 	pcireg_t data;
    743 
    744 	/* clear device specific PCI configuration register 0x41 */
    745 	data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
    746 	data &= ~0x0000ff00;
    747 	pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data);
    748 
    749 	if (ifp->if_flags & IFF_UP) {
    750 		iwi_init(ifp);
    751 		if (ifp->if_flags & IFF_RUNNING)
    752 			iwi_start(ifp);
    753 	}
    754 
    755 	return 0;
    756 }
    757 
    758 static void
    759 iwi_powerhook(int why, void *arg)
    760 {
    761         struct iwi_softc *sc = arg;
    762 	int s;
    763 
    764 	s = splnet();
    765 	switch (why) {
    766 	case PWR_SUSPEND:
    767 	case PWR_STANDBY:
    768 		iwi_suspend(sc);
    769 		break;
    770 	case PWR_RESUME:
    771 		iwi_resume(sc);
    772 		break;
    773 	case PWR_SOFTSUSPEND:
    774 	case PWR_SOFTSTANDBY:
    775 	case PWR_SOFTRESUME:
    776 		break;
    777 	}
    778 	splx(s);
    779 }
    780 
    781 static int
    782 iwi_media_change(struct ifnet *ifp)
    783 {
    784 	int error;
    785 
    786 	error = ieee80211_media_change(ifp);
    787 	if (error != ENETRESET)
    788 		return error;
    789 
    790 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
    791 		iwi_init(ifp);
    792 
    793 	return 0;
    794 }
    795 
    796 static void
    797 iwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
    798 {
    799 	struct iwi_softc *sc = ifp->if_softc;
    800 	struct ieee80211com *ic = &sc->sc_ic;
    801 #define N(a)	(sizeof (a) / sizeof (a[0]))
    802 	static const struct {
    803 		u_int32_t	val;
    804 		int		rate;
    805 	} rates[] = {
    806 		{ IWI_RATE_DS1,      2 },
    807 		{ IWI_RATE_DS2,      4 },
    808 		{ IWI_RATE_DS5,     11 },
    809 		{ IWI_RATE_DS11,    22 },
    810 		{ IWI_RATE_OFDM6,   12 },
    811 		{ IWI_RATE_OFDM9,   18 },
    812 		{ IWI_RATE_OFDM12,  24 },
    813 		{ IWI_RATE_OFDM18,  36 },
    814 		{ IWI_RATE_OFDM24,  48 },
    815 		{ IWI_RATE_OFDM36,  72 },
    816 		{ IWI_RATE_OFDM48,  96 },
    817 		{ IWI_RATE_OFDM54, 108 },
    818 	};
    819 	u_int32_t val;
    820 	int rate, i;
    821 
    822 	imr->ifm_status = IFM_AVALID;
    823 	imr->ifm_active = IFM_IEEE80211;
    824 	if (ic->ic_state == IEEE80211_S_RUN)
    825 		imr->ifm_status |= IFM_ACTIVE;
    826 
    827 	/* read current transmission rate from adapter */
    828 	val = CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE);
    829 
    830 	/* convert rate to 802.11 rate */
    831 	for (i = 0; i < N(rates) && rates[i].val != val; i++);
    832 	rate = (i < N(rates)) ? rates[i].rate : 0;
    833 
    834 	imr->ifm_active |= ieee80211_rate2media(ic, rate, ic->ic_curmode);
    835 	switch (ic->ic_opmode) {
    836 	case IEEE80211_M_STA:
    837 		break;
    838 
    839 	case IEEE80211_M_IBSS:
    840 		imr->ifm_active |= IFM_IEEE80211_ADHOC;
    841 		break;
    842 
    843 	case IEEE80211_M_MONITOR:
    844 		imr->ifm_active |= IFM_IEEE80211_MONITOR;
    845 		break;
    846 
    847 	case IEEE80211_M_AHDEMO:
    848 	case IEEE80211_M_HOSTAP:
    849 		/* should not get there */
    850 		break;
    851 	}
    852 #undef N
    853 }
    854 
    855 static int
    856 iwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
    857 {
    858 	struct iwi_softc *sc = ic->ic_ifp->if_softc;
    859 
    860 	switch (nstate) {
    861 	case IEEE80211_S_SCAN:
    862 		if (sc->flags & IWI_FLAG_SCANNING)
    863 			break;
    864 
    865 		ieee80211_node_table_reset(&ic->ic_scan);
    866 		ic->ic_flags |= IEEE80211_F_SCAN | IEEE80211_F_ASCAN;
    867 		sc->flags |= IWI_FLAG_SCANNING;
    868 		iwi_scan(sc);
    869 		break;
    870 
    871 	case IEEE80211_S_AUTH:
    872 		iwi_auth_and_assoc(sc);
    873 		break;
    874 
    875 	case IEEE80211_S_RUN:
    876 		if (ic->ic_opmode == IEEE80211_M_IBSS)
    877 			ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
    878 		else if (ic->ic_opmode == IEEE80211_M_MONITOR)
    879 			iwi_set_chan(sc, ic->ic_ibss_chan);
    880 
    881 		return (*sc->sc_newstate)(ic, nstate,
    882 		    IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
    883 
    884 	case IEEE80211_S_ASSOC:
    885 		break;
    886 
    887 	case IEEE80211_S_INIT:
    888 		sc->flags &= ~IWI_FLAG_SCANNING;
    889 		break;
    890 	}
    891 
    892 	ic->ic_state = nstate;
    893 	return 0;
    894 }
    895 
    896 /*
    897  * Read 16 bits at address 'addr' from the serial EEPROM.
    898  * DON'T PLAY WITH THIS CODE UNLESS YOU KNOW *EXACTLY* WHAT YOU'RE DOING!
    899  */
    900 static u_int16_t
    901 iwi_read_prom_word(struct iwi_softc *sc, u_int8_t addr)
    902 {
    903 	u_int32_t tmp;
    904 	u_int16_t val;
    905 	int n;
    906 
    907 	/* Clock C once before the first command */
    908 	IWI_EEPROM_CTL(sc, 0);
    909 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
    910 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
    911 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
    912 
    913 	/* Write start bit (1) */
    914 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
    915 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
    916 
    917 	/* Write READ opcode (10) */
    918 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
    919 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
    920 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
    921 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
    922 
    923 	/* Write address A7-A0 */
    924 	for (n = 7; n >= 0; n--) {
    925 		IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
    926 		    (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D));
    927 		IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
    928 		    (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D) | IWI_EEPROM_C);
    929 	}
    930 
    931 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
    932 
    933 	/* Read data Q15-Q0 */
    934 	val = 0;
    935 	for (n = 15; n >= 0; n--) {
    936 		IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
    937 		IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
    938 		tmp = MEM_READ_4(sc, IWI_MEM_EEPROM_CTL);
    939 		val |= ((tmp & IWI_EEPROM_Q) >> IWI_EEPROM_SHIFT_Q) << n;
    940 	}
    941 
    942 	IWI_EEPROM_CTL(sc, 0);
    943 
    944 	/* Clear Chip Select and clock C */
    945 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
    946 	IWI_EEPROM_CTL(sc, 0);
    947 	IWI_EEPROM_CTL(sc, IWI_EEPROM_C);
    948 
    949 	return be16toh(val);
    950 }
    951 
    952 /*
    953  * XXX: Hack to set the current channel to the value advertised in beacons or
    954  * probe responses. Only used during AP detection.
    955  */
    956 static void
    957 iwi_fix_channel(struct ieee80211com *ic, struct mbuf *m)
    958 {
    959 	struct ieee80211_frame *wh;
    960 	u_int8_t subtype;
    961 	u_int8_t *frm, *efrm;
    962 
    963 	wh = mtod(m, struct ieee80211_frame *);
    964 
    965 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
    966 		return;
    967 
    968 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
    969 
    970 	if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
    971 	    subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
    972 		return;
    973 
    974 	frm = (u_int8_t *)(wh + 1);
    975 	efrm = mtod(m, u_int8_t *) + m->m_len;
    976 
    977 	frm += 12;	/* skip tstamp, bintval and capinfo fields */
    978 	while (frm < efrm) {
    979 		if (*frm == IEEE80211_ELEMID_DSPARMS)
    980 #if IEEE80211_CHAN_MAX < 255
    981 		if (frm[2] <= IEEE80211_CHAN_MAX)
    982 #endif
    983 			ic->ic_bss->ni_chan = &ic->ic_channels[frm[2]];
    984 
    985 		frm += frm[1] + 2;
    986 	}
    987 }
    988 
    989 static void
    990 iwi_frame_intr(struct iwi_softc *sc, struct iwi_rx_data *data, int i,
    991     struct iwi_frame *frame)
    992 {
    993 	struct ieee80211com *ic = &sc->sc_ic;
    994 	struct ifnet *ifp = ic->ic_ifp;
    995 	struct mbuf *m;
    996 	struct ieee80211_frame *wh;
    997 	struct ieee80211_node *ni;
    998 	int error;
    999 
   1000 	DPRINTFN(5, ("received frame len=%u chan=%u rssi=%u\n",
   1001 	    le16toh(frame->len), frame->chan, frame->rssi_dbm));
   1002 
   1003 	bus_dmamap_sync(sc->sc_dmat, data->map, sizeof (struct iwi_hdr),
   1004 	    sizeof (struct iwi_frame) + le16toh(frame->len),
   1005 	    BUS_DMASYNC_POSTREAD);
   1006 
   1007 	if (le16toh(frame->len) < sizeof (struct ieee80211_frame) ||
   1008 	    le16toh(frame->len) > MCLBYTES) {
   1009 		DPRINTF(("%s: bad frame length\n", sc->sc_dev.dv_xname));
   1010 		ifp->if_ierrors++;
   1011 		return;
   1012 	}
   1013 
   1014 	bus_dmamap_unload(sc->sc_dmat, data->map);
   1015 
   1016 	/* Finalize mbuf */
   1017 	m = data->m;
   1018 	m->m_pkthdr.rcvif = ifp;
   1019 	m->m_pkthdr.len = m->m_len = sizeof (struct iwi_hdr) +
   1020 	    sizeof (struct iwi_frame) + le16toh(frame->len);
   1021 
   1022 	m_adj(m, sizeof (struct iwi_hdr) + sizeof (struct iwi_frame));
   1023 
   1024 	if (ic->ic_state == IEEE80211_S_SCAN)
   1025 		iwi_fix_channel(ic, m);
   1026 
   1027 #if NBPFILTER > 0
   1028 	if (sc->sc_drvbpf != NULL) {
   1029 		struct iwi_rx_radiotap_header *tap = &sc->sc_rxtap;
   1030 
   1031 		tap->wr_flags = 0;
   1032 		tap->wr_rate = frame->rate;
   1033 		tap->wr_chan_freq =
   1034 		    htole16(ic->ic_channels[frame->chan].ic_freq);
   1035 		tap->wr_chan_flags =
   1036 		    htole16(ic->ic_channels[frame->chan].ic_flags);
   1037 		tap->wr_antsignal = frame->signal;
   1038 		tap->wr_antenna = frame->antenna;
   1039 
   1040 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m);
   1041 	}
   1042 #endif
   1043 
   1044 	wh = mtod(m, struct ieee80211_frame *);
   1045 	ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
   1046 
   1047 	/* Send the frame to the upper layer */
   1048 	ieee80211_input(ic, m, ni, frame->rssi_dbm, 0);
   1049 
   1050 	/* node is no longer needed */
   1051 	ieee80211_free_node(ni);
   1052 
   1053 	MGETHDR(data->m, M_DONTWAIT, MT_DATA);
   1054 	if (data->m == NULL) {
   1055 		aprint_error("%s: could not allocate rx mbuf\n",
   1056 		    sc->sc_dev.dv_xname);
   1057 		return;
   1058 	}
   1059 
   1060 	MCLGET(data->m, M_DONTWAIT);
   1061 	if (!(data->m->m_flags & M_EXT)) {
   1062 		aprint_error("%s: could not allocate rx mbuf cluster\n",
   1063 		    sc->sc_dev.dv_xname);
   1064 		m_freem(data->m);
   1065 		data->m = NULL;
   1066 		return;
   1067 	}
   1068 
   1069 	error = bus_dmamap_load(sc->sc_dmat, data->map, mtod(data->m, void *),
   1070 	    MCLBYTES, NULL, BUS_DMA_NOWAIT);
   1071 	if (error != 0) {
   1072 		aprint_error("%s: could not load rx buf DMA map\n",
   1073 		    sc->sc_dev.dv_xname);
   1074 		m_freem(data->m);
   1075 		data->m = NULL;
   1076 		return;
   1077 	}
   1078 
   1079 	CSR_WRITE_4(sc, IWI_CSR_RX_BASE + i * 4, data->map->dm_segs[0].ds_addr);
   1080 }
   1081 
   1082 static void
   1083 iwi_notification_intr(struct iwi_softc *sc, struct iwi_rx_data *buf,
   1084     struct iwi_notif *notif)
   1085 {
   1086 	struct ieee80211com *ic = &sc->sc_ic;
   1087 	struct iwi_notif_scan_channel *chan;
   1088 	struct iwi_notif_scan_complete *scan;
   1089 	struct iwi_notif_authentication *auth;
   1090 	struct iwi_notif_association *assoc;
   1091 
   1092 	bus_dmamap_sync(sc->sc_dmat, buf->map, sizeof (struct iwi_hdr),
   1093 	    sizeof (struct iwi_notif) + le16toh(notif->len),
   1094 	    BUS_DMASYNC_POSTREAD);
   1095 
   1096 	switch (notif->type) {
   1097 	case IWI_NOTIF_TYPE_SCAN_CHANNEL:
   1098 		chan = (struct iwi_notif_scan_channel *)(notif + 1);
   1099 
   1100 		DPRINTFN(2, ("Scanning channel (%u)\n", chan->nchan));
   1101 		break;
   1102 
   1103 	case IWI_NOTIF_TYPE_SCAN_COMPLETE:
   1104 		scan = (struct iwi_notif_scan_complete *)(notif + 1);
   1105 
   1106 		DPRINTFN(2, ("Scan completed (%u, %u)\n", scan->nchan,
   1107 		    scan->status));
   1108 
   1109 		/* monitor mode uses scan to set the channel ... */
   1110 		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
   1111 			sc->flags &= ~IWI_FLAG_SCANNING;
   1112 			ieee80211_end_scan(ic);
   1113 		} else
   1114 			iwi_set_chan(sc, ic->ic_ibss_chan);
   1115 		break;
   1116 
   1117 	case IWI_NOTIF_TYPE_AUTHENTICATION:
   1118 		auth = (struct iwi_notif_authentication *)(notif + 1);
   1119 
   1120 		DPRINTFN(2, ("Authentication (%u)\n", auth->state));
   1121 
   1122 		switch (auth->state) {
   1123 		case IWI_AUTHENTICATED:
   1124 			ieee80211_node_authorize(ic, ic->ic_bss);
   1125 			ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1);
   1126 			break;
   1127 
   1128 		case IWI_DEAUTHENTICATED:
   1129 			break;
   1130 
   1131 		default:
   1132 			aprint_error("%s: unknown authentication state %u\n",
   1133 			    sc->sc_dev.dv_xname, auth->state);
   1134 		}
   1135 		break;
   1136 
   1137 	case IWI_NOTIF_TYPE_ASSOCIATION:
   1138 		assoc = (struct iwi_notif_association *)(notif + 1);
   1139 
   1140 		DPRINTFN(2, ("Association (%u, %u)\n", assoc->state,
   1141 		    assoc->status));
   1142 
   1143 		switch (assoc->state) {
   1144 		case IWI_AUTHENTICATED:
   1145 			/* re-association, do nothing */
   1146 			break;
   1147 
   1148 		case IWI_ASSOCIATED:
   1149 			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
   1150 			break;
   1151 
   1152 		case IWI_DEASSOCIATED:
   1153 			ieee80211_begin_scan(ic, 1);
   1154 			break;
   1155 
   1156 		default:
   1157 			aprint_error("%s: unknown association state %u\n",
   1158 			    sc->sc_dev.dv_xname, assoc->state);
   1159 		}
   1160 		break;
   1161 
   1162 	case IWI_NOTIF_TYPE_CALIBRATION:
   1163 	case IWI_NOTIF_TYPE_BEACON:
   1164 	case IWI_NOTIF_TYPE_NOISE:
   1165 		DPRINTFN(5, ("Notification (%u)\n", notif->type));
   1166 		break;
   1167 
   1168 	default:
   1169 		aprint_error("%s: unknown notification type %u\n",
   1170 		    sc->sc_dev.dv_xname, notif->type);
   1171 	}
   1172 }
   1173 
   1174 static void
   1175 iwi_rx_intr(struct iwi_softc *sc)
   1176 {
   1177 	struct iwi_rx_data *data;
   1178 	struct iwi_hdr *hdr;
   1179 	uint32_t hw;
   1180 
   1181 	hw = CSR_READ_4(sc, IWI_CSR_RX_RIDX);
   1182 
   1183 	for (; sc->rxq.cur != hw;) {
   1184 		data = &sc->rxq.data[sc->rxq.cur];
   1185 
   1186 		bus_dmamap_sync(sc->sc_dmat, data->map, 0,
   1187 		    sizeof (struct iwi_hdr), BUS_DMASYNC_POSTREAD);
   1188 
   1189 		hdr = mtod(data->m, struct iwi_hdr *);
   1190 
   1191 		switch (hdr->type) {
   1192 		case IWI_HDR_TYPE_FRAME:
   1193 			iwi_frame_intr(sc, data, sc->rxq.cur,
   1194 			    (struct iwi_frame *)(hdr + 1));
   1195 			break;
   1196 
   1197 		case IWI_HDR_TYPE_NOTIF:
   1198 			iwi_notification_intr(sc, data,
   1199 			    (struct iwi_notif *)(hdr + 1));
   1200 			break;
   1201 
   1202 		default:
   1203 			aprint_error("%s: unknown hdr type %u\n",
   1204 			    sc->sc_dev.dv_xname, hdr->type);
   1205 		}
   1206 
   1207 		DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur));
   1208 
   1209 		sc->rxq.cur = (sc->rxq.cur + 1) % sc->rxq.count;
   1210 	}
   1211 
   1212 
   1213 	/* Tell the firmware what we have processed */
   1214 	hw = (hw == 0) ? sc->rxq.count - 1 : hw - 1;
   1215 	CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, hw);
   1216 }
   1217 
   1218 static void
   1219 iwi_tx_intr(struct iwi_softc *sc)
   1220 {
   1221 	struct ifnet *ifp = &sc->sc_if;
   1222 	struct iwi_tx_data *data;
   1223 	u_int32_t hw;
   1224 
   1225 	hw = CSR_READ_4(sc, IWI_CSR_TX1_RIDX);
   1226 
   1227 	for (; sc->txq.next != hw;) {
   1228 		data = &sc->txq.data[sc->txq.next];
   1229 
   1230 		bus_dmamap_sync(sc->sc_dmat, data->map, 0,
   1231 		    MCLBYTES, BUS_DMASYNC_POSTWRITE);
   1232 		bus_dmamap_unload(sc->sc_dmat, data->map);
   1233 		m_freem(data->m);
   1234 		data->m = NULL;
   1235 		ieee80211_free_node(data->ni);
   1236 		data->ni = NULL;
   1237 
   1238 		DPRINTFN(15, ("tx done idx=%u\n", sc->txq.next));
   1239 
   1240 		ifp->if_opackets++;
   1241 
   1242 		sc->txq.queued--;
   1243 		sc->txq.next = (sc->txq.next + 1) % sc->txq.count;
   1244 	}
   1245 
   1246 	sc->sc_tx_timer = 0;
   1247 	ifp->if_flags &= ~IFF_OACTIVE;
   1248 
   1249 	/* Call start() since some buffer descriptors have been released */
   1250 	(*ifp->if_start)(ifp);
   1251 }
   1252 
   1253 static int
   1254 iwi_intr(void *arg)
   1255 {
   1256 	struct iwi_softc *sc = arg;
   1257 	u_int32_t r;
   1258 
   1259 	if ((r = CSR_READ_4(sc, IWI_CSR_INTR)) == 0 || r == 0xffffffff)
   1260 		return 0;
   1261 
   1262 	/* Disable interrupts */
   1263 	CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
   1264 
   1265 	if (r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR)) {
   1266 		aprint_error("%s: fatal error\n", sc->sc_dev.dv_xname);
   1267 		sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP;
   1268 		iwi_stop(&sc->sc_if, 1);
   1269 	}
   1270 
   1271 	if (r & IWI_INTR_FW_INITED) {
   1272 		if (!(r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR)))
   1273 			wakeup(sc);
   1274 	}
   1275 
   1276 	if (r & IWI_INTR_RADIO_OFF) {
   1277 		DPRINTF(("radio transmitter off\n"));
   1278 		sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP;
   1279 		iwi_stop(&sc->sc_if, 1);
   1280 	}
   1281 
   1282 	if (r & IWI_INTR_RX_DONE)
   1283 		iwi_rx_intr(sc);
   1284 
   1285 	if (r & IWI_INTR_CMD_DONE)
   1286 		wakeup(sc);
   1287 
   1288 	if (r & IWI_INTR_TX1_DONE)
   1289 		iwi_tx_intr(sc);
   1290 
   1291 	/* Acknowledge interrupts */
   1292 	CSR_WRITE_4(sc, IWI_CSR_INTR, r);
   1293 
   1294 	/* Re-enable interrupts */
   1295 	CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
   1296 
   1297 	return 1;
   1298 }
   1299 
   1300 static int
   1301 iwi_cmd(struct iwi_softc *sc, u_int8_t type, void *data, u_int8_t len,
   1302     int async)
   1303 {
   1304 	struct iwi_cmd_desc *desc;
   1305 
   1306 	desc = &sc->cmdq.desc[sc->cmdq.cur];
   1307 
   1308 	desc->hdr.type = IWI_HDR_TYPE_COMMAND;
   1309 	desc->hdr.flags = IWI_HDR_FLAG_IRQ;
   1310 	desc->type = type;
   1311 	desc->len = len;
   1312 	memcpy(desc->data, data, len);
   1313 
   1314 	bus_dmamap_sync(sc->sc_dmat, sc->cmdq.desc_map,
   1315 	    sc->cmdq.cur * sizeof (struct iwi_cmd_desc),
   1316 	    sizeof (struct iwi_cmd_desc), BUS_DMASYNC_PREWRITE);
   1317 
   1318 	DPRINTFN(2, ("sending command type=%u len=%u\n",
   1319 	    type, len));
   1320 
   1321 	sc->cmdq.cur = (sc->cmdq.cur + 1) % sc->cmdq.count;
   1322 	CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
   1323 
   1324 	return async ? 0 : tsleep(sc, 0, "iwicmd", hz);
   1325 }
   1326 
   1327 static int
   1328 iwi_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni)
   1329 {
   1330 	struct iwi_softc *sc = ifp->if_softc;
   1331 	struct ieee80211com *ic = &sc->sc_ic;
   1332 	struct ieee80211_frame wh;
   1333 	struct ieee80211_key *k;
   1334 	struct iwi_tx_data *data;
   1335 	struct iwi_tx_desc *desc;
   1336 	struct mbuf *mnew;
   1337 	int error, i;
   1338 
   1339 	(void)memcpy(&wh, mtod(m0, struct ieee80211_frame *), sizeof(wh));
   1340 	if (wh.i_fc[1] & IEEE80211_FC1_WEP) {
   1341 		k = ieee80211_crypto_encap(ic, ni, m0);
   1342 		if (k == NULL) {
   1343 			m_freem(m0);
   1344 			return ENOBUFS;
   1345 		}
   1346 	}
   1347 
   1348 #if NBPFILTER > 0
   1349 	if (sc->sc_drvbpf != NULL) {
   1350 		struct iwi_tx_radiotap_header *tap = &sc->sc_txtap;
   1351 
   1352 		tap->wt_flags = 0;
   1353 		tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
   1354 		tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
   1355 
   1356 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
   1357 	}
   1358 #endif
   1359 
   1360 	data = &sc->txq.data[sc->txq.cur];
   1361 	desc = &sc->txq.desc[sc->txq.cur];
   1362 
   1363 	/* trim IEEE802.11 header */
   1364 	m_adj(m0, sizeof (struct ieee80211_frame));
   1365 
   1366 	error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0, BUS_DMA_NOWAIT);
   1367 	if (error != 0 && error != EFBIG) {
   1368 		aprint_error("%s: could not map mbuf (error %d)\n",
   1369 		    sc->sc_dev.dv_xname, error);
   1370 		m_freem(m0);
   1371 		return error;
   1372 	}
   1373 	if (error != 0) {
   1374 		/* too many fragments, linearize */
   1375 
   1376 		MGETHDR(mnew, M_DONTWAIT, MT_DATA);
   1377 		if (mnew == NULL) {
   1378 			m_freem(m0);
   1379 			return ENOMEM;
   1380 		}
   1381 
   1382 		M_COPY_PKTHDR(mnew, m0);
   1383 		MCLGET(mnew, M_DONTWAIT);
   1384 		if (!(mnew->m_flags & M_EXT)) {
   1385 			m_freem(m0);
   1386 			m_freem(mnew);
   1387 			return ENOMEM;
   1388 		}
   1389 
   1390 		m_copydata(m0, 0, m0->m_pkthdr.len, mtod(mnew, caddr_t));
   1391 		m_freem(m0);
   1392 		mnew->m_len = mnew->m_pkthdr.len;
   1393 		m0 = mnew;
   1394 
   1395 		error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
   1396 		    BUS_DMA_NOWAIT);
   1397 		if (error != 0) {
   1398 			aprint_error("%s: could not map mbuf (error %d)\n",
   1399 			    sc->sc_dev.dv_xname, error);
   1400 			m_freem(m0);
   1401 			return error;
   1402 		}
   1403 	}
   1404 
   1405 	data->m = m0;
   1406 	data->ni = ni;
   1407 
   1408 	desc->hdr.type = IWI_HDR_TYPE_DATA;
   1409 	desc->hdr.flags = IWI_HDR_FLAG_IRQ;
   1410 	desc->cmd = IWI_DATA_CMD_TX;
   1411 	desc->len = htole16(m0->m_pkthdr.len);
   1412 	(void)memcpy(&desc->wh, &wh, sizeof (struct ieee80211_frame));
   1413 	desc->flags = 0;
   1414 	if (!IEEE80211_IS_MULTICAST(wh.i_addr1))
   1415 		desc->flags |= IWI_DATA_FLAG_NEED_ACK;
   1416 
   1417 #if 0
   1418 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
   1419 		wh.i_fc[1] |= IEEE80211_FC1_WEP;
   1420 		desc->wep_txkey = ic->ic_crypto.cs_def_txkey;
   1421 	} else
   1422 #endif
   1423 		desc->flags |= IWI_DATA_FLAG_NO_WEP;
   1424 
   1425 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
   1426 		desc->flags |= IWI_DATA_FLAG_SHPREAMBLE;
   1427 
   1428 	desc->nseg = htole32(data->map->dm_nsegs);
   1429 	for (i = 0; i < data->map->dm_nsegs; i++) {
   1430 		desc->seg_addr[i] = htole32(data->map->dm_segs[i].ds_addr);
   1431 		desc->seg_len[i]  = htole32(data->map->dm_segs[i].ds_len);
   1432 	}
   1433 
   1434 	bus_dmamap_sync(sc->sc_dmat, sc->txq.desc_map,
   1435 	    sc->txq.cur * sizeof (struct iwi_tx_desc),
   1436 	    sizeof (struct iwi_tx_desc), BUS_DMASYNC_PREWRITE);
   1437 
   1438 	bus_dmamap_sync(sc->sc_dmat, data->map, 0, MCLBYTES,
   1439 	    BUS_DMASYNC_PREWRITE);
   1440 
   1441 	DPRINTFN(5, ("sending data frame len=%u nseg=%u\n",
   1442 	    desc->len, desc->nseg));
   1443 
   1444 	/* Inform firmware about this new packet */
   1445 	sc->txq.queued++;
   1446 	sc->txq.cur = (sc->txq.cur + 1) % sc->txq.count;
   1447 	CSR_WRITE_4(sc, IWI_CSR_TX1_WIDX, sc->txq.cur);
   1448 
   1449 	return 0;
   1450 }
   1451 
   1452 static void
   1453 iwi_start(struct ifnet *ifp)
   1454 {
   1455 	struct iwi_softc *sc = ifp->if_softc;
   1456 	struct ieee80211com *ic = &sc->sc_ic;
   1457 	struct mbuf *m0;
   1458 	struct ether_header *eh;
   1459 	struct ieee80211_node *ni;
   1460 
   1461 	if (ic->ic_state != IEEE80211_S_RUN)
   1462 		return;
   1463 
   1464 	for (;;) {
   1465 		IF_DEQUEUE(&ifp->if_snd, m0);
   1466 		if (m0 == NULL)
   1467 			break;
   1468 
   1469 		if (sc->txq.queued >= sc->txq.count - 4) {
   1470 			IF_PREPEND(&ifp->if_snd, m0);
   1471 			ifp->if_flags |= IFF_OACTIVE;
   1472 			break;
   1473 		}
   1474 
   1475 		if (m0->m_len < sizeof (struct ether_header) &&
   1476 		    (m0 = m_pullup(m0, sizeof (struct ether_header))) == NULL)
   1477 				continue;
   1478 
   1479 #if NBPFILTER > 0
   1480 		if (ifp->if_bpf != NULL)
   1481 			bpf_mtap(ifp->if_bpf, m0);
   1482 #endif
   1483 
   1484 		eh = mtod(m0, struct ether_header *);
   1485 		ni = ieee80211_find_txnode(ic, eh->ether_dhost);
   1486 		if (ni == NULL) {
   1487 			m_freem(m0);
   1488 			continue;
   1489 		}
   1490 
   1491 #if NBPFILTER > 0
   1492 		if (ic->ic_rawbpf != NULL)
   1493 			bpf_mtap(ic->ic_rawbpf, m0);
   1494 #endif
   1495 		m0 = ieee80211_encap(ic, m0, ni);
   1496 		if (m0 == NULL) {
   1497 			ieee80211_free_node(ni);
   1498 			continue;
   1499 		}
   1500 
   1501 		if (iwi_tx_start(ifp, m0, ni) != 0) {
   1502 			ieee80211_free_node(ni);
   1503 			ifp->if_oerrors++;
   1504 			break;
   1505 		}
   1506 
   1507 		/* start watchdog timer */
   1508 		sc->sc_tx_timer = 5;
   1509 		ifp->if_timer = 1;
   1510 	}
   1511 }
   1512 
   1513 static void
   1514 iwi_watchdog(struct ifnet *ifp)
   1515 {
   1516 	struct iwi_softc *sc = ifp->if_softc;
   1517 
   1518 	ifp->if_timer = 0;
   1519 
   1520 	if (sc->sc_tx_timer > 0) {
   1521 		if (--sc->sc_tx_timer == 0) {
   1522 			aprint_error("%s: device timeout\n",
   1523 			    sc->sc_dev.dv_xname);
   1524 			ifp->if_oerrors++;
   1525 			ifp->if_flags &= ~IFF_UP;
   1526 			iwi_stop(ifp, 1);
   1527 			return;
   1528 		}
   1529 		ifp->if_timer = 1;
   1530 	}
   1531 
   1532 	ieee80211_watchdog(&sc->sc_ic);
   1533 }
   1534 
   1535 static int
   1536 iwi_get_table0(struct iwi_softc *sc, u_int32_t *tbl)
   1537 {
   1538 	u_int32_t size, buf[128];
   1539 
   1540 	if (!(sc->flags & IWI_FLAG_FW_INITED)) {
   1541 		memset(buf, 0, sizeof buf);
   1542 		return copyout(buf, tbl, sizeof buf);
   1543 	}
   1544 
   1545 	size = min(CSR_READ_4(sc, IWI_CSR_TABLE0_SIZE), 128 - 1);
   1546 	CSR_READ_REGION_4(sc, IWI_CSR_TABLE0_BASE, &buf[1], size);
   1547 
   1548 	return copyout(buf, tbl, sizeof buf);
   1549 }
   1550 
   1551 static int
   1552 iwi_get_radio(struct iwi_softc *sc, int *ret)
   1553 {
   1554 	int val;
   1555 
   1556 	val = (CSR_READ_4(sc, IWI_CSR_IO) & IWI_IO_RADIO_ENABLED) ? 1 : 0;
   1557 	return copyout(&val, ret, sizeof val);
   1558 }
   1559 
   1560 static int
   1561 iwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
   1562 {
   1563 	struct iwi_softc *sc = ifp->if_softc;
   1564 	struct ifreq *ifr;
   1565 	int s, error = 0;
   1566 
   1567 	s = splnet();
   1568 
   1569 	switch (cmd) {
   1570 	case SIOCSIFFLAGS:
   1571 		if (ifp->if_flags & IFF_UP) {
   1572 			if (!(ifp->if_flags & IFF_RUNNING))
   1573 				iwi_init(ifp);
   1574 		} else {
   1575 			if (ifp->if_flags & IFF_RUNNING)
   1576 				iwi_stop(ifp, 1);
   1577 		}
   1578 		break;
   1579 
   1580 	case SIOCGTABLE0:
   1581 		ifr = (struct ifreq *)data;
   1582 		error = iwi_get_table0(sc, (u_int32_t *)ifr->ifr_data);
   1583 		break;
   1584 
   1585 	case SIOCGRADIO:
   1586 		ifr = (struct ifreq *)data;
   1587 		error = iwi_get_radio(sc, (int *)ifr->ifr_data);
   1588 		break;
   1589 
   1590 	case SIOCSLOADFW:
   1591 		/* only super-user can do that! */
   1592 		if ((error = suser(curproc->p_ucred, &curproc->p_acflag)) != 0)
   1593 			break;
   1594 
   1595 		ifr = (struct ifreq *)data;
   1596 		error = iwi_cache_firmware(sc, ifr->ifr_data);
   1597 		break;
   1598 
   1599 	case SIOCSKILLFW:
   1600 		/* only super-user can do that! */
   1601 		if ((error = suser(curproc->p_ucred, &curproc->p_acflag)) != 0)
   1602 			break;
   1603 
   1604 		ifp->if_flags &= ~IFF_UP;
   1605 		iwi_stop(ifp, 1);
   1606 		iwi_free_firmware(sc);
   1607 		break;
   1608 
   1609 	default:
   1610 		error = ieee80211_ioctl(&sc->sc_ic, cmd, data);
   1611 	}
   1612 
   1613 	if (error == ENETRESET && cmd != SIOCADDMULTI) {
   1614 		if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
   1615 		    (IFF_UP | IFF_RUNNING))
   1616 			iwi_init(ifp);
   1617 		error = 0;
   1618 	}
   1619 
   1620 	splx(s);
   1621 	return error;
   1622 }
   1623 
   1624 static void
   1625 iwi_stop_master(struct iwi_softc *sc)
   1626 {
   1627 	int ntries;
   1628 
   1629 	/* Disable interrupts */
   1630 	CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
   1631 
   1632 	CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_STOP_MASTER);
   1633 	for (ntries = 0; ntries < 5; ntries++) {
   1634 		if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
   1635 			break;
   1636 		DELAY(10);
   1637 	}
   1638 	if (ntries == 5)
   1639 		aprint_error("%s: timeout waiting for master\n",
   1640 		    sc->sc_dev.dv_xname);
   1641 
   1642 	CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
   1643 	    IWI_RST_PRINCETON_RESET);
   1644 
   1645 	sc->flags &= ~IWI_FLAG_FW_INITED;
   1646 }
   1647 
   1648 static int
   1649 iwi_reset(struct iwi_softc *sc)
   1650 {
   1651 	int i, ntries;
   1652 
   1653 	iwi_stop_master(sc);
   1654 
   1655 	/* Move adapter to D0 state */
   1656 	CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
   1657 	    IWI_CTL_INIT);
   1658 
   1659 	/* Initialize Phase-Locked Level  (PLL) */
   1660 	CSR_WRITE_4(sc, IWI_CSR_READ_INT, IWI_READ_INT_INIT_HOST);
   1661 
   1662 	/* Wait for clock stabilization */
   1663 	for (ntries = 0; ntries < 1000; ntries++) {
   1664 		if (CSR_READ_4(sc, IWI_CSR_CTL) & IWI_CTL_CLOCK_READY)
   1665 			break;
   1666 		DELAY(200);
   1667 	}
   1668 	if (ntries == 1000)
   1669 		return EIO;
   1670 
   1671 	CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
   1672 	    IWI_RST_SW_RESET);
   1673 
   1674 	DELAY(10);
   1675 
   1676 	CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
   1677 	    IWI_CTL_INIT);
   1678 
   1679 	/* Clear NIC memory */
   1680 	CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0);
   1681 	for (i = 0; i < 0xc000; i++)
   1682 		CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
   1683 
   1684 	return 0;
   1685 }
   1686 
   1687 static int
   1688 iwi_load_ucode(struct iwi_softc *sc, void *uc, int size)
   1689 {
   1690 	u_int16_t *w;
   1691 	int ntries, i;
   1692 
   1693 	CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
   1694 	    IWI_RST_STOP_MASTER);
   1695 	for (ntries = 0; ntries < 5; ntries++) {
   1696 		if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
   1697 			break;
   1698 		DELAY(10);
   1699 	}
   1700 	if (ntries == 5) {
   1701 		aprint_error("%s: timeout waiting for master\n",
   1702 		    sc->sc_dev.dv_xname);
   1703 		return EIO;
   1704 	}
   1705 
   1706 	MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
   1707 	DELAY(5000);
   1708 	CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) &
   1709 	    ~IWI_RST_PRINCETON_RESET);
   1710 	DELAY(5000);
   1711 	MEM_WRITE_4(sc, 0x3000e0, 0);
   1712 	DELAY(1000);
   1713 	MEM_WRITE_4(sc, 0x300004, 1);
   1714 	DELAY(1000);
   1715 	MEM_WRITE_4(sc, 0x300004, 0);
   1716 	DELAY(1000);
   1717 	MEM_WRITE_1(sc, 0x200000, 0x00);
   1718 	MEM_WRITE_1(sc, 0x200000, 0x40);
   1719 	DELAY(1000);
   1720 
   1721 	/* Adapter is buggy, we must set the address for each word */
   1722 	for (w = uc; size > 0; w++, size -= 2)
   1723 		MEM_WRITE_2(sc, 0x200010, *w);
   1724 
   1725 	MEM_WRITE_1(sc, 0x200000, 0x00);
   1726 	MEM_WRITE_1(sc, 0x200000, 0x80);
   1727 
   1728 	/* Wait until we get a response in the uc queue */
   1729 	for (ntries = 0; ntries < 100; ntries++) {
   1730 		if (MEM_READ_1(sc, 0x200000) & 1)
   1731 			break;
   1732 		DELAY(100);
   1733 	}
   1734 	if (ntries == 100) {
   1735 		aprint_error("%s: timeout waiting for ucode to initialize\n",
   1736 		    sc->sc_dev.dv_xname);
   1737 		return EIO;
   1738 	}
   1739 
   1740 	/* Empty the uc queue or the firmware will not initialize properly */
   1741 	for (i = 0; i < 7; i++)
   1742 		MEM_READ_4(sc, 0x200004);
   1743 
   1744 	MEM_WRITE_1(sc, 0x200000, 0x00);
   1745 
   1746 	return 0;
   1747 }
   1748 
   1749 /* macro to handle unaligned little endian data in firmware image */
   1750 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
   1751 static int
   1752 iwi_load_firmware(struct iwi_softc *sc, void *fw, int size)
   1753 {
   1754 	bus_dmamap_t map;
   1755 	bus_dma_segment_t seg;
   1756 	caddr_t virtaddr;
   1757 	u_char *p, *end;
   1758 	u_int32_t sentinel, ctl, src, dst, sum, len, mlen;
   1759 	int ntries, nsegs, error;
   1760 
   1761 	/* Allocate DMA memory for storing firmware image */
   1762 	error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
   1763 	    BUS_DMA_NOWAIT, &map);
   1764 	if (error != 0) {
   1765 		aprint_error("%s: could not create firmware DMA map\n",
   1766 		    sc->sc_dev.dv_xname);
   1767 		goto fail1;
   1768 	}
   1769 
   1770 	/*
   1771 	 * We cannot map fw directly because of some hardware constraints on
   1772 	 * the mapping address.
   1773 	 */
   1774 	error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &seg, 1,
   1775 	    &nsegs, BUS_DMA_NOWAIT);
   1776 	if (error != 0) {
   1777 		aprint_error("%s: could not allocate firmware DMA memory\n",
   1778 		    sc->sc_dev.dv_xname);
   1779 		goto fail2;
   1780 	}
   1781 
   1782 	error = bus_dmamem_map(sc->sc_dmat, &seg, nsegs, size, &virtaddr,
   1783 	    BUS_DMA_NOWAIT);
   1784 	if (error != 0) {
   1785 		aprint_error("%s: could not load firmware DMA map\n",
   1786 		    sc->sc_dev.dv_xname);
   1787 		goto fail3;
   1788 	}
   1789 
   1790 	error = bus_dmamap_load(sc->sc_dmat, map, virtaddr, size, NULL,
   1791 	    BUS_DMA_NOWAIT);
   1792 	if (error != 0) {
   1793 		aprint_error("%s: could not load fw dma map\n",
   1794 		    sc->sc_dev.dv_xname);
   1795 		goto fail4;
   1796 	}
   1797 
   1798 	/* Copy firmware image to DMA memory */
   1799 	memcpy(virtaddr, fw, size);
   1800 
   1801 	/* Make sure the adapter will get up-to-date values */
   1802 	bus_dmamap_sync(sc->sc_dmat, map, 0, size, BUS_DMASYNC_PREWRITE);
   1803 
   1804 	/* Tell the adapter where the command blocks are stored */
   1805 	MEM_WRITE_4(sc, 0x3000a0, 0x27000);
   1806 
   1807 	/*
   1808 	 * Store command blocks into adapter's internal memory using register
   1809 	 * indirections. The adapter will read the firmware image through DMA
   1810 	 * using information stored in command blocks.
   1811 	 */
   1812 	src = map->dm_segs[0].ds_addr;
   1813 	p = virtaddr;
   1814 	end = p + size;
   1815 	CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0x27000);
   1816 
   1817 	while (p < end) {
   1818 		dst = GETLE32(p); p += 4; src += 4;
   1819 		len = GETLE32(p); p += 4; src += 4;
   1820 		p += len;
   1821 
   1822 		while (len > 0) {
   1823 			mlen = min(len, IWI_CB_MAXDATALEN);
   1824 
   1825 			ctl = IWI_CB_DEFAULT_CTL | mlen;
   1826 			sum = ctl ^ src ^ dst;
   1827 
   1828 			/* Write a command block */
   1829 			CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, ctl);
   1830 			CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, src);
   1831 			CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, dst);
   1832 			CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, sum);
   1833 
   1834 			src += mlen;
   1835 			dst += mlen;
   1836 			len -= mlen;
   1837 		}
   1838 	}
   1839 
   1840 	/* Write a fictive final command block (sentinel) */
   1841 	sentinel = CSR_READ_4(sc, IWI_CSR_AUTOINC_ADDR);
   1842 	CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
   1843 
   1844 	CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) &
   1845 	    ~(IWI_RST_MASTER_DISABLED | IWI_RST_STOP_MASTER));
   1846 
   1847 	/* Tell the adapter to start processing command blocks */
   1848 	MEM_WRITE_4(sc, 0x3000a4, 0x540100);
   1849 
   1850 	/* Wait until the adapter has processed all command blocks */
   1851 	for (ntries = 0; ntries < 400; ntries++) {
   1852 		if (MEM_READ_4(sc, 0x3000d0) >= sentinel)
   1853 			break;
   1854 		DELAY(100);
   1855 	}
   1856 	if (ntries == 400) {
   1857 		aprint_error("%s: timeout processing cb\n",
   1858 		    sc->sc_dev.dv_xname);
   1859 		error = EIO;
   1860 		goto fail5;
   1861 	}
   1862 
   1863 	/* We're done with command blocks processing */
   1864 	MEM_WRITE_4(sc, 0x3000a4, 0x540c00);
   1865 
   1866 	/* Allow interrupts so we know when the firmware is inited */
   1867 	CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
   1868 
   1869 	/* Tell the adapter to initialize the firmware */
   1870 	CSR_WRITE_4(sc, IWI_CSR_RST, 0);
   1871 	CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
   1872 	    IWI_CTL_ALLOW_STANDBY);
   1873 
   1874 	/* Wait at most one second for firmware initialization to complete */
   1875 	if ((error = tsleep(sc, 0, "iwiinit", hz)) != 0) {
   1876 		aprint_error("%s: timeout waiting for firmware initialization "
   1877 		    "to complete\n", sc->sc_dev.dv_xname);
   1878 		goto fail5;
   1879 	}
   1880 
   1881 fail5:	bus_dmamap_sync(sc->sc_dmat, map, 0, size, BUS_DMASYNC_POSTWRITE);
   1882 	bus_dmamap_unload(sc->sc_dmat, map);
   1883 fail4:	bus_dmamem_unmap(sc->sc_dmat, virtaddr, size);
   1884 fail3:	bus_dmamem_free(sc->sc_dmat, &seg, 1);
   1885 fail2:	bus_dmamap_destroy(sc->sc_dmat, map);
   1886 
   1887 fail1:	return error;
   1888 }
   1889 
   1890 /*
   1891  * Store firmware into kernel memory so we can download it when we need to,
   1892  * e.g when the adapter wakes up from suspend mode.
   1893  */
   1894 static int
   1895 iwi_cache_firmware(struct iwi_softc *sc, void *data)
   1896 {
   1897 	struct iwi_firmware *kfw = &sc->fw;
   1898 	struct iwi_firmware ufw;
   1899 	int error;
   1900 
   1901 	iwi_free_firmware(sc);
   1902 
   1903 	if ((error = copyin(data, &ufw, sizeof ufw)) != 0)
   1904 		goto fail1;
   1905 
   1906 	kfw->boot_size  = ufw.boot_size;
   1907 	kfw->ucode_size = ufw.ucode_size;
   1908 	kfw->main_size  = ufw.main_size;
   1909 
   1910 	kfw->boot = malloc(kfw->boot_size, M_DEVBUF, M_NOWAIT);
   1911 	if (kfw->boot == NULL) {
   1912 		error = ENOMEM;
   1913 		goto fail1;
   1914 	}
   1915 
   1916 	kfw->ucode = malloc(kfw->ucode_size, M_DEVBUF, M_NOWAIT);
   1917 	if (kfw->ucode == NULL) {
   1918 		error = ENOMEM;
   1919 		goto fail2;
   1920 	}
   1921 
   1922 	kfw->main = malloc(kfw->main_size, M_DEVBUF, M_NOWAIT);
   1923 	if (kfw->main == NULL) {
   1924 		error = ENOMEM;
   1925 		goto fail3;
   1926 	}
   1927 
   1928 	if ((error = copyin(ufw.boot, kfw->boot, kfw->boot_size)) != 0)
   1929 		goto fail4;
   1930 
   1931 	if ((error = copyin(ufw.ucode, kfw->ucode, kfw->ucode_size)) != 0)
   1932 		goto fail4;
   1933 
   1934 	if ((error = copyin(ufw.main, kfw->main, kfw->main_size)) != 0)
   1935 		goto fail4;
   1936 
   1937 	DPRINTF(("Firmware cached: boot %u, ucode %u, main %u\n",
   1938 	    kfw->boot_size, kfw->ucode_size, kfw->main_size));
   1939 
   1940 	sc->flags |= IWI_FLAG_FW_CACHED;
   1941 
   1942 	return 0;
   1943 
   1944 fail4:	free(kfw->boot, M_DEVBUF);
   1945 fail3:	free(kfw->ucode, M_DEVBUF);
   1946 fail2:	free(kfw->main, M_DEVBUF);
   1947 fail1:
   1948 	return error;
   1949 }
   1950 
   1951 static void
   1952 iwi_free_firmware(struct iwi_softc *sc)
   1953 {
   1954 	if (!(sc->flags & IWI_FLAG_FW_CACHED))
   1955 		return;
   1956 
   1957 	free(sc->fw.boot, M_DEVBUF);
   1958 	free(sc->fw.ucode, M_DEVBUF);
   1959 	free(sc->fw.main, M_DEVBUF);
   1960 
   1961 	sc->flags &= ~IWI_FLAG_FW_CACHED;
   1962 }
   1963 
   1964 static int
   1965 iwi_config(struct iwi_softc *sc)
   1966 {
   1967 	struct ieee80211com *ic = &sc->sc_ic;
   1968 	struct ifnet *ifp = &sc->sc_if;
   1969 	struct iwi_configuration config;
   1970 	struct iwi_rateset rs;
   1971 	struct iwi_txpower power;
   1972 	struct ieee80211_key *wk;
   1973 	struct iwi_wep_key wepkey;
   1974 	u_int32_t data;
   1975 	int error, i;
   1976 
   1977 	IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
   1978 	DPRINTF(("Setting MAC address to %s\n", ether_sprintf(ic->ic_myaddr)));
   1979 	error = iwi_cmd(sc, IWI_CMD_SET_MAC_ADDRESS, ic->ic_myaddr,
   1980 	    IEEE80211_ADDR_LEN, 0);
   1981 	if (error != 0)
   1982 		return error;
   1983 
   1984 	memset(&config, 0, sizeof config);
   1985 	config.bluetooth_coexistence = sc->bluetooth;
   1986 	config.antenna = sc->antenna;
   1987 	config.multicast_enabled = 1;
   1988 	config.answer_pbreq = (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0;
   1989 	config.disable_unicast_decryption = 1;
   1990 	config.disable_multicast_decryption = 1;
   1991 	DPRINTF(("Configuring adapter\n"));
   1992 	error = iwi_cmd(sc, IWI_CMD_SET_CONFIGURATION, &config, sizeof config,
   1993 	    0);
   1994 	if (error != 0)
   1995 		return error;
   1996 
   1997 	data = htole32(IWI_POWER_MODE_CAM);
   1998 	DPRINTF(("Setting power mode to %u\n", le32toh(data)));
   1999 	error = iwi_cmd(sc, IWI_CMD_SET_POWER_MODE, &data, sizeof data, 0);
   2000 	if (error != 0)
   2001 		return error;
   2002 
   2003 	data = htole32(ic->ic_rtsthreshold);
   2004 	DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
   2005 	error = iwi_cmd(sc, IWI_CMD_SET_RTS_THRESHOLD, &data, sizeof data, 0);
   2006 	if (error != 0)
   2007 		return error;
   2008 
   2009 	data = htole32(ic->ic_fragthreshold);
   2010 	DPRINTF(("Setting fragmentation threshold to %u\n", le32toh(data)));
   2011 	error = iwi_cmd(sc, IWI_CMD_SET_FRAG_THRESHOLD, &data, sizeof data, 0);
   2012 	if (error != 0)
   2013 		return error;
   2014 
   2015 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
   2016 		power.mode = IWI_MODE_11B;
   2017 		power.nchan = 11;
   2018 		for (i = 0; i < 11; i++) {
   2019 			power.chan[i].chan = i + 1;
   2020 			power.chan[i].power = IWI_TXPOWER_MAX;
   2021 		}
   2022 		DPRINTF(("Setting .11b channels tx power\n"));
   2023 		error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power,
   2024 		    0);
   2025 		if (error != 0)
   2026 			return error;
   2027 
   2028 		power.mode = IWI_MODE_11G;
   2029 		DPRINTF(("Setting .11g channels tx power\n"));
   2030 		error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power,
   2031 		    0);
   2032 		if (error != 0)
   2033 			return error;
   2034 	}
   2035 
   2036 	rs.mode = IWI_MODE_11G;
   2037 	rs.type = IWI_RATESET_TYPE_SUPPORTED;
   2038 	rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates;
   2039 	memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11G].rs_rates,
   2040 	    rs.nrates);
   2041 	DPRINTF(("Setting .11bg supported rates (%u)\n", rs.nrates));
   2042 	error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
   2043 	if (error != 0)
   2044 		return error;
   2045 
   2046 	rs.mode = IWI_MODE_11A;
   2047 	rs.type = IWI_RATESET_TYPE_SUPPORTED;
   2048 	rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates;
   2049 	memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11A].rs_rates,
   2050 	    rs.nrates);
   2051 	DPRINTF(("Setting .11a supported rates (%u)\n", rs.nrates));
   2052 	error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
   2053 	if (error != 0)
   2054 		return error;
   2055 
   2056 	data = htole32(arc4random());
   2057 	DPRINTF(("Setting initialization vector to %u\n", le32toh(data)));
   2058 	error = iwi_cmd(sc, IWI_CMD_SET_IV, &data, sizeof data, 0);
   2059 	if (error != 0)
   2060 		return error;
   2061 
   2062 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   2063 		wk = &ic->ic_crypto.cs_nw_keys[i];
   2064 
   2065 		wepkey.cmd = IWI_WEP_KEY_CMD_SETKEY;
   2066 		wepkey.idx = i;
   2067 		wepkey.len = wk->wk_keylen;
   2068 		memset(wepkey.key, 0, sizeof wepkey.key);
   2069 		memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
   2070 		DPRINTF(("Setting wep key index %u len %u\n",
   2071 		    wepkey.idx, wepkey.len));
   2072 		error = iwi_cmd(sc, IWI_CMD_SET_WEP_KEY, &wepkey,
   2073 		    sizeof wepkey, 0);
   2074 		if (error != 0)
   2075 			return error;
   2076 	}
   2077 
   2078 	/* Enable adapter */
   2079 	DPRINTF(("Enabling adapter\n"));
   2080 	return iwi_cmd(sc, IWI_CMD_ENABLE, NULL, 0, 0);
   2081 }
   2082 
   2083 static int
   2084 iwi_set_chan(struct iwi_softc *sc, struct ieee80211_channel *chan)
   2085 {
   2086 	struct ieee80211com *ic = &sc->sc_ic;
   2087 	struct iwi_scan scan;
   2088 
   2089 	(void)memset(&scan, 0, sizeof scan);
   2090 	scan.type = IWI_SCAN_TYPE_PASSIVE;
   2091 	scan.dwelltime = htole16(2000);
   2092 	scan.channels[0] = 1 | (IEEE80211_IS_CHAN_5GHZ(chan) ? IWI_CHAN_5GHZ :
   2093 	    IWI_CHAN_2GHZ);
   2094 	scan.channels[1] = ieee80211_chan2ieee(ic, chan);
   2095 
   2096 	DPRINTF(("Setting channel to %u\n", ieee80211_chan2ieee(ic, chan)));
   2097 	return iwi_cmd(sc, IWI_CMD_SCAN, &scan, sizeof scan, 1);
   2098 }
   2099 
   2100 static int
   2101 iwi_scan(struct iwi_softc *sc)
   2102 {
   2103 	struct ieee80211com *ic = &sc->sc_ic;
   2104 	struct iwi_scan scan;
   2105 	u_int8_t *p;
   2106 	int i, count;
   2107 
   2108 	(void)memset(&scan, 0, sizeof scan);
   2109 	scan.type = IWI_SCAN_TYPE_BROADCAST;
   2110 	scan.dwelltime = htole16(sc->dwelltime);
   2111 
   2112 	p = scan.channels;
   2113 	count = 0;
   2114 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
   2115 		if (IEEE80211_IS_CHAN_5GHZ(&ic->ic_channels[i]) &&
   2116 		    isset(ic->ic_chan_active, i)) {
   2117 			*++p = i;
   2118 			count++;
   2119 		}
   2120 	}
   2121 	*(p - count) = IWI_CHAN_5GHZ | count;
   2122 
   2123 	count = 0;
   2124 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
   2125 		if (IEEE80211_IS_CHAN_2GHZ(&ic->ic_channels[i]) &&
   2126 		    isset(ic->ic_chan_active, i)) {
   2127 			*++p = i;
   2128 			count++;
   2129 		}
   2130 	}
   2131 	*(p - count) = IWI_CHAN_2GHZ | count;
   2132 
   2133 	DPRINTF(("Start scanning\n"));
   2134 	return iwi_cmd(sc, IWI_CMD_SCAN, &scan, sizeof scan, 1);
   2135 }
   2136 
   2137 static int
   2138 iwi_auth_and_assoc(struct iwi_softc *sc)
   2139 {
   2140 	struct ieee80211com *ic = &sc->sc_ic;
   2141 	struct ieee80211_node *ni = ic->ic_bss;
   2142 	struct ifnet *ifp = &sc->sc_if;
   2143 	struct iwi_configuration config;
   2144 	struct iwi_associate assoc;
   2145 	struct iwi_rateset rs;
   2146 	u_int16_t capinfo;
   2147 	u_int32_t data;
   2148 	int error;
   2149 
   2150 	if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
   2151 		memset(&config, 0, sizeof config);
   2152 		config.bluetooth_coexistence = sc->bluetooth;
   2153 		config.antenna = sc->antenna;
   2154 		config.multicast_enabled = 1;
   2155 		config.use_protection = 1;
   2156 		config.answer_pbreq =
   2157 		    (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0;
   2158 		config.disable_unicast_decryption = 1;
   2159 		config.disable_multicast_decryption = 1;
   2160 		DPRINTF(("Configuring adapter\n"));
   2161 		error = iwi_cmd(sc, IWI_CMD_SET_CONFIGURATION, &config,
   2162 		    sizeof config, 1);
   2163 		if (error != 0)
   2164 			return error;
   2165 	}
   2166 
   2167 #ifdef IWI_DEBUG
   2168 	if (iwi_debug > 0) {
   2169 		printf("Setting ESSID to ");
   2170 		ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
   2171 		printf("\n");
   2172 	}
   2173 #endif
   2174 	error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ni->ni_essid, ni->ni_esslen, 1);
   2175 	if (error != 0)
   2176 		return error;
   2177 
   2178 	/* the rate set has already been "negociated" */
   2179 	rs.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A :
   2180 	    IWI_MODE_11G;
   2181 	rs.type = IWI_RATESET_TYPE_NEGOCIATED;
   2182 	rs.nrates = ni->ni_rates.rs_nrates;
   2183 	memcpy(rs.rates, ni->ni_rates.rs_rates, rs.nrates);
   2184 	DPRINTF(("Setting negociated rates (%u)\n", rs.nrates));
   2185 	error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 1);
   2186 	if (error != 0)
   2187 		return error;
   2188 
   2189 	if (ic->ic_opt_ie != NULL) {
   2190 		DPRINTF(("Setting optional IE (len=%u)\n", ic->ic_opt_ie_len));
   2191 		error = iwi_cmd(sc, IWI_CMD_SET_OPTIE, ic->ic_opt_ie,
   2192 		    ic->ic_opt_ie_len, 1);
   2193 		if (error != 0)
   2194 			return error;
   2195 	}
   2196 	data = htole32(ni->ni_rssi);
   2197 	DPRINTF(("Setting sensitivity to %d\n", (int8_t)ni->ni_rssi));
   2198 	error = iwi_cmd(sc, IWI_CMD_SET_SENSITIVITY, &data, sizeof data, 1);
   2199 	if (error != 0)
   2200 		return error;
   2201 
   2202 	memset(&assoc, 0, sizeof assoc);
   2203 	assoc.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A :
   2204 	    IWI_MODE_11G;
   2205 	assoc.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
   2206 	if (ni->ni_authmode == IEEE80211_AUTH_SHARED)
   2207 		assoc.auth = (ic->ic_crypto.cs_def_txkey << 4) | IWI_AUTH_SHARED;
   2208 	if (ic->ic_opt_ie != NULL)
   2209 		assoc.policy |= htole16(IWI_POLICY_OPTIE);
   2210 	memcpy(assoc.tstamp, ni->ni_tstamp.data, 8);
   2211 
   2212 	if (ic->ic_opmode == IEEE80211_M_IBSS)
   2213 		capinfo = IEEE80211_CAPINFO_IBSS;
   2214 	else
   2215 		capinfo = IEEE80211_CAPINFO_ESS;
   2216 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
   2217 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
   2218 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
   2219 	    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
   2220 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
   2221 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
   2222 		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
   2223 	assoc.capinfo = htole16(capinfo);
   2224 
   2225 	assoc.lintval = htole16(ic->ic_lintval);
   2226 	assoc.intval = htole16(ni->ni_intval);
   2227 	IEEE80211_ADDR_COPY(assoc.bssid, ni->ni_bssid);
   2228 	if (ic->ic_opmode == IEEE80211_M_IBSS)
   2229 		IEEE80211_ADDR_COPY(assoc.dst, ifp->if_broadcastaddr);
   2230 	else
   2231 		IEEE80211_ADDR_COPY(assoc.dst, ni->ni_bssid);
   2232 	DPRINTF(("Trying to associate to %s channel %u auth %u\n",
   2233 	    ether_sprintf(assoc.bssid), assoc.chan, assoc.auth));
   2234 	return iwi_cmd(sc, IWI_CMD_ASSOCIATE, &assoc, sizeof assoc, 1);
   2235 }
   2236 
   2237 static int
   2238 iwi_init(struct ifnet *ifp)
   2239 {
   2240 	struct iwi_softc *sc = ifp->if_softc;
   2241 	struct ieee80211com *ic = &sc->sc_ic;
   2242 	struct iwi_firmware *fw = &sc->fw;
   2243 	int i, error;
   2244 
   2245 	/* exit immediately if firmware has not been ioctl'd */
   2246 	if (!(sc->flags & IWI_FLAG_FW_CACHED)) {
   2247 		if (!(sc->flags & IWI_FLAG_FW_WARNED))
   2248 			aprint_error("%s: Firmware not loaded\n",
   2249 			    sc->sc_dev.dv_xname);
   2250 		sc->flags |= IWI_FLAG_FW_WARNED;
   2251 		ifp->if_flags &= ~IFF_UP;
   2252 		return EIO;
   2253 	}
   2254 
   2255 	iwi_stop(ifp, 0);
   2256 
   2257 	if ((error = iwi_reset(sc)) != 0) {
   2258 		aprint_error("%s: could not reset adapter\n",
   2259 		    sc->sc_dev.dv_xname);
   2260 		goto fail;
   2261 	}
   2262 
   2263 	if ((error = iwi_load_firmware(sc, fw->boot, fw->boot_size)) != 0) {
   2264 		aprint_error("%s: could not load boot firmware\n",
   2265 		    sc->sc_dev.dv_xname);
   2266 		goto fail;
   2267 	}
   2268 
   2269 	if ((error = iwi_load_ucode(sc, fw->ucode, fw->ucode_size)) != 0) {
   2270 		aprint_error("%s: could not load microcode\n",
   2271 		    sc->sc_dev.dv_xname);
   2272 		goto fail;
   2273 	}
   2274 
   2275 	iwi_stop_master(sc);
   2276 
   2277 	CSR_WRITE_4(sc, IWI_CSR_CMD_BASE, sc->cmdq.desc_map->dm_segs[0].ds_addr);
   2278 	CSR_WRITE_4(sc, IWI_CSR_CMD_SIZE, sc->cmdq.count);
   2279 	CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
   2280 
   2281 	CSR_WRITE_4(sc, IWI_CSR_TX1_BASE, sc->txq.desc_map->dm_segs[0].ds_addr);
   2282 	CSR_WRITE_4(sc, IWI_CSR_TX1_SIZE, sc->txq.count);
   2283 	CSR_WRITE_4(sc, IWI_CSR_TX1_WIDX, sc->txq.cur);
   2284 
   2285 	CSR_WRITE_4(sc, IWI_CSR_TX2_BASE, sc->txq.desc_map->dm_segs[0].ds_addr);
   2286 	CSR_WRITE_4(sc, IWI_CSR_TX2_SIZE, sc->txq.count);
   2287 	CSR_WRITE_4(sc, IWI_CSR_TX2_WIDX, sc->txq.cur);
   2288 
   2289 	CSR_WRITE_4(sc, IWI_CSR_TX3_BASE, sc->txq.desc_map->dm_segs[0].ds_addr);
   2290 	CSR_WRITE_4(sc, IWI_CSR_TX3_SIZE, sc->txq.count);
   2291 	CSR_WRITE_4(sc, IWI_CSR_TX3_WIDX, sc->txq.cur);
   2292 
   2293 	CSR_WRITE_4(sc, IWI_CSR_TX4_BASE, sc->txq.desc_map->dm_segs[0].ds_addr);
   2294 	CSR_WRITE_4(sc, IWI_CSR_TX4_SIZE, sc->txq.count);
   2295 	CSR_WRITE_4(sc, IWI_CSR_TX4_WIDX, sc->txq.cur);
   2296 
   2297 	for (i = 0; i < sc->rxq.count; i++)
   2298 		CSR_WRITE_4(sc, IWI_CSR_RX_BASE + i * 4,
   2299 		    sc->rxq.data[i].map->dm_segs[0].ds_addr);
   2300 
   2301 	CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, sc->rxq.count -1);
   2302 
   2303 	if ((error = iwi_load_firmware(sc, fw->main, fw->main_size)) != 0) {
   2304 		aprint_error("%s: could not load main firmware\n",
   2305 		    sc->sc_dev.dv_xname);
   2306 		goto fail;
   2307 	}
   2308 
   2309 	sc->flags |= IWI_FLAG_FW_INITED;
   2310 
   2311 	if ((error = iwi_config(sc)) != 0) {
   2312 		aprint_error("%s: device configuration failed\n",
   2313 		    sc->sc_dev.dv_xname);
   2314 		goto fail;
   2315 	}
   2316 
   2317 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
   2318 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
   2319 	else
   2320 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
   2321 
   2322 	ifp->if_flags &= ~IFF_OACTIVE;
   2323 	ifp->if_flags |= IFF_RUNNING;
   2324 
   2325 	return 0;
   2326 
   2327 fail:	ifp->if_flags &= ~IFF_UP;
   2328 	iwi_stop(ifp, 0);
   2329 
   2330 	return error;
   2331 }
   2332 
   2333 static void
   2334 iwi_stop(struct ifnet *ifp, int disable)
   2335 {
   2336 	struct iwi_softc *sc = ifp->if_softc;
   2337 	struct ieee80211com *ic = &sc->sc_ic;
   2338 
   2339 	iwi_stop_master(sc);
   2340 	CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_SW_RESET);
   2341 
   2342 	/* reset rings */
   2343 	iwi_reset_cmd_ring(sc, &sc->cmdq);
   2344 	iwi_reset_tx_ring(sc, &sc->txq);
   2345 	iwi_reset_rx_ring(sc, &sc->rxq);
   2346 
   2347 	ifp->if_timer = 0;
   2348 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   2349 
   2350 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
   2351 }
   2352