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