Home | History | Annotate | Line # | Download | only in ic
ath.c revision 1.24.2.2
      1  1.24.2.2      he /*	$NetBSD: ath.c,v 1.24.2.2 2004/07/23 22:32:14 he Exp $	*/
      2       1.9  itojun 
      3       1.1  dyoung /*-
      4       1.1  dyoung  * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
      5       1.1  dyoung  * All rights reserved.
      6       1.1  dyoung  *
      7       1.1  dyoung  * Redistribution and use in source and binary forms, with or without
      8       1.1  dyoung  * modification, are permitted provided that the following conditions
      9       1.1  dyoung  * are met:
     10       1.1  dyoung  * 1. Redistributions of source code must retain the above copyright
     11       1.1  dyoung  *    notice, this list of conditions and the following disclaimer,
     12       1.1  dyoung  *    without modification.
     13       1.1  dyoung  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     14       1.1  dyoung  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
     15       1.1  dyoung  *    redistribution must be conditioned upon including a substantially
     16       1.1  dyoung  *    similar Disclaimer requirement for further binary redistribution.
     17       1.1  dyoung  * 3. Neither the names of the above-listed copyright holders nor the names
     18       1.1  dyoung  *    of any contributors may be used to endorse or promote products derived
     19       1.1  dyoung  *    from this software without specific prior written permission.
     20       1.1  dyoung  *
     21       1.1  dyoung  * Alternatively, this software may be distributed under the terms of the
     22       1.1  dyoung  * GNU General Public License ("GPL") version 2 as published by the Free
     23       1.1  dyoung  * Software Foundation.
     24       1.1  dyoung  *
     25       1.1  dyoung  * NO WARRANTY
     26       1.1  dyoung  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     27       1.1  dyoung  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     28       1.1  dyoung  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
     29       1.1  dyoung  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
     30       1.1  dyoung  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
     31       1.1  dyoung  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.1  dyoung  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.1  dyoung  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
     34       1.1  dyoung  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.1  dyoung  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     36       1.1  dyoung  * THE POSSIBILITY OF SUCH DAMAGES.
     37       1.1  dyoung  */
     38       1.1  dyoung 
     39       1.1  dyoung #include <sys/cdefs.h>
     40       1.2  dyoung #ifdef __FreeBSD__
     41      1.18  dyoung __FBSDID("$FreeBSD: src/sys/dev/ath/if_ath.c,v 1.36 2003/11/29 01:23:59 sam Exp $");
     42       1.2  dyoung #endif
     43       1.2  dyoung #ifdef __NetBSD__
     44  1.24.2.2      he __KERNEL_RCSID(0, "$NetBSD: ath.c,v 1.24.2.2 2004/07/23 22:32:14 he Exp $");
     45       1.2  dyoung #endif
     46       1.1  dyoung 
     47       1.1  dyoung /*
     48       1.1  dyoung  * Driver for the Atheros Wireless LAN controller.
     49       1.1  dyoung  *
     50       1.1  dyoung  * This software is derived from work of Atsushi Onoe; his contribution
     51       1.1  dyoung  * is greatly appreciated.
     52       1.1  dyoung  */
     53       1.1  dyoung 
     54       1.1  dyoung #include "opt_inet.h"
     55       1.1  dyoung 
     56       1.2  dyoung #ifdef __NetBSD__
     57       1.2  dyoung #include "bpfilter.h"
     58       1.2  dyoung #endif /* __NetBSD__ */
     59       1.2  dyoung 
     60       1.1  dyoung #include <sys/param.h>
     61       1.1  dyoung #include <sys/systm.h>
     62       1.2  dyoung #include <sys/types.h>
     63       1.1  dyoung #include <sys/sysctl.h>
     64       1.1  dyoung #include <sys/mbuf.h>
     65       1.1  dyoung #include <sys/malloc.h>
     66       1.1  dyoung #include <sys/lock.h>
     67       1.2  dyoung #ifdef __FreeBSD__
     68       1.1  dyoung #include <sys/mutex.h>
     69       1.2  dyoung #endif
     70       1.1  dyoung #include <sys/kernel.h>
     71       1.1  dyoung #include <sys/socket.h>
     72       1.1  dyoung #include <sys/sockio.h>
     73       1.1  dyoung #include <sys/errno.h>
     74       1.1  dyoung #include <sys/callout.h>
     75       1.2  dyoung #ifdef __FreeBSD__
     76       1.1  dyoung #include <sys/bus.h>
     77       1.2  dyoung #else
     78       1.2  dyoung #include <machine/bus.h>
     79       1.2  dyoung #endif
     80       1.1  dyoung #include <sys/endian.h>
     81       1.1  dyoung 
     82       1.1  dyoung #include <machine/bus.h>
     83       1.1  dyoung 
     84       1.1  dyoung #include <net/if.h>
     85       1.1  dyoung #include <net/if_dl.h>
     86       1.1  dyoung #include <net/if_media.h>
     87       1.1  dyoung #include <net/if_arp.h>
     88       1.2  dyoung #ifdef __FreeBSD__
     89       1.1  dyoung #include <net/ethernet.h>
     90       1.2  dyoung #else
     91       1.2  dyoung #include <net/if_ether.h>
     92       1.2  dyoung #endif
     93       1.1  dyoung #include <net/if_llc.h>
     94       1.1  dyoung 
     95       1.1  dyoung #include <net80211/ieee80211_var.h>
     96       1.2  dyoung #include <net80211/ieee80211_compat.h>
     97       1.1  dyoung 
     98       1.2  dyoung #if NBPFILTER > 0
     99       1.1  dyoung #include <net/bpf.h>
    100       1.2  dyoung #endif
    101       1.1  dyoung 
    102       1.1  dyoung #ifdef INET
    103       1.1  dyoung #include <netinet/in.h>
    104       1.1  dyoung #endif
    105       1.1  dyoung 
    106       1.2  dyoung #include <dev/ic/athcompat.h>
    107       1.2  dyoung 
    108       1.1  dyoung #define	AR_DEBUG
    109       1.2  dyoung #ifdef __FreeBSD__
    110       1.1  dyoung #include <dev/ath/if_athvar.h>
    111       1.1  dyoung #include <contrib/dev/ath/ah_desc.h>
    112       1.2  dyoung #else
    113       1.2  dyoung #include <dev/ic/athvar.h>
    114       1.2  dyoung #include <../contrib/sys/dev/ic/athhal_desc.h>
    115       1.2  dyoung #endif
    116       1.1  dyoung 
    117      1.15     wiz /* unaligned little endian access */
    118       1.1  dyoung #define LE_READ_2(p)							\
    119       1.1  dyoung 	((u_int16_t)							\
    120       1.1  dyoung 	 ((((u_int8_t *)(p))[0]      ) | (((u_int8_t *)(p))[1] <<  8)))
    121       1.1  dyoung #define LE_READ_4(p)							\
    122       1.1  dyoung 	((u_int32_t)							\
    123       1.1  dyoung 	 ((((u_int8_t *)(p))[0]      ) | (((u_int8_t *)(p))[1] <<  8) |	\
    124       1.1  dyoung 	  (((u_int8_t *)(p))[2] << 16) | (((u_int8_t *)(p))[3] << 24)))
    125       1.1  dyoung 
    126       1.2  dyoung #ifdef __FreeBSD__
    127       1.1  dyoung static void	ath_init(void *);
    128       1.2  dyoung #else
    129       1.2  dyoung static int	ath_init(struct ifnet *);
    130       1.2  dyoung #endif
    131       1.2  dyoung static int	ath_init1(struct ath_softc *);
    132       1.2  dyoung static int	ath_intr1(struct ath_softc *);
    133       1.1  dyoung static void	ath_stop(struct ifnet *);
    134       1.1  dyoung static void	ath_start(struct ifnet *);
    135       1.1  dyoung static void	ath_reset(struct ath_softc *);
    136       1.1  dyoung static int	ath_media_change(struct ifnet *);
    137       1.1  dyoung static void	ath_watchdog(struct ifnet *);
    138       1.1  dyoung static int	ath_ioctl(struct ifnet *, u_long, caddr_t);
    139       1.1  dyoung static void	ath_fatal_proc(void *, int);
    140       1.1  dyoung static void	ath_rxorn_proc(void *, int);
    141       1.1  dyoung static void	ath_bmiss_proc(void *, int);
    142       1.1  dyoung static void	ath_initkeytable(struct ath_softc *);
    143       1.1  dyoung static void	ath_mode_init(struct ath_softc *);
    144       1.1  dyoung static int	ath_beacon_alloc(struct ath_softc *, struct ieee80211_node *);
    145       1.1  dyoung static void	ath_beacon_proc(void *, int);
    146       1.1  dyoung static void	ath_beacon_free(struct ath_softc *);
    147       1.1  dyoung static void	ath_beacon_config(struct ath_softc *);
    148       1.1  dyoung static int	ath_desc_alloc(struct ath_softc *);
    149       1.1  dyoung static void	ath_desc_free(struct ath_softc *);
    150       1.1  dyoung static struct ieee80211_node *ath_node_alloc(struct ieee80211com *);
    151       1.1  dyoung static void	ath_node_free(struct ieee80211com *, struct ieee80211_node *);
    152       1.1  dyoung static void	ath_node_copy(struct ieee80211com *,
    153       1.1  dyoung 			struct ieee80211_node *, const struct ieee80211_node *);
    154      1.18  dyoung static u_int8_t	ath_node_getrssi(struct ieee80211com *,
    155      1.18  dyoung 			struct ieee80211_node *);
    156       1.1  dyoung static int	ath_rxbuf_init(struct ath_softc *, struct ath_buf *);
    157       1.1  dyoung static void	ath_rx_proc(void *, int);
    158       1.1  dyoung static int	ath_tx_start(struct ath_softc *, struct ieee80211_node *,
    159       1.1  dyoung 			     struct ath_buf *, struct mbuf *);
    160       1.1  dyoung static void	ath_tx_proc(void *, int);
    161       1.1  dyoung static int	ath_chan_set(struct ath_softc *, struct ieee80211_channel *);
    162       1.1  dyoung static void	ath_draintxq(struct ath_softc *);
    163       1.1  dyoung static void	ath_stoprecv(struct ath_softc *);
    164       1.1  dyoung static int	ath_startrecv(struct ath_softc *);
    165       1.1  dyoung static void	ath_next_scan(void *);
    166       1.1  dyoung static void	ath_calibrate(void *);
    167       1.1  dyoung static int	ath_newstate(struct ieee80211com *, enum ieee80211_state, int);
    168       1.1  dyoung static void	ath_newassoc(struct ieee80211com *,
    169       1.1  dyoung 			struct ieee80211_node *, int);
    170      1.21  dyoung static int	ath_getchannels(struct ath_softc *, u_int cc, HAL_BOOL outdoor,
    171      1.21  dyoung 			HAL_BOOL xchanmode);
    172       1.1  dyoung 
    173       1.1  dyoung static int	ath_rate_setup(struct ath_softc *sc, u_int mode);
    174       1.1  dyoung static void	ath_setcurmode(struct ath_softc *, enum ieee80211_phymode);
    175       1.1  dyoung static void	ath_rate_ctl_reset(struct ath_softc *, enum ieee80211_state);
    176       1.1  dyoung static void	ath_rate_ctl(void *, struct ieee80211_node *);
    177       1.1  dyoung 
    178       1.3  ichiro #ifdef __NetBSD__
    179       1.3  ichiro int	ath_enable(struct ath_softc *);
    180       1.3  ichiro void	ath_disable(struct ath_softc *);
    181       1.3  ichiro void	ath_power(int, void *);
    182       1.3  ichiro #endif
    183       1.3  ichiro 
    184       1.2  dyoung #ifdef __FreeBSD__
    185       1.1  dyoung SYSCTL_DECL(_hw_ath);
    186       1.1  dyoung /* XXX validate sysctl values */
    187       1.1  dyoung SYSCTL_INT(_hw_ath, OID_AUTO, dwell, CTLFLAG_RW, &ath_dwelltime,
    188       1.1  dyoung 	    0, "channel dwell time (ms) for AP/station scanning");
    189       1.1  dyoung SYSCTL_INT(_hw_ath, OID_AUTO, calibrate, CTLFLAG_RW, &ath_calinterval,
    190       1.1  dyoung 	    0, "chip calibration interval (secs)");
    191       1.1  dyoung SYSCTL_INT(_hw_ath, OID_AUTO, outdoor, CTLFLAG_RD, &ath_outdoor,
    192       1.1  dyoung 	    0, "enable/disable outdoor operation");
    193       1.1  dyoung SYSCTL_INT(_hw_ath, OID_AUTO, countrycode, CTLFLAG_RD, &ath_countrycode,
    194       1.1  dyoung 	    0, "country code");
    195       1.1  dyoung SYSCTL_INT(_hw_ath, OID_AUTO, regdomain, CTLFLAG_RD, &ath_regdomain,
    196       1.1  dyoung 	    0, "regulatory domain");
    197       1.2  dyoung #endif /* __FreeBSD__ */
    198       1.2  dyoung 
    199      1.20  dyoung #ifdef __NetBSD__
    200      1.20  dyoung static int ath_dwelltime_nodenum, ath_calibrate_nodenum, ath_outdoor_nodenum,
    201      1.20  dyoung            ath_countrycode_nodenum, ath_regdomain_nodenum, ath_debug_nodenum;
    202      1.20  dyoung #endif /* __NetBSD__ */
    203      1.20  dyoung 
    204       1.2  dyoung static	int ath_dwelltime = 200;		/* 5 channels/second */
    205       1.2  dyoung static	int ath_calinterval = 30;		/* calibrate every 30 secs */
    206       1.2  dyoung static	int ath_outdoor = AH_TRUE;		/* outdoor operation */
    207      1.21  dyoung static	int ath_xchanmode = AH_TRUE;		/* enable extended channels */
    208       1.2  dyoung static	int ath_countrycode = CTRY_DEFAULT;	/* country code */
    209       1.2  dyoung static	int ath_regdomain = 0;			/* regulatory domain */
    210       1.1  dyoung 
    211       1.1  dyoung #ifdef AR_DEBUG
    212       1.1  dyoung int	ath_debug = 0;
    213       1.2  dyoung #ifdef __FreeBSD__
    214       1.1  dyoung SYSCTL_INT(_hw_ath, OID_AUTO, debug, CTLFLAG_RW, &ath_debug,
    215       1.1  dyoung 	    0, "control debugging printfs");
    216       1.2  dyoung #endif /* __FreeBSD__ */
    217       1.1  dyoung #define	IFF_DUMPPKTS(_ifp) \
    218       1.1  dyoung 	(ath_debug || \
    219       1.1  dyoung 	    ((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
    220       1.1  dyoung static	void ath_printrxbuf(struct ath_buf *bf, int);
    221       1.1  dyoung static	void ath_printtxbuf(struct ath_buf *bf, int);
    222       1.1  dyoung #define	DPRINTF(X)	if (ath_debug) printf X
    223       1.1  dyoung #define	DPRINTF2(X)	if (ath_debug > 1) printf X
    224       1.1  dyoung #else
    225       1.1  dyoung #define	IFF_DUMPPKTS(_ifp) \
    226       1.1  dyoung 	(((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
    227       1.1  dyoung #define	DPRINTF(X)
    228       1.1  dyoung #define	DPRINTF2(X)
    229       1.1  dyoung #endif
    230       1.1  dyoung 
    231       1.3  ichiro #ifdef __NetBSD__
    232       1.3  ichiro int
    233       1.3  ichiro ath_activate(struct device *self, enum devact act)
    234       1.3  ichiro {
    235       1.3  ichiro 	struct ath_softc *sc = (struct ath_softc *)self;
    236       1.3  ichiro 	int rv = 0, s;
    237       1.3  ichiro 
    238       1.3  ichiro 	s = splnet();
    239       1.3  ichiro 	switch (act) {
    240       1.3  ichiro 	case DVACT_ACTIVATE:
    241       1.3  ichiro 		rv = EOPNOTSUPP;
    242       1.3  ichiro 		break;
    243       1.3  ichiro 	case DVACT_DEACTIVATE:
    244       1.3  ichiro 		if_deactivate(&sc->sc_ic.ic_if);
    245       1.3  ichiro 		break;
    246       1.3  ichiro 	}
    247       1.3  ichiro 	splx(s);
    248       1.3  ichiro 	return rv;
    249       1.3  ichiro }
    250       1.3  ichiro 
    251       1.3  ichiro int
    252       1.3  ichiro ath_enable(struct ath_softc *sc)
    253       1.3  ichiro {
    254       1.3  ichiro 	if (ATH_IS_ENABLED(sc) == 0) {
    255       1.3  ichiro 		if (sc->sc_enable != NULL && (*sc->sc_enable)(sc) != 0) {
    256       1.3  ichiro 			printf("%s: device enable failed\n",
    257       1.3  ichiro 				sc->sc_dev.dv_xname);
    258       1.3  ichiro 			return (EIO);
    259       1.3  ichiro 		}
    260       1.3  ichiro 		sc->sc_flags |= ATH_ENABLED;
    261       1.3  ichiro 	}
    262       1.3  ichiro 	return (0);
    263       1.3  ichiro }
    264       1.3  ichiro 
    265       1.3  ichiro void
    266       1.3  ichiro ath_disable(struct ath_softc *sc)
    267       1.3  ichiro {
    268       1.3  ichiro 	if (!ATH_IS_ENABLED(sc))
    269       1.3  ichiro 		return;
    270       1.3  ichiro 	if (sc->sc_disable != NULL)
    271       1.3  ichiro 		(*sc->sc_disable)(sc);
    272       1.3  ichiro 	sc->sc_flags &= ~ATH_ENABLED;
    273       1.3  ichiro }
    274      1.20  dyoung 
    275      1.20  dyoung static int
    276      1.20  dyoung sysctl_ath_verify(SYSCTLFN_ARGS)
    277      1.20  dyoung {
    278      1.20  dyoung 	int error, t;
    279      1.20  dyoung 	struct sysctlnode node;
    280      1.20  dyoung 
    281      1.20  dyoung 	node = *rnode;
    282      1.20  dyoung 	t = *(int*)rnode->sysctl_data;
    283      1.20  dyoung 	node.sysctl_data = &t;
    284      1.20  dyoung 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    285      1.20  dyoung 	if (error || newp == NULL)
    286      1.20  dyoung 		return (error);
    287      1.20  dyoung 
    288      1.20  dyoung 	DPRINTF2(("%s: t = %d, nodenum = %d, rnodenum = %d\n", __func__, t,
    289      1.20  dyoung 	    node.sysctl_num, rnode->sysctl_num));
    290      1.20  dyoung 
    291      1.20  dyoung 	if (node.sysctl_num == ath_dwelltime_nodenum) {
    292      1.20  dyoung 		if (t <= 0)
    293      1.20  dyoung 			return (EINVAL);
    294      1.20  dyoung 	} else if (node.sysctl_num == ath_calibrate_nodenum) {
    295      1.20  dyoung 		if (t <= 0)
    296      1.20  dyoung 			return (EINVAL);
    297      1.20  dyoung #ifdef AR_DEBUG
    298      1.20  dyoung 	} else if (node.sysctl_num == ath_debug_nodenum) {
    299      1.20  dyoung 		if (t < 0 || t > 2)
    300      1.20  dyoung 			return (EINVAL);
    301      1.20  dyoung #endif /* AR_DEBUG */
    302      1.20  dyoung 	} else
    303      1.20  dyoung 		return (EINVAL);
    304      1.20  dyoung 
    305      1.20  dyoung 	*(int*)rnode->sysctl_data = t;
    306      1.20  dyoung 
    307      1.20  dyoung 	return (0);
    308      1.20  dyoung }
    309      1.20  dyoung 
    310      1.20  dyoung /*
    311      1.20  dyoung  * Setup sysctl(3) MIB, ath.*.
    312      1.20  dyoung  *
    313      1.23  atatat  * TBD condition CTLFLAG_PERMANENT on being an LKM or not
    314      1.20  dyoung  */
    315      1.20  dyoung SYSCTL_SETUP(sysctl_ath, "sysctl ath subtree setup")
    316      1.20  dyoung {
    317      1.24  atatat 	int rc, ath_node_num;
    318      1.23  atatat 	struct sysctlnode *node;
    319      1.20  dyoung 
    320      1.23  atatat 	if ((rc = sysctl_createv(clog, 0, NULL, NULL,
    321      1.23  atatat 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
    322      1.23  atatat 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0)
    323      1.22  dyoung 		goto err;
    324      1.22  dyoung 
    325      1.23  atatat 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    326  1.24.2.1    tron 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "ath",
    327  1.24.2.1    tron 	    SYSCTL_DESCR("ath information and options"),
    328      1.23  atatat 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
    329      1.20  dyoung 		goto err;
    330      1.20  dyoung 
    331      1.24  atatat 	ath_node_num = node->sysctl_num;
    332      1.20  dyoung 
    333      1.20  dyoung 	/* channel dwell time (ms) for AP/station scanning */
    334      1.23  atatat 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    335      1.23  atatat 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    336  1.24.2.1    tron 	    CTLTYPE_INT, "dwell",
    337  1.24.2.1    tron 	    SYSCTL_DESCR("Channel dwell time (ms) for AP/station scanning"),
    338  1.24.2.1    tron 	    sysctl_ath_verify, 0, &ath_dwelltime,
    339      1.24  atatat 	    0, CTL_HW, ath_node_num, CTL_CREATE,
    340      1.22  dyoung 	    CTL_EOL)) != 0)
    341      1.20  dyoung 		goto err;
    342      1.20  dyoung 
    343      1.20  dyoung 	ath_dwelltime_nodenum = node->sysctl_num;
    344      1.20  dyoung 
    345      1.20  dyoung 	/* chip calibration interval (secs) */
    346      1.23  atatat 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    347      1.23  atatat 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    348  1.24.2.1    tron 	    CTLTYPE_INT, "calibrate",
    349  1.24.2.1    tron 	    SYSCTL_DESCR("Chip calibration interval (secs)"), sysctl_ath_verify,
    350      1.22  dyoung 	    0, &ath_calinterval, 0, CTL_HW,
    351      1.24  atatat 	    ath_node_num, CTL_CREATE, CTL_EOL)) != 0)
    352      1.20  dyoung 		goto err;
    353      1.20  dyoung 
    354      1.20  dyoung 	ath_calibrate_nodenum = node->sysctl_num;
    355      1.20  dyoung 
    356      1.20  dyoung 	/* enable/disable outdoor operation */
    357      1.23  atatat 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    358      1.23  atatat 	    CTLFLAG_PERMANENT|CTLFLAG_READONLY, CTLTYPE_INT,
    359  1.24.2.1    tron 	    "outdoor", SYSCTL_DESCR("Enable/disable outdoor operation"),
    360  1.24.2.1    tron 	    NULL, 0, &ath_outdoor, 0,
    361      1.24  atatat 	    CTL_HW, ath_node_num, CTL_CREATE,
    362      1.22  dyoung 	    CTL_EOL)) != 0)
    363      1.20  dyoung 		goto err;
    364      1.20  dyoung 
    365      1.20  dyoung 	ath_outdoor_nodenum = node->sysctl_num;
    366      1.20  dyoung 
    367      1.20  dyoung 	/* country code */
    368      1.23  atatat 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    369      1.23  atatat 	    CTLFLAG_PERMANENT|CTLFLAG_READONLY, CTLTYPE_INT,
    370  1.24.2.1    tron 	    "countrycode", SYSCTL_DESCR("Country code"),
    371  1.24.2.1    tron 	    NULL, 0, &ath_countrycode, 0,
    372      1.24  atatat 	    CTL_HW, ath_node_num, CTL_CREATE,
    373      1.22  dyoung 	    CTL_EOL)) != 0)
    374      1.20  dyoung 		goto err;
    375      1.20  dyoung 
    376      1.20  dyoung 	ath_countrycode_nodenum = node->sysctl_num;
    377      1.20  dyoung 
    378      1.20  dyoung 	/* regulatory domain */
    379      1.23  atatat 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    380      1.23  atatat 	    CTLFLAG_PERMANENT|CTLFLAG_READONLY, CTLTYPE_INT,
    381  1.24.2.1    tron 	    "regdomain", SYSCTL_DESCR("Regulatory domain"),
    382  1.24.2.1    tron 	    NULL, 0, &ath_regdomain, 0,
    383      1.24  atatat 	    CTL_HW, ath_node_num, CTL_CREATE,
    384      1.22  dyoung 	    CTL_EOL)) != 0)
    385      1.20  dyoung 		goto err;
    386      1.20  dyoung 
    387      1.20  dyoung 	ath_regdomain_nodenum = node->sysctl_num;
    388      1.20  dyoung 
    389      1.20  dyoung #ifdef AR_DEBUG
    390      1.20  dyoung 
    391      1.20  dyoung 	/* control debugging printfs */
    392      1.23  atatat 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    393      1.23  atatat 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    394  1.24.2.1    tron 	    "debug", SYSCTL_DESCR("Enable/disable ath debugging output"),
    395  1.24.2.1    tron 	    sysctl_ath_verify, 0, &ath_debug, 0,
    396      1.24  atatat 	    CTL_HW, ath_node_num, CTL_CREATE,
    397      1.22  dyoung 	    CTL_EOL)) != 0)
    398      1.20  dyoung 		goto err;
    399      1.20  dyoung 
    400      1.20  dyoung 	ath_debug_nodenum = node->sysctl_num;
    401      1.20  dyoung 
    402      1.20  dyoung #endif /* AR_DEBUG */
    403      1.20  dyoung 	return;
    404      1.20  dyoung err:
    405      1.20  dyoung 	printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
    406      1.20  dyoung }
    407      1.20  dyoung #endif /* __NetBSD__ */
    408       1.3  ichiro 
    409       1.1  dyoung int
    410       1.1  dyoung ath_attach(u_int16_t devid, struct ath_softc *sc)
    411       1.1  dyoung {
    412       1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
    413       1.1  dyoung 	struct ifnet *ifp = &ic->ic_if;
    414       1.1  dyoung 	struct ath_hal *ah;
    415       1.1  dyoung 	HAL_STATUS status;
    416       1.1  dyoung 	int error = 0;
    417       1.1  dyoung 
    418       1.1  dyoung 	DPRINTF(("ath_attach: devid 0x%x\n", devid));
    419       1.1  dyoung 
    420       1.2  dyoung #ifdef __FreeBSD__
    421       1.1  dyoung 	/* set these up early for if_printf use */
    422      1.18  dyoung 	if_initname(ifp, device_get_name(sc->sc_dev),
    423      1.18  dyoung 	    device_get_unit(sc->sc_dev));
    424       1.2  dyoung #else
    425       1.2  dyoung 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
    426       1.2  dyoung #endif
    427       1.1  dyoung 
    428       1.1  dyoung 	ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh, &status);
    429       1.1  dyoung 	if (ah == NULL) {
    430       1.1  dyoung 		if_printf(ifp, "unable to attach hardware; HAL status %u\n",
    431       1.1  dyoung 			status);
    432       1.1  dyoung 		error = ENXIO;
    433       1.1  dyoung 		goto bad;
    434       1.1  dyoung 	}
    435      1.18  dyoung 	if (ah->ah_abi != HAL_ABI_VERSION) {
    436      1.18  dyoung 		if_printf(ifp, "HAL ABI mismatch detected (0x%x != 0x%x)\n",
    437      1.18  dyoung 			ah->ah_abi, HAL_ABI_VERSION);
    438      1.18  dyoung 		error = ENXIO;
    439      1.18  dyoung 		goto bad;
    440      1.18  dyoung 	}
    441      1.18  dyoung 	if_printf(ifp, "mac %d.%d phy %d.%d",
    442      1.18  dyoung 		ah->ah_macVersion, ah->ah_macRev,
    443      1.18  dyoung 		ah->ah_phyRev >> 4, ah->ah_phyRev & 0xf);
    444      1.18  dyoung 	if (ah->ah_analog5GhzRev)
    445      1.18  dyoung 		printf(" 5ghz radio %d.%d",
    446      1.18  dyoung 			ah->ah_analog5GhzRev >> 4, ah->ah_analog5GhzRev & 0xf);
    447      1.18  dyoung 	if (ah->ah_analog2GhzRev)
    448      1.18  dyoung 		printf(" 2ghz radio %d.%d",
    449      1.18  dyoung 			ah->ah_analog2GhzRev >> 4, ah->ah_analog2GhzRev & 0xf);
    450      1.18  dyoung 	printf("\n");
    451       1.1  dyoung 	sc->sc_ah = ah;
    452       1.1  dyoung 	sc->sc_invalid = 0;	/* ready to go, enable interrupt handling */
    453       1.1  dyoung 
    454       1.1  dyoung 	/*
    455       1.1  dyoung 	 * Collect the channel list using the default country
    456       1.1  dyoung 	 * code and including outdoor channels.  The 802.11 layer
    457       1.1  dyoung 	 * is resposible for filtering this list based on settings
    458       1.1  dyoung 	 * like the phy mode.
    459       1.1  dyoung 	 */
    460      1.21  dyoung 	error = ath_getchannels(sc, ath_countrycode, ath_outdoor,
    461      1.21  dyoung 	    ath_xchanmode);
    462       1.1  dyoung 	if (error != 0)
    463       1.1  dyoung 		goto bad;
    464       1.1  dyoung 	/*
    465       1.1  dyoung 	 * Copy these back; they are set as a side effect
    466       1.1  dyoung 	 * of constructing the channel list.
    467       1.1  dyoung 	 */
    468       1.1  dyoung 	ath_regdomain = ath_hal_getregdomain(ah);
    469       1.1  dyoung 	ath_countrycode = ath_hal_getcountrycode(ah);
    470       1.1  dyoung 
    471       1.1  dyoung 	/*
    472       1.1  dyoung 	 * Setup rate tables for all potential media types.
    473       1.1  dyoung 	 */
    474       1.1  dyoung 	ath_rate_setup(sc, IEEE80211_MODE_11A);
    475       1.1  dyoung 	ath_rate_setup(sc, IEEE80211_MODE_11B);
    476       1.1  dyoung 	ath_rate_setup(sc, IEEE80211_MODE_11G);
    477       1.1  dyoung 	ath_rate_setup(sc, IEEE80211_MODE_TURBO);
    478       1.1  dyoung 
    479       1.1  dyoung 	error = ath_desc_alloc(sc);
    480       1.1  dyoung 	if (error != 0) {
    481       1.1  dyoung 		if_printf(ifp, "failed to allocate descriptors: %d\n", error);
    482       1.1  dyoung 		goto bad;
    483       1.1  dyoung 	}
    484       1.2  dyoung 	ATH_CALLOUT_INIT(&sc->sc_scan_ch);
    485       1.2  dyoung 	ATH_CALLOUT_INIT(&sc->sc_cal_ch);
    486       1.1  dyoung 
    487       1.2  dyoung #ifdef __FreeBSD__
    488      1.18  dyoung 	ATH_TXBUF_LOCK_INIT(sc);
    489      1.18  dyoung 	ATH_TXQ_LOCK_INIT(sc);
    490       1.2  dyoung #endif
    491       1.1  dyoung 
    492       1.2  dyoung 	ATH_TASK_INIT(&sc->sc_txtask, ath_tx_proc, sc);
    493       1.2  dyoung 	ATH_TASK_INIT(&sc->sc_rxtask, ath_rx_proc, sc);
    494       1.2  dyoung 	ATH_TASK_INIT(&sc->sc_swbatask, ath_beacon_proc, sc);
    495       1.2  dyoung 	ATH_TASK_INIT(&sc->sc_rxorntask, ath_rxorn_proc, sc);
    496       1.2  dyoung 	ATH_TASK_INIT(&sc->sc_fataltask, ath_fatal_proc, sc);
    497       1.2  dyoung 	ATH_TASK_INIT(&sc->sc_bmisstask, ath_bmiss_proc, sc);
    498       1.1  dyoung 
    499       1.1  dyoung 	/*
    500       1.1  dyoung 	 * For now just pre-allocate one data queue and one
    501       1.1  dyoung 	 * beacon queue.  Note that the HAL handles resetting
    502       1.1  dyoung 	 * them at the needed time.  Eventually we'll want to
    503       1.1  dyoung 	 * allocate more tx queues for splitting management
    504       1.1  dyoung 	 * frames and for QOS support.
    505       1.1  dyoung 	 */
    506       1.1  dyoung 	sc->sc_txhalq = ath_hal_setuptxqueue(ah,
    507       1.1  dyoung 		HAL_TX_QUEUE_DATA,
    508       1.1  dyoung 		AH_TRUE			/* enable interrupts */
    509       1.1  dyoung 	);
    510       1.1  dyoung 	if (sc->sc_txhalq == (u_int) -1) {
    511       1.1  dyoung 		if_printf(ifp, "unable to setup a data xmit queue!\n");
    512       1.1  dyoung 		goto bad;
    513       1.1  dyoung 	}
    514       1.1  dyoung 	sc->sc_bhalq = ath_hal_setuptxqueue(ah,
    515       1.1  dyoung 		HAL_TX_QUEUE_BEACON,
    516       1.1  dyoung 		AH_TRUE			/* enable interrupts */
    517       1.1  dyoung 	);
    518       1.1  dyoung 	if (sc->sc_bhalq == (u_int) -1) {
    519       1.1  dyoung 		if_printf(ifp, "unable to setup a beacon xmit queue!\n");
    520       1.1  dyoung 		goto bad;
    521       1.1  dyoung 	}
    522       1.1  dyoung 
    523       1.1  dyoung 	ifp->if_softc = sc;
    524       1.1  dyoung 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
    525       1.1  dyoung 	ifp->if_start = ath_start;
    526       1.1  dyoung 	ifp->if_watchdog = ath_watchdog;
    527       1.1  dyoung 	ifp->if_ioctl = ath_ioctl;
    528       1.1  dyoung 	ifp->if_init = ath_init;
    529       1.2  dyoung #ifdef __FreeBSD__
    530       1.1  dyoung 	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
    531       1.2  dyoung #else
    532       1.2  dyoung #if 0
    533       1.2  dyoung 	ifp->if_stop = ath_stop;		/* XXX */
    534       1.2  dyoung #endif
    535       1.2  dyoung 	IFQ_SET_READY(&ifp->if_snd);
    536       1.2  dyoung #endif
    537       1.1  dyoung 
    538       1.1  dyoung 	ic->ic_softc = sc;
    539       1.1  dyoung 	ic->ic_newassoc = ath_newassoc;
    540       1.1  dyoung 	/* XXX not right but it's not used anywhere important */
    541       1.1  dyoung 	ic->ic_phytype = IEEE80211_T_OFDM;
    542       1.1  dyoung 	ic->ic_opmode = IEEE80211_M_STA;
    543      1.18  dyoung 	ic->ic_caps = IEEE80211_C_WEP		/* wep supported */
    544      1.18  dyoung 		| IEEE80211_C_IBSS		/* ibss, nee adhoc, mode */
    545      1.18  dyoung 		| IEEE80211_C_HOSTAP		/* hostap mode */
    546      1.18  dyoung 		| IEEE80211_C_MONITOR		/* monitor mode */
    547      1.18  dyoung 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
    548      1.18  dyoung 		| IEEE80211_C_RCVMGT;		/* recv management frames */
    549       1.1  dyoung 
    550       1.1  dyoung 	/* get mac address from hardware */
    551       1.1  dyoung 	ath_hal_getmac(ah, ic->ic_myaddr);
    552       1.1  dyoung 
    553       1.2  dyoung #ifdef __NetBSD__
    554       1.2  dyoung 	if_attach(ifp);
    555       1.2  dyoung #endif
    556       1.1  dyoung 	/* call MI attach routine. */
    557       1.1  dyoung 	ieee80211_ifattach(ifp);
    558       1.1  dyoung 	/* override default methods */
    559       1.1  dyoung 	ic->ic_node_alloc = ath_node_alloc;
    560       1.1  dyoung 	ic->ic_node_free = ath_node_free;
    561       1.1  dyoung 	ic->ic_node_copy = ath_node_copy;
    562      1.18  dyoung 	ic->ic_node_getrssi = ath_node_getrssi;
    563       1.1  dyoung 	sc->sc_newstate = ic->ic_newstate;
    564       1.1  dyoung 	ic->ic_newstate = ath_newstate;
    565       1.1  dyoung 	/* complete initialization */
    566       1.1  dyoung 	ieee80211_media_init(ifp, ath_media_change, ieee80211_media_status);
    567       1.1  dyoung 
    568       1.2  dyoung #if NBPFILTER > 0
    569       1.1  dyoung 	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
    570       1.1  dyoung 		sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th),
    571       1.1  dyoung 		&sc->sc_drvbpf);
    572       1.2  dyoung #endif
    573       1.1  dyoung 	/*
    574       1.1  dyoung 	 * Initialize constant fields.
    575       1.1  dyoung 	 *
    576       1.1  dyoung 	 * NB: the channel is setup each time we transition to the
    577       1.1  dyoung 	 *     RUN state to avoid filling it in for each frame.
    578       1.1  dyoung 	 */
    579       1.1  dyoung 	sc->sc_tx_th.wt_ihdr.it_len = sizeof(sc->sc_tx_th);
    580       1.1  dyoung 	sc->sc_tx_th.wt_ihdr.it_present = ATH_TX_RADIOTAP_PRESENT;
    581       1.1  dyoung 
    582       1.1  dyoung 	sc->sc_rx_th.wr_ihdr.it_len = sizeof(sc->sc_rx_th);
    583       1.1  dyoung 	sc->sc_rx_th.wr_ihdr.it_present = ATH_RX_RADIOTAP_PRESENT;
    584       1.1  dyoung 
    585       1.1  dyoung 	if_printf(ifp, "802.11 address: %s\n", ether_sprintf(ic->ic_myaddr));
    586       1.1  dyoung 
    587       1.3  ichiro #ifdef __NetBSD__
    588       1.3  ichiro 	sc->sc_flags |= ATH_ATTACHED;
    589       1.3  ichiro 	/*
    590       1.3  ichiro 	 * Make sure the interface is shutdown during reboot.
    591       1.3  ichiro 	 */
    592       1.3  ichiro 	sc->sc_sdhook = shutdownhook_establish(ath_shutdown, sc);
    593       1.3  ichiro 	if (sc->sc_sdhook == NULL)
    594       1.3  ichiro 		printf("%s: WARNING: unable to establish shutdown hook\n",
    595       1.3  ichiro 			sc->sc_dev.dv_xname);
    596       1.3  ichiro 	sc->sc_powerhook = powerhook_establish(ath_power, sc);
    597       1.3  ichiro 	if (sc->sc_powerhook == NULL)
    598       1.3  ichiro 		printf("%s: WARNING: unable to establish power hook\n",
    599       1.3  ichiro 			sc->sc_dev.dv_xname);
    600       1.3  ichiro #endif
    601       1.1  dyoung 	return 0;
    602       1.1  dyoung bad:
    603       1.1  dyoung 	if (ah)
    604       1.1  dyoung 		ath_hal_detach(ah);
    605       1.1  dyoung 	sc->sc_invalid = 1;
    606       1.1  dyoung 	return error;
    607       1.1  dyoung }
    608       1.1  dyoung 
    609       1.1  dyoung int
    610       1.1  dyoung ath_detach(struct ath_softc *sc)
    611       1.1  dyoung {
    612       1.1  dyoung 	struct ifnet *ifp = &sc->sc_ic.ic_if;
    613       1.2  dyoung 	ath_softc_critsect_decl(s);
    614       1.1  dyoung 
    615       1.1  dyoung 	DPRINTF(("ath_detach: if_flags %x\n", ifp->if_flags));
    616       1.3  ichiro 	if ((sc->sc_flags & ATH_ATTACHED) == 0)
    617       1.3  ichiro 		return (0);
    618       1.1  dyoung 
    619       1.2  dyoung 	ath_softc_critsect_begin(sc, s);
    620       1.1  dyoung 	ath_stop(ifp);
    621       1.2  dyoung #if NBPFILTER > 0
    622       1.1  dyoung 	bpfdetach(ifp);
    623       1.2  dyoung #endif
    624       1.1  dyoung 	ath_desc_free(sc);
    625       1.1  dyoung 	ath_hal_detach(sc->sc_ah);
    626       1.1  dyoung 	ieee80211_ifdetach(ifp);
    627       1.2  dyoung #ifdef __NetBSD__
    628       1.2  dyoung 	if_detach(ifp);
    629      1.18  dyoung #endif /* __NetBSD__ */
    630       1.2  dyoung 	ath_softc_critsect_end(sc, s);
    631      1.10  ichiro #ifdef __NetBSD__
    632      1.13    yamt 	powerhook_disestablish(sc->sc_powerhook);
    633      1.13    yamt 	shutdownhook_disestablish(sc->sc_sdhook);
    634      1.18  dyoung #endif /* __NetBSD__ */
    635      1.18  dyoung #ifdef __FreeBSD__
    636      1.18  dyoung 
    637      1.18  dyoung 	ATH_TXBUF_LOCK_DESTROY(sc);
    638      1.18  dyoung 	ATH_TXQ_LOCK_DESTROY(sc);
    639      1.18  dyoung 
    640      1.18  dyoung #endif /* __FreeBSD__ */
    641       1.1  dyoung 	return 0;
    642       1.1  dyoung }
    643       1.1  dyoung 
    644      1.10  ichiro #ifdef __NetBSD__
    645       1.1  dyoung void
    646       1.3  ichiro ath_power(int why, void *arg)
    647       1.3  ichiro {
    648       1.3  ichiro 	struct ath_softc *sc = arg;
    649       1.3  ichiro 	int s;
    650       1.3  ichiro 
    651       1.3  ichiro 	DPRINTF(("ath_power(%d)\n", why));
    652       1.3  ichiro 
    653       1.3  ichiro 	s = splnet();
    654       1.3  ichiro 	switch (why) {
    655       1.3  ichiro 	case PWR_SUSPEND:
    656       1.3  ichiro 	case PWR_STANDBY:
    657       1.3  ichiro 		ath_suspend(sc, why);
    658       1.3  ichiro 		break;
    659       1.3  ichiro 	case PWR_RESUME:
    660       1.3  ichiro 		ath_resume(sc, why);
    661       1.3  ichiro 		break;
    662       1.3  ichiro 	case PWR_SOFTSUSPEND:
    663       1.3  ichiro 	case PWR_SOFTSTANDBY:
    664       1.3  ichiro 	case PWR_SOFTRESUME:
    665       1.3  ichiro 		break;
    666       1.3  ichiro 	}
    667       1.3  ichiro 	splx(s);
    668       1.3  ichiro }
    669      1.10  ichiro #endif
    670       1.3  ichiro 
    671       1.3  ichiro void
    672       1.3  ichiro ath_suspend(struct ath_softc *sc, int why)
    673       1.1  dyoung {
    674       1.1  dyoung 	struct ifnet *ifp = &sc->sc_ic.ic_if;
    675       1.1  dyoung 
    676       1.1  dyoung 	DPRINTF(("ath_suspend: if_flags %x\n", ifp->if_flags));
    677       1.1  dyoung 
    678       1.1  dyoung 	ath_stop(ifp);
    679       1.3  ichiro 	if (sc->sc_power != NULL)
    680       1.3  ichiro 		(*sc->sc_power)(sc, why);
    681       1.1  dyoung }
    682       1.1  dyoung 
    683       1.1  dyoung void
    684       1.3  ichiro ath_resume(struct ath_softc *sc, int why)
    685       1.1  dyoung {
    686       1.1  dyoung 	struct ifnet *ifp = &sc->sc_ic.ic_if;
    687       1.1  dyoung 
    688       1.1  dyoung 	DPRINTF(("ath_resume: if_flags %x\n", ifp->if_flags));
    689       1.1  dyoung 
    690       1.1  dyoung 	if (ifp->if_flags & IFF_UP) {
    691       1.1  dyoung 		ath_init(ifp);
    692       1.3  ichiro #if 0
    693       1.3  ichiro 		(void)ath_intr(sc);
    694       1.3  ichiro #endif
    695       1.3  ichiro 		if (sc->sc_power != NULL)
    696       1.3  ichiro 			(*sc->sc_power)(sc, why);
    697       1.1  dyoung 		if (ifp->if_flags & IFF_RUNNING)
    698       1.1  dyoung 			ath_start(ifp);
    699       1.1  dyoung 	}
    700       1.1  dyoung }
    701       1.1  dyoung 
    702      1.10  ichiro #ifdef __NetBSD__
    703      1.10  ichiro void
    704      1.10  ichiro ath_shutdown(void *arg)
    705      1.10  ichiro {
    706      1.10  ichiro 	struct ath_softc *sc = arg;
    707      1.10  ichiro 
    708      1.10  ichiro 	ath_stop(&sc->sc_ic.ic_if);
    709      1.10  ichiro }
    710      1.10  ichiro #else
    711       1.1  dyoung void
    712       1.1  dyoung ath_shutdown(struct ath_softc *sc)
    713       1.1  dyoung {
    714       1.2  dyoung #if 1
    715       1.2  dyoung 	return;
    716       1.2  dyoung #else
    717       1.1  dyoung 	struct ifnet *ifp = &sc->sc_ic.ic_if;
    718       1.1  dyoung 
    719       1.1  dyoung 	DPRINTF(("ath_shutdown: if_flags %x\n", ifp->if_flags));
    720       1.1  dyoung 
    721       1.1  dyoung 	ath_stop(ifp);
    722       1.2  dyoung #endif
    723       1.1  dyoung }
    724      1.10  ichiro #endif
    725       1.1  dyoung 
    726       1.2  dyoung #ifdef __NetBSD__
    727       1.2  dyoung int
    728       1.2  dyoung ath_intr(void *arg)
    729       1.2  dyoung {
    730       1.2  dyoung 	return ath_intr1((struct ath_softc *)arg);
    731       1.2  dyoung }
    732       1.2  dyoung #else
    733       1.1  dyoung void
    734       1.1  dyoung ath_intr(void *arg)
    735       1.1  dyoung {
    736       1.2  dyoung 	(void)ath_intr1((struct ath_softc *)arg);
    737       1.2  dyoung }
    738       1.2  dyoung #endif
    739       1.2  dyoung 
    740       1.2  dyoung static int
    741       1.2  dyoung ath_intr1(struct ath_softc *sc)
    742       1.2  dyoung {
    743       1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
    744       1.1  dyoung 	struct ifnet *ifp = &ic->ic_if;
    745       1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
    746       1.1  dyoung 	HAL_INT status;
    747       1.1  dyoung 
    748       1.1  dyoung 	if (sc->sc_invalid) {
    749       1.1  dyoung 		/*
    750       1.1  dyoung 		 * The hardware is not ready/present, don't touch anything.
    751       1.1  dyoung 		 * Note this can happen early on if the IRQ is shared.
    752       1.1  dyoung 		 */
    753       1.1  dyoung 		DPRINTF(("ath_intr: invalid; ignored\n"));
    754       1.2  dyoung 		return 0;
    755       1.1  dyoung 	}
    756       1.1  dyoung 	if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) != (IFF_RUNNING|IFF_UP)) {
    757       1.1  dyoung 		DPRINTF(("ath_intr: if_flags 0x%x\n", ifp->if_flags));
    758       1.1  dyoung 		ath_hal_getisr(ah, &status);	/* clear ISR */
    759       1.1  dyoung 		ath_hal_intrset(ah, 0);		/* disable further intr's */
    760       1.2  dyoung 		return 1; /* XXX */
    761       1.1  dyoung 	}
    762       1.1  dyoung 	ath_hal_getisr(ah, &status);		/* NB: clears ISR too */
    763       1.1  dyoung 	DPRINTF2(("ath_intr: status 0x%x\n", status));
    764       1.1  dyoung #ifdef AR_DEBUG
    765       1.1  dyoung 	if (ath_debug &&
    766       1.1  dyoung 	    (status & (HAL_INT_FATAL|HAL_INT_RXORN|HAL_INT_BMISS))) {
    767       1.1  dyoung 		if_printf(ifp, "ath_intr: status 0x%x\n", status);
    768       1.1  dyoung 		ath_hal_dumpstate(ah);
    769       1.1  dyoung 	}
    770       1.1  dyoung #endif /* AR_DEBUG */
    771      1.18  dyoung 	status &= sc->sc_imask;			/* discard unasked for bits */
    772       1.1  dyoung 	if (status & HAL_INT_FATAL) {
    773       1.1  dyoung 		sc->sc_stats.ast_hardware++;
    774       1.1  dyoung 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
    775       1.2  dyoung 		ATH_TASK_RUN_OR_ENQUEUE(&sc->sc_fataltask);
    776       1.1  dyoung 	} else if (status & HAL_INT_RXORN) {
    777       1.1  dyoung 		sc->sc_stats.ast_rxorn++;
    778       1.1  dyoung 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
    779       1.2  dyoung 		ATH_TASK_RUN_OR_ENQUEUE(&sc->sc_rxorntask);
    780       1.1  dyoung 	} else {
    781       1.1  dyoung 		if (status & HAL_INT_RXEOL) {
    782       1.1  dyoung 			/*
    783       1.1  dyoung 			 * NB: the hardware should re-read the link when
    784       1.1  dyoung 			 *     RXE bit is written, but it doesn't work at
    785       1.1  dyoung 			 *     least on older hardware revs.
    786       1.1  dyoung 			 */
    787       1.1  dyoung 			sc->sc_stats.ast_rxeol++;
    788       1.1  dyoung 			sc->sc_rxlink = NULL;
    789       1.1  dyoung 		}
    790       1.1  dyoung 		if (status & HAL_INT_TXURN) {
    791       1.1  dyoung 			sc->sc_stats.ast_txurn++;
    792       1.1  dyoung 			/* bump tx trigger level */
    793       1.1  dyoung 			ath_hal_updatetxtriglevel(ah, AH_TRUE);
    794       1.1  dyoung 		}
    795       1.1  dyoung 		if (status & HAL_INT_RX)
    796       1.2  dyoung 			ATH_TASK_RUN_OR_ENQUEUE(&sc->sc_rxtask);
    797       1.1  dyoung 		if (status & HAL_INT_TX)
    798       1.2  dyoung 			ATH_TASK_RUN_OR_ENQUEUE(&sc->sc_txtask);
    799       1.1  dyoung 		if (status & HAL_INT_SWBA)
    800       1.2  dyoung 			ATH_TASK_RUN_OR_ENQUEUE(&sc->sc_swbatask);
    801       1.1  dyoung 		if (status & HAL_INT_BMISS) {
    802       1.1  dyoung 			sc->sc_stats.ast_bmiss++;
    803       1.2  dyoung 			ATH_TASK_RUN_OR_ENQUEUE(&sc->sc_bmisstask);
    804       1.1  dyoung 		}
    805       1.1  dyoung 	}
    806       1.2  dyoung 	return 1;
    807       1.1  dyoung }
    808       1.1  dyoung 
    809       1.1  dyoung static void
    810       1.1  dyoung ath_fatal_proc(void *arg, int pending)
    811       1.1  dyoung {
    812       1.1  dyoung 	struct ath_softc *sc = arg;
    813       1.1  dyoung 
    814       1.1  dyoung 	device_printf(sc->sc_dev, "hardware error; resetting\n");
    815       1.1  dyoung 	ath_reset(sc);
    816       1.1  dyoung }
    817       1.1  dyoung 
    818       1.1  dyoung static void
    819       1.1  dyoung ath_rxorn_proc(void *arg, int pending)
    820       1.1  dyoung {
    821       1.1  dyoung 	struct ath_softc *sc = arg;
    822       1.1  dyoung 
    823       1.1  dyoung 	device_printf(sc->sc_dev, "rx FIFO overrun; resetting\n");
    824       1.1  dyoung 	ath_reset(sc);
    825       1.1  dyoung }
    826       1.1  dyoung 
    827       1.1  dyoung static void
    828       1.1  dyoung ath_bmiss_proc(void *arg, int pending)
    829       1.1  dyoung {
    830       1.1  dyoung 	struct ath_softc *sc = arg;
    831       1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
    832       1.1  dyoung 
    833       1.1  dyoung 	DPRINTF(("ath_bmiss_proc: pending %u\n", pending));
    834      1.17  dyoung 	if (ic->ic_opmode != IEEE80211_M_STA)
    835      1.17  dyoung 		return;
    836      1.18  dyoung 	if (ic->ic_state == IEEE80211_S_RUN) {
    837      1.18  dyoung 		/*
    838      1.18  dyoung 		 * Rather than go directly to scan state, try to
    839      1.18  dyoung 		 * reassociate first.  If that fails then the state
    840      1.18  dyoung 		 * machine will drop us into scanning after timing
    841      1.18  dyoung 		 * out waiting for a probe response.
    842      1.18  dyoung 		 */
    843      1.18  dyoung 		ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1);
    844      1.18  dyoung 	}
    845       1.1  dyoung }
    846       1.1  dyoung 
    847       1.1  dyoung static u_int
    848       1.1  dyoung ath_chan2flags(struct ieee80211com *ic, struct ieee80211_channel *chan)
    849       1.1  dyoung {
    850       1.4  dyoung 	enum ieee80211_phymode mode = ieee80211_chan2mode(ic, chan);
    851       1.4  dyoung 
    852       1.4  dyoung 	switch (mode) {
    853       1.4  dyoung 	case IEEE80211_MODE_AUTO:
    854       1.4  dyoung 		return 0;
    855       1.4  dyoung 	case IEEE80211_MODE_11A:
    856       1.4  dyoung 		return CHANNEL_A;
    857       1.4  dyoung 	case IEEE80211_MODE_11B:
    858       1.4  dyoung 		return CHANNEL_B;
    859       1.4  dyoung 	case IEEE80211_MODE_11G:
    860       1.4  dyoung 		return CHANNEL_PUREG;
    861       1.4  dyoung 	case IEEE80211_MODE_TURBO:
    862       1.4  dyoung 		return CHANNEL_T;
    863       1.4  dyoung 	default:
    864       1.4  dyoung 		panic("%s: unsupported mode %d\n", __func__, mode);
    865       1.4  dyoung 		return 0;
    866       1.4  dyoung 	}
    867       1.1  dyoung }
    868       1.1  dyoung 
    869       1.2  dyoung #ifdef __NetBSD__
    870       1.2  dyoung static int
    871       1.2  dyoung ath_init(struct ifnet *ifp)
    872       1.2  dyoung {
    873       1.2  dyoung 	return ath_init1((struct ath_softc *)ifp->if_softc);
    874       1.2  dyoung }
    875       1.2  dyoung #else
    876       1.1  dyoung static void
    877       1.1  dyoung ath_init(void *arg)
    878       1.1  dyoung {
    879       1.2  dyoung 	(void)ath_init1((struct ath_softc *)arg);
    880       1.2  dyoung }
    881       1.2  dyoung #endif
    882       1.2  dyoung 
    883       1.2  dyoung static int
    884       1.2  dyoung ath_init1(struct ath_softc *sc)
    885       1.2  dyoung {
    886       1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
    887       1.1  dyoung 	struct ifnet *ifp = &ic->ic_if;
    888       1.1  dyoung 	struct ieee80211_node *ni;
    889       1.1  dyoung 	enum ieee80211_phymode mode;
    890       1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
    891       1.1  dyoung 	HAL_STATUS status;
    892       1.1  dyoung 	HAL_CHANNEL hchan;
    893       1.2  dyoung 	int error = 0;
    894       1.2  dyoung 	ath_softc_critsect_decl(s);
    895       1.1  dyoung 
    896       1.1  dyoung 	DPRINTF(("ath_init: if_flags 0x%x\n", ifp->if_flags));
    897       1.1  dyoung 
    898       1.3  ichiro #ifdef __NetBSD__
    899       1.3  ichiro 	if ((error = ath_enable(sc)) != 0)
    900       1.3  ichiro 		return error;
    901       1.3  ichiro #endif
    902       1.3  ichiro 
    903       1.2  dyoung 	ath_softc_critsect_begin(sc, s);
    904       1.1  dyoung 	/*
    905       1.1  dyoung 	 * Stop anything previously setup.  This is safe
    906       1.1  dyoung 	 * whether this is the first time through or not.
    907       1.1  dyoung 	 */
    908       1.1  dyoung 	ath_stop(ifp);
    909       1.1  dyoung 
    910       1.1  dyoung 	/*
    911       1.1  dyoung 	 * The basic interface to setting the hardware in a good
    912       1.1  dyoung 	 * state is ``reset''.  On return the hardware is known to
    913       1.1  dyoung 	 * be powered up and with interrupts disabled.  This must
    914       1.1  dyoung 	 * be followed by initialization of the appropriate bits
    915       1.1  dyoung 	 * and then setup of the interrupt mask.
    916       1.1  dyoung 	 */
    917       1.1  dyoung 	hchan.channel = ic->ic_ibss_chan->ic_freq;
    918       1.1  dyoung 	hchan.channelFlags = ath_chan2flags(ic, ic->ic_ibss_chan);
    919       1.1  dyoung 	if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_FALSE, &status)) {
    920       1.1  dyoung 		if_printf(ifp, "unable to reset hardware; hal status %u\n",
    921       1.1  dyoung 			status);
    922       1.2  dyoung 		error = -1;
    923       1.1  dyoung 		goto done;
    924       1.1  dyoung 	}
    925       1.1  dyoung 
    926       1.1  dyoung 	/*
    927       1.1  dyoung 	 * Setup the hardware after reset: the key cache
    928       1.1  dyoung 	 * is filled as needed and the receive engine is
    929       1.1  dyoung 	 * set going.  Frame transmit is handled entirely
    930       1.1  dyoung 	 * in the frame output path; there's nothing to do
    931       1.1  dyoung 	 * here except setup the interrupt mask.
    932       1.1  dyoung 	 */
    933       1.1  dyoung 	if (ic->ic_flags & IEEE80211_F_WEPON)
    934       1.1  dyoung 		ath_initkeytable(sc);
    935       1.2  dyoung 	if ((error = ath_startrecv(sc)) != 0) {
    936       1.1  dyoung 		if_printf(ifp, "unable to start recv logic\n");
    937       1.1  dyoung 		goto done;
    938       1.1  dyoung 	}
    939       1.1  dyoung 
    940       1.1  dyoung 	/*
    941       1.1  dyoung 	 * Enable interrupts.
    942       1.1  dyoung 	 */
    943       1.1  dyoung 	sc->sc_imask = HAL_INT_RX | HAL_INT_TX
    944       1.1  dyoung 		  | HAL_INT_RXEOL | HAL_INT_RXORN
    945       1.1  dyoung 		  | HAL_INT_FATAL | HAL_INT_GLOBAL;
    946       1.1  dyoung 	ath_hal_intrset(ah, sc->sc_imask);
    947       1.1  dyoung 
    948       1.1  dyoung 	ifp->if_flags |= IFF_RUNNING;
    949       1.1  dyoung 	ic->ic_state = IEEE80211_S_INIT;
    950       1.1  dyoung 
    951       1.1  dyoung 	/*
    952       1.1  dyoung 	 * The hardware should be ready to go now so it's safe
    953       1.1  dyoung 	 * to kick the 802.11 state machine as it's likely to
    954       1.1  dyoung 	 * immediately call back to us to send mgmt frames.
    955       1.1  dyoung 	 */
    956       1.1  dyoung 	ni = ic->ic_bss;
    957       1.1  dyoung 	ni->ni_chan = ic->ic_ibss_chan;
    958       1.1  dyoung 	mode = ieee80211_chan2mode(ic, ni->ni_chan);
    959       1.1  dyoung 	if (mode != sc->sc_curmode)
    960       1.1  dyoung 		ath_setcurmode(sc, mode);
    961       1.1  dyoung 	if (ic->ic_opmode != IEEE80211_M_MONITOR)
    962       1.1  dyoung 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
    963       1.1  dyoung 	else
    964       1.1  dyoung 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
    965       1.1  dyoung done:
    966       1.2  dyoung 	ath_softc_critsect_end(sc, s);
    967       1.2  dyoung 	return error;
    968       1.1  dyoung }
    969       1.1  dyoung 
    970       1.1  dyoung static void
    971       1.1  dyoung ath_stop(struct ifnet *ifp)
    972       1.1  dyoung {
    973       1.1  dyoung 	struct ieee80211com *ic = (struct ieee80211com *) ifp;
    974       1.1  dyoung 	struct ath_softc *sc = ifp->if_softc;
    975       1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
    976       1.2  dyoung 	ath_softc_critsect_decl(s);
    977       1.1  dyoung 
    978       1.1  dyoung 	DPRINTF(("ath_stop: invalid %u if_flags 0x%x\n",
    979       1.1  dyoung 		sc->sc_invalid, ifp->if_flags));
    980       1.1  dyoung 
    981       1.2  dyoung 	ath_softc_critsect_begin(sc, s);
    982       1.1  dyoung 	if (ifp->if_flags & IFF_RUNNING) {
    983       1.1  dyoung 		/*
    984       1.1  dyoung 		 * Shutdown the hardware and driver:
    985       1.1  dyoung 		 *    disable interrupts
    986       1.1  dyoung 		 *    turn off timers
    987       1.1  dyoung 		 *    clear transmit machinery
    988       1.1  dyoung 		 *    clear receive machinery
    989       1.1  dyoung 		 *    drain and release tx queues
    990       1.1  dyoung 		 *    reclaim beacon resources
    991       1.1  dyoung 		 *    reset 802.11 state machine
    992       1.1  dyoung 		 *    power down hardware
    993       1.1  dyoung 		 *
    994       1.1  dyoung 		 * Note that some of this work is not possible if the
    995       1.1  dyoung 		 * hardware is gone (invalid).
    996       1.1  dyoung 		 */
    997       1.1  dyoung 		ifp->if_flags &= ~IFF_RUNNING;
    998       1.1  dyoung 		ifp->if_timer = 0;
    999       1.1  dyoung 		if (!sc->sc_invalid)
   1000       1.1  dyoung 			ath_hal_intrset(ah, 0);
   1001       1.1  dyoung 		ath_draintxq(sc);
   1002       1.1  dyoung 		if (!sc->sc_invalid)
   1003       1.1  dyoung 			ath_stoprecv(sc);
   1004       1.1  dyoung 		else
   1005       1.1  dyoung 			sc->sc_rxlink = NULL;
   1006       1.2  dyoung #ifdef __FreeBSD__
   1007       1.1  dyoung 		IF_DRAIN(&ifp->if_snd);
   1008       1.2  dyoung #else
   1009       1.2  dyoung 		IF_PURGE(&ifp->if_snd);
   1010       1.2  dyoung #endif
   1011       1.1  dyoung 		ath_beacon_free(sc);
   1012       1.1  dyoung 		ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
   1013       1.3  ichiro 		if (!sc->sc_invalid) {
   1014       1.1  dyoung 			ath_hal_setpower(ah, HAL_PM_FULL_SLEEP, 0);
   1015       1.3  ichiro 		}
   1016       1.3  ichiro #ifdef __NetBSD__
   1017       1.3  ichiro 		ath_disable(sc);
   1018       1.3  ichiro #endif
   1019       1.1  dyoung 	}
   1020       1.2  dyoung 	ath_softc_critsect_end(sc, s);
   1021       1.1  dyoung }
   1022       1.1  dyoung 
   1023       1.1  dyoung /*
   1024       1.1  dyoung  * Reset the hardware w/o losing operational state.  This is
   1025       1.1  dyoung  * basically a more efficient way of doing ath_stop, ath_init,
   1026       1.1  dyoung  * followed by state transitions to the current 802.11
   1027       1.1  dyoung  * operational state.  Used to recover from errors rx overrun
   1028       1.1  dyoung  * and to reset the hardware when rf gain settings must be reset.
   1029       1.1  dyoung  */
   1030       1.1  dyoung static void
   1031       1.1  dyoung ath_reset(struct ath_softc *sc)
   1032       1.1  dyoung {
   1033       1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   1034       1.1  dyoung 	struct ifnet *ifp = &ic->ic_if;
   1035       1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   1036       1.1  dyoung 	struct ieee80211_channel *c;
   1037       1.1  dyoung 	HAL_STATUS status;
   1038       1.1  dyoung 	HAL_CHANNEL hchan;
   1039       1.1  dyoung 
   1040       1.1  dyoung 	/*
   1041       1.1  dyoung 	 * Convert to a HAL channel description with the flags
   1042       1.1  dyoung 	 * constrained to reflect the current operating mode.
   1043       1.1  dyoung 	 */
   1044       1.1  dyoung 	c = ic->ic_ibss_chan;
   1045       1.1  dyoung 	hchan.channel = c->ic_freq;
   1046       1.1  dyoung 	hchan.channelFlags = ath_chan2flags(ic, c);
   1047       1.1  dyoung 
   1048       1.1  dyoung 	ath_hal_intrset(ah, 0);		/* disable interrupts */
   1049       1.1  dyoung 	ath_draintxq(sc);		/* stop xmit side */
   1050       1.1  dyoung 	ath_stoprecv(sc);		/* stop recv side */
   1051       1.1  dyoung 	/* NB: indicate channel change so we do a full reset */
   1052       1.1  dyoung 	if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_TRUE, &status))
   1053       1.1  dyoung 		if_printf(ifp, "%s: unable to reset hardware; hal status %u\n",
   1054       1.1  dyoung 			__func__, status);
   1055       1.1  dyoung 	ath_hal_intrset(ah, sc->sc_imask);
   1056       1.1  dyoung 	if (ath_startrecv(sc) != 0)	/* restart recv */
   1057       1.1  dyoung 		if_printf(ifp, "%s: unable to start recv logic\n", __func__);
   1058       1.1  dyoung 	ath_start(ifp);			/* restart xmit */
   1059       1.1  dyoung 	if (ic->ic_state == IEEE80211_S_RUN)
   1060       1.1  dyoung 		ath_beacon_config(sc);	/* restart beacons */
   1061       1.1  dyoung }
   1062       1.1  dyoung 
   1063       1.1  dyoung static void
   1064       1.1  dyoung ath_start(struct ifnet *ifp)
   1065       1.1  dyoung {
   1066       1.1  dyoung 	struct ath_softc *sc = ifp->if_softc;
   1067       1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   1068       1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   1069       1.1  dyoung 	struct ieee80211_node *ni;
   1070       1.1  dyoung 	struct ath_buf *bf;
   1071       1.1  dyoung 	struct mbuf *m;
   1072       1.1  dyoung 	struct ieee80211_frame *wh;
   1073       1.2  dyoung 	ath_txbuf_critsect_decl(s);
   1074       1.1  dyoung 
   1075       1.1  dyoung 	if ((ifp->if_flags & IFF_RUNNING) == 0 || sc->sc_invalid)
   1076       1.1  dyoung 		return;
   1077       1.1  dyoung 	for (;;) {
   1078       1.1  dyoung 		/*
   1079       1.1  dyoung 		 * Grab a TX buffer and associated resources.
   1080       1.1  dyoung 		 */
   1081       1.2  dyoung 		ath_txbuf_critsect_begin(sc, s);
   1082       1.1  dyoung 		bf = TAILQ_FIRST(&sc->sc_txbuf);
   1083       1.1  dyoung 		if (bf != NULL)
   1084       1.1  dyoung 			TAILQ_REMOVE(&sc->sc_txbuf, bf, bf_list);
   1085       1.2  dyoung 		ath_txbuf_critsect_end(sc, s);
   1086       1.1  dyoung 		if (bf == NULL) {
   1087       1.1  dyoung 			DPRINTF(("ath_start: out of xmit buffers\n"));
   1088       1.1  dyoung 			sc->sc_stats.ast_tx_qstop++;
   1089       1.1  dyoung 			ifp->if_flags |= IFF_OACTIVE;
   1090       1.1  dyoung 			break;
   1091       1.1  dyoung 		}
   1092       1.1  dyoung 		/*
   1093       1.1  dyoung 		 * Poll the management queue for frames; they
   1094       1.1  dyoung 		 * have priority over normal data frames.
   1095       1.1  dyoung 		 */
   1096       1.1  dyoung 		IF_DEQUEUE(&ic->ic_mgtq, m);
   1097       1.1  dyoung 		if (m == NULL) {
   1098       1.1  dyoung 			/*
   1099       1.1  dyoung 			 * No data frames go out unless we're associated.
   1100       1.1  dyoung 			 */
   1101       1.1  dyoung 			if (ic->ic_state != IEEE80211_S_RUN) {
   1102       1.1  dyoung 				DPRINTF(("ath_start: ignore data packet, "
   1103       1.1  dyoung 					"state %u\n", ic->ic_state));
   1104       1.1  dyoung 				sc->sc_stats.ast_tx_discard++;
   1105       1.2  dyoung 				ath_txbuf_critsect_begin(sc, s);
   1106       1.1  dyoung 				TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
   1107       1.2  dyoung 				ath_txbuf_critsect_end(sc, s);
   1108       1.1  dyoung 				break;
   1109       1.1  dyoung 			}
   1110       1.1  dyoung 			IF_DEQUEUE(&ifp->if_snd, m);
   1111       1.1  dyoung 			if (m == NULL) {
   1112       1.2  dyoung 				ath_txbuf_critsect_begin(sc, s);
   1113       1.1  dyoung 				TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
   1114       1.2  dyoung 				ath_txbuf_critsect_end(sc, s);
   1115       1.1  dyoung 				break;
   1116       1.1  dyoung 			}
   1117       1.1  dyoung 			ifp->if_opackets++;
   1118       1.2  dyoung 
   1119       1.2  dyoung #ifdef __NetBSD__
   1120       1.2  dyoung #if NBPFILTER > 0
   1121       1.2  dyoung 			if (ifp->if_bpf)
   1122       1.2  dyoung 				bpf_mtap(ifp->if_bpf, m);
   1123       1.2  dyoung #endif
   1124       1.2  dyoung #endif
   1125       1.2  dyoung #ifdef __FreeBSD__
   1126       1.1  dyoung 			BPF_MTAP(ifp, m);
   1127       1.2  dyoung #endif
   1128       1.1  dyoung 			/*
   1129       1.1  dyoung 			 * Encapsulate the packet in prep for transmission.
   1130       1.1  dyoung 			 */
   1131       1.1  dyoung 			m = ieee80211_encap(ifp, m, &ni);
   1132       1.1  dyoung 			if (m == NULL) {
   1133       1.1  dyoung 				DPRINTF(("ath_start: encapsulation failure\n"));
   1134       1.1  dyoung 				sc->sc_stats.ast_tx_encap++;
   1135       1.1  dyoung 				goto bad;
   1136       1.1  dyoung 			}
   1137       1.1  dyoung 			wh = mtod(m, struct ieee80211_frame *);
   1138       1.1  dyoung 			if (ic->ic_flags & IEEE80211_F_WEPON)
   1139       1.1  dyoung 				wh->i_fc[1] |= IEEE80211_FC1_WEP;
   1140       1.1  dyoung 		} else {
   1141       1.1  dyoung 			/*
   1142       1.1  dyoung 			 * Hack!  The referenced node pointer is in the
   1143       1.1  dyoung 			 * rcvif field of the packet header.  This is
   1144       1.1  dyoung 			 * placed there by ieee80211_mgmt_output because
   1145       1.1  dyoung 			 * we need to hold the reference with the frame
   1146       1.1  dyoung 			 * and there's no other way (other than packet
   1147       1.1  dyoung 			 * tags which we consider too expensive to use)
   1148       1.1  dyoung 			 * to pass it along.
   1149       1.1  dyoung 			 */
   1150       1.1  dyoung 			ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
   1151       1.1  dyoung 			m->m_pkthdr.rcvif = NULL;
   1152       1.1  dyoung 
   1153       1.1  dyoung 			wh = mtod(m, struct ieee80211_frame *);
   1154       1.1  dyoung 			if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
   1155       1.1  dyoung 			    IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
   1156       1.1  dyoung 				/* fill time stamp */
   1157       1.1  dyoung 				u_int64_t tsf;
   1158       1.1  dyoung 				u_int32_t *tstamp;
   1159       1.1  dyoung 
   1160       1.1  dyoung 				tsf = ath_hal_gettsf64(ah);
   1161       1.1  dyoung 				/* XXX: adjust 100us delay to xmit */
   1162       1.1  dyoung 				tsf += 100;
   1163       1.1  dyoung 				tstamp = (u_int32_t *)&wh[1];
   1164       1.1  dyoung 				tstamp[0] = htole32(tsf & 0xffffffff);
   1165       1.1  dyoung 				tstamp[1] = htole32(tsf >> 32);
   1166       1.1  dyoung 			}
   1167       1.1  dyoung 			sc->sc_stats.ast_tx_mgmt++;
   1168       1.1  dyoung 		}
   1169       1.2  dyoung #if NBPFILTER > 0
   1170       1.1  dyoung 		if (ic->ic_rawbpf)
   1171       1.1  dyoung 			bpf_mtap(ic->ic_rawbpf, m);
   1172       1.2  dyoung #endif
   1173       1.1  dyoung 
   1174       1.2  dyoung #if NBPFILTER > 0
   1175       1.1  dyoung 		if (sc->sc_drvbpf) {
   1176       1.2  dyoung #ifdef __FreeBSD__
   1177       1.1  dyoung 			struct mbuf *mb;
   1178       1.1  dyoung 
   1179       1.1  dyoung 			MGETHDR(mb, M_DONTWAIT, m->m_type);
   1180       1.1  dyoung 			if (mb != NULL) {
   1181       1.1  dyoung 				sc->sc_tx_th.wt_rate =
   1182       1.1  dyoung 					ni->ni_rates.rs_rates[ni->ni_txrate];
   1183       1.1  dyoung 
   1184       1.1  dyoung 				mb->m_next = m;
   1185       1.1  dyoung 				mb->m_data = (caddr_t)&sc->sc_tx_th;
   1186       1.1  dyoung 				mb->m_len = sizeof(sc->sc_tx_th);
   1187       1.1  dyoung 				mb->m_pkthdr.len += mb->m_len;
   1188       1.1  dyoung 				bpf_mtap(sc->sc_drvbpf, mb);
   1189       1.1  dyoung 				m_free(mb);
   1190       1.1  dyoung 			}
   1191       1.2  dyoung #else
   1192       1.2  dyoung 			struct mbuf mb;
   1193       1.2  dyoung 
   1194       1.2  dyoung 			M_COPY_PKTHDR(&mb, m);
   1195       1.2  dyoung 			sc->sc_tx_th.wt_rate =
   1196       1.2  dyoung 				ni->ni_rates.rs_rates[ni->ni_txrate];
   1197       1.2  dyoung 
   1198       1.2  dyoung 			mb.m_next = m;
   1199       1.2  dyoung 			mb.m_data = (caddr_t)&sc->sc_tx_th;
   1200       1.2  dyoung 			mb.m_len = sizeof(sc->sc_tx_th);
   1201       1.2  dyoung 			mb.m_pkthdr.len += mb.m_len;
   1202       1.2  dyoung 			bpf_mtap(sc->sc_drvbpf, &mb);
   1203       1.2  dyoung #endif
   1204       1.1  dyoung 		}
   1205       1.2  dyoung #endif
   1206       1.1  dyoung 
   1207       1.1  dyoung 		if (ath_tx_start(sc, ni, bf, m)) {
   1208       1.1  dyoung 	bad:
   1209       1.2  dyoung 			ath_txbuf_critsect_begin(sc, s);
   1210       1.1  dyoung 			TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
   1211       1.2  dyoung 			ath_txbuf_critsect_end(sc, s);
   1212       1.1  dyoung 			ifp->if_oerrors++;
   1213       1.1  dyoung 			if (ni && ni != ic->ic_bss)
   1214       1.1  dyoung 				ieee80211_free_node(ic, ni);
   1215       1.1  dyoung 			continue;
   1216       1.1  dyoung 		}
   1217       1.1  dyoung 
   1218       1.1  dyoung 		sc->sc_tx_timer = 5;
   1219       1.1  dyoung 		ifp->if_timer = 1;
   1220       1.1  dyoung 	}
   1221       1.1  dyoung }
   1222       1.1  dyoung 
   1223       1.1  dyoung static int
   1224       1.1  dyoung ath_media_change(struct ifnet *ifp)
   1225       1.1  dyoung {
   1226       1.1  dyoung 	int error;
   1227       1.1  dyoung 
   1228       1.1  dyoung 	error = ieee80211_media_change(ifp);
   1229       1.1  dyoung 	if (error == ENETRESET) {
   1230       1.1  dyoung 		if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) ==
   1231       1.1  dyoung 		    (IFF_RUNNING|IFF_UP))
   1232       1.1  dyoung 			ath_init(ifp);		/* XXX lose error */
   1233       1.1  dyoung 		error = 0;
   1234       1.1  dyoung 	}
   1235       1.1  dyoung 	return error;
   1236       1.1  dyoung }
   1237       1.1  dyoung 
   1238       1.1  dyoung static void
   1239       1.1  dyoung ath_watchdog(struct ifnet *ifp)
   1240       1.1  dyoung {
   1241       1.1  dyoung 	struct ath_softc *sc = ifp->if_softc;
   1242       1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   1243       1.1  dyoung 
   1244       1.1  dyoung 	ifp->if_timer = 0;
   1245       1.1  dyoung 	if ((ifp->if_flags & IFF_RUNNING) == 0 || sc->sc_invalid)
   1246       1.1  dyoung 		return;
   1247       1.1  dyoung 	if (sc->sc_tx_timer) {
   1248       1.1  dyoung 		if (--sc->sc_tx_timer == 0) {
   1249       1.1  dyoung 			if_printf(ifp, "device timeout\n");
   1250       1.1  dyoung #ifdef AR_DEBUG
   1251       1.1  dyoung 			if (ath_debug)
   1252       1.1  dyoung 				ath_hal_dumpstate(sc->sc_ah);
   1253       1.1  dyoung #endif /* AR_DEBUG */
   1254       1.1  dyoung 			ath_init(ifp);		/* XXX ath_reset??? */
   1255       1.1  dyoung 			ifp->if_oerrors++;
   1256       1.1  dyoung 			sc->sc_stats.ast_watchdog++;
   1257       1.1  dyoung 			return;
   1258       1.1  dyoung 		}
   1259       1.1  dyoung 		ifp->if_timer = 1;
   1260       1.1  dyoung 	}
   1261       1.1  dyoung 	if (ic->ic_fixed_rate == -1) {
   1262       1.1  dyoung 		/*
   1263       1.1  dyoung 		 * Run the rate control algorithm if we're not
   1264       1.1  dyoung 		 * locked at a fixed rate.
   1265       1.1  dyoung 		 */
   1266       1.1  dyoung 		if (ic->ic_opmode == IEEE80211_M_STA)
   1267       1.1  dyoung 			ath_rate_ctl(sc, ic->ic_bss);
   1268       1.1  dyoung 		else
   1269       1.1  dyoung 			ieee80211_iterate_nodes(ic, ath_rate_ctl, sc);
   1270       1.1  dyoung 	}
   1271       1.1  dyoung 	ieee80211_watchdog(ifp);
   1272       1.1  dyoung }
   1273       1.1  dyoung 
   1274       1.1  dyoung static int
   1275       1.1  dyoung ath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
   1276       1.1  dyoung {
   1277       1.1  dyoung 	struct ath_softc *sc = ifp->if_softc;
   1278       1.1  dyoung 	struct ifreq *ifr = (struct ifreq *)data;
   1279       1.1  dyoung 	int error = 0;
   1280       1.2  dyoung 	ath_softc_critsect_decl(s);
   1281       1.1  dyoung 
   1282       1.2  dyoung 	ath_softc_critsect_begin(sc, s);
   1283       1.1  dyoung 	switch (cmd) {
   1284       1.1  dyoung 	case SIOCSIFFLAGS:
   1285       1.1  dyoung 		if (ifp->if_flags & IFF_UP) {
   1286       1.1  dyoung 			if (ifp->if_flags & IFF_RUNNING) {
   1287       1.1  dyoung 				/*
   1288       1.1  dyoung 				 * To avoid rescanning another access point,
   1289       1.1  dyoung 				 * do not call ath_init() here.  Instead,
   1290       1.1  dyoung 				 * only reflect promisc mode settings.
   1291       1.1  dyoung 				 */
   1292       1.1  dyoung 				ath_mode_init(sc);
   1293      1.18  dyoung 			} else {
   1294      1.18  dyoung 				/*
   1295      1.18  dyoung 				 * Beware of being called during detach to
   1296      1.18  dyoung 				 * reset promiscuous mode.  In that case we
   1297      1.18  dyoung 				 * will still be marked UP but not RUNNING.
   1298      1.18  dyoung 				 * However trying to re-init the interface
   1299      1.18  dyoung 				 * is the wrong thing to do as we've already
   1300      1.18  dyoung 				 * torn down much of our state.  There's
   1301      1.18  dyoung 				 * probably a better way to deal with this.
   1302      1.18  dyoung 				 */
   1303      1.18  dyoung 				if (!sc->sc_invalid)
   1304      1.18  dyoung 					ath_init(ifp);	/* XXX lose error */
   1305      1.18  dyoung 			}
   1306       1.1  dyoung 		} else
   1307       1.1  dyoung 			ath_stop(ifp);
   1308       1.1  dyoung 		break;
   1309       1.1  dyoung 	case SIOCADDMULTI:
   1310       1.1  dyoung 	case SIOCDELMULTI:
   1311       1.5   enami #ifdef __FreeBSD__
   1312       1.1  dyoung 		/*
   1313       1.1  dyoung 		 * The upper layer has already installed/removed
   1314       1.1  dyoung 		 * the multicast address(es), just recalculate the
   1315       1.1  dyoung 		 * multicast filter for the card.
   1316       1.1  dyoung 		 */
   1317       1.1  dyoung 		if (ifp->if_flags & IFF_RUNNING)
   1318       1.1  dyoung 			ath_mode_init(sc);
   1319       1.5   enami #endif
   1320       1.5   enami #ifdef __NetBSD__
   1321       1.5   enami 		error = (cmd == SIOCADDMULTI) ?
   1322       1.5   enami 		    ether_addmulti(ifr, &sc->sc_ic.ic_ec) :
   1323       1.5   enami 		    ether_delmulti(ifr, &sc->sc_ic.ic_ec);
   1324       1.5   enami 		if (error == ENETRESET) {
   1325       1.5   enami 			if (ifp->if_flags & IFF_RUNNING)
   1326       1.5   enami 				ath_mode_init(sc);
   1327       1.8   enami 			error = 0;
   1328       1.5   enami 		}
   1329       1.5   enami #endif
   1330       1.1  dyoung 		break;
   1331       1.1  dyoung 	case SIOCGATHSTATS:
   1332      1.18  dyoung 		error = copyout(&sc->sc_stats,
   1333      1.18  dyoung 				ifr->ifr_data, sizeof (sc->sc_stats));
   1334       1.1  dyoung 		break;
   1335      1.18  dyoung 	case SIOCGATHDIAG: {
   1336      1.18  dyoung 		struct ath_diag *ad = (struct ath_diag *)data;
   1337      1.18  dyoung 		struct ath_hal *ah = sc->sc_ah;
   1338      1.18  dyoung 		void *data;
   1339      1.18  dyoung 		u_int size;
   1340      1.18  dyoung 
   1341      1.18  dyoung 		if (ath_hal_getdiagstate(ah, ad->ad_id, &data, &size)) {
   1342      1.18  dyoung 			if (size < ad->ad_size)
   1343      1.18  dyoung 				ad->ad_size = size;
   1344      1.18  dyoung 			if (data)
   1345      1.18  dyoung 				error = copyout(data, ad->ad_data, ad->ad_size);
   1346      1.18  dyoung 		} else
   1347      1.18  dyoung 			error = EINVAL;
   1348      1.18  dyoung 		break;
   1349      1.18  dyoung 	}
   1350       1.1  dyoung 	default:
   1351       1.1  dyoung 		error = ieee80211_ioctl(ifp, cmd, data);
   1352       1.1  dyoung 		if (error == ENETRESET) {
   1353       1.1  dyoung 			if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) ==
   1354       1.1  dyoung 			    (IFF_RUNNING|IFF_UP))
   1355       1.1  dyoung 				ath_init(ifp);		/* XXX lose error */
   1356       1.1  dyoung 			error = 0;
   1357       1.1  dyoung 		}
   1358       1.1  dyoung 		break;
   1359       1.1  dyoung 	}
   1360       1.2  dyoung 	ath_softc_critsect_end(sc, s);
   1361       1.1  dyoung 	return error;
   1362       1.1  dyoung }
   1363       1.1  dyoung 
   1364       1.1  dyoung /*
   1365       1.1  dyoung  * Fill the hardware key cache with key entries.
   1366       1.1  dyoung  */
   1367       1.1  dyoung static void
   1368       1.1  dyoung ath_initkeytable(struct ath_softc *sc)
   1369       1.1  dyoung {
   1370       1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   1371       1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   1372       1.1  dyoung 	int i;
   1373       1.1  dyoung 
   1374       1.1  dyoung 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   1375       1.1  dyoung 		struct ieee80211_wepkey *k = &ic->ic_nw_keys[i];
   1376       1.1  dyoung 		if (k->wk_len == 0)
   1377       1.1  dyoung 			ath_hal_keyreset(ah, i);
   1378       1.1  dyoung 		else
   1379       1.1  dyoung 			/* XXX return value */
   1380       1.1  dyoung 			/* NB: this uses HAL_KEYVAL == ieee80211_wepkey */
   1381       1.1  dyoung 			ath_hal_keyset(ah, i, (const HAL_KEYVAL *) k);
   1382       1.1  dyoung 	}
   1383       1.1  dyoung }
   1384       1.1  dyoung 
   1385       1.1  dyoung static void
   1386       1.2  dyoung ath_mcastfilter_accum(caddr_t dl, u_int32_t (*mfilt)[2])
   1387       1.2  dyoung {
   1388       1.2  dyoung 	u_int32_t val;
   1389       1.2  dyoung 	u_int8_t pos;
   1390       1.2  dyoung 
   1391       1.2  dyoung 	val = LE_READ_4(dl + 0);
   1392       1.2  dyoung 	pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
   1393       1.2  dyoung 	val = LE_READ_4(dl + 3);
   1394       1.2  dyoung 	pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
   1395       1.2  dyoung 	pos &= 0x3f;
   1396       1.2  dyoung 	(*mfilt)[pos / 32] |= (1 << (pos % 32));
   1397       1.2  dyoung }
   1398       1.2  dyoung 
   1399       1.2  dyoung #ifdef __FreeBSD__
   1400       1.2  dyoung static void
   1401       1.2  dyoung ath_mcastfilter_compute(struct ath_softc *sc, u_int32_t (*mfilt)[2])
   1402       1.2  dyoung {
   1403       1.2  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   1404       1.2  dyoung 	struct ifnet *ifp = &ic->ic_if;
   1405       1.2  dyoung 	struct ifmultiaddr *ifma;
   1406       1.2  dyoung 
   1407       1.2  dyoung 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
   1408       1.2  dyoung 		caddr_t dl;
   1409       1.2  dyoung 
   1410       1.2  dyoung 		/* calculate XOR of eight 6bit values */
   1411       1.2  dyoung 		dl = LLADDR((struct sockaddr_dl *) ifma->ifma_addr);
   1412       1.2  dyoung 		ath_mcastfilter_accum(dl, &mfilt);
   1413       1.2  dyoung 	}
   1414       1.2  dyoung }
   1415       1.2  dyoung #else
   1416       1.2  dyoung static void
   1417       1.2  dyoung ath_mcastfilter_compute(struct ath_softc *sc, u_int32_t (*mfilt)[2])
   1418       1.2  dyoung {
   1419       1.5   enami 	struct ifnet *ifp = &sc->sc_ic.ic_if;
   1420       1.2  dyoung 	struct ether_multi *enm;
   1421       1.2  dyoung 	struct ether_multistep estep;
   1422       1.2  dyoung 
   1423       1.2  dyoung 	ETHER_FIRST_MULTI(estep, &sc->sc_ic.ic_ec, enm);
   1424       1.2  dyoung 	while (enm != NULL) {
   1425       1.2  dyoung 		/* XXX Punt on ranges. */
   1426       1.2  dyoung 		if (!IEEE80211_ADDR_EQ(enm->enm_addrlo, enm->enm_addrhi)) {
   1427       1.2  dyoung 			(*mfilt)[0] = (*mfilt)[1] = ~((u_int32_t)0);
   1428       1.5   enami 			ifp->if_flags |= IFF_ALLMULTI;
   1429       1.5   enami 			return;
   1430       1.2  dyoung 		}
   1431       1.2  dyoung 		ath_mcastfilter_accum(enm->enm_addrlo, mfilt);
   1432       1.2  dyoung 		ETHER_NEXT_MULTI(estep, enm);
   1433       1.2  dyoung 	}
   1434       1.5   enami 	ifp->if_flags &= ~IFF_ALLMULTI;
   1435       1.2  dyoung }
   1436       1.2  dyoung #endif
   1437       1.2  dyoung 
   1438      1.18  dyoung /*
   1439      1.18  dyoung  * Calculate the receive filter according to the
   1440      1.18  dyoung  * operating mode and state:
   1441      1.18  dyoung  *
   1442      1.18  dyoung  * o always accept unicast, broadcast, and multicast traffic
   1443      1.18  dyoung  * o maintain current state of phy error reception
   1444      1.18  dyoung  * o probe request frames are accepted only when operating in
   1445      1.18  dyoung  *   hostap, adhoc, or monitor modes
   1446      1.18  dyoung  * o enable promiscuous mode according to the interface state
   1447      1.18  dyoung  * o accept beacons:
   1448      1.18  dyoung  *   - when operating in adhoc mode so the 802.11 layer creates
   1449      1.18  dyoung  *     node table entries for peers,
   1450      1.18  dyoung  *   - when operating in station mode for collecting rssi data when
   1451      1.18  dyoung  *     the station is otherwise quiet, or
   1452      1.18  dyoung  *   - when scanning
   1453      1.18  dyoung  */
   1454      1.18  dyoung static u_int32_t
   1455      1.18  dyoung ath_calcrxfilter(struct ath_softc *sc)
   1456       1.1  dyoung {
   1457       1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   1458       1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   1459       1.1  dyoung 	struct ifnet *ifp = &ic->ic_if;
   1460      1.18  dyoung 	u_int32_t rfilt;
   1461       1.1  dyoung 
   1462       1.1  dyoung 	rfilt = (ath_hal_getrxfilter(ah) & HAL_RX_FILTER_PHYERR)
   1463       1.1  dyoung 	      | HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST;
   1464       1.1  dyoung 	if (ic->ic_opmode != IEEE80211_M_STA)
   1465       1.1  dyoung 		rfilt |= HAL_RX_FILTER_PROBEREQ;
   1466      1.18  dyoung 	if (ic->ic_opmode == IEEE80211_M_STA ||
   1467      1.18  dyoung 	    ic->ic_opmode == IEEE80211_M_IBSS ||
   1468      1.18  dyoung 	    ic->ic_state == IEEE80211_S_SCAN)
   1469       1.1  dyoung 		rfilt |= HAL_RX_FILTER_BEACON;
   1470  1.24.2.2      he 	if (ifp->if_flags & IFF_PROMISC)
   1471  1.24.2.2      he 		rfilt |= HAL_RX_FILTER_PROM;
   1472      1.18  dyoung 	return rfilt;
   1473      1.18  dyoung }
   1474      1.18  dyoung 
   1475      1.18  dyoung static void
   1476      1.18  dyoung ath_mode_init(struct ath_softc *sc)
   1477      1.18  dyoung {
   1478      1.19  dyoung #ifdef __FreeBSD__
   1479      1.18  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   1480      1.19  dyoung #endif
   1481      1.18  dyoung 	struct ath_hal *ah = sc->sc_ah;
   1482      1.18  dyoung 	u_int32_t rfilt, mfilt[2];
   1483      1.18  dyoung 
   1484      1.18  dyoung 	/* configure rx filter */
   1485      1.18  dyoung 	rfilt = ath_calcrxfilter(sc);
   1486       1.1  dyoung 	ath_hal_setrxfilter(ah, rfilt);
   1487       1.1  dyoung 
   1488      1.18  dyoung 	/* configure operational mode */
   1489      1.19  dyoung 	ath_hal_setopmode(ah);
   1490      1.18  dyoung 
   1491       1.1  dyoung 	/* calculate and install multicast filter */
   1492       1.5   enami #ifdef __FreeBSD__
   1493      1.18  dyoung 	if ((ic->ic_if.if_flags & IFF_ALLMULTI) == 0) {
   1494       1.1  dyoung 		mfilt[0] = mfilt[1] = 0;
   1495       1.2  dyoung 		ath_mcastfilter_compute(sc, &mfilt);
   1496       1.1  dyoung 	} else {
   1497       1.1  dyoung 		mfilt[0] = mfilt[1] = ~0;
   1498       1.1  dyoung 	}
   1499       1.5   enami #endif
   1500       1.5   enami #ifdef __NetBSD__
   1501       1.5   enami 	mfilt[0] = mfilt[1] = 0;
   1502       1.5   enami 	ath_mcastfilter_compute(sc, &mfilt);
   1503       1.5   enami #endif
   1504       1.1  dyoung 	ath_hal_setmcastfilter(ah, mfilt[0], mfilt[1]);
   1505       1.1  dyoung 	DPRINTF(("ath_mode_init: RX filter 0x%x, MC filter %08x:%08x\n",
   1506       1.1  dyoung 		rfilt, mfilt[0], mfilt[1]));
   1507       1.1  dyoung }
   1508       1.1  dyoung 
   1509       1.2  dyoung #ifdef __FreeBSD__
   1510       1.1  dyoung static void
   1511       1.1  dyoung ath_mbuf_load_cb(void *arg, bus_dma_segment_t *seg, int nseg, bus_size_t mapsize, int error)
   1512       1.1  dyoung {
   1513       1.1  dyoung 	struct ath_buf *bf = arg;
   1514       1.1  dyoung 
   1515       1.1  dyoung 	KASSERT(nseg <= ATH_MAX_SCATTER,
   1516       1.1  dyoung 		("ath_mbuf_load_cb: too many DMA segments %u", nseg));
   1517       1.1  dyoung 	bf->bf_mapsize = mapsize;
   1518       1.1  dyoung 	bf->bf_nseg = nseg;
   1519       1.1  dyoung 	bcopy(seg, bf->bf_segs, nseg * sizeof (seg[0]));
   1520       1.1  dyoung }
   1521       1.2  dyoung #endif /* __FreeBSD__ */
   1522       1.2  dyoung 
   1523       1.2  dyoung static struct mbuf *
   1524       1.2  dyoung ath_getmbuf(int flags, int type, u_int pktlen)
   1525       1.2  dyoung {
   1526       1.2  dyoung 	struct mbuf *m;
   1527       1.2  dyoung 
   1528       1.2  dyoung 	KASSERT(pktlen <= MCLBYTES, ("802.11 packet too large: %u", pktlen));
   1529       1.2  dyoung #ifdef __FreeBSD__
   1530       1.2  dyoung 	if (pktlen <= MHLEN)
   1531       1.2  dyoung 		MGETHDR(m, flags, type);
   1532       1.2  dyoung 	else
   1533       1.2  dyoung 		m = m_getcl(flags, type, M_PKTHDR);
   1534       1.2  dyoung #else
   1535       1.2  dyoung 	MGETHDR(m, flags, type);
   1536       1.2  dyoung 	if (m != NULL && pktlen > MHLEN)
   1537       1.2  dyoung 		MCLGET(m, flags);
   1538       1.2  dyoung #endif
   1539       1.2  dyoung 	return m;
   1540       1.2  dyoung }
   1541       1.1  dyoung 
   1542       1.1  dyoung static int
   1543       1.1  dyoung ath_beacon_alloc(struct ath_softc *sc, struct ieee80211_node *ni)
   1544       1.1  dyoung {
   1545       1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   1546       1.1  dyoung 	struct ifnet *ifp = &ic->ic_if;
   1547       1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   1548       1.1  dyoung 	struct ieee80211_frame *wh;
   1549       1.1  dyoung 	struct ath_buf *bf;
   1550       1.1  dyoung 	struct ath_desc *ds;
   1551       1.1  dyoung 	struct mbuf *m;
   1552       1.1  dyoung 	int error, pktlen;
   1553       1.1  dyoung 	u_int8_t *frm, rate;
   1554       1.1  dyoung 	u_int16_t capinfo;
   1555       1.1  dyoung 	struct ieee80211_rateset *rs;
   1556       1.1  dyoung 	const HAL_RATE_TABLE *rt;
   1557       1.1  dyoung 
   1558       1.1  dyoung 	bf = sc->sc_bcbuf;
   1559       1.1  dyoung 	if (bf->bf_m != NULL) {
   1560       1.1  dyoung 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
   1561       1.1  dyoung 		m_freem(bf->bf_m);
   1562       1.1  dyoung 		bf->bf_m = NULL;
   1563       1.1  dyoung 		bf->bf_node = NULL;
   1564       1.1  dyoung 	}
   1565       1.1  dyoung 	/*
   1566       1.1  dyoung 	 * NB: the beacon data buffer must be 32-bit aligned;
   1567       1.1  dyoung 	 * we assume the mbuf routines will return us something
   1568       1.1  dyoung 	 * with this alignment (perhaps should assert).
   1569       1.1  dyoung 	 */
   1570       1.1  dyoung 	rs = &ni->ni_rates;
   1571       1.1  dyoung 	pktlen = sizeof (struct ieee80211_frame)
   1572      1.18  dyoung 	       + 8 + 2 + 2 + 2+ni->ni_esslen + 2+rs->rs_nrates + 3 + 6;
   1573       1.1  dyoung 	if (rs->rs_nrates > IEEE80211_RATE_SIZE)
   1574       1.1  dyoung 		pktlen += 2;
   1575       1.2  dyoung 	m = ath_getmbuf(M_DONTWAIT, MT_DATA, pktlen);
   1576       1.1  dyoung 	if (m == NULL) {
   1577       1.1  dyoung 		DPRINTF(("ath_beacon_alloc: cannot get mbuf/cluster; size %u\n",
   1578       1.1  dyoung 			pktlen));
   1579       1.1  dyoung 		sc->sc_stats.ast_be_nombuf++;
   1580       1.1  dyoung 		return ENOMEM;
   1581       1.1  dyoung 	}
   1582       1.1  dyoung 
   1583       1.1  dyoung 	wh = mtod(m, struct ieee80211_frame *);
   1584       1.1  dyoung 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
   1585       1.1  dyoung 	    IEEE80211_FC0_SUBTYPE_BEACON;
   1586       1.1  dyoung 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
   1587       1.1  dyoung 	*(u_int16_t *)wh->i_dur = 0;
   1588       1.1  dyoung 	memcpy(wh->i_addr1, ifp->if_broadcastaddr, IEEE80211_ADDR_LEN);
   1589       1.1  dyoung 	memcpy(wh->i_addr2, ic->ic_myaddr, IEEE80211_ADDR_LEN);
   1590       1.1  dyoung 	memcpy(wh->i_addr3, ni->ni_bssid, IEEE80211_ADDR_LEN);
   1591       1.1  dyoung 	*(u_int16_t *)wh->i_seq = 0;
   1592       1.1  dyoung 
   1593       1.1  dyoung 	/*
   1594       1.1  dyoung 	 * beacon frame format
   1595       1.1  dyoung 	 *	[8] time stamp
   1596       1.1  dyoung 	 *	[2] beacon interval
   1597       1.1  dyoung 	 *	[2] cabability information
   1598       1.1  dyoung 	 *	[tlv] ssid
   1599       1.1  dyoung 	 *	[tlv] supported rates
   1600       1.1  dyoung 	 *	[tlv] parameter set (IBSS)
   1601       1.1  dyoung 	 *	[tlv] extended supported rates
   1602       1.1  dyoung 	 */
   1603       1.1  dyoung 	frm = (u_int8_t *)&wh[1];
   1604       1.1  dyoung 	memset(frm, 0, 8);	/* timestamp is set by hardware */
   1605       1.1  dyoung 	frm += 8;
   1606       1.1  dyoung 	*(u_int16_t *)frm = htole16(ni->ni_intval);
   1607       1.1  dyoung 	frm += 2;
   1608       1.1  dyoung 	if (ic->ic_opmode == IEEE80211_M_IBSS)
   1609       1.1  dyoung 		capinfo = IEEE80211_CAPINFO_IBSS;
   1610       1.1  dyoung 	else
   1611       1.1  dyoung 		capinfo = IEEE80211_CAPINFO_ESS;
   1612       1.1  dyoung 	if (ic->ic_flags & IEEE80211_F_WEPON)
   1613       1.1  dyoung 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
   1614      1.18  dyoung 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
   1615      1.18  dyoung 	    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
   1616       1.1  dyoung 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
   1617       1.1  dyoung 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
   1618       1.1  dyoung 		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
   1619       1.1  dyoung 	*(u_int16_t *)frm = htole16(capinfo);
   1620       1.1  dyoung 	frm += 2;
   1621       1.1  dyoung 	*frm++ = IEEE80211_ELEMID_SSID;
   1622       1.1  dyoung 	*frm++ = ni->ni_esslen;
   1623       1.1  dyoung 	memcpy(frm, ni->ni_essid, ni->ni_esslen);
   1624       1.1  dyoung 	frm += ni->ni_esslen;
   1625       1.1  dyoung 	frm = ieee80211_add_rates(frm, rs);
   1626      1.18  dyoung 	*frm++ = IEEE80211_ELEMID_DSPARMS;
   1627      1.18  dyoung 	*frm++ = 1;
   1628      1.18  dyoung 	*frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
   1629       1.1  dyoung 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
   1630       1.1  dyoung 		*frm++ = IEEE80211_ELEMID_IBSSPARMS;
   1631       1.1  dyoung 		*frm++ = 2;
   1632       1.1  dyoung 		*frm++ = 0; *frm++ = 0;		/* TODO: ATIM window */
   1633       1.1  dyoung 	} else {
   1634       1.1  dyoung 		/* TODO: TIM */
   1635       1.1  dyoung 		*frm++ = IEEE80211_ELEMID_TIM;
   1636       1.1  dyoung 		*frm++ = 4;	/* length */
   1637       1.1  dyoung 		*frm++ = 0;	/* DTIM count */
   1638       1.1  dyoung 		*frm++ = 1;	/* DTIM period */
   1639       1.1  dyoung 		*frm++ = 0;	/* bitmap control */
   1640       1.1  dyoung 		*frm++ = 0;	/* Partial Virtual Bitmap (variable length) */
   1641       1.1  dyoung 	}
   1642       1.1  dyoung 	frm = ieee80211_add_xrates(frm, rs);
   1643       1.1  dyoung 	m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
   1644       1.1  dyoung 	KASSERT(m->m_pkthdr.len <= pktlen,
   1645       1.1  dyoung 		("beacon bigger than expected, len %u calculated %u",
   1646       1.1  dyoung 		m->m_pkthdr.len, pktlen));
   1647       1.1  dyoung 
   1648       1.1  dyoung 	DPRINTF2(("ath_beacon_alloc: m %p len %u\n", m, m->m_len));
   1649       1.2  dyoung 	error = ath_buf_dmamap_load_mbuf(sc->sc_dmat, bf, m, BUS_DMA_NOWAIT);
   1650       1.1  dyoung 	if (error != 0) {
   1651       1.1  dyoung 		m_freem(m);
   1652       1.1  dyoung 		return error;
   1653       1.1  dyoung 	}
   1654       1.1  dyoung 	KASSERT(bf->bf_nseg == 1,
   1655       1.1  dyoung 		("ath_beacon_alloc: multi-segment packet; nseg %u",
   1656       1.1  dyoung 		bf->bf_nseg));
   1657       1.1  dyoung 	bf->bf_m = m;
   1658       1.1  dyoung 
   1659       1.1  dyoung 	/* setup descriptors */
   1660       1.1  dyoung 	ds = bf->bf_desc;
   1661       1.1  dyoung 
   1662       1.1  dyoung 	ds->ds_link = 0;
   1663       1.1  dyoung 	ds->ds_data = bf->bf_segs[0].ds_addr;
   1664       1.2  dyoung 
   1665       1.2  dyoung 	DPRINTF2(("%s: segaddr %p seglen %u\n", __func__,
   1666       1.2  dyoung 	    (caddr_t)bf->bf_segs[0].ds_addr, (u_int)bf->bf_segs[0].ds_len));
   1667       1.2  dyoung 
   1668       1.1  dyoung 	/*
   1669       1.1  dyoung 	 * Calculate rate code.
   1670       1.1  dyoung 	 * XXX everything at min xmit rate
   1671       1.1  dyoung 	 */
   1672       1.1  dyoung 	rt = sc->sc_currates;
   1673       1.1  dyoung 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
   1674       1.1  dyoung 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
   1675       1.1  dyoung 		rate = rt->info[0].rateCode | rt->info[0].shortPreamble;
   1676       1.1  dyoung 	else
   1677       1.1  dyoung 		rate = rt->info[0].rateCode;
   1678       1.2  dyoung 	if (!ath_hal_setuptxdesc(ah, ds
   1679       1.1  dyoung 		, m->m_pkthdr.len + IEEE80211_CRC_LEN	/* packet length */
   1680       1.1  dyoung 		, sizeof(struct ieee80211_frame)	/* header length */
   1681       1.1  dyoung 		, HAL_PKT_TYPE_BEACON		/* Atheros packet type */
   1682       1.1  dyoung 		, 0x20				/* txpower XXX */
   1683       1.1  dyoung 		, rate, 1			/* series 0 rate/tries */
   1684       1.1  dyoung 		, HAL_TXKEYIX_INVALID		/* no encryption */
   1685       1.1  dyoung 		, 0				/* antenna mode */
   1686       1.1  dyoung 		, HAL_TXDESC_NOACK		/* no ack for beacons */
   1687       1.1  dyoung 		, 0				/* rts/cts rate */
   1688       1.1  dyoung 		, 0				/* rts/cts duration */
   1689       1.2  dyoung 	)) {
   1690       1.2  dyoung 		printf("%s: ath_hal_setuptxdesc failed\n", __func__);
   1691       1.2  dyoung 		return -1;
   1692       1.2  dyoung 	}
   1693       1.1  dyoung 	/* NB: beacon's BufLen must be a multiple of 4 bytes */
   1694       1.1  dyoung 	/* XXX verify mbuf data area covers this roundup */
   1695       1.2  dyoung 	if (!ath_hal_filltxdesc(ah, ds
   1696       1.1  dyoung 		, roundup(bf->bf_segs[0].ds_len, 4)	/* buffer length */
   1697       1.1  dyoung 		, AH_TRUE				/* first segment */
   1698       1.1  dyoung 		, AH_TRUE				/* last segment */
   1699       1.2  dyoung 	)) {
   1700       1.2  dyoung 		printf("%s: ath_hal_filltxdesc failed\n", __func__);
   1701       1.2  dyoung 		return -1;
   1702       1.2  dyoung 	}
   1703       1.2  dyoung 
   1704       1.2  dyoung 	/* XXX it is not appropriate to bus_dmamap_sync? -dcy */
   1705       1.1  dyoung 
   1706       1.1  dyoung 	return 0;
   1707       1.1  dyoung }
   1708       1.1  dyoung 
   1709       1.1  dyoung static void
   1710       1.1  dyoung ath_beacon_proc(void *arg, int pending)
   1711       1.1  dyoung {
   1712       1.1  dyoung 	struct ath_softc *sc = arg;
   1713       1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   1714       1.1  dyoung 	struct ath_buf *bf = sc->sc_bcbuf;
   1715       1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   1716       1.1  dyoung 
   1717       1.1  dyoung 	DPRINTF2(("%s: pending %u\n", __func__, pending));
   1718       1.1  dyoung 	if (ic->ic_opmode == IEEE80211_M_STA ||
   1719       1.1  dyoung 	    bf == NULL || bf->bf_m == NULL) {
   1720       1.1  dyoung 		DPRINTF(("%s: ic_flags=%x bf=%p bf_m=%p\n",
   1721       1.1  dyoung 			__func__, ic->ic_flags, bf, bf ? bf->bf_m : NULL));
   1722       1.1  dyoung 		return;
   1723       1.1  dyoung 	}
   1724       1.1  dyoung 	/* TODO: update beacon to reflect PS poll state */
   1725       1.1  dyoung 	if (!ath_hal_stoptxdma(ah, sc->sc_bhalq)) {
   1726       1.1  dyoung 		DPRINTF(("%s: beacon queue %u did not stop?",
   1727       1.1  dyoung 			__func__, sc->sc_bhalq));
   1728       1.1  dyoung 		return;			/* busy, XXX is this right? */
   1729       1.1  dyoung 	}
   1730       1.2  dyoung 	ath_buf_dmamap_sync(sc->sc_dmat, bf, BUS_DMASYNC_PREWRITE);
   1731       1.1  dyoung 
   1732       1.1  dyoung 	ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr);
   1733       1.1  dyoung 	ath_hal_txstart(ah, sc->sc_bhalq);
   1734       1.2  dyoung 	DPRINTF2(("%s: BCDP%u = %p (%p)\n", __func__,
   1735       1.1  dyoung 		sc->sc_bhalq, (caddr_t)bf->bf_daddr, bf->bf_desc));
   1736       1.1  dyoung }
   1737       1.1  dyoung 
   1738       1.1  dyoung static void
   1739       1.1  dyoung ath_beacon_free(struct ath_softc *sc)
   1740       1.1  dyoung {
   1741       1.1  dyoung 	struct ath_buf *bf = sc->sc_bcbuf;
   1742       1.1  dyoung 
   1743       1.1  dyoung 	if (bf->bf_m != NULL) {
   1744       1.1  dyoung 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
   1745       1.1  dyoung 		m_freem(bf->bf_m);
   1746       1.1  dyoung 		bf->bf_m = NULL;
   1747       1.1  dyoung 		bf->bf_node = NULL;
   1748       1.1  dyoung 	}
   1749       1.1  dyoung }
   1750       1.1  dyoung 
   1751       1.1  dyoung /*
   1752       1.1  dyoung  * Configure the beacon and sleep timers.
   1753       1.1  dyoung  *
   1754       1.1  dyoung  * When operating as an AP this resets the TSF and sets
   1755       1.1  dyoung  * up the hardware to notify us when we need to issue beacons.
   1756       1.1  dyoung  *
   1757       1.1  dyoung  * When operating in station mode this sets up the beacon
   1758       1.1  dyoung  * timers according to the timestamp of the last received
   1759       1.1  dyoung  * beacon and the current TSF, configures PCF and DTIM
   1760       1.1  dyoung  * handling, programs the sleep registers so the hardware
   1761       1.1  dyoung  * will wakeup in time to receive beacons, and configures
   1762       1.1  dyoung  * the beacon miss handling so we'll receive a BMISS
   1763       1.1  dyoung  * interrupt when we stop seeing beacons from the AP
   1764       1.1  dyoung  * we've associated with.
   1765       1.1  dyoung  */
   1766       1.1  dyoung static void
   1767       1.1  dyoung ath_beacon_config(struct ath_softc *sc)
   1768       1.1  dyoung {
   1769       1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   1770       1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   1771       1.1  dyoung 	struct ieee80211_node *ni = ic->ic_bss;
   1772       1.1  dyoung 	u_int32_t nexttbtt;
   1773       1.1  dyoung 
   1774       1.1  dyoung 	nexttbtt = (LE_READ_4(ni->ni_tstamp + 4) << 22) |
   1775       1.1  dyoung 	    (LE_READ_4(ni->ni_tstamp) >> 10);
   1776       1.1  dyoung 	DPRINTF(("%s: nexttbtt=%u\n", __func__, nexttbtt));
   1777       1.1  dyoung 	nexttbtt += ni->ni_intval;
   1778       1.1  dyoung 	if (ic->ic_opmode == IEEE80211_M_STA) {
   1779       1.1  dyoung 		HAL_BEACON_STATE bs;
   1780       1.1  dyoung 		u_int32_t bmisstime;
   1781       1.1  dyoung 
   1782       1.1  dyoung 		/* NB: no PCF support right now */
   1783       1.1  dyoung 		memset(&bs, 0, sizeof(bs));
   1784       1.1  dyoung 		bs.bs_intval = ni->ni_intval;
   1785       1.1  dyoung 		bs.bs_nexttbtt = nexttbtt;
   1786       1.1  dyoung 		bs.bs_dtimperiod = bs.bs_intval;
   1787       1.1  dyoung 		bs.bs_nextdtim = nexttbtt;
   1788       1.1  dyoung 		/*
   1789       1.1  dyoung 		 * Calculate the number of consecutive beacons to miss
   1790       1.1  dyoung 		 * before taking a BMISS interrupt.  The configuration
   1791       1.1  dyoung 		 * is specified in ms, so we need to convert that to
   1792       1.1  dyoung 		 * TU's and then calculate based on the beacon interval.
   1793       1.1  dyoung 		 * Note that we clamp the result to at most 10 beacons.
   1794       1.1  dyoung 		 */
   1795       1.1  dyoung 		bmisstime = (ic->ic_bmisstimeout * 1000) / 1024;
   1796       1.1  dyoung 		bs.bs_bmissthreshold = howmany(bmisstime,ni->ni_intval);
   1797       1.1  dyoung 		if (bs.bs_bmissthreshold > 10)
   1798       1.1  dyoung 			bs.bs_bmissthreshold = 10;
   1799       1.1  dyoung 		else if (bs.bs_bmissthreshold <= 0)
   1800       1.1  dyoung 			bs.bs_bmissthreshold = 1;
   1801       1.1  dyoung 
   1802       1.1  dyoung 		/*
   1803       1.1  dyoung 		 * Calculate sleep duration.  The configuration is
   1804       1.1  dyoung 		 * given in ms.  We insure a multiple of the beacon
   1805       1.1  dyoung 		 * period is used.  Also, if the sleep duration is
   1806       1.1  dyoung 		 * greater than the DTIM period then it makes senses
   1807       1.1  dyoung 		 * to make it a multiple of that.
   1808       1.1  dyoung 		 *
   1809       1.1  dyoung 		 * XXX fixed at 100ms
   1810       1.1  dyoung 		 */
   1811       1.1  dyoung 		bs.bs_sleepduration =
   1812       1.1  dyoung 			roundup((100 * 1000) / 1024, bs.bs_intval);
   1813       1.1  dyoung 		if (bs.bs_sleepduration > bs.bs_dtimperiod)
   1814       1.1  dyoung 			bs.bs_sleepduration = roundup(bs.bs_sleepduration, bs.bs_dtimperiod);
   1815       1.1  dyoung 
   1816       1.1  dyoung 		DPRINTF(("%s: intval %u nexttbtt %u dtim %u nextdtim %u bmiss %u sleep %u\n"
   1817       1.1  dyoung 			, __func__
   1818       1.1  dyoung 			, bs.bs_intval
   1819       1.1  dyoung 			, bs.bs_nexttbtt
   1820       1.1  dyoung 			, bs.bs_dtimperiod
   1821       1.1  dyoung 			, bs.bs_nextdtim
   1822       1.1  dyoung 			, bs.bs_bmissthreshold
   1823       1.1  dyoung 			, bs.bs_sleepduration
   1824       1.1  dyoung 		));
   1825       1.1  dyoung 		ath_hal_intrset(ah, 0);
   1826       1.1  dyoung 		/*
   1827       1.1  dyoung 		 * Reset our tsf so the hardware will update the
   1828       1.1  dyoung 		 * tsf register to reflect timestamps found in
   1829       1.1  dyoung 		 * received beacons.
   1830       1.1  dyoung 		 */
   1831       1.1  dyoung 		ath_hal_resettsf(ah);
   1832       1.1  dyoung 		ath_hal_beacontimers(ah, &bs, 0/*XXX*/, 0, 0);
   1833       1.1  dyoung 		sc->sc_imask |= HAL_INT_BMISS;
   1834       1.1  dyoung 		ath_hal_intrset(ah, sc->sc_imask);
   1835       1.1  dyoung 	} else {
   1836       1.1  dyoung 		DPRINTF(("%s: intval %u nexttbtt %u\n",
   1837       1.1  dyoung 			__func__, ni->ni_intval, nexttbtt));
   1838       1.1  dyoung 		ath_hal_intrset(ah, 0);
   1839      1.19  dyoung 		ath_hal_beaconinit(ah, nexttbtt, ni->ni_intval);
   1840       1.1  dyoung 		if (ic->ic_opmode != IEEE80211_M_MONITOR)
   1841       1.1  dyoung 			sc->sc_imask |= HAL_INT_SWBA;	/* beacon prepare */
   1842       1.1  dyoung 		ath_hal_intrset(ah, sc->sc_imask);
   1843       1.1  dyoung 	}
   1844       1.1  dyoung }
   1845       1.1  dyoung 
   1846       1.2  dyoung #ifdef __FreeBSD__
   1847       1.1  dyoung static void
   1848       1.1  dyoung ath_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
   1849       1.1  dyoung {
   1850       1.1  dyoung 	bus_addr_t *paddr = (bus_addr_t*) arg;
   1851       1.1  dyoung 	*paddr = segs->ds_addr;
   1852       1.1  dyoung }
   1853       1.2  dyoung #endif
   1854       1.1  dyoung 
   1855       1.2  dyoung #ifdef __FreeBSD__
   1856       1.1  dyoung static int
   1857       1.1  dyoung ath_desc_alloc(struct ath_softc *sc)
   1858       1.1  dyoung {
   1859       1.1  dyoung 	int i, bsize, error;
   1860       1.1  dyoung 	struct ath_desc *ds;
   1861       1.1  dyoung 	struct ath_buf *bf;
   1862       1.1  dyoung 
   1863       1.1  dyoung 	/* allocate descriptors */
   1864       1.1  dyoung 	sc->sc_desc_len = sizeof(struct ath_desc) *
   1865       1.1  dyoung 				(ATH_TXBUF * ATH_TXDESC + ATH_RXBUF + 1);
   1866       1.1  dyoung 	error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT, &sc->sc_ddmamap);
   1867       1.1  dyoung 	if (error != 0)
   1868       1.1  dyoung 		return error;
   1869       1.1  dyoung 
   1870       1.1  dyoung 	error = bus_dmamem_alloc(sc->sc_dmat, (void**) &sc->sc_desc,
   1871       1.1  dyoung 				 BUS_DMA_NOWAIT, &sc->sc_ddmamap);
   1872       1.2  dyoung 
   1873       1.1  dyoung 	if (error != 0)
   1874       1.1  dyoung 		goto fail0;
   1875       1.1  dyoung 
   1876       1.1  dyoung 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_ddmamap,
   1877       1.1  dyoung 				sc->sc_desc, sc->sc_desc_len,
   1878       1.1  dyoung 				ath_load_cb, &sc->sc_desc_paddr,
   1879       1.1  dyoung 				BUS_DMA_NOWAIT);
   1880       1.1  dyoung 	if (error != 0)
   1881       1.1  dyoung 		goto fail1;
   1882       1.1  dyoung 
   1883       1.1  dyoung 	ds = sc->sc_desc;
   1884       1.1  dyoung 	DPRINTF(("ath_desc_alloc: DMA map: %p (%d) -> %p (%lu)\n",
   1885       1.1  dyoung 	    ds, sc->sc_desc_len,
   1886       1.1  dyoung 	    (caddr_t) sc->sc_desc_paddr, /*XXX*/ (u_long) sc->sc_desc_len));
   1887       1.1  dyoung 
   1888       1.1  dyoung 	/* allocate buffers */
   1889       1.1  dyoung 	bsize = sizeof(struct ath_buf) * (ATH_TXBUF + ATH_RXBUF + 1);
   1890       1.1  dyoung 	bf = malloc(bsize, M_DEVBUF, M_NOWAIT | M_ZERO);
   1891       1.2  dyoung 	if (bf == NULL) {
   1892       1.2  dyoung 		printf("%s: unable to allocate Tx/Rx buffers\n",
   1893       1.2  dyoung 		    sc->sc_dev.dv_xname);
   1894       1.2  dyoung 		error = -1;
   1895       1.1  dyoung 		goto fail2;
   1896       1.2  dyoung 	}
   1897       1.1  dyoung 	sc->sc_bufptr = bf;
   1898       1.1  dyoung 
   1899       1.1  dyoung 	TAILQ_INIT(&sc->sc_rxbuf);
   1900       1.1  dyoung 	for (i = 0; i < ATH_RXBUF; i++, bf++, ds++) {
   1901       1.1  dyoung 		bf->bf_desc = ds;
   1902       1.1  dyoung 		bf->bf_daddr = sc->sc_desc_paddr +
   1903       1.1  dyoung 		    ((caddr_t)ds - (caddr_t)sc->sc_desc);
   1904       1.1  dyoung 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
   1905       1.1  dyoung 					  &bf->bf_dmamap);
   1906       1.1  dyoung 		if (error != 0)
   1907       1.1  dyoung 			break;
   1908       1.1  dyoung 		TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
   1909       1.1  dyoung 	}
   1910       1.1  dyoung 
   1911       1.1  dyoung 	TAILQ_INIT(&sc->sc_txbuf);
   1912       1.1  dyoung 	for (i = 0; i < ATH_TXBUF; i++, bf++, ds += ATH_TXDESC) {
   1913       1.1  dyoung 		bf->bf_desc = ds;
   1914       1.1  dyoung 		bf->bf_daddr = sc->sc_desc_paddr +
   1915       1.1  dyoung 		    ((caddr_t)ds - (caddr_t)sc->sc_desc);
   1916       1.1  dyoung 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
   1917       1.1  dyoung 					  &bf->bf_dmamap);
   1918       1.1  dyoung 		if (error != 0)
   1919       1.1  dyoung 			break;
   1920       1.1  dyoung 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
   1921       1.1  dyoung 	}
   1922       1.1  dyoung 	TAILQ_INIT(&sc->sc_txq);
   1923       1.1  dyoung 
   1924       1.1  dyoung 	/* beacon buffer */
   1925       1.1  dyoung 	bf->bf_desc = ds;
   1926       1.1  dyoung 	bf->bf_daddr = sc->sc_desc_paddr + ((caddr_t)ds - (caddr_t)sc->sc_desc);
   1927       1.1  dyoung 	error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT, &bf->bf_dmamap);
   1928       1.1  dyoung 	if (error != 0)
   1929       1.1  dyoung 		return error;
   1930       1.1  dyoung 	sc->sc_bcbuf = bf;
   1931       1.1  dyoung 	return 0;
   1932       1.1  dyoung 
   1933       1.1  dyoung fail2:
   1934       1.1  dyoung 	bus_dmamap_unload(sc->sc_dmat, sc->sc_ddmamap);
   1935       1.1  dyoung fail1:
   1936       1.1  dyoung 	bus_dmamem_free(sc->sc_dmat, sc->sc_desc, sc->sc_ddmamap);
   1937       1.1  dyoung fail0:
   1938       1.1  dyoung 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_ddmamap);
   1939       1.1  dyoung 	sc->sc_ddmamap = NULL;
   1940       1.1  dyoung 	return error;
   1941       1.1  dyoung }
   1942       1.2  dyoung #else
   1943       1.2  dyoung static int
   1944       1.2  dyoung ath_desc_alloc(struct ath_softc *sc)
   1945       1.2  dyoung {
   1946       1.2  dyoung 	int i, bsize, error = -1;
   1947       1.2  dyoung 	struct ath_desc *ds;
   1948       1.2  dyoung 	struct ath_buf *bf;
   1949       1.2  dyoung 
   1950       1.2  dyoung 	/* allocate descriptors */
   1951       1.2  dyoung 	sc->sc_desc_len = sizeof(struct ath_desc) *
   1952       1.2  dyoung 				(ATH_TXBUF * ATH_TXDESC + ATH_RXBUF + 1);
   1953       1.2  dyoung 	if ((error = bus_dmamem_alloc(sc->sc_dmat, sc->sc_desc_len, PAGE_SIZE,
   1954       1.2  dyoung 	    0, &sc->sc_dseg, 1, &sc->sc_dnseg, 0)) != 0) {
   1955       1.2  dyoung 		printf("%s: unable to allocate control data, error = %d\n",
   1956       1.2  dyoung 		    sc->sc_dev.dv_xname, error);
   1957       1.2  dyoung 		goto fail0;
   1958       1.2  dyoung 	}
   1959       1.2  dyoung 
   1960       1.2  dyoung 	if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_dseg, sc->sc_dnseg,
   1961       1.2  dyoung 	    sc->sc_desc_len, (caddr_t *)&sc->sc_desc, BUS_DMA_COHERENT)) != 0) {
   1962       1.2  dyoung 		printf("%s: unable to map control data, error = %d\n",
   1963       1.2  dyoung 		    sc->sc_dev.dv_xname, error);
   1964       1.2  dyoung 		goto fail1;
   1965       1.2  dyoung 	}
   1966       1.2  dyoung 
   1967       1.2  dyoung 	if ((error = bus_dmamap_create(sc->sc_dmat, sc->sc_desc_len, 1,
   1968       1.2  dyoung 	    sc->sc_desc_len, 0, 0, &sc->sc_ddmamap)) != 0) {
   1969       1.2  dyoung 		printf("%s: unable to create control data DMA map, "
   1970       1.2  dyoung 		    "error = %d\n", sc->sc_dev.dv_xname, error);
   1971       1.2  dyoung 		goto fail2;
   1972       1.2  dyoung 	}
   1973       1.2  dyoung 
   1974       1.2  dyoung 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_ddmamap, sc->sc_desc,
   1975       1.2  dyoung 	    sc->sc_desc_len, NULL, 0)) != 0) {
   1976       1.2  dyoung 		printf("%s: unable to load control data DMA map, error = %d\n",
   1977       1.2  dyoung 		    sc->sc_dev.dv_xname, error);
   1978       1.2  dyoung 		goto fail3;
   1979       1.2  dyoung 	}
   1980       1.2  dyoung 
   1981       1.2  dyoung 	ds = sc->sc_desc;
   1982       1.2  dyoung 	sc->sc_desc_paddr = sc->sc_ddmamap->dm_segs[0].ds_addr;
   1983       1.2  dyoung 
   1984       1.2  dyoung 	DPRINTF(("ath_desc_alloc: DMA map: %p (%lu) -> %p (%lu)\n",
   1985       1.2  dyoung 	    ds, (u_long)sc->sc_desc_len,
   1986       1.2  dyoung 	    (caddr_t) sc->sc_desc_paddr, /*XXX*/ (u_long) sc->sc_desc_len));
   1987       1.2  dyoung 
   1988       1.2  dyoung 	/* allocate buffers */
   1989       1.2  dyoung 	bsize = sizeof(struct ath_buf) * (ATH_TXBUF + ATH_RXBUF + 1);
   1990       1.2  dyoung 	bf = malloc(bsize, M_DEVBUF, M_NOWAIT | M_ZERO);
   1991       1.2  dyoung 	if (bf == NULL) {
   1992       1.2  dyoung 		printf("%s: unable to allocate Tx/Rx buffers\n",
   1993       1.2  dyoung 		    sc->sc_dev.dv_xname);
   1994       1.2  dyoung 		error = ENOMEM;
   1995       1.2  dyoung 		goto fail3;
   1996       1.2  dyoung 	}
   1997       1.2  dyoung 	sc->sc_bufptr = bf;
   1998       1.2  dyoung 
   1999       1.2  dyoung 	TAILQ_INIT(&sc->sc_rxbuf);
   2000       1.2  dyoung 	for (i = 0; i < ATH_RXBUF; i++, bf++, ds++) {
   2001       1.2  dyoung 		bf->bf_desc = ds;
   2002       1.2  dyoung 		bf->bf_daddr = sc->sc_desc_paddr +
   2003       1.2  dyoung 		    ((caddr_t)ds - (caddr_t)sc->sc_desc);
   2004       1.2  dyoung 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
   2005       1.2  dyoung 		    MCLBYTES, 0, 0, &bf->bf_dmamap)) != 0) {
   2006       1.2  dyoung 			printf("%s: unable to create Rx dmamap, error = %d\n",
   2007       1.2  dyoung 			    sc->sc_dev.dv_xname, error);
   2008       1.2  dyoung 			goto fail4;
   2009       1.2  dyoung 		}
   2010       1.2  dyoung 		TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
   2011       1.2  dyoung 	}
   2012       1.2  dyoung 
   2013       1.2  dyoung 	TAILQ_INIT(&sc->sc_txbuf);
   2014       1.2  dyoung 	for (i = 0; i < ATH_TXBUF; i++, bf++, ds += ATH_TXDESC) {
   2015       1.2  dyoung 		bf->bf_desc = ds;
   2016       1.2  dyoung 		bf->bf_daddr = sc->sc_desc_paddr +
   2017       1.2  dyoung 		    ((caddr_t)ds - (caddr_t)sc->sc_desc);
   2018       1.2  dyoung 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
   2019       1.2  dyoung 		    ATH_TXDESC, MCLBYTES, 0, 0, &bf->bf_dmamap)) != 0) {
   2020       1.2  dyoung 			printf("%s: unable to create Tx dmamap, error = %d\n",
   2021       1.2  dyoung 			    sc->sc_dev.dv_xname, error);
   2022       1.2  dyoung 			goto fail5;
   2023       1.2  dyoung 		}
   2024       1.2  dyoung 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
   2025       1.2  dyoung 	}
   2026       1.2  dyoung 	TAILQ_INIT(&sc->sc_txq);
   2027       1.2  dyoung 
   2028       1.2  dyoung 	/* beacon buffer */
   2029       1.2  dyoung 	bf->bf_desc = ds;
   2030       1.2  dyoung 	bf->bf_daddr = sc->sc_desc_paddr + ((caddr_t)ds - (caddr_t)sc->sc_desc);
   2031       1.2  dyoung 	if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, 0, 0,
   2032       1.2  dyoung 	    &bf->bf_dmamap)) != 0) {
   2033       1.2  dyoung 		printf("%s: unable to create beacon dmamap, error = %d\n",
   2034       1.2  dyoung 		    sc->sc_dev.dv_xname, error);
   2035       1.2  dyoung 		goto fail5;
   2036       1.2  dyoung 	}
   2037       1.2  dyoung 	sc->sc_bcbuf = bf;
   2038       1.2  dyoung 	return 0;
   2039       1.2  dyoung 
   2040       1.2  dyoung fail5:
   2041       1.2  dyoung 	for (i = ATH_RXBUF; i < ATH_RXBUF + ATH_TXBUF; i++) {
   2042       1.2  dyoung 		if (sc->sc_bufptr[i].bf_dmamap == NULL)
   2043       1.2  dyoung 			continue;
   2044       1.2  dyoung 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_bufptr[i].bf_dmamap);
   2045       1.2  dyoung 	}
   2046       1.2  dyoung fail4:
   2047       1.2  dyoung 	for (i = 0; i < ATH_RXBUF; i++) {
   2048       1.2  dyoung 		if (sc->sc_bufptr[i].bf_dmamap == NULL)
   2049       1.2  dyoung 			continue;
   2050       1.2  dyoung 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_bufptr[i].bf_dmamap);
   2051       1.2  dyoung 	}
   2052       1.2  dyoung fail3:
   2053       1.2  dyoung 	bus_dmamap_unload(sc->sc_dmat, sc->sc_ddmamap);
   2054       1.2  dyoung fail2:
   2055       1.2  dyoung 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_ddmamap);
   2056       1.2  dyoung 	sc->sc_ddmamap = NULL;
   2057       1.2  dyoung fail1:
   2058       1.2  dyoung 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_desc, sc->sc_desc_len);
   2059       1.2  dyoung fail0:
   2060       1.2  dyoung 	bus_dmamem_free(sc->sc_dmat, &sc->sc_dseg, sc->sc_dnseg);
   2061       1.2  dyoung 	return error;
   2062       1.2  dyoung }
   2063       1.2  dyoung #endif
   2064       1.1  dyoung 
   2065       1.1  dyoung static void
   2066       1.1  dyoung ath_desc_free(struct ath_softc *sc)
   2067       1.1  dyoung {
   2068       1.1  dyoung 	struct ath_buf *bf;
   2069       1.1  dyoung 
   2070       1.2  dyoung #ifdef __FreeBSD__
   2071       1.1  dyoung 	bus_dmamap_unload(sc->sc_dmat, sc->sc_ddmamap);
   2072       1.1  dyoung 	bus_dmamem_free(sc->sc_dmat, sc->sc_desc, sc->sc_ddmamap);
   2073       1.1  dyoung 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_ddmamap);
   2074       1.2  dyoung #else
   2075       1.2  dyoung 	bus_dmamap_unload(sc->sc_dmat, sc->sc_ddmamap);
   2076       1.2  dyoung 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_ddmamap);
   2077       1.2  dyoung 	bus_dmamem_free(sc->sc_dmat, &sc->sc_dseg, sc->sc_dnseg);
   2078       1.2  dyoung #endif
   2079       1.1  dyoung 
   2080       1.1  dyoung 	TAILQ_FOREACH(bf, &sc->sc_txq, bf_list) {
   2081       1.1  dyoung 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
   2082       1.1  dyoung 		bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
   2083       1.1  dyoung 		m_freem(bf->bf_m);
   2084       1.1  dyoung 	}
   2085       1.1  dyoung 	TAILQ_FOREACH(bf, &sc->sc_txbuf, bf_list)
   2086       1.1  dyoung 		bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
   2087       1.1  dyoung 	TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
   2088       1.1  dyoung 		if (bf->bf_m) {
   2089       1.1  dyoung 			bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
   2090       1.1  dyoung 			bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
   2091       1.1  dyoung 			m_freem(bf->bf_m);
   2092       1.1  dyoung 			bf->bf_m = NULL;
   2093       1.1  dyoung 		}
   2094       1.1  dyoung 	}
   2095       1.1  dyoung 	if (sc->sc_bcbuf != NULL) {
   2096       1.1  dyoung 		bus_dmamap_unload(sc->sc_dmat, sc->sc_bcbuf->bf_dmamap);
   2097       1.1  dyoung 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_bcbuf->bf_dmamap);
   2098       1.1  dyoung 		sc->sc_bcbuf = NULL;
   2099       1.1  dyoung 	}
   2100       1.1  dyoung 
   2101       1.1  dyoung 	TAILQ_INIT(&sc->sc_rxbuf);
   2102       1.1  dyoung 	TAILQ_INIT(&sc->sc_txbuf);
   2103       1.1  dyoung 	TAILQ_INIT(&sc->sc_txq);
   2104       1.1  dyoung 	free(sc->sc_bufptr, M_DEVBUF);
   2105       1.1  dyoung 	sc->sc_bufptr = NULL;
   2106       1.1  dyoung }
   2107       1.1  dyoung 
   2108       1.1  dyoung static struct ieee80211_node *
   2109       1.1  dyoung ath_node_alloc(struct ieee80211com *ic)
   2110       1.1  dyoung {
   2111       1.1  dyoung 	struct ath_node *an =
   2112       1.1  dyoung 		malloc(sizeof(struct ath_node), M_DEVBUF, M_NOWAIT | M_ZERO);
   2113      1.18  dyoung 	if (an) {
   2114      1.18  dyoung 		int i;
   2115      1.18  dyoung 		for (i = 0; i < ATH_RHIST_SIZE; i++)
   2116      1.18  dyoung 			an->an_rx_hist[i].arh_ticks = ATH_RHIST_NOTIME;
   2117      1.18  dyoung 		an->an_rx_hist_next = ATH_RHIST_SIZE-1;
   2118      1.18  dyoung 		return &an->an_node;
   2119      1.18  dyoung 	} else
   2120      1.18  dyoung 		return NULL;
   2121       1.1  dyoung }
   2122       1.1  dyoung 
   2123       1.1  dyoung static void
   2124       1.1  dyoung ath_node_free(struct ieee80211com *ic, struct ieee80211_node *ni)
   2125       1.1  dyoung {
   2126       1.2  dyoung 	struct ath_softc *sc = ic->ic_if.if_softc;
   2127       1.1  dyoung 	struct ath_buf *bf;
   2128       1.1  dyoung 
   2129       1.1  dyoung 	TAILQ_FOREACH(bf, &sc->sc_txq, bf_list) {
   2130       1.1  dyoung 		if (bf->bf_node == ni)
   2131       1.1  dyoung 			bf->bf_node = NULL;
   2132       1.1  dyoung 	}
   2133       1.1  dyoung 	free(ni, M_DEVBUF);
   2134       1.1  dyoung }
   2135       1.1  dyoung 
   2136       1.1  dyoung static void
   2137       1.1  dyoung ath_node_copy(struct ieee80211com *ic,
   2138       1.1  dyoung 	struct ieee80211_node *dst, const struct ieee80211_node *src)
   2139       1.1  dyoung {
   2140       1.1  dyoung 	*(struct ath_node *)dst = *(const struct ath_node *)src;
   2141       1.1  dyoung }
   2142       1.1  dyoung 
   2143      1.18  dyoung static u_int8_t
   2144      1.18  dyoung ath_node_getrssi(struct ieee80211com *ic, struct ieee80211_node *ni)
   2145      1.18  dyoung {
   2146      1.18  dyoung 	struct ath_node *an = ATH_NODE(ni);
   2147      1.18  dyoung 	int i, now, nsamples, rssi;
   2148      1.18  dyoung 
   2149      1.18  dyoung 	/*
   2150      1.18  dyoung 	 * Calculate the average over the last second of sampled data.
   2151      1.18  dyoung 	 */
   2152      1.18  dyoung 	now = ATH_TICKS();
   2153      1.18  dyoung 	nsamples = 0;
   2154      1.18  dyoung 	rssi = 0;
   2155      1.18  dyoung 	i = an->an_rx_hist_next;
   2156      1.18  dyoung 	do {
   2157      1.18  dyoung 		struct ath_recv_hist *rh = &an->an_rx_hist[i];
   2158      1.18  dyoung 		if (rh->arh_ticks == ATH_RHIST_NOTIME)
   2159      1.18  dyoung 			goto done;
   2160      1.18  dyoung 		if (now - rh->arh_ticks > hz)
   2161      1.18  dyoung 			goto done;
   2162      1.18  dyoung 		rssi += rh->arh_rssi;
   2163      1.18  dyoung 		nsamples++;
   2164      1.18  dyoung 		if (i == 0)
   2165      1.18  dyoung 			i = ATH_RHIST_SIZE-1;
   2166      1.18  dyoung 		else
   2167      1.18  dyoung 			i--;
   2168      1.18  dyoung 	} while (i != an->an_rx_hist_next);
   2169      1.18  dyoung done:
   2170      1.18  dyoung 	/*
   2171      1.18  dyoung 	 * Return either the average or the last known
   2172      1.18  dyoung 	 * value if there is no recent data.
   2173      1.18  dyoung 	 */
   2174      1.18  dyoung 	return (nsamples ? rssi / nsamples : an->an_rx_hist[i].arh_rssi);
   2175      1.18  dyoung }
   2176      1.18  dyoung 
   2177       1.1  dyoung static int
   2178       1.1  dyoung ath_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf)
   2179       1.1  dyoung {
   2180       1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   2181       1.1  dyoung 	int error;
   2182       1.1  dyoung 	struct mbuf *m;
   2183       1.1  dyoung 	struct ath_desc *ds;
   2184       1.1  dyoung 
   2185       1.1  dyoung 	m = bf->bf_m;
   2186       1.1  dyoung 	if (m == NULL) {
   2187       1.1  dyoung 		/*
   2188       1.1  dyoung 		 * NB: by assigning a page to the rx dma buffer we
   2189       1.1  dyoung 		 * implicitly satisfy the Atheros requirement that
   2190       1.1  dyoung 		 * this buffer be cache-line-aligned and sized to be
   2191       1.1  dyoung 		 * multiple of the cache line size.  Not doing this
   2192       1.1  dyoung 		 * causes weird stuff to happen (for the 5210 at least).
   2193       1.1  dyoung 		 */
   2194       1.2  dyoung 		m = ath_getmbuf(M_DONTWAIT, MT_DATA, MCLBYTES);
   2195       1.1  dyoung 		if (m == NULL) {
   2196       1.1  dyoung 			DPRINTF(("ath_rxbuf_init: no mbuf/cluster\n"));
   2197       1.1  dyoung 			sc->sc_stats.ast_rx_nombuf++;
   2198       1.1  dyoung 			return ENOMEM;
   2199       1.1  dyoung 		}
   2200       1.1  dyoung 		bf->bf_m = m;
   2201       1.1  dyoung 		m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
   2202       1.1  dyoung 
   2203       1.2  dyoung 		error = ath_buf_dmamap_load_mbuf(sc->sc_dmat, bf, m,
   2204       1.2  dyoung 		                                 BUS_DMA_NOWAIT);
   2205       1.1  dyoung 		if (error != 0) {
   2206       1.2  dyoung 			DPRINTF(("ath_rxbuf_init: ath_buf_dmamap_load_mbuf failed;"
   2207       1.1  dyoung 				" error %d\n", error));
   2208       1.1  dyoung 			sc->sc_stats.ast_rx_busdma++;
   2209       1.1  dyoung 			return error;
   2210       1.1  dyoung 		}
   2211       1.1  dyoung 		KASSERT(bf->bf_nseg == 1,
   2212       1.1  dyoung 			("ath_rxbuf_init: multi-segment packet; nseg %u",
   2213       1.1  dyoung 			bf->bf_nseg));
   2214       1.1  dyoung 	}
   2215       1.2  dyoung 	ath_buf_dmamap_sync(sc->sc_dmat, bf, BUS_DMASYNC_PREREAD);
   2216       1.1  dyoung 
   2217      1.18  dyoung 	/*
   2218      1.18  dyoung 	 * Setup descriptors.  For receive we always terminate
   2219      1.18  dyoung 	 * the descriptor list with a self-linked entry so we'll
   2220      1.18  dyoung 	 * not get overrun under high load (as can happen with a
   2221      1.18  dyoung 	 * 5212 when ANI processing enables PHY errors).
   2222      1.18  dyoung 	 *
   2223      1.18  dyoung 	 * To insure the last descriptor is self-linked we create
   2224      1.18  dyoung 	 * each descriptor as self-linked and add it to the end.  As
   2225      1.18  dyoung 	 * each additional descriptor is added the previous self-linked
   2226      1.18  dyoung 	 * entry is ``fixed'' naturally.  This should be safe even
   2227      1.18  dyoung 	 * if DMA is happening.  When processing RX interrupts we
   2228      1.18  dyoung 	 * never remove/process the last, self-linked, entry on the
   2229      1.18  dyoung 	 * descriptor list.  This insures the hardware always has
   2230      1.18  dyoung 	 * someplace to write a new frame.
   2231      1.18  dyoung 	 */
   2232       1.1  dyoung 	ds = bf->bf_desc;
   2233      1.18  dyoung 	ds->ds_link = bf->bf_daddr;	/* link to self */
   2234       1.1  dyoung 	ds->ds_data = bf->bf_segs[0].ds_addr;
   2235       1.1  dyoung 	ath_hal_setuprxdesc(ah, ds
   2236       1.1  dyoung 		, m->m_len		/* buffer size */
   2237       1.1  dyoung 		, 0
   2238       1.1  dyoung 	);
   2239       1.1  dyoung 
   2240       1.1  dyoung 	if (sc->sc_rxlink != NULL)
   2241       1.1  dyoung 		*sc->sc_rxlink = bf->bf_daddr;
   2242       1.1  dyoung 	sc->sc_rxlink = &ds->ds_link;
   2243       1.1  dyoung 	return 0;
   2244       1.1  dyoung }
   2245       1.1  dyoung 
   2246       1.1  dyoung static void
   2247       1.1  dyoung ath_rx_proc(void *arg, int npending)
   2248       1.1  dyoung {
   2249      1.18  dyoung #define	PA2DESC(_sc, _pa) \
   2250      1.18  dyoung 	((struct ath_desc *)((caddr_t)(_sc)->sc_desc + \
   2251      1.18  dyoung 		((_pa) - (_sc)->sc_desc_paddr)))
   2252       1.1  dyoung 	struct ath_softc *sc = arg;
   2253       1.1  dyoung 	struct ath_buf *bf;
   2254       1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   2255       1.1  dyoung 	struct ifnet *ifp = &ic->ic_if;
   2256       1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   2257       1.1  dyoung 	struct ath_desc *ds;
   2258       1.1  dyoung 	struct mbuf *m;
   2259       1.1  dyoung 	struct ieee80211_frame *wh, whbuf;
   2260       1.1  dyoung 	struct ieee80211_node *ni;
   2261      1.18  dyoung 	struct ath_node *an;
   2262      1.18  dyoung 	struct ath_recv_hist *rh;
   2263       1.1  dyoung 	int len;
   2264       1.1  dyoung 	u_int phyerr;
   2265       1.1  dyoung 	HAL_STATUS status;
   2266       1.1  dyoung 
   2267       1.1  dyoung 	DPRINTF2(("ath_rx_proc: pending %u\n", npending));
   2268       1.1  dyoung 	do {
   2269       1.1  dyoung 		bf = TAILQ_FIRST(&sc->sc_rxbuf);
   2270       1.1  dyoung 		if (bf == NULL) {		/* NB: shouldn't happen */
   2271       1.1  dyoung 			if_printf(ifp, "ath_rx_proc: no buffer!\n");
   2272       1.1  dyoung 			break;
   2273       1.1  dyoung 		}
   2274      1.18  dyoung 		ds = bf->bf_desc;
   2275      1.18  dyoung 		if (ds->ds_link == bf->bf_daddr) {
   2276      1.18  dyoung 			/* NB: never process the self-linked entry at the end */
   2277      1.18  dyoung 			break;
   2278      1.18  dyoung 		}
   2279       1.1  dyoung 		m = bf->bf_m;
   2280       1.1  dyoung 		if (m == NULL) {		/* NB: shouldn't happen */
   2281       1.1  dyoung 			if_printf(ifp, "ath_rx_proc: no mbuf!\n");
   2282       1.1  dyoung 			continue;
   2283       1.1  dyoung 		}
   2284      1.18  dyoung 		/* XXX sync descriptor memory */
   2285      1.18  dyoung 		/*
   2286      1.18  dyoung 		 * Must provide the virtual address of the current
   2287      1.18  dyoung 		 * descriptor, the physical address, and the virtual
   2288      1.18  dyoung 		 * address of the next descriptor in the h/w chain.
   2289      1.18  dyoung 		 * This allows the HAL to look ahead to see if the
   2290      1.18  dyoung 		 * hardware is done with a descriptor by checking the
   2291      1.18  dyoung 		 * done bit in the following descriptor and the address
   2292      1.18  dyoung 		 * of the current descriptor the DMA engine is working
   2293      1.18  dyoung 		 * on.  All this is necessary because of our use of
   2294      1.18  dyoung 		 * a self-linked list to avoid rx overruns.
   2295      1.18  dyoung 		 */
   2296      1.18  dyoung 		status = ath_hal_rxprocdesc(ah, ds,
   2297      1.18  dyoung 				bf->bf_daddr, PA2DESC(sc, ds->ds_link));
   2298       1.1  dyoung #ifdef AR_DEBUG
   2299       1.1  dyoung 		if (ath_debug > 1)
   2300       1.1  dyoung 			ath_printrxbuf(bf, status == HAL_OK);
   2301       1.1  dyoung #endif
   2302       1.1  dyoung 		if (status == HAL_EINPROGRESS)
   2303       1.1  dyoung 			break;
   2304       1.1  dyoung 		TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
   2305       1.1  dyoung 		if (ds->ds_rxstat.rs_status != 0) {
   2306       1.1  dyoung 			if (ds->ds_rxstat.rs_status & HAL_RXERR_CRC)
   2307       1.1  dyoung 				sc->sc_stats.ast_rx_crcerr++;
   2308       1.1  dyoung 			if (ds->ds_rxstat.rs_status & HAL_RXERR_FIFO)
   2309       1.1  dyoung 				sc->sc_stats.ast_rx_fifoerr++;
   2310       1.1  dyoung 			if (ds->ds_rxstat.rs_status & HAL_RXERR_DECRYPT)
   2311       1.1  dyoung 				sc->sc_stats.ast_rx_badcrypt++;
   2312       1.1  dyoung 			if (ds->ds_rxstat.rs_status & HAL_RXERR_PHY) {
   2313       1.1  dyoung 				sc->sc_stats.ast_rx_phyerr++;
   2314       1.1  dyoung 				phyerr = ds->ds_rxstat.rs_phyerr & 0x1f;
   2315       1.1  dyoung 				sc->sc_stats.ast_rx_phy[phyerr]++;
   2316      1.18  dyoung 			} else {
   2317      1.18  dyoung 				/*
   2318      1.18  dyoung 				 * NB: don't count PHY errors as input errors;
   2319      1.18  dyoung 				 * we enable them on the 5212 to collect info
   2320      1.18  dyoung 				 * about environmental noise and, in that
   2321      1.18  dyoung 				 * setting, they don't really reflect tx/rx
   2322      1.18  dyoung 				 * errors.
   2323      1.18  dyoung 				 */
   2324      1.18  dyoung 				ifp->if_ierrors++;
   2325       1.1  dyoung 			}
   2326       1.1  dyoung 			goto rx_next;
   2327       1.1  dyoung 		}
   2328       1.1  dyoung 
   2329       1.1  dyoung 		len = ds->ds_rxstat.rs_datalen;
   2330      1.18  dyoung 		if (len < IEEE80211_MIN_LEN) {
   2331       1.1  dyoung 			DPRINTF(("ath_rx_proc: short packet %d\n", len));
   2332       1.1  dyoung 			sc->sc_stats.ast_rx_tooshort++;
   2333       1.1  dyoung 			goto rx_next;
   2334       1.1  dyoung 		}
   2335       1.1  dyoung 
   2336       1.2  dyoung 		ath_buf_dmamap_sync(sc->sc_dmat, bf, BUS_DMASYNC_POSTREAD);
   2337       1.1  dyoung 
   2338       1.1  dyoung 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
   2339       1.1  dyoung 		bf->bf_m = NULL;
   2340       1.1  dyoung 		m->m_pkthdr.rcvif = ifp;
   2341       1.1  dyoung 		m->m_pkthdr.len = m->m_len = len;
   2342       1.1  dyoung 
   2343       1.2  dyoung #if NBPFILTER > 0
   2344       1.1  dyoung 		if (sc->sc_drvbpf) {
   2345       1.2  dyoung #ifdef __FreeBSD__
   2346       1.1  dyoung 			struct mbuf *mb;
   2347       1.1  dyoung 
   2348       1.1  dyoung 			/* XXX pre-allocate space when setting up recv's */
   2349       1.1  dyoung 			MGETHDR(mb, M_DONTWAIT, m->m_type);
   2350       1.1  dyoung 			if (mb != NULL) {
   2351       1.1  dyoung 				sc->sc_rx_th.wr_rate =
   2352       1.1  dyoung 					sc->sc_hwmap[ds->ds_rxstat.rs_rate];
   2353       1.1  dyoung 				sc->sc_rx_th.wr_antsignal =
   2354       1.1  dyoung 					ds->ds_rxstat.rs_rssi;
   2355       1.1  dyoung 				sc->sc_rx_th.wr_antenna =
   2356       1.1  dyoung 					ds->ds_rxstat.rs_antenna;
   2357       1.1  dyoung 				/* XXX TSF */
   2358       1.1  dyoung 
   2359       1.1  dyoung 				(void) m_dup_pkthdr(mb, m, M_DONTWAIT);
   2360       1.1  dyoung 				mb->m_next = m;
   2361       1.1  dyoung 				mb->m_data = (caddr_t)&sc->sc_rx_th;
   2362       1.1  dyoung 				mb->m_len = sizeof(sc->sc_rx_th);
   2363       1.1  dyoung 				mb->m_pkthdr.len += mb->m_len;
   2364       1.1  dyoung 				bpf_mtap(sc->sc_drvbpf, mb);
   2365       1.1  dyoung 				m_free(mb);
   2366       1.1  dyoung 			}
   2367       1.2  dyoung #else
   2368       1.2  dyoung 			/* XXX pre-allocate space when setting up recv's */
   2369       1.2  dyoung 			struct mbuf mb;
   2370       1.2  dyoung 
   2371       1.2  dyoung 			sc->sc_rx_th.wr_rate =
   2372       1.2  dyoung 				sc->sc_hwmap[ds->ds_rxstat.rs_rate];
   2373       1.2  dyoung 			sc->sc_rx_th.wr_antsignal =
   2374       1.2  dyoung 				ds->ds_rxstat.rs_rssi;
   2375       1.2  dyoung 			sc->sc_rx_th.wr_antenna =
   2376       1.2  dyoung 				ds->ds_rxstat.rs_antenna;
   2377       1.2  dyoung 			/* XXX TSF */
   2378       1.2  dyoung 
   2379       1.2  dyoung 			M_COPY_PKTHDR(&mb, m);
   2380       1.2  dyoung 			mb.m_next = m;
   2381       1.2  dyoung 			mb.m_data = (caddr_t)&sc->sc_rx_th;
   2382       1.2  dyoung 			mb.m_len = sizeof(sc->sc_rx_th);
   2383       1.2  dyoung 			mb.m_pkthdr.len += mb.m_len;
   2384       1.2  dyoung 			bpf_mtap(sc->sc_drvbpf, &mb);
   2385       1.2  dyoung #endif
   2386       1.1  dyoung 		}
   2387       1.2  dyoung #endif
   2388       1.1  dyoung 
   2389       1.1  dyoung 		m_adj(m, -IEEE80211_CRC_LEN);
   2390      1.18  dyoung 		wh = mtod(m, struct ieee80211_frame *);
   2391       1.1  dyoung 		if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
   2392       1.1  dyoung 			/*
   2393       1.1  dyoung 			 * WEP is decrypted by hardware. Clear WEP bit
   2394       1.1  dyoung 			 * and trim WEP header for ieee80211_input().
   2395       1.1  dyoung 			 */
   2396       1.1  dyoung 			wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
   2397       1.1  dyoung 			memcpy(&whbuf, wh, sizeof(whbuf));
   2398       1.1  dyoung 			m_adj(m, IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN);
   2399      1.18  dyoung 			wh = mtod(m, struct ieee80211_frame *);
   2400      1.18  dyoung 			memcpy(wh, &whbuf, sizeof(whbuf));
   2401       1.1  dyoung 			/*
   2402       1.1  dyoung 			 * Also trim WEP ICV from the tail.
   2403       1.1  dyoung 			 */
   2404       1.1  dyoung 			m_adj(m, -IEEE80211_WEP_CRCLEN);
   2405       1.6  dyoung 			/*
   2406       1.6  dyoung 			 * The header has probably moved.
   2407       1.6  dyoung 			 */
   2408       1.6  dyoung 			wh = mtod(m, struct ieee80211_frame *);
   2409       1.1  dyoung 		}
   2410       1.1  dyoung 
   2411       1.1  dyoung 		/*
   2412       1.1  dyoung 		 * Locate the node for sender, track state, and
   2413       1.1  dyoung 		 * then pass this node (referenced) up to the 802.11
   2414       1.1  dyoung 		 * layer for its use.  We are required to pass
   2415       1.1  dyoung 		 * something so we fall back to ic_bss when this frame
   2416       1.1  dyoung 		 * is from an unknown sender.
   2417       1.1  dyoung 		 */
   2418      1.14  dyoung 		ni = ieee80211_find_rxnode(ic, wh);
   2419      1.18  dyoung 
   2420      1.18  dyoung 		/*
   2421      1.18  dyoung 		 * Record driver-specific state.
   2422      1.18  dyoung 		 */
   2423      1.18  dyoung 		an = ATH_NODE(ni);
   2424      1.18  dyoung 		if (++(an->an_rx_hist_next) == ATH_RHIST_SIZE)
   2425      1.18  dyoung 			an->an_rx_hist_next = 0;
   2426      1.18  dyoung 		rh = &an->an_rx_hist[an->an_rx_hist_next];
   2427      1.18  dyoung 		rh->arh_ticks = ATH_TICKS();
   2428      1.18  dyoung 		rh->arh_rssi = ds->ds_rxstat.rs_rssi;
   2429      1.18  dyoung 		rh->arh_antenna = ds->ds_rxstat.rs_antenna;
   2430      1.18  dyoung 
   2431       1.1  dyoung 		/*
   2432       1.1  dyoung 		 * Send frame up for processing.
   2433       1.1  dyoung 		 */
   2434       1.1  dyoung 		ieee80211_input(ifp, m, ni,
   2435       1.1  dyoung 			ds->ds_rxstat.rs_rssi, ds->ds_rxstat.rs_tstamp);
   2436      1.18  dyoung 
   2437       1.1  dyoung 		/*
   2438       1.1  dyoung 		 * The frame may have caused the node to be marked for
   2439       1.1  dyoung 		 * reclamation (e.g. in response to a DEAUTH message)
   2440       1.1  dyoung 		 * so use free_node here instead of unref_node.
   2441       1.1  dyoung 		 */
   2442       1.1  dyoung 		if (ni == ic->ic_bss)
   2443       1.1  dyoung 			ieee80211_unref_node(&ni);
   2444       1.1  dyoung 		else
   2445       1.1  dyoung 			ieee80211_free_node(ic, ni);
   2446       1.1  dyoung   rx_next:
   2447       1.1  dyoung 		TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
   2448       1.1  dyoung 	} while (ath_rxbuf_init(sc, bf) == 0);
   2449       1.1  dyoung 
   2450       1.1  dyoung 	ath_hal_rxmonitor(ah);			/* rx signal state monitoring */
   2451       1.1  dyoung 	ath_hal_rxena(ah);			/* in case of RXEOL */
   2452      1.16  dyoung 
   2453      1.18  dyoung #ifdef __NetBSD__
   2454      1.16  dyoung 	if ((ifp->if_flags & IFF_OACTIVE) == 0 && !IFQ_IS_EMPTY(&ifp->if_snd))
   2455      1.16  dyoung 		ath_start(ifp);
   2456      1.18  dyoung #endif /* __NetBSD__ */
   2457      1.18  dyoung #undef PA2DESC
   2458       1.1  dyoung }
   2459       1.1  dyoung 
   2460       1.1  dyoung /*
   2461       1.1  dyoung  * XXX Size of an ACK control frame in bytes.
   2462       1.1  dyoung  */
   2463       1.1  dyoung #define	IEEE80211_ACK_SIZE	(2+2+IEEE80211_ADDR_LEN+4)
   2464       1.1  dyoung 
   2465       1.1  dyoung static int
   2466       1.1  dyoung ath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni, struct ath_buf *bf,
   2467       1.1  dyoung     struct mbuf *m0)
   2468       1.1  dyoung {
   2469       1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   2470       1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   2471       1.1  dyoung 	struct ifnet *ifp = &sc->sc_ic.ic_if;
   2472       1.1  dyoung 	int i, error, iswep, hdrlen, pktlen;
   2473       1.1  dyoung 	u_int8_t rix, cix, txrate, ctsrate;
   2474       1.1  dyoung 	struct ath_desc *ds;
   2475       1.1  dyoung 	struct mbuf *m;
   2476       1.1  dyoung 	struct ieee80211_frame *wh;
   2477       1.1  dyoung 	u_int32_t iv;
   2478       1.1  dyoung 	u_int8_t *ivp;
   2479       1.1  dyoung 	u_int8_t hdrbuf[sizeof(struct ieee80211_frame) +
   2480       1.1  dyoung 	    IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN];
   2481       1.1  dyoung 	u_int subtype, flags, ctsduration, antenna;
   2482       1.1  dyoung 	HAL_PKT_TYPE atype;
   2483       1.1  dyoung 	const HAL_RATE_TABLE *rt;
   2484       1.1  dyoung 	HAL_BOOL shortPreamble;
   2485       1.1  dyoung 	struct ath_node *an;
   2486       1.2  dyoung 	ath_txq_critsect_decl(s);
   2487       1.1  dyoung 
   2488       1.1  dyoung 	wh = mtod(m0, struct ieee80211_frame *);
   2489       1.1  dyoung 	iswep = wh->i_fc[1] & IEEE80211_FC1_WEP;
   2490       1.1  dyoung 	hdrlen = sizeof(struct ieee80211_frame);
   2491       1.1  dyoung 	pktlen = m0->m_pkthdr.len;
   2492       1.1  dyoung 
   2493       1.1  dyoung 	if (iswep) {
   2494       1.1  dyoung 		memcpy(hdrbuf, mtod(m0, caddr_t), hdrlen);
   2495       1.1  dyoung 		m_adj(m0, hdrlen);
   2496       1.1  dyoung 		M_PREPEND(m0, sizeof(hdrbuf), M_DONTWAIT);
   2497       1.1  dyoung 		if (m0 == NULL) {
   2498       1.1  dyoung 			sc->sc_stats.ast_tx_nombuf++;
   2499       1.1  dyoung 			return ENOMEM;
   2500       1.1  dyoung 		}
   2501       1.1  dyoung 		ivp = hdrbuf + hdrlen;
   2502      1.12  dyoung 		wh = mtod(m0, struct ieee80211_frame *);
   2503       1.1  dyoung 		/*
   2504       1.1  dyoung 		 * XXX
   2505       1.1  dyoung 		 * IV must not duplicate during the lifetime of the key.
   2506       1.1  dyoung 		 * But no mechanism to renew keys is defined in IEEE 802.11
   2507       1.1  dyoung 		 * WEP.  And IV may be duplicated between other stations
   2508       1.1  dyoung 		 * because of the session key itself is shared.
   2509       1.1  dyoung 		 * So we use pseudo random IV for now, though it is not the
   2510       1.1  dyoung 		 * right way.
   2511       1.1  dyoung 		 */
   2512      1.18  dyoung                 iv = ic->ic_iv;
   2513      1.18  dyoung 		/*
   2514      1.18  dyoung 		 * Skip 'bad' IVs from Fluhrer/Mantin/Shamir:
   2515      1.18  dyoung 		 * (B, 255, N) with 3 <= B < 8
   2516      1.18  dyoung 		 */
   2517      1.18  dyoung 		if (iv >= 0x03ff00 && (iv & 0xf8ff00) == 0x00ff00)
   2518      1.18  dyoung 			iv += 0x000100;
   2519      1.18  dyoung 		ic->ic_iv = iv + 1;
   2520       1.1  dyoung 		for (i = 0; i < IEEE80211_WEP_IVLEN; i++) {
   2521       1.1  dyoung 			ivp[i] = iv;
   2522       1.1  dyoung 			iv >>= 8;
   2523       1.1  dyoung 		}
   2524       1.1  dyoung 		ivp[i] = sc->sc_ic.ic_wep_txkey << 6;	/* Key ID and pad */
   2525       1.1  dyoung 		memcpy(mtod(m0, caddr_t), hdrbuf, sizeof(hdrbuf));
   2526       1.1  dyoung 		/*
   2527       1.1  dyoung 		 * The ICV length must be included into hdrlen and pktlen.
   2528       1.1  dyoung 		 */
   2529       1.1  dyoung 		hdrlen = sizeof(hdrbuf) + IEEE80211_WEP_CRCLEN;
   2530       1.1  dyoung 		pktlen = m0->m_pkthdr.len + IEEE80211_WEP_CRCLEN;
   2531       1.1  dyoung 	}
   2532       1.1  dyoung 	pktlen += IEEE80211_CRC_LEN;
   2533       1.1  dyoung 
   2534       1.1  dyoung 	/*
   2535       1.1  dyoung 	 * Load the DMA map so any coalescing is done.  This
   2536       1.1  dyoung 	 * also calculates the number of descriptors we need.
   2537       1.1  dyoung 	 */
   2538       1.2  dyoung 	error = ath_buf_dmamap_load_mbuf(sc->sc_dmat, bf, m0, BUS_DMA_NOWAIT);
   2539       1.1  dyoung 	/*
   2540       1.1  dyoung 	 * Discard null packets and check for packets that
   2541       1.1  dyoung 	 * require too many TX descriptors.  We try to convert
   2542       1.1  dyoung 	 * the latter to a cluster.
   2543       1.1  dyoung 	 */
   2544      1.11  dyoung 	if (error == EFBIG) {		/* too many desc's, linearize */
   2545       1.1  dyoung 		sc->sc_stats.ast_tx_linear++;
   2546       1.1  dyoung 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   2547       1.1  dyoung 		if (m == NULL) {
   2548       1.1  dyoung 			sc->sc_stats.ast_tx_nombuf++;
   2549       1.1  dyoung 			m_freem(m0);
   2550       1.1  dyoung 			return ENOMEM;
   2551       1.1  dyoung 		}
   2552       1.2  dyoung #ifdef __FreeBSD__
   2553       1.1  dyoung 		M_MOVE_PKTHDR(m, m0);
   2554       1.2  dyoung #else
   2555       1.2  dyoung 		M_COPY_PKTHDR(m, m0);
   2556       1.2  dyoung #endif
   2557       1.1  dyoung 		MCLGET(m, M_DONTWAIT);
   2558       1.1  dyoung 		if ((m->m_flags & M_EXT) == 0) {
   2559       1.1  dyoung 			sc->sc_stats.ast_tx_nomcl++;
   2560       1.1  dyoung 			m_freem(m0);
   2561       1.1  dyoung 			m_free(m);
   2562       1.1  dyoung 			return ENOMEM;
   2563       1.1  dyoung 		}
   2564       1.1  dyoung 		m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
   2565       1.1  dyoung 		m_freem(m0);
   2566       1.1  dyoung 		m->m_len = m->m_pkthdr.len;
   2567       1.1  dyoung 		m0 = m;
   2568       1.2  dyoung 		error = ath_buf_dmamap_load_mbuf(sc->sc_dmat, bf, m0,
   2569       1.2  dyoung 					         BUS_DMA_NOWAIT);
   2570       1.1  dyoung 		if (error != 0) {
   2571       1.1  dyoung 			sc->sc_stats.ast_tx_busdma++;
   2572       1.1  dyoung 			m_freem(m0);
   2573       1.1  dyoung 			return error;
   2574       1.1  dyoung 		}
   2575       1.1  dyoung 		KASSERT(bf->bf_nseg == 1,
   2576       1.1  dyoung 			("ath_tx_start: packet not one segment; nseg %u",
   2577       1.1  dyoung 			bf->bf_nseg));
   2578      1.11  dyoung 	} else if (error != 0) {
   2579      1.11  dyoung 		sc->sc_stats.ast_tx_busdma++;
   2580      1.11  dyoung 		m_freem(m0);
   2581      1.11  dyoung 		return error;
   2582       1.1  dyoung 	} else if (bf->bf_nseg == 0) {		/* null packet, discard */
   2583       1.1  dyoung 		sc->sc_stats.ast_tx_nodata++;
   2584       1.1  dyoung 		m_freem(m0);
   2585       1.1  dyoung 		return EIO;
   2586       1.1  dyoung 	}
   2587       1.1  dyoung 	DPRINTF2(("ath_tx_start: m %p len %u\n", m0, pktlen));
   2588       1.2  dyoung 	ath_buf_dmamap_sync(sc->sc_dmat, bf, BUS_DMASYNC_PREWRITE);
   2589       1.1  dyoung 	bf->bf_m = m0;
   2590       1.1  dyoung 	bf->bf_node = ni;			/* NB: held reference */
   2591       1.1  dyoung 
   2592       1.1  dyoung 	/* setup descriptors */
   2593       1.1  dyoung 	ds = bf->bf_desc;
   2594       1.1  dyoung 	rt = sc->sc_currates;
   2595       1.1  dyoung 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
   2596       1.1  dyoung 
   2597       1.1  dyoung 	/*
   2598       1.1  dyoung 	 * Calculate Atheros packet type from IEEE80211 packet header
   2599       1.1  dyoung 	 * and setup for rate calculations.
   2600       1.1  dyoung 	 */
   2601       1.1  dyoung 	atype = HAL_PKT_TYPE_NORMAL;			/* default */
   2602       1.1  dyoung 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
   2603       1.1  dyoung 	case IEEE80211_FC0_TYPE_MGT:
   2604       1.1  dyoung 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
   2605       1.1  dyoung 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON)
   2606       1.1  dyoung 			atype = HAL_PKT_TYPE_BEACON;
   2607       1.1  dyoung 		else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
   2608       1.1  dyoung 			atype = HAL_PKT_TYPE_PROBE_RESP;
   2609       1.1  dyoung 		else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM)
   2610       1.1  dyoung 			atype = HAL_PKT_TYPE_ATIM;
   2611       1.1  dyoung 		rix = 0;			/* XXX lowest rate */
   2612       1.1  dyoung 		break;
   2613       1.1  dyoung 	case IEEE80211_FC0_TYPE_CTL:
   2614       1.1  dyoung 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
   2615       1.1  dyoung 		if (subtype == IEEE80211_FC0_SUBTYPE_PS_POLL)
   2616       1.1  dyoung 			atype = HAL_PKT_TYPE_PSPOLL;
   2617       1.1  dyoung 		rix = 0;			/* XXX lowest rate */
   2618       1.1  dyoung 		break;
   2619       1.1  dyoung 	default:
   2620       1.1  dyoung 		rix = sc->sc_rixmap[ni->ni_rates.rs_rates[ni->ni_txrate] &
   2621       1.1  dyoung 				IEEE80211_RATE_VAL];
   2622       1.1  dyoung 		if (rix == 0xff) {
   2623       1.1  dyoung 			if_printf(ifp, "bogus xmit rate 0x%x\n",
   2624       1.1  dyoung 				ni->ni_rates.rs_rates[ni->ni_txrate]);
   2625       1.1  dyoung 			sc->sc_stats.ast_tx_badrate++;
   2626       1.1  dyoung 			m_freem(m0);
   2627       1.1  dyoung 			return EIO;
   2628       1.1  dyoung 		}
   2629       1.1  dyoung 		break;
   2630       1.1  dyoung 	}
   2631       1.1  dyoung 	/*
   2632       1.1  dyoung 	 * NB: the 802.11 layer marks whether or not we should
   2633       1.1  dyoung 	 * use short preamble based on the current mode and
   2634       1.1  dyoung 	 * negotiated parameters.
   2635       1.1  dyoung 	 */
   2636      1.18  dyoung 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
   2637      1.18  dyoung 	    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
   2638       1.1  dyoung 		txrate = rt->info[rix].rateCode | rt->info[rix].shortPreamble;
   2639       1.1  dyoung 		shortPreamble = AH_TRUE;
   2640       1.1  dyoung 		sc->sc_stats.ast_tx_shortpre++;
   2641       1.1  dyoung 	} else {
   2642       1.1  dyoung 		txrate = rt->info[rix].rateCode;
   2643       1.1  dyoung 		shortPreamble = AH_FALSE;
   2644       1.1  dyoung 	}
   2645       1.1  dyoung 
   2646       1.1  dyoung 	/*
   2647       1.1  dyoung 	 * Calculate miscellaneous flags.
   2648       1.1  dyoung 	 */
   2649       1.1  dyoung 	flags = HAL_TXDESC_CLRDMASK;		/* XXX needed for wep errors */
   2650       1.1  dyoung 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
   2651       1.1  dyoung 		flags |= HAL_TXDESC_NOACK;	/* no ack on broad/multicast */
   2652       1.1  dyoung 		sc->sc_stats.ast_tx_noack++;
   2653       1.1  dyoung 	} else if (pktlen > ic->ic_rtsthreshold) {
   2654       1.1  dyoung 		flags |= HAL_TXDESC_RTSENA;	/* RTS based on frame length */
   2655       1.1  dyoung 		sc->sc_stats.ast_tx_rts++;
   2656       1.1  dyoung 	}
   2657       1.1  dyoung 
   2658       1.1  dyoung 	/*
   2659      1.18  dyoung 	 * Calculate duration.  This logically belongs in the 802.11
   2660      1.18  dyoung 	 * layer but it lacks sufficient information to calculate it.
   2661      1.18  dyoung 	 */
   2662      1.18  dyoung 	if ((flags & HAL_TXDESC_NOACK) == 0 &&
   2663      1.18  dyoung 	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL) {
   2664      1.18  dyoung 		u_int16_t dur;
   2665      1.18  dyoung 		/*
   2666      1.18  dyoung 		 * XXX not right with fragmentation.
   2667      1.18  dyoung 		 */
   2668      1.18  dyoung 		dur = ath_hal_computetxtime(ah, rt, IEEE80211_ACK_SIZE,
   2669      1.18  dyoung 				rix, shortPreamble);
   2670      1.18  dyoung 		*((u_int16_t*) wh->i_dur) = htole16(dur);
   2671      1.18  dyoung 	}
   2672      1.18  dyoung 
   2673      1.18  dyoung 	/*
   2674       1.1  dyoung 	 * Calculate RTS/CTS rate and duration if needed.
   2675       1.1  dyoung 	 */
   2676       1.1  dyoung 	ctsduration = 0;
   2677       1.1  dyoung 	if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA)) {
   2678       1.1  dyoung 		/*
   2679       1.1  dyoung 		 * CTS transmit rate is derived from the transmit rate
   2680       1.1  dyoung 		 * by looking in the h/w rate table.  We must also factor
   2681       1.1  dyoung 		 * in whether or not a short preamble is to be used.
   2682       1.1  dyoung 		 */
   2683       1.1  dyoung 		cix = rt->info[rix].controlRate;
   2684       1.1  dyoung 		ctsrate = rt->info[cix].rateCode;
   2685       1.1  dyoung 		if (shortPreamble)
   2686       1.1  dyoung 			ctsrate |= rt->info[cix].shortPreamble;
   2687       1.1  dyoung 		/*
   2688       1.1  dyoung 		 * Compute the transmit duration based on the size
   2689       1.1  dyoung 		 * of an ACK frame.  We call into the HAL to do the
   2690       1.1  dyoung 		 * computation since it depends on the characteristics
   2691       1.1  dyoung 		 * of the actual PHY being used.
   2692       1.1  dyoung 		 */
   2693       1.1  dyoung 		if (flags & HAL_TXDESC_RTSENA) {	/* SIFS + CTS */
   2694       1.1  dyoung 			ctsduration += ath_hal_computetxtime(ah,
   2695       1.1  dyoung 				rt, IEEE80211_ACK_SIZE, cix, shortPreamble);
   2696       1.1  dyoung 		}
   2697       1.1  dyoung 		/* SIFS + data */
   2698       1.1  dyoung 		ctsduration += ath_hal_computetxtime(ah,
   2699       1.1  dyoung 			rt, pktlen, rix, shortPreamble);
   2700       1.1  dyoung 		if ((flags & HAL_TXDESC_NOACK) == 0) {	/* SIFS + ACK */
   2701       1.1  dyoung 			ctsduration += ath_hal_computetxtime(ah,
   2702       1.1  dyoung 				rt, IEEE80211_ACK_SIZE, cix, shortPreamble);
   2703       1.1  dyoung 		}
   2704       1.1  dyoung 	} else
   2705       1.1  dyoung 		ctsrate = 0;
   2706       1.1  dyoung 
   2707       1.1  dyoung 	/*
   2708       1.1  dyoung 	 * For now use the antenna on which the last good
   2709       1.1  dyoung 	 * frame was received on.  We assume this field is
   2710       1.1  dyoung 	 * initialized to 0 which gives us ``auto'' or the
   2711       1.1  dyoung 	 * ``default'' antenna.
   2712       1.1  dyoung 	 */
   2713       1.1  dyoung 	an = (struct ath_node *) ni;
   2714       1.1  dyoung 	if (an->an_tx_antenna)
   2715       1.1  dyoung 		antenna = an->an_tx_antenna;
   2716       1.1  dyoung 	else
   2717      1.18  dyoung 		antenna = an->an_rx_hist[an->an_rx_hist_next].arh_antenna;
   2718       1.1  dyoung 
   2719       1.1  dyoung 	/*
   2720       1.1  dyoung 	 * Formulate first tx descriptor with tx controls.
   2721       1.1  dyoung 	 */
   2722       1.1  dyoung 	/* XXX check return value? */
   2723       1.1  dyoung 	ath_hal_setuptxdesc(ah, ds
   2724       1.1  dyoung 		, pktlen		/* packet length */
   2725       1.1  dyoung 		, hdrlen		/* header length */
   2726       1.1  dyoung 		, atype			/* Atheros packet type */
   2727       1.1  dyoung 		, 60			/* txpower XXX */
   2728       1.1  dyoung 		, txrate, 1+10		/* series 0 rate/tries */
   2729       1.1  dyoung 		, iswep ? sc->sc_ic.ic_wep_txkey : HAL_TXKEYIX_INVALID
   2730       1.1  dyoung 		, antenna		/* antenna mode */
   2731       1.1  dyoung 		, flags			/* flags */
   2732       1.1  dyoung 		, ctsrate		/* rts/cts rate */
   2733       1.1  dyoung 		, ctsduration		/* rts/cts duration */
   2734       1.1  dyoung 	);
   2735       1.1  dyoung #ifdef notyet
   2736       1.1  dyoung 	ath_hal_setupxtxdesc(ah, ds
   2737       1.1  dyoung 		, AH_FALSE		/* short preamble */
   2738       1.1  dyoung 		, 0, 0			/* series 1 rate/tries */
   2739       1.1  dyoung 		, 0, 0			/* series 2 rate/tries */
   2740       1.1  dyoung 		, 0, 0			/* series 3 rate/tries */
   2741       1.1  dyoung 	);
   2742       1.1  dyoung #endif
   2743       1.1  dyoung 	/*
   2744       1.1  dyoung 	 * Fillin the remainder of the descriptor info.
   2745       1.1  dyoung 	 */
   2746       1.1  dyoung 	for (i = 0; i < bf->bf_nseg; i++, ds++) {
   2747       1.1  dyoung 		ds->ds_data = bf->bf_segs[i].ds_addr;
   2748       1.1  dyoung 		if (i == bf->bf_nseg - 1)
   2749       1.1  dyoung 			ds->ds_link = 0;
   2750       1.1  dyoung 		else
   2751       1.1  dyoung 			ds->ds_link = bf->bf_daddr + sizeof(*ds) * (i + 1);
   2752       1.1  dyoung 		ath_hal_filltxdesc(ah, ds
   2753       1.1  dyoung 			, bf->bf_segs[i].ds_len	/* segment length */
   2754       1.1  dyoung 			, i == 0		/* first segment */
   2755       1.1  dyoung 			, i == bf->bf_nseg - 1	/* last segment */
   2756       1.1  dyoung 		);
   2757       1.1  dyoung 		DPRINTF2(("ath_tx_start: %d: %08x %08x %08x %08x %08x %08x\n",
   2758       1.1  dyoung 		    i, ds->ds_link, ds->ds_data, ds->ds_ctl0, ds->ds_ctl1,
   2759       1.1  dyoung 		    ds->ds_hw[0], ds->ds_hw[1]));
   2760       1.1  dyoung 	}
   2761       1.1  dyoung 
   2762       1.1  dyoung 	/*
   2763       1.1  dyoung 	 * Insert the frame on the outbound list and
   2764       1.1  dyoung 	 * pass it on to the hardware.
   2765       1.1  dyoung 	 */
   2766       1.2  dyoung 	ath_txq_critsect_begin(sc, s);
   2767       1.1  dyoung 	TAILQ_INSERT_TAIL(&sc->sc_txq, bf, bf_list);
   2768       1.1  dyoung 	if (sc->sc_txlink == NULL) {
   2769       1.1  dyoung 		ath_hal_puttxbuf(ah, sc->sc_txhalq, bf->bf_daddr);
   2770       1.1  dyoung 		DPRINTF2(("ath_tx_start: TXDP0 = %p (%p)\n",
   2771       1.1  dyoung 		    (caddr_t)bf->bf_daddr, bf->bf_desc));
   2772       1.1  dyoung 	} else {
   2773       1.1  dyoung 		*sc->sc_txlink = bf->bf_daddr;
   2774       1.1  dyoung 		DPRINTF2(("ath_tx_start: link(%p)=%p (%p)\n",
   2775       1.1  dyoung 		    sc->sc_txlink, (caddr_t)bf->bf_daddr, bf->bf_desc));
   2776       1.1  dyoung 	}
   2777       1.1  dyoung 	sc->sc_txlink = &bf->bf_desc[bf->bf_nseg - 1].ds_link;
   2778       1.2  dyoung 	ath_txq_critsect_end(sc, s);
   2779       1.1  dyoung 
   2780       1.1  dyoung 	ath_hal_txstart(ah, sc->sc_txhalq);
   2781       1.1  dyoung 	return 0;
   2782       1.1  dyoung }
   2783       1.1  dyoung 
   2784       1.1  dyoung static void
   2785       1.1  dyoung ath_tx_proc(void *arg, int npending)
   2786       1.1  dyoung {
   2787       1.1  dyoung 	struct ath_softc *sc = arg;
   2788       1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   2789       1.1  dyoung 	struct ath_buf *bf;
   2790       1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   2791       1.1  dyoung 	struct ifnet *ifp = &ic->ic_if;
   2792       1.1  dyoung 	struct ath_desc *ds;
   2793       1.1  dyoung 	struct ieee80211_node *ni;
   2794       1.1  dyoung 	struct ath_node *an;
   2795       1.1  dyoung 	int sr, lr;
   2796       1.1  dyoung 	HAL_STATUS status;
   2797       1.2  dyoung 	ath_txq_critsect_decl(s);
   2798       1.2  dyoung 	ath_txbuf_critsect_decl(s2);
   2799       1.1  dyoung 
   2800       1.1  dyoung 	DPRINTF2(("ath_tx_proc: pending %u tx queue %p, link %p\n",
   2801       1.1  dyoung 		npending, (caddr_t) ath_hal_gettxbuf(sc->sc_ah, sc->sc_txhalq),
   2802       1.1  dyoung 		sc->sc_txlink));
   2803       1.1  dyoung 	for (;;) {
   2804       1.2  dyoung 		ath_txq_critsect_begin(sc, s);
   2805       1.1  dyoung 		bf = TAILQ_FIRST(&sc->sc_txq);
   2806       1.1  dyoung 		if (bf == NULL) {
   2807       1.1  dyoung 			sc->sc_txlink = NULL;
   2808       1.2  dyoung 			ath_txq_critsect_end(sc, s);
   2809       1.1  dyoung 			break;
   2810       1.1  dyoung 		}
   2811       1.1  dyoung 		/* only the last descriptor is needed */
   2812       1.1  dyoung 		ds = &bf->bf_desc[bf->bf_nseg - 1];
   2813       1.1  dyoung 		status = ath_hal_txprocdesc(ah, ds);
   2814       1.1  dyoung #ifdef AR_DEBUG
   2815       1.1  dyoung 		if (ath_debug > 1)
   2816       1.1  dyoung 			ath_printtxbuf(bf, status == HAL_OK);
   2817       1.1  dyoung #endif
   2818       1.1  dyoung 		if (status == HAL_EINPROGRESS) {
   2819       1.2  dyoung 			ath_txq_critsect_end(sc, s);
   2820       1.1  dyoung 			break;
   2821       1.1  dyoung 		}
   2822       1.1  dyoung 		TAILQ_REMOVE(&sc->sc_txq, bf, bf_list);
   2823       1.2  dyoung 		ath_txq_critsect_end(sc, s);
   2824       1.1  dyoung 
   2825       1.1  dyoung 		ni = bf->bf_node;
   2826       1.1  dyoung 		if (ni != NULL) {
   2827       1.1  dyoung 			an = (struct ath_node *) ni;
   2828       1.1  dyoung 			if (ds->ds_txstat.ts_status == 0) {
   2829       1.1  dyoung 				an->an_tx_ok++;
   2830       1.1  dyoung 				an->an_tx_antenna = ds->ds_txstat.ts_antenna;
   2831       1.1  dyoung 			} else {
   2832       1.1  dyoung 				an->an_tx_err++;
   2833       1.1  dyoung 				ifp->if_oerrors++;
   2834       1.1  dyoung 				if (ds->ds_txstat.ts_status & HAL_TXERR_XRETRY)
   2835       1.1  dyoung 					sc->sc_stats.ast_tx_xretries++;
   2836       1.1  dyoung 				if (ds->ds_txstat.ts_status & HAL_TXERR_FIFO)
   2837       1.1  dyoung 					sc->sc_stats.ast_tx_fifoerr++;
   2838       1.1  dyoung 				if (ds->ds_txstat.ts_status & HAL_TXERR_FILT)
   2839       1.1  dyoung 					sc->sc_stats.ast_tx_filtered++;
   2840       1.1  dyoung 				an->an_tx_antenna = 0;	/* invalidate */
   2841       1.1  dyoung 			}
   2842       1.1  dyoung 			sr = ds->ds_txstat.ts_shortretry;
   2843       1.1  dyoung 			lr = ds->ds_txstat.ts_longretry;
   2844       1.1  dyoung 			sc->sc_stats.ast_tx_shortretry += sr;
   2845       1.1  dyoung 			sc->sc_stats.ast_tx_longretry += lr;
   2846       1.1  dyoung 			if (sr + lr)
   2847       1.1  dyoung 				an->an_tx_retr++;
   2848       1.1  dyoung 			/*
   2849       1.1  dyoung 			 * Reclaim reference to node.
   2850       1.1  dyoung 			 *
   2851       1.1  dyoung 			 * NB: the node may be reclaimed here if, for example
   2852       1.1  dyoung 			 *     this is a DEAUTH message that was sent and the
   2853       1.1  dyoung 			 *     node was timed out due to inactivity.
   2854       1.1  dyoung 			 */
   2855       1.1  dyoung 			if (ni != ic->ic_bss)
   2856       1.1  dyoung 				ieee80211_free_node(ic, ni);
   2857       1.1  dyoung 		}
   2858       1.2  dyoung 		ath_buf_dmamap_sync(sc->sc_dmat, bf, BUS_DMASYNC_POSTWRITE);
   2859       1.1  dyoung 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
   2860       1.1  dyoung 		m_freem(bf->bf_m);
   2861       1.1  dyoung 		bf->bf_m = NULL;
   2862       1.1  dyoung 		bf->bf_node = NULL;
   2863       1.1  dyoung 
   2864       1.2  dyoung 		ath_txbuf_critsect_begin(sc, s2);
   2865       1.1  dyoung 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
   2866       1.2  dyoung 		ath_txbuf_critsect_end(sc, s2);
   2867       1.1  dyoung 	}
   2868       1.1  dyoung 	ifp->if_flags &= ~IFF_OACTIVE;
   2869       1.1  dyoung 	sc->sc_tx_timer = 0;
   2870       1.1  dyoung 
   2871       1.1  dyoung 	ath_start(ifp);
   2872       1.1  dyoung }
   2873       1.1  dyoung 
   2874       1.1  dyoung /*
   2875       1.1  dyoung  * Drain the transmit queue and reclaim resources.
   2876       1.1  dyoung  */
   2877       1.1  dyoung static void
   2878       1.1  dyoung ath_draintxq(struct ath_softc *sc)
   2879       1.1  dyoung {
   2880       1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   2881       1.1  dyoung 	struct ifnet *ifp = &sc->sc_ic.ic_if;
   2882       1.1  dyoung 	struct ath_buf *bf;
   2883       1.2  dyoung 	ath_txq_critsect_decl(s);
   2884       1.2  dyoung 	ath_txbuf_critsect_decl(s2);
   2885       1.1  dyoung 
   2886       1.1  dyoung 	/* XXX return value */
   2887       1.1  dyoung 	if (!sc->sc_invalid) {
   2888       1.1  dyoung 		/* don't touch the hardware if marked invalid */
   2889       1.1  dyoung 		(void) ath_hal_stoptxdma(ah, sc->sc_txhalq);
   2890       1.1  dyoung 		DPRINTF(("ath_draintxq: tx queue %p, link %p\n",
   2891       1.1  dyoung 		    (caddr_t) ath_hal_gettxbuf(ah, sc->sc_txhalq),
   2892       1.1  dyoung 		    sc->sc_txlink));
   2893       1.1  dyoung 		(void) ath_hal_stoptxdma(ah, sc->sc_bhalq);
   2894       1.1  dyoung 		DPRINTF(("ath_draintxq: beacon queue %p\n",
   2895       1.1  dyoung 		    (caddr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq)));
   2896       1.1  dyoung 	}
   2897       1.1  dyoung 	for (;;) {
   2898       1.2  dyoung 		ath_txq_critsect_begin(sc, s);
   2899       1.1  dyoung 		bf = TAILQ_FIRST(&sc->sc_txq);
   2900       1.1  dyoung 		if (bf == NULL) {
   2901       1.1  dyoung 			sc->sc_txlink = NULL;
   2902       1.2  dyoung 			ath_txq_critsect_end(sc, s);
   2903       1.1  dyoung 			break;
   2904       1.1  dyoung 		}
   2905       1.1  dyoung 		TAILQ_REMOVE(&sc->sc_txq, bf, bf_list);
   2906       1.2  dyoung 		ath_txq_critsect_end(sc, s);
   2907       1.1  dyoung #ifdef AR_DEBUG
   2908       1.1  dyoung 		if (ath_debug)
   2909       1.1  dyoung 			ath_printtxbuf(bf,
   2910       1.1  dyoung 				ath_hal_txprocdesc(ah, bf->bf_desc) == HAL_OK);
   2911       1.1  dyoung #endif /* AR_DEBUG */
   2912       1.1  dyoung 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
   2913       1.1  dyoung 		m_freem(bf->bf_m);
   2914       1.1  dyoung 		bf->bf_m = NULL;
   2915       1.1  dyoung 		bf->bf_node = NULL;
   2916       1.2  dyoung 		ath_txbuf_critsect_begin(sc, s2);
   2917       1.1  dyoung 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
   2918       1.2  dyoung 		ath_txbuf_critsect_end(sc, s2);
   2919       1.1  dyoung 	}
   2920       1.1  dyoung 	ifp->if_flags &= ~IFF_OACTIVE;
   2921       1.1  dyoung 	sc->sc_tx_timer = 0;
   2922       1.1  dyoung }
   2923       1.1  dyoung 
   2924       1.1  dyoung /*
   2925       1.1  dyoung  * Disable the receive h/w in preparation for a reset.
   2926       1.1  dyoung  */
   2927       1.1  dyoung static void
   2928       1.1  dyoung ath_stoprecv(struct ath_softc *sc)
   2929       1.1  dyoung {
   2930      1.18  dyoung #define	PA2DESC(_sc, _pa) \
   2931      1.18  dyoung 	((struct ath_desc *)((caddr_t)(_sc)->sc_desc + \
   2932      1.18  dyoung 		((_pa) - (_sc)->sc_desc_paddr)))
   2933       1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   2934       1.1  dyoung 
   2935       1.1  dyoung 	ath_hal_stoppcurecv(ah);	/* disable PCU */
   2936       1.1  dyoung 	ath_hal_setrxfilter(ah, 0);	/* clear recv filter */
   2937       1.1  dyoung 	ath_hal_stopdmarecv(ah);	/* disable DMA engine */
   2938       1.1  dyoung 	DELAY(3000);			/* long enough for 1 frame */
   2939       1.1  dyoung #ifdef AR_DEBUG
   2940       1.1  dyoung 	if (ath_debug) {
   2941       1.1  dyoung 		struct ath_buf *bf;
   2942       1.1  dyoung 
   2943       1.1  dyoung 		DPRINTF(("ath_stoprecv: rx queue %p, link %p\n",
   2944       1.1  dyoung 		    (caddr_t) ath_hal_getrxbuf(ah), sc->sc_rxlink));
   2945       1.1  dyoung 		TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
   2946      1.18  dyoung 			struct ath_desc *ds = bf->bf_desc;
   2947      1.18  dyoung 			if (ath_hal_rxprocdesc(ah, ds, bf->bf_daddr,
   2948      1.18  dyoung 			    PA2DESC(sc, ds->ds_link)) == HAL_OK)
   2949       1.1  dyoung 				ath_printrxbuf(bf, 1);
   2950       1.1  dyoung 		}
   2951       1.1  dyoung 	}
   2952       1.1  dyoung #endif
   2953       1.1  dyoung 	sc->sc_rxlink = NULL;		/* just in case */
   2954      1.18  dyoung #undef PA2DESC
   2955       1.1  dyoung }
   2956       1.1  dyoung 
   2957       1.1  dyoung /*
   2958       1.1  dyoung  * Enable the receive h/w following a reset.
   2959       1.1  dyoung  */
   2960       1.1  dyoung static int
   2961       1.1  dyoung ath_startrecv(struct ath_softc *sc)
   2962       1.1  dyoung {
   2963       1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   2964       1.1  dyoung 	struct ath_buf *bf;
   2965       1.1  dyoung 
   2966       1.1  dyoung 	sc->sc_rxlink = NULL;
   2967       1.1  dyoung 	TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
   2968       1.1  dyoung 		int error = ath_rxbuf_init(sc, bf);
   2969       1.1  dyoung 		if (error != 0) {
   2970       1.1  dyoung 			DPRINTF(("ath_startrecv: ath_rxbuf_init failed %d\n",
   2971       1.1  dyoung 				error));
   2972       1.1  dyoung 			return error;
   2973       1.1  dyoung 		}
   2974       1.1  dyoung 	}
   2975       1.1  dyoung 
   2976       1.1  dyoung 	bf = TAILQ_FIRST(&sc->sc_rxbuf);
   2977       1.1  dyoung 	ath_hal_putrxbuf(ah, bf->bf_daddr);
   2978       1.1  dyoung 	ath_hal_rxena(ah);		/* enable recv descriptors */
   2979       1.1  dyoung 	ath_mode_init(sc);		/* set filters, etc. */
   2980       1.1  dyoung 	ath_hal_startpcurecv(ah);	/* re-enable PCU/DMA engine */
   2981       1.1  dyoung 	return 0;
   2982       1.1  dyoung }
   2983       1.1  dyoung 
   2984       1.1  dyoung /*
   2985       1.1  dyoung  * Set/change channels.  If the channel is really being changed,
   2986       1.1  dyoung  * it's done by resetting the chip.  To accomplish this we must
   2987       1.1  dyoung  * first cleanup any pending DMA, then restart stuff after a la
   2988       1.1  dyoung  * ath_init.
   2989       1.1  dyoung  */
   2990       1.1  dyoung static int
   2991       1.1  dyoung ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan)
   2992       1.1  dyoung {
   2993       1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   2994       1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   2995       1.1  dyoung 
   2996       1.1  dyoung 	DPRINTF(("ath_chan_set: %u (%u MHz) -> %u (%u MHz)\n",
   2997       1.1  dyoung 	    ieee80211_chan2ieee(ic, ic->ic_ibss_chan),
   2998       1.1  dyoung 		ic->ic_ibss_chan->ic_freq,
   2999       1.1  dyoung 	    ieee80211_chan2ieee(ic, chan), chan->ic_freq));
   3000       1.1  dyoung 	if (chan != ic->ic_ibss_chan) {
   3001       1.1  dyoung 		HAL_STATUS status;
   3002       1.1  dyoung 		HAL_CHANNEL hchan;
   3003       1.1  dyoung 		enum ieee80211_phymode mode;
   3004       1.1  dyoung 
   3005       1.1  dyoung 		/*
   3006       1.1  dyoung 		 * To switch channels clear any pending DMA operations;
   3007       1.1  dyoung 		 * wait long enough for the RX fifo to drain, reset the
   3008       1.1  dyoung 		 * hardware at the new frequency, and then re-enable
   3009       1.1  dyoung 		 * the relevant bits of the h/w.
   3010       1.1  dyoung 		 */
   3011       1.1  dyoung 		ath_hal_intrset(ah, 0);		/* disable interrupts */
   3012       1.1  dyoung 		ath_draintxq(sc);		/* clear pending tx frames */
   3013       1.1  dyoung 		ath_stoprecv(sc);		/* turn off frame recv */
   3014       1.1  dyoung 		/*
   3015       1.1  dyoung 		 * Convert to a HAL channel description with
   3016       1.1  dyoung 		 * the flags constrained to reflect the current
   3017       1.1  dyoung 		 * operating mode.
   3018       1.1  dyoung 		 */
   3019       1.1  dyoung 		hchan.channel = chan->ic_freq;
   3020       1.1  dyoung 		hchan.channelFlags = ath_chan2flags(ic, chan);
   3021       1.1  dyoung 		if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_TRUE, &status)) {
   3022       1.1  dyoung 			if_printf(&ic->ic_if, "ath_chan_set: unable to reset "
   3023       1.1  dyoung 				"channel %u (%u Mhz)\n",
   3024       1.1  dyoung 				ieee80211_chan2ieee(ic, chan), chan->ic_freq);
   3025       1.1  dyoung 			return EIO;
   3026       1.1  dyoung 		}
   3027       1.1  dyoung 		/*
   3028       1.1  dyoung 		 * Re-enable rx framework.
   3029       1.1  dyoung 		 */
   3030       1.1  dyoung 		if (ath_startrecv(sc) != 0) {
   3031       1.1  dyoung 			if_printf(&ic->ic_if,
   3032       1.1  dyoung 				"ath_chan_set: unable to restart recv logic\n");
   3033       1.1  dyoung 			return EIO;
   3034       1.1  dyoung 		}
   3035       1.1  dyoung 
   3036       1.1  dyoung 		/*
   3037       1.1  dyoung 		 * Update BPF state.
   3038       1.1  dyoung 		 */
   3039       1.1  dyoung 		sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
   3040       1.1  dyoung 			htole16(chan->ic_freq);
   3041       1.1  dyoung 		sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =
   3042       1.1  dyoung 			htole16(chan->ic_flags);
   3043       1.1  dyoung 
   3044       1.1  dyoung 		/*
   3045       1.1  dyoung 		 * Change channels and update the h/w rate map
   3046       1.1  dyoung 		 * if we're switching; e.g. 11a to 11b/g.
   3047       1.1  dyoung 		 */
   3048       1.1  dyoung 		ic->ic_ibss_chan = chan;
   3049       1.1  dyoung 		mode = ieee80211_chan2mode(ic, chan);
   3050       1.1  dyoung 		if (mode != sc->sc_curmode)
   3051       1.1  dyoung 			ath_setcurmode(sc, mode);
   3052       1.1  dyoung 
   3053       1.1  dyoung 		/*
   3054       1.1  dyoung 		 * Re-enable interrupts.
   3055       1.1  dyoung 		 */
   3056       1.1  dyoung 		ath_hal_intrset(ah, sc->sc_imask);
   3057       1.1  dyoung 	}
   3058       1.1  dyoung 	return 0;
   3059       1.1  dyoung }
   3060       1.1  dyoung 
   3061       1.1  dyoung static void
   3062       1.1  dyoung ath_next_scan(void *arg)
   3063       1.1  dyoung {
   3064       1.1  dyoung 	struct ath_softc *sc = arg;
   3065       1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   3066       1.1  dyoung 	struct ifnet *ifp = &ic->ic_if;
   3067       1.2  dyoung 	int s;
   3068       1.2  dyoung 
   3069       1.2  dyoung 	/* don't call ath_start w/o network interrupts blocked */
   3070       1.2  dyoung 	s = splnet();
   3071       1.1  dyoung 
   3072       1.1  dyoung 	if (ic->ic_state == IEEE80211_S_SCAN)
   3073       1.1  dyoung 		ieee80211_next_scan(ifp);
   3074       1.2  dyoung 	splx(s);
   3075       1.1  dyoung }
   3076       1.1  dyoung 
   3077       1.1  dyoung /*
   3078       1.1  dyoung  * Periodically recalibrate the PHY to account
   3079       1.1  dyoung  * for temperature/environment changes.
   3080       1.1  dyoung  */
   3081       1.1  dyoung static void
   3082       1.1  dyoung ath_calibrate(void *arg)
   3083       1.1  dyoung {
   3084       1.1  dyoung 	struct ath_softc *sc = arg;
   3085       1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   3086       1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   3087       1.1  dyoung 	struct ieee80211_channel *c;
   3088       1.1  dyoung 	HAL_CHANNEL hchan;
   3089       1.1  dyoung 
   3090       1.1  dyoung 	sc->sc_stats.ast_per_cal++;
   3091       1.1  dyoung 
   3092       1.1  dyoung 	/*
   3093       1.1  dyoung 	 * Convert to a HAL channel description with the flags
   3094       1.1  dyoung 	 * constrained to reflect the current operating mode.
   3095       1.1  dyoung 	 */
   3096       1.1  dyoung 	c = ic->ic_ibss_chan;
   3097       1.1  dyoung 	hchan.channel = c->ic_freq;
   3098       1.1  dyoung 	hchan.channelFlags = ath_chan2flags(ic, c);
   3099       1.1  dyoung 
   3100       1.1  dyoung 	DPRINTF(("%s: channel %u/%x\n", __func__, c->ic_freq, c->ic_flags));
   3101       1.1  dyoung 
   3102       1.1  dyoung 	if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) {
   3103       1.1  dyoung 		/*
   3104       1.1  dyoung 		 * Rfgain is out of bounds, reset the chip
   3105       1.1  dyoung 		 * to load new gain values.
   3106       1.1  dyoung 		 */
   3107       1.1  dyoung 		sc->sc_stats.ast_per_rfgain++;
   3108       1.1  dyoung 		ath_reset(sc);
   3109       1.1  dyoung 	}
   3110       1.1  dyoung 	if (!ath_hal_calibrate(ah, &hchan)) {
   3111       1.1  dyoung 		DPRINTF(("%s: calibration of channel %u failed\n",
   3112       1.1  dyoung 			__func__, c->ic_freq));
   3113       1.1  dyoung 		sc->sc_stats.ast_per_calfail++;
   3114       1.1  dyoung 	}
   3115       1.1  dyoung 	callout_reset(&sc->sc_cal_ch, hz * ath_calinterval, ath_calibrate, sc);
   3116       1.1  dyoung }
   3117       1.1  dyoung 
   3118       1.4  dyoung static HAL_LED_STATE
   3119       1.4  dyoung ath_state_to_led(enum ieee80211_state state)
   3120       1.4  dyoung {
   3121       1.4  dyoung 	switch (state) {
   3122       1.4  dyoung 	case IEEE80211_S_INIT:
   3123       1.4  dyoung 		return HAL_LED_INIT;
   3124       1.4  dyoung 	case IEEE80211_S_SCAN:
   3125       1.4  dyoung 		return HAL_LED_SCAN;
   3126       1.4  dyoung 	case IEEE80211_S_AUTH:
   3127       1.4  dyoung 		return HAL_LED_AUTH;
   3128       1.4  dyoung 	case IEEE80211_S_ASSOC:
   3129       1.4  dyoung 		return HAL_LED_ASSOC;
   3130       1.4  dyoung 	case IEEE80211_S_RUN:
   3131       1.4  dyoung 		return HAL_LED_RUN;
   3132       1.4  dyoung 	default:
   3133       1.4  dyoung 		panic("%s: unknown 802.11 state %d\n", __func__, state);
   3134       1.4  dyoung 		return HAL_LED_INIT;
   3135       1.4  dyoung 	}
   3136       1.4  dyoung }
   3137       1.4  dyoung 
   3138       1.1  dyoung static int
   3139       1.1  dyoung ath_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
   3140       1.1  dyoung {
   3141       1.1  dyoung 	struct ifnet *ifp = &ic->ic_if;
   3142       1.1  dyoung 	struct ath_softc *sc = ifp->if_softc;
   3143       1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   3144       1.1  dyoung 	struct ieee80211_node *ni;
   3145       1.1  dyoung 	int i, error;
   3146      1.18  dyoung 	const u_int8_t *bssid;
   3147       1.1  dyoung 	u_int32_t rfilt;
   3148       1.1  dyoung 
   3149       1.1  dyoung 	DPRINTF(("%s: %s -> %s\n", __func__,
   3150       1.1  dyoung 		ieee80211_state_name[ic->ic_state],
   3151       1.1  dyoung 		ieee80211_state_name[nstate]));
   3152       1.1  dyoung 
   3153       1.4  dyoung 	ath_hal_setledstate(ah, ath_state_to_led(nstate));	/* set LED */
   3154       1.1  dyoung 
   3155       1.1  dyoung 	if (nstate == IEEE80211_S_INIT) {
   3156       1.1  dyoung 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
   3157       1.1  dyoung 		ath_hal_intrset(ah, sc->sc_imask);
   3158       1.1  dyoung 		callout_stop(&sc->sc_scan_ch);
   3159       1.1  dyoung 		callout_stop(&sc->sc_cal_ch);
   3160       1.1  dyoung 		return (*sc->sc_newstate)(ic, nstate, arg);
   3161       1.1  dyoung 	}
   3162       1.1  dyoung 	ni = ic->ic_bss;
   3163       1.1  dyoung 	error = ath_chan_set(sc, ni->ni_chan);
   3164       1.1  dyoung 	if (error != 0)
   3165       1.1  dyoung 		goto bad;
   3166      1.18  dyoung 	rfilt = ath_calcrxfilter(sc);
   3167       1.1  dyoung 	if (nstate == IEEE80211_S_SCAN) {
   3168       1.1  dyoung 		callout_reset(&sc->sc_scan_ch, (hz * ath_dwelltime) / 1000,
   3169       1.1  dyoung 			ath_next_scan, sc);
   3170       1.1  dyoung 		bssid = ifp->if_broadcastaddr;
   3171       1.1  dyoung 	} else {
   3172       1.1  dyoung 		callout_stop(&sc->sc_scan_ch);
   3173       1.1  dyoung 		bssid = ni->ni_bssid;
   3174       1.1  dyoung 	}
   3175       1.1  dyoung 	ath_hal_setrxfilter(ah, rfilt);
   3176       1.1  dyoung 	DPRINTF(("%s: RX filter 0x%x bssid %s\n",
   3177       1.1  dyoung 		 __func__, rfilt, ether_sprintf(bssid)));
   3178       1.1  dyoung 
   3179       1.1  dyoung 	if (nstate == IEEE80211_S_RUN && ic->ic_opmode == IEEE80211_M_STA)
   3180       1.1  dyoung 		ath_hal_setassocid(ah, bssid, ni->ni_associd);
   3181       1.1  dyoung 	else
   3182       1.1  dyoung 		ath_hal_setassocid(ah, bssid, 0);
   3183       1.1  dyoung 	if (ic->ic_flags & IEEE80211_F_WEPON) {
   3184       1.1  dyoung 		for (i = 0; i < IEEE80211_WEP_NKID; i++)
   3185       1.1  dyoung 			if (ath_hal_keyisvalid(ah, i))
   3186       1.1  dyoung 				ath_hal_keysetmac(ah, i, bssid);
   3187       1.1  dyoung 	}
   3188       1.1  dyoung 
   3189       1.1  dyoung 	if (nstate == IEEE80211_S_RUN) {
   3190       1.1  dyoung 		DPRINTF(("%s(RUN): ic_flags=0x%08x iv=%d bssid=%s "
   3191       1.1  dyoung 			"capinfo=0x%04x chan=%d\n"
   3192       1.1  dyoung 			 , __func__
   3193       1.1  dyoung 			 , ic->ic_flags
   3194       1.1  dyoung 			 , ni->ni_intval
   3195       1.1  dyoung 			 , ether_sprintf(ni->ni_bssid)
   3196       1.1  dyoung 			 , ni->ni_capinfo
   3197       1.1  dyoung 			 , ieee80211_chan2ieee(ic, ni->ni_chan)));
   3198       1.1  dyoung 
   3199       1.1  dyoung 		/*
   3200       1.1  dyoung 		 * Allocate and setup the beacon frame for AP or adhoc mode.
   3201       1.1  dyoung 		 */
   3202       1.1  dyoung 		if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
   3203       1.1  dyoung 		    ic->ic_opmode == IEEE80211_M_IBSS) {
   3204       1.1  dyoung 			error = ath_beacon_alloc(sc, ni);
   3205       1.1  dyoung 			if (error != 0)
   3206       1.1  dyoung 				goto bad;
   3207       1.1  dyoung 		}
   3208       1.1  dyoung 
   3209       1.1  dyoung 		/*
   3210       1.1  dyoung 		 * Configure the beacon and sleep timers.
   3211       1.1  dyoung 		 */
   3212       1.1  dyoung 		ath_beacon_config(sc);
   3213       1.1  dyoung 
   3214       1.1  dyoung 		/* start periodic recalibration timer */
   3215       1.1  dyoung 		callout_reset(&sc->sc_cal_ch, hz * ath_calinterval,
   3216       1.1  dyoung 			ath_calibrate, sc);
   3217       1.1  dyoung 	} else {
   3218       1.1  dyoung 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
   3219       1.1  dyoung 		ath_hal_intrset(ah, sc->sc_imask);
   3220       1.1  dyoung 		callout_stop(&sc->sc_cal_ch);		/* no calibration */
   3221       1.1  dyoung 	}
   3222       1.1  dyoung 	/*
   3223       1.1  dyoung 	 * Reset the rate control state.
   3224       1.1  dyoung 	 */
   3225       1.1  dyoung 	ath_rate_ctl_reset(sc, nstate);
   3226       1.1  dyoung 	/*
   3227       1.1  dyoung 	 * Invoke the parent method to complete the work.
   3228       1.1  dyoung 	 */
   3229       1.1  dyoung 	return (*sc->sc_newstate)(ic, nstate, arg);
   3230       1.1  dyoung bad:
   3231       1.1  dyoung 	callout_stop(&sc->sc_scan_ch);
   3232       1.1  dyoung 	callout_stop(&sc->sc_cal_ch);
   3233       1.1  dyoung 	/* NB: do not invoke the parent */
   3234       1.1  dyoung 	return error;
   3235       1.1  dyoung }
   3236       1.1  dyoung 
   3237       1.1  dyoung /*
   3238       1.1  dyoung  * Setup driver-specific state for a newly associated node.
   3239       1.1  dyoung  * Note that we're called also on a re-associate, the isnew
   3240       1.1  dyoung  * param tells us if this is the first time or not.
   3241       1.1  dyoung  */
   3242       1.1  dyoung static void
   3243       1.1  dyoung ath_newassoc(struct ieee80211com *ic, struct ieee80211_node *ni, int isnew)
   3244       1.1  dyoung {
   3245       1.1  dyoung 	if (isnew) {
   3246       1.1  dyoung 		struct ath_node *an = (struct ath_node *) ni;
   3247       1.1  dyoung 
   3248       1.1  dyoung 		an->an_tx_ok = an->an_tx_err =
   3249       1.1  dyoung 			an->an_tx_retr = an->an_tx_upper = 0;
   3250       1.1  dyoung 		/* start with highest negotiated rate */
   3251       1.1  dyoung 		/*
   3252       1.1  dyoung 		 * XXX should do otherwise but only when
   3253       1.1  dyoung 		 * the rate control algorithm is better.
   3254       1.1  dyoung 		 */
   3255       1.1  dyoung 		KASSERT(ni->ni_rates.rs_nrates > 0,
   3256       1.1  dyoung 			("new association w/ no rates!"));
   3257       1.1  dyoung 		ni->ni_txrate = ni->ni_rates.rs_nrates - 1;
   3258       1.1  dyoung 	}
   3259       1.1  dyoung }
   3260       1.1  dyoung 
   3261       1.1  dyoung static int
   3262      1.21  dyoung ath_getchannels(struct ath_softc *sc, u_int cc, HAL_BOOL outdoor,
   3263      1.21  dyoung     HAL_BOOL xchanmode)
   3264       1.1  dyoung {
   3265       1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   3266       1.1  dyoung 	struct ifnet *ifp = &ic->ic_if;
   3267       1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   3268       1.1  dyoung 	HAL_CHANNEL *chans;
   3269       1.1  dyoung 	int i, ix, nchan;
   3270       1.1  dyoung 
   3271       1.1  dyoung 	chans = malloc(IEEE80211_CHAN_MAX * sizeof(HAL_CHANNEL),
   3272       1.1  dyoung 			M_TEMP, M_NOWAIT);
   3273       1.1  dyoung 	if (chans == NULL) {
   3274       1.1  dyoung 		if_printf(ifp, "unable to allocate channel table\n");
   3275       1.1  dyoung 		return ENOMEM;
   3276       1.1  dyoung 	}
   3277       1.1  dyoung 	if (!ath_hal_init_channels(ah, chans, IEEE80211_CHAN_MAX, &nchan,
   3278      1.21  dyoung 	    cc, HAL_MODE_ALL, outdoor, xchanmode)) {
   3279       1.1  dyoung 		if_printf(ifp, "unable to collect channel list from hal\n");
   3280       1.1  dyoung 		free(chans, M_TEMP);
   3281       1.1  dyoung 		return EINVAL;
   3282       1.1  dyoung 	}
   3283       1.1  dyoung 
   3284       1.1  dyoung 	/*
   3285       1.1  dyoung 	 * Convert HAL channels to ieee80211 ones and insert
   3286       1.1  dyoung 	 * them in the table according to their channel number.
   3287       1.1  dyoung 	 */
   3288       1.1  dyoung 	for (i = 0; i < nchan; i++) {
   3289       1.1  dyoung 		HAL_CHANNEL *c = &chans[i];
   3290       1.1  dyoung 		ix = ath_hal_mhz2ieee(c->channel, c->channelFlags);
   3291       1.1  dyoung 		if (ix > IEEE80211_CHAN_MAX) {
   3292       1.1  dyoung 			if_printf(ifp, "bad hal channel %u (%u/%x) ignored\n",
   3293       1.1  dyoung 				ix, c->channel, c->channelFlags);
   3294       1.1  dyoung 			continue;
   3295       1.1  dyoung 		}
   3296      1.21  dyoung 		DPRINTF(("%s: HAL channel %d/%d freq %d flags %#04x idx %d\n",
   3297      1.21  dyoung 		    sc->sc_dev.dv_xname, i, nchan, c->channel, c->channelFlags,
   3298      1.21  dyoung 		    ix));
   3299       1.1  dyoung 		/* NB: flags are known to be compatible */
   3300       1.1  dyoung 		if (ic->ic_channels[ix].ic_freq == 0) {
   3301       1.1  dyoung 			ic->ic_channels[ix].ic_freq = c->channel;
   3302       1.1  dyoung 			ic->ic_channels[ix].ic_flags = c->channelFlags;
   3303       1.1  dyoung 		} else {
   3304       1.1  dyoung 			/* channels overlap; e.g. 11g and 11b */
   3305       1.1  dyoung 			ic->ic_channels[ix].ic_flags |= c->channelFlags;
   3306       1.1  dyoung 		}
   3307       1.1  dyoung 	}
   3308       1.1  dyoung 	free(chans, M_TEMP);
   3309       1.1  dyoung 	return 0;
   3310       1.1  dyoung }
   3311       1.1  dyoung 
   3312       1.1  dyoung static int
   3313       1.1  dyoung ath_rate_setup(struct ath_softc *sc, u_int mode)
   3314       1.1  dyoung {
   3315       1.1  dyoung 	struct ath_hal *ah = sc->sc_ah;
   3316       1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   3317       1.1  dyoung 	const HAL_RATE_TABLE *rt;
   3318       1.1  dyoung 	struct ieee80211_rateset *rs;
   3319       1.1  dyoung 	int i, maxrates;
   3320       1.1  dyoung 
   3321       1.1  dyoung 	switch (mode) {
   3322       1.1  dyoung 	case IEEE80211_MODE_11A:
   3323       1.1  dyoung 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11A);
   3324       1.1  dyoung 		break;
   3325       1.1  dyoung 	case IEEE80211_MODE_11B:
   3326       1.1  dyoung 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11B);
   3327       1.1  dyoung 		break;
   3328       1.1  dyoung 	case IEEE80211_MODE_11G:
   3329       1.1  dyoung 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11G);
   3330       1.1  dyoung 		break;
   3331       1.1  dyoung 	case IEEE80211_MODE_TURBO:
   3332       1.1  dyoung 		sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_TURBO);
   3333       1.1  dyoung 		break;
   3334       1.1  dyoung 	default:
   3335       1.1  dyoung 		DPRINTF(("%s: invalid mode %u\n", __func__, mode));
   3336       1.1  dyoung 		return 0;
   3337       1.1  dyoung 	}
   3338       1.1  dyoung 	rt = sc->sc_rates[mode];
   3339       1.1  dyoung 	if (rt == NULL)
   3340       1.1  dyoung 		return 0;
   3341       1.1  dyoung 	if (rt->rateCount > IEEE80211_RATE_MAXSIZE) {
   3342       1.1  dyoung 		DPRINTF(("%s: rate table too small (%u > %u)\n",
   3343       1.1  dyoung 			__func__, rt->rateCount, IEEE80211_RATE_MAXSIZE));
   3344       1.1  dyoung 		maxrates = IEEE80211_RATE_MAXSIZE;
   3345       1.1  dyoung 	} else
   3346       1.1  dyoung 		maxrates = rt->rateCount;
   3347       1.1  dyoung 	rs = &ic->ic_sup_rates[mode];
   3348       1.1  dyoung 	for (i = 0; i < maxrates; i++)
   3349       1.1  dyoung 		rs->rs_rates[i] = rt->info[i].dot11Rate;
   3350       1.1  dyoung 	rs->rs_nrates = maxrates;
   3351       1.1  dyoung 	return 1;
   3352       1.1  dyoung }
   3353       1.1  dyoung 
   3354       1.1  dyoung static void
   3355       1.1  dyoung ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode)
   3356       1.1  dyoung {
   3357       1.1  dyoung 	const HAL_RATE_TABLE *rt;
   3358       1.1  dyoung 	int i;
   3359       1.1  dyoung 
   3360       1.1  dyoung 	memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap));
   3361       1.1  dyoung 	rt = sc->sc_rates[mode];
   3362       1.1  dyoung 	KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode));
   3363       1.1  dyoung 	for (i = 0; i < rt->rateCount; i++)
   3364       1.1  dyoung 		sc->sc_rixmap[rt->info[i].dot11Rate & IEEE80211_RATE_VAL] = i;
   3365       1.1  dyoung 	memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap));
   3366       1.1  dyoung 	for (i = 0; i < 32; i++)
   3367       1.1  dyoung 		sc->sc_hwmap[i] = rt->info[rt->rateCodeToIndex[i]].dot11Rate;
   3368       1.1  dyoung 	sc->sc_currates = rt;
   3369       1.1  dyoung 	sc->sc_curmode = mode;
   3370       1.1  dyoung }
   3371       1.1  dyoung 
   3372       1.1  dyoung /*
   3373       1.1  dyoung  * Reset the rate control state for each 802.11 state transition.
   3374       1.1  dyoung  */
   3375       1.1  dyoung static void
   3376       1.1  dyoung ath_rate_ctl_reset(struct ath_softc *sc, enum ieee80211_state state)
   3377       1.1  dyoung {
   3378       1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
   3379       1.1  dyoung 	struct ieee80211_node *ni;
   3380       1.1  dyoung 	struct ath_node *an;
   3381       1.1  dyoung 
   3382      1.18  dyoung 	if (ic->ic_opmode != IEEE80211_M_STA) {
   3383      1.18  dyoung 		/*
   3384      1.18  dyoung 		 * When operating as a station the node table holds
   3385      1.18  dyoung 		 * the AP's that were discovered during scanning.
   3386      1.18  dyoung 		 * For any other operating mode we want to reset the
   3387      1.18  dyoung 		 * tx rate state of each node.
   3388      1.18  dyoung 		 */
   3389       1.1  dyoung 		TAILQ_FOREACH(ni, &ic->ic_node, ni_list) {
   3390       1.1  dyoung 			ni->ni_txrate = 0;		/* use lowest rate */
   3391       1.1  dyoung 			an = (struct ath_node *) ni;
   3392       1.1  dyoung 			an->an_tx_ok = an->an_tx_err = an->an_tx_retr =
   3393       1.1  dyoung 			    an->an_tx_upper = 0;
   3394       1.1  dyoung 		}
   3395       1.1  dyoung 	}
   3396      1.18  dyoung 	/*
   3397      1.18  dyoung 	 * Reset local xmit state; this is really only meaningful
   3398      1.18  dyoung 	 * when operating in station or adhoc mode.
   3399      1.18  dyoung 	 */
   3400      1.18  dyoung 	ni = ic->ic_bss;
   3401      1.18  dyoung 	an = (struct ath_node *) ni;
   3402      1.18  dyoung 	an->an_tx_ok = an->an_tx_err = an->an_tx_retr = an->an_tx_upper = 0;
   3403      1.18  dyoung 	if (state == IEEE80211_S_RUN) {
   3404      1.18  dyoung 		/* start with highest negotiated rate */
   3405      1.18  dyoung 		KASSERT(ni->ni_rates.rs_nrates > 0,
   3406      1.18  dyoung 			("transition to RUN state w/ no rates!"));
   3407      1.18  dyoung 		ni->ni_txrate = ni->ni_rates.rs_nrates - 1;
   3408      1.18  dyoung 	} else {
   3409      1.18  dyoung 		/* use lowest rate */
   3410      1.18  dyoung 		ni->ni_txrate = 0;
   3411      1.18  dyoung 	}
   3412       1.1  dyoung }
   3413       1.1  dyoung 
   3414       1.1  dyoung /*
   3415       1.1  dyoung  * Examine and potentially adjust the transmit rate.
   3416       1.1  dyoung  */
   3417       1.1  dyoung static void
   3418       1.1  dyoung ath_rate_ctl(void *arg, struct ieee80211_node *ni)
   3419       1.1  dyoung {
   3420       1.1  dyoung 	struct ath_softc *sc = arg;
   3421       1.1  dyoung 	struct ath_node *an = (struct ath_node *) ni;
   3422       1.1  dyoung 	struct ieee80211_rateset *rs = &ni->ni_rates;
   3423       1.1  dyoung 	int mod = 0, orate, enough;
   3424       1.1  dyoung 
   3425       1.1  dyoung 	/*
   3426       1.1  dyoung 	 * Rate control
   3427       1.1  dyoung 	 * XXX: very primitive version.
   3428       1.1  dyoung 	 */
   3429       1.1  dyoung 	sc->sc_stats.ast_rate_calls++;
   3430       1.1  dyoung 
   3431       1.1  dyoung 	enough = (an->an_tx_ok + an->an_tx_err >= 10);
   3432       1.1  dyoung 
   3433       1.1  dyoung 	/* no packet reached -> down */
   3434       1.1  dyoung 	if (an->an_tx_err > 0 && an->an_tx_ok == 0)
   3435       1.1  dyoung 		mod = -1;
   3436       1.1  dyoung 
   3437       1.1  dyoung 	/* all packets needs retry in average -> down */
   3438       1.1  dyoung 	if (enough && an->an_tx_ok < an->an_tx_retr)
   3439       1.1  dyoung 		mod = -1;
   3440       1.1  dyoung 
   3441       1.1  dyoung 	/* no error and less than 10% of packets needs retry -> up */
   3442       1.1  dyoung 	if (enough && an->an_tx_err == 0 && an->an_tx_ok > an->an_tx_retr * 10)
   3443       1.1  dyoung 		mod = 1;
   3444       1.1  dyoung 
   3445       1.1  dyoung 	orate = ni->ni_txrate;
   3446       1.1  dyoung 	switch (mod) {
   3447       1.1  dyoung 	case 0:
   3448       1.1  dyoung 		if (enough && an->an_tx_upper > 0)
   3449       1.1  dyoung 			an->an_tx_upper--;
   3450       1.1  dyoung 		break;
   3451       1.1  dyoung 	case -1:
   3452       1.1  dyoung 		if (ni->ni_txrate > 0) {
   3453       1.1  dyoung 			ni->ni_txrate--;
   3454       1.1  dyoung 			sc->sc_stats.ast_rate_drop++;
   3455       1.1  dyoung 		}
   3456       1.1  dyoung 		an->an_tx_upper = 0;
   3457       1.1  dyoung 		break;
   3458       1.1  dyoung 	case 1:
   3459       1.1  dyoung 		if (++an->an_tx_upper < 2)
   3460       1.1  dyoung 			break;
   3461       1.1  dyoung 		an->an_tx_upper = 0;
   3462       1.1  dyoung 		if (ni->ni_txrate + 1 < rs->rs_nrates) {
   3463       1.1  dyoung 			ni->ni_txrate++;
   3464       1.1  dyoung 			sc->sc_stats.ast_rate_raise++;
   3465       1.1  dyoung 		}
   3466       1.1  dyoung 		break;
   3467       1.1  dyoung 	}
   3468       1.1  dyoung 
   3469       1.1  dyoung 	if (ni->ni_txrate != orate) {
   3470       1.7  itojun 		DPRINTF(("%s: %dM -> %dM (%d ok, %d err, %d retr)\n",
   3471       1.1  dyoung 		    __func__,
   3472       1.1  dyoung 		    (rs->rs_rates[orate] & IEEE80211_RATE_VAL) / 2,
   3473       1.1  dyoung 		    (rs->rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL) / 2,
   3474       1.7  itojun 		    an->an_tx_ok, an->an_tx_err, an->an_tx_retr));
   3475       1.1  dyoung 	}
   3476       1.1  dyoung 	if (ni->ni_txrate != orate || enough)
   3477       1.1  dyoung 		an->an_tx_ok = an->an_tx_err = an->an_tx_retr = 0;
   3478       1.1  dyoung }
   3479       1.1  dyoung 
   3480       1.1  dyoung #ifdef AR_DEBUG
   3481       1.2  dyoung #ifdef __FreeBSD__
   3482       1.1  dyoung static int
   3483       1.1  dyoung sysctl_hw_ath_dump(SYSCTL_HANDLER_ARGS)
   3484       1.1  dyoung {
   3485       1.1  dyoung 	char dmode[64];
   3486       1.1  dyoung 	int error;
   3487       1.1  dyoung 
   3488       1.1  dyoung 	strncpy(dmode, "", sizeof(dmode) - 1);
   3489       1.1  dyoung 	dmode[sizeof(dmode) - 1] = '\0';
   3490       1.1  dyoung 	error = sysctl_handle_string(oidp, &dmode[0], sizeof(dmode), req);
   3491       1.1  dyoung 
   3492       1.1  dyoung 	if (error == 0 && req->newptr != NULL) {
   3493       1.1  dyoung 		struct ifnet *ifp;
   3494       1.1  dyoung 		struct ath_softc *sc;
   3495       1.1  dyoung 
   3496       1.1  dyoung 		ifp = ifunit("ath0");		/* XXX */
   3497       1.1  dyoung 		if (!ifp)
   3498       1.1  dyoung 			return EINVAL;
   3499       1.1  dyoung 		sc = ifp->if_softc;
   3500       1.1  dyoung 		if (strcmp(dmode, "hal") == 0)
   3501       1.1  dyoung 			ath_hal_dumpstate(sc->sc_ah);
   3502       1.1  dyoung 		else
   3503       1.1  dyoung 			return EINVAL;
   3504       1.1  dyoung 	}
   3505       1.1  dyoung 	return error;
   3506       1.1  dyoung }
   3507       1.1  dyoung SYSCTL_PROC(_hw_ath, OID_AUTO, dump, CTLTYPE_STRING | CTLFLAG_RW,
   3508       1.1  dyoung 	0, 0, sysctl_hw_ath_dump, "A", "Dump driver state");
   3509       1.2  dyoung #endif /* __FreeBSD__ */
   3510       1.1  dyoung 
   3511      1.20  dyoung #if 0 /* #ifdef __NetBSD__ */
   3512      1.20  dyoung static int
   3513      1.20  dyoung sysctl_hw_ath_dump(SYSCTL_HANDLER_ARGS)
   3514      1.20  dyoung {
   3515      1.20  dyoung 	char dmode[64];
   3516      1.20  dyoung 	int error;
   3517      1.20  dyoung 
   3518      1.20  dyoung 	strncpy(dmode, "", sizeof(dmode) - 1);
   3519      1.20  dyoung 	dmode[sizeof(dmode) - 1] = '\0';
   3520      1.20  dyoung 	error = sysctl_handle_string(oidp, &dmode[0], sizeof(dmode), req);
   3521      1.20  dyoung 
   3522      1.20  dyoung 	if (error == 0 && req->newptr != NULL) {
   3523      1.20  dyoung 		struct ifnet *ifp;
   3524      1.20  dyoung 		struct ath_softc *sc;
   3525      1.20  dyoung 
   3526      1.20  dyoung 		ifp = ifunit("ath0");		/* XXX */
   3527      1.20  dyoung 		if (!ifp)
   3528      1.20  dyoung 			return EINVAL;
   3529      1.20  dyoung 		sc = ifp->if_softc;
   3530      1.20  dyoung 		if (strcmp(dmode, "hal") == 0)
   3531      1.20  dyoung 			ath_hal_dumpstate(sc->sc_ah);
   3532      1.20  dyoung 		else
   3533      1.20  dyoung 			return EINVAL;
   3534      1.20  dyoung 	}
   3535      1.20  dyoung 	return error;
   3536      1.20  dyoung }
   3537      1.20  dyoung SYSCTL_PROC(_hw_ath, OID_AUTO, dump, CTLTYPE_STRING | CTLFLAG_RW,
   3538      1.20  dyoung 	0, 0, sysctl_hw_ath_dump, "A", "Dump driver state");
   3539      1.20  dyoung #endif /* __NetBSD__ */
   3540      1.20  dyoung 
   3541       1.1  dyoung static void
   3542       1.1  dyoung ath_printrxbuf(struct ath_buf *bf, int done)
   3543       1.1  dyoung {
   3544       1.1  dyoung 	struct ath_desc *ds;
   3545       1.1  dyoung 	int i;
   3546       1.1  dyoung 
   3547       1.1  dyoung 	for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) {
   3548       1.1  dyoung 		printf("R%d (%p %p) %08x %08x %08x %08x %08x %08x %c\n",
   3549       1.1  dyoung 		    i, ds, (struct ath_desc *)bf->bf_daddr + i,
   3550       1.1  dyoung 		    ds->ds_link, ds->ds_data,
   3551       1.1  dyoung 		    ds->ds_ctl0, ds->ds_ctl1,
   3552       1.1  dyoung 		    ds->ds_hw[0], ds->ds_hw[1],
   3553       1.1  dyoung 		    !done ? ' ' : (ds->ds_rxstat.rs_status == 0) ? '*' : '!');
   3554       1.1  dyoung 	}
   3555       1.1  dyoung }
   3556       1.1  dyoung 
   3557       1.1  dyoung static void
   3558       1.1  dyoung ath_printtxbuf(struct ath_buf *bf, int done)
   3559       1.1  dyoung {
   3560       1.1  dyoung 	struct ath_desc *ds;
   3561       1.1  dyoung 	int i;
   3562       1.1  dyoung 
   3563       1.1  dyoung 	for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) {
   3564       1.1  dyoung 		printf("T%d (%p %p) %08x %08x %08x %08x %08x %08x %08x %08x %c\n",
   3565       1.1  dyoung 		    i, ds, (struct ath_desc *)bf->bf_daddr + i,
   3566       1.1  dyoung 		    ds->ds_link, ds->ds_data,
   3567       1.1  dyoung 		    ds->ds_ctl0, ds->ds_ctl1,
   3568       1.1  dyoung 		    ds->ds_hw[0], ds->ds_hw[1], ds->ds_hw[2], ds->ds_hw[3],
   3569       1.1  dyoung 		    !done ? ' ' : (ds->ds_txstat.ts_status == 0) ? '*' : '!');
   3570       1.1  dyoung 	}
   3571       1.1  dyoung }
   3572       1.1  dyoung #endif /* AR_DEBUG */
   3573