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