Home | History | Annotate | Line # | Download | only in pci
if_wpi.c revision 1.11
      1  1.11     pooka /*  $NetBSD: if_wpi.c,v 1.11 2007/07/04 23:18:49 pooka Exp $    */
      2   1.1    simonb 
      3   1.1    simonb /*-
      4   1.1    simonb  * Copyright (c) 2006
      5   1.1    simonb  *	Damien Bergamini <damien.bergamini (at) free.fr>
      6   1.1    simonb  *
      7   1.1    simonb  * Permission to use, copy, modify, and distribute this software for any
      8   1.1    simonb  * purpose with or without fee is hereby granted, provided that the above
      9   1.1    simonb  * copyright notice and this permission notice appear in all copies.
     10   1.1    simonb  *
     11   1.1    simonb  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12   1.1    simonb  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13   1.1    simonb  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14   1.1    simonb  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15   1.1    simonb  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16   1.1    simonb  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17   1.1    simonb  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18   1.1    simonb  */
     19   1.1    simonb 
     20   1.1    simonb #include <sys/cdefs.h>
     21  1.11     pooka __KERNEL_RCSID(0, "$NetBSD: if_wpi.c,v 1.11 2007/07/04 23:18:49 pooka Exp $");
     22   1.1    simonb 
     23   1.1    simonb /*
     24   1.1    simonb  * Driver for Intel PRO/Wireless 3945ABG 802.11 network adapters.
     25   1.1    simonb  */
     26   1.1    simonb 
     27   1.1    simonb #include "bpfilter.h"
     28   1.1    simonb 
     29   1.1    simonb #include <sys/param.h>
     30   1.1    simonb #include <sys/sockio.h>
     31   1.1    simonb #include <sys/sysctl.h>
     32   1.1    simonb #include <sys/mbuf.h>
     33   1.1    simonb #include <sys/kernel.h>
     34   1.1    simonb #include <sys/socket.h>
     35   1.1    simonb #include <sys/systm.h>
     36   1.1    simonb #include <sys/malloc.h>
     37   1.1    simonb #include <sys/conf.h>
     38   1.1    simonb #include <sys/kauth.h>
     39   1.7  degroote #include <sys/callout.h>
     40   1.1    simonb 
     41   1.1    simonb #include <machine/bus.h>
     42   1.1    simonb #include <machine/endian.h>
     43   1.1    simonb #include <machine/intr.h>
     44   1.1    simonb 
     45   1.1    simonb #include <dev/pci/pcireg.h>
     46   1.1    simonb #include <dev/pci/pcivar.h>
     47   1.1    simonb #include <dev/pci/pcidevs.h>
     48   1.1    simonb 
     49   1.1    simonb #if NBPFILTER > 0
     50   1.1    simonb #include <net/bpf.h>
     51   1.1    simonb #endif
     52   1.1    simonb #include <net/if.h>
     53   1.1    simonb #include <net/if_arp.h>
     54   1.1    simonb #include <net/if_dl.h>
     55   1.1    simonb #include <net/if_ether.h>
     56   1.1    simonb #include <net/if_media.h>
     57   1.1    simonb #include <net/if_types.h>
     58   1.1    simonb 
     59   1.1    simonb #include <net80211/ieee80211_var.h>
     60   1.5     joerg #include <net80211/ieee80211_amrr.h>
     61   1.1    simonb #include <net80211/ieee80211_radiotap.h>
     62   1.1    simonb 
     63   1.1    simonb #include <netinet/in.h>
     64   1.1    simonb #include <netinet/in_systm.h>
     65   1.1    simonb #include <netinet/in_var.h>
     66   1.1    simonb #include <netinet/ip.h>
     67   1.1    simonb 
     68   1.1    simonb #include <dev/firmload.h>
     69   1.1    simonb 
     70   1.1    simonb #include <dev/pci/if_wpireg.h>
     71   1.1    simonb #include <dev/pci/if_wpivar.h>
     72   1.1    simonb 
     73   1.1    simonb #ifdef WPI_DEBUG
     74   1.1    simonb #define DPRINTF(x)	if (wpi_debug > 0) printf x
     75   1.1    simonb #define DPRINTFN(n, x)	if (wpi_debug >= (n)) printf x
     76   1.1    simonb int wpi_debug = 1;
     77   1.1    simonb #else
     78   1.1    simonb #define DPRINTF(x)
     79   1.1    simonb #define DPRINTFN(n, x)
     80   1.1    simonb #endif
     81   1.1    simonb 
     82   1.1    simonb /*
     83   1.1    simonb  * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
     84   1.1    simonb  */
     85   1.1    simonb static const struct ieee80211_rateset wpi_rateset_11a =
     86   1.1    simonb 	{ 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
     87   1.1    simonb 
     88   1.1    simonb static const struct ieee80211_rateset wpi_rateset_11b =
     89   1.1    simonb 	{ 4, { 2, 4, 11, 22 } };
     90   1.1    simonb 
     91   1.1    simonb static const struct ieee80211_rateset wpi_rateset_11g =
     92   1.1    simonb 	{ 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
     93   1.1    simonb 
     94   1.1    simonb static const uint8_t wpi_ridx_to_plcp[] = {
     95   1.1    simonb 	0xd, 0xf, 0x5, 0x7, 0x9, 0xb, 0x1, 0x3,	/* OFDM R1-R4 */
     96   1.1    simonb 	10, 20, 55, 110	/* CCK */
     97   1.1    simonb };
     98   1.1    simonb 
     99   1.1    simonb static int  wpi_match(struct device *, struct cfdata *, void *);
    100   1.1    simonb static void wpi_attach(struct device *, struct device *, void *);
    101   1.1    simonb static int  wpi_detach(struct device*, int);
    102   1.1    simonb static void wpi_power(int, void *);
    103   1.7  degroote static int  wpi_dma_contig_alloc(bus_dma_tag_t, struct wpi_dma_info *,
    104   1.1    simonb 	void **, bus_size_t, bus_size_t, int);
    105   1.7  degroote static void wpi_dma_contig_free(struct wpi_dma_info *);
    106   1.1    simonb static int  wpi_alloc_shared(struct wpi_softc *);
    107   1.1    simonb static void wpi_free_shared(struct wpi_softc *);
    108   1.7  degroote static struct wpi_rbuf *wpi_alloc_rbuf(struct wpi_softc *);
    109   1.9  christos static void wpi_free_rbuf(struct mbuf *, void *, size_t, void *);
    110   1.7  degroote static int  wpi_alloc_rpool(struct wpi_softc *);
    111   1.7  degroote static void wpi_free_rpool(struct wpi_softc *);
    112   1.1    simonb static int  wpi_alloc_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
    113   1.1    simonb static void wpi_reset_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
    114   1.1    simonb static void wpi_free_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
    115   1.1    simonb static int  wpi_alloc_tx_ring(struct wpi_softc *, struct wpi_tx_ring *, int,
    116   1.1    simonb 	int);
    117   1.1    simonb static void wpi_reset_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
    118   1.1    simonb static void wpi_free_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
    119   1.1    simonb static struct ieee80211_node * wpi_node_alloc(struct ieee80211_node_table *);
    120   1.1    simonb static int  wpi_media_change(struct ifnet *);
    121   1.1    simonb static int  wpi_newstate(struct ieee80211com *, enum ieee80211_state, int);
    122   1.1    simonb static void wpi_mem_lock(struct wpi_softc *);
    123   1.1    simonb static void wpi_mem_unlock(struct wpi_softc *);
    124   1.1    simonb static uint32_t wpi_mem_read(struct wpi_softc *, uint16_t);
    125   1.1    simonb static void wpi_mem_write(struct wpi_softc *, uint16_t, uint32_t);
    126   1.1    simonb static void wpi_mem_write_region_4(struct wpi_softc *, uint16_t,
    127   1.1    simonb 	const uint32_t *, int);
    128   1.1    simonb static uint16_t wpi_read_prom_word(struct wpi_softc *, uint32_t);
    129   1.1    simonb static int  wpi_load_firmware(struct wpi_softc *, uint32_t, const char *,
    130   1.1    simonb 	int);
    131   1.1    simonb static void wpi_rx_intr(struct wpi_softc *, struct wpi_rx_desc *,
    132   1.1    simonb 	struct wpi_rx_data *);
    133   1.1    simonb static void wpi_tx_intr(struct wpi_softc *, struct wpi_rx_desc *);
    134   1.1    simonb static void wpi_cmd_intr(struct wpi_softc *, struct wpi_rx_desc *);
    135   1.1    simonb static void wpi_notif_intr(struct wpi_softc *);
    136   1.1    simonb static int  wpi_intr(void *);
    137   1.1    simonb static uint8_t wpi_plcp_signal(int);
    138   1.1    simonb static int  wpi_tx_data(struct wpi_softc *, struct mbuf *,
    139   1.1    simonb 	struct ieee80211_node *, int);
    140   1.1    simonb static void wpi_start(struct ifnet *);
    141   1.1    simonb static void wpi_watchdog(struct ifnet *);
    142   1.9  christos static int  wpi_ioctl(struct ifnet *, u_long, void *);
    143   1.1    simonb static void wpi_read_eeprom(struct wpi_softc *);
    144   1.1    simonb static int  wpi_cmd(struct wpi_softc *, int, const void *, int, int);
    145   1.1    simonb static int  wpi_wme_update(struct ieee80211com *);
    146   1.1    simonb static int  wpi_mrr_setup(struct wpi_softc *);
    147   1.1    simonb static void wpi_set_led(struct wpi_softc *, uint8_t, uint8_t, uint8_t);
    148   1.1    simonb static void wpi_enable_tsf(struct wpi_softc *, struct ieee80211_node *);
    149   1.1    simonb static int  wpi_setup_beacon(struct wpi_softc *, struct ieee80211_node *);
    150   1.1    simonb static int  wpi_auth(struct wpi_softc *);
    151   1.1    simonb static int  wpi_scan(struct wpi_softc *, uint16_t);
    152   1.1    simonb static int  wpi_config(struct wpi_softc *);
    153   1.1    simonb static void wpi_stop_master(struct wpi_softc *);
    154   1.1    simonb static int  wpi_power_up(struct wpi_softc *);
    155   1.1    simonb static int  wpi_reset(struct wpi_softc *);
    156   1.1    simonb static void wpi_hw_config(struct wpi_softc *);
    157   1.1    simonb static int  wpi_init(struct ifnet *);
    158   1.1    simonb static void wpi_stop(struct ifnet *, int);
    159   1.1    simonb 
    160   1.1    simonb /* rate control algorithm: should be moved to net80211 */
    161   1.5     joerg static void wpi_iter_func(void *, struct ieee80211_node *);
    162   1.1    simonb static void wpi_amrr_timeout(void *);
    163   1.7  degroote static void wpi_newassoc(struct ieee80211_node *, int);
    164   1.1    simonb 
    165   1.1    simonb CFATTACH_DECL(wpi, sizeof (struct wpi_softc), wpi_match, wpi_attach,
    166   1.1    simonb 	wpi_detach, NULL);
    167   1.1    simonb 
    168   1.1    simonb static int
    169   1.7  degroote wpi_match(struct device *parent, struct cfdata *match __unused, void *aux)
    170   1.1    simonb {
    171   1.1    simonb 	struct pci_attach_args *pa = aux;
    172   1.1    simonb 
    173   1.1    simonb 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
    174   1.1    simonb 		return 0;
    175   1.1    simonb 
    176   1.1    simonb 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_3945ABG_1 ||
    177   1.7  degroote 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_3945ABG_2)
    178   1.1    simonb 		return 1;
    179   1.1    simonb 
    180   1.1    simonb 	return 0;
    181   1.1    simonb }
    182   1.1    simonb 
    183   1.1    simonb /* Base Address Register */
    184   1.1    simonb #define WPI_PCI_BAR0	0x10
    185   1.1    simonb 
    186   1.1    simonb static void
    187   1.7  degroote wpi_attach(struct device *parent __unused, struct device *self, void *aux)
    188   1.1    simonb {
    189   1.1    simonb 	struct wpi_softc *sc = (struct wpi_softc *)self;
    190   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
    191   1.1    simonb 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    192   1.1    simonb 	struct pci_attach_args *pa = aux;
    193   1.1    simonb 	const char *intrstr;
    194   1.1    simonb 	char devinfo[256];
    195   1.1    simonb 	bus_space_tag_t memt;
    196   1.1    simonb 	bus_space_handle_t memh;
    197   1.1    simonb 	pci_intr_handle_t ih;
    198   1.1    simonb 	pcireg_t data;
    199   1.1    simonb 	int error, ac, revision, i;
    200   1.1    simonb 
    201   1.1    simonb 	sc->sc_pct = pa->pa_pc;
    202   1.1    simonb 	sc->sc_pcitag = pa->pa_tag;
    203   1.1    simonb 
    204   1.1    simonb 	callout_init(&sc->amrr_ch);
    205   1.1    simonb 
    206   1.1    simonb 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof devinfo);
    207   1.1    simonb 	revision = PCI_REVISION(pa->pa_class);
    208   1.1    simonb 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo, revision);
    209   1.1    simonb 
    210   1.1    simonb 	/* clear device specific PCI configuration register 0x41 */
    211   1.1    simonb 	data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
    212   1.1    simonb 	data &= ~0x0000ff00;
    213   1.1    simonb 	pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data);
    214   1.1    simonb 
    215   1.1    simonb 	/* enable bus-mastering */
    216   1.1    simonb 	data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
    217   1.1    simonb 	data |= PCI_COMMAND_MASTER_ENABLE;
    218   1.1    simonb 	pci_conf_write(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG, data);
    219   1.1    simonb 
    220   1.1    simonb 	/* map the register window */
    221   1.1    simonb 	error = pci_mapreg_map(pa, WPI_PCI_BAR0, PCI_MAPREG_TYPE_MEM |
    222   1.7  degroote 		PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh, NULL, &sc->sc_sz);
    223   1.1    simonb 	if (error != 0) {
    224   1.1    simonb 		aprint_error("%s: could not map memory space\n",
    225   1.1    simonb 			sc->sc_dev.dv_xname);
    226   1.1    simonb 		return;
    227   1.1    simonb 	}
    228   1.1    simonb 
    229   1.1    simonb 	sc->sc_st = memt;
    230   1.1    simonb 	sc->sc_sh = memh;
    231   1.1    simonb 	sc->sc_dmat = pa->pa_dmat;
    232   1.1    simonb 
    233   1.1    simonb 	if (pci_intr_map(pa, &ih) != 0) {
    234   1.1    simonb 		aprint_error("%s: could not map interrupt\n",
    235   1.1    simonb 			sc->sc_dev.dv_xname);
    236   1.1    simonb 		return;
    237   1.1    simonb 	}
    238   1.1    simonb 
    239   1.1    simonb 	intrstr = pci_intr_string(sc->sc_pct, ih);
    240   1.1    simonb 	sc->sc_ih = pci_intr_establish(sc->sc_pct, ih, IPL_NET, wpi_intr, sc);
    241   1.1    simonb 	if (sc->sc_ih == NULL) {
    242   1.1    simonb 		aprint_error("%s: could not establish interrupt",
    243   1.1    simonb 			sc->sc_dev.dv_xname);
    244   1.1    simonb 		if (intrstr != NULL)
    245   1.1    simonb 			aprint_error(" at %s", intrstr);
    246   1.1    simonb 		aprint_error("\n");
    247   1.1    simonb 		return;
    248   1.1    simonb 	}
    249   1.1    simonb 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    250   1.1    simonb 
    251   1.1    simonb 	if (wpi_reset(sc) != 0) {
    252   1.1    simonb 		aprint_error("%s: could not reset adapter\n",
    253   1.1    simonb 			sc->sc_dev.dv_xname);
    254   1.1    simonb 		return;
    255   1.1    simonb 	}
    256   1.1    simonb 
    257   1.1    simonb 	/*
    258   1.1    simonb 	 * Allocate shared page and Tx/Rx rings.
    259   1.1    simonb 	 */
    260   1.1    simonb 	if ((error = wpi_alloc_shared(sc)) != 0) {
    261   1.1    simonb 		aprint_error("%s: could not allocate shared area\n",
    262   1.1    simonb 			sc->sc_dev.dv_xname);
    263   1.1    simonb 		return;
    264   1.1    simonb 	}
    265   1.1    simonb 
    266   1.7  degroote 	if ((error = wpi_alloc_rpool(sc)) != 0) {
    267   1.7  degroote 		aprint_error("%s: could not allocate Rx buffers\n",
    268   1.7  degroote 			sc->sc_dev.dv_xname);
    269   1.7  degroote 		goto fail1;
    270   1.7  degroote 	}
    271   1.7  degroote 
    272   1.1    simonb 	for (ac = 0; ac < 4; ac++) {
    273   1.1    simonb 		error = wpi_alloc_tx_ring(sc, &sc->txq[ac], WPI_TX_RING_COUNT, ac);
    274   1.1    simonb 		if (error != 0) {
    275   1.1    simonb 			aprint_error("%s: could not allocate Tx ring %d\n",
    276   1.1    simonb 					sc->sc_dev.dv_xname, ac);
    277   1.7  degroote 			goto fail2;
    278   1.1    simonb 		}
    279   1.1    simonb 	}
    280   1.1    simonb 
    281   1.1    simonb 	error = wpi_alloc_tx_ring(sc, &sc->cmdq, WPI_CMD_RING_COUNT, 4);
    282   1.1    simonb 	if (error != 0) {
    283   1.1    simonb 		aprint_error("%s: could not allocate command ring\n",
    284   1.1    simonb 			sc->sc_dev.dv_xname);
    285   1.7  degroote 		goto fail2;
    286   1.1    simonb 	}
    287   1.1    simonb 
    288   1.1    simonb 	error = wpi_alloc_tx_ring(sc, &sc->svcq, WPI_SVC_RING_COUNT, 5);
    289   1.1    simonb 	if (error != 0) {
    290   1.1    simonb 		aprint_error("%s: could not allocate service ring\n",
    291   1.1    simonb 			sc->sc_dev.dv_xname);
    292   1.7  degroote 		goto fail3;
    293   1.1    simonb 	}
    294   1.1    simonb 
    295   1.1    simonb 	if (wpi_alloc_rx_ring(sc, &sc->rxq) != 0) {
    296   1.1    simonb 		aprint_error("%s: could not allocate Rx ring\n",
    297   1.1    simonb 			sc->sc_dev.dv_xname);
    298   1.7  degroote 		goto fail4;
    299   1.1    simonb 	}
    300   1.1    simonb 
    301   1.1    simonb 	ic->ic_ifp = ifp;
    302   1.1    simonb 	ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
    303   1.1    simonb 	ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
    304   1.1    simonb 	ic->ic_state = IEEE80211_S_INIT;
    305   1.1    simonb 
    306   1.1    simonb 	/* set device capabilities */
    307   1.1    simonb 	ic->ic_caps =
    308   1.1    simonb 		IEEE80211_C_IBSS |       /* IBSS mode support */
    309   1.1    simonb 		IEEE80211_C_WPA |        /* 802.11i */
    310   1.1    simonb 		IEEE80211_C_MONITOR |    /* monitor mode supported */
    311   1.1    simonb 		IEEE80211_C_TXPMGT |     /* tx power management */
    312   1.1    simonb 		IEEE80211_C_SHSLOT |     /* short slot time supported */
    313   1.1    simonb 		IEEE80211_C_SHPREAMBLE | /* short preamble supported */
    314   1.1    simonb 		IEEE80211_C_WME;         /* 802.11e */
    315   1.1    simonb 
    316   1.1    simonb 	wpi_read_eeprom(sc);
    317   1.1    simonb 	aprint_normal("%s: 802.11 address %s\n", sc->sc_dev.dv_xname,
    318   1.1    simonb 		ether_sprintf(ic->ic_myaddr));
    319   1.1    simonb 
    320   1.7  degroote 	/* set supported .11a rates */
    321   1.7  degroote 	ic->ic_sup_rates[IEEE80211_MODE_11A] = wpi_rateset_11a;
    322   1.1    simonb 
    323   1.7  degroote 	/* set supported .11a channels */
    324   1.7  degroote 	for (i = 36; i <= 64; i += 4) {
    325   1.7  degroote 		ic->ic_channels[i].ic_freq =
    326   1.7  degroote 			ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
    327   1.7  degroote 		ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
    328   1.7  degroote 	}
    329   1.7  degroote 	for (i = 100; i <= 140; i += 4) {
    330   1.7  degroote 		ic->ic_channels[i].ic_freq =
    331   1.7  degroote 			ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
    332   1.7  degroote 		ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
    333   1.7  degroote 	}
    334   1.7  degroote 	for (i = 149; i <= 165; i += 4) {
    335   1.7  degroote 		ic->ic_channels[i].ic_freq =
    336   1.7  degroote 			ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
    337   1.7  degroote 		ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
    338   1.7  degroote 	}
    339   1.1    simonb 
    340   1.1    simonb 	/* set supported .11b and .11g rates */
    341   1.1    simonb 	ic->ic_sup_rates[IEEE80211_MODE_11B] = wpi_rateset_11b;
    342   1.1    simonb 	ic->ic_sup_rates[IEEE80211_MODE_11G] = wpi_rateset_11g;
    343   1.1    simonb 
    344   1.1    simonb 	/* set supported .11b and .11g channels (1 through 14) */
    345   1.1    simonb 	for (i = 1; i <= 14; i++) {
    346   1.1    simonb 		ic->ic_channels[i].ic_freq =
    347   1.1    simonb 			ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
    348   1.1    simonb 		ic->ic_channels[i].ic_flags =
    349   1.1    simonb 			IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
    350   1.1    simonb 			IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
    351   1.1    simonb 	}
    352   1.1    simonb 
    353   1.1    simonb 	ic->ic_ibss_chan = &ic->ic_channels[0];
    354   1.1    simonb 
    355   1.1    simonb 	ifp->if_softc = sc;
    356   1.1    simonb 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    357   1.1    simonb 	ifp->if_init = wpi_init;
    358   1.1    simonb 	ifp->if_stop = wpi_stop;
    359   1.1    simonb 	ifp->if_ioctl = wpi_ioctl;
    360   1.1    simonb 	ifp->if_start = wpi_start;
    361   1.1    simonb 	ifp->if_watchdog = wpi_watchdog;
    362   1.1    simonb 	IFQ_SET_READY(&ifp->if_snd);
    363   1.1    simonb 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
    364   1.1    simonb 
    365   1.1    simonb 	if_attach(ifp);
    366   1.1    simonb 	ieee80211_ifattach(ic);
    367   1.1    simonb 	/* override default methods */
    368   1.1    simonb 	ic->ic_node_alloc = wpi_node_alloc;
    369   1.5     joerg 	ic->ic_newassoc = wpi_newassoc;
    370   1.1    simonb 	ic->ic_wme.wme_update = wpi_wme_update;
    371   1.1    simonb 
    372   1.1    simonb 	/* override state transition machine */
    373   1.1    simonb 	sc->sc_newstate = ic->ic_newstate;
    374   1.1    simonb 	ic->ic_newstate = wpi_newstate;
    375   1.1    simonb 	ieee80211_media_init(ic, wpi_media_change, ieee80211_media_status);
    376   1.1    simonb 
    377   1.5     joerg 	sc->amrr.amrr_min_success_threshold = 1;
    378   1.5     joerg 	sc->amrr.amrr_max_success_threshold = 15;
    379   1.5     joerg 
    380   1.1    simonb 	/* set powerhook */
    381   1.3  jmcneill 	sc->powerhook = powerhook_establish(sc->sc_dev.dv_xname, wpi_power, sc);
    382   1.1    simonb 
    383   1.1    simonb #if NBPFILTER > 0
    384   1.1    simonb 	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
    385   1.1    simonb 		sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
    386   1.1    simonb 		&sc->sc_drvbpf);
    387   1.1    simonb 
    388   1.1    simonb 	sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
    389   1.1    simonb 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
    390   1.1    simonb 	sc->sc_rxtap.wr_ihdr.it_present = htole32(WPI_RX_RADIOTAP_PRESENT);
    391   1.1    simonb 
    392   1.1    simonb 	sc->sc_txtap_len = sizeof sc->sc_txtapu;
    393   1.1    simonb 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
    394   1.1    simonb 	sc->sc_txtap.wt_ihdr.it_present = htole32(WPI_TX_RADIOTAP_PRESENT);
    395   1.1    simonb #endif
    396   1.1    simonb 
    397   1.1    simonb 	ieee80211_announce(ic);
    398   1.1    simonb 
    399   1.1    simonb 	return;
    400   1.1    simonb 
    401   1.7  degroote fail4:  wpi_free_tx_ring(sc, &sc->svcq);
    402   1.7  degroote fail3:  wpi_free_tx_ring(sc, &sc->cmdq);
    403   1.7  degroote fail2:  while (--ac >= 0)
    404   1.1    simonb 			wpi_free_tx_ring(sc, &sc->txq[ac]);
    405   1.7  degroote 	wpi_free_rpool(sc);
    406   1.7  degroote fail1:	wpi_free_shared(sc);
    407   1.1    simonb }
    408   1.1    simonb 
    409   1.1    simonb static int
    410   1.7  degroote wpi_detach(struct device* self, int flags __unused)
    411   1.1    simonb {
    412   1.1    simonb 	struct wpi_softc *sc = (struct wpi_softc *)self;
    413   1.7  degroote 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
    414   1.1    simonb 	int ac;
    415   1.1    simonb 
    416   1.1    simonb 	wpi_stop(ifp, 1);
    417   1.1    simonb 
    418   1.1    simonb #if NBPFILTER > 0
    419   1.1    simonb 	if (ifp != NULL)
    420   1.1    simonb 		bpfdetach(ifp);
    421   1.1    simonb #endif
    422   1.1    simonb 	ieee80211_ifdetach(&sc->sc_ic);
    423   1.1    simonb 	if (ifp != NULL)
    424   1.1    simonb 		if_detach(ifp);
    425   1.1    simonb 
    426   1.1    simonb 	for (ac = 0; ac < 4; ac++)
    427   1.1    simonb 		wpi_free_tx_ring(sc, &sc->txq[ac]);
    428   1.1    simonb 	wpi_free_tx_ring(sc, &sc->cmdq);
    429   1.1    simonb 	wpi_free_tx_ring(sc, &sc->svcq);
    430   1.1    simonb 	wpi_free_rx_ring(sc, &sc->rxq);
    431   1.7  degroote 	wpi_free_rpool(sc);
    432   1.1    simonb 	wpi_free_shared(sc);
    433   1.1    simonb 
    434   1.1    simonb 	if (sc->sc_ih != NULL) {
    435   1.1    simonb 		pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
    436   1.1    simonb 		sc->sc_ih = NULL;
    437   1.1    simonb 	}
    438   1.1    simonb 
    439   1.1    simonb 	bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz);
    440   1.1    simonb 
    441   1.1    simonb 	return 0;
    442   1.1    simonb }
    443   1.1    simonb 
    444   1.1    simonb static void
    445   1.1    simonb wpi_power(int why, void *arg)
    446   1.1    simonb {
    447   1.1    simonb 	struct wpi_softc *sc = arg;
    448   1.1    simonb 	struct ifnet *ifp;
    449   1.1    simonb 	pcireg_t data;
    450   1.1    simonb 	int s;
    451   1.1    simonb 
    452   1.1    simonb 	if (why != PWR_RESUME)
    453   1.1    simonb 		return;
    454   1.1    simonb 
    455   1.1    simonb 	/* clear device specific PCI configuration register 0x41 */
    456   1.1    simonb 	data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
    457   1.1    simonb 	data &= ~0x0000ff00;
    458   1.1    simonb 	pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data);
    459   1.1    simonb 
    460   1.1    simonb 	s = splnet();
    461   1.1    simonb 	ifp = sc->sc_ic.ic_ifp;
    462   1.1    simonb 	if (ifp->if_flags & IFF_UP) {
    463   1.1    simonb 		ifp->if_init(ifp);
    464   1.1    simonb 		if (ifp->if_flags & IFF_RUNNING)
    465   1.1    simonb 			ifp->if_start(ifp);
    466   1.1    simonb 	}
    467   1.1    simonb 	splx(s);
    468   1.1    simonb }
    469   1.1    simonb 
    470   1.1    simonb static int
    471   1.7  degroote wpi_dma_contig_alloc(bus_dma_tag_t tag, struct wpi_dma_info *dma,
    472   1.1    simonb 	void **kvap, bus_size_t size, bus_size_t alignment, int flags)
    473   1.1    simonb {
    474   1.1    simonb 	int nsegs, error;
    475   1.1    simonb 
    476   1.7  degroote 	dma->tag = tag;
    477   1.1    simonb 	dma->size = size;
    478   1.1    simonb 
    479   1.7  degroote 	error = bus_dmamap_create(tag, size, 1, size, 0, flags, &dma->map);
    480   1.7  degroote 	if (error != 0)
    481   1.1    simonb 		goto fail;
    482   1.1    simonb 
    483   1.7  degroote 	error = bus_dmamem_alloc(tag, size, alignment, 0, &dma->seg, 1, &nsegs,
    484   1.7  degroote 	    flags);
    485   1.7  degroote 	if (error != 0)
    486   1.1    simonb 		goto fail;
    487   1.1    simonb 
    488   1.7  degroote 	error = bus_dmamem_map(tag, &dma->seg, 1, size, &dma->vaddr, flags);
    489   1.7  degroote 	if (error != 0)
    490   1.1    simonb 		goto fail;
    491   1.1    simonb 
    492   1.7  degroote 	error = bus_dmamap_load(tag, dma->map, dma->vaddr, size, NULL, flags);
    493   1.7  degroote 	if (error != 0)
    494   1.1    simonb 		goto fail;
    495   1.1    simonb 
    496   1.1    simonb 	memset(dma->vaddr, 0, size);
    497   1.1    simonb 
    498   1.1    simonb 	dma->paddr = dma->map->dm_segs[0].ds_addr;
    499   1.7  degroote 	if (kvap != NULL)
    500   1.7  degroote 		*kvap = dma->vaddr;
    501   1.1    simonb 
    502   1.1    simonb 	return 0;
    503   1.1    simonb 
    504   1.7  degroote fail:   wpi_dma_contig_free(dma);
    505   1.1    simonb 	return error;
    506   1.1    simonb }
    507   1.1    simonb 
    508   1.1    simonb static void
    509   1.7  degroote wpi_dma_contig_free(struct wpi_dma_info *dma)
    510   1.1    simonb {
    511   1.1    simonb 	if (dma->map != NULL) {
    512   1.1    simonb 		if (dma->vaddr != NULL) {
    513   1.7  degroote 			bus_dmamap_unload(dma->tag, dma->map);
    514   1.7  degroote 			bus_dmamem_unmap(dma->tag, dma->vaddr, dma->size);
    515   1.7  degroote 			bus_dmamem_free(dma->tag, &dma->seg, 1);
    516   1.1    simonb 			dma->vaddr = NULL;
    517   1.1    simonb 		}
    518   1.7  degroote 		bus_dmamap_destroy(dma->tag, dma->map);
    519   1.1    simonb 		dma->map = NULL;
    520   1.1    simonb 	}
    521   1.1    simonb }
    522   1.1    simonb 
    523   1.1    simonb /*
    524   1.1    simonb  * Allocate a shared page between host and NIC.
    525   1.1    simonb  */
    526   1.1    simonb static int
    527   1.1    simonb wpi_alloc_shared(struct wpi_softc *sc)
    528   1.1    simonb {
    529   1.1    simonb 	int error;
    530   1.1    simonb 	/* must be aligned on a 4K-page boundary */
    531   1.7  degroote 	error = wpi_dma_contig_alloc(sc->sc_dmat, &sc->shared_dma,
    532   1.7  degroote 			(void **)&sc->shared, sizeof (struct wpi_shared), WPI_BUF_ALIGN
    533   1.7  degroote             ,BUS_DMA_NOWAIT);
    534   1.1    simonb 	if (error != 0)
    535   1.1    simonb 		aprint_error("%s: could not allocate shared area DMA memory\n",
    536   1.1    simonb 			sc->sc_dev.dv_xname);
    537   1.1    simonb 
    538   1.1    simonb 	return error;
    539   1.1    simonb }
    540   1.1    simonb 
    541   1.1    simonb static void
    542   1.1    simonb wpi_free_shared(struct wpi_softc *sc)
    543   1.1    simonb {
    544   1.7  degroote 	wpi_dma_contig_free(&sc->shared_dma);
    545   1.7  degroote }
    546   1.7  degroote 
    547   1.7  degroote static struct wpi_rbuf *
    548   1.7  degroote wpi_alloc_rbuf(struct wpi_softc *sc)
    549   1.7  degroote {
    550   1.7  degroote 	struct wpi_rbuf *rbuf;
    551   1.7  degroote 
    552   1.7  degroote 	rbuf = SLIST_FIRST(&sc->rxq.freelist);
    553   1.7  degroote 	if (rbuf == NULL)
    554   1.7  degroote 		return NULL;
    555   1.7  degroote 	SLIST_REMOVE_HEAD(&sc->rxq.freelist, next);
    556  1.10  degroote 	sc->rxq.nb_free_entries --;
    557  1.10  degroote 
    558   1.7  degroote 	return rbuf;
    559   1.7  degroote }
    560   1.7  degroote 
    561   1.7  degroote /*
    562   1.7  degroote  * This is called automatically by the network stack when the mbuf to which our
    563   1.7  degroote  * Rx buffer is attached is freed.
    564   1.7  degroote  */
    565   1.7  degroote static void
    566   1.9  christos wpi_free_rbuf(struct mbuf* m, void *buf, size_t size, void *arg)
    567   1.7  degroote {
    568   1.7  degroote 	struct wpi_rbuf *rbuf = arg;
    569   1.7  degroote 	struct wpi_softc *sc = rbuf->sc;
    570   1.7  degroote 	int s;
    571   1.7  degroote 
    572   1.7  degroote 	/* put the buffer back in the free list */
    573  1.10  degroote 
    574   1.7  degroote 	SLIST_INSERT_HEAD(&sc->rxq.freelist, rbuf, next);
    575  1.10  degroote 	sc->rxq.nb_free_entries ++;
    576   1.7  degroote 
    577   1.7  degroote 	if (__predict_true(m != NULL)) {
    578   1.7  degroote 		s = splvm();
    579   1.7  degroote 		pool_cache_put(&mbpool_cache, m);
    580   1.7  degroote 		splx(s);
    581   1.7  degroote 	}
    582   1.7  degroote }
    583   1.7  degroote 
    584   1.7  degroote static int
    585   1.7  degroote wpi_alloc_rpool(struct wpi_softc *sc)
    586   1.7  degroote {
    587   1.7  degroote 	struct wpi_rx_ring *ring = &sc->rxq;
    588   1.7  degroote 	struct wpi_rbuf *rbuf;
    589   1.7  degroote 	int i, error;
    590   1.7  degroote 
    591   1.7  degroote 	/* allocate a big chunk of DMA'able memory.. */
    592   1.7  degroote 	error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->buf_dma, NULL,
    593   1.7  degroote 	    WPI_RBUF_COUNT * WPI_RBUF_SIZE, WPI_BUF_ALIGN, BUS_DMA_NOWAIT);
    594   1.7  degroote 	if (error != 0) {
    595   1.7  degroote 		aprint_normal("%s: could not allocate Rx buffers DMA memory\n",
    596   1.7  degroote 		    sc->sc_dev.dv_xname);
    597   1.7  degroote 	return error;
    598   1.7  degroote 	}
    599   1.7  degroote 
    600   1.7  degroote 	/* ..and split it into 3KB chunks */
    601   1.7  degroote 	SLIST_INIT(&ring->freelist);
    602   1.7  degroote 	for (i = 0; i < WPI_RBUF_COUNT; i++) {
    603   1.7  degroote 		rbuf = &ring->rbuf[i];
    604   1.7  degroote 		rbuf->sc = sc;	/* backpointer for callbacks */
    605   1.9  christos 		rbuf->vaddr = (char *)ring->buf_dma.vaddr + i * WPI_RBUF_SIZE;
    606   1.7  degroote 		rbuf->paddr = ring->buf_dma.paddr + i * WPI_RBUF_SIZE;
    607   1.7  degroote 
    608   1.7  degroote 		SLIST_INSERT_HEAD(&ring->freelist, rbuf, next);
    609   1.7  degroote 	}
    610  1.10  degroote 
    611  1.10  degroote 	ring->nb_free_entries = WPI_RBUF_COUNT;
    612   1.7  degroote 	return 0;
    613   1.7  degroote }
    614   1.7  degroote 
    615   1.7  degroote static void
    616   1.7  degroote wpi_free_rpool(struct wpi_softc *sc)
    617   1.7  degroote {
    618   1.7  degroote 	wpi_dma_contig_free(&sc->rxq.buf_dma);
    619   1.1    simonb }
    620   1.1    simonb 
    621   1.1    simonb static int
    622   1.1    simonb wpi_alloc_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
    623   1.1    simonb {
    624   1.1    simonb 	struct wpi_rx_data *data;
    625   1.7  degroote 	struct wpi_rbuf *rbuf;
    626   1.1    simonb 	int i, error;
    627   1.1    simonb 
    628   1.1    simonb 	ring->cur = 0;
    629   1.1    simonb 
    630   1.7  degroote 	error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->desc_dma,
    631   1.1    simonb 		(void **)&ring->desc,
    632   1.1    simonb 		WPI_RX_RING_COUNT * sizeof (struct wpi_rx_desc),
    633   1.1    simonb 		WPI_RING_DMA_ALIGN, BUS_DMA_NOWAIT);
    634   1.1    simonb 	if (error != 0) {
    635   1.1    simonb 		aprint_error("%s: could not allocate rx ring DMA memory\n",
    636   1.1    simonb 			sc->sc_dev.dv_xname);
    637   1.1    simonb 		goto fail;
    638   1.1    simonb 	}
    639   1.1    simonb 
    640   1.1    simonb 	/*
    641   1.7  degroote 	 * Setup Rx buffers.
    642   1.1    simonb 	 */
    643   1.1    simonb 	for (i = 0; i < WPI_RX_RING_COUNT; i++) {
    644   1.1    simonb 		data = &ring->data[i];
    645   1.1    simonb 
    646   1.1    simonb 		MGETHDR(data->m, M_DONTWAIT, MT_DATA);
    647   1.1    simonb 		if (data->m == NULL) {
    648   1.1    simonb 			aprint_error("%s: could not allocate rx mbuf\n",
    649   1.1    simonb 				sc->sc_dev.dv_xname);
    650   1.1    simonb 			error = ENOMEM;
    651   1.1    simonb 			goto fail;
    652   1.1    simonb 		}
    653   1.7  degroote 		if ((rbuf = wpi_alloc_rbuf(sc)) == NULL) {
    654   1.1    simonb 			m_freem(data->m);
    655   1.1    simonb 			data->m = NULL;
    656   1.7  degroote 			aprint_error("%s: could not allocate rx cluster\n",
    657   1.1    simonb 				sc->sc_dev.dv_xname);
    658   1.1    simonb 			error = ENOMEM;
    659   1.1    simonb 			goto fail;
    660   1.1    simonb 		}
    661   1.7  degroote 		/* attach Rx buffer to mbuf */
    662   1.7  degroote 		MEXTADD(data->m, rbuf->vaddr, WPI_RBUF_SIZE, 0, wpi_free_rbuf,
    663   1.7  degroote 		    rbuf);
    664   1.7  degroote 		data->m->m_flags |= M_EXT_RW;
    665   1.1    simonb 
    666   1.7  degroote 		ring->desc[i] = htole32(rbuf->paddr);
    667   1.1    simonb 	}
    668   1.1    simonb 
    669   1.1    simonb 	return 0;
    670   1.1    simonb 
    671   1.1    simonb fail:	wpi_free_rx_ring(sc, ring);
    672   1.1    simonb 	return error;
    673   1.1    simonb }
    674   1.1    simonb 
    675   1.1    simonb static void
    676   1.1    simonb wpi_reset_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
    677   1.1    simonb {
    678   1.1    simonb 	int ntries;
    679   1.1    simonb 
    680   1.1    simonb 	wpi_mem_lock(sc);
    681   1.1    simonb 
    682   1.1    simonb 	WPI_WRITE(sc, WPI_RX_CONFIG, 0);
    683   1.1    simonb 	for (ntries = 0; ntries < 100; ntries++) {
    684   1.1    simonb 		if (WPI_READ(sc, WPI_RX_STATUS) & WPI_RX_IDLE)
    685   1.1    simonb 			break;
    686   1.1    simonb 		DELAY(10);
    687   1.1    simonb 	}
    688   1.1    simonb #ifdef WPI_DEBUG
    689   1.1    simonb 	if (ntries == 100 && wpi_debug > 0)
    690   1.1    simonb 		aprint_error("%s: timeout resetting Rx ring\n",
    691   1.1    simonb 			sc->sc_dev.dv_xname);
    692   1.1    simonb #endif
    693   1.1    simonb 	wpi_mem_unlock(sc);
    694   1.1    simonb 
    695   1.1    simonb 	ring->cur = 0;
    696   1.1    simonb }
    697   1.1    simonb 
    698   1.1    simonb static void
    699   1.1    simonb wpi_free_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
    700   1.1    simonb {
    701   1.1    simonb 	int i;
    702   1.1    simonb 
    703   1.7  degroote 	wpi_dma_contig_free(&ring->desc_dma);
    704   1.1    simonb 
    705   1.1    simonb 	for (i = 0; i < WPI_RX_RING_COUNT; i++) {
    706   1.7  degroote 		if (ring->data[i].m != NULL)
    707   1.7  degroote 			m_freem(ring->data[i].m);
    708   1.1    simonb 	}
    709   1.1    simonb }
    710   1.1    simonb 
    711   1.1    simonb static int
    712   1.1    simonb wpi_alloc_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring, int count,
    713   1.1    simonb 	int qid)
    714   1.1    simonb {
    715   1.1    simonb 	struct wpi_tx_data *data;
    716   1.1    simonb 	int i, error;
    717   1.1    simonb 
    718   1.1    simonb 	ring->qid = qid;
    719   1.1    simonb 	ring->count = count;
    720   1.1    simonb 	ring->queued = 0;
    721   1.1    simonb 	ring->cur = 0;
    722   1.1    simonb 
    723   1.7  degroote 	error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->desc_dma,
    724   1.1    simonb 		(void **)&ring->desc, count * sizeof (struct wpi_tx_desc),
    725   1.1    simonb 		WPI_RING_DMA_ALIGN, BUS_DMA_NOWAIT);
    726   1.1    simonb 	if (error != 0) {
    727   1.1    simonb 		aprint_error("%s: could not allocate tx ring DMA memory\n",
    728   1.1    simonb 			sc->sc_dev.dv_xname);
    729   1.1    simonb 		goto fail;
    730   1.1    simonb 	}
    731   1.1    simonb 
    732   1.1    simonb 	/* update shared page with ring's base address */
    733   1.1    simonb 	sc->shared->txbase[qid] = htole32(ring->desc_dma.paddr);
    734   1.1    simonb 
    735   1.7  degroote 	error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->cmd_dma,
    736   1.7  degroote 		(void **)&ring->cmd,
    737   1.1    simonb 		count * sizeof (struct wpi_tx_cmd), 4, BUS_DMA_NOWAIT);
    738   1.1    simonb 	if (error != 0) {
    739   1.1    simonb 		aprint_error("%s: could not allocate tx cmd DMA memory\n",
    740   1.1    simonb 			sc->sc_dev.dv_xname);
    741   1.1    simonb 		goto fail;
    742   1.1    simonb 	}
    743   1.1    simonb 
    744   1.1    simonb 	ring->data = malloc(count * sizeof (struct wpi_tx_data), M_DEVBUF,
    745   1.1    simonb 		M_NOWAIT);
    746   1.1    simonb 	if (ring->data == NULL) {
    747   1.1    simonb 		aprint_error("%s: could not allocate tx data slots\n",
    748   1.1    simonb 			sc->sc_dev.dv_xname);
    749   1.1    simonb 		goto fail;
    750   1.1    simonb 	}
    751   1.1    simonb 
    752   1.1    simonb 	memset(ring->data, 0, count * sizeof (struct wpi_tx_data));
    753   1.1    simonb 
    754   1.1    simonb 	for (i = 0; i < count; i++) {
    755   1.1    simonb 		data = &ring->data[i];
    756   1.1    simonb 
    757   1.1    simonb 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    758   1.1    simonb 			WPI_MAX_SCATTER - 1, MCLBYTES, 0, BUS_DMA_NOWAIT,
    759   1.1    simonb 			&data->map);
    760   1.1    simonb 		if (error != 0) {
    761   1.1    simonb 			aprint_error("%s: could not create tx buf DMA map\n",
    762   1.1    simonb 				sc->sc_dev.dv_xname);
    763   1.1    simonb 			goto fail;
    764   1.1    simonb 		}
    765   1.1    simonb 	}
    766   1.1    simonb 
    767   1.1    simonb 	return 0;
    768   1.1    simonb 
    769   1.1    simonb fail:	wpi_free_tx_ring(sc, ring);
    770   1.1    simonb 	return error;
    771   1.1    simonb }
    772   1.1    simonb 
    773   1.1    simonb static void
    774   1.1    simonb wpi_reset_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
    775   1.1    simonb {
    776   1.1    simonb 	struct wpi_tx_data *data;
    777   1.1    simonb 	int i, ntries;
    778   1.1    simonb 
    779   1.1    simonb 	wpi_mem_lock(sc);
    780   1.1    simonb 
    781   1.1    simonb 	WPI_WRITE(sc, WPI_TX_CONFIG(ring->qid), 0);
    782   1.1    simonb 	for (ntries = 0; ntries < 100; ntries++) {
    783   1.1    simonb 		if (WPI_READ(sc, WPI_TX_STATUS) & WPI_TX_IDLE(ring->qid))
    784   1.1    simonb 			break;
    785   1.1    simonb 		DELAY(10);
    786   1.1    simonb 	}
    787   1.1    simonb #ifdef WPI_DEBUG
    788   1.1    simonb 	if (ntries == 100 && wpi_debug > 0) {
    789   1.1    simonb 		aprint_error("%s: timeout resetting Tx ring %d\n",
    790   1.1    simonb 			sc->sc_dev.dv_xname, ring->qid);
    791   1.1    simonb 	}
    792   1.1    simonb #endif
    793   1.1    simonb 	wpi_mem_unlock(sc);
    794   1.1    simonb 
    795   1.1    simonb 	for (i = 0; i < ring->count; i++) {
    796   1.1    simonb 		data = &ring->data[i];
    797   1.1    simonb 
    798   1.1    simonb 		if (data->m != NULL) {
    799   1.1    simonb 			bus_dmamap_unload(sc->sc_dmat, data->map);
    800   1.1    simonb 			m_freem(data->m);
    801   1.1    simonb 			data->m = NULL;
    802   1.1    simonb 		}
    803   1.1    simonb 	}
    804   1.1    simonb 
    805   1.1    simonb 	ring->queued = 0;
    806   1.1    simonb 	ring->cur = 0;
    807   1.1    simonb }
    808   1.1    simonb 
    809   1.1    simonb static void
    810   1.1    simonb wpi_free_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
    811   1.1    simonb {
    812   1.1    simonb 	struct wpi_tx_data *data;
    813   1.1    simonb 	int i;
    814   1.1    simonb 
    815   1.7  degroote 	wpi_dma_contig_free(&ring->desc_dma);
    816   1.7  degroote 	wpi_dma_contig_free(&ring->cmd_dma);
    817   1.1    simonb 
    818   1.1    simonb 	if (ring->data != NULL) {
    819   1.1    simonb 		for (i = 0; i < ring->count; i++) {
    820   1.1    simonb 			data = &ring->data[i];
    821   1.1    simonb 
    822   1.1    simonb 			if (data->m != NULL) {
    823   1.1    simonb 				bus_dmamap_unload(sc->sc_dmat, data->map);
    824   1.1    simonb 				m_freem(data->m);
    825   1.1    simonb 			}
    826   1.1    simonb 		}
    827   1.1    simonb 		free(ring->data, M_DEVBUF);
    828   1.1    simonb 	}
    829   1.1    simonb }
    830   1.1    simonb 
    831   1.1    simonb /*ARGUSED*/
    832   1.1    simonb static struct ieee80211_node *
    833   1.7  degroote wpi_node_alloc(struct ieee80211_node_table *nt __unused)
    834   1.1    simonb {
    835   1.5     joerg 	struct wpi_node *wn;
    836   1.1    simonb 
    837   1.5     joerg 	wn = malloc(sizeof (struct wpi_node), M_DEVBUF, M_NOWAIT);
    838   1.5     joerg 
    839   1.5     joerg 	if (wn != NULL)
    840   1.5     joerg 		memset(wn, 0, sizeof (struct wpi_node));
    841   1.5     joerg 	return (struct ieee80211_node *)wn;
    842   1.1    simonb }
    843   1.1    simonb 
    844   1.1    simonb static int
    845   1.1    simonb wpi_media_change(struct ifnet *ifp)
    846   1.1    simonb {
    847   1.1    simonb 	int error;
    848   1.1    simonb 
    849   1.1    simonb 	error = ieee80211_media_change(ifp);
    850   1.1    simonb 	if (error != ENETRESET)
    851   1.1    simonb 		return error;
    852   1.1    simonb 
    853   1.1    simonb 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
    854   1.1    simonb 		wpi_init(ifp);
    855   1.1    simonb 
    856   1.1    simonb 	return 0;
    857   1.1    simonb }
    858   1.1    simonb 
    859   1.1    simonb static int
    860   1.1    simonb wpi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
    861   1.1    simonb {
    862   1.1    simonb 	struct ifnet *ifp = ic->ic_ifp;
    863   1.1    simonb 	struct wpi_softc *sc = ifp->if_softc;
    864   1.1    simonb 	int error;
    865   1.1    simonb 
    866   1.1    simonb 	callout_stop(&sc->amrr_ch);
    867   1.1    simonb 
    868   1.1    simonb 	switch (nstate) {
    869   1.1    simonb 	case IEEE80211_S_SCAN:
    870   1.1    simonb 		ieee80211_node_table_reset(&ic->ic_scan);
    871   1.1    simonb 		ic->ic_flags |= IEEE80211_F_SCAN | IEEE80211_F_ASCAN;
    872   1.1    simonb 
    873   1.1    simonb 		/* make the link LED blink while we're scanning */
    874   1.1    simonb 		wpi_set_led(sc, WPI_LED_LINK, 20, 2);
    875   1.1    simonb 
    876   1.1    simonb 		if ((error = wpi_scan(sc, IEEE80211_CHAN_G)) != 0) {
    877   1.1    simonb 			aprint_error("%s: could not initiate scan\n",
    878   1.1    simonb 				sc->sc_dev.dv_xname);
    879   1.1    simonb 			ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN);
    880   1.1    simonb 			return error;
    881   1.1    simonb 		}
    882   1.1    simonb 
    883   1.1    simonb 		ic->ic_state = nstate;
    884   1.1    simonb 		return 0;
    885   1.1    simonb 
    886   1.7  degroote 	case IEEE80211_S_ASSOC:
    887   1.7  degroote 		if (ic->ic_state != IEEE80211_S_RUN)
    888   1.7  degroote 			break;
    889   1.7  degroote 		/* FALLTHROUGH */
    890   1.1    simonb 	case IEEE80211_S_AUTH:
    891   1.1    simonb 		sc->config.state &= ~htole16(WPI_STATE_ASSOCIATED);
    892   1.1    simonb 		sc->config.filter &= ~htole32(WPI_FILTER_BSS);
    893   1.1    simonb 		if ((error = wpi_auth(sc)) != 0) {
    894   1.1    simonb 			aprint_error("%s: could not send authentication request\n",
    895   1.1    simonb 				sc->sc_dev.dv_xname);
    896   1.1    simonb 			return error;
    897   1.1    simonb 		}
    898   1.1    simonb 		break;
    899   1.1    simonb 
    900   1.1    simonb 	case IEEE80211_S_RUN:
    901   1.1    simonb 		if (ic->ic_opmode == IEEE80211_M_MONITOR) {
    902   1.1    simonb 			/* link LED blinks while monitoring */
    903   1.1    simonb 			wpi_set_led(sc, WPI_LED_LINK, 5, 5);
    904   1.1    simonb 			break;
    905   1.1    simonb 		}
    906   1.1    simonb 
    907   1.1    simonb 		if (ic->ic_opmode != IEEE80211_M_STA) {
    908   1.1    simonb 			(void) wpi_auth(sc);    /* XXX */
    909   1.1    simonb 			wpi_setup_beacon(sc, ic->ic_bss);
    910   1.1    simonb 		}
    911   1.1    simonb 
    912   1.1    simonb 		wpi_enable_tsf(sc, ic->ic_bss);
    913   1.1    simonb 
    914   1.1    simonb 		/* update adapter's configuration */
    915   1.1    simonb 		sc->config.state = htole16(WPI_STATE_ASSOCIATED);
    916   1.1    simonb 		/* short preamble/slot time are negotiated when associating */
    917   1.1    simonb 		sc->config.flags &= ~htole32(WPI_CONFIG_SHPREAMBLE |
    918   1.1    simonb 			WPI_CONFIG_SHSLOT);
    919   1.1    simonb 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
    920   1.1    simonb 			sc->config.flags |= htole32(WPI_CONFIG_SHSLOT);
    921   1.1    simonb 		if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
    922   1.1    simonb 			sc->config.flags |= htole32(WPI_CONFIG_SHPREAMBLE);
    923   1.1    simonb 		sc->config.filter |= htole32(WPI_FILTER_BSS);
    924   1.1    simonb 		if (ic->ic_opmode != IEEE80211_M_STA)
    925   1.1    simonb 			sc->config.filter |= htole32(WPI_FILTER_BEACON);
    926   1.1    simonb 
    927   1.1    simonb /* XXX put somewhere HC_QOS_SUPPORT_ASSOC + HC_IBSS_START */
    928   1.1    simonb 
    929   1.1    simonb 		DPRINTF(("config chan %d flags %x\n", sc->config.chan,
    930   1.1    simonb 			sc->config.flags));
    931   1.1    simonb 		error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config,
    932   1.1    simonb 			sizeof (struct wpi_config), 1);
    933   1.1    simonb 		if (error != 0) {
    934   1.1    simonb 			aprint_error("%s: could not update configuration\n",
    935   1.1    simonb 				sc->sc_dev.dv_xname);
    936   1.1    simonb 			return error;
    937   1.1    simonb 		}
    938   1.1    simonb 
    939   1.5     joerg 		if (ic->ic_opmode == IEEE80211_M_STA) {
    940   1.5     joerg 			/* fake a join to init the tx rate */
    941   1.5     joerg 			wpi_newassoc(ic->ic_bss, 1);
    942   1.5     joerg 		}
    943   1.5     joerg 
    944   1.1    simonb 		/* enable automatic rate adaptation in STA mode */
    945   1.1    simonb 		if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
    946   1.7  degroote 			callout_reset(&sc->amrr_ch, hz/2, wpi_amrr_timeout, sc);
    947   1.1    simonb 
    948   1.1    simonb 		/* link LED always on while associated */
    949   1.1    simonb 		wpi_set_led(sc, WPI_LED_LINK, 0, 1);
    950   1.1    simonb 		break;
    951   1.1    simonb 
    952   1.1    simonb 	case IEEE80211_S_INIT:
    953   1.1    simonb 		break;
    954   1.1    simonb 	}
    955   1.1    simonb 
    956   1.1    simonb 	return sc->sc_newstate(ic, nstate, arg);
    957   1.1    simonb }
    958   1.1    simonb 
    959   1.1    simonb /*
    960   1.1    simonb  * Grab exclusive access to NIC memory.
    961   1.1    simonb  */
    962   1.1    simonb static void
    963   1.1    simonb wpi_mem_lock(struct wpi_softc *sc)
    964   1.1    simonb {
    965   1.1    simonb 	uint32_t tmp;
    966   1.1    simonb 	int ntries;
    967   1.1    simonb 
    968   1.1    simonb 	tmp = WPI_READ(sc, WPI_GPIO_CTL);
    969   1.1    simonb 	WPI_WRITE(sc, WPI_GPIO_CTL, tmp | WPI_GPIO_MAC);
    970   1.1    simonb 
    971   1.1    simonb 	/* spin until we actually get the lock */
    972   1.1    simonb 	for (ntries = 0; ntries < 1000; ntries++) {
    973   1.1    simonb 		if ((WPI_READ(sc, WPI_GPIO_CTL) &
    974   1.1    simonb 			(WPI_GPIO_CLOCK | WPI_GPIO_SLEEP)) == WPI_GPIO_CLOCK)
    975   1.1    simonb 			break;
    976   1.1    simonb 		DELAY(10);
    977   1.1    simonb 	}
    978   1.1    simonb 	if (ntries == 1000)
    979   1.1    simonb 		aprint_error("%s: could not lock memory\n", sc->sc_dev.dv_xname);
    980   1.1    simonb }
    981   1.1    simonb 
    982   1.1    simonb /*
    983   1.1    simonb  * Release lock on NIC memory.
    984   1.1    simonb  */
    985   1.1    simonb static void
    986   1.1    simonb wpi_mem_unlock(struct wpi_softc *sc)
    987   1.1    simonb {
    988   1.1    simonb 	uint32_t tmp = WPI_READ(sc, WPI_GPIO_CTL);
    989   1.1    simonb 	WPI_WRITE(sc, WPI_GPIO_CTL, tmp & ~WPI_GPIO_MAC);
    990   1.1    simonb }
    991   1.1    simonb 
    992   1.1    simonb static uint32_t
    993   1.1    simonb wpi_mem_read(struct wpi_softc *sc, uint16_t addr)
    994   1.1    simonb {
    995   1.1    simonb 	WPI_WRITE(sc, WPI_READ_MEM_ADDR, WPI_MEM_4 | addr);
    996   1.1    simonb 	return WPI_READ(sc, WPI_READ_MEM_DATA);
    997   1.1    simonb }
    998   1.1    simonb 
    999   1.1    simonb static void
   1000   1.1    simonb wpi_mem_write(struct wpi_softc *sc, uint16_t addr, uint32_t data)
   1001   1.1    simonb {
   1002   1.1    simonb 	WPI_WRITE(sc, WPI_WRITE_MEM_ADDR, WPI_MEM_4 | addr);
   1003   1.1    simonb 	WPI_WRITE(sc, WPI_WRITE_MEM_DATA, data);
   1004   1.1    simonb }
   1005   1.1    simonb 
   1006   1.1    simonb static void
   1007   1.1    simonb wpi_mem_write_region_4(struct wpi_softc *sc, uint16_t addr,
   1008   1.1    simonb 	const uint32_t *data, int wlen)
   1009   1.1    simonb {
   1010   1.1    simonb 	for (; wlen > 0; wlen--, data++, addr += 4)
   1011   1.1    simonb 		wpi_mem_write(sc, addr, *data);
   1012   1.1    simonb }
   1013   1.1    simonb 
   1014   1.1    simonb /*
   1015   1.1    simonb  * Read 16 bits from the EEPROM.  We access EEPROM through the MAC instead of
   1016   1.1    simonb  * using the traditional bit-bang method.
   1017   1.1    simonb  */
   1018   1.1    simonb static uint16_t
   1019   1.1    simonb wpi_read_prom_word(struct wpi_softc *sc, uint32_t addr)
   1020   1.1    simonb {
   1021   1.1    simonb 	int ntries;
   1022   1.1    simonb 	uint32_t val;
   1023   1.1    simonb 
   1024   1.1    simonb 	WPI_WRITE(sc, WPI_EEPROM_CTL, addr << 2);
   1025   1.1    simonb 
   1026   1.1    simonb 	wpi_mem_lock(sc);
   1027   1.1    simonb 	for (ntries = 0; ntries < 10; ntries++) {
   1028   1.1    simonb 		if ((val = WPI_READ(sc, WPI_EEPROM_CTL)) & WPI_EEPROM_READY)
   1029   1.1    simonb 			break;
   1030   1.1    simonb 		DELAY(10);
   1031   1.1    simonb 	}
   1032   1.1    simonb 	wpi_mem_unlock(sc);
   1033   1.1    simonb 
   1034   1.1    simonb 	if (ntries == 10) {
   1035   1.1    simonb 		aprint_error("%s: could not read EEPROM\n", sc->sc_dev.dv_xname);
   1036   1.1    simonb 		return 0xdead;
   1037   1.1    simonb 	}
   1038   1.1    simonb 	return val >> 16;
   1039   1.1    simonb }
   1040   1.1    simonb 
   1041   1.1    simonb /*
   1042   1.1    simonb  * The firmware boot code is small and is intended to be copied directly into
   1043   1.1    simonb  * the NIC internal memory.
   1044   1.1    simonb  */
   1045   1.1    simonb static int
   1046   1.1    simonb wpi_load_microcode(struct wpi_softc *sc, const char *ucode, int size)
   1047   1.1    simonb {
   1048   1.1    simonb 	/* check that microcode size is a multiple of 4 */
   1049   1.1    simonb 	if (size & 3)
   1050   1.1    simonb 		return EINVAL;
   1051   1.1    simonb 
   1052   1.1    simonb 	size /= sizeof (uint32_t);
   1053   1.1    simonb 
   1054   1.1    simonb 	wpi_mem_lock(sc);
   1055   1.1    simonb 
   1056   1.1    simonb 	/* copy microcode image into NIC memory */
   1057   1.1    simonb 	wpi_mem_write_region_4(sc, WPI_MEM_UCODE_BASE, (const uint32_t *)ucode,
   1058   1.1    simonb 		size);
   1059   1.1    simonb 
   1060   1.1    simonb 	wpi_mem_write(sc, WPI_MEM_UCODE_SRC, 0);
   1061   1.1    simonb 	wpi_mem_write(sc, WPI_MEM_UCODE_DST, WPI_FW_TEXT);
   1062   1.1    simonb 	wpi_mem_write(sc, WPI_MEM_UCODE_SIZE, size);
   1063   1.1    simonb 
   1064   1.1    simonb 	/* run microcode */
   1065   1.1    simonb 	wpi_mem_write(sc, WPI_MEM_UCODE_CTL, WPI_UC_RUN);
   1066   1.1    simonb 
   1067   1.1    simonb 	wpi_mem_unlock(sc);
   1068   1.1    simonb 
   1069   1.1    simonb 	return 0;
   1070   1.1    simonb }
   1071   1.1    simonb 
   1072   1.1    simonb /*
   1073   1.1    simonb  * The firmware text and data segments are transferred to the NIC using DMA.
   1074   1.1    simonb  * The driver just copies the firmware into DMA-safe memory and tells the NIC
   1075   1.1    simonb  * where to find it.  Once the NIC has copied the firmware into its internal
   1076   1.1    simonb  * memory, we can free our local copy in the driver.
   1077   1.1    simonb  */
   1078   1.1    simonb static int
   1079   1.1    simonb wpi_load_firmware(struct wpi_softc *sc, uint32_t target, const char *fw,
   1080   1.1    simonb 	int size)
   1081   1.1    simonb {
   1082   1.1    simonb 	bus_dmamap_t map;
   1083   1.1    simonb 	bus_dma_segment_t seg;
   1084   1.9  christos 	void *virtaddr;
   1085   1.1    simonb 	struct wpi_tx_desc desc;
   1086   1.1    simonb 	int i, ntries, nsegs, error;
   1087   1.1    simonb 
   1088   1.1    simonb 	/*
   1089   1.1    simonb 	 * Allocate DMA-safe memory to store the firmware.
   1090   1.1    simonb 	 */
   1091   1.1    simonb 	error = bus_dmamap_create(sc->sc_dmat, size, WPI_MAX_SCATTER,
   1092   1.1    simonb 		WPI_MAX_SEG_LEN, 0, BUS_DMA_NOWAIT, &map);
   1093   1.1    simonb 	if (error != 0) {
   1094   1.1    simonb 		aprint_error("%s: could not create firmware DMA map\n",
   1095   1.1    simonb 			sc->sc_dev.dv_xname);
   1096   1.1    simonb 		goto fail1;
   1097   1.1    simonb 	}
   1098   1.1    simonb 
   1099   1.1    simonb 	error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &seg, 1,
   1100   1.1    simonb 		&nsegs, BUS_DMA_NOWAIT);
   1101   1.1    simonb 	if (error != 0) {
   1102   1.1    simonb 		aprint_error("%s: could not allocate firmware DMA memory\n",
   1103   1.1    simonb 			sc->sc_dev.dv_xname);
   1104   1.1    simonb 		goto fail2;
   1105   1.1    simonb 	}
   1106   1.1    simonb 
   1107   1.1    simonb 	error = bus_dmamem_map(sc->sc_dmat, &seg, nsegs, size, &virtaddr,
   1108   1.1    simonb 		BUS_DMA_NOWAIT);
   1109   1.1    simonb 	if (error != 0) {
   1110   1.1    simonb 		aprint_error("%s: could not map firmware DMA memory\n",
   1111   1.1    simonb 			sc->sc_dev.dv_xname);
   1112   1.1    simonb 		goto fail3;
   1113   1.1    simonb 	}
   1114   1.1    simonb 
   1115   1.1    simonb 	error = bus_dmamap_load(sc->sc_dmat, map, virtaddr, size, NULL,
   1116   1.1    simonb 		BUS_DMA_NOWAIT | BUS_DMA_WRITE);
   1117   1.1    simonb 	if (error != 0) {
   1118   1.1    simonb 		aprint_error("%s: could not load firmware DMA map\n",
   1119   1.1    simonb 			sc->sc_dev.dv_xname);
   1120   1.1    simonb 		goto fail4;
   1121   1.1    simonb 	}
   1122   1.1    simonb 
   1123   1.1    simonb 	/* copy firmware image to DMA-safe memory */
   1124   1.7  degroote 	memcpy(virtaddr, fw, size);
   1125   1.1    simonb 
   1126   1.1    simonb 	/* make sure the adapter will get up-to-date values */
   1127   1.1    simonb 	bus_dmamap_sync(sc->sc_dmat, map, 0, size, BUS_DMASYNC_PREWRITE);
   1128   1.1    simonb 
   1129   1.1    simonb 	bzero(&desc, sizeof desc);
   1130   1.1    simonb 	desc.flags = htole32(WPI_PAD32(size) << 28 | map->dm_nsegs << 24);
   1131   1.1    simonb 	for (i = 0; i < map->dm_nsegs; i++) {
   1132   1.1    simonb 		desc.segs[i].addr = htole32(map->dm_segs[i].ds_addr);
   1133   1.1    simonb 		desc.segs[i].len  = htole32(map->dm_segs[i].ds_len);
   1134   1.1    simonb 	}
   1135   1.1    simonb 
   1136   1.1    simonb 	wpi_mem_lock(sc);
   1137   1.1    simonb 
   1138   1.1    simonb 	/* tell adapter where to copy image in its internal memory */
   1139   1.1    simonb 	WPI_WRITE(sc, WPI_FW_TARGET, target);
   1140   1.1    simonb 
   1141   1.1    simonb 	WPI_WRITE(sc, WPI_TX_CONFIG(6), 0);
   1142   1.1    simonb 
   1143   1.1    simonb 	/* copy firmware descriptor into NIC memory */
   1144   1.1    simonb 	WPI_WRITE_REGION_4(sc, WPI_TX_DESC(6), (uint32_t *)&desc,
   1145   1.1    simonb 		sizeof desc / sizeof (uint32_t));
   1146   1.1    simonb 
   1147   1.1    simonb 	WPI_WRITE(sc, WPI_TX_CREDIT(6), 0xfffff);
   1148   1.1    simonb 	WPI_WRITE(sc, WPI_TX_STATE(6), 0x4001);
   1149   1.1    simonb 	WPI_WRITE(sc, WPI_TX_CONFIG(6), 0x80000001);
   1150   1.1    simonb 
   1151   1.1    simonb 	/* wait while the adapter is busy copying the firmware */
   1152   1.1    simonb 	for (ntries = 0; ntries < 100; ntries++) {
   1153   1.1    simonb 		if (WPI_READ(sc, WPI_TX_STATUS) & WPI_TX_IDLE(6))
   1154   1.1    simonb 			break;
   1155   1.1    simonb 		DELAY(1000);
   1156   1.1    simonb 	}
   1157   1.1    simonb 	if (ntries == 100) {
   1158   1.1    simonb 		aprint_error("%s: timeout transferring firmware\n",
   1159   1.1    simonb 			sc->sc_dev.dv_xname);
   1160   1.1    simonb 		error = ETIMEDOUT;
   1161   1.1    simonb 	}
   1162   1.1    simonb 
   1163   1.1    simonb 	WPI_WRITE(sc, WPI_TX_CREDIT(6), 0);
   1164   1.1    simonb 
   1165   1.1    simonb 	wpi_mem_unlock(sc);
   1166   1.1    simonb 
   1167   1.1    simonb 	bus_dmamap_sync(sc->sc_dmat, map, 0, size, BUS_DMASYNC_POSTWRITE);
   1168   1.1    simonb 	bus_dmamap_unload(sc->sc_dmat, map);
   1169   1.1    simonb fail4:	bus_dmamem_unmap(sc->sc_dmat, virtaddr, size);
   1170   1.1    simonb fail3:	bus_dmamem_free(sc->sc_dmat, &seg, 1);
   1171   1.1    simonb fail2:	bus_dmamap_destroy(sc->sc_dmat, map);
   1172   1.1    simonb fail1:	return error;
   1173   1.1    simonb }
   1174   1.1    simonb 
   1175   1.1    simonb static void
   1176   1.1    simonb wpi_rx_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc,
   1177   1.1    simonb 	struct wpi_rx_data *data)
   1178   1.1    simonb {
   1179   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
   1180   1.1    simonb 	struct ifnet *ifp = ic->ic_ifp;
   1181   1.1    simonb 	struct wpi_rx_ring *ring = &sc->rxq;
   1182   1.1    simonb 	struct wpi_rx_stat *stat;
   1183   1.1    simonb 	struct wpi_rx_head *head;
   1184   1.1    simonb 	struct wpi_rx_tail *tail;
   1185   1.7  degroote 	struct wpi_rbuf *rbuf;
   1186   1.1    simonb 	struct ieee80211_frame *wh;
   1187   1.1    simonb 	struct ieee80211_node *ni;
   1188   1.1    simonb 	struct mbuf *m, *mnew;
   1189   1.1    simonb 
   1190   1.1    simonb 	stat = (struct wpi_rx_stat *)(desc + 1);
   1191   1.1    simonb 
   1192   1.1    simonb 	if (stat->len > WPI_STAT_MAXLEN) {
   1193   1.1    simonb 		aprint_error("%s: invalid rx statistic header\n",
   1194   1.1    simonb 			sc->sc_dev.dv_xname);
   1195   1.1    simonb 		ifp->if_ierrors++;
   1196   1.1    simonb 		return;
   1197   1.1    simonb 	}
   1198   1.1    simonb 
   1199   1.9  christos 	head = (struct wpi_rx_head *)((char *)(stat + 1) + stat->len);
   1200   1.9  christos 	tail = (struct wpi_rx_tail *)((char *)(head + 1) + le16toh(head->len));
   1201   1.1    simonb 
   1202   1.1    simonb 	DPRINTFN(4, ("rx intr: idx=%d len=%d stat len=%d rssi=%d rate=%x "
   1203   1.1    simonb 		"chan=%d tstamp=%llu\n", ring->cur, le32toh(desc->len),
   1204   1.1    simonb 		le16toh(head->len), (int8_t)stat->rssi, head->rate, head->chan,
   1205   1.1    simonb 		le64toh(tail->tstamp)));
   1206   1.1    simonb 
   1207   1.1    simonb 	/*
   1208   1.1    simonb 	 * Discard Rx frames with bad CRC early (XXX we may want to pass them
   1209   1.1    simonb 	 * to radiotap in monitor mode).
   1210   1.1    simonb 	 */
   1211   1.1    simonb 	if ((le32toh(tail->flags) & WPI_RX_NOERROR) != WPI_RX_NOERROR) {
   1212   1.1    simonb 		DPRINTF(("rx tail flags error %x\n", le32toh(tail->flags)));
   1213   1.1    simonb 		ifp->if_ierrors++;
   1214   1.1    simonb 		return;
   1215   1.1    simonb 	}
   1216   1.1    simonb 
   1217   1.1    simonb 
   1218   1.1    simonb 
   1219  1.10  degroote 	/*
   1220  1.10  degroote 	 * If the number of free entry is too low
   1221  1.10  degroote 	 * just dup the data->m socket and reuse the same rbuf entry
   1222  1.10  degroote 	 */
   1223  1.10  degroote 	if (sc->rxq.nb_free_entries <= WPI_RBUF_LOW_LIMIT) {
   1224  1.10  degroote 
   1225  1.10  degroote 		/* Set length before calling m_dup */
   1226  1.10  degroote 		data->m->m_pkthdr.len = data->m->m_len = le16toh(head->len);
   1227  1.10  degroote 
   1228  1.10  degroote 		m = m_dup(data->m,0,M_COPYALL,M_DONTWAIT);
   1229  1.10  degroote 	} else {
   1230  1.10  degroote 
   1231  1.10  degroote 		MGETHDR(mnew, M_DONTWAIT, MT_DATA);
   1232  1.10  degroote 		if (mnew == NULL) {
   1233  1.10  degroote 			ifp->if_ierrors++;
   1234  1.10  degroote 			return;
   1235  1.10  degroote 		}
   1236  1.10  degroote 
   1237  1.10  degroote 		rbuf = wpi_alloc_rbuf(sc);
   1238  1.10  degroote 		KASSERT(rbuf != NULL);
   1239   1.1    simonb 
   1240  1.10  degroote  		/* attach Rx buffer to mbuf */
   1241  1.10  degroote 		MEXTADD(mnew, rbuf->vaddr, WPI_RBUF_SIZE, 0, wpi_free_rbuf,
   1242  1.10  degroote 		 	rbuf);
   1243  1.10  degroote 		mnew->m_flags |= M_EXT_RW;
   1244   1.1    simonb 
   1245  1.10  degroote 		m = data->m;
   1246  1.10  degroote 		data->m = mnew;
   1247   1.1    simonb 
   1248  1.10  degroote 		/* update Rx descriptor */
   1249  1.10  degroote 		ring->desc[ring->cur] = htole32(rbuf->paddr);
   1250  1.10  degroote 	}
   1251   1.1    simonb 
   1252   1.1    simonb 	/* finalize mbuf */
   1253   1.1    simonb 	m->m_pkthdr.rcvif = ifp;
   1254   1.9  christos 	m->m_data = (void *)(head + 1);
   1255   1.1    simonb 	m->m_pkthdr.len = m->m_len = le16toh(head->len);
   1256   1.1    simonb 
   1257   1.1    simonb #if NBPFILTER > 0
   1258   1.1    simonb 	if (sc->sc_drvbpf != NULL) {
   1259   1.1    simonb 		struct wpi_rx_radiotap_header *tap = &sc->sc_rxtap;
   1260   1.1    simonb 
   1261   1.1    simonb 		tap->wr_flags = 0;
   1262   1.1    simonb 		tap->wr_chan_freq =
   1263   1.1    simonb 			htole16(ic->ic_channels[head->chan].ic_freq);
   1264   1.1    simonb 		tap->wr_chan_flags =
   1265   1.1    simonb 			htole16(ic->ic_channels[head->chan].ic_flags);
   1266   1.1    simonb 		tap->wr_dbm_antsignal = (int8_t)(stat->rssi - WPI_RSSI_OFFSET);
   1267   1.1    simonb 		tap->wr_dbm_antnoise = (int8_t)le16toh(stat->noise);
   1268   1.1    simonb 		tap->wr_tsft = tail->tstamp;
   1269   1.1    simonb 		tap->wr_antenna = (le16toh(head->flags) >> 4) & 0xf;
   1270   1.1    simonb 		switch (head->rate) {
   1271   1.1    simonb 		/* CCK rates */
   1272   1.1    simonb 		case  10: tap->wr_rate =   2; break;
   1273   1.1    simonb 		case  20: tap->wr_rate =   4; break;
   1274   1.1    simonb 		case  55: tap->wr_rate =  11; break;
   1275   1.1    simonb 		case 110: tap->wr_rate =  22; break;
   1276   1.1    simonb 		/* OFDM rates */
   1277   1.1    simonb 		case 0xd: tap->wr_rate =  12; break;
   1278   1.1    simonb 		case 0xf: tap->wr_rate =  18; break;
   1279   1.1    simonb 		case 0x5: tap->wr_rate =  24; break;
   1280   1.1    simonb 		case 0x7: tap->wr_rate =  36; break;
   1281   1.1    simonb 		case 0x9: tap->wr_rate =  48; break;
   1282   1.1    simonb 		case 0xb: tap->wr_rate =  72; break;
   1283   1.1    simonb 		case 0x1: tap->wr_rate =  96; break;
   1284   1.1    simonb 		case 0x3: tap->wr_rate = 108; break;
   1285   1.1    simonb 		/* unknown rate: should not happen */
   1286   1.1    simonb 		default:  tap->wr_rate =   0;
   1287   1.1    simonb 		}
   1288   1.1    simonb 		if (le16toh(head->flags) & 0x4)
   1289   1.1    simonb 			tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
   1290   1.1    simonb 
   1291   1.1    simonb 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
   1292   1.1    simonb 	}
   1293   1.1    simonb #endif
   1294   1.1    simonb 
   1295   1.1    simonb 	/* grab a reference to the source node */
   1296   1.1    simonb 	wh = mtod(m, struct ieee80211_frame *);
   1297   1.1    simonb 	ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
   1298   1.1    simonb 
   1299   1.1    simonb 	/* send the frame to the 802.11 layer */
   1300   1.1    simonb 	ieee80211_input(ic, m, ni, stat->rssi, 0);
   1301   1.1    simonb 
   1302   1.1    simonb 	/* release node reference */
   1303   1.1    simonb 	ieee80211_free_node(ni);
   1304   1.1    simonb }
   1305   1.1    simonb 
   1306   1.1    simonb static void
   1307   1.1    simonb wpi_tx_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc)
   1308   1.1    simonb {
   1309   1.1    simonb 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
   1310   1.1    simonb 	struct wpi_tx_ring *ring = &sc->txq[desc->qid & 0x3];
   1311   1.1    simonb 	struct wpi_tx_data *txdata = &ring->data[desc->idx];
   1312   1.1    simonb 	struct wpi_tx_stat *stat = (struct wpi_tx_stat *)(desc + 1);
   1313   1.5     joerg 	struct wpi_node *wn = (struct wpi_node *)txdata->ni;
   1314   1.1    simonb 
   1315   1.1    simonb 	DPRINTFN(4, ("tx done: qid=%d idx=%d retries=%d nkill=%d rate=%x "
   1316   1.1    simonb 		"duration=%d status=%x\n", desc->qid, desc->idx, stat->ntries,
   1317   1.1    simonb 		stat->nkill, stat->rate, le32toh(stat->duration),
   1318   1.1    simonb 		le32toh(stat->status)));
   1319   1.1    simonb 
   1320   1.1    simonb 	/*
   1321   1.1    simonb 	 * Update rate control statistics for the node.
   1322   1.1    simonb 	 * XXX we should not count mgmt frames since they're always sent at
   1323   1.1    simonb 	 * the lowest available bit-rate.
   1324   1.1    simonb 	 */
   1325   1.5     joerg 	wn->amn.amn_txcnt++;
   1326   1.1    simonb 	if (stat->ntries > 0) {
   1327   1.1    simonb 		DPRINTFN(3, ("tx intr ntries %d\n", stat->ntries));
   1328   1.5     joerg 		wn->amn.amn_retrycnt++;
   1329   1.1    simonb 	}
   1330   1.1    simonb 
   1331   1.2     oster 	if ((le32toh(stat->status) & 0xff) != 1)
   1332   1.2     oster 		ifp->if_oerrors++;
   1333   1.2     oster 	else
   1334   1.2     oster 		ifp->if_opackets++;
   1335   1.2     oster 
   1336   1.1    simonb 	bus_dmamap_unload(sc->sc_dmat, txdata->map);
   1337   1.1    simonb 	m_freem(txdata->m);
   1338   1.1    simonb 	txdata->m = NULL;
   1339   1.1    simonb 	ieee80211_free_node(txdata->ni);
   1340   1.1    simonb 	txdata->ni = NULL;
   1341   1.1    simonb 
   1342   1.1    simonb 	ring->queued--;
   1343   1.1    simonb 
   1344   1.1    simonb 	sc->sc_tx_timer = 0;
   1345   1.1    simonb 	ifp->if_flags &= ~IFF_OACTIVE;
   1346   1.1    simonb 	wpi_start(ifp);
   1347   1.1    simonb }
   1348   1.1    simonb 
   1349   1.1    simonb static void
   1350   1.1    simonb wpi_cmd_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc)
   1351   1.1    simonb {
   1352   1.1    simonb 	struct wpi_tx_ring *ring = &sc->cmdq;
   1353   1.1    simonb 	struct wpi_tx_data *data;
   1354   1.1    simonb 
   1355   1.1    simonb 	if ((desc->qid & 7) != 4)
   1356   1.1    simonb 		return;	/* not a command ack */
   1357   1.1    simonb 
   1358   1.1    simonb 	data = &ring->data[desc->idx];
   1359   1.1    simonb 
   1360   1.1    simonb 	/* if the command was mapped in a mbuf, free it */
   1361   1.1    simonb 	if (data->m != NULL) {
   1362   1.1    simonb 		bus_dmamap_unload(sc->sc_dmat, data->map);
   1363   1.1    simonb 		m_freem(data->m);
   1364   1.1    simonb 		data->m = NULL;
   1365   1.1    simonb 	}
   1366   1.1    simonb 
   1367   1.1    simonb 	wakeup(&ring->cmd[desc->idx]);
   1368   1.1    simonb }
   1369   1.1    simonb 
   1370   1.1    simonb static void
   1371   1.1    simonb wpi_notif_intr(struct wpi_softc *sc)
   1372   1.1    simonb {
   1373   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
   1374   1.7  degroote 	struct ifnet *ifp =  ic->ic_ifp;
   1375   1.1    simonb 	struct wpi_rx_desc *desc;
   1376   1.1    simonb 	struct wpi_rx_data *data;
   1377   1.1    simonb 	uint32_t hw;
   1378   1.1    simonb 
   1379   1.1    simonb 	hw = le32toh(sc->shared->next);
   1380   1.1    simonb 	while (sc->rxq.cur != hw) {
   1381   1.1    simonb 		data = &sc->rxq.data[sc->rxq.cur];
   1382   1.1    simonb 
   1383   1.1    simonb 		desc = mtod(data->m, struct wpi_rx_desc *);
   1384   1.1    simonb 
   1385   1.1    simonb 		DPRINTFN(4, ("rx notification qid=%x idx=%d flags=%x type=%d "
   1386   1.1    simonb 			"len=%d\n", desc->qid, desc->idx, desc->flags,
   1387   1.1    simonb 			desc->type, le32toh(desc->len)));
   1388   1.1    simonb 
   1389   1.1    simonb 		if (!(desc->qid & 0x80))	/* reply to a command */
   1390   1.1    simonb 			wpi_cmd_intr(sc, desc);
   1391   1.1    simonb 
   1392   1.1    simonb 		switch (desc->type) {
   1393   1.1    simonb 		case WPI_RX_DONE:
   1394   1.1    simonb 			/* a 802.11 frame was received */
   1395   1.1    simonb 			wpi_rx_intr(sc, desc, data);
   1396   1.1    simonb 			break;
   1397   1.1    simonb 
   1398   1.1    simonb 		case WPI_TX_DONE:
   1399   1.1    simonb 			/* a 802.11 frame has been transmitted */
   1400   1.1    simonb 			wpi_tx_intr(sc, desc);
   1401   1.1    simonb 			break;
   1402   1.1    simonb 
   1403   1.1    simonb 		case WPI_UC_READY:
   1404   1.1    simonb 		{
   1405   1.1    simonb 			struct wpi_ucode_info *uc =
   1406   1.1    simonb 				(struct wpi_ucode_info *)(desc + 1);
   1407   1.1    simonb 
   1408   1.1    simonb 			/* the microcontroller is ready */
   1409   1.1    simonb 			DPRINTF(("microcode alive notification version %x "
   1410   1.1    simonb 				"alive %x\n", le32toh(uc->version),
   1411   1.1    simonb 				le32toh(uc->valid)));
   1412   1.1    simonb 
   1413   1.1    simonb 			if (le32toh(uc->valid) != 1) {
   1414   1.1    simonb 				aprint_error("%s: microcontroller "
   1415   1.1    simonb 					"initialization failed\n",
   1416   1.1    simonb 					sc->sc_dev.dv_xname);
   1417   1.1    simonb 			}
   1418   1.1    simonb 			break;
   1419   1.1    simonb 		}
   1420   1.1    simonb 		case WPI_STATE_CHANGED:
   1421   1.1    simonb 		{
   1422   1.1    simonb 			uint32_t *status = (uint32_t *)(desc + 1);
   1423   1.1    simonb 
   1424   1.1    simonb 			/* enabled/disabled notification */
   1425   1.1    simonb 			DPRINTF(("state changed to %x\n", le32toh(*status)));
   1426   1.1    simonb 
   1427   1.1    simonb 			if (le32toh(*status) & 1) {
   1428   1.1    simonb 				/* the radio button has to be pushed */
   1429   1.1    simonb 				aprint_error("%s: Radio transmitter is off\n",
   1430   1.1    simonb 					sc->sc_dev.dv_xname);
   1431   1.7  degroote 				/* turn the interface down */
   1432   1.7  degroote 				ifp->if_flags &= ~IFF_UP;
   1433   1.7  degroote 				wpi_stop(ifp, 1);
   1434   1.7  degroote 				return;	/* no further processing */
   1435   1.1    simonb 			}
   1436   1.1    simonb 			break;
   1437   1.1    simonb 		}
   1438   1.1    simonb 		case WPI_START_SCAN:
   1439   1.1    simonb 		{
   1440   1.1    simonb 			struct wpi_start_scan *scan =
   1441   1.1    simonb 				(struct wpi_start_scan *)(desc + 1);
   1442   1.1    simonb 
   1443   1.1    simonb 			DPRINTFN(2, ("scanning channel %d status %x\n",
   1444   1.1    simonb 				scan->chan, le32toh(scan->status)));
   1445   1.1    simonb 
   1446   1.1    simonb 			/* fix current channel */
   1447   1.1    simonb 			ic->ic_bss->ni_chan = &ic->ic_channels[scan->chan];
   1448   1.1    simonb 			break;
   1449   1.1    simonb 		}
   1450   1.1    simonb 		case WPI_STOP_SCAN:
   1451   1.1    simonb 		{
   1452   1.1    simonb 			struct wpi_stop_scan *scan =
   1453   1.1    simonb 				(struct wpi_stop_scan *)(desc + 1);
   1454   1.1    simonb 
   1455   1.1    simonb 			DPRINTF(("scan finished nchan=%d status=%d chan=%d\n",
   1456   1.1    simonb 				scan->nchan, scan->status, scan->chan));
   1457   1.1    simonb 
   1458   1.1    simonb 			if (scan->status == 1 && scan->chan <= 14) {
   1459   1.1    simonb 				/*
   1460   1.1    simonb 				 * We just finished scanning 802.11g channels,
   1461   1.1    simonb 				 * start scanning 802.11a ones.
   1462   1.1    simonb 				 */
   1463   1.1    simonb 				if (wpi_scan(sc, IEEE80211_CHAN_A) == 0)
   1464   1.1    simonb 					break;
   1465   1.1    simonb 			}
   1466   1.1    simonb 			ieee80211_end_scan(ic);
   1467   1.1    simonb 			break;
   1468   1.1    simonb 		}
   1469   1.1    simonb 		}
   1470   1.1    simonb 
   1471   1.1    simonb 		sc->rxq.cur = (sc->rxq.cur + 1) % WPI_RX_RING_COUNT;
   1472   1.1    simonb 	}
   1473   1.1    simonb 
   1474   1.1    simonb 	/* tell the firmware what we have processed */
   1475   1.1    simonb 	hw = (hw == 0) ? WPI_RX_RING_COUNT - 1 : hw - 1;
   1476   1.1    simonb 	WPI_WRITE(sc, WPI_RX_WIDX, hw & ~7);
   1477   1.1    simonb }
   1478   1.1    simonb 
   1479   1.1    simonb static int
   1480   1.1    simonb wpi_intr(void *arg)
   1481   1.1    simonb {
   1482   1.1    simonb 	struct wpi_softc *sc = arg;
   1483   1.7  degroote 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
   1484   1.1    simonb 	uint32_t r;
   1485   1.1    simonb 
   1486   1.1    simonb 	r = WPI_READ(sc, WPI_INTR);
   1487   1.1    simonb 	if (r == 0 || r == 0xffffffff)
   1488   1.1    simonb 		return 0;	/* not for us */
   1489   1.1    simonb 
   1490   1.1    simonb 	DPRINTFN(5, ("interrupt reg %x\n", r));
   1491   1.1    simonb 
   1492   1.1    simonb 	/* disable interrupts */
   1493   1.1    simonb 	WPI_WRITE(sc, WPI_MASK, 0);
   1494   1.1    simonb 	/* ack interrupts */
   1495   1.1    simonb 	WPI_WRITE(sc, WPI_INTR, r);
   1496   1.1    simonb 
   1497   1.1    simonb 	if (r & (WPI_SW_ERROR | WPI_HW_ERROR)) {
   1498   1.1    simonb 		aprint_error("%s: fatal firmware error\n", sc->sc_dev.dv_xname);
   1499   1.1    simonb 		sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP;
   1500   1.7  degroote 		wpi_stop(sc->sc_ic.ic_ifp, 1);
   1501   1.1    simonb 		return 1;
   1502   1.1    simonb 	}
   1503   1.1    simonb 
   1504   1.1    simonb 	if (r & WPI_RX_INTR)
   1505   1.1    simonb 		wpi_notif_intr(sc);
   1506   1.1    simonb 
   1507   1.1    simonb 	if (r & WPI_ALIVE_INTR)	/* firmware initialized */
   1508   1.1    simonb 		wakeup(sc);
   1509   1.1    simonb 
   1510   1.1    simonb 	/* re-enable interrupts */
   1511   1.7  degroote 	if (ifp->if_flags & IFF_UP)
   1512   1.7  degroote 		WPI_WRITE(sc, WPI_MASK, WPI_INTR_MASK);
   1513   1.1    simonb 
   1514   1.1    simonb 	return 1;
   1515   1.1    simonb }
   1516   1.1    simonb 
   1517   1.1    simonb static uint8_t
   1518   1.1    simonb wpi_plcp_signal(int rate)
   1519   1.1    simonb {
   1520   1.1    simonb 	switch (rate) {
   1521   1.1    simonb 	/* CCK rates (returned values are device-dependent) */
   1522   1.1    simonb 	case 2:		return 10;
   1523   1.1    simonb 	case 4:		return 20;
   1524   1.1    simonb 	case 11:	return 55;
   1525   1.1    simonb 	case 22:	return 110;
   1526   1.1    simonb 
   1527   1.1    simonb 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
   1528   1.1    simonb 	/* R1-R4, (u)ral is R4-R1 */
   1529   1.1    simonb 	case 12:	return 0xd;
   1530   1.1    simonb 	case 18:	return 0xf;
   1531   1.1    simonb 	case 24:	return 0x5;
   1532   1.1    simonb 	case 36:	return 0x7;
   1533   1.1    simonb 	case 48:	return 0x9;
   1534   1.1    simonb 	case 72:	return 0xb;
   1535   1.1    simonb 	case 96:	return 0x1;
   1536   1.1    simonb 	case 108:	return 0x3;
   1537   1.1    simonb 
   1538   1.1    simonb 	/* unsupported rates (should not get there) */
   1539   1.1    simonb 	default:	return 0;
   1540   1.1    simonb 	}
   1541   1.1    simonb }
   1542   1.1    simonb 
   1543   1.1    simonb /* quickly determine if a given rate is CCK or OFDM */
   1544   1.1    simonb #define WPI_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
   1545   1.1    simonb 
   1546   1.1    simonb static int
   1547   1.1    simonb wpi_tx_data(struct wpi_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
   1548   1.1    simonb 	int ac)
   1549   1.1    simonb {
   1550   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
   1551   1.1    simonb 	struct wpi_tx_ring *ring = &sc->txq[ac];
   1552   1.1    simonb 	struct wpi_tx_desc *desc;
   1553   1.1    simonb 	struct wpi_tx_data *data;
   1554   1.1    simonb 	struct wpi_tx_cmd *cmd;
   1555   1.1    simonb 	struct wpi_cmd_data *tx;
   1556   1.1    simonb 	struct ieee80211_frame *wh;
   1557   1.1    simonb 	struct ieee80211_key *k;
   1558   1.1    simonb 	const struct chanAccParams *cap;
   1559   1.1    simonb 	struct mbuf *mnew;
   1560   1.1    simonb 	int i, error, rate, hdrlen, noack = 0;
   1561   1.1    simonb 
   1562   1.1    simonb 	desc = &ring->desc[ring->cur];
   1563   1.1    simonb 	data = &ring->data[ring->cur];
   1564   1.1    simonb 
   1565   1.1    simonb 	wh = mtod(m0, struct ieee80211_frame *);
   1566   1.1    simonb 
   1567   1.1    simonb 	if (IEEE80211_QOS_HAS_SEQ(wh)) {
   1568   1.1    simonb 		hdrlen = sizeof (struct ieee80211_qosframe);
   1569   1.1    simonb 		cap = &ic->ic_wme.wme_chanParams;
   1570   1.1    simonb 		noack = cap->cap_wmeParams[ac].wmep_noackPolicy;
   1571   1.1    simonb 	} else
   1572   1.1    simonb 		hdrlen = sizeof (struct ieee80211_frame);
   1573   1.1    simonb 
   1574   1.1    simonb 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
   1575   1.1    simonb 		k = ieee80211_crypto_encap(ic, ni, m0);
   1576   1.1    simonb 		if (k == NULL) {
   1577   1.1    simonb 			m_freem(m0);
   1578   1.1    simonb 			return ENOBUFS;
   1579   1.1    simonb 		}
   1580   1.1    simonb 
   1581   1.1    simonb 		/* packet header may have moved, reset our local pointer */
   1582   1.1    simonb 		wh = mtod(m0, struct ieee80211_frame *);
   1583   1.1    simonb 	}
   1584   1.1    simonb 
   1585   1.1    simonb 	/* pickup a rate */
   1586   1.1    simonb 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
   1587   1.1    simonb 		IEEE80211_FC0_TYPE_MGT) {
   1588   1.1    simonb 		/* mgmt frames are sent at the lowest available bit-rate */
   1589   1.1    simonb 		rate = ni->ni_rates.rs_rates[0];
   1590   1.1    simonb 	} else {
   1591   1.1    simonb 		if (ic->ic_fixed_rate != -1) {
   1592   1.1    simonb 			rate = ic->ic_sup_rates[ic->ic_curmode].
   1593   1.1    simonb 				rs_rates[ic->ic_fixed_rate];
   1594   1.1    simonb 		} else
   1595   1.1    simonb 			rate = ni->ni_rates.rs_rates[ni->ni_txrate];
   1596   1.1    simonb 	}
   1597   1.1    simonb 	rate &= IEEE80211_RATE_VAL;
   1598   1.1    simonb 
   1599   1.1    simonb 
   1600   1.1    simonb #if NBPFILTER > 0
   1601   1.1    simonb 	if (sc->sc_drvbpf != NULL) {
   1602   1.1    simonb 		struct wpi_tx_radiotap_header *tap = &sc->sc_txtap;
   1603   1.1    simonb 
   1604   1.1    simonb 		tap->wt_flags = 0;
   1605   1.1    simonb 		tap->wt_chan_freq = htole16(ni->ni_chan->ic_freq);
   1606   1.1    simonb 		tap->wt_chan_flags = htole16(ni->ni_chan->ic_flags);
   1607   1.1    simonb 		tap->wt_rate = rate;
   1608   1.1    simonb 		tap->wt_hwqueue = ac;
   1609   1.1    simonb 		if (wh->i_fc[1] & IEEE80211_FC1_WEP)
   1610   1.1    simonb 			tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
   1611   1.1    simonb 
   1612   1.1    simonb 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
   1613   1.1    simonb 	}
   1614   1.1    simonb #endif
   1615   1.1    simonb 
   1616   1.1    simonb 	cmd = &ring->cmd[ring->cur];
   1617   1.1    simonb 	cmd->code = WPI_CMD_TX_DATA;
   1618   1.1    simonb 	cmd->flags = 0;
   1619   1.1    simonb 	cmd->qid = ring->qid;
   1620   1.1    simonb 	cmd->idx = ring->cur;
   1621   1.1    simonb 
   1622   1.1    simonb 	tx = (struct wpi_cmd_data *)cmd->data;
   1623   1.1    simonb 	tx->flags = 0;
   1624   1.1    simonb 
   1625   1.1    simonb 	if (!noack && !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
   1626   1.1    simonb 		tx->flags |= htole32(WPI_TX_NEED_ACK);
   1627   1.7  degroote 	} else if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > ic->ic_rtsthreshold)
   1628   1.7  degroote 		tx->flags |= htole32(WPI_TX_NEED_RTS | WPI_TX_FULL_TXOP);
   1629   1.1    simonb 
   1630   1.1    simonb 	tx->flags |= htole32(WPI_TX_AUTO_SEQ);
   1631   1.1    simonb 
   1632   1.7  degroote 	/* retrieve destination node's id */
   1633   1.7  degroote 	tx->id = IEEE80211_IS_MULTICAST(wh->i_addr1) ? WPI_ID_BROADCAST :
   1634   1.7  degroote 		WPI_ID_BSS;
   1635   1.7  degroote 
   1636   1.1    simonb 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
   1637   1.1    simonb 		IEEE80211_FC0_TYPE_MGT) {
   1638   1.1    simonb 		/* tell h/w to set timestamp in probe responses */
   1639   1.1    simonb 		if ((wh->i_fc[0] &
   1640   1.1    simonb 		    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
   1641   1.1    simonb 		    (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
   1642   1.1    simonb 			tx->flags |= htole32(WPI_TX_INSERT_TSTAMP);
   1643   1.1    simonb 
   1644   1.1    simonb 		if (((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
   1645   1.1    simonb 			 IEEE80211_FC0_SUBTYPE_ASSOC_REQ) ||
   1646   1.1    simonb 			((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
   1647   1.1    simonb 			 IEEE80211_FC0_SUBTYPE_REASSOC_REQ))
   1648   1.1    simonb 			tx->timeout = htole16(3);
   1649   1.1    simonb 		else
   1650   1.1    simonb 			tx->timeout = htole16(2);
   1651   1.1    simonb 	} else
   1652   1.1    simonb 		tx->timeout = htole16(0);
   1653   1.1    simonb 
   1654   1.1    simonb 	tx->rate = wpi_plcp_signal(rate);
   1655   1.1    simonb 
   1656   1.1    simonb 	/* be very persistant at sending frames out */
   1657   1.1    simonb 	tx->rts_ntries = 7;
   1658   1.1    simonb 	tx->data_ntries = 15;
   1659   1.1    simonb 
   1660   1.1    simonb 	tx->ofdm_mask = 0xff;
   1661   1.1    simonb 	tx->cck_mask = 0xf;
   1662   1.1    simonb 	tx->lifetime = htole32(0xffffffff);
   1663   1.1    simonb 
   1664   1.1    simonb 	tx->len = htole16(m0->m_pkthdr.len);
   1665   1.1    simonb 
   1666   1.1    simonb 	/* save and trim IEEE802.11 header */
   1667   1.9  christos 	m_copydata(m0, 0, hdrlen, (void *)&tx->wh);
   1668   1.1    simonb 	m_adj(m0, hdrlen);
   1669   1.1    simonb 
   1670   1.1    simonb 	error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
   1671   1.1    simonb 		BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   1672   1.1    simonb 	if (error != 0 && error != EFBIG) {
   1673   1.1    simonb 		aprint_error("%s: could not map mbuf (error %d)\n",
   1674   1.1    simonb 			sc->sc_dev.dv_xname, error);
   1675   1.1    simonb 		m_freem(m0);
   1676   1.1    simonb 		return error;
   1677   1.1    simonb 	}
   1678   1.1    simonb 	if (error != 0) {
   1679   1.1    simonb 		/* too many fragments, linearize */
   1680   1.1    simonb 		MGETHDR(mnew, M_DONTWAIT, MT_DATA);
   1681   1.1    simonb 		if (mnew == NULL) {
   1682   1.1    simonb 			m_freem(m0);
   1683   1.1    simonb 			return ENOMEM;
   1684   1.1    simonb 		}
   1685   1.1    simonb 
   1686   1.1    simonb 		M_COPY_PKTHDR(mnew, m0);
   1687   1.1    simonb 		if (m0->m_pkthdr.len > MHLEN) {
   1688   1.1    simonb 			MCLGET(mnew, M_DONTWAIT);
   1689   1.1    simonb 			if (!(mnew->m_flags & M_EXT)) {
   1690   1.1    simonb 				m_freem(m0);
   1691   1.1    simonb 				m_freem(mnew);
   1692   1.1    simonb 				return ENOMEM;
   1693   1.1    simonb 			}
   1694   1.1    simonb 		}
   1695   1.1    simonb 
   1696   1.9  christos 		m_copydata(m0, 0, m0->m_pkthdr.len, mtod(mnew, void *));
   1697   1.1    simonb 		m_freem(m0);
   1698   1.1    simonb 		mnew->m_len = mnew->m_pkthdr.len;
   1699   1.1    simonb 		m0 = mnew;
   1700   1.1    simonb 
   1701   1.1    simonb 		error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
   1702   1.1    simonb 			BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   1703   1.1    simonb 		if (error != 0) {
   1704   1.1    simonb 			aprint_error("%s: could not map mbuf (error %d)\n",
   1705   1.1    simonb 				sc->sc_dev.dv_xname, error);
   1706   1.1    simonb 			m_freem(m0);
   1707   1.1    simonb 			return error;
   1708   1.1    simonb 		}
   1709   1.1    simonb 	}
   1710   1.1    simonb 
   1711   1.1    simonb 	data->m = m0;
   1712   1.1    simonb 	data->ni = ni;
   1713   1.1    simonb 
   1714   1.1    simonb 	DPRINTFN(4, ("sending data: qid=%d idx=%d len=%d nsegs=%d\n",
   1715   1.1    simonb 		ring->qid, ring->cur, m0->m_pkthdr.len, data->map->dm_nsegs));
   1716   1.1    simonb 
   1717   1.1    simonb 	/* first scatter/gather segment is used by the tx data command */
   1718   1.1    simonb 	desc->flags = htole32(WPI_PAD32(m0->m_pkthdr.len) << 28 |
   1719   1.1    simonb 		(1 + data->map->dm_nsegs) << 24);
   1720   1.1    simonb 	desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
   1721   1.1    simonb 		ring->cur * sizeof (struct wpi_tx_cmd));
   1722   1.1    simonb 	/*XXX The next line might be wrong. I don't use hdrlen*/
   1723   1.1    simonb 	desc->segs[0].len  = htole32(4 + sizeof (struct wpi_cmd_data));
   1724   1.1    simonb 
   1725   1.1    simonb 	for (i = 1; i <= data->map->dm_nsegs; i++) {
   1726   1.1    simonb 		desc->segs[i].addr =
   1727   1.1    simonb 			htole32(data->map->dm_segs[i - 1].ds_addr);
   1728   1.1    simonb 		desc->segs[i].len  =
   1729   1.1    simonb 			htole32(data->map->dm_segs[i - 1].ds_len);
   1730   1.1    simonb 	}
   1731   1.1    simonb 
   1732   1.1    simonb 	ring->queued++;
   1733   1.1    simonb 
   1734   1.1    simonb 	/* kick ring */
   1735   1.1    simonb 	ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT;
   1736   1.1    simonb 	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
   1737   1.1    simonb 
   1738   1.1    simonb 	return 0;
   1739   1.1    simonb }
   1740   1.1    simonb 
   1741   1.1    simonb static void
   1742   1.1    simonb wpi_start(struct ifnet *ifp)
   1743   1.1    simonb {
   1744   1.1    simonb 	struct wpi_softc *sc = ifp->if_softc;
   1745   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
   1746   1.1    simonb 	struct ieee80211_node *ni;
   1747   1.1    simonb 	struct ether_header *eh;
   1748   1.1    simonb 	struct mbuf *m0;
   1749   1.1    simonb 	int ac;
   1750   1.1    simonb 
   1751   1.1    simonb 	/*
   1752   1.1    simonb 	 * net80211 may still try to send management frames even if the
   1753   1.1    simonb 	 * IFF_RUNNING flag is not set...
   1754   1.1    simonb 	 */
   1755   1.1    simonb 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
   1756   1.1    simonb 		return;
   1757   1.1    simonb 
   1758   1.1    simonb 	for (;;) {
   1759   1.1    simonb 		IF_POLL(&ic->ic_mgtq, m0);
   1760   1.1    simonb 		if (m0 != NULL) {
   1761   1.1    simonb 			IF_DEQUEUE(&ic->ic_mgtq, m0);
   1762   1.1    simonb 
   1763   1.1    simonb 			ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
   1764   1.1    simonb 			m0->m_pkthdr.rcvif = NULL;
   1765   1.1    simonb 
   1766   1.1    simonb 			/* management frames go into ring 0 */
   1767   1.1    simonb 			if (sc->txq[0].queued > sc->txq[0].count - 8) {
   1768   1.1    simonb 				ifp->if_oerrors++;
   1769   1.1    simonb 				continue;
   1770   1.1    simonb 			}
   1771   1.1    simonb #if NBPFILTER > 0
   1772   1.1    simonb 			if (ic->ic_rawbpf != NULL)
   1773   1.1    simonb 				bpf_mtap(ic->ic_rawbpf, m0);
   1774   1.1    simonb #endif
   1775   1.1    simonb 			if (wpi_tx_data(sc, m0, ni, 0) != 0) {
   1776   1.1    simonb 				ifp->if_oerrors++;
   1777   1.1    simonb 				break;
   1778   1.1    simonb 			}
   1779   1.1    simonb 		} else {
   1780   1.1    simonb 			if (ic->ic_state != IEEE80211_S_RUN)
   1781   1.1    simonb 				break;
   1782   1.7  degroote 			IFQ_POLL(&ifp->if_snd, m0);
   1783   1.1    simonb 			if (m0 == NULL)
   1784   1.1    simonb 				break;
   1785   1.1    simonb 
   1786   1.1    simonb 			if (m0->m_len < sizeof (*eh) &&
   1787   1.1    simonb 			    (m0 = m_pullup(m0, sizeof (*eh))) != NULL) {
   1788   1.1    simonb 				ifp->if_oerrors++;
   1789   1.1    simonb 				continue;
   1790   1.1    simonb 			}
   1791   1.1    simonb 			eh = mtod(m0, struct ether_header *);
   1792   1.1    simonb 			ni = ieee80211_find_txnode(ic, eh->ether_dhost);
   1793   1.1    simonb 			if (ni == NULL) {
   1794   1.1    simonb 				m_freem(m0);
   1795   1.1    simonb 				ifp->if_oerrors++;
   1796   1.1    simonb 				continue;
   1797   1.1    simonb 			}
   1798   1.1    simonb 
   1799   1.1    simonb 			/* classify mbuf so we can find which tx ring to use */
   1800   1.1    simonb 			if (ieee80211_classify(ic, m0, ni) != 0) {
   1801   1.1    simonb 				m_freem(m0);
   1802   1.1    simonb 				ieee80211_free_node(ni);
   1803   1.1    simonb 				ifp->if_oerrors++;
   1804   1.1    simonb 				continue;
   1805   1.1    simonb 			}
   1806   1.1    simonb 
   1807   1.1    simonb 			/* no QoS encapsulation for EAPOL frames */
   1808   1.1    simonb 			ac = (eh->ether_type != htons(ETHERTYPE_PAE)) ?
   1809   1.1    simonb 			    M_WME_GETAC(m0) : WME_AC_BE;
   1810   1.1    simonb 
   1811   1.1    simonb 			if (sc->txq[ac].queued > sc->txq[ac].count - 8) {
   1812   1.1    simonb 				/* there is no place left in this ring */
   1813   1.1    simonb 				ifp->if_flags |= IFF_OACTIVE;
   1814   1.1    simonb 				break;
   1815   1.1    simonb 			}
   1816   1.7  degroote 			IFQ_DEQUEUE(&ifp->if_snd, m0);
   1817   1.1    simonb #if NBPFILTER > 0
   1818   1.1    simonb 			if (ifp->if_bpf != NULL)
   1819   1.1    simonb 				bpf_mtap(ifp->if_bpf, m0);
   1820   1.1    simonb #endif
   1821   1.1    simonb 			m0 = ieee80211_encap(ic, m0, ni);
   1822   1.1    simonb 			if (m0 == NULL) {
   1823   1.1    simonb 				ieee80211_free_node(ni);
   1824   1.1    simonb 				ifp->if_oerrors++;
   1825   1.1    simonb 				continue;
   1826   1.1    simonb 			}
   1827   1.1    simonb #if NBPFILTER > 0
   1828   1.1    simonb 			if (ic->ic_rawbpf != NULL)
   1829   1.1    simonb 				bpf_mtap(ic->ic_rawbpf, m0);
   1830   1.1    simonb #endif
   1831   1.1    simonb 			if (wpi_tx_data(sc, m0, ni, ac) != 0) {
   1832   1.1    simonb 				ieee80211_free_node(ni);
   1833   1.1    simonb 				ifp->if_oerrors++;
   1834   1.1    simonb 				break;
   1835   1.1    simonb 			}
   1836   1.1    simonb 		}
   1837   1.1    simonb 
   1838   1.1    simonb 		sc->sc_tx_timer = 5;
   1839   1.1    simonb 		ifp->if_timer = 1;
   1840   1.1    simonb 	}
   1841   1.1    simonb }
   1842   1.1    simonb 
   1843   1.1    simonb static void
   1844   1.1    simonb wpi_watchdog(struct ifnet *ifp)
   1845   1.1    simonb {
   1846   1.1    simonb 	struct wpi_softc *sc = ifp->if_softc;
   1847   1.1    simonb 
   1848   1.1    simonb 	ifp->if_timer = 0;
   1849   1.1    simonb 
   1850   1.1    simonb 	if (sc->sc_tx_timer > 0) {
   1851   1.1    simonb 		if (--sc->sc_tx_timer == 0) {
   1852   1.1    simonb 			aprint_error("%s: device timeout\n",
   1853   1.1    simonb 				sc->sc_dev.dv_xname);
   1854   1.1    simonb 			ifp->if_oerrors++;
   1855   1.1    simonb 			ifp->if_flags &= ~IFF_UP;
   1856   1.1    simonb 			wpi_stop(ifp, 1);
   1857   1.1    simonb 			return;
   1858   1.1    simonb 		}
   1859   1.1    simonb 		ifp->if_timer = 1;
   1860   1.1    simonb 	}
   1861   1.1    simonb 
   1862   1.1    simonb 	ieee80211_watchdog(&sc->sc_ic);
   1863   1.1    simonb }
   1864   1.1    simonb 
   1865   1.1    simonb static int
   1866   1.9  christos wpi_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1867   1.1    simonb {
   1868   1.1    simonb #define IS_RUNNING(ifp) \
   1869   1.1    simonb 	((ifp->if_flags & IFF_UP) && (ifp->if_flags & IFF_RUNNING))
   1870   1.1    simonb 
   1871   1.1    simonb 	struct wpi_softc *sc = ifp->if_softc;
   1872   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
   1873   1.1    simonb 	struct ifreq *ifr = (struct ifreq *)data;
   1874   1.1    simonb 	int s, error = 0;
   1875   1.1    simonb 
   1876   1.1    simonb 	s = splnet();
   1877   1.1    simonb 
   1878   1.1    simonb 	switch (cmd) {
   1879   1.1    simonb 	case SIOCSIFFLAGS:
   1880   1.1    simonb 		if (ifp->if_flags & IFF_UP) {
   1881   1.1    simonb 			if (!(ifp->if_flags & IFF_RUNNING))
   1882   1.1    simonb 				wpi_init(ifp);
   1883   1.1    simonb 		} else {
   1884   1.1    simonb 			if (ifp->if_flags & IFF_RUNNING)
   1885   1.1    simonb 				wpi_stop(ifp, 1);
   1886   1.1    simonb 		}
   1887   1.1    simonb 		break;
   1888   1.1    simonb 
   1889   1.1    simonb 	case SIOCADDMULTI:
   1890   1.1    simonb 	case SIOCDELMULTI:
   1891   1.1    simonb 		error = (cmd == SIOCADDMULTI) ?
   1892   1.1    simonb 			ether_addmulti(ifr, &sc->sc_ec) :
   1893   1.1    simonb 			ether_delmulti(ifr, &sc->sc_ec);
   1894   1.1    simonb 		if (error == ENETRESET) {
   1895   1.1    simonb 			/* setup multicast filter, etc */
   1896   1.1    simonb 			error = 0;
   1897   1.1    simonb 		}
   1898   1.1    simonb 		break;
   1899   1.1    simonb 
   1900   1.1    simonb 	default:
   1901   1.7  degroote 		error = ieee80211_ioctl(ic, cmd, data);
   1902   1.1    simonb 	}
   1903   1.1    simonb 
   1904   1.1    simonb 	if (error == ENETRESET) {
   1905   1.1    simonb 		if (IS_RUNNING(ifp) &&
   1906   1.1    simonb 			(ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
   1907   1.1    simonb 			wpi_init(ifp);
   1908   1.1    simonb 		error = 0;
   1909   1.1    simonb 	}
   1910   1.1    simonb 
   1911   1.1    simonb 	splx(s);
   1912   1.1    simonb 	return error;
   1913   1.1    simonb 
   1914   1.1    simonb #undef IS_RUNNING
   1915   1.1    simonb }
   1916   1.1    simonb 
   1917   1.1    simonb /*
   1918   1.1    simonb  * Extract various information from EEPROM.
   1919   1.1    simonb  */
   1920   1.1    simonb static void
   1921   1.1    simonb wpi_read_eeprom(struct wpi_softc *sc)
   1922   1.1    simonb {
   1923   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
   1924   1.1    simonb 	uint16_t val;
   1925   1.1    simonb 	int i;
   1926   1.1    simonb 
   1927   1.1    simonb 	/* read MAC address */
   1928   1.1    simonb 	val = wpi_read_prom_word(sc, WPI_EEPROM_MAC + 0);
   1929   1.1    simonb 	ic->ic_myaddr[0] = val & 0xff;
   1930   1.1    simonb 	ic->ic_myaddr[1] = val >> 8;
   1931   1.1    simonb 	val = wpi_read_prom_word(sc, WPI_EEPROM_MAC + 1);
   1932   1.1    simonb 	ic->ic_myaddr[2] = val & 0xff;
   1933   1.1    simonb 	ic->ic_myaddr[3] = val >> 8;
   1934   1.1    simonb 	val = wpi_read_prom_word(sc, WPI_EEPROM_MAC + 2);
   1935   1.1    simonb 	ic->ic_myaddr[4] = val & 0xff;
   1936   1.1    simonb 	ic->ic_myaddr[5] = val >> 8;
   1937   1.1    simonb 
   1938   1.1    simonb 	/* read power settings for 2.4GHz channels */
   1939   1.1    simonb 	for (i = 0; i < 14; i++) {
   1940   1.1    simonb 		sc->pwr1[i] = wpi_read_prom_word(sc, WPI_EEPROM_PWR1 + i);
   1941   1.1    simonb 		sc->pwr2[i] = wpi_read_prom_word(sc, WPI_EEPROM_PWR2 + i);
   1942   1.1    simonb 		DPRINTFN(2, ("channel %d pwr1 0x%04x pwr2 0x%04x\n", i + 1,
   1943   1.1    simonb 			sc->pwr1[i], sc->pwr2[i]));
   1944   1.1    simonb 	}
   1945   1.1    simonb }
   1946   1.1    simonb 
   1947   1.1    simonb /*
   1948   1.1    simonb  * Send a command to the firmware.
   1949   1.1    simonb  */
   1950   1.1    simonb static int
   1951   1.1    simonb wpi_cmd(struct wpi_softc *sc, int code, const void *buf, int size, int async)
   1952   1.1    simonb {
   1953   1.1    simonb 	struct wpi_tx_ring *ring = &sc->cmdq;
   1954   1.1    simonb 	struct wpi_tx_desc *desc;
   1955   1.1    simonb 	struct wpi_tx_cmd *cmd;
   1956   1.1    simonb 
   1957   1.1    simonb 	KASSERT(size <= sizeof cmd->data);
   1958   1.1    simonb 
   1959   1.1    simonb 	desc = &ring->desc[ring->cur];
   1960   1.1    simonb 	cmd = &ring->cmd[ring->cur];
   1961   1.1    simonb 
   1962   1.1    simonb 	cmd->code = code;
   1963   1.1    simonb 	cmd->flags = 0;
   1964   1.1    simonb 	cmd->qid = ring->qid;
   1965   1.1    simonb 	cmd->idx = ring->cur;
   1966   1.1    simonb 	memcpy(cmd->data, buf, size);
   1967   1.1    simonb 
   1968   1.1    simonb 	desc->flags = htole32(WPI_PAD32(size) << 28 | 1 << 24);
   1969   1.1    simonb 	desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
   1970   1.1    simonb 		ring->cur * sizeof (struct wpi_tx_cmd));
   1971   1.1    simonb 	desc->segs[0].len  = htole32(4 + size);
   1972   1.1    simonb 
   1973   1.1    simonb 	/* kick cmd ring */
   1974   1.1    simonb 	ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
   1975   1.1    simonb 	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
   1976   1.1    simonb 
   1977   1.1    simonb 	return async ? 0 : tsleep(cmd, PCATCH, "wpicmd", hz);
   1978   1.1    simonb }
   1979   1.1    simonb 
   1980   1.1    simonb static int
   1981   1.1    simonb wpi_wme_update(struct ieee80211com *ic)
   1982   1.1    simonb {
   1983   1.1    simonb #define WPI_EXP2(v)	htole16((1 << (v)) - 1)
   1984   1.1    simonb #define WPI_USEC(v)	htole16(IEEE80211_TXOP_TO_US(v))
   1985   1.1    simonb 	struct wpi_softc *sc = ic->ic_ifp->if_softc;
   1986   1.1    simonb 	const struct wmeParams *wmep;
   1987   1.1    simonb 	struct wpi_wme_setup wme;
   1988   1.1    simonb 	int ac;
   1989   1.1    simonb 
   1990   1.1    simonb 	/* don't override default WME values if WME is not actually enabled */
   1991   1.1    simonb 	if (!(ic->ic_flags & IEEE80211_F_WME))
   1992   1.1    simonb 		return 0;
   1993   1.1    simonb 
   1994   1.1    simonb 	wme.flags = 0;
   1995   1.1    simonb 	for (ac = 0; ac < WME_NUM_AC; ac++) {
   1996   1.1    simonb 		wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
   1997   1.1    simonb 		wme.ac[ac].aifsn = wmep->wmep_aifsn;
   1998   1.1    simonb 		wme.ac[ac].cwmin = WPI_EXP2(wmep->wmep_logcwmin);
   1999   1.1    simonb 		wme.ac[ac].cwmax = WPI_EXP2(wmep->wmep_logcwmax);
   2000   1.1    simonb 		wme.ac[ac].txop  = WPI_USEC(wmep->wmep_txopLimit);
   2001   1.1    simonb 
   2002   1.1    simonb 		DPRINTF(("setting WME for queue %d aifsn=%d cwmin=%d cwmax=%d "
   2003   1.1    simonb 		    "txop=%d\n", ac, wme.ac[ac].aifsn, wme.ac[ac].cwmin,
   2004   1.1    simonb 		    wme.ac[ac].cwmax, wme.ac[ac].txop));
   2005   1.1    simonb 	}
   2006   1.1    simonb 
   2007   1.1    simonb 	return wpi_cmd(sc, WPI_CMD_SET_WME, &wme, sizeof wme, 1);
   2008   1.1    simonb #undef WPI_USEC
   2009   1.1    simonb #undef WPI_EXP2
   2010   1.1    simonb }
   2011   1.1    simonb 
   2012   1.1    simonb /*
   2013   1.1    simonb  * Configure h/w multi-rate retries.
   2014   1.1    simonb  */
   2015   1.1    simonb static int
   2016   1.1    simonb wpi_mrr_setup(struct wpi_softc *sc)
   2017   1.1    simonb {
   2018   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
   2019   1.1    simonb 	struct wpi_mrr_setup mrr;
   2020   1.1    simonb 	int i, error;
   2021   1.1    simonb 
   2022   1.1    simonb 	/* CCK rates (not used with 802.11a) */
   2023   1.1    simonb 	for (i = WPI_CCK1; i <= WPI_CCK11; i++) {
   2024   1.1    simonb 		mrr.rates[i].flags = 0;
   2025   1.1    simonb 		mrr.rates[i].plcp = wpi_ridx_to_plcp[i];
   2026   1.1    simonb 		/* fallback to the immediate lower CCK rate (if any) */
   2027   1.1    simonb 		mrr.rates[i].next = (i == WPI_CCK1) ? WPI_CCK1 : i - 1;
   2028   1.1    simonb 		/* try one time at this rate before falling back to "next" */
   2029   1.1    simonb 		mrr.rates[i].ntries = 1;
   2030   1.1    simonb 	}
   2031   1.1    simonb 
   2032   1.1    simonb 	/* OFDM rates (not used with 802.11b) */
   2033   1.1    simonb 	for (i = WPI_OFDM6; i <= WPI_OFDM54; i++) {
   2034   1.1    simonb 		mrr.rates[i].flags = 0;
   2035   1.1    simonb 		mrr.rates[i].plcp = wpi_ridx_to_plcp[i];
   2036   1.1    simonb 		/* fallback to the immediate lower rate (if any) */
   2037   1.1    simonb 		/* we allow fallback from OFDM/6 to CCK/2 in 11b/g mode */
   2038   1.1    simonb 		mrr.rates[i].next = (i == WPI_OFDM6) ?
   2039   1.1    simonb 		    ((ic->ic_curmode == IEEE80211_MODE_11A) ?
   2040   1.1    simonb 			WPI_OFDM6 : WPI_CCK2) :
   2041   1.1    simonb 		    i - 1;
   2042   1.1    simonb 		/* try one time at this rate before falling back to "next" */
   2043   1.1    simonb 		mrr.rates[i].ntries = 1;
   2044   1.1    simonb 	}
   2045   1.1    simonb 
   2046   1.1    simonb 	/* setup MRR for control frames */
   2047   1.1    simonb 	mrr.which = htole32(WPI_MRR_CTL);
   2048   1.1    simonb 	error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 1);
   2049   1.1    simonb 	if (error != 0) {
   2050   1.1    simonb 		aprint_error("%s: could not setup MRR for control frames\n",
   2051   1.1    simonb 			sc->sc_dev.dv_xname);
   2052   1.1    simonb 		return error;
   2053   1.1    simonb 	}
   2054   1.1    simonb 
   2055   1.1    simonb 	/* setup MRR for data frames */
   2056   1.1    simonb 	mrr.which = htole32(WPI_MRR_DATA);
   2057   1.1    simonb 	error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 1);
   2058   1.1    simonb 	if (error != 0) {
   2059   1.1    simonb 		aprint_error("%s: could not setup MRR for data frames\n",
   2060   1.1    simonb 			sc->sc_dev.dv_xname);
   2061   1.1    simonb 		return error;
   2062   1.1    simonb 	}
   2063   1.1    simonb 
   2064   1.1    simonb 	return 0;
   2065   1.1    simonb }
   2066   1.1    simonb 
   2067   1.1    simonb static void
   2068   1.1    simonb wpi_set_led(struct wpi_softc *sc, uint8_t which, uint8_t off, uint8_t on)
   2069   1.1    simonb {
   2070   1.1    simonb 	struct wpi_cmd_led led;
   2071   1.1    simonb 
   2072   1.1    simonb 	led.which = which;
   2073   1.1    simonb 	led.unit = htole32(100000);	/* on/off in unit of 100ms */
   2074   1.1    simonb 	led.off = off;
   2075   1.1    simonb 	led.on = on;
   2076   1.1    simonb 
   2077   1.1    simonb 	(void)wpi_cmd(sc, WPI_CMD_SET_LED, &led, sizeof led, 1);
   2078   1.1    simonb }
   2079   1.1    simonb 
   2080   1.1    simonb static void
   2081   1.1    simonb wpi_enable_tsf(struct wpi_softc *sc, struct ieee80211_node *ni)
   2082   1.1    simonb {
   2083   1.1    simonb 	struct wpi_cmd_tsf tsf;
   2084   1.1    simonb 	uint64_t val, mod;
   2085   1.1    simonb 
   2086   1.1    simonb 	memset(&tsf, 0, sizeof tsf);
   2087   1.1    simonb 	memcpy(&tsf.tstamp, ni->ni_tstamp.data, 8);
   2088   1.1    simonb 	tsf.bintval = htole16(ni->ni_intval);
   2089   1.1    simonb 	tsf.lintval = htole16(10);
   2090   1.1    simonb 
   2091   1.1    simonb 	/* compute remaining time until next beacon */
   2092   1.1    simonb 	val = (uint64_t)ni->ni_intval  * 1024;	/* msecs -> usecs */
   2093   1.1    simonb 	mod = le64toh(tsf.tstamp) % val;
   2094   1.1    simonb 	tsf.binitval = htole32((uint32_t)(val - mod));
   2095   1.1    simonb 
   2096   1.1    simonb 	DPRINTF(("TSF bintval=%u tstamp=%llu, init=%u\n",
   2097   1.1    simonb 	    ni->ni_intval, le64toh(tsf.tstamp), (uint32_t)(val - mod)));
   2098   1.1    simonb 
   2099   1.1    simonb 	if (wpi_cmd(sc, WPI_CMD_TSF, &tsf, sizeof tsf, 1) != 0)
   2100   1.1    simonb 		aprint_error("%s: could not enable TSF\n", sc->sc_dev.dv_xname);
   2101   1.1    simonb }
   2102   1.1    simonb 
   2103   1.1    simonb /*
   2104   1.1    simonb  * Build a beacon frame that the firmware will broadcast periodically in
   2105   1.1    simonb  * IBSS or HostAP modes.
   2106   1.1    simonb  */
   2107   1.1    simonb static int
   2108   1.1    simonb wpi_setup_beacon(struct wpi_softc *sc, struct ieee80211_node *ni)
   2109   1.1    simonb {
   2110   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
   2111   1.1    simonb 	struct wpi_tx_ring *ring = &sc->cmdq;
   2112   1.1    simonb 	struct wpi_tx_desc *desc;
   2113   1.1    simonb 	struct wpi_tx_data *data;
   2114   1.1    simonb 	struct wpi_tx_cmd *cmd;
   2115   1.1    simonb 	struct wpi_cmd_beacon *bcn;
   2116   1.1    simonb 	struct ieee80211_beacon_offsets bo;
   2117   1.1    simonb 	struct mbuf *m0;
   2118   1.1    simonb 	int error;
   2119   1.1    simonb 
   2120   1.1    simonb 	desc = &ring->desc[ring->cur];
   2121   1.1    simonb 	data = &ring->data[ring->cur];
   2122   1.1    simonb 
   2123   1.1    simonb 	m0 = ieee80211_beacon_alloc(ic, ni, &bo);
   2124   1.1    simonb 	if (m0 == NULL) {
   2125   1.1    simonb 		aprint_error("%s: could not allocate beacon frame\n",
   2126   1.1    simonb 			sc->sc_dev.dv_xname);
   2127   1.1    simonb 		return ENOMEM;
   2128   1.1    simonb 	}
   2129   1.1    simonb 
   2130   1.1    simonb 	cmd = &ring->cmd[ring->cur];
   2131   1.1    simonb 	cmd->code = WPI_CMD_SET_BEACON;
   2132   1.1    simonb 	cmd->flags = 0;
   2133   1.1    simonb 	cmd->qid = ring->qid;
   2134   1.1    simonb 	cmd->idx = ring->cur;
   2135   1.1    simonb 
   2136   1.1    simonb 	bcn = (struct wpi_cmd_beacon *)cmd->data;
   2137   1.1    simonb 	memset(bcn, 0, sizeof (struct wpi_cmd_beacon));
   2138   1.1    simonb 	bcn->id = WPI_ID_BROADCAST;
   2139   1.1    simonb 	bcn->ofdm_mask = 0xff;
   2140   1.1    simonb 	bcn->cck_mask = 0x0f;
   2141   1.1    simonb 	bcn->lifetime = htole32(0xffffffff);
   2142   1.1    simonb 	bcn->len = htole16(m0->m_pkthdr.len);
   2143   1.1    simonb 	bcn->rate = (ic->ic_curmode == IEEE80211_MODE_11A) ?
   2144   1.1    simonb 		wpi_plcp_signal(12) : wpi_plcp_signal(2);
   2145   1.1    simonb 	bcn->flags = htole32(WPI_TX_AUTO_SEQ | WPI_TX_INSERT_TSTAMP);
   2146   1.1    simonb 
   2147   1.1    simonb 	/* save and trim IEEE802.11 header */
   2148   1.9  christos 	m_copydata(m0, 0, sizeof (struct ieee80211_frame), (void *)&bcn->wh);
   2149   1.1    simonb 	m_adj(m0, sizeof (struct ieee80211_frame));
   2150   1.1    simonb 
   2151   1.1    simonb 	/* assume beacon frame is contiguous */
   2152   1.1    simonb 	error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
   2153   1.1    simonb 		BUS_DMA_READ | BUS_DMA_NOWAIT);
   2154   1.1    simonb 	if (error) {
   2155   1.1    simonb 		aprint_error("%s: could not map beacon\n", sc->sc_dev.dv_xname);
   2156   1.1    simonb 		m_freem(m0);
   2157   1.1    simonb 		return error;
   2158   1.1    simonb 	}
   2159   1.1    simonb 
   2160   1.1    simonb 	data->m = m0;
   2161   1.1    simonb 
   2162   1.1    simonb 	/* first scatter/gather segment is used by the beacon command */
   2163   1.1    simonb 	desc->flags = htole32(WPI_PAD32(m0->m_pkthdr.len) << 28 | 2 << 24);
   2164   1.1    simonb 	desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
   2165   1.1    simonb 		ring->cur * sizeof (struct wpi_tx_cmd));
   2166   1.1    simonb 	desc->segs[0].len  = htole32(4 + sizeof (struct wpi_cmd_beacon));
   2167   1.1    simonb 	desc->segs[1].addr = htole32(data->map->dm_segs[0].ds_addr);
   2168   1.1    simonb 	desc->segs[1].len  = htole32(data->map->dm_segs[0].ds_len);
   2169   1.1    simonb 
   2170   1.1    simonb 	/* kick cmd ring */
   2171   1.1    simonb 	ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
   2172   1.1    simonb 	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
   2173   1.1    simonb 
   2174   1.1    simonb 	return 0;
   2175   1.1    simonb }
   2176   1.1    simonb 
   2177   1.1    simonb static int
   2178   1.1    simonb wpi_auth(struct wpi_softc *sc)
   2179   1.1    simonb {
   2180   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
   2181   1.1    simonb 	struct ieee80211_node *ni = ic->ic_bss;
   2182   1.5     joerg 	struct wpi_node_info node;
   2183   1.1    simonb 	int error;
   2184   1.1    simonb 
   2185   1.1    simonb 	/* update adapter's configuration */
   2186   1.1    simonb 	IEEE80211_ADDR_COPY(sc->config.bssid, ni->ni_bssid);
   2187   1.1    simonb 	sc->config.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
   2188   1.1    simonb 	sc->config.flags = htole32(WPI_CONFIG_TSF);
   2189   1.1    simonb 	if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
   2190   1.1    simonb 		sc->config.flags |= htole32(WPI_CONFIG_AUTO |
   2191   1.1    simonb 		    WPI_CONFIG_24GHZ);
   2192   1.1    simonb 	}
   2193   1.1    simonb 	switch (ic->ic_curmode) {
   2194   1.1    simonb 	case IEEE80211_MODE_11A:
   2195   1.1    simonb 		sc->config.cck_mask  = 0;
   2196   1.1    simonb 		sc->config.ofdm_mask = 0x15;
   2197   1.1    simonb 		break;
   2198   1.1    simonb 	case IEEE80211_MODE_11B:
   2199   1.1    simonb 		sc->config.cck_mask  = 0x03;
   2200   1.1    simonb 		sc->config.ofdm_mask = 0;
   2201   1.1    simonb 		break;
   2202   1.1    simonb 	default:	/* assume 802.11b/g */
   2203   1.1    simonb 		sc->config.cck_mask  = 0x0f;
   2204   1.1    simonb 		sc->config.ofdm_mask = 0x15;
   2205   1.1    simonb 	}
   2206   1.1    simonb 
   2207   1.1    simonb 	DPRINTF(("config chan %d flags %x cck %x ofdm %x\n", sc->config.chan,
   2208   1.1    simonb 		sc->config.flags, sc->config.cck_mask, sc->config.ofdm_mask));
   2209   1.1    simonb 	error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config,
   2210   1.1    simonb 		sizeof (struct wpi_config), 1);
   2211   1.1    simonb 	if (error != 0) {
   2212   1.1    simonb 		aprint_error("%s: could not configure\n", sc->sc_dev.dv_xname);
   2213   1.1    simonb 		return error;
   2214   1.1    simonb 	}
   2215   1.1    simonb 
   2216   1.1    simonb 	/* add default node */
   2217   1.1    simonb 	memset(&node, 0, sizeof node);
   2218   1.1    simonb 	IEEE80211_ADDR_COPY(node.bssid, ni->ni_bssid);
   2219   1.1    simonb 	node.id = WPI_ID_BSS;
   2220   1.1    simonb 	node.rate = (ic->ic_curmode == IEEE80211_MODE_11A) ?
   2221   1.1    simonb 	    wpi_plcp_signal(12) : wpi_plcp_signal(2);
   2222   1.1    simonb 	error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
   2223   1.1    simonb 	if (error != 0) {
   2224   1.1    simonb 		aprint_error("%s: could not add BSS node\n", sc->sc_dev.dv_xname);
   2225   1.1    simonb 		return error;
   2226   1.1    simonb 	}
   2227   1.1    simonb 
   2228   1.1    simonb 	error = wpi_mrr_setup(sc);
   2229   1.1    simonb 	if (error != 0) {
   2230   1.1    simonb 		aprint_error("%s: could not setup MRR\n", sc->sc_dev.dv_xname);
   2231   1.1    simonb 		return error;
   2232   1.1    simonb 	}
   2233   1.1    simonb 
   2234   1.1    simonb 	return 0;
   2235   1.1    simonb }
   2236   1.1    simonb 
   2237   1.1    simonb /*
   2238   1.1    simonb  * Send a scan request to the firmware.  Since this command is huge, we map it
   2239   1.1    simonb  * into a mbuf instead of using the pre-allocated set of commands.
   2240   1.1    simonb  */
   2241   1.1    simonb static int
   2242   1.1    simonb wpi_scan(struct wpi_softc *sc, uint16_t flags)
   2243   1.1    simonb {
   2244   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
   2245   1.1    simonb 	struct wpi_tx_ring *ring = &sc->cmdq;
   2246   1.1    simonb 	struct wpi_tx_desc *desc;
   2247   1.1    simonb 	struct wpi_tx_data *data;
   2248   1.1    simonb 	struct wpi_tx_cmd *cmd;
   2249   1.1    simonb 	struct wpi_scan_hdr *hdr;
   2250   1.1    simonb 	struct wpi_scan_chan *chan;
   2251   1.1    simonb 	struct ieee80211_frame *wh;
   2252   1.1    simonb 	struct ieee80211_rateset *rs;
   2253   1.1    simonb 	struct ieee80211_channel *c;
   2254   1.1    simonb 	enum ieee80211_phymode mode;
   2255   1.1    simonb 	uint8_t *frm;
   2256   1.1    simonb 	int nrates, pktlen, error;
   2257   1.1    simonb 
   2258   1.1    simonb 	desc = &ring->desc[ring->cur];
   2259   1.1    simonb 	data = &ring->data[ring->cur];
   2260   1.1    simonb 
   2261   1.1    simonb 	MGETHDR(data->m, M_DONTWAIT, MT_DATA);
   2262   1.1    simonb 	if (data->m == NULL) {
   2263   1.1    simonb 		aprint_error("%s: could not allocate mbuf for scan command\n",
   2264   1.1    simonb 			sc->sc_dev.dv_xname);
   2265   1.1    simonb 		return ENOMEM;
   2266   1.1    simonb 	}
   2267   1.1    simonb 
   2268   1.1    simonb 	MCLGET(data->m, M_DONTWAIT);
   2269   1.1    simonb 	if (!(data->m->m_flags & M_EXT)) {
   2270   1.1    simonb 		m_freem(data->m);
   2271   1.1    simonb 		data->m = NULL;
   2272   1.1    simonb 		aprint_error("%s: could not allocate mbuf for scan command\n",
   2273   1.1    simonb 			sc->sc_dev.dv_xname);
   2274   1.1    simonb 		return ENOMEM;
   2275   1.1    simonb 	}
   2276   1.1    simonb 
   2277   1.1    simonb 	cmd = mtod(data->m, struct wpi_tx_cmd *);
   2278   1.1    simonb 	cmd->code = WPI_CMD_SCAN;
   2279   1.1    simonb 	cmd->flags = 0;
   2280   1.1    simonb 	cmd->qid = ring->qid;
   2281   1.1    simonb 	cmd->idx = ring->cur;
   2282   1.1    simonb 
   2283   1.1    simonb 	hdr = (struct wpi_scan_hdr *)cmd->data;
   2284   1.1    simonb 	memset(hdr, 0, sizeof (struct wpi_scan_hdr));
   2285   1.1    simonb 	hdr->first = 1;
   2286   1.1    simonb 	/*
   2287   1.1    simonb 	 * Move to the next channel if no packets are received within 5 msecs
   2288   1.1    simonb 	 * after sending the probe request (this helps to reduce the duration
   2289   1.1    simonb 	 * of active scans).
   2290   1.1    simonb 	 */
   2291   1.1    simonb 	hdr->quiet = htole16(5);        /* timeout in milliseconds */
   2292   1.1    simonb 	hdr->threshold = htole16(1);    /* min # of packets */
   2293   1.1    simonb 
   2294   1.1    simonb 	if (flags & IEEE80211_CHAN_A) {
   2295   1.1    simonb 		hdr->band = htole16(WPI_SCAN_5GHZ);
   2296   1.1    simonb 		/* send probe requests at 6Mbps */
   2297   1.1    simonb 		hdr->rate = wpi_plcp_signal(12);
   2298   1.1    simonb 	} else {
   2299   1.1    simonb 		hdr->flags = htole32(WPI_CONFIG_24GHZ | WPI_CONFIG_AUTO);
   2300   1.1    simonb 		/* send probe requests at 1Mbps */
   2301   1.1    simonb 		hdr->rate = wpi_plcp_signal(2);
   2302   1.1    simonb 	}
   2303   1.1    simonb 	hdr->id = WPI_ID_BROADCAST;
   2304   1.1    simonb 	hdr->mask = htole32(0xffffffff);
   2305   1.1    simonb 	hdr->magic1 = htole32(1 << 13);
   2306   1.1    simonb 
   2307   1.1    simonb 	hdr->esslen = ic->ic_des_esslen;
   2308   1.1    simonb 	memcpy(hdr->essid, ic->ic_des_essid, ic->ic_des_esslen);
   2309   1.1    simonb 
   2310   1.1    simonb 	/*
   2311   1.1    simonb 	 * Build a probe request frame.  Most of the following code is a
   2312   1.1    simonb 	 * copy & paste of what is done in net80211.
   2313   1.1    simonb 	 */
   2314   1.1    simonb 	wh = (struct ieee80211_frame *)(hdr + 1);
   2315   1.1    simonb 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
   2316   1.1    simonb 		IEEE80211_FC0_SUBTYPE_PROBE_REQ;
   2317   1.1    simonb 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
   2318   1.1    simonb 	IEEE80211_ADDR_COPY(wh->i_addr1, etherbroadcastaddr);
   2319   1.1    simonb 	IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
   2320   1.1    simonb 	IEEE80211_ADDR_COPY(wh->i_addr3, etherbroadcastaddr);
   2321   1.1    simonb 	*(u_int16_t *)&wh->i_dur[0] = 0;	/* filled by h/w */
   2322   1.1    simonb 	*(u_int16_t *)&wh->i_seq[0] = 0;	/* filled by h/w */
   2323   1.1    simonb 
   2324   1.1    simonb 	frm = (uint8_t *)(wh + 1);
   2325   1.1    simonb 
   2326   1.1    simonb 	/* add essid IE */
   2327   1.1    simonb 	*frm++ = IEEE80211_ELEMID_SSID;
   2328   1.1    simonb 	*frm++ = ic->ic_des_esslen;
   2329   1.1    simonb 	memcpy(frm, ic->ic_des_essid, ic->ic_des_esslen);
   2330   1.1    simonb 	frm += ic->ic_des_esslen;
   2331   1.1    simonb 
   2332   1.1    simonb 	mode = ieee80211_chan2mode(ic, ic->ic_ibss_chan);
   2333   1.1    simonb 	rs = &ic->ic_sup_rates[mode];
   2334   1.1    simonb 
   2335   1.1    simonb 	/* add supported rates IE */
   2336   1.1    simonb 	*frm++ = IEEE80211_ELEMID_RATES;
   2337   1.1    simonb 	nrates = rs->rs_nrates;
   2338   1.1    simonb 	if (nrates > IEEE80211_RATE_SIZE)
   2339   1.1    simonb 		nrates = IEEE80211_RATE_SIZE;
   2340   1.1    simonb 	*frm++ = nrates;
   2341   1.1    simonb 	memcpy(frm, rs->rs_rates, nrates);
   2342   1.1    simonb 	frm += nrates;
   2343   1.1    simonb 
   2344   1.1    simonb 	/* add supported xrates IE */
   2345   1.1    simonb 	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
   2346   1.1    simonb 		nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
   2347   1.1    simonb 		*frm++ = IEEE80211_ELEMID_XRATES;
   2348   1.1    simonb 		*frm++ = nrates;
   2349   1.1    simonb 		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
   2350   1.1    simonb 		frm += nrates;
   2351   1.1    simonb 	}
   2352   1.1    simonb 
   2353   1.1    simonb 	/* setup length of probe request */
   2354   1.1    simonb 	hdr->pbrlen = htole16(frm - (uint8_t *)wh);
   2355   1.1    simonb 
   2356   1.1    simonb 	chan = (struct wpi_scan_chan *)frm;
   2357   1.1    simonb 	for (c  = &ic->ic_channels[1];
   2358   1.1    simonb 	     c <= &ic->ic_channels[IEEE80211_CHAN_MAX]; c++) {
   2359   1.1    simonb 		if ((c->ic_flags & flags) != flags)
   2360   1.1    simonb 			continue;
   2361   1.1    simonb 
   2362   1.1    simonb 		chan->chan = ieee80211_chan2ieee(ic, c);
   2363   1.1    simonb 		chan->flags = (c->ic_flags & IEEE80211_CHAN_PASSIVE) ?
   2364   1.1    simonb 			0 : WPI_CHAN_ACTIVE;
   2365   1.1    simonb 		chan->magic = htole16(0x62ab);
   2366   1.1    simonb 		if (IEEE80211_IS_CHAN_5GHZ(c)) {
   2367   1.1    simonb 			chan->active = htole16(10);
   2368   1.1    simonb 			chan->passive = htole16(110);
   2369   1.1    simonb 		} else {
   2370   1.1    simonb 			chan->active = htole16(20);
   2371   1.1    simonb 			chan->passive = htole16(120);
   2372   1.1    simonb 		}
   2373   1.1    simonb 		hdr->nchan++;
   2374   1.1    simonb 		chan++;
   2375   1.1    simonb 
   2376   1.1    simonb 		frm += sizeof (struct wpi_scan_chan);
   2377   1.1    simonb 	}
   2378   1.1    simonb 
   2379   1.1    simonb 	hdr->len = hdr->nchan * sizeof (struct wpi_scan_chan);
   2380   1.1    simonb 	pktlen = frm - mtod(data->m, uint8_t *);
   2381   1.1    simonb 
   2382   1.1    simonb 	error = bus_dmamap_load(sc->sc_dmat, data->map, cmd, pktlen,
   2383   1.1    simonb 		NULL, BUS_DMA_NOWAIT);
   2384   1.1    simonb 	if (error) {
   2385   1.1    simonb 		aprint_error("%s: could not map scan command\n",
   2386   1.1    simonb 			sc->sc_dev.dv_xname);
   2387   1.1    simonb 		m_freem(data->m);
   2388   1.1    simonb 		data->m = NULL;
   2389   1.1    simonb 		return error;
   2390   1.1    simonb 	}
   2391   1.1    simonb 
   2392   1.1    simonb 	desc->flags = htole32(WPI_PAD32(pktlen) << 28 | 1 << 24);
   2393   1.1    simonb 	desc->segs[0].addr = htole32(data->map->dm_segs[0].ds_addr);
   2394   1.1    simonb 	desc->segs[0].len  = htole32(data->map->dm_segs[0].ds_len);
   2395   1.1    simonb 
   2396   1.1    simonb 	/* kick cmd ring */
   2397   1.1    simonb 	ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
   2398   1.1    simonb 	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
   2399   1.1    simonb 
   2400   1.1    simonb 	return 0;	/* will be notified async. of failure/success */
   2401   1.1    simonb }
   2402   1.1    simonb 
   2403   1.1    simonb static int
   2404   1.1    simonb wpi_config(struct wpi_softc *sc)
   2405   1.1    simonb {
   2406   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
   2407   1.1    simonb 	struct ifnet *ifp = ic->ic_ifp;
   2408   1.1    simonb 	struct wpi_txpower txpower;
   2409   1.1    simonb 	struct wpi_power power;
   2410   1.1    simonb 	struct wpi_bluetooth bluetooth;
   2411   1.5     joerg 	struct wpi_node_info node;
   2412   1.1    simonb 	int error;
   2413   1.1    simonb 
   2414   1.1    simonb 	/* set Tx power for 2.4GHz channels (values read from EEPROM) */
   2415   1.1    simonb 	memset(&txpower, 0, sizeof txpower);
   2416   1.1    simonb 	memcpy(txpower.pwr1, sc->pwr1, 14 * sizeof (uint16_t));
   2417   1.1    simonb 	memcpy(txpower.pwr2, sc->pwr2, 14 * sizeof (uint16_t));
   2418   1.1    simonb 	error = wpi_cmd(sc, WPI_CMD_TXPOWER, &txpower, sizeof txpower, 0);
   2419   1.1    simonb 	if (error != 0) {
   2420   1.1    simonb 		aprint_error("%s: could not set txpower\n",
   2421   1.1    simonb 			sc->sc_dev.dv_xname);
   2422   1.1    simonb 		return error;
   2423   1.1    simonb 	}
   2424   1.1    simonb 
   2425   1.1    simonb 	/* set power mode */
   2426   1.1    simonb 	memset(&power, 0, sizeof power);
   2427   1.1    simonb 	power.flags = htole32(0x8);	/* XXX */
   2428   1.1    simonb 	error = wpi_cmd(sc, WPI_CMD_SET_POWER_MODE, &power, sizeof power, 0);
   2429   1.1    simonb 	if (error != 0) {
   2430   1.1    simonb 		aprint_error("%s: could not set power mode\n",
   2431   1.1    simonb 			sc->sc_dev.dv_xname);
   2432   1.1    simonb 		return error;
   2433   1.1    simonb 	}
   2434   1.1    simonb 
   2435   1.1    simonb 	/* configure bluetooth coexistence */
   2436   1.1    simonb 	memset(&bluetooth, 0, sizeof bluetooth);
   2437   1.1    simonb 	bluetooth.flags = 3;
   2438   1.1    simonb 	bluetooth.lead = 0xaa;
   2439   1.1    simonb 	bluetooth.kill = 1;
   2440   1.1    simonb 	error = wpi_cmd(sc, WPI_CMD_BLUETOOTH, &bluetooth, sizeof bluetooth,
   2441   1.1    simonb 		0);
   2442   1.1    simonb 	if (error != 0) {
   2443   1.1    simonb 		aprint_error(
   2444   1.1    simonb 			"%s: could not configure bluetooth coexistence\n",
   2445   1.1    simonb 			sc->sc_dev.dv_xname);
   2446   1.1    simonb 		return error;
   2447   1.1    simonb 	}
   2448   1.1    simonb 
   2449   1.1    simonb 	/* configure adapter */
   2450   1.1    simonb 	memset(&sc->config, 0, sizeof (struct wpi_config));
   2451   1.1    simonb 	IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
   2452   1.1    simonb 	IEEE80211_ADDR_COPY(sc->config.myaddr, ic->ic_myaddr);
   2453   1.1    simonb 	/*set default channel*/
   2454   1.1    simonb 	sc->config.chan = ieee80211_chan2ieee(ic, ic->ic_ibss_chan);
   2455   1.1    simonb 	sc->config.flags = htole32(WPI_CONFIG_TSF);
   2456   1.1    simonb 	if (IEEE80211_IS_CHAN_2GHZ(ic->ic_ibss_chan)) {
   2457   1.1    simonb 		sc->config.flags |= htole32(WPI_CONFIG_AUTO |
   2458   1.1    simonb 		    WPI_CONFIG_24GHZ);
   2459   1.1    simonb 	}
   2460   1.1    simonb 	sc->config.filter = 0;
   2461   1.1    simonb 	switch (ic->ic_opmode) {
   2462   1.1    simonb 	case IEEE80211_M_STA:
   2463   1.1    simonb 		sc->config.mode = WPI_MODE_STA;
   2464   1.1    simonb 		sc->config.filter |= htole32(WPI_FILTER_MULTICAST);
   2465   1.1    simonb 		break;
   2466   1.1    simonb 	case IEEE80211_M_IBSS:
   2467   1.1    simonb 	case IEEE80211_M_AHDEMO:
   2468   1.1    simonb 		sc->config.mode = WPI_MODE_IBSS;
   2469   1.1    simonb 		break;
   2470   1.1    simonb 	case IEEE80211_M_HOSTAP:
   2471   1.1    simonb 		sc->config.mode = WPI_MODE_HOSTAP;
   2472   1.1    simonb 		break;
   2473   1.1    simonb 	case IEEE80211_M_MONITOR:
   2474   1.1    simonb 		sc->config.mode = WPI_MODE_MONITOR;
   2475   1.1    simonb 		sc->config.filter |= htole32(WPI_FILTER_MULTICAST |
   2476   1.1    simonb 			WPI_FILTER_CTL | WPI_FILTER_PROMISC);
   2477   1.1    simonb 		break;
   2478   1.1    simonb 	}
   2479   1.1    simonb 	sc->config.cck_mask  = 0x0f;	/* not yet negotiated */
   2480   1.1    simonb 	sc->config.ofdm_mask = 0xff;	/* not yet negotiated */
   2481   1.1    simonb 	error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config,
   2482   1.1    simonb 		sizeof (struct wpi_config), 0);
   2483   1.1    simonb 	if (error != 0) {
   2484   1.1    simonb 		aprint_error("%s: configure command failed\n",
   2485   1.1    simonb 			sc->sc_dev.dv_xname);
   2486   1.1    simonb 		return error;
   2487   1.1    simonb 	}
   2488   1.1    simonb 
   2489   1.1    simonb 	/* add broadcast node */
   2490   1.1    simonb 	memset(&node, 0, sizeof node);
   2491   1.1    simonb 	IEEE80211_ADDR_COPY(node.bssid, etherbroadcastaddr);
   2492   1.1    simonb 	node.id = WPI_ID_BROADCAST;
   2493   1.1    simonb 	node.rate = wpi_plcp_signal(2);
   2494   1.1    simonb 	error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 0);
   2495   1.1    simonb 	if (error != 0) {
   2496   1.1    simonb 		aprint_error("%s: could not add broadcast node\n",
   2497   1.1    simonb 			sc->sc_dev.dv_xname);
   2498   1.1    simonb 		return error;
   2499   1.1    simonb 	}
   2500   1.1    simonb 
   2501   1.1    simonb 	return 0;
   2502   1.1    simonb }
   2503   1.1    simonb 
   2504   1.1    simonb static void
   2505   1.1    simonb wpi_stop_master(struct wpi_softc *sc)
   2506   1.1    simonb {
   2507   1.1    simonb 	uint32_t tmp;
   2508   1.1    simonb 	int ntries;
   2509   1.1    simonb 
   2510   1.1    simonb 	tmp = WPI_READ(sc, WPI_RESET);
   2511   1.1    simonb 	WPI_WRITE(sc, WPI_RESET, tmp | WPI_STOP_MASTER);
   2512   1.1    simonb 
   2513   1.1    simonb 	tmp = WPI_READ(sc, WPI_GPIO_CTL);
   2514   1.1    simonb 	if ((tmp & WPI_GPIO_PWR_STATUS) == WPI_GPIO_PWR_SLEEP)
   2515   1.1    simonb 		return;	/* already asleep */
   2516   1.1    simonb 
   2517   1.1    simonb 	for (ntries = 0; ntries < 100; ntries++) {
   2518   1.1    simonb 		if (WPI_READ(sc, WPI_RESET) & WPI_MASTER_DISABLED)
   2519   1.1    simonb 			break;
   2520   1.1    simonb 		DELAY(10);
   2521   1.1    simonb 	}
   2522   1.1    simonb 	if (ntries == 100) {
   2523   1.1    simonb 		aprint_error("%s: timeout waiting for master\n",
   2524   1.1    simonb 			sc->sc_dev.dv_xname);
   2525   1.1    simonb 	}
   2526   1.1    simonb }
   2527   1.1    simonb 
   2528   1.1    simonb static int
   2529   1.1    simonb wpi_power_up(struct wpi_softc *sc)
   2530   1.1    simonb {
   2531   1.1    simonb 	uint32_t tmp;
   2532   1.1    simonb 	int ntries;
   2533   1.1    simonb 
   2534   1.1    simonb 	wpi_mem_lock(sc);
   2535   1.1    simonb 	tmp = wpi_mem_read(sc, WPI_MEM_POWER);
   2536   1.1    simonb 	wpi_mem_write(sc, WPI_MEM_POWER, tmp & ~0x03000000);
   2537   1.1    simonb 	wpi_mem_unlock(sc);
   2538   1.1    simonb 
   2539   1.1    simonb 	for (ntries = 0; ntries < 5000; ntries++) {
   2540   1.1    simonb 		if (WPI_READ(sc, WPI_GPIO_STATUS) & WPI_POWERED)
   2541   1.1    simonb 			break;
   2542   1.1    simonb 		DELAY(10);
   2543   1.1    simonb 	}
   2544   1.1    simonb 	if (ntries == 5000) {
   2545   1.1    simonb 		aprint_error("%s: timeout waiting for NIC to power up\n",
   2546   1.1    simonb 			sc->sc_dev.dv_xname);
   2547   1.1    simonb 		return ETIMEDOUT;
   2548   1.1    simonb 	}
   2549   1.1    simonb 	return 0;
   2550   1.1    simonb }
   2551   1.1    simonb 
   2552   1.1    simonb static int
   2553   1.1    simonb wpi_reset(struct wpi_softc *sc)
   2554   1.1    simonb {
   2555   1.1    simonb 	uint32_t tmp;
   2556   1.1    simonb 	int ntries;
   2557   1.1    simonb 
   2558   1.1    simonb 	/* clear any pending interrupts */
   2559   1.1    simonb 	WPI_WRITE(sc, WPI_INTR, 0xffffffff);
   2560   1.1    simonb 
   2561   1.1    simonb 	tmp = WPI_READ(sc, WPI_PLL_CTL);
   2562   1.1    simonb 	WPI_WRITE(sc, WPI_PLL_CTL, tmp | WPI_PLL_INIT);
   2563   1.1    simonb 
   2564   1.1    simonb 	tmp = WPI_READ(sc, WPI_CHICKEN);
   2565   1.1    simonb 	WPI_WRITE(sc, WPI_CHICKEN, tmp | WPI_CHICKEN_RXNOLOS);
   2566   1.1    simonb 
   2567   1.1    simonb 	tmp = WPI_READ(sc, WPI_GPIO_CTL);
   2568   1.1    simonb 	WPI_WRITE(sc, WPI_GPIO_CTL, tmp | WPI_GPIO_INIT);
   2569   1.1    simonb 
   2570   1.1    simonb 	/* wait for clock stabilization */
   2571   1.1    simonb 	for (ntries = 0; ntries < 1000; ntries++) {
   2572   1.1    simonb 		if (WPI_READ(sc, WPI_GPIO_CTL) & WPI_GPIO_CLOCK)
   2573   1.1    simonb 			break;
   2574   1.1    simonb 		DELAY(10);
   2575   1.1    simonb 	}
   2576   1.1    simonb 	if (ntries == 1000) {
   2577   1.1    simonb 		aprint_error("%s: timeout waiting for clock stabilization\n",
   2578   1.1    simonb 			sc->sc_dev.dv_xname);
   2579   1.1    simonb 		return ETIMEDOUT;
   2580   1.1    simonb 	}
   2581   1.1    simonb 
   2582   1.1    simonb 	/* initialize EEPROM */
   2583   1.1    simonb 	tmp = WPI_READ(sc, WPI_EEPROM_STATUS);
   2584   1.1    simonb 	if ((tmp & WPI_EEPROM_VERSION) == 0) {
   2585   1.1    simonb 		aprint_error("%s: EEPROM not found\n", sc->sc_dev.dv_xname);
   2586   1.1    simonb 		return EIO;
   2587   1.1    simonb 	}
   2588   1.1    simonb 	WPI_WRITE(sc, WPI_EEPROM_STATUS, tmp & ~WPI_EEPROM_LOCKED);
   2589   1.1    simonb 
   2590   1.1    simonb 	return 0;
   2591   1.1    simonb }
   2592   1.1    simonb 
   2593   1.1    simonb static void
   2594   1.1    simonb wpi_hw_config(struct wpi_softc *sc)
   2595   1.1    simonb {
   2596   1.1    simonb 	uint16_t val;
   2597   1.1    simonb 	uint32_t rev, hw;
   2598   1.1    simonb 
   2599   1.1    simonb 	/* voodoo from the Linux "driver".. */
   2600   1.1    simonb 	hw = WPI_READ(sc, WPI_HWCONFIG);
   2601   1.1    simonb 
   2602   1.1    simonb 	rev = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_CLASS_REG);
   2603   1.1    simonb 	rev = PCI_REVISION(rev);
   2604   1.1    simonb 	if ((rev & 0xc0) == 0x40)
   2605   1.1    simonb 		hw |= WPI_HW_ALM_MB;
   2606   1.1    simonb 	else if (!(rev & 0x80))
   2607   1.1    simonb 		hw |= WPI_HW_ALM_MM;
   2608   1.1    simonb 
   2609   1.1    simonb 	val = wpi_read_prom_word(sc, WPI_EEPROM_CAPABILITIES);
   2610   1.1    simonb 	if ((val & 0xff) == 0x80)
   2611   1.1    simonb 		hw |= WPI_HW_SKU_MRC;
   2612   1.1    simonb 
   2613   1.1    simonb 	val = wpi_read_prom_word(sc, WPI_EEPROM_REVISION);
   2614   1.1    simonb 	hw &= ~WPI_HW_REV_D;
   2615   1.1    simonb 	if ((val & 0xf0) == 0xd0)
   2616   1.1    simonb 		hw |= WPI_HW_REV_D;
   2617   1.1    simonb 
   2618   1.1    simonb 	val = wpi_read_prom_word(sc, WPI_EEPROM_TYPE);
   2619   1.1    simonb 	if ((val & 0xff) > 1)
   2620   1.1    simonb 		hw |= WPI_HW_TYPE_B;
   2621   1.1    simonb 
   2622   1.1    simonb 	DPRINTF(("setting h/w config %x\n", hw));
   2623   1.1    simonb 	WPI_WRITE(sc, WPI_HWCONFIG, hw);
   2624   1.1    simonb }
   2625   1.1    simonb 
   2626   1.1    simonb static int
   2627   1.1    simonb wpi_init(struct ifnet *ifp)
   2628   1.1    simonb {
   2629   1.1    simonb 	struct wpi_softc *sc = ifp->if_softc;
   2630   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
   2631   1.1    simonb 	struct wpi_firmware_hdr hdr;
   2632   1.1    simonb 	const char *boot, *text, *data;
   2633   1.1    simonb 	firmware_handle_t fw;
   2634   1.1    simonb 	u_char *dfw;
   2635   1.1    simonb 	off_t size;
   2636   1.1    simonb 	size_t wsize;
   2637   1.1    simonb 	uint32_t tmp;
   2638   1.1    simonb 	int qid, ntries, error;
   2639   1.1    simonb 
   2640   1.1    simonb 	(void)wpi_reset(sc);
   2641   1.1    simonb 
   2642   1.1    simonb 	wpi_mem_lock(sc);
   2643   1.1    simonb 	wpi_mem_write(sc, WPI_MEM_CLOCK1, 0xa00);
   2644   1.1    simonb 	DELAY(20);
   2645   1.1    simonb 	tmp = wpi_mem_read(sc, WPI_MEM_PCIDEV);
   2646   1.1    simonb 	wpi_mem_write(sc, WPI_MEM_PCIDEV, tmp | 0x800);
   2647   1.1    simonb 	wpi_mem_unlock(sc);
   2648   1.1    simonb 
   2649   1.1    simonb 	(void)wpi_power_up(sc);
   2650   1.1    simonb 	wpi_hw_config(sc);
   2651   1.1    simonb 
   2652   1.1    simonb 	/* init Rx ring */
   2653   1.1    simonb 	wpi_mem_lock(sc);
   2654   1.1    simonb 	WPI_WRITE(sc, WPI_RX_BASE, sc->rxq.desc_dma.paddr);
   2655   1.1    simonb 	WPI_WRITE(sc, WPI_RX_RIDX_PTR, sc->shared_dma.paddr +
   2656   1.1    simonb 	    offsetof(struct wpi_shared, next));
   2657   1.1    simonb 	WPI_WRITE(sc, WPI_RX_WIDX, (WPI_RX_RING_COUNT - 1) & ~7);
   2658   1.1    simonb 	WPI_WRITE(sc, WPI_RX_CONFIG, 0xa9601010);
   2659   1.1    simonb 	wpi_mem_unlock(sc);
   2660   1.1    simonb 
   2661   1.1    simonb 	/* init Tx rings */
   2662   1.1    simonb 	wpi_mem_lock(sc);
   2663   1.1    simonb 	wpi_mem_write(sc, WPI_MEM_MODE, 2); /* bypass mode */
   2664   1.1    simonb 	wpi_mem_write(sc, WPI_MEM_RA, 1);   /* enable RA0 */
   2665   1.1    simonb 	wpi_mem_write(sc, WPI_MEM_TXCFG, 0x3f); /* enable all 6 Tx rings */
   2666   1.1    simonb 	wpi_mem_write(sc, WPI_MEM_BYPASS1, 0x10000);
   2667   1.1    simonb 	wpi_mem_write(sc, WPI_MEM_BYPASS2, 0x30002);
   2668   1.1    simonb 	wpi_mem_write(sc, WPI_MEM_MAGIC4, 4);
   2669   1.1    simonb 	wpi_mem_write(sc, WPI_MEM_MAGIC5, 5);
   2670   1.1    simonb 
   2671   1.1    simonb 	WPI_WRITE(sc, WPI_TX_BASE_PTR, sc->shared_dma.paddr);
   2672   1.1    simonb 	WPI_WRITE(sc, WPI_MSG_CONFIG, 0xffff05a5);
   2673   1.1    simonb 
   2674   1.1    simonb 	for (qid = 0; qid < 6; qid++) {
   2675   1.1    simonb 		WPI_WRITE(sc, WPI_TX_CTL(qid), 0);
   2676   1.1    simonb 		WPI_WRITE(sc, WPI_TX_BASE(qid), 0);
   2677   1.1    simonb 		WPI_WRITE(sc, WPI_TX_CONFIG(qid), 0x80200008);
   2678   1.1    simonb 	}
   2679   1.1    simonb 	wpi_mem_unlock(sc);
   2680   1.1    simonb 
   2681   1.1    simonb 	/* clear "radio off" and "disable command" bits (reversed logic) */
   2682   1.1    simonb 	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
   2683   1.1    simonb 	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_DISABLE_CMD);
   2684   1.1    simonb 
   2685   1.1    simonb 	/* clear any pending interrupts */
   2686   1.1    simonb 	WPI_WRITE(sc, WPI_INTR, 0xffffffff);
   2687   1.1    simonb 	/* enable interrupts */
   2688   1.1    simonb 	WPI_WRITE(sc, WPI_MASK, WPI_INTR_MASK);
   2689   1.1    simonb 
   2690   1.1    simonb 	if ((error = firmware_open("if_wpi", "ipw3945.ucode", &fw)) != 0) {
   2691   1.1    simonb 		aprint_error("%s: could not read firmware file\n",
   2692   1.1    simonb 			sc->sc_dev.dv_xname);
   2693   1.1    simonb 		goto fail1;
   2694   1.1    simonb 	}
   2695   1.1    simonb 
   2696   1.1    simonb 	size = firmware_get_size(fw);
   2697   1.1    simonb 
   2698   1.1    simonb 	if (size < sizeof (struct wpi_firmware_hdr)) {
   2699   1.1    simonb 		aprint_error("%s: firmware file too short\n",
   2700   1.1    simonb 			sc->sc_dev.dv_xname);
   2701   1.1    simonb 		error = EINVAL;
   2702   1.1    simonb 		goto fail2;
   2703   1.1    simonb 	}
   2704   1.1    simonb 
   2705   1.1    simonb 	if ((error = firmware_read(fw, 0, &hdr,
   2706   1.1    simonb 		sizeof (struct wpi_firmware_hdr))) != 0) {
   2707   1.1    simonb 		aprint_error("%s: can't get firmware header\n",
   2708   1.1    simonb 			sc->sc_dev.dv_xname);
   2709   1.1    simonb 		goto fail2;
   2710   1.1    simonb 	}
   2711   1.1    simonb 
   2712   1.1    simonb 	wsize = sizeof (struct wpi_firmware_hdr) + le32toh(hdr.textsz) +
   2713   1.1    simonb 		le32toh(hdr.datasz) + le32toh(hdr.bootsz);
   2714   1.1    simonb 
   2715   1.1    simonb 	if (size < wsize) {
   2716  1.11     pooka 		aprint_error("%s: fw file too short: should be %zu bytes\n",
   2717   1.1    simonb 			sc->sc_dev.dv_xname, wsize);
   2718   1.1    simonb 		error = EINVAL;
   2719   1.1    simonb 		goto fail2;
   2720   1.1    simonb 	}
   2721   1.1    simonb 
   2722   1.1    simonb 	dfw = firmware_malloc(size);
   2723   1.1    simonb 	if (dfw == NULL) {
   2724   1.1    simonb 		aprint_error("%s: not enough memory to stock firmware\n",
   2725   1.1    simonb 			sc->sc_dev.dv_xname);
   2726   1.1    simonb 		error = ENOMEM;
   2727   1.1    simonb 		goto fail2;
   2728   1.1    simonb 	}
   2729   1.1    simonb 
   2730   1.1    simonb 	if ((error = firmware_read(fw, 0, dfw, size)) != 0) {
   2731   1.1    simonb 		aprint_error("%s: can't get firmware\n",
   2732   1.1    simonb 			sc->sc_dev.dv_xname);
   2733   1.1    simonb 		goto fail2;
   2734   1.1    simonb 	}
   2735   1.1    simonb 
   2736   1.1    simonb 	/* firmware image layout: |HDR|<--TEXT-->|<--DATA-->|<--BOOT-->| */
   2737   1.1    simonb 	text = dfw + sizeof (struct wpi_firmware_hdr);
   2738   1.1    simonb 	data = text + le32toh(hdr.textsz);
   2739   1.1    simonb 	boot = data + le32toh(hdr.datasz);
   2740   1.1    simonb 
   2741   1.1    simonb 	/* load firmware boot code into NIC */
   2742   1.1    simonb 	error = wpi_load_microcode(sc, boot, le32toh(hdr.bootsz));
   2743   1.1    simonb 	if (error != 0) {
   2744   1.1    simonb 		aprint_error("%s: could not load microcode\n", sc->sc_dev.dv_xname);
   2745   1.1    simonb 		goto fail3;
   2746   1.1    simonb 	}
   2747   1.1    simonb 
   2748   1.1    simonb 	/* load firmware .text segment into NIC */
   2749   1.1    simonb 	error = wpi_load_firmware(sc, WPI_FW_TEXT, text, le32toh(hdr.textsz));
   2750   1.1    simonb 	if (error != 0) {
   2751   1.1    simonb 		aprint_error("%s: could not load firmware\n",
   2752   1.1    simonb 			sc->sc_dev.dv_xname);
   2753   1.1    simonb 		goto fail3;
   2754   1.1    simonb 	}
   2755   1.1    simonb 
   2756   1.1    simonb 	/* load firmware .data segment into NIC */
   2757   1.1    simonb 	error = wpi_load_firmware(sc, WPI_FW_DATA, data, le32toh(hdr.datasz));
   2758   1.1    simonb 	if (error != 0) {
   2759   1.1    simonb 		aprint_error("%s: could not load firmware\n",
   2760   1.1    simonb 			sc->sc_dev.dv_xname);
   2761   1.1    simonb 		goto fail3;
   2762   1.1    simonb 	}
   2763   1.1    simonb 
   2764   1.1    simonb 	firmware_free(dfw, 0);
   2765   1.1    simonb 	firmware_close(fw);
   2766   1.1    simonb 
   2767   1.1    simonb 	/* now press "execute" ;-) */
   2768   1.1    simonb 	tmp = WPI_READ(sc, WPI_RESET);
   2769   1.1    simonb 	tmp &= ~(WPI_MASTER_DISABLED | WPI_STOP_MASTER | WPI_NEVO_RESET);
   2770   1.1    simonb 	WPI_WRITE(sc, WPI_RESET, tmp);
   2771   1.1    simonb 
   2772   1.1    simonb 	/* ..and wait at most one second for adapter to initialize */
   2773   1.1    simonb 	if ((error = tsleep(sc, PCATCH, "wpiinit", hz)) != 0) {
   2774   1.1    simonb 		/* this isn't what was supposed to happen.. */
   2775   1.1    simonb 		aprint_error("%s: timeout waiting for adapter to initialize\n",
   2776   1.1    simonb 			sc->sc_dev.dv_xname);
   2777   1.1    simonb 		goto fail1;
   2778   1.1    simonb 	}
   2779   1.1    simonb 
   2780   1.1    simonb 	/* wait for thermal sensors to calibrate */
   2781   1.1    simonb 	for (ntries = 0; ntries < 1000; ntries++) {
   2782   1.1    simonb 		if (WPI_READ(sc, WPI_TEMPERATURE) != 0)
   2783   1.1    simonb 			break;
   2784   1.1    simonb 		DELAY(10);
   2785   1.1    simonb 	}
   2786   1.1    simonb 	if (ntries == 1000) {
   2787   1.1    simonb 		aprint_error("%s: timeout waiting for thermal sensors calibration\n",
   2788   1.1    simonb 			sc->sc_dev.dv_xname);
   2789   1.1    simonb 		error = ETIMEDOUT;
   2790   1.1    simonb 		goto fail1;
   2791   1.1    simonb 	}
   2792   1.1    simonb 	DPRINTF(("temperature %d\n", (int)WPI_READ(sc, WPI_TEMPERATURE)));
   2793   1.1    simonb 
   2794   1.1    simonb 	if ((error = wpi_config(sc)) != 0) {
   2795   1.1    simonb 		aprint_error("%s: could not configure device\n",
   2796   1.1    simonb 			sc->sc_dev.dv_xname);
   2797   1.1    simonb 		goto fail1;
   2798   1.1    simonb 	}
   2799   1.1    simonb 
   2800   1.1    simonb 	ifp->if_flags &= ~IFF_OACTIVE;
   2801   1.1    simonb 	ifp->if_flags |= IFF_RUNNING;
   2802   1.1    simonb 
   2803   1.1    simonb 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
   2804   1.1    simonb 		if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
   2805   1.1    simonb 			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
   2806   1.1    simonb 	}
   2807   1.1    simonb 	else
   2808   1.1    simonb 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
   2809   1.1    simonb 
   2810   1.1    simonb 	return 0;
   2811   1.1    simonb 
   2812   1.1    simonb fail3:	firmware_free(dfw, 0);
   2813   1.1    simonb fail2:	firmware_close(fw);
   2814   1.1    simonb fail1:	wpi_stop(ifp, 1);
   2815   1.1    simonb 	return error;
   2816   1.1    simonb }
   2817   1.1    simonb 
   2818   1.1    simonb 
   2819   1.1    simonb static void
   2820   1.6  christos wpi_stop(struct ifnet *ifp, int disable)
   2821   1.1    simonb {
   2822   1.1    simonb 	struct wpi_softc *sc = ifp->if_softc;
   2823   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
   2824   1.1    simonb 	uint32_t tmp;
   2825   1.1    simonb 	int ac;
   2826   1.1    simonb 
   2827   1.1    simonb 	ifp->if_timer = sc->sc_tx_timer = 0;
   2828   1.1    simonb 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   2829   1.1    simonb 
   2830   1.1    simonb 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
   2831   1.1    simonb 
   2832   1.1    simonb 	/* disable interrupts */
   2833   1.1    simonb 	WPI_WRITE(sc, WPI_MASK, 0);
   2834   1.1    simonb 	WPI_WRITE(sc, WPI_INTR, WPI_INTR_MASK);
   2835   1.1    simonb 	WPI_WRITE(sc, WPI_INTR_STATUS, 0xff);
   2836   1.1    simonb 	WPI_WRITE(sc, WPI_INTR_STATUS, 0x00070000);
   2837   1.1    simonb 
   2838   1.1    simonb 	wpi_mem_lock(sc);
   2839   1.1    simonb 	wpi_mem_write(sc, WPI_MEM_MODE, 0);
   2840   1.1    simonb 	wpi_mem_unlock(sc);
   2841   1.1    simonb 
   2842   1.1    simonb 	/* reset all Tx rings */
   2843   1.1    simonb 	for (ac = 0; ac < 4; ac++)
   2844   1.1    simonb 		wpi_reset_tx_ring(sc, &sc->txq[ac]);
   2845   1.1    simonb 	wpi_reset_tx_ring(sc, &sc->cmdq);
   2846   1.1    simonb 	wpi_reset_tx_ring(sc, &sc->svcq);
   2847   1.1    simonb 
   2848   1.1    simonb 	/* reset Rx ring */
   2849   1.1    simonb 	wpi_reset_rx_ring(sc, &sc->rxq);
   2850   1.1    simonb 
   2851   1.1    simonb 	wpi_mem_lock(sc);
   2852   1.1    simonb 	wpi_mem_write(sc, WPI_MEM_CLOCK2, 0x200);
   2853   1.1    simonb 	wpi_mem_unlock(sc);
   2854   1.1    simonb 
   2855   1.1    simonb 	DELAY(5);
   2856   1.1    simonb 
   2857   1.1    simonb 	wpi_stop_master(sc);
   2858   1.1    simonb 
   2859   1.1    simonb 	tmp = WPI_READ(sc, WPI_RESET);
   2860   1.1    simonb 	WPI_WRITE(sc, WPI_RESET, tmp | WPI_SW_RESET);
   2861   1.1    simonb }
   2862   1.1    simonb 
   2863   1.1    simonb static void
   2864   1.5     joerg wpi_iter_func(void *arg, struct ieee80211_node *ni)
   2865   1.1    simonb {
   2866   1.5     joerg 	struct wpi_softc *sc = arg;
   2867   1.5     joerg 	struct wpi_node *wn = (struct wpi_node *)ni;
   2868   1.1    simonb 
   2869   1.5     joerg 	ieee80211_amrr_choose(&sc->amrr, ni, &wn->amn);
   2870   1.1    simonb }
   2871   1.1    simonb 
   2872   1.1    simonb static void
   2873   1.1    simonb wpi_amrr_timeout(void *arg)
   2874   1.1    simonb {
   2875   1.1    simonb 	struct wpi_softc *sc = arg;
   2876   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
   2877   1.7  degroote 	int s;
   2878   1.1    simonb 
   2879   1.7  degroote 	s = splnet();
   2880   1.1    simonb 	if (ic->ic_opmode == IEEE80211_M_STA)
   2881   1.5     joerg 		wpi_iter_func(sc, ic->ic_bss);
   2882   1.1    simonb 	else
   2883   1.5     joerg 		ieee80211_iterate_nodes(&ic->ic_sta, wpi_iter_func, sc);
   2884   1.7  degroote 	splx(s);
   2885   1.1    simonb 
   2886   1.7  degroote 	callout_reset(&sc->amrr_ch, hz/2, wpi_amrr_timeout, sc);
   2887   1.1    simonb }
   2888   1.1    simonb 
   2889   1.1    simonb static void
   2890   1.5     joerg wpi_newassoc(struct ieee80211_node *ni, int isnew)
   2891   1.1    simonb {
   2892   1.5     joerg 	struct wpi_softc *sc = ni->ni_ic->ic_ifp->if_softc;
   2893   1.5     joerg 	int i;
   2894   1.1    simonb 
   2895   1.5     joerg 	ieee80211_amrr_node_init(&sc->amrr, &((struct wpi_node *)ni)->amn);
   2896   1.5     joerg 	/* set rate to some reasonable initial value */
   2897   1.5     joerg 	for (i = ni->ni_rates.rs_nrates - 1;
   2898   1.5     joerg 	     i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72;
   2899   1.5     joerg 	     i--);
   2900   1.7  degroote 		ni->ni_txrate = i;
   2901   1.1    simonb }
   2902