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