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