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