Home | History | Annotate | Line # | Download | only in pci
if_ipw.c revision 1.8.4.1
      1  1.8.4.1   kent /*	$NetBSD: if_ipw.c,v 1.8.4.1 2005/04/29 11:29:06 kent Exp $	*/
      2      1.1  lukem 
      3      1.1  lukem /*-
      4      1.1  lukem  * Copyright (c) 2004
      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.8.4.1   kent __KERNEL_RCSID(0, "$NetBSD: if_ipw.c,v 1.8.4.1 2005/04/29 11:29:06 kent Exp $");
     32      1.1  lukem 
     33      1.1  lukem /*-
     34      1.1  lukem  * Intel(R) PRO/Wireless 2100 MiniPCI driver
     35      1.1  lukem  * http://www.intel.com/products/mobiletechnology/prowireless.htm
     36      1.1  lukem  */
     37      1.1  lukem 
     38      1.1  lukem #include "bpfilter.h"
     39      1.1  lukem 
     40      1.1  lukem #include <sys/param.h>
     41      1.1  lukem #include <sys/sockio.h>
     42      1.1  lukem #include <sys/sysctl.h>
     43      1.1  lukem #include <sys/mbuf.h>
     44      1.1  lukem #include <sys/kernel.h>
     45      1.1  lukem #include <sys/socket.h>
     46      1.1  lukem #include <sys/systm.h>
     47      1.1  lukem #include <sys/malloc.h>
     48      1.1  lukem #include <sys/conf.h>
     49      1.1  lukem 
     50      1.1  lukem #include <machine/bus.h>
     51      1.1  lukem #include <machine/endian.h>
     52      1.1  lukem #include <machine/intr.h>
     53      1.1  lukem 
     54      1.1  lukem #include <dev/pci/pcireg.h>
     55      1.1  lukem #include <dev/pci/pcivar.h>
     56      1.1  lukem #include <dev/pci/pcidevs.h>
     57      1.1  lukem 
     58      1.1  lukem #if NBPFILTER > 0
     59      1.1  lukem #include <net/bpf.h>
     60      1.1  lukem #endif
     61      1.1  lukem #include <net/if.h>
     62      1.1  lukem #include <net/if_arp.h>
     63      1.1  lukem #include <net/if_dl.h>
     64      1.1  lukem #include <net/if_ether.h>
     65      1.1  lukem #include <net/if_media.h>
     66      1.1  lukem #include <net/if_types.h>
     67      1.1  lukem 
     68      1.1  lukem #include <net80211/ieee80211_var.h>
     69      1.4  lukem #include <net80211/ieee80211_radiotap.h>
     70      1.1  lukem 
     71      1.1  lukem #include <netinet/in.h>
     72      1.1  lukem #include <netinet/in_systm.h>
     73      1.1  lukem #include <netinet/in_var.h>
     74      1.1  lukem #include <netinet/ip.h>
     75      1.1  lukem 
     76      1.3  lukem #include <dev/pci/if_ipwreg.h>
     77      1.3  lukem #include <dev/pci/if_ipwvar.h>
     78      1.1  lukem 
     79      1.1  lukem static int ipw_match(struct device *, struct cfdata *, void *);
     80      1.1  lukem static void ipw_attach(struct device *, struct device *, void *);
     81      1.1  lukem static int ipw_detach(struct device *, int);
     82      1.1  lukem static int ipw_media_change(struct ifnet *);
     83      1.1  lukem static int ipw_newstate(struct ieee80211com *, enum ieee80211_state, int);
     84      1.1  lukem static void ipw_command_intr(struct ipw_softc *, struct ipw_soft_buf *);
     85      1.1  lukem static void ipw_newstate_intr(struct ipw_softc *, struct ipw_soft_buf *);
     86      1.8  lukem static void ipw_data_intr(struct ipw_softc *, struct ipw_status *,
     87      1.1  lukem     struct ipw_soft_bd *, struct ipw_soft_buf *);
     88      1.1  lukem static void ipw_notification_intr(struct ipw_softc *, struct ipw_soft_buf *);
     89      1.1  lukem static void ipw_rx_intr(struct ipw_softc *);
     90      1.1  lukem static void ipw_release_sbd(struct ipw_softc *, struct ipw_soft_bd *);
     91      1.1  lukem static void ipw_tx_intr(struct ipw_softc *);
     92      1.1  lukem static int ipw_intr(void *);
     93      1.1  lukem static int ipw_cmd(struct ipw_softc *, u_int32_t, void *, u_int32_t);
     94      1.1  lukem static int ipw_tx_start(struct ifnet *, struct mbuf *, struct ieee80211_node *);
     95      1.1  lukem static void ipw_start(struct ifnet *);
     96      1.1  lukem static void ipw_watchdog(struct ifnet *);
     97      1.1  lukem static int ipw_get_table1(struct ipw_softc *, u_int32_t *);
     98      1.1  lukem static int ipw_get_radio(struct ipw_softc *, int *);
     99      1.1  lukem static int ipw_ioctl(struct ifnet *, u_long, caddr_t);
    100      1.1  lukem static u_int32_t ipw_read_table1(struct ipw_softc *, u_int32_t);
    101      1.1  lukem static void ipw_write_table1(struct ipw_softc *, u_int32_t, u_int32_t);
    102      1.1  lukem static int ipw_read_table2(struct ipw_softc *, u_int32_t, void *, u_int32_t *);
    103      1.1  lukem static int ipw_tx_init(struct ipw_softc *);
    104      1.1  lukem static void ipw_tx_stop(struct ipw_softc *);
    105      1.1  lukem static int ipw_rx_init(struct ipw_softc *);
    106      1.1  lukem static void ipw_rx_stop(struct ipw_softc *);
    107      1.1  lukem static void ipw_reset(struct ipw_softc *);
    108      1.1  lukem static int ipw_clock_sync(struct ipw_softc *);
    109      1.1  lukem static int ipw_load_ucode(struct ipw_softc *, u_char *, int);
    110      1.1  lukem static int ipw_load_firmware(struct ipw_softc *, u_char *, int);
    111      1.1  lukem static int ipw_firmware_init(struct ipw_softc *, u_char *);
    112      1.1  lukem static int ipw_config(struct ipw_softc *);
    113      1.1  lukem static int ipw_init(struct ifnet *);
    114      1.1  lukem static void ipw_stop(struct ifnet *, int);
    115      1.8  lukem static void ipw_read_mem_1(struct ipw_softc *, bus_size_t, u_int8_t *,
    116      1.1  lukem     bus_size_t);
    117      1.8  lukem static void ipw_write_mem_1(struct ipw_softc *, bus_size_t, u_int8_t *,
    118      1.1  lukem     bus_size_t);
    119      1.1  lukem static void ipw_zero_mem_4(struct ipw_softc *, bus_size_t, bus_size_t);
    120      1.1  lukem 
    121      1.1  lukem static inline u_int8_t MEM_READ_1(struct ipw_softc *sc, u_int32_t addr)
    122      1.1  lukem {
    123      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, addr);
    124      1.1  lukem 	return CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA);
    125      1.1  lukem }
    126      1.1  lukem 
    127      1.1  lukem static inline u_int16_t MEM_READ_2(struct ipw_softc *sc, u_int32_t addr)
    128      1.1  lukem {
    129      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, addr);
    130      1.1  lukem 	return CSR_READ_2(sc, IPW_CSR_INDIRECT_DATA);
    131      1.1  lukem }
    132      1.1  lukem 
    133      1.1  lukem static inline u_int32_t MEM_READ_4(struct ipw_softc *sc, u_int32_t addr)
    134      1.1  lukem {
    135      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, addr);
    136      1.1  lukem 	return CSR_READ_4(sc, IPW_CSR_INDIRECT_DATA);
    137      1.1  lukem }
    138      1.1  lukem 
    139      1.1  lukem #ifdef IPW_DEBUG
    140      1.1  lukem #define DPRINTF(x)	if (ipw_debug > 0) printf x
    141      1.1  lukem #define DPRINTFN(n, x)	if (ipw_debug >= (n)) printf x
    142      1.1  lukem int ipw_debug = 0;
    143      1.1  lukem #else
    144      1.1  lukem #define DPRINTF(x)
    145      1.1  lukem #define DPRINTFN(n, x)
    146      1.1  lukem #endif
    147      1.1  lukem 
    148      1.8  lukem CFATTACH_DECL(ipw, sizeof (struct ipw_softc), ipw_match, ipw_attach,
    149      1.1  lukem     ipw_detach, NULL);
    150      1.1  lukem 
    151      1.1  lukem static int
    152      1.1  lukem ipw_match(struct device *parent, struct cfdata *match, void *aux)
    153      1.1  lukem {
    154      1.1  lukem 	struct pci_attach_args *pa = aux;
    155      1.1  lukem 
    156      1.8  lukem 	if (PCI_VENDOR (pa->pa_id) == PCI_VENDOR_INTEL &&
    157      1.1  lukem 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2100)
    158      1.1  lukem 		return 1;
    159      1.1  lukem 
    160      1.1  lukem 	return 0;
    161      1.1  lukem }
    162      1.1  lukem 
    163      1.1  lukem /* Base Address Register */
    164      1.1  lukem #define IPW_PCI_BAR0 0x10
    165      1.1  lukem 
    166      1.1  lukem static void
    167      1.1  lukem ipw_attach(struct device *parent, struct device *self, void *aux)
    168      1.1  lukem {
    169      1.1  lukem 	struct ipw_softc *sc = (struct ipw_softc *)self;
    170      1.1  lukem 	struct ieee80211com *ic = &sc->sc_ic;
    171      1.1  lukem 	struct ifnet *ifp = &ic->ic_if;
    172      1.1  lukem 	struct ieee80211_rateset *rs;
    173      1.1  lukem 	struct pci_attach_args *pa = aux;
    174      1.1  lukem 	const char *intrstr;
    175      1.1  lukem 	char devinfo[256];
    176      1.1  lukem 	bus_space_tag_t memt;
    177      1.1  lukem 	bus_space_handle_t memh;
    178      1.1  lukem 	bus_addr_t base;
    179      1.1  lukem 	pci_intr_handle_t ih;
    180      1.1  lukem 	u_int32_t data;
    181      1.1  lukem 	int i, revision, error;
    182      1.1  lukem 
    183      1.1  lukem 	sc->sc_pct = pa->pa_pc;
    184      1.1  lukem 
    185      1.1  lukem 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof devinfo);
    186      1.1  lukem 	revision = PCI_REVISION(pa->pa_class);
    187      1.1  lukem 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo, revision);
    188      1.1  lukem 
    189      1.1  lukem 	/* enable bus-mastering */
    190      1.1  lukem 	data = pci_conf_read(sc->sc_pct, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    191      1.1  lukem 	data |= PCI_COMMAND_MASTER_ENABLE;
    192      1.1  lukem 	pci_conf_write(sc->sc_pct, pa->pa_tag, PCI_COMMAND_STATUS_REG, data);
    193      1.1  lukem 
    194      1.1  lukem 	/* map the register window */
    195      1.8  lukem 	error = pci_mapreg_map(pa, IPW_PCI_BAR0, PCI_MAPREG_TYPE_MEM |
    196      1.1  lukem 	    PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh, &base, &sc->sc_sz);
    197      1.1  lukem 	if (error != 0) {
    198      1.8  lukem 		aprint_error("%s: could not map memory space\n",
    199      1.1  lukem 		    sc->sc_dev.dv_xname);
    200      1.1  lukem 		return;
    201      1.1  lukem 	}
    202      1.1  lukem 
    203      1.1  lukem 	sc->sc_st = memt;
    204      1.1  lukem 	sc->sc_sh = memh;
    205      1.1  lukem 	sc->sc_dmat = pa->pa_dmat;
    206      1.1  lukem 
    207      1.1  lukem 	/* disable interrupts */
    208      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
    209      1.1  lukem 
    210      1.1  lukem 	if (pci_intr_map(pa, &ih) != 0) {
    211      1.8  lukem 		aprint_error("%s: could not map interrupt\n",
    212      1.1  lukem 		    sc->sc_dev.dv_xname);
    213      1.1  lukem 		return;
    214      1.1  lukem 	}
    215      1.1  lukem 
    216      1.1  lukem 	intrstr = pci_intr_string(sc->sc_pct, ih);
    217      1.1  lukem 	sc->sc_ih = pci_intr_establish(sc->sc_pct, ih, IPL_NET, ipw_intr, sc);
    218      1.1  lukem 	if (sc->sc_ih == NULL) {
    219      1.8  lukem 		aprint_error("%s: could not establish interrupt",
    220      1.1  lukem 		    sc->sc_dev.dv_xname);
    221      1.1  lukem 		if (intrstr != NULL)
    222      1.1  lukem 			aprint_error(" at %s", intrstr);
    223      1.1  lukem 		aprint_error("\n");
    224      1.1  lukem 		return;
    225      1.1  lukem 	}
    226      1.1  lukem 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    227      1.1  lukem 
    228      1.1  lukem 	ic->ic_phytype = IEEE80211_T_DS;
    229      1.1  lukem 	ic->ic_opmode = IEEE80211_M_STA;
    230      1.1  lukem 	ic->ic_state = IEEE80211_S_INIT;
    231      1.1  lukem 
    232      1.1  lukem 	/* set device capabilities */
    233      1.8  lukem 	ic->ic_caps =  IEEE80211_C_IBSS | IEEE80211_C_MONITOR |
    234      1.1  lukem 	    IEEE80211_C_PMGT | IEEE80211_C_TXPMGT | IEEE80211_C_WEP;
    235      1.1  lukem 
    236      1.1  lukem 	/* set supported 11.b rates */
    237      1.1  lukem 	rs = &ic->ic_sup_rates[IEEE80211_MODE_11B];
    238      1.1  lukem 	rs->rs_nrates = 4;
    239      1.1  lukem 	rs->rs_rates[0] = 2;	/* 1Mbps */
    240      1.1  lukem 	rs->rs_rates[1] = 4;	/* 2Mbps */
    241      1.1  lukem 	rs->rs_rates[2] = 11;	/* 5.5Mbps */
    242      1.8  lukem 	rs->rs_rates[3] = 22;	/* 11Mbps */
    243      1.1  lukem 
    244      1.1  lukem 	/* set supported 11.b channels (1 through 14) */
    245      1.1  lukem 	for (i = 1; i <= 14; i++) {
    246      1.8  lukem 		ic->ic_channels[i].ic_freq =
    247      1.1  lukem 		    ieee80211_ieee2mhz(i, IEEE80211_CHAN_B);
    248      1.1  lukem 		ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B;
    249      1.1  lukem 	}
    250      1.1  lukem 
    251      1.1  lukem 	ic->ic_ibss_chan = &ic->ic_channels[0];
    252      1.1  lukem 
    253      1.1  lukem 	ifp->if_softc = sc;
    254      1.1  lukem 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    255      1.1  lukem 	ifp->if_init = ipw_init;
    256      1.1  lukem 	ifp->if_stop = ipw_stop;
    257      1.1  lukem 	ifp->if_ioctl = ipw_ioctl;
    258      1.1  lukem 	ifp->if_start = ipw_start;
    259      1.1  lukem 	ifp->if_watchdog = ipw_watchdog;
    260      1.1  lukem 	IFQ_SET_READY(&ifp->if_snd);
    261      1.1  lukem 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    262      1.1  lukem 
    263      1.1  lukem 	if_attach(ifp);
    264      1.1  lukem 	ieee80211_ifattach(ifp);
    265      1.1  lukem 	/* override state transition machine */
    266      1.1  lukem 	sc->sc_newstate = ic->ic_newstate;
    267      1.1  lukem 	ic->ic_newstate = ipw_newstate;
    268      1.1  lukem 
    269      1.1  lukem 	ieee80211_media_init(ifp, ipw_media_change, ieee80211_media_status);
    270      1.4  lukem 
    271      1.4  lukem #if NBPFILTER > 0
    272      1.8  lukem 	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
    273      1.4  lukem 	    sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf);
    274      1.4  lukem 
    275      1.4  lukem 	sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
    276      1.4  lukem 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
    277      1.4  lukem 	sc->sc_rxtap.wr_ihdr.it_present = htole32(IPW_RX_RADIOTAP_PRESENT);
    278      1.4  lukem 
    279      1.4  lukem 	sc->sc_txtap_len = sizeof sc->sc_txtapu;
    280      1.4  lukem 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
    281      1.4  lukem 	sc->sc_txtap.wt_ihdr.it_present = htole32(IPW_TX_RADIOTAP_PRESENT);
    282      1.4  lukem #endif
    283      1.1  lukem }
    284      1.1  lukem 
    285      1.1  lukem static int
    286      1.1  lukem ipw_detach(struct device* self, int flags)
    287      1.1  lukem {
    288      1.1  lukem 	struct ipw_softc *sc = (struct ipw_softc *)self;
    289      1.1  lukem 	struct ifnet *ifp = &sc->sc_ic.ic_if;
    290      1.1  lukem 
    291      1.1  lukem 	ipw_reset(sc);
    292      1.1  lukem 
    293      1.4  lukem #if NBPFILTER > 0
    294      1.4  lukem 	bpfdetach(ifp);
    295      1.4  lukem #endif
    296      1.1  lukem 	ieee80211_ifdetach(ifp);
    297      1.1  lukem 	if_detach(ifp);
    298      1.1  lukem 
    299      1.1  lukem 	if (sc->sc_ih != NULL) {
    300      1.1  lukem 		pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
    301      1.1  lukem 		sc->sc_ih = NULL;
    302      1.1  lukem 	}
    303      1.1  lukem 
    304      1.1  lukem 	bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz);
    305      1.1  lukem 
    306      1.1  lukem 	return 0;
    307      1.1  lukem }
    308      1.1  lukem 
    309      1.1  lukem static int
    310      1.1  lukem ipw_media_change(struct ifnet *ifp)
    311      1.1  lukem {
    312      1.1  lukem 	int error;
    313      1.1  lukem 
    314      1.1  lukem 	error = ieee80211_media_change(ifp);
    315      1.1  lukem 	if (error != ENETRESET)
    316      1.1  lukem 		return error;
    317      1.1  lukem 
    318      1.1  lukem 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
    319      1.1  lukem 		ipw_init(ifp);
    320      1.1  lukem 
    321      1.1  lukem 	return 0;
    322      1.1  lukem }
    323      1.1  lukem 
    324      1.1  lukem static int
    325      1.1  lukem ipw_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
    326      1.1  lukem {
    327      1.1  lukem 	struct ifnet *ifp = &ic->ic_if;
    328      1.1  lukem 	struct ipw_softc *sc = ifp->if_softc;
    329      1.1  lukem 	struct ieee80211_node *ni = ic->ic_bss;
    330      1.1  lukem 	u_int32_t val, len;
    331      1.1  lukem 
    332      1.1  lukem 	switch (nstate) {
    333      1.1  lukem 	case IEEE80211_S_INIT:
    334      1.1  lukem 		break;
    335      1.1  lukem 
    336      1.1  lukem 	case IEEE80211_S_RUN:
    337      1.1  lukem 		len = IEEE80211_NWID_LEN;
    338      1.1  lukem 		ipw_read_table2(sc, IPW_INFO_CURRENT_SSID, ni->ni_essid, &len);
    339      1.1  lukem 		ni->ni_esslen = len;
    340      1.1  lukem 
    341      1.1  lukem 		val = ipw_read_table1(sc, IPW_INFO_CURRENT_CHANNEL);
    342      1.1  lukem 		ni->ni_chan = &ic->ic_channels[val];
    343      1.1  lukem 
    344      1.1  lukem 		DELAY(100); /* firmware needs a short delay here */
    345      1.1  lukem 
    346      1.1  lukem 		len = IEEE80211_ADDR_LEN;
    347      1.1  lukem 		ipw_read_table2(sc, IPW_INFO_CURRENT_BSSID, ni->ni_bssid, &len);
    348      1.1  lukem 		break;
    349      1.1  lukem 
    350      1.1  lukem 	case IEEE80211_S_SCAN:
    351      1.1  lukem 	case IEEE80211_S_AUTH:
    352      1.1  lukem 	case IEEE80211_S_ASSOC:
    353      1.1  lukem 		break;
    354      1.1  lukem 	}
    355      1.1  lukem 
    356      1.1  lukem 	ic->ic_state = nstate;
    357      1.1  lukem 	return 0;
    358      1.1  lukem }
    359      1.1  lukem 
    360      1.1  lukem static void
    361      1.1  lukem ipw_command_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
    362      1.1  lukem {
    363      1.1  lukem 	struct ipw_cmd *cmd;
    364      1.1  lukem 
    365      1.1  lukem 	bus_dmamap_sync(sc->sc_dmat, sbuf->map, 0, sizeof (struct ipw_cmd),
    366      1.1  lukem 	    BUS_DMASYNC_POSTREAD);
    367      1.1  lukem 
    368      1.1  lukem 	cmd = mtod(sbuf->m, struct ipw_cmd *);
    369      1.1  lukem 
    370      1.8  lukem 	DPRINTFN(2, ("RX!CMD!%u!%u!%u!%u!%u\n",
    371      1.1  lukem 	    le32toh(cmd->type), le32toh(cmd->subtype), le32toh(cmd->seq),
    372      1.1  lukem 	    le32toh(cmd->len), le32toh(cmd->status)));
    373      1.1  lukem 
    374      1.8  lukem 	/*
    375      1.8  lukem 	 * Wake up processes waiting for command ack. In the case of the
    376      1.1  lukem 	 * IPW_CMD_DISABLE command, wake up the process only when the adapter
    377      1.1  lukem 	 * enters the IPW_STATE_DISABLED state. This is notified in
    378      1.1  lukem 	 * ipw_newstate_intr().
    379      1.1  lukem 	 */
    380      1.1  lukem 	if (le32toh(cmd->type) != IPW_CMD_DISABLE)
    381      1.1  lukem 		wakeup(sc->cmd);
    382      1.1  lukem }
    383      1.1  lukem 
    384      1.1  lukem static void
    385      1.1  lukem ipw_newstate_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
    386      1.1  lukem {
    387      1.1  lukem 	struct ieee80211com *ic = &sc->sc_ic;
    388      1.1  lukem 	u_int32_t state;
    389      1.1  lukem 
    390      1.1  lukem 	bus_dmamap_sync(sc->sc_dmat, sbuf->map, 0, sizeof state,
    391      1.1  lukem 	    BUS_DMASYNC_POSTREAD);
    392      1.1  lukem 
    393      1.1  lukem 	state = le32toh(*mtod(sbuf->m, u_int32_t *));
    394      1.1  lukem 
    395      1.1  lukem 	DPRINTFN(2, ("RX!NEWSTATE!%u\n", state));
    396      1.1  lukem 
    397      1.1  lukem 	switch (state) {
    398      1.1  lukem 	case IPW_STATE_ASSOCIATED:
    399      1.1  lukem 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
    400      1.1  lukem 		break;
    401      1.1  lukem 
    402      1.1  lukem 	case IPW_STATE_SCANNING:
    403      1.1  lukem 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
    404      1.1  lukem 		break;
    405      1.1  lukem 
    406      1.1  lukem 	case IPW_STATE_ASSOCIATION_LOST:
    407      1.1  lukem 		ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
    408      1.1  lukem 		break;
    409      1.1  lukem 
    410      1.1  lukem 	case IPW_STATE_DISABLED:
    411      1.1  lukem 		wakeup(sc->cmd);
    412      1.1  lukem 		break;
    413      1.1  lukem 
    414      1.1  lukem 	case IPW_STATE_RADIO_DISABLED:
    415      1.1  lukem 		/* XXX should turn the interface down */
    416      1.1  lukem 		break;
    417      1.1  lukem 	}
    418      1.1  lukem }
    419      1.1  lukem 
    420      1.1  lukem static void
    421      1.8  lukem ipw_data_intr(struct ipw_softc *sc, struct ipw_status *status,
    422      1.1  lukem     struct ipw_soft_bd *sbd, struct ipw_soft_buf *sbuf)
    423      1.1  lukem {
    424      1.1  lukem 	struct ieee80211com *ic = &sc->sc_ic;
    425      1.1  lukem 	struct ifnet *ifp = &ic->ic_if;
    426      1.1  lukem 	struct mbuf *m;
    427      1.1  lukem 	struct ieee80211_frame *wh;
    428      1.1  lukem 	struct ieee80211_node *ni;
    429      1.1  lukem 	int error;
    430      1.1  lukem 
    431      1.1  lukem 	DPRINTFN(5, ("RX!DATA!%u!%u\n", le32toh(status->len), status->rssi));
    432      1.1  lukem 
    433      1.1  lukem 	bus_dmamap_sync(sc->sc_dmat, sbuf->map, 0, le32toh(status->len),
    434      1.1  lukem 	    BUS_DMASYNC_POSTREAD);
    435      1.1  lukem 
    436      1.1  lukem 	bus_dmamap_unload(sc->sc_dmat, sbuf->map);
    437      1.1  lukem 
    438      1.1  lukem 	/* Finalize mbuf */
    439      1.1  lukem 	m = sbuf->m;
    440      1.1  lukem 	m->m_pkthdr.rcvif = ifp;
    441      1.1  lukem 	m->m_pkthdr.len = m->m_len = le32toh(status->len);
    442      1.1  lukem 
    443      1.4  lukem #if NBPFILTER > 0
    444      1.4  lukem 	if (sc->sc_drvbpf != NULL) {
    445      1.4  lukem 		struct ipw_rx_radiotap_header *tap = &sc->sc_rxtap;
    446      1.4  lukem 
    447      1.4  lukem 		tap->wr_flags = 0;
    448      1.4  lukem 		tap->wr_antsignal = status->rssi;
    449      1.4  lukem 		tap->wr_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
    450      1.4  lukem 		tap->wr_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
    451      1.4  lukem 
    452      1.4  lukem 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
    453      1.4  lukem 	}
    454      1.4  lukem #endif
    455      1.4  lukem 
    456      1.1  lukem 	wh = mtod(m, struct ieee80211_frame *);
    457      1.1  lukem 
    458      1.7  lukem 	ni = ieee80211_find_rxnode(ic, wh);
    459      1.1  lukem 
    460      1.1  lukem 	/* Send it up to the upper layer */
    461      1.1  lukem 	ieee80211_input(ifp, m, ni, status->rssi, 0/*rstamp*/);
    462      1.1  lukem 
    463      1.1  lukem 	ieee80211_release_node(ic, ni);
    464      1.1  lukem 
    465      1.1  lukem 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    466      1.1  lukem 	if (m == NULL) {
    467      1.8  lukem 		aprint_error("%s: could not allocate rx mbuf\n",
    468      1.1  lukem 		    sc->sc_dev.dv_xname);
    469      1.1  lukem 		return;
    470      1.1  lukem 	}
    471      1.1  lukem 	MCLGET(m, M_DONTWAIT);
    472      1.1  lukem 	if (!(m->m_flags & M_EXT)) {
    473      1.1  lukem 		m_freem(m);
    474      1.8  lukem 		aprint_error("%s: could not allocate rx mbuf cluster\n",
    475      1.1  lukem 		    sc->sc_dev.dv_xname);
    476      1.1  lukem 		return;
    477      1.1  lukem 	}
    478      1.1  lukem 
    479      1.8  lukem 	error = bus_dmamap_load(sc->sc_dmat, sbuf->map, mtod(m, void *),
    480      1.1  lukem 	    MCLBYTES, NULL, BUS_DMA_NOWAIT);
    481      1.1  lukem 	if (error != 0) {
    482      1.8  lukem 		aprint_error("%s: could not map rxbuf dma memory\n",
    483      1.1  lukem 		    sc->sc_dev.dv_xname);
    484      1.1  lukem 		m_freem(m);
    485      1.1  lukem 		return;
    486      1.1  lukem 	}
    487      1.1  lukem 
    488      1.1  lukem 	sbuf->m = m;
    489      1.1  lukem 	sbd->bd->physaddr = htole32(sbuf->map->dm_segs[0].ds_addr);
    490      1.1  lukem }
    491      1.1  lukem 
    492      1.1  lukem static void
    493      1.1  lukem ipw_notification_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
    494      1.1  lukem {
    495      1.1  lukem 	DPRINTFN(2, ("RX!NOTIFICATION\n"));
    496      1.1  lukem }
    497      1.1  lukem 
    498      1.1  lukem static void
    499      1.1  lukem ipw_rx_intr(struct ipw_softc *sc)
    500      1.1  lukem {
    501      1.1  lukem 	struct ipw_status *status;
    502      1.1  lukem 	struct ipw_soft_bd *sbd;
    503      1.1  lukem 	struct ipw_soft_buf *sbuf;
    504      1.1  lukem 	u_int32_t r, i;
    505      1.1  lukem 
    506      1.1  lukem 	r = CSR_READ_4(sc, IPW_CSR_RX_READ_INDEX);
    507      1.1  lukem 
    508      1.1  lukem 	for (i = (sc->rxcur + 1) % IPW_NRBD; i != r; i = (i + 1) % IPW_NRBD) {
    509      1.1  lukem 
    510      1.8  lukem 		bus_dmamap_sync(sc->sc_dmat, sc->rbd_map,
    511      1.8  lukem 		    i * sizeof (struct ipw_bd), sizeof (struct ipw_bd),
    512      1.1  lukem 		    BUS_DMASYNC_POSTREAD);
    513      1.1  lukem 
    514      1.8  lukem 		bus_dmamap_sync(sc->sc_dmat, sc->status_map,
    515      1.8  lukem 		    i * sizeof (struct ipw_status), sizeof (struct ipw_status),
    516      1.1  lukem 		    BUS_DMASYNC_POSTREAD);
    517      1.1  lukem 
    518      1.1  lukem 		status = &sc->status_list[i];
    519      1.1  lukem 		sbd = &sc->srbd_list[i];
    520      1.1  lukem 		sbuf = sbd->priv;
    521      1.1  lukem 
    522      1.1  lukem 		switch (le16toh(status->code) & 0xf) {
    523      1.1  lukem 		case IPW_STATUS_CODE_COMMAND:
    524      1.1  lukem 			ipw_command_intr(sc, sbuf);
    525      1.1  lukem 			break;
    526      1.1  lukem 
    527      1.1  lukem 		case IPW_STATUS_CODE_NEWSTATE:
    528      1.1  lukem 			ipw_newstate_intr(sc, sbuf);
    529      1.1  lukem 			break;
    530      1.1  lukem 
    531      1.1  lukem 		case IPW_STATUS_CODE_DATA_802_3:
    532      1.1  lukem 		case IPW_STATUS_CODE_DATA_802_11:
    533      1.1  lukem 			ipw_data_intr(sc, status, sbd, sbuf);
    534      1.1  lukem 			break;
    535      1.1  lukem 
    536      1.1  lukem 		case IPW_STATUS_CODE_NOTIFICATION:
    537      1.1  lukem 			ipw_notification_intr(sc, sbuf);
    538      1.1  lukem 			break;
    539      1.1  lukem 
    540      1.1  lukem 		default:
    541      1.8  lukem 			aprint_debug("%s: unknown status code %u\n",
    542      1.1  lukem 			    sc->sc_dev.dv_xname, le16toh(status->code));
    543      1.1  lukem 		}
    544      1.1  lukem 		sbd->bd->flags = 0;
    545      1.1  lukem 
    546      1.8  lukem 		bus_dmamap_sync(sc->sc_dmat, sc->rbd_map,
    547      1.8  lukem 		    i * sizeof (struct ipw_bd), sizeof (struct ipw_bd),
    548      1.1  lukem 		    BUS_DMASYNC_PREWRITE);
    549      1.1  lukem 	}
    550      1.1  lukem 
    551      1.1  lukem 	/* Tell the firmware what we have processed */
    552      1.1  lukem 	sc->rxcur = (r == 0) ? IPW_NRBD - 1 : r - 1;
    553      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_RX_WRITE_INDEX, sc->rxcur);
    554      1.1  lukem }
    555      1.1  lukem 
    556      1.1  lukem static void
    557      1.1  lukem ipw_release_sbd(struct ipw_softc *sc, struct ipw_soft_bd *sbd)
    558      1.1  lukem {
    559      1.1  lukem 	struct ieee80211com *ic;
    560      1.1  lukem 	struct ipw_soft_hdr *shdr;
    561      1.1  lukem 	struct ipw_soft_buf *sbuf;
    562      1.1  lukem 
    563      1.1  lukem 	switch (sbd->type) {
    564      1.1  lukem 	case IPW_SBD_TYPE_COMMAND:
    565      1.1  lukem 		bus_dmamap_unload(sc->sc_dmat, sc->cmd_map);
    566      1.1  lukem 		break;
    567      1.1  lukem 
    568      1.1  lukem 	case IPW_SBD_TYPE_HEADER:
    569      1.1  lukem 		shdr = sbd->priv;
    570      1.1  lukem 		bus_dmamap_unload(sc->sc_dmat, shdr->map);
    571      1.1  lukem 		TAILQ_INSERT_TAIL(&sc->sc_free_shdr, shdr, next);
    572      1.1  lukem 		break;
    573      1.1  lukem 
    574      1.1  lukem 	case IPW_SBD_TYPE_DATA:
    575      1.1  lukem 		ic = &sc->sc_ic;
    576      1.1  lukem 		sbuf = sbd->priv;
    577      1.1  lukem 		bus_dmamap_unload(sc->sc_dmat, sbuf->map);
    578      1.1  lukem 		m_freem(sbuf->m);
    579      1.1  lukem 		if (sbuf->ni != NULL)
    580      1.1  lukem 			ieee80211_release_node(ic, sbuf->ni);
    581      1.1  lukem 		/* kill watchdog timer */
    582      1.1  lukem 		sc->sc_tx_timer = 0;
    583      1.1  lukem 		TAILQ_INSERT_TAIL(&sc->sc_free_sbuf, sbuf, next);
    584      1.1  lukem 		break;
    585      1.1  lukem 	}
    586  1.8.4.1   kent 	++sc->txfree;
    587      1.1  lukem 	sbd->type = IPW_SBD_TYPE_NOASSOC;
    588      1.1  lukem }
    589      1.1  lukem 
    590      1.1  lukem static void
    591      1.1  lukem ipw_tx_intr(struct ipw_softc *sc)
    592      1.1  lukem {
    593      1.1  lukem 	struct ifnet *ifp = &sc->sc_ic.ic_if;
    594      1.1  lukem 	u_int32_t r, i;
    595      1.1  lukem 
    596      1.1  lukem 	r = CSR_READ_4(sc, IPW_CSR_TX_READ_INDEX);
    597      1.1  lukem 
    598      1.1  lukem 	for (i = (sc->txold + 1) % IPW_NTBD; i != r; i = (i + 1) % IPW_NTBD)
    599      1.1  lukem 		ipw_release_sbd(sc, &sc->stbd_list[i]);
    600      1.1  lukem 
    601      1.1  lukem 	/* Remember what the firmware has processed */
    602      1.1  lukem 	sc->txold = (r == 0) ? IPW_NTBD - 1 : r - 1;
    603      1.1  lukem 
    604      1.1  lukem 	/* Call start() since some buffer descriptors have been released */
    605      1.1  lukem 	ifp->if_flags &= ~IFF_OACTIVE;
    606      1.1  lukem 	(*ifp->if_start)(ifp);
    607      1.1  lukem }
    608      1.1  lukem 
    609      1.1  lukem static int
    610      1.1  lukem ipw_intr(void *arg)
    611      1.1  lukem {
    612      1.1  lukem 	struct ipw_softc *sc = arg;
    613      1.1  lukem 	u_int32_t r;
    614      1.1  lukem 
    615      1.1  lukem 	if ((r = CSR_READ_4(sc, IPW_CSR_INTR)) == 0)
    616      1.1  lukem 		return 0;
    617      1.1  lukem 
    618      1.1  lukem 	/* Disable interrupts */
    619      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
    620      1.1  lukem 
    621      1.1  lukem 	DPRINTFN(8, ("INTR!0x%08x\n", r));
    622      1.1  lukem 
    623      1.1  lukem 	if (r & IPW_INTR_RX_TRANSFER)
    624      1.1  lukem 		ipw_rx_intr(sc);
    625      1.1  lukem 
    626      1.1  lukem 	if (r & IPW_INTR_TX_TRANSFER)
    627      1.1  lukem 		ipw_tx_intr(sc);
    628      1.1  lukem 
    629      1.1  lukem 	if (r & IPW_INTR_FW_INIT_DONE) {
    630      1.1  lukem 		if (!(r & (IPW_INTR_FATAL_ERROR | IPW_INTR_PARITY_ERROR)))
    631      1.1  lukem 			wakeup(sc);
    632      1.1  lukem 	}
    633      1.1  lukem 
    634      1.1  lukem 	/* Acknowledge interrupts */
    635      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_INTR, r);
    636      1.1  lukem 
    637      1.1  lukem 	/* Re-enable interrupts */
    638      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
    639      1.1  lukem 
    640      1.1  lukem 	return 0;
    641      1.1  lukem }
    642      1.1  lukem 
    643      1.1  lukem static int
    644      1.1  lukem ipw_cmd(struct ipw_softc *sc, u_int32_t type, void *data, u_int32_t len)
    645      1.1  lukem {
    646      1.1  lukem 	struct ipw_soft_bd *sbd;
    647      1.1  lukem 	int error;
    648      1.1  lukem 
    649  1.8.4.1   kent #ifdef DIAGNOSTIC
    650  1.8.4.1   kent 	KASSERT(sc->txfree != 0);
    651  1.8.4.1   kent #endif /* DIAGNOSTIC */
    652  1.8.4.1   kent 
    653      1.1  lukem 	sbd = &sc->stbd_list[sc->txcur];
    654      1.1  lukem 
    655      1.8  lukem 	error = bus_dmamap_load(sc->sc_dmat, sc->cmd_map, sc->cmd,
    656      1.1  lukem 	    sizeof (struct ipw_cmd), NULL, BUS_DMA_NOWAIT);
    657      1.1  lukem 	if (error != 0) {
    658      1.8  lukem 		aprint_error("%s: could not map cmd dma memory\n",
    659      1.1  lukem 		    sc->sc_dev.dv_xname);
    660      1.1  lukem 		return error;
    661      1.1  lukem 	}
    662      1.1  lukem 
    663      1.1  lukem 	sc->cmd->type = htole32(type);
    664      1.1  lukem 	sc->cmd->subtype = htole32(0);
    665      1.1  lukem 	sc->cmd->len = htole32(len);
    666      1.1  lukem 	sc->cmd->seq = htole32(0);
    667      1.1  lukem 	if (data != NULL)
    668      1.1  lukem 		bcopy(data, sc->cmd->data, len);
    669      1.1  lukem 
    670      1.1  lukem 	sbd->type = IPW_SBD_TYPE_COMMAND;
    671      1.1  lukem 	sbd->bd->physaddr = htole32(sc->cmd_map->dm_segs[0].ds_addr);
    672      1.1  lukem 	sbd->bd->len = htole32(sizeof (struct ipw_cmd));
    673      1.1  lukem 	sbd->bd->nfrag = 1;
    674      1.8  lukem 	sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_COMMAND |
    675      1.1  lukem 			 IPW_BD_FLAG_TX_LAST_FRAGMENT;
    676      1.1  lukem 
    677      1.1  lukem 	bus_dmamap_sync(sc->sc_dmat, sc->cmd_map, 0, sizeof (struct ipw_cmd),
    678      1.1  lukem 	    BUS_DMASYNC_PREWRITE);
    679      1.8  lukem 
    680      1.8  lukem 	bus_dmamap_sync(sc->sc_dmat, sc->tbd_map,
    681      1.1  lukem 	    sc->txcur * sizeof (struct ipw_bd), sizeof (struct ipw_bd),
    682      1.1  lukem 	    BUS_DMASYNC_PREWRITE);
    683      1.1  lukem 
    684  1.8.4.1   kent 	--sc->txfree;
    685      1.1  lukem 	sc->txcur = (sc->txcur + 1) % IPW_NTBD;
    686      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE_INDEX, sc->txcur);
    687      1.1  lukem 
    688      1.1  lukem 	DPRINTFN(2, ("TX!CMD!%u!%u!%u!%u\n", type, 0, 0, len));
    689      1.1  lukem 
    690      1.1  lukem 	/* Wait at most two seconds for command to complete */
    691      1.1  lukem 	return tsleep(sc->cmd, 0, "ipwcmd", 2 * hz);
    692      1.1  lukem }
    693      1.1  lukem 
    694  1.8.4.1   kent /* Check that descriptors are available to transmit one packet.
    695  1.8.4.1   kent  * Always reserve one transmit-buffer descriptor for ipw_cmd.
    696  1.8.4.1   kent  */
    697  1.8.4.1   kent static __inline int
    698  1.8.4.1   kent ipw_tx_ready(struct ipw_softc *sc)
    699  1.8.4.1   kent {
    700  1.8.4.1   kent 	return !TAILQ_EMPTY(&sc->sc_free_shdr) &&
    701  1.8.4.1   kent 	       !TAILQ_EMPTY(&sc->sc_free_sbuf) && sc->txfree > 1;
    702  1.8.4.1   kent }
    703  1.8.4.1   kent 
    704  1.8.4.1   kent /* Must not be called unless ipw_tx_ready(sc). */
    705      1.1  lukem static int
    706      1.1  lukem ipw_tx_start(struct ifnet *ifp, struct mbuf *m, struct ieee80211_node *ni)
    707      1.1  lukem {
    708      1.1  lukem 	struct ipw_softc *sc = ifp->if_softc;
    709      1.1  lukem 	struct ieee80211com *ic = &sc->sc_ic;
    710      1.1  lukem 	struct ieee80211_frame *wh;
    711      1.1  lukem 	struct ipw_soft_bd *sbd;
    712      1.1  lukem 	struct ipw_soft_hdr *shdr;
    713      1.1  lukem 	struct ipw_soft_buf *sbuf;
    714      1.1  lukem 	int error, i;
    715      1.1  lukem 
    716  1.8.4.1   kent #ifdef DIAGNOSTIC
    717  1.8.4.1   kent 	KASSERT(ipw_tx_ready(sc));
    718  1.8.4.1   kent #endif
    719  1.8.4.1   kent 
    720      1.1  lukem 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
    721      1.1  lukem 		m = ieee80211_wep_crypt(ifp, m, 1);
    722      1.1  lukem 		if (m == NULL)
    723      1.1  lukem 			return ENOBUFS;
    724      1.1  lukem 	}
    725      1.1  lukem 
    726      1.4  lukem #if NBPFILTER > 0
    727      1.4  lukem 	if (sc->sc_drvbpf != NULL) {
    728      1.4  lukem 		struct ipw_tx_radiotap_header *tap = &sc->sc_txtap;
    729      1.4  lukem 
    730      1.4  lukem 		tap->wt_flags = 0;
    731      1.4  lukem 		tap->wt_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
    732      1.4  lukem 		tap->wt_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
    733      1.4  lukem 
    734      1.4  lukem 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m);
    735      1.4  lukem 	}
    736      1.4  lukem #endif
    737      1.4  lukem 
    738      1.1  lukem 	wh = mtod(m, struct ieee80211_frame *);
    739      1.1  lukem 
    740      1.1  lukem 	shdr = TAILQ_FIRST(&sc->sc_free_shdr);
    741      1.1  lukem 	sbuf = TAILQ_FIRST(&sc->sc_free_sbuf);
    742      1.1  lukem 
    743      1.1  lukem 	shdr->hdr.type = htole32(IPW_HDR_TYPE_SEND);
    744      1.1  lukem 	shdr->hdr.subtype = htole32(0);
    745      1.1  lukem 	shdr->hdr.encrypted = (wh->i_fc[1] & IEEE80211_FC1_WEP) ? 1 : 0;
    746      1.1  lukem 	shdr->hdr.encrypt = 0;
    747      1.1  lukem 	shdr->hdr.keyidx = 0;
    748      1.1  lukem 	shdr->hdr.keysz = 0;
    749      1.1  lukem 	shdr->hdr.fragmentsz = htole16(0);
    750      1.1  lukem 	IEEE80211_ADDR_COPY(shdr->hdr.src_addr, wh->i_addr2);
    751      1.1  lukem 	if (ic->ic_opmode == IEEE80211_M_STA)
    752      1.1  lukem 		IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr3);
    753      1.1  lukem 	else
    754      1.1  lukem 		IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr1);
    755      1.1  lukem 
    756      1.1  lukem 	/* trim IEEE802.11 header */
    757      1.1  lukem 	m_adj(m, sizeof (struct ieee80211_frame));
    758      1.1  lukem 
    759      1.1  lukem 	/*
    760      1.8  lukem 	 * We need to map the mbuf first to know how many buffer descriptors
    761      1.1  lukem 	 * are needed for this transfer.
    762      1.1  lukem 	 */
    763      1.1  lukem 	error = bus_dmamap_load_mbuf(sc->sc_dmat, sbuf->map, m, BUS_DMA_NOWAIT);
    764      1.1  lukem 	if (error != 0) {
    765      1.8  lukem 		aprint_error("%s: could not map mbuf (error %d)\n",
    766      1.1  lukem 		    sc->sc_dev.dv_xname, error);
    767      1.1  lukem 		m_freem(m);
    768      1.1  lukem 		return error;
    769      1.1  lukem 	}
    770      1.1  lukem 
    771      1.1  lukem 	error = bus_dmamap_load(sc->sc_dmat, shdr->map, &shdr->hdr,
    772      1.1  lukem 	    sizeof (struct ipw_hdr), NULL, BUS_DMA_NOWAIT);
    773      1.1  lukem 	if (error != 0) {
    774      1.8  lukem 		aprint_error("%s: could not map hdr (error %d)\n",
    775      1.1  lukem 		    sc->sc_dev.dv_xname, error);
    776      1.1  lukem 		bus_dmamap_unload(sc->sc_dmat, sbuf->map);
    777      1.1  lukem 		m_freem(m);
    778      1.1  lukem 		return error;
    779      1.1  lukem 	}
    780      1.1  lukem 
    781      1.1  lukem 	TAILQ_REMOVE(&sc->sc_free_sbuf, sbuf, next);
    782      1.1  lukem 	TAILQ_REMOVE(&sc->sc_free_shdr, shdr, next);
    783      1.1  lukem 
    784      1.1  lukem 	sbd = &sc->stbd_list[sc->txcur];
    785      1.1  lukem 	sbd->type = IPW_SBD_TYPE_HEADER;
    786      1.1  lukem 	sbd->priv = shdr;
    787      1.1  lukem 	sbd->bd->physaddr = htole32(shdr->map->dm_segs[0].ds_addr);
    788      1.1  lukem 	sbd->bd->len = htole32(sizeof (struct ipw_hdr));
    789      1.1  lukem 	sbd->bd->nfrag = 1 + sbuf->map->dm_nsegs;
    790      1.1  lukem 	sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3 |
    791      1.1  lukem 			 IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
    792      1.1  lukem 
    793      1.8  lukem 	DPRINTFN(5, ("TX!HDR!%u!%u!%u!%u\n", shdr->hdr.type, shdr->hdr.subtype,
    794      1.8  lukem 	    shdr->hdr.encrypted, shdr->hdr.encrypt));
    795      1.8  lukem 	DPRINTFN(5, ("!%s", ether_sprintf(shdr->hdr.src_addr)));
    796      1.1  lukem 	DPRINTFN(5, ("!%s\n", ether_sprintf(shdr->hdr.dst_addr)));
    797      1.6  lukem 
    798      1.8  lukem 	bus_dmamap_sync(sc->sc_dmat, sc->tbd_map,
    799      1.8  lukem 	    sc->txcur * sizeof (struct ipw_bd),
    800      1.6  lukem 	    sizeof (struct ipw_bd), BUS_DMASYNC_PREWRITE);
    801      1.6  lukem 
    802      1.1  lukem 	sc->txcur = (sc->txcur + 1) % IPW_NTBD;
    803  1.8.4.1   kent 	--sc->txfree;
    804      1.1  lukem 
    805      1.1  lukem 	sbuf->m = m;
    806      1.1  lukem 	sbuf->ni = ni;
    807      1.1  lukem 
    808      1.1  lukem 	for (i = 0; i < sbuf->map->dm_nsegs; i++) {
    809      1.1  lukem 		sbd = &sc->stbd_list[sc->txcur];
    810      1.1  lukem 		sbd->bd->physaddr = htole32(sbuf->map->dm_segs[i].ds_addr);
    811      1.1  lukem 		sbd->bd->len = htole32(sbuf->map->dm_segs[i].ds_len);
    812      1.1  lukem 		sbd->bd->nfrag = 0; /* used only in first bd */
    813      1.1  lukem 		sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3;
    814      1.1  lukem 		if (i == sbuf->map->dm_nsegs - 1) {
    815      1.1  lukem 			sbd->type = IPW_SBD_TYPE_DATA;
    816      1.1  lukem 			sbd->priv = sbuf;
    817      1.1  lukem 			sbd->bd->flags |= IPW_BD_FLAG_TX_LAST_FRAGMENT;
    818      1.1  lukem 		} else {
    819      1.1  lukem 			sbd->type = IPW_SBD_TYPE_NOASSOC;
    820      1.1  lukem 			sbd->bd->flags |= IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
    821      1.1  lukem 		}
    822      1.1  lukem 
    823      1.8  lukem 		DPRINTFN(5, ("TX!FRAG!%d!%ld\n", i,
    824      1.1  lukem 		    sbuf->map->dm_segs[i].ds_len));
    825      1.1  lukem 
    826      1.8  lukem 		bus_dmamap_sync(sc->sc_dmat, sc->tbd_map,
    827      1.8  lukem 		    sc->txcur * sizeof (struct ipw_bd),
    828      1.1  lukem 		    sizeof (struct ipw_bd), BUS_DMASYNC_PREWRITE);
    829      1.1  lukem 
    830      1.1  lukem 		sc->txcur = (sc->txcur + 1) % IPW_NTBD;
    831      1.1  lukem 	}
    832      1.1  lukem 
    833      1.8  lukem 	bus_dmamap_sync(sc->sc_dmat, shdr->map, 0, sizeof (struct ipw_hdr),
    834      1.1  lukem 	    BUS_DMASYNC_PREWRITE);
    835      1.1  lukem 
    836      1.8  lukem 	bus_dmamap_sync(sc->sc_dmat, sbuf->map, 0, MCLBYTES,
    837      1.1  lukem 	    BUS_DMASYNC_PREWRITE);
    838      1.1  lukem 
    839      1.1  lukem 	/* Inform firmware about this new packet */
    840      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE_INDEX, sc->txcur);
    841      1.1  lukem 
    842      1.1  lukem 	return 0;
    843      1.1  lukem }
    844      1.1  lukem 
    845      1.1  lukem static void
    846      1.1  lukem ipw_start(struct ifnet *ifp)
    847      1.1  lukem {
    848      1.1  lukem 	struct ipw_softc *sc = ifp->if_softc;
    849      1.1  lukem 	struct ieee80211com *ic = &sc->sc_ic;
    850      1.1  lukem 	struct mbuf *m;
    851      1.1  lukem 	struct ieee80211_node *ni;
    852      1.1  lukem 
    853      1.1  lukem 	for (;;) {
    854  1.8.4.1   kent 		if (!ipw_tx_ready(sc)) {
    855  1.8.4.1   kent 			DPRINTFN(2, ("%s: no tx descriptors\n", ifp->if_xname));
    856  1.8.4.1   kent 			ifp->if_flags |= IFF_OACTIVE;
    857  1.8.4.1   kent 			break;
    858  1.8.4.1   kent 		}
    859  1.8.4.1   kent 		IFQ_DEQUEUE(&ifp->if_snd, m);
    860      1.1  lukem 		if (m == NULL)
    861      1.1  lukem 			break;
    862      1.1  lukem 
    863      1.1  lukem #if NBPFILTER > 0
    864      1.1  lukem 		if (ifp->if_bpf != NULL)
    865      1.1  lukem 			bpf_mtap(ifp->if_bpf, m);
    866      1.1  lukem #endif
    867      1.1  lukem 
    868      1.1  lukem 		m = ieee80211_encap(ifp, m, &ni);
    869      1.1  lukem 		if (m == NULL)
    870      1.1  lukem 			continue;
    871      1.1  lukem 
    872      1.1  lukem #if NBPFILTER > 0
    873      1.1  lukem 		if (ic->ic_rawbpf != NULL)
    874      1.1  lukem 			bpf_mtap(ic->ic_rawbpf, m);
    875      1.1  lukem #endif
    876      1.1  lukem 
    877      1.1  lukem 		if (ipw_tx_start(ifp, m, ni) != 0) {
    878      1.1  lukem 			if (ni != NULL)
    879      1.1  lukem 				ieee80211_release_node(ic, ni);
    880      1.1  lukem 			break;
    881      1.1  lukem 		}
    882      1.1  lukem 
    883      1.1  lukem 		/* start watchdog timer */
    884      1.1  lukem 		sc->sc_tx_timer = 5;
    885      1.1  lukem 		ifp->if_timer = 1;
    886      1.1  lukem 	}
    887      1.1  lukem }
    888      1.1  lukem 
    889      1.1  lukem static void
    890      1.1  lukem ipw_watchdog(struct ifnet *ifp)
    891      1.1  lukem {
    892      1.1  lukem 	struct ipw_softc *sc = ifp->if_softc;
    893      1.1  lukem 
    894      1.1  lukem 	ifp->if_timer = 0;
    895      1.1  lukem 
    896      1.1  lukem 	if (sc->sc_tx_timer > 0) {
    897      1.1  lukem 		if (--sc->sc_tx_timer == 0) {
    898      1.8  lukem 			aprint_error("%s: device timeout\n",
    899      1.1  lukem 			    sc->sc_dev.dv_xname);
    900      1.1  lukem #ifdef notyet
    901      1.1  lukem 			ipw_init(ifp);
    902      1.1  lukem #endif
    903      1.1  lukem 			return;
    904      1.1  lukem 		}
    905      1.1  lukem 		ifp->if_timer = 1;
    906      1.1  lukem 	}
    907      1.1  lukem 
    908      1.1  lukem 	ieee80211_watchdog(ifp);
    909      1.1  lukem }
    910      1.1  lukem 
    911      1.1  lukem static int
    912      1.1  lukem ipw_get_table1(struct ipw_softc *sc, u_int32_t *tbl)
    913      1.1  lukem {
    914      1.1  lukem 	u_int32_t addr, size, i;
    915      1.1  lukem 
    916      1.1  lukem 	if (!(sc->flags & IPW_FLAG_FW_INITED))
    917      1.1  lukem 		return ENOTTY;
    918      1.1  lukem 
    919      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_AUTOINC_ADDR, sc->table1_base);
    920      1.1  lukem 
    921      1.1  lukem 	size = CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA);
    922      1.1  lukem 	if (suword(tbl, size) != 0)
    923      1.1  lukem 		return EFAULT;
    924      1.1  lukem 
    925      1.1  lukem 	for (i = 1, ++tbl; i < size; i++, tbl++) {
    926      1.1  lukem 		addr = CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA);
    927      1.1  lukem 		if (suword(tbl, MEM_READ_4(sc, addr)) != 0)
    928      1.1  lukem 			return EFAULT;
    929      1.1  lukem 	}
    930      1.1  lukem 	return 0;
    931      1.1  lukem }
    932      1.1  lukem 
    933      1.1  lukem static int
    934      1.1  lukem ipw_get_radio(struct ipw_softc *sc, int *ret)
    935      1.1  lukem {
    936      1.1  lukem 	u_int32_t addr;
    937      1.1  lukem 
    938      1.1  lukem 	if (!(sc->flags & IPW_FLAG_FW_INITED))
    939      1.1  lukem 		return ENOTTY;
    940      1.1  lukem 
    941      1.1  lukem 	addr = ipw_read_table1(sc, IPW_INFO_EEPROM_ADDRESS);
    942      1.1  lukem 	if ((MEM_READ_4(sc, addr + 32) >> 24) & 1) {
    943      1.1  lukem 		suword(ret, -1);
    944      1.1  lukem 		return 0;
    945      1.1  lukem 	}
    946      1.1  lukem 
    947      1.1  lukem 	if (CSR_READ_4(sc, IPW_CSR_IO) & IPW_IO_RADIO_DISABLED)
    948      1.1  lukem 		suword(ret, 0);
    949      1.1  lukem 	else
    950      1.1  lukem 		suword(ret, 1);
    951      1.1  lukem 
    952      1.1  lukem 	return 0;
    953      1.1  lukem }
    954      1.1  lukem 
    955      1.1  lukem static int
    956      1.1  lukem ipw_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    957      1.1  lukem {
    958      1.1  lukem 	struct ipw_softc *sc = ifp->if_softc;
    959      1.1  lukem 	struct ifreq *ifr;
    960      1.1  lukem 	int s, error = 0;
    961      1.1  lukem 
    962      1.1  lukem 	s = splnet();
    963      1.1  lukem 
    964      1.1  lukem 	switch (cmd) {
    965      1.1  lukem 	case SIOCSIFFLAGS:
    966      1.1  lukem 		if (ifp->if_flags & IFF_UP) {
    967      1.1  lukem 			if (!(ifp->if_flags & IFF_RUNNING))
    968      1.1  lukem 				ipw_init(ifp);
    969      1.1  lukem 		} else {
    970      1.1  lukem 			if (ifp->if_flags & IFF_RUNNING)
    971      1.1  lukem 				ipw_stop(ifp, 1);
    972      1.1  lukem 		}
    973      1.1  lukem 		break;
    974      1.1  lukem 
    975      1.1  lukem 	case SIOCGTABLE1:
    976      1.1  lukem 		ifr = (struct ifreq *)data;
    977      1.1  lukem 		error = ipw_get_table1(sc, (u_int32_t *)ifr->ifr_data);
    978      1.1  lukem 		break;
    979      1.1  lukem 
    980      1.1  lukem 	case SIOCGRADIO:
    981      1.1  lukem 		ifr = (struct ifreq *)data;
    982      1.1  lukem 		error = ipw_get_radio(sc, (int *)ifr->ifr_data);
    983      1.1  lukem 		break;
    984      1.1  lukem 
    985      1.1  lukem 	case SIOCSLOADFW:
    986      1.1  lukem 		/* only super-user can do that! */
    987      1.1  lukem 		if ((error = suser(curproc->p_ucred, &curproc->p_acflag)) != 0)
    988      1.1  lukem 			break;
    989      1.1  lukem 
    990      1.1  lukem 		ifr = (struct ifreq *)data;
    991      1.1  lukem 		error = ipw_firmware_init(sc, (u_char *)ifr->ifr_data);
    992      1.1  lukem 		break;
    993      1.1  lukem 
    994      1.1  lukem 	case SIOCSKILLFW:
    995      1.1  lukem 		/* only super-user can do that! */
    996      1.1  lukem 		if ((error = suser(curproc->p_ucred, &curproc->p_acflag)) != 0)
    997      1.1  lukem 			break;
    998      1.1  lukem 
    999      1.1  lukem 		ipw_reset(sc);
   1000      1.1  lukem 		break;
   1001      1.1  lukem 
   1002      1.1  lukem 	default:
   1003      1.1  lukem 		error = ieee80211_ioctl(ifp, cmd, data);
   1004      1.1  lukem 		if (error != ENETRESET)
   1005      1.1  lukem 			break;
   1006      1.1  lukem 
   1007      1.8  lukem 		if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
   1008      1.1  lukem 		    (IFF_UP | IFF_RUNNING))
   1009      1.1  lukem 			ipw_init(ifp);
   1010      1.1  lukem 		error = 0;
   1011      1.1  lukem 	}
   1012      1.1  lukem 
   1013      1.1  lukem 	splx(s);
   1014      1.1  lukem 	return error;
   1015      1.1  lukem }
   1016      1.1  lukem 
   1017      1.1  lukem static u_int32_t
   1018      1.1  lukem ipw_read_table1(struct ipw_softc *sc, u_int32_t off)
   1019      1.1  lukem {
   1020      1.1  lukem 	return MEM_READ_4(sc, MEM_READ_4(sc, sc->table1_base + off));
   1021      1.1  lukem }
   1022      1.1  lukem 
   1023      1.1  lukem static void
   1024      1.1  lukem ipw_write_table1(struct ipw_softc *sc, u_int32_t off, u_int32_t info)
   1025      1.1  lukem {
   1026      1.1  lukem 	MEM_WRITE_4(sc, MEM_READ_4(sc, sc->table1_base + off), info);
   1027      1.1  lukem }
   1028      1.1  lukem 
   1029      1.1  lukem static int
   1030      1.1  lukem ipw_read_table2(struct ipw_softc *sc, u_int32_t off, void *buf, u_int32_t *len)
   1031      1.1  lukem {
   1032      1.1  lukem 	u_int32_t addr, info;
   1033      1.1  lukem 	u_int16_t count, size;
   1034      1.1  lukem 	u_int32_t total;
   1035      1.1  lukem 
   1036      1.1  lukem 	/* addr[4] + count[2] + size[2] */
   1037      1.1  lukem 	addr = MEM_READ_4(sc, sc->table2_base + off);
   1038      1.1  lukem 	info = MEM_READ_4(sc, sc->table2_base + off + 4);
   1039      1.1  lukem 
   1040      1.1  lukem 	count = info >> 16;
   1041      1.1  lukem 	size = info & 0xffff;
   1042      1.1  lukem 	total = count * size;
   1043      1.1  lukem 
   1044      1.1  lukem 	if (total > *len) {
   1045      1.1  lukem 		*len = total;
   1046      1.1  lukem 		return EINVAL;
   1047      1.1  lukem 	}
   1048      1.1  lukem 
   1049      1.1  lukem 	*len = total;
   1050      1.1  lukem 	ipw_read_mem_1(sc, addr, buf, total);
   1051      1.1  lukem 
   1052      1.1  lukem 	return 0;
   1053      1.1  lukem }
   1054      1.1  lukem 
   1055      1.1  lukem static int
   1056      1.1  lukem ipw_tx_init(struct ipw_softc *sc)
   1057      1.1  lukem {
   1058      1.1  lukem 	char *errmsg;
   1059      1.1  lukem 	struct ipw_bd *bd;
   1060      1.1  lukem 	struct ipw_soft_bd *sbd;
   1061      1.1  lukem 	struct ipw_soft_hdr *shdr;
   1062      1.1  lukem 	struct ipw_soft_buf *sbuf;
   1063      1.1  lukem 	int error, i, nsegs;
   1064      1.1  lukem 
   1065      1.1  lukem 	/* Allocate transmission buffer descriptors */
   1066      1.8  lukem 	error = bus_dmamap_create(sc->sc_dmat, IPW_TBD_SZ, 1, IPW_TBD_SZ, 0,
   1067      1.1  lukem 	    BUS_DMA_NOWAIT, &sc->tbd_map);
   1068      1.1  lukem 	if (error != 0) {
   1069      1.1  lukem 		errmsg = "could not create tbd dma map";
   1070      1.1  lukem 		goto fail;
   1071      1.1  lukem 	}
   1072      1.1  lukem 
   1073      1.8  lukem 	error = bus_dmamem_alloc(sc->sc_dmat, IPW_TBD_SZ, PAGE_SIZE, 0,
   1074      1.1  lukem 	    &sc->tbd_seg, 1, &nsegs, BUS_DMA_NOWAIT);
   1075      1.1  lukem 	if (error != 0) {
   1076      1.1  lukem 		errmsg = "could not allocate tbd dma memory";
   1077      1.1  lukem 		goto fail;
   1078      1.1  lukem 	}
   1079      1.1  lukem 
   1080      1.8  lukem 	error = bus_dmamem_map(sc->sc_dmat, &sc->tbd_seg, nsegs, IPW_TBD_SZ,
   1081      1.1  lukem 	    (caddr_t *)&sc->tbd_list, BUS_DMA_NOWAIT);
   1082      1.1  lukem 	if (error != 0) {
   1083      1.1  lukem 		errmsg = "could not map tbd dma memory";
   1084      1.1  lukem 		goto fail;
   1085      1.1  lukem 	}
   1086      1.1  lukem 
   1087      1.8  lukem 	error = bus_dmamap_load(sc->sc_dmat, sc->tbd_map, sc->tbd_list,
   1088      1.1  lukem 	    IPW_TBD_SZ, NULL, BUS_DMA_NOWAIT);
   1089      1.1  lukem 	if (error != 0) {
   1090      1.1  lukem 		errmsg = "could not load tbd dma memory";
   1091      1.1  lukem 		goto fail;
   1092      1.1  lukem 	}
   1093      1.1  lukem 
   1094      1.8  lukem 	sc->stbd_list = malloc(IPW_NTBD * sizeof (struct ipw_soft_bd),
   1095      1.1  lukem 	    M_DEVBUF, M_NOWAIT);
   1096      1.1  lukem 	if (sc->stbd_list == NULL) {
   1097      1.1  lukem 		errmsg = "could not allocate soft tbd";
   1098      1.1  lukem 		error = ENOMEM;
   1099      1.1  lukem 		goto fail;
   1100      1.1  lukem 	}
   1101      1.1  lukem 	sbd = sc->stbd_list;
   1102      1.1  lukem 	bd = sc->tbd_list;
   1103      1.1  lukem 	for (i = 0; i < IPW_NTBD; i++, sbd++, bd++) {
   1104      1.1  lukem 		sbd->type = IPW_SBD_TYPE_NOASSOC;
   1105      1.1  lukem 		sbd->bd = bd;
   1106      1.1  lukem 	}
   1107      1.1  lukem 
   1108      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_TX_BD_BASE, sc->tbd_map->dm_segs[0].ds_addr);
   1109      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_TX_BD_SIZE, IPW_NTBD);
   1110      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_TX_READ_INDEX, 0);
   1111      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE_INDEX, 0);
   1112      1.1  lukem 	sc->txold = IPW_NTBD - 1; /* latest bd index ack'ed by firmware */
   1113  1.8.4.1   kent 	sc->txfree = IPW_NTBD; /* number of descriptors free */
   1114      1.1  lukem 	sc->txcur = 0; /* bd index to write to */
   1115      1.1  lukem 
   1116      1.1  lukem 	/* Allocate a DMA-able command */
   1117      1.8  lukem 	error = bus_dmamap_create(sc->sc_dmat, sizeof (struct ipw_cmd), 1,
   1118      1.1  lukem 	    sizeof (struct ipw_cmd), 0, BUS_DMA_NOWAIT, &sc->cmd_map);
   1119      1.1  lukem 	if (error != 0) {
   1120      1.1  lukem 		errmsg = "could not create cmd dma map";
   1121      1.1  lukem 		goto fail;
   1122      1.1  lukem 	}
   1123      1.1  lukem 
   1124      1.8  lukem 	error = bus_dmamem_alloc(sc->sc_dmat, sizeof (struct ipw_cmd),
   1125      1.1  lukem 	    PAGE_SIZE, 0, &sc->cmd_seg, 1, &nsegs, BUS_DMA_NOWAIT);
   1126      1.1  lukem 	if (error != 0) {
   1127      1.1  lukem 		errmsg = "could not allocate cmd dma memory";
   1128      1.1  lukem 		goto fail;
   1129      1.1  lukem 	}
   1130      1.1  lukem 
   1131      1.8  lukem 	error = bus_dmamem_map(sc->sc_dmat, &sc->cmd_seg, nsegs,
   1132      1.1  lukem 	    sizeof (struct ipw_cmd), (caddr_t *)&sc->cmd, BUS_DMA_NOWAIT);
   1133      1.1  lukem 	if (error != 0) {
   1134      1.1  lukem 		errmsg = "could not map cmd dma memory";
   1135      1.1  lukem 		goto fail;
   1136      1.1  lukem 	}
   1137      1.1  lukem 
   1138      1.1  lukem 	/* Allocate a pool of DMA-able headers */
   1139      1.1  lukem 	sc->shdr_list = malloc(IPW_NDATA * sizeof (struct ipw_soft_hdr),
   1140      1.1  lukem 	    M_DEVBUF, M_NOWAIT);
   1141      1.1  lukem 	if (sc->shdr_list == NULL) {
   1142      1.1  lukem 		errmsg = "could not allocate soft hdr";
   1143      1.1  lukem 		error = ENOMEM;
   1144      1.1  lukem 		goto fail;
   1145      1.1  lukem 	}
   1146      1.1  lukem 	TAILQ_INIT(&sc->sc_free_shdr);
   1147      1.1  lukem 	for (i = 0, shdr = sc->shdr_list; i < IPW_NDATA; i++, shdr++) {
   1148      1.8  lukem 		error = bus_dmamap_create(sc->sc_dmat,
   1149      1.8  lukem 		    sizeof (struct ipw_soft_hdr), 1,
   1150      1.8  lukem 		    sizeof (struct ipw_soft_hdr), 0, BUS_DMA_NOWAIT,
   1151      1.1  lukem 		    &shdr->map);
   1152      1.1  lukem 		if (error != 0) {
   1153      1.1  lukem 			errmsg = "could not create hdr dma map";
   1154      1.1  lukem 			goto fail;
   1155      1.1  lukem 		}
   1156      1.1  lukem 		TAILQ_INSERT_TAIL(&sc->sc_free_shdr, shdr, next);
   1157      1.1  lukem 	}
   1158      1.1  lukem 
   1159      1.1  lukem 	/* Allocate a pool of DMA-able buffers */
   1160      1.1  lukem 	sc->tx_sbuf_list = malloc(IPW_NDATA * sizeof (struct ipw_soft_buf),
   1161      1.1  lukem 	    M_DEVBUF, M_NOWAIT);
   1162      1.1  lukem 	if (sc->tx_sbuf_list == NULL) {
   1163      1.1  lukem 		errmsg = "could not allocate soft txbuf";
   1164      1.1  lukem 		error = ENOMEM;
   1165      1.1  lukem 		goto fail;
   1166      1.1  lukem 	}
   1167      1.1  lukem 	TAILQ_INIT(&sc->sc_free_sbuf);
   1168      1.1  lukem 	for (i = 0, sbuf = sc->tx_sbuf_list; i < IPW_NDATA; i++, sbuf++) {
   1169      1.8  lukem 		error = bus_dmamap_create(sc->sc_dmat, IPW_NDATA * MCLBYTES,
   1170      1.1  lukem 		    IPW_NDATA, MCLBYTES, 0, BUS_DMA_NOWAIT, &sbuf->map);
   1171      1.1  lukem 		if (error != 0) {
   1172      1.1  lukem 			errmsg = "could not create txbuf dma map";
   1173      1.1  lukem 			goto fail;
   1174      1.1  lukem 		}
   1175      1.1  lukem 		TAILQ_INSERT_TAIL(&sc->sc_free_sbuf, sbuf, next);
   1176      1.1  lukem 	}
   1177      1.1  lukem 
   1178      1.1  lukem 	return 0;
   1179      1.1  lukem 
   1180      1.1  lukem fail:	aprint_error("%s: %s\n", sc->sc_dev.dv_xname, errmsg);
   1181      1.1  lukem 	ipw_tx_stop(sc);
   1182      1.1  lukem 
   1183      1.1  lukem 	return error;
   1184      1.1  lukem }
   1185      1.1  lukem 
   1186      1.1  lukem static void
   1187      1.1  lukem ipw_tx_stop(struct ipw_softc *sc)
   1188      1.1  lukem {
   1189      1.1  lukem 	struct ipw_soft_hdr *shdr;
   1190      1.1  lukem 	struct ipw_soft_buf *sbuf;
   1191      1.1  lukem 	int i;
   1192      1.1  lukem 
   1193      1.1  lukem 	if (sc->tbd_map != NULL) {
   1194      1.1  lukem 		if (sc->tbd_list != NULL) {
   1195      1.1  lukem 			bus_dmamap_unload(sc->sc_dmat, sc->tbd_map);
   1196      1.8  lukem 			bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->tbd_list,
   1197      1.1  lukem 			    IPW_TBD_SZ);
   1198      1.1  lukem 			bus_dmamem_free(sc->sc_dmat, &sc->tbd_seg, 1);
   1199      1.1  lukem 			sc->tbd_list = NULL;
   1200      1.1  lukem 		}
   1201      1.1  lukem 		bus_dmamap_destroy(sc->sc_dmat, sc->tbd_map);
   1202      1.1  lukem 		sc->tbd_map = NULL;
   1203      1.1  lukem 	}
   1204      1.1  lukem 
   1205      1.1  lukem 	if (sc->stbd_list != NULL) {
   1206      1.1  lukem 		for (i = 0; i < IPW_NTBD; i++)
   1207      1.1  lukem 			ipw_release_sbd(sc, &sc->stbd_list[i]);
   1208      1.1  lukem 		free(sc->stbd_list, M_DEVBUF);
   1209      1.1  lukem 		sc->stbd_list = NULL;
   1210      1.1  lukem 	}
   1211      1.1  lukem 
   1212      1.1  lukem 	if (sc->cmd_map != NULL) {
   1213      1.1  lukem 		if (sc->cmd != NULL) {
   1214      1.8  lukem 			bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->cmd,
   1215      1.1  lukem 			    sizeof (struct ipw_cmd));
   1216      1.1  lukem 			bus_dmamem_free(sc->sc_dmat, &sc->cmd_seg, 1);
   1217      1.1  lukem 			sc->cmd = NULL;
   1218      1.1  lukem 		}
   1219      1.1  lukem 		bus_dmamap_destroy(sc->sc_dmat, sc->cmd_map);
   1220      1.1  lukem 		sc->cmd_map = NULL;
   1221      1.1  lukem 	}
   1222      1.1  lukem 
   1223      1.1  lukem 	if (sc->shdr_list != NULL) {
   1224      1.1  lukem 		TAILQ_FOREACH(shdr, &sc->sc_free_shdr, next)
   1225      1.1  lukem 			bus_dmamap_destroy(sc->sc_dmat, shdr->map);
   1226      1.1  lukem 		free(sc->shdr_list, M_DEVBUF);
   1227      1.1  lukem 		sc->shdr_list = NULL;
   1228      1.1  lukem 	}
   1229      1.1  lukem 
   1230      1.1  lukem 
   1231      1.1  lukem 	if (sc->tx_sbuf_list != NULL) {
   1232      1.1  lukem 		TAILQ_FOREACH(sbuf, &sc->sc_free_sbuf, next)
   1233      1.1  lukem 			bus_dmamap_destroy(sc->sc_dmat, sbuf->map);
   1234      1.1  lukem 		free(sc->tx_sbuf_list, M_DEVBUF);
   1235      1.1  lukem 		sc->tx_sbuf_list = NULL;
   1236      1.1  lukem 	}
   1237      1.1  lukem }
   1238      1.1  lukem 
   1239      1.1  lukem static int
   1240      1.1  lukem ipw_rx_init(struct ipw_softc *sc)
   1241      1.1  lukem {
   1242      1.1  lukem 	char *errmsg;
   1243      1.1  lukem 	struct ipw_bd *bd;
   1244      1.1  lukem 	struct ipw_soft_bd *sbd;
   1245      1.1  lukem 	struct ipw_soft_buf *sbuf;
   1246      1.1  lukem 	int error, i, nsegs;
   1247      1.1  lukem 
   1248      1.1  lukem 	/* Allocate reception buffer descriptors */
   1249      1.8  lukem 	error = bus_dmamap_create(sc->sc_dmat, IPW_RBD_SZ, 1, IPW_RBD_SZ, 0,
   1250      1.1  lukem 	    BUS_DMA_NOWAIT, &sc->rbd_map);
   1251      1.1  lukem 	if (error != 0) {
   1252      1.1  lukem 		errmsg = "could not create rbd dma map";
   1253      1.1  lukem 		goto fail;
   1254      1.1  lukem 	}
   1255      1.1  lukem 
   1256      1.8  lukem 	error = bus_dmamem_alloc(sc->sc_dmat, IPW_RBD_SZ, PAGE_SIZE, 0,
   1257      1.1  lukem 	    &sc->rbd_seg, 1, &nsegs, BUS_DMA_NOWAIT);
   1258      1.1  lukem 	if (error != 0) {
   1259      1.1  lukem 		errmsg = "could not allocate rbd dma memory";
   1260      1.1  lukem 		goto fail;
   1261      1.1  lukem 	}
   1262      1.1  lukem 
   1263      1.8  lukem 	error = bus_dmamem_map(sc->sc_dmat, &sc->rbd_seg, nsegs, IPW_RBD_SZ,
   1264      1.1  lukem 	    (caddr_t *)&sc->rbd_list, BUS_DMA_NOWAIT);
   1265      1.1  lukem 	if (error != 0) {
   1266      1.1  lukem 		errmsg = "could not map rbd dma memory";
   1267      1.1  lukem 		goto fail;
   1268      1.1  lukem 	}
   1269      1.1  lukem 
   1270      1.8  lukem 	error = bus_dmamap_load(sc->sc_dmat, sc->rbd_map, sc->rbd_list,
   1271      1.1  lukem 	    IPW_RBD_SZ, NULL, BUS_DMA_NOWAIT);
   1272      1.1  lukem 	if (error != 0) {
   1273      1.1  lukem 		errmsg = "could not load rbd dma memory";
   1274      1.1  lukem 		goto fail;
   1275      1.1  lukem 	}
   1276      1.1  lukem 
   1277      1.8  lukem 	sc->srbd_list = malloc(IPW_NRBD * sizeof (struct ipw_soft_bd),
   1278      1.1  lukem 	    M_DEVBUF, M_NOWAIT);
   1279      1.1  lukem 	if (sc->srbd_list == NULL) {
   1280      1.1  lukem 		errmsg = "could not allocate soft rbd";
   1281      1.1  lukem 		error = ENOMEM;
   1282      1.1  lukem 		goto fail;
   1283      1.1  lukem 	}
   1284      1.1  lukem 	sbd = sc->srbd_list;
   1285      1.1  lukem 	bd = sc->rbd_list;
   1286      1.1  lukem 	for (i = 0; i < IPW_NRBD; i++, sbd++, bd++) {
   1287      1.1  lukem 		sbd->type = IPW_SBD_TYPE_NOASSOC;
   1288      1.1  lukem 		sbd->bd = bd;
   1289      1.1  lukem 	}
   1290      1.1  lukem 
   1291      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_RX_BD_BASE, sc->rbd_map->dm_segs[0].ds_addr);
   1292      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_RX_BD_SIZE, IPW_NRBD);
   1293      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_RX_READ_INDEX, 0);
   1294      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_RX_WRITE_INDEX, IPW_NRBD - 1);
   1295      1.1  lukem 	sc->rxcur = IPW_NRBD - 1; /* latest bd index I've read */
   1296      1.1  lukem 
   1297      1.1  lukem 	/* Allocate status descriptors */
   1298      1.8  lukem 	error = bus_dmamap_create(sc->sc_dmat, IPW_STATUS_SZ, 1, IPW_STATUS_SZ,
   1299      1.1  lukem 	    0, BUS_DMA_NOWAIT, &sc->status_map);
   1300      1.1  lukem 	if (error != 0) {
   1301      1.1  lukem 		errmsg = "could not create status dma map";
   1302      1.1  lukem 		goto fail;
   1303      1.1  lukem 	}
   1304      1.1  lukem 
   1305      1.8  lukem 	error = bus_dmamem_alloc(sc->sc_dmat, IPW_STATUS_SZ, PAGE_SIZE, 0,
   1306      1.1  lukem 	    &sc->status_seg, 1, &nsegs, BUS_DMA_NOWAIT);
   1307      1.1  lukem 	if (error != 0) {
   1308      1.1  lukem 		errmsg = "could not allocate status dma memory";
   1309      1.1  lukem 		goto fail;
   1310      1.1  lukem 	}
   1311      1.1  lukem 
   1312      1.8  lukem 	error = bus_dmamem_map(sc->sc_dmat, &sc->status_seg, nsegs,
   1313      1.1  lukem 	    IPW_STATUS_SZ, (caddr_t *)&sc->status_list, BUS_DMA_NOWAIT);
   1314      1.1  lukem 	if (error != 0) {
   1315      1.1  lukem 		errmsg = "could not map status dma memory";
   1316      1.1  lukem 		goto fail;
   1317      1.1  lukem 	}
   1318      1.1  lukem 
   1319      1.8  lukem 	error = bus_dmamap_load(sc->sc_dmat, sc->status_map, sc->status_list,
   1320      1.1  lukem 	    IPW_STATUS_SZ, NULL, BUS_DMA_NOWAIT);
   1321      1.1  lukem 	if (error != 0) {
   1322      1.1  lukem 		errmsg = "could not load status dma memory";
   1323      1.1  lukem 		goto fail;
   1324      1.1  lukem 	}
   1325      1.1  lukem 
   1326      1.8  lukem 	CSR_WRITE_4(sc, IPW_CSR_RX_STATUS_BASE,
   1327      1.1  lukem 	    sc->status_map->dm_segs[0].ds_addr);
   1328      1.1  lukem 
   1329      1.1  lukem 	sc->rx_sbuf_list = malloc(IPW_NRBD * sizeof (struct ipw_soft_buf),
   1330      1.1  lukem 	    M_DEVBUF, M_NOWAIT);
   1331      1.1  lukem 	if (sc->rx_sbuf_list == NULL) {
   1332      1.1  lukem 		errmsg = "could not allocate soft rxbuf";
   1333      1.1  lukem 		error = ENOMEM;
   1334      1.1  lukem 		goto fail;
   1335      1.1  lukem 	}
   1336      1.1  lukem 
   1337      1.1  lukem 	sbuf = sc->rx_sbuf_list;
   1338      1.1  lukem 	sbd = sc->srbd_list;
   1339      1.1  lukem 	for (i = 0; i < IPW_NRBD; i++, sbuf++, sbd++) {
   1340      1.1  lukem 
   1341      1.1  lukem 		MGETHDR(sbuf->m, M_DONTWAIT, MT_DATA);
   1342      1.1  lukem 		if (sbuf->m == NULL) {
   1343      1.1  lukem 			errmsg = "could not allocate rx mbuf";
   1344      1.1  lukem 			error = ENOMEM;
   1345      1.1  lukem 			goto fail;
   1346      1.1  lukem 		}
   1347      1.1  lukem 		MCLGET(sbuf->m, M_DONTWAIT);
   1348      1.1  lukem 		if (!(sbuf->m->m_flags & M_EXT)) {
   1349      1.1  lukem 			m_freem(sbuf->m);
   1350      1.1  lukem 			errmsg = "could not allocate rx mbuf cluster";
   1351      1.1  lukem 			error = ENOMEM;
   1352      1.1  lukem 			goto fail;
   1353      1.1  lukem 		}
   1354      1.1  lukem 
   1355      1.8  lukem 		error = bus_dmamap_create(sc->sc_dmat, IPW_NRBD * MCLBYTES,
   1356      1.1  lukem 		    IPW_NRBD, MCLBYTES, 0, BUS_DMA_NOWAIT, &sbuf->map);
   1357      1.1  lukem 		if (error != 0) {
   1358      1.1  lukem 			m_freem(sbuf->m);
   1359      1.1  lukem 			errmsg = "could not create rxbuf dma map";
   1360      1.1  lukem 			goto fail;
   1361      1.1  lukem 		}
   1362      1.8  lukem 		error = bus_dmamap_load(sc->sc_dmat, sbuf->map,
   1363      1.1  lukem 		    mtod(sbuf->m, void *), MCLBYTES, NULL, BUS_DMA_NOWAIT);
   1364      1.1  lukem 		if (error != 0) {
   1365      1.1  lukem 			bus_dmamap_destroy(sc->sc_dmat, sbuf->map);
   1366      1.1  lukem 			m_freem(sbuf->m);
   1367      1.1  lukem 			errmsg = "could not map rxbuf dma memory";
   1368      1.1  lukem 			goto fail;
   1369      1.1  lukem 		}
   1370      1.1  lukem 		sbd->type = IPW_SBD_TYPE_DATA;
   1371      1.1  lukem 		sbd->priv = sbuf;
   1372      1.1  lukem 		sbd->bd->physaddr = htole32(sbuf->map->dm_segs[0].ds_addr);
   1373      1.1  lukem 		sbd->bd->len = htole32(MCLBYTES);
   1374      1.1  lukem 	}
   1375      1.1  lukem 
   1376      1.1  lukem 	return 0;
   1377      1.1  lukem 
   1378      1.1  lukem fail:	aprint_error("%s: %s\n", sc->sc_dev.dv_xname, errmsg);
   1379      1.1  lukem 	ipw_rx_stop(sc);
   1380      1.1  lukem 
   1381      1.1  lukem 	return error;
   1382      1.1  lukem }
   1383      1.1  lukem 
   1384      1.1  lukem static void
   1385      1.1  lukem ipw_rx_stop(struct ipw_softc *sc)
   1386      1.1  lukem {
   1387      1.1  lukem 	struct ipw_soft_bd *sbd;
   1388      1.1  lukem 	struct ipw_soft_buf *sbuf;
   1389      1.1  lukem 	int i;
   1390      1.1  lukem 
   1391      1.1  lukem 	if (sc->rbd_map != NULL) {
   1392      1.1  lukem 		if (sc->rbd_list != NULL) {
   1393      1.1  lukem 			bus_dmamap_unload(sc->sc_dmat, sc->rbd_map);
   1394      1.8  lukem 			bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->rbd_list,
   1395      1.1  lukem 			    IPW_RBD_SZ);
   1396      1.1  lukem 			bus_dmamem_free(sc->sc_dmat, &sc->rbd_seg, 1);
   1397      1.1  lukem 			sc->rbd_list = NULL;
   1398      1.1  lukem 		}
   1399      1.1  lukem 		bus_dmamap_destroy(sc->sc_dmat, sc->rbd_map);
   1400      1.1  lukem 		sc->rbd_map = NULL;
   1401      1.1  lukem 	}
   1402      1.1  lukem 
   1403      1.1  lukem 	if (sc->status_map != NULL) {
   1404      1.1  lukem 		if (sc->status_list != NULL) {
   1405      1.1  lukem 			bus_dmamap_unload(sc->sc_dmat, sc->status_map);
   1406      1.8  lukem 			bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->status_list,
   1407      1.1  lukem 			    IPW_STATUS_SZ);
   1408      1.1  lukem 			bus_dmamem_free(sc->sc_dmat, &sc->status_seg, 1);
   1409      1.1  lukem 			sc->status_list = NULL;
   1410      1.1  lukem 		}
   1411      1.1  lukem 		bus_dmamap_destroy(sc->sc_dmat, sc->status_map);
   1412      1.1  lukem 		sc->status_map = NULL;
   1413      1.1  lukem 	}
   1414      1.1  lukem 
   1415      1.1  lukem 	if (sc->srbd_list != NULL) {
   1416      1.1  lukem 		for (i = 0, sbd = sc->srbd_list; i < IPW_NRBD; i++, sbd++) {
   1417      1.1  lukem 			if (sbd->type == IPW_SBD_TYPE_NOASSOC)
   1418      1.1  lukem 				continue;
   1419      1.1  lukem 
   1420      1.1  lukem 			sbuf = sbd->priv;
   1421      1.1  lukem 			bus_dmamap_unload(sc->sc_dmat, sbuf->map);
   1422      1.1  lukem 			bus_dmamap_destroy(sc->sc_dmat, sbuf->map);
   1423      1.1  lukem 			m_freem(sbuf->m);
   1424      1.1  lukem 		}
   1425      1.1  lukem 		free(sc->srbd_list, M_DEVBUF);
   1426      1.1  lukem 		sc->srbd_list = NULL;
   1427      1.1  lukem 	}
   1428      1.1  lukem 
   1429      1.1  lukem 	if (sc->rx_sbuf_list != NULL) {
   1430      1.1  lukem 		free(sc->rx_sbuf_list, M_DEVBUF);
   1431      1.1  lukem 		sc->rx_sbuf_list = NULL;
   1432      1.1  lukem 	}
   1433      1.1  lukem }
   1434      1.1  lukem 
   1435      1.1  lukem static void
   1436      1.1  lukem ipw_reset(struct ipw_softc *sc)
   1437      1.1  lukem {
   1438      1.1  lukem 	struct ifnet *ifp = &sc->sc_ic.ic_if;
   1439      1.1  lukem 	int ntries;
   1440      1.1  lukem 
   1441      1.1  lukem 	ipw_stop(ifp, 1);
   1442      1.1  lukem 
   1443      1.1  lukem 	if (sc->flags & IPW_FLAG_FW_INITED) {
   1444      1.1  lukem 		ipw_cmd(sc, IPW_CMD_DISABLE_PHY, NULL, 0);
   1445      1.1  lukem 		ipw_cmd(sc, IPW_CMD_PREPARE_POWER_DOWN, NULL, 0);
   1446      1.1  lukem 
   1447      1.1  lukem 		sc->flags &= ~IPW_FLAG_FW_INITED;
   1448      1.1  lukem 	}
   1449      1.1  lukem 
   1450      1.1  lukem 	/* Disable interrupts */
   1451      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
   1452      1.1  lukem 
   1453      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_STOP_MASTER);
   1454      1.1  lukem 	for (ntries = 0; ntries < 5; ntries++) {
   1455      1.1  lukem 		if (CSR_READ_4(sc, IPW_CSR_RST) & IPW_RST_MASTER_DISABLED)
   1456      1.1  lukem 			break;
   1457      1.1  lukem 		DELAY(10);
   1458      1.1  lukem 	}
   1459      1.1  lukem 
   1460      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_SW_RESET);
   1461      1.1  lukem 
   1462      1.1  lukem 	ipw_rx_stop(sc);
   1463      1.1  lukem 	ipw_tx_stop(sc);
   1464      1.1  lukem 
   1465      1.1  lukem 	ifp->if_flags &= ~IFF_UP;
   1466      1.1  lukem }
   1467      1.1  lukem 
   1468      1.1  lukem static int
   1469      1.1  lukem ipw_clock_sync(struct ipw_softc *sc)
   1470      1.1  lukem {
   1471      1.1  lukem 	int ntries;
   1472      1.1  lukem 	u_int32_t r;
   1473      1.1  lukem 
   1474      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_SW_RESET);
   1475      1.1  lukem 	for (ntries = 0; ntries < 1000; ntries++) {
   1476      1.1  lukem 		if (CSR_READ_4(sc, IPW_CSR_RST) & IPW_RST_PRINCETON_RESET)
   1477      1.1  lukem 			break;
   1478      1.1  lukem 		DELAY(10);
   1479      1.1  lukem 	}
   1480      1.1  lukem 	if (ntries == 1000)
   1481      1.1  lukem 		return EIO;
   1482      1.1  lukem 
   1483      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_CTL, IPW_CTL_INIT_DONE);
   1484      1.1  lukem 	for (ntries = 0; ntries < 1000; ntries++) {
   1485      1.1  lukem 		if ((r = CSR_READ_4(sc, IPW_CSR_CTL)) & IPW_CTL_CLOCK_READY)
   1486      1.1  lukem 			break;
   1487      1.1  lukem 		DELAY(200);
   1488      1.1  lukem 	}
   1489      1.1  lukem 	if (ntries == 1000)
   1490      1.1  lukem 		return EIO;
   1491      1.1  lukem 
   1492      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_CTL, r | IPW_CTL_ALLOW_STANDBY);
   1493      1.1  lukem 
   1494      1.1  lukem 	return 0;
   1495      1.1  lukem }
   1496      1.1  lukem 
   1497      1.1  lukem static int
   1498      1.1  lukem ipw_load_ucode(struct ipw_softc *sc, u_char *uc, int size)
   1499      1.1  lukem {
   1500      1.1  lukem 	int ntries;
   1501      1.1  lukem 
   1502      1.1  lukem 	MEM_WRITE_2(sc, 0x220000, 0x0703);
   1503      1.1  lukem 	MEM_WRITE_2(sc, 0x220000, 0x0707);
   1504      1.1  lukem 
   1505      1.1  lukem 	MEM_WRITE_1(sc, 0x210014, 0x72);
   1506      1.1  lukem 	MEM_WRITE_1(sc, 0x210014, 0x72);
   1507      1.1  lukem 
   1508      1.1  lukem 	MEM_WRITE_1(sc, 0x210000, 0x40);
   1509      1.1  lukem 	MEM_WRITE_1(sc, 0x210000, 0x00);
   1510      1.1  lukem 	MEM_WRITE_1(sc, 0x210000, 0x40);
   1511      1.1  lukem 
   1512      1.1  lukem 	MEM_WRITE_MULTI_1(sc, 0x210010, uc, size);
   1513      1.1  lukem 
   1514      1.1  lukem 	MEM_WRITE_1(sc, 0x210000, 0x00);
   1515      1.1  lukem 	MEM_WRITE_1(sc, 0x210000, 0x00);
   1516      1.1  lukem 	MEM_WRITE_1(sc, 0x210000, 0x80);
   1517      1.1  lukem 
   1518      1.1  lukem 	MEM_WRITE_2(sc, 0x220000, 0x0703);
   1519      1.1  lukem 	MEM_WRITE_2(sc, 0x220000, 0x0707);
   1520      1.1  lukem 
   1521      1.1  lukem 	MEM_WRITE_1(sc, 0x210014, 0x72);
   1522      1.1  lukem 	MEM_WRITE_1(sc, 0x210014, 0x72);
   1523      1.1  lukem 
   1524      1.1  lukem 	MEM_WRITE_1(sc, 0x210000, 0x00);
   1525      1.1  lukem 	MEM_WRITE_1(sc, 0x210000, 0x80);
   1526      1.1  lukem 
   1527      1.1  lukem 	for (ntries = 0; ntries < 10; ntries++) {
   1528      1.1  lukem 		if (MEM_READ_1(sc, 0x210000) & 1)
   1529      1.1  lukem 			break;
   1530      1.1  lukem 		DELAY(10);
   1531      1.1  lukem 	}
   1532      1.1  lukem 	if (ntries == 10)
   1533      1.1  lukem 		return EIO;
   1534      1.1  lukem 
   1535      1.1  lukem 	return 0;
   1536      1.1  lukem }
   1537      1.1  lukem 
   1538      1.1  lukem /* set of macros to handle unaligned little endian data in firmware image */
   1539      1.1  lukem #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
   1540      1.1  lukem #define GETLE16(p) ((p)[0] | (p)[1] << 8)
   1541      1.1  lukem static int
   1542      1.1  lukem ipw_load_firmware(struct ipw_softc *sc, u_char *fw, int size)
   1543      1.1  lukem {
   1544      1.1  lukem 	u_char *p, *end;
   1545      1.1  lukem 	u_int32_t addr;
   1546      1.1  lukem 	u_int16_t len;
   1547      1.1  lukem 
   1548      1.1  lukem 	p = fw;
   1549      1.1  lukem 	end = fw + size;
   1550      1.1  lukem 	while (p < end) {
   1551      1.1  lukem 		if (p + 6 > end)
   1552      1.1  lukem 			return EINVAL;
   1553      1.1  lukem 
   1554      1.1  lukem 		addr = GETLE32(p);
   1555      1.1  lukem 		p += 4;
   1556      1.1  lukem 		len = GETLE16(p);
   1557      1.1  lukem 		p += 2;
   1558      1.1  lukem 
   1559      1.1  lukem 		if (p + len > end)
   1560      1.1  lukem 			return EINVAL;
   1561      1.1  lukem 
   1562      1.1  lukem 		ipw_write_mem_1(sc, addr, p, len);
   1563      1.1  lukem 		p += len;
   1564      1.1  lukem 	}
   1565      1.1  lukem 	return 0;
   1566      1.1  lukem }
   1567      1.1  lukem 
   1568      1.1  lukem static int
   1569      1.1  lukem ipw_firmware_init(struct ipw_softc *sc, u_char *data)
   1570      1.1  lukem {
   1571      1.1  lukem 	struct ieee80211com *ic = &sc->sc_ic;
   1572      1.1  lukem 	struct ifnet *ifp = &ic->ic_if;
   1573      1.1  lukem 	struct ipw_fw_hdr hdr;
   1574      1.1  lukem 	u_int32_t r, len, fw_size, uc_size;
   1575      1.1  lukem 	u_char *fw, *uc;
   1576      1.1  lukem 	int error;
   1577      1.1  lukem 
   1578      1.1  lukem 	ipw_reset(sc);
   1579      1.1  lukem 
   1580      1.1  lukem 	if ((error = copyin(data, &hdr, sizeof hdr)) != 0)
   1581      1.1  lukem 		goto fail1;
   1582      1.1  lukem 
   1583      1.1  lukem 	fw_size = le32toh(hdr.fw_size);
   1584      1.1  lukem 	uc_size = le32toh(hdr.uc_size);
   1585      1.1  lukem 	data += sizeof hdr;
   1586      1.1  lukem 
   1587      1.1  lukem 	if ((fw = malloc(fw_size, M_DEVBUF, M_NOWAIT)) == NULL) {
   1588      1.1  lukem 		error = ENOMEM;
   1589      1.1  lukem 		goto fail1;
   1590      1.1  lukem 	}
   1591      1.1  lukem 
   1592      1.1  lukem 	if ((error = copyin(data, fw, fw_size)) != 0)
   1593      1.1  lukem 		goto fail2;
   1594      1.1  lukem 
   1595      1.1  lukem 	data += fw_size;
   1596      1.1  lukem 
   1597      1.1  lukem 	if ((uc = malloc(uc_size, M_DEVBUF, M_NOWAIT)) == NULL) {
   1598      1.1  lukem 		error = ENOMEM;
   1599      1.1  lukem 		goto fail2;
   1600      1.1  lukem 	}
   1601      1.1  lukem 
   1602      1.1  lukem 	if ((error = copyin(data, uc, uc_size)) != 0)
   1603      1.1  lukem 		goto fail3;
   1604      1.1  lukem 
   1605      1.1  lukem 	if ((error = ipw_clock_sync(sc)) != 0) {
   1606      1.1  lukem 		aprint_error("%s: clock synchronization failed\n",
   1607      1.1  lukem 		    sc->sc_dev.dv_xname);
   1608      1.1  lukem 		goto fail3;
   1609      1.1  lukem 	}
   1610      1.1  lukem 
   1611      1.1  lukem 	MEM_WRITE_4(sc, 0x003000e0, 0x80000000);
   1612      1.1  lukem 
   1613      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_RST, 0);
   1614      1.1  lukem 
   1615      1.1  lukem 	if ((error = ipw_load_ucode(sc, uc, uc_size)) != 0) {
   1616      1.1  lukem 		aprint_error("%s: could not load microcode\n",
   1617      1.1  lukem 		    sc->sc_dev.dv_xname);
   1618      1.1  lukem 		goto fail3;
   1619      1.1  lukem 	}
   1620      1.1  lukem 
   1621      1.1  lukem 	MEM_WRITE_4(sc, 0x003000e0, 0);
   1622      1.1  lukem 
   1623      1.1  lukem 	if ((error = ipw_clock_sync(sc)) != 0) {
   1624      1.1  lukem 		aprint_error("%s: clock synchronization failed\n",
   1625      1.1  lukem 		    sc->sc_dev.dv_xname);
   1626      1.1  lukem 		goto fail3;
   1627      1.1  lukem 	}
   1628      1.1  lukem 
   1629      1.1  lukem 	if ((error = ipw_load_firmware(sc, fw, fw_size))) {
   1630      1.8  lukem 		aprint_error("%s: could not load firmware\n",
   1631      1.1  lukem 		    sc->sc_dev.dv_xname);
   1632      1.1  lukem 		goto fail3;
   1633      1.1  lukem 	}
   1634      1.1  lukem 
   1635      1.1  lukem 	ipw_zero_mem_4(sc, 0x0002f200, 196);
   1636      1.1  lukem 	ipw_zero_mem_4(sc, 0x0002f610, 8);
   1637      1.1  lukem 	ipw_zero_mem_4(sc, 0x0002fa00, 8);
   1638      1.1  lukem 	ipw_zero_mem_4(sc, 0x0002fc00, 4);
   1639      1.1  lukem 	ipw_zero_mem_4(sc, 0x0002ff80, 32);
   1640      1.1  lukem 
   1641      1.1  lukem 	if ((error = ipw_rx_init(sc)) != 0) {
   1642      1.1  lukem 		aprint_error("%s: could not initialize rx queue\n",
   1643      1.1  lukem 		    sc->sc_dev.dv_xname);
   1644      1.1  lukem 		goto fail3;
   1645      1.1  lukem 	}
   1646      1.1  lukem 
   1647      1.1  lukem 	if ((error = ipw_tx_init(sc)) != 0) {
   1648      1.1  lukem 		aprint_error("%s: could not initialize tx queue\n",
   1649      1.1  lukem 		    sc->sc_dev.dv_xname);
   1650      1.1  lukem 		goto fail3;
   1651      1.1  lukem 	}
   1652      1.1  lukem 
   1653      1.8  lukem 	CSR_WRITE_4(sc, IPW_CSR_IO, IPW_IO_GPIO1_ENABLE | IPW_IO_GPIO3_MASK |
   1654      1.1  lukem 	    IPW_IO_LED_OFF);
   1655      1.1  lukem 
   1656      1.1  lukem 	/* Enable interrupts */
   1657      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
   1658      1.1  lukem 
   1659      1.1  lukem 	/* Let's go! */
   1660      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_RST, 0);
   1661      1.1  lukem 
   1662      1.1  lukem 	/* Wait at most 5 seconds for firmware initialization to complete */
   1663      1.1  lukem 	if ((error = tsleep(sc, 0, "ipwinit", 5 * hz)) != 0) {
   1664      1.1  lukem 		aprint_error("%s: timeout waiting for firmware initialization "
   1665      1.1  lukem 		    "to complete\n", sc->sc_dev.dv_xname);
   1666      1.1  lukem 		goto fail3;
   1667      1.1  lukem 	}
   1668      1.1  lukem 
   1669      1.1  lukem 	/* Firmware initialization completed */
   1670      1.1  lukem 	sc->flags |= IPW_FLAG_FW_INITED;
   1671      1.1  lukem 
   1672      1.1  lukem 	free(uc, M_DEVBUF);
   1673      1.1  lukem 	free(fw, M_DEVBUF);
   1674      1.1  lukem 
   1675      1.1  lukem 	r = CSR_READ_4(sc, IPW_CSR_IO);
   1676      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_IO, r | IPW_IO_GPIO1_MASK | IPW_IO_GPIO3_MASK);
   1677      1.1  lukem 
   1678      1.1  lukem 	/* Retrieve information tables base addresses */
   1679      1.1  lukem 	sc->table1_base = CSR_READ_4(sc, IPW_CSR_TABLE1_BASE);
   1680      1.1  lukem 	sc->table2_base = CSR_READ_4(sc, IPW_CSR_TABLE2_BASE);
   1681      1.1  lukem 
   1682      1.1  lukem 	ipw_write_table1(sc, IPW_INFO_LOCK, 0);
   1683      1.1  lukem 
   1684      1.1  lukem 	/* Retrieve adapter MAC address */
   1685      1.1  lukem 	len = IEEE80211_ADDR_LEN;
   1686      1.1  lukem 	ipw_read_table2(sc, IPW_INFO_ADAPTER_MAC, ic->ic_myaddr, &len);
   1687      1.1  lukem 
   1688      1.1  lukem 	IEEE80211_ADDR_COPY(LLADDR(ifp->if_sadl), ic->ic_myaddr);
   1689      1.1  lukem 
   1690      1.1  lukem 	return 0;
   1691      1.1  lukem 
   1692      1.1  lukem fail3:	free(uc, M_DEVBUF);
   1693      1.1  lukem fail2:	free(fw, M_DEVBUF);
   1694      1.1  lukem fail1:	ipw_reset(sc);
   1695      1.1  lukem 
   1696      1.1  lukem 	return error;
   1697      1.1  lukem }
   1698      1.1  lukem 
   1699      1.1  lukem static int
   1700      1.1  lukem ipw_config(struct ipw_softc *sc)
   1701      1.1  lukem {
   1702      1.1  lukem 	struct ieee80211com *ic = &sc->sc_ic;
   1703      1.1  lukem 	struct ifnet *ifp = &ic->ic_if;
   1704      1.1  lukem 	struct ipw_security security;
   1705      1.1  lukem 	struct ieee80211_wepkey *k;
   1706      1.1  lukem 	struct ipw_wep_key wepkey;
   1707      1.1  lukem 	struct ipw_scan_options options;
   1708      1.1  lukem 	struct ipw_configuration config;
   1709      1.1  lukem 	u_int32_t data;
   1710      1.1  lukem 	int error, i;
   1711      1.1  lukem 
   1712      1.1  lukem 	switch (ic->ic_opmode) {
   1713      1.1  lukem 	case IEEE80211_M_STA:
   1714      1.1  lukem 	case IEEE80211_M_HOSTAP:
   1715      1.1  lukem 		data = htole32(IPW_MODE_BSS);
   1716      1.1  lukem 		break;
   1717      1.1  lukem 
   1718      1.1  lukem 	case IEEE80211_M_IBSS:
   1719      1.1  lukem 	case IEEE80211_M_AHDEMO:
   1720      1.1  lukem 		data = htole32(IPW_MODE_IBSS);
   1721      1.1  lukem 		break;
   1722      1.1  lukem 
   1723      1.1  lukem 	case IEEE80211_M_MONITOR:
   1724      1.1  lukem 		data = htole32(IPW_MODE_MONITOR);
   1725      1.1  lukem 		break;
   1726      1.1  lukem 	}
   1727      1.1  lukem 	DPRINTF(("Setting adapter mode to %u\n", data));
   1728      1.1  lukem 	error = ipw_cmd(sc, IPW_CMD_SET_MODE, &data, sizeof data);
   1729      1.1  lukem 	if (error != 0)
   1730      1.1  lukem 		return error;
   1731      1.1  lukem 
   1732      1.8  lukem 	if (ic->ic_opmode == IEEE80211_M_IBSS ||
   1733      1.1  lukem 	    ic->ic_opmode == IEEE80211_M_MONITOR) {
   1734      1.1  lukem 		data = htole32(ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
   1735      1.1  lukem 		DPRINTF(("Setting adapter channel to %u\n", data));
   1736      1.1  lukem 		error = ipw_cmd(sc, IPW_CMD_SET_CHANNEL, &data, sizeof data);
   1737      1.1  lukem 		if (error != 0)
   1738      1.1  lukem 			return error;
   1739      1.1  lukem 	}
   1740      1.1  lukem 
   1741      1.5  lukem 	if (ic->ic_opmode == IEEE80211_M_MONITOR) {
   1742      1.5  lukem 		DPRINTF(("Enabling adapter\n"));
   1743      1.5  lukem 		return ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
   1744      1.5  lukem 	}
   1745      1.5  lukem 
   1746      1.5  lukem 	DPRINTF(("Setting adapter MAC to %s\n", ether_sprintf(ic->ic_myaddr)));
   1747      1.5  lukem 	IEEE80211_ADDR_COPY(LLADDR(ifp->if_sadl), ic->ic_myaddr);
   1748      1.8  lukem 	error = ipw_cmd(sc, IPW_CMD_SET_MAC_ADDRESS, ic->ic_myaddr,
   1749      1.5  lukem 	    IEEE80211_ADDR_LEN);
   1750      1.5  lukem 	if (error != 0)
   1751      1.5  lukem 		return error;
   1752      1.5  lukem 
   1753      1.8  lukem 	config.flags = htole32(IPW_CFG_BSS_MASK | IPW_CFG_IBSS_MASK |
   1754      1.1  lukem 			       IPW_CFG_PREAMBLE_LEN | IPW_CFG_802_1x_ENABLE);
   1755      1.1  lukem 	if (ic->ic_opmode == IEEE80211_M_IBSS)
   1756      1.1  lukem 		config.flags |= htole32(IPW_CFG_IBSS_AUTO_START);
   1757      1.1  lukem 	if (ifp->if_flags & IFF_PROMISC)
   1758      1.1  lukem 		config.flags |= htole32(IPW_CFG_PROMISCUOUS);
   1759      1.1  lukem 	config.channels = htole32(0x3fff); /* channels 1-14 */
   1760      1.1  lukem 	config.ibss_chan = htole32(0x7ff);
   1761      1.1  lukem 	DPRINTF(("Setting adapter configuration 0x%08x\n", config.flags));
   1762      1.1  lukem 	error = ipw_cmd(sc, IPW_CMD_SET_CONFIGURATION, &config, sizeof config);
   1763      1.1  lukem 	if (error != 0)
   1764      1.1  lukem 		return error;
   1765      1.1  lukem 
   1766      1.1  lukem 	data = htole32(0x3); /* 1, 2 */
   1767      1.1  lukem 	DPRINTF(("Setting adapter basic tx rates to 0x%x\n", data));
   1768      1.1  lukem 	error = ipw_cmd(sc, IPW_CMD_SET_BASIC_TX_RATES, &data, sizeof data);
   1769      1.1  lukem 	if (error != 0)
   1770      1.1  lukem 		return error;
   1771      1.1  lukem 
   1772      1.1  lukem 	data = htole32(0xf); /* 1, 2, 5.5, 11 */
   1773      1.1  lukem 	DPRINTF(("Setting adapter tx rates to 0x%x\n", data));
   1774      1.1  lukem 	error = ipw_cmd(sc, IPW_CMD_SET_TX_RATES, &data, sizeof data);
   1775      1.1  lukem 	if (error != 0)
   1776      1.1  lukem 		return error;
   1777      1.1  lukem 
   1778      1.1  lukem 	data = htole32(IPW_POWER_MODE_CAM);
   1779      1.1  lukem 	DPRINTF(("Setting adapter power mode to %u\n", data));
   1780      1.1  lukem 	error = ipw_cmd(sc, IPW_CMD_SET_POWER_MODE, &data, sizeof data);
   1781      1.1  lukem 	if (error != 0)
   1782      1.1  lukem 		return error;
   1783      1.1  lukem 
   1784      1.1  lukem 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
   1785      1.1  lukem 		data = htole32(ic->ic_txpower);
   1786      1.1  lukem 		DPRINTF(("Setting adapter tx power index to %u\n", data));
   1787      1.8  lukem 		error = ipw_cmd(sc, IPW_CMD_SET_TX_POWER_INDEX, &data,
   1788      1.1  lukem 		    sizeof data);
   1789      1.1  lukem 		if (error != 0)
   1790      1.1  lukem 			return error;
   1791      1.1  lukem 	}
   1792      1.1  lukem 
   1793      1.1  lukem 	data = htole32(ic->ic_rtsthreshold);
   1794      1.1  lukem 	DPRINTF(("Setting adapter RTS threshold to %u\n", data));
   1795      1.1  lukem 	error = ipw_cmd(sc, IPW_CMD_SET_RTS_THRESHOLD, &data, sizeof data);
   1796      1.1  lukem 	if (error != 0)
   1797      1.1  lukem 		return error;
   1798      1.1  lukem 
   1799      1.1  lukem 	data = htole32(ic->ic_fragthreshold);
   1800      1.1  lukem 	DPRINTF(("Setting adapter frag threshold to %u\n", data));
   1801      1.1  lukem 	error = ipw_cmd(sc, IPW_CMD_SET_FRAG_THRESHOLD, &data, sizeof data);
   1802      1.1  lukem 	if (error != 0)
   1803      1.1  lukem 		return error;
   1804      1.1  lukem 
   1805      1.1  lukem #ifdef IPW_DEBUG
   1806      1.1  lukem 	if (ipw_debug > 0) {
   1807      1.1  lukem 		printf("Setting adapter ESSID to ");
   1808      1.1  lukem 		ieee80211_print_essid(ic->ic_des_essid, ic->ic_des_esslen);
   1809      1.1  lukem 		printf("\n");
   1810      1.1  lukem 	}
   1811      1.1  lukem #endif
   1812      1.8  lukem 	error = ipw_cmd(sc, IPW_CMD_SET_ESSID, ic->ic_des_essid,
   1813      1.1  lukem 	    ic->ic_des_esslen);
   1814      1.1  lukem 	if (error != 0)
   1815      1.1  lukem 		return error;
   1816      1.1  lukem 
   1817      1.1  lukem 	/* no mandatory BSSID */
   1818      1.1  lukem 	error = ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID, NULL, 0);
   1819      1.1  lukem 	if (error != 0)
   1820      1.1  lukem 		return error;
   1821      1.1  lukem 
   1822      1.1  lukem 	if (ic->ic_flags & IEEE80211_F_DESBSSID) {
   1823      1.8  lukem 		DPRINTF(("Setting adapter desired BSSID to %s\n",
   1824      1.1  lukem 		    ether_sprintf(ic->ic_des_bssid)));
   1825      1.8  lukem 		error = ipw_cmd(sc, IPW_CMD_SET_DESIRED_BSSID,
   1826      1.1  lukem 		    ic->ic_des_bssid, IEEE80211_ADDR_LEN);
   1827      1.1  lukem 		if (error != 0)
   1828      1.1  lukem 			return error;
   1829      1.1  lukem 	}
   1830      1.1  lukem 
   1831      1.1  lukem 	security.authmode = IPW_AUTH_OPEN;
   1832      1.1  lukem 	security.ciphers = htole32(IPW_CIPHER_NONE);
   1833      1.1  lukem 	security.version = htole16(0);
   1834      1.1  lukem 	security.replay_counters_number = 0;
   1835      1.1  lukem 	security.unicast_using_group = 0;
   1836      1.1  lukem 	DPRINTF(("Setting adapter authmode to %u\n", security.authmode));
   1837      1.8  lukem 	error = ipw_cmd(sc, IPW_CMD_SET_SECURITY_INFORMATION, &security,
   1838      1.1  lukem 	    sizeof security);
   1839      1.1  lukem 	if (error != 0)
   1840      1.1  lukem 		return error;
   1841      1.1  lukem 
   1842      1.1  lukem 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
   1843      1.1  lukem 		k = ic->ic_nw_keys;
   1844      1.1  lukem 		for (i = 0; i < IEEE80211_WEP_NKID; i++, k++) {
   1845      1.1  lukem 			if (k->wk_len == 0)
   1846      1.1  lukem 				continue;
   1847      1.1  lukem 
   1848      1.1  lukem 			wepkey.idx = i;
   1849      1.1  lukem 			wepkey.len = k->wk_len;
   1850      1.1  lukem 			bzero(wepkey.key, sizeof wepkey.key);
   1851      1.1  lukem 			bcopy(k->wk_key, wepkey.key, k->wk_len);
   1852      1.8  lukem 			DPRINTF(("Setting wep key index %d len %d\n",
   1853      1.1  lukem 			    wepkey.idx, wepkey.len));
   1854      1.8  lukem 			error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY, &wepkey,
   1855      1.1  lukem 			    sizeof wepkey);
   1856      1.1  lukem 			if (error != 0)
   1857      1.1  lukem 				return error;
   1858      1.1  lukem 		}
   1859      1.1  lukem 
   1860      1.1  lukem 		data = htole32(ic->ic_wep_txkey);
   1861      1.1  lukem 		DPRINTF(("Setting adapter tx key index to %u\n", data));
   1862      1.8  lukem 		error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY_INDEX, &data,
   1863      1.1  lukem 		    sizeof data);
   1864      1.1  lukem 		if (error != 0)
   1865      1.1  lukem 			return error;
   1866      1.1  lukem 	}
   1867      1.1  lukem 
   1868      1.1  lukem 	data = htole32((sc->sc_ic.ic_flags & IEEE80211_F_PRIVACY) ? 0x8 : 0);
   1869      1.1  lukem 	DPRINTF(("Setting adapter wep flags to 0x%x\n", data));
   1870      1.1  lukem 	error = ipw_cmd(sc, IPW_CMD_SET_WEP_FLAGS, &data, sizeof data);
   1871      1.1  lukem 	if (error != 0)
   1872      1.1  lukem 		return error;
   1873      1.1  lukem 
   1874      1.8  lukem 	if (ic->ic_opmode == IEEE80211_M_IBSS ||
   1875      1.1  lukem 	    ic->ic_opmode == IEEE80211_M_HOSTAP) {
   1876      1.1  lukem 		data = htole32(ic->ic_lintval);
   1877      1.1  lukem 		DPRINTF(("Setting adapter beacon interval to %u\n", data));
   1878      1.8  lukem 		error = ipw_cmd(sc, IPW_CMD_SET_BEACON_INTERVAL, &data,
   1879      1.1  lukem 		    sizeof data);
   1880      1.1  lukem 		if (error != 0)
   1881      1.1  lukem 			return error;
   1882      1.1  lukem 	}
   1883      1.1  lukem 
   1884      1.1  lukem 	options.flags = htole32(0);
   1885      1.1  lukem 	options.channels = htole32(0x3fff); /* scan channels 1-14 */
   1886      1.1  lukem 	error = ipw_cmd(sc, IPW_CMD_SET_SCAN_OPTIONS, &options, sizeof options);
   1887      1.1  lukem 	if (error != 0)
   1888      1.1  lukem 		return error;
   1889      1.1  lukem 
   1890      1.1  lukem 	/* finally, enable adapter (start scanning for an access point) */
   1891      1.1  lukem 	DPRINTF(("Enabling adapter\n"));
   1892      1.1  lukem 	error = ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
   1893      1.1  lukem 	if (error != 0)
   1894      1.1  lukem 		return error;
   1895      1.1  lukem 
   1896      1.1  lukem 	return 0;
   1897      1.1  lukem }
   1898      1.1  lukem 
   1899      1.8  lukem static int
   1900      1.1  lukem ipw_init(struct ifnet *ifp)
   1901      1.1  lukem {
   1902      1.1  lukem 	struct ipw_softc *sc = ifp->if_softc;
   1903      1.1  lukem 	struct ieee80211com *ic = &sc->sc_ic;
   1904      1.1  lukem 
   1905      1.1  lukem 	/* exit immediately if firmware has not been ioctl'd */
   1906      1.1  lukem 	if (!(sc->flags & IPW_FLAG_FW_INITED)) {
   1907      1.1  lukem 		ifp->if_flags &= ~IFF_UP;
   1908      1.1  lukem 		return EIO;
   1909      1.1  lukem 	}
   1910      1.1  lukem 
   1911      1.1  lukem 	ipw_stop(ifp, 0);
   1912      1.1  lukem 
   1913      1.1  lukem 	if (ipw_config(sc) != 0) {
   1914      1.1  lukem 		aprint_error("%s: device configuration failed\n",
   1915      1.1  lukem 		    sc->sc_dev.dv_xname);
   1916      1.1  lukem 		goto fail;
   1917      1.1  lukem 	}
   1918      1.1  lukem 
   1919      1.1  lukem 	ifp->if_flags &= ~IFF_OACTIVE;
   1920      1.1  lukem 	ifp->if_flags |= IFF_RUNNING;
   1921      1.1  lukem 
   1922      1.1  lukem 	ic->ic_bss->ni_chan = ic->ic_channels;
   1923      1.1  lukem 
   1924      1.1  lukem 	return 0;
   1925      1.1  lukem 
   1926      1.1  lukem fail:	ipw_stop(ifp, 0);
   1927      1.1  lukem 
   1928      1.1  lukem 	return EIO;
   1929      1.1  lukem }
   1930      1.1  lukem 
   1931      1.1  lukem static void
   1932      1.1  lukem ipw_stop(struct ifnet *ifp, int disable)
   1933      1.1  lukem {
   1934      1.1  lukem 	struct ipw_softc *sc = ifp->if_softc;
   1935      1.1  lukem 	struct ieee80211com *ic = &sc->sc_ic;
   1936      1.1  lukem 
   1937      1.1  lukem 	if (ifp->if_flags & IFF_RUNNING) {
   1938      1.1  lukem 		DPRINTF(("Disabling adapter\n"));
   1939      1.1  lukem 		ipw_cmd(sc, IPW_CMD_DISABLE, NULL, 0);
   1940      1.1  lukem 	}
   1941      1.1  lukem 
   1942      1.1  lukem 	ifp->if_timer = 0;
   1943      1.1  lukem 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1944      1.1  lukem 
   1945      1.1  lukem 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
   1946      1.1  lukem }
   1947      1.1  lukem 
   1948      1.1  lukem static void
   1949      1.8  lukem ipw_read_mem_1(struct ipw_softc *sc, bus_size_t offset, u_int8_t *datap,
   1950      1.1  lukem     bus_size_t count)
   1951      1.1  lukem {
   1952      1.1  lukem 	for (; count > 0; offset++, datap++, count--) {
   1953      1.1  lukem 		CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
   1954      1.1  lukem 		*datap = CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3));
   1955      1.1  lukem 	}
   1956      1.1  lukem }
   1957      1.1  lukem 
   1958      1.1  lukem static void
   1959      1.8  lukem ipw_write_mem_1(struct ipw_softc *sc, bus_size_t offset, u_int8_t *datap,
   1960      1.1  lukem     bus_size_t count)
   1961      1.1  lukem {
   1962      1.1  lukem 	for (; count > 0; offset++, datap++, count--) {
   1963      1.1  lukem 		CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
   1964      1.1  lukem 		CSR_WRITE_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3), *datap);
   1965      1.1  lukem 	}
   1966      1.1  lukem }
   1967      1.1  lukem 
   1968      1.1  lukem static void
   1969      1.1  lukem ipw_zero_mem_4(struct ipw_softc *sc, bus_size_t offset, bus_size_t count)
   1970      1.1  lukem {
   1971      1.1  lukem 	CSR_WRITE_4(sc, IPW_CSR_AUTOINC_ADDR, offset);
   1972      1.1  lukem 	while (count-- > 0)
   1973      1.1  lukem 		CSR_WRITE_4(sc, IPW_CSR_AUTOINC_DATA, 0);
   1974      1.1  lukem }
   1975      1.1  lukem 
   1976