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