1 /*- 2 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer, 10 * without modification. 11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 13 * redistribution must be conditioned upon including a substantially 14 * similar Disclaimer requirement for further binary redistribution. 15 * 16 * NO WARRANTY 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27 * THE POSSIBILITY OF SUCH DAMAGES. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 /* 34 * Driver for the Atheros Wireless LAN controller. 35 * 36 * This software is derived from work of Atsushi Onoe; his contribution 37 * is greatly appreciated. 38 */ 39 40 #include "opt_inet.h" 41 #include "opt_ath.h" 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/sysctl.h> 46 #include <sys/mbuf.h> 47 #include <sys/malloc.h> 48 #include <sys/lock.h> 49 #include <sys/mutex.h> 50 #include <sys/kernel.h> 51 #include <sys/socket.h> 52 #include <sys/sockio.h> 53 #include <sys/errno.h> 54 #include <sys/callout.h> 55 #include <sys/bus.h> 56 #include <sys/endian.h> 57 #include <sys/kthread.h> 58 #include <sys/taskqueue.h> 59 #include <sys/priv.h> 60 61 #include <machine/bus.h> 62 63 #include <net/if.h> 64 #include <net/if_dl.h> 65 #include <net/if_media.h> 66 #include <net/if_types.h> 67 #include <net/if_arp.h> 68 #include <net/ethernet.h> 69 #include <net/if_llc.h> 70 71 #include <net80211/ieee80211_var.h> 72 #include <net80211/ieee80211_regdomain.h> 73 #ifdef ATH_SUPPORT_TDMA 74 #include <net80211/ieee80211_tdma.h> 75 #endif 76 77 #include <net/bpf.h> 78 79 #ifdef INET 80 #include <netinet/in.h> 81 #include <netinet/if_ether.h> 82 #endif 83 84 #include <dev/ath/if_athvar.h> 85 #include <dev/ath/ath_hal/ah_devid.h> /* XXX for softled */ 86 87 #ifdef ATH_TX99_DIAG 88 #include <dev/ath/ath_tx99/ath_tx99.h> 89 #endif 90 91 /* 92 * ATH_BCBUF determines the number of vap's that can transmit 93 * beacons and also (currently) the number of vap's that can 94 * have unique mac addresses/bssid. When staggering beacons 95 * 4 is probably a good max as otherwise the beacons become 96 * very closely spaced and there is limited time for cab q traffic 97 * to go out. You can burst beacons instead but that is not good 98 * for stations in power save and at some point you really want 99 * another radio (and channel). 100 * 101 * The limit on the number of mac addresses is tied to our use of 102 * the U/L bit and tracking addresses in a byte; it would be 103 * worthwhile to allow more for applications like proxy sta. 104 */ 105 CTASSERT(ATH_BCBUF <= 8); 106 107 /* unaligned little endian access */ 108 #define LE_READ_2(p) \ 109 ((u_int16_t) \ 110 ((((u_int8_t *)(p))[0] ) | (((u_int8_t *)(p))[1] << 8))) 111 #define LE_READ_4(p) \ 112 ((u_int32_t) \ 113 ((((u_int8_t *)(p))[0] ) | (((u_int8_t *)(p))[1] << 8) | \ 114 (((u_int8_t *)(p))[2] << 16) | (((u_int8_t *)(p))[3] << 24))) 115 116 static struct ieee80211vap *ath_vap_create(struct ieee80211com *, 117 const char name[IFNAMSIZ], int unit, int opmode, 118 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN], 119 const uint8_t mac[IEEE80211_ADDR_LEN]); 120 static void ath_vap_delete(struct ieee80211vap *); 121 static void ath_init(void *); 122 static void ath_stop_locked(struct ifnet *); 123 static void ath_stop(struct ifnet *); 124 static void ath_start(struct ifnet *); 125 static int ath_reset(struct ifnet *); 126 static int ath_reset_vap(struct ieee80211vap *, u_long); 127 static int ath_media_change(struct ifnet *); 128 static void ath_watchdog(void *); 129 static int ath_ioctl(struct ifnet *, u_long, caddr_t); 130 static void ath_fatal_proc(void *, int); 131 static void ath_bmiss_vap(struct ieee80211vap *); 132 static void ath_bmiss_proc(void *, int); 133 static int ath_keyset(struct ath_softc *, const struct ieee80211_key *, 134 struct ieee80211_node *); 135 static int ath_key_alloc(struct ieee80211vap *, 136 struct ieee80211_key *, 137 ieee80211_keyix *, ieee80211_keyix *); 138 static int ath_key_delete(struct ieee80211vap *, 139 const struct ieee80211_key *); 140 static int ath_key_set(struct ieee80211vap *, const struct ieee80211_key *, 141 const u_int8_t mac[IEEE80211_ADDR_LEN]); 142 static void ath_key_update_begin(struct ieee80211vap *); 143 static void ath_key_update_end(struct ieee80211vap *); 144 static void ath_update_mcast(struct ifnet *); 145 static void ath_update_promisc(struct ifnet *); 146 static void ath_mode_init(struct ath_softc *); 147 static void ath_setslottime(struct ath_softc *); 148 static void ath_updateslot(struct ifnet *); 149 static int ath_beaconq_setup(struct ath_hal *); 150 static int ath_beacon_alloc(struct ath_softc *, struct ieee80211_node *); 151 static void ath_beacon_update(struct ieee80211vap *, int item); 152 static void ath_beacon_setup(struct ath_softc *, struct ath_buf *); 153 static void ath_beacon_proc(void *, int); 154 static struct ath_buf *ath_beacon_generate(struct ath_softc *, 155 struct ieee80211vap *); 156 static void ath_bstuck_proc(void *, int); 157 static void ath_beacon_return(struct ath_softc *, struct ath_buf *); 158 static void ath_beacon_free(struct ath_softc *); 159 static void ath_beacon_config(struct ath_softc *, struct ieee80211vap *); 160 static void ath_descdma_cleanup(struct ath_softc *sc, 161 struct ath_descdma *, ath_bufhead *); 162 static int ath_desc_alloc(struct ath_softc *); 163 static void ath_desc_free(struct ath_softc *); 164 static struct ieee80211_node *ath_node_alloc(struct ieee80211vap *, 165 const uint8_t [IEEE80211_ADDR_LEN]); 166 static void ath_node_free(struct ieee80211_node *); 167 static void ath_node_getsignal(const struct ieee80211_node *, 168 int8_t *, int8_t *); 169 static int ath_rxbuf_init(struct ath_softc *, struct ath_buf *); 170 static void ath_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m, 171 int subtype, int rssi, int noise, u_int32_t rstamp); 172 static void ath_setdefantenna(struct ath_softc *, u_int); 173 static void ath_rx_proc(void *, int); 174 static void ath_txq_init(struct ath_softc *sc, struct ath_txq *, int); 175 static struct ath_txq *ath_txq_setup(struct ath_softc*, int qtype, int subtype); 176 static int ath_tx_setup(struct ath_softc *, int, int); 177 static int ath_wme_update(struct ieee80211com *); 178 static void ath_tx_cleanupq(struct ath_softc *, struct ath_txq *); 179 static void ath_tx_cleanup(struct ath_softc *); 180 static void ath_freetx(struct mbuf *); 181 static int ath_tx_start(struct ath_softc *, struct ieee80211_node *, 182 struct ath_buf *, struct mbuf *); 183 static void ath_tx_proc_q0(void *, int); 184 static void ath_tx_proc_q0123(void *, int); 185 static void ath_tx_proc(void *, int); 186 static void ath_tx_draintxq(struct ath_softc *, struct ath_txq *); 187 static int ath_chan_set(struct ath_softc *, struct ieee80211_channel *); 188 static void ath_draintxq(struct ath_softc *); 189 static void ath_stoprecv(struct ath_softc *); 190 static int ath_startrecv(struct ath_softc *); 191 static void ath_chan_change(struct ath_softc *, struct ieee80211_channel *); 192 static void ath_scan_start(struct ieee80211com *); 193 static void ath_scan_end(struct ieee80211com *); 194 static void ath_set_channel(struct ieee80211com *); 195 static void ath_calibrate(void *); 196 static int ath_newstate(struct ieee80211vap *, enum ieee80211_state, int); 197 static void ath_setup_stationkey(struct ieee80211_node *); 198 static void ath_newassoc(struct ieee80211_node *, int); 199 static int ath_setregdomain(struct ieee80211com *, 200 struct ieee80211_regdomain *, int, 201 struct ieee80211_channel []); 202 static void ath_getradiocaps(struct ieee80211com *, int, int *, 203 struct ieee80211_channel []); 204 static int ath_getchannels(struct ath_softc *); 205 static void ath_led_event(struct ath_softc *, int); 206 207 static int ath_rate_setup(struct ath_softc *, u_int mode); 208 static void ath_setcurmode(struct ath_softc *, enum ieee80211_phymode); 209 210 static void ath_sysctlattach(struct ath_softc *); 211 static int ath_raw_xmit(struct ieee80211_node *, 212 struct mbuf *, const struct ieee80211_bpf_params *); 213 static void ath_bpfattach(struct ath_softc *); 214 static void ath_announce(struct ath_softc *); 215 216 #ifdef ATH_SUPPORT_TDMA 217 static void ath_tdma_settimers(struct ath_softc *sc, u_int32_t nexttbtt, 218 u_int32_t bintval); 219 static void ath_tdma_bintvalsetup(struct ath_softc *sc, 220 const struct ieee80211_tdma_state *tdma); 221 static void ath_tdma_config(struct ath_softc *sc, struct ieee80211vap *vap); 222 static void ath_tdma_update(struct ieee80211_node *ni, 223 const struct ieee80211_tdma_param *tdma, int); 224 static void ath_tdma_beacon_send(struct ath_softc *sc, 225 struct ieee80211vap *vap); 226 227 static __inline void 228 ath_hal_setcca(struct ath_hal *ah, int ena) 229 { 230 /* 231 * NB: fill me in; this is not provided by default because disabling 232 * CCA in most locales violates regulatory. 233 */ 234 } 235 236 static __inline int 237 ath_hal_getcca(struct ath_hal *ah) 238 { 239 u_int32_t diag; 240 if (ath_hal_getcapability(ah, HAL_CAP_DIAG, 0, &diag) != HAL_OK) 241 return 1; 242 return ((diag & 0x500000) == 0); 243 } 244 245 #define TDMA_EP_MULTIPLIER (1<<10) /* pow2 to optimize out * and / */ 246 #define TDMA_LPF_LEN 6 247 #define TDMA_DUMMY_MARKER 0x127 248 #define TDMA_EP_MUL(x, mul) ((x) * (mul)) 249 #define TDMA_IN(x) (TDMA_EP_MUL((x), TDMA_EP_MULTIPLIER)) 250 #define TDMA_LPF(x, y, len) \ 251 ((x != TDMA_DUMMY_MARKER) ? (((x) * ((len)-1) + (y)) / (len)) : (y)) 252 #define TDMA_SAMPLE(x, y) do { \ 253 x = TDMA_LPF((x), TDMA_IN(y), TDMA_LPF_LEN); \ 254 } while (0) 255 #define TDMA_EP_RND(x,mul) \ 256 ((((x)%(mul)) >= ((mul)/2)) ? ((x) + ((mul) - 1)) / (mul) : (x)/(mul)) 257 #define TDMA_AVG(x) TDMA_EP_RND(x, TDMA_EP_MULTIPLIER) 258 #endif /* ATH_SUPPORT_TDMA */ 259 260 SYSCTL_DECL(_hw_ath); 261 262 /* XXX validate sysctl values */ 263 static int ath_longcalinterval = 30; /* long cals every 30 secs */ 264 SYSCTL_INT(_hw_ath, OID_AUTO, longcal, CTLFLAG_RW, &ath_longcalinterval, 265 0, "long chip calibration interval (secs)"); 266 static int ath_shortcalinterval = 100; /* short cals every 100 ms */ 267 SYSCTL_INT(_hw_ath, OID_AUTO, shortcal, CTLFLAG_RW, &ath_shortcalinterval, 268 0, "short chip calibration interval (msecs)"); 269 static int ath_resetcalinterval = 20*60; /* reset cal state 20 mins */ 270 SYSCTL_INT(_hw_ath, OID_AUTO, resetcal, CTLFLAG_RW, &ath_resetcalinterval, 271 0, "reset chip calibration results (secs)"); 272 273 static int ath_rxbuf = ATH_RXBUF; /* # rx buffers to allocate */ 274 SYSCTL_INT(_hw_ath, OID_AUTO, rxbuf, CTLFLAG_RW, &ath_rxbuf, 275 0, "rx buffers allocated"); 276 TUNABLE_INT("hw.ath.rxbuf", &ath_rxbuf); 277 static int ath_txbuf = ATH_TXBUF; /* # tx buffers to allocate */ 278 SYSCTL_INT(_hw_ath, OID_AUTO, txbuf, CTLFLAG_RW, &ath_txbuf, 279 0, "tx buffers allocated"); 280 TUNABLE_INT("hw.ath.txbuf", &ath_txbuf); 281 282 static int ath_bstuck_threshold = 4; /* max missed beacons */ 283 SYSCTL_INT(_hw_ath, OID_AUTO, bstuck, CTLFLAG_RW, &ath_bstuck_threshold, 284 0, "max missed beacon xmits before chip reset"); 285 286 #ifdef ATH_DEBUG 287 enum { 288 ATH_DEBUG_XMIT = 0x00000001, /* basic xmit operation */ 289 ATH_DEBUG_XMIT_DESC = 0x00000002, /* xmit descriptors */ 290 ATH_DEBUG_RECV = 0x00000004, /* basic recv operation */ 291 ATH_DEBUG_RECV_DESC = 0x00000008, /* recv descriptors */ 292 ATH_DEBUG_RATE = 0x00000010, /* rate control */ 293 ATH_DEBUG_RESET = 0x00000020, /* reset processing */ 294 ATH_DEBUG_MODE = 0x00000040, /* mode init/setup */ 295 ATH_DEBUG_BEACON = 0x00000080, /* beacon handling */ 296 ATH_DEBUG_WATCHDOG = 0x00000100, /* watchdog timeout */ 297 ATH_DEBUG_INTR = 0x00001000, /* ISR */ 298 ATH_DEBUG_TX_PROC = 0x00002000, /* tx ISR proc */ 299 ATH_DEBUG_RX_PROC = 0x00004000, /* rx ISR proc */ 300 ATH_DEBUG_BEACON_PROC = 0x00008000, /* beacon ISR proc */ 301 ATH_DEBUG_CALIBRATE = 0x00010000, /* periodic calibration */ 302 ATH_DEBUG_KEYCACHE = 0x00020000, /* key cache management */ 303 ATH_DEBUG_STATE = 0x00040000, /* 802.11 state transitions */ 304 ATH_DEBUG_NODE = 0x00080000, /* node management */ 305 ATH_DEBUG_LED = 0x00100000, /* led management */ 306 ATH_DEBUG_FF = 0x00200000, /* fast frames */ 307 ATH_DEBUG_DFS = 0x00400000, /* DFS processing */ 308 ATH_DEBUG_TDMA = 0x00800000, /* TDMA processing */ 309 ATH_DEBUG_TDMA_TIMER = 0x01000000, /* TDMA timer processing */ 310 ATH_DEBUG_REGDOMAIN = 0x02000000, /* regulatory processing */ 311 ATH_DEBUG_FATAL = 0x80000000, /* fatal errors */ 312 ATH_DEBUG_ANY = 0xffffffff 313 }; 314 static int ath_debug = 0; 315 SYSCTL_INT(_hw_ath, OID_AUTO, debug, CTLFLAG_RW, &ath_debug, 316 0, "control debugging printfs"); 317 TUNABLE_INT("hw.ath.debug", &ath_debug); 318 319 #define IFF_DUMPPKTS(sc, m) \ 320 ((sc->sc_debug & (m)) || \ 321 (sc->sc_ifp->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2)) 322 #define DPRINTF(sc, m, fmt, ...) do { \ 323 if (sc->sc_debug & (m)) \ 324 printf(fmt, __VA_ARGS__); \ 325 } while (0) 326 #define KEYPRINTF(sc, ix, hk, mac) do { \ 327 if (sc->sc_debug & ATH_DEBUG_KEYCACHE) \ 328 ath_keyprint(sc, __func__, ix, hk, mac); \ 329 } while (0) 330 static void ath_printrxbuf(struct ath_softc *, const struct ath_buf *bf, 331 u_int ix, int); 332 static void ath_printtxbuf(struct ath_softc *, const struct ath_buf *bf, 333 u_int qnum, u_int ix, int done); 334 #else 335 #define IFF_DUMPPKTS(sc, m) \ 336 ((sc->sc_ifp->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2)) 337 #define DPRINTF(sc, m, fmt, ...) do { \ 338 (void) sc; \ 339 } while (0) 340 #define KEYPRINTF(sc, k, ix, mac) do { \ 341 (void) sc; \ 342 } while (0) 343 #endif 344 345 MALLOC_DEFINE(M_ATHDEV, "athdev", "ath driver dma buffers"); 346 347 int 348 ath_attach(u_int16_t devid, struct ath_softc *sc) 349 { 350 struct ifnet *ifp; 351 struct ieee80211com *ic; 352 struct ath_hal *ah = NULL; 353 HAL_STATUS status; 354 int error = 0, i; 355 u_int wmodes; 356 357 DPRINTF(sc, ATH_DEBUG_ANY, "%s: devid 0x%x\n", __func__, devid); 358 359 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211); 360 if (ifp == NULL) { 361 device_printf(sc->sc_dev, "can not if_alloc()\n"); 362 error = ENOSPC; 363 goto bad; 364 } 365 ic = ifp->if_l2com; 366 367 /* set these up early for if_printf use */ 368 if_initname(ifp, device_get_name(sc->sc_dev), 369 device_get_unit(sc->sc_dev)); 370 371 ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh, &status); 372 if (ah == NULL) { 373 if_printf(ifp, "unable to attach hardware; HAL status %u\n", 374 status); 375 error = ENXIO; 376 goto bad; 377 } 378 sc->sc_ah = ah; 379 sc->sc_invalid = 0; /* ready to go, enable interrupt handling */ 380 #ifdef ATH_DEBUG 381 sc->sc_debug = ath_debug; 382 #endif 383 384 /* 385 * Check if the MAC has multi-rate retry support. 386 * We do this by trying to setup a fake extended 387 * descriptor. MAC's that don't have support will 388 * return false w/o doing anything. MAC's that do 389 * support it will return true w/o doing anything. 390 */ 391 sc->sc_mrretry = ath_hal_setupxtxdesc(ah, NULL, 0,0, 0,0, 0,0); 392 393 /* 394 * Check if the device has hardware counters for PHY 395 * errors. If so we need to enable the MIB interrupt 396 * so we can act on stat triggers. 397 */ 398 if (ath_hal_hwphycounters(ah)) 399 sc->sc_needmib = 1; 400 401 /* 402 * Get the hardware key cache size. 403 */ 404 sc->sc_keymax = ath_hal_keycachesize(ah); 405 if (sc->sc_keymax > ATH_KEYMAX) { 406 if_printf(ifp, "Warning, using only %u of %u key cache slots\n", 407 ATH_KEYMAX, sc->sc_keymax); 408 sc->sc_keymax = ATH_KEYMAX; 409 } 410 /* 411 * Reset the key cache since some parts do not 412 * reset the contents on initial power up. 413 */ 414 for (i = 0; i < sc->sc_keymax; i++) 415 ath_hal_keyreset(ah, i); 416 417 /* 418 * Collect the default channel list. 419 */ 420 error = ath_getchannels(sc); 421 if (error != 0) 422 goto bad; 423 424 /* 425 * Setup rate tables for all potential media types. 426 */ 427 ath_rate_setup(sc, IEEE80211_MODE_11A); 428 ath_rate_setup(sc, IEEE80211_MODE_11B); 429 ath_rate_setup(sc, IEEE80211_MODE_11G); 430 ath_rate_setup(sc, IEEE80211_MODE_TURBO_A); 431 ath_rate_setup(sc, IEEE80211_MODE_TURBO_G); 432 ath_rate_setup(sc, IEEE80211_MODE_STURBO_A); 433 ath_rate_setup(sc, IEEE80211_MODE_11NA); 434 ath_rate_setup(sc, IEEE80211_MODE_11NG); 435 ath_rate_setup(sc, IEEE80211_MODE_HALF); 436 ath_rate_setup(sc, IEEE80211_MODE_QUARTER); 437 438 /* NB: setup here so ath_rate_update is happy */ 439 ath_setcurmode(sc, IEEE80211_MODE_11A); 440 441 /* 442 * Allocate tx+rx descriptors and populate the lists. 443 */ 444 error = ath_desc_alloc(sc); 445 if (error != 0) { 446 if_printf(ifp, "failed to allocate descriptors: %d\n", error); 447 goto bad; 448 } 449 callout_init_mtx(&sc->sc_cal_ch, &sc->sc_mtx, 0); 450 callout_init_mtx(&sc->sc_wd_ch, &sc->sc_mtx, 0); 451 452 ATH_TXBUF_LOCK_INIT(sc); 453 454 sc->sc_tq = taskqueue_create("ath_taskq", M_NOWAIT, 455 taskqueue_thread_enqueue, &sc->sc_tq); 456 taskqueue_start_threads(&sc->sc_tq, 1, PI_NET, 457 "%s taskq", ifp->if_xname); 458 459 TASK_INIT(&sc->sc_rxtask, 0, ath_rx_proc, sc); 460 TASK_INIT(&sc->sc_bmisstask, 0, ath_bmiss_proc, sc); 461 TASK_INIT(&sc->sc_bstucktask,0, ath_bstuck_proc, sc); 462 463 /* 464 * Allocate hardware transmit queues: one queue for 465 * beacon frames and one data queue for each QoS 466 * priority. Note that the hal handles reseting 467 * these queues at the needed time. 468 * 469 * XXX PS-Poll 470 */ 471 sc->sc_bhalq = ath_beaconq_setup(ah); 472 if (sc->sc_bhalq == (u_int) -1) { 473 if_printf(ifp, "unable to setup a beacon xmit queue!\n"); 474 error = EIO; 475 goto bad2; 476 } 477 sc->sc_cabq = ath_txq_setup(sc, HAL_TX_QUEUE_CAB, 0); 478 if (sc->sc_cabq == NULL) { 479 if_printf(ifp, "unable to setup CAB xmit queue!\n"); 480 error = EIO; 481 goto bad2; 482 } 483 /* NB: insure BK queue is the lowest priority h/w queue */ 484 if (!ath_tx_setup(sc, WME_AC_BK, HAL_WME_AC_BK)) { 485 if_printf(ifp, "unable to setup xmit queue for %s traffic!\n", 486 ieee80211_wme_acnames[WME_AC_BK]); 487 error = EIO; 488 goto bad2; 489 } 490 if (!ath_tx_setup(sc, WME_AC_BE, HAL_WME_AC_BE) || 491 !ath_tx_setup(sc, WME_AC_VI, HAL_WME_AC_VI) || 492 !ath_tx_setup(sc, WME_AC_VO, HAL_WME_AC_VO)) { 493 /* 494 * Not enough hardware tx queues to properly do WME; 495 * just punt and assign them all to the same h/w queue. 496 * We could do a better job of this if, for example, 497 * we allocate queues when we switch from station to 498 * AP mode. 499 */ 500 if (sc->sc_ac2q[WME_AC_VI] != NULL) 501 ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_VI]); 502 if (sc->sc_ac2q[WME_AC_BE] != NULL) 503 ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_BE]); 504 sc->sc_ac2q[WME_AC_BE] = sc->sc_ac2q[WME_AC_BK]; 505 sc->sc_ac2q[WME_AC_VI] = sc->sc_ac2q[WME_AC_BK]; 506 sc->sc_ac2q[WME_AC_VO] = sc->sc_ac2q[WME_AC_BK]; 507 } 508 509 /* 510 * Special case certain configurations. Note the 511 * CAB queue is handled by these specially so don't 512 * include them when checking the txq setup mask. 513 */ 514 switch (sc->sc_txqsetup &~ (1<<sc->sc_cabq->axq_qnum)) { 515 case 0x01: 516 TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0, sc); 517 break; 518 case 0x0f: 519 TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0123, sc); 520 break; 521 default: 522 TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc, sc); 523 break; 524 } 525 526 /* 527 * Setup rate control. Some rate control modules 528 * call back to change the anntena state so expose 529 * the necessary entry points. 530 * XXX maybe belongs in struct ath_ratectrl? 531 */ 532 sc->sc_setdefantenna = ath_setdefantenna; 533 sc->sc_rc = ath_rate_attach(sc); 534 if (sc->sc_rc == NULL) { 535 error = EIO; 536 goto bad2; 537 } 538 539 sc->sc_blinking = 0; 540 sc->sc_ledstate = 1; 541 sc->sc_ledon = 0; /* low true */ 542 sc->sc_ledidle = (2700*hz)/1000; /* 2.7sec */ 543 callout_init(&sc->sc_ledtimer, CALLOUT_MPSAFE); 544 /* 545 * Auto-enable soft led processing for IBM cards and for 546 * 5211 minipci cards. Users can also manually enable/disable 547 * support with a sysctl. 548 */ 549 sc->sc_softled = (devid == AR5212_DEVID_IBM || devid == AR5211_DEVID); 550 if (sc->sc_softled) { 551 ath_hal_gpioCfgOutput(ah, sc->sc_ledpin, 552 HAL_GPIO_MUX_MAC_NETWORK_LED); 553 ath_hal_gpioset(ah, sc->sc_ledpin, !sc->sc_ledon); 554 } 555 556 ifp->if_softc = sc; 557 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST; 558 ifp->if_start = ath_start; 559 ifp->if_watchdog = NULL; 560 ifp->if_ioctl = ath_ioctl; 561 ifp->if_init = ath_init; 562 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 563 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN; 564 IFQ_SET_READY(&ifp->if_snd); 565 566 ic->ic_ifp = ifp; 567 /* XXX not right but it's not used anywhere important */ 568 ic->ic_phytype = IEEE80211_T_OFDM; 569 ic->ic_opmode = IEEE80211_M_STA; 570 ic->ic_caps = 571 IEEE80211_C_STA /* station mode */ 572 | IEEE80211_C_IBSS /* ibss, nee adhoc, mode */ 573 | IEEE80211_C_HOSTAP /* hostap mode */ 574 | IEEE80211_C_MONITOR /* monitor mode */ 575 | IEEE80211_C_AHDEMO /* adhoc demo mode */ 576 | IEEE80211_C_WDS /* 4-address traffic works */ 577 | IEEE80211_C_SHPREAMBLE /* short preamble supported */ 578 | IEEE80211_C_SHSLOT /* short slot time supported */ 579 | IEEE80211_C_WPA /* capable of WPA1+WPA2 */ 580 | IEEE80211_C_BGSCAN /* capable of bg scanning */ 581 | IEEE80211_C_TXFRAG /* handle tx frags */ 582 ; 583 /* 584 * Query the hal to figure out h/w crypto support. 585 */ 586 if (ath_hal_ciphersupported(ah, HAL_CIPHER_WEP)) 587 ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP; 588 if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_OCB)) 589 ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_OCB; 590 if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_CCM)) 591 ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_CCM; 592 if (ath_hal_ciphersupported(ah, HAL_CIPHER_CKIP)) 593 ic->ic_cryptocaps |= IEEE80211_CRYPTO_CKIP; 594 if (ath_hal_ciphersupported(ah, HAL_CIPHER_TKIP)) { 595 ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIP; 596 /* 597 * Check if h/w does the MIC and/or whether the 598 * separate key cache entries are required to 599 * handle both tx+rx MIC keys. 600 */ 601 if (ath_hal_ciphersupported(ah, HAL_CIPHER_MIC)) 602 ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC; 603 /* 604 * If the h/w supports storing tx+rx MIC keys 605 * in one cache slot automatically enable use. 606 */ 607 if (ath_hal_hastkipsplit(ah) || 608 !ath_hal_settkipsplit(ah, AH_FALSE)) 609 sc->sc_splitmic = 1; 610 /* 611 * If the h/w can do TKIP MIC together with WME then 612 * we use it; otherwise we force the MIC to be done 613 * in software by the net80211 layer. 614 */ 615 if (ath_hal_haswmetkipmic(ah)) 616 sc->sc_wmetkipmic = 1; 617 } 618 sc->sc_hasclrkey = ath_hal_ciphersupported(ah, HAL_CIPHER_CLR); 619 sc->sc_mcastkey = ath_hal_getmcastkeysearch(ah); 620 /* 621 * Mark key cache slots associated with global keys 622 * as in use. If we knew TKIP was not to be used we 623 * could leave the +32, +64, and +32+64 slots free. 624 */ 625 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 626 setbit(sc->sc_keymap, i); 627 setbit(sc->sc_keymap, i+64); 628 if (sc->sc_splitmic) { 629 setbit(sc->sc_keymap, i+32); 630 setbit(sc->sc_keymap, i+32+64); 631 } 632 } 633 /* 634 * TPC support can be done either with a global cap or 635 * per-packet support. The latter is not available on 636 * all parts. We're a bit pedantic here as all parts 637 * support a global cap. 638 */ 639 if (ath_hal_hastpc(ah) || ath_hal_hastxpowlimit(ah)) 640 ic->ic_caps |= IEEE80211_C_TXPMGT; 641 642 /* 643 * Mark WME capability only if we have sufficient 644 * hardware queues to do proper priority scheduling. 645 */ 646 if (sc->sc_ac2q[WME_AC_BE] != sc->sc_ac2q[WME_AC_BK]) 647 ic->ic_caps |= IEEE80211_C_WME; 648 /* 649 * Check for misc other capabilities. 650 */ 651 if (ath_hal_hasbursting(ah)) 652 ic->ic_caps |= IEEE80211_C_BURST; 653 sc->sc_hasbmask = ath_hal_hasbssidmask(ah); 654 sc->sc_hastsfadd = ath_hal_hastsfadjust(ah); 655 if (ath_hal_hasfastframes(ah)) 656 ic->ic_caps |= IEEE80211_C_FF; 657 wmodes = ath_hal_getwirelessmodes(ah); 658 if (wmodes & (HAL_MODE_108G|HAL_MODE_TURBO)) 659 ic->ic_caps |= IEEE80211_C_TURBOP; 660 #ifdef ATH_SUPPORT_TDMA 661 if (ath_hal_macversion(ah) > 0x78) { 662 ic->ic_caps |= IEEE80211_C_TDMA; /* capable of TDMA */ 663 ic->ic_tdma_update = ath_tdma_update; 664 } 665 #endif 666 /* 667 * Indicate we need the 802.11 header padded to a 668 * 32-bit boundary for 4-address and QoS frames. 669 */ 670 ic->ic_flags |= IEEE80211_F_DATAPAD; 671 672 /* 673 * Query the hal about antenna support. 674 */ 675 sc->sc_defant = ath_hal_getdefantenna(ah); 676 677 /* 678 * Not all chips have the VEOL support we want to 679 * use with IBSS beacons; check here for it. 680 */ 681 sc->sc_hasveol = ath_hal_hasveol(ah); 682 683 /* get mac address from hardware */ 684 ath_hal_getmac(ah, ic->ic_myaddr); 685 if (sc->sc_hasbmask) 686 ath_hal_getbssidmask(ah, sc->sc_hwbssidmask); 687 688 /* NB: used to size node table key mapping array */ 689 ic->ic_max_keyix = sc->sc_keymax; 690 /* call MI attach routine. */ 691 ieee80211_ifattach(ic); 692 ic->ic_setregdomain = ath_setregdomain; 693 ic->ic_getradiocaps = ath_getradiocaps; 694 sc->sc_opmode = HAL_M_STA; 695 696 /* override default methods */ 697 ic->ic_newassoc = ath_newassoc; 698 ic->ic_updateslot = ath_updateslot; 699 ic->ic_wme.wme_update = ath_wme_update; 700 ic->ic_vap_create = ath_vap_create; 701 ic->ic_vap_delete = ath_vap_delete; 702 ic->ic_raw_xmit = ath_raw_xmit; 703 ic->ic_update_mcast = ath_update_mcast; 704 ic->ic_update_promisc = ath_update_promisc; 705 ic->ic_node_alloc = ath_node_alloc; 706 sc->sc_node_free = ic->ic_node_free; 707 ic->ic_node_free = ath_node_free; 708 ic->ic_node_getsignal = ath_node_getsignal; 709 ic->ic_scan_start = ath_scan_start; 710 ic->ic_scan_end = ath_scan_end; 711 ic->ic_set_channel = ath_set_channel; 712 713 ath_bpfattach(sc); 714 /* 715 * Setup dynamic sysctl's now that country code and 716 * regdomain are available from the hal. 717 */ 718 ath_sysctlattach(sc); 719 720 if (bootverbose) 721 ieee80211_announce(ic); 722 ath_announce(sc); 723 return 0; 724 bad2: 725 ath_tx_cleanup(sc); 726 ath_desc_free(sc); 727 bad: 728 if (ah) 729 ath_hal_detach(ah); 730 if (ifp != NULL) 731 if_free(ifp); 732 sc->sc_invalid = 1; 733 return error; 734 } 735 736 int 737 ath_detach(struct ath_softc *sc) 738 { 739 struct ifnet *ifp = sc->sc_ifp; 740 741 DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n", 742 __func__, ifp->if_flags); 743 744 /* 745 * NB: the order of these is important: 746 * o stop the chip so no more interrupts will fire 747 * o call the 802.11 layer before detaching the hal to 748 * insure callbacks into the driver to delete global 749 * key cache entries can be handled 750 * o free the taskqueue which drains any pending tasks 751 * o reclaim the bpf tap now that we know nothing will use 752 * it (e.g. rx processing from the task q thread) 753 * o reclaim the tx queue data structures after calling 754 * the 802.11 layer as we'll get called back to reclaim 755 * node state and potentially want to use them 756 * o to cleanup the tx queues the hal is called, so detach 757 * it last 758 * Other than that, it's straightforward... 759 */ 760 ath_stop(ifp); 761 ieee80211_ifdetach(ifp->if_l2com); 762 taskqueue_free(sc->sc_tq); 763 bpfdetach(ifp); 764 #ifdef ATH_TX99_DIAG 765 if (sc->sc_tx99 != NULL) 766 sc->sc_tx99->detach(sc->sc_tx99); 767 #endif 768 ath_rate_detach(sc->sc_rc); 769 ath_desc_free(sc); 770 ath_tx_cleanup(sc); 771 ath_hal_detach(sc->sc_ah); /* NB: sets chip in full sleep */ 772 if_free(ifp); 773 774 return 0; 775 } 776 777 /* 778 * MAC address handling for multiple BSS on the same radio. 779 * The first vap uses the MAC address from the EEPROM. For 780 * subsequent vap's we set the U/L bit (bit 1) in the MAC 781 * address and use the next six bits as an index. 782 */ 783 static void 784 assign_address(struct ath_softc *sc, uint8_t mac[IEEE80211_ADDR_LEN], int clone) 785 { 786 int i; 787 788 if (clone && sc->sc_hasbmask) { 789 /* NB: we only do this if h/w supports multiple bssid */ 790 for (i = 0; i < 8; i++) 791 if ((sc->sc_bssidmask & (1<<i)) == 0) 792 break; 793 if (i != 0) 794 mac[0] |= (i << 2)|0x2; 795 } else 796 i = 0; 797 sc->sc_bssidmask |= 1<<i; 798 sc->sc_hwbssidmask[0] &= ~mac[0]; 799 if (i == 0) 800 sc->sc_nbssid0++; 801 } 802 803 static void 804 reclaim_address(struct ath_softc *sc, const uint8_t mac[IEEE80211_ADDR_LEN]) 805 { 806 int i = mac[0] >> 2; 807 uint8_t mask; 808 809 if (i != 0 || --sc->sc_nbssid0 == 0) { 810 sc->sc_bssidmask &= ~(1<<i); 811 /* recalculate bssid mask from remaining addresses */ 812 mask = 0xff; 813 for (i = 1; i < 8; i++) 814 if (sc->sc_bssidmask & (1<<i)) 815 mask &= ~((i<<2)|0x2); 816 sc->sc_hwbssidmask[0] |= mask; 817 } 818 } 819 820 /* 821 * Assign a beacon xmit slot. We try to space out 822 * assignments so when beacons are staggered the 823 * traffic coming out of the cab q has maximal time 824 * to go out before the next beacon is scheduled. 825 */ 826 static int 827 assign_bslot(struct ath_softc *sc) 828 { 829 u_int slot, free; 830 831 free = 0; 832 for (slot = 0; slot < ATH_BCBUF; slot++) 833 if (sc->sc_bslot[slot] == NULL) { 834 if (sc->sc_bslot[(slot+1)%ATH_BCBUF] == NULL && 835 sc->sc_bslot[(slot-1)%ATH_BCBUF] == NULL) 836 return slot; 837 free = slot; 838 /* NB: keep looking for a double slot */ 839 } 840 return free; 841 } 842 843 static struct ieee80211vap * 844 ath_vap_create(struct ieee80211com *ic, 845 const char name[IFNAMSIZ], int unit, int opmode, int flags, 846 const uint8_t bssid[IEEE80211_ADDR_LEN], 847 const uint8_t mac0[IEEE80211_ADDR_LEN]) 848 { 849 struct ath_softc *sc = ic->ic_ifp->if_softc; 850 struct ath_vap *avp; 851 struct ieee80211vap *vap; 852 uint8_t mac[IEEE80211_ADDR_LEN]; 853 int ic_opmode, needbeacon, error; 854 855 avp = (struct ath_vap *) malloc(sizeof(struct ath_vap), 856 M_80211_VAP, M_WAITOK | M_ZERO); 857 needbeacon = 0; 858 IEEE80211_ADDR_COPY(mac, mac0); 859 860 ATH_LOCK(sc); 861 switch (opmode) { 862 case IEEE80211_M_STA: 863 if (sc->sc_nstavaps != 0) { /* XXX only 1 sta for now */ 864 device_printf(sc->sc_dev, "only 1 sta vap supported\n"); 865 goto bad; 866 } 867 if (sc->sc_nvaps) { 868 /* 869 * When there are multiple vaps we must fall 870 * back to s/w beacon miss handling. 871 */ 872 flags |= IEEE80211_CLONE_NOBEACONS; 873 } 874 if (flags & IEEE80211_CLONE_NOBEACONS) 875 ic_opmode = IEEE80211_M_HOSTAP; 876 else 877 ic_opmode = opmode; 878 break; 879 case IEEE80211_M_IBSS: 880 if (sc->sc_nvaps != 0) { /* XXX only 1 for now */ 881 device_printf(sc->sc_dev, 882 "only 1 ibss vap supported\n"); 883 goto bad; 884 } 885 ic_opmode = opmode; 886 needbeacon = 1; 887 break; 888 case IEEE80211_M_AHDEMO: 889 #ifdef ATH_SUPPORT_TDMA 890 if (flags & IEEE80211_CLONE_TDMA) { 891 needbeacon = 1; 892 flags |= IEEE80211_CLONE_NOBEACONS; 893 } 894 /* fall thru... */ 895 #endif 896 case IEEE80211_M_MONITOR: 897 if (sc->sc_nvaps != 0 && ic->ic_opmode != opmode) { 898 /* XXX not right for monitor mode */ 899 ic_opmode = ic->ic_opmode; 900 } else 901 ic_opmode = opmode; 902 break; 903 case IEEE80211_M_HOSTAP: 904 needbeacon = 1; 905 /* fall thru... */ 906 case IEEE80211_M_WDS: 907 if (sc->sc_nvaps && ic->ic_opmode == IEEE80211_M_STA) { 908 device_printf(sc->sc_dev, 909 "wds not supported in sta mode\n"); 910 goto bad; 911 } 912 if (opmode == IEEE80211_M_WDS) { 913 /* 914 * Silently remove any request for a unique 915 * bssid; WDS vap's always share the local 916 * mac address. 917 */ 918 flags &= ~IEEE80211_CLONE_BSSID; 919 } 920 ic_opmode = IEEE80211_M_HOSTAP; 921 break; 922 default: 923 device_printf(sc->sc_dev, "unknown opmode %d\n", opmode); 924 goto bad; 925 } 926 /* 927 * Check that a beacon buffer is available; the code below assumes it. 928 */ 929 if (needbeacon & STAILQ_EMPTY(&sc->sc_bbuf)) { 930 device_printf(sc->sc_dev, "no beacon buffer available\n"); 931 goto bad; 932 } 933 934 /* STA, AHDEMO? */ 935 if (opmode == IEEE80211_M_HOSTAP) { 936 assign_address(sc, mac, flags & IEEE80211_CLONE_BSSID); 937 ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask); 938 } 939 940 vap = &avp->av_vap; 941 /* XXX can't hold mutex across if_alloc */ 942 ATH_UNLOCK(sc); 943 error = ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, 944 bssid, mac); 945 ATH_LOCK(sc); 946 if (error != 0) { 947 device_printf(sc->sc_dev, "%s: error %d creating vap\n", 948 __func__, error); 949 goto bad2; 950 } 951 952 /* h/w crypto support */ 953 vap->iv_key_alloc = ath_key_alloc; 954 vap->iv_key_delete = ath_key_delete; 955 vap->iv_key_set = ath_key_set; 956 vap->iv_key_update_begin = ath_key_update_begin; 957 vap->iv_key_update_end = ath_key_update_end; 958 959 /* override various methods */ 960 avp->av_recv_mgmt = vap->iv_recv_mgmt; 961 vap->iv_recv_mgmt = ath_recv_mgmt; 962 vap->iv_reset = ath_reset_vap; 963 vap->iv_update_beacon = ath_beacon_update; 964 avp->av_newstate = vap->iv_newstate; 965 vap->iv_newstate = ath_newstate; 966 avp->av_bmiss = vap->iv_bmiss; 967 vap->iv_bmiss = ath_bmiss_vap; 968 969 avp->av_bslot = -1; 970 if (needbeacon) { 971 /* 972 * Allocate beacon state and setup the q for buffered 973 * multicast frames. We know a beacon buffer is 974 * available because we checked above. 975 */ 976 avp->av_bcbuf = STAILQ_FIRST(&sc->sc_bbuf); 977 STAILQ_REMOVE_HEAD(&sc->sc_bbuf, bf_list); 978 if (opmode != IEEE80211_M_IBSS || !sc->sc_hasveol) { 979 /* 980 * Assign the vap to a beacon xmit slot. As above 981 * this cannot fail to find a free one. 982 */ 983 avp->av_bslot = assign_bslot(sc); 984 KASSERT(sc->sc_bslot[avp->av_bslot] == NULL, 985 ("beacon slot %u not empty", avp->av_bslot)); 986 sc->sc_bslot[avp->av_bslot] = vap; 987 sc->sc_nbcnvaps++; 988 } 989 if (sc->sc_hastsfadd && sc->sc_nbcnvaps > 0) { 990 /* 991 * Multple vaps are to transmit beacons and we 992 * have h/w support for TSF adjusting; enable 993 * use of staggered beacons. 994 */ 995 sc->sc_stagbeacons = 1; 996 } 997 ath_txq_init(sc, &avp->av_mcastq, ATH_TXQ_SWQ); 998 } 999 1000 ic->ic_opmode = ic_opmode; 1001 if (opmode != IEEE80211_M_WDS) { 1002 sc->sc_nvaps++; 1003 if (opmode == IEEE80211_M_STA) 1004 sc->sc_nstavaps++; 1005 } 1006 switch (ic_opmode) { 1007 case IEEE80211_M_IBSS: 1008 sc->sc_opmode = HAL_M_IBSS; 1009 break; 1010 case IEEE80211_M_STA: 1011 sc->sc_opmode = HAL_M_STA; 1012 break; 1013 case IEEE80211_M_AHDEMO: 1014 #ifdef ATH_SUPPORT_TDMA 1015 if (vap->iv_caps & IEEE80211_C_TDMA) { 1016 sc->sc_tdma = 1; 1017 /* NB: disable tsf adjust */ 1018 sc->sc_stagbeacons = 0; 1019 } 1020 /* 1021 * NB: adhoc demo mode is a pseudo mode; to the hal it's 1022 * just ap mode. 1023 */ 1024 /* fall thru... */ 1025 #endif 1026 case IEEE80211_M_HOSTAP: 1027 sc->sc_opmode = HAL_M_HOSTAP; 1028 break; 1029 case IEEE80211_M_MONITOR: 1030 sc->sc_opmode = HAL_M_MONITOR; 1031 break; 1032 default: 1033 /* XXX should not happen */ 1034 break; 1035 } 1036 if (sc->sc_hastsfadd) { 1037 /* 1038 * Configure whether or not TSF adjust should be done. 1039 */ 1040 ath_hal_settsfadjust(sc->sc_ah, sc->sc_stagbeacons); 1041 } 1042 if (flags & IEEE80211_CLONE_NOBEACONS) { 1043 /* 1044 * Enable s/w beacon miss handling. 1045 */ 1046 sc->sc_swbmiss = 1; 1047 } 1048 ATH_UNLOCK(sc); 1049 1050 /* complete setup */ 1051 ieee80211_vap_attach(vap, ath_media_change, ieee80211_media_status); 1052 return vap; 1053 bad2: 1054 reclaim_address(sc, mac); 1055 ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask); 1056 bad: 1057 free(avp, M_80211_VAP); 1058 ATH_UNLOCK(sc); 1059 return NULL; 1060 } 1061 1062 static void 1063 ath_vap_delete(struct ieee80211vap *vap) 1064 { 1065 struct ieee80211com *ic = vap->iv_ic; 1066 struct ifnet *ifp = ic->ic_ifp; 1067 struct ath_softc *sc = ifp->if_softc; 1068 struct ath_hal *ah = sc->sc_ah; 1069 struct ath_vap *avp = ATH_VAP(vap); 1070 1071 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 1072 /* 1073 * Quiesce the hardware while we remove the vap. In 1074 * particular we need to reclaim all references to 1075 * the vap state by any frames pending on the tx queues. 1076 */ 1077 ath_hal_intrset(ah, 0); /* disable interrupts */ 1078 ath_draintxq(sc); /* stop xmit side */ 1079 ath_stoprecv(sc); /* stop recv side */ 1080 } 1081 1082 ieee80211_vap_detach(vap); 1083 ATH_LOCK(sc); 1084 /* 1085 * Reclaim beacon state. Note this must be done before 1086 * the vap instance is reclaimed as we may have a reference 1087 * to it in the buffer for the beacon frame. 1088 */ 1089 if (avp->av_bcbuf != NULL) { 1090 if (avp->av_bslot != -1) { 1091 sc->sc_bslot[avp->av_bslot] = NULL; 1092 sc->sc_nbcnvaps--; 1093 } 1094 ath_beacon_return(sc, avp->av_bcbuf); 1095 avp->av_bcbuf = NULL; 1096 if (sc->sc_nbcnvaps == 0) { 1097 sc->sc_stagbeacons = 0; 1098 if (sc->sc_hastsfadd) 1099 ath_hal_settsfadjust(sc->sc_ah, 0); 1100 } 1101 /* 1102 * Reclaim any pending mcast frames for the vap. 1103 */ 1104 ath_tx_draintxq(sc, &avp->av_mcastq); 1105 ATH_TXQ_LOCK_DESTROY(&avp->av_mcastq); 1106 } 1107 /* 1108 * Update bookkeeping. 1109 */ 1110 if (vap->iv_opmode == IEEE80211_M_STA) { 1111 sc->sc_nstavaps--; 1112 if (sc->sc_nstavaps == 0 && sc->sc_swbmiss) 1113 sc->sc_swbmiss = 0; 1114 } else if (vap->iv_opmode == IEEE80211_M_HOSTAP) { 1115 reclaim_address(sc, vap->iv_myaddr); 1116 ath_hal_setbssidmask(ah, sc->sc_hwbssidmask); 1117 } 1118 if (vap->iv_opmode != IEEE80211_M_WDS) 1119 sc->sc_nvaps--; 1120 #ifdef ATH_SUPPORT_TDMA 1121 /* TDMA operation ceases when the last vap is destroyed */ 1122 if (sc->sc_tdma && sc->sc_nvaps == 0) { 1123 sc->sc_tdma = 0; 1124 sc->sc_swbmiss = 0; 1125 } 1126 #endif 1127 ATH_UNLOCK(sc); 1128 free(avp, M_80211_VAP); 1129 1130 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 1131 /* 1132 * Restart rx+tx machines if still running (RUNNING will 1133 * be reset if we just destroyed the last vap). 1134 */ 1135 if (ath_startrecv(sc) != 0) 1136 if_printf(ifp, "%s: unable to restart recv logic\n", 1137 __func__); 1138 if (sc->sc_beacons) 1139 ath_beacon_config(sc, NULL); 1140 ath_hal_intrset(ah, sc->sc_imask); 1141 } 1142 } 1143 1144 void 1145 ath_suspend(struct ath_softc *sc) 1146 { 1147 struct ifnet *ifp = sc->sc_ifp; 1148 struct ieee80211com *ic = ifp->if_l2com; 1149 1150 DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n", 1151 __func__, ifp->if_flags); 1152 1153 sc->sc_resume_up = (ifp->if_flags & IFF_UP) != 0; 1154 if (ic->ic_opmode == IEEE80211_M_STA) 1155 ath_stop(ifp); 1156 else 1157 ieee80211_suspend_all(ic); 1158 /* 1159 * NB: don't worry about putting the chip in low power 1160 * mode; pci will power off our socket on suspend and 1161 * cardbus detaches the device. 1162 */ 1163 } 1164 1165 /* 1166 * Reset the key cache since some parts do not reset the 1167 * contents on resume. First we clear all entries, then 1168 * re-load keys that the 802.11 layer assumes are setup 1169 * in h/w. 1170 */ 1171 static void 1172 ath_reset_keycache(struct ath_softc *sc) 1173 { 1174 struct ifnet *ifp = sc->sc_ifp; 1175 struct ieee80211com *ic = ifp->if_l2com; 1176 struct ath_hal *ah = sc->sc_ah; 1177 int i; 1178 1179 for (i = 0; i < sc->sc_keymax; i++) 1180 ath_hal_keyreset(ah, i); 1181 ieee80211_crypto_reload_keys(ic); 1182 } 1183 1184 void 1185 ath_resume(struct ath_softc *sc) 1186 { 1187 struct ifnet *ifp = sc->sc_ifp; 1188 struct ieee80211com *ic = ifp->if_l2com; 1189 struct ath_hal *ah = sc->sc_ah; 1190 HAL_STATUS status; 1191 1192 DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n", 1193 __func__, ifp->if_flags); 1194 1195 /* 1196 * Must reset the chip before we reload the 1197 * keycache as we were powered down on suspend. 1198 */ 1199 ath_hal_reset(ah, sc->sc_opmode, 1200 sc->sc_curchan != NULL ? sc->sc_curchan : ic->ic_curchan, 1201 AH_FALSE, &status); 1202 ath_reset_keycache(sc); 1203 if (sc->sc_resume_up) { 1204 if (ic->ic_opmode == IEEE80211_M_STA) { 1205 ath_init(sc); 1206 ieee80211_beacon_miss(ic); 1207 } else 1208 ieee80211_resume_all(ic); 1209 } 1210 if (sc->sc_softled) { 1211 ath_hal_gpioCfgOutput(ah, sc->sc_ledpin, 1212 HAL_GPIO_MUX_MAC_NETWORK_LED); 1213 ath_hal_gpioset(ah, sc->sc_ledpin, !sc->sc_ledon); 1214 } 1215 } 1216 1217 void 1218 ath_shutdown(struct ath_softc *sc) 1219 { 1220 struct ifnet *ifp = sc->sc_ifp; 1221 1222 DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n", 1223 __func__, ifp->if_flags); 1224 1225 ath_stop(ifp); 1226 /* NB: no point powering down chip as we're about to reboot */ 1227 } 1228 1229 /* 1230 * Interrupt handler. Most of the actual processing is deferred. 1231 */ 1232 void 1233 ath_intr(void *arg) 1234 { 1235 struct ath_softc *sc = arg; 1236 struct ifnet *ifp = sc->sc_ifp; 1237 struct ath_hal *ah = sc->sc_ah; 1238 HAL_INT status; 1239 1240 if (sc->sc_invalid) { 1241 /* 1242 * The hardware is not ready/present, don't touch anything. 1243 * Note this can happen early on if the IRQ is shared. 1244 */ 1245 DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid; ignored\n", __func__); 1246 return; 1247 } 1248 if (!ath_hal_intrpend(ah)) /* shared irq, not for us */ 1249 return; 1250 if ((ifp->if_flags & IFF_UP) == 0 || 1251 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 1252 HAL_INT status; 1253 1254 DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n", 1255 __func__, ifp->if_flags); 1256 ath_hal_getisr(ah, &status); /* clear ISR */ 1257 ath_hal_intrset(ah, 0); /* disable further intr's */ 1258 return; 1259 } 1260 /* 1261 * Figure out the reason(s) for the interrupt. Note 1262 * that the hal returns a pseudo-ISR that may include 1263 * bits we haven't explicitly enabled so we mask the 1264 * value to insure we only process bits we requested. 1265 */ 1266 ath_hal_getisr(ah, &status); /* NB: clears ISR too */ 1267 DPRINTF(sc, ATH_DEBUG_INTR, "%s: status 0x%x\n", __func__, status); 1268 status &= sc->sc_imask; /* discard unasked for bits */ 1269 if (status & HAL_INT_FATAL) { 1270 sc->sc_stats.ast_hardware++; 1271 ath_hal_intrset(ah, 0); /* disable intr's until reset */ 1272 ath_fatal_proc(sc, 0); 1273 } else { 1274 if (status & HAL_INT_SWBA) { 1275 /* 1276 * Software beacon alert--time to send a beacon. 1277 * Handle beacon transmission directly; deferring 1278 * this is too slow to meet timing constraints 1279 * under load. 1280 */ 1281 #ifdef ATH_SUPPORT_TDMA 1282 if (sc->sc_tdma) { 1283 if (sc->sc_tdmaswba == 0) { 1284 struct ieee80211com *ic = ifp->if_l2com; 1285 struct ieee80211vap *vap = 1286 TAILQ_FIRST(&ic->ic_vaps); 1287 ath_tdma_beacon_send(sc, vap); 1288 sc->sc_tdmaswba = 1289 vap->iv_tdma->tdma_bintval; 1290 } else 1291 sc->sc_tdmaswba--; 1292 } else 1293 #endif 1294 ath_beacon_proc(sc, 0); 1295 } 1296 if (status & HAL_INT_RXEOL) { 1297 /* 1298 * NB: the hardware should re-read the link when 1299 * RXE bit is written, but it doesn't work at 1300 * least on older hardware revs. 1301 */ 1302 sc->sc_stats.ast_rxeol++; 1303 sc->sc_rxlink = NULL; 1304 } 1305 if (status & HAL_INT_TXURN) { 1306 sc->sc_stats.ast_txurn++; 1307 /* bump tx trigger level */ 1308 ath_hal_updatetxtriglevel(ah, AH_TRUE); 1309 } 1310 if (status & HAL_INT_RX) 1311 taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask); 1312 if (status & HAL_INT_TX) 1313 taskqueue_enqueue(sc->sc_tq, &sc->sc_txtask); 1314 if (status & HAL_INT_BMISS) { 1315 sc->sc_stats.ast_bmiss++; 1316 taskqueue_enqueue(sc->sc_tq, &sc->sc_bmisstask); 1317 } 1318 if (status & HAL_INT_MIB) { 1319 sc->sc_stats.ast_mib++; 1320 /* 1321 * Disable interrupts until we service the MIB 1322 * interrupt; otherwise it will continue to fire. 1323 */ 1324 ath_hal_intrset(ah, 0); 1325 /* 1326 * Let the hal handle the event. We assume it will 1327 * clear whatever condition caused the interrupt. 1328 */ 1329 ath_hal_mibevent(ah, &sc->sc_halstats); 1330 ath_hal_intrset(ah, sc->sc_imask); 1331 } 1332 if (status & HAL_INT_RXORN) { 1333 /* NB: hal marks HAL_INT_FATAL when RXORN is fatal */ 1334 sc->sc_stats.ast_rxorn++; 1335 } 1336 } 1337 } 1338 1339 static void 1340 ath_fatal_proc(void *arg, int pending) 1341 { 1342 struct ath_softc *sc = arg; 1343 struct ifnet *ifp = sc->sc_ifp; 1344 u_int32_t *state; 1345 u_int32_t len; 1346 void *sp; 1347 1348 if_printf(ifp, "hardware error; resetting\n"); 1349 /* 1350 * Fatal errors are unrecoverable. Typically these 1351 * are caused by DMA errors. Collect h/w state from 1352 * the hal so we can diagnose what's going on. 1353 */ 1354 if (ath_hal_getfatalstate(sc->sc_ah, &sp, &len)) { 1355 KASSERT(len >= 6*sizeof(u_int32_t), ("len %u bytes", len)); 1356 state = sp; 1357 if_printf(ifp, "0x%08x 0x%08x 0x%08x, 0x%08x 0x%08x 0x%08x\n", 1358 state[0], state[1] , state[2], state[3], 1359 state[4], state[5]); 1360 } 1361 ath_reset(ifp); 1362 } 1363 1364 static void 1365 ath_bmiss_vap(struct ieee80211vap *vap) 1366 { 1367 /* 1368 * Workaround phantom bmiss interrupts by sanity-checking 1369 * the time of our last rx'd frame. If it is within the 1370 * beacon miss interval then ignore the interrupt. If it's 1371 * truly a bmiss we'll get another interrupt soon and that'll 1372 * be dispatched up for processing. Note this applies only 1373 * for h/w beacon miss events. 1374 */ 1375 if ((vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) == 0) { 1376 struct ifnet *ifp = vap->iv_ic->ic_ifp; 1377 struct ath_softc *sc = ifp->if_softc; 1378 u_int64_t lastrx = sc->sc_lastrx; 1379 u_int64_t tsf = ath_hal_gettsf64(sc->sc_ah); 1380 u_int bmisstimeout = 1381 vap->iv_bmissthreshold * vap->iv_bss->ni_intval * 1024; 1382 1383 DPRINTF(sc, ATH_DEBUG_BEACON, 1384 "%s: tsf %llu lastrx %lld (%llu) bmiss %u\n", 1385 __func__, (unsigned long long) tsf, 1386 (unsigned long long)(tsf - lastrx), 1387 (unsigned long long) lastrx, bmisstimeout); 1388 1389 if (tsf - lastrx <= bmisstimeout) { 1390 sc->sc_stats.ast_bmiss_phantom++; 1391 return; 1392 } 1393 } 1394 ATH_VAP(vap)->av_bmiss(vap); 1395 } 1396 1397 static int 1398 ath_hal_gethangstate(struct ath_hal *ah, uint32_t mask, uint32_t *hangs) 1399 { 1400 uint32_t rsize; 1401 void *sp; 1402 1403 if (!ath_hal_getdiagstate(ah, 32, &mask, sizeof(&mask), &sp, &rsize)) 1404 return 0; 1405 KASSERT(rsize == sizeof(uint32_t), ("resultsize %u", rsize)); 1406 *hangs = *(uint32_t *)sp; 1407 return 1; 1408 } 1409 1410 static void 1411 ath_bmiss_proc(void *arg, int pending) 1412 { 1413 struct ath_softc *sc = arg; 1414 struct ifnet *ifp = sc->sc_ifp; 1415 uint32_t hangs; 1416 1417 DPRINTF(sc, ATH_DEBUG_ANY, "%s: pending %u\n", __func__, pending); 1418 1419 if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0) { 1420 if_printf(ifp, "bb hang detected (0x%x), reseting\n", hangs); 1421 ath_reset(ifp); 1422 } else 1423 ieee80211_beacon_miss(ifp->if_l2com); 1424 } 1425 1426 /* 1427 * Handle TKIP MIC setup to deal hardware that doesn't do MIC 1428 * calcs together with WME. If necessary disable the crypto 1429 * hardware and mark the 802.11 state so keys will be setup 1430 * with the MIC work done in software. 1431 */ 1432 static void 1433 ath_settkipmic(struct ath_softc *sc) 1434 { 1435 struct ifnet *ifp = sc->sc_ifp; 1436 struct ieee80211com *ic = ifp->if_l2com; 1437 1438 if ((ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIP) && !sc->sc_wmetkipmic) { 1439 if (ic->ic_flags & IEEE80211_F_WME) { 1440 ath_hal_settkipmic(sc->sc_ah, AH_FALSE); 1441 ic->ic_cryptocaps &= ~IEEE80211_CRYPTO_TKIPMIC; 1442 } else { 1443 ath_hal_settkipmic(sc->sc_ah, AH_TRUE); 1444 ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC; 1445 } 1446 } 1447 } 1448 1449 static void 1450 ath_init(void *arg) 1451 { 1452 struct ath_softc *sc = (struct ath_softc *) arg; 1453 struct ifnet *ifp = sc->sc_ifp; 1454 struct ieee80211com *ic = ifp->if_l2com; 1455 struct ath_hal *ah = sc->sc_ah; 1456 HAL_STATUS status; 1457 1458 DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n", 1459 __func__, ifp->if_flags); 1460 1461 ATH_LOCK(sc); 1462 /* 1463 * Stop anything previously setup. This is safe 1464 * whether this is the first time through or not. 1465 */ 1466 ath_stop_locked(ifp); 1467 1468 /* 1469 * The basic interface to setting the hardware in a good 1470 * state is ``reset''. On return the hardware is known to 1471 * be powered up and with interrupts disabled. This must 1472 * be followed by initialization of the appropriate bits 1473 * and then setup of the interrupt mask. 1474 */ 1475 ath_settkipmic(sc); 1476 if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_FALSE, &status)) { 1477 if_printf(ifp, "unable to reset hardware; hal status %u\n", 1478 status); 1479 ATH_UNLOCK(sc); 1480 return; 1481 } 1482 ath_chan_change(sc, ic->ic_curchan); 1483 1484 /* 1485 * Likewise this is set during reset so update 1486 * state cached in the driver. 1487 */ 1488 sc->sc_diversity = ath_hal_getdiversity(ah); 1489 sc->sc_lastlongcal = 0; 1490 sc->sc_resetcal = 1; 1491 sc->sc_lastcalreset = 0; 1492 1493 /* 1494 * Setup the hardware after reset: the key cache 1495 * is filled as needed and the receive engine is 1496 * set going. Frame transmit is handled entirely 1497 * in the frame output path; there's nothing to do 1498 * here except setup the interrupt mask. 1499 */ 1500 if (ath_startrecv(sc) != 0) { 1501 if_printf(ifp, "unable to start recv logic\n"); 1502 ATH_UNLOCK(sc); 1503 return; 1504 } 1505 1506 /* 1507 * Enable interrupts. 1508 */ 1509 sc->sc_imask = HAL_INT_RX | HAL_INT_TX 1510 | HAL_INT_RXEOL | HAL_INT_RXORN 1511 | HAL_INT_FATAL | HAL_INT_GLOBAL; 1512 /* 1513 * Enable MIB interrupts when there are hardware phy counters. 1514 * Note we only do this (at the moment) for station mode. 1515 */ 1516 if (sc->sc_needmib && ic->ic_opmode == IEEE80211_M_STA) 1517 sc->sc_imask |= HAL_INT_MIB; 1518 1519 ifp->if_drv_flags |= IFF_DRV_RUNNING; 1520 callout_reset(&sc->sc_wd_ch, hz, ath_watchdog, sc); 1521 ath_hal_intrset(ah, sc->sc_imask); 1522 1523 ATH_UNLOCK(sc); 1524 1525 #ifdef ATH_TX99_DIAG 1526 if (sc->sc_tx99 != NULL) 1527 sc->sc_tx99->start(sc->sc_tx99); 1528 else 1529 #endif 1530 ieee80211_start_all(ic); /* start all vap's */ 1531 } 1532 1533 static void 1534 ath_stop_locked(struct ifnet *ifp) 1535 { 1536 struct ath_softc *sc = ifp->if_softc; 1537 struct ath_hal *ah = sc->sc_ah; 1538 1539 DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid %u if_flags 0x%x\n", 1540 __func__, sc->sc_invalid, ifp->if_flags); 1541 1542 ATH_LOCK_ASSERT(sc); 1543 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 1544 /* 1545 * Shutdown the hardware and driver: 1546 * reset 802.11 state machine 1547 * turn off timers 1548 * disable interrupts 1549 * turn off the radio 1550 * clear transmit machinery 1551 * clear receive machinery 1552 * drain and release tx queues 1553 * reclaim beacon resources 1554 * power down hardware 1555 * 1556 * Note that some of this work is not possible if the 1557 * hardware is gone (invalid). 1558 */ 1559 #ifdef ATH_TX99_DIAG 1560 if (sc->sc_tx99 != NULL) 1561 sc->sc_tx99->stop(sc->sc_tx99); 1562 #endif 1563 callout_stop(&sc->sc_wd_ch); 1564 sc->sc_wd_timer = 0; 1565 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1566 if (!sc->sc_invalid) { 1567 if (sc->sc_softled) { 1568 callout_stop(&sc->sc_ledtimer); 1569 ath_hal_gpioset(ah, sc->sc_ledpin, 1570 !sc->sc_ledon); 1571 sc->sc_blinking = 0; 1572 } 1573 ath_hal_intrset(ah, 0); 1574 } 1575 ath_draintxq(sc); 1576 if (!sc->sc_invalid) { 1577 ath_stoprecv(sc); 1578 ath_hal_phydisable(ah); 1579 } else 1580 sc->sc_rxlink = NULL; 1581 ath_beacon_free(sc); /* XXX not needed */ 1582 } 1583 } 1584 1585 static void 1586 ath_stop(struct ifnet *ifp) 1587 { 1588 struct ath_softc *sc = ifp->if_softc; 1589 1590 ATH_LOCK(sc); 1591 ath_stop_locked(ifp); 1592 ATH_UNLOCK(sc); 1593 } 1594 1595 /* 1596 * Reset the hardware w/o losing operational state. This is 1597 * basically a more efficient way of doing ath_stop, ath_init, 1598 * followed by state transitions to the current 802.11 1599 * operational state. Used to recover from various errors and 1600 * to reset or reload hardware state. 1601 */ 1602 static int 1603 ath_reset(struct ifnet *ifp) 1604 { 1605 struct ath_softc *sc = ifp->if_softc; 1606 struct ieee80211com *ic = ifp->if_l2com; 1607 struct ath_hal *ah = sc->sc_ah; 1608 HAL_STATUS status; 1609 1610 ath_hal_intrset(ah, 0); /* disable interrupts */ 1611 ath_draintxq(sc); /* stop xmit side */ 1612 ath_stoprecv(sc); /* stop recv side */ 1613 ath_settkipmic(sc); /* configure TKIP MIC handling */ 1614 /* NB: indicate channel change so we do a full reset */ 1615 if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_TRUE, &status)) 1616 if_printf(ifp, "%s: unable to reset hardware; hal status %u\n", 1617 __func__, status); 1618 sc->sc_diversity = ath_hal_getdiversity(ah); 1619 if (ath_startrecv(sc) != 0) /* restart recv */ 1620 if_printf(ifp, "%s: unable to start recv logic\n", __func__); 1621 /* 1622 * We may be doing a reset in response to an ioctl 1623 * that changes the channel so update any state that 1624 * might change as a result. 1625 */ 1626 ath_chan_change(sc, ic->ic_curchan); 1627 if (sc->sc_beacons) { 1628 #ifdef ATH_SUPPORT_TDMA 1629 if (sc->sc_tdma) 1630 ath_tdma_config(sc, NULL); 1631 else 1632 #endif 1633 ath_beacon_config(sc, NULL); /* restart beacons */ 1634 } 1635 ath_hal_intrset(ah, sc->sc_imask); 1636 1637 ath_start(ifp); /* restart xmit */ 1638 return 0; 1639 } 1640 1641 static int 1642 ath_reset_vap(struct ieee80211vap *vap, u_long cmd) 1643 { 1644 struct ieee80211com *ic = vap->iv_ic; 1645 struct ifnet *ifp = ic->ic_ifp; 1646 struct ath_softc *sc = ifp->if_softc; 1647 struct ath_hal *ah = sc->sc_ah; 1648 1649 switch (cmd) { 1650 case IEEE80211_IOC_TXPOWER: 1651 /* 1652 * If per-packet TPC is enabled, then we have nothing 1653 * to do; otherwise we need to force the global limit. 1654 * All this can happen directly; no need to reset. 1655 */ 1656 if (!ath_hal_gettpc(ah)) 1657 ath_hal_settxpowlimit(ah, ic->ic_txpowlimit); 1658 return 0; 1659 } 1660 return ath_reset(ifp); 1661 } 1662 1663 static int 1664 ath_ff_always(struct ath_txq *txq, struct ath_buf *bf) 1665 { 1666 return 0; 1667 } 1668 1669 #if 0 1670 static int 1671 ath_ff_ageflushtestdone(struct ath_txq *txq, struct ath_buf *bf) 1672 { 1673 return (txq->axq_curage - bf->bf_age) < ATH_FF_STAGEMAX; 1674 } 1675 #endif 1676 1677 /* 1678 * Flush FF staging queue. 1679 */ 1680 static void 1681 ath_ff_stageq_flush(struct ath_softc *sc, struct ath_txq *txq, 1682 int (*ath_ff_flushdonetest)(struct ath_txq *txq, struct ath_buf *bf)) 1683 { 1684 struct ath_buf *bf; 1685 struct ieee80211_node *ni; 1686 int pktlen, pri; 1687 1688 for (;;) { 1689 ATH_TXQ_LOCK(txq); 1690 /* 1691 * Go from the back (oldest) to front so we can 1692 * stop early based on the age of the entry. 1693 */ 1694 bf = TAILQ_LAST(&txq->axq_stageq, axq_headtype); 1695 if (bf == NULL || ath_ff_flushdonetest(txq, bf)) { 1696 ATH_TXQ_UNLOCK(txq); 1697 break; 1698 } 1699 1700 ni = bf->bf_node; 1701 pri = M_WME_GETAC(bf->bf_m); 1702 KASSERT(ATH_NODE(ni)->an_ff_buf[pri], 1703 ("no bf on staging queue %p", bf)); 1704 ATH_NODE(ni)->an_ff_buf[pri] = NULL; 1705 TAILQ_REMOVE(&txq->axq_stageq, bf, bf_stagelist); 1706 1707 ATH_TXQ_UNLOCK(txq); 1708 1709 DPRINTF(sc, ATH_DEBUG_FF, "%s: flush frame, age %u\n", 1710 __func__, bf->bf_age); 1711 1712 sc->sc_stats.ast_ff_flush++; 1713 1714 /* encap and xmit */ 1715 bf->bf_m = ieee80211_encap(ni, bf->bf_m); 1716 if (bf->bf_m == NULL) { 1717 DPRINTF(sc, ATH_DEBUG_XMIT | ATH_DEBUG_FF, 1718 "%s: discard, encapsulation failure\n", 1719 __func__); 1720 sc->sc_stats.ast_tx_encap++; 1721 goto bad; 1722 } 1723 pktlen = bf->bf_m->m_pkthdr.len; /* NB: don't reference below */ 1724 if (ath_tx_start(sc, ni, bf, bf->bf_m) == 0) { 1725 #if 0 /*XXX*/ 1726 ifp->if_opackets++; 1727 #endif 1728 continue; 1729 } 1730 bad: 1731 if (ni != NULL) 1732 ieee80211_free_node(ni); 1733 bf->bf_node = NULL; 1734 if (bf->bf_m != NULL) { 1735 m_freem(bf->bf_m); 1736 bf->bf_m = NULL; 1737 } 1738 1739 ATH_TXBUF_LOCK(sc); 1740 STAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list); 1741 ATH_TXBUF_UNLOCK(sc); 1742 } 1743 } 1744 1745 static __inline u_int32_t 1746 ath_ff_approx_txtime(struct ath_softc *sc, struct ath_node *an, struct mbuf *m) 1747 { 1748 struct ieee80211com *ic = sc->sc_ifp->if_l2com; 1749 u_int32_t framelen; 1750 struct ath_buf *bf; 1751 1752 /* 1753 * Approximate the frame length to be transmitted. A swag to add 1754 * the following maximal values to the skb payload: 1755 * - 32: 802.11 encap + CRC 1756 * - 24: encryption overhead (if wep bit) 1757 * - 4 + 6: fast-frame header and padding 1758 * - 16: 2 LLC FF tunnel headers 1759 * - 14: 1 802.3 FF tunnel header (skb already accounts for 2nd) 1760 */ 1761 framelen = m->m_pkthdr.len + 32 + 4 + 6 + 16 + 14; 1762 if (ic->ic_flags & IEEE80211_F_PRIVACY) 1763 framelen += 24; 1764 bf = an->an_ff_buf[M_WME_GETAC(m)]; 1765 if (bf != NULL) 1766 framelen += bf->bf_m->m_pkthdr.len; 1767 return ath_hal_computetxtime(sc->sc_ah, sc->sc_currates, framelen, 1768 sc->sc_lastdatarix, AH_FALSE); 1769 } 1770 1771 /* 1772 * Determine if a data frame may be aggregated via ff tunnelling. 1773 * Note the caller is responsible for checking if the destination 1774 * supports fast frames. 1775 * 1776 * NB: allowing EAPOL frames to be aggregated with other unicast traffic. 1777 * Do 802.1x EAPOL frames proceed in the clear? Then they couldn't 1778 * be aggregated with other types of frames when encryption is on? 1779 * 1780 * NB: assumes lock on an_ff_buf effectively held by txq lock mechanism. 1781 */ 1782 static __inline int 1783 ath_ff_can_aggregate(struct ath_softc *sc, 1784 struct ath_node *an, struct mbuf *m, int *flushq) 1785 { 1786 struct ieee80211com *ic = sc->sc_ifp->if_l2com; 1787 struct ath_txq *txq; 1788 u_int32_t txoplimit; 1789 u_int pri; 1790 1791 *flushq = 0; 1792 1793 /* 1794 * If there is no frame to combine with and the txq has 1795 * fewer frames than the minimum required; then do not 1796 * attempt to aggregate this frame. 1797 */ 1798 pri = M_WME_GETAC(m); 1799 txq = sc->sc_ac2q[pri]; 1800 if (an->an_ff_buf[pri] == NULL && txq->axq_depth < sc->sc_fftxqmin) 1801 return 0; 1802 /* 1803 * When not in station mode never aggregate a multicast 1804 * frame; this insures, for example, that a combined frame 1805 * does not require multiple encryption keys when using 1806 * 802.1x/WPA. 1807 */ 1808 if (ic->ic_opmode != IEEE80211_M_STA && 1809 ETHER_IS_MULTICAST(mtod(m, struct ether_header *)->ether_dhost)) 1810 return 0; 1811 /* 1812 * Consult the max bursting interval to insure a combined 1813 * frame fits within the TxOp window. 1814 */ 1815 txoplimit = IEEE80211_TXOP_TO_US( 1816 ic->ic_wme.wme_chanParams.cap_wmeParams[pri].wmep_txopLimit); 1817 if (txoplimit != 0 && ath_ff_approx_txtime(sc, an, m) > txoplimit) { 1818 DPRINTF(sc, ATH_DEBUG_XMIT | ATH_DEBUG_FF, 1819 "%s: FF TxOp violation\n", __func__); 1820 if (an->an_ff_buf[pri] != NULL) 1821 *flushq = 1; 1822 return 0; 1823 } 1824 return 1; /* try to aggregate */ 1825 } 1826 1827 /* 1828 * Check if the supplied frame can be partnered with an existing 1829 * or pending frame. Return a reference to any frame that should be 1830 * sent on return; otherwise return NULL. 1831 */ 1832 static struct mbuf * 1833 ath_ff_check(struct ath_softc *sc, struct ath_txq *txq, 1834 struct ath_buf *bf, struct mbuf *m, struct ieee80211_node *ni) 1835 { 1836 struct ath_node *an = ATH_NODE(ni); 1837 struct ath_buf *bfstaged; 1838 int ff_flush, pri; 1839 1840 /* 1841 * Check if the supplied frame can be aggregated. 1842 * 1843 * NB: we use the txq lock to protect references to 1844 * an->an_ff_txbuf in ath_ff_can_aggregate(). 1845 */ 1846 ATH_TXQ_LOCK(txq); 1847 pri = M_WME_GETAC(m); 1848 if (ath_ff_can_aggregate(sc, an, m, &ff_flush)) { 1849 struct ath_buf *bfstaged = an->an_ff_buf[pri]; 1850 if (bfstaged != NULL) { 1851 /* 1852 * A frame is available for partnering; remove 1853 * it, chain it to this one, and encapsulate. 1854 */ 1855 an->an_ff_buf[pri] = NULL; 1856 TAILQ_REMOVE(&txq->axq_stageq, bfstaged, bf_stagelist); 1857 ATH_TXQ_UNLOCK(txq); 1858 1859 /* 1860 * Chain mbufs and add FF magic. 1861 */ 1862 DPRINTF(sc, ATH_DEBUG_FF, 1863 "[%s] aggregate fast-frame, age %u\n", 1864 ether_sprintf(ni->ni_macaddr), txq->axq_curage); 1865 m->m_nextpkt = NULL; 1866 bfstaged->bf_m->m_nextpkt = m; 1867 m = bfstaged->bf_m; 1868 bfstaged->bf_m = NULL; 1869 m->m_flags |= M_FF; 1870 /* 1871 * Release the node reference held while 1872 * the packet sat on an_ff_buf[] 1873 */ 1874 bfstaged->bf_node = NULL; 1875 ieee80211_free_node(ni); 1876 1877 /* 1878 * Return bfstaged to the free list. 1879 */ 1880 ATH_TXBUF_LOCK(sc); 1881 STAILQ_INSERT_HEAD(&sc->sc_txbuf, bfstaged, bf_list); 1882 ATH_TXBUF_UNLOCK(sc); 1883 1884 return m; /* ready to go */ 1885 } else { 1886 /* 1887 * No frame available, queue this frame to wait 1888 * for a partner. Note that we hold the buffer 1889 * and a reference to the node; we need the 1890 * buffer in particular so we're certain we 1891 * can flush the frame at a later time. 1892 */ 1893 DPRINTF(sc, ATH_DEBUG_FF, 1894 "[%s] stage fast-frame, age %u\n", 1895 ether_sprintf(ni->ni_macaddr), txq->axq_curage); 1896 1897 bf->bf_m = m; 1898 bf->bf_node = ni; /* NB: held reference */ 1899 bf->bf_age = txq->axq_curage; 1900 an->an_ff_buf[pri] = bf; 1901 TAILQ_INSERT_HEAD(&txq->axq_stageq, bf, bf_stagelist); 1902 ATH_TXQ_UNLOCK(txq); 1903 1904 return NULL; /* consumed */ 1905 } 1906 } 1907 /* 1908 * Frame could not be aggregated, it needs to be returned 1909 * to the caller for immediate transmission. In addition 1910 * we check if we should first flush a frame from the 1911 * staging queue before sending this one. 1912 * 1913 * NB: ath_ff_can_aggregate only marks ff_flush if a frame 1914 * is present to flush. 1915 */ 1916 if (ff_flush) { 1917 int pktlen; 1918 1919 bfstaged = an->an_ff_buf[pri]; 1920 an->an_ff_buf[pri] = NULL; 1921 TAILQ_REMOVE(&txq->axq_stageq, bfstaged, bf_stagelist); 1922 ATH_TXQ_UNLOCK(txq); 1923 1924 DPRINTF(sc, ATH_DEBUG_FF, "[%s] flush staged frame\n", 1925 ether_sprintf(an->an_node.ni_macaddr)); 1926 1927 /* encap and xmit */ 1928 bfstaged->bf_m = ieee80211_encap(ni, bfstaged->bf_m); 1929 if (bfstaged->bf_m == NULL) { 1930 DPRINTF(sc, ATH_DEBUG_XMIT | ATH_DEBUG_FF, 1931 "%s: discard, encap failure\n", __func__); 1932 sc->sc_stats.ast_tx_encap++; 1933 goto ff_flushbad; 1934 } 1935 pktlen = bfstaged->bf_m->m_pkthdr.len; 1936 if (ath_tx_start(sc, ni, bfstaged, bfstaged->bf_m)) { 1937 DPRINTF(sc, ATH_DEBUG_XMIT, 1938 "%s: discard, xmit failure\n", __func__); 1939 ff_flushbad: 1940 /* 1941 * Unable to transmit frame that was on the staging 1942 * queue. Reclaim the node reference and other 1943 * resources. 1944 */ 1945 if (ni != NULL) 1946 ieee80211_free_node(ni); 1947 bfstaged->bf_node = NULL; 1948 if (bfstaged->bf_m != NULL) { 1949 m_freem(bfstaged->bf_m); 1950 bfstaged->bf_m = NULL; 1951 } 1952 1953 ATH_TXBUF_LOCK(sc); 1954 STAILQ_INSERT_HEAD(&sc->sc_txbuf, bfstaged, bf_list); 1955 ATH_TXBUF_UNLOCK(sc); 1956 } else { 1957 #if 0 1958 ifp->if_opackets++; 1959 #endif 1960 } 1961 } else { 1962 if (an->an_ff_buf[pri] != NULL) { 1963 /* 1964 * XXX: out-of-order condition only occurs for AP 1965 * mode and multicast. There may be no valid way 1966 * to get this condition. 1967 */ 1968 DPRINTF(sc, ATH_DEBUG_FF, "[%s] out-of-order frame\n", 1969 ether_sprintf(an->an_node.ni_macaddr)); 1970 /* XXX stat */ 1971 } 1972 ATH_TXQ_UNLOCK(txq); 1973 } 1974 return m; 1975 } 1976 1977 static struct ath_buf * 1978 _ath_getbuf_locked(struct ath_softc *sc) 1979 { 1980 struct ath_buf *bf; 1981 1982 ATH_TXBUF_LOCK_ASSERT(sc); 1983 1984 bf = STAILQ_FIRST(&sc->sc_txbuf); 1985 if (bf != NULL && (bf->bf_flags & ATH_BUF_BUSY) == 0) 1986 STAILQ_REMOVE_HEAD(&sc->sc_txbuf, bf_list); 1987 else 1988 bf = NULL; 1989 if (bf == NULL) { 1990 DPRINTF(sc, ATH_DEBUG_XMIT, "%s: %s\n", __func__, 1991 STAILQ_FIRST(&sc->sc_txbuf) == NULL ? 1992 "out of xmit buffers" : "xmit buffer busy"); 1993 sc->sc_stats.ast_tx_nobuf++; 1994 } 1995 return bf; 1996 } 1997 1998 static struct ath_buf * 1999 ath_getbuf(struct ath_softc *sc) 2000 { 2001 struct ath_buf *bf; 2002 2003 ATH_TXBUF_LOCK(sc); 2004 bf = _ath_getbuf_locked(sc); 2005 if (bf == NULL) { 2006 struct ifnet *ifp = sc->sc_ifp; 2007 2008 DPRINTF(sc, ATH_DEBUG_XMIT, "%s: stop queue\n", __func__); 2009 sc->sc_stats.ast_tx_qstop++; 2010 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2011 } 2012 ATH_TXBUF_UNLOCK(sc); 2013 return bf; 2014 } 2015 2016 /* 2017 * Cleanup driver resources when we run out of buffers 2018 * while processing fragments; return the tx buffers 2019 * allocated and drop node references. 2020 */ 2021 static void 2022 ath_txfrag_cleanup(struct ath_softc *sc, 2023 ath_bufhead *frags, struct ieee80211_node *ni) 2024 { 2025 struct ath_buf *bf, *next; 2026 2027 ATH_TXBUF_LOCK_ASSERT(sc); 2028 2029 STAILQ_FOREACH_SAFE(bf, frags, bf_list, next) { 2030 /* NB: bf assumed clean */ 2031 STAILQ_REMOVE_HEAD(frags, bf_list); 2032 STAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list); 2033 ieee80211_node_decref(ni); 2034 } 2035 } 2036 2037 /* 2038 * Setup xmit of a fragmented frame. Allocate a buffer 2039 * for each frag and bump the node reference count to 2040 * reflect the held reference to be setup by ath_tx_start. 2041 */ 2042 static int 2043 ath_txfrag_setup(struct ath_softc *sc, ath_bufhead *frags, 2044 struct mbuf *m0, struct ieee80211_node *ni) 2045 { 2046 struct mbuf *m; 2047 struct ath_buf *bf; 2048 2049 ATH_TXBUF_LOCK(sc); 2050 for (m = m0->m_nextpkt; m != NULL; m = m->m_nextpkt) { 2051 bf = _ath_getbuf_locked(sc); 2052 if (bf == NULL) { /* out of buffers, cleanup */ 2053 ath_txfrag_cleanup(sc, frags, ni); 2054 break; 2055 } 2056 ieee80211_node_incref(ni); 2057 STAILQ_INSERT_TAIL(frags, bf, bf_list); 2058 } 2059 ATH_TXBUF_UNLOCK(sc); 2060 2061 return !STAILQ_EMPTY(frags); 2062 } 2063 2064 static void 2065 ath_start(struct ifnet *ifp) 2066 { 2067 struct ath_softc *sc = ifp->if_softc; 2068 struct ieee80211_node *ni; 2069 struct ath_buf *bf; 2070 struct mbuf *m, *next; 2071 struct ath_txq *txq; 2072 ath_bufhead frags; 2073 int pri; 2074 2075 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->sc_invalid) 2076 return; 2077 for (;;) { 2078 /* 2079 * Grab a TX buffer and associated resources. 2080 */ 2081 bf = ath_getbuf(sc); 2082 if (bf == NULL) 2083 break; 2084 2085 IFQ_DEQUEUE(&ifp->if_snd, m); 2086 if (m == NULL) { 2087 ATH_TXBUF_LOCK(sc); 2088 STAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list); 2089 ATH_TXBUF_UNLOCK(sc); 2090 break; 2091 } 2092 STAILQ_INIT(&frags); 2093 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif; 2094 pri = M_WME_GETAC(m); 2095 txq = sc->sc_ac2q[pri]; 2096 if (IEEE80211_ATH_CAP(ni->ni_vap, ni, IEEE80211_NODE_FF)) { 2097 /* 2098 * Check queue length; if too deep drop this 2099 * frame (tail drop considered good). 2100 */ 2101 if (txq->axq_depth >= sc->sc_fftxqmax) { 2102 DPRINTF(sc, ATH_DEBUG_FF, 2103 "[%s] tail drop on q %u depth %u\n", 2104 ether_sprintf(ni->ni_macaddr), 2105 txq->axq_qnum, txq->axq_depth); 2106 sc->sc_stats.ast_tx_qfull++; 2107 m_freem(m); 2108 goto reclaim; 2109 } 2110 m = ath_ff_check(sc, txq, bf, m, ni); 2111 if (m == NULL) { 2112 /* NB: ni ref & bf held on stageq */ 2113 continue; 2114 } 2115 } 2116 ifp->if_opackets++; 2117 /* 2118 * Encapsulate the packet in prep for transmission. 2119 */ 2120 m = ieee80211_encap(ni, m); 2121 if (m == NULL) { 2122 DPRINTF(sc, ATH_DEBUG_XMIT, 2123 "%s: encapsulation failure\n", __func__); 2124 sc->sc_stats.ast_tx_encap++; 2125 goto bad; 2126 } 2127 /* 2128 * Check for fragmentation. If this frame 2129 * has been broken up verify we have enough 2130 * buffers to send all the fragments so all 2131 * go out or none... 2132 */ 2133 if ((m->m_flags & M_FRAG) && 2134 !ath_txfrag_setup(sc, &frags, m, ni)) { 2135 DPRINTF(sc, ATH_DEBUG_XMIT, 2136 "%s: out of txfrag buffers\n", __func__); 2137 sc->sc_stats.ast_tx_nofrag++; 2138 ath_freetx(m); 2139 goto bad; 2140 } 2141 nextfrag: 2142 /* 2143 * Pass the frame to the h/w for transmission. 2144 * Fragmented frames have each frag chained together 2145 * with m_nextpkt. We know there are sufficient ath_buf's 2146 * to send all the frags because of work done by 2147 * ath_txfrag_setup. We leave m_nextpkt set while 2148 * calling ath_tx_start so it can use it to extend the 2149 * the tx duration to cover the subsequent frag and 2150 * so it can reclaim all the mbufs in case of an error; 2151 * ath_tx_start clears m_nextpkt once it commits to 2152 * handing the frame to the hardware. 2153 */ 2154 next = m->m_nextpkt; 2155 if (ath_tx_start(sc, ni, bf, m)) { 2156 bad: 2157 ifp->if_oerrors++; 2158 reclaim: 2159 bf->bf_m = NULL; 2160 bf->bf_node = NULL; 2161 ATH_TXBUF_LOCK(sc); 2162 STAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list); 2163 ath_txfrag_cleanup(sc, &frags, ni); 2164 ATH_TXBUF_UNLOCK(sc); 2165 if (ni != NULL) 2166 ieee80211_free_node(ni); 2167 continue; 2168 } 2169 if (next != NULL) { 2170 /* 2171 * Beware of state changing between frags. 2172 * XXX check sta power-save state? 2173 */ 2174 if (ni->ni_vap->iv_state != IEEE80211_S_RUN) { 2175 DPRINTF(sc, ATH_DEBUG_XMIT, 2176 "%s: flush fragmented packet, state %s\n", 2177 __func__, 2178 ieee80211_state_name[ni->ni_vap->iv_state]); 2179 ath_freetx(next); 2180 goto reclaim; 2181 } 2182 m = next; 2183 bf = STAILQ_FIRST(&frags); 2184 KASSERT(bf != NULL, ("no buf for txfrag")); 2185 STAILQ_REMOVE_HEAD(&frags, bf_list); 2186 goto nextfrag; 2187 } 2188 2189 sc->sc_wd_timer = 5; 2190 #if 0 2191 /* 2192 * Flush stale frames from the fast-frame staging queue. 2193 */ 2194 if (ic->ic_opmode != IEEE80211_M_STA) 2195 ath_ff_stageq_flush(sc, txq, ath_ff_ageflushtestdone); 2196 #endif 2197 } 2198 } 2199 2200 static int 2201 ath_media_change(struct ifnet *ifp) 2202 { 2203 int error = ieee80211_media_change(ifp); 2204 /* NB: only the fixed rate can change and that doesn't need a reset */ 2205 return (error == ENETRESET ? 0 : error); 2206 } 2207 2208 #ifdef ATH_DEBUG 2209 static void 2210 ath_keyprint(struct ath_softc *sc, const char *tag, u_int ix, 2211 const HAL_KEYVAL *hk, const u_int8_t mac[IEEE80211_ADDR_LEN]) 2212 { 2213 static const char *ciphers[] = { 2214 "WEP", 2215 "AES-OCB", 2216 "AES-CCM", 2217 "CKIP", 2218 "TKIP", 2219 "CLR", 2220 }; 2221 int i, n; 2222 2223 printf("%s: [%02u] %-7s ", tag, ix, ciphers[hk->kv_type]); 2224 for (i = 0, n = hk->kv_len; i < n; i++) 2225 printf("%02x", hk->kv_val[i]); 2226 printf(" mac %s", ether_sprintf(mac)); 2227 if (hk->kv_type == HAL_CIPHER_TKIP) { 2228 printf(" %s ", sc->sc_splitmic ? "mic" : "rxmic"); 2229 for (i = 0; i < sizeof(hk->kv_mic); i++) 2230 printf("%02x", hk->kv_mic[i]); 2231 if (!sc->sc_splitmic) { 2232 printf(" txmic "); 2233 for (i = 0; i < sizeof(hk->kv_txmic); i++) 2234 printf("%02x", hk->kv_txmic[i]); 2235 } 2236 } 2237 printf("\n"); 2238 } 2239 #endif 2240 2241 /* 2242 * Set a TKIP key into the hardware. This handles the 2243 * potential distribution of key state to multiple key 2244 * cache slots for TKIP. 2245 */ 2246 static int 2247 ath_keyset_tkip(struct ath_softc *sc, const struct ieee80211_key *k, 2248 HAL_KEYVAL *hk, const u_int8_t mac[IEEE80211_ADDR_LEN]) 2249 { 2250 #define IEEE80211_KEY_XR (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV) 2251 static const u_int8_t zerobssid[IEEE80211_ADDR_LEN]; 2252 struct ath_hal *ah = sc->sc_ah; 2253 2254 KASSERT(k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP, 2255 ("got a non-TKIP key, cipher %u", k->wk_cipher->ic_cipher)); 2256 if ((k->wk_flags & IEEE80211_KEY_XR) == IEEE80211_KEY_XR) { 2257 if (sc->sc_splitmic) { 2258 /* 2259 * TX key goes at first index, RX key at the rx index. 2260 * The hal handles the MIC keys at index+64. 2261 */ 2262 memcpy(hk->kv_mic, k->wk_txmic, sizeof(hk->kv_mic)); 2263 KEYPRINTF(sc, k->wk_keyix, hk, zerobssid); 2264 if (!ath_hal_keyset(ah, k->wk_keyix, hk, zerobssid)) 2265 return 0; 2266 2267 memcpy(hk->kv_mic, k->wk_rxmic, sizeof(hk->kv_mic)); 2268 KEYPRINTF(sc, k->wk_keyix+32, hk, mac); 2269 /* XXX delete tx key on failure? */ 2270 return ath_hal_keyset(ah, k->wk_keyix+32, hk, mac); 2271 } else { 2272 /* 2273 * Room for both TX+RX MIC keys in one key cache 2274 * slot, just set key at the first index; the hal 2275 * will handle the rest. 2276 */ 2277 memcpy(hk->kv_mic, k->wk_rxmic, sizeof(hk->kv_mic)); 2278 memcpy(hk->kv_txmic, k->wk_txmic, sizeof(hk->kv_txmic)); 2279 KEYPRINTF(sc, k->wk_keyix, hk, mac); 2280 return ath_hal_keyset(ah, k->wk_keyix, hk, mac); 2281 } 2282 } else if (k->wk_flags & IEEE80211_KEY_XMIT) { 2283 if (sc->sc_splitmic) { 2284 /* 2285 * NB: must pass MIC key in expected location when 2286 * the keycache only holds one MIC key per entry. 2287 */ 2288 memcpy(hk->kv_mic, k->wk_txmic, sizeof(hk->kv_txmic)); 2289 } else 2290 memcpy(hk->kv_txmic, k->wk_txmic, sizeof(hk->kv_txmic)); 2291 KEYPRINTF(sc, k->wk_keyix, hk, mac); 2292 return ath_hal_keyset(ah, k->wk_keyix, hk, mac); 2293 } else if (k->wk_flags & IEEE80211_KEY_RECV) { 2294 memcpy(hk->kv_mic, k->wk_rxmic, sizeof(hk->kv_mic)); 2295 KEYPRINTF(sc, k->wk_keyix, hk, mac); 2296 return ath_hal_keyset(ah, k->wk_keyix, hk, mac); 2297 } 2298 return 0; 2299 #undef IEEE80211_KEY_XR 2300 } 2301 2302 /* 2303 * Set a net80211 key into the hardware. This handles the 2304 * potential distribution of key state to multiple key 2305 * cache slots for TKIP with hardware MIC support. 2306 */ 2307 static int 2308 ath_keyset(struct ath_softc *sc, const struct ieee80211_key *k, 2309 struct ieee80211_node *bss) 2310 { 2311 #define N(a) (sizeof(a)/sizeof(a[0])) 2312 static const u_int8_t ciphermap[] = { 2313 HAL_CIPHER_WEP, /* IEEE80211_CIPHER_WEP */ 2314 HAL_CIPHER_TKIP, /* IEEE80211_CIPHER_TKIP */ 2315 HAL_CIPHER_AES_OCB, /* IEEE80211_CIPHER_AES_OCB */ 2316 HAL_CIPHER_AES_CCM, /* IEEE80211_CIPHER_AES_CCM */ 2317 (u_int8_t) -1, /* 4 is not allocated */ 2318 HAL_CIPHER_CKIP, /* IEEE80211_CIPHER_CKIP */ 2319 HAL_CIPHER_CLR, /* IEEE80211_CIPHER_NONE */ 2320 }; 2321 struct ath_hal *ah = sc->sc_ah; 2322 const struct ieee80211_cipher *cip = k->wk_cipher; 2323 u_int8_t gmac[IEEE80211_ADDR_LEN]; 2324 const u_int8_t *mac; 2325 HAL_KEYVAL hk; 2326 2327 memset(&hk, 0, sizeof(hk)); 2328 /* 2329 * Software crypto uses a "clear key" so non-crypto 2330 * state kept in the key cache are maintained and 2331 * so that rx frames have an entry to match. 2332 */ 2333 if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) == 0) { 2334 KASSERT(cip->ic_cipher < N(ciphermap), 2335 ("invalid cipher type %u", cip->ic_cipher)); 2336 hk.kv_type = ciphermap[cip->ic_cipher]; 2337 hk.kv_len = k->wk_keylen; 2338 memcpy(hk.kv_val, k->wk_key, k->wk_keylen); 2339 } else 2340 hk.kv_type = HAL_CIPHER_CLR; 2341 2342 if ((k->wk_flags & IEEE80211_KEY_GROUP) && sc->sc_mcastkey) { 2343 /* 2344 * Group keys on hardware that supports multicast frame 2345 * key search use a mac that is the sender's address with 2346 * the high bit set instead of the app-specified address. 2347 */ 2348 IEEE80211_ADDR_COPY(gmac, bss->ni_macaddr); 2349 gmac[0] |= 0x80; 2350 mac = gmac; 2351 } else 2352 mac = k->wk_macaddr; 2353 2354 if (hk.kv_type == HAL_CIPHER_TKIP && 2355 (k->wk_flags & IEEE80211_KEY_SWMIC) == 0) { 2356 return ath_keyset_tkip(sc, k, &hk, mac); 2357 } else { 2358 KEYPRINTF(sc, k->wk_keyix, &hk, mac); 2359 return ath_hal_keyset(ah, k->wk_keyix, &hk, mac); 2360 } 2361 #undef N 2362 } 2363 2364 /* 2365 * Allocate tx/rx key slots for TKIP. We allocate two slots for 2366 * each key, one for decrypt/encrypt and the other for the MIC. 2367 */ 2368 static u_int16_t 2369 key_alloc_2pair(struct ath_softc *sc, 2370 ieee80211_keyix *txkeyix, ieee80211_keyix *rxkeyix) 2371 { 2372 #define N(a) (sizeof(a)/sizeof(a[0])) 2373 u_int i, keyix; 2374 2375 KASSERT(sc->sc_splitmic, ("key cache !split")); 2376 /* XXX could optimize */ 2377 for (i = 0; i < N(sc->sc_keymap)/4; i++) { 2378 u_int8_t b = sc->sc_keymap[i]; 2379 if (b != 0xff) { 2380 /* 2381 * One or more slots in this byte are free. 2382 */ 2383 keyix = i*NBBY; 2384 while (b & 1) { 2385 again: 2386 keyix++; 2387 b >>= 1; 2388 } 2389 /* XXX IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV */ 2390 if (isset(sc->sc_keymap, keyix+32) || 2391 isset(sc->sc_keymap, keyix+64) || 2392 isset(sc->sc_keymap, keyix+32+64)) { 2393 /* full pair unavailable */ 2394 /* XXX statistic */ 2395 if (keyix == (i+1)*NBBY) { 2396 /* no slots were appropriate, advance */ 2397 continue; 2398 } 2399 goto again; 2400 } 2401 setbit(sc->sc_keymap, keyix); 2402 setbit(sc->sc_keymap, keyix+64); 2403 setbit(sc->sc_keymap, keyix+32); 2404 setbit(sc->sc_keymap, keyix+32+64); 2405 DPRINTF(sc, ATH_DEBUG_KEYCACHE, 2406 "%s: key pair %u,%u %u,%u\n", 2407 __func__, keyix, keyix+64, 2408 keyix+32, keyix+32+64); 2409 *txkeyix = keyix; 2410 *rxkeyix = keyix+32; 2411 return 1; 2412 } 2413 } 2414 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of pair space\n", __func__); 2415 return 0; 2416 #undef N 2417 } 2418 2419 /* 2420 * Allocate tx/rx key slots for TKIP. We allocate two slots for 2421 * each key, one for decrypt/encrypt and the other for the MIC. 2422 */ 2423 static u_int16_t 2424 key_alloc_pair(struct ath_softc *sc, 2425 ieee80211_keyix *txkeyix, ieee80211_keyix *rxkeyix) 2426 { 2427 #define N(a) (sizeof(a)/sizeof(a[0])) 2428 u_int i, keyix; 2429 2430 KASSERT(!sc->sc_splitmic, ("key cache split")); 2431 /* XXX could optimize */ 2432 for (i = 0; i < N(sc->sc_keymap)/4; i++) { 2433 u_int8_t b = sc->sc_keymap[i]; 2434 if (b != 0xff) { 2435 /* 2436 * One or more slots in this byte are free. 2437 */ 2438 keyix = i*NBBY; 2439 while (b & 1) { 2440 again: 2441 keyix++; 2442 b >>= 1; 2443 } 2444 if (isset(sc->sc_keymap, keyix+64)) { 2445 /* full pair unavailable */ 2446 /* XXX statistic */ 2447 if (keyix == (i+1)*NBBY) { 2448 /* no slots were appropriate, advance */ 2449 continue; 2450 } 2451 goto again; 2452 } 2453 setbit(sc->sc_keymap, keyix); 2454 setbit(sc->sc_keymap, keyix+64); 2455 DPRINTF(sc, ATH_DEBUG_KEYCACHE, 2456 "%s: key pair %u,%u\n", 2457 __func__, keyix, keyix+64); 2458 *txkeyix = *rxkeyix = keyix; 2459 return 1; 2460 } 2461 } 2462 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of pair space\n", __func__); 2463 return 0; 2464 #undef N 2465 } 2466 2467 /* 2468 * Allocate a single key cache slot. 2469 */ 2470 static int 2471 key_alloc_single(struct ath_softc *sc, 2472 ieee80211_keyix *txkeyix, ieee80211_keyix *rxkeyix) 2473 { 2474 #define N(a) (sizeof(a)/sizeof(a[0])) 2475 u_int i, keyix; 2476 2477 /* XXX try i,i+32,i+64,i+32+64 to minimize key pair conflicts */ 2478 for (i = 0; i < N(sc->sc_keymap); i++) { 2479 u_int8_t b = sc->sc_keymap[i]; 2480 if (b != 0xff) { 2481 /* 2482 * One or more slots are free. 2483 */ 2484 keyix = i*NBBY; 2485 while (b & 1) 2486 keyix++, b >>= 1; 2487 setbit(sc->sc_keymap, keyix); 2488 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: key %u\n", 2489 __func__, keyix); 2490 *txkeyix = *rxkeyix = keyix; 2491 return 1; 2492 } 2493 } 2494 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of space\n", __func__); 2495 return 0; 2496 #undef N 2497 } 2498 2499 /* 2500 * Allocate one or more key cache slots for a uniacst key. The 2501 * key itself is needed only to identify the cipher. For hardware 2502 * TKIP with split cipher+MIC keys we allocate two key cache slot 2503 * pairs so that we can setup separate TX and RX MIC keys. Note 2504 * that the MIC key for a TKIP key at slot i is assumed by the 2505 * hardware to be at slot i+64. This limits TKIP keys to the first 2506 * 64 entries. 2507 */ 2508 static int 2509 ath_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k, 2510 ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix) 2511 { 2512 struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc; 2513 2514 /* 2515 * Group key allocation must be handled specially for 2516 * parts that do not support multicast key cache search 2517 * functionality. For those parts the key id must match 2518 * the h/w key index so lookups find the right key. On 2519 * parts w/ the key search facility we install the sender's 2520 * mac address (with the high bit set) and let the hardware 2521 * find the key w/o using the key id. This is preferred as 2522 * it permits us to support multiple users for adhoc and/or 2523 * multi-station operation. 2524 */ 2525 if (k->wk_keyix != IEEE80211_KEYIX_NONE || /* global key */ 2526 ((k->wk_flags & IEEE80211_KEY_GROUP) && !sc->sc_mcastkey)) { 2527 if (!(&vap->iv_nw_keys[0] <= k && 2528 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) { 2529 /* should not happen */ 2530 DPRINTF(sc, ATH_DEBUG_KEYCACHE, 2531 "%s: bogus group key\n", __func__); 2532 return 0; 2533 } 2534 /* 2535 * XXX we pre-allocate the global keys so 2536 * have no way to check if they've already been allocated. 2537 */ 2538 *keyix = *rxkeyix = k - vap->iv_nw_keys; 2539 return 1; 2540 } 2541 2542 /* 2543 * We allocate two pair for TKIP when using the h/w to do 2544 * the MIC. For everything else, including software crypto, 2545 * we allocate a single entry. Note that s/w crypto requires 2546 * a pass-through slot on the 5211 and 5212. The 5210 does 2547 * not support pass-through cache entries and we map all 2548 * those requests to slot 0. 2549 */ 2550 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) { 2551 return key_alloc_single(sc, keyix, rxkeyix); 2552 } else if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP && 2553 (k->wk_flags & IEEE80211_KEY_SWMIC) == 0) { 2554 if (sc->sc_splitmic) 2555 return key_alloc_2pair(sc, keyix, rxkeyix); 2556 else 2557 return key_alloc_pair(sc, keyix, rxkeyix); 2558 } else { 2559 return key_alloc_single(sc, keyix, rxkeyix); 2560 } 2561 } 2562 2563 /* 2564 * Delete an entry in the key cache allocated by ath_key_alloc. 2565 */ 2566 static int 2567 ath_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k) 2568 { 2569 struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc; 2570 struct ath_hal *ah = sc->sc_ah; 2571 const struct ieee80211_cipher *cip = k->wk_cipher; 2572 u_int keyix = k->wk_keyix; 2573 2574 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: delete key %u\n", __func__, keyix); 2575 2576 ath_hal_keyreset(ah, keyix); 2577 /* 2578 * Handle split tx/rx keying required for TKIP with h/w MIC. 2579 */ 2580 if (cip->ic_cipher == IEEE80211_CIPHER_TKIP && 2581 (k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && sc->sc_splitmic) 2582 ath_hal_keyreset(ah, keyix+32); /* RX key */ 2583 if (keyix >= IEEE80211_WEP_NKID) { 2584 /* 2585 * Don't touch keymap entries for global keys so 2586 * they are never considered for dynamic allocation. 2587 */ 2588 clrbit(sc->sc_keymap, keyix); 2589 if (cip->ic_cipher == IEEE80211_CIPHER_TKIP && 2590 (k->wk_flags & IEEE80211_KEY_SWMIC) == 0) { 2591 clrbit(sc->sc_keymap, keyix+64); /* TX key MIC */ 2592 if (sc->sc_splitmic) { 2593 /* +32 for RX key, +32+64 for RX key MIC */ 2594 clrbit(sc->sc_keymap, keyix+32); 2595 clrbit(sc->sc_keymap, keyix+32+64); 2596 } 2597 } 2598 } 2599 return 1; 2600 } 2601 2602 /* 2603 * Set the key cache contents for the specified key. Key cache 2604 * slot(s) must already have been allocated by ath_key_alloc. 2605 */ 2606 static int 2607 ath_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k, 2608 const u_int8_t mac[IEEE80211_ADDR_LEN]) 2609 { 2610 struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc; 2611 2612 return ath_keyset(sc, k, vap->iv_bss); 2613 } 2614 2615 /* 2616 * Block/unblock tx+rx processing while a key change is done. 2617 * We assume the caller serializes key management operations 2618 * so we only need to worry about synchronization with other 2619 * uses that originate in the driver. 2620 */ 2621 static void 2622 ath_key_update_begin(struct ieee80211vap *vap) 2623 { 2624 struct ifnet *ifp = vap->iv_ic->ic_ifp; 2625 struct ath_softc *sc = ifp->if_softc; 2626 2627 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__); 2628 taskqueue_block(sc->sc_tq); 2629 IF_LOCK(&ifp->if_snd); /* NB: doesn't block mgmt frames */ 2630 } 2631 2632 static void 2633 ath_key_update_end(struct ieee80211vap *vap) 2634 { 2635 struct ifnet *ifp = vap->iv_ic->ic_ifp; 2636 struct ath_softc *sc = ifp->if_softc; 2637 2638 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__); 2639 IF_UNLOCK(&ifp->if_snd); 2640 taskqueue_unblock(sc->sc_tq); 2641 } 2642 2643 /* 2644 * Calculate the receive filter according to the 2645 * operating mode and state: 2646 * 2647 * o always accept unicast, broadcast, and multicast traffic 2648 * o accept PHY error frames when hardware doesn't have MIB support 2649 * to count and we need them for ANI (sta mode only until recently) 2650 * and we are not scanning (ANI is disabled) 2651 * NB: older hal's add rx filter bits out of sight and we need to 2652 * blindly preserve them 2653 * o probe request frames are accepted only when operating in 2654 * hostap, adhoc, or monitor modes 2655 * o enable promiscuous mode 2656 * - when in monitor mode 2657 * - if interface marked PROMISC (assumes bridge setting is filtered) 2658 * o accept beacons: 2659 * - when operating in station mode for collecting rssi data when 2660 * the station is otherwise quiet, or 2661 * - when operating in adhoc mode so the 802.11 layer creates 2662 * node table entries for peers, 2663 * - when scanning 2664 * - when doing s/w beacon miss (e.g. for ap+sta) 2665 * - when operating in ap mode in 11g to detect overlapping bss that 2666 * require protection 2667 * o accept control frames: 2668 * - when in monitor mode 2669 * XXX BAR frames for 11n 2670 * XXX HT protection for 11n 2671 */ 2672 static u_int32_t 2673 ath_calcrxfilter(struct ath_softc *sc) 2674 { 2675 struct ifnet *ifp = sc->sc_ifp; 2676 struct ieee80211com *ic = ifp->if_l2com; 2677 u_int32_t rfilt; 2678 2679 rfilt = HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST; 2680 if (!sc->sc_needmib && !sc->sc_scanning) 2681 rfilt |= HAL_RX_FILTER_PHYERR; 2682 if (ic->ic_opmode != IEEE80211_M_STA) 2683 rfilt |= HAL_RX_FILTER_PROBEREQ; 2684 if (ic->ic_opmode == IEEE80211_M_MONITOR || (ifp->if_flags & IFF_PROMISC)) 2685 rfilt |= HAL_RX_FILTER_PROM; 2686 if (ic->ic_opmode == IEEE80211_M_STA || 2687 ic->ic_opmode == IEEE80211_M_IBSS || 2688 sc->sc_swbmiss || sc->sc_scanning) 2689 rfilt |= HAL_RX_FILTER_BEACON; 2690 /* 2691 * NB: We don't recalculate the rx filter when 2692 * ic_protmode changes; otherwise we could do 2693 * this only when ic_protmode != NONE. 2694 */ 2695 if (ic->ic_opmode == IEEE80211_M_HOSTAP && 2696 IEEE80211_IS_CHAN_ANYG(ic->ic_curchan)) 2697 rfilt |= HAL_RX_FILTER_BEACON; 2698 if (ic->ic_opmode == IEEE80211_M_MONITOR) 2699 rfilt |= HAL_RX_FILTER_CONTROL; 2700 DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x, %s if_flags 0x%x\n", 2701 __func__, rfilt, ieee80211_opmode_name[ic->ic_opmode], ifp->if_flags); 2702 return rfilt; 2703 } 2704 2705 static void 2706 ath_update_promisc(struct ifnet *ifp) 2707 { 2708 struct ath_softc *sc = ifp->if_softc; 2709 u_int32_t rfilt; 2710 2711 /* configure rx filter */ 2712 rfilt = ath_calcrxfilter(sc); 2713 ath_hal_setrxfilter(sc->sc_ah, rfilt); 2714 2715 DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x\n", __func__, rfilt); 2716 } 2717 2718 static void 2719 ath_update_mcast(struct ifnet *ifp) 2720 { 2721 struct ath_softc *sc = ifp->if_softc; 2722 u_int32_t mfilt[2]; 2723 2724 /* calculate and install multicast filter */ 2725 if ((ifp->if_flags & IFF_ALLMULTI) == 0) { 2726 struct ifmultiaddr *ifma; 2727 /* 2728 * Merge multicast addresses to form the hardware filter. 2729 */ 2730 mfilt[0] = mfilt[1] = 0; 2731 IF_ADDR_LOCK(ifp); /* XXX need some fiddling to remove? */ 2732 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 2733 caddr_t dl; 2734 u_int32_t val; 2735 u_int8_t pos; 2736 2737 /* calculate XOR of eight 6bit values */ 2738 dl = LLADDR((struct sockaddr_dl *) ifma->ifma_addr); 2739 val = LE_READ_4(dl + 0); 2740 pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val; 2741 val = LE_READ_4(dl + 3); 2742 pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val; 2743 pos &= 0x3f; 2744 mfilt[pos / 32] |= (1 << (pos % 32)); 2745 } 2746 IF_ADDR_UNLOCK(ifp); 2747 } else 2748 mfilt[0] = mfilt[1] = ~0; 2749 ath_hal_setmcastfilter(sc->sc_ah, mfilt[0], mfilt[1]); 2750 DPRINTF(sc, ATH_DEBUG_MODE, "%s: MC filter %08x:%08x\n", 2751 __func__, mfilt[0], mfilt[1]); 2752 } 2753 2754 static void 2755 ath_mode_init(struct ath_softc *sc) 2756 { 2757 struct ifnet *ifp = sc->sc_ifp; 2758 struct ieee80211com *ic = ifp->if_l2com; 2759 struct ath_hal *ah = sc->sc_ah; 2760 u_int32_t rfilt; 2761 2762 /* configure rx filter */ 2763 rfilt = ath_calcrxfilter(sc); 2764 ath_hal_setrxfilter(ah, rfilt); 2765 2766 /* configure operational mode */ 2767 ath_hal_setopmode(ah); 2768 2769 /* 2770 * Handle any link-level address change. Note that we only 2771 * need to force ic_myaddr; any other addresses are handled 2772 * as a byproduct of the ifnet code marking the interface 2773 * down then up. 2774 * 2775 * XXX should get from lladdr instead of arpcom but that's more work 2776 */ 2777 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp)); 2778 ath_hal_setmac(ah, ic->ic_myaddr); 2779 2780 /* calculate and install multicast filter */ 2781 ath_update_mcast(ifp); 2782 } 2783 2784 /* 2785 * Set the slot time based on the current setting. 2786 */ 2787 static void 2788 ath_setslottime(struct ath_softc *sc) 2789 { 2790 struct ieee80211com *ic = sc->sc_ifp->if_l2com; 2791 struct ath_hal *ah = sc->sc_ah; 2792 u_int usec; 2793 2794 if (IEEE80211_IS_CHAN_HALF(ic->ic_curchan)) 2795 usec = 13; 2796 else if (IEEE80211_IS_CHAN_QUARTER(ic->ic_curchan)) 2797 usec = 21; 2798 else if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan)) { 2799 /* honor short/long slot time only in 11g */ 2800 /* XXX shouldn't honor on pure g or turbo g channel */ 2801 if (ic->ic_flags & IEEE80211_F_SHSLOT) 2802 usec = HAL_SLOT_TIME_9; 2803 else 2804 usec = HAL_SLOT_TIME_20; 2805 } else 2806 usec = HAL_SLOT_TIME_9; 2807 2808 DPRINTF(sc, ATH_DEBUG_RESET, 2809 "%s: chan %u MHz flags 0x%x %s slot, %u usec\n", 2810 __func__, ic->ic_curchan->ic_freq, ic->ic_curchan->ic_flags, 2811 ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", usec); 2812 2813 ath_hal_setslottime(ah, usec); 2814 sc->sc_updateslot = OK; 2815 } 2816 2817 /* 2818 * Callback from the 802.11 layer to update the 2819 * slot time based on the current setting. 2820 */ 2821 static void 2822 ath_updateslot(struct ifnet *ifp) 2823 { 2824 struct ath_softc *sc = ifp->if_softc; 2825 struct ieee80211com *ic = ifp->if_l2com; 2826 2827 /* 2828 * When not coordinating the BSS, change the hardware 2829 * immediately. For other operation we defer the change 2830 * until beacon updates have propagated to the stations. 2831 */ 2832 if (ic->ic_opmode == IEEE80211_M_HOSTAP) 2833 sc->sc_updateslot = UPDATE; 2834 else 2835 ath_setslottime(sc); 2836 } 2837 2838 /* 2839 * Setup a h/w transmit queue for beacons. 2840 */ 2841 static int 2842 ath_beaconq_setup(struct ath_hal *ah) 2843 { 2844 HAL_TXQ_INFO qi; 2845 2846 memset(&qi, 0, sizeof(qi)); 2847 qi.tqi_aifs = HAL_TXQ_USEDEFAULT; 2848 qi.tqi_cwmin = HAL_TXQ_USEDEFAULT; 2849 qi.tqi_cwmax = HAL_TXQ_USEDEFAULT; 2850 /* NB: for dynamic turbo, don't enable any other interrupts */ 2851 qi.tqi_qflags = HAL_TXQ_TXDESCINT_ENABLE; 2852 return ath_hal_setuptxqueue(ah, HAL_TX_QUEUE_BEACON, &qi); 2853 } 2854 2855 /* 2856 * Setup the transmit queue parameters for the beacon queue. 2857 */ 2858 static int 2859 ath_beaconq_config(struct ath_softc *sc) 2860 { 2861 #define ATH_EXPONENT_TO_VALUE(v) ((1<<(v))-1) 2862 struct ieee80211com *ic = sc->sc_ifp->if_l2com; 2863 struct ath_hal *ah = sc->sc_ah; 2864 HAL_TXQ_INFO qi; 2865 2866 ath_hal_gettxqueueprops(ah, sc->sc_bhalq, &qi); 2867 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 2868 /* 2869 * Always burst out beacon and CAB traffic. 2870 */ 2871 qi.tqi_aifs = ATH_BEACON_AIFS_DEFAULT; 2872 qi.tqi_cwmin = ATH_BEACON_CWMIN_DEFAULT; 2873 qi.tqi_cwmax = ATH_BEACON_CWMAX_DEFAULT; 2874 } else { 2875 struct wmeParams *wmep = 2876 &ic->ic_wme.wme_chanParams.cap_wmeParams[WME_AC_BE]; 2877 /* 2878 * Adhoc mode; important thing is to use 2x cwmin. 2879 */ 2880 qi.tqi_aifs = wmep->wmep_aifsn; 2881 qi.tqi_cwmin = 2*ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin); 2882 qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax); 2883 } 2884 2885 if (!ath_hal_settxqueueprops(ah, sc->sc_bhalq, &qi)) { 2886 device_printf(sc->sc_dev, "unable to update parameters for " 2887 "beacon hardware queue!\n"); 2888 return 0; 2889 } else { 2890 ath_hal_resettxqueue(ah, sc->sc_bhalq); /* push to h/w */ 2891 return 1; 2892 } 2893 #undef ATH_EXPONENT_TO_VALUE 2894 } 2895 2896 /* 2897 * Allocate and setup an initial beacon frame. 2898 */ 2899 static int 2900 ath_beacon_alloc(struct ath_softc *sc, struct ieee80211_node *ni) 2901 { 2902 struct ieee80211vap *vap = ni->ni_vap; 2903 struct ath_vap *avp = ATH_VAP(vap); 2904 struct ath_buf *bf; 2905 struct mbuf *m; 2906 int error; 2907 2908 bf = avp->av_bcbuf; 2909 if (bf->bf_m != NULL) { 2910 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 2911 m_freem(bf->bf_m); 2912 bf->bf_m = NULL; 2913 } 2914 if (bf->bf_node != NULL) { 2915 ieee80211_free_node(bf->bf_node); 2916 bf->bf_node = NULL; 2917 } 2918 2919 /* 2920 * NB: the beacon data buffer must be 32-bit aligned; 2921 * we assume the mbuf routines will return us something 2922 * with this alignment (perhaps should assert). 2923 */ 2924 m = ieee80211_beacon_alloc(ni, &avp->av_boff); 2925 if (m == NULL) { 2926 device_printf(sc->sc_dev, "%s: cannot get mbuf\n", __func__); 2927 sc->sc_stats.ast_be_nombuf++; 2928 return ENOMEM; 2929 } 2930 error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m, 2931 bf->bf_segs, &bf->bf_nseg, 2932 BUS_DMA_NOWAIT); 2933 if (error != 0) { 2934 device_printf(sc->sc_dev, 2935 "%s: cannot map mbuf, bus_dmamap_load_mbuf_sg returns %d\n", 2936 __func__, error); 2937 m_freem(m); 2938 return error; 2939 } 2940 2941 /* 2942 * Calculate a TSF adjustment factor required for staggered 2943 * beacons. Note that we assume the format of the beacon 2944 * frame leaves the tstamp field immediately following the 2945 * header. 2946 */ 2947 if (sc->sc_stagbeacons && avp->av_bslot > 0) { 2948 uint64_t tsfadjust; 2949 struct ieee80211_frame *wh; 2950 2951 /* 2952 * The beacon interval is in TU's; the TSF is in usecs. 2953 * We figure out how many TU's to add to align the timestamp 2954 * then convert to TSF units and handle byte swapping before 2955 * inserting it in the frame. The hardware will then add this 2956 * each time a beacon frame is sent. Note that we align vap's 2957 * 1..N and leave vap 0 untouched. This means vap 0 has a 2958 * timestamp in one beacon interval while the others get a 2959 * timstamp aligned to the next interval. 2960 */ 2961 tsfadjust = ni->ni_intval * 2962 (ATH_BCBUF - avp->av_bslot) / ATH_BCBUF; 2963 tsfadjust = htole64(tsfadjust << 10); /* TU -> TSF */ 2964 2965 DPRINTF(sc, ATH_DEBUG_BEACON, 2966 "%s: %s beacons bslot %d intval %u tsfadjust %llu\n", 2967 __func__, sc->sc_stagbeacons ? "stagger" : "burst", 2968 avp->av_bslot, ni->ni_intval, 2969 (long long unsigned) le64toh(tsfadjust)); 2970 2971 wh = mtod(m, struct ieee80211_frame *); 2972 memcpy(&wh[1], &tsfadjust, sizeof(tsfadjust)); 2973 } 2974 bf->bf_m = m; 2975 bf->bf_node = ieee80211_ref_node(ni); 2976 2977 return 0; 2978 } 2979 2980 /* 2981 * Setup the beacon frame for transmit. 2982 */ 2983 static void 2984 ath_beacon_setup(struct ath_softc *sc, struct ath_buf *bf) 2985 { 2986 #define USE_SHPREAMBLE(_ic) \ 2987 (((_ic)->ic_flags & (IEEE80211_F_SHPREAMBLE | IEEE80211_F_USEBARKER))\ 2988 == IEEE80211_F_SHPREAMBLE) 2989 struct ieee80211_node *ni = bf->bf_node; 2990 struct ieee80211com *ic = ni->ni_ic; 2991 struct mbuf *m = bf->bf_m; 2992 struct ath_hal *ah = sc->sc_ah; 2993 struct ath_desc *ds; 2994 int flags, antenna; 2995 const HAL_RATE_TABLE *rt; 2996 u_int8_t rix, rate; 2997 2998 DPRINTF(sc, ATH_DEBUG_BEACON_PROC, "%s: m %p len %u\n", 2999 __func__, m, m->m_len); 3000 3001 /* setup descriptors */ 3002 ds = bf->bf_desc; 3003 3004 flags = HAL_TXDESC_NOACK; 3005 if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_hasveol) { 3006 ds->ds_link = bf->bf_daddr; /* self-linked */ 3007 flags |= HAL_TXDESC_VEOL; 3008 /* 3009 * Let hardware handle antenna switching. 3010 */ 3011 antenna = sc->sc_txantenna; 3012 } else { 3013 ds->ds_link = 0; 3014 /* 3015 * Switch antenna every 4 beacons. 3016 * XXX assumes two antenna 3017 */ 3018 if (sc->sc_txantenna != 0) 3019 antenna = sc->sc_txantenna; 3020 else if (sc->sc_stagbeacons && sc->sc_nbcnvaps != 0) 3021 antenna = ((sc->sc_stats.ast_be_xmit / sc->sc_nbcnvaps) & 4 ? 2 : 1); 3022 else 3023 antenna = (sc->sc_stats.ast_be_xmit & 4 ? 2 : 1); 3024 } 3025 3026 KASSERT(bf->bf_nseg == 1, 3027 ("multi-segment beacon frame; nseg %u", bf->bf_nseg)); 3028 ds->ds_data = bf->bf_segs[0].ds_addr; 3029 /* 3030 * Calculate rate code. 3031 * XXX everything at min xmit rate 3032 */ 3033 rix = 0; 3034 rt = sc->sc_currates; 3035 rate = rt->info[rix].rateCode; 3036 if (USE_SHPREAMBLE(ic)) 3037 rate |= rt->info[rix].shortPreamble; 3038 ath_hal_setuptxdesc(ah, ds 3039 , m->m_len + IEEE80211_CRC_LEN /* frame length */ 3040 , sizeof(struct ieee80211_frame)/* header length */ 3041 , HAL_PKT_TYPE_BEACON /* Atheros packet type */ 3042 , ni->ni_txpower /* txpower XXX */ 3043 , rate, 1 /* series 0 rate/tries */ 3044 , HAL_TXKEYIX_INVALID /* no encryption */ 3045 , antenna /* antenna mode */ 3046 , flags /* no ack, veol for beacons */ 3047 , 0 /* rts/cts rate */ 3048 , 0 /* rts/cts duration */ 3049 ); 3050 /* NB: beacon's BufLen must be a multiple of 4 bytes */ 3051 ath_hal_filltxdesc(ah, ds 3052 , roundup(m->m_len, 4) /* buffer length */ 3053 , AH_TRUE /* first segment */ 3054 , AH_TRUE /* last segment */ 3055 , ds /* first descriptor */ 3056 ); 3057 #if 0 3058 ath_desc_swap(ds); 3059 #endif 3060 #undef USE_SHPREAMBLE 3061 } 3062 3063 static void 3064 ath_beacon_update(struct ieee80211vap *vap, int item) 3065 { 3066 struct ieee80211_beacon_offsets *bo = &ATH_VAP(vap)->av_boff; 3067 3068 setbit(bo->bo_flags, item); 3069 } 3070 3071 /* 3072 * Append the contents of src to dst; both queues 3073 * are assumed to be locked. 3074 */ 3075 static void 3076 ath_txqmove(struct ath_txq *dst, struct ath_txq *src) 3077 { 3078 STAILQ_CONCAT(&dst->axq_q, &src->axq_q); 3079 dst->axq_link = src->axq_link; 3080 src->axq_link = NULL; 3081 dst->axq_depth += src->axq_depth; 3082 src->axq_depth = 0; 3083 } 3084 3085 /* 3086 * Transmit a beacon frame at SWBA. Dynamic updates to the 3087 * frame contents are done as needed and the slot time is 3088 * also adjusted based on current state. 3089 */ 3090 static void 3091 ath_beacon_proc(void *arg, int pending) 3092 { 3093 struct ath_softc *sc = arg; 3094 struct ath_hal *ah = sc->sc_ah; 3095 struct ieee80211vap *vap; 3096 struct ath_buf *bf; 3097 int slot, otherant; 3098 uint32_t bfaddr; 3099 3100 DPRINTF(sc, ATH_DEBUG_BEACON_PROC, "%s: pending %u\n", 3101 __func__, pending); 3102 /* 3103 * Check if the previous beacon has gone out. If 3104 * not don't try to post another, skip this period 3105 * and wait for the next. Missed beacons indicate 3106 * a problem and should not occur. If we miss too 3107 * many consecutive beacons reset the device. 3108 */ 3109 if (ath_hal_numtxpending(ah, sc->sc_bhalq) != 0) { 3110 sc->sc_bmisscount++; 3111 DPRINTF(sc, ATH_DEBUG_BEACON, 3112 "%s: missed %u consecutive beacons\n", 3113 __func__, sc->sc_bmisscount); 3114 if (sc->sc_bmisscount >= ath_bstuck_threshold) 3115 taskqueue_enqueue(sc->sc_tq, &sc->sc_bstucktask); 3116 return; 3117 } 3118 if (sc->sc_bmisscount != 0) { 3119 DPRINTF(sc, ATH_DEBUG_BEACON, 3120 "%s: resume beacon xmit after %u misses\n", 3121 __func__, sc->sc_bmisscount); 3122 sc->sc_bmisscount = 0; 3123 } 3124 3125 if (sc->sc_stagbeacons) { /* staggered beacons */ 3126 struct ieee80211com *ic = sc->sc_ifp->if_l2com; 3127 uint32_t tsftu; 3128 3129 tsftu = ath_hal_gettsf32(ah) >> 10; 3130 /* XXX lintval */ 3131 slot = ((tsftu % ic->ic_lintval) * ATH_BCBUF) / ic->ic_lintval; 3132 vap = sc->sc_bslot[(slot+1) % ATH_BCBUF]; 3133 bfaddr = 0; 3134 if (vap != NULL && vap->iv_state == IEEE80211_S_RUN) { 3135 bf = ath_beacon_generate(sc, vap); 3136 if (bf != NULL) 3137 bfaddr = bf->bf_daddr; 3138 } 3139 } else { /* burst'd beacons */ 3140 uint32_t *bflink = &bfaddr; 3141 3142 for (slot = 0; slot < ATH_BCBUF; slot++) { 3143 vap = sc->sc_bslot[slot]; 3144 if (vap != NULL && vap->iv_state == IEEE80211_S_RUN) { 3145 bf = ath_beacon_generate(sc, vap); 3146 if (bf != NULL) { 3147 *bflink = bf->bf_daddr; 3148 bflink = &bf->bf_desc->ds_link; 3149 } 3150 } 3151 } 3152 *bflink = 0; /* terminate list */ 3153 } 3154 3155 /* 3156 * Handle slot time change when a non-ERP station joins/leaves 3157 * an 11g network. The 802.11 layer notifies us via callback, 3158 * we mark updateslot, then wait one beacon before effecting 3159 * the change. This gives associated stations at least one 3160 * beacon interval to note the state change. 3161 */ 3162 /* XXX locking */ 3163 if (sc->sc_updateslot == UPDATE) { 3164 sc->sc_updateslot = COMMIT; /* commit next beacon */ 3165 sc->sc_slotupdate = slot; 3166 } else if (sc->sc_updateslot == COMMIT && sc->sc_slotupdate == slot) 3167 ath_setslottime(sc); /* commit change to h/w */ 3168 3169 /* 3170 * Check recent per-antenna transmit statistics and flip 3171 * the default antenna if noticeably more frames went out 3172 * on the non-default antenna. 3173 * XXX assumes 2 anntenae 3174 */ 3175 if (!sc->sc_diversity && (!sc->sc_stagbeacons || slot == 0)) { 3176 otherant = sc->sc_defant & 1 ? 2 : 1; 3177 if (sc->sc_ant_tx[otherant] > sc->sc_ant_tx[sc->sc_defant] + 2) 3178 ath_setdefantenna(sc, otherant); 3179 sc->sc_ant_tx[1] = sc->sc_ant_tx[2] = 0; 3180 } 3181 3182 if (bfaddr != 0) { 3183 /* 3184 * Stop any current dma and put the new frame on the queue. 3185 * This should never fail since we check above that no frames 3186 * are still pending on the queue. 3187 */ 3188 if (!ath_hal_stoptxdma(ah, sc->sc_bhalq)) { 3189 DPRINTF(sc, ATH_DEBUG_ANY, 3190 "%s: beacon queue %u did not stop?\n", 3191 __func__, sc->sc_bhalq); 3192 } 3193 /* NB: cabq traffic should already be queued and primed */ 3194 ath_hal_puttxbuf(ah, sc->sc_bhalq, bfaddr); 3195 ath_hal_txstart(ah, sc->sc_bhalq); 3196 3197 sc->sc_stats.ast_be_xmit++; 3198 } 3199 } 3200 3201 static struct ath_buf * 3202 ath_beacon_generate(struct ath_softc *sc, struct ieee80211vap *vap) 3203 { 3204 struct ath_vap *avp = ATH_VAP(vap); 3205 struct ath_txq *cabq = sc->sc_cabq; 3206 struct ath_buf *bf; 3207 struct mbuf *m; 3208 int nmcastq, error; 3209 3210 KASSERT(vap->iv_state == IEEE80211_S_RUN, 3211 ("not running, state %d", vap->iv_state)); 3212 KASSERT(avp->av_bcbuf != NULL, ("no beacon buffer")); 3213 3214 /* 3215 * Update dynamic beacon contents. If this returns 3216 * non-zero then we need to remap the memory because 3217 * the beacon frame changed size (probably because 3218 * of the TIM bitmap). 3219 */ 3220 bf = avp->av_bcbuf; 3221 m = bf->bf_m; 3222 nmcastq = avp->av_mcastq.axq_depth; 3223 if (ieee80211_beacon_update(bf->bf_node, &avp->av_boff, m, nmcastq)) { 3224 /* XXX too conservative? */ 3225 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 3226 error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m, 3227 bf->bf_segs, &bf->bf_nseg, 3228 BUS_DMA_NOWAIT); 3229 if (error != 0) { 3230 if_printf(vap->iv_ifp, 3231 "%s: bus_dmamap_load_mbuf_sg failed, error %u\n", 3232 __func__, error); 3233 return NULL; 3234 } 3235 } 3236 if ((avp->av_boff.bo_tim[4] & 1) && cabq->axq_depth) { 3237 DPRINTF(sc, ATH_DEBUG_BEACON, 3238 "%s: cabq did not drain, mcastq %u cabq %u\n", 3239 __func__, nmcastq, cabq->axq_depth); 3240 sc->sc_stats.ast_cabq_busy++; 3241 if (sc->sc_nvaps > 1 && sc->sc_stagbeacons) { 3242 /* 3243 * CABQ traffic from a previous vap is still pending. 3244 * We must drain the q before this beacon frame goes 3245 * out as otherwise this vap's stations will get cab 3246 * frames from a different vap. 3247 * XXX could be slow causing us to miss DBA 3248 */ 3249 ath_tx_draintxq(sc, cabq); 3250 } 3251 } 3252 ath_beacon_setup(sc, bf); 3253 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE); 3254 3255 /* 3256 * Enable the CAB queue before the beacon queue to 3257 * insure cab frames are triggered by this beacon. 3258 */ 3259 if (avp->av_boff.bo_tim[4] & 1) { 3260 struct ath_hal *ah = sc->sc_ah; 3261 3262 /* NB: only at DTIM */ 3263 ATH_TXQ_LOCK(cabq); 3264 ATH_TXQ_LOCK(&avp->av_mcastq); 3265 if (nmcastq) { 3266 struct ath_buf *bfm; 3267 3268 /* 3269 * Move frames from the s/w mcast q to the h/w cab q. 3270 * XXX MORE_DATA bit 3271 */ 3272 bfm = STAILQ_FIRST(&avp->av_mcastq.axq_q); 3273 if (cabq->axq_link != NULL) { 3274 *cabq->axq_link = bfm->bf_daddr; 3275 } else 3276 ath_hal_puttxbuf(ah, cabq->axq_qnum, 3277 bfm->bf_daddr); 3278 ath_txqmove(cabq, &avp->av_mcastq); 3279 3280 sc->sc_stats.ast_cabq_xmit += nmcastq; 3281 } 3282 /* NB: gated by beacon so safe to start here */ 3283 ath_hal_txstart(ah, cabq->axq_qnum); 3284 ATH_TXQ_UNLOCK(cabq); 3285 ATH_TXQ_UNLOCK(&avp->av_mcastq); 3286 } 3287 return bf; 3288 } 3289 3290 static void 3291 ath_beacon_start_adhoc(struct ath_softc *sc, struct ieee80211vap *vap) 3292 { 3293 struct ath_vap *avp = ATH_VAP(vap); 3294 struct ath_hal *ah = sc->sc_ah; 3295 struct ath_buf *bf; 3296 struct mbuf *m; 3297 int error; 3298 3299 KASSERT(avp->av_bcbuf != NULL, ("no beacon buffer")); 3300 3301 /* 3302 * Update dynamic beacon contents. If this returns 3303 * non-zero then we need to remap the memory because 3304 * the beacon frame changed size (probably because 3305 * of the TIM bitmap). 3306 */ 3307 bf = avp->av_bcbuf; 3308 m = bf->bf_m; 3309 if (ieee80211_beacon_update(bf->bf_node, &avp->av_boff, m, 0)) { 3310 /* XXX too conservative? */ 3311 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 3312 error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m, 3313 bf->bf_segs, &bf->bf_nseg, 3314 BUS_DMA_NOWAIT); 3315 if (error != 0) { 3316 if_printf(vap->iv_ifp, 3317 "%s: bus_dmamap_load_mbuf_sg failed, error %u\n", 3318 __func__, error); 3319 return; 3320 } 3321 } 3322 ath_beacon_setup(sc, bf); 3323 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE); 3324 3325 /* NB: caller is known to have already stopped tx dma */ 3326 ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr); 3327 ath_hal_txstart(ah, sc->sc_bhalq); 3328 } 3329 3330 /* 3331 * Reset the hardware after detecting beacons have stopped. 3332 */ 3333 static void 3334 ath_bstuck_proc(void *arg, int pending) 3335 { 3336 struct ath_softc *sc = arg; 3337 struct ifnet *ifp = sc->sc_ifp; 3338 3339 if_printf(ifp, "stuck beacon; resetting (bmiss count %u)\n", 3340 sc->sc_bmisscount); 3341 sc->sc_stats.ast_bstuck++; 3342 ath_reset(ifp); 3343 } 3344 3345 /* 3346 * Reclaim beacon resources and return buffer to the pool. 3347 */ 3348 static void 3349 ath_beacon_return(struct ath_softc *sc, struct ath_buf *bf) 3350 { 3351 3352 if (bf->bf_m != NULL) { 3353 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 3354 m_freem(bf->bf_m); 3355 bf->bf_m = NULL; 3356 } 3357 if (bf->bf_node != NULL) { 3358 ieee80211_free_node(bf->bf_node); 3359 bf->bf_node = NULL; 3360 } 3361 STAILQ_INSERT_TAIL(&sc->sc_bbuf, bf, bf_list); 3362 } 3363 3364 /* 3365 * Reclaim beacon resources. 3366 */ 3367 static void 3368 ath_beacon_free(struct ath_softc *sc) 3369 { 3370 struct ath_buf *bf; 3371 3372 STAILQ_FOREACH(bf, &sc->sc_bbuf, bf_list) { 3373 if (bf->bf_m != NULL) { 3374 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 3375 m_freem(bf->bf_m); 3376 bf->bf_m = NULL; 3377 } 3378 if (bf->bf_node != NULL) { 3379 ieee80211_free_node(bf->bf_node); 3380 bf->bf_node = NULL; 3381 } 3382 } 3383 } 3384 3385 /* 3386 * Configure the beacon and sleep timers. 3387 * 3388 * When operating as an AP this resets the TSF and sets 3389 * up the hardware to notify us when we need to issue beacons. 3390 * 3391 * When operating in station mode this sets up the beacon 3392 * timers according to the timestamp of the last received 3393 * beacon and the current TSF, configures PCF and DTIM 3394 * handling, programs the sleep registers so the hardware 3395 * will wakeup in time to receive beacons, and configures 3396 * the beacon miss handling so we'll receive a BMISS 3397 * interrupt when we stop seeing beacons from the AP 3398 * we've associated with. 3399 */ 3400 static void 3401 ath_beacon_config(struct ath_softc *sc, struct ieee80211vap *vap) 3402 { 3403 #define TSF_TO_TU(_h,_l) \ 3404 ((((u_int32_t)(_h)) << 22) | (((u_int32_t)(_l)) >> 10)) 3405 #define FUDGE 2 3406 struct ath_hal *ah = sc->sc_ah; 3407 struct ieee80211com *ic = sc->sc_ifp->if_l2com; 3408 struct ieee80211_node *ni; 3409 u_int32_t nexttbtt, intval, tsftu; 3410 u_int64_t tsf; 3411 3412 if (vap == NULL) 3413 vap = TAILQ_FIRST(&ic->ic_vaps); /* XXX */ 3414 ni = vap->iv_bss; 3415 3416 /* extract tstamp from last beacon and convert to TU */ 3417 nexttbtt = TSF_TO_TU(LE_READ_4(ni->ni_tstamp.data + 4), 3418 LE_READ_4(ni->ni_tstamp.data)); 3419 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 3420 /* 3421 * For multi-bss ap support beacons are either staggered 3422 * evenly over N slots or burst together. For the former 3423 * arrange for the SWBA to be delivered for each slot. 3424 * Slots that are not occupied will generate nothing. 3425 */ 3426 /* NB: the beacon interval is kept internally in TU's */ 3427 intval = ni->ni_intval & HAL_BEACON_PERIOD; 3428 if (sc->sc_stagbeacons) 3429 intval /= ATH_BCBUF; 3430 } else { 3431 /* NB: the beacon interval is kept internally in TU's */ 3432 intval = ni->ni_intval & HAL_BEACON_PERIOD; 3433 } 3434 if (nexttbtt == 0) /* e.g. for ap mode */ 3435 nexttbtt = intval; 3436 else if (intval) /* NB: can be 0 for monitor mode */ 3437 nexttbtt = roundup(nexttbtt, intval); 3438 DPRINTF(sc, ATH_DEBUG_BEACON, "%s: nexttbtt %u intval %u (%u)\n", 3439 __func__, nexttbtt, intval, ni->ni_intval); 3440 if (ic->ic_opmode == IEEE80211_M_STA && !sc->sc_swbmiss) { 3441 HAL_BEACON_STATE bs; 3442 int dtimperiod, dtimcount; 3443 int cfpperiod, cfpcount; 3444 3445 /* 3446 * Setup dtim and cfp parameters according to 3447 * last beacon we received (which may be none). 3448 */ 3449 dtimperiod = ni->ni_dtim_period; 3450 if (dtimperiod <= 0) /* NB: 0 if not known */ 3451 dtimperiod = 1; 3452 dtimcount = ni->ni_dtim_count; 3453 if (dtimcount >= dtimperiod) /* NB: sanity check */ 3454 dtimcount = 0; /* XXX? */ 3455 cfpperiod = 1; /* NB: no PCF support yet */ 3456 cfpcount = 0; 3457 /* 3458 * Pull nexttbtt forward to reflect the current 3459 * TSF and calculate dtim+cfp state for the result. 3460 */ 3461 tsf = ath_hal_gettsf64(ah); 3462 tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE; 3463 do { 3464 nexttbtt += intval; 3465 if (--dtimcount < 0) { 3466 dtimcount = dtimperiod - 1; 3467 if (--cfpcount < 0) 3468 cfpcount = cfpperiod - 1; 3469 } 3470 } while (nexttbtt < tsftu); 3471 memset(&bs, 0, sizeof(bs)); 3472 bs.bs_intval = intval; 3473 bs.bs_nexttbtt = nexttbtt; 3474 bs.bs_dtimperiod = dtimperiod*intval; 3475 bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval; 3476 bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod; 3477 bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod; 3478 bs.bs_cfpmaxduration = 0; 3479 #if 0 3480 /* 3481 * The 802.11 layer records the offset to the DTIM 3482 * bitmap while receiving beacons; use it here to 3483 * enable h/w detection of our AID being marked in 3484 * the bitmap vector (to indicate frames for us are 3485 * pending at the AP). 3486 * XXX do DTIM handling in s/w to WAR old h/w bugs 3487 * XXX enable based on h/w rev for newer chips 3488 */ 3489 bs.bs_timoffset = ni->ni_timoff; 3490 #endif 3491 /* 3492 * Calculate the number of consecutive beacons to miss 3493 * before taking a BMISS interrupt. 3494 * Note that we clamp the result to at most 10 beacons. 3495 */ 3496 bs.bs_bmissthreshold = vap->iv_bmissthreshold; 3497 if (bs.bs_bmissthreshold > 10) 3498 bs.bs_bmissthreshold = 10; 3499 else if (bs.bs_bmissthreshold <= 0) 3500 bs.bs_bmissthreshold = 1; 3501 3502 /* 3503 * Calculate sleep duration. The configuration is 3504 * given in ms. We insure a multiple of the beacon 3505 * period is used. Also, if the sleep duration is 3506 * greater than the DTIM period then it makes senses 3507 * to make it a multiple of that. 3508 * 3509 * XXX fixed at 100ms 3510 */ 3511 bs.bs_sleepduration = 3512 roundup(IEEE80211_MS_TO_TU(100), bs.bs_intval); 3513 if (bs.bs_sleepduration > bs.bs_dtimperiod) 3514 bs.bs_sleepduration = roundup(bs.bs_sleepduration, bs.bs_dtimperiod); 3515 3516 DPRINTF(sc, ATH_DEBUG_BEACON, 3517 "%s: tsf %ju tsf:tu %u intval %u nexttbtt %u dtim %u nextdtim %u bmiss %u sleep %u cfp:period %u maxdur %u next %u timoffset %u\n" 3518 , __func__ 3519 , tsf, tsftu 3520 , bs.bs_intval 3521 , bs.bs_nexttbtt 3522 , bs.bs_dtimperiod 3523 , bs.bs_nextdtim 3524 , bs.bs_bmissthreshold 3525 , bs.bs_sleepduration 3526 , bs.bs_cfpperiod 3527 , bs.bs_cfpmaxduration 3528 , bs.bs_cfpnext 3529 , bs.bs_timoffset 3530 ); 3531 ath_hal_intrset(ah, 0); 3532 ath_hal_beacontimers(ah, &bs); 3533 sc->sc_imask |= HAL_INT_BMISS; 3534 ath_hal_intrset(ah, sc->sc_imask); 3535 } else { 3536 ath_hal_intrset(ah, 0); 3537 if (nexttbtt == intval) 3538 intval |= HAL_BEACON_RESET_TSF; 3539 if (ic->ic_opmode == IEEE80211_M_IBSS) { 3540 /* 3541 * In IBSS mode enable the beacon timers but only 3542 * enable SWBA interrupts if we need to manually 3543 * prepare beacon frames. Otherwise we use a 3544 * self-linked tx descriptor and let the hardware 3545 * deal with things. 3546 */ 3547 intval |= HAL_BEACON_ENA; 3548 if (!sc->sc_hasveol) 3549 sc->sc_imask |= HAL_INT_SWBA; 3550 if ((intval & HAL_BEACON_RESET_TSF) == 0) { 3551 /* 3552 * Pull nexttbtt forward to reflect 3553 * the current TSF. 3554 */ 3555 tsf = ath_hal_gettsf64(ah); 3556 tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE; 3557 do { 3558 nexttbtt += intval; 3559 } while (nexttbtt < tsftu); 3560 } 3561 ath_beaconq_config(sc); 3562 } else if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 3563 /* 3564 * In AP mode we enable the beacon timers and 3565 * SWBA interrupts to prepare beacon frames. 3566 */ 3567 intval |= HAL_BEACON_ENA; 3568 sc->sc_imask |= HAL_INT_SWBA; /* beacon prepare */ 3569 ath_beaconq_config(sc); 3570 } 3571 ath_hal_beaconinit(ah, nexttbtt, intval); 3572 sc->sc_bmisscount = 0; 3573 ath_hal_intrset(ah, sc->sc_imask); 3574 /* 3575 * When using a self-linked beacon descriptor in 3576 * ibss mode load it once here. 3577 */ 3578 if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_hasveol) 3579 ath_beacon_start_adhoc(sc, vap); 3580 } 3581 sc->sc_syncbeacon = 0; 3582 #undef FUDGE 3583 #undef TSF_TO_TU 3584 } 3585 3586 static void 3587 ath_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 3588 { 3589 bus_addr_t *paddr = (bus_addr_t*) arg; 3590 KASSERT(error == 0, ("error %u on bus_dma callback", error)); 3591 *paddr = segs->ds_addr; 3592 } 3593 3594 static int 3595 ath_descdma_setup(struct ath_softc *sc, 3596 struct ath_descdma *dd, ath_bufhead *head, 3597 const char *name, int nbuf, int ndesc) 3598 { 3599 #define DS2PHYS(_dd, _ds) \ 3600 ((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc)) 3601 struct ifnet *ifp = sc->sc_ifp; 3602 struct ath_desc *ds; 3603 struct ath_buf *bf; 3604 int i, bsize, error; 3605 3606 DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA: %u buffers %u desc/buf\n", 3607 __func__, name, nbuf, ndesc); 3608 3609 dd->dd_name = name; 3610 dd->dd_desc_len = sizeof(struct ath_desc) * nbuf * ndesc; 3611 3612 /* 3613 * Setup DMA descriptor area. 3614 */ 3615 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), /* parent */ 3616 PAGE_SIZE, 0, /* alignment, bounds */ 3617 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 3618 BUS_SPACE_MAXADDR, /* highaddr */ 3619 NULL, NULL, /* filter, filterarg */ 3620 dd->dd_desc_len, /* maxsize */ 3621 1, /* nsegments */ 3622 dd->dd_desc_len, /* maxsegsize */ 3623 BUS_DMA_ALLOCNOW, /* flags */ 3624 NULL, /* lockfunc */ 3625 NULL, /* lockarg */ 3626 &dd->dd_dmat); 3627 if (error != 0) { 3628 if_printf(ifp, "cannot allocate %s DMA tag\n", dd->dd_name); 3629 return error; 3630 } 3631 3632 /* allocate descriptors */ 3633 error = bus_dmamap_create(dd->dd_dmat, BUS_DMA_NOWAIT, &dd->dd_dmamap); 3634 if (error != 0) { 3635 if_printf(ifp, "unable to create dmamap for %s descriptors, " 3636 "error %u\n", dd->dd_name, error); 3637 goto fail0; 3638 } 3639 3640 error = bus_dmamem_alloc(dd->dd_dmat, (void**) &dd->dd_desc, 3641 BUS_DMA_NOWAIT | BUS_DMA_COHERENT, 3642 &dd->dd_dmamap); 3643 if (error != 0) { 3644 if_printf(ifp, "unable to alloc memory for %u %s descriptors, " 3645 "error %u\n", nbuf * ndesc, dd->dd_name, error); 3646 goto fail1; 3647 } 3648 3649 error = bus_dmamap_load(dd->dd_dmat, dd->dd_dmamap, 3650 dd->dd_desc, dd->dd_desc_len, 3651 ath_load_cb, &dd->dd_desc_paddr, 3652 BUS_DMA_NOWAIT); 3653 if (error != 0) { 3654 if_printf(ifp, "unable to map %s descriptors, error %u\n", 3655 dd->dd_name, error); 3656 goto fail2; 3657 } 3658 3659 ds = dd->dd_desc; 3660 DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA map: %p (%lu) -> %p (%lu)\n", 3661 __func__, dd->dd_name, ds, (u_long) dd->dd_desc_len, 3662 (caddr_t) dd->dd_desc_paddr, /*XXX*/ (u_long) dd->dd_desc_len); 3663 3664 /* allocate rx buffers */ 3665 bsize = sizeof(struct ath_buf) * nbuf; 3666 bf = malloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO); 3667 if (bf == NULL) { 3668 if_printf(ifp, "malloc of %s buffers failed, size %u\n", 3669 dd->dd_name, bsize); 3670 goto fail3; 3671 } 3672 dd->dd_bufptr = bf; 3673 3674 STAILQ_INIT(head); 3675 for (i = 0; i < nbuf; i++, bf++, ds += ndesc) { 3676 bf->bf_desc = ds; 3677 bf->bf_daddr = DS2PHYS(dd, ds); 3678 error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT, 3679 &bf->bf_dmamap); 3680 if (error != 0) { 3681 if_printf(ifp, "unable to create dmamap for %s " 3682 "buffer %u, error %u\n", dd->dd_name, i, error); 3683 ath_descdma_cleanup(sc, dd, head); 3684 return error; 3685 } 3686 STAILQ_INSERT_TAIL(head, bf, bf_list); 3687 } 3688 return 0; 3689 fail3: 3690 bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap); 3691 fail2: 3692 bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap); 3693 fail1: 3694 bus_dmamap_destroy(dd->dd_dmat, dd->dd_dmamap); 3695 fail0: 3696 bus_dma_tag_destroy(dd->dd_dmat); 3697 memset(dd, 0, sizeof(*dd)); 3698 return error; 3699 #undef DS2PHYS 3700 } 3701 3702 static void 3703 ath_descdma_cleanup(struct ath_softc *sc, 3704 struct ath_descdma *dd, ath_bufhead *head) 3705 { 3706 struct ath_buf *bf; 3707 struct ieee80211_node *ni; 3708 3709 bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap); 3710 bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap); 3711 bus_dmamap_destroy(dd->dd_dmat, dd->dd_dmamap); 3712 bus_dma_tag_destroy(dd->dd_dmat); 3713 3714 STAILQ_FOREACH(bf, head, bf_list) { 3715 if (bf->bf_m) { 3716 m_freem(bf->bf_m); 3717 bf->bf_m = NULL; 3718 } 3719 if (bf->bf_dmamap != NULL) { 3720 bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap); 3721 bf->bf_dmamap = NULL; 3722 } 3723 ni = bf->bf_node; 3724 bf->bf_node = NULL; 3725 if (ni != NULL) { 3726 /* 3727 * Reclaim node reference. 3728 */ 3729 ieee80211_free_node(ni); 3730 } 3731 } 3732 3733 STAILQ_INIT(head); 3734 free(dd->dd_bufptr, M_ATHDEV); 3735 memset(dd, 0, sizeof(*dd)); 3736 } 3737 3738 static int 3739 ath_desc_alloc(struct ath_softc *sc) 3740 { 3741 int error; 3742 3743 error = ath_descdma_setup(sc, &sc->sc_rxdma, &sc->sc_rxbuf, 3744 "rx", ath_rxbuf, 1); 3745 if (error != 0) 3746 return error; 3747 3748 error = ath_descdma_setup(sc, &sc->sc_txdma, &sc->sc_txbuf, 3749 "tx", ath_txbuf, ATH_TXDESC); 3750 if (error != 0) { 3751 ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf); 3752 return error; 3753 } 3754 3755 error = ath_descdma_setup(sc, &sc->sc_bdma, &sc->sc_bbuf, 3756 "beacon", ATH_BCBUF, 1); 3757 if (error != 0) { 3758 ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf); 3759 ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf); 3760 return error; 3761 } 3762 return 0; 3763 } 3764 3765 static void 3766 ath_desc_free(struct ath_softc *sc) 3767 { 3768 3769 if (sc->sc_bdma.dd_desc_len != 0) 3770 ath_descdma_cleanup(sc, &sc->sc_bdma, &sc->sc_bbuf); 3771 if (sc->sc_txdma.dd_desc_len != 0) 3772 ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf); 3773 if (sc->sc_rxdma.dd_desc_len != 0) 3774 ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf); 3775 } 3776 3777 static struct ieee80211_node * 3778 ath_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN]) 3779 { 3780 struct ieee80211com *ic = vap->iv_ic; 3781 struct ath_softc *sc = ic->ic_ifp->if_softc; 3782 const size_t space = sizeof(struct ath_node) + sc->sc_rc->arc_space; 3783 struct ath_node *an; 3784 3785 an = malloc(space, M_80211_NODE, M_NOWAIT|M_ZERO); 3786 if (an == NULL) { 3787 /* XXX stat+msg */ 3788 return NULL; 3789 } 3790 ath_rate_node_init(sc, an); 3791 3792 DPRINTF(sc, ATH_DEBUG_NODE, "%s: an %p\n", __func__, an); 3793 return &an->an_node; 3794 } 3795 3796 static void 3797 ath_node_free(struct ieee80211_node *ni) 3798 { 3799 struct ieee80211com *ic = ni->ni_ic; 3800 struct ath_softc *sc = ic->ic_ifp->if_softc; 3801 3802 DPRINTF(sc, ATH_DEBUG_NODE, "%s: ni %p\n", __func__, ni); 3803 3804 ath_rate_node_cleanup(sc, ATH_NODE(ni)); 3805 sc->sc_node_free(ni); 3806 } 3807 3808 static void 3809 ath_node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise) 3810 { 3811 struct ieee80211com *ic = ni->ni_ic; 3812 struct ath_softc *sc = ic->ic_ifp->if_softc; 3813 struct ath_hal *ah = sc->sc_ah; 3814 3815 *rssi = ic->ic_node_getrssi(ni); 3816 if (ni->ni_chan != IEEE80211_CHAN_ANYC) 3817 *noise = ath_hal_getchannoise(ah, ni->ni_chan); 3818 else 3819 *noise = -95; /* nominally correct */ 3820 } 3821 3822 static int 3823 ath_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf) 3824 { 3825 struct ath_hal *ah = sc->sc_ah; 3826 int error; 3827 struct mbuf *m; 3828 struct ath_desc *ds; 3829 3830 m = bf->bf_m; 3831 if (m == NULL) { 3832 /* 3833 * NB: by assigning a page to the rx dma buffer we 3834 * implicitly satisfy the Atheros requirement that 3835 * this buffer be cache-line-aligned and sized to be 3836 * multiple of the cache line size. Not doing this 3837 * causes weird stuff to happen (for the 5210 at least). 3838 */ 3839 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 3840 if (m == NULL) { 3841 DPRINTF(sc, ATH_DEBUG_ANY, 3842 "%s: no mbuf/cluster\n", __func__); 3843 sc->sc_stats.ast_rx_nombuf++; 3844 return ENOMEM; 3845 } 3846 m->m_pkthdr.len = m->m_len = m->m_ext.ext_size; 3847 3848 error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, 3849 bf->bf_dmamap, m, 3850 bf->bf_segs, &bf->bf_nseg, 3851 BUS_DMA_NOWAIT); 3852 if (error != 0) { 3853 DPRINTF(sc, ATH_DEBUG_ANY, 3854 "%s: bus_dmamap_load_mbuf_sg failed; error %d\n", 3855 __func__, error); 3856 sc->sc_stats.ast_rx_busdma++; 3857 m_freem(m); 3858 return error; 3859 } 3860 KASSERT(bf->bf_nseg == 1, 3861 ("multi-segment packet; nseg %u", bf->bf_nseg)); 3862 bf->bf_m = m; 3863 } 3864 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREREAD); 3865 3866 /* 3867 * Setup descriptors. For receive we always terminate 3868 * the descriptor list with a self-linked entry so we'll 3869 * not get overrun under high load (as can happen with a 3870 * 5212 when ANI processing enables PHY error frames). 3871 * 3872 * To insure the last descriptor is self-linked we create 3873 * each descriptor as self-linked and add it to the end. As 3874 * each additional descriptor is added the previous self-linked 3875 * entry is ``fixed'' naturally. This should be safe even 3876 * if DMA is happening. When processing RX interrupts we 3877 * never remove/process the last, self-linked, entry on the 3878 * descriptor list. This insures the hardware always has 3879 * someplace to write a new frame. 3880 */ 3881 ds = bf->bf_desc; 3882 ds->ds_link = bf->bf_daddr; /* link to self */ 3883 ds->ds_data = bf->bf_segs[0].ds_addr; 3884 ath_hal_setuprxdesc(ah, ds 3885 , m->m_len /* buffer size */ 3886 , 0 3887 ); 3888 3889 if (sc->sc_rxlink != NULL) 3890 *sc->sc_rxlink = bf->bf_daddr; 3891 sc->sc_rxlink = &ds->ds_link; 3892 return 0; 3893 } 3894 3895 /* 3896 * Extend 15-bit time stamp from rx descriptor to 3897 * a full 64-bit TSF using the specified TSF. 3898 */ 3899 static __inline u_int64_t 3900 ath_extend_tsf(u_int32_t rstamp, u_int64_t tsf) 3901 { 3902 if ((tsf & 0x7fff) < rstamp) 3903 tsf -= 0x8000; 3904 return ((tsf &~ 0x7fff) | rstamp); 3905 } 3906 3907 /* 3908 * Intercept management frames to collect beacon rssi data 3909 * and to do ibss merges. 3910 */ 3911 static void 3912 ath_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m, 3913 int subtype, int rssi, int noise, u_int32_t rstamp) 3914 { 3915 struct ieee80211vap *vap = ni->ni_vap; 3916 struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc; 3917 3918 /* 3919 * Call up first so subsequent work can use information 3920 * potentially stored in the node (e.g. for ibss merge). 3921 */ 3922 ATH_VAP(vap)->av_recv_mgmt(ni, m, subtype, rssi, noise, rstamp); 3923 switch (subtype) { 3924 case IEEE80211_FC0_SUBTYPE_BEACON: 3925 /* update rssi statistics for use by the hal */ 3926 ATH_RSSI_LPF(sc->sc_halstats.ns_avgbrssi, rssi); 3927 if (sc->sc_syncbeacon && 3928 ni == vap->iv_bss && vap->iv_state == IEEE80211_S_RUN) { 3929 /* 3930 * Resync beacon timers using the tsf of the beacon 3931 * frame we just received. 3932 */ 3933 ath_beacon_config(sc, vap); 3934 } 3935 /* fall thru... */ 3936 case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 3937 if (vap->iv_opmode == IEEE80211_M_IBSS && 3938 vap->iv_state == IEEE80211_S_RUN) { 3939 u_int64_t tsf = ath_extend_tsf(rstamp, 3940 ath_hal_gettsf64(sc->sc_ah)); 3941 /* 3942 * Handle ibss merge as needed; check the tsf on the 3943 * frame before attempting the merge. The 802.11 spec 3944 * says the station should change it's bssid to match 3945 * the oldest station with the same ssid, where oldest 3946 * is determined by the tsf. Note that hardware 3947 * reconfiguration happens through callback to 3948 * ath_newstate as the state machine will go from 3949 * RUN -> RUN when this happens. 3950 */ 3951 if (le64toh(ni->ni_tstamp.tsf) >= tsf) { 3952 DPRINTF(sc, ATH_DEBUG_STATE, 3953 "ibss merge, rstamp %u tsf %ju " 3954 "tstamp %ju\n", rstamp, (uintmax_t)tsf, 3955 (uintmax_t)ni->ni_tstamp.tsf); 3956 (void) ieee80211_ibss_merge(ni); 3957 } 3958 } 3959 break; 3960 } 3961 } 3962 3963 /* 3964 * Set the default antenna. 3965 */ 3966 static void 3967 ath_setdefantenna(struct ath_softc *sc, u_int antenna) 3968 { 3969 struct ath_hal *ah = sc->sc_ah; 3970 3971 /* XXX block beacon interrupts */ 3972 ath_hal_setdefantenna(ah, antenna); 3973 if (sc->sc_defant != antenna) 3974 sc->sc_stats.ast_ant_defswitch++; 3975 sc->sc_defant = antenna; 3976 sc->sc_rxotherant = 0; 3977 } 3978 3979 static int 3980 ath_rx_tap(struct ifnet *ifp, struct mbuf *m, 3981 const struct ath_rx_status *rs, u_int64_t tsf, int16_t nf) 3982 { 3983 #define CHAN_HT20 htole32(IEEE80211_CHAN_HT20) 3984 #define CHAN_HT40U htole32(IEEE80211_CHAN_HT40U) 3985 #define CHAN_HT40D htole32(IEEE80211_CHAN_HT40D) 3986 #define CHAN_HT (CHAN_HT20|CHAN_HT40U|CHAN_HT40D) 3987 struct ath_softc *sc = ifp->if_softc; 3988 const HAL_RATE_TABLE *rt; 3989 uint8_t rix; 3990 3991 /* 3992 * Discard anything shorter than an ack or cts. 3993 */ 3994 if (m->m_pkthdr.len < IEEE80211_ACK_LEN) { 3995 DPRINTF(sc, ATH_DEBUG_RECV, "%s: runt packet %d\n", 3996 __func__, m->m_pkthdr.len); 3997 sc->sc_stats.ast_rx_tooshort++; 3998 return 0; 3999 } 4000 rt = sc->sc_currates; 4001 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode)); 4002 rix = rt->rateCodeToIndex[rs->rs_rate]; 4003 sc->sc_rx_th.wr_rate = sc->sc_hwmap[rix].ieeerate; 4004 sc->sc_rx_th.wr_flags = sc->sc_hwmap[rix].rxflags; 4005 #ifdef AH_SUPPORT_AR5416 4006 sc->sc_rx_th.wr_chan_flags &= ~CHAN_HT; 4007 if (sc->sc_rx_th.wr_rate & IEEE80211_RATE_MCS) { /* HT rate */ 4008 struct ieee80211com *ic = ifp->if_l2com; 4009 4010 if ((rs->rs_flags & HAL_RX_2040) == 0) 4011 sc->sc_rx_th.wr_chan_flags |= CHAN_HT20; 4012 else if (IEEE80211_IS_CHAN_HT40U(ic->ic_curchan)) 4013 sc->sc_rx_th.wr_chan_flags |= CHAN_HT40U; 4014 else 4015 sc->sc_rx_th.wr_chan_flags |= CHAN_HT40D; 4016 if ((rs->rs_flags & HAL_RX_GI) == 0) 4017 sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI; 4018 } 4019 #endif 4020 sc->sc_rx_th.wr_tsf = htole64(ath_extend_tsf(rs->rs_tstamp, tsf)); 4021 if (rs->rs_status & HAL_RXERR_CRC) 4022 sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_BADFCS; 4023 /* XXX propagate other error flags from descriptor */ 4024 sc->sc_rx_th.wr_antsignal = rs->rs_rssi + nf; 4025 sc->sc_rx_th.wr_antnoise = nf; 4026 sc->sc_rx_th.wr_antenna = rs->rs_antenna; 4027 4028 bpf_mtap2(ifp->if_bpf, &sc->sc_rx_th, sc->sc_rx_th_len, m); 4029 4030 return 1; 4031 #undef CHAN_HT 4032 #undef CHAN_HT20 4033 #undef CHAN_HT40U 4034 #undef CHAN_HT40D 4035 } 4036 4037 static void 4038 ath_handle_micerror(struct ieee80211com *ic, 4039 struct ieee80211_frame *wh, int keyix) 4040 { 4041 struct ieee80211_node *ni; 4042 4043 /* XXX recheck MIC to deal w/ chips that lie */ 4044 /* XXX discard MIC errors on !data frames */ 4045 ni = ieee80211_find_rxnode(ic, (const struct ieee80211_frame_min *) wh); 4046 if (ni != NULL) { 4047 ieee80211_notify_michael_failure(ni->ni_vap, wh, keyix); 4048 ieee80211_free_node(ni); 4049 } 4050 } 4051 4052 static void 4053 ath_rx_proc(void *arg, int npending) 4054 { 4055 #define PA2DESC(_sc, _pa) \ 4056 ((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \ 4057 ((_pa) - (_sc)->sc_rxdma.dd_desc_paddr))) 4058 struct ath_softc *sc = arg; 4059 struct ath_buf *bf; 4060 struct ifnet *ifp = sc->sc_ifp; 4061 struct ieee80211com *ic = ifp->if_l2com; 4062 struct ath_hal *ah = sc->sc_ah; 4063 struct ath_desc *ds; 4064 struct ath_rx_status *rs; 4065 struct mbuf *m; 4066 struct ieee80211_node *ni; 4067 int len, type, ngood; 4068 u_int phyerr; 4069 HAL_STATUS status; 4070 int16_t nf; 4071 u_int64_t tsf; 4072 4073 DPRINTF(sc, ATH_DEBUG_RX_PROC, "%s: pending %u\n", __func__, npending); 4074 ngood = 0; 4075 nf = ath_hal_getchannoise(ah, sc->sc_curchan); 4076 sc->sc_stats.ast_rx_noise = nf; 4077 tsf = ath_hal_gettsf64(ah); 4078 do { 4079 bf = STAILQ_FIRST(&sc->sc_rxbuf); 4080 if (bf == NULL) { /* NB: shouldn't happen */ 4081 if_printf(ifp, "%s: no buffer!\n", __func__); 4082 break; 4083 } 4084 m = bf->bf_m; 4085 if (m == NULL) { /* NB: shouldn't happen */ 4086 /* 4087 * If mbuf allocation failed previously there 4088 * will be no mbuf; try again to re-populate it. 4089 */ 4090 /* XXX make debug msg */ 4091 if_printf(ifp, "%s: no mbuf!\n", __func__); 4092 STAILQ_REMOVE_HEAD(&sc->sc_rxbuf, bf_list); 4093 goto rx_next; 4094 } 4095 ds = bf->bf_desc; 4096 if (ds->ds_link == bf->bf_daddr) { 4097 /* NB: never process the self-linked entry at the end */ 4098 break; 4099 } 4100 /* XXX sync descriptor memory */ 4101 /* 4102 * Must provide the virtual address of the current 4103 * descriptor, the physical address, and the virtual 4104 * address of the next descriptor in the h/w chain. 4105 * This allows the HAL to look ahead to see if the 4106 * hardware is done with a descriptor by checking the 4107 * done bit in the following descriptor and the address 4108 * of the current descriptor the DMA engine is working 4109 * on. All this is necessary because of our use of 4110 * a self-linked list to avoid rx overruns. 4111 */ 4112 rs = &bf->bf_status.ds_rxstat; 4113 status = ath_hal_rxprocdesc(ah, ds, 4114 bf->bf_daddr, PA2DESC(sc, ds->ds_link), rs); 4115 #ifdef ATH_DEBUG 4116 if (sc->sc_debug & ATH_DEBUG_RECV_DESC) 4117 ath_printrxbuf(sc, bf, 0, status == HAL_OK); 4118 #endif 4119 if (status == HAL_EINPROGRESS) 4120 break; 4121 STAILQ_REMOVE_HEAD(&sc->sc_rxbuf, bf_list); 4122 if (rs->rs_status != 0) { 4123 if (rs->rs_status & HAL_RXERR_CRC) 4124 sc->sc_stats.ast_rx_crcerr++; 4125 if (rs->rs_status & HAL_RXERR_FIFO) 4126 sc->sc_stats.ast_rx_fifoerr++; 4127 if (rs->rs_status & HAL_RXERR_PHY) { 4128 sc->sc_stats.ast_rx_phyerr++; 4129 phyerr = rs->rs_phyerr & 0x1f; 4130 sc->sc_stats.ast_rx_phy[phyerr]++; 4131 goto rx_error; /* NB: don't count in ierrors */ 4132 } 4133 if (rs->rs_status & HAL_RXERR_DECRYPT) { 4134 /* 4135 * Decrypt error. If the error occurred 4136 * because there was no hardware key, then 4137 * let the frame through so the upper layers 4138 * can process it. This is necessary for 5210 4139 * parts which have no way to setup a ``clear'' 4140 * key cache entry. 4141 * 4142 * XXX do key cache faulting 4143 */ 4144 if (rs->rs_keyix == HAL_RXKEYIX_INVALID) 4145 goto rx_accept; 4146 sc->sc_stats.ast_rx_badcrypt++; 4147 } 4148 if (rs->rs_status & HAL_RXERR_MIC) { 4149 sc->sc_stats.ast_rx_badmic++; 4150 /* 4151 * Do minimal work required to hand off 4152 * the 802.11 header for notifcation. 4153 */ 4154 /* XXX frag's and qos frames */ 4155 len = rs->rs_datalen; 4156 if (len >= sizeof (struct ieee80211_frame)) { 4157 bus_dmamap_sync(sc->sc_dmat, 4158 bf->bf_dmamap, 4159 BUS_DMASYNC_POSTREAD); 4160 ath_handle_micerror(ic, 4161 mtod(m, struct ieee80211_frame *), 4162 sc->sc_splitmic ? 4163 rs->rs_keyix-32 : rs->rs_keyix); 4164 } 4165 } 4166 ifp->if_ierrors++; 4167 rx_error: 4168 /* 4169 * Cleanup any pending partial frame. 4170 */ 4171 if (sc->sc_rxpending != NULL) { 4172 m_freem(sc->sc_rxpending); 4173 sc->sc_rxpending = NULL; 4174 } 4175 /* 4176 * When a tap is present pass error frames 4177 * that have been requested. By default we 4178 * pass decrypt+mic errors but others may be 4179 * interesting (e.g. crc). 4180 */ 4181 if (bpf_peers_present(ifp->if_bpf) && 4182 (rs->rs_status & sc->sc_monpass)) { 4183 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, 4184 BUS_DMASYNC_POSTREAD); 4185 /* NB: bpf needs the mbuf length setup */ 4186 len = rs->rs_datalen; 4187 m->m_pkthdr.len = m->m_len = len; 4188 (void) ath_rx_tap(ifp, m, rs, tsf, nf); 4189 } 4190 /* XXX pass MIC errors up for s/w reclaculation */ 4191 goto rx_next; 4192 } 4193 rx_accept: 4194 /* 4195 * Sync and unmap the frame. At this point we're 4196 * committed to passing the mbuf somewhere so clear 4197 * bf_m; this means a new mbuf must be allocated 4198 * when the rx descriptor is setup again to receive 4199 * another frame. 4200 */ 4201 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, 4202 BUS_DMASYNC_POSTREAD); 4203 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 4204 bf->bf_m = NULL; 4205 4206 len = rs->rs_datalen; 4207 m->m_len = len; 4208 4209 if (rs->rs_more) { 4210 /* 4211 * Frame spans multiple descriptors; save 4212 * it for the next completed descriptor, it 4213 * will be used to construct a jumbogram. 4214 */ 4215 if (sc->sc_rxpending != NULL) { 4216 /* NB: max frame size is currently 2 clusters */ 4217 sc->sc_stats.ast_rx_toobig++; 4218 m_freem(sc->sc_rxpending); 4219 } 4220 m->m_pkthdr.rcvif = ifp; 4221 m->m_pkthdr.len = len; 4222 sc->sc_rxpending = m; 4223 goto rx_next; 4224 } else if (sc->sc_rxpending != NULL) { 4225 /* 4226 * This is the second part of a jumbogram, 4227 * chain it to the first mbuf, adjust the 4228 * frame length, and clear the rxpending state. 4229 */ 4230 sc->sc_rxpending->m_next = m; 4231 sc->sc_rxpending->m_pkthdr.len += len; 4232 m = sc->sc_rxpending; 4233 sc->sc_rxpending = NULL; 4234 } else { 4235 /* 4236 * Normal single-descriptor receive; setup 4237 * the rcvif and packet length. 4238 */ 4239 m->m_pkthdr.rcvif = ifp; 4240 m->m_pkthdr.len = len; 4241 } 4242 4243 ifp->if_ipackets++; 4244 sc->sc_stats.ast_ant_rx[rs->rs_antenna]++; 4245 4246 if (bpf_peers_present(ifp->if_bpf) && 4247 !ath_rx_tap(ifp, m, rs, tsf, nf)) { 4248 m_freem(m); /* XXX reclaim */ 4249 goto rx_next; 4250 } 4251 4252 /* 4253 * From this point on we assume the frame is at least 4254 * as large as ieee80211_frame_min; verify that. 4255 */ 4256 if (len < IEEE80211_MIN_LEN) { 4257 DPRINTF(sc, ATH_DEBUG_RECV, "%s: short packet %d\n", 4258 __func__, len); 4259 sc->sc_stats.ast_rx_tooshort++; 4260 m_freem(m); 4261 goto rx_next; 4262 } 4263 4264 if (IFF_DUMPPKTS(sc, ATH_DEBUG_RECV)) { 4265 const HAL_RATE_TABLE *rt = sc->sc_currates; 4266 uint8_t rix = rt->rateCodeToIndex[rs->rs_rate]; 4267 4268 ieee80211_dump_pkt(ic, mtod(m, caddr_t), len, 4269 sc->sc_hwmap[rix].ieeerate, rs->rs_rssi); 4270 } 4271 4272 m_adj(m, -IEEE80211_CRC_LEN); 4273 4274 /* 4275 * Locate the node for sender, track state, and then 4276 * pass the (referenced) node up to the 802.11 layer 4277 * for its use. 4278 */ 4279 ni = ieee80211_find_rxnode_withkey(ic, 4280 mtod(m, const struct ieee80211_frame_min *), 4281 rs->rs_keyix == HAL_RXKEYIX_INVALID ? 4282 IEEE80211_KEYIX_NONE : rs->rs_keyix); 4283 if (ni != NULL) { 4284 /* 4285 * Sending station is known, dispatch directly. 4286 */ 4287 #ifdef ATH_SUPPORT_TDMA 4288 sc->sc_tdmars = rs; 4289 #endif 4290 type = ieee80211_input(ni, m, 4291 rs->rs_rssi, nf, rs->rs_tstamp); 4292 ieee80211_free_node(ni); 4293 /* 4294 * Arrange to update the last rx timestamp only for 4295 * frames from our ap when operating in station mode. 4296 * This assumes the rx key is always setup when 4297 * associated. 4298 */ 4299 if (ic->ic_opmode == IEEE80211_M_STA && 4300 rs->rs_keyix != HAL_RXKEYIX_INVALID) 4301 ngood++; 4302 } else { 4303 type = ieee80211_input_all(ic, m, 4304 rs->rs_rssi, nf, rs->rs_tstamp); 4305 } 4306 /* 4307 * Track rx rssi and do any rx antenna management. 4308 */ 4309 ATH_RSSI_LPF(sc->sc_halstats.ns_avgrssi, rs->rs_rssi); 4310 if (sc->sc_diversity) { 4311 /* 4312 * When using fast diversity, change the default rx 4313 * antenna if diversity chooses the other antenna 3 4314 * times in a row. 4315 */ 4316 if (sc->sc_defant != rs->rs_antenna) { 4317 if (++sc->sc_rxotherant >= 3) 4318 ath_setdefantenna(sc, rs->rs_antenna); 4319 } else 4320 sc->sc_rxotherant = 0; 4321 } 4322 if (sc->sc_softled) { 4323 /* 4324 * Blink for any data frame. Otherwise do a 4325 * heartbeat-style blink when idle. The latter 4326 * is mainly for station mode where we depend on 4327 * periodic beacon frames to trigger the poll event. 4328 */ 4329 if (type == IEEE80211_FC0_TYPE_DATA) { 4330 const HAL_RATE_TABLE *rt = sc->sc_currates; 4331 ath_led_event(sc, 4332 rt->rateCodeToIndex[rs->rs_rate]); 4333 } else if (ticks - sc->sc_ledevent >= sc->sc_ledidle) 4334 ath_led_event(sc, 0); 4335 } 4336 rx_next: 4337 STAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list); 4338 } while (ath_rxbuf_init(sc, bf) == 0); 4339 4340 /* rx signal state monitoring */ 4341 ath_hal_rxmonitor(ah, &sc->sc_halstats, sc->sc_curchan); 4342 if (ngood) 4343 sc->sc_lastrx = tsf; 4344 4345 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 && 4346 !IFQ_IS_EMPTY(&ifp->if_snd)) 4347 ath_start(ifp); 4348 4349 #undef PA2DESC 4350 } 4351 4352 static void 4353 ath_txq_init(struct ath_softc *sc, struct ath_txq *txq, int qnum) 4354 { 4355 txq->axq_qnum = qnum; 4356 txq->axq_depth = 0; 4357 txq->axq_intrcnt = 0; 4358 txq->axq_link = NULL; 4359 STAILQ_INIT(&txq->axq_q); 4360 ATH_TXQ_LOCK_INIT(sc, txq); 4361 TAILQ_INIT(&txq->axq_stageq); 4362 txq->axq_curage = 0; 4363 } 4364 4365 /* 4366 * Setup a h/w transmit queue. 4367 */ 4368 static struct ath_txq * 4369 ath_txq_setup(struct ath_softc *sc, int qtype, int subtype) 4370 { 4371 #define N(a) (sizeof(a)/sizeof(a[0])) 4372 struct ath_hal *ah = sc->sc_ah; 4373 HAL_TXQ_INFO qi; 4374 int qnum; 4375 4376 memset(&qi, 0, sizeof(qi)); 4377 qi.tqi_subtype = subtype; 4378 qi.tqi_aifs = HAL_TXQ_USEDEFAULT; 4379 qi.tqi_cwmin = HAL_TXQ_USEDEFAULT; 4380 qi.tqi_cwmax = HAL_TXQ_USEDEFAULT; 4381 /* 4382 * Enable interrupts only for EOL and DESC conditions. 4383 * We mark tx descriptors to receive a DESC interrupt 4384 * when a tx queue gets deep; otherwise waiting for the 4385 * EOL to reap descriptors. Note that this is done to 4386 * reduce interrupt load and this only defers reaping 4387 * descriptors, never transmitting frames. Aside from 4388 * reducing interrupts this also permits more concurrency. 4389 * The only potential downside is if the tx queue backs 4390 * up in which case the top half of the kernel may backup 4391 * due to a lack of tx descriptors. 4392 */ 4393 qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE | HAL_TXQ_TXDESCINT_ENABLE; 4394 qnum = ath_hal_setuptxqueue(ah, qtype, &qi); 4395 if (qnum == -1) { 4396 /* 4397 * NB: don't print a message, this happens 4398 * normally on parts with too few tx queues 4399 */ 4400 return NULL; 4401 } 4402 if (qnum >= N(sc->sc_txq)) { 4403 device_printf(sc->sc_dev, 4404 "hal qnum %u out of range, max %zu!\n", 4405 qnum, N(sc->sc_txq)); 4406 ath_hal_releasetxqueue(ah, qnum); 4407 return NULL; 4408 } 4409 if (!ATH_TXQ_SETUP(sc, qnum)) { 4410 ath_txq_init(sc, &sc->sc_txq[qnum], qnum); 4411 sc->sc_txqsetup |= 1<<qnum; 4412 } 4413 return &sc->sc_txq[qnum]; 4414 #undef N 4415 } 4416 4417 /* 4418 * Setup a hardware data transmit queue for the specified 4419 * access control. The hal may not support all requested 4420 * queues in which case it will return a reference to a 4421 * previously setup queue. We record the mapping from ac's 4422 * to h/w queues for use by ath_tx_start and also track 4423 * the set of h/w queues being used to optimize work in the 4424 * transmit interrupt handler and related routines. 4425 */ 4426 static int 4427 ath_tx_setup(struct ath_softc *sc, int ac, int haltype) 4428 { 4429 #define N(a) (sizeof(a)/sizeof(a[0])) 4430 struct ath_txq *txq; 4431 4432 if (ac >= N(sc->sc_ac2q)) { 4433 device_printf(sc->sc_dev, "AC %u out of range, max %zu!\n", 4434 ac, N(sc->sc_ac2q)); 4435 return 0; 4436 } 4437 txq = ath_txq_setup(sc, HAL_TX_QUEUE_DATA, haltype); 4438 if (txq != NULL) { 4439 sc->sc_ac2q[ac] = txq; 4440 return 1; 4441 } else 4442 return 0; 4443 #undef N 4444 } 4445 4446 /* 4447 * Update WME parameters for a transmit queue. 4448 */ 4449 static int 4450 ath_txq_update(struct ath_softc *sc, int ac) 4451 { 4452 #define ATH_EXPONENT_TO_VALUE(v) ((1<<v)-1) 4453 #define ATH_TXOP_TO_US(v) (v<<5) 4454 struct ifnet *ifp = sc->sc_ifp; 4455 struct ieee80211com *ic = ifp->if_l2com; 4456 struct ath_txq *txq = sc->sc_ac2q[ac]; 4457 struct wmeParams *wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac]; 4458 struct ath_hal *ah = sc->sc_ah; 4459 HAL_TXQ_INFO qi; 4460 4461 ath_hal_gettxqueueprops(ah, txq->axq_qnum, &qi); 4462 #ifdef ATH_SUPPORT_TDMA 4463 if (sc->sc_tdma) { 4464 /* 4465 * AIFS is zero so there's no pre-transmit wait. The 4466 * burst time defines the slot duration and is configured 4467 * via sysctl. The QCU is setup to not do post-xmit 4468 * back off, lockout all lower-priority QCU's, and fire 4469 * off the DMA beacon alert timer which is setup based 4470 * on the slot configuration. 4471 */ 4472 qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE 4473 | HAL_TXQ_TXERRINT_ENABLE 4474 | HAL_TXQ_TXURNINT_ENABLE 4475 | HAL_TXQ_TXEOLINT_ENABLE 4476 | HAL_TXQ_DBA_GATED 4477 | HAL_TXQ_BACKOFF_DISABLE 4478 | HAL_TXQ_ARB_LOCKOUT_GLOBAL 4479 ; 4480 qi.tqi_aifs = 0; 4481 /* XXX +dbaprep? */ 4482 qi.tqi_readyTime = sc->sc_tdmaslotlen; 4483 qi.tqi_burstTime = qi.tqi_readyTime; 4484 } else { 4485 #endif 4486 qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE 4487 | HAL_TXQ_TXERRINT_ENABLE 4488 | HAL_TXQ_TXDESCINT_ENABLE 4489 | HAL_TXQ_TXURNINT_ENABLE 4490 ; 4491 qi.tqi_aifs = wmep->wmep_aifsn; 4492 qi.tqi_cwmin = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin); 4493 qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax); 4494 qi.tqi_readyTime = 0; 4495 qi.tqi_burstTime = ATH_TXOP_TO_US(wmep->wmep_txopLimit); 4496 #ifdef ATH_SUPPORT_TDMA 4497 } 4498 #endif 4499 4500 DPRINTF(sc, ATH_DEBUG_RESET, 4501 "%s: Q%u qflags 0x%x aifs %u cwmin %u cwmax %u burstTime %u\n", 4502 __func__, txq->axq_qnum, qi.tqi_qflags, 4503 qi.tqi_aifs, qi.tqi_cwmin, qi.tqi_cwmax, qi.tqi_burstTime); 4504 4505 if (!ath_hal_settxqueueprops(ah, txq->axq_qnum, &qi)) { 4506 if_printf(ifp, "unable to update hardware queue " 4507 "parameters for %s traffic!\n", 4508 ieee80211_wme_acnames[ac]); 4509 return 0; 4510 } else { 4511 ath_hal_resettxqueue(ah, txq->axq_qnum); /* push to h/w */ 4512 return 1; 4513 } 4514 #undef ATH_TXOP_TO_US 4515 #undef ATH_EXPONENT_TO_VALUE 4516 } 4517 4518 /* 4519 * Callback from the 802.11 layer to update WME parameters. 4520 */ 4521 static int 4522 ath_wme_update(struct ieee80211com *ic) 4523 { 4524 struct ath_softc *sc = ic->ic_ifp->if_softc; 4525 4526 return !ath_txq_update(sc, WME_AC_BE) || 4527 !ath_txq_update(sc, WME_AC_BK) || 4528 !ath_txq_update(sc, WME_AC_VI) || 4529 !ath_txq_update(sc, WME_AC_VO) ? EIO : 0; 4530 } 4531 4532 /* 4533 * Reclaim resources for a setup queue. 4534 */ 4535 static void 4536 ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq) 4537 { 4538 4539 ath_hal_releasetxqueue(sc->sc_ah, txq->axq_qnum); 4540 ATH_TXQ_LOCK_DESTROY(txq); 4541 sc->sc_txqsetup &= ~(1<<txq->axq_qnum); 4542 } 4543 4544 /* 4545 * Reclaim all tx queue resources. 4546 */ 4547 static void 4548 ath_tx_cleanup(struct ath_softc *sc) 4549 { 4550 int i; 4551 4552 ATH_TXBUF_LOCK_DESTROY(sc); 4553 for (i = 0; i < HAL_NUM_TX_QUEUES; i++) 4554 if (ATH_TXQ_SETUP(sc, i)) 4555 ath_tx_cleanupq(sc, &sc->sc_txq[i]); 4556 } 4557 4558 /* 4559 * Return h/w rate index for an IEEE rate (w/o basic rate bit). 4560 */ 4561 static int 4562 ath_tx_findrix(const HAL_RATE_TABLE *rt, int rate) 4563 { 4564 int i; 4565 4566 for (i = 0; i < rt->rateCount; i++) 4567 if ((rt->info[i].dot11Rate & IEEE80211_RATE_VAL) == rate) 4568 return i; 4569 return 0; /* NB: lowest rate */ 4570 } 4571 4572 /* 4573 * Reclaim mbuf resources. For fragmented frames we 4574 * need to claim each frag chained with m_nextpkt. 4575 */ 4576 static void 4577 ath_freetx(struct mbuf *m) 4578 { 4579 struct mbuf *next; 4580 4581 do { 4582 next = m->m_nextpkt; 4583 m->m_nextpkt = NULL; 4584 m_freem(m); 4585 } while ((m = next) != NULL); 4586 } 4587 4588 static int 4589 ath_tx_dmasetup(struct ath_softc *sc, struct ath_buf *bf, struct mbuf *m0) 4590 { 4591 struct mbuf *m; 4592 int error; 4593 4594 /* 4595 * Load the DMA map so any coalescing is done. This 4596 * also calculates the number of descriptors we need. 4597 */ 4598 error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0, 4599 bf->bf_segs, &bf->bf_nseg, 4600 BUS_DMA_NOWAIT); 4601 if (error == EFBIG) { 4602 /* XXX packet requires too many descriptors */ 4603 bf->bf_nseg = ATH_TXDESC+1; 4604 } else if (error != 0) { 4605 sc->sc_stats.ast_tx_busdma++; 4606 ath_freetx(m0); 4607 return error; 4608 } 4609 /* 4610 * Discard null packets and check for packets that 4611 * require too many TX descriptors. We try to convert 4612 * the latter to a cluster. 4613 */ 4614 if (bf->bf_nseg > ATH_TXDESC) { /* too many desc's, linearize */ 4615 sc->sc_stats.ast_tx_linear++; 4616 m = m_collapse(m0, M_DONTWAIT, ATH_TXDESC); 4617 if (m == NULL) { 4618 ath_freetx(m0); 4619 sc->sc_stats.ast_tx_nombuf++; 4620 return ENOMEM; 4621 } 4622 m0 = m; 4623 error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0, 4624 bf->bf_segs, &bf->bf_nseg, 4625 BUS_DMA_NOWAIT); 4626 if (error != 0) { 4627 sc->sc_stats.ast_tx_busdma++; 4628 ath_freetx(m0); 4629 return error; 4630 } 4631 KASSERT(bf->bf_nseg <= ATH_TXDESC, 4632 ("too many segments after defrag; nseg %u", bf->bf_nseg)); 4633 } else if (bf->bf_nseg == 0) { /* null packet, discard */ 4634 sc->sc_stats.ast_tx_nodata++; 4635 ath_freetx(m0); 4636 return EIO; 4637 } 4638 DPRINTF(sc, ATH_DEBUG_XMIT, "%s: m %p len %u\n", 4639 __func__, m0, m0->m_pkthdr.len); 4640 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE); 4641 bf->bf_m = m0; 4642 4643 return 0; 4644 } 4645 4646 static void 4647 ath_tx_handoff(struct ath_softc *sc, struct ath_txq *txq, struct ath_buf *bf) 4648 { 4649 struct ath_hal *ah = sc->sc_ah; 4650 struct ath_desc *ds, *ds0; 4651 int i; 4652 4653 /* 4654 * Fillin the remainder of the descriptor info. 4655 */ 4656 ds0 = ds = bf->bf_desc; 4657 for (i = 0; i < bf->bf_nseg; i++, ds++) { 4658 ds->ds_data = bf->bf_segs[i].ds_addr; 4659 if (i == bf->bf_nseg - 1) 4660 ds->ds_link = 0; 4661 else 4662 ds->ds_link = bf->bf_daddr + sizeof(*ds) * (i + 1); 4663 ath_hal_filltxdesc(ah, ds 4664 , bf->bf_segs[i].ds_len /* segment length */ 4665 , i == 0 /* first segment */ 4666 , i == bf->bf_nseg - 1 /* last segment */ 4667 , ds0 /* first descriptor */ 4668 ); 4669 DPRINTF(sc, ATH_DEBUG_XMIT, 4670 "%s: %d: %08x %08x %08x %08x %08x %08x\n", 4671 __func__, i, ds->ds_link, ds->ds_data, 4672 ds->ds_ctl0, ds->ds_ctl1, ds->ds_hw[0], ds->ds_hw[1]); 4673 } 4674 /* 4675 * Insert the frame on the outbound list and pass it on 4676 * to the hardware. Multicast frames buffered for power 4677 * save stations and transmit from the CAB queue are stored 4678 * on a s/w only queue and loaded on to the CAB queue in 4679 * the SWBA handler since frames only go out on DTIM and 4680 * to avoid possible races. 4681 */ 4682 ATH_TXQ_LOCK(txq); 4683 KASSERT((bf->bf_flags & ATH_BUF_BUSY) == 0, 4684 ("busy status 0x%x", bf->bf_flags)); 4685 if (txq->axq_qnum != ATH_TXQ_SWQ) { 4686 #ifdef ATH_SUPPORT_TDMA 4687 int qbusy; 4688 4689 ATH_TXQ_INSERT_TAIL(txq, bf, bf_list); 4690 qbusy = ath_hal_txqenabled(ah, txq->axq_qnum); 4691 if (txq->axq_link == NULL) { 4692 /* 4693 * Be careful writing the address to TXDP. If 4694 * the tx q is enabled then this write will be 4695 * ignored. Normally this is not an issue but 4696 * when tdma is in use and the q is beacon gated 4697 * this race can occur. If the q is busy then 4698 * defer the work to later--either when another 4699 * packet comes along or when we prepare a beacon 4700 * frame at SWBA. 4701 */ 4702 if (!qbusy) { 4703 ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr); 4704 txq->axq_flags &= ~ATH_TXQ_PUTPENDING; 4705 DPRINTF(sc, ATH_DEBUG_XMIT, 4706 "%s: TXDP[%u] = %p (%p) depth %d\n", 4707 __func__, txq->axq_qnum, 4708 (caddr_t)bf->bf_daddr, bf->bf_desc, 4709 txq->axq_depth); 4710 } else { 4711 txq->axq_flags |= ATH_TXQ_PUTPENDING; 4712 DPRINTF(sc, ATH_DEBUG_TDMA | ATH_DEBUG_XMIT, 4713 "%s: Q%u busy, defer enable\n", __func__, 4714 txq->axq_qnum); 4715 } 4716 } else { 4717 *txq->axq_link = bf->bf_daddr; 4718 DPRINTF(sc, ATH_DEBUG_XMIT, 4719 "%s: link[%u](%p)=%p (%p) depth %d\n", __func__, 4720 txq->axq_qnum, txq->axq_link, 4721 (caddr_t)bf->bf_daddr, bf->bf_desc, txq->axq_depth); 4722 if ((txq->axq_flags & ATH_TXQ_PUTPENDING) && !qbusy) { 4723 /* 4724 * The q was busy when we previously tried 4725 * to write the address of the first buffer 4726 * in the chain. Since it's not busy now 4727 * handle this chore. We are certain the 4728 * buffer at the front is the right one since 4729 * axq_link is NULL only when the buffer list 4730 * is/was empty. 4731 */ 4732 ath_hal_puttxbuf(ah, txq->axq_qnum, 4733 STAILQ_FIRST(&txq->axq_q)->bf_daddr); 4734 txq->axq_flags &= ~ATH_TXQ_PUTPENDING; 4735 DPRINTF(sc, ATH_DEBUG_TDMA | ATH_DEBUG_XMIT, 4736 "%s: Q%u restarted\n", __func__, 4737 txq->axq_qnum); 4738 } 4739 } 4740 #else 4741 ATH_TXQ_INSERT_TAIL(txq, bf, bf_list); 4742 if (txq->axq_link == NULL) { 4743 ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr); 4744 DPRINTF(sc, ATH_DEBUG_XMIT, 4745 "%s: TXDP[%u] = %p (%p) depth %d\n", 4746 __func__, txq->axq_qnum, 4747 (caddr_t)bf->bf_daddr, bf->bf_desc, 4748 txq->axq_depth); 4749 } else { 4750 *txq->axq_link = bf->bf_daddr; 4751 DPRINTF(sc, ATH_DEBUG_XMIT, 4752 "%s: link[%u](%p)=%p (%p) depth %d\n", __func__, 4753 txq->axq_qnum, txq->axq_link, 4754 (caddr_t)bf->bf_daddr, bf->bf_desc, txq->axq_depth); 4755 } 4756 #endif /* ATH_SUPPORT_TDMA */ 4757 txq->axq_link = &bf->bf_desc[bf->bf_nseg - 1].ds_link; 4758 ath_hal_txstart(ah, txq->axq_qnum); 4759 } else { 4760 if (txq->axq_link != NULL) { 4761 struct ath_buf *last = ATH_TXQ_LAST(txq); 4762 struct ieee80211_frame *wh; 4763 4764 /* mark previous frame */ 4765 wh = mtod(last->bf_m, struct ieee80211_frame *); 4766 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA; 4767 bus_dmamap_sync(sc->sc_dmat, last->bf_dmamap, 4768 BUS_DMASYNC_PREWRITE); 4769 4770 /* link descriptor */ 4771 *txq->axq_link = bf->bf_daddr; 4772 } 4773 ATH_TXQ_INSERT_TAIL(txq, bf, bf_list); 4774 txq->axq_link = &bf->bf_desc[bf->bf_nseg - 1].ds_link; 4775 } 4776 ATH_TXQ_UNLOCK(txq); 4777 } 4778 4779 static int 4780 ath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni, struct ath_buf *bf, 4781 struct mbuf *m0) 4782 { 4783 struct ieee80211vap *vap = ni->ni_vap; 4784 struct ath_vap *avp = ATH_VAP(vap); 4785 struct ath_hal *ah = sc->sc_ah; 4786 struct ifnet *ifp = sc->sc_ifp; 4787 struct ieee80211com *ic = ifp->if_l2com; 4788 const struct chanAccParams *cap = &ic->ic_wme.wme_chanParams; 4789 int error, iswep, ismcast, isfrag, ismrr; 4790 int keyix, hdrlen, pktlen, try0; 4791 u_int8_t rix, txrate, ctsrate; 4792 u_int8_t cix = 0xff; /* NB: silence compiler */ 4793 struct ath_desc *ds; 4794 struct ath_txq *txq; 4795 struct ieee80211_frame *wh; 4796 u_int subtype, flags, ctsduration; 4797 HAL_PKT_TYPE atype; 4798 const HAL_RATE_TABLE *rt; 4799 HAL_BOOL shortPreamble; 4800 struct ath_node *an; 4801 u_int pri; 4802 4803 wh = mtod(m0, struct ieee80211_frame *); 4804 iswep = wh->i_fc[1] & IEEE80211_FC1_WEP; 4805 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1); 4806 isfrag = m0->m_flags & M_FRAG; 4807 hdrlen = ieee80211_anyhdrsize(wh); 4808 /* 4809 * Packet length must not include any 4810 * pad bytes; deduct them here. 4811 */ 4812 pktlen = m0->m_pkthdr.len - (hdrlen & 3); 4813 4814 if (iswep) { 4815 const struct ieee80211_cipher *cip; 4816 struct ieee80211_key *k; 4817 4818 /* 4819 * Construct the 802.11 header+trailer for an encrypted 4820 * frame. The only reason this can fail is because of an 4821 * unknown or unsupported cipher/key type. 4822 */ 4823 k = ieee80211_crypto_encap(ni, m0); 4824 if (k == NULL) { 4825 /* 4826 * This can happen when the key is yanked after the 4827 * frame was queued. Just discard the frame; the 4828 * 802.11 layer counts failures and provides 4829 * debugging/diagnostics. 4830 */ 4831 ath_freetx(m0); 4832 return EIO; 4833 } 4834 /* 4835 * Adjust the packet + header lengths for the crypto 4836 * additions and calculate the h/w key index. When 4837 * a s/w mic is done the frame will have had any mic 4838 * added to it prior to entry so m0->m_pkthdr.len will 4839 * account for it. Otherwise we need to add it to the 4840 * packet length. 4841 */ 4842 cip = k->wk_cipher; 4843 hdrlen += cip->ic_header; 4844 pktlen += cip->ic_header + cip->ic_trailer; 4845 /* NB: frags always have any TKIP MIC done in s/w */ 4846 if ((k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && !isfrag) 4847 pktlen += cip->ic_miclen; 4848 keyix = k->wk_keyix; 4849 4850 /* packet header may have moved, reset our local pointer */ 4851 wh = mtod(m0, struct ieee80211_frame *); 4852 } else if (ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) { 4853 /* 4854 * Use station key cache slot, if assigned. 4855 */ 4856 keyix = ni->ni_ucastkey.wk_keyix; 4857 if (keyix == IEEE80211_KEYIX_NONE) 4858 keyix = HAL_TXKEYIX_INVALID; 4859 } else 4860 keyix = HAL_TXKEYIX_INVALID; 4861 4862 pktlen += IEEE80211_CRC_LEN; 4863 4864 /* 4865 * Load the DMA map so any coalescing is done. This 4866 * also calculates the number of descriptors we need. 4867 */ 4868 error = ath_tx_dmasetup(sc, bf, m0); 4869 if (error != 0) 4870 return error; 4871 bf->bf_node = ni; /* NB: held reference */ 4872 m0 = bf->bf_m; /* NB: may have changed */ 4873 wh = mtod(m0, struct ieee80211_frame *); 4874 4875 /* setup descriptors */ 4876 ds = bf->bf_desc; 4877 rt = sc->sc_currates; 4878 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode)); 4879 4880 /* 4881 * NB: the 802.11 layer marks whether or not we should 4882 * use short preamble based on the current mode and 4883 * negotiated parameters. 4884 */ 4885 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 4886 (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) { 4887 shortPreamble = AH_TRUE; 4888 sc->sc_stats.ast_tx_shortpre++; 4889 } else { 4890 shortPreamble = AH_FALSE; 4891 } 4892 4893 an = ATH_NODE(ni); 4894 flags = HAL_TXDESC_CLRDMASK; /* XXX needed for crypto errs */ 4895 ismrr = 0; /* default no multi-rate retry*/ 4896 pri = M_WME_GETAC(m0); /* honor classification */ 4897 /* XXX use txparams instead of fixed values */ 4898 /* 4899 * Calculate Atheros packet type from IEEE80211 packet header, 4900 * setup for rate calculations, and select h/w transmit queue. 4901 */ 4902 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) { 4903 case IEEE80211_FC0_TYPE_MGT: 4904 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 4905 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) 4906 atype = HAL_PKT_TYPE_BEACON; 4907 else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) 4908 atype = HAL_PKT_TYPE_PROBE_RESP; 4909 else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM) 4910 atype = HAL_PKT_TYPE_ATIM; 4911 else 4912 atype = HAL_PKT_TYPE_NORMAL; /* XXX */ 4913 rix = an->an_mgmtrix; 4914 txrate = rt->info[rix].rateCode; 4915 if (shortPreamble) 4916 txrate |= rt->info[rix].shortPreamble; 4917 try0 = ATH_TXMGTTRY; 4918 flags |= HAL_TXDESC_INTREQ; /* force interrupt */ 4919 break; 4920 case IEEE80211_FC0_TYPE_CTL: 4921 atype = HAL_PKT_TYPE_PSPOLL; /* stop setting of duration */ 4922 rix = an->an_mgmtrix; 4923 txrate = rt->info[rix].rateCode; 4924 if (shortPreamble) 4925 txrate |= rt->info[rix].shortPreamble; 4926 try0 = ATH_TXMGTTRY; 4927 flags |= HAL_TXDESC_INTREQ; /* force interrupt */ 4928 break; 4929 case IEEE80211_FC0_TYPE_DATA: 4930 atype = HAL_PKT_TYPE_NORMAL; /* default */ 4931 /* 4932 * Data frames: multicast frames go out at a fixed rate, 4933 * EAPOL frames use the mgmt frame rate; otherwise consult 4934 * the rate control module for the rate to use. 4935 */ 4936 if (ismcast) { 4937 rix = an->an_mcastrix; 4938 txrate = rt->info[rix].rateCode; 4939 if (shortPreamble) 4940 txrate |= rt->info[rix].shortPreamble; 4941 try0 = 1; 4942 } else if (m0->m_flags & M_EAPOL) { 4943 /* XXX? maybe always use long preamble? */ 4944 rix = an->an_mgmtrix; 4945 txrate = rt->info[rix].rateCode; 4946 if (shortPreamble) 4947 txrate |= rt->info[rix].shortPreamble; 4948 try0 = ATH_TXMAXTRY; /* XXX?too many? */ 4949 } else { 4950 ath_rate_findrate(sc, an, shortPreamble, pktlen, 4951 &rix, &try0, &txrate); 4952 sc->sc_txrix = rix; /* for LED blinking */ 4953 sc->sc_lastdatarix = rix; /* for fast frames */ 4954 if (try0 != ATH_TXMAXTRY) 4955 ismrr = 1; 4956 } 4957 if (cap->cap_wmeParams[pri].wmep_noackPolicy) 4958 flags |= HAL_TXDESC_NOACK; 4959 break; 4960 default: 4961 if_printf(ifp, "bogus frame type 0x%x (%s)\n", 4962 wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__); 4963 /* XXX statistic */ 4964 ath_freetx(m0); 4965 return EIO; 4966 } 4967 txq = sc->sc_ac2q[pri]; 4968 4969 /* 4970 * When servicing one or more stations in power-save mode 4971 * (or) if there is some mcast data waiting on the mcast 4972 * queue (to prevent out of order delivery) multicast 4973 * frames must be buffered until after the beacon. 4974 */ 4975 if (ismcast && (vap->iv_ps_sta || avp->av_mcastq.axq_depth)) 4976 txq = &avp->av_mcastq; 4977 4978 /* 4979 * Calculate miscellaneous flags. 4980 */ 4981 if (ismcast) { 4982 flags |= HAL_TXDESC_NOACK; /* no ack on broad/multicast */ 4983 } else if (pktlen > vap->iv_rtsthreshold && 4984 (ni->ni_ath_flags & IEEE80211_NODE_FF) == 0) { 4985 flags |= HAL_TXDESC_RTSENA; /* RTS based on frame length */ 4986 cix = rt->info[rix].controlRate; 4987 sc->sc_stats.ast_tx_rts++; 4988 } 4989 if (flags & HAL_TXDESC_NOACK) /* NB: avoid double counting */ 4990 sc->sc_stats.ast_tx_noack++; 4991 #ifdef ATH_SUPPORT_TDMA 4992 if (sc->sc_tdma && (flags & HAL_TXDESC_NOACK) == 0) { 4993 DPRINTF(sc, ATH_DEBUG_TDMA, 4994 "%s: discard frame, ACK required w/ TDMA\n", __func__); 4995 sc->sc_stats.ast_tdma_ack++; 4996 ath_freetx(m0); 4997 return EIO; 4998 } 4999 #endif 5000 5001 /* 5002 * If 802.11g protection is enabled, determine whether 5003 * to use RTS/CTS or just CTS. Note that this is only 5004 * done for OFDM unicast frames. 5005 */ 5006 if ((ic->ic_flags & IEEE80211_F_USEPROT) && 5007 rt->info[rix].phy == IEEE80211_T_OFDM && 5008 (flags & HAL_TXDESC_NOACK) == 0) { 5009 /* XXX fragments must use CCK rates w/ protection */ 5010 if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) 5011 flags |= HAL_TXDESC_RTSENA; 5012 else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) 5013 flags |= HAL_TXDESC_CTSENA; 5014 if (isfrag) { 5015 /* 5016 * For frags it would be desirable to use the 5017 * highest CCK rate for RTS/CTS. But stations 5018 * farther away may detect it at a lower CCK rate 5019 * so use the configured protection rate instead 5020 * (for now). 5021 */ 5022 cix = rt->info[sc->sc_protrix].controlRate; 5023 } else 5024 cix = rt->info[sc->sc_protrix].controlRate; 5025 sc->sc_stats.ast_tx_protect++; 5026 } 5027 5028 /* 5029 * Calculate duration. This logically belongs in the 802.11 5030 * layer but it lacks sufficient information to calculate it. 5031 */ 5032 if ((flags & HAL_TXDESC_NOACK) == 0 && 5033 (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL) { 5034 u_int16_t dur; 5035 if (shortPreamble) 5036 dur = rt->info[rix].spAckDuration; 5037 else 5038 dur = rt->info[rix].lpAckDuration; 5039 if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) { 5040 dur += dur; /* additional SIFS+ACK */ 5041 KASSERT(m0->m_nextpkt != NULL, ("no fragment")); 5042 /* 5043 * Include the size of next fragment so NAV is 5044 * updated properly. The last fragment uses only 5045 * the ACK duration 5046 */ 5047 dur += ath_hal_computetxtime(ah, rt, 5048 m0->m_nextpkt->m_pkthdr.len, 5049 rix, shortPreamble); 5050 } 5051 if (isfrag) { 5052 /* 5053 * Force hardware to use computed duration for next 5054 * fragment by disabling multi-rate retry which updates 5055 * duration based on the multi-rate duration table. 5056 */ 5057 ismrr = 0; 5058 try0 = ATH_TXMGTTRY; /* XXX? */ 5059 } 5060 *(u_int16_t *)wh->i_dur = htole16(dur); 5061 } 5062 5063 /* 5064 * Calculate RTS/CTS rate and duration if needed. 5065 */ 5066 ctsduration = 0; 5067 if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA)) { 5068 /* 5069 * CTS transmit rate is derived from the transmit rate 5070 * by looking in the h/w rate table. We must also factor 5071 * in whether or not a short preamble is to be used. 5072 */ 5073 /* NB: cix is set above where RTS/CTS is enabled */ 5074 KASSERT(cix != 0xff, ("cix not setup")); 5075 ctsrate = rt->info[cix].rateCode; 5076 /* 5077 * Compute the transmit duration based on the frame 5078 * size and the size of an ACK frame. We call into the 5079 * HAL to do the computation since it depends on the 5080 * characteristics of the actual PHY being used. 5081 * 5082 * NB: CTS is assumed the same size as an ACK so we can 5083 * use the precalculated ACK durations. 5084 */ 5085 if (shortPreamble) { 5086 ctsrate |= rt->info[cix].shortPreamble; 5087 if (flags & HAL_TXDESC_RTSENA) /* SIFS + CTS */ 5088 ctsduration += rt->info[cix].spAckDuration; 5089 ctsduration += ath_hal_computetxtime(ah, 5090 rt, pktlen, rix, AH_TRUE); 5091 if ((flags & HAL_TXDESC_NOACK) == 0) /* SIFS + ACK */ 5092 ctsduration += rt->info[rix].spAckDuration; 5093 } else { 5094 if (flags & HAL_TXDESC_RTSENA) /* SIFS + CTS */ 5095 ctsduration += rt->info[cix].lpAckDuration; 5096 ctsduration += ath_hal_computetxtime(ah, 5097 rt, pktlen, rix, AH_FALSE); 5098 if ((flags & HAL_TXDESC_NOACK) == 0) /* SIFS + ACK */ 5099 ctsduration += rt->info[rix].lpAckDuration; 5100 } 5101 /* 5102 * Must disable multi-rate retry when using RTS/CTS. 5103 */ 5104 ismrr = 0; 5105 try0 = ATH_TXMGTTRY; /* XXX */ 5106 } else 5107 ctsrate = 0; 5108 5109 /* 5110 * At this point we are committed to sending the frame 5111 * and we don't need to look at m_nextpkt; clear it in 5112 * case this frame is part of frag chain. 5113 */ 5114 m0->m_nextpkt = NULL; 5115 5116 if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT)) 5117 ieee80211_dump_pkt(ic, mtod(m0, caddr_t), m0->m_len, 5118 sc->sc_hwmap[rix].ieeerate, -1); 5119 5120 if (bpf_peers_present(ifp->if_bpf)) { 5121 u_int64_t tsf = ath_hal_gettsf64(ah); 5122 5123 sc->sc_tx_th.wt_tsf = htole64(tsf); 5124 sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags; 5125 if (iswep) 5126 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP; 5127 if (isfrag) 5128 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG; 5129 sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate; 5130 sc->sc_tx_th.wt_txpower = ni->ni_txpower; 5131 sc->sc_tx_th.wt_antenna = sc->sc_txantenna; 5132 5133 bpf_mtap2(ifp->if_bpf, &sc->sc_tx_th, sc->sc_tx_th_len, m0); 5134 } 5135 5136 /* 5137 * Determine if a tx interrupt should be generated for 5138 * this descriptor. We take a tx interrupt to reap 5139 * descriptors when the h/w hits an EOL condition or 5140 * when the descriptor is specifically marked to generate 5141 * an interrupt. We periodically mark descriptors in this 5142 * way to insure timely replenishing of the supply needed 5143 * for sending frames. Defering interrupts reduces system 5144 * load and potentially allows more concurrent work to be 5145 * done but if done to aggressively can cause senders to 5146 * backup. 5147 * 5148 * NB: use >= to deal with sc_txintrperiod changing 5149 * dynamically through sysctl. 5150 */ 5151 if (flags & HAL_TXDESC_INTREQ) { 5152 txq->axq_intrcnt = 0; 5153 } else if (++txq->axq_intrcnt >= sc->sc_txintrperiod) { 5154 flags |= HAL_TXDESC_INTREQ; 5155 txq->axq_intrcnt = 0; 5156 } 5157 5158 /* 5159 * Formulate first tx descriptor with tx controls. 5160 */ 5161 /* XXX check return value? */ 5162 ath_hal_setuptxdesc(ah, ds 5163 , pktlen /* packet length */ 5164 , hdrlen /* header length */ 5165 , atype /* Atheros packet type */ 5166 , ni->ni_txpower /* txpower */ 5167 , txrate, try0 /* series 0 rate/tries */ 5168 , keyix /* key cache index */ 5169 , sc->sc_txantenna /* antenna mode */ 5170 , flags /* flags */ 5171 , ctsrate /* rts/cts rate */ 5172 , ctsduration /* rts/cts duration */ 5173 ); 5174 bf->bf_txflags = flags; 5175 /* 5176 * Setup the multi-rate retry state only when we're 5177 * going to use it. This assumes ath_hal_setuptxdesc 5178 * initializes the descriptors (so we don't have to) 5179 * when the hardware supports multi-rate retry and 5180 * we don't use it. 5181 */ 5182 if (ismrr) 5183 ath_rate_setupxtxdesc(sc, an, ds, shortPreamble, rix); 5184 5185 ath_tx_handoff(sc, txq, bf); 5186 return 0; 5187 } 5188 5189 /* 5190 * Process completed xmit descriptors from the specified queue. 5191 */ 5192 static int 5193 ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq) 5194 { 5195 struct ath_hal *ah = sc->sc_ah; 5196 struct ifnet *ifp = sc->sc_ifp; 5197 struct ieee80211com *ic = ifp->if_l2com; 5198 struct ath_buf *bf, *last; 5199 struct ath_desc *ds, *ds0; 5200 struct ath_tx_status *ts; 5201 struct ieee80211_node *ni; 5202 struct ath_node *an; 5203 int sr, lr, pri, nacked; 5204 HAL_STATUS status; 5205 5206 DPRINTF(sc, ATH_DEBUG_TX_PROC, "%s: tx queue %u head %p link %p\n", 5207 __func__, txq->axq_qnum, 5208 (caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum), 5209 txq->axq_link); 5210 nacked = 0; 5211 for (;;) { 5212 ATH_TXQ_LOCK(txq); 5213 txq->axq_intrcnt = 0; /* reset periodic desc intr count */ 5214 bf = STAILQ_FIRST(&txq->axq_q); 5215 if (bf == NULL) { 5216 ATH_TXQ_UNLOCK(txq); 5217 break; 5218 } 5219 ds0 = &bf->bf_desc[0]; 5220 ds = &bf->bf_desc[bf->bf_nseg - 1]; 5221 ts = &bf->bf_status.ds_txstat; 5222 status = ath_hal_txprocdesc(ah, ds, ts); 5223 #ifdef ATH_DEBUG 5224 if (sc->sc_debug & ATH_DEBUG_XMIT_DESC) 5225 ath_printtxbuf(sc, bf, txq->axq_qnum, 0, 5226 status == HAL_OK); 5227 #endif 5228 if (status == HAL_EINPROGRESS) { 5229 ATH_TXQ_UNLOCK(txq); 5230 break; 5231 } 5232 ATH_TXQ_REMOVE_HEAD(txq, bf_list); 5233 #ifdef ATH_SUPPORT_TDMA 5234 if (txq->axq_depth > 0) { 5235 /* 5236 * More frames follow. Mark the buffer busy 5237 * so it's not re-used while the hardware may 5238 * still re-read the link field in the descriptor. 5239 */ 5240 bf->bf_flags |= ATH_BUF_BUSY; 5241 } else 5242 #else 5243 if (txq->axq_depth == 0) 5244 #endif 5245 txq->axq_link = NULL; 5246 ATH_TXQ_UNLOCK(txq); 5247 5248 ni = bf->bf_node; 5249 if (ni != NULL) { 5250 an = ATH_NODE(ni); 5251 if (ts->ts_status == 0) { 5252 u_int8_t txant = ts->ts_antenna; 5253 sc->sc_stats.ast_ant_tx[txant]++; 5254 sc->sc_ant_tx[txant]++; 5255 if (ts->ts_rate & HAL_TXSTAT_ALTRATE) 5256 sc->sc_stats.ast_tx_altrate++; 5257 pri = M_WME_GETAC(bf->bf_m); 5258 if (pri >= WME_AC_VO) 5259 ic->ic_wme.wme_hipri_traffic++; 5260 if ((bf->bf_txflags & HAL_TXDESC_NOACK) == 0) 5261 ni->ni_inact = ni->ni_inact_reload; 5262 } else { 5263 if (ts->ts_status & HAL_TXERR_XRETRY) 5264 sc->sc_stats.ast_tx_xretries++; 5265 if (ts->ts_status & HAL_TXERR_FIFO) 5266 sc->sc_stats.ast_tx_fifoerr++; 5267 if (ts->ts_status & HAL_TXERR_FILT) 5268 sc->sc_stats.ast_tx_filtered++; 5269 if (bf->bf_m->m_flags & M_FF) 5270 sc->sc_stats.ast_ff_txerr++; 5271 } 5272 sr = ts->ts_shortretry; 5273 lr = ts->ts_longretry; 5274 sc->sc_stats.ast_tx_shortretry += sr; 5275 sc->sc_stats.ast_tx_longretry += lr; 5276 /* 5277 * Hand the descriptor to the rate control algorithm. 5278 */ 5279 if ((ts->ts_status & HAL_TXERR_FILT) == 0 && 5280 (bf->bf_txflags & HAL_TXDESC_NOACK) == 0) { 5281 /* 5282 * If frame was ack'd update statistics, 5283 * including the last rx time used to 5284 * workaround phantom bmiss interrupts. 5285 */ 5286 if (ts->ts_status == 0) { 5287 nacked++; 5288 sc->sc_stats.ast_tx_rssi = ts->ts_rssi; 5289 ATH_RSSI_LPF(sc->sc_halstats.ns_avgtxrssi, 5290 ts->ts_rssi); 5291 } 5292 ath_rate_tx_complete(sc, an, bf); 5293 } 5294 /* 5295 * Do any tx complete callback. Note this must 5296 * be done before releasing the node reference. 5297 */ 5298 if (bf->bf_m->m_flags & M_TXCB) 5299 ieee80211_process_callback(ni, bf->bf_m, 5300 (bf->bf_txflags & HAL_TXDESC_NOACK) == 0 ? 5301 ts->ts_status : HAL_TXERR_XRETRY); 5302 /* 5303 * Reclaim reference to node. 5304 * 5305 * NB: the node may be reclaimed here if, for example 5306 * this is a DEAUTH message that was sent and the 5307 * node was timed out due to inactivity. 5308 */ 5309 ieee80211_free_node(ni); 5310 } 5311 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, 5312 BUS_DMASYNC_POSTWRITE); 5313 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 5314 5315 m_freem(bf->bf_m); 5316 bf->bf_m = NULL; 5317 bf->bf_node = NULL; 5318 5319 ATH_TXBUF_LOCK(sc); 5320 last = STAILQ_LAST(&sc->sc_txbuf, ath_buf, bf_list); 5321 if (last != NULL) 5322 last->bf_flags &= ~ATH_BUF_BUSY; 5323 STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list); 5324 ATH_TXBUF_UNLOCK(sc); 5325 } 5326 /* 5327 * Flush fast-frame staging queue when traffic slows. 5328 */ 5329 if (txq->axq_depth <= 1) 5330 ath_ff_stageq_flush(sc, txq, ath_ff_always); 5331 return nacked; 5332 } 5333 5334 static __inline int 5335 txqactive(struct ath_hal *ah, int qnum) 5336 { 5337 u_int32_t txqs = 1<<qnum; 5338 ath_hal_gettxintrtxqs(ah, &txqs); 5339 return (txqs & (1<<qnum)); 5340 } 5341 5342 /* 5343 * Deferred processing of transmit interrupt; special-cased 5344 * for a single hardware transmit queue (e.g. 5210 and 5211). 5345 */ 5346 static void 5347 ath_tx_proc_q0(void *arg, int npending) 5348 { 5349 struct ath_softc *sc = arg; 5350 struct ifnet *ifp = sc->sc_ifp; 5351 5352 if (txqactive(sc->sc_ah, 0) && ath_tx_processq(sc, &sc->sc_txq[0])) 5353 sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah); 5354 if (txqactive(sc->sc_ah, sc->sc_cabq->axq_qnum)) 5355 ath_tx_processq(sc, sc->sc_cabq); 5356 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 5357 sc->sc_wd_timer = 0; 5358 5359 if (sc->sc_softled) 5360 ath_led_event(sc, sc->sc_txrix); 5361 5362 ath_start(ifp); 5363 } 5364 5365 /* 5366 * Deferred processing of transmit interrupt; special-cased 5367 * for four hardware queues, 0-3 (e.g. 5212 w/ WME support). 5368 */ 5369 static void 5370 ath_tx_proc_q0123(void *arg, int npending) 5371 { 5372 struct ath_softc *sc = arg; 5373 struct ifnet *ifp = sc->sc_ifp; 5374 int nacked; 5375 5376 /* 5377 * Process each active queue. 5378 */ 5379 nacked = 0; 5380 if (txqactive(sc->sc_ah, 0)) 5381 nacked += ath_tx_processq(sc, &sc->sc_txq[0]); 5382 if (txqactive(sc->sc_ah, 1)) 5383 nacked += ath_tx_processq(sc, &sc->sc_txq[1]); 5384 if (txqactive(sc->sc_ah, 2)) 5385 nacked += ath_tx_processq(sc, &sc->sc_txq[2]); 5386 if (txqactive(sc->sc_ah, 3)) 5387 nacked += ath_tx_processq(sc, &sc->sc_txq[3]); 5388 if (txqactive(sc->sc_ah, sc->sc_cabq->axq_qnum)) 5389 ath_tx_processq(sc, sc->sc_cabq); 5390 if (nacked) 5391 sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah); 5392 5393 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 5394 sc->sc_wd_timer = 0; 5395 5396 if (sc->sc_softled) 5397 ath_led_event(sc, sc->sc_txrix); 5398 5399 ath_start(ifp); 5400 } 5401 5402 /* 5403 * Deferred processing of transmit interrupt. 5404 */ 5405 static void 5406 ath_tx_proc(void *arg, int npending) 5407 { 5408 struct ath_softc *sc = arg; 5409 struct ifnet *ifp = sc->sc_ifp; 5410 int i, nacked; 5411 5412 /* 5413 * Process each active queue. 5414 */ 5415 nacked = 0; 5416 for (i = 0; i < HAL_NUM_TX_QUEUES; i++) 5417 if (ATH_TXQ_SETUP(sc, i) && txqactive(sc->sc_ah, i)) 5418 nacked += ath_tx_processq(sc, &sc->sc_txq[i]); 5419 if (nacked) 5420 sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah); 5421 5422 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 5423 sc->sc_wd_timer = 0; 5424 5425 if (sc->sc_softled) 5426 ath_led_event(sc, sc->sc_txrix); 5427 5428 ath_start(ifp); 5429 } 5430 5431 static void 5432 ath_tx_draintxq(struct ath_softc *sc, struct ath_txq *txq) 5433 { 5434 #ifdef ATH_DEBUG 5435 struct ath_hal *ah = sc->sc_ah; 5436 #endif 5437 struct ieee80211_node *ni; 5438 struct ath_buf *bf; 5439 u_int ix; 5440 5441 /* 5442 * NB: this assumes output has been stopped and 5443 * we do not need to block ath_tx_proc 5444 */ 5445 ATH_TXBUF_LOCK(sc); 5446 bf = STAILQ_LAST(&sc->sc_txbuf, ath_buf, bf_list); 5447 if (bf != NULL) 5448 bf->bf_flags &= ~ATH_BUF_BUSY; 5449 ATH_TXBUF_UNLOCK(sc); 5450 for (ix = 0;; ix++) { 5451 ATH_TXQ_LOCK(txq); 5452 bf = STAILQ_FIRST(&txq->axq_q); 5453 if (bf == NULL) { 5454 txq->axq_link = NULL; 5455 ATH_TXQ_UNLOCK(txq); 5456 break; 5457 } 5458 ATH_TXQ_REMOVE_HEAD(txq, bf_list); 5459 ATH_TXQ_UNLOCK(txq); 5460 #ifdef ATH_DEBUG 5461 if (sc->sc_debug & ATH_DEBUG_RESET) { 5462 struct ieee80211com *ic = sc->sc_ifp->if_l2com; 5463 5464 ath_printtxbuf(sc, bf, txq->axq_qnum, ix, 5465 ath_hal_txprocdesc(ah, bf->bf_desc, 5466 &bf->bf_status.ds_txstat) == HAL_OK); 5467 ieee80211_dump_pkt(ic, mtod(bf->bf_m, caddr_t), 5468 bf->bf_m->m_len, 0, -1); 5469 } 5470 #endif /* ATH_DEBUG */ 5471 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 5472 ni = bf->bf_node; 5473 bf->bf_node = NULL; 5474 if (ni != NULL) { 5475 /* 5476 * Do any callback and reclaim the node reference. 5477 */ 5478 if (bf->bf_m->m_flags & M_TXCB) 5479 ieee80211_process_callback(ni, bf->bf_m, -1); 5480 ieee80211_free_node(ni); 5481 } 5482 m_freem(bf->bf_m); 5483 bf->bf_m = NULL; 5484 bf->bf_flags &= ~ATH_BUF_BUSY; 5485 5486 ATH_TXBUF_LOCK(sc); 5487 STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list); 5488 ATH_TXBUF_UNLOCK(sc); 5489 } 5490 } 5491 5492 static void 5493 ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq) 5494 { 5495 struct ath_hal *ah = sc->sc_ah; 5496 5497 DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n", 5498 __func__, txq->axq_qnum, 5499 (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, txq->axq_qnum), 5500 txq->axq_link); 5501 (void) ath_hal_stoptxdma(ah, txq->axq_qnum); 5502 } 5503 5504 /* 5505 * Drain the transmit queues and reclaim resources. 5506 */ 5507 static void 5508 ath_draintxq(struct ath_softc *sc) 5509 { 5510 struct ath_hal *ah = sc->sc_ah; 5511 struct ifnet *ifp = sc->sc_ifp; 5512 int i; 5513 5514 /* XXX return value */ 5515 if (!sc->sc_invalid) { 5516 /* don't touch the hardware if marked invalid */ 5517 DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n", 5518 __func__, sc->sc_bhalq, 5519 (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq), 5520 NULL); 5521 (void) ath_hal_stoptxdma(ah, sc->sc_bhalq); 5522 for (i = 0; i < HAL_NUM_TX_QUEUES; i++) 5523 if (ATH_TXQ_SETUP(sc, i)) 5524 ath_tx_stopdma(sc, &sc->sc_txq[i]); 5525 } 5526 for (i = 0; i < HAL_NUM_TX_QUEUES; i++) 5527 if (ATH_TXQ_SETUP(sc, i)) 5528 ath_tx_draintxq(sc, &sc->sc_txq[i]); 5529 #ifdef ATH_DEBUG 5530 if (sc->sc_debug & ATH_DEBUG_RESET) { 5531 struct ath_buf *bf = STAILQ_FIRST(&sc->sc_bbuf); 5532 if (bf != NULL && bf->bf_m != NULL) { 5533 ath_printtxbuf(sc, bf, sc->sc_bhalq, 0, 5534 ath_hal_txprocdesc(ah, bf->bf_desc, 5535 &bf->bf_status.ds_txstat) == HAL_OK); 5536 ieee80211_dump_pkt(ifp->if_l2com, mtod(bf->bf_m, caddr_t), 5537 bf->bf_m->m_len, 0, -1); 5538 } 5539 } 5540 #endif /* ATH_DEBUG */ 5541 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 5542 sc->sc_wd_timer = 0; 5543 } 5544 5545 /* 5546 * Disable the receive h/w in preparation for a reset. 5547 */ 5548 static void 5549 ath_stoprecv(struct ath_softc *sc) 5550 { 5551 #define PA2DESC(_sc, _pa) \ 5552 ((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \ 5553 ((_pa) - (_sc)->sc_rxdma.dd_desc_paddr))) 5554 struct ath_hal *ah = sc->sc_ah; 5555 5556 ath_hal_stoppcurecv(ah); /* disable PCU */ 5557 ath_hal_setrxfilter(ah, 0); /* clear recv filter */ 5558 ath_hal_stopdmarecv(ah); /* disable DMA engine */ 5559 DELAY(3000); /* 3ms is long enough for 1 frame */ 5560 #ifdef ATH_DEBUG 5561 if (sc->sc_debug & (ATH_DEBUG_RESET | ATH_DEBUG_FATAL)) { 5562 struct ath_buf *bf; 5563 u_int ix; 5564 5565 printf("%s: rx queue %p, link %p\n", __func__, 5566 (caddr_t)(uintptr_t) ath_hal_getrxbuf(ah), sc->sc_rxlink); 5567 ix = 0; 5568 STAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) { 5569 struct ath_desc *ds = bf->bf_desc; 5570 struct ath_rx_status *rs = &bf->bf_status.ds_rxstat; 5571 HAL_STATUS status = ath_hal_rxprocdesc(ah, ds, 5572 bf->bf_daddr, PA2DESC(sc, ds->ds_link), rs); 5573 if (status == HAL_OK || (sc->sc_debug & ATH_DEBUG_FATAL)) 5574 ath_printrxbuf(sc, bf, ix, status == HAL_OK); 5575 ix++; 5576 } 5577 } 5578 #endif 5579 if (sc->sc_rxpending != NULL) { 5580 m_freem(sc->sc_rxpending); 5581 sc->sc_rxpending = NULL; 5582 } 5583 sc->sc_rxlink = NULL; /* just in case */ 5584 #undef PA2DESC 5585 } 5586 5587 /* 5588 * Enable the receive h/w following a reset. 5589 */ 5590 static int 5591 ath_startrecv(struct ath_softc *sc) 5592 { 5593 struct ath_hal *ah = sc->sc_ah; 5594 struct ath_buf *bf; 5595 5596 sc->sc_rxlink = NULL; 5597 sc->sc_rxpending = NULL; 5598 STAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) { 5599 int error = ath_rxbuf_init(sc, bf); 5600 if (error != 0) { 5601 DPRINTF(sc, ATH_DEBUG_RECV, 5602 "%s: ath_rxbuf_init failed %d\n", 5603 __func__, error); 5604 return error; 5605 } 5606 } 5607 5608 bf = STAILQ_FIRST(&sc->sc_rxbuf); 5609 ath_hal_putrxbuf(ah, bf->bf_daddr); 5610 ath_hal_rxena(ah); /* enable recv descriptors */ 5611 ath_mode_init(sc); /* set filters, etc. */ 5612 ath_hal_startpcurecv(ah); /* re-enable PCU/DMA engine */ 5613 return 0; 5614 } 5615 5616 /* 5617 * Update internal state after a channel change. 5618 */ 5619 static void 5620 ath_chan_change(struct ath_softc *sc, struct ieee80211_channel *chan) 5621 { 5622 enum ieee80211_phymode mode; 5623 5624 /* 5625 * Change channels and update the h/w rate map 5626 * if we're switching; e.g. 11a to 11b/g. 5627 */ 5628 mode = ieee80211_chan2mode(chan); 5629 if (mode != sc->sc_curmode) 5630 ath_setcurmode(sc, mode); 5631 sc->sc_curchan = chan; 5632 5633 sc->sc_rx_th.wr_chan_flags = htole32(chan->ic_flags); 5634 sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags; 5635 sc->sc_rx_th.wr_chan_freq = htole16(chan->ic_freq); 5636 sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq; 5637 sc->sc_rx_th.wr_chan_ieee = chan->ic_ieee; 5638 sc->sc_tx_th.wt_chan_ieee = sc->sc_rx_th.wr_chan_ieee; 5639 sc->sc_rx_th.wr_chan_maxpow = chan->ic_maxregpower; 5640 sc->sc_tx_th.wt_chan_maxpow = sc->sc_rx_th.wr_chan_maxpow; 5641 } 5642 5643 /* 5644 * Set/change channels. If the channel is really being changed, 5645 * it's done by reseting the chip. To accomplish this we must 5646 * first cleanup any pending DMA, then restart stuff after a la 5647 * ath_init. 5648 */ 5649 static int 5650 ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan) 5651 { 5652 struct ifnet *ifp = sc->sc_ifp; 5653 struct ieee80211com *ic = ifp->if_l2com; 5654 struct ath_hal *ah = sc->sc_ah; 5655 5656 DPRINTF(sc, ATH_DEBUG_RESET, "%s: %u (%u MHz, flags 0x%x)\n", 5657 __func__, ieee80211_chan2ieee(ic, chan), 5658 chan->ic_freq, chan->ic_flags); 5659 if (chan != sc->sc_curchan) { 5660 HAL_STATUS status; 5661 /* 5662 * To switch channels clear any pending DMA operations; 5663 * wait long enough for the RX fifo to drain, reset the 5664 * hardware at the new frequency, and then re-enable 5665 * the relevant bits of the h/w. 5666 */ 5667 ath_hal_intrset(ah, 0); /* disable interrupts */ 5668 ath_draintxq(sc); /* clear pending tx frames */ 5669 ath_stoprecv(sc); /* turn off frame recv */ 5670 if (!ath_hal_reset(ah, sc->sc_opmode, chan, AH_TRUE, &status)) { 5671 if_printf(ifp, "%s: unable to reset " 5672 "channel %u (%u Mhz, flags 0x%x), hal status %u\n", 5673 __func__, ieee80211_chan2ieee(ic, chan), 5674 chan->ic_freq, chan->ic_flags, status); 5675 return EIO; 5676 } 5677 sc->sc_diversity = ath_hal_getdiversity(ah); 5678 5679 /* 5680 * Re-enable rx framework. 5681 */ 5682 if (ath_startrecv(sc) != 0) { 5683 if_printf(ifp, "%s: unable to restart recv logic\n", 5684 __func__); 5685 return EIO; 5686 } 5687 5688 /* 5689 * Change channels and update the h/w rate map 5690 * if we're switching; e.g. 11a to 11b/g. 5691 */ 5692 ath_chan_change(sc, chan); 5693 5694 /* 5695 * Re-enable interrupts. 5696 */ 5697 ath_hal_intrset(ah, sc->sc_imask); 5698 } 5699 return 0; 5700 } 5701 5702 /* 5703 * Periodically recalibrate the PHY to account 5704 * for temperature/environment changes. 5705 */ 5706 static void 5707 ath_calibrate(void *arg) 5708 { 5709 struct ath_softc *sc = arg; 5710 struct ath_hal *ah = sc->sc_ah; 5711 struct ifnet *ifp = sc->sc_ifp; 5712 struct ieee80211com *ic = ifp->if_l2com; 5713 HAL_BOOL longCal, isCalDone; 5714 int nextcal; 5715 5716 if (ic->ic_flags & IEEE80211_F_SCAN) /* defer, off channel */ 5717 goto restart; 5718 longCal = (ticks - sc->sc_lastlongcal >= ath_longcalinterval*hz); 5719 if (longCal) { 5720 sc->sc_stats.ast_per_cal++; 5721 if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) { 5722 /* 5723 * Rfgain is out of bounds, reset the chip 5724 * to load new gain values. 5725 */ 5726 DPRINTF(sc, ATH_DEBUG_CALIBRATE, 5727 "%s: rfgain change\n", __func__); 5728 sc->sc_stats.ast_per_rfgain++; 5729 ath_reset(ifp); 5730 } 5731 /* 5732 * If this long cal is after an idle period, then 5733 * reset the data collection state so we start fresh. 5734 */ 5735 if (sc->sc_resetcal) { 5736 (void) ath_hal_calreset(ah, sc->sc_curchan); 5737 sc->sc_lastcalreset = ticks; 5738 sc->sc_resetcal = 0; 5739 } 5740 } 5741 if (ath_hal_calibrateN(ah, sc->sc_curchan, longCal, &isCalDone)) { 5742 if (longCal) { 5743 /* 5744 * Calibrate noise floor data again in case of change. 5745 */ 5746 ath_hal_process_noisefloor(ah); 5747 } 5748 } else { 5749 DPRINTF(sc, ATH_DEBUG_ANY, 5750 "%s: calibration of channel %u failed\n", 5751 __func__, sc->sc_curchan->ic_freq); 5752 sc->sc_stats.ast_per_calfail++; 5753 } 5754 if (!isCalDone) { 5755 restart: 5756 /* 5757 * Use a shorter interval to potentially collect multiple 5758 * data samples required to complete calibration. Once 5759 * we're told the work is done we drop back to a longer 5760 * interval between requests. We're more aggressive doing 5761 * work when operating as an AP to improve operation right 5762 * after startup. 5763 */ 5764 nextcal = (1000*ath_shortcalinterval)/hz; 5765 if (sc->sc_opmode != HAL_M_HOSTAP) 5766 nextcal *= 10; 5767 } else { 5768 nextcal = ath_longcalinterval*hz; 5769 sc->sc_lastlongcal = ticks; 5770 if (sc->sc_lastcalreset == 0) 5771 sc->sc_lastcalreset = sc->sc_lastlongcal; 5772 else if (ticks - sc->sc_lastcalreset >= ath_resetcalinterval*hz) 5773 sc->sc_resetcal = 1; /* setup reset next trip */ 5774 } 5775 5776 if (nextcal != 0) { 5777 DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: next +%u (%sisCalDone)\n", 5778 __func__, nextcal, isCalDone ? "" : "!"); 5779 callout_reset(&sc->sc_cal_ch, nextcal, ath_calibrate, sc); 5780 } else { 5781 DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: calibration disabled\n", 5782 __func__); 5783 /* NB: don't rearm timer */ 5784 } 5785 } 5786 5787 static void 5788 ath_scan_start(struct ieee80211com *ic) 5789 { 5790 struct ifnet *ifp = ic->ic_ifp; 5791 struct ath_softc *sc = ifp->if_softc; 5792 struct ath_hal *ah = sc->sc_ah; 5793 u_int32_t rfilt; 5794 5795 /* XXX calibration timer? */ 5796 5797 sc->sc_scanning = 1; 5798 sc->sc_syncbeacon = 0; 5799 rfilt = ath_calcrxfilter(sc); 5800 ath_hal_setrxfilter(ah, rfilt); 5801 ath_hal_setassocid(ah, ifp->if_broadcastaddr, 0); 5802 5803 DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0\n", 5804 __func__, rfilt, ether_sprintf(ifp->if_broadcastaddr)); 5805 } 5806 5807 static void 5808 ath_scan_end(struct ieee80211com *ic) 5809 { 5810 struct ifnet *ifp = ic->ic_ifp; 5811 struct ath_softc *sc = ifp->if_softc; 5812 struct ath_hal *ah = sc->sc_ah; 5813 u_int32_t rfilt; 5814 5815 sc->sc_scanning = 0; 5816 rfilt = ath_calcrxfilter(sc); 5817 ath_hal_setrxfilter(ah, rfilt); 5818 ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid); 5819 5820 ath_hal_process_noisefloor(ah); 5821 5822 DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n", 5823 __func__, rfilt, ether_sprintf(sc->sc_curbssid), 5824 sc->sc_curaid); 5825 } 5826 5827 static void 5828 ath_set_channel(struct ieee80211com *ic) 5829 { 5830 struct ifnet *ifp = ic->ic_ifp; 5831 struct ath_softc *sc = ifp->if_softc; 5832 5833 (void) ath_chan_set(sc, ic->ic_curchan); 5834 /* 5835 * If we are returning to our bss channel then mark state 5836 * so the next recv'd beacon's tsf will be used to sync the 5837 * beacon timers. Note that since we only hear beacons in 5838 * sta/ibss mode this has no effect in other operating modes. 5839 */ 5840 if (!sc->sc_scanning && ic->ic_curchan == ic->ic_bsschan) 5841 sc->sc_syncbeacon = 1; 5842 } 5843 5844 /* 5845 * Walk the vap list and check if there any vap's in RUN state. 5846 */ 5847 static int 5848 ath_isanyrunningvaps(struct ieee80211vap *this) 5849 { 5850 struct ieee80211com *ic = this->iv_ic; 5851 struct ieee80211vap *vap; 5852 5853 IEEE80211_LOCK_ASSERT(ic); 5854 5855 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 5856 if (vap != this && vap->iv_state == IEEE80211_S_RUN) 5857 return 1; 5858 } 5859 return 0; 5860 } 5861 5862 static int 5863 ath_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 5864 { 5865 struct ieee80211com *ic = vap->iv_ic; 5866 struct ath_softc *sc = ic->ic_ifp->if_softc; 5867 struct ath_vap *avp = ATH_VAP(vap); 5868 struct ath_hal *ah = sc->sc_ah; 5869 struct ieee80211_node *ni = NULL; 5870 int i, error, stamode; 5871 u_int32_t rfilt; 5872 static const HAL_LED_STATE leds[] = { 5873 HAL_LED_INIT, /* IEEE80211_S_INIT */ 5874 HAL_LED_SCAN, /* IEEE80211_S_SCAN */ 5875 HAL_LED_AUTH, /* IEEE80211_S_AUTH */ 5876 HAL_LED_ASSOC, /* IEEE80211_S_ASSOC */ 5877 HAL_LED_RUN, /* IEEE80211_S_CAC */ 5878 HAL_LED_RUN, /* IEEE80211_S_RUN */ 5879 HAL_LED_RUN, /* IEEE80211_S_CSA */ 5880 HAL_LED_RUN, /* IEEE80211_S_SLEEP */ 5881 }; 5882 5883 DPRINTF(sc, ATH_DEBUG_STATE, "%s: %s -> %s\n", __func__, 5884 ieee80211_state_name[vap->iv_state], 5885 ieee80211_state_name[nstate]); 5886 5887 callout_drain(&sc->sc_cal_ch); 5888 ath_hal_setledstate(ah, leds[nstate]); /* set LED */ 5889 5890 if (nstate == IEEE80211_S_SCAN) { 5891 /* 5892 * Scanning: turn off beacon miss and don't beacon. 5893 * Mark beacon state so when we reach RUN state we'll 5894 * [re]setup beacons. Unblock the task q thread so 5895 * deferred interrupt processing is done. 5896 */ 5897 ath_hal_intrset(ah, 5898 sc->sc_imask &~ (HAL_INT_SWBA | HAL_INT_BMISS)); 5899 sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS); 5900 sc->sc_beacons = 0; 5901 taskqueue_unblock(sc->sc_tq); 5902 } 5903 5904 ni = vap->iv_bss; 5905 rfilt = ath_calcrxfilter(sc); 5906 stamode = (vap->iv_opmode == IEEE80211_M_STA || 5907 vap->iv_opmode == IEEE80211_M_AHDEMO || 5908 vap->iv_opmode == IEEE80211_M_IBSS); 5909 if (stamode && nstate == IEEE80211_S_RUN) { 5910 sc->sc_curaid = ni->ni_associd; 5911 IEEE80211_ADDR_COPY(sc->sc_curbssid, ni->ni_bssid); 5912 ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid); 5913 } 5914 DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n", 5915 __func__, rfilt, ether_sprintf(sc->sc_curbssid), sc->sc_curaid); 5916 ath_hal_setrxfilter(ah, rfilt); 5917 5918 /* XXX is this to restore keycache on resume? */ 5919 if (vap->iv_opmode != IEEE80211_M_STA && 5920 (vap->iv_flags & IEEE80211_F_PRIVACY)) { 5921 for (i = 0; i < IEEE80211_WEP_NKID; i++) 5922 if (ath_hal_keyisvalid(ah, i)) 5923 ath_hal_keysetmac(ah, i, ni->ni_bssid); 5924 } 5925 5926 /* 5927 * Invoke the parent method to do net80211 work. 5928 */ 5929 error = avp->av_newstate(vap, nstate, arg); 5930 if (error != 0) 5931 goto bad; 5932 5933 if (nstate == IEEE80211_S_RUN) { 5934 /* NB: collect bss node again, it may have changed */ 5935 ni = vap->iv_bss; 5936 5937 DPRINTF(sc, ATH_DEBUG_STATE, 5938 "%s(RUN): iv_flags 0x%08x bintvl %d bssid %s " 5939 "capinfo 0x%04x chan %d\n", __func__, 5940 vap->iv_flags, ni->ni_intval, ether_sprintf(ni->ni_bssid), 5941 ni->ni_capinfo, ieee80211_chan2ieee(ic, ic->ic_curchan)); 5942 5943 switch (vap->iv_opmode) { 5944 #ifdef ATH_SUPPORT_TDMA 5945 case IEEE80211_M_AHDEMO: 5946 if ((vap->iv_caps & IEEE80211_C_TDMA) == 0) 5947 break; 5948 /* fall thru... */ 5949 #endif 5950 case IEEE80211_M_HOSTAP: 5951 case IEEE80211_M_IBSS: 5952 /* 5953 * Allocate and setup the beacon frame. 5954 * 5955 * Stop any previous beacon DMA. This may be 5956 * necessary, for example, when an ibss merge 5957 * causes reconfiguration; there will be a state 5958 * transition from RUN->RUN that means we may 5959 * be called with beacon transmission active. 5960 */ 5961 ath_hal_stoptxdma(ah, sc->sc_bhalq); 5962 5963 error = ath_beacon_alloc(sc, ni); 5964 if (error != 0) 5965 goto bad; 5966 /* 5967 * If joining an adhoc network defer beacon timer 5968 * configuration to the next beacon frame so we 5969 * have a current TSF to use. Otherwise we're 5970 * starting an ibss/bss so there's no need to delay; 5971 * if this is the first vap moving to RUN state, then 5972 * beacon state needs to be [re]configured. 5973 */ 5974 if (vap->iv_opmode == IEEE80211_M_IBSS && 5975 ni->ni_tstamp.tsf != 0) { 5976 sc->sc_syncbeacon = 1; 5977 } else if (!sc->sc_beacons) { 5978 #ifdef ATH_SUPPORT_TDMA 5979 if (vap->iv_caps & IEEE80211_C_TDMA) 5980 ath_tdma_config(sc, vap); 5981 else 5982 #endif 5983 ath_beacon_config(sc, vap); 5984 sc->sc_beacons = 1; 5985 } 5986 break; 5987 case IEEE80211_M_STA: 5988 /* 5989 * Defer beacon timer configuration to the next 5990 * beacon frame so we have a current TSF to use 5991 * (any TSF collected when scanning is likely old). 5992 */ 5993 sc->sc_syncbeacon = 1; 5994 break; 5995 case IEEE80211_M_MONITOR: 5996 /* 5997 * Monitor mode vaps have only INIT->RUN and RUN->RUN 5998 * transitions so we must re-enable interrupts here to 5999 * handle the case of a single monitor mode vap. 6000 */ 6001 ath_hal_intrset(ah, sc->sc_imask); 6002 break; 6003 case IEEE80211_M_WDS: 6004 break; 6005 default: 6006 break; 6007 } 6008 /* 6009 * Let the hal process statistics collected during a 6010 * scan so it can provide calibrated noise floor data. 6011 */ 6012 ath_hal_process_noisefloor(ah); 6013 /* 6014 * Reset rssi stats; maybe not the best place... 6015 */ 6016 sc->sc_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER; 6017 sc->sc_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER; 6018 sc->sc_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER; 6019 /* 6020 * Finally, start any timers and the task q thread 6021 * (in case we didn't go through SCAN state). 6022 */ 6023 if (ath_longcalinterval != 0) { 6024 /* start periodic recalibration timer */ 6025 callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc); 6026 } else { 6027 DPRINTF(sc, ATH_DEBUG_CALIBRATE, 6028 "%s: calibration disabled\n", __func__); 6029 } 6030 taskqueue_unblock(sc->sc_tq); 6031 } else if (nstate == IEEE80211_S_INIT) { 6032 /* 6033 * If there are no vaps left in RUN state then 6034 * shutdown host/driver operation: 6035 * o disable interrupts 6036 * o disable the task queue thread 6037 * o mark beacon processing as stopped 6038 */ 6039 if (!ath_isanyrunningvaps(vap)) { 6040 sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS); 6041 /* disable interrupts */ 6042 ath_hal_intrset(ah, sc->sc_imask &~ HAL_INT_GLOBAL); 6043 taskqueue_block(sc->sc_tq); 6044 sc->sc_beacons = 0; 6045 } 6046 #ifdef ATH_SUPPORT_TDMA 6047 ath_hal_setcca(ah, AH_TRUE); 6048 #endif 6049 } 6050 bad: 6051 return error; 6052 } 6053 6054 /* 6055 * Allocate a key cache slot to the station so we can 6056 * setup a mapping from key index to node. The key cache 6057 * slot is needed for managing antenna state and for 6058 * compression when stations do not use crypto. We do 6059 * it uniliaterally here; if crypto is employed this slot 6060 * will be reassigned. 6061 */ 6062 static void 6063 ath_setup_stationkey(struct ieee80211_node *ni) 6064 { 6065 struct ieee80211vap *vap = ni->ni_vap; 6066 struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc; 6067 ieee80211_keyix keyix, rxkeyix; 6068 6069 if (!ath_key_alloc(vap, &ni->ni_ucastkey, &keyix, &rxkeyix)) { 6070 /* 6071 * Key cache is full; we'll fall back to doing 6072 * the more expensive lookup in software. Note 6073 * this also means no h/w compression. 6074 */ 6075 /* XXX msg+statistic */ 6076 } else { 6077 /* XXX locking? */ 6078 ni->ni_ucastkey.wk_keyix = keyix; 6079 ni->ni_ucastkey.wk_rxkeyix = rxkeyix; 6080 /* NB: must mark device key to get called back on delete */ 6081 ni->ni_ucastkey.wk_flags |= IEEE80211_KEY_DEVKEY; 6082 IEEE80211_ADDR_COPY(ni->ni_ucastkey.wk_macaddr, ni->ni_macaddr); 6083 /* NB: this will create a pass-thru key entry */ 6084 ath_keyset(sc, &ni->ni_ucastkey, vap->iv_bss); 6085 } 6086 } 6087 6088 /* 6089 * Setup driver-specific state for a newly associated node. 6090 * Note that we're called also on a re-associate, the isnew 6091 * param tells us if this is the first time or not. 6092 */ 6093 static void 6094 ath_newassoc(struct ieee80211_node *ni, int isnew) 6095 { 6096 struct ath_node *an = ATH_NODE(ni); 6097 struct ieee80211vap *vap = ni->ni_vap; 6098 struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc; 6099 const struct ieee80211_txparam *tp = ni->ni_txparms; 6100 6101 an->an_mcastrix = ath_tx_findrix(sc->sc_currates, tp->mcastrate); 6102 an->an_mgmtrix = ath_tx_findrix(sc->sc_currates, tp->mgmtrate); 6103 6104 ath_rate_newassoc(sc, an, isnew); 6105 if (isnew && 6106 (vap->iv_flags & IEEE80211_F_PRIVACY) == 0 && sc->sc_hasclrkey && 6107 ni->ni_ucastkey.wk_keyix == IEEE80211_KEYIX_NONE) 6108 ath_setup_stationkey(ni); 6109 } 6110 6111 static int 6112 ath_setregdomain(struct ieee80211com *ic, struct ieee80211_regdomain *reg, 6113 int nchans, struct ieee80211_channel chans[]) 6114 { 6115 struct ath_softc *sc = ic->ic_ifp->if_softc; 6116 struct ath_hal *ah = sc->sc_ah; 6117 HAL_STATUS status; 6118 6119 DPRINTF(sc, ATH_DEBUG_REGDOMAIN, 6120 "%s: rd %u cc %u location %c%s\n", 6121 __func__, reg->regdomain, reg->country, reg->location, 6122 reg->ecm ? " ecm" : ""); 6123 6124 status = ath_hal_set_channels(ah, chans, nchans, 6125 reg->country, reg->regdomain); 6126 if (status != HAL_OK) { 6127 DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: failed, status %u\n", 6128 __func__, status); 6129 return EINVAL; /* XXX */ 6130 } 6131 return 0; 6132 } 6133 6134 static void 6135 ath_getradiocaps(struct ieee80211com *ic, 6136 int maxchans, int *nchans, struct ieee80211_channel chans[]) 6137 { 6138 struct ath_softc *sc = ic->ic_ifp->if_softc; 6139 struct ath_hal *ah = sc->sc_ah; 6140 6141 DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: use rd %u cc %d\n", 6142 __func__, SKU_DEBUG, CTRY_DEFAULT); 6143 6144 /* XXX check return */ 6145 (void) ath_hal_getchannels(ah, chans, maxchans, nchans, 6146 HAL_MODE_ALL, CTRY_DEFAULT, SKU_DEBUG, AH_TRUE); 6147 6148 } 6149 6150 static int 6151 ath_getchannels(struct ath_softc *sc) 6152 { 6153 struct ifnet *ifp = sc->sc_ifp; 6154 struct ieee80211com *ic = ifp->if_l2com; 6155 struct ath_hal *ah = sc->sc_ah; 6156 HAL_STATUS status; 6157 6158 /* 6159 * Collect channel set based on EEPROM contents. 6160 */ 6161 status = ath_hal_init_channels(ah, ic->ic_channels, IEEE80211_CHAN_MAX, 6162 &ic->ic_nchans, HAL_MODE_ALL, CTRY_DEFAULT, SKU_NONE, AH_TRUE); 6163 if (status != HAL_OK) { 6164 if_printf(ifp, "%s: unable to collect channel list from hal, " 6165 "status %d\n", __func__, status); 6166 return EINVAL; 6167 } 6168 (void) ath_hal_getregdomain(ah, &sc->sc_eerd); 6169 ath_hal_getcountrycode(ah, &sc->sc_eecc); /* NB: cannot fail */ 6170 /* XXX map Atheros sku's to net80211 SKU's */ 6171 /* XXX net80211 types too small */ 6172 ic->ic_regdomain.regdomain = (uint16_t) sc->sc_eerd; 6173 ic->ic_regdomain.country = (uint16_t) sc->sc_eecc; 6174 ic->ic_regdomain.isocc[0] = ' '; /* XXX don't know */ 6175 ic->ic_regdomain.isocc[1] = ' '; 6176 6177 ic->ic_regdomain.ecm = 1; 6178 ic->ic_regdomain.location = 'I'; 6179 6180 DPRINTF(sc, ATH_DEBUG_REGDOMAIN, 6181 "%s: eeprom rd %u cc %u (mapped rd %u cc %u) location %c%s\n", 6182 __func__, sc->sc_eerd, sc->sc_eecc, 6183 ic->ic_regdomain.regdomain, ic->ic_regdomain.country, 6184 ic->ic_regdomain.location, ic->ic_regdomain.ecm ? " ecm" : ""); 6185 return 0; 6186 } 6187 6188 static void 6189 ath_led_done(void *arg) 6190 { 6191 struct ath_softc *sc = arg; 6192 6193 sc->sc_blinking = 0; 6194 } 6195 6196 /* 6197 * Turn the LED off: flip the pin and then set a timer so no 6198 * update will happen for the specified duration. 6199 */ 6200 static void 6201 ath_led_off(void *arg) 6202 { 6203 struct ath_softc *sc = arg; 6204 6205 ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, !sc->sc_ledon); 6206 callout_reset(&sc->sc_ledtimer, sc->sc_ledoff, ath_led_done, sc); 6207 } 6208 6209 /* 6210 * Blink the LED according to the specified on/off times. 6211 */ 6212 static void 6213 ath_led_blink(struct ath_softc *sc, int on, int off) 6214 { 6215 DPRINTF(sc, ATH_DEBUG_LED, "%s: on %u off %u\n", __func__, on, off); 6216 ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, sc->sc_ledon); 6217 sc->sc_blinking = 1; 6218 sc->sc_ledoff = off; 6219 callout_reset(&sc->sc_ledtimer, on, ath_led_off, sc); 6220 } 6221 6222 static void 6223 ath_led_event(struct ath_softc *sc, int rix) 6224 { 6225 sc->sc_ledevent = ticks; /* time of last event */ 6226 if (sc->sc_blinking) /* don't interrupt active blink */ 6227 return; 6228 ath_led_blink(sc, sc->sc_hwmap[rix].ledon, sc->sc_hwmap[rix].ledoff); 6229 } 6230 6231 static int 6232 ath_rate_setup(struct ath_softc *sc, u_int mode) 6233 { 6234 struct ath_hal *ah = sc->sc_ah; 6235 const HAL_RATE_TABLE *rt; 6236 6237 switch (mode) { 6238 case IEEE80211_MODE_11A: 6239 rt = ath_hal_getratetable(ah, HAL_MODE_11A); 6240 break; 6241 case IEEE80211_MODE_HALF: 6242 rt = ath_hal_getratetable(ah, HAL_MODE_11A_HALF_RATE); 6243 break; 6244 case IEEE80211_MODE_QUARTER: 6245 rt = ath_hal_getratetable(ah, HAL_MODE_11A_QUARTER_RATE); 6246 break; 6247 case IEEE80211_MODE_11B: 6248 rt = ath_hal_getratetable(ah, HAL_MODE_11B); 6249 break; 6250 case IEEE80211_MODE_11G: 6251 rt = ath_hal_getratetable(ah, HAL_MODE_11G); 6252 break; 6253 case IEEE80211_MODE_TURBO_A: 6254 rt = ath_hal_getratetable(ah, HAL_MODE_108A); 6255 break; 6256 case IEEE80211_MODE_TURBO_G: 6257 rt = ath_hal_getratetable(ah, HAL_MODE_108G); 6258 break; 6259 case IEEE80211_MODE_STURBO_A: 6260 rt = ath_hal_getratetable(ah, HAL_MODE_TURBO); 6261 break; 6262 case IEEE80211_MODE_11NA: 6263 rt = ath_hal_getratetable(ah, HAL_MODE_11NA_HT20); 6264 break; 6265 case IEEE80211_MODE_11NG: 6266 rt = ath_hal_getratetable(ah, HAL_MODE_11NG_HT20); 6267 break; 6268 default: 6269 DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid mode %u\n", 6270 __func__, mode); 6271 return 0; 6272 } 6273 sc->sc_rates[mode] = rt; 6274 return (rt != NULL); 6275 } 6276 6277 static void 6278 ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode) 6279 { 6280 #define N(a) (sizeof(a)/sizeof(a[0])) 6281 /* NB: on/off times from the Atheros NDIS driver, w/ permission */ 6282 static const struct { 6283 u_int rate; /* tx/rx 802.11 rate */ 6284 u_int16_t timeOn; /* LED on time (ms) */ 6285 u_int16_t timeOff; /* LED off time (ms) */ 6286 } blinkrates[] = { 6287 { 108, 40, 10 }, 6288 { 96, 44, 11 }, 6289 { 72, 50, 13 }, 6290 { 48, 57, 14 }, 6291 { 36, 67, 16 }, 6292 { 24, 80, 20 }, 6293 { 22, 100, 25 }, 6294 { 18, 133, 34 }, 6295 { 12, 160, 40 }, 6296 { 10, 200, 50 }, 6297 { 6, 240, 58 }, 6298 { 4, 267, 66 }, 6299 { 2, 400, 100 }, 6300 { 0, 500, 130 }, 6301 /* XXX half/quarter rates */ 6302 }; 6303 const HAL_RATE_TABLE *rt; 6304 int i, j; 6305 6306 memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap)); 6307 rt = sc->sc_rates[mode]; 6308 KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode)); 6309 for (i = 0; i < rt->rateCount; i++) { 6310 uint8_t ieeerate = rt->info[i].dot11Rate & IEEE80211_RATE_VAL; 6311 if (rt->info[i].phy != IEEE80211_T_HT) 6312 sc->sc_rixmap[ieeerate] = i; 6313 else 6314 sc->sc_rixmap[ieeerate | IEEE80211_RATE_MCS] = i; 6315 } 6316 memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap)); 6317 for (i = 0; i < N(sc->sc_hwmap); i++) { 6318 if (i >= rt->rateCount) { 6319 sc->sc_hwmap[i].ledon = (500 * hz) / 1000; 6320 sc->sc_hwmap[i].ledoff = (130 * hz) / 1000; 6321 continue; 6322 } 6323 sc->sc_hwmap[i].ieeerate = 6324 rt->info[i].dot11Rate & IEEE80211_RATE_VAL; 6325 if (rt->info[i].phy == IEEE80211_T_HT) 6326 sc->sc_hwmap[i].ieeerate |= IEEE80211_RATE_MCS; 6327 sc->sc_hwmap[i].txflags = IEEE80211_RADIOTAP_F_DATAPAD; 6328 if (rt->info[i].shortPreamble || 6329 rt->info[i].phy == IEEE80211_T_OFDM) 6330 sc->sc_hwmap[i].txflags |= IEEE80211_RADIOTAP_F_SHORTPRE; 6331 /* NB: receive frames include FCS */ 6332 sc->sc_hwmap[i].rxflags = sc->sc_hwmap[i].txflags | 6333 IEEE80211_RADIOTAP_F_FCS; 6334 /* setup blink rate table to avoid per-packet lookup */ 6335 for (j = 0; j < N(blinkrates)-1; j++) 6336 if (blinkrates[j].rate == sc->sc_hwmap[i].ieeerate) 6337 break; 6338 /* NB: this uses the last entry if the rate isn't found */ 6339 /* XXX beware of overlow */ 6340 sc->sc_hwmap[i].ledon = (blinkrates[j].timeOn * hz) / 1000; 6341 sc->sc_hwmap[i].ledoff = (blinkrates[j].timeOff * hz) / 1000; 6342 } 6343 sc->sc_currates = rt; 6344 sc->sc_curmode = mode; 6345 /* 6346 * All protection frames are transmited at 2Mb/s for 6347 * 11g, otherwise at 1Mb/s. 6348 */ 6349 if (mode == IEEE80211_MODE_11G) 6350 sc->sc_protrix = ath_tx_findrix(rt, 2*2); 6351 else 6352 sc->sc_protrix = ath_tx_findrix(rt, 2*1); 6353 /* NB: caller is responsible for reseting rate control state */ 6354 #undef N 6355 } 6356 6357 #ifdef ATH_DEBUG 6358 static void 6359 ath_printrxbuf(struct ath_softc *sc, const struct ath_buf *bf, 6360 u_int ix, int done) 6361 { 6362 const struct ath_rx_status *rs = &bf->bf_status.ds_rxstat; 6363 struct ath_hal *ah = sc->sc_ah; 6364 const struct ath_desc *ds; 6365 int i; 6366 6367 for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) { 6368 printf("R[%2u] (DS.V:%p DS.P:%p) L:%08x D:%08x%s\n" 6369 " %08x %08x %08x %08x\n", 6370 ix, ds, (const struct ath_desc *)bf->bf_daddr + i, 6371 ds->ds_link, ds->ds_data, 6372 !done ? "" : (rs->rs_status == 0) ? " *" : " !", 6373 ds->ds_ctl0, ds->ds_ctl1, 6374 ds->ds_hw[0], ds->ds_hw[1]); 6375 if (ah->ah_magic == 0x20065416) { 6376 printf(" %08x %08x %08x %08x %08x %08x %08x\n", 6377 ds->ds_hw[2], ds->ds_hw[3], ds->ds_hw[4], 6378 ds->ds_hw[5], ds->ds_hw[6], ds->ds_hw[7], 6379 ds->ds_hw[8]); 6380 } 6381 } 6382 } 6383 6384 static void 6385 ath_printtxbuf(struct ath_softc *sc, const struct ath_buf *bf, 6386 u_int qnum, u_int ix, int done) 6387 { 6388 const struct ath_tx_status *ts = &bf->bf_status.ds_txstat; 6389 struct ath_hal *ah = sc->sc_ah; 6390 const struct ath_desc *ds; 6391 int i; 6392 6393 printf("Q%u[%3u]", qnum, ix); 6394 for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) { 6395 printf(" (DS.V:%p DS.P:%p) L:%08x D:%08x F:04%x%s\n" 6396 " %08x %08x %08x %08x %08x %08x\n", 6397 ds, (const struct ath_desc *)bf->bf_daddr + i, 6398 ds->ds_link, ds->ds_data, bf->bf_txflags, 6399 !done ? "" : (ts->ts_status == 0) ? " *" : " !", 6400 ds->ds_ctl0, ds->ds_ctl1, 6401 ds->ds_hw[0], ds->ds_hw[1], ds->ds_hw[2], ds->ds_hw[3]); 6402 if (ah->ah_magic == 0x20065416) { 6403 printf(" %08x %08x %08x %08x %08x %08x %08x %08x\n", 6404 ds->ds_hw[4], ds->ds_hw[5], ds->ds_hw[6], 6405 ds->ds_hw[7], ds->ds_hw[8], ds->ds_hw[9], 6406 ds->ds_hw[10],ds->ds_hw[11]); 6407 printf(" %08x %08x %08x %08x %08x %08x %08x %08x\n", 6408 ds->ds_hw[12],ds->ds_hw[13],ds->ds_hw[14], 6409 ds->ds_hw[15],ds->ds_hw[16],ds->ds_hw[17], 6410 ds->ds_hw[18], ds->ds_hw[19]); 6411 } 6412 } 6413 } 6414 #endif /* ATH_DEBUG */ 6415 6416 static void 6417 ath_watchdog(void *arg) 6418 { 6419 struct ath_softc *sc = arg; 6420 6421 if (sc->sc_wd_timer != 0 && --sc->sc_wd_timer == 0) { 6422 struct ifnet *ifp = sc->sc_ifp; 6423 uint32_t hangs; 6424 6425 if (ath_hal_gethangstate(sc->sc_ah, 0xffff, &hangs) && 6426 hangs != 0) { 6427 if_printf(ifp, "%s hang detected (0x%x)\n", 6428 hangs & 0xff ? "bb" : "mac", hangs); 6429 } else 6430 if_printf(ifp, "device timeout\n"); 6431 ath_reset(ifp); 6432 ifp->if_oerrors++; 6433 sc->sc_stats.ast_watchdog++; 6434 } 6435 callout_schedule(&sc->sc_wd_ch, hz); 6436 } 6437 6438 #ifdef ATH_DIAGAPI 6439 /* 6440 * Diagnostic interface to the HAL. This is used by various 6441 * tools to do things like retrieve register contents for 6442 * debugging. The mechanism is intentionally opaque so that 6443 * it can change frequently w/o concern for compatiblity. 6444 */ 6445 static int 6446 ath_ioctl_diag(struct ath_softc *sc, struct ath_diag *ad) 6447 { 6448 struct ath_hal *ah = sc->sc_ah; 6449 u_int id = ad->ad_id & ATH_DIAG_ID; 6450 void *indata = NULL; 6451 void *outdata = NULL; 6452 u_int32_t insize = ad->ad_in_size; 6453 u_int32_t outsize = ad->ad_out_size; 6454 int error = 0; 6455 6456 if (ad->ad_id & ATH_DIAG_IN) { 6457 /* 6458 * Copy in data. 6459 */ 6460 indata = malloc(insize, M_TEMP, M_NOWAIT); 6461 if (indata == NULL) { 6462 error = ENOMEM; 6463 goto bad; 6464 } 6465 error = copyin(ad->ad_in_data, indata, insize); 6466 if (error) 6467 goto bad; 6468 } 6469 if (ad->ad_id & ATH_DIAG_DYN) { 6470 /* 6471 * Allocate a buffer for the results (otherwise the HAL 6472 * returns a pointer to a buffer where we can read the 6473 * results). Note that we depend on the HAL leaving this 6474 * pointer for us to use below in reclaiming the buffer; 6475 * may want to be more defensive. 6476 */ 6477 outdata = malloc(outsize, M_TEMP, M_NOWAIT); 6478 if (outdata == NULL) { 6479 error = ENOMEM; 6480 goto bad; 6481 } 6482 } 6483 if (ath_hal_getdiagstate(ah, id, indata, insize, &outdata, &outsize)) { 6484 if (outsize < ad->ad_out_size) 6485 ad->ad_out_size = outsize; 6486 if (outdata != NULL) 6487 error = copyout(outdata, ad->ad_out_data, 6488 ad->ad_out_size); 6489 } else { 6490 error = EINVAL; 6491 } 6492 bad: 6493 if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL) 6494 free(indata, M_TEMP); 6495 if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL) 6496 free(outdata, M_TEMP); 6497 return error; 6498 } 6499 #endif /* ATH_DIAGAPI */ 6500 6501 static int 6502 ath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 6503 { 6504 #define IS_RUNNING(ifp) \ 6505 ((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING)) 6506 struct ath_softc *sc = ifp->if_softc; 6507 struct ieee80211com *ic = ifp->if_l2com; 6508 struct ifreq *ifr = (struct ifreq *)data; 6509 const HAL_RATE_TABLE *rt; 6510 int error = 0; 6511 6512 switch (cmd) { 6513 case SIOCSIFFLAGS: 6514 ATH_LOCK(sc); 6515 if (IS_RUNNING(ifp)) { 6516 /* 6517 * To avoid rescanning another access point, 6518 * do not call ath_init() here. Instead, 6519 * only reflect promisc mode settings. 6520 */ 6521 ath_mode_init(sc); 6522 } else if (ifp->if_flags & IFF_UP) { 6523 /* 6524 * Beware of being called during attach/detach 6525 * to reset promiscuous mode. In that case we 6526 * will still be marked UP but not RUNNING. 6527 * However trying to re-init the interface 6528 * is the wrong thing to do as we've already 6529 * torn down much of our state. There's 6530 * probably a better way to deal with this. 6531 */ 6532 if (!sc->sc_invalid) 6533 ath_init(sc); /* XXX lose error */ 6534 } else { 6535 ath_stop_locked(ifp); 6536 #ifdef notyet 6537 /* XXX must wakeup in places like ath_vap_delete */ 6538 if (!sc->sc_invalid) 6539 ath_hal_setpower(sc->sc_ah, HAL_PM_FULL_SLEEP); 6540 #endif 6541 } 6542 ATH_UNLOCK(sc); 6543 break; 6544 case SIOCGIFMEDIA: 6545 case SIOCSIFMEDIA: 6546 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd); 6547 break; 6548 case SIOCGATHSTATS: 6549 /* NB: embed these numbers to get a consistent view */ 6550 sc->sc_stats.ast_tx_packets = ifp->if_opackets; 6551 sc->sc_stats.ast_rx_packets = ifp->if_ipackets; 6552 sc->sc_stats.ast_tx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgtxrssi); 6553 sc->sc_stats.ast_rx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgrssi); 6554 #ifdef ATH_SUPPORT_TDMA 6555 sc->sc_stats.ast_tdma_tsfadjp = TDMA_AVG(sc->sc_avgtsfdeltap); 6556 sc->sc_stats.ast_tdma_tsfadjm = TDMA_AVG(sc->sc_avgtsfdeltam); 6557 #endif 6558 rt = sc->sc_currates; 6559 /* XXX HT rates */ 6560 sc->sc_stats.ast_tx_rate = 6561 rt->info[sc->sc_txrix].dot11Rate &~ IEEE80211_RATE_BASIC; 6562 return copyout(&sc->sc_stats, 6563 ifr->ifr_data, sizeof (sc->sc_stats)); 6564 case SIOCZATHSTATS: 6565 error = priv_check(curthread, PRIV_DRIVER); 6566 if (error == 0) 6567 memset(&sc->sc_stats, 0, sizeof(sc->sc_stats)); 6568 break; 6569 #ifdef ATH_DIAGAPI 6570 case SIOCGATHDIAG: 6571 error = ath_ioctl_diag(sc, (struct ath_diag *) ifr); 6572 break; 6573 #endif 6574 case SIOCGIFADDR: 6575 error = ether_ioctl(ifp, cmd, data); 6576 break; 6577 default: 6578 error = EINVAL; 6579 break; 6580 } 6581 return error; 6582 #undef IS_RUNNING 6583 } 6584 6585 static int 6586 ath_sysctl_slottime(SYSCTL_HANDLER_ARGS) 6587 { 6588 struct ath_softc *sc = arg1; 6589 u_int slottime = ath_hal_getslottime(sc->sc_ah); 6590 int error; 6591 6592 error = sysctl_handle_int(oidp, &slottime, 0, req); 6593 if (error || !req->newptr) 6594 return error; 6595 return !ath_hal_setslottime(sc->sc_ah, slottime) ? EINVAL : 0; 6596 } 6597 6598 static int 6599 ath_sysctl_acktimeout(SYSCTL_HANDLER_ARGS) 6600 { 6601 struct ath_softc *sc = arg1; 6602 u_int acktimeout = ath_hal_getacktimeout(sc->sc_ah); 6603 int error; 6604 6605 error = sysctl_handle_int(oidp, &acktimeout, 0, req); 6606 if (error || !req->newptr) 6607 return error; 6608 return !ath_hal_setacktimeout(sc->sc_ah, acktimeout) ? EINVAL : 0; 6609 } 6610 6611 static int 6612 ath_sysctl_ctstimeout(SYSCTL_HANDLER_ARGS) 6613 { 6614 struct ath_softc *sc = arg1; 6615 u_int ctstimeout = ath_hal_getctstimeout(sc->sc_ah); 6616 int error; 6617 6618 error = sysctl_handle_int(oidp, &ctstimeout, 0, req); 6619 if (error || !req->newptr) 6620 return error; 6621 return !ath_hal_setctstimeout(sc->sc_ah, ctstimeout) ? EINVAL : 0; 6622 } 6623 6624 static int 6625 ath_sysctl_softled(SYSCTL_HANDLER_ARGS) 6626 { 6627 struct ath_softc *sc = arg1; 6628 int softled = sc->sc_softled; 6629 int error; 6630 6631 error = sysctl_handle_int(oidp, &softled, 0, req); 6632 if (error || !req->newptr) 6633 return error; 6634 softled = (softled != 0); 6635 if (softled != sc->sc_softled) { 6636 if (softled) { 6637 /* NB: handle any sc_ledpin change */ 6638 ath_hal_gpioCfgOutput(sc->sc_ah, sc->sc_ledpin, 6639 HAL_GPIO_MUX_MAC_NETWORK_LED); 6640 ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, 6641 !sc->sc_ledon); 6642 } 6643 sc->sc_softled = softled; 6644 } 6645 return 0; 6646 } 6647 6648 static int 6649 ath_sysctl_ledpin(SYSCTL_HANDLER_ARGS) 6650 { 6651 struct ath_softc *sc = arg1; 6652 int ledpin = sc->sc_ledpin; 6653 int error; 6654 6655 error = sysctl_handle_int(oidp, &ledpin, 0, req); 6656 if (error || !req->newptr) 6657 return error; 6658 if (ledpin != sc->sc_ledpin) { 6659 sc->sc_ledpin = ledpin; 6660 if (sc->sc_softled) { 6661 ath_hal_gpioCfgOutput(sc->sc_ah, sc->sc_ledpin, 6662 HAL_GPIO_MUX_MAC_NETWORK_LED); 6663 ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, 6664 !sc->sc_ledon); 6665 } 6666 } 6667 return 0; 6668 } 6669 6670 static int 6671 ath_sysctl_txantenna(SYSCTL_HANDLER_ARGS) 6672 { 6673 struct ath_softc *sc = arg1; 6674 u_int txantenna = ath_hal_getantennaswitch(sc->sc_ah); 6675 int error; 6676 6677 error = sysctl_handle_int(oidp, &txantenna, 0, req); 6678 if (!error && req->newptr) { 6679 /* XXX assumes 2 antenna ports */ 6680 if (txantenna < HAL_ANT_VARIABLE || txantenna > HAL_ANT_FIXED_B) 6681 return EINVAL; 6682 ath_hal_setantennaswitch(sc->sc_ah, txantenna); 6683 /* 6684 * NB: with the switch locked this isn't meaningful, 6685 * but set it anyway so things like radiotap get 6686 * consistent info in their data. 6687 */ 6688 sc->sc_txantenna = txantenna; 6689 } 6690 return error; 6691 } 6692 6693 static int 6694 ath_sysctl_rxantenna(SYSCTL_HANDLER_ARGS) 6695 { 6696 struct ath_softc *sc = arg1; 6697 u_int defantenna = ath_hal_getdefantenna(sc->sc_ah); 6698 int error; 6699 6700 error = sysctl_handle_int(oidp, &defantenna, 0, req); 6701 if (!error && req->newptr) 6702 ath_hal_setdefantenna(sc->sc_ah, defantenna); 6703 return error; 6704 } 6705 6706 static int 6707 ath_sysctl_diversity(SYSCTL_HANDLER_ARGS) 6708 { 6709 struct ath_softc *sc = arg1; 6710 u_int diversity = ath_hal_getdiversity(sc->sc_ah); 6711 int error; 6712 6713 error = sysctl_handle_int(oidp, &diversity, 0, req); 6714 if (error || !req->newptr) 6715 return error; 6716 if (!ath_hal_setdiversity(sc->sc_ah, diversity)) 6717 return EINVAL; 6718 sc->sc_diversity = diversity; 6719 return 0; 6720 } 6721 6722 static int 6723 ath_sysctl_diag(SYSCTL_HANDLER_ARGS) 6724 { 6725 struct ath_softc *sc = arg1; 6726 u_int32_t diag; 6727 int error; 6728 6729 if (!ath_hal_getdiag(sc->sc_ah, &diag)) 6730 return EINVAL; 6731 error = sysctl_handle_int(oidp, &diag, 0, req); 6732 if (error || !req->newptr) 6733 return error; 6734 return !ath_hal_setdiag(sc->sc_ah, diag) ? EINVAL : 0; 6735 } 6736 6737 static int 6738 ath_sysctl_tpscale(SYSCTL_HANDLER_ARGS) 6739 { 6740 struct ath_softc *sc = arg1; 6741 struct ifnet *ifp = sc->sc_ifp; 6742 u_int32_t scale; 6743 int error; 6744 6745 (void) ath_hal_gettpscale(sc->sc_ah, &scale); 6746 error = sysctl_handle_int(oidp, &scale, 0, req); 6747 if (error || !req->newptr) 6748 return error; 6749 return !ath_hal_settpscale(sc->sc_ah, scale) ? EINVAL : 6750 (ifp->if_drv_flags & IFF_DRV_RUNNING) ? ath_reset(ifp) : 0; 6751 } 6752 6753 static int 6754 ath_sysctl_tpc(SYSCTL_HANDLER_ARGS) 6755 { 6756 struct ath_softc *sc = arg1; 6757 u_int tpc = ath_hal_gettpc(sc->sc_ah); 6758 int error; 6759 6760 error = sysctl_handle_int(oidp, &tpc, 0, req); 6761 if (error || !req->newptr) 6762 return error; 6763 return !ath_hal_settpc(sc->sc_ah, tpc) ? EINVAL : 0; 6764 } 6765 6766 static int 6767 ath_sysctl_rfkill(SYSCTL_HANDLER_ARGS) 6768 { 6769 struct ath_softc *sc = arg1; 6770 struct ifnet *ifp = sc->sc_ifp; 6771 struct ath_hal *ah = sc->sc_ah; 6772 u_int rfkill = ath_hal_getrfkill(ah); 6773 int error; 6774 6775 error = sysctl_handle_int(oidp, &rfkill, 0, req); 6776 if (error || !req->newptr) 6777 return error; 6778 if (rfkill == ath_hal_getrfkill(ah)) /* unchanged */ 6779 return 0; 6780 if (!ath_hal_setrfkill(ah, rfkill)) 6781 return EINVAL; 6782 return (ifp->if_drv_flags & IFF_DRV_RUNNING) ? ath_reset(ifp) : 0; 6783 } 6784 6785 static int 6786 ath_sysctl_rfsilent(SYSCTL_HANDLER_ARGS) 6787 { 6788 struct ath_softc *sc = arg1; 6789 u_int rfsilent; 6790 int error; 6791 6792 (void) ath_hal_getrfsilent(sc->sc_ah, &rfsilent); 6793 error = sysctl_handle_int(oidp, &rfsilent, 0, req); 6794 if (error || !req->newptr) 6795 return error; 6796 if (!ath_hal_setrfsilent(sc->sc_ah, rfsilent)) 6797 return EINVAL; 6798 sc->sc_rfsilentpin = rfsilent & 0x1c; 6799 sc->sc_rfsilentpol = (rfsilent & 0x2) != 0; 6800 return 0; 6801 } 6802 6803 static int 6804 ath_sysctl_tpack(SYSCTL_HANDLER_ARGS) 6805 { 6806 struct ath_softc *sc = arg1; 6807 u_int32_t tpack; 6808 int error; 6809 6810 (void) ath_hal_gettpack(sc->sc_ah, &tpack); 6811 error = sysctl_handle_int(oidp, &tpack, 0, req); 6812 if (error || !req->newptr) 6813 return error; 6814 return !ath_hal_settpack(sc->sc_ah, tpack) ? EINVAL : 0; 6815 } 6816 6817 static int 6818 ath_sysctl_tpcts(SYSCTL_HANDLER_ARGS) 6819 { 6820 struct ath_softc *sc = arg1; 6821 u_int32_t tpcts; 6822 int error; 6823 6824 (void) ath_hal_gettpcts(sc->sc_ah, &tpcts); 6825 error = sysctl_handle_int(oidp, &tpcts, 0, req); 6826 if (error || !req->newptr) 6827 return error; 6828 return !ath_hal_settpcts(sc->sc_ah, tpcts) ? EINVAL : 0; 6829 } 6830 6831 static int 6832 ath_sysctl_intmit(SYSCTL_HANDLER_ARGS) 6833 { 6834 struct ath_softc *sc = arg1; 6835 int intmit, error; 6836 6837 intmit = ath_hal_getintmit(sc->sc_ah); 6838 error = sysctl_handle_int(oidp, &intmit, 0, req); 6839 if (error || !req->newptr) 6840 return error; 6841 return !ath_hal_setintmit(sc->sc_ah, intmit) ? EINVAL : 0; 6842 } 6843 6844 #ifdef ATH_SUPPORT_TDMA 6845 static int 6846 ath_sysctl_setcca(SYSCTL_HANDLER_ARGS) 6847 { 6848 struct ath_softc *sc = arg1; 6849 int setcca, error; 6850 6851 setcca = sc->sc_setcca; 6852 error = sysctl_handle_int(oidp, &setcca, 0, req); 6853 if (error || !req->newptr) 6854 return error; 6855 sc->sc_setcca = (setcca != 0); 6856 return 0; 6857 } 6858 #endif /* ATH_SUPPORT_TDMA */ 6859 6860 static void 6861 ath_sysctlattach(struct ath_softc *sc) 6862 { 6863 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev); 6864 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev); 6865 struct ath_hal *ah = sc->sc_ah; 6866 6867 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6868 "countrycode", CTLFLAG_RD, &sc->sc_eecc, 0, 6869 "EEPROM country code"); 6870 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6871 "regdomain", CTLFLAG_RD, &sc->sc_eerd, 0, 6872 "EEPROM regdomain code"); 6873 #ifdef ATH_DEBUG 6874 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6875 "debug", CTLFLAG_RW, &sc->sc_debug, 0, 6876 "control debugging printfs"); 6877 #endif 6878 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6879 "slottime", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 6880 ath_sysctl_slottime, "I", "802.11 slot time (us)"); 6881 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6882 "acktimeout", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 6883 ath_sysctl_acktimeout, "I", "802.11 ACK timeout (us)"); 6884 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6885 "ctstimeout", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 6886 ath_sysctl_ctstimeout, "I", "802.11 CTS timeout (us)"); 6887 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6888 "softled", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 6889 ath_sysctl_softled, "I", "enable/disable software LED support"); 6890 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6891 "ledpin", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 6892 ath_sysctl_ledpin, "I", "GPIO pin connected to LED"); 6893 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6894 "ledon", CTLFLAG_RW, &sc->sc_ledon, 0, 6895 "setting to turn LED on"); 6896 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6897 "ledidle", CTLFLAG_RW, &sc->sc_ledidle, 0, 6898 "idle time for inactivity LED (ticks)"); 6899 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6900 "txantenna", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 6901 ath_sysctl_txantenna, "I", "antenna switch"); 6902 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6903 "rxantenna", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 6904 ath_sysctl_rxantenna, "I", "default/rx antenna"); 6905 if (ath_hal_hasdiversity(ah)) 6906 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6907 "diversity", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 6908 ath_sysctl_diversity, "I", "antenna diversity"); 6909 sc->sc_txintrperiod = ATH_TXINTR_PERIOD; 6910 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6911 "txintrperiod", CTLFLAG_RW, &sc->sc_txintrperiod, 0, 6912 "tx descriptor batching"); 6913 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6914 "diag", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 6915 ath_sysctl_diag, "I", "h/w diagnostic control"); 6916 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6917 "tpscale", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 6918 ath_sysctl_tpscale, "I", "tx power scaling"); 6919 if (ath_hal_hastpc(ah)) { 6920 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6921 "tpc", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 6922 ath_sysctl_tpc, "I", "enable/disable per-packet TPC"); 6923 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6924 "tpack", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 6925 ath_sysctl_tpack, "I", "tx power for ack frames"); 6926 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6927 "tpcts", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 6928 ath_sysctl_tpcts, "I", "tx power for cts frames"); 6929 } 6930 if (ath_hal_hasfastframes(sc->sc_ah)) { 6931 sc->sc_fftxqmin = ATH_FF_TXQMIN; 6932 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6933 "fftxqmin", CTLFLAG_RW, &sc->sc_fftxqmin, 0, 6934 "min frames before fast-frame staging"); 6935 sc->sc_fftxqmax = ATH_FF_TXQMAX; 6936 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6937 "fftxqmax", CTLFLAG_RW, &sc->sc_fftxqmax, 0, 6938 "max queued frames before tail drop"); 6939 } 6940 if (ath_hal_hasrfsilent(ah)) { 6941 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6942 "rfsilent", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 6943 ath_sysctl_rfsilent, "I", "h/w RF silent config"); 6944 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6945 "rfkill", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 6946 ath_sysctl_rfkill, "I", "enable/disable RF kill switch"); 6947 } 6948 if (ath_hal_hasintmit(ah)) { 6949 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6950 "intmit", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 6951 ath_sysctl_intmit, "I", "interference mitigation"); 6952 } 6953 sc->sc_monpass = HAL_RXERR_DECRYPT | HAL_RXERR_MIC; 6954 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6955 "monpass", CTLFLAG_RW, &sc->sc_monpass, 0, 6956 "mask of error frames to pass when monitoring"); 6957 #ifdef ATH_SUPPORT_TDMA 6958 if (ath_hal_macversion(ah) > 0x78) { 6959 sc->sc_tdmadbaprep = 2; 6960 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6961 "dbaprep", CTLFLAG_RW, &sc->sc_tdmadbaprep, 0, 6962 "TDMA DBA preparation time"); 6963 sc->sc_tdmaswbaprep = 10; 6964 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6965 "swbaprep", CTLFLAG_RW, &sc->sc_tdmaswbaprep, 0, 6966 "TDMA SWBA preparation time"); 6967 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6968 "guardtime", CTLFLAG_RW, &sc->sc_tdmaguard, 0, 6969 "TDMA slot guard time"); 6970 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6971 "superframe", CTLFLAG_RD, &sc->sc_tdmabintval, 0, 6972 "TDMA calculated super frame"); 6973 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 6974 "setcca", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 6975 ath_sysctl_setcca, "I", "enable CCA control"); 6976 } 6977 #endif 6978 } 6979 6980 static void 6981 ath_bpfattach(struct ath_softc *sc) 6982 { 6983 struct ifnet *ifp = sc->sc_ifp; 6984 6985 bpfattach(ifp, DLT_IEEE802_11_RADIO, 6986 sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th)); 6987 /* 6988 * Initialize constant fields. 6989 * XXX make header lengths a multiple of 32-bits so subsequent 6990 * headers are properly aligned; this is a kludge to keep 6991 * certain applications happy. 6992 * 6993 * NB: the channel is setup each time we transition to the 6994 * RUN state to avoid filling it in for each frame. 6995 */ 6996 sc->sc_tx_th_len = roundup(sizeof(sc->sc_tx_th), sizeof(u_int32_t)); 6997 sc->sc_tx_th.wt_ihdr.it_len = htole16(sc->sc_tx_th_len); 6998 sc->sc_tx_th.wt_ihdr.it_present = htole32(ATH_TX_RADIOTAP_PRESENT); 6999 7000 sc->sc_rx_th_len = roundup(sizeof(sc->sc_rx_th), sizeof(u_int32_t)); 7001 sc->sc_rx_th.wr_ihdr.it_len = htole16(sc->sc_rx_th_len); 7002 sc->sc_rx_th.wr_ihdr.it_present = htole32(ATH_RX_RADIOTAP_PRESENT); 7003 } 7004 7005 static int 7006 ath_tx_raw_start(struct ath_softc *sc, struct ieee80211_node *ni, 7007 struct ath_buf *bf, struct mbuf *m0, 7008 const struct ieee80211_bpf_params *params) 7009 { 7010 struct ifnet *ifp = sc->sc_ifp; 7011 struct ieee80211com *ic = ifp->if_l2com; 7012 struct ath_hal *ah = sc->sc_ah; 7013 int error, ismcast, ismrr; 7014 int keyix, hdrlen, pktlen, try0, txantenna; 7015 u_int8_t rix, cix, txrate, ctsrate, rate1, rate2, rate3; 7016 struct ieee80211_frame *wh; 7017 u_int flags, ctsduration; 7018 HAL_PKT_TYPE atype; 7019 const HAL_RATE_TABLE *rt; 7020 struct ath_desc *ds; 7021 u_int pri; 7022 7023 wh = mtod(m0, struct ieee80211_frame *); 7024 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1); 7025 hdrlen = ieee80211_anyhdrsize(wh); 7026 /* 7027 * Packet length must not include any 7028 * pad bytes; deduct them here. 7029 */ 7030 /* XXX honor IEEE80211_BPF_DATAPAD */ 7031 pktlen = m0->m_pkthdr.len - (hdrlen & 3) + IEEE80211_CRC_LEN; 7032 7033 if (params->ibp_flags & IEEE80211_BPF_CRYPTO) { 7034 const struct ieee80211_cipher *cip; 7035 struct ieee80211_key *k; 7036 7037 /* 7038 * Construct the 802.11 header+trailer for an encrypted 7039 * frame. The only reason this can fail is because of an 7040 * unknown or unsupported cipher/key type. 7041 */ 7042 k = ieee80211_crypto_encap(ni, m0); 7043 if (k == NULL) { 7044 /* 7045 * This can happen when the key is yanked after the 7046 * frame was queued. Just discard the frame; the 7047 * 802.11 layer counts failures and provides 7048 * debugging/diagnostics. 7049 */ 7050 ath_freetx(m0); 7051 return EIO; 7052 } 7053 /* 7054 * Adjust the packet + header lengths for the crypto 7055 * additions and calculate the h/w key index. When 7056 * a s/w mic is done the frame will have had any mic 7057 * added to it prior to entry so m0->m_pkthdr.len will 7058 * account for it. Otherwise we need to add it to the 7059 * packet length. 7060 */ 7061 cip = k->wk_cipher; 7062 hdrlen += cip->ic_header; 7063 pktlen += cip->ic_header + cip->ic_trailer; 7064 /* NB: frags always have any TKIP MIC done in s/w */ 7065 if ((k->wk_flags & IEEE80211_KEY_SWMIC) == 0) 7066 pktlen += cip->ic_miclen; 7067 keyix = k->wk_keyix; 7068 7069 /* packet header may have moved, reset our local pointer */ 7070 wh = mtod(m0, struct ieee80211_frame *); 7071 } else if (ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) { 7072 /* 7073 * Use station key cache slot, if assigned. 7074 */ 7075 keyix = ni->ni_ucastkey.wk_keyix; 7076 if (keyix == IEEE80211_KEYIX_NONE) 7077 keyix = HAL_TXKEYIX_INVALID; 7078 } else 7079 keyix = HAL_TXKEYIX_INVALID; 7080 7081 error = ath_tx_dmasetup(sc, bf, m0); 7082 if (error != 0) 7083 return error; 7084 m0 = bf->bf_m; /* NB: may have changed */ 7085 wh = mtod(m0, struct ieee80211_frame *); 7086 bf->bf_node = ni; /* NB: held reference */ 7087 7088 flags = HAL_TXDESC_CLRDMASK; /* XXX needed for crypto errs */ 7089 flags |= HAL_TXDESC_INTREQ; /* force interrupt */ 7090 if (params->ibp_flags & IEEE80211_BPF_RTS) 7091 flags |= HAL_TXDESC_RTSENA; 7092 else if (params->ibp_flags & IEEE80211_BPF_CTS) 7093 flags |= HAL_TXDESC_CTSENA; 7094 /* XXX leave ismcast to injector? */ 7095 if ((params->ibp_flags & IEEE80211_BPF_NOACK) || ismcast) 7096 flags |= HAL_TXDESC_NOACK; 7097 7098 rt = sc->sc_currates; 7099 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode)); 7100 rix = ath_tx_findrix(rt, params->ibp_rate0); 7101 txrate = rt->info[rix].rateCode; 7102 if (params->ibp_flags & IEEE80211_BPF_SHORTPRE) 7103 txrate |= rt->info[rix].shortPreamble; 7104 sc->sc_txrix = rix; 7105 try0 = params->ibp_try0; 7106 ismrr = (params->ibp_try1 != 0); 7107 txantenna = params->ibp_pri >> 2; 7108 if (txantenna == 0) /* XXX? */ 7109 txantenna = sc->sc_txantenna; 7110 ctsduration = 0; 7111 if (flags & (HAL_TXDESC_CTSENA | HAL_TXDESC_RTSENA)) { 7112 cix = ath_tx_findrix(rt, params->ibp_ctsrate); 7113 ctsrate = rt->info[cix].rateCode; 7114 if (params->ibp_flags & IEEE80211_BPF_SHORTPRE) { 7115 ctsrate |= rt->info[cix].shortPreamble; 7116 if (flags & HAL_TXDESC_RTSENA) /* SIFS + CTS */ 7117 ctsduration += rt->info[cix].spAckDuration; 7118 ctsduration += ath_hal_computetxtime(ah, 7119 rt, pktlen, rix, AH_TRUE); 7120 if ((flags & HAL_TXDESC_NOACK) == 0) /* SIFS + ACK */ 7121 ctsduration += rt->info[rix].spAckDuration; 7122 } else { 7123 if (flags & HAL_TXDESC_RTSENA) /* SIFS + CTS */ 7124 ctsduration += rt->info[cix].lpAckDuration; 7125 ctsduration += ath_hal_computetxtime(ah, 7126 rt, pktlen, rix, AH_FALSE); 7127 if ((flags & HAL_TXDESC_NOACK) == 0) /* SIFS + ACK */ 7128 ctsduration += rt->info[rix].lpAckDuration; 7129 } 7130 ismrr = 0; /* XXX */ 7131 } else 7132 ctsrate = 0; 7133 pri = params->ibp_pri & 3; 7134 /* 7135 * NB: we mark all packets as type PSPOLL so the h/w won't 7136 * set the sequence number, duration, etc. 7137 */ 7138 atype = HAL_PKT_TYPE_PSPOLL; 7139 7140 if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT)) 7141 ieee80211_dump_pkt(ic, mtod(m0, caddr_t), m0->m_len, 7142 sc->sc_hwmap[rix].ieeerate, -1); 7143 7144 if (bpf_peers_present(ifp->if_bpf)) { 7145 u_int64_t tsf = ath_hal_gettsf64(ah); 7146 7147 sc->sc_tx_th.wt_tsf = htole64(tsf); 7148 sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags; 7149 if (wh->i_fc[1] & IEEE80211_FC1_WEP) 7150 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP; 7151 sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate; 7152 sc->sc_tx_th.wt_txpower = ni->ni_txpower; 7153 sc->sc_tx_th.wt_antenna = sc->sc_txantenna; 7154 7155 bpf_mtap2(ifp->if_bpf, &sc->sc_tx_th, sc->sc_tx_th_len, m0); 7156 } 7157 7158 /* 7159 * Formulate first tx descriptor with tx controls. 7160 */ 7161 ds = bf->bf_desc; 7162 /* XXX check return value? */ 7163 ath_hal_setuptxdesc(ah, ds 7164 , pktlen /* packet length */ 7165 , hdrlen /* header length */ 7166 , atype /* Atheros packet type */ 7167 , params->ibp_power /* txpower */ 7168 , txrate, try0 /* series 0 rate/tries */ 7169 , keyix /* key cache index */ 7170 , txantenna /* antenna mode */ 7171 , flags /* flags */ 7172 , ctsrate /* rts/cts rate */ 7173 , ctsduration /* rts/cts duration */ 7174 ); 7175 bf->bf_txflags = flags; 7176 7177 if (ismrr) { 7178 rix = ath_tx_findrix(rt, params->ibp_rate1); 7179 rate1 = rt->info[rix].rateCode; 7180 if (params->ibp_flags & IEEE80211_BPF_SHORTPRE) 7181 rate1 |= rt->info[rix].shortPreamble; 7182 if (params->ibp_try2) { 7183 rix = ath_tx_findrix(rt, params->ibp_rate2); 7184 rate2 = rt->info[rix].rateCode; 7185 if (params->ibp_flags & IEEE80211_BPF_SHORTPRE) 7186 rate2 |= rt->info[rix].shortPreamble; 7187 } else 7188 rate2 = 0; 7189 if (params->ibp_try3) { 7190 rix = ath_tx_findrix(rt, params->ibp_rate3); 7191 rate3 = rt->info[rix].rateCode; 7192 if (params->ibp_flags & IEEE80211_BPF_SHORTPRE) 7193 rate3 |= rt->info[rix].shortPreamble; 7194 } else 7195 rate3 = 0; 7196 ath_hal_setupxtxdesc(ah, ds 7197 , rate1, params->ibp_try1 /* series 1 */ 7198 , rate2, params->ibp_try2 /* series 2 */ 7199 , rate3, params->ibp_try3 /* series 3 */ 7200 ); 7201 } 7202 7203 /* NB: no buffered multicast in power save support */ 7204 ath_tx_handoff(sc, sc->sc_ac2q[pri], bf); 7205 return 0; 7206 } 7207 7208 static int 7209 ath_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 7210 const struct ieee80211_bpf_params *params) 7211 { 7212 struct ieee80211com *ic = ni->ni_ic; 7213 struct ifnet *ifp = ic->ic_ifp; 7214 struct ath_softc *sc = ifp->if_softc; 7215 struct ath_buf *bf; 7216 7217 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->sc_invalid) { 7218 DPRINTF(sc, ATH_DEBUG_XMIT, "%s: discard frame, %s", __func__, 7219 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ? 7220 "!running" : "invalid"); 7221 sc->sc_stats.ast_tx_raw_fail++; 7222 ieee80211_free_node(ni); 7223 m_freem(m); 7224 return ENETDOWN; 7225 } 7226 /* 7227 * Grab a TX buffer and associated resources. 7228 */ 7229 bf = ath_getbuf(sc); 7230 if (bf == NULL) { 7231 /* NB: ath_getbuf handles stat+msg */ 7232 ieee80211_free_node(ni); 7233 m_freem(m); 7234 return ENOBUFS; 7235 } 7236 7237 ifp->if_opackets++; 7238 sc->sc_stats.ast_tx_raw++; 7239 7240 if (params == NULL) { 7241 /* 7242 * Legacy path; interpret frame contents to decide 7243 * precisely how to send the frame. 7244 */ 7245 if (ath_tx_start(sc, ni, bf, m)) 7246 goto bad; 7247 } else { 7248 /* 7249 * Caller supplied explicit parameters to use in 7250 * sending the frame. 7251 */ 7252 if (ath_tx_raw_start(sc, ni, bf, m, params)) 7253 goto bad; 7254 } 7255 sc->sc_wd_timer = 5; 7256 7257 return 0; 7258 bad: 7259 ifp->if_oerrors++; 7260 ATH_TXBUF_LOCK(sc); 7261 STAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list); 7262 ATH_TXBUF_UNLOCK(sc); 7263 ieee80211_free_node(ni); 7264 return EIO; /* XXX */ 7265 } 7266 7267 /* 7268 * Announce various information on device/driver attach. 7269 */ 7270 static void 7271 ath_announce(struct ath_softc *sc) 7272 { 7273 struct ifnet *ifp = sc->sc_ifp; 7274 struct ath_hal *ah = sc->sc_ah; 7275 7276 if_printf(ifp, "AR%s mac %d.%d RF%s phy %d.%d\n", 7277 ath_hal_mac_name(ah), ah->ah_macVersion, ah->ah_macRev, 7278 ath_hal_rf_name(ah), ah->ah_phyRev >> 4, ah->ah_phyRev & 0xf); 7279 if (bootverbose) { 7280 int i; 7281 for (i = 0; i <= WME_AC_VO; i++) { 7282 struct ath_txq *txq = sc->sc_ac2q[i]; 7283 if_printf(ifp, "Use hw queue %u for %s traffic\n", 7284 txq->axq_qnum, ieee80211_wme_acnames[i]); 7285 } 7286 if_printf(ifp, "Use hw queue %u for CAB traffic\n", 7287 sc->sc_cabq->axq_qnum); 7288 if_printf(ifp, "Use hw queue %u for beacons\n", sc->sc_bhalq); 7289 } 7290 if (ath_rxbuf != ATH_RXBUF) 7291 if_printf(ifp, "using %u rx buffers\n", ath_rxbuf); 7292 if (ath_txbuf != ATH_TXBUF) 7293 if_printf(ifp, "using %u tx buffers\n", ath_txbuf); 7294 } 7295 7296 #ifdef ATH_SUPPORT_TDMA 7297 static __inline uint32_t 7298 ath_hal_getnexttbtt(struct ath_hal *ah) 7299 { 7300 #define AR_TIMER0 0x8028 7301 return OS_REG_READ(ah, AR_TIMER0); 7302 } 7303 7304 static __inline void 7305 ath_hal_adjusttsf(struct ath_hal *ah, int32_t tsfdelta) 7306 { 7307 /* XXX handle wrap/overflow */ 7308 OS_REG_WRITE(ah, AR_TSF_L32, OS_REG_READ(ah, AR_TSF_L32) + tsfdelta); 7309 } 7310 7311 static void 7312 ath_tdma_settimers(struct ath_softc *sc, u_int32_t nexttbtt, u_int32_t bintval) 7313 { 7314 struct ath_hal *ah = sc->sc_ah; 7315 HAL_BEACON_TIMERS bt; 7316 7317 bt.bt_intval = bintval | HAL_BEACON_ENA; 7318 bt.bt_nexttbtt = nexttbtt; 7319 bt.bt_nextdba = (nexttbtt<<3) - sc->sc_tdmadbaprep; 7320 bt.bt_nextswba = (nexttbtt<<3) - sc->sc_tdmaswbaprep; 7321 bt.bt_nextatim = nexttbtt+1; 7322 ath_hal_beaconsettimers(ah, &bt); 7323 } 7324 7325 /* 7326 * Calculate the beacon interval. This is periodic in the 7327 * superframe for the bss. We assume each station is configured 7328 * identically wrt transmit rate so the guard time we calculate 7329 * above will be the same on all stations. Note we need to 7330 * factor in the xmit time because the hardware will schedule 7331 * a frame for transmit if the start of the frame is within 7332 * the burst time. When we get hardware that properly kills 7333 * frames in the PCU we can reduce/eliminate the guard time. 7334 * 7335 * Roundup to 1024 is so we have 1 TU buffer in the guard time 7336 * to deal with the granularity of the nexttbtt timer. 11n MAC's 7337 * with 1us timer granularity should allow us to reduce/eliminate 7338 * this. 7339 */ 7340 static void 7341 ath_tdma_bintvalsetup(struct ath_softc *sc, 7342 const struct ieee80211_tdma_state *tdma) 7343 { 7344 /* copy from vap state (XXX check all vaps have same value?) */ 7345 sc->sc_tdmaslotlen = tdma->tdma_slotlen; 7346 sc->sc_tdmabintcnt = tdma->tdma_bintval; 7347 7348 sc->sc_tdmabintval = roundup((sc->sc_tdmaslotlen+sc->sc_tdmaguard) * 7349 tdma->tdma_slotcnt, 1024); 7350 sc->sc_tdmabintval >>= 10; /* TSF -> TU */ 7351 if (sc->sc_tdmabintval & 1) 7352 sc->sc_tdmabintval++; 7353 7354 if (tdma->tdma_slot == 0) { 7355 /* 7356 * Only slot 0 beacons; other slots respond. 7357 */ 7358 sc->sc_imask |= HAL_INT_SWBA; 7359 sc->sc_tdmaswba = 0; /* beacon immediately */ 7360 } else { 7361 /* XXX all vaps must be slot 0 or slot !0 */ 7362 sc->sc_imask &= ~HAL_INT_SWBA; 7363 } 7364 } 7365 7366 /* 7367 * Max 802.11 overhead. This assumes no 4-address frames and 7368 * the encapsulation done by ieee80211_encap (llc). We also 7369 * include potential crypto overhead. 7370 */ 7371 #define IEEE80211_MAXOVERHEAD \ 7372 (sizeof(struct ieee80211_qosframe) \ 7373 + sizeof(struct llc) \ 7374 + IEEE80211_ADDR_LEN \ 7375 + IEEE80211_WEP_IVLEN \ 7376 + IEEE80211_WEP_KIDLEN \ 7377 + IEEE80211_WEP_CRCLEN \ 7378 + IEEE80211_WEP_MICLEN \ 7379 + IEEE80211_CRC_LEN) 7380 7381 /* 7382 * Setup initially for tdma operation. Start the beacon 7383 * timers and enable SWBA if we are slot 0. Otherwise 7384 * we wait for slot 0 to arrive so we can sync up before 7385 * starting to transmit. 7386 */ 7387 static void 7388 ath_tdma_config(struct ath_softc *sc, struct ieee80211vap *vap) 7389 { 7390 struct ath_hal *ah = sc->sc_ah; 7391 struct ifnet *ifp = sc->sc_ifp; 7392 struct ieee80211com *ic = ifp->if_l2com; 7393 const struct ieee80211_txparam *tp; 7394 const struct ieee80211_tdma_state *tdma = NULL; 7395 int rix; 7396 7397 if (vap == NULL) { 7398 vap = TAILQ_FIRST(&ic->ic_vaps); /* XXX */ 7399 if (vap == NULL) { 7400 if_printf(ifp, "%s: no vaps?\n", __func__); 7401 return; 7402 } 7403 } 7404 tp = vap->iv_bss->ni_txparms; 7405 /* 7406 * Calculate the guard time for each slot. This is the 7407 * time to send a maximal-size frame according to the 7408 * fixed/lowest transmit rate. Note that the interface 7409 * mtu does not include the 802.11 overhead so we must 7410 * tack that on (ath_hal_computetxtime includes the 7411 * preamble and plcp in it's calculation). 7412 */ 7413 tdma = vap->iv_tdma; 7414 if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) 7415 rix = ath_tx_findrix(sc->sc_currates, tp->ucastrate); 7416 else 7417 rix = ath_tx_findrix(sc->sc_currates, tp->mcastrate); 7418 /* XXX short preamble assumed */ 7419 sc->sc_tdmaguard = ath_hal_computetxtime(ah, sc->sc_currates, 7420 ifp->if_mtu + IEEE80211_MAXOVERHEAD, rix, AH_TRUE); 7421 7422 ath_hal_intrset(ah, 0); 7423 7424 ath_beaconq_config(sc); /* setup h/w beacon q */ 7425 if (sc->sc_setcca) 7426 ath_hal_setcca(ah, AH_FALSE); /* disable CCA */ 7427 ath_tdma_bintvalsetup(sc, tdma); /* calculate beacon interval */ 7428 ath_tdma_settimers(sc, sc->sc_tdmabintval, 7429 sc->sc_tdmabintval | HAL_BEACON_RESET_TSF); 7430 sc->sc_syncbeacon = 0; 7431 7432 sc->sc_avgtsfdeltap = TDMA_DUMMY_MARKER; 7433 sc->sc_avgtsfdeltam = TDMA_DUMMY_MARKER; 7434 7435 ath_hal_intrset(ah, sc->sc_imask); 7436 7437 DPRINTF(sc, ATH_DEBUG_TDMA, "%s: slot %u len %uus cnt %u " 7438 "bsched %u guard %uus bintval %u TU dba prep %u\n", __func__, 7439 tdma->tdma_slot, tdma->tdma_slotlen, tdma->tdma_slotcnt, 7440 tdma->tdma_bintval, sc->sc_tdmaguard, sc->sc_tdmabintval, 7441 sc->sc_tdmadbaprep); 7442 } 7443 7444 /* 7445 * Update tdma operation. Called from the 802.11 layer 7446 * when a beacon is received from the TDMA station operating 7447 * in the slot immediately preceding us in the bss. Use 7448 * the rx timestamp for the beacon frame to update our 7449 * beacon timers so we follow their schedule. Note that 7450 * by using the rx timestamp we implicitly include the 7451 * propagation delay in our schedule. 7452 */ 7453 static void 7454 ath_tdma_update(struct ieee80211_node *ni, 7455 const struct ieee80211_tdma_param *tdma, int changed) 7456 { 7457 #define TSF_TO_TU(_h,_l) \ 7458 ((((u_int32_t)(_h)) << 22) | (((u_int32_t)(_l)) >> 10)) 7459 #define TU_TO_TSF(_tu) (((u_int64_t)(_tu)) << 10) 7460 struct ieee80211vap *vap = ni->ni_vap; 7461 struct ieee80211com *ic = ni->ni_ic; 7462 struct ath_softc *sc = ic->ic_ifp->if_softc; 7463 struct ath_hal *ah = sc->sc_ah; 7464 const HAL_RATE_TABLE *rt = sc->sc_currates; 7465 u_int64_t tsf, rstamp, nextslot; 7466 u_int32_t txtime, nextslottu, timer0; 7467 int32_t tudelta, tsfdelta; 7468 const struct ath_rx_status *rs; 7469 int rix; 7470 7471 sc->sc_stats.ast_tdma_update++; 7472 7473 /* 7474 * Check for and adopt configuration changes. 7475 */ 7476 if (changed != 0) { 7477 const struct ieee80211_tdma_state *ts = vap->iv_tdma; 7478 7479 ath_tdma_bintvalsetup(sc, ts); 7480 7481 DPRINTF(sc, ATH_DEBUG_TDMA, 7482 "%s: adopt slot %u slotcnt %u slotlen %u us " 7483 "bintval %u TU\n", __func__, 7484 ts->tdma_slot, ts->tdma_slotcnt, ts->tdma_slotlen, 7485 sc->sc_tdmabintval); 7486 7487 ath_beaconq_config(sc); 7488 /* XXX right? */ 7489 ath_hal_intrset(ah, sc->sc_imask); 7490 /* NB: beacon timers programmed below */ 7491 } 7492 7493 /* extend rx timestamp to 64 bits */ 7494 tsf = ath_hal_gettsf64(ah); 7495 rstamp = ath_extend_tsf(ni->ni_rstamp, tsf); 7496 /* 7497 * The rx timestamp is set by the hardware on completing 7498 * reception (at the point where the rx descriptor is DMA'd 7499 * to the host). To find the start of our next slot we 7500 * must adjust this time by the time required to send 7501 * the packet just received. 7502 */ 7503 rs = sc->sc_tdmars; 7504 rix = rt->rateCodeToIndex[rs->rs_rate]; 7505 txtime = ath_hal_computetxtime(ah, rt, rs->rs_datalen, rix, 7506 rt->info[rix].shortPreamble); 7507 /* NB: << 9 is to cvt to TU and /2 */ 7508 nextslot = (rstamp - txtime) + (sc->sc_tdmabintval << 9); 7509 nextslottu = TSF_TO_TU(nextslot>>32, nextslot) & HAL_BEACON_PERIOD; 7510 7511 /* 7512 * TIMER0 is the h/w's idea of NextTBTT (in TU's). Convert 7513 * to usecs and calculate the difference between what the 7514 * other station thinks and what we have programmed. This 7515 * lets us figure how to adjust our timers to match. The 7516 * adjustments are done by pulling the TSF forward and possibly 7517 * rewriting the beacon timers. 7518 */ 7519 timer0 = ath_hal_getnexttbtt(ah); 7520 tsfdelta = (int32_t)((nextslot % TU_TO_TSF(HAL_BEACON_PERIOD+1)) - TU_TO_TSF(timer0)); 7521 7522 DPRINTF(sc, ATH_DEBUG_TDMA_TIMER, 7523 "tsfdelta %d avg +%d/-%d\n", tsfdelta, 7524 TDMA_AVG(sc->sc_avgtsfdeltap), TDMA_AVG(sc->sc_avgtsfdeltam)); 7525 7526 if (tsfdelta < 0) { 7527 TDMA_SAMPLE(sc->sc_avgtsfdeltap, 0); 7528 TDMA_SAMPLE(sc->sc_avgtsfdeltam, -tsfdelta); 7529 tsfdelta = -tsfdelta % 1024; 7530 nextslottu++; 7531 } else if (tsfdelta > 0) { 7532 TDMA_SAMPLE(sc->sc_avgtsfdeltap, tsfdelta); 7533 TDMA_SAMPLE(sc->sc_avgtsfdeltam, 0); 7534 tsfdelta = 1024 - (tsfdelta % 1024); 7535 nextslottu++; 7536 } else { 7537 TDMA_SAMPLE(sc->sc_avgtsfdeltap, 0); 7538 TDMA_SAMPLE(sc->sc_avgtsfdeltam, 0); 7539 } 7540 tudelta = nextslottu - timer0; 7541 7542 /* 7543 * Copy sender's timetstamp into tdma ie so they can 7544 * calculate roundtrip time. We submit a beacon frame 7545 * below after any timer adjustment. The frame goes out 7546 * at the next TBTT so the sender can calculate the 7547 * roundtrip by inspecting the tdma ie in our beacon frame. 7548 * 7549 * NB: This tstamp is subtlely preserved when 7550 * IEEE80211_BEACON_TDMA is marked (e.g. when the 7551 * slot position changes) because ieee80211_add_tdma 7552 * skips over the data. 7553 */ 7554 memcpy(ATH_VAP(vap)->av_boff.bo_tdma + 7555 __offsetof(struct ieee80211_tdma_param, tdma_tstamp), 7556 &ni->ni_tstamp.data, 8); 7557 #if 0 7558 DPRINTF(sc, ATH_DEBUG_TDMA_TIMER, 7559 "tsf %llu nextslot %llu (%d, %d) nextslottu %u timer0 %u (%d)\n", 7560 (unsigned long long) tsf, (unsigned long long) nextslot, 7561 (int)(nextslot - tsf), tsfdelta, 7562 nextslottu, timer0, tudelta); 7563 #endif 7564 /* 7565 * Adjust the beacon timers only when pulling them forward 7566 * or when going back by less than the beacon interval. 7567 * Negative jumps larger than the beacon interval seem to 7568 * cause the timers to stop and generally cause instability. 7569 * This basically filters out jumps due to missed beacons. 7570 */ 7571 if (tudelta != 0 && (tudelta > 0 || -tudelta < sc->sc_tdmabintval)) { 7572 ath_tdma_settimers(sc, nextslottu, sc->sc_tdmabintval); 7573 sc->sc_stats.ast_tdma_timers++; 7574 } 7575 if (tsfdelta > 0) { 7576 ath_hal_adjusttsf(ah, tsfdelta); 7577 sc->sc_stats.ast_tdma_tsf++; 7578 } 7579 ath_tdma_beacon_send(sc, vap); /* prepare response */ 7580 #undef TU_TO_TSF 7581 #undef TSF_TO_TU 7582 } 7583 7584 /* 7585 * Transmit a beacon frame at SWBA. Dynamic updates 7586 * to the frame contents are done as needed. 7587 */ 7588 static void 7589 ath_tdma_beacon_send(struct ath_softc *sc, struct ieee80211vap *vap) 7590 { 7591 struct ath_hal *ah = sc->sc_ah; 7592 struct ath_buf *bf; 7593 int otherant; 7594 7595 /* 7596 * Check if the previous beacon has gone out. If 7597 * not don't try to post another, skip this period 7598 * and wait for the next. Missed beacons indicate 7599 * a problem and should not occur. If we miss too 7600 * many consecutive beacons reset the device. 7601 */ 7602 if (ath_hal_numtxpending(ah, sc->sc_bhalq) != 0) { 7603 sc->sc_bmisscount++; 7604 DPRINTF(sc, ATH_DEBUG_BEACON, 7605 "%s: missed %u consecutive beacons\n", 7606 __func__, sc->sc_bmisscount); 7607 if (sc->sc_bmisscount >= ath_bstuck_threshold) 7608 taskqueue_enqueue(sc->sc_tq, &sc->sc_bstucktask); 7609 return; 7610 } 7611 if (sc->sc_bmisscount != 0) { 7612 DPRINTF(sc, ATH_DEBUG_BEACON, 7613 "%s: resume beacon xmit after %u misses\n", 7614 __func__, sc->sc_bmisscount); 7615 sc->sc_bmisscount = 0; 7616 } 7617 7618 /* 7619 * Check recent per-antenna transmit statistics and flip 7620 * the default antenna if noticeably more frames went out 7621 * on the non-default antenna. 7622 * XXX assumes 2 anntenae 7623 */ 7624 if (!sc->sc_diversity) { 7625 otherant = sc->sc_defant & 1 ? 2 : 1; 7626 if (sc->sc_ant_tx[otherant] > sc->sc_ant_tx[sc->sc_defant] + 2) 7627 ath_setdefantenna(sc, otherant); 7628 sc->sc_ant_tx[1] = sc->sc_ant_tx[2] = 0; 7629 } 7630 7631 bf = ath_beacon_generate(sc, vap); 7632 if (bf != NULL) { 7633 /* 7634 * Stop any current dma and put the new frame on the queue. 7635 * This should never fail since we check above that no frames 7636 * are still pending on the queue. 7637 */ 7638 if (!ath_hal_stoptxdma(ah, sc->sc_bhalq)) { 7639 DPRINTF(sc, ATH_DEBUG_ANY, 7640 "%s: beacon queue %u did not stop?\n", 7641 __func__, sc->sc_bhalq); 7642 /* NB: the HAL still stops DMA, so proceed */ 7643 } 7644 ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr); 7645 ath_hal_txstart(ah, sc->sc_bhalq); 7646 7647 sc->sc_stats.ast_be_xmit++; /* XXX per-vap? */ 7648 7649 /* 7650 * Record local TSF for our last send for use 7651 * in arbitrating slot collisions. 7652 */ 7653 vap->iv_bss->ni_tstamp.tsf = ath_hal_gettsf64(ah); 7654 } 7655 } 7656 #endif /* ATH_SUPPORT_TDMA */ 7657