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