Home | History | Annotate | Line # | Download | only in ic
ath.c revision 1.1.1.1
      1  1.1  dyoung /*-
      2  1.1  dyoung  * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
      3  1.1  dyoung  * All rights reserved.
      4  1.1  dyoung  *
      5  1.1  dyoung  * Redistribution and use in source and binary forms, with or without
      6  1.1  dyoung  * modification, are permitted provided that the following conditions
      7  1.1  dyoung  * are met:
      8  1.1  dyoung  * 1. Redistributions of source code must retain the above copyright
      9  1.1  dyoung  *    notice, this list of conditions and the following disclaimer,
     10  1.1  dyoung  *    without modification.
     11  1.1  dyoung  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     12  1.1  dyoung  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
     13  1.1  dyoung  *    redistribution must be conditioned upon including a substantially
     14  1.1  dyoung  *    similar Disclaimer requirement for further binary redistribution.
     15  1.1  dyoung  * 3. Neither the names of the above-listed copyright holders nor the names
     16  1.1  dyoung  *    of any contributors may be used to endorse or promote products derived
     17  1.1  dyoung  *    from this software without specific prior written permission.
     18  1.1  dyoung  *
     19  1.1  dyoung  * Alternatively, this software may be distributed under the terms of the
     20  1.1  dyoung  * GNU General Public License ("GPL") version 2 as published by the Free
     21  1.1  dyoung  * Software Foundation.
     22  1.1  dyoung  *
     23  1.1  dyoung  * NO WARRANTY
     24  1.1  dyoung  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     25  1.1  dyoung  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     26  1.1  dyoung  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
     27  1.1  dyoung  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
     28  1.1  dyoung  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
     29  1.1  dyoung  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     30  1.1  dyoung  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     31  1.1  dyoung  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
     32  1.1  dyoung  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     33  1.1  dyoung  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     34  1.1  dyoung  * THE POSSIBILITY OF SUCH DAMAGES.
     35  1.1  dyoung  */
     36  1.1  dyoung 
     37  1.1  dyoung #include <sys/cdefs.h>
     38  1.1  dyoung __FBSDID("$FreeBSD: src/sys/dev/ath/if_ath.c,v 1.14 2003/09/05 22:22:49 sam Exp $");
     39  1.1  dyoung 
     40  1.1  dyoung /*
     41  1.1  dyoung  * Driver for the Atheros Wireless LAN controller.
     42  1.1  dyoung  *
     43  1.1  dyoung  * This software is derived from work of Atsushi Onoe; his contribution
     44  1.1  dyoung  * is greatly appreciated.
     45  1.1  dyoung  */
     46  1.1  dyoung 
     47  1.1  dyoung #include "opt_inet.h"
     48  1.1  dyoung 
     49  1.1  dyoung #include <sys/param.h>
     50  1.1  dyoung #include <sys/systm.h>
     51  1.1  dyoung #include <sys/sysctl.h>
     52  1.1  dyoung #include <sys/mbuf.h>
     53  1.1  dyoung #include <sys/malloc.h>
     54  1.1  dyoung #include <sys/lock.h>
     55  1.1  dyoung #include <sys/mutex.h>
     56  1.1  dyoung #include <sys/kernel.h>
     57  1.1  dyoung #include <sys/socket.h>
     58  1.1  dyoung #include <sys/sockio.h>
     59  1.1  dyoung #include <sys/errno.h>
     60  1.1  dyoung #include <sys/callout.h>
     61  1.1  dyoung #include <sys/bus.h>
     62  1.1  dyoung #include <sys/endian.h>
     63  1.1  dyoung 
     64  1.1  dyoung #include <machine/bus.h>
     65  1.1  dyoung 
     66  1.1  dyoung #include <net/if.h>
     67  1.1  dyoung #include <net/if_dl.h>
     68  1.1  dyoung #include <net/if_media.h>
     69  1.1  dyoung #include <net/if_arp.h>
     70  1.1  dyoung #include <net/ethernet.h>
     71  1.1  dyoung #include <net/if_llc.h>
     72  1.1  dyoung 
     73  1.1  dyoung #include <net80211/ieee80211_var.h>
     74  1.1  dyoung 
     75  1.1  dyoung #include <net/bpf.h>
     76  1.1  dyoung 
     77  1.1  dyoung #ifdef INET
     78  1.1  dyoung #include <netinet/in.h>
     79  1.1  dyoung #include <netinet/if_ether.h>
     80  1.1  dyoung #endif
     81  1.1  dyoung 
     82  1.1  dyoung #define	AR_DEBUG
     83  1.1  dyoung #include <dev/ath/if_athvar.h>
     84  1.1  dyoung #include <contrib/dev/ath/ah_desc.h>
     85  1.1  dyoung 
     86  1.1  dyoung /* unalligned little endian access */
     87  1.1  dyoung #define LE_READ_2(p)							\
     88  1.1  dyoung 	((u_int16_t)							\
     89  1.1  dyoung 	 ((((u_int8_t *)(p))[0]      ) | (((u_int8_t *)(p))[1] <<  8)))
     90  1.1  dyoung #define LE_READ_4(p)							\
     91  1.1  dyoung 	((u_int32_t)							\
     92  1.1  dyoung 	 ((((u_int8_t *)(p))[0]      ) | (((u_int8_t *)(p))[1] <<  8) |	\
     93  1.1  dyoung 	  (((u_int8_t *)(p))[2] << 16) | (((u_int8_t *)(p))[3] << 24)))
     94  1.1  dyoung 
     95  1.1  dyoung static void	ath_init(void *);
     96  1.1  dyoung static void	ath_stop(struct ifnet *);
     97  1.1  dyoung static void	ath_start(struct ifnet *);
     98  1.1  dyoung static void	ath_reset(struct ath_softc *);
     99  1.1  dyoung static int	ath_media_change(struct ifnet *);
    100  1.1  dyoung static void	ath_watchdog(struct ifnet *);
    101  1.1  dyoung static int	ath_ioctl(struct ifnet *, u_long, caddr_t);
    102  1.1  dyoung static void	ath_fatal_proc(void *, int);
    103  1.1  dyoung static void	ath_rxorn_proc(void *, int);
    104  1.1  dyoung static void	ath_bmiss_proc(void *, int);
    105  1.1  dyoung static void	ath_initkeytable(struct ath_softc *);
    106  1.1  dyoung static void	ath_mode_init(struct ath_softc *);
    107  1.1  dyoung static int	ath_beacon_alloc(struct ath_softc *, struct ieee80211_node *);
    108  1.1  dyoung static void	ath_beacon_proc(void *, int);
    109  1.1  dyoung static void	ath_beacon_free(struct ath_softc *);
    110  1.1  dyoung static void	ath_beacon_config(struct ath_softc *);
    111  1.1  dyoung static int	ath_desc_alloc(struct ath_softc *);
    112  1.1  dyoung static void	ath_desc_free(struct ath_softc *);
    113  1.1  dyoung static struct ieee80211_node *ath_node_alloc(struct ieee80211com *);
    114  1.1  dyoung static void	ath_node_free(struct ieee80211com *, struct ieee80211_node *);
    115  1.1  dyoung static void	ath_node_copy(struct ieee80211com *,
    116  1.1  dyoung 			struct ieee80211_node *, const struct ieee80211_node *);
    117  1.1  dyoung static int	ath_rxbuf_init(struct ath_softc *, struct ath_buf *);
    118  1.1  dyoung static void	ath_rx_proc(void *, int);
    119  1.1  dyoung static int	ath_tx_start(struct ath_softc *, struct ieee80211_node *,
    120  1.1  dyoung 			     struct ath_buf *, struct mbuf *);
    121  1.1  dyoung static void	ath_tx_proc(void *, int);
    122  1.1  dyoung static int	ath_chan_set(struct ath_softc *, struct ieee80211_channel *);
    123  1.1  dyoung static void	ath_draintxq(struct ath_softc *);
    124  1.1  dyoung static void	ath_stoprecv(struct ath_softc *);
    125  1.1  dyoung static int	ath_startrecv(struct ath_softc *);
    126  1.1  dyoung static void	ath_next_scan(void *);
    127  1.1  dyoung static void	ath_calibrate(void *);
    128  1.1  dyoung static int	ath_newstate(struct ieee80211com *, enum ieee80211_state, int);
    129  1.1  dyoung static void	ath_newassoc(struct ieee80211com *,
    130  1.1  dyoung 			struct ieee80211_node *, int);
    131  1.1  dyoung static int	ath_getchannels(struct ath_softc *, u_int cc, HAL_BOOL outdoor);
    132  1.1  dyoung 
    133  1.1  dyoung static int	ath_rate_setup(struct ath_softc *sc, u_int mode);
    134  1.1  dyoung static void	ath_setcurmode(struct ath_softc *, enum ieee80211_phymode);
    135  1.1  dyoung static void	ath_rate_ctl_reset(struct ath_softc *, enum ieee80211_state);
    136  1.1  dyoung static void	ath_rate_ctl(void *, struct ieee80211_node *);
    137  1.1  dyoung 
    138  1.1  dyoung SYSCTL_DECL(_hw_ath);
    139  1.1  dyoung 
    140  1.1  dyoung /* XXX validate sysctl values */
    141  1.1  dyoung static	int ath_dwelltime = 200;		/* 5 channels/second */
    142  1.1  dyoung SYSCTL_INT(_hw_ath, OID_AUTO, dwell, CTLFLAG_RW, &ath_dwelltime,
    143  1.1  dyoung 	    0, "channel dwell time (ms) for AP/station scanning");
    144  1.1  dyoung static	int ath_calinterval = 30;		/* calibrate every 30 secs */
    145  1.1  dyoung SYSCTL_INT(_hw_ath, OID_AUTO, calibrate, CTLFLAG_RW, &ath_calinterval,
    146  1.1  dyoung 	    0, "chip calibration interval (secs)");
    147  1.1  dyoung static	int ath_outdoor = AH_TRUE;		/* outdoor operation */
    148  1.1  dyoung SYSCTL_INT(_hw_ath, OID_AUTO, outdoor, CTLFLAG_RD, &ath_outdoor,
    149  1.1  dyoung 	    0, "enable/disable outdoor operation");
    150  1.1  dyoung static	int ath_countrycode = CTRY_DEFAULT;	/* country code */
    151  1.1  dyoung SYSCTL_INT(_hw_ath, OID_AUTO, countrycode, CTLFLAG_RD, &ath_countrycode,
    152  1.1  dyoung 	    0, "country code");
    153  1.1  dyoung static	int ath_regdomain = 0;			/* regulatory domain */
    154  1.1  dyoung SYSCTL_INT(_hw_ath, OID_AUTO, regdomain, CTLFLAG_RD, &ath_regdomain,
    155  1.1  dyoung 	    0, "regulatory domain");
    156  1.1  dyoung 
    157  1.1  dyoung #ifdef AR_DEBUG
    158  1.1  dyoung int	ath_debug = 0;
    159  1.1  dyoung SYSCTL_INT(_hw_ath, OID_AUTO, debug, CTLFLAG_RW, &ath_debug,
    160  1.1  dyoung 	    0, "control debugging printfs");
    161  1.1  dyoung #define	IFF_DUMPPKTS(_ifp) \
    162  1.1  dyoung 	(ath_debug || \
    163  1.1  dyoung 	    ((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
    164  1.1  dyoung static	void ath_printrxbuf(struct ath_buf *bf, int);
    165  1.1  dyoung static	void ath_printtxbuf(struct ath_buf *bf, int);
    166  1.1  dyoung #define	DPRINTF(X)	if (ath_debug) printf X
    167  1.1  dyoung #define	DPRINTF2(X)	if (ath_debug > 1) printf X
    168  1.1  dyoung #else
    169  1.1  dyoung #define	IFF_DUMPPKTS(_ifp) \
    170  1.1  dyoung 	(((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
    171  1.1  dyoung #define	DPRINTF(X)
    172  1.1  dyoung #define	DPRINTF2(X)
    173  1.1  dyoung #endif
    174  1.1  dyoung 
    175  1.1  dyoung int
    176  1.1  dyoung ath_attach(u_int16_t devid, struct ath_softc *sc)
    177  1.1  dyoung {
    178  1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
    179  1.1  dyoung 	struct ifnet *ifp = &ic->ic_if;
    180  1.1  dyoung 	struct ath_hal *ah;
    181  1.1  dyoung 	HAL_STATUS status;
    182  1.1  dyoung 	int error = 0;
    183  1.1  dyoung 
    184  1.1  dyoung 	DPRINTF(("ath_attach: devid 0x%x\n", devid));
    185  1.1  dyoung 
    186  1.1  dyoung 	/* set these up early for if_printf use */
    187  1.1  dyoung 	ifp->if_unit = device_get_unit(sc->sc_dev);
    188  1.1  dyoung 	ifp->if_name = "ath";
    189  1.1  dyoung 
    190  1.1  dyoung 	ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh, &status);
    191  1.1  dyoung 	if (ah == NULL) {
    192  1.1  dyoung 		if_printf(ifp, "unable to attach hardware; HAL status %u\n",
    193  1.1  dyoung 			status);
    194  1.1  dyoung 		error = ENXIO;
    195  1.1  dyoung 		goto bad;
    196  1.1  dyoung 	}
    197  1.1  dyoung 	sc->sc_ah = ah;
    198  1.1  dyoung 	sc->sc_invalid = 0;	/* ready to go, enable interrupt handling */
    199  1.1  dyoung 
    200  1.1  dyoung 	/*
    201  1.1  dyoung 	 * Collect the channel list using the default country
    202  1.1  dyoung 	 * code and including outdoor channels.  The 802.11 layer
    203  1.1  dyoung 	 * is resposible for filtering this list based on settings
    204  1.1  dyoung 	 * like the phy mode.
    205  1.1  dyoung 	 */
    206  1.1  dyoung 	error = ath_getchannels(sc, ath_countrycode, ath_outdoor);
    207  1.1  dyoung 	if (error != 0)
    208  1.1  dyoung 		goto bad;
    209  1.1  dyoung 	/*
    210  1.1  dyoung 	 * Copy these back; they are set as a side effect
    211  1.1  dyoung 	 * of constructing the channel list.
    212  1.1  dyoung 	 */
    213  1.1  dyoung 	ath_regdomain = ath_hal_getregdomain(ah);
    214  1.1  dyoung 	ath_countrycode = ath_hal_getcountrycode(ah);
    215  1.1  dyoung 
    216  1.1  dyoung 	/*
    217  1.1  dyoung 	 * Setup rate tables for all potential media types.
    218  1.1  dyoung 	 */
    219  1.1  dyoung 	ath_rate_setup(sc, IEEE80211_MODE_11A);
    220  1.1  dyoung 	ath_rate_setup(sc, IEEE80211_MODE_11B);
    221  1.1  dyoung 	ath_rate_setup(sc, IEEE80211_MODE_11G);
    222  1.1  dyoung 	ath_rate_setup(sc, IEEE80211_MODE_TURBO);
    223  1.1  dyoung 
    224  1.1  dyoung 	error = ath_desc_alloc(sc);
    225  1.1  dyoung 	if (error != 0) {
    226  1.1  dyoung 		if_printf(ifp, "failed to allocate descriptors: %d\n", error);
    227  1.1  dyoung 		goto bad;
    228  1.1  dyoung 	}
    229  1.1  dyoung 	callout_init(&sc->sc_scan_ch, CALLOUT_MPSAFE);
    230  1.1  dyoung 	callout_init(&sc->sc_cal_ch, CALLOUT_MPSAFE);
    231  1.1  dyoung 
    232  1.1  dyoung 	mtx_init(&sc->sc_txbuflock,
    233  1.1  dyoung 		device_get_nameunit(sc->sc_dev), "xmit buf q", MTX_DEF);
    234  1.1  dyoung 	mtx_init(&sc->sc_txqlock,
    235  1.1  dyoung 		device_get_nameunit(sc->sc_dev), "xmit q", MTX_DEF);
    236  1.1  dyoung 
    237  1.1  dyoung 	TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc, sc);
    238  1.1  dyoung 	TASK_INIT(&sc->sc_rxtask, 0, ath_rx_proc, sc);
    239  1.1  dyoung 	TASK_INIT(&sc->sc_swbatask, 0, ath_beacon_proc, sc);
    240  1.1  dyoung 	TASK_INIT(&sc->sc_rxorntask, 0, ath_rxorn_proc, sc);
    241  1.1  dyoung 	TASK_INIT(&sc->sc_fataltask, 0, ath_fatal_proc, sc);
    242  1.1  dyoung 	TASK_INIT(&sc->sc_bmisstask, 0, ath_bmiss_proc, sc);
    243  1.1  dyoung 
    244  1.1  dyoung 	/*
    245  1.1  dyoung 	 * For now just pre-allocate one data queue and one
    246  1.1  dyoung 	 * beacon queue.  Note that the HAL handles resetting
    247  1.1  dyoung 	 * them at the needed time.  Eventually we'll want to
    248  1.1  dyoung 	 * allocate more tx queues for splitting management
    249  1.1  dyoung 	 * frames and for QOS support.
    250  1.1  dyoung 	 */
    251  1.1  dyoung 	sc->sc_txhalq = ath_hal_setuptxqueue(ah,
    252  1.1  dyoung 		HAL_TX_QUEUE_DATA,
    253  1.1  dyoung 		AH_TRUE			/* enable interrupts */
    254  1.1  dyoung 	);
    255  1.1  dyoung 	if (sc->sc_txhalq == (u_int) -1) {
    256  1.1  dyoung 		if_printf(ifp, "unable to setup a data xmit queue!\n");
    257  1.1  dyoung 		goto bad;
    258  1.1  dyoung 	}
    259  1.1  dyoung 	sc->sc_bhalq = ath_hal_setuptxqueue(ah,
    260  1.1  dyoung 		HAL_TX_QUEUE_BEACON,
    261  1.1  dyoung 		AH_TRUE			/* enable interrupts */
    262  1.1  dyoung 	);
    263  1.1  dyoung 	if (sc->sc_bhalq == (u_int) -1) {
    264  1.1  dyoung 		if_printf(ifp, "unable to setup a beacon xmit queue!\n");
    265  1.1  dyoung 		goto bad;
    266  1.1  dyoung 	}
    267  1.1  dyoung 
    268  1.1  dyoung 	ifp->if_softc = sc;
    269  1.1  dyoung 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
    270  1.1  dyoung 	ifp->if_start = ath_start;
    271  1.1  dyoung 	ifp->if_watchdog = ath_watchdog;
    272  1.1  dyoung 	ifp->if_ioctl = ath_ioctl;
    273  1.1  dyoung 	ifp->if_init = ath_init;
    274  1.1  dyoung 	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
    275  1.1  dyoung 
    276  1.1  dyoung 	ic->ic_softc = sc;
    277  1.1  dyoung 	ic->ic_newassoc = ath_newassoc;
    278  1.1  dyoung 	/* XXX not right but it's not used anywhere important */
    279  1.1  dyoung 	ic->ic_phytype = IEEE80211_T_OFDM;
    280  1.1  dyoung 	ic->ic_opmode = IEEE80211_M_STA;
    281  1.1  dyoung 	ic->ic_caps = IEEE80211_C_WEP | IEEE80211_C_IBSS | IEEE80211_C_HOSTAP
    282  1.1  dyoung 		| IEEE80211_C_MONITOR;
    283  1.1  dyoung 	/* NB: 11g support is identified when we fetch the channel set */
    284  1.1  dyoung 	if (sc->sc_have11g)
    285  1.1  dyoung 		ic->ic_caps |= IEEE80211_C_SHPREAMBLE;
    286  1.1  dyoung 
    287  1.1  dyoung 	/* get mac address from hardware */
    288  1.1  dyoung 	ath_hal_getmac(ah, ic->ic_myaddr);
    289  1.1  dyoung 
    290  1.1  dyoung 	/* call MI attach routine. */
    291  1.1  dyoung 	ieee80211_ifattach(ifp);
    292  1.1  dyoung 	/* override default methods */
    293  1.1  dyoung 	ic->ic_node_alloc = ath_node_alloc;
    294  1.1  dyoung 	ic->ic_node_free = ath_node_free;
    295  1.1  dyoung 	ic->ic_node_copy = ath_node_copy;
    296  1.1  dyoung 	sc->sc_newstate = ic->ic_newstate;
    297  1.1  dyoung 	ic->ic_newstate = ath_newstate;
    298  1.1  dyoung 	/* complete initialization */
    299  1.1  dyoung 	ieee80211_media_init(ifp, ath_media_change, ieee80211_media_status);
    300  1.1  dyoung 
    301  1.1  dyoung 	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
    302  1.1  dyoung 		sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th),
    303  1.1  dyoung 		&sc->sc_drvbpf);
    304  1.1  dyoung 	/*
    305  1.1  dyoung 	 * Initialize constant fields.
    306  1.1  dyoung 	 *
    307  1.1  dyoung 	 * NB: the channel is setup each time we transition to the
    308  1.1  dyoung 	 *     RUN state to avoid filling it in for each frame.
    309  1.1  dyoung 	 */
    310  1.1  dyoung 	sc->sc_tx_th.wt_ihdr.it_len = sizeof(sc->sc_tx_th);
    311  1.1  dyoung 	sc->sc_tx_th.wt_ihdr.it_present = ATH_TX_RADIOTAP_PRESENT;
    312  1.1  dyoung 
    313  1.1  dyoung 	sc->sc_rx_th.wr_ihdr.it_len = sizeof(sc->sc_rx_th);
    314  1.1  dyoung 	sc->sc_rx_th.wr_ihdr.it_present = ATH_RX_RADIOTAP_PRESENT;
    315  1.1  dyoung 
    316  1.1  dyoung 	if_printf(ifp, "802.11 address: %s\n", ether_sprintf(ic->ic_myaddr));
    317  1.1  dyoung 
    318  1.1  dyoung 	return 0;
    319  1.1  dyoung bad:
    320  1.1  dyoung 	if (ah)
    321  1.1  dyoung 		ath_hal_detach(ah);
    322  1.1  dyoung 	sc->sc_invalid = 1;
    323  1.1  dyoung 	return error;
    324  1.1  dyoung }
    325  1.1  dyoung 
    326  1.1  dyoung int
    327  1.1  dyoung ath_detach(struct ath_softc *sc)
    328  1.1  dyoung {
    329  1.1  dyoung 	struct ifnet *ifp = &sc->sc_ic.ic_if;
    330  1.1  dyoung 
    331  1.1  dyoung 	DPRINTF(("ath_detach: if_flags %x\n", ifp->if_flags));
    332  1.1  dyoung 
    333  1.1  dyoung 	mtx_lock(&sc->sc_mtx);
    334  1.1  dyoung 	ath_stop(ifp);
    335  1.1  dyoung 	bpfdetach(ifp);
    336  1.1  dyoung 	ath_desc_free(sc);
    337  1.1  dyoung 	ath_hal_detach(sc->sc_ah);
    338  1.1  dyoung 	ieee80211_ifdetach(ifp);
    339  1.1  dyoung 	mtx_unlock(&sc->sc_mtx);
    340  1.1  dyoung 	return 0;
    341  1.1  dyoung }
    342  1.1  dyoung 
    343  1.1  dyoung void
    344  1.1  dyoung ath_suspend(struct ath_softc *sc)
    345  1.1  dyoung {
    346  1.1  dyoung 	struct ifnet *ifp = &sc->sc_ic.ic_if;
    347  1.1  dyoung 
    348  1.1  dyoung 	DPRINTF(("ath_suspend: if_flags %x\n", ifp->if_flags));
    349  1.1  dyoung 
    350  1.1  dyoung 	ath_stop(ifp);
    351  1.1  dyoung }
    352  1.1  dyoung 
    353  1.1  dyoung void
    354  1.1  dyoung ath_resume(struct ath_softc *sc)
    355  1.1  dyoung {
    356  1.1  dyoung 	struct ifnet *ifp = &sc->sc_ic.ic_if;
    357  1.1  dyoung 
    358  1.1  dyoung 	DPRINTF(("ath_resume: if_flags %x\n", ifp->if_flags));
    359  1.1  dyoung 
    360  1.1  dyoung 	if (ifp->if_flags & IFF_UP) {
    361  1.1  dyoung 		ath_init(ifp);
    362  1.1  dyoung 		if (ifp->if_flags & IFF_RUNNING)
    363  1.1  dyoung 			ath_start(ifp);
    364  1.1  dyoung 	}
    365  1.1  dyoung }
    366  1.1  dyoung 
    367  1.1  dyoung void
    368  1.1  dyoung ath_shutdown(struct ath_softc *sc)
    369  1.1  dyoung {
    370  1.1  dyoung 	struct ifnet *ifp = &sc->sc_ic.ic_if;
    371  1.1  dyoung 
    372  1.1  dyoung 	DPRINTF(("ath_shutdown: if_flags %x\n", ifp->if_flags));
    373  1.1  dyoung 
    374  1.1  dyoung 	ath_stop(ifp);
    375  1.1  dyoung }
    376  1.1  dyoung 
    377  1.1  dyoung void
    378  1.1  dyoung ath_intr(void *arg)
    379  1.1  dyoung {
    380  1.1  dyoung 	struct ath_softc *sc = arg;
    381  1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
    382  1.1  dyoung 	struct ifnet *ifp = &ic->ic_if;
    383  1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
    384  1.1  dyoung 	HAL_INT status;
    385  1.1  dyoung 
    386  1.1  dyoung 	if (sc->sc_invalid) {
    387  1.1  dyoung 		/*
    388  1.1  dyoung 		 * The hardware is not ready/present, don't touch anything.
    389  1.1  dyoung 		 * Note this can happen early on if the IRQ is shared.
    390  1.1  dyoung 		 */
    391  1.1  dyoung 		DPRINTF(("ath_intr: invalid; ignored\n"));
    392  1.1  dyoung 		return;
    393  1.1  dyoung 	}
    394  1.1  dyoung 	if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) != (IFF_RUNNING|IFF_UP)) {
    395  1.1  dyoung 		DPRINTF(("ath_intr: if_flags 0x%x\n", ifp->if_flags));
    396  1.1  dyoung 		ath_hal_getisr(ah, &status);	/* clear ISR */
    397  1.1  dyoung 		ath_hal_intrset(ah, 0);		/* disable further intr's */
    398  1.1  dyoung 		return;
    399  1.1  dyoung 	}
    400  1.1  dyoung 	ath_hal_getisr(ah, &status);		/* NB: clears ISR too */
    401  1.1  dyoung 	DPRINTF2(("ath_intr: status 0x%x\n", status));
    402  1.1  dyoung #ifdef AR_DEBUG
    403  1.1  dyoung 	if (ath_debug &&
    404  1.1  dyoung 	    (status & (HAL_INT_FATAL|HAL_INT_RXORN|HAL_INT_BMISS))) {
    405  1.1  dyoung 		if_printf(ifp, "ath_intr: status 0x%x\n", status);
    406  1.1  dyoung 		ath_hal_dumpstate(ah);
    407  1.1  dyoung 	}
    408  1.1  dyoung #endif /* AR_DEBUG */
    409  1.1  dyoung 	if (status & HAL_INT_FATAL) {
    410  1.1  dyoung 		sc->sc_stats.ast_hardware++;
    411  1.1  dyoung 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
    412  1.1  dyoung 		taskqueue_enqueue(taskqueue_swi, &sc->sc_fataltask);
    413  1.1  dyoung 	} else if (status & HAL_INT_RXORN) {
    414  1.1  dyoung 		sc->sc_stats.ast_rxorn++;
    415  1.1  dyoung 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
    416  1.1  dyoung 		taskqueue_enqueue(taskqueue_swi, &sc->sc_rxorntask);
    417  1.1  dyoung 	} else {
    418  1.1  dyoung 		if (status & HAL_INT_RXEOL) {
    419  1.1  dyoung 			/*
    420  1.1  dyoung 			 * NB: the hardware should re-read the link when
    421  1.1  dyoung 			 *     RXE bit is written, but it doesn't work at
    422  1.1  dyoung 			 *     least on older hardware revs.
    423  1.1  dyoung 			 */
    424  1.1  dyoung 			sc->sc_stats.ast_rxeol++;
    425  1.1  dyoung 			sc->sc_rxlink = NULL;
    426  1.1  dyoung 		}
    427  1.1  dyoung 		if (status & HAL_INT_TXURN) {
    428  1.1  dyoung 			sc->sc_stats.ast_txurn++;
    429  1.1  dyoung 			/* bump tx trigger level */
    430  1.1  dyoung 			ath_hal_updatetxtriglevel(ah, AH_TRUE);
    431  1.1  dyoung 		}
    432  1.1  dyoung 		if (status & HAL_INT_RX)
    433  1.1  dyoung 			taskqueue_enqueue(taskqueue_swi, &sc->sc_rxtask);
    434  1.1  dyoung 		if (status & HAL_INT_TX)
    435  1.1  dyoung 			taskqueue_enqueue(taskqueue_swi, &sc->sc_txtask);
    436  1.1  dyoung 		if (status & HAL_INT_SWBA)
    437  1.1  dyoung 			taskqueue_enqueue(taskqueue_swi, &sc->sc_swbatask);
    438  1.1  dyoung 		if (status & HAL_INT_BMISS) {
    439  1.1  dyoung 			sc->sc_stats.ast_bmiss++;
    440  1.1  dyoung 			taskqueue_enqueue(taskqueue_swi, &sc->sc_bmisstask);
    441  1.1  dyoung 		}
    442  1.1  dyoung 	}
    443  1.1  dyoung }
    444  1.1  dyoung 
    445  1.1  dyoung static void
    446  1.1  dyoung ath_fatal_proc(void *arg, int pending)
    447  1.1  dyoung {
    448  1.1  dyoung 	struct ath_softc *sc = arg;
    449  1.1  dyoung 
    450  1.1  dyoung 	device_printf(sc->sc_dev, "hardware error; resetting\n");
    451  1.1  dyoung 	ath_reset(sc);
    452  1.1  dyoung }
    453  1.1  dyoung 
    454  1.1  dyoung static void
    455  1.1  dyoung ath_rxorn_proc(void *arg, int pending)
    456  1.1  dyoung {
    457  1.1  dyoung 	struct ath_softc *sc = arg;
    458  1.1  dyoung 
    459  1.1  dyoung 	device_printf(sc->sc_dev, "rx FIFO overrun; resetting\n");
    460  1.1  dyoung 	ath_reset(sc);
    461  1.1  dyoung }
    462  1.1  dyoung 
    463  1.1  dyoung static void
    464  1.1  dyoung ath_bmiss_proc(void *arg, int pending)
    465  1.1  dyoung {
    466  1.1  dyoung 	struct ath_softc *sc = arg;
    467  1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
    468  1.1  dyoung 
    469  1.1  dyoung 	DPRINTF(("ath_bmiss_proc: pending %u\n", pending));
    470  1.1  dyoung 	KASSERT(ic->ic_opmode == IEEE80211_M_STA,
    471  1.1  dyoung 		("unexpect operating mode %u", ic->ic_opmode));
    472  1.1  dyoung 	if (ic->ic_state == IEEE80211_S_RUN)
    473  1.1  dyoung 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
    474  1.1  dyoung }
    475  1.1  dyoung 
    476  1.1  dyoung static u_int
    477  1.1  dyoung ath_chan2flags(struct ieee80211com *ic, struct ieee80211_channel *chan)
    478  1.1  dyoung {
    479  1.1  dyoung 	static const u_int modeflags[] = {
    480  1.1  dyoung 		0,			/* IEEE80211_MODE_AUTO */
    481  1.1  dyoung 		CHANNEL_A,		/* IEEE80211_MODE_11A */
    482  1.1  dyoung 		CHANNEL_B,		/* IEEE80211_MODE_11B */
    483  1.1  dyoung 		CHANNEL_PUREG,		/* IEEE80211_MODE_11G */
    484  1.1  dyoung 		CHANNEL_T		/* IEEE80211_MODE_TURBO */
    485  1.1  dyoung 	};
    486  1.1  dyoung 	return modeflags[ieee80211_chan2mode(ic, chan)];
    487  1.1  dyoung }
    488  1.1  dyoung 
    489  1.1  dyoung static void
    490  1.1  dyoung ath_init(void *arg)
    491  1.1  dyoung {
    492  1.1  dyoung 	struct ath_softc *sc = (struct ath_softc *) arg;
    493  1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
    494  1.1  dyoung 	struct ifnet *ifp = &ic->ic_if;
    495  1.1  dyoung 	struct ieee80211_node *ni;
    496  1.1  dyoung 	enum ieee80211_phymode mode;
    497  1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
    498  1.1  dyoung 	HAL_STATUS status;
    499  1.1  dyoung 	HAL_CHANNEL hchan;
    500  1.1  dyoung 
    501  1.1  dyoung 	DPRINTF(("ath_init: if_flags 0x%x\n", ifp->if_flags));
    502  1.1  dyoung 
    503  1.1  dyoung 	mtx_lock(&sc->sc_mtx);
    504  1.1  dyoung 	/*
    505  1.1  dyoung 	 * Stop anything previously setup.  This is safe
    506  1.1  dyoung 	 * whether this is the first time through or not.
    507  1.1  dyoung 	 */
    508  1.1  dyoung 	ath_stop(ifp);
    509  1.1  dyoung 
    510  1.1  dyoung 	/*
    511  1.1  dyoung 	 * The basic interface to setting the hardware in a good
    512  1.1  dyoung 	 * state is ``reset''.  On return the hardware is known to
    513  1.1  dyoung 	 * be powered up and with interrupts disabled.  This must
    514  1.1  dyoung 	 * be followed by initialization of the appropriate bits
    515  1.1  dyoung 	 * and then setup of the interrupt mask.
    516  1.1  dyoung 	 */
    517  1.1  dyoung 	hchan.channel = ic->ic_ibss_chan->ic_freq;
    518  1.1  dyoung 	hchan.channelFlags = ath_chan2flags(ic, ic->ic_ibss_chan);
    519  1.1  dyoung 	if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_FALSE, &status)) {
    520  1.1  dyoung 		if_printf(ifp, "unable to reset hardware; hal status %u\n",
    521  1.1  dyoung 			status);
    522  1.1  dyoung 		goto done;
    523  1.1  dyoung 	}
    524  1.1  dyoung 
    525  1.1  dyoung 	/*
    526  1.1  dyoung 	 * Setup the hardware after reset: the key cache
    527  1.1  dyoung 	 * is filled as needed and the receive engine is
    528  1.1  dyoung 	 * set going.  Frame transmit is handled entirely
    529  1.1  dyoung 	 * in the frame output path; there's nothing to do
    530  1.1  dyoung 	 * here except setup the interrupt mask.
    531  1.1  dyoung 	 */
    532  1.1  dyoung 	if (ic->ic_flags & IEEE80211_F_WEPON)
    533  1.1  dyoung 		ath_initkeytable(sc);
    534  1.1  dyoung 	if (ath_startrecv(sc) != 0) {
    535  1.1  dyoung 		if_printf(ifp, "unable to start recv logic\n");
    536  1.1  dyoung 		goto done;
    537  1.1  dyoung 	}
    538  1.1  dyoung 
    539  1.1  dyoung 	/*
    540  1.1  dyoung 	 * Enable interrupts.
    541  1.1  dyoung 	 */
    542  1.1  dyoung 	sc->sc_imask = HAL_INT_RX | HAL_INT_TX
    543  1.1  dyoung 		  | HAL_INT_RXEOL | HAL_INT_RXORN
    544  1.1  dyoung 		  | HAL_INT_FATAL | HAL_INT_GLOBAL;
    545  1.1  dyoung 	ath_hal_intrset(ah, sc->sc_imask);
    546  1.1  dyoung 
    547  1.1  dyoung 	ifp->if_flags |= IFF_RUNNING;
    548  1.1  dyoung 	ic->ic_state = IEEE80211_S_INIT;
    549  1.1  dyoung 
    550  1.1  dyoung 	/*
    551  1.1  dyoung 	 * The hardware should be ready to go now so it's safe
    552  1.1  dyoung 	 * to kick the 802.11 state machine as it's likely to
    553  1.1  dyoung 	 * immediately call back to us to send mgmt frames.
    554  1.1  dyoung 	 */
    555  1.1  dyoung 	ni = ic->ic_bss;
    556  1.1  dyoung 	ni->ni_chan = ic->ic_ibss_chan;
    557  1.1  dyoung 	mode = ieee80211_chan2mode(ic, ni->ni_chan);
    558  1.1  dyoung 	if (mode != sc->sc_curmode)
    559  1.1  dyoung 		ath_setcurmode(sc, mode);
    560  1.1  dyoung 	if (ic->ic_opmode != IEEE80211_M_MONITOR)
    561  1.1  dyoung 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
    562  1.1  dyoung 	else
    563  1.1  dyoung 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
    564  1.1  dyoung done:
    565  1.1  dyoung 	mtx_unlock(&sc->sc_mtx);
    566  1.1  dyoung }
    567  1.1  dyoung 
    568  1.1  dyoung static void
    569  1.1  dyoung ath_stop(struct ifnet *ifp)
    570  1.1  dyoung {
    571  1.1  dyoung 	struct ieee80211com *ic = (struct ieee80211com *) ifp;
    572  1.1  dyoung 	struct ath_softc *sc = ifp->if_softc;
    573  1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
    574  1.1  dyoung 
    575  1.1  dyoung 	DPRINTF(("ath_stop: invalid %u if_flags 0x%x\n",
    576  1.1  dyoung 		sc->sc_invalid, ifp->if_flags));
    577  1.1  dyoung 
    578  1.1  dyoung 	mtx_lock(&sc->sc_mtx);
    579  1.1  dyoung 	if (ifp->if_flags & IFF_RUNNING) {
    580  1.1  dyoung 		/*
    581  1.1  dyoung 		 * Shutdown the hardware and driver:
    582  1.1  dyoung 		 *    disable interrupts
    583  1.1  dyoung 		 *    turn off timers
    584  1.1  dyoung 		 *    clear transmit machinery
    585  1.1  dyoung 		 *    clear receive machinery
    586  1.1  dyoung 		 *    drain and release tx queues
    587  1.1  dyoung 		 *    reclaim beacon resources
    588  1.1  dyoung 		 *    reset 802.11 state machine
    589  1.1  dyoung 		 *    power down hardware
    590  1.1  dyoung 		 *
    591  1.1  dyoung 		 * Note that some of this work is not possible if the
    592  1.1  dyoung 		 * hardware is gone (invalid).
    593  1.1  dyoung 		 */
    594  1.1  dyoung 		ifp->if_flags &= ~IFF_RUNNING;
    595  1.1  dyoung 		ifp->if_timer = 0;
    596  1.1  dyoung 		if (!sc->sc_invalid)
    597  1.1  dyoung 			ath_hal_intrset(ah, 0);
    598  1.1  dyoung 		ath_draintxq(sc);
    599  1.1  dyoung 		if (!sc->sc_invalid)
    600  1.1  dyoung 			ath_stoprecv(sc);
    601  1.1  dyoung 		else
    602  1.1  dyoung 			sc->sc_rxlink = NULL;
    603  1.1  dyoung 		IF_DRAIN(&ifp->if_snd);
    604  1.1  dyoung 		ath_beacon_free(sc);
    605  1.1  dyoung 		ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
    606  1.1  dyoung 		if (!sc->sc_invalid)
    607  1.1  dyoung 			ath_hal_setpower(ah, HAL_PM_FULL_SLEEP, 0);
    608  1.1  dyoung 	}
    609  1.1  dyoung 	mtx_unlock(&sc->sc_mtx);
    610  1.1  dyoung }
    611  1.1  dyoung 
    612  1.1  dyoung /*
    613  1.1  dyoung  * Reset the hardware w/o losing operational state.  This is
    614  1.1  dyoung  * basically a more efficient way of doing ath_stop, ath_init,
    615  1.1  dyoung  * followed by state transitions to the current 802.11
    616  1.1  dyoung  * operational state.  Used to recover from errors rx overrun
    617  1.1  dyoung  * and to reset the hardware when rf gain settings must be reset.
    618  1.1  dyoung  */
    619  1.1  dyoung static void
    620  1.1  dyoung ath_reset(struct ath_softc *sc)
    621  1.1  dyoung {
    622  1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
    623  1.1  dyoung 	struct ifnet *ifp = &ic->ic_if;
    624  1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
    625  1.1  dyoung 	struct ieee80211_channel *c;
    626  1.1  dyoung 	HAL_STATUS status;
    627  1.1  dyoung 	HAL_CHANNEL hchan;
    628  1.1  dyoung 
    629  1.1  dyoung 	/*
    630  1.1  dyoung 	 * Convert to a HAL channel description with the flags
    631  1.1  dyoung 	 * constrained to reflect the current operating mode.
    632  1.1  dyoung 	 */
    633  1.1  dyoung 	c = ic->ic_ibss_chan;
    634  1.1  dyoung 	hchan.channel = c->ic_freq;
    635  1.1  dyoung 	hchan.channelFlags = ath_chan2flags(ic, c);
    636  1.1  dyoung 
    637  1.1  dyoung 	ath_hal_intrset(ah, 0);		/* disable interrupts */
    638  1.1  dyoung 	ath_draintxq(sc);		/* stop xmit side */
    639  1.1  dyoung 	ath_stoprecv(sc);		/* stop recv side */
    640  1.1  dyoung 	/* NB: indicate channel change so we do a full reset */
    641  1.1  dyoung 	if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_TRUE, &status))
    642  1.1  dyoung 		if_printf(ifp, "%s: unable to reset hardware; hal status %u\n",
    643  1.1  dyoung 			__func__, status);
    644  1.1  dyoung 	ath_hal_intrset(ah, sc->sc_imask);
    645  1.1  dyoung 	if (ath_startrecv(sc) != 0)	/* restart recv */
    646  1.1  dyoung 		if_printf(ifp, "%s: unable to start recv logic\n", __func__);
    647  1.1  dyoung 	ath_start(ifp);			/* restart xmit */
    648  1.1  dyoung 	if (ic->ic_state == IEEE80211_S_RUN)
    649  1.1  dyoung 		ath_beacon_config(sc);	/* restart beacons */
    650  1.1  dyoung }
    651  1.1  dyoung 
    652  1.1  dyoung static void
    653  1.1  dyoung ath_start(struct ifnet *ifp)
    654  1.1  dyoung {
    655  1.1  dyoung 	struct ath_softc *sc = ifp->if_softc;
    656  1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
    657  1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
    658  1.1  dyoung 	struct ieee80211_node *ni;
    659  1.1  dyoung 	struct ath_buf *bf;
    660  1.1  dyoung 	struct mbuf *m;
    661  1.1  dyoung 	struct ieee80211_frame *wh;
    662  1.1  dyoung 
    663  1.1  dyoung 	if ((ifp->if_flags & IFF_RUNNING) == 0 || sc->sc_invalid)
    664  1.1  dyoung 		return;
    665  1.1  dyoung 	for (;;) {
    666  1.1  dyoung 		/*
    667  1.1  dyoung 		 * Grab a TX buffer and associated resources.
    668  1.1  dyoung 		 */
    669  1.1  dyoung 		mtx_lock(&sc->sc_txbuflock);
    670  1.1  dyoung 		bf = TAILQ_FIRST(&sc->sc_txbuf);
    671  1.1  dyoung 		if (bf != NULL)
    672  1.1  dyoung 			TAILQ_REMOVE(&sc->sc_txbuf, bf, bf_list);
    673  1.1  dyoung 		mtx_unlock(&sc->sc_txbuflock);
    674  1.1  dyoung 		if (bf == NULL) {
    675  1.1  dyoung 			DPRINTF(("ath_start: out of xmit buffers\n"));
    676  1.1  dyoung 			sc->sc_stats.ast_tx_qstop++;
    677  1.1  dyoung 			ifp->if_flags |= IFF_OACTIVE;
    678  1.1  dyoung 			break;
    679  1.1  dyoung 		}
    680  1.1  dyoung 		/*
    681  1.1  dyoung 		 * Poll the management queue for frames; they
    682  1.1  dyoung 		 * have priority over normal data frames.
    683  1.1  dyoung 		 */
    684  1.1  dyoung 		IF_DEQUEUE(&ic->ic_mgtq, m);
    685  1.1  dyoung 		if (m == NULL) {
    686  1.1  dyoung 			/*
    687  1.1  dyoung 			 * No data frames go out unless we're associated.
    688  1.1  dyoung 			 */
    689  1.1  dyoung 			if (ic->ic_state != IEEE80211_S_RUN) {
    690  1.1  dyoung 				DPRINTF(("ath_start: ignore data packet, "
    691  1.1  dyoung 					"state %u\n", ic->ic_state));
    692  1.1  dyoung 				sc->sc_stats.ast_tx_discard++;
    693  1.1  dyoung 				mtx_lock(&sc->sc_txbuflock);
    694  1.1  dyoung 				TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
    695  1.1  dyoung 				mtx_unlock(&sc->sc_txbuflock);
    696  1.1  dyoung 				break;
    697  1.1  dyoung 			}
    698  1.1  dyoung 			IF_DEQUEUE(&ifp->if_snd, m);
    699  1.1  dyoung 			if (m == NULL) {
    700  1.1  dyoung 				mtx_lock(&sc->sc_txbuflock);
    701  1.1  dyoung 				TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
    702  1.1  dyoung 				mtx_unlock(&sc->sc_txbuflock);
    703  1.1  dyoung 				break;
    704  1.1  dyoung 			}
    705  1.1  dyoung 			ifp->if_opackets++;
    706  1.1  dyoung 			BPF_MTAP(ifp, m);
    707  1.1  dyoung 			/*
    708  1.1  dyoung 			 * Encapsulate the packet in prep for transmission.
    709  1.1  dyoung 			 */
    710  1.1  dyoung 			m = ieee80211_encap(ifp, m, &ni);
    711  1.1  dyoung 			if (m == NULL) {
    712  1.1  dyoung 				DPRINTF(("ath_start: encapsulation failure\n"));
    713  1.1  dyoung 				sc->sc_stats.ast_tx_encap++;
    714  1.1  dyoung 				goto bad;
    715  1.1  dyoung 			}
    716  1.1  dyoung 			wh = mtod(m, struct ieee80211_frame *);
    717  1.1  dyoung 			if (ic->ic_flags & IEEE80211_F_WEPON)
    718  1.1  dyoung 				wh->i_fc[1] |= IEEE80211_FC1_WEP;
    719  1.1  dyoung 		} else {
    720  1.1  dyoung 			/*
    721  1.1  dyoung 			 * Hack!  The referenced node pointer is in the
    722  1.1  dyoung 			 * rcvif field of the packet header.  This is
    723  1.1  dyoung 			 * placed there by ieee80211_mgmt_output because
    724  1.1  dyoung 			 * we need to hold the reference with the frame
    725  1.1  dyoung 			 * and there's no other way (other than packet
    726  1.1  dyoung 			 * tags which we consider too expensive to use)
    727  1.1  dyoung 			 * to pass it along.
    728  1.1  dyoung 			 */
    729  1.1  dyoung 			ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
    730  1.1  dyoung 			m->m_pkthdr.rcvif = NULL;
    731  1.1  dyoung 
    732  1.1  dyoung 			wh = mtod(m, struct ieee80211_frame *);
    733  1.1  dyoung 			if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
    734  1.1  dyoung 			    IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
    735  1.1  dyoung 				/* fill time stamp */
    736  1.1  dyoung 				u_int64_t tsf;
    737  1.1  dyoung 				u_int32_t *tstamp;
    738  1.1  dyoung 
    739  1.1  dyoung 				tsf = ath_hal_gettsf64(ah);
    740  1.1  dyoung 				/* XXX: adjust 100us delay to xmit */
    741  1.1  dyoung 				tsf += 100;
    742  1.1  dyoung 				tstamp = (u_int32_t *)&wh[1];
    743  1.1  dyoung 				tstamp[0] = htole32(tsf & 0xffffffff);
    744  1.1  dyoung 				tstamp[1] = htole32(tsf >> 32);
    745  1.1  dyoung 			}
    746  1.1  dyoung 			sc->sc_stats.ast_tx_mgmt++;
    747  1.1  dyoung 		}
    748  1.1  dyoung 		if (ic->ic_rawbpf)
    749  1.1  dyoung 			bpf_mtap(ic->ic_rawbpf, m);
    750  1.1  dyoung 
    751  1.1  dyoung 		if (sc->sc_drvbpf) {
    752  1.1  dyoung 			struct mbuf *mb;
    753  1.1  dyoung 
    754  1.1  dyoung 			MGETHDR(mb, M_DONTWAIT, m->m_type);
    755  1.1  dyoung 			if (mb != NULL) {
    756  1.1  dyoung 				sc->sc_tx_th.wt_rate =
    757  1.1  dyoung 					ni->ni_rates.rs_rates[ni->ni_txrate];
    758  1.1  dyoung 
    759  1.1  dyoung 				mb->m_next = m;
    760  1.1  dyoung 				mb->m_data = (caddr_t)&sc->sc_tx_th;
    761  1.1  dyoung 				mb->m_len = sizeof(sc->sc_tx_th);
    762  1.1  dyoung 				mb->m_pkthdr.len += mb->m_len;
    763  1.1  dyoung 				bpf_mtap(sc->sc_drvbpf, mb);
    764  1.1  dyoung 				m_free(mb);
    765  1.1  dyoung 			}
    766  1.1  dyoung 		}
    767  1.1  dyoung 
    768  1.1  dyoung 		/*
    769  1.1  dyoung 		 * TODO:
    770  1.1  dyoung 		 * The duration field of 802.11 header should be filled.
    771  1.1  dyoung 		 * XXX This may be done in the ieee80211 layer, but the upper
    772  1.1  dyoung 		 *     doesn't know the detail of parameters such as IFS
    773  1.1  dyoung 		 *     for now..
    774  1.1  dyoung 		 */
    775  1.1  dyoung 		if (ath_tx_start(sc, ni, bf, m)) {
    776  1.1  dyoung 	bad:
    777  1.1  dyoung 			mtx_lock(&sc->sc_txbuflock);
    778  1.1  dyoung 			TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
    779  1.1  dyoung 			mtx_unlock(&sc->sc_txbuflock);
    780  1.1  dyoung 			ifp->if_oerrors++;
    781  1.1  dyoung 			if (ni && ni != ic->ic_bss)
    782  1.1  dyoung 				ieee80211_free_node(ic, ni);
    783  1.1  dyoung 			continue;
    784  1.1  dyoung 		}
    785  1.1  dyoung 
    786  1.1  dyoung 		sc->sc_tx_timer = 5;
    787  1.1  dyoung 		ifp->if_timer = 1;
    788  1.1  dyoung 	}
    789  1.1  dyoung }
    790  1.1  dyoung 
    791  1.1  dyoung static int
    792  1.1  dyoung ath_media_change(struct ifnet *ifp)
    793  1.1  dyoung {
    794  1.1  dyoung 	int error;
    795  1.1  dyoung 
    796  1.1  dyoung 	error = ieee80211_media_change(ifp);
    797  1.1  dyoung 	if (error == ENETRESET) {
    798  1.1  dyoung 		if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) ==
    799  1.1  dyoung 		    (IFF_RUNNING|IFF_UP))
    800  1.1  dyoung 			ath_init(ifp);		/* XXX lose error */
    801  1.1  dyoung 		error = 0;
    802  1.1  dyoung 	}
    803  1.1  dyoung 	return error;
    804  1.1  dyoung }
    805  1.1  dyoung 
    806  1.1  dyoung static void
    807  1.1  dyoung ath_watchdog(struct ifnet *ifp)
    808  1.1  dyoung {
    809  1.1  dyoung 	struct ath_softc *sc = ifp->if_softc;
    810  1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
    811  1.1  dyoung 
    812  1.1  dyoung 	ifp->if_timer = 0;
    813  1.1  dyoung 	if ((ifp->if_flags & IFF_RUNNING) == 0 || sc->sc_invalid)
    814  1.1  dyoung 		return;
    815  1.1  dyoung 	if (sc->sc_tx_timer) {
    816  1.1  dyoung 		if (--sc->sc_tx_timer == 0) {
    817  1.1  dyoung 			if_printf(ifp, "device timeout\n");
    818  1.1  dyoung #ifdef AR_DEBUG
    819  1.1  dyoung 			if (ath_debug)
    820  1.1  dyoung 				ath_hal_dumpstate(sc->sc_ah);
    821  1.1  dyoung #endif /* AR_DEBUG */
    822  1.1  dyoung 			ath_init(ifp);		/* XXX ath_reset??? */
    823  1.1  dyoung 			ifp->if_oerrors++;
    824  1.1  dyoung 			sc->sc_stats.ast_watchdog++;
    825  1.1  dyoung 			return;
    826  1.1  dyoung 		}
    827  1.1  dyoung 		ifp->if_timer = 1;
    828  1.1  dyoung 	}
    829  1.1  dyoung 	if (ic->ic_fixed_rate == -1) {
    830  1.1  dyoung 		/*
    831  1.1  dyoung 		 * Run the rate control algorithm if we're not
    832  1.1  dyoung 		 * locked at a fixed rate.
    833  1.1  dyoung 		 */
    834  1.1  dyoung 		if (ic->ic_opmode == IEEE80211_M_STA)
    835  1.1  dyoung 			ath_rate_ctl(sc, ic->ic_bss);
    836  1.1  dyoung 		else
    837  1.1  dyoung 			ieee80211_iterate_nodes(ic, ath_rate_ctl, sc);
    838  1.1  dyoung 	}
    839  1.1  dyoung 	ieee80211_watchdog(ifp);
    840  1.1  dyoung }
    841  1.1  dyoung 
    842  1.1  dyoung static int
    843  1.1  dyoung ath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    844  1.1  dyoung {
    845  1.1  dyoung 	struct ath_softc *sc = ifp->if_softc;
    846  1.1  dyoung 	struct ifreq *ifr = (struct ifreq *)data;
    847  1.1  dyoung 	int error = 0;
    848  1.1  dyoung 
    849  1.1  dyoung 	mtx_lock(&sc->sc_mtx);
    850  1.1  dyoung 	switch (cmd) {
    851  1.1  dyoung 	case SIOCSIFFLAGS:
    852  1.1  dyoung 		if (ifp->if_flags & IFF_UP) {
    853  1.1  dyoung 			if (ifp->if_flags & IFF_RUNNING) {
    854  1.1  dyoung 				/*
    855  1.1  dyoung 				 * To avoid rescanning another access point,
    856  1.1  dyoung 				 * do not call ath_init() here.  Instead,
    857  1.1  dyoung 				 * only reflect promisc mode settings.
    858  1.1  dyoung 				 */
    859  1.1  dyoung 				ath_mode_init(sc);
    860  1.1  dyoung 			} else
    861  1.1  dyoung 				ath_init(ifp);		/* XXX lose error */
    862  1.1  dyoung 		} else
    863  1.1  dyoung 			ath_stop(ifp);
    864  1.1  dyoung 		break;
    865  1.1  dyoung 	case SIOCADDMULTI:
    866  1.1  dyoung 	case SIOCDELMULTI:
    867  1.1  dyoung 		/*
    868  1.1  dyoung 		 * The upper layer has already installed/removed
    869  1.1  dyoung 		 * the multicast address(es), just recalculate the
    870  1.1  dyoung 		 * multicast filter for the card.
    871  1.1  dyoung 		 */
    872  1.1  dyoung 		if (ifp->if_flags & IFF_RUNNING)
    873  1.1  dyoung 			ath_mode_init(sc);
    874  1.1  dyoung 		break;
    875  1.1  dyoung 	case SIOCGATHSTATS:
    876  1.1  dyoung 		copyout(&sc->sc_stats, ifr->ifr_data, sizeof (sc->sc_stats));
    877  1.1  dyoung 		break;
    878  1.1  dyoung 	default:
    879  1.1  dyoung 		error = ieee80211_ioctl(ifp, cmd, data);
    880  1.1  dyoung 		if (error == ENETRESET) {
    881  1.1  dyoung 			if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) ==
    882  1.1  dyoung 			    (IFF_RUNNING|IFF_UP))
    883  1.1  dyoung 				ath_init(ifp);		/* XXX lose error */
    884  1.1  dyoung 			error = 0;
    885  1.1  dyoung 		}
    886  1.1  dyoung 		break;
    887  1.1  dyoung 	}
    888  1.1  dyoung 	mtx_unlock(&sc->sc_mtx);
    889  1.1  dyoung 	return error;
    890  1.1  dyoung }
    891  1.1  dyoung 
    892  1.1  dyoung /*
    893  1.1  dyoung  * Fill the hardware key cache with key entries.
    894  1.1  dyoung  */
    895  1.1  dyoung static void
    896  1.1  dyoung ath_initkeytable(struct ath_softc *sc)
    897  1.1  dyoung {
    898  1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
    899  1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
    900  1.1  dyoung 	int i;
    901  1.1  dyoung 
    902  1.1  dyoung 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    903  1.1  dyoung 		struct ieee80211_wepkey *k = &ic->ic_nw_keys[i];
    904  1.1  dyoung 		if (k->wk_len == 0)
    905  1.1  dyoung 			ath_hal_keyreset(ah, i);
    906  1.1  dyoung 		else
    907  1.1  dyoung 			/* XXX return value */
    908  1.1  dyoung 			/* NB: this uses HAL_KEYVAL == ieee80211_wepkey */
    909  1.1  dyoung 			ath_hal_keyset(ah, i, (const HAL_KEYVAL *) k);
    910  1.1  dyoung 	}
    911  1.1  dyoung }
    912  1.1  dyoung 
    913  1.1  dyoung static void
    914  1.1  dyoung ath_mode_init(struct ath_softc *sc)
    915  1.1  dyoung {
    916  1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
    917  1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
    918  1.1  dyoung 	struct ifnet *ifp = &ic->ic_if;
    919  1.1  dyoung 	u_int32_t rfilt, mfilt[2], val;
    920  1.1  dyoung 	u_int8_t pos;
    921  1.1  dyoung 	struct ifmultiaddr *ifma;
    922  1.1  dyoung 
    923  1.1  dyoung 	/* configure operational mode */
    924  1.1  dyoung 	ath_hal_setopmode(ah, ic->ic_opmode);
    925  1.1  dyoung 
    926  1.1  dyoung 	/* receive filter */
    927  1.1  dyoung 	rfilt = (ath_hal_getrxfilter(ah) & HAL_RX_FILTER_PHYERR)
    928  1.1  dyoung 	      | HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST;
    929  1.1  dyoung 	if (ic->ic_opmode != IEEE80211_M_STA)
    930  1.1  dyoung 		rfilt |= HAL_RX_FILTER_PROBEREQ;
    931  1.1  dyoung 	if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
    932  1.1  dyoung 	    (ifp->if_flags & IFF_PROMISC))
    933  1.1  dyoung 		rfilt |= HAL_RX_FILTER_PROM;
    934  1.1  dyoung 	if (ic->ic_state == IEEE80211_S_SCAN)
    935  1.1  dyoung 		rfilt |= HAL_RX_FILTER_BEACON;
    936  1.1  dyoung 	ath_hal_setrxfilter(ah, rfilt);
    937  1.1  dyoung 
    938  1.1  dyoung 	/* calculate and install multicast filter */
    939  1.1  dyoung 	if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
    940  1.1  dyoung 		mfilt[0] = mfilt[1] = 0;
    941  1.1  dyoung 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
    942  1.1  dyoung 			caddr_t dl;
    943  1.1  dyoung 
    944  1.1  dyoung 			/* calculate XOR of eight 6bit values */
    945  1.1  dyoung 			dl = LLADDR((struct sockaddr_dl *) ifma->ifma_addr);
    946  1.1  dyoung 			val = LE_READ_4(dl + 0);
    947  1.1  dyoung 			pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
    948  1.1  dyoung 			val = LE_READ_4(dl + 3);
    949  1.1  dyoung 			pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
    950  1.1  dyoung 			pos &= 0x3f;
    951  1.1  dyoung 			mfilt[pos / 32] |= (1 << (pos % 32));
    952  1.1  dyoung 		}
    953  1.1  dyoung 	} else {
    954  1.1  dyoung 		mfilt[0] = mfilt[1] = ~0;
    955  1.1  dyoung 	}
    956  1.1  dyoung 	ath_hal_setmcastfilter(ah, mfilt[0], mfilt[1]);
    957  1.1  dyoung 	DPRINTF(("ath_mode_init: RX filter 0x%x, MC filter %08x:%08x\n",
    958  1.1  dyoung 		rfilt, mfilt[0], mfilt[1]));
    959  1.1  dyoung }
    960  1.1  dyoung 
    961  1.1  dyoung static void
    962  1.1  dyoung ath_mbuf_load_cb(void *arg, bus_dma_segment_t *seg, int nseg, bus_size_t mapsize, int error)
    963  1.1  dyoung {
    964  1.1  dyoung 	struct ath_buf *bf = arg;
    965  1.1  dyoung 
    966  1.1  dyoung 	KASSERT(nseg <= ATH_MAX_SCATTER,
    967  1.1  dyoung 		("ath_mbuf_load_cb: too many DMA segments %u", nseg));
    968  1.1  dyoung 	bf->bf_mapsize = mapsize;
    969  1.1  dyoung 	bf->bf_nseg = nseg;
    970  1.1  dyoung 	bcopy(seg, bf->bf_segs, nseg * sizeof (seg[0]));
    971  1.1  dyoung }
    972  1.1  dyoung 
    973  1.1  dyoung static int
    974  1.1  dyoung ath_beacon_alloc(struct ath_softc *sc, struct ieee80211_node *ni)
    975  1.1  dyoung {
    976  1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
    977  1.1  dyoung 	struct ifnet *ifp = &ic->ic_if;
    978  1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
    979  1.1  dyoung 	struct ieee80211_frame *wh;
    980  1.1  dyoung 	struct ath_buf *bf;
    981  1.1  dyoung 	struct ath_desc *ds;
    982  1.1  dyoung 	struct mbuf *m;
    983  1.1  dyoung 	int error, pktlen;
    984  1.1  dyoung 	u_int8_t *frm, rate;
    985  1.1  dyoung 	u_int16_t capinfo;
    986  1.1  dyoung 	struct ieee80211_rateset *rs;
    987  1.1  dyoung 	const HAL_RATE_TABLE *rt;
    988  1.1  dyoung 
    989  1.1  dyoung 	bf = sc->sc_bcbuf;
    990  1.1  dyoung 	if (bf->bf_m != NULL) {
    991  1.1  dyoung 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
    992  1.1  dyoung 		m_freem(bf->bf_m);
    993  1.1  dyoung 		bf->bf_m = NULL;
    994  1.1  dyoung 		bf->bf_node = NULL;
    995  1.1  dyoung 	}
    996  1.1  dyoung 	/*
    997  1.1  dyoung 	 * NB: the beacon data buffer must be 32-bit aligned;
    998  1.1  dyoung 	 * we assume the mbuf routines will return us something
    999  1.1  dyoung 	 * with this alignment (perhaps should assert).
   1000  1.1  dyoung 	 */
   1001  1.1  dyoung 	rs = &ni->ni_rates;
   1002  1.1  dyoung 	pktlen = sizeof (struct ieee80211_frame)
   1003  1.1  dyoung 	       + 8 + 2 + 2 + 2+ni->ni_esslen + 2+rs->rs_nrates + 6;
   1004  1.1  dyoung 	if (rs->rs_nrates > IEEE80211_RATE_SIZE)
   1005  1.1  dyoung 		pktlen += 2;
   1006  1.1  dyoung 	if (pktlen <= MHLEN)
   1007  1.1  dyoung 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   1008  1.1  dyoung 	else
   1009  1.1  dyoung 		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
   1010  1.1  dyoung 	if (m == NULL) {
   1011  1.1  dyoung 		DPRINTF(("ath_beacon_alloc: cannot get mbuf/cluster; size %u\n",
   1012  1.1  dyoung 			pktlen));
   1013  1.1  dyoung 		sc->sc_stats.ast_be_nombuf++;
   1014  1.1  dyoung 		return ENOMEM;
   1015  1.1  dyoung 	}
   1016  1.1  dyoung 
   1017  1.1  dyoung 	wh = mtod(m, struct ieee80211_frame *);
   1018  1.1  dyoung 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
   1019  1.1  dyoung 	    IEEE80211_FC0_SUBTYPE_BEACON;
   1020  1.1  dyoung 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
   1021  1.1  dyoung 	*(u_int16_t *)wh->i_dur = 0;
   1022  1.1  dyoung 	memcpy(wh->i_addr1, ifp->if_broadcastaddr, IEEE80211_ADDR_LEN);
   1023  1.1  dyoung 	memcpy(wh->i_addr2, ic->ic_myaddr, IEEE80211_ADDR_LEN);
   1024  1.1  dyoung 	memcpy(wh->i_addr3, ni->ni_bssid, IEEE80211_ADDR_LEN);
   1025  1.1  dyoung 	*(u_int16_t *)wh->i_seq = 0;
   1026  1.1  dyoung 
   1027  1.1  dyoung 	/*
   1028  1.1  dyoung 	 * beacon frame format
   1029  1.1  dyoung 	 *	[8] time stamp
   1030  1.1  dyoung 	 *	[2] beacon interval
   1031  1.1  dyoung 	 *	[2] cabability information
   1032  1.1  dyoung 	 *	[tlv] ssid
   1033  1.1  dyoung 	 *	[tlv] supported rates
   1034  1.1  dyoung 	 *	[tlv] parameter set (IBSS)
   1035  1.1  dyoung 	 *	[tlv] extended supported rates
   1036  1.1  dyoung 	 */
   1037  1.1  dyoung 	frm = (u_int8_t *)&wh[1];
   1038  1.1  dyoung 	memset(frm, 0, 8);	/* timestamp is set by hardware */
   1039  1.1  dyoung 	frm += 8;
   1040  1.1  dyoung 	*(u_int16_t *)frm = htole16(ni->ni_intval);
   1041  1.1  dyoung 	frm += 2;
   1042  1.1  dyoung 	if (ic->ic_opmode == IEEE80211_M_IBSS)
   1043  1.1  dyoung 		capinfo = IEEE80211_CAPINFO_IBSS;
   1044  1.1  dyoung 	else
   1045  1.1  dyoung 		capinfo = IEEE80211_CAPINFO_ESS;
   1046  1.1  dyoung 	if (ic->ic_flags & IEEE80211_F_WEPON)
   1047  1.1  dyoung 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
   1048  1.1  dyoung 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
   1049  1.1  dyoung 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
   1050  1.1  dyoung 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
   1051  1.1  dyoung 		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
   1052  1.1  dyoung 	*(u_int16_t *)frm = htole16(capinfo);
   1053  1.1  dyoung 	frm += 2;
   1054  1.1  dyoung 	*frm++ = IEEE80211_ELEMID_SSID;
   1055  1.1  dyoung 	*frm++ = ni->ni_esslen;
   1056  1.1  dyoung 	memcpy(frm, ni->ni_essid, ni->ni_esslen);
   1057  1.1  dyoung 	frm += ni->ni_esslen;
   1058  1.1  dyoung 	frm = ieee80211_add_rates(frm, rs);
   1059  1.1  dyoung 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
   1060  1.1  dyoung 		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
   1061  1.1  dyoung 		*frm++ = 2;
   1062  1.1  dyoung 		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
   1063  1.1  dyoung 	} else {
   1064  1.1  dyoung 		/* TODO: TIM */
   1065  1.1  dyoung 		*frm++ = IEEE80211_ELEMID_TIM;
   1066  1.1  dyoung 		*frm++ = 4;	/* length */
   1067  1.1  dyoung 		*frm++ = 0;	/* DTIM count */
   1068  1.1  dyoung 		*frm++ = 1;	/* DTIM period */
   1069  1.1  dyoung 		*frm++ = 0;	/* bitmap control */
   1070  1.1  dyoung 		*frm++ = 0;	/* Partial Virtual Bitmap (variable length) */
   1071  1.1  dyoung 	}
   1072  1.1  dyoung 	frm = ieee80211_add_xrates(frm, rs);
   1073  1.1  dyoung 	m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
   1074  1.1  dyoung 	KASSERT(m->m_pkthdr.len <= pktlen,
   1075  1.1  dyoung 		("beacon bigger than expected, len %u calculated %u",
   1076  1.1  dyoung 		m->m_pkthdr.len, pktlen));
   1077  1.1  dyoung 
   1078  1.1  dyoung 	DPRINTF2(("ath_beacon_alloc: m %p len %u\n", m, m->m_len));
   1079  1.1  dyoung 	error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m,
   1080  1.1  dyoung 				     ath_mbuf_load_cb, bf,
   1081  1.1  dyoung 				     BUS_DMA_NOWAIT);
   1082  1.1  dyoung 	if (error != 0) {
   1083  1.1  dyoung 		m_freem(m);
   1084  1.1  dyoung 		return error;
   1085  1.1  dyoung 	}
   1086  1.1  dyoung 	KASSERT(bf->bf_nseg == 1,
   1087  1.1  dyoung 		("ath_beacon_alloc: multi-segment packet; nseg %u",
   1088  1.1  dyoung 		bf->bf_nseg));
   1089  1.1  dyoung 	bf->bf_m = m;
   1090  1.1  dyoung 
   1091  1.1  dyoung 	/* setup descriptors */
   1092  1.1  dyoung 	ds = bf->bf_desc;
   1093  1.1  dyoung 
   1094  1.1  dyoung 	ds->ds_link = 0;
   1095  1.1  dyoung 	ds->ds_data = bf->bf_segs[0].ds_addr;
   1096  1.1  dyoung 	/*
   1097  1.1  dyoung 	 * Calculate rate code.
   1098  1.1  dyoung 	 * XXX everything at min xmit rate
   1099  1.1  dyoung 	 */
   1100  1.1  dyoung 	rt = sc->sc_currates;
   1101  1.1  dyoung 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
   1102  1.1  dyoung 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
   1103  1.1  dyoung 		rate = rt->info[0].rateCode | rt->info[0].shortPreamble;
   1104  1.1  dyoung 	else
   1105  1.1  dyoung 		rate = rt->info[0].rateCode;
   1106  1.1  dyoung 	ath_hal_setuptxdesc(ah, ds
   1107  1.1  dyoung 		, m->m_pkthdr.len + IEEE80211_CRC_LEN	/* packet length */
   1108  1.1  dyoung 		, sizeof(struct ieee80211_frame)	/* header length */
   1109  1.1  dyoung 		, HAL_PKT_TYPE_BEACON		/* Atheros packet type */
   1110  1.1  dyoung 		, 0x20				/* txpower XXX */
   1111  1.1  dyoung 		, rate, 1			/* series 0 rate/tries */
   1112  1.1  dyoung 		, HAL_TXKEYIX_INVALID		/* no encryption */
   1113  1.1  dyoung 		, 0				/* antenna mode */
   1114  1.1  dyoung 		, HAL_TXDESC_NOACK		/* no ack for beacons */
   1115  1.1  dyoung 		, 0				/* rts/cts rate */
   1116  1.1  dyoung 		, 0				/* rts/cts duration */
   1117  1.1  dyoung 	);
   1118  1.1  dyoung 	/* NB: beacon's BufLen must be a multiple of 4 bytes */
   1119  1.1  dyoung 	/* XXX verify mbuf data area covers this roundup */
   1120  1.1  dyoung 	ath_hal_filltxdesc(ah, ds
   1121  1.1  dyoung 		, roundup(bf->bf_segs[0].ds_len, 4)	/* buffer length */
   1122  1.1  dyoung 		, AH_TRUE				/* first segment */
   1123  1.1  dyoung 		, AH_TRUE				/* last segment */
   1124  1.1  dyoung 	);
   1125  1.1  dyoung 
   1126  1.1  dyoung 	return 0;
   1127  1.1  dyoung }
   1128  1.1  dyoung 
   1129  1.1  dyoung static void
   1130  1.1  dyoung ath_beacon_proc(void *arg, int pending)
   1131  1.1  dyoung {
   1132  1.1  dyoung 	struct ath_softc *sc = arg;
   1133  1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   1134  1.1  dyoung 	struct ath_buf *bf = sc->sc_bcbuf;
   1135  1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   1136  1.1  dyoung 
   1137  1.1  dyoung 	DPRINTF2(("%s: pending %u\n", __func__, pending));
   1138  1.1  dyoung 	if (ic->ic_opmode == IEEE80211_M_STA ||
   1139  1.1  dyoung 	    bf == NULL || bf->bf_m == NULL) {
   1140  1.1  dyoung 		DPRINTF(("%s: ic_flags=%x bf=%p bf_m=%p\n",
   1141  1.1  dyoung 			__func__, ic->ic_flags, bf, bf ? bf->bf_m : NULL));
   1142  1.1  dyoung 		return;
   1143  1.1  dyoung 	}
   1144  1.1  dyoung 	/* TODO: update beacon to reflect PS poll state */
   1145  1.1  dyoung 	if (!ath_hal_stoptxdma(ah, sc->sc_bhalq)) {
   1146  1.1  dyoung 		DPRINTF(("%s: beacon queue %u did not stop?",
   1147  1.1  dyoung 			__func__, sc->sc_bhalq));
   1148  1.1  dyoung 		return;			/* busy, XXX is this right? */
   1149  1.1  dyoung 	}
   1150  1.1  dyoung 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
   1151  1.1  dyoung 
   1152  1.1  dyoung 	ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr);
   1153  1.1  dyoung 	ath_hal_txstart(ah, sc->sc_bhalq);
   1154  1.1  dyoung 	DPRINTF2(("%s: TXDP%u = %p (%p)\n", __func__,
   1155  1.1  dyoung 		sc->sc_bhalq, (caddr_t)bf->bf_daddr, bf->bf_desc));
   1156  1.1  dyoung }
   1157  1.1  dyoung 
   1158  1.1  dyoung static void
   1159  1.1  dyoung ath_beacon_free(struct ath_softc *sc)
   1160  1.1  dyoung {
   1161  1.1  dyoung 	struct ath_buf *bf = sc->sc_bcbuf;
   1162  1.1  dyoung 
   1163  1.1  dyoung 	if (bf->bf_m != NULL) {
   1164  1.1  dyoung 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
   1165  1.1  dyoung 		m_freem(bf->bf_m);
   1166  1.1  dyoung 		bf->bf_m = NULL;
   1167  1.1  dyoung 		bf->bf_node = NULL;
   1168  1.1  dyoung 	}
   1169  1.1  dyoung }
   1170  1.1  dyoung 
   1171  1.1  dyoung /*
   1172  1.1  dyoung  * Configure the beacon and sleep timers.
   1173  1.1  dyoung  *
   1174  1.1  dyoung  * When operating as an AP this resets the TSF and sets
   1175  1.1  dyoung  * up the hardware to notify us when we need to issue beacons.
   1176  1.1  dyoung  *
   1177  1.1  dyoung  * When operating in station mode this sets up the beacon
   1178  1.1  dyoung  * timers according to the timestamp of the last received
   1179  1.1  dyoung  * beacon and the current TSF, configures PCF and DTIM
   1180  1.1  dyoung  * handling, programs the sleep registers so the hardware
   1181  1.1  dyoung  * will wakeup in time to receive beacons, and configures
   1182  1.1  dyoung  * the beacon miss handling so we'll receive a BMISS
   1183  1.1  dyoung  * interrupt when we stop seeing beacons from the AP
   1184  1.1  dyoung  * we've associated with.
   1185  1.1  dyoung  */
   1186  1.1  dyoung static void
   1187  1.1  dyoung ath_beacon_config(struct ath_softc *sc)
   1188  1.1  dyoung {
   1189  1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   1190  1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   1191  1.1  dyoung 	struct ieee80211_node *ni = ic->ic_bss;
   1192  1.1  dyoung 	u_int32_t nexttbtt;
   1193  1.1  dyoung 
   1194  1.1  dyoung 	nexttbtt = (LE_READ_4(ni->ni_tstamp + 4) << 22) |
   1195  1.1  dyoung 	    (LE_READ_4(ni->ni_tstamp) >> 10);
   1196  1.1  dyoung 	DPRINTF(("%s: nexttbtt=%u\n", __func__, nexttbtt));
   1197  1.1  dyoung 	nexttbtt += ni->ni_intval;
   1198  1.1  dyoung 	if (ic->ic_opmode == IEEE80211_M_STA) {
   1199  1.1  dyoung 		HAL_BEACON_STATE bs;
   1200  1.1  dyoung 		u_int32_t bmisstime;
   1201  1.1  dyoung 
   1202  1.1  dyoung 		/* NB: no PCF support right now */
   1203  1.1  dyoung 		memset(&bs, 0, sizeof(bs));
   1204  1.1  dyoung 		bs.bs_intval = ni->ni_intval;
   1205  1.1  dyoung 		bs.bs_nexttbtt = nexttbtt;
   1206  1.1  dyoung 		bs.bs_dtimperiod = bs.bs_intval;
   1207  1.1  dyoung 		bs.bs_nextdtim = nexttbtt;
   1208  1.1  dyoung 		/*
   1209  1.1  dyoung 		 * Calculate the number of consecutive beacons to miss
   1210  1.1  dyoung 		 * before taking a BMISS interrupt.  The configuration
   1211  1.1  dyoung 		 * is specified in ms, so we need to convert that to
   1212  1.1  dyoung 		 * TU's and then calculate based on the beacon interval.
   1213  1.1  dyoung 		 * Note that we clamp the result to at most 10 beacons.
   1214  1.1  dyoung 		 */
   1215  1.1  dyoung 		bmisstime = (ic->ic_bmisstimeout * 1000) / 1024;
   1216  1.1  dyoung 		bs.bs_bmissthreshold = howmany(bmisstime,ni->ni_intval);
   1217  1.1  dyoung 		if (bs.bs_bmissthreshold > 10)
   1218  1.1  dyoung 			bs.bs_bmissthreshold = 10;
   1219  1.1  dyoung 		else if (bs.bs_bmissthreshold <= 0)
   1220  1.1  dyoung 			bs.bs_bmissthreshold = 1;
   1221  1.1  dyoung 
   1222  1.1  dyoung 		/*
   1223  1.1  dyoung 		 * Calculate sleep duration.  The configuration is
   1224  1.1  dyoung 		 * given in ms.  We insure a multiple of the beacon
   1225  1.1  dyoung 		 * period is used.  Also, if the sleep duration is
   1226  1.1  dyoung 		 * greater than the DTIM period then it makes senses
   1227  1.1  dyoung 		 * to make it a multiple of that.
   1228  1.1  dyoung 		 *
   1229  1.1  dyoung 		 * XXX fixed at 100ms
   1230  1.1  dyoung 		 */
   1231  1.1  dyoung 		bs.bs_sleepduration =
   1232  1.1  dyoung 			roundup((100 * 1000) / 1024, bs.bs_intval);
   1233  1.1  dyoung 		if (bs.bs_sleepduration > bs.bs_dtimperiod)
   1234  1.1  dyoung 			bs.bs_sleepduration = roundup(bs.bs_sleepduration, bs.bs_dtimperiod);
   1235  1.1  dyoung 
   1236  1.1  dyoung 		DPRINTF(("%s: intval %u nexttbtt %u dtim %u nextdtim %u bmiss %u sleep %u\n"
   1237  1.1  dyoung 			, __func__
   1238  1.1  dyoung 			, bs.bs_intval
   1239  1.1  dyoung 			, bs.bs_nexttbtt
   1240  1.1  dyoung 			, bs.bs_dtimperiod
   1241  1.1  dyoung 			, bs.bs_nextdtim
   1242  1.1  dyoung 			, bs.bs_bmissthreshold
   1243  1.1  dyoung 			, bs.bs_sleepduration
   1244  1.1  dyoung 		));
   1245  1.1  dyoung 		ath_hal_intrset(ah, 0);
   1246  1.1  dyoung 		/*
   1247  1.1  dyoung 		 * Reset our tsf so the hardware will update the
   1248  1.1  dyoung 		 * tsf register to reflect timestamps found in
   1249  1.1  dyoung 		 * received beacons.
   1250  1.1  dyoung 		 */
   1251  1.1  dyoung 		ath_hal_resettsf(ah);
   1252  1.1  dyoung 		ath_hal_beacontimers(ah, &bs, 0/*XXX*/, 0, 0);
   1253  1.1  dyoung 		sc->sc_imask |= HAL_INT_BMISS;
   1254  1.1  dyoung 		ath_hal_intrset(ah, sc->sc_imask);
   1255  1.1  dyoung 	} else {
   1256  1.1  dyoung 		DPRINTF(("%s: intval %u nexttbtt %u\n",
   1257  1.1  dyoung 			__func__, ni->ni_intval, nexttbtt));
   1258  1.1  dyoung 		ath_hal_intrset(ah, 0);
   1259  1.1  dyoung 		ath_hal_beaconinit(ah, ic->ic_opmode,
   1260  1.1  dyoung 			nexttbtt, ni->ni_intval);
   1261  1.1  dyoung 		if (ic->ic_opmode != IEEE80211_M_MONITOR)
   1262  1.1  dyoung 			sc->sc_imask |= HAL_INT_SWBA;	/* beacon prepare */
   1263  1.1  dyoung 		ath_hal_intrset(ah, sc->sc_imask);
   1264  1.1  dyoung 	}
   1265  1.1  dyoung }
   1266  1.1  dyoung 
   1267  1.1  dyoung static void
   1268  1.1  dyoung ath_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
   1269  1.1  dyoung {
   1270  1.1  dyoung 	bus_addr_t *paddr = (bus_addr_t*) arg;
   1271  1.1  dyoung 	*paddr = segs->ds_addr;
   1272  1.1  dyoung }
   1273  1.1  dyoung 
   1274  1.1  dyoung static int
   1275  1.1  dyoung ath_desc_alloc(struct ath_softc *sc)
   1276  1.1  dyoung {
   1277  1.1  dyoung 	int i, bsize, error;
   1278  1.1  dyoung 	struct ath_desc *ds;
   1279  1.1  dyoung 	struct ath_buf *bf;
   1280  1.1  dyoung 
   1281  1.1  dyoung 	/* allocate descriptors */
   1282  1.1  dyoung 	sc->sc_desc_len = sizeof(struct ath_desc) *
   1283  1.1  dyoung 				(ATH_TXBUF * ATH_TXDESC + ATH_RXBUF + 1);
   1284  1.1  dyoung 	error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT, &sc->sc_ddmamap);
   1285  1.1  dyoung 	if (error != 0)
   1286  1.1  dyoung 		return error;
   1287  1.1  dyoung 
   1288  1.1  dyoung 	error = bus_dmamem_alloc(sc->sc_dmat, (void**) &sc->sc_desc,
   1289  1.1  dyoung 				 BUS_DMA_NOWAIT, &sc->sc_ddmamap);
   1290  1.1  dyoung 	if (error != 0)
   1291  1.1  dyoung 		goto fail0;
   1292  1.1  dyoung 
   1293  1.1  dyoung 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_ddmamap,
   1294  1.1  dyoung 				sc->sc_desc, sc->sc_desc_len,
   1295  1.1  dyoung 				ath_load_cb, &sc->sc_desc_paddr,
   1296  1.1  dyoung 				BUS_DMA_NOWAIT);
   1297  1.1  dyoung 	if (error != 0)
   1298  1.1  dyoung 		goto fail1;
   1299  1.1  dyoung 
   1300  1.1  dyoung 	ds = sc->sc_desc;
   1301  1.1  dyoung 	DPRINTF(("ath_desc_alloc: DMA map: %p (%d) -> %p (%lu)\n",
   1302  1.1  dyoung 	    ds, sc->sc_desc_len,
   1303  1.1  dyoung 	    (caddr_t) sc->sc_desc_paddr, /*XXX*/ (u_long) sc->sc_desc_len));
   1304  1.1  dyoung 
   1305  1.1  dyoung 	/* allocate buffers */
   1306  1.1  dyoung 	bsize = sizeof(struct ath_buf) * (ATH_TXBUF + ATH_RXBUF + 1);
   1307  1.1  dyoung 	bf = malloc(bsize, M_DEVBUF, M_NOWAIT | M_ZERO);
   1308  1.1  dyoung 	if (bf == NULL)
   1309  1.1  dyoung 		goto fail2;
   1310  1.1  dyoung 	sc->sc_bufptr = bf;
   1311  1.1  dyoung 
   1312  1.1  dyoung 	TAILQ_INIT(&sc->sc_rxbuf);
   1313  1.1  dyoung 	for (i = 0; i < ATH_RXBUF; i++, bf++, ds++) {
   1314  1.1  dyoung 		bf->bf_desc = ds;
   1315  1.1  dyoung 		bf->bf_daddr = sc->sc_desc_paddr +
   1316  1.1  dyoung 		    ((caddr_t)ds - (caddr_t)sc->sc_desc);
   1317  1.1  dyoung 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
   1318  1.1  dyoung 					  &bf->bf_dmamap);
   1319  1.1  dyoung 		if (error != 0)
   1320  1.1  dyoung 			break;
   1321  1.1  dyoung 		TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
   1322  1.1  dyoung 	}
   1323  1.1  dyoung 
   1324  1.1  dyoung 	TAILQ_INIT(&sc->sc_txbuf);
   1325  1.1  dyoung 	for (i = 0; i < ATH_TXBUF; i++, bf++, ds += ATH_TXDESC) {
   1326  1.1  dyoung 		bf->bf_desc = ds;
   1327  1.1  dyoung 		bf->bf_daddr = sc->sc_desc_paddr +
   1328  1.1  dyoung 		    ((caddr_t)ds - (caddr_t)sc->sc_desc);
   1329  1.1  dyoung 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
   1330  1.1  dyoung 					  &bf->bf_dmamap);
   1331  1.1  dyoung 		if (error != 0)
   1332  1.1  dyoung 			break;
   1333  1.1  dyoung 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
   1334  1.1  dyoung 	}
   1335  1.1  dyoung 	TAILQ_INIT(&sc->sc_txq);
   1336  1.1  dyoung 
   1337  1.1  dyoung 	/* beacon buffer */
   1338  1.1  dyoung 	bf->bf_desc = ds;
   1339  1.1  dyoung 	bf->bf_daddr = sc->sc_desc_paddr + ((caddr_t)ds - (caddr_t)sc->sc_desc);
   1340  1.1  dyoung 	error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT, &bf->bf_dmamap);
   1341  1.1  dyoung 	if (error != 0)
   1342  1.1  dyoung 		return error;
   1343  1.1  dyoung 	sc->sc_bcbuf = bf;
   1344  1.1  dyoung 	return 0;
   1345  1.1  dyoung 
   1346  1.1  dyoung fail2:
   1347  1.1  dyoung 	bus_dmamap_unload(sc->sc_dmat, sc->sc_ddmamap);
   1348  1.1  dyoung fail1:
   1349  1.1  dyoung 	bus_dmamem_free(sc->sc_dmat, sc->sc_desc, sc->sc_ddmamap);
   1350  1.1  dyoung fail0:
   1351  1.1  dyoung 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_ddmamap);
   1352  1.1  dyoung 	sc->sc_ddmamap = NULL;
   1353  1.1  dyoung 	return error;
   1354  1.1  dyoung }
   1355  1.1  dyoung 
   1356  1.1  dyoung static void
   1357  1.1  dyoung ath_desc_free(struct ath_softc *sc)
   1358  1.1  dyoung {
   1359  1.1  dyoung 	struct ath_buf *bf;
   1360  1.1  dyoung 
   1361  1.1  dyoung 	bus_dmamap_unload(sc->sc_dmat, sc->sc_ddmamap);
   1362  1.1  dyoung 	bus_dmamem_free(sc->sc_dmat, sc->sc_desc, sc->sc_ddmamap);
   1363  1.1  dyoung 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_ddmamap);
   1364  1.1  dyoung 
   1365  1.1  dyoung 	TAILQ_FOREACH(bf, &sc->sc_txq, bf_list) {
   1366  1.1  dyoung 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
   1367  1.1  dyoung 		bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
   1368  1.1  dyoung 		m_freem(bf->bf_m);
   1369  1.1  dyoung 	}
   1370  1.1  dyoung 	TAILQ_FOREACH(bf, &sc->sc_txbuf, bf_list)
   1371  1.1  dyoung 		bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
   1372  1.1  dyoung 	TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
   1373  1.1  dyoung 		if (bf->bf_m) {
   1374  1.1  dyoung 			bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
   1375  1.1  dyoung 			bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
   1376  1.1  dyoung 			m_freem(bf->bf_m);
   1377  1.1  dyoung 			bf->bf_m = NULL;
   1378  1.1  dyoung 		}
   1379  1.1  dyoung 	}
   1380  1.1  dyoung 	if (sc->sc_bcbuf != NULL) {
   1381  1.1  dyoung 		bus_dmamap_unload(sc->sc_dmat, sc->sc_bcbuf->bf_dmamap);
   1382  1.1  dyoung 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_bcbuf->bf_dmamap);
   1383  1.1  dyoung 		sc->sc_bcbuf = NULL;
   1384  1.1  dyoung 	}
   1385  1.1  dyoung 
   1386  1.1  dyoung 	TAILQ_INIT(&sc->sc_rxbuf);
   1387  1.1  dyoung 	TAILQ_INIT(&sc->sc_txbuf);
   1388  1.1  dyoung 	TAILQ_INIT(&sc->sc_txq);
   1389  1.1  dyoung 	free(sc->sc_bufptr, M_DEVBUF);
   1390  1.1  dyoung 	sc->sc_bufptr = NULL;
   1391  1.1  dyoung }
   1392  1.1  dyoung 
   1393  1.1  dyoung static struct ieee80211_node *
   1394  1.1  dyoung ath_node_alloc(struct ieee80211com *ic)
   1395  1.1  dyoung {
   1396  1.1  dyoung 	struct ath_node *an =
   1397  1.1  dyoung 		malloc(sizeof(struct ath_node), M_DEVBUF, M_NOWAIT | M_ZERO);
   1398  1.1  dyoung 	return an ? &an->an_node : NULL;
   1399  1.1  dyoung }
   1400  1.1  dyoung 
   1401  1.1  dyoung static void
   1402  1.1  dyoung ath_node_free(struct ieee80211com *ic, struct ieee80211_node *ni)
   1403  1.1  dyoung {
   1404  1.1  dyoung         struct ath_softc *sc = ic->ic_if.if_softc;
   1405  1.1  dyoung 	struct ath_buf *bf;
   1406  1.1  dyoung 
   1407  1.1  dyoung 	TAILQ_FOREACH(bf, &sc->sc_txq, bf_list) {
   1408  1.1  dyoung 		if (bf->bf_node == ni)
   1409  1.1  dyoung 			bf->bf_node = NULL;
   1410  1.1  dyoung 	}
   1411  1.1  dyoung 	free(ni, M_DEVBUF);
   1412  1.1  dyoung }
   1413  1.1  dyoung 
   1414  1.1  dyoung static void
   1415  1.1  dyoung ath_node_copy(struct ieee80211com *ic,
   1416  1.1  dyoung 	struct ieee80211_node *dst, const struct ieee80211_node *src)
   1417  1.1  dyoung {
   1418  1.1  dyoung 	*(struct ath_node *)dst = *(const struct ath_node *)src;
   1419  1.1  dyoung }
   1420  1.1  dyoung 
   1421  1.1  dyoung static int
   1422  1.1  dyoung ath_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf)
   1423  1.1  dyoung {
   1424  1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   1425  1.1  dyoung 	int error;
   1426  1.1  dyoung 	struct mbuf *m;
   1427  1.1  dyoung 	struct ath_desc *ds;
   1428  1.1  dyoung 
   1429  1.1  dyoung 	m = bf->bf_m;
   1430  1.1  dyoung 	if (m == NULL) {
   1431  1.1  dyoung 		/*
   1432  1.1  dyoung 		 * NB: by assigning a page to the rx dma buffer we
   1433  1.1  dyoung 		 * implicitly satisfy the Atheros requirement that
   1434  1.1  dyoung 		 * this buffer be cache-line-aligned and sized to be
   1435  1.1  dyoung 		 * multiple of the cache line size.  Not doing this
   1436  1.1  dyoung 		 * causes weird stuff to happen (for the 5210 at least).
   1437  1.1  dyoung 		 */
   1438  1.1  dyoung 		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
   1439  1.1  dyoung 		if (m == NULL) {
   1440  1.1  dyoung 			DPRINTF(("ath_rxbuf_init: no mbuf/cluster\n"));
   1441  1.1  dyoung 			sc->sc_stats.ast_rx_nombuf++;
   1442  1.1  dyoung 			return ENOMEM;
   1443  1.1  dyoung 		}
   1444  1.1  dyoung 		bf->bf_m = m;
   1445  1.1  dyoung 		m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
   1446  1.1  dyoung 
   1447  1.1  dyoung 		error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m,
   1448  1.1  dyoung 					     ath_mbuf_load_cb, bf,
   1449  1.1  dyoung 					     BUS_DMA_NOWAIT);
   1450  1.1  dyoung 		if (error != 0) {
   1451  1.1  dyoung 			DPRINTF(("ath_rxbuf_init: bus_dmamap_load_mbuf failed;"
   1452  1.1  dyoung 				" error %d\n", error));
   1453  1.1  dyoung 			sc->sc_stats.ast_rx_busdma++;
   1454  1.1  dyoung 			return error;
   1455  1.1  dyoung 		}
   1456  1.1  dyoung 		KASSERT(bf->bf_nseg == 1,
   1457  1.1  dyoung 			("ath_rxbuf_init: multi-segment packet; nseg %u",
   1458  1.1  dyoung 			bf->bf_nseg));
   1459  1.1  dyoung 	}
   1460  1.1  dyoung 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREREAD);
   1461  1.1  dyoung 
   1462  1.1  dyoung 	/* setup descriptors */
   1463  1.1  dyoung 	ds = bf->bf_desc;
   1464  1.1  dyoung 	ds->ds_link = 0;
   1465  1.1  dyoung 	ds->ds_data = bf->bf_segs[0].ds_addr;
   1466  1.1  dyoung 	ath_hal_setuprxdesc(ah, ds
   1467  1.1  dyoung 		, m->m_len		/* buffer size */
   1468  1.1  dyoung 		, 0
   1469  1.1  dyoung 	);
   1470  1.1  dyoung 
   1471  1.1  dyoung 	if (sc->sc_rxlink != NULL)
   1472  1.1  dyoung 		*sc->sc_rxlink = bf->bf_daddr;
   1473  1.1  dyoung 	sc->sc_rxlink = &ds->ds_link;
   1474  1.1  dyoung 	return 0;
   1475  1.1  dyoung }
   1476  1.1  dyoung 
   1477  1.1  dyoung static void
   1478  1.1  dyoung ath_rx_proc(void *arg, int npending)
   1479  1.1  dyoung {
   1480  1.1  dyoung 	struct ath_softc *sc = arg;
   1481  1.1  dyoung 	struct ath_buf *bf;
   1482  1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   1483  1.1  dyoung 	struct ifnet *ifp = &ic->ic_if;
   1484  1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   1485  1.1  dyoung 	struct ath_desc *ds;
   1486  1.1  dyoung 	struct mbuf *m;
   1487  1.1  dyoung 	struct ieee80211_frame *wh, whbuf;
   1488  1.1  dyoung 	struct ieee80211_node *ni;
   1489  1.1  dyoung 	int len;
   1490  1.1  dyoung 	u_int phyerr;
   1491  1.1  dyoung 	HAL_STATUS status;
   1492  1.1  dyoung 
   1493  1.1  dyoung 	DPRINTF2(("ath_rx_proc: pending %u\n", npending));
   1494  1.1  dyoung 	do {
   1495  1.1  dyoung 		bf = TAILQ_FIRST(&sc->sc_rxbuf);
   1496  1.1  dyoung 		if (bf == NULL) {		/* NB: shouldn't happen */
   1497  1.1  dyoung 			if_printf(ifp, "ath_rx_proc: no buffer!\n");
   1498  1.1  dyoung 			break;
   1499  1.1  dyoung 		}
   1500  1.1  dyoung 		m = bf->bf_m;
   1501  1.1  dyoung 		if (m == NULL) {		/* NB: shouldn't happen */
   1502  1.1  dyoung 			if_printf(ifp, "ath_rx_proc: no mbuf!\n");
   1503  1.1  dyoung 			continue;
   1504  1.1  dyoung 		}
   1505  1.1  dyoung 		ds = bf->bf_desc;
   1506  1.1  dyoung 		status = ath_hal_rxprocdesc(ah, ds);
   1507  1.1  dyoung #ifdef AR_DEBUG
   1508  1.1  dyoung 		if (ath_debug > 1)
   1509  1.1  dyoung 			ath_printrxbuf(bf, status == HAL_OK);
   1510  1.1  dyoung #endif
   1511  1.1  dyoung 		if (status == HAL_EINPROGRESS)
   1512  1.1  dyoung 			break;
   1513  1.1  dyoung 		TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
   1514  1.1  dyoung 		if (ds->ds_rxstat.rs_status != 0) {
   1515  1.1  dyoung 			ifp->if_ierrors++;
   1516  1.1  dyoung 			if (ds->ds_rxstat.rs_status & HAL_RXERR_CRC)
   1517  1.1  dyoung 				sc->sc_stats.ast_rx_crcerr++;
   1518  1.1  dyoung 			if (ds->ds_rxstat.rs_status & HAL_RXERR_FIFO)
   1519  1.1  dyoung 				sc->sc_stats.ast_rx_fifoerr++;
   1520  1.1  dyoung 			if (ds->ds_rxstat.rs_status & HAL_RXERR_DECRYPT)
   1521  1.1  dyoung 				sc->sc_stats.ast_rx_badcrypt++;
   1522  1.1  dyoung 			if (ds->ds_rxstat.rs_status & HAL_RXERR_PHY) {
   1523  1.1  dyoung 				sc->sc_stats.ast_rx_phyerr++;
   1524  1.1  dyoung 				phyerr = ds->ds_rxstat.rs_phyerr & 0x1f;
   1525  1.1  dyoung 				sc->sc_stats.ast_rx_phy[phyerr]++;
   1526  1.1  dyoung 			}
   1527  1.1  dyoung 			goto rx_next;
   1528  1.1  dyoung 		}
   1529  1.1  dyoung 
   1530  1.1  dyoung 		len = ds->ds_rxstat.rs_datalen;
   1531  1.1  dyoung 		if (len < sizeof(struct ieee80211_frame)) {
   1532  1.1  dyoung 			DPRINTF(("ath_rx_proc: short packet %d\n", len));
   1533  1.1  dyoung 			sc->sc_stats.ast_rx_tooshort++;
   1534  1.1  dyoung 			goto rx_next;
   1535  1.1  dyoung 		}
   1536  1.1  dyoung 
   1537  1.1  dyoung 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
   1538  1.1  dyoung 		    BUS_DMASYNC_POSTREAD);
   1539  1.1  dyoung 
   1540  1.1  dyoung 		wh = mtod(m, struct ieee80211_frame *);
   1541  1.1  dyoung 		if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
   1542  1.1  dyoung 		    IEEE80211_FC0_TYPE_CTL &&
   1543  1.1  dyoung 		    ic->ic_opmode != IEEE80211_M_MONITOR) {
   1544  1.1  dyoung 			/*
   1545  1.1  dyoung 			 * Discard control frame when not in monitor mode.
   1546  1.1  dyoung 			 */
   1547  1.1  dyoung 			DPRINTF(("ath_rx_proc: control frame\n"));
   1548  1.1  dyoung 			sc->sc_stats.ast_rx_ctl++;
   1549  1.1  dyoung 			goto rx_next;
   1550  1.1  dyoung 		}
   1551  1.1  dyoung 
   1552  1.1  dyoung 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
   1553  1.1  dyoung 		bf->bf_m = NULL;
   1554  1.1  dyoung 		m->m_pkthdr.rcvif = ifp;
   1555  1.1  dyoung 		m->m_pkthdr.len = m->m_len = len;
   1556  1.1  dyoung 
   1557  1.1  dyoung 		if (sc->sc_drvbpf) {
   1558  1.1  dyoung 			struct mbuf *mb;
   1559  1.1  dyoung 
   1560  1.1  dyoung 			/* XXX pre-allocate space when setting up recv's */
   1561  1.1  dyoung 			MGETHDR(mb, M_DONTWAIT, m->m_type);
   1562  1.1  dyoung 			if (mb != NULL) {
   1563  1.1  dyoung 				sc->sc_rx_th.wr_rate =
   1564  1.1  dyoung 					sc->sc_hwmap[ds->ds_rxstat.rs_rate];
   1565  1.1  dyoung 				sc->sc_rx_th.wr_antsignal =
   1566  1.1  dyoung 					ds->ds_rxstat.rs_rssi;
   1567  1.1  dyoung 				sc->sc_rx_th.wr_antenna =
   1568  1.1  dyoung 					ds->ds_rxstat.rs_antenna;
   1569  1.1  dyoung 				/* XXX TSF */
   1570  1.1  dyoung 
   1571  1.1  dyoung 				(void) m_dup_pkthdr(mb, m, M_DONTWAIT);
   1572  1.1  dyoung 				mb->m_next = m;
   1573  1.1  dyoung 				mb->m_data = (caddr_t)&sc->sc_rx_th;
   1574  1.1  dyoung 				mb->m_len = sizeof(sc->sc_rx_th);
   1575  1.1  dyoung 				mb->m_pkthdr.len += mb->m_len;
   1576  1.1  dyoung 				bpf_mtap(sc->sc_drvbpf, mb);
   1577  1.1  dyoung 				m_free(mb);
   1578  1.1  dyoung 			}
   1579  1.1  dyoung 		}
   1580  1.1  dyoung 
   1581  1.1  dyoung 		m_adj(m, -IEEE80211_CRC_LEN);
   1582  1.1  dyoung 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
   1583  1.1  dyoung 			/*
   1584  1.1  dyoung 			 * WEP is decrypted by hardware. Clear WEP bit
   1585  1.1  dyoung 			 * and trim WEP header for ieee80211_input().
   1586  1.1  dyoung 			 */
   1587  1.1  dyoung 			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
   1588  1.1  dyoung 			memcpy(&whbuf, wh, sizeof(whbuf));
   1589  1.1  dyoung 			m_adj(m, IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN);
   1590  1.1  dyoung 			memcpy(mtod(m, caddr_t), &whbuf, sizeof(whbuf));
   1591  1.1  dyoung 			/*
   1592  1.1  dyoung 			 * Also trim WEP ICV from the tail.
   1593  1.1  dyoung 			 */
   1594  1.1  dyoung 			m_adj(m, -IEEE80211_WEP_CRCLEN);
   1595  1.1  dyoung 		}
   1596  1.1  dyoung 
   1597  1.1  dyoung 		/*
   1598  1.1  dyoung 		 * Locate the node for sender, track state, and
   1599  1.1  dyoung 		 * then pass this node (referenced) up to the 802.11
   1600  1.1  dyoung 		 * layer for its use.  We are required to pass
   1601  1.1  dyoung 		 * something so we fall back to ic_bss when this frame
   1602  1.1  dyoung 		 * is from an unknown sender.
   1603  1.1  dyoung 		 */
   1604  1.1  dyoung 		if (ic->ic_opmode != IEEE80211_M_STA) {
   1605  1.1  dyoung 			ni = ieee80211_find_node(ic, wh->i_addr2);
   1606  1.1  dyoung 			if (ni == NULL)
   1607  1.1  dyoung 				ni = ieee80211_ref_node(ic->ic_bss);
   1608  1.1  dyoung 		} else
   1609  1.1  dyoung 			ni = ieee80211_ref_node(ic->ic_bss);
   1610  1.1  dyoung 		ATH_NODE(ni)->an_rx_antenna = ds->ds_rxstat.rs_antenna;
   1611  1.1  dyoung 		/*
   1612  1.1  dyoung 		 * Send frame up for processing.
   1613  1.1  dyoung 		 */
   1614  1.1  dyoung 		ieee80211_input(ifp, m, ni,
   1615  1.1  dyoung 			ds->ds_rxstat.rs_rssi, ds->ds_rxstat.rs_tstamp);
   1616  1.1  dyoung 		/*
   1617  1.1  dyoung 		 * The frame may have caused the node to be marked for
   1618  1.1  dyoung 		 * reclamation (e.g. in response to a DEAUTH message)
   1619  1.1  dyoung 		 * so use free_node here instead of unref_node.
   1620  1.1  dyoung 		 */
   1621  1.1  dyoung 		if (ni == ic->ic_bss)
   1622  1.1  dyoung 			ieee80211_unref_node(&ni);
   1623  1.1  dyoung 		else
   1624  1.1  dyoung 			ieee80211_free_node(ic, ni);
   1625  1.1  dyoung   rx_next:
   1626  1.1  dyoung 		TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
   1627  1.1  dyoung 	} while (ath_rxbuf_init(sc, bf) == 0);
   1628  1.1  dyoung 
   1629  1.1  dyoung 	ath_hal_rxmonitor(ah);			/* rx signal state monitoring */
   1630  1.1  dyoung 	ath_hal_rxena(ah);			/* in case of RXEOL */
   1631  1.1  dyoung }
   1632  1.1  dyoung 
   1633  1.1  dyoung /*
   1634  1.1  dyoung  * XXX Size of an ACK control frame in bytes.
   1635  1.1  dyoung  */
   1636  1.1  dyoung #define	IEEE80211_ACK_SIZE	(2+2+IEEE80211_ADDR_LEN+4)
   1637  1.1  dyoung 
   1638  1.1  dyoung static int
   1639  1.1  dyoung ath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni, struct ath_buf *bf,
   1640  1.1  dyoung     struct mbuf *m0)
   1641  1.1  dyoung {
   1642  1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   1643  1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   1644  1.1  dyoung 	struct ifnet *ifp = &sc->sc_ic.ic_if;
   1645  1.1  dyoung 	int i, error, iswep, hdrlen, pktlen;
   1646  1.1  dyoung 	u_int8_t rix, cix, txrate, ctsrate;
   1647  1.1  dyoung 	struct ath_desc *ds;
   1648  1.1  dyoung 	struct mbuf *m;
   1649  1.1  dyoung 	struct ieee80211_frame *wh;
   1650  1.1  dyoung 	u_int32_t iv;
   1651  1.1  dyoung 	u_int8_t *ivp;
   1652  1.1  dyoung 	u_int8_t hdrbuf[sizeof(struct ieee80211_frame) +
   1653  1.1  dyoung 	    IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN];
   1654  1.1  dyoung 	u_int subtype, flags, ctsduration, antenna;
   1655  1.1  dyoung 	HAL_PKT_TYPE atype;
   1656  1.1  dyoung 	const HAL_RATE_TABLE *rt;
   1657  1.1  dyoung 	HAL_BOOL shortPreamble;
   1658  1.1  dyoung 	struct ath_node *an;
   1659  1.1  dyoung 
   1660  1.1  dyoung 	wh = mtod(m0, struct ieee80211_frame *);
   1661  1.1  dyoung 	iswep = wh->i_fc[1] & IEEE80211_FC1_WEP;
   1662  1.1  dyoung 	hdrlen = sizeof(struct ieee80211_frame);
   1663  1.1  dyoung 	pktlen = m0->m_pkthdr.len;
   1664  1.1  dyoung 
   1665  1.1  dyoung 	if (iswep) {
   1666  1.1  dyoung 		memcpy(hdrbuf, mtod(m0, caddr_t), hdrlen);
   1667  1.1  dyoung 		m_adj(m0, hdrlen);
   1668  1.1  dyoung 		M_PREPEND(m0, sizeof(hdrbuf), M_DONTWAIT);
   1669  1.1  dyoung 		if (m0 == NULL) {
   1670  1.1  dyoung 			sc->sc_stats.ast_tx_nombuf++;
   1671  1.1  dyoung 			return ENOMEM;
   1672  1.1  dyoung 		}
   1673  1.1  dyoung 		ivp = hdrbuf + hdrlen;
   1674  1.1  dyoung 		/*
   1675  1.1  dyoung 		 * XXX
   1676  1.1  dyoung 		 * IV must not duplicate during the lifetime of the key.
   1677  1.1  dyoung 		 * But no mechanism to renew keys is defined in IEEE 802.11
   1678  1.1  dyoung 		 * WEP.  And IV may be duplicated between other stations
   1679  1.1  dyoung 		 * because of the session key itself is shared.
   1680  1.1  dyoung 		 * So we use pseudo random IV for now, though it is not the
   1681  1.1  dyoung 		 * right way.
   1682  1.1  dyoung 		 */
   1683  1.1  dyoung 		iv = arc4random();
   1684  1.1  dyoung 		for (i = 0; i < IEEE80211_WEP_IVLEN; i++) {
   1685  1.1  dyoung 			ivp[i] = iv;
   1686  1.1  dyoung 			iv >>= 8;
   1687  1.1  dyoung 		}
   1688  1.1  dyoung 		ivp[i] = sc->sc_ic.ic_wep_txkey << 6;	/* Key ID and pad */
   1689  1.1  dyoung 		memcpy(mtod(m0, caddr_t), hdrbuf, sizeof(hdrbuf));
   1690  1.1  dyoung 		/*
   1691  1.1  dyoung 		 * The ICV length must be included into hdrlen and pktlen.
   1692  1.1  dyoung 		 */
   1693  1.1  dyoung 		hdrlen = sizeof(hdrbuf) + IEEE80211_WEP_CRCLEN;
   1694  1.1  dyoung 		pktlen = m0->m_pkthdr.len + IEEE80211_WEP_CRCLEN;
   1695  1.1  dyoung 	}
   1696  1.1  dyoung 	pktlen += IEEE80211_CRC_LEN;
   1697  1.1  dyoung 
   1698  1.1  dyoung 	/*
   1699  1.1  dyoung 	 * Load the DMA map so any coalescing is done.  This
   1700  1.1  dyoung 	 * also calculates the number of descriptors we need.
   1701  1.1  dyoung 	 */
   1702  1.1  dyoung 	error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m0,
   1703  1.1  dyoung 				     ath_mbuf_load_cb, bf,
   1704  1.1  dyoung 				     BUS_DMA_NOWAIT);
   1705  1.1  dyoung 	if (error != 0) {
   1706  1.1  dyoung 		sc->sc_stats.ast_tx_busdma++;
   1707  1.1  dyoung 		m_freem(m0);
   1708  1.1  dyoung 		return error;
   1709  1.1  dyoung 	}
   1710  1.1  dyoung 	/*
   1711  1.1  dyoung 	 * Discard null packets and check for packets that
   1712  1.1  dyoung 	 * require too many TX descriptors.  We try to convert
   1713  1.1  dyoung 	 * the latter to a cluster.
   1714  1.1  dyoung 	 */
   1715  1.1  dyoung 	if (bf->bf_nseg > ATH_TXDESC) {		/* too many desc's, linearize */
   1716  1.1  dyoung 		sc->sc_stats.ast_tx_linear++;
   1717  1.1  dyoung 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   1718  1.1  dyoung 		if (m == NULL) {
   1719  1.1  dyoung 			sc->sc_stats.ast_tx_nombuf++;
   1720  1.1  dyoung 			m_freem(m0);
   1721  1.1  dyoung 			return ENOMEM;
   1722  1.1  dyoung 		}
   1723  1.1  dyoung 		M_MOVE_PKTHDR(m, m0);
   1724  1.1  dyoung 		MCLGET(m, M_DONTWAIT);
   1725  1.1  dyoung 		if ((m->m_flags & M_EXT) == 0) {
   1726  1.1  dyoung 			sc->sc_stats.ast_tx_nomcl++;
   1727  1.1  dyoung 			m_freem(m0);
   1728  1.1  dyoung 			m_free(m);
   1729  1.1  dyoung 			return ENOMEM;
   1730  1.1  dyoung 		}
   1731  1.1  dyoung 		m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
   1732  1.1  dyoung 		m_freem(m0);
   1733  1.1  dyoung 		m->m_len = m->m_pkthdr.len;
   1734  1.1  dyoung 		m0 = m;
   1735  1.1  dyoung 		error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m0,
   1736  1.1  dyoung 					     ath_mbuf_load_cb, bf,
   1737  1.1  dyoung 					     BUS_DMA_NOWAIT);
   1738  1.1  dyoung 		if (error != 0) {
   1739  1.1  dyoung 			sc->sc_stats.ast_tx_busdma++;
   1740  1.1  dyoung 			m_freem(m0);
   1741  1.1  dyoung 			return error;
   1742  1.1  dyoung 		}
   1743  1.1  dyoung 		KASSERT(bf->bf_nseg == 1,
   1744  1.1  dyoung 			("ath_tx_start: packet not one segment; nseg %u",
   1745  1.1  dyoung 			bf->bf_nseg));
   1746  1.1  dyoung 	} else if (bf->bf_nseg == 0) {		/* null packet, discard */
   1747  1.1  dyoung 		sc->sc_stats.ast_tx_nodata++;
   1748  1.1  dyoung 		m_freem(m0);
   1749  1.1  dyoung 		return EIO;
   1750  1.1  dyoung 	}
   1751  1.1  dyoung 	DPRINTF2(("ath_tx_start: m %p len %u\n", m0, pktlen));
   1752  1.1  dyoung 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
   1753  1.1  dyoung 	bf->bf_m = m0;
   1754  1.1  dyoung 	bf->bf_node = ni;			/* NB: held reference */
   1755  1.1  dyoung 
   1756  1.1  dyoung 	/* setup descriptors */
   1757  1.1  dyoung 	ds = bf->bf_desc;
   1758  1.1  dyoung 	rt = sc->sc_currates;
   1759  1.1  dyoung 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
   1760  1.1  dyoung 
   1761  1.1  dyoung 	/*
   1762  1.1  dyoung 	 * Calculate Atheros packet type from IEEE80211 packet header
   1763  1.1  dyoung 	 * and setup for rate calculations.
   1764  1.1  dyoung 	 */
   1765  1.1  dyoung 	atype = HAL_PKT_TYPE_NORMAL;			/* default */
   1766  1.1  dyoung 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
   1767  1.1  dyoung 	case IEEE80211_FC0_TYPE_MGT:
   1768  1.1  dyoung 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
   1769  1.1  dyoung 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON)
   1770  1.1  dyoung 			atype = HAL_PKT_TYPE_BEACON;
   1771  1.1  dyoung 		else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
   1772  1.1  dyoung 			atype = HAL_PKT_TYPE_PROBE_RESP;
   1773  1.1  dyoung 		else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM)
   1774  1.1  dyoung 			atype = HAL_PKT_TYPE_ATIM;
   1775  1.1  dyoung 		rix = 0;			/* XXX lowest rate */
   1776  1.1  dyoung 		break;
   1777  1.1  dyoung 	case IEEE80211_FC0_TYPE_CTL:
   1778  1.1  dyoung 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
   1779  1.1  dyoung 		if (subtype == IEEE80211_FC0_SUBTYPE_PS_POLL)
   1780  1.1  dyoung 			atype = HAL_PKT_TYPE_PSPOLL;
   1781  1.1  dyoung 		rix = 0;			/* XXX lowest rate */
   1782  1.1  dyoung 		break;
   1783  1.1  dyoung 	default:
   1784  1.1  dyoung 		rix = sc->sc_rixmap[ni->ni_rates.rs_rates[ni->ni_txrate] &
   1785  1.1  dyoung 				IEEE80211_RATE_VAL];
   1786  1.1  dyoung 		if (rix == 0xff) {
   1787  1.1  dyoung 			if_printf(ifp, "bogus xmit rate 0x%x\n",
   1788  1.1  dyoung 				ni->ni_rates.rs_rates[ni->ni_txrate]);
   1789  1.1  dyoung 			sc->sc_stats.ast_tx_badrate++;
   1790  1.1  dyoung 			m_freem(m0);
   1791  1.1  dyoung 			return EIO;
   1792  1.1  dyoung 		}
   1793  1.1  dyoung 		break;
   1794  1.1  dyoung 	}
   1795  1.1  dyoung 	/*
   1796  1.1  dyoung 	 * NB: the 802.11 layer marks whether or not we should
   1797  1.1  dyoung 	 * use short preamble based on the current mode and
   1798  1.1  dyoung 	 * negotiated parameters.
   1799  1.1  dyoung 	 */
   1800  1.1  dyoung 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) {
   1801  1.1  dyoung 		txrate = rt->info[rix].rateCode | rt->info[rix].shortPreamble;
   1802  1.1  dyoung 		shortPreamble = AH_TRUE;
   1803  1.1  dyoung 		sc->sc_stats.ast_tx_shortpre++;
   1804  1.1  dyoung 	} else {
   1805  1.1  dyoung 		txrate = rt->info[rix].rateCode;
   1806  1.1  dyoung 		shortPreamble = AH_FALSE;
   1807  1.1  dyoung 	}
   1808  1.1  dyoung 
   1809  1.1  dyoung 	/*
   1810  1.1  dyoung 	 * Calculate miscellaneous flags.
   1811  1.1  dyoung 	 */
   1812  1.1  dyoung 	flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for wep errors */
   1813  1.1  dyoung 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
   1814  1.1  dyoung 		flags |= HAL_TXDESC_NOACK;	/* no ack on broad/multicast */
   1815  1.1  dyoung 		sc->sc_stats.ast_tx_noack++;
   1816  1.1  dyoung 	} else if (pktlen > ic->ic_rtsthreshold) {
   1817  1.1  dyoung 		flags |= HAL_TXDESC_RTSENA;	/* RTS based on frame length */
   1818  1.1  dyoung 		sc->sc_stats.ast_tx_rts++;
   1819  1.1  dyoung 	}
   1820  1.1  dyoung 
   1821  1.1  dyoung 	/*
   1822  1.1  dyoung 	 * Calculate RTS/CTS rate and duration if needed.
   1823  1.1  dyoung 	 */
   1824  1.1  dyoung 	ctsduration = 0;
   1825  1.1  dyoung 	if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA)) {
   1826  1.1  dyoung 		/*
   1827  1.1  dyoung 		 * CTS transmit rate is derived from the transmit rate
   1828  1.1  dyoung 		 * by looking in the h/w rate table.  We must also factor
   1829  1.1  dyoung 		 * in whether or not a short preamble is to be used.
   1830  1.1  dyoung 		 */
   1831  1.1  dyoung 		cix = rt->info[rix].controlRate;
   1832  1.1  dyoung 		ctsrate = rt->info[cix].rateCode;
   1833  1.1  dyoung 		if (shortPreamble)
   1834  1.1  dyoung 			ctsrate |= rt->info[cix].shortPreamble;
   1835  1.1  dyoung 		/*
   1836  1.1  dyoung 		 * Compute the transmit duration based on the size
   1837  1.1  dyoung 		 * of an ACK frame.  We call into the HAL to do the
   1838  1.1  dyoung 		 * computation since it depends on the characteristics
   1839  1.1  dyoung 		 * of the actual PHY being used.
   1840  1.1  dyoung 		 */
   1841  1.1  dyoung 		if (flags & HAL_TXDESC_RTSENA) {	/* SIFS + CTS */
   1842  1.1  dyoung 			ctsduration += ath_hal_computetxtime(ah,
   1843  1.1  dyoung 				rt, IEEE80211_ACK_SIZE, cix, shortPreamble);
   1844  1.1  dyoung 		}
   1845  1.1  dyoung 		/* SIFS + data */
   1846  1.1  dyoung 		ctsduration += ath_hal_computetxtime(ah,
   1847  1.1  dyoung 			rt, pktlen, rix, shortPreamble);
   1848  1.1  dyoung 		if ((flags & HAL_TXDESC_NOACK) == 0) {	/* SIFS + ACK */
   1849  1.1  dyoung 			ctsduration += ath_hal_computetxtime(ah,
   1850  1.1  dyoung 				rt, IEEE80211_ACK_SIZE, cix, shortPreamble);
   1851  1.1  dyoung 		}
   1852  1.1  dyoung 	} else
   1853  1.1  dyoung 		ctsrate = 0;
   1854  1.1  dyoung 
   1855  1.1  dyoung 	/*
   1856  1.1  dyoung 	 * For now use the antenna on which the last good
   1857  1.1  dyoung 	 * frame was received on.  We assume this field is
   1858  1.1  dyoung 	 * initialized to 0 which gives us ``auto'' or the
   1859  1.1  dyoung 	 * ``default'' antenna.
   1860  1.1  dyoung 	 */
   1861  1.1  dyoung 	an = (struct ath_node *) ni;
   1862  1.1  dyoung 	if (an->an_tx_antenna)
   1863  1.1  dyoung 		antenna = an->an_tx_antenna;
   1864  1.1  dyoung 	else
   1865  1.1  dyoung 		antenna = an->an_rx_antenna;
   1866  1.1  dyoung 
   1867  1.1  dyoung 	/*
   1868  1.1  dyoung 	 * Formulate first tx descriptor with tx controls.
   1869  1.1  dyoung 	 */
   1870  1.1  dyoung 	/* XXX check return value? */
   1871  1.1  dyoung 	ath_hal_setuptxdesc(ah, ds
   1872  1.1  dyoung 		, pktlen		/* packet length */
   1873  1.1  dyoung 		, hdrlen		/* header length */
   1874  1.1  dyoung 		, atype			/* Atheros packet type */
   1875  1.1  dyoung 		, 60			/* txpower XXX */
   1876  1.1  dyoung 		, txrate, 1+10		/* series 0 rate/tries */
   1877  1.1  dyoung 		, iswep ? sc->sc_ic.ic_wep_txkey : HAL_TXKEYIX_INVALID
   1878  1.1  dyoung 		, antenna		/* antenna mode */
   1879  1.1  dyoung 		, flags			/* flags */
   1880  1.1  dyoung 		, ctsrate		/* rts/cts rate */
   1881  1.1  dyoung 		, ctsduration		/* rts/cts duration */
   1882  1.1  dyoung 	);
   1883  1.1  dyoung #ifdef notyet
   1884  1.1  dyoung 	ath_hal_setupxtxdesc(ah, ds
   1885  1.1  dyoung 		, AH_FALSE		/* short preamble */
   1886  1.1  dyoung 		, 0, 0			/* series 1 rate/tries */
   1887  1.1  dyoung 		, 0, 0			/* series 2 rate/tries */
   1888  1.1  dyoung 		, 0, 0			/* series 3 rate/tries */
   1889  1.1  dyoung 	);
   1890  1.1  dyoung #endif
   1891  1.1  dyoung 	/*
   1892  1.1  dyoung 	 * Fillin the remainder of the descriptor info.
   1893  1.1  dyoung 	 */
   1894  1.1  dyoung 	for (i = 0; i < bf->bf_nseg; i++, ds++) {
   1895  1.1  dyoung 		ds->ds_data = bf->bf_segs[i].ds_addr;
   1896  1.1  dyoung 		if (i == bf->bf_nseg - 1)
   1897  1.1  dyoung 			ds->ds_link = 0;
   1898  1.1  dyoung 		else
   1899  1.1  dyoung 			ds->ds_link = bf->bf_daddr + sizeof(*ds) * (i + 1);
   1900  1.1  dyoung 		ath_hal_filltxdesc(ah, ds
   1901  1.1  dyoung 			, bf->bf_segs[i].ds_len	/* segment length */
   1902  1.1  dyoung 			, i == 0		/* first segment */
   1903  1.1  dyoung 			, i == bf->bf_nseg - 1	/* last segment */
   1904  1.1  dyoung 		);
   1905  1.1  dyoung 		DPRINTF2(("ath_tx_start: %d: %08x %08x %08x %08x %08x %08x\n",
   1906  1.1  dyoung 		    i, ds->ds_link, ds->ds_data, ds->ds_ctl0, ds->ds_ctl1,
   1907  1.1  dyoung 		    ds->ds_hw[0], ds->ds_hw[1]));
   1908  1.1  dyoung 	}
   1909  1.1  dyoung 
   1910  1.1  dyoung 	/*
   1911  1.1  dyoung 	 * Insert the frame on the outbound list and
   1912  1.1  dyoung 	 * pass it on to the hardware.
   1913  1.1  dyoung 	 */
   1914  1.1  dyoung 	mtx_lock(&sc->sc_txqlock);
   1915  1.1  dyoung 	TAILQ_INSERT_TAIL(&sc->sc_txq, bf, bf_list);
   1916  1.1  dyoung 	if (sc->sc_txlink == NULL) {
   1917  1.1  dyoung 		ath_hal_puttxbuf(ah, sc->sc_txhalq, bf->bf_daddr);
   1918  1.1  dyoung 		DPRINTF2(("ath_tx_start: TXDP0 = %p (%p)\n",
   1919  1.1  dyoung 		    (caddr_t)bf->bf_daddr, bf->bf_desc));
   1920  1.1  dyoung 	} else {
   1921  1.1  dyoung 		*sc->sc_txlink = bf->bf_daddr;
   1922  1.1  dyoung 		DPRINTF2(("ath_tx_start: link(%p)=%p (%p)\n",
   1923  1.1  dyoung 		    sc->sc_txlink, (caddr_t)bf->bf_daddr, bf->bf_desc));
   1924  1.1  dyoung 	}
   1925  1.1  dyoung 	sc->sc_txlink = &bf->bf_desc[bf->bf_nseg - 1].ds_link;
   1926  1.1  dyoung 	mtx_unlock(&sc->sc_txqlock);
   1927  1.1  dyoung 
   1928  1.1  dyoung 	ath_hal_txstart(ah, sc->sc_txhalq);
   1929  1.1  dyoung 	return 0;
   1930  1.1  dyoung }
   1931  1.1  dyoung 
   1932  1.1  dyoung static void
   1933  1.1  dyoung ath_tx_proc(void *arg, int npending)
   1934  1.1  dyoung {
   1935  1.1  dyoung 	struct ath_softc *sc = arg;
   1936  1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   1937  1.1  dyoung 	struct ath_buf *bf;
   1938  1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   1939  1.1  dyoung 	struct ifnet *ifp = &ic->ic_if;
   1940  1.1  dyoung 	struct ath_desc *ds;
   1941  1.1  dyoung 	struct ieee80211_node *ni;
   1942  1.1  dyoung 	struct ath_node *an;
   1943  1.1  dyoung 	int sr, lr;
   1944  1.1  dyoung 	HAL_STATUS status;
   1945  1.1  dyoung 
   1946  1.1  dyoung 	DPRINTF2(("ath_tx_proc: pending %u tx queue %p, link %p\n",
   1947  1.1  dyoung 		npending, (caddr_t) ath_hal_gettxbuf(sc->sc_ah, sc->sc_txhalq),
   1948  1.1  dyoung 		sc->sc_txlink));
   1949  1.1  dyoung 	for (;;) {
   1950  1.1  dyoung 		mtx_lock(&sc->sc_txqlock);
   1951  1.1  dyoung 		bf = TAILQ_FIRST(&sc->sc_txq);
   1952  1.1  dyoung 		if (bf == NULL) {
   1953  1.1  dyoung 			sc->sc_txlink = NULL;
   1954  1.1  dyoung 			mtx_unlock(&sc->sc_txqlock);
   1955  1.1  dyoung 			break;
   1956  1.1  dyoung 		}
   1957  1.1  dyoung 		/* only the last descriptor is needed */
   1958  1.1  dyoung 		ds = &bf->bf_desc[bf->bf_nseg - 1];
   1959  1.1  dyoung 		status = ath_hal_txprocdesc(ah, ds);
   1960  1.1  dyoung #ifdef AR_DEBUG
   1961  1.1  dyoung 		if (ath_debug > 1)
   1962  1.1  dyoung 			ath_printtxbuf(bf, status == HAL_OK);
   1963  1.1  dyoung #endif
   1964  1.1  dyoung 		if (status == HAL_EINPROGRESS) {
   1965  1.1  dyoung 			mtx_unlock(&sc->sc_txqlock);
   1966  1.1  dyoung 			break;
   1967  1.1  dyoung 		}
   1968  1.1  dyoung 		TAILQ_REMOVE(&sc->sc_txq, bf, bf_list);
   1969  1.1  dyoung 		mtx_unlock(&sc->sc_txqlock);
   1970  1.1  dyoung 
   1971  1.1  dyoung 		ni = bf->bf_node;
   1972  1.1  dyoung 		if (ni != NULL) {
   1973  1.1  dyoung 			an = (struct ath_node *) ni;
   1974  1.1  dyoung 			if (ds->ds_txstat.ts_status == 0) {
   1975  1.1  dyoung 				an->an_tx_ok++;
   1976  1.1  dyoung 				an->an_tx_antenna = ds->ds_txstat.ts_antenna;
   1977  1.1  dyoung 			} else {
   1978  1.1  dyoung 				an->an_tx_err++;
   1979  1.1  dyoung 				ifp->if_oerrors++;
   1980  1.1  dyoung 				if (ds->ds_txstat.ts_status & HAL_TXERR_XRETRY)
   1981  1.1  dyoung 					sc->sc_stats.ast_tx_xretries++;
   1982  1.1  dyoung 				if (ds->ds_txstat.ts_status & HAL_TXERR_FIFO)
   1983  1.1  dyoung 					sc->sc_stats.ast_tx_fifoerr++;
   1984  1.1  dyoung 				if (ds->ds_txstat.ts_status & HAL_TXERR_FILT)
   1985  1.1  dyoung 					sc->sc_stats.ast_tx_filtered++;
   1986  1.1  dyoung 				an->an_tx_antenna = 0;	/* invalidate */
   1987  1.1  dyoung 			}
   1988  1.1  dyoung 			sr = ds->ds_txstat.ts_shortretry;
   1989  1.1  dyoung 			lr = ds->ds_txstat.ts_longretry;
   1990  1.1  dyoung 			sc->sc_stats.ast_tx_shortretry += sr;
   1991  1.1  dyoung 			sc->sc_stats.ast_tx_longretry += lr;
   1992  1.1  dyoung 			if (sr + lr)
   1993  1.1  dyoung 				an->an_tx_retr++;
   1994  1.1  dyoung 			/*
   1995  1.1  dyoung 			 * Reclaim reference to node.
   1996  1.1  dyoung 			 *
   1997  1.1  dyoung 			 * NB: the node may be reclaimed here if, for example
   1998  1.1  dyoung 			 *     this is a DEAUTH message that was sent and the
   1999  1.1  dyoung 			 *     node was timed out due to inactivity.
   2000  1.1  dyoung 			 */
   2001  1.1  dyoung 			if (ni != ic->ic_bss)
   2002  1.1  dyoung 				ieee80211_free_node(ic, ni);
   2003  1.1  dyoung 		}
   2004  1.1  dyoung 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
   2005  1.1  dyoung 		    BUS_DMASYNC_POSTWRITE);
   2006  1.1  dyoung 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
   2007  1.1  dyoung 		m_freem(bf->bf_m);
   2008  1.1  dyoung 		bf->bf_m = NULL;
   2009  1.1  dyoung 		bf->bf_node = NULL;
   2010  1.1  dyoung 
   2011  1.1  dyoung 		mtx_lock(&sc->sc_txbuflock);
   2012  1.1  dyoung 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
   2013  1.1  dyoung 		mtx_unlock(&sc->sc_txbuflock);
   2014  1.1  dyoung 	}
   2015  1.1  dyoung 	ifp->if_flags &= ~IFF_OACTIVE;
   2016  1.1  dyoung 	sc->sc_tx_timer = 0;
   2017  1.1  dyoung 
   2018  1.1  dyoung 	ath_start(ifp);
   2019  1.1  dyoung }
   2020  1.1  dyoung 
   2021  1.1  dyoung /*
   2022  1.1  dyoung  * Drain the transmit queue and reclaim resources.
   2023  1.1  dyoung  */
   2024  1.1  dyoung static void
   2025  1.1  dyoung ath_draintxq(struct ath_softc *sc)
   2026  1.1  dyoung {
   2027  1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   2028  1.1  dyoung 	struct ifnet *ifp = &sc->sc_ic.ic_if;
   2029  1.1  dyoung 	struct ath_buf *bf;
   2030  1.1  dyoung 
   2031  1.1  dyoung 	/* XXX return value */
   2032  1.1  dyoung 	if (!sc->sc_invalid) {
   2033  1.1  dyoung 		/* don't touch the hardware if marked invalid */
   2034  1.1  dyoung 		(void) ath_hal_stoptxdma(ah, sc->sc_txhalq);
   2035  1.1  dyoung 		DPRINTF(("ath_draintxq: tx queue %p, link %p\n",
   2036  1.1  dyoung 		    (caddr_t) ath_hal_gettxbuf(ah, sc->sc_txhalq),
   2037  1.1  dyoung 		    sc->sc_txlink));
   2038  1.1  dyoung 		(void) ath_hal_stoptxdma(ah, sc->sc_bhalq);
   2039  1.1  dyoung 		DPRINTF(("ath_draintxq: beacon queue %p\n",
   2040  1.1  dyoung 		    (caddr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq)));
   2041  1.1  dyoung 	}
   2042  1.1  dyoung 	for (;;) {
   2043  1.1  dyoung 		mtx_lock(&sc->sc_txqlock);
   2044  1.1  dyoung 		bf = TAILQ_FIRST(&sc->sc_txq);
   2045  1.1  dyoung 		if (bf == NULL) {
   2046  1.1  dyoung 			sc->sc_txlink = NULL;
   2047  1.1  dyoung 			mtx_unlock(&sc->sc_txqlock);
   2048  1.1  dyoung 			break;
   2049  1.1  dyoung 		}
   2050  1.1  dyoung 		TAILQ_REMOVE(&sc->sc_txq, bf, bf_list);
   2051  1.1  dyoung 		mtx_unlock(&sc->sc_txqlock);
   2052  1.1  dyoung #ifdef AR_DEBUG
   2053  1.1  dyoung 		if (ath_debug)
   2054  1.1  dyoung 			ath_printtxbuf(bf,
   2055  1.1  dyoung 				ath_hal_txprocdesc(ah, bf->bf_desc) == HAL_OK);
   2056  1.1  dyoung #endif /* AR_DEBUG */
   2057  1.1  dyoung 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
   2058  1.1  dyoung 		m_freem(bf->bf_m);
   2059  1.1  dyoung 		bf->bf_m = NULL;
   2060  1.1  dyoung 		bf->bf_node = NULL;
   2061  1.1  dyoung 		mtx_lock(&sc->sc_txbuflock);
   2062  1.1  dyoung 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
   2063  1.1  dyoung 		mtx_unlock(&sc->sc_txbuflock);
   2064  1.1  dyoung 	}
   2065  1.1  dyoung 	ifp->if_flags &= ~IFF_OACTIVE;
   2066  1.1  dyoung 	sc->sc_tx_timer = 0;
   2067  1.1  dyoung }
   2068  1.1  dyoung 
   2069  1.1  dyoung /*
   2070  1.1  dyoung  * Disable the receive h/w in preparation for a reset.
   2071  1.1  dyoung  */
   2072  1.1  dyoung static void
   2073  1.1  dyoung ath_stoprecv(struct ath_softc *sc)
   2074  1.1  dyoung {
   2075  1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   2076  1.1  dyoung 
   2077  1.1  dyoung 	ath_hal_stoppcurecv(ah);	/* disable PCU */
   2078  1.1  dyoung 	ath_hal_setrxfilter(ah, 0);	/* clear recv filter */
   2079  1.1  dyoung 	ath_hal_stopdmarecv(ah);	/* disable DMA engine */
   2080  1.1  dyoung 	DELAY(3000);			/* long enough for 1 frame */
   2081  1.1  dyoung #ifdef AR_DEBUG
   2082  1.1  dyoung 	if (ath_debug) {
   2083  1.1  dyoung 		struct ath_buf *bf;
   2084  1.1  dyoung 
   2085  1.1  dyoung 		DPRINTF(("ath_stoprecv: rx queue %p, link %p\n",
   2086  1.1  dyoung 		    (caddr_t) ath_hal_getrxbuf(ah), sc->sc_rxlink));
   2087  1.1  dyoung 		TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
   2088  1.1  dyoung 			if (ath_hal_rxprocdesc(ah, bf->bf_desc) == HAL_OK)
   2089  1.1  dyoung 				ath_printrxbuf(bf, 1);
   2090  1.1  dyoung 		}
   2091  1.1  dyoung 	}
   2092  1.1  dyoung #endif
   2093  1.1  dyoung 	sc->sc_rxlink = NULL;		/* just in case */
   2094  1.1  dyoung }
   2095  1.1  dyoung 
   2096  1.1  dyoung /*
   2097  1.1  dyoung  * Enable the receive h/w following a reset.
   2098  1.1  dyoung  */
   2099  1.1  dyoung static int
   2100  1.1  dyoung ath_startrecv(struct ath_softc *sc)
   2101  1.1  dyoung {
   2102  1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   2103  1.1  dyoung 	struct ath_buf *bf;
   2104  1.1  dyoung 
   2105  1.1  dyoung 	sc->sc_rxlink = NULL;
   2106  1.1  dyoung 	TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
   2107  1.1  dyoung 		int error = ath_rxbuf_init(sc, bf);
   2108  1.1  dyoung 		if (error != 0) {
   2109  1.1  dyoung 			DPRINTF(("ath_startrecv: ath_rxbuf_init failed %d\n",
   2110  1.1  dyoung 				error));
   2111  1.1  dyoung 			return error;
   2112  1.1  dyoung 		}
   2113  1.1  dyoung 	}
   2114  1.1  dyoung 
   2115  1.1  dyoung 	bf = TAILQ_FIRST(&sc->sc_rxbuf);
   2116  1.1  dyoung 	ath_hal_putrxbuf(ah, bf->bf_daddr);
   2117  1.1  dyoung 	ath_hal_rxena(ah);		/* enable recv descriptors */
   2118  1.1  dyoung 	ath_mode_init(sc);		/* set filters, etc. */
   2119  1.1  dyoung 	ath_hal_startpcurecv(ah);	/* re-enable PCU/DMA engine */
   2120  1.1  dyoung 	return 0;
   2121  1.1  dyoung }
   2122  1.1  dyoung 
   2123  1.1  dyoung /*
   2124  1.1  dyoung  * Set/change channels.  If the channel is really being changed,
   2125  1.1  dyoung  * it's done by resetting the chip.  To accomplish this we must
   2126  1.1  dyoung  * first cleanup any pending DMA, then restart stuff after a la
   2127  1.1  dyoung  * ath_init.
   2128  1.1  dyoung  */
   2129  1.1  dyoung static int
   2130  1.1  dyoung ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan)
   2131  1.1  dyoung {
   2132  1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   2133  1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   2134  1.1  dyoung 
   2135  1.1  dyoung 	DPRINTF(("ath_chan_set: %u (%u MHz) -> %u (%u MHz)\n",
   2136  1.1  dyoung 	    ieee80211_chan2ieee(ic, ic->ic_ibss_chan),
   2137  1.1  dyoung 		ic->ic_ibss_chan->ic_freq,
   2138  1.1  dyoung 	    ieee80211_chan2ieee(ic, chan), chan->ic_freq));
   2139  1.1  dyoung 	if (chan != ic->ic_ibss_chan) {
   2140  1.1  dyoung 		HAL_STATUS status;
   2141  1.1  dyoung 		HAL_CHANNEL hchan;
   2142  1.1  dyoung 		enum ieee80211_phymode mode;
   2143  1.1  dyoung 
   2144  1.1  dyoung 		/*
   2145  1.1  dyoung 		 * To switch channels clear any pending DMA operations;
   2146  1.1  dyoung 		 * wait long enough for the RX fifo to drain, reset the
   2147  1.1  dyoung 		 * hardware at the new frequency, and then re-enable
   2148  1.1  dyoung 		 * the relevant bits of the h/w.
   2149  1.1  dyoung 		 */
   2150  1.1  dyoung 		ath_hal_intrset(ah, 0);		/* disable interrupts */
   2151  1.1  dyoung 		ath_draintxq(sc);		/* clear pending tx frames */
   2152  1.1  dyoung 		ath_stoprecv(sc);		/* turn off frame recv */
   2153  1.1  dyoung 		/*
   2154  1.1  dyoung 		 * Convert to a HAL channel description with
   2155  1.1  dyoung 		 * the flags constrained to reflect the current
   2156  1.1  dyoung 		 * operating mode.
   2157  1.1  dyoung 		 */
   2158  1.1  dyoung 		hchan.channel = chan->ic_freq;
   2159  1.1  dyoung 		hchan.channelFlags = ath_chan2flags(ic, chan);
   2160  1.1  dyoung 		if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_TRUE, &status)) {
   2161  1.1  dyoung 			if_printf(&ic->ic_if, "ath_chan_set: unable to reset "
   2162  1.1  dyoung 				"channel %u (%u Mhz)\n",
   2163  1.1  dyoung 				ieee80211_chan2ieee(ic, chan), chan->ic_freq);
   2164  1.1  dyoung 			return EIO;
   2165  1.1  dyoung 		}
   2166  1.1  dyoung 		/*
   2167  1.1  dyoung 		 * Re-enable rx framework.
   2168  1.1  dyoung 		 */
   2169  1.1  dyoung 		if (ath_startrecv(sc) != 0) {
   2170  1.1  dyoung 			if_printf(&ic->ic_if,
   2171  1.1  dyoung 				"ath_chan_set: unable to restart recv logic\n");
   2172  1.1  dyoung 			return EIO;
   2173  1.1  dyoung 		}
   2174  1.1  dyoung 
   2175  1.1  dyoung 		/*
   2176  1.1  dyoung 		 * Update BPF state.
   2177  1.1  dyoung 		 */
   2178  1.1  dyoung 		sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
   2179  1.1  dyoung 			htole16(chan->ic_freq);
   2180  1.1  dyoung 		sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =
   2181  1.1  dyoung 			htole16(chan->ic_flags);
   2182  1.1  dyoung 
   2183  1.1  dyoung 		/*
   2184  1.1  dyoung 		 * Change channels and update the h/w rate map
   2185  1.1  dyoung 		 * if we're switching; e.g. 11a to 11b/g.
   2186  1.1  dyoung 		 */
   2187  1.1  dyoung 		ic->ic_ibss_chan = chan;
   2188  1.1  dyoung 		mode = ieee80211_chan2mode(ic, chan);
   2189  1.1  dyoung 		if (mode != sc->sc_curmode)
   2190  1.1  dyoung 			ath_setcurmode(sc, mode);
   2191  1.1  dyoung 
   2192  1.1  dyoung 		/*
   2193  1.1  dyoung 		 * Re-enable interrupts.
   2194  1.1  dyoung 		 */
   2195  1.1  dyoung 		ath_hal_intrset(ah, sc->sc_imask);
   2196  1.1  dyoung 	}
   2197  1.1  dyoung 	return 0;
   2198  1.1  dyoung }
   2199  1.1  dyoung 
   2200  1.1  dyoung static void
   2201  1.1  dyoung ath_next_scan(void *arg)
   2202  1.1  dyoung {
   2203  1.1  dyoung 	struct ath_softc *sc = arg;
   2204  1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   2205  1.1  dyoung 	struct ifnet *ifp = &ic->ic_if;
   2206  1.1  dyoung 
   2207  1.1  dyoung 	if (ic->ic_state == IEEE80211_S_SCAN)
   2208  1.1  dyoung 		ieee80211_next_scan(ifp);
   2209  1.1  dyoung }
   2210  1.1  dyoung 
   2211  1.1  dyoung /*
   2212  1.1  dyoung  * Periodically recalibrate the PHY to account
   2213  1.1  dyoung  * for temperature/environment changes.
   2214  1.1  dyoung  */
   2215  1.1  dyoung static void
   2216  1.1  dyoung ath_calibrate(void *arg)
   2217  1.1  dyoung {
   2218  1.1  dyoung 	struct ath_softc *sc = arg;
   2219  1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   2220  1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   2221  1.1  dyoung 	struct ieee80211_channel *c;
   2222  1.1  dyoung 	HAL_CHANNEL hchan;
   2223  1.1  dyoung 
   2224  1.1  dyoung 	sc->sc_stats.ast_per_cal++;
   2225  1.1  dyoung 
   2226  1.1  dyoung 	/*
   2227  1.1  dyoung 	 * Convert to a HAL channel description with the flags
   2228  1.1  dyoung 	 * constrained to reflect the current operating mode.
   2229  1.1  dyoung 	 */
   2230  1.1  dyoung 	c = ic->ic_ibss_chan;
   2231  1.1  dyoung 	hchan.channel = c->ic_freq;
   2232  1.1  dyoung 	hchan.channelFlags = ath_chan2flags(ic, c);
   2233  1.1  dyoung 
   2234  1.1  dyoung 	DPRINTF(("%s: channel %u/%x\n", __func__, c->ic_freq, c->ic_flags));
   2235  1.1  dyoung 
   2236  1.1  dyoung 	if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) {
   2237  1.1  dyoung 		/*
   2238  1.1  dyoung 		 * Rfgain is out of bounds, reset the chip
   2239  1.1  dyoung 		 * to load new gain values.
   2240  1.1  dyoung 		 */
   2241  1.1  dyoung 		sc->sc_stats.ast_per_rfgain++;
   2242  1.1  dyoung 		ath_reset(sc);
   2243  1.1  dyoung 	}
   2244  1.1  dyoung 	if (!ath_hal_calibrate(ah, &hchan)) {
   2245  1.1  dyoung 		DPRINTF(("%s: calibration of channel %u failed\n",
   2246  1.1  dyoung 			__func__, c->ic_freq));
   2247  1.1  dyoung 		sc->sc_stats.ast_per_calfail++;
   2248  1.1  dyoung 	}
   2249  1.1  dyoung 	callout_reset(&sc->sc_cal_ch, hz * ath_calinterval, ath_calibrate, sc);
   2250  1.1  dyoung }
   2251  1.1  dyoung 
   2252  1.1  dyoung static int
   2253  1.1  dyoung ath_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
   2254  1.1  dyoung {
   2255  1.1  dyoung 	struct ifnet *ifp = &ic->ic_if;
   2256  1.1  dyoung 	struct ath_softc *sc = ifp->if_softc;
   2257  1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   2258  1.1  dyoung 	struct ieee80211_node *ni;
   2259  1.1  dyoung 	int i, error;
   2260  1.1  dyoung 	u_int8_t *bssid;
   2261  1.1  dyoung 	u_int32_t rfilt;
   2262  1.1  dyoung 	static const HAL_LED_STATE leds[] = {
   2263  1.1  dyoung 	    HAL_LED_INIT,	/* IEEE80211_S_INIT */
   2264  1.1  dyoung 	    HAL_LED_SCAN,	/* IEEE80211_S_SCAN */
   2265  1.1  dyoung 	    HAL_LED_AUTH,	/* IEEE80211_S_AUTH */
   2266  1.1  dyoung 	    HAL_LED_ASSOC, 	/* IEEE80211_S_ASSOC */
   2267  1.1  dyoung 	    HAL_LED_RUN, 	/* IEEE80211_S_RUN */
   2268  1.1  dyoung 	};
   2269  1.1  dyoung 
   2270  1.1  dyoung 	DPRINTF(("%s: %s -> %s\n", __func__,
   2271  1.1  dyoung 		ieee80211_state_name[ic->ic_state],
   2272  1.1  dyoung 		ieee80211_state_name[nstate]));
   2273  1.1  dyoung 
   2274  1.1  dyoung 	ath_hal_setledstate(ah, leds[nstate]);	/* set LED */
   2275  1.1  dyoung 
   2276  1.1  dyoung 	if (nstate == IEEE80211_S_INIT) {
   2277  1.1  dyoung 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
   2278  1.1  dyoung 		ath_hal_intrset(ah, sc->sc_imask);
   2279  1.1  dyoung 		callout_stop(&sc->sc_scan_ch);
   2280  1.1  dyoung 		callout_stop(&sc->sc_cal_ch);
   2281  1.1  dyoung 		return (*sc->sc_newstate)(ic, nstate, arg);
   2282  1.1  dyoung 	}
   2283  1.1  dyoung 	ni = ic->ic_bss;
   2284  1.1  dyoung 	error = ath_chan_set(sc, ni->ni_chan);
   2285  1.1  dyoung 	if (error != 0)
   2286  1.1  dyoung 		goto bad;
   2287  1.1  dyoung 	rfilt = (ath_hal_getrxfilter(ah) & HAL_RX_FILTER_PHYERR)
   2288  1.1  dyoung 	      | HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST;
   2289  1.1  dyoung 	if (ic->ic_opmode != IEEE80211_M_STA)
   2290  1.1  dyoung 		rfilt |= HAL_RX_FILTER_PROBEREQ;
   2291  1.1  dyoung 	if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
   2292  1.1  dyoung 	    (ifp->if_flags & IFF_PROMISC))
   2293  1.1  dyoung 		rfilt |= HAL_RX_FILTER_PROM;
   2294  1.1  dyoung 	if (nstate == IEEE80211_S_SCAN) {
   2295  1.1  dyoung 		callout_reset(&sc->sc_scan_ch, (hz * ath_dwelltime) / 1000,
   2296  1.1  dyoung 			ath_next_scan, sc);
   2297  1.1  dyoung 		bssid = ifp->if_broadcastaddr;
   2298  1.1  dyoung 		rfilt |= HAL_RX_FILTER_BEACON;
   2299  1.1  dyoung 	} else {
   2300  1.1  dyoung 		callout_stop(&sc->sc_scan_ch);
   2301  1.1  dyoung 		bssid = ni->ni_bssid;
   2302  1.1  dyoung 	}
   2303  1.1  dyoung 	ath_hal_setrxfilter(ah, rfilt);
   2304  1.1  dyoung 	DPRINTF(("%s: RX filter 0x%x bssid %s\n",
   2305  1.1  dyoung 		 __func__, rfilt, ether_sprintf(bssid)));
   2306  1.1  dyoung 
   2307  1.1  dyoung 	if (nstate == IEEE80211_S_RUN && ic->ic_opmode == IEEE80211_M_STA)
   2308  1.1  dyoung 		ath_hal_setassocid(ah, bssid, ni->ni_associd);
   2309  1.1  dyoung 	else
   2310  1.1  dyoung 		ath_hal_setassocid(ah, bssid, 0);
   2311  1.1  dyoung 	if (ic->ic_flags & IEEE80211_F_WEPON) {
   2312  1.1  dyoung 		for (i = 0; i < IEEE80211_WEP_NKID; i++)
   2313  1.1  dyoung 			if (ath_hal_keyisvalid(ah, i))
   2314  1.1  dyoung 				ath_hal_keysetmac(ah, i, bssid);
   2315  1.1  dyoung 	}
   2316  1.1  dyoung 
   2317  1.1  dyoung 	if (nstate == IEEE80211_S_RUN) {
   2318  1.1  dyoung 		DPRINTF(("%s(RUN): ic_flags=0x%08x iv=%d bssid=%s "
   2319  1.1  dyoung 			"capinfo=0x%04x chan=%d\n"
   2320  1.1  dyoung 			 , __func__
   2321  1.1  dyoung 			 , ic->ic_flags
   2322  1.1  dyoung 			 , ni->ni_intval
   2323  1.1  dyoung 			 , ether_sprintf(ni->ni_bssid)
   2324  1.1  dyoung 			 , ni->ni_capinfo
   2325  1.1  dyoung 			 , ieee80211_chan2ieee(ic, ni->ni_chan)));
   2326  1.1  dyoung 
   2327  1.1  dyoung 		/*
   2328  1.1  dyoung 		 * Allocate and setup the beacon frame for AP or adhoc mode.
   2329  1.1  dyoung 		 */
   2330  1.1  dyoung 		if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
   2331  1.1  dyoung 		    ic->ic_opmode == IEEE80211_M_IBSS) {
   2332  1.1  dyoung 			error = ath_beacon_alloc(sc, ni);
   2333  1.1  dyoung 			if (error != 0)
   2334  1.1  dyoung 				goto bad;
   2335  1.1  dyoung 		}
   2336  1.1  dyoung 
   2337  1.1  dyoung 		/*
   2338  1.1  dyoung 		 * Configure the beacon and sleep timers.
   2339  1.1  dyoung 		 */
   2340  1.1  dyoung 		ath_beacon_config(sc);
   2341  1.1  dyoung 
   2342  1.1  dyoung 		/* start periodic recalibration timer */
   2343  1.1  dyoung 		callout_reset(&sc->sc_cal_ch, hz * ath_calinterval,
   2344  1.1  dyoung 			ath_calibrate, sc);
   2345  1.1  dyoung 	} else {
   2346  1.1  dyoung 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
   2347  1.1  dyoung 		ath_hal_intrset(ah, sc->sc_imask);
   2348  1.1  dyoung 		callout_stop(&sc->sc_cal_ch);		/* no calibration */
   2349  1.1  dyoung 	}
   2350  1.1  dyoung 	/*
   2351  1.1  dyoung 	 * Reset the rate control state.
   2352  1.1  dyoung 	 */
   2353  1.1  dyoung 	ath_rate_ctl_reset(sc, nstate);
   2354  1.1  dyoung 	/*
   2355  1.1  dyoung 	 * Invoke the parent method to complete the work.
   2356  1.1  dyoung 	 */
   2357  1.1  dyoung 	return (*sc->sc_newstate)(ic, nstate, arg);
   2358  1.1  dyoung bad:
   2359  1.1  dyoung 	callout_stop(&sc->sc_scan_ch);
   2360  1.1  dyoung 	callout_stop(&sc->sc_cal_ch);
   2361  1.1  dyoung 	/* NB: do not invoke the parent */
   2362  1.1  dyoung 	return error;
   2363  1.1  dyoung }
   2364  1.1  dyoung 
   2365  1.1  dyoung /*
   2366  1.1  dyoung  * Setup driver-specific state for a newly associated node.
   2367  1.1  dyoung  * Note that we're called also on a re-associate, the isnew
   2368  1.1  dyoung  * param tells us if this is the first time or not.
   2369  1.1  dyoung  */
   2370  1.1  dyoung static void
   2371  1.1  dyoung ath_newassoc(struct ieee80211com *ic, struct ieee80211_node *ni, int isnew)
   2372  1.1  dyoung {
   2373  1.1  dyoung 	if (isnew) {
   2374  1.1  dyoung 		struct ath_node *an = (struct ath_node *) ni;
   2375  1.1  dyoung 
   2376  1.1  dyoung 		an->an_tx_ok = an->an_tx_err =
   2377  1.1  dyoung 			an->an_tx_retr = an->an_tx_upper = 0;
   2378  1.1  dyoung 		/* start with highest negotiated rate */
   2379  1.1  dyoung 		/*
   2380  1.1  dyoung 		 * XXX should do otherwise but only when
   2381  1.1  dyoung 		 * the rate control algorithm is better.
   2382  1.1  dyoung 		 */
   2383  1.1  dyoung 		KASSERT(ni->ni_rates.rs_nrates > 0,
   2384  1.1  dyoung 			("new association w/ no rates!"));
   2385  1.1  dyoung 		ni->ni_txrate = ni->ni_rates.rs_nrates - 1;
   2386  1.1  dyoung 	}
   2387  1.1  dyoung }
   2388  1.1  dyoung 
   2389  1.1  dyoung static int
   2390  1.1  dyoung ath_getchannels(struct ath_softc *sc, u_int cc, HAL_BOOL outdoor)
   2391  1.1  dyoung {
   2392  1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   2393  1.1  dyoung 	struct ifnet *ifp = &ic->ic_if;
   2394  1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   2395  1.1  dyoung 	HAL_CHANNEL *chans;
   2396  1.1  dyoung 	int i, ix, nchan;
   2397  1.1  dyoung 
   2398  1.1  dyoung 	sc->sc_have11g = 0;
   2399  1.1  dyoung 	chans = malloc(IEEE80211_CHAN_MAX * sizeof(HAL_CHANNEL),
   2400  1.1  dyoung 			M_TEMP, M_NOWAIT);
   2401  1.1  dyoung 	if (chans == NULL) {
   2402  1.1  dyoung 		if_printf(ifp, "unable to allocate channel table\n");
   2403  1.1  dyoung 		return ENOMEM;
   2404  1.1  dyoung 	}
   2405  1.1  dyoung 	if (!ath_hal_init_channels(ah, chans, IEEE80211_CHAN_MAX, &nchan,
   2406  1.1  dyoung 	    cc, HAL_MODE_ALL, outdoor)) {
   2407  1.1  dyoung 		if_printf(ifp, "unable to collect channel list from hal\n");
   2408  1.1  dyoung 		free(chans, M_TEMP);
   2409  1.1  dyoung 		return EINVAL;
   2410  1.1  dyoung 	}
   2411  1.1  dyoung 
   2412  1.1  dyoung 	/*
   2413  1.1  dyoung 	 * Convert HAL channels to ieee80211 ones and insert
   2414  1.1  dyoung 	 * them in the table according to their channel number.
   2415  1.1  dyoung 	 */
   2416  1.1  dyoung 	for (i = 0; i < nchan; i++) {
   2417  1.1  dyoung 		HAL_CHANNEL *c = &chans[i];
   2418  1.1  dyoung 		ix = ath_hal_mhz2ieee(c->channel, c->channelFlags);
   2419  1.1  dyoung 		if (ix > IEEE80211_CHAN_MAX) {
   2420  1.1  dyoung 			if_printf(ifp, "bad hal channel %u (%u/%x) ignored\n",
   2421  1.1  dyoung 				ix, c->channel, c->channelFlags);
   2422  1.1  dyoung 			continue;
   2423  1.1  dyoung 		}
   2424  1.1  dyoung 		/* NB: flags are known to be compatible */
   2425  1.1  dyoung 		if (ic->ic_channels[ix].ic_freq == 0) {
   2426  1.1  dyoung 			ic->ic_channels[ix].ic_freq = c->channel;
   2427  1.1  dyoung 			ic->ic_channels[ix].ic_flags = c->channelFlags;
   2428  1.1  dyoung 		} else {
   2429  1.1  dyoung 			/* channels overlap; e.g. 11g and 11b */
   2430  1.1  dyoung 			ic->ic_channels[ix].ic_flags |= c->channelFlags;
   2431  1.1  dyoung 		}
   2432  1.1  dyoung 		if ((c->channelFlags & CHANNEL_G) == CHANNEL_G)
   2433  1.1  dyoung 			sc->sc_have11g = 1;
   2434  1.1  dyoung 	}
   2435  1.1  dyoung 	free(chans, M_TEMP);
   2436  1.1  dyoung 	return 0;
   2437  1.1  dyoung }
   2438  1.1  dyoung 
   2439  1.1  dyoung static int
   2440  1.1  dyoung ath_rate_setup(struct ath_softc *sc, u_int mode)
   2441  1.1  dyoung {
   2442  1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   2443  1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   2444  1.1  dyoung 	const HAL_RATE_TABLE *rt;
   2445  1.1  dyoung 	struct ieee80211_rateset *rs;
   2446  1.1  dyoung 	int i, maxrates;
   2447  1.1  dyoung 
   2448  1.1  dyoung 	switch (mode) {
   2449  1.1  dyoung 	case IEEE80211_MODE_11A:
   2450  1.1  dyoung 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11A);
   2451  1.1  dyoung 		break;
   2452  1.1  dyoung 	case IEEE80211_MODE_11B:
   2453  1.1  dyoung 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11B);
   2454  1.1  dyoung 		break;
   2455  1.1  dyoung 	case IEEE80211_MODE_11G:
   2456  1.1  dyoung 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11G);
   2457  1.1  dyoung 		break;
   2458  1.1  dyoung 	case IEEE80211_MODE_TURBO:
   2459  1.1  dyoung 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_TURBO);
   2460  1.1  dyoung 		break;
   2461  1.1  dyoung 	default:
   2462  1.1  dyoung 		DPRINTF(("%s: invalid mode %u\n", __func__, mode));
   2463  1.1  dyoung 		return 0;
   2464  1.1  dyoung 	}
   2465  1.1  dyoung 	rt = sc->sc_rates[mode];
   2466  1.1  dyoung 	if (rt == NULL)
   2467  1.1  dyoung 		return 0;
   2468  1.1  dyoung 	if (rt->rateCount > IEEE80211_RATE_MAXSIZE) {
   2469  1.1  dyoung 		DPRINTF(("%s: rate table too small (%u > %u)\n",
   2470  1.1  dyoung 			__func__, rt->rateCount, IEEE80211_RATE_MAXSIZE));
   2471  1.1  dyoung 		maxrates = IEEE80211_RATE_MAXSIZE;
   2472  1.1  dyoung 	} else
   2473  1.1  dyoung 		maxrates = rt->rateCount;
   2474  1.1  dyoung 	rs = &ic->ic_sup_rates[mode];
   2475  1.1  dyoung 	for (i = 0; i < maxrates; i++)
   2476  1.1  dyoung 		rs->rs_rates[i] = rt->info[i].dot11Rate;
   2477  1.1  dyoung 	rs->rs_nrates = maxrates;
   2478  1.1  dyoung 	return 1;
   2479  1.1  dyoung }
   2480  1.1  dyoung 
   2481  1.1  dyoung static void
   2482  1.1  dyoung ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode)
   2483  1.1  dyoung {
   2484  1.1  dyoung 	const HAL_RATE_TABLE *rt;
   2485  1.1  dyoung 	int i;
   2486  1.1  dyoung 
   2487  1.1  dyoung 	memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap));
   2488  1.1  dyoung 	rt = sc->sc_rates[mode];
   2489  1.1  dyoung 	KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode));
   2490  1.1  dyoung 	for (i = 0; i < rt->rateCount; i++)
   2491  1.1  dyoung 		sc->sc_rixmap[rt->info[i].dot11Rate & IEEE80211_RATE_VAL] = i;
   2492  1.1  dyoung 	memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap));
   2493  1.1  dyoung 	for (i = 0; i < 32; i++)
   2494  1.1  dyoung 		sc->sc_hwmap[i] = rt->info[rt->rateCodeToIndex[i]].dot11Rate;
   2495  1.1  dyoung 	sc->sc_currates = rt;
   2496  1.1  dyoung 	sc->sc_curmode = mode;
   2497  1.1  dyoung }
   2498  1.1  dyoung 
   2499  1.1  dyoung /*
   2500  1.1  dyoung  * Reset the rate control state for each 802.11 state transition.
   2501  1.1  dyoung  */
   2502  1.1  dyoung static void
   2503  1.1  dyoung ath_rate_ctl_reset(struct ath_softc *sc, enum ieee80211_state state)
   2504  1.1  dyoung {
   2505  1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   2506  1.1  dyoung 	struct ieee80211_node *ni;
   2507  1.1  dyoung 	struct ath_node *an;
   2508  1.1  dyoung 
   2509  1.1  dyoung 	an = (struct ath_node *) ic->ic_bss;
   2510  1.1  dyoung 	an->an_tx_ok = an->an_tx_err = an->an_tx_retr = an->an_tx_upper = 0;
   2511  1.1  dyoung 	if (ic->ic_opmode == IEEE80211_M_STA) {
   2512  1.1  dyoung 		ni = ic->ic_bss;
   2513  1.1  dyoung 		if (state == IEEE80211_S_RUN) {
   2514  1.1  dyoung 			/* start with highest negotiated rate */
   2515  1.1  dyoung 			KASSERT(ni->ni_rates.rs_nrates > 0,
   2516  1.1  dyoung 				("transition to RUN state w/ no rates!"));
   2517  1.1  dyoung 			ni->ni_txrate = ni->ni_rates.rs_nrates - 1;
   2518  1.1  dyoung 		} else {
   2519  1.1  dyoung 			/* use lowest rate */
   2520  1.1  dyoung 			ni->ni_txrate = 0;
   2521  1.1  dyoung 		}
   2522  1.1  dyoung 	} else {
   2523  1.1  dyoung 		TAILQ_FOREACH(ni, &ic->ic_node, ni_list) {
   2524  1.1  dyoung 			ni->ni_txrate = 0;		/* use lowest rate */
   2525  1.1  dyoung 			an = (struct ath_node *) ni;
   2526  1.1  dyoung 			an->an_tx_ok = an->an_tx_err = an->an_tx_retr =
   2527  1.1  dyoung 			    an->an_tx_upper = 0;
   2528  1.1  dyoung 		}
   2529  1.1  dyoung 	}
   2530  1.1  dyoung }
   2531  1.1  dyoung 
   2532  1.1  dyoung /*
   2533  1.1  dyoung  * Examine and potentially adjust the transmit rate.
   2534  1.1  dyoung  */
   2535  1.1  dyoung static void
   2536  1.1  dyoung ath_rate_ctl(void *arg, struct ieee80211_node *ni)
   2537  1.1  dyoung {
   2538  1.1  dyoung 	struct ath_softc *sc = arg;
   2539  1.1  dyoung 	struct ath_node *an = (struct ath_node *) ni;
   2540  1.1  dyoung 	struct ieee80211_rateset *rs = &ni->ni_rates;
   2541  1.1  dyoung 	int mod = 0, orate, enough;
   2542  1.1  dyoung 
   2543  1.1  dyoung 	/*
   2544  1.1  dyoung 	 * Rate control
   2545  1.1  dyoung 	 * XXX: very primitive version.
   2546  1.1  dyoung 	 */
   2547  1.1  dyoung 	sc->sc_stats.ast_rate_calls++;
   2548  1.1  dyoung 
   2549  1.1  dyoung 	enough = (an->an_tx_ok + an->an_tx_err >= 10);
   2550  1.1  dyoung 
   2551  1.1  dyoung 	/* no packet reached -> down */
   2552  1.1  dyoung 	if (an->an_tx_err > 0 && an->an_tx_ok == 0)
   2553  1.1  dyoung 		mod = -1;
   2554  1.1  dyoung 
   2555  1.1  dyoung 	/* all packets needs retry in average -> down */
   2556  1.1  dyoung 	if (enough && an->an_tx_ok < an->an_tx_retr)
   2557  1.1  dyoung 		mod = -1;
   2558  1.1  dyoung 
   2559  1.1  dyoung 	/* no error and less than 10% of packets needs retry -> up */
   2560  1.1  dyoung 	if (enough && an->an_tx_err == 0 && an->an_tx_ok > an->an_tx_retr * 10)
   2561  1.1  dyoung 		mod = 1;
   2562  1.1  dyoung 
   2563  1.1  dyoung 	orate = ni->ni_txrate;
   2564  1.1  dyoung 	switch (mod) {
   2565  1.1  dyoung 	case 0:
   2566  1.1  dyoung 		if (enough && an->an_tx_upper > 0)
   2567  1.1  dyoung 			an->an_tx_upper--;
   2568  1.1  dyoung 		break;
   2569  1.1  dyoung 	case -1:
   2570  1.1  dyoung 		if (ni->ni_txrate > 0) {
   2571  1.1  dyoung 			ni->ni_txrate--;
   2572  1.1  dyoung 			sc->sc_stats.ast_rate_drop++;
   2573  1.1  dyoung 		}
   2574  1.1  dyoung 		an->an_tx_upper = 0;
   2575  1.1  dyoung 		break;
   2576  1.1  dyoung 	case 1:
   2577  1.1  dyoung 		if (++an->an_tx_upper < 2)
   2578  1.1  dyoung 			break;
   2579  1.1  dyoung 		an->an_tx_upper = 0;
   2580  1.1  dyoung 		if (ni->ni_txrate + 1 < rs->rs_nrates) {
   2581  1.1  dyoung 			ni->ni_txrate++;
   2582  1.1  dyoung 			sc->sc_stats.ast_rate_raise++;
   2583  1.1  dyoung 		}
   2584  1.1  dyoung 		break;
   2585  1.1  dyoung 	}
   2586  1.1  dyoung 
   2587  1.1  dyoung 	if (ni->ni_txrate != orate) {
   2588  1.1  dyoung 		printf("%s: %dM -> %dM (%d ok, %d err, %d retr)\n",
   2589  1.1  dyoung 		    __func__,
   2590  1.1  dyoung 		    (rs->rs_rates[orate] & IEEE80211_RATE_VAL) / 2,
   2591  1.1  dyoung 		    (rs->rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL) / 2,
   2592  1.1  dyoung 		    an->an_tx_ok, an->an_tx_err, an->an_tx_retr);
   2593  1.1  dyoung 	}
   2594  1.1  dyoung 	if (ni->ni_txrate != orate || enough)
   2595  1.1  dyoung 		an->an_tx_ok = an->an_tx_err = an->an_tx_retr = 0;
   2596  1.1  dyoung }
   2597  1.1  dyoung 
   2598  1.1  dyoung #ifdef AR_DEBUG
   2599  1.1  dyoung static int
   2600  1.1  dyoung sysctl_hw_ath_dump(SYSCTL_HANDLER_ARGS)
   2601  1.1  dyoung {
   2602  1.1  dyoung 	char dmode[64];
   2603  1.1  dyoung 	int error;
   2604  1.1  dyoung 
   2605  1.1  dyoung 	strncpy(dmode, "", sizeof(dmode) - 1);
   2606  1.1  dyoung 	dmode[sizeof(dmode) - 1] = '\0';
   2607  1.1  dyoung 	error = sysctl_handle_string(oidp, &dmode[0], sizeof(dmode), req);
   2608  1.1  dyoung 
   2609  1.1  dyoung 	if (error == 0 && req->newptr != NULL) {
   2610  1.1  dyoung 		struct ifnet *ifp;
   2611  1.1  dyoung 		struct ath_softc *sc;
   2612  1.1  dyoung 
   2613  1.1  dyoung 		ifp = ifunit("ath0");		/* XXX */
   2614  1.1  dyoung 		if (!ifp)
   2615  1.1  dyoung 			return EINVAL;
   2616  1.1  dyoung 		sc = ifp->if_softc;
   2617  1.1  dyoung 		if (strcmp(dmode, "hal") == 0)
   2618  1.1  dyoung 			ath_hal_dumpstate(sc->sc_ah);
   2619  1.1  dyoung 		else if (strcmp(dmode, "eeprom") == 0)
   2620  1.1  dyoung 			ath_hal_dumpeeprom(sc->sc_ah);
   2621  1.1  dyoung 		else if (strcmp(dmode, "rfgain") == 0)
   2622  1.1  dyoung 			ath_hal_dumprfgain(sc->sc_ah);
   2623  1.1  dyoung 		else if (strcmp(dmode, "ani") == 0)
   2624  1.1  dyoung 			ath_hal_dumpani(sc->sc_ah);
   2625  1.1  dyoung 		else
   2626  1.1  dyoung 			return EINVAL;
   2627  1.1  dyoung 	}
   2628  1.1  dyoung 	return error;
   2629  1.1  dyoung }
   2630  1.1  dyoung SYSCTL_PROC(_hw_ath, OID_AUTO, dump, CTLTYPE_STRING | CTLFLAG_RW,
   2631  1.1  dyoung 	0, 0, sysctl_hw_ath_dump, "A", "Dump driver state");
   2632  1.1  dyoung 
   2633  1.1  dyoung static void
   2634  1.1  dyoung ath_printrxbuf(struct ath_buf *bf, int done)
   2635  1.1  dyoung {
   2636  1.1  dyoung 	struct ath_desc *ds;
   2637  1.1  dyoung 	int i;
   2638  1.1  dyoung 
   2639  1.1  dyoung 	for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) {
   2640  1.1  dyoung 		printf("R%d (%p %p) %08x %08x %08x %08x %08x %08x %c\n",
   2641  1.1  dyoung 		    i, ds, (struct ath_desc *)bf->bf_daddr + i,
   2642  1.1  dyoung 		    ds->ds_link, ds->ds_data,
   2643  1.1  dyoung 		    ds->ds_ctl0, ds->ds_ctl1,
   2644  1.1  dyoung 		    ds->ds_hw[0], ds->ds_hw[1],
   2645  1.1  dyoung 		    !done ? ' ' : (ds->ds_rxstat.rs_status == 0) ? '*' : '!');
   2646  1.1  dyoung 	}
   2647  1.1  dyoung }
   2648  1.1  dyoung 
   2649  1.1  dyoung static void
   2650  1.1  dyoung ath_printtxbuf(struct ath_buf *bf, int done)
   2651  1.1  dyoung {
   2652  1.1  dyoung 	struct ath_desc *ds;
   2653  1.1  dyoung 	int i;
   2654  1.1  dyoung 
   2655  1.1  dyoung 	for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) {
   2656  1.1  dyoung 		printf("T%d (%p %p) %08x %08x %08x %08x %08x %08x %08x %08x %c\n",
   2657  1.1  dyoung 		    i, ds, (struct ath_desc *)bf->bf_daddr + i,
   2658  1.1  dyoung 		    ds->ds_link, ds->ds_data,
   2659  1.1  dyoung 		    ds->ds_ctl0, ds->ds_ctl1,
   2660  1.1  dyoung 		    ds->ds_hw[0], ds->ds_hw[1], ds->ds_hw[2], ds->ds_hw[3],
   2661  1.1  dyoung 		    !done ? ' ' : (ds->ds_txstat.ts_status == 0) ? '*' : '!');
   2662  1.1  dyoung 	}
   2663  1.1  dyoung }
   2664  1.1  dyoung #endif /* AR_DEBUG */
   2665