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_var.h> 745591b213SSam Leffler 755591b213SSam Leffler #include <net/bpf.h> 765591b213SSam Leffler 775591b213SSam Leffler #ifdef INET 785591b213SSam Leffler #include <netinet/in.h> 795591b213SSam Leffler #include <netinet/if_ether.h> 805591b213SSam Leffler #endif 815591b213SSam Leffler 825591b213SSam Leffler #define AR_DEBUG 835591b213SSam Leffler #include <dev/ath/if_athvar.h> 845591b213SSam Leffler #include <contrib/dev/ath/ah_desc.h> 855591b213SSam Leffler 865591b213SSam Leffler /* unalligned little endian access */ 875591b213SSam Leffler #define LE_READ_2(p) \ 885591b213SSam Leffler ((u_int16_t) \ 895591b213SSam Leffler ((((u_int8_t *)(p))[0] ) | (((u_int8_t *)(p))[1] << 8))) 905591b213SSam Leffler #define LE_READ_4(p) \ 915591b213SSam Leffler ((u_int32_t) \ 925591b213SSam Leffler ((((u_int8_t *)(p))[0] ) | (((u_int8_t *)(p))[1] << 8) | \ 935591b213SSam Leffler (((u_int8_t *)(p))[2] << 16) | (((u_int8_t *)(p))[3] << 24))) 945591b213SSam Leffler 955591b213SSam Leffler static void ath_init(void *); 965591b213SSam Leffler static void ath_stop(struct ifnet *); 975591b213SSam Leffler static void ath_start(struct ifnet *); 985591b213SSam Leffler static void ath_reset(struct ath_softc *); 995591b213SSam Leffler static int ath_media_change(struct ifnet *); 1005591b213SSam Leffler static void ath_watchdog(struct ifnet *); 1015591b213SSam Leffler static int ath_ioctl(struct ifnet *, u_long, caddr_t); 1025591b213SSam Leffler static void ath_fatal_proc(void *, int); 1035591b213SSam Leffler static void ath_rxorn_proc(void *, int); 1045591b213SSam Leffler static void ath_bmiss_proc(void *, int); 1055591b213SSam Leffler static void ath_initkeytable(struct ath_softc *); 1065591b213SSam Leffler static void ath_mode_init(struct ath_softc *); 1075591b213SSam Leffler static int ath_beacon_alloc(struct ath_softc *, struct ieee80211_node *); 1085591b213SSam Leffler static void ath_beacon_proc(void *, int); 1095591b213SSam Leffler static void ath_beacon_free(struct ath_softc *); 1105591b213SSam Leffler static void ath_beacon_config(struct ath_softc *); 1115591b213SSam Leffler static int ath_desc_alloc(struct ath_softc *); 1125591b213SSam Leffler static void ath_desc_free(struct ath_softc *); 1135591b213SSam Leffler static struct ieee80211_node *ath_node_alloc(struct ieee80211com *); 1145591b213SSam Leffler static void ath_node_free(struct ieee80211com *, struct ieee80211_node *); 1155591b213SSam Leffler static void ath_node_copy(struct ieee80211com *, 1165591b213SSam Leffler struct ieee80211_node *, const struct ieee80211_node *); 117de5af704SSam Leffler static u_int8_t ath_node_getrssi(struct ieee80211com *, 118de5af704SSam Leffler struct ieee80211_node *); 1195591b213SSam Leffler static int ath_rxbuf_init(struct ath_softc *, struct ath_buf *); 1205591b213SSam Leffler static void ath_rx_proc(void *, int); 1215591b213SSam Leffler static int ath_tx_start(struct ath_softc *, struct ieee80211_node *, 1225591b213SSam Leffler struct ath_buf *, struct mbuf *); 1235591b213SSam Leffler static void ath_tx_proc(void *, int); 1245591b213SSam Leffler static int ath_chan_set(struct ath_softc *, struct ieee80211_channel *); 1255591b213SSam Leffler static void ath_draintxq(struct ath_softc *); 1265591b213SSam Leffler static void ath_stoprecv(struct ath_softc *); 1275591b213SSam Leffler static int ath_startrecv(struct ath_softc *); 1285591b213SSam Leffler static void ath_next_scan(void *); 1295591b213SSam Leffler static void ath_calibrate(void *); 13045bbf62fSSam Leffler static int ath_newstate(struct ieee80211com *, enum ieee80211_state, int); 1315591b213SSam Leffler static void ath_newassoc(struct ieee80211com *, 1325591b213SSam Leffler struct ieee80211_node *, int); 1335591b213SSam Leffler static int ath_getchannels(struct ath_softc *, u_int cc, HAL_BOOL outdoor); 1345591b213SSam Leffler 1355591b213SSam Leffler static int ath_rate_setup(struct ath_softc *sc, u_int mode); 1365591b213SSam Leffler static void ath_setcurmode(struct ath_softc *, enum ieee80211_phymode); 1375591b213SSam Leffler static void ath_rate_ctl_reset(struct ath_softc *, enum ieee80211_state); 1385591b213SSam Leffler static void ath_rate_ctl(void *, struct ieee80211_node *); 1395591b213SSam Leffler 1405591b213SSam Leffler SYSCTL_DECL(_hw_ath); 1415591b213SSam Leffler 1425591b213SSam Leffler /* XXX validate sysctl values */ 1435591b213SSam Leffler static int ath_dwelltime = 200; /* 5 channels/second */ 1445591b213SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, dwell, CTLFLAG_RW, &ath_dwelltime, 1455591b213SSam Leffler 0, "channel dwell time (ms) for AP/station scanning"); 1465591b213SSam Leffler static int ath_calinterval = 30; /* calibrate every 30 secs */ 1475591b213SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, calibrate, CTLFLAG_RW, &ath_calinterval, 1485591b213SSam Leffler 0, "chip calibration interval (secs)"); 14945cabbdcSSam Leffler static int ath_outdoor = AH_TRUE; /* outdoor operation */ 15045cabbdcSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, outdoor, CTLFLAG_RD, &ath_outdoor, 15145cabbdcSSam Leffler 0, "enable/disable outdoor operation"); 15245cabbdcSSam Leffler static int ath_countrycode = CTRY_DEFAULT; /* country code */ 15345cabbdcSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, countrycode, CTLFLAG_RD, &ath_countrycode, 15445cabbdcSSam Leffler 0, "country code"); 15545cabbdcSSam Leffler static int ath_regdomain = 0; /* regulatory domain */ 15645cabbdcSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, regdomain, CTLFLAG_RD, &ath_regdomain, 15745cabbdcSSam Leffler 0, "regulatory domain"); 1585591b213SSam Leffler 1595591b213SSam Leffler #ifdef AR_DEBUG 1605591b213SSam Leffler int ath_debug = 0; 1615591b213SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, debug, CTLFLAG_RW, &ath_debug, 1625591b213SSam Leffler 0, "control debugging printfs"); 1635591b213SSam Leffler #define IFF_DUMPPKTS(_ifp) \ 1645591b213SSam Leffler (ath_debug || \ 1655591b213SSam Leffler ((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2)) 1665591b213SSam Leffler static void ath_printrxbuf(struct ath_buf *bf, int); 1675591b213SSam Leffler static void ath_printtxbuf(struct ath_buf *bf, int); 1685591b213SSam Leffler #define DPRINTF(X) if (ath_debug) printf X 1695591b213SSam Leffler #define DPRINTF2(X) if (ath_debug > 1) printf X 1705591b213SSam Leffler #else 1715591b213SSam Leffler #define IFF_DUMPPKTS(_ifp) \ 1725591b213SSam Leffler (((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2)) 1735591b213SSam Leffler #define DPRINTF(X) 1745591b213SSam Leffler #define DPRINTF2(X) 1755591b213SSam Leffler #endif 1765591b213SSam Leffler 1775591b213SSam Leffler int 1785591b213SSam Leffler ath_attach(u_int16_t devid, struct ath_softc *sc) 1795591b213SSam Leffler { 1805591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 1815591b213SSam Leffler struct ifnet *ifp = &ic->ic_if; 1825591b213SSam Leffler struct ath_hal *ah; 1835591b213SSam Leffler HAL_STATUS status; 1845591b213SSam Leffler int error = 0; 1855591b213SSam Leffler 1865591b213SSam Leffler DPRINTF(("ath_attach: devid 0x%x\n", devid)); 1875591b213SSam Leffler 1885591b213SSam Leffler /* set these up early for if_printf use */ 1895591b213SSam Leffler ifp->if_unit = device_get_unit(sc->sc_dev); 1905591b213SSam Leffler ifp->if_name = "ath"; 1915591b213SSam Leffler 1925591b213SSam Leffler ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh, &status); 1935591b213SSam Leffler if (ah == NULL) { 1945591b213SSam Leffler if_printf(ifp, "unable to attach hardware; HAL status %u\n", 1955591b213SSam Leffler status); 1965591b213SSam Leffler error = ENXIO; 1975591b213SSam Leffler goto bad; 1985591b213SSam Leffler } 1995591b213SSam Leffler sc->sc_ah = ah; 200b58b3803SSam Leffler sc->sc_invalid = 0; /* ready to go, enable interrupt handling */ 2015591b213SSam Leffler 2025591b213SSam Leffler /* 2035591b213SSam Leffler * Collect the channel list using the default country 2045591b213SSam Leffler * code and including outdoor channels. The 802.11 layer 20545cabbdcSSam Leffler * is resposible for filtering this list based on settings 20645cabbdcSSam Leffler * like the phy mode. 2075591b213SSam Leffler */ 20845cabbdcSSam Leffler error = ath_getchannels(sc, ath_countrycode, ath_outdoor); 2095591b213SSam Leffler if (error != 0) 2105591b213SSam Leffler goto bad; 21145cabbdcSSam Leffler /* 21245cabbdcSSam Leffler * Copy these back; they are set as a side effect 21345cabbdcSSam Leffler * of constructing the channel list. 21445cabbdcSSam Leffler */ 21545cabbdcSSam Leffler ath_regdomain = ath_hal_getregdomain(ah); 21645cabbdcSSam Leffler ath_countrycode = ath_hal_getcountrycode(ah); 2175591b213SSam Leffler 2185591b213SSam Leffler /* 2195591b213SSam Leffler * Setup rate tables for all potential media types. 2205591b213SSam Leffler */ 2215591b213SSam Leffler ath_rate_setup(sc, IEEE80211_MODE_11A); 2225591b213SSam Leffler ath_rate_setup(sc, IEEE80211_MODE_11B); 2235591b213SSam Leffler ath_rate_setup(sc, IEEE80211_MODE_11G); 2245591b213SSam Leffler ath_rate_setup(sc, IEEE80211_MODE_TURBO); 2255591b213SSam Leffler 2265591b213SSam Leffler error = ath_desc_alloc(sc); 2275591b213SSam Leffler if (error != 0) { 2285591b213SSam Leffler if_printf(ifp, "failed to allocate descriptors: %d\n", error); 2295591b213SSam Leffler goto bad; 2305591b213SSam Leffler } 2312274d8c8SSam Leffler callout_init(&sc->sc_scan_ch, CALLOUT_MPSAFE); 2322274d8c8SSam Leffler callout_init(&sc->sc_cal_ch, CALLOUT_MPSAFE); 2335591b213SSam Leffler 2345591b213SSam Leffler mtx_init(&sc->sc_txbuflock, 2355591b213SSam Leffler device_get_nameunit(sc->sc_dev), "xmit buf q", MTX_DEF); 2365591b213SSam Leffler mtx_init(&sc->sc_txqlock, 2375591b213SSam Leffler device_get_nameunit(sc->sc_dev), "xmit q", MTX_DEF); 2385591b213SSam Leffler 2395591b213SSam Leffler TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc, sc); 2405591b213SSam Leffler TASK_INIT(&sc->sc_rxtask, 0, ath_rx_proc, sc); 2415591b213SSam Leffler TASK_INIT(&sc->sc_swbatask, 0, ath_beacon_proc, sc); 2425591b213SSam Leffler TASK_INIT(&sc->sc_rxorntask, 0, ath_rxorn_proc, sc); 2435591b213SSam Leffler TASK_INIT(&sc->sc_fataltask, 0, ath_fatal_proc, sc); 2445591b213SSam Leffler TASK_INIT(&sc->sc_bmisstask, 0, ath_bmiss_proc, sc); 2455591b213SSam Leffler 2465591b213SSam Leffler /* 2475591b213SSam Leffler * For now just pre-allocate one data queue and one 2485591b213SSam Leffler * beacon queue. Note that the HAL handles resetting 2495591b213SSam Leffler * them at the needed time. Eventually we'll want to 2505591b213SSam Leffler * allocate more tx queues for splitting management 2515591b213SSam Leffler * frames and for QOS support. 2525591b213SSam Leffler */ 2535591b213SSam Leffler sc->sc_txhalq = ath_hal_setuptxqueue(ah, 2545591b213SSam Leffler HAL_TX_QUEUE_DATA, 2555591b213SSam Leffler AH_TRUE /* enable interrupts */ 2565591b213SSam Leffler ); 2575591b213SSam Leffler if (sc->sc_txhalq == (u_int) -1) { 2585591b213SSam Leffler if_printf(ifp, "unable to setup a data xmit queue!\n"); 2595591b213SSam Leffler goto bad; 2605591b213SSam Leffler } 2615591b213SSam Leffler sc->sc_bhalq = ath_hal_setuptxqueue(ah, 2625591b213SSam Leffler HAL_TX_QUEUE_BEACON, 2635591b213SSam Leffler AH_TRUE /* enable interrupts */ 2645591b213SSam Leffler ); 2655591b213SSam Leffler if (sc->sc_bhalq == (u_int) -1) { 2665591b213SSam Leffler if_printf(ifp, "unable to setup a beacon xmit queue!\n"); 2675591b213SSam Leffler goto bad; 2685591b213SSam Leffler } 2695591b213SSam Leffler 2705591b213SSam Leffler ifp->if_softc = sc; 2715591b213SSam Leffler ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST; 2725591b213SSam Leffler ifp->if_start = ath_start; 2735591b213SSam Leffler ifp->if_watchdog = ath_watchdog; 2745591b213SSam Leffler ifp->if_ioctl = ath_ioctl; 2755591b213SSam Leffler ifp->if_init = ath_init; 2765591b213SSam Leffler ifp->if_snd.ifq_maxlen = IFQ_MAXLEN; 2775591b213SSam Leffler 2785591b213SSam Leffler ic->ic_softc = sc; 2795591b213SSam Leffler ic->ic_newassoc = ath_newassoc; 2805591b213SSam Leffler /* XXX not right but it's not used anywhere important */ 2815591b213SSam Leffler ic->ic_phytype = IEEE80211_T_OFDM; 2825591b213SSam Leffler ic->ic_opmode = IEEE80211_M_STA; 2836b59f5e3SSam Leffler ic->ic_caps = IEEE80211_C_WEP | IEEE80211_C_IBSS | IEEE80211_C_HOSTAP 284f6aa038bSSam Leffler | IEEE80211_C_MONITOR | IEEE80211_C_SHPREAMBLE; 2855591b213SSam Leffler 2865591b213SSam Leffler /* get mac address from hardware */ 2875591b213SSam Leffler ath_hal_getmac(ah, ic->ic_myaddr); 2885591b213SSam Leffler 2895591b213SSam Leffler /* call MI attach routine. */ 2905591b213SSam Leffler ieee80211_ifattach(ifp); 2915591b213SSam Leffler /* override default methods */ 2925591b213SSam Leffler ic->ic_node_alloc = ath_node_alloc; 2935591b213SSam Leffler ic->ic_node_free = ath_node_free; 2945591b213SSam Leffler ic->ic_node_copy = ath_node_copy; 295de5af704SSam Leffler ic->ic_node_getrssi = ath_node_getrssi; 29645bbf62fSSam Leffler sc->sc_newstate = ic->ic_newstate; 29745bbf62fSSam Leffler ic->ic_newstate = ath_newstate; 29845bbf62fSSam Leffler /* complete initialization */ 2995591b213SSam Leffler ieee80211_media_init(ifp, ath_media_change, ieee80211_media_status); 3005591b213SSam Leffler 30173454c73SSam Leffler bpfattach2(ifp, DLT_IEEE802_11_RADIO, 30273454c73SSam Leffler sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th), 30373454c73SSam Leffler &sc->sc_drvbpf); 30473454c73SSam Leffler /* 30573454c73SSam Leffler * Initialize constant fields. 30673454c73SSam Leffler * 30773454c73SSam Leffler * NB: the channel is setup each time we transition to the 30873454c73SSam Leffler * RUN state to avoid filling it in for each frame. 30973454c73SSam Leffler */ 31073454c73SSam Leffler sc->sc_tx_th.wt_ihdr.it_len = sizeof(sc->sc_tx_th); 31173454c73SSam Leffler sc->sc_tx_th.wt_ihdr.it_present = ATH_TX_RADIOTAP_PRESENT; 31273454c73SSam Leffler 31373454c73SSam Leffler sc->sc_rx_th.wr_ihdr.it_len = sizeof(sc->sc_rx_th); 31473454c73SSam Leffler sc->sc_rx_th.wr_ihdr.it_present = ATH_RX_RADIOTAP_PRESENT; 31573454c73SSam Leffler 3165591b213SSam Leffler if_printf(ifp, "802.11 address: %s\n", ether_sprintf(ic->ic_myaddr)); 3175591b213SSam Leffler 3185591b213SSam Leffler return 0; 3195591b213SSam Leffler bad: 3205591b213SSam Leffler if (ah) 3215591b213SSam Leffler ath_hal_detach(ah); 3225591b213SSam Leffler sc->sc_invalid = 1; 3235591b213SSam Leffler return error; 3245591b213SSam Leffler } 3255591b213SSam Leffler 3265591b213SSam Leffler int 3275591b213SSam Leffler ath_detach(struct ath_softc *sc) 3285591b213SSam Leffler { 3295591b213SSam Leffler struct ifnet *ifp = &sc->sc_ic.ic_if; 3305591b213SSam Leffler 3315591b213SSam Leffler DPRINTF(("ath_detach: if_flags %x\n", ifp->if_flags)); 3325591b213SSam Leffler 3335591b213SSam Leffler mtx_lock(&sc->sc_mtx); 3345591b213SSam Leffler ath_stop(ifp); 33573454c73SSam Leffler bpfdetach(ifp); 3365591b213SSam Leffler ath_desc_free(sc); 3375591b213SSam Leffler ath_hal_detach(sc->sc_ah); 3385591b213SSam Leffler ieee80211_ifdetach(ifp); 3395591b213SSam Leffler mtx_unlock(&sc->sc_mtx); 3405591b213SSam Leffler return 0; 3415591b213SSam Leffler } 3425591b213SSam Leffler 3435591b213SSam Leffler void 3445591b213SSam Leffler ath_suspend(struct ath_softc *sc) 3455591b213SSam Leffler { 3465591b213SSam Leffler struct ifnet *ifp = &sc->sc_ic.ic_if; 3475591b213SSam Leffler 3485591b213SSam Leffler DPRINTF(("ath_suspend: if_flags %x\n", ifp->if_flags)); 3495591b213SSam Leffler 3505591b213SSam Leffler ath_stop(ifp); 3515591b213SSam Leffler } 3525591b213SSam Leffler 3535591b213SSam Leffler void 3545591b213SSam Leffler ath_resume(struct ath_softc *sc) 3555591b213SSam Leffler { 3565591b213SSam Leffler struct ifnet *ifp = &sc->sc_ic.ic_if; 3575591b213SSam Leffler 3585591b213SSam Leffler DPRINTF(("ath_resume: if_flags %x\n", ifp->if_flags)); 3595591b213SSam Leffler 3606b59f5e3SSam Leffler if (ifp->if_flags & IFF_UP) { 3615591b213SSam Leffler ath_init(ifp); 3626b59f5e3SSam Leffler if (ifp->if_flags & IFF_RUNNING) 3635591b213SSam Leffler ath_start(ifp); 3645591b213SSam Leffler } 3656b59f5e3SSam Leffler } 3665591b213SSam Leffler 3675591b213SSam Leffler void 3685591b213SSam Leffler ath_shutdown(struct ath_softc *sc) 3695591b213SSam Leffler { 3705591b213SSam Leffler struct ifnet *ifp = &sc->sc_ic.ic_if; 3715591b213SSam Leffler 3725591b213SSam Leffler DPRINTF(("ath_shutdown: if_flags %x\n", ifp->if_flags)); 3735591b213SSam Leffler 3745591b213SSam Leffler ath_stop(ifp); 3755591b213SSam Leffler } 3765591b213SSam Leffler 3775591b213SSam Leffler void 3785591b213SSam Leffler ath_intr(void *arg) 3795591b213SSam Leffler { 3805591b213SSam Leffler struct ath_softc *sc = arg; 3815591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 3825591b213SSam Leffler struct ifnet *ifp = &ic->ic_if; 3835591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 3845591b213SSam Leffler HAL_INT status; 3855591b213SSam Leffler 3865591b213SSam Leffler if (sc->sc_invalid) { 3875591b213SSam Leffler /* 388b58b3803SSam Leffler * The hardware is not ready/present, don't touch anything. 389b58b3803SSam Leffler * Note this can happen early on if the IRQ is shared. 3905591b213SSam Leffler */ 3915591b213SSam Leffler DPRINTF(("ath_intr: invalid; ignored\n")); 3925591b213SSam Leffler return; 3935591b213SSam Leffler } 3945591b213SSam Leffler if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) != (IFF_RUNNING|IFF_UP)) { 3955591b213SSam Leffler DPRINTF(("ath_intr: if_flags 0x%x\n", ifp->if_flags)); 3965591b213SSam Leffler ath_hal_getisr(ah, &status); /* clear ISR */ 3975591b213SSam Leffler ath_hal_intrset(ah, 0); /* disable further intr's */ 3985591b213SSam Leffler return; 3995591b213SSam Leffler } 4005591b213SSam Leffler ath_hal_getisr(ah, &status); /* NB: clears ISR too */ 4015591b213SSam Leffler DPRINTF2(("ath_intr: status 0x%x\n", status)); 4025591b213SSam Leffler #ifdef AR_DEBUG 4035591b213SSam Leffler if (ath_debug && 4045591b213SSam Leffler (status & (HAL_INT_FATAL|HAL_INT_RXORN|HAL_INT_BMISS))) { 4055591b213SSam Leffler if_printf(ifp, "ath_intr: status 0x%x\n", status); 4065591b213SSam Leffler ath_hal_dumpstate(ah); 4075591b213SSam Leffler } 4085591b213SSam Leffler #endif /* AR_DEBUG */ 409ecddff40SSam Leffler status &= sc->sc_imask; /* discard unasked for bits */ 4105591b213SSam Leffler if (status & HAL_INT_FATAL) { 4115591b213SSam Leffler sc->sc_stats.ast_hardware++; 4125591b213SSam Leffler ath_hal_intrset(ah, 0); /* disable intr's until reset */ 4135591b213SSam Leffler taskqueue_enqueue(taskqueue_swi, &sc->sc_fataltask); 4145591b213SSam Leffler } else if (status & HAL_INT_RXORN) { 4155591b213SSam Leffler sc->sc_stats.ast_rxorn++; 4165591b213SSam Leffler ath_hal_intrset(ah, 0); /* disable intr's until reset */ 4175591b213SSam Leffler taskqueue_enqueue(taskqueue_swi, &sc->sc_rxorntask); 4185591b213SSam Leffler } else { 4195591b213SSam Leffler if (status & HAL_INT_RXEOL) { 4205591b213SSam Leffler /* 4215591b213SSam Leffler * NB: the hardware should re-read the link when 4225591b213SSam Leffler * RXE bit is written, but it doesn't work at 4235591b213SSam Leffler * least on older hardware revs. 4245591b213SSam Leffler */ 4255591b213SSam Leffler sc->sc_stats.ast_rxeol++; 4265591b213SSam Leffler sc->sc_rxlink = NULL; 4275591b213SSam Leffler } 4285591b213SSam Leffler if (status & HAL_INT_TXURN) { 4295591b213SSam Leffler sc->sc_stats.ast_txurn++; 4305591b213SSam Leffler /* bump tx trigger level */ 4315591b213SSam Leffler ath_hal_updatetxtriglevel(ah, AH_TRUE); 4325591b213SSam Leffler } 4335591b213SSam Leffler if (status & HAL_INT_RX) 4345591b213SSam Leffler taskqueue_enqueue(taskqueue_swi, &sc->sc_rxtask); 4355591b213SSam Leffler if (status & HAL_INT_TX) 4365591b213SSam Leffler taskqueue_enqueue(taskqueue_swi, &sc->sc_txtask); 4375591b213SSam Leffler if (status & HAL_INT_SWBA) 4385591b213SSam Leffler taskqueue_enqueue(taskqueue_swi, &sc->sc_swbatask); 4395591b213SSam Leffler if (status & HAL_INT_BMISS) { 4405591b213SSam Leffler sc->sc_stats.ast_bmiss++; 4415591b213SSam Leffler taskqueue_enqueue(taskqueue_swi, &sc->sc_bmisstask); 4425591b213SSam Leffler } 4435591b213SSam Leffler } 4445591b213SSam Leffler } 4455591b213SSam Leffler 4465591b213SSam Leffler static void 4475591b213SSam Leffler ath_fatal_proc(void *arg, int pending) 4485591b213SSam Leffler { 4495591b213SSam Leffler struct ath_softc *sc = arg; 4505591b213SSam Leffler 4515591b213SSam Leffler device_printf(sc->sc_dev, "hardware error; resetting\n"); 4525591b213SSam Leffler ath_reset(sc); 4535591b213SSam Leffler } 4545591b213SSam Leffler 4555591b213SSam Leffler static void 4565591b213SSam Leffler ath_rxorn_proc(void *arg, int pending) 4575591b213SSam Leffler { 4585591b213SSam Leffler struct ath_softc *sc = arg; 4595591b213SSam Leffler 4605591b213SSam Leffler device_printf(sc->sc_dev, "rx FIFO overrun; resetting\n"); 4615591b213SSam Leffler ath_reset(sc); 4625591b213SSam Leffler } 4635591b213SSam Leffler 4645591b213SSam Leffler static void 4655591b213SSam Leffler ath_bmiss_proc(void *arg, int pending) 4665591b213SSam Leffler { 4675591b213SSam Leffler struct ath_softc *sc = arg; 4685591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 4695591b213SSam Leffler 4705591b213SSam Leffler DPRINTF(("ath_bmiss_proc: pending %u\n", pending)); 4715591b213SSam Leffler KASSERT(ic->ic_opmode == IEEE80211_M_STA, 4725591b213SSam Leffler ("unexpect operating mode %u", ic->ic_opmode)); 4735591b213SSam Leffler if (ic->ic_state == IEEE80211_S_RUN) 47445bbf62fSSam Leffler ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 4755591b213SSam Leffler } 4765591b213SSam Leffler 4775591b213SSam Leffler static u_int 4785591b213SSam Leffler ath_chan2flags(struct ieee80211com *ic, struct ieee80211_channel *chan) 4795591b213SSam Leffler { 4805591b213SSam Leffler static const u_int modeflags[] = { 4815591b213SSam Leffler 0, /* IEEE80211_MODE_AUTO */ 4825591b213SSam Leffler CHANNEL_A, /* IEEE80211_MODE_11A */ 4835591b213SSam Leffler CHANNEL_B, /* IEEE80211_MODE_11B */ 4845591b213SSam Leffler CHANNEL_PUREG, /* IEEE80211_MODE_11G */ 4855591b213SSam Leffler CHANNEL_T /* IEEE80211_MODE_TURBO */ 4865591b213SSam Leffler }; 4875591b213SSam Leffler return modeflags[ieee80211_chan2mode(ic, chan)]; 4885591b213SSam Leffler } 4895591b213SSam Leffler 4905591b213SSam Leffler static void 4915591b213SSam Leffler ath_init(void *arg) 4925591b213SSam Leffler { 4935591b213SSam Leffler struct ath_softc *sc = (struct ath_softc *) arg; 4945591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 4955591b213SSam Leffler struct ifnet *ifp = &ic->ic_if; 4965591b213SSam Leffler struct ieee80211_node *ni; 4975591b213SSam Leffler enum ieee80211_phymode mode; 4985591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 4995591b213SSam Leffler HAL_STATUS status; 5005591b213SSam Leffler HAL_CHANNEL hchan; 5015591b213SSam Leffler 5025591b213SSam Leffler DPRINTF(("ath_init: if_flags 0x%x\n", ifp->if_flags)); 5035591b213SSam Leffler 5045591b213SSam Leffler mtx_lock(&sc->sc_mtx); 5055591b213SSam Leffler /* 5065591b213SSam Leffler * Stop anything previously setup. This is safe 5075591b213SSam Leffler * whether this is the first time through or not. 5085591b213SSam Leffler */ 5095591b213SSam Leffler ath_stop(ifp); 5105591b213SSam Leffler 5115591b213SSam Leffler /* 5125591b213SSam Leffler * The basic interface to setting the hardware in a good 5135591b213SSam Leffler * state is ``reset''. On return the hardware is known to 5145591b213SSam Leffler * be powered up and with interrupts disabled. This must 5155591b213SSam Leffler * be followed by initialization of the appropriate bits 5165591b213SSam Leffler * and then setup of the interrupt mask. 5175591b213SSam Leffler */ 5185591b213SSam Leffler hchan.channel = ic->ic_ibss_chan->ic_freq; 5195591b213SSam Leffler hchan.channelFlags = ath_chan2flags(ic, ic->ic_ibss_chan); 5205591b213SSam Leffler if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_FALSE, &status)) { 5215591b213SSam Leffler if_printf(ifp, "unable to reset hardware; hal status %u\n", 5225591b213SSam Leffler status); 5235591b213SSam Leffler goto done; 5245591b213SSam Leffler } 5255591b213SSam Leffler 5265591b213SSam Leffler /* 5275591b213SSam Leffler * Setup the hardware after reset: the key cache 5285591b213SSam Leffler * is filled as needed and the receive engine is 5295591b213SSam Leffler * set going. Frame transmit is handled entirely 5305591b213SSam Leffler * in the frame output path; there's nothing to do 5315591b213SSam Leffler * here except setup the interrupt mask. 5325591b213SSam Leffler */ 5335591b213SSam Leffler if (ic->ic_flags & IEEE80211_F_WEPON) 5345591b213SSam Leffler ath_initkeytable(sc); 5355591b213SSam Leffler if (ath_startrecv(sc) != 0) { 5365591b213SSam Leffler if_printf(ifp, "unable to start recv logic\n"); 5375591b213SSam Leffler goto done; 5385591b213SSam Leffler } 5395591b213SSam Leffler 5405591b213SSam Leffler /* 5415591b213SSam Leffler * Enable interrupts. 5425591b213SSam Leffler */ 5435591b213SSam Leffler sc->sc_imask = HAL_INT_RX | HAL_INT_TX 5445591b213SSam Leffler | HAL_INT_RXEOL | HAL_INT_RXORN 5455591b213SSam Leffler | HAL_INT_FATAL | HAL_INT_GLOBAL; 5465591b213SSam Leffler ath_hal_intrset(ah, sc->sc_imask); 5475591b213SSam Leffler 5485591b213SSam Leffler ifp->if_flags |= IFF_RUNNING; 5495591b213SSam Leffler ic->ic_state = IEEE80211_S_INIT; 5505591b213SSam Leffler 5515591b213SSam Leffler /* 5525591b213SSam Leffler * The hardware should be ready to go now so it's safe 5535591b213SSam Leffler * to kick the 802.11 state machine as it's likely to 5545591b213SSam Leffler * immediately call back to us to send mgmt frames. 5555591b213SSam Leffler */ 5565591b213SSam Leffler ni = ic->ic_bss; 5575591b213SSam Leffler ni->ni_chan = ic->ic_ibss_chan; 5585591b213SSam Leffler mode = ieee80211_chan2mode(ic, ni->ni_chan); 5595591b213SSam Leffler if (mode != sc->sc_curmode) 5605591b213SSam Leffler ath_setcurmode(sc, mode); 5616b59f5e3SSam Leffler if (ic->ic_opmode != IEEE80211_M_MONITOR) 56245bbf62fSSam Leffler ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 5636b59f5e3SSam Leffler else 5646b59f5e3SSam Leffler ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 5655591b213SSam Leffler done: 5665591b213SSam Leffler mtx_unlock(&sc->sc_mtx); 5675591b213SSam Leffler } 5685591b213SSam Leffler 5695591b213SSam Leffler static void 5705591b213SSam Leffler ath_stop(struct ifnet *ifp) 5715591b213SSam Leffler { 57245bbf62fSSam Leffler struct ieee80211com *ic = (struct ieee80211com *) ifp; 5735591b213SSam Leffler struct ath_softc *sc = ifp->if_softc; 5745591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 5755591b213SSam Leffler 5765591b213SSam Leffler DPRINTF(("ath_stop: invalid %u if_flags 0x%x\n", 5775591b213SSam Leffler sc->sc_invalid, ifp->if_flags)); 5785591b213SSam Leffler 5795591b213SSam Leffler mtx_lock(&sc->sc_mtx); 5805591b213SSam Leffler if (ifp->if_flags & IFF_RUNNING) { 5815591b213SSam Leffler /* 5825591b213SSam Leffler * Shutdown the hardware and driver: 5835591b213SSam Leffler * disable interrupts 5845591b213SSam Leffler * turn off timers 5855591b213SSam Leffler * clear transmit machinery 5865591b213SSam Leffler * clear receive machinery 5875591b213SSam Leffler * drain and release tx queues 5885591b213SSam Leffler * reclaim beacon resources 5895591b213SSam Leffler * reset 802.11 state machine 5905591b213SSam Leffler * power down hardware 5915591b213SSam Leffler * 5925591b213SSam Leffler * Note that some of this work is not possible if the 5935591b213SSam Leffler * hardware is gone (invalid). 5945591b213SSam Leffler */ 5955591b213SSam Leffler ifp->if_flags &= ~IFF_RUNNING; 5965591b213SSam Leffler ifp->if_timer = 0; 5975591b213SSam Leffler if (!sc->sc_invalid) 5985591b213SSam Leffler ath_hal_intrset(ah, 0); 5995591b213SSam Leffler ath_draintxq(sc); 6005591b213SSam Leffler if (!sc->sc_invalid) 6015591b213SSam Leffler ath_stoprecv(sc); 6025591b213SSam Leffler else 6035591b213SSam Leffler sc->sc_rxlink = NULL; 6045591b213SSam Leffler IF_DRAIN(&ifp->if_snd); 6055591b213SSam Leffler ath_beacon_free(sc); 60645bbf62fSSam Leffler ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 6075591b213SSam Leffler if (!sc->sc_invalid) 6085591b213SSam Leffler ath_hal_setpower(ah, HAL_PM_FULL_SLEEP, 0); 6095591b213SSam Leffler } 6105591b213SSam Leffler mtx_unlock(&sc->sc_mtx); 6115591b213SSam Leffler } 6125591b213SSam Leffler 6135591b213SSam Leffler /* 6145591b213SSam Leffler * Reset the hardware w/o losing operational state. This is 6155591b213SSam Leffler * basically a more efficient way of doing ath_stop, ath_init, 6165591b213SSam Leffler * followed by state transitions to the current 802.11 6175591b213SSam Leffler * operational state. Used to recover from errors rx overrun 6185591b213SSam Leffler * and to reset the hardware when rf gain settings must be reset. 6195591b213SSam Leffler */ 6205591b213SSam Leffler static void 6215591b213SSam Leffler ath_reset(struct ath_softc *sc) 6225591b213SSam Leffler { 6235591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 6245591b213SSam Leffler struct ifnet *ifp = &ic->ic_if; 6255591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 6265591b213SSam Leffler struct ieee80211_channel *c; 6275591b213SSam Leffler HAL_STATUS status; 6285591b213SSam Leffler HAL_CHANNEL hchan; 6295591b213SSam Leffler 6305591b213SSam Leffler /* 6315591b213SSam Leffler * Convert to a HAL channel description with the flags 6325591b213SSam Leffler * constrained to reflect the current operating mode. 6335591b213SSam Leffler */ 6345591b213SSam Leffler c = ic->ic_ibss_chan; 6355591b213SSam Leffler hchan.channel = c->ic_freq; 6365591b213SSam Leffler hchan.channelFlags = ath_chan2flags(ic, c); 6375591b213SSam Leffler 6385591b213SSam Leffler ath_hal_intrset(ah, 0); /* disable interrupts */ 6395591b213SSam Leffler ath_draintxq(sc); /* stop xmit side */ 6405591b213SSam Leffler ath_stoprecv(sc); /* stop recv side */ 6415591b213SSam Leffler /* NB: indicate channel change so we do a full reset */ 6425591b213SSam Leffler if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_TRUE, &status)) 6435591b213SSam Leffler if_printf(ifp, "%s: unable to reset hardware; hal status %u\n", 6445591b213SSam Leffler __func__, status); 6455591b213SSam Leffler ath_hal_intrset(ah, sc->sc_imask); 6465591b213SSam Leffler if (ath_startrecv(sc) != 0) /* restart recv */ 6475591b213SSam Leffler if_printf(ifp, "%s: unable to start recv logic\n", __func__); 6485591b213SSam Leffler ath_start(ifp); /* restart xmit */ 6495591b213SSam Leffler if (ic->ic_state == IEEE80211_S_RUN) 6505591b213SSam Leffler ath_beacon_config(sc); /* restart beacons */ 6515591b213SSam Leffler } 6525591b213SSam Leffler 6535591b213SSam Leffler static void 6545591b213SSam Leffler ath_start(struct ifnet *ifp) 6555591b213SSam Leffler { 6565591b213SSam Leffler struct ath_softc *sc = ifp->if_softc; 6575591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 6585591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 6595591b213SSam Leffler struct ieee80211_node *ni; 6605591b213SSam Leffler struct ath_buf *bf; 6615591b213SSam Leffler struct mbuf *m; 6625591b213SSam Leffler struct ieee80211_frame *wh; 6635591b213SSam Leffler 6645591b213SSam Leffler if ((ifp->if_flags & IFF_RUNNING) == 0 || sc->sc_invalid) 6655591b213SSam Leffler return; 6665591b213SSam Leffler for (;;) { 6675591b213SSam Leffler /* 6685591b213SSam Leffler * Grab a TX buffer and associated resources. 6695591b213SSam Leffler */ 6705591b213SSam Leffler mtx_lock(&sc->sc_txbuflock); 6715591b213SSam Leffler bf = TAILQ_FIRST(&sc->sc_txbuf); 6725591b213SSam Leffler if (bf != NULL) 6735591b213SSam Leffler TAILQ_REMOVE(&sc->sc_txbuf, bf, bf_list); 6745591b213SSam Leffler mtx_unlock(&sc->sc_txbuflock); 6755591b213SSam Leffler if (bf == NULL) { 6765591b213SSam Leffler DPRINTF(("ath_start: out of xmit buffers\n")); 6775591b213SSam Leffler sc->sc_stats.ast_tx_qstop++; 6785591b213SSam Leffler ifp->if_flags |= IFF_OACTIVE; 6795591b213SSam Leffler break; 6805591b213SSam Leffler } 6815591b213SSam Leffler /* 6825591b213SSam Leffler * Poll the management queue for frames; they 6835591b213SSam Leffler * have priority over normal data frames. 6845591b213SSam Leffler */ 6855591b213SSam Leffler IF_DEQUEUE(&ic->ic_mgtq, m); 6865591b213SSam Leffler if (m == NULL) { 6875591b213SSam Leffler /* 6885591b213SSam Leffler * No data frames go out unless we're associated. 6895591b213SSam Leffler */ 6905591b213SSam Leffler if (ic->ic_state != IEEE80211_S_RUN) { 6915591b213SSam Leffler DPRINTF(("ath_start: ignore data packet, " 6925591b213SSam Leffler "state %u\n", ic->ic_state)); 6935591b213SSam Leffler sc->sc_stats.ast_tx_discard++; 6945591b213SSam Leffler mtx_lock(&sc->sc_txbuflock); 6955591b213SSam Leffler TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list); 6965591b213SSam Leffler mtx_unlock(&sc->sc_txbuflock); 6975591b213SSam Leffler break; 6985591b213SSam Leffler } 6995591b213SSam Leffler IF_DEQUEUE(&ifp->if_snd, m); 7005591b213SSam Leffler if (m == NULL) { 7015591b213SSam Leffler mtx_lock(&sc->sc_txbuflock); 7025591b213SSam Leffler TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list); 7035591b213SSam Leffler mtx_unlock(&sc->sc_txbuflock); 7045591b213SSam Leffler break; 7055591b213SSam Leffler } 7065591b213SSam Leffler ifp->if_opackets++; 7075591b213SSam Leffler BPF_MTAP(ifp, m); 7085591b213SSam Leffler /* 7095591b213SSam Leffler * Encapsulate the packet in prep for transmission. 7105591b213SSam Leffler */ 7110a915fadSSam Leffler m = ieee80211_encap(ifp, m, &ni); 7125591b213SSam Leffler if (m == NULL) { 7135591b213SSam Leffler DPRINTF(("ath_start: encapsulation failure\n")); 7145591b213SSam Leffler sc->sc_stats.ast_tx_encap++; 7155591b213SSam Leffler goto bad; 7165591b213SSam Leffler } 7175591b213SSam Leffler wh = mtod(m, struct ieee80211_frame *); 7185591b213SSam Leffler if (ic->ic_flags & IEEE80211_F_WEPON) 7195591b213SSam Leffler wh->i_fc[1] |= IEEE80211_FC1_WEP; 7205591b213SSam Leffler } else { 7210a915fadSSam Leffler /* 7220a915fadSSam Leffler * Hack! The referenced node pointer is in the 7230a915fadSSam Leffler * rcvif field of the packet header. This is 7240a915fadSSam Leffler * placed there by ieee80211_mgmt_output because 7250a915fadSSam Leffler * we need to hold the reference with the frame 7260a915fadSSam Leffler * and there's no other way (other than packet 7270a915fadSSam Leffler * tags which we consider too expensive to use) 7280a915fadSSam Leffler * to pass it along. 7290a915fadSSam Leffler */ 7300a915fadSSam Leffler ni = (struct ieee80211_node *) m->m_pkthdr.rcvif; 7310a915fadSSam Leffler m->m_pkthdr.rcvif = NULL; 7320a915fadSSam Leffler 7335591b213SSam Leffler wh = mtod(m, struct ieee80211_frame *); 7345591b213SSam Leffler if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == 7355591b213SSam Leffler IEEE80211_FC0_SUBTYPE_PROBE_RESP) { 7365591b213SSam Leffler /* fill time stamp */ 7375591b213SSam Leffler u_int64_t tsf; 7385591b213SSam Leffler u_int32_t *tstamp; 7395591b213SSam Leffler 7405591b213SSam Leffler tsf = ath_hal_gettsf64(ah); 7415591b213SSam Leffler /* XXX: adjust 100us delay to xmit */ 7425591b213SSam Leffler tsf += 100; 7435591b213SSam Leffler tstamp = (u_int32_t *)&wh[1]; 7445591b213SSam Leffler tstamp[0] = htole32(tsf & 0xffffffff); 7455591b213SSam Leffler tstamp[1] = htole32(tsf >> 32); 7465591b213SSam Leffler } 7475591b213SSam Leffler sc->sc_stats.ast_tx_mgmt++; 7485591b213SSam Leffler } 7495591b213SSam Leffler if (ic->ic_rawbpf) 7505591b213SSam Leffler bpf_mtap(ic->ic_rawbpf, m); 7515591b213SSam Leffler 75273454c73SSam Leffler if (sc->sc_drvbpf) { 75373454c73SSam Leffler struct mbuf *mb; 75473454c73SSam Leffler 75573454c73SSam Leffler MGETHDR(mb, M_DONTWAIT, m->m_type); 75673454c73SSam Leffler if (mb != NULL) { 75773454c73SSam Leffler sc->sc_tx_th.wt_rate = 75873454c73SSam Leffler ni->ni_rates.rs_rates[ni->ni_txrate]; 75973454c73SSam Leffler 76073454c73SSam Leffler mb->m_next = m; 76173454c73SSam Leffler mb->m_data = (caddr_t)&sc->sc_tx_th; 76273454c73SSam Leffler mb->m_len = sizeof(sc->sc_tx_th); 76373454c73SSam Leffler mb->m_pkthdr.len += mb->m_len; 76473454c73SSam Leffler bpf_mtap(sc->sc_drvbpf, mb); 76573454c73SSam Leffler m_free(mb); 76673454c73SSam Leffler } 76773454c73SSam Leffler } 76873454c73SSam Leffler 7695591b213SSam Leffler if (ath_tx_start(sc, ni, bf, m)) { 7705591b213SSam Leffler bad: 7715591b213SSam Leffler mtx_lock(&sc->sc_txbuflock); 7725591b213SSam Leffler TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list); 7735591b213SSam Leffler mtx_unlock(&sc->sc_txbuflock); 7745591b213SSam Leffler ifp->if_oerrors++; 7750a915fadSSam Leffler if (ni && ni != ic->ic_bss) 7760a915fadSSam Leffler ieee80211_free_node(ic, ni); 7775591b213SSam Leffler continue; 7785591b213SSam Leffler } 7795591b213SSam Leffler 7805591b213SSam Leffler sc->sc_tx_timer = 5; 7815591b213SSam Leffler ifp->if_timer = 1; 7825591b213SSam Leffler } 7835591b213SSam Leffler } 7845591b213SSam Leffler 7855591b213SSam Leffler static int 7865591b213SSam Leffler ath_media_change(struct ifnet *ifp) 7875591b213SSam Leffler { 7885591b213SSam Leffler int error; 7895591b213SSam Leffler 7905591b213SSam Leffler error = ieee80211_media_change(ifp); 7915591b213SSam Leffler if (error == ENETRESET) { 7925591b213SSam Leffler if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) == 7935591b213SSam Leffler (IFF_RUNNING|IFF_UP)) 7945591b213SSam Leffler ath_init(ifp); /* XXX lose error */ 7955591b213SSam Leffler error = 0; 7965591b213SSam Leffler } 7975591b213SSam Leffler return error; 7985591b213SSam Leffler } 7995591b213SSam Leffler 8005591b213SSam Leffler static void 8015591b213SSam Leffler ath_watchdog(struct ifnet *ifp) 8025591b213SSam Leffler { 8035591b213SSam Leffler struct ath_softc *sc = ifp->if_softc; 8045591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 8055591b213SSam Leffler 8065591b213SSam Leffler ifp->if_timer = 0; 8075591b213SSam Leffler if ((ifp->if_flags & IFF_RUNNING) == 0 || sc->sc_invalid) 8085591b213SSam Leffler return; 8095591b213SSam Leffler if (sc->sc_tx_timer) { 8105591b213SSam Leffler if (--sc->sc_tx_timer == 0) { 8115591b213SSam Leffler if_printf(ifp, "device timeout\n"); 8125591b213SSam Leffler #ifdef AR_DEBUG 8135591b213SSam Leffler if (ath_debug) 8145591b213SSam Leffler ath_hal_dumpstate(sc->sc_ah); 8155591b213SSam Leffler #endif /* AR_DEBUG */ 8165591b213SSam Leffler ath_init(ifp); /* XXX ath_reset??? */ 8175591b213SSam Leffler ifp->if_oerrors++; 8185591b213SSam Leffler sc->sc_stats.ast_watchdog++; 8195591b213SSam Leffler return; 8205591b213SSam Leffler } 8215591b213SSam Leffler ifp->if_timer = 1; 8225591b213SSam Leffler } 8235591b213SSam Leffler if (ic->ic_fixed_rate == -1) { 8245591b213SSam Leffler /* 8255591b213SSam Leffler * Run the rate control algorithm if we're not 8265591b213SSam Leffler * locked at a fixed rate. 8275591b213SSam Leffler */ 8285591b213SSam Leffler if (ic->ic_opmode == IEEE80211_M_STA) 8295591b213SSam Leffler ath_rate_ctl(sc, ic->ic_bss); 8305591b213SSam Leffler else 8315591b213SSam Leffler ieee80211_iterate_nodes(ic, ath_rate_ctl, sc); 8325591b213SSam Leffler } 8335591b213SSam Leffler ieee80211_watchdog(ifp); 8345591b213SSam Leffler } 8355591b213SSam Leffler 8365591b213SSam Leffler static int 8375591b213SSam Leffler ath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 8385591b213SSam Leffler { 8395591b213SSam Leffler struct ath_softc *sc = ifp->if_softc; 8405591b213SSam Leffler struct ifreq *ifr = (struct ifreq *)data; 8415591b213SSam Leffler int error = 0; 8425591b213SSam Leffler 8435591b213SSam Leffler mtx_lock(&sc->sc_mtx); 8445591b213SSam Leffler switch (cmd) { 8455591b213SSam Leffler case SIOCSIFFLAGS: 8465591b213SSam Leffler if (ifp->if_flags & IFF_UP) { 8475591b213SSam Leffler if (ifp->if_flags & IFF_RUNNING) { 8485591b213SSam Leffler /* 8495591b213SSam Leffler * To avoid rescanning another access point, 8505591b213SSam Leffler * do not call ath_init() here. Instead, 8515591b213SSam Leffler * only reflect promisc mode settings. 8525591b213SSam Leffler */ 8535591b213SSam Leffler ath_mode_init(sc); 8545591b213SSam Leffler } else 8555591b213SSam Leffler ath_init(ifp); /* XXX lose error */ 8565591b213SSam Leffler } else 8575591b213SSam Leffler ath_stop(ifp); 8585591b213SSam Leffler break; 8595591b213SSam Leffler case SIOCADDMULTI: 8605591b213SSam Leffler case SIOCDELMULTI: 8615591b213SSam Leffler /* 8625591b213SSam Leffler * The upper layer has already installed/removed 8635591b213SSam Leffler * the multicast address(es), just recalculate the 8645591b213SSam Leffler * multicast filter for the card. 8655591b213SSam Leffler */ 8665591b213SSam Leffler if (ifp->if_flags & IFF_RUNNING) 8675591b213SSam Leffler ath_mode_init(sc); 8685591b213SSam Leffler break; 8695591b213SSam Leffler case SIOCGATHSTATS: 8705591b213SSam Leffler copyout(&sc->sc_stats, ifr->ifr_data, sizeof (sc->sc_stats)); 8715591b213SSam Leffler break; 8725591b213SSam Leffler default: 8735591b213SSam Leffler error = ieee80211_ioctl(ifp, cmd, data); 8745591b213SSam Leffler if (error == ENETRESET) { 8755591b213SSam Leffler if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) == 8765591b213SSam Leffler (IFF_RUNNING|IFF_UP)) 8775591b213SSam Leffler ath_init(ifp); /* XXX lose error */ 8785591b213SSam Leffler error = 0; 8795591b213SSam Leffler } 8805591b213SSam Leffler break; 8815591b213SSam Leffler } 8825591b213SSam Leffler mtx_unlock(&sc->sc_mtx); 8835591b213SSam Leffler return error; 8845591b213SSam Leffler } 8855591b213SSam Leffler 8865591b213SSam Leffler /* 8875591b213SSam Leffler * Fill the hardware key cache with key entries. 8885591b213SSam Leffler */ 8895591b213SSam Leffler static void 8905591b213SSam Leffler ath_initkeytable(struct ath_softc *sc) 8915591b213SSam Leffler { 8925591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 8935591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 8945591b213SSam Leffler int i; 8955591b213SSam Leffler 8965591b213SSam Leffler for (i = 0; i < IEEE80211_WEP_NKID; i++) { 8975591b213SSam Leffler struct ieee80211_wepkey *k = &ic->ic_nw_keys[i]; 8985591b213SSam Leffler if (k->wk_len == 0) 8995591b213SSam Leffler ath_hal_keyreset(ah, i); 9005591b213SSam Leffler else 9015591b213SSam Leffler /* XXX return value */ 9025591b213SSam Leffler /* NB: this uses HAL_KEYVAL == ieee80211_wepkey */ 9035591b213SSam Leffler ath_hal_keyset(ah, i, (const HAL_KEYVAL *) k); 9045591b213SSam Leffler } 9055591b213SSam Leffler } 9065591b213SSam Leffler 9075591b213SSam Leffler static void 9085591b213SSam Leffler ath_mode_init(struct ath_softc *sc) 9095591b213SSam Leffler { 9105591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 9115591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 9125591b213SSam Leffler struct ifnet *ifp = &ic->ic_if; 9135591b213SSam Leffler u_int32_t rfilt, mfilt[2], val; 9145591b213SSam Leffler u_int8_t pos; 9155591b213SSam Leffler struct ifmultiaddr *ifma; 9165591b213SSam Leffler 9175591b213SSam Leffler /* configure operational mode */ 9185591b213SSam Leffler ath_hal_setopmode(ah, ic->ic_opmode); 9195591b213SSam Leffler 9205591b213SSam Leffler /* receive filter */ 9215591b213SSam Leffler rfilt = (ath_hal_getrxfilter(ah) & HAL_RX_FILTER_PHYERR) 9225591b213SSam Leffler | HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST; 923f07a6d98SSam Leffler if (ic->ic_opmode != IEEE80211_M_STA) 924f07a6d98SSam Leffler rfilt |= HAL_RX_FILTER_PROBEREQ; 9255591b213SSam Leffler if (ic->ic_opmode != IEEE80211_M_HOSTAP && 9265591b213SSam Leffler (ifp->if_flags & IFF_PROMISC)) 9275591b213SSam Leffler rfilt |= HAL_RX_FILTER_PROM; 928de5af704SSam Leffler if (ic->ic_opmode == IEEE80211_M_STA || 929de5af704SSam Leffler ic->ic_state == IEEE80211_S_SCAN) 9305591b213SSam Leffler rfilt |= HAL_RX_FILTER_BEACON; 9315591b213SSam Leffler ath_hal_setrxfilter(ah, rfilt); 9325591b213SSam Leffler 9335591b213SSam Leffler /* calculate and install multicast filter */ 9345591b213SSam Leffler if ((ifp->if_flags & IFF_ALLMULTI) == 0) { 9355591b213SSam Leffler mfilt[0] = mfilt[1] = 0; 9365591b213SSam Leffler TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 9375591b213SSam Leffler caddr_t dl; 9385591b213SSam Leffler 9395591b213SSam Leffler /* calculate XOR of eight 6bit values */ 9405591b213SSam Leffler dl = LLADDR((struct sockaddr_dl *) ifma->ifma_addr); 9415591b213SSam Leffler val = LE_READ_4(dl + 0); 9425591b213SSam Leffler pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val; 9435591b213SSam Leffler val = LE_READ_4(dl + 3); 9445591b213SSam Leffler pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val; 9455591b213SSam Leffler pos &= 0x3f; 9465591b213SSam Leffler mfilt[pos / 32] |= (1 << (pos % 32)); 9475591b213SSam Leffler } 9485591b213SSam Leffler } else { 9495591b213SSam Leffler mfilt[0] = mfilt[1] = ~0; 9505591b213SSam Leffler } 9515591b213SSam Leffler ath_hal_setmcastfilter(ah, mfilt[0], mfilt[1]); 9525591b213SSam Leffler DPRINTF(("ath_mode_init: RX filter 0x%x, MC filter %08x:%08x\n", 9535591b213SSam Leffler rfilt, mfilt[0], mfilt[1])); 9545591b213SSam Leffler } 9555591b213SSam Leffler 9565591b213SSam Leffler static void 9575591b213SSam Leffler ath_mbuf_load_cb(void *arg, bus_dma_segment_t *seg, int nseg, bus_size_t mapsize, int error) 9585591b213SSam Leffler { 9595591b213SSam Leffler struct ath_buf *bf = arg; 9605591b213SSam Leffler 9615591b213SSam Leffler KASSERT(nseg <= ATH_MAX_SCATTER, 9625591b213SSam Leffler ("ath_mbuf_load_cb: too many DMA segments %u", nseg)); 9635591b213SSam Leffler bf->bf_mapsize = mapsize; 9645591b213SSam Leffler bf->bf_nseg = nseg; 9655591b213SSam Leffler bcopy(seg, bf->bf_segs, nseg * sizeof (seg[0])); 9665591b213SSam Leffler } 9675591b213SSam Leffler 9685591b213SSam Leffler static int 9695591b213SSam Leffler ath_beacon_alloc(struct ath_softc *sc, struct ieee80211_node *ni) 9705591b213SSam Leffler { 9715591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 9725591b213SSam Leffler struct ifnet *ifp = &ic->ic_if; 9735591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 9745591b213SSam Leffler struct ieee80211_frame *wh; 9755591b213SSam Leffler struct ath_buf *bf; 9765591b213SSam Leffler struct ath_desc *ds; 9775591b213SSam Leffler struct mbuf *m; 9785591b213SSam Leffler int error, pktlen; 9795591b213SSam Leffler u_int8_t *frm, rate; 9805591b213SSam Leffler u_int16_t capinfo; 9815591b213SSam Leffler struct ieee80211_rateset *rs; 9825591b213SSam Leffler const HAL_RATE_TABLE *rt; 9835591b213SSam Leffler 9845591b213SSam Leffler bf = sc->sc_bcbuf; 9855591b213SSam Leffler if (bf->bf_m != NULL) { 9865591b213SSam Leffler bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 9875591b213SSam Leffler m_freem(bf->bf_m); 9885591b213SSam Leffler bf->bf_m = NULL; 9895591b213SSam Leffler bf->bf_node = NULL; 9905591b213SSam Leffler } 9915591b213SSam Leffler /* 9925591b213SSam Leffler * NB: the beacon data buffer must be 32-bit aligned; 9935591b213SSam Leffler * we assume the mbuf routines will return us something 9945591b213SSam Leffler * with this alignment (perhaps should assert). 9955591b213SSam Leffler */ 9965591b213SSam Leffler rs = &ni->ni_rates; 9979cccabebSSam Leffler pktlen = sizeof (struct ieee80211_frame) 998abb62a41SSam Leffler + 8 + 2 + 2 + 2+ni->ni_esslen + 2+rs->rs_nrates + 3 + 6; 9995591b213SSam Leffler if (rs->rs_nrates > IEEE80211_RATE_SIZE) 10005591b213SSam Leffler pktlen += 2; 10015591b213SSam Leffler if (pktlen <= MHLEN) 10025591b213SSam Leffler MGETHDR(m, M_DONTWAIT, MT_DATA); 10035591b213SSam Leffler else 10045591b213SSam Leffler m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 10055591b213SSam Leffler if (m == NULL) { 10065591b213SSam Leffler DPRINTF(("ath_beacon_alloc: cannot get mbuf/cluster; size %u\n", 10075591b213SSam Leffler pktlen)); 10085591b213SSam Leffler sc->sc_stats.ast_be_nombuf++; 10095591b213SSam Leffler return ENOMEM; 10105591b213SSam Leffler } 10115591b213SSam Leffler 10125591b213SSam Leffler wh = mtod(m, struct ieee80211_frame *); 10135591b213SSam Leffler wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | 10145591b213SSam Leffler IEEE80211_FC0_SUBTYPE_BEACON; 10155591b213SSam Leffler wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 10165591b213SSam Leffler *(u_int16_t *)wh->i_dur = 0; 10175591b213SSam Leffler memcpy(wh->i_addr1, ifp->if_broadcastaddr, IEEE80211_ADDR_LEN); 10185591b213SSam Leffler memcpy(wh->i_addr2, ic->ic_myaddr, IEEE80211_ADDR_LEN); 10195591b213SSam Leffler memcpy(wh->i_addr3, ni->ni_bssid, IEEE80211_ADDR_LEN); 10205591b213SSam Leffler *(u_int16_t *)wh->i_seq = 0; 10215591b213SSam Leffler 10225591b213SSam Leffler /* 10235591b213SSam Leffler * beacon frame format 10245591b213SSam Leffler * [8] time stamp 10255591b213SSam Leffler * [2] beacon interval 10265591b213SSam Leffler * [2] cabability information 10275591b213SSam Leffler * [tlv] ssid 10285591b213SSam Leffler * [tlv] supported rates 10295591b213SSam Leffler * [tlv] parameter set (IBSS) 10305591b213SSam Leffler * [tlv] extended supported rates 10315591b213SSam Leffler */ 10325591b213SSam Leffler frm = (u_int8_t *)&wh[1]; 10335591b213SSam Leffler memset(frm, 0, 8); /* timestamp is set by hardware */ 10345591b213SSam Leffler frm += 8; 10355591b213SSam Leffler *(u_int16_t *)frm = htole16(ni->ni_intval); 10365591b213SSam Leffler frm += 2; 10375591b213SSam Leffler if (ic->ic_opmode == IEEE80211_M_IBSS) 10385591b213SSam Leffler capinfo = IEEE80211_CAPINFO_IBSS; 10395591b213SSam Leffler else 10405591b213SSam Leffler capinfo = IEEE80211_CAPINFO_ESS; 10415591b213SSam Leffler if (ic->ic_flags & IEEE80211_F_WEPON) 10425591b213SSam Leffler capinfo |= IEEE80211_CAPINFO_PRIVACY; 10433065b96eSSam Leffler if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 10443065b96eSSam Leffler IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) 10455591b213SSam Leffler capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 10465591b213SSam Leffler if (ic->ic_flags & IEEE80211_F_SHSLOT) 10475591b213SSam Leffler capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 10485591b213SSam Leffler *(u_int16_t *)frm = htole16(capinfo); 10495591b213SSam Leffler frm += 2; 10505591b213SSam Leffler *frm++ = IEEE80211_ELEMID_SSID; 10515591b213SSam Leffler *frm++ = ni->ni_esslen; 10525591b213SSam Leffler memcpy(frm, ni->ni_essid, ni->ni_esslen); 10535591b213SSam Leffler frm += ni->ni_esslen; 10545591b213SSam Leffler frm = ieee80211_add_rates(frm, rs); 1055abb62a41SSam Leffler *frm++ = IEEE80211_ELEMID_DSPARMS; 1056abb62a41SSam Leffler *frm++ = 1; 1057abb62a41SSam Leffler *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan); 10585591b213SSam Leffler if (ic->ic_opmode == IEEE80211_M_IBSS) { 10595591b213SSam Leffler *frm++ = IEEE80211_ELEMID_IBSSPARMS; 10605591b213SSam Leffler *frm++ = 2; 10615591b213SSam Leffler *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */ 10625591b213SSam Leffler } else { 10635591b213SSam Leffler /* TODO: TIM */ 10645591b213SSam Leffler *frm++ = IEEE80211_ELEMID_TIM; 10655591b213SSam Leffler *frm++ = 4; /* length */ 10665591b213SSam Leffler *frm++ = 0; /* DTIM count */ 10675591b213SSam Leffler *frm++ = 1; /* DTIM period */ 10685591b213SSam Leffler *frm++ = 0; /* bitmap control */ 10695591b213SSam Leffler *frm++ = 0; /* Partial Virtual Bitmap (variable length) */ 10705591b213SSam Leffler } 10715591b213SSam Leffler frm = ieee80211_add_xrates(frm, rs); 10725591b213SSam Leffler m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *); 10739cccabebSSam Leffler KASSERT(m->m_pkthdr.len <= pktlen, 10749cccabebSSam Leffler ("beacon bigger than expected, len %u calculated %u", 10759cccabebSSam Leffler m->m_pkthdr.len, pktlen)); 10765591b213SSam Leffler 10775591b213SSam Leffler DPRINTF2(("ath_beacon_alloc: m %p len %u\n", m, m->m_len)); 10785591b213SSam Leffler error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m, 10795591b213SSam Leffler ath_mbuf_load_cb, bf, 10805591b213SSam Leffler BUS_DMA_NOWAIT); 10815591b213SSam Leffler if (error != 0) { 10825591b213SSam Leffler m_freem(m); 10835591b213SSam Leffler return error; 10845591b213SSam Leffler } 10855591b213SSam Leffler KASSERT(bf->bf_nseg == 1, 10865591b213SSam Leffler ("ath_beacon_alloc: multi-segment packet; nseg %u", 10875591b213SSam Leffler bf->bf_nseg)); 10885591b213SSam Leffler bf->bf_m = m; 10895591b213SSam Leffler 10905591b213SSam Leffler /* setup descriptors */ 10915591b213SSam Leffler ds = bf->bf_desc; 10925591b213SSam Leffler 10935591b213SSam Leffler ds->ds_link = 0; 10945591b213SSam Leffler ds->ds_data = bf->bf_segs[0].ds_addr; 10955591b213SSam Leffler /* 10965591b213SSam Leffler * Calculate rate code. 10975591b213SSam Leffler * XXX everything at min xmit rate 10985591b213SSam Leffler */ 10995591b213SSam Leffler rt = sc->sc_currates; 11005591b213SSam Leffler KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode)); 11016b59f5e3SSam Leffler if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 11025591b213SSam Leffler rate = rt->info[0].rateCode | rt->info[0].shortPreamble; 11035591b213SSam Leffler else 11045591b213SSam Leffler rate = rt->info[0].rateCode; 11055591b213SSam Leffler ath_hal_setuptxdesc(ah, ds 11065591b213SSam Leffler , m->m_pkthdr.len + IEEE80211_CRC_LEN /* packet length */ 11075591b213SSam Leffler , sizeof(struct ieee80211_frame) /* header length */ 11085591b213SSam Leffler , HAL_PKT_TYPE_BEACON /* Atheros packet type */ 11095591b213SSam Leffler , 0x20 /* txpower XXX */ 11105591b213SSam Leffler , rate, 1 /* series 0 rate/tries */ 11115591b213SSam Leffler , HAL_TXKEYIX_INVALID /* no encryption */ 11125591b213SSam Leffler , 0 /* antenna mode */ 11135591b213SSam Leffler , HAL_TXDESC_NOACK /* no ack for beacons */ 11145591b213SSam Leffler , 0 /* rts/cts rate */ 11155591b213SSam Leffler , 0 /* rts/cts duration */ 11165591b213SSam Leffler ); 11175591b213SSam Leffler /* NB: beacon's BufLen must be a multiple of 4 bytes */ 11189cccabebSSam Leffler /* XXX verify mbuf data area covers this roundup */ 11195591b213SSam Leffler ath_hal_filltxdesc(ah, ds 11205591b213SSam Leffler , roundup(bf->bf_segs[0].ds_len, 4) /* buffer length */ 11215591b213SSam Leffler , AH_TRUE /* first segment */ 11225591b213SSam Leffler , AH_TRUE /* last segment */ 11235591b213SSam Leffler ); 11245591b213SSam Leffler 11255591b213SSam Leffler return 0; 11265591b213SSam Leffler } 11275591b213SSam Leffler 11285591b213SSam Leffler static void 11295591b213SSam Leffler ath_beacon_proc(void *arg, int pending) 11305591b213SSam Leffler { 11315591b213SSam Leffler struct ath_softc *sc = arg; 11325591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 11335591b213SSam Leffler struct ath_buf *bf = sc->sc_bcbuf; 11345591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 11355591b213SSam Leffler 11365591b213SSam Leffler DPRINTF2(("%s: pending %u\n", __func__, pending)); 11370a915fadSSam Leffler if (ic->ic_opmode == IEEE80211_M_STA || 11380a915fadSSam Leffler bf == NULL || bf->bf_m == NULL) { 11395591b213SSam Leffler DPRINTF(("%s: ic_flags=%x bf=%p bf_m=%p\n", 11405591b213SSam Leffler __func__, ic->ic_flags, bf, bf ? bf->bf_m : NULL)); 11415591b213SSam Leffler return; 11425591b213SSam Leffler } 11430a915fadSSam Leffler /* TODO: update beacon to reflect PS poll state */ 11445591b213SSam Leffler if (!ath_hal_stoptxdma(ah, sc->sc_bhalq)) { 11455591b213SSam Leffler DPRINTF(("%s: beacon queue %u did not stop?", 11465591b213SSam Leffler __func__, sc->sc_bhalq)); 11475591b213SSam Leffler return; /* busy, XXX is this right? */ 11485591b213SSam Leffler } 11495591b213SSam Leffler bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE); 11505591b213SSam Leffler 11515591b213SSam Leffler ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr); 11525591b213SSam Leffler ath_hal_txstart(ah, sc->sc_bhalq); 11535591b213SSam Leffler DPRINTF2(("%s: TXDP%u = %p (%p)\n", __func__, 11545591b213SSam Leffler sc->sc_bhalq, (caddr_t)bf->bf_daddr, bf->bf_desc)); 11555591b213SSam Leffler } 11565591b213SSam Leffler 11575591b213SSam Leffler static void 11585591b213SSam Leffler ath_beacon_free(struct ath_softc *sc) 11595591b213SSam Leffler { 11605591b213SSam Leffler struct ath_buf *bf = sc->sc_bcbuf; 11615591b213SSam Leffler 11625591b213SSam Leffler if (bf->bf_m != NULL) { 11635591b213SSam Leffler bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 11645591b213SSam Leffler m_freem(bf->bf_m); 11655591b213SSam Leffler bf->bf_m = NULL; 11665591b213SSam Leffler bf->bf_node = NULL; 11675591b213SSam Leffler } 11685591b213SSam Leffler } 11695591b213SSam Leffler 11705591b213SSam Leffler /* 11715591b213SSam Leffler * Configure the beacon and sleep timers. 11725591b213SSam Leffler * 11735591b213SSam Leffler * When operating as an AP this resets the TSF and sets 11745591b213SSam Leffler * up the hardware to notify us when we need to issue beacons. 11755591b213SSam Leffler * 11765591b213SSam Leffler * When operating in station mode this sets up the beacon 11775591b213SSam Leffler * timers according to the timestamp of the last received 11785591b213SSam Leffler * beacon and the current TSF, configures PCF and DTIM 11795591b213SSam Leffler * handling, programs the sleep registers so the hardware 11805591b213SSam Leffler * will wakeup in time to receive beacons, and configures 11815591b213SSam Leffler * the beacon miss handling so we'll receive a BMISS 11825591b213SSam Leffler * interrupt when we stop seeing beacons from the AP 11835591b213SSam Leffler * we've associated with. 11845591b213SSam Leffler */ 11855591b213SSam Leffler static void 11865591b213SSam Leffler ath_beacon_config(struct ath_softc *sc) 11875591b213SSam Leffler { 11885591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 11895591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 11905591b213SSam Leffler struct ieee80211_node *ni = ic->ic_bss; 11915591b213SSam Leffler u_int32_t nexttbtt; 11925591b213SSam Leffler 11935591b213SSam Leffler nexttbtt = (LE_READ_4(ni->ni_tstamp + 4) << 22) | 11945591b213SSam Leffler (LE_READ_4(ni->ni_tstamp) >> 10); 11955591b213SSam Leffler DPRINTF(("%s: nexttbtt=%u\n", __func__, nexttbtt)); 11965591b213SSam Leffler nexttbtt += ni->ni_intval; 11976b59f5e3SSam Leffler if (ic->ic_opmode == IEEE80211_M_STA) { 11985591b213SSam Leffler HAL_BEACON_STATE bs; 11995591b213SSam Leffler u_int32_t bmisstime; 12005591b213SSam Leffler 12015591b213SSam Leffler /* NB: no PCF support right now */ 12025591b213SSam Leffler memset(&bs, 0, sizeof(bs)); 12035591b213SSam Leffler bs.bs_intval = ni->ni_intval; 12045591b213SSam Leffler bs.bs_nexttbtt = nexttbtt; 12055591b213SSam Leffler bs.bs_dtimperiod = bs.bs_intval; 12065591b213SSam Leffler bs.bs_nextdtim = nexttbtt; 12075591b213SSam Leffler /* 12085591b213SSam Leffler * Calculate the number of consecutive beacons to miss 12095591b213SSam Leffler * before taking a BMISS interrupt. The configuration 12105591b213SSam Leffler * is specified in ms, so we need to convert that to 12115591b213SSam Leffler * TU's and then calculate based on the beacon interval. 12125591b213SSam Leffler * Note that we clamp the result to at most 10 beacons. 12135591b213SSam Leffler */ 12145591b213SSam Leffler bmisstime = (ic->ic_bmisstimeout * 1000) / 1024; 12155591b213SSam Leffler bs.bs_bmissthreshold = howmany(bmisstime,ni->ni_intval); 12165591b213SSam Leffler if (bs.bs_bmissthreshold > 10) 12175591b213SSam Leffler bs.bs_bmissthreshold = 10; 12185591b213SSam Leffler else if (bs.bs_bmissthreshold <= 0) 12195591b213SSam Leffler bs.bs_bmissthreshold = 1; 12205591b213SSam Leffler 12215591b213SSam Leffler /* 12225591b213SSam Leffler * Calculate sleep duration. The configuration is 12235591b213SSam Leffler * given in ms. We insure a multiple of the beacon 12245591b213SSam Leffler * period is used. Also, if the sleep duration is 12255591b213SSam Leffler * greater than the DTIM period then it makes senses 12265591b213SSam Leffler * to make it a multiple of that. 12275591b213SSam Leffler * 12285591b213SSam Leffler * XXX fixed at 100ms 12295591b213SSam Leffler */ 12305591b213SSam Leffler bs.bs_sleepduration = 12315591b213SSam Leffler roundup((100 * 1000) / 1024, bs.bs_intval); 12325591b213SSam Leffler if (bs.bs_sleepduration > bs.bs_dtimperiod) 12335591b213SSam Leffler bs.bs_sleepduration = roundup(bs.bs_sleepduration, bs.bs_dtimperiod); 12345591b213SSam Leffler 12355591b213SSam Leffler DPRINTF(("%s: intval %u nexttbtt %u dtim %u nextdtim %u bmiss %u sleep %u\n" 12365591b213SSam Leffler , __func__ 12375591b213SSam Leffler , bs.bs_intval 12385591b213SSam Leffler , bs.bs_nexttbtt 12395591b213SSam Leffler , bs.bs_dtimperiod 12405591b213SSam Leffler , bs.bs_nextdtim 12415591b213SSam Leffler , bs.bs_bmissthreshold 12425591b213SSam Leffler , bs.bs_sleepduration 12435591b213SSam Leffler )); 12445591b213SSam Leffler ath_hal_intrset(ah, 0); 12455591b213SSam Leffler /* 12465591b213SSam Leffler * Reset our tsf so the hardware will update the 12475591b213SSam Leffler * tsf register to reflect timestamps found in 12485591b213SSam Leffler * received beacons. 12495591b213SSam Leffler */ 12505591b213SSam Leffler ath_hal_resettsf(ah); 12515591b213SSam Leffler ath_hal_beacontimers(ah, &bs, 0/*XXX*/, 0, 0); 12525591b213SSam Leffler sc->sc_imask |= HAL_INT_BMISS; 12535591b213SSam Leffler ath_hal_intrset(ah, sc->sc_imask); 12545591b213SSam Leffler } else { 12555591b213SSam Leffler DPRINTF(("%s: intval %u nexttbtt %u\n", 12565591b213SSam Leffler __func__, ni->ni_intval, nexttbtt)); 12575591b213SSam Leffler ath_hal_intrset(ah, 0); 12585591b213SSam Leffler ath_hal_beaconinit(ah, ic->ic_opmode, 12595591b213SSam Leffler nexttbtt, ni->ni_intval); 12606b59f5e3SSam Leffler if (ic->ic_opmode != IEEE80211_M_MONITOR) 12615591b213SSam Leffler sc->sc_imask |= HAL_INT_SWBA; /* beacon prepare */ 12625591b213SSam Leffler ath_hal_intrset(ah, sc->sc_imask); 12635591b213SSam Leffler } 12645591b213SSam Leffler } 12655591b213SSam Leffler 12665591b213SSam Leffler static void 12675591b213SSam Leffler ath_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 12685591b213SSam Leffler { 12695591b213SSam Leffler bus_addr_t *paddr = (bus_addr_t*) arg; 12705591b213SSam Leffler *paddr = segs->ds_addr; 12715591b213SSam Leffler } 12725591b213SSam Leffler 12735591b213SSam Leffler static int 12745591b213SSam Leffler ath_desc_alloc(struct ath_softc *sc) 12755591b213SSam Leffler { 12765591b213SSam Leffler int i, bsize, error; 12775591b213SSam Leffler struct ath_desc *ds; 12785591b213SSam Leffler struct ath_buf *bf; 12795591b213SSam Leffler 12805591b213SSam Leffler /* allocate descriptors */ 12815591b213SSam Leffler sc->sc_desc_len = sizeof(struct ath_desc) * 12825591b213SSam Leffler (ATH_TXBUF * ATH_TXDESC + ATH_RXBUF + 1); 12835591b213SSam Leffler error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT, &sc->sc_ddmamap); 12845591b213SSam Leffler if (error != 0) 12855591b213SSam Leffler return error; 12865591b213SSam Leffler 12875591b213SSam Leffler error = bus_dmamem_alloc(sc->sc_dmat, (void**) &sc->sc_desc, 12885591b213SSam Leffler BUS_DMA_NOWAIT, &sc->sc_ddmamap); 12895591b213SSam Leffler if (error != 0) 12905591b213SSam Leffler goto fail0; 12915591b213SSam Leffler 12925591b213SSam Leffler error = bus_dmamap_load(sc->sc_dmat, sc->sc_ddmamap, 12935591b213SSam Leffler sc->sc_desc, sc->sc_desc_len, 12945591b213SSam Leffler ath_load_cb, &sc->sc_desc_paddr, 12955591b213SSam Leffler BUS_DMA_NOWAIT); 12965591b213SSam Leffler if (error != 0) 12975591b213SSam Leffler goto fail1; 12985591b213SSam Leffler 12995591b213SSam Leffler ds = sc->sc_desc; 13005591b213SSam Leffler DPRINTF(("ath_desc_alloc: DMA map: %p (%d) -> %p (%lu)\n", 13015591b213SSam Leffler ds, sc->sc_desc_len, 13025591b213SSam Leffler (caddr_t) sc->sc_desc_paddr, /*XXX*/ (u_long) sc->sc_desc_len)); 13035591b213SSam Leffler 13045591b213SSam Leffler /* allocate buffers */ 13055591b213SSam Leffler bsize = sizeof(struct ath_buf) * (ATH_TXBUF + ATH_RXBUF + 1); 13065591b213SSam Leffler bf = malloc(bsize, M_DEVBUF, M_NOWAIT | M_ZERO); 13075591b213SSam Leffler if (bf == NULL) 13085591b213SSam Leffler goto fail2; 13095591b213SSam Leffler sc->sc_bufptr = bf; 13105591b213SSam Leffler 13115591b213SSam Leffler TAILQ_INIT(&sc->sc_rxbuf); 13125591b213SSam Leffler for (i = 0; i < ATH_RXBUF; i++, bf++, ds++) { 13135591b213SSam Leffler bf->bf_desc = ds; 13145591b213SSam Leffler bf->bf_daddr = sc->sc_desc_paddr + 13155591b213SSam Leffler ((caddr_t)ds - (caddr_t)sc->sc_desc); 13165591b213SSam Leffler error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT, 13175591b213SSam Leffler &bf->bf_dmamap); 13185591b213SSam Leffler if (error != 0) 13195591b213SSam Leffler break; 13205591b213SSam Leffler TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list); 13215591b213SSam Leffler } 13225591b213SSam Leffler 13235591b213SSam Leffler TAILQ_INIT(&sc->sc_txbuf); 13245591b213SSam Leffler for (i = 0; i < ATH_TXBUF; i++, bf++, ds += ATH_TXDESC) { 13255591b213SSam Leffler bf->bf_desc = ds; 13265591b213SSam Leffler bf->bf_daddr = sc->sc_desc_paddr + 13275591b213SSam Leffler ((caddr_t)ds - (caddr_t)sc->sc_desc); 13285591b213SSam Leffler error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT, 13295591b213SSam Leffler &bf->bf_dmamap); 13305591b213SSam Leffler if (error != 0) 13315591b213SSam Leffler break; 13325591b213SSam Leffler TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list); 13335591b213SSam Leffler } 13345591b213SSam Leffler TAILQ_INIT(&sc->sc_txq); 13355591b213SSam Leffler 13365591b213SSam Leffler /* beacon buffer */ 13375591b213SSam Leffler bf->bf_desc = ds; 13385591b213SSam Leffler bf->bf_daddr = sc->sc_desc_paddr + ((caddr_t)ds - (caddr_t)sc->sc_desc); 13395591b213SSam Leffler error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT, &bf->bf_dmamap); 13405591b213SSam Leffler if (error != 0) 13415591b213SSam Leffler return error; 13425591b213SSam Leffler sc->sc_bcbuf = bf; 13435591b213SSam Leffler return 0; 13445591b213SSam Leffler 13455591b213SSam Leffler fail2: 13465591b213SSam Leffler bus_dmamap_unload(sc->sc_dmat, sc->sc_ddmamap); 13475591b213SSam Leffler fail1: 13485591b213SSam Leffler bus_dmamem_free(sc->sc_dmat, sc->sc_desc, sc->sc_ddmamap); 13495591b213SSam Leffler fail0: 13505591b213SSam Leffler bus_dmamap_destroy(sc->sc_dmat, sc->sc_ddmamap); 13515591b213SSam Leffler sc->sc_ddmamap = NULL; 13525591b213SSam Leffler return error; 13535591b213SSam Leffler } 13545591b213SSam Leffler 13555591b213SSam Leffler static void 13565591b213SSam Leffler ath_desc_free(struct ath_softc *sc) 13575591b213SSam Leffler { 13585591b213SSam Leffler struct ath_buf *bf; 13595591b213SSam Leffler 13605591b213SSam Leffler bus_dmamap_unload(sc->sc_dmat, sc->sc_ddmamap); 13615591b213SSam Leffler bus_dmamem_free(sc->sc_dmat, sc->sc_desc, sc->sc_ddmamap); 13625591b213SSam Leffler bus_dmamap_destroy(sc->sc_dmat, sc->sc_ddmamap); 13635591b213SSam Leffler 13645591b213SSam Leffler TAILQ_FOREACH(bf, &sc->sc_txq, bf_list) { 13655591b213SSam Leffler bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 13665591b213SSam Leffler bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap); 13675591b213SSam Leffler m_freem(bf->bf_m); 13685591b213SSam Leffler } 13695591b213SSam Leffler TAILQ_FOREACH(bf, &sc->sc_txbuf, bf_list) 13705591b213SSam Leffler bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap); 13715591b213SSam Leffler TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) { 13725591b213SSam Leffler if (bf->bf_m) { 13735591b213SSam Leffler bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 13745591b213SSam Leffler bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap); 13755591b213SSam Leffler m_freem(bf->bf_m); 13765591b213SSam Leffler bf->bf_m = NULL; 13775591b213SSam Leffler } 13785591b213SSam Leffler } 13795591b213SSam Leffler if (sc->sc_bcbuf != NULL) { 13805591b213SSam Leffler bus_dmamap_unload(sc->sc_dmat, sc->sc_bcbuf->bf_dmamap); 13815591b213SSam Leffler bus_dmamap_destroy(sc->sc_dmat, sc->sc_bcbuf->bf_dmamap); 13825591b213SSam Leffler sc->sc_bcbuf = NULL; 13835591b213SSam Leffler } 13845591b213SSam Leffler 13855591b213SSam Leffler TAILQ_INIT(&sc->sc_rxbuf); 13865591b213SSam Leffler TAILQ_INIT(&sc->sc_txbuf); 13875591b213SSam Leffler TAILQ_INIT(&sc->sc_txq); 13885591b213SSam Leffler free(sc->sc_bufptr, M_DEVBUF); 13895591b213SSam Leffler sc->sc_bufptr = NULL; 13905591b213SSam Leffler } 13915591b213SSam Leffler 13925591b213SSam Leffler static struct ieee80211_node * 13935591b213SSam Leffler ath_node_alloc(struct ieee80211com *ic) 13945591b213SSam Leffler { 13955591b213SSam Leffler struct ath_node *an = 13965591b213SSam Leffler malloc(sizeof(struct ath_node), M_DEVBUF, M_NOWAIT | M_ZERO); 1397de5af704SSam Leffler if (an) { 1398de5af704SSam Leffler int i; 1399de5af704SSam Leffler for (i = 0; i < ATH_RHIST_SIZE; i++) 1400de5af704SSam Leffler an->an_rx_hist[i].arh_ticks = ATH_RHIST_NOTIME; 1401de5af704SSam Leffler an->an_rx_hist_next = ATH_RHIST_SIZE-1; 1402de5af704SSam Leffler return &an->an_node; 1403de5af704SSam Leffler } else 1404de5af704SSam Leffler return NULL; 14055591b213SSam Leffler } 14065591b213SSam Leffler 14075591b213SSam Leffler static void 14085591b213SSam Leffler ath_node_free(struct ieee80211com *ic, struct ieee80211_node *ni) 14095591b213SSam Leffler { 14105591b213SSam Leffler struct ath_softc *sc = ic->ic_if.if_softc; 14115591b213SSam Leffler struct ath_buf *bf; 14125591b213SSam Leffler 14135591b213SSam Leffler TAILQ_FOREACH(bf, &sc->sc_txq, bf_list) { 14145591b213SSam Leffler if (bf->bf_node == ni) 14155591b213SSam Leffler bf->bf_node = NULL; 14165591b213SSam Leffler } 14175591b213SSam Leffler free(ni, M_DEVBUF); 14185591b213SSam Leffler } 14195591b213SSam Leffler 14205591b213SSam Leffler static void 14215591b213SSam Leffler ath_node_copy(struct ieee80211com *ic, 14225591b213SSam Leffler struct ieee80211_node *dst, const struct ieee80211_node *src) 14235591b213SSam Leffler { 14245591b213SSam Leffler *(struct ath_node *)dst = *(const struct ath_node *)src; 14255591b213SSam Leffler } 14265591b213SSam Leffler 1427de5af704SSam Leffler 1428de5af704SSam Leffler static u_int8_t 1429de5af704SSam Leffler ath_node_getrssi(struct ieee80211com *ic, struct ieee80211_node *ni) 1430de5af704SSam Leffler { 1431de5af704SSam Leffler struct ath_node *an = ATH_NODE(ni); 1432de5af704SSam Leffler int i, now, nsamples, rssi; 1433de5af704SSam Leffler 1434de5af704SSam Leffler /* 1435de5af704SSam Leffler * Calculate the average over the last second of sampled data. 1436de5af704SSam Leffler */ 1437de5af704SSam Leffler now = ticks; 1438de5af704SSam Leffler nsamples = 0; 1439de5af704SSam Leffler rssi = 0; 1440de5af704SSam Leffler i = an->an_rx_hist_next; 1441de5af704SSam Leffler do { 1442de5af704SSam Leffler struct ath_recv_hist *rh = &an->an_rx_hist[i]; 1443de5af704SSam Leffler if (rh->arh_ticks == ATH_RHIST_NOTIME) 1444de5af704SSam Leffler goto done; 1445de5af704SSam Leffler if (now - rh->arh_ticks > hz) 1446de5af704SSam Leffler goto done; 1447de5af704SSam Leffler rssi += rh->arh_rssi; 1448de5af704SSam Leffler nsamples++; 1449de5af704SSam Leffler if (i == 0) 1450de5af704SSam Leffler i = ATH_RHIST_SIZE-1; 1451de5af704SSam Leffler else 1452de5af704SSam Leffler i--; 1453de5af704SSam Leffler } while (i != an->an_rx_hist_next); 1454de5af704SSam Leffler done: 1455de5af704SSam Leffler /* 1456de5af704SSam Leffler * Return either the average or the last known 1457de5af704SSam Leffler * value if there is no recent data. 1458de5af704SSam Leffler */ 1459de5af704SSam Leffler return (nsamples ? rssi / nsamples : an->an_rx_hist[i].arh_rssi); 1460de5af704SSam Leffler } 1461de5af704SSam Leffler 14625591b213SSam Leffler static int 14635591b213SSam Leffler ath_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf) 14645591b213SSam Leffler { 14655591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 14665591b213SSam Leffler int error; 14675591b213SSam Leffler struct mbuf *m; 14685591b213SSam Leffler struct ath_desc *ds; 14695591b213SSam Leffler 14705591b213SSam Leffler m = bf->bf_m; 14715591b213SSam Leffler if (m == NULL) { 14725591b213SSam Leffler /* 14735591b213SSam Leffler * NB: by assigning a page to the rx dma buffer we 14745591b213SSam Leffler * implicitly satisfy the Atheros requirement that 14755591b213SSam Leffler * this buffer be cache-line-aligned and sized to be 14765591b213SSam Leffler * multiple of the cache line size. Not doing this 14775591b213SSam Leffler * causes weird stuff to happen (for the 5210 at least). 14785591b213SSam Leffler */ 14795591b213SSam Leffler m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 14805591b213SSam Leffler if (m == NULL) { 14815591b213SSam Leffler DPRINTF(("ath_rxbuf_init: no mbuf/cluster\n")); 14825591b213SSam Leffler sc->sc_stats.ast_rx_nombuf++; 14835591b213SSam Leffler return ENOMEM; 14845591b213SSam Leffler } 14855591b213SSam Leffler bf->bf_m = m; 14865591b213SSam Leffler m->m_pkthdr.len = m->m_len = m->m_ext.ext_size; 14875591b213SSam Leffler 14885591b213SSam Leffler error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m, 14895591b213SSam Leffler ath_mbuf_load_cb, bf, 14905591b213SSam Leffler BUS_DMA_NOWAIT); 14915591b213SSam Leffler if (error != 0) { 14925591b213SSam Leffler DPRINTF(("ath_rxbuf_init: bus_dmamap_load_mbuf failed;" 14935591b213SSam Leffler " error %d\n", error)); 14945591b213SSam Leffler sc->sc_stats.ast_rx_busdma++; 14955591b213SSam Leffler return error; 14965591b213SSam Leffler } 14975591b213SSam Leffler KASSERT(bf->bf_nseg == 1, 14985591b213SSam Leffler ("ath_rxbuf_init: multi-segment packet; nseg %u", 14995591b213SSam Leffler bf->bf_nseg)); 15005591b213SSam Leffler } 15015591b213SSam Leffler bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREREAD); 15025591b213SSam Leffler 15035591b213SSam Leffler /* setup descriptors */ 15045591b213SSam Leffler ds = bf->bf_desc; 15055591b213SSam Leffler ds->ds_link = 0; 15065591b213SSam Leffler ds->ds_data = bf->bf_segs[0].ds_addr; 15075591b213SSam Leffler ath_hal_setuprxdesc(ah, ds 15085591b213SSam Leffler , m->m_len /* buffer size */ 15095591b213SSam Leffler , 0 15105591b213SSam Leffler ); 15115591b213SSam Leffler 15125591b213SSam Leffler if (sc->sc_rxlink != NULL) 15135591b213SSam Leffler *sc->sc_rxlink = bf->bf_daddr; 15145591b213SSam Leffler sc->sc_rxlink = &ds->ds_link; 15155591b213SSam Leffler return 0; 15165591b213SSam Leffler } 15175591b213SSam Leffler 15185591b213SSam Leffler static void 15195591b213SSam Leffler ath_rx_proc(void *arg, int npending) 15205591b213SSam Leffler { 15215591b213SSam Leffler struct ath_softc *sc = arg; 15225591b213SSam Leffler struct ath_buf *bf; 1523d1d0cf62SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 1524d1d0cf62SSam Leffler struct ifnet *ifp = &ic->ic_if; 15255591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 15265591b213SSam Leffler struct ath_desc *ds; 15275591b213SSam Leffler struct mbuf *m; 15285591b213SSam Leffler struct ieee80211_frame *wh, whbuf; 15290a915fadSSam Leffler struct ieee80211_node *ni; 1530de5af704SSam Leffler struct ath_node *an; 1531de5af704SSam Leffler struct ath_recv_hist *rh; 15325591b213SSam Leffler int len; 15335591b213SSam Leffler u_int phyerr; 15345591b213SSam Leffler HAL_STATUS status; 15355591b213SSam Leffler 15365591b213SSam Leffler DPRINTF2(("ath_rx_proc: pending %u\n", npending)); 15375591b213SSam Leffler do { 15385591b213SSam Leffler bf = TAILQ_FIRST(&sc->sc_rxbuf); 15395591b213SSam Leffler if (bf == NULL) { /* NB: shouldn't happen */ 15405591b213SSam Leffler if_printf(ifp, "ath_rx_proc: no buffer!\n"); 15415591b213SSam Leffler break; 15425591b213SSam Leffler } 15435591b213SSam Leffler m = bf->bf_m; 15445591b213SSam Leffler if (m == NULL) { /* NB: shouldn't happen */ 15455591b213SSam Leffler if_printf(ifp, "ath_rx_proc: no mbuf!\n"); 15465591b213SSam Leffler continue; 15475591b213SSam Leffler } 15485591b213SSam Leffler ds = bf->bf_desc; 15495591b213SSam Leffler status = ath_hal_rxprocdesc(ah, ds); 15505591b213SSam Leffler #ifdef AR_DEBUG 15515591b213SSam Leffler if (ath_debug > 1) 15525591b213SSam Leffler ath_printrxbuf(bf, status == HAL_OK); 15535591b213SSam Leffler #endif 15545591b213SSam Leffler if (status == HAL_EINPROGRESS) 15555591b213SSam Leffler break; 15565591b213SSam Leffler TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list); 15575591b213SSam Leffler if (ds->ds_rxstat.rs_status != 0) { 15585591b213SSam Leffler ifp->if_ierrors++; 15595591b213SSam Leffler if (ds->ds_rxstat.rs_status & HAL_RXERR_CRC) 15605591b213SSam Leffler sc->sc_stats.ast_rx_crcerr++; 15615591b213SSam Leffler if (ds->ds_rxstat.rs_status & HAL_RXERR_FIFO) 15625591b213SSam Leffler sc->sc_stats.ast_rx_fifoerr++; 15635591b213SSam Leffler if (ds->ds_rxstat.rs_status & HAL_RXERR_DECRYPT) 15645591b213SSam Leffler sc->sc_stats.ast_rx_badcrypt++; 15655591b213SSam Leffler if (ds->ds_rxstat.rs_status & HAL_RXERR_PHY) { 15665591b213SSam Leffler sc->sc_stats.ast_rx_phyerr++; 15675591b213SSam Leffler phyerr = ds->ds_rxstat.rs_phyerr & 0x1f; 15685591b213SSam Leffler sc->sc_stats.ast_rx_phy[phyerr]++; 15695591b213SSam Leffler } 15705591b213SSam Leffler goto rx_next; 15715591b213SSam Leffler } 15725591b213SSam Leffler 15735591b213SSam Leffler len = ds->ds_rxstat.rs_datalen; 1574ecddff40SSam Leffler if (len < IEEE80211_MIN_LEN) { 15755591b213SSam Leffler DPRINTF(("ath_rx_proc: short packet %d\n", len)); 15760a915fadSSam Leffler sc->sc_stats.ast_rx_tooshort++; 15775591b213SSam Leffler goto rx_next; 15785591b213SSam Leffler } 15795591b213SSam Leffler 15805591b213SSam Leffler bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, 15815591b213SSam Leffler BUS_DMASYNC_POSTREAD); 15825591b213SSam Leffler 15835591b213SSam Leffler bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 15845591b213SSam Leffler bf->bf_m = NULL; 15855591b213SSam Leffler m->m_pkthdr.rcvif = ifp; 15865591b213SSam Leffler m->m_pkthdr.len = m->m_len = len; 158773454c73SSam Leffler 158873454c73SSam Leffler if (sc->sc_drvbpf) { 158973454c73SSam Leffler struct mbuf *mb; 159073454c73SSam Leffler 159173454c73SSam Leffler /* XXX pre-allocate space when setting up recv's */ 159273454c73SSam Leffler MGETHDR(mb, M_DONTWAIT, m->m_type); 159373454c73SSam Leffler if (mb != NULL) { 159473454c73SSam Leffler sc->sc_rx_th.wr_rate = 159573454c73SSam Leffler sc->sc_hwmap[ds->ds_rxstat.rs_rate]; 159673454c73SSam Leffler sc->sc_rx_th.wr_antsignal = 159773454c73SSam Leffler ds->ds_rxstat.rs_rssi; 159873454c73SSam Leffler sc->sc_rx_th.wr_antenna = 159973454c73SSam Leffler ds->ds_rxstat.rs_antenna; 160073454c73SSam Leffler /* XXX TSF */ 160173454c73SSam Leffler 160273454c73SSam Leffler (void) m_dup_pkthdr(mb, m, M_DONTWAIT); 160373454c73SSam Leffler mb->m_next = m; 160473454c73SSam Leffler mb->m_data = (caddr_t)&sc->sc_rx_th; 160573454c73SSam Leffler mb->m_len = sizeof(sc->sc_rx_th); 160673454c73SSam Leffler mb->m_pkthdr.len += mb->m_len; 160773454c73SSam Leffler bpf_mtap(sc->sc_drvbpf, mb); 160873454c73SSam Leffler m_free(mb); 160973454c73SSam Leffler } 16105591b213SSam Leffler } 16110a915fadSSam Leffler 16125591b213SSam Leffler m_adj(m, -IEEE80211_CRC_LEN); 1613ecddff40SSam Leffler wh = mtod(m, struct ieee80211_frame *); 16145591b213SSam Leffler if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 16155591b213SSam Leffler /* 16165591b213SSam Leffler * WEP is decrypted by hardware. Clear WEP bit 16175591b213SSam Leffler * and trim WEP header for ieee80211_input(). 16185591b213SSam Leffler */ 16195591b213SSam Leffler wh->i_fc[1] &= ~IEEE80211_FC1_WEP; 16205591b213SSam Leffler memcpy(&whbuf, wh, sizeof(whbuf)); 16215591b213SSam Leffler m_adj(m, IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN); 16225591b213SSam Leffler memcpy(mtod(m, caddr_t), &whbuf, sizeof(whbuf)); 16235591b213SSam Leffler /* 16245591b213SSam Leffler * Also trim WEP ICV from the tail. 16255591b213SSam Leffler */ 16265591b213SSam Leffler m_adj(m, -IEEE80211_WEP_CRCLEN); 16275591b213SSam Leffler } 16280a915fadSSam Leffler 16290a915fadSSam Leffler /* 16300a915fadSSam Leffler * Locate the node for sender, track state, and 16310a915fadSSam Leffler * then pass this node (referenced) up to the 802.11 16320a915fadSSam Leffler * layer for its use. We are required to pass 16330a915fadSSam Leffler * something so we fall back to ic_bss when this frame 16340a915fadSSam Leffler * is from an unknown sender. 16350a915fadSSam Leffler */ 16360a915fadSSam Leffler if (ic->ic_opmode != IEEE80211_M_STA) { 16370a915fadSSam Leffler ni = ieee80211_find_node(ic, wh->i_addr2); 16380a915fadSSam Leffler if (ni == NULL) 16390a915fadSSam Leffler ni = ieee80211_ref_node(ic->ic_bss); 16400a915fadSSam Leffler } else 16410a915fadSSam Leffler ni = ieee80211_ref_node(ic->ic_bss); 1642de5af704SSam Leffler 1643de5af704SSam Leffler /* 1644de5af704SSam Leffler * Record driver-specific state. 1645de5af704SSam Leffler */ 1646de5af704SSam Leffler an = ATH_NODE(ni); 1647de5af704SSam Leffler if (++(an->an_rx_hist_next) == ATH_RHIST_SIZE) 1648de5af704SSam Leffler an->an_rx_hist_next = 0; 1649de5af704SSam Leffler rh = &an->an_rx_hist[an->an_rx_hist_next]; 1650de5af704SSam Leffler rh->arh_ticks = ticks; 1651de5af704SSam Leffler rh->arh_rssi = ds->ds_rxstat.rs_rssi; 1652de5af704SSam Leffler rh->arh_antenna = ds->ds_rxstat.rs_antenna; 1653de5af704SSam Leffler 16540a915fadSSam Leffler /* 16550a915fadSSam Leffler * Send frame up for processing. 16560a915fadSSam Leffler */ 16570a915fadSSam Leffler ieee80211_input(ifp, m, ni, 16580a915fadSSam Leffler ds->ds_rxstat.rs_rssi, ds->ds_rxstat.rs_tstamp); 1659de5af704SSam Leffler 16600a915fadSSam Leffler /* 16610a915fadSSam Leffler * The frame may have caused the node to be marked for 16620a915fadSSam Leffler * reclamation (e.g. in response to a DEAUTH message) 16630a915fadSSam Leffler * so use free_node here instead of unref_node. 16640a915fadSSam Leffler */ 16650a915fadSSam Leffler if (ni == ic->ic_bss) 16660a915fadSSam Leffler ieee80211_unref_node(&ni); 16670a915fadSSam Leffler else 16680a915fadSSam Leffler ieee80211_free_node(ic, ni); 16695591b213SSam Leffler rx_next: 16705591b213SSam Leffler TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list); 16715591b213SSam Leffler } while (ath_rxbuf_init(sc, bf) == 0); 16725591b213SSam Leffler 16735591b213SSam Leffler ath_hal_rxmonitor(ah); /* rx signal state monitoring */ 16745591b213SSam Leffler ath_hal_rxena(ah); /* in case of RXEOL */ 16755591b213SSam Leffler } 16765591b213SSam Leffler 16775591b213SSam Leffler /* 16785591b213SSam Leffler * XXX Size of an ACK control frame in bytes. 16795591b213SSam Leffler */ 16805591b213SSam Leffler #define IEEE80211_ACK_SIZE (2+2+IEEE80211_ADDR_LEN+4) 16815591b213SSam Leffler 16825591b213SSam Leffler static int 16835591b213SSam Leffler ath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni, struct ath_buf *bf, 16845591b213SSam Leffler struct mbuf *m0) 16855591b213SSam Leffler { 16865591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 16875591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 16885591b213SSam Leffler struct ifnet *ifp = &sc->sc_ic.ic_if; 16895591b213SSam Leffler int i, error, iswep, hdrlen, pktlen; 16905591b213SSam Leffler u_int8_t rix, cix, txrate, ctsrate; 16915591b213SSam Leffler struct ath_desc *ds; 16925591b213SSam Leffler struct mbuf *m; 16935591b213SSam Leffler struct ieee80211_frame *wh; 16945591b213SSam Leffler u_int32_t iv; 16955591b213SSam Leffler u_int8_t *ivp; 16965591b213SSam Leffler u_int8_t hdrbuf[sizeof(struct ieee80211_frame) + 16975591b213SSam Leffler IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN]; 16985591b213SSam Leffler u_int subtype, flags, ctsduration, antenna; 16995591b213SSam Leffler HAL_PKT_TYPE atype; 17005591b213SSam Leffler const HAL_RATE_TABLE *rt; 17015591b213SSam Leffler HAL_BOOL shortPreamble; 17025591b213SSam Leffler struct ath_node *an; 17035591b213SSam Leffler 17045591b213SSam Leffler wh = mtod(m0, struct ieee80211_frame *); 17055591b213SSam Leffler iswep = wh->i_fc[1] & IEEE80211_FC1_WEP; 17065591b213SSam Leffler hdrlen = sizeof(struct ieee80211_frame); 17075591b213SSam Leffler pktlen = m0->m_pkthdr.len; 17085591b213SSam Leffler 17095591b213SSam Leffler if (iswep) { 17105591b213SSam Leffler memcpy(hdrbuf, mtod(m0, caddr_t), hdrlen); 17115591b213SSam Leffler m_adj(m0, hdrlen); 17125591b213SSam Leffler M_PREPEND(m0, sizeof(hdrbuf), M_DONTWAIT); 17135591b213SSam Leffler if (m0 == NULL) { 17145591b213SSam Leffler sc->sc_stats.ast_tx_nombuf++; 17155591b213SSam Leffler return ENOMEM; 17165591b213SSam Leffler } 17175591b213SSam Leffler ivp = hdrbuf + hdrlen; 17185591b213SSam Leffler /* 17195591b213SSam Leffler * XXX 17205591b213SSam Leffler * IV must not duplicate during the lifetime of the key. 17215591b213SSam Leffler * But no mechanism to renew keys is defined in IEEE 802.11 17225591b213SSam Leffler * WEP. And IV may be duplicated between other stations 17235591b213SSam Leffler * because of the session key itself is shared. 17245591b213SSam Leffler * So we use pseudo random IV for now, though it is not the 17255591b213SSam Leffler * right way. 17265591b213SSam Leffler */ 17275591b213SSam Leffler iv = arc4random(); 17285591b213SSam Leffler for (i = 0; i < IEEE80211_WEP_IVLEN; i++) { 17295591b213SSam Leffler ivp[i] = iv; 17305591b213SSam Leffler iv >>= 8; 17315591b213SSam Leffler } 17325591b213SSam Leffler ivp[i] = sc->sc_ic.ic_wep_txkey << 6; /* Key ID and pad */ 17335591b213SSam Leffler memcpy(mtod(m0, caddr_t), hdrbuf, sizeof(hdrbuf)); 17345591b213SSam Leffler /* 17355591b213SSam Leffler * The ICV length must be included into hdrlen and pktlen. 17365591b213SSam Leffler */ 17375591b213SSam Leffler hdrlen = sizeof(hdrbuf) + IEEE80211_WEP_CRCLEN; 17385591b213SSam Leffler pktlen = m0->m_pkthdr.len + IEEE80211_WEP_CRCLEN; 17395591b213SSam Leffler } 17405591b213SSam Leffler pktlen += IEEE80211_CRC_LEN; 17415591b213SSam Leffler 17425591b213SSam Leffler /* 17435591b213SSam Leffler * Load the DMA map so any coalescing is done. This 17445591b213SSam Leffler * also calculates the number of descriptors we need. 17455591b213SSam Leffler */ 17465591b213SSam Leffler error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m0, 17475591b213SSam Leffler ath_mbuf_load_cb, bf, 17485591b213SSam Leffler BUS_DMA_NOWAIT); 17495591b213SSam Leffler if (error != 0) { 17505591b213SSam Leffler sc->sc_stats.ast_tx_busdma++; 17515591b213SSam Leffler m_freem(m0); 17525591b213SSam Leffler return error; 17535591b213SSam Leffler } 17545591b213SSam Leffler /* 17555591b213SSam Leffler * Discard null packets and check for packets that 17565591b213SSam Leffler * require too many TX descriptors. We try to convert 17575591b213SSam Leffler * the latter to a cluster. 17585591b213SSam Leffler */ 17595591b213SSam Leffler if (bf->bf_nseg > ATH_TXDESC) { /* too many desc's, linearize */ 17605591b213SSam Leffler sc->sc_stats.ast_tx_linear++; 17615591b213SSam Leffler MGETHDR(m, M_DONTWAIT, MT_DATA); 17625591b213SSam Leffler if (m == NULL) { 17635591b213SSam Leffler sc->sc_stats.ast_tx_nombuf++; 17645591b213SSam Leffler m_freem(m0); 17655591b213SSam Leffler return ENOMEM; 17665591b213SSam Leffler } 17675591b213SSam Leffler M_MOVE_PKTHDR(m, m0); 17685591b213SSam Leffler MCLGET(m, M_DONTWAIT); 17695591b213SSam Leffler if ((m->m_flags & M_EXT) == 0) { 17705591b213SSam Leffler sc->sc_stats.ast_tx_nomcl++; 17715591b213SSam Leffler m_freem(m0); 17725591b213SSam Leffler m_free(m); 17735591b213SSam Leffler return ENOMEM; 17745591b213SSam Leffler } 17755591b213SSam Leffler m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t)); 17765591b213SSam Leffler m_freem(m0); 17775591b213SSam Leffler m->m_len = m->m_pkthdr.len; 17785591b213SSam Leffler m0 = m; 17795591b213SSam Leffler error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m0, 17805591b213SSam Leffler ath_mbuf_load_cb, bf, 17815591b213SSam Leffler BUS_DMA_NOWAIT); 17825591b213SSam Leffler if (error != 0) { 17835591b213SSam Leffler sc->sc_stats.ast_tx_busdma++; 17845591b213SSam Leffler m_freem(m0); 17855591b213SSam Leffler return error; 17865591b213SSam Leffler } 17875591b213SSam Leffler KASSERT(bf->bf_nseg == 1, 17885591b213SSam Leffler ("ath_tx_start: packet not one segment; nseg %u", 17895591b213SSam Leffler bf->bf_nseg)); 17905591b213SSam Leffler } else if (bf->bf_nseg == 0) { /* null packet, discard */ 17915591b213SSam Leffler sc->sc_stats.ast_tx_nodata++; 17925591b213SSam Leffler m_freem(m0); 17935591b213SSam Leffler return EIO; 17945591b213SSam Leffler } 17955591b213SSam Leffler DPRINTF2(("ath_tx_start: m %p len %u\n", m0, pktlen)); 17965591b213SSam Leffler bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE); 17975591b213SSam Leffler bf->bf_m = m0; 17980a915fadSSam Leffler bf->bf_node = ni; /* NB: held reference */ 17995591b213SSam Leffler 18005591b213SSam Leffler /* setup descriptors */ 18015591b213SSam Leffler ds = bf->bf_desc; 18025591b213SSam Leffler rt = sc->sc_currates; 18035591b213SSam Leffler KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode)); 18045591b213SSam Leffler 18055591b213SSam Leffler /* 18065591b213SSam Leffler * Calculate Atheros packet type from IEEE80211 packet header 18075591b213SSam Leffler * and setup for rate calculations. 18085591b213SSam Leffler */ 18095591b213SSam Leffler atype = HAL_PKT_TYPE_NORMAL; /* default */ 18105591b213SSam Leffler switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) { 18115591b213SSam Leffler case IEEE80211_FC0_TYPE_MGT: 18125591b213SSam Leffler subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 18135591b213SSam Leffler if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) 18145591b213SSam Leffler atype = HAL_PKT_TYPE_BEACON; 18155591b213SSam Leffler else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) 18165591b213SSam Leffler atype = HAL_PKT_TYPE_PROBE_RESP; 18175591b213SSam Leffler else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM) 18185591b213SSam Leffler atype = HAL_PKT_TYPE_ATIM; 18195591b213SSam Leffler rix = 0; /* XXX lowest rate */ 18205591b213SSam Leffler break; 18215591b213SSam Leffler case IEEE80211_FC0_TYPE_CTL: 18225591b213SSam Leffler subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 18235591b213SSam Leffler if (subtype == IEEE80211_FC0_SUBTYPE_PS_POLL) 18245591b213SSam Leffler atype = HAL_PKT_TYPE_PSPOLL; 18255591b213SSam Leffler rix = 0; /* XXX lowest rate */ 18265591b213SSam Leffler break; 18275591b213SSam Leffler default: 18285591b213SSam Leffler rix = sc->sc_rixmap[ni->ni_rates.rs_rates[ni->ni_txrate] & 18295591b213SSam Leffler IEEE80211_RATE_VAL]; 18305591b213SSam Leffler if (rix == 0xff) { 18315591b213SSam Leffler if_printf(ifp, "bogus xmit rate 0x%x\n", 18325591b213SSam Leffler ni->ni_rates.rs_rates[ni->ni_txrate]); 18335591b213SSam Leffler sc->sc_stats.ast_tx_badrate++; 18345591b213SSam Leffler m_freem(m0); 18355591b213SSam Leffler return EIO; 18365591b213SSam Leffler } 18375591b213SSam Leffler break; 18385591b213SSam Leffler } 18395591b213SSam Leffler /* 18405591b213SSam Leffler * NB: the 802.11 layer marks whether or not we should 18415591b213SSam Leffler * use short preamble based on the current mode and 18425591b213SSam Leffler * negotiated parameters. 18435591b213SSam Leffler */ 1844f6aa038bSSam Leffler if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 1845f6aa038bSSam Leffler (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) { 18465591b213SSam Leffler txrate = rt->info[rix].rateCode | rt->info[rix].shortPreamble; 18475591b213SSam Leffler shortPreamble = AH_TRUE; 18485591b213SSam Leffler sc->sc_stats.ast_tx_shortpre++; 18495591b213SSam Leffler } else { 18505591b213SSam Leffler txrate = rt->info[rix].rateCode; 18515591b213SSam Leffler shortPreamble = AH_FALSE; 18525591b213SSam Leffler } 18535591b213SSam Leffler 18545591b213SSam Leffler /* 18555591b213SSam Leffler * Calculate miscellaneous flags. 18565591b213SSam Leffler */ 18575591b213SSam Leffler flags = HAL_TXDESC_CLRDMASK; /* XXX needed for wep errors */ 18585591b213SSam Leffler if (IEEE80211_IS_MULTICAST(wh->i_addr1)) { 18595591b213SSam Leffler flags |= HAL_TXDESC_NOACK; /* no ack on broad/multicast */ 18605591b213SSam Leffler sc->sc_stats.ast_tx_noack++; 18615591b213SSam Leffler } else if (pktlen > ic->ic_rtsthreshold) { 18625591b213SSam Leffler flags |= HAL_TXDESC_RTSENA; /* RTS based on frame length */ 18635591b213SSam Leffler sc->sc_stats.ast_tx_rts++; 18645591b213SSam Leffler } 18655591b213SSam Leffler 18665591b213SSam Leffler /* 1867f6aa038bSSam Leffler * Calculate duration. This logically belongs in the 802.11 1868f6aa038bSSam Leffler * layer but it lacks sufficient information to calculate it. 1869f6aa038bSSam Leffler */ 1870f6aa038bSSam Leffler if ((flags & HAL_TXDESC_NOACK) == 0 && 1871f6aa038bSSam Leffler (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL) { 1872f6aa038bSSam Leffler u_int16_t dur; 1873f6aa038bSSam Leffler /* 1874f6aa038bSSam Leffler * XXX not right with fragmentation. 1875f6aa038bSSam Leffler */ 1876f6aa038bSSam Leffler dur = ath_hal_computetxtime(ah, rt, IEEE80211_ACK_SIZE, 1877f6aa038bSSam Leffler rix, shortPreamble); 1878f6aa038bSSam Leffler *((u_int16_t*) wh->i_dur) = htole16(dur); 1879f6aa038bSSam Leffler } 1880f6aa038bSSam Leffler 1881f6aa038bSSam Leffler /* 18825591b213SSam Leffler * Calculate RTS/CTS rate and duration if needed. 18835591b213SSam Leffler */ 18845591b213SSam Leffler ctsduration = 0; 18855591b213SSam Leffler if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA)) { 18865591b213SSam Leffler /* 18875591b213SSam Leffler * CTS transmit rate is derived from the transmit rate 18885591b213SSam Leffler * by looking in the h/w rate table. We must also factor 18895591b213SSam Leffler * in whether or not a short preamble is to be used. 18905591b213SSam Leffler */ 18915591b213SSam Leffler cix = rt->info[rix].controlRate; 18925591b213SSam Leffler ctsrate = rt->info[cix].rateCode; 18935591b213SSam Leffler if (shortPreamble) 18945591b213SSam Leffler ctsrate |= rt->info[cix].shortPreamble; 18955591b213SSam Leffler /* 18965591b213SSam Leffler * Compute the transmit duration based on the size 18975591b213SSam Leffler * of an ACK frame. We call into the HAL to do the 18985591b213SSam Leffler * computation since it depends on the characteristics 18995591b213SSam Leffler * of the actual PHY being used. 19005591b213SSam Leffler */ 19015591b213SSam Leffler if (flags & HAL_TXDESC_RTSENA) { /* SIFS + CTS */ 19025591b213SSam Leffler ctsduration += ath_hal_computetxtime(ah, 19035591b213SSam Leffler rt, IEEE80211_ACK_SIZE, cix, shortPreamble); 19045591b213SSam Leffler } 19055591b213SSam Leffler /* SIFS + data */ 19065591b213SSam Leffler ctsduration += ath_hal_computetxtime(ah, 19075591b213SSam Leffler rt, pktlen, rix, shortPreamble); 19085591b213SSam Leffler if ((flags & HAL_TXDESC_NOACK) == 0) { /* SIFS + ACK */ 19095591b213SSam Leffler ctsduration += ath_hal_computetxtime(ah, 19105591b213SSam Leffler rt, IEEE80211_ACK_SIZE, cix, shortPreamble); 19115591b213SSam Leffler } 19125591b213SSam Leffler } else 19135591b213SSam Leffler ctsrate = 0; 19145591b213SSam Leffler 19155591b213SSam Leffler /* 19165591b213SSam Leffler * For now use the antenna on which the last good 19175591b213SSam Leffler * frame was received on. We assume this field is 19185591b213SSam Leffler * initialized to 0 which gives us ``auto'' or the 19195591b213SSam Leffler * ``default'' antenna. 19205591b213SSam Leffler */ 19215591b213SSam Leffler an = (struct ath_node *) ni; 19225591b213SSam Leffler if (an->an_tx_antenna) 19235591b213SSam Leffler antenna = an->an_tx_antenna; 19245591b213SSam Leffler else 1925de5af704SSam Leffler antenna = an->an_rx_hist[an->an_rx_hist_next].arh_antenna; 19265591b213SSam Leffler 19275591b213SSam Leffler /* 19285591b213SSam Leffler * Formulate first tx descriptor with tx controls. 19295591b213SSam Leffler */ 19305591b213SSam Leffler /* XXX check return value? */ 19315591b213SSam Leffler ath_hal_setuptxdesc(ah, ds 19325591b213SSam Leffler , pktlen /* packet length */ 19335591b213SSam Leffler , hdrlen /* header length */ 19345591b213SSam Leffler , atype /* Atheros packet type */ 19355591b213SSam Leffler , 60 /* txpower XXX */ 19365591b213SSam Leffler , txrate, 1+10 /* series 0 rate/tries */ 19375591b213SSam Leffler , iswep ? sc->sc_ic.ic_wep_txkey : HAL_TXKEYIX_INVALID 19385591b213SSam Leffler , antenna /* antenna mode */ 19395591b213SSam Leffler , flags /* flags */ 19405591b213SSam Leffler , ctsrate /* rts/cts rate */ 19415591b213SSam Leffler , ctsduration /* rts/cts duration */ 19425591b213SSam Leffler ); 19435591b213SSam Leffler #ifdef notyet 19445591b213SSam Leffler ath_hal_setupxtxdesc(ah, ds 19455591b213SSam Leffler , AH_FALSE /* short preamble */ 19465591b213SSam Leffler , 0, 0 /* series 1 rate/tries */ 19475591b213SSam Leffler , 0, 0 /* series 2 rate/tries */ 19485591b213SSam Leffler , 0, 0 /* series 3 rate/tries */ 19495591b213SSam Leffler ); 19505591b213SSam Leffler #endif 19515591b213SSam Leffler /* 19525591b213SSam Leffler * Fillin the remainder of the descriptor info. 19535591b213SSam Leffler */ 19545591b213SSam Leffler for (i = 0; i < bf->bf_nseg; i++, ds++) { 19555591b213SSam Leffler ds->ds_data = bf->bf_segs[i].ds_addr; 19565591b213SSam Leffler if (i == bf->bf_nseg - 1) 19575591b213SSam Leffler ds->ds_link = 0; 19585591b213SSam Leffler else 19595591b213SSam Leffler ds->ds_link = bf->bf_daddr + sizeof(*ds) * (i + 1); 19605591b213SSam Leffler ath_hal_filltxdesc(ah, ds 19615591b213SSam Leffler , bf->bf_segs[i].ds_len /* segment length */ 19625591b213SSam Leffler , i == 0 /* first segment */ 19635591b213SSam Leffler , i == bf->bf_nseg - 1 /* last segment */ 19645591b213SSam Leffler ); 19655591b213SSam Leffler DPRINTF2(("ath_tx_start: %d: %08x %08x %08x %08x %08x %08x\n", 19665591b213SSam Leffler i, ds->ds_link, ds->ds_data, ds->ds_ctl0, ds->ds_ctl1, 19675591b213SSam Leffler ds->ds_hw[0], ds->ds_hw[1])); 19685591b213SSam Leffler } 19695591b213SSam Leffler 19705591b213SSam Leffler /* 19715591b213SSam Leffler * Insert the frame on the outbound list and 19725591b213SSam Leffler * pass it on to the hardware. 19735591b213SSam Leffler */ 19745591b213SSam Leffler mtx_lock(&sc->sc_txqlock); 19755591b213SSam Leffler TAILQ_INSERT_TAIL(&sc->sc_txq, bf, bf_list); 19765591b213SSam Leffler if (sc->sc_txlink == NULL) { 19775591b213SSam Leffler ath_hal_puttxbuf(ah, sc->sc_txhalq, bf->bf_daddr); 19785591b213SSam Leffler DPRINTF2(("ath_tx_start: TXDP0 = %p (%p)\n", 19795591b213SSam Leffler (caddr_t)bf->bf_daddr, bf->bf_desc)); 19805591b213SSam Leffler } else { 19815591b213SSam Leffler *sc->sc_txlink = bf->bf_daddr; 19825591b213SSam Leffler DPRINTF2(("ath_tx_start: link(%p)=%p (%p)\n", 19835591b213SSam Leffler sc->sc_txlink, (caddr_t)bf->bf_daddr, bf->bf_desc)); 19845591b213SSam Leffler } 19855591b213SSam Leffler sc->sc_txlink = &bf->bf_desc[bf->bf_nseg - 1].ds_link; 19865591b213SSam Leffler mtx_unlock(&sc->sc_txqlock); 19875591b213SSam Leffler 19885591b213SSam Leffler ath_hal_txstart(ah, sc->sc_txhalq); 19895591b213SSam Leffler return 0; 19905591b213SSam Leffler } 19915591b213SSam Leffler 19925591b213SSam Leffler static void 19935591b213SSam Leffler ath_tx_proc(void *arg, int npending) 19945591b213SSam Leffler { 19955591b213SSam Leffler struct ath_softc *sc = arg; 19965591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 19975591b213SSam Leffler struct ath_buf *bf; 19980a915fadSSam Leffler struct ieee80211com *ic = &sc->sc_ic; 19990a915fadSSam Leffler struct ifnet *ifp = &ic->ic_if; 20005591b213SSam Leffler struct ath_desc *ds; 20015591b213SSam Leffler struct ieee80211_node *ni; 20025591b213SSam Leffler struct ath_node *an; 20035591b213SSam Leffler int sr, lr; 20045591b213SSam Leffler HAL_STATUS status; 20055591b213SSam Leffler 20065591b213SSam Leffler DPRINTF2(("ath_tx_proc: pending %u tx queue %p, link %p\n", 20075591b213SSam Leffler npending, (caddr_t) ath_hal_gettxbuf(sc->sc_ah, sc->sc_txhalq), 20085591b213SSam Leffler sc->sc_txlink)); 20095591b213SSam Leffler for (;;) { 20105591b213SSam Leffler mtx_lock(&sc->sc_txqlock); 20115591b213SSam Leffler bf = TAILQ_FIRST(&sc->sc_txq); 20125591b213SSam Leffler if (bf == NULL) { 20135591b213SSam Leffler sc->sc_txlink = NULL; 20145591b213SSam Leffler mtx_unlock(&sc->sc_txqlock); 20155591b213SSam Leffler break; 20165591b213SSam Leffler } 20175591b213SSam Leffler /* only the last descriptor is needed */ 20185591b213SSam Leffler ds = &bf->bf_desc[bf->bf_nseg - 1]; 20195591b213SSam Leffler status = ath_hal_txprocdesc(ah, ds); 20205591b213SSam Leffler #ifdef AR_DEBUG 20215591b213SSam Leffler if (ath_debug > 1) 20225591b213SSam Leffler ath_printtxbuf(bf, status == HAL_OK); 20235591b213SSam Leffler #endif 20245591b213SSam Leffler if (status == HAL_EINPROGRESS) { 20255591b213SSam Leffler mtx_unlock(&sc->sc_txqlock); 20265591b213SSam Leffler break; 20275591b213SSam Leffler } 20285591b213SSam Leffler TAILQ_REMOVE(&sc->sc_txq, bf, bf_list); 20295591b213SSam Leffler mtx_unlock(&sc->sc_txqlock); 20305591b213SSam Leffler 20315591b213SSam Leffler ni = bf->bf_node; 20325591b213SSam Leffler if (ni != NULL) { 20335591b213SSam Leffler an = (struct ath_node *) ni; 20345591b213SSam Leffler if (ds->ds_txstat.ts_status == 0) { 20355591b213SSam Leffler an->an_tx_ok++; 20365591b213SSam Leffler an->an_tx_antenna = ds->ds_txstat.ts_antenna; 20375591b213SSam Leffler } else { 20385591b213SSam Leffler an->an_tx_err++; 20395591b213SSam Leffler ifp->if_oerrors++; 20405591b213SSam Leffler if (ds->ds_txstat.ts_status & HAL_TXERR_XRETRY) 20415591b213SSam Leffler sc->sc_stats.ast_tx_xretries++; 20425591b213SSam Leffler if (ds->ds_txstat.ts_status & HAL_TXERR_FIFO) 20435591b213SSam Leffler sc->sc_stats.ast_tx_fifoerr++; 20445591b213SSam Leffler if (ds->ds_txstat.ts_status & HAL_TXERR_FILT) 20455591b213SSam Leffler sc->sc_stats.ast_tx_filtered++; 20465591b213SSam Leffler an->an_tx_antenna = 0; /* invalidate */ 20475591b213SSam Leffler } 20485591b213SSam Leffler sr = ds->ds_txstat.ts_shortretry; 20495591b213SSam Leffler lr = ds->ds_txstat.ts_longretry; 20505591b213SSam Leffler sc->sc_stats.ast_tx_shortretry += sr; 20515591b213SSam Leffler sc->sc_stats.ast_tx_longretry += lr; 20525591b213SSam Leffler if (sr + lr) 20535591b213SSam Leffler an->an_tx_retr++; 20540a915fadSSam Leffler /* 20550a915fadSSam Leffler * Reclaim reference to node. 20560a915fadSSam Leffler * 20570a915fadSSam Leffler * NB: the node may be reclaimed here if, for example 20580a915fadSSam Leffler * this is a DEAUTH message that was sent and the 20590a915fadSSam Leffler * node was timed out due to inactivity. 20600a915fadSSam Leffler */ 20610a915fadSSam Leffler if (ni != ic->ic_bss) 20620a915fadSSam Leffler ieee80211_free_node(ic, ni); 20635591b213SSam Leffler } 20645591b213SSam Leffler bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, 20655591b213SSam Leffler BUS_DMASYNC_POSTWRITE); 20665591b213SSam Leffler bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 20675591b213SSam Leffler m_freem(bf->bf_m); 20685591b213SSam Leffler bf->bf_m = NULL; 20695591b213SSam Leffler bf->bf_node = NULL; 20705591b213SSam Leffler 20715591b213SSam Leffler mtx_lock(&sc->sc_txbuflock); 20725591b213SSam Leffler TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list); 20735591b213SSam Leffler mtx_unlock(&sc->sc_txbuflock); 20745591b213SSam Leffler } 20755591b213SSam Leffler ifp->if_flags &= ~IFF_OACTIVE; 20765591b213SSam Leffler sc->sc_tx_timer = 0; 20775591b213SSam Leffler 20785591b213SSam Leffler ath_start(ifp); 20795591b213SSam Leffler } 20805591b213SSam Leffler 20815591b213SSam Leffler /* 20825591b213SSam Leffler * Drain the transmit queue and reclaim resources. 20835591b213SSam Leffler */ 20845591b213SSam Leffler static void 20855591b213SSam Leffler ath_draintxq(struct ath_softc *sc) 20865591b213SSam Leffler { 20875591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 20885591b213SSam Leffler struct ifnet *ifp = &sc->sc_ic.ic_if; 20895591b213SSam Leffler struct ath_buf *bf; 20905591b213SSam Leffler 20915591b213SSam Leffler /* XXX return value */ 20925591b213SSam Leffler if (!sc->sc_invalid) { 20935591b213SSam Leffler /* don't touch the hardware if marked invalid */ 20945591b213SSam Leffler (void) ath_hal_stoptxdma(ah, sc->sc_txhalq); 20955591b213SSam Leffler DPRINTF(("ath_draintxq: tx queue %p, link %p\n", 20965591b213SSam Leffler (caddr_t) ath_hal_gettxbuf(ah, sc->sc_txhalq), 20975591b213SSam Leffler sc->sc_txlink)); 20985591b213SSam Leffler (void) ath_hal_stoptxdma(ah, sc->sc_bhalq); 20995591b213SSam Leffler DPRINTF(("ath_draintxq: beacon queue %p\n", 21005591b213SSam Leffler (caddr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq))); 21015591b213SSam Leffler } 21025591b213SSam Leffler for (;;) { 21035591b213SSam Leffler mtx_lock(&sc->sc_txqlock); 21045591b213SSam Leffler bf = TAILQ_FIRST(&sc->sc_txq); 21055591b213SSam Leffler if (bf == NULL) { 21065591b213SSam Leffler sc->sc_txlink = NULL; 21075591b213SSam Leffler mtx_unlock(&sc->sc_txqlock); 21085591b213SSam Leffler break; 21095591b213SSam Leffler } 21105591b213SSam Leffler TAILQ_REMOVE(&sc->sc_txq, bf, bf_list); 21115591b213SSam Leffler mtx_unlock(&sc->sc_txqlock); 21125591b213SSam Leffler #ifdef AR_DEBUG 21135591b213SSam Leffler if (ath_debug) 21145591b213SSam Leffler ath_printtxbuf(bf, 21155591b213SSam Leffler ath_hal_txprocdesc(ah, bf->bf_desc) == HAL_OK); 21165591b213SSam Leffler #endif /* AR_DEBUG */ 21175591b213SSam Leffler bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 21185591b213SSam Leffler m_freem(bf->bf_m); 21195591b213SSam Leffler bf->bf_m = NULL; 21205591b213SSam Leffler bf->bf_node = NULL; 21215591b213SSam Leffler mtx_lock(&sc->sc_txbuflock); 21225591b213SSam Leffler TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list); 21235591b213SSam Leffler mtx_unlock(&sc->sc_txbuflock); 21245591b213SSam Leffler } 21255591b213SSam Leffler ifp->if_flags &= ~IFF_OACTIVE; 21265591b213SSam Leffler sc->sc_tx_timer = 0; 21275591b213SSam Leffler } 21285591b213SSam Leffler 21295591b213SSam Leffler /* 21305591b213SSam Leffler * Disable the receive h/w in preparation for a reset. 21315591b213SSam Leffler */ 21325591b213SSam Leffler static void 21335591b213SSam Leffler ath_stoprecv(struct ath_softc *sc) 21345591b213SSam Leffler { 21355591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 21365591b213SSam Leffler 21375591b213SSam Leffler ath_hal_stoppcurecv(ah); /* disable PCU */ 21385591b213SSam Leffler ath_hal_setrxfilter(ah, 0); /* clear recv filter */ 21395591b213SSam Leffler ath_hal_stopdmarecv(ah); /* disable DMA engine */ 21405591b213SSam Leffler DELAY(3000); /* long enough for 1 frame */ 21415591b213SSam Leffler #ifdef AR_DEBUG 21425591b213SSam Leffler if (ath_debug) { 21435591b213SSam Leffler struct ath_buf *bf; 21445591b213SSam Leffler 21455591b213SSam Leffler DPRINTF(("ath_stoprecv: rx queue %p, link %p\n", 21465591b213SSam Leffler (caddr_t) ath_hal_getrxbuf(ah), sc->sc_rxlink)); 21475591b213SSam Leffler TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) { 21485591b213SSam Leffler if (ath_hal_rxprocdesc(ah, bf->bf_desc) == HAL_OK) 21495591b213SSam Leffler ath_printrxbuf(bf, 1); 21505591b213SSam Leffler } 21515591b213SSam Leffler } 21525591b213SSam Leffler #endif 21535591b213SSam Leffler sc->sc_rxlink = NULL; /* just in case */ 21545591b213SSam Leffler } 21555591b213SSam Leffler 21565591b213SSam Leffler /* 21575591b213SSam Leffler * Enable the receive h/w following a reset. 21585591b213SSam Leffler */ 21595591b213SSam Leffler static int 21605591b213SSam Leffler ath_startrecv(struct ath_softc *sc) 21615591b213SSam Leffler { 21625591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 21635591b213SSam Leffler struct ath_buf *bf; 21645591b213SSam Leffler 21655591b213SSam Leffler sc->sc_rxlink = NULL; 21665591b213SSam Leffler TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) { 21675591b213SSam Leffler int error = ath_rxbuf_init(sc, bf); 21685591b213SSam Leffler if (error != 0) { 21695591b213SSam Leffler DPRINTF(("ath_startrecv: ath_rxbuf_init failed %d\n", 21705591b213SSam Leffler error)); 21715591b213SSam Leffler return error; 21725591b213SSam Leffler } 21735591b213SSam Leffler } 21745591b213SSam Leffler 21755591b213SSam Leffler bf = TAILQ_FIRST(&sc->sc_rxbuf); 21765591b213SSam Leffler ath_hal_putrxbuf(ah, bf->bf_daddr); 21775591b213SSam Leffler ath_hal_rxena(ah); /* enable recv descriptors */ 21785591b213SSam Leffler ath_mode_init(sc); /* set filters, etc. */ 21795591b213SSam Leffler ath_hal_startpcurecv(ah); /* re-enable PCU/DMA engine */ 21805591b213SSam Leffler return 0; 21815591b213SSam Leffler } 21825591b213SSam Leffler 21835591b213SSam Leffler /* 21845591b213SSam Leffler * Set/change channels. If the channel is really being changed, 21855591b213SSam Leffler * it's done by resetting the chip. To accomplish this we must 21865591b213SSam Leffler * first cleanup any pending DMA, then restart stuff after a la 21875591b213SSam Leffler * ath_init. 21885591b213SSam Leffler */ 21895591b213SSam Leffler static int 21905591b213SSam Leffler ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan) 21915591b213SSam Leffler { 21925591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 21935591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 21945591b213SSam Leffler 21955591b213SSam Leffler DPRINTF(("ath_chan_set: %u (%u MHz) -> %u (%u MHz)\n", 21965591b213SSam Leffler ieee80211_chan2ieee(ic, ic->ic_ibss_chan), 21975591b213SSam Leffler ic->ic_ibss_chan->ic_freq, 21985591b213SSam Leffler ieee80211_chan2ieee(ic, chan), chan->ic_freq)); 21995591b213SSam Leffler if (chan != ic->ic_ibss_chan) { 22005591b213SSam Leffler HAL_STATUS status; 22015591b213SSam Leffler HAL_CHANNEL hchan; 22025591b213SSam Leffler enum ieee80211_phymode mode; 22035591b213SSam Leffler 22045591b213SSam Leffler /* 22055591b213SSam Leffler * To switch channels clear any pending DMA operations; 22065591b213SSam Leffler * wait long enough for the RX fifo to drain, reset the 22075591b213SSam Leffler * hardware at the new frequency, and then re-enable 22085591b213SSam Leffler * the relevant bits of the h/w. 22095591b213SSam Leffler */ 22105591b213SSam Leffler ath_hal_intrset(ah, 0); /* disable interrupts */ 22115591b213SSam Leffler ath_draintxq(sc); /* clear pending tx frames */ 22125591b213SSam Leffler ath_stoprecv(sc); /* turn off frame recv */ 22135591b213SSam Leffler /* 22145591b213SSam Leffler * Convert to a HAL channel description with 22155591b213SSam Leffler * the flags constrained to reflect the current 22165591b213SSam Leffler * operating mode. 22175591b213SSam Leffler */ 22185591b213SSam Leffler hchan.channel = chan->ic_freq; 22195591b213SSam Leffler hchan.channelFlags = ath_chan2flags(ic, chan); 22205591b213SSam Leffler if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_TRUE, &status)) { 22215591b213SSam Leffler if_printf(&ic->ic_if, "ath_chan_set: unable to reset " 22225591b213SSam Leffler "channel %u (%u Mhz)\n", 22235591b213SSam Leffler ieee80211_chan2ieee(ic, chan), chan->ic_freq); 22245591b213SSam Leffler return EIO; 22255591b213SSam Leffler } 22265591b213SSam Leffler /* 22275591b213SSam Leffler * Re-enable rx framework. 22285591b213SSam Leffler */ 22295591b213SSam Leffler if (ath_startrecv(sc) != 0) { 22305591b213SSam Leffler if_printf(&ic->ic_if, 22315591b213SSam Leffler "ath_chan_set: unable to restart recv logic\n"); 22325591b213SSam Leffler return EIO; 22335591b213SSam Leffler } 22345591b213SSam Leffler 22355591b213SSam Leffler /* 223673454c73SSam Leffler * Update BPF state. 223773454c73SSam Leffler */ 223873454c73SSam Leffler sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq = 223973454c73SSam Leffler htole16(chan->ic_freq); 224073454c73SSam Leffler sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags = 224173454c73SSam Leffler htole16(chan->ic_flags); 224273454c73SSam Leffler 224373454c73SSam Leffler /* 22445591b213SSam Leffler * Change channels and update the h/w rate map 22455591b213SSam Leffler * if we're switching; e.g. 11a to 11b/g. 22465591b213SSam Leffler */ 22475591b213SSam Leffler ic->ic_ibss_chan = chan; 22485591b213SSam Leffler mode = ieee80211_chan2mode(ic, chan); 22495591b213SSam Leffler if (mode != sc->sc_curmode) 22505591b213SSam Leffler ath_setcurmode(sc, mode); 22510a915fadSSam Leffler 22520a915fadSSam Leffler /* 22530a915fadSSam Leffler * Re-enable interrupts. 22540a915fadSSam Leffler */ 22550a915fadSSam Leffler ath_hal_intrset(ah, sc->sc_imask); 22565591b213SSam Leffler } 22575591b213SSam Leffler return 0; 22585591b213SSam Leffler } 22595591b213SSam Leffler 22605591b213SSam Leffler static void 22615591b213SSam Leffler ath_next_scan(void *arg) 22625591b213SSam Leffler { 22635591b213SSam Leffler struct ath_softc *sc = arg; 22645591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 22655591b213SSam Leffler struct ifnet *ifp = &ic->ic_if; 22665591b213SSam Leffler 22675591b213SSam Leffler if (ic->ic_state == IEEE80211_S_SCAN) 22685591b213SSam Leffler ieee80211_next_scan(ifp); 22695591b213SSam Leffler } 22705591b213SSam Leffler 22715591b213SSam Leffler /* 22725591b213SSam Leffler * Periodically recalibrate the PHY to account 22735591b213SSam Leffler * for temperature/environment changes. 22745591b213SSam Leffler */ 22755591b213SSam Leffler static void 22765591b213SSam Leffler ath_calibrate(void *arg) 22775591b213SSam Leffler { 22785591b213SSam Leffler struct ath_softc *sc = arg; 22795591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 22805591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 22815591b213SSam Leffler struct ieee80211_channel *c; 22825591b213SSam Leffler HAL_CHANNEL hchan; 22835591b213SSam Leffler 22845591b213SSam Leffler sc->sc_stats.ast_per_cal++; 22855591b213SSam Leffler 22865591b213SSam Leffler /* 22875591b213SSam Leffler * Convert to a HAL channel description with the flags 22885591b213SSam Leffler * constrained to reflect the current operating mode. 22895591b213SSam Leffler */ 22905591b213SSam Leffler c = ic->ic_ibss_chan; 22915591b213SSam Leffler hchan.channel = c->ic_freq; 22925591b213SSam Leffler hchan.channelFlags = ath_chan2flags(ic, c); 22935591b213SSam Leffler 22945591b213SSam Leffler DPRINTF(("%s: channel %u/%x\n", __func__, c->ic_freq, c->ic_flags)); 22955591b213SSam Leffler 22965591b213SSam Leffler if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) { 22975591b213SSam Leffler /* 22985591b213SSam Leffler * Rfgain is out of bounds, reset the chip 22995591b213SSam Leffler * to load new gain values. 23005591b213SSam Leffler */ 23015591b213SSam Leffler sc->sc_stats.ast_per_rfgain++; 23025591b213SSam Leffler ath_reset(sc); 23035591b213SSam Leffler } 23045591b213SSam Leffler if (!ath_hal_calibrate(ah, &hchan)) { 23055591b213SSam Leffler DPRINTF(("%s: calibration of channel %u failed\n", 23065591b213SSam Leffler __func__, c->ic_freq)); 23075591b213SSam Leffler sc->sc_stats.ast_per_calfail++; 23085591b213SSam Leffler } 23095591b213SSam Leffler callout_reset(&sc->sc_cal_ch, hz * ath_calinterval, ath_calibrate, sc); 23105591b213SSam Leffler } 23115591b213SSam Leffler 23125591b213SSam Leffler static int 231345bbf62fSSam Leffler ath_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 23145591b213SSam Leffler { 23155591b213SSam Leffler struct ifnet *ifp = &ic->ic_if; 231645bbf62fSSam Leffler struct ath_softc *sc = ifp->if_softc; 231745bbf62fSSam Leffler struct ath_hal *ah = sc->sc_ah; 23185591b213SSam Leffler struct ieee80211_node *ni; 23195591b213SSam Leffler int i, error; 23205591b213SSam Leffler u_int8_t *bssid; 23215591b213SSam Leffler u_int32_t rfilt; 23225591b213SSam Leffler static const HAL_LED_STATE leds[] = { 23235591b213SSam Leffler HAL_LED_INIT, /* IEEE80211_S_INIT */ 23245591b213SSam Leffler HAL_LED_SCAN, /* IEEE80211_S_SCAN */ 23255591b213SSam Leffler HAL_LED_AUTH, /* IEEE80211_S_AUTH */ 23265591b213SSam Leffler HAL_LED_ASSOC, /* IEEE80211_S_ASSOC */ 23275591b213SSam Leffler HAL_LED_RUN, /* IEEE80211_S_RUN */ 23285591b213SSam Leffler }; 23295591b213SSam Leffler 233045bbf62fSSam Leffler DPRINTF(("%s: %s -> %s\n", __func__, 233145bbf62fSSam Leffler ieee80211_state_name[ic->ic_state], 233245bbf62fSSam Leffler ieee80211_state_name[nstate])); 23335591b213SSam Leffler 23345591b213SSam Leffler ath_hal_setledstate(ah, leds[nstate]); /* set LED */ 23355591b213SSam Leffler 23365591b213SSam Leffler if (nstate == IEEE80211_S_INIT) { 23375591b213SSam Leffler sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS); 23385591b213SSam Leffler ath_hal_intrset(ah, sc->sc_imask); 233945bbf62fSSam Leffler callout_stop(&sc->sc_scan_ch); 234045bbf62fSSam Leffler callout_stop(&sc->sc_cal_ch); 234145bbf62fSSam Leffler return (*sc->sc_newstate)(ic, nstate, arg); 23425591b213SSam Leffler } 23435591b213SSam Leffler ni = ic->ic_bss; 23445591b213SSam Leffler error = ath_chan_set(sc, ni->ni_chan); 23455591b213SSam Leffler if (error != 0) 23465591b213SSam Leffler goto bad; 23475591b213SSam Leffler rfilt = (ath_hal_getrxfilter(ah) & HAL_RX_FILTER_PHYERR) 23485591b213SSam Leffler | HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST; 2349f07a6d98SSam Leffler if (ic->ic_opmode != IEEE80211_M_STA) 2350f07a6d98SSam Leffler rfilt |= HAL_RX_FILTER_PROBEREQ; 23515591b213SSam Leffler if (ic->ic_opmode != IEEE80211_M_HOSTAP && 23525591b213SSam Leffler (ifp->if_flags & IFF_PROMISC)) 23535591b213SSam Leffler rfilt |= HAL_RX_FILTER_PROM; 2354de5af704SSam Leffler if (ic->ic_opmode == IEEE80211_M_STA || 2355de5af704SSam Leffler ic->ic_state == IEEE80211_S_SCAN) 2356de5af704SSam Leffler rfilt |= HAL_RX_FILTER_BEACON; 23575591b213SSam Leffler if (nstate == IEEE80211_S_SCAN) { 23585591b213SSam Leffler callout_reset(&sc->sc_scan_ch, (hz * ath_dwelltime) / 1000, 23595591b213SSam Leffler ath_next_scan, sc); 23605591b213SSam Leffler bssid = ifp->if_broadcastaddr; 23615591b213SSam Leffler } else { 23625591b213SSam Leffler callout_stop(&sc->sc_scan_ch); 23635591b213SSam Leffler bssid = ni->ni_bssid; 23645591b213SSam Leffler } 23655591b213SSam Leffler ath_hal_setrxfilter(ah, rfilt); 23665591b213SSam Leffler DPRINTF(("%s: RX filter 0x%x bssid %s\n", 23675591b213SSam Leffler __func__, rfilt, ether_sprintf(bssid))); 23685591b213SSam Leffler 23695591b213SSam Leffler if (nstate == IEEE80211_S_RUN && ic->ic_opmode == IEEE80211_M_STA) 23705591b213SSam Leffler ath_hal_setassocid(ah, bssid, ni->ni_associd); 23715591b213SSam Leffler else 23725591b213SSam Leffler ath_hal_setassocid(ah, bssid, 0); 23735591b213SSam Leffler if (ic->ic_flags & IEEE80211_F_WEPON) { 23745591b213SSam Leffler for (i = 0; i < IEEE80211_WEP_NKID; i++) 23755591b213SSam Leffler if (ath_hal_keyisvalid(ah, i)) 23765591b213SSam Leffler ath_hal_keysetmac(ah, i, bssid); 23775591b213SSam Leffler } 23785591b213SSam Leffler 23795591b213SSam Leffler if (nstate == IEEE80211_S_RUN) { 23805591b213SSam Leffler DPRINTF(("%s(RUN): ic_flags=0x%08x iv=%d bssid=%s " 23815591b213SSam Leffler "capinfo=0x%04x chan=%d\n" 23825591b213SSam Leffler , __func__ 23835591b213SSam Leffler , ic->ic_flags 23845591b213SSam Leffler , ni->ni_intval 23855591b213SSam Leffler , ether_sprintf(ni->ni_bssid) 23865591b213SSam Leffler , ni->ni_capinfo 23875591b213SSam Leffler , ieee80211_chan2ieee(ic, ni->ni_chan))); 23885591b213SSam Leffler 23895591b213SSam Leffler /* 23905591b213SSam Leffler * Allocate and setup the beacon frame for AP or adhoc mode. 23915591b213SSam Leffler */ 23926b59f5e3SSam Leffler if (ic->ic_opmode == IEEE80211_M_HOSTAP || 23936b59f5e3SSam Leffler ic->ic_opmode == IEEE80211_M_IBSS) { 23945591b213SSam Leffler error = ath_beacon_alloc(sc, ni); 23955591b213SSam Leffler if (error != 0) 23965591b213SSam Leffler goto bad; 23975591b213SSam Leffler } 23985591b213SSam Leffler 23995591b213SSam Leffler /* 24005591b213SSam Leffler * Configure the beacon and sleep timers. 24015591b213SSam Leffler */ 24025591b213SSam Leffler ath_beacon_config(sc); 24035591b213SSam Leffler 24045591b213SSam Leffler /* start periodic recalibration timer */ 24055591b213SSam Leffler callout_reset(&sc->sc_cal_ch, hz * ath_calinterval, 24065591b213SSam Leffler ath_calibrate, sc); 24075591b213SSam Leffler } else { 24085591b213SSam Leffler sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS); 24095591b213SSam Leffler ath_hal_intrset(ah, sc->sc_imask); 24105591b213SSam Leffler callout_stop(&sc->sc_cal_ch); /* no calibration */ 24115591b213SSam Leffler } 24125591b213SSam Leffler /* 24135591b213SSam Leffler * Reset the rate control state. 24145591b213SSam Leffler */ 24155591b213SSam Leffler ath_rate_ctl_reset(sc, nstate); 241645bbf62fSSam Leffler /* 241745bbf62fSSam Leffler * Invoke the parent method to complete the work. 241845bbf62fSSam Leffler */ 241945bbf62fSSam Leffler return (*sc->sc_newstate)(ic, nstate, arg); 24205591b213SSam Leffler bad: 24215591b213SSam Leffler callout_stop(&sc->sc_scan_ch); 24225591b213SSam Leffler callout_stop(&sc->sc_cal_ch); 242345bbf62fSSam Leffler /* NB: do not invoke the parent */ 24245591b213SSam Leffler return error; 24255591b213SSam Leffler } 24265591b213SSam Leffler 24275591b213SSam Leffler /* 24285591b213SSam Leffler * Setup driver-specific state for a newly associated node. 24295591b213SSam Leffler * Note that we're called also on a re-associate, the isnew 24305591b213SSam Leffler * param tells us if this is the first time or not. 24315591b213SSam Leffler */ 24325591b213SSam Leffler static void 24335591b213SSam Leffler ath_newassoc(struct ieee80211com *ic, struct ieee80211_node *ni, int isnew) 24345591b213SSam Leffler { 24355591b213SSam Leffler if (isnew) { 24365591b213SSam Leffler struct ath_node *an = (struct ath_node *) ni; 24375591b213SSam Leffler 24385591b213SSam Leffler an->an_tx_ok = an->an_tx_err = 24395591b213SSam Leffler an->an_tx_retr = an->an_tx_upper = 0; 24405591b213SSam Leffler /* start with highest negotiated rate */ 24415591b213SSam Leffler /* 24425591b213SSam Leffler * XXX should do otherwise but only when 24435591b213SSam Leffler * the rate control algorithm is better. 24445591b213SSam Leffler */ 24455591b213SSam Leffler KASSERT(ni->ni_rates.rs_nrates > 0, 24465591b213SSam Leffler ("new association w/ no rates!")); 24475591b213SSam Leffler ni->ni_txrate = ni->ni_rates.rs_nrates - 1; 24485591b213SSam Leffler } 24495591b213SSam Leffler } 24505591b213SSam Leffler 24515591b213SSam Leffler static int 24525591b213SSam Leffler ath_getchannels(struct ath_softc *sc, u_int cc, HAL_BOOL outdoor) 24535591b213SSam Leffler { 24545591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 24555591b213SSam Leffler struct ifnet *ifp = &ic->ic_if; 24565591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 24575591b213SSam Leffler HAL_CHANNEL *chans; 24585591b213SSam Leffler int i, ix, nchan; 24595591b213SSam Leffler 24605591b213SSam Leffler chans = malloc(IEEE80211_CHAN_MAX * sizeof(HAL_CHANNEL), 24615591b213SSam Leffler M_TEMP, M_NOWAIT); 24625591b213SSam Leffler if (chans == NULL) { 24635591b213SSam Leffler if_printf(ifp, "unable to allocate channel table\n"); 24645591b213SSam Leffler return ENOMEM; 24655591b213SSam Leffler } 24665591b213SSam Leffler if (!ath_hal_init_channels(ah, chans, IEEE80211_CHAN_MAX, &nchan, 246745cabbdcSSam Leffler cc, HAL_MODE_ALL, outdoor)) { 24685591b213SSam Leffler if_printf(ifp, "unable to collect channel list from hal\n"); 24695591b213SSam Leffler free(chans, M_TEMP); 24705591b213SSam Leffler return EINVAL; 24715591b213SSam Leffler } 24725591b213SSam Leffler 24735591b213SSam Leffler /* 24745591b213SSam Leffler * Convert HAL channels to ieee80211 ones and insert 24755591b213SSam Leffler * them in the table according to their channel number. 24765591b213SSam Leffler */ 24775591b213SSam Leffler for (i = 0; i < nchan; i++) { 24785591b213SSam Leffler HAL_CHANNEL *c = &chans[i]; 24795591b213SSam Leffler ix = ath_hal_mhz2ieee(c->channel, c->channelFlags); 24805591b213SSam Leffler if (ix > IEEE80211_CHAN_MAX) { 24815591b213SSam Leffler if_printf(ifp, "bad hal channel %u (%u/%x) ignored\n", 24825591b213SSam Leffler ix, c->channel, c->channelFlags); 24835591b213SSam Leffler continue; 24845591b213SSam Leffler } 24855591b213SSam Leffler /* NB: flags are known to be compatible */ 24865591b213SSam Leffler if (ic->ic_channels[ix].ic_freq == 0) { 24875591b213SSam Leffler ic->ic_channels[ix].ic_freq = c->channel; 24885591b213SSam Leffler ic->ic_channels[ix].ic_flags = c->channelFlags; 24895591b213SSam Leffler } else { 24905591b213SSam Leffler /* channels overlap; e.g. 11g and 11b */ 24915591b213SSam Leffler ic->ic_channels[ix].ic_flags |= c->channelFlags; 24925591b213SSam Leffler } 24935591b213SSam Leffler } 24945591b213SSam Leffler free(chans, M_TEMP); 24955591b213SSam Leffler return 0; 24965591b213SSam Leffler } 24975591b213SSam Leffler 24985591b213SSam Leffler static int 24995591b213SSam Leffler ath_rate_setup(struct ath_softc *sc, u_int mode) 25005591b213SSam Leffler { 25015591b213SSam Leffler struct ath_hal *ah = sc->sc_ah; 25025591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 25035591b213SSam Leffler const HAL_RATE_TABLE *rt; 25045591b213SSam Leffler struct ieee80211_rateset *rs; 25055591b213SSam Leffler int i, maxrates; 25065591b213SSam Leffler 25075591b213SSam Leffler switch (mode) { 25085591b213SSam Leffler case IEEE80211_MODE_11A: 25095591b213SSam Leffler sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11A); 25105591b213SSam Leffler break; 25115591b213SSam Leffler case IEEE80211_MODE_11B: 25125591b213SSam Leffler sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11B); 25135591b213SSam Leffler break; 25145591b213SSam Leffler case IEEE80211_MODE_11G: 25155591b213SSam Leffler sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11G); 25165591b213SSam Leffler break; 25175591b213SSam Leffler case IEEE80211_MODE_TURBO: 25185591b213SSam Leffler sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_TURBO); 25195591b213SSam Leffler break; 25205591b213SSam Leffler default: 25215591b213SSam Leffler DPRINTF(("%s: invalid mode %u\n", __func__, mode)); 25225591b213SSam Leffler return 0; 25235591b213SSam Leffler } 25245591b213SSam Leffler rt = sc->sc_rates[mode]; 25255591b213SSam Leffler if (rt == NULL) 25265591b213SSam Leffler return 0; 25275591b213SSam Leffler if (rt->rateCount > IEEE80211_RATE_MAXSIZE) { 25285591b213SSam Leffler DPRINTF(("%s: rate table too small (%u > %u)\n", 25295591b213SSam Leffler __func__, rt->rateCount, IEEE80211_RATE_MAXSIZE)); 25305591b213SSam Leffler maxrates = IEEE80211_RATE_MAXSIZE; 25315591b213SSam Leffler } else 25325591b213SSam Leffler maxrates = rt->rateCount; 25335591b213SSam Leffler rs = &ic->ic_sup_rates[mode]; 25345591b213SSam Leffler for (i = 0; i < maxrates; i++) 25355591b213SSam Leffler rs->rs_rates[i] = rt->info[i].dot11Rate; 25365591b213SSam Leffler rs->rs_nrates = maxrates; 25375591b213SSam Leffler return 1; 25385591b213SSam Leffler } 25395591b213SSam Leffler 25405591b213SSam Leffler static void 25415591b213SSam Leffler ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode) 25425591b213SSam Leffler { 25435591b213SSam Leffler const HAL_RATE_TABLE *rt; 25445591b213SSam Leffler int i; 25455591b213SSam Leffler 25465591b213SSam Leffler memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap)); 25475591b213SSam Leffler rt = sc->sc_rates[mode]; 25485591b213SSam Leffler KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode)); 25495591b213SSam Leffler for (i = 0; i < rt->rateCount; i++) 25505591b213SSam Leffler sc->sc_rixmap[rt->info[i].dot11Rate & IEEE80211_RATE_VAL] = i; 25511b1a8e41SSam Leffler memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap)); 25521b1a8e41SSam Leffler for (i = 0; i < 32; i++) 25531b1a8e41SSam Leffler sc->sc_hwmap[i] = rt->info[rt->rateCodeToIndex[i]].dot11Rate; 25545591b213SSam Leffler sc->sc_currates = rt; 25555591b213SSam Leffler sc->sc_curmode = mode; 25565591b213SSam Leffler } 25575591b213SSam Leffler 25585591b213SSam Leffler /* 25595591b213SSam Leffler * Reset the rate control state for each 802.11 state transition. 25605591b213SSam Leffler */ 25615591b213SSam Leffler static void 25625591b213SSam Leffler ath_rate_ctl_reset(struct ath_softc *sc, enum ieee80211_state state) 25635591b213SSam Leffler { 25645591b213SSam Leffler struct ieee80211com *ic = &sc->sc_ic; 25655591b213SSam Leffler struct ieee80211_node *ni; 25665591b213SSam Leffler struct ath_node *an; 25675591b213SSam Leffler 25685591b213SSam Leffler an = (struct ath_node *) ic->ic_bss; 25695591b213SSam Leffler an->an_tx_ok = an->an_tx_err = an->an_tx_retr = an->an_tx_upper = 0; 25705591b213SSam Leffler if (ic->ic_opmode == IEEE80211_M_STA) { 25715591b213SSam Leffler ni = ic->ic_bss; 25725591b213SSam Leffler if (state == IEEE80211_S_RUN) { 25735591b213SSam Leffler /* start with highest negotiated rate */ 25745591b213SSam Leffler KASSERT(ni->ni_rates.rs_nrates > 0, 25755591b213SSam Leffler ("transition to RUN state w/ no rates!")); 25765591b213SSam Leffler ni->ni_txrate = ni->ni_rates.rs_nrates - 1; 25775591b213SSam Leffler } else { 25785591b213SSam Leffler /* use lowest rate */ 25795591b213SSam Leffler ni->ni_txrate = 0; 25805591b213SSam Leffler } 25815591b213SSam Leffler } else { 25825591b213SSam Leffler TAILQ_FOREACH(ni, &ic->ic_node, ni_list) { 25835591b213SSam Leffler ni->ni_txrate = 0; /* use lowest rate */ 25845591b213SSam Leffler an = (struct ath_node *) ni; 25855591b213SSam Leffler an->an_tx_ok = an->an_tx_err = an->an_tx_retr = 25865591b213SSam Leffler an->an_tx_upper = 0; 25875591b213SSam Leffler } 25885591b213SSam Leffler } 25895591b213SSam Leffler } 25905591b213SSam Leffler 25915591b213SSam Leffler /* 25925591b213SSam Leffler * Examine and potentially adjust the transmit rate. 25935591b213SSam Leffler */ 25945591b213SSam Leffler static void 25955591b213SSam Leffler ath_rate_ctl(void *arg, struct ieee80211_node *ni) 25965591b213SSam Leffler { 25975591b213SSam Leffler struct ath_softc *sc = arg; 25985591b213SSam Leffler struct ath_node *an = (struct ath_node *) ni; 25995591b213SSam Leffler struct ieee80211_rateset *rs = &ni->ni_rates; 26005591b213SSam Leffler int mod = 0, orate, enough; 26015591b213SSam Leffler 26025591b213SSam Leffler /* 26035591b213SSam Leffler * Rate control 26045591b213SSam Leffler * XXX: very primitive version. 26055591b213SSam Leffler */ 26065591b213SSam Leffler sc->sc_stats.ast_rate_calls++; 26075591b213SSam Leffler 26085591b213SSam Leffler enough = (an->an_tx_ok + an->an_tx_err >= 10); 26095591b213SSam Leffler 26105591b213SSam Leffler /* no packet reached -> down */ 26115591b213SSam Leffler if (an->an_tx_err > 0 && an->an_tx_ok == 0) 26125591b213SSam Leffler mod = -1; 26135591b213SSam Leffler 26145591b213SSam Leffler /* all packets needs retry in average -> down */ 26155591b213SSam Leffler if (enough && an->an_tx_ok < an->an_tx_retr) 26165591b213SSam Leffler mod = -1; 26175591b213SSam Leffler 26185591b213SSam Leffler /* no error and less than 10% of packets needs retry -> up */ 26195591b213SSam Leffler if (enough && an->an_tx_err == 0 && an->an_tx_ok > an->an_tx_retr * 10) 26205591b213SSam Leffler mod = 1; 26215591b213SSam Leffler 26225591b213SSam Leffler orate = ni->ni_txrate; 26235591b213SSam Leffler switch (mod) { 26245591b213SSam Leffler case 0: 26255591b213SSam Leffler if (enough && an->an_tx_upper > 0) 26265591b213SSam Leffler an->an_tx_upper--; 26275591b213SSam Leffler break; 26285591b213SSam Leffler case -1: 26295591b213SSam Leffler if (ni->ni_txrate > 0) { 26305591b213SSam Leffler ni->ni_txrate--; 26315591b213SSam Leffler sc->sc_stats.ast_rate_drop++; 26325591b213SSam Leffler } 26335591b213SSam Leffler an->an_tx_upper = 0; 26345591b213SSam Leffler break; 26355591b213SSam Leffler case 1: 26365591b213SSam Leffler if (++an->an_tx_upper < 2) 26375591b213SSam Leffler break; 26385591b213SSam Leffler an->an_tx_upper = 0; 26395591b213SSam Leffler if (ni->ni_txrate + 1 < rs->rs_nrates) { 26405591b213SSam Leffler ni->ni_txrate++; 26415591b213SSam Leffler sc->sc_stats.ast_rate_raise++; 26425591b213SSam Leffler } 26435591b213SSam Leffler break; 26445591b213SSam Leffler } 26455591b213SSam Leffler 26465591b213SSam Leffler if (ni->ni_txrate != orate) { 26475591b213SSam Leffler printf("%s: %dM -> %dM (%d ok, %d err, %d retr)\n", 26485591b213SSam Leffler __func__, 26495591b213SSam Leffler (rs->rs_rates[orate] & IEEE80211_RATE_VAL) / 2, 26505591b213SSam Leffler (rs->rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL) / 2, 26515591b213SSam Leffler an->an_tx_ok, an->an_tx_err, an->an_tx_retr); 26525591b213SSam Leffler } 26535591b213SSam Leffler if (ni->ni_txrate != orate || enough) 26545591b213SSam Leffler an->an_tx_ok = an->an_tx_err = an->an_tx_retr = 0; 26555591b213SSam Leffler } 26565591b213SSam Leffler 26575591b213SSam Leffler #ifdef AR_DEBUG 26585591b213SSam Leffler static int 26595591b213SSam Leffler sysctl_hw_ath_dump(SYSCTL_HANDLER_ARGS) 26605591b213SSam Leffler { 26615591b213SSam Leffler char dmode[64]; 26625591b213SSam Leffler int error; 26635591b213SSam Leffler 26645591b213SSam Leffler strncpy(dmode, "", sizeof(dmode) - 1); 26655591b213SSam Leffler dmode[sizeof(dmode) - 1] = '\0'; 26665591b213SSam Leffler error = sysctl_handle_string(oidp, &dmode[0], sizeof(dmode), req); 26675591b213SSam Leffler 26685591b213SSam Leffler if (error == 0 && req->newptr != NULL) { 26695591b213SSam Leffler struct ifnet *ifp; 26705591b213SSam Leffler struct ath_softc *sc; 26715591b213SSam Leffler 26725591b213SSam Leffler ifp = ifunit("ath0"); /* XXX */ 26735591b213SSam Leffler if (!ifp) 26745591b213SSam Leffler return EINVAL; 26755591b213SSam Leffler sc = ifp->if_softc; 26765591b213SSam Leffler if (strcmp(dmode, "hal") == 0) 26775591b213SSam Leffler ath_hal_dumpstate(sc->sc_ah); 26785591b213SSam Leffler else if (strcmp(dmode, "eeprom") == 0) 26795591b213SSam Leffler ath_hal_dumpeeprom(sc->sc_ah); 26805591b213SSam Leffler else if (strcmp(dmode, "rfgain") == 0) 26815591b213SSam Leffler ath_hal_dumprfgain(sc->sc_ah); 26825591b213SSam Leffler else if (strcmp(dmode, "ani") == 0) 26835591b213SSam Leffler ath_hal_dumpani(sc->sc_ah); 26845591b213SSam Leffler else 26855591b213SSam Leffler return EINVAL; 26865591b213SSam Leffler } 26875591b213SSam Leffler return error; 26885591b213SSam Leffler } 26895591b213SSam Leffler SYSCTL_PROC(_hw_ath, OID_AUTO, dump, CTLTYPE_STRING | CTLFLAG_RW, 26905591b213SSam Leffler 0, 0, sysctl_hw_ath_dump, "A", "Dump driver state"); 26915591b213SSam Leffler 26925591b213SSam Leffler static void 26935591b213SSam Leffler ath_printrxbuf(struct ath_buf *bf, int done) 26945591b213SSam Leffler { 26955591b213SSam Leffler struct ath_desc *ds; 26965591b213SSam Leffler int i; 26975591b213SSam Leffler 26985591b213SSam Leffler for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) { 26995591b213SSam Leffler printf("R%d (%p %p) %08x %08x %08x %08x %08x %08x %c\n", 27005591b213SSam Leffler i, ds, (struct ath_desc *)bf->bf_daddr + i, 27015591b213SSam Leffler ds->ds_link, ds->ds_data, 27025591b213SSam Leffler ds->ds_ctl0, ds->ds_ctl1, 27035591b213SSam Leffler ds->ds_hw[0], ds->ds_hw[1], 27045591b213SSam Leffler !done ? ' ' : (ds->ds_rxstat.rs_status == 0) ? '*' : '!'); 27055591b213SSam Leffler } 27065591b213SSam Leffler } 27075591b213SSam Leffler 27085591b213SSam Leffler static void 27095591b213SSam Leffler ath_printtxbuf(struct ath_buf *bf, int done) 27105591b213SSam Leffler { 27115591b213SSam Leffler struct ath_desc *ds; 27125591b213SSam Leffler int i; 27135591b213SSam Leffler 27145591b213SSam Leffler for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) { 27155591b213SSam Leffler printf("T%d (%p %p) %08x %08x %08x %08x %08x %08x %08x %08x %c\n", 27165591b213SSam Leffler i, ds, (struct ath_desc *)bf->bf_daddr + i, 27175591b213SSam Leffler ds->ds_link, ds->ds_data, 27185591b213SSam Leffler ds->ds_ctl0, ds->ds_ctl1, 27195591b213SSam Leffler ds->ds_hw[0], ds->ds_hw[1], ds->ds_hw[2], ds->ds_hw[3], 27205591b213SSam Leffler !done ? ' ' : (ds->ds_txstat.ts_status == 0) ? '*' : '!'); 27215591b213SSam Leffler } 27225591b213SSam Leffler } 27235591b213SSam Leffler #endif /* AR_DEBUG */ 2724