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