15591b213SSam Leffler /*- 25591b213SSam Leffler * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting 35591b213SSam Leffler * All rights reserved. 45591b213SSam Leffler * 55591b213SSam Leffler * Redistribution and use in source and binary forms, with or without 65591b213SSam Leffler * modification, are permitted provided that the following conditions 75591b213SSam Leffler * are met: 85591b213SSam Leffler * 1. Redistributions of source code must retain the above copyright 95591b213SSam Leffler * notice, this list of conditions and the following disclaimer, 105591b213SSam Leffler * without modification. 115591b213SSam Leffler * 2. Redistributions in binary form must reproduce at minimum a disclaimer 125591b213SSam Leffler * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 135591b213SSam Leffler * redistribution must be conditioned upon including a substantially 145591b213SSam Leffler * similar Disclaimer requirement for further binary redistribution. 155591b213SSam Leffler * 3. Neither the names of the above-listed copyright holders nor the names 165591b213SSam Leffler * of any contributors may be used to endorse or promote products derived 175591b213SSam Leffler * from this software without specific prior written permission. 185591b213SSam Leffler * 195591b213SSam Leffler * Alternatively, this software may be distributed under the terms of the 205591b213SSam Leffler * GNU General Public License ("GPL") version 2 as published by the Free 215591b213SSam Leffler * Software Foundation. 225591b213SSam Leffler * 235591b213SSam Leffler * NO WARRANTY 245591b213SSam Leffler * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 255591b213SSam Leffler * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 265591b213SSam Leffler * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 275591b213SSam Leffler * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 285591b213SSam Leffler * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 295591b213SSam Leffler * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 305591b213SSam Leffler * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 315591b213SSam Leffler * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 325591b213SSam Leffler * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 335591b213SSam Leffler * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 345591b213SSam Leffler * THE POSSIBILITY OF SUCH DAMAGES. 355591b213SSam Leffler */ 365591b213SSam Leffler 375591b213SSam Leffler #include <sys/cdefs.h> 385591b213SSam Leffler __FBSDID("$FreeBSD$"); 395591b213SSam Leffler 405591b213SSam Leffler /* 415591b213SSam Leffler * Driver for the Atheros Wireless LAN controller. 425f3721d5SSam Leffler * 435f3721d5SSam Leffler * This software is derived from work of Atsushi Onoe; his contribution 445f3721d5SSam Leffler * is greatly appreciated. 455591b213SSam Leffler */ 465591b213SSam Leffler 475591b213SSam Leffler #include "opt_inet.h" 485591b213SSam Leffler 495591b213SSam Leffler #include <sys/param.h> 505591b213SSam Leffler #include <sys/systm.h> 515591b213SSam Leffler #include <sys/sysctl.h> 525591b213SSam Leffler #include <sys/mbuf.h> 535591b213SSam Leffler #include <sys/malloc.h> 545591b213SSam Leffler #include <sys/lock.h> 555591b213SSam Leffler #include <sys/mutex.h> 565591b213SSam Leffler #include <sys/kernel.h> 575591b213SSam Leffler #include <sys/socket.h> 585591b213SSam Leffler #include <sys/sockio.h> 595591b213SSam Leffler #include <sys/errno.h> 605591b213SSam Leffler #include <sys/callout.h> 615591b213SSam Leffler #include <sys/bus.h> 625591b213SSam Leffler #include <sys/endian.h> 635591b213SSam Leffler 645591b213SSam Leffler #include <machine/bus.h> 655591b213SSam Leffler 665591b213SSam Leffler #include <net/if.h> 675591b213SSam Leffler #include <net/if_dl.h> 685591b213SSam Leffler #include <net/if_media.h> 695591b213SSam Leffler #include <net/if_arp.h> 705591b213SSam Leffler #include <net/ethernet.h> 715591b213SSam Leffler #include <net/if_llc.h> 725591b213SSam Leffler 735591b213SSam Leffler #include <net80211/ieee80211.h> 745591b213SSam Leffler #include <net80211/ieee80211_crypto.h> 755591b213SSam Leffler #include <net80211/ieee80211_node.h> 765591b213SSam Leffler #include <net80211/ieee80211_proto.h> 775591b213SSam Leffler #include <net80211/ieee80211_var.h> 785591b213SSam Leffler 795591b213SSam Leffler #include <net/bpf.h> 805591b213SSam Leffler 815591b213SSam Leffler #ifdef INET 825591b213SSam Leffler #include <netinet/in.h> 835591b213SSam Leffler #include <netinet/if_ether.h> 845591b213SSam Leffler #endif 855591b213SSam Leffler 865591b213SSam Leffler #define AR_DEBUG 875591b213SSam Leffler #include <dev/ath/if_athvar.h> 885591b213SSam Leffler #include <contrib/dev/ath/ah_desc.h> 895591b213SSam Leffler 905591b213SSam Leffler /* unalligned little endian access */ 915591b213SSam Leffler #define LE_READ_2(p) \ 925591b213SSam Leffler ((u_int16_t) \ 935591b213SSam Leffler ((((u_int8_t *)(p))[0] ) | (((u_int8_t *)(p))[1] << 8))) 945591b213SSam Leffler #define LE_READ_4(p) \ 955591b213SSam Leffler ((u_int32_t) \ 965591b213SSam Leffler ((((u_int8_t *)(p))[0] ) | (((u_int8_t *)(p))[1] << 8) | \ 975591b213SSam Leffler (((u_int8_t *)(p))[2] << 16) | (((u_int8_t *)(p))[3] << 24))) 985591b213SSam Leffler 995591b213SSam Leffler static void ath_init(void *); 1005591b213SSam Leffler static void ath_stop(struct ifnet *); 1015591b213SSam Leffler static void ath_start(struct ifnet *); 1025591b213SSam Leffler static void ath_reset(struct ath_softc *); 1035591b213SSam Leffler static int ath_media_change(struct ifnet *); 1045591b213SSam Leffler static void ath_watchdog(struct ifnet *); 1055591b213SSam Leffler static int ath_ioctl(struct ifnet *, u_long, caddr_t); 1065591b213SSam Leffler static void ath_fatal_proc(void *, int); 1075591b213SSam Leffler static void ath_rxorn_proc(void *, int); 1085591b213SSam Leffler static void ath_bmiss_proc(void *, int); 1095591b213SSam Leffler static void ath_initkeytable(struct ath_softc *); 1105591b213SSam Leffler static void ath_mode_init(struct ath_softc *); 1115591b213SSam Leffler static int ath_beacon_alloc(struct ath_softc *, struct ieee80211_node *); 1125591b213SSam Leffler static void ath_beacon_proc(void *, int); 1135591b213SSam Leffler static void ath_beacon_free(struct ath_softc *); 1145591b213SSam Leffler static void ath_beacon_config(struct ath_softc *); 1155591b213SSam Leffler static int ath_desc_alloc(struct ath_softc *); 1165591b213SSam Leffler static void ath_desc_free(struct ath_softc *); 1175591b213SSam Leffler static struct ieee80211_node *ath_node_alloc(struct ieee80211com *); 1185591b213SSam Leffler static void ath_node_free(struct ieee80211com *, struct ieee80211_node *); 1195591b213SSam Leffler static void ath_node_copy(struct ieee80211com *, 1205591b213SSam Leffler struct ieee80211_node *, const struct ieee80211_node *); 1215591b213SSam Leffler static int ath_rxbuf_init(struct ath_softc *, struct ath_buf *); 1225591b213SSam Leffler static void ath_rx_proc(void *, int); 1235591b213SSam Leffler static int ath_tx_start(struct ath_softc *, struct ieee80211_node *, 1245591b213SSam Leffler struct ath_buf *, struct mbuf *); 1255591b213SSam Leffler static void ath_tx_proc(void *, int); 1265591b213SSam Leffler static int ath_chan_set(struct ath_softc *, struct ieee80211_channel *); 1275591b213SSam Leffler static void ath_draintxq(struct ath_softc *); 1285591b213SSam Leffler static void ath_stoprecv(struct ath_softc *); 1295591b213SSam Leffler static int ath_startrecv(struct ath_softc *); 1305591b213SSam Leffler static void ath_next_scan(void *); 1315591b213SSam Leffler static void ath_calibrate(void *); 13245bbf62fSSam Leffler static int ath_newstate(struct ieee80211com *, enum ieee80211_state, int); 1335591b213SSam Leffler static void ath_newassoc(struct ieee80211com *, 1345591b213SSam Leffler struct ieee80211_node *, int); 1355591b213SSam Leffler static int ath_getchannels(struct ath_softc *, u_int cc, HAL_BOOL outdoor); 1365591b213SSam Leffler 1375591b213SSam Leffler static int ath_rate_setup(struct ath_softc *sc, u_int mode); 1385591b213SSam Leffler static void ath_setcurmode(struct ath_softc *, enum ieee80211_phymode); 1395591b213SSam Leffler static void ath_rate_ctl_reset(struct ath_softc *, enum ieee80211_state); 1405591b213SSam Leffler static void ath_rate_ctl(void *, struct ieee80211_node *); 1415591b213SSam Leffler 1425591b213SSam Leffler SYSCTL_DECL(_hw_ath); 1435591b213SSam Leffler 1445591b213SSam Leffler /* XXX validate sysctl values */ 1455591b213SSam Leffler static int ath_dwelltime = 200; /* 5 channels/second */ 1465591b213SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, dwell, CTLFLAG_RW, &ath_dwelltime, 1475591b213SSam Leffler 0, "channel dwell time (ms) for AP/station scanning"); 1485591b213SSam Leffler static int ath_calinterval = 30; /* calibrate every 30 secs */ 1495591b213SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, calibrate, CTLFLAG_RW, &ath_calinterval, 1505591b213SSam Leffler 0, "chip calibration interval (secs)"); 15145cabbdcSSam Leffler static int ath_outdoor = AH_TRUE; /* outdoor operation */ 15245cabbdcSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, outdoor, CTLFLAG_RD, &ath_outdoor, 15345cabbdcSSam Leffler 0, "enable/disable outdoor operation"); 15445cabbdcSSam Leffler static int ath_countrycode = CTRY_DEFAULT; /* country code */ 15545cabbdcSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, countrycode, CTLFLAG_RD, &ath_countrycode, 15645cabbdcSSam Leffler 0, "country code"); 15745cabbdcSSam Leffler static int ath_regdomain = 0; /* regulatory domain */ 15845cabbdcSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, regdomain, CTLFLAG_RD, &ath_regdomain, 15945cabbdcSSam Leffler 0, "regulatory domain"); 1605591b213SSam Leffler 1615591b213SSam Leffler static int ath_bmisshack = 1; 1625591b213SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, bmisshack, CTLFLAG_RW, &ath_bmisshack, 1635591b213SSam Leffler 0, "enable/disable hack to discard bmiss interrupts"); 1645591b213SSam Leffler 1655591b213SSam Leffler #ifdef AR_DEBUG 1665591b213SSam Leffler int ath_debug = 0; 1675591b213SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, debug, CTLFLAG_RW, &ath_debug, 1685591b213SSam Leffler 0, "control debugging printfs"); 1695591b213SSam Leffler #define IFF_DUMPPKTS(_ifp) \ 1705591b213SSam Leffler (ath_debug || \ 1715591b213SSam Leffler ((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2)) 1725591b213SSam Leffler static void ath_printrxbuf(struct ath_buf *bf, int); 1735591b213SSam Leffler static void ath_printtxbuf(struct ath_buf *bf, int); 1745591b213SSam Leffler #define DPRINTF(X) if (ath_debug) printf X 1755591b213SSam Leffler #define DPRINTF2(X) if (ath_debug > 1) printf X 1765591b213SSam Leffler #else 1775591b213SSam Leffler #define IFF_DUMPPKTS(_ifp) \ 1785591b213SSam Leffler (((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2)) 1795591b213SSam Leffler #define DPRINTF(X) 1805591b213SSam Leffler #define DPRINTF2(X) 1815591b213SSam Leffler #endif 1825591b213SSam Leffler 1835591b213SSam Leffler int 1845591b213SSam Leffler ath_attach(u_int16_t devid, struct ath_softc *sc) 1855591b213SSam Leffler { 1865591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 1875591b213SSam Leffler struct ifnet *ifp = &ic->ic_if; 1885591b213SSam Leffler struct ath_hal *ah; 1895591b213SSam Leffler HAL_STATUS status; 1905591b213SSam Leffler int error = 0; 1915591b213SSam Leffler 1925591b213SSam Leffler DPRINTF(("ath_attach: devid 0x%x\n", devid)); 1935591b213SSam Leffler 1945591b213SSam Leffler /* set these up early for if_printf use */ 1955591b213SSam Leffler ifp->if_unit = device_get_unit(sc->sc_dev); 1965591b213SSam Leffler ifp->if_name = "ath"; 1975591b213SSam Leffler 1985591b213SSam Leffler ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh, &status); 1995591b213SSam Leffler if (ah == NULL) { 2005591b213SSam Leffler if_printf(ifp, "unable to attach hardware; HAL status %u\n", 2015591b213SSam Leffler status); 2025591b213SSam Leffler error = ENXIO; 2035591b213SSam Leffler goto bad; 2045591b213SSam Leffler } 2055591b213SSam Leffler sc->sc_ah = ah; 2065591b213SSam Leffler 2075591b213SSam Leffler /* 2085591b213SSam Leffler * Collect the channel list using the default country 2095591b213SSam Leffler * code and including outdoor channels. The 802.11 layer 21045cabbdcSSam Leffler * is resposible for filtering this list based on settings 21145cabbdcSSam Leffler * like the phy mode. 2125591b213SSam Leffler */ 21345cabbdcSSam Leffler error = ath_getchannels(sc, ath_countrycode, ath_outdoor); 2145591b213SSam Leffler if (error != 0) 2155591b213SSam Leffler goto bad; 21645cabbdcSSam Leffler /* 21745cabbdcSSam Leffler * Copy these back; they are set as a side effect 21845cabbdcSSam Leffler * of constructing the channel list. 21945cabbdcSSam Leffler */ 22045cabbdcSSam Leffler ath_regdomain = ath_hal_getregdomain(ah); 22145cabbdcSSam Leffler ath_countrycode = ath_hal_getcountrycode(ah); 2225591b213SSam Leffler 2235591b213SSam Leffler /* 2245591b213SSam Leffler * Setup rate tables for all potential media types. 2255591b213SSam Leffler */ 2265591b213SSam Leffler ath_rate_setup(sc, IEEE80211_MODE_11A); 2275591b213SSam Leffler ath_rate_setup(sc, IEEE80211_MODE_11B); 2285591b213SSam Leffler ath_rate_setup(sc, IEEE80211_MODE_11G); 2295591b213SSam Leffler ath_rate_setup(sc, IEEE80211_MODE_TURBO); 2305591b213SSam Leffler 2315591b213SSam Leffler error = ath_desc_alloc(sc); 2325591b213SSam Leffler if (error != 0) { 2335591b213SSam Leffler if_printf(ifp, "failed to allocate descriptors: %d\n", error); 2345591b213SSam Leffler goto bad; 2355591b213SSam Leffler } 2365591b213SSam Leffler callout_init(&sc->sc_scan_ch, 0); 2375591b213SSam Leffler callout_init(&sc->sc_cal_ch, 0); 2385591b213SSam Leffler 2395591b213SSam Leffler mtx_init(&sc->sc_txbuflock, 2405591b213SSam Leffler device_get_nameunit(sc->sc_dev), "xmit buf q", MTX_DEF); 2415591b213SSam Leffler mtx_init(&sc->sc_txqlock, 2425591b213SSam Leffler device_get_nameunit(sc->sc_dev), "xmit q", MTX_DEF); 2435591b213SSam Leffler 2445591b213SSam Leffler TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc, sc); 2455591b213SSam Leffler TASK_INIT(&sc->sc_rxtask, 0, ath_rx_proc, sc); 2465591b213SSam Leffler TASK_INIT(&sc->sc_swbatask, 0, ath_beacon_proc, sc); 2475591b213SSam Leffler TASK_INIT(&sc->sc_rxorntask, 0, ath_rxorn_proc, sc); 2485591b213SSam Leffler TASK_INIT(&sc->sc_fataltask, 0, ath_fatal_proc, sc); 2495591b213SSam Leffler TASK_INIT(&sc->sc_bmisstask, 0, ath_bmiss_proc, sc); 2505591b213SSam Leffler 2515591b213SSam Leffler /* 2525591b213SSam Leffler * For now just pre-allocate one data queue and one 2535591b213SSam Leffler * beacon queue. Note that the HAL handles resetting 2545591b213SSam Leffler * them at the needed time. Eventually we'll want to 2555591b213SSam Leffler * allocate more tx queues for splitting management 2565591b213SSam Leffler * frames and for QOS support. 2575591b213SSam Leffler */ 2585591b213SSam Leffler sc->sc_txhalq = ath_hal_setuptxqueue(ah, 2595591b213SSam Leffler HAL_TX_QUEUE_DATA, 2605591b213SSam Leffler AH_TRUE /* enable interrupts */ 2615591b213SSam Leffler ); 2625591b213SSam Leffler if (sc->sc_txhalq == (u_int) -1) { 2635591b213SSam Leffler if_printf(ifp, "unable to setup a data xmit queue!\n"); 2645591b213SSam Leffler goto bad; 2655591b213SSam Leffler } 2665591b213SSam Leffler sc->sc_bhalq = ath_hal_setuptxqueue(ah, 2675591b213SSam Leffler HAL_TX_QUEUE_BEACON, 2685591b213SSam Leffler AH_TRUE /* enable interrupts */ 2695591b213SSam Leffler ); 2705591b213SSam Leffler if (sc->sc_bhalq == (u_int) -1) { 2715591b213SSam Leffler if_printf(ifp, "unable to setup a beacon xmit queue!\n"); 2725591b213SSam Leffler goto bad; 2735591b213SSam Leffler } 2745591b213SSam Leffler 2755591b213SSam Leffler ifp->if_softc = sc; 2765591b213SSam Leffler ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST; 2775591b213SSam Leffler ifp->if_start = ath_start; 2785591b213SSam Leffler ifp->if_watchdog = ath_watchdog; 2795591b213SSam Leffler ifp->if_ioctl = ath_ioctl; 2805591b213SSam Leffler ifp->if_init = ath_init; 2815591b213SSam Leffler ifp->if_snd.ifq_maxlen = IFQ_MAXLEN; 2825591b213SSam Leffler 2835591b213SSam Leffler ic->ic_softc = sc; 2845591b213SSam Leffler ic->ic_newassoc = ath_newassoc; 2855591b213SSam Leffler /* XXX not right but it's not used anywhere important */ 2865591b213SSam Leffler ic->ic_phytype = IEEE80211_T_OFDM; 2875591b213SSam Leffler ic->ic_opmode = IEEE80211_M_STA; 2885591b213SSam Leffler ic->ic_caps = IEEE80211_C_WEP | IEEE80211_C_IBSS | IEEE80211_C_HOSTAP; 2895591b213SSam Leffler /* NB: 11g support is identified when we fetch the channel set */ 2905591b213SSam Leffler if (sc->sc_have11g) 2915591b213SSam Leffler ic->ic_caps |= IEEE80211_C_SHPREAMBLE; 2925591b213SSam Leffler 2935591b213SSam Leffler /* get mac address from hardware */ 2945591b213SSam Leffler ath_hal_getmac(ah, ic->ic_myaddr); 2955591b213SSam Leffler 2965591b213SSam Leffler /* call MI attach routine. */ 2975591b213SSam Leffler ieee80211_ifattach(ifp); 2985591b213SSam Leffler /* override default methods */ 2995591b213SSam Leffler ic->ic_node_alloc = ath_node_alloc; 3005591b213SSam Leffler ic->ic_node_free = ath_node_free; 3015591b213SSam Leffler ic->ic_node_copy = ath_node_copy; 30245bbf62fSSam Leffler sc->sc_newstate = ic->ic_newstate; 30345bbf62fSSam Leffler ic->ic_newstate = ath_newstate; 30445bbf62fSSam Leffler /* complete initialization */ 3055591b213SSam Leffler ieee80211_media_init(ifp, ath_media_change, ieee80211_media_status); 3065591b213SSam Leffler 3075591b213SSam Leffler if_printf(ifp, "802.11 address: %s\n", ether_sprintf(ic->ic_myaddr)); 3085591b213SSam Leffler 3095591b213SSam Leffler return 0; 3105591b213SSam Leffler bad: 3115591b213SSam Leffler if (ah) 3125591b213SSam Leffler ath_hal_detach(ah); 3135591b213SSam Leffler sc->sc_invalid = 1; 3145591b213SSam Leffler return error; 3155591b213SSam Leffler } 3165591b213SSam Leffler 3175591b213SSam Leffler int 3185591b213SSam Leffler ath_detach(struct ath_softc *sc) 3195591b213SSam Leffler { 3205591b213SSam Leffler struct ifnet *ifp = &sc->sc_ic.ic_if; 3215591b213SSam Leffler 3225591b213SSam Leffler DPRINTF(("ath_detach: if_flags %x\n", ifp->if_flags)); 3235591b213SSam Leffler 3245591b213SSam Leffler mtx_lock(&sc->sc_mtx); 3255591b213SSam Leffler ath_stop(ifp); 3265591b213SSam Leffler ath_desc_free(sc); 3275591b213SSam Leffler ath_hal_detach(sc->sc_ah); 3285591b213SSam Leffler ieee80211_ifdetach(ifp); 3295591b213SSam Leffler mtx_unlock(&sc->sc_mtx); 3305591b213SSam Leffler return 0; 3315591b213SSam Leffler } 3325591b213SSam Leffler 3335591b213SSam Leffler void 3345591b213SSam Leffler ath_suspend(struct ath_softc *sc) 3355591b213SSam Leffler { 3365591b213SSam Leffler struct ifnet *ifp = &sc->sc_ic.ic_if; 3375591b213SSam Leffler 3385591b213SSam Leffler DPRINTF(("ath_suspend: if_flags %x\n", ifp->if_flags)); 3395591b213SSam Leffler 3405591b213SSam Leffler ath_stop(ifp); 3415591b213SSam Leffler } 3425591b213SSam Leffler 3435591b213SSam Leffler void 3445591b213SSam Leffler ath_resume(struct ath_softc *sc) 3455591b213SSam Leffler { 3465591b213SSam Leffler struct ifnet *ifp = &sc->sc_ic.ic_if; 3475591b213SSam Leffler 3485591b213SSam Leffler DPRINTF(("ath_resume: if_flags %x\n", ifp->if_flags)); 3495591b213SSam Leffler 3505591b213SSam Leffler ath_init(ifp); 3515591b213SSam Leffler if (ifp->if_flags & IFF_UP) 3525591b213SSam Leffler ath_start(ifp); 3535591b213SSam Leffler } 3545591b213SSam Leffler 3555591b213SSam Leffler void 3565591b213SSam Leffler ath_shutdown(struct ath_softc *sc) 3575591b213SSam Leffler { 3585591b213SSam Leffler struct ifnet *ifp = &sc->sc_ic.ic_if; 3595591b213SSam Leffler 3605591b213SSam Leffler DPRINTF(("ath_shutdown: if_flags %x\n", ifp->if_flags)); 3615591b213SSam Leffler 3625591b213SSam Leffler ath_stop(ifp); 3635591b213SSam Leffler } 3645591b213SSam Leffler 3655591b213SSam Leffler void 3665591b213SSam Leffler ath_intr(void *arg) 3675591b213SSam Leffler { 3685591b213SSam Leffler struct ath_softc *sc = arg; 3695591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 3705591b213SSam Leffler struct ifnet *ifp = &ic->ic_if; 3715591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 3725591b213SSam Leffler HAL_INT status; 3735591b213SSam Leffler 3745591b213SSam Leffler if (sc->sc_invalid) { 3755591b213SSam Leffler /* 3765591b213SSam Leffler * The hardware is gone, don't touch anything. 3775591b213SSam Leffler * XXX can this happen? 3785591b213SSam Leffler */ 3795591b213SSam Leffler DPRINTF(("ath_intr: invalid; ignored\n")); 3805591b213SSam Leffler return; 3815591b213SSam Leffler } 3825591b213SSam Leffler if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) != (IFF_RUNNING|IFF_UP)) { 3835591b213SSam Leffler DPRINTF(("ath_intr: if_flags 0x%x\n", ifp->if_flags)); 3845591b213SSam Leffler ath_hal_getisr(ah, &status); /* clear ISR */ 3855591b213SSam Leffler ath_hal_intrset(ah, 0); /* disable further intr's */ 3865591b213SSam Leffler return; 3875591b213SSam Leffler } 3885591b213SSam Leffler ath_hal_getisr(ah, &status); /* NB: clears ISR too */ 3895591b213SSam Leffler DPRINTF2(("ath_intr: status 0x%x\n", status)); 3905591b213SSam Leffler if (ath_bmisshack) status &= ~HAL_INT_BMISS; /*XXX*/ 3915591b213SSam Leffler #ifdef AR_DEBUG 3925591b213SSam Leffler if (ath_debug && 3935591b213SSam Leffler (status & (HAL_INT_FATAL|HAL_INT_RXORN|HAL_INT_BMISS))) { 3945591b213SSam Leffler if_printf(ifp, "ath_intr: status 0x%x\n", status); 3955591b213SSam Leffler ath_hal_dumpstate(ah); 3965591b213SSam Leffler } 3975591b213SSam Leffler #endif /* AR_DEBUG */ 3985591b213SSam Leffler if (status & HAL_INT_FATAL) { 3995591b213SSam Leffler sc->sc_stats.ast_hardware++; 4005591b213SSam Leffler ath_hal_intrset(ah, 0); /* disable intr's until reset */ 4015591b213SSam Leffler taskqueue_enqueue(taskqueue_swi, &sc->sc_fataltask); 4025591b213SSam Leffler } else if (status & HAL_INT_RXORN) { 4035591b213SSam Leffler sc->sc_stats.ast_rxorn++; 4045591b213SSam Leffler ath_hal_intrset(ah, 0); /* disable intr's until reset */ 4055591b213SSam Leffler taskqueue_enqueue(taskqueue_swi, &sc->sc_rxorntask); 4065591b213SSam Leffler } else { 4075591b213SSam Leffler if (status & HAL_INT_RXEOL) { 4085591b213SSam Leffler /* 4095591b213SSam Leffler * NB: the hardware should re-read the link when 4105591b213SSam Leffler * RXE bit is written, but it doesn't work at 4115591b213SSam Leffler * least on older hardware revs. 4125591b213SSam Leffler */ 4135591b213SSam Leffler sc->sc_stats.ast_rxeol++; 4145591b213SSam Leffler sc->sc_rxlink = NULL; 4155591b213SSam Leffler } 4165591b213SSam Leffler if (status & HAL_INT_TXURN) { 4175591b213SSam Leffler sc->sc_stats.ast_txurn++; 4185591b213SSam Leffler /* bump tx trigger level */ 4195591b213SSam Leffler ath_hal_updatetxtriglevel(ah, AH_TRUE); 4205591b213SSam Leffler } 4215591b213SSam Leffler if (status & HAL_INT_RX) 4225591b213SSam Leffler taskqueue_enqueue(taskqueue_swi, &sc->sc_rxtask); 4235591b213SSam Leffler if (status & HAL_INT_TX) 4245591b213SSam Leffler taskqueue_enqueue(taskqueue_swi, &sc->sc_txtask); 4255591b213SSam Leffler if (status & HAL_INT_SWBA) 4265591b213SSam Leffler taskqueue_enqueue(taskqueue_swi, &sc->sc_swbatask); 4275591b213SSam Leffler if (status & HAL_INT_BMISS) { 4285591b213SSam Leffler sc->sc_stats.ast_bmiss++; 4295591b213SSam Leffler taskqueue_enqueue(taskqueue_swi, &sc->sc_bmisstask); 4305591b213SSam Leffler } 4315591b213SSam Leffler } 4325591b213SSam Leffler } 4335591b213SSam Leffler 4345591b213SSam Leffler static void 4355591b213SSam Leffler ath_fatal_proc(void *arg, int pending) 4365591b213SSam Leffler { 4375591b213SSam Leffler struct ath_softc *sc = arg; 4385591b213SSam Leffler 4395591b213SSam Leffler device_printf(sc->sc_dev, "hardware error; resetting\n"); 4405591b213SSam Leffler ath_reset(sc); 4415591b213SSam Leffler } 4425591b213SSam Leffler 4435591b213SSam Leffler static void 4445591b213SSam Leffler ath_rxorn_proc(void *arg, int pending) 4455591b213SSam Leffler { 4465591b213SSam Leffler struct ath_softc *sc = arg; 4475591b213SSam Leffler 4485591b213SSam Leffler device_printf(sc->sc_dev, "rx FIFO overrun; resetting\n"); 4495591b213SSam Leffler ath_reset(sc); 4505591b213SSam Leffler } 4515591b213SSam Leffler 4525591b213SSam Leffler static void 4535591b213SSam Leffler ath_bmiss_proc(void *arg, int pending) 4545591b213SSam Leffler { 4555591b213SSam Leffler struct ath_softc *sc = arg; 4565591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 4575591b213SSam Leffler 4585591b213SSam Leffler DPRINTF(("ath_bmiss_proc: pending %u\n", pending)); 4595591b213SSam Leffler KASSERT(ic->ic_opmode == IEEE80211_M_STA, 4605591b213SSam Leffler ("unexpect operating mode %u", ic->ic_opmode)); 4615591b213SSam Leffler if (ic->ic_state == IEEE80211_S_RUN) 46245bbf62fSSam Leffler ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 4635591b213SSam Leffler } 4645591b213SSam Leffler 4655591b213SSam Leffler static u_int 4665591b213SSam Leffler ath_chan2flags(struct ieee80211com *ic, struct ieee80211_channel *chan) 4675591b213SSam Leffler { 4685591b213SSam Leffler static const u_int modeflags[] = { 4695591b213SSam Leffler 0, /* IEEE80211_MODE_AUTO */ 4705591b213SSam Leffler CHANNEL_A, /* IEEE80211_MODE_11A */ 4715591b213SSam Leffler CHANNEL_B, /* IEEE80211_MODE_11B */ 4725591b213SSam Leffler CHANNEL_PUREG, /* IEEE80211_MODE_11G */ 4735591b213SSam Leffler CHANNEL_T /* IEEE80211_MODE_TURBO */ 4745591b213SSam Leffler }; 4755591b213SSam Leffler return modeflags[ieee80211_chan2mode(ic, chan)]; 4765591b213SSam Leffler } 4775591b213SSam Leffler 4785591b213SSam Leffler static void 4795591b213SSam Leffler ath_init(void *arg) 4805591b213SSam Leffler { 4815591b213SSam Leffler struct ath_softc *sc = (struct ath_softc *) arg; 4825591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 4835591b213SSam Leffler struct ifnet *ifp = &ic->ic_if; 4845591b213SSam Leffler struct ieee80211_node *ni; 4855591b213SSam Leffler enum ieee80211_phymode mode; 4865591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 4875591b213SSam Leffler HAL_STATUS status; 4885591b213SSam Leffler HAL_CHANNEL hchan; 4895591b213SSam Leffler 4905591b213SSam Leffler DPRINTF(("ath_init: if_flags 0x%x\n", ifp->if_flags)); 4915591b213SSam Leffler 4925591b213SSam Leffler mtx_lock(&sc->sc_mtx); 4935591b213SSam Leffler /* 4945591b213SSam Leffler * Stop anything previously setup. This is safe 4955591b213SSam Leffler * whether this is the first time through or not. 4965591b213SSam Leffler */ 4975591b213SSam Leffler ath_stop(ifp); 4985591b213SSam Leffler 4995591b213SSam Leffler /* 5005591b213SSam Leffler * The basic interface to setting the hardware in a good 5015591b213SSam Leffler * state is ``reset''. On return the hardware is known to 5025591b213SSam Leffler * be powered up and with interrupts disabled. This must 5035591b213SSam Leffler * be followed by initialization of the appropriate bits 5045591b213SSam Leffler * and then setup of the interrupt mask. 5055591b213SSam Leffler */ 5065591b213SSam Leffler hchan.channel = ic->ic_ibss_chan->ic_freq; 5075591b213SSam Leffler hchan.channelFlags = ath_chan2flags(ic, ic->ic_ibss_chan); 5085591b213SSam Leffler if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_FALSE, &status)) { 5095591b213SSam Leffler if_printf(ifp, "unable to reset hardware; hal status %u\n", 5105591b213SSam Leffler status); 5115591b213SSam Leffler goto done; 5125591b213SSam Leffler } 5135591b213SSam Leffler 5145591b213SSam Leffler /* 5155591b213SSam Leffler * Setup the hardware after reset: the key cache 5165591b213SSam Leffler * is filled as needed and the receive engine is 5175591b213SSam Leffler * set going. Frame transmit is handled entirely 5185591b213SSam Leffler * in the frame output path; there's nothing to do 5195591b213SSam Leffler * here except setup the interrupt mask. 5205591b213SSam Leffler */ 5215591b213SSam Leffler if (ic->ic_flags & IEEE80211_F_WEPON) 5225591b213SSam Leffler ath_initkeytable(sc); 5235591b213SSam Leffler if (ath_startrecv(sc) != 0) { 5245591b213SSam Leffler if_printf(ifp, "unable to start recv logic\n"); 5255591b213SSam Leffler goto done; 5265591b213SSam Leffler } 5275591b213SSam Leffler 5285591b213SSam Leffler /* 5295591b213SSam Leffler * Enable interrupts. 5305591b213SSam Leffler */ 5315591b213SSam Leffler sc->sc_imask = HAL_INT_RX | HAL_INT_TX 5325591b213SSam Leffler | HAL_INT_RXEOL | HAL_INT_RXORN 5335591b213SSam Leffler | HAL_INT_FATAL | HAL_INT_GLOBAL; 5345591b213SSam Leffler ath_hal_intrset(ah, sc->sc_imask); 5355591b213SSam Leffler 5365591b213SSam Leffler ifp->if_flags |= IFF_RUNNING; 5375591b213SSam Leffler ic->ic_state = IEEE80211_S_INIT; 5385591b213SSam Leffler 5395591b213SSam Leffler /* 5405591b213SSam Leffler * The hardware should be ready to go now so it's safe 5415591b213SSam Leffler * to kick the 802.11 state machine as it's likely to 5425591b213SSam Leffler * immediately call back to us to send mgmt frames. 5435591b213SSam Leffler */ 5445591b213SSam Leffler ni = ic->ic_bss; 5455591b213SSam Leffler ni->ni_chan = ic->ic_ibss_chan; 5465591b213SSam Leffler mode = ieee80211_chan2mode(ic, ni->ni_chan); 5475591b213SSam Leffler if (mode != sc->sc_curmode) 5485591b213SSam Leffler ath_setcurmode(sc, mode); 54945bbf62fSSam Leffler ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 5505591b213SSam Leffler done: 5515591b213SSam Leffler mtx_unlock(&sc->sc_mtx); 5525591b213SSam Leffler } 5535591b213SSam Leffler 5545591b213SSam Leffler static void 5555591b213SSam Leffler ath_stop(struct ifnet *ifp) 5565591b213SSam Leffler { 55745bbf62fSSam Leffler struct ieee80211com *ic = (struct ieee80211com *) ifp; 5585591b213SSam Leffler struct ath_softc *sc = ifp->if_softc; 5595591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 5605591b213SSam Leffler 5615591b213SSam Leffler DPRINTF(("ath_stop: invalid %u if_flags 0x%x\n", 5625591b213SSam Leffler sc->sc_invalid, ifp->if_flags)); 5635591b213SSam Leffler 5645591b213SSam Leffler mtx_lock(&sc->sc_mtx); 5655591b213SSam Leffler if (ifp->if_flags & IFF_RUNNING) { 5665591b213SSam Leffler /* 5675591b213SSam Leffler * Shutdown the hardware and driver: 5685591b213SSam Leffler * disable interrupts 5695591b213SSam Leffler * turn off timers 5705591b213SSam Leffler * clear transmit machinery 5715591b213SSam Leffler * clear receive machinery 5725591b213SSam Leffler * drain and release tx queues 5735591b213SSam Leffler * reclaim beacon resources 5745591b213SSam Leffler * reset 802.11 state machine 5755591b213SSam Leffler * power down hardware 5765591b213SSam Leffler * 5775591b213SSam Leffler * Note that some of this work is not possible if the 5785591b213SSam Leffler * hardware is gone (invalid). 5795591b213SSam Leffler */ 5805591b213SSam Leffler ifp->if_flags &= ~IFF_RUNNING; 5815591b213SSam Leffler ifp->if_timer = 0; 5825591b213SSam Leffler if (!sc->sc_invalid) 5835591b213SSam Leffler ath_hal_intrset(ah, 0); 5845591b213SSam Leffler ath_draintxq(sc); 5855591b213SSam Leffler if (!sc->sc_invalid) 5865591b213SSam Leffler ath_stoprecv(sc); 5875591b213SSam Leffler else 5885591b213SSam Leffler sc->sc_rxlink = NULL; 5895591b213SSam Leffler IF_DRAIN(&ifp->if_snd); 5905591b213SSam Leffler ath_beacon_free(sc); 59145bbf62fSSam Leffler ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 5925591b213SSam Leffler if (!sc->sc_invalid) 5935591b213SSam Leffler ath_hal_setpower(ah, HAL_PM_FULL_SLEEP, 0); 5945591b213SSam Leffler } 5955591b213SSam Leffler mtx_unlock(&sc->sc_mtx); 5965591b213SSam Leffler } 5975591b213SSam Leffler 5985591b213SSam Leffler /* 5995591b213SSam Leffler * Reset the hardware w/o losing operational state. This is 6005591b213SSam Leffler * basically a more efficient way of doing ath_stop, ath_init, 6015591b213SSam Leffler * followed by state transitions to the current 802.11 6025591b213SSam Leffler * operational state. Used to recover from errors rx overrun 6035591b213SSam Leffler * and to reset the hardware when rf gain settings must be reset. 6045591b213SSam Leffler */ 6055591b213SSam Leffler static void 6065591b213SSam Leffler ath_reset(struct ath_softc *sc) 6075591b213SSam Leffler { 6085591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 6095591b213SSam Leffler struct ifnet *ifp = &ic->ic_if; 6105591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 6115591b213SSam Leffler struct ieee80211_channel *c; 6125591b213SSam Leffler HAL_STATUS status; 6135591b213SSam Leffler HAL_CHANNEL hchan; 6145591b213SSam Leffler 6155591b213SSam Leffler /* 6165591b213SSam Leffler * Convert to a HAL channel description with the flags 6175591b213SSam Leffler * constrained to reflect the current operating mode. 6185591b213SSam Leffler */ 6195591b213SSam Leffler c = ic->ic_ibss_chan; 6205591b213SSam Leffler hchan.channel = c->ic_freq; 6215591b213SSam Leffler hchan.channelFlags = ath_chan2flags(ic, c); 6225591b213SSam Leffler 6235591b213SSam Leffler ath_hal_intrset(ah, 0); /* disable interrupts */ 6245591b213SSam Leffler ath_draintxq(sc); /* stop xmit side */ 6255591b213SSam Leffler ath_stoprecv(sc); /* stop recv side */ 6265591b213SSam Leffler /* NB: indicate channel change so we do a full reset */ 6275591b213SSam Leffler if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_TRUE, &status)) 6285591b213SSam Leffler if_printf(ifp, "%s: unable to reset hardware; hal status %u\n", 6295591b213SSam Leffler __func__, status); 6305591b213SSam Leffler ath_hal_intrset(ah, sc->sc_imask); 6315591b213SSam Leffler if (ath_startrecv(sc) != 0) /* restart recv */ 6325591b213SSam Leffler if_printf(ifp, "%s: unable to start recv logic\n", __func__); 6335591b213SSam Leffler ath_start(ifp); /* restart xmit */ 6345591b213SSam Leffler if (ic->ic_state == IEEE80211_S_RUN) 6355591b213SSam Leffler ath_beacon_config(sc); /* restart beacons */ 6365591b213SSam Leffler } 6375591b213SSam Leffler 6385591b213SSam Leffler static void 6395591b213SSam Leffler ath_start(struct ifnet *ifp) 6405591b213SSam Leffler { 6415591b213SSam Leffler struct ath_softc *sc = ifp->if_softc; 6425591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 6435591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 6445591b213SSam Leffler struct ieee80211_node *ni; 6455591b213SSam Leffler struct ath_buf *bf; 6465591b213SSam Leffler struct mbuf *m; 6475591b213SSam Leffler struct ieee80211_frame *wh; 6485591b213SSam Leffler 6495591b213SSam Leffler if ((ifp->if_flags & IFF_RUNNING) == 0 || sc->sc_invalid) 6505591b213SSam Leffler return; 6515591b213SSam Leffler for (;;) { 6525591b213SSam Leffler /* 6535591b213SSam Leffler * Grab a TX buffer and associated resources. 6545591b213SSam Leffler */ 6555591b213SSam Leffler mtx_lock(&sc->sc_txbuflock); 6565591b213SSam Leffler bf = TAILQ_FIRST(&sc->sc_txbuf); 6575591b213SSam Leffler if (bf != NULL) 6585591b213SSam Leffler TAILQ_REMOVE(&sc->sc_txbuf, bf, bf_list); 6595591b213SSam Leffler mtx_unlock(&sc->sc_txbuflock); 6605591b213SSam Leffler if (bf == NULL) { 6615591b213SSam Leffler DPRINTF(("ath_start: out of xmit buffers\n")); 6625591b213SSam Leffler sc->sc_stats.ast_tx_qstop++; 6635591b213SSam Leffler ifp->if_flags |= IFF_OACTIVE; 6645591b213SSam Leffler break; 6655591b213SSam Leffler } 6665591b213SSam Leffler /* 6675591b213SSam Leffler * Poll the management queue for frames; they 6685591b213SSam Leffler * have priority over normal data frames. 6695591b213SSam Leffler */ 6705591b213SSam Leffler IF_DEQUEUE(&ic->ic_mgtq, m); 6715591b213SSam Leffler if (m == NULL) { 6725591b213SSam Leffler /* 6735591b213SSam Leffler * No data frames go out unless we're associated. 6745591b213SSam Leffler */ 6755591b213SSam Leffler if (ic->ic_state != IEEE80211_S_RUN) { 6765591b213SSam Leffler DPRINTF(("ath_start: ignore data packet, " 6775591b213SSam Leffler "state %u\n", ic->ic_state)); 6785591b213SSam Leffler sc->sc_stats.ast_tx_discard++; 6795591b213SSam Leffler mtx_lock(&sc->sc_txbuflock); 6805591b213SSam Leffler TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list); 6815591b213SSam Leffler mtx_unlock(&sc->sc_txbuflock); 6825591b213SSam Leffler break; 6835591b213SSam Leffler } 6845591b213SSam Leffler IF_DEQUEUE(&ifp->if_snd, m); 6855591b213SSam Leffler if (m == NULL) { 6865591b213SSam Leffler mtx_lock(&sc->sc_txbuflock); 6875591b213SSam Leffler TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list); 6885591b213SSam Leffler mtx_unlock(&sc->sc_txbuflock); 6895591b213SSam Leffler break; 6905591b213SSam Leffler } 6915591b213SSam Leffler ifp->if_opackets++; 6925591b213SSam Leffler BPF_MTAP(ifp, m); 6935591b213SSam Leffler /* 6945591b213SSam Leffler * Encapsulate the packet in prep for transmission. 6955591b213SSam Leffler */ 6965591b213SSam Leffler m = ieee80211_encap(ifp, m); 6975591b213SSam Leffler if (m == NULL) { 6985591b213SSam Leffler DPRINTF(("ath_start: encapsulation failure\n")); 6995591b213SSam Leffler sc->sc_stats.ast_tx_encap++; 7005591b213SSam Leffler goto bad; 7015591b213SSam Leffler } 7025591b213SSam Leffler wh = mtod(m, struct ieee80211_frame *); 7035591b213SSam Leffler if (ic->ic_flags & IEEE80211_F_WEPON) 7045591b213SSam Leffler wh->i_fc[1] |= IEEE80211_FC1_WEP; 7055591b213SSam Leffler } else { 7065591b213SSam Leffler wh = mtod(m, struct ieee80211_frame *); 7075591b213SSam Leffler if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == 7085591b213SSam Leffler IEEE80211_FC0_SUBTYPE_PROBE_RESP) { 7095591b213SSam Leffler /* fill time stamp */ 7105591b213SSam Leffler u_int64_t tsf; 7115591b213SSam Leffler u_int32_t *tstamp; 7125591b213SSam Leffler 7135591b213SSam Leffler tsf = ath_hal_gettsf64(ah); 7145591b213SSam Leffler /* XXX: adjust 100us delay to xmit */ 7155591b213SSam Leffler tsf += 100; 7165591b213SSam Leffler tstamp = (u_int32_t *)&wh[1]; 7175591b213SSam Leffler tstamp[0] = htole32(tsf & 0xffffffff); 7185591b213SSam Leffler tstamp[1] = htole32(tsf >> 32); 7195591b213SSam Leffler } 7205591b213SSam Leffler sc->sc_stats.ast_tx_mgmt++; 7215591b213SSam Leffler } 7225591b213SSam Leffler if (ic->ic_rawbpf) 7235591b213SSam Leffler bpf_mtap(ic->ic_rawbpf, m); 7245591b213SSam Leffler 7255591b213SSam Leffler if (ic->ic_opmode != IEEE80211_M_STA) { 7265591b213SSam Leffler ni = ieee80211_find_node(ic, wh->i_addr1); 7275591b213SSam Leffler if (ni == NULL) { 7285591b213SSam Leffler /* 7295591b213SSam Leffler * When not in station mode the destination 7305591b213SSam Leffler * address should always be in the node table 7315591b213SSam Leffler * unless this is a multicast/broadcast frame. 7325591b213SSam Leffler */ 7335591b213SSam Leffler if (!IEEE80211_IS_MULTICAST(wh->i_addr1) && 7345591b213SSam Leffler (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == 7355591b213SSam Leffler IEEE80211_FC0_TYPE_DATA) { 7365591b213SSam Leffler m_freem(m); 7375591b213SSam Leffler sc->sc_stats.ast_tx_nonode++; 7385591b213SSam Leffler goto bad; 7395591b213SSam Leffler } 7405591b213SSam Leffler ni = ic->ic_bss; 7415591b213SSam Leffler } 7425591b213SSam Leffler } else 7435591b213SSam Leffler ni = ic->ic_bss; 7445591b213SSam Leffler 7455591b213SSam Leffler /* 7465591b213SSam Leffler * TODO: 7475591b213SSam Leffler * The duration field of 802.11 header should be filled. 7485591b213SSam Leffler * XXX This may be done in the ieee80211 layer, but the upper 7495591b213SSam Leffler * doesn't know the detail of parameters such as IFS 7505591b213SSam Leffler * for now.. 7515591b213SSam Leffler */ 7525591b213SSam Leffler 7535591b213SSam Leffler if (IFF_DUMPPKTS(ifp)) 7545591b213SSam Leffler ieee80211_dump_pkt(mtod(m, u_int8_t *), m->m_len, 7555591b213SSam Leffler ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL, 7565591b213SSam Leffler -1); 7575591b213SSam Leffler 7585591b213SSam Leffler if (ath_tx_start(sc, ni, bf, m)) { 7595591b213SSam Leffler bad: 7605591b213SSam Leffler mtx_lock(&sc->sc_txbuflock); 7615591b213SSam Leffler TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list); 7625591b213SSam Leffler mtx_unlock(&sc->sc_txbuflock); 7635591b213SSam Leffler ifp->if_oerrors++; 7645591b213SSam Leffler continue; 7655591b213SSam Leffler } 7665591b213SSam Leffler 7675591b213SSam Leffler sc->sc_tx_timer = 5; 7685591b213SSam Leffler ifp->if_timer = 1; 7695591b213SSam Leffler } 7705591b213SSam Leffler } 7715591b213SSam Leffler 7725591b213SSam Leffler static int 7735591b213SSam Leffler ath_media_change(struct ifnet *ifp) 7745591b213SSam Leffler { 7755591b213SSam Leffler int error; 7765591b213SSam Leffler 7775591b213SSam Leffler error = ieee80211_media_change(ifp); 7785591b213SSam Leffler if (error == ENETRESET) { 7795591b213SSam Leffler if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) == 7805591b213SSam Leffler (IFF_RUNNING|IFF_UP)) 7815591b213SSam Leffler ath_init(ifp); /* XXX lose error */ 7825591b213SSam Leffler error = 0; 7835591b213SSam Leffler } 7845591b213SSam Leffler return error; 7855591b213SSam Leffler } 7865591b213SSam Leffler 7875591b213SSam Leffler static void 7885591b213SSam Leffler ath_watchdog(struct ifnet *ifp) 7895591b213SSam Leffler { 7905591b213SSam Leffler struct ath_softc *sc = ifp->if_softc; 7915591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 7925591b213SSam Leffler 7935591b213SSam Leffler ifp->if_timer = 0; 7945591b213SSam Leffler if ((ifp->if_flags & IFF_RUNNING) == 0 || sc->sc_invalid) 7955591b213SSam Leffler return; 7965591b213SSam Leffler if (sc->sc_tx_timer) { 7975591b213SSam Leffler if (--sc->sc_tx_timer == 0) { 7985591b213SSam Leffler if_printf(ifp, "device timeout\n"); 7995591b213SSam Leffler #ifdef AR_DEBUG 8005591b213SSam Leffler if (ath_debug) 8015591b213SSam Leffler ath_hal_dumpstate(sc->sc_ah); 8025591b213SSam Leffler #endif /* AR_DEBUG */ 8035591b213SSam Leffler ath_init(ifp); /* XXX ath_reset??? */ 8045591b213SSam Leffler ifp->if_oerrors++; 8055591b213SSam Leffler sc->sc_stats.ast_watchdog++; 8065591b213SSam Leffler return; 8075591b213SSam Leffler } 8085591b213SSam Leffler ifp->if_timer = 1; 8095591b213SSam Leffler } 8105591b213SSam Leffler if (ic->ic_fixed_rate == -1) { 8115591b213SSam Leffler /* 8125591b213SSam Leffler * Run the rate control algorithm if we're not 8135591b213SSam Leffler * locked at a fixed rate. 8145591b213SSam Leffler */ 8155591b213SSam Leffler if (ic->ic_opmode == IEEE80211_M_STA) 8165591b213SSam Leffler ath_rate_ctl(sc, ic->ic_bss); 8175591b213SSam Leffler else 8185591b213SSam Leffler ieee80211_iterate_nodes(ic, ath_rate_ctl, sc); 8195591b213SSam Leffler } 8205591b213SSam Leffler ieee80211_watchdog(ifp); 8215591b213SSam Leffler } 8225591b213SSam Leffler 8235591b213SSam Leffler static int 8245591b213SSam Leffler ath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 8255591b213SSam Leffler { 8265591b213SSam Leffler struct ath_softc *sc = ifp->if_softc; 8275591b213SSam Leffler struct ifreq *ifr = (struct ifreq *)data; 8285591b213SSam Leffler int error = 0; 8295591b213SSam Leffler 8305591b213SSam Leffler mtx_lock(&sc->sc_mtx); 8315591b213SSam Leffler switch (cmd) { 8325591b213SSam Leffler case SIOCSIFFLAGS: 8335591b213SSam Leffler if (ifp->if_flags & IFF_UP) { 8345591b213SSam Leffler if (ifp->if_flags & IFF_RUNNING) { 8355591b213SSam Leffler /* 8365591b213SSam Leffler * To avoid rescanning another access point, 8375591b213SSam Leffler * do not call ath_init() here. Instead, 8385591b213SSam Leffler * only reflect promisc mode settings. 8395591b213SSam Leffler */ 8405591b213SSam Leffler ath_mode_init(sc); 8415591b213SSam Leffler } else 8425591b213SSam Leffler ath_init(ifp); /* XXX lose error */ 8435591b213SSam Leffler } else 8445591b213SSam Leffler ath_stop(ifp); 8455591b213SSam Leffler break; 8465591b213SSam Leffler case SIOCADDMULTI: 8475591b213SSam Leffler case SIOCDELMULTI: 8485591b213SSam Leffler /* 8495591b213SSam Leffler * The upper layer has already installed/removed 8505591b213SSam Leffler * the multicast address(es), just recalculate the 8515591b213SSam Leffler * multicast filter for the card. 8525591b213SSam Leffler */ 8535591b213SSam Leffler if (ifp->if_flags & IFF_RUNNING) 8545591b213SSam Leffler ath_mode_init(sc); 8555591b213SSam Leffler break; 8565591b213SSam Leffler case SIOCGATHSTATS: 8575591b213SSam Leffler copyout(&sc->sc_stats, ifr->ifr_data, sizeof (sc->sc_stats)); 8585591b213SSam Leffler break; 8595591b213SSam Leffler default: 8605591b213SSam Leffler error = ieee80211_ioctl(ifp, cmd, data); 8615591b213SSam Leffler if (error == ENETRESET) { 8625591b213SSam Leffler if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) == 8635591b213SSam Leffler (IFF_RUNNING|IFF_UP)) 8645591b213SSam Leffler ath_init(ifp); /* XXX lose error */ 8655591b213SSam Leffler error = 0; 8665591b213SSam Leffler } 8675591b213SSam Leffler break; 8685591b213SSam Leffler } 8695591b213SSam Leffler mtx_unlock(&sc->sc_mtx); 8705591b213SSam Leffler return error; 8715591b213SSam Leffler } 8725591b213SSam Leffler 8735591b213SSam Leffler /* 8745591b213SSam Leffler * Fill the hardware key cache with key entries. 8755591b213SSam Leffler */ 8765591b213SSam Leffler static void 8775591b213SSam Leffler ath_initkeytable(struct ath_softc *sc) 8785591b213SSam Leffler { 8795591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 8805591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 8815591b213SSam Leffler int i; 8825591b213SSam Leffler 8835591b213SSam Leffler for (i = 0; i < IEEE80211_WEP_NKID; i++) { 8845591b213SSam Leffler struct ieee80211_wepkey *k = &ic->ic_nw_keys[i]; 8855591b213SSam Leffler if (k->wk_len == 0) 8865591b213SSam Leffler ath_hal_keyreset(ah, i); 8875591b213SSam Leffler else 8885591b213SSam Leffler /* XXX return value */ 8895591b213SSam Leffler /* NB: this uses HAL_KEYVAL == ieee80211_wepkey */ 8905591b213SSam Leffler ath_hal_keyset(ah, i, (const HAL_KEYVAL *) k); 8915591b213SSam Leffler } 8925591b213SSam Leffler } 8935591b213SSam Leffler 8945591b213SSam Leffler static void 8955591b213SSam Leffler ath_mode_init(struct ath_softc *sc) 8965591b213SSam Leffler { 8975591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 8985591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 8995591b213SSam Leffler struct ifnet *ifp = &ic->ic_if; 9005591b213SSam Leffler u_int32_t rfilt, mfilt[2], val; 9015591b213SSam Leffler u_int8_t pos; 9025591b213SSam Leffler struct ifmultiaddr *ifma; 9035591b213SSam Leffler 9045591b213SSam Leffler /* configure operational mode */ 9055591b213SSam Leffler ath_hal_setopmode(ah, ic->ic_opmode); 9065591b213SSam Leffler 9075591b213SSam Leffler /* receive filter */ 9085591b213SSam Leffler rfilt = (ath_hal_getrxfilter(ah) & HAL_RX_FILTER_PHYERR) 9095591b213SSam Leffler | HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST; 9105591b213SSam Leffler if (ic->ic_opmode != IEEE80211_M_HOSTAP && 9115591b213SSam Leffler (ifp->if_flags & IFF_PROMISC)) 9125591b213SSam Leffler rfilt |= HAL_RX_FILTER_PROM; 9135591b213SSam Leffler if (ic->ic_state == IEEE80211_S_SCAN) 9145591b213SSam Leffler rfilt |= HAL_RX_FILTER_BEACON; 9155591b213SSam Leffler ath_hal_setrxfilter(ah, rfilt); 9165591b213SSam Leffler 9175591b213SSam Leffler /* calculate and install multicast filter */ 9185591b213SSam Leffler if ((ifp->if_flags & IFF_ALLMULTI) == 0) { 9195591b213SSam Leffler mfilt[0] = mfilt[1] = 0; 9205591b213SSam Leffler TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 9215591b213SSam Leffler caddr_t dl; 9225591b213SSam Leffler 9235591b213SSam Leffler /* calculate XOR of eight 6bit values */ 9245591b213SSam Leffler dl = LLADDR((struct sockaddr_dl *) ifma->ifma_addr); 9255591b213SSam Leffler val = LE_READ_4(dl + 0); 9265591b213SSam Leffler pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val; 9275591b213SSam Leffler val = LE_READ_4(dl + 3); 9285591b213SSam Leffler pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val; 9295591b213SSam Leffler pos &= 0x3f; 9305591b213SSam Leffler mfilt[pos / 32] |= (1 << (pos % 32)); 9315591b213SSam Leffler } 9325591b213SSam Leffler } else { 9335591b213SSam Leffler mfilt[0] = mfilt[1] = ~0; 9345591b213SSam Leffler } 9355591b213SSam Leffler ath_hal_setmcastfilter(ah, mfilt[0], mfilt[1]); 9365591b213SSam Leffler DPRINTF(("ath_mode_init: RX filter 0x%x, MC filter %08x:%08x\n", 9375591b213SSam Leffler rfilt, mfilt[0], mfilt[1])); 9385591b213SSam Leffler } 9395591b213SSam Leffler 9405591b213SSam Leffler static void 9415591b213SSam Leffler ath_mbuf_load_cb(void *arg, bus_dma_segment_t *seg, int nseg, bus_size_t mapsize, int error) 9425591b213SSam Leffler { 9435591b213SSam Leffler struct ath_buf *bf = arg; 9445591b213SSam Leffler 9455591b213SSam Leffler KASSERT(nseg <= ATH_MAX_SCATTER, 9465591b213SSam Leffler ("ath_mbuf_load_cb: too many DMA segments %u", nseg)); 9475591b213SSam Leffler bf->bf_mapsize = mapsize; 9485591b213SSam Leffler bf->bf_nseg = nseg; 9495591b213SSam Leffler bcopy(seg, bf->bf_segs, nseg * sizeof (seg[0])); 9505591b213SSam Leffler } 9515591b213SSam Leffler 9525591b213SSam Leffler static int 9535591b213SSam Leffler ath_beacon_alloc(struct ath_softc *sc, struct ieee80211_node *ni) 9545591b213SSam Leffler { 9555591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 9565591b213SSam Leffler struct ifnet *ifp = &ic->ic_if; 9575591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 9585591b213SSam Leffler struct ieee80211_frame *wh; 9595591b213SSam Leffler struct ath_buf *bf; 9605591b213SSam Leffler struct ath_desc *ds; 9615591b213SSam Leffler struct mbuf *m; 9625591b213SSam Leffler int error, pktlen; 9635591b213SSam Leffler u_int8_t *frm, rate; 9645591b213SSam Leffler u_int16_t capinfo; 9655591b213SSam Leffler struct ieee80211_rateset *rs; 9665591b213SSam Leffler const HAL_RATE_TABLE *rt; 9675591b213SSam Leffler 9685591b213SSam Leffler bf = sc->sc_bcbuf; 9695591b213SSam Leffler if (bf->bf_m != NULL) { 9705591b213SSam Leffler bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 9715591b213SSam Leffler m_freem(bf->bf_m); 9725591b213SSam Leffler bf->bf_m = NULL; 9735591b213SSam Leffler bf->bf_node = NULL; 9745591b213SSam Leffler } 9755591b213SSam Leffler /* 9765591b213SSam Leffler * NB: the beacon data buffer must be 32-bit aligned; 9775591b213SSam Leffler * we assume the mbuf routines will return us something 9785591b213SSam Leffler * with this alignment (perhaps should assert). 9795591b213SSam Leffler */ 9805591b213SSam Leffler rs = &ni->ni_rates; 9815591b213SSam Leffler pktlen = 8 + 2 + 2+ 2+ni->ni_esslen + 2+rs->rs_nrates + 6; 9825591b213SSam Leffler if (rs->rs_nrates > IEEE80211_RATE_SIZE) 9835591b213SSam Leffler pktlen += 2; 9845591b213SSam Leffler if (pktlen <= MHLEN) 9855591b213SSam Leffler MGETHDR(m, M_DONTWAIT, MT_DATA); 9865591b213SSam Leffler else 9875591b213SSam Leffler m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 9885591b213SSam Leffler if (m == NULL) { 9895591b213SSam Leffler DPRINTF(("ath_beacon_alloc: cannot get mbuf/cluster; size %u\n", 9905591b213SSam Leffler pktlen)); 9915591b213SSam Leffler sc->sc_stats.ast_be_nombuf++; 9925591b213SSam Leffler return ENOMEM; 9935591b213SSam Leffler } 9945591b213SSam Leffler 9955591b213SSam Leffler wh = mtod(m, struct ieee80211_frame *); 9965591b213SSam Leffler wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | 9975591b213SSam Leffler IEEE80211_FC0_SUBTYPE_BEACON; 9985591b213SSam Leffler wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 9995591b213SSam Leffler *(u_int16_t *)wh->i_dur = 0; 10005591b213SSam Leffler memcpy(wh->i_addr1, ifp->if_broadcastaddr, IEEE80211_ADDR_LEN); 10015591b213SSam Leffler memcpy(wh->i_addr2, ic->ic_myaddr, IEEE80211_ADDR_LEN); 10025591b213SSam Leffler memcpy(wh->i_addr3, ni->ni_bssid, IEEE80211_ADDR_LEN); 10035591b213SSam Leffler *(u_int16_t *)wh->i_seq = 0; 10045591b213SSam Leffler 10055591b213SSam Leffler /* 10065591b213SSam Leffler * beacon frame format 10075591b213SSam Leffler * [8] time stamp 10085591b213SSam Leffler * [2] beacon interval 10095591b213SSam Leffler * [2] cabability information 10105591b213SSam Leffler * [tlv] ssid 10115591b213SSam Leffler * [tlv] supported rates 10125591b213SSam Leffler * [tlv] parameter set (IBSS) 10135591b213SSam Leffler * [tlv] extended supported rates 10145591b213SSam Leffler */ 10155591b213SSam Leffler frm = (u_int8_t *)&wh[1]; 10165591b213SSam Leffler memset(frm, 0, 8); /* timestamp is set by hardware */ 10175591b213SSam Leffler frm += 8; 10185591b213SSam Leffler *(u_int16_t *)frm = htole16(ni->ni_intval); 10195591b213SSam Leffler frm += 2; 10205591b213SSam Leffler if (ic->ic_opmode == IEEE80211_M_IBSS) 10215591b213SSam Leffler capinfo = IEEE80211_CAPINFO_IBSS; 10225591b213SSam Leffler else 10235591b213SSam Leffler capinfo = IEEE80211_CAPINFO_ESS; 10245591b213SSam Leffler if (ic->ic_flags & IEEE80211_F_WEPON) 10255591b213SSam Leffler capinfo |= IEEE80211_CAPINFO_PRIVACY; 10265591b213SSam Leffler if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 10275591b213SSam Leffler capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 10285591b213SSam Leffler if (ic->ic_flags & IEEE80211_F_SHSLOT) 10295591b213SSam Leffler capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 10305591b213SSam Leffler *(u_int16_t *)frm = htole16(capinfo); 10315591b213SSam Leffler frm += 2; 10325591b213SSam Leffler *frm++ = IEEE80211_ELEMID_SSID; 10335591b213SSam Leffler *frm++ = ni->ni_esslen; 10345591b213SSam Leffler memcpy(frm, ni->ni_essid, ni->ni_esslen); 10355591b213SSam Leffler frm += ni->ni_esslen; 10365591b213SSam Leffler frm = ieee80211_add_rates(frm, rs); 10375591b213SSam Leffler if (ic->ic_opmode == IEEE80211_M_IBSS) { 10385591b213SSam Leffler *frm++ = IEEE80211_ELEMID_IBSSPARMS; 10395591b213SSam Leffler *frm++ = 2; 10405591b213SSam Leffler *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */ 10415591b213SSam Leffler } else { 10425591b213SSam Leffler /* TODO: TIM */ 10435591b213SSam Leffler *frm++ = IEEE80211_ELEMID_TIM; 10445591b213SSam Leffler *frm++ = 4; /* length */ 10455591b213SSam Leffler *frm++ = 0; /* DTIM count */ 10465591b213SSam Leffler *frm++ = 1; /* DTIM period */ 10475591b213SSam Leffler *frm++ = 0; /* bitmap control */ 10485591b213SSam Leffler *frm++ = 0; /* Partial Virtual Bitmap (variable length) */ 10495591b213SSam Leffler } 10505591b213SSam Leffler frm = ieee80211_add_xrates(frm, rs); 10515591b213SSam Leffler m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *); 10525591b213SSam Leffler 10535591b213SSam Leffler DPRINTF2(("ath_beacon_alloc: m %p len %u\n", m, m->m_len)); 10545591b213SSam Leffler error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m, 10555591b213SSam Leffler ath_mbuf_load_cb, bf, 10565591b213SSam Leffler BUS_DMA_NOWAIT); 10575591b213SSam Leffler if (error != 0) { 10585591b213SSam Leffler m_freem(m); 10595591b213SSam Leffler return error; 10605591b213SSam Leffler } 10615591b213SSam Leffler KASSERT(bf->bf_nseg == 1, 10625591b213SSam Leffler ("ath_beacon_alloc: multi-segment packet; nseg %u", 10635591b213SSam Leffler bf->bf_nseg)); 10645591b213SSam Leffler bf->bf_m = m; 10655591b213SSam Leffler 10665591b213SSam Leffler /* setup descriptors */ 10675591b213SSam Leffler ds = bf->bf_desc; 10685591b213SSam Leffler 10695591b213SSam Leffler ds->ds_link = 0; 10705591b213SSam Leffler ds->ds_data = bf->bf_segs[0].ds_addr; 10715591b213SSam Leffler /* XXX verify mbuf data area covers this roundup */ 10725591b213SSam Leffler /* 10735591b213SSam Leffler * Calculate rate code. 10745591b213SSam Leffler * XXX everything at min xmit rate 10755591b213SSam Leffler */ 10765591b213SSam Leffler rt = sc->sc_currates; 10775591b213SSam Leffler KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode)); 10785591b213SSam Leffler if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) == 0) 10795591b213SSam Leffler rate = rt->info[0].rateCode | rt->info[0].shortPreamble; 10805591b213SSam Leffler else 10815591b213SSam Leffler rate = rt->info[0].rateCode; 10825591b213SSam Leffler ath_hal_setuptxdesc(ah, ds 10835591b213SSam Leffler , m->m_pkthdr.len + IEEE80211_CRC_LEN /* packet length */ 10845591b213SSam Leffler , sizeof(struct ieee80211_frame) /* header length */ 10855591b213SSam Leffler , HAL_PKT_TYPE_BEACON /* Atheros packet type */ 10865591b213SSam Leffler , 0x20 /* txpower XXX */ 10875591b213SSam Leffler , rate, 1 /* series 0 rate/tries */ 10885591b213SSam Leffler , HAL_TXKEYIX_INVALID /* no encryption */ 10895591b213SSam Leffler , 0 /* antenna mode */ 10905591b213SSam Leffler , HAL_TXDESC_NOACK /* no ack for beacons */ 10915591b213SSam Leffler , 0 /* rts/cts rate */ 10925591b213SSam Leffler , 0 /* rts/cts duration */ 10935591b213SSam Leffler ); 10945591b213SSam Leffler /* NB: beacon's BufLen must be a multiple of 4 bytes */ 10955591b213SSam Leffler ath_hal_filltxdesc(ah, ds 10965591b213SSam Leffler , roundup(bf->bf_segs[0].ds_len, 4) /* buffer length */ 10975591b213SSam Leffler , AH_TRUE /* first segment */ 10985591b213SSam Leffler , AH_TRUE /* last segment */ 10995591b213SSam Leffler ); 11005591b213SSam Leffler 11015591b213SSam Leffler return 0; 11025591b213SSam Leffler } 11035591b213SSam Leffler 11045591b213SSam Leffler static void 11055591b213SSam Leffler ath_beacon_proc(void *arg, int pending) 11065591b213SSam Leffler { 11075591b213SSam Leffler struct ath_softc *sc = arg; 11085591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 11095591b213SSam Leffler struct ath_buf *bf = sc->sc_bcbuf; 11105591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 11115591b213SSam Leffler 11125591b213SSam Leffler DPRINTF2(("%s: pending %u\n", __func__, pending)); 11135591b213SSam Leffler if (ic->ic_opmode == IEEE80211_M_STA || bf == NULL || bf->bf_m == NULL) { 11145591b213SSam Leffler DPRINTF(("%s: ic_flags=%x bf=%p bf_m=%p\n", 11155591b213SSam Leffler __func__, ic->ic_flags, bf, bf ? bf->bf_m : NULL)); 11165591b213SSam Leffler return; 11175591b213SSam Leffler } 11185591b213SSam Leffler /* update beacon to reflect PS poll state */ 11195591b213SSam Leffler if (!ath_hal_stoptxdma(ah, sc->sc_bhalq)) { 11205591b213SSam Leffler DPRINTF(("%s: beacon queue %u did not stop?", 11215591b213SSam Leffler __func__, sc->sc_bhalq)); 11225591b213SSam Leffler return; /* busy, XXX is this right? */ 11235591b213SSam Leffler } 11245591b213SSam Leffler bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE); 11255591b213SSam Leffler 11265591b213SSam Leffler ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr); 11275591b213SSam Leffler ath_hal_txstart(ah, sc->sc_bhalq); 11285591b213SSam Leffler DPRINTF2(("%s: TXDP%u = %p (%p)\n", __func__, 11295591b213SSam Leffler sc->sc_bhalq, (caddr_t)bf->bf_daddr, bf->bf_desc)); 11305591b213SSam Leffler } 11315591b213SSam Leffler 11325591b213SSam Leffler static void 11335591b213SSam Leffler ath_beacon_free(struct ath_softc *sc) 11345591b213SSam Leffler { 11355591b213SSam Leffler struct ath_buf *bf = sc->sc_bcbuf; 11365591b213SSam Leffler 11375591b213SSam Leffler if (bf->bf_m != NULL) { 11385591b213SSam Leffler bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 11395591b213SSam Leffler m_freem(bf->bf_m); 11405591b213SSam Leffler bf->bf_m = NULL; 11415591b213SSam Leffler bf->bf_node = NULL; 11425591b213SSam Leffler } 11435591b213SSam Leffler } 11445591b213SSam Leffler 11455591b213SSam Leffler /* 11465591b213SSam Leffler * Configure the beacon and sleep timers. 11475591b213SSam Leffler * 11485591b213SSam Leffler * When operating as an AP this resets the TSF and sets 11495591b213SSam Leffler * up the hardware to notify us when we need to issue beacons. 11505591b213SSam Leffler * 11515591b213SSam Leffler * When operating in station mode this sets up the beacon 11525591b213SSam Leffler * timers according to the timestamp of the last received 11535591b213SSam Leffler * beacon and the current TSF, configures PCF and DTIM 11545591b213SSam Leffler * handling, programs the sleep registers so the hardware 11555591b213SSam Leffler * will wakeup in time to receive beacons, and configures 11565591b213SSam Leffler * the beacon miss handling so we'll receive a BMISS 11575591b213SSam Leffler * interrupt when we stop seeing beacons from the AP 11585591b213SSam Leffler * we've associated with. 11595591b213SSam Leffler */ 11605591b213SSam Leffler static void 11615591b213SSam Leffler ath_beacon_config(struct ath_softc *sc) 11625591b213SSam Leffler { 11635591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 11645591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 11655591b213SSam Leffler struct ieee80211_node *ni = ic->ic_bss; 11665591b213SSam Leffler u_int32_t nexttbtt; 11675591b213SSam Leffler 11685591b213SSam Leffler nexttbtt = (LE_READ_4(ni->ni_tstamp + 4) << 22) | 11695591b213SSam Leffler (LE_READ_4(ni->ni_tstamp) >> 10); 11705591b213SSam Leffler DPRINTF(("%s: nexttbtt=%u\n", __func__, nexttbtt)); 11715591b213SSam Leffler nexttbtt += ni->ni_intval; 11725591b213SSam Leffler if (ic->ic_opmode != IEEE80211_M_HOSTAP) { 11735591b213SSam Leffler HAL_BEACON_STATE bs; 11745591b213SSam Leffler u_int32_t bmisstime; 11755591b213SSam Leffler 11765591b213SSam Leffler /* NB: no PCF support right now */ 11775591b213SSam Leffler memset(&bs, 0, sizeof(bs)); 11785591b213SSam Leffler bs.bs_intval = ni->ni_intval; 11795591b213SSam Leffler bs.bs_nexttbtt = nexttbtt; 11805591b213SSam Leffler bs.bs_dtimperiod = bs.bs_intval; 11815591b213SSam Leffler bs.bs_nextdtim = nexttbtt; 11825591b213SSam Leffler /* 11835591b213SSam Leffler * Calculate the number of consecutive beacons to miss 11845591b213SSam Leffler * before taking a BMISS interrupt. The configuration 11855591b213SSam Leffler * is specified in ms, so we need to convert that to 11865591b213SSam Leffler * TU's and then calculate based on the beacon interval. 11875591b213SSam Leffler * Note that we clamp the result to at most 10 beacons. 11885591b213SSam Leffler */ 11895591b213SSam Leffler bmisstime = (ic->ic_bmisstimeout * 1000) / 1024; 11905591b213SSam Leffler bs.bs_bmissthreshold = howmany(bmisstime,ni->ni_intval); 11915591b213SSam Leffler if (bs.bs_bmissthreshold > 10) 11925591b213SSam Leffler bs.bs_bmissthreshold = 10; 11935591b213SSam Leffler else if (bs.bs_bmissthreshold <= 0) 11945591b213SSam Leffler bs.bs_bmissthreshold = 1; 11955591b213SSam Leffler 11965591b213SSam Leffler /* 11975591b213SSam Leffler * Calculate sleep duration. The configuration is 11985591b213SSam Leffler * given in ms. We insure a multiple of the beacon 11995591b213SSam Leffler * period is used. Also, if the sleep duration is 12005591b213SSam Leffler * greater than the DTIM period then it makes senses 12015591b213SSam Leffler * to make it a multiple of that. 12025591b213SSam Leffler * 12035591b213SSam Leffler * XXX fixed at 100ms 12045591b213SSam Leffler */ 12055591b213SSam Leffler bs.bs_sleepduration = 12065591b213SSam Leffler roundup((100 * 1000) / 1024, bs.bs_intval); 12075591b213SSam Leffler if (bs.bs_sleepduration > bs.bs_dtimperiod) 12085591b213SSam Leffler bs.bs_sleepduration = roundup(bs.bs_sleepduration, bs.bs_dtimperiod); 12095591b213SSam Leffler 12105591b213SSam Leffler DPRINTF(("%s: intval %u nexttbtt %u dtim %u nextdtim %u bmiss %u sleep %u\n" 12115591b213SSam Leffler , __func__ 12125591b213SSam Leffler , bs.bs_intval 12135591b213SSam Leffler , bs.bs_nexttbtt 12145591b213SSam Leffler , bs.bs_dtimperiod 12155591b213SSam Leffler , bs.bs_nextdtim 12165591b213SSam Leffler , bs.bs_bmissthreshold 12175591b213SSam Leffler , bs.bs_sleepduration 12185591b213SSam Leffler )); 12195591b213SSam Leffler ath_hal_intrset(ah, 0); 12205591b213SSam Leffler /* 12215591b213SSam Leffler * Reset our tsf so the hardware will update the 12225591b213SSam Leffler * tsf register to reflect timestamps found in 12235591b213SSam Leffler * received beacons. 12245591b213SSam Leffler */ 12255591b213SSam Leffler ath_hal_resettsf(ah); 12265591b213SSam Leffler ath_hal_beacontimers(ah, &bs, 0/*XXX*/, 0, 0); 12275591b213SSam Leffler sc->sc_imask |= HAL_INT_BMISS; 12285591b213SSam Leffler ath_hal_intrset(ah, sc->sc_imask); 12295591b213SSam Leffler } else { 12305591b213SSam Leffler DPRINTF(("%s: intval %u nexttbtt %u\n", 12315591b213SSam Leffler __func__, ni->ni_intval, nexttbtt)); 12325591b213SSam Leffler ath_hal_intrset(ah, 0); 12335591b213SSam Leffler ath_hal_beaconinit(ah, ic->ic_opmode, 12345591b213SSam Leffler nexttbtt, ni->ni_intval); 12355591b213SSam Leffler sc->sc_imask |= HAL_INT_SWBA; /* beacon prepare */ 12365591b213SSam Leffler ath_hal_intrset(ah, sc->sc_imask); 12375591b213SSam Leffler } 12385591b213SSam Leffler } 12395591b213SSam Leffler 12405591b213SSam Leffler static void 12415591b213SSam Leffler ath_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 12425591b213SSam Leffler { 12435591b213SSam Leffler bus_addr_t *paddr = (bus_addr_t*) arg; 12445591b213SSam Leffler *paddr = segs->ds_addr; 12455591b213SSam Leffler } 12465591b213SSam Leffler 12475591b213SSam Leffler static int 12485591b213SSam Leffler ath_desc_alloc(struct ath_softc *sc) 12495591b213SSam Leffler { 12505591b213SSam Leffler int i, bsize, error; 12515591b213SSam Leffler struct ath_desc *ds; 12525591b213SSam Leffler struct ath_buf *bf; 12535591b213SSam Leffler 12545591b213SSam Leffler /* allocate descriptors */ 12555591b213SSam Leffler sc->sc_desc_len = sizeof(struct ath_desc) * 12565591b213SSam Leffler (ATH_TXBUF * ATH_TXDESC + ATH_RXBUF + 1); 12575591b213SSam Leffler error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT, &sc->sc_ddmamap); 12585591b213SSam Leffler if (error != 0) 12595591b213SSam Leffler return error; 12605591b213SSam Leffler 12615591b213SSam Leffler error = bus_dmamem_alloc(sc->sc_dmat, (void**) &sc->sc_desc, 12625591b213SSam Leffler BUS_DMA_NOWAIT, &sc->sc_ddmamap); 12635591b213SSam Leffler if (error != 0) 12645591b213SSam Leffler goto fail0; 12655591b213SSam Leffler 12665591b213SSam Leffler error = bus_dmamap_load(sc->sc_dmat, sc->sc_ddmamap, 12675591b213SSam Leffler sc->sc_desc, sc->sc_desc_len, 12685591b213SSam Leffler ath_load_cb, &sc->sc_desc_paddr, 12695591b213SSam Leffler BUS_DMA_NOWAIT); 12705591b213SSam Leffler if (error != 0) 12715591b213SSam Leffler goto fail1; 12725591b213SSam Leffler 12735591b213SSam Leffler ds = sc->sc_desc; 12745591b213SSam Leffler DPRINTF(("ath_desc_alloc: DMA map: %p (%d) -> %p (%lu)\n", 12755591b213SSam Leffler ds, sc->sc_desc_len, 12765591b213SSam Leffler (caddr_t) sc->sc_desc_paddr, /*XXX*/ (u_long) sc->sc_desc_len)); 12775591b213SSam Leffler 12785591b213SSam Leffler /* allocate buffers */ 12795591b213SSam Leffler bsize = sizeof(struct ath_buf) * (ATH_TXBUF + ATH_RXBUF + 1); 12805591b213SSam Leffler bf = malloc(bsize, M_DEVBUF, M_NOWAIT | M_ZERO); 12815591b213SSam Leffler if (bf == NULL) 12825591b213SSam Leffler goto fail2; 12835591b213SSam Leffler sc->sc_bufptr = bf; 12845591b213SSam Leffler 12855591b213SSam Leffler TAILQ_INIT(&sc->sc_rxbuf); 12865591b213SSam Leffler for (i = 0; i < ATH_RXBUF; i++, bf++, ds++) { 12875591b213SSam Leffler bf->bf_desc = ds; 12885591b213SSam Leffler bf->bf_daddr = sc->sc_desc_paddr + 12895591b213SSam Leffler ((caddr_t)ds - (caddr_t)sc->sc_desc); 12905591b213SSam Leffler error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT, 12915591b213SSam Leffler &bf->bf_dmamap); 12925591b213SSam Leffler if (error != 0) 12935591b213SSam Leffler break; 12945591b213SSam Leffler TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list); 12955591b213SSam Leffler } 12965591b213SSam Leffler 12975591b213SSam Leffler TAILQ_INIT(&sc->sc_txbuf); 12985591b213SSam Leffler for (i = 0; i < ATH_TXBUF; i++, bf++, ds += ATH_TXDESC) { 12995591b213SSam Leffler bf->bf_desc = ds; 13005591b213SSam Leffler bf->bf_daddr = sc->sc_desc_paddr + 13015591b213SSam Leffler ((caddr_t)ds - (caddr_t)sc->sc_desc); 13025591b213SSam Leffler error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT, 13035591b213SSam Leffler &bf->bf_dmamap); 13045591b213SSam Leffler if (error != 0) 13055591b213SSam Leffler break; 13065591b213SSam Leffler TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list); 13075591b213SSam Leffler } 13085591b213SSam Leffler TAILQ_INIT(&sc->sc_txq); 13095591b213SSam Leffler 13105591b213SSam Leffler /* beacon buffer */ 13115591b213SSam Leffler bf->bf_desc = ds; 13125591b213SSam Leffler bf->bf_daddr = sc->sc_desc_paddr + ((caddr_t)ds - (caddr_t)sc->sc_desc); 13135591b213SSam Leffler error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT, &bf->bf_dmamap); 13145591b213SSam Leffler if (error != 0) 13155591b213SSam Leffler return error; 13165591b213SSam Leffler sc->sc_bcbuf = bf; 13175591b213SSam Leffler return 0; 13185591b213SSam Leffler 13195591b213SSam Leffler fail2: 13205591b213SSam Leffler bus_dmamap_unload(sc->sc_dmat, sc->sc_ddmamap); 13215591b213SSam Leffler fail1: 13225591b213SSam Leffler bus_dmamem_free(sc->sc_dmat, sc->sc_desc, sc->sc_ddmamap); 13235591b213SSam Leffler fail0: 13245591b213SSam Leffler bus_dmamap_destroy(sc->sc_dmat, sc->sc_ddmamap); 13255591b213SSam Leffler sc->sc_ddmamap = NULL; 13265591b213SSam Leffler return error; 13275591b213SSam Leffler } 13285591b213SSam Leffler 13295591b213SSam Leffler static void 13305591b213SSam Leffler ath_desc_free(struct ath_softc *sc) 13315591b213SSam Leffler { 13325591b213SSam Leffler struct ath_buf *bf; 13335591b213SSam Leffler 13345591b213SSam Leffler bus_dmamap_unload(sc->sc_dmat, sc->sc_ddmamap); 13355591b213SSam Leffler bus_dmamem_free(sc->sc_dmat, sc->sc_desc, sc->sc_ddmamap); 13365591b213SSam Leffler bus_dmamap_destroy(sc->sc_dmat, sc->sc_ddmamap); 13375591b213SSam Leffler 13385591b213SSam Leffler TAILQ_FOREACH(bf, &sc->sc_txq, bf_list) { 13395591b213SSam Leffler bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 13405591b213SSam Leffler bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap); 13415591b213SSam Leffler m_freem(bf->bf_m); 13425591b213SSam Leffler } 13435591b213SSam Leffler TAILQ_FOREACH(bf, &sc->sc_txbuf, bf_list) 13445591b213SSam Leffler bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap); 13455591b213SSam Leffler TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) { 13465591b213SSam Leffler if (bf->bf_m) { 13475591b213SSam Leffler bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 13485591b213SSam Leffler bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap); 13495591b213SSam Leffler m_freem(bf->bf_m); 13505591b213SSam Leffler bf->bf_m = NULL; 13515591b213SSam Leffler } 13525591b213SSam Leffler } 13535591b213SSam Leffler if (sc->sc_bcbuf != NULL) { 13545591b213SSam Leffler bus_dmamap_unload(sc->sc_dmat, sc->sc_bcbuf->bf_dmamap); 13555591b213SSam Leffler bus_dmamap_destroy(sc->sc_dmat, sc->sc_bcbuf->bf_dmamap); 13565591b213SSam Leffler sc->sc_bcbuf = NULL; 13575591b213SSam Leffler } 13585591b213SSam Leffler 13595591b213SSam Leffler TAILQ_INIT(&sc->sc_rxbuf); 13605591b213SSam Leffler TAILQ_INIT(&sc->sc_txbuf); 13615591b213SSam Leffler TAILQ_INIT(&sc->sc_txq); 13625591b213SSam Leffler free(sc->sc_bufptr, M_DEVBUF); 13635591b213SSam Leffler sc->sc_bufptr = NULL; 13645591b213SSam Leffler } 13655591b213SSam Leffler 13665591b213SSam Leffler static struct ieee80211_node * 13675591b213SSam Leffler ath_node_alloc(struct ieee80211com *ic) 13685591b213SSam Leffler { 13695591b213SSam Leffler struct ath_node *an = 13705591b213SSam Leffler malloc(sizeof(struct ath_node), M_DEVBUF, M_NOWAIT | M_ZERO); 13715591b213SSam Leffler return an ? &an->st_node : NULL; 13725591b213SSam Leffler } 13735591b213SSam Leffler 13745591b213SSam Leffler static void 13755591b213SSam Leffler ath_node_free(struct ieee80211com *ic, struct ieee80211_node *ni) 13765591b213SSam Leffler { 13775591b213SSam Leffler struct ath_softc *sc = ic->ic_if.if_softc; 13785591b213SSam Leffler struct ath_buf *bf; 13795591b213SSam Leffler 13805591b213SSam Leffler TAILQ_FOREACH(bf, &sc->sc_txq, bf_list) { 13815591b213SSam Leffler if (bf->bf_node == ni) 13825591b213SSam Leffler bf->bf_node = NULL; 13835591b213SSam Leffler } 13845591b213SSam Leffler free(ni, M_DEVBUF); 13855591b213SSam Leffler } 13865591b213SSam Leffler 13875591b213SSam Leffler static void 13885591b213SSam Leffler ath_node_copy(struct ieee80211com *ic, 13895591b213SSam Leffler struct ieee80211_node *dst, const struct ieee80211_node *src) 13905591b213SSam Leffler { 13915591b213SSam Leffler *(struct ath_node *)dst = *(const struct ath_node *)src; 13925591b213SSam Leffler } 13935591b213SSam Leffler 13945591b213SSam Leffler static int 13955591b213SSam Leffler ath_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf) 13965591b213SSam Leffler { 13975591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 13985591b213SSam Leffler int error; 13995591b213SSam Leffler struct mbuf *m; 14005591b213SSam Leffler struct ath_desc *ds; 14015591b213SSam Leffler 14025591b213SSam Leffler m = bf->bf_m; 14035591b213SSam Leffler if (m == NULL) { 14045591b213SSam Leffler /* 14055591b213SSam Leffler * NB: by assigning a page to the rx dma buffer we 14065591b213SSam Leffler * implicitly satisfy the Atheros requirement that 14075591b213SSam Leffler * this buffer be cache-line-aligned and sized to be 14085591b213SSam Leffler * multiple of the cache line size. Not doing this 14095591b213SSam Leffler * causes weird stuff to happen (for the 5210 at least). 14105591b213SSam Leffler */ 14115591b213SSam Leffler m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 14125591b213SSam Leffler if (m == NULL) { 14135591b213SSam Leffler DPRINTF(("ath_rxbuf_init: no mbuf/cluster\n")); 14145591b213SSam Leffler sc->sc_stats.ast_rx_nombuf++; 14155591b213SSam Leffler return ENOMEM; 14165591b213SSam Leffler } 14175591b213SSam Leffler bf->bf_m = m; 14185591b213SSam Leffler m->m_pkthdr.len = m->m_len = m->m_ext.ext_size; 14195591b213SSam Leffler 14205591b213SSam Leffler error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m, 14215591b213SSam Leffler ath_mbuf_load_cb, bf, 14225591b213SSam Leffler BUS_DMA_NOWAIT); 14235591b213SSam Leffler if (error != 0) { 14245591b213SSam Leffler DPRINTF(("ath_rxbuf_init: bus_dmamap_load_mbuf failed;" 14255591b213SSam Leffler " error %d\n", error)); 14265591b213SSam Leffler sc->sc_stats.ast_rx_busdma++; 14275591b213SSam Leffler return error; 14285591b213SSam Leffler } 14295591b213SSam Leffler KASSERT(bf->bf_nseg == 1, 14305591b213SSam Leffler ("ath_rxbuf_init: multi-segment packet; nseg %u", 14315591b213SSam Leffler bf->bf_nseg)); 14325591b213SSam Leffler } 14335591b213SSam Leffler bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREREAD); 14345591b213SSam Leffler 14355591b213SSam Leffler /* setup descriptors */ 14365591b213SSam Leffler ds = bf->bf_desc; 14375591b213SSam Leffler ds->ds_link = 0; 14385591b213SSam Leffler ds->ds_data = bf->bf_segs[0].ds_addr; 14395591b213SSam Leffler ath_hal_setuprxdesc(ah, ds 14405591b213SSam Leffler , m->m_len /* buffer size */ 14415591b213SSam Leffler , 0 14425591b213SSam Leffler ); 14435591b213SSam Leffler 14445591b213SSam Leffler if (sc->sc_rxlink != NULL) 14455591b213SSam Leffler *sc->sc_rxlink = bf->bf_daddr; 14465591b213SSam Leffler sc->sc_rxlink = &ds->ds_link; 14475591b213SSam Leffler return 0; 14485591b213SSam Leffler } 14495591b213SSam Leffler 14505591b213SSam Leffler static void 14515591b213SSam Leffler ath_rx_proc(void *arg, int npending) 14525591b213SSam Leffler { 14535591b213SSam Leffler struct ath_softc *sc = arg; 14545591b213SSam Leffler struct ath_buf *bf; 14555591b213SSam Leffler struct ifnet *ifp = &sc->sc_ic.ic_if; 14565591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 14575591b213SSam Leffler struct ath_desc *ds; 14585591b213SSam Leffler struct mbuf *m; 14595591b213SSam Leffler struct ieee80211_frame *wh, whbuf; 14605591b213SSam Leffler int len; 14615591b213SSam Leffler u_int phyerr; 14625591b213SSam Leffler HAL_STATUS status; 14635591b213SSam Leffler 14645591b213SSam Leffler DPRINTF2(("ath_rx_proc: pending %u\n", npending)); 14655591b213SSam Leffler do { 14665591b213SSam Leffler bf = TAILQ_FIRST(&sc->sc_rxbuf); 14675591b213SSam Leffler if (bf == NULL) { /* NB: shouldn't happen */ 14685591b213SSam Leffler if_printf(ifp, "ath_rx_proc: no buffer!\n"); 14695591b213SSam Leffler break; 14705591b213SSam Leffler } 14715591b213SSam Leffler m = bf->bf_m; 14725591b213SSam Leffler if (m == NULL) { /* NB: shouldn't happen */ 14735591b213SSam Leffler if_printf(ifp, "ath_rx_proc: no mbuf!\n"); 14745591b213SSam Leffler continue; 14755591b213SSam Leffler } 14765591b213SSam Leffler ds = bf->bf_desc; 14775591b213SSam Leffler status = ath_hal_rxprocdesc(ah, ds); 14785591b213SSam Leffler #ifdef AR_DEBUG 14795591b213SSam Leffler if (ath_debug > 1) 14805591b213SSam Leffler ath_printrxbuf(bf, status == HAL_OK); 14815591b213SSam Leffler #endif 14825591b213SSam Leffler if (status == HAL_EINPROGRESS) 14835591b213SSam Leffler break; 14845591b213SSam Leffler TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list); 14855591b213SSam Leffler if (ds->ds_rxstat.rs_status != 0) { 14865591b213SSam Leffler ifp->if_ierrors++; 14875591b213SSam Leffler if (ds->ds_rxstat.rs_status & HAL_RXERR_CRC) 14885591b213SSam Leffler sc->sc_stats.ast_rx_crcerr++; 14895591b213SSam Leffler if (ds->ds_rxstat.rs_status & HAL_RXERR_FIFO) 14905591b213SSam Leffler sc->sc_stats.ast_rx_fifoerr++; 14915591b213SSam Leffler if (ds->ds_rxstat.rs_status & HAL_RXERR_DECRYPT) 14925591b213SSam Leffler sc->sc_stats.ast_rx_badcrypt++; 14935591b213SSam Leffler if (ds->ds_rxstat.rs_status & HAL_RXERR_PHY) { 14945591b213SSam Leffler sc->sc_stats.ast_rx_phyerr++; 14955591b213SSam Leffler phyerr = ds->ds_rxstat.rs_phyerr & 0x1f; 14965591b213SSam Leffler sc->sc_stats.ast_rx_phy[phyerr]++; 14975591b213SSam Leffler } 14985591b213SSam Leffler goto rx_next; 14995591b213SSam Leffler } 15005591b213SSam Leffler 15015591b213SSam Leffler len = ds->ds_rxstat.rs_datalen; 15025591b213SSam Leffler if (len < sizeof(struct ieee80211_frame)) { 15035591b213SSam Leffler DPRINTF(("ath_rx_proc: short packet %d\n", len)); 15045591b213SSam Leffler goto rx_next; 15055591b213SSam Leffler } 15065591b213SSam Leffler 15075591b213SSam Leffler bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, 15085591b213SSam Leffler BUS_DMASYNC_POSTREAD); 15095591b213SSam Leffler 15105591b213SSam Leffler wh = mtod(m, struct ieee80211_frame *); 15115591b213SSam Leffler if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == 15125591b213SSam Leffler IEEE80211_FC0_TYPE_CTL) { 15135591b213SSam Leffler /* 15145591b213SSam Leffler * Ignore control frame received in promisc mode. 15155591b213SSam Leffler */ 15165591b213SSam Leffler DPRINTF(("ath_rx_proc: control frame\n")); 15175591b213SSam Leffler goto rx_next; 15185591b213SSam Leffler } 15195591b213SSam Leffler 15205591b213SSam Leffler bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 15215591b213SSam Leffler bf->bf_m = NULL; 15225591b213SSam Leffler m->m_pkthdr.rcvif = ifp; 15235591b213SSam Leffler m->m_pkthdr.len = m->m_len = len; 15245591b213SSam Leffler if (IFF_DUMPPKTS(ifp)) { 15255591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 15265591b213SSam Leffler const HAL_RATE_TABLE *rt = sc->sc_rates[ic->ic_curmode]; 15275591b213SSam Leffler ieee80211_dump_pkt(mtod(m, u_int8_t *), len, 15285591b213SSam Leffler rt ? rt->info[rt->rateCodeToIndex[ds->ds_rxstat.rs_rate]].dot11Rate & IEEE80211_RATE_VAL : 0, 15295591b213SSam Leffler ds->ds_rxstat.rs_rssi); 15305591b213SSam Leffler } 15315591b213SSam Leffler m_adj(m, -IEEE80211_CRC_LEN); 15325591b213SSam Leffler if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 15335591b213SSam Leffler /* 15345591b213SSam Leffler * WEP is decrypted by hardware. Clear WEP bit 15355591b213SSam Leffler * and trim WEP header for ieee80211_input(). 15365591b213SSam Leffler */ 15375591b213SSam Leffler wh->i_fc[1] &= ~IEEE80211_FC1_WEP; 15385591b213SSam Leffler memcpy(&whbuf, wh, sizeof(whbuf)); 15395591b213SSam Leffler m_adj(m, IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN); 15405591b213SSam Leffler memcpy(mtod(m, caddr_t), &whbuf, sizeof(whbuf)); 15415591b213SSam Leffler /* 15425591b213SSam Leffler * Also trim WEP ICV from the tail. 15435591b213SSam Leffler */ 15445591b213SSam Leffler m_adj(m, -IEEE80211_WEP_CRCLEN); 15455591b213SSam Leffler } 15465591b213SSam Leffler ieee80211_input(ifp, m, 15475591b213SSam Leffler ds->ds_rxstat.rs_rssi, 15485591b213SSam Leffler ds->ds_rxstat.rs_tstamp, 15495591b213SSam Leffler ds->ds_rxstat.rs_antenna); 15505591b213SSam Leffler rx_next: 15515591b213SSam Leffler TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list); 15525591b213SSam Leffler } while (ath_rxbuf_init(sc, bf) == 0); 15535591b213SSam Leffler 15545591b213SSam Leffler ath_hal_rxmonitor(ah); /* rx signal state monitoring */ 15555591b213SSam Leffler ath_hal_rxena(ah); /* in case of RXEOL */ 15565591b213SSam Leffler } 15575591b213SSam Leffler 15585591b213SSam Leffler /* 15595591b213SSam Leffler * XXX Size of an ACK control frame in bytes. 15605591b213SSam Leffler */ 15615591b213SSam Leffler #define IEEE80211_ACK_SIZE (2+2+IEEE80211_ADDR_LEN+4) 15625591b213SSam Leffler 15635591b213SSam Leffler static int 15645591b213SSam Leffler ath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni, struct ath_buf *bf, 15655591b213SSam Leffler struct mbuf *m0) 15665591b213SSam Leffler { 15675591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 15685591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 15695591b213SSam Leffler struct ifnet *ifp = &sc->sc_ic.ic_if; 15705591b213SSam Leffler int i, error, iswep, hdrlen, pktlen; 15715591b213SSam Leffler u_int8_t rix, cix, txrate, ctsrate; 15725591b213SSam Leffler struct ath_desc *ds; 15735591b213SSam Leffler struct mbuf *m; 15745591b213SSam Leffler struct ieee80211_frame *wh; 15755591b213SSam Leffler u_int32_t iv; 15765591b213SSam Leffler u_int8_t *ivp; 15775591b213SSam Leffler u_int8_t hdrbuf[sizeof(struct ieee80211_frame) + 15785591b213SSam Leffler IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN]; 15795591b213SSam Leffler u_int subtype, flags, ctsduration, antenna; 15805591b213SSam Leffler HAL_PKT_TYPE atype; 15815591b213SSam Leffler const HAL_RATE_TABLE *rt; 15825591b213SSam Leffler HAL_BOOL shortPreamble; 15835591b213SSam Leffler struct ath_node *an; 15845591b213SSam Leffler 15855591b213SSam Leffler wh = mtod(m0, struct ieee80211_frame *); 15865591b213SSam Leffler iswep = wh->i_fc[1] & IEEE80211_FC1_WEP; 15875591b213SSam Leffler hdrlen = sizeof(struct ieee80211_frame); 15885591b213SSam Leffler pktlen = m0->m_pkthdr.len; 15895591b213SSam Leffler 15905591b213SSam Leffler if (iswep) { 15915591b213SSam Leffler memcpy(hdrbuf, mtod(m0, caddr_t), hdrlen); 15925591b213SSam Leffler m_adj(m0, hdrlen); 15935591b213SSam Leffler M_PREPEND(m0, sizeof(hdrbuf), M_DONTWAIT); 15945591b213SSam Leffler if (m0 == NULL) { 15955591b213SSam Leffler sc->sc_stats.ast_tx_nombuf++; 15965591b213SSam Leffler return ENOMEM; 15975591b213SSam Leffler } 15985591b213SSam Leffler ivp = hdrbuf + hdrlen; 15995591b213SSam Leffler /* 16005591b213SSam Leffler * XXX 16015591b213SSam Leffler * IV must not duplicate during the lifetime of the key. 16025591b213SSam Leffler * But no mechanism to renew keys is defined in IEEE 802.11 16035591b213SSam Leffler * WEP. And IV may be duplicated between other stations 16045591b213SSam Leffler * because of the session key itself is shared. 16055591b213SSam Leffler * So we use pseudo random IV for now, though it is not the 16065591b213SSam Leffler * right way. 16075591b213SSam Leffler */ 16085591b213SSam Leffler iv = arc4random(); 16095591b213SSam Leffler for (i = 0; i < IEEE80211_WEP_IVLEN; i++) { 16105591b213SSam Leffler ivp[i] = iv; 16115591b213SSam Leffler iv >>= 8; 16125591b213SSam Leffler } 16135591b213SSam Leffler ivp[i] = sc->sc_ic.ic_wep_txkey << 6; /* Key ID and pad */ 16145591b213SSam Leffler memcpy(mtod(m0, caddr_t), hdrbuf, sizeof(hdrbuf)); 16155591b213SSam Leffler /* 16165591b213SSam Leffler * The ICV length must be included into hdrlen and pktlen. 16175591b213SSam Leffler */ 16185591b213SSam Leffler hdrlen = sizeof(hdrbuf) + IEEE80211_WEP_CRCLEN; 16195591b213SSam Leffler pktlen = m0->m_pkthdr.len + IEEE80211_WEP_CRCLEN; 16205591b213SSam Leffler } 16215591b213SSam Leffler pktlen += IEEE80211_CRC_LEN; 16225591b213SSam Leffler 16235591b213SSam Leffler /* 16245591b213SSam Leffler * Load the DMA map so any coalescing is done. This 16255591b213SSam Leffler * also calculates the number of descriptors we need. 16265591b213SSam Leffler */ 16275591b213SSam Leffler error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m0, 16285591b213SSam Leffler ath_mbuf_load_cb, bf, 16295591b213SSam Leffler BUS_DMA_NOWAIT); 16305591b213SSam Leffler if (error != 0) { 16315591b213SSam Leffler sc->sc_stats.ast_tx_busdma++; 16325591b213SSam Leffler m_freem(m0); 16335591b213SSam Leffler return error; 16345591b213SSam Leffler } 16355591b213SSam Leffler /* 16365591b213SSam Leffler * Discard null packets and check for packets that 16375591b213SSam Leffler * require too many TX descriptors. We try to convert 16385591b213SSam Leffler * the latter to a cluster. 16395591b213SSam Leffler */ 16405591b213SSam Leffler if (bf->bf_nseg > ATH_TXDESC) { /* too many desc's, linearize */ 16415591b213SSam Leffler sc->sc_stats.ast_tx_linear++; 16425591b213SSam Leffler MGETHDR(m, M_DONTWAIT, MT_DATA); 16435591b213SSam Leffler if (m == NULL) { 16445591b213SSam Leffler sc->sc_stats.ast_tx_nombuf++; 16455591b213SSam Leffler m_freem(m0); 16465591b213SSam Leffler return ENOMEM; 16475591b213SSam Leffler } 16485591b213SSam Leffler M_MOVE_PKTHDR(m, m0); 16495591b213SSam Leffler MCLGET(m, M_DONTWAIT); 16505591b213SSam Leffler if ((m->m_flags & M_EXT) == 0) { 16515591b213SSam Leffler sc->sc_stats.ast_tx_nomcl++; 16525591b213SSam Leffler m_freem(m0); 16535591b213SSam Leffler m_free(m); 16545591b213SSam Leffler return ENOMEM; 16555591b213SSam Leffler } 16565591b213SSam Leffler m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t)); 16575591b213SSam Leffler m_freem(m0); 16585591b213SSam Leffler m->m_len = m->m_pkthdr.len; 16595591b213SSam Leffler m0 = m; 16605591b213SSam Leffler error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m0, 16615591b213SSam Leffler ath_mbuf_load_cb, bf, 16625591b213SSam Leffler BUS_DMA_NOWAIT); 16635591b213SSam Leffler if (error != 0) { 16645591b213SSam Leffler sc->sc_stats.ast_tx_busdma++; 16655591b213SSam Leffler m_freem(m0); 16665591b213SSam Leffler return error; 16675591b213SSam Leffler } 16685591b213SSam Leffler KASSERT(bf->bf_nseg == 1, 16695591b213SSam Leffler ("ath_tx_start: packet not one segment; nseg %u", 16705591b213SSam Leffler bf->bf_nseg)); 16715591b213SSam Leffler } else if (bf->bf_nseg == 0) { /* null packet, discard */ 16725591b213SSam Leffler sc->sc_stats.ast_tx_nodata++; 16735591b213SSam Leffler m_freem(m0); 16745591b213SSam Leffler return EIO; 16755591b213SSam Leffler } 16765591b213SSam Leffler DPRINTF2(("ath_tx_start: m %p len %u\n", m0, pktlen)); 16775591b213SSam Leffler bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE); 16785591b213SSam Leffler bf->bf_m = m0; 16795591b213SSam Leffler bf->bf_node = ni; 16805591b213SSam Leffler 16815591b213SSam Leffler /* setup descriptors */ 16825591b213SSam Leffler ds = bf->bf_desc; 16835591b213SSam Leffler rt = sc->sc_currates; 16845591b213SSam Leffler KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode)); 16855591b213SSam Leffler 16865591b213SSam Leffler /* 16875591b213SSam Leffler * Calculate Atheros packet type from IEEE80211 packet header 16885591b213SSam Leffler * and setup for rate calculations. 16895591b213SSam Leffler */ 16905591b213SSam Leffler atype = HAL_PKT_TYPE_NORMAL; /* default */ 16915591b213SSam Leffler switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) { 16925591b213SSam Leffler case IEEE80211_FC0_TYPE_MGT: 16935591b213SSam Leffler subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 16945591b213SSam Leffler if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) 16955591b213SSam Leffler atype = HAL_PKT_TYPE_BEACON; 16965591b213SSam Leffler else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) 16975591b213SSam Leffler atype = HAL_PKT_TYPE_PROBE_RESP; 16985591b213SSam Leffler else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM) 16995591b213SSam Leffler atype = HAL_PKT_TYPE_ATIM; 17005591b213SSam Leffler rix = 0; /* XXX lowest rate */ 17015591b213SSam Leffler break; 17025591b213SSam Leffler case IEEE80211_FC0_TYPE_CTL: 17035591b213SSam Leffler subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 17045591b213SSam Leffler if (subtype == IEEE80211_FC0_SUBTYPE_PS_POLL) 17055591b213SSam Leffler atype = HAL_PKT_TYPE_PSPOLL; 17065591b213SSam Leffler rix = 0; /* XXX lowest rate */ 17075591b213SSam Leffler break; 17085591b213SSam Leffler default: 17095591b213SSam Leffler rix = sc->sc_rixmap[ni->ni_rates.rs_rates[ni->ni_txrate] & 17105591b213SSam Leffler IEEE80211_RATE_VAL]; 17115591b213SSam Leffler if (rix == 0xff) { 17125591b213SSam Leffler if_printf(ifp, "bogus xmit rate 0x%x\n", 17135591b213SSam Leffler ni->ni_rates.rs_rates[ni->ni_txrate]); 17145591b213SSam Leffler sc->sc_stats.ast_tx_badrate++; 17155591b213SSam Leffler m_freem(m0); 17165591b213SSam Leffler return EIO; 17175591b213SSam Leffler } 17185591b213SSam Leffler break; 17195591b213SSam Leffler } 17205591b213SSam Leffler /* 17215591b213SSam Leffler * NB: the 802.11 layer marks whether or not we should 17225591b213SSam Leffler * use short preamble based on the current mode and 17235591b213SSam Leffler * negotiated parameters. 17245591b213SSam Leffler */ 17255591b213SSam Leffler if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) { 17265591b213SSam Leffler txrate = rt->info[rix].rateCode | rt->info[rix].shortPreamble; 17275591b213SSam Leffler shortPreamble = AH_TRUE; 17285591b213SSam Leffler sc->sc_stats.ast_tx_shortpre++; 17295591b213SSam Leffler } else { 17305591b213SSam Leffler txrate = rt->info[rix].rateCode; 17315591b213SSam Leffler shortPreamble = AH_FALSE; 17325591b213SSam Leffler } 17335591b213SSam Leffler 17345591b213SSam Leffler /* 17355591b213SSam Leffler * Calculate miscellaneous flags. 17365591b213SSam Leffler */ 17375591b213SSam Leffler flags = HAL_TXDESC_CLRDMASK; /* XXX needed for wep errors */ 17385591b213SSam Leffler if (IEEE80211_IS_MULTICAST(wh->i_addr1)) { 17395591b213SSam Leffler flags |= HAL_TXDESC_NOACK; /* no ack on broad/multicast */ 17405591b213SSam Leffler sc->sc_stats.ast_tx_noack++; 17415591b213SSam Leffler } else if (pktlen > ic->ic_rtsthreshold) { 17425591b213SSam Leffler flags |= HAL_TXDESC_RTSENA; /* RTS based on frame length */ 17435591b213SSam Leffler sc->sc_stats.ast_tx_rts++; 17445591b213SSam Leffler } 17455591b213SSam Leffler 17465591b213SSam Leffler /* 17475591b213SSam Leffler * Calculate RTS/CTS rate and duration if needed. 17485591b213SSam Leffler */ 17495591b213SSam Leffler ctsduration = 0; 17505591b213SSam Leffler if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA)) { 17515591b213SSam Leffler /* 17525591b213SSam Leffler * CTS transmit rate is derived from the transmit rate 17535591b213SSam Leffler * by looking in the h/w rate table. We must also factor 17545591b213SSam Leffler * in whether or not a short preamble is to be used. 17555591b213SSam Leffler */ 17565591b213SSam Leffler cix = rt->info[rix].controlRate; 17575591b213SSam Leffler ctsrate = rt->info[cix].rateCode; 17585591b213SSam Leffler if (shortPreamble) 17595591b213SSam Leffler ctsrate |= rt->info[cix].shortPreamble; 17605591b213SSam Leffler /* 17615591b213SSam Leffler * Compute the transmit duration based on the size 17625591b213SSam Leffler * of an ACK frame. We call into the HAL to do the 17635591b213SSam Leffler * computation since it depends on the characteristics 17645591b213SSam Leffler * of the actual PHY being used. 17655591b213SSam Leffler */ 17665591b213SSam Leffler if (flags & HAL_TXDESC_RTSENA) { /* SIFS + CTS */ 17675591b213SSam Leffler ctsduration += ath_hal_computetxtime(ah, 17685591b213SSam Leffler rt, IEEE80211_ACK_SIZE, cix, shortPreamble); 17695591b213SSam Leffler } 17705591b213SSam Leffler /* SIFS + data */ 17715591b213SSam Leffler ctsduration += ath_hal_computetxtime(ah, 17725591b213SSam Leffler rt, pktlen, rix, shortPreamble); 17735591b213SSam Leffler if ((flags & HAL_TXDESC_NOACK) == 0) { /* SIFS + ACK */ 17745591b213SSam Leffler ctsduration += ath_hal_computetxtime(ah, 17755591b213SSam Leffler rt, IEEE80211_ACK_SIZE, cix, shortPreamble); 17765591b213SSam Leffler } 17775591b213SSam Leffler } else 17785591b213SSam Leffler ctsrate = 0; 17795591b213SSam Leffler 17805591b213SSam Leffler /* 17815591b213SSam Leffler * For now use the antenna on which the last good 17825591b213SSam Leffler * frame was received on. We assume this field is 17835591b213SSam Leffler * initialized to 0 which gives us ``auto'' or the 17845591b213SSam Leffler * ``default'' antenna. 17855591b213SSam Leffler */ 17865591b213SSam Leffler an = (struct ath_node *) ni; 17875591b213SSam Leffler if (an->an_tx_antenna) 17885591b213SSam Leffler antenna = an->an_tx_antenna; 17895591b213SSam Leffler else 17905591b213SSam Leffler antenna = ni->ni_rantenna; 17915591b213SSam Leffler 17925591b213SSam Leffler /* 17935591b213SSam Leffler * Formulate first tx descriptor with tx controls. 17945591b213SSam Leffler */ 17955591b213SSam Leffler /* XXX check return value? */ 17965591b213SSam Leffler ath_hal_setuptxdesc(ah, ds 17975591b213SSam Leffler , pktlen /* packet length */ 17985591b213SSam Leffler , hdrlen /* header length */ 17995591b213SSam Leffler , atype /* Atheros packet type */ 18005591b213SSam Leffler , 60 /* txpower XXX */ 18015591b213SSam Leffler , txrate, 1+10 /* series 0 rate/tries */ 18025591b213SSam Leffler , iswep ? sc->sc_ic.ic_wep_txkey : HAL_TXKEYIX_INVALID 18035591b213SSam Leffler , antenna /* antenna mode */ 18045591b213SSam Leffler , flags /* flags */ 18055591b213SSam Leffler , ctsrate /* rts/cts rate */ 18065591b213SSam Leffler , ctsduration /* rts/cts duration */ 18075591b213SSam Leffler ); 18085591b213SSam Leffler #ifdef notyet 18095591b213SSam Leffler ath_hal_setupxtxdesc(ah, ds 18105591b213SSam Leffler , AH_FALSE /* short preamble */ 18115591b213SSam Leffler , 0, 0 /* series 1 rate/tries */ 18125591b213SSam Leffler , 0, 0 /* series 2 rate/tries */ 18135591b213SSam Leffler , 0, 0 /* series 3 rate/tries */ 18145591b213SSam Leffler ); 18155591b213SSam Leffler #endif 18165591b213SSam Leffler /* 18175591b213SSam Leffler * Fillin the remainder of the descriptor info. 18185591b213SSam Leffler */ 18195591b213SSam Leffler for (i = 0; i < bf->bf_nseg; i++, ds++) { 18205591b213SSam Leffler ds->ds_data = bf->bf_segs[i].ds_addr; 18215591b213SSam Leffler if (i == bf->bf_nseg - 1) 18225591b213SSam Leffler ds->ds_link = 0; 18235591b213SSam Leffler else 18245591b213SSam Leffler ds->ds_link = bf->bf_daddr + sizeof(*ds) * (i + 1); 18255591b213SSam Leffler ath_hal_filltxdesc(ah, ds 18265591b213SSam Leffler , bf->bf_segs[i].ds_len /* segment length */ 18275591b213SSam Leffler , i == 0 /* first segment */ 18285591b213SSam Leffler , i == bf->bf_nseg - 1 /* last segment */ 18295591b213SSam Leffler ); 18305591b213SSam Leffler DPRINTF2(("ath_tx_start: %d: %08x %08x %08x %08x %08x %08x\n", 18315591b213SSam Leffler i, ds->ds_link, ds->ds_data, ds->ds_ctl0, ds->ds_ctl1, 18325591b213SSam Leffler ds->ds_hw[0], ds->ds_hw[1])); 18335591b213SSam Leffler } 18345591b213SSam Leffler 18355591b213SSam Leffler /* 18365591b213SSam Leffler * Insert the frame on the outbound list and 18375591b213SSam Leffler * pass it on to the hardware. 18385591b213SSam Leffler */ 18395591b213SSam Leffler mtx_lock(&sc->sc_txqlock); 18405591b213SSam Leffler TAILQ_INSERT_TAIL(&sc->sc_txq, bf, bf_list); 18415591b213SSam Leffler if (sc->sc_txlink == NULL) { 18425591b213SSam Leffler ath_hal_puttxbuf(ah, sc->sc_txhalq, bf->bf_daddr); 18435591b213SSam Leffler DPRINTF2(("ath_tx_start: TXDP0 = %p (%p)\n", 18445591b213SSam Leffler (caddr_t)bf->bf_daddr, bf->bf_desc)); 18455591b213SSam Leffler } else { 18465591b213SSam Leffler *sc->sc_txlink = bf->bf_daddr; 18475591b213SSam Leffler DPRINTF2(("ath_tx_start: link(%p)=%p (%p)\n", 18485591b213SSam Leffler sc->sc_txlink, (caddr_t)bf->bf_daddr, bf->bf_desc)); 18495591b213SSam Leffler } 18505591b213SSam Leffler sc->sc_txlink = &bf->bf_desc[bf->bf_nseg - 1].ds_link; 18515591b213SSam Leffler mtx_unlock(&sc->sc_txqlock); 18525591b213SSam Leffler 18535591b213SSam Leffler ath_hal_txstart(ah, sc->sc_txhalq); 18545591b213SSam Leffler return 0; 18555591b213SSam Leffler } 18565591b213SSam Leffler 18575591b213SSam Leffler static void 18585591b213SSam Leffler ath_tx_proc(void *arg, int npending) 18595591b213SSam Leffler { 18605591b213SSam Leffler struct ath_softc *sc = arg; 18615591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 18625591b213SSam Leffler struct ath_buf *bf; 18635591b213SSam Leffler struct ifnet *ifp = &sc->sc_ic.ic_if; 18645591b213SSam Leffler struct ath_desc *ds; 18655591b213SSam Leffler struct ieee80211_node *ni; 18665591b213SSam Leffler struct ath_node *an; 18675591b213SSam Leffler int sr, lr; 18685591b213SSam Leffler HAL_STATUS status; 18695591b213SSam Leffler 18705591b213SSam Leffler DPRINTF2(("ath_tx_proc: pending %u tx queue %p, link %p\n", 18715591b213SSam Leffler npending, (caddr_t) ath_hal_gettxbuf(sc->sc_ah, sc->sc_txhalq), 18725591b213SSam Leffler sc->sc_txlink)); 18735591b213SSam Leffler for (;;) { 18745591b213SSam Leffler mtx_lock(&sc->sc_txqlock); 18755591b213SSam Leffler bf = TAILQ_FIRST(&sc->sc_txq); 18765591b213SSam Leffler if (bf == NULL) { 18775591b213SSam Leffler sc->sc_txlink = NULL; 18785591b213SSam Leffler mtx_unlock(&sc->sc_txqlock); 18795591b213SSam Leffler break; 18805591b213SSam Leffler } 18815591b213SSam Leffler /* only the last descriptor is needed */ 18825591b213SSam Leffler ds = &bf->bf_desc[bf->bf_nseg - 1]; 18835591b213SSam Leffler status = ath_hal_txprocdesc(ah, ds); 18845591b213SSam Leffler #ifdef AR_DEBUG 18855591b213SSam Leffler if (ath_debug > 1) 18865591b213SSam Leffler ath_printtxbuf(bf, status == HAL_OK); 18875591b213SSam Leffler #endif 18885591b213SSam Leffler if (status == HAL_EINPROGRESS) { 18895591b213SSam Leffler mtx_unlock(&sc->sc_txqlock); 18905591b213SSam Leffler break; 18915591b213SSam Leffler } 18925591b213SSam Leffler TAILQ_REMOVE(&sc->sc_txq, bf, bf_list); 18935591b213SSam Leffler mtx_unlock(&sc->sc_txqlock); 18945591b213SSam Leffler 18955591b213SSam Leffler ni = bf->bf_node; 18965591b213SSam Leffler if (ni != NULL) { 18975591b213SSam Leffler an = (struct ath_node *) ni; 18985591b213SSam Leffler if (ds->ds_txstat.ts_status == 0) { 18995591b213SSam Leffler an->an_tx_ok++; 19005591b213SSam Leffler an->an_tx_antenna = ds->ds_txstat.ts_antenna; 19015591b213SSam Leffler } else { 19025591b213SSam Leffler an->an_tx_err++; 19035591b213SSam Leffler ifp->if_oerrors++; 19045591b213SSam Leffler if (ds->ds_txstat.ts_status & HAL_TXERR_XRETRY) 19055591b213SSam Leffler sc->sc_stats.ast_tx_xretries++; 19065591b213SSam Leffler if (ds->ds_txstat.ts_status & HAL_TXERR_FIFO) 19075591b213SSam Leffler sc->sc_stats.ast_tx_fifoerr++; 19085591b213SSam Leffler if (ds->ds_txstat.ts_status & HAL_TXERR_FILT) 19095591b213SSam Leffler sc->sc_stats.ast_tx_filtered++; 19105591b213SSam Leffler an->an_tx_antenna = 0; /* invalidate */ 19115591b213SSam Leffler } 19125591b213SSam Leffler sr = ds->ds_txstat.ts_shortretry; 19135591b213SSam Leffler lr = ds->ds_txstat.ts_longretry; 19145591b213SSam Leffler sc->sc_stats.ast_tx_shortretry += sr; 19155591b213SSam Leffler sc->sc_stats.ast_tx_longretry += lr; 19165591b213SSam Leffler if (sr + lr) 19175591b213SSam Leffler an->an_tx_retr++; 19185591b213SSam Leffler } 19195591b213SSam Leffler bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, 19205591b213SSam Leffler BUS_DMASYNC_POSTWRITE); 19215591b213SSam Leffler bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 19225591b213SSam Leffler m_freem(bf->bf_m); 19235591b213SSam Leffler bf->bf_m = NULL; 19245591b213SSam Leffler bf->bf_node = NULL; 19255591b213SSam Leffler 19265591b213SSam Leffler mtx_lock(&sc->sc_txbuflock); 19275591b213SSam Leffler TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list); 19285591b213SSam Leffler mtx_unlock(&sc->sc_txbuflock); 19295591b213SSam Leffler } 19305591b213SSam Leffler ifp->if_flags &= ~IFF_OACTIVE; 19315591b213SSam Leffler sc->sc_tx_timer = 0; 19325591b213SSam Leffler 19335591b213SSam Leffler ath_start(ifp); 19345591b213SSam Leffler } 19355591b213SSam Leffler 19365591b213SSam Leffler /* 19375591b213SSam Leffler * Drain the transmit queue and reclaim resources. 19385591b213SSam Leffler */ 19395591b213SSam Leffler static void 19405591b213SSam Leffler ath_draintxq(struct ath_softc *sc) 19415591b213SSam Leffler { 19425591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 19435591b213SSam Leffler struct ifnet *ifp = &sc->sc_ic.ic_if; 19445591b213SSam Leffler struct ath_buf *bf; 19455591b213SSam Leffler 19465591b213SSam Leffler /* XXX return value */ 19475591b213SSam Leffler if (!sc->sc_invalid) { 19485591b213SSam Leffler /* don't touch the hardware if marked invalid */ 19495591b213SSam Leffler (void) ath_hal_stoptxdma(ah, sc->sc_txhalq); 19505591b213SSam Leffler DPRINTF(("ath_draintxq: tx queue %p, link %p\n", 19515591b213SSam Leffler (caddr_t) ath_hal_gettxbuf(ah, sc->sc_txhalq), 19525591b213SSam Leffler sc->sc_txlink)); 19535591b213SSam Leffler (void) ath_hal_stoptxdma(ah, sc->sc_bhalq); 19545591b213SSam Leffler DPRINTF(("ath_draintxq: beacon queue %p\n", 19555591b213SSam Leffler (caddr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq))); 19565591b213SSam Leffler } 19575591b213SSam Leffler for (;;) { 19585591b213SSam Leffler mtx_lock(&sc->sc_txqlock); 19595591b213SSam Leffler bf = TAILQ_FIRST(&sc->sc_txq); 19605591b213SSam Leffler if (bf == NULL) { 19615591b213SSam Leffler sc->sc_txlink = NULL; 19625591b213SSam Leffler mtx_unlock(&sc->sc_txqlock); 19635591b213SSam Leffler break; 19645591b213SSam Leffler } 19655591b213SSam Leffler TAILQ_REMOVE(&sc->sc_txq, bf, bf_list); 19665591b213SSam Leffler mtx_unlock(&sc->sc_txqlock); 19675591b213SSam Leffler #ifdef AR_DEBUG 19685591b213SSam Leffler if (ath_debug) 19695591b213SSam Leffler ath_printtxbuf(bf, 19705591b213SSam Leffler ath_hal_txprocdesc(ah, bf->bf_desc) == HAL_OK); 19715591b213SSam Leffler #endif /* AR_DEBUG */ 19725591b213SSam Leffler bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 19735591b213SSam Leffler m_freem(bf->bf_m); 19745591b213SSam Leffler bf->bf_m = NULL; 19755591b213SSam Leffler bf->bf_node = NULL; 19765591b213SSam Leffler mtx_lock(&sc->sc_txbuflock); 19775591b213SSam Leffler TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list); 19785591b213SSam Leffler mtx_unlock(&sc->sc_txbuflock); 19795591b213SSam Leffler } 19805591b213SSam Leffler ifp->if_flags &= ~IFF_OACTIVE; 19815591b213SSam Leffler sc->sc_tx_timer = 0; 19825591b213SSam Leffler } 19835591b213SSam Leffler 19845591b213SSam Leffler /* 19855591b213SSam Leffler * Disable the receive h/w in preparation for a reset. 19865591b213SSam Leffler */ 19875591b213SSam Leffler static void 19885591b213SSam Leffler ath_stoprecv(struct ath_softc *sc) 19895591b213SSam Leffler { 19905591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 19915591b213SSam Leffler 19925591b213SSam Leffler ath_hal_stoppcurecv(ah); /* disable PCU */ 19935591b213SSam Leffler ath_hal_setrxfilter(ah, 0); /* clear recv filter */ 19945591b213SSam Leffler ath_hal_stopdmarecv(ah); /* disable DMA engine */ 19955591b213SSam Leffler DELAY(3000); /* long enough for 1 frame */ 19965591b213SSam Leffler #ifdef AR_DEBUG 19975591b213SSam Leffler if (ath_debug) { 19985591b213SSam Leffler struct ath_buf *bf; 19995591b213SSam Leffler 20005591b213SSam Leffler DPRINTF(("ath_stoprecv: rx queue %p, link %p\n", 20015591b213SSam Leffler (caddr_t) ath_hal_getrxbuf(ah), sc->sc_rxlink)); 20025591b213SSam Leffler TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) { 20035591b213SSam Leffler if (ath_hal_rxprocdesc(ah, bf->bf_desc) == HAL_OK) 20045591b213SSam Leffler ath_printrxbuf(bf, 1); 20055591b213SSam Leffler } 20065591b213SSam Leffler } 20075591b213SSam Leffler #endif 20085591b213SSam Leffler sc->sc_rxlink = NULL; /* just in case */ 20095591b213SSam Leffler } 20105591b213SSam Leffler 20115591b213SSam Leffler /* 20125591b213SSam Leffler * Enable the receive h/w following a reset. 20135591b213SSam Leffler */ 20145591b213SSam Leffler static int 20155591b213SSam Leffler ath_startrecv(struct ath_softc *sc) 20165591b213SSam Leffler { 20175591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 20185591b213SSam Leffler struct ath_buf *bf; 20195591b213SSam Leffler 20205591b213SSam Leffler sc->sc_rxlink = NULL; 20215591b213SSam Leffler TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) { 20225591b213SSam Leffler int error = ath_rxbuf_init(sc, bf); 20235591b213SSam Leffler if (error != 0) { 20245591b213SSam Leffler DPRINTF(("ath_startrecv: ath_rxbuf_init failed %d\n", 20255591b213SSam Leffler error)); 20265591b213SSam Leffler return error; 20275591b213SSam Leffler } 20285591b213SSam Leffler } 20295591b213SSam Leffler 20305591b213SSam Leffler bf = TAILQ_FIRST(&sc->sc_rxbuf); 20315591b213SSam Leffler ath_hal_putrxbuf(ah, bf->bf_daddr); 20325591b213SSam Leffler ath_hal_rxena(ah); /* enable recv descriptors */ 20335591b213SSam Leffler ath_mode_init(sc); /* set filters, etc. */ 20345591b213SSam Leffler ath_hal_startpcurecv(ah); /* re-enable PCU/DMA engine */ 20355591b213SSam Leffler return 0; 20365591b213SSam Leffler } 20375591b213SSam Leffler 20385591b213SSam Leffler /* 20395591b213SSam Leffler * Set/change channels. If the channel is really being changed, 20405591b213SSam Leffler * it's done by resetting the chip. To accomplish this we must 20415591b213SSam Leffler * first cleanup any pending DMA, then restart stuff after a la 20425591b213SSam Leffler * ath_init. 20435591b213SSam Leffler */ 20445591b213SSam Leffler static int 20455591b213SSam Leffler ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan) 20465591b213SSam Leffler { 20475591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 20485591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 20495591b213SSam Leffler 20505591b213SSam Leffler DPRINTF(("ath_chan_set: %u (%u MHz) -> %u (%u MHz)\n", 20515591b213SSam Leffler ieee80211_chan2ieee(ic, ic->ic_ibss_chan), 20525591b213SSam Leffler ic->ic_ibss_chan->ic_freq, 20535591b213SSam Leffler ieee80211_chan2ieee(ic, chan), chan->ic_freq)); 20545591b213SSam Leffler if (chan != ic->ic_ibss_chan) { 20555591b213SSam Leffler HAL_STATUS status; 20565591b213SSam Leffler HAL_CHANNEL hchan; 20575591b213SSam Leffler enum ieee80211_phymode mode; 20585591b213SSam Leffler 20595591b213SSam Leffler /* 20605591b213SSam Leffler * To switch channels clear any pending DMA operations; 20615591b213SSam Leffler * wait long enough for the RX fifo to drain, reset the 20625591b213SSam Leffler * hardware at the new frequency, and then re-enable 20635591b213SSam Leffler * the relevant bits of the h/w. 20645591b213SSam Leffler */ 20655591b213SSam Leffler ath_hal_intrset(ah, 0); /* disable interrupts */ 20665591b213SSam Leffler ath_draintxq(sc); /* clear pending tx frames */ 20675591b213SSam Leffler ath_stoprecv(sc); /* turn off frame recv */ 20685591b213SSam Leffler /* 20695591b213SSam Leffler * Convert to a HAL channel description with 20705591b213SSam Leffler * the flags constrained to reflect the current 20715591b213SSam Leffler * operating mode. 20725591b213SSam Leffler */ 20735591b213SSam Leffler hchan.channel = chan->ic_freq; 20745591b213SSam Leffler hchan.channelFlags = ath_chan2flags(ic, chan); 20755591b213SSam Leffler if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_TRUE, &status)) { 20765591b213SSam Leffler if_printf(&ic->ic_if, "ath_chan_set: unable to reset " 20775591b213SSam Leffler "channel %u (%u Mhz)\n", 20785591b213SSam Leffler ieee80211_chan2ieee(ic, chan), chan->ic_freq); 20795591b213SSam Leffler return EIO; 20805591b213SSam Leffler } 20815591b213SSam Leffler /* 20825591b213SSam Leffler * Re-enable rx framework. 20835591b213SSam Leffler */ 20845591b213SSam Leffler if (ath_startrecv(sc) != 0) { 20855591b213SSam Leffler if_printf(&ic->ic_if, 20865591b213SSam Leffler "ath_chan_set: unable to restart recv logic\n"); 20875591b213SSam Leffler return EIO; 20885591b213SSam Leffler } 20895591b213SSam Leffler 20905591b213SSam Leffler /* 20915591b213SSam Leffler * Re-enable interrupts. 20925591b213SSam Leffler */ 20935591b213SSam Leffler ath_hal_intrset(ah, sc->sc_imask); 20945591b213SSam Leffler 20955591b213SSam Leffler /* 20965591b213SSam Leffler * Change channels and update the h/w rate map 20975591b213SSam Leffler * if we're switching; e.g. 11a to 11b/g. 20985591b213SSam Leffler */ 20995591b213SSam Leffler ic->ic_ibss_chan = chan; 21005591b213SSam Leffler mode = ieee80211_chan2mode(ic, chan); 21015591b213SSam Leffler if (mode != sc->sc_curmode) 21025591b213SSam Leffler ath_setcurmode(sc, mode); 21035591b213SSam Leffler } 21045591b213SSam Leffler return 0; 21055591b213SSam Leffler } 21065591b213SSam Leffler 21075591b213SSam Leffler static void 21085591b213SSam Leffler ath_next_scan(void *arg) 21095591b213SSam Leffler { 21105591b213SSam Leffler struct ath_softc *sc = arg; 21115591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 21125591b213SSam Leffler struct ifnet *ifp = &ic->ic_if; 21135591b213SSam Leffler 21145591b213SSam Leffler if (ic->ic_state == IEEE80211_S_SCAN) 21155591b213SSam Leffler ieee80211_next_scan(ifp); 21165591b213SSam Leffler } 21175591b213SSam Leffler 21185591b213SSam Leffler /* 21195591b213SSam Leffler * Periodically recalibrate the PHY to account 21205591b213SSam Leffler * for temperature/environment changes. 21215591b213SSam Leffler */ 21225591b213SSam Leffler static void 21235591b213SSam Leffler ath_calibrate(void *arg) 21245591b213SSam Leffler { 21255591b213SSam Leffler struct ath_softc *sc = arg; 21265591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 21275591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 21285591b213SSam Leffler struct ieee80211_channel *c; 21295591b213SSam Leffler HAL_CHANNEL hchan; 21305591b213SSam Leffler 21315591b213SSam Leffler sc->sc_stats.ast_per_cal++; 21325591b213SSam Leffler 21335591b213SSam Leffler /* 21345591b213SSam Leffler * Convert to a HAL channel description with the flags 21355591b213SSam Leffler * constrained to reflect the current operating mode. 21365591b213SSam Leffler */ 21375591b213SSam Leffler c = ic->ic_ibss_chan; 21385591b213SSam Leffler hchan.channel = c->ic_freq; 21395591b213SSam Leffler hchan.channelFlags = ath_chan2flags(ic, c); 21405591b213SSam Leffler 21415591b213SSam Leffler DPRINTF(("%s: channel %u/%x\n", __func__, c->ic_freq, c->ic_flags)); 21425591b213SSam Leffler 21435591b213SSam Leffler if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) { 21445591b213SSam Leffler /* 21455591b213SSam Leffler * Rfgain is out of bounds, reset the chip 21465591b213SSam Leffler * to load new gain values. 21475591b213SSam Leffler */ 21485591b213SSam Leffler sc->sc_stats.ast_per_rfgain++; 21495591b213SSam Leffler ath_reset(sc); 21505591b213SSam Leffler } 21515591b213SSam Leffler if (!ath_hal_calibrate(ah, &hchan)) { 21525591b213SSam Leffler DPRINTF(("%s: calibration of channel %u failed\n", 21535591b213SSam Leffler __func__, c->ic_freq)); 21545591b213SSam Leffler sc->sc_stats.ast_per_calfail++; 21555591b213SSam Leffler } 21565591b213SSam Leffler callout_reset(&sc->sc_cal_ch, hz * ath_calinterval, ath_calibrate, sc); 21575591b213SSam Leffler } 21585591b213SSam Leffler 21595591b213SSam Leffler static int 216045bbf62fSSam Leffler ath_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 21615591b213SSam Leffler { 21625591b213SSam Leffler struct ifnet *ifp = &ic->ic_if; 216345bbf62fSSam Leffler struct ath_softc *sc = ifp->if_softc; 216445bbf62fSSam Leffler struct ath_hal *ah = sc->sc_ah; 21655591b213SSam Leffler struct ieee80211_node *ni; 21665591b213SSam Leffler int i, error; 21675591b213SSam Leffler u_int8_t *bssid; 21685591b213SSam Leffler u_int32_t rfilt; 21695591b213SSam Leffler static const HAL_LED_STATE leds[] = { 21705591b213SSam Leffler HAL_LED_INIT, /* IEEE80211_S_INIT */ 21715591b213SSam Leffler HAL_LED_SCAN, /* IEEE80211_S_SCAN */ 21725591b213SSam Leffler HAL_LED_AUTH, /* IEEE80211_S_AUTH */ 21735591b213SSam Leffler HAL_LED_ASSOC, /* IEEE80211_S_ASSOC */ 21745591b213SSam Leffler HAL_LED_RUN, /* IEEE80211_S_RUN */ 21755591b213SSam Leffler }; 21765591b213SSam Leffler 217745bbf62fSSam Leffler DPRINTF(("%s: %s -> %s\n", __func__, 217845bbf62fSSam Leffler ieee80211_state_name[ic->ic_state], 217945bbf62fSSam Leffler ieee80211_state_name[nstate])); 21805591b213SSam Leffler 21815591b213SSam Leffler ath_hal_setledstate(ah, leds[nstate]); /* set LED */ 21825591b213SSam Leffler 21835591b213SSam Leffler if (nstate == IEEE80211_S_INIT) { 21845591b213SSam Leffler sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS); 21855591b213SSam Leffler ath_hal_intrset(ah, sc->sc_imask); 218645bbf62fSSam Leffler callout_stop(&sc->sc_scan_ch); 218745bbf62fSSam Leffler callout_stop(&sc->sc_cal_ch); 218845bbf62fSSam Leffler return (*sc->sc_newstate)(ic, nstate, arg); 21895591b213SSam Leffler } 21905591b213SSam Leffler ni = ic->ic_bss; 21915591b213SSam Leffler error = ath_chan_set(sc, ni->ni_chan); 21925591b213SSam Leffler if (error != 0) 21935591b213SSam Leffler goto bad; 21945591b213SSam Leffler rfilt = (ath_hal_getrxfilter(ah) & HAL_RX_FILTER_PHYERR) 21955591b213SSam Leffler | HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST; 21965591b213SSam Leffler if (ic->ic_opmode != IEEE80211_M_HOSTAP && 21975591b213SSam Leffler (ifp->if_flags & IFF_PROMISC)) 21985591b213SSam Leffler rfilt |= HAL_RX_FILTER_PROM; 21995591b213SSam Leffler if (nstate == IEEE80211_S_SCAN) { 22005591b213SSam Leffler callout_reset(&sc->sc_scan_ch, (hz * ath_dwelltime) / 1000, 22015591b213SSam Leffler ath_next_scan, sc); 22025591b213SSam Leffler bssid = ifp->if_broadcastaddr; 22035591b213SSam Leffler rfilt |= HAL_RX_FILTER_BEACON; 22045591b213SSam Leffler } else { 22055591b213SSam Leffler callout_stop(&sc->sc_scan_ch); 22065591b213SSam Leffler bssid = ni->ni_bssid; 22075591b213SSam Leffler } 22085591b213SSam Leffler ath_hal_setrxfilter(ah, rfilt); 22095591b213SSam Leffler DPRINTF(("%s: RX filter 0x%x bssid %s\n", 22105591b213SSam Leffler __func__, rfilt, ether_sprintf(bssid))); 22115591b213SSam Leffler 22125591b213SSam Leffler if (nstate == IEEE80211_S_RUN && ic->ic_opmode == IEEE80211_M_STA) 22135591b213SSam Leffler ath_hal_setassocid(ah, bssid, ni->ni_associd); 22145591b213SSam Leffler else 22155591b213SSam Leffler ath_hal_setassocid(ah, bssid, 0); 22165591b213SSam Leffler if (ic->ic_flags & IEEE80211_F_WEPON) { 22175591b213SSam Leffler for (i = 0; i < IEEE80211_WEP_NKID; i++) 22185591b213SSam Leffler if (ath_hal_keyisvalid(ah, i)) 22195591b213SSam Leffler ath_hal_keysetmac(ah, i, bssid); 22205591b213SSam Leffler } 22215591b213SSam Leffler 22225591b213SSam Leffler if (nstate == IEEE80211_S_RUN) { 22235591b213SSam Leffler DPRINTF(("%s(RUN): ic_flags=0x%08x iv=%d bssid=%s " 22245591b213SSam Leffler "capinfo=0x%04x chan=%d\n" 22255591b213SSam Leffler , __func__ 22265591b213SSam Leffler , ic->ic_flags 22275591b213SSam Leffler , ni->ni_intval 22285591b213SSam Leffler , ether_sprintf(ni->ni_bssid) 22295591b213SSam Leffler , ni->ni_capinfo 22305591b213SSam Leffler , ieee80211_chan2ieee(ic, ni->ni_chan))); 22315591b213SSam Leffler 22325591b213SSam Leffler /* 22335591b213SSam Leffler * Allocate and setup the beacon frame for AP or adhoc mode. 22345591b213SSam Leffler */ 22355591b213SSam Leffler if (ic->ic_opmode != IEEE80211_M_STA) { 22365591b213SSam Leffler error = ath_beacon_alloc(sc, ni); 22375591b213SSam Leffler if (error != 0) 22385591b213SSam Leffler goto bad; 22395591b213SSam Leffler } 22405591b213SSam Leffler 22415591b213SSam Leffler /* 22425591b213SSam Leffler * Configure the beacon and sleep timers. 22435591b213SSam Leffler */ 22445591b213SSam Leffler ath_beacon_config(sc); 22455591b213SSam Leffler 22465591b213SSam Leffler /* start periodic recalibration timer */ 22475591b213SSam Leffler callout_reset(&sc->sc_cal_ch, hz * ath_calinterval, 22485591b213SSam Leffler ath_calibrate, sc); 22495591b213SSam Leffler } else { 22505591b213SSam Leffler sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS); 22515591b213SSam Leffler ath_hal_intrset(ah, sc->sc_imask); 22525591b213SSam Leffler callout_stop(&sc->sc_cal_ch); /* no calibration */ 22535591b213SSam Leffler } 22545591b213SSam Leffler /* 22555591b213SSam Leffler * Reset the rate control state. 22565591b213SSam Leffler */ 22575591b213SSam Leffler ath_rate_ctl_reset(sc, nstate); 225845bbf62fSSam Leffler /* 225945bbf62fSSam Leffler * Invoke the parent method to complete the work. 226045bbf62fSSam Leffler */ 226145bbf62fSSam Leffler return (*sc->sc_newstate)(ic, nstate, arg); 22625591b213SSam Leffler bad: 22635591b213SSam Leffler callout_stop(&sc->sc_scan_ch); 22645591b213SSam Leffler callout_stop(&sc->sc_cal_ch); 226545bbf62fSSam Leffler /* NB: do not invoke the parent */ 22665591b213SSam Leffler return error; 22675591b213SSam Leffler } 22685591b213SSam Leffler 22695591b213SSam Leffler /* 22705591b213SSam Leffler * Setup driver-specific state for a newly associated node. 22715591b213SSam Leffler * Note that we're called also on a re-associate, the isnew 22725591b213SSam Leffler * param tells us if this is the first time or not. 22735591b213SSam Leffler */ 22745591b213SSam Leffler static void 22755591b213SSam Leffler ath_newassoc(struct ieee80211com *ic, struct ieee80211_node *ni, int isnew) 22765591b213SSam Leffler { 22775591b213SSam Leffler if (isnew) { 22785591b213SSam Leffler struct ath_node *an = (struct ath_node *) ni; 22795591b213SSam Leffler 22805591b213SSam Leffler an->an_tx_ok = an->an_tx_err = 22815591b213SSam Leffler an->an_tx_retr = an->an_tx_upper = 0; 22825591b213SSam Leffler /* start with highest negotiated rate */ 22835591b213SSam Leffler /* 22845591b213SSam Leffler * XXX should do otherwise but only when 22855591b213SSam Leffler * the rate control algorithm is better. 22865591b213SSam Leffler */ 22875591b213SSam Leffler KASSERT(ni->ni_rates.rs_nrates > 0, 22885591b213SSam Leffler ("new association w/ no rates!")); 22895591b213SSam Leffler ni->ni_txrate = ni->ni_rates.rs_nrates - 1; 22905591b213SSam Leffler } 22915591b213SSam Leffler } 22925591b213SSam Leffler 22935591b213SSam Leffler static int 22945591b213SSam Leffler ath_getchannels(struct ath_softc *sc, u_int cc, HAL_BOOL outdoor) 22955591b213SSam Leffler { 22965591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 22975591b213SSam Leffler struct ifnet *ifp = &ic->ic_if; 22985591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 22995591b213SSam Leffler HAL_CHANNEL *chans; 23005591b213SSam Leffler int i, ix, nchan; 23015591b213SSam Leffler 23025591b213SSam Leffler sc->sc_have11g = 0; 23035591b213SSam Leffler chans = malloc(IEEE80211_CHAN_MAX * sizeof(HAL_CHANNEL), 23045591b213SSam Leffler M_TEMP, M_NOWAIT); 23055591b213SSam Leffler if (chans == NULL) { 23065591b213SSam Leffler if_printf(ifp, "unable to allocate channel table\n"); 23075591b213SSam Leffler return ENOMEM; 23085591b213SSam Leffler } 23095591b213SSam Leffler if (!ath_hal_init_channels(ah, chans, IEEE80211_CHAN_MAX, &nchan, 231045cabbdcSSam Leffler cc, HAL_MODE_ALL, outdoor)) { 23115591b213SSam Leffler if_printf(ifp, "unable to collect channel list from hal\n"); 23125591b213SSam Leffler free(chans, M_TEMP); 23135591b213SSam Leffler return EINVAL; 23145591b213SSam Leffler } 23155591b213SSam Leffler 23165591b213SSam Leffler /* 23175591b213SSam Leffler * Convert HAL channels to ieee80211 ones and insert 23185591b213SSam Leffler * them in the table according to their channel number. 23195591b213SSam Leffler */ 23205591b213SSam Leffler for (i = 0; i < nchan; i++) { 23215591b213SSam Leffler HAL_CHANNEL *c = &chans[i]; 23225591b213SSam Leffler ix = ath_hal_mhz2ieee(c->channel, c->channelFlags); 23235591b213SSam Leffler if (ix > IEEE80211_CHAN_MAX) { 23245591b213SSam Leffler if_printf(ifp, "bad hal channel %u (%u/%x) ignored\n", 23255591b213SSam Leffler ix, c->channel, c->channelFlags); 23265591b213SSam Leffler continue; 23275591b213SSam Leffler } 23285591b213SSam Leffler /* NB: flags are known to be compatible */ 23295591b213SSam Leffler if (ic->ic_channels[ix].ic_freq == 0) { 23305591b213SSam Leffler ic->ic_channels[ix].ic_freq = c->channel; 23315591b213SSam Leffler ic->ic_channels[ix].ic_flags = c->channelFlags; 23325591b213SSam Leffler } else { 23335591b213SSam Leffler /* channels overlap; e.g. 11g and 11b */ 23345591b213SSam Leffler ic->ic_channels[ix].ic_flags |= c->channelFlags; 23355591b213SSam Leffler } 23365591b213SSam Leffler if ((c->channelFlags & CHANNEL_G) == CHANNEL_G) 23375591b213SSam Leffler sc->sc_have11g = 1; 23385591b213SSam Leffler } 23395591b213SSam Leffler free(chans, M_TEMP); 23405591b213SSam Leffler return 0; 23415591b213SSam Leffler } 23425591b213SSam Leffler 23435591b213SSam Leffler static int 23445591b213SSam Leffler ath_rate_setup(struct ath_softc *sc, u_int mode) 23455591b213SSam Leffler { 23465591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 23475591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 23485591b213SSam Leffler const HAL_RATE_TABLE *rt; 23495591b213SSam Leffler struct ieee80211_rateset *rs; 23505591b213SSam Leffler int i, maxrates; 23515591b213SSam Leffler 23525591b213SSam Leffler switch (mode) { 23535591b213SSam Leffler case IEEE80211_MODE_11A: 23545591b213SSam Leffler sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11A); 23555591b213SSam Leffler break; 23565591b213SSam Leffler case IEEE80211_MODE_11B: 23575591b213SSam Leffler sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11B); 23585591b213SSam Leffler break; 23595591b213SSam Leffler case IEEE80211_MODE_11G: 23605591b213SSam Leffler sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11G); 23615591b213SSam Leffler break; 23625591b213SSam Leffler case IEEE80211_MODE_TURBO: 23635591b213SSam Leffler sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_TURBO); 23645591b213SSam Leffler break; 23655591b213SSam Leffler default: 23665591b213SSam Leffler DPRINTF(("%s: invalid mode %u\n", __func__, mode)); 23675591b213SSam Leffler return 0; 23685591b213SSam Leffler } 23695591b213SSam Leffler rt = sc->sc_rates[mode]; 23705591b213SSam Leffler if (rt == NULL) 23715591b213SSam Leffler return 0; 23725591b213SSam Leffler if (rt->rateCount > IEEE80211_RATE_MAXSIZE) { 23735591b213SSam Leffler DPRINTF(("%s: rate table too small (%u > %u)\n", 23745591b213SSam Leffler __func__, rt->rateCount, IEEE80211_RATE_MAXSIZE)); 23755591b213SSam Leffler maxrates = IEEE80211_RATE_MAXSIZE; 23765591b213SSam Leffler } else 23775591b213SSam Leffler maxrates = rt->rateCount; 23785591b213SSam Leffler rs = &ic->ic_sup_rates[mode]; 23795591b213SSam Leffler for (i = 0; i < maxrates; i++) 23805591b213SSam Leffler rs->rs_rates[i] = rt->info[i].dot11Rate; 23815591b213SSam Leffler rs->rs_nrates = maxrates; 23825591b213SSam Leffler return 1; 23835591b213SSam Leffler } 23845591b213SSam Leffler 23855591b213SSam Leffler static void 23865591b213SSam Leffler ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode) 23875591b213SSam Leffler { 23885591b213SSam Leffler const HAL_RATE_TABLE *rt; 23895591b213SSam Leffler int i; 23905591b213SSam Leffler 23915591b213SSam Leffler memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap)); 23925591b213SSam Leffler rt = sc->sc_rates[mode]; 23935591b213SSam Leffler KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode)); 23945591b213SSam Leffler for (i = 0; i < rt->rateCount; i++) 23955591b213SSam Leffler sc->sc_rixmap[rt->info[i].dot11Rate & IEEE80211_RATE_VAL] = i; 23965591b213SSam Leffler sc->sc_currates = rt; 23975591b213SSam Leffler sc->sc_curmode = mode; 23985591b213SSam Leffler } 23995591b213SSam Leffler 24005591b213SSam Leffler /* 24015591b213SSam Leffler * Reset the rate control state for each 802.11 state transition. 24025591b213SSam Leffler */ 24035591b213SSam Leffler static void 24045591b213SSam Leffler ath_rate_ctl_reset(struct ath_softc *sc, enum ieee80211_state state) 24055591b213SSam Leffler { 24065591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 24075591b213SSam Leffler struct ieee80211_node *ni; 24085591b213SSam Leffler struct ath_node *an; 24095591b213SSam Leffler 24105591b213SSam Leffler an = (struct ath_node *) ic->ic_bss; 24115591b213SSam Leffler an->an_tx_ok = an->an_tx_err = an->an_tx_retr = an->an_tx_upper = 0; 24125591b213SSam Leffler if (ic->ic_opmode == IEEE80211_M_STA) { 24135591b213SSam Leffler ni = ic->ic_bss; 24145591b213SSam Leffler if (state == IEEE80211_S_RUN) { 24155591b213SSam Leffler /* start with highest negotiated rate */ 24165591b213SSam Leffler KASSERT(ni->ni_rates.rs_nrates > 0, 24175591b213SSam Leffler ("transition to RUN state w/ no rates!")); 24185591b213SSam Leffler ni->ni_txrate = ni->ni_rates.rs_nrates - 1; 24195591b213SSam Leffler } else { 24205591b213SSam Leffler /* use lowest rate */ 24215591b213SSam Leffler ni->ni_txrate = 0; 24225591b213SSam Leffler } 24235591b213SSam Leffler } else { 24245591b213SSam Leffler TAILQ_FOREACH(ni, &ic->ic_node, ni_list) { 24255591b213SSam Leffler ni->ni_txrate = 0; /* use lowest rate */ 24265591b213SSam Leffler an = (struct ath_node *) ni; 24275591b213SSam Leffler an->an_tx_ok = an->an_tx_err = an->an_tx_retr = 24285591b213SSam Leffler an->an_tx_upper = 0; 24295591b213SSam Leffler } 24305591b213SSam Leffler } 24315591b213SSam Leffler } 24325591b213SSam Leffler 24335591b213SSam Leffler /* 24345591b213SSam Leffler * Examine and potentially adjust the transmit rate. 24355591b213SSam Leffler */ 24365591b213SSam Leffler static void 24375591b213SSam Leffler ath_rate_ctl(void *arg, struct ieee80211_node *ni) 24385591b213SSam Leffler { 24395591b213SSam Leffler struct ath_softc *sc = arg; 24405591b213SSam Leffler struct ath_node *an = (struct ath_node *) ni; 24415591b213SSam Leffler struct ieee80211_rateset *rs = &ni->ni_rates; 24425591b213SSam Leffler int mod = 0, orate, enough; 24435591b213SSam Leffler 24445591b213SSam Leffler /* 24455591b213SSam Leffler * Rate control 24465591b213SSam Leffler * XXX: very primitive version. 24475591b213SSam Leffler */ 24485591b213SSam Leffler sc->sc_stats.ast_rate_calls++; 24495591b213SSam Leffler 24505591b213SSam Leffler enough = (an->an_tx_ok + an->an_tx_err >= 10); 24515591b213SSam Leffler 24525591b213SSam Leffler /* no packet reached -> down */ 24535591b213SSam Leffler if (an->an_tx_err > 0 && an->an_tx_ok == 0) 24545591b213SSam Leffler mod = -1; 24555591b213SSam Leffler 24565591b213SSam Leffler /* all packets needs retry in average -> down */ 24575591b213SSam Leffler if (enough && an->an_tx_ok < an->an_tx_retr) 24585591b213SSam Leffler mod = -1; 24595591b213SSam Leffler 24605591b213SSam Leffler /* no error and less than 10% of packets needs retry -> up */ 24615591b213SSam Leffler if (enough && an->an_tx_err == 0 && an->an_tx_ok > an->an_tx_retr * 10) 24625591b213SSam Leffler mod = 1; 24635591b213SSam Leffler 24645591b213SSam Leffler orate = ni->ni_txrate; 24655591b213SSam Leffler switch (mod) { 24665591b213SSam Leffler case 0: 24675591b213SSam Leffler if (enough && an->an_tx_upper > 0) 24685591b213SSam Leffler an->an_tx_upper--; 24695591b213SSam Leffler break; 24705591b213SSam Leffler case -1: 24715591b213SSam Leffler if (ni->ni_txrate > 0) { 24725591b213SSam Leffler ni->ni_txrate--; 24735591b213SSam Leffler sc->sc_stats.ast_rate_drop++; 24745591b213SSam Leffler } 24755591b213SSam Leffler an->an_tx_upper = 0; 24765591b213SSam Leffler break; 24775591b213SSam Leffler case 1: 24785591b213SSam Leffler if (++an->an_tx_upper < 2) 24795591b213SSam Leffler break; 24805591b213SSam Leffler an->an_tx_upper = 0; 24815591b213SSam Leffler if (ni->ni_txrate + 1 < rs->rs_nrates) { 24825591b213SSam Leffler ni->ni_txrate++; 24835591b213SSam Leffler sc->sc_stats.ast_rate_raise++; 24845591b213SSam Leffler } 24855591b213SSam Leffler break; 24865591b213SSam Leffler } 24875591b213SSam Leffler 24885591b213SSam Leffler if (ni->ni_txrate != orate) { 24895591b213SSam Leffler printf("%s: %dM -> %dM (%d ok, %d err, %d retr)\n", 24905591b213SSam Leffler __func__, 24915591b213SSam Leffler (rs->rs_rates[orate] & IEEE80211_RATE_VAL) / 2, 24925591b213SSam Leffler (rs->rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL) / 2, 24935591b213SSam Leffler an->an_tx_ok, an->an_tx_err, an->an_tx_retr); 24945591b213SSam Leffler } 24955591b213SSam Leffler if (ni->ni_txrate != orate || enough) 24965591b213SSam Leffler an->an_tx_ok = an->an_tx_err = an->an_tx_retr = 0; 24975591b213SSam Leffler } 24985591b213SSam Leffler 24995591b213SSam Leffler #ifdef AR_DEBUG 25005591b213SSam Leffler static int 25015591b213SSam Leffler sysctl_hw_ath_dump(SYSCTL_HANDLER_ARGS) 25025591b213SSam Leffler { 25035591b213SSam Leffler char dmode[64]; 25045591b213SSam Leffler int error; 25055591b213SSam Leffler 25065591b213SSam Leffler strncpy(dmode, "", sizeof(dmode) - 1); 25075591b213SSam Leffler dmode[sizeof(dmode) - 1] = '\0'; 25085591b213SSam Leffler error = sysctl_handle_string(oidp, &dmode[0], sizeof(dmode), req); 25095591b213SSam Leffler 25105591b213SSam Leffler if (error == 0 && req->newptr != NULL) { 25115591b213SSam Leffler struct ifnet *ifp; 25125591b213SSam Leffler struct ath_softc *sc; 25135591b213SSam Leffler 25145591b213SSam Leffler ifp = ifunit("ath0"); /* XXX */ 25155591b213SSam Leffler if (!ifp) 25165591b213SSam Leffler return EINVAL; 25175591b213SSam Leffler sc = ifp->if_softc; 25185591b213SSam Leffler if (strcmp(dmode, "hal") == 0) 25195591b213SSam Leffler ath_hal_dumpstate(sc->sc_ah); 25205591b213SSam Leffler else if (strcmp(dmode, "eeprom") == 0) 25215591b213SSam Leffler ath_hal_dumpeeprom(sc->sc_ah); 25225591b213SSam Leffler else if (strcmp(dmode, "rfgain") == 0) 25235591b213SSam Leffler ath_hal_dumprfgain(sc->sc_ah); 25245591b213SSam Leffler else if (strcmp(dmode, "ani") == 0) 25255591b213SSam Leffler ath_hal_dumpani(sc->sc_ah); 25265591b213SSam Leffler else 25275591b213SSam Leffler return EINVAL; 25285591b213SSam Leffler } 25295591b213SSam Leffler return error; 25305591b213SSam Leffler } 25315591b213SSam Leffler SYSCTL_PROC(_hw_ath, OID_AUTO, dump, CTLTYPE_STRING | CTLFLAG_RW, 25325591b213SSam Leffler 0, 0, sysctl_hw_ath_dump, "A", "Dump driver state"); 25335591b213SSam Leffler 25345591b213SSam Leffler static void 25355591b213SSam Leffler ath_printrxbuf(struct ath_buf *bf, int done) 25365591b213SSam Leffler { 25375591b213SSam Leffler struct ath_desc *ds; 25385591b213SSam Leffler int i; 25395591b213SSam Leffler 25405591b213SSam Leffler for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) { 25415591b213SSam Leffler printf("R%d (%p %p) %08x %08x %08x %08x %08x %08x %c\n", 25425591b213SSam Leffler i, ds, (struct ath_desc *)bf->bf_daddr + i, 25435591b213SSam Leffler ds->ds_link, ds->ds_data, 25445591b213SSam Leffler ds->ds_ctl0, ds->ds_ctl1, 25455591b213SSam Leffler ds->ds_hw[0], ds->ds_hw[1], 25465591b213SSam Leffler !done ? ' ' : (ds->ds_rxstat.rs_status == 0) ? '*' : '!'); 25475591b213SSam Leffler } 25485591b213SSam Leffler } 25495591b213SSam Leffler 25505591b213SSam Leffler static void 25515591b213SSam Leffler ath_printtxbuf(struct ath_buf *bf, int done) 25525591b213SSam Leffler { 25535591b213SSam Leffler struct ath_desc *ds; 25545591b213SSam Leffler int i; 25555591b213SSam Leffler 25565591b213SSam Leffler for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) { 25575591b213SSam Leffler printf("T%d (%p %p) %08x %08x %08x %08x %08x %08x %08x %08x %c\n", 25585591b213SSam Leffler i, ds, (struct ath_desc *)bf->bf_daddr + i, 25595591b213SSam Leffler ds->ds_link, ds->ds_data, 25605591b213SSam Leffler ds->ds_ctl0, ds->ds_ctl1, 25615591b213SSam Leffler ds->ds_hw[0], ds->ds_hw[1], ds->ds_hw[2], ds->ds_hw[3], 25625591b213SSam Leffler !done ? ' ' : (ds->ds_txstat.ts_status == 0) ? '*' : '!'); 25635591b213SSam Leffler } 25645591b213SSam Leffler } 25655591b213SSam Leffler #endif /* AR_DEBUG */ 2566