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