1 /*- 2 * Copyright (c) 2002-2005 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 * 3. Neither the names of the above-listed copyright holders nor the names 16 * of any contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 * 19 * Alternatively, this software may be distributed under the terms of the 20 * GNU General Public License ("GPL") version 2 as published by the Free 21 * Software Foundation. 22 * 23 * NO WARRANTY 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 26 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 28 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 29 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 32 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 34 * THE POSSIBILITY OF SUCH DAMAGES. 35 */ 36 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 /* 41 * Driver for the Atheros Wireless LAN controller. 42 * 43 * This software is derived from work of Atsushi Onoe; his contribution 44 * is greatly appreciated. 45 */ 46 47 #include "opt_inet.h" 48 #include "opt_ath.h" 49 50 #include <sys/param.h> 51 #include <sys/systm.h> 52 #include <sys/sysctl.h> 53 #include <sys/mbuf.h> 54 #include <sys/malloc.h> 55 #include <sys/lock.h> 56 #include <sys/mutex.h> 57 #include <sys/kernel.h> 58 #include <sys/socket.h> 59 #include <sys/sockio.h> 60 #include <sys/errno.h> 61 #include <sys/callout.h> 62 #include <sys/bus.h> 63 #include <sys/endian.h> 64 #include <sys/kthread.h> 65 #include <sys/taskqueue.h> 66 67 #include <machine/bus.h> 68 69 #include <net/if.h> 70 #include <net/if_dl.h> 71 #include <net/if_media.h> 72 #include <net/if_types.h> 73 #include <net/if_arp.h> 74 #include <net/ethernet.h> 75 #include <net/if_llc.h> 76 77 #include <net80211/ieee80211_var.h> 78 79 #include <net/bpf.h> 80 81 #ifdef INET 82 #include <netinet/in.h> 83 #include <netinet/if_ether.h> 84 #endif 85 86 #include <dev/ath/if_athvar.h> 87 #include <contrib/dev/ath/ah_desc.h> 88 #include <contrib/dev/ath/ah_devid.h> /* XXX for softled */ 89 90 #ifdef ATH_TX99_DIAG 91 #include <dev/ath/ath_tx99/ath_tx99.h> 92 #endif 93 94 /* unaligned little endian access */ 95 #define LE_READ_2(p) \ 96 ((u_int16_t) \ 97 ((((u_int8_t *)(p))[0] ) | (((u_int8_t *)(p))[1] << 8))) 98 #define LE_READ_4(p) \ 99 ((u_int32_t) \ 100 ((((u_int8_t *)(p))[0] ) | (((u_int8_t *)(p))[1] << 8) | \ 101 (((u_int8_t *)(p))[2] << 16) | (((u_int8_t *)(p))[3] << 24))) 102 103 enum { 104 ATH_LED_TX, 105 ATH_LED_RX, 106 ATH_LED_POLL, 107 }; 108 109 static void ath_init(void *); 110 static void ath_stop_locked(struct ifnet *); 111 static void ath_stop(struct ifnet *); 112 static void ath_start(struct ifnet *); 113 static int ath_reset(struct ifnet *); 114 static int ath_media_change(struct ifnet *); 115 static void ath_watchdog(struct ifnet *); 116 static int ath_ioctl(struct ifnet *, u_long, caddr_t); 117 static void ath_fatal_proc(void *, int); 118 static void ath_rxorn_proc(void *, int); 119 static void ath_bmiss_proc(void *, int); 120 static void ath_radar_proc(void *, int); 121 static int ath_key_alloc(struct ieee80211com *, 122 const struct ieee80211_key *, 123 ieee80211_keyix *, ieee80211_keyix *); 124 static int ath_key_delete(struct ieee80211com *, 125 const struct ieee80211_key *); 126 static int ath_key_set(struct ieee80211com *, const struct ieee80211_key *, 127 const u_int8_t mac[IEEE80211_ADDR_LEN]); 128 static void ath_key_update_begin(struct ieee80211com *); 129 static void ath_key_update_end(struct ieee80211com *); 130 static void ath_mode_init(struct ath_softc *); 131 static void ath_setslottime(struct ath_softc *); 132 static void ath_updateslot(struct ifnet *); 133 static int ath_beaconq_setup(struct ath_hal *); 134 static int ath_beacon_alloc(struct ath_softc *, struct ieee80211_node *); 135 static void ath_beacon_setup(struct ath_softc *, struct ath_buf *); 136 static void ath_beacon_proc(void *, int); 137 static void ath_bstuck_proc(void *, int); 138 static void ath_beacon_free(struct ath_softc *); 139 static void ath_beacon_config(struct ath_softc *); 140 static void ath_descdma_cleanup(struct ath_softc *sc, 141 struct ath_descdma *, ath_bufhead *); 142 static int ath_desc_alloc(struct ath_softc *); 143 static void ath_desc_free(struct ath_softc *); 144 static struct ieee80211_node *ath_node_alloc(struct ieee80211_node_table *); 145 static void ath_node_free(struct ieee80211_node *); 146 static u_int8_t ath_node_getrssi(const struct ieee80211_node *); 147 static int ath_rxbuf_init(struct ath_softc *, struct ath_buf *); 148 static void ath_recv_mgmt(struct ieee80211com *ic, struct mbuf *m, 149 struct ieee80211_node *ni, 150 int subtype, int rssi, u_int32_t rstamp); 151 static void ath_setdefantenna(struct ath_softc *, u_int); 152 static void ath_rx_proc(void *, int); 153 static struct ath_txq *ath_txq_setup(struct ath_softc*, int qtype, int subtype); 154 static int ath_tx_setup(struct ath_softc *, int, int); 155 static int ath_wme_update(struct ieee80211com *); 156 static void ath_tx_cleanupq(struct ath_softc *, struct ath_txq *); 157 static void ath_tx_cleanup(struct ath_softc *); 158 static int ath_tx_start(struct ath_softc *, struct ieee80211_node *, 159 struct ath_buf *, struct mbuf *); 160 static void ath_tx_proc_q0(void *, int); 161 static void ath_tx_proc_q0123(void *, int); 162 static void ath_tx_proc(void *, int); 163 static int ath_chan_set(struct ath_softc *, struct ieee80211_channel *); 164 static void ath_draintxq(struct ath_softc *); 165 static void ath_stoprecv(struct ath_softc *); 166 static int ath_startrecv(struct ath_softc *); 167 static void ath_chan_change(struct ath_softc *, struct ieee80211_channel *); 168 static void ath_next_scan(void *); 169 static void ath_calibrate(void *); 170 static int ath_newstate(struct ieee80211com *, enum ieee80211_state, int); 171 static void ath_setup_stationkey(struct ieee80211_node *); 172 static void ath_newassoc(struct ieee80211_node *, int); 173 static int ath_getchannels(struct ath_softc *, u_int cc, 174 HAL_BOOL outdoor, HAL_BOOL xchanmode); 175 static void ath_led_event(struct ath_softc *, int); 176 static void ath_update_txpow(struct ath_softc *); 177 178 static int ath_rate_setup(struct ath_softc *, u_int mode); 179 static void ath_setcurmode(struct ath_softc *, enum ieee80211_phymode); 180 181 static void ath_sysctlattach(struct ath_softc *); 182 static void ath_bpfattach(struct ath_softc *); 183 static void ath_announce(struct ath_softc *); 184 185 SYSCTL_DECL(_hw_ath); 186 187 /* XXX validate sysctl values */ 188 static int ath_dwelltime = 200; /* 5 channels/second */ 189 SYSCTL_INT(_hw_ath, OID_AUTO, dwell, CTLFLAG_RW, &ath_dwelltime, 190 0, "channel dwell time (ms) for AP/station scanning"); 191 static int ath_calinterval = 30; /* calibrate every 30 secs */ 192 SYSCTL_INT(_hw_ath, OID_AUTO, calibrate, CTLFLAG_RW, &ath_calinterval, 193 0, "chip calibration interval (secs)"); 194 static int ath_outdoor = AH_TRUE; /* outdoor operation */ 195 SYSCTL_INT(_hw_ath, OID_AUTO, outdoor, CTLFLAG_RD, &ath_outdoor, 196 0, "outdoor operation"); 197 TUNABLE_INT("hw.ath.outdoor", &ath_outdoor); 198 static int ath_xchanmode = AH_TRUE; /* extended channel use */ 199 SYSCTL_INT(_hw_ath, OID_AUTO, xchanmode, CTLFLAG_RD, &ath_xchanmode, 200 0, "extended channel mode"); 201 TUNABLE_INT("hw.ath.xchanmode", &ath_xchanmode); 202 static int ath_countrycode = CTRY_DEFAULT; /* country code */ 203 SYSCTL_INT(_hw_ath, OID_AUTO, countrycode, CTLFLAG_RD, &ath_countrycode, 204 0, "country code"); 205 TUNABLE_INT("hw.ath.countrycode", &ath_countrycode); 206 static int ath_regdomain = 0; /* regulatory domain */ 207 SYSCTL_INT(_hw_ath, OID_AUTO, regdomain, CTLFLAG_RD, &ath_regdomain, 208 0, "regulatory domain"); 209 210 static int ath_rxbuf = ATH_RXBUF; /* # rx buffers to allocate */ 211 SYSCTL_INT(_hw_ath, OID_AUTO, rxbuf, CTLFLAG_RD, &ath_rxbuf, 212 0, "rx buffers allocated"); 213 TUNABLE_INT("hw.ath.rxbuf", &ath_rxbuf); 214 static int ath_txbuf = ATH_TXBUF; /* # tx buffers to allocate */ 215 SYSCTL_INT(_hw_ath, OID_AUTO, txbuf, CTLFLAG_RD, &ath_txbuf, 216 0, "tx buffers allocated"); 217 TUNABLE_INT("hw.ath.txbuf", &ath_txbuf); 218 219 #ifdef ATH_DEBUG 220 static int ath_debug = 0; 221 SYSCTL_INT(_hw_ath, OID_AUTO, debug, CTLFLAG_RW, &ath_debug, 222 0, "control debugging printfs"); 223 TUNABLE_INT("hw.ath.debug", &ath_debug); 224 enum { 225 ATH_DEBUG_XMIT = 0x00000001, /* basic xmit operation */ 226 ATH_DEBUG_XMIT_DESC = 0x00000002, /* xmit descriptors */ 227 ATH_DEBUG_RECV = 0x00000004, /* basic recv operation */ 228 ATH_DEBUG_RECV_DESC = 0x00000008, /* recv descriptors */ 229 ATH_DEBUG_RATE = 0x00000010, /* rate control */ 230 ATH_DEBUG_RESET = 0x00000020, /* reset processing */ 231 ATH_DEBUG_MODE = 0x00000040, /* mode init/setup */ 232 ATH_DEBUG_BEACON = 0x00000080, /* beacon handling */ 233 ATH_DEBUG_WATCHDOG = 0x00000100, /* watchdog timeout */ 234 ATH_DEBUG_INTR = 0x00001000, /* ISR */ 235 ATH_DEBUG_TX_PROC = 0x00002000, /* tx ISR proc */ 236 ATH_DEBUG_RX_PROC = 0x00004000, /* rx ISR proc */ 237 ATH_DEBUG_BEACON_PROC = 0x00008000, /* beacon ISR proc */ 238 ATH_DEBUG_CALIBRATE = 0x00010000, /* periodic calibration */ 239 ATH_DEBUG_KEYCACHE = 0x00020000, /* key cache management */ 240 ATH_DEBUG_STATE = 0x00040000, /* 802.11 state transitions */ 241 ATH_DEBUG_NODE = 0x00080000, /* node management */ 242 ATH_DEBUG_LED = 0x00100000, /* led management */ 243 ATH_DEBUG_FF = 0x00200000, /* fast frames */ 244 ATH_DEBUG_DFS = 0x00400000, /* DFS processing */ 245 ATH_DEBUG_FATAL = 0x80000000, /* fatal errors */ 246 ATH_DEBUG_ANY = 0xffffffff 247 }; 248 #define IFF_DUMPPKTS(sc, m) \ 249 ((sc->sc_debug & (m)) || \ 250 (sc->sc_ifp->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2)) 251 #define DPRINTF(sc, m, fmt, ...) do { \ 252 if (sc->sc_debug & (m)) \ 253 printf(fmt, __VA_ARGS__); \ 254 } while (0) 255 #define KEYPRINTF(sc, ix, hk, mac) do { \ 256 if (sc->sc_debug & ATH_DEBUG_KEYCACHE) \ 257 ath_keyprint(__func__, ix, hk, mac); \ 258 } while (0) 259 static void ath_printrxbuf(struct ath_buf *bf, u_int ix, int); 260 static void ath_printtxbuf(struct ath_buf *bf, u_int qnum, u_int ix, int done); 261 #else 262 #define IFF_DUMPPKTS(sc, m) \ 263 ((sc->sc_ifp->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2)) 264 #define DPRINTF(sc, m, fmt, ...) do { \ 265 (void) sc; \ 266 } while (0) 267 #define KEYPRINTF(sc, k, ix, mac) do { \ 268 (void) sc; \ 269 } while (0) 270 #endif 271 272 MALLOC_DEFINE(M_ATHDEV, "athdev", "ath driver dma buffers"); 273 274 int 275 ath_attach(u_int16_t devid, struct ath_softc *sc) 276 { 277 struct ifnet *ifp; 278 struct ieee80211com *ic = &sc->sc_ic; 279 struct ath_hal *ah = NULL; 280 HAL_STATUS status; 281 int error = 0, i; 282 283 DPRINTF(sc, ATH_DEBUG_ANY, "%s: devid 0x%x\n", __func__, devid); 284 285 ifp = sc->sc_ifp = if_alloc(IFT_ETHER); 286 if (ifp == NULL) { 287 device_printf(sc->sc_dev, "can not if_alloc()\n"); 288 error = ENOSPC; 289 goto bad; 290 } 291 292 /* set these up early for if_printf use */ 293 if_initname(ifp, device_get_name(sc->sc_dev), 294 device_get_unit(sc->sc_dev)); 295 296 ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh, &status); 297 if (ah == NULL) { 298 if_printf(ifp, "unable to attach hardware; HAL status %u\n", 299 status); 300 error = ENXIO; 301 goto bad; 302 } 303 if (ah->ah_abi != HAL_ABI_VERSION) { 304 if_printf(ifp, "HAL ABI mismatch detected " 305 "(HAL:0x%x != driver:0x%x)\n", 306 ah->ah_abi, HAL_ABI_VERSION); 307 error = ENXIO; 308 goto bad; 309 } 310 sc->sc_ah = ah; 311 sc->sc_invalid = 0; /* ready to go, enable interrupt handling */ 312 313 /* 314 * Check if the MAC has multi-rate retry support. 315 * We do this by trying to setup a fake extended 316 * descriptor. MAC's that don't have support will 317 * return false w/o doing anything. MAC's that do 318 * support it will return true w/o doing anything. 319 */ 320 sc->sc_mrretry = ath_hal_setupxtxdesc(ah, NULL, 0,0, 0,0, 0,0); 321 322 /* 323 * Check if the device has hardware counters for PHY 324 * errors. If so we need to enable the MIB interrupt 325 * so we can act on stat triggers. 326 */ 327 if (ath_hal_hwphycounters(ah)) 328 sc->sc_needmib = 1; 329 330 /* 331 * Get the hardware key cache size. 332 */ 333 sc->sc_keymax = ath_hal_keycachesize(ah); 334 if (sc->sc_keymax > ATH_KEYMAX) { 335 if_printf(ifp, "Warning, using only %u of %u key cache slots\n", 336 ATH_KEYMAX, sc->sc_keymax); 337 sc->sc_keymax = ATH_KEYMAX; 338 } 339 /* 340 * Reset the key cache since some parts do not 341 * reset the contents on initial power up. 342 */ 343 for (i = 0; i < sc->sc_keymax; i++) 344 ath_hal_keyreset(ah, i); 345 /* 346 * Mark key cache slots associated with global keys 347 * as in use. If we knew TKIP was not to be used we 348 * could leave the +32, +64, and +32+64 slots free. 349 * XXX only for splitmic. 350 */ 351 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 352 setbit(sc->sc_keymap, i); 353 setbit(sc->sc_keymap, i+32); 354 setbit(sc->sc_keymap, i+64); 355 setbit(sc->sc_keymap, i+32+64); 356 } 357 358 /* 359 * Collect the channel list using the default country 360 * code and including outdoor channels. The 802.11 layer 361 * is resposible for filtering this list based on settings 362 * like the phy mode. 363 */ 364 error = ath_getchannels(sc, ath_countrycode, 365 ath_outdoor, ath_xchanmode); 366 if (error != 0) 367 goto bad; 368 369 /* 370 * Setup rate tables for all potential media types. 371 */ 372 ath_rate_setup(sc, IEEE80211_MODE_11A); 373 ath_rate_setup(sc, IEEE80211_MODE_11B); 374 ath_rate_setup(sc, IEEE80211_MODE_11G); 375 ath_rate_setup(sc, IEEE80211_MODE_TURBO_A); 376 ath_rate_setup(sc, IEEE80211_MODE_TURBO_G); 377 /* NB: setup here so ath_rate_update is happy */ 378 ath_setcurmode(sc, IEEE80211_MODE_11A); 379 380 /* 381 * Allocate tx+rx descriptors and populate the lists. 382 */ 383 error = ath_desc_alloc(sc); 384 if (error != 0) { 385 if_printf(ifp, "failed to allocate descriptors: %d\n", error); 386 goto bad; 387 } 388 callout_init(&sc->sc_scan_ch, debug_mpsafenet ? CALLOUT_MPSAFE : 0); 389 callout_init(&sc->sc_cal_ch, CALLOUT_MPSAFE); 390 callout_init(&sc->sc_dfs_ch, CALLOUT_MPSAFE); 391 392 ATH_TXBUF_LOCK_INIT(sc); 393 394 sc->sc_tq = taskqueue_create("ath_taskq", M_NOWAIT, 395 taskqueue_thread_enqueue, &sc->sc_tq); 396 taskqueue_start_threads(&sc->sc_tq, 1, PI_NET, 397 "%s taskq", ifp->if_xname); 398 399 TASK_INIT(&sc->sc_rxtask, 0, ath_rx_proc, sc); 400 TASK_INIT(&sc->sc_rxorntask, 0, ath_rxorn_proc, sc); 401 TASK_INIT(&sc->sc_bmisstask, 0, ath_bmiss_proc, sc); 402 TASK_INIT(&sc->sc_bstucktask,0, ath_bstuck_proc, sc); 403 TASK_INIT(&sc->sc_radartask, 0, ath_radar_proc, sc); 404 405 /* 406 * Allocate hardware transmit queues: one queue for 407 * beacon frames and one data queue for each QoS 408 * priority. Note that the hal handles reseting 409 * these queues at the needed time. 410 * 411 * XXX PS-Poll 412 */ 413 sc->sc_bhalq = ath_beaconq_setup(ah); 414 if (sc->sc_bhalq == (u_int) -1) { 415 if_printf(ifp, "unable to setup a beacon xmit queue!\n"); 416 error = EIO; 417 goto bad2; 418 } 419 sc->sc_cabq = ath_txq_setup(sc, HAL_TX_QUEUE_CAB, 0); 420 if (sc->sc_cabq == NULL) { 421 if_printf(ifp, "unable to setup CAB xmit queue!\n"); 422 error = EIO; 423 goto bad2; 424 } 425 /* NB: insure BK queue is the lowest priority h/w queue */ 426 if (!ath_tx_setup(sc, WME_AC_BK, HAL_WME_AC_BK)) { 427 if_printf(ifp, "unable to setup xmit queue for %s traffic!\n", 428 ieee80211_wme_acnames[WME_AC_BK]); 429 error = EIO; 430 goto bad2; 431 } 432 if (!ath_tx_setup(sc, WME_AC_BE, HAL_WME_AC_BE) || 433 !ath_tx_setup(sc, WME_AC_VI, HAL_WME_AC_VI) || 434 !ath_tx_setup(sc, WME_AC_VO, HAL_WME_AC_VO)) { 435 /* 436 * Not enough hardware tx queues to properly do WME; 437 * just punt and assign them all to the same h/w queue. 438 * We could do a better job of this if, for example, 439 * we allocate queues when we switch from station to 440 * AP mode. 441 */ 442 if (sc->sc_ac2q[WME_AC_VI] != NULL) 443 ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_VI]); 444 if (sc->sc_ac2q[WME_AC_BE] != NULL) 445 ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_BE]); 446 sc->sc_ac2q[WME_AC_BE] = sc->sc_ac2q[WME_AC_BK]; 447 sc->sc_ac2q[WME_AC_VI] = sc->sc_ac2q[WME_AC_BK]; 448 sc->sc_ac2q[WME_AC_VO] = sc->sc_ac2q[WME_AC_BK]; 449 } 450 451 /* 452 * Special case certain configurations. Note the 453 * CAB queue is handled by these specially so don't 454 * include them when checking the txq setup mask. 455 */ 456 switch (sc->sc_txqsetup &~ (1<<sc->sc_cabq->axq_qnum)) { 457 case 0x01: 458 TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0, sc); 459 break; 460 case 0x0f: 461 TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0123, sc); 462 break; 463 default: 464 TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc, sc); 465 break; 466 } 467 468 /* 469 * Setup rate control. Some rate control modules 470 * call back to change the anntena state so expose 471 * the necessary entry points. 472 * XXX maybe belongs in struct ath_ratectrl? 473 */ 474 sc->sc_setdefantenna = ath_setdefantenna; 475 sc->sc_rc = ath_rate_attach(sc); 476 if (sc->sc_rc == NULL) { 477 error = EIO; 478 goto bad2; 479 } 480 481 sc->sc_blinking = 0; 482 sc->sc_ledstate = 1; 483 sc->sc_ledon = 0; /* low true */ 484 sc->sc_ledidle = (2700*hz)/1000; /* 2.7sec */ 485 callout_init(&sc->sc_ledtimer, CALLOUT_MPSAFE); 486 /* 487 * Auto-enable soft led processing for IBM cards and for 488 * 5211 minipci cards. Users can also manually enable/disable 489 * support with a sysctl. 490 */ 491 sc->sc_softled = (devid == AR5212_DEVID_IBM || devid == AR5211_DEVID); 492 if (sc->sc_softled) { 493 ath_hal_gpioCfgOutput(ah, sc->sc_ledpin); 494 ath_hal_gpioset(ah, sc->sc_ledpin, !sc->sc_ledon); 495 } 496 497 ifp->if_softc = sc; 498 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST; 499 ifp->if_start = ath_start; 500 ifp->if_watchdog = ath_watchdog; 501 ifp->if_ioctl = ath_ioctl; 502 ifp->if_init = ath_init; 503 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 504 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN; 505 IFQ_SET_READY(&ifp->if_snd); 506 507 ic->ic_ifp = ifp; 508 ic->ic_reset = ath_reset; 509 ic->ic_newassoc = ath_newassoc; 510 ic->ic_updateslot = ath_updateslot; 511 ic->ic_wme.wme_update = ath_wme_update; 512 /* XXX not right but it's not used anywhere important */ 513 ic->ic_phytype = IEEE80211_T_OFDM; 514 ic->ic_opmode = IEEE80211_M_STA; 515 ic->ic_caps = 516 IEEE80211_C_IBSS /* ibss, nee adhoc, mode */ 517 | IEEE80211_C_HOSTAP /* hostap mode */ 518 | IEEE80211_C_MONITOR /* monitor mode */ 519 | IEEE80211_C_AHDEMO /* adhoc demo mode */ 520 | IEEE80211_C_SHPREAMBLE /* short preamble supported */ 521 | IEEE80211_C_SHSLOT /* short slot time supported */ 522 | IEEE80211_C_WPA /* capable of WPA1+WPA2 */ 523 ; 524 /* 525 * Query the hal to figure out h/w crypto support. 526 */ 527 if (ath_hal_ciphersupported(ah, HAL_CIPHER_WEP)) 528 ic->ic_caps |= IEEE80211_C_WEP; 529 if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_OCB)) 530 ic->ic_caps |= IEEE80211_C_AES; 531 if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_CCM)) 532 ic->ic_caps |= IEEE80211_C_AES_CCM; 533 if (ath_hal_ciphersupported(ah, HAL_CIPHER_CKIP)) 534 ic->ic_caps |= IEEE80211_C_CKIP; 535 if (ath_hal_ciphersupported(ah, HAL_CIPHER_TKIP)) { 536 ic->ic_caps |= IEEE80211_C_TKIP; 537 /* 538 * Check if h/w does the MIC and/or whether the 539 * separate key cache entries are required to 540 * handle both tx+rx MIC keys. 541 */ 542 if (ath_hal_ciphersupported(ah, HAL_CIPHER_MIC)) 543 ic->ic_caps |= IEEE80211_C_TKIPMIC; 544 if (ath_hal_tkipsplit(ah)) 545 sc->sc_splitmic = 1; 546 } 547 sc->sc_hasclrkey = ath_hal_ciphersupported(ah, HAL_CIPHER_CLR); 548 sc->sc_mcastkey = ath_hal_getmcastkeysearch(ah); 549 /* 550 * TPC support can be done either with a global cap or 551 * per-packet support. The latter is not available on 552 * all parts. We're a bit pedantic here as all parts 553 * support a global cap. 554 */ 555 if (ath_hal_hastpc(ah) || ath_hal_hastxpowlimit(ah)) 556 ic->ic_caps |= IEEE80211_C_TXPMGT; 557 558 /* 559 * Mark WME capability only if we have sufficient 560 * hardware queues to do proper priority scheduling. 561 */ 562 if (sc->sc_ac2q[WME_AC_BE] != sc->sc_ac2q[WME_AC_BK]) 563 ic->ic_caps |= IEEE80211_C_WME; 564 /* 565 * Check for misc other capabilities. 566 */ 567 if (ath_hal_hasbursting(ah)) 568 ic->ic_caps |= IEEE80211_C_BURST; 569 570 /* 571 * Indicate we need the 802.11 header padded to a 572 * 32-bit boundary for 4-address and QoS frames. 573 */ 574 ic->ic_flags |= IEEE80211_F_DATAPAD; 575 576 /* 577 * Query the hal about antenna support. 578 */ 579 sc->sc_defant = ath_hal_getdefantenna(ah); 580 581 /* 582 * Not all chips have the VEOL support we want to 583 * use with IBSS beacons; check here for it. 584 */ 585 sc->sc_hasveol = ath_hal_hasveol(ah); 586 587 /* get mac address from hardware */ 588 ath_hal_getmac(ah, ic->ic_myaddr); 589 590 /* call MI attach routine. */ 591 ieee80211_ifattach(ic); 592 sc->sc_opmode = ic->ic_opmode; 593 /* override default methods */ 594 ic->ic_node_alloc = ath_node_alloc; 595 sc->sc_node_free = ic->ic_node_free; 596 ic->ic_node_free = ath_node_free; 597 ic->ic_node_getrssi = ath_node_getrssi; 598 sc->sc_recv_mgmt = ic->ic_recv_mgmt; 599 ic->ic_recv_mgmt = ath_recv_mgmt; 600 sc->sc_newstate = ic->ic_newstate; 601 ic->ic_newstate = ath_newstate; 602 ic->ic_crypto.cs_max_keyix = sc->sc_keymax; 603 ic->ic_crypto.cs_key_alloc = ath_key_alloc; 604 ic->ic_crypto.cs_key_delete = ath_key_delete; 605 ic->ic_crypto.cs_key_set = ath_key_set; 606 ic->ic_crypto.cs_key_update_begin = ath_key_update_begin; 607 ic->ic_crypto.cs_key_update_end = ath_key_update_end; 608 /* complete initialization */ 609 ieee80211_media_init(ic, ath_media_change, ieee80211_media_status); 610 611 ath_bpfattach(sc); 612 /* 613 * Setup dynamic sysctl's now that country code and 614 * regdomain are available from the hal. 615 */ 616 ath_sysctlattach(sc); 617 618 if (bootverbose) 619 ieee80211_announce(ic); 620 ath_announce(sc); 621 return 0; 622 bad2: 623 ath_tx_cleanup(sc); 624 ath_desc_free(sc); 625 bad: 626 if (ah) 627 ath_hal_detach(ah); 628 if (ifp != NULL) 629 if_free(ifp); 630 sc->sc_invalid = 1; 631 return error; 632 } 633 634 int 635 ath_detach(struct ath_softc *sc) 636 { 637 struct ifnet *ifp = sc->sc_ifp; 638 639 DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n", 640 __func__, ifp->if_flags); 641 642 ath_stop(ifp); 643 bpfdetach(ifp); 644 /* 645 * NB: the order of these is important: 646 * o call the 802.11 layer before detaching the hal to 647 * insure callbacks into the driver to delete global 648 * key cache entries can be handled 649 * o reclaim the tx queue data structures after calling 650 * the 802.11 layer as we'll get called back to reclaim 651 * node state and potentially want to use them 652 * o to cleanup the tx queues the hal is called, so detach 653 * it last 654 * Other than that, it's straightforward... 655 */ 656 ieee80211_ifdetach(&sc->sc_ic); 657 #ifdef ATH_TX99_DIAG 658 if (sc->sc_tx99 != NULL) 659 sc->sc_tx99->detach(sc->sc_tx99); 660 #endif 661 taskqueue_free(sc->sc_tq); 662 ath_rate_detach(sc->sc_rc); 663 ath_desc_free(sc); 664 ath_tx_cleanup(sc); 665 ath_hal_detach(sc->sc_ah); 666 if_free(ifp); 667 668 return 0; 669 } 670 671 void 672 ath_suspend(struct ath_softc *sc) 673 { 674 struct ifnet *ifp = sc->sc_ifp; 675 676 DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n", 677 __func__, ifp->if_flags); 678 679 ath_stop(ifp); 680 } 681 682 void 683 ath_resume(struct ath_softc *sc) 684 { 685 struct ifnet *ifp = sc->sc_ifp; 686 687 DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n", 688 __func__, ifp->if_flags); 689 690 if (ifp->if_flags & IFF_UP) { 691 ath_init(sc); 692 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 693 ath_start(ifp); 694 } 695 if (sc->sc_softled) { 696 ath_hal_gpioCfgOutput(sc->sc_ah, sc->sc_ledpin); 697 ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, !sc->sc_ledon); 698 } 699 } 700 701 void 702 ath_shutdown(struct ath_softc *sc) 703 { 704 struct ifnet *ifp = sc->sc_ifp; 705 706 DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n", 707 __func__, ifp->if_flags); 708 709 ath_stop(ifp); 710 } 711 712 /* 713 * Interrupt handler. Most of the actual processing is deferred. 714 */ 715 void 716 ath_intr(void *arg) 717 { 718 struct ath_softc *sc = arg; 719 struct ifnet *ifp = sc->sc_ifp; 720 struct ath_hal *ah = sc->sc_ah; 721 HAL_INT status; 722 723 if (sc->sc_invalid) { 724 /* 725 * The hardware is not ready/present, don't touch anything. 726 * Note this can happen early on if the IRQ is shared. 727 */ 728 DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid; ignored\n", __func__); 729 return; 730 } 731 if (!ath_hal_intrpend(ah)) /* shared irq, not for us */ 732 return; 733 if (!((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & 734 IFF_DRV_RUNNING))) { 735 DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n", 736 __func__, ifp->if_flags); 737 ath_hal_getisr(ah, &status); /* clear ISR */ 738 ath_hal_intrset(ah, 0); /* disable further intr's */ 739 return; 740 } 741 /* 742 * Figure out the reason(s) for the interrupt. Note 743 * that the hal returns a pseudo-ISR that may include 744 * bits we haven't explicitly enabled so we mask the 745 * value to insure we only process bits we requested. 746 */ 747 ath_hal_getisr(ah, &status); /* NB: clears ISR too */ 748 DPRINTF(sc, ATH_DEBUG_INTR, "%s: status 0x%x\n", __func__, status); 749 status &= sc->sc_imask; /* discard unasked for bits */ 750 if (status & HAL_INT_FATAL) { 751 sc->sc_stats.ast_hardware++; 752 ath_hal_intrset(ah, 0); /* disable intr's until reset */ 753 ath_fatal_proc(sc, 0); 754 } else if (status & HAL_INT_RXORN) { 755 sc->sc_stats.ast_rxorn++; 756 ath_hal_intrset(ah, 0); /* disable intr's until reset */ 757 taskqueue_enqueue(sc->sc_tq, &sc->sc_rxorntask); 758 } else { 759 if (status & HAL_INT_SWBA) { 760 /* 761 * Software beacon alert--time to send a beacon. 762 * Handle beacon transmission directly; deferring 763 * this is too slow to meet timing constraints 764 * under load. 765 */ 766 ath_beacon_proc(sc, 0); 767 } 768 if (status & HAL_INT_RXEOL) { 769 /* 770 * NB: the hardware should re-read the link when 771 * RXE bit is written, but it doesn't work at 772 * least on older hardware revs. 773 */ 774 sc->sc_stats.ast_rxeol++; 775 sc->sc_rxlink = NULL; 776 } 777 if (status & HAL_INT_TXURN) { 778 sc->sc_stats.ast_txurn++; 779 /* bump tx trigger level */ 780 ath_hal_updatetxtriglevel(ah, AH_TRUE); 781 } 782 if (status & HAL_INT_RX) 783 taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask); 784 if (status & HAL_INT_TX) 785 taskqueue_enqueue(sc->sc_tq, &sc->sc_txtask); 786 if (status & HAL_INT_BMISS) { 787 sc->sc_stats.ast_bmiss++; 788 taskqueue_enqueue(sc->sc_tq, &sc->sc_bmisstask); 789 } 790 if (status & HAL_INT_MIB) { 791 sc->sc_stats.ast_mib++; 792 /* 793 * Disable interrupts until we service the MIB 794 * interrupt; otherwise it will continue to fire. 795 */ 796 ath_hal_intrset(ah, 0); 797 /* 798 * Let the hal handle the event. We assume it will 799 * clear whatever condition caused the interrupt. 800 */ 801 ath_hal_mibevent(ah, &sc->sc_halstats); 802 ath_hal_intrset(ah, sc->sc_imask); 803 } 804 } 805 } 806 807 static void 808 ath_fatal_proc(void *arg, int pending) 809 { 810 struct ath_softc *sc = arg; 811 struct ifnet *ifp = sc->sc_ifp; 812 u_int32_t *state; 813 u_int32_t len; 814 815 if_printf(ifp, "hardware error; resetting\n"); 816 /* 817 * Fatal errors are unrecoverable. Typically these 818 * are caused by DMA errors. Collect h/w state from 819 * the hal so we can diagnose what's going on. 820 */ 821 if (ath_hal_getfatalstate(sc->sc_ah, &state, &len)) { 822 KASSERT(len >= 6*sizeof(u_int32_t), ("len %u bytes", len)); 823 if_printf(ifp, "0x%08x 0x%08x 0x%08x, 0x%08x 0x%08x 0x%08x\n", 824 state[0], state[1] , state[2], state[3], 825 state[4], state[5]); 826 } 827 ath_reset(ifp); 828 } 829 830 static void 831 ath_rxorn_proc(void *arg, int pending) 832 { 833 struct ath_softc *sc = arg; 834 struct ifnet *ifp = sc->sc_ifp; 835 836 if_printf(ifp, "rx FIFO overrun; resetting\n"); 837 ath_reset(ifp); 838 } 839 840 static void 841 ath_bmiss_proc(void *arg, int pending) 842 { 843 struct ath_softc *sc = arg; 844 struct ieee80211com *ic = &sc->sc_ic; 845 846 DPRINTF(sc, ATH_DEBUG_ANY, "%s: pending %u\n", __func__, pending); 847 KASSERT(ic->ic_opmode == IEEE80211_M_STA, 848 ("unexpect operating mode %u", ic->ic_opmode)); 849 if (ic->ic_state == IEEE80211_S_RUN) { 850 u_int64_t lastrx = sc->sc_lastrx; 851 u_int64_t tsf = ath_hal_gettsf64(sc->sc_ah); 852 u_int bmisstimeout = 853 ic->ic_bmissthreshold * ic->ic_bss->ni_intval * 1024; 854 855 DPRINTF(sc, ATH_DEBUG_BEACON, 856 "%s: tsf %llu lastrx %lld (%llu) bmiss %u\n", 857 __func__, (unsigned long long) tsf, 858 (unsigned long long)(tsf - lastrx), 859 (unsigned long long) lastrx, bmisstimeout); 860 /* 861 * Workaround phantom bmiss interrupts by sanity-checking 862 * the time of our last rx'd frame. If it is within the 863 * beacon miss interval then ignore the interrupt. If it's 864 * truly a bmiss we'll get another interrupt soon and that'll 865 * be dispatched up for processing. 866 */ 867 if (tsf - lastrx > bmisstimeout) { 868 NET_LOCK_GIANT(); 869 ieee80211_beacon_miss(ic); 870 NET_UNLOCK_GIANT(); 871 } else 872 sc->sc_stats.ast_bmiss_phantom++; 873 } 874 } 875 876 static void 877 ath_radar_proc(void *arg, int pending) 878 { 879 struct ath_softc *sc = arg; 880 struct ifnet *ifp = sc->sc_ifp; 881 struct ath_hal *ah = sc->sc_ah; 882 HAL_CHANNEL hchan; 883 884 if (ath_hal_procdfs(ah, &hchan)) { 885 if_printf(ifp, "radar detected on channel %u/0x%x/0x%x\n", 886 hchan.channel, hchan.channelFlags, hchan.privFlags); 887 /* 888 * Initiate channel change. 889 */ 890 /* XXX not yet */ 891 } 892 } 893 894 static u_int 895 ath_chan2flags(struct ieee80211com *ic, struct ieee80211_channel *chan) 896 { 897 #define N(a) (sizeof(a) / sizeof(a[0])) 898 static const u_int modeflags[] = { 899 0, /* IEEE80211_MODE_AUTO */ 900 CHANNEL_A, /* IEEE80211_MODE_11A */ 901 CHANNEL_B, /* IEEE80211_MODE_11B */ 902 CHANNEL_PUREG, /* IEEE80211_MODE_11G */ 903 0, /* IEEE80211_MODE_FH */ 904 CHANNEL_ST, /* IEEE80211_MODE_TURBO_A */ 905 CHANNEL_108G /* IEEE80211_MODE_TURBO_G */ 906 }; 907 enum ieee80211_phymode mode = ieee80211_chan2mode(ic, chan); 908 909 KASSERT(mode < N(modeflags), ("unexpected phy mode %u", mode)); 910 KASSERT(modeflags[mode] != 0, ("mode %u undefined", mode)); 911 return modeflags[mode]; 912 #undef N 913 } 914 915 static void 916 ath_init(void *arg) 917 { 918 struct ath_softc *sc = (struct ath_softc *) arg; 919 struct ieee80211com *ic = &sc->sc_ic; 920 struct ifnet *ifp = sc->sc_ifp; 921 struct ath_hal *ah = sc->sc_ah; 922 HAL_STATUS status; 923 924 DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n", 925 __func__, ifp->if_flags); 926 927 ATH_LOCK(sc); 928 /* 929 * Stop anything previously setup. This is safe 930 * whether this is the first time through or not. 931 */ 932 ath_stop_locked(ifp); 933 934 /* 935 * The basic interface to setting the hardware in a good 936 * state is ``reset''. On return the hardware is known to 937 * be powered up and with interrupts disabled. This must 938 * be followed by initialization of the appropriate bits 939 * and then setup of the interrupt mask. 940 */ 941 sc->sc_curchan.channel = ic->ic_curchan->ic_freq; 942 sc->sc_curchan.channelFlags = ath_chan2flags(ic, ic->ic_curchan); 943 if (!ath_hal_reset(ah, sc->sc_opmode, &sc->sc_curchan, AH_FALSE, &status)) { 944 if_printf(ifp, "unable to reset hardware; hal status %u\n", 945 status); 946 goto done; 947 } 948 949 /* 950 * This is needed only to setup initial state 951 * but it's best done after a reset. 952 */ 953 ath_update_txpow(sc); 954 /* 955 * Likewise this is set during reset so update 956 * state cached in the driver. 957 */ 958 sc->sc_diversity = ath_hal_getdiversity(ah); 959 sc->sc_calinterval = 1; 960 sc->sc_caltries = 0; 961 962 /* 963 * Setup the hardware after reset: the key cache 964 * is filled as needed and the receive engine is 965 * set going. Frame transmit is handled entirely 966 * in the frame output path; there's nothing to do 967 * here except setup the interrupt mask. 968 */ 969 if (ath_startrecv(sc) != 0) { 970 if_printf(ifp, "unable to start recv logic\n"); 971 goto done; 972 } 973 974 /* 975 * Enable interrupts. 976 */ 977 sc->sc_imask = HAL_INT_RX | HAL_INT_TX 978 | HAL_INT_RXEOL | HAL_INT_RXORN 979 | HAL_INT_FATAL | HAL_INT_GLOBAL; 980 /* 981 * Enable MIB interrupts when there are hardware phy counters. 982 * Note we only do this (at the moment) for station mode. 983 */ 984 if (sc->sc_needmib && ic->ic_opmode == IEEE80211_M_STA) 985 sc->sc_imask |= HAL_INT_MIB; 986 ath_hal_intrset(ah, sc->sc_imask); 987 988 ifp->if_drv_flags |= IFF_DRV_RUNNING; 989 ic->ic_state = IEEE80211_S_INIT; 990 991 /* 992 * The hardware should be ready to go now so it's safe 993 * to kick the 802.11 state machine as it's likely to 994 * immediately call back to us to send mgmt frames. 995 */ 996 ath_chan_change(sc, ic->ic_curchan); 997 #ifdef ATH_TX99_DIAG 998 if (sc->sc_tx99 != NULL) 999 sc->sc_tx99->start(sc->sc_tx99); 1000 else 1001 #endif 1002 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 1003 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL) 1004 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 1005 } else 1006 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 1007 done: 1008 ATH_UNLOCK(sc); 1009 } 1010 1011 static void 1012 ath_stop_locked(struct ifnet *ifp) 1013 { 1014 struct ath_softc *sc = ifp->if_softc; 1015 struct ieee80211com *ic = &sc->sc_ic; 1016 struct ath_hal *ah = sc->sc_ah; 1017 1018 DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid %u if_flags 0x%x\n", 1019 __func__, sc->sc_invalid, ifp->if_flags); 1020 1021 ATH_LOCK_ASSERT(sc); 1022 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 1023 /* 1024 * Shutdown the hardware and driver: 1025 * reset 802.11 state machine 1026 * turn off timers 1027 * disable interrupts 1028 * turn off the radio 1029 * clear transmit machinery 1030 * clear receive machinery 1031 * drain and release tx queues 1032 * reclaim beacon resources 1033 * power down hardware 1034 * 1035 * Note that some of this work is not possible if the 1036 * hardware is gone (invalid). 1037 */ 1038 #ifdef ATH_TX99_DIAG 1039 if (sc->sc_tx99 != NULL) 1040 sc->sc_tx99->stop(sc->sc_tx99); 1041 #endif 1042 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 1043 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1044 ifp->if_timer = 0; 1045 if (!sc->sc_invalid) { 1046 if (sc->sc_softled) { 1047 callout_stop(&sc->sc_ledtimer); 1048 ath_hal_gpioset(ah, sc->sc_ledpin, 1049 !sc->sc_ledon); 1050 sc->sc_blinking = 0; 1051 } 1052 ath_hal_intrset(ah, 0); 1053 } 1054 ath_draintxq(sc); 1055 if (!sc->sc_invalid) { 1056 ath_stoprecv(sc); 1057 ath_hal_phydisable(ah); 1058 } else 1059 sc->sc_rxlink = NULL; 1060 IFQ_DRV_PURGE(&ifp->if_snd); 1061 ath_beacon_free(sc); 1062 } 1063 } 1064 1065 static void 1066 ath_stop(struct ifnet *ifp) 1067 { 1068 struct ath_softc *sc = ifp->if_softc; 1069 1070 ATH_LOCK(sc); 1071 ath_stop_locked(ifp); 1072 if (!sc->sc_invalid) { 1073 /* 1074 * Set the chip in full sleep mode. Note that we are 1075 * careful to do this only when bringing the interface 1076 * completely to a stop. When the chip is in this state 1077 * it must be carefully woken up or references to 1078 * registers in the PCI clock domain may freeze the bus 1079 * (and system). This varies by chip and is mostly an 1080 * issue with newer parts that go to sleep more quickly. 1081 */ 1082 ath_hal_setpower(sc->sc_ah, HAL_PM_FULL_SLEEP); 1083 } 1084 ATH_UNLOCK(sc); 1085 } 1086 1087 /* 1088 * Reset the hardware w/o losing operational state. This is 1089 * basically a more efficient way of doing ath_stop, ath_init, 1090 * followed by state transitions to the current 802.11 1091 * operational state. Used to recover from various errors and 1092 * to reset or reload hardware state. 1093 */ 1094 static int 1095 ath_reset(struct ifnet *ifp) 1096 { 1097 struct ath_softc *sc = ifp->if_softc; 1098 struct ieee80211com *ic = &sc->sc_ic; 1099 struct ath_hal *ah = sc->sc_ah; 1100 struct ieee80211_channel *c; 1101 HAL_STATUS status; 1102 1103 /* 1104 * Convert to a HAL channel description with the flags 1105 * constrained to reflect the current operating mode. 1106 */ 1107 c = ic->ic_curchan; 1108 sc->sc_curchan.channel = c->ic_freq; 1109 sc->sc_curchan.channelFlags = ath_chan2flags(ic, c); 1110 1111 ath_hal_intrset(ah, 0); /* disable interrupts */ 1112 ath_draintxq(sc); /* stop xmit side */ 1113 ath_stoprecv(sc); /* stop recv side */ 1114 /* NB: indicate channel change so we do a full reset */ 1115 if (!ath_hal_reset(ah, sc->sc_opmode, &sc->sc_curchan, AH_TRUE, &status)) 1116 if_printf(ifp, "%s: unable to reset hardware; hal status %u\n", 1117 __func__, status); 1118 ath_update_txpow(sc); /* update tx power state */ 1119 sc->sc_diversity = ath_hal_getdiversity(ah); 1120 sc->sc_calinterval = 1; 1121 sc->sc_caltries = 0; 1122 /* 1123 * We may be doing a reset in response to an ioctl 1124 * that changes the channel so update any state that 1125 * might change as a result. 1126 */ 1127 ath_chan_change(sc, c); 1128 if (ath_startrecv(sc) != 0) /* restart recv */ 1129 if_printf(ifp, "%s: unable to start recv logic\n", __func__); 1130 if (ic->ic_state == IEEE80211_S_RUN) 1131 ath_beacon_config(sc); /* restart beacons */ 1132 ath_hal_intrset(ah, sc->sc_imask); 1133 1134 ath_start(ifp); /* restart xmit */ 1135 return 0; 1136 } 1137 1138 static void 1139 ath_start(struct ifnet *ifp) 1140 { 1141 struct ath_softc *sc = ifp->if_softc; 1142 struct ath_hal *ah = sc->sc_ah; 1143 struct ieee80211com *ic = &sc->sc_ic; 1144 struct ieee80211_node *ni; 1145 struct ath_buf *bf; 1146 struct mbuf *m; 1147 struct ieee80211_frame *wh; 1148 struct ether_header *eh; 1149 1150 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->sc_invalid) 1151 return; 1152 for (;;) { 1153 /* 1154 * Grab a TX buffer and associated resources. 1155 */ 1156 ATH_TXBUF_LOCK(sc); 1157 bf = STAILQ_FIRST(&sc->sc_txbuf); 1158 if (bf != NULL) 1159 STAILQ_REMOVE_HEAD(&sc->sc_txbuf, bf_list); 1160 ATH_TXBUF_UNLOCK(sc); 1161 if (bf == NULL) { 1162 DPRINTF(sc, ATH_DEBUG_XMIT, "%s: out of xmit buffers\n", 1163 __func__); 1164 sc->sc_stats.ast_tx_qstop++; 1165 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1166 break; 1167 } 1168 /* 1169 * Poll the management queue for frames; they 1170 * have priority over normal data frames. 1171 */ 1172 IF_DEQUEUE(&ic->ic_mgtq, m); 1173 if (m == NULL) { 1174 /* 1175 * No data frames go out unless we're associated. 1176 */ 1177 if (ic->ic_state != IEEE80211_S_RUN) { 1178 DPRINTF(sc, ATH_DEBUG_XMIT, 1179 "%s: discard data packet, state %s\n", 1180 __func__, 1181 ieee80211_state_name[ic->ic_state]); 1182 sc->sc_stats.ast_tx_discard++; 1183 ATH_TXBUF_LOCK(sc); 1184 STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list); 1185 ATH_TXBUF_UNLOCK(sc); 1186 break; 1187 } 1188 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); /* XXX: LOCK */ 1189 if (m == NULL) { 1190 ATH_TXBUF_LOCK(sc); 1191 STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list); 1192 ATH_TXBUF_UNLOCK(sc); 1193 break; 1194 } 1195 /* 1196 * Find the node for the destination so we can do 1197 * things like power save and fast frames aggregation. 1198 */ 1199 if (m->m_len < sizeof(struct ether_header) && 1200 (m = m_pullup(m, sizeof(struct ether_header))) == NULL) { 1201 ic->ic_stats.is_tx_nobuf++; /* XXX */ 1202 ni = NULL; 1203 goto bad; 1204 } 1205 eh = mtod(m, struct ether_header *); 1206 ni = ieee80211_find_txnode(ic, eh->ether_dhost); 1207 if (ni == NULL) { 1208 /* NB: ieee80211_find_txnode does stat+msg */ 1209 m_freem(m); 1210 goto bad; 1211 } 1212 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) && 1213 (m->m_flags & M_PWR_SAV) == 0) { 1214 /* 1215 * Station in power save mode; pass the frame 1216 * to the 802.11 layer and continue. We'll get 1217 * the frame back when the time is right. 1218 */ 1219 ieee80211_pwrsave(ic, ni, m); 1220 goto reclaim; 1221 } 1222 /* calculate priority so we can find the tx queue */ 1223 if (ieee80211_classify(ic, m, ni)) { 1224 DPRINTF(sc, ATH_DEBUG_XMIT, 1225 "%s: discard, classification failure\n", 1226 __func__); 1227 m_freem(m); 1228 goto bad; 1229 } 1230 ifp->if_opackets++; 1231 BPF_MTAP(ifp, m); 1232 /* 1233 * Encapsulate the packet in prep for transmission. 1234 */ 1235 m = ieee80211_encap(ic, m, ni); 1236 if (m == NULL) { 1237 DPRINTF(sc, ATH_DEBUG_XMIT, 1238 "%s: encapsulation failure\n", 1239 __func__); 1240 sc->sc_stats.ast_tx_encap++; 1241 goto bad; 1242 } 1243 } else { 1244 /* 1245 * Hack! The referenced node pointer is in the 1246 * rcvif field of the packet header. This is 1247 * placed there by ieee80211_mgmt_output because 1248 * we need to hold the reference with the frame 1249 * and there's no other way (other than packet 1250 * tags which we consider too expensive to use) 1251 * to pass it along. 1252 */ 1253 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif; 1254 m->m_pkthdr.rcvif = NULL; 1255 1256 wh = mtod(m, struct ieee80211_frame *); 1257 if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == 1258 IEEE80211_FC0_SUBTYPE_PROBE_RESP) { 1259 /* fill time stamp */ 1260 u_int64_t tsf; 1261 u_int32_t *tstamp; 1262 1263 tsf = ath_hal_gettsf64(ah); 1264 /* XXX: adjust 100us delay to xmit */ 1265 tsf += 100; 1266 tstamp = (u_int32_t *)&wh[1]; 1267 tstamp[0] = htole32(tsf & 0xffffffff); 1268 tstamp[1] = htole32(tsf >> 32); 1269 } 1270 sc->sc_stats.ast_tx_mgmt++; 1271 } 1272 1273 if (ath_tx_start(sc, ni, bf, m)) { 1274 bad: 1275 ifp->if_oerrors++; 1276 reclaim: 1277 ATH_TXBUF_LOCK(sc); 1278 STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list); 1279 ATH_TXBUF_UNLOCK(sc); 1280 if (ni != NULL) 1281 ieee80211_free_node(ni); 1282 continue; 1283 } 1284 1285 sc->sc_tx_timer = 5; 1286 ifp->if_timer = 1; 1287 } 1288 } 1289 1290 static int 1291 ath_media_change(struct ifnet *ifp) 1292 { 1293 #define IS_UP(ifp) \ 1294 ((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING)) 1295 int error; 1296 1297 error = ieee80211_media_change(ifp); 1298 if (error == ENETRESET) { 1299 struct ath_softc *sc = ifp->if_softc; 1300 struct ieee80211com *ic = &sc->sc_ic; 1301 1302 if (ic->ic_opmode == IEEE80211_M_AHDEMO) { 1303 /* 1304 * Adhoc demo mode is just ibss mode w/o beacons 1305 * (mostly). The hal knows nothing about it; 1306 * tell it we're operating in ibss mode. 1307 */ 1308 sc->sc_opmode = HAL_M_IBSS; 1309 } else 1310 sc->sc_opmode = ic->ic_opmode; 1311 if (IS_UP(ifp)) 1312 ath_init(ifp->if_softc); /* XXX lose error */ 1313 error = 0; 1314 } 1315 return error; 1316 #undef IS_UP 1317 } 1318 1319 #ifdef ATH_DEBUG 1320 static void 1321 ath_keyprint(const char *tag, u_int ix, 1322 const HAL_KEYVAL *hk, const u_int8_t mac[IEEE80211_ADDR_LEN]) 1323 { 1324 static const char *ciphers[] = { 1325 "WEP", 1326 "AES-OCB", 1327 "AES-CCM", 1328 "CKIP", 1329 "TKIP", 1330 "CLR", 1331 }; 1332 int i, n; 1333 1334 printf("%s: [%02u] %-7s ", tag, ix, ciphers[hk->kv_type]); 1335 for (i = 0, n = hk->kv_len; i < n; i++) 1336 printf("%02x", hk->kv_val[i]); 1337 printf(" mac %s", ether_sprintf(mac)); 1338 if (hk->kv_type == HAL_CIPHER_TKIP) { 1339 printf(" mic "); 1340 for (i = 0; i < sizeof(hk->kv_mic); i++) 1341 printf("%02x", hk->kv_mic[i]); 1342 } 1343 printf("\n"); 1344 } 1345 #endif 1346 1347 /* 1348 * Set a TKIP key into the hardware. This handles the 1349 * potential distribution of key state to multiple key 1350 * cache slots for TKIP. 1351 */ 1352 static int 1353 ath_keyset_tkip(struct ath_softc *sc, const struct ieee80211_key *k, 1354 HAL_KEYVAL *hk, const u_int8_t mac[IEEE80211_ADDR_LEN]) 1355 { 1356 #define IEEE80211_KEY_XR (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV) 1357 static const u_int8_t zerobssid[IEEE80211_ADDR_LEN]; 1358 struct ath_hal *ah = sc->sc_ah; 1359 1360 KASSERT(k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP, 1361 ("got a non-TKIP key, cipher %u", k->wk_cipher->ic_cipher)); 1362 KASSERT(sc->sc_splitmic, ("key cache !split")); 1363 if ((k->wk_flags & IEEE80211_KEY_XR) == IEEE80211_KEY_XR) { 1364 /* 1365 * TX key goes at first index, RX key at the rx index. 1366 * The hal handles the MIC keys at index+64. 1367 */ 1368 memcpy(hk->kv_mic, k->wk_txmic, sizeof(hk->kv_mic)); 1369 KEYPRINTF(sc, k->wk_keyix, hk, zerobssid); 1370 if (!ath_hal_keyset(ah, k->wk_keyix, hk, zerobssid)) 1371 return 0; 1372 1373 memcpy(hk->kv_mic, k->wk_rxmic, sizeof(hk->kv_mic)); 1374 KEYPRINTF(sc, k->wk_keyix+32, hk, mac); 1375 /* XXX delete tx key on failure? */ 1376 return ath_hal_keyset(ah, k->wk_keyix+32, hk, mac); 1377 } else if (k->wk_flags & IEEE80211_KEY_XR) { 1378 /* 1379 * TX/RX key goes at first index. 1380 * The hal handles the MIC keys are index+64. 1381 */ 1382 memcpy(hk->kv_mic, k->wk_flags & IEEE80211_KEY_XMIT ? 1383 k->wk_txmic : k->wk_rxmic, sizeof(hk->kv_mic)); 1384 KEYPRINTF(sc, k->wk_keyix, hk, mac); 1385 return ath_hal_keyset(ah, k->wk_keyix, hk, mac); 1386 } 1387 return 0; 1388 #undef IEEE80211_KEY_XR 1389 } 1390 1391 /* 1392 * Set a net80211 key into the hardware. This handles the 1393 * potential distribution of key state to multiple key 1394 * cache slots for TKIP with hardware MIC support. 1395 */ 1396 static int 1397 ath_keyset(struct ath_softc *sc, const struct ieee80211_key *k, 1398 const u_int8_t mac0[IEEE80211_ADDR_LEN], 1399 struct ieee80211_node *bss) 1400 { 1401 #define N(a) (sizeof(a)/sizeof(a[0])) 1402 static const u_int8_t ciphermap[] = { 1403 HAL_CIPHER_WEP, /* IEEE80211_CIPHER_WEP */ 1404 HAL_CIPHER_TKIP, /* IEEE80211_CIPHER_TKIP */ 1405 HAL_CIPHER_AES_OCB, /* IEEE80211_CIPHER_AES_OCB */ 1406 HAL_CIPHER_AES_CCM, /* IEEE80211_CIPHER_AES_CCM */ 1407 (u_int8_t) -1, /* 4 is not allocated */ 1408 HAL_CIPHER_CKIP, /* IEEE80211_CIPHER_CKIP */ 1409 HAL_CIPHER_CLR, /* IEEE80211_CIPHER_NONE */ 1410 }; 1411 struct ath_hal *ah = sc->sc_ah; 1412 const struct ieee80211_cipher *cip = k->wk_cipher; 1413 u_int8_t gmac[IEEE80211_ADDR_LEN]; 1414 const u_int8_t *mac; 1415 HAL_KEYVAL hk; 1416 1417 memset(&hk, 0, sizeof(hk)); 1418 /* 1419 * Software crypto uses a "clear key" so non-crypto 1420 * state kept in the key cache are maintained and 1421 * so that rx frames have an entry to match. 1422 */ 1423 if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) == 0) { 1424 KASSERT(cip->ic_cipher < N(ciphermap), 1425 ("invalid cipher type %u", cip->ic_cipher)); 1426 hk.kv_type = ciphermap[cip->ic_cipher]; 1427 hk.kv_len = k->wk_keylen; 1428 memcpy(hk.kv_val, k->wk_key, k->wk_keylen); 1429 } else 1430 hk.kv_type = HAL_CIPHER_CLR; 1431 1432 if ((k->wk_flags & IEEE80211_KEY_GROUP) && sc->sc_mcastkey) { 1433 /* 1434 * Group keys on hardware that supports multicast frame 1435 * key search use a mac that is the sender's address with 1436 * the high bit set instead of the app-specified address. 1437 */ 1438 IEEE80211_ADDR_COPY(gmac, bss->ni_macaddr); 1439 gmac[0] |= 0x80; 1440 mac = gmac; 1441 } else 1442 mac = mac0; 1443 1444 if (hk.kv_type == HAL_CIPHER_TKIP && 1445 (k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && 1446 sc->sc_splitmic) { 1447 return ath_keyset_tkip(sc, k, &hk, mac); 1448 } else { 1449 KEYPRINTF(sc, k->wk_keyix, &hk, mac); 1450 return ath_hal_keyset(ah, k->wk_keyix, &hk, mac); 1451 } 1452 #undef N 1453 } 1454 1455 /* 1456 * Allocate tx/rx key slots for TKIP. We allocate two slots for 1457 * each key, one for decrypt/encrypt and the other for the MIC. 1458 */ 1459 static u_int16_t 1460 key_alloc_2pair(struct ath_softc *sc, 1461 ieee80211_keyix *txkeyix, ieee80211_keyix *rxkeyix) 1462 { 1463 #define N(a) (sizeof(a)/sizeof(a[0])) 1464 u_int i, keyix; 1465 1466 KASSERT(sc->sc_splitmic, ("key cache !split")); 1467 /* XXX could optimize */ 1468 for (i = 0; i < N(sc->sc_keymap)/4; i++) { 1469 u_int8_t b = sc->sc_keymap[i]; 1470 if (b != 0xff) { 1471 /* 1472 * One or more slots in this byte are free. 1473 */ 1474 keyix = i*NBBY; 1475 while (b & 1) { 1476 again: 1477 keyix++; 1478 b >>= 1; 1479 } 1480 /* XXX IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV */ 1481 if (isset(sc->sc_keymap, keyix+32) || 1482 isset(sc->sc_keymap, keyix+64) || 1483 isset(sc->sc_keymap, keyix+32+64)) { 1484 /* full pair unavailable */ 1485 /* XXX statistic */ 1486 if (keyix == (i+1)*NBBY) { 1487 /* no slots were appropriate, advance */ 1488 continue; 1489 } 1490 goto again; 1491 } 1492 setbit(sc->sc_keymap, keyix); 1493 setbit(sc->sc_keymap, keyix+64); 1494 setbit(sc->sc_keymap, keyix+32); 1495 setbit(sc->sc_keymap, keyix+32+64); 1496 DPRINTF(sc, ATH_DEBUG_KEYCACHE, 1497 "%s: key pair %u,%u %u,%u\n", 1498 __func__, keyix, keyix+64, 1499 keyix+32, keyix+32+64); 1500 *txkeyix = keyix; 1501 *rxkeyix = keyix+32; 1502 return 1; 1503 } 1504 } 1505 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of pair space\n", __func__); 1506 return 0; 1507 #undef N 1508 } 1509 1510 /* 1511 * Allocate a single key cache slot. 1512 */ 1513 static int 1514 key_alloc_single(struct ath_softc *sc, 1515 ieee80211_keyix *txkeyix, ieee80211_keyix *rxkeyix) 1516 { 1517 #define N(a) (sizeof(a)/sizeof(a[0])) 1518 u_int i, keyix; 1519 1520 /* XXX try i,i+32,i+64,i+32+64 to minimize key pair conflicts */ 1521 for (i = 0; i < N(sc->sc_keymap); i++) { 1522 u_int8_t b = sc->sc_keymap[i]; 1523 if (b != 0xff) { 1524 /* 1525 * One or more slots are free. 1526 */ 1527 keyix = i*NBBY; 1528 while (b & 1) 1529 keyix++, b >>= 1; 1530 setbit(sc->sc_keymap, keyix); 1531 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: key %u\n", 1532 __func__, keyix); 1533 *txkeyix = *rxkeyix = keyix; 1534 return 1; 1535 } 1536 } 1537 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of space\n", __func__); 1538 return 0; 1539 #undef N 1540 } 1541 1542 /* 1543 * Allocate one or more key cache slots for a uniacst key. The 1544 * key itself is needed only to identify the cipher. For hardware 1545 * TKIP with split cipher+MIC keys we allocate two key cache slot 1546 * pairs so that we can setup separate TX and RX MIC keys. Note 1547 * that the MIC key for a TKIP key at slot i is assumed by the 1548 * hardware to be at slot i+64. This limits TKIP keys to the first 1549 * 64 entries. 1550 */ 1551 static int 1552 ath_key_alloc(struct ieee80211com *ic, const struct ieee80211_key *k, 1553 ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix) 1554 { 1555 struct ath_softc *sc = ic->ic_ifp->if_softc; 1556 1557 /* 1558 * Group key allocation must be handled specially for 1559 * parts that do not support multicast key cache search 1560 * functionality. For those parts the key id must match 1561 * the h/w key index so lookups find the right key. On 1562 * parts w/ the key search facility we install the sender's 1563 * mac address (with the high bit set) and let the hardware 1564 * find the key w/o using the key id. This is preferred as 1565 * it permits us to support multiple users for adhoc and/or 1566 * multi-station operation. 1567 */ 1568 if ((k->wk_flags & IEEE80211_KEY_GROUP) && !sc->sc_mcastkey) { 1569 if (!(&ic->ic_nw_keys[0] <= k && 1570 k < &ic->ic_nw_keys[IEEE80211_WEP_NKID])) { 1571 /* should not happen */ 1572 DPRINTF(sc, ATH_DEBUG_KEYCACHE, 1573 "%s: bogus group key\n", __func__); 1574 return 0; 1575 } 1576 /* 1577 * XXX we pre-allocate the global keys so 1578 * have no way to check if they've already been allocated. 1579 */ 1580 *keyix = *rxkeyix = k - ic->ic_nw_keys; 1581 return 1; 1582 } 1583 1584 /* 1585 * We allocate two pair for TKIP when using the h/w to do 1586 * the MIC. For everything else, including software crypto, 1587 * we allocate a single entry. Note that s/w crypto requires 1588 * a pass-through slot on the 5211 and 5212. The 5210 does 1589 * not support pass-through cache entries and we map all 1590 * those requests to slot 0. 1591 */ 1592 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) { 1593 return key_alloc_single(sc, keyix, rxkeyix); 1594 } else if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP && 1595 (k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && sc->sc_splitmic) { 1596 return key_alloc_2pair(sc, keyix, rxkeyix); 1597 } else { 1598 return key_alloc_single(sc, keyix, rxkeyix); 1599 } 1600 } 1601 1602 /* 1603 * Delete an entry in the key cache allocated by ath_key_alloc. 1604 */ 1605 static int 1606 ath_key_delete(struct ieee80211com *ic, const struct ieee80211_key *k) 1607 { 1608 struct ath_softc *sc = ic->ic_ifp->if_softc; 1609 struct ath_hal *ah = sc->sc_ah; 1610 const struct ieee80211_cipher *cip = k->wk_cipher; 1611 u_int keyix = k->wk_keyix; 1612 1613 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: delete key %u\n", __func__, keyix); 1614 1615 ath_hal_keyreset(ah, keyix); 1616 /* 1617 * Handle split tx/rx keying required for TKIP with h/w MIC. 1618 */ 1619 if (cip->ic_cipher == IEEE80211_CIPHER_TKIP && 1620 (k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && sc->sc_splitmic) 1621 ath_hal_keyreset(ah, keyix+32); /* RX key */ 1622 if (keyix >= IEEE80211_WEP_NKID) { 1623 /* 1624 * Don't touch keymap entries for global keys so 1625 * they are never considered for dynamic allocation. 1626 */ 1627 clrbit(sc->sc_keymap, keyix); 1628 if (cip->ic_cipher == IEEE80211_CIPHER_TKIP && 1629 (k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && 1630 sc->sc_splitmic) { 1631 clrbit(sc->sc_keymap, keyix+64); /* TX key MIC */ 1632 clrbit(sc->sc_keymap, keyix+32); /* RX key */ 1633 clrbit(sc->sc_keymap, keyix+32+64); /* RX key MIC */ 1634 } 1635 } 1636 return 1; 1637 } 1638 1639 /* 1640 * Set the key cache contents for the specified key. Key cache 1641 * slot(s) must already have been allocated by ath_key_alloc. 1642 */ 1643 static int 1644 ath_key_set(struct ieee80211com *ic, const struct ieee80211_key *k, 1645 const u_int8_t mac[IEEE80211_ADDR_LEN]) 1646 { 1647 struct ath_softc *sc = ic->ic_ifp->if_softc; 1648 1649 return ath_keyset(sc, k, mac, ic->ic_bss); 1650 } 1651 1652 /* 1653 * Block/unblock tx+rx processing while a key change is done. 1654 * We assume the caller serializes key management operations 1655 * so we only need to worry about synchronization with other 1656 * uses that originate in the driver. 1657 */ 1658 static void 1659 ath_key_update_begin(struct ieee80211com *ic) 1660 { 1661 struct ifnet *ifp = ic->ic_ifp; 1662 struct ath_softc *sc = ifp->if_softc; 1663 1664 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__); 1665 #if 0 1666 tasklet_disable(&sc->sc_rxtq); 1667 #endif 1668 IF_LOCK(&ifp->if_snd); /* NB: doesn't block mgmt frames */ 1669 } 1670 1671 static void 1672 ath_key_update_end(struct ieee80211com *ic) 1673 { 1674 struct ifnet *ifp = ic->ic_ifp; 1675 struct ath_softc *sc = ifp->if_softc; 1676 1677 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__); 1678 IF_UNLOCK(&ifp->if_snd); 1679 #if 0 1680 tasklet_enable(&sc->sc_rxtq); 1681 #endif 1682 } 1683 1684 /* 1685 * Calculate the receive filter according to the 1686 * operating mode and state: 1687 * 1688 * o always accept unicast, broadcast, and multicast traffic 1689 * o maintain current state of phy error reception (the hal 1690 * may enable phy error frames for noise immunity work) 1691 * o probe request frames are accepted only when operating in 1692 * hostap, adhoc, or monitor modes 1693 * o enable promiscuous mode according to the interface state 1694 * o accept beacons: 1695 * - when operating in adhoc mode so the 802.11 layer creates 1696 * node table entries for peers, 1697 * - when operating in station mode for collecting rssi data when 1698 * the station is otherwise quiet, or 1699 * - when scanning 1700 */ 1701 static u_int32_t 1702 ath_calcrxfilter(struct ath_softc *sc, enum ieee80211_state state) 1703 { 1704 #define RX_FILTER_PRESERVE (HAL_RX_FILTER_PHYERR | HAL_RX_FILTER_PHYRADAR) 1705 struct ieee80211com *ic = &sc->sc_ic; 1706 struct ath_hal *ah = sc->sc_ah; 1707 struct ifnet *ifp = sc->sc_ifp; 1708 u_int32_t rfilt; 1709 1710 rfilt = (ath_hal_getrxfilter(ah) & RX_FILTER_PRESERVE) 1711 | HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST; 1712 if (ic->ic_opmode != IEEE80211_M_STA) 1713 rfilt |= HAL_RX_FILTER_PROBEREQ; 1714 if (ic->ic_opmode != IEEE80211_M_HOSTAP && 1715 (ifp->if_flags & IFF_PROMISC)) 1716 rfilt |= HAL_RX_FILTER_PROM; 1717 if (ic->ic_opmode == IEEE80211_M_STA || 1718 ic->ic_opmode == IEEE80211_M_IBSS || 1719 state == IEEE80211_S_SCAN) 1720 rfilt |= HAL_RX_FILTER_BEACON; 1721 return rfilt; 1722 #undef RX_FILTER_PRESERVE 1723 } 1724 1725 static void 1726 ath_mode_init(struct ath_softc *sc) 1727 { 1728 struct ieee80211com *ic = &sc->sc_ic; 1729 struct ath_hal *ah = sc->sc_ah; 1730 struct ifnet *ifp = sc->sc_ifp; 1731 u_int32_t rfilt, mfilt[2], val; 1732 u_int8_t pos; 1733 struct ifmultiaddr *ifma; 1734 1735 /* configure rx filter */ 1736 rfilt = ath_calcrxfilter(sc, ic->ic_state); 1737 ath_hal_setrxfilter(ah, rfilt); 1738 1739 /* configure operational mode */ 1740 ath_hal_setopmode(ah); 1741 1742 /* 1743 * Handle any link-level address change. Note that we only 1744 * need to force ic_myaddr; any other addresses are handled 1745 * as a byproduct of the ifnet code marking the interface 1746 * down then up. 1747 * 1748 * XXX should get from lladdr instead of arpcom but that's more work 1749 */ 1750 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp)); 1751 ath_hal_setmac(ah, ic->ic_myaddr); 1752 1753 /* calculate and install multicast filter */ 1754 if ((ifp->if_flags & IFF_ALLMULTI) == 0) { 1755 mfilt[0] = mfilt[1] = 0; 1756 IF_ADDR_LOCK(ifp); 1757 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1758 caddr_t dl; 1759 1760 /* calculate XOR of eight 6bit values */ 1761 dl = LLADDR((struct sockaddr_dl *) ifma->ifma_addr); 1762 val = LE_READ_4(dl + 0); 1763 pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val; 1764 val = LE_READ_4(dl + 3); 1765 pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val; 1766 pos &= 0x3f; 1767 mfilt[pos / 32] |= (1 << (pos % 32)); 1768 } 1769 IF_ADDR_UNLOCK(ifp); 1770 } else { 1771 mfilt[0] = mfilt[1] = ~0; 1772 } 1773 ath_hal_setmcastfilter(ah, mfilt[0], mfilt[1]); 1774 DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x, MC filter %08x:%08x\n", 1775 __func__, rfilt, mfilt[0], mfilt[1]); 1776 } 1777 1778 /* 1779 * Set the slot time based on the current setting. 1780 */ 1781 static void 1782 ath_setslottime(struct ath_softc *sc) 1783 { 1784 struct ieee80211com *ic = &sc->sc_ic; 1785 struct ath_hal *ah = sc->sc_ah; 1786 1787 if (ic->ic_flags & IEEE80211_F_SHSLOT) 1788 ath_hal_setslottime(ah, HAL_SLOT_TIME_9); 1789 else 1790 ath_hal_setslottime(ah, HAL_SLOT_TIME_20); 1791 sc->sc_updateslot = OK; 1792 } 1793 1794 /* 1795 * Callback from the 802.11 layer to update the 1796 * slot time based on the current setting. 1797 */ 1798 static void 1799 ath_updateslot(struct ifnet *ifp) 1800 { 1801 struct ath_softc *sc = ifp->if_softc; 1802 struct ieee80211com *ic = &sc->sc_ic; 1803 1804 /* 1805 * When not coordinating the BSS, change the hardware 1806 * immediately. For other operation we defer the change 1807 * until beacon updates have propagated to the stations. 1808 */ 1809 if (ic->ic_opmode == IEEE80211_M_HOSTAP) 1810 sc->sc_updateslot = UPDATE; 1811 else 1812 ath_setslottime(sc); 1813 } 1814 1815 /* 1816 * Setup a h/w transmit queue for beacons. 1817 */ 1818 static int 1819 ath_beaconq_setup(struct ath_hal *ah) 1820 { 1821 HAL_TXQ_INFO qi; 1822 1823 memset(&qi, 0, sizeof(qi)); 1824 qi.tqi_aifs = HAL_TXQ_USEDEFAULT; 1825 qi.tqi_cwmin = HAL_TXQ_USEDEFAULT; 1826 qi.tqi_cwmax = HAL_TXQ_USEDEFAULT; 1827 /* NB: for dynamic turbo, don't enable any other interrupts */ 1828 qi.tqi_qflags = HAL_TXQ_TXDESCINT_ENABLE; 1829 return ath_hal_setuptxqueue(ah, HAL_TX_QUEUE_BEACON, &qi); 1830 } 1831 1832 /* 1833 * Setup the transmit queue parameters for the beacon queue. 1834 */ 1835 static int 1836 ath_beaconq_config(struct ath_softc *sc) 1837 { 1838 #define ATH_EXPONENT_TO_VALUE(v) ((1<<(v))-1) 1839 struct ieee80211com *ic = &sc->sc_ic; 1840 struct ath_hal *ah = sc->sc_ah; 1841 HAL_TXQ_INFO qi; 1842 1843 ath_hal_gettxqueueprops(ah, sc->sc_bhalq, &qi); 1844 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 1845 /* 1846 * Always burst out beacon and CAB traffic. 1847 */ 1848 qi.tqi_aifs = ATH_BEACON_AIFS_DEFAULT; 1849 qi.tqi_cwmin = ATH_BEACON_CWMIN_DEFAULT; 1850 qi.tqi_cwmax = ATH_BEACON_CWMAX_DEFAULT; 1851 } else { 1852 struct wmeParams *wmep = 1853 &ic->ic_wme.wme_chanParams.cap_wmeParams[WME_AC_BE]; 1854 /* 1855 * Adhoc mode; important thing is to use 2x cwmin. 1856 */ 1857 qi.tqi_aifs = wmep->wmep_aifsn; 1858 qi.tqi_cwmin = 2*ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin); 1859 qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax); 1860 } 1861 1862 if (!ath_hal_settxqueueprops(ah, sc->sc_bhalq, &qi)) { 1863 device_printf(sc->sc_dev, "unable to update parameters for " 1864 "beacon hardware queue!\n"); 1865 return 0; 1866 } else { 1867 ath_hal_resettxqueue(ah, sc->sc_bhalq); /* push to h/w */ 1868 return 1; 1869 } 1870 #undef ATH_EXPONENT_TO_VALUE 1871 } 1872 1873 /* 1874 * Allocate and setup an initial beacon frame. 1875 */ 1876 static int 1877 ath_beacon_alloc(struct ath_softc *sc, struct ieee80211_node *ni) 1878 { 1879 struct ieee80211com *ic = ni->ni_ic; 1880 struct ath_buf *bf; 1881 struct mbuf *m; 1882 int error; 1883 1884 bf = STAILQ_FIRST(&sc->sc_bbuf); 1885 if (bf == NULL) { 1886 DPRINTF(sc, ATH_DEBUG_BEACON, "%s: no dma buffers\n", __func__); 1887 sc->sc_stats.ast_be_nombuf++; /* XXX */ 1888 return ENOMEM; /* XXX */ 1889 } 1890 /* 1891 * NB: the beacon data buffer must be 32-bit aligned; 1892 * we assume the mbuf routines will return us something 1893 * with this alignment (perhaps should assert). 1894 */ 1895 m = ieee80211_beacon_alloc(ic, ni, &sc->sc_boff); 1896 if (m == NULL) { 1897 DPRINTF(sc, ATH_DEBUG_BEACON, "%s: cannot get mbuf\n", 1898 __func__); 1899 sc->sc_stats.ast_be_nombuf++; 1900 return ENOMEM; 1901 } 1902 error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m, 1903 bf->bf_segs, &bf->bf_nseg, 1904 BUS_DMA_NOWAIT); 1905 if (error == 0) { 1906 bf->bf_m = m; 1907 bf->bf_node = ieee80211_ref_node(ni); 1908 } else { 1909 m_freem(m); 1910 } 1911 return error; 1912 } 1913 1914 /* 1915 * Setup the beacon frame for transmit. 1916 */ 1917 static void 1918 ath_beacon_setup(struct ath_softc *sc, struct ath_buf *bf) 1919 { 1920 #define USE_SHPREAMBLE(_ic) \ 1921 (((_ic)->ic_flags & (IEEE80211_F_SHPREAMBLE | IEEE80211_F_USEBARKER))\ 1922 == IEEE80211_F_SHPREAMBLE) 1923 struct ieee80211_node *ni = bf->bf_node; 1924 struct ieee80211com *ic = ni->ni_ic; 1925 struct mbuf *m = bf->bf_m; 1926 struct ath_hal *ah = sc->sc_ah; 1927 struct ath_desc *ds; 1928 int flags, antenna; 1929 const HAL_RATE_TABLE *rt; 1930 u_int8_t rix, rate; 1931 1932 DPRINTF(sc, ATH_DEBUG_BEACON_PROC, "%s: m %p len %u\n", 1933 __func__, m, m->m_len); 1934 1935 /* setup descriptors */ 1936 ds = bf->bf_desc; 1937 1938 flags = HAL_TXDESC_NOACK; 1939 if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_hasveol) { 1940 ds->ds_link = bf->bf_daddr; /* self-linked */ 1941 flags |= HAL_TXDESC_VEOL; 1942 /* 1943 * Let hardware handle antenna switching. 1944 */ 1945 antenna = sc->sc_txantenna; 1946 } else { 1947 ds->ds_link = 0; 1948 /* 1949 * Switch antenna every 4 beacons. 1950 * XXX assumes two antenna 1951 */ 1952 antenna = sc->sc_txantenna != 0 ? sc->sc_txantenna 1953 : (sc->sc_stats.ast_be_xmit & 4 ? 2 : 1); 1954 } 1955 1956 KASSERT(bf->bf_nseg == 1, 1957 ("multi-segment beacon frame; nseg %u", bf->bf_nseg)); 1958 ds->ds_data = bf->bf_segs[0].ds_addr; 1959 /* 1960 * Calculate rate code. 1961 * XXX everything at min xmit rate 1962 */ 1963 rix = sc->sc_minrateix; 1964 rt = sc->sc_currates; 1965 rate = rt->info[rix].rateCode; 1966 if (USE_SHPREAMBLE(ic)) 1967 rate |= rt->info[rix].shortPreamble; 1968 ath_hal_setuptxdesc(ah, ds 1969 , m->m_len + IEEE80211_CRC_LEN /* frame length */ 1970 , sizeof(struct ieee80211_frame)/* header length */ 1971 , HAL_PKT_TYPE_BEACON /* Atheros packet type */ 1972 , ni->ni_txpower /* txpower XXX */ 1973 , rate, 1 /* series 0 rate/tries */ 1974 , HAL_TXKEYIX_INVALID /* no encryption */ 1975 , antenna /* antenna mode */ 1976 , flags /* no ack, veol for beacons */ 1977 , 0 /* rts/cts rate */ 1978 , 0 /* rts/cts duration */ 1979 ); 1980 /* NB: beacon's BufLen must be a multiple of 4 bytes */ 1981 ath_hal_filltxdesc(ah, ds 1982 , roundup(m->m_len, 4) /* buffer length */ 1983 , AH_TRUE /* first segment */ 1984 , AH_TRUE /* last segment */ 1985 , ds /* first descriptor */ 1986 ); 1987 #undef USE_SHPREAMBLE 1988 } 1989 1990 /* 1991 * Transmit a beacon frame at SWBA. Dynamic updates to the 1992 * frame contents are done as needed and the slot time is 1993 * also adjusted based on current state. 1994 */ 1995 static void 1996 ath_beacon_proc(void *arg, int pending) 1997 { 1998 struct ath_softc *sc = arg; 1999 struct ath_buf *bf = STAILQ_FIRST(&sc->sc_bbuf); 2000 struct ieee80211_node *ni = bf->bf_node; 2001 struct ieee80211com *ic = ni->ni_ic; 2002 struct ath_hal *ah = sc->sc_ah; 2003 struct mbuf *m; 2004 int ncabq, error, otherant; 2005 2006 DPRINTF(sc, ATH_DEBUG_BEACON_PROC, "%s: pending %u\n", 2007 __func__, pending); 2008 2009 if (ic->ic_opmode == IEEE80211_M_STA || 2010 ic->ic_opmode == IEEE80211_M_MONITOR || 2011 bf == NULL || bf->bf_m == NULL) { 2012 DPRINTF(sc, ATH_DEBUG_ANY, "%s: ic_flags=%x bf=%p bf_m=%p\n", 2013 __func__, ic->ic_flags, bf, bf ? bf->bf_m : NULL); 2014 return; 2015 } 2016 /* 2017 * Check if the previous beacon has gone out. If 2018 * not don't try to post another, skip this period 2019 * and wait for the next. Missed beacons indicate 2020 * a problem and should not occur. If we miss too 2021 * many consecutive beacons reset the device. 2022 */ 2023 if (ath_hal_numtxpending(ah, sc->sc_bhalq) != 0) { 2024 sc->sc_bmisscount++; 2025 DPRINTF(sc, ATH_DEBUG_BEACON, 2026 "%s: missed %u consecutive beacons\n", 2027 __func__, sc->sc_bmisscount); 2028 if (sc->sc_bmisscount > 3) /* NB: 3 is a guess */ 2029 taskqueue_enqueue(sc->sc_tq, &sc->sc_bstucktask); 2030 return; 2031 } 2032 if (sc->sc_bmisscount != 0) { 2033 DPRINTF(sc, ATH_DEBUG_BEACON, 2034 "%s: resume beacon xmit after %u misses\n", 2035 __func__, sc->sc_bmisscount); 2036 sc->sc_bmisscount = 0; 2037 } 2038 2039 /* 2040 * Update dynamic beacon contents. If this returns 2041 * non-zero then we need to remap the memory because 2042 * the beacon frame changed size (probably because 2043 * of the TIM bitmap). 2044 */ 2045 m = bf->bf_m; 2046 ncabq = sc->sc_cabq->axq_depth; 2047 if (ieee80211_beacon_update(ic, bf->bf_node, &sc->sc_boff, m, ncabq)) { 2048 /* XXX too conservative? */ 2049 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 2050 error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m, 2051 bf->bf_segs, &bf->bf_nseg, 2052 BUS_DMA_NOWAIT); 2053 if (error != 0) { 2054 if_printf(ic->ic_ifp, 2055 "%s: bus_dmamap_load_mbuf_sg failed, error %u\n", 2056 __func__, error); 2057 return; 2058 } 2059 } 2060 2061 /* 2062 * Handle slot time change when a non-ERP station joins/leaves 2063 * an 11g network. The 802.11 layer notifies us via callback, 2064 * we mark updateslot, then wait one beacon before effecting 2065 * the change. This gives associated stations at least one 2066 * beacon interval to note the state change. 2067 */ 2068 /* XXX locking */ 2069 if (sc->sc_updateslot == UPDATE) 2070 sc->sc_updateslot = COMMIT; /* commit next beacon */ 2071 else if (sc->sc_updateslot == COMMIT) 2072 ath_setslottime(sc); /* commit change to h/w */ 2073 2074 /* 2075 * Check recent per-antenna transmit statistics and flip 2076 * the default antenna if noticeably more frames went out 2077 * on the non-default antenna. 2078 * XXX assumes 2 anntenae 2079 */ 2080 otherant = sc->sc_defant & 1 ? 2 : 1; 2081 if (sc->sc_ant_tx[otherant] > sc->sc_ant_tx[sc->sc_defant] + 2) 2082 ath_setdefantenna(sc, otherant); 2083 sc->sc_ant_tx[1] = sc->sc_ant_tx[2] = 0; 2084 2085 /* 2086 * Construct tx descriptor. 2087 */ 2088 ath_beacon_setup(sc, bf); 2089 2090 /* 2091 * Stop any current dma and put the new frame on the queue. 2092 * This should never fail since we check above that no frames 2093 * are still pending on the queue. 2094 */ 2095 if (!ath_hal_stoptxdma(ah, sc->sc_bhalq)) { 2096 DPRINTF(sc, ATH_DEBUG_ANY, 2097 "%s: beacon queue %u did not stop?\n", 2098 __func__, sc->sc_bhalq); 2099 } 2100 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE); 2101 2102 /* 2103 * Enable the CAB queue before the beacon queue to 2104 * insure cab frames are triggered by this beacon. 2105 */ 2106 if (sc->sc_boff.bo_tim[4] & 1) /* NB: only at DTIM */ 2107 ath_hal_txstart(ah, sc->sc_cabq->axq_qnum); 2108 ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr); 2109 ath_hal_txstart(ah, sc->sc_bhalq); 2110 DPRINTF(sc, ATH_DEBUG_BEACON_PROC, 2111 "%s: TXDP[%u] = %p (%p)\n", __func__, 2112 sc->sc_bhalq, (caddr_t)bf->bf_daddr, bf->bf_desc); 2113 2114 sc->sc_stats.ast_be_xmit++; 2115 } 2116 2117 /* 2118 * Reset the hardware after detecting beacons have stopped. 2119 */ 2120 static void 2121 ath_bstuck_proc(void *arg, int pending) 2122 { 2123 struct ath_softc *sc = arg; 2124 struct ifnet *ifp = sc->sc_ifp; 2125 2126 if_printf(ifp, "stuck beacon; resetting (bmiss count %u)\n", 2127 sc->sc_bmisscount); 2128 ath_reset(ifp); 2129 } 2130 2131 /* 2132 * Reclaim beacon resources. 2133 */ 2134 static void 2135 ath_beacon_free(struct ath_softc *sc) 2136 { 2137 struct ath_buf *bf; 2138 2139 STAILQ_FOREACH(bf, &sc->sc_bbuf, bf_list) { 2140 if (bf->bf_m != NULL) { 2141 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 2142 m_freem(bf->bf_m); 2143 bf->bf_m = NULL; 2144 } 2145 if (bf->bf_node != NULL) { 2146 ieee80211_free_node(bf->bf_node); 2147 bf->bf_node = NULL; 2148 } 2149 } 2150 } 2151 2152 /* 2153 * Configure the beacon and sleep timers. 2154 * 2155 * When operating as an AP this resets the TSF and sets 2156 * up the hardware to notify us when we need to issue beacons. 2157 * 2158 * When operating in station mode this sets up the beacon 2159 * timers according to the timestamp of the last received 2160 * beacon and the current TSF, configures PCF and DTIM 2161 * handling, programs the sleep registers so the hardware 2162 * will wakeup in time to receive beacons, and configures 2163 * the beacon miss handling so we'll receive a BMISS 2164 * interrupt when we stop seeing beacons from the AP 2165 * we've associated with. 2166 */ 2167 static void 2168 ath_beacon_config(struct ath_softc *sc) 2169 { 2170 #define TSF_TO_TU(_h,_l) \ 2171 ((((u_int32_t)(_h)) << 22) | (((u_int32_t)(_l)) >> 10)) 2172 #define FUDGE 2 2173 struct ath_hal *ah = sc->sc_ah; 2174 struct ieee80211com *ic = &sc->sc_ic; 2175 struct ieee80211_node *ni = ic->ic_bss; 2176 u_int32_t nexttbtt, intval, tsftu; 2177 u_int64_t tsf; 2178 2179 /* extract tstamp from last beacon and convert to TU */ 2180 nexttbtt = TSF_TO_TU(LE_READ_4(ni->ni_tstamp.data + 4), 2181 LE_READ_4(ni->ni_tstamp.data)); 2182 /* NB: the beacon interval is kept internally in TU's */ 2183 intval = ni->ni_intval & HAL_BEACON_PERIOD; 2184 if (nexttbtt == 0) /* e.g. for ap mode */ 2185 nexttbtt = intval; 2186 else if (intval) /* NB: can be 0 for monitor mode */ 2187 nexttbtt = roundup(nexttbtt, intval); 2188 DPRINTF(sc, ATH_DEBUG_BEACON, "%s: nexttbtt %u intval %u (%u)\n", 2189 __func__, nexttbtt, intval, ni->ni_intval); 2190 if (ic->ic_opmode == IEEE80211_M_STA) { 2191 HAL_BEACON_STATE bs; 2192 int dtimperiod, dtimcount; 2193 int cfpperiod, cfpcount; 2194 2195 /* 2196 * Setup dtim and cfp parameters according to 2197 * last beacon we received (which may be none). 2198 */ 2199 dtimperiod = ni->ni_dtim_period; 2200 if (dtimperiod <= 0) /* NB: 0 if not known */ 2201 dtimperiod = 1; 2202 dtimcount = ni->ni_dtim_count; 2203 if (dtimcount >= dtimperiod) /* NB: sanity check */ 2204 dtimcount = 0; /* XXX? */ 2205 cfpperiod = 1; /* NB: no PCF support yet */ 2206 cfpcount = 0; 2207 /* 2208 * Pull nexttbtt forward to reflect the current 2209 * TSF and calculate dtim+cfp state for the result. 2210 */ 2211 tsf = ath_hal_gettsf64(ah); 2212 tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE; 2213 do { 2214 nexttbtt += intval; 2215 if (--dtimcount < 0) { 2216 dtimcount = dtimperiod - 1; 2217 if (--cfpcount < 0) 2218 cfpcount = cfpperiod - 1; 2219 } 2220 } while (nexttbtt < tsftu); 2221 memset(&bs, 0, sizeof(bs)); 2222 bs.bs_intval = intval; 2223 bs.bs_nexttbtt = nexttbtt; 2224 bs.bs_dtimperiod = dtimperiod*intval; 2225 bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval; 2226 bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod; 2227 bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod; 2228 bs.bs_cfpmaxduration = 0; 2229 #if 0 2230 /* 2231 * The 802.11 layer records the offset to the DTIM 2232 * bitmap while receiving beacons; use it here to 2233 * enable h/w detection of our AID being marked in 2234 * the bitmap vector (to indicate frames for us are 2235 * pending at the AP). 2236 * XXX do DTIM handling in s/w to WAR old h/w bugs 2237 * XXX enable based on h/w rev for newer chips 2238 */ 2239 bs.bs_timoffset = ni->ni_timoff; 2240 #endif 2241 /* 2242 * Calculate the number of consecutive beacons to miss 2243 * before taking a BMISS interrupt. The configuration 2244 * is specified in ms, so we need to convert that to 2245 * TU's and then calculate based on the beacon interval. 2246 * Note that we clamp the result to at most 10 beacons. 2247 */ 2248 bs.bs_bmissthreshold = ic->ic_bmissthreshold; 2249 if (bs.bs_bmissthreshold > 10) 2250 bs.bs_bmissthreshold = 10; 2251 else if (bs.bs_bmissthreshold <= 0) 2252 bs.bs_bmissthreshold = 1; 2253 2254 /* 2255 * Calculate sleep duration. The configuration is 2256 * given in ms. We insure a multiple of the beacon 2257 * period is used. Also, if the sleep duration is 2258 * greater than the DTIM period then it makes senses 2259 * to make it a multiple of that. 2260 * 2261 * XXX fixed at 100ms 2262 */ 2263 bs.bs_sleepduration = 2264 roundup(IEEE80211_MS_TO_TU(100), bs.bs_intval); 2265 if (bs.bs_sleepduration > bs.bs_dtimperiod) 2266 bs.bs_sleepduration = roundup(bs.bs_sleepduration, bs.bs_dtimperiod); 2267 2268 DPRINTF(sc, ATH_DEBUG_BEACON, 2269 "%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" 2270 , __func__ 2271 , tsf, tsftu 2272 , bs.bs_intval 2273 , bs.bs_nexttbtt 2274 , bs.bs_dtimperiod 2275 , bs.bs_nextdtim 2276 , bs.bs_bmissthreshold 2277 , bs.bs_sleepduration 2278 , bs.bs_cfpperiod 2279 , bs.bs_cfpmaxduration 2280 , bs.bs_cfpnext 2281 , bs.bs_timoffset 2282 ); 2283 ath_hal_intrset(ah, 0); 2284 ath_hal_beacontimers(ah, &bs); 2285 sc->sc_imask |= HAL_INT_BMISS; 2286 ath_hal_intrset(ah, sc->sc_imask); 2287 } else { 2288 ath_hal_intrset(ah, 0); 2289 if (nexttbtt == intval) 2290 intval |= HAL_BEACON_RESET_TSF; 2291 if (ic->ic_opmode == IEEE80211_M_IBSS) { 2292 /* 2293 * In IBSS mode enable the beacon timers but only 2294 * enable SWBA interrupts if we need to manually 2295 * prepare beacon frames. Otherwise we use a 2296 * self-linked tx descriptor and let the hardware 2297 * deal with things. 2298 */ 2299 intval |= HAL_BEACON_ENA; 2300 if (!sc->sc_hasveol) 2301 sc->sc_imask |= HAL_INT_SWBA; 2302 if ((intval & HAL_BEACON_RESET_TSF) == 0) { 2303 /* 2304 * Pull nexttbtt forward to reflect 2305 * the current TSF. 2306 */ 2307 tsf = ath_hal_gettsf64(ah); 2308 tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE; 2309 do { 2310 nexttbtt += intval; 2311 } while (nexttbtt < tsftu); 2312 } 2313 ath_beaconq_config(sc); 2314 } else if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 2315 /* 2316 * In AP mode we enable the beacon timers and 2317 * SWBA interrupts to prepare beacon frames. 2318 */ 2319 intval |= HAL_BEACON_ENA; 2320 sc->sc_imask |= HAL_INT_SWBA; /* beacon prepare */ 2321 ath_beaconq_config(sc); 2322 } 2323 ath_hal_beaconinit(ah, nexttbtt, intval); 2324 sc->sc_bmisscount = 0; 2325 ath_hal_intrset(ah, sc->sc_imask); 2326 /* 2327 * When using a self-linked beacon descriptor in 2328 * ibss mode load it once here. 2329 */ 2330 if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_hasveol) 2331 ath_beacon_proc(sc, 0); 2332 } 2333 sc->sc_syncbeacon = 0; 2334 #undef FUDGE 2335 #undef TSF_TO_TU 2336 } 2337 2338 static void 2339 ath_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 2340 { 2341 bus_addr_t *paddr = (bus_addr_t*) arg; 2342 KASSERT(error == 0, ("error %u on bus_dma callback", error)); 2343 *paddr = segs->ds_addr; 2344 } 2345 2346 static int 2347 ath_descdma_setup(struct ath_softc *sc, 2348 struct ath_descdma *dd, ath_bufhead *head, 2349 const char *name, int nbuf, int ndesc) 2350 { 2351 #define DS2PHYS(_dd, _ds) \ 2352 ((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc)) 2353 struct ifnet *ifp = sc->sc_ifp; 2354 struct ath_desc *ds; 2355 struct ath_buf *bf; 2356 int i, bsize, error; 2357 2358 DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA: %u buffers %u desc/buf\n", 2359 __func__, name, nbuf, ndesc); 2360 2361 dd->dd_name = name; 2362 dd->dd_desc_len = sizeof(struct ath_desc) * nbuf * ndesc; 2363 2364 /* 2365 * Setup DMA descriptor area. 2366 */ 2367 error = bus_dma_tag_create(NULL, /* parent */ 2368 PAGE_SIZE, 0, /* alignment, bounds */ 2369 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 2370 BUS_SPACE_MAXADDR, /* highaddr */ 2371 NULL, NULL, /* filter, filterarg */ 2372 dd->dd_desc_len, /* maxsize */ 2373 1, /* nsegments */ 2374 dd->dd_desc_len, /* maxsegsize */ 2375 BUS_DMA_ALLOCNOW, /* flags */ 2376 NULL, /* lockfunc */ 2377 NULL, /* lockarg */ 2378 &dd->dd_dmat); 2379 if (error != 0) { 2380 if_printf(ifp, "cannot allocate %s DMA tag\n", dd->dd_name); 2381 return error; 2382 } 2383 2384 /* allocate descriptors */ 2385 error = bus_dmamap_create(dd->dd_dmat, BUS_DMA_NOWAIT, &dd->dd_dmamap); 2386 if (error != 0) { 2387 if_printf(ifp, "unable to create dmamap for %s descriptors, " 2388 "error %u\n", dd->dd_name, error); 2389 goto fail0; 2390 } 2391 2392 error = bus_dmamem_alloc(dd->dd_dmat, (void**) &dd->dd_desc, 2393 BUS_DMA_NOWAIT, &dd->dd_dmamap); 2394 if (error != 0) { 2395 if_printf(ifp, "unable to alloc memory for %u %s descriptors, " 2396 "error %u\n", nbuf * ndesc, dd->dd_name, error); 2397 goto fail1; 2398 } 2399 2400 error = bus_dmamap_load(dd->dd_dmat, dd->dd_dmamap, 2401 dd->dd_desc, dd->dd_desc_len, 2402 ath_load_cb, &dd->dd_desc_paddr, 2403 BUS_DMA_NOWAIT); 2404 if (error != 0) { 2405 if_printf(ifp, "unable to map %s descriptors, error %u\n", 2406 dd->dd_name, error); 2407 goto fail2; 2408 } 2409 2410 ds = dd->dd_desc; 2411 DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA map: %p (%lu) -> %p (%lu)\n", 2412 __func__, dd->dd_name, ds, (u_long) dd->dd_desc_len, 2413 (caddr_t) dd->dd_desc_paddr, /*XXX*/ (u_long) dd->dd_desc_len); 2414 2415 /* allocate rx buffers */ 2416 bsize = sizeof(struct ath_buf) * nbuf; 2417 bf = malloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO); 2418 if (bf == NULL) { 2419 if_printf(ifp, "malloc of %s buffers failed, size %u\n", 2420 dd->dd_name, bsize); 2421 goto fail3; 2422 } 2423 dd->dd_bufptr = bf; 2424 2425 STAILQ_INIT(head); 2426 for (i = 0; i < nbuf; i++, bf++, ds += ndesc) { 2427 bf->bf_desc = ds; 2428 bf->bf_daddr = DS2PHYS(dd, ds); 2429 error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT, 2430 &bf->bf_dmamap); 2431 if (error != 0) { 2432 if_printf(ifp, "unable to create dmamap for %s " 2433 "buffer %u, error %u\n", dd->dd_name, i, error); 2434 ath_descdma_cleanup(sc, dd, head); 2435 return error; 2436 } 2437 STAILQ_INSERT_TAIL(head, bf, bf_list); 2438 } 2439 return 0; 2440 fail3: 2441 bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap); 2442 fail2: 2443 bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap); 2444 fail1: 2445 bus_dmamap_destroy(dd->dd_dmat, dd->dd_dmamap); 2446 fail0: 2447 bus_dma_tag_destroy(dd->dd_dmat); 2448 memset(dd, 0, sizeof(*dd)); 2449 return error; 2450 #undef DS2PHYS 2451 } 2452 2453 static void 2454 ath_descdma_cleanup(struct ath_softc *sc, 2455 struct ath_descdma *dd, ath_bufhead *head) 2456 { 2457 struct ath_buf *bf; 2458 struct ieee80211_node *ni; 2459 2460 bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap); 2461 bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap); 2462 bus_dmamap_destroy(dd->dd_dmat, dd->dd_dmamap); 2463 bus_dma_tag_destroy(dd->dd_dmat); 2464 2465 STAILQ_FOREACH(bf, head, bf_list) { 2466 if (bf->bf_m) { 2467 m_freem(bf->bf_m); 2468 bf->bf_m = NULL; 2469 } 2470 if (bf->bf_dmamap != NULL) { 2471 bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap); 2472 bf->bf_dmamap = NULL; 2473 } 2474 ni = bf->bf_node; 2475 bf->bf_node = NULL; 2476 if (ni != NULL) { 2477 /* 2478 * Reclaim node reference. 2479 */ 2480 ieee80211_free_node(ni); 2481 } 2482 } 2483 2484 STAILQ_INIT(head); 2485 free(dd->dd_bufptr, M_ATHDEV); 2486 memset(dd, 0, sizeof(*dd)); 2487 } 2488 2489 static int 2490 ath_desc_alloc(struct ath_softc *sc) 2491 { 2492 int error; 2493 2494 error = ath_descdma_setup(sc, &sc->sc_rxdma, &sc->sc_rxbuf, 2495 "rx", ath_rxbuf, 1); 2496 if (error != 0) 2497 return error; 2498 2499 error = ath_descdma_setup(sc, &sc->sc_txdma, &sc->sc_txbuf, 2500 "tx", ath_txbuf, ATH_TXDESC); 2501 if (error != 0) { 2502 ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf); 2503 return error; 2504 } 2505 2506 error = ath_descdma_setup(sc, &sc->sc_bdma, &sc->sc_bbuf, 2507 "beacon", 1, 1); 2508 if (error != 0) { 2509 ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf); 2510 ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf); 2511 return error; 2512 } 2513 return 0; 2514 } 2515 2516 static void 2517 ath_desc_free(struct ath_softc *sc) 2518 { 2519 2520 if (sc->sc_bdma.dd_desc_len != 0) 2521 ath_descdma_cleanup(sc, &sc->sc_bdma, &sc->sc_bbuf); 2522 if (sc->sc_txdma.dd_desc_len != 0) 2523 ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf); 2524 if (sc->sc_rxdma.dd_desc_len != 0) 2525 ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf); 2526 } 2527 2528 static struct ieee80211_node * 2529 ath_node_alloc(struct ieee80211_node_table *nt) 2530 { 2531 struct ieee80211com *ic = nt->nt_ic; 2532 struct ath_softc *sc = ic->ic_ifp->if_softc; 2533 const size_t space = sizeof(struct ath_node) + sc->sc_rc->arc_space; 2534 struct ath_node *an; 2535 2536 an = malloc(space, M_80211_NODE, M_NOWAIT|M_ZERO); 2537 if (an == NULL) { 2538 /* XXX stat+msg */ 2539 return NULL; 2540 } 2541 an->an_avgrssi = ATH_RSSI_DUMMY_MARKER; 2542 ath_rate_node_init(sc, an); 2543 2544 DPRINTF(sc, ATH_DEBUG_NODE, "%s: an %p\n", __func__, an); 2545 return &an->an_node; 2546 } 2547 2548 static void 2549 ath_node_free(struct ieee80211_node *ni) 2550 { 2551 struct ieee80211com *ic = ni->ni_ic; 2552 struct ath_softc *sc = ic->ic_ifp->if_softc; 2553 2554 DPRINTF(sc, ATH_DEBUG_NODE, "%s: ni %p\n", __func__, ni); 2555 2556 ath_rate_node_cleanup(sc, ATH_NODE(ni)); 2557 sc->sc_node_free(ni); 2558 } 2559 2560 static u_int8_t 2561 ath_node_getrssi(const struct ieee80211_node *ni) 2562 { 2563 #define HAL_EP_RND(x, mul) \ 2564 ((((x)%(mul)) >= ((mul)/2)) ? ((x) + ((mul) - 1)) / (mul) : (x)/(mul)) 2565 u_int32_t avgrssi = ATH_NODE_CONST(ni)->an_avgrssi; 2566 int32_t rssi; 2567 2568 /* 2569 * When only one frame is received there will be no state in 2570 * avgrssi so fallback on the value recorded by the 802.11 layer. 2571 */ 2572 if (avgrssi != ATH_RSSI_DUMMY_MARKER) 2573 rssi = HAL_EP_RND(avgrssi, HAL_RSSI_EP_MULTIPLIER); 2574 else 2575 rssi = ni->ni_rssi; 2576 return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi; 2577 #undef HAL_EP_RND 2578 } 2579 2580 static int 2581 ath_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf) 2582 { 2583 struct ath_hal *ah = sc->sc_ah; 2584 int error; 2585 struct mbuf *m; 2586 struct ath_desc *ds; 2587 2588 m = bf->bf_m; 2589 if (m == NULL) { 2590 /* 2591 * NB: by assigning a page to the rx dma buffer we 2592 * implicitly satisfy the Atheros requirement that 2593 * this buffer be cache-line-aligned and sized to be 2594 * multiple of the cache line size. Not doing this 2595 * causes weird stuff to happen (for the 5210 at least). 2596 */ 2597 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 2598 if (m == NULL) { 2599 DPRINTF(sc, ATH_DEBUG_ANY, 2600 "%s: no mbuf/cluster\n", __func__); 2601 sc->sc_stats.ast_rx_nombuf++; 2602 return ENOMEM; 2603 } 2604 m->m_pkthdr.len = m->m_len = m->m_ext.ext_size; 2605 2606 error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, 2607 bf->bf_dmamap, m, 2608 bf->bf_segs, &bf->bf_nseg, 2609 BUS_DMA_NOWAIT); 2610 if (error != 0) { 2611 DPRINTF(sc, ATH_DEBUG_ANY, 2612 "%s: bus_dmamap_load_mbuf_sg failed; error %d\n", 2613 __func__, error); 2614 sc->sc_stats.ast_rx_busdma++; 2615 m_freem(m); 2616 return error; 2617 } 2618 KASSERT(bf->bf_nseg == 1, 2619 ("multi-segment packet; nseg %u", bf->bf_nseg)); 2620 bf->bf_m = m; 2621 } 2622 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREREAD); 2623 2624 /* 2625 * Setup descriptors. For receive we always terminate 2626 * the descriptor list with a self-linked entry so we'll 2627 * not get overrun under high load (as can happen with a 2628 * 5212 when ANI processing enables PHY error frames). 2629 * 2630 * To insure the last descriptor is self-linked we create 2631 * each descriptor as self-linked and add it to the end. As 2632 * each additional descriptor is added the previous self-linked 2633 * entry is ``fixed'' naturally. This should be safe even 2634 * if DMA is happening. When processing RX interrupts we 2635 * never remove/process the last, self-linked, entry on the 2636 * descriptor list. This insures the hardware always has 2637 * someplace to write a new frame. 2638 */ 2639 ds = bf->bf_desc; 2640 ds->ds_link = bf->bf_daddr; /* link to self */ 2641 ds->ds_data = bf->bf_segs[0].ds_addr; 2642 ds->ds_vdata = mtod(m, void *); /* for radar */ 2643 ath_hal_setuprxdesc(ah, ds 2644 , m->m_len /* buffer size */ 2645 , 0 2646 ); 2647 2648 if (sc->sc_rxlink != NULL) 2649 *sc->sc_rxlink = bf->bf_daddr; 2650 sc->sc_rxlink = &ds->ds_link; 2651 return 0; 2652 } 2653 2654 /* 2655 * Extend 15-bit time stamp from rx descriptor to 2656 * a full 64-bit TSF using the specified TSF. 2657 */ 2658 static __inline u_int64_t 2659 ath_extend_tsf(u_int32_t rstamp, u_int64_t tsf) 2660 { 2661 if ((tsf & 0x7fff) < rstamp) 2662 tsf -= 0x8000; 2663 return ((tsf &~ 0x7fff) | rstamp); 2664 } 2665 2666 /* 2667 * Intercept management frames to collect beacon rssi data 2668 * and to do ibss merges. 2669 */ 2670 static void 2671 ath_recv_mgmt(struct ieee80211com *ic, struct mbuf *m, 2672 struct ieee80211_node *ni, 2673 int subtype, int rssi, u_int32_t rstamp) 2674 { 2675 struct ath_softc *sc = ic->ic_ifp->if_softc; 2676 2677 /* 2678 * Call up first so subsequent work can use information 2679 * potentially stored in the node (e.g. for ibss merge). 2680 */ 2681 sc->sc_recv_mgmt(ic, m, ni, subtype, rssi, rstamp); 2682 switch (subtype) { 2683 case IEEE80211_FC0_SUBTYPE_BEACON: 2684 /* update rssi statistics for use by the hal */ 2685 ATH_RSSI_LPF(sc->sc_halstats.ns_avgbrssi, rssi); 2686 if (sc->sc_syncbeacon && 2687 ni == ic->ic_bss && ic->ic_state == IEEE80211_S_RUN) { 2688 /* 2689 * Resync beacon timers using the tsf of the beacon 2690 * frame we just received. 2691 */ 2692 ath_beacon_config(sc); 2693 } 2694 /* fall thru... */ 2695 case IEEE80211_FC0_SUBTYPE_PROBE_RESP: 2696 if (ic->ic_opmode == IEEE80211_M_IBSS && 2697 ic->ic_state == IEEE80211_S_RUN) { 2698 u_int64_t tsf = ath_extend_tsf(rstamp, 2699 ath_hal_gettsf64(sc->sc_ah)); 2700 /* 2701 * Handle ibss merge as needed; check the tsf on the 2702 * frame before attempting the merge. The 802.11 spec 2703 * says the station should change it's bssid to match 2704 * the oldest station with the same ssid, where oldest 2705 * is determined by the tsf. Note that hardware 2706 * reconfiguration happens through callback to 2707 * ath_newstate as the state machine will go from 2708 * RUN -> RUN when this happens. 2709 */ 2710 if (le64toh(ni->ni_tstamp.tsf) >= tsf) { 2711 DPRINTF(sc, ATH_DEBUG_STATE, 2712 "ibss merge, rstamp %u tsf %ju " 2713 "tstamp %ju\n", rstamp, (uintmax_t)tsf, 2714 (uintmax_t)ni->ni_tstamp.tsf); 2715 (void) ieee80211_ibss_merge(ni); 2716 } 2717 } 2718 break; 2719 } 2720 } 2721 2722 /* 2723 * Set the default antenna. 2724 */ 2725 static void 2726 ath_setdefantenna(struct ath_softc *sc, u_int antenna) 2727 { 2728 struct ath_hal *ah = sc->sc_ah; 2729 2730 /* XXX block beacon interrupts */ 2731 ath_hal_setdefantenna(ah, antenna); 2732 if (sc->sc_defant != antenna) 2733 sc->sc_stats.ast_ant_defswitch++; 2734 sc->sc_defant = antenna; 2735 sc->sc_rxotherant = 0; 2736 } 2737 2738 static int 2739 ath_rx_tap(struct ath_softc *sc, struct mbuf *m, 2740 const struct ath_desc *ds, u_int64_t tsf, int16_t nf) 2741 { 2742 u_int8_t rix; 2743 2744 KASSERT(sc->sc_drvbpf != NULL, ("no tap")); 2745 2746 /* 2747 * Discard anything shorter than an ack or cts. 2748 */ 2749 if (m->m_pkthdr.len < IEEE80211_ACK_LEN) { 2750 DPRINTF(sc, ATH_DEBUG_RECV, "%s: runt packet %d\n", 2751 __func__, m->m_pkthdr.len); 2752 sc->sc_stats.ast_rx_tooshort++; 2753 return 0; 2754 } 2755 sc->sc_rx_th.wr_tsf = htole64( 2756 ath_extend_tsf(ds->ds_rxstat.rs_tstamp, tsf)); 2757 rix = ds->ds_rxstat.rs_rate; 2758 sc->sc_rx_th.wr_flags = sc->sc_hwmap[rix].rxflags; 2759 if (ds->ds_rxstat.rs_status & HAL_RXERR_CRC) 2760 sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_BADFCS; 2761 /* XXX propagate other error flags from descriptor */ 2762 sc->sc_rx_th.wr_rate = sc->sc_hwmap[rix].ieeerate; 2763 sc->sc_rx_th.wr_antsignal = ds->ds_rxstat.rs_rssi + nf; 2764 sc->sc_rx_th.wr_antnoise = nf; 2765 sc->sc_rx_th.wr_antenna = ds->ds_rxstat.rs_antenna; 2766 2767 bpf_mtap2(sc->sc_drvbpf, &sc->sc_rx_th, sc->sc_rx_th_len, m); 2768 2769 return 1; 2770 } 2771 2772 static void 2773 ath_rx_proc(void *arg, int npending) 2774 { 2775 #define PA2DESC(_sc, _pa) \ 2776 ((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \ 2777 ((_pa) - (_sc)->sc_rxdma.dd_desc_paddr))) 2778 struct ath_softc *sc = arg; 2779 struct ath_buf *bf; 2780 struct ieee80211com *ic = &sc->sc_ic; 2781 struct ifnet *ifp = sc->sc_ifp; 2782 struct ath_hal *ah = sc->sc_ah; 2783 struct ath_desc *ds; 2784 struct mbuf *m; 2785 struct ieee80211_node *ni; 2786 struct ath_node *an; 2787 int len, type, ngood; 2788 u_int phyerr; 2789 HAL_STATUS status; 2790 int16_t nf; 2791 u_int64_t tsf; 2792 2793 NET_LOCK_GIANT(); /* XXX */ 2794 2795 DPRINTF(sc, ATH_DEBUG_RX_PROC, "%s: pending %u\n", __func__, npending); 2796 ngood = 0; 2797 nf = ath_hal_getchannoise(ah, &sc->sc_curchan); 2798 tsf = ath_hal_gettsf64(ah); 2799 do { 2800 bf = STAILQ_FIRST(&sc->sc_rxbuf); 2801 if (bf == NULL) { /* NB: shouldn't happen */ 2802 if_printf(ifp, "%s: no buffer!\n", __func__); 2803 break; 2804 } 2805 m = bf->bf_m; 2806 if (m == NULL) { /* NB: shouldn't happen */ 2807 /* 2808 * If mbuf allocation failed previously there 2809 * will be no mbuf; try again to re-populate it. 2810 */ 2811 /* XXX make debug msg */ 2812 if_printf(ifp, "%s: no mbuf!\n", __func__); 2813 STAILQ_REMOVE_HEAD(&sc->sc_rxbuf, bf_list); 2814 goto rx_next; 2815 } 2816 ds = bf->bf_desc; 2817 if (ds->ds_link == bf->bf_daddr) { 2818 /* NB: never process the self-linked entry at the end */ 2819 break; 2820 } 2821 /* XXX sync descriptor memory */ 2822 /* 2823 * Must provide the virtual address of the current 2824 * descriptor, the physical address, and the virtual 2825 * address of the next descriptor in the h/w chain. 2826 * This allows the HAL to look ahead to see if the 2827 * hardware is done with a descriptor by checking the 2828 * done bit in the following descriptor and the address 2829 * of the current descriptor the DMA engine is working 2830 * on. All this is necessary because of our use of 2831 * a self-linked list to avoid rx overruns. 2832 */ 2833 status = ath_hal_rxprocdesc(ah, ds, 2834 bf->bf_daddr, PA2DESC(sc, ds->ds_link)); 2835 #ifdef ATH_DEBUG 2836 if (sc->sc_debug & ATH_DEBUG_RECV_DESC) 2837 ath_printrxbuf(bf, 0, status == HAL_OK); 2838 #endif 2839 if (status == HAL_EINPROGRESS) 2840 break; 2841 STAILQ_REMOVE_HEAD(&sc->sc_rxbuf, bf_list); 2842 if (ds->ds_rxstat.rs_more) { 2843 /* 2844 * Frame spans multiple descriptors; this 2845 * cannot happen yet as we don't support 2846 * jumbograms. If not in monitor mode, 2847 * discard the frame. 2848 */ 2849 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 2850 sc->sc_stats.ast_rx_toobig++; 2851 goto rx_next; 2852 } 2853 /* fall thru for monitor mode handling... */ 2854 } else if (ds->ds_rxstat.rs_status != 0) { 2855 if (ds->ds_rxstat.rs_status & HAL_RXERR_CRC) 2856 sc->sc_stats.ast_rx_crcerr++; 2857 if (ds->ds_rxstat.rs_status & HAL_RXERR_FIFO) 2858 sc->sc_stats.ast_rx_fifoerr++; 2859 if (ds->ds_rxstat.rs_status & HAL_RXERR_PHY) { 2860 sc->sc_stats.ast_rx_phyerr++; 2861 phyerr = ds->ds_rxstat.rs_phyerr & 0x1f; 2862 sc->sc_stats.ast_rx_phy[phyerr]++; 2863 goto rx_next; 2864 } 2865 if (ds->ds_rxstat.rs_status & HAL_RXERR_DECRYPT) { 2866 /* 2867 * Decrypt error. If the error occurred 2868 * because there was no hardware key, then 2869 * let the frame through so the upper layers 2870 * can process it. This is necessary for 5210 2871 * parts which have no way to setup a ``clear'' 2872 * key cache entry. 2873 * 2874 * XXX do key cache faulting 2875 */ 2876 if (ds->ds_rxstat.rs_keyix == HAL_RXKEYIX_INVALID) 2877 goto rx_accept; 2878 sc->sc_stats.ast_rx_badcrypt++; 2879 } 2880 if (ds->ds_rxstat.rs_status & HAL_RXERR_MIC) { 2881 sc->sc_stats.ast_rx_badmic++; 2882 /* 2883 * Do minimal work required to hand off 2884 * the 802.11 header for notifcation. 2885 */ 2886 /* XXX frag's and qos frames */ 2887 len = ds->ds_rxstat.rs_datalen; 2888 if (len >= sizeof (struct ieee80211_frame)) { 2889 bus_dmamap_sync(sc->sc_dmat, 2890 bf->bf_dmamap, 2891 BUS_DMASYNC_POSTREAD); 2892 ieee80211_notify_michael_failure(ic, 2893 mtod(m, struct ieee80211_frame *), 2894 sc->sc_splitmic ? 2895 ds->ds_rxstat.rs_keyix-32 : 2896 ds->ds_rxstat.rs_keyix 2897 ); 2898 } 2899 } 2900 ifp->if_ierrors++; 2901 /* 2902 * When a tap is present pass error frames 2903 * that have been requested. By default we 2904 * pass decrypt+mic errors but others may be 2905 * interesting (e.g. crc). 2906 */ 2907 if (bpf_peers_present(sc->sc_drvbpf) && 2908 (ds->ds_rxstat.rs_status & sc->sc_monpass)) { 2909 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, 2910 BUS_DMASYNC_POSTREAD); 2911 /* NB: bpf needs the mbuf length setup */ 2912 len = ds->ds_rxstat.rs_datalen; 2913 m->m_pkthdr.len = m->m_len = len; 2914 (void) ath_rx_tap(sc, m, ds, tsf, nf); 2915 } 2916 /* XXX pass MIC errors up for s/w reclaculation */ 2917 goto rx_next; 2918 } 2919 rx_accept: 2920 /* 2921 * Sync and unmap the frame. At this point we're 2922 * committed to passing the mbuf somewhere so clear 2923 * bf_m; this means a new mbuf must be allocated 2924 * when the rx descriptor is setup again to receive 2925 * another frame. 2926 */ 2927 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, 2928 BUS_DMASYNC_POSTREAD); 2929 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 2930 bf->bf_m = NULL; 2931 2932 m->m_pkthdr.rcvif = ifp; 2933 len = ds->ds_rxstat.rs_datalen; 2934 m->m_pkthdr.len = m->m_len = len; 2935 2936 sc->sc_stats.ast_ant_rx[ds->ds_rxstat.rs_antenna]++; 2937 2938 if (bpf_peers_present(sc->sc_drvbpf) && 2939 !ath_rx_tap(sc, m, ds, tsf, nf)) { 2940 m_freem(m); /* XXX reclaim */ 2941 goto rx_next; 2942 } 2943 2944 /* 2945 * From this point on we assume the frame is at least 2946 * as large as ieee80211_frame_min; verify that. 2947 */ 2948 if (len < IEEE80211_MIN_LEN) { 2949 DPRINTF(sc, ATH_DEBUG_RECV, "%s: short packet %d\n", 2950 __func__, len); 2951 sc->sc_stats.ast_rx_tooshort++; 2952 m_freem(m); 2953 goto rx_next; 2954 } 2955 2956 if (IFF_DUMPPKTS(sc, ATH_DEBUG_RECV)) { 2957 ieee80211_dump_pkt(mtod(m, caddr_t), len, 2958 sc->sc_hwmap[ds->ds_rxstat.rs_rate].ieeerate, 2959 ds->ds_rxstat.rs_rssi); 2960 } 2961 2962 m_adj(m, -IEEE80211_CRC_LEN); 2963 2964 /* 2965 * Locate the node for sender, track state, and then 2966 * pass the (referenced) node up to the 802.11 layer 2967 * for its use. 2968 */ 2969 ni = ieee80211_find_rxnode_withkey(ic, 2970 mtod(m, const struct ieee80211_frame_min *), 2971 ds->ds_rxstat.rs_keyix == HAL_RXKEYIX_INVALID ? 2972 IEEE80211_KEYIX_NONE : ds->ds_rxstat.rs_keyix); 2973 /* 2974 * Track rx rssi and do any rx antenna management. 2975 */ 2976 an = ATH_NODE(ni); 2977 ATH_RSSI_LPF(an->an_avgrssi, ds->ds_rxstat.rs_rssi); 2978 ATH_RSSI_LPF(sc->sc_halstats.ns_avgrssi, ds->ds_rxstat.rs_rssi); 2979 /* 2980 * Send frame up for processing. 2981 */ 2982 type = ieee80211_input(ic, m, ni, 2983 ds->ds_rxstat.rs_rssi, ds->ds_rxstat.rs_tstamp); 2984 ieee80211_free_node(ni); 2985 if (sc->sc_diversity) { 2986 /* 2987 * When using fast diversity, change the default rx 2988 * antenna if diversity chooses the other antenna 3 2989 * times in a row. 2990 */ 2991 if (sc->sc_defant != ds->ds_rxstat.rs_antenna) { 2992 if (++sc->sc_rxotherant >= 3) 2993 ath_setdefantenna(sc, 2994 ds->ds_rxstat.rs_antenna); 2995 } else 2996 sc->sc_rxotherant = 0; 2997 } 2998 if (sc->sc_softled) { 2999 /* 3000 * Blink for any data frame. Otherwise do a 3001 * heartbeat-style blink when idle. The latter 3002 * is mainly for station mode where we depend on 3003 * periodic beacon frames to trigger the poll event. 3004 */ 3005 if (type == IEEE80211_FC0_TYPE_DATA) { 3006 sc->sc_rxrate = ds->ds_rxstat.rs_rate; 3007 ath_led_event(sc, ATH_LED_RX); 3008 } else if (ticks - sc->sc_ledevent >= sc->sc_ledidle) 3009 ath_led_event(sc, ATH_LED_POLL); 3010 } 3011 /* 3012 * Arrange to update the last rx timestamp only for 3013 * frames from our ap when operating in station mode. 3014 * This assumes the rx key is always setup when associated. 3015 */ 3016 if (ic->ic_opmode == IEEE80211_M_STA && 3017 ds->ds_rxstat.rs_keyix != HAL_RXKEYIX_INVALID) 3018 ngood++; 3019 rx_next: 3020 STAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list); 3021 } while (ath_rxbuf_init(sc, bf) == 0); 3022 3023 /* rx signal state monitoring */ 3024 ath_hal_rxmonitor(ah, &sc->sc_halstats, &sc->sc_curchan); 3025 if (ath_hal_radar_event(ah)) 3026 taskqueue_enqueue(sc->sc_tq, &sc->sc_radartask); 3027 if (ngood) 3028 sc->sc_lastrx = tsf; 3029 3030 NET_UNLOCK_GIANT(); /* XXX */ 3031 #undef PA2DESC 3032 } 3033 3034 /* 3035 * Setup a h/w transmit queue. 3036 */ 3037 static struct ath_txq * 3038 ath_txq_setup(struct ath_softc *sc, int qtype, int subtype) 3039 { 3040 #define N(a) (sizeof(a)/sizeof(a[0])) 3041 struct ath_hal *ah = sc->sc_ah; 3042 HAL_TXQ_INFO qi; 3043 int qnum; 3044 3045 memset(&qi, 0, sizeof(qi)); 3046 qi.tqi_subtype = subtype; 3047 qi.tqi_aifs = HAL_TXQ_USEDEFAULT; 3048 qi.tqi_cwmin = HAL_TXQ_USEDEFAULT; 3049 qi.tqi_cwmax = HAL_TXQ_USEDEFAULT; 3050 /* 3051 * Enable interrupts only for EOL and DESC conditions. 3052 * We mark tx descriptors to receive a DESC interrupt 3053 * when a tx queue gets deep; otherwise waiting for the 3054 * EOL to reap descriptors. Note that this is done to 3055 * reduce interrupt load and this only defers reaping 3056 * descriptors, never transmitting frames. Aside from 3057 * reducing interrupts this also permits more concurrency. 3058 * The only potential downside is if the tx queue backs 3059 * up in which case the top half of the kernel may backup 3060 * due to a lack of tx descriptors. 3061 */ 3062 qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE | HAL_TXQ_TXDESCINT_ENABLE; 3063 qnum = ath_hal_setuptxqueue(ah, qtype, &qi); 3064 if (qnum == -1) { 3065 /* 3066 * NB: don't print a message, this happens 3067 * normally on parts with too few tx queues 3068 */ 3069 return NULL; 3070 } 3071 if (qnum >= N(sc->sc_txq)) { 3072 device_printf(sc->sc_dev, 3073 "hal qnum %u out of range, max %zu!\n", 3074 qnum, N(sc->sc_txq)); 3075 ath_hal_releasetxqueue(ah, qnum); 3076 return NULL; 3077 } 3078 if (!ATH_TXQ_SETUP(sc, qnum)) { 3079 struct ath_txq *txq = &sc->sc_txq[qnum]; 3080 3081 txq->axq_qnum = qnum; 3082 txq->axq_depth = 0; 3083 txq->axq_intrcnt = 0; 3084 txq->axq_link = NULL; 3085 STAILQ_INIT(&txq->axq_q); 3086 ATH_TXQ_LOCK_INIT(sc, txq); 3087 sc->sc_txqsetup |= 1<<qnum; 3088 } 3089 return &sc->sc_txq[qnum]; 3090 #undef N 3091 } 3092 3093 /* 3094 * Setup a hardware data transmit queue for the specified 3095 * access control. The hal may not support all requested 3096 * queues in which case it will return a reference to a 3097 * previously setup queue. We record the mapping from ac's 3098 * to h/w queues for use by ath_tx_start and also track 3099 * the set of h/w queues being used to optimize work in the 3100 * transmit interrupt handler and related routines. 3101 */ 3102 static int 3103 ath_tx_setup(struct ath_softc *sc, int ac, int haltype) 3104 { 3105 #define N(a) (sizeof(a)/sizeof(a[0])) 3106 struct ath_txq *txq; 3107 3108 if (ac >= N(sc->sc_ac2q)) { 3109 device_printf(sc->sc_dev, "AC %u out of range, max %zu!\n", 3110 ac, N(sc->sc_ac2q)); 3111 return 0; 3112 } 3113 txq = ath_txq_setup(sc, HAL_TX_QUEUE_DATA, haltype); 3114 if (txq != NULL) { 3115 sc->sc_ac2q[ac] = txq; 3116 return 1; 3117 } else 3118 return 0; 3119 #undef N 3120 } 3121 3122 /* 3123 * Update WME parameters for a transmit queue. 3124 */ 3125 static int 3126 ath_txq_update(struct ath_softc *sc, int ac) 3127 { 3128 #define ATH_EXPONENT_TO_VALUE(v) ((1<<v)-1) 3129 #define ATH_TXOP_TO_US(v) (v<<5) 3130 struct ieee80211com *ic = &sc->sc_ic; 3131 struct ath_txq *txq = sc->sc_ac2q[ac]; 3132 struct wmeParams *wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac]; 3133 struct ath_hal *ah = sc->sc_ah; 3134 HAL_TXQ_INFO qi; 3135 3136 ath_hal_gettxqueueprops(ah, txq->axq_qnum, &qi); 3137 qi.tqi_aifs = wmep->wmep_aifsn; 3138 qi.tqi_cwmin = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin); 3139 qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax); 3140 qi.tqi_burstTime = ATH_TXOP_TO_US(wmep->wmep_txopLimit); 3141 3142 if (!ath_hal_settxqueueprops(ah, txq->axq_qnum, &qi)) { 3143 device_printf(sc->sc_dev, "unable to update hardware queue " 3144 "parameters for %s traffic!\n", 3145 ieee80211_wme_acnames[ac]); 3146 return 0; 3147 } else { 3148 ath_hal_resettxqueue(ah, txq->axq_qnum); /* push to h/w */ 3149 return 1; 3150 } 3151 #undef ATH_TXOP_TO_US 3152 #undef ATH_EXPONENT_TO_VALUE 3153 } 3154 3155 /* 3156 * Callback from the 802.11 layer to update WME parameters. 3157 */ 3158 static int 3159 ath_wme_update(struct ieee80211com *ic) 3160 { 3161 struct ath_softc *sc = ic->ic_ifp->if_softc; 3162 3163 return !ath_txq_update(sc, WME_AC_BE) || 3164 !ath_txq_update(sc, WME_AC_BK) || 3165 !ath_txq_update(sc, WME_AC_VI) || 3166 !ath_txq_update(sc, WME_AC_VO) ? EIO : 0; 3167 } 3168 3169 /* 3170 * Reclaim resources for a setup queue. 3171 */ 3172 static void 3173 ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq) 3174 { 3175 3176 ath_hal_releasetxqueue(sc->sc_ah, txq->axq_qnum); 3177 ATH_TXQ_LOCK_DESTROY(txq); 3178 sc->sc_txqsetup &= ~(1<<txq->axq_qnum); 3179 } 3180 3181 /* 3182 * Reclaim all tx queue resources. 3183 */ 3184 static void 3185 ath_tx_cleanup(struct ath_softc *sc) 3186 { 3187 int i; 3188 3189 ATH_TXBUF_LOCK_DESTROY(sc); 3190 for (i = 0; i < HAL_NUM_TX_QUEUES; i++) 3191 if (ATH_TXQ_SETUP(sc, i)) 3192 ath_tx_cleanupq(sc, &sc->sc_txq[i]); 3193 } 3194 3195 /* 3196 * Defragment an mbuf chain, returning at most maxfrags separate 3197 * mbufs+clusters. If this is not possible NULL is returned and 3198 * the original mbuf chain is left in it's present (potentially 3199 * modified) state. We use two techniques: collapsing consecutive 3200 * mbufs and replacing consecutive mbufs by a cluster. 3201 */ 3202 static struct mbuf * 3203 ath_defrag(struct mbuf *m0, int how, int maxfrags) 3204 { 3205 struct mbuf *m, *n, *n2, **prev; 3206 u_int curfrags; 3207 3208 /* 3209 * Calculate the current number of frags. 3210 */ 3211 curfrags = 0; 3212 for (m = m0; m != NULL; m = m->m_next) 3213 curfrags++; 3214 /* 3215 * First, try to collapse mbufs. Note that we always collapse 3216 * towards the front so we don't need to deal with moving the 3217 * pkthdr. This may be suboptimal if the first mbuf has much 3218 * less data than the following. 3219 */ 3220 m = m0; 3221 again: 3222 for (;;) { 3223 n = m->m_next; 3224 if (n == NULL) 3225 break; 3226 if ((m->m_flags & M_RDONLY) == 0 && 3227 n->m_len < M_TRAILINGSPACE(m)) { 3228 bcopy(mtod(n, void *), mtod(m, char *) + m->m_len, 3229 n->m_len); 3230 m->m_len += n->m_len; 3231 m->m_next = n->m_next; 3232 m_free(n); 3233 if (--curfrags <= maxfrags) 3234 return m0; 3235 } else 3236 m = n; 3237 } 3238 KASSERT(maxfrags > 1, 3239 ("maxfrags %u, but normal collapse failed", maxfrags)); 3240 /* 3241 * Collapse consecutive mbufs to a cluster. 3242 */ 3243 prev = &m0->m_next; /* NB: not the first mbuf */ 3244 while ((n = *prev) != NULL) { 3245 if ((n2 = n->m_next) != NULL && 3246 n->m_len + n2->m_len < MCLBYTES) { 3247 m = m_getcl(how, MT_DATA, 0); 3248 if (m == NULL) 3249 goto bad; 3250 bcopy(mtod(n, void *), mtod(m, void *), n->m_len); 3251 bcopy(mtod(n2, void *), mtod(m, char *) + n->m_len, 3252 n2->m_len); 3253 m->m_len = n->m_len + n2->m_len; 3254 m->m_next = n2->m_next; 3255 *prev = m; 3256 m_free(n); 3257 m_free(n2); 3258 if (--curfrags <= maxfrags) /* +1 cl -2 mbufs */ 3259 return m0; 3260 /* 3261 * Still not there, try the normal collapse 3262 * again before we allocate another cluster. 3263 */ 3264 goto again; 3265 } 3266 prev = &n->m_next; 3267 } 3268 /* 3269 * No place where we can collapse to a cluster; punt. 3270 * This can occur if, for example, you request 2 frags 3271 * but the packet requires that both be clusters (we 3272 * never reallocate the first mbuf to avoid moving the 3273 * packet header). 3274 */ 3275 bad: 3276 return NULL; 3277 } 3278 3279 /* 3280 * Return h/w rate index for an IEEE rate (w/o basic rate bit). 3281 */ 3282 static int 3283 ath_tx_findrix(const HAL_RATE_TABLE *rt, int rate) 3284 { 3285 int i; 3286 3287 for (i = 0; i < rt->rateCount; i++) 3288 if ((rt->info[i].dot11Rate & IEEE80211_RATE_VAL) == rate) 3289 return i; 3290 return 0; /* NB: lowest rate */ 3291 } 3292 3293 static int 3294 ath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni, struct ath_buf *bf, 3295 struct mbuf *m0) 3296 { 3297 struct ieee80211com *ic = &sc->sc_ic; 3298 struct ath_hal *ah = sc->sc_ah; 3299 struct ifnet *ifp = sc->sc_ifp; 3300 const struct chanAccParams *cap = &ic->ic_wme.wme_chanParams; 3301 int i, error, iswep, ismcast, ismrr; 3302 int keyix, hdrlen, pktlen, try0; 3303 u_int8_t rix, txrate, ctsrate; 3304 u_int8_t cix = 0xff; /* NB: silence compiler */ 3305 struct ath_desc *ds, *ds0; 3306 struct ath_txq *txq; 3307 struct ieee80211_frame *wh; 3308 u_int subtype, flags, ctsduration; 3309 HAL_PKT_TYPE atype; 3310 const HAL_RATE_TABLE *rt; 3311 HAL_BOOL shortPreamble; 3312 struct ath_node *an; 3313 struct mbuf *m; 3314 u_int pri; 3315 3316 wh = mtod(m0, struct ieee80211_frame *); 3317 iswep = wh->i_fc[1] & IEEE80211_FC1_WEP; 3318 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1); 3319 hdrlen = ieee80211_anyhdrsize(wh); 3320 /* 3321 * Packet length must not include any 3322 * pad bytes; deduct them here. 3323 */ 3324 pktlen = m0->m_pkthdr.len - (hdrlen & 3); 3325 3326 if (iswep) { 3327 const struct ieee80211_cipher *cip; 3328 struct ieee80211_key *k; 3329 3330 /* 3331 * Construct the 802.11 header+trailer for an encrypted 3332 * frame. The only reason this can fail is because of an 3333 * unknown or unsupported cipher/key type. 3334 */ 3335 k = ieee80211_crypto_encap(ic, ni, m0); 3336 if (k == NULL) { 3337 /* 3338 * This can happen when the key is yanked after the 3339 * frame was queued. Just discard the frame; the 3340 * 802.11 layer counts failures and provides 3341 * debugging/diagnostics. 3342 */ 3343 m_freem(m0); 3344 return EIO; 3345 } 3346 /* 3347 * Adjust the packet + header lengths for the crypto 3348 * additions and calculate the h/w key index. When 3349 * a s/w mic is done the frame will have had any mic 3350 * added to it prior to entry so m0->m_pkthdr.len above will 3351 * account for it. Otherwise we need to add it to the 3352 * packet length. 3353 */ 3354 cip = k->wk_cipher; 3355 hdrlen += cip->ic_header; 3356 pktlen += cip->ic_header + cip->ic_trailer; 3357 if ((k->wk_flags & IEEE80211_KEY_SWMIC) == 0) 3358 pktlen += cip->ic_miclen; 3359 keyix = k->wk_keyix; 3360 3361 /* packet header may have moved, reset our local pointer */ 3362 wh = mtod(m0, struct ieee80211_frame *); 3363 } else if (ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) { 3364 /* 3365 * Use station key cache slot, if assigned. 3366 */ 3367 keyix = ni->ni_ucastkey.wk_keyix; 3368 if (keyix == IEEE80211_KEYIX_NONE) 3369 keyix = HAL_TXKEYIX_INVALID; 3370 } else 3371 keyix = HAL_TXKEYIX_INVALID; 3372 3373 pktlen += IEEE80211_CRC_LEN; 3374 3375 /* 3376 * Load the DMA map so any coalescing is done. This 3377 * also calculates the number of descriptors we need. 3378 */ 3379 error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0, 3380 bf->bf_segs, &bf->bf_nseg, 3381 BUS_DMA_NOWAIT); 3382 if (error == EFBIG) { 3383 /* XXX packet requires too many descriptors */ 3384 bf->bf_nseg = ATH_TXDESC+1; 3385 } else if (error != 0) { 3386 sc->sc_stats.ast_tx_busdma++; 3387 m_freem(m0); 3388 return error; 3389 } 3390 /* 3391 * Discard null packets and check for packets that 3392 * require too many TX descriptors. We try to convert 3393 * the latter to a cluster. 3394 */ 3395 if (bf->bf_nseg > ATH_TXDESC) { /* too many desc's, linearize */ 3396 sc->sc_stats.ast_tx_linear++; 3397 m = ath_defrag(m0, M_DONTWAIT, ATH_TXDESC); 3398 if (m == NULL) { 3399 m_freem(m0); 3400 sc->sc_stats.ast_tx_nombuf++; 3401 return ENOMEM; 3402 } 3403 m0 = m; 3404 error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0, 3405 bf->bf_segs, &bf->bf_nseg, 3406 BUS_DMA_NOWAIT); 3407 if (error != 0) { 3408 sc->sc_stats.ast_tx_busdma++; 3409 m_freem(m0); 3410 return error; 3411 } 3412 KASSERT(bf->bf_nseg <= ATH_TXDESC, 3413 ("too many segments after defrag; nseg %u", bf->bf_nseg)); 3414 } else if (bf->bf_nseg == 0) { /* null packet, discard */ 3415 sc->sc_stats.ast_tx_nodata++; 3416 m_freem(m0); 3417 return EIO; 3418 } 3419 DPRINTF(sc, ATH_DEBUG_XMIT, "%s: m %p len %u\n", __func__, m0, pktlen); 3420 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE); 3421 bf->bf_m = m0; 3422 bf->bf_node = ni; /* NB: held reference */ 3423 3424 /* setup descriptors */ 3425 ds = bf->bf_desc; 3426 rt = sc->sc_currates; 3427 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode)); 3428 3429 /* 3430 * NB: the 802.11 layer marks whether or not we should 3431 * use short preamble based on the current mode and 3432 * negotiated parameters. 3433 */ 3434 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 3435 (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) { 3436 shortPreamble = AH_TRUE; 3437 sc->sc_stats.ast_tx_shortpre++; 3438 } else { 3439 shortPreamble = AH_FALSE; 3440 } 3441 3442 an = ATH_NODE(ni); 3443 flags = HAL_TXDESC_CLRDMASK; /* XXX needed for crypto errs */ 3444 ismrr = 0; /* default no multi-rate retry*/ 3445 /* 3446 * Calculate Atheros packet type from IEEE80211 packet header, 3447 * setup for rate calculations, and select h/w transmit queue. 3448 */ 3449 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) { 3450 case IEEE80211_FC0_TYPE_MGT: 3451 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 3452 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON) 3453 atype = HAL_PKT_TYPE_BEACON; 3454 else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) 3455 atype = HAL_PKT_TYPE_PROBE_RESP; 3456 else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM) 3457 atype = HAL_PKT_TYPE_ATIM; 3458 else 3459 atype = HAL_PKT_TYPE_NORMAL; /* XXX */ 3460 rix = sc->sc_minrateix; 3461 txrate = rt->info[rix].rateCode; 3462 if (shortPreamble) 3463 txrate |= rt->info[rix].shortPreamble; 3464 try0 = ATH_TXMGTTRY; 3465 /* NB: force all management frames to highest queue */ 3466 if (ni->ni_flags & IEEE80211_NODE_QOS) { 3467 /* NB: force all management frames to highest queue */ 3468 pri = WME_AC_VO; 3469 } else 3470 pri = WME_AC_BE; 3471 flags |= HAL_TXDESC_INTREQ; /* force interrupt */ 3472 break; 3473 case IEEE80211_FC0_TYPE_CTL: 3474 atype = HAL_PKT_TYPE_PSPOLL; /* stop setting of duration */ 3475 rix = sc->sc_minrateix; 3476 txrate = rt->info[rix].rateCode; 3477 if (shortPreamble) 3478 txrate |= rt->info[rix].shortPreamble; 3479 try0 = ATH_TXMGTTRY; 3480 /* NB: force all ctl frames to highest queue */ 3481 if (ni->ni_flags & IEEE80211_NODE_QOS) { 3482 /* NB: force all ctl frames to highest queue */ 3483 pri = WME_AC_VO; 3484 } else 3485 pri = WME_AC_BE; 3486 flags |= HAL_TXDESC_INTREQ; /* force interrupt */ 3487 break; 3488 case IEEE80211_FC0_TYPE_DATA: 3489 atype = HAL_PKT_TYPE_NORMAL; /* default */ 3490 /* 3491 * Data frames: multicast frames go out at a fixed rate, 3492 * otherwise consult the rate control module for the 3493 * rate to use. 3494 */ 3495 if (ismcast) { 3496 /* 3497 * Check mcast rate setting in case it's changed. 3498 * XXX move out of fastpath 3499 */ 3500 if (ic->ic_mcast_rate != sc->sc_mcastrate) { 3501 sc->sc_mcastrix = 3502 ath_tx_findrix(rt, ic->ic_mcast_rate); 3503 sc->sc_mcastrate = ic->ic_mcast_rate; 3504 } 3505 rix = sc->sc_mcastrix; 3506 txrate = rt->info[rix].rateCode; 3507 if (shortPreamble) 3508 txrate |= rt->info[rix].shortPreamble; 3509 try0 = 1; 3510 } else { 3511 ath_rate_findrate(sc, an, shortPreamble, pktlen, 3512 &rix, &try0, &txrate); 3513 sc->sc_txrate = txrate; /* for LED blinking */ 3514 if (try0 != ATH_TXMAXTRY) 3515 ismrr = 1; 3516 } 3517 pri = M_WME_GETAC(m0); 3518 if (cap->cap_wmeParams[pri].wmep_noackPolicy) 3519 flags |= HAL_TXDESC_NOACK; 3520 break; 3521 default: 3522 if_printf(ifp, "bogus frame type 0x%x (%s)\n", 3523 wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__); 3524 /* XXX statistic */ 3525 m_freem(m0); 3526 return EIO; 3527 } 3528 txq = sc->sc_ac2q[pri]; 3529 3530 /* 3531 * When servicing one or more stations in power-save mode 3532 * multicast frames must be buffered until after the beacon. 3533 * We use the CAB queue for that. 3534 */ 3535 if (ismcast && ic->ic_ps_sta) { 3536 txq = sc->sc_cabq; 3537 /* XXX? more bit in 802.11 frame header */ 3538 } 3539 3540 /* 3541 * Calculate miscellaneous flags. 3542 */ 3543 if (ismcast) { 3544 flags |= HAL_TXDESC_NOACK; /* no ack on broad/multicast */ 3545 } else if (pktlen > ic->ic_rtsthreshold) { 3546 flags |= HAL_TXDESC_RTSENA; /* RTS based on frame length */ 3547 cix = rt->info[rix].controlRate; 3548 sc->sc_stats.ast_tx_rts++; 3549 } 3550 if (flags & HAL_TXDESC_NOACK) /* NB: avoid double counting */ 3551 sc->sc_stats.ast_tx_noack++; 3552 3553 /* 3554 * If 802.11g protection is enabled, determine whether 3555 * to use RTS/CTS or just CTS. Note that this is only 3556 * done for OFDM unicast frames. 3557 */ 3558 if ((ic->ic_flags & IEEE80211_F_USEPROT) && 3559 rt->info[rix].phy == IEEE80211_T_OFDM && 3560 (flags & HAL_TXDESC_NOACK) == 0) { 3561 /* XXX fragments must use CCK rates w/ protection */ 3562 if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) 3563 flags |= HAL_TXDESC_RTSENA; 3564 else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) 3565 flags |= HAL_TXDESC_CTSENA; 3566 cix = rt->info[sc->sc_protrix].controlRate; 3567 sc->sc_stats.ast_tx_protect++; 3568 } 3569 3570 /* 3571 * Calculate duration. This logically belongs in the 802.11 3572 * layer but it lacks sufficient information to calculate it. 3573 */ 3574 if ((flags & HAL_TXDESC_NOACK) == 0 && 3575 (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL) { 3576 u_int16_t dur; 3577 /* 3578 * XXX not right with fragmentation. 3579 */ 3580 if (shortPreamble) 3581 dur = rt->info[rix].spAckDuration; 3582 else 3583 dur = rt->info[rix].lpAckDuration; 3584 *(u_int16_t *)wh->i_dur = htole16(dur); 3585 } 3586 3587 /* 3588 * Calculate RTS/CTS rate and duration if needed. 3589 */ 3590 ctsduration = 0; 3591 if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA)) { 3592 /* 3593 * CTS transmit rate is derived from the transmit rate 3594 * by looking in the h/w rate table. We must also factor 3595 * in whether or not a short preamble is to be used. 3596 */ 3597 /* NB: cix is set above where RTS/CTS is enabled */ 3598 KASSERT(cix != 0xff, ("cix not setup")); 3599 ctsrate = rt->info[cix].rateCode; 3600 /* 3601 * Compute the transmit duration based on the frame 3602 * size and the size of an ACK frame. We call into the 3603 * HAL to do the computation since it depends on the 3604 * characteristics of the actual PHY being used. 3605 * 3606 * NB: CTS is assumed the same size as an ACK so we can 3607 * use the precalculated ACK durations. 3608 */ 3609 if (shortPreamble) { 3610 ctsrate |= rt->info[cix].shortPreamble; 3611 if (flags & HAL_TXDESC_RTSENA) /* SIFS + CTS */ 3612 ctsduration += rt->info[cix].spAckDuration; 3613 ctsduration += ath_hal_computetxtime(ah, 3614 rt, pktlen, rix, AH_TRUE); 3615 if ((flags & HAL_TXDESC_NOACK) == 0) /* SIFS + ACK */ 3616 ctsduration += rt->info[rix].spAckDuration; 3617 } else { 3618 if (flags & HAL_TXDESC_RTSENA) /* SIFS + CTS */ 3619 ctsduration += rt->info[cix].lpAckDuration; 3620 ctsduration += ath_hal_computetxtime(ah, 3621 rt, pktlen, rix, AH_FALSE); 3622 if ((flags & HAL_TXDESC_NOACK) == 0) /* SIFS + ACK */ 3623 ctsduration += rt->info[rix].lpAckDuration; 3624 } 3625 /* 3626 * Must disable multi-rate retry when using RTS/CTS. 3627 */ 3628 ismrr = 0; 3629 try0 = ATH_TXMGTTRY; /* XXX */ 3630 } else 3631 ctsrate = 0; 3632 3633 if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT)) 3634 ieee80211_dump_pkt(mtod(m0, caddr_t), m0->m_len, 3635 sc->sc_hwmap[txrate].ieeerate, -1); 3636 3637 if (bpf_peers_present(ic->ic_rawbpf)) 3638 bpf_mtap(ic->ic_rawbpf, m0); 3639 if (bpf_peers_present(sc->sc_drvbpf)) { 3640 u_int64_t tsf = ath_hal_gettsf64(ah); 3641 3642 sc->sc_tx_th.wt_tsf = htole64(tsf); 3643 sc->sc_tx_th.wt_flags = sc->sc_hwmap[txrate].txflags; 3644 if (iswep) 3645 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP; 3646 sc->sc_tx_th.wt_rate = sc->sc_hwmap[txrate].ieeerate; 3647 sc->sc_tx_th.wt_txpower = ni->ni_txpower; 3648 sc->sc_tx_th.wt_antenna = sc->sc_txantenna; 3649 3650 bpf_mtap2(sc->sc_drvbpf, 3651 &sc->sc_tx_th, sc->sc_tx_th_len, m0); 3652 } 3653 3654 /* 3655 * Determine if a tx interrupt should be generated for 3656 * this descriptor. We take a tx interrupt to reap 3657 * descriptors when the h/w hits an EOL condition or 3658 * when the descriptor is specifically marked to generate 3659 * an interrupt. We periodically mark descriptors in this 3660 * way to insure timely replenishing of the supply needed 3661 * for sending frames. Defering interrupts reduces system 3662 * load and potentially allows more concurrent work to be 3663 * done but if done to aggressively can cause senders to 3664 * backup. 3665 * 3666 * NB: use >= to deal with sc_txintrperiod changing 3667 * dynamically through sysctl. 3668 */ 3669 if (flags & HAL_TXDESC_INTREQ) { 3670 txq->axq_intrcnt = 0; 3671 } else if (++txq->axq_intrcnt >= sc->sc_txintrperiod) { 3672 flags |= HAL_TXDESC_INTREQ; 3673 txq->axq_intrcnt = 0; 3674 } 3675 3676 /* 3677 * Formulate first tx descriptor with tx controls. 3678 */ 3679 /* XXX check return value? */ 3680 ath_hal_setuptxdesc(ah, ds 3681 , pktlen /* packet length */ 3682 , hdrlen /* header length */ 3683 , atype /* Atheros packet type */ 3684 , ni->ni_txpower /* txpower */ 3685 , txrate, try0 /* series 0 rate/tries */ 3686 , keyix /* key cache index */ 3687 , sc->sc_txantenna /* antenna mode */ 3688 , flags /* flags */ 3689 , ctsrate /* rts/cts rate */ 3690 , ctsduration /* rts/cts duration */ 3691 ); 3692 bf->bf_flags = flags; 3693 /* 3694 * Setup the multi-rate retry state only when we're 3695 * going to use it. This assumes ath_hal_setuptxdesc 3696 * initializes the descriptors (so we don't have to) 3697 * when the hardware supports multi-rate retry and 3698 * we don't use it. 3699 */ 3700 if (ismrr) 3701 ath_rate_setupxtxdesc(sc, an, ds, shortPreamble, rix); 3702 3703 /* 3704 * Fillin the remainder of the descriptor info. 3705 */ 3706 ds0 = ds; 3707 for (i = 0; i < bf->bf_nseg; i++, ds++) { 3708 ds->ds_data = bf->bf_segs[i].ds_addr; 3709 if (i == bf->bf_nseg - 1) 3710 ds->ds_link = 0; 3711 else 3712 ds->ds_link = bf->bf_daddr + sizeof(*ds) * (i + 1); 3713 ath_hal_filltxdesc(ah, ds 3714 , bf->bf_segs[i].ds_len /* segment length */ 3715 , i == 0 /* first segment */ 3716 , i == bf->bf_nseg - 1 /* last segment */ 3717 , ds0 /* first descriptor */ 3718 ); 3719 DPRINTF(sc, ATH_DEBUG_XMIT, 3720 "%s: %d: %08x %08x %08x %08x %08x %08x\n", 3721 __func__, i, ds->ds_link, ds->ds_data, 3722 ds->ds_ctl0, ds->ds_ctl1, ds->ds_hw[0], ds->ds_hw[1]); 3723 } 3724 /* 3725 * Insert the frame on the outbound list and 3726 * pass it on to the hardware. 3727 */ 3728 ATH_TXQ_LOCK(txq); 3729 ATH_TXQ_INSERT_TAIL(txq, bf, bf_list); 3730 if (txq->axq_link == NULL) { 3731 ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr); 3732 DPRINTF(sc, ATH_DEBUG_XMIT, 3733 "%s: TXDP[%u] = %p (%p) depth %d\n", __func__, 3734 txq->axq_qnum, (caddr_t)bf->bf_daddr, bf->bf_desc, 3735 txq->axq_depth); 3736 } else { 3737 *txq->axq_link = bf->bf_daddr; 3738 DPRINTF(sc, ATH_DEBUG_XMIT, 3739 "%s: link[%u](%p)=%p (%p) depth %d\n", __func__, 3740 txq->axq_qnum, txq->axq_link, 3741 (caddr_t)bf->bf_daddr, bf->bf_desc, txq->axq_depth); 3742 } 3743 txq->axq_link = &bf->bf_desc[bf->bf_nseg - 1].ds_link; 3744 /* 3745 * The CAB queue is started from the SWBA handler since 3746 * frames only go out on DTIM and to avoid possible races. 3747 */ 3748 if (txq != sc->sc_cabq) 3749 ath_hal_txstart(ah, txq->axq_qnum); 3750 ATH_TXQ_UNLOCK(txq); 3751 3752 return 0; 3753 } 3754 3755 /* 3756 * Process completed xmit descriptors from the specified queue. 3757 */ 3758 static int 3759 ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq) 3760 { 3761 struct ath_hal *ah = sc->sc_ah; 3762 struct ieee80211com *ic = &sc->sc_ic; 3763 struct ath_buf *bf; 3764 struct ath_desc *ds, *ds0; 3765 struct ieee80211_node *ni; 3766 struct ath_node *an; 3767 int sr, lr, pri, nacked; 3768 HAL_STATUS status; 3769 3770 DPRINTF(sc, ATH_DEBUG_TX_PROC, "%s: tx queue %u head %p link %p\n", 3771 __func__, txq->axq_qnum, 3772 (caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum), 3773 txq->axq_link); 3774 nacked = 0; 3775 for (;;) { 3776 ATH_TXQ_LOCK(txq); 3777 txq->axq_intrcnt = 0; /* reset periodic desc intr count */ 3778 bf = STAILQ_FIRST(&txq->axq_q); 3779 if (bf == NULL) { 3780 ATH_TXQ_UNLOCK(txq); 3781 break; 3782 } 3783 ds0 = &bf->bf_desc[0]; 3784 ds = &bf->bf_desc[bf->bf_nseg - 1]; 3785 status = ath_hal_txprocdesc(ah, ds); 3786 #ifdef ATH_DEBUG 3787 if (sc->sc_debug & ATH_DEBUG_XMIT_DESC) 3788 ath_printtxbuf(bf, txq->axq_qnum, 0, status == HAL_OK); 3789 #endif 3790 if (status == HAL_EINPROGRESS) { 3791 ATH_TXQ_UNLOCK(txq); 3792 break; 3793 } 3794 ATH_TXQ_REMOVE_HEAD(txq, bf_list); 3795 if (txq->axq_depth == 0) 3796 txq->axq_link = NULL; 3797 ATH_TXQ_UNLOCK(txq); 3798 3799 ni = bf->bf_node; 3800 if (ni != NULL) { 3801 an = ATH_NODE(ni); 3802 if (ds->ds_txstat.ts_status == 0) { 3803 u_int8_t txant = ds->ds_txstat.ts_antenna; 3804 sc->sc_stats.ast_ant_tx[txant]++; 3805 sc->sc_ant_tx[txant]++; 3806 if (ds->ds_txstat.ts_rate & HAL_TXSTAT_ALTRATE) 3807 sc->sc_stats.ast_tx_altrate++; 3808 sc->sc_stats.ast_tx_rssi = 3809 ds->ds_txstat.ts_rssi; 3810 ATH_RSSI_LPF(sc->sc_halstats.ns_avgtxrssi, 3811 ds->ds_txstat.ts_rssi); 3812 pri = M_WME_GETAC(bf->bf_m); 3813 if (pri >= WME_AC_VO) 3814 ic->ic_wme.wme_hipri_traffic++; 3815 ni->ni_inact = ni->ni_inact_reload; 3816 } else { 3817 if (ds->ds_txstat.ts_status & HAL_TXERR_XRETRY) 3818 sc->sc_stats.ast_tx_xretries++; 3819 if (ds->ds_txstat.ts_status & HAL_TXERR_FIFO) 3820 sc->sc_stats.ast_tx_fifoerr++; 3821 if (ds->ds_txstat.ts_status & HAL_TXERR_FILT) 3822 sc->sc_stats.ast_tx_filtered++; 3823 } 3824 sr = ds->ds_txstat.ts_shortretry; 3825 lr = ds->ds_txstat.ts_longretry; 3826 sc->sc_stats.ast_tx_shortretry += sr; 3827 sc->sc_stats.ast_tx_longretry += lr; 3828 /* 3829 * Hand the descriptor to the rate control algorithm. 3830 */ 3831 if ((ds->ds_txstat.ts_status & HAL_TXERR_FILT) == 0 && 3832 (bf->bf_flags & HAL_TXDESC_NOACK) == 0) { 3833 /* 3834 * If frame was ack'd update the last rx time 3835 * used to workaround phantom bmiss interrupts. 3836 */ 3837 if (ds->ds_txstat.ts_status == 0) 3838 nacked++; 3839 ath_rate_tx_complete(sc, an, ds, ds0); 3840 } 3841 /* 3842 * Reclaim reference to node. 3843 * 3844 * NB: the node may be reclaimed here if, for example 3845 * this is a DEAUTH message that was sent and the 3846 * node was timed out due to inactivity. 3847 */ 3848 ieee80211_free_node(ni); 3849 } 3850 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, 3851 BUS_DMASYNC_POSTWRITE); 3852 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 3853 m_freem(bf->bf_m); 3854 bf->bf_m = NULL; 3855 bf->bf_node = NULL; 3856 3857 ATH_TXBUF_LOCK(sc); 3858 STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list); 3859 ATH_TXBUF_UNLOCK(sc); 3860 } 3861 return nacked; 3862 } 3863 3864 static __inline int 3865 txqactive(struct ath_hal *ah, int qnum) 3866 { 3867 u_int32_t txqs = 1<<qnum; 3868 ath_hal_gettxintrtxqs(ah, &txqs); 3869 return (txqs & (1<<qnum)); 3870 } 3871 3872 /* 3873 * Deferred processing of transmit interrupt; special-cased 3874 * for a single hardware transmit queue (e.g. 5210 and 5211). 3875 */ 3876 static void 3877 ath_tx_proc_q0(void *arg, int npending) 3878 { 3879 struct ath_softc *sc = arg; 3880 struct ifnet *ifp = sc->sc_ifp; 3881 3882 if (txqactive(sc->sc_ah, 0) && ath_tx_processq(sc, &sc->sc_txq[0])) 3883 sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah); 3884 if (txqactive(sc->sc_ah, sc->sc_cabq->axq_qnum)) 3885 ath_tx_processq(sc, sc->sc_cabq); 3886 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 3887 sc->sc_tx_timer = 0; 3888 3889 if (sc->sc_softled) 3890 ath_led_event(sc, ATH_LED_TX); 3891 3892 ath_start(ifp); 3893 } 3894 3895 /* 3896 * Deferred processing of transmit interrupt; special-cased 3897 * for four hardware queues, 0-3 (e.g. 5212 w/ WME support). 3898 */ 3899 static void 3900 ath_tx_proc_q0123(void *arg, int npending) 3901 { 3902 struct ath_softc *sc = arg; 3903 struct ifnet *ifp = sc->sc_ifp; 3904 int nacked; 3905 3906 /* 3907 * Process each active queue. 3908 */ 3909 nacked = 0; 3910 if (txqactive(sc->sc_ah, 0)) 3911 nacked += ath_tx_processq(sc, &sc->sc_txq[0]); 3912 if (txqactive(sc->sc_ah, 1)) 3913 nacked += ath_tx_processq(sc, &sc->sc_txq[1]); 3914 if (txqactive(sc->sc_ah, 2)) 3915 nacked += ath_tx_processq(sc, &sc->sc_txq[2]); 3916 if (txqactive(sc->sc_ah, 3)) 3917 nacked += ath_tx_processq(sc, &sc->sc_txq[3]); 3918 if (txqactive(sc->sc_ah, sc->sc_cabq->axq_qnum)) 3919 ath_tx_processq(sc, sc->sc_cabq); 3920 if (nacked) 3921 sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah); 3922 3923 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 3924 sc->sc_tx_timer = 0; 3925 3926 if (sc->sc_softled) 3927 ath_led_event(sc, ATH_LED_TX); 3928 3929 ath_start(ifp); 3930 } 3931 3932 /* 3933 * Deferred processing of transmit interrupt. 3934 */ 3935 static void 3936 ath_tx_proc(void *arg, int npending) 3937 { 3938 struct ath_softc *sc = arg; 3939 struct ifnet *ifp = sc->sc_ifp; 3940 int i, nacked; 3941 3942 /* 3943 * Process each active queue. 3944 */ 3945 nacked = 0; 3946 for (i = 0; i < HAL_NUM_TX_QUEUES; i++) 3947 if (ATH_TXQ_SETUP(sc, i) && txqactive(sc->sc_ah, i)) 3948 nacked += ath_tx_processq(sc, &sc->sc_txq[i]); 3949 if (nacked) 3950 sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah); 3951 3952 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 3953 sc->sc_tx_timer = 0; 3954 3955 if (sc->sc_softled) 3956 ath_led_event(sc, ATH_LED_TX); 3957 3958 ath_start(ifp); 3959 } 3960 3961 static void 3962 ath_tx_draintxq(struct ath_softc *sc, struct ath_txq *txq) 3963 { 3964 #ifdef ATH_DEBUG 3965 struct ath_hal *ah = sc->sc_ah; 3966 #endif 3967 struct ieee80211_node *ni; 3968 struct ath_buf *bf; 3969 u_int ix; 3970 3971 /* 3972 * NB: this assumes output has been stopped and 3973 * we do not need to block ath_tx_tasklet 3974 */ 3975 for (ix = 0;; ix++) { 3976 ATH_TXQ_LOCK(txq); 3977 bf = STAILQ_FIRST(&txq->axq_q); 3978 if (bf == NULL) { 3979 txq->axq_link = NULL; 3980 ATH_TXQ_UNLOCK(txq); 3981 break; 3982 } 3983 ATH_TXQ_REMOVE_HEAD(txq, bf_list); 3984 ATH_TXQ_UNLOCK(txq); 3985 #ifdef ATH_DEBUG 3986 if (sc->sc_debug & ATH_DEBUG_RESET) { 3987 ath_printtxbuf(bf, txq->axq_qnum, ix, 3988 ath_hal_txprocdesc(ah, bf->bf_desc) == HAL_OK); 3989 ieee80211_dump_pkt(mtod(bf->bf_m, caddr_t), 3990 bf->bf_m->m_len, 0, -1); 3991 } 3992 #endif /* ATH_DEBUG */ 3993 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 3994 m_freem(bf->bf_m); 3995 bf->bf_m = NULL; 3996 ni = bf->bf_node; 3997 bf->bf_node = NULL; 3998 if (ni != NULL) { 3999 /* 4000 * Reclaim node reference. 4001 */ 4002 ieee80211_free_node(ni); 4003 } 4004 ATH_TXBUF_LOCK(sc); 4005 STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list); 4006 ATH_TXBUF_UNLOCK(sc); 4007 } 4008 } 4009 4010 static void 4011 ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq) 4012 { 4013 struct ath_hal *ah = sc->sc_ah; 4014 4015 DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n", 4016 __func__, txq->axq_qnum, 4017 (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, txq->axq_qnum), 4018 txq->axq_link); 4019 (void) ath_hal_stoptxdma(ah, txq->axq_qnum); 4020 } 4021 4022 /* 4023 * Drain the transmit queues and reclaim resources. 4024 */ 4025 static void 4026 ath_draintxq(struct ath_softc *sc) 4027 { 4028 struct ath_hal *ah = sc->sc_ah; 4029 struct ifnet *ifp = sc->sc_ifp; 4030 int i; 4031 4032 /* XXX return value */ 4033 if (!sc->sc_invalid) { 4034 /* don't touch the hardware if marked invalid */ 4035 DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n", 4036 __func__, sc->sc_bhalq, 4037 (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq), 4038 NULL); 4039 (void) ath_hal_stoptxdma(ah, sc->sc_bhalq); 4040 for (i = 0; i < HAL_NUM_TX_QUEUES; i++) 4041 if (ATH_TXQ_SETUP(sc, i)) 4042 ath_tx_stopdma(sc, &sc->sc_txq[i]); 4043 } 4044 for (i = 0; i < HAL_NUM_TX_QUEUES; i++) 4045 if (ATH_TXQ_SETUP(sc, i)) 4046 ath_tx_draintxq(sc, &sc->sc_txq[i]); 4047 #ifdef ATH_DEBUG 4048 if (sc->sc_debug & ATH_DEBUG_RESET) { 4049 struct ath_buf *bf = STAILQ_FIRST(&sc->sc_bbuf); 4050 if (bf != NULL && bf->bf_m != NULL) { 4051 ath_printtxbuf(bf, sc->sc_bhalq, 0, 4052 ath_hal_txprocdesc(ah, bf->bf_desc) == HAL_OK); 4053 ieee80211_dump_pkt(mtod(bf->bf_m, caddr_t), 4054 bf->bf_m->m_len, 0, -1); 4055 } 4056 } 4057 #endif /* ATH_DEBUG */ 4058 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 4059 sc->sc_tx_timer = 0; 4060 } 4061 4062 /* 4063 * Disable the receive h/w in preparation for a reset. 4064 */ 4065 static void 4066 ath_stoprecv(struct ath_softc *sc) 4067 { 4068 #define PA2DESC(_sc, _pa) \ 4069 ((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \ 4070 ((_pa) - (_sc)->sc_rxdma.dd_desc_paddr))) 4071 struct ath_hal *ah = sc->sc_ah; 4072 4073 ath_hal_stoppcurecv(ah); /* disable PCU */ 4074 ath_hal_setrxfilter(ah, 0); /* clear recv filter */ 4075 ath_hal_stopdmarecv(ah); /* disable DMA engine */ 4076 DELAY(3000); /* 3ms is long enough for 1 frame */ 4077 #ifdef ATH_DEBUG 4078 if (sc->sc_debug & (ATH_DEBUG_RESET | ATH_DEBUG_FATAL)) { 4079 struct ath_buf *bf; 4080 u_int ix; 4081 4082 printf("%s: rx queue %p, link %p\n", __func__, 4083 (caddr_t)(uintptr_t) ath_hal_getrxbuf(ah), sc->sc_rxlink); 4084 ix = 0; 4085 STAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) { 4086 struct ath_desc *ds = bf->bf_desc; 4087 HAL_STATUS status = ath_hal_rxprocdesc(ah, ds, 4088 bf->bf_daddr, PA2DESC(sc, ds->ds_link)); 4089 if (status == HAL_OK || (sc->sc_debug & ATH_DEBUG_FATAL)) 4090 ath_printrxbuf(bf, ix, status == HAL_OK); 4091 ix++; 4092 } 4093 } 4094 #endif 4095 sc->sc_rxlink = NULL; /* just in case */ 4096 #undef PA2DESC 4097 } 4098 4099 /* 4100 * Enable the receive h/w following a reset. 4101 */ 4102 static int 4103 ath_startrecv(struct ath_softc *sc) 4104 { 4105 struct ath_hal *ah = sc->sc_ah; 4106 struct ath_buf *bf; 4107 4108 sc->sc_rxlink = NULL; 4109 STAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) { 4110 int error = ath_rxbuf_init(sc, bf); 4111 if (error != 0) { 4112 DPRINTF(sc, ATH_DEBUG_RECV, 4113 "%s: ath_rxbuf_init failed %d\n", 4114 __func__, error); 4115 return error; 4116 } 4117 } 4118 4119 bf = STAILQ_FIRST(&sc->sc_rxbuf); 4120 ath_hal_putrxbuf(ah, bf->bf_daddr); 4121 ath_hal_rxena(ah); /* enable recv descriptors */ 4122 ath_mode_init(sc); /* set filters, etc. */ 4123 ath_hal_startpcurecv(ah); /* re-enable PCU/DMA engine */ 4124 return 0; 4125 } 4126 4127 /* 4128 * Update internal state after a channel change. 4129 */ 4130 static void 4131 ath_chan_change(struct ath_softc *sc, struct ieee80211_channel *chan) 4132 { 4133 struct ieee80211com *ic = &sc->sc_ic; 4134 enum ieee80211_phymode mode; 4135 u_int16_t flags; 4136 4137 /* 4138 * Change channels and update the h/w rate map 4139 * if we're switching; e.g. 11a to 11b/g. 4140 */ 4141 mode = ieee80211_chan2mode(ic, chan); 4142 if (mode != sc->sc_curmode) 4143 ath_setcurmode(sc, mode); 4144 /* 4145 * Update BPF state. NB: ethereal et. al. don't handle 4146 * merged flags well so pick a unique mode for their use. 4147 */ 4148 if (IEEE80211_IS_CHAN_A(chan)) 4149 flags = IEEE80211_CHAN_A; 4150 /* XXX 11g schizophrenia */ 4151 else if (IEEE80211_IS_CHAN_G(chan) || 4152 IEEE80211_IS_CHAN_PUREG(chan)) 4153 flags = IEEE80211_CHAN_G; 4154 else 4155 flags = IEEE80211_CHAN_B; 4156 if (IEEE80211_IS_CHAN_T(chan)) 4157 flags |= IEEE80211_CHAN_TURBO; 4158 sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq = 4159 htole16(chan->ic_freq); 4160 sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags = 4161 htole16(flags); 4162 } 4163 4164 /* 4165 * Poll for a channel clear indication; this is required 4166 * for channels requiring DFS and not previously visited 4167 * and/or with a recent radar detection. 4168 */ 4169 static void 4170 ath_dfswait(void *arg) 4171 { 4172 struct ath_softc *sc = arg; 4173 struct ath_hal *ah = sc->sc_ah; 4174 HAL_CHANNEL hchan; 4175 4176 ath_hal_radar_wait(ah, &hchan); 4177 DPRINTF(sc, ATH_DEBUG_DFS, "%s: radar_wait %u/%x/%x\n", 4178 __func__, hchan.channel, hchan.channelFlags, hchan.privFlags); 4179 4180 if (hchan.privFlags & CHANNEL_INTERFERENCE) { 4181 if_printf(sc->sc_ifp, 4182 "channel %u/0x%x/0x%x has interference\n", 4183 hchan.channel, hchan.channelFlags, hchan.privFlags); 4184 return; 4185 } 4186 if ((hchan.privFlags & CHANNEL_DFS) == 0) { 4187 /* XXX should not happen */ 4188 return; 4189 } 4190 if (hchan.privFlags & CHANNEL_DFS_CLEAR) { 4191 sc->sc_curchan.privFlags |= CHANNEL_DFS_CLEAR; 4192 sc->sc_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 4193 if_printf(sc->sc_ifp, 4194 "channel %u/0x%x/0x%x marked clear\n", 4195 hchan.channel, hchan.channelFlags, hchan.privFlags); 4196 } else 4197 callout_reset(&sc->sc_dfs_ch, 2 * hz, ath_dfswait, sc); 4198 } 4199 4200 /* 4201 * Set/change channels. If the channel is really being changed, 4202 * it's done by reseting the chip. To accomplish this we must 4203 * first cleanup any pending DMA, then restart stuff after a la 4204 * ath_init. 4205 */ 4206 static int 4207 ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan) 4208 { 4209 struct ath_hal *ah = sc->sc_ah; 4210 struct ieee80211com *ic = &sc->sc_ic; 4211 HAL_CHANNEL hchan; 4212 4213 /* 4214 * Convert to a HAL channel description with 4215 * the flags constrained to reflect the current 4216 * operating mode. 4217 */ 4218 hchan.channel = chan->ic_freq; 4219 hchan.channelFlags = ath_chan2flags(ic, chan); 4220 4221 DPRINTF(sc, ATH_DEBUG_RESET, 4222 "%s: %u (%u MHz, hal flags 0x%x) -> %u (%u MHz, hal flags 0x%x)\n", 4223 __func__, 4224 ath_hal_mhz2ieee(ah, sc->sc_curchan.channel, 4225 sc->sc_curchan.channelFlags), 4226 sc->sc_curchan.channel, sc->sc_curchan.channelFlags, 4227 ath_hal_mhz2ieee(ah, hchan.channel, hchan.channelFlags), 4228 hchan.channel, hchan.channelFlags); 4229 if (hchan.channel != sc->sc_curchan.channel || 4230 hchan.channelFlags != sc->sc_curchan.channelFlags) { 4231 HAL_STATUS status; 4232 4233 /* 4234 * To switch channels clear any pending DMA operations; 4235 * wait long enough for the RX fifo to drain, reset the 4236 * hardware at the new frequency, and then re-enable 4237 * the relevant bits of the h/w. 4238 */ 4239 ath_hal_intrset(ah, 0); /* disable interrupts */ 4240 ath_draintxq(sc); /* clear pending tx frames */ 4241 ath_stoprecv(sc); /* turn off frame recv */ 4242 if (!ath_hal_reset(ah, sc->sc_opmode, &hchan, AH_TRUE, &status)) { 4243 if_printf(ic->ic_ifp, "%s: unable to reset " 4244 "channel %u (%u Mhz, flags 0x%x hal flags 0x%x)\n", 4245 __func__, ieee80211_chan2ieee(ic, chan), 4246 chan->ic_freq, chan->ic_flags, hchan.channelFlags); 4247 return EIO; 4248 } 4249 sc->sc_curchan = hchan; 4250 ath_update_txpow(sc); /* update tx power state */ 4251 sc->sc_diversity = ath_hal_getdiversity(ah); 4252 sc->sc_calinterval = 1; 4253 sc->sc_caltries = 0; 4254 4255 /* 4256 * Re-enable rx framework. 4257 */ 4258 if (ath_startrecv(sc) != 0) { 4259 if_printf(ic->ic_ifp, 4260 "%s: unable to restart recv logic\n", __func__); 4261 return EIO; 4262 } 4263 4264 /* 4265 * Change channels and update the h/w rate map 4266 * if we're switching; e.g. 11a to 11b/g. 4267 */ 4268 ic->ic_ibss_chan = chan; 4269 ath_chan_change(sc, chan); 4270 4271 /* 4272 * Handle DFS required waiting period to determine 4273 * if channel is clear of radar traffic. 4274 */ 4275 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 4276 #define DFS_AND_NOT_CLEAR(_c) \ 4277 (((_c)->privFlags & (CHANNEL_DFS | CHANNEL_DFS_CLEAR)) == CHANNEL_DFS) 4278 if (DFS_AND_NOT_CLEAR(&sc->sc_curchan)) { 4279 if_printf(sc->sc_ifp, 4280 "wait for DFS clear channel signal\n"); 4281 /* XXX stop sndq */ 4282 sc->sc_ifp->if_drv_flags |= IFF_DRV_OACTIVE; 4283 callout_reset(&sc->sc_dfs_ch, 4284 2 * hz, ath_dfswait, sc); 4285 } else 4286 callout_stop(&sc->sc_dfs_ch); 4287 #undef DFS_NOT_CLEAR 4288 } 4289 4290 /* 4291 * Re-enable interrupts. 4292 */ 4293 ath_hal_intrset(ah, sc->sc_imask); 4294 } 4295 return 0; 4296 } 4297 4298 static void 4299 ath_next_scan(void *arg) 4300 { 4301 struct ath_softc *sc = arg; 4302 struct ieee80211com *ic = &sc->sc_ic; 4303 4304 if (ic->ic_state == IEEE80211_S_SCAN) 4305 ieee80211_next_scan(ic); 4306 } 4307 4308 /* 4309 * Periodically recalibrate the PHY to account 4310 * for temperature/environment changes. 4311 */ 4312 static void 4313 ath_calibrate(void *arg) 4314 { 4315 struct ath_softc *sc = arg; 4316 struct ath_hal *ah = sc->sc_ah; 4317 HAL_BOOL iqCalDone; 4318 4319 sc->sc_stats.ast_per_cal++; 4320 4321 if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) { 4322 /* 4323 * Rfgain is out of bounds, reset the chip 4324 * to load new gain values. 4325 */ 4326 DPRINTF(sc, ATH_DEBUG_CALIBRATE, 4327 "%s: rfgain change\n", __func__); 4328 sc->sc_stats.ast_per_rfgain++; 4329 ath_reset(sc->sc_ifp); 4330 } 4331 if (!ath_hal_calibrate(ah, &sc->sc_curchan, &iqCalDone)) { 4332 DPRINTF(sc, ATH_DEBUG_ANY, 4333 "%s: calibration of channel %u failed\n", 4334 __func__, sc->sc_curchan.channel); 4335 sc->sc_stats.ast_per_calfail++; 4336 } 4337 /* 4338 * Calibrate noise floor data again in case of change. 4339 */ 4340 ath_hal_process_noisefloor(ah); 4341 /* 4342 * Poll more frequently when the IQ calibration is in 4343 * progress to speedup loading the final settings. 4344 * We temper this aggressive polling with an exponential 4345 * back off after 4 tries up to ath_calinterval. 4346 */ 4347 if (iqCalDone || sc->sc_calinterval >= ath_calinterval) { 4348 sc->sc_caltries = 0; 4349 sc->sc_calinterval = ath_calinterval; 4350 } else if (sc->sc_caltries > 4) { 4351 sc->sc_caltries = 0; 4352 sc->sc_calinterval <<= 1; 4353 if (sc->sc_calinterval > ath_calinterval) 4354 sc->sc_calinterval = ath_calinterval; 4355 } 4356 KASSERT(0 < sc->sc_calinterval && sc->sc_calinterval <= ath_calinterval, 4357 ("bad calibration interval %u", sc->sc_calinterval)); 4358 4359 DPRINTF(sc, ATH_DEBUG_CALIBRATE, 4360 "%s: next +%u (%siqCalDone tries %u)\n", __func__, 4361 sc->sc_calinterval, iqCalDone ? "" : "!", sc->sc_caltries); 4362 sc->sc_caltries++; 4363 callout_reset(&sc->sc_cal_ch, sc->sc_calinterval * hz, 4364 ath_calibrate, sc); 4365 } 4366 4367 static int 4368 ath_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 4369 { 4370 struct ifnet *ifp = ic->ic_ifp; 4371 struct ath_softc *sc = ifp->if_softc; 4372 struct ath_hal *ah = sc->sc_ah; 4373 struct ieee80211_node *ni; 4374 int i, error; 4375 const u_int8_t *bssid; 4376 u_int32_t rfilt; 4377 static const HAL_LED_STATE leds[] = { 4378 HAL_LED_INIT, /* IEEE80211_S_INIT */ 4379 HAL_LED_SCAN, /* IEEE80211_S_SCAN */ 4380 HAL_LED_AUTH, /* IEEE80211_S_AUTH */ 4381 HAL_LED_ASSOC, /* IEEE80211_S_ASSOC */ 4382 HAL_LED_RUN, /* IEEE80211_S_RUN */ 4383 }; 4384 4385 DPRINTF(sc, ATH_DEBUG_STATE, "%s: %s -> %s\n", __func__, 4386 ieee80211_state_name[ic->ic_state], 4387 ieee80211_state_name[nstate]); 4388 4389 callout_stop(&sc->sc_scan_ch); 4390 callout_stop(&sc->sc_cal_ch); 4391 callout_stop(&sc->sc_dfs_ch); 4392 ath_hal_setledstate(ah, leds[nstate]); /* set LED */ 4393 4394 if (nstate == IEEE80211_S_INIT) { 4395 sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS); 4396 /* 4397 * NB: disable interrupts so we don't rx frames. 4398 */ 4399 ath_hal_intrset(ah, sc->sc_imask &~ HAL_INT_GLOBAL); 4400 /* 4401 * Notify the rate control algorithm. 4402 */ 4403 ath_rate_newstate(sc, nstate); 4404 goto done; 4405 } 4406 ni = ic->ic_bss; 4407 error = ath_chan_set(sc, ic->ic_curchan); 4408 if (error != 0) 4409 goto bad; 4410 rfilt = ath_calcrxfilter(sc, nstate); 4411 if (nstate == IEEE80211_S_SCAN) 4412 bssid = ifp->if_broadcastaddr; 4413 else 4414 bssid = ni->ni_bssid; 4415 ath_hal_setrxfilter(ah, rfilt); 4416 DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s\n", 4417 __func__, rfilt, ether_sprintf(bssid)); 4418 4419 if (nstate == IEEE80211_S_RUN && ic->ic_opmode == IEEE80211_M_STA) 4420 ath_hal_setassocid(ah, bssid, ni->ni_associd); 4421 else 4422 ath_hal_setassocid(ah, bssid, 0); 4423 if (ic->ic_flags & IEEE80211_F_PRIVACY) { 4424 for (i = 0; i < IEEE80211_WEP_NKID; i++) 4425 if (ath_hal_keyisvalid(ah, i)) 4426 ath_hal_keysetmac(ah, i, bssid); 4427 } 4428 4429 /* 4430 * Notify the rate control algorithm so rates 4431 * are setup should ath_beacon_alloc be called. 4432 */ 4433 ath_rate_newstate(sc, nstate); 4434 4435 if (ic->ic_opmode == IEEE80211_M_MONITOR) { 4436 /* nothing to do */; 4437 } else if (nstate == IEEE80211_S_RUN) { 4438 DPRINTF(sc, ATH_DEBUG_STATE, 4439 "%s(RUN): ic_flags=0x%08x iv=%d bssid=%s " 4440 "capinfo=0x%04x chan=%d\n" 4441 , __func__ 4442 , ic->ic_flags 4443 , ni->ni_intval 4444 , ether_sprintf(ni->ni_bssid) 4445 , ni->ni_capinfo 4446 , ieee80211_chan2ieee(ic, ic->ic_curchan)); 4447 4448 switch (ic->ic_opmode) { 4449 case IEEE80211_M_HOSTAP: 4450 case IEEE80211_M_IBSS: 4451 /* 4452 * Allocate and setup the beacon frame. 4453 * 4454 * Stop any previous beacon DMA. This may be 4455 * necessary, for example, when an ibss merge 4456 * causes reconfiguration; there will be a state 4457 * transition from RUN->RUN that means we may 4458 * be called with beacon transmission active. 4459 */ 4460 ath_hal_stoptxdma(ah, sc->sc_bhalq); 4461 ath_beacon_free(sc); 4462 error = ath_beacon_alloc(sc, ni); 4463 if (error != 0) 4464 goto bad; 4465 /* 4466 * If joining an adhoc network defer beacon timer 4467 * configuration to the next beacon frame so we 4468 * have a current TSF to use. Otherwise we're 4469 * starting an ibss/bss so there's no need to delay. 4470 */ 4471 if (ic->ic_opmode == IEEE80211_M_IBSS && 4472 ic->ic_bss->ni_tstamp.tsf != 0) 4473 sc->sc_syncbeacon = 1; 4474 else 4475 ath_beacon_config(sc); 4476 break; 4477 case IEEE80211_M_STA: 4478 /* 4479 * Allocate a key cache slot to the station. 4480 */ 4481 if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0 && 4482 sc->sc_hasclrkey && 4483 ni->ni_ucastkey.wk_keyix == IEEE80211_KEYIX_NONE) 4484 ath_setup_stationkey(ni); 4485 /* 4486 * Defer beacon timer configuration to the next 4487 * beacon frame so we have a current TSF to use 4488 * (any TSF collected when scanning is likely old). 4489 */ 4490 sc->sc_syncbeacon = 1; 4491 break; 4492 default: 4493 break; 4494 } 4495 4496 /* 4497 * Let the hal process statistics collected during a 4498 * scan so it can provide calibrated noise floor data. 4499 */ 4500 ath_hal_process_noisefloor(ah); 4501 /* 4502 * Reset rssi stats; maybe not the best place... 4503 */ 4504 sc->sc_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER; 4505 sc->sc_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER; 4506 sc->sc_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER; 4507 } else { 4508 ath_hal_intrset(ah, 4509 sc->sc_imask &~ (HAL_INT_SWBA | HAL_INT_BMISS)); 4510 sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS); 4511 } 4512 done: 4513 /* 4514 * Invoke the parent method to complete the work. 4515 */ 4516 error = sc->sc_newstate(ic, nstate, arg); 4517 /* 4518 * Finally, start any timers. 4519 */ 4520 if (nstate == IEEE80211_S_RUN) { 4521 /* start periodic recalibration timer */ 4522 callout_reset(&sc->sc_cal_ch, sc->sc_calinterval * hz, 4523 ath_calibrate, sc); 4524 } else if (nstate == IEEE80211_S_SCAN) { 4525 /* start ap/neighbor scan timer */ 4526 callout_reset(&sc->sc_scan_ch, (ath_dwelltime * hz) / 1000, 4527 ath_next_scan, sc); 4528 } 4529 bad: 4530 return error; 4531 } 4532 4533 /* 4534 * Allocate a key cache slot to the station so we can 4535 * setup a mapping from key index to node. The key cache 4536 * slot is needed for managing antenna state and for 4537 * compression when stations do not use crypto. We do 4538 * it uniliaterally here; if crypto is employed this slot 4539 * will be reassigned. 4540 */ 4541 static void 4542 ath_setup_stationkey(struct ieee80211_node *ni) 4543 { 4544 struct ieee80211com *ic = ni->ni_ic; 4545 struct ath_softc *sc = ic->ic_ifp->if_softc; 4546 ieee80211_keyix keyix, rxkeyix; 4547 4548 if (!ath_key_alloc(ic, &ni->ni_ucastkey, &keyix, &rxkeyix)) { 4549 /* 4550 * Key cache is full; we'll fall back to doing 4551 * the more expensive lookup in software. Note 4552 * this also means no h/w compression. 4553 */ 4554 /* XXX msg+statistic */ 4555 } else { 4556 /* XXX locking? */ 4557 ni->ni_ucastkey.wk_keyix = keyix; 4558 ni->ni_ucastkey.wk_rxkeyix = rxkeyix; 4559 /* NB: this will create a pass-thru key entry */ 4560 ath_keyset(sc, &ni->ni_ucastkey, ni->ni_macaddr, ic->ic_bss); 4561 } 4562 } 4563 4564 /* 4565 * Setup driver-specific state for a newly associated node. 4566 * Note that we're called also on a re-associate, the isnew 4567 * param tells us if this is the first time or not. 4568 */ 4569 static void 4570 ath_newassoc(struct ieee80211_node *ni, int isnew) 4571 { 4572 struct ieee80211com *ic = ni->ni_ic; 4573 struct ath_softc *sc = ic->ic_ifp->if_softc; 4574 4575 ath_rate_newassoc(sc, ATH_NODE(ni), isnew); 4576 if (isnew && 4577 (ic->ic_flags & IEEE80211_F_PRIVACY) == 0 && sc->sc_hasclrkey) { 4578 KASSERT(ni->ni_ucastkey.wk_keyix == IEEE80211_KEYIX_NONE, 4579 ("new assoc with a unicast key already setup (keyix %u)", 4580 ni->ni_ucastkey.wk_keyix)); 4581 ath_setup_stationkey(ni); 4582 } 4583 } 4584 4585 static int 4586 ath_getchannels(struct ath_softc *sc, u_int cc, 4587 HAL_BOOL outdoor, HAL_BOOL xchanmode) 4588 { 4589 #define COMPAT (CHANNEL_ALL_NOTURBO|CHANNEL_PASSIVE) 4590 #define IS_CHAN_PUBLIC_SAFETY(_c) \ 4591 (((_c)->channelFlags & CHANNEL_5GHZ) && \ 4592 ((_c)->channel > 4940 && (_c)->channel < 4990)) 4593 struct ieee80211com *ic = &sc->sc_ic; 4594 struct ifnet *ifp = sc->sc_ifp; 4595 struct ath_hal *ah = sc->sc_ah; 4596 HAL_CHANNEL *chans; 4597 int i, ix, nchan; 4598 4599 chans = malloc(IEEE80211_CHAN_MAX * sizeof(HAL_CHANNEL), 4600 M_TEMP, M_NOWAIT); 4601 if (chans == NULL) { 4602 if_printf(ifp, "unable to allocate channel table\n"); 4603 return ENOMEM; 4604 } 4605 if (!ath_hal_init_channels(ah, chans, IEEE80211_CHAN_MAX, &nchan, 4606 NULL, 0, NULL, 4607 cc, HAL_MODE_ALL, outdoor, xchanmode)) { 4608 u_int32_t rd; 4609 4610 ath_hal_getregdomain(ah, &rd); 4611 if_printf(ifp, "unable to collect channel list from hal; " 4612 "regdomain likely %u country code %u\n", rd, cc); 4613 free(chans, M_TEMP); 4614 return EINVAL; 4615 } 4616 4617 /* 4618 * Convert HAL channels to ieee80211 ones and insert 4619 * them in the table according to their channel number. 4620 */ 4621 for (i = 0; i < nchan; i++) { 4622 HAL_CHANNEL *c = &chans[i]; 4623 u_int16_t flags; 4624 4625 /* 4626 * XXX we're not ready to handle the ieee number mapping 4627 * for public safety channels as they overlap with any 4628 * 2GHz channels; for now use the non-public safety 4629 * numbering which is non-overlapping. 4630 */ 4631 if (IS_CHAN_PUBLIC_SAFETY(c)) 4632 ix = (c->channel - 4000) / 5; 4633 else 4634 ix = ath_hal_mhz2ieee(ah, c->channel, c->channelFlags); 4635 if (ix > IEEE80211_CHAN_MAX) { 4636 if_printf(ifp, "bad hal channel %d (%u/%x) ignored\n", 4637 ix, c->channel, c->channelFlags); 4638 continue; 4639 } 4640 if (ix < 0) { 4641 /* XXX can't handle stuff <2400 right now */ 4642 if (bootverbose) 4643 if_printf(ifp, "hal channel %d (%u/%x) " 4644 "cannot be handled; ignored\n", 4645 ix, c->channel, c->channelFlags); 4646 continue; 4647 } 4648 /* 4649 * Calculate net80211 flags; most are compatible 4650 * but some need massaging. Note the static turbo 4651 * conversion can be removed once net80211 is updated 4652 * to understand static vs. dynamic turbo. 4653 */ 4654 flags = c->channelFlags & COMPAT; 4655 if (c->channelFlags & CHANNEL_STURBO) 4656 flags |= IEEE80211_CHAN_TURBO; 4657 if (ic->ic_channels[ix].ic_freq == 0) { 4658 ic->ic_channels[ix].ic_freq = c->channel; 4659 ic->ic_channels[ix].ic_flags = flags; 4660 } else { 4661 /* channels overlap; e.g. 11g and 11b */ 4662 ic->ic_channels[ix].ic_flags |= flags; 4663 } 4664 } 4665 free(chans, M_TEMP); 4666 return 0; 4667 #undef IS_CHAN_PUBLIC_SAFETY 4668 #undef COMPAT 4669 } 4670 4671 static void 4672 ath_led_done(void *arg) 4673 { 4674 struct ath_softc *sc = arg; 4675 4676 sc->sc_blinking = 0; 4677 } 4678 4679 /* 4680 * Turn the LED off: flip the pin and then set a timer so no 4681 * update will happen for the specified duration. 4682 */ 4683 static void 4684 ath_led_off(void *arg) 4685 { 4686 struct ath_softc *sc = arg; 4687 4688 ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, !sc->sc_ledon); 4689 callout_reset(&sc->sc_ledtimer, sc->sc_ledoff, ath_led_done, sc); 4690 } 4691 4692 /* 4693 * Blink the LED according to the specified on/off times. 4694 */ 4695 static void 4696 ath_led_blink(struct ath_softc *sc, int on, int off) 4697 { 4698 DPRINTF(sc, ATH_DEBUG_LED, "%s: on %u off %u\n", __func__, on, off); 4699 ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, sc->sc_ledon); 4700 sc->sc_blinking = 1; 4701 sc->sc_ledoff = off; 4702 callout_reset(&sc->sc_ledtimer, on, ath_led_off, sc); 4703 } 4704 4705 static void 4706 ath_led_event(struct ath_softc *sc, int event) 4707 { 4708 4709 sc->sc_ledevent = ticks; /* time of last event */ 4710 if (sc->sc_blinking) /* don't interrupt active blink */ 4711 return; 4712 switch (event) { 4713 case ATH_LED_POLL: 4714 ath_led_blink(sc, sc->sc_hwmap[0].ledon, 4715 sc->sc_hwmap[0].ledoff); 4716 break; 4717 case ATH_LED_TX: 4718 ath_led_blink(sc, sc->sc_hwmap[sc->sc_txrate].ledon, 4719 sc->sc_hwmap[sc->sc_txrate].ledoff); 4720 break; 4721 case ATH_LED_RX: 4722 ath_led_blink(sc, sc->sc_hwmap[sc->sc_rxrate].ledon, 4723 sc->sc_hwmap[sc->sc_rxrate].ledoff); 4724 break; 4725 } 4726 } 4727 4728 static void 4729 ath_update_txpow(struct ath_softc *sc) 4730 { 4731 struct ieee80211com *ic = &sc->sc_ic; 4732 struct ath_hal *ah = sc->sc_ah; 4733 u_int32_t txpow; 4734 4735 if (sc->sc_curtxpow != ic->ic_txpowlimit) { 4736 ath_hal_settxpowlimit(ah, ic->ic_txpowlimit); 4737 /* read back in case value is clamped */ 4738 ath_hal_gettxpowlimit(ah, &txpow); 4739 ic->ic_txpowlimit = sc->sc_curtxpow = txpow; 4740 } 4741 /* 4742 * Fetch max tx power level for status requests. 4743 */ 4744 ath_hal_getmaxtxpow(sc->sc_ah, &txpow); 4745 ic->ic_bss->ni_txpower = txpow; 4746 } 4747 4748 static void 4749 rate_setup(struct ath_softc *sc, 4750 const HAL_RATE_TABLE *rt, struct ieee80211_rateset *rs) 4751 { 4752 int i, maxrates; 4753 4754 if (rt->rateCount > IEEE80211_RATE_MAXSIZE) { 4755 DPRINTF(sc, ATH_DEBUG_ANY, 4756 "%s: rate table too small (%u > %u)\n", 4757 __func__, rt->rateCount, IEEE80211_RATE_MAXSIZE); 4758 maxrates = IEEE80211_RATE_MAXSIZE; 4759 } else 4760 maxrates = rt->rateCount; 4761 for (i = 0; i < maxrates; i++) 4762 rs->rs_rates[i] = rt->info[i].dot11Rate; 4763 rs->rs_nrates = maxrates; 4764 } 4765 4766 static int 4767 ath_rate_setup(struct ath_softc *sc, u_int mode) 4768 { 4769 struct ath_hal *ah = sc->sc_ah; 4770 struct ieee80211com *ic = &sc->sc_ic; 4771 const HAL_RATE_TABLE *rt; 4772 4773 switch (mode) { 4774 case IEEE80211_MODE_11A: 4775 rt = ath_hal_getratetable(ah, HAL_MODE_11A); 4776 break; 4777 case IEEE80211_MODE_11B: 4778 rt = ath_hal_getratetable(ah, HAL_MODE_11B); 4779 break; 4780 case IEEE80211_MODE_11G: 4781 rt = ath_hal_getratetable(ah, HAL_MODE_11G); 4782 break; 4783 case IEEE80211_MODE_TURBO_A: 4784 /* XXX until static/dynamic turbo is fixed */ 4785 rt = ath_hal_getratetable(ah, HAL_MODE_TURBO); 4786 break; 4787 case IEEE80211_MODE_TURBO_G: 4788 rt = ath_hal_getratetable(ah, HAL_MODE_108G); 4789 break; 4790 default: 4791 DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid mode %u\n", 4792 __func__, mode); 4793 return 0; 4794 } 4795 sc->sc_rates[mode] = rt; 4796 if (rt != NULL) { 4797 rate_setup(sc, rt, &ic->ic_sup_rates[mode]); 4798 return 1; 4799 } else 4800 return 0; 4801 } 4802 4803 static void 4804 ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode) 4805 { 4806 #define N(a) (sizeof(a)/sizeof(a[0])) 4807 /* NB: on/off times from the Atheros NDIS driver, w/ permission */ 4808 static const struct { 4809 u_int rate; /* tx/rx 802.11 rate */ 4810 u_int16_t timeOn; /* LED on time (ms) */ 4811 u_int16_t timeOff; /* LED off time (ms) */ 4812 } blinkrates[] = { 4813 { 108, 40, 10 }, 4814 { 96, 44, 11 }, 4815 { 72, 50, 13 }, 4816 { 48, 57, 14 }, 4817 { 36, 67, 16 }, 4818 { 24, 80, 20 }, 4819 { 22, 100, 25 }, 4820 { 18, 133, 34 }, 4821 { 12, 160, 40 }, 4822 { 10, 200, 50 }, 4823 { 6, 240, 58 }, 4824 { 4, 267, 66 }, 4825 { 2, 400, 100 }, 4826 { 0, 500, 130 }, 4827 }; 4828 const HAL_RATE_TABLE *rt; 4829 int i, j; 4830 4831 memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap)); 4832 rt = sc->sc_rates[mode]; 4833 KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode)); 4834 for (i = 0; i < rt->rateCount; i++) 4835 sc->sc_rixmap[rt->info[i].dot11Rate & IEEE80211_RATE_VAL] = i; 4836 memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap)); 4837 for (i = 0; i < 32; i++) { 4838 u_int8_t ix = rt->rateCodeToIndex[i]; 4839 if (ix == 0xff) { 4840 sc->sc_hwmap[i].ledon = (500 * hz) / 1000; 4841 sc->sc_hwmap[i].ledoff = (130 * hz) / 1000; 4842 continue; 4843 } 4844 sc->sc_hwmap[i].ieeerate = 4845 rt->info[ix].dot11Rate & IEEE80211_RATE_VAL; 4846 sc->sc_hwmap[i].txflags = IEEE80211_RADIOTAP_F_DATAPAD; 4847 if (rt->info[ix].shortPreamble || 4848 rt->info[ix].phy == IEEE80211_T_OFDM) 4849 sc->sc_hwmap[i].txflags |= IEEE80211_RADIOTAP_F_SHORTPRE; 4850 /* NB: receive frames include FCS */ 4851 sc->sc_hwmap[i].rxflags = sc->sc_hwmap[i].txflags | 4852 IEEE80211_RADIOTAP_F_FCS; 4853 /* setup blink rate table to avoid per-packet lookup */ 4854 for (j = 0; j < N(blinkrates)-1; j++) 4855 if (blinkrates[j].rate == sc->sc_hwmap[i].ieeerate) 4856 break; 4857 /* NB: this uses the last entry if the rate isn't found */ 4858 /* XXX beware of overlow */ 4859 sc->sc_hwmap[i].ledon = (blinkrates[j].timeOn * hz) / 1000; 4860 sc->sc_hwmap[i].ledoff = (blinkrates[j].timeOff * hz) / 1000; 4861 } 4862 sc->sc_currates = rt; 4863 sc->sc_curmode = mode; 4864 /* 4865 * All protection frames are transmited at 2Mb/s for 4866 * 11g, otherwise at 1Mb/s. 4867 */ 4868 if (mode == IEEE80211_MODE_11G) 4869 sc->sc_protrix = ath_tx_findrix(rt, 2*2); 4870 else 4871 sc->sc_protrix = ath_tx_findrix(rt, 2*1); 4872 /* rate index used to send management frames */ 4873 sc->sc_minrateix = 0; 4874 /* 4875 * Setup multicast rate state. 4876 */ 4877 /* XXX layering violation */ 4878 sc->sc_mcastrix = ath_tx_findrix(rt, sc->sc_ic.ic_mcast_rate); 4879 sc->sc_mcastrate = sc->sc_ic.ic_mcast_rate; 4880 /* NB: caller is responsible for reseting rate control state */ 4881 #undef N 4882 } 4883 4884 #ifdef ATH_DEBUG 4885 static void 4886 ath_printrxbuf(struct ath_buf *bf, u_int ix, int done) 4887 { 4888 struct ath_desc *ds; 4889 int i; 4890 4891 for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) { 4892 printf("R[%2u] (DS.V:%p DS.P:%p) L:%08x D:%08x%s\n" 4893 " %08x %08x %08x %08x\n", 4894 ix, ds, (struct ath_desc *)bf->bf_daddr + i, 4895 ds->ds_link, ds->ds_data, 4896 !done ? "" : (ds->ds_rxstat.rs_status == 0) ? " *" : " !", 4897 ds->ds_ctl0, ds->ds_ctl1, 4898 ds->ds_hw[0], ds->ds_hw[1]); 4899 } 4900 } 4901 4902 static void 4903 ath_printtxbuf(struct ath_buf *bf, u_int qnum, u_int ix, int done) 4904 { 4905 struct ath_desc *ds; 4906 int i; 4907 4908 printf("Q%u[%3u]", qnum, ix); 4909 for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) { 4910 printf(" (DS.V:%p DS.P:%p) L:%08x D:%08x F:04%x%s\n" 4911 " %08x %08x %08x %08x %08x %08x\n", 4912 ds, (struct ath_desc *)bf->bf_daddr + i, 4913 ds->ds_link, ds->ds_data, bf->bf_flags, 4914 !done ? "" : (ds->ds_txstat.ts_status == 0) ? " *" : " !", 4915 ds->ds_ctl0, ds->ds_ctl1, 4916 ds->ds_hw[0], ds->ds_hw[1], ds->ds_hw[2], ds->ds_hw[3]); 4917 } 4918 } 4919 #endif /* ATH_DEBUG */ 4920 4921 static void 4922 ath_watchdog(struct ifnet *ifp) 4923 { 4924 struct ath_softc *sc = ifp->if_softc; 4925 struct ieee80211com *ic = &sc->sc_ic; 4926 4927 ifp->if_timer = 0; 4928 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->sc_invalid) 4929 return; 4930 if (sc->sc_tx_timer) { 4931 if (--sc->sc_tx_timer == 0) { 4932 if_printf(ifp, "device timeout\n"); 4933 ath_reset(ifp); 4934 ifp->if_oerrors++; 4935 sc->sc_stats.ast_watchdog++; 4936 } else 4937 ifp->if_timer = 1; 4938 } 4939 ieee80211_watchdog(ic); 4940 } 4941 4942 #ifdef ATH_DIAGAPI 4943 /* 4944 * Diagnostic interface to the HAL. This is used by various 4945 * tools to do things like retrieve register contents for 4946 * debugging. The mechanism is intentionally opaque so that 4947 * it can change frequently w/o concern for compatiblity. 4948 */ 4949 static int 4950 ath_ioctl_diag(struct ath_softc *sc, struct ath_diag *ad) 4951 { 4952 struct ath_hal *ah = sc->sc_ah; 4953 u_int id = ad->ad_id & ATH_DIAG_ID; 4954 void *indata = NULL; 4955 void *outdata = NULL; 4956 u_int32_t insize = ad->ad_in_size; 4957 u_int32_t outsize = ad->ad_out_size; 4958 int error = 0; 4959 4960 if (ad->ad_id & ATH_DIAG_IN) { 4961 /* 4962 * Copy in data. 4963 */ 4964 indata = malloc(insize, M_TEMP, M_NOWAIT); 4965 if (indata == NULL) { 4966 error = ENOMEM; 4967 goto bad; 4968 } 4969 error = copyin(ad->ad_in_data, indata, insize); 4970 if (error) 4971 goto bad; 4972 } 4973 if (ad->ad_id & ATH_DIAG_DYN) { 4974 /* 4975 * Allocate a buffer for the results (otherwise the HAL 4976 * returns a pointer to a buffer where we can read the 4977 * results). Note that we depend on the HAL leaving this 4978 * pointer for us to use below in reclaiming the buffer; 4979 * may want to be more defensive. 4980 */ 4981 outdata = malloc(outsize, M_TEMP, M_NOWAIT); 4982 if (outdata == NULL) { 4983 error = ENOMEM; 4984 goto bad; 4985 } 4986 } 4987 if (ath_hal_getdiagstate(ah, id, indata, insize, &outdata, &outsize)) { 4988 if (outsize < ad->ad_out_size) 4989 ad->ad_out_size = outsize; 4990 if (outdata != NULL) 4991 error = copyout(outdata, ad->ad_out_data, 4992 ad->ad_out_size); 4993 } else { 4994 error = EINVAL; 4995 } 4996 bad: 4997 if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL) 4998 free(indata, M_TEMP); 4999 if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL) 5000 free(outdata, M_TEMP); 5001 return error; 5002 } 5003 #endif /* ATH_DIAGAPI */ 5004 5005 static int 5006 ath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 5007 { 5008 #define IS_RUNNING(ifp) \ 5009 ((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING)) 5010 struct ath_softc *sc = ifp->if_softc; 5011 struct ieee80211com *ic = &sc->sc_ic; 5012 struct ifreq *ifr = (struct ifreq *)data; 5013 int error = 0; 5014 5015 ATH_LOCK(sc); 5016 switch (cmd) { 5017 case SIOCSIFFLAGS: 5018 if (IS_RUNNING(ifp)) { 5019 /* 5020 * To avoid rescanning another access point, 5021 * do not call ath_init() here. Instead, 5022 * only reflect promisc mode settings. 5023 */ 5024 ath_mode_init(sc); 5025 } else if (ifp->if_flags & IFF_UP) { 5026 /* 5027 * Beware of being called during attach/detach 5028 * to reset promiscuous mode. In that case we 5029 * will still be marked UP but not RUNNING. 5030 * However trying to re-init the interface 5031 * is the wrong thing to do as we've already 5032 * torn down much of our state. There's 5033 * probably a better way to deal with this. 5034 */ 5035 if (!sc->sc_invalid && ic->ic_bss != NULL) 5036 ath_init(sc); /* XXX lose error */ 5037 } else 5038 ath_stop_locked(ifp); 5039 break; 5040 case SIOCADDMULTI: 5041 case SIOCDELMULTI: 5042 /* 5043 * The upper layer has already installed/removed 5044 * the multicast address(es), just recalculate the 5045 * multicast filter for the card. 5046 */ 5047 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 5048 ath_mode_init(sc); 5049 break; 5050 case SIOCGATHSTATS: 5051 /* NB: embed these numbers to get a consistent view */ 5052 sc->sc_stats.ast_tx_packets = ifp->if_opackets; 5053 sc->sc_stats.ast_rx_packets = ifp->if_ipackets; 5054 sc->sc_stats.ast_rx_rssi = ieee80211_getrssi(ic); 5055 ATH_UNLOCK(sc); 5056 /* 5057 * NB: Drop the softc lock in case of a page fault; 5058 * we'll accept any potential inconsisentcy in the 5059 * statistics. The alternative is to copy the data 5060 * to a local structure. 5061 */ 5062 return copyout(&sc->sc_stats, 5063 ifr->ifr_data, sizeof (sc->sc_stats)); 5064 #ifdef ATH_DIAGAPI 5065 case SIOCGATHDIAG: 5066 ATH_UNLOCK(sc); 5067 error = ath_ioctl_diag(sc, (struct ath_diag *) ifr); 5068 ATH_LOCK(sc); 5069 break; 5070 #endif 5071 default: 5072 error = ieee80211_ioctl(ic, cmd, data); 5073 if (error == ENETRESET) { 5074 if (IS_RUNNING(ifp) && 5075 ic->ic_roaming != IEEE80211_ROAMING_MANUAL) 5076 ath_init(sc); /* XXX lose error */ 5077 error = 0; 5078 } 5079 if (error == ERESTART) 5080 error = IS_RUNNING(ifp) ? ath_reset(ifp) : 0; 5081 break; 5082 } 5083 ATH_UNLOCK(sc); 5084 return error; 5085 #undef IS_RUNNING 5086 } 5087 5088 static int 5089 ath_sysctl_slottime(SYSCTL_HANDLER_ARGS) 5090 { 5091 struct ath_softc *sc = arg1; 5092 u_int slottime = ath_hal_getslottime(sc->sc_ah); 5093 int error; 5094 5095 error = sysctl_handle_int(oidp, &slottime, 0, req); 5096 if (error || !req->newptr) 5097 return error; 5098 return !ath_hal_setslottime(sc->sc_ah, slottime) ? EINVAL : 0; 5099 } 5100 5101 static int 5102 ath_sysctl_acktimeout(SYSCTL_HANDLER_ARGS) 5103 { 5104 struct ath_softc *sc = arg1; 5105 u_int acktimeout = ath_hal_getacktimeout(sc->sc_ah); 5106 int error; 5107 5108 error = sysctl_handle_int(oidp, &acktimeout, 0, req); 5109 if (error || !req->newptr) 5110 return error; 5111 return !ath_hal_setacktimeout(sc->sc_ah, acktimeout) ? EINVAL : 0; 5112 } 5113 5114 static int 5115 ath_sysctl_ctstimeout(SYSCTL_HANDLER_ARGS) 5116 { 5117 struct ath_softc *sc = arg1; 5118 u_int ctstimeout = ath_hal_getctstimeout(sc->sc_ah); 5119 int error; 5120 5121 error = sysctl_handle_int(oidp, &ctstimeout, 0, req); 5122 if (error || !req->newptr) 5123 return error; 5124 return !ath_hal_setctstimeout(sc->sc_ah, ctstimeout) ? EINVAL : 0; 5125 } 5126 5127 static int 5128 ath_sysctl_softled(SYSCTL_HANDLER_ARGS) 5129 { 5130 struct ath_softc *sc = arg1; 5131 int softled = sc->sc_softled; 5132 int error; 5133 5134 error = sysctl_handle_int(oidp, &softled, 0, req); 5135 if (error || !req->newptr) 5136 return error; 5137 softled = (softled != 0); 5138 if (softled != sc->sc_softled) { 5139 if (softled) { 5140 /* NB: handle any sc_ledpin change */ 5141 ath_hal_gpioCfgOutput(sc->sc_ah, sc->sc_ledpin); 5142 ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, 5143 !sc->sc_ledon); 5144 } 5145 sc->sc_softled = softled; 5146 } 5147 return 0; 5148 } 5149 5150 static int 5151 ath_sysctl_rxantenna(SYSCTL_HANDLER_ARGS) 5152 { 5153 struct ath_softc *sc = arg1; 5154 u_int defantenna = ath_hal_getdefantenna(sc->sc_ah); 5155 int error; 5156 5157 error = sysctl_handle_int(oidp, &defantenna, 0, req); 5158 if (!error && req->newptr) 5159 ath_hal_setdefantenna(sc->sc_ah, defantenna); 5160 return error; 5161 } 5162 5163 static int 5164 ath_sysctl_diversity(SYSCTL_HANDLER_ARGS) 5165 { 5166 struct ath_softc *sc = arg1; 5167 u_int diversity = ath_hal_getdiversity(sc->sc_ah); 5168 int error; 5169 5170 error = sysctl_handle_int(oidp, &diversity, 0, req); 5171 if (error || !req->newptr) 5172 return error; 5173 if (!ath_hal_setdiversity(sc->sc_ah, diversity)) 5174 return EINVAL; 5175 sc->sc_diversity = diversity; 5176 return 0; 5177 } 5178 5179 static int 5180 ath_sysctl_diag(SYSCTL_HANDLER_ARGS) 5181 { 5182 struct ath_softc *sc = arg1; 5183 u_int32_t diag; 5184 int error; 5185 5186 if (!ath_hal_getdiag(sc->sc_ah, &diag)) 5187 return EINVAL; 5188 error = sysctl_handle_int(oidp, &diag, 0, req); 5189 if (error || !req->newptr) 5190 return error; 5191 return !ath_hal_setdiag(sc->sc_ah, diag) ? EINVAL : 0; 5192 } 5193 5194 static int 5195 ath_sysctl_tpscale(SYSCTL_HANDLER_ARGS) 5196 { 5197 struct ath_softc *sc = arg1; 5198 struct ifnet *ifp = sc->sc_ifp; 5199 u_int32_t scale; 5200 int error; 5201 5202 ath_hal_gettpscale(sc->sc_ah, &scale); 5203 error = sysctl_handle_int(oidp, &scale, 0, req); 5204 if (error || !req->newptr) 5205 return error; 5206 return !ath_hal_settpscale(sc->sc_ah, scale) ? EINVAL : ath_reset(ifp); 5207 } 5208 5209 static int 5210 ath_sysctl_tpc(SYSCTL_HANDLER_ARGS) 5211 { 5212 struct ath_softc *sc = arg1; 5213 u_int tpc = ath_hal_gettpc(sc->sc_ah); 5214 int error; 5215 5216 error = sysctl_handle_int(oidp, &tpc, 0, req); 5217 if (error || !req->newptr) 5218 return error; 5219 return !ath_hal_settpc(sc->sc_ah, tpc) ? EINVAL : 0; 5220 } 5221 5222 static int 5223 ath_sysctl_rfkill(SYSCTL_HANDLER_ARGS) 5224 { 5225 struct ath_softc *sc = arg1; 5226 struct ath_hal *ah = sc->sc_ah; 5227 u_int rfkill = ath_hal_getrfkill(ah); 5228 int error; 5229 5230 error = sysctl_handle_int(oidp, &rfkill, 0, req); 5231 if (error || !req->newptr) 5232 return error; 5233 if (rfkill == ath_hal_getrfkill(ah)) /* unchanged */ 5234 return 0; 5235 if (!ath_hal_setrfkill(ah, rfkill) || ath_reset(sc->sc_ifp) != 0) 5236 return EINVAL; 5237 else 5238 return 0; 5239 } 5240 5241 static int 5242 ath_sysctl_rfsilent(SYSCTL_HANDLER_ARGS) 5243 { 5244 struct ath_softc *sc = arg1; 5245 u_int rfsilent; 5246 int error; 5247 5248 ath_hal_getrfsilent(sc->sc_ah, &rfsilent); 5249 error = sysctl_handle_int(oidp, &rfsilent, 0, req); 5250 if (error || !req->newptr) 5251 return error; 5252 if (!ath_hal_setrfsilent(sc->sc_ah, rfsilent)) 5253 return EINVAL; 5254 sc->sc_rfsilentpin = rfsilent & 0x1c; 5255 sc->sc_rfsilentpol = (rfsilent & 0x2) != 0; 5256 return 0; 5257 } 5258 5259 static int 5260 ath_sysctl_regdomain(SYSCTL_HANDLER_ARGS) 5261 { 5262 struct ath_softc *sc = arg1; 5263 u_int32_t rd; 5264 int error; 5265 5266 if (!ath_hal_getregdomain(sc->sc_ah, &rd)) 5267 return EINVAL; 5268 error = sysctl_handle_int(oidp, &rd, 0, req); 5269 if (error || !req->newptr) 5270 return error; 5271 return !ath_hal_setregdomain(sc->sc_ah, rd) ? EINVAL : 0; 5272 } 5273 5274 static int 5275 ath_sysctl_tpack(SYSCTL_HANDLER_ARGS) 5276 { 5277 struct ath_softc *sc = arg1; 5278 u_int32_t tpack; 5279 int error; 5280 5281 ath_hal_gettpack(sc->sc_ah, &tpack); 5282 error = sysctl_handle_int(oidp, &tpack, 0, req); 5283 if (error || !req->newptr) 5284 return error; 5285 return !ath_hal_settpack(sc->sc_ah, tpack) ? EINVAL : 0; 5286 } 5287 5288 static int 5289 ath_sysctl_tpcts(SYSCTL_HANDLER_ARGS) 5290 { 5291 struct ath_softc *sc = arg1; 5292 u_int32_t tpcts; 5293 int error; 5294 5295 ath_hal_gettpcts(sc->sc_ah, &tpcts); 5296 error = sysctl_handle_int(oidp, &tpcts, 0, req); 5297 if (error || !req->newptr) 5298 return error; 5299 return !ath_hal_settpcts(sc->sc_ah, tpcts) ? EINVAL : 0; 5300 } 5301 5302 static void 5303 ath_sysctlattach(struct ath_softc *sc) 5304 { 5305 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev); 5306 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev); 5307 struct ath_hal *ah = sc->sc_ah; 5308 5309 ath_hal_getcountrycode(sc->sc_ah, &sc->sc_countrycode); 5310 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 5311 "countrycode", CTLFLAG_RD, &sc->sc_countrycode, 0, 5312 "EEPROM country code"); 5313 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 5314 "regdomain", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 5315 ath_sysctl_regdomain, "I", "EEPROM regdomain code"); 5316 #ifdef ATH_DEBUG 5317 sc->sc_debug = ath_debug; 5318 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 5319 "debug", CTLFLAG_RW, &sc->sc_debug, 0, 5320 "control debugging printfs"); 5321 #endif 5322 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 5323 "slottime", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 5324 ath_sysctl_slottime, "I", "802.11 slot time (us)"); 5325 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 5326 "acktimeout", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 5327 ath_sysctl_acktimeout, "I", "802.11 ACK timeout (us)"); 5328 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 5329 "ctstimeout", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 5330 ath_sysctl_ctstimeout, "I", "802.11 CTS timeout (us)"); 5331 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 5332 "softled", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 5333 ath_sysctl_softled, "I", "enable/disable software LED support"); 5334 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 5335 "ledpin", CTLFLAG_RW, &sc->sc_ledpin, 0, 5336 "GPIO pin connected to LED"); 5337 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 5338 "ledon", CTLFLAG_RW, &sc->sc_ledon, 0, 5339 "setting to turn LED on"); 5340 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 5341 "ledidle", CTLFLAG_RW, &sc->sc_ledidle, 0, 5342 "idle time for inactivity LED (ticks)"); 5343 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 5344 "txantenna", CTLFLAG_RW, &sc->sc_txantenna, 0, 5345 "tx antenna (0=auto)"); 5346 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 5347 "rxantenna", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 5348 ath_sysctl_rxantenna, "I", "default/rx antenna"); 5349 if (ath_hal_hasdiversity(ah)) 5350 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 5351 "diversity", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 5352 ath_sysctl_diversity, "I", "antenna diversity"); 5353 sc->sc_txintrperiod = ATH_TXINTR_PERIOD; 5354 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 5355 "txintrperiod", CTLFLAG_RW, &sc->sc_txintrperiod, 0, 5356 "tx descriptor batching"); 5357 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 5358 "diag", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 5359 ath_sysctl_diag, "I", "h/w diagnostic control"); 5360 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 5361 "tpscale", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 5362 ath_sysctl_tpscale, "I", "tx power scaling"); 5363 if (ath_hal_hastpc(ah)) { 5364 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 5365 "tpc", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 5366 ath_sysctl_tpc, "I", "enable/disable per-packet TPC"); 5367 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 5368 "tpack", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 5369 ath_sysctl_tpack, "I", "tx power for ack frames"); 5370 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 5371 "tpcts", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 5372 ath_sysctl_tpcts, "I", "tx power for cts frames"); 5373 } 5374 if (ath_hal_hasrfsilent(ah)) { 5375 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 5376 "rfsilent", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 5377 ath_sysctl_rfsilent, "I", "h/w RF silent config"); 5378 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 5379 "rfkill", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 5380 ath_sysctl_rfkill, "I", "enable/disable RF kill switch"); 5381 } 5382 sc->sc_monpass = HAL_RXERR_DECRYPT | HAL_RXERR_MIC; 5383 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 5384 "monpass", CTLFLAG_RW, &sc->sc_monpass, 0, 5385 "mask of error frames to pass when monitoring"); 5386 } 5387 5388 static void 5389 ath_bpfattach(struct ath_softc *sc) 5390 { 5391 struct ifnet *ifp = sc->sc_ifp; 5392 5393 bpfattach2(ifp, DLT_IEEE802_11_RADIO, 5394 sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th), 5395 &sc->sc_drvbpf); 5396 /* 5397 * Initialize constant fields. 5398 * XXX make header lengths a multiple of 32-bits so subsequent 5399 * headers are properly aligned; this is a kludge to keep 5400 * certain applications happy. 5401 * 5402 * NB: the channel is setup each time we transition to the 5403 * RUN state to avoid filling it in for each frame. 5404 */ 5405 sc->sc_tx_th_len = roundup(sizeof(sc->sc_tx_th), sizeof(u_int32_t)); 5406 sc->sc_tx_th.wt_ihdr.it_len = htole16(sc->sc_tx_th_len); 5407 sc->sc_tx_th.wt_ihdr.it_present = htole32(ATH_TX_RADIOTAP_PRESENT); 5408 5409 sc->sc_rx_th_len = roundup(sizeof(sc->sc_rx_th), sizeof(u_int32_t)); 5410 sc->sc_rx_th.wr_ihdr.it_len = htole16(sc->sc_rx_th_len); 5411 sc->sc_rx_th.wr_ihdr.it_present = htole32(ATH_RX_RADIOTAP_PRESENT); 5412 } 5413 5414 /* 5415 * Announce various information on device/driver attach. 5416 */ 5417 static void 5418 ath_announce(struct ath_softc *sc) 5419 { 5420 #define HAL_MODE_DUALBAND (HAL_MODE_11A|HAL_MODE_11B) 5421 struct ifnet *ifp = sc->sc_ifp; 5422 struct ath_hal *ah = sc->sc_ah; 5423 u_int modes, cc; 5424 5425 if_printf(ifp, "mac %d.%d phy %d.%d", 5426 ah->ah_macVersion, ah->ah_macRev, 5427 ah->ah_phyRev >> 4, ah->ah_phyRev & 0xf); 5428 /* 5429 * Print radio revision(s). We check the wireless modes 5430 * to avoid falsely printing revs for inoperable parts. 5431 * Dual-band radio revs are returned in the 5Ghz rev number. 5432 */ 5433 ath_hal_getcountrycode(ah, &cc); 5434 modes = ath_hal_getwirelessmodes(ah, cc); 5435 if ((modes & HAL_MODE_DUALBAND) == HAL_MODE_DUALBAND) { 5436 if (ah->ah_analog5GhzRev && ah->ah_analog2GhzRev) 5437 printf(" 5ghz radio %d.%d 2ghz radio %d.%d", 5438 ah->ah_analog5GhzRev >> 4, 5439 ah->ah_analog5GhzRev & 0xf, 5440 ah->ah_analog2GhzRev >> 4, 5441 ah->ah_analog2GhzRev & 0xf); 5442 else 5443 printf(" radio %d.%d", ah->ah_analog5GhzRev >> 4, 5444 ah->ah_analog5GhzRev & 0xf); 5445 } else 5446 printf(" radio %d.%d", ah->ah_analog5GhzRev >> 4, 5447 ah->ah_analog5GhzRev & 0xf); 5448 printf("\n"); 5449 if (bootverbose) { 5450 int i; 5451 for (i = 0; i <= WME_AC_VO; i++) { 5452 struct ath_txq *txq = sc->sc_ac2q[i]; 5453 if_printf(ifp, "Use hw queue %u for %s traffic\n", 5454 txq->axq_qnum, ieee80211_wme_acnames[i]); 5455 } 5456 if_printf(ifp, "Use hw queue %u for CAB traffic\n", 5457 sc->sc_cabq->axq_qnum); 5458 if_printf(ifp, "Use hw queue %u for beacons\n", sc->sc_bhalq); 5459 } 5460 if (ath_rxbuf != ATH_RXBUF) 5461 if_printf(ifp, "using %u rx buffers\n", ath_rxbuf); 5462 if (ath_txbuf != ATH_TXBUF) 5463 if_printf(ifp, "using %u tx buffers\n", ath_txbuf); 5464 #undef HAL_MODE_DUALBAND 5465 } 5466