Home | History | Annotate | Line # | Download | only in pci
if_iwi.c revision 1.4.4.4
      1  1.4.4.4  skrll /*	$NetBSD: if_iwi.c,v 1.4.4.4 2005/11/10 14:06:01 skrll Exp $  */
      2  1.4.4.2  skrll 
      3  1.4.4.2  skrll /*-
      4  1.4.4.2  skrll  * Copyright (c) 2004, 2005
      5  1.4.4.2  skrll  *      Damien Bergamini <damien.bergamini (at) free.fr>. All rights reserved.
      6  1.4.4.2  skrll  *
      7  1.4.4.2  skrll  * Redistribution and use in source and binary forms, with or without
      8  1.4.4.2  skrll  * modification, are permitted provided that the following conditions
      9  1.4.4.2  skrll  * are met:
     10  1.4.4.2  skrll  * 1. Redistributions of source code must retain the above copyright
     11  1.4.4.2  skrll  *    notice unmodified, this list of conditions, and the following
     12  1.4.4.2  skrll  *    disclaimer.
     13  1.4.4.2  skrll  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.4.4.2  skrll  *    notice, this list of conditions and the following disclaimer in the
     15  1.4.4.2  skrll  *    documentation and/or other materials provided with the distribution.
     16  1.4.4.2  skrll  *
     17  1.4.4.2  skrll  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     18  1.4.4.2  skrll  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  1.4.4.2  skrll  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  1.4.4.2  skrll  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21  1.4.4.2  skrll  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  1.4.4.2  skrll  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  1.4.4.2  skrll  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  1.4.4.2  skrll  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  1.4.4.2  skrll  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  1.4.4.2  skrll  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  1.4.4.2  skrll  * SUCH DAMAGE.
     28  1.4.4.2  skrll  */
     29  1.4.4.2  skrll 
     30  1.4.4.2  skrll #include <sys/cdefs.h>
     31  1.4.4.4  skrll __KERNEL_RCSID(0, "$NetBSD: if_iwi.c,v 1.4.4.4 2005/11/10 14:06:01 skrll Exp $");
     32  1.4.4.2  skrll 
     33  1.4.4.2  skrll /*-
     34  1.4.4.4  skrll  * Intel(R) PRO/Wireless 2200BG/2225BG/2915ABG driver
     35  1.4.4.2  skrll  * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
     36  1.4.4.2  skrll  */
     37  1.4.4.2  skrll 
     38  1.4.4.2  skrll #include "bpfilter.h"
     39  1.4.4.2  skrll 
     40  1.4.4.2  skrll #include <sys/param.h>
     41  1.4.4.2  skrll #include <sys/sockio.h>
     42  1.4.4.2  skrll #include <sys/sysctl.h>
     43  1.4.4.2  skrll #include <sys/mbuf.h>
     44  1.4.4.2  skrll #include <sys/kernel.h>
     45  1.4.4.2  skrll #include <sys/socket.h>
     46  1.4.4.2  skrll #include <sys/systm.h>
     47  1.4.4.2  skrll #include <sys/malloc.h>
     48  1.4.4.2  skrll #include <sys/conf.h>
     49  1.4.4.2  skrll 
     50  1.4.4.2  skrll #include <machine/bus.h>
     51  1.4.4.2  skrll #include <machine/endian.h>
     52  1.4.4.2  skrll #include <machine/intr.h>
     53  1.4.4.2  skrll 
     54  1.4.4.2  skrll #include <dev/pci/pcireg.h>
     55  1.4.4.2  skrll #include <dev/pci/pcivar.h>
     56  1.4.4.2  skrll #include <dev/pci/pcidevs.h>
     57  1.4.4.2  skrll 
     58  1.4.4.2  skrll #if NBPFILTER > 0
     59  1.4.4.2  skrll #include <net/bpf.h>
     60  1.4.4.2  skrll #endif
     61  1.4.4.2  skrll #include <net/if.h>
     62  1.4.4.2  skrll #include <net/if_arp.h>
     63  1.4.4.2  skrll #include <net/if_dl.h>
     64  1.4.4.2  skrll #include <net/if_ether.h>
     65  1.4.4.2  skrll #include <net/if_media.h>
     66  1.4.4.2  skrll #include <net/if_types.h>
     67  1.4.4.2  skrll 
     68  1.4.4.2  skrll #include <net80211/ieee80211_var.h>
     69  1.4.4.2  skrll #include <net80211/ieee80211_radiotap.h>
     70  1.4.4.2  skrll 
     71  1.4.4.2  skrll #include <netinet/in.h>
     72  1.4.4.2  skrll #include <netinet/in_systm.h>
     73  1.4.4.2  skrll #include <netinet/in_var.h>
     74  1.4.4.2  skrll #include <netinet/ip.h>
     75  1.4.4.2  skrll 
     76  1.4.4.2  skrll #include <crypto/arc4/arc4.h>
     77  1.4.4.2  skrll 
     78  1.4.4.2  skrll #include <dev/pci/if_iwireg.h>
     79  1.4.4.2  skrll #include <dev/pci/if_iwivar.h>
     80  1.4.4.2  skrll 
     81  1.4.4.4  skrll #ifdef IWI_DEBUG
     82  1.4.4.4  skrll #define DPRINTF(x)	if (iwi_debug > 0) printf x
     83  1.4.4.4  skrll #define DPRINTFN(n, x)	if (iwi_debug >= (n)) printf x
     84  1.4.4.4  skrll int iwi_debug = 4;
     85  1.4.4.4  skrll #else
     86  1.4.4.4  skrll #define DPRINTF(x)
     87  1.4.4.4  skrll #define DPRINTFN(n, x)
     88  1.4.4.4  skrll #endif
     89  1.4.4.4  skrll 
     90  1.4.4.4  skrll static int	iwi_match(struct device *, struct cfdata *, void *);
     91  1.4.4.4  skrll static void	iwi_attach(struct device *, struct device *, void *);
     92  1.4.4.4  skrll static int	iwi_detach(struct device *, int);
     93  1.4.4.4  skrll 
     94  1.4.4.4  skrll static void	iwi_shutdown(void *);
     95  1.4.4.4  skrll static int	iwi_suspend(struct iwi_softc *);
     96  1.4.4.4  skrll static int	iwi_resume(struct iwi_softc *);
     97  1.4.4.4  skrll static void	iwi_powerhook(int, void *);
     98  1.4.4.4  skrll 
     99  1.4.4.4  skrll static int	iwi_alloc_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *,
    100  1.4.4.4  skrll     int);
    101  1.4.4.4  skrll static void	iwi_reset_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
    102  1.4.4.4  skrll static void	iwi_free_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
    103  1.4.4.4  skrll static int	iwi_alloc_tx_ring(struct iwi_softc *, struct iwi_tx_ring *,
    104  1.4.4.4  skrll     int);
    105  1.4.4.4  skrll static void	iwi_reset_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
    106  1.4.4.4  skrll static void	iwi_free_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
    107  1.4.4.4  skrll static struct mbuf *
    108  1.4.4.4  skrll 		iwi_alloc_rx_buf(struct iwi_softc *sc);
    109  1.4.4.4  skrll static int	iwi_alloc_rx_ring(struct iwi_softc *, struct iwi_rx_ring *,
    110  1.4.4.4  skrll     int);
    111  1.4.4.4  skrll static void	iwi_reset_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
    112  1.4.4.4  skrll static void	iwi_free_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
    113  1.4.4.4  skrll 
    114  1.4.4.4  skrll static int	iwi_media_change(struct ifnet *);
    115  1.4.4.4  skrll static void	iwi_media_status(struct ifnet *, struct ifmediareq *);
    116  1.4.4.4  skrll static uint16_t	iwi_read_prom_word(struct iwi_softc *, uint8_t);
    117  1.4.4.4  skrll static int	iwi_newstate(struct ieee80211com *, enum ieee80211_state, int);
    118  1.4.4.4  skrll static void	iwi_fix_channel(struct ieee80211com *, struct mbuf *);
    119  1.4.4.4  skrll static void	iwi_frame_intr(struct iwi_softc *, struct iwi_rx_data *, int,
    120  1.4.4.4  skrll     struct iwi_frame *);
    121  1.4.4.4  skrll static void	iwi_notification_intr(struct iwi_softc *, struct iwi_notif *);
    122  1.4.4.4  skrll static void	iwi_rx_intr(struct iwi_softc *);
    123  1.4.4.4  skrll static void	iwi_tx_intr(struct iwi_softc *);
    124  1.4.4.4  skrll static int	iwi_intr(void *);
    125  1.4.4.4  skrll static int	iwi_cmd(struct iwi_softc *, uint8_t, void *, uint8_t, int);
    126  1.4.4.4  skrll static int	iwi_tx_start(struct ifnet *, struct mbuf *, struct ieee80211_node *);
    127  1.4.4.4  skrll static void	iwi_start(struct ifnet *);
    128  1.4.4.4  skrll static void	iwi_watchdog(struct ifnet *);
    129  1.4.4.4  skrll static int	iwi_get_table0(struct iwi_softc *, uint32_t *);
    130  1.4.4.4  skrll static int	iwi_get_radio(struct iwi_softc *, int *);
    131  1.4.4.4  skrll static int	iwi_ioctl(struct ifnet *, u_long, caddr_t);
    132  1.4.4.4  skrll static void	iwi_stop_master(struct iwi_softc *);
    133  1.4.4.4  skrll static int	iwi_reset(struct iwi_softc *);
    134  1.4.4.4  skrll static int	iwi_load_ucode(struct iwi_softc *, void *, int);
    135  1.4.4.4  skrll static int	iwi_load_firmware(struct iwi_softc *, void *, int);
    136  1.4.4.4  skrll static int	iwi_cache_firmware(struct iwi_softc *, void *);
    137  1.4.4.4  skrll static void	iwi_free_firmware(struct iwi_softc *);
    138  1.4.4.4  skrll static int	iwi_config(struct iwi_softc *);
    139  1.4.4.4  skrll static int	iwi_set_chan(struct iwi_softc *, struct ieee80211_channel *);
    140  1.4.4.4  skrll static int	iwi_scan(struct iwi_softc *);
    141  1.4.4.4  skrll static int	iwi_auth_and_assoc(struct iwi_softc *);
    142  1.4.4.4  skrll static int	iwi_init(struct ifnet *);
    143  1.4.4.4  skrll static void	iwi_stop(struct ifnet *, int);
    144  1.4.4.4  skrll 
    145  1.4.4.4  skrll /*
    146  1.4.4.4  skrll  * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
    147  1.4.4.4  skrll  */
    148  1.4.4.2  skrll static const struct ieee80211_rateset iwi_rateset_11a =
    149  1.4.4.2  skrll 	{ 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
    150  1.4.4.2  skrll 
    151  1.4.4.2  skrll static const struct ieee80211_rateset iwi_rateset_11b =
    152  1.4.4.2  skrll 	{ 4, { 2, 4, 11, 22 } };
    153  1.4.4.2  skrll 
    154  1.4.4.2  skrll static const struct ieee80211_rateset iwi_rateset_11g =
    155  1.4.4.2  skrll 	{ 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
    156  1.4.4.2  skrll 
    157  1.4.4.4  skrll static __inline uint8_t
    158  1.4.4.4  skrll MEM_READ_1(struct iwi_softc *sc, uint32_t addr)
    159  1.4.4.2  skrll {
    160  1.4.4.2  skrll 	CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
    161  1.4.4.2  skrll 	return CSR_READ_1(sc, IWI_CSR_INDIRECT_DATA);
    162  1.4.4.2  skrll }
    163  1.4.4.2  skrll 
    164  1.4.4.4  skrll static __inline uint32_t
    165  1.4.4.4  skrll MEM_READ_4(struct iwi_softc *sc, uint32_t addr)
    166  1.4.4.2  skrll {
    167  1.4.4.2  skrll 	CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
    168  1.4.4.2  skrll 	return CSR_READ_4(sc, IWI_CSR_INDIRECT_DATA);
    169  1.4.4.2  skrll }
    170  1.4.4.2  skrll 
    171  1.4.4.2  skrll CFATTACH_DECL(iwi, sizeof (struct iwi_softc), iwi_match, iwi_attach,
    172  1.4.4.2  skrll     iwi_detach, NULL);
    173  1.4.4.2  skrll 
    174  1.4.4.2  skrll static int
    175  1.4.4.2  skrll iwi_match(struct device *parent, struct cfdata *match, void *aux)
    176  1.4.4.2  skrll {
    177  1.4.4.2  skrll 	struct pci_attach_args *pa = aux;
    178  1.4.4.3  skrll 
    179  1.4.4.2  skrll 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
    180  1.4.4.2  skrll 		return 0;
    181  1.4.4.2  skrll 
    182  1.4.4.2  skrll 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2200BG ||
    183  1.4.4.4  skrll 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2225BG ||
    184  1.4.4.4  skrll 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_1 ||
    185  1.4.4.4  skrll 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_2)
    186  1.4.4.2  skrll 		return 1;
    187  1.4.4.3  skrll 
    188  1.4.4.2  skrll 	return 0;
    189  1.4.4.2  skrll }
    190  1.4.4.2  skrll 
    191  1.4.4.2  skrll /* Base Address Register */
    192  1.4.4.2  skrll #define IWI_PCI_BAR0	0x10
    193  1.4.4.2  skrll 
    194  1.4.4.2  skrll static void
    195  1.4.4.2  skrll iwi_attach(struct device *parent, struct device *self, void *aux)
    196  1.4.4.2  skrll {
    197  1.4.4.2  skrll 	struct iwi_softc *sc = (struct iwi_softc *)self;
    198  1.4.4.2  skrll 	struct ieee80211com *ic = &sc->sc_ic;
    199  1.4.4.4  skrll 	struct ifnet *ifp = &sc->sc_if;
    200  1.4.4.2  skrll 	struct pci_attach_args *pa = aux;
    201  1.4.4.2  skrll 	const char *intrstr;
    202  1.4.4.2  skrll 	char devinfo[256];
    203  1.4.4.2  skrll 	bus_space_tag_t memt;
    204  1.4.4.2  skrll 	bus_space_handle_t memh;
    205  1.4.4.2  skrll 	bus_addr_t base;
    206  1.4.4.2  skrll 	pci_intr_handle_t ih;
    207  1.4.4.2  skrll 	pcireg_t data;
    208  1.4.4.4  skrll 	uint16_t val;
    209  1.4.4.2  skrll 	int error, revision, i;
    210  1.4.4.2  skrll 
    211  1.4.4.2  skrll 	sc->sc_pct = pa->pa_pc;
    212  1.4.4.2  skrll 	sc->sc_pcitag = pa->pa_tag;
    213  1.4.4.2  skrll 
    214  1.4.4.2  skrll 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof devinfo);
    215  1.4.4.2  skrll 	revision = PCI_REVISION(pa->pa_class);
    216  1.4.4.2  skrll 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo, revision);
    217  1.4.4.2  skrll 
    218  1.4.4.2  skrll 	/* clear device specific PCI configuration register 0x41 */
    219  1.4.4.2  skrll 	data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
    220  1.4.4.2  skrll 	data &= ~0x0000ff00;
    221  1.4.4.2  skrll 	pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data);
    222  1.4.4.2  skrll 
    223  1.4.4.2  skrll 	/* enable bus-mastering */
    224  1.4.4.2  skrll 	data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
    225  1.4.4.2  skrll 	data |= PCI_COMMAND_MASTER_ENABLE;
    226  1.4.4.2  skrll 	pci_conf_write(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG, data);
    227  1.4.4.2  skrll 
    228  1.4.4.2  skrll 	/* map the register window */
    229  1.4.4.2  skrll 	error = pci_mapreg_map(pa, IWI_PCI_BAR0, PCI_MAPREG_TYPE_MEM |
    230  1.4.4.2  skrll 	    PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh, &base, &sc->sc_sz);
    231  1.4.4.2  skrll 	if (error != 0) {
    232  1.4.4.2  skrll 		aprint_error("%s: could not map memory space\n",
    233  1.4.4.2  skrll 		    sc->sc_dev.dv_xname);
    234  1.4.4.2  skrll 		return;
    235  1.4.4.2  skrll 	}
    236  1.4.4.2  skrll 
    237  1.4.4.2  skrll 	sc->sc_st = memt;
    238  1.4.4.2  skrll 	sc->sc_sh = memh;
    239  1.4.4.2  skrll 	sc->sc_dmat = pa->pa_dmat;
    240  1.4.4.2  skrll 
    241  1.4.4.2  skrll 	/* disable interrupts */
    242  1.4.4.2  skrll 	CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
    243  1.4.4.2  skrll 
    244  1.4.4.2  skrll 	if (pci_intr_map(pa, &ih) != 0) {
    245  1.4.4.2  skrll 		aprint_error("%s: could not map interrupt\n",
    246  1.4.4.2  skrll 		    sc->sc_dev.dv_xname);
    247  1.4.4.2  skrll 		return;
    248  1.4.4.2  skrll 	}
    249  1.4.4.2  skrll 
    250  1.4.4.2  skrll 	intrstr = pci_intr_string(sc->sc_pct, ih);
    251  1.4.4.2  skrll 	sc->sc_ih = pci_intr_establish(sc->sc_pct, ih, IPL_NET, iwi_intr, sc);
    252  1.4.4.2  skrll 	if (sc->sc_ih == NULL) {
    253  1.4.4.2  skrll 		aprint_error("%s: could not establish interrupt",
    254  1.4.4.2  skrll 		    sc->sc_dev.dv_xname);
    255  1.4.4.2  skrll 		if (intrstr != NULL)
    256  1.4.4.2  skrll 			aprint_error(" at %s", intrstr);
    257  1.4.4.2  skrll 		aprint_error("\n");
    258  1.4.4.2  skrll 		return;
    259  1.4.4.2  skrll 	}
    260  1.4.4.2  skrll 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    261  1.4.4.2  skrll 
    262  1.4.4.2  skrll 	if (iwi_reset(sc) != 0) {
    263  1.4.4.2  skrll 		aprint_error("%s: could not reset adapter\n",
    264  1.4.4.2  skrll 		    sc->sc_dev.dv_xname);
    265  1.4.4.2  skrll 		return;
    266  1.4.4.2  skrll 	}
    267  1.4.4.2  skrll 
    268  1.4.4.4  skrll 	/*
    269  1.4.4.4  skrll 	 * Allocate rings.
    270  1.4.4.4  skrll 	 */
    271  1.4.4.4  skrll 	if (iwi_alloc_cmd_ring(sc, &sc->cmdq, IWI_CMD_RING_COUNT) != 0) {
    272  1.4.4.4  skrll 		aprint_error("%s: could not allocate command ring\n",
    273  1.4.4.2  skrll 		    sc->sc_dev.dv_xname);
    274  1.4.4.4  skrll 		goto fail;
    275  1.4.4.4  skrll 	}
    276  1.4.4.4  skrll 
    277  1.4.4.4  skrll 	if (iwi_alloc_tx_ring(sc, &sc->txq, IWI_TX_RING_COUNT) != 0) {
    278  1.4.4.4  skrll 		aprint_error("%s: could not allocate Tx ring\n",
    279  1.4.4.4  skrll 		    sc->sc_dev.dv_xname);
    280  1.4.4.4  skrll 		goto fail;
    281  1.4.4.4  skrll 	}
    282  1.4.4.4  skrll 
    283  1.4.4.4  skrll 	if (iwi_alloc_rx_ring(sc, &sc->rxq, IWI_RX_RING_COUNT) != 0) {
    284  1.4.4.4  skrll 		aprint_error("%s: could not allocate Rx ring\n",
    285  1.4.4.4  skrll 		    sc->sc_dev.dv_xname);
    286  1.4.4.4  skrll 		goto fail;
    287  1.4.4.2  skrll 	}
    288  1.4.4.2  skrll 
    289  1.4.4.4  skrll 	ic->ic_ifp = ifp;
    290  1.4.4.4  skrll 	ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
    291  1.4.4.4  skrll 	ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
    292  1.4.4.2  skrll 	ic->ic_state = IEEE80211_S_INIT;
    293  1.4.4.2  skrll 
    294  1.4.4.2  skrll 	/* set device capabilities */
    295  1.4.4.4  skrll 	ic->ic_caps = IEEE80211_C_WPA | IEEE80211_C_PMGT | IEEE80211_C_TXPMGT |
    296  1.4.4.4  skrll 	    IEEE80211_C_SHPREAMBLE | IEEE80211_C_MONITOR;
    297  1.4.4.2  skrll 
    298  1.4.4.2  skrll 	/* read MAC address from EEPROM */
    299  1.4.4.2  skrll 	val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 0);
    300  1.4.4.4  skrll 	ic->ic_myaddr[0] = val & 0xff;
    301  1.4.4.4  skrll 	ic->ic_myaddr[1] = val >> 8;
    302  1.4.4.2  skrll 	val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 1);
    303  1.4.4.4  skrll 	ic->ic_myaddr[2] = val & 0xff;
    304  1.4.4.4  skrll 	ic->ic_myaddr[3] = val >> 8;
    305  1.4.4.2  skrll 	val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 2);
    306  1.4.4.4  skrll 	ic->ic_myaddr[4] = val & 0xff;
    307  1.4.4.4  skrll 	ic->ic_myaddr[5] = val >> 8;
    308  1.4.4.2  skrll 
    309  1.4.4.2  skrll 	aprint_normal("%s: 802.11 address %s\n", sc->sc_dev.dv_xname,
    310  1.4.4.2  skrll 	    ether_sprintf(ic->ic_myaddr));
    311  1.4.4.2  skrll 
    312  1.4.4.4  skrll 
    313  1.4.4.4  skrll 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_1 ||
    314  1.4.4.4  skrll 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_2) {
    315  1.4.4.4  skrll 		/* set supported .11a rates (2915ABG only) */
    316  1.4.4.2  skrll 		ic->ic_sup_rates[IEEE80211_MODE_11A] = iwi_rateset_11a;
    317  1.4.4.2  skrll 
    318  1.4.4.2  skrll 		/* set supported .11a channels */
    319  1.4.4.2  skrll 		for (i = 36; i <= 64; i += 4) {
    320  1.4.4.2  skrll 			ic->ic_channels[i].ic_freq =
    321  1.4.4.2  skrll 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
    322  1.4.4.2  skrll 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
    323  1.4.4.2  skrll 		}
    324  1.4.4.4  skrll 		for (i = 149; i <= 165; i += 4) {
    325  1.4.4.2  skrll 			ic->ic_channels[i].ic_freq =
    326  1.4.4.2  skrll 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
    327  1.4.4.2  skrll 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
    328  1.4.4.2  skrll 		}
    329  1.4.4.2  skrll 	}
    330  1.4.4.2  skrll 
    331  1.4.4.2  skrll 	/* set supported .11b and .11g rates */
    332  1.4.4.2  skrll 	ic->ic_sup_rates[IEEE80211_MODE_11B] = iwi_rateset_11b;
    333  1.4.4.2  skrll 	ic->ic_sup_rates[IEEE80211_MODE_11G] = iwi_rateset_11g;
    334  1.4.4.2  skrll 
    335  1.4.4.2  skrll 	/* set supported .11b and .11g channels (1 through 14) */
    336  1.4.4.2  skrll 	for (i = 1; i <= 14; i++) {
    337  1.4.4.2  skrll 		ic->ic_channels[i].ic_freq =
    338  1.4.4.2  skrll 		    ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
    339  1.4.4.2  skrll 		ic->ic_channels[i].ic_flags =
    340  1.4.4.2  skrll 		    IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
    341  1.4.4.2  skrll 		    IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
    342  1.4.4.2  skrll 	}
    343  1.4.4.2  skrll 
    344  1.4.4.2  skrll 	ifp->if_softc = sc;
    345  1.4.4.2  skrll 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    346  1.4.4.2  skrll 	ifp->if_init = iwi_init;
    347  1.4.4.2  skrll 	ifp->if_stop = iwi_stop;
    348  1.4.4.2  skrll 	ifp->if_ioctl = iwi_ioctl;
    349  1.4.4.2  skrll 	ifp->if_start = iwi_start;
    350  1.4.4.2  skrll 	ifp->if_watchdog = iwi_watchdog;
    351  1.4.4.2  skrll 	IFQ_SET_READY(&ifp->if_snd);
    352  1.4.4.2  skrll 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
    353  1.4.4.2  skrll 
    354  1.4.4.2  skrll 	if_attach(ifp);
    355  1.4.4.4  skrll 	ieee80211_ifattach(ic);
    356  1.4.4.2  skrll 	/* override state transition machine */
    357  1.4.4.2  skrll 	sc->sc_newstate = ic->ic_newstate;
    358  1.4.4.2  skrll 	ic->ic_newstate = iwi_newstate;
    359  1.4.4.4  skrll 	ieee80211_media_init(ic, iwi_media_change, iwi_media_status);
    360  1.4.4.2  skrll 
    361  1.4.4.2  skrll #if NBPFILTER > 0
    362  1.4.4.2  skrll 	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
    363  1.4.4.2  skrll 	    sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf);
    364  1.4.4.2  skrll 
    365  1.4.4.2  skrll 	sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
    366  1.4.4.2  skrll 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
    367  1.4.4.2  skrll 	sc->sc_rxtap.wr_ihdr.it_present = htole32(IWI_RX_RADIOTAP_PRESENT);
    368  1.4.4.2  skrll 
    369  1.4.4.2  skrll 	sc->sc_txtap_len = sizeof sc->sc_txtapu;
    370  1.4.4.2  skrll 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
    371  1.4.4.2  skrll 	sc->sc_txtap.wt_ihdr.it_present = htole32(IWI_TX_RADIOTAP_PRESENT);
    372  1.4.4.2  skrll #endif
    373  1.4.4.4  skrll 
    374  1.4.4.4  skrll 	/*
    375  1.4.4.4  skrll 	 * Make sure the interface is shutdown during reboot.
    376  1.4.4.4  skrll 	 */
    377  1.4.4.4  skrll 	sc->sc_sdhook = shutdownhook_establish(iwi_shutdown, sc);
    378  1.4.4.4  skrll 	if (sc->sc_sdhook == NULL)
    379  1.4.4.4  skrll 		aprint_error("%s: WARNING: unable to establish shutdown hook\n",
    380  1.4.4.4  skrll 		    sc->sc_dev.dv_xname);
    381  1.4.4.4  skrll 	sc->sc_powerhook = powerhook_establish(iwi_powerhook, sc);
    382  1.4.4.4  skrll 	if (sc->sc_powerhook == NULL)
    383  1.4.4.4  skrll 		printf("%s: WARNING: unable to establish power hook\n",
    384  1.4.4.4  skrll 		    sc->sc_dev.dv_xname);
    385  1.4.4.4  skrll 
    386  1.4.4.4  skrll 	ieee80211_announce(ic);
    387  1.4.4.4  skrll 	/*
    388  1.4.4.4  skrll 	 * Add a few sysctl knobs.
    389  1.4.4.4  skrll 	 * XXX: Not yet.
    390  1.4.4.4  skrll 	 */
    391  1.4.4.4  skrll 	sc->dwelltime = 100;
    392  1.4.4.4  skrll 	sc->bluetooth = 1;
    393  1.4.4.4  skrll 	sc->antenna = 0;
    394  1.4.4.4  skrll 
    395  1.4.4.4  skrll 	return;
    396  1.4.4.4  skrll 
    397  1.4.4.4  skrll fail:	iwi_detach(self, 0);
    398  1.4.4.2  skrll }
    399  1.4.4.2  skrll 
    400  1.4.4.2  skrll static int
    401  1.4.4.2  skrll iwi_detach(struct device* self, int flags)
    402  1.4.4.2  skrll {
    403  1.4.4.2  skrll 	struct iwi_softc *sc = (struct iwi_softc *)self;
    404  1.4.4.4  skrll 	struct ifnet *ifp = &sc->sc_if;
    405  1.4.4.2  skrll 
    406  1.4.4.2  skrll 	iwi_stop(ifp, 1);
    407  1.4.4.2  skrll 	iwi_free_firmware(sc);
    408  1.4.4.2  skrll 
    409  1.4.4.2  skrll #if NBPFILTER > 0
    410  1.4.4.4  skrll 	if (ifp != NULL)
    411  1.4.4.4  skrll 		bpfdetach(ifp);
    412  1.4.4.2  skrll #endif
    413  1.4.4.4  skrll 	ieee80211_ifdetach(&sc->sc_ic);
    414  1.4.4.4  skrll 	if (ifp != NULL)
    415  1.4.4.4  skrll 		if_detach(ifp);
    416  1.4.4.4  skrll 
    417  1.4.4.4  skrll 	iwi_free_cmd_ring(sc, &sc->cmdq);
    418  1.4.4.4  skrll 	iwi_free_tx_ring(sc, &sc->txq);
    419  1.4.4.4  skrll 	iwi_free_rx_ring(sc, &sc->rxq);
    420  1.4.4.2  skrll 
    421  1.4.4.2  skrll 	if (sc->sc_ih != NULL) {
    422  1.4.4.2  skrll 		pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
    423  1.4.4.2  skrll 		sc->sc_ih = NULL;
    424  1.4.4.2  skrll 	}
    425  1.4.4.2  skrll 
    426  1.4.4.2  skrll 	bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz);
    427  1.4.4.2  skrll 
    428  1.4.4.4  skrll 	powerhook_disestablish(sc->sc_powerhook);
    429  1.4.4.4  skrll 	shutdownhook_disestablish(sc->sc_sdhook);
    430  1.4.4.4  skrll 
    431  1.4.4.2  skrll 	return 0;
    432  1.4.4.2  skrll }
    433  1.4.4.2  skrll 
    434  1.4.4.2  skrll static int
    435  1.4.4.4  skrll iwi_alloc_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring,
    436  1.4.4.4  skrll     int count)
    437  1.4.4.2  skrll {
    438  1.4.4.4  skrll 	int error, nsegs;
    439  1.4.4.4  skrll 
    440  1.4.4.4  skrll 	ring->count = count;
    441  1.4.4.4  skrll 	ring->queued = 0;
    442  1.4.4.4  skrll 	ring->cur = ring->next = 0;
    443  1.4.4.2  skrll 
    444  1.4.4.2  skrll 	/*
    445  1.4.4.4  skrll 	 * Allocate and map command ring
    446  1.4.4.2  skrll 	 */
    447  1.4.4.2  skrll 	error = bus_dmamap_create(sc->sc_dmat,
    448  1.4.4.4  skrll 	    IWI_CMD_DESC_SIZE * count, 1,
    449  1.4.4.4  skrll 	    IWI_CMD_DESC_SIZE * count, 0,
    450  1.4.4.4  skrll 	    BUS_DMA_NOWAIT, &ring->desc_map);
    451  1.4.4.2  skrll 	if (error != 0) {
    452  1.4.4.4  skrll 		aprint_error("%s: could not create command ring DMA map\n",
    453  1.4.4.2  skrll 		    sc->sc_dev.dv_xname);
    454  1.4.4.2  skrll 		goto fail;
    455  1.4.4.2  skrll 	}
    456  1.4.4.2  skrll 
    457  1.4.4.2  skrll 	error = bus_dmamem_alloc(sc->sc_dmat,
    458  1.4.4.4  skrll 	    IWI_CMD_DESC_SIZE * count, PAGE_SIZE, 0,
    459  1.4.4.4  skrll 	    &sc->cmdq.desc_seg, 1, &nsegs, BUS_DMA_NOWAIT);
    460  1.4.4.2  skrll 	if (error != 0) {
    461  1.4.4.4  skrll 		aprint_error("%s: could not allocate command ring DMA memory\n",
    462  1.4.4.2  skrll 		    sc->sc_dev.dv_xname);
    463  1.4.4.2  skrll 		goto fail;
    464  1.4.4.2  skrll 	}
    465  1.4.4.2  skrll 
    466  1.4.4.4  skrll 	error = bus_dmamem_map(sc->sc_dmat, &sc->cmdq.desc_seg, nsegs,
    467  1.4.4.4  skrll 	    IWI_CMD_DESC_SIZE * count,
    468  1.4.4.4  skrll 	    (caddr_t *)&sc->cmdq.desc, BUS_DMA_NOWAIT);
    469  1.4.4.2  skrll 	if (error != 0) {
    470  1.4.4.4  skrll 		aprint_error("%s: could not map command ring DMA memory\n",
    471  1.4.4.2  skrll 		    sc->sc_dev.dv_xname);
    472  1.4.4.2  skrll 		goto fail;
    473  1.4.4.2  skrll 	}
    474  1.4.4.2  skrll 
    475  1.4.4.4  skrll 	error = bus_dmamap_load(sc->sc_dmat, sc->cmdq.desc_map, sc->cmdq.desc,
    476  1.4.4.4  skrll 	    IWI_CMD_DESC_SIZE * count, NULL,
    477  1.4.4.2  skrll 	    BUS_DMA_NOWAIT);
    478  1.4.4.2  skrll 	if (error != 0) {
    479  1.4.4.4  skrll 		aprint_error("%s: could not load command ring DMA map\n",
    480  1.4.4.2  skrll 		    sc->sc_dev.dv_xname);
    481  1.4.4.2  skrll 		goto fail;
    482  1.4.4.2  skrll 	}
    483  1.4.4.2  skrll 
    484  1.4.4.4  skrll 	memset(sc->cmdq.desc, 0,
    485  1.4.4.4  skrll 	    IWI_CMD_DESC_SIZE * count);
    486  1.4.4.4  skrll 
    487  1.4.4.4  skrll 	return 0;
    488  1.4.4.4  skrll 
    489  1.4.4.4  skrll fail:	iwi_free_cmd_ring(sc, ring);
    490  1.4.4.4  skrll 	return error;
    491  1.4.4.4  skrll }
    492  1.4.4.4  skrll 
    493  1.4.4.4  skrll static void
    494  1.4.4.4  skrll iwi_reset_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
    495  1.4.4.4  skrll {
    496  1.4.4.4  skrll 	ring->queued = 0;
    497  1.4.4.4  skrll 	ring->cur = ring->next = 0;
    498  1.4.4.4  skrll }
    499  1.4.4.4  skrll 
    500  1.4.4.4  skrll static void
    501  1.4.4.4  skrll iwi_free_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
    502  1.4.4.4  skrll {
    503  1.4.4.4  skrll 	if (ring->desc_map != NULL) {
    504  1.4.4.4  skrll 		if (ring->desc != NULL) {
    505  1.4.4.4  skrll 			bus_dmamap_unload(sc->sc_dmat, ring->desc_map);
    506  1.4.4.4  skrll 			bus_dmamem_unmap(sc->sc_dmat, (caddr_t)ring->desc,
    507  1.4.4.4  skrll 			    IWI_CMD_DESC_SIZE * ring->count);
    508  1.4.4.4  skrll 			bus_dmamem_free(sc->sc_dmat, &ring->desc_seg, 1);
    509  1.4.4.4  skrll 		}
    510  1.4.4.4  skrll 		bus_dmamap_destroy(sc->sc_dmat, ring->desc_map);
    511  1.4.4.4  skrll 	}
    512  1.4.4.4  skrll }
    513  1.4.4.4  skrll 
    514  1.4.4.4  skrll static int
    515  1.4.4.4  skrll iwi_alloc_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring,
    516  1.4.4.4  skrll     int count)
    517  1.4.4.4  skrll {
    518  1.4.4.4  skrll 	int i, error, nsegs;
    519  1.4.4.4  skrll 
    520  1.4.4.4  skrll 	ring->count = count;
    521  1.4.4.4  skrll 	ring->queued = 0;
    522  1.4.4.4  skrll 	ring->cur = ring->next = 0;
    523  1.4.4.2  skrll 
    524  1.4.4.2  skrll 	/*
    525  1.4.4.4  skrll 	 * Allocate and map Tx ring
    526  1.4.4.2  skrll 	 */
    527  1.4.4.2  skrll 	error = bus_dmamap_create(sc->sc_dmat,
    528  1.4.4.4  skrll 	    IWI_TX_DESC_SIZE * count, 1,
    529  1.4.4.4  skrll 	    IWI_TX_DESC_SIZE * count, 0, BUS_DMA_NOWAIT,
    530  1.4.4.4  skrll 	    &ring->desc_map);
    531  1.4.4.2  skrll 	if (error != 0) {
    532  1.4.4.4  skrll 		aprint_error("%s: could not create tx ring DMA map\n",
    533  1.4.4.2  skrll 		    sc->sc_dev.dv_xname);
    534  1.4.4.2  skrll 		goto fail;
    535  1.4.4.2  skrll 	}
    536  1.4.4.2  skrll 
    537  1.4.4.2  skrll 	error = bus_dmamem_alloc(sc->sc_dmat,
    538  1.4.4.4  skrll 	    IWI_TX_DESC_SIZE * count, PAGE_SIZE, 0,
    539  1.4.4.4  skrll 	    &ring->desc_seg, 1, &nsegs, BUS_DMA_NOWAIT);
    540  1.4.4.2  skrll 	if (error != 0) {
    541  1.4.4.4  skrll 		aprint_error("%s: could not allocate tx ring DMA memory\n",
    542  1.4.4.2  skrll 		    sc->sc_dev.dv_xname);
    543  1.4.4.2  skrll 		goto fail;
    544  1.4.4.2  skrll 	}
    545  1.4.4.2  skrll 
    546  1.4.4.4  skrll 	error = bus_dmamem_map(sc->sc_dmat, &ring->desc_seg, nsegs,
    547  1.4.4.4  skrll 	    IWI_TX_DESC_SIZE * count,
    548  1.4.4.4  skrll 	    (caddr_t *)&ring->desc, BUS_DMA_NOWAIT);
    549  1.4.4.2  skrll 	if (error != 0) {
    550  1.4.4.4  skrll 		aprint_error("%s: could not map tx ring DMA memory\n",
    551  1.4.4.2  skrll 		    sc->sc_dev.dv_xname);
    552  1.4.4.2  skrll 		goto fail;
    553  1.4.4.2  skrll 	}
    554  1.4.4.2  skrll 
    555  1.4.4.4  skrll 	error = bus_dmamap_load(sc->sc_dmat, ring->desc_map, ring->desc,
    556  1.4.4.4  skrll 	    IWI_TX_DESC_SIZE * count, NULL,
    557  1.4.4.2  skrll 	    BUS_DMA_NOWAIT);
    558  1.4.4.2  skrll 	if (error != 0) {
    559  1.4.4.4  skrll 		aprint_error("%s: could not load tx ring DMA map\n",
    560  1.4.4.2  skrll 		    sc->sc_dev.dv_xname);
    561  1.4.4.2  skrll 		goto fail;
    562  1.4.4.2  skrll 	}
    563  1.4.4.2  skrll 
    564  1.4.4.4  skrll 	memset(ring->desc, 0, IWI_TX_DESC_SIZE * count);
    565  1.4.4.4  skrll 
    566  1.4.4.4  skrll 	ring->data = malloc(count * sizeof (struct iwi_tx_data), M_DEVBUF,
    567  1.4.4.4  skrll 	    M_NOWAIT | M_ZERO);
    568  1.4.4.4  skrll 	if (ring->data == NULL) {
    569  1.4.4.4  skrll 		aprint_error("%s: could not allocate soft data\n",
    570  1.4.4.4  skrll 		    sc->sc_dev.dv_xname);
    571  1.4.4.4  skrll 		error = ENOMEM;
    572  1.4.4.4  skrll 		goto fail;
    573  1.4.4.4  skrll 	}
    574  1.4.4.2  skrll 
    575  1.4.4.2  skrll 	/*
    576  1.4.4.2  skrll 	 * Allocate Tx buffers DMA maps
    577  1.4.4.2  skrll 	 */
    578  1.4.4.4  skrll 	for (i = 0; i < count; i++) {
    579  1.4.4.2  skrll 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, IWI_MAX_NSEG,
    580  1.4.4.4  skrll 		    MCLBYTES, 0, BUS_DMA_NOWAIT, &ring->data[i].map);
    581  1.4.4.2  skrll 		if (error != 0) {
    582  1.4.4.2  skrll 			aprint_error("%s: could not create tx buf DMA map",
    583  1.4.4.2  skrll 			    sc->sc_dev.dv_xname);
    584  1.4.4.2  skrll 			goto fail;
    585  1.4.4.2  skrll 		}
    586  1.4.4.2  skrll 	}
    587  1.4.4.4  skrll 	return 0;
    588  1.4.4.4  skrll 
    589  1.4.4.4  skrll fail:	iwi_free_tx_ring(sc, ring);
    590  1.4.4.4  skrll 	return error;
    591  1.4.4.4  skrll }
    592  1.4.4.4  skrll 
    593  1.4.4.4  skrll static void
    594  1.4.4.4  skrll iwi_reset_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
    595  1.4.4.4  skrll {
    596  1.4.4.4  skrll 	struct iwi_tx_data *data;
    597  1.4.4.4  skrll 	int i;
    598  1.4.4.4  skrll 
    599  1.4.4.4  skrll 	for (i = 0; i < ring->count; i++) {
    600  1.4.4.4  skrll 		data = &ring->data[i];
    601  1.4.4.4  skrll 
    602  1.4.4.4  skrll 		if (data->m != NULL) {
    603  1.4.4.4  skrll 			bus_dmamap_sync(sc->sc_dmat, data->map, 0,
    604  1.4.4.4  skrll 			    data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
    605  1.4.4.4  skrll 			bus_dmamap_unload(sc->sc_dmat, data->map);
    606  1.4.4.4  skrll 			m_freem(data->m);
    607  1.4.4.4  skrll 			data->m = NULL;
    608  1.4.4.4  skrll 		}
    609  1.4.4.4  skrll 
    610  1.4.4.4  skrll 		if (data->ni != NULL) {
    611  1.4.4.4  skrll 			ieee80211_free_node(data->ni);
    612  1.4.4.4  skrll 			data->ni = NULL;
    613  1.4.4.4  skrll 		}
    614  1.4.4.4  skrll 	}
    615  1.4.4.4  skrll 
    616  1.4.4.4  skrll 	ring->queued = 0;
    617  1.4.4.4  skrll 	ring->cur = ring->next = 0;
    618  1.4.4.4  skrll }
    619  1.4.4.4  skrll 
    620  1.4.4.4  skrll static void
    621  1.4.4.4  skrll iwi_free_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
    622  1.4.4.4  skrll {
    623  1.4.4.4  skrll 	int i;
    624  1.4.4.4  skrll 
    625  1.4.4.4  skrll 	if (ring->desc_map != NULL) {
    626  1.4.4.4  skrll 		if (ring->desc != NULL) {
    627  1.4.4.4  skrll 			bus_dmamap_unload(sc->sc_dmat, ring->desc_map);
    628  1.4.4.4  skrll 			bus_dmamem_unmap(sc->sc_dmat, (caddr_t)ring->desc,
    629  1.4.4.4  skrll 			    IWI_TX_DESC_SIZE * ring->count);
    630  1.4.4.4  skrll 			bus_dmamem_free(sc->sc_dmat, &ring->desc_seg, 1);
    631  1.4.4.4  skrll 		}
    632  1.4.4.4  skrll 		bus_dmamap_destroy(sc->sc_dmat, ring->desc_map);
    633  1.4.4.4  skrll 	}
    634  1.4.4.4  skrll 
    635  1.4.4.4  skrll 	for (i = 0; i < ring->count; i++) {
    636  1.4.4.4  skrll 		if (ring->data[i].m != NULL) {
    637  1.4.4.4  skrll 			bus_dmamap_unload(sc->sc_dmat, ring->data[i].map);
    638  1.4.4.4  skrll 			m_freem(ring->data[i].m);
    639  1.4.4.4  skrll 		}
    640  1.4.4.4  skrll 		bus_dmamap_destroy(sc->sc_dmat, ring->data[i].map);
    641  1.4.4.4  skrll 	}
    642  1.4.4.4  skrll }
    643  1.4.4.4  skrll 
    644  1.4.4.4  skrll static int
    645  1.4.4.4  skrll iwi_alloc_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring,
    646  1.4.4.4  skrll     int count)
    647  1.4.4.4  skrll {
    648  1.4.4.4  skrll 	int i, error;
    649  1.4.4.4  skrll 
    650  1.4.4.4  skrll 	ring->count = count;
    651  1.4.4.4  skrll 	ring->cur = 0;
    652  1.4.4.4  skrll 
    653  1.4.4.4  skrll 	ring->data = malloc(count * sizeof (struct iwi_rx_data), M_DEVBUF,
    654  1.4.4.4  skrll 	    M_NOWAIT | M_ZERO);
    655  1.4.4.4  skrll 	if (ring->data == NULL) {
    656  1.4.4.4  skrll 		aprint_error("%s: could not allocate soft data\n",
    657  1.4.4.4  skrll 		    sc->sc_dev.dv_xname);
    658  1.4.4.4  skrll 		error = ENOMEM;
    659  1.4.4.4  skrll 		goto fail;
    660  1.4.4.4  skrll 	}
    661  1.4.4.2  skrll 
    662  1.4.4.2  skrll 	/*
    663  1.4.4.2  skrll 	 * Allocate and map Rx buffers
    664  1.4.4.2  skrll 	 */
    665  1.4.4.4  skrll 	for (i = 0; i < count; i++) {
    666  1.4.4.2  skrll 
    667  1.4.4.2  skrll 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
    668  1.4.4.4  skrll 		    0, BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW, &ring->data[i].map);
    669  1.4.4.2  skrll 		if (error != 0) {
    670  1.4.4.2  skrll 			aprint_error("%s: could not create rx buf DMA map",
    671  1.4.4.2  skrll 			    sc->sc_dev.dv_xname);
    672  1.4.4.2  skrll 			goto fail;
    673  1.4.4.2  skrll 		}
    674  1.4.4.2  skrll 
    675  1.4.4.4  skrll 		if ((ring->data[i].m = iwi_alloc_rx_buf(sc)) == NULL) {
    676  1.4.4.2  skrll 			error = ENOMEM;
    677  1.4.4.2  skrll 			goto fail;
    678  1.4.4.2  skrll 		}
    679  1.4.4.2  skrll 
    680  1.4.4.4  skrll 		error = bus_dmamap_load_mbuf(sc->sc_dmat, ring->data[i].map,
    681  1.4.4.4  skrll 		    ring->data[i].m, BUS_DMA_READ | BUS_DMA_NOWAIT);
    682  1.4.4.2  skrll 		if (error != 0) {
    683  1.4.4.2  skrll 			aprint_error("%s: could not load rx buffer DMA map\n",
    684  1.4.4.2  skrll 			    sc->sc_dev.dv_xname);
    685  1.4.4.2  skrll 			goto fail;
    686  1.4.4.2  skrll 		}
    687  1.4.4.2  skrll 	}
    688  1.4.4.2  skrll 
    689  1.4.4.2  skrll 	return 0;
    690  1.4.4.2  skrll 
    691  1.4.4.4  skrll fail:	iwi_free_rx_ring(sc, ring);
    692  1.4.4.2  skrll 	return error;
    693  1.4.4.2  skrll }
    694  1.4.4.2  skrll 
    695  1.4.4.2  skrll static void
    696  1.4.4.4  skrll iwi_reset_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
    697  1.4.4.4  skrll {
    698  1.4.4.4  skrll 	ring->cur = 0;
    699  1.4.4.4  skrll }
    700  1.4.4.4  skrll 
    701  1.4.4.4  skrll static void
    702  1.4.4.4  skrll iwi_free_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
    703  1.4.4.2  skrll {
    704  1.4.4.2  skrll 	int i;
    705  1.4.4.2  skrll 
    706  1.4.4.4  skrll 	for (i = 0; i < ring->count; i++) {
    707  1.4.4.4  skrll 		if (ring->data[i].m != NULL) {
    708  1.4.4.4  skrll 			bus_dmamap_unload(sc->sc_dmat, ring->data[i].map);
    709  1.4.4.4  skrll 			m_freem(ring->data[i].m);
    710  1.4.4.2  skrll 		}
    711  1.4.4.4  skrll 		bus_dmamap_destroy(sc->sc_dmat, ring->data[i].map);
    712  1.4.4.2  skrll 	}
    713  1.4.4.4  skrll }
    714  1.4.4.2  skrll 
    715  1.4.4.4  skrll static void
    716  1.4.4.4  skrll iwi_shutdown(void *arg)
    717  1.4.4.4  skrll {
    718  1.4.4.4  skrll 	struct iwi_softc *sc = (struct iwi_softc *)arg;
    719  1.4.4.4  skrll 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
    720  1.4.4.2  skrll 
    721  1.4.4.4  skrll 	iwi_stop(ifp, 1);
    722  1.4.4.4  skrll }
    723  1.4.4.4  skrll 
    724  1.4.4.4  skrll static int
    725  1.4.4.4  skrll iwi_suspend(struct iwi_softc *sc)
    726  1.4.4.4  skrll {
    727  1.4.4.4  skrll 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
    728  1.4.4.4  skrll 
    729  1.4.4.4  skrll 	iwi_stop(ifp, 1);
    730  1.4.4.4  skrll 
    731  1.4.4.4  skrll 	return 0;
    732  1.4.4.4  skrll }
    733  1.4.4.4  skrll 
    734  1.4.4.4  skrll static int
    735  1.4.4.4  skrll iwi_resume(struct iwi_softc *sc)
    736  1.4.4.4  skrll {
    737  1.4.4.4  skrll 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
    738  1.4.4.4  skrll 	pcireg_t data;
    739  1.4.4.4  skrll 
    740  1.4.4.4  skrll 	/* clear device specific PCI configuration register 0x41 */
    741  1.4.4.4  skrll 	data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
    742  1.4.4.4  skrll 	data &= ~0x0000ff00;
    743  1.4.4.4  skrll 	pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data);
    744  1.4.4.4  skrll 
    745  1.4.4.4  skrll 	if (ifp->if_flags & IFF_UP) {
    746  1.4.4.4  skrll 		iwi_init(ifp);
    747  1.4.4.4  skrll 		if (ifp->if_flags & IFF_RUNNING)
    748  1.4.4.4  skrll 			iwi_start(ifp);
    749  1.4.4.2  skrll 	}
    750  1.4.4.2  skrll 
    751  1.4.4.4  skrll 	return 0;
    752  1.4.4.4  skrll }
    753  1.4.4.4  skrll 
    754  1.4.4.4  skrll static void
    755  1.4.4.4  skrll iwi_powerhook(int why, void *arg)
    756  1.4.4.4  skrll {
    757  1.4.4.4  skrll         struct iwi_softc *sc = arg;
    758  1.4.4.4  skrll 	int s;
    759  1.4.4.4  skrll 
    760  1.4.4.4  skrll 	s = splnet();
    761  1.4.4.4  skrll 	switch (why) {
    762  1.4.4.4  skrll 	case PWR_SUSPEND:
    763  1.4.4.4  skrll 	case PWR_STANDBY:
    764  1.4.4.4  skrll 		iwi_suspend(sc);
    765  1.4.4.4  skrll 		break;
    766  1.4.4.4  skrll 	case PWR_RESUME:
    767  1.4.4.4  skrll 		iwi_resume(sc);
    768  1.4.4.4  skrll 		break;
    769  1.4.4.4  skrll 	case PWR_SOFTSUSPEND:
    770  1.4.4.4  skrll 	case PWR_SOFTSTANDBY:
    771  1.4.4.4  skrll 	case PWR_SOFTRESUME:
    772  1.4.4.4  skrll 		break;
    773  1.4.4.2  skrll 	}
    774  1.4.4.4  skrll 	splx(s);
    775  1.4.4.2  skrll }
    776  1.4.4.2  skrll 
    777  1.4.4.2  skrll static int
    778  1.4.4.2  skrll iwi_media_change(struct ifnet *ifp)
    779  1.4.4.2  skrll {
    780  1.4.4.2  skrll 	int error;
    781  1.4.4.2  skrll 
    782  1.4.4.2  skrll 	error = ieee80211_media_change(ifp);
    783  1.4.4.2  skrll 	if (error != ENETRESET)
    784  1.4.4.2  skrll 		return error;
    785  1.4.4.2  skrll 
    786  1.4.4.2  skrll 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
    787  1.4.4.2  skrll 		iwi_init(ifp);
    788  1.4.4.2  skrll 
    789  1.4.4.2  skrll 	return 0;
    790  1.4.4.2  skrll }
    791  1.4.4.2  skrll 
    792  1.4.4.4  skrll /*
    793  1.4.4.4  skrll  * The firmware automaticly adapt the transmit speed. We report the current
    794  1.4.4.4  skrll  * transmit speed here.
    795  1.4.4.4  skrll  */
    796  1.4.4.2  skrll static void
    797  1.4.4.2  skrll iwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
    798  1.4.4.2  skrll {
    799  1.4.4.2  skrll 	struct iwi_softc *sc = ifp->if_softc;
    800  1.4.4.2  skrll 	struct ieee80211com *ic = &sc->sc_ic;
    801  1.4.4.2  skrll #define N(a)	(sizeof (a) / sizeof (a[0]))
    802  1.4.4.2  skrll 	static const struct {
    803  1.4.4.4  skrll 		uint32_t	val;
    804  1.4.4.2  skrll 		int		rate;
    805  1.4.4.2  skrll 	} rates[] = {
    806  1.4.4.2  skrll 		{ IWI_RATE_DS1,      2 },
    807  1.4.4.2  skrll 		{ IWI_RATE_DS2,      4 },
    808  1.4.4.2  skrll 		{ IWI_RATE_DS5,     11 },
    809  1.4.4.2  skrll 		{ IWI_RATE_DS11,    22 },
    810  1.4.4.2  skrll 		{ IWI_RATE_OFDM6,   12 },
    811  1.4.4.2  skrll 		{ IWI_RATE_OFDM9,   18 },
    812  1.4.4.2  skrll 		{ IWI_RATE_OFDM12,  24 },
    813  1.4.4.2  skrll 		{ IWI_RATE_OFDM18,  36 },
    814  1.4.4.2  skrll 		{ IWI_RATE_OFDM24,  48 },
    815  1.4.4.2  skrll 		{ IWI_RATE_OFDM36,  72 },
    816  1.4.4.2  skrll 		{ IWI_RATE_OFDM48,  96 },
    817  1.4.4.2  skrll 		{ IWI_RATE_OFDM54, 108 },
    818  1.4.4.2  skrll 	};
    819  1.4.4.4  skrll 	uint32_t val;
    820  1.4.4.2  skrll 	int rate, i;
    821  1.4.4.2  skrll 
    822  1.4.4.2  skrll 	imr->ifm_status = IFM_AVALID;
    823  1.4.4.2  skrll 	imr->ifm_active = IFM_IEEE80211;
    824  1.4.4.2  skrll 	if (ic->ic_state == IEEE80211_S_RUN)
    825  1.4.4.2  skrll 		imr->ifm_status |= IFM_ACTIVE;
    826  1.4.4.2  skrll 
    827  1.4.4.2  skrll 	/* read current transmission rate from adapter */
    828  1.4.4.2  skrll 	val = CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE);
    829  1.4.4.2  skrll 
    830  1.4.4.2  skrll 	/* convert rate to 802.11 rate */
    831  1.4.4.2  skrll 	for (i = 0; i < N(rates) && rates[i].val != val; i++);
    832  1.4.4.2  skrll 	rate = (i < N(rates)) ? rates[i].rate : 0;
    833  1.4.4.2  skrll 
    834  1.4.4.2  skrll 	imr->ifm_active |= ieee80211_rate2media(ic, rate, ic->ic_curmode);
    835  1.4.4.2  skrll 	switch (ic->ic_opmode) {
    836  1.4.4.2  skrll 	case IEEE80211_M_STA:
    837  1.4.4.2  skrll 		break;
    838  1.4.4.2  skrll 
    839  1.4.4.2  skrll 	case IEEE80211_M_IBSS:
    840  1.4.4.2  skrll 		imr->ifm_active |= IFM_IEEE80211_ADHOC;
    841  1.4.4.2  skrll 		break;
    842  1.4.4.2  skrll 
    843  1.4.4.2  skrll 	case IEEE80211_M_MONITOR:
    844  1.4.4.2  skrll 		imr->ifm_active |= IFM_IEEE80211_MONITOR;
    845  1.4.4.2  skrll 		break;
    846  1.4.4.2  skrll 
    847  1.4.4.2  skrll 	case IEEE80211_M_AHDEMO:
    848  1.4.4.2  skrll 	case IEEE80211_M_HOSTAP:
    849  1.4.4.2  skrll 		/* should not get there */
    850  1.4.4.2  skrll 		break;
    851  1.4.4.2  skrll 	}
    852  1.4.4.2  skrll #undef N
    853  1.4.4.2  skrll }
    854  1.4.4.2  skrll 
    855  1.4.4.2  skrll static int
    856  1.4.4.2  skrll iwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
    857  1.4.4.2  skrll {
    858  1.4.4.4  skrll 	struct iwi_softc *sc = ic->ic_ifp->if_softc;
    859  1.4.4.2  skrll 
    860  1.4.4.2  skrll 	switch (nstate) {
    861  1.4.4.2  skrll 	case IEEE80211_S_SCAN:
    862  1.4.4.4  skrll 		if (sc->flags & IWI_FLAG_SCANNING)
    863  1.4.4.4  skrll 			break;
    864  1.4.4.4  skrll 
    865  1.4.4.4  skrll 		ieee80211_node_table_reset(&ic->ic_scan);
    866  1.4.4.4  skrll 		ic->ic_flags |= IEEE80211_F_SCAN | IEEE80211_F_ASCAN;
    867  1.4.4.4  skrll 		sc->flags |= IWI_FLAG_SCANNING;
    868  1.4.4.2  skrll 		iwi_scan(sc);
    869  1.4.4.2  skrll 		break;
    870  1.4.4.2  skrll 
    871  1.4.4.2  skrll 	case IEEE80211_S_AUTH:
    872  1.4.4.2  skrll 		iwi_auth_and_assoc(sc);
    873  1.4.4.2  skrll 		break;
    874  1.4.4.2  skrll 
    875  1.4.4.2  skrll 	case IEEE80211_S_RUN:
    876  1.4.4.2  skrll 		if (ic->ic_opmode == IEEE80211_M_IBSS)
    877  1.4.4.2  skrll 			ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
    878  1.4.4.4  skrll 		else if (ic->ic_opmode == IEEE80211_M_MONITOR)
    879  1.4.4.4  skrll 			iwi_set_chan(sc, ic->ic_ibss_chan);
    880  1.4.4.4  skrll 
    881  1.4.4.4  skrll 		return (*sc->sc_newstate)(ic, nstate,
    882  1.4.4.4  skrll 		    IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
    883  1.4.4.2  skrll 
    884  1.4.4.2  skrll 	case IEEE80211_S_ASSOC:
    885  1.4.4.2  skrll 		break;
    886  1.4.4.4  skrll 
    887  1.4.4.4  skrll 	case IEEE80211_S_INIT:
    888  1.4.4.4  skrll 		sc->flags &= ~IWI_FLAG_SCANNING;
    889  1.4.4.4  skrll 		return (*sc->sc_newstate)(ic, nstate, arg);
    890  1.4.4.2  skrll 	}
    891  1.4.4.2  skrll 
    892  1.4.4.2  skrll 	ic->ic_state = nstate;
    893  1.4.4.2  skrll 	return 0;
    894  1.4.4.2  skrll }
    895  1.4.4.2  skrll 
    896  1.4.4.2  skrll /*
    897  1.4.4.2  skrll  * Read 16 bits at address 'addr' from the serial EEPROM.
    898  1.4.4.2  skrll  */
    899  1.4.4.4  skrll static uint16_t
    900  1.4.4.4  skrll iwi_read_prom_word(struct iwi_softc *sc, uint8_t addr)
    901  1.4.4.2  skrll {
    902  1.4.4.4  skrll 	uint32_t tmp;
    903  1.4.4.4  skrll 	uint16_t val;
    904  1.4.4.2  skrll 	int n;
    905  1.4.4.2  skrll 
    906  1.4.4.2  skrll 	/* Clock C once before the first command */
    907  1.4.4.2  skrll 	IWI_EEPROM_CTL(sc, 0);
    908  1.4.4.2  skrll 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
    909  1.4.4.2  skrll 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
    910  1.4.4.2  skrll 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
    911  1.4.4.2  skrll 
    912  1.4.4.2  skrll 	/* Write start bit (1) */
    913  1.4.4.2  skrll 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
    914  1.4.4.2  skrll 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
    915  1.4.4.2  skrll 
    916  1.4.4.2  skrll 	/* Write READ opcode (10) */
    917  1.4.4.2  skrll 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
    918  1.4.4.2  skrll 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
    919  1.4.4.2  skrll 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
    920  1.4.4.2  skrll 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
    921  1.4.4.2  skrll 
    922  1.4.4.2  skrll 	/* Write address A7-A0 */
    923  1.4.4.2  skrll 	for (n = 7; n >= 0; n--) {
    924  1.4.4.2  skrll 		IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
    925  1.4.4.2  skrll 		    (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D));
    926  1.4.4.2  skrll 		IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
    927  1.4.4.2  skrll 		    (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D) | IWI_EEPROM_C);
    928  1.4.4.2  skrll 	}
    929  1.4.4.2  skrll 
    930  1.4.4.2  skrll 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
    931  1.4.4.2  skrll 
    932  1.4.4.2  skrll 	/* Read data Q15-Q0 */
    933  1.4.4.2  skrll 	val = 0;
    934  1.4.4.2  skrll 	for (n = 15; n >= 0; n--) {
    935  1.4.4.2  skrll 		IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
    936  1.4.4.2  skrll 		IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
    937  1.4.4.2  skrll 		tmp = MEM_READ_4(sc, IWI_MEM_EEPROM_CTL);
    938  1.4.4.2  skrll 		val |= ((tmp & IWI_EEPROM_Q) >> IWI_EEPROM_SHIFT_Q) << n;
    939  1.4.4.2  skrll 	}
    940  1.4.4.2  skrll 
    941  1.4.4.2  skrll 	IWI_EEPROM_CTL(sc, 0);
    942  1.4.4.2  skrll 
    943  1.4.4.2  skrll 	/* Clear Chip Select and clock C */
    944  1.4.4.2  skrll 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
    945  1.4.4.2  skrll 	IWI_EEPROM_CTL(sc, 0);
    946  1.4.4.2  skrll 	IWI_EEPROM_CTL(sc, IWI_EEPROM_C);
    947  1.4.4.2  skrll 
    948  1.4.4.4  skrll 	return val;
    949  1.4.4.2  skrll }
    950  1.4.4.2  skrll 
    951  1.4.4.2  skrll /*
    952  1.4.4.2  skrll  * XXX: Hack to set the current channel to the value advertised in beacons or
    953  1.4.4.2  skrll  * probe responses. Only used during AP detection.
    954  1.4.4.2  skrll  */
    955  1.4.4.2  skrll static void
    956  1.4.4.2  skrll iwi_fix_channel(struct ieee80211com *ic, struct mbuf *m)
    957  1.4.4.2  skrll {
    958  1.4.4.2  skrll 	struct ieee80211_frame *wh;
    959  1.4.4.4  skrll 	uint8_t subtype;
    960  1.4.4.4  skrll 	uint8_t *frm, *efrm;
    961  1.4.4.2  skrll 
    962  1.4.4.2  skrll 	wh = mtod(m, struct ieee80211_frame *);
    963  1.4.4.2  skrll 
    964  1.4.4.2  skrll 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
    965  1.4.4.2  skrll 		return;
    966  1.4.4.2  skrll 
    967  1.4.4.2  skrll 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
    968  1.4.4.2  skrll 
    969  1.4.4.2  skrll 	if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
    970  1.4.4.2  skrll 	    subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
    971  1.4.4.2  skrll 		return;
    972  1.4.4.2  skrll 
    973  1.4.4.4  skrll 	frm = (uint8_t *)(wh + 1);
    974  1.4.4.4  skrll 	efrm = mtod(m, uint8_t *) + m->m_len;
    975  1.4.4.2  skrll 
    976  1.4.4.2  skrll 	frm += 12;	/* skip tstamp, bintval and capinfo fields */
    977  1.4.4.2  skrll 	while (frm < efrm) {
    978  1.4.4.2  skrll 		if (*frm == IEEE80211_ELEMID_DSPARMS)
    979  1.4.4.2  skrll #if IEEE80211_CHAN_MAX < 255
    980  1.4.4.2  skrll 		if (frm[2] <= IEEE80211_CHAN_MAX)
    981  1.4.4.2  skrll #endif
    982  1.4.4.2  skrll 			ic->ic_bss->ni_chan = &ic->ic_channels[frm[2]];
    983  1.4.4.2  skrll 
    984  1.4.4.2  skrll 		frm += frm[1] + 2;
    985  1.4.4.2  skrll 	}
    986  1.4.4.2  skrll }
    987  1.4.4.2  skrll 
    988  1.4.4.4  skrll static struct mbuf *
    989  1.4.4.4  skrll iwi_alloc_rx_buf(struct iwi_softc *sc)
    990  1.4.4.4  skrll {
    991  1.4.4.4  skrll 	struct mbuf *m;
    992  1.4.4.4  skrll 
    993  1.4.4.4  skrll 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    994  1.4.4.4  skrll 	if (m == NULL) {
    995  1.4.4.4  skrll 		aprint_error("%s: could not allocate rx mbuf\n",
    996  1.4.4.4  skrll 		    sc->sc_dev.dv_xname);
    997  1.4.4.4  skrll 		return NULL;
    998  1.4.4.4  skrll 	}
    999  1.4.4.4  skrll 
   1000  1.4.4.4  skrll 	MCLGET(m, M_DONTWAIT);
   1001  1.4.4.4  skrll 	if (!(m->m_flags & M_EXT)) {
   1002  1.4.4.4  skrll 		aprint_error("%s: could not allocate rx mbuf cluster\n",
   1003  1.4.4.4  skrll 		    sc->sc_dev.dv_xname);
   1004  1.4.4.4  skrll 		m_freem(m);
   1005  1.4.4.4  skrll 		return NULL;
   1006  1.4.4.4  skrll 	}
   1007  1.4.4.4  skrll 
   1008  1.4.4.4  skrll 	m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
   1009  1.4.4.4  skrll 	return m;
   1010  1.4.4.4  skrll }
   1011  1.4.4.4  skrll 
   1012  1.4.4.2  skrll static void
   1013  1.4.4.4  skrll iwi_frame_intr(struct iwi_softc *sc, struct iwi_rx_data *data, int i,
   1014  1.4.4.2  skrll     struct iwi_frame *frame)
   1015  1.4.4.2  skrll {
   1016  1.4.4.2  skrll 	struct ieee80211com *ic = &sc->sc_ic;
   1017  1.4.4.4  skrll 	struct ifnet *ifp = ic->ic_ifp;
   1018  1.4.4.4  skrll 	struct mbuf *m, *m_new;
   1019  1.4.4.2  skrll 	struct ieee80211_frame *wh;
   1020  1.4.4.2  skrll 	struct ieee80211_node *ni;
   1021  1.4.4.2  skrll 	int error;
   1022  1.4.4.2  skrll 
   1023  1.4.4.4  skrll 	DPRINTFN(5, ("received frame len=%u chan=%u rssi=%u\n",
   1024  1.4.4.4  skrll 	    le16toh(frame->len), frame->chan, frame->rssi_dbm));
   1025  1.4.4.2  skrll 
   1026  1.4.4.4  skrll 	if (le16toh(frame->len) < sizeof (struct ieee80211_frame) ||
   1027  1.4.4.2  skrll 	    le16toh(frame->len) > MCLBYTES) {
   1028  1.4.4.4  skrll 		DPRINTF(("%s: bad frame length\n", sc->sc_dev.dv_xname));
   1029  1.4.4.4  skrll 		ifp->if_ierrors++;
   1030  1.4.4.4  skrll 		return;
   1031  1.4.4.2  skrll 	}
   1032  1.4.4.2  skrll 
   1033  1.4.4.4  skrll 	/*
   1034  1.4.4.4  skrll 	 * Try to allocate a new mbuf for this ring element and
   1035  1.4.4.4  skrll 	 * load it before processing the current mbuf. If the ring
   1036  1.4.4.4  skrll 	 * element cannot be reloaded, drop the received packet
   1037  1.4.4.4  skrll 	 * and reuse the old mbuf. In the unlikely case that
   1038  1.4.4.4  skrll 	 * the old mbuf can't be reloaded either, explicitly panic.
   1039  1.4.4.4  skrll 	 *
   1040  1.4.4.4  skrll 	 * XXX Reorganize buffer by moving elements from the logical
   1041  1.4.4.4  skrll 	 * end of the ring to the front instead of dropping.
   1042  1.4.4.4  skrll 	 */
   1043  1.4.4.4  skrll 	if ((m_new = iwi_alloc_rx_buf(sc)) == NULL) {
   1044  1.4.4.4  skrll 		ifp->if_ierrors++;
   1045  1.4.4.4  skrll 		return;
   1046  1.4.4.4  skrll 	}
   1047  1.4.4.4  skrll 
   1048  1.4.4.4  skrll 	bus_dmamap_unload(sc->sc_dmat, data->map);
   1049  1.4.4.4  skrll 
   1050  1.4.4.4  skrll 	error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m_new,
   1051  1.4.4.4  skrll 	    BUS_DMA_READ | BUS_DMA_NOWAIT);
   1052  1.4.4.4  skrll 	if (error != 0) {
   1053  1.4.4.4  skrll 		aprint_error("%s: could not load rx buf DMA map\n",
   1054  1.4.4.4  skrll 		    sc->sc_dev.dv_xname);
   1055  1.4.4.4  skrll 		m_freem(m_new);
   1056  1.4.4.4  skrll 		ifp->if_ierrors++;
   1057  1.4.4.4  skrll 		error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map,
   1058  1.4.4.4  skrll 		    data->m, BUS_DMA_READ | BUS_DMA_NOWAIT);
   1059  1.4.4.4  skrll 		if (error)
   1060  1.4.4.4  skrll 			panic("%s: unable to remap rx buf",
   1061  1.4.4.4  skrll 			    sc->sc_dev.dv_xname);
   1062  1.4.4.4  skrll 		return;
   1063  1.4.4.4  skrll 	}
   1064  1.4.4.4  skrll 
   1065  1.4.4.4  skrll 	/*
   1066  1.4.4.4  skrll 	 * New mbuf successfully loaded, update RX ring and continue
   1067  1.4.4.4  skrll 	 * processing.
   1068  1.4.4.4  skrll 	 */
   1069  1.4.4.4  skrll 	m = data->m;
   1070  1.4.4.4  skrll 	data->m = m_new;
   1071  1.4.4.4  skrll 	CSR_WRITE_4(sc, IWI_CSR_RX_BASE + i * 4, data->map->dm_segs[0].ds_addr);
   1072  1.4.4.2  skrll 
   1073  1.4.4.2  skrll 	/* Finalize mbuf */
   1074  1.4.4.2  skrll 	m->m_pkthdr.rcvif = ifp;
   1075  1.4.4.2  skrll 	m->m_pkthdr.len = m->m_len = sizeof (struct iwi_hdr) +
   1076  1.4.4.2  skrll 	    sizeof (struct iwi_frame) + le16toh(frame->len);
   1077  1.4.4.2  skrll 
   1078  1.4.4.2  skrll 	m_adj(m, sizeof (struct iwi_hdr) + sizeof (struct iwi_frame));
   1079  1.4.4.2  skrll 
   1080  1.4.4.4  skrll 	if (ic->ic_state == IEEE80211_S_SCAN)
   1081  1.4.4.4  skrll 		iwi_fix_channel(ic, m);
   1082  1.4.4.2  skrll 
   1083  1.4.4.2  skrll #if NBPFILTER > 0
   1084  1.4.4.2  skrll 	if (sc->sc_drvbpf != NULL) {
   1085  1.4.4.2  skrll 		struct iwi_rx_radiotap_header *tap = &sc->sc_rxtap;
   1086  1.4.4.2  skrll 
   1087  1.4.4.4  skrll 		tap->wr_flags = 0;
   1088  1.4.4.4  skrll 		tap->wr_rate = frame->rate;
   1089  1.4.4.4  skrll 		tap->wr_chan_freq =
   1090  1.4.4.4  skrll 		    htole16(ic->ic_channels[frame->chan].ic_freq);
   1091  1.4.4.4  skrll 		tap->wr_chan_flags =
   1092  1.4.4.4  skrll 		    htole16(ic->ic_channels[frame->chan].ic_flags);
   1093  1.4.4.4  skrll 		tap->wr_antsignal = frame->signal;
   1094  1.4.4.4  skrll 		tap->wr_antenna = frame->antenna;
   1095  1.4.4.4  skrll 
   1096  1.4.4.4  skrll 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
   1097  1.4.4.2  skrll 	}
   1098  1.4.4.2  skrll #endif
   1099  1.4.4.2  skrll 
   1100  1.4.4.4  skrll 	wh = mtod(m, struct ieee80211_frame *);
   1101  1.4.4.4  skrll 	ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
   1102  1.4.4.3  skrll 
   1103  1.4.4.2  skrll 	/* Send the frame to the upper layer */
   1104  1.4.4.4  skrll 	ieee80211_input(ic, m, ni, frame->rssi_dbm, 0);
   1105  1.4.4.2  skrll 
   1106  1.4.4.4  skrll 	/* node is no longer needed */
   1107  1.4.4.4  skrll 	ieee80211_free_node(ni);
   1108  1.4.4.2  skrll }
   1109  1.4.4.2  skrll 
   1110  1.4.4.2  skrll static void
   1111  1.4.4.4  skrll iwi_notification_intr(struct iwi_softc *sc, struct iwi_notif *notif)
   1112  1.4.4.2  skrll {
   1113  1.4.4.2  skrll 	struct ieee80211com *ic = &sc->sc_ic;
   1114  1.4.4.2  skrll 	struct iwi_notif_scan_channel *chan;
   1115  1.4.4.2  skrll 	struct iwi_notif_scan_complete *scan;
   1116  1.4.4.2  skrll 	struct iwi_notif_authentication *auth;
   1117  1.4.4.2  skrll 	struct iwi_notif_association *assoc;
   1118  1.4.4.2  skrll 
   1119  1.4.4.2  skrll 	switch (notif->type) {
   1120  1.4.4.2  skrll 	case IWI_NOTIF_TYPE_SCAN_CHANNEL:
   1121  1.4.4.2  skrll 		chan = (struct iwi_notif_scan_channel *)(notif + 1);
   1122  1.4.4.2  skrll 
   1123  1.4.4.4  skrll 		DPRINTFN(2, ("Finished scanning channel (%u)\n", chan->nchan));
   1124  1.4.4.2  skrll 		break;
   1125  1.4.4.2  skrll 
   1126  1.4.4.2  skrll 	case IWI_NOTIF_TYPE_SCAN_COMPLETE:
   1127  1.4.4.2  skrll 		scan = (struct iwi_notif_scan_complete *)(notif + 1);
   1128  1.4.4.2  skrll 
   1129  1.4.4.2  skrll 		DPRINTFN(2, ("Scan completed (%u, %u)\n", scan->nchan,
   1130  1.4.4.2  skrll 		    scan->status));
   1131  1.4.4.2  skrll 
   1132  1.4.4.4  skrll 		/* monitor mode uses scan to set the channel ... */
   1133  1.4.4.4  skrll 		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
   1134  1.4.4.4  skrll 			sc->flags &= ~IWI_FLAG_SCANNING;
   1135  1.4.4.4  skrll 			ieee80211_end_scan(ic);
   1136  1.4.4.4  skrll 		} else
   1137  1.4.4.4  skrll 			iwi_set_chan(sc, ic->ic_ibss_chan);
   1138  1.4.4.2  skrll 		break;
   1139  1.4.4.2  skrll 
   1140  1.4.4.2  skrll 	case IWI_NOTIF_TYPE_AUTHENTICATION:
   1141  1.4.4.2  skrll 		auth = (struct iwi_notif_authentication *)(notif + 1);
   1142  1.4.4.2  skrll 
   1143  1.4.4.2  skrll 		DPRINTFN(2, ("Authentication (%u)\n", auth->state));
   1144  1.4.4.2  skrll 
   1145  1.4.4.2  skrll 		switch (auth->state) {
   1146  1.4.4.2  skrll 		case IWI_AUTHENTICATED:
   1147  1.4.4.4  skrll 			ieee80211_node_authorize(ic, ic->ic_bss);
   1148  1.4.4.2  skrll 			ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1);
   1149  1.4.4.2  skrll 			break;
   1150  1.4.4.2  skrll 
   1151  1.4.4.2  skrll 		case IWI_DEAUTHENTICATED:
   1152  1.4.4.2  skrll 			break;
   1153  1.4.4.2  skrll 
   1154  1.4.4.2  skrll 		default:
   1155  1.4.4.2  skrll 			aprint_error("%s: unknown authentication state %u\n",
   1156  1.4.4.2  skrll 			    sc->sc_dev.dv_xname, auth->state);
   1157  1.4.4.2  skrll 		}
   1158  1.4.4.2  skrll 		break;
   1159  1.4.4.2  skrll 
   1160  1.4.4.2  skrll 	case IWI_NOTIF_TYPE_ASSOCIATION:
   1161  1.4.4.2  skrll 		assoc = (struct iwi_notif_association *)(notif + 1);
   1162  1.4.4.2  skrll 
   1163  1.4.4.2  skrll 		DPRINTFN(2, ("Association (%u, %u)\n", assoc->state,
   1164  1.4.4.2  skrll 		    assoc->status));
   1165  1.4.4.2  skrll 
   1166  1.4.4.2  skrll 		switch (assoc->state) {
   1167  1.4.4.4  skrll 		case IWI_AUTHENTICATED:
   1168  1.4.4.4  skrll 			/* re-association, do nothing */
   1169  1.4.4.4  skrll 			break;
   1170  1.4.4.4  skrll 
   1171  1.4.4.2  skrll 		case IWI_ASSOCIATED:
   1172  1.4.4.2  skrll 			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
   1173  1.4.4.2  skrll 			break;
   1174  1.4.4.2  skrll 
   1175  1.4.4.2  skrll 		case IWI_DEASSOCIATED:
   1176  1.4.4.4  skrll 			ieee80211_begin_scan(ic, 1);
   1177  1.4.4.2  skrll 			break;
   1178  1.4.4.2  skrll 
   1179  1.4.4.2  skrll 		default:
   1180  1.4.4.2  skrll 			aprint_error("%s: unknown association state %u\n",
   1181  1.4.4.2  skrll 			    sc->sc_dev.dv_xname, assoc->state);
   1182  1.4.4.2  skrll 		}
   1183  1.4.4.2  skrll 		break;
   1184  1.4.4.2  skrll 
   1185  1.4.4.2  skrll 	case IWI_NOTIF_TYPE_CALIBRATION:
   1186  1.4.4.2  skrll 	case IWI_NOTIF_TYPE_BEACON:
   1187  1.4.4.2  skrll 	case IWI_NOTIF_TYPE_NOISE:
   1188  1.4.4.2  skrll 		DPRINTFN(5, ("Notification (%u)\n", notif->type));
   1189  1.4.4.2  skrll 		break;
   1190  1.4.4.2  skrll 
   1191  1.4.4.2  skrll 	default:
   1192  1.4.4.2  skrll 		aprint_error("%s: unknown notification type %u\n",
   1193  1.4.4.2  skrll 		    sc->sc_dev.dv_xname, notif->type);
   1194  1.4.4.2  skrll 	}
   1195  1.4.4.2  skrll }
   1196  1.4.4.2  skrll 
   1197  1.4.4.2  skrll static void
   1198  1.4.4.2  skrll iwi_rx_intr(struct iwi_softc *sc)
   1199  1.4.4.2  skrll {
   1200  1.4.4.4  skrll 	struct iwi_rx_data *data;
   1201  1.4.4.2  skrll 	struct iwi_hdr *hdr;
   1202  1.4.4.4  skrll 	uint32_t hw;
   1203  1.4.4.2  skrll 
   1204  1.4.4.4  skrll 	hw = CSR_READ_4(sc, IWI_CSR_RX_RIDX);
   1205  1.4.4.2  skrll 
   1206  1.4.4.4  skrll 	for (; sc->rxq.cur != hw;) {
   1207  1.4.4.4  skrll 		data = &sc->rxq.data[sc->rxq.cur];
   1208  1.4.4.2  skrll 
   1209  1.4.4.4  skrll 		bus_dmamap_sync(sc->sc_dmat, data->map, 0,
   1210  1.4.4.4  skrll 		    data->map->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1211  1.4.4.2  skrll 
   1212  1.4.4.4  skrll 		hdr = mtod(data->m, struct iwi_hdr *);
   1213  1.4.4.2  skrll 
   1214  1.4.4.2  skrll 		switch (hdr->type) {
   1215  1.4.4.2  skrll 		case IWI_HDR_TYPE_FRAME:
   1216  1.4.4.4  skrll 			iwi_frame_intr(sc, data, sc->rxq.cur,
   1217  1.4.4.2  skrll 			    (struct iwi_frame *)(hdr + 1));
   1218  1.4.4.2  skrll 			break;
   1219  1.4.4.2  skrll 
   1220  1.4.4.2  skrll 		case IWI_HDR_TYPE_NOTIF:
   1221  1.4.4.4  skrll 			iwi_notification_intr(sc,
   1222  1.4.4.2  skrll 			    (struct iwi_notif *)(hdr + 1));
   1223  1.4.4.2  skrll 			break;
   1224  1.4.4.2  skrll 
   1225  1.4.4.2  skrll 		default:
   1226  1.4.4.2  skrll 			aprint_error("%s: unknown hdr type %u\n",
   1227  1.4.4.2  skrll 			    sc->sc_dev.dv_xname, hdr->type);
   1228  1.4.4.2  skrll 		}
   1229  1.4.4.4  skrll 
   1230  1.4.4.4  skrll 		DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur));
   1231  1.4.4.4  skrll 
   1232  1.4.4.4  skrll 		sc->rxq.cur = (sc->rxq.cur + 1) % sc->rxq.count;
   1233  1.4.4.2  skrll 	}
   1234  1.4.4.2  skrll 
   1235  1.4.4.4  skrll 
   1236  1.4.4.2  skrll 	/* Tell the firmware what we have processed */
   1237  1.4.4.4  skrll 	hw = (hw == 0) ? sc->rxq.count - 1 : hw - 1;
   1238  1.4.4.4  skrll 	CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, hw);
   1239  1.4.4.2  skrll }
   1240  1.4.4.2  skrll 
   1241  1.4.4.2  skrll static void
   1242  1.4.4.2  skrll iwi_tx_intr(struct iwi_softc *sc)
   1243  1.4.4.2  skrll {
   1244  1.4.4.4  skrll 	struct ifnet *ifp = &sc->sc_if;
   1245  1.4.4.4  skrll 	struct iwi_tx_data *data;
   1246  1.4.4.4  skrll 	uint32_t hw;
   1247  1.4.4.2  skrll 
   1248  1.4.4.4  skrll 	hw = CSR_READ_4(sc, IWI_CSR_TX1_RIDX);
   1249  1.4.4.2  skrll 
   1250  1.4.4.4  skrll 	for (; sc->txq.next != hw;) {
   1251  1.4.4.4  skrll 		data = &sc->txq.data[sc->txq.next];
   1252  1.4.4.2  skrll 
   1253  1.4.4.4  skrll 		bus_dmamap_sync(sc->sc_dmat, data->map, 0,
   1254  1.4.4.4  skrll 		    data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1255  1.4.4.4  skrll 		bus_dmamap_unload(sc->sc_dmat, data->map);
   1256  1.4.4.4  skrll 		m_freem(data->m);
   1257  1.4.4.4  skrll 		data->m = NULL;
   1258  1.4.4.4  skrll 		ieee80211_free_node(data->ni);
   1259  1.4.4.4  skrll 		data->ni = NULL;
   1260  1.4.4.2  skrll 
   1261  1.4.4.4  skrll 		DPRINTFN(15, ("tx done idx=%u\n", sc->txq.next));
   1262  1.4.4.2  skrll 
   1263  1.4.4.4  skrll 		ifp->if_opackets++;
   1264  1.4.4.2  skrll 
   1265  1.4.4.4  skrll 		sc->txq.queued--;
   1266  1.4.4.4  skrll 		sc->txq.next = (sc->txq.next + 1) % sc->txq.count;
   1267  1.4.4.2  skrll 	}
   1268  1.4.4.2  skrll 
   1269  1.4.4.4  skrll 	sc->sc_tx_timer = 0;
   1270  1.4.4.4  skrll 	ifp->if_flags &= ~IFF_OACTIVE;
   1271  1.4.4.2  skrll 
   1272  1.4.4.2  skrll 	/* Call start() since some buffer descriptors have been released */
   1273  1.4.4.2  skrll 	(*ifp->if_start)(ifp);
   1274  1.4.4.2  skrll }
   1275  1.4.4.2  skrll 
   1276  1.4.4.2  skrll static int
   1277  1.4.4.2  skrll iwi_intr(void *arg)
   1278  1.4.4.2  skrll {
   1279  1.4.4.2  skrll 	struct iwi_softc *sc = arg;
   1280  1.4.4.4  skrll 	uint32_t r;
   1281  1.4.4.2  skrll 
   1282  1.4.4.2  skrll 	if ((r = CSR_READ_4(sc, IWI_CSR_INTR)) == 0 || r == 0xffffffff)
   1283  1.4.4.2  skrll 		return 0;
   1284  1.4.4.2  skrll 
   1285  1.4.4.4  skrll 	/* Acknowledge interrupts */
   1286  1.4.4.4  skrll 	CSR_WRITE_4(sc, IWI_CSR_INTR, r);
   1287  1.4.4.2  skrll 
   1288  1.4.4.2  skrll 	if (r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR)) {
   1289  1.4.4.2  skrll 		aprint_error("%s: fatal error\n", sc->sc_dev.dv_xname);
   1290  1.4.4.4  skrll 		sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP;
   1291  1.4.4.4  skrll 		iwi_stop(&sc->sc_if, 1);
   1292  1.4.4.2  skrll 	}
   1293  1.4.4.2  skrll 
   1294  1.4.4.2  skrll 	if (r & IWI_INTR_FW_INITED) {
   1295  1.4.4.2  skrll 		if (!(r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR)))
   1296  1.4.4.2  skrll 			wakeup(sc);
   1297  1.4.4.2  skrll 	}
   1298  1.4.4.2  skrll 
   1299  1.4.4.2  skrll 	if (r & IWI_INTR_RADIO_OFF) {
   1300  1.4.4.2  skrll 		DPRINTF(("radio transmitter off\n"));
   1301  1.4.4.4  skrll 		sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP;
   1302  1.4.4.4  skrll 		iwi_stop(&sc->sc_if, 1);
   1303  1.4.4.2  skrll 	}
   1304  1.4.4.2  skrll 
   1305  1.4.4.4  skrll 	if (r & IWI_INTR_RX_DONE)
   1306  1.4.4.2  skrll 		iwi_rx_intr(sc);
   1307  1.4.4.2  skrll 
   1308  1.4.4.4  skrll 	if (r & IWI_INTR_CMD_DONE)
   1309  1.4.4.2  skrll 		wakeup(sc);
   1310  1.4.4.2  skrll 
   1311  1.4.4.4  skrll 	if (r & IWI_INTR_TX1_DONE)
   1312  1.4.4.2  skrll 		iwi_tx_intr(sc);
   1313  1.4.4.2  skrll 
   1314  1.4.4.2  skrll 	return 1;
   1315  1.4.4.2  skrll }
   1316  1.4.4.2  skrll 
   1317  1.4.4.2  skrll static int
   1318  1.4.4.4  skrll iwi_cmd(struct iwi_softc *sc, uint8_t type, void *data, uint8_t len,
   1319  1.4.4.2  skrll     int async)
   1320  1.4.4.2  skrll {
   1321  1.4.4.2  skrll 	struct iwi_cmd_desc *desc;
   1322  1.4.4.2  skrll 
   1323  1.4.4.4  skrll 	desc = &sc->cmdq.desc[sc->cmdq.cur];
   1324  1.4.4.2  skrll 
   1325  1.4.4.2  skrll 	desc->hdr.type = IWI_HDR_TYPE_COMMAND;
   1326  1.4.4.2  skrll 	desc->hdr.flags = IWI_HDR_FLAG_IRQ;
   1327  1.4.4.2  skrll 	desc->type = type;
   1328  1.4.4.2  skrll 	desc->len = len;
   1329  1.4.4.2  skrll 	memcpy(desc->data, data, len);
   1330  1.4.4.2  skrll 
   1331  1.4.4.4  skrll 	bus_dmamap_sync(sc->sc_dmat, sc->cmdq.desc_map,
   1332  1.4.4.4  skrll 	    sc->cmdq.cur * IWI_CMD_DESC_SIZE,
   1333  1.4.4.4  skrll 	    IWI_CMD_DESC_SIZE, BUS_DMASYNC_PREWRITE);
   1334  1.4.4.4  skrll 
   1335  1.4.4.4  skrll 	DPRINTFN(2, ("sending command idx=%u type=%u len=%u\n", sc->cmdq.cur,
   1336  1.4.4.4  skrll 	    type, len));
   1337  1.4.4.2  skrll 
   1338  1.4.4.4  skrll 	sc->cmdq.cur = (sc->cmdq.cur + 1) % sc->cmdq.count;
   1339  1.4.4.4  skrll 	CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
   1340  1.4.4.2  skrll 
   1341  1.4.4.2  skrll 	return async ? 0 : tsleep(sc, 0, "iwicmd", hz);
   1342  1.4.4.2  skrll }
   1343  1.4.4.2  skrll 
   1344  1.4.4.2  skrll static int
   1345  1.4.4.2  skrll iwi_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni)
   1346  1.4.4.2  skrll {
   1347  1.4.4.2  skrll 	struct iwi_softc *sc = ifp->if_softc;
   1348  1.4.4.2  skrll 	struct ieee80211com *ic = &sc->sc_ic;
   1349  1.4.4.4  skrll 	struct ieee80211_frame wh;
   1350  1.4.4.4  skrll 	struct ieee80211_key *k;
   1351  1.4.4.4  skrll 	struct iwi_tx_data *data;
   1352  1.4.4.2  skrll 	struct iwi_tx_desc *desc;
   1353  1.4.4.2  skrll 	struct mbuf *mnew;
   1354  1.4.4.2  skrll 	int error, i;
   1355  1.4.4.2  skrll 
   1356  1.4.4.4  skrll 	(void)memcpy(&wh, mtod(m0, struct ieee80211_frame *), sizeof(wh));
   1357  1.4.4.4  skrll 	if (wh.i_fc[1] & IEEE80211_FC1_WEP) {
   1358  1.4.4.4  skrll 		k = ieee80211_crypto_encap(ic, ni, m0);
   1359  1.4.4.4  skrll 		if (k == NULL) {
   1360  1.4.4.4  skrll 			m_freem(m0);
   1361  1.4.4.4  skrll 			return ENOBUFS;
   1362  1.4.4.4  skrll 		}
   1363  1.4.4.4  skrll 	}
   1364  1.4.4.4  skrll 
   1365  1.4.4.2  skrll #if NBPFILTER > 0
   1366  1.4.4.2  skrll 	if (sc->sc_drvbpf != NULL) {
   1367  1.4.4.2  skrll 		struct iwi_tx_radiotap_header *tap = &sc->sc_txtap;
   1368  1.4.4.2  skrll 
   1369  1.4.4.2  skrll 		tap->wt_flags = 0;
   1370  1.4.4.4  skrll 		tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
   1371  1.4.4.4  skrll 		tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
   1372  1.4.4.2  skrll 
   1373  1.4.4.2  skrll 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
   1374  1.4.4.2  skrll 	}
   1375  1.4.4.2  skrll #endif
   1376  1.4.4.2  skrll 
   1377  1.4.4.4  skrll 	data = &sc->txq.data[sc->txq.cur];
   1378  1.4.4.4  skrll 	desc = &sc->txq.desc[sc->txq.cur];
   1379  1.4.4.2  skrll 
   1380  1.4.4.2  skrll 	/* trim IEEE802.11 header */
   1381  1.4.4.2  skrll 	m_adj(m0, sizeof (struct ieee80211_frame));
   1382  1.4.4.2  skrll 
   1383  1.4.4.4  skrll 	error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
   1384  1.4.4.4  skrll 	    BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   1385  1.4.4.2  skrll 	if (error != 0 && error != EFBIG) {
   1386  1.4.4.2  skrll 		aprint_error("%s: could not map mbuf (error %d)\n",
   1387  1.4.4.2  skrll 		    sc->sc_dev.dv_xname, error);
   1388  1.4.4.2  skrll 		m_freem(m0);
   1389  1.4.4.2  skrll 		return error;
   1390  1.4.4.2  skrll 	}
   1391  1.4.4.2  skrll 	if (error != 0) {
   1392  1.4.4.2  skrll 		/* too many fragments, linearize */
   1393  1.4.4.2  skrll 
   1394  1.4.4.2  skrll 		MGETHDR(mnew, M_DONTWAIT, MT_DATA);
   1395  1.4.4.2  skrll 		if (mnew == NULL) {
   1396  1.4.4.2  skrll 			m_freem(m0);
   1397  1.4.4.2  skrll 			return ENOMEM;
   1398  1.4.4.2  skrll 		}
   1399  1.4.4.2  skrll 
   1400  1.4.4.2  skrll 		M_COPY_PKTHDR(mnew, m0);
   1401  1.4.4.2  skrll 
   1402  1.4.4.4  skrll 		/* If the data won't fit in the header, get a cluster */
   1403  1.4.4.4  skrll 		if (m0->m_pkthdr.len > MHLEN) {
   1404  1.4.4.4  skrll 			MCLGET(mnew, M_DONTWAIT);
   1405  1.4.4.4  skrll 			if (!(mnew->m_flags & M_EXT)) {
   1406  1.4.4.4  skrll 				m_freem(m0);
   1407  1.4.4.4  skrll 				m_freem(mnew);
   1408  1.4.4.4  skrll 				return ENOMEM;
   1409  1.4.4.4  skrll 			}
   1410  1.4.4.4  skrll 		}
   1411  1.4.4.2  skrll 		m_copydata(m0, 0, m0->m_pkthdr.len, mtod(mnew, caddr_t));
   1412  1.4.4.2  skrll 		m_freem(m0);
   1413  1.4.4.2  skrll 		mnew->m_len = mnew->m_pkthdr.len;
   1414  1.4.4.2  skrll 		m0 = mnew;
   1415  1.4.4.2  skrll 
   1416  1.4.4.4  skrll 		error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
   1417  1.4.4.4  skrll 		    BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   1418  1.4.4.2  skrll 		if (error != 0) {
   1419  1.4.4.2  skrll 			aprint_error("%s: could not map mbuf (error %d)\n",
   1420  1.4.4.2  skrll 			    sc->sc_dev.dv_xname, error);
   1421  1.4.4.2  skrll 			m_freem(m0);
   1422  1.4.4.2  skrll 			return error;
   1423  1.4.4.2  skrll 		}
   1424  1.4.4.2  skrll 	}
   1425  1.4.4.2  skrll 
   1426  1.4.4.4  skrll 	data->m = m0;
   1427  1.4.4.4  skrll 	data->ni = ni;
   1428  1.4.4.2  skrll 
   1429  1.4.4.2  skrll 	desc->hdr.type = IWI_HDR_TYPE_DATA;
   1430  1.4.4.2  skrll 	desc->hdr.flags = IWI_HDR_FLAG_IRQ;
   1431  1.4.4.2  skrll 	desc->cmd = IWI_DATA_CMD_TX;
   1432  1.4.4.2  skrll 	desc->len = htole16(m0->m_pkthdr.len);
   1433  1.4.4.4  skrll 	(void)memcpy(&desc->wh, &wh, sizeof (struct ieee80211_frame));
   1434  1.4.4.2  skrll 	desc->flags = 0;
   1435  1.4.4.4  skrll 	if (!IEEE80211_IS_MULTICAST(wh.i_addr1))
   1436  1.4.4.2  skrll 		desc->flags |= IWI_DATA_FLAG_NEED_ACK;
   1437  1.4.4.2  skrll 
   1438  1.4.4.4  skrll #if 0
   1439  1.4.4.2  skrll 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
   1440  1.4.4.4  skrll 		wh.i_fc[1] |= IEEE80211_FC1_WEP;
   1441  1.4.4.4  skrll 		desc->wep_txkey = ic->ic_crypto.cs_def_txkey;
   1442  1.4.4.2  skrll 	} else
   1443  1.4.4.4  skrll #endif
   1444  1.4.4.2  skrll 		desc->flags |= IWI_DATA_FLAG_NO_WEP;
   1445  1.4.4.2  skrll 
   1446  1.4.4.2  skrll 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
   1447  1.4.4.2  skrll 		desc->flags |= IWI_DATA_FLAG_SHPREAMBLE;
   1448  1.4.4.2  skrll 
   1449  1.4.4.4  skrll 	desc->nseg = htole32(data->map->dm_nsegs);
   1450  1.4.4.4  skrll 	for (i = 0; i < data->map->dm_nsegs; i++) {
   1451  1.4.4.4  skrll 		desc->seg_addr[i] = htole32(data->map->dm_segs[i].ds_addr);
   1452  1.4.4.4  skrll 		desc->seg_len[i]  = htole16(data->map->dm_segs[i].ds_len);
   1453  1.4.4.2  skrll 	}
   1454  1.4.4.2  skrll 
   1455  1.4.4.4  skrll 	bus_dmamap_sync(sc->sc_dmat, sc->txq.desc_map,
   1456  1.4.4.4  skrll 	    sc->txq.cur * IWI_TX_DESC_SIZE,
   1457  1.4.4.4  skrll 	    IWI_TX_DESC_SIZE, BUS_DMASYNC_PREWRITE);
   1458  1.4.4.2  skrll 
   1459  1.4.4.4  skrll 	bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize,
   1460  1.4.4.2  skrll 	    BUS_DMASYNC_PREWRITE);
   1461  1.4.4.2  skrll 
   1462  1.4.4.4  skrll 	DPRINTFN(5, ("sending data frame len=%u nseg=%u\n",
   1463  1.4.4.4  skrll 	    le16toh(desc->len), le32toh(desc->nseg)));
   1464  1.4.4.2  skrll 
   1465  1.4.4.2  skrll 	/* Inform firmware about this new packet */
   1466  1.4.4.4  skrll 	sc->txq.queued++;
   1467  1.4.4.4  skrll 	sc->txq.cur = (sc->txq.cur + 1) % sc->txq.count;
   1468  1.4.4.4  skrll 	CSR_WRITE_4(sc, IWI_CSR_TX1_WIDX, sc->txq.cur);
   1469  1.4.4.2  skrll 
   1470  1.4.4.2  skrll 	return 0;
   1471  1.4.4.2  skrll }
   1472  1.4.4.2  skrll 
   1473  1.4.4.2  skrll static void
   1474  1.4.4.2  skrll iwi_start(struct ifnet *ifp)
   1475  1.4.4.2  skrll {
   1476  1.4.4.2  skrll 	struct iwi_softc *sc = ifp->if_softc;
   1477  1.4.4.2  skrll 	struct ieee80211com *ic = &sc->sc_ic;
   1478  1.4.4.2  skrll 	struct mbuf *m0;
   1479  1.4.4.4  skrll 	struct ether_header *eh;
   1480  1.4.4.2  skrll 	struct ieee80211_node *ni;
   1481  1.4.4.2  skrll 
   1482  1.4.4.2  skrll 	if (ic->ic_state != IEEE80211_S_RUN)
   1483  1.4.4.2  skrll 		return;
   1484  1.4.4.2  skrll 
   1485  1.4.4.2  skrll 	for (;;) {
   1486  1.4.4.2  skrll 		IF_DEQUEUE(&ifp->if_snd, m0);
   1487  1.4.4.2  skrll 		if (m0 == NULL)
   1488  1.4.4.2  skrll 			break;
   1489  1.4.4.2  skrll 
   1490  1.4.4.4  skrll 		if (sc->txq.queued >= sc->txq.count - 4) {
   1491  1.4.4.2  skrll 			IF_PREPEND(&ifp->if_snd, m0);
   1492  1.4.4.2  skrll 			ifp->if_flags |= IFF_OACTIVE;
   1493  1.4.4.2  skrll 			break;
   1494  1.4.4.2  skrll 		}
   1495  1.4.4.2  skrll 
   1496  1.4.4.4  skrll 		if (m0->m_len < sizeof (struct ether_header) &&
   1497  1.4.4.4  skrll 		    (m0 = m_pullup(m0, sizeof (struct ether_header))) == NULL)
   1498  1.4.4.4  skrll 			continue;
   1499  1.4.4.4  skrll 
   1500  1.4.4.2  skrll #if NBPFILTER > 0
   1501  1.4.4.2  skrll 		if (ifp->if_bpf != NULL)
   1502  1.4.4.2  skrll 			bpf_mtap(ifp->if_bpf, m0);
   1503  1.4.4.2  skrll #endif
   1504  1.4.4.2  skrll 
   1505  1.4.4.4  skrll 		eh = mtod(m0, struct ether_header *);
   1506  1.4.4.4  skrll 		ni = ieee80211_find_txnode(ic, eh->ether_dhost);
   1507  1.4.4.4  skrll 		if (ni == NULL) {
   1508  1.4.4.4  skrll 			m_freem(m0);
   1509  1.4.4.4  skrll 			continue;
   1510  1.4.4.4  skrll 		}
   1511  1.4.4.4  skrll 
   1512  1.4.4.4  skrll 		m0 = ieee80211_encap(ic, m0, ni);
   1513  1.4.4.4  skrll 		if (m0 == NULL) {
   1514  1.4.4.4  skrll 			ieee80211_free_node(ni);
   1515  1.4.4.2  skrll 			continue;
   1516  1.4.4.4  skrll 		}
   1517  1.4.4.2  skrll 
   1518  1.4.4.2  skrll #if NBPFILTER > 0
   1519  1.4.4.2  skrll 		if (ic->ic_rawbpf != NULL)
   1520  1.4.4.2  skrll 			bpf_mtap(ic->ic_rawbpf, m0);
   1521  1.4.4.2  skrll #endif
   1522  1.4.4.2  skrll 
   1523  1.4.4.2  skrll 		if (iwi_tx_start(ifp, m0, ni) != 0) {
   1524  1.4.4.4  skrll 			ieee80211_free_node(ni);
   1525  1.4.4.4  skrll 			ifp->if_oerrors++;
   1526  1.4.4.2  skrll 			break;
   1527  1.4.4.2  skrll 		}
   1528  1.4.4.2  skrll 
   1529  1.4.4.2  skrll 		/* start watchdog timer */
   1530  1.4.4.2  skrll 		sc->sc_tx_timer = 5;
   1531  1.4.4.2  skrll 		ifp->if_timer = 1;
   1532  1.4.4.2  skrll 	}
   1533  1.4.4.2  skrll }
   1534  1.4.4.2  skrll 
   1535  1.4.4.2  skrll static void
   1536  1.4.4.2  skrll iwi_watchdog(struct ifnet *ifp)
   1537  1.4.4.2  skrll {
   1538  1.4.4.2  skrll 	struct iwi_softc *sc = ifp->if_softc;
   1539  1.4.4.2  skrll 
   1540  1.4.4.2  skrll 	ifp->if_timer = 0;
   1541  1.4.4.2  skrll 
   1542  1.4.4.2  skrll 	if (sc->sc_tx_timer > 0) {
   1543  1.4.4.2  skrll 		if (--sc->sc_tx_timer == 0) {
   1544  1.4.4.2  skrll 			aprint_error("%s: device timeout\n",
   1545  1.4.4.2  skrll 			    sc->sc_dev.dv_xname);
   1546  1.4.4.4  skrll 			ifp->if_oerrors++;
   1547  1.4.4.4  skrll 			ifp->if_flags &= ~IFF_UP;
   1548  1.4.4.2  skrll 			iwi_stop(ifp, 1);
   1549  1.4.4.2  skrll 			return;
   1550  1.4.4.2  skrll 		}
   1551  1.4.4.2  skrll 		ifp->if_timer = 1;
   1552  1.4.4.2  skrll 	}
   1553  1.4.4.2  skrll 
   1554  1.4.4.4  skrll 	ieee80211_watchdog(&sc->sc_ic);
   1555  1.4.4.2  skrll }
   1556  1.4.4.2  skrll 
   1557  1.4.4.2  skrll static int
   1558  1.4.4.4  skrll iwi_get_table0(struct iwi_softc *sc, uint32_t *tbl)
   1559  1.4.4.2  skrll {
   1560  1.4.4.4  skrll 	uint32_t size, buf[128];
   1561  1.4.4.2  skrll 
   1562  1.4.4.2  skrll 	if (!(sc->flags & IWI_FLAG_FW_INITED)) {
   1563  1.4.4.2  skrll 		memset(buf, 0, sizeof buf);
   1564  1.4.4.2  skrll 		return copyout(buf, tbl, sizeof buf);
   1565  1.4.4.2  skrll 	}
   1566  1.4.4.2  skrll 
   1567  1.4.4.2  skrll 	size = min(CSR_READ_4(sc, IWI_CSR_TABLE0_SIZE), 128 - 1);
   1568  1.4.4.2  skrll 	CSR_READ_REGION_4(sc, IWI_CSR_TABLE0_BASE, &buf[1], size);
   1569  1.4.4.2  skrll 
   1570  1.4.4.2  skrll 	return copyout(buf, tbl, sizeof buf);
   1571  1.4.4.2  skrll }
   1572  1.4.4.2  skrll 
   1573  1.4.4.2  skrll static int
   1574  1.4.4.2  skrll iwi_get_radio(struct iwi_softc *sc, int *ret)
   1575  1.4.4.2  skrll {
   1576  1.4.4.2  skrll 	int val;
   1577  1.4.4.2  skrll 
   1578  1.4.4.2  skrll 	val = (CSR_READ_4(sc, IWI_CSR_IO) & IWI_IO_RADIO_ENABLED) ? 1 : 0;
   1579  1.4.4.2  skrll 	return copyout(&val, ret, sizeof val);
   1580  1.4.4.2  skrll }
   1581  1.4.4.2  skrll 
   1582  1.4.4.2  skrll static int
   1583  1.4.4.2  skrll iwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
   1584  1.4.4.2  skrll {
   1585  1.4.4.2  skrll 	struct iwi_softc *sc = ifp->if_softc;
   1586  1.4.4.2  skrll 	struct ifreq *ifr;
   1587  1.4.4.2  skrll 	int s, error = 0;
   1588  1.4.4.2  skrll 
   1589  1.4.4.2  skrll 	s = splnet();
   1590  1.4.4.2  skrll 
   1591  1.4.4.2  skrll 	switch (cmd) {
   1592  1.4.4.2  skrll 	case SIOCSIFFLAGS:
   1593  1.4.4.2  skrll 		if (ifp->if_flags & IFF_UP) {
   1594  1.4.4.2  skrll 			if (!(ifp->if_flags & IFF_RUNNING))
   1595  1.4.4.2  skrll 				iwi_init(ifp);
   1596  1.4.4.2  skrll 		} else {
   1597  1.4.4.2  skrll 			if (ifp->if_flags & IFF_RUNNING)
   1598  1.4.4.2  skrll 				iwi_stop(ifp, 1);
   1599  1.4.4.2  skrll 		}
   1600  1.4.4.2  skrll 		break;
   1601  1.4.4.2  skrll 
   1602  1.4.4.2  skrll 	case SIOCGTABLE0:
   1603  1.4.4.2  skrll 		ifr = (struct ifreq *)data;
   1604  1.4.4.4  skrll 		error = iwi_get_table0(sc, (uint32_t *)ifr->ifr_data);
   1605  1.4.4.2  skrll 		break;
   1606  1.4.4.2  skrll 
   1607  1.4.4.2  skrll 	case SIOCGRADIO:
   1608  1.4.4.2  skrll 		ifr = (struct ifreq *)data;
   1609  1.4.4.2  skrll 		error = iwi_get_radio(sc, (int *)ifr->ifr_data);
   1610  1.4.4.2  skrll 		break;
   1611  1.4.4.2  skrll 
   1612  1.4.4.2  skrll 	case SIOCSLOADFW:
   1613  1.4.4.2  skrll 		/* only super-user can do that! */
   1614  1.4.4.2  skrll 		if ((error = suser(curproc->p_ucred, &curproc->p_acflag)) != 0)
   1615  1.4.4.2  skrll 			break;
   1616  1.4.4.2  skrll 
   1617  1.4.4.2  skrll 		ifr = (struct ifreq *)data;
   1618  1.4.4.2  skrll 		error = iwi_cache_firmware(sc, ifr->ifr_data);
   1619  1.4.4.2  skrll 		break;
   1620  1.4.4.2  skrll 
   1621  1.4.4.2  skrll 	case SIOCSKILLFW:
   1622  1.4.4.2  skrll 		/* only super-user can do that! */
   1623  1.4.4.2  skrll 		if ((error = suser(curproc->p_ucred, &curproc->p_acflag)) != 0)
   1624  1.4.4.2  skrll 			break;
   1625  1.4.4.2  skrll 
   1626  1.4.4.4  skrll 		ifp->if_flags &= ~IFF_UP;
   1627  1.4.4.2  skrll 		iwi_stop(ifp, 1);
   1628  1.4.4.2  skrll 		iwi_free_firmware(sc);
   1629  1.4.4.2  skrll 		break;
   1630  1.4.4.2  skrll 
   1631  1.4.4.2  skrll 	default:
   1632  1.4.4.4  skrll 		error = ieee80211_ioctl(&sc->sc_ic, cmd, data);
   1633  1.4.4.2  skrll 	}
   1634  1.4.4.2  skrll 
   1635  1.4.4.2  skrll 	if (error == ENETRESET && cmd != SIOCADDMULTI) {
   1636  1.4.4.2  skrll 		if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
   1637  1.4.4.2  skrll 		    (IFF_UP | IFF_RUNNING))
   1638  1.4.4.2  skrll 			iwi_init(ifp);
   1639  1.4.4.2  skrll 		error = 0;
   1640  1.4.4.2  skrll 	}
   1641  1.4.4.2  skrll 
   1642  1.4.4.2  skrll 	splx(s);
   1643  1.4.4.2  skrll 	return error;
   1644  1.4.4.2  skrll }
   1645  1.4.4.2  skrll 
   1646  1.4.4.2  skrll static void
   1647  1.4.4.2  skrll iwi_stop_master(struct iwi_softc *sc)
   1648  1.4.4.2  skrll {
   1649  1.4.4.2  skrll 	int ntries;
   1650  1.4.4.2  skrll 
   1651  1.4.4.2  skrll 	/* Disable interrupts */
   1652  1.4.4.2  skrll 	CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
   1653  1.4.4.2  skrll 
   1654  1.4.4.2  skrll 	CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_STOP_MASTER);
   1655  1.4.4.2  skrll 	for (ntries = 0; ntries < 5; ntries++) {
   1656  1.4.4.2  skrll 		if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
   1657  1.4.4.2  skrll 			break;
   1658  1.4.4.2  skrll 		DELAY(10);
   1659  1.4.4.2  skrll 	}
   1660  1.4.4.2  skrll 	if (ntries == 5)
   1661  1.4.4.2  skrll 		aprint_error("%s: timeout waiting for master\n",
   1662  1.4.4.2  skrll 		    sc->sc_dev.dv_xname);
   1663  1.4.4.2  skrll 
   1664  1.4.4.2  skrll 	CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
   1665  1.4.4.2  skrll 	    IWI_RST_PRINCETON_RESET);
   1666  1.4.4.2  skrll 
   1667  1.4.4.2  skrll 	sc->flags &= ~IWI_FLAG_FW_INITED;
   1668  1.4.4.2  skrll }
   1669  1.4.4.2  skrll 
   1670  1.4.4.2  skrll static int
   1671  1.4.4.2  skrll iwi_reset(struct iwi_softc *sc)
   1672  1.4.4.2  skrll {
   1673  1.4.4.2  skrll 	int i, ntries;
   1674  1.4.4.2  skrll 
   1675  1.4.4.2  skrll 	iwi_stop_master(sc);
   1676  1.4.4.2  skrll 
   1677  1.4.4.2  skrll 	/* Move adapter to D0 state */
   1678  1.4.4.2  skrll 	CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
   1679  1.4.4.2  skrll 	    IWI_CTL_INIT);
   1680  1.4.4.2  skrll 
   1681  1.4.4.2  skrll 	/* Initialize Phase-Locked Level  (PLL) */
   1682  1.4.4.2  skrll 	CSR_WRITE_4(sc, IWI_CSR_READ_INT, IWI_READ_INT_INIT_HOST);
   1683  1.4.4.2  skrll 
   1684  1.4.4.2  skrll 	/* Wait for clock stabilization */
   1685  1.4.4.2  skrll 	for (ntries = 0; ntries < 1000; ntries++) {
   1686  1.4.4.2  skrll 		if (CSR_READ_4(sc, IWI_CSR_CTL) & IWI_CTL_CLOCK_READY)
   1687  1.4.4.2  skrll 			break;
   1688  1.4.4.2  skrll 		DELAY(200);
   1689  1.4.4.2  skrll 	}
   1690  1.4.4.4  skrll 	if (ntries == 1000) {
   1691  1.4.4.4  skrll 		aprint_error("%s: timeout waiting for clock stabilization\n",
   1692  1.4.4.4  skrll 		    sc->sc_dev.dv_xname);
   1693  1.4.4.2  skrll 		return EIO;
   1694  1.4.4.4  skrll 	}
   1695  1.4.4.2  skrll 
   1696  1.4.4.2  skrll 	CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
   1697  1.4.4.2  skrll 	    IWI_RST_SW_RESET);
   1698  1.4.4.2  skrll 
   1699  1.4.4.2  skrll 	DELAY(10);
   1700  1.4.4.2  skrll 
   1701  1.4.4.2  skrll 	CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
   1702  1.4.4.2  skrll 	    IWI_CTL_INIT);
   1703  1.4.4.2  skrll 
   1704  1.4.4.2  skrll 	/* Clear NIC memory */
   1705  1.4.4.2  skrll 	CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0);
   1706  1.4.4.2  skrll 	for (i = 0; i < 0xc000; i++)
   1707  1.4.4.2  skrll 		CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
   1708  1.4.4.2  skrll 
   1709  1.4.4.2  skrll 	return 0;
   1710  1.4.4.2  skrll }
   1711  1.4.4.2  skrll 
   1712  1.4.4.2  skrll static int
   1713  1.4.4.2  skrll iwi_load_ucode(struct iwi_softc *sc, void *uc, int size)
   1714  1.4.4.2  skrll {
   1715  1.4.4.4  skrll 	uint16_t *w;
   1716  1.4.4.2  skrll 	int ntries, i;
   1717  1.4.4.2  skrll 
   1718  1.4.4.2  skrll 	CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
   1719  1.4.4.2  skrll 	    IWI_RST_STOP_MASTER);
   1720  1.4.4.2  skrll 	for (ntries = 0; ntries < 5; ntries++) {
   1721  1.4.4.2  skrll 		if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
   1722  1.4.4.2  skrll 			break;
   1723  1.4.4.2  skrll 		DELAY(10);
   1724  1.4.4.2  skrll 	}
   1725  1.4.4.2  skrll 	if (ntries == 5) {
   1726  1.4.4.2  skrll 		aprint_error("%s: timeout waiting for master\n",
   1727  1.4.4.2  skrll 		    sc->sc_dev.dv_xname);
   1728  1.4.4.2  skrll 		return EIO;
   1729  1.4.4.2  skrll 	}
   1730  1.4.4.2  skrll 
   1731  1.4.4.2  skrll 	MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
   1732  1.4.4.2  skrll 	DELAY(5000);
   1733  1.4.4.2  skrll 	CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) &
   1734  1.4.4.2  skrll 	    ~IWI_RST_PRINCETON_RESET);
   1735  1.4.4.2  skrll 	DELAY(5000);
   1736  1.4.4.2  skrll 	MEM_WRITE_4(sc, 0x3000e0, 0);
   1737  1.4.4.2  skrll 	DELAY(1000);
   1738  1.4.4.2  skrll 	MEM_WRITE_4(sc, 0x300004, 1);
   1739  1.4.4.2  skrll 	DELAY(1000);
   1740  1.4.4.2  skrll 	MEM_WRITE_4(sc, 0x300004, 0);
   1741  1.4.4.2  skrll 	DELAY(1000);
   1742  1.4.4.2  skrll 	MEM_WRITE_1(sc, 0x200000, 0x00);
   1743  1.4.4.2  skrll 	MEM_WRITE_1(sc, 0x200000, 0x40);
   1744  1.4.4.4  skrll 	DELAY(1000);
   1745  1.4.4.2  skrll 
   1746  1.4.4.2  skrll 	/* Adapter is buggy, we must set the address for each word */
   1747  1.4.4.2  skrll 	for (w = uc; size > 0; w++, size -= 2)
   1748  1.4.4.4  skrll 		MEM_WRITE_2(sc, 0x200010, htole16(*w));
   1749  1.4.4.2  skrll 
   1750  1.4.4.2  skrll 	MEM_WRITE_1(sc, 0x200000, 0x00);
   1751  1.4.4.2  skrll 	MEM_WRITE_1(sc, 0x200000, 0x80);
   1752  1.4.4.2  skrll 
   1753  1.4.4.2  skrll 	/* Wait until we get a response in the uc queue */
   1754  1.4.4.2  skrll 	for (ntries = 0; ntries < 100; ntries++) {
   1755  1.4.4.2  skrll 		if (MEM_READ_1(sc, 0x200000) & 1)
   1756  1.4.4.2  skrll 			break;
   1757  1.4.4.2  skrll 		DELAY(100);
   1758  1.4.4.2  skrll 	}
   1759  1.4.4.2  skrll 	if (ntries == 100) {
   1760  1.4.4.2  skrll 		aprint_error("%s: timeout waiting for ucode to initialize\n",
   1761  1.4.4.2  skrll 		    sc->sc_dev.dv_xname);
   1762  1.4.4.2  skrll 		return EIO;
   1763  1.4.4.2  skrll 	}
   1764  1.4.4.2  skrll 
   1765  1.4.4.2  skrll 	/* Empty the uc queue or the firmware will not initialize properly */
   1766  1.4.4.2  skrll 	for (i = 0; i < 7; i++)
   1767  1.4.4.2  skrll 		MEM_READ_4(sc, 0x200004);
   1768  1.4.4.2  skrll 
   1769  1.4.4.2  skrll 	MEM_WRITE_1(sc, 0x200000, 0x00);
   1770  1.4.4.2  skrll 
   1771  1.4.4.2  skrll 	return 0;
   1772  1.4.4.2  skrll }
   1773  1.4.4.2  skrll 
   1774  1.4.4.2  skrll /* macro to handle unaligned little endian data in firmware image */
   1775  1.4.4.2  skrll #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
   1776  1.4.4.2  skrll static int
   1777  1.4.4.2  skrll iwi_load_firmware(struct iwi_softc *sc, void *fw, int size)
   1778  1.4.4.2  skrll {
   1779  1.4.4.2  skrll 	bus_dmamap_t map;
   1780  1.4.4.2  skrll 	u_char *p, *end;
   1781  1.4.4.4  skrll 	uint32_t sentinel, ctl, sum;
   1782  1.4.4.4  skrll 	uint32_t cs, sl, cd, cl;
   1783  1.4.4.2  skrll 	int ntries, nsegs, error;
   1784  1.4.4.4  skrll 	int sn;
   1785  1.4.4.4  skrll 
   1786  1.4.4.4  skrll 	nsegs = (size + PAGE_SIZE - 1) / PAGE_SIZE;
   1787  1.4.4.2  skrll 
   1788  1.4.4.4  skrll 	/* Create a DMA map for the firmware image */
   1789  1.4.4.4  skrll 	error = bus_dmamap_create(sc->sc_dmat, size, nsegs, size, 0,
   1790  1.4.4.2  skrll 	    BUS_DMA_NOWAIT, &map);
   1791  1.4.4.2  skrll 	if (error != 0) {
   1792  1.4.4.2  skrll 		aprint_error("%s: could not create firmware DMA map\n",
   1793  1.4.4.2  skrll 		    sc->sc_dev.dv_xname);
   1794  1.4.4.2  skrll 		goto fail1;
   1795  1.4.4.2  skrll 	}
   1796  1.4.4.2  skrll 
   1797  1.4.4.4  skrll 	error = bus_dmamap_load(sc->sc_dmat, map, fw, size, NULL,
   1798  1.4.4.4  skrll 	    BUS_DMA_NOWAIT | BUS_DMA_WRITE);
   1799  1.4.4.2  skrll 	if (error != 0) {
   1800  1.4.4.4  skrll 		aprint_error("%s: could not load fw dma map(%d)\n",
   1801  1.4.4.4  skrll 		    sc->sc_dev.dv_xname, error);
   1802  1.4.4.2  skrll 		goto fail2;
   1803  1.4.4.2  skrll 	}
   1804  1.4.4.2  skrll 
   1805  1.4.4.2  skrll 	/* Make sure the adapter will get up-to-date values */
   1806  1.4.4.2  skrll 	bus_dmamap_sync(sc->sc_dmat, map, 0, size, BUS_DMASYNC_PREWRITE);
   1807  1.4.4.2  skrll 
   1808  1.4.4.2  skrll 	/* Tell the adapter where the command blocks are stored */
   1809  1.4.4.2  skrll 	MEM_WRITE_4(sc, 0x3000a0, 0x27000);
   1810  1.4.4.2  skrll 
   1811  1.4.4.2  skrll 	/*
   1812  1.4.4.2  skrll 	 * Store command blocks into adapter's internal memory using register
   1813  1.4.4.2  skrll 	 * indirections. The adapter will read the firmware image through DMA
   1814  1.4.4.2  skrll 	 * using information stored in command blocks.
   1815  1.4.4.2  skrll 	 */
   1816  1.4.4.4  skrll 	p = fw;
   1817  1.4.4.2  skrll 	end = p + size;
   1818  1.4.4.2  skrll 	CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0x27000);
   1819  1.4.4.2  skrll 
   1820  1.4.4.4  skrll 	sn = 0;
   1821  1.4.4.4  skrll 	sl = cl = 0;
   1822  1.4.4.4  skrll 	cs = cd = 0;
   1823  1.4.4.2  skrll 	while (p < end) {
   1824  1.4.4.4  skrll 		if (sl == 0) {
   1825  1.4.4.4  skrll 			cs = map->dm_segs[sn].ds_addr;
   1826  1.4.4.4  skrll 			sl = map->dm_segs[sn].ds_len;
   1827  1.4.4.4  skrll 			sn++;
   1828  1.4.4.4  skrll 		}
   1829  1.4.4.4  skrll 		if (cl == 0) {
   1830  1.4.4.4  skrll 			cd = GETLE32(p); p += 4; cs += 4; sl -= 4;
   1831  1.4.4.4  skrll 			cl = GETLE32(p); p += 4; cs += 4; sl -= 4;
   1832  1.4.4.4  skrll 		}
   1833  1.4.4.4  skrll 		while (sl > 0 && cl > 0) {
   1834  1.4.4.4  skrll 			int len = min(cl, sl);
   1835  1.4.4.4  skrll 
   1836  1.4.4.4  skrll 			sl -= len;
   1837  1.4.4.4  skrll 			cl -= len;
   1838  1.4.4.4  skrll 			p += len;
   1839  1.4.4.4  skrll 
   1840  1.4.4.4  skrll 			while (len > 0) {
   1841  1.4.4.4  skrll 				int mlen = min(len, IWI_CB_MAXDATALEN);
   1842  1.4.4.4  skrll 
   1843  1.4.4.4  skrll 				ctl = IWI_CB_DEFAULT_CTL | mlen;
   1844  1.4.4.4  skrll 				sum = ctl ^ cs ^ cd;
   1845  1.4.4.4  skrll 
   1846  1.4.4.4  skrll 				/* Write a command block */
   1847  1.4.4.4  skrll 				CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, ctl);
   1848  1.4.4.4  skrll 				CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, cs);
   1849  1.4.4.4  skrll 				CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, cd);
   1850  1.4.4.4  skrll 				CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, sum);
   1851  1.4.4.4  skrll 
   1852  1.4.4.4  skrll 				cs += mlen;
   1853  1.4.4.4  skrll 				cd += mlen;
   1854  1.4.4.4  skrll 				len -= mlen;
   1855  1.4.4.4  skrll 			}
   1856  1.4.4.2  skrll 		}
   1857  1.4.4.2  skrll 	}
   1858  1.4.4.2  skrll 
   1859  1.4.4.2  skrll 	/* Write a fictive final command block (sentinel) */
   1860  1.4.4.2  skrll 	sentinel = CSR_READ_4(sc, IWI_CSR_AUTOINC_ADDR);
   1861  1.4.4.2  skrll 	CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
   1862  1.4.4.2  skrll 
   1863  1.4.4.2  skrll 	CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) &
   1864  1.4.4.2  skrll 	    ~(IWI_RST_MASTER_DISABLED | IWI_RST_STOP_MASTER));
   1865  1.4.4.2  skrll 
   1866  1.4.4.2  skrll 	/* Tell the adapter to start processing command blocks */
   1867  1.4.4.2  skrll 	MEM_WRITE_4(sc, 0x3000a4, 0x540100);
   1868  1.4.4.2  skrll 
   1869  1.4.4.2  skrll 	/* Wait until the adapter has processed all command blocks */
   1870  1.4.4.2  skrll 	for (ntries = 0; ntries < 400; ntries++) {
   1871  1.4.4.2  skrll 		if (MEM_READ_4(sc, 0x3000d0) >= sentinel)
   1872  1.4.4.2  skrll 			break;
   1873  1.4.4.2  skrll 		DELAY(100);
   1874  1.4.4.2  skrll 	}
   1875  1.4.4.2  skrll 	if (ntries == 400) {
   1876  1.4.4.2  skrll 		aprint_error("%s: timeout processing cb\n",
   1877  1.4.4.2  skrll 		    sc->sc_dev.dv_xname);
   1878  1.4.4.2  skrll 		error = EIO;
   1879  1.4.4.4  skrll 		goto fail2;
   1880  1.4.4.2  skrll 	}
   1881  1.4.4.2  skrll 
   1882  1.4.4.2  skrll 	/* We're done with command blocks processing */
   1883  1.4.4.2  skrll 	MEM_WRITE_4(sc, 0x3000a4, 0x540c00);
   1884  1.4.4.2  skrll 
   1885  1.4.4.2  skrll 	/* Allow interrupts so we know when the firmware is inited */
   1886  1.4.4.2  skrll 	CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
   1887  1.4.4.2  skrll 
   1888  1.4.4.2  skrll 	/* Tell the adapter to initialize the firmware */
   1889  1.4.4.2  skrll 	CSR_WRITE_4(sc, IWI_CSR_RST, 0);
   1890  1.4.4.2  skrll 	CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
   1891  1.4.4.2  skrll 	    IWI_CTL_ALLOW_STANDBY);
   1892  1.4.4.2  skrll 
   1893  1.4.4.2  skrll 	/* Wait at most one second for firmware initialization to complete */
   1894  1.4.4.2  skrll 	if ((error = tsleep(sc, 0, "iwiinit", hz)) != 0) {
   1895  1.4.4.2  skrll 		aprint_error("%s: timeout waiting for firmware initialization "
   1896  1.4.4.2  skrll 		    "to complete\n", sc->sc_dev.dv_xname);
   1897  1.4.4.4  skrll 		goto fail3;
   1898  1.4.4.2  skrll 	}
   1899  1.4.4.2  skrll 
   1900  1.4.4.4  skrll fail3:
   1901  1.4.4.4  skrll 	bus_dmamap_sync(sc->sc_dmat, map, 0, size, BUS_DMASYNC_POSTWRITE);
   1902  1.4.4.2  skrll 	bus_dmamap_unload(sc->sc_dmat, map);
   1903  1.4.4.4  skrll fail2:
   1904  1.4.4.4  skrll 	bus_dmamap_destroy(sc->sc_dmat, map);
   1905  1.4.4.3  skrll 
   1906  1.4.4.4  skrll fail1:
   1907  1.4.4.4  skrll 	return error;
   1908  1.4.4.2  skrll }
   1909  1.4.4.2  skrll 
   1910  1.4.4.2  skrll /*
   1911  1.4.4.2  skrll  * Store firmware into kernel memory so we can download it when we need to,
   1912  1.4.4.2  skrll  * e.g when the adapter wakes up from suspend mode.
   1913  1.4.4.2  skrll  */
   1914  1.4.4.2  skrll static int
   1915  1.4.4.2  skrll iwi_cache_firmware(struct iwi_softc *sc, void *data)
   1916  1.4.4.2  skrll {
   1917  1.4.4.2  skrll 	struct iwi_firmware *kfw = &sc->fw;
   1918  1.4.4.2  skrll 	struct iwi_firmware ufw;
   1919  1.4.4.2  skrll 	int error;
   1920  1.4.4.2  skrll 
   1921  1.4.4.2  skrll 	iwi_free_firmware(sc);
   1922  1.4.4.2  skrll 
   1923  1.4.4.2  skrll 	if ((error = copyin(data, &ufw, sizeof ufw)) != 0)
   1924  1.4.4.2  skrll 		goto fail1;
   1925  1.4.4.2  skrll 
   1926  1.4.4.2  skrll 	kfw->boot_size  = ufw.boot_size;
   1927  1.4.4.2  skrll 	kfw->ucode_size = ufw.ucode_size;
   1928  1.4.4.2  skrll 	kfw->main_size  = ufw.main_size;
   1929  1.4.4.2  skrll 
   1930  1.4.4.2  skrll 	kfw->boot = malloc(kfw->boot_size, M_DEVBUF, M_NOWAIT);
   1931  1.4.4.2  skrll 	if (kfw->boot == NULL) {
   1932  1.4.4.2  skrll 		error = ENOMEM;
   1933  1.4.4.2  skrll 		goto fail1;
   1934  1.4.4.2  skrll 	}
   1935  1.4.4.2  skrll 
   1936  1.4.4.2  skrll 	kfw->ucode = malloc(kfw->ucode_size, M_DEVBUF, M_NOWAIT);
   1937  1.4.4.2  skrll 	if (kfw->ucode == NULL) {
   1938  1.4.4.2  skrll 		error = ENOMEM;
   1939  1.4.4.2  skrll 		goto fail2;
   1940  1.4.4.2  skrll 	}
   1941  1.4.4.2  skrll 
   1942  1.4.4.2  skrll 	kfw->main = malloc(kfw->main_size, M_DEVBUF, M_NOWAIT);
   1943  1.4.4.2  skrll 	if (kfw->main == NULL) {
   1944  1.4.4.2  skrll 		error = ENOMEM;
   1945  1.4.4.2  skrll 		goto fail3;
   1946  1.4.4.2  skrll 	}
   1947  1.4.4.2  skrll 
   1948  1.4.4.2  skrll 	if ((error = copyin(ufw.boot, kfw->boot, kfw->boot_size)) != 0)
   1949  1.4.4.2  skrll 		goto fail4;
   1950  1.4.4.2  skrll 
   1951  1.4.4.2  skrll 	if ((error = copyin(ufw.ucode, kfw->ucode, kfw->ucode_size)) != 0)
   1952  1.4.4.2  skrll 		goto fail4;
   1953  1.4.4.2  skrll 
   1954  1.4.4.2  skrll 	if ((error = copyin(ufw.main, kfw->main, kfw->main_size)) != 0)
   1955  1.4.4.2  skrll 		goto fail4;
   1956  1.4.4.2  skrll 
   1957  1.4.4.2  skrll 	DPRINTF(("Firmware cached: boot %u, ucode %u, main %u\n",
   1958  1.4.4.2  skrll 	    kfw->boot_size, kfw->ucode_size, kfw->main_size));
   1959  1.4.4.2  skrll 
   1960  1.4.4.2  skrll 	sc->flags |= IWI_FLAG_FW_CACHED;
   1961  1.4.4.2  skrll 
   1962  1.4.4.2  skrll 	return 0;
   1963  1.4.4.2  skrll 
   1964  1.4.4.2  skrll fail4:	free(kfw->boot, M_DEVBUF);
   1965  1.4.4.2  skrll fail3:	free(kfw->ucode, M_DEVBUF);
   1966  1.4.4.2  skrll fail2:	free(kfw->main, M_DEVBUF);
   1967  1.4.4.2  skrll fail1:
   1968  1.4.4.2  skrll 	return error;
   1969  1.4.4.2  skrll }
   1970  1.4.4.2  skrll 
   1971  1.4.4.2  skrll static void
   1972  1.4.4.2  skrll iwi_free_firmware(struct iwi_softc *sc)
   1973  1.4.4.2  skrll {
   1974  1.4.4.2  skrll 	if (!(sc->flags & IWI_FLAG_FW_CACHED))
   1975  1.4.4.2  skrll 		return;
   1976  1.4.4.3  skrll 
   1977  1.4.4.2  skrll 	free(sc->fw.boot, M_DEVBUF);
   1978  1.4.4.2  skrll 	free(sc->fw.ucode, M_DEVBUF);
   1979  1.4.4.2  skrll 	free(sc->fw.main, M_DEVBUF);
   1980  1.4.4.2  skrll 
   1981  1.4.4.2  skrll 	sc->flags &= ~IWI_FLAG_FW_CACHED;
   1982  1.4.4.2  skrll }
   1983  1.4.4.2  skrll 
   1984  1.4.4.2  skrll static int
   1985  1.4.4.2  skrll iwi_config(struct iwi_softc *sc)
   1986  1.4.4.2  skrll {
   1987  1.4.4.2  skrll 	struct ieee80211com *ic = &sc->sc_ic;
   1988  1.4.4.4  skrll 	struct ifnet *ifp = &sc->sc_if;
   1989  1.4.4.2  skrll 	struct iwi_configuration config;
   1990  1.4.4.2  skrll 	struct iwi_rateset rs;
   1991  1.4.4.2  skrll 	struct iwi_txpower power;
   1992  1.4.4.4  skrll 	struct ieee80211_key *wk;
   1993  1.4.4.2  skrll 	struct iwi_wep_key wepkey;
   1994  1.4.4.4  skrll 	uint32_t data;
   1995  1.4.4.2  skrll 	int error, i;
   1996  1.4.4.2  skrll 
   1997  1.4.4.2  skrll 	IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
   1998  1.4.4.2  skrll 	DPRINTF(("Setting MAC address to %s\n", ether_sprintf(ic->ic_myaddr)));
   1999  1.4.4.2  skrll 	error = iwi_cmd(sc, IWI_CMD_SET_MAC_ADDRESS, ic->ic_myaddr,
   2000  1.4.4.2  skrll 	    IEEE80211_ADDR_LEN, 0);
   2001  1.4.4.2  skrll 	if (error != 0)
   2002  1.4.4.2  skrll 		return error;
   2003  1.4.4.2  skrll 
   2004  1.4.4.2  skrll 	memset(&config, 0, sizeof config);
   2005  1.4.4.4  skrll 	config.bluetooth_coexistence = sc->bluetooth;
   2006  1.4.4.4  skrll 	config.antenna = sc->antenna;
   2007  1.4.4.2  skrll 	config.multicast_enabled = 1;
   2008  1.4.4.4  skrll 	config.answer_pbreq = (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0;
   2009  1.4.4.4  skrll 	config.disable_unicast_decryption = 1;
   2010  1.4.4.4  skrll 	config.disable_multicast_decryption = 1;
   2011  1.4.4.2  skrll 	DPRINTF(("Configuring adapter\n"));
   2012  1.4.4.2  skrll 	error = iwi_cmd(sc, IWI_CMD_SET_CONFIGURATION, &config, sizeof config,
   2013  1.4.4.2  skrll 	    0);
   2014  1.4.4.2  skrll 	if (error != 0)
   2015  1.4.4.2  skrll 		return error;
   2016  1.4.4.2  skrll 
   2017  1.4.4.2  skrll 	data = htole32(IWI_POWER_MODE_CAM);
   2018  1.4.4.2  skrll 	DPRINTF(("Setting power mode to %u\n", le32toh(data)));
   2019  1.4.4.2  skrll 	error = iwi_cmd(sc, IWI_CMD_SET_POWER_MODE, &data, sizeof data, 0);
   2020  1.4.4.2  skrll 	if (error != 0)
   2021  1.4.4.2  skrll 		return error;
   2022  1.4.4.2  skrll 
   2023  1.4.4.2  skrll 	data = htole32(ic->ic_rtsthreshold);
   2024  1.4.4.2  skrll 	DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
   2025  1.4.4.2  skrll 	error = iwi_cmd(sc, IWI_CMD_SET_RTS_THRESHOLD, &data, sizeof data, 0);
   2026  1.4.4.2  skrll 	if (error != 0)
   2027  1.4.4.2  skrll 		return error;
   2028  1.4.4.2  skrll 
   2029  1.4.4.4  skrll 	data = htole32(ic->ic_fragthreshold);
   2030  1.4.4.4  skrll 	DPRINTF(("Setting fragmentation threshold to %u\n", le32toh(data)));
   2031  1.4.4.4  skrll 	error = iwi_cmd(sc, IWI_CMD_SET_FRAG_THRESHOLD, &data, sizeof data, 0);
   2032  1.4.4.4  skrll 	if (error != 0)
   2033  1.4.4.4  skrll 		return error;
   2034  1.4.4.4  skrll 
   2035  1.4.4.2  skrll 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
   2036  1.4.4.2  skrll 		power.mode = IWI_MODE_11B;
   2037  1.4.4.2  skrll 		power.nchan = 11;
   2038  1.4.4.2  skrll 		for (i = 0; i < 11; i++) {
   2039  1.4.4.2  skrll 			power.chan[i].chan = i + 1;
   2040  1.4.4.2  skrll 			power.chan[i].power = IWI_TXPOWER_MAX;
   2041  1.4.4.2  skrll 		}
   2042  1.4.4.2  skrll 		DPRINTF(("Setting .11b channels tx power\n"));
   2043  1.4.4.2  skrll 		error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power,
   2044  1.4.4.2  skrll 		    0);
   2045  1.4.4.2  skrll 		if (error != 0)
   2046  1.4.4.2  skrll 			return error;
   2047  1.4.4.2  skrll 
   2048  1.4.4.2  skrll 		power.mode = IWI_MODE_11G;
   2049  1.4.4.2  skrll 		DPRINTF(("Setting .11g channels tx power\n"));
   2050  1.4.4.2  skrll 		error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power,
   2051  1.4.4.2  skrll 		    0);
   2052  1.4.4.2  skrll 		if (error != 0)
   2053  1.4.4.2  skrll 			return error;
   2054  1.4.4.2  skrll 	}
   2055  1.4.4.2  skrll 
   2056  1.4.4.2  skrll 	rs.mode = IWI_MODE_11G;
   2057  1.4.4.2  skrll 	rs.type = IWI_RATESET_TYPE_SUPPORTED;
   2058  1.4.4.2  skrll 	rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates;
   2059  1.4.4.2  skrll 	memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11G].rs_rates,
   2060  1.4.4.2  skrll 	    rs.nrates);
   2061  1.4.4.2  skrll 	DPRINTF(("Setting .11bg supported rates (%u)\n", rs.nrates));
   2062  1.4.4.2  skrll 	error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
   2063  1.4.4.2  skrll 	if (error != 0)
   2064  1.4.4.2  skrll 		return error;
   2065  1.4.4.2  skrll 
   2066  1.4.4.2  skrll 	rs.mode = IWI_MODE_11A;
   2067  1.4.4.2  skrll 	rs.type = IWI_RATESET_TYPE_SUPPORTED;
   2068  1.4.4.2  skrll 	rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates;
   2069  1.4.4.2  skrll 	memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11A].rs_rates,
   2070  1.4.4.2  skrll 	    rs.nrates);
   2071  1.4.4.2  skrll 	DPRINTF(("Setting .11a supported rates (%u)\n", rs.nrates));
   2072  1.4.4.2  skrll 	error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
   2073  1.4.4.2  skrll 	if (error != 0)
   2074  1.4.4.2  skrll 		return error;
   2075  1.4.4.2  skrll 
   2076  1.4.4.2  skrll 	data = htole32(arc4random());
   2077  1.4.4.2  skrll 	DPRINTF(("Setting initialization vector to %u\n", le32toh(data)));
   2078  1.4.4.2  skrll 	error = iwi_cmd(sc, IWI_CMD_SET_IV, &data, sizeof data, 0);
   2079  1.4.4.2  skrll 	if (error != 0)
   2080  1.4.4.2  skrll 		return error;
   2081  1.4.4.2  skrll 
   2082  1.4.4.4  skrll 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   2083  1.4.4.4  skrll 		wk = &ic->ic_crypto.cs_nw_keys[i];
   2084  1.4.4.4  skrll 
   2085  1.4.4.4  skrll 		wepkey.cmd = IWI_WEP_KEY_CMD_SETKEY;
   2086  1.4.4.4  skrll 		wepkey.idx = i;
   2087  1.4.4.4  skrll 		wepkey.len = wk->wk_keylen;
   2088  1.4.4.4  skrll 		memset(wepkey.key, 0, sizeof wepkey.key);
   2089  1.4.4.4  skrll 		memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
   2090  1.4.4.4  skrll 		DPRINTF(("Setting wep key index %u len %u\n",
   2091  1.4.4.4  skrll 		    wepkey.idx, wepkey.len));
   2092  1.4.4.4  skrll 		error = iwi_cmd(sc, IWI_CMD_SET_WEP_KEY, &wepkey,
   2093  1.4.4.4  skrll 		    sizeof wepkey, 0);
   2094  1.4.4.4  skrll 		if (error != 0)
   2095  1.4.4.4  skrll 			return error;
   2096  1.4.4.2  skrll 	}
   2097  1.4.4.2  skrll 
   2098  1.4.4.2  skrll 	/* Enable adapter */
   2099  1.4.4.2  skrll 	DPRINTF(("Enabling adapter\n"));
   2100  1.4.4.2  skrll 	return iwi_cmd(sc, IWI_CMD_ENABLE, NULL, 0, 0);
   2101  1.4.4.2  skrll }
   2102  1.4.4.2  skrll 
   2103  1.4.4.2  skrll static int
   2104  1.4.4.4  skrll iwi_set_chan(struct iwi_softc *sc, struct ieee80211_channel *chan)
   2105  1.4.4.4  skrll {
   2106  1.4.4.4  skrll 	struct ieee80211com *ic = &sc->sc_ic;
   2107  1.4.4.4  skrll 	struct iwi_scan_v2 scan;
   2108  1.4.4.4  skrll 
   2109  1.4.4.4  skrll 	(void)memset(&scan, 0, sizeof scan);
   2110  1.4.4.4  skrll 
   2111  1.4.4.4  skrll 	scan.dwelltime[IWI_SCAN_TYPE_PASSIVE] = htole16(2000);
   2112  1.4.4.4  skrll 	scan.channels[0] = 1 |
   2113  1.4.4.4  skrll 	    (IEEE80211_IS_CHAN_5GHZ(chan) ? IWI_CHAN_5GHZ : IWI_CHAN_2GHZ);
   2114  1.4.4.4  skrll 	scan.channels[1] = ieee80211_chan2ieee(ic, chan);
   2115  1.4.4.4  skrll 	iwi_scan_type_set(scan, 1, IWI_SCAN_TYPE_PASSIVE);
   2116  1.4.4.4  skrll 
   2117  1.4.4.4  skrll 	DPRINTF(("Setting channel to %u\n", ieee80211_chan2ieee(ic, chan)));
   2118  1.4.4.4  skrll 	return iwi_cmd(sc, IWI_CMD_SCAN_V2, &scan, sizeof scan, 1);
   2119  1.4.4.4  skrll }
   2120  1.4.4.4  skrll 
   2121  1.4.4.4  skrll static int
   2122  1.4.4.2  skrll iwi_scan(struct iwi_softc *sc)
   2123  1.4.4.2  skrll {
   2124  1.4.4.2  skrll 	struct ieee80211com *ic = &sc->sc_ic;
   2125  1.4.4.4  skrll 	struct iwi_scan_v2 scan;
   2126  1.4.4.4  skrll 	uint32_t type;
   2127  1.4.4.4  skrll 	uint8_t *p;
   2128  1.4.4.4  skrll 	int i, count, idx;
   2129  1.4.4.4  skrll 
   2130  1.4.4.4  skrll 	(void)memset(&scan, 0, sizeof scan);
   2131  1.4.4.4  skrll 	scan.dwelltime[IWI_SCAN_TYPE_ACTIVE_BROADCAST] =
   2132  1.4.4.4  skrll 	    htole16(sc->dwelltime);
   2133  1.4.4.4  skrll 	scan.dwelltime[IWI_SCAN_TYPE_ACTIVE_BDIRECT] =
   2134  1.4.4.4  skrll 	    htole16(sc->dwelltime);
   2135  1.4.4.4  skrll 
   2136  1.4.4.4  skrll 	/* tell the firmware about the desired essid */
   2137  1.4.4.4  skrll 	if (ic->ic_des_esslen) {
   2138  1.4.4.4  skrll 		int error;
   2139  1.4.4.4  skrll 
   2140  1.4.4.4  skrll 		DPRINTF(("%s: Setting adapter desired ESSID to %s\n",
   2141  1.4.4.4  skrll 		    __func__, ic->ic_des_essid));
   2142  1.4.4.4  skrll 
   2143  1.4.4.4  skrll 		error = iwi_cmd(sc, IWI_CMD_SET_ESSID,
   2144  1.4.4.4  skrll 		    ic->ic_des_essid, ic->ic_des_esslen, 1);
   2145  1.4.4.4  skrll 		if (error)
   2146  1.4.4.4  skrll 			return error;
   2147  1.4.4.2  skrll 
   2148  1.4.4.4  skrll 		type = IWI_SCAN_TYPE_ACTIVE_BDIRECT;
   2149  1.4.4.4  skrll 	} else {
   2150  1.4.4.4  skrll 		type = IWI_SCAN_TYPE_ACTIVE_BROADCAST;
   2151  1.4.4.4  skrll 	}
   2152  1.4.4.4  skrll 
   2153  1.4.4.4  skrll 	p = &scan.channels[0];
   2154  1.4.4.4  skrll 	count = idx = 0;
   2155  1.4.4.2  skrll 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
   2156  1.4.4.2  skrll 		if (IEEE80211_IS_CHAN_5GHZ(&ic->ic_channels[i]) &&
   2157  1.4.4.2  skrll 		    isset(ic->ic_chan_active, i)) {
   2158  1.4.4.2  skrll 			*++p = i;
   2159  1.4.4.2  skrll 			count++;
   2160  1.4.4.4  skrll 			idx++;
   2161  1.4.4.4  skrll  			iwi_scan_type_set(scan, idx, type);
   2162  1.4.4.2  skrll 		}
   2163  1.4.4.2  skrll 	}
   2164  1.4.4.4  skrll 	if (count) {
   2165  1.4.4.4  skrll 		*(p - count) = IWI_CHAN_5GHZ | count;
   2166  1.4.4.4  skrll 		p++;
   2167  1.4.4.4  skrll 	}
   2168  1.4.4.2  skrll 
   2169  1.4.4.2  skrll 	count = 0;
   2170  1.4.4.2  skrll 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
   2171  1.4.4.2  skrll 		if (IEEE80211_IS_CHAN_2GHZ(&ic->ic_channels[i]) &&
   2172  1.4.4.2  skrll 		    isset(ic->ic_chan_active, i)) {
   2173  1.4.4.2  skrll 			*++p = i;
   2174  1.4.4.2  skrll 			count++;
   2175  1.4.4.4  skrll 			idx++;
   2176  1.4.4.4  skrll 			iwi_scan_type_set(scan, idx, type);
   2177  1.4.4.2  skrll 		}
   2178  1.4.4.2  skrll 	}
   2179  1.4.4.2  skrll 	*(p - count) = IWI_CHAN_2GHZ | count;
   2180  1.4.4.2  skrll 
   2181  1.4.4.2  skrll 	DPRINTF(("Start scanning\n"));
   2182  1.4.4.4  skrll 	return iwi_cmd(sc, IWI_CMD_SCAN_V2, &scan, sizeof scan, 1);
   2183  1.4.4.2  skrll }
   2184  1.4.4.2  skrll 
   2185  1.4.4.2  skrll static int
   2186  1.4.4.2  skrll iwi_auth_and_assoc(struct iwi_softc *sc)
   2187  1.4.4.2  skrll {
   2188  1.4.4.2  skrll 	struct ieee80211com *ic = &sc->sc_ic;
   2189  1.4.4.2  skrll 	struct ieee80211_node *ni = ic->ic_bss;
   2190  1.4.4.4  skrll 	struct ifnet *ifp = &sc->sc_if;
   2191  1.4.4.2  skrll 	struct iwi_configuration config;
   2192  1.4.4.2  skrll 	struct iwi_associate assoc;
   2193  1.4.4.2  skrll 	struct iwi_rateset rs;
   2194  1.4.4.4  skrll 	uint16_t capinfo;
   2195  1.4.4.4  skrll 	uint32_t data;
   2196  1.4.4.2  skrll 	int error;
   2197  1.4.4.2  skrll 
   2198  1.4.4.2  skrll 	if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
   2199  1.4.4.2  skrll 		memset(&config, 0, sizeof config);
   2200  1.4.4.4  skrll 		config.bluetooth_coexistence = sc->bluetooth;
   2201  1.4.4.4  skrll 		config.antenna = sc->antenna;
   2202  1.4.4.2  skrll 		config.multicast_enabled = 1;
   2203  1.4.4.4  skrll 		config.use_protection = 1;
   2204  1.4.4.4  skrll 		config.answer_pbreq =
   2205  1.4.4.4  skrll 		    (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0;
   2206  1.4.4.4  skrll 		config.disable_unicast_decryption = 1;
   2207  1.4.4.4  skrll 		config.disable_multicast_decryption = 1;
   2208  1.4.4.2  skrll 		DPRINTF(("Configuring adapter\n"));
   2209  1.4.4.2  skrll 		error = iwi_cmd(sc, IWI_CMD_SET_CONFIGURATION, &config,
   2210  1.4.4.2  skrll 		    sizeof config, 1);
   2211  1.4.4.2  skrll 		if (error != 0)
   2212  1.4.4.2  skrll 			return error;
   2213  1.4.4.2  skrll 	}
   2214  1.4.4.2  skrll 
   2215  1.4.4.2  skrll #ifdef IWI_DEBUG
   2216  1.4.4.2  skrll 	if (iwi_debug > 0) {
   2217  1.4.4.2  skrll 		printf("Setting ESSID to ");
   2218  1.4.4.2  skrll 		ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
   2219  1.4.4.2  skrll 		printf("\n");
   2220  1.4.4.2  skrll 	}
   2221  1.4.4.2  skrll #endif
   2222  1.4.4.2  skrll 	error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ni->ni_essid, ni->ni_esslen, 1);
   2223  1.4.4.2  skrll 	if (error != 0)
   2224  1.4.4.2  skrll 		return error;
   2225  1.4.4.2  skrll 
   2226  1.4.4.4  skrll 	/* the rate set has already been "negotiated" */
   2227  1.4.4.2  skrll 	rs.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A :
   2228  1.4.4.2  skrll 	    IWI_MODE_11G;
   2229  1.4.4.4  skrll 	rs.type = IWI_RATESET_TYPE_NEGOTIATED;
   2230  1.4.4.2  skrll 	rs.nrates = ni->ni_rates.rs_nrates;
   2231  1.4.4.2  skrll 	memcpy(rs.rates, ni->ni_rates.rs_rates, rs.nrates);
   2232  1.4.4.4  skrll 	DPRINTF(("Setting negotiated rates (%u)\n", rs.nrates));
   2233  1.4.4.2  skrll 	error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 1);
   2234  1.4.4.2  skrll 	if (error != 0)
   2235  1.4.4.2  skrll 		return error;
   2236  1.4.4.2  skrll 
   2237  1.4.4.4  skrll 	if (ic->ic_opt_ie != NULL) {
   2238  1.4.4.4  skrll 		DPRINTF(("Setting optional IE (len=%u)\n", ic->ic_opt_ie_len));
   2239  1.4.4.4  skrll 		error = iwi_cmd(sc, IWI_CMD_SET_OPTIE, ic->ic_opt_ie,
   2240  1.4.4.4  skrll 		    ic->ic_opt_ie_len, 1);
   2241  1.4.4.4  skrll 		if (error != 0)
   2242  1.4.4.4  skrll 			return error;
   2243  1.4.4.4  skrll 	}
   2244  1.4.4.2  skrll 	data = htole32(ni->ni_rssi);
   2245  1.4.4.2  skrll 	DPRINTF(("Setting sensitivity to %d\n", (int8_t)ni->ni_rssi));
   2246  1.4.4.2  skrll 	error = iwi_cmd(sc, IWI_CMD_SET_SENSITIVITY, &data, sizeof data, 1);
   2247  1.4.4.2  skrll 	if (error != 0)
   2248  1.4.4.2  skrll 		return error;
   2249  1.4.4.2  skrll 
   2250  1.4.4.2  skrll 	memset(&assoc, 0, sizeof assoc);
   2251  1.4.4.2  skrll 	assoc.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A :
   2252  1.4.4.2  skrll 	    IWI_MODE_11G;
   2253  1.4.4.2  skrll 	assoc.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
   2254  1.4.4.4  skrll 	if (ni->ni_authmode == IEEE80211_AUTH_SHARED)
   2255  1.4.4.4  skrll 		assoc.auth = (ic->ic_crypto.cs_def_txkey << 4) | IWI_AUTH_SHARED;
   2256  1.4.4.4  skrll 	if (ic->ic_opt_ie != NULL)
   2257  1.4.4.4  skrll 		assoc.policy |= htole16(IWI_POLICY_OPTIE);
   2258  1.4.4.4  skrll 	memcpy(assoc.tstamp, ni->ni_tstamp.data, 8);
   2259  1.4.4.4  skrll 
   2260  1.4.4.4  skrll 	if (ic->ic_opmode == IEEE80211_M_IBSS)
   2261  1.4.4.4  skrll 		capinfo = IEEE80211_CAPINFO_IBSS;
   2262  1.4.4.4  skrll 	else
   2263  1.4.4.4  skrll 		capinfo = IEEE80211_CAPINFO_ESS;
   2264  1.4.4.4  skrll 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
   2265  1.4.4.4  skrll 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
   2266  1.4.4.4  skrll 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
   2267  1.4.4.4  skrll 	    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
   2268  1.4.4.4  skrll 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
   2269  1.4.4.4  skrll 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
   2270  1.4.4.4  skrll 		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
   2271  1.4.4.4  skrll 	assoc.capinfo = htole16(capinfo);
   2272  1.4.4.4  skrll 
   2273  1.4.4.2  skrll 	assoc.lintval = htole16(ic->ic_lintval);
   2274  1.4.4.2  skrll 	assoc.intval = htole16(ni->ni_intval);
   2275  1.4.4.2  skrll 	IEEE80211_ADDR_COPY(assoc.bssid, ni->ni_bssid);
   2276  1.4.4.4  skrll 	if (ic->ic_opmode == IEEE80211_M_IBSS)
   2277  1.4.4.4  skrll 		IEEE80211_ADDR_COPY(assoc.dst, ifp->if_broadcastaddr);
   2278  1.4.4.4  skrll 	else
   2279  1.4.4.4  skrll 		IEEE80211_ADDR_COPY(assoc.dst, ni->ni_bssid);
   2280  1.4.4.2  skrll 	DPRINTF(("Trying to associate to %s channel %u auth %u\n",
   2281  1.4.4.2  skrll 	    ether_sprintf(assoc.bssid), assoc.chan, assoc.auth));
   2282  1.4.4.2  skrll 	return iwi_cmd(sc, IWI_CMD_ASSOCIATE, &assoc, sizeof assoc, 1);
   2283  1.4.4.2  skrll }
   2284  1.4.4.2  skrll 
   2285  1.4.4.2  skrll static int
   2286  1.4.4.2  skrll iwi_init(struct ifnet *ifp)
   2287  1.4.4.2  skrll {
   2288  1.4.4.2  skrll 	struct iwi_softc *sc = ifp->if_softc;
   2289  1.4.4.2  skrll 	struct ieee80211com *ic = &sc->sc_ic;
   2290  1.4.4.2  skrll 	struct iwi_firmware *fw = &sc->fw;
   2291  1.4.4.2  skrll 	int i, error;
   2292  1.4.4.2  skrll 
   2293  1.4.4.2  skrll 	/* exit immediately if firmware has not been ioctl'd */
   2294  1.4.4.2  skrll 	if (!(sc->flags & IWI_FLAG_FW_CACHED)) {
   2295  1.4.4.4  skrll 		if (!(sc->flags & IWI_FLAG_FW_WARNED))
   2296  1.4.4.4  skrll 			aprint_error("%s: Firmware not loaded\n",
   2297  1.4.4.4  skrll 			    sc->sc_dev.dv_xname);
   2298  1.4.4.4  skrll 		sc->flags |= IWI_FLAG_FW_WARNED;
   2299  1.4.4.2  skrll 		ifp->if_flags &= ~IFF_UP;
   2300  1.4.4.2  skrll 		return EIO;
   2301  1.4.4.2  skrll 	}
   2302  1.4.4.2  skrll 
   2303  1.4.4.4  skrll 	iwi_stop(ifp, 0);
   2304  1.4.4.4  skrll 
   2305  1.4.4.2  skrll 	if ((error = iwi_reset(sc)) != 0) {
   2306  1.4.4.2  skrll 		aprint_error("%s: could not reset adapter\n",
   2307  1.4.4.2  skrll 		    sc->sc_dev.dv_xname);
   2308  1.4.4.2  skrll 		goto fail;
   2309  1.4.4.2  skrll 	}
   2310  1.4.4.2  skrll 
   2311  1.4.4.2  skrll 	if ((error = iwi_load_firmware(sc, fw->boot, fw->boot_size)) != 0) {
   2312  1.4.4.2  skrll 		aprint_error("%s: could not load boot firmware\n",
   2313  1.4.4.2  skrll 		    sc->sc_dev.dv_xname);
   2314  1.4.4.2  skrll 		goto fail;
   2315  1.4.4.2  skrll 	}
   2316  1.4.4.2  skrll 
   2317  1.4.4.2  skrll 	if ((error = iwi_load_ucode(sc, fw->ucode, fw->ucode_size)) != 0) {
   2318  1.4.4.2  skrll 		aprint_error("%s: could not load microcode\n",
   2319  1.4.4.2  skrll 		    sc->sc_dev.dv_xname);
   2320  1.4.4.2  skrll 		goto fail;
   2321  1.4.4.2  skrll 	}
   2322  1.4.4.2  skrll 
   2323  1.4.4.2  skrll 	iwi_stop_master(sc);
   2324  1.4.4.2  skrll 
   2325  1.4.4.4  skrll 	CSR_WRITE_4(sc, IWI_CSR_CMD_BASE, sc->cmdq.desc_map->dm_segs[0].ds_addr);
   2326  1.4.4.4  skrll 	CSR_WRITE_4(sc, IWI_CSR_CMD_SIZE, sc->cmdq.count);
   2327  1.4.4.4  skrll 	CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
   2328  1.4.4.4  skrll 
   2329  1.4.4.4  skrll 	CSR_WRITE_4(sc, IWI_CSR_TX1_BASE, sc->txq.desc_map->dm_segs[0].ds_addr);
   2330  1.4.4.4  skrll 	CSR_WRITE_4(sc, IWI_CSR_TX1_SIZE, sc->txq.count);
   2331  1.4.4.4  skrll 	CSR_WRITE_4(sc, IWI_CSR_TX1_WIDX, sc->txq.cur);
   2332  1.4.4.4  skrll 
   2333  1.4.4.4  skrll 	CSR_WRITE_4(sc, IWI_CSR_TX2_BASE, sc->txq.desc_map->dm_segs[0].ds_addr);
   2334  1.4.4.4  skrll 	CSR_WRITE_4(sc, IWI_CSR_TX2_SIZE, sc->txq.count);
   2335  1.4.4.4  skrll 	CSR_WRITE_4(sc, IWI_CSR_TX2_WIDX, sc->txq.cur);
   2336  1.4.4.4  skrll 
   2337  1.4.4.4  skrll 	CSR_WRITE_4(sc, IWI_CSR_TX3_BASE, sc->txq.desc_map->dm_segs[0].ds_addr);
   2338  1.4.4.4  skrll 	CSR_WRITE_4(sc, IWI_CSR_TX3_SIZE, sc->txq.count);
   2339  1.4.4.4  skrll 	CSR_WRITE_4(sc, IWI_CSR_TX3_WIDX, sc->txq.cur);
   2340  1.4.4.4  skrll 
   2341  1.4.4.4  skrll 	CSR_WRITE_4(sc, IWI_CSR_TX4_BASE, sc->txq.desc_map->dm_segs[0].ds_addr);
   2342  1.4.4.4  skrll 	CSR_WRITE_4(sc, IWI_CSR_TX4_SIZE, sc->txq.count);
   2343  1.4.4.4  skrll 	CSR_WRITE_4(sc, IWI_CSR_TX4_WIDX, sc->txq.cur);
   2344  1.4.4.2  skrll 
   2345  1.4.4.4  skrll 	for (i = 0; i < sc->rxq.count; i++)
   2346  1.4.4.2  skrll 		CSR_WRITE_4(sc, IWI_CSR_RX_BASE + i * 4,
   2347  1.4.4.4  skrll 		    sc->rxq.data[i].map->dm_segs[0].ds_addr);
   2348  1.4.4.2  skrll 
   2349  1.4.4.4  skrll 	CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, sc->rxq.count -1);
   2350  1.4.4.2  skrll 
   2351  1.4.4.2  skrll 	if ((error = iwi_load_firmware(sc, fw->main, fw->main_size)) != 0) {
   2352  1.4.4.2  skrll 		aprint_error("%s: could not load main firmware\n",
   2353  1.4.4.2  skrll 		    sc->sc_dev.dv_xname);
   2354  1.4.4.2  skrll 		goto fail;
   2355  1.4.4.2  skrll 	}
   2356  1.4.4.2  skrll 
   2357  1.4.4.2  skrll 	sc->flags |= IWI_FLAG_FW_INITED;
   2358  1.4.4.2  skrll 
   2359  1.4.4.2  skrll 	if ((error = iwi_config(sc)) != 0) {
   2360  1.4.4.2  skrll 		aprint_error("%s: device configuration failed\n",
   2361  1.4.4.2  skrll 		    sc->sc_dev.dv_xname);
   2362  1.4.4.2  skrll 		goto fail;
   2363  1.4.4.2  skrll 	}
   2364  1.4.4.2  skrll 
   2365  1.4.4.4  skrll 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
   2366  1.4.4.4  skrll 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
   2367  1.4.4.4  skrll 	else
   2368  1.4.4.4  skrll 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
   2369  1.4.4.2  skrll 
   2370  1.4.4.2  skrll 	ifp->if_flags &= ~IFF_OACTIVE;
   2371  1.4.4.2  skrll 	ifp->if_flags |= IFF_RUNNING;
   2372  1.4.4.2  skrll 
   2373  1.4.4.2  skrll 	return 0;
   2374  1.4.4.2  skrll 
   2375  1.4.4.4  skrll fail:	ifp->if_flags &= ~IFF_UP;
   2376  1.4.4.4  skrll 	iwi_stop(ifp, 0);
   2377  1.4.4.2  skrll 
   2378  1.4.4.2  skrll 	return error;
   2379  1.4.4.2  skrll }
   2380  1.4.4.2  skrll 
   2381  1.4.4.2  skrll static void
   2382  1.4.4.2  skrll iwi_stop(struct ifnet *ifp, int disable)
   2383  1.4.4.2  skrll {
   2384  1.4.4.2  skrll 	struct iwi_softc *sc = ifp->if_softc;
   2385  1.4.4.2  skrll 	struct ieee80211com *ic = &sc->sc_ic;
   2386  1.4.4.2  skrll 
   2387  1.4.4.2  skrll 	iwi_stop_master(sc);
   2388  1.4.4.2  skrll 	CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_SW_RESET);
   2389  1.4.4.2  skrll 
   2390  1.4.4.4  skrll 	/* reset rings */
   2391  1.4.4.4  skrll 	iwi_reset_cmd_ring(sc, &sc->cmdq);
   2392  1.4.4.4  skrll 	iwi_reset_tx_ring(sc, &sc->txq);
   2393  1.4.4.4  skrll 	iwi_reset_rx_ring(sc, &sc->rxq);
   2394  1.4.4.2  skrll 
   2395  1.4.4.2  skrll 	ifp->if_timer = 0;
   2396  1.4.4.2  skrll 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   2397  1.4.4.2  skrll 
   2398  1.4.4.2  skrll 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
   2399  1.4.4.2  skrll }
   2400