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