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