Home | History | Annotate | Line # | Download | only in pci
      1  1.92   msaitoh /*	$NetBSD: if_wpi.c,v 1.92 2021/12/05 07:08:08 msaitoh Exp $	*/
      2   1.1    simonb 
      3   1.1    simonb /*-
      4  1.12  degroote  * Copyright (c) 2006, 2007
      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.92   msaitoh __KERNEL_RCSID(0, "$NetBSD: if_wpi.c,v 1.92 2021/12/05 07:08:08 msaitoh 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 
     28   1.1    simonb #include <sys/param.h>
     29   1.1    simonb #include <sys/sockio.h>
     30   1.1    simonb #include <sys/sysctl.h>
     31   1.1    simonb #include <sys/mbuf.h>
     32   1.1    simonb #include <sys/kernel.h>
     33   1.1    simonb #include <sys/socket.h>
     34   1.1    simonb #include <sys/systm.h>
     35   1.1    simonb #include <sys/malloc.h>
     36  1.39      cube #include <sys/mutex.h>
     37  1.41     joerg #include <sys/once.h>
     38   1.1    simonb #include <sys/conf.h>
     39   1.1    simonb #include <sys/kauth.h>
     40   1.7  degroote #include <sys/callout.h>
     41  1.48  uebayasi #include <sys/proc.h>
     42  1.70    bouyer #include <sys/kthread.h>
     43   1.1    simonb 
     44  1.25        ad #include <sys/bus.h>
     45   1.1    simonb #include <machine/endian.h>
     46  1.25        ad #include <sys/intr.h>
     47   1.1    simonb 
     48   1.1    simonb #include <dev/pci/pcireg.h>
     49   1.1    simonb #include <dev/pci/pcivar.h>
     50   1.1    simonb #include <dev/pci/pcidevs.h>
     51   1.1    simonb 
     52  1.70    bouyer #include <dev/sysmon/sysmonvar.h>
     53  1.70    bouyer 
     54   1.1    simonb #include <net/bpf.h>
     55   1.1    simonb #include <net/if.h>
     56   1.1    simonb #include <net/if_arp.h>
     57   1.1    simonb #include <net/if_dl.h>
     58   1.1    simonb #include <net/if_ether.h>
     59   1.1    simonb #include <net/if_media.h>
     60   1.1    simonb #include <net/if_types.h>
     61   1.1    simonb 
     62   1.1    simonb #include <netinet/in.h>
     63   1.1    simonb #include <netinet/in_systm.h>
     64   1.1    simonb #include <netinet/in_var.h>
     65   1.1    simonb #include <netinet/ip.h>
     66   1.1    simonb 
     67  1.61  jakllsch #include <net80211/ieee80211_var.h>
     68  1.61  jakllsch #include <net80211/ieee80211_amrr.h>
     69  1.61  jakllsch #include <net80211/ieee80211_radiotap.h>
     70  1.61  jakllsch 
     71   1.1    simonb #include <dev/firmload.h>
     72   1.1    simonb 
     73   1.1    simonb #include <dev/pci/if_wpireg.h>
     74   1.1    simonb #include <dev/pci/if_wpivar.h>
     75   1.1    simonb 
     76  1.54  riastrad static const char wpi_firmware_name[] = "iwlwifi-3945.ucode";
     77  1.41     joerg static once_t wpi_firmware_init;
     78  1.41     joerg static kmutex_t wpi_firmware_mutex;
     79  1.41     joerg static size_t wpi_firmware_users;
     80  1.41     joerg static uint8_t *wpi_firmware_image;
     81  1.41     joerg static size_t wpi_firmware_size;
     82  1.41     joerg 
     83  1.61  jakllsch static int	wpi_match(device_t, cfdata_t, void *);
     84  1.61  jakllsch static void	wpi_attach(device_t, device_t, void *);
     85  1.61  jakllsch static int	wpi_detach(device_t , int);
     86  1.61  jakllsch static int	wpi_dma_contig_alloc(bus_dma_tag_t, struct wpi_dma_info *,
     87  1.61  jakllsch 		    void **, bus_size_t, bus_size_t, int);
     88  1.61  jakllsch static void	wpi_dma_contig_free(struct wpi_dma_info *);
     89  1.61  jakllsch static int	wpi_alloc_shared(struct wpi_softc *);
     90  1.61  jakllsch static void	wpi_free_shared(struct wpi_softc *);
     91  1.61  jakllsch static int	wpi_alloc_fwmem(struct wpi_softc *);
     92  1.61  jakllsch static void	wpi_free_fwmem(struct wpi_softc *);
     93  1.61  jakllsch static struct	wpi_rbuf *wpi_alloc_rbuf(struct wpi_softc *);
     94  1.61  jakllsch static void	wpi_free_rbuf(struct mbuf *, void *, size_t, void *);
     95  1.61  jakllsch static int	wpi_alloc_rpool(struct wpi_softc *);
     96  1.61  jakllsch static void	wpi_free_rpool(struct wpi_softc *);
     97  1.61  jakllsch static int	wpi_alloc_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
     98  1.61  jakllsch static void	wpi_reset_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
     99  1.61  jakllsch static void	wpi_free_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
    100  1.61  jakllsch static int	wpi_alloc_tx_ring(struct wpi_softc *, struct wpi_tx_ring *,
    101  1.61  jakllsch 		    int, int);
    102  1.61  jakllsch static void	wpi_reset_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
    103  1.61  jakllsch static void	wpi_free_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
    104  1.61  jakllsch static struct	ieee80211_node * wpi_node_alloc(struct ieee80211_node_table *);
    105  1.61  jakllsch static void	wpi_newassoc(struct ieee80211_node *, int);
    106  1.61  jakllsch static int	wpi_media_change(struct ifnet *);
    107  1.61  jakllsch static int	wpi_newstate(struct ieee80211com *, enum ieee80211_state, int);
    108  1.61  jakllsch static void	wpi_mem_lock(struct wpi_softc *);
    109  1.61  jakllsch static void	wpi_mem_unlock(struct wpi_softc *);
    110  1.61  jakllsch static uint32_t	wpi_mem_read(struct wpi_softc *, uint16_t);
    111  1.61  jakllsch static void	wpi_mem_write(struct wpi_softc *, uint16_t, uint32_t);
    112  1.61  jakllsch static void	wpi_mem_write_region_4(struct wpi_softc *, uint16_t,
    113  1.61  jakllsch 		    const uint32_t *, int);
    114  1.61  jakllsch static int	wpi_read_prom_data(struct wpi_softc *, uint32_t, void *, int);
    115  1.61  jakllsch static int	wpi_load_microcode(struct wpi_softc *,  const uint8_t *, int);
    116  1.61  jakllsch static int	wpi_cache_firmware(struct wpi_softc *);
    117  1.61  jakllsch static void	wpi_release_firmware(void);
    118  1.61  jakllsch static int	wpi_load_firmware(struct wpi_softc *);
    119  1.61  jakllsch static void	wpi_calib_timeout(void *);
    120  1.61  jakllsch static void	wpi_iter_func(void *, struct ieee80211_node *);
    121  1.61  jakllsch static void	wpi_power_calibration(struct wpi_softc *, int);
    122  1.61  jakllsch static void	wpi_rx_intr(struct wpi_softc *, struct wpi_rx_desc *,
    123  1.61  jakllsch 		    struct wpi_rx_data *);
    124  1.61  jakllsch static void	wpi_tx_intr(struct wpi_softc *, struct wpi_rx_desc *);
    125  1.61  jakllsch static void	wpi_cmd_intr(struct wpi_softc *, struct wpi_rx_desc *);
    126  1.61  jakllsch static void	wpi_notif_intr(struct wpi_softc *);
    127  1.61  jakllsch static int	wpi_intr(void *);
    128  1.77    nonaka static void	wpi_softintr(void *);
    129  1.61  jakllsch static void	wpi_read_eeprom(struct wpi_softc *);
    130  1.61  jakllsch static void	wpi_read_eeprom_channels(struct wpi_softc *, int);
    131  1.61  jakllsch static void	wpi_read_eeprom_group(struct wpi_softc *, int);
    132  1.61  jakllsch static uint8_t	wpi_plcp_signal(int);
    133  1.61  jakllsch static int	wpi_tx_data(struct wpi_softc *, struct mbuf *,
    134  1.61  jakllsch 		    struct ieee80211_node *, int);
    135  1.61  jakllsch static void	wpi_start(struct ifnet *);
    136  1.61  jakllsch static void	wpi_watchdog(struct ifnet *);
    137  1.61  jakllsch static int	wpi_ioctl(struct ifnet *, u_long, void *);
    138  1.61  jakllsch static int	wpi_cmd(struct wpi_softc *, int, const void *, int, int);
    139  1.61  jakllsch static int	wpi_wme_update(struct ieee80211com *);
    140  1.61  jakllsch static int	wpi_mrr_setup(struct wpi_softc *);
    141  1.61  jakllsch static void	wpi_set_led(struct wpi_softc *, uint8_t, uint8_t, uint8_t);
    142  1.61  jakllsch static void	wpi_enable_tsf(struct wpi_softc *, struct ieee80211_node *);
    143  1.61  jakllsch static int	wpi_set_txpower(struct wpi_softc *,
    144  1.61  jakllsch 		    struct ieee80211_channel *, int);
    145  1.61  jakllsch static int	wpi_get_power_index(struct wpi_softc *,
    146  1.61  jakllsch 		    struct wpi_power_group *, struct ieee80211_channel *, int);
    147  1.61  jakllsch static int	wpi_setup_beacon(struct wpi_softc *, struct ieee80211_node *);
    148  1.61  jakllsch static int	wpi_auth(struct wpi_softc *);
    149  1.67  jmcneill static int	wpi_scan(struct wpi_softc *);
    150  1.61  jakllsch static int	wpi_config(struct wpi_softc *);
    151  1.61  jakllsch static void	wpi_stop_master(struct wpi_softc *);
    152  1.61  jakllsch static int	wpi_power_up(struct wpi_softc *);
    153  1.61  jakllsch static int	wpi_reset(struct wpi_softc *);
    154  1.61  jakllsch static void	wpi_hw_config(struct wpi_softc *);
    155  1.61  jakllsch static int	wpi_init(struct ifnet *);
    156  1.61  jakllsch static void	wpi_stop(struct ifnet *, int);
    157  1.61  jakllsch static bool	wpi_resume(device_t, const pmf_qual_t *);
    158  1.34  degroote static int	wpi_getrfkill(struct wpi_softc *);
    159  1.61  jakllsch static void	wpi_sysctlattach(struct wpi_softc *);
    160  1.70    bouyer static void	wpi_rsw_thread(void *);
    161  1.90  christos static void	wpi_rsw_suspend(struct wpi_softc *);
    162  1.90  christos static void	wpi_stop_intr(struct ifnet *, int);
    163  1.61  jakllsch 
    164  1.61  jakllsch #ifdef WPI_DEBUG
    165  1.61  jakllsch #define DPRINTF(x)	do { if (wpi_debug > 0) printf x; } while (0)
    166  1.61  jakllsch #define DPRINTFN(n, x)	do { if (wpi_debug >= (n)) printf x; } while (0)
    167  1.61  jakllsch int wpi_debug = 1;
    168  1.61  jakllsch #else
    169  1.61  jakllsch #define DPRINTF(x)
    170  1.61  jakllsch #define DPRINTFN(n, x)
    171  1.61  jakllsch #endif
    172   1.1    simonb 
    173  1.28  degroote CFATTACH_DECL_NEW(wpi, sizeof (struct wpi_softc), wpi_match, wpi_attach,
    174   1.1    simonb 	wpi_detach, NULL);
    175   1.1    simonb 
    176   1.1    simonb static int
    177  1.42    cegger wpi_match(device_t parent, cfdata_t match __unused, void *aux)
    178   1.1    simonb {
    179   1.1    simonb 	struct pci_attach_args *pa = aux;
    180   1.1    simonb 
    181   1.1    simonb 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
    182   1.1    simonb 		return 0;
    183   1.1    simonb 
    184   1.1    simonb 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_3945ABG_1 ||
    185   1.7  degroote 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_3945ABG_2)
    186   1.1    simonb 		return 1;
    187   1.1    simonb 
    188   1.1    simonb 	return 0;
    189   1.1    simonb }
    190   1.1    simonb 
    191   1.1    simonb /* Base Address Register */
    192   1.1    simonb #define WPI_PCI_BAR0	0x10
    193   1.1    simonb 
    194  1.41     joerg static int
    195  1.41     joerg wpi_attach_once(void)
    196  1.41     joerg {
    197  1.54  riastrad 
    198  1.41     joerg 	mutex_init(&wpi_firmware_mutex, MUTEX_DEFAULT, IPL_NONE);
    199  1.41     joerg 	return 0;
    200  1.41     joerg }
    201  1.41     joerg 
    202   1.1    simonb static void
    203  1.28  degroote wpi_attach(device_t parent __unused, device_t self, void *aux)
    204   1.1    simonb {
    205  1.28  degroote 	struct wpi_softc *sc = device_private(self);
    206   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
    207   1.1    simonb 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    208   1.1    simonb 	struct pci_attach_args *pa = aux;
    209   1.1    simonb 	const char *intrstr;
    210   1.1    simonb 	bus_space_tag_t memt;
    211   1.1    simonb 	bus_space_handle_t memh;
    212   1.1    simonb 	pcireg_t data;
    213  1.61  jakllsch 	int ac, error;
    214  1.58  christos 	char intrbuf[PCI_INTRSTR_LEN];
    215   1.1    simonb 
    216  1.41     joerg 	RUN_ONCE(&wpi_firmware_init, wpi_attach_once);
    217  1.41     joerg 	sc->fw_used = false;
    218  1.41     joerg 
    219  1.30    plunky 	sc->sc_dev = self;
    220   1.1    simonb 	sc->sc_pct = pa->pa_pc;
    221   1.1    simonb 	sc->sc_pcitag = pa->pa_tag;
    222   1.1    simonb 
    223  1.70    bouyer 	sc->sc_rsw_status = WPI_RSW_UNKNOWN;
    224  1.70    bouyer 	sc->sc_rsw.smpsw_name = device_xname(self);
    225  1.70    bouyer 	sc->sc_rsw.smpsw_type = PSWITCH_TYPE_RADIO;
    226  1.70    bouyer 	error = sysmon_pswitch_register(&sc->sc_rsw);
    227  1.70    bouyer 	if (error) {
    228  1.70    bouyer 		aprint_error_dev(self,
    229  1.70    bouyer 		    "unable to register radio switch with sysmon\n");
    230  1.70    bouyer 		return;
    231  1.70    bouyer 	}
    232  1.70    bouyer 	mutex_init(&sc->sc_rsw_mtx, MUTEX_DEFAULT, IPL_NONE);
    233  1.70    bouyer 	cv_init(&sc->sc_rsw_cv, "wpirsw");
    234  1.82  riastrad 	sc->sc_rsw_suspend = false;
    235  1.82  riastrad 	sc->sc_rsw_suspended = false;
    236  1.70    bouyer 	if (kthread_create(PRI_NONE, 0, NULL,
    237  1.70    bouyer 	    wpi_rsw_thread, sc, &sc->sc_rsw_lwp, "%s", device_xname(self))) {
    238  1.70    bouyer 		aprint_error_dev(self, "couldn't create switch thread\n");
    239  1.70    bouyer 	}
    240  1.70    bouyer 
    241  1.14        ad 	callout_init(&sc->calib_to, 0);
    242  1.28  degroote 	callout_setfunc(&sc->calib_to, wpi_calib_timeout, sc);
    243   1.1    simonb 
    244  1.50  drochner 	pci_aprint_devinfo(pa, NULL);
    245   1.1    simonb 
    246   1.1    simonb 	/* enable bus-mastering */
    247   1.1    simonb 	data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
    248   1.1    simonb 	data |= PCI_COMMAND_MASTER_ENABLE;
    249   1.1    simonb 	pci_conf_write(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG, data);
    250   1.1    simonb 
    251   1.1    simonb 	/* map the register window */
    252   1.1    simonb 	error = pci_mapreg_map(pa, WPI_PCI_BAR0, PCI_MAPREG_TYPE_MEM |
    253  1.59  jakllsch 	    PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh, NULL, &sc->sc_sz);
    254   1.1    simonb 	if (error != 0) {
    255  1.28  degroote 		aprint_error_dev(self, "could not map memory space\n");
    256   1.1    simonb 		return;
    257   1.1    simonb 	}
    258   1.1    simonb 
    259   1.1    simonb 	sc->sc_st = memt;
    260   1.1    simonb 	sc->sc_sh = memh;
    261   1.1    simonb 	sc->sc_dmat = pa->pa_dmat;
    262   1.1    simonb 
    263  1.77    nonaka 	sc->sc_soft_ih = softint_establish(SOFTINT_NET, wpi_softintr, sc);
    264  1.77    nonaka 	if (sc->sc_soft_ih == NULL) {
    265  1.77    nonaka 		aprint_error_dev(self, "could not establish softint\n");
    266  1.77    nonaka 		goto unmap;
    267  1.77    nonaka 	}
    268  1.77    nonaka 
    269  1.76  jakllsch 	if (pci_intr_alloc(pa, &sc->sc_pihp, NULL, 0)) {
    270  1.28  degroote 		aprint_error_dev(self, "could not map interrupt\n");
    271  1.77    nonaka 		goto failsi;
    272   1.1    simonb 	}
    273   1.1    simonb 
    274  1.76  jakllsch 	intrstr = pci_intr_string(sc->sc_pct, sc->sc_pihp[0], intrbuf,
    275  1.76  jakllsch 	    sizeof(intrbuf));
    276  1.84  jdolecek 	sc->sc_ih = pci_intr_establish_xname(sc->sc_pct, sc->sc_pihp[0],
    277  1.84  jdolecek 	    IPL_NET, wpi_intr, sc, device_xname(self));
    278   1.1    simonb 	if (sc->sc_ih == NULL) {
    279  1.28  degroote 		aprint_error_dev(self, "could not establish interrupt");
    280   1.1    simonb 		if (intrstr != NULL)
    281   1.1    simonb 			aprint_error(" at %s", intrstr);
    282   1.1    simonb 		aprint_error("\n");
    283  1.77    nonaka 		goto failia;
    284   1.1    simonb 	}
    285  1.28  degroote 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
    286   1.1    simonb 
    287  1.61  jakllsch 	/*
    288  1.61  jakllsch 	 * Put adapter into a known state.
    289  1.61  jakllsch 	 */
    290  1.61  jakllsch 	if ((error = wpi_reset(sc)) != 0) {
    291  1.28  degroote 		aprint_error_dev(self, "could not reset adapter\n");
    292  1.77    nonaka 		goto failih;
    293   1.1    simonb 	}
    294   1.1    simonb 
    295  1.59  jakllsch 	/*
    296  1.12  degroote 	 * Allocate DMA memory for firmware transfers.
    297  1.12  degroote 	 */
    298  1.61  jakllsch 	if ((error = wpi_alloc_fwmem(sc)) != 0) {
    299  1.61  jakllsch 		aprint_error_dev(self, "could not allocate firmware memory\n");
    300  1.77    nonaka 		goto failih;
    301  1.61  jakllsch 	}
    302  1.12  degroote 
    303   1.1    simonb 	/*
    304   1.1    simonb 	 * Allocate shared page and Tx/Rx rings.
    305   1.1    simonb 	 */
    306   1.1    simonb 	if ((error = wpi_alloc_shared(sc)) != 0) {
    307  1.28  degroote 		aprint_error_dev(self, "could not allocate shared area\n");
    308  1.12  degroote 		goto fail1;
    309   1.1    simonb 	}
    310   1.1    simonb 
    311   1.7  degroote 	if ((error = wpi_alloc_rpool(sc)) != 0) {
    312  1.28  degroote 		aprint_error_dev(self, "could not allocate Rx buffers\n");
    313  1.12  degroote 		goto fail2;
    314   1.7  degroote 	}
    315   1.7  degroote 
    316   1.1    simonb 	for (ac = 0; ac < 4; ac++) {
    317  1.61  jakllsch 		error = wpi_alloc_tx_ring(sc, &sc->txq[ac], WPI_TX_RING_COUNT,
    318  1.61  jakllsch 		    ac);
    319   1.1    simonb 		if (error != 0) {
    320  1.61  jakllsch 			aprint_error_dev(self,
    321  1.61  jakllsch 			    "could not allocate Tx ring %d\n", ac);
    322  1.12  degroote 			goto fail3;
    323   1.1    simonb 		}
    324   1.1    simonb 	}
    325   1.1    simonb 
    326   1.1    simonb 	error = wpi_alloc_tx_ring(sc, &sc->cmdq, WPI_CMD_RING_COUNT, 4);
    327   1.1    simonb 	if (error != 0) {
    328  1.28  degroote 		aprint_error_dev(self, "could not allocate command ring\n");
    329  1.12  degroote 		goto fail3;
    330   1.1    simonb 	}
    331   1.1    simonb 
    332  1.61  jakllsch 	error = wpi_alloc_rx_ring(sc, &sc->rxq);
    333  1.61  jakllsch 	if (error != 0) {
    334  1.28  degroote 		aprint_error_dev(self, "could not allocate Rx ring\n");
    335  1.24  degroote 		goto fail4;
    336   1.1    simonb 	}
    337   1.1    simonb 
    338   1.1    simonb 	ic->ic_ifp = ifp;
    339  1.61  jakllsch 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
    340  1.61  jakllsch 	ic->ic_opmode = IEEE80211_M_STA;	/* default to BSS mode */
    341   1.1    simonb 	ic->ic_state = IEEE80211_S_INIT;
    342   1.1    simonb 
    343   1.1    simonb 	/* set device capabilities */
    344   1.1    simonb 	ic->ic_caps =
    345  1.59  jakllsch 	    IEEE80211_C_WPA |		/* 802.11i */
    346  1.59  jakllsch 	    IEEE80211_C_MONITOR |	/* monitor mode supported */
    347  1.59  jakllsch 	    IEEE80211_C_TXPMGT |	/* tx power management */
    348  1.59  jakllsch 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
    349  1.59  jakllsch 	    IEEE80211_C_SHPREAMBLE |	/* short preamble supported */
    350  1.59  jakllsch 	    IEEE80211_C_WME;		/* 802.11e */
    351   1.1    simonb 
    352  1.12  degroote 	/* read supported channels and MAC address from EEPROM */
    353   1.1    simonb 	wpi_read_eeprom(sc);
    354   1.1    simonb 
    355  1.59  jakllsch 	/* set supported .11a, .11b and .11g rates */
    356  1.59  jakllsch 	ic->ic_sup_rates[IEEE80211_MODE_11A] = ieee80211_std_rateset_11a;
    357  1.59  jakllsch 	ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b;
    358  1.59  jakllsch 	ic->ic_sup_rates[IEEE80211_MODE_11G] = ieee80211_std_rateset_11g;
    359   1.1    simonb 
    360  1.59  jakllsch 	/* IBSS channel undefined for now */
    361   1.1    simonb 	ic->ic_ibss_chan = &ic->ic_channels[0];
    362   1.1    simonb 
    363   1.1    simonb 	ifp->if_softc = sc;
    364   1.1    simonb 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    365   1.1    simonb 	ifp->if_init = wpi_init;
    366   1.1    simonb 	ifp->if_stop = wpi_stop;
    367   1.1    simonb 	ifp->if_ioctl = wpi_ioctl;
    368   1.1    simonb 	ifp->if_start = wpi_start;
    369   1.1    simonb 	ifp->if_watchdog = wpi_watchdog;
    370   1.1    simonb 	IFQ_SET_READY(&ifp->if_snd);
    371  1.28  degroote 	memcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
    372   1.1    simonb 
    373  1.91  riastrad 	if_initialize(ifp);
    374   1.1    simonb 	ieee80211_ifattach(ic);
    375  1.77    nonaka 	/* Use common softint-based if_input */
    376  1.77    nonaka 	ifp->if_percpuq = if_percpuq_create(ifp);
    377  1.77    nonaka 	if_register(ifp);
    378  1.77    nonaka 
    379   1.1    simonb 	/* override default methods */
    380   1.1    simonb 	ic->ic_node_alloc = wpi_node_alloc;
    381   1.5     joerg 	ic->ic_newassoc = wpi_newassoc;
    382   1.1    simonb 	ic->ic_wme.wme_update = wpi_wme_update;
    383   1.1    simonb 
    384   1.1    simonb 	/* override state transition machine */
    385   1.1    simonb 	sc->sc_newstate = ic->ic_newstate;
    386   1.1    simonb 	ic->ic_newstate = wpi_newstate;
    387  1.89     sevan 
    388  1.89     sevan 	/* XXX media locking needs revisiting */
    389  1.89     sevan 	mutex_init(&sc->sc_media_mtx, MUTEX_DEFAULT, IPL_SOFTNET);
    390  1.89     sevan 	ieee80211_media_init_with_lock(ic,
    391  1.89     sevan 	    wpi_media_change, ieee80211_media_status, &sc->sc_media_mtx);
    392   1.1    simonb 
    393  1.59  jakllsch 	sc->amrr.amrr_min_success_threshold =  1;
    394   1.5     joerg 	sc->amrr.amrr_max_success_threshold = 15;
    395   1.5     joerg 
    396  1.34  degroote 	wpi_sysctlattach(sc);
    397  1.34  degroote 
    398  1.43   tsutsui 	if (pmf_device_register(self, NULL, wpi_resume))
    399  1.43   tsutsui 		pmf_class_network_register(self, ifp);
    400  1.43   tsutsui 	else
    401  1.33  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    402   1.1    simonb 
    403  1.47     joerg 	bpf_attach2(ifp, DLT_IEEE802_11_RADIO,
    404  1.45     pooka 	    sizeof(struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
    405  1.45     pooka 	    &sc->sc_drvbpf);
    406   1.1    simonb 
    407   1.1    simonb 	sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
    408   1.1    simonb 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
    409   1.1    simonb 	sc->sc_rxtap.wr_ihdr.it_present = htole32(WPI_RX_RADIOTAP_PRESENT);
    410   1.1    simonb 
    411   1.1    simonb 	sc->sc_txtap_len = sizeof sc->sc_txtapu;
    412   1.1    simonb 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
    413   1.1    simonb 	sc->sc_txtap.wt_ihdr.it_present = htole32(WPI_TX_RADIOTAP_PRESENT);
    414   1.1    simonb 
    415   1.1    simonb 	ieee80211_announce(ic);
    416   1.1    simonb 
    417   1.1    simonb 	return;
    418   1.1    simonb 
    419  1.61  jakllsch 	/* free allocated memory if something failed during attachment */
    420  1.61  jakllsch fail4:	wpi_free_tx_ring(sc, &sc->cmdq);
    421  1.61  jakllsch fail3:	while (--ac >= 0)
    422  1.61  jakllsch 		wpi_free_tx_ring(sc, &sc->txq[ac]);
    423   1.7  degroote 	wpi_free_rpool(sc);
    424  1.12  degroote fail2:	wpi_free_shared(sc);
    425  1.12  degroote fail1:	wpi_free_fwmem(sc);
    426  1.77    nonaka failih:	pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
    427  1.77    nonaka 	sc->sc_ih = NULL;
    428  1.77    nonaka failia:	pci_intr_release(sc->sc_pct, sc->sc_pihp, 1);
    429  1.77    nonaka 	sc->sc_pihp = NULL;
    430  1.77    nonaka failsi:	softint_disestablish(sc->sc_soft_ih);
    431  1.77    nonaka 	sc->sc_soft_ih = NULL;
    432  1.77    nonaka unmap:	bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz);
    433   1.1    simonb }
    434   1.1    simonb 
    435   1.1    simonb static int
    436  1.28  degroote wpi_detach(device_t self, int flags __unused)
    437   1.1    simonb {
    438  1.28  degroote 	struct wpi_softc *sc = device_private(self);
    439   1.7  degroote 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
    440   1.1    simonb 	int ac;
    441   1.1    simonb 
    442   1.1    simonb 	wpi_stop(ifp, 1);
    443   1.1    simonb 
    444   1.1    simonb 	if (ifp != NULL)
    445  1.47     joerg 		bpf_detach(ifp);
    446   1.1    simonb 	ieee80211_ifdetach(&sc->sc_ic);
    447   1.1    simonb 	if (ifp != NULL)
    448   1.1    simonb 		if_detach(ifp);
    449   1.1    simonb 
    450   1.1    simonb 	for (ac = 0; ac < 4; ac++)
    451   1.1    simonb 		wpi_free_tx_ring(sc, &sc->txq[ac]);
    452   1.1    simonb 	wpi_free_tx_ring(sc, &sc->cmdq);
    453   1.1    simonb 	wpi_free_rx_ring(sc, &sc->rxq);
    454   1.7  degroote 	wpi_free_rpool(sc);
    455   1.1    simonb 	wpi_free_shared(sc);
    456   1.1    simonb 
    457   1.1    simonb 	if (sc->sc_ih != NULL) {
    458   1.1    simonb 		pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
    459   1.1    simonb 		sc->sc_ih = NULL;
    460   1.1    simonb 	}
    461  1.76  jakllsch 	if (sc->sc_pihp != NULL) {
    462  1.76  jakllsch 		pci_intr_release(sc->sc_pct, sc->sc_pihp, 1);
    463  1.76  jakllsch 		sc->sc_pihp = NULL;
    464  1.76  jakllsch 	}
    465  1.77    nonaka 	if (sc->sc_soft_ih != NULL) {
    466  1.77    nonaka 		softint_disestablish(sc->sc_soft_ih);
    467  1.77    nonaka 		sc->sc_soft_ih = NULL;
    468  1.77    nonaka 	}
    469  1.77    nonaka 
    470  1.70    bouyer 	mutex_enter(&sc->sc_rsw_mtx);
    471  1.70    bouyer 	sc->sc_dying = 1;
    472  1.70    bouyer 	cv_signal(&sc->sc_rsw_cv);
    473  1.70    bouyer 	while (sc->sc_rsw_lwp != NULL)
    474  1.70    bouyer 		cv_wait(&sc->sc_rsw_cv, &sc->sc_rsw_mtx);
    475  1.70    bouyer 	mutex_exit(&sc->sc_rsw_mtx);
    476  1.70    bouyer 	sysmon_pswitch_unregister(&sc->sc_rsw);
    477   1.1    simonb 
    478   1.1    simonb 	bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz);
    479   1.1    simonb 
    480  1.41     joerg 	if (sc->fw_used) {
    481  1.54  riastrad 		sc->fw_used = false;
    482  1.54  riastrad 		wpi_release_firmware();
    483  1.41     joerg 	}
    484  1.70    bouyer 	cv_destroy(&sc->sc_rsw_cv);
    485  1.70    bouyer 	mutex_destroy(&sc->sc_rsw_mtx);
    486   1.1    simonb 	return 0;
    487   1.1    simonb }
    488   1.1    simonb 
    489   1.1    simonb static int
    490  1.61  jakllsch wpi_dma_contig_alloc(bus_dma_tag_t tag, struct wpi_dma_info *dma, void **kvap,
    491  1.61  jakllsch     bus_size_t size, bus_size_t alignment, int flags)
    492   1.1    simonb {
    493   1.1    simonb 	int nsegs, error;
    494   1.1    simonb 
    495   1.7  degroote 	dma->tag = tag;
    496   1.1    simonb 	dma->size = size;
    497   1.1    simonb 
    498   1.7  degroote 	error = bus_dmamap_create(tag, size, 1, size, 0, flags, &dma->map);
    499   1.7  degroote 	if (error != 0)
    500   1.1    simonb 		goto fail;
    501   1.1    simonb 
    502   1.7  degroote 	error = bus_dmamem_alloc(tag, size, alignment, 0, &dma->seg, 1, &nsegs,
    503   1.7  degroote 	    flags);
    504   1.7  degroote 	if (error != 0)
    505   1.1    simonb 		goto fail;
    506   1.1    simonb 
    507   1.7  degroote 	error = bus_dmamem_map(tag, &dma->seg, 1, size, &dma->vaddr, flags);
    508   1.7  degroote 	if (error != 0)
    509   1.1    simonb 		goto fail;
    510   1.1    simonb 
    511   1.7  degroote 	error = bus_dmamap_load(tag, dma->map, dma->vaddr, size, NULL, flags);
    512   1.7  degroote 	if (error != 0)
    513   1.1    simonb 		goto fail;
    514   1.1    simonb 
    515   1.1    simonb 	memset(dma->vaddr, 0, size);
    516  1.63  jmcneill 	bus_dmamap_sync(dma->tag, dma->map, 0, size, BUS_DMASYNC_PREWRITE);
    517   1.1    simonb 
    518   1.1    simonb 	dma->paddr = dma->map->dm_segs[0].ds_addr;
    519   1.7  degroote 	if (kvap != NULL)
    520   1.7  degroote 		*kvap = dma->vaddr;
    521   1.1    simonb 
    522   1.1    simonb 	return 0;
    523   1.1    simonb 
    524  1.61  jakllsch fail:	wpi_dma_contig_free(dma);
    525   1.1    simonb 	return error;
    526   1.1    simonb }
    527   1.1    simonb 
    528   1.1    simonb static void
    529   1.7  degroote wpi_dma_contig_free(struct wpi_dma_info *dma)
    530   1.1    simonb {
    531   1.1    simonb 	if (dma->map != NULL) {
    532   1.1    simonb 		if (dma->vaddr != NULL) {
    533   1.7  degroote 			bus_dmamap_unload(dma->tag, dma->map);
    534   1.7  degroote 			bus_dmamem_unmap(dma->tag, dma->vaddr, dma->size);
    535   1.7  degroote 			bus_dmamem_free(dma->tag, &dma->seg, 1);
    536   1.1    simonb 			dma->vaddr = NULL;
    537   1.1    simonb 		}
    538   1.7  degroote 		bus_dmamap_destroy(dma->tag, dma->map);
    539   1.1    simonb 		dma->map = NULL;
    540   1.1    simonb 	}
    541   1.1    simonb }
    542   1.1    simonb 
    543   1.1    simonb /*
    544   1.1    simonb  * Allocate a shared page between host and NIC.
    545   1.1    simonb  */
    546   1.1    simonb static int
    547   1.1    simonb wpi_alloc_shared(struct wpi_softc *sc)
    548   1.1    simonb {
    549   1.1    simonb 	int error;
    550  1.61  jakllsch 
    551   1.1    simonb 	/* must be aligned on a 4K-page boundary */
    552   1.7  degroote 	error = wpi_dma_contig_alloc(sc->sc_dmat, &sc->shared_dma,
    553  1.61  jakllsch 	    (void **)&sc->shared, sizeof (struct wpi_shared), WPI_BUF_ALIGN,
    554  1.61  jakllsch 	    BUS_DMA_NOWAIT);
    555   1.1    simonb 	if (error != 0)
    556  1.28  degroote 		aprint_error_dev(sc->sc_dev,
    557  1.61  jakllsch 		    "could not allocate shared area DMA memory\n");
    558   1.1    simonb 
    559   1.1    simonb 	return error;
    560   1.1    simonb }
    561   1.1    simonb 
    562   1.1    simonb static void
    563   1.1    simonb wpi_free_shared(struct wpi_softc *sc)
    564   1.1    simonb {
    565   1.7  degroote 	wpi_dma_contig_free(&sc->shared_dma);
    566   1.7  degroote }
    567   1.7  degroote 
    568  1.12  degroote /*
    569  1.12  degroote  * Allocate DMA-safe memory for firmware transfer.
    570  1.12  degroote  */
    571  1.12  degroote static int
    572  1.12  degroote wpi_alloc_fwmem(struct wpi_softc *sc)
    573  1.12  degroote {
    574  1.12  degroote 	int error;
    575  1.61  jakllsch 
    576  1.12  degroote 	/* allocate enough contiguous space to store text and data */
    577  1.12  degroote 	error = wpi_dma_contig_alloc(sc->sc_dmat, &sc->fw_dma, NULL,
    578  1.12  degroote 	    WPI_FW_MAIN_TEXT_MAXSZ + WPI_FW_MAIN_DATA_MAXSZ, 0,
    579  1.12  degroote 	    BUS_DMA_NOWAIT);
    580  1.12  degroote 
    581  1.12  degroote 	if (error != 0)
    582  1.28  degroote 		aprint_error_dev(sc->sc_dev,
    583  1.61  jakllsch 		    "could not allocate firmware transfer area DMA memory\n");
    584  1.12  degroote 	return error;
    585  1.12  degroote }
    586  1.12  degroote 
    587  1.12  degroote static void
    588  1.12  degroote wpi_free_fwmem(struct wpi_softc *sc)
    589  1.12  degroote {
    590  1.12  degroote 	wpi_dma_contig_free(&sc->fw_dma);
    591  1.12  degroote }
    592  1.12  degroote 
    593   1.7  degroote static struct wpi_rbuf *
    594   1.7  degroote wpi_alloc_rbuf(struct wpi_softc *sc)
    595   1.7  degroote {
    596   1.7  degroote 	struct wpi_rbuf *rbuf;
    597   1.7  degroote 
    598  1.39      cube 	mutex_enter(&sc->rxq.freelist_mtx);
    599   1.7  degroote 	rbuf = SLIST_FIRST(&sc->rxq.freelist);
    600  1.39      cube 	if (rbuf != NULL) {
    601  1.39      cube 		SLIST_REMOVE_HEAD(&sc->rxq.freelist, next);
    602  1.39      cube 	}
    603  1.39      cube 	mutex_exit(&sc->rxq.freelist_mtx);
    604  1.10  degroote 
    605   1.7  degroote 	return rbuf;
    606   1.7  degroote }
    607   1.7  degroote 
    608   1.7  degroote /*
    609   1.7  degroote  * This is called automatically by the network stack when the mbuf to which our
    610   1.7  degroote  * Rx buffer is attached is freed.
    611   1.7  degroote  */
    612   1.7  degroote static void
    613   1.9  christos wpi_free_rbuf(struct mbuf* m, void *buf, size_t size, void *arg)
    614   1.7  degroote {
    615   1.7  degroote 	struct wpi_rbuf *rbuf = arg;
    616   1.7  degroote 	struct wpi_softc *sc = rbuf->sc;
    617   1.7  degroote 
    618   1.7  degroote 	/* put the buffer back in the free list */
    619  1.10  degroote 
    620  1.39      cube 	mutex_enter(&sc->rxq.freelist_mtx);
    621   1.7  degroote 	SLIST_INSERT_HEAD(&sc->rxq.freelist, rbuf, next);
    622  1.39      cube 	mutex_exit(&sc->rxq.freelist_mtx);
    623   1.7  degroote 
    624  1.27        ad 	if (__predict_true(m != NULL))
    625  1.27        ad 		pool_cache_put(mb_cache, m);
    626   1.7  degroote }
    627   1.7  degroote 
    628   1.7  degroote static int
    629   1.7  degroote wpi_alloc_rpool(struct wpi_softc *sc)
    630   1.7  degroote {
    631   1.7  degroote 	struct wpi_rx_ring *ring = &sc->rxq;
    632   1.7  degroote 	int i, error;
    633   1.7  degroote 
    634   1.7  degroote 	/* allocate a big chunk of DMA'able memory.. */
    635   1.7  degroote 	error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->buf_dma, NULL,
    636   1.7  degroote 	    WPI_RBUF_COUNT * WPI_RBUF_SIZE, WPI_BUF_ALIGN, BUS_DMA_NOWAIT);
    637   1.7  degroote 	if (error != 0) {
    638  1.61  jakllsch 		aprint_normal_dev(sc->sc_dev,
    639  1.61  jakllsch 		    "could not allocate Rx buffers DMA memory\n");
    640  1.28  degroote 		return error;
    641   1.7  degroote 	}
    642   1.7  degroote 
    643   1.7  degroote 	/* ..and split it into 3KB chunks */
    644  1.39      cube 	mutex_init(&ring->freelist_mtx, MUTEX_DEFAULT, IPL_NET);
    645   1.7  degroote 	SLIST_INIT(&ring->freelist);
    646   1.7  degroote 	for (i = 0; i < WPI_RBUF_COUNT; i++) {
    647  1.61  jakllsch 		struct wpi_rbuf *rbuf = &ring->rbuf[i];
    648  1.61  jakllsch 
    649   1.7  degroote 		rbuf->sc = sc;	/* backpointer for callbacks */
    650   1.9  christos 		rbuf->vaddr = (char *)ring->buf_dma.vaddr + i * WPI_RBUF_SIZE;
    651   1.7  degroote 		rbuf->paddr = ring->buf_dma.paddr + i * WPI_RBUF_SIZE;
    652   1.7  degroote 
    653   1.7  degroote 		SLIST_INSERT_HEAD(&ring->freelist, rbuf, next);
    654   1.7  degroote 	}
    655  1.10  degroote 
    656   1.7  degroote 	return 0;
    657   1.7  degroote }
    658   1.7  degroote 
    659   1.7  degroote static void
    660   1.7  degroote wpi_free_rpool(struct wpi_softc *sc)
    661   1.7  degroote {
    662  1.81  riastrad 	mutex_destroy(&sc->rxq.freelist_mtx);
    663   1.7  degroote 	wpi_dma_contig_free(&sc->rxq.buf_dma);
    664   1.1    simonb }
    665   1.1    simonb 
    666   1.1    simonb static int
    667   1.1    simonb wpi_alloc_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
    668   1.1    simonb {
    669  1.63  jmcneill 	bus_size_t size;
    670   1.1    simonb 	int i, error;
    671   1.1    simonb 
    672   1.1    simonb 	ring->cur = 0;
    673   1.1    simonb 
    674  1.86  jakllsch 	size = WPI_RX_RING_COUNT * sizeof (struct wpi_rx_desc);
    675   1.7  degroote 	error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->desc_dma,
    676  1.63  jmcneill 	    (void **)&ring->desc, size,
    677  1.59  jakllsch 	    WPI_RING_DMA_ALIGN, BUS_DMA_NOWAIT);
    678   1.1    simonb 	if (error != 0) {
    679  1.61  jakllsch 		aprint_error_dev(sc->sc_dev,
    680  1.61  jakllsch 		    "could not allocate rx ring DMA memory\n");
    681   1.1    simonb 		goto fail;
    682   1.1    simonb 	}
    683   1.1    simonb 
    684   1.1    simonb 	/*
    685   1.7  degroote 	 * Setup Rx buffers.
    686   1.1    simonb 	 */
    687   1.1    simonb 	for (i = 0; i < WPI_RX_RING_COUNT; i++) {
    688  1.61  jakllsch 		struct wpi_rx_data *data = &ring->data[i];
    689  1.61  jakllsch 		struct wpi_rbuf *rbuf;
    690   1.1    simonb 
    691  1.64  jmcneill 		error = bus_dmamap_create(sc->sc_dmat, WPI_RBUF_SIZE, 1,
    692  1.64  jmcneill 		    WPI_RBUF_SIZE, 0, BUS_DMA_NOWAIT, &data->map);
    693  1.64  jmcneill 		if (error) {
    694  1.64  jmcneill 			aprint_error_dev(sc->sc_dev,
    695  1.64  jmcneill 			    "could not allocate rx dma map\n");
    696  1.64  jmcneill 			goto fail;
    697  1.64  jmcneill 		}
    698  1.64  jmcneill 
    699   1.1    simonb 		MGETHDR(data->m, M_DONTWAIT, MT_DATA);
    700   1.1    simonb 		if (data->m == NULL) {
    701  1.61  jakllsch 			aprint_error_dev(sc->sc_dev,
    702  1.61  jakllsch 			    "could not allocate rx mbuf\n");
    703   1.1    simonb 			error = ENOMEM;
    704   1.1    simonb 			goto fail;
    705   1.1    simonb 		}
    706   1.7  degroote 		if ((rbuf = wpi_alloc_rbuf(sc)) == NULL) {
    707   1.1    simonb 			m_freem(data->m);
    708   1.1    simonb 			data->m = NULL;
    709  1.61  jakllsch 			aprint_error_dev(sc->sc_dev,
    710  1.61  jakllsch 			    "could not allocate rx cluster\n");
    711   1.1    simonb 			error = ENOMEM;
    712   1.1    simonb 			goto fail;
    713   1.1    simonb 		}
    714   1.7  degroote 		/* attach Rx buffer to mbuf */
    715   1.7  degroote 		MEXTADD(data->m, rbuf->vaddr, WPI_RBUF_SIZE, 0, wpi_free_rbuf,
    716   1.7  degroote 		    rbuf);
    717   1.7  degroote 		data->m->m_flags |= M_EXT_RW;
    718   1.1    simonb 
    719  1.64  jmcneill 		error = bus_dmamap_load(sc->sc_dmat, data->map,
    720  1.64  jmcneill 		    mtod(data->m, void *), WPI_RBUF_SIZE, NULL,
    721  1.64  jmcneill 		    BUS_DMA_NOWAIT | BUS_DMA_READ);
    722  1.64  jmcneill 		if (error) {
    723  1.64  jmcneill 			aprint_error_dev(sc->sc_dev,
    724  1.64  jmcneill 			    "could not load mbuf: %d\n", error);
    725  1.64  jmcneill 			goto fail;
    726  1.64  jmcneill 		}
    727  1.64  jmcneill 
    728   1.7  degroote 		ring->desc[i] = htole32(rbuf->paddr);
    729   1.1    simonb 	}
    730   1.1    simonb 
    731  1.63  jmcneill 	bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map, 0, size,
    732  1.63  jmcneill 	    BUS_DMASYNC_PREWRITE);
    733  1.63  jmcneill 
    734   1.1    simonb 	return 0;
    735   1.1    simonb 
    736   1.1    simonb fail:	wpi_free_rx_ring(sc, ring);
    737   1.1    simonb 	return error;
    738   1.1    simonb }
    739   1.1    simonb 
    740   1.1    simonb static void
    741   1.1    simonb wpi_reset_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
    742   1.1    simonb {
    743   1.1    simonb 	int ntries;
    744   1.1    simonb 
    745   1.1    simonb 	wpi_mem_lock(sc);
    746   1.1    simonb 
    747   1.1    simonb 	WPI_WRITE(sc, WPI_RX_CONFIG, 0);
    748   1.1    simonb 	for (ntries = 0; ntries < 100; ntries++) {
    749   1.1    simonb 		if (WPI_READ(sc, WPI_RX_STATUS) & WPI_RX_IDLE)
    750   1.1    simonb 			break;
    751   1.1    simonb 		DELAY(10);
    752   1.1    simonb 	}
    753   1.1    simonb #ifdef WPI_DEBUG
    754   1.1    simonb 	if (ntries == 100 && wpi_debug > 0)
    755  1.28  degroote 		aprint_error_dev(sc->sc_dev, "timeout resetting Rx ring\n");
    756   1.1    simonb #endif
    757   1.1    simonb 	wpi_mem_unlock(sc);
    758   1.1    simonb 
    759   1.1    simonb 	ring->cur = 0;
    760   1.1    simonb }
    761   1.1    simonb 
    762   1.1    simonb static void
    763   1.1    simonb wpi_free_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
    764   1.1    simonb {
    765   1.1    simonb 	int i;
    766   1.1    simonb 
    767   1.7  degroote 	wpi_dma_contig_free(&ring->desc_dma);
    768   1.1    simonb 
    769   1.1    simonb 	for (i = 0; i < WPI_RX_RING_COUNT; i++) {
    770  1.64  jmcneill 		if (ring->data[i].m != NULL) {
    771  1.64  jmcneill 			bus_dmamap_unload(sc->sc_dmat, ring->data[i].map);
    772   1.7  degroote 			m_freem(ring->data[i].m);
    773  1.64  jmcneill 		}
    774  1.64  jmcneill 		if (ring->data[i].map != NULL) {
    775  1.64  jmcneill 			bus_dmamap_destroy(sc->sc_dmat, ring->data[i].map);
    776  1.64  jmcneill 		}
    777   1.1    simonb 	}
    778   1.1    simonb }
    779   1.1    simonb 
    780   1.1    simonb static int
    781   1.1    simonb wpi_alloc_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring, int count,
    782  1.61  jakllsch     int qid)
    783   1.1    simonb {
    784   1.1    simonb 	int i, error;
    785   1.1    simonb 
    786   1.1    simonb 	ring->qid = qid;
    787   1.1    simonb 	ring->count = count;
    788   1.1    simonb 	ring->queued = 0;
    789   1.1    simonb 	ring->cur = 0;
    790   1.1    simonb 
    791   1.7  degroote 	error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->desc_dma,
    792  1.61  jakllsch 	    (void **)&ring->desc, count * sizeof (struct wpi_tx_desc),
    793  1.61  jakllsch 	    WPI_RING_DMA_ALIGN, BUS_DMA_NOWAIT);
    794   1.1    simonb 	if (error != 0) {
    795  1.61  jakllsch 		aprint_error_dev(sc->sc_dev,
    796  1.61  jakllsch 		    "could not allocate tx ring DMA memory\n");
    797   1.1    simonb 		goto fail;
    798   1.1    simonb 	}
    799   1.1    simonb 
    800   1.1    simonb 	/* update shared page with ring's base address */
    801   1.1    simonb 	sc->shared->txbase[qid] = htole32(ring->desc_dma.paddr);
    802  1.63  jmcneill 	bus_dmamap_sync(sc->sc_dmat, sc->shared_dma.map, 0,
    803  1.63  jmcneill 	    sizeof(struct wpi_shared), BUS_DMASYNC_PREWRITE);
    804   1.1    simonb 
    805   1.7  degroote 	error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->cmd_dma,
    806  1.61  jakllsch 	    (void **)&ring->cmd, count * sizeof (struct wpi_tx_cmd), 4,
    807  1.61  jakllsch 	    BUS_DMA_NOWAIT);
    808   1.1    simonb 	if (error != 0) {
    809  1.61  jakllsch 		aprint_error_dev(sc->sc_dev,
    810  1.61  jakllsch 		    "could not allocate tx cmd DMA memory\n");
    811   1.1    simonb 		goto fail;
    812   1.1    simonb 	}
    813   1.1    simonb 
    814   1.1    simonb 	ring->data = malloc(count * sizeof (struct wpi_tx_data), M_DEVBUF,
    815  1.87       chs 	    M_WAITOK | M_ZERO);
    816   1.1    simonb 
    817   1.1    simonb 	for (i = 0; i < count; i++) {
    818  1.61  jakllsch 		struct wpi_tx_data *data = &ring->data[i];
    819   1.1    simonb 
    820   1.1    simonb 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    821  1.61  jakllsch 		    WPI_MAX_SCATTER - 1, MCLBYTES, 0, BUS_DMA_NOWAIT,
    822  1.61  jakllsch 		    &data->map);
    823   1.1    simonb 		if (error != 0) {
    824  1.61  jakllsch 			aprint_error_dev(sc->sc_dev,
    825  1.61  jakllsch 			    "could not create tx buf DMA map\n");
    826   1.1    simonb 			goto fail;
    827   1.1    simonb 		}
    828   1.1    simonb 	}
    829   1.1    simonb 
    830   1.1    simonb 	return 0;
    831   1.1    simonb 
    832   1.1    simonb fail:	wpi_free_tx_ring(sc, ring);
    833   1.1    simonb 	return error;
    834   1.1    simonb }
    835   1.1    simonb 
    836   1.1    simonb static void
    837   1.1    simonb wpi_reset_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
    838   1.1    simonb {
    839   1.1    simonb 	int i, ntries;
    840   1.1    simonb 
    841   1.1    simonb 	wpi_mem_lock(sc);
    842   1.1    simonb 
    843   1.1    simonb 	WPI_WRITE(sc, WPI_TX_CONFIG(ring->qid), 0);
    844   1.1    simonb 	for (ntries = 0; ntries < 100; ntries++) {
    845   1.1    simonb 		if (WPI_READ(sc, WPI_TX_STATUS) & WPI_TX_IDLE(ring->qid))
    846   1.1    simonb 			break;
    847   1.1    simonb 		DELAY(10);
    848   1.1    simonb 	}
    849   1.1    simonb #ifdef WPI_DEBUG
    850   1.1    simonb 	if (ntries == 100 && wpi_debug > 0) {
    851  1.28  degroote 		aprint_error_dev(sc->sc_dev, "timeout resetting Tx ring %d\n",
    852  1.59  jakllsch 		    ring->qid);
    853   1.1    simonb 	}
    854   1.1    simonb #endif
    855   1.1    simonb 	wpi_mem_unlock(sc);
    856   1.1    simonb 
    857   1.1    simonb 	for (i = 0; i < ring->count; i++) {
    858  1.61  jakllsch 		struct wpi_tx_data *data = &ring->data[i];
    859   1.1    simonb 
    860   1.1    simonb 		if (data->m != NULL) {
    861   1.1    simonb 			bus_dmamap_unload(sc->sc_dmat, data->map);
    862   1.1    simonb 			m_freem(data->m);
    863   1.1    simonb 			data->m = NULL;
    864   1.1    simonb 		}
    865   1.1    simonb 	}
    866   1.1    simonb 
    867   1.1    simonb 	ring->queued = 0;
    868   1.1    simonb 	ring->cur = 0;
    869   1.1    simonb }
    870   1.1    simonb 
    871   1.1    simonb static void
    872   1.1    simonb wpi_free_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
    873   1.1    simonb {
    874   1.1    simonb 	int i;
    875   1.1    simonb 
    876   1.7  degroote 	wpi_dma_contig_free(&ring->desc_dma);
    877   1.7  degroote 	wpi_dma_contig_free(&ring->cmd_dma);
    878   1.1    simonb 
    879   1.1    simonb 	if (ring->data != NULL) {
    880   1.1    simonb 		for (i = 0; i < ring->count; i++) {
    881  1.61  jakllsch 			struct wpi_tx_data *data = &ring->data[i];
    882   1.1    simonb 
    883   1.1    simonb 			if (data->m != NULL) {
    884   1.1    simonb 				bus_dmamap_unload(sc->sc_dmat, data->map);
    885   1.1    simonb 				m_freem(data->m);
    886   1.1    simonb 			}
    887   1.1    simonb 		}
    888   1.1    simonb 		free(ring->data, M_DEVBUF);
    889   1.1    simonb 	}
    890   1.1    simonb }
    891   1.1    simonb 
    892   1.1    simonb /*ARGUSED*/
    893   1.1    simonb static struct ieee80211_node *
    894   1.7  degroote wpi_node_alloc(struct ieee80211_node_table *nt __unused)
    895   1.1    simonb {
    896   1.5     joerg 	struct wpi_node *wn;
    897   1.1    simonb 
    898  1.35    simonb 	wn = malloc(sizeof (struct wpi_node), M_80211_NODE, M_NOWAIT | M_ZERO);
    899   1.5     joerg 
    900   1.5     joerg 	return (struct ieee80211_node *)wn;
    901   1.1    simonb }
    902   1.1    simonb 
    903  1.12  degroote static void
    904  1.12  degroote wpi_newassoc(struct ieee80211_node *ni, int isnew)
    905  1.12  degroote {
    906  1.12  degroote 	struct wpi_softc *sc = ni->ni_ic->ic_ifp->if_softc;
    907  1.12  degroote 	int i;
    908  1.12  degroote 
    909  1.12  degroote 	ieee80211_amrr_node_init(&sc->amrr, &((struct wpi_node *)ni)->amn);
    910  1.12  degroote 
    911  1.12  degroote 	/* set rate to some reasonable initial value */
    912  1.12  degroote 	for (i = ni->ni_rates.rs_nrates - 1;
    913  1.12  degroote 	     i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72;
    914  1.12  degroote 	     i--);
    915  1.12  degroote 	ni->ni_txrate = i;
    916  1.12  degroote }
    917  1.12  degroote 
    918   1.1    simonb static int
    919   1.1    simonb wpi_media_change(struct ifnet *ifp)
    920   1.1    simonb {
    921   1.1    simonb 	int error;
    922   1.1    simonb 
    923   1.1    simonb 	error = ieee80211_media_change(ifp);
    924   1.1    simonb 	if (error != ENETRESET)
    925   1.1    simonb 		return error;
    926   1.1    simonb 
    927   1.1    simonb 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
    928   1.1    simonb 		wpi_init(ifp);
    929   1.1    simonb 
    930   1.1    simonb 	return 0;
    931   1.1    simonb }
    932   1.1    simonb 
    933   1.1    simonb static int
    934   1.1    simonb wpi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
    935   1.1    simonb {
    936   1.1    simonb 	struct ifnet *ifp = ic->ic_ifp;
    937   1.1    simonb 	struct wpi_softc *sc = ifp->if_softc;
    938  1.12  degroote 	struct ieee80211_node *ni;
    939  1.67  jmcneill 	enum ieee80211_state ostate = ic->ic_state;
    940   1.1    simonb 	int error;
    941   1.1    simonb 
    942  1.12  degroote 	callout_stop(&sc->calib_to);
    943   1.1    simonb 
    944   1.1    simonb 	switch (nstate) {
    945   1.1    simonb 	case IEEE80211_S_SCAN:
    946  1.55  christos 
    947  1.23  degroote 		if (sc->is_scanning)
    948  1.23  degroote 			break;
    949  1.23  degroote 
    950  1.23  degroote 		sc->is_scanning = true;
    951   1.1    simonb 
    952  1.67  jmcneill 		if (ostate != IEEE80211_S_SCAN) {
    953  1.67  jmcneill 			/* make the link LED blink while we're scanning */
    954  1.67  jmcneill 			wpi_set_led(sc, WPI_LED_LINK, 20, 2);
    955  1.67  jmcneill 		}
    956   1.1    simonb 
    957  1.67  jmcneill 		if ((error = wpi_scan(sc)) != 0) {
    958  1.61  jakllsch 			aprint_error_dev(sc->sc_dev,
    959  1.61  jakllsch 			    "could not initiate scan\n");
    960   1.1    simonb 			return error;
    961   1.1    simonb 		}
    962  1.67  jmcneill 		break;
    963   1.1    simonb 
    964   1.7  degroote 	case IEEE80211_S_ASSOC:
    965   1.7  degroote 		if (ic->ic_state != IEEE80211_S_RUN)
    966   1.7  degroote 			break;
    967   1.7  degroote 		/* FALLTHROUGH */
    968   1.1    simonb 	case IEEE80211_S_AUTH:
    969  1.61  jakllsch 		/* reset state to handle reassociations correctly */
    970  1.12  degroote 		sc->config.associd = 0;
    971   1.1    simonb 		sc->config.filter &= ~htole32(WPI_FILTER_BSS);
    972  1.61  jakllsch 
    973   1.1    simonb 		if ((error = wpi_auth(sc)) != 0) {
    974  1.55  christos 			aprint_error_dev(sc->sc_dev,
    975  1.59  jakllsch 			    "could not send authentication request\n");
    976   1.1    simonb 			return error;
    977   1.1    simonb 		}
    978   1.1    simonb 		break;
    979   1.1    simonb 
    980   1.1    simonb 	case IEEE80211_S_RUN:
    981   1.1    simonb 		if (ic->ic_opmode == IEEE80211_M_MONITOR) {
    982   1.1    simonb 			/* link LED blinks while monitoring */
    983   1.1    simonb 			wpi_set_led(sc, WPI_LED_LINK, 5, 5);
    984   1.1    simonb 			break;
    985   1.1    simonb 		}
    986  1.12  degroote 		ni = ic->ic_bss;
    987   1.1    simonb 
    988   1.1    simonb 		if (ic->ic_opmode != IEEE80211_M_STA) {
    989   1.1    simonb 			(void) wpi_auth(sc);    /* XXX */
    990  1.12  degroote 			wpi_setup_beacon(sc, ni);
    991   1.1    simonb 		}
    992   1.1    simonb 
    993  1.12  degroote 		wpi_enable_tsf(sc, ni);
    994   1.1    simonb 
    995   1.1    simonb 		/* update adapter's configuration */
    996  1.12  degroote 		sc->config.associd = htole16(ni->ni_associd & ~0xc000);
    997   1.1    simonb 		/* short preamble/slot time are negotiated when associating */
    998   1.1    simonb 		sc->config.flags &= ~htole32(WPI_CONFIG_SHPREAMBLE |
    999  1.61  jakllsch 		    WPI_CONFIG_SHSLOT);
   1000   1.1    simonb 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
   1001   1.1    simonb 			sc->config.flags |= htole32(WPI_CONFIG_SHSLOT);
   1002   1.1    simonb 		if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
   1003   1.1    simonb 			sc->config.flags |= htole32(WPI_CONFIG_SHPREAMBLE);
   1004   1.1    simonb 		sc->config.filter |= htole32(WPI_FILTER_BSS);
   1005   1.1    simonb 		if (ic->ic_opmode != IEEE80211_M_STA)
   1006   1.1    simonb 			sc->config.filter |= htole32(WPI_FILTER_BEACON);
   1007   1.1    simonb 
   1008   1.1    simonb /* XXX put somewhere HC_QOS_SUPPORT_ASSOC + HC_IBSS_START */
   1009   1.1    simonb 
   1010   1.1    simonb 		DPRINTF(("config chan %d flags %x\n", sc->config.chan,
   1011  1.61  jakllsch 		    sc->config.flags));
   1012   1.1    simonb 		error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config,
   1013  1.61  jakllsch 		    sizeof (struct wpi_config), 1);
   1014   1.1    simonb 		if (error != 0) {
   1015  1.61  jakllsch 			aprint_error_dev(sc->sc_dev,
   1016  1.61  jakllsch 			    "could not update configuration\n");
   1017   1.1    simonb 			return error;
   1018   1.1    simonb 		}
   1019   1.1    simonb 
   1020  1.12  degroote 		/* configuration has changed, set Tx power accordingly */
   1021  1.66  jmcneill 		if ((error = wpi_set_txpower(sc, ic->ic_curchan, 1)) != 0) {
   1022  1.61  jakllsch 			aprint_error_dev(sc->sc_dev,
   1023  1.61  jakllsch 			    "could not set Tx power\n");
   1024  1.12  degroote 			return error;
   1025  1.12  degroote 		}
   1026  1.12  degroote 
   1027   1.5     joerg 		if (ic->ic_opmode == IEEE80211_M_STA) {
   1028   1.5     joerg 			/* fake a join to init the tx rate */
   1029  1.12  degroote 			wpi_newassoc(ni, 1);
   1030   1.5     joerg 		}
   1031   1.5     joerg 
   1032  1.12  degroote 		/* start periodic calibration timer */
   1033  1.12  degroote 		sc->calib_cnt = 0;
   1034  1.28  degroote 		callout_schedule(&sc->calib_to, hz/2);
   1035   1.1    simonb 
   1036   1.1    simonb 		/* link LED always on while associated */
   1037   1.1    simonb 		wpi_set_led(sc, WPI_LED_LINK, 0, 1);
   1038   1.1    simonb 		break;
   1039   1.1    simonb 
   1040   1.1    simonb 	case IEEE80211_S_INIT:
   1041  1.23  degroote 		sc->is_scanning = false;
   1042   1.1    simonb 		break;
   1043   1.1    simonb 	}
   1044   1.1    simonb 
   1045   1.1    simonb 	return sc->sc_newstate(ic, nstate, arg);
   1046   1.1    simonb }
   1047   1.1    simonb 
   1048   1.1    simonb /*
   1049   1.1    simonb  * Grab exclusive access to NIC memory.
   1050   1.1    simonb  */
   1051   1.1    simonb static void
   1052   1.1    simonb wpi_mem_lock(struct wpi_softc *sc)
   1053   1.1    simonb {
   1054   1.1    simonb 	uint32_t tmp;
   1055   1.1    simonb 	int ntries;
   1056   1.1    simonb 
   1057   1.1    simonb 	tmp = WPI_READ(sc, WPI_GPIO_CTL);
   1058   1.1    simonb 	WPI_WRITE(sc, WPI_GPIO_CTL, tmp | WPI_GPIO_MAC);
   1059   1.1    simonb 
   1060   1.1    simonb 	/* spin until we actually get the lock */
   1061   1.1    simonb 	for (ntries = 0; ntries < 1000; ntries++) {
   1062   1.1    simonb 		if ((WPI_READ(sc, WPI_GPIO_CTL) &
   1063  1.61  jakllsch 		    (WPI_GPIO_CLOCK | WPI_GPIO_SLEEP)) == WPI_GPIO_CLOCK)
   1064   1.1    simonb 			break;
   1065   1.1    simonb 		DELAY(10);
   1066   1.1    simonb 	}
   1067   1.1    simonb 	if (ntries == 1000)
   1068  1.28  degroote 		aprint_error_dev(sc->sc_dev, "could not lock memory\n");
   1069   1.1    simonb }
   1070   1.1    simonb 
   1071   1.1    simonb /*
   1072   1.1    simonb  * Release lock on NIC memory.
   1073   1.1    simonb  */
   1074   1.1    simonb static void
   1075   1.1    simonb wpi_mem_unlock(struct wpi_softc *sc)
   1076   1.1    simonb {
   1077   1.1    simonb 	uint32_t tmp = WPI_READ(sc, WPI_GPIO_CTL);
   1078   1.1    simonb 	WPI_WRITE(sc, WPI_GPIO_CTL, tmp & ~WPI_GPIO_MAC);
   1079   1.1    simonb }
   1080   1.1    simonb 
   1081   1.1    simonb static uint32_t
   1082   1.1    simonb wpi_mem_read(struct wpi_softc *sc, uint16_t addr)
   1083   1.1    simonb {
   1084   1.1    simonb 	WPI_WRITE(sc, WPI_READ_MEM_ADDR, WPI_MEM_4 | addr);
   1085   1.1    simonb 	return WPI_READ(sc, WPI_READ_MEM_DATA);
   1086   1.1    simonb }
   1087   1.1    simonb 
   1088   1.1    simonb static void
   1089   1.1    simonb wpi_mem_write(struct wpi_softc *sc, uint16_t addr, uint32_t data)
   1090   1.1    simonb {
   1091   1.1    simonb 	WPI_WRITE(sc, WPI_WRITE_MEM_ADDR, WPI_MEM_4 | addr);
   1092   1.1    simonb 	WPI_WRITE(sc, WPI_WRITE_MEM_DATA, data);
   1093   1.1    simonb }
   1094   1.1    simonb 
   1095  1.17  degroote static void
   1096  1.17  degroote wpi_mem_write_region_4(struct wpi_softc *sc, uint16_t addr,
   1097  1.59  jakllsch     const uint32_t *data, int wlen)
   1098  1.17  degroote {
   1099  1.17  degroote 	for (; wlen > 0; wlen--, data++, addr += 4)
   1100  1.17  degroote 		wpi_mem_write(sc, addr, *data);
   1101  1.17  degroote }
   1102  1.59  jakllsch 
   1103   1.1    simonb /*
   1104  1.12  degroote  * Read `len' bytes from the EEPROM.  We access the EEPROM through the MAC
   1105  1.12  degroote  * instead of using the traditional bit-bang method.
   1106   1.1    simonb  */
   1107  1.12  degroote static int
   1108  1.12  degroote wpi_read_prom_data(struct wpi_softc *sc, uint32_t addr, void *data, int len)
   1109   1.1    simonb {
   1110  1.12  degroote 	uint8_t *out = data;
   1111  1.12  degroote 	uint32_t val;
   1112   1.1    simonb 	int ntries;
   1113   1.1    simonb 
   1114  1.12  degroote 	wpi_mem_lock(sc);
   1115  1.12  degroote 	for (; len > 0; len -= 2, addr++) {
   1116  1.12  degroote 		WPI_WRITE(sc, WPI_EEPROM_CTL, addr << 2);
   1117   1.1    simonb 
   1118  1.12  degroote 		for (ntries = 0; ntries < 10; ntries++) {
   1119  1.12  degroote 			if ((val = WPI_READ(sc, WPI_EEPROM_CTL)) &
   1120  1.12  degroote 			    WPI_EEPROM_READY)
   1121  1.12  degroote 				break;
   1122  1.12  degroote 			DELAY(5);
   1123  1.12  degroote 		}
   1124  1.12  degroote 		if (ntries == 10) {
   1125  1.28  degroote 			aprint_error_dev(sc->sc_dev, "could not read EEPROM\n");
   1126  1.12  degroote 			return ETIMEDOUT;
   1127  1.12  degroote 		}
   1128  1.12  degroote 		*out++ = val >> 16;
   1129  1.12  degroote 		if (len > 1)
   1130  1.12  degroote 			*out++ = val >> 24;
   1131   1.1    simonb 	}
   1132   1.1    simonb 	wpi_mem_unlock(sc);
   1133   1.1    simonb 
   1134  1.12  degroote 	return 0;
   1135   1.1    simonb }
   1136   1.1    simonb 
   1137  1.17  degroote /*
   1138  1.17  degroote  * The firmware boot code is small and is intended to be copied directly into
   1139  1.17  degroote  * the NIC internal memory.
   1140  1.17  degroote  */
   1141  1.17  degroote int
   1142  1.17  degroote wpi_load_microcode(struct wpi_softc *sc, const uint8_t *ucode, int size)
   1143   1.1    simonb {
   1144  1.17  degroote 	int ntries;
   1145   1.1    simonb 
   1146  1.17  degroote 	size /= sizeof (uint32_t);
   1147   1.1    simonb 
   1148  1.17  degroote 	wpi_mem_lock(sc);
   1149  1.12  degroote 
   1150  1.17  degroote 	/* copy microcode image into NIC memory */
   1151  1.17  degroote 	wpi_mem_write_region_4(sc, WPI_MEM_UCODE_BASE,
   1152  1.17  degroote 	    (const uint32_t *)ucode, size);
   1153  1.17  degroote 
   1154  1.17  degroote 	wpi_mem_write(sc, WPI_MEM_UCODE_SRC, 0);
   1155  1.17  degroote 	wpi_mem_write(sc, WPI_MEM_UCODE_DST, WPI_FW_TEXT);
   1156  1.17  degroote 	wpi_mem_write(sc, WPI_MEM_UCODE_SIZE, size);
   1157  1.12  degroote 
   1158  1.17  degroote 	/* run microcode */
   1159  1.17  degroote 	wpi_mem_write(sc, WPI_MEM_UCODE_CTL, WPI_UC_RUN);
   1160  1.12  degroote 
   1161  1.17  degroote 	/* wait for transfer to complete */
   1162  1.17  degroote 	for (ntries = 0; ntries < 1000; ntries++) {
   1163  1.17  degroote 		if (!(wpi_mem_read(sc, WPI_MEM_UCODE_CTL) & WPI_UC_RUN))
   1164  1.12  degroote 			break;
   1165  1.17  degroote 		DELAY(10);
   1166  1.12  degroote 	}
   1167  1.17  degroote 	if (ntries == 1000) {
   1168  1.17  degroote 		wpi_mem_unlock(sc);
   1169  1.28  degroote 		aprint_error_dev(sc->sc_dev, "could not load boot firmware\n");
   1170  1.17  degroote 		return ETIMEDOUT;
   1171  1.12  degroote 	}
   1172  1.17  degroote 	wpi_mem_write(sc, WPI_MEM_UCODE_CTL, WPI_UC_ENABLE);
   1173  1.12  degroote 
   1174  1.17  degroote 	wpi_mem_unlock(sc);
   1175   1.1    simonb 
   1176  1.17  degroote 	return 0;
   1177   1.1    simonb }
   1178   1.1    simonb 
   1179   1.1    simonb static int
   1180  1.41     joerg wpi_cache_firmware(struct wpi_softc *sc)
   1181   1.1    simonb {
   1182  1.54  riastrad 	const char *const fwname = wpi_firmware_name;
   1183  1.12  degroote 	firmware_handle_t fw;
   1184  1.12  degroote 	int error;
   1185   1.1    simonb 
   1186  1.54  riastrad 	/* sc is used here only to report error messages.  */
   1187  1.41     joerg 
   1188  1.41     joerg 	mutex_enter(&wpi_firmware_mutex);
   1189  1.54  riastrad 
   1190  1.54  riastrad 	if (wpi_firmware_users == SIZE_MAX) {
   1191  1.54  riastrad 		mutex_exit(&wpi_firmware_mutex);
   1192  1.54  riastrad 		return ENFILE;	/* Too many of something in the system...  */
   1193  1.54  riastrad 	}
   1194  1.41     joerg 	if (wpi_firmware_users++) {
   1195  1.54  riastrad 		KASSERT(wpi_firmware_image != NULL);
   1196  1.54  riastrad 		KASSERT(wpi_firmware_size > 0);
   1197  1.41     joerg 		mutex_exit(&wpi_firmware_mutex);
   1198  1.54  riastrad 		return 0;	/* Already good to go.  */
   1199  1.41     joerg 	}
   1200  1.41     joerg 
   1201  1.54  riastrad 	KASSERT(wpi_firmware_image == NULL);
   1202  1.54  riastrad 	KASSERT(wpi_firmware_size == 0);
   1203  1.54  riastrad 
   1204  1.12  degroote 	/* load firmware image from disk */
   1205  1.54  riastrad 	if ((error = firmware_open("if_wpi", fwname, &fw)) != 0) {
   1206  1.54  riastrad 		aprint_error_dev(sc->sc_dev,
   1207  1.54  riastrad 		    "could not open firmware file %s: %d\n", fwname, error);
   1208  1.53  riastrad 		goto fail0;
   1209   1.1    simonb 	}
   1210  1.12  degroote 
   1211  1.41     joerg 	wpi_firmware_size = firmware_get_size(fw);
   1212  1.41     joerg 
   1213  1.41     joerg 	if (wpi_firmware_size > sizeof (struct wpi_firmware_hdr) +
   1214  1.41     joerg 	    WPI_FW_MAIN_TEXT_MAXSZ + WPI_FW_MAIN_DATA_MAXSZ +
   1215  1.41     joerg 	    WPI_FW_INIT_TEXT_MAXSZ + WPI_FW_INIT_DATA_MAXSZ +
   1216  1.41     joerg 	    WPI_FW_BOOT_TEXT_MAXSZ) {
   1217  1.54  riastrad 		aprint_error_dev(sc->sc_dev,
   1218  1.54  riastrad 		    "firmware file %s too large: %zu bytes\n",
   1219  1.54  riastrad 		    fwname, wpi_firmware_size);
   1220  1.41     joerg 		error = EFBIG;
   1221  1.41     joerg 		goto fail1;
   1222  1.41     joerg 	}
   1223  1.41     joerg 
   1224  1.41     joerg 	if (wpi_firmware_size < sizeof (struct wpi_firmware_hdr)) {
   1225  1.41     joerg 		aprint_error_dev(sc->sc_dev,
   1226  1.54  riastrad 		    "firmware file %s too small: %zu bytes\n",
   1227  1.54  riastrad 		    fwname, wpi_firmware_size);
   1228  1.12  degroote 		error = EINVAL;
   1229  1.54  riastrad 		goto fail1;
   1230  1.12  degroote 	}
   1231   1.1    simonb 
   1232  1.41     joerg 	wpi_firmware_image = firmware_malloc(wpi_firmware_size);
   1233  1.41     joerg 	if (wpi_firmware_image == NULL) {
   1234  1.54  riastrad 		aprint_error_dev(sc->sc_dev,
   1235  1.54  riastrad 		    "not enough memory for firmware file %s\n", fwname);
   1236  1.41     joerg 		error = ENOMEM;
   1237  1.41     joerg 		goto fail1;
   1238  1.41     joerg 	}
   1239  1.41     joerg 
   1240  1.54  riastrad 	error = firmware_read(fw, 0, wpi_firmware_image, wpi_firmware_size);
   1241  1.54  riastrad 	if (error != 0) {
   1242  1.54  riastrad 		aprint_error_dev(sc->sc_dev,
   1243  1.54  riastrad 		    "error reading firmware file %s: %d\n", fwname, error);
   1244   1.1    simonb 		goto fail2;
   1245   1.1    simonb 	}
   1246   1.1    simonb 
   1247  1.54  riastrad 	/* Success!  */
   1248  1.41     joerg 	firmware_close(fw);
   1249  1.41     joerg 	mutex_exit(&wpi_firmware_mutex);
   1250  1.41     joerg 	return 0;
   1251  1.41     joerg 
   1252  1.41     joerg fail2:
   1253  1.41     joerg 	firmware_free(wpi_firmware_image, wpi_firmware_size);
   1254  1.54  riastrad 	wpi_firmware_image = NULL;
   1255  1.41     joerg fail1:
   1256  1.54  riastrad 	wpi_firmware_size = 0;
   1257  1.41     joerg 	firmware_close(fw);
   1258  1.53  riastrad fail0:
   1259  1.54  riastrad 	KASSERT(wpi_firmware_users == 1);
   1260  1.54  riastrad 	wpi_firmware_users = 0;
   1261  1.54  riastrad 	KASSERT(wpi_firmware_image == NULL);
   1262  1.54  riastrad 	KASSERT(wpi_firmware_size == 0);
   1263  1.54  riastrad 
   1264  1.41     joerg 	mutex_exit(&wpi_firmware_mutex);
   1265  1.41     joerg 	return error;
   1266  1.41     joerg }
   1267  1.41     joerg 
   1268  1.54  riastrad static void
   1269  1.54  riastrad wpi_release_firmware(void)
   1270  1.54  riastrad {
   1271  1.54  riastrad 
   1272  1.54  riastrad 	mutex_enter(&wpi_firmware_mutex);
   1273  1.54  riastrad 
   1274  1.54  riastrad 	KASSERT(wpi_firmware_users > 0);
   1275  1.54  riastrad 	KASSERT(wpi_firmware_image != NULL);
   1276  1.54  riastrad 	KASSERT(wpi_firmware_size != 0);
   1277  1.54  riastrad 
   1278  1.54  riastrad 	if (--wpi_firmware_users == 0) {
   1279  1.54  riastrad 		firmware_free(wpi_firmware_image, wpi_firmware_size);
   1280  1.54  riastrad 		wpi_firmware_image = NULL;
   1281  1.54  riastrad 		wpi_firmware_size = 0;
   1282  1.54  riastrad 	}
   1283  1.54  riastrad 
   1284  1.54  riastrad 	mutex_exit(&wpi_firmware_mutex);
   1285  1.54  riastrad }
   1286  1.54  riastrad 
   1287  1.41     joerg static int
   1288  1.41     joerg wpi_load_firmware(struct wpi_softc *sc)
   1289  1.41     joerg {
   1290  1.41     joerg 	struct wpi_dma_info *dma = &sc->fw_dma;
   1291  1.41     joerg 	struct wpi_firmware_hdr hdr;
   1292  1.41     joerg 	const uint8_t *init_text, *init_data, *main_text, *main_data;
   1293  1.41     joerg 	const uint8_t *boot_text;
   1294  1.41     joerg 	uint32_t init_textsz, init_datasz, main_textsz, main_datasz;
   1295  1.41     joerg 	uint32_t boot_textsz;
   1296  1.54  riastrad 	size_t size;
   1297  1.41     joerg 	int error;
   1298  1.41     joerg 
   1299  1.54  riastrad 	if (!sc->fw_used) {
   1300  1.54  riastrad 		if ((error = wpi_cache_firmware(sc)) != 0)
   1301  1.54  riastrad 			return error;
   1302  1.54  riastrad 		sc->fw_used = true;
   1303  1.54  riastrad 	}
   1304  1.54  riastrad 
   1305  1.54  riastrad 	KASSERT(sc->fw_used);
   1306  1.54  riastrad 	KASSERT(wpi_firmware_image != NULL);
   1307  1.54  riastrad 	KASSERT(wpi_firmware_size > sizeof(hdr));
   1308  1.41     joerg 
   1309  1.41     joerg 	memcpy(&hdr, wpi_firmware_image, sizeof(hdr));
   1310  1.41     joerg 
   1311  1.12  degroote 	main_textsz = le32toh(hdr.main_textsz);
   1312  1.12  degroote 	main_datasz = le32toh(hdr.main_datasz);
   1313  1.17  degroote 	init_textsz = le32toh(hdr.init_textsz);
   1314  1.17  degroote 	init_datasz = le32toh(hdr.init_datasz);
   1315  1.12  degroote 	boot_textsz = le32toh(hdr.boot_textsz);
   1316  1.12  degroote 
   1317  1.17  degroote 	/* sanity-check firmware segments sizes */
   1318  1.17  degroote 	if (main_textsz > WPI_FW_MAIN_TEXT_MAXSZ ||
   1319  1.17  degroote 	    main_datasz > WPI_FW_MAIN_DATA_MAXSZ ||
   1320  1.17  degroote 	    init_textsz > WPI_FW_INIT_TEXT_MAXSZ ||
   1321  1.17  degroote 	    init_datasz > WPI_FW_INIT_DATA_MAXSZ ||
   1322  1.17  degroote 	    boot_textsz > WPI_FW_BOOT_TEXT_MAXSZ ||
   1323  1.17  degroote 	    (boot_textsz & 3) != 0) {
   1324  1.28  degroote 		aprint_error_dev(sc->sc_dev, "invalid firmware header\n");
   1325  1.12  degroote 		error = EINVAL;
   1326  1.41     joerg 		goto free_firmware;
   1327  1.12  degroote 	}
   1328  1.12  degroote 
   1329  1.12  degroote 	/* check that all firmware segments are present */
   1330  1.54  riastrad 	size = sizeof (struct wpi_firmware_hdr) + main_textsz +
   1331  1.54  riastrad 	    main_datasz + init_textsz + init_datasz + boot_textsz;
   1332  1.54  riastrad 	if (wpi_firmware_size < size) {
   1333  1.41     joerg 		aprint_error_dev(sc->sc_dev,
   1334  1.54  riastrad 		    "firmware file truncated: %zu bytes, expected %zu bytes\n",
   1335  1.54  riastrad 		    wpi_firmware_size, size);
   1336  1.12  degroote 		error = EINVAL;
   1337  1.41     joerg 		goto free_firmware;
   1338   1.1    simonb 	}
   1339   1.1    simonb 
   1340  1.12  degroote 	/* get pointers to firmware segments */
   1341  1.41     joerg 	main_text = wpi_firmware_image + sizeof (struct wpi_firmware_hdr);
   1342  1.12  degroote 	main_data = main_text + main_textsz;
   1343  1.17  degroote 	init_text = main_data + main_datasz;
   1344  1.17  degroote 	init_data = init_text + init_textsz;
   1345  1.17  degroote 	boot_text = init_data + init_datasz;
   1346  1.17  degroote 
   1347  1.17  degroote 	/* copy initialization images into pre-allocated DMA-safe memory */
   1348  1.17  degroote 	memcpy(dma->vaddr, init_data, init_datasz);
   1349  1.54  riastrad 	memcpy((char *)dma->vaddr + WPI_FW_INIT_DATA_MAXSZ, init_text,
   1350  1.54  riastrad 	    init_textsz);
   1351  1.17  degroote 
   1352  1.63  jmcneill 	bus_dmamap_sync(dma->tag, dma->map, 0, dma->size, BUS_DMASYNC_PREWRITE);
   1353  1.63  jmcneill 
   1354  1.17  degroote 	/* tell adapter where to find initialization images */
   1355  1.17  degroote 	wpi_mem_lock(sc);
   1356  1.17  degroote 	wpi_mem_write(sc, WPI_MEM_DATA_BASE, dma->paddr);
   1357  1.17  degroote 	wpi_mem_write(sc, WPI_MEM_DATA_SIZE, init_datasz);
   1358  1.17  degroote 	wpi_mem_write(sc, WPI_MEM_TEXT_BASE,
   1359  1.17  degroote 	    dma->paddr + WPI_FW_INIT_DATA_MAXSZ);
   1360  1.17  degroote 	wpi_mem_write(sc, WPI_MEM_TEXT_SIZE, init_textsz);
   1361  1.17  degroote 	wpi_mem_unlock(sc);
   1362   1.1    simonb 
   1363  1.17  degroote 	/* load firmware boot code */
   1364  1.17  degroote 	if ((error = wpi_load_microcode(sc, boot_text, boot_textsz)) != 0) {
   1365  1.28  degroote 		aprint_error_dev(sc->sc_dev, "could not load boot firmware\n");
   1366  1.41     joerg 		return error;
   1367  1.12  degroote 	}
   1368   1.1    simonb 
   1369  1.17  degroote 	/* now press "execute" ;-) */
   1370  1.17  degroote 	WPI_WRITE(sc, WPI_RESET, 0);
   1371  1.17  degroote 
   1372  1.59  jakllsch 	/* wait at most one second for first alive notification */
   1373  1.17  degroote 	if ((error = tsleep(sc, PCATCH, "wpiinit", hz)) != 0) {
   1374  1.17  degroote 		/* this isn't what was supposed to happen.. */
   1375  1.54  riastrad 		aprint_error_dev(sc->sc_dev,
   1376  1.41     joerg 		    "timeout waiting for adapter to initialize\n");
   1377   1.1    simonb 	}
   1378   1.1    simonb 
   1379  1.17  degroote 	/* copy runtime images into pre-allocated DMA-safe memory */
   1380  1.17  degroote 	memcpy(dma->vaddr, main_data, main_datasz);
   1381  1.54  riastrad 	memcpy((char *)dma->vaddr + WPI_FW_MAIN_DATA_MAXSZ, main_text,
   1382  1.54  riastrad 	    main_textsz);
   1383  1.12  degroote 
   1384  1.63  jmcneill 	bus_dmamap_sync(dma->tag, dma->map, 0, dma->size, BUS_DMASYNC_PREWRITE);
   1385  1.63  jmcneill 
   1386  1.17  degroote 	/* tell adapter where to find runtime images */
   1387   1.1    simonb 	wpi_mem_lock(sc);
   1388  1.17  degroote 	wpi_mem_write(sc, WPI_MEM_DATA_BASE, dma->paddr);
   1389  1.17  degroote 	wpi_mem_write(sc, WPI_MEM_DATA_SIZE, main_datasz);
   1390  1.17  degroote 	wpi_mem_write(sc, WPI_MEM_TEXT_BASE,
   1391  1.17  degroote 	    dma->paddr + WPI_FW_MAIN_DATA_MAXSZ);
   1392  1.17  degroote 	wpi_mem_write(sc, WPI_MEM_TEXT_SIZE, WPI_FW_UPDATED | main_textsz);
   1393  1.12  degroote 	wpi_mem_unlock(sc);
   1394  1.12  degroote 
   1395  1.17  degroote 	/* wait at most one second for second alive notification */
   1396  1.12  degroote 	if ((error = tsleep(sc, PCATCH, "wpiinit", hz)) != 0) {
   1397  1.12  degroote 		/* this isn't what was supposed to happen.. */
   1398  1.54  riastrad 		aprint_error_dev(sc->sc_dev,
   1399  1.41     joerg 		    "timeout waiting for adapter to initialize\n");
   1400  1.12  degroote 	}
   1401  1.12  degroote 
   1402  1.41     joerg 	return error;
   1403  1.17  degroote 
   1404  1.41     joerg free_firmware:
   1405  1.41     joerg 	sc->fw_used = false;
   1406  1.54  riastrad 	wpi_release_firmware();
   1407  1.41     joerg 	return error;
   1408  1.12  degroote }
   1409   1.1    simonb 
   1410  1.12  degroote static void
   1411  1.12  degroote wpi_calib_timeout(void *arg)
   1412  1.12  degroote {
   1413  1.12  degroote 	struct wpi_softc *sc = arg;
   1414  1.12  degroote 	struct ieee80211com *ic = &sc->sc_ic;
   1415  1.12  degroote 	int temp, s;
   1416   1.1    simonb 
   1417  1.12  degroote 	/* automatic rate control triggered every 500ms */
   1418  1.12  degroote 	if (ic->ic_fixed_rate == -1) {
   1419  1.12  degroote 		s = splnet();
   1420  1.12  degroote 		if (ic->ic_opmode == IEEE80211_M_STA)
   1421  1.12  degroote 			wpi_iter_func(sc, ic->ic_bss);
   1422  1.12  degroote 		else
   1423  1.59  jakllsch 			ieee80211_iterate_nodes(&ic->ic_sta, wpi_iter_func, sc);
   1424  1.12  degroote 		splx(s);
   1425  1.12  degroote 	}
   1426   1.1    simonb 
   1427  1.12  degroote 	/* update sensor data */
   1428  1.12  degroote 	temp = (int)WPI_READ(sc, WPI_TEMPERATURE);
   1429   1.1    simonb 
   1430  1.12  degroote 	/* automatic power calibration every 60s */
   1431  1.12  degroote 	if (++sc->calib_cnt >= 120) {
   1432  1.12  degroote 		wpi_power_calibration(sc, temp);
   1433  1.12  degroote 		sc->calib_cnt = 0;
   1434   1.1    simonb 	}
   1435  1.12  degroote 
   1436  1.28  degroote 	callout_schedule(&sc->calib_to, hz/2);
   1437  1.12  degroote }
   1438  1.12  degroote 
   1439  1.12  degroote static void
   1440  1.12  degroote wpi_iter_func(void *arg, struct ieee80211_node *ni)
   1441  1.12  degroote {
   1442  1.12  degroote 	struct wpi_softc *sc = arg;
   1443  1.12  degroote 	struct wpi_node *wn = (struct wpi_node *)ni;
   1444  1.12  degroote 
   1445  1.12  degroote 	ieee80211_amrr_choose(&sc->amrr, ni, &wn->amn);
   1446  1.12  degroote }
   1447  1.12  degroote 
   1448  1.12  degroote /*
   1449  1.12  degroote  * This function is called periodically (every 60 seconds) to adjust output
   1450  1.12  degroote  * power to temperature changes.
   1451  1.12  degroote  */
   1452  1.12  degroote void
   1453  1.12  degroote wpi_power_calibration(struct wpi_softc *sc, int temp)
   1454  1.12  degroote {
   1455  1.12  degroote 	/* sanity-check read value */
   1456  1.12  degroote 	if (temp < -260 || temp > 25) {
   1457  1.12  degroote 		/* this can't be correct, ignore */
   1458  1.12  degroote 		DPRINTF(("out-of-range temperature reported: %d\n", temp));
   1459  1.12  degroote 		return;
   1460   1.1    simonb 	}
   1461   1.1    simonb 
   1462  1.12  degroote 	DPRINTF(("temperature %d->%d\n", sc->temp, temp));
   1463  1.12  degroote 
   1464  1.12  degroote 	/* adjust Tx power if need be */
   1465  1.12  degroote 	if (abs(temp - sc->temp) <= 6)
   1466  1.12  degroote 		return;
   1467   1.1    simonb 
   1468  1.12  degroote 	sc->temp = temp;
   1469   1.1    simonb 
   1470  1.66  jmcneill 	if (wpi_set_txpower(sc, sc->sc_ic.ic_curchan, 1) != 0) {
   1471  1.12  degroote 		/* just warn, too bad for the automatic calibration... */
   1472  1.28  degroote 		aprint_error_dev(sc->sc_dev, "could not adjust Tx power\n");
   1473  1.12  degroote 	}
   1474   1.1    simonb }
   1475   1.1    simonb 
   1476   1.1    simonb static void
   1477   1.1    simonb wpi_rx_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc,
   1478  1.61  jakllsch     struct wpi_rx_data *data)
   1479   1.1    simonb {
   1480   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
   1481   1.1    simonb 	struct ifnet *ifp = ic->ic_ifp;
   1482   1.1    simonb 	struct wpi_rx_ring *ring = &sc->rxq;
   1483   1.1    simonb 	struct wpi_rx_stat *stat;
   1484   1.1    simonb 	struct wpi_rx_head *head;
   1485   1.1    simonb 	struct wpi_rx_tail *tail;
   1486   1.7  degroote 	struct wpi_rbuf *rbuf;
   1487   1.1    simonb 	struct ieee80211_frame *wh;
   1488   1.1    simonb 	struct ieee80211_node *ni;
   1489   1.1    simonb 	struct mbuf *m, *mnew;
   1490  1.77    nonaka 	int data_off, error, s;
   1491   1.1    simonb 
   1492  1.64  jmcneill 	bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize,
   1493  1.64  jmcneill 	    BUS_DMASYNC_POSTREAD);
   1494   1.1    simonb 	stat = (struct wpi_rx_stat *)(desc + 1);
   1495   1.1    simonb 
   1496   1.1    simonb 	if (stat->len > WPI_STAT_MAXLEN) {
   1497  1.28  degroote 		aprint_error_dev(sc->sc_dev, "invalid rx statistic header\n");
   1498  1.88   thorpej 		if_statinc(ifp, if_ierrors);
   1499   1.1    simonb 		return;
   1500   1.1    simonb 	}
   1501   1.1    simonb 
   1502   1.9  christos 	head = (struct wpi_rx_head *)((char *)(stat + 1) + stat->len);
   1503   1.9  christos 	tail = (struct wpi_rx_tail *)((char *)(head + 1) + le16toh(head->len));
   1504   1.1    simonb 
   1505   1.1    simonb 	DPRINTFN(4, ("rx intr: idx=%d len=%d stat len=%d rssi=%d rate=%x "
   1506  1.61  jakllsch 	    "chan=%d tstamp=%" PRIu64 "\n", ring->cur, le32toh(desc->len),
   1507  1.61  jakllsch 	    le16toh(head->len), (int8_t)stat->rssi, head->rate, head->chan,
   1508  1.61  jakllsch 	    le64toh(tail->tstamp)));
   1509   1.1    simonb 
   1510   1.1    simonb 	/*
   1511   1.1    simonb 	 * Discard Rx frames with bad CRC early (XXX we may want to pass them
   1512   1.1    simonb 	 * to radiotap in monitor mode).
   1513   1.1    simonb 	 */
   1514   1.1    simonb 	if ((le32toh(tail->flags) & WPI_RX_NOERROR) != WPI_RX_NOERROR) {
   1515  1.61  jakllsch 		DPRINTF(("rx tail flags error %x\n",
   1516  1.61  jakllsch 		    le32toh(tail->flags)));
   1517  1.88   thorpej 		if_statinc(ifp, if_ierrors);
   1518   1.1    simonb 		return;
   1519   1.1    simonb 	}
   1520   1.1    simonb 
   1521  1.19  degroote 	/* Compute where are the useful datas */
   1522  1.55  christos 	data_off = (char*)(head + 1) - mtod(data->m, char*);
   1523  1.55  christos 
   1524  1.65  jmcneill 	MGETHDR(mnew, M_DONTWAIT, MT_DATA);
   1525  1.65  jmcneill 	if (mnew == NULL) {
   1526  1.88   thorpej 		if_statinc(ifp, if_ierrors);
   1527  1.65  jmcneill 		return;
   1528  1.65  jmcneill 	}
   1529  1.19  degroote 
   1530  1.65  jmcneill 	rbuf = wpi_alloc_rbuf(sc);
   1531  1.65  jmcneill 	if (rbuf == NULL) {
   1532  1.65  jmcneill 		m_freem(mnew);
   1533  1.88   thorpej 		if_statinc(ifp, if_ierrors);
   1534  1.65  jmcneill 		return;
   1535  1.65  jmcneill 	}
   1536  1.19  degroote 
   1537  1.65  jmcneill 	/* attach Rx buffer to mbuf */
   1538  1.65  jmcneill 	MEXTADD(mnew, rbuf->vaddr, WPI_RBUF_SIZE, 0, wpi_free_rbuf,
   1539  1.65  jmcneill 		rbuf);
   1540  1.65  jmcneill 	mnew->m_flags |= M_EXT_RW;
   1541  1.19  degroote 
   1542  1.65  jmcneill 	bus_dmamap_unload(sc->sc_dmat, data->map);
   1543  1.10  degroote 
   1544  1.65  jmcneill 	error = bus_dmamap_load(sc->sc_dmat, data->map,
   1545  1.65  jmcneill 	    mtod(mnew, void *), WPI_RBUF_SIZE, NULL,
   1546  1.65  jmcneill 	    BUS_DMA_NOWAIT | BUS_DMA_READ);
   1547  1.65  jmcneill 	if (error) {
   1548  1.65  jmcneill 		device_printf(sc->sc_dev,
   1549  1.65  jmcneill 		    "couldn't load rx mbuf: %d\n", error);
   1550  1.65  jmcneill 		m_freem(mnew);
   1551  1.88   thorpej 		if_statinc(ifp, if_ierrors);
   1552   1.1    simonb 
   1553  1.64  jmcneill 		error = bus_dmamap_load(sc->sc_dmat, data->map,
   1554  1.64  jmcneill 		    mtod(data->m, void *), WPI_RBUF_SIZE, NULL,
   1555  1.64  jmcneill 		    BUS_DMA_NOWAIT | BUS_DMA_READ);
   1556  1.65  jmcneill 		if (error)
   1557  1.64  jmcneill 			panic("%s: bus_dmamap_load failed: %d\n",
   1558  1.64  jmcneill 			    device_xname(sc->sc_dev), error);
   1559  1.65  jmcneill 		return;
   1560  1.65  jmcneill 	}
   1561  1.65  jmcneill 
   1562  1.65  jmcneill 	/* new mbuf loaded successfully */
   1563  1.65  jmcneill 	m = data->m;
   1564  1.65  jmcneill 	data->m = mnew;
   1565  1.64  jmcneill 
   1566  1.65  jmcneill 	/* update Rx descriptor */
   1567  1.65  jmcneill 	ring->desc[ring->cur] = htole32(rbuf->paddr);
   1568  1.65  jmcneill 	bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map, 0,
   1569  1.65  jmcneill 	    ring->desc_dma.size,
   1570  1.65  jmcneill 	    BUS_DMASYNC_PREWRITE);
   1571  1.19  degroote 
   1572  1.65  jmcneill 	m->m_data = (char*)m->m_data + data_off;
   1573  1.65  jmcneill 	m->m_pkthdr.len = m->m_len = le16toh(head->len);
   1574   1.1    simonb 
   1575   1.1    simonb 	/* finalize mbuf */
   1576  1.74     ozaki 	m_set_rcvif(m, ifp);
   1577   1.1    simonb 
   1578  1.77    nonaka 	s = splnet();
   1579  1.77    nonaka 
   1580   1.1    simonb 	if (sc->sc_drvbpf != NULL) {
   1581   1.1    simonb 		struct wpi_rx_radiotap_header *tap = &sc->sc_rxtap;
   1582   1.1    simonb 
   1583   1.1    simonb 		tap->wr_flags = 0;
   1584   1.1    simonb 		tap->wr_chan_freq =
   1585  1.61  jakllsch 		    htole16(ic->ic_channels[head->chan].ic_freq);
   1586   1.1    simonb 		tap->wr_chan_flags =
   1587  1.61  jakllsch 		    htole16(ic->ic_channels[head->chan].ic_flags);
   1588   1.1    simonb 		tap->wr_dbm_antsignal = (int8_t)(stat->rssi - WPI_RSSI_OFFSET);
   1589   1.1    simonb 		tap->wr_dbm_antnoise = (int8_t)le16toh(stat->noise);
   1590   1.1    simonb 		tap->wr_tsft = tail->tstamp;
   1591   1.1    simonb 		tap->wr_antenna = (le16toh(head->flags) >> 4) & 0xf;
   1592   1.1    simonb 		switch (head->rate) {
   1593   1.1    simonb 		/* CCK rates */
   1594   1.1    simonb 		case  10: tap->wr_rate =   2; break;
   1595   1.1    simonb 		case  20: tap->wr_rate =   4; break;
   1596   1.1    simonb 		case  55: tap->wr_rate =  11; break;
   1597   1.1    simonb 		case 110: tap->wr_rate =  22; break;
   1598   1.1    simonb 		/* OFDM rates */
   1599   1.1    simonb 		case 0xd: tap->wr_rate =  12; break;
   1600   1.1    simonb 		case 0xf: tap->wr_rate =  18; break;
   1601   1.1    simonb 		case 0x5: tap->wr_rate =  24; break;
   1602   1.1    simonb 		case 0x7: tap->wr_rate =  36; break;
   1603   1.1    simonb 		case 0x9: tap->wr_rate =  48; break;
   1604   1.1    simonb 		case 0xb: tap->wr_rate =  72; break;
   1605   1.1    simonb 		case 0x1: tap->wr_rate =  96; break;
   1606   1.1    simonb 		case 0x3: tap->wr_rate = 108; break;
   1607   1.1    simonb 		/* unknown rate: should not happen */
   1608   1.1    simonb 		default:  tap->wr_rate =   0;
   1609   1.1    simonb 		}
   1610   1.1    simonb 		if (le16toh(head->flags) & 0x4)
   1611   1.1    simonb 			tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
   1612   1.1    simonb 
   1613  1.80   msaitoh 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m, BPF_D_IN);
   1614   1.1    simonb 	}
   1615   1.1    simonb 
   1616   1.1    simonb 	/* grab a reference to the source node */
   1617   1.1    simonb 	wh = mtod(m, struct ieee80211_frame *);
   1618   1.1    simonb 	ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
   1619   1.1    simonb 
   1620   1.1    simonb 	/* send the frame to the 802.11 layer */
   1621   1.1    simonb 	ieee80211_input(ic, m, ni, stat->rssi, 0);
   1622   1.1    simonb 
   1623   1.1    simonb 	/* release node reference */
   1624   1.1    simonb 	ieee80211_free_node(ni);
   1625  1.77    nonaka 
   1626  1.77    nonaka 	splx(s);
   1627   1.1    simonb }
   1628   1.1    simonb 
   1629   1.1    simonb static void
   1630   1.1    simonb wpi_tx_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc)
   1631   1.1    simonb {
   1632   1.1    simonb 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
   1633   1.1    simonb 	struct wpi_tx_ring *ring = &sc->txq[desc->qid & 0x3];
   1634  1.61  jakllsch 	struct wpi_tx_data *data = &ring->data[desc->idx];
   1635   1.1    simonb 	struct wpi_tx_stat *stat = (struct wpi_tx_stat *)(desc + 1);
   1636  1.61  jakllsch 	struct wpi_node *wn = (struct wpi_node *)data->ni;
   1637  1.77    nonaka 	int s;
   1638   1.1    simonb 
   1639   1.1    simonb 	DPRINTFN(4, ("tx done: qid=%d idx=%d retries=%d nkill=%d rate=%x "
   1640  1.61  jakllsch 	    "duration=%d status=%x\n", desc->qid, desc->idx, stat->ntries,
   1641  1.61  jakllsch 	    stat->nkill, stat->rate, le32toh(stat->duration),
   1642  1.61  jakllsch 	    le32toh(stat->status)));
   1643   1.1    simonb 
   1644  1.77    nonaka 	s = splnet();
   1645  1.77    nonaka 
   1646   1.1    simonb 	/*
   1647   1.1    simonb 	 * Update rate control statistics for the node.
   1648   1.1    simonb 	 * XXX we should not count mgmt frames since they're always sent at
   1649   1.1    simonb 	 * the lowest available bit-rate.
   1650   1.1    simonb 	 */
   1651   1.5     joerg 	wn->amn.amn_txcnt++;
   1652   1.1    simonb 	if (stat->ntries > 0) {
   1653   1.1    simonb 		DPRINTFN(3, ("tx intr ntries %d\n", stat->ntries));
   1654   1.5     joerg 		wn->amn.amn_retrycnt++;
   1655   1.1    simonb 	}
   1656   1.1    simonb 
   1657   1.2     oster 	if ((le32toh(stat->status) & 0xff) != 1)
   1658  1.88   thorpej 		if_statinc(ifp, if_oerrors);
   1659   1.2     oster 	else
   1660  1.88   thorpej 		if_statinc(ifp, if_opackets);
   1661   1.2     oster 
   1662  1.61  jakllsch 	bus_dmamap_unload(sc->sc_dmat, data->map);
   1663  1.61  jakllsch 	m_freem(data->m);
   1664  1.61  jakllsch 	data->m = NULL;
   1665  1.61  jakllsch 	ieee80211_free_node(data->ni);
   1666  1.61  jakllsch 	data->ni = NULL;
   1667   1.1    simonb 
   1668   1.1    simonb 	ring->queued--;
   1669   1.1    simonb 
   1670   1.1    simonb 	sc->sc_tx_timer = 0;
   1671   1.1    simonb 	ifp->if_flags &= ~IFF_OACTIVE;
   1672  1.78     ozaki 	wpi_start(ifp); /* in softint */
   1673  1.77    nonaka 
   1674  1.77    nonaka 	splx(s);
   1675   1.1    simonb }
   1676   1.1    simonb 
   1677   1.1    simonb static void
   1678   1.1    simonb wpi_cmd_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc)
   1679   1.1    simonb {
   1680   1.1    simonb 	struct wpi_tx_ring *ring = &sc->cmdq;
   1681   1.1    simonb 	struct wpi_tx_data *data;
   1682   1.1    simonb 
   1683   1.1    simonb 	if ((desc->qid & 7) != 4)
   1684   1.1    simonb 		return;	/* not a command ack */
   1685   1.1    simonb 
   1686   1.1    simonb 	data = &ring->data[desc->idx];
   1687   1.1    simonb 
   1688   1.1    simonb 	/* if the command was mapped in a mbuf, free it */
   1689   1.1    simonb 	if (data->m != NULL) {
   1690   1.1    simonb 		bus_dmamap_unload(sc->sc_dmat, data->map);
   1691   1.1    simonb 		m_freem(data->m);
   1692   1.1    simonb 		data->m = NULL;
   1693   1.1    simonb 	}
   1694   1.1    simonb 
   1695   1.1    simonb 	wakeup(&ring->cmd[desc->idx]);
   1696   1.1    simonb }
   1697   1.1    simonb 
   1698   1.1    simonb static void
   1699   1.1    simonb wpi_notif_intr(struct wpi_softc *sc)
   1700   1.1    simonb {
   1701   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
   1702   1.7  degroote 	struct ifnet *ifp =  ic->ic_ifp;
   1703   1.1    simonb 	uint32_t hw;
   1704  1.77    nonaka 	int s;
   1705   1.1    simonb 
   1706  1.64  jmcneill 	bus_dmamap_sync(sc->sc_dmat, sc->shared_dma.map, 0,
   1707  1.64  jmcneill 	    sizeof(struct wpi_shared), BUS_DMASYNC_POSTREAD);
   1708  1.64  jmcneill 
   1709   1.1    simonb 	hw = le32toh(sc->shared->next);
   1710   1.1    simonb 	while (sc->rxq.cur != hw) {
   1711  1.61  jakllsch 		struct wpi_rx_data *data = &sc->rxq.data[sc->rxq.cur];
   1712  1.65  jmcneill 		struct wpi_rx_desc *desc;
   1713   1.1    simonb 
   1714  1.64  jmcneill 		bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize,
   1715  1.64  jmcneill 		    BUS_DMASYNC_POSTREAD);
   1716  1.65  jmcneill 		desc = mtod(data->m, struct wpi_rx_desc *);
   1717  1.64  jmcneill 
   1718   1.1    simonb 		DPRINTFN(4, ("rx notification qid=%x idx=%d flags=%x type=%d "
   1719  1.61  jakllsch 		    "len=%d\n", desc->qid, desc->idx, desc->flags,
   1720  1.61  jakllsch 		    desc->type, le32toh(desc->len)));
   1721   1.1    simonb 
   1722   1.1    simonb 		if (!(desc->qid & 0x80))	/* reply to a command */
   1723   1.1    simonb 			wpi_cmd_intr(sc, desc);
   1724   1.1    simonb 
   1725   1.1    simonb 		switch (desc->type) {
   1726   1.1    simonb 		case WPI_RX_DONE:
   1727   1.1    simonb 			/* a 802.11 frame was received */
   1728   1.1    simonb 			wpi_rx_intr(sc, desc, data);
   1729   1.1    simonb 			break;
   1730   1.1    simonb 
   1731   1.1    simonb 		case WPI_TX_DONE:
   1732   1.1    simonb 			/* a 802.11 frame has been transmitted */
   1733   1.1    simonb 			wpi_tx_intr(sc, desc);
   1734   1.1    simonb 			break;
   1735   1.1    simonb 
   1736   1.1    simonb 		case WPI_UC_READY:
   1737   1.1    simonb 		{
   1738   1.1    simonb 			struct wpi_ucode_info *uc =
   1739  1.59  jakllsch 			    (struct wpi_ucode_info *)(desc + 1);
   1740   1.1    simonb 
   1741   1.1    simonb 			/* the microcontroller is ready */
   1742   1.1    simonb 			DPRINTF(("microcode alive notification version %x "
   1743  1.61  jakllsch 			    "alive %x\n", le32toh(uc->version),
   1744  1.61  jakllsch 			    le32toh(uc->valid)));
   1745   1.1    simonb 
   1746   1.1    simonb 			if (le32toh(uc->valid) != 1) {
   1747  1.55  christos 				aprint_error_dev(sc->sc_dev,
   1748  1.61  jakllsch 				    "microcontroller initialization failed\n");
   1749   1.1    simonb 			}
   1750   1.1    simonb 			break;
   1751   1.1    simonb 		}
   1752   1.1    simonb 		case WPI_STATE_CHANGED:
   1753   1.1    simonb 		{
   1754   1.1    simonb 			uint32_t *status = (uint32_t *)(desc + 1);
   1755   1.1    simonb 
   1756   1.1    simonb 			/* enabled/disabled notification */
   1757   1.1    simonb 			DPRINTF(("state changed to %x\n", le32toh(*status)));
   1758   1.1    simonb 
   1759   1.1    simonb 			if (le32toh(*status) & 1) {
   1760  1.77    nonaka 				s = splnet();
   1761   1.1    simonb 				/* the radio button has to be pushed */
   1762  1.70    bouyer 				/* wake up thread to signal powerd */
   1763  1.70    bouyer 				cv_signal(&sc->sc_rsw_cv);
   1764  1.61  jakllsch 				aprint_error_dev(sc->sc_dev,
   1765  1.61  jakllsch 				    "Radio transmitter is off\n");
   1766   1.7  degroote 				/* turn the interface down */
   1767   1.7  degroote 				ifp->if_flags &= ~IFF_UP;
   1768  1.90  christos 				wpi_stop_intr(ifp, 1);
   1769  1.77    nonaka 				splx(s);
   1770   1.7  degroote 				return;	/* no further processing */
   1771   1.1    simonb 			}
   1772   1.1    simonb 			break;
   1773   1.1    simonb 		}
   1774   1.1    simonb 		case WPI_START_SCAN:
   1775   1.1    simonb 		{
   1776  1.66  jmcneill #if 0
   1777   1.1    simonb 			struct wpi_start_scan *scan =
   1778  1.59  jakllsch 			    (struct wpi_start_scan *)(desc + 1);
   1779   1.1    simonb 
   1780   1.1    simonb 			DPRINTFN(2, ("scanning channel %d status %x\n",
   1781  1.61  jakllsch 			    scan->chan, le32toh(scan->status)));
   1782   1.1    simonb 
   1783   1.1    simonb 			/* fix current channel */
   1784  1.67  jmcneill 			ic->ic_curchan = &ic->ic_channels[scan->chan];
   1785  1.66  jmcneill #endif
   1786   1.1    simonb 			break;
   1787   1.1    simonb 		}
   1788   1.1    simonb 		case WPI_STOP_SCAN:
   1789   1.1    simonb 		{
   1790  1.67  jmcneill #ifdef WPI_DEBUG
   1791   1.1    simonb 			struct wpi_stop_scan *scan =
   1792  1.59  jakllsch 			    (struct wpi_stop_scan *)(desc + 1);
   1793  1.67  jmcneill #endif
   1794   1.1    simonb 
   1795   1.1    simonb 			DPRINTF(("scan finished nchan=%d status=%d chan=%d\n",
   1796  1.61  jakllsch 			    scan->nchan, scan->status, scan->chan));
   1797   1.1    simonb 
   1798  1.77    nonaka 			s = splnet();
   1799  1.23  degroote 			sc->is_scanning = false;
   1800  1.67  jmcneill 			if (ic->ic_state == IEEE80211_S_SCAN)
   1801  1.67  jmcneill 				ieee80211_next_scan(ic);
   1802  1.77    nonaka 			splx(s);
   1803   1.1    simonb 			break;
   1804   1.1    simonb 		}
   1805   1.1    simonb 		}
   1806   1.1    simonb 
   1807   1.1    simonb 		sc->rxq.cur = (sc->rxq.cur + 1) % WPI_RX_RING_COUNT;
   1808   1.1    simonb 	}
   1809   1.1    simonb 
   1810   1.1    simonb 	/* tell the firmware what we have processed */
   1811   1.1    simonb 	hw = (hw == 0) ? WPI_RX_RING_COUNT - 1 : hw - 1;
   1812   1.1    simonb 	WPI_WRITE(sc, WPI_RX_WIDX, hw & ~7);
   1813   1.1    simonb }
   1814   1.1    simonb 
   1815   1.1    simonb static int
   1816   1.1    simonb wpi_intr(void *arg)
   1817   1.1    simonb {
   1818   1.1    simonb 	struct wpi_softc *sc = arg;
   1819   1.1    simonb 	uint32_t r;
   1820   1.1    simonb 
   1821   1.1    simonb 	r = WPI_READ(sc, WPI_INTR);
   1822   1.1    simonb 	if (r == 0 || r == 0xffffffff)
   1823   1.1    simonb 		return 0;	/* not for us */
   1824   1.1    simonb 
   1825  1.61  jakllsch 	DPRINTFN(6, ("interrupt reg %x\n", r));
   1826   1.1    simonb 
   1827   1.1    simonb 	/* disable interrupts */
   1828   1.1    simonb 	WPI_WRITE(sc, WPI_MASK, 0);
   1829  1.77    nonaka 
   1830  1.77    nonaka 	softint_schedule(sc->sc_soft_ih);
   1831  1.77    nonaka 	return 1;
   1832  1.77    nonaka }
   1833  1.77    nonaka 
   1834  1.77    nonaka static void
   1835  1.77    nonaka wpi_softintr(void *arg)
   1836  1.77    nonaka {
   1837  1.77    nonaka 	struct wpi_softc *sc = arg;
   1838  1.77    nonaka 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
   1839  1.77    nonaka 	uint32_t r;
   1840  1.77    nonaka 
   1841  1.77    nonaka 	r = WPI_READ(sc, WPI_INTR);
   1842  1.77    nonaka 	if (r == 0 || r == 0xffffffff)
   1843  1.77    nonaka 		goto out;
   1844  1.77    nonaka 
   1845   1.1    simonb 	/* ack interrupts */
   1846   1.1    simonb 	WPI_WRITE(sc, WPI_INTR, r);
   1847   1.1    simonb 
   1848   1.1    simonb 	if (r & (WPI_SW_ERROR | WPI_HW_ERROR)) {
   1849  1.61  jakllsch 		/* SYSTEM FAILURE, SYSTEM FAILURE */
   1850  1.28  degroote 		aprint_error_dev(sc->sc_dev, "fatal firmware error\n");
   1851  1.61  jakllsch 		ifp->if_flags &= ~IFF_UP;
   1852  1.90  christos 		wpi_stop_intr(ifp, 1);
   1853  1.77    nonaka 		return;
   1854   1.1    simonb 	}
   1855   1.1    simonb 
   1856   1.1    simonb 	if (r & WPI_RX_INTR)
   1857   1.1    simonb 		wpi_notif_intr(sc);
   1858   1.1    simonb 
   1859   1.1    simonb 	if (r & WPI_ALIVE_INTR)	/* firmware initialized */
   1860   1.1    simonb 		wakeup(sc);
   1861   1.1    simonb 
   1862  1.77    nonaka  out:
   1863   1.1    simonb 	/* re-enable interrupts */
   1864   1.7  degroote 	if (ifp->if_flags & IFF_UP)
   1865   1.7  degroote 		WPI_WRITE(sc, WPI_MASK, WPI_INTR_MASK);
   1866   1.1    simonb }
   1867   1.1    simonb 
   1868   1.1    simonb static uint8_t
   1869   1.1    simonb wpi_plcp_signal(int rate)
   1870   1.1    simonb {
   1871   1.1    simonb 	switch (rate) {
   1872   1.1    simonb 	/* CCK rates (returned values are device-dependent) */
   1873   1.1    simonb 	case 2:		return 10;
   1874   1.1    simonb 	case 4:		return 20;
   1875   1.1    simonb 	case 11:	return 55;
   1876   1.1    simonb 	case 22:	return 110;
   1877   1.1    simonb 
   1878   1.1    simonb 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
   1879   1.1    simonb 	/* R1-R4, (u)ral is R4-R1 */
   1880   1.1    simonb 	case 12:	return 0xd;
   1881   1.1    simonb 	case 18:	return 0xf;
   1882   1.1    simonb 	case 24:	return 0x5;
   1883   1.1    simonb 	case 36:	return 0x7;
   1884   1.1    simonb 	case 48:	return 0x9;
   1885   1.1    simonb 	case 72:	return 0xb;
   1886   1.1    simonb 	case 96:	return 0x1;
   1887   1.1    simonb 	case 108:	return 0x3;
   1888   1.1    simonb 
   1889   1.1    simonb 	/* unsupported rates (should not get there) */
   1890   1.1    simonb 	default:	return 0;
   1891   1.1    simonb 	}
   1892   1.1    simonb }
   1893   1.1    simonb 
   1894   1.1    simonb /* quickly determine if a given rate is CCK or OFDM */
   1895   1.1    simonb #define WPI_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
   1896   1.1    simonb 
   1897   1.1    simonb static int
   1898   1.1    simonb wpi_tx_data(struct wpi_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
   1899  1.61  jakllsch     int ac)
   1900   1.1    simonb {
   1901   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
   1902   1.1    simonb 	struct wpi_tx_ring *ring = &sc->txq[ac];
   1903   1.1    simonb 	struct wpi_tx_desc *desc;
   1904   1.1    simonb 	struct wpi_tx_data *data;
   1905   1.1    simonb 	struct wpi_tx_cmd *cmd;
   1906   1.1    simonb 	struct wpi_cmd_data *tx;
   1907   1.1    simonb 	struct ieee80211_frame *wh;
   1908   1.1    simonb 	struct ieee80211_key *k;
   1909   1.1    simonb 	const struct chanAccParams *cap;
   1910   1.1    simonb 	struct mbuf *mnew;
   1911  1.61  jakllsch 	int i, rate, error, hdrlen, noack = 0;
   1912   1.1    simonb 
   1913   1.1    simonb 	desc = &ring->desc[ring->cur];
   1914   1.1    simonb 	data = &ring->data[ring->cur];
   1915   1.1    simonb 
   1916   1.1    simonb 	wh = mtod(m0, struct ieee80211_frame *);
   1917   1.1    simonb 
   1918  1.56  christos 	if (ieee80211_has_qos(wh)) {
   1919   1.1    simonb 		cap = &ic->ic_wme.wme_chanParams;
   1920   1.1    simonb 		noack = cap->cap_wmeParams[ac].wmep_noackPolicy;
   1921  1.26  degroote 	}
   1922   1.1    simonb 
   1923   1.1    simonb 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
   1924   1.1    simonb 		k = ieee80211_crypto_encap(ic, ni, m0);
   1925   1.1    simonb 		if (k == NULL) {
   1926   1.1    simonb 			m_freem(m0);
   1927   1.1    simonb 			return ENOBUFS;
   1928   1.1    simonb 		}
   1929   1.1    simonb 
   1930   1.1    simonb 		/* packet header may have moved, reset our local pointer */
   1931   1.1    simonb 		wh = mtod(m0, struct ieee80211_frame *);
   1932   1.1    simonb 	}
   1933   1.1    simonb 
   1934  1.26  degroote 	hdrlen = ieee80211_anyhdrsize(wh);
   1935  1.26  degroote 
   1936   1.1    simonb 	/* pickup a rate */
   1937   1.1    simonb 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
   1938  1.61  jakllsch 	    IEEE80211_FC0_TYPE_MGT) {
   1939   1.1    simonb 		/* mgmt frames are sent at the lowest available bit-rate */
   1940   1.1    simonb 		rate = ni->ni_rates.rs_rates[0];
   1941   1.1    simonb 	} else {
   1942   1.1    simonb 		if (ic->ic_fixed_rate != -1) {
   1943   1.1    simonb 			rate = ic->ic_sup_rates[ic->ic_curmode].
   1944  1.61  jakllsch 			    rs_rates[ic->ic_fixed_rate];
   1945   1.1    simonb 		} else
   1946   1.1    simonb 			rate = ni->ni_rates.rs_rates[ni->ni_txrate];
   1947   1.1    simonb 	}
   1948   1.1    simonb 	rate &= IEEE80211_RATE_VAL;
   1949   1.1    simonb 
   1950   1.1    simonb 	if (sc->sc_drvbpf != NULL) {
   1951   1.1    simonb 		struct wpi_tx_radiotap_header *tap = &sc->sc_txtap;
   1952   1.1    simonb 
   1953   1.1    simonb 		tap->wt_flags = 0;
   1954   1.1    simonb 		tap->wt_chan_freq = htole16(ni->ni_chan->ic_freq);
   1955   1.1    simonb 		tap->wt_chan_flags = htole16(ni->ni_chan->ic_flags);
   1956   1.1    simonb 		tap->wt_rate = rate;
   1957   1.1    simonb 		tap->wt_hwqueue = ac;
   1958   1.1    simonb 		if (wh->i_fc[1] & IEEE80211_FC1_WEP)
   1959   1.1    simonb 			tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
   1960   1.1    simonb 
   1961  1.80   msaitoh 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0, BPF_D_OUT);
   1962   1.1    simonb 	}
   1963   1.1    simonb 
   1964   1.1    simonb 	cmd = &ring->cmd[ring->cur];
   1965   1.1    simonb 	cmd->code = WPI_CMD_TX_DATA;
   1966   1.1    simonb 	cmd->flags = 0;
   1967   1.1    simonb 	cmd->qid = ring->qid;
   1968   1.1    simonb 	cmd->idx = ring->cur;
   1969   1.1    simonb 
   1970   1.1    simonb 	tx = (struct wpi_cmd_data *)cmd->data;
   1971  1.61  jakllsch 	/* no need to zero tx, all fields are reinitialized here */
   1972   1.1    simonb 	tx->flags = 0;
   1973   1.1    simonb 
   1974   1.1    simonb 	if (!noack && !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
   1975   1.1    simonb 		tx->flags |= htole32(WPI_TX_NEED_ACK);
   1976   1.7  degroote 	} else if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > ic->ic_rtsthreshold)
   1977   1.7  degroote 		tx->flags |= htole32(WPI_TX_NEED_RTS | WPI_TX_FULL_TXOP);
   1978   1.1    simonb 
   1979   1.1    simonb 	tx->flags |= htole32(WPI_TX_AUTO_SEQ);
   1980   1.1    simonb 
   1981   1.7  degroote 	/* retrieve destination node's id */
   1982   1.7  degroote 	tx->id = IEEE80211_IS_MULTICAST(wh->i_addr1) ? WPI_ID_BROADCAST :
   1983   1.7  degroote 		WPI_ID_BSS;
   1984   1.7  degroote 
   1985   1.1    simonb 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
   1986  1.61  jakllsch 	    IEEE80211_FC0_TYPE_MGT) {
   1987   1.1    simonb 		/* tell h/w to set timestamp in probe responses */
   1988   1.1    simonb 		if ((wh->i_fc[0] &
   1989   1.1    simonb 		    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
   1990   1.1    simonb 		    (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
   1991   1.1    simonb 			tx->flags |= htole32(WPI_TX_INSERT_TSTAMP);
   1992   1.1    simonb 
   1993   1.1    simonb 		if (((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
   1994   1.1    simonb 			 IEEE80211_FC0_SUBTYPE_ASSOC_REQ) ||
   1995   1.1    simonb 			((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
   1996   1.1    simonb 			 IEEE80211_FC0_SUBTYPE_REASSOC_REQ))
   1997   1.1    simonb 			tx->timeout = htole16(3);
   1998   1.1    simonb 		else
   1999   1.1    simonb 			tx->timeout = htole16(2);
   2000   1.1    simonb 	} else
   2001   1.1    simonb 		tx->timeout = htole16(0);
   2002   1.1    simonb 
   2003   1.1    simonb 	tx->rate = wpi_plcp_signal(rate);
   2004   1.1    simonb 
   2005  1.92   msaitoh 	/* be very persistent at sending frames out */
   2006   1.1    simonb 	tx->rts_ntries = 7;
   2007   1.1    simonb 	tx->data_ntries = 15;
   2008   1.1    simonb 
   2009   1.1    simonb 	tx->ofdm_mask = 0xff;
   2010  1.61  jakllsch 	tx->cck_mask = 0x0f;
   2011  1.12  degroote 	tx->lifetime = htole32(WPI_LIFETIME_INFINITE);
   2012   1.1    simonb 
   2013   1.1    simonb 	tx->len = htole16(m0->m_pkthdr.len);
   2014   1.1    simonb 
   2015   1.1    simonb 	/* save and trim IEEE802.11 header */
   2016  1.26  degroote 	memcpy((uint8_t *)(tx + 1), wh, hdrlen);
   2017   1.1    simonb 	m_adj(m0, hdrlen);
   2018   1.1    simonb 
   2019   1.1    simonb 	error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
   2020  1.61  jakllsch 	    BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   2021   1.1    simonb 	if (error != 0 && error != EFBIG) {
   2022  1.61  jakllsch 		aprint_error_dev(sc->sc_dev, "could not map mbuf (error %d)\n",
   2023  1.61  jakllsch 		    error);
   2024   1.1    simonb 		m_freem(m0);
   2025   1.1    simonb 		return error;
   2026   1.1    simonb 	}
   2027   1.1    simonb 	if (error != 0) {
   2028   1.1    simonb 		/* too many fragments, linearize */
   2029  1.61  jakllsch 
   2030   1.1    simonb 		MGETHDR(mnew, M_DONTWAIT, MT_DATA);
   2031   1.1    simonb 		if (mnew == NULL) {
   2032   1.1    simonb 			m_freem(m0);
   2033   1.1    simonb 			return ENOMEM;
   2034   1.1    simonb 		}
   2035  1.85      maxv 		m_copy_pkthdr(mnew, m0);
   2036   1.1    simonb 		if (m0->m_pkthdr.len > MHLEN) {
   2037   1.1    simonb 			MCLGET(mnew, M_DONTWAIT);
   2038   1.1    simonb 			if (!(mnew->m_flags & M_EXT)) {
   2039   1.1    simonb 				m_freem(m0);
   2040   1.1    simonb 				m_freem(mnew);
   2041   1.1    simonb 				return ENOMEM;
   2042   1.1    simonb 			}
   2043   1.1    simonb 		}
   2044   1.1    simonb 
   2045   1.9  christos 		m_copydata(m0, 0, m0->m_pkthdr.len, mtod(mnew, void *));
   2046   1.1    simonb 		m_freem(m0);
   2047   1.1    simonb 		mnew->m_len = mnew->m_pkthdr.len;
   2048   1.1    simonb 		m0 = mnew;
   2049   1.1    simonb 
   2050   1.1    simonb 		error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
   2051  1.61  jakllsch 		    BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   2052   1.1    simonb 		if (error != 0) {
   2053  1.61  jakllsch 			aprint_error_dev(sc->sc_dev,
   2054  1.61  jakllsch 			    "could not map mbuf (error %d)\n", error);
   2055   1.1    simonb 			m_freem(m0);
   2056   1.1    simonb 			return error;
   2057   1.1    simonb 		}
   2058   1.1    simonb 	}
   2059   1.1    simonb 
   2060   1.1    simonb 	data->m = m0;
   2061   1.1    simonb 	data->ni = ni;
   2062   1.1    simonb 
   2063   1.1    simonb 	DPRINTFN(4, ("sending data: qid=%d idx=%d len=%d nsegs=%d\n",
   2064  1.61  jakllsch 	    ring->qid, ring->cur, m0->m_pkthdr.len, data->map->dm_nsegs));
   2065   1.1    simonb 
   2066   1.1    simonb 	/* first scatter/gather segment is used by the tx data command */
   2067   1.1    simonb 	desc->flags = htole32(WPI_PAD32(m0->m_pkthdr.len) << 28 |
   2068  1.61  jakllsch 	    (1 + data->map->dm_nsegs) << 24);
   2069   1.1    simonb 	desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
   2070  1.61  jakllsch 	    ring->cur * sizeof (struct wpi_tx_cmd));
   2071  1.55  christos 	desc->segs[0].len  = htole32(4 + sizeof (struct wpi_cmd_data) +
   2072  1.61  jakllsch 	    ((hdrlen + 3) & ~3));
   2073   1.1    simonb 	for (i = 1; i <= data->map->dm_nsegs; i++) {
   2074   1.1    simonb 		desc->segs[i].addr =
   2075  1.61  jakllsch 		    htole32(data->map->dm_segs[i - 1].ds_addr);
   2076   1.1    simonb 		desc->segs[i].len  =
   2077  1.61  jakllsch 		    htole32(data->map->dm_segs[i - 1].ds_len);
   2078   1.1    simonb 	}
   2079   1.1    simonb 
   2080   1.1    simonb 	ring->queued++;
   2081   1.1    simonb 
   2082  1.63  jmcneill 	bus_dmamap_sync(sc->sc_dmat, data->map, 0,
   2083  1.63  jmcneill 	    data->map->dm_mapsize,
   2084  1.63  jmcneill 	    BUS_DMASYNC_PREWRITE);
   2085  1.63  jmcneill 	bus_dmamap_sync(sc->sc_dmat, ring->cmd_dma.map, 0,
   2086  1.63  jmcneill 	    ring->cmd_dma.size,
   2087  1.63  jmcneill 	    BUS_DMASYNC_PREWRITE);
   2088  1.63  jmcneill 	bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map, 0,
   2089  1.63  jmcneill 	    ring->desc_dma.size,
   2090  1.63  jmcneill 	    BUS_DMASYNC_PREWRITE);
   2091  1.63  jmcneill 
   2092   1.1    simonb 	/* kick ring */
   2093   1.1    simonb 	ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT;
   2094   1.1    simonb 	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
   2095   1.1    simonb 
   2096   1.1    simonb 	return 0;
   2097   1.1    simonb }
   2098   1.1    simonb 
   2099   1.1    simonb static void
   2100   1.1    simonb wpi_start(struct ifnet *ifp)
   2101   1.1    simonb {
   2102   1.1    simonb 	struct wpi_softc *sc = ifp->if_softc;
   2103   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
   2104   1.1    simonb 	struct ieee80211_node *ni;
   2105   1.1    simonb 	struct ether_header *eh;
   2106   1.1    simonb 	struct mbuf *m0;
   2107   1.1    simonb 	int ac;
   2108   1.1    simonb 
   2109   1.1    simonb 	/*
   2110   1.1    simonb 	 * net80211 may still try to send management frames even if the
   2111   1.1    simonb 	 * IFF_RUNNING flag is not set...
   2112   1.1    simonb 	 */
   2113   1.1    simonb 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
   2114   1.1    simonb 		return;
   2115   1.1    simonb 
   2116   1.1    simonb 	for (;;) {
   2117  1.22    dyoung 		IF_DEQUEUE(&ic->ic_mgtq, m0);
   2118   1.1    simonb 		if (m0 != NULL) {
   2119   1.1    simonb 
   2120  1.72     ozaki 			ni = M_GETCTX(m0, struct ieee80211_node *);
   2121  1.73     ozaki 			M_CLEARCTX(m0);
   2122   1.1    simonb 
   2123   1.1    simonb 			/* management frames go into ring 0 */
   2124   1.1    simonb 			if (sc->txq[0].queued > sc->txq[0].count - 8) {
   2125  1.88   thorpej 				if_statinc(ifp, if_oerrors);
   2126   1.1    simonb 				continue;
   2127   1.1    simonb 			}
   2128  1.80   msaitoh 			bpf_mtap3(ic->ic_rawbpf, m0, BPF_D_OUT);
   2129   1.1    simonb 			if (wpi_tx_data(sc, m0, ni, 0) != 0) {
   2130  1.88   thorpej 				if_statinc(ifp, if_oerrors);
   2131   1.1    simonb 				break;
   2132   1.1    simonb 			}
   2133   1.1    simonb 		} else {
   2134   1.1    simonb 			if (ic->ic_state != IEEE80211_S_RUN)
   2135   1.1    simonb 				break;
   2136   1.7  degroote 			IFQ_POLL(&ifp->if_snd, m0);
   2137   1.1    simonb 			if (m0 == NULL)
   2138   1.1    simonb 				break;
   2139   1.1    simonb 
   2140   1.1    simonb 			if (m0->m_len < sizeof (*eh) &&
   2141  1.38  drochner 			    (m0 = m_pullup(m0, sizeof (*eh))) == NULL) {
   2142  1.88   thorpej 				if_statinc(ifp, if_oerrors);
   2143   1.1    simonb 				continue;
   2144   1.1    simonb 			}
   2145   1.1    simonb 			eh = mtod(m0, struct ether_header *);
   2146   1.1    simonb 			ni = ieee80211_find_txnode(ic, eh->ether_dhost);
   2147   1.1    simonb 			if (ni == NULL) {
   2148   1.1    simonb 				m_freem(m0);
   2149  1.88   thorpej 				if_statinc(ifp, if_oerrors);
   2150   1.1    simonb 				continue;
   2151   1.1    simonb 			}
   2152   1.1    simonb 
   2153   1.1    simonb 			/* classify mbuf so we can find which tx ring to use */
   2154   1.1    simonb 			if (ieee80211_classify(ic, m0, ni) != 0) {
   2155   1.1    simonb 				m_freem(m0);
   2156   1.1    simonb 				ieee80211_free_node(ni);
   2157  1.88   thorpej 				if_statinc(ifp, if_oerrors);
   2158   1.1    simonb 				continue;
   2159   1.1    simonb 			}
   2160   1.1    simonb 
   2161   1.1    simonb 			/* no QoS encapsulation for EAPOL frames */
   2162   1.1    simonb 			ac = (eh->ether_type != htons(ETHERTYPE_PAE)) ?
   2163   1.1    simonb 			    M_WME_GETAC(m0) : WME_AC_BE;
   2164   1.1    simonb 
   2165   1.1    simonb 			if (sc->txq[ac].queued > sc->txq[ac].count - 8) {
   2166   1.1    simonb 				/* there is no place left in this ring */
   2167   1.1    simonb 				ifp->if_flags |= IFF_OACTIVE;
   2168   1.1    simonb 				break;
   2169   1.1    simonb 			}
   2170   1.7  degroote 			IFQ_DEQUEUE(&ifp->if_snd, m0);
   2171  1.80   msaitoh 			bpf_mtap(ifp, m0, BPF_D_OUT);
   2172   1.1    simonb 			m0 = ieee80211_encap(ic, m0, ni);
   2173   1.1    simonb 			if (m0 == NULL) {
   2174   1.1    simonb 				ieee80211_free_node(ni);
   2175  1.88   thorpej 				if_statinc(ifp, if_oerrors);
   2176   1.1    simonb 				continue;
   2177   1.1    simonb 			}
   2178  1.80   msaitoh 			bpf_mtap3(ic->ic_rawbpf, m0, BPF_D_OUT);
   2179   1.1    simonb 			if (wpi_tx_data(sc, m0, ni, ac) != 0) {
   2180   1.1    simonb 				ieee80211_free_node(ni);
   2181  1.88   thorpej 				if_statinc(ifp, if_oerrors);
   2182   1.1    simonb 				break;
   2183   1.1    simonb 			}
   2184   1.1    simonb 		}
   2185   1.1    simonb 
   2186   1.1    simonb 		sc->sc_tx_timer = 5;
   2187   1.1    simonb 		ifp->if_timer = 1;
   2188   1.1    simonb 	}
   2189   1.1    simonb }
   2190   1.1    simonb 
   2191   1.1    simonb static void
   2192   1.1    simonb wpi_watchdog(struct ifnet *ifp)
   2193   1.1    simonb {
   2194   1.1    simonb 	struct wpi_softc *sc = ifp->if_softc;
   2195   1.1    simonb 
   2196   1.1    simonb 	ifp->if_timer = 0;
   2197   1.1    simonb 
   2198   1.1    simonb 	if (sc->sc_tx_timer > 0) {
   2199   1.1    simonb 		if (--sc->sc_tx_timer == 0) {
   2200  1.28  degroote 			aprint_error_dev(sc->sc_dev, "device timeout\n");
   2201   1.1    simonb 			ifp->if_flags &= ~IFF_UP;
   2202  1.90  christos 			wpi_stop_intr(ifp, 1);
   2203  1.88   thorpej 			if_statinc(ifp, if_oerrors);
   2204   1.1    simonb 			return;
   2205   1.1    simonb 		}
   2206   1.1    simonb 		ifp->if_timer = 1;
   2207   1.1    simonb 	}
   2208   1.1    simonb 
   2209   1.1    simonb 	ieee80211_watchdog(&sc->sc_ic);
   2210   1.1    simonb }
   2211   1.1    simonb 
   2212   1.1    simonb static int
   2213   1.9  christos wpi_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   2214   1.1    simonb {
   2215   1.1    simonb #define IS_RUNNING(ifp) \
   2216   1.1    simonb 	((ifp->if_flags & IFF_UP) && (ifp->if_flags & IFF_RUNNING))
   2217   1.1    simonb 
   2218   1.1    simonb 	struct wpi_softc *sc = ifp->if_softc;
   2219   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
   2220   1.1    simonb 	int s, error = 0;
   2221   1.1    simonb 
   2222   1.1    simonb 	s = splnet();
   2223   1.1    simonb 
   2224   1.1    simonb 	switch (cmd) {
   2225   1.1    simonb 	case SIOCSIFFLAGS:
   2226  1.40    dyoung 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   2227  1.40    dyoung 			break;
   2228   1.1    simonb 		if (ifp->if_flags & IFF_UP) {
   2229   1.1    simonb 			if (!(ifp->if_flags & IFF_RUNNING))
   2230   1.1    simonb 				wpi_init(ifp);
   2231   1.1    simonb 		} else {
   2232   1.1    simonb 			if (ifp->if_flags & IFF_RUNNING)
   2233   1.1    simonb 				wpi_stop(ifp, 1);
   2234   1.1    simonb 		}
   2235   1.1    simonb 		break;
   2236   1.1    simonb 
   2237   1.1    simonb 	case SIOCADDMULTI:
   2238   1.1    simonb 	case SIOCDELMULTI:
   2239  1.21    dyoung 		/* XXX no h/w multicast filter? --dyoung */
   2240  1.21    dyoung 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
   2241   1.1    simonb 			/* setup multicast filter, etc */
   2242   1.1    simonb 			error = 0;
   2243   1.1    simonb 		}
   2244   1.1    simonb 		break;
   2245   1.1    simonb 
   2246   1.1    simonb 	default:
   2247   1.7  degroote 		error = ieee80211_ioctl(ic, cmd, data);
   2248   1.1    simonb 	}
   2249   1.1    simonb 
   2250   1.1    simonb 	if (error == ENETRESET) {
   2251   1.1    simonb 		if (IS_RUNNING(ifp) &&
   2252   1.1    simonb 			(ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
   2253   1.1    simonb 			wpi_init(ifp);
   2254   1.1    simonb 		error = 0;
   2255   1.1    simonb 	}
   2256   1.1    simonb 
   2257   1.1    simonb 	splx(s);
   2258   1.1    simonb 	return error;
   2259   1.1    simonb 
   2260   1.1    simonb #undef IS_RUNNING
   2261   1.1    simonb }
   2262   1.1    simonb 
   2263   1.1    simonb /*
   2264   1.1    simonb  * Extract various information from EEPROM.
   2265   1.1    simonb  */
   2266   1.1    simonb static void
   2267   1.1    simonb wpi_read_eeprom(struct wpi_softc *sc)
   2268   1.1    simonb {
   2269   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
   2270  1.12  degroote 	char domain[4];
   2271   1.1    simonb 	int i;
   2272   1.1    simonb 
   2273  1.12  degroote 	wpi_read_prom_data(sc, WPI_EEPROM_CAPABILITIES, &sc->cap, 1);
   2274  1.12  degroote 	wpi_read_prom_data(sc, WPI_EEPROM_REVISION, &sc->rev, 2);
   2275  1.12  degroote 	wpi_read_prom_data(sc, WPI_EEPROM_TYPE, &sc->type, 1);
   2276  1.12  degroote 
   2277  1.12  degroote 	DPRINTF(("cap=%x rev=%x type=%x\n", sc->cap, le16toh(sc->rev),
   2278  1.12  degroote 	    sc->type));
   2279  1.12  degroote 
   2280  1.12  degroote 	/* read and print regulatory domain */
   2281  1.12  degroote 	wpi_read_prom_data(sc, WPI_EEPROM_DOMAIN, domain, 4);
   2282  1.32  jmcneill 	aprint_normal_dev(sc->sc_dev, "%.4s", domain);
   2283  1.12  degroote 
   2284  1.12  degroote 	/* read and print MAC address */
   2285  1.12  degroote 	wpi_read_prom_data(sc, WPI_EEPROM_MAC, ic->ic_myaddr, 6);
   2286  1.12  degroote 	aprint_normal(", address %s\n", ether_sprintf(ic->ic_myaddr));
   2287  1.12  degroote 
   2288  1.12  degroote 	/* read the list of authorized channels */
   2289  1.12  degroote 	for (i = 0; i < WPI_CHAN_BANDS_COUNT; i++)
   2290  1.12  degroote 		wpi_read_eeprom_channels(sc, i);
   2291  1.12  degroote 
   2292  1.12  degroote 	/* read the list of power groups */
   2293  1.12  degroote 	for (i = 0; i < WPI_POWER_GROUPS_COUNT; i++)
   2294  1.12  degroote 		wpi_read_eeprom_group(sc, i);
   2295  1.12  degroote }
   2296  1.12  degroote 
   2297  1.12  degroote static void
   2298  1.12  degroote wpi_read_eeprom_channels(struct wpi_softc *sc, int n)
   2299  1.12  degroote {
   2300  1.12  degroote 	struct ieee80211com *ic = &sc->sc_ic;
   2301  1.12  degroote 	const struct wpi_chan_band *band = &wpi_bands[n];
   2302  1.12  degroote 	struct wpi_eeprom_chan channels[WPI_MAX_CHAN_PER_BAND];
   2303  1.12  degroote 	int chan, i;
   2304  1.12  degroote 
   2305  1.12  degroote 	wpi_read_prom_data(sc, band->addr, channels,
   2306  1.12  degroote 	    band->nchan * sizeof (struct wpi_eeprom_chan));
   2307  1.12  degroote 
   2308  1.12  degroote 	for (i = 0; i < band->nchan; i++) {
   2309  1.12  degroote 		if (!(channels[i].flags & WPI_EEPROM_CHAN_VALID))
   2310  1.12  degroote 			continue;
   2311  1.12  degroote 
   2312  1.12  degroote 		chan = band->chan[i];
   2313  1.12  degroote 
   2314  1.12  degroote 		if (n == 0) {	/* 2GHz band */
   2315  1.12  degroote 			ic->ic_channels[chan].ic_freq =
   2316  1.12  degroote 			    ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ);
   2317  1.12  degroote 			ic->ic_channels[chan].ic_flags =
   2318  1.12  degroote 			    IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
   2319  1.12  degroote 			    IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
   2320  1.12  degroote 
   2321  1.12  degroote 		} else {	/* 5GHz band */
   2322  1.12  degroote 			/*
   2323  1.61  jakllsch 			 * Some 3945ABG adapters support channels 7, 8, 11
   2324  1.12  degroote 			 * and 12 in the 2GHz *and* 5GHz bands.
   2325  1.12  degroote 			 * Because of limitations in our net80211(9) stack,
   2326  1.12  degroote 			 * we can't support these channels in 5GHz band.
   2327  1.12  degroote 			 */
   2328  1.12  degroote 			if (chan <= 14)
   2329  1.12  degroote 				continue;
   2330  1.12  degroote 
   2331  1.12  degroote 			ic->ic_channels[chan].ic_freq =
   2332  1.12  degroote 			    ieee80211_ieee2mhz(chan, IEEE80211_CHAN_5GHZ);
   2333  1.12  degroote 			ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_A;
   2334  1.12  degroote 		}
   2335  1.12  degroote 
   2336  1.12  degroote 		/* is active scan allowed on this channel? */
   2337  1.12  degroote 		if (!(channels[i].flags & WPI_EEPROM_CHAN_ACTIVE)) {
   2338  1.12  degroote 			ic->ic_channels[chan].ic_flags |=
   2339  1.12  degroote 			    IEEE80211_CHAN_PASSIVE;
   2340  1.12  degroote 		}
   2341  1.12  degroote 
   2342  1.12  degroote 		/* save maximum allowed power for this channel */
   2343  1.12  degroote 		sc->maxpwr[chan] = channels[i].maxpwr;
   2344  1.12  degroote 
   2345  1.12  degroote 		DPRINTF(("adding chan %d flags=0x%x maxpwr=%d\n",
   2346  1.12  degroote 		    chan, channels[i].flags, sc->maxpwr[chan]));
   2347  1.12  degroote 	}
   2348  1.12  degroote }
   2349  1.12  degroote 
   2350  1.12  degroote static void
   2351  1.12  degroote wpi_read_eeprom_group(struct wpi_softc *sc, int n)
   2352  1.12  degroote {
   2353  1.12  degroote 	struct wpi_power_group *group = &sc->groups[n];
   2354  1.12  degroote 	struct wpi_eeprom_group rgroup;
   2355  1.12  degroote 	int i;
   2356  1.12  degroote 
   2357  1.12  degroote 	wpi_read_prom_data(sc, WPI_EEPROM_POWER_GRP + n * 32, &rgroup,
   2358  1.12  degroote 	    sizeof rgroup);
   2359  1.12  degroote 
   2360  1.12  degroote 	/* save power group information */
   2361  1.12  degroote 	group->chan   = rgroup.chan;
   2362  1.12  degroote 	group->maxpwr = rgroup.maxpwr;
   2363  1.12  degroote 	/* temperature at which the samples were taken */
   2364  1.12  degroote 	group->temp   = (int16_t)le16toh(rgroup.temp);
   2365  1.12  degroote 
   2366  1.12  degroote 	DPRINTF(("power group %d: chan=%d maxpwr=%d temp=%d\n", n,
   2367  1.12  degroote 	    group->chan, group->maxpwr, group->temp));
   2368  1.12  degroote 
   2369  1.12  degroote 	for (i = 0; i < WPI_SAMPLES_COUNT; i++) {
   2370  1.12  degroote 		group->samples[i].index = rgroup.samples[i].index;
   2371  1.12  degroote 		group->samples[i].power = rgroup.samples[i].power;
   2372  1.12  degroote 
   2373  1.12  degroote 		DPRINTF(("\tsample %d: index=%d power=%d\n", i,
   2374  1.12  degroote 		    group->samples[i].index, group->samples[i].power));
   2375   1.1    simonb 	}
   2376   1.1    simonb }
   2377   1.1    simonb 
   2378   1.1    simonb /*
   2379   1.1    simonb  * Send a command to the firmware.
   2380   1.1    simonb  */
   2381   1.1    simonb static int
   2382   1.1    simonb wpi_cmd(struct wpi_softc *sc, int code, const void *buf, int size, int async)
   2383   1.1    simonb {
   2384   1.1    simonb 	struct wpi_tx_ring *ring = &sc->cmdq;
   2385   1.1    simonb 	struct wpi_tx_desc *desc;
   2386   1.1    simonb 	struct wpi_tx_cmd *cmd;
   2387  1.63  jmcneill 	struct wpi_dma_info *dma;
   2388   1.1    simonb 
   2389   1.1    simonb 	KASSERT(size <= sizeof cmd->data);
   2390   1.1    simonb 
   2391   1.1    simonb 	desc = &ring->desc[ring->cur];
   2392   1.1    simonb 	cmd = &ring->cmd[ring->cur];
   2393   1.1    simonb 
   2394   1.1    simonb 	cmd->code = code;
   2395   1.1    simonb 	cmd->flags = 0;
   2396   1.1    simonb 	cmd->qid = ring->qid;
   2397   1.1    simonb 	cmd->idx = ring->cur;
   2398   1.1    simonb 	memcpy(cmd->data, buf, size);
   2399   1.1    simonb 
   2400  1.63  jmcneill 	dma = &ring->cmd_dma;
   2401  1.63  jmcneill 	bus_dmamap_sync(dma->tag, dma->map, 0, dma->size, BUS_DMASYNC_PREWRITE);
   2402  1.63  jmcneill 
   2403   1.1    simonb 	desc->flags = htole32(WPI_PAD32(size) << 28 | 1 << 24);
   2404   1.1    simonb 	desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
   2405  1.61  jakllsch 	    ring->cur * sizeof (struct wpi_tx_cmd));
   2406   1.1    simonb 	desc->segs[0].len  = htole32(4 + size);
   2407   1.1    simonb 
   2408  1.63  jmcneill 	dma = &ring->desc_dma;
   2409  1.63  jmcneill 	bus_dmamap_sync(dma->tag, dma->map, 0, dma->size, BUS_DMASYNC_PREWRITE);
   2410  1.63  jmcneill 
   2411   1.1    simonb 	/* kick cmd ring */
   2412   1.1    simonb 	ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
   2413   1.1    simonb 	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
   2414   1.1    simonb 
   2415   1.1    simonb 	return async ? 0 : tsleep(cmd, PCATCH, "wpicmd", hz);
   2416   1.1    simonb }
   2417   1.1    simonb 
   2418   1.1    simonb static int
   2419   1.1    simonb wpi_wme_update(struct ieee80211com *ic)
   2420   1.1    simonb {
   2421   1.1    simonb #define WPI_EXP2(v)	htole16((1 << (v)) - 1)
   2422   1.1    simonb #define WPI_USEC(v)	htole16(IEEE80211_TXOP_TO_US(v))
   2423   1.1    simonb 	struct wpi_softc *sc = ic->ic_ifp->if_softc;
   2424   1.1    simonb 	const struct wmeParams *wmep;
   2425   1.1    simonb 	struct wpi_wme_setup wme;
   2426   1.1    simonb 	int ac;
   2427   1.1    simonb 
   2428   1.1    simonb 	/* don't override default WME values if WME is not actually enabled */
   2429   1.1    simonb 	if (!(ic->ic_flags & IEEE80211_F_WME))
   2430   1.1    simonb 		return 0;
   2431   1.1    simonb 
   2432   1.1    simonb 	wme.flags = 0;
   2433   1.1    simonb 	for (ac = 0; ac < WME_NUM_AC; ac++) {
   2434   1.1    simonb 		wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
   2435   1.1    simonb 		wme.ac[ac].aifsn = wmep->wmep_aifsn;
   2436   1.1    simonb 		wme.ac[ac].cwmin = WPI_EXP2(wmep->wmep_logcwmin);
   2437   1.1    simonb 		wme.ac[ac].cwmax = WPI_EXP2(wmep->wmep_logcwmax);
   2438   1.1    simonb 		wme.ac[ac].txop  = WPI_USEC(wmep->wmep_txopLimit);
   2439   1.1    simonb 
   2440   1.1    simonb 		DPRINTF(("setting WME for queue %d aifsn=%d cwmin=%d cwmax=%d "
   2441   1.1    simonb 		    "txop=%d\n", ac, wme.ac[ac].aifsn, wme.ac[ac].cwmin,
   2442   1.1    simonb 		    wme.ac[ac].cwmax, wme.ac[ac].txop));
   2443   1.1    simonb 	}
   2444   1.1    simonb 
   2445   1.1    simonb 	return wpi_cmd(sc, WPI_CMD_SET_WME, &wme, sizeof wme, 1);
   2446   1.1    simonb #undef WPI_USEC
   2447   1.1    simonb #undef WPI_EXP2
   2448   1.1    simonb }
   2449   1.1    simonb 
   2450   1.1    simonb /*
   2451   1.1    simonb  * Configure h/w multi-rate retries.
   2452   1.1    simonb  */
   2453   1.1    simonb static int
   2454   1.1    simonb wpi_mrr_setup(struct wpi_softc *sc)
   2455   1.1    simonb {
   2456   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
   2457   1.1    simonb 	struct wpi_mrr_setup mrr;
   2458   1.1    simonb 	int i, error;
   2459   1.1    simonb 
   2460   1.1    simonb 	/* CCK rates (not used with 802.11a) */
   2461   1.1    simonb 	for (i = WPI_CCK1; i <= WPI_CCK11; i++) {
   2462   1.1    simonb 		mrr.rates[i].flags = 0;
   2463   1.1    simonb 		mrr.rates[i].plcp = wpi_ridx_to_plcp[i];
   2464   1.1    simonb 		/* fallback to the immediate lower CCK rate (if any) */
   2465   1.1    simonb 		mrr.rates[i].next = (i == WPI_CCK1) ? WPI_CCK1 : i - 1;
   2466   1.1    simonb 		/* try one time at this rate before falling back to "next" */
   2467   1.1    simonb 		mrr.rates[i].ntries = 1;
   2468   1.1    simonb 	}
   2469   1.1    simonb 
   2470   1.1    simonb 	/* OFDM rates (not used with 802.11b) */
   2471   1.1    simonb 	for (i = WPI_OFDM6; i <= WPI_OFDM54; i++) {
   2472   1.1    simonb 		mrr.rates[i].flags = 0;
   2473   1.1    simonb 		mrr.rates[i].plcp = wpi_ridx_to_plcp[i];
   2474   1.1    simonb 		/* fallback to the immediate lower rate (if any) */
   2475   1.1    simonb 		/* we allow fallback from OFDM/6 to CCK/2 in 11b/g mode */
   2476   1.1    simonb 		mrr.rates[i].next = (i == WPI_OFDM6) ?
   2477   1.1    simonb 		    ((ic->ic_curmode == IEEE80211_MODE_11A) ?
   2478   1.1    simonb 			WPI_OFDM6 : WPI_CCK2) :
   2479   1.1    simonb 		    i - 1;
   2480   1.1    simonb 		/* try one time at this rate before falling back to "next" */
   2481   1.1    simonb 		mrr.rates[i].ntries = 1;
   2482   1.1    simonb 	}
   2483   1.1    simonb 
   2484   1.1    simonb 	/* setup MRR for control frames */
   2485   1.1    simonb 	mrr.which = htole32(WPI_MRR_CTL);
   2486  1.12  degroote 	error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
   2487   1.1    simonb 	if (error != 0) {
   2488  1.61  jakllsch 		aprint_error_dev(sc->sc_dev,
   2489  1.61  jakllsch 		    "could not setup MRR for control frames\n");
   2490   1.1    simonb 		return error;
   2491   1.1    simonb 	}
   2492   1.1    simonb 
   2493   1.1    simonb 	/* setup MRR for data frames */
   2494   1.1    simonb 	mrr.which = htole32(WPI_MRR_DATA);
   2495  1.12  degroote 	error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
   2496   1.1    simonb 	if (error != 0) {
   2497  1.61  jakllsch 		aprint_error_dev(sc->sc_dev,
   2498  1.61  jakllsch 		    "could not setup MRR for data frames\n");
   2499   1.1    simonb 		return error;
   2500   1.1    simonb 	}
   2501   1.1    simonb 
   2502   1.1    simonb 	return 0;
   2503   1.1    simonb }
   2504   1.1    simonb 
   2505   1.1    simonb static void
   2506   1.1    simonb wpi_set_led(struct wpi_softc *sc, uint8_t which, uint8_t off, uint8_t on)
   2507   1.1    simonb {
   2508   1.1    simonb 	struct wpi_cmd_led led;
   2509   1.1    simonb 
   2510   1.1    simonb 	led.which = which;
   2511   1.1    simonb 	led.unit = htole32(100000);	/* on/off in unit of 100ms */
   2512   1.1    simonb 	led.off = off;
   2513   1.1    simonb 	led.on = on;
   2514   1.1    simonb 
   2515   1.1    simonb 	(void)wpi_cmd(sc, WPI_CMD_SET_LED, &led, sizeof led, 1);
   2516   1.1    simonb }
   2517   1.1    simonb 
   2518   1.1    simonb static void
   2519   1.1    simonb wpi_enable_tsf(struct wpi_softc *sc, struct ieee80211_node *ni)
   2520   1.1    simonb {
   2521   1.1    simonb 	struct wpi_cmd_tsf tsf;
   2522   1.1    simonb 	uint64_t val, mod;
   2523   1.1    simonb 
   2524   1.1    simonb 	memset(&tsf, 0, sizeof tsf);
   2525  1.61  jakllsch 	memcpy(&tsf.tstamp, ni->ni_tstamp.data, sizeof (uint64_t));
   2526   1.1    simonb 	tsf.bintval = htole16(ni->ni_intval);
   2527   1.1    simonb 	tsf.lintval = htole16(10);
   2528   1.1    simonb 
   2529   1.1    simonb 	/* compute remaining time until next beacon */
   2530  1.61  jakllsch 	val = (uint64_t)ni->ni_intval * 1024;	/* msecs -> usecs */
   2531   1.1    simonb 	mod = le64toh(tsf.tstamp) % val;
   2532   1.1    simonb 	tsf.binitval = htole32((uint32_t)(val - mod));
   2533   1.1    simonb 
   2534  1.61  jakllsch 	DPRINTF(("TSF bintval=%u tstamp=%" PRIu64 ", init=%u\n",
   2535   1.1    simonb 	    ni->ni_intval, le64toh(tsf.tstamp), (uint32_t)(val - mod)));
   2536   1.1    simonb 
   2537   1.1    simonb 	if (wpi_cmd(sc, WPI_CMD_TSF, &tsf, sizeof tsf, 1) != 0)
   2538  1.28  degroote 		aprint_error_dev(sc->sc_dev, "could not enable TSF\n");
   2539   1.1    simonb }
   2540   1.1    simonb 
   2541   1.1    simonb /*
   2542  1.12  degroote  * Update Tx power to match what is defined for channel `c'.
   2543  1.12  degroote  */
   2544  1.12  degroote static int
   2545  1.12  degroote wpi_set_txpower(struct wpi_softc *sc, struct ieee80211_channel *c, int async)
   2546  1.12  degroote {
   2547  1.12  degroote 	struct ieee80211com *ic = &sc->sc_ic;
   2548  1.12  degroote 	struct wpi_power_group *group;
   2549  1.12  degroote 	struct wpi_cmd_txpower txpower;
   2550  1.12  degroote 	u_int chan;
   2551  1.12  degroote 	int i;
   2552  1.12  degroote 
   2553  1.12  degroote 	/* get channel number */
   2554  1.12  degroote 	chan = ieee80211_chan2ieee(ic, c);
   2555  1.12  degroote 
   2556  1.12  degroote 	/* find the power group to which this channel belongs */
   2557  1.12  degroote 	if (IEEE80211_IS_CHAN_5GHZ(c)) {
   2558  1.12  degroote 		for (group = &sc->groups[1]; group < &sc->groups[4]; group++)
   2559  1.12  degroote 			if (chan <= group->chan)
   2560  1.12  degroote 				break;
   2561  1.12  degroote 	} else
   2562  1.12  degroote 		group = &sc->groups[0];
   2563  1.12  degroote 
   2564  1.12  degroote 	memset(&txpower, 0, sizeof txpower);
   2565  1.12  degroote 	txpower.band = IEEE80211_IS_CHAN_5GHZ(c) ? 0 : 1;
   2566  1.12  degroote 	txpower.chan = htole16(chan);
   2567  1.12  degroote 
   2568  1.12  degroote 	/* set Tx power for all OFDM and CCK rates */
   2569  1.12  degroote 	for (i = 0; i <= 11 ; i++) {
   2570  1.12  degroote 		/* retrieve Tx power for this channel/rate combination */
   2571  1.12  degroote 		int idx = wpi_get_power_index(sc, group, c,
   2572  1.12  degroote 		    wpi_ridx_to_rate[i]);
   2573  1.12  degroote 
   2574  1.12  degroote 		txpower.rates[i].plcp = wpi_ridx_to_plcp[i];
   2575  1.12  degroote 
   2576  1.12  degroote 		if (IEEE80211_IS_CHAN_5GHZ(c)) {
   2577  1.12  degroote 			txpower.rates[i].rf_gain = wpi_rf_gain_5ghz[idx];
   2578  1.12  degroote 			txpower.rates[i].dsp_gain = wpi_dsp_gain_5ghz[idx];
   2579  1.12  degroote 		} else {
   2580  1.12  degroote 			txpower.rates[i].rf_gain = wpi_rf_gain_2ghz[idx];
   2581  1.12  degroote 			txpower.rates[i].dsp_gain = wpi_dsp_gain_2ghz[idx];
   2582  1.12  degroote 		}
   2583  1.12  degroote 		DPRINTF(("chan %d/rate %d: power index %d\n", chan,
   2584  1.12  degroote 		    wpi_ridx_to_rate[i], idx));
   2585  1.12  degroote 	}
   2586  1.12  degroote 
   2587  1.12  degroote 	return wpi_cmd(sc, WPI_CMD_TXPOWER, &txpower, sizeof txpower, async);
   2588  1.12  degroote }
   2589  1.12  degroote 
   2590  1.12  degroote /*
   2591  1.12  degroote  * Determine Tx power index for a given channel/rate combination.
   2592  1.12  degroote  * This takes into account the regulatory information from EEPROM and the
   2593  1.12  degroote  * current temperature.
   2594  1.12  degroote  */
   2595  1.12  degroote static int
   2596  1.12  degroote wpi_get_power_index(struct wpi_softc *sc, struct wpi_power_group *group,
   2597  1.12  degroote     struct ieee80211_channel *c, int rate)
   2598  1.12  degroote {
   2599  1.12  degroote /* fixed-point arithmetic division using a n-bit fractional part */
   2600  1.12  degroote #define fdivround(a, b, n)	\
   2601  1.12  degroote 	((((1 << n) * (a)) / (b) + (1 << n) / 2) / (1 << n))
   2602  1.12  degroote 
   2603  1.12  degroote /* linear interpolation */
   2604  1.12  degroote #define interpolate(x, x1, y1, x2, y2, n)	\
   2605  1.12  degroote 	((y1) + fdivround(((x) - (x1)) * ((y2) - (y1)), (x2) - (x1), n))
   2606  1.12  degroote 
   2607  1.12  degroote 	struct ieee80211com *ic = &sc->sc_ic;
   2608  1.12  degroote 	struct wpi_power_sample *sample;
   2609  1.12  degroote 	int pwr, idx;
   2610  1.12  degroote 	u_int chan;
   2611  1.12  degroote 
   2612  1.12  degroote 	/* get channel number */
   2613  1.12  degroote 	chan = ieee80211_chan2ieee(ic, c);
   2614  1.12  degroote 
   2615  1.12  degroote 	/* default power is group's maximum power - 3dB */
   2616  1.12  degroote 	pwr = group->maxpwr / 2;
   2617  1.12  degroote 
   2618  1.12  degroote 	/* decrease power for highest OFDM rates to reduce distortion */
   2619  1.12  degroote 	switch (rate) {
   2620  1.12  degroote 	case 72:	/* 36Mb/s */
   2621  1.12  degroote 		pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 0 :  5;
   2622  1.12  degroote 		break;
   2623  1.12  degroote 	case 96:	/* 48Mb/s */
   2624  1.12  degroote 		pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 7 : 10;
   2625  1.12  degroote 		break;
   2626  1.12  degroote 	case 108:	/* 54Mb/s */
   2627  1.12  degroote 		pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 9 : 12;
   2628  1.12  degroote 		break;
   2629  1.12  degroote 	}
   2630  1.12  degroote 
   2631  1.12  degroote 	/* never exceed channel's maximum allowed Tx power */
   2632  1.83  riastrad 	pwr = uimin(pwr, sc->maxpwr[chan]);
   2633  1.12  degroote 
   2634  1.12  degroote 	/* retrieve power index into gain tables from samples */
   2635  1.12  degroote 	for (sample = group->samples; sample < &group->samples[3]; sample++)
   2636  1.12  degroote 		if (pwr > sample[1].power)
   2637  1.12  degroote 			break;
   2638  1.12  degroote 	/* fixed-point linear interpolation using a 19-bit fractional part */
   2639  1.12  degroote 	idx = interpolate(pwr, sample[0].power, sample[0].index,
   2640  1.12  degroote 	    sample[1].power, sample[1].index, 19);
   2641  1.12  degroote 
   2642  1.61  jakllsch 	/*-
   2643  1.12  degroote 	 * Adjust power index based on current temperature:
   2644  1.12  degroote 	 * - if cooler than factory-calibrated: decrease output power
   2645  1.12  degroote 	 * - if warmer than factory-calibrated: increase output power
   2646  1.12  degroote 	 */
   2647  1.12  degroote 	idx -= (sc->temp - group->temp) * 11 / 100;
   2648  1.12  degroote 
   2649  1.12  degroote 	/* decrease power for CCK rates (-5dB) */
   2650  1.12  degroote 	if (!WPI_RATE_IS_OFDM(rate))
   2651  1.12  degroote 		idx += 10;
   2652  1.12  degroote 
   2653  1.12  degroote 	/* keep power index in a valid range */
   2654  1.12  degroote 	if (idx < 0)
   2655  1.12  degroote 		return 0;
   2656  1.12  degroote 	if (idx > WPI_MAX_PWR_INDEX)
   2657  1.12  degroote 		return WPI_MAX_PWR_INDEX;
   2658  1.12  degroote 	return idx;
   2659  1.12  degroote 
   2660  1.12  degroote #undef interpolate
   2661  1.12  degroote #undef fdivround
   2662  1.12  degroote }
   2663  1.12  degroote 
   2664  1.12  degroote /*
   2665   1.1    simonb  * Build a beacon frame that the firmware will broadcast periodically in
   2666   1.1    simonb  * IBSS or HostAP modes.
   2667   1.1    simonb  */
   2668   1.1    simonb static int
   2669   1.1    simonb wpi_setup_beacon(struct wpi_softc *sc, struct ieee80211_node *ni)
   2670   1.1    simonb {
   2671   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
   2672   1.1    simonb 	struct wpi_tx_ring *ring = &sc->cmdq;
   2673   1.1    simonb 	struct wpi_tx_desc *desc;
   2674   1.1    simonb 	struct wpi_tx_data *data;
   2675   1.1    simonb 	struct wpi_tx_cmd *cmd;
   2676   1.1    simonb 	struct wpi_cmd_beacon *bcn;
   2677   1.1    simonb 	struct ieee80211_beacon_offsets bo;
   2678   1.1    simonb 	struct mbuf *m0;
   2679   1.1    simonb 	int error;
   2680   1.1    simonb 
   2681   1.1    simonb 	desc = &ring->desc[ring->cur];
   2682   1.1    simonb 	data = &ring->data[ring->cur];
   2683   1.1    simonb 
   2684   1.1    simonb 	m0 = ieee80211_beacon_alloc(ic, ni, &bo);
   2685   1.1    simonb 	if (m0 == NULL) {
   2686  1.61  jakllsch 		aprint_error_dev(sc->sc_dev,
   2687  1.61  jakllsch 		    "could not allocate beacon frame\n");
   2688   1.1    simonb 		return ENOMEM;
   2689   1.1    simonb 	}
   2690   1.1    simonb 
   2691   1.1    simonb 	cmd = &ring->cmd[ring->cur];
   2692   1.1    simonb 	cmd->code = WPI_CMD_SET_BEACON;
   2693   1.1    simonb 	cmd->flags = 0;
   2694   1.1    simonb 	cmd->qid = ring->qid;
   2695   1.1    simonb 	cmd->idx = ring->cur;
   2696   1.1    simonb 
   2697   1.1    simonb 	bcn = (struct wpi_cmd_beacon *)cmd->data;
   2698   1.1    simonb 	memset(bcn, 0, sizeof (struct wpi_cmd_beacon));
   2699   1.1    simonb 	bcn->id = WPI_ID_BROADCAST;
   2700   1.1    simonb 	bcn->ofdm_mask = 0xff;
   2701   1.1    simonb 	bcn->cck_mask = 0x0f;
   2702  1.12  degroote 	bcn->lifetime = htole32(WPI_LIFETIME_INFINITE);
   2703   1.1    simonb 	bcn->len = htole16(m0->m_pkthdr.len);
   2704   1.1    simonb 	bcn->rate = (ic->ic_curmode == IEEE80211_MODE_11A) ?
   2705  1.61  jakllsch 	    wpi_plcp_signal(12) : wpi_plcp_signal(2);
   2706   1.1    simonb 	bcn->flags = htole32(WPI_TX_AUTO_SEQ | WPI_TX_INSERT_TSTAMP);
   2707   1.1    simonb 
   2708   1.1    simonb 	/* save and trim IEEE802.11 header */
   2709   1.9  christos 	m_copydata(m0, 0, sizeof (struct ieee80211_frame), (void *)&bcn->wh);
   2710   1.1    simonb 	m_adj(m0, sizeof (struct ieee80211_frame));
   2711   1.1    simonb 
   2712   1.1    simonb 	/* assume beacon frame is contiguous */
   2713   1.1    simonb 	error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
   2714  1.61  jakllsch 	    BUS_DMA_READ | BUS_DMA_NOWAIT);
   2715  1.61  jakllsch 	if (error != 0) {
   2716  1.55  christos 		aprint_error_dev(sc->sc_dev, "could not map beacon\n");
   2717   1.1    simonb 		m_freem(m0);
   2718   1.1    simonb 		return error;
   2719   1.1    simonb 	}
   2720   1.1    simonb 
   2721   1.1    simonb 	data->m = m0;
   2722   1.1    simonb 
   2723   1.1    simonb 	/* first scatter/gather segment is used by the beacon command */
   2724   1.1    simonb 	desc->flags = htole32(WPI_PAD32(m0->m_pkthdr.len) << 28 | 2 << 24);
   2725   1.1    simonb 	desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
   2726  1.61  jakllsch 	    ring->cur * sizeof (struct wpi_tx_cmd));
   2727   1.1    simonb 	desc->segs[0].len  = htole32(4 + sizeof (struct wpi_cmd_beacon));
   2728   1.1    simonb 	desc->segs[1].addr = htole32(data->map->dm_segs[0].ds_addr);
   2729   1.1    simonb 	desc->segs[1].len  = htole32(data->map->dm_segs[0].ds_len);
   2730   1.1    simonb 
   2731  1.65  jmcneill 	bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map, 0,
   2732  1.65  jmcneill 	    ring->desc_dma.map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   2733  1.63  jmcneill 	bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize,
   2734  1.63  jmcneill 	    BUS_DMASYNC_PREWRITE);
   2735  1.63  jmcneill 
   2736   1.1    simonb 	/* kick cmd ring */
   2737   1.1    simonb 	ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
   2738   1.1    simonb 	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
   2739   1.1    simonb 
   2740   1.1    simonb 	return 0;
   2741   1.1    simonb }
   2742   1.1    simonb 
   2743   1.1    simonb static int
   2744   1.1    simonb wpi_auth(struct wpi_softc *sc)
   2745   1.1    simonb {
   2746   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
   2747   1.1    simonb 	struct ieee80211_node *ni = ic->ic_bss;
   2748   1.5     joerg 	struct wpi_node_info node;
   2749   1.1    simonb 	int error;
   2750   1.1    simonb 
   2751   1.1    simonb 	/* update adapter's configuration */
   2752   1.1    simonb 	IEEE80211_ADDR_COPY(sc->config.bssid, ni->ni_bssid);
   2753   1.1    simonb 	sc->config.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
   2754   1.1    simonb 	sc->config.flags = htole32(WPI_CONFIG_TSF);
   2755   1.1    simonb 	if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
   2756   1.1    simonb 		sc->config.flags |= htole32(WPI_CONFIG_AUTO |
   2757   1.1    simonb 		    WPI_CONFIG_24GHZ);
   2758   1.1    simonb 	}
   2759   1.1    simonb 	switch (ic->ic_curmode) {
   2760   1.1    simonb 	case IEEE80211_MODE_11A:
   2761   1.1    simonb 		sc->config.cck_mask  = 0;
   2762   1.1    simonb 		sc->config.ofdm_mask = 0x15;
   2763   1.1    simonb 		break;
   2764   1.1    simonb 	case IEEE80211_MODE_11B:
   2765   1.1    simonb 		sc->config.cck_mask  = 0x03;
   2766   1.1    simonb 		sc->config.ofdm_mask = 0;
   2767   1.1    simonb 		break;
   2768   1.1    simonb 	default:	/* assume 802.11b/g */
   2769   1.1    simonb 		sc->config.cck_mask  = 0x0f;
   2770   1.1    simonb 		sc->config.ofdm_mask = 0x15;
   2771   1.1    simonb 	}
   2772   1.1    simonb 	DPRINTF(("config chan %d flags %x cck %x ofdm %x\n", sc->config.chan,
   2773  1.61  jakllsch 	    sc->config.flags, sc->config.cck_mask, sc->config.ofdm_mask));
   2774   1.1    simonb 	error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config,
   2775  1.61  jakllsch 	    sizeof (struct wpi_config), 1);
   2776   1.1    simonb 	if (error != 0) {
   2777  1.55  christos 		aprint_error_dev(sc->sc_dev, "could not configure\n");
   2778   1.1    simonb 		return error;
   2779   1.1    simonb 	}
   2780   1.1    simonb 
   2781  1.12  degroote 	/* configuration has changed, set Tx power accordingly */
   2782  1.12  degroote 	if ((error = wpi_set_txpower(sc, ni->ni_chan, 1)) != 0) {
   2783  1.28  degroote 		aprint_error_dev(sc->sc_dev, "could not set Tx power\n");
   2784  1.12  degroote 		return error;
   2785  1.12  degroote 	}
   2786  1.12  degroote 
   2787   1.1    simonb 	/* add default node */
   2788   1.1    simonb 	memset(&node, 0, sizeof node);
   2789   1.1    simonb 	IEEE80211_ADDR_COPY(node.bssid, ni->ni_bssid);
   2790   1.1    simonb 	node.id = WPI_ID_BSS;
   2791   1.1    simonb 	node.rate = (ic->ic_curmode == IEEE80211_MODE_11A) ?
   2792   1.1    simonb 	    wpi_plcp_signal(12) : wpi_plcp_signal(2);
   2793  1.12  degroote 	node.action = htole32(WPI_ACTION_SET_RATE);
   2794  1.12  degroote 	node.antenna = WPI_ANTENNA_BOTH;
   2795   1.1    simonb 	error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
   2796   1.1    simonb 	if (error != 0) {
   2797  1.55  christos 		aprint_error_dev(sc->sc_dev, "could not add BSS node\n");
   2798   1.1    simonb 		return error;
   2799   1.1    simonb 	}
   2800   1.1    simonb 
   2801   1.1    simonb 	return 0;
   2802   1.1    simonb }
   2803   1.1    simonb 
   2804   1.1    simonb /*
   2805   1.1    simonb  * Send a scan request to the firmware.  Since this command is huge, we map it
   2806   1.1    simonb  * into a mbuf instead of using the pre-allocated set of commands.
   2807   1.1    simonb  */
   2808   1.1    simonb static int
   2809  1.67  jmcneill wpi_scan(struct wpi_softc *sc)
   2810   1.1    simonb {
   2811   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
   2812   1.1    simonb 	struct wpi_tx_ring *ring = &sc->cmdq;
   2813   1.1    simonb 	struct wpi_tx_desc *desc;
   2814   1.1    simonb 	struct wpi_tx_data *data;
   2815   1.1    simonb 	struct wpi_tx_cmd *cmd;
   2816   1.1    simonb 	struct wpi_scan_hdr *hdr;
   2817   1.1    simonb 	struct wpi_scan_chan *chan;
   2818   1.1    simonb 	struct ieee80211_frame *wh;
   2819   1.1    simonb 	struct ieee80211_rateset *rs;
   2820   1.1    simonb 	struct ieee80211_channel *c;
   2821   1.1    simonb 	uint8_t *frm;
   2822  1.61  jakllsch 	int pktlen, error, nrates;
   2823   1.1    simonb 
   2824  1.67  jmcneill 	if (ic->ic_curchan == NULL)
   2825  1.67  jmcneill 		return EIO;
   2826  1.67  jmcneill 
   2827   1.1    simonb 	desc = &ring->desc[ring->cur];
   2828   1.1    simonb 	data = &ring->data[ring->cur];
   2829   1.1    simonb 
   2830   1.1    simonb 	MGETHDR(data->m, M_DONTWAIT, MT_DATA);
   2831   1.1    simonb 	if (data->m == NULL) {
   2832  1.55  christos 		aprint_error_dev(sc->sc_dev,
   2833  1.59  jakllsch 		    "could not allocate mbuf for scan command\n");
   2834   1.1    simonb 		return ENOMEM;
   2835   1.1    simonb 	}
   2836   1.1    simonb 	MCLGET(data->m, M_DONTWAIT);
   2837   1.1    simonb 	if (!(data->m->m_flags & M_EXT)) {
   2838   1.1    simonb 		m_freem(data->m);
   2839   1.1    simonb 		data->m = NULL;
   2840  1.55  christos 		aprint_error_dev(sc->sc_dev,
   2841  1.59  jakllsch 		    "could not allocate mbuf for scan command\n");
   2842   1.1    simonb 		return ENOMEM;
   2843   1.1    simonb 	}
   2844   1.1    simonb 
   2845   1.1    simonb 	cmd = mtod(data->m, struct wpi_tx_cmd *);
   2846   1.1    simonb 	cmd->code = WPI_CMD_SCAN;
   2847   1.1    simonb 	cmd->flags = 0;
   2848   1.1    simonb 	cmd->qid = ring->qid;
   2849   1.1    simonb 	cmd->idx = ring->cur;
   2850   1.1    simonb 
   2851   1.1    simonb 	hdr = (struct wpi_scan_hdr *)cmd->data;
   2852   1.1    simonb 	memset(hdr, 0, sizeof (struct wpi_scan_hdr));
   2853  1.60  jakllsch 	hdr->cmd.flags = htole32(WPI_TX_AUTO_SEQ);
   2854  1.60  jakllsch 	hdr->cmd.id = WPI_ID_BROADCAST;
   2855  1.60  jakllsch 	hdr->cmd.lifetime = htole32(WPI_LIFETIME_INFINITE);
   2856   1.1    simonb 	/*
   2857  1.67  jmcneill 	 * Move to the next channel if no packets are received within 5 msecs
   2858   1.1    simonb 	 * after sending the probe request (this helps to reduce the duration
   2859   1.1    simonb 	 * of active scans).
   2860   1.1    simonb 	 */
   2861  1.67  jmcneill 	hdr->quiet = htole16(5);	/* timeout in milliseconds */
   2862  1.12  degroote 	hdr->plcp_threshold = htole16(1);	/* min # of packets */
   2863   1.1    simonb 
   2864  1.67  jmcneill 	if (ic->ic_curchan->ic_flags & IEEE80211_CHAN_5GHZ) {
   2865  1.12  degroote 		hdr->crc_threshold = htole16(1);
   2866   1.1    simonb 		/* send probe requests at 6Mbps */
   2867  1.60  jakllsch 		hdr->cmd.rate = wpi_plcp_signal(12);
   2868  1.66  jmcneill 		rs = &ic->ic_sup_rates[IEEE80211_MODE_11A];
   2869   1.1    simonb 	} else {
   2870   1.1    simonb 		hdr->flags = htole32(WPI_CONFIG_24GHZ | WPI_CONFIG_AUTO);
   2871   1.1    simonb 		/* send probe requests at 1Mbps */
   2872  1.60  jakllsch 		hdr->cmd.rate = wpi_plcp_signal(2);
   2873  1.66  jmcneill 		rs = &ic->ic_sup_rates[IEEE80211_MODE_11G];
   2874   1.1    simonb 	}
   2875   1.1    simonb 
   2876  1.12  degroote 	/* for directed scans, firmware inserts the essid IE itself */
   2877  1.66  jmcneill 	if (ic->ic_des_esslen != 0) {
   2878  1.66  jmcneill 		hdr->essid[0].id  = IEEE80211_ELEMID_SSID;
   2879  1.66  jmcneill 		hdr->essid[0].len = ic->ic_des_esslen;
   2880  1.66  jmcneill 		memcpy(hdr->essid[0].data, ic->ic_des_essid, ic->ic_des_esslen);
   2881  1.66  jmcneill 	}
   2882   1.1    simonb 
   2883   1.1    simonb 	/*
   2884   1.1    simonb 	 * Build a probe request frame.  Most of the following code is a
   2885   1.1    simonb 	 * copy & paste of what is done in net80211.
   2886   1.1    simonb 	 */
   2887   1.1    simonb 	wh = (struct ieee80211_frame *)(hdr + 1);
   2888   1.1    simonb 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
   2889  1.61  jakllsch 	    IEEE80211_FC0_SUBTYPE_PROBE_REQ;
   2890   1.1    simonb 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
   2891   1.1    simonb 	IEEE80211_ADDR_COPY(wh->i_addr1, etherbroadcastaddr);
   2892   1.1    simonb 	IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
   2893   1.1    simonb 	IEEE80211_ADDR_COPY(wh->i_addr3, etherbroadcastaddr);
   2894   1.1    simonb 	*(u_int16_t *)&wh->i_dur[0] = 0;	/* filled by h/w */
   2895   1.1    simonb 	*(u_int16_t *)&wh->i_seq[0] = 0;	/* filled by h/w */
   2896   1.1    simonb 
   2897   1.1    simonb 	frm = (uint8_t *)(wh + 1);
   2898   1.1    simonb 
   2899  1.12  degroote 	/* add empty essid IE (firmware generates it for directed scans) */
   2900  1.12  degroote 	*frm++ = IEEE80211_ELEMID_SSID;
   2901  1.12  degroote 	*frm++ = 0;
   2902   1.1    simonb 
   2903   1.1    simonb 	/* add supported rates IE */
   2904   1.1    simonb 	*frm++ = IEEE80211_ELEMID_RATES;
   2905   1.1    simonb 	nrates = rs->rs_nrates;
   2906   1.1    simonb 	if (nrates > IEEE80211_RATE_SIZE)
   2907   1.1    simonb 		nrates = IEEE80211_RATE_SIZE;
   2908   1.1    simonb 	*frm++ = nrates;
   2909   1.1    simonb 	memcpy(frm, rs->rs_rates, nrates);
   2910   1.1    simonb 	frm += nrates;
   2911   1.1    simonb 
   2912   1.1    simonb 	/* add supported xrates IE */
   2913   1.1    simonb 	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
   2914   1.1    simonb 		nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
   2915   1.1    simonb 		*frm++ = IEEE80211_ELEMID_XRATES;
   2916   1.1    simonb 		*frm++ = nrates;
   2917   1.1    simonb 		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
   2918   1.1    simonb 		frm += nrates;
   2919   1.1    simonb 	}
   2920   1.1    simonb 
   2921   1.1    simonb 	/* setup length of probe request */
   2922  1.60  jakllsch 	hdr->cmd.len = htole16(frm - (uint8_t *)wh);
   2923   1.1    simonb 
   2924   1.1    simonb 	chan = (struct wpi_scan_chan *)frm;
   2925  1.67  jmcneill 	c = ic->ic_curchan;
   2926   1.1    simonb 
   2927  1.67  jmcneill 	chan->chan = ieee80211_chan2ieee(ic, c);
   2928  1.67  jmcneill 	chan->flags = 0;
   2929  1.67  jmcneill 	if (!(c->ic_flags & IEEE80211_CHAN_PASSIVE)) {
   2930  1.67  jmcneill 		chan->flags |= WPI_CHAN_ACTIVE;
   2931  1.66  jmcneill 		if (ic->ic_des_esslen != 0)
   2932  1.66  jmcneill 			chan->flags |= WPI_CHAN_DIRECT;
   2933  1.67  jmcneill 	}
   2934  1.67  jmcneill 	chan->dsp_gain = 0x6e;
   2935  1.67  jmcneill 	if (IEEE80211_IS_CHAN_5GHZ(c)) {
   2936  1.67  jmcneill 		chan->rf_gain = 0x3b;
   2937  1.67  jmcneill 		chan->active  = htole16(10);
   2938  1.67  jmcneill 		chan->passive = htole16(110);
   2939  1.67  jmcneill 	} else {
   2940  1.67  jmcneill 		chan->rf_gain = 0x28;
   2941  1.67  jmcneill 		chan->active  = htole16(20);
   2942  1.67  jmcneill 		chan->passive = htole16(120);
   2943  1.67  jmcneill 	}
   2944  1.67  jmcneill 	hdr->nchan++;
   2945  1.67  jmcneill 	chan++;
   2946   1.1    simonb 
   2947  1.67  jmcneill 	frm += sizeof (struct wpi_scan_chan);
   2948  1.61  jakllsch 
   2949  1.12  degroote 	hdr->len = htole16(frm - (uint8_t *)hdr);
   2950  1.12  degroote 	pktlen = frm - (uint8_t *)cmd;
   2951   1.1    simonb 
   2952  1.61  jakllsch 	error = bus_dmamap_load(sc->sc_dmat, data->map, cmd, pktlen, NULL,
   2953  1.61  jakllsch 	    BUS_DMA_NOWAIT);
   2954  1.61  jakllsch 	if (error != 0) {
   2955  1.28  degroote 		aprint_error_dev(sc->sc_dev, "could not map scan command\n");
   2956   1.1    simonb 		m_freem(data->m);
   2957   1.1    simonb 		data->m = NULL;
   2958   1.1    simonb 		return error;
   2959   1.1    simonb 	}
   2960   1.1    simonb 
   2961   1.1    simonb 	desc->flags = htole32(WPI_PAD32(pktlen) << 28 | 1 << 24);
   2962   1.1    simonb 	desc->segs[0].addr = htole32(data->map->dm_segs[0].ds_addr);
   2963   1.1    simonb 	desc->segs[0].len  = htole32(data->map->dm_segs[0].ds_len);
   2964   1.1    simonb 
   2965  1.65  jmcneill 	bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map, 0,
   2966  1.65  jmcneill 	    ring->desc_dma.map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   2967  1.63  jmcneill 	bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize,
   2968  1.63  jmcneill 	    BUS_DMASYNC_PREWRITE);
   2969  1.63  jmcneill 
   2970   1.1    simonb 	/* kick cmd ring */
   2971   1.1    simonb 	ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
   2972   1.1    simonb 	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
   2973   1.1    simonb 
   2974   1.1    simonb 	return 0;	/* will be notified async. of failure/success */
   2975   1.1    simonb }
   2976   1.1    simonb 
   2977   1.1    simonb static int
   2978   1.1    simonb wpi_config(struct wpi_softc *sc)
   2979   1.1    simonb {
   2980   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
   2981   1.1    simonb 	struct ifnet *ifp = ic->ic_ifp;
   2982   1.1    simonb 	struct wpi_power power;
   2983   1.1    simonb 	struct wpi_bluetooth bluetooth;
   2984   1.5     joerg 	struct wpi_node_info node;
   2985   1.1    simonb 	int error;
   2986   1.1    simonb 
   2987   1.1    simonb 	memset(&power, 0, sizeof power);
   2988  1.12  degroote 	power.flags = htole32(WPI_POWER_CAM | 0x8);
   2989   1.1    simonb 	error = wpi_cmd(sc, WPI_CMD_SET_POWER_MODE, &power, sizeof power, 0);
   2990   1.1    simonb 	if (error != 0) {
   2991  1.28  degroote 		aprint_error_dev(sc->sc_dev, "could not set power mode\n");
   2992   1.1    simonb 		return error;
   2993   1.1    simonb 	}
   2994   1.1    simonb 
   2995   1.1    simonb 	/* configure bluetooth coexistence */
   2996   1.1    simonb 	memset(&bluetooth, 0, sizeof bluetooth);
   2997   1.1    simonb 	bluetooth.flags = 3;
   2998   1.1    simonb 	bluetooth.lead = 0xaa;
   2999   1.1    simonb 	bluetooth.kill = 1;
   3000   1.1    simonb 	error = wpi_cmd(sc, WPI_CMD_BLUETOOTH, &bluetooth, sizeof bluetooth,
   3001  1.61  jakllsch 	    0);
   3002   1.1    simonb 	if (error != 0) {
   3003  1.28  degroote 		aprint_error_dev(sc->sc_dev,
   3004  1.28  degroote 			"could not configure bluetooth coexistence\n");
   3005   1.1    simonb 		return error;
   3006   1.1    simonb 	}
   3007   1.1    simonb 
   3008   1.1    simonb 	/* configure adapter */
   3009   1.1    simonb 	memset(&sc->config, 0, sizeof (struct wpi_config));
   3010  1.20    dyoung 	IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl));
   3011   1.1    simonb 	IEEE80211_ADDR_COPY(sc->config.myaddr, ic->ic_myaddr);
   3012  1.61  jakllsch 	/* set default channel */
   3013  1.66  jmcneill 	sc->config.chan = ieee80211_chan2ieee(ic, ic->ic_curchan);
   3014   1.1    simonb 	sc->config.flags = htole32(WPI_CONFIG_TSF);
   3015  1.66  jmcneill 	if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
   3016   1.1    simonb 		sc->config.flags |= htole32(WPI_CONFIG_AUTO |
   3017   1.1    simonb 		    WPI_CONFIG_24GHZ);
   3018   1.1    simonb 	}
   3019   1.1    simonb 	sc->config.filter = 0;
   3020   1.1    simonb 	switch (ic->ic_opmode) {
   3021   1.1    simonb 	case IEEE80211_M_STA:
   3022   1.1    simonb 		sc->config.mode = WPI_MODE_STA;
   3023   1.1    simonb 		sc->config.filter |= htole32(WPI_FILTER_MULTICAST);
   3024   1.1    simonb 		break;
   3025   1.1    simonb 	case IEEE80211_M_IBSS:
   3026   1.1    simonb 	case IEEE80211_M_AHDEMO:
   3027   1.1    simonb 		sc->config.mode = WPI_MODE_IBSS;
   3028   1.1    simonb 		break;
   3029   1.1    simonb 	case IEEE80211_M_HOSTAP:
   3030   1.1    simonb 		sc->config.mode = WPI_MODE_HOSTAP;
   3031   1.1    simonb 		break;
   3032   1.1    simonb 	case IEEE80211_M_MONITOR:
   3033   1.1    simonb 		sc->config.mode = WPI_MODE_MONITOR;
   3034   1.1    simonb 		sc->config.filter |= htole32(WPI_FILTER_MULTICAST |
   3035  1.61  jakllsch 		    WPI_FILTER_CTL | WPI_FILTER_PROMISC);
   3036   1.1    simonb 		break;
   3037   1.1    simonb 	}
   3038   1.1    simonb 	sc->config.cck_mask  = 0x0f;	/* not yet negotiated */
   3039   1.1    simonb 	sc->config.ofdm_mask = 0xff;	/* not yet negotiated */
   3040   1.1    simonb 	error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config,
   3041  1.61  jakllsch 	    sizeof (struct wpi_config), 0);
   3042   1.1    simonb 	if (error != 0) {
   3043  1.28  degroote 		aprint_error_dev(sc->sc_dev, "configure command failed\n");
   3044   1.1    simonb 		return error;
   3045   1.1    simonb 	}
   3046   1.1    simonb 
   3047  1.12  degroote 	/* configuration has changed, set Tx power accordingly */
   3048  1.66  jmcneill 	if ((error = wpi_set_txpower(sc, ic->ic_curchan, 0)) != 0) {
   3049  1.28  degroote 		aprint_error_dev(sc->sc_dev, "could not set Tx power\n");
   3050  1.12  degroote 		return error;
   3051  1.12  degroote 	}
   3052  1.12  degroote 
   3053   1.1    simonb 	/* add broadcast node */
   3054   1.1    simonb 	memset(&node, 0, sizeof node);
   3055   1.1    simonb 	IEEE80211_ADDR_COPY(node.bssid, etherbroadcastaddr);
   3056   1.1    simonb 	node.id = WPI_ID_BROADCAST;
   3057   1.1    simonb 	node.rate = wpi_plcp_signal(2);
   3058  1.12  degroote 	node.action = htole32(WPI_ACTION_SET_RATE);
   3059  1.55  christos 	node.antenna = WPI_ANTENNA_BOTH;
   3060   1.1    simonb 	error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 0);
   3061   1.1    simonb 	if (error != 0) {
   3062  1.28  degroote 		aprint_error_dev(sc->sc_dev, "could not add broadcast node\n");
   3063   1.1    simonb 		return error;
   3064   1.1    simonb 	}
   3065   1.1    simonb 
   3066  1.12  degroote 	if ((error = wpi_mrr_setup(sc)) != 0) {
   3067  1.28  degroote 		aprint_error_dev(sc->sc_dev, "could not setup MRR\n");
   3068  1.12  degroote 		return error;
   3069  1.12  degroote 	}
   3070  1.12  degroote 
   3071   1.1    simonb 	return 0;
   3072   1.1    simonb }
   3073   1.1    simonb 
   3074   1.1    simonb static void
   3075   1.1    simonb wpi_stop_master(struct wpi_softc *sc)
   3076   1.1    simonb {
   3077   1.1    simonb 	uint32_t tmp;
   3078   1.1    simonb 	int ntries;
   3079   1.1    simonb 
   3080   1.1    simonb 	tmp = WPI_READ(sc, WPI_RESET);
   3081   1.1    simonb 	WPI_WRITE(sc, WPI_RESET, tmp | WPI_STOP_MASTER);
   3082   1.1    simonb 
   3083   1.1    simonb 	tmp = WPI_READ(sc, WPI_GPIO_CTL);
   3084   1.1    simonb 	if ((tmp & WPI_GPIO_PWR_STATUS) == WPI_GPIO_PWR_SLEEP)
   3085   1.1    simonb 		return;	/* already asleep */
   3086   1.1    simonb 
   3087   1.1    simonb 	for (ntries = 0; ntries < 100; ntries++) {
   3088   1.1    simonb 		if (WPI_READ(sc, WPI_RESET) & WPI_MASTER_DISABLED)
   3089   1.1    simonb 			break;
   3090   1.1    simonb 		DELAY(10);
   3091   1.1    simonb 	}
   3092   1.1    simonb 	if (ntries == 100) {
   3093  1.28  degroote 		aprint_error_dev(sc->sc_dev, "timeout waiting for master\n");
   3094   1.1    simonb 	}
   3095   1.1    simonb }
   3096   1.1    simonb 
   3097   1.1    simonb static int
   3098   1.1    simonb wpi_power_up(struct wpi_softc *sc)
   3099   1.1    simonb {
   3100   1.1    simonb 	uint32_t tmp;
   3101   1.1    simonb 	int ntries;
   3102   1.1    simonb 
   3103   1.1    simonb 	wpi_mem_lock(sc);
   3104   1.1    simonb 	tmp = wpi_mem_read(sc, WPI_MEM_POWER);
   3105   1.1    simonb 	wpi_mem_write(sc, WPI_MEM_POWER, tmp & ~0x03000000);
   3106   1.1    simonb 	wpi_mem_unlock(sc);
   3107   1.1    simonb 
   3108   1.1    simonb 	for (ntries = 0; ntries < 5000; ntries++) {
   3109   1.1    simonb 		if (WPI_READ(sc, WPI_GPIO_STATUS) & WPI_POWERED)
   3110   1.1    simonb 			break;
   3111   1.1    simonb 		DELAY(10);
   3112   1.1    simonb 	}
   3113   1.1    simonb 	if (ntries == 5000) {
   3114  1.61  jakllsch 		aprint_error_dev(sc->sc_dev,
   3115  1.61  jakllsch 		    "timeout waiting for NIC to power up\n");
   3116   1.1    simonb 		return ETIMEDOUT;
   3117   1.1    simonb 	}
   3118   1.1    simonb 	return 0;
   3119   1.1    simonb }
   3120   1.1    simonb 
   3121   1.1    simonb static int
   3122   1.1    simonb wpi_reset(struct wpi_softc *sc)
   3123   1.1    simonb {
   3124   1.1    simonb 	uint32_t tmp;
   3125   1.1    simonb 	int ntries;
   3126   1.1    simonb 
   3127   1.1    simonb 	/* clear any pending interrupts */
   3128   1.1    simonb 	WPI_WRITE(sc, WPI_INTR, 0xffffffff);
   3129   1.1    simonb 
   3130   1.1    simonb 	tmp = WPI_READ(sc, WPI_PLL_CTL);
   3131   1.1    simonb 	WPI_WRITE(sc, WPI_PLL_CTL, tmp | WPI_PLL_INIT);
   3132   1.1    simonb 
   3133   1.1    simonb 	tmp = WPI_READ(sc, WPI_CHICKEN);
   3134   1.1    simonb 	WPI_WRITE(sc, WPI_CHICKEN, tmp | WPI_CHICKEN_RXNOLOS);
   3135   1.1    simonb 
   3136   1.1    simonb 	tmp = WPI_READ(sc, WPI_GPIO_CTL);
   3137   1.1    simonb 	WPI_WRITE(sc, WPI_GPIO_CTL, tmp | WPI_GPIO_INIT);
   3138   1.1    simonb 
   3139   1.1    simonb 	/* wait for clock stabilization */
   3140   1.1    simonb 	for (ntries = 0; ntries < 1000; ntries++) {
   3141   1.1    simonb 		if (WPI_READ(sc, WPI_GPIO_CTL) & WPI_GPIO_CLOCK)
   3142   1.1    simonb 			break;
   3143   1.1    simonb 		DELAY(10);
   3144   1.1    simonb 	}
   3145   1.1    simonb 	if (ntries == 1000) {
   3146  1.55  christos 		aprint_error_dev(sc->sc_dev,
   3147  1.61  jakllsch 		    "timeout waiting for clock stabilization\n");
   3148   1.1    simonb 		return ETIMEDOUT;
   3149   1.1    simonb 	}
   3150   1.1    simonb 
   3151   1.1    simonb 	/* initialize EEPROM */
   3152   1.1    simonb 	tmp = WPI_READ(sc, WPI_EEPROM_STATUS);
   3153   1.1    simonb 	if ((tmp & WPI_EEPROM_VERSION) == 0) {
   3154  1.28  degroote 		aprint_error_dev(sc->sc_dev, "EEPROM not found\n");
   3155   1.1    simonb 		return EIO;
   3156   1.1    simonb 	}
   3157   1.1    simonb 	WPI_WRITE(sc, WPI_EEPROM_STATUS, tmp & ~WPI_EEPROM_LOCKED);
   3158   1.1    simonb 
   3159   1.1    simonb 	return 0;
   3160   1.1    simonb }
   3161   1.1    simonb 
   3162   1.1    simonb static void
   3163   1.1    simonb wpi_hw_config(struct wpi_softc *sc)
   3164   1.1    simonb {
   3165   1.1    simonb 	uint32_t rev, hw;
   3166   1.1    simonb 
   3167  1.12  degroote 	/* voodoo from the reference driver */
   3168   1.1    simonb 	hw = WPI_READ(sc, WPI_HWCONFIG);
   3169   1.1    simonb 
   3170   1.1    simonb 	rev = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_CLASS_REG);
   3171   1.1    simonb 	rev = PCI_REVISION(rev);
   3172   1.1    simonb 	if ((rev & 0xc0) == 0x40)
   3173   1.1    simonb 		hw |= WPI_HW_ALM_MB;
   3174   1.1    simonb 	else if (!(rev & 0x80))
   3175   1.1    simonb 		hw |= WPI_HW_ALM_MM;
   3176   1.1    simonb 
   3177  1.12  degroote 	if (sc->cap == 0x80)
   3178   1.1    simonb 		hw |= WPI_HW_SKU_MRC;
   3179   1.1    simonb 
   3180   1.1    simonb 	hw &= ~WPI_HW_REV_D;
   3181  1.55  christos 	if ((le16toh(sc->rev) & 0xf0) == 0xd0)
   3182   1.1    simonb 		hw |= WPI_HW_REV_D;
   3183   1.1    simonb 
   3184  1.12  degroote 	if (sc->type > 1)
   3185   1.1    simonb 		hw |= WPI_HW_TYPE_B;
   3186   1.1    simonb 
   3187   1.1    simonb 	DPRINTF(("setting h/w config %x\n", hw));
   3188   1.1    simonb 	WPI_WRITE(sc, WPI_HWCONFIG, hw);
   3189   1.1    simonb }
   3190   1.1    simonb 
   3191   1.1    simonb static int
   3192   1.1    simonb wpi_init(struct ifnet *ifp)
   3193   1.1    simonb {
   3194   1.1    simonb 	struct wpi_softc *sc = ifp->if_softc;
   3195   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
   3196   1.1    simonb 	uint32_t tmp;
   3197   1.1    simonb 	int qid, ntries, error;
   3198   1.1    simonb 
   3199  1.90  christos 	wpi_stop(ifp, 1);
   3200   1.1    simonb 	(void)wpi_reset(sc);
   3201   1.1    simonb 
   3202   1.1    simonb 	wpi_mem_lock(sc);
   3203   1.1    simonb 	wpi_mem_write(sc, WPI_MEM_CLOCK1, 0xa00);
   3204   1.1    simonb 	DELAY(20);
   3205   1.1    simonb 	tmp = wpi_mem_read(sc, WPI_MEM_PCIDEV);
   3206   1.1    simonb 	wpi_mem_write(sc, WPI_MEM_PCIDEV, tmp | 0x800);
   3207   1.1    simonb 	wpi_mem_unlock(sc);
   3208   1.1    simonb 
   3209   1.1    simonb 	(void)wpi_power_up(sc);
   3210   1.1    simonb 	wpi_hw_config(sc);
   3211   1.1    simonb 
   3212   1.1    simonb 	/* init Rx ring */
   3213   1.1    simonb 	wpi_mem_lock(sc);
   3214   1.1    simonb 	WPI_WRITE(sc, WPI_RX_BASE, sc->rxq.desc_dma.paddr);
   3215   1.1    simonb 	WPI_WRITE(sc, WPI_RX_RIDX_PTR, sc->shared_dma.paddr +
   3216   1.1    simonb 	    offsetof(struct wpi_shared, next));
   3217   1.1    simonb 	WPI_WRITE(sc, WPI_RX_WIDX, (WPI_RX_RING_COUNT - 1) & ~7);
   3218   1.1    simonb 	WPI_WRITE(sc, WPI_RX_CONFIG, 0xa9601010);
   3219   1.1    simonb 	wpi_mem_unlock(sc);
   3220   1.1    simonb 
   3221   1.1    simonb 	/* init Tx rings */
   3222   1.1    simonb 	wpi_mem_lock(sc);
   3223  1.61  jakllsch 	wpi_mem_write(sc, WPI_MEM_MODE, 2);	/* bypass mode */
   3224  1.61  jakllsch 	wpi_mem_write(sc, WPI_MEM_RA, 1);	/* enable RA0 */
   3225  1.61  jakllsch 	wpi_mem_write(sc, WPI_MEM_TXCFG, 0x3f);	/* enable all 6 Tx rings */
   3226   1.1    simonb 	wpi_mem_write(sc, WPI_MEM_BYPASS1, 0x10000);
   3227   1.1    simonb 	wpi_mem_write(sc, WPI_MEM_BYPASS2, 0x30002);
   3228   1.1    simonb 	wpi_mem_write(sc, WPI_MEM_MAGIC4, 4);
   3229   1.1    simonb 	wpi_mem_write(sc, WPI_MEM_MAGIC5, 5);
   3230   1.1    simonb 
   3231   1.1    simonb 	WPI_WRITE(sc, WPI_TX_BASE_PTR, sc->shared_dma.paddr);
   3232   1.1    simonb 	WPI_WRITE(sc, WPI_MSG_CONFIG, 0xffff05a5);
   3233   1.1    simonb 
   3234   1.1    simonb 	for (qid = 0; qid < 6; qid++) {
   3235   1.1    simonb 		WPI_WRITE(sc, WPI_TX_CTL(qid), 0);
   3236   1.1    simonb 		WPI_WRITE(sc, WPI_TX_BASE(qid), 0);
   3237   1.1    simonb 		WPI_WRITE(sc, WPI_TX_CONFIG(qid), 0x80200008);
   3238   1.1    simonb 	}
   3239   1.1    simonb 	wpi_mem_unlock(sc);
   3240   1.1    simonb 
   3241   1.1    simonb 	/* clear "radio off" and "disable command" bits (reversed logic) */
   3242   1.1    simonb 	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
   3243   1.1    simonb 	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_DISABLE_CMD);
   3244   1.1    simonb 
   3245   1.1    simonb 	/* clear any pending interrupts */
   3246   1.1    simonb 	WPI_WRITE(sc, WPI_INTR, 0xffffffff);
   3247   1.1    simonb 	/* enable interrupts */
   3248   1.1    simonb 	WPI_WRITE(sc, WPI_MASK, WPI_INTR_MASK);
   3249   1.1    simonb 
   3250  1.12  degroote 	/* not sure why/if this is necessary... */
   3251  1.12  degroote 	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
   3252  1.12  degroote 	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
   3253   1.1    simonb 
   3254  1.54  riastrad 	if ((error = wpi_load_firmware(sc)) != 0)
   3255  1.54  riastrad 		/* wpi_load_firmware prints error messages for us.  */
   3256   1.1    simonb 		goto fail1;
   3257   1.1    simonb 
   3258  1.31  degroote 	/* Check the status of the radio switch */
   3259  1.71    bouyer 	mutex_enter(&sc->sc_rsw_mtx);
   3260  1.34  degroote 	if (wpi_getrfkill(sc)) {
   3261  1.71    bouyer 		mutex_exit(&sc->sc_rsw_mtx);
   3262  1.54  riastrad 		aprint_error_dev(sc->sc_dev,
   3263  1.54  riastrad 		    "radio is disabled by hardware switch\n");
   3264  1.69    bouyer 		ifp->if_flags &= ~IFF_UP;
   3265  1.54  riastrad 		error = EBUSY;
   3266  1.31  degroote 		goto fail1;
   3267  1.31  degroote 	}
   3268  1.82  riastrad 	sc->sc_rsw_suspend = false;
   3269  1.82  riastrad 	cv_broadcast(&sc->sc_rsw_cv);
   3270  1.82  riastrad 	while (sc->sc_rsw_suspend)
   3271  1.82  riastrad 		cv_wait(&sc->sc_rsw_cv, &sc->sc_rsw_mtx);
   3272  1.71    bouyer 	mutex_exit(&sc->sc_rsw_mtx);
   3273  1.31  degroote 
   3274   1.1    simonb 	/* wait for thermal sensors to calibrate */
   3275   1.1    simonb 	for (ntries = 0; ntries < 1000; ntries++) {
   3276  1.55  christos 		if ((sc->temp = (int)WPI_READ(sc, WPI_TEMPERATURE)) != 0)
   3277   1.1    simonb 			break;
   3278   1.1    simonb 		DELAY(10);
   3279   1.1    simonb 	}
   3280   1.1    simonb 	if (ntries == 1000) {
   3281  1.54  riastrad 		aprint_error_dev(sc->sc_dev,
   3282  1.54  riastrad 		    "timeout waiting for thermal sensors calibration\n");
   3283   1.1    simonb 		error = ETIMEDOUT;
   3284   1.1    simonb 		goto fail1;
   3285   1.1    simonb 	}
   3286  1.12  degroote 	DPRINTF(("temperature %d\n", sc->temp));
   3287   1.1    simonb 
   3288   1.1    simonb 	if ((error = wpi_config(sc)) != 0) {
   3289  1.28  degroote 		aprint_error_dev(sc->sc_dev, "could not configure device\n");
   3290   1.1    simonb 		goto fail1;
   3291   1.1    simonb 	}
   3292   1.1    simonb 
   3293   1.1    simonb 	ifp->if_flags &= ~IFF_OACTIVE;
   3294   1.1    simonb 	ifp->if_flags |= IFF_RUNNING;
   3295   1.1    simonb 
   3296   1.1    simonb 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
   3297   1.1    simonb 		if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
   3298   1.1    simonb 			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
   3299   1.1    simonb 	}
   3300   1.1    simonb 	else
   3301   1.1    simonb 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
   3302   1.1    simonb 
   3303   1.1    simonb 	return 0;
   3304   1.1    simonb 
   3305   1.1    simonb fail1:	wpi_stop(ifp, 1);
   3306   1.1    simonb 	return error;
   3307   1.1    simonb }
   3308   1.1    simonb 
   3309   1.1    simonb static void
   3310  1.90  christos wpi_stop1(struct ifnet *ifp, int disable, bool fromintr)
   3311   1.1    simonb {
   3312   1.1    simonb 	struct wpi_softc *sc = ifp->if_softc;
   3313   1.1    simonb 	struct ieee80211com *ic = &sc->sc_ic;
   3314   1.1    simonb 	uint32_t tmp;
   3315   1.1    simonb 	int ac;
   3316   1.1    simonb 
   3317   1.1    simonb 	ifp->if_timer = sc->sc_tx_timer = 0;
   3318   1.1    simonb 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   3319   1.1    simonb 
   3320   1.1    simonb 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
   3321   1.1    simonb 
   3322  1.90  christos 	if (fromintr) {
   3323  1.90  christos 		sc->sc_rsw_suspend = true; // XXX: without mutex or wait
   3324  1.90  christos 	} else {
   3325  1.90  christos 		wpi_rsw_suspend(sc);
   3326  1.90  christos 	}
   3327  1.82  riastrad 
   3328   1.1    simonb 	/* disable interrupts */
   3329   1.1    simonb 	WPI_WRITE(sc, WPI_MASK, 0);
   3330   1.1    simonb 	WPI_WRITE(sc, WPI_INTR, WPI_INTR_MASK);
   3331   1.1    simonb 	WPI_WRITE(sc, WPI_INTR_STATUS, 0xff);
   3332   1.1    simonb 	WPI_WRITE(sc, WPI_INTR_STATUS, 0x00070000);
   3333   1.1    simonb 
   3334   1.1    simonb 	wpi_mem_lock(sc);
   3335   1.1    simonb 	wpi_mem_write(sc, WPI_MEM_MODE, 0);
   3336   1.1    simonb 	wpi_mem_unlock(sc);
   3337   1.1    simonb 
   3338   1.1    simonb 	/* reset all Tx rings */
   3339   1.1    simonb 	for (ac = 0; ac < 4; ac++)
   3340   1.1    simonb 		wpi_reset_tx_ring(sc, &sc->txq[ac]);
   3341   1.1    simonb 	wpi_reset_tx_ring(sc, &sc->cmdq);
   3342   1.1    simonb 
   3343   1.1    simonb 	/* reset Rx ring */
   3344   1.1    simonb 	wpi_reset_rx_ring(sc, &sc->rxq);
   3345  1.55  christos 
   3346   1.1    simonb 	wpi_mem_lock(sc);
   3347   1.1    simonb 	wpi_mem_write(sc, WPI_MEM_CLOCK2, 0x200);
   3348   1.1    simonb 	wpi_mem_unlock(sc);
   3349   1.1    simonb 
   3350   1.1    simonb 	DELAY(5);
   3351   1.1    simonb 
   3352   1.1    simonb 	wpi_stop_master(sc);
   3353   1.1    simonb 
   3354   1.1    simonb 	tmp = WPI_READ(sc, WPI_RESET);
   3355   1.1    simonb 	WPI_WRITE(sc, WPI_RESET, tmp | WPI_SW_RESET);
   3356   1.1    simonb }
   3357  1.33  jmcneill 
   3358  1.90  christos static void
   3359  1.90  christos wpi_stop(struct ifnet *ifp, int disable)
   3360  1.90  christos {
   3361  1.90  christos 	wpi_stop1(ifp, disable, false);
   3362  1.90  christos }
   3363  1.90  christos 
   3364  1.90  christos static void
   3365  1.90  christos wpi_stop_intr(struct ifnet *ifp, int disable)
   3366  1.90  christos {
   3367  1.90  christos 	wpi_stop1(ifp, disable, true);
   3368  1.90  christos }
   3369  1.90  christos 
   3370  1.33  jmcneill static bool
   3371  1.46    dyoung wpi_resume(device_t dv, const pmf_qual_t *qual)
   3372  1.33  jmcneill {
   3373  1.33  jmcneill 	struct wpi_softc *sc = device_private(dv);
   3374  1.33  jmcneill 
   3375  1.33  jmcneill 	(void)wpi_reset(sc);
   3376  1.33  jmcneill 
   3377  1.33  jmcneill 	return true;
   3378  1.33  jmcneill }
   3379  1.34  degroote 
   3380  1.34  degroote /*
   3381  1.34  degroote  * Return whether or not the radio is enabled in hardware
   3382  1.34  degroote  * (i.e. the rfkill switch is "off").
   3383  1.34  degroote  */
   3384  1.34  degroote static int
   3385  1.34  degroote wpi_getrfkill(struct wpi_softc *sc)
   3386  1.34  degroote {
   3387  1.34  degroote 	uint32_t tmp;
   3388  1.34  degroote 
   3389  1.34  degroote 	wpi_mem_lock(sc);
   3390  1.34  degroote 	tmp = wpi_mem_read(sc, WPI_MEM_RFKILL);
   3391  1.34  degroote 	wpi_mem_unlock(sc);
   3392  1.34  degroote 
   3393  1.70    bouyer 	KASSERT(mutex_owned(&sc->sc_rsw_mtx));
   3394  1.70    bouyer 	if (tmp & 0x01) {
   3395  1.70    bouyer 		/* switch is on */
   3396  1.70    bouyer 		if (sc->sc_rsw_status != WPI_RSW_ON) {
   3397  1.70    bouyer 			sc->sc_rsw_status = WPI_RSW_ON;
   3398  1.70    bouyer 			sysmon_pswitch_event(&sc->sc_rsw,
   3399  1.70    bouyer 			    PSWITCH_EVENT_PRESSED);
   3400  1.70    bouyer 		}
   3401  1.70    bouyer 	} else {
   3402  1.70    bouyer 		/* switch is off */
   3403  1.70    bouyer 		if (sc->sc_rsw_status != WPI_RSW_OFF) {
   3404  1.70    bouyer 			sc->sc_rsw_status = WPI_RSW_OFF;
   3405  1.70    bouyer 			sysmon_pswitch_event(&sc->sc_rsw,
   3406  1.70    bouyer 			    PSWITCH_EVENT_RELEASED);
   3407  1.70    bouyer 		}
   3408  1.70    bouyer 	}
   3409  1.70    bouyer 
   3410  1.34  degroote 	return !(tmp & 0x01);
   3411  1.34  degroote }
   3412  1.34  degroote 
   3413  1.34  degroote static int
   3414  1.34  degroote wpi_sysctl_radio(SYSCTLFN_ARGS)
   3415  1.34  degroote {
   3416  1.34  degroote 	struct sysctlnode node;
   3417  1.34  degroote 	struct wpi_softc *sc;
   3418  1.34  degroote 	int val, error;
   3419  1.34  degroote 
   3420  1.34  degroote 	node = *rnode;
   3421  1.34  degroote 	sc = (struct wpi_softc *)node.sysctl_data;
   3422  1.34  degroote 
   3423  1.70    bouyer 	mutex_enter(&sc->sc_rsw_mtx);
   3424  1.34  degroote 	val = !wpi_getrfkill(sc);
   3425  1.70    bouyer 	mutex_exit(&sc->sc_rsw_mtx);
   3426  1.34  degroote 
   3427  1.34  degroote 	node.sysctl_data = &val;
   3428  1.34  degroote 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   3429  1.34  degroote 
   3430  1.34  degroote 	if (error || newp == NULL)
   3431  1.34  degroote 		return error;
   3432  1.34  degroote 
   3433  1.34  degroote 	return 0;
   3434  1.34  degroote }
   3435  1.34  degroote 
   3436  1.34  degroote static void
   3437  1.34  degroote wpi_sysctlattach(struct wpi_softc *sc)
   3438  1.34  degroote {
   3439  1.34  degroote 	int rc;
   3440  1.34  degroote 	const struct sysctlnode *rnode;
   3441  1.34  degroote 	const struct sysctlnode *cnode;
   3442  1.34  degroote 
   3443  1.34  degroote 	struct sysctllog **clog = &sc->sc_sysctllog;
   3444  1.34  degroote 
   3445  1.34  degroote 	if ((rc = sysctl_createv(clog, 0, NULL, &rnode,
   3446  1.34  degroote 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, device_xname(sc->sc_dev),
   3447  1.34  degroote 	    SYSCTL_DESCR("wpi controls and statistics"),
   3448  1.57     pooka 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
   3449  1.34  degroote 		goto err;
   3450  1.34  degroote 
   3451  1.34  degroote 	if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
   3452  1.34  degroote 	    CTLFLAG_PERMANENT, CTLTYPE_INT, "radio",
   3453  1.34  degroote 	    SYSCTL_DESCR("radio transmitter switch state (0=off, 1=on)"),
   3454  1.52       dsl 	    wpi_sysctl_radio, 0, (void *)sc, 0, CTL_CREATE, CTL_EOL)) != 0)
   3455  1.34  degroote 		goto err;
   3456  1.34  degroote 
   3457  1.34  degroote #ifdef WPI_DEBUG
   3458  1.34  degroote 	/* control debugging printfs */
   3459  1.34  degroote 	if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
   3460  1.34  degroote 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
   3461  1.34  degroote 	    "debug", SYSCTL_DESCR("Enable debugging output"),
   3462  1.34  degroote 	    NULL, 0, &wpi_debug, 0, CTL_CREATE, CTL_EOL)) != 0)
   3463  1.34  degroote 		goto err;
   3464  1.34  degroote #endif
   3465  1.34  degroote 
   3466  1.34  degroote 	return;
   3467  1.34  degroote err:
   3468  1.34  degroote 	aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
   3469  1.34  degroote }
   3470  1.70    bouyer 
   3471  1.70    bouyer static void
   3472  1.90  christos wpi_rsw_suspend(struct wpi_softc *sc)
   3473  1.90  christos {
   3474  1.90  christos 	/* suspend rfkill test thread */
   3475  1.90  christos 	mutex_enter(&sc->sc_rsw_mtx);
   3476  1.90  christos 	sc->sc_rsw_suspend = true;
   3477  1.90  christos 	cv_broadcast(&sc->sc_rsw_cv);
   3478  1.90  christos 	while (!sc->sc_rsw_suspended)
   3479  1.90  christos 		cv_wait(&sc->sc_rsw_cv, &sc->sc_rsw_mtx);
   3480  1.90  christos 	mutex_exit(&sc->sc_rsw_mtx);
   3481  1.90  christos }
   3482  1.90  christos 
   3483  1.90  christos static void
   3484  1.70    bouyer wpi_rsw_thread(void *arg)
   3485  1.70    bouyer {
   3486  1.70    bouyer 	struct wpi_softc *sc = (struct wpi_softc *)arg;
   3487  1.70    bouyer 
   3488  1.70    bouyer 	mutex_enter(&sc->sc_rsw_mtx);
   3489  1.70    bouyer 	for (;;) {
   3490  1.70    bouyer 		cv_timedwait(&sc->sc_rsw_cv, &sc->sc_rsw_mtx, hz);
   3491  1.70    bouyer 		if (sc->sc_dying) {
   3492  1.70    bouyer 			sc->sc_rsw_lwp = NULL;
   3493  1.70    bouyer 			cv_broadcast(&sc->sc_rsw_cv);
   3494  1.70    bouyer 			mutex_exit(&sc->sc_rsw_mtx);
   3495  1.70    bouyer 			kthread_exit(0);
   3496  1.70    bouyer 		}
   3497  1.82  riastrad 		if (sc->sc_rsw_suspend) {
   3498  1.82  riastrad 			sc->sc_rsw_suspended = true;
   3499  1.82  riastrad 			cv_broadcast(&sc->sc_rsw_cv);
   3500  1.82  riastrad 			while (sc->sc_rsw_suspend || sc->sc_dying)
   3501  1.82  riastrad 				cv_wait(&sc->sc_rsw_cv, &sc->sc_rsw_mtx);
   3502  1.82  riastrad 			sc->sc_rsw_suspended = false;
   3503  1.82  riastrad 			cv_broadcast(&sc->sc_rsw_cv);
   3504  1.82  riastrad 		}
   3505  1.70    bouyer 		wpi_getrfkill(sc);
   3506  1.70    bouyer 	}
   3507  1.70    bouyer }
   3508