Home | History | Annotate | Line # | Download | only in pci
if_ipw.c revision 1.20
      1 /*	$NetBSD: if_ipw.c,v 1.20 2006/04/18 12:49:02 rpaulo Exp $	*/
      2 /*	FreeBSD: src/sys/dev/ipw/if_ipw.c,v 1.15 2005/11/13 17:17:40 damien Exp 	*/
      3 
      4 /*-
      5  * Copyright (c) 2004, 2005
      6  *      Damien Bergamini <damien.bergamini (at) free.fr>. All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice unmodified, this list of conditions, and the following
     13  *    disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: if_ipw.c,v 1.20 2006/04/18 12:49:02 rpaulo Exp $");
     33 
     34 /*-
     35  * Intel(R) PRO/Wireless 2100 MiniPCI driver
     36  * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
     37  */
     38 
     39 #include "bpfilter.h"
     40 
     41 #include <sys/param.h>
     42 #include <sys/sockio.h>
     43 #include <sys/sysctl.h>
     44 #include <sys/mbuf.h>
     45 #include <sys/kernel.h>
     46 #include <sys/socket.h>
     47 #include <sys/systm.h>
     48 #include <sys/malloc.h>
     49 #include <sys/conf.h>
     50 
     51 #include <machine/bus.h>
     52 #include <machine/endian.h>
     53 #include <machine/intr.h>
     54 
     55 #include <dev/pci/pcireg.h>
     56 #include <dev/pci/pcivar.h>
     57 #include <dev/pci/pcidevs.h>
     58 
     59 #if NBPFILTER > 0
     60 #include <net/bpf.h>
     61 #endif
     62 #include <net/if.h>
     63 #include <net/if_arp.h>
     64 #include <net/if_dl.h>
     65 #include <net/if_ether.h>
     66 #include <net/if_media.h>
     67 #include <net/if_types.h>
     68 
     69 #include <net80211/ieee80211_var.h>
     70 #include <net80211/ieee80211_radiotap.h>
     71 
     72 #include <netinet/in.h>
     73 #include <netinet/in_systm.h>
     74 #include <netinet/in_var.h>
     75 #include <netinet/ip.h>
     76 
     77 #include <dev/firmload.h>
     78 
     79 #include <dev/pci/if_ipwreg.h>
     80 #include <dev/pci/if_ipwvar.h>
     81 
     82 #ifdef IPW_DEBUG
     83 #define DPRINTF(x)	if (ipw_debug > 0) printf x
     84 #define DPRINTFN(n, x)	if (ipw_debug >= (n)) printf x
     85 int ipw_debug = 0;
     86 #else
     87 #define DPRINTF(x)
     88 #define DPRINTFN(n, x)
     89 #endif
     90 
     91 static int	ipw_dma_alloc(struct ipw_softc *);
     92 static void	ipw_release(struct ipw_softc *);
     93 static int	ipw_match(struct device *, struct cfdata *, void *);
     94 static void	ipw_attach(struct device *, struct device *, void *);
     95 static int	ipw_detach(struct device *, int);
     96 
     97 static void	ipw_shutdown(void *);
     98 static int	ipw_suspend(struct ipw_softc *);
     99 static int	ipw_resume(struct ipw_softc *);
    100 static void	ipw_powerhook(int, void *);
    101 
    102 static int	ipw_media_change(struct ifnet *);
    103 static void	ipw_media_status(struct ifnet *, struct ifmediareq *);
    104 static int	ipw_newstate(struct ieee80211com *, enum ieee80211_state, int);
    105 static uint16_t	ipw_read_prom_word(struct ipw_softc *, uint8_t);
    106 static void	ipw_command_intr(struct ipw_softc *, struct ipw_soft_buf *);
    107 static void	ipw_newstate_intr(struct ipw_softc *, struct ipw_soft_buf *);
    108 static void	ipw_data_intr(struct ipw_softc *, struct ipw_status *,
    109     struct ipw_soft_bd *, struct ipw_soft_buf *);
    110 static void	ipw_rx_intr(struct ipw_softc *);
    111 static void	ipw_release_sbd(struct ipw_softc *, struct ipw_soft_bd *);
    112 static void	ipw_tx_intr(struct ipw_softc *);
    113 static int	ipw_intr(void *);
    114 static int	ipw_cmd(struct ipw_softc *, uint32_t, void *, uint32_t);
    115 static int	ipw_tx_start(struct ifnet *, struct mbuf *,
    116     struct ieee80211_node *);
    117 static void	ipw_start(struct ifnet *);
    118 static void	ipw_watchdog(struct ifnet *);
    119 static int	ipw_ioctl(struct ifnet *, u_long, caddr_t);
    120 static int	ipw_get_table1(struct ipw_softc *, uint32_t *);
    121 static int	ipw_get_radio(struct ipw_softc *, int *);
    122 static void	ipw_stop_master(struct ipw_softc *);
    123 static int	ipw_reset(struct ipw_softc *);
    124 static int	ipw_load_ucode(struct ipw_softc *, u_char *, int);
    125 static int	ipw_load_firmware(struct ipw_softc *, u_char *, int);
    126 static int	ipw_cache_firmware(struct ipw_softc *);
    127 static void	ipw_free_firmware(struct ipw_softc *);
    128 static int	ipw_config(struct ipw_softc *);
    129 static int	ipw_init(struct ifnet *);
    130 static void	ipw_stop(struct ifnet *, int);
    131 static uint32_t	ipw_read_table1(struct ipw_softc *, uint32_t);
    132 static void	ipw_write_table1(struct ipw_softc *, uint32_t, uint32_t);
    133 static int	ipw_read_table2(struct ipw_softc *, uint32_t, void *, uint32_t *);
    134 static void	ipw_read_mem_1(struct ipw_softc *, bus_size_t, uint8_t *,
    135     bus_size_t);
    136 static void	ipw_write_mem_1(struct ipw_softc *, bus_size_t, uint8_t *,
    137     bus_size_t);
    138 
    139 /*
    140  * Supported rates for 802.11b mode (in 500Kbps unit).
    141  */
    142 static const struct ieee80211_rateset ipw_rateset_11b =
    143 	{ 4, { 2, 4, 11, 22 } };
    144 
    145 static inline uint8_t
    146 MEM_READ_1(struct ipw_softc *sc, uint32_t addr)
    147 {
    148 	CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, addr);
    149 	return CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA);
    150 }
    151 
    152 static inline uint32_t
    153 MEM_READ_4(struct ipw_softc *sc, uint32_t addr)
    154 {
    155 	CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, addr);
    156 	return CSR_READ_4(sc, IPW_CSR_INDIRECT_DATA);
    157 }
    158 
    159 CFATTACH_DECL(ipw, sizeof (struct ipw_softc), ipw_match, ipw_attach,
    160     ipw_detach, NULL);
    161 
    162 static int
    163 ipw_match(struct device *parent, struct cfdata *match, void *aux)
    164 {
    165 	struct pci_attach_args *pa = aux;
    166 
    167 	if (PCI_VENDOR (pa->pa_id) == PCI_VENDOR_INTEL &&
    168 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2100)
    169 		return 1;
    170 
    171 	return 0;
    172 }
    173 
    174 /* Base Address Register */
    175 #define IPW_PCI_BAR0	0x10
    176 
    177 static void
    178 ipw_attach(struct device *parent, struct device *self, void *aux)
    179 {
    180 	struct ipw_softc *sc = (struct ipw_softc *)self;
    181 	struct ieee80211com *ic = &sc->sc_ic;
    182 	struct ifnet *ifp = &sc->sc_if;
    183 	struct pci_attach_args *pa = aux;
    184 	const char *intrstr;
    185 	char devinfo[256];
    186 	bus_space_tag_t memt;
    187 	bus_space_handle_t memh;
    188 	bus_addr_t base;
    189 	pci_intr_handle_t ih;
    190 	uint32_t data;
    191 	uint16_t val;
    192 	int i, revision, error;
    193 
    194 	sc->sc_pct = pa->pa_pc;
    195 	sc->sc_pcitag = pa->pa_tag;
    196 
    197 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof devinfo);
    198 	revision = PCI_REVISION(pa->pa_class);
    199 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo, revision);
    200 
    201 	/* enable bus-mastering */
    202 	data = pci_conf_read(sc->sc_pct, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    203 	data |= PCI_COMMAND_MASTER_ENABLE;
    204 	pci_conf_write(sc->sc_pct, pa->pa_tag, PCI_COMMAND_STATUS_REG, data);
    205 
    206 	/* clear device specific PCI configuration register 0x41 */
    207 	data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
    208 	data &= ~0x0000ff00;
    209 	pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data);
    210 
    211 	/* map the register window */
    212 	error = pci_mapreg_map(pa, IPW_PCI_BAR0, PCI_MAPREG_TYPE_MEM |
    213 	    PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh, &base, &sc->sc_sz);
    214 	if (error != 0) {
    215 		aprint_error("%s: could not map memory space\n",
    216 		    sc->sc_dev.dv_xname);
    217 		return;
    218 	}
    219 
    220 	sc->sc_st = memt;
    221 	sc->sc_sh = memh;
    222 	sc->sc_dmat = pa->pa_dmat;
    223 	strlcpy(sc->sc_fwname, "ipw2100-1.2.fw", sizeof(sc->sc_fwname));
    224 
    225 	/* disable interrupts */
    226 	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
    227 
    228 	if (pci_intr_map(pa, &ih) != 0) {
    229 		aprint_error("%s: could not map interrupt\n",
    230 		    sc->sc_dev.dv_xname);
    231 		return;
    232 	}
    233 
    234 	intrstr = pci_intr_string(sc->sc_pct, ih);
    235 	sc->sc_ih = pci_intr_establish(sc->sc_pct, ih, IPL_NET, ipw_intr, sc);
    236 	if (sc->sc_ih == NULL) {
    237 		aprint_error("%s: could not establish interrupt",
    238 		    sc->sc_dev.dv_xname);
    239 		if (intrstr != NULL)
    240 			aprint_error(" at %s", intrstr);
    241 		aprint_error("\n");
    242 		return;
    243 	}
    244 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    245 
    246 	if (ipw_reset(sc) != 0) {
    247 		aprint_error("%s: could not reset adapter\n",
    248 		    sc->sc_dev.dv_xname);
    249 		goto fail;
    250 	}
    251 
    252 	if (ipw_dma_alloc(sc) != 0) {
    253 		aprint_error("%s: could not allocate DMA resources\n",
    254 		    sc->sc_dev.dv_xname);
    255 		goto fail;
    256 	}
    257 
    258 	ifp->if_softc = sc;
    259 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    260 	ifp->if_init = ipw_init;
    261 	ifp->if_stop = ipw_stop;
    262 	ifp->if_ioctl = ipw_ioctl;
    263 	ifp->if_start = ipw_start;
    264 	ifp->if_watchdog = ipw_watchdog;
    265 	IFQ_SET_READY(&ifp->if_snd);
    266 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    267 
    268 	ic->ic_ifp = ifp;
    269 	ic->ic_phytype = IEEE80211_T_DS;
    270 	ic->ic_opmode = IEEE80211_M_STA;
    271 	ic->ic_state = IEEE80211_S_INIT;
    272 
    273 	/* set device capabilities */
    274 	ic->ic_caps = IEEE80211_C_SHPREAMBLE | IEEE80211_C_TXPMGT |
    275 	    IEEE80211_C_PMGT | IEEE80211_C_IBSS | IEEE80211_C_MONITOR;
    276 
    277 	/* read MAC address from EEPROM */
    278 	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 0);
    279 	ic->ic_myaddr[0] = val >> 8;
    280 	ic->ic_myaddr[1] = val & 0xff;
    281 	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 1);
    282 	ic->ic_myaddr[2] = val >> 8;
    283 	ic->ic_myaddr[3] = val & 0xff;
    284 	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 2);
    285 	ic->ic_myaddr[4] = val >> 8;
    286 	ic->ic_myaddr[5] = val & 0xff;
    287 
    288 	/* set supported .11b rates */
    289 	ic->ic_sup_rates[IEEE80211_MODE_11B] = ipw_rateset_11b;
    290 
    291 	/* set supported .11b channels (read from EEPROM) */
    292 	if ((val = ipw_read_prom_word(sc, IPW_EEPROM_CHANNEL_LIST)) == 0)
    293 		val = 0x7ff; /* default to channels 1-11 */
    294 	val <<= 1;
    295 	for (i = 1; i < 16; i++) {
    296 		if (val & (1 << i)) {
    297 			ic->ic_channels[i].ic_freq =
    298 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_B);
    299 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B;
    300 		}
    301 	}
    302 
    303 	/* check support for radio transmitter switch in EEPROM */
    304 	if (!(ipw_read_prom_word(sc, IPW_EEPROM_RADIO) & 8))
    305 		sc->flags |= IPW_FLAG_HAS_RADIO_SWITCH;
    306 
    307 	if_attach(ifp);
    308 	ieee80211_ifattach(ic);
    309 
    310 	/* override state transition machine */
    311 	sc->sc_newstate = ic->ic_newstate;
    312 	ic->ic_newstate = ipw_newstate;
    313 
    314 	ieee80211_media_init(ic, ipw_media_change, ipw_media_status);
    315 
    316 #if NBPFILTER > 0
    317 	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
    318 	    sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf);
    319 
    320 	sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
    321 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
    322 	sc->sc_rxtap.wr_ihdr.it_present = htole32(IPW_RX_RADIOTAP_PRESENT);
    323 
    324 	sc->sc_txtap_len = sizeof sc->sc_txtapu;
    325 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
    326 	sc->sc_txtap.wt_ihdr.it_present = htole32(IPW_TX_RADIOTAP_PRESENT);
    327 #endif
    328 
    329 	/*
    330 	 * Add a few sysctl knobs.
    331 	 * XXX: Not yet
    332 	 */
    333 	sc->dwelltime = 100;
    334 
    335 	/*
    336 	 * Make sure the interface is shutdown during reboot.
    337 	 */
    338 	sc->sc_sdhook = shutdownhook_establish(ipw_shutdown, sc);
    339 	if (sc->sc_sdhook == NULL)
    340 		aprint_error("%s: WARNING: unable to establish shutdown hook\n",
    341 		    sc->sc_dev.dv_xname);
    342 	sc->sc_powerhook = powerhook_establish(ipw_powerhook, sc);
    343 	if (sc->sc_powerhook == NULL)
    344 		printf("%s: WARNING: unable to establish power hook\n",
    345 		    sc->sc_dev.dv_xname);
    346 
    347 	ieee80211_announce(ic);
    348 
    349 	return;
    350 
    351 fail:	ipw_detach(self, 0);
    352 }
    353 
    354 static int
    355 ipw_detach(struct device* self, int flags)
    356 {
    357 	struct ipw_softc *sc = (struct ipw_softc *)self;
    358 	struct ifnet *ifp = &sc->sc_if;
    359 
    360 	if (ifp->if_softc) {
    361 		ipw_stop(ifp, 1);
    362 		ipw_free_firmware(sc);
    363 
    364 #if NBPFILTER > 0
    365 		bpfdetach(ifp);
    366 #endif
    367 		ieee80211_ifdetach(&sc->sc_ic);
    368 		if_detach(ifp);
    369 
    370 		ipw_release(sc);
    371 	}
    372 
    373 	if (sc->sc_ih != NULL) {
    374 		pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
    375 		sc->sc_ih = NULL;
    376 	}
    377 
    378 	bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz);
    379 
    380 	return 0;
    381 }
    382 
    383 static int
    384 ipw_dma_alloc(struct ipw_softc *sc)
    385 {
    386 	struct ipw_soft_bd *sbd;
    387 	struct ipw_soft_hdr *shdr;
    388 	struct ipw_soft_buf *sbuf;
    389 	int error, i, nsegs;
    390 
    391 	/*
    392 	 * Allocate and map tx ring.
    393 	 */
    394 	error = bus_dmamap_create(sc->sc_dmat, IPW_TBD_SZ, 1, IPW_TBD_SZ, 0,
    395 	    BUS_DMA_NOWAIT, &sc->tbd_map);
    396 	if (error != 0) {
    397 		aprint_error("%s: could not create tbd dma map\n",
    398 		    sc->sc_dev.dv_xname);
    399 		goto fail;
    400 	}
    401 
    402 	error = bus_dmamem_alloc(sc->sc_dmat, IPW_TBD_SZ, PAGE_SIZE, 0,
    403 	    &sc->tbd_seg, 1, &nsegs, BUS_DMA_NOWAIT);
    404 	if (error != 0) {
    405 		aprint_error("%s: could not allocate tbd dma memory\n",
    406 		    sc->sc_dev.dv_xname);
    407 		goto fail;
    408 	}
    409 
    410 	error = bus_dmamem_map(sc->sc_dmat, &sc->tbd_seg, nsegs, IPW_TBD_SZ,
    411 	    (caddr_t *)&sc->tbd_list, BUS_DMA_NOWAIT);
    412 	if (error != 0) {
    413 		aprint_error("%s: could not map tbd dma memory\n",
    414 		    sc->sc_dev.dv_xname);
    415 		goto fail;
    416 	}
    417 
    418 	error = bus_dmamap_load(sc->sc_dmat, sc->tbd_map, sc->tbd_list,
    419 	    IPW_TBD_SZ, NULL, BUS_DMA_NOWAIT);
    420 	if (error != 0) {
    421 		aprint_error("%s: could not load tbd dma memory\n",
    422 		    sc->sc_dev.dv_xname);
    423 		goto fail;
    424 	}
    425 
    426 	(void)memset(sc->tbd_list, 0, IPW_TBD_SZ);
    427 
    428 	/*
    429 	 * Allocate and map rx ring.
    430 	 */
    431 	error = bus_dmamap_create(sc->sc_dmat, IPW_RBD_SZ, 1, IPW_RBD_SZ, 0,
    432 	    BUS_DMA_NOWAIT, &sc->rbd_map);
    433 	if (error != 0) {
    434 		aprint_error("%s: could not create rbd dma map\n",
    435 		    sc->sc_dev.dv_xname);
    436 		goto fail;
    437 	}
    438 
    439 	error = bus_dmamem_alloc(sc->sc_dmat, IPW_RBD_SZ, PAGE_SIZE, 0,
    440 	    &sc->rbd_seg, 1, &nsegs, BUS_DMA_NOWAIT);
    441 	if (error != 0) {
    442 		aprint_error("%s: could not allocate rbd dma memory\n",
    443 		    sc->sc_dev.dv_xname);
    444 		goto fail;
    445 	}
    446 
    447 	error = bus_dmamem_map(sc->sc_dmat, &sc->rbd_seg, nsegs, IPW_RBD_SZ,
    448 	    (caddr_t *)&sc->rbd_list, BUS_DMA_NOWAIT);
    449 	if (error != 0) {
    450 		aprint_error("%s: could not map rbd dma memory\n",
    451 		    sc->sc_dev.dv_xname);
    452 		goto fail;
    453 	}
    454 
    455 	error = bus_dmamap_load(sc->sc_dmat, sc->rbd_map, sc->rbd_list,
    456 	    IPW_RBD_SZ, NULL, BUS_DMA_NOWAIT);
    457 	if (error != 0) {
    458 		aprint_error("%s: could not load rbd dma memory\n",
    459 		    sc->sc_dev.dv_xname);
    460 		goto fail;
    461 	}
    462 
    463 	(void)memset(sc->rbd_list, 0, IPW_RBD_SZ);
    464 
    465 	/*
    466 	 * Allocate and map status ring.
    467 	 */
    468 	error = bus_dmamap_create(sc->sc_dmat, IPW_STATUS_SZ, 1, IPW_STATUS_SZ,
    469 	    0, BUS_DMA_NOWAIT, &sc->status_map);
    470 	if (error != 0) {
    471 		aprint_error("%s: could not create status dma map\n",
    472 		    sc->sc_dev.dv_xname);
    473 		goto fail;
    474 	}
    475 
    476 	error = bus_dmamem_alloc(sc->sc_dmat, IPW_STATUS_SZ, PAGE_SIZE, 0,
    477 	    &sc->status_seg, 1, &nsegs, BUS_DMA_NOWAIT);
    478 	if (error != 0) {
    479 		aprint_error("%s: could not allocate status dma memory\n",
    480 		    sc->sc_dev.dv_xname);
    481 		goto fail;
    482 	}
    483 
    484 	error = bus_dmamem_map(sc->sc_dmat, &sc->status_seg, nsegs,
    485 	    IPW_STATUS_SZ, (caddr_t *)&sc->status_list, BUS_DMA_NOWAIT);
    486 	if (error != 0) {
    487 		aprint_error("%s: could not map status dma memory\n",
    488 		    sc->sc_dev.dv_xname);
    489 		goto fail;
    490 	}
    491 
    492 	error = bus_dmamap_load(sc->sc_dmat, sc->status_map, sc->status_list,
    493 	    IPW_STATUS_SZ, NULL, BUS_DMA_NOWAIT);
    494 	if (error != 0) {
    495 		aprint_error("%s: could not load status dma memory\n",
    496 		    sc->sc_dev.dv_xname);
    497 		goto fail;
    498 	}
    499 
    500 	(void)memset(sc->status_list, 0, IPW_STATUS_SZ);
    501 
    502 	/*
    503 	 * Allocate command DMA map.
    504 	 */
    505 	error = bus_dmamap_create(sc->sc_dmat, sizeof (struct ipw_cmd),
    506 	    1, sizeof (struct ipw_cmd), 0, BUS_DMA_NOWAIT, &sc->cmd_map);
    507 	if (error != 0) {
    508 		aprint_error("%s: could not create cmd dma map\n",
    509 		    sc->sc_dev.dv_xname);
    510 		goto fail;
    511 	}
    512 
    513 	error = bus_dmamem_alloc(sc->sc_dmat, sizeof (struct ipw_cmd),
    514 	    PAGE_SIZE, 0, &sc->cmd_seg, 1, &nsegs, BUS_DMA_NOWAIT);
    515 	if (error != 0) {
    516 		aprint_error("%s: could not allocate cmd dma memory\n",
    517 		    sc->sc_dev.dv_xname);
    518 		goto fail;
    519 	}
    520 
    521 	error = bus_dmamem_map(sc->sc_dmat, &sc->cmd_seg, nsegs,
    522 	    sizeof (struct ipw_cmd), (caddr_t *)&sc->cmd, BUS_DMA_NOWAIT);
    523 	if (error != 0) {
    524 		aprint_error("%s: could not map cmd dma memory\n",
    525 		    sc->sc_dev.dv_xname);
    526 		goto fail;
    527 	}
    528 
    529 	error = bus_dmamap_load(sc->sc_dmat, sc->cmd_map, &sc->cmd,
    530 	    sizeof (struct ipw_cmd), NULL, BUS_DMA_NOWAIT);
    531 	if (error != 0) {
    532 		aprint_error("%s: could not map cmd dma memory\n",
    533 		    sc->sc_dev.dv_xname);
    534 		return error;
    535 	}
    536 
    537 	/*
    538 	 * Allocate and map hdr list.
    539 	 */
    540 
    541 	error = bus_dmamap_create(sc->sc_dmat,
    542 	    IPW_NDATA * sizeof(struct ipw_hdr), 1,
    543 	    sizeof(struct ipw_hdr), 0, BUS_DMA_NOWAIT,
    544 	    &sc->hdr_map);
    545 	if (error != 0) {
    546 		aprint_error("%s: could not create hdr dma map\n",
    547 		    sc->sc_dev.dv_xname);
    548 		goto fail;
    549 	}
    550 
    551 	error = bus_dmamem_alloc(sc->sc_dmat,
    552 	    IPW_NDATA * sizeof(struct ipw_hdr), PAGE_SIZE, 0, &sc->hdr_seg,
    553 	    1, &nsegs, BUS_DMA_NOWAIT);
    554 	if (error != 0) {
    555 		aprint_error("%s: could not allocate hdr memory\n",
    556 		    sc->sc_dev.dv_xname);
    557 		goto fail;
    558 	}
    559 
    560 	error = bus_dmamem_map(sc->sc_dmat, &sc->hdr_seg, nsegs,
    561 	    IPW_NDATA * sizeof(struct ipw_hdr), (caddr_t *)&sc->hdr_list,
    562 	    BUS_DMA_NOWAIT);
    563 	if (error != 0) {
    564 		aprint_error("%s: could not map hdr memory\n",
    565 		    sc->sc_dev.dv_xname);
    566 		goto fail;
    567 	}
    568 
    569 	error = bus_dmamap_load(sc->sc_dmat, sc->hdr_map, sc->hdr_list,
    570 	    IPW_NDATA * sizeof(struct ipw_hdr), NULL, BUS_DMA_NOWAIT);
    571 	if (error != 0) {
    572 		aprint_error("%s: could not load hdr memory\n",
    573 		    sc->sc_dev.dv_xname);
    574 		goto fail;
    575 	}
    576 
    577 	(void)memset(sc->hdr_list, 0, IPW_HDR_SZ);
    578 
    579 	/*
    580 	 * Create DMA hdrs tailq.
    581 	 */
    582 	TAILQ_INIT(&sc->sc_free_shdr);
    583 	for (i = 0; i < IPW_NDATA; i++) {
    584 		shdr = &sc->shdr_list[i];
    585 		shdr->hdr = sc->hdr_list + i;
    586 		shdr->offset = sizeof(struct ipw_hdr) * i;
    587 		shdr->addr = sc->hdr_map->dm_segs[0].ds_addr + shdr->offset;
    588 		TAILQ_INSERT_TAIL(&sc->sc_free_shdr, shdr, next);
    589 	}
    590 
    591 	/*
    592 	 * Allocate tx buffers DMA maps.
    593 	 */
    594 	TAILQ_INIT(&sc->sc_free_sbuf);
    595 	for (i = 0; i < IPW_NDATA; i++) {
    596 		sbuf = &sc->tx_sbuf_list[i];
    597 
    598 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    599 		    IPW_MAX_NSEG, MCLBYTES, 0, BUS_DMA_NOWAIT, &sbuf->map);
    600 		if (error != 0) {
    601 			aprint_error("%s: could not create txbuf dma map\n",
    602 			    sc->sc_dev.dv_xname);
    603 			goto fail;
    604 		}
    605 		TAILQ_INSERT_TAIL(&sc->sc_free_sbuf, sbuf, next);
    606 	}
    607 
    608 	/*
    609 	 * Initialize tx ring.
    610 	 */
    611 	for (i = 0; i < IPW_NTBD; i++) {
    612 		sbd = &sc->stbd_list[i];
    613 		sbd->bd = &sc->tbd_list[i];
    614 		sbd->type = IPW_SBD_TYPE_NOASSOC;
    615 	}
    616 
    617 	/*
    618 	 * Pre-allocate rx buffers and DMA maps
    619 	 */
    620 	for (i = 0; i < IPW_NRBD; i++) {
    621 		sbd = &sc->srbd_list[i];
    622 		sbuf = &sc->rx_sbuf_list[i];
    623 		sbd->bd = &sc->rbd_list[i];
    624 
    625 		MGETHDR(sbuf->m, M_DONTWAIT, MT_DATA);
    626 		if (sbuf->m == NULL) {
    627 			aprint_error("%s: could not allocate rx mbuf\n",
    628 			    sc->sc_dev.dv_xname);
    629 			error = ENOMEM;
    630 			goto fail;
    631 		}
    632 
    633 		MCLGET(sbuf->m, M_DONTWAIT);
    634 		if (!(sbuf->m->m_flags & M_EXT)) {
    635 			m_freem(sbuf->m);
    636 			aprint_error("%s: could not allocate rx mbuf cluster\n",
    637 			    sc->sc_dev.dv_xname);
    638 			error = ENOMEM;
    639 			goto fail;
    640 		}
    641 
    642 		sbuf->m->m_pkthdr.len = sbuf->m->m_len = sbuf->m->m_ext.ext_size;
    643 
    644 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
    645 		    0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &sbuf->map);
    646 		if (error != 0) {
    647 			aprint_error("%s: could not create rxbuf dma map\n",
    648 			    sc->sc_dev.dv_xname);
    649 			m_freem(sbuf->m);
    650 			goto fail;
    651 		}
    652 
    653 		error = bus_dmamap_load_mbuf(sc->sc_dmat, sbuf->map,
    654 		    sbuf->m, BUS_DMA_READ | BUS_DMA_NOWAIT);
    655 		if (error != 0) {
    656 			bus_dmamap_destroy(sc->sc_dmat, sbuf->map);
    657 			m_freem(sbuf->m);
    658 			aprint_error("%s: could not map rxbuf dma memory\n",
    659 			    sc->sc_dev.dv_xname);
    660 			goto fail;
    661 		}
    662 
    663 		sbd->type = IPW_SBD_TYPE_DATA;
    664 		sbd->priv = sbuf;
    665 		sbd->bd->physaddr = htole32(sbuf->map->dm_segs[0].ds_addr);
    666 		sbd->bd->len = htole32(MCLBYTES);
    667 
    668 		bus_dmamap_sync(sc->sc_dmat, sbuf->map, 0,
    669 		    sbuf->map->dm_mapsize, BUS_DMASYNC_PREREAD);
    670 
    671 	}
    672 
    673 	bus_dmamap_sync(sc->sc_dmat, sc->rbd_map, 0, IPW_RBD_SZ,
    674 	    BUS_DMASYNC_PREREAD);
    675 
    676 	return 0;
    677 
    678 fail:	ipw_release(sc);
    679 	return error;
    680 }
    681 
    682 static void
    683 ipw_release(struct ipw_softc *sc)
    684 {
    685 	struct ipw_soft_buf *sbuf;
    686 	int i;
    687 
    688 	if (sc->tbd_map != NULL) {
    689 		if (sc->tbd_list != NULL) {
    690 			bus_dmamap_unload(sc->sc_dmat, sc->tbd_map);
    691 			bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->tbd_list,
    692 			    IPW_TBD_SZ);
    693 			bus_dmamem_free(sc->sc_dmat, &sc->tbd_seg, 1);
    694 		}
    695 		bus_dmamap_destroy(sc->sc_dmat, sc->tbd_map);
    696 	}
    697 
    698 	if (sc->rbd_map != NULL) {
    699 		if (sc->rbd_list != NULL) {
    700 			bus_dmamap_unload(sc->sc_dmat, sc->rbd_map);
    701 			bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->rbd_list,
    702 			    IPW_RBD_SZ);
    703 			bus_dmamem_free(sc->sc_dmat, &sc->rbd_seg, 1);
    704 		}
    705 		bus_dmamap_destroy(sc->sc_dmat, sc->rbd_map);
    706 	}
    707 
    708 	if (sc->status_map != NULL) {
    709 		if (sc->status_list != NULL) {
    710 			bus_dmamap_unload(sc->sc_dmat, sc->status_map);
    711 			bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->status_list,
    712 			    IPW_RBD_SZ);
    713 			bus_dmamem_free(sc->sc_dmat, &sc->status_seg, 1);
    714 		}
    715 		bus_dmamap_destroy(sc->sc_dmat, sc->status_map);
    716 	}
    717 
    718 	for (i = 0; i < IPW_NTBD; i++)
    719 		ipw_release_sbd(sc, &sc->stbd_list[i]);
    720 
    721 	if (sc->cmd_map != NULL)
    722 		bus_dmamap_destroy(sc->sc_dmat, sc->cmd_map);
    723 
    724  	if (sc->hdr_list != NULL) {
    725  		bus_dmamap_unload(sc->sc_dmat, sc->hdr_map);
    726  		bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->hdr_list,
    727  		    IPW_NDATA * sizeof(struct ipw_hdr));
    728  	}
    729  	if (sc->hdr_map != NULL) {
    730  		bus_dmamem_free(sc->sc_dmat, &sc->hdr_seg, 1);
    731  		bus_dmamap_destroy(sc->sc_dmat, sc->hdr_map);
    732  	}
    733 
    734 	for (i = 0; i < IPW_NDATA; i++)
    735 		bus_dmamap_destroy(sc->sc_dmat, sc->tx_sbuf_list[i].map);
    736 
    737 	for (i = 0; i < IPW_NRBD; i++) {
    738 		sbuf = &sc->rx_sbuf_list[i];
    739 		if (sbuf->map != NULL) {
    740 			if (sbuf->m != NULL) {
    741 				bus_dmamap_unload(sc->sc_dmat, sbuf->map);
    742 				m_freem(sbuf->m);
    743 			}
    744 			bus_dmamap_destroy(sc->sc_dmat, sbuf->map);
    745 		}
    746 	}
    747 
    748 }
    749 
    750 static void
    751 ipw_shutdown(void *arg)
    752 {
    753 	struct ipw_softc *sc = (struct ipw_softc *)arg;
    754 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
    755 
    756 	ipw_stop(ifp, 1);
    757 }
    758 
    759 
    760 static int
    761 ipw_suspend(struct ipw_softc *sc)
    762 {
    763 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
    764 
    765 	ipw_stop(ifp, 1);
    766 
    767 	return 0;
    768 }
    769 
    770 static int
    771 ipw_resume(struct ipw_softc *sc)
    772 {
    773 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
    774 	pcireg_t data;
    775 
    776 	/* clear device specific PCI configuration register 0x41 */
    777 	data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
    778 	data &= ~0x0000ff00;
    779 	pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data);
    780 
    781 	if (ifp->if_flags & IFF_UP) {
    782 		ipw_init(ifp);
    783 		if (ifp->if_flags & IFF_RUNNING)
    784 			ipw_start(ifp);
    785 	}
    786 
    787 	return 0;
    788 }
    789 
    790 static void
    791 ipw_powerhook(int why, void *arg)
    792 {
    793         struct ipw_softc *sc = arg;
    794 	int s;
    795 
    796 	s = splnet();
    797 	switch (why) {
    798 	case PWR_SUSPEND:
    799 	case PWR_STANDBY:
    800 		ipw_suspend(sc);
    801 		break;
    802 	case PWR_RESUME:
    803 		ipw_resume(sc);
    804 		break;
    805 	case PWR_SOFTSUSPEND:
    806 	case PWR_SOFTSTANDBY:
    807 	case PWR_SOFTRESUME:
    808 		break;
    809 	}
    810 	splx(s);
    811 }
    812 
    813 static int
    814 ipw_media_change(struct ifnet *ifp)
    815 {
    816 	int error;
    817 
    818 	error = ieee80211_media_change(ifp);
    819 	if (error != ENETRESET)
    820 		return error;
    821 
    822 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
    823 		ipw_init(ifp);
    824 
    825 	return 0;
    826 }
    827 
    828 /*
    829  * The firmware automatically adapts the transmit speed. We report the current
    830  * transmit speed here.
    831  */
    832 static void
    833 ipw_media_status(struct ifnet *ifp, struct ifmediareq *imr)
    834 {
    835 #define N(a)	(sizeof (a) / sizeof (a[0]))
    836 	struct ipw_softc *sc = ifp->if_softc;
    837 	struct ieee80211com *ic = &sc->sc_ic;
    838 	static const struct {
    839 		uint32_t	val;
    840 		int		rate;
    841 	} rates[] = {
    842 		{ IPW_RATE_DS1,   2 },
    843 		{ IPW_RATE_DS2,   4 },
    844 		{ IPW_RATE_DS5,  11 },
    845 		{ IPW_RATE_DS11, 22 },
    846 	};
    847 	uint32_t val;
    848 	int rate, i;
    849 
    850 	imr->ifm_status = IFM_AVALID;
    851 	imr->ifm_active = IFM_IEEE80211;
    852 	if (ic->ic_state == IEEE80211_S_RUN)
    853 		imr->ifm_status |= IFM_ACTIVE;
    854 
    855 	/* read current transmission rate from adapter */
    856 	val = ipw_read_table1(sc, IPW_INFO_CURRENT_TX_RATE) & 0xf;
    857 
    858 	/* convert ipw rate to 802.11 rate */
    859 	for (i = 0; i < N(rates) && rates[i].val != val; i++);
    860 	rate = (i < N(rates)) ? rates[i].rate : 0;
    861 
    862 	imr->ifm_active |= IFM_IEEE80211_11B;
    863 	imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
    864 	switch (ic->ic_opmode) {
    865 	case IEEE80211_M_STA:
    866 		break;
    867 
    868 	case IEEE80211_M_IBSS:
    869 		imr->ifm_active |= IFM_IEEE80211_ADHOC;
    870 		break;
    871 
    872 	case IEEE80211_M_MONITOR:
    873 		imr->ifm_active |= IFM_IEEE80211_MONITOR;
    874 		break;
    875 
    876 	case IEEE80211_M_AHDEMO:
    877 	case IEEE80211_M_HOSTAP:
    878 		/* should not get there */
    879 		break;
    880 	}
    881 #undef N
    882 }
    883 
    884 static int
    885 ipw_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
    886 {
    887 	struct ifnet *ifp = ic->ic_ifp;
    888 	struct ipw_softc *sc = ifp->if_softc;
    889 	struct ieee80211_node *ni;
    890 	uint8_t macaddr[IEEE80211_ADDR_LEN];
    891 	uint32_t len;
    892 
    893 	switch (nstate) {
    894 	case IEEE80211_S_RUN:
    895 		DELAY(200); /* firmware needs a short delay here */
    896 
    897 		len = IEEE80211_ADDR_LEN;
    898 		ipw_read_table2(sc, IPW_INFO_CURRENT_BSSID, macaddr, &len);
    899 
    900 		ni = ieee80211_find_node(&ic->ic_scan, macaddr);
    901 		if (ni == NULL)
    902 			break;
    903 
    904 		ieee80211_ref_node(ni);
    905 		ieee80211_sta_join(ic, ni);
    906 		ieee80211_node_authorize(ni);
    907 
    908 		if (ic->ic_opmode == IEEE80211_M_STA)
    909 			ieee80211_notify_node_join(ic, ni, 1);
    910 		break;
    911 
    912 	case IEEE80211_S_INIT:
    913 	case IEEE80211_S_SCAN:
    914 	case IEEE80211_S_AUTH:
    915 	case IEEE80211_S_ASSOC:
    916 		break;
    917 	}
    918 
    919 	ic->ic_state = nstate;
    920 	return 0;
    921 }
    922 
    923 /*
    924  * Read 16 bits at address 'addr' from the serial EEPROM.
    925  */
    926 static uint16_t
    927 ipw_read_prom_word(struct ipw_softc *sc, uint8_t addr)
    928 {
    929 	uint32_t tmp;
    930 	uint16_t val;
    931 	int n;
    932 
    933 	/* clock C once before the first command */
    934 	IPW_EEPROM_CTL(sc, 0);
    935 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
    936 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
    937 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
    938 
    939 	/* write start bit (1) */
    940 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
    941 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
    942 
    943 	/* write READ opcode (10) */
    944 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
    945 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
    946 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
    947 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
    948 
    949 	/* write address A7-A0 */
    950 	for (n = 7; n >= 0; n--) {
    951 		IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
    952 		    (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D));
    953 		IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
    954 		    (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D) | IPW_EEPROM_C);
    955 	}
    956 
    957 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
    958 
    959 	/* read data Q15-Q0 */
    960 	val = 0;
    961 	for (n = 15; n >= 0; n--) {
    962 		IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
    963 		IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
    964 		tmp = MEM_READ_4(sc, IPW_MEM_EEPROM_CTL);
    965 		val |= ((tmp & IPW_EEPROM_Q) >> IPW_EEPROM_SHIFT_Q) << n;
    966 	}
    967 
    968 	IPW_EEPROM_CTL(sc, 0);
    969 
    970 	/* clear Chip Select and clock C */
    971 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
    972 	IPW_EEPROM_CTL(sc, 0);
    973 	IPW_EEPROM_CTL(sc, IPW_EEPROM_C);
    974 
    975 	return le16toh(val);
    976 }
    977 
    978 static void
    979 ipw_command_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
    980 {
    981 	struct ipw_cmd *cmd;
    982 
    983 	bus_dmamap_sync(sc->sc_dmat, sbuf->map, 0, sizeof (struct ipw_cmd),
    984 	    BUS_DMASYNC_POSTREAD);
    985 
    986 	cmd = mtod(sbuf->m, struct ipw_cmd *);
    987 
    988 	DPRINTFN(2, ("cmd ack'ed (%u, %u, %u, %u, %u)\n", le32toh(cmd->type),
    989 	    le32toh(cmd->subtype), le32toh(cmd->seq), le32toh(cmd->len),
    990 	    le32toh(cmd->status)));
    991 
    992 	wakeup(&sc->cmd);
    993 }
    994 
    995 static void
    996 ipw_newstate_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
    997 {
    998 	struct ieee80211com *ic = &sc->sc_ic;
    999 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
   1000 	uint32_t state;
   1001 
   1002 	bus_dmamap_sync(sc->sc_dmat, sbuf->map, 0, sizeof state,
   1003 	    BUS_DMASYNC_POSTREAD);
   1004 
   1005 	state = le32toh(*mtod(sbuf->m, uint32_t *));
   1006 
   1007 	DPRINTFN(2, ("entering state %u\n", state));
   1008 
   1009 	switch (state) {
   1010 	case IPW_STATE_ASSOCIATED:
   1011 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
   1012 		break;
   1013 
   1014 	case IPW_STATE_SCANNING:
   1015 		/* don't leave run state on background scan */
   1016 		if (ic->ic_state != IEEE80211_S_RUN)
   1017 			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
   1018 
   1019 		ic->ic_flags |= IEEE80211_F_SCAN;
   1020 		break;
   1021 
   1022 	case IPW_STATE_SCAN_COMPLETE:
   1023 		ieee80211_notify_scan_done(ic);
   1024 		ic->ic_flags &= ~IEEE80211_F_SCAN;
   1025 		break;
   1026 
   1027 	case IPW_STATE_ASSOCIATION_LOST:
   1028 		ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
   1029 		break;
   1030 
   1031 	case IPW_STATE_RADIO_DISABLED:
   1032 		ic->ic_ifp->if_flags &= ~IFF_UP;
   1033 		ipw_stop(ifp, 1);
   1034 		break;
   1035 	}
   1036 }
   1037 
   1038 /*
   1039  * XXX: Hack to set the current channel to the value advertised in beacons or
   1040  * probe responses. Only used during AP detection.
   1041  */
   1042 static void
   1043 ipw_fix_channel(struct ieee80211com *ic, struct mbuf *m)
   1044 {
   1045 	struct ieee80211_frame *wh;
   1046 	uint8_t subtype;
   1047 	uint8_t *frm, *efrm;
   1048 
   1049 	wh = mtod(m, struct ieee80211_frame *);
   1050 
   1051 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
   1052 		return;
   1053 
   1054 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
   1055 
   1056 	if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
   1057 	    subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
   1058 		return;
   1059 
   1060 	frm = (uint8_t *)(wh + 1);
   1061 	efrm = mtod(m, uint8_t *) + m->m_len;
   1062 
   1063 	frm += 12;	/* skip tstamp, bintval and capinfo fields */
   1064 	while (frm < efrm) {
   1065 		if (*frm == IEEE80211_ELEMID_DSPARMS)
   1066 #if IEEE80211_CHAN_MAX < 255
   1067 		if (frm[2] <= IEEE80211_CHAN_MAX)
   1068 #endif
   1069 			ic->ic_curchan = &ic->ic_channels[frm[2]];
   1070 
   1071 		frm += frm[1] + 2;
   1072 	}
   1073 }
   1074 
   1075 static void
   1076 ipw_data_intr(struct ipw_softc *sc, struct ipw_status *status,
   1077     struct ipw_soft_bd *sbd, struct ipw_soft_buf *sbuf)
   1078 {
   1079 	struct ieee80211com *ic = &sc->sc_ic;
   1080 	struct ifnet *ifp = &sc->sc_if;
   1081 	struct mbuf *mnew, *m;
   1082 	struct ieee80211_frame *wh;
   1083 	struct ieee80211_node *ni;
   1084 	int error;
   1085 
   1086 	DPRINTFN(5, ("received frame len=%u, rssi=%u\n", le32toh(status->len),
   1087 	    status->rssi));
   1088 
   1089 	if (le32toh(status->len) < sizeof (struct ieee80211_frame_min) ||
   1090 	    le32toh(status->len) > MCLBYTES)
   1091 		return;
   1092 
   1093 	/*
   1094 	 * Try to allocate a new mbuf for this ring element and load it before
   1095 	 * processing the current mbuf. If the ring element cannot be loaded,
   1096 	 * drop the received packet and reuse the old mbuf. In the unlikely
   1097 	 * case that the old mbuf can't be reloaded either, explicitly panic.
   1098 	 */
   1099 	MGETHDR(mnew, M_DONTWAIT, MT_DATA);
   1100 	if (mnew == NULL) {
   1101 		aprint_error("%s: could not allocate rx mbuf\n",
   1102 		    sc->sc_dev.dv_xname);
   1103 		ifp->if_ierrors++;
   1104 		return;
   1105 	}
   1106 
   1107 	MCLGET(mnew, M_DONTWAIT);
   1108 	if (!(mnew->m_flags & M_EXT)) {
   1109 		aprint_error("%s: could not allocate rx mbuf cluster\n",
   1110 		    sc->sc_dev.dv_xname);
   1111 		m_freem(mnew);
   1112 		ifp->if_ierrors++;
   1113 		return;
   1114 	}
   1115 
   1116 	mnew->m_pkthdr.len = mnew->m_len = mnew->m_ext.ext_size;
   1117 
   1118 	bus_dmamap_sync(sc->sc_dmat, sbuf->map, 0, le32toh(status->len),
   1119 	    BUS_DMASYNC_POSTREAD);
   1120 	bus_dmamap_unload(sc->sc_dmat, sbuf->map);
   1121 
   1122 	error = bus_dmamap_load_mbuf(sc->sc_dmat, sbuf->map, mnew,
   1123 	    BUS_DMA_READ | BUS_DMA_NOWAIT);
   1124 	if (error != 0) {
   1125 		aprint_error("%s: could not load rx buf DMA map\n",
   1126 		    sc->sc_dev.dv_xname);
   1127 		m_freem(mnew);
   1128 
   1129 		/* try to reload the old mbuf */
   1130 		error = bus_dmamap_load_mbuf(sc->sc_dmat, sbuf->map,
   1131 		    sbuf->m, BUS_DMA_READ | BUS_DMA_NOWAIT);
   1132 		if (error != 0) {
   1133 			/* very unlikely that it will fail... */
   1134 			panic("%s: unable to remap rx buf",
   1135 			    sc->sc_dev.dv_xname);
   1136 		}
   1137 		ifp->if_ierrors++;
   1138 		return;
   1139 	}
   1140 
   1141 	/*
   1142 	 * New mbuf successfully loaded, update Rx ring and continue
   1143 	 * processing.
   1144 	 */
   1145 	m = sbuf->m;
   1146 	sbuf->m = mnew;
   1147 	sbd->bd->physaddr = htole32(sbuf->map->dm_segs[0].ds_addr);
   1148 
   1149 	/* finalize mbuf */
   1150 	m->m_pkthdr.rcvif = ifp;
   1151 	m->m_pkthdr.len = m->m_len = le32toh(status->len);
   1152 
   1153 #if NBPFILTER > 0
   1154 	if (sc->sc_drvbpf != NULL) {
   1155 		struct ipw_rx_radiotap_header *tap = &sc->sc_rxtap;
   1156 
   1157 		tap->wr_flags = 0;
   1158 		tap->wr_antsignal = status->rssi;
   1159 		tap->wr_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
   1160 		tap->wr_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
   1161 
   1162 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
   1163 	}
   1164 #endif
   1165 
   1166 	if (ic->ic_state == IEEE80211_S_SCAN)
   1167 		ipw_fix_channel(ic, m);
   1168 
   1169 	wh = mtod(m, struct ieee80211_frame *);
   1170 	ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
   1171 
   1172 	/* send the frame to the 802.11 layer */
   1173 	ieee80211_input(ic, m, ni, status->rssi, 0);
   1174 
   1175 	/* node is no longer needed */
   1176 	ieee80211_free_node(ni);
   1177 
   1178 	bus_dmamap_sync(sc->sc_dmat, sbuf->map, 0,
   1179 	    sbuf->map->dm_mapsize, BUS_DMASYNC_PREREAD);
   1180 }
   1181 
   1182 static void
   1183 ipw_rx_intr(struct ipw_softc *sc)
   1184 {
   1185 	struct ipw_status *status;
   1186 	struct ipw_soft_bd *sbd;
   1187 	struct ipw_soft_buf *sbuf;
   1188 	uint32_t r, i;
   1189 
   1190 	if (!(sc->flags & IPW_FLAG_FW_INITED))
   1191 		return;
   1192 
   1193 	r = CSR_READ_4(sc, IPW_CSR_RX_READ);
   1194 
   1195 	for (i = (sc->rxcur + 1) % IPW_NRBD; i != r; i = (i + 1) % IPW_NRBD) {
   1196 
   1197 		/* firmware was killed, stop processing received frames */
   1198 		if (!(sc->flags & IPW_FLAG_FW_INITED))
   1199 			return;
   1200 
   1201 		bus_dmamap_sync(sc->sc_dmat, sc->rbd_map,
   1202 		    i * sizeof (struct ipw_bd), sizeof (struct ipw_bd),
   1203 		    BUS_DMASYNC_POSTREAD);
   1204 
   1205 		bus_dmamap_sync(sc->sc_dmat, sc->status_map,
   1206 		    i * sizeof (struct ipw_status), sizeof (struct ipw_status),
   1207 		    BUS_DMASYNC_POSTREAD);
   1208 
   1209 		status = &sc->status_list[i];
   1210 		sbd = &sc->srbd_list[i];
   1211 		sbuf = sbd->priv;
   1212 
   1213 		switch (le16toh(status->code) & 0xf) {
   1214 		case IPW_STATUS_CODE_COMMAND:
   1215 			ipw_command_intr(sc, sbuf);
   1216 			break;
   1217 
   1218 		case IPW_STATUS_CODE_NEWSTATE:
   1219 			ipw_newstate_intr(sc, sbuf);
   1220 			break;
   1221 
   1222 		case IPW_STATUS_CODE_DATA_802_3:
   1223 		case IPW_STATUS_CODE_DATA_802_11:
   1224 			ipw_data_intr(sc, status, sbd, sbuf);
   1225 			break;
   1226 
   1227 		case IPW_STATUS_CODE_NOTIFICATION:
   1228 			DPRINTFN(2, ("received notification\n"));
   1229 			break;
   1230 
   1231 		default:
   1232 			aprint_debug("%s: unknown status code %u\n",
   1233 			    sc->sc_dev.dv_xname, le16toh(status->code));
   1234 		}
   1235 
   1236 		sbd->bd->flags = 0;
   1237 
   1238 		bus_dmamap_sync(sc->sc_dmat, sc->rbd_map,
   1239 		    i * sizeof (struct ipw_bd), sizeof (struct ipw_bd),
   1240 		    BUS_DMASYNC_PREREAD);
   1241 
   1242 		bus_dmamap_sync(sc->sc_dmat, sc->status_map,
   1243 		    i * sizeof (struct ipw_status), sizeof (struct ipw_status),
   1244 		    BUS_DMASYNC_PREREAD);
   1245 	}
   1246 
   1247 	/* Tell the firmware what we have processed */
   1248 	sc->rxcur = (r == 0) ? IPW_NRBD - 1 : r - 1;
   1249 	CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
   1250 }
   1251 
   1252 static void
   1253 ipw_release_sbd(struct ipw_softc *sc, struct ipw_soft_bd *sbd)
   1254 {
   1255 	struct ieee80211com *ic;
   1256 	struct ipw_soft_hdr *shdr;
   1257 	struct ipw_soft_buf *sbuf;
   1258 
   1259 	switch (sbd->type) {
   1260 	case IPW_SBD_TYPE_COMMAND:
   1261 		bus_dmamap_sync(sc->sc_dmat, sc->cmd_map,
   1262 		    0, sizeof(struct ipw_cmd), BUS_DMASYNC_POSTWRITE);
   1263 /*		bus_dmamap_unload(sc->sc_dmat, sc->cmd_map); */
   1264 		break;
   1265 
   1266 	case IPW_SBD_TYPE_HEADER:
   1267 		shdr = sbd->priv;
   1268  		bus_dmamap_sync(sc->sc_dmat, sc->hdr_map,
   1269  		    shdr->offset, sizeof(struct ipw_hdr), BUS_DMASYNC_POSTWRITE);
   1270 		TAILQ_INSERT_TAIL(&sc->sc_free_shdr, shdr, next);
   1271 		break;
   1272 
   1273 	case IPW_SBD_TYPE_DATA:
   1274 		ic = &sc->sc_ic;
   1275 		sbuf = sbd->priv;
   1276 
   1277 		bus_dmamap_sync(sc->sc_dmat, sbuf->map,
   1278 		    0, MCLBYTES, BUS_DMASYNC_POSTWRITE);
   1279 		bus_dmamap_unload(sc->sc_dmat, sbuf->map);
   1280 		m_freem(sbuf->m);
   1281 		if (sbuf->ni != NULL)
   1282 			ieee80211_free_node(sbuf->ni);
   1283 		/* kill watchdog timer */
   1284 		sc->sc_tx_timer = 0;
   1285 		TAILQ_INSERT_TAIL(&sc->sc_free_sbuf, sbuf, next);
   1286 		break;
   1287 	}
   1288 	sbd->type = IPW_SBD_TYPE_NOASSOC;
   1289 }
   1290 
   1291 static void
   1292 ipw_tx_intr(struct ipw_softc *sc)
   1293 {
   1294 	struct ifnet *ifp = &sc->sc_if;
   1295 	struct ipw_soft_bd *sbd;
   1296 	uint32_t r, i;
   1297 
   1298 	if (!(sc->flags & IPW_FLAG_FW_INITED))
   1299 		return;
   1300 
   1301 	r = CSR_READ_4(sc, IPW_CSR_TX_READ);
   1302 
   1303 	for (i = (sc->txold + 1) % IPW_NTBD; i != r; i = (i + 1) % IPW_NTBD) {
   1304 		sbd = &sc->stbd_list[i];
   1305 
   1306 		if (sbd->type == IPW_SBD_TYPE_DATA)
   1307 			ifp->if_opackets++;
   1308 
   1309 		ipw_release_sbd(sc, sbd);
   1310 		sc->txfree++;
   1311 	}
   1312 
   1313 	/* remember what the firmware has processed */
   1314 	sc->txold = (r == 0) ? IPW_NTBD - 1 : r - 1;
   1315 
   1316 	/* Call start() since some buffer descriptors have been released */
   1317 	ifp->if_flags &= ~IFF_OACTIVE;
   1318 	(*ifp->if_start)(ifp);
   1319 }
   1320 
   1321 static int
   1322 ipw_intr(void *arg)
   1323 {
   1324 	struct ipw_softc *sc = arg;
   1325 	uint32_t r;
   1326 
   1327 	r = CSR_READ_4(sc, IPW_CSR_INTR);
   1328 	if (r == 0 || r == 0xffffffff)
   1329 		return 0;
   1330 
   1331 	/* Disable interrupts */
   1332 	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
   1333 
   1334 	if (r & (IPW_INTR_FATAL_ERROR | IPW_INTR_PARITY_ERROR)) {
   1335 		aprint_error("%s: fatal error\n",
   1336 		    sc->sc_dev.dv_xname);
   1337 		sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP;
   1338 		ipw_stop(&sc->sc_if, 1);
   1339 	}
   1340 
   1341 	if (r & IPW_INTR_FW_INIT_DONE) {
   1342 		if (!(r & (IPW_INTR_FATAL_ERROR | IPW_INTR_PARITY_ERROR)))
   1343 			wakeup(sc);
   1344 	}
   1345 
   1346 	if (r & IPW_INTR_RX_TRANSFER)
   1347 		ipw_rx_intr(sc);
   1348 
   1349 	if (r & IPW_INTR_TX_TRANSFER)
   1350 		ipw_tx_intr(sc);
   1351 
   1352 	/* Acknowledge all interrupts */
   1353 	CSR_WRITE_4(sc, IPW_CSR_INTR, r);
   1354 
   1355 	/* Re-enable interrupts */
   1356 	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
   1357 
   1358 	return 0;
   1359 }
   1360 
   1361 /*
   1362  * Send a command to the firmware and wait for the acknowledgement.
   1363  */
   1364 static int
   1365 ipw_cmd(struct ipw_softc *sc, uint32_t type, void *data, uint32_t len)
   1366 {
   1367 	struct ipw_soft_bd *sbd;
   1368 
   1369 	sbd = &sc->stbd_list[sc->txcur];
   1370 
   1371 	sc->cmd.type = htole32(type);
   1372 	sc->cmd.subtype = 0;
   1373 	sc->cmd.len = htole32(len);
   1374 	sc->cmd.seq = 0;
   1375 
   1376 	(void)memcpy(sc->cmd.data, data, len);
   1377 
   1378 	sbd->type = IPW_SBD_TYPE_COMMAND;
   1379 	sbd->bd->physaddr = htole32(sc->cmd_map->dm_segs[0].ds_addr);
   1380 	sbd->bd->len = htole32(sizeof (struct ipw_cmd));
   1381 	sbd->bd->nfrag = 1;
   1382 	sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_COMMAND |
   1383 			 IPW_BD_FLAG_TX_LAST_FRAGMENT;
   1384 
   1385 	bus_dmamap_sync(sc->sc_dmat, sc->cmd_map, 0, sizeof (struct ipw_cmd),
   1386 	    BUS_DMASYNC_PREWRITE);
   1387 
   1388 	bus_dmamap_sync(sc->sc_dmat, sc->tbd_map,
   1389 	    sc->txcur * sizeof (struct ipw_bd), sizeof (struct ipw_bd),
   1390 	    BUS_DMASYNC_PREWRITE);
   1391 
   1392 	DPRINTFN(2, ("sending command (%u, %u, %u, %u)\n", type, 0, 0, len));
   1393 
   1394 	/* kick firmware */
   1395 	sc->txfree--;
   1396 	sc->txcur = (sc->txcur + 1) % IPW_NTBD;
   1397 	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
   1398 
   1399 	/* Wait at most one second for command to complete */
   1400 	return tsleep(&sc->cmd, 0, "ipwcmd", hz);
   1401 }
   1402 
   1403 static int
   1404 ipw_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni)
   1405 {
   1406 	struct ipw_softc *sc = ifp->if_softc;
   1407 	struct ieee80211com *ic = &sc->sc_ic;
   1408 	struct ieee80211_frame *wh;
   1409 	struct ipw_soft_bd *sbd;
   1410 	struct ipw_soft_hdr *shdr;
   1411 	struct ipw_soft_buf *sbuf;
   1412 	struct ieee80211_key *k;
   1413 	struct mbuf *mnew;
   1414 	int error, i;
   1415 
   1416 	wh = mtod(m0, struct ieee80211_frame *);
   1417 
   1418 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
   1419 		k = ieee80211_crypto_encap(ic, ni, m0);
   1420 		if (k == NULL) {
   1421 			m_freem(m0);
   1422 			return ENOBUFS;
   1423 		}
   1424 
   1425 		/* packet header may have moved, reset our local pointer */
   1426 		wh = mtod(m0, struct ieee80211_frame *);
   1427 	}
   1428 
   1429 #if NBPFILTER > 0
   1430 	if (sc->sc_drvbpf != NULL) {
   1431 		struct ipw_tx_radiotap_header *tap = &sc->sc_txtap;
   1432 
   1433 		tap->wt_flags = 0;
   1434 		tap->wt_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
   1435 		tap->wt_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
   1436 
   1437 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
   1438 	}
   1439 #endif
   1440 
   1441 	shdr = TAILQ_FIRST(&sc->sc_free_shdr);
   1442 	sbuf = TAILQ_FIRST(&sc->sc_free_sbuf);
   1443 	KASSERT(shdr != NULL && sbuf != NULL);
   1444 
   1445 	shdr->hdr->type = htole32(IPW_HDR_TYPE_SEND);
   1446 	shdr->hdr->subtype = 0;
   1447 	shdr->hdr->encrypted = (wh->i_fc[1] & IEEE80211_FC1_WEP) ? 1 : 0;
   1448 	shdr->hdr->encrypt = 0;
   1449 	shdr->hdr->keyidx = 0;
   1450 	shdr->hdr->keysz = 0;
   1451 	shdr->hdr->fragmentsz = 0;
   1452 	IEEE80211_ADDR_COPY(shdr->hdr->src_addr, wh->i_addr2);
   1453 	if (ic->ic_opmode == IEEE80211_M_STA)
   1454 		IEEE80211_ADDR_COPY(shdr->hdr->dst_addr, wh->i_addr3);
   1455 	else
   1456 		IEEE80211_ADDR_COPY(shdr->hdr->dst_addr, wh->i_addr1);
   1457 
   1458 	/* trim IEEE802.11 header */
   1459 	m_adj(m0, sizeof (struct ieee80211_frame));
   1460 
   1461 	error = bus_dmamap_load_mbuf(sc->sc_dmat, sbuf->map, m0, BUS_DMA_NOWAIT);
   1462 	if (error != 0 && error != EFBIG) {
   1463 		aprint_error("%s: could not map mbuf (error %d)\n",
   1464 		    sc->sc_dev.dv_xname, error);
   1465 		m_freem(m0);
   1466 		return error;
   1467 	}
   1468 
   1469 	if (error != 0) {
   1470 		/* too many fragments, linearize */
   1471 
   1472 		MGETHDR(mnew, M_DONTWAIT, MT_DATA);
   1473 		if (mnew == NULL) {
   1474 			m_freem(m0);
   1475 			return ENOMEM;
   1476 		}
   1477 
   1478 		M_COPY_PKTHDR(mnew, m0);
   1479 
   1480 		/* If the data won't fit in the header, get a cluster */
   1481 		if (m0->m_pkthdr.len > MHLEN) {
   1482 			MCLGET(mnew, M_DONTWAIT);
   1483 			if (!(mnew->m_flags & M_EXT)) {
   1484 				m_freem(m0);
   1485 				m_freem(mnew);
   1486 				return ENOMEM;
   1487 			}
   1488 		}
   1489 		m_copydata(m0, 0, m0->m_pkthdr.len, mtod(mnew, caddr_t));
   1490 		m_freem(m0);
   1491 		mnew->m_len = mnew->m_pkthdr.len;
   1492 		m0 = mnew;
   1493 
   1494 		error = bus_dmamap_load_mbuf(sc->sc_dmat, sbuf->map, m0,
   1495 		    BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   1496 		if (error != 0) {
   1497 			aprint_error("%s: could not map mbuf (error %d)\n",
   1498 			    sc->sc_dev.dv_xname, error);
   1499 			m_freem(m0);
   1500 			return error;
   1501 		}
   1502 	}
   1503 
   1504 	TAILQ_REMOVE(&sc->sc_free_sbuf, sbuf, next);
   1505 	TAILQ_REMOVE(&sc->sc_free_shdr, shdr, next);
   1506 
   1507 	sbd = &sc->stbd_list[sc->txcur];
   1508 	sbd->type = IPW_SBD_TYPE_HEADER;
   1509 	sbd->priv = shdr;
   1510  	sbd->bd->physaddr = htole32(shdr->addr);
   1511 	sbd->bd->len = htole32(sizeof (struct ipw_hdr));
   1512 	sbd->bd->nfrag = 1 + sbuf->map->dm_nsegs;
   1513 	sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3 |
   1514 			 IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
   1515 
   1516 	DPRINTFN(5, ("sending tx hdr (%u, %u, %u, %u, )\n",
   1517 	    shdr->hdr->type, shdr->hdr->subtype, shdr->hdr->encrypted,
   1518 	    shdr->hdr->encrypt));
   1519 	DPRINTFN(5, ("%s->", ether_sprintf(shdr->hdr->src_addr)));
   1520 	DPRINTFN(5, ("%s\n", ether_sprintf(shdr->hdr->dst_addr)));
   1521 
   1522 	bus_dmamap_sync(sc->sc_dmat, sc->tbd_map,
   1523 	    sc->txcur * sizeof (struct ipw_bd),
   1524 	    sizeof (struct ipw_bd), BUS_DMASYNC_PREWRITE);
   1525 
   1526 	sc->txfree--;
   1527 	sc->txcur = (sc->txcur + 1) % IPW_NTBD;
   1528 
   1529 	sbuf->m = m0;
   1530 	sbuf->ni = ni;
   1531 
   1532 	for (i = 0; i < sbuf->map->dm_nsegs; i++) {
   1533 		sbd = &sc->stbd_list[sc->txcur];
   1534 
   1535 		sbd->bd->physaddr = htole32(sbuf->map->dm_segs[i].ds_addr);
   1536 		sbd->bd->len = htole32(sbuf->map->dm_segs[i].ds_len);
   1537 		sbd->bd->nfrag = 0;
   1538 		sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3;
   1539 		if (i == sbuf->map->dm_nsegs - 1) {
   1540 			sbd->type = IPW_SBD_TYPE_DATA;
   1541 			sbd->priv = sbuf;
   1542 			sbd->bd->flags |= IPW_BD_FLAG_TX_LAST_FRAGMENT;
   1543 		} else {
   1544 			sbd->type = IPW_SBD_TYPE_NOASSOC;
   1545 			sbd->bd->flags |= IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
   1546 		}
   1547 
   1548 		DPRINTFN(5, ("sending fragment (%d, %d)\n", i,
   1549 		    (int)sbuf->map->dm_segs[i].ds_len));
   1550 
   1551 		bus_dmamap_sync(sc->sc_dmat, sc->tbd_map,
   1552 		    sc->txcur * sizeof (struct ipw_bd),
   1553 		    sizeof (struct ipw_bd), BUS_DMASYNC_PREWRITE);
   1554 
   1555 		sc->txfree--;
   1556 		sc->txcur = (sc->txcur + 1) % IPW_NTBD;
   1557 	}
   1558 
   1559 	bus_dmamap_sync(sc->sc_dmat, sc->hdr_map, shdr->offset,
   1560 	    sizeof (struct ipw_hdr), BUS_DMASYNC_PREWRITE);
   1561 
   1562 	bus_dmamap_sync(sc->sc_dmat, sbuf->map, 0, MCLBYTES,
   1563 	    BUS_DMASYNC_PREWRITE);
   1564 
   1565 	/* Inform firmware about this new packet */
   1566 	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
   1567 
   1568 	return 0;
   1569 }
   1570 
   1571 static void
   1572 ipw_start(struct ifnet *ifp)
   1573 {
   1574 	struct ipw_softc *sc = ifp->if_softc;
   1575 	struct ieee80211com *ic = &sc->sc_ic;
   1576 	struct mbuf *m0;
   1577 	struct ether_header *eh;
   1578 	struct ieee80211_node *ni;
   1579 
   1580 
   1581 	if (ic->ic_state != IEEE80211_S_RUN)
   1582 		return;
   1583 
   1584 	for (;;) {
   1585 		IF_DEQUEUE(&ifp->if_snd, m0);
   1586 		if (m0 == NULL)
   1587 			break;
   1588 
   1589 		if (sc->txfree < 1 + IPW_MAX_NSEG) {
   1590 			IF_PREPEND(&ifp->if_snd, m0);
   1591 			ifp->if_flags |= IFF_OACTIVE;
   1592 			break;
   1593 		}
   1594 
   1595 		if (m0->m_len < sizeof (struct ether_header) &&
   1596 		    (m0 = m_pullup(m0, sizeof (struct ether_header))) == NULL)
   1597 			continue;
   1598 
   1599 		eh = mtod(m0, struct ether_header *);
   1600 		ni = ieee80211_find_txnode(ic, eh->ether_dhost);
   1601 		if (ni == NULL) {
   1602 			m_freem(m0);
   1603 			continue;
   1604 		}
   1605 
   1606 #if NBPFILTER > 0
   1607 		if (ifp->if_bpf != NULL)
   1608 			bpf_mtap(ifp->if_bpf, m0);
   1609 #endif
   1610 
   1611 		m0 = ieee80211_encap(ic, m0, ni);
   1612 		if (m0 == NULL) {
   1613 			ieee80211_free_node(ni);
   1614 			continue;
   1615 		}
   1616 
   1617 #if NBPFILTER > 0
   1618 		if (ic->ic_rawbpf != NULL)
   1619 			bpf_mtap(ic->ic_rawbpf, m0);
   1620 #endif
   1621 
   1622 		if (ipw_tx_start(ifp, m0, ni) != 0) {
   1623 			ieee80211_free_node(ni);
   1624 			ifp->if_oerrors++;
   1625 			break;
   1626 		}
   1627 
   1628 		/* start watchdog timer */
   1629 		sc->sc_tx_timer = 5;
   1630 		ifp->if_timer = 1;
   1631 	}
   1632 }
   1633 
   1634 static void
   1635 ipw_watchdog(struct ifnet *ifp)
   1636 {
   1637 	struct ipw_softc *sc = ifp->if_softc;
   1638 
   1639 	ifp->if_timer = 0;
   1640 
   1641 	if (sc->sc_tx_timer > 0) {
   1642 		if (--sc->sc_tx_timer == 0) {
   1643 			aprint_error("%s: device timeout\n",
   1644 			    sc->sc_dev.dv_xname);
   1645 			ifp->if_oerrors++;
   1646 			ifp->if_flags &= ~IFF_UP;
   1647 			ipw_stop(ifp, 1);
   1648 			return;
   1649 		}
   1650 		ifp->if_timer = 1;
   1651 	}
   1652 
   1653 	ieee80211_watchdog(&sc->sc_ic);
   1654 }
   1655 
   1656 static int
   1657 ipw_get_table1(struct ipw_softc *sc, uint32_t *tbl)
   1658 {
   1659 	uint32_t addr, size, i;
   1660 
   1661 	if (!(sc->flags & IPW_FLAG_FW_INITED))
   1662 		return ENOTTY;
   1663 
   1664 	CSR_WRITE_4(sc, IPW_CSR_AUTOINC_ADDR, sc->table1_base);
   1665 
   1666 	size = CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA);
   1667 	if (suword(tbl, size) != 0)
   1668 		return EFAULT;
   1669 
   1670 	for (i = 1, ++tbl; i < size; i++, tbl++) {
   1671 		addr = CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA);
   1672 		if (suword(tbl, MEM_READ_4(sc, addr)) != 0)
   1673 			return EFAULT;
   1674 	}
   1675 	return 0;
   1676 }
   1677 
   1678 static int
   1679 ipw_get_radio(struct ipw_softc *sc, int *ret)
   1680 {
   1681 	uint32_t addr;
   1682 
   1683 	if (!(sc->flags & IPW_FLAG_FW_INITED))
   1684 		return ENOTTY;
   1685 
   1686 	addr = ipw_read_table1(sc, IPW_INFO_EEPROM_ADDRESS);
   1687 	if ((MEM_READ_4(sc, addr + 32) >> 24) & 1) {
   1688 		suword(ret, -1);
   1689 		return 0;
   1690 	}
   1691 
   1692 	if (CSR_READ_4(sc, IPW_CSR_IO) & IPW_IO_RADIO_DISABLED)
   1693 		suword(ret, 0);
   1694 	else
   1695 		suword(ret, 1);
   1696 
   1697 	return 0;
   1698 }
   1699 
   1700 static int
   1701 ipw_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
   1702 {
   1703 #define	IS_RUNNING(ifp) \
   1704 	((ifp->if_flags & IFF_UP) && (ifp->if_flags & IFF_RUNNING))
   1705 
   1706 	struct ipw_softc *sc = ifp->if_softc;
   1707 	struct ieee80211com *ic = &sc->sc_ic;
   1708 	struct ifreq *ifr = (struct ifreq *)data;
   1709 	int s, error = 0;
   1710 
   1711 	s = splnet();
   1712 
   1713 	switch (cmd) {
   1714 	case SIOCSIFFLAGS:
   1715 		if (ifp->if_flags & IFF_UP) {
   1716 			if (!(ifp->if_flags & IFF_RUNNING))
   1717 				ipw_init(ifp);
   1718 		} else {
   1719 			if (ifp->if_flags & IFF_RUNNING)
   1720 				ipw_stop(ifp, 1);
   1721 		}
   1722 		break;
   1723 
   1724 	case SIOCADDMULTI:
   1725 	case SIOCDELMULTI:
   1726 		error = (cmd == SIOCADDMULTI) ?
   1727 		    ether_addmulti(ifr, &sc->sc_ec) :
   1728 		    ether_delmulti(ifr, &sc->sc_ec);
   1729 		if (error == ENETRESET) {
   1730 			/* setup multicast filter, etc */
   1731 			error = 0;
   1732 		}
   1733 		break;
   1734 
   1735 	case SIOCGTABLE1:
   1736 		error = ipw_get_table1(sc, (uint32_t *)ifr->ifr_data);
   1737 		break;
   1738 
   1739 	case SIOCGRADIO:
   1740 		error = ipw_get_radio(sc, (int *)ifr->ifr_data);
   1741 		break;
   1742 
   1743 	case SIOCSIFMEDIA:
   1744 		if (ifr->ifr_media & IFM_IEEE80211_ADHOC)
   1745 			strlcpy(sc->sc_fwname, "ipw2100-1.2-i.fw",
   1746 			    sizeof(sc->sc_fwname));
   1747 		else if (ifr->ifr_media & IFM_IEEE80211_MONITOR)
   1748 			strlcpy(sc->sc_fwname, "ipw2100-1.2-p.fw",
   1749 			    sizeof(sc->sc_fwname));
   1750 		else
   1751 			strlcpy(sc->sc_fwname, "ipw2100-1.2.fw",
   1752 			    sizeof(sc->sc_fwname));
   1753 
   1754 		ipw_free_firmware(sc);
   1755 		/* FALLTRHOUGH */
   1756 	default:
   1757 		error = ieee80211_ioctl(&sc->sc_ic, cmd, data);
   1758 		if (error != ENETRESET)
   1759 			break;
   1760 
   1761 		if (error == ENETRESET) {
   1762 			if (IS_RUNNING(ifp) &&
   1763 			    (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
   1764 				ipw_init(ifp);
   1765 			error = 0;
   1766 		}
   1767 
   1768 	}
   1769 
   1770 	splx(s);
   1771 	return error;
   1772 #undef IS_RUNNING
   1773 }
   1774 
   1775 static uint32_t
   1776 ipw_read_table1(struct ipw_softc *sc, uint32_t off)
   1777 {
   1778 	return MEM_READ_4(sc, MEM_READ_4(sc, sc->table1_base + off));
   1779 }
   1780 
   1781 static void
   1782 ipw_write_table1(struct ipw_softc *sc, uint32_t off, uint32_t info)
   1783 {
   1784 	MEM_WRITE_4(sc, MEM_READ_4(sc, sc->table1_base + off), info);
   1785 }
   1786 
   1787 static int
   1788 ipw_read_table2(struct ipw_softc *sc, uint32_t off, void *buf, uint32_t *len)
   1789 {
   1790 	uint32_t addr, info;
   1791 	uint16_t count, size;
   1792 	uint32_t total;
   1793 
   1794 	/* addr[4] + count[2] + size[2] */
   1795 	addr = MEM_READ_4(sc, sc->table2_base + off);
   1796 	info = MEM_READ_4(sc, sc->table2_base + off + 4);
   1797 
   1798 	count = info >> 16;
   1799 	size = info & 0xffff;
   1800 	total = count * size;
   1801 
   1802 	if (total > *len) {
   1803 		*len = total;
   1804 		return EINVAL;
   1805 	}
   1806 
   1807 	*len = total;
   1808 	ipw_read_mem_1(sc, addr, buf, total);
   1809 
   1810 	return 0;
   1811 }
   1812 
   1813 static void
   1814 ipw_stop_master(struct ipw_softc *sc)
   1815 {
   1816 	int ntries;
   1817 
   1818 	/* disable interrupts */
   1819 	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
   1820 
   1821 	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_STOP_MASTER);
   1822 	for (ntries = 0; ntries < 50; ntries++) {
   1823 		if (CSR_READ_4(sc, IPW_CSR_RST) & IPW_RST_MASTER_DISABLED)
   1824 			break;
   1825 		DELAY(10);
   1826 	}
   1827 	if (ntries == 50)
   1828 		aprint_error("%s: timeout waiting for master\n",
   1829 		    sc->sc_dev.dv_xname);
   1830 
   1831 	CSR_WRITE_4(sc, IPW_CSR_RST, CSR_READ_4(sc, IPW_CSR_RST) |
   1832 	    IPW_RST_PRINCETON_RESET);
   1833 
   1834 	sc->flags &= ~IPW_FLAG_FW_INITED;
   1835 }
   1836 
   1837 static int
   1838 ipw_reset(struct ipw_softc *sc)
   1839 {
   1840 	int ntries;
   1841 
   1842 	ipw_stop_master(sc);
   1843 
   1844 	/* move adapter to D0 state */
   1845 	CSR_WRITE_4(sc, IPW_CSR_CTL, CSR_READ_4(sc, IPW_CSR_CTL) |
   1846 	    IPW_CTL_INIT);
   1847 
   1848 	/* wait for clock stabilization */
   1849 	for (ntries = 0; ntries < 1000; ntries++) {
   1850 		if (CSR_READ_4(sc, IPW_CSR_CTL) & IPW_CTL_CLOCK_READY)
   1851 			break;
   1852 		DELAY(200);
   1853 	}
   1854 	if (ntries == 1000)
   1855 		return EIO;
   1856 
   1857 	CSR_WRITE_4(sc, IPW_CSR_RST, CSR_READ_4(sc, IPW_CSR_RST) |
   1858 	    IPW_RST_SW_RESET);
   1859 
   1860 	DELAY(10);
   1861 
   1862 	CSR_WRITE_4(sc, IPW_CSR_CTL, CSR_READ_4(sc, IPW_CSR_CTL) |
   1863 	    IPW_CTL_INIT);
   1864 
   1865 	return 0;
   1866 }
   1867 
   1868 /*
   1869  * Upload the microcode to the device.
   1870  */
   1871 static int
   1872 ipw_load_ucode(struct ipw_softc *sc, u_char *uc, int size)
   1873 {
   1874 	int ntries;
   1875 
   1876 	MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
   1877 	CSR_WRITE_4(sc, IPW_CSR_RST, 0);
   1878 
   1879 	MEM_WRITE_2(sc, 0x220000, 0x0703);
   1880 	MEM_WRITE_2(sc, 0x220000, 0x0707);
   1881 
   1882 	MEM_WRITE_1(sc, 0x210014, 0x72);
   1883 	MEM_WRITE_1(sc, 0x210014, 0x72);
   1884 
   1885 	MEM_WRITE_1(sc, 0x210000, 0x40);
   1886 	MEM_WRITE_1(sc, 0x210000, 0x00);
   1887 	MEM_WRITE_1(sc, 0x210000, 0x40);
   1888 
   1889 	MEM_WRITE_MULTI_1(sc, 0x210010, uc, size);
   1890 
   1891 	MEM_WRITE_1(sc, 0x210000, 0x00);
   1892 	MEM_WRITE_1(sc, 0x210000, 0x00);
   1893 	MEM_WRITE_1(sc, 0x210000, 0x80);
   1894 
   1895 	MEM_WRITE_2(sc, 0x220000, 0x0703);
   1896 	MEM_WRITE_2(sc, 0x220000, 0x0707);
   1897 
   1898 	MEM_WRITE_1(sc, 0x210014, 0x72);
   1899 	MEM_WRITE_1(sc, 0x210014, 0x72);
   1900 
   1901 	MEM_WRITE_1(sc, 0x210000, 0x00);
   1902 	MEM_WRITE_1(sc, 0x210000, 0x80);
   1903 
   1904 	for (ntries = 0; ntries < 10; ntries++) {
   1905 		if (MEM_READ_1(sc, 0x210000) & 1)
   1906 			break;
   1907 		DELAY(10);
   1908 	}
   1909 	if (ntries == 10) {
   1910 		aprint_error("%s: timeout waiting for ucode to initialize\n",
   1911 		    sc->sc_dev.dv_xname);
   1912 		return EIO;
   1913 	}
   1914 
   1915 	MEM_WRITE_4(sc, 0x3000e0, 0);
   1916 
   1917 	return 0;
   1918 }
   1919 
   1920 /* set of macros to handle unaligned little endian data in firmware image */
   1921 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
   1922 #define GETLE16(p) ((p)[0] | (p)[1] << 8)
   1923 static int
   1924 ipw_load_firmware(struct ipw_softc *sc, u_char *fw, int size)
   1925 {
   1926 	u_char *p, *end;
   1927 	uint32_t dst;
   1928 	uint16_t len;
   1929 	int error;
   1930 
   1931 	p = fw;
   1932 	end = fw + size;
   1933 	while (p < end) {
   1934 		dst = GETLE32(p); p += 4;
   1935 		len = GETLE16(p); p += 2;
   1936 
   1937 		ipw_write_mem_1(sc, dst, p, len);
   1938 		p += len;
   1939 	}
   1940 
   1941 	CSR_WRITE_4(sc, IPW_CSR_IO, IPW_IO_GPIO1_ENABLE | IPW_IO_GPIO3_MASK |
   1942 	    IPW_IO_LED_OFF);
   1943 
   1944 	/* enable interrupts */
   1945 	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
   1946 
   1947 	/* kick the firmware */
   1948 	CSR_WRITE_4(sc, IPW_CSR_RST, 0);
   1949 
   1950 	CSR_WRITE_4(sc, IPW_CSR_CTL, CSR_READ_4(sc, IPW_CSR_CTL) |
   1951 	    IPW_CTL_ALLOW_STANDBY);
   1952 
   1953 	/* wait at most one second for firmware initialization to complete */
   1954 	if ((error = tsleep(sc, 0, "ipwinit", hz)) != 0) {
   1955 		aprint_error("%s: timeout waiting for firmware initialization "
   1956 		    "to complete\n", sc->sc_dev.dv_xname);
   1957 		return error;
   1958 	}
   1959 
   1960 	CSR_WRITE_4(sc, IPW_CSR_IO, CSR_READ_4(sc, IPW_CSR_IO) |
   1961 	    IPW_IO_GPIO1_MASK | IPW_IO_GPIO3_MASK);
   1962 
   1963 	return 0;
   1964 }
   1965 
   1966 /*
   1967  * Store firmware into kernel memory so we can download it when we need to,
   1968  * e.g when the adapter wakes up from suspend mode.
   1969  */
   1970 static int
   1971 ipw_cache_firmware(struct ipw_softc *sc)
   1972 {
   1973 	struct ipw_firmware *fw = &sc->fw;
   1974 	struct ipw_firmware_hdr hdr;
   1975 	firmware_handle_t fwh;
   1976 	off_t fwsz, p;
   1977 	int error;
   1978 
   1979 	ipw_free_firmware(sc);
   1980 
   1981 	if ((error = firmware_open("if_ipw", sc->sc_fwname, &fwh)) != 0)
   1982 		goto fail0;
   1983 
   1984 	fwsz = firmware_get_size(fwh);
   1985 
   1986 	if (fwsz < sizeof(hdr))
   1987 		goto fail2;
   1988 
   1989 	if ((error = firmware_read(fwh, 0, &hdr, sizeof(hdr))) != 0)
   1990 		goto fail2;
   1991 
   1992 	fw->main_size  = le32toh(hdr.main_size);
   1993 	fw->ucode_size = le32toh(hdr.ucode_size);
   1994 
   1995 	fw->main = firmware_malloc(fw->main_size);
   1996 	if (fw->main == NULL) {
   1997 		error = ENOMEM;
   1998 		goto fail1;
   1999 	}
   2000 
   2001 	fw->ucode = firmware_malloc(fw->ucode_size);
   2002 	if (fw->ucode == NULL) {
   2003 		error = ENOMEM;
   2004 		goto fail2;
   2005 	}
   2006 
   2007 	p = sizeof(hdr);
   2008 	if ((error = firmware_read(fwh, p, fw->main, fw->main_size)) != 0)
   2009 		goto fail3;
   2010 
   2011 	if ((error = firmware_read(fwh, p, fw->main, fw->main_size)) != 0)
   2012 		goto fail3;
   2013 
   2014 	p += fw->main_size;
   2015 	if ((error = firmware_read(fwh, p, fw->ucode, fw->ucode_size)) != 0)
   2016 		goto fail3;
   2017 
   2018 	DPRINTF(("Firmware cached: main %u, ucode %u\n", fw->main_size,
   2019 	    fw->ucode_size));
   2020 
   2021 	sc->flags |= IPW_FLAG_FW_CACHED;
   2022 
   2023 	return 0;
   2024 
   2025 fail3:	firmware_free(fw->ucode, 0);
   2026 fail2:	firmware_free(fw->main, 0);
   2027 fail1:  firmware_close(fwh);
   2028 fail0:
   2029 	return error;
   2030 }
   2031 
   2032 static void
   2033 ipw_free_firmware(struct ipw_softc *sc)
   2034 {
   2035 	if (!(sc->flags & IPW_FLAG_FW_CACHED))
   2036 		return;
   2037 
   2038 	firmware_free(sc->fw.main, 0);
   2039 	firmware_free(sc->fw.ucode, 0);
   2040 
   2041 	sc->flags &= ~IPW_FLAG_FW_CACHED;
   2042 }
   2043 
   2044 static int
   2045 ipw_config(struct ipw_softc *sc)
   2046 {
   2047 	struct ieee80211com *ic = &sc->sc_ic;
   2048 	struct ifnet *ifp = &sc->sc_if;
   2049 	struct ipw_security security;
   2050 	struct ieee80211_key *k;
   2051 	struct ipw_wep_key wepkey;
   2052 	struct ipw_scan_options options;
   2053 	struct ipw_configuration config;
   2054 	uint32_t data;
   2055 	int error, i;
   2056 
   2057 	switch (ic->ic_opmode) {
   2058 	case IEEE80211_M_STA:
   2059 	case IEEE80211_M_HOSTAP:
   2060 		data = htole32(IPW_MODE_BSS);
   2061 		break;
   2062 
   2063 	case IEEE80211_M_IBSS:
   2064 	case IEEE80211_M_AHDEMO:
   2065 		data = htole32(IPW_MODE_IBSS);
   2066 		break;
   2067 
   2068 	case IEEE80211_M_MONITOR:
   2069 		data = htole32(IPW_MODE_MONITOR);
   2070 		break;
   2071 	}
   2072 	DPRINTF(("Setting mode to %u\n", le32toh(data)));
   2073 	error = ipw_cmd(sc, IPW_CMD_SET_MODE, &data, sizeof data);
   2074 	if (error != 0)
   2075 		return error;
   2076 
   2077 	if (ic->ic_opmode == IEEE80211_M_IBSS ||
   2078 	    ic->ic_opmode == IEEE80211_M_MONITOR) {
   2079 		data = htole32(ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
   2080 		DPRINTF(("Setting channel to %u\n", le32toh(data)));
   2081 		error = ipw_cmd(sc, IPW_CMD_SET_CHANNEL, &data, sizeof data);
   2082 		if (error != 0)
   2083 			return error;
   2084 	}
   2085 
   2086 	if (ic->ic_opmode == IEEE80211_M_MONITOR) {
   2087 		DPRINTF(("Enabling adapter\n"));
   2088 		return ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
   2089 	}
   2090 
   2091 	DPRINTF(("Setting MAC to %s\n", ether_sprintf(ic->ic_myaddr)));
   2092 	IEEE80211_ADDR_COPY(LLADDR(ifp->if_sadl), ic->ic_myaddr);
   2093 	error = ipw_cmd(sc, IPW_CMD_SET_MAC_ADDRESS, ic->ic_myaddr,
   2094 	    IEEE80211_ADDR_LEN);
   2095 	if (error != 0)
   2096 		return error;
   2097 
   2098 	config.flags = htole32(IPW_CFG_BSS_MASK | IPW_CFG_IBSS_MASK |
   2099 	    IPW_CFG_PREAMBLE_AUTO | IPW_CFG_802_1x_ENABLE);
   2100 
   2101 	if (ic->ic_opmode == IEEE80211_M_IBSS)
   2102 		config.flags |= htole32(IPW_CFG_IBSS_AUTO_START);
   2103 	if (ifp->if_flags & IFF_PROMISC)
   2104 		config.flags |= htole32(IPW_CFG_PROMISCUOUS);
   2105 	config.bss_chan = htole32(0x3fff); /* channels 1-14 */
   2106 	config.ibss_chan = htole32(0x7ff); /* channels 1-11 */
   2107 	DPRINTF(("Setting adapter configuration 0x%08x\n", config.flags));
   2108 	error = ipw_cmd(sc, IPW_CMD_SET_CONFIGURATION, &config, sizeof config);
   2109 	if (error != 0)
   2110 		return error;
   2111 
   2112 	data = htole32(0x3); /* 1, 2 */
   2113 	DPRINTF(("Setting basic tx rates to 0x%x\n", le32toh(data)));
   2114 	error = ipw_cmd(sc, IPW_CMD_SET_BASIC_TX_RATES, &data, sizeof data);
   2115 	if (error != 0)
   2116 		return error;
   2117 
   2118 	data = htole32(0xf); /* 1, 2, 5.5, 11 */
   2119 	DPRINTF(("Setting tx rates to 0x%x\n", le32toh(data)));
   2120 	error = ipw_cmd(sc, IPW_CMD_SET_TX_RATES, &data, sizeof data);
   2121 	if (error != 0)
   2122 		return error;
   2123 
   2124 	data = htole32(IPW_POWER_MODE_CAM);
   2125 	DPRINTF(("Setting power mode to %u\n", le32toh(data)));
   2126 	error = ipw_cmd(sc, IPW_CMD_SET_POWER_MODE, &data, sizeof data);
   2127 	if (error != 0)
   2128 		return error;
   2129 
   2130 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
   2131 		data = htole32(32); /* default value */
   2132 		DPRINTF(("Setting tx power index to %u\n", le32toh(data)));
   2133 		error = ipw_cmd(sc, IPW_CMD_SET_TX_POWER_INDEX, &data,
   2134 		    sizeof data);
   2135 		if (error != 0)
   2136 			return error;
   2137 	}
   2138 
   2139 	data = htole32(ic->ic_rtsthreshold);
   2140 	DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
   2141 	error = ipw_cmd(sc, IPW_CMD_SET_RTS_THRESHOLD, &data, sizeof data);
   2142 	if (error != 0)
   2143 		return error;
   2144 
   2145 	data = htole32(ic->ic_fragthreshold);
   2146 	DPRINTF(("Setting frag threshold to %u\n", le32toh(data)));
   2147 	error = ipw_cmd(sc, IPW_CMD_SET_FRAG_THRESHOLD, &data, sizeof data);
   2148 	if (error != 0)
   2149 		return error;
   2150 
   2151 #ifdef IPW_DEBUG
   2152 	if (ipw_debug > 0) {
   2153 		printf("Setting ESSID to ");
   2154 		ieee80211_print_essid(ic->ic_des_essid, ic->ic_des_esslen);
   2155 		printf("\n");
   2156 	}
   2157 #endif
   2158 	error = ipw_cmd(sc, IPW_CMD_SET_ESSID, ic->ic_des_essid,
   2159 	    ic->ic_des_esslen);
   2160 	if (error != 0)
   2161 		return error;
   2162 
   2163 	/* no mandatory BSSID */
   2164 	DPRINTF(("Setting mandatory BSSID to null\n"));
   2165 	error = ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID, NULL, 0);
   2166 	if (error != 0)
   2167 		return error;
   2168 
   2169 	if (ic->ic_flags & IEEE80211_F_DESBSSID) {
   2170 		DPRINTF(("Setting desired BSSID to %s\n",
   2171 		    ether_sprintf(ic->ic_des_bssid)));
   2172 		error = ipw_cmd(sc, IPW_CMD_SET_DESIRED_BSSID,
   2173 		    ic->ic_des_bssid, IEEE80211_ADDR_LEN);
   2174 		if (error != 0)
   2175 			return error;
   2176 	}
   2177 
   2178 	(void)memset(&security, 0, sizeof(security));
   2179 	security.authmode = (ic->ic_bss->ni_authmode == IEEE80211_AUTH_SHARED) ?
   2180 	    IPW_AUTH_SHARED : IPW_AUTH_OPEN;
   2181 	security.ciphers = htole32(IPW_CIPHER_NONE);
   2182 	DPRINTF(("Setting authmode to %u\n", security.authmode));
   2183 	error = ipw_cmd(sc, IPW_CMD_SET_SECURITY_INFORMATION, &security,
   2184 	    sizeof security);
   2185 	if (error != 0)
   2186 		return error;
   2187 
   2188 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
   2189 		k = ic->ic_crypto.cs_nw_keys;
   2190 		for (i = 0; i < IEEE80211_WEP_NKID; i++, k++) {
   2191 			if (k->wk_keylen == 0)
   2192 				continue;
   2193 
   2194 			wepkey.idx = i;
   2195 			wepkey.len = k->wk_keylen;
   2196 			bzero(wepkey.key, sizeof wepkey.key);
   2197 			bcopy(k->wk_key, wepkey.key, k->wk_keylen);
   2198 			DPRINTF(("Setting wep key index %u len %u\n",
   2199 			    wepkey.idx, wepkey.len));
   2200 			error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY, &wepkey,
   2201 			    sizeof wepkey);
   2202 			if (error != 0)
   2203 				return error;
   2204 		}
   2205 
   2206 		data = htole32(ic->ic_crypto.cs_def_txkey);
   2207 		DPRINTF(("Setting tx key index to %u\n", le32toh(data)));
   2208 		error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY_INDEX, &data,
   2209 		    sizeof data);
   2210 		if (error != 0)
   2211 			return error;
   2212 	}
   2213 
   2214 	data = htole32((sc->sc_ic.ic_flags & IEEE80211_F_PRIVACY) ? IPW_WEPON : 0);
   2215 	DPRINTF(("Setting wep flags to 0x%x\n", le32toh(data)));
   2216 	error = ipw_cmd(sc, IPW_CMD_SET_WEP_FLAGS, &data, sizeof data);
   2217 	if (error != 0)
   2218 		return error;
   2219 
   2220 #if 0
   2221 	struct ipw_wpa_ie ie;
   2222 
   2223 	bzero(&ie, sizeof ie);
   2224 	ie.len = htole32(sizeof (struct ieee80211_ie_wpa));
   2225 	DPRINTF(("Setting wpa ie\n"));
   2226 	error = ipw_cmd(sc, IPW_CMD_SET_WPA_IE, &ie, sizeof ie);
   2227 	if (error != 0)
   2228 		return error;
   2229 #endif
   2230 
   2231 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
   2232 		data = htole32(ic->ic_bintval);
   2233 		DPRINTF(("Setting beacon interval to %u\n", le32toh(data)));
   2234 		error = ipw_cmd(sc, IPW_CMD_SET_BEACON_INTERVAL, &data,
   2235 		    sizeof data);
   2236 		if (error != 0)
   2237 			return error;
   2238 	}
   2239 
   2240 	options.flags = 0;
   2241 	options.channels = htole32(0x3fff); /* scan channels 1-14 */
   2242 	DPRINTF(("Setting scan options to 0x%x\n", le32toh(options.flags)));
   2243 	error = ipw_cmd(sc, IPW_CMD_SET_SCAN_OPTIONS, &options, sizeof options);
   2244 	if (error != 0)
   2245 		return error;
   2246 
   2247 	/* finally, enable adapter (start scanning for an access point) */
   2248 	DPRINTF(("Enabling adapter\n"));
   2249 	return ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
   2250 }
   2251 
   2252 static int
   2253 ipw_init(struct ifnet *ifp)
   2254 {
   2255 	struct ipw_softc *sc = ifp->if_softc;
   2256 	struct ipw_firmware *fw = &sc->fw;
   2257 
   2258 	if (!(sc->flags & IPW_FLAG_FW_CACHED)) {
   2259 		if (ipw_cache_firmware(sc) != 0) {
   2260 			aprint_error("%s: could not cache the firmware (%s)\n",
   2261 			    sc->sc_dev.dv_xname, sc->sc_fwname);
   2262 			goto fail;
   2263 		}
   2264 	}
   2265 
   2266 	ipw_stop(ifp, 0);
   2267 
   2268 	if (ipw_reset(sc) != 0) {
   2269 		aprint_error("%s: could not reset adapter\n",
   2270 		    sc->sc_dev.dv_xname);
   2271 		goto fail;
   2272 	}
   2273 
   2274 	if (ipw_load_ucode(sc, fw->ucode, fw->ucode_size) != 0) {
   2275 		aprint_error("%s: could not load microcode\n",
   2276 		    sc->sc_dev.dv_xname);
   2277 		goto fail;
   2278 	}
   2279 
   2280 	ipw_stop_master(sc);
   2281 
   2282 	/*
   2283 	 * Setup tx, rx and status rings.
   2284 	 */
   2285 	sc->txold = IPW_NTBD - 1;
   2286 	sc->txcur = 0;
   2287 	sc->txfree = IPW_NTBD - 2;
   2288 	sc->rxcur = IPW_NRBD - 1;
   2289 
   2290 	CSR_WRITE_4(sc, IPW_CSR_TX_BASE,  sc->tbd_map->dm_segs[0].ds_addr);
   2291 	CSR_WRITE_4(sc, IPW_CSR_TX_SIZE,  IPW_NTBD);
   2292 	CSR_WRITE_4(sc, IPW_CSR_TX_READ,  0);
   2293 	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
   2294 
   2295 	CSR_WRITE_4(sc, IPW_CSR_RX_BASE,  sc->rbd_map->dm_segs[0].ds_addr);
   2296 	CSR_WRITE_4(sc, IPW_CSR_RX_SIZE,  IPW_NRBD);
   2297 	CSR_WRITE_4(sc, IPW_CSR_RX_READ,  0);
   2298 	CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
   2299 
   2300 	CSR_WRITE_4(sc, IPW_CSR_STATUS_BASE, sc->status_map->dm_segs[0].ds_addr);
   2301 
   2302 	if (ipw_load_firmware(sc, fw->main, fw->main_size) != 0) {
   2303 		aprint_error("%s: could not load firmware\n",
   2304 		    sc->sc_dev.dv_xname);
   2305 		goto fail;
   2306 	}
   2307 
   2308 	sc->flags |= IPW_FLAG_FW_INITED;
   2309 
   2310 	/* retrieve information tables base addresses */
   2311 	sc->table1_base = CSR_READ_4(sc, IPW_CSR_TABLE1_BASE);
   2312 	sc->table2_base = CSR_READ_4(sc, IPW_CSR_TABLE2_BASE);
   2313 
   2314 	ipw_write_table1(sc, IPW_INFO_LOCK, 0);
   2315 
   2316 	if (ipw_config(sc) != 0) {
   2317 		aprint_error("%s: device configuration failed\n",
   2318 		    sc->sc_dev.dv_xname);
   2319 		goto fail;
   2320 	}
   2321 
   2322 	ifp->if_flags &= ~IFF_OACTIVE;
   2323 	ifp->if_flags |= IFF_RUNNING;
   2324 
   2325 	return 0;
   2326 
   2327 fail:	ifp->if_flags &= ~IFF_UP;
   2328 	ipw_stop(ifp, 0);
   2329 
   2330 	return EIO;
   2331 }
   2332 
   2333 static void
   2334 ipw_stop(struct ifnet *ifp, int disable)
   2335 {
   2336 	struct ipw_softc *sc = ifp->if_softc;
   2337 	struct ieee80211com *ic = &sc->sc_ic;
   2338 	int i;
   2339 
   2340 	ipw_stop_master(sc);
   2341 
   2342 	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_SW_RESET);
   2343 
   2344 	/*
   2345 	 * Release tx buffers.
   2346 	 */
   2347 	for (i = 0; i < IPW_NTBD; i++)
   2348 		ipw_release_sbd(sc, &sc->stbd_list[i]);
   2349 
   2350 	sc->sc_tx_timer = 0;
   2351 	ifp->if_timer = 0;
   2352 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   2353 
   2354 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
   2355 }
   2356 
   2357 static void
   2358 ipw_read_mem_1(struct ipw_softc *sc, bus_size_t offset, uint8_t *datap,
   2359     bus_size_t count)
   2360 {
   2361 	for (; count > 0; offset++, datap++, count--) {
   2362 		CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
   2363 		*datap = CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3));
   2364 	}
   2365 }
   2366 
   2367 static void
   2368 ipw_write_mem_1(struct ipw_softc *sc, bus_size_t offset, uint8_t *datap,
   2369     bus_size_t count)
   2370 {
   2371 	for (; count > 0; offset++, datap++, count--) {
   2372 		CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
   2373 		CSR_WRITE_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3), *datap);
   2374 	}
   2375 }
   2376