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