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