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