Home | History | Annotate | Line # | Download | only in pci
if_iwi.c revision 1.64.4.1
      1  1.64.4.1    bouyer /*	$NetBSD: if_iwi.c,v 1.64.4.1 2007/10/25 22:39:00 bouyer Exp $  */
      2       1.1     skrll 
      3       1.1     skrll /*-
      4       1.1     skrll  * Copyright (c) 2004, 2005
      5       1.1     skrll  *      Damien Bergamini <damien.bergamini (at) free.fr>. All rights reserved.
      6       1.1     skrll  *
      7       1.1     skrll  * Redistribution and use in source and binary forms, with or without
      8       1.1     skrll  * modification, are permitted provided that the following conditions
      9       1.1     skrll  * are met:
     10       1.1     skrll  * 1. Redistributions of source code must retain the above copyright
     11       1.1     skrll  *    notice unmodified, this list of conditions, and the following
     12       1.1     skrll  *    disclaimer.
     13       1.1     skrll  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1     skrll  *    notice, this list of conditions and the following disclaimer in the
     15       1.1     skrll  *    documentation and/or other materials provided with the distribution.
     16       1.1     skrll  *
     17       1.1     skrll  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     18       1.1     skrll  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19       1.1     skrll  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20       1.1     skrll  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21       1.1     skrll  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22       1.1     skrll  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23       1.1     skrll  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24       1.1     skrll  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25       1.1     skrll  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26       1.1     skrll  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27       1.1     skrll  * SUCH DAMAGE.
     28       1.1     skrll  */
     29       1.1     skrll 
     30       1.1     skrll #include <sys/cdefs.h>
     31  1.64.4.1    bouyer __KERNEL_RCSID(0, "$NetBSD: if_iwi.c,v 1.64.4.1 2007/10/25 22:39:00 bouyer Exp $");
     32       1.1     skrll 
     33       1.1     skrll /*-
     34      1.13     skrll  * Intel(R) PRO/Wireless 2200BG/2225BG/2915ABG driver
     35       1.1     skrll  * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
     36       1.1     skrll  */
     37       1.1     skrll 
     38       1.1     skrll #include "bpfilter.h"
     39       1.1     skrll 
     40       1.1     skrll #include <sys/param.h>
     41       1.1     skrll #include <sys/sockio.h>
     42       1.1     skrll #include <sys/sysctl.h>
     43       1.1     skrll #include <sys/mbuf.h>
     44       1.1     skrll #include <sys/kernel.h>
     45       1.1     skrll #include <sys/socket.h>
     46       1.1     skrll #include <sys/systm.h>
     47       1.1     skrll #include <sys/malloc.h>
     48       1.1     skrll #include <sys/conf.h>
     49      1.48      elad #include <sys/kauth.h>
     50       1.1     skrll 
     51  1.64.4.1    bouyer #include <sys/bus.h>
     52       1.1     skrll #include <machine/endian.h>
     53  1.64.4.1    bouyer #include <sys/intr.h>
     54       1.1     skrll 
     55      1.52     skrll #include <dev/firmload.h>
     56      1.52     skrll 
     57       1.1     skrll #include <dev/pci/pcireg.h>
     58       1.1     skrll #include <dev/pci/pcivar.h>
     59       1.1     skrll #include <dev/pci/pcidevs.h>
     60       1.1     skrll 
     61       1.1     skrll #if NBPFILTER > 0
     62       1.1     skrll #include <net/bpf.h>
     63       1.1     skrll #endif
     64       1.1     skrll #include <net/if.h>
     65       1.1     skrll #include <net/if_arp.h>
     66       1.1     skrll #include <net/if_dl.h>
     67       1.1     skrll #include <net/if_ether.h>
     68       1.1     skrll #include <net/if_media.h>
     69       1.1     skrll #include <net/if_types.h>
     70       1.1     skrll 
     71       1.1     skrll #include <net80211/ieee80211_var.h>
     72       1.1     skrll #include <net80211/ieee80211_radiotap.h>
     73       1.1     skrll 
     74       1.1     skrll #include <netinet/in.h>
     75       1.1     skrll #include <netinet/in_systm.h>
     76       1.1     skrll #include <netinet/in_var.h>
     77       1.1     skrll #include <netinet/ip.h>
     78       1.1     skrll 
     79       1.1     skrll #include <crypto/arc4/arc4.h>
     80       1.1     skrll 
     81       1.1     skrll #include <dev/pci/if_iwireg.h>
     82       1.1     skrll #include <dev/pci/if_iwivar.h>
     83       1.1     skrll 
     84      1.20     skrll #ifdef IWI_DEBUG
     85      1.20     skrll #define DPRINTF(x)	if (iwi_debug > 0) printf x
     86      1.20     skrll #define DPRINTFN(n, x)	if (iwi_debug >= (n)) printf x
     87      1.20     skrll int iwi_debug = 4;
     88      1.20     skrll #else
     89      1.20     skrll #define DPRINTF(x)
     90      1.20     skrll #define DPRINTFN(n, x)
     91      1.20     skrll #endif
     92       1.1     skrll 
     93  1.64.4.1    bouyer static int	iwi_match(device_t, struct cfdata *, void *);
     94  1.64.4.1    bouyer static void	iwi_attach(device_t, device_t, void *);
     95  1.64.4.1    bouyer static int	iwi_detach(device_t, int);
     96      1.25     skrll 
     97      1.25     skrll static void	iwi_shutdown(void *);
     98      1.25     skrll static int	iwi_suspend(struct iwi_softc *);
     99      1.25     skrll static int	iwi_resume(struct iwi_softc *);
    100      1.25     skrll static void	iwi_powerhook(int, void *);
    101      1.15     skrll 
    102      1.25     skrll static int	iwi_alloc_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *,
    103      1.14     skrll     int);
    104      1.25     skrll static void	iwi_reset_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
    105      1.25     skrll static void	iwi_free_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
    106      1.25     skrll static int	iwi_alloc_tx_ring(struct iwi_softc *, struct iwi_tx_ring *,
    107      1.59     skrll     int, bus_addr_t, bus_size_t);
    108      1.25     skrll static void	iwi_reset_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
    109      1.25     skrll static void	iwi_free_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
    110      1.31     joerg static struct mbuf *
    111      1.31     joerg 		iwi_alloc_rx_buf(struct iwi_softc *sc);
    112      1.25     skrll static int	iwi_alloc_rx_ring(struct iwi_softc *, struct iwi_rx_ring *,
    113      1.14     skrll     int);
    114      1.25     skrll static void	iwi_reset_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
    115      1.25     skrll static void	iwi_free_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
    116      1.14     skrll 
    117      1.38     skrll static struct	ieee80211_node *iwi_node_alloc(struct ieee80211_node_table *);
    118      1.38     skrll static void	iwi_node_free(struct ieee80211_node *);
    119      1.38     skrll 
    120      1.59     skrll static int	iwi_cvtrate(int);
    121      1.25     skrll static int	iwi_media_change(struct ifnet *);
    122      1.25     skrll static void	iwi_media_status(struct ifnet *, struct ifmediareq *);
    123      1.38     skrll static int	iwi_wme_update(struct ieee80211com *);
    124      1.25     skrll static uint16_t	iwi_read_prom_word(struct iwi_softc *, uint8_t);
    125      1.25     skrll static int	iwi_newstate(struct ieee80211com *, enum ieee80211_state, int);
    126      1.25     skrll static void	iwi_fix_channel(struct ieee80211com *, struct mbuf *);
    127      1.25     skrll static void	iwi_frame_intr(struct iwi_softc *, struct iwi_rx_data *, int,
    128       1.1     skrll     struct iwi_frame *);
    129      1.25     skrll static void	iwi_notification_intr(struct iwi_softc *, struct iwi_notif *);
    130      1.41     skrll static void	iwi_cmd_intr(struct iwi_softc *);
    131      1.25     skrll static void	iwi_rx_intr(struct iwi_softc *);
    132      1.38     skrll static void	iwi_tx_intr(struct iwi_softc *, struct iwi_tx_ring *);
    133      1.25     skrll static int	iwi_intr(void *);
    134      1.25     skrll static int	iwi_cmd(struct iwi_softc *, uint8_t, void *, uint8_t, int);
    135      1.38     skrll static void	iwi_write_ibssnode(struct iwi_softc *, const struct iwi_node *);
    136      1.38     skrll static int	iwi_tx_start(struct ifnet *, struct mbuf *, struct ieee80211_node *,
    137      1.38     skrll     int);
    138      1.25     skrll static void	iwi_start(struct ifnet *);
    139      1.25     skrll static void	iwi_watchdog(struct ifnet *);
    140      1.38     skrll 
    141      1.38     skrll static int	iwi_alloc_unr(struct iwi_softc *);
    142      1.38     skrll static void	iwi_free_unr(struct iwi_softc *, int);
    143      1.38     skrll 
    144      1.25     skrll static int	iwi_get_table0(struct iwi_softc *, uint32_t *);
    145      1.38     skrll 
    146      1.61  christos static int	iwi_ioctl(struct ifnet *, u_long, void *);
    147      1.25     skrll static void	iwi_stop_master(struct iwi_softc *);
    148      1.25     skrll static int	iwi_reset(struct iwi_softc *);
    149      1.25     skrll static int	iwi_load_ucode(struct iwi_softc *, void *, int);
    150      1.25     skrll static int	iwi_load_firmware(struct iwi_softc *, void *, int);
    151      1.52     skrll static int	iwi_cache_firmware(struct iwi_softc *);
    152      1.25     skrll static void	iwi_free_firmware(struct iwi_softc *);
    153      1.25     skrll static int	iwi_config(struct iwi_softc *);
    154      1.25     skrll static int	iwi_set_chan(struct iwi_softc *, struct ieee80211_channel *);
    155      1.25     skrll static int	iwi_scan(struct iwi_softc *);
    156      1.25     skrll static int	iwi_auth_and_assoc(struct iwi_softc *);
    157      1.25     skrll static int	iwi_init(struct ifnet *);
    158      1.25     skrll static void	iwi_stop(struct ifnet *, int);
    159      1.54     skrll static int	iwi_getrfkill(struct iwi_softc *);
    160      1.42    rpaulo static void	iwi_led_set(struct iwi_softc *, uint32_t, int);
    161      1.54     skrll static void	iwi_sysctlattach(struct iwi_softc *);
    162       1.1     skrll 
    163      1.20     skrll /*
    164      1.20     skrll  * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
    165      1.20     skrll  */
    166      1.20     skrll static const struct ieee80211_rateset iwi_rateset_11a =
    167      1.20     skrll 	{ 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
    168      1.20     skrll 
    169      1.20     skrll static const struct ieee80211_rateset iwi_rateset_11b =
    170      1.20     skrll 	{ 4, { 2, 4, 11, 22 } };
    171      1.20     skrll 
    172      1.20     skrll static const struct ieee80211_rateset iwi_rateset_11g =
    173      1.20     skrll 	{ 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
    174      1.20     skrll 
    175      1.44     perry static inline uint8_t
    176      1.24     skrll MEM_READ_1(struct iwi_softc *sc, uint32_t addr)
    177       1.1     skrll {
    178       1.1     skrll 	CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
    179       1.1     skrll 	return CSR_READ_1(sc, IWI_CSR_INDIRECT_DATA);
    180       1.1     skrll }
    181       1.1     skrll 
    182      1.44     perry static inline uint32_t
    183      1.24     skrll MEM_READ_4(struct iwi_softc *sc, uint32_t addr)
    184       1.1     skrll {
    185       1.1     skrll 	CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
    186       1.1     skrll 	return CSR_READ_4(sc, IWI_CSR_INDIRECT_DATA);
    187       1.1     skrll }
    188       1.1     skrll 
    189  1.64.4.1    bouyer CFATTACH_DECL_NEW(iwi, sizeof (struct iwi_softc), iwi_match, iwi_attach,
    190       1.1     skrll     iwi_detach, NULL);
    191       1.1     skrll 
    192       1.1     skrll static int
    193  1.64.4.1    bouyer iwi_match(device_t parent, struct cfdata *match, void *aux)
    194       1.1     skrll {
    195       1.1     skrll 	struct pci_attach_args *pa = aux;
    196       1.5     perry 
    197       1.1     skrll 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
    198       1.1     skrll 		return 0;
    199       1.1     skrll 
    200       1.1     skrll 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2200BG ||
    201      1.13     skrll 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2225BG ||
    202      1.13     skrll 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_1 ||
    203      1.13     skrll 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_2)
    204       1.1     skrll 		return 1;
    205       1.5     perry 
    206       1.1     skrll 	return 0;
    207       1.1     skrll }
    208       1.1     skrll 
    209       1.1     skrll /* Base Address Register */
    210       1.1     skrll #define IWI_PCI_BAR0	0x10
    211       1.1     skrll 
    212       1.1     skrll static void
    213  1.64.4.1    bouyer iwi_attach(device_t parent, device_t self, void *aux)
    214       1.1     skrll {
    215  1.64.4.1    bouyer 	struct iwi_softc *sc = device_private(self);
    216       1.1     skrll 	struct ieee80211com *ic = &sc->sc_ic;
    217       1.9    dyoung 	struct ifnet *ifp = &sc->sc_if;
    218       1.1     skrll 	struct pci_attach_args *pa = aux;
    219       1.1     skrll 	const char *intrstr;
    220       1.1     skrll 	char devinfo[256];
    221       1.1     skrll 	bus_space_tag_t memt;
    222       1.1     skrll 	bus_space_handle_t memh;
    223       1.1     skrll 	pci_intr_handle_t ih;
    224       1.1     skrll 	pcireg_t data;
    225      1.24     skrll 	uint16_t val;
    226       1.1     skrll 	int error, revision, i;
    227       1.1     skrll 
    228  1.64.4.1    bouyer 	sc->sc_dev = self;
    229       1.1     skrll 	sc->sc_pct = pa->pa_pc;
    230       1.1     skrll 	sc->sc_pcitag = pa->pa_tag;
    231       1.1     skrll 
    232       1.1     skrll 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof devinfo);
    233       1.1     skrll 	revision = PCI_REVISION(pa->pa_class);
    234       1.1     skrll 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo, revision);
    235       1.1     skrll 
    236       1.1     skrll 	/* clear device specific PCI configuration register 0x41 */
    237       1.1     skrll 	data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
    238       1.1     skrll 	data &= ~0x0000ff00;
    239       1.1     skrll 	pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data);
    240       1.1     skrll 
    241      1.38     skrll 	/* clear unit numbers allocated to IBSS */
    242      1.38     skrll 	sc->sc_unr = 0;
    243      1.38     skrll 
    244      1.50  christos 	/* power up chip */
    245      1.50  christos 	if ((error = pci_activate(pa->pa_pc, pa->pa_tag, sc,
    246      1.50  christos 	    NULL)) && error != EOPNOTSUPP) {
    247  1.64.4.1    bouyer 		aprint_error_dev(self, "cannot activate %d\n", error);
    248      1.50  christos 		return;
    249      1.50  christos 	}
    250      1.50  christos 
    251       1.1     skrll 	/* enable bus-mastering */
    252       1.1     skrll 	data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
    253       1.1     skrll 	data |= PCI_COMMAND_MASTER_ENABLE;
    254       1.1     skrll 	pci_conf_write(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG, data);
    255       1.1     skrll 
    256       1.1     skrll 	/* map the register window */
    257       1.1     skrll 	error = pci_mapreg_map(pa, IWI_PCI_BAR0, PCI_MAPREG_TYPE_MEM |
    258      1.59     skrll 	    PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh, NULL, &sc->sc_sz);
    259       1.1     skrll 	if (error != 0) {
    260  1.64.4.1    bouyer 		aprint_error_dev(self, "could not map memory space\n");
    261       1.1     skrll 		return;
    262       1.1     skrll 	}
    263       1.1     skrll 
    264       1.1     skrll 	sc->sc_st = memt;
    265       1.1     skrll 	sc->sc_sh = memh;
    266       1.1     skrll 	sc->sc_dmat = pa->pa_dmat;
    267       1.1     skrll 
    268       1.1     skrll 	/* disable interrupts */
    269       1.1     skrll 	CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
    270       1.1     skrll 
    271       1.1     skrll 	if (pci_intr_map(pa, &ih) != 0) {
    272  1.64.4.1    bouyer 		aprint_error_dev(self, "could not map interrupt\n");
    273       1.1     skrll 		return;
    274       1.1     skrll 	}
    275       1.1     skrll 
    276       1.1     skrll 	intrstr = pci_intr_string(sc->sc_pct, ih);
    277       1.1     skrll 	sc->sc_ih = pci_intr_establish(sc->sc_pct, ih, IPL_NET, iwi_intr, sc);
    278       1.1     skrll 	if (sc->sc_ih == NULL) {
    279  1.64.4.1    bouyer 		aprint_error_dev(self, "could not establish interrupt");
    280       1.1     skrll 		if (intrstr != NULL)
    281       1.1     skrll 			aprint_error(" at %s", intrstr);
    282       1.1     skrll 		aprint_error("\n");
    283       1.1     skrll 		return;
    284       1.1     skrll 	}
    285  1.64.4.1    bouyer 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
    286       1.1     skrll 
    287       1.1     skrll 	if (iwi_reset(sc) != 0) {
    288  1.64.4.1    bouyer 		aprint_error_dev(self, "could not reset adapter\n");
    289       1.1     skrll 		return;
    290       1.1     skrll 	}
    291       1.1     skrll 
    292      1.14     skrll 	/*
    293      1.14     skrll 	 * Allocate rings.
    294      1.14     skrll 	 */
    295      1.14     skrll 	if (iwi_alloc_cmd_ring(sc, &sc->cmdq, IWI_CMD_RING_COUNT) != 0) {
    296  1.64.4.1    bouyer 		aprint_error_dev(self, "could not allocate command ring\n");
    297      1.14     skrll 		goto fail;
    298      1.14     skrll 	}
    299      1.14     skrll 
    300      1.38     skrll 	error = iwi_alloc_tx_ring(sc, &sc->txq[0], IWI_TX_RING_COUNT,
    301      1.38     skrll 	    IWI_CSR_TX1_RIDX, IWI_CSR_TX1_WIDX);
    302      1.38     skrll 	if (error != 0) {
    303  1.64.4.1    bouyer 		aprint_error_dev(self, "could not allocate Tx ring 1\n");
    304      1.38     skrll 		goto fail;
    305      1.38     skrll 	}
    306      1.38     skrll 
    307      1.38     skrll 	error = iwi_alloc_tx_ring(sc, &sc->txq[1], IWI_TX_RING_COUNT,
    308      1.38     skrll 	    IWI_CSR_TX2_RIDX, IWI_CSR_TX2_WIDX);
    309      1.38     skrll 	if (error != 0) {
    310  1.64.4.1    bouyer 		aprint_error_dev(self, "could not allocate Tx ring 2\n");
    311      1.38     skrll 		goto fail;
    312      1.38     skrll 	}
    313      1.38     skrll 
    314      1.38     skrll 	error = iwi_alloc_tx_ring(sc, &sc->txq[2], IWI_TX_RING_COUNT,
    315      1.38     skrll 	    IWI_CSR_TX3_RIDX, IWI_CSR_TX3_WIDX);
    316      1.38     skrll 	if (error != 0) {
    317  1.64.4.1    bouyer 		aprint_error_dev(self, "could not allocate Tx ring 3\n");
    318      1.38     skrll 		goto fail;
    319      1.38     skrll 	}
    320      1.38     skrll 
    321      1.38     skrll 	error = iwi_alloc_tx_ring(sc, &sc->txq[3], IWI_TX_RING_COUNT,
    322      1.38     skrll 	    IWI_CSR_TX4_RIDX, IWI_CSR_TX4_WIDX);
    323      1.38     skrll 	if (error != 0) {
    324  1.64.4.1    bouyer 		aprint_error_dev(self, "could not allocate Tx ring 4\n");
    325      1.14     skrll 		goto fail;
    326      1.14     skrll 	}
    327      1.14     skrll 
    328      1.14     skrll 	if (iwi_alloc_rx_ring(sc, &sc->rxq, IWI_RX_RING_COUNT) != 0) {
    329  1.64.4.1    bouyer 		aprint_error_dev(self, "could not allocate Rx ring\n");
    330      1.14     skrll 		goto fail;
    331       1.1     skrll 	}
    332       1.1     skrll 
    333       1.9    dyoung 	ic->ic_ifp = ifp;
    334      1.38     skrll 	ic->ic_wme.wme_update = iwi_wme_update;
    335      1.12  christos 	ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
    336      1.12  christos 	ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
    337      1.13     skrll 	ic->ic_state = IEEE80211_S_INIT;
    338       1.1     skrll 
    339      1.52     skrll 	sc->sc_fwname = "iwi-bss.fw";
    340      1.52     skrll 
    341       1.1     skrll 	/* set device capabilities */
    342      1.38     skrll 	ic->ic_caps =
    343      1.38     skrll 	    IEEE80211_C_IBSS |		/* IBSS mode supported */
    344      1.38     skrll 	    IEEE80211_C_MONITOR |	/* monitor mode supported */
    345      1.38     skrll 	    IEEE80211_C_TXPMGT |	/* tx power management */
    346      1.38     skrll 	    IEEE80211_C_SHPREAMBLE |	/* short preamble supported */
    347      1.59     skrll 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
    348      1.38     skrll 	    IEEE80211_C_WPA |		/* 802.11i */
    349      1.38     skrll 	    IEEE80211_C_WME;		/* 802.11e */
    350       1.1     skrll 
    351       1.1     skrll 	/* read MAC address from EEPROM */
    352       1.1     skrll 	val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 0);
    353      1.33     skrll 	ic->ic_myaddr[0] = val & 0xff;
    354      1.33     skrll 	ic->ic_myaddr[1] = val >> 8;
    355       1.1     skrll 	val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 1);
    356      1.33     skrll 	ic->ic_myaddr[2] = val & 0xff;
    357      1.33     skrll 	ic->ic_myaddr[3] = val >> 8;
    358       1.1     skrll 	val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 2);
    359      1.33     skrll 	ic->ic_myaddr[4] = val & 0xff;
    360      1.33     skrll 	ic->ic_myaddr[5] = val >> 8;
    361       1.1     skrll 
    362  1.64.4.1    bouyer 	aprint_verbose_dev(self, "802.11 address %s\n",
    363       1.1     skrll 	    ether_sprintf(ic->ic_myaddr));
    364       1.1     skrll 
    365      1.42    rpaulo 	/* read the NIC type from EEPROM */
    366      1.49     blymn 	val = iwi_read_prom_word(sc, IWI_EEPROM_NIC_TYPE);
    367      1.42    rpaulo 	sc->nictype = val & 0xff;
    368      1.42    rpaulo 
    369  1.64.4.1    bouyer 	DPRINTF(("%s: NIC type %d\n", device_xname(self), sc->nictype));
    370      1.13     skrll 
    371      1.16     skrll 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_1 ||
    372      1.16     skrll 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_2) {
    373      1.13     skrll 		/* set supported .11a rates (2915ABG only) */
    374       1.1     skrll 		ic->ic_sup_rates[IEEE80211_MODE_11A] = iwi_rateset_11a;
    375       1.1     skrll 
    376       1.1     skrll 		/* set supported .11a channels */
    377       1.1     skrll 		for (i = 36; i <= 64; i += 4) {
    378       1.1     skrll 			ic->ic_channels[i].ic_freq =
    379       1.1     skrll 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
    380       1.1     skrll 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
    381       1.1     skrll 		}
    382      1.12  christos 		for (i = 149; i <= 165; i += 4) {
    383       1.1     skrll 			ic->ic_channels[i].ic_freq =
    384       1.1     skrll 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
    385       1.1     skrll 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
    386       1.1     skrll 		}
    387       1.1     skrll 	}
    388       1.1     skrll 
    389       1.1     skrll 	/* set supported .11b and .11g rates */
    390       1.1     skrll 	ic->ic_sup_rates[IEEE80211_MODE_11B] = iwi_rateset_11b;
    391       1.1     skrll 	ic->ic_sup_rates[IEEE80211_MODE_11G] = iwi_rateset_11g;
    392       1.1     skrll 
    393       1.1     skrll 	/* set supported .11b and .11g channels (1 through 14) */
    394       1.1     skrll 	for (i = 1; i <= 14; i++) {
    395       1.1     skrll 		ic->ic_channels[i].ic_freq =
    396       1.1     skrll 		    ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
    397       1.1     skrll 		ic->ic_channels[i].ic_flags =
    398       1.1     skrll 		    IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
    399       1.1     skrll 		    IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
    400       1.1     skrll 	}
    401       1.1     skrll 
    402       1.1     skrll 	ifp->if_softc = sc;
    403       1.1     skrll 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    404       1.1     skrll 	ifp->if_init = iwi_init;
    405       1.1     skrll 	ifp->if_stop = iwi_stop;
    406       1.1     skrll 	ifp->if_ioctl = iwi_ioctl;
    407       1.1     skrll 	ifp->if_start = iwi_start;
    408       1.1     skrll 	ifp->if_watchdog = iwi_watchdog;
    409       1.1     skrll 	IFQ_SET_READY(&ifp->if_snd);
    410  1.64.4.1    bouyer 	memcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
    411       1.1     skrll 
    412       1.1     skrll 	if_attach(ifp);
    413       1.9    dyoung 	ieee80211_ifattach(ic);
    414      1.38     skrll 	/* override default methods */
    415      1.38     skrll 	ic->ic_node_alloc = iwi_node_alloc;
    416      1.38     skrll 	sc->sc_node_free = ic->ic_node_free;
    417      1.38     skrll 	ic->ic_node_free = iwi_node_free;
    418       1.1     skrll 	/* override state transition machine */
    419       1.1     skrll 	sc->sc_newstate = ic->ic_newstate;
    420       1.1     skrll 	ic->ic_newstate = iwi_newstate;
    421       1.9    dyoung 	ieee80211_media_init(ic, iwi_media_change, iwi_media_status);
    422       1.1     skrll 
    423       1.1     skrll #if NBPFILTER > 0
    424       1.1     skrll 	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
    425       1.1     skrll 	    sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf);
    426       1.1     skrll 
    427       1.1     skrll 	sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
    428       1.1     skrll 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
    429       1.1     skrll 	sc->sc_rxtap.wr_ihdr.it_present = htole32(IWI_RX_RADIOTAP_PRESENT);
    430       1.1     skrll 
    431       1.1     skrll 	sc->sc_txtap_len = sizeof sc->sc_txtapu;
    432       1.1     skrll 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
    433       1.1     skrll 	sc->sc_txtap.wt_ihdr.it_present = htole32(IWI_TX_RADIOTAP_PRESENT);
    434       1.1     skrll #endif
    435      1.15     skrll 
    436      1.54     skrll 	iwi_sysctlattach(sc);
    437      1.54     skrll 
    438      1.15     skrll 	/*
    439      1.15     skrll 	 * Make sure the interface is shutdown during reboot.
    440      1.15     skrll 	 */
    441      1.15     skrll 	sc->sc_sdhook = shutdownhook_establish(iwi_shutdown, sc);
    442      1.15     skrll 	if (sc->sc_sdhook == NULL)
    443  1.64.4.1    bouyer 		aprint_error_dev(self,
    444  1.64.4.1    bouyer 		    "WARNING: unable to establish shutdown hook\n");
    445  1.64.4.1    bouyer 	sc->sc_powerhook = powerhook_establish(device_xname(self),
    446      1.56  jmcneill 	    iwi_powerhook, sc);
    447      1.15     skrll 	if (sc->sc_powerhook == NULL)
    448  1.64.4.1    bouyer 		aprint_error_dev(self,
    449  1.64.4.1    bouyer 		    "WARNING: unable to establish power hook\n");
    450      1.15     skrll 
    451      1.13     skrll 	ieee80211_announce(ic);
    452      1.14     skrll 
    453      1.14     skrll 	return;
    454      1.14     skrll 
    455      1.14     skrll fail:	iwi_detach(self, 0);
    456       1.1     skrll }
    457       1.1     skrll 
    458       1.1     skrll static int
    459  1.64.4.1    bouyer iwi_detach(device_t self, int flags)
    460       1.1     skrll {
    461  1.64.4.1    bouyer 	struct iwi_softc *sc = device_private(self);
    462       1.9    dyoung 	struct ifnet *ifp = &sc->sc_if;
    463       1.1     skrll 
    464      1.47    rpaulo 	if (ifp != NULL)
    465      1.47    rpaulo 		iwi_stop(ifp, 1);
    466      1.47    rpaulo 
    467       1.1     skrll 	iwi_free_firmware(sc);
    468       1.1     skrll 
    469       1.9    dyoung 	ieee80211_ifdetach(&sc->sc_ic);
    470      1.25     skrll 	if (ifp != NULL)
    471      1.25     skrll 		if_detach(ifp);
    472       1.1     skrll 
    473      1.14     skrll 	iwi_free_cmd_ring(sc, &sc->cmdq);
    474      1.38     skrll 	iwi_free_tx_ring(sc, &sc->txq[0]);
    475      1.38     skrll 	iwi_free_tx_ring(sc, &sc->txq[1]);
    476      1.38     skrll 	iwi_free_tx_ring(sc, &sc->txq[2]);
    477      1.38     skrll 	iwi_free_tx_ring(sc, &sc->txq[3]);
    478      1.14     skrll 	iwi_free_rx_ring(sc, &sc->rxq);
    479       1.1     skrll 
    480       1.1     skrll 	if (sc->sc_ih != NULL) {
    481       1.1     skrll 		pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
    482       1.1     skrll 		sc->sc_ih = NULL;
    483       1.1     skrll 	}
    484       1.1     skrll 
    485       1.1     skrll 	bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz);
    486       1.1     skrll 
    487      1.18     skrll 	powerhook_disestablish(sc->sc_powerhook);
    488      1.18     skrll 	shutdownhook_disestablish(sc->sc_sdhook);
    489      1.18     skrll 
    490       1.1     skrll 	return 0;
    491       1.1     skrll }
    492       1.1     skrll 
    493       1.1     skrll static int
    494      1.14     skrll iwi_alloc_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring,
    495      1.14     skrll     int count)
    496       1.1     skrll {
    497      1.14     skrll 	int error, nsegs;
    498      1.14     skrll 
    499      1.14     skrll 	ring->count = count;
    500      1.14     skrll 	ring->queued = 0;
    501      1.14     skrll 	ring->cur = ring->next = 0;
    502       1.1     skrll 
    503       1.1     skrll 	/*
    504      1.14     skrll 	 * Allocate and map command ring
    505       1.1     skrll 	 */
    506       1.1     skrll 	error = bus_dmamap_create(sc->sc_dmat,
    507      1.25     skrll 	    IWI_CMD_DESC_SIZE * count, 1,
    508      1.25     skrll 	    IWI_CMD_DESC_SIZE * count, 0,
    509      1.14     skrll 	    BUS_DMA_NOWAIT, &ring->desc_map);
    510       1.1     skrll 	if (error != 0) {
    511  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev,
    512  1.64.4.1    bouyer 		    "could not create command ring DMA map\n");
    513       1.1     skrll 		goto fail;
    514       1.1     skrll 	}
    515       1.1     skrll 
    516       1.1     skrll 	error = bus_dmamem_alloc(sc->sc_dmat,
    517      1.25     skrll 	    IWI_CMD_DESC_SIZE * count, PAGE_SIZE, 0,
    518      1.14     skrll 	    &sc->cmdq.desc_seg, 1, &nsegs, BUS_DMA_NOWAIT);
    519       1.1     skrll 	if (error != 0) {
    520  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev,
    521  1.64.4.1    bouyer 		    "could not allocate command ring DMA memory\n");
    522       1.1     skrll 		goto fail;
    523       1.1     skrll 	}
    524       1.1     skrll 
    525      1.14     skrll 	error = bus_dmamem_map(sc->sc_dmat, &sc->cmdq.desc_seg, nsegs,
    526      1.25     skrll 	    IWI_CMD_DESC_SIZE * count,
    527      1.61  christos 	    (void **)&sc->cmdq.desc, BUS_DMA_NOWAIT);
    528       1.1     skrll 	if (error != 0) {
    529  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev,
    530  1.64.4.1    bouyer 		    "could not map command ring DMA memory\n");
    531       1.1     skrll 		goto fail;
    532       1.1     skrll 	}
    533       1.1     skrll 
    534      1.14     skrll 	error = bus_dmamap_load(sc->sc_dmat, sc->cmdq.desc_map, sc->cmdq.desc,
    535      1.25     skrll 	    IWI_CMD_DESC_SIZE * count, NULL,
    536       1.1     skrll 	    BUS_DMA_NOWAIT);
    537       1.1     skrll 	if (error != 0) {
    538  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev,
    539  1.64.4.1    bouyer 		    "could not load command ring DMA map\n");
    540       1.1     skrll 		goto fail;
    541       1.1     skrll 	}
    542       1.1     skrll 
    543      1.14     skrll 	memset(sc->cmdq.desc, 0,
    544      1.25     skrll 	    IWI_CMD_DESC_SIZE * count);
    545      1.14     skrll 
    546      1.14     skrll 	return 0;
    547      1.14     skrll 
    548      1.14     skrll fail:	iwi_free_cmd_ring(sc, ring);
    549      1.14     skrll 	return error;
    550      1.14     skrll }
    551      1.14     skrll 
    552      1.14     skrll static void
    553      1.14     skrll iwi_reset_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
    554      1.14     skrll {
    555      1.41     skrll 	int i;
    556      1.41     skrll 
    557      1.41     skrll 	for (i = ring->next; i != ring->cur;) {
    558      1.41     skrll 		bus_dmamap_sync(sc->sc_dmat, sc->cmdq.desc_map,
    559      1.41     skrll 		    i * IWI_CMD_DESC_SIZE, IWI_CMD_DESC_SIZE,
    560      1.41     skrll 		    BUS_DMASYNC_POSTWRITE);
    561      1.41     skrll 
    562      1.41     skrll 		wakeup(&ring->desc[i]);
    563      1.41     skrll 		i = (i + 1) % ring->count;
    564      1.41     skrll 	}
    565      1.41     skrll 
    566      1.14     skrll 	ring->queued = 0;
    567      1.14     skrll 	ring->cur = ring->next = 0;
    568      1.14     skrll }
    569      1.14     skrll 
    570      1.14     skrll static void
    571      1.14     skrll iwi_free_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
    572      1.14     skrll {
    573      1.14     skrll 	if (ring->desc_map != NULL) {
    574      1.14     skrll 		if (ring->desc != NULL) {
    575      1.14     skrll 			bus_dmamap_unload(sc->sc_dmat, ring->desc_map);
    576      1.61  christos 			bus_dmamem_unmap(sc->sc_dmat, (void *)ring->desc,
    577      1.25     skrll 			    IWI_CMD_DESC_SIZE * ring->count);
    578      1.14     skrll 			bus_dmamem_free(sc->sc_dmat, &ring->desc_seg, 1);
    579      1.14     skrll 		}
    580      1.14     skrll 		bus_dmamap_destroy(sc->sc_dmat, ring->desc_map);
    581      1.14     skrll 	}
    582      1.14     skrll }
    583      1.14     skrll 
    584      1.14     skrll static int
    585      1.14     skrll iwi_alloc_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring,
    586      1.59     skrll     int count, bus_size_t csr_ridx, bus_size_t csr_widx)
    587      1.14     skrll {
    588      1.14     skrll 	int i, error, nsegs;
    589      1.14     skrll 
    590      1.14     skrll 	ring->count = count;
    591      1.14     skrll 	ring->queued = 0;
    592      1.14     skrll 	ring->cur = ring->next = 0;
    593      1.38     skrll 	ring->csr_ridx = csr_ridx;
    594      1.38     skrll 	ring->csr_widx = csr_widx;
    595       1.1     skrll 
    596       1.1     skrll 	/*
    597      1.14     skrll 	 * Allocate and map Tx ring
    598       1.1     skrll 	 */
    599       1.1     skrll 	error = bus_dmamap_create(sc->sc_dmat,
    600      1.25     skrll 	    IWI_TX_DESC_SIZE * count, 1,
    601      1.25     skrll 	    IWI_TX_DESC_SIZE * count, 0, BUS_DMA_NOWAIT,
    602      1.14     skrll 	    &ring->desc_map);
    603       1.1     skrll 	if (error != 0) {
    604  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev,
    605  1.64.4.1    bouyer 		    "could not create tx ring DMA map\n");
    606       1.1     skrll 		goto fail;
    607       1.1     skrll 	}
    608       1.1     skrll 
    609       1.1     skrll 	error = bus_dmamem_alloc(sc->sc_dmat,
    610      1.25     skrll 	    IWI_TX_DESC_SIZE * count, PAGE_SIZE, 0,
    611      1.14     skrll 	    &ring->desc_seg, 1, &nsegs, BUS_DMA_NOWAIT);
    612       1.1     skrll 	if (error != 0) {
    613  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev,
    614  1.64.4.1    bouyer 		    "could not allocate tx ring DMA memory\n");
    615       1.1     skrll 		goto fail;
    616       1.1     skrll 	}
    617       1.1     skrll 
    618      1.14     skrll 	error = bus_dmamem_map(sc->sc_dmat, &ring->desc_seg, nsegs,
    619      1.25     skrll 	    IWI_TX_DESC_SIZE * count,
    620      1.61  christos 	    (void **)&ring->desc, BUS_DMA_NOWAIT);
    621       1.1     skrll 	if (error != 0) {
    622  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev,
    623  1.64.4.1    bouyer 		    "could not map tx ring DMA memory\n");
    624       1.1     skrll 		goto fail;
    625       1.1     skrll 	}
    626       1.1     skrll 
    627      1.14     skrll 	error = bus_dmamap_load(sc->sc_dmat, ring->desc_map, ring->desc,
    628      1.25     skrll 	    IWI_TX_DESC_SIZE * count, NULL,
    629       1.1     skrll 	    BUS_DMA_NOWAIT);
    630       1.1     skrll 	if (error != 0) {
    631  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev,
    632  1.64.4.1    bouyer 		    "could not load tx ring DMA map\n");
    633       1.1     skrll 		goto fail;
    634       1.1     skrll 	}
    635       1.1     skrll 
    636      1.25     skrll 	memset(ring->desc, 0, IWI_TX_DESC_SIZE * count);
    637      1.14     skrll 
    638      1.14     skrll 	ring->data = malloc(count * sizeof (struct iwi_tx_data), M_DEVBUF,
    639      1.14     skrll 	    M_NOWAIT | M_ZERO);
    640      1.14     skrll 	if (ring->data == NULL) {
    641  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev, "could not allocate soft data\n");
    642      1.14     skrll 		error = ENOMEM;
    643      1.14     skrll 		goto fail;
    644      1.14     skrll 	}
    645       1.1     skrll 
    646       1.1     skrll 	/*
    647       1.1     skrll 	 * Allocate Tx buffers DMA maps
    648       1.1     skrll 	 */
    649      1.14     skrll 	for (i = 0; i < count; i++) {
    650       1.1     skrll 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, IWI_MAX_NSEG,
    651      1.14     skrll 		    MCLBYTES, 0, BUS_DMA_NOWAIT, &ring->data[i].map);
    652       1.1     skrll 		if (error != 0) {
    653  1.64.4.1    bouyer 			aprint_error_dev(sc->sc_dev,
    654  1.64.4.1    bouyer 			    "could not create tx buf DMA map");
    655       1.1     skrll 			goto fail;
    656       1.1     skrll 		}
    657       1.1     skrll 	}
    658      1.14     skrll 	return 0;
    659      1.14     skrll 
    660      1.14     skrll fail:	iwi_free_tx_ring(sc, ring);
    661      1.14     skrll 	return error;
    662      1.14     skrll }
    663      1.14     skrll 
    664      1.14     skrll static void
    665      1.14     skrll iwi_reset_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
    666      1.14     skrll {
    667      1.14     skrll 	struct iwi_tx_data *data;
    668      1.14     skrll 	int i;
    669      1.14     skrll 
    670      1.14     skrll 	for (i = 0; i < ring->count; i++) {
    671      1.14     skrll 		data = &ring->data[i];
    672      1.14     skrll 
    673      1.14     skrll 		if (data->m != NULL) {
    674      1.14     skrll 			bus_dmamap_sync(sc->sc_dmat, data->map, 0,
    675      1.34       scw 			    data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
    676      1.14     skrll 			bus_dmamap_unload(sc->sc_dmat, data->map);
    677      1.14     skrll 			m_freem(data->m);
    678      1.14     skrll 			data->m = NULL;
    679      1.14     skrll 		}
    680      1.14     skrll 
    681      1.14     skrll 		if (data->ni != NULL) {
    682      1.14     skrll 			ieee80211_free_node(data->ni);
    683      1.14     skrll 			data->ni = NULL;
    684      1.14     skrll 		}
    685      1.14     skrll 	}
    686      1.14     skrll 
    687      1.14     skrll 	ring->queued = 0;
    688      1.14     skrll 	ring->cur = ring->next = 0;
    689      1.14     skrll }
    690      1.14     skrll 
    691      1.14     skrll static void
    692      1.14     skrll iwi_free_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
    693      1.14     skrll {
    694      1.14     skrll 	int i;
    695      1.14     skrll 
    696      1.14     skrll 	if (ring->desc_map != NULL) {
    697      1.14     skrll 		if (ring->desc != NULL) {
    698      1.14     skrll 			bus_dmamap_unload(sc->sc_dmat, ring->desc_map);
    699      1.61  christos 			bus_dmamem_unmap(sc->sc_dmat, (void *)ring->desc,
    700      1.25     skrll 			    IWI_TX_DESC_SIZE * ring->count);
    701      1.14     skrll 			bus_dmamem_free(sc->sc_dmat, &ring->desc_seg, 1);
    702      1.14     skrll 		}
    703      1.14     skrll 		bus_dmamap_destroy(sc->sc_dmat, ring->desc_map);
    704      1.14     skrll 	}
    705      1.14     skrll 
    706      1.14     skrll 	for (i = 0; i < ring->count; i++) {
    707      1.14     skrll 		if (ring->data[i].m != NULL) {
    708      1.14     skrll 			bus_dmamap_unload(sc->sc_dmat, ring->data[i].map);
    709      1.14     skrll 			m_freem(ring->data[i].m);
    710      1.14     skrll 		}
    711      1.14     skrll 		bus_dmamap_destroy(sc->sc_dmat, ring->data[i].map);
    712      1.14     skrll 	}
    713      1.14     skrll }
    714      1.14     skrll 
    715      1.14     skrll static int
    716  1.64.4.1    bouyer iwi_alloc_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring, int count)
    717      1.14     skrll {
    718      1.14     skrll 	int i, error;
    719      1.14     skrll 
    720      1.14     skrll 	ring->count = count;
    721      1.14     skrll 	ring->cur = 0;
    722      1.14     skrll 
    723      1.14     skrll 	ring->data = malloc(count * sizeof (struct iwi_rx_data), M_DEVBUF,
    724      1.14     skrll 	    M_NOWAIT | M_ZERO);
    725      1.14     skrll 	if (ring->data == NULL) {
    726  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev, "could not allocate soft data\n");
    727      1.14     skrll 		error = ENOMEM;
    728      1.14     skrll 		goto fail;
    729      1.14     skrll 	}
    730       1.1     skrll 
    731       1.1     skrll 	/*
    732       1.1     skrll 	 * Allocate and map Rx buffers
    733       1.1     skrll 	 */
    734      1.14     skrll 	for (i = 0; i < count; i++) {
    735       1.1     skrll 
    736       1.1     skrll 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
    737      1.31     joerg 		    0, BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW, &ring->data[i].map);
    738       1.1     skrll 		if (error != 0) {
    739  1.64.4.1    bouyer 			aprint_error_dev(sc->sc_dev,
    740  1.64.4.1    bouyer 			    "could not create rx buf DMA map");
    741       1.1     skrll 			goto fail;
    742       1.1     skrll 		}
    743       1.1     skrll 
    744      1.31     joerg 		if ((ring->data[i].m = iwi_alloc_rx_buf(sc)) == NULL) {
    745       1.1     skrll 			error = ENOMEM;
    746       1.1     skrll 			goto fail;
    747       1.1     skrll 		}
    748       1.1     skrll 
    749      1.34       scw 		error = bus_dmamap_load_mbuf(sc->sc_dmat, ring->data[i].map,
    750      1.34       scw 		    ring->data[i].m, BUS_DMA_READ | BUS_DMA_NOWAIT);
    751       1.1     skrll 		if (error != 0) {
    752  1.64.4.1    bouyer 			aprint_error_dev(sc->sc_dev,
    753  1.64.4.1    bouyer 			    "could not load rx buffer DMA map\n");
    754       1.1     skrll 			goto fail;
    755       1.1     skrll 		}
    756      1.41     skrll 
    757      1.41     skrll 		bus_dmamap_sync(sc->sc_dmat, ring->data[i].map, 0,
    758      1.41     skrll 		    ring->data[i].map->dm_mapsize, BUS_DMASYNC_PREREAD);
    759       1.1     skrll 	}
    760       1.1     skrll 
    761       1.1     skrll 	return 0;
    762       1.1     skrll 
    763      1.14     skrll fail:	iwi_free_rx_ring(sc, ring);
    764       1.1     skrll 	return error;
    765       1.1     skrll }
    766       1.1     skrll 
    767       1.1     skrll static void
    768      1.58  christos iwi_reset_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
    769      1.14     skrll {
    770      1.14     skrll 	ring->cur = 0;
    771      1.14     skrll }
    772      1.14     skrll 
    773      1.14     skrll static void
    774      1.14     skrll iwi_free_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
    775       1.1     skrll {
    776       1.1     skrll 	int i;
    777       1.1     skrll 
    778      1.14     skrll 	for (i = 0; i < ring->count; i++) {
    779      1.14     skrll 		if (ring->data[i].m != NULL) {
    780      1.14     skrll 			bus_dmamap_unload(sc->sc_dmat, ring->data[i].map);
    781      1.14     skrll 			m_freem(ring->data[i].m);
    782       1.1     skrll 		}
    783      1.14     skrll 		bus_dmamap_destroy(sc->sc_dmat, ring->data[i].map);
    784       1.1     skrll 	}
    785       1.1     skrll }
    786       1.1     skrll 
    787      1.15     skrll static void
    788      1.15     skrll iwi_shutdown(void *arg)
    789      1.15     skrll {
    790      1.15     skrll 	struct iwi_softc *sc = (struct iwi_softc *)arg;
    791      1.15     skrll 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
    792      1.15     skrll 
    793      1.15     skrll 	iwi_stop(ifp, 1);
    794      1.15     skrll }
    795      1.15     skrll 
    796      1.15     skrll static int
    797      1.15     skrll iwi_suspend(struct iwi_softc *sc)
    798      1.15     skrll {
    799      1.15     skrll 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
    800      1.15     skrll 
    801      1.15     skrll 	iwi_stop(ifp, 1);
    802      1.15     skrll 
    803      1.15     skrll 	return 0;
    804      1.15     skrll }
    805      1.15     skrll 
    806      1.15     skrll static int
    807      1.15     skrll iwi_resume(struct iwi_softc *sc)
    808      1.15     skrll {
    809      1.15     skrll 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
    810      1.15     skrll 	pcireg_t data;
    811      1.15     skrll 
    812      1.15     skrll 	/* clear device specific PCI configuration register 0x41 */
    813      1.15     skrll 	data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
    814      1.15     skrll 	data &= ~0x0000ff00;
    815      1.15     skrll 	pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data);
    816      1.15     skrll 
    817      1.15     skrll 	if (ifp->if_flags & IFF_UP) {
    818      1.15     skrll 		iwi_init(ifp);
    819      1.15     skrll 		if (ifp->if_flags & IFF_RUNNING)
    820      1.15     skrll 			iwi_start(ifp);
    821      1.15     skrll 	}
    822      1.15     skrll 
    823      1.15     skrll 	return 0;
    824      1.15     skrll }
    825      1.15     skrll 
    826      1.15     skrll static void
    827      1.15     skrll iwi_powerhook(int why, void *arg)
    828      1.15     skrll {
    829      1.15     skrll         struct iwi_softc *sc = arg;
    830      1.46  jmcneill 	pci_chipset_tag_t pc = sc->sc_pct;
    831      1.46  jmcneill 	pcitag_t tag = sc->sc_pcitag;
    832      1.15     skrll 	int s;
    833      1.15     skrll 
    834      1.15     skrll 	s = splnet();
    835      1.15     skrll 	switch (why) {
    836      1.15     skrll 	case PWR_SUSPEND:
    837      1.15     skrll 	case PWR_STANDBY:
    838      1.46  jmcneill 		pci_conf_capture(pc, tag, &sc->sc_pciconf);
    839      1.15     skrll 		break;
    840      1.15     skrll 	case PWR_RESUME:
    841      1.46  jmcneill 		pci_conf_restore(pc, tag, &sc->sc_pciconf);
    842      1.15     skrll 		break;
    843      1.15     skrll 	case PWR_SOFTSUSPEND:
    844      1.15     skrll 	case PWR_SOFTSTANDBY:
    845      1.46  jmcneill 		iwi_suspend(sc);
    846      1.46  jmcneill 		break;
    847      1.15     skrll 	case PWR_SOFTRESUME:
    848      1.46  jmcneill 		iwi_resume(sc);
    849      1.15     skrll 		break;
    850      1.15     skrll 	}
    851      1.15     skrll 	splx(s);
    852      1.15     skrll }
    853      1.15     skrll 
    854      1.38     skrll static struct ieee80211_node *
    855      1.58  christos iwi_node_alloc(struct ieee80211_node_table *nt)
    856      1.38     skrll {
    857      1.38     skrll 	struct iwi_node *in;
    858      1.38     skrll 
    859      1.38     skrll 	in = malloc(sizeof (struct iwi_node), M_80211_NODE, M_NOWAIT | M_ZERO);
    860      1.38     skrll 	if (in == NULL)
    861      1.38     skrll 		return NULL;
    862      1.38     skrll 
    863      1.38     skrll 	in->in_station = -1;
    864      1.38     skrll 
    865      1.38     skrll 	return &in->in_node;
    866      1.38     skrll }
    867      1.38     skrll 
    868      1.38     skrll static int
    869      1.38     skrll iwi_alloc_unr(struct iwi_softc *sc)
    870      1.38     skrll {
    871      1.38     skrll 	int i;
    872      1.38     skrll 
    873      1.38     skrll 	for (i = 0; i < IWI_MAX_IBSSNODE - 1; i++)
    874      1.38     skrll 		if ((sc->sc_unr & (1 << i)) == 0) {
    875      1.38     skrll 			sc->sc_unr |= 1 << i;
    876      1.38     skrll 			return i;
    877      1.38     skrll 		}
    878      1.38     skrll 
    879      1.38     skrll 	return -1;
    880      1.38     skrll }
    881      1.38     skrll 
    882      1.38     skrll static void
    883      1.38     skrll iwi_free_unr(struct iwi_softc *sc, int r)
    884      1.38     skrll {
    885      1.38     skrll 
    886      1.38     skrll 	sc->sc_unr &= 1 << r;
    887      1.38     skrll }
    888      1.38     skrll 
    889      1.38     skrll static void
    890      1.38     skrll iwi_node_free(struct ieee80211_node *ni)
    891      1.38     skrll {
    892      1.38     skrll 	struct ieee80211com *ic = ni->ni_ic;
    893      1.38     skrll 	struct iwi_softc *sc = ic->ic_ifp->if_softc;
    894      1.38     skrll 	struct iwi_node *in = (struct iwi_node *)ni;
    895      1.38     skrll 
    896      1.38     skrll 	if (in->in_station != -1)
    897      1.38     skrll 		iwi_free_unr(sc, in->in_station);
    898      1.38     skrll 
    899      1.38     skrll 	sc->sc_node_free(ni);
    900      1.38     skrll }
    901      1.38     skrll 
    902       1.1     skrll static int
    903       1.1     skrll iwi_media_change(struct ifnet *ifp)
    904       1.1     skrll {
    905       1.1     skrll 	int error;
    906       1.1     skrll 
    907       1.1     skrll 	error = ieee80211_media_change(ifp);
    908       1.1     skrll 	if (error != ENETRESET)
    909       1.1     skrll 		return error;
    910       1.1     skrll 
    911       1.1     skrll 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
    912       1.1     skrll 		iwi_init(ifp);
    913       1.1     skrll 
    914       1.1     skrll 	return 0;
    915       1.1     skrll }
    916       1.1     skrll 
    917      1.59     skrll /*
    918      1.59     skrll  * Convert h/w rate code to IEEE rate code.
    919      1.59     skrll  */
    920      1.59     skrll static int
    921      1.59     skrll iwi_cvtrate(int iwirate)
    922      1.59     skrll {
    923      1.59     skrll 	switch (iwirate) {
    924      1.59     skrll 	case IWI_RATE_DS1:	return 2;
    925      1.59     skrll 	case IWI_RATE_DS2:	return 4;
    926      1.59     skrll 	case IWI_RATE_DS5:	return 11;
    927      1.59     skrll 	case IWI_RATE_DS11:	return 22;
    928      1.59     skrll 	case IWI_RATE_OFDM6:	return 12;
    929      1.59     skrll 	case IWI_RATE_OFDM9:	return 18;
    930      1.59     skrll 	case IWI_RATE_OFDM12:	return 24;
    931      1.59     skrll 	case IWI_RATE_OFDM18:	return 36;
    932      1.59     skrll 	case IWI_RATE_OFDM24:	return 48;
    933      1.59     skrll 	case IWI_RATE_OFDM36:	return 72;
    934      1.59     skrll 	case IWI_RATE_OFDM48:	return 96;
    935      1.59     skrll 	case IWI_RATE_OFDM54:	return 108;
    936      1.59     skrll 	}
    937      1.59     skrll 	return 0;
    938      1.59     skrll }
    939      1.59     skrll 
    940      1.20     skrll /*
    941      1.38     skrll  * The firmware automatically adapts the transmit speed.  We report its current
    942      1.38     skrll  * value here.
    943      1.20     skrll  */
    944       1.1     skrll static void
    945       1.1     skrll iwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
    946       1.1     skrll {
    947       1.1     skrll 	struct iwi_softc *sc = ifp->if_softc;
    948       1.1     skrll 	struct ieee80211com *ic = &sc->sc_ic;
    949      1.59     skrll 	int rate;
    950       1.1     skrll 
    951       1.1     skrll 	imr->ifm_status = IFM_AVALID;
    952       1.1     skrll 	imr->ifm_active = IFM_IEEE80211;
    953       1.1     skrll 	if (ic->ic_state == IEEE80211_S_RUN)
    954       1.1     skrll 		imr->ifm_status |= IFM_ACTIVE;
    955       1.1     skrll 
    956       1.1     skrll 	/* read current transmission rate from adapter */
    957      1.59     skrll 	rate = iwi_cvtrate(CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE));
    958      1.59     skrll 	imr->ifm_active |= ieee80211_rate2media(ic, rate, ic->ic_curmode);
    959       1.1     skrll 
    960       1.1     skrll 	switch (ic->ic_opmode) {
    961       1.1     skrll 	case IEEE80211_M_STA:
    962       1.1     skrll 		break;
    963       1.1     skrll 
    964       1.1     skrll 	case IEEE80211_M_IBSS:
    965       1.1     skrll 		imr->ifm_active |= IFM_IEEE80211_ADHOC;
    966       1.1     skrll 		break;
    967       1.1     skrll 
    968       1.1     skrll 	case IEEE80211_M_MONITOR:
    969       1.1     skrll 		imr->ifm_active |= IFM_IEEE80211_MONITOR;
    970       1.1     skrll 		break;
    971       1.1     skrll 
    972       1.1     skrll 	case IEEE80211_M_AHDEMO:
    973       1.1     skrll 	case IEEE80211_M_HOSTAP:
    974       1.1     skrll 		/* should not get there */
    975       1.1     skrll 		break;
    976       1.1     skrll 	}
    977       1.1     skrll }
    978       1.1     skrll 
    979       1.1     skrll static int
    980       1.1     skrll iwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
    981       1.1     skrll {
    982       1.9    dyoung 	struct iwi_softc *sc = ic->ic_ifp->if_softc;
    983       1.1     skrll 
    984      1.59     skrll 	DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__,
    985      1.59     skrll 	    ieee80211_state_name[ic->ic_state],
    986      1.59     skrll 	    ieee80211_state_name[nstate], sc->flags));
    987      1.59     skrll 
    988       1.1     skrll 	switch (nstate) {
    989       1.1     skrll 	case IEEE80211_S_SCAN:
    990      1.12  christos 		if (sc->flags & IWI_FLAG_SCANNING)
    991      1.12  christos 			break;
    992      1.12  christos 
    993      1.12  christos 		ieee80211_node_table_reset(&ic->ic_scan);
    994      1.12  christos 		ic->ic_flags |= IEEE80211_F_SCAN | IEEE80211_F_ASCAN;
    995      1.12  christos 		sc->flags |= IWI_FLAG_SCANNING;
    996      1.42    rpaulo 		/* blink the led while scanning */
    997      1.42    rpaulo 		iwi_led_set(sc, IWI_LED_ASSOCIATED, 1);
    998       1.1     skrll 		iwi_scan(sc);
    999       1.1     skrll 		break;
   1000       1.1     skrll 
   1001       1.1     skrll 	case IEEE80211_S_AUTH:
   1002       1.1     skrll 		iwi_auth_and_assoc(sc);
   1003       1.1     skrll 		break;
   1004       1.1     skrll 
   1005       1.1     skrll 	case IEEE80211_S_RUN:
   1006       1.1     skrll 		if (ic->ic_opmode == IEEE80211_M_IBSS)
   1007       1.1     skrll 			ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
   1008       1.8    sekiya 		else if (ic->ic_opmode == IEEE80211_M_MONITOR)
   1009       1.8    sekiya 			iwi_set_chan(sc, ic->ic_ibss_chan);
   1010      1.12  christos 
   1011      1.12  christos 		return (*sc->sc_newstate)(ic, nstate,
   1012      1.12  christos 		    IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
   1013       1.1     skrll 
   1014       1.1     skrll 	case IEEE80211_S_ASSOC:
   1015      1.42    rpaulo 		iwi_led_set(sc, IWI_LED_ASSOCIATED, 0);
   1016      1.12  christos 		break;
   1017      1.12  christos 
   1018       1.1     skrll 	case IEEE80211_S_INIT:
   1019      1.12  christos 		sc->flags &= ~IWI_FLAG_SCANNING;
   1020      1.19     skrll 		return (*sc->sc_newstate)(ic, nstate, arg);
   1021       1.1     skrll 	}
   1022       1.1     skrll 
   1023       1.1     skrll 	ic->ic_state = nstate;
   1024       1.1     skrll 	return 0;
   1025       1.1     skrll }
   1026       1.1     skrll 
   1027       1.1     skrll /*
   1028      1.38     skrll  * WME parameters coming from IEEE 802.11e specification.  These values are
   1029      1.38     skrll  * already declared in ieee80211_proto.c, but they are static so they can't
   1030      1.38     skrll  * be reused here.
   1031      1.38     skrll  */
   1032      1.38     skrll static const struct wmeParams iwi_wme_cck_params[WME_NUM_AC] = {
   1033      1.55  christos 	{ 0, 3, 5,  7,   0, 0, },	/* WME_AC_BE */
   1034      1.55  christos 	{ 0, 3, 5, 10,   0, 0, },	/* WME_AC_BK */
   1035      1.55  christos 	{ 0, 2, 4,  5, 188, 0, },	/* WME_AC_VI */
   1036      1.55  christos 	{ 0, 2, 3,  4, 102, 0, },	/* WME_AC_VO */
   1037      1.38     skrll };
   1038      1.38     skrll 
   1039      1.38     skrll static const struct wmeParams iwi_wme_ofdm_params[WME_NUM_AC] = {
   1040      1.55  christos 	{ 0, 3, 4,  6,   0, 0, },	/* WME_AC_BE */
   1041      1.55  christos 	{ 0, 3, 4, 10,   0, 0, },	/* WME_AC_BK */
   1042      1.55  christos 	{ 0, 2, 3,  4,  94, 0, },	/* WME_AC_VI */
   1043      1.55  christos 	{ 0, 2, 2,  3,  47, 0, },	/* WME_AC_VO */
   1044      1.38     skrll };
   1045      1.38     skrll 
   1046      1.38     skrll static int
   1047      1.38     skrll iwi_wme_update(struct ieee80211com *ic)
   1048      1.38     skrll {
   1049      1.38     skrll #define IWI_EXP2(v)	htole16((1 << (v)) - 1)
   1050      1.38     skrll #define IWI_USEC(v)	htole16(IEEE80211_TXOP_TO_US(v))
   1051      1.38     skrll 	struct iwi_softc *sc = ic->ic_ifp->if_softc;
   1052      1.38     skrll 	struct iwi_wme_params wme[3];
   1053      1.38     skrll 	const struct wmeParams *wmep;
   1054      1.38     skrll 	int ac;
   1055      1.38     skrll 
   1056      1.38     skrll 	/*
   1057      1.38     skrll 	 * We shall not override firmware default WME values if WME is not
   1058      1.38     skrll 	 * actually enabled.
   1059      1.38     skrll 	 */
   1060      1.38     skrll 	if (!(ic->ic_flags & IEEE80211_F_WME))
   1061      1.38     skrll 		return 0;
   1062      1.38     skrll 
   1063      1.38     skrll 	for (ac = 0; ac < WME_NUM_AC; ac++) {
   1064      1.38     skrll 		/* set WME values for current operating mode */
   1065      1.38     skrll 		wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
   1066      1.38     skrll 		wme[0].aifsn[ac] = wmep->wmep_aifsn;
   1067      1.38     skrll 		wme[0].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
   1068      1.38     skrll 		wme[0].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
   1069      1.38     skrll 		wme[0].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
   1070      1.38     skrll 		wme[0].acm[ac]   = wmep->wmep_acm;
   1071      1.38     skrll 
   1072      1.38     skrll 		/* set WME values for CCK modulation */
   1073      1.38     skrll 		wmep = &iwi_wme_cck_params[ac];
   1074      1.38     skrll 		wme[1].aifsn[ac] = wmep->wmep_aifsn;
   1075      1.38     skrll 		wme[1].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
   1076      1.38     skrll 		wme[1].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
   1077      1.38     skrll 		wme[1].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
   1078      1.38     skrll 		wme[1].acm[ac]   = wmep->wmep_acm;
   1079      1.38     skrll 
   1080      1.38     skrll 		/* set WME values for OFDM modulation */
   1081      1.38     skrll 		wmep = &iwi_wme_ofdm_params[ac];
   1082      1.38     skrll 		wme[2].aifsn[ac] = wmep->wmep_aifsn;
   1083      1.38     skrll 		wme[2].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
   1084      1.38     skrll 		wme[2].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
   1085      1.38     skrll 		wme[2].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
   1086      1.38     skrll 		wme[2].acm[ac]   = wmep->wmep_acm;
   1087      1.38     skrll 	}
   1088      1.38     skrll 
   1089      1.38     skrll 	DPRINTF(("Setting WME parameters\n"));
   1090      1.38     skrll 	return iwi_cmd(sc, IWI_CMD_SET_WME_PARAMS, wme, sizeof wme, 1);
   1091      1.38     skrll #undef IWI_USEC
   1092      1.38     skrll #undef IWI_EXP2
   1093      1.38     skrll }
   1094      1.38     skrll 
   1095      1.38     skrll /*
   1096       1.1     skrll  * Read 16 bits at address 'addr' from the serial EEPROM.
   1097       1.1     skrll  */
   1098      1.24     skrll static uint16_t
   1099      1.24     skrll iwi_read_prom_word(struct iwi_softc *sc, uint8_t addr)
   1100       1.1     skrll {
   1101      1.24     skrll 	uint32_t tmp;
   1102      1.24     skrll 	uint16_t val;
   1103       1.1     skrll 	int n;
   1104       1.1     skrll 
   1105       1.1     skrll 	/* Clock C once before the first command */
   1106       1.1     skrll 	IWI_EEPROM_CTL(sc, 0);
   1107       1.1     skrll 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
   1108       1.1     skrll 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
   1109       1.1     skrll 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
   1110       1.1     skrll 
   1111       1.1     skrll 	/* Write start bit (1) */
   1112       1.1     skrll 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
   1113       1.1     skrll 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
   1114       1.1     skrll 
   1115       1.1     skrll 	/* Write READ opcode (10) */
   1116       1.1     skrll 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
   1117       1.1     skrll 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
   1118       1.1     skrll 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
   1119       1.1     skrll 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
   1120       1.1     skrll 
   1121       1.1     skrll 	/* Write address A7-A0 */
   1122       1.1     skrll 	for (n = 7; n >= 0; n--) {
   1123       1.1     skrll 		IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
   1124       1.1     skrll 		    (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D));
   1125       1.1     skrll 		IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
   1126       1.1     skrll 		    (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D) | IWI_EEPROM_C);
   1127       1.1     skrll 	}
   1128       1.1     skrll 
   1129       1.1     skrll 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
   1130       1.1     skrll 
   1131       1.1     skrll 	/* Read data Q15-Q0 */
   1132       1.1     skrll 	val = 0;
   1133       1.1     skrll 	for (n = 15; n >= 0; n--) {
   1134       1.1     skrll 		IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
   1135       1.1     skrll 		IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
   1136       1.1     skrll 		tmp = MEM_READ_4(sc, IWI_MEM_EEPROM_CTL);
   1137       1.1     skrll 		val |= ((tmp & IWI_EEPROM_Q) >> IWI_EEPROM_SHIFT_Q) << n;
   1138       1.1     skrll 	}
   1139       1.1     skrll 
   1140       1.1     skrll 	IWI_EEPROM_CTL(sc, 0);
   1141       1.1     skrll 
   1142       1.1     skrll 	/* Clear Chip Select and clock C */
   1143       1.1     skrll 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
   1144       1.1     skrll 	IWI_EEPROM_CTL(sc, 0);
   1145       1.1     skrll 	IWI_EEPROM_CTL(sc, IWI_EEPROM_C);
   1146       1.1     skrll 
   1147      1.33     skrll 	return val;
   1148       1.1     skrll }
   1149       1.1     skrll 
   1150       1.1     skrll /*
   1151       1.1     skrll  * XXX: Hack to set the current channel to the value advertised in beacons or
   1152       1.1     skrll  * probe responses. Only used during AP detection.
   1153       1.1     skrll  */
   1154       1.1     skrll static void
   1155       1.1     skrll iwi_fix_channel(struct ieee80211com *ic, struct mbuf *m)
   1156       1.1     skrll {
   1157       1.1     skrll 	struct ieee80211_frame *wh;
   1158      1.24     skrll 	uint8_t subtype;
   1159      1.24     skrll 	uint8_t *frm, *efrm;
   1160       1.1     skrll 
   1161       1.1     skrll 	wh = mtod(m, struct ieee80211_frame *);
   1162       1.1     skrll 
   1163       1.1     skrll 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
   1164       1.1     skrll 		return;
   1165       1.1     skrll 
   1166       1.1     skrll 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
   1167       1.1     skrll 
   1168       1.1     skrll 	if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
   1169       1.1     skrll 	    subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
   1170       1.1     skrll 		return;
   1171       1.1     skrll 
   1172      1.24     skrll 	frm = (uint8_t *)(wh + 1);
   1173      1.24     skrll 	efrm = mtod(m, uint8_t *) + m->m_len;
   1174       1.1     skrll 
   1175       1.1     skrll 	frm += 12;	/* skip tstamp, bintval and capinfo fields */
   1176       1.1     skrll 	while (frm < efrm) {
   1177       1.1     skrll 		if (*frm == IEEE80211_ELEMID_DSPARMS)
   1178       1.1     skrll #if IEEE80211_CHAN_MAX < 255
   1179       1.1     skrll 		if (frm[2] <= IEEE80211_CHAN_MAX)
   1180       1.1     skrll #endif
   1181      1.38     skrll 			ic->ic_curchan = &ic->ic_channels[frm[2]];
   1182       1.1     skrll 
   1183       1.1     skrll 		frm += frm[1] + 2;
   1184       1.1     skrll 	}
   1185       1.1     skrll }
   1186       1.1     skrll 
   1187      1.31     joerg static struct mbuf *
   1188      1.31     joerg iwi_alloc_rx_buf(struct iwi_softc *sc)
   1189      1.31     joerg {
   1190      1.31     joerg 	struct mbuf *m;
   1191      1.31     joerg 
   1192      1.31     joerg 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1193      1.31     joerg 	if (m == NULL) {
   1194  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev, "could not allocate rx mbuf\n");
   1195      1.31     joerg 		return NULL;
   1196      1.31     joerg 	}
   1197      1.31     joerg 
   1198      1.31     joerg 	MCLGET(m, M_DONTWAIT);
   1199      1.31     joerg 	if (!(m->m_flags & M_EXT)) {
   1200  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev,
   1201  1.64.4.1    bouyer 		    "could not allocate rx mbuf cluster\n");
   1202      1.31     joerg 		m_freem(m);
   1203      1.31     joerg 		return NULL;
   1204      1.31     joerg 	}
   1205      1.31     joerg 
   1206      1.34       scw 	m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
   1207      1.31     joerg 	return m;
   1208      1.31     joerg }
   1209      1.31     joerg 
   1210       1.1     skrll static void
   1211      1.14     skrll iwi_frame_intr(struct iwi_softc *sc, struct iwi_rx_data *data, int i,
   1212       1.1     skrll     struct iwi_frame *frame)
   1213       1.1     skrll {
   1214       1.1     skrll 	struct ieee80211com *ic = &sc->sc_ic;
   1215      1.12  christos 	struct ifnet *ifp = ic->ic_ifp;
   1216      1.31     joerg 	struct mbuf *m, *m_new;
   1217      1.12  christos 	struct ieee80211_frame *wh;
   1218       1.1     skrll 	struct ieee80211_node *ni;
   1219       1.1     skrll 	int error;
   1220       1.1     skrll 
   1221      1.14     skrll 	DPRINTFN(5, ("received frame len=%u chan=%u rssi=%u\n",
   1222      1.14     skrll 	    le16toh(frame->len), frame->chan, frame->rssi_dbm));
   1223       1.1     skrll 
   1224      1.12  christos 	if (le16toh(frame->len) < sizeof (struct ieee80211_frame) ||
   1225       1.1     skrll 	    le16toh(frame->len) > MCLBYTES) {
   1226  1.64.4.1    bouyer 		DPRINTF(("%s: bad frame length\n", device_xname(sc->sc_dev)));
   1227      1.14     skrll 		ifp->if_ierrors++;
   1228      1.14     skrll 		return;
   1229       1.1     skrll 	}
   1230       1.1     skrll 
   1231      1.31     joerg 	/*
   1232      1.31     joerg 	 * Try to allocate a new mbuf for this ring element and
   1233      1.31     joerg 	 * load it before processing the current mbuf. If the ring
   1234      1.31     joerg 	 * element cannot be reloaded, drop the received packet
   1235      1.31     joerg 	 * and reuse the old mbuf. In the unlikely case that
   1236      1.31     joerg 	 * the old mbuf can't be reloaded either, explicitly panic.
   1237      1.31     joerg 	 *
   1238      1.31     joerg 	 * XXX Reorganize buffer by moving elements from the logical
   1239      1.31     joerg 	 * end of the ring to the front instead of dropping.
   1240      1.31     joerg 	 */
   1241      1.31     joerg 	if ((m_new = iwi_alloc_rx_buf(sc)) == NULL) {
   1242      1.31     joerg 		ifp->if_ierrors++;
   1243      1.31     joerg 		return;
   1244      1.31     joerg 	}
   1245      1.31     joerg 
   1246      1.14     skrll 	bus_dmamap_unload(sc->sc_dmat, data->map);
   1247       1.1     skrll 
   1248      1.34       scw 	error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m_new,
   1249      1.34       scw 	    BUS_DMA_READ | BUS_DMA_NOWAIT);
   1250      1.31     joerg 	if (error != 0) {
   1251  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev,
   1252  1.64.4.1    bouyer 		    "could not load rx buf DMA map\n");
   1253      1.31     joerg 		m_freem(m_new);
   1254      1.31     joerg 		ifp->if_ierrors++;
   1255      1.34       scw 		error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map,
   1256      1.34       scw 		    data->m, BUS_DMA_READ | BUS_DMA_NOWAIT);
   1257      1.31     joerg 		if (error)
   1258      1.31     joerg 			panic("%s: unable to remap rx buf",
   1259  1.64.4.1    bouyer 			    device_xname(sc->sc_dev));
   1260      1.31     joerg 		return;
   1261      1.31     joerg 	}
   1262      1.31     joerg 
   1263      1.31     joerg 	/*
   1264      1.31     joerg 	 * New mbuf successfully loaded, update RX ring and continue
   1265      1.31     joerg 	 * processing.
   1266      1.31     joerg 	 */
   1267      1.31     joerg 	m = data->m;
   1268      1.31     joerg 	data->m = m_new;
   1269      1.31     joerg 	CSR_WRITE_4(sc, IWI_CSR_RX_BASE + i * 4, data->map->dm_segs[0].ds_addr);
   1270      1.31     joerg 
   1271       1.1     skrll 	/* Finalize mbuf */
   1272       1.1     skrll 	m->m_pkthdr.rcvif = ifp;
   1273       1.1     skrll 	m->m_pkthdr.len = m->m_len = sizeof (struct iwi_hdr) +
   1274       1.1     skrll 	    sizeof (struct iwi_frame) + le16toh(frame->len);
   1275       1.1     skrll 
   1276       1.1     skrll 	m_adj(m, sizeof (struct iwi_hdr) + sizeof (struct iwi_frame));
   1277       1.1     skrll 
   1278      1.12  christos 	if (ic->ic_state == IEEE80211_S_SCAN)
   1279      1.12  christos 		iwi_fix_channel(ic, m);
   1280       1.1     skrll 
   1281       1.1     skrll #if NBPFILTER > 0
   1282       1.1     skrll 	if (sc->sc_drvbpf != NULL) {
   1283       1.1     skrll 		struct iwi_rx_radiotap_header *tap = &sc->sc_rxtap;
   1284      1.14     skrll 
   1285      1.12  christos 		tap->wr_flags = 0;
   1286      1.59     skrll 		tap->wr_rate = iwi_cvtrate(frame->rate);
   1287      1.12  christos 		tap->wr_chan_freq =
   1288      1.12  christos 		    htole16(ic->ic_channels[frame->chan].ic_freq);
   1289      1.12  christos 		tap->wr_chan_flags =
   1290      1.12  christos 		    htole16(ic->ic_channels[frame->chan].ic_flags);
   1291      1.12  christos 		tap->wr_antsignal = frame->signal;
   1292      1.12  christos 		tap->wr_antenna = frame->antenna;
   1293       1.1     skrll 
   1294      1.21     skrll 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
   1295       1.1     skrll 	}
   1296       1.1     skrll #endif
   1297      1.12  christos 	wh = mtod(m, struct ieee80211_frame *);
   1298      1.12  christos 	ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
   1299       1.5     perry 
   1300       1.1     skrll 	/* Send the frame to the upper layer */
   1301      1.12  christos 	ieee80211_input(ic, m, ni, frame->rssi_dbm, 0);
   1302       1.1     skrll 
   1303      1.12  christos 	/* node is no longer needed */
   1304       1.9    dyoung 	ieee80211_free_node(ni);
   1305       1.1     skrll }
   1306       1.1     skrll 
   1307       1.1     skrll static void
   1308      1.25     skrll iwi_notification_intr(struct iwi_softc *sc, struct iwi_notif *notif)
   1309       1.1     skrll {
   1310       1.1     skrll 	struct ieee80211com *ic = &sc->sc_ic;
   1311       1.1     skrll 	struct iwi_notif_scan_channel *chan;
   1312       1.1     skrll 	struct iwi_notif_scan_complete *scan;
   1313       1.1     skrll 	struct iwi_notif_authentication *auth;
   1314       1.1     skrll 	struct iwi_notif_association *assoc;
   1315      1.59     skrll 	struct iwi_notif_beacon_state *beacon;
   1316       1.1     skrll 
   1317       1.1     skrll 	switch (notif->type) {
   1318       1.1     skrll 	case IWI_NOTIF_TYPE_SCAN_CHANNEL:
   1319       1.1     skrll 		chan = (struct iwi_notif_scan_channel *)(notif + 1);
   1320       1.1     skrll 
   1321      1.59     skrll 		DPRINTFN(2, ("Scan of channel %u complete (%u)\n",
   1322      1.59     skrll 		    ic->ic_channels[chan->nchan].ic_freq, chan->nchan));
   1323       1.1     skrll 		break;
   1324       1.1     skrll 
   1325       1.1     skrll 	case IWI_NOTIF_TYPE_SCAN_COMPLETE:
   1326       1.1     skrll 		scan = (struct iwi_notif_scan_complete *)(notif + 1);
   1327       1.1     skrll 
   1328       1.1     skrll 		DPRINTFN(2, ("Scan completed (%u, %u)\n", scan->nchan,
   1329       1.1     skrll 		    scan->status));
   1330       1.1     skrll 
   1331       1.8    sekiya 		/* monitor mode uses scan to set the channel ... */
   1332      1.12  christos 		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
   1333      1.12  christos 			sc->flags &= ~IWI_FLAG_SCANNING;
   1334       1.8    sekiya 			ieee80211_end_scan(ic);
   1335      1.12  christos 		} else
   1336       1.8    sekiya 			iwi_set_chan(sc, ic->ic_ibss_chan);
   1337       1.1     skrll 		break;
   1338       1.1     skrll 
   1339       1.1     skrll 	case IWI_NOTIF_TYPE_AUTHENTICATION:
   1340       1.1     skrll 		auth = (struct iwi_notif_authentication *)(notif + 1);
   1341       1.1     skrll 
   1342       1.1     skrll 		DPRINTFN(2, ("Authentication (%u)\n", auth->state));
   1343       1.1     skrll 
   1344       1.1     skrll 		switch (auth->state) {
   1345      1.59     skrll 		case IWI_AUTH_SUCCESS:
   1346      1.38     skrll 			ieee80211_node_authorize(ic->ic_bss);
   1347       1.1     skrll 			ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1);
   1348       1.1     skrll 			break;
   1349       1.1     skrll 
   1350      1.59     skrll 		case IWI_AUTH_FAIL:
   1351       1.1     skrll 			break;
   1352       1.1     skrll 
   1353       1.1     skrll 		default:
   1354  1.64.4.1    bouyer 			aprint_error_dev(sc->sc_dev,
   1355  1.64.4.1    bouyer 			    "unknown authentication state %u\n", auth->state);
   1356       1.1     skrll 		}
   1357       1.1     skrll 		break;
   1358       1.1     skrll 
   1359       1.1     skrll 	case IWI_NOTIF_TYPE_ASSOCIATION:
   1360       1.1     skrll 		assoc = (struct iwi_notif_association *)(notif + 1);
   1361       1.1     skrll 
   1362       1.1     skrll 		DPRINTFN(2, ("Association (%u, %u)\n", assoc->state,
   1363       1.1     skrll 		    assoc->status));
   1364       1.1     skrll 
   1365       1.1     skrll 		switch (assoc->state) {
   1366      1.59     skrll 		case IWI_AUTH_SUCCESS:
   1367      1.12  christos 			/* re-association, do nothing */
   1368      1.12  christos 			break;
   1369      1.12  christos 
   1370      1.59     skrll 		case IWI_ASSOC_SUCCESS:
   1371       1.1     skrll 			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
   1372       1.1     skrll 			break;
   1373       1.1     skrll 
   1374      1.59     skrll 		case IWI_ASSOC_FAIL:
   1375      1.12  christos 			ieee80211_begin_scan(ic, 1);
   1376       1.1     skrll 			break;
   1377       1.1     skrll 
   1378       1.1     skrll 		default:
   1379  1.64.4.1    bouyer 			aprint_error_dev(sc->sc_dev,
   1380  1.64.4.1    bouyer 			    "unknown association state %u\n", assoc->state);
   1381       1.1     skrll 		}
   1382       1.1     skrll 		break;
   1383       1.1     skrll 
   1384      1.59     skrll 	case IWI_NOTIF_TYPE_BEACON:
   1385      1.59     skrll 		beacon = (struct iwi_notif_beacon_state *)(notif + 1);
   1386      1.59     skrll 
   1387      1.59     skrll 		if (beacon->state == IWI_BEACON_MISS) {
   1388  1.64.4.1    bouyer 			DPRINTFN(5, ("%s: %u beacon(s) missed\n",
   1389  1.64.4.1    bouyer 			    device_xname(sc->sc_dev), le32toh(beacon->number)));
   1390      1.59     skrll 		}
   1391      1.59     skrll 		break;
   1392      1.59     skrll 
   1393      1.59     skrll 	case IWI_NOTIF_TYPE_FRAG_LENGTH:
   1394      1.59     skrll 	case IWI_NOTIF_TYPE_LINK_QUALITY:
   1395      1.59     skrll 	case IWI_NOTIF_TYPE_TGI_TX_KEY:
   1396       1.1     skrll 	case IWI_NOTIF_TYPE_CALIBRATION:
   1397       1.1     skrll 	case IWI_NOTIF_TYPE_NOISE:
   1398       1.1     skrll 		DPRINTFN(5, ("Notification (%u)\n", notif->type));
   1399       1.1     skrll 		break;
   1400       1.1     skrll 
   1401       1.1     skrll 	default:
   1402      1.59     skrll 		DPRINTF(("%s: unknown notification type %u flags 0x%x len %d\n",
   1403  1.64.4.1    bouyer 		    device_xname(sc->sc_dev), notif->type, notif->flags,
   1404  1.64.4.1    bouyer 		    le16toh(notif->len)));
   1405       1.1     skrll 	}
   1406       1.1     skrll }
   1407       1.1     skrll 
   1408       1.1     skrll static void
   1409      1.41     skrll iwi_cmd_intr(struct iwi_softc *sc)
   1410      1.41     skrll {
   1411      1.41     skrll 	uint32_t hw;
   1412      1.41     skrll 
   1413      1.41     skrll 	hw = CSR_READ_4(sc, IWI_CSR_CMD_RIDX);
   1414      1.41     skrll 
   1415      1.59     skrll 	bus_dmamap_sync(sc->sc_dmat, sc->cmdq.desc_map,
   1416      1.59     skrll 	    sc->cmdq.next * IWI_CMD_DESC_SIZE, IWI_CMD_DESC_SIZE,
   1417      1.59     skrll 	    BUS_DMASYNC_POSTWRITE);
   1418      1.59     skrll 
   1419      1.59     skrll 	wakeup(&sc->cmdq.desc[sc->cmdq.next]);
   1420      1.59     skrll 
   1421      1.59     skrll 	sc->cmdq.next = (sc->cmdq.next + 1) % sc->cmdq.count;
   1422      1.41     skrll 
   1423      1.59     skrll 	if (--sc->cmdq.queued > 0) {
   1424      1.59     skrll 		CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, (sc->cmdq.next + 1) % sc->cmdq.count);
   1425      1.41     skrll 	}
   1426      1.41     skrll }
   1427      1.41     skrll 
   1428      1.41     skrll static void
   1429       1.1     skrll iwi_rx_intr(struct iwi_softc *sc)
   1430       1.1     skrll {
   1431      1.14     skrll 	struct iwi_rx_data *data;
   1432       1.1     skrll 	struct iwi_hdr *hdr;
   1433      1.14     skrll 	uint32_t hw;
   1434       1.1     skrll 
   1435      1.14     skrll 	hw = CSR_READ_4(sc, IWI_CSR_RX_RIDX);
   1436       1.1     skrll 
   1437      1.14     skrll 	for (; sc->rxq.cur != hw;) {
   1438      1.14     skrll 		data = &sc->rxq.data[sc->rxq.cur];
   1439       1.1     skrll 
   1440      1.14     skrll 		bus_dmamap_sync(sc->sc_dmat, data->map, 0,
   1441      1.34       scw 		    data->map->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1442       1.1     skrll 
   1443      1.14     skrll 		hdr = mtod(data->m, struct iwi_hdr *);
   1444       1.1     skrll 
   1445       1.1     skrll 		switch (hdr->type) {
   1446       1.1     skrll 		case IWI_HDR_TYPE_FRAME:
   1447      1.14     skrll 			iwi_frame_intr(sc, data, sc->rxq.cur,
   1448       1.1     skrll 			    (struct iwi_frame *)(hdr + 1));
   1449       1.1     skrll 			break;
   1450       1.1     skrll 
   1451       1.1     skrll 		case IWI_HDR_TYPE_NOTIF:
   1452      1.25     skrll 			iwi_notification_intr(sc,
   1453       1.1     skrll 			    (struct iwi_notif *)(hdr + 1));
   1454       1.1     skrll 			break;
   1455       1.1     skrll 
   1456       1.1     skrll 		default:
   1457  1.64.4.1    bouyer 			aprint_error_dev(sc->sc_dev, "unknown hdr type %u\n",
   1458  1.64.4.1    bouyer 			    hdr->type);
   1459       1.1     skrll 		}
   1460      1.14     skrll 
   1461      1.41     skrll 		bus_dmamap_sync(sc->sc_dmat, data->map, 0,
   1462      1.41     skrll 		    data->map->dm_mapsize, BUS_DMASYNC_PREREAD);
   1463      1.41     skrll 
   1464      1.14     skrll 		DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur));
   1465      1.14     skrll 
   1466      1.14     skrll 		sc->rxq.cur = (sc->rxq.cur + 1) % sc->rxq.count;
   1467       1.1     skrll 	}
   1468       1.1     skrll 
   1469       1.1     skrll 	/* Tell the firmware what we have processed */
   1470      1.14     skrll 	hw = (hw == 0) ? sc->rxq.count - 1 : hw - 1;
   1471      1.14     skrll 	CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, hw);
   1472       1.1     skrll }
   1473       1.1     skrll 
   1474       1.1     skrll static void
   1475      1.38     skrll iwi_tx_intr(struct iwi_softc *sc, struct iwi_tx_ring *txq)
   1476       1.1     skrll {
   1477       1.9    dyoung 	struct ifnet *ifp = &sc->sc_if;
   1478      1.14     skrll 	struct iwi_tx_data *data;
   1479      1.24     skrll 	uint32_t hw;
   1480       1.1     skrll 
   1481      1.38     skrll 	hw = CSR_READ_4(sc, txq->csr_ridx);
   1482       1.1     skrll 
   1483      1.38     skrll 	for (; txq->next != hw;) {
   1484      1.38     skrll 		data = &txq->data[txq->next];
   1485       1.1     skrll 
   1486      1.14     skrll 		bus_dmamap_sync(sc->sc_dmat, data->map, 0,
   1487      1.34       scw 		    data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1488      1.14     skrll 		bus_dmamap_unload(sc->sc_dmat, data->map);
   1489      1.14     skrll 		m_freem(data->m);
   1490      1.14     skrll 		data->m = NULL;
   1491      1.14     skrll 		ieee80211_free_node(data->ni);
   1492      1.14     skrll 		data->ni = NULL;
   1493       1.1     skrll 
   1494      1.38     skrll 		DPRINTFN(15, ("tx done idx=%u\n", txq->next));
   1495       1.1     skrll 
   1496      1.12  christos 		ifp->if_opackets++;
   1497       1.1     skrll 
   1498      1.38     skrll 		txq->queued--;
   1499      1.38     skrll 		txq->next = (txq->next + 1) % txq->count;
   1500       1.1     skrll 	}
   1501       1.1     skrll 
   1502      1.14     skrll 	sc->sc_tx_timer = 0;
   1503      1.14     skrll 	ifp->if_flags &= ~IFF_OACTIVE;
   1504       1.1     skrll 
   1505       1.1     skrll 	/* Call start() since some buffer descriptors have been released */
   1506       1.1     skrll 	(*ifp->if_start)(ifp);
   1507       1.1     skrll }
   1508       1.1     skrll 
   1509       1.1     skrll static int
   1510       1.1     skrll iwi_intr(void *arg)
   1511       1.1     skrll {
   1512       1.1     skrll 	struct iwi_softc *sc = arg;
   1513      1.24     skrll 	uint32_t r;
   1514       1.1     skrll 
   1515       1.1     skrll 	if ((r = CSR_READ_4(sc, IWI_CSR_INTR)) == 0 || r == 0xffffffff)
   1516       1.1     skrll 		return 0;
   1517       1.1     skrll 
   1518      1.29     skrll 	/* Acknowledge interrupts */
   1519      1.29     skrll 	CSR_WRITE_4(sc, IWI_CSR_INTR, r);
   1520       1.1     skrll 
   1521      1.59     skrll 	if (r & IWI_INTR_FATAL_ERROR) {
   1522  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev, "fatal error\n");
   1523      1.13     skrll 		sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP;
   1524       1.9    dyoung 		iwi_stop(&sc->sc_if, 1);
   1525      1.41     skrll 		return (1);
   1526       1.1     skrll 	}
   1527       1.1     skrll 
   1528       1.1     skrll 	if (r & IWI_INTR_FW_INITED) {
   1529       1.1     skrll 		if (!(r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR)))
   1530       1.1     skrll 			wakeup(sc);
   1531       1.1     skrll 	}
   1532       1.1     skrll 
   1533       1.1     skrll 	if (r & IWI_INTR_RADIO_OFF) {
   1534       1.1     skrll 		DPRINTF(("radio transmitter off\n"));
   1535      1.13     skrll 		sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP;
   1536       1.9    dyoung 		iwi_stop(&sc->sc_if, 1);
   1537      1.41     skrll 		return (1);
   1538       1.1     skrll 	}
   1539       1.1     skrll 
   1540      1.14     skrll 	if (r & IWI_INTR_CMD_DONE)
   1541      1.41     skrll 		iwi_cmd_intr(sc);
   1542       1.1     skrll 
   1543      1.14     skrll 	if (r & IWI_INTR_TX1_DONE)
   1544      1.38     skrll 		iwi_tx_intr(sc, &sc->txq[0]);
   1545      1.38     skrll 
   1546      1.38     skrll 	if (r & IWI_INTR_TX2_DONE)
   1547      1.38     skrll 		iwi_tx_intr(sc, &sc->txq[1]);
   1548      1.38     skrll 
   1549      1.38     skrll 	if (r & IWI_INTR_TX3_DONE)
   1550      1.38     skrll 		iwi_tx_intr(sc, &sc->txq[2]);
   1551      1.38     skrll 
   1552      1.38     skrll 	if (r & IWI_INTR_TX4_DONE)
   1553      1.38     skrll 		iwi_tx_intr(sc, &sc->txq[3]);
   1554      1.38     skrll 
   1555      1.38     skrll 	if (r & IWI_INTR_RX_DONE)
   1556      1.38     skrll 		iwi_rx_intr(sc);
   1557       1.1     skrll 
   1558  1.64.4.1    bouyer 	if (r & IWI_INTR_PARITY_ERROR)
   1559  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev, "parity error\n");
   1560      1.59     skrll 
   1561       1.1     skrll 	return 1;
   1562       1.1     skrll }
   1563       1.1     skrll 
   1564       1.1     skrll static int
   1565      1.24     skrll iwi_cmd(struct iwi_softc *sc, uint8_t type, void *data, uint8_t len,
   1566       1.1     skrll     int async)
   1567       1.1     skrll {
   1568       1.1     skrll 	struct iwi_cmd_desc *desc;
   1569       1.1     skrll 
   1570      1.14     skrll 	desc = &sc->cmdq.desc[sc->cmdq.cur];
   1571       1.1     skrll 
   1572       1.1     skrll 	desc->hdr.type = IWI_HDR_TYPE_COMMAND;
   1573       1.1     skrll 	desc->hdr.flags = IWI_HDR_FLAG_IRQ;
   1574       1.1     skrll 	desc->type = type;
   1575       1.1     skrll 	desc->len = len;
   1576       1.1     skrll 	memcpy(desc->data, data, len);
   1577       1.1     skrll 
   1578      1.14     skrll 	bus_dmamap_sync(sc->sc_dmat, sc->cmdq.desc_map,
   1579      1.25     skrll 	    sc->cmdq.cur * IWI_CMD_DESC_SIZE,
   1580      1.25     skrll 	    IWI_CMD_DESC_SIZE, BUS_DMASYNC_PREWRITE);
   1581       1.1     skrll 
   1582      1.59     skrll 	DPRINTFN(2, ("sending command idx=%u type=%u len=%u async=%d\n",
   1583      1.59     skrll 	    sc->cmdq.cur, type, len, async));
   1584      1.14     skrll 
   1585      1.14     skrll 	sc->cmdq.cur = (sc->cmdq.cur + 1) % sc->cmdq.count;
   1586      1.59     skrll 
   1587      1.59     skrll 	if (++sc->cmdq.queued == 1)
   1588      1.59     skrll 		CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
   1589       1.1     skrll 
   1590      1.41     skrll 	return async ? 0 : tsleep(desc, 0, "iwicmd", hz);
   1591       1.1     skrll }
   1592       1.1     skrll 
   1593      1.38     skrll static void
   1594      1.38     skrll iwi_write_ibssnode(struct iwi_softc *sc, const struct iwi_node *in)
   1595      1.38     skrll {
   1596      1.38     skrll 	struct iwi_ibssnode node;
   1597      1.38     skrll 
   1598      1.38     skrll 	/* write node information into NIC memory */
   1599      1.38     skrll 	memset(&node, 0, sizeof node);
   1600      1.38     skrll 	IEEE80211_ADDR_COPY(node.bssid, in->in_node.ni_macaddr);
   1601      1.38     skrll 
   1602      1.38     skrll 	CSR_WRITE_REGION_1(sc,
   1603      1.38     skrll 	    IWI_CSR_NODE_BASE + in->in_station * sizeof node,
   1604      1.38     skrll 	    (uint8_t *)&node, sizeof node);
   1605      1.38     skrll }
   1606      1.38     skrll 
   1607       1.1     skrll static int
   1608      1.38     skrll iwi_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni,
   1609      1.38     skrll     int ac)
   1610       1.1     skrll {
   1611       1.1     skrll 	struct iwi_softc *sc = ifp->if_softc;
   1612       1.1     skrll 	struct ieee80211com *ic = &sc->sc_ic;
   1613      1.38     skrll 	struct iwi_node *in = (struct iwi_node *)ni;
   1614      1.38     skrll 	struct ieee80211_frame *wh;
   1615      1.12  christos 	struct ieee80211_key *k;
   1616      1.38     skrll 	const struct chanAccParams *cap;
   1617      1.38     skrll 	struct iwi_tx_ring *txq = &sc->txq[ac];
   1618      1.14     skrll 	struct iwi_tx_data *data;
   1619       1.1     skrll 	struct iwi_tx_desc *desc;
   1620       1.1     skrll 	struct mbuf *mnew;
   1621      1.38     skrll 	int error, hdrlen, i, noack = 0;
   1622      1.38     skrll 
   1623      1.38     skrll 	wh = mtod(m0, struct ieee80211_frame *);
   1624      1.38     skrll 
   1625      1.38     skrll 	if (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_QOS) {
   1626      1.38     skrll 		hdrlen = sizeof (struct ieee80211_qosframe);
   1627      1.38     skrll 		cap = &ic->ic_wme.wme_chanParams;
   1628      1.38     skrll 		noack = cap->cap_wmeParams[ac].wmep_noackPolicy;
   1629      1.38     skrll 	} else
   1630      1.38     skrll 		hdrlen = sizeof (struct ieee80211_frame);
   1631      1.38     skrll 
   1632      1.38     skrll 	/*
   1633      1.38     skrll 	 * This is only used in IBSS mode where the firmware expect an index
   1634      1.38     skrll 	 * in a h/w table instead of a destination address.
   1635      1.38     skrll 	 */
   1636      1.38     skrll 	if (ic->ic_opmode == IEEE80211_M_IBSS && in->in_station == -1) {
   1637      1.38     skrll 		in->in_station = iwi_alloc_unr(sc);
   1638      1.38     skrll 
   1639      1.38     skrll 		if (in->in_station == -1) {	/* h/w table is full */
   1640      1.38     skrll 			m_freem(m0);
   1641      1.38     skrll 			ieee80211_free_node(ni);
   1642      1.38     skrll 			ifp->if_oerrors++;
   1643      1.38     skrll 			return 0;
   1644      1.38     skrll 		}
   1645      1.38     skrll 		iwi_write_ibssnode(sc, in);
   1646      1.38     skrll 	}
   1647       1.1     skrll 
   1648      1.38     skrll 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
   1649      1.12  christos 		k = ieee80211_crypto_encap(ic, ni, m0);
   1650      1.12  christos 		if (k == NULL) {
   1651      1.12  christos 			m_freem(m0);
   1652      1.12  christos 			return ENOBUFS;
   1653      1.12  christos 		}
   1654      1.38     skrll 
   1655      1.38     skrll 		/* packet header may have moved, reset our local pointer */
   1656      1.38     skrll 		wh = mtod(m0, struct ieee80211_frame *);
   1657      1.12  christos 	}
   1658      1.12  christos 
   1659       1.1     skrll #if NBPFILTER > 0
   1660       1.1     skrll 	if (sc->sc_drvbpf != NULL) {
   1661       1.1     skrll 		struct iwi_tx_radiotap_header *tap = &sc->sc_txtap;
   1662       1.1     skrll 
   1663       1.1     skrll 		tap->wt_flags = 0;
   1664      1.12  christos 		tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
   1665      1.12  christos 		tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
   1666       1.1     skrll 
   1667       1.1     skrll 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
   1668       1.1     skrll 	}
   1669       1.1     skrll #endif
   1670       1.1     skrll 
   1671      1.38     skrll 	data = &txq->data[txq->cur];
   1672      1.38     skrll 	desc = &txq->desc[txq->cur];
   1673       1.1     skrll 
   1674      1.38     skrll 	/* save and trim IEEE802.11 header */
   1675      1.61  christos 	m_copydata(m0, 0, hdrlen, (void *)&desc->wh);
   1676      1.38     skrll 	m_adj(m0, hdrlen);
   1677       1.1     skrll 
   1678      1.34       scw 	error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
   1679      1.34       scw 	    BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   1680       1.1     skrll 	if (error != 0 && error != EFBIG) {
   1681  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev, "could not map mbuf (error %d)\n",
   1682  1.64.4.1    bouyer 		    error);
   1683       1.1     skrll 		m_freem(m0);
   1684       1.1     skrll 		return error;
   1685       1.1     skrll 	}
   1686       1.1     skrll 	if (error != 0) {
   1687       1.1     skrll 		/* too many fragments, linearize */
   1688       1.1     skrll 
   1689       1.1     skrll 		MGETHDR(mnew, M_DONTWAIT, MT_DATA);
   1690       1.1     skrll 		if (mnew == NULL) {
   1691       1.1     skrll 			m_freem(m0);
   1692       1.1     skrll 			return ENOMEM;
   1693       1.1     skrll 		}
   1694       1.1     skrll 
   1695       1.1     skrll 		M_COPY_PKTHDR(mnew, m0);
   1696      1.34       scw 
   1697      1.34       scw 		/* If the data won't fit in the header, get a cluster */
   1698      1.34       scw 		if (m0->m_pkthdr.len > MHLEN) {
   1699      1.34       scw 			MCLGET(mnew, M_DONTWAIT);
   1700      1.34       scw 			if (!(mnew->m_flags & M_EXT)) {
   1701      1.34       scw 				m_freem(m0);
   1702      1.34       scw 				m_freem(mnew);
   1703      1.34       scw 				return ENOMEM;
   1704      1.34       scw 			}
   1705       1.1     skrll 		}
   1706      1.61  christos 		m_copydata(m0, 0, m0->m_pkthdr.len, mtod(mnew, void *));
   1707       1.1     skrll 		m_freem(m0);
   1708       1.1     skrll 		mnew->m_len = mnew->m_pkthdr.len;
   1709       1.1     skrll 		m0 = mnew;
   1710       1.1     skrll 
   1711      1.14     skrll 		error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
   1712      1.34       scw 		    BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   1713       1.1     skrll 		if (error != 0) {
   1714  1.64.4.1    bouyer 			aprint_error_dev(sc->sc_dev,
   1715  1.64.4.1    bouyer 			    "could not map mbuf (error %d)\n", error);
   1716       1.1     skrll 			m_freem(m0);
   1717       1.1     skrll 			return error;
   1718       1.1     skrll 		}
   1719       1.1     skrll 	}
   1720       1.1     skrll 
   1721      1.14     skrll 	data->m = m0;
   1722      1.14     skrll 	data->ni = ni;
   1723       1.1     skrll 
   1724       1.1     skrll 	desc->hdr.type = IWI_HDR_TYPE_DATA;
   1725       1.1     skrll 	desc->hdr.flags = IWI_HDR_FLAG_IRQ;
   1726      1.38     skrll 	desc->station =
   1727      1.38     skrll 	    (ic->ic_opmode == IEEE80211_M_IBSS) ? in->in_station : 0;
   1728       1.1     skrll 	desc->cmd = IWI_DATA_CMD_TX;
   1729       1.1     skrll 	desc->len = htole16(m0->m_pkthdr.len);
   1730       1.1     skrll 	desc->flags = 0;
   1731      1.38     skrll 	desc->xflags = 0;
   1732      1.38     skrll 
   1733      1.38     skrll 	if (!noack && !IEEE80211_IS_MULTICAST(desc->wh.i_addr1))
   1734       1.1     skrll 		desc->flags |= IWI_DATA_FLAG_NEED_ACK;
   1735       1.1     skrll 
   1736      1.12  christos #if 0
   1737       1.1     skrll 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
   1738      1.38     skrll 		desc->wh.i_fc[1] |= IEEE80211_FC1_WEP;
   1739      1.12  christos 		desc->wep_txkey = ic->ic_crypto.cs_def_txkey;
   1740       1.1     skrll 	} else
   1741      1.12  christos #endif
   1742       1.1     skrll 		desc->flags |= IWI_DATA_FLAG_NO_WEP;
   1743       1.1     skrll 
   1744       1.1     skrll 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
   1745       1.1     skrll 		desc->flags |= IWI_DATA_FLAG_SHPREAMBLE;
   1746       1.1     skrll 
   1747      1.38     skrll 	if (desc->wh.i_fc[0] & IEEE80211_FC0_SUBTYPE_QOS)
   1748      1.38     skrll 		desc->xflags |= IWI_DATA_XFLAG_QOS;
   1749      1.38     skrll 
   1750      1.59     skrll 	if (ic->ic_curmode == IEEE80211_MODE_11B)
   1751      1.59     skrll 		desc->xflags |= IWI_DATA_XFLAG_CCK;
   1752      1.59     skrll 
   1753      1.14     skrll 	desc->nseg = htole32(data->map->dm_nsegs);
   1754      1.14     skrll 	for (i = 0; i < data->map->dm_nsegs; i++) {
   1755      1.14     skrll 		desc->seg_addr[i] = htole32(data->map->dm_segs[i].ds_addr);
   1756      1.32       scw 		desc->seg_len[i]  = htole16(data->map->dm_segs[i].ds_len);
   1757       1.1     skrll 	}
   1758       1.1     skrll 
   1759      1.38     skrll 	bus_dmamap_sync(sc->sc_dmat, txq->desc_map,
   1760      1.38     skrll 	    txq->cur * IWI_TX_DESC_SIZE,
   1761      1.25     skrll 	    IWI_TX_DESC_SIZE, BUS_DMASYNC_PREWRITE);
   1762       1.1     skrll 
   1763      1.32       scw 	bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize,
   1764       1.1     skrll 	    BUS_DMASYNC_PREWRITE);
   1765       1.1     skrll 
   1766      1.38     skrll 	DPRINTFN(5, ("sending data frame txq=%u idx=%u len=%u nseg=%u\n",
   1767      1.38     skrll 	    ac, txq->cur, le16toh(desc->len), le32toh(desc->nseg)));
   1768       1.1     skrll 
   1769       1.1     skrll 	/* Inform firmware about this new packet */
   1770      1.38     skrll 	txq->queued++;
   1771      1.38     skrll 	txq->cur = (txq->cur + 1) % txq->count;
   1772      1.38     skrll 	CSR_WRITE_4(sc, txq->csr_widx, txq->cur);
   1773       1.1     skrll 
   1774       1.1     skrll 	return 0;
   1775       1.1     skrll }
   1776       1.1     skrll 
   1777       1.1     skrll static void
   1778       1.1     skrll iwi_start(struct ifnet *ifp)
   1779       1.1     skrll {
   1780       1.1     skrll 	struct iwi_softc *sc = ifp->if_softc;
   1781       1.1     skrll 	struct ieee80211com *ic = &sc->sc_ic;
   1782       1.1     skrll 	struct mbuf *m0;
   1783      1.12  christos 	struct ether_header *eh;
   1784       1.1     skrll 	struct ieee80211_node *ni;
   1785      1.38     skrll 	int ac;
   1786       1.1     skrll 
   1787       1.1     skrll 	if (ic->ic_state != IEEE80211_S_RUN)
   1788       1.1     skrll 		return;
   1789       1.1     skrll 
   1790       1.1     skrll 	for (;;) {
   1791       1.1     skrll 		IF_DEQUEUE(&ifp->if_snd, m0);
   1792       1.1     skrll 		if (m0 == NULL)
   1793       1.1     skrll 			break;
   1794       1.1     skrll 
   1795      1.14     skrll 		if (m0->m_len < sizeof (struct ether_header) &&
   1796      1.38     skrll 		    (m0 = m_pullup(m0, sizeof (struct ether_header))) == NULL) {
   1797      1.38     skrll 			ifp->if_oerrors++;
   1798      1.25     skrll 			continue;
   1799      1.38     skrll 		}
   1800      1.14     skrll 
   1801      1.12  christos 		eh = mtod(m0, struct ether_header *);
   1802      1.12  christos 		ni = ieee80211_find_txnode(ic, eh->ether_dhost);
   1803      1.12  christos 		if (ni == NULL) {
   1804       1.9    dyoung 			m_freem(m0);
   1805      1.38     skrll 			ifp->if_oerrors++;
   1806       1.9    dyoung 			continue;
   1807       1.9    dyoung 		}
   1808       1.1     skrll 
   1809      1.38     skrll 		/* classify mbuf so we can find which tx ring to use */
   1810      1.38     skrll 		if (ieee80211_classify(ic, m0, ni) != 0) {
   1811      1.38     skrll 			m_freem(m0);
   1812      1.38     skrll 			ieee80211_free_node(ni);
   1813      1.38     skrll 			ifp->if_oerrors++;
   1814      1.38     skrll 			continue;
   1815      1.38     skrll 		}
   1816      1.38     skrll 
   1817      1.38     skrll 		/* no QoS encapsulation for EAPOL frames */
   1818      1.38     skrll 		ac = (eh->ether_type != htons(ETHERTYPE_PAE)) ?
   1819      1.38     skrll 		    M_WME_GETAC(m0) : WME_AC_BE;
   1820      1.38     skrll 
   1821      1.38     skrll 		if (sc->txq[ac].queued > sc->txq[ac].count - 8) {
   1822      1.38     skrll 			/* there is no place left in this ring */
   1823      1.38     skrll 			IF_PREPEND(&ifp->if_snd, m0);
   1824      1.38     skrll 			ifp->if_flags |= IFF_OACTIVE;
   1825      1.38     skrll 			break;
   1826      1.38     skrll 		}
   1827      1.39     skrll 
   1828      1.39     skrll #if NBPFILTER > 0
   1829      1.39     skrll 		if (ifp->if_bpf != NULL)
   1830      1.39     skrll 			bpf_mtap(ifp->if_bpf, m0);
   1831      1.38     skrll #endif
   1832      1.39     skrll 
   1833      1.12  christos 		m0 = ieee80211_encap(ic, m0, ni);
   1834      1.12  christos 		if (m0 == NULL) {
   1835      1.12  christos 			ieee80211_free_node(ni);
   1836      1.38     skrll 			ifp->if_oerrors++;
   1837      1.12  christos 			continue;
   1838      1.12  christos 		}
   1839       1.1     skrll 
   1840      1.25     skrll #if NBPFILTER > 0
   1841      1.25     skrll 		if (ic->ic_rawbpf != NULL)
   1842      1.25     skrll 			bpf_mtap(ic->ic_rawbpf, m0);
   1843      1.25     skrll #endif
   1844      1.25     skrll 
   1845      1.38     skrll 		if (iwi_tx_start(ifp, m0, ni, ac) != 0) {
   1846      1.12  christos 			ieee80211_free_node(ni);
   1847      1.12  christos 			ifp->if_oerrors++;
   1848       1.1     skrll 			break;
   1849       1.1     skrll 		}
   1850       1.1     skrll 
   1851       1.1     skrll 		/* start watchdog timer */
   1852       1.1     skrll 		sc->sc_tx_timer = 5;
   1853       1.1     skrll 		ifp->if_timer = 1;
   1854       1.1     skrll 	}
   1855       1.1     skrll }
   1856       1.1     skrll 
   1857       1.1     skrll static void
   1858       1.1     skrll iwi_watchdog(struct ifnet *ifp)
   1859       1.1     skrll {
   1860       1.1     skrll 	struct iwi_softc *sc = ifp->if_softc;
   1861       1.1     skrll 
   1862       1.1     skrll 	ifp->if_timer = 0;
   1863       1.1     skrll 
   1864       1.1     skrll 	if (sc->sc_tx_timer > 0) {
   1865       1.1     skrll 		if (--sc->sc_tx_timer == 0) {
   1866  1.64.4.1    bouyer 			aprint_error_dev(sc->sc_dev, "device timeout\n");
   1867      1.12  christos 			ifp->if_oerrors++;
   1868      1.12  christos 			ifp->if_flags &= ~IFF_UP;
   1869       1.1     skrll 			iwi_stop(ifp, 1);
   1870       1.1     skrll 			return;
   1871       1.1     skrll 		}
   1872       1.1     skrll 		ifp->if_timer = 1;
   1873       1.1     skrll 	}
   1874       1.1     skrll 
   1875       1.9    dyoung 	ieee80211_watchdog(&sc->sc_ic);
   1876       1.1     skrll }
   1877       1.1     skrll 
   1878       1.1     skrll static int
   1879      1.24     skrll iwi_get_table0(struct iwi_softc *sc, uint32_t *tbl)
   1880       1.1     skrll {
   1881      1.24     skrll 	uint32_t size, buf[128];
   1882       1.1     skrll 
   1883       1.1     skrll 	if (!(sc->flags & IWI_FLAG_FW_INITED)) {
   1884       1.1     skrll 		memset(buf, 0, sizeof buf);
   1885       1.1     skrll 		return copyout(buf, tbl, sizeof buf);
   1886       1.1     skrll 	}
   1887       1.1     skrll 
   1888       1.1     skrll 	size = min(CSR_READ_4(sc, IWI_CSR_TABLE0_SIZE), 128 - 1);
   1889       1.1     skrll 	CSR_READ_REGION_4(sc, IWI_CSR_TABLE0_BASE, &buf[1], size);
   1890       1.1     skrll 
   1891       1.1     skrll 	return copyout(buf, tbl, sizeof buf);
   1892       1.1     skrll }
   1893       1.1     skrll 
   1894       1.1     skrll static int
   1895      1.61  christos iwi_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1896       1.1     skrll {
   1897      1.43     skrll #define	IS_RUNNING(ifp) \
   1898      1.43     skrll 	((ifp->if_flags & IFF_UP) && (ifp->if_flags & IFF_RUNNING))
   1899      1.43     skrll 
   1900       1.1     skrll 	struct iwi_softc *sc = ifp->if_softc;
   1901      1.38     skrll 	struct ieee80211com *ic = &sc->sc_ic;
   1902      1.43     skrll 	struct ifreq *ifr = (struct ifreq *)data;
   1903       1.1     skrll 	int s, error = 0;
   1904      1.54     skrll 	int val;
   1905       1.1     skrll 
   1906       1.1     skrll 	s = splnet();
   1907       1.1     skrll 
   1908       1.1     skrll 	switch (cmd) {
   1909       1.1     skrll 	case SIOCSIFFLAGS:
   1910       1.1     skrll 		if (ifp->if_flags & IFF_UP) {
   1911       1.1     skrll 			if (!(ifp->if_flags & IFF_RUNNING))
   1912       1.1     skrll 				iwi_init(ifp);
   1913       1.1     skrll 		} else {
   1914       1.1     skrll 			if (ifp->if_flags & IFF_RUNNING)
   1915       1.1     skrll 				iwi_stop(ifp, 1);
   1916       1.1     skrll 		}
   1917       1.1     skrll 		break;
   1918       1.1     skrll 
   1919      1.43     skrll 	case SIOCADDMULTI:
   1920      1.43     skrll 	case SIOCDELMULTI:
   1921      1.64    dyoung 		/* XXX no h/w multicast filter? --dyoung */
   1922      1.64    dyoung 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
   1923      1.43     skrll 			/* setup multicast filter, etc */
   1924      1.43     skrll 			error = 0;
   1925      1.43     skrll 		}
   1926      1.43     skrll 		break;
   1927      1.43     skrll 
   1928       1.1     skrll 	case SIOCGTABLE0:
   1929      1.24     skrll 		error = iwi_get_table0(sc, (uint32_t *)ifr->ifr_data);
   1930       1.1     skrll 		break;
   1931       1.1     skrll 
   1932       1.1     skrll 	case SIOCGRADIO:
   1933      1.54     skrll 		val = !iwi_getrfkill(sc);
   1934      1.54     skrll 		error = copyout(&val, (int *)ifr->ifr_data, sizeof val);
   1935       1.1     skrll 		break;
   1936       1.1     skrll 
   1937      1.52     skrll 	case SIOCSIFMEDIA:
   1938      1.52     skrll 		if (ifr->ifr_media & IFM_IEEE80211_ADHOC) {
   1939      1.52     skrll 			sc->sc_fwname = "iwi-ibss.fw";
   1940      1.52     skrll 		} else if (ifr->ifr_media & IFM_IEEE80211_MONITOR) {
   1941      1.52     skrll 			sc->sc_fwname = "iwi-sniffer.fw";
   1942      1.52     skrll 		} else {
   1943      1.52     skrll 			sc->sc_fwname = "iwi-bss.fw";
   1944      1.52     skrll 		}
   1945      1.52     skrll 		error = iwi_cache_firmware(sc);
   1946      1.52     skrll 		if (error)
   1947      1.52     skrll  			break;
   1948      1.52     skrll  		/* FALLTRHOUGH */
   1949       1.1     skrll 
   1950       1.1     skrll 	default:
   1951       1.9    dyoung 		error = ieee80211_ioctl(&sc->sc_ic, cmd, data);
   1952       1.1     skrll 
   1953      1.43     skrll 		if (error == ENETRESET) {
   1954      1.43     skrll 			if (IS_RUNNING(ifp) &&
   1955      1.43     skrll 			    (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
   1956      1.43     skrll 				iwi_init(ifp);
   1957      1.43     skrll 			error = 0;
   1958      1.43     skrll 		}
   1959       1.1     skrll 	}
   1960       1.1     skrll 
   1961       1.1     skrll 	splx(s);
   1962       1.1     skrll 	return error;
   1963      1.43     skrll #undef IS_RUNNING
   1964       1.1     skrll }
   1965       1.1     skrll 
   1966       1.1     skrll static void
   1967       1.1     skrll iwi_stop_master(struct iwi_softc *sc)
   1968       1.1     skrll {
   1969       1.1     skrll 	int ntries;
   1970       1.1     skrll 
   1971       1.1     skrll 	/* Disable interrupts */
   1972       1.1     skrll 	CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
   1973       1.1     skrll 
   1974       1.1     skrll 	CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_STOP_MASTER);
   1975       1.1     skrll 	for (ntries = 0; ntries < 5; ntries++) {
   1976       1.1     skrll 		if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
   1977       1.1     skrll 			break;
   1978       1.1     skrll 		DELAY(10);
   1979       1.1     skrll 	}
   1980       1.1     skrll 	if (ntries == 5)
   1981  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev, "timeout waiting for master\n");
   1982       1.1     skrll 
   1983       1.1     skrll 	CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
   1984       1.1     skrll 	    IWI_RST_PRINCETON_RESET);
   1985       1.1     skrll 
   1986       1.1     skrll 	sc->flags &= ~IWI_FLAG_FW_INITED;
   1987       1.1     skrll }
   1988       1.1     skrll 
   1989       1.1     skrll static int
   1990       1.1     skrll iwi_reset(struct iwi_softc *sc)
   1991       1.1     skrll {
   1992       1.1     skrll 	int i, ntries;
   1993       1.1     skrll 
   1994       1.1     skrll 	iwi_stop_master(sc);
   1995       1.1     skrll 
   1996       1.1     skrll 	/* Move adapter to D0 state */
   1997       1.1     skrll 	CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
   1998       1.1     skrll 	    IWI_CTL_INIT);
   1999       1.1     skrll 
   2000       1.1     skrll 	/* Initialize Phase-Locked Level  (PLL) */
   2001       1.1     skrll 	CSR_WRITE_4(sc, IWI_CSR_READ_INT, IWI_READ_INT_INIT_HOST);
   2002       1.1     skrll 
   2003       1.1     skrll 	/* Wait for clock stabilization */
   2004       1.1     skrll 	for (ntries = 0; ntries < 1000; ntries++) {
   2005       1.1     skrll 		if (CSR_READ_4(sc, IWI_CSR_CTL) & IWI_CTL_CLOCK_READY)
   2006       1.1     skrll 			break;
   2007       1.1     skrll 		DELAY(200);
   2008       1.1     skrll 	}
   2009      1.25     skrll 	if (ntries == 1000) {
   2010  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev,
   2011  1.64.4.1    bouyer 		    "timeout waiting for clock stabilization\n");
   2012      1.59     skrll 		return ETIMEDOUT;
   2013      1.25     skrll 	}
   2014       1.1     skrll 
   2015       1.1     skrll 	CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
   2016       1.1     skrll 	    IWI_RST_SW_RESET);
   2017       1.1     skrll 
   2018       1.1     skrll 	DELAY(10);
   2019       1.1     skrll 
   2020       1.1     skrll 	CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
   2021       1.1     skrll 	    IWI_CTL_INIT);
   2022       1.1     skrll 
   2023       1.1     skrll 	/* Clear NIC memory */
   2024       1.1     skrll 	CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0);
   2025       1.1     skrll 	for (i = 0; i < 0xc000; i++)
   2026       1.1     skrll 		CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
   2027       1.1     skrll 
   2028       1.1     skrll 	return 0;
   2029       1.1     skrll }
   2030       1.1     skrll 
   2031       1.1     skrll static int
   2032       1.1     skrll iwi_load_ucode(struct iwi_softc *sc, void *uc, int size)
   2033       1.1     skrll {
   2034      1.24     skrll 	uint16_t *w;
   2035       1.1     skrll 	int ntries, i;
   2036       1.1     skrll 
   2037       1.1     skrll 	CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
   2038       1.1     skrll 	    IWI_RST_STOP_MASTER);
   2039       1.1     skrll 	for (ntries = 0; ntries < 5; ntries++) {
   2040       1.1     skrll 		if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
   2041       1.1     skrll 			break;
   2042       1.1     skrll 		DELAY(10);
   2043       1.1     skrll 	}
   2044       1.1     skrll 	if (ntries == 5) {
   2045  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev, "timeout waiting for master\n");
   2046      1.59     skrll 		return ETIMEDOUT;
   2047       1.1     skrll 	}
   2048       1.1     skrll 
   2049       1.1     skrll 	MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
   2050       1.1     skrll 	DELAY(5000);
   2051       1.1     skrll 	CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) &
   2052       1.1     skrll 	    ~IWI_RST_PRINCETON_RESET);
   2053       1.1     skrll 	DELAY(5000);
   2054       1.1     skrll 	MEM_WRITE_4(sc, 0x3000e0, 0);
   2055       1.1     skrll 	DELAY(1000);
   2056       1.1     skrll 	MEM_WRITE_4(sc, 0x300004, 1);
   2057       1.1     skrll 	DELAY(1000);
   2058       1.1     skrll 	MEM_WRITE_4(sc, 0x300004, 0);
   2059       1.1     skrll 	DELAY(1000);
   2060       1.1     skrll 	MEM_WRITE_1(sc, 0x200000, 0x00);
   2061       1.1     skrll 	MEM_WRITE_1(sc, 0x200000, 0x40);
   2062      1.14     skrll 	DELAY(1000);
   2063       1.1     skrll 
   2064       1.1     skrll 	/* Adapter is buggy, we must set the address for each word */
   2065       1.1     skrll 	for (w = uc; size > 0; w++, size -= 2)
   2066      1.32       scw 		MEM_WRITE_2(sc, 0x200010, htole16(*w));
   2067       1.1     skrll 
   2068       1.1     skrll 	MEM_WRITE_1(sc, 0x200000, 0x00);
   2069       1.1     skrll 	MEM_WRITE_1(sc, 0x200000, 0x80);
   2070       1.1     skrll 
   2071       1.1     skrll 	/* Wait until we get a response in the uc queue */
   2072       1.1     skrll 	for (ntries = 0; ntries < 100; ntries++) {
   2073       1.1     skrll 		if (MEM_READ_1(sc, 0x200000) & 1)
   2074       1.1     skrll 			break;
   2075       1.1     skrll 		DELAY(100);
   2076       1.1     skrll 	}
   2077       1.1     skrll 	if (ntries == 100) {
   2078  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev,
   2079  1.64.4.1    bouyer 		    "timeout waiting for ucode to initialize\n");
   2080      1.59     skrll 		return ETIMEDOUT;
   2081       1.1     skrll 	}
   2082       1.1     skrll 
   2083       1.1     skrll 	/* Empty the uc queue or the firmware will not initialize properly */
   2084       1.1     skrll 	for (i = 0; i < 7; i++)
   2085       1.1     skrll 		MEM_READ_4(sc, 0x200004);
   2086       1.1     skrll 
   2087       1.1     skrll 	MEM_WRITE_1(sc, 0x200000, 0x00);
   2088       1.1     skrll 
   2089       1.1     skrll 	return 0;
   2090       1.1     skrll }
   2091       1.1     skrll 
   2092       1.1     skrll /* macro to handle unaligned little endian data in firmware image */
   2093       1.1     skrll #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
   2094       1.1     skrll static int
   2095       1.1     skrll iwi_load_firmware(struct iwi_softc *sc, void *fw, int size)
   2096       1.1     skrll {
   2097       1.1     skrll 	bus_dmamap_t map;
   2098       1.1     skrll 	u_char *p, *end;
   2099      1.27     skrll 	uint32_t sentinel, ctl, sum;
   2100      1.27     skrll 	uint32_t cs, sl, cd, cl;
   2101       1.1     skrll 	int ntries, nsegs, error;
   2102      1.27     skrll 	int sn;
   2103       1.1     skrll 
   2104      1.27     skrll 	nsegs = (size + PAGE_SIZE - 1) / PAGE_SIZE;
   2105      1.27     skrll 
   2106      1.28     skrll 	/* Create a DMA map for the firmware image */
   2107      1.27     skrll 	error = bus_dmamap_create(sc->sc_dmat, size, nsegs, size, 0,
   2108       1.1     skrll 	    BUS_DMA_NOWAIT, &map);
   2109       1.1     skrll 	if (error != 0) {
   2110  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev,
   2111  1.64.4.1    bouyer 		    "could not create firmware DMA map\n");
   2112       1.1     skrll 		goto fail1;
   2113       1.1     skrll 	}
   2114       1.1     skrll 
   2115      1.27     skrll 	error = bus_dmamap_load(sc->sc_dmat, map, fw, size, NULL,
   2116      1.27     skrll 	    BUS_DMA_NOWAIT | BUS_DMA_WRITE);
   2117       1.1     skrll 	if (error != 0) {
   2118  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev, "could not load fw dma map(%d)\n",
   2119  1.64.4.1    bouyer 		    error);
   2120       1.1     skrll 		goto fail2;
   2121       1.1     skrll 	}
   2122       1.1     skrll 
   2123       1.1     skrll 	/* Make sure the adapter will get up-to-date values */
   2124       1.1     skrll 	bus_dmamap_sync(sc->sc_dmat, map, 0, size, BUS_DMASYNC_PREWRITE);
   2125       1.1     skrll 
   2126       1.1     skrll 	/* Tell the adapter where the command blocks are stored */
   2127       1.1     skrll 	MEM_WRITE_4(sc, 0x3000a0, 0x27000);
   2128       1.1     skrll 
   2129       1.1     skrll 	/*
   2130       1.1     skrll 	 * Store command blocks into adapter's internal memory using register
   2131       1.1     skrll 	 * indirections. The adapter will read the firmware image through DMA
   2132       1.1     skrll 	 * using information stored in command blocks.
   2133       1.1     skrll 	 */
   2134      1.27     skrll 	p = fw;
   2135       1.1     skrll 	end = p + size;
   2136       1.1     skrll 	CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0x27000);
   2137       1.1     skrll 
   2138      1.27     skrll 	sn = 0;
   2139      1.27     skrll 	sl = cl = 0;
   2140      1.27     skrll 	cs = cd = 0;
   2141       1.1     skrll 	while (p < end) {
   2142      1.27     skrll 		if (sl == 0) {
   2143      1.27     skrll 			cs = map->dm_segs[sn].ds_addr;
   2144      1.27     skrll 			sl = map->dm_segs[sn].ds_len;
   2145      1.27     skrll 			sn++;
   2146      1.27     skrll 		}
   2147      1.27     skrll 		if (cl == 0) {
   2148      1.27     skrll 			cd = GETLE32(p); p += 4; cs += 4; sl -= 4;
   2149      1.27     skrll 			cl = GETLE32(p); p += 4; cs += 4; sl -= 4;
   2150      1.27     skrll 		}
   2151      1.27     skrll 		while (sl > 0 && cl > 0) {
   2152      1.27     skrll 			int len = min(cl, sl);
   2153      1.27     skrll 
   2154      1.27     skrll 			sl -= len;
   2155      1.27     skrll 			cl -= len;
   2156      1.27     skrll 			p += len;
   2157      1.27     skrll 
   2158      1.27     skrll 			while (len > 0) {
   2159      1.27     skrll 				int mlen = min(len, IWI_CB_MAXDATALEN);
   2160      1.27     skrll 
   2161      1.27     skrll 				ctl = IWI_CB_DEFAULT_CTL | mlen;
   2162      1.27     skrll 				sum = ctl ^ cs ^ cd;
   2163      1.27     skrll 
   2164      1.27     skrll 				/* Write a command block */
   2165      1.27     skrll 				CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, ctl);
   2166      1.27     skrll 				CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, cs);
   2167      1.27     skrll 				CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, cd);
   2168      1.27     skrll 				CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, sum);
   2169      1.27     skrll 
   2170      1.27     skrll 				cs += mlen;
   2171      1.27     skrll 				cd += mlen;
   2172      1.27     skrll 				len -= mlen;
   2173      1.27     skrll 			}
   2174       1.1     skrll 		}
   2175       1.1     skrll 	}
   2176       1.1     skrll 
   2177       1.1     skrll 	/* Write a fictive final command block (sentinel) */
   2178       1.1     skrll 	sentinel = CSR_READ_4(sc, IWI_CSR_AUTOINC_ADDR);
   2179       1.1     skrll 	CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
   2180       1.1     skrll 
   2181       1.1     skrll 	CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) &
   2182       1.1     skrll 	    ~(IWI_RST_MASTER_DISABLED | IWI_RST_STOP_MASTER));
   2183       1.1     skrll 
   2184       1.1     skrll 	/* Tell the adapter to start processing command blocks */
   2185       1.1     skrll 	MEM_WRITE_4(sc, 0x3000a4, 0x540100);
   2186       1.1     skrll 
   2187       1.1     skrll 	/* Wait until the adapter has processed all command blocks */
   2188       1.1     skrll 	for (ntries = 0; ntries < 400; ntries++) {
   2189       1.1     skrll 		if (MEM_READ_4(sc, 0x3000d0) >= sentinel)
   2190       1.1     skrll 			break;
   2191       1.1     skrll 		DELAY(100);
   2192       1.1     skrll 	}
   2193       1.1     skrll 	if (ntries == 400) {
   2194  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev, "timeout processing cb\n");
   2195      1.59     skrll 		error = ETIMEDOUT;
   2196      1.41     skrll 		goto fail3;
   2197       1.1     skrll 	}
   2198       1.1     skrll 
   2199       1.1     skrll 	/* We're done with command blocks processing */
   2200       1.1     skrll 	MEM_WRITE_4(sc, 0x3000a4, 0x540c00);
   2201       1.1     skrll 
   2202       1.1     skrll 	/* Allow interrupts so we know when the firmware is inited */
   2203       1.1     skrll 	CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
   2204       1.1     skrll 
   2205       1.1     skrll 	/* Tell the adapter to initialize the firmware */
   2206       1.1     skrll 	CSR_WRITE_4(sc, IWI_CSR_RST, 0);
   2207       1.1     skrll 	CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
   2208       1.1     skrll 	    IWI_CTL_ALLOW_STANDBY);
   2209       1.1     skrll 
   2210       1.1     skrll 	/* Wait at most one second for firmware initialization to complete */
   2211       1.1     skrll 	if ((error = tsleep(sc, 0, "iwiinit", hz)) != 0) {
   2212  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev,
   2213  1.64.4.1    bouyer 		    "timeout waiting for firmware initialization to complete\n");
   2214      1.27     skrll 		goto fail3;
   2215       1.1     skrll 	}
   2216       1.1     skrll 
   2217      1.27     skrll fail3:
   2218      1.27     skrll 	bus_dmamap_sync(sc->sc_dmat, map, 0, size, BUS_DMASYNC_POSTWRITE);
   2219       1.1     skrll 	bus_dmamap_unload(sc->sc_dmat, map);
   2220      1.27     skrll fail2:
   2221      1.27     skrll 	bus_dmamap_destroy(sc->sc_dmat, map);
   2222       1.5     perry 
   2223      1.27     skrll fail1:
   2224      1.27     skrll 	return error;
   2225       1.1     skrll }
   2226       1.1     skrll 
   2227       1.1     skrll /*
   2228       1.1     skrll  * Store firmware into kernel memory so we can download it when we need to,
   2229       1.1     skrll  * e.g when the adapter wakes up from suspend mode.
   2230       1.1     skrll  */
   2231       1.1     skrll static int
   2232      1.52     skrll iwi_cache_firmware(struct iwi_softc *sc)
   2233       1.1     skrll {
   2234       1.1     skrll 	struct iwi_firmware *kfw = &sc->fw;
   2235      1.52     skrll 	firmware_handle_t fwh;
   2236      1.59     skrll 	const struct iwi_firmware_hdr *hdr;
   2237      1.59     skrll 	off_t size;
   2238      1.59     skrll 	char *fw;
   2239       1.1     skrll 	int error;
   2240       1.1     skrll 
   2241       1.1     skrll 	iwi_free_firmware(sc);
   2242      1.59     skrll 	error = firmware_open("if_iwi", sc->sc_fwname, &fwh);
   2243      1.59     skrll 	if (error != 0) {
   2244  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev, "firmware_open failed\n");
   2245       1.1     skrll 		goto fail1;
   2246      1.59     skrll 	}
   2247       1.1     skrll 
   2248      1.59     skrll 	size = firmware_get_size(fwh);
   2249      1.59     skrll 	if (size < sizeof(struct iwi_firmware_hdr)) {
   2250  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev, "image '%s' has no header\n",
   2251  1.64.4.1    bouyer 		    sc->sc_fwname);
   2252      1.59     skrll 		error = EIO;
   2253      1.59     skrll 		goto fail1;
   2254      1.59     skrll 	}
   2255      1.59     skrll 
   2256      1.59     skrll 	sc->sc_blob = firmware_malloc(size);
   2257      1.59     skrll 	if (sc->sc_blob == NULL) {
   2258       1.1     skrll 		error = ENOMEM;
   2259      1.52     skrll 		firmware_close(fwh);
   2260       1.1     skrll 		goto fail1;
   2261       1.1     skrll 	}
   2262       1.1     skrll 
   2263      1.59     skrll 	error = firmware_read(fwh, 0, sc->sc_blob, size);
   2264      1.52     skrll 	firmware_close(fwh);
   2265      1.52     skrll 	if (error != 0)
   2266      1.52     skrll 		goto fail2;
   2267      1.52     skrll 
   2268      1.52     skrll 
   2269      1.59     skrll 	hdr = (const struct iwi_firmware_hdr *)sc->sc_blob;
   2270      1.59     skrll 	if (size < sizeof(struct iwi_firmware_hdr) + hdr->bsize + hdr->usize + hdr->fsize) {
   2271  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev, "image '%s' too small\n",
   2272  1.64.4.1    bouyer 		    sc->sc_fwname);
   2273      1.59     skrll 		error = EIO;
   2274       1.1     skrll 		goto fail2;
   2275       1.1     skrll 	}
   2276       1.1     skrll 
   2277      1.59     skrll 	hdr = (const struct iwi_firmware_hdr *)sc->sc_blob;
   2278      1.62    sketch 	DPRINTF(("firmware version = %d\n", le32toh(hdr->version)));
   2279      1.59     skrll 	if ((IWI_FW_GET_MAJOR(le32toh(hdr->version)) != IWI_FW_REQ_MAJOR) ||
   2280      1.59     skrll 	    (IWI_FW_GET_MINOR(le32toh(hdr->version)) != IWI_FW_REQ_MINOR)) {
   2281  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev,
   2282  1.64.4.1    bouyer 		    "version for '%s' %d.%d != %d.%d\n", sc->sc_fwname,
   2283      1.59     skrll 		    IWI_FW_GET_MAJOR(le32toh(hdr->version)),
   2284      1.59     skrll 		    IWI_FW_GET_MINOR(le32toh(hdr->version)),
   2285      1.59     skrll 		    IWI_FW_REQ_MAJOR, IWI_FW_REQ_MINOR);
   2286      1.59     skrll 		error = EIO;
   2287      1.59     skrll 		goto fail2;
   2288       1.1     skrll 	}
   2289       1.1     skrll 
   2290      1.59     skrll 	kfw->boot_size = hdr->bsize;
   2291      1.59     skrll 	kfw->ucode_size = hdr->usize;
   2292      1.59     skrll 	kfw->main_size = hdr->fsize;
   2293      1.59     skrll 
   2294      1.59     skrll 	fw = sc->sc_blob + sizeof(struct iwi_firmware_hdr);
   2295      1.59     skrll 	kfw->boot = fw;
   2296      1.59     skrll 	fw += kfw->boot_size;
   2297      1.59     skrll 	kfw->ucode = fw;
   2298      1.59     skrll 	fw += kfw->ucode_size;
   2299      1.59     skrll 	kfw->main = fw;
   2300       1.1     skrll 
   2301      1.59     skrll 	DPRINTF(("Firmware cached: boot %p, ucode %p, main %p\n",
   2302      1.59     skrll 	    kfw->boot, kfw->ucode, kfw->main));
   2303       1.1     skrll 	DPRINTF(("Firmware cached: boot %u, ucode %u, main %u\n",
   2304       1.1     skrll 	    kfw->boot_size, kfw->ucode_size, kfw->main_size));
   2305       1.1     skrll 
   2306       1.1     skrll 	sc->flags |= IWI_FLAG_FW_CACHED;
   2307       1.1     skrll 
   2308       1.1     skrll 	return 0;
   2309       1.1     skrll 
   2310      1.59     skrll 
   2311      1.59     skrll fail2:	firmware_free(sc->sc_blob, 0);
   2312       1.1     skrll fail1:
   2313       1.1     skrll 	return error;
   2314       1.1     skrll }
   2315       1.1     skrll 
   2316       1.1     skrll static void
   2317       1.1     skrll iwi_free_firmware(struct iwi_softc *sc)
   2318       1.1     skrll {
   2319      1.52     skrll 
   2320       1.1     skrll 	if (!(sc->flags & IWI_FLAG_FW_CACHED))
   2321       1.1     skrll 		return;
   2322       1.5     perry 
   2323      1.60     skrll 	firmware_free(sc->sc_blob, 0);
   2324       1.1     skrll 
   2325       1.1     skrll 	sc->flags &= ~IWI_FLAG_FW_CACHED;
   2326       1.1     skrll }
   2327       1.1     skrll 
   2328       1.1     skrll static int
   2329       1.1     skrll iwi_config(struct iwi_softc *sc)
   2330       1.1     skrll {
   2331       1.1     skrll 	struct ieee80211com *ic = &sc->sc_ic;
   2332       1.9    dyoung 	struct ifnet *ifp = &sc->sc_if;
   2333       1.1     skrll 	struct iwi_configuration config;
   2334       1.1     skrll 	struct iwi_rateset rs;
   2335       1.1     skrll 	struct iwi_txpower power;
   2336      1.12  christos 	struct ieee80211_key *wk;
   2337       1.1     skrll 	struct iwi_wep_key wepkey;
   2338      1.24     skrll 	uint32_t data;
   2339      1.59     skrll 	int error, nchan, i;
   2340       1.1     skrll 
   2341      1.63    dyoung 	IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl));
   2342       1.1     skrll 	DPRINTF(("Setting MAC address to %s\n", ether_sprintf(ic->ic_myaddr)));
   2343       1.1     skrll 	error = iwi_cmd(sc, IWI_CMD_SET_MAC_ADDRESS, ic->ic_myaddr,
   2344       1.1     skrll 	    IEEE80211_ADDR_LEN, 0);
   2345       1.1     skrll 	if (error != 0)
   2346       1.1     skrll 		return error;
   2347       1.1     skrll 
   2348       1.1     skrll 	memset(&config, 0, sizeof config);
   2349      1.12  christos 	config.bluetooth_coexistence = sc->bluetooth;
   2350      1.12  christos 	config.antenna = sc->antenna;
   2351      1.59     skrll 	config.silence_threshold = 0x1e;
   2352       1.1     skrll 	config.multicast_enabled = 1;
   2353      1.12  christos 	config.answer_pbreq = (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0;
   2354      1.12  christos 	config.disable_unicast_decryption = 1;
   2355      1.12  christos 	config.disable_multicast_decryption = 1;
   2356       1.1     skrll 	DPRINTF(("Configuring adapter\n"));
   2357       1.1     skrll 	error = iwi_cmd(sc, IWI_CMD_SET_CONFIGURATION, &config, sizeof config,
   2358       1.1     skrll 	    0);
   2359       1.1     skrll 	if (error != 0)
   2360       1.1     skrll 		return error;
   2361       1.1     skrll 
   2362       1.1     skrll 	data = htole32(IWI_POWER_MODE_CAM);
   2363       1.1     skrll 	DPRINTF(("Setting power mode to %u\n", le32toh(data)));
   2364       1.1     skrll 	error = iwi_cmd(sc, IWI_CMD_SET_POWER_MODE, &data, sizeof data, 0);
   2365       1.1     skrll 	if (error != 0)
   2366       1.1     skrll 		return error;
   2367       1.1     skrll 
   2368       1.1     skrll 	data = htole32(ic->ic_rtsthreshold);
   2369       1.1     skrll 	DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
   2370       1.1     skrll 	error = iwi_cmd(sc, IWI_CMD_SET_RTS_THRESHOLD, &data, sizeof data, 0);
   2371       1.1     skrll 	if (error != 0)
   2372       1.1     skrll 		return error;
   2373       1.1     skrll 
   2374      1.12  christos 	data = htole32(ic->ic_fragthreshold);
   2375      1.12  christos 	DPRINTF(("Setting fragmentation threshold to %u\n", le32toh(data)));
   2376      1.12  christos 	error = iwi_cmd(sc, IWI_CMD_SET_FRAG_THRESHOLD, &data, sizeof data, 0);
   2377      1.12  christos 	if (error != 0)
   2378      1.12  christos 		return error;
   2379      1.12  christos 
   2380      1.59     skrll 	/*
   2381      1.59     skrll 	 * Set default Tx power for 802.11b/g and 802.11a channels.
   2382      1.59     skrll 	 */
   2383      1.59     skrll 	nchan = 0;
   2384      1.59     skrll 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
   2385      1.59     skrll 		if (!IEEE80211_IS_CHAN_2GHZ(&ic->ic_channels[i]))
   2386      1.59     skrll 			continue;
   2387      1.59     skrll 		power.chan[nchan].chan = i;
   2388      1.59     skrll 		power.chan[nchan].power = IWI_TXPOWER_MAX;
   2389      1.59     skrll 		nchan++;
   2390      1.59     skrll 	}
   2391      1.59     skrll 	power.nchan = nchan;
   2392      1.59     skrll 
   2393      1.59     skrll 	power.mode = IWI_MODE_11G;
   2394      1.59     skrll 	DPRINTF(("Setting .11g channels tx power\n"));
   2395      1.59     skrll 	error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power, 0);
   2396      1.59     skrll 	if (error != 0)
   2397      1.59     skrll 		return error;
   2398      1.59     skrll 
   2399      1.59     skrll 	power.mode = IWI_MODE_11B;
   2400      1.59     skrll 	DPRINTF(("Setting .11b channels tx power\n"));
   2401      1.59     skrll 	error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power, 0);
   2402      1.59     skrll 	if (error != 0)
   2403      1.59     skrll 		return error;
   2404       1.1     skrll 
   2405      1.59     skrll 	nchan = 0;
   2406      1.59     skrll 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
   2407      1.59     skrll 		if (!IEEE80211_IS_CHAN_5GHZ(&ic->ic_channels[i]))
   2408      1.59     skrll 			continue;
   2409      1.59     skrll 		power.chan[nchan].chan = i;
   2410      1.59     skrll 		power.chan[nchan].power = IWI_TXPOWER_MAX;
   2411      1.59     skrll 		nchan++;
   2412      1.59     skrll 	}
   2413      1.59     skrll 	power.nchan = nchan;
   2414      1.59     skrll 
   2415      1.59     skrll 	if (nchan > 0) {	/* 2915ABG only */
   2416      1.59     skrll 		power.mode = IWI_MODE_11A;
   2417      1.59     skrll 		DPRINTF(("Setting .11a channels tx power\n"));
   2418       1.1     skrll 		error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power,
   2419       1.1     skrll 		    0);
   2420       1.1     skrll 		if (error != 0)
   2421       1.1     skrll 			return error;
   2422       1.1     skrll 	}
   2423       1.1     skrll 
   2424       1.1     skrll 	rs.mode = IWI_MODE_11G;
   2425       1.1     skrll 	rs.type = IWI_RATESET_TYPE_SUPPORTED;
   2426       1.1     skrll 	rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates;
   2427       1.1     skrll 	memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11G].rs_rates,
   2428       1.1     skrll 	    rs.nrates);
   2429       1.1     skrll 	DPRINTF(("Setting .11bg supported rates (%u)\n", rs.nrates));
   2430       1.1     skrll 	error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
   2431       1.1     skrll 	if (error != 0)
   2432       1.1     skrll 		return error;
   2433       1.1     skrll 
   2434       1.1     skrll 	rs.mode = IWI_MODE_11A;
   2435       1.1     skrll 	rs.type = IWI_RATESET_TYPE_SUPPORTED;
   2436       1.1     skrll 	rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates;
   2437       1.1     skrll 	memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11A].rs_rates,
   2438       1.1     skrll 	    rs.nrates);
   2439       1.1     skrll 	DPRINTF(("Setting .11a supported rates (%u)\n", rs.nrates));
   2440       1.1     skrll 	error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
   2441       1.1     skrll 	if (error != 0)
   2442       1.1     skrll 		return error;
   2443       1.1     skrll 
   2444      1.38     skrll 	/* if we have a desired ESSID, set it now */
   2445      1.38     skrll 	if (ic->ic_des_esslen != 0) {
   2446      1.38     skrll #ifdef IWI_DEBUG
   2447      1.38     skrll 		if (iwi_debug > 0) {
   2448      1.38     skrll 			printf("Setting desired ESSID to ");
   2449      1.38     skrll 			ieee80211_print_essid(ic->ic_des_essid,
   2450      1.38     skrll 			    ic->ic_des_esslen);
   2451      1.38     skrll 			printf("\n");
   2452      1.38     skrll 		}
   2453      1.38     skrll #endif
   2454      1.38     skrll 		error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ic->ic_des_essid,
   2455      1.38     skrll 		    ic->ic_des_esslen, 0);
   2456      1.38     skrll 		if (error != 0)
   2457      1.38     skrll 			return error;
   2458      1.38     skrll 	}
   2459      1.38     skrll 
   2460       1.1     skrll 	data = htole32(arc4random());
   2461       1.1     skrll 	DPRINTF(("Setting initialization vector to %u\n", le32toh(data)));
   2462       1.1     skrll 	error = iwi_cmd(sc, IWI_CMD_SET_IV, &data, sizeof data, 0);
   2463       1.1     skrll 	if (error != 0)
   2464       1.1     skrll 		return error;
   2465       1.1     skrll 
   2466      1.59     skrll 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
   2467      1.59     skrll 		/* XXX iwi_setwepkeys? */
   2468      1.59     skrll 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   2469      1.59     skrll 			wk = &ic->ic_crypto.cs_nw_keys[i];
   2470      1.59     skrll 
   2471      1.59     skrll 			wepkey.cmd = IWI_WEP_KEY_CMD_SETKEY;
   2472      1.59     skrll 			wepkey.idx = i;
   2473      1.59     skrll 			wepkey.len = wk->wk_keylen;
   2474      1.59     skrll 			memset(wepkey.key, 0, sizeof wepkey.key);
   2475      1.59     skrll 			memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
   2476      1.59     skrll 			DPRINTF(("Setting wep key index %u len %u\n",
   2477      1.59     skrll 			    wepkey.idx, wepkey.len));
   2478      1.59     skrll 			error = iwi_cmd(sc, IWI_CMD_SET_WEP_KEY, &wepkey,
   2479      1.59     skrll 			    sizeof wepkey, 0);
   2480      1.59     skrll 			if (error != 0)
   2481      1.59     skrll 				return error;
   2482      1.59     skrll 		}
   2483       1.1     skrll 	}
   2484       1.1     skrll 
   2485       1.1     skrll 	/* Enable adapter */
   2486       1.1     skrll 	DPRINTF(("Enabling adapter\n"));
   2487       1.1     skrll 	return iwi_cmd(sc, IWI_CMD_ENABLE, NULL, 0, 0);
   2488       1.1     skrll }
   2489       1.1     skrll 
   2490       1.1     skrll static int
   2491       1.8    sekiya iwi_set_chan(struct iwi_softc *sc, struct ieee80211_channel *chan)
   2492       1.8    sekiya {
   2493       1.8    sekiya 	struct ieee80211com *ic = &sc->sc_ic;
   2494      1.30     skrll 	struct iwi_scan_v2 scan;
   2495       1.8    sekiya 
   2496      1.12  christos 	(void)memset(&scan, 0, sizeof scan);
   2497      1.30     skrll 
   2498      1.30     skrll 	scan.dwelltime[IWI_SCAN_TYPE_PASSIVE] = htole16(2000);
   2499      1.30     skrll 	scan.channels[0] = 1 |
   2500      1.30     skrll 	    (IEEE80211_IS_CHAN_5GHZ(chan) ? IWI_CHAN_5GHZ : IWI_CHAN_2GHZ);
   2501       1.8    sekiya 	scan.channels[1] = ieee80211_chan2ieee(ic, chan);
   2502      1.30     skrll 	iwi_scan_type_set(scan, 1, IWI_SCAN_TYPE_PASSIVE);
   2503       1.8    sekiya 
   2504       1.8    sekiya 	DPRINTF(("Setting channel to %u\n", ieee80211_chan2ieee(ic, chan)));
   2505      1.30     skrll 	return iwi_cmd(sc, IWI_CMD_SCAN_V2, &scan, sizeof scan, 1);
   2506       1.8    sekiya }
   2507       1.8    sekiya 
   2508       1.8    sekiya static int
   2509       1.1     skrll iwi_scan(struct iwi_softc *sc)
   2510       1.1     skrll {
   2511       1.1     skrll 	struct ieee80211com *ic = &sc->sc_ic;
   2512      1.26     skrll 	struct iwi_scan_v2 scan;
   2513      1.26     skrll 	uint32_t type;
   2514      1.24     skrll 	uint8_t *p;
   2515      1.26     skrll 	int i, count, idx;
   2516       1.1     skrll 
   2517      1.12  christos 	(void)memset(&scan, 0, sizeof scan);
   2518      1.26     skrll 	scan.dwelltime[IWI_SCAN_TYPE_ACTIVE_BROADCAST] =
   2519      1.26     skrll 	    htole16(sc->dwelltime);
   2520      1.26     skrll 	scan.dwelltime[IWI_SCAN_TYPE_ACTIVE_BDIRECT] =
   2521      1.26     skrll 	    htole16(sc->dwelltime);
   2522      1.26     skrll 
   2523      1.26     skrll 	/* tell the firmware about the desired essid */
   2524      1.26     skrll 	if (ic->ic_des_esslen) {
   2525      1.26     skrll 		int error;
   2526      1.26     skrll 
   2527      1.26     skrll 		DPRINTF(("%s: Setting adapter desired ESSID to %s\n",
   2528      1.26     skrll 		    __func__, ic->ic_des_essid));
   2529      1.26     skrll 
   2530      1.26     skrll 		error = iwi_cmd(sc, IWI_CMD_SET_ESSID,
   2531      1.26     skrll 		    ic->ic_des_essid, ic->ic_des_esslen, 1);
   2532      1.26     skrll 		if (error)
   2533      1.26     skrll 			return error;
   2534      1.26     skrll 
   2535      1.26     skrll 		type = IWI_SCAN_TYPE_ACTIVE_BDIRECT;
   2536      1.26     skrll 	} else {
   2537      1.26     skrll 		type = IWI_SCAN_TYPE_ACTIVE_BROADCAST;
   2538      1.26     skrll 	}
   2539       1.1     skrll 
   2540      1.26     skrll 	p = &scan.channels[0];
   2541      1.26     skrll 	count = idx = 0;
   2542       1.1     skrll 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
   2543       1.1     skrll 		if (IEEE80211_IS_CHAN_5GHZ(&ic->ic_channels[i]) &&
   2544       1.1     skrll 		    isset(ic->ic_chan_active, i)) {
   2545       1.1     skrll 			*++p = i;
   2546       1.1     skrll 			count++;
   2547      1.26     skrll 			idx++;
   2548      1.26     skrll  			iwi_scan_type_set(scan, idx, type);
   2549       1.1     skrll 		}
   2550       1.1     skrll 	}
   2551      1.26     skrll 	if (count) {
   2552      1.26     skrll 		*(p - count) = IWI_CHAN_5GHZ | count;
   2553      1.26     skrll 		p++;
   2554      1.26     skrll 	}
   2555       1.1     skrll 
   2556       1.1     skrll 	count = 0;
   2557       1.1     skrll 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
   2558       1.1     skrll 		if (IEEE80211_IS_CHAN_2GHZ(&ic->ic_channels[i]) &&
   2559       1.1     skrll 		    isset(ic->ic_chan_active, i)) {
   2560       1.1     skrll 			*++p = i;
   2561       1.1     skrll 			count++;
   2562      1.26     skrll 			idx++;
   2563      1.26     skrll 			iwi_scan_type_set(scan, idx, type);
   2564       1.1     skrll 		}
   2565       1.1     skrll 	}
   2566       1.1     skrll 	*(p - count) = IWI_CHAN_2GHZ | count;
   2567       1.1     skrll 
   2568       1.1     skrll 	DPRINTF(("Start scanning\n"));
   2569      1.26     skrll 	return iwi_cmd(sc, IWI_CMD_SCAN_V2, &scan, sizeof scan, 1);
   2570       1.1     skrll }
   2571       1.1     skrll 
   2572       1.1     skrll static int
   2573       1.1     skrll iwi_auth_and_assoc(struct iwi_softc *sc)
   2574       1.1     skrll {
   2575       1.1     skrll 	struct ieee80211com *ic = &sc->sc_ic;
   2576       1.1     skrll 	struct ieee80211_node *ni = ic->ic_bss;
   2577      1.12  christos 	struct ifnet *ifp = &sc->sc_if;
   2578      1.38     skrll 	struct ieee80211_wme_info wme;
   2579       1.1     skrll 	struct iwi_configuration config;
   2580       1.1     skrll 	struct iwi_associate assoc;
   2581       1.1     skrll 	struct iwi_rateset rs;
   2582      1.24     skrll 	uint16_t capinfo;
   2583      1.24     skrll 	uint32_t data;
   2584       1.1     skrll 	int error;
   2585       1.1     skrll 
   2586      1.59     skrll 	memset(&config, 0, sizeof config);
   2587      1.59     skrll 	config.bluetooth_coexistence = sc->bluetooth;
   2588      1.59     skrll 	config.antenna = sc->antenna;
   2589      1.59     skrll 	config.multicast_enabled = 1;
   2590      1.59     skrll 	config.silence_threshold = 0x1e;
   2591      1.59     skrll 	if (ic->ic_curmode == IEEE80211_MODE_11G)
   2592      1.12  christos 		config.use_protection = 1;
   2593      1.59     skrll 	config.answer_pbreq = (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0;
   2594      1.59     skrll 	config.disable_unicast_decryption = 1;
   2595      1.59     skrll 	config.disable_multicast_decryption = 1;
   2596      1.59     skrll 
   2597      1.59     skrll 	DPRINTF(("Configuring adapter\n"));
   2598      1.59     skrll 	error = iwi_cmd(sc, IWI_CMD_SET_CONFIGURATION, &config,
   2599      1.59     skrll 	    sizeof config, 1);
   2600      1.59     skrll 	if (error != 0)
   2601      1.59     skrll 		return error;
   2602       1.1     skrll 
   2603       1.1     skrll #ifdef IWI_DEBUG
   2604       1.1     skrll 	if (iwi_debug > 0) {
   2605  1.64.4.1    bouyer 		aprint_debug_dev(sc->sc_dev, "Setting ESSID to ");
   2606       1.1     skrll 		ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
   2607  1.64.4.1    bouyer 		aprint_debug("\n");
   2608       1.1     skrll 	}
   2609       1.1     skrll #endif
   2610       1.1     skrll 	error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ni->ni_essid, ni->ni_esslen, 1);
   2611       1.1     skrll 	if (error != 0)
   2612       1.1     skrll 		return error;
   2613       1.1     skrll 
   2614      1.22     skrll 	/* the rate set has already been "negotiated" */
   2615       1.1     skrll 	rs.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A :
   2616       1.1     skrll 	    IWI_MODE_11G;
   2617      1.22     skrll 	rs.type = IWI_RATESET_TYPE_NEGOTIATED;
   2618       1.1     skrll 	rs.nrates = ni->ni_rates.rs_nrates;
   2619      1.59     skrll 
   2620      1.59     skrll 	if (rs.nrates > IWI_RATESET_SIZE) {
   2621      1.59     skrll 		DPRINTF(("Truncating negotiated rate set from %u\n",
   2622      1.59     skrll 		    rs.nrates));
   2623      1.59     skrll 		rs.nrates = IWI_RATESET_SIZE;
   2624      1.59     skrll 	}
   2625       1.1     skrll 	memcpy(rs.rates, ni->ni_rates.rs_rates, rs.nrates);
   2626      1.22     skrll 	DPRINTF(("Setting negotiated rates (%u)\n", rs.nrates));
   2627       1.1     skrll 	error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 1);
   2628       1.1     skrll 	if (error != 0)
   2629       1.1     skrll 		return error;
   2630       1.1     skrll 
   2631      1.38     skrll 	if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL) {
   2632      1.38     skrll 		wme.wme_id = IEEE80211_ELEMID_VENDOR;
   2633      1.38     skrll 		wme.wme_len = sizeof (struct ieee80211_wme_info) - 2;
   2634      1.38     skrll 		wme.wme_oui[0] = 0x00;
   2635      1.38     skrll 		wme.wme_oui[1] = 0x50;
   2636      1.38     skrll 		wme.wme_oui[2] = 0xf2;
   2637      1.38     skrll 		wme.wme_type = WME_OUI_TYPE;
   2638      1.38     skrll 		wme.wme_subtype = WME_INFO_OUI_SUBTYPE;
   2639      1.38     skrll 		wme.wme_version = WME_VERSION;
   2640      1.38     skrll 		wme.wme_info = 0;
   2641      1.38     skrll 
   2642      1.38     skrll 		DPRINTF(("Setting WME IE (len=%u)\n", wme.wme_len));
   2643      1.38     skrll 		error = iwi_cmd(sc, IWI_CMD_SET_WMEIE, &wme, sizeof wme, 1);
   2644      1.38     skrll 		if (error != 0)
   2645      1.38     skrll 			return error;
   2646      1.38     skrll 	}
   2647      1.38     skrll 
   2648      1.12  christos 	if (ic->ic_opt_ie != NULL) {
   2649      1.12  christos 		DPRINTF(("Setting optional IE (len=%u)\n", ic->ic_opt_ie_len));
   2650      1.12  christos 		error = iwi_cmd(sc, IWI_CMD_SET_OPTIE, ic->ic_opt_ie,
   2651      1.12  christos 		    ic->ic_opt_ie_len, 1);
   2652      1.12  christos 		if (error != 0)
   2653      1.12  christos 			return error;
   2654      1.12  christos 	}
   2655       1.1     skrll 	data = htole32(ni->ni_rssi);
   2656       1.1     skrll 	DPRINTF(("Setting sensitivity to %d\n", (int8_t)ni->ni_rssi));
   2657       1.1     skrll 	error = iwi_cmd(sc, IWI_CMD_SET_SENSITIVITY, &data, sizeof data, 1);
   2658       1.1     skrll 	if (error != 0)
   2659       1.1     skrll 		return error;
   2660       1.1     skrll 
   2661       1.1     skrll 	memset(&assoc, 0, sizeof assoc);
   2662      1.59     skrll 	if (IEEE80211_IS_CHAN_A(ni->ni_chan))
   2663      1.59     skrll 		assoc.mode = IWI_MODE_11A;
   2664      1.59     skrll 	else if (IEEE80211_IS_CHAN_G(ni->ni_chan))
   2665      1.59     skrll 		assoc.mode = IWI_MODE_11G;
   2666      1.59     skrll 	else if (IEEE80211_IS_CHAN_B(ni->ni_chan))
   2667      1.59     skrll 		assoc.mode = IWI_MODE_11B;
   2668      1.59     skrll 
   2669       1.1     skrll 	assoc.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
   2670      1.59     skrll 
   2671      1.12  christos 	if (ni->ni_authmode == IEEE80211_AUTH_SHARED)
   2672      1.12  christos 		assoc.auth = (ic->ic_crypto.cs_def_txkey << 4) | IWI_AUTH_SHARED;
   2673      1.59     skrll 
   2674      1.59     skrll 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
   2675      1.59     skrll 		assoc.plen = IWI_ASSOC_SHPREAMBLE;
   2676      1.59     skrll 
   2677      1.38     skrll 	if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL)
   2678      1.38     skrll 		assoc.policy |= htole16(IWI_POLICY_WME);
   2679      1.45     skrll 	if (ic->ic_flags & IEEE80211_F_WPA)
   2680      1.38     skrll 		assoc.policy |= htole16(IWI_POLICY_WPA);
   2681      1.59     skrll 	if (ic->ic_opmode == IEEE80211_M_IBSS && ni->ni_tstamp.tsf == 0)
   2682      1.59     skrll 		assoc.type = IWI_HC_IBSS_START;
   2683      1.59     skrll 	else
   2684      1.59     skrll 		assoc.type = IWI_HC_ASSOC;
   2685       1.9    dyoung 	memcpy(assoc.tstamp, ni->ni_tstamp.data, 8);
   2686      1.12  christos 
   2687      1.12  christos 	if (ic->ic_opmode == IEEE80211_M_IBSS)
   2688      1.12  christos 		capinfo = IEEE80211_CAPINFO_IBSS;
   2689      1.12  christos 	else
   2690      1.12  christos 		capinfo = IEEE80211_CAPINFO_ESS;
   2691      1.12  christos 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
   2692      1.12  christos 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
   2693      1.12  christos 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
   2694      1.12  christos 	    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
   2695      1.12  christos 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
   2696      1.12  christos 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
   2697      1.12  christos 		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
   2698      1.12  christos 	assoc.capinfo = htole16(capinfo);
   2699      1.12  christos 
   2700       1.1     skrll 	assoc.lintval = htole16(ic->ic_lintval);
   2701       1.1     skrll 	assoc.intval = htole16(ni->ni_intval);
   2702       1.1     skrll 	IEEE80211_ADDR_COPY(assoc.bssid, ni->ni_bssid);
   2703      1.12  christos 	if (ic->ic_opmode == IEEE80211_M_IBSS)
   2704      1.12  christos 		IEEE80211_ADDR_COPY(assoc.dst, ifp->if_broadcastaddr);
   2705      1.12  christos 	else
   2706      1.12  christos 		IEEE80211_ADDR_COPY(assoc.dst, ni->ni_bssid);
   2707      1.59     skrll 
   2708      1.59     skrll 	DPRINTF(("%s bssid %s dst %s channel %u policy 0x%x "
   2709      1.59     skrll 	    "auth %u capinfo 0x%x lintval %u bintval %u\n",
   2710      1.59     skrll 	    assoc.type == IWI_HC_IBSS_START ? "Start" : "Join",
   2711      1.59     skrll 	    ether_sprintf(assoc.bssid), ether_sprintf(assoc.dst),
   2712      1.59     skrll 	    assoc.chan, le16toh(assoc.policy), assoc.auth,
   2713      1.59     skrll 	    le16toh(assoc.capinfo), le16toh(assoc.lintval),
   2714      1.59     skrll 	    le16toh(assoc.intval)));
   2715      1.59     skrll 
   2716       1.1     skrll 	return iwi_cmd(sc, IWI_CMD_ASSOCIATE, &assoc, sizeof assoc, 1);
   2717       1.1     skrll }
   2718       1.1     skrll 
   2719       1.1     skrll static int
   2720       1.1     skrll iwi_init(struct ifnet *ifp)
   2721       1.1     skrll {
   2722       1.1     skrll 	struct iwi_softc *sc = ifp->if_softc;
   2723       1.1     skrll 	struct ieee80211com *ic = &sc->sc_ic;
   2724       1.1     skrll 	struct iwi_firmware *fw = &sc->fw;
   2725       1.1     skrll 	int i, error;
   2726       1.1     skrll 
   2727       1.1     skrll 	/* exit immediately if firmware has not been ioctl'd */
   2728       1.1     skrll 	if (!(sc->flags & IWI_FLAG_FW_CACHED)) {
   2729      1.52     skrll 		if ((error = iwi_cache_firmware(sc)) != 0) {
   2730  1.64.4.1    bouyer 			aprint_error_dev(sc->sc_dev,
   2731  1.64.4.1    bouyer 			    "could not cache the firmware\n");
   2732      1.52     skrll 			goto fail;
   2733      1.52     skrll 		}
   2734       1.1     skrll 	}
   2735       1.1     skrll 
   2736      1.12  christos 	iwi_stop(ifp, 0);
   2737      1.12  christos 
   2738       1.1     skrll 	if ((error = iwi_reset(sc)) != 0) {
   2739  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev, "could not reset adapter\n");
   2740       1.1     skrll 		goto fail;
   2741       1.1     skrll 	}
   2742       1.1     skrll 
   2743       1.1     skrll 	if ((error = iwi_load_firmware(sc, fw->boot, fw->boot_size)) != 0) {
   2744  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev, "could not load boot firmware\n");
   2745       1.1     skrll 		goto fail;
   2746       1.1     skrll 	}
   2747       1.1     skrll 
   2748       1.1     skrll 	if ((error = iwi_load_ucode(sc, fw->ucode, fw->ucode_size)) != 0) {
   2749  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev, "could not load microcode\n");
   2750       1.1     skrll 		goto fail;
   2751       1.1     skrll 	}
   2752       1.1     skrll 
   2753       1.1     skrll 	iwi_stop_master(sc);
   2754       1.1     skrll 
   2755      1.14     skrll 	CSR_WRITE_4(sc, IWI_CSR_CMD_BASE, sc->cmdq.desc_map->dm_segs[0].ds_addr);
   2756      1.14     skrll 	CSR_WRITE_4(sc, IWI_CSR_CMD_SIZE, sc->cmdq.count);
   2757      1.14     skrll 	CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
   2758      1.14     skrll 
   2759      1.38     skrll 	CSR_WRITE_4(sc, IWI_CSR_TX1_BASE, sc->txq[0].desc_map->dm_segs[0].ds_addr);
   2760      1.38     skrll 	CSR_WRITE_4(sc, IWI_CSR_TX1_SIZE, sc->txq[0].count);
   2761      1.38     skrll 	CSR_WRITE_4(sc, IWI_CSR_TX1_WIDX, sc->txq[0].cur);
   2762      1.38     skrll 
   2763      1.38     skrll 	CSR_WRITE_4(sc, IWI_CSR_TX2_BASE, sc->txq[1].desc_map->dm_segs[0].ds_addr);
   2764      1.38     skrll 	CSR_WRITE_4(sc, IWI_CSR_TX2_SIZE, sc->txq[1].count);
   2765      1.38     skrll 	CSR_WRITE_4(sc, IWI_CSR_TX2_WIDX, sc->txq[1].cur);
   2766      1.38     skrll 
   2767      1.38     skrll 	CSR_WRITE_4(sc, IWI_CSR_TX3_BASE, sc->txq[2].desc_map->dm_segs[0].ds_addr);
   2768      1.38     skrll 	CSR_WRITE_4(sc, IWI_CSR_TX3_SIZE, sc->txq[2].count);
   2769      1.38     skrll 	CSR_WRITE_4(sc, IWI_CSR_TX3_WIDX, sc->txq[2].cur);
   2770      1.38     skrll 
   2771      1.38     skrll 	CSR_WRITE_4(sc, IWI_CSR_TX4_BASE, sc->txq[3].desc_map->dm_segs[0].ds_addr);
   2772      1.38     skrll 	CSR_WRITE_4(sc, IWI_CSR_TX4_SIZE, sc->txq[3].count);
   2773      1.38     skrll 	CSR_WRITE_4(sc, IWI_CSR_TX4_WIDX, sc->txq[3].cur);
   2774       1.1     skrll 
   2775      1.14     skrll 	for (i = 0; i < sc->rxq.count; i++)
   2776       1.1     skrll 		CSR_WRITE_4(sc, IWI_CSR_RX_BASE + i * 4,
   2777      1.14     skrll 		    sc->rxq.data[i].map->dm_segs[0].ds_addr);
   2778       1.1     skrll 
   2779      1.14     skrll 	CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, sc->rxq.count -1);
   2780       1.1     skrll 
   2781       1.1     skrll 	if ((error = iwi_load_firmware(sc, fw->main, fw->main_size)) != 0) {
   2782  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev, "could not load main firmware\n");
   2783       1.1     skrll 		goto fail;
   2784       1.1     skrll 	}
   2785       1.1     skrll 
   2786       1.1     skrll 	sc->flags |= IWI_FLAG_FW_INITED;
   2787       1.1     skrll 
   2788       1.1     skrll 	if ((error = iwi_config(sc)) != 0) {
   2789  1.64.4.1    bouyer 		aprint_error_dev(sc->sc_dev, "device configuration failed\n");
   2790       1.1     skrll 		goto fail;
   2791       1.1     skrll 	}
   2792       1.1     skrll 
   2793      1.43     skrll 	ic->ic_state = IEEE80211_S_INIT;
   2794      1.43     skrll 
   2795      1.43     skrll 	ifp->if_flags &= ~IFF_OACTIVE;
   2796      1.43     skrll 	ifp->if_flags |= IFF_RUNNING;
   2797      1.43     skrll 
   2798      1.38     skrll 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
   2799      1.38     skrll 		if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
   2800      1.38     skrll 			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
   2801      1.38     skrll 	} else
   2802      1.12  christos 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
   2803       1.1     skrll 
   2804       1.1     skrll 	return 0;
   2805       1.1     skrll 
   2806      1.14     skrll fail:	ifp->if_flags &= ~IFF_UP;
   2807      1.14     skrll 	iwi_stop(ifp, 0);
   2808       1.1     skrll 
   2809       1.1     skrll 	return error;
   2810       1.1     skrll }
   2811       1.1     skrll 
   2812      1.54     skrll 
   2813      1.54     skrll /*
   2814      1.54     skrll  * Return whether or not the radio is enabled in hardware
   2815      1.54     skrll  * (i.e. the rfkill switch is "off").
   2816      1.54     skrll  */
   2817      1.54     skrll static int
   2818      1.54     skrll iwi_getrfkill(struct iwi_softc *sc)
   2819      1.54     skrll {
   2820      1.54     skrll 	return (CSR_READ_4(sc, IWI_CSR_IO) & IWI_IO_RADIO_ENABLED) == 0;
   2821      1.54     skrll }
   2822      1.54     skrll 
   2823      1.54     skrll static int
   2824      1.54     skrll iwi_sysctl_radio(SYSCTLFN_ARGS)
   2825      1.54     skrll {
   2826      1.54     skrll 	struct sysctlnode node;
   2827      1.54     skrll 	struct iwi_softc *sc;
   2828      1.54     skrll 	int val, error;
   2829      1.54     skrll 
   2830      1.54     skrll 	node = *rnode;
   2831      1.54     skrll 	sc = (struct iwi_softc *)node.sysctl_data;
   2832      1.54     skrll 
   2833      1.54     skrll 	val = !iwi_getrfkill(sc);
   2834      1.54     skrll 
   2835      1.54     skrll 	node.sysctl_data = &val;
   2836      1.54     skrll 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2837      1.54     skrll 
   2838      1.54     skrll 	if (error || newp == NULL)
   2839      1.54     skrll 		return error;
   2840      1.54     skrll 
   2841      1.54     skrll 	return 0;
   2842      1.54     skrll }
   2843      1.54     skrll 
   2844      1.54     skrll #ifdef IWI_DEBUG
   2845      1.54     skrll SYSCTL_SETUP(sysctl_iwi, "sysctl iwi(4) subtree setup")
   2846      1.54     skrll {
   2847      1.54     skrll 	int rc;
   2848      1.54     skrll 	const struct sysctlnode *rnode;
   2849      1.54     skrll 	const struct sysctlnode *cnode;
   2850      1.54     skrll 
   2851      1.54     skrll 	if ((rc = sysctl_createv(clog, 0, NULL, &rnode,
   2852      1.54     skrll 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
   2853      1.54     skrll 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0)
   2854      1.54     skrll 		goto err;
   2855      1.54     skrll 
   2856      1.54     skrll 	if ((rc = sysctl_createv(clog, 0, &rnode, &rnode,
   2857      1.54     skrll 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "iwi",
   2858      1.54     skrll 	    SYSCTL_DESCR("iwi global controls"),
   2859      1.54     skrll 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
   2860      1.54     skrll 		goto err;
   2861      1.54     skrll 
   2862      1.54     skrll 	/* control debugging printfs */
   2863      1.54     skrll 	if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
   2864      1.54     skrll 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
   2865      1.54     skrll 	    "debug", SYSCTL_DESCR("Enable debugging output"),
   2866      1.54     skrll 	    NULL, 0, &iwi_debug, 0, CTL_CREATE, CTL_EOL)) != 0)
   2867      1.54     skrll 		goto err;
   2868      1.54     skrll 
   2869      1.54     skrll 	return;
   2870      1.54     skrll err:
   2871      1.54     skrll 	aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
   2872      1.54     skrll }
   2873      1.54     skrll 
   2874      1.54     skrll #endif /* IWI_DEBUG */
   2875      1.54     skrll 
   2876      1.54     skrll /*
   2877      1.54     skrll  * Add sysctl knobs.
   2878      1.54     skrll  */
   2879      1.54     skrll static void
   2880      1.54     skrll iwi_sysctlattach(struct iwi_softc *sc)
   2881      1.54     skrll {
   2882      1.54     skrll 	int rc;
   2883      1.54     skrll 	const struct sysctlnode *rnode;
   2884      1.54     skrll 	const struct sysctlnode *cnode;
   2885      1.54     skrll 
   2886      1.54     skrll 	struct sysctllog **clog = &sc->sc_sysctllog;
   2887      1.54     skrll 
   2888      1.54     skrll 	if ((rc = sysctl_createv(clog, 0, NULL, &rnode,
   2889      1.54     skrll 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
   2890      1.54     skrll 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0)
   2891      1.54     skrll 		goto err;
   2892      1.54     skrll 
   2893      1.54     skrll 	if ((rc = sysctl_createv(clog, 0, &rnode, &rnode,
   2894  1.64.4.1    bouyer 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, device_xname(sc->sc_dev),
   2895      1.54     skrll 	    SYSCTL_DESCR("iwi controls and statistics"),
   2896      1.54     skrll 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
   2897      1.54     skrll 		goto err;
   2898      1.54     skrll 
   2899      1.54     skrll 	if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
   2900      1.54     skrll 	    CTLFLAG_PERMANENT, CTLTYPE_INT, "radio",
   2901      1.54     skrll 	    SYSCTL_DESCR("radio transmitter switch state (0=off, 1=on)"),
   2902      1.54     skrll 	    iwi_sysctl_radio, 0, sc, 0, CTL_CREATE, CTL_EOL)) != 0)
   2903      1.54     skrll 		goto err;
   2904      1.54     skrll 
   2905      1.54     skrll 	sc->dwelltime = 100;
   2906      1.54     skrll 	if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
   2907      1.54     skrll 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
   2908      1.54     skrll 	    "dwell", SYSCTL_DESCR("channel dwell time (ms) for AP/station scanning"),
   2909      1.54     skrll 	    NULL, 0, &sc->dwelltime, 0, CTL_CREATE, CTL_EOL)) != 0)
   2910      1.54     skrll 		goto err;
   2911      1.54     skrll 
   2912      1.59     skrll 	sc->bluetooth = 0;
   2913      1.54     skrll 	if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
   2914      1.54     skrll 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
   2915      1.54     skrll 	    "bluetooth", SYSCTL_DESCR("bluetooth coexistence"),
   2916      1.54     skrll 	    NULL, 0, &sc->bluetooth, 0, CTL_CREATE, CTL_EOL)) != 0)
   2917      1.54     skrll 		goto err;
   2918      1.54     skrll 
   2919      1.59     skrll 	sc->antenna = IWI_ANTENNA_AUTO;
   2920      1.54     skrll 	if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
   2921      1.54     skrll 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
   2922      1.54     skrll 	    "antenna", SYSCTL_DESCR("antenna (0=auto)"),
   2923      1.54     skrll 	    NULL, 0, &sc->antenna, 0, CTL_CREATE, CTL_EOL)) != 0)
   2924      1.54     skrll 		goto err;
   2925      1.54     skrll 
   2926      1.54     skrll 	return;
   2927      1.54     skrll err:
   2928      1.54     skrll 	aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
   2929      1.54     skrll }
   2930      1.54     skrll 
   2931       1.1     skrll static void
   2932      1.58  christos iwi_stop(struct ifnet *ifp, int disable)
   2933       1.1     skrll {
   2934       1.1     skrll 	struct iwi_softc *sc = ifp->if_softc;
   2935       1.1     skrll 	struct ieee80211com *ic = &sc->sc_ic;
   2936       1.1     skrll 
   2937      1.42    rpaulo 	IWI_LED_OFF(sc);
   2938      1.42    rpaulo 
   2939       1.1     skrll 	iwi_stop_master(sc);
   2940       1.1     skrll 	CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_SW_RESET);
   2941       1.1     skrll 
   2942      1.14     skrll 	/* reset rings */
   2943      1.14     skrll 	iwi_reset_cmd_ring(sc, &sc->cmdq);
   2944      1.38     skrll 	iwi_reset_tx_ring(sc, &sc->txq[0]);
   2945      1.38     skrll 	iwi_reset_tx_ring(sc, &sc->txq[1]);
   2946      1.38     skrll 	iwi_reset_tx_ring(sc, &sc->txq[2]);
   2947      1.38     skrll 	iwi_reset_tx_ring(sc, &sc->txq[3]);
   2948      1.14     skrll 	iwi_reset_rx_ring(sc, &sc->rxq);
   2949       1.1     skrll 
   2950       1.1     skrll 	ifp->if_timer = 0;
   2951       1.1     skrll 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   2952       1.1     skrll 
   2953       1.1     skrll 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
   2954       1.1     skrll }
   2955      1.36     skrll 
   2956      1.36     skrll static void
   2957      1.42    rpaulo iwi_led_set(struct iwi_softc *sc, uint32_t state, int toggle)
   2958      1.42    rpaulo {
   2959      1.42    rpaulo 	uint32_t val;
   2960      1.42    rpaulo 
   2961      1.42    rpaulo 	val = MEM_READ_4(sc, IWI_MEM_EVENT_CTL);
   2962      1.42    rpaulo 
   2963      1.42    rpaulo 	switch (sc->nictype) {
   2964      1.42    rpaulo 	case 1:
   2965      1.42    rpaulo 		/* special NIC type: reversed leds */
   2966      1.42    rpaulo 		if (state == IWI_LED_ACTIVITY) {
   2967      1.42    rpaulo 			state &= ~IWI_LED_ACTIVITY;
   2968      1.42    rpaulo 			state |= IWI_LED_ASSOCIATED;
   2969      1.42    rpaulo 		} else if (state == IWI_LED_ASSOCIATED) {
   2970      1.42    rpaulo 			state &= ~IWI_LED_ASSOCIATED;
   2971      1.42    rpaulo 			state |= IWI_LED_ACTIVITY;
   2972      1.42    rpaulo 		}
   2973      1.42    rpaulo 		/* and ignore toggle effect */
   2974      1.42    rpaulo 		val |= state;
   2975      1.42    rpaulo 		break;
   2976      1.42    rpaulo 	case 0:
   2977      1.42    rpaulo 	case 2:
   2978      1.42    rpaulo 	case 3:
   2979      1.42    rpaulo 	case 4:
   2980      1.42    rpaulo 		val = (toggle && (val & state)) ? val & ~state : val | state;
   2981      1.42    rpaulo 		break;
   2982      1.42    rpaulo 	default:
   2983  1.64.4.1    bouyer 		aprint_normal_dev(sc->sc_dev, "unknown NIC type %d\n",
   2984  1.64.4.1    bouyer 		    sc->nictype);
   2985      1.42    rpaulo 		return;
   2986      1.42    rpaulo 		break;
   2987      1.42    rpaulo 	}
   2988      1.42    rpaulo 
   2989      1.42    rpaulo 	MEM_WRITE_4(sc, IWI_MEM_EVENT_CTL, val);
   2990      1.42    rpaulo 
   2991      1.42    rpaulo 	return;
   2992      1.42    rpaulo }
   2993