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