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