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