1 /*- 2 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer, 10 * without modification. 11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 13 * redistribution must be conditioned upon including a substantially 14 * similar Disclaimer requirement for further binary redistribution. 15 * 16 * NO WARRANTY 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27 * THE POSSIBILITY OF SUCH DAMAGES. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 /* 34 * Driver for the Atheros Wireless LAN controller. 35 * 36 * This software is derived from work of Atsushi Onoe; his contribution 37 * is greatly appreciated. 38 */ 39 40 #include "opt_inet.h" 41 #include "opt_ath.h" 42 /* 43 * This is needed for register operations which are performed 44 * by the driver - eg, calls to ath_hal_gettsf32(). 45 * 46 * It's also required for any AH_DEBUG checks in here, eg the 47 * module dependencies. 48 */ 49 #include "opt_ah.h" 50 #include "opt_wlan.h" 51 52 #include <sys/param.h> 53 #include <sys/systm.h> 54 #include <sys/sysctl.h> 55 #include <sys/mbuf.h> 56 #include <sys/malloc.h> 57 #include <sys/lock.h> 58 #include <sys/mutex.h> 59 #include <sys/kernel.h> 60 #include <sys/socket.h> 61 #include <sys/sockio.h> 62 #include <sys/errno.h> 63 #include <sys/callout.h> 64 #include <sys/bus.h> 65 #include <sys/endian.h> 66 #include <sys/kthread.h> 67 #include <sys/taskqueue.h> 68 #include <sys/priv.h> 69 #include <sys/module.h> 70 #include <sys/ktr.h> 71 #include <sys/smp.h> /* for mp_ncpus */ 72 73 #include <machine/bus.h> 74 75 #include <net/if.h> 76 #include <net/if_var.h> 77 #include <net/if_dl.h> 78 #include <net/if_media.h> 79 #include <net/if_types.h> 80 #include <net/if_arp.h> 81 #include <net/ethernet.h> 82 #include <net/if_llc.h> 83 84 #include <net80211/ieee80211_var.h> 85 #include <net80211/ieee80211_regdomain.h> 86 #ifdef IEEE80211_SUPPORT_SUPERG 87 #include <net80211/ieee80211_superg.h> 88 #endif 89 #ifdef IEEE80211_SUPPORT_TDMA 90 #include <net80211/ieee80211_tdma.h> 91 #endif 92 93 #include <net/bpf.h> 94 95 #ifdef INET 96 #include <netinet/in.h> 97 #include <netinet/if_ether.h> 98 #endif 99 100 #include <dev/ath/if_athvar.h> 101 #include <dev/ath/ath_hal/ah_devid.h> /* XXX for softled */ 102 #include <dev/ath/ath_hal/ah_diagcodes.h> 103 104 #include <dev/ath/if_ath_debug.h> 105 #include <dev/ath/if_ath_misc.h> 106 #include <dev/ath/if_ath_tsf.h> 107 #include <dev/ath/if_ath_tx.h> 108 #include <dev/ath/if_ath_sysctl.h> 109 #include <dev/ath/if_ath_led.h> 110 #include <dev/ath/if_ath_keycache.h> 111 #include <dev/ath/if_ath_rx.h> 112 #include <dev/ath/if_ath_rx_edma.h> 113 #include <dev/ath/if_ath_tx_edma.h> 114 #include <dev/ath/if_ath_beacon.h> 115 #include <dev/ath/if_ath_btcoex.h> 116 #include <dev/ath/if_ath_spectral.h> 117 #include <dev/ath/if_ath_lna_div.h> 118 #include <dev/ath/if_athdfs.h> 119 120 #ifdef ATH_TX99_DIAG 121 #include <dev/ath/ath_tx99/ath_tx99.h> 122 #endif 123 124 #ifdef ATH_DEBUG_ALQ 125 #include <dev/ath/if_ath_alq.h> 126 #endif 127 128 /* 129 * Only enable this if you're working on PS-POLL support. 130 */ 131 #define ATH_SW_PSQ 132 133 /* 134 * ATH_BCBUF determines the number of vap's that can transmit 135 * beacons and also (currently) the number of vap's that can 136 * have unique mac addresses/bssid. When staggering beacons 137 * 4 is probably a good max as otherwise the beacons become 138 * very closely spaced and there is limited time for cab q traffic 139 * to go out. You can burst beacons instead but that is not good 140 * for stations in power save and at some point you really want 141 * another radio (and channel). 142 * 143 * The limit on the number of mac addresses is tied to our use of 144 * the U/L bit and tracking addresses in a byte; it would be 145 * worthwhile to allow more for applications like proxy sta. 146 */ 147 CTASSERT(ATH_BCBUF <= 8); 148 149 static struct ieee80211vap *ath_vap_create(struct ieee80211com *, 150 const char [IFNAMSIZ], int, enum ieee80211_opmode, int, 151 const uint8_t [IEEE80211_ADDR_LEN], 152 const uint8_t [IEEE80211_ADDR_LEN]); 153 static void ath_vap_delete(struct ieee80211vap *); 154 static int ath_init(struct ath_softc *); 155 static void ath_stop(struct ath_softc *); 156 static int ath_reset_vap(struct ieee80211vap *, u_long); 157 static int ath_transmit(struct ieee80211com *, struct mbuf *); 158 static int ath_media_change(struct ifnet *); 159 static void ath_watchdog(void *); 160 static int ath_ioctl(struct ieee80211com *, u_long, void *); 161 static void ath_parent(struct ieee80211com *); 162 static void ath_fatal_proc(void *, int); 163 static void ath_bmiss_vap(struct ieee80211vap *); 164 static void ath_bmiss_proc(void *, int); 165 static void ath_key_update_begin(struct ieee80211vap *); 166 static void ath_key_update_end(struct ieee80211vap *); 167 static void ath_update_mcast_hw(struct ath_softc *); 168 static void ath_update_mcast(struct ieee80211com *); 169 static void ath_update_promisc(struct ieee80211com *); 170 static void ath_updateslot(struct ieee80211com *); 171 static void ath_bstuck_proc(void *, int); 172 static void ath_reset_proc(void *, int); 173 static int ath_desc_alloc(struct ath_softc *); 174 static void ath_desc_free(struct ath_softc *); 175 static struct ieee80211_node *ath_node_alloc(struct ieee80211vap *, 176 const uint8_t [IEEE80211_ADDR_LEN]); 177 static void ath_node_cleanup(struct ieee80211_node *); 178 static void ath_node_free(struct ieee80211_node *); 179 static void ath_node_getsignal(const struct ieee80211_node *, 180 int8_t *, int8_t *); 181 static void ath_txq_init(struct ath_softc *sc, struct ath_txq *, int); 182 static struct ath_txq *ath_txq_setup(struct ath_softc*, int qtype, int subtype); 183 static int ath_tx_setup(struct ath_softc *, int, int); 184 static void ath_tx_cleanupq(struct ath_softc *, struct ath_txq *); 185 static void ath_tx_cleanup(struct ath_softc *); 186 static int ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq, 187 int dosched); 188 static void ath_tx_proc_q0(void *, int); 189 static void ath_tx_proc_q0123(void *, int); 190 static void ath_tx_proc(void *, int); 191 static void ath_txq_sched_tasklet(void *, int); 192 static int ath_chan_set(struct ath_softc *, struct ieee80211_channel *); 193 static void ath_chan_change(struct ath_softc *, struct ieee80211_channel *); 194 static void ath_scan_start(struct ieee80211com *); 195 static void ath_scan_end(struct ieee80211com *); 196 static void ath_set_channel(struct ieee80211com *); 197 #ifdef ATH_ENABLE_11N 198 static void ath_update_chw(struct ieee80211com *); 199 #endif /* ATH_ENABLE_11N */ 200 static void ath_calibrate(void *); 201 static int ath_newstate(struct ieee80211vap *, enum ieee80211_state, int); 202 static void ath_setup_stationkey(struct ieee80211_node *); 203 static void ath_newassoc(struct ieee80211_node *, int); 204 static int ath_setregdomain(struct ieee80211com *, 205 struct ieee80211_regdomain *, int, 206 struct ieee80211_channel []); 207 static void ath_getradiocaps(struct ieee80211com *, int, int *, 208 struct ieee80211_channel []); 209 static int ath_getchannels(struct ath_softc *); 210 211 static int ath_rate_setup(struct ath_softc *, u_int mode); 212 static void ath_setcurmode(struct ath_softc *, enum ieee80211_phymode); 213 214 static void ath_announce(struct ath_softc *); 215 216 static void ath_dfs_tasklet(void *, int); 217 static void ath_node_powersave(struct ieee80211_node *, int); 218 static int ath_node_set_tim(struct ieee80211_node *, int); 219 static void ath_node_recv_pspoll(struct ieee80211_node *, struct mbuf *); 220 221 #ifdef IEEE80211_SUPPORT_TDMA 222 #include <dev/ath/if_ath_tdma.h> 223 #endif 224 225 SYSCTL_DECL(_hw_ath); 226 227 /* XXX validate sysctl values */ 228 static int ath_longcalinterval = 30; /* long cals every 30 secs */ 229 SYSCTL_INT(_hw_ath, OID_AUTO, longcal, CTLFLAG_RW, &ath_longcalinterval, 230 0, "long chip calibration interval (secs)"); 231 static int ath_shortcalinterval = 100; /* short cals every 100 ms */ 232 SYSCTL_INT(_hw_ath, OID_AUTO, shortcal, CTLFLAG_RW, &ath_shortcalinterval, 233 0, "short chip calibration interval (msecs)"); 234 static int ath_resetcalinterval = 20*60; /* reset cal state 20 mins */ 235 SYSCTL_INT(_hw_ath, OID_AUTO, resetcal, CTLFLAG_RW, &ath_resetcalinterval, 236 0, "reset chip calibration results (secs)"); 237 static int ath_anicalinterval = 100; /* ANI calibration - 100 msec */ 238 SYSCTL_INT(_hw_ath, OID_AUTO, anical, CTLFLAG_RW, &ath_anicalinterval, 239 0, "ANI calibration (msecs)"); 240 241 int ath_rxbuf = ATH_RXBUF; /* # rx buffers to allocate */ 242 SYSCTL_INT(_hw_ath, OID_AUTO, rxbuf, CTLFLAG_RWTUN, &ath_rxbuf, 243 0, "rx buffers allocated"); 244 int ath_txbuf = ATH_TXBUF; /* # tx buffers to allocate */ 245 SYSCTL_INT(_hw_ath, OID_AUTO, txbuf, CTLFLAG_RWTUN, &ath_txbuf, 246 0, "tx buffers allocated"); 247 int ath_txbuf_mgmt = ATH_MGMT_TXBUF; /* # mgmt tx buffers to allocate */ 248 SYSCTL_INT(_hw_ath, OID_AUTO, txbuf_mgmt, CTLFLAG_RWTUN, &ath_txbuf_mgmt, 249 0, "tx (mgmt) buffers allocated"); 250 251 int ath_bstuck_threshold = 4; /* max missed beacons */ 252 SYSCTL_INT(_hw_ath, OID_AUTO, bstuck, CTLFLAG_RW, &ath_bstuck_threshold, 253 0, "max missed beacon xmits before chip reset"); 254 255 MALLOC_DEFINE(M_ATHDEV, "athdev", "ath driver dma buffers"); 256 257 void 258 ath_legacy_attach_comp_func(struct ath_softc *sc) 259 { 260 261 /* 262 * Special case certain configurations. Note the 263 * CAB queue is handled by these specially so don't 264 * include them when checking the txq setup mask. 265 */ 266 switch (sc->sc_txqsetup &~ (1<<sc->sc_cabq->axq_qnum)) { 267 case 0x01: 268 TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0, sc); 269 break; 270 case 0x0f: 271 TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0123, sc); 272 break; 273 default: 274 TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc, sc); 275 break; 276 } 277 } 278 279 /* 280 * Set the target power mode. 281 * 282 * If this is called during a point in time where 283 * the hardware is being programmed elsewhere, it will 284 * simply store it away and update it when all current 285 * uses of the hardware are completed. 286 */ 287 void 288 _ath_power_setpower(struct ath_softc *sc, int power_state, const char *file, int line) 289 { 290 ATH_LOCK_ASSERT(sc); 291 292 sc->sc_target_powerstate = power_state; 293 294 DPRINTF(sc, ATH_DEBUG_PWRSAVE, "%s: (%s:%d) state=%d, refcnt=%d\n", 295 __func__, 296 file, 297 line, 298 power_state, 299 sc->sc_powersave_refcnt); 300 301 if (sc->sc_powersave_refcnt == 0 && 302 power_state != sc->sc_cur_powerstate) { 303 sc->sc_cur_powerstate = power_state; 304 ath_hal_setpower(sc->sc_ah, power_state); 305 306 /* 307 * If the NIC is force-awake, then set the 308 * self-gen frame state appropriately. 309 * 310 * If the nic is in network sleep or full-sleep, 311 * we let the above call leave the self-gen 312 * state as "sleep". 313 */ 314 if (sc->sc_cur_powerstate == HAL_PM_AWAKE && 315 sc->sc_target_selfgen_state != HAL_PM_AWAKE) { 316 ath_hal_setselfgenpower(sc->sc_ah, 317 sc->sc_target_selfgen_state); 318 } 319 } 320 } 321 322 /* 323 * Set the current self-generated frames state. 324 * 325 * This is separate from the target power mode. The chip may be 326 * awake but the desired state is "sleep", so frames sent to the 327 * destination has PWRMGT=1 in the 802.11 header. The NIC also 328 * needs to know to set PWRMGT=1 in self-generated frames. 329 */ 330 void 331 _ath_power_set_selfgen(struct ath_softc *sc, int power_state, const char *file, int line) 332 { 333 334 ATH_LOCK_ASSERT(sc); 335 336 DPRINTF(sc, ATH_DEBUG_PWRSAVE, "%s: (%s:%d) state=%d, refcnt=%d\n", 337 __func__, 338 file, 339 line, 340 power_state, 341 sc->sc_target_selfgen_state); 342 343 sc->sc_target_selfgen_state = power_state; 344 345 /* 346 * If the NIC is force-awake, then set the power state. 347 * Network-state and full-sleep will already transition it to 348 * mark self-gen frames as sleeping - and we can't 349 * guarantee the NIC is awake to program the self-gen frame 350 * setting anyway. 351 */ 352 if (sc->sc_cur_powerstate == HAL_PM_AWAKE) { 353 ath_hal_setselfgenpower(sc->sc_ah, power_state); 354 } 355 } 356 357 /* 358 * Set the hardware power mode and take a reference. 359 * 360 * This doesn't update the target power mode in the driver; 361 * it just updates the hardware power state. 362 * 363 * XXX it should only ever force the hardware awake; it should 364 * never be called to set it asleep. 365 */ 366 void 367 _ath_power_set_power_state(struct ath_softc *sc, int power_state, const char *file, int line) 368 { 369 ATH_LOCK_ASSERT(sc); 370 371 DPRINTF(sc, ATH_DEBUG_PWRSAVE, "%s: (%s:%d) state=%d, refcnt=%d\n", 372 __func__, 373 file, 374 line, 375 power_state, 376 sc->sc_powersave_refcnt); 377 378 sc->sc_powersave_refcnt++; 379 380 if (power_state != sc->sc_cur_powerstate) { 381 ath_hal_setpower(sc->sc_ah, power_state); 382 sc->sc_cur_powerstate = power_state; 383 384 /* 385 * Adjust the self-gen powerstate if appropriate. 386 */ 387 if (sc->sc_cur_powerstate == HAL_PM_AWAKE && 388 sc->sc_target_selfgen_state != HAL_PM_AWAKE) { 389 ath_hal_setselfgenpower(sc->sc_ah, 390 sc->sc_target_selfgen_state); 391 } 392 393 } 394 } 395 396 /* 397 * Restore the power save mode to what it once was. 398 * 399 * This will decrement the reference counter and once it hits 400 * zero, it'll restore the powersave state. 401 */ 402 void 403 _ath_power_restore_power_state(struct ath_softc *sc, const char *file, int line) 404 { 405 406 ATH_LOCK_ASSERT(sc); 407 408 DPRINTF(sc, ATH_DEBUG_PWRSAVE, "%s: (%s:%d) refcnt=%d, target state=%d\n", 409 __func__, 410 file, 411 line, 412 sc->sc_powersave_refcnt, 413 sc->sc_target_powerstate); 414 415 if (sc->sc_powersave_refcnt == 0) 416 device_printf(sc->sc_dev, "%s: refcnt=0?\n", __func__); 417 else 418 sc->sc_powersave_refcnt--; 419 420 if (sc->sc_powersave_refcnt == 0 && 421 sc->sc_target_powerstate != sc->sc_cur_powerstate) { 422 sc->sc_cur_powerstate = sc->sc_target_powerstate; 423 ath_hal_setpower(sc->sc_ah, sc->sc_target_powerstate); 424 } 425 426 /* 427 * Adjust the self-gen powerstate if appropriate. 428 */ 429 if (sc->sc_cur_powerstate == HAL_PM_AWAKE && 430 sc->sc_target_selfgen_state != HAL_PM_AWAKE) { 431 ath_hal_setselfgenpower(sc->sc_ah, 432 sc->sc_target_selfgen_state); 433 } 434 435 } 436 437 /* 438 * Configure the initial HAL configuration values based on bus 439 * specific parameters. 440 * 441 * Some PCI IDs and other information may need tweaking. 442 * 443 * XXX TODO: ath9k and the Atheros HAL only program comm2g_switch_enable 444 * if BT antenna diversity isn't enabled. 445 * 446 * So, let's also figure out how to enable BT diversity for AR9485. 447 */ 448 static void 449 ath_setup_hal_config(struct ath_softc *sc, HAL_OPS_CONFIG *ah_config) 450 { 451 /* XXX TODO: only for PCI devices? */ 452 453 if (sc->sc_pci_devinfo & (ATH_PCI_CUS198 | ATH_PCI_CUS230)) { 454 ah_config->ath_hal_ext_lna_ctl_gpio = 0x200; /* bit 9 */ 455 ah_config->ath_hal_ext_atten_margin_cfg = AH_TRUE; 456 ah_config->ath_hal_min_gainidx = AH_TRUE; 457 ah_config->ath_hal_ant_ctrl_comm2g_switch_enable = 0x000bbb88; 458 /* XXX low_rssi_thresh */ 459 /* XXX fast_div_bias */ 460 device_printf(sc->sc_dev, "configuring for %s\n", 461 (sc->sc_pci_devinfo & ATH_PCI_CUS198) ? 462 "CUS198" : "CUS230"); 463 } 464 465 if (sc->sc_pci_devinfo & ATH_PCI_CUS217) 466 device_printf(sc->sc_dev, "CUS217 card detected\n"); 467 468 if (sc->sc_pci_devinfo & ATH_PCI_CUS252) 469 device_printf(sc->sc_dev, "CUS252 card detected\n"); 470 471 if (sc->sc_pci_devinfo & ATH_PCI_AR9565_1ANT) 472 device_printf(sc->sc_dev, "WB335 1-ANT card detected\n"); 473 474 if (sc->sc_pci_devinfo & ATH_PCI_AR9565_2ANT) 475 device_printf(sc->sc_dev, "WB335 2-ANT card detected\n"); 476 477 if (sc->sc_pci_devinfo & ATH_PCI_KILLER) 478 device_printf(sc->sc_dev, "Killer Wireless card detected\n"); 479 480 #if 0 481 /* 482 * Some WB335 cards do not support antenna diversity. Since 483 * we use a hardcoded value for AR9565 instead of using the 484 * EEPROM/OTP data, remove the combining feature from 485 * the HW capabilities bitmap. 486 */ 487 if (sc->sc_pci_devinfo & (ATH9K_PCI_AR9565_1ANT | ATH9K_PCI_AR9565_2ANT)) { 488 if (!(sc->sc_pci_devinfo & ATH9K_PCI_BT_ANT_DIV)) 489 pCap->hw_caps &= ~ATH9K_HW_CAP_ANT_DIV_COMB; 490 } 491 492 if (sc->sc_pci_devinfo & ATH9K_PCI_BT_ANT_DIV) { 493 pCap->hw_caps |= ATH9K_HW_CAP_BT_ANT_DIV; 494 device_printf(sc->sc_dev, "Set BT/WLAN RX diversity capability\n"); 495 } 496 #endif 497 498 if (sc->sc_pci_devinfo & ATH_PCI_D3_L1_WAR) { 499 ah_config->ath_hal_pcie_waen = 0x0040473b; 500 device_printf(sc->sc_dev, "Enable WAR for ASPM D3/L1\n"); 501 } 502 503 #if 0 504 if (sc->sc_pci_devinfo & ATH9K_PCI_NO_PLL_PWRSAVE) { 505 ah->config.no_pll_pwrsave = true; 506 device_printf(sc->sc_dev, "Disable PLL PowerSave\n"); 507 } 508 #endif 509 510 } 511 512 /* 513 * Attempt to fetch the MAC address from the kernel environment. 514 * 515 * Returns 0, macaddr in macaddr if successful; -1 otherwise. 516 */ 517 static int 518 ath_fetch_mac_kenv(struct ath_softc *sc, uint8_t *macaddr) 519 { 520 char devid_str[32]; 521 int local_mac = 0; 522 char *local_macstr; 523 524 /* 525 * Fetch from the kenv rather than using hints. 526 * 527 * Hints would be nice but the transition to dynamic 528 * hints/kenv doesn't happen early enough for this 529 * to work reliably (eg on anything embedded.) 530 */ 531 snprintf(devid_str, 32, "hint.%s.%d.macaddr", 532 device_get_name(sc->sc_dev), 533 device_get_unit(sc->sc_dev)); 534 535 if ((local_macstr = kern_getenv(devid_str)) != NULL) { 536 uint32_t tmpmac[ETHER_ADDR_LEN]; 537 int count; 538 int i; 539 540 /* Have a MAC address; should use it */ 541 device_printf(sc->sc_dev, 542 "Overriding MAC address from environment: '%s'\n", 543 local_macstr); 544 545 /* Extract out the MAC address */ 546 count = sscanf(local_macstr, "%x%*c%x%*c%x%*c%x%*c%x%*c%x", 547 &tmpmac[0], &tmpmac[1], 548 &tmpmac[2], &tmpmac[3], 549 &tmpmac[4], &tmpmac[5]); 550 if (count == 6) { 551 /* Valid! */ 552 local_mac = 1; 553 for (i = 0; i < ETHER_ADDR_LEN; i++) 554 macaddr[i] = tmpmac[i]; 555 } 556 /* Done! */ 557 freeenv(local_macstr); 558 local_macstr = NULL; 559 } 560 561 if (local_mac) 562 return (0); 563 return (-1); 564 } 565 566 #define HAL_MODE_HT20 (HAL_MODE_11NG_HT20 | HAL_MODE_11NA_HT20) 567 #define HAL_MODE_HT40 \ 568 (HAL_MODE_11NG_HT40PLUS | HAL_MODE_11NG_HT40MINUS | \ 569 HAL_MODE_11NA_HT40PLUS | HAL_MODE_11NA_HT40MINUS) 570 int 571 ath_attach(u_int16_t devid, struct ath_softc *sc) 572 { 573 struct ieee80211com *ic = &sc->sc_ic; 574 struct ath_hal *ah = NULL; 575 HAL_STATUS status; 576 int error = 0, i; 577 u_int wmodes; 578 int rx_chainmask, tx_chainmask; 579 HAL_OPS_CONFIG ah_config; 580 581 DPRINTF(sc, ATH_DEBUG_ANY, "%s: devid 0x%x\n", __func__, devid); 582 583 ic->ic_softc = sc; 584 ic->ic_name = device_get_nameunit(sc->sc_dev); 585 586 /* 587 * Configure the initial configuration data. 588 * 589 * This is stuff that may be needed early during attach 590 * rather than done via configuration calls later. 591 */ 592 bzero(&ah_config, sizeof(ah_config)); 593 ath_setup_hal_config(sc, &ah_config); 594 595 ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh, 596 sc->sc_eepromdata, &ah_config, &status); 597 if (ah == NULL) { 598 device_printf(sc->sc_dev, 599 "unable to attach hardware; HAL status %u\n", status); 600 error = ENXIO; 601 goto bad; 602 } 603 sc->sc_ah = ah; 604 sc->sc_invalid = 0; /* ready to go, enable interrupt handling */ 605 #ifdef ATH_DEBUG 606 sc->sc_debug = ath_debug; 607 #endif 608 609 /* 610 * Setup the DMA/EDMA functions based on the current 611 * hardware support. 612 * 613 * This is required before the descriptors are allocated. 614 */ 615 if (ath_hal_hasedma(sc->sc_ah)) { 616 sc->sc_isedma = 1; 617 ath_recv_setup_edma(sc); 618 ath_xmit_setup_edma(sc); 619 } else { 620 ath_recv_setup_legacy(sc); 621 ath_xmit_setup_legacy(sc); 622 } 623 624 if (ath_hal_hasmybeacon(sc->sc_ah)) { 625 sc->sc_do_mybeacon = 1; 626 } 627 628 /* 629 * Check if the MAC has multi-rate retry support. 630 * We do this by trying to setup a fake extended 631 * descriptor. MAC's that don't have support will 632 * return false w/o doing anything. MAC's that do 633 * support it will return true w/o doing anything. 634 */ 635 sc->sc_mrretry = ath_hal_setupxtxdesc(ah, NULL, 0,0, 0,0, 0,0); 636 637 /* 638 * Check if the device has hardware counters for PHY 639 * errors. If so we need to enable the MIB interrupt 640 * so we can act on stat triggers. 641 */ 642 if (ath_hal_hwphycounters(ah)) 643 sc->sc_needmib = 1; 644 645 /* 646 * Get the hardware key cache size. 647 */ 648 sc->sc_keymax = ath_hal_keycachesize(ah); 649 if (sc->sc_keymax > ATH_KEYMAX) { 650 device_printf(sc->sc_dev, 651 "Warning, using only %u of %u key cache slots\n", 652 ATH_KEYMAX, sc->sc_keymax); 653 sc->sc_keymax = ATH_KEYMAX; 654 } 655 /* 656 * Reset the key cache since some parts do not 657 * reset the contents on initial power up. 658 */ 659 for (i = 0; i < sc->sc_keymax; i++) 660 ath_hal_keyreset(ah, i); 661 662 /* 663 * Collect the default channel list. 664 */ 665 error = ath_getchannels(sc); 666 if (error != 0) 667 goto bad; 668 669 /* 670 * Setup rate tables for all potential media types. 671 */ 672 ath_rate_setup(sc, IEEE80211_MODE_11A); 673 ath_rate_setup(sc, IEEE80211_MODE_11B); 674 ath_rate_setup(sc, IEEE80211_MODE_11G); 675 ath_rate_setup(sc, IEEE80211_MODE_TURBO_A); 676 ath_rate_setup(sc, IEEE80211_MODE_TURBO_G); 677 ath_rate_setup(sc, IEEE80211_MODE_STURBO_A); 678 ath_rate_setup(sc, IEEE80211_MODE_11NA); 679 ath_rate_setup(sc, IEEE80211_MODE_11NG); 680 ath_rate_setup(sc, IEEE80211_MODE_HALF); 681 ath_rate_setup(sc, IEEE80211_MODE_QUARTER); 682 683 /* NB: setup here so ath_rate_update is happy */ 684 ath_setcurmode(sc, IEEE80211_MODE_11A); 685 686 /* 687 * Allocate TX descriptors and populate the lists. 688 */ 689 error = ath_desc_alloc(sc); 690 if (error != 0) { 691 device_printf(sc->sc_dev, 692 "failed to allocate TX descriptors: %d\n", error); 693 goto bad; 694 } 695 error = ath_txdma_setup(sc); 696 if (error != 0) { 697 device_printf(sc->sc_dev, 698 "failed to allocate TX descriptors: %d\n", error); 699 goto bad; 700 } 701 702 /* 703 * Allocate RX descriptors and populate the lists. 704 */ 705 error = ath_rxdma_setup(sc); 706 if (error != 0) { 707 device_printf(sc->sc_dev, 708 "failed to allocate RX descriptors: %d\n", error); 709 goto bad; 710 } 711 712 callout_init_mtx(&sc->sc_cal_ch, &sc->sc_mtx, 0); 713 callout_init_mtx(&sc->sc_wd_ch, &sc->sc_mtx, 0); 714 715 ATH_TXBUF_LOCK_INIT(sc); 716 717 sc->sc_tq = taskqueue_create("ath_taskq", M_NOWAIT, 718 taskqueue_thread_enqueue, &sc->sc_tq); 719 taskqueue_start_threads(&sc->sc_tq, 1, PI_NET, "%s taskq", 720 device_get_nameunit(sc->sc_dev)); 721 722 TASK_INIT(&sc->sc_rxtask, 0, sc->sc_rx.recv_tasklet, sc); 723 TASK_INIT(&sc->sc_bmisstask, 0, ath_bmiss_proc, sc); 724 TASK_INIT(&sc->sc_bstucktask,0, ath_bstuck_proc, sc); 725 TASK_INIT(&sc->sc_resettask,0, ath_reset_proc, sc); 726 TASK_INIT(&sc->sc_txqtask, 0, ath_txq_sched_tasklet, sc); 727 TASK_INIT(&sc->sc_fataltask, 0, ath_fatal_proc, sc); 728 729 /* 730 * Allocate hardware transmit queues: one queue for 731 * beacon frames and one data queue for each QoS 732 * priority. Note that the hal handles resetting 733 * these queues at the needed time. 734 * 735 * XXX PS-Poll 736 */ 737 sc->sc_bhalq = ath_beaconq_setup(sc); 738 if (sc->sc_bhalq == (u_int) -1) { 739 device_printf(sc->sc_dev, 740 "unable to setup a beacon xmit queue!\n"); 741 error = EIO; 742 goto bad2; 743 } 744 sc->sc_cabq = ath_txq_setup(sc, HAL_TX_QUEUE_CAB, 0); 745 if (sc->sc_cabq == NULL) { 746 device_printf(sc->sc_dev, "unable to setup CAB xmit queue!\n"); 747 error = EIO; 748 goto bad2; 749 } 750 /* NB: insure BK queue is the lowest priority h/w queue */ 751 if (!ath_tx_setup(sc, WME_AC_BK, HAL_WME_AC_BK)) { 752 device_printf(sc->sc_dev, 753 "unable to setup xmit queue for %s traffic!\n", 754 ieee80211_wme_acnames[WME_AC_BK]); 755 error = EIO; 756 goto bad2; 757 } 758 if (!ath_tx_setup(sc, WME_AC_BE, HAL_WME_AC_BE) || 759 !ath_tx_setup(sc, WME_AC_VI, HAL_WME_AC_VI) || 760 !ath_tx_setup(sc, WME_AC_VO, HAL_WME_AC_VO)) { 761 /* 762 * Not enough hardware tx queues to properly do WME; 763 * just punt and assign them all to the same h/w queue. 764 * We could do a better job of this if, for example, 765 * we allocate queues when we switch from station to 766 * AP mode. 767 */ 768 if (sc->sc_ac2q[WME_AC_VI] != NULL) 769 ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_VI]); 770 if (sc->sc_ac2q[WME_AC_BE] != NULL) 771 ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_BE]); 772 sc->sc_ac2q[WME_AC_BE] = sc->sc_ac2q[WME_AC_BK]; 773 sc->sc_ac2q[WME_AC_VI] = sc->sc_ac2q[WME_AC_BK]; 774 sc->sc_ac2q[WME_AC_VO] = sc->sc_ac2q[WME_AC_BK]; 775 } 776 777 /* 778 * Attach the TX completion function. 779 * 780 * The non-EDMA chips may have some special case optimisations; 781 * this method gives everyone a chance to attach cleanly. 782 */ 783 sc->sc_tx.xmit_attach_comp_func(sc); 784 785 /* 786 * Setup rate control. Some rate control modules 787 * call back to change the anntena state so expose 788 * the necessary entry points. 789 * XXX maybe belongs in struct ath_ratectrl? 790 */ 791 sc->sc_setdefantenna = ath_setdefantenna; 792 sc->sc_rc = ath_rate_attach(sc); 793 if (sc->sc_rc == NULL) { 794 error = EIO; 795 goto bad2; 796 } 797 798 /* Attach DFS module */ 799 if (! ath_dfs_attach(sc)) { 800 device_printf(sc->sc_dev, 801 "%s: unable to attach DFS\n", __func__); 802 error = EIO; 803 goto bad2; 804 } 805 806 /* Attach spectral module */ 807 if (ath_spectral_attach(sc) < 0) { 808 device_printf(sc->sc_dev, 809 "%s: unable to attach spectral\n", __func__); 810 error = EIO; 811 goto bad2; 812 } 813 814 /* Attach bluetooth coexistence module */ 815 if (ath_btcoex_attach(sc) < 0) { 816 device_printf(sc->sc_dev, 817 "%s: unable to attach bluetooth coexistence\n", __func__); 818 error = EIO; 819 goto bad2; 820 } 821 822 /* Attach LNA diversity module */ 823 if (ath_lna_div_attach(sc) < 0) { 824 device_printf(sc->sc_dev, 825 "%s: unable to attach LNA diversity\n", __func__); 826 error = EIO; 827 goto bad2; 828 } 829 830 /* Start DFS processing tasklet */ 831 TASK_INIT(&sc->sc_dfstask, 0, ath_dfs_tasklet, sc); 832 833 /* Configure LED state */ 834 sc->sc_blinking = 0; 835 sc->sc_ledstate = 1; 836 sc->sc_ledon = 0; /* low true */ 837 sc->sc_ledidle = (2700*hz)/1000; /* 2.7sec */ 838 callout_init(&sc->sc_ledtimer, 1); 839 840 /* 841 * Don't setup hardware-based blinking. 842 * 843 * Although some NICs may have this configured in the 844 * default reset register values, the user may wish 845 * to alter which pins have which function. 846 * 847 * The reference driver attaches the MAC network LED to GPIO1 and 848 * the MAC power LED to GPIO2. However, the DWA-552 cardbus 849 * NIC has these reversed. 850 */ 851 sc->sc_hardled = (1 == 0); 852 sc->sc_led_net_pin = -1; 853 sc->sc_led_pwr_pin = -1; 854 /* 855 * Auto-enable soft led processing for IBM cards and for 856 * 5211 minipci cards. Users can also manually enable/disable 857 * support with a sysctl. 858 */ 859 sc->sc_softled = (devid == AR5212_DEVID_IBM || devid == AR5211_DEVID); 860 ath_led_config(sc); 861 ath_hal_setledstate(ah, HAL_LED_INIT); 862 863 /* XXX not right but it's not used anywhere important */ 864 ic->ic_phytype = IEEE80211_T_OFDM; 865 ic->ic_opmode = IEEE80211_M_STA; 866 ic->ic_caps = 867 IEEE80211_C_STA /* station mode */ 868 | IEEE80211_C_IBSS /* ibss, nee adhoc, mode */ 869 | IEEE80211_C_HOSTAP /* hostap mode */ 870 | IEEE80211_C_MONITOR /* monitor mode */ 871 | IEEE80211_C_AHDEMO /* adhoc demo mode */ 872 | IEEE80211_C_WDS /* 4-address traffic works */ 873 | IEEE80211_C_MBSS /* mesh point link mode */ 874 | IEEE80211_C_SHPREAMBLE /* short preamble supported */ 875 | IEEE80211_C_SHSLOT /* short slot time supported */ 876 | IEEE80211_C_WPA /* capable of WPA1+WPA2 */ 877 #ifndef ATH_ENABLE_11N 878 | IEEE80211_C_BGSCAN /* capable of bg scanning */ 879 #endif 880 | IEEE80211_C_TXFRAG /* handle tx frags */ 881 #ifdef ATH_ENABLE_DFS 882 | IEEE80211_C_DFS /* Enable radar detection */ 883 #endif 884 | IEEE80211_C_PMGT /* Station side power mgmt */ 885 | IEEE80211_C_SWSLEEP 886 ; 887 /* 888 * Query the hal to figure out h/w crypto support. 889 */ 890 if (ath_hal_ciphersupported(ah, HAL_CIPHER_WEP)) 891 ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP; 892 if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_OCB)) 893 ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_OCB; 894 if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_CCM)) 895 ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_CCM; 896 if (ath_hal_ciphersupported(ah, HAL_CIPHER_CKIP)) 897 ic->ic_cryptocaps |= IEEE80211_CRYPTO_CKIP; 898 if (ath_hal_ciphersupported(ah, HAL_CIPHER_TKIP)) { 899 ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIP; 900 /* 901 * Check if h/w does the MIC and/or whether the 902 * separate key cache entries are required to 903 * handle both tx+rx MIC keys. 904 */ 905 if (ath_hal_ciphersupported(ah, HAL_CIPHER_MIC)) 906 ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC; 907 /* 908 * If the h/w supports storing tx+rx MIC keys 909 * in one cache slot automatically enable use. 910 */ 911 if (ath_hal_hastkipsplit(ah) || 912 !ath_hal_settkipsplit(ah, AH_FALSE)) 913 sc->sc_splitmic = 1; 914 /* 915 * If the h/w can do TKIP MIC together with WME then 916 * we use it; otherwise we force the MIC to be done 917 * in software by the net80211 layer. 918 */ 919 if (ath_hal_haswmetkipmic(ah)) 920 sc->sc_wmetkipmic = 1; 921 } 922 sc->sc_hasclrkey = ath_hal_ciphersupported(ah, HAL_CIPHER_CLR); 923 /* 924 * Check for multicast key search support. 925 */ 926 if (ath_hal_hasmcastkeysearch(sc->sc_ah) && 927 !ath_hal_getmcastkeysearch(sc->sc_ah)) { 928 ath_hal_setmcastkeysearch(sc->sc_ah, 1); 929 } 930 sc->sc_mcastkey = ath_hal_getmcastkeysearch(ah); 931 /* 932 * Mark key cache slots associated with global keys 933 * as in use. If we knew TKIP was not to be used we 934 * could leave the +32, +64, and +32+64 slots free. 935 */ 936 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 937 setbit(sc->sc_keymap, i); 938 setbit(sc->sc_keymap, i+64); 939 if (sc->sc_splitmic) { 940 setbit(sc->sc_keymap, i+32); 941 setbit(sc->sc_keymap, i+32+64); 942 } 943 } 944 /* 945 * TPC support can be done either with a global cap or 946 * per-packet support. The latter is not available on 947 * all parts. We're a bit pedantic here as all parts 948 * support a global cap. 949 */ 950 if (ath_hal_hastpc(ah) || ath_hal_hastxpowlimit(ah)) 951 ic->ic_caps |= IEEE80211_C_TXPMGT; 952 953 /* 954 * Mark WME capability only if we have sufficient 955 * hardware queues to do proper priority scheduling. 956 */ 957 if (sc->sc_ac2q[WME_AC_BE] != sc->sc_ac2q[WME_AC_BK]) 958 ic->ic_caps |= IEEE80211_C_WME; 959 /* 960 * Check for misc other capabilities. 961 */ 962 if (ath_hal_hasbursting(ah)) 963 ic->ic_caps |= IEEE80211_C_BURST; 964 sc->sc_hasbmask = ath_hal_hasbssidmask(ah); 965 sc->sc_hasbmatch = ath_hal_hasbssidmatch(ah); 966 sc->sc_hastsfadd = ath_hal_hastsfadjust(ah); 967 sc->sc_rxslink = ath_hal_self_linked_final_rxdesc(ah); 968 sc->sc_rxtsf32 = ath_hal_has_long_rxdesc_tsf(ah); 969 sc->sc_hasenforcetxop = ath_hal_hasenforcetxop(ah); 970 sc->sc_rx_lnamixer = ath_hal_hasrxlnamixer(ah); 971 sc->sc_hasdivcomb = ath_hal_hasdivantcomb(ah); 972 973 if (ath_hal_hasfastframes(ah)) 974 ic->ic_caps |= IEEE80211_C_FF; 975 wmodes = ath_hal_getwirelessmodes(ah); 976 if (wmodes & (HAL_MODE_108G|HAL_MODE_TURBO)) 977 ic->ic_caps |= IEEE80211_C_TURBOP; 978 #ifdef IEEE80211_SUPPORT_TDMA 979 if (ath_hal_macversion(ah) > 0x78) { 980 ic->ic_caps |= IEEE80211_C_TDMA; /* capable of TDMA */ 981 ic->ic_tdma_update = ath_tdma_update; 982 } 983 #endif 984 985 /* 986 * TODO: enforce that at least this many frames are available 987 * in the txbuf list before allowing data frames (raw or 988 * otherwise) to be transmitted. 989 */ 990 sc->sc_txq_data_minfree = 10; 991 /* 992 * Leave this as default to maintain legacy behaviour. 993 * Shortening the cabq/mcastq may end up causing some 994 * undesirable behaviour. 995 */ 996 sc->sc_txq_mcastq_maxdepth = ath_txbuf; 997 998 /* 999 * How deep can the node software TX queue get whilst it's asleep. 1000 */ 1001 sc->sc_txq_node_psq_maxdepth = 16; 1002 1003 /* 1004 * Default the maximum queue depth for a given node 1005 * to 1/4'th the TX buffers, or 64, whichever 1006 * is larger. 1007 */ 1008 sc->sc_txq_node_maxdepth = MAX(64, ath_txbuf / 4); 1009 1010 /* Enable CABQ by default */ 1011 sc->sc_cabq_enable = 1; 1012 1013 /* 1014 * Allow the TX and RX chainmasks to be overridden by 1015 * environment variables and/or device.hints. 1016 * 1017 * This must be done early - before the hardware is 1018 * calibrated or before the 802.11n stream calculation 1019 * is done. 1020 */ 1021 if (resource_int_value(device_get_name(sc->sc_dev), 1022 device_get_unit(sc->sc_dev), "rx_chainmask", 1023 &rx_chainmask) == 0) { 1024 device_printf(sc->sc_dev, "Setting RX chainmask to 0x%x\n", 1025 rx_chainmask); 1026 (void) ath_hal_setrxchainmask(sc->sc_ah, rx_chainmask); 1027 } 1028 if (resource_int_value(device_get_name(sc->sc_dev), 1029 device_get_unit(sc->sc_dev), "tx_chainmask", 1030 &tx_chainmask) == 0) { 1031 device_printf(sc->sc_dev, "Setting TX chainmask to 0x%x\n", 1032 tx_chainmask); 1033 (void) ath_hal_settxchainmask(sc->sc_ah, tx_chainmask); 1034 } 1035 1036 /* 1037 * Query the TX/RX chainmask configuration. 1038 * 1039 * This is only relevant for 11n devices. 1040 */ 1041 ath_hal_getrxchainmask(ah, &sc->sc_rxchainmask); 1042 ath_hal_gettxchainmask(ah, &sc->sc_txchainmask); 1043 1044 /* 1045 * Disable MRR with protected frames by default. 1046 * Only 802.11n series NICs can handle this. 1047 */ 1048 sc->sc_mrrprot = 0; /* XXX should be a capability */ 1049 1050 /* 1051 * Query the enterprise mode information the HAL. 1052 */ 1053 if (ath_hal_getcapability(ah, HAL_CAP_ENTERPRISE_MODE, 0, 1054 &sc->sc_ent_cfg) == HAL_OK) 1055 sc->sc_use_ent = 1; 1056 1057 #ifdef ATH_ENABLE_11N 1058 /* 1059 * Query HT capabilities 1060 */ 1061 if (ath_hal_getcapability(ah, HAL_CAP_HT, 0, NULL) == HAL_OK && 1062 (wmodes & (HAL_MODE_HT20 | HAL_MODE_HT40))) { 1063 uint32_t rxs, txs; 1064 1065 device_printf(sc->sc_dev, "[HT] enabling HT modes\n"); 1066 1067 sc->sc_mrrprot = 1; /* XXX should be a capability */ 1068 1069 ic->ic_htcaps = IEEE80211_HTC_HT /* HT operation */ 1070 | IEEE80211_HTC_AMPDU /* A-MPDU tx/rx */ 1071 | IEEE80211_HTC_AMSDU /* A-MSDU tx/rx */ 1072 | IEEE80211_HTCAP_MAXAMSDU_3839 1073 /* max A-MSDU length */ 1074 | IEEE80211_HTCAP_SMPS_OFF; /* SM power save off */ 1075 ; 1076 1077 /* 1078 * Enable short-GI for HT20 only if the hardware 1079 * advertises support. 1080 * Notably, anything earlier than the AR9287 doesn't. 1081 */ 1082 if ((ath_hal_getcapability(ah, 1083 HAL_CAP_HT20_SGI, 0, NULL) == HAL_OK) && 1084 (wmodes & HAL_MODE_HT20)) { 1085 device_printf(sc->sc_dev, 1086 "[HT] enabling short-GI in 20MHz mode\n"); 1087 ic->ic_htcaps |= IEEE80211_HTCAP_SHORTGI20; 1088 } 1089 1090 if (wmodes & HAL_MODE_HT40) 1091 ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40 1092 | IEEE80211_HTCAP_SHORTGI40; 1093 1094 /* 1095 * TX/RX streams need to be taken into account when 1096 * negotiating which MCS rates it'll receive and 1097 * what MCS rates are available for TX. 1098 */ 1099 (void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 0, &txs); 1100 (void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 1, &rxs); 1101 ic->ic_txstream = txs; 1102 ic->ic_rxstream = rxs; 1103 1104 /* 1105 * Setup TX and RX STBC based on what the HAL allows and 1106 * the currently configured chainmask set. 1107 * Ie - don't enable STBC TX if only one chain is enabled. 1108 * STBC RX is fine on a single RX chain; it just won't 1109 * provide any real benefit. 1110 */ 1111 if (ath_hal_getcapability(ah, HAL_CAP_RX_STBC, 0, 1112 NULL) == HAL_OK) { 1113 sc->sc_rx_stbc = 1; 1114 device_printf(sc->sc_dev, 1115 "[HT] 1 stream STBC receive enabled\n"); 1116 ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_1STREAM; 1117 } 1118 if (txs > 1 && ath_hal_getcapability(ah, HAL_CAP_TX_STBC, 0, 1119 NULL) == HAL_OK) { 1120 sc->sc_tx_stbc = 1; 1121 device_printf(sc->sc_dev, 1122 "[HT] 1 stream STBC transmit enabled\n"); 1123 ic->ic_htcaps |= IEEE80211_HTCAP_TXSTBC; 1124 } 1125 1126 (void) ath_hal_getcapability(ah, HAL_CAP_RTS_AGGR_LIMIT, 1, 1127 &sc->sc_rts_aggr_limit); 1128 if (sc->sc_rts_aggr_limit != (64 * 1024)) 1129 device_printf(sc->sc_dev, 1130 "[HT] RTS aggregates limited to %d KiB\n", 1131 sc->sc_rts_aggr_limit / 1024); 1132 1133 device_printf(sc->sc_dev, 1134 "[HT] %d RX streams; %d TX streams\n", rxs, txs); 1135 } 1136 #endif 1137 1138 /* 1139 * Initial aggregation settings. 1140 */ 1141 sc->sc_hwq_limit_aggr = ATH_AGGR_MIN_QDEPTH; 1142 sc->sc_hwq_limit_nonaggr = ATH_NONAGGR_MIN_QDEPTH; 1143 sc->sc_tid_hwq_lo = ATH_AGGR_SCHED_LOW; 1144 sc->sc_tid_hwq_hi = ATH_AGGR_SCHED_HIGH; 1145 sc->sc_aggr_limit = ATH_AGGR_MAXSIZE; 1146 sc->sc_delim_min_pad = 0; 1147 1148 /* 1149 * Check if the hardware requires PCI register serialisation. 1150 * Some of the Owl based MACs require this. 1151 */ 1152 if (mp_ncpus > 1 && 1153 ath_hal_getcapability(ah, HAL_CAP_SERIALISE_WAR, 1154 0, NULL) == HAL_OK) { 1155 sc->sc_ah->ah_config.ah_serialise_reg_war = 1; 1156 device_printf(sc->sc_dev, 1157 "Enabling register serialisation\n"); 1158 } 1159 1160 /* 1161 * Initialise the deferred completed RX buffer list. 1162 */ 1163 TAILQ_INIT(&sc->sc_rx_rxlist[HAL_RX_QUEUE_HP]); 1164 TAILQ_INIT(&sc->sc_rx_rxlist[HAL_RX_QUEUE_LP]); 1165 1166 /* 1167 * Indicate we need the 802.11 header padded to a 1168 * 32-bit boundary for 4-address and QoS frames. 1169 */ 1170 ic->ic_flags |= IEEE80211_F_DATAPAD; 1171 1172 /* 1173 * Query the hal about antenna support. 1174 */ 1175 sc->sc_defant = ath_hal_getdefantenna(ah); 1176 1177 /* 1178 * Not all chips have the VEOL support we want to 1179 * use with IBSS beacons; check here for it. 1180 */ 1181 sc->sc_hasveol = ath_hal_hasveol(ah); 1182 1183 /* get mac address from kenv first, then hardware */ 1184 if (ath_fetch_mac_kenv(sc, ic->ic_macaddr) == 0) { 1185 /* Tell the HAL now about the new MAC */ 1186 ath_hal_setmac(ah, ic->ic_macaddr); 1187 } else { 1188 ath_hal_getmac(ah, ic->ic_macaddr); 1189 } 1190 1191 if (sc->sc_hasbmask) 1192 ath_hal_getbssidmask(ah, sc->sc_hwbssidmask); 1193 1194 /* NB: used to size node table key mapping array */ 1195 ic->ic_max_keyix = sc->sc_keymax; 1196 /* call MI attach routine. */ 1197 ieee80211_ifattach(ic); 1198 ic->ic_setregdomain = ath_setregdomain; 1199 ic->ic_getradiocaps = ath_getradiocaps; 1200 sc->sc_opmode = HAL_M_STA; 1201 1202 /* override default methods */ 1203 ic->ic_ioctl = ath_ioctl; 1204 ic->ic_parent = ath_parent; 1205 ic->ic_transmit = ath_transmit; 1206 ic->ic_newassoc = ath_newassoc; 1207 ic->ic_updateslot = ath_updateslot; 1208 ic->ic_wme.wme_update = ath_wme_update; 1209 ic->ic_vap_create = ath_vap_create; 1210 ic->ic_vap_delete = ath_vap_delete; 1211 ic->ic_raw_xmit = ath_raw_xmit; 1212 ic->ic_update_mcast = ath_update_mcast; 1213 ic->ic_update_promisc = ath_update_promisc; 1214 ic->ic_node_alloc = ath_node_alloc; 1215 sc->sc_node_free = ic->ic_node_free; 1216 ic->ic_node_free = ath_node_free; 1217 sc->sc_node_cleanup = ic->ic_node_cleanup; 1218 ic->ic_node_cleanup = ath_node_cleanup; 1219 ic->ic_node_getsignal = ath_node_getsignal; 1220 ic->ic_scan_start = ath_scan_start; 1221 ic->ic_scan_end = ath_scan_end; 1222 ic->ic_set_channel = ath_set_channel; 1223 #ifdef ATH_ENABLE_11N 1224 /* 802.11n specific - but just override anyway */ 1225 sc->sc_addba_request = ic->ic_addba_request; 1226 sc->sc_addba_response = ic->ic_addba_response; 1227 sc->sc_addba_stop = ic->ic_addba_stop; 1228 sc->sc_bar_response = ic->ic_bar_response; 1229 sc->sc_addba_response_timeout = ic->ic_addba_response_timeout; 1230 1231 ic->ic_addba_request = ath_addba_request; 1232 ic->ic_addba_response = ath_addba_response; 1233 ic->ic_addba_response_timeout = ath_addba_response_timeout; 1234 ic->ic_addba_stop = ath_addba_stop; 1235 ic->ic_bar_response = ath_bar_response; 1236 1237 ic->ic_update_chw = ath_update_chw; 1238 #endif /* ATH_ENABLE_11N */ 1239 1240 #ifdef ATH_ENABLE_RADIOTAP_VENDOR_EXT 1241 /* 1242 * There's one vendor bitmap entry in the RX radiotap 1243 * header; make sure that's taken into account. 1244 */ 1245 ieee80211_radiotap_attachv(ic, 1246 &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th), 0, 1247 ATH_TX_RADIOTAP_PRESENT, 1248 &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th), 1, 1249 ATH_RX_RADIOTAP_PRESENT); 1250 #else 1251 /* 1252 * No vendor bitmap/extensions are present. 1253 */ 1254 ieee80211_radiotap_attach(ic, 1255 &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th), 1256 ATH_TX_RADIOTAP_PRESENT, 1257 &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th), 1258 ATH_RX_RADIOTAP_PRESENT); 1259 #endif /* ATH_ENABLE_RADIOTAP_VENDOR_EXT */ 1260 1261 /* 1262 * Setup the ALQ logging if required 1263 */ 1264 #ifdef ATH_DEBUG_ALQ 1265 if_ath_alq_init(&sc->sc_alq, device_get_nameunit(sc->sc_dev)); 1266 if_ath_alq_setcfg(&sc->sc_alq, 1267 sc->sc_ah->ah_macVersion, 1268 sc->sc_ah->ah_macRev, 1269 sc->sc_ah->ah_phyRev, 1270 sc->sc_ah->ah_magic); 1271 #endif 1272 1273 /* 1274 * Setup dynamic sysctl's now that country code and 1275 * regdomain are available from the hal. 1276 */ 1277 ath_sysctlattach(sc); 1278 ath_sysctl_stats_attach(sc); 1279 ath_sysctl_hal_attach(sc); 1280 1281 if (bootverbose) 1282 ieee80211_announce(ic); 1283 ath_announce(sc); 1284 1285 /* 1286 * Put it to sleep for now. 1287 */ 1288 ATH_LOCK(sc); 1289 ath_power_setpower(sc, HAL_PM_FULL_SLEEP); 1290 ATH_UNLOCK(sc); 1291 1292 return 0; 1293 bad2: 1294 ath_tx_cleanup(sc); 1295 ath_desc_free(sc); 1296 ath_txdma_teardown(sc); 1297 ath_rxdma_teardown(sc); 1298 bad: 1299 if (ah) 1300 ath_hal_detach(ah); 1301 sc->sc_invalid = 1; 1302 return error; 1303 } 1304 1305 int 1306 ath_detach(struct ath_softc *sc) 1307 { 1308 1309 /* 1310 * NB: the order of these is important: 1311 * o stop the chip so no more interrupts will fire 1312 * o call the 802.11 layer before detaching the hal to 1313 * insure callbacks into the driver to delete global 1314 * key cache entries can be handled 1315 * o free the taskqueue which drains any pending tasks 1316 * o reclaim the tx queue data structures after calling 1317 * the 802.11 layer as we'll get called back to reclaim 1318 * node state and potentially want to use them 1319 * o to cleanup the tx queues the hal is called, so detach 1320 * it last 1321 * Other than that, it's straightforward... 1322 */ 1323 1324 /* 1325 * XXX Wake the hardware up first. ath_stop() will still 1326 * wake it up first, but I'd rather do it here just to 1327 * ensure it's awake. 1328 */ 1329 ATH_LOCK(sc); 1330 ath_power_set_power_state(sc, HAL_PM_AWAKE); 1331 ath_power_setpower(sc, HAL_PM_AWAKE); 1332 1333 /* 1334 * Stop things cleanly. 1335 */ 1336 ath_stop(sc); 1337 ATH_UNLOCK(sc); 1338 1339 ieee80211_ifdetach(&sc->sc_ic); 1340 taskqueue_free(sc->sc_tq); 1341 #ifdef ATH_TX99_DIAG 1342 if (sc->sc_tx99 != NULL) 1343 sc->sc_tx99->detach(sc->sc_tx99); 1344 #endif 1345 ath_rate_detach(sc->sc_rc); 1346 #ifdef ATH_DEBUG_ALQ 1347 if_ath_alq_tidyup(&sc->sc_alq); 1348 #endif 1349 ath_lna_div_detach(sc); 1350 ath_btcoex_detach(sc); 1351 ath_spectral_detach(sc); 1352 ath_dfs_detach(sc); 1353 ath_desc_free(sc); 1354 ath_txdma_teardown(sc); 1355 ath_rxdma_teardown(sc); 1356 ath_tx_cleanup(sc); 1357 ath_hal_detach(sc->sc_ah); /* NB: sets chip in full sleep */ 1358 1359 return 0; 1360 } 1361 1362 /* 1363 * MAC address handling for multiple BSS on the same radio. 1364 * The first vap uses the MAC address from the EEPROM. For 1365 * subsequent vap's we set the U/L bit (bit 1) in the MAC 1366 * address and use the next six bits as an index. 1367 */ 1368 static void 1369 assign_address(struct ath_softc *sc, uint8_t mac[IEEE80211_ADDR_LEN], int clone) 1370 { 1371 int i; 1372 1373 if (clone && sc->sc_hasbmask) { 1374 /* NB: we only do this if h/w supports multiple bssid */ 1375 for (i = 0; i < 8; i++) 1376 if ((sc->sc_bssidmask & (1<<i)) == 0) 1377 break; 1378 if (i != 0) 1379 mac[0] |= (i << 2)|0x2; 1380 } else 1381 i = 0; 1382 sc->sc_bssidmask |= 1<<i; 1383 sc->sc_hwbssidmask[0] &= ~mac[0]; 1384 if (i == 0) 1385 sc->sc_nbssid0++; 1386 } 1387 1388 static void 1389 reclaim_address(struct ath_softc *sc, const uint8_t mac[IEEE80211_ADDR_LEN]) 1390 { 1391 int i = mac[0] >> 2; 1392 uint8_t mask; 1393 1394 if (i != 0 || --sc->sc_nbssid0 == 0) { 1395 sc->sc_bssidmask &= ~(1<<i); 1396 /* recalculate bssid mask from remaining addresses */ 1397 mask = 0xff; 1398 for (i = 1; i < 8; i++) 1399 if (sc->sc_bssidmask & (1<<i)) 1400 mask &= ~((i<<2)|0x2); 1401 sc->sc_hwbssidmask[0] |= mask; 1402 } 1403 } 1404 1405 /* 1406 * Assign a beacon xmit slot. We try to space out 1407 * assignments so when beacons are staggered the 1408 * traffic coming out of the cab q has maximal time 1409 * to go out before the next beacon is scheduled. 1410 */ 1411 static int 1412 assign_bslot(struct ath_softc *sc) 1413 { 1414 u_int slot, free; 1415 1416 free = 0; 1417 for (slot = 0; slot < ATH_BCBUF; slot++) 1418 if (sc->sc_bslot[slot] == NULL) { 1419 if (sc->sc_bslot[(slot+1)%ATH_BCBUF] == NULL && 1420 sc->sc_bslot[(slot-1)%ATH_BCBUF] == NULL) 1421 return slot; 1422 free = slot; 1423 /* NB: keep looking for a double slot */ 1424 } 1425 return free; 1426 } 1427 1428 static struct ieee80211vap * 1429 ath_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit, 1430 enum ieee80211_opmode opmode, int flags, 1431 const uint8_t bssid[IEEE80211_ADDR_LEN], 1432 const uint8_t mac0[IEEE80211_ADDR_LEN]) 1433 { 1434 struct ath_softc *sc = ic->ic_softc; 1435 struct ath_vap *avp; 1436 struct ieee80211vap *vap; 1437 uint8_t mac[IEEE80211_ADDR_LEN]; 1438 int needbeacon, error; 1439 enum ieee80211_opmode ic_opmode; 1440 1441 avp = malloc(sizeof(struct ath_vap), M_80211_VAP, M_WAITOK | M_ZERO); 1442 needbeacon = 0; 1443 IEEE80211_ADDR_COPY(mac, mac0); 1444 1445 ATH_LOCK(sc); 1446 ic_opmode = opmode; /* default to opmode of new vap */ 1447 switch (opmode) { 1448 case IEEE80211_M_STA: 1449 if (sc->sc_nstavaps != 0) { /* XXX only 1 for now */ 1450 device_printf(sc->sc_dev, "only 1 sta vap supported\n"); 1451 goto bad; 1452 } 1453 if (sc->sc_nvaps) { 1454 /* 1455 * With multiple vaps we must fall back 1456 * to s/w beacon miss handling. 1457 */ 1458 flags |= IEEE80211_CLONE_NOBEACONS; 1459 } 1460 if (flags & IEEE80211_CLONE_NOBEACONS) { 1461 /* 1462 * Station mode w/o beacons are implemented w/ AP mode. 1463 */ 1464 ic_opmode = IEEE80211_M_HOSTAP; 1465 } 1466 break; 1467 case IEEE80211_M_IBSS: 1468 if (sc->sc_nvaps != 0) { /* XXX only 1 for now */ 1469 device_printf(sc->sc_dev, 1470 "only 1 ibss vap supported\n"); 1471 goto bad; 1472 } 1473 needbeacon = 1; 1474 break; 1475 case IEEE80211_M_AHDEMO: 1476 #ifdef IEEE80211_SUPPORT_TDMA 1477 if (flags & IEEE80211_CLONE_TDMA) { 1478 if (sc->sc_nvaps != 0) { 1479 device_printf(sc->sc_dev, 1480 "only 1 tdma vap supported\n"); 1481 goto bad; 1482 } 1483 needbeacon = 1; 1484 flags |= IEEE80211_CLONE_NOBEACONS; 1485 } 1486 /* fall thru... */ 1487 #endif 1488 case IEEE80211_M_MONITOR: 1489 if (sc->sc_nvaps != 0 && ic->ic_opmode != opmode) { 1490 /* 1491 * Adopt existing mode. Adding a monitor or ahdemo 1492 * vap to an existing configuration is of dubious 1493 * value but should be ok. 1494 */ 1495 /* XXX not right for monitor mode */ 1496 ic_opmode = ic->ic_opmode; 1497 } 1498 break; 1499 case IEEE80211_M_HOSTAP: 1500 case IEEE80211_M_MBSS: 1501 needbeacon = 1; 1502 break; 1503 case IEEE80211_M_WDS: 1504 if (sc->sc_nvaps != 0 && ic->ic_opmode == IEEE80211_M_STA) { 1505 device_printf(sc->sc_dev, 1506 "wds not supported in sta mode\n"); 1507 goto bad; 1508 } 1509 /* 1510 * Silently remove any request for a unique 1511 * bssid; WDS vap's always share the local 1512 * mac address. 1513 */ 1514 flags &= ~IEEE80211_CLONE_BSSID; 1515 if (sc->sc_nvaps == 0) 1516 ic_opmode = IEEE80211_M_HOSTAP; 1517 else 1518 ic_opmode = ic->ic_opmode; 1519 break; 1520 default: 1521 device_printf(sc->sc_dev, "unknown opmode %d\n", opmode); 1522 goto bad; 1523 } 1524 /* 1525 * Check that a beacon buffer is available; the code below assumes it. 1526 */ 1527 if (needbeacon & TAILQ_EMPTY(&sc->sc_bbuf)) { 1528 device_printf(sc->sc_dev, "no beacon buffer available\n"); 1529 goto bad; 1530 } 1531 1532 /* STA, AHDEMO? */ 1533 if (opmode == IEEE80211_M_HOSTAP || opmode == IEEE80211_M_MBSS) { 1534 assign_address(sc, mac, flags & IEEE80211_CLONE_BSSID); 1535 ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask); 1536 } 1537 1538 vap = &avp->av_vap; 1539 /* XXX can't hold mutex across if_alloc */ 1540 ATH_UNLOCK(sc); 1541 error = ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid); 1542 ATH_LOCK(sc); 1543 if (error != 0) { 1544 device_printf(sc->sc_dev, "%s: error %d creating vap\n", 1545 __func__, error); 1546 goto bad2; 1547 } 1548 1549 /* h/w crypto support */ 1550 vap->iv_key_alloc = ath_key_alloc; 1551 vap->iv_key_delete = ath_key_delete; 1552 vap->iv_key_set = ath_key_set; 1553 vap->iv_key_update_begin = ath_key_update_begin; 1554 vap->iv_key_update_end = ath_key_update_end; 1555 1556 /* override various methods */ 1557 avp->av_recv_mgmt = vap->iv_recv_mgmt; 1558 vap->iv_recv_mgmt = ath_recv_mgmt; 1559 vap->iv_reset = ath_reset_vap; 1560 vap->iv_update_beacon = ath_beacon_update; 1561 avp->av_newstate = vap->iv_newstate; 1562 vap->iv_newstate = ath_newstate; 1563 avp->av_bmiss = vap->iv_bmiss; 1564 vap->iv_bmiss = ath_bmiss_vap; 1565 1566 avp->av_node_ps = vap->iv_node_ps; 1567 vap->iv_node_ps = ath_node_powersave; 1568 1569 avp->av_set_tim = vap->iv_set_tim; 1570 vap->iv_set_tim = ath_node_set_tim; 1571 1572 avp->av_recv_pspoll = vap->iv_recv_pspoll; 1573 vap->iv_recv_pspoll = ath_node_recv_pspoll; 1574 1575 /* Set default parameters */ 1576 1577 /* 1578 * Anything earlier than some AR9300 series MACs don't 1579 * support a smaller MPDU density. 1580 */ 1581 vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_8; 1582 /* 1583 * All NICs can handle the maximum size, however 1584 * AR5416 based MACs can only TX aggregates w/ RTS 1585 * protection when the total aggregate size is <= 8k. 1586 * However, for now that's enforced by the TX path. 1587 */ 1588 vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_64K; 1589 1590 avp->av_bslot = -1; 1591 if (needbeacon) { 1592 /* 1593 * Allocate beacon state and setup the q for buffered 1594 * multicast frames. We know a beacon buffer is 1595 * available because we checked above. 1596 */ 1597 avp->av_bcbuf = TAILQ_FIRST(&sc->sc_bbuf); 1598 TAILQ_REMOVE(&sc->sc_bbuf, avp->av_bcbuf, bf_list); 1599 if (opmode != IEEE80211_M_IBSS || !sc->sc_hasveol) { 1600 /* 1601 * Assign the vap to a beacon xmit slot. As above 1602 * this cannot fail to find a free one. 1603 */ 1604 avp->av_bslot = assign_bslot(sc); 1605 KASSERT(sc->sc_bslot[avp->av_bslot] == NULL, 1606 ("beacon slot %u not empty", avp->av_bslot)); 1607 sc->sc_bslot[avp->av_bslot] = vap; 1608 sc->sc_nbcnvaps++; 1609 } 1610 if (sc->sc_hastsfadd && sc->sc_nbcnvaps > 0) { 1611 /* 1612 * Multple vaps are to transmit beacons and we 1613 * have h/w support for TSF adjusting; enable 1614 * use of staggered beacons. 1615 */ 1616 sc->sc_stagbeacons = 1; 1617 } 1618 ath_txq_init(sc, &avp->av_mcastq, ATH_TXQ_SWQ); 1619 } 1620 1621 ic->ic_opmode = ic_opmode; 1622 if (opmode != IEEE80211_M_WDS) { 1623 sc->sc_nvaps++; 1624 if (opmode == IEEE80211_M_STA) 1625 sc->sc_nstavaps++; 1626 if (opmode == IEEE80211_M_MBSS) 1627 sc->sc_nmeshvaps++; 1628 } 1629 switch (ic_opmode) { 1630 case IEEE80211_M_IBSS: 1631 sc->sc_opmode = HAL_M_IBSS; 1632 break; 1633 case IEEE80211_M_STA: 1634 sc->sc_opmode = HAL_M_STA; 1635 break; 1636 case IEEE80211_M_AHDEMO: 1637 #ifdef IEEE80211_SUPPORT_TDMA 1638 if (vap->iv_caps & IEEE80211_C_TDMA) { 1639 sc->sc_tdma = 1; 1640 /* NB: disable tsf adjust */ 1641 sc->sc_stagbeacons = 0; 1642 } 1643 /* 1644 * NB: adhoc demo mode is a pseudo mode; to the hal it's 1645 * just ap mode. 1646 */ 1647 /* fall thru... */ 1648 #endif 1649 case IEEE80211_M_HOSTAP: 1650 case IEEE80211_M_MBSS: 1651 sc->sc_opmode = HAL_M_HOSTAP; 1652 break; 1653 case IEEE80211_M_MONITOR: 1654 sc->sc_opmode = HAL_M_MONITOR; 1655 break; 1656 default: 1657 /* XXX should not happen */ 1658 break; 1659 } 1660 if (sc->sc_hastsfadd) { 1661 /* 1662 * Configure whether or not TSF adjust should be done. 1663 */ 1664 ath_hal_settsfadjust(sc->sc_ah, sc->sc_stagbeacons); 1665 } 1666 if (flags & IEEE80211_CLONE_NOBEACONS) { 1667 /* 1668 * Enable s/w beacon miss handling. 1669 */ 1670 sc->sc_swbmiss = 1; 1671 } 1672 ATH_UNLOCK(sc); 1673 1674 /* complete setup */ 1675 ieee80211_vap_attach(vap, ath_media_change, ieee80211_media_status, 1676 mac); 1677 return vap; 1678 bad2: 1679 reclaim_address(sc, mac); 1680 ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask); 1681 bad: 1682 free(avp, M_80211_VAP); 1683 ATH_UNLOCK(sc); 1684 return NULL; 1685 } 1686 1687 static void 1688 ath_vap_delete(struct ieee80211vap *vap) 1689 { 1690 struct ieee80211com *ic = vap->iv_ic; 1691 struct ath_softc *sc = ic->ic_softc; 1692 struct ath_hal *ah = sc->sc_ah; 1693 struct ath_vap *avp = ATH_VAP(vap); 1694 1695 ATH_LOCK(sc); 1696 ath_power_set_power_state(sc, HAL_PM_AWAKE); 1697 ATH_UNLOCK(sc); 1698 1699 DPRINTF(sc, ATH_DEBUG_RESET, "%s: called\n", __func__); 1700 if (sc->sc_running) { 1701 /* 1702 * Quiesce the hardware while we remove the vap. In 1703 * particular we need to reclaim all references to 1704 * the vap state by any frames pending on the tx queues. 1705 */ 1706 ath_hal_intrset(ah, 0); /* disable interrupts */ 1707 /* XXX Do all frames from all vaps/nodes need draining here? */ 1708 ath_stoprecv(sc, 1); /* stop recv side */ 1709 ath_draintxq(sc, ATH_RESET_DEFAULT); /* stop hw xmit side */ 1710 } 1711 1712 /* .. leave the hardware awake for now. */ 1713 1714 ieee80211_vap_detach(vap); 1715 1716 /* 1717 * XXX Danger Will Robinson! Danger! 1718 * 1719 * Because ieee80211_vap_detach() can queue a frame (the station 1720 * diassociate message?) after we've drained the TXQ and 1721 * flushed the software TXQ, we will end up with a frame queued 1722 * to a node whose vap is about to be freed. 1723 * 1724 * To work around this, flush the hardware/software again. 1725 * This may be racy - the ath task may be running and the packet 1726 * may be being scheduled between sw->hw txq. Tsk. 1727 * 1728 * TODO: figure out why a new node gets allocated somewhere around 1729 * here (after the ath_tx_swq() call; and after an ath_stop() 1730 * call!) 1731 */ 1732 1733 ath_draintxq(sc, ATH_RESET_DEFAULT); 1734 1735 ATH_LOCK(sc); 1736 /* 1737 * Reclaim beacon state. Note this must be done before 1738 * the vap instance is reclaimed as we may have a reference 1739 * to it in the buffer for the beacon frame. 1740 */ 1741 if (avp->av_bcbuf != NULL) { 1742 if (avp->av_bslot != -1) { 1743 sc->sc_bslot[avp->av_bslot] = NULL; 1744 sc->sc_nbcnvaps--; 1745 } 1746 ath_beacon_return(sc, avp->av_bcbuf); 1747 avp->av_bcbuf = NULL; 1748 if (sc->sc_nbcnvaps == 0) { 1749 sc->sc_stagbeacons = 0; 1750 if (sc->sc_hastsfadd) 1751 ath_hal_settsfadjust(sc->sc_ah, 0); 1752 } 1753 /* 1754 * Reclaim any pending mcast frames for the vap. 1755 */ 1756 ath_tx_draintxq(sc, &avp->av_mcastq); 1757 } 1758 /* 1759 * Update bookkeeping. 1760 */ 1761 if (vap->iv_opmode == IEEE80211_M_STA) { 1762 sc->sc_nstavaps--; 1763 if (sc->sc_nstavaps == 0 && sc->sc_swbmiss) 1764 sc->sc_swbmiss = 0; 1765 } else if (vap->iv_opmode == IEEE80211_M_HOSTAP || 1766 vap->iv_opmode == IEEE80211_M_MBSS) { 1767 reclaim_address(sc, vap->iv_myaddr); 1768 ath_hal_setbssidmask(ah, sc->sc_hwbssidmask); 1769 if (vap->iv_opmode == IEEE80211_M_MBSS) 1770 sc->sc_nmeshvaps--; 1771 } 1772 if (vap->iv_opmode != IEEE80211_M_WDS) 1773 sc->sc_nvaps--; 1774 #ifdef IEEE80211_SUPPORT_TDMA 1775 /* TDMA operation ceases when the last vap is destroyed */ 1776 if (sc->sc_tdma && sc->sc_nvaps == 0) { 1777 sc->sc_tdma = 0; 1778 sc->sc_swbmiss = 0; 1779 } 1780 #endif 1781 free(avp, M_80211_VAP); 1782 1783 if (sc->sc_running) { 1784 /* 1785 * Restart rx+tx machines if still running (RUNNING will 1786 * be reset if we just destroyed the last vap). 1787 */ 1788 if (ath_startrecv(sc) != 0) 1789 device_printf(sc->sc_dev, 1790 "%s: unable to restart recv logic\n", __func__); 1791 if (sc->sc_beacons) { /* restart beacons */ 1792 #ifdef IEEE80211_SUPPORT_TDMA 1793 if (sc->sc_tdma) 1794 ath_tdma_config(sc, NULL); 1795 else 1796 #endif 1797 ath_beacon_config(sc, NULL); 1798 } 1799 ath_hal_intrset(ah, sc->sc_imask); 1800 } 1801 1802 /* Ok, let the hardware asleep. */ 1803 ath_power_restore_power_state(sc); 1804 ATH_UNLOCK(sc); 1805 } 1806 1807 void 1808 ath_suspend(struct ath_softc *sc) 1809 { 1810 struct ieee80211com *ic = &sc->sc_ic; 1811 1812 sc->sc_resume_up = ic->ic_nrunning != 0; 1813 1814 ieee80211_suspend_all(ic); 1815 /* 1816 * NB: don't worry about putting the chip in low power 1817 * mode; pci will power off our socket on suspend and 1818 * CardBus detaches the device. 1819 * 1820 * XXX TODO: well, that's great, except for non-cardbus 1821 * devices! 1822 */ 1823 1824 /* 1825 * XXX This doesn't wait until all pending taskqueue 1826 * items and parallel transmit/receive/other threads 1827 * are running! 1828 */ 1829 ath_hal_intrset(sc->sc_ah, 0); 1830 taskqueue_block(sc->sc_tq); 1831 1832 ATH_LOCK(sc); 1833 callout_stop(&sc->sc_cal_ch); 1834 ATH_UNLOCK(sc); 1835 1836 /* 1837 * XXX ensure sc_invalid is 1 1838 */ 1839 1840 /* Disable the PCIe PHY, complete with workarounds */ 1841 ath_hal_enablepcie(sc->sc_ah, 1, 1); 1842 } 1843 1844 /* 1845 * Reset the key cache since some parts do not reset the 1846 * contents on resume. First we clear all entries, then 1847 * re-load keys that the 802.11 layer assumes are setup 1848 * in h/w. 1849 */ 1850 static void 1851 ath_reset_keycache(struct ath_softc *sc) 1852 { 1853 struct ieee80211com *ic = &sc->sc_ic; 1854 struct ath_hal *ah = sc->sc_ah; 1855 int i; 1856 1857 ATH_LOCK(sc); 1858 ath_power_set_power_state(sc, HAL_PM_AWAKE); 1859 for (i = 0; i < sc->sc_keymax; i++) 1860 ath_hal_keyreset(ah, i); 1861 ath_power_restore_power_state(sc); 1862 ATH_UNLOCK(sc); 1863 ieee80211_crypto_reload_keys(ic); 1864 } 1865 1866 /* 1867 * Fetch the current chainmask configuration based on the current 1868 * operating channel and options. 1869 */ 1870 static void 1871 ath_update_chainmasks(struct ath_softc *sc, struct ieee80211_channel *chan) 1872 { 1873 1874 /* 1875 * Set TX chainmask to the currently configured chainmask; 1876 * the TX chainmask depends upon the current operating mode. 1877 */ 1878 sc->sc_cur_rxchainmask = sc->sc_rxchainmask; 1879 if (IEEE80211_IS_CHAN_HT(chan)) { 1880 sc->sc_cur_txchainmask = sc->sc_txchainmask; 1881 } else { 1882 sc->sc_cur_txchainmask = 1; 1883 } 1884 1885 DPRINTF(sc, ATH_DEBUG_RESET, 1886 "%s: TX chainmask is now 0x%x, RX is now 0x%x\n", 1887 __func__, 1888 sc->sc_cur_txchainmask, 1889 sc->sc_cur_rxchainmask); 1890 } 1891 1892 void 1893 ath_resume(struct ath_softc *sc) 1894 { 1895 struct ieee80211com *ic = &sc->sc_ic; 1896 struct ath_hal *ah = sc->sc_ah; 1897 HAL_STATUS status; 1898 1899 ath_hal_enablepcie(ah, 0, 0); 1900 1901 /* 1902 * Must reset the chip before we reload the 1903 * keycache as we were powered down on suspend. 1904 */ 1905 ath_update_chainmasks(sc, 1906 sc->sc_curchan != NULL ? sc->sc_curchan : ic->ic_curchan); 1907 ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask, 1908 sc->sc_cur_rxchainmask); 1909 1910 /* Ensure we set the current power state to on */ 1911 ATH_LOCK(sc); 1912 ath_power_setselfgen(sc, HAL_PM_AWAKE); 1913 ath_power_set_power_state(sc, HAL_PM_AWAKE); 1914 ath_power_setpower(sc, HAL_PM_AWAKE); 1915 ATH_UNLOCK(sc); 1916 1917 ath_hal_reset(ah, sc->sc_opmode, 1918 sc->sc_curchan != NULL ? sc->sc_curchan : ic->ic_curchan, 1919 AH_FALSE, HAL_RESET_NORMAL, &status); 1920 ath_reset_keycache(sc); 1921 1922 ATH_RX_LOCK(sc); 1923 sc->sc_rx_stopped = 1; 1924 sc->sc_rx_resetted = 1; 1925 ATH_RX_UNLOCK(sc); 1926 1927 /* Let DFS at it in case it's a DFS channel */ 1928 ath_dfs_radar_enable(sc, ic->ic_curchan); 1929 1930 /* Let spectral at in case spectral is enabled */ 1931 ath_spectral_enable(sc, ic->ic_curchan); 1932 1933 /* 1934 * Let bluetooth coexistence at in case it's needed for this channel 1935 */ 1936 ath_btcoex_enable(sc, ic->ic_curchan); 1937 1938 /* 1939 * If we're doing TDMA, enforce the TXOP limitation for chips that 1940 * support it. 1941 */ 1942 if (sc->sc_hasenforcetxop && sc->sc_tdma) 1943 ath_hal_setenforcetxop(sc->sc_ah, 1); 1944 else 1945 ath_hal_setenforcetxop(sc->sc_ah, 0); 1946 1947 /* Restore the LED configuration */ 1948 ath_led_config(sc); 1949 ath_hal_setledstate(ah, HAL_LED_INIT); 1950 1951 if (sc->sc_resume_up) 1952 ieee80211_resume_all(ic); 1953 1954 ATH_LOCK(sc); 1955 ath_power_restore_power_state(sc); 1956 ATH_UNLOCK(sc); 1957 1958 /* XXX beacons ? */ 1959 } 1960 1961 void 1962 ath_shutdown(struct ath_softc *sc) 1963 { 1964 1965 ATH_LOCK(sc); 1966 ath_stop(sc); 1967 ATH_UNLOCK(sc); 1968 /* NB: no point powering down chip as we're about to reboot */ 1969 } 1970 1971 /* 1972 * Interrupt handler. Most of the actual processing is deferred. 1973 */ 1974 void 1975 ath_intr(void *arg) 1976 { 1977 struct ath_softc *sc = arg; 1978 struct ath_hal *ah = sc->sc_ah; 1979 HAL_INT status = 0; 1980 uint32_t txqs; 1981 1982 /* 1983 * If we're inside a reset path, just print a warning and 1984 * clear the ISR. The reset routine will finish it for us. 1985 */ 1986 ATH_PCU_LOCK(sc); 1987 if (sc->sc_inreset_cnt) { 1988 HAL_INT status; 1989 ath_hal_getisr(ah, &status); /* clear ISR */ 1990 ath_hal_intrset(ah, 0); /* disable further intr's */ 1991 DPRINTF(sc, ATH_DEBUG_ANY, 1992 "%s: in reset, ignoring: status=0x%x\n", 1993 __func__, status); 1994 ATH_PCU_UNLOCK(sc); 1995 return; 1996 } 1997 1998 if (sc->sc_invalid) { 1999 /* 2000 * The hardware is not ready/present, don't touch anything. 2001 * Note this can happen early on if the IRQ is shared. 2002 */ 2003 DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid; ignored\n", __func__); 2004 ATH_PCU_UNLOCK(sc); 2005 return; 2006 } 2007 if (!ath_hal_intrpend(ah)) { /* shared irq, not for us */ 2008 ATH_PCU_UNLOCK(sc); 2009 return; 2010 } 2011 2012 ATH_LOCK(sc); 2013 ath_power_set_power_state(sc, HAL_PM_AWAKE); 2014 ATH_UNLOCK(sc); 2015 2016 if (sc->sc_ic.ic_nrunning == 0 && sc->sc_running == 0) { 2017 HAL_INT status; 2018 2019 DPRINTF(sc, ATH_DEBUG_ANY, "%s: ic_nrunning %d sc_running %d\n", 2020 __func__, sc->sc_ic.ic_nrunning, sc->sc_running); 2021 ath_hal_getisr(ah, &status); /* clear ISR */ 2022 ath_hal_intrset(ah, 0); /* disable further intr's */ 2023 ATH_PCU_UNLOCK(sc); 2024 2025 ATH_LOCK(sc); 2026 ath_power_restore_power_state(sc); 2027 ATH_UNLOCK(sc); 2028 return; 2029 } 2030 2031 /* 2032 * Figure out the reason(s) for the interrupt. Note 2033 * that the hal returns a pseudo-ISR that may include 2034 * bits we haven't explicitly enabled so we mask the 2035 * value to insure we only process bits we requested. 2036 */ 2037 ath_hal_getisr(ah, &status); /* NB: clears ISR too */ 2038 DPRINTF(sc, ATH_DEBUG_INTR, "%s: status 0x%x\n", __func__, status); 2039 ATH_KTR(sc, ATH_KTR_INTERRUPTS, 1, "ath_intr: mask=0x%.8x", status); 2040 #ifdef ATH_DEBUG_ALQ 2041 if_ath_alq_post_intr(&sc->sc_alq, status, ah->ah_intrstate, 2042 ah->ah_syncstate); 2043 #endif /* ATH_DEBUG_ALQ */ 2044 #ifdef ATH_KTR_INTR_DEBUG 2045 ATH_KTR(sc, ATH_KTR_INTERRUPTS, 5, 2046 "ath_intr: ISR=0x%.8x, ISR_S0=0x%.8x, ISR_S1=0x%.8x, ISR_S2=0x%.8x, ISR_S5=0x%.8x", 2047 ah->ah_intrstate[0], 2048 ah->ah_intrstate[1], 2049 ah->ah_intrstate[2], 2050 ah->ah_intrstate[3], 2051 ah->ah_intrstate[6]); 2052 #endif 2053 2054 /* Squirrel away SYNC interrupt debugging */ 2055 if (ah->ah_syncstate != 0) { 2056 int i; 2057 for (i = 0; i < 32; i++) 2058 if (ah->ah_syncstate & (i << i)) 2059 sc->sc_intr_stats.sync_intr[i]++; 2060 } 2061 2062 status &= sc->sc_imask; /* discard unasked for bits */ 2063 2064 /* Short-circuit un-handled interrupts */ 2065 if (status == 0x0) { 2066 ATH_PCU_UNLOCK(sc); 2067 2068 ATH_LOCK(sc); 2069 ath_power_restore_power_state(sc); 2070 ATH_UNLOCK(sc); 2071 2072 return; 2073 } 2074 2075 /* 2076 * Take a note that we're inside the interrupt handler, so 2077 * the reset routines know to wait. 2078 */ 2079 sc->sc_intr_cnt++; 2080 ATH_PCU_UNLOCK(sc); 2081 2082 /* 2083 * Handle the interrupt. We won't run concurrent with the reset 2084 * or channel change routines as they'll wait for sc_intr_cnt 2085 * to be 0 before continuing. 2086 */ 2087 if (status & HAL_INT_FATAL) { 2088 sc->sc_stats.ast_hardware++; 2089 ath_hal_intrset(ah, 0); /* disable intr's until reset */ 2090 taskqueue_enqueue(sc->sc_tq, &sc->sc_fataltask); 2091 } else { 2092 if (status & HAL_INT_SWBA) { 2093 /* 2094 * Software beacon alert--time to send a beacon. 2095 * Handle beacon transmission directly; deferring 2096 * this is too slow to meet timing constraints 2097 * under load. 2098 */ 2099 #ifdef IEEE80211_SUPPORT_TDMA 2100 if (sc->sc_tdma) { 2101 if (sc->sc_tdmaswba == 0) { 2102 struct ieee80211com *ic = &sc->sc_ic; 2103 struct ieee80211vap *vap = 2104 TAILQ_FIRST(&ic->ic_vaps); 2105 ath_tdma_beacon_send(sc, vap); 2106 sc->sc_tdmaswba = 2107 vap->iv_tdma->tdma_bintval; 2108 } else 2109 sc->sc_tdmaswba--; 2110 } else 2111 #endif 2112 { 2113 ath_beacon_proc(sc, 0); 2114 #ifdef IEEE80211_SUPPORT_SUPERG 2115 /* 2116 * Schedule the rx taskq in case there's no 2117 * traffic so any frames held on the staging 2118 * queue are aged and potentially flushed. 2119 */ 2120 sc->sc_rx.recv_sched(sc, 1); 2121 #endif 2122 } 2123 } 2124 if (status & HAL_INT_RXEOL) { 2125 int imask; 2126 ATH_KTR(sc, ATH_KTR_ERROR, 0, "ath_intr: RXEOL"); 2127 if (! sc->sc_isedma) { 2128 ATH_PCU_LOCK(sc); 2129 /* 2130 * NB: the hardware should re-read the link when 2131 * RXE bit is written, but it doesn't work at 2132 * least on older hardware revs. 2133 */ 2134 sc->sc_stats.ast_rxeol++; 2135 /* 2136 * Disable RXEOL/RXORN - prevent an interrupt 2137 * storm until the PCU logic can be reset. 2138 * In case the interface is reset some other 2139 * way before "sc_kickpcu" is called, don't 2140 * modify sc_imask - that way if it is reset 2141 * by a call to ath_reset() somehow, the 2142 * interrupt mask will be correctly reprogrammed. 2143 */ 2144 imask = sc->sc_imask; 2145 imask &= ~(HAL_INT_RXEOL | HAL_INT_RXORN); 2146 ath_hal_intrset(ah, imask); 2147 /* 2148 * Only blank sc_rxlink if we've not yet kicked 2149 * the PCU. 2150 * 2151 * This isn't entirely correct - the correct solution 2152 * would be to have a PCU lock and engage that for 2153 * the duration of the PCU fiddling; which would include 2154 * running the RX process. Otherwise we could end up 2155 * messing up the RX descriptor chain and making the 2156 * RX desc list much shorter. 2157 */ 2158 if (! sc->sc_kickpcu) 2159 sc->sc_rxlink = NULL; 2160 sc->sc_kickpcu = 1; 2161 ATH_PCU_UNLOCK(sc); 2162 } 2163 /* 2164 * Enqueue an RX proc to handle whatever 2165 * is in the RX queue. 2166 * This will then kick the PCU if required. 2167 */ 2168 sc->sc_rx.recv_sched(sc, 1); 2169 } 2170 if (status & HAL_INT_TXURN) { 2171 sc->sc_stats.ast_txurn++; 2172 /* bump tx trigger level */ 2173 ath_hal_updatetxtriglevel(ah, AH_TRUE); 2174 } 2175 /* 2176 * Handle both the legacy and RX EDMA interrupt bits. 2177 * Note that HAL_INT_RXLP is also HAL_INT_RXDESC. 2178 */ 2179 if (status & (HAL_INT_RX | HAL_INT_RXHP | HAL_INT_RXLP)) { 2180 sc->sc_stats.ast_rx_intr++; 2181 sc->sc_rx.recv_sched(sc, 1); 2182 } 2183 if (status & HAL_INT_TX) { 2184 sc->sc_stats.ast_tx_intr++; 2185 /* 2186 * Grab all the currently set bits in the HAL txq bitmap 2187 * and blank them. This is the only place we should be 2188 * doing this. 2189 */ 2190 if (! sc->sc_isedma) { 2191 ATH_PCU_LOCK(sc); 2192 txqs = 0xffffffff; 2193 ath_hal_gettxintrtxqs(sc->sc_ah, &txqs); 2194 ATH_KTR(sc, ATH_KTR_INTERRUPTS, 3, 2195 "ath_intr: TX; txqs=0x%08x, txq_active was 0x%08x, now 0x%08x", 2196 txqs, 2197 sc->sc_txq_active, 2198 sc->sc_txq_active | txqs); 2199 sc->sc_txq_active |= txqs; 2200 ATH_PCU_UNLOCK(sc); 2201 } 2202 taskqueue_enqueue(sc->sc_tq, &sc->sc_txtask); 2203 } 2204 if (status & HAL_INT_BMISS) { 2205 sc->sc_stats.ast_bmiss++; 2206 taskqueue_enqueue(sc->sc_tq, &sc->sc_bmisstask); 2207 } 2208 if (status & HAL_INT_GTT) 2209 sc->sc_stats.ast_tx_timeout++; 2210 if (status & HAL_INT_CST) 2211 sc->sc_stats.ast_tx_cst++; 2212 if (status & HAL_INT_MIB) { 2213 sc->sc_stats.ast_mib++; 2214 ATH_PCU_LOCK(sc); 2215 /* 2216 * Disable interrupts until we service the MIB 2217 * interrupt; otherwise it will continue to fire. 2218 */ 2219 ath_hal_intrset(ah, 0); 2220 /* 2221 * Let the hal handle the event. We assume it will 2222 * clear whatever condition caused the interrupt. 2223 */ 2224 ath_hal_mibevent(ah, &sc->sc_halstats); 2225 /* 2226 * Don't reset the interrupt if we've just 2227 * kicked the PCU, or we may get a nested 2228 * RXEOL before the rxproc has had a chance 2229 * to run. 2230 */ 2231 if (sc->sc_kickpcu == 0) 2232 ath_hal_intrset(ah, sc->sc_imask); 2233 ATH_PCU_UNLOCK(sc); 2234 } 2235 if (status & HAL_INT_RXORN) { 2236 /* NB: hal marks HAL_INT_FATAL when RXORN is fatal */ 2237 ATH_KTR(sc, ATH_KTR_ERROR, 0, "ath_intr: RXORN"); 2238 sc->sc_stats.ast_rxorn++; 2239 } 2240 if (status & HAL_INT_TSFOOR) { 2241 device_printf(sc->sc_dev, "%s: TSFOOR\n", __func__); 2242 sc->sc_syncbeacon = 1; 2243 } 2244 } 2245 ATH_PCU_LOCK(sc); 2246 sc->sc_intr_cnt--; 2247 ATH_PCU_UNLOCK(sc); 2248 2249 ATH_LOCK(sc); 2250 ath_power_restore_power_state(sc); 2251 ATH_UNLOCK(sc); 2252 } 2253 2254 static void 2255 ath_fatal_proc(void *arg, int pending) 2256 { 2257 struct ath_softc *sc = arg; 2258 u_int32_t *state; 2259 u_int32_t len; 2260 void *sp; 2261 2262 if (sc->sc_invalid) 2263 return; 2264 2265 device_printf(sc->sc_dev, "hardware error; resetting\n"); 2266 /* 2267 * Fatal errors are unrecoverable. Typically these 2268 * are caused by DMA errors. Collect h/w state from 2269 * the hal so we can diagnose what's going on. 2270 */ 2271 if (ath_hal_getfatalstate(sc->sc_ah, &sp, &len)) { 2272 KASSERT(len >= 6*sizeof(u_int32_t), ("len %u bytes", len)); 2273 state = sp; 2274 device_printf(sc->sc_dev, 2275 "0x%08x 0x%08x 0x%08x, 0x%08x 0x%08x 0x%08x\n", state[0], 2276 state[1] , state[2], state[3], state[4], state[5]); 2277 } 2278 ath_reset(sc, ATH_RESET_NOLOSS); 2279 } 2280 2281 static void 2282 ath_bmiss_vap(struct ieee80211vap *vap) 2283 { 2284 struct ath_softc *sc = vap->iv_ic->ic_softc; 2285 2286 /* 2287 * Workaround phantom bmiss interrupts by sanity-checking 2288 * the time of our last rx'd frame. If it is within the 2289 * beacon miss interval then ignore the interrupt. If it's 2290 * truly a bmiss we'll get another interrupt soon and that'll 2291 * be dispatched up for processing. Note this applies only 2292 * for h/w beacon miss events. 2293 */ 2294 2295 /* 2296 * XXX TODO: Just read the TSF during the interrupt path; 2297 * that way we don't have to wake up again just to read it 2298 * again. 2299 */ 2300 ATH_LOCK(sc); 2301 ath_power_set_power_state(sc, HAL_PM_AWAKE); 2302 ATH_UNLOCK(sc); 2303 2304 if ((vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) == 0) { 2305 u_int64_t lastrx = sc->sc_lastrx; 2306 u_int64_t tsf = ath_hal_gettsf64(sc->sc_ah); 2307 /* XXX should take a locked ref to iv_bss */ 2308 u_int bmisstimeout = 2309 vap->iv_bmissthreshold * vap->iv_bss->ni_intval * 1024; 2310 2311 DPRINTF(sc, ATH_DEBUG_BEACON, 2312 "%s: tsf %llu lastrx %lld (%llu) bmiss %u\n", 2313 __func__, (unsigned long long) tsf, 2314 (unsigned long long)(tsf - lastrx), 2315 (unsigned long long) lastrx, bmisstimeout); 2316 2317 if (tsf - lastrx <= bmisstimeout) { 2318 sc->sc_stats.ast_bmiss_phantom++; 2319 2320 ATH_LOCK(sc); 2321 ath_power_restore_power_state(sc); 2322 ATH_UNLOCK(sc); 2323 2324 return; 2325 } 2326 } 2327 2328 /* 2329 * There's no need to keep the hardware awake during the call 2330 * to av_bmiss(). 2331 */ 2332 ATH_LOCK(sc); 2333 ath_power_restore_power_state(sc); 2334 ATH_UNLOCK(sc); 2335 2336 /* 2337 * Attempt to force a beacon resync. 2338 */ 2339 sc->sc_syncbeacon = 1; 2340 2341 ATH_VAP(vap)->av_bmiss(vap); 2342 } 2343 2344 /* XXX this needs a force wakeup! */ 2345 int 2346 ath_hal_gethangstate(struct ath_hal *ah, uint32_t mask, uint32_t *hangs) 2347 { 2348 uint32_t rsize; 2349 void *sp; 2350 2351 if (!ath_hal_getdiagstate(ah, HAL_DIAG_CHECK_HANGS, &mask, sizeof(mask), &sp, &rsize)) 2352 return 0; 2353 KASSERT(rsize == sizeof(uint32_t), ("resultsize %u", rsize)); 2354 *hangs = *(uint32_t *)sp; 2355 return 1; 2356 } 2357 2358 static void 2359 ath_bmiss_proc(void *arg, int pending) 2360 { 2361 struct ath_softc *sc = arg; 2362 uint32_t hangs; 2363 2364 DPRINTF(sc, ATH_DEBUG_ANY, "%s: pending %u\n", __func__, pending); 2365 2366 ATH_LOCK(sc); 2367 ath_power_set_power_state(sc, HAL_PM_AWAKE); 2368 ATH_UNLOCK(sc); 2369 2370 ath_beacon_miss(sc); 2371 2372 /* 2373 * Do a reset upon any becaon miss event. 2374 * 2375 * It may be a non-recognised RX clear hang which needs a reset 2376 * to clear. 2377 */ 2378 if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0) { 2379 ath_reset(sc, ATH_RESET_NOLOSS); 2380 device_printf(sc->sc_dev, 2381 "bb hang detected (0x%x), resetting\n", hangs); 2382 } else { 2383 ath_reset(sc, ATH_RESET_NOLOSS); 2384 ieee80211_beacon_miss(&sc->sc_ic); 2385 } 2386 2387 /* Force a beacon resync, in case they've drifted */ 2388 sc->sc_syncbeacon = 1; 2389 2390 ATH_LOCK(sc); 2391 ath_power_restore_power_state(sc); 2392 ATH_UNLOCK(sc); 2393 } 2394 2395 /* 2396 * Handle TKIP MIC setup to deal hardware that doesn't do MIC 2397 * calcs together with WME. If necessary disable the crypto 2398 * hardware and mark the 802.11 state so keys will be setup 2399 * with the MIC work done in software. 2400 */ 2401 static void 2402 ath_settkipmic(struct ath_softc *sc) 2403 { 2404 struct ieee80211com *ic = &sc->sc_ic; 2405 2406 if ((ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIP) && !sc->sc_wmetkipmic) { 2407 if (ic->ic_flags & IEEE80211_F_WME) { 2408 ath_hal_settkipmic(sc->sc_ah, AH_FALSE); 2409 ic->ic_cryptocaps &= ~IEEE80211_CRYPTO_TKIPMIC; 2410 } else { 2411 ath_hal_settkipmic(sc->sc_ah, AH_TRUE); 2412 ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC; 2413 } 2414 } 2415 } 2416 2417 static int 2418 ath_init(struct ath_softc *sc) 2419 { 2420 struct ieee80211com *ic = &sc->sc_ic; 2421 struct ath_hal *ah = sc->sc_ah; 2422 HAL_STATUS status; 2423 2424 ATH_LOCK_ASSERT(sc); 2425 2426 /* 2427 * Force the sleep state awake. 2428 */ 2429 ath_power_setselfgen(sc, HAL_PM_AWAKE); 2430 ath_power_set_power_state(sc, HAL_PM_AWAKE); 2431 ath_power_setpower(sc, HAL_PM_AWAKE); 2432 2433 /* 2434 * Stop anything previously setup. This is safe 2435 * whether this is the first time through or not. 2436 */ 2437 ath_stop(sc); 2438 2439 /* 2440 * The basic interface to setting the hardware in a good 2441 * state is ``reset''. On return the hardware is known to 2442 * be powered up and with interrupts disabled. This must 2443 * be followed by initialization of the appropriate bits 2444 * and then setup of the interrupt mask. 2445 */ 2446 ath_settkipmic(sc); 2447 ath_update_chainmasks(sc, ic->ic_curchan); 2448 ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask, 2449 sc->sc_cur_rxchainmask); 2450 2451 if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_FALSE, 2452 HAL_RESET_NORMAL, &status)) { 2453 device_printf(sc->sc_dev, 2454 "unable to reset hardware; hal status %u\n", status); 2455 return (ENODEV); 2456 } 2457 2458 ATH_RX_LOCK(sc); 2459 sc->sc_rx_stopped = 1; 2460 sc->sc_rx_resetted = 1; 2461 ATH_RX_UNLOCK(sc); 2462 2463 ath_chan_change(sc, ic->ic_curchan); 2464 2465 /* Let DFS at it in case it's a DFS channel */ 2466 ath_dfs_radar_enable(sc, ic->ic_curchan); 2467 2468 /* Let spectral at in case spectral is enabled */ 2469 ath_spectral_enable(sc, ic->ic_curchan); 2470 2471 /* 2472 * Let bluetooth coexistence at in case it's needed for this channel 2473 */ 2474 ath_btcoex_enable(sc, ic->ic_curchan); 2475 2476 /* 2477 * If we're doing TDMA, enforce the TXOP limitation for chips that 2478 * support it. 2479 */ 2480 if (sc->sc_hasenforcetxop && sc->sc_tdma) 2481 ath_hal_setenforcetxop(sc->sc_ah, 1); 2482 else 2483 ath_hal_setenforcetxop(sc->sc_ah, 0); 2484 2485 /* 2486 * Likewise this is set during reset so update 2487 * state cached in the driver. 2488 */ 2489 sc->sc_diversity = ath_hal_getdiversity(ah); 2490 sc->sc_lastlongcal = ticks; 2491 sc->sc_resetcal = 1; 2492 sc->sc_lastcalreset = 0; 2493 sc->sc_lastani = ticks; 2494 sc->sc_lastshortcal = ticks; 2495 sc->sc_doresetcal = AH_FALSE; 2496 /* 2497 * Beacon timers were cleared here; give ath_newstate() 2498 * a hint that the beacon timers should be poked when 2499 * things transition to the RUN state. 2500 */ 2501 sc->sc_beacons = 0; 2502 2503 /* 2504 * Setup the hardware after reset: the key cache 2505 * is filled as needed and the receive engine is 2506 * set going. Frame transmit is handled entirely 2507 * in the frame output path; there's nothing to do 2508 * here except setup the interrupt mask. 2509 */ 2510 if (ath_startrecv(sc) != 0) { 2511 device_printf(sc->sc_dev, "unable to start recv logic\n"); 2512 ath_power_restore_power_state(sc); 2513 return (ENODEV); 2514 } 2515 2516 /* 2517 * Enable interrupts. 2518 */ 2519 sc->sc_imask = HAL_INT_RX | HAL_INT_TX 2520 | HAL_INT_RXORN | HAL_INT_TXURN 2521 | HAL_INT_FATAL | HAL_INT_GLOBAL; 2522 2523 /* 2524 * Enable RX EDMA bits. Note these overlap with 2525 * HAL_INT_RX and HAL_INT_RXDESC respectively. 2526 */ 2527 if (sc->sc_isedma) 2528 sc->sc_imask |= (HAL_INT_RXHP | HAL_INT_RXLP); 2529 2530 /* 2531 * If we're an EDMA NIC, we don't care about RXEOL. 2532 * Writing a new descriptor in will simply restart 2533 * RX DMA. 2534 */ 2535 if (! sc->sc_isedma) 2536 sc->sc_imask |= HAL_INT_RXEOL; 2537 2538 /* 2539 * Enable MIB interrupts when there are hardware phy counters. 2540 * Note we only do this (at the moment) for station mode. 2541 */ 2542 if (sc->sc_needmib && ic->ic_opmode == IEEE80211_M_STA) 2543 sc->sc_imask |= HAL_INT_MIB; 2544 2545 /* 2546 * XXX add capability for this. 2547 * 2548 * If we're in STA mode (and maybe IBSS?) then register for 2549 * TSFOOR interrupts. 2550 */ 2551 if (ic->ic_opmode == IEEE80211_M_STA) 2552 sc->sc_imask |= HAL_INT_TSFOOR; 2553 2554 /* Enable global TX timeout and carrier sense timeout if available */ 2555 if (ath_hal_gtxto_supported(ah)) 2556 sc->sc_imask |= HAL_INT_GTT; 2557 2558 DPRINTF(sc, ATH_DEBUG_RESET, "%s: imask=0x%x\n", 2559 __func__, sc->sc_imask); 2560 2561 sc->sc_running = 1; 2562 callout_reset(&sc->sc_wd_ch, hz, ath_watchdog, sc); 2563 ath_hal_intrset(ah, sc->sc_imask); 2564 2565 ath_power_restore_power_state(sc); 2566 2567 return (0); 2568 } 2569 2570 static void 2571 ath_stop(struct ath_softc *sc) 2572 { 2573 struct ath_hal *ah = sc->sc_ah; 2574 2575 ATH_LOCK_ASSERT(sc); 2576 2577 /* 2578 * Wake the hardware up before fiddling with it. 2579 */ 2580 ath_power_set_power_state(sc, HAL_PM_AWAKE); 2581 2582 if (sc->sc_running) { 2583 /* 2584 * Shutdown the hardware and driver: 2585 * reset 802.11 state machine 2586 * turn off timers 2587 * disable interrupts 2588 * turn off the radio 2589 * clear transmit machinery 2590 * clear receive machinery 2591 * drain and release tx queues 2592 * reclaim beacon resources 2593 * power down hardware 2594 * 2595 * Note that some of this work is not possible if the 2596 * hardware is gone (invalid). 2597 */ 2598 #ifdef ATH_TX99_DIAG 2599 if (sc->sc_tx99 != NULL) 2600 sc->sc_tx99->stop(sc->sc_tx99); 2601 #endif 2602 callout_stop(&sc->sc_wd_ch); 2603 sc->sc_wd_timer = 0; 2604 sc->sc_running = 0; 2605 if (!sc->sc_invalid) { 2606 if (sc->sc_softled) { 2607 callout_stop(&sc->sc_ledtimer); 2608 ath_hal_gpioset(ah, sc->sc_ledpin, 2609 !sc->sc_ledon); 2610 sc->sc_blinking = 0; 2611 } 2612 ath_hal_intrset(ah, 0); 2613 } 2614 /* XXX we should stop RX regardless of whether it's valid */ 2615 if (!sc->sc_invalid) { 2616 ath_stoprecv(sc, 1); 2617 ath_hal_phydisable(ah); 2618 } else 2619 sc->sc_rxlink = NULL; 2620 ath_draintxq(sc, ATH_RESET_DEFAULT); 2621 ath_beacon_free(sc); /* XXX not needed */ 2622 } 2623 2624 /* And now, restore the current power state */ 2625 ath_power_restore_power_state(sc); 2626 } 2627 2628 /* 2629 * Wait until all pending TX/RX has completed. 2630 * 2631 * This waits until all existing transmit, receive and interrupts 2632 * have completed. It's assumed that the caller has first 2633 * grabbed the reset lock so it doesn't try to do overlapping 2634 * chip resets. 2635 */ 2636 #define MAX_TXRX_ITERATIONS 100 2637 static void 2638 ath_txrx_stop_locked(struct ath_softc *sc) 2639 { 2640 int i = MAX_TXRX_ITERATIONS; 2641 2642 ATH_UNLOCK_ASSERT(sc); 2643 ATH_PCU_LOCK_ASSERT(sc); 2644 2645 /* 2646 * Sleep until all the pending operations have completed. 2647 * 2648 * The caller must ensure that reset has been incremented 2649 * or the pending operations may continue being queued. 2650 */ 2651 while (sc->sc_rxproc_cnt || sc->sc_txproc_cnt || 2652 sc->sc_txstart_cnt || sc->sc_intr_cnt) { 2653 if (i <= 0) 2654 break; 2655 msleep(sc, &sc->sc_pcu_mtx, 0, "ath_txrx_stop", 2656 msecs_to_ticks(10)); 2657 i--; 2658 } 2659 2660 if (i <= 0) 2661 device_printf(sc->sc_dev, 2662 "%s: didn't finish after %d iterations\n", 2663 __func__, MAX_TXRX_ITERATIONS); 2664 } 2665 #undef MAX_TXRX_ITERATIONS 2666 2667 #if 0 2668 static void 2669 ath_txrx_stop(struct ath_softc *sc) 2670 { 2671 ATH_UNLOCK_ASSERT(sc); 2672 ATH_PCU_UNLOCK_ASSERT(sc); 2673 2674 ATH_PCU_LOCK(sc); 2675 ath_txrx_stop_locked(sc); 2676 ATH_PCU_UNLOCK(sc); 2677 } 2678 #endif 2679 2680 static void 2681 ath_txrx_start(struct ath_softc *sc) 2682 { 2683 2684 taskqueue_unblock(sc->sc_tq); 2685 } 2686 2687 /* 2688 * Grab the reset lock, and wait around until noone else 2689 * is trying to do anything with it. 2690 * 2691 * This is totally horrible but we can't hold this lock for 2692 * long enough to do TX/RX or we end up with net80211/ip stack 2693 * LORs and eventual deadlock. 2694 * 2695 * "dowait" signals whether to spin, waiting for the reset 2696 * lock count to reach 0. This should (for now) only be used 2697 * during the reset path, as the rest of the code may not 2698 * be locking-reentrant enough to behave correctly. 2699 * 2700 * Another, cleaner way should be found to serialise all of 2701 * these operations. 2702 */ 2703 #define MAX_RESET_ITERATIONS 25 2704 static int 2705 ath_reset_grablock(struct ath_softc *sc, int dowait) 2706 { 2707 int w = 0; 2708 int i = MAX_RESET_ITERATIONS; 2709 2710 ATH_PCU_LOCK_ASSERT(sc); 2711 do { 2712 if (sc->sc_inreset_cnt == 0) { 2713 w = 1; 2714 break; 2715 } 2716 if (dowait == 0) { 2717 w = 0; 2718 break; 2719 } 2720 ATH_PCU_UNLOCK(sc); 2721 /* 2722 * 1 tick is likely not enough time for long calibrations 2723 * to complete. So we should wait quite a while. 2724 */ 2725 pause("ath_reset_grablock", msecs_to_ticks(100)); 2726 i--; 2727 ATH_PCU_LOCK(sc); 2728 } while (i > 0); 2729 2730 /* 2731 * We always increment the refcounter, regardless 2732 * of whether we succeeded to get it in an exclusive 2733 * way. 2734 */ 2735 sc->sc_inreset_cnt++; 2736 2737 if (i <= 0) 2738 device_printf(sc->sc_dev, 2739 "%s: didn't finish after %d iterations\n", 2740 __func__, MAX_RESET_ITERATIONS); 2741 2742 if (w == 0) 2743 device_printf(sc->sc_dev, 2744 "%s: warning, recursive reset path!\n", 2745 __func__); 2746 2747 return w; 2748 } 2749 #undef MAX_RESET_ITERATIONS 2750 2751 /* 2752 * Reset the hardware w/o losing operational state. This is 2753 * basically a more efficient way of doing ath_stop, ath_init, 2754 * followed by state transitions to the current 802.11 2755 * operational state. Used to recover from various errors and 2756 * to reset or reload hardware state. 2757 */ 2758 int 2759 ath_reset(struct ath_softc *sc, ATH_RESET_TYPE reset_type) 2760 { 2761 struct ieee80211com *ic = &sc->sc_ic; 2762 struct ath_hal *ah = sc->sc_ah; 2763 HAL_STATUS status; 2764 int i; 2765 2766 DPRINTF(sc, ATH_DEBUG_RESET, "%s: called\n", __func__); 2767 2768 /* Ensure ATH_LOCK isn't held; ath_rx_proc can't be locked */ 2769 ATH_PCU_UNLOCK_ASSERT(sc); 2770 ATH_UNLOCK_ASSERT(sc); 2771 2772 /* Try to (stop any further TX/RX from occuring */ 2773 taskqueue_block(sc->sc_tq); 2774 2775 /* 2776 * Wake the hardware up. 2777 */ 2778 ATH_LOCK(sc); 2779 ath_power_set_power_state(sc, HAL_PM_AWAKE); 2780 ATH_UNLOCK(sc); 2781 2782 ATH_PCU_LOCK(sc); 2783 2784 /* 2785 * Grab the reset lock before TX/RX is stopped. 2786 * 2787 * This is needed to ensure that when the TX/RX actually does finish, 2788 * no further TX/RX/reset runs in parallel with this. 2789 */ 2790 if (ath_reset_grablock(sc, 1) == 0) { 2791 device_printf(sc->sc_dev, "%s: concurrent reset! Danger!\n", 2792 __func__); 2793 } 2794 2795 /* disable interrupts */ 2796 ath_hal_intrset(ah, 0); 2797 2798 /* 2799 * Now, ensure that any in progress TX/RX completes before we 2800 * continue. 2801 */ 2802 ath_txrx_stop_locked(sc); 2803 2804 ATH_PCU_UNLOCK(sc); 2805 2806 /* 2807 * Regardless of whether we're doing a no-loss flush or 2808 * not, stop the PCU and handle what's in the RX queue. 2809 * That way frames aren't dropped which shouldn't be. 2810 */ 2811 ath_stoprecv(sc, (reset_type != ATH_RESET_NOLOSS)); 2812 ath_rx_flush(sc); 2813 2814 /* 2815 * Should now wait for pending TX/RX to complete 2816 * and block future ones from occuring. This needs to be 2817 * done before the TX queue is drained. 2818 */ 2819 ath_draintxq(sc, reset_type); /* stop xmit side */ 2820 2821 ath_settkipmic(sc); /* configure TKIP MIC handling */ 2822 /* NB: indicate channel change so we do a full reset */ 2823 ath_update_chainmasks(sc, ic->ic_curchan); 2824 ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask, 2825 sc->sc_cur_rxchainmask); 2826 if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_TRUE, 2827 HAL_RESET_NORMAL, &status)) 2828 device_printf(sc->sc_dev, 2829 "%s: unable to reset hardware; hal status %u\n", 2830 __func__, status); 2831 sc->sc_diversity = ath_hal_getdiversity(ah); 2832 2833 ATH_RX_LOCK(sc); 2834 sc->sc_rx_stopped = 1; 2835 sc->sc_rx_resetted = 1; 2836 ATH_RX_UNLOCK(sc); 2837 2838 /* Let DFS at it in case it's a DFS channel */ 2839 ath_dfs_radar_enable(sc, ic->ic_curchan); 2840 2841 /* Let spectral at in case spectral is enabled */ 2842 ath_spectral_enable(sc, ic->ic_curchan); 2843 2844 /* 2845 * Let bluetooth coexistence at in case it's needed for this channel 2846 */ 2847 ath_btcoex_enable(sc, ic->ic_curchan); 2848 2849 /* 2850 * If we're doing TDMA, enforce the TXOP limitation for chips that 2851 * support it. 2852 */ 2853 if (sc->sc_hasenforcetxop && sc->sc_tdma) 2854 ath_hal_setenforcetxop(sc->sc_ah, 1); 2855 else 2856 ath_hal_setenforcetxop(sc->sc_ah, 0); 2857 2858 if (ath_startrecv(sc) != 0) /* restart recv */ 2859 device_printf(sc->sc_dev, 2860 "%s: unable to start recv logic\n", __func__); 2861 /* 2862 * We may be doing a reset in response to an ioctl 2863 * that changes the channel so update any state that 2864 * might change as a result. 2865 */ 2866 ath_chan_change(sc, ic->ic_curchan); 2867 if (sc->sc_beacons) { /* restart beacons */ 2868 #ifdef IEEE80211_SUPPORT_TDMA 2869 if (sc->sc_tdma) 2870 ath_tdma_config(sc, NULL); 2871 else 2872 #endif 2873 ath_beacon_config(sc, NULL); 2874 } 2875 2876 /* 2877 * Release the reset lock and re-enable interrupts here. 2878 * If an interrupt was being processed in ath_intr(), 2879 * it would disable interrupts at this point. So we have 2880 * to atomically enable interrupts and decrement the 2881 * reset counter - this way ath_intr() doesn't end up 2882 * disabling interrupts without a corresponding enable 2883 * in the rest or channel change path. 2884 * 2885 * Grab the TX reference in case we need to transmit. 2886 * That way a parallel transmit doesn't. 2887 */ 2888 ATH_PCU_LOCK(sc); 2889 sc->sc_inreset_cnt--; 2890 sc->sc_txstart_cnt++; 2891 /* XXX only do this if sc_inreset_cnt == 0? */ 2892 ath_hal_intrset(ah, sc->sc_imask); 2893 ATH_PCU_UNLOCK(sc); 2894 2895 /* 2896 * TX and RX can be started here. If it were started with 2897 * sc_inreset_cnt > 0, the TX and RX path would abort. 2898 * Thus if this is a nested call through the reset or 2899 * channel change code, TX completion will occur but 2900 * RX completion and ath_start / ath_tx_start will not 2901 * run. 2902 */ 2903 2904 /* Restart TX/RX as needed */ 2905 ath_txrx_start(sc); 2906 2907 /* XXX TODO: we need to hold the tx refcount here! */ 2908 2909 /* Restart TX completion and pending TX */ 2910 if (reset_type == ATH_RESET_NOLOSS) { 2911 for (i = 0; i < HAL_NUM_TX_QUEUES; i++) { 2912 if (ATH_TXQ_SETUP(sc, i)) { 2913 ATH_TXQ_LOCK(&sc->sc_txq[i]); 2914 ath_txq_restart_dma(sc, &sc->sc_txq[i]); 2915 ATH_TXQ_UNLOCK(&sc->sc_txq[i]); 2916 2917 ATH_TX_LOCK(sc); 2918 ath_txq_sched(sc, &sc->sc_txq[i]); 2919 ATH_TX_UNLOCK(sc); 2920 } 2921 } 2922 } 2923 2924 ATH_LOCK(sc); 2925 ath_power_restore_power_state(sc); 2926 ATH_UNLOCK(sc); 2927 2928 ATH_PCU_LOCK(sc); 2929 sc->sc_txstart_cnt--; 2930 ATH_PCU_UNLOCK(sc); 2931 2932 /* Handle any frames in the TX queue */ 2933 /* 2934 * XXX should this be done by the caller, rather than 2935 * ath_reset() ? 2936 */ 2937 ath_tx_kick(sc); /* restart xmit */ 2938 return 0; 2939 } 2940 2941 static int 2942 ath_reset_vap(struct ieee80211vap *vap, u_long cmd) 2943 { 2944 struct ieee80211com *ic = vap->iv_ic; 2945 struct ath_softc *sc = ic->ic_softc; 2946 struct ath_hal *ah = sc->sc_ah; 2947 2948 switch (cmd) { 2949 case IEEE80211_IOC_TXPOWER: 2950 /* 2951 * If per-packet TPC is enabled, then we have nothing 2952 * to do; otherwise we need to force the global limit. 2953 * All this can happen directly; no need to reset. 2954 */ 2955 if (!ath_hal_gettpc(ah)) 2956 ath_hal_settxpowlimit(ah, ic->ic_txpowlimit); 2957 return 0; 2958 } 2959 /* XXX? Full or NOLOSS? */ 2960 return ath_reset(sc, ATH_RESET_FULL); 2961 } 2962 2963 struct ath_buf * 2964 _ath_getbuf_locked(struct ath_softc *sc, ath_buf_type_t btype) 2965 { 2966 struct ath_buf *bf; 2967 2968 ATH_TXBUF_LOCK_ASSERT(sc); 2969 2970 if (btype == ATH_BUFTYPE_MGMT) 2971 bf = TAILQ_FIRST(&sc->sc_txbuf_mgmt); 2972 else 2973 bf = TAILQ_FIRST(&sc->sc_txbuf); 2974 2975 if (bf == NULL) { 2976 sc->sc_stats.ast_tx_getnobuf++; 2977 } else { 2978 if (bf->bf_flags & ATH_BUF_BUSY) { 2979 sc->sc_stats.ast_tx_getbusybuf++; 2980 bf = NULL; 2981 } 2982 } 2983 2984 if (bf != NULL && (bf->bf_flags & ATH_BUF_BUSY) == 0) { 2985 if (btype == ATH_BUFTYPE_MGMT) 2986 TAILQ_REMOVE(&sc->sc_txbuf_mgmt, bf, bf_list); 2987 else { 2988 TAILQ_REMOVE(&sc->sc_txbuf, bf, bf_list); 2989 sc->sc_txbuf_cnt--; 2990 2991 /* 2992 * This shuldn't happen; however just to be 2993 * safe print a warning and fudge the txbuf 2994 * count. 2995 */ 2996 if (sc->sc_txbuf_cnt < 0) { 2997 device_printf(sc->sc_dev, 2998 "%s: sc_txbuf_cnt < 0?\n", 2999 __func__); 3000 sc->sc_txbuf_cnt = 0; 3001 } 3002 } 3003 } else 3004 bf = NULL; 3005 3006 if (bf == NULL) { 3007 /* XXX should check which list, mgmt or otherwise */ 3008 DPRINTF(sc, ATH_DEBUG_XMIT, "%s: %s\n", __func__, 3009 TAILQ_FIRST(&sc->sc_txbuf) == NULL ? 3010 "out of xmit buffers" : "xmit buffer busy"); 3011 return NULL; 3012 } 3013 3014 /* XXX TODO: should do this at buffer list initialisation */ 3015 /* XXX (then, ensure the buffer has the right flag set) */ 3016 bf->bf_flags = 0; 3017 if (btype == ATH_BUFTYPE_MGMT) 3018 bf->bf_flags |= ATH_BUF_MGMT; 3019 else 3020 bf->bf_flags &= (~ATH_BUF_MGMT); 3021 3022 /* Valid bf here; clear some basic fields */ 3023 bf->bf_next = NULL; /* XXX just to be sure */ 3024 bf->bf_last = NULL; /* XXX again, just to be sure */ 3025 bf->bf_comp = NULL; /* XXX again, just to be sure */ 3026 bzero(&bf->bf_state, sizeof(bf->bf_state)); 3027 3028 /* 3029 * Track the descriptor ID only if doing EDMA 3030 */ 3031 if (sc->sc_isedma) { 3032 bf->bf_descid = sc->sc_txbuf_descid; 3033 sc->sc_txbuf_descid++; 3034 } 3035 3036 return bf; 3037 } 3038 3039 /* 3040 * When retrying a software frame, buffers marked ATH_BUF_BUSY 3041 * can't be thrown back on the queue as they could still be 3042 * in use by the hardware. 3043 * 3044 * This duplicates the buffer, or returns NULL. 3045 * 3046 * The descriptor is also copied but the link pointers and 3047 * the DMA segments aren't copied; this frame should thus 3048 * be again passed through the descriptor setup/chain routines 3049 * so the link is correct. 3050 * 3051 * The caller must free the buffer using ath_freebuf(). 3052 */ 3053 struct ath_buf * 3054 ath_buf_clone(struct ath_softc *sc, struct ath_buf *bf) 3055 { 3056 struct ath_buf *tbf; 3057 3058 tbf = ath_getbuf(sc, 3059 (bf->bf_flags & ATH_BUF_MGMT) ? 3060 ATH_BUFTYPE_MGMT : ATH_BUFTYPE_NORMAL); 3061 if (tbf == NULL) 3062 return NULL; /* XXX failure? Why? */ 3063 3064 /* Copy basics */ 3065 tbf->bf_next = NULL; 3066 tbf->bf_nseg = bf->bf_nseg; 3067 tbf->bf_flags = bf->bf_flags & ATH_BUF_FLAGS_CLONE; 3068 tbf->bf_status = bf->bf_status; 3069 tbf->bf_m = bf->bf_m; 3070 tbf->bf_node = bf->bf_node; 3071 KASSERT((bf->bf_node != NULL), ("%s: bf_node=NULL!", __func__)); 3072 /* will be setup by the chain/setup function */ 3073 tbf->bf_lastds = NULL; 3074 /* for now, last == self */ 3075 tbf->bf_last = tbf; 3076 tbf->bf_comp = bf->bf_comp; 3077 3078 /* NOTE: DMA segments will be setup by the setup/chain functions */ 3079 3080 /* The caller has to re-init the descriptor + links */ 3081 3082 /* 3083 * Free the DMA mapping here, before we NULL the mbuf. 3084 * We must only call bus_dmamap_unload() once per mbuf chain 3085 * or behaviour is undefined. 3086 */ 3087 if (bf->bf_m != NULL) { 3088 /* 3089 * XXX is this POSTWRITE call required? 3090 */ 3091 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, 3092 BUS_DMASYNC_POSTWRITE); 3093 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 3094 } 3095 3096 bf->bf_m = NULL; 3097 bf->bf_node = NULL; 3098 3099 /* Copy state */ 3100 memcpy(&tbf->bf_state, &bf->bf_state, sizeof(bf->bf_state)); 3101 3102 return tbf; 3103 } 3104 3105 struct ath_buf * 3106 ath_getbuf(struct ath_softc *sc, ath_buf_type_t btype) 3107 { 3108 struct ath_buf *bf; 3109 3110 ATH_TXBUF_LOCK(sc); 3111 bf = _ath_getbuf_locked(sc, btype); 3112 /* 3113 * If a mgmt buffer was requested but we're out of those, 3114 * try requesting a normal one. 3115 */ 3116 if (bf == NULL && btype == ATH_BUFTYPE_MGMT) 3117 bf = _ath_getbuf_locked(sc, ATH_BUFTYPE_NORMAL); 3118 ATH_TXBUF_UNLOCK(sc); 3119 if (bf == NULL) { 3120 DPRINTF(sc, ATH_DEBUG_XMIT, "%s: stop queue\n", __func__); 3121 sc->sc_stats.ast_tx_qstop++; 3122 } 3123 return bf; 3124 } 3125 3126 /* 3127 * Transmit a single frame. 3128 * 3129 * net80211 will free the node reference if the transmit 3130 * fails, so don't free the node reference here. 3131 */ 3132 static int 3133 ath_transmit(struct ieee80211com *ic, struct mbuf *m) 3134 { 3135 struct ath_softc *sc = ic->ic_softc; 3136 struct ieee80211_node *ni; 3137 struct mbuf *next; 3138 struct ath_buf *bf; 3139 ath_bufhead frags; 3140 int retval = 0; 3141 3142 /* 3143 * Tell the reset path that we're currently transmitting. 3144 */ 3145 ATH_PCU_LOCK(sc); 3146 if (sc->sc_inreset_cnt > 0) { 3147 DPRINTF(sc, ATH_DEBUG_XMIT, 3148 "%s: sc_inreset_cnt > 0; bailing\n", __func__); 3149 ATH_PCU_UNLOCK(sc); 3150 sc->sc_stats.ast_tx_qstop++; 3151 ATH_KTR(sc, ATH_KTR_TX, 0, "ath_start_task: OACTIVE, finish"); 3152 return (ENOBUFS); /* XXX should be EINVAL or? */ 3153 } 3154 sc->sc_txstart_cnt++; 3155 ATH_PCU_UNLOCK(sc); 3156 3157 /* Wake the hardware up already */ 3158 ATH_LOCK(sc); 3159 ath_power_set_power_state(sc, HAL_PM_AWAKE); 3160 ATH_UNLOCK(sc); 3161 3162 ATH_KTR(sc, ATH_KTR_TX, 0, "ath_transmit: start"); 3163 /* 3164 * Grab the TX lock - it's ok to do this here; we haven't 3165 * yet started transmitting. 3166 */ 3167 ATH_TX_LOCK(sc); 3168 3169 /* 3170 * Node reference, if there's one. 3171 */ 3172 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif; 3173 3174 /* 3175 * Enforce how deep a node queue can get. 3176 * 3177 * XXX it would be nicer if we kept an mbuf queue per 3178 * node and only whacked them into ath_bufs when we 3179 * are ready to schedule some traffic from them. 3180 * .. that may come later. 3181 * 3182 * XXX we should also track the per-node hardware queue 3183 * depth so it is easy to limit the _SUM_ of the swq and 3184 * hwq frames. Since we only schedule two HWQ frames 3185 * at a time, this should be OK for now. 3186 */ 3187 if ((!(m->m_flags & M_EAPOL)) && 3188 (ATH_NODE(ni)->an_swq_depth > sc->sc_txq_node_maxdepth)) { 3189 sc->sc_stats.ast_tx_nodeq_overflow++; 3190 retval = ENOBUFS; 3191 goto finish; 3192 } 3193 3194 /* 3195 * Check how many TX buffers are available. 3196 * 3197 * If this is for non-EAPOL traffic, just leave some 3198 * space free in order for buffer cloning and raw 3199 * frame transmission to occur. 3200 * 3201 * If it's for EAPOL traffic, ignore this for now. 3202 * Management traffic will be sent via the raw transmit 3203 * method which bypasses this check. 3204 * 3205 * This is needed to ensure that EAPOL frames during 3206 * (re) keying have a chance to go out. 3207 * 3208 * See kern/138379 for more information. 3209 */ 3210 if ((!(m->m_flags & M_EAPOL)) && 3211 (sc->sc_txbuf_cnt <= sc->sc_txq_data_minfree)) { 3212 sc->sc_stats.ast_tx_nobuf++; 3213 retval = ENOBUFS; 3214 goto finish; 3215 } 3216 3217 /* 3218 * Grab a TX buffer and associated resources. 3219 * 3220 * If it's an EAPOL frame, allocate a MGMT ath_buf. 3221 * That way even with temporary buffer exhaustion due to 3222 * the data path doesn't leave us without the ability 3223 * to transmit management frames. 3224 * 3225 * Otherwise allocate a normal buffer. 3226 */ 3227 if (m->m_flags & M_EAPOL) 3228 bf = ath_getbuf(sc, ATH_BUFTYPE_MGMT); 3229 else 3230 bf = ath_getbuf(sc, ATH_BUFTYPE_NORMAL); 3231 3232 if (bf == NULL) { 3233 /* 3234 * If we failed to allocate a buffer, fail. 3235 * 3236 * We shouldn't fail normally, due to the check 3237 * above. 3238 */ 3239 sc->sc_stats.ast_tx_nobuf++; 3240 retval = ENOBUFS; 3241 goto finish; 3242 } 3243 3244 /* 3245 * At this point we have a buffer; so we need to free it 3246 * if we hit any error conditions. 3247 */ 3248 3249 /* 3250 * Check for fragmentation. If this frame 3251 * has been broken up verify we have enough 3252 * buffers to send all the fragments so all 3253 * go out or none... 3254 */ 3255 TAILQ_INIT(&frags); 3256 if ((m->m_flags & M_FRAG) && 3257 !ath_txfrag_setup(sc, &frags, m, ni)) { 3258 DPRINTF(sc, ATH_DEBUG_XMIT, 3259 "%s: out of txfrag buffers\n", __func__); 3260 sc->sc_stats.ast_tx_nofrag++; 3261 if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, 1); 3262 /* 3263 * XXXGL: is mbuf valid after ath_txfrag_setup? If yes, 3264 * we shouldn't free it but return back. 3265 */ 3266 ieee80211_free_mbuf(m); 3267 m = NULL; 3268 goto bad; 3269 } 3270 3271 /* 3272 * At this point if we have any TX fragments, then we will 3273 * have bumped the node reference once for each of those. 3274 */ 3275 3276 /* 3277 * XXX Is there anything actually _enforcing_ that the 3278 * fragments are being transmitted in one hit, rather than 3279 * being interleaved with other transmissions on that 3280 * hardware queue? 3281 * 3282 * The ATH TX output lock is the only thing serialising this 3283 * right now. 3284 */ 3285 3286 /* 3287 * Calculate the "next fragment" length field in ath_buf 3288 * in order to let the transmit path know enough about 3289 * what to next write to the hardware. 3290 */ 3291 if (m->m_flags & M_FRAG) { 3292 struct ath_buf *fbf = bf; 3293 struct ath_buf *n_fbf = NULL; 3294 struct mbuf *fm = m->m_nextpkt; 3295 3296 /* 3297 * We need to walk the list of fragments and set 3298 * the next size to the following buffer. 3299 * However, the first buffer isn't in the frag 3300 * list, so we have to do some gymnastics here. 3301 */ 3302 TAILQ_FOREACH(n_fbf, &frags, bf_list) { 3303 fbf->bf_nextfraglen = fm->m_pkthdr.len; 3304 fbf = n_fbf; 3305 fm = fm->m_nextpkt; 3306 } 3307 } 3308 3309 nextfrag: 3310 /* 3311 * Pass the frame to the h/w for transmission. 3312 * Fragmented frames have each frag chained together 3313 * with m_nextpkt. We know there are sufficient ath_buf's 3314 * to send all the frags because of work done by 3315 * ath_txfrag_setup. We leave m_nextpkt set while 3316 * calling ath_tx_start so it can use it to extend the 3317 * the tx duration to cover the subsequent frag and 3318 * so it can reclaim all the mbufs in case of an error; 3319 * ath_tx_start clears m_nextpkt once it commits to 3320 * handing the frame to the hardware. 3321 * 3322 * Note: if this fails, then the mbufs are freed but 3323 * not the node reference. 3324 * 3325 * So, we now have to free the node reference ourselves here 3326 * and return OK up to the stack. 3327 */ 3328 next = m->m_nextpkt; 3329 if (ath_tx_start(sc, ni, bf, m)) { 3330 bad: 3331 if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, 1); 3332 reclaim: 3333 bf->bf_m = NULL; 3334 bf->bf_node = NULL; 3335 ATH_TXBUF_LOCK(sc); 3336 ath_returnbuf_head(sc, bf); 3337 /* 3338 * Free the rest of the node references and 3339 * buffers for the fragment list. 3340 */ 3341 ath_txfrag_cleanup(sc, &frags, ni); 3342 ATH_TXBUF_UNLOCK(sc); 3343 3344 /* 3345 * XXX: And free the node/return OK; ath_tx_start() may have 3346 * modified the buffer. We currently have no way to 3347 * signify that the mbuf was freed but there was an error. 3348 */ 3349 ieee80211_free_node(ni); 3350 retval = 0; 3351 goto finish; 3352 } 3353 3354 /* 3355 * Check here if the node is in power save state. 3356 */ 3357 ath_tx_update_tim(sc, ni, 1); 3358 3359 if (next != NULL) { 3360 /* 3361 * Beware of state changing between frags. 3362 * XXX check sta power-save state? 3363 */ 3364 if (ni->ni_vap->iv_state != IEEE80211_S_RUN) { 3365 DPRINTF(sc, ATH_DEBUG_XMIT, 3366 "%s: flush fragmented packet, state %s\n", 3367 __func__, 3368 ieee80211_state_name[ni->ni_vap->iv_state]); 3369 /* XXX dmamap */ 3370 ieee80211_free_mbuf(next); 3371 goto reclaim; 3372 } 3373 m = next; 3374 bf = TAILQ_FIRST(&frags); 3375 KASSERT(bf != NULL, ("no buf for txfrag")); 3376 TAILQ_REMOVE(&frags, bf, bf_list); 3377 goto nextfrag; 3378 } 3379 3380 /* 3381 * Bump watchdog timer. 3382 */ 3383 sc->sc_wd_timer = 5; 3384 3385 finish: 3386 ATH_TX_UNLOCK(sc); 3387 3388 /* 3389 * Finished transmitting! 3390 */ 3391 ATH_PCU_LOCK(sc); 3392 sc->sc_txstart_cnt--; 3393 ATH_PCU_UNLOCK(sc); 3394 3395 /* Sleep the hardware if required */ 3396 ATH_LOCK(sc); 3397 ath_power_restore_power_state(sc); 3398 ATH_UNLOCK(sc); 3399 3400 ATH_KTR(sc, ATH_KTR_TX, 0, "ath_transmit: finished"); 3401 3402 return (retval); 3403 } 3404 3405 static int 3406 ath_media_change(struct ifnet *ifp) 3407 { 3408 int error = ieee80211_media_change(ifp); 3409 /* NB: only the fixed rate can change and that doesn't need a reset */ 3410 return (error == ENETRESET ? 0 : error); 3411 } 3412 3413 /* 3414 * Block/unblock tx+rx processing while a key change is done. 3415 * We assume the caller serializes key management operations 3416 * so we only need to worry about synchronization with other 3417 * uses that originate in the driver. 3418 */ 3419 static void 3420 ath_key_update_begin(struct ieee80211vap *vap) 3421 { 3422 struct ath_softc *sc = vap->iv_ic->ic_softc; 3423 3424 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__); 3425 taskqueue_block(sc->sc_tq); 3426 } 3427 3428 static void 3429 ath_key_update_end(struct ieee80211vap *vap) 3430 { 3431 struct ath_softc *sc = vap->iv_ic->ic_softc; 3432 3433 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__); 3434 taskqueue_unblock(sc->sc_tq); 3435 } 3436 3437 static void 3438 ath_update_promisc(struct ieee80211com *ic) 3439 { 3440 struct ath_softc *sc = ic->ic_softc; 3441 u_int32_t rfilt; 3442 3443 /* configure rx filter */ 3444 ATH_LOCK(sc); 3445 ath_power_set_power_state(sc, HAL_PM_AWAKE); 3446 rfilt = ath_calcrxfilter(sc); 3447 ath_hal_setrxfilter(sc->sc_ah, rfilt); 3448 ath_power_restore_power_state(sc); 3449 ATH_UNLOCK(sc); 3450 3451 DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x\n", __func__, rfilt); 3452 } 3453 3454 /* 3455 * Driver-internal mcast update call. 3456 * 3457 * Assumes the hardware is already awake. 3458 */ 3459 static void 3460 ath_update_mcast_hw(struct ath_softc *sc) 3461 { 3462 struct ieee80211com *ic = &sc->sc_ic; 3463 u_int32_t mfilt[2]; 3464 3465 /* calculate and install multicast filter */ 3466 if (ic->ic_allmulti == 0) { 3467 struct ieee80211vap *vap; 3468 struct ifnet *ifp; 3469 struct ifmultiaddr *ifma; 3470 3471 /* 3472 * Merge multicast addresses to form the hardware filter. 3473 */ 3474 mfilt[0] = mfilt[1] = 0; 3475 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 3476 ifp = vap->iv_ifp; 3477 if_maddr_rlock(ifp); 3478 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 3479 caddr_t dl; 3480 uint32_t val; 3481 uint8_t pos; 3482 3483 /* calculate XOR of eight 6bit values */ 3484 dl = LLADDR((struct sockaddr_dl *) 3485 ifma->ifma_addr); 3486 val = LE_READ_4(dl + 0); 3487 pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ 3488 val; 3489 val = LE_READ_4(dl + 3); 3490 pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ 3491 val; 3492 pos &= 0x3f; 3493 mfilt[pos / 32] |= (1 << (pos % 32)); 3494 } 3495 if_maddr_runlock(ifp); 3496 } 3497 } else 3498 mfilt[0] = mfilt[1] = ~0; 3499 3500 ath_hal_setmcastfilter(sc->sc_ah, mfilt[0], mfilt[1]); 3501 3502 DPRINTF(sc, ATH_DEBUG_MODE, "%s: MC filter %08x:%08x\n", 3503 __func__, mfilt[0], mfilt[1]); 3504 } 3505 3506 /* 3507 * Called from the net80211 layer - force the hardware 3508 * awake before operating. 3509 */ 3510 static void 3511 ath_update_mcast(struct ieee80211com *ic) 3512 { 3513 struct ath_softc *sc = ic->ic_softc; 3514 3515 ATH_LOCK(sc); 3516 ath_power_set_power_state(sc, HAL_PM_AWAKE); 3517 ATH_UNLOCK(sc); 3518 3519 ath_update_mcast_hw(sc); 3520 3521 ATH_LOCK(sc); 3522 ath_power_restore_power_state(sc); 3523 ATH_UNLOCK(sc); 3524 } 3525 3526 void 3527 ath_mode_init(struct ath_softc *sc) 3528 { 3529 struct ieee80211com *ic = &sc->sc_ic; 3530 struct ath_hal *ah = sc->sc_ah; 3531 u_int32_t rfilt; 3532 3533 /* configure rx filter */ 3534 rfilt = ath_calcrxfilter(sc); 3535 ath_hal_setrxfilter(ah, rfilt); 3536 3537 /* configure operational mode */ 3538 ath_hal_setopmode(ah); 3539 3540 /* handle any link-level address change */ 3541 ath_hal_setmac(ah, ic->ic_macaddr); 3542 3543 /* calculate and install multicast filter */ 3544 ath_update_mcast_hw(sc); 3545 } 3546 3547 /* 3548 * Set the slot time based on the current setting. 3549 */ 3550 void 3551 ath_setslottime(struct ath_softc *sc) 3552 { 3553 struct ieee80211com *ic = &sc->sc_ic; 3554 struct ath_hal *ah = sc->sc_ah; 3555 u_int usec; 3556 3557 if (IEEE80211_IS_CHAN_HALF(ic->ic_curchan)) 3558 usec = 13; 3559 else if (IEEE80211_IS_CHAN_QUARTER(ic->ic_curchan)) 3560 usec = 21; 3561 else if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan)) { 3562 /* honor short/long slot time only in 11g */ 3563 /* XXX shouldn't honor on pure g or turbo g channel */ 3564 if (ic->ic_flags & IEEE80211_F_SHSLOT) 3565 usec = HAL_SLOT_TIME_9; 3566 else 3567 usec = HAL_SLOT_TIME_20; 3568 } else 3569 usec = HAL_SLOT_TIME_9; 3570 3571 DPRINTF(sc, ATH_DEBUG_RESET, 3572 "%s: chan %u MHz flags 0x%x %s slot, %u usec\n", 3573 __func__, ic->ic_curchan->ic_freq, ic->ic_curchan->ic_flags, 3574 ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", usec); 3575 3576 /* Wake up the hardware first before updating the slot time */ 3577 ATH_LOCK(sc); 3578 ath_power_set_power_state(sc, HAL_PM_AWAKE); 3579 ath_hal_setslottime(ah, usec); 3580 ath_power_restore_power_state(sc); 3581 sc->sc_updateslot = OK; 3582 ATH_UNLOCK(sc); 3583 } 3584 3585 /* 3586 * Callback from the 802.11 layer to update the 3587 * slot time based on the current setting. 3588 */ 3589 static void 3590 ath_updateslot(struct ieee80211com *ic) 3591 { 3592 struct ath_softc *sc = ic->ic_softc; 3593 3594 /* 3595 * When not coordinating the BSS, change the hardware 3596 * immediately. For other operation we defer the change 3597 * until beacon updates have propagated to the stations. 3598 * 3599 * XXX sc_updateslot isn't changed behind a lock? 3600 */ 3601 if (ic->ic_opmode == IEEE80211_M_HOSTAP || 3602 ic->ic_opmode == IEEE80211_M_MBSS) 3603 sc->sc_updateslot = UPDATE; 3604 else 3605 ath_setslottime(sc); 3606 } 3607 3608 /* 3609 * Append the contents of src to dst; both queues 3610 * are assumed to be locked. 3611 */ 3612 void 3613 ath_txqmove(struct ath_txq *dst, struct ath_txq *src) 3614 { 3615 3616 ATH_TXQ_LOCK_ASSERT(src); 3617 ATH_TXQ_LOCK_ASSERT(dst); 3618 3619 TAILQ_CONCAT(&dst->axq_q, &src->axq_q, bf_list); 3620 dst->axq_link = src->axq_link; 3621 src->axq_link = NULL; 3622 dst->axq_depth += src->axq_depth; 3623 dst->axq_aggr_depth += src->axq_aggr_depth; 3624 src->axq_depth = 0; 3625 src->axq_aggr_depth = 0; 3626 } 3627 3628 /* 3629 * Reset the hardware, with no loss. 3630 * 3631 * This can't be used for a general case reset. 3632 */ 3633 static void 3634 ath_reset_proc(void *arg, int pending) 3635 { 3636 struct ath_softc *sc = arg; 3637 3638 #if 0 3639 device_printf(sc->sc_dev, "%s: resetting\n", __func__); 3640 #endif 3641 ath_reset(sc, ATH_RESET_NOLOSS); 3642 } 3643 3644 /* 3645 * Reset the hardware after detecting beacons have stopped. 3646 */ 3647 static void 3648 ath_bstuck_proc(void *arg, int pending) 3649 { 3650 struct ath_softc *sc = arg; 3651 uint32_t hangs = 0; 3652 3653 if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0) 3654 device_printf(sc->sc_dev, "bb hang detected (0x%x)\n", hangs); 3655 3656 #ifdef ATH_DEBUG_ALQ 3657 if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_STUCK_BEACON)) 3658 if_ath_alq_post(&sc->sc_alq, ATH_ALQ_STUCK_BEACON, 0, NULL); 3659 #endif 3660 3661 device_printf(sc->sc_dev, "stuck beacon; resetting (bmiss count %u)\n", 3662 sc->sc_bmisscount); 3663 sc->sc_stats.ast_bstuck++; 3664 /* 3665 * This assumes that there's no simultaneous channel mode change 3666 * occuring. 3667 */ 3668 ath_reset(sc, ATH_RESET_NOLOSS); 3669 } 3670 3671 static void 3672 ath_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 3673 { 3674 bus_addr_t *paddr = (bus_addr_t*) arg; 3675 KASSERT(error == 0, ("error %u on bus_dma callback", error)); 3676 *paddr = segs->ds_addr; 3677 } 3678 3679 /* 3680 * Allocate the descriptors and appropriate DMA tag/setup. 3681 * 3682 * For some situations (eg EDMA TX completion), there isn't a requirement 3683 * for the ath_buf entries to be allocated. 3684 */ 3685 int 3686 ath_descdma_alloc_desc(struct ath_softc *sc, 3687 struct ath_descdma *dd, ath_bufhead *head, 3688 const char *name, int ds_size, int ndesc) 3689 { 3690 #define DS2PHYS(_dd, _ds) \ 3691 ((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc)) 3692 #define ATH_DESC_4KB_BOUND_CHECK(_daddr, _len) \ 3693 ((((u_int32_t)(_daddr) & 0xFFF) > (0x1000 - (_len))) ? 1 : 0) 3694 int error; 3695 3696 dd->dd_descsize = ds_size; 3697 3698 DPRINTF(sc, ATH_DEBUG_RESET, 3699 "%s: %s DMA: %u desc, %d bytes per descriptor\n", 3700 __func__, name, ndesc, dd->dd_descsize); 3701 3702 dd->dd_name = name; 3703 dd->dd_desc_len = dd->dd_descsize * ndesc; 3704 3705 /* 3706 * Merlin work-around: 3707 * Descriptors that cross the 4KB boundary can't be used. 3708 * Assume one skipped descriptor per 4KB page. 3709 */ 3710 if (! ath_hal_split4ktrans(sc->sc_ah)) { 3711 int numpages = dd->dd_desc_len / 4096; 3712 dd->dd_desc_len += ds_size * numpages; 3713 } 3714 3715 /* 3716 * Setup DMA descriptor area. 3717 * 3718 * BUS_DMA_ALLOCNOW is not used; we never use bounce 3719 * buffers for the descriptors themselves. 3720 */ 3721 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), /* parent */ 3722 PAGE_SIZE, 0, /* alignment, bounds */ 3723 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 3724 BUS_SPACE_MAXADDR, /* highaddr */ 3725 NULL, NULL, /* filter, filterarg */ 3726 dd->dd_desc_len, /* maxsize */ 3727 1, /* nsegments */ 3728 dd->dd_desc_len, /* maxsegsize */ 3729 0, /* flags */ 3730 NULL, /* lockfunc */ 3731 NULL, /* lockarg */ 3732 &dd->dd_dmat); 3733 if (error != 0) { 3734 device_printf(sc->sc_dev, 3735 "cannot allocate %s DMA tag\n", dd->dd_name); 3736 return error; 3737 } 3738 3739 /* allocate descriptors */ 3740 error = bus_dmamem_alloc(dd->dd_dmat, (void**) &dd->dd_desc, 3741 BUS_DMA_NOWAIT | BUS_DMA_COHERENT, 3742 &dd->dd_dmamap); 3743 if (error != 0) { 3744 device_printf(sc->sc_dev, 3745 "unable to alloc memory for %u %s descriptors, error %u\n", 3746 ndesc, dd->dd_name, error); 3747 goto fail1; 3748 } 3749 3750 error = bus_dmamap_load(dd->dd_dmat, dd->dd_dmamap, 3751 dd->dd_desc, dd->dd_desc_len, 3752 ath_load_cb, &dd->dd_desc_paddr, 3753 BUS_DMA_NOWAIT); 3754 if (error != 0) { 3755 device_printf(sc->sc_dev, 3756 "unable to map %s descriptors, error %u\n", 3757 dd->dd_name, error); 3758 goto fail2; 3759 } 3760 3761 DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA map: %p (%lu) -> %p (%lu)\n", 3762 __func__, dd->dd_name, (uint8_t *) dd->dd_desc, 3763 (u_long) dd->dd_desc_len, (caddr_t) dd->dd_desc_paddr, 3764 /*XXX*/ (u_long) dd->dd_desc_len); 3765 3766 return (0); 3767 3768 fail2: 3769 bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap); 3770 fail1: 3771 bus_dma_tag_destroy(dd->dd_dmat); 3772 memset(dd, 0, sizeof(*dd)); 3773 return error; 3774 #undef DS2PHYS 3775 #undef ATH_DESC_4KB_BOUND_CHECK 3776 } 3777 3778 int 3779 ath_descdma_setup(struct ath_softc *sc, 3780 struct ath_descdma *dd, ath_bufhead *head, 3781 const char *name, int ds_size, int nbuf, int ndesc) 3782 { 3783 #define DS2PHYS(_dd, _ds) \ 3784 ((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc)) 3785 #define ATH_DESC_4KB_BOUND_CHECK(_daddr, _len) \ 3786 ((((u_int32_t)(_daddr) & 0xFFF) > (0x1000 - (_len))) ? 1 : 0) 3787 uint8_t *ds; 3788 struct ath_buf *bf; 3789 int i, bsize, error; 3790 3791 /* Allocate descriptors */ 3792 error = ath_descdma_alloc_desc(sc, dd, head, name, ds_size, 3793 nbuf * ndesc); 3794 3795 /* Assume any errors during allocation were dealt with */ 3796 if (error != 0) { 3797 return (error); 3798 } 3799 3800 ds = (uint8_t *) dd->dd_desc; 3801 3802 /* allocate rx buffers */ 3803 bsize = sizeof(struct ath_buf) * nbuf; 3804 bf = malloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO); 3805 if (bf == NULL) { 3806 device_printf(sc->sc_dev, 3807 "malloc of %s buffers failed, size %u\n", 3808 dd->dd_name, bsize); 3809 goto fail3; 3810 } 3811 dd->dd_bufptr = bf; 3812 3813 TAILQ_INIT(head); 3814 for (i = 0; i < nbuf; i++, bf++, ds += (ndesc * dd->dd_descsize)) { 3815 bf->bf_desc = (struct ath_desc *) ds; 3816 bf->bf_daddr = DS2PHYS(dd, ds); 3817 if (! ath_hal_split4ktrans(sc->sc_ah)) { 3818 /* 3819 * Merlin WAR: Skip descriptor addresses which 3820 * cause 4KB boundary crossing along any point 3821 * in the descriptor. 3822 */ 3823 if (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr, 3824 dd->dd_descsize)) { 3825 /* Start at the next page */ 3826 ds += 0x1000 - (bf->bf_daddr & 0xFFF); 3827 bf->bf_desc = (struct ath_desc *) ds; 3828 bf->bf_daddr = DS2PHYS(dd, ds); 3829 } 3830 } 3831 error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT, 3832 &bf->bf_dmamap); 3833 if (error != 0) { 3834 device_printf(sc->sc_dev, "unable to create dmamap " 3835 "for %s buffer %u, error %u\n", 3836 dd->dd_name, i, error); 3837 ath_descdma_cleanup(sc, dd, head); 3838 return error; 3839 } 3840 bf->bf_lastds = bf->bf_desc; /* Just an initial value */ 3841 TAILQ_INSERT_TAIL(head, bf, bf_list); 3842 } 3843 3844 /* 3845 * XXX TODO: ensure that ds doesn't overflow the descriptor 3846 * allocation otherwise weird stuff will occur and crash your 3847 * machine. 3848 */ 3849 return 0; 3850 /* XXX this should likely just call ath_descdma_cleanup() */ 3851 fail3: 3852 bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap); 3853 bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap); 3854 bus_dma_tag_destroy(dd->dd_dmat); 3855 memset(dd, 0, sizeof(*dd)); 3856 return error; 3857 #undef DS2PHYS 3858 #undef ATH_DESC_4KB_BOUND_CHECK 3859 } 3860 3861 /* 3862 * Allocate ath_buf entries but no descriptor contents. 3863 * 3864 * This is for RX EDMA where the descriptors are the header part of 3865 * the RX buffer. 3866 */ 3867 int 3868 ath_descdma_setup_rx_edma(struct ath_softc *sc, 3869 struct ath_descdma *dd, ath_bufhead *head, 3870 const char *name, int nbuf, int rx_status_len) 3871 { 3872 struct ath_buf *bf; 3873 int i, bsize, error; 3874 3875 DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA: %u buffers\n", 3876 __func__, name, nbuf); 3877 3878 dd->dd_name = name; 3879 /* 3880 * This is (mostly) purely for show. We're not allocating any actual 3881 * descriptors here as EDMA RX has the descriptor be part 3882 * of the RX buffer. 3883 * 3884 * However, dd_desc_len is used by ath_descdma_free() to determine 3885 * whether we have already freed this DMA mapping. 3886 */ 3887 dd->dd_desc_len = rx_status_len * nbuf; 3888 dd->dd_descsize = rx_status_len; 3889 3890 /* allocate rx buffers */ 3891 bsize = sizeof(struct ath_buf) * nbuf; 3892 bf = malloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO); 3893 if (bf == NULL) { 3894 device_printf(sc->sc_dev, 3895 "malloc of %s buffers failed, size %u\n", 3896 dd->dd_name, bsize); 3897 error = ENOMEM; 3898 goto fail3; 3899 } 3900 dd->dd_bufptr = bf; 3901 3902 TAILQ_INIT(head); 3903 for (i = 0; i < nbuf; i++, bf++) { 3904 bf->bf_desc = NULL; 3905 bf->bf_daddr = 0; 3906 bf->bf_lastds = NULL; /* Just an initial value */ 3907 3908 error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT, 3909 &bf->bf_dmamap); 3910 if (error != 0) { 3911 device_printf(sc->sc_dev, "unable to create dmamap " 3912 "for %s buffer %u, error %u\n", 3913 dd->dd_name, i, error); 3914 ath_descdma_cleanup(sc, dd, head); 3915 return error; 3916 } 3917 TAILQ_INSERT_TAIL(head, bf, bf_list); 3918 } 3919 return 0; 3920 fail3: 3921 memset(dd, 0, sizeof(*dd)); 3922 return error; 3923 } 3924 3925 void 3926 ath_descdma_cleanup(struct ath_softc *sc, 3927 struct ath_descdma *dd, ath_bufhead *head) 3928 { 3929 struct ath_buf *bf; 3930 struct ieee80211_node *ni; 3931 int do_warning = 0; 3932 3933 if (dd->dd_dmamap != 0) { 3934 bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap); 3935 bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap); 3936 bus_dma_tag_destroy(dd->dd_dmat); 3937 } 3938 3939 if (head != NULL) { 3940 TAILQ_FOREACH(bf, head, bf_list) { 3941 if (bf->bf_m) { 3942 /* 3943 * XXX warn if there's buffers here. 3944 * XXX it should have been freed by the 3945 * owner! 3946 */ 3947 3948 if (do_warning == 0) { 3949 do_warning = 1; 3950 device_printf(sc->sc_dev, 3951 "%s: %s: mbuf should've been" 3952 " unmapped/freed!\n", 3953 __func__, 3954 dd->dd_name); 3955 } 3956 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, 3957 BUS_DMASYNC_POSTREAD); 3958 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 3959 m_freem(bf->bf_m); 3960 bf->bf_m = NULL; 3961 } 3962 if (bf->bf_dmamap != NULL) { 3963 bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap); 3964 bf->bf_dmamap = NULL; 3965 } 3966 ni = bf->bf_node; 3967 bf->bf_node = NULL; 3968 if (ni != NULL) { 3969 /* 3970 * Reclaim node reference. 3971 */ 3972 ieee80211_free_node(ni); 3973 } 3974 } 3975 } 3976 3977 if (head != NULL) 3978 TAILQ_INIT(head); 3979 3980 if (dd->dd_bufptr != NULL) 3981 free(dd->dd_bufptr, M_ATHDEV); 3982 memset(dd, 0, sizeof(*dd)); 3983 } 3984 3985 static int 3986 ath_desc_alloc(struct ath_softc *sc) 3987 { 3988 int error; 3989 3990 error = ath_descdma_setup(sc, &sc->sc_txdma, &sc->sc_txbuf, 3991 "tx", sc->sc_tx_desclen, ath_txbuf, ATH_MAX_SCATTER); 3992 if (error != 0) { 3993 return error; 3994 } 3995 sc->sc_txbuf_cnt = ath_txbuf; 3996 3997 error = ath_descdma_setup(sc, &sc->sc_txdma_mgmt, &sc->sc_txbuf_mgmt, 3998 "tx_mgmt", sc->sc_tx_desclen, ath_txbuf_mgmt, 3999 ATH_TXDESC); 4000 if (error != 0) { 4001 ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf); 4002 return error; 4003 } 4004 4005 /* 4006 * XXX mark txbuf_mgmt frames with ATH_BUF_MGMT, so the 4007 * flag doesn't have to be set in ath_getbuf_locked(). 4008 */ 4009 4010 error = ath_descdma_setup(sc, &sc->sc_bdma, &sc->sc_bbuf, 4011 "beacon", sc->sc_tx_desclen, ATH_BCBUF, 1); 4012 if (error != 0) { 4013 ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf); 4014 ath_descdma_cleanup(sc, &sc->sc_txdma_mgmt, 4015 &sc->sc_txbuf_mgmt); 4016 return error; 4017 } 4018 return 0; 4019 } 4020 4021 static void 4022 ath_desc_free(struct ath_softc *sc) 4023 { 4024 4025 if (sc->sc_bdma.dd_desc_len != 0) 4026 ath_descdma_cleanup(sc, &sc->sc_bdma, &sc->sc_bbuf); 4027 if (sc->sc_txdma.dd_desc_len != 0) 4028 ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf); 4029 if (sc->sc_txdma_mgmt.dd_desc_len != 0) 4030 ath_descdma_cleanup(sc, &sc->sc_txdma_mgmt, 4031 &sc->sc_txbuf_mgmt); 4032 } 4033 4034 static struct ieee80211_node * 4035 ath_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN]) 4036 { 4037 struct ieee80211com *ic = vap->iv_ic; 4038 struct ath_softc *sc = ic->ic_softc; 4039 const size_t space = sizeof(struct ath_node) + sc->sc_rc->arc_space; 4040 struct ath_node *an; 4041 4042 an = malloc(space, M_80211_NODE, M_NOWAIT|M_ZERO); 4043 if (an == NULL) { 4044 /* XXX stat+msg */ 4045 return NULL; 4046 } 4047 ath_rate_node_init(sc, an); 4048 4049 /* Setup the mutex - there's no associd yet so set the name to NULL */ 4050 snprintf(an->an_name, sizeof(an->an_name), "%s: node %p", 4051 device_get_nameunit(sc->sc_dev), an); 4052 mtx_init(&an->an_mtx, an->an_name, NULL, MTX_DEF); 4053 4054 /* XXX setup ath_tid */ 4055 ath_tx_tid_init(sc, an); 4056 4057 DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__, mac, ":", an); 4058 return &an->an_node; 4059 } 4060 4061 static void 4062 ath_node_cleanup(struct ieee80211_node *ni) 4063 { 4064 struct ieee80211com *ic = ni->ni_ic; 4065 struct ath_softc *sc = ic->ic_softc; 4066 4067 DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__, 4068 ni->ni_macaddr, ":", ATH_NODE(ni)); 4069 4070 /* Cleanup ath_tid, free unused bufs, unlink bufs in TXQ */ 4071 ath_tx_node_flush(sc, ATH_NODE(ni)); 4072 ath_rate_node_cleanup(sc, ATH_NODE(ni)); 4073 sc->sc_node_cleanup(ni); 4074 } 4075 4076 static void 4077 ath_node_free(struct ieee80211_node *ni) 4078 { 4079 struct ieee80211com *ic = ni->ni_ic; 4080 struct ath_softc *sc = ic->ic_softc; 4081 4082 DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__, 4083 ni->ni_macaddr, ":", ATH_NODE(ni)); 4084 mtx_destroy(&ATH_NODE(ni)->an_mtx); 4085 sc->sc_node_free(ni); 4086 } 4087 4088 static void 4089 ath_node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise) 4090 { 4091 struct ieee80211com *ic = ni->ni_ic; 4092 struct ath_softc *sc = ic->ic_softc; 4093 struct ath_hal *ah = sc->sc_ah; 4094 4095 *rssi = ic->ic_node_getrssi(ni); 4096 if (ni->ni_chan != IEEE80211_CHAN_ANYC) 4097 *noise = ath_hal_getchannoise(ah, ni->ni_chan); 4098 else 4099 *noise = -95; /* nominally correct */ 4100 } 4101 4102 /* 4103 * Set the default antenna. 4104 */ 4105 void 4106 ath_setdefantenna(struct ath_softc *sc, u_int antenna) 4107 { 4108 struct ath_hal *ah = sc->sc_ah; 4109 4110 /* XXX block beacon interrupts */ 4111 ath_hal_setdefantenna(ah, antenna); 4112 if (sc->sc_defant != antenna) 4113 sc->sc_stats.ast_ant_defswitch++; 4114 sc->sc_defant = antenna; 4115 sc->sc_rxotherant = 0; 4116 } 4117 4118 static void 4119 ath_txq_init(struct ath_softc *sc, struct ath_txq *txq, int qnum) 4120 { 4121 txq->axq_qnum = qnum; 4122 txq->axq_ac = 0; 4123 txq->axq_depth = 0; 4124 txq->axq_aggr_depth = 0; 4125 txq->axq_intrcnt = 0; 4126 txq->axq_link = NULL; 4127 txq->axq_softc = sc; 4128 TAILQ_INIT(&txq->axq_q); 4129 TAILQ_INIT(&txq->axq_tidq); 4130 TAILQ_INIT(&txq->fifo.axq_q); 4131 ATH_TXQ_LOCK_INIT(sc, txq); 4132 } 4133 4134 /* 4135 * Setup a h/w transmit queue. 4136 */ 4137 static struct ath_txq * 4138 ath_txq_setup(struct ath_softc *sc, int qtype, int subtype) 4139 { 4140 struct ath_hal *ah = sc->sc_ah; 4141 HAL_TXQ_INFO qi; 4142 int qnum; 4143 4144 memset(&qi, 0, sizeof(qi)); 4145 qi.tqi_subtype = subtype; 4146 qi.tqi_aifs = HAL_TXQ_USEDEFAULT; 4147 qi.tqi_cwmin = HAL_TXQ_USEDEFAULT; 4148 qi.tqi_cwmax = HAL_TXQ_USEDEFAULT; 4149 /* 4150 * Enable interrupts only for EOL and DESC conditions. 4151 * We mark tx descriptors to receive a DESC interrupt 4152 * when a tx queue gets deep; otherwise waiting for the 4153 * EOL to reap descriptors. Note that this is done to 4154 * reduce interrupt load and this only defers reaping 4155 * descriptors, never transmitting frames. Aside from 4156 * reducing interrupts this also permits more concurrency. 4157 * The only potential downside is if the tx queue backs 4158 * up in which case the top half of the kernel may backup 4159 * due to a lack of tx descriptors. 4160 */ 4161 if (sc->sc_isedma) 4162 qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE | 4163 HAL_TXQ_TXOKINT_ENABLE; 4164 else 4165 qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE | 4166 HAL_TXQ_TXDESCINT_ENABLE; 4167 4168 qnum = ath_hal_setuptxqueue(ah, qtype, &qi); 4169 if (qnum == -1) { 4170 /* 4171 * NB: don't print a message, this happens 4172 * normally on parts with too few tx queues 4173 */ 4174 return NULL; 4175 } 4176 if (qnum >= nitems(sc->sc_txq)) { 4177 device_printf(sc->sc_dev, 4178 "hal qnum %u out of range, max %zu!\n", 4179 qnum, nitems(sc->sc_txq)); 4180 ath_hal_releasetxqueue(ah, qnum); 4181 return NULL; 4182 } 4183 if (!ATH_TXQ_SETUP(sc, qnum)) { 4184 ath_txq_init(sc, &sc->sc_txq[qnum], qnum); 4185 sc->sc_txqsetup |= 1<<qnum; 4186 } 4187 return &sc->sc_txq[qnum]; 4188 } 4189 4190 /* 4191 * Setup a hardware data transmit queue for the specified 4192 * access control. The hal may not support all requested 4193 * queues in which case it will return a reference to a 4194 * previously setup queue. We record the mapping from ac's 4195 * to h/w queues for use by ath_tx_start and also track 4196 * the set of h/w queues being used to optimize work in the 4197 * transmit interrupt handler and related routines. 4198 */ 4199 static int 4200 ath_tx_setup(struct ath_softc *sc, int ac, int haltype) 4201 { 4202 struct ath_txq *txq; 4203 4204 if (ac >= nitems(sc->sc_ac2q)) { 4205 device_printf(sc->sc_dev, "AC %u out of range, max %zu!\n", 4206 ac, nitems(sc->sc_ac2q)); 4207 return 0; 4208 } 4209 txq = ath_txq_setup(sc, HAL_TX_QUEUE_DATA, haltype); 4210 if (txq != NULL) { 4211 txq->axq_ac = ac; 4212 sc->sc_ac2q[ac] = txq; 4213 return 1; 4214 } else 4215 return 0; 4216 } 4217 4218 /* 4219 * Update WME parameters for a transmit queue. 4220 */ 4221 static int 4222 ath_txq_update(struct ath_softc *sc, int ac) 4223 { 4224 #define ATH_EXPONENT_TO_VALUE(v) ((1<<v)-1) 4225 struct ieee80211com *ic = &sc->sc_ic; 4226 struct ath_txq *txq = sc->sc_ac2q[ac]; 4227 struct wmeParams *wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac]; 4228 struct ath_hal *ah = sc->sc_ah; 4229 HAL_TXQ_INFO qi; 4230 4231 ath_hal_gettxqueueprops(ah, txq->axq_qnum, &qi); 4232 #ifdef IEEE80211_SUPPORT_TDMA 4233 if (sc->sc_tdma) { 4234 /* 4235 * AIFS is zero so there's no pre-transmit wait. The 4236 * burst time defines the slot duration and is configured 4237 * through net80211. The QCU is setup to not do post-xmit 4238 * back off, lockout all lower-priority QCU's, and fire 4239 * off the DMA beacon alert timer which is setup based 4240 * on the slot configuration. 4241 */ 4242 qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE 4243 | HAL_TXQ_TXERRINT_ENABLE 4244 | HAL_TXQ_TXURNINT_ENABLE 4245 | HAL_TXQ_TXEOLINT_ENABLE 4246 | HAL_TXQ_DBA_GATED 4247 | HAL_TXQ_BACKOFF_DISABLE 4248 | HAL_TXQ_ARB_LOCKOUT_GLOBAL 4249 ; 4250 qi.tqi_aifs = 0; 4251 /* XXX +dbaprep? */ 4252 qi.tqi_readyTime = sc->sc_tdmaslotlen; 4253 qi.tqi_burstTime = qi.tqi_readyTime; 4254 } else { 4255 #endif 4256 /* 4257 * XXX shouldn't this just use the default flags 4258 * used in the previous queue setup? 4259 */ 4260 qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE 4261 | HAL_TXQ_TXERRINT_ENABLE 4262 | HAL_TXQ_TXDESCINT_ENABLE 4263 | HAL_TXQ_TXURNINT_ENABLE 4264 | HAL_TXQ_TXEOLINT_ENABLE 4265 ; 4266 qi.tqi_aifs = wmep->wmep_aifsn; 4267 qi.tqi_cwmin = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin); 4268 qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax); 4269 qi.tqi_readyTime = 0; 4270 qi.tqi_burstTime = IEEE80211_TXOP_TO_US(wmep->wmep_txopLimit); 4271 #ifdef IEEE80211_SUPPORT_TDMA 4272 } 4273 #endif 4274 4275 DPRINTF(sc, ATH_DEBUG_RESET, 4276 "%s: Q%u qflags 0x%x aifs %u cwmin %u cwmax %u burstTime %u\n", 4277 __func__, txq->axq_qnum, qi.tqi_qflags, 4278 qi.tqi_aifs, qi.tqi_cwmin, qi.tqi_cwmax, qi.tqi_burstTime); 4279 4280 if (!ath_hal_settxqueueprops(ah, txq->axq_qnum, &qi)) { 4281 device_printf(sc->sc_dev, "unable to update hardware queue " 4282 "parameters for %s traffic!\n", ieee80211_wme_acnames[ac]); 4283 return 0; 4284 } else { 4285 ath_hal_resettxqueue(ah, txq->axq_qnum); /* push to h/w */ 4286 return 1; 4287 } 4288 #undef ATH_EXPONENT_TO_VALUE 4289 } 4290 4291 /* 4292 * Callback from the 802.11 layer to update WME parameters. 4293 */ 4294 int 4295 ath_wme_update(struct ieee80211com *ic) 4296 { 4297 struct ath_softc *sc = ic->ic_softc; 4298 4299 return !ath_txq_update(sc, WME_AC_BE) || 4300 !ath_txq_update(sc, WME_AC_BK) || 4301 !ath_txq_update(sc, WME_AC_VI) || 4302 !ath_txq_update(sc, WME_AC_VO) ? EIO : 0; 4303 } 4304 4305 /* 4306 * Reclaim resources for a setup queue. 4307 */ 4308 static void 4309 ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq) 4310 { 4311 4312 ath_hal_releasetxqueue(sc->sc_ah, txq->axq_qnum); 4313 sc->sc_txqsetup &= ~(1<<txq->axq_qnum); 4314 ATH_TXQ_LOCK_DESTROY(txq); 4315 } 4316 4317 /* 4318 * Reclaim all tx queue resources. 4319 */ 4320 static void 4321 ath_tx_cleanup(struct ath_softc *sc) 4322 { 4323 int i; 4324 4325 ATH_TXBUF_LOCK_DESTROY(sc); 4326 for (i = 0; i < HAL_NUM_TX_QUEUES; i++) 4327 if (ATH_TXQ_SETUP(sc, i)) 4328 ath_tx_cleanupq(sc, &sc->sc_txq[i]); 4329 } 4330 4331 /* 4332 * Return h/w rate index for an IEEE rate (w/o basic rate bit) 4333 * using the current rates in sc_rixmap. 4334 */ 4335 int 4336 ath_tx_findrix(const struct ath_softc *sc, uint8_t rate) 4337 { 4338 int rix = sc->sc_rixmap[rate]; 4339 /* NB: return lowest rix for invalid rate */ 4340 return (rix == 0xff ? 0 : rix); 4341 } 4342 4343 static void 4344 ath_tx_update_stats(struct ath_softc *sc, struct ath_tx_status *ts, 4345 struct ath_buf *bf) 4346 { 4347 struct ieee80211_node *ni = bf->bf_node; 4348 struct ieee80211com *ic = &sc->sc_ic; 4349 int sr, lr, pri; 4350 4351 if (ts->ts_status == 0) { 4352 u_int8_t txant = ts->ts_antenna; 4353 sc->sc_stats.ast_ant_tx[txant]++; 4354 sc->sc_ant_tx[txant]++; 4355 if (ts->ts_finaltsi != 0) 4356 sc->sc_stats.ast_tx_altrate++; 4357 pri = M_WME_GETAC(bf->bf_m); 4358 if (pri >= WME_AC_VO) 4359 ic->ic_wme.wme_hipri_traffic++; 4360 if ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0) 4361 ni->ni_inact = ni->ni_inact_reload; 4362 } else { 4363 if (ts->ts_status & HAL_TXERR_XRETRY) 4364 sc->sc_stats.ast_tx_xretries++; 4365 if (ts->ts_status & HAL_TXERR_FIFO) 4366 sc->sc_stats.ast_tx_fifoerr++; 4367 if (ts->ts_status & HAL_TXERR_FILT) 4368 sc->sc_stats.ast_tx_filtered++; 4369 if (ts->ts_status & HAL_TXERR_XTXOP) 4370 sc->sc_stats.ast_tx_xtxop++; 4371 if (ts->ts_status & HAL_TXERR_TIMER_EXPIRED) 4372 sc->sc_stats.ast_tx_timerexpired++; 4373 4374 if (bf->bf_m->m_flags & M_FF) 4375 sc->sc_stats.ast_ff_txerr++; 4376 } 4377 /* XXX when is this valid? */ 4378 if (ts->ts_flags & HAL_TX_DESC_CFG_ERR) 4379 sc->sc_stats.ast_tx_desccfgerr++; 4380 /* 4381 * This can be valid for successful frame transmission! 4382 * If there's a TX FIFO underrun during aggregate transmission, 4383 * the MAC will pad the rest of the aggregate with delimiters. 4384 * If a BA is returned, the frame is marked as "OK" and it's up 4385 * to the TX completion code to notice which frames weren't 4386 * successfully transmitted. 4387 */ 4388 if (ts->ts_flags & HAL_TX_DATA_UNDERRUN) 4389 sc->sc_stats.ast_tx_data_underrun++; 4390 if (ts->ts_flags & HAL_TX_DELIM_UNDERRUN) 4391 sc->sc_stats.ast_tx_delim_underrun++; 4392 4393 sr = ts->ts_shortretry; 4394 lr = ts->ts_longretry; 4395 sc->sc_stats.ast_tx_shortretry += sr; 4396 sc->sc_stats.ast_tx_longretry += lr; 4397 4398 } 4399 4400 /* 4401 * The default completion. If fail is 1, this means 4402 * "please don't retry the frame, and just return -1 status 4403 * to the net80211 stack. 4404 */ 4405 void 4406 ath_tx_default_comp(struct ath_softc *sc, struct ath_buf *bf, int fail) 4407 { 4408 struct ath_tx_status *ts = &bf->bf_status.ds_txstat; 4409 int st; 4410 4411 if (fail == 1) 4412 st = -1; 4413 else 4414 st = ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0) ? 4415 ts->ts_status : HAL_TXERR_XRETRY; 4416 4417 #if 0 4418 if (bf->bf_state.bfs_dobaw) 4419 device_printf(sc->sc_dev, 4420 "%s: bf %p: seqno %d: dobaw should've been cleared!\n", 4421 __func__, 4422 bf, 4423 SEQNO(bf->bf_state.bfs_seqno)); 4424 #endif 4425 if (bf->bf_next != NULL) 4426 device_printf(sc->sc_dev, 4427 "%s: bf %p: seqno %d: bf_next not NULL!\n", 4428 __func__, 4429 bf, 4430 SEQNO(bf->bf_state.bfs_seqno)); 4431 4432 /* 4433 * Check if the node software queue is empty; if so 4434 * then clear the TIM. 4435 * 4436 * This needs to be done before the buffer is freed as 4437 * otherwise the node reference will have been released 4438 * and the node may not actually exist any longer. 4439 * 4440 * XXX I don't like this belonging here, but it's cleaner 4441 * to do it here right now then all the other places 4442 * where ath_tx_default_comp() is called. 4443 * 4444 * XXX TODO: during drain, ensure that the callback is 4445 * being called so we get a chance to update the TIM. 4446 */ 4447 if (bf->bf_node) { 4448 ATH_TX_LOCK(sc); 4449 ath_tx_update_tim(sc, bf->bf_node, 0); 4450 ATH_TX_UNLOCK(sc); 4451 } 4452 4453 /* 4454 * Do any tx complete callback. Note this must 4455 * be done before releasing the node reference. 4456 * This will free the mbuf, release the net80211 4457 * node and recycle the ath_buf. 4458 */ 4459 ath_tx_freebuf(sc, bf, st); 4460 } 4461 4462 /* 4463 * Update rate control with the given completion status. 4464 */ 4465 void 4466 ath_tx_update_ratectrl(struct ath_softc *sc, struct ieee80211_node *ni, 4467 struct ath_rc_series *rc, struct ath_tx_status *ts, int frmlen, 4468 int nframes, int nbad) 4469 { 4470 struct ath_node *an; 4471 4472 /* Only for unicast frames */ 4473 if (ni == NULL) 4474 return; 4475 4476 an = ATH_NODE(ni); 4477 ATH_NODE_UNLOCK_ASSERT(an); 4478 4479 if ((ts->ts_status & HAL_TXERR_FILT) == 0) { 4480 ATH_NODE_LOCK(an); 4481 ath_rate_tx_complete(sc, an, rc, ts, frmlen, nframes, nbad); 4482 ATH_NODE_UNLOCK(an); 4483 } 4484 } 4485 4486 /* 4487 * Process the completion of the given buffer. 4488 * 4489 * This calls the rate control update and then the buffer completion. 4490 * This will either free the buffer or requeue it. In any case, the 4491 * bf pointer should be treated as invalid after this function is called. 4492 */ 4493 void 4494 ath_tx_process_buf_completion(struct ath_softc *sc, struct ath_txq *txq, 4495 struct ath_tx_status *ts, struct ath_buf *bf) 4496 { 4497 struct ieee80211_node *ni = bf->bf_node; 4498 4499 ATH_TX_UNLOCK_ASSERT(sc); 4500 ATH_TXQ_UNLOCK_ASSERT(txq); 4501 4502 /* If unicast frame, update general statistics */ 4503 if (ni != NULL) { 4504 /* update statistics */ 4505 ath_tx_update_stats(sc, ts, bf); 4506 } 4507 4508 /* 4509 * Call the completion handler. 4510 * The completion handler is responsible for 4511 * calling the rate control code. 4512 * 4513 * Frames with no completion handler get the 4514 * rate control code called here. 4515 */ 4516 if (bf->bf_comp == NULL) { 4517 if ((ts->ts_status & HAL_TXERR_FILT) == 0 && 4518 (bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0) { 4519 /* 4520 * XXX assume this isn't an aggregate 4521 * frame. 4522 */ 4523 ath_tx_update_ratectrl(sc, ni, 4524 bf->bf_state.bfs_rc, ts, 4525 bf->bf_state.bfs_pktlen, 1, 4526 (ts->ts_status == 0 ? 0 : 1)); 4527 } 4528 ath_tx_default_comp(sc, bf, 0); 4529 } else 4530 bf->bf_comp(sc, bf, 0); 4531 } 4532 4533 4534 4535 /* 4536 * Process completed xmit descriptors from the specified queue. 4537 * Kick the packet scheduler if needed. This can occur from this 4538 * particular task. 4539 */ 4540 static int 4541 ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq, int dosched) 4542 { 4543 struct ath_hal *ah = sc->sc_ah; 4544 struct ath_buf *bf; 4545 struct ath_desc *ds; 4546 struct ath_tx_status *ts; 4547 struct ieee80211_node *ni; 4548 #ifdef IEEE80211_SUPPORT_SUPERG 4549 struct ieee80211com *ic = &sc->sc_ic; 4550 #endif /* IEEE80211_SUPPORT_SUPERG */ 4551 int nacked; 4552 HAL_STATUS status; 4553 4554 DPRINTF(sc, ATH_DEBUG_TX_PROC, "%s: tx queue %u head %p link %p\n", 4555 __func__, txq->axq_qnum, 4556 (caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum), 4557 txq->axq_link); 4558 4559 ATH_KTR(sc, ATH_KTR_TXCOMP, 4, 4560 "ath_tx_processq: txq=%u head %p link %p depth %p", 4561 txq->axq_qnum, 4562 (caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum), 4563 txq->axq_link, 4564 txq->axq_depth); 4565 4566 nacked = 0; 4567 for (;;) { 4568 ATH_TXQ_LOCK(txq); 4569 txq->axq_intrcnt = 0; /* reset periodic desc intr count */ 4570 bf = TAILQ_FIRST(&txq->axq_q); 4571 if (bf == NULL) { 4572 ATH_TXQ_UNLOCK(txq); 4573 break; 4574 } 4575 ds = bf->bf_lastds; /* XXX must be setup correctly! */ 4576 ts = &bf->bf_status.ds_txstat; 4577 4578 status = ath_hal_txprocdesc(ah, ds, ts); 4579 #ifdef ATH_DEBUG 4580 if (sc->sc_debug & ATH_DEBUG_XMIT_DESC) 4581 ath_printtxbuf(sc, bf, txq->axq_qnum, 0, 4582 status == HAL_OK); 4583 else if ((sc->sc_debug & ATH_DEBUG_RESET) && (dosched == 0)) 4584 ath_printtxbuf(sc, bf, txq->axq_qnum, 0, 4585 status == HAL_OK); 4586 #endif 4587 #ifdef ATH_DEBUG_ALQ 4588 if (if_ath_alq_checkdebug(&sc->sc_alq, 4589 ATH_ALQ_EDMA_TXSTATUS)) { 4590 if_ath_alq_post(&sc->sc_alq, ATH_ALQ_EDMA_TXSTATUS, 4591 sc->sc_tx_statuslen, 4592 (char *) ds); 4593 } 4594 #endif 4595 4596 if (status == HAL_EINPROGRESS) { 4597 ATH_KTR(sc, ATH_KTR_TXCOMP, 3, 4598 "ath_tx_processq: txq=%u, bf=%p ds=%p, HAL_EINPROGRESS", 4599 txq->axq_qnum, bf, ds); 4600 ATH_TXQ_UNLOCK(txq); 4601 break; 4602 } 4603 ATH_TXQ_REMOVE(txq, bf, bf_list); 4604 4605 /* 4606 * Sanity check. 4607 */ 4608 if (txq->axq_qnum != bf->bf_state.bfs_tx_queue) { 4609 device_printf(sc->sc_dev, 4610 "%s: TXQ=%d: bf=%p, bfs_tx_queue=%d\n", 4611 __func__, 4612 txq->axq_qnum, 4613 bf, 4614 bf->bf_state.bfs_tx_queue); 4615 } 4616 if (txq->axq_qnum != bf->bf_last->bf_state.bfs_tx_queue) { 4617 device_printf(sc->sc_dev, 4618 "%s: TXQ=%d: bf_last=%p, bfs_tx_queue=%d\n", 4619 __func__, 4620 txq->axq_qnum, 4621 bf->bf_last, 4622 bf->bf_last->bf_state.bfs_tx_queue); 4623 } 4624 4625 #if 0 4626 if (txq->axq_depth > 0) { 4627 /* 4628 * More frames follow. Mark the buffer busy 4629 * so it's not re-used while the hardware may 4630 * still re-read the link field in the descriptor. 4631 * 4632 * Use the last buffer in an aggregate as that 4633 * is where the hardware may be - intermediate 4634 * descriptors won't be "busy". 4635 */ 4636 bf->bf_last->bf_flags |= ATH_BUF_BUSY; 4637 } else 4638 txq->axq_link = NULL; 4639 #else 4640 bf->bf_last->bf_flags |= ATH_BUF_BUSY; 4641 #endif 4642 if (bf->bf_state.bfs_aggr) 4643 txq->axq_aggr_depth--; 4644 4645 ni = bf->bf_node; 4646 4647 ATH_KTR(sc, ATH_KTR_TXCOMP, 5, 4648 "ath_tx_processq: txq=%u, bf=%p, ds=%p, ni=%p, ts_status=0x%08x", 4649 txq->axq_qnum, bf, ds, ni, ts->ts_status); 4650 /* 4651 * If unicast frame was ack'd update RSSI, 4652 * including the last rx time used to 4653 * workaround phantom bmiss interrupts. 4654 */ 4655 if (ni != NULL && ts->ts_status == 0 && 4656 ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0)) { 4657 nacked++; 4658 sc->sc_stats.ast_tx_rssi = ts->ts_rssi; 4659 ATH_RSSI_LPF(sc->sc_halstats.ns_avgtxrssi, 4660 ts->ts_rssi); 4661 } 4662 ATH_TXQ_UNLOCK(txq); 4663 4664 /* 4665 * Update statistics and call completion 4666 */ 4667 ath_tx_process_buf_completion(sc, txq, ts, bf); 4668 4669 /* XXX at this point, bf and ni may be totally invalid */ 4670 } 4671 #ifdef IEEE80211_SUPPORT_SUPERG 4672 /* 4673 * Flush fast-frame staging queue when traffic slows. 4674 */ 4675 if (txq->axq_depth <= 1) 4676 ieee80211_ff_flush(ic, txq->axq_ac); 4677 #endif 4678 4679 /* Kick the software TXQ scheduler */ 4680 if (dosched) { 4681 ATH_TX_LOCK(sc); 4682 ath_txq_sched(sc, txq); 4683 ATH_TX_UNLOCK(sc); 4684 } 4685 4686 ATH_KTR(sc, ATH_KTR_TXCOMP, 1, 4687 "ath_tx_processq: txq=%u: done", 4688 txq->axq_qnum); 4689 4690 return nacked; 4691 } 4692 4693 #define TXQACTIVE(t, q) ( (t) & (1 << (q))) 4694 4695 /* 4696 * Deferred processing of transmit interrupt; special-cased 4697 * for a single hardware transmit queue (e.g. 5210 and 5211). 4698 */ 4699 static void 4700 ath_tx_proc_q0(void *arg, int npending) 4701 { 4702 struct ath_softc *sc = arg; 4703 uint32_t txqs; 4704 4705 ATH_PCU_LOCK(sc); 4706 sc->sc_txproc_cnt++; 4707 txqs = sc->sc_txq_active; 4708 sc->sc_txq_active &= ~txqs; 4709 ATH_PCU_UNLOCK(sc); 4710 4711 ATH_LOCK(sc); 4712 ath_power_set_power_state(sc, HAL_PM_AWAKE); 4713 ATH_UNLOCK(sc); 4714 4715 ATH_KTR(sc, ATH_KTR_TXCOMP, 1, 4716 "ath_tx_proc_q0: txqs=0x%08x", txqs); 4717 4718 if (TXQACTIVE(txqs, 0) && ath_tx_processq(sc, &sc->sc_txq[0], 1)) 4719 /* XXX why is lastrx updated in tx code? */ 4720 sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah); 4721 if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum)) 4722 ath_tx_processq(sc, sc->sc_cabq, 1); 4723 sc->sc_wd_timer = 0; 4724 4725 if (sc->sc_softled) 4726 ath_led_event(sc, sc->sc_txrix); 4727 4728 ATH_PCU_LOCK(sc); 4729 sc->sc_txproc_cnt--; 4730 ATH_PCU_UNLOCK(sc); 4731 4732 ATH_LOCK(sc); 4733 ath_power_restore_power_state(sc); 4734 ATH_UNLOCK(sc); 4735 4736 ath_tx_kick(sc); 4737 } 4738 4739 /* 4740 * Deferred processing of transmit interrupt; special-cased 4741 * for four hardware queues, 0-3 (e.g. 5212 w/ WME support). 4742 */ 4743 static void 4744 ath_tx_proc_q0123(void *arg, int npending) 4745 { 4746 struct ath_softc *sc = arg; 4747 int nacked; 4748 uint32_t txqs; 4749 4750 ATH_PCU_LOCK(sc); 4751 sc->sc_txproc_cnt++; 4752 txqs = sc->sc_txq_active; 4753 sc->sc_txq_active &= ~txqs; 4754 ATH_PCU_UNLOCK(sc); 4755 4756 ATH_LOCK(sc); 4757 ath_power_set_power_state(sc, HAL_PM_AWAKE); 4758 ATH_UNLOCK(sc); 4759 4760 ATH_KTR(sc, ATH_KTR_TXCOMP, 1, 4761 "ath_tx_proc_q0123: txqs=0x%08x", txqs); 4762 4763 /* 4764 * Process each active queue. 4765 */ 4766 nacked = 0; 4767 if (TXQACTIVE(txqs, 0)) 4768 nacked += ath_tx_processq(sc, &sc->sc_txq[0], 1); 4769 if (TXQACTIVE(txqs, 1)) 4770 nacked += ath_tx_processq(sc, &sc->sc_txq[1], 1); 4771 if (TXQACTIVE(txqs, 2)) 4772 nacked += ath_tx_processq(sc, &sc->sc_txq[2], 1); 4773 if (TXQACTIVE(txqs, 3)) 4774 nacked += ath_tx_processq(sc, &sc->sc_txq[3], 1); 4775 if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum)) 4776 ath_tx_processq(sc, sc->sc_cabq, 1); 4777 if (nacked) 4778 sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah); 4779 4780 sc->sc_wd_timer = 0; 4781 4782 if (sc->sc_softled) 4783 ath_led_event(sc, sc->sc_txrix); 4784 4785 ATH_PCU_LOCK(sc); 4786 sc->sc_txproc_cnt--; 4787 ATH_PCU_UNLOCK(sc); 4788 4789 ATH_LOCK(sc); 4790 ath_power_restore_power_state(sc); 4791 ATH_UNLOCK(sc); 4792 4793 ath_tx_kick(sc); 4794 } 4795 4796 /* 4797 * Deferred processing of transmit interrupt. 4798 */ 4799 static void 4800 ath_tx_proc(void *arg, int npending) 4801 { 4802 struct ath_softc *sc = arg; 4803 int i, nacked; 4804 uint32_t txqs; 4805 4806 ATH_PCU_LOCK(sc); 4807 sc->sc_txproc_cnt++; 4808 txqs = sc->sc_txq_active; 4809 sc->sc_txq_active &= ~txqs; 4810 ATH_PCU_UNLOCK(sc); 4811 4812 ATH_LOCK(sc); 4813 ath_power_set_power_state(sc, HAL_PM_AWAKE); 4814 ATH_UNLOCK(sc); 4815 4816 ATH_KTR(sc, ATH_KTR_TXCOMP, 1, "ath_tx_proc: txqs=0x%08x", txqs); 4817 4818 /* 4819 * Process each active queue. 4820 */ 4821 nacked = 0; 4822 for (i = 0; i < HAL_NUM_TX_QUEUES; i++) 4823 if (ATH_TXQ_SETUP(sc, i) && TXQACTIVE(txqs, i)) 4824 nacked += ath_tx_processq(sc, &sc->sc_txq[i], 1); 4825 if (nacked) 4826 sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah); 4827 4828 sc->sc_wd_timer = 0; 4829 4830 if (sc->sc_softled) 4831 ath_led_event(sc, sc->sc_txrix); 4832 4833 ATH_PCU_LOCK(sc); 4834 sc->sc_txproc_cnt--; 4835 ATH_PCU_UNLOCK(sc); 4836 4837 ATH_LOCK(sc); 4838 ath_power_restore_power_state(sc); 4839 ATH_UNLOCK(sc); 4840 4841 ath_tx_kick(sc); 4842 } 4843 #undef TXQACTIVE 4844 4845 /* 4846 * Deferred processing of TXQ rescheduling. 4847 */ 4848 static void 4849 ath_txq_sched_tasklet(void *arg, int npending) 4850 { 4851 struct ath_softc *sc = arg; 4852 int i; 4853 4854 /* XXX is skipping ok? */ 4855 ATH_PCU_LOCK(sc); 4856 #if 0 4857 if (sc->sc_inreset_cnt > 0) { 4858 device_printf(sc->sc_dev, 4859 "%s: sc_inreset_cnt > 0; skipping\n", __func__); 4860 ATH_PCU_UNLOCK(sc); 4861 return; 4862 } 4863 #endif 4864 sc->sc_txproc_cnt++; 4865 ATH_PCU_UNLOCK(sc); 4866 4867 ATH_LOCK(sc); 4868 ath_power_set_power_state(sc, HAL_PM_AWAKE); 4869 ATH_UNLOCK(sc); 4870 4871 ATH_TX_LOCK(sc); 4872 for (i = 0; i < HAL_NUM_TX_QUEUES; i++) { 4873 if (ATH_TXQ_SETUP(sc, i)) { 4874 ath_txq_sched(sc, &sc->sc_txq[i]); 4875 } 4876 } 4877 ATH_TX_UNLOCK(sc); 4878 4879 ATH_LOCK(sc); 4880 ath_power_restore_power_state(sc); 4881 ATH_UNLOCK(sc); 4882 4883 ATH_PCU_LOCK(sc); 4884 sc->sc_txproc_cnt--; 4885 ATH_PCU_UNLOCK(sc); 4886 } 4887 4888 void 4889 ath_returnbuf_tail(struct ath_softc *sc, struct ath_buf *bf) 4890 { 4891 4892 ATH_TXBUF_LOCK_ASSERT(sc); 4893 4894 if (bf->bf_flags & ATH_BUF_MGMT) 4895 TAILQ_INSERT_TAIL(&sc->sc_txbuf_mgmt, bf, bf_list); 4896 else { 4897 TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list); 4898 sc->sc_txbuf_cnt++; 4899 if (sc->sc_txbuf_cnt > ath_txbuf) { 4900 device_printf(sc->sc_dev, 4901 "%s: sc_txbuf_cnt > %d?\n", 4902 __func__, 4903 ath_txbuf); 4904 sc->sc_txbuf_cnt = ath_txbuf; 4905 } 4906 } 4907 } 4908 4909 void 4910 ath_returnbuf_head(struct ath_softc *sc, struct ath_buf *bf) 4911 { 4912 4913 ATH_TXBUF_LOCK_ASSERT(sc); 4914 4915 if (bf->bf_flags & ATH_BUF_MGMT) 4916 TAILQ_INSERT_HEAD(&sc->sc_txbuf_mgmt, bf, bf_list); 4917 else { 4918 TAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list); 4919 sc->sc_txbuf_cnt++; 4920 if (sc->sc_txbuf_cnt > ATH_TXBUF) { 4921 device_printf(sc->sc_dev, 4922 "%s: sc_txbuf_cnt > %d?\n", 4923 __func__, 4924 ATH_TXBUF); 4925 sc->sc_txbuf_cnt = ATH_TXBUF; 4926 } 4927 } 4928 } 4929 4930 /* 4931 * Free the holding buffer if it exists 4932 */ 4933 void 4934 ath_txq_freeholdingbuf(struct ath_softc *sc, struct ath_txq *txq) 4935 { 4936 ATH_TXBUF_UNLOCK_ASSERT(sc); 4937 ATH_TXQ_LOCK_ASSERT(txq); 4938 4939 if (txq->axq_holdingbf == NULL) 4940 return; 4941 4942 txq->axq_holdingbf->bf_flags &= ~ATH_BUF_BUSY; 4943 4944 ATH_TXBUF_LOCK(sc); 4945 ath_returnbuf_tail(sc, txq->axq_holdingbf); 4946 ATH_TXBUF_UNLOCK(sc); 4947 4948 txq->axq_holdingbf = NULL; 4949 } 4950 4951 /* 4952 * Add this buffer to the holding queue, freeing the previous 4953 * one if it exists. 4954 */ 4955 static void 4956 ath_txq_addholdingbuf(struct ath_softc *sc, struct ath_buf *bf) 4957 { 4958 struct ath_txq *txq; 4959 4960 txq = &sc->sc_txq[bf->bf_state.bfs_tx_queue]; 4961 4962 ATH_TXBUF_UNLOCK_ASSERT(sc); 4963 ATH_TXQ_LOCK_ASSERT(txq); 4964 4965 /* XXX assert ATH_BUF_BUSY is set */ 4966 4967 /* XXX assert the tx queue is under the max number */ 4968 if (bf->bf_state.bfs_tx_queue > HAL_NUM_TX_QUEUES) { 4969 device_printf(sc->sc_dev, "%s: bf=%p: invalid tx queue (%d)\n", 4970 __func__, 4971 bf, 4972 bf->bf_state.bfs_tx_queue); 4973 bf->bf_flags &= ~ATH_BUF_BUSY; 4974 ath_returnbuf_tail(sc, bf); 4975 return; 4976 } 4977 ath_txq_freeholdingbuf(sc, txq); 4978 txq->axq_holdingbf = bf; 4979 } 4980 4981 /* 4982 * Return a buffer to the pool and update the 'busy' flag on the 4983 * previous 'tail' entry. 4984 * 4985 * This _must_ only be called when the buffer is involved in a completed 4986 * TX. The logic is that if it was part of an active TX, the previous 4987 * buffer on the list is now not involved in a halted TX DMA queue, waiting 4988 * for restart (eg for TDMA.) 4989 * 4990 * The caller must free the mbuf and recycle the node reference. 4991 * 4992 * XXX This method of handling busy / holding buffers is insanely stupid. 4993 * It requires bf_state.bfs_tx_queue to be correctly assigned. It would 4994 * be much nicer if buffers in the processq() methods would instead be 4995 * always completed there (pushed onto a txq or ath_bufhead) so we knew 4996 * exactly what hardware queue they came from in the first place. 4997 */ 4998 void 4999 ath_freebuf(struct ath_softc *sc, struct ath_buf *bf) 5000 { 5001 struct ath_txq *txq; 5002 5003 txq = &sc->sc_txq[bf->bf_state.bfs_tx_queue]; 5004 5005 KASSERT((bf->bf_node == NULL), ("%s: bf->bf_node != NULL\n", __func__)); 5006 KASSERT((bf->bf_m == NULL), ("%s: bf->bf_m != NULL\n", __func__)); 5007 5008 /* 5009 * If this buffer is busy, push it onto the holding queue. 5010 */ 5011 if (bf->bf_flags & ATH_BUF_BUSY) { 5012 ATH_TXQ_LOCK(txq); 5013 ath_txq_addholdingbuf(sc, bf); 5014 ATH_TXQ_UNLOCK(txq); 5015 return; 5016 } 5017 5018 /* 5019 * Not a busy buffer, so free normally 5020 */ 5021 ATH_TXBUF_LOCK(sc); 5022 ath_returnbuf_tail(sc, bf); 5023 ATH_TXBUF_UNLOCK(sc); 5024 } 5025 5026 /* 5027 * This is currently used by ath_tx_draintxq() and 5028 * ath_tx_tid_free_pkts(). 5029 * 5030 * It recycles a single ath_buf. 5031 */ 5032 void 5033 ath_tx_freebuf(struct ath_softc *sc, struct ath_buf *bf, int status) 5034 { 5035 struct ieee80211_node *ni = bf->bf_node; 5036 struct mbuf *m0 = bf->bf_m; 5037 5038 /* 5039 * Make sure that we only sync/unload if there's an mbuf. 5040 * If not (eg we cloned a buffer), the unload will have already 5041 * occured. 5042 */ 5043 if (bf->bf_m != NULL) { 5044 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, 5045 BUS_DMASYNC_POSTWRITE); 5046 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 5047 } 5048 5049 bf->bf_node = NULL; 5050 bf->bf_m = NULL; 5051 5052 /* Free the buffer, it's not needed any longer */ 5053 ath_freebuf(sc, bf); 5054 5055 /* Pass the buffer back to net80211 - completing it */ 5056 ieee80211_tx_complete(ni, m0, status); 5057 } 5058 5059 static struct ath_buf * 5060 ath_tx_draintxq_get_one(struct ath_softc *sc, struct ath_txq *txq) 5061 { 5062 struct ath_buf *bf; 5063 5064 ATH_TXQ_LOCK_ASSERT(txq); 5065 5066 /* 5067 * Drain the FIFO queue first, then if it's 5068 * empty, move to the normal frame queue. 5069 */ 5070 bf = TAILQ_FIRST(&txq->fifo.axq_q); 5071 if (bf != NULL) { 5072 /* 5073 * Is it the last buffer in this set? 5074 * Decrement the FIFO counter. 5075 */ 5076 if (bf->bf_flags & ATH_BUF_FIFOEND) { 5077 if (txq->axq_fifo_depth == 0) { 5078 device_printf(sc->sc_dev, 5079 "%s: Q%d: fifo_depth=0, fifo.axq_depth=%d?\n", 5080 __func__, 5081 txq->axq_qnum, 5082 txq->fifo.axq_depth); 5083 } else 5084 txq->axq_fifo_depth--; 5085 } 5086 ATH_TXQ_REMOVE(&txq->fifo, bf, bf_list); 5087 return (bf); 5088 } 5089 5090 /* 5091 * Debugging! 5092 */ 5093 if (txq->axq_fifo_depth != 0 || txq->fifo.axq_depth != 0) { 5094 device_printf(sc->sc_dev, 5095 "%s: Q%d: fifo_depth=%d, fifo.axq_depth=%d\n", 5096 __func__, 5097 txq->axq_qnum, 5098 txq->axq_fifo_depth, 5099 txq->fifo.axq_depth); 5100 } 5101 5102 /* 5103 * Now drain the pending queue. 5104 */ 5105 bf = TAILQ_FIRST(&txq->axq_q); 5106 if (bf == NULL) { 5107 txq->axq_link = NULL; 5108 return (NULL); 5109 } 5110 ATH_TXQ_REMOVE(txq, bf, bf_list); 5111 return (bf); 5112 } 5113 5114 void 5115 ath_tx_draintxq(struct ath_softc *sc, struct ath_txq *txq) 5116 { 5117 #ifdef ATH_DEBUG 5118 struct ath_hal *ah = sc->sc_ah; 5119 #endif 5120 struct ath_buf *bf; 5121 u_int ix; 5122 5123 /* 5124 * NB: this assumes output has been stopped and 5125 * we do not need to block ath_tx_proc 5126 */ 5127 for (ix = 0;; ix++) { 5128 ATH_TXQ_LOCK(txq); 5129 bf = ath_tx_draintxq_get_one(sc, txq); 5130 if (bf == NULL) { 5131 ATH_TXQ_UNLOCK(txq); 5132 break; 5133 } 5134 if (bf->bf_state.bfs_aggr) 5135 txq->axq_aggr_depth--; 5136 #ifdef ATH_DEBUG 5137 if (sc->sc_debug & ATH_DEBUG_RESET) { 5138 struct ieee80211com *ic = &sc->sc_ic; 5139 int status = 0; 5140 5141 /* 5142 * EDMA operation has a TX completion FIFO 5143 * separate from the TX descriptor, so this 5144 * method of checking the "completion" status 5145 * is wrong. 5146 */ 5147 if (! sc->sc_isedma) { 5148 status = (ath_hal_txprocdesc(ah, 5149 bf->bf_lastds, 5150 &bf->bf_status.ds_txstat) == HAL_OK); 5151 } 5152 ath_printtxbuf(sc, bf, txq->axq_qnum, ix, status); 5153 ieee80211_dump_pkt(ic, mtod(bf->bf_m, const uint8_t *), 5154 bf->bf_m->m_len, 0, -1); 5155 } 5156 #endif /* ATH_DEBUG */ 5157 /* 5158 * Since we're now doing magic in the completion 5159 * functions, we -must- call it for aggregation 5160 * destinations or BAW tracking will get upset. 5161 */ 5162 /* 5163 * Clear ATH_BUF_BUSY; the completion handler 5164 * will free the buffer. 5165 */ 5166 ATH_TXQ_UNLOCK(txq); 5167 bf->bf_flags &= ~ATH_BUF_BUSY; 5168 if (bf->bf_comp) 5169 bf->bf_comp(sc, bf, 1); 5170 else 5171 ath_tx_default_comp(sc, bf, 1); 5172 } 5173 5174 /* 5175 * Free the holding buffer if it exists 5176 */ 5177 ATH_TXQ_LOCK(txq); 5178 ath_txq_freeholdingbuf(sc, txq); 5179 ATH_TXQ_UNLOCK(txq); 5180 5181 /* 5182 * Drain software queued frames which are on 5183 * active TIDs. 5184 */ 5185 ath_tx_txq_drain(sc, txq); 5186 } 5187 5188 static void 5189 ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq) 5190 { 5191 struct ath_hal *ah = sc->sc_ah; 5192 5193 ATH_TXQ_LOCK_ASSERT(txq); 5194 5195 DPRINTF(sc, ATH_DEBUG_RESET, 5196 "%s: tx queue [%u] %p, active=%d, hwpending=%d, flags 0x%08x, " 5197 "link %p, holdingbf=%p\n", 5198 __func__, 5199 txq->axq_qnum, 5200 (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, txq->axq_qnum), 5201 (int) (!! ath_hal_txqenabled(ah, txq->axq_qnum)), 5202 (int) ath_hal_numtxpending(ah, txq->axq_qnum), 5203 txq->axq_flags, 5204 txq->axq_link, 5205 txq->axq_holdingbf); 5206 5207 (void) ath_hal_stoptxdma(ah, txq->axq_qnum); 5208 /* We've stopped TX DMA, so mark this as stopped. */ 5209 txq->axq_flags &= ~ATH_TXQ_PUTRUNNING; 5210 5211 #ifdef ATH_DEBUG 5212 if ((sc->sc_debug & ATH_DEBUG_RESET) 5213 && (txq->axq_holdingbf != NULL)) { 5214 ath_printtxbuf(sc, txq->axq_holdingbf, txq->axq_qnum, 0, 0); 5215 } 5216 #endif 5217 } 5218 5219 int 5220 ath_stoptxdma(struct ath_softc *sc) 5221 { 5222 struct ath_hal *ah = sc->sc_ah; 5223 int i; 5224 5225 /* XXX return value */ 5226 if (sc->sc_invalid) 5227 return 0; 5228 5229 if (!sc->sc_invalid) { 5230 /* don't touch the hardware if marked invalid */ 5231 DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n", 5232 __func__, sc->sc_bhalq, 5233 (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq), 5234 NULL); 5235 5236 /* stop the beacon queue */ 5237 (void) ath_hal_stoptxdma(ah, sc->sc_bhalq); 5238 5239 /* Stop the data queues */ 5240 for (i = 0; i < HAL_NUM_TX_QUEUES; i++) { 5241 if (ATH_TXQ_SETUP(sc, i)) { 5242 ATH_TXQ_LOCK(&sc->sc_txq[i]); 5243 ath_tx_stopdma(sc, &sc->sc_txq[i]); 5244 ATH_TXQ_UNLOCK(&sc->sc_txq[i]); 5245 } 5246 } 5247 } 5248 5249 return 1; 5250 } 5251 5252 #ifdef ATH_DEBUG 5253 void 5254 ath_tx_dump(struct ath_softc *sc, struct ath_txq *txq) 5255 { 5256 struct ath_hal *ah = sc->sc_ah; 5257 struct ath_buf *bf; 5258 int i = 0; 5259 5260 if (! (sc->sc_debug & ATH_DEBUG_RESET)) 5261 return; 5262 5263 device_printf(sc->sc_dev, "%s: Q%d: begin\n", 5264 __func__, txq->axq_qnum); 5265 TAILQ_FOREACH(bf, &txq->axq_q, bf_list) { 5266 ath_printtxbuf(sc, bf, txq->axq_qnum, i, 5267 ath_hal_txprocdesc(ah, bf->bf_lastds, 5268 &bf->bf_status.ds_txstat) == HAL_OK); 5269 i++; 5270 } 5271 device_printf(sc->sc_dev, "%s: Q%d: end\n", 5272 __func__, txq->axq_qnum); 5273 } 5274 #endif /* ATH_DEBUG */ 5275 5276 /* 5277 * Drain the transmit queues and reclaim resources. 5278 */ 5279 void 5280 ath_legacy_tx_drain(struct ath_softc *sc, ATH_RESET_TYPE reset_type) 5281 { 5282 struct ath_hal *ah = sc->sc_ah; 5283 struct ath_buf *bf_last; 5284 int i; 5285 5286 (void) ath_stoptxdma(sc); 5287 5288 /* 5289 * Dump the queue contents 5290 */ 5291 for (i = 0; i < HAL_NUM_TX_QUEUES; i++) { 5292 /* 5293 * XXX TODO: should we just handle the completed TX frames 5294 * here, whether or not the reset is a full one or not? 5295 */ 5296 if (ATH_TXQ_SETUP(sc, i)) { 5297 #ifdef ATH_DEBUG 5298 if (sc->sc_debug & ATH_DEBUG_RESET) 5299 ath_tx_dump(sc, &sc->sc_txq[i]); 5300 #endif /* ATH_DEBUG */ 5301 if (reset_type == ATH_RESET_NOLOSS) { 5302 ath_tx_processq(sc, &sc->sc_txq[i], 0); 5303 ATH_TXQ_LOCK(&sc->sc_txq[i]); 5304 /* 5305 * Free the holding buffer; DMA is now 5306 * stopped. 5307 */ 5308 ath_txq_freeholdingbuf(sc, &sc->sc_txq[i]); 5309 /* 5310 * Setup the link pointer to be the 5311 * _last_ buffer/descriptor in the list. 5312 * If there's nothing in the list, set it 5313 * to NULL. 5314 */ 5315 bf_last = ATH_TXQ_LAST(&sc->sc_txq[i], 5316 axq_q_s); 5317 if (bf_last != NULL) { 5318 ath_hal_gettxdesclinkptr(ah, 5319 bf_last->bf_lastds, 5320 &sc->sc_txq[i].axq_link); 5321 } else { 5322 sc->sc_txq[i].axq_link = NULL; 5323 } 5324 ATH_TXQ_UNLOCK(&sc->sc_txq[i]); 5325 } else 5326 ath_tx_draintxq(sc, &sc->sc_txq[i]); 5327 } 5328 } 5329 #ifdef ATH_DEBUG 5330 if (sc->sc_debug & ATH_DEBUG_RESET) { 5331 struct ath_buf *bf = TAILQ_FIRST(&sc->sc_bbuf); 5332 if (bf != NULL && bf->bf_m != NULL) { 5333 ath_printtxbuf(sc, bf, sc->sc_bhalq, 0, 5334 ath_hal_txprocdesc(ah, bf->bf_lastds, 5335 &bf->bf_status.ds_txstat) == HAL_OK); 5336 ieee80211_dump_pkt(&sc->sc_ic, 5337 mtod(bf->bf_m, const uint8_t *), bf->bf_m->m_len, 5338 0, -1); 5339 } 5340 } 5341 #endif /* ATH_DEBUG */ 5342 sc->sc_wd_timer = 0; 5343 } 5344 5345 /* 5346 * Update internal state after a channel change. 5347 */ 5348 static void 5349 ath_chan_change(struct ath_softc *sc, struct ieee80211_channel *chan) 5350 { 5351 enum ieee80211_phymode mode; 5352 5353 /* 5354 * Change channels and update the h/w rate map 5355 * if we're switching; e.g. 11a to 11b/g. 5356 */ 5357 mode = ieee80211_chan2mode(chan); 5358 if (mode != sc->sc_curmode) 5359 ath_setcurmode(sc, mode); 5360 sc->sc_curchan = chan; 5361 } 5362 5363 /* 5364 * Set/change channels. If the channel is really being changed, 5365 * it's done by resetting the chip. To accomplish this we must 5366 * first cleanup any pending DMA, then restart stuff after a la 5367 * ath_init. 5368 */ 5369 static int 5370 ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan) 5371 { 5372 struct ieee80211com *ic = &sc->sc_ic; 5373 struct ath_hal *ah = sc->sc_ah; 5374 int ret = 0; 5375 5376 /* Treat this as an interface reset */ 5377 ATH_PCU_UNLOCK_ASSERT(sc); 5378 ATH_UNLOCK_ASSERT(sc); 5379 5380 /* (Try to) stop TX/RX from occuring */ 5381 taskqueue_block(sc->sc_tq); 5382 5383 ATH_PCU_LOCK(sc); 5384 5385 /* Disable interrupts */ 5386 ath_hal_intrset(ah, 0); 5387 5388 /* Stop new RX/TX/interrupt completion */ 5389 if (ath_reset_grablock(sc, 1) == 0) { 5390 device_printf(sc->sc_dev, "%s: concurrent reset! Danger!\n", 5391 __func__); 5392 } 5393 5394 /* Stop pending RX/TX completion */ 5395 ath_txrx_stop_locked(sc); 5396 5397 ATH_PCU_UNLOCK(sc); 5398 5399 DPRINTF(sc, ATH_DEBUG_RESET, "%s: %u (%u MHz, flags 0x%x)\n", 5400 __func__, ieee80211_chan2ieee(ic, chan), 5401 chan->ic_freq, chan->ic_flags); 5402 if (chan != sc->sc_curchan) { 5403 HAL_STATUS status; 5404 /* 5405 * To switch channels clear any pending DMA operations; 5406 * wait long enough for the RX fifo to drain, reset the 5407 * hardware at the new frequency, and then re-enable 5408 * the relevant bits of the h/w. 5409 */ 5410 #if 0 5411 ath_hal_intrset(ah, 0); /* disable interrupts */ 5412 #endif 5413 ath_stoprecv(sc, 1); /* turn off frame recv */ 5414 /* 5415 * First, handle completed TX/RX frames. 5416 */ 5417 ath_rx_flush(sc); 5418 ath_draintxq(sc, ATH_RESET_NOLOSS); 5419 /* 5420 * Next, flush the non-scheduled frames. 5421 */ 5422 ath_draintxq(sc, ATH_RESET_FULL); /* clear pending tx frames */ 5423 5424 ath_update_chainmasks(sc, chan); 5425 ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask, 5426 sc->sc_cur_rxchainmask); 5427 if (!ath_hal_reset(ah, sc->sc_opmode, chan, AH_TRUE, 5428 HAL_RESET_NORMAL, &status)) { 5429 device_printf(sc->sc_dev, "%s: unable to reset " 5430 "channel %u (%u MHz, flags 0x%x), hal status %u\n", 5431 __func__, ieee80211_chan2ieee(ic, chan), 5432 chan->ic_freq, chan->ic_flags, status); 5433 ret = EIO; 5434 goto finish; 5435 } 5436 sc->sc_diversity = ath_hal_getdiversity(ah); 5437 5438 ATH_RX_LOCK(sc); 5439 sc->sc_rx_stopped = 1; 5440 sc->sc_rx_resetted = 1; 5441 ATH_RX_UNLOCK(sc); 5442 5443 /* Let DFS at it in case it's a DFS channel */ 5444 ath_dfs_radar_enable(sc, chan); 5445 5446 /* Let spectral at in case spectral is enabled */ 5447 ath_spectral_enable(sc, chan); 5448 5449 /* 5450 * Let bluetooth coexistence at in case it's needed for this 5451 * channel 5452 */ 5453 ath_btcoex_enable(sc, ic->ic_curchan); 5454 5455 /* 5456 * If we're doing TDMA, enforce the TXOP limitation for chips 5457 * that support it. 5458 */ 5459 if (sc->sc_hasenforcetxop && sc->sc_tdma) 5460 ath_hal_setenforcetxop(sc->sc_ah, 1); 5461 else 5462 ath_hal_setenforcetxop(sc->sc_ah, 0); 5463 5464 /* 5465 * Re-enable rx framework. 5466 */ 5467 if (ath_startrecv(sc) != 0) { 5468 device_printf(sc->sc_dev, 5469 "%s: unable to restart recv logic\n", __func__); 5470 ret = EIO; 5471 goto finish; 5472 } 5473 5474 /* 5475 * Change channels and update the h/w rate map 5476 * if we're switching; e.g. 11a to 11b/g. 5477 */ 5478 ath_chan_change(sc, chan); 5479 5480 /* 5481 * Reset clears the beacon timers; reset them 5482 * here if needed. 5483 */ 5484 if (sc->sc_beacons) { /* restart beacons */ 5485 #ifdef IEEE80211_SUPPORT_TDMA 5486 if (sc->sc_tdma) 5487 ath_tdma_config(sc, NULL); 5488 else 5489 #endif 5490 ath_beacon_config(sc, NULL); 5491 } 5492 5493 /* 5494 * Re-enable interrupts. 5495 */ 5496 #if 0 5497 ath_hal_intrset(ah, sc->sc_imask); 5498 #endif 5499 } 5500 5501 finish: 5502 ATH_PCU_LOCK(sc); 5503 sc->sc_inreset_cnt--; 5504 /* XXX only do this if sc_inreset_cnt == 0? */ 5505 ath_hal_intrset(ah, sc->sc_imask); 5506 ATH_PCU_UNLOCK(sc); 5507 5508 ath_txrx_start(sc); 5509 /* XXX ath_start? */ 5510 5511 return ret; 5512 } 5513 5514 /* 5515 * Periodically recalibrate the PHY to account 5516 * for temperature/environment changes. 5517 */ 5518 static void 5519 ath_calibrate(void *arg) 5520 { 5521 struct ath_softc *sc = arg; 5522 struct ath_hal *ah = sc->sc_ah; 5523 struct ieee80211com *ic = &sc->sc_ic; 5524 HAL_BOOL longCal, isCalDone = AH_TRUE; 5525 HAL_BOOL aniCal, shortCal = AH_FALSE; 5526 int nextcal; 5527 5528 ATH_LOCK_ASSERT(sc); 5529 5530 /* 5531 * Force the hardware awake for ANI work. 5532 */ 5533 ath_power_set_power_state(sc, HAL_PM_AWAKE); 5534 5535 /* Skip trying to do this if we're in reset */ 5536 if (sc->sc_inreset_cnt) 5537 goto restart; 5538 5539 if (ic->ic_flags & IEEE80211_F_SCAN) /* defer, off channel */ 5540 goto restart; 5541 longCal = (ticks - sc->sc_lastlongcal >= ath_longcalinterval*hz); 5542 aniCal = (ticks - sc->sc_lastani >= ath_anicalinterval*hz/1000); 5543 if (sc->sc_doresetcal) 5544 shortCal = (ticks - sc->sc_lastshortcal >= ath_shortcalinterval*hz/1000); 5545 5546 DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: shortCal=%d; longCal=%d; aniCal=%d\n", __func__, shortCal, longCal, aniCal); 5547 if (aniCal) { 5548 sc->sc_stats.ast_ani_cal++; 5549 sc->sc_lastani = ticks; 5550 ath_hal_ani_poll(ah, sc->sc_curchan); 5551 } 5552 5553 if (longCal) { 5554 sc->sc_stats.ast_per_cal++; 5555 sc->sc_lastlongcal = ticks; 5556 if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) { 5557 /* 5558 * Rfgain is out of bounds, reset the chip 5559 * to load new gain values. 5560 */ 5561 DPRINTF(sc, ATH_DEBUG_CALIBRATE, 5562 "%s: rfgain change\n", __func__); 5563 sc->sc_stats.ast_per_rfgain++; 5564 sc->sc_resetcal = 0; 5565 sc->sc_doresetcal = AH_TRUE; 5566 taskqueue_enqueue(sc->sc_tq, &sc->sc_resettask); 5567 callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc); 5568 ath_power_restore_power_state(sc); 5569 return; 5570 } 5571 /* 5572 * If this long cal is after an idle period, then 5573 * reset the data collection state so we start fresh. 5574 */ 5575 if (sc->sc_resetcal) { 5576 (void) ath_hal_calreset(ah, sc->sc_curchan); 5577 sc->sc_lastcalreset = ticks; 5578 sc->sc_lastshortcal = ticks; 5579 sc->sc_resetcal = 0; 5580 sc->sc_doresetcal = AH_TRUE; 5581 } 5582 } 5583 5584 /* Only call if we're doing a short/long cal, not for ANI calibration */ 5585 if (shortCal || longCal) { 5586 isCalDone = AH_FALSE; 5587 if (ath_hal_calibrateN(ah, sc->sc_curchan, longCal, &isCalDone)) { 5588 if (longCal) { 5589 /* 5590 * Calibrate noise floor data again in case of change. 5591 */ 5592 ath_hal_process_noisefloor(ah); 5593 } 5594 } else { 5595 DPRINTF(sc, ATH_DEBUG_ANY, 5596 "%s: calibration of channel %u failed\n", 5597 __func__, sc->sc_curchan->ic_freq); 5598 sc->sc_stats.ast_per_calfail++; 5599 } 5600 if (shortCal) 5601 sc->sc_lastshortcal = ticks; 5602 } 5603 if (!isCalDone) { 5604 restart: 5605 /* 5606 * Use a shorter interval to potentially collect multiple 5607 * data samples required to complete calibration. Once 5608 * we're told the work is done we drop back to a longer 5609 * interval between requests. We're more aggressive doing 5610 * work when operating as an AP to improve operation right 5611 * after startup. 5612 */ 5613 sc->sc_lastshortcal = ticks; 5614 nextcal = ath_shortcalinterval*hz/1000; 5615 if (sc->sc_opmode != HAL_M_HOSTAP) 5616 nextcal *= 10; 5617 sc->sc_doresetcal = AH_TRUE; 5618 } else { 5619 /* nextcal should be the shortest time for next event */ 5620 nextcal = ath_longcalinterval*hz; 5621 if (sc->sc_lastcalreset == 0) 5622 sc->sc_lastcalreset = sc->sc_lastlongcal; 5623 else if (ticks - sc->sc_lastcalreset >= ath_resetcalinterval*hz) 5624 sc->sc_resetcal = 1; /* setup reset next trip */ 5625 sc->sc_doresetcal = AH_FALSE; 5626 } 5627 /* ANI calibration may occur more often than short/long/resetcal */ 5628 if (ath_anicalinterval > 0) 5629 nextcal = MIN(nextcal, ath_anicalinterval*hz/1000); 5630 5631 if (nextcal != 0) { 5632 DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: next +%u (%sisCalDone)\n", 5633 __func__, nextcal, isCalDone ? "" : "!"); 5634 callout_reset(&sc->sc_cal_ch, nextcal, ath_calibrate, sc); 5635 } else { 5636 DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: calibration disabled\n", 5637 __func__); 5638 /* NB: don't rearm timer */ 5639 } 5640 /* 5641 * Restore power state now that we're done. 5642 */ 5643 ath_power_restore_power_state(sc); 5644 } 5645 5646 static void 5647 ath_scan_start(struct ieee80211com *ic) 5648 { 5649 struct ath_softc *sc = ic->ic_softc; 5650 struct ath_hal *ah = sc->sc_ah; 5651 u_int32_t rfilt; 5652 5653 /* XXX calibration timer? */ 5654 /* XXXGL: is constant ieee80211broadcastaddr a correct choice? */ 5655 5656 ATH_LOCK(sc); 5657 sc->sc_scanning = 1; 5658 sc->sc_syncbeacon = 0; 5659 rfilt = ath_calcrxfilter(sc); 5660 ATH_UNLOCK(sc); 5661 5662 ATH_PCU_LOCK(sc); 5663 ath_hal_setrxfilter(ah, rfilt); 5664 ath_hal_setassocid(ah, ieee80211broadcastaddr, 0); 5665 ATH_PCU_UNLOCK(sc); 5666 5667 DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0\n", 5668 __func__, rfilt, ether_sprintf(ieee80211broadcastaddr)); 5669 } 5670 5671 static void 5672 ath_scan_end(struct ieee80211com *ic) 5673 { 5674 struct ath_softc *sc = ic->ic_softc; 5675 struct ath_hal *ah = sc->sc_ah; 5676 u_int32_t rfilt; 5677 5678 ATH_LOCK(sc); 5679 sc->sc_scanning = 0; 5680 rfilt = ath_calcrxfilter(sc); 5681 ATH_UNLOCK(sc); 5682 5683 ATH_PCU_LOCK(sc); 5684 ath_hal_setrxfilter(ah, rfilt); 5685 ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid); 5686 5687 ath_hal_process_noisefloor(ah); 5688 ATH_PCU_UNLOCK(sc); 5689 5690 DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n", 5691 __func__, rfilt, ether_sprintf(sc->sc_curbssid), 5692 sc->sc_curaid); 5693 } 5694 5695 #ifdef ATH_ENABLE_11N 5696 /* 5697 * For now, just do a channel change. 5698 * 5699 * Later, we'll go through the hard slog of suspending tx/rx, changing rate 5700 * control state and resetting the hardware without dropping frames out 5701 * of the queue. 5702 * 5703 * The unfortunate trouble here is making absolutely sure that the 5704 * channel width change has propagated enough so the hardware 5705 * absolutely isn't handed bogus frames for it's current operating 5706 * mode. (Eg, 40MHz frames in 20MHz mode.) Since TX and RX can and 5707 * does occur in parallel, we need to make certain we've blocked 5708 * any further ongoing TX (and RX, that can cause raw TX) 5709 * before we do this. 5710 */ 5711 static void 5712 ath_update_chw(struct ieee80211com *ic) 5713 { 5714 struct ath_softc *sc = ic->ic_softc; 5715 5716 DPRINTF(sc, ATH_DEBUG_STATE, "%s: called\n", __func__); 5717 ath_set_channel(ic); 5718 } 5719 #endif /* ATH_ENABLE_11N */ 5720 5721 static void 5722 ath_set_channel(struct ieee80211com *ic) 5723 { 5724 struct ath_softc *sc = ic->ic_softc; 5725 5726 ATH_LOCK(sc); 5727 ath_power_set_power_state(sc, HAL_PM_AWAKE); 5728 ATH_UNLOCK(sc); 5729 5730 (void) ath_chan_set(sc, ic->ic_curchan); 5731 /* 5732 * If we are returning to our bss channel then mark state 5733 * so the next recv'd beacon's tsf will be used to sync the 5734 * beacon timers. Note that since we only hear beacons in 5735 * sta/ibss mode this has no effect in other operating modes. 5736 */ 5737 ATH_LOCK(sc); 5738 if (!sc->sc_scanning && ic->ic_curchan == ic->ic_bsschan) 5739 sc->sc_syncbeacon = 1; 5740 ath_power_restore_power_state(sc); 5741 ATH_UNLOCK(sc); 5742 } 5743 5744 /* 5745 * Walk the vap list and check if there any vap's in RUN state. 5746 */ 5747 static int 5748 ath_isanyrunningvaps(struct ieee80211vap *this) 5749 { 5750 struct ieee80211com *ic = this->iv_ic; 5751 struct ieee80211vap *vap; 5752 5753 IEEE80211_LOCK_ASSERT(ic); 5754 5755 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 5756 if (vap != this && vap->iv_state >= IEEE80211_S_RUN) 5757 return 1; 5758 } 5759 return 0; 5760 } 5761 5762 static int 5763 ath_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 5764 { 5765 struct ieee80211com *ic = vap->iv_ic; 5766 struct ath_softc *sc = ic->ic_softc; 5767 struct ath_vap *avp = ATH_VAP(vap); 5768 struct ath_hal *ah = sc->sc_ah; 5769 struct ieee80211_node *ni = NULL; 5770 int i, error, stamode; 5771 u_int32_t rfilt; 5772 int csa_run_transition = 0; 5773 enum ieee80211_state ostate = vap->iv_state; 5774 5775 static const HAL_LED_STATE leds[] = { 5776 HAL_LED_INIT, /* IEEE80211_S_INIT */ 5777 HAL_LED_SCAN, /* IEEE80211_S_SCAN */ 5778 HAL_LED_AUTH, /* IEEE80211_S_AUTH */ 5779 HAL_LED_ASSOC, /* IEEE80211_S_ASSOC */ 5780 HAL_LED_RUN, /* IEEE80211_S_CAC */ 5781 HAL_LED_RUN, /* IEEE80211_S_RUN */ 5782 HAL_LED_RUN, /* IEEE80211_S_CSA */ 5783 HAL_LED_RUN, /* IEEE80211_S_SLEEP */ 5784 }; 5785 5786 DPRINTF(sc, ATH_DEBUG_STATE, "%s: %s -> %s\n", __func__, 5787 ieee80211_state_name[ostate], 5788 ieee80211_state_name[nstate]); 5789 5790 /* 5791 * net80211 _should_ have the comlock asserted at this point. 5792 * There are some comments around the calls to vap->iv_newstate 5793 * which indicate that it (newstate) may end up dropping the 5794 * lock. This and the subsequent lock assert check after newstate 5795 * are an attempt to catch these and figure out how/why. 5796 */ 5797 IEEE80211_LOCK_ASSERT(ic); 5798 5799 /* Before we touch the hardware - wake it up */ 5800 ATH_LOCK(sc); 5801 /* 5802 * If the NIC is in anything other than SLEEP state, 5803 * we need to ensure that self-generated frames are 5804 * set for PWRMGT=0. Otherwise we may end up with 5805 * strange situations. 5806 * 5807 * XXX TODO: is this actually the case? :-) 5808 */ 5809 if (nstate != IEEE80211_S_SLEEP) 5810 ath_power_setselfgen(sc, HAL_PM_AWAKE); 5811 5812 /* 5813 * Now, wake the thing up. 5814 */ 5815 ath_power_set_power_state(sc, HAL_PM_AWAKE); 5816 5817 /* 5818 * And stop the calibration callout whilst we have 5819 * ATH_LOCK held. 5820 */ 5821 callout_stop(&sc->sc_cal_ch); 5822 ATH_UNLOCK(sc); 5823 5824 if (ostate == IEEE80211_S_CSA && nstate == IEEE80211_S_RUN) 5825 csa_run_transition = 1; 5826 5827 ath_hal_setledstate(ah, leds[nstate]); /* set LED */ 5828 5829 if (nstate == IEEE80211_S_SCAN) { 5830 /* 5831 * Scanning: turn off beacon miss and don't beacon. 5832 * Mark beacon state so when we reach RUN state we'll 5833 * [re]setup beacons. Unblock the task q thread so 5834 * deferred interrupt processing is done. 5835 */ 5836 5837 /* Ensure we stay awake during scan */ 5838 ATH_LOCK(sc); 5839 ath_power_setselfgen(sc, HAL_PM_AWAKE); 5840 ath_power_setpower(sc, HAL_PM_AWAKE); 5841 ATH_UNLOCK(sc); 5842 5843 ath_hal_intrset(ah, 5844 sc->sc_imask &~ (HAL_INT_SWBA | HAL_INT_BMISS)); 5845 sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS); 5846 sc->sc_beacons = 0; 5847 taskqueue_unblock(sc->sc_tq); 5848 } 5849 5850 ni = ieee80211_ref_node(vap->iv_bss); 5851 rfilt = ath_calcrxfilter(sc); 5852 stamode = (vap->iv_opmode == IEEE80211_M_STA || 5853 vap->iv_opmode == IEEE80211_M_AHDEMO || 5854 vap->iv_opmode == IEEE80211_M_IBSS); 5855 5856 /* 5857 * XXX Dont need to do this (and others) if we've transitioned 5858 * from SLEEP->RUN. 5859 */ 5860 if (stamode && nstate == IEEE80211_S_RUN) { 5861 sc->sc_curaid = ni->ni_associd; 5862 IEEE80211_ADDR_COPY(sc->sc_curbssid, ni->ni_bssid); 5863 ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid); 5864 } 5865 DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n", 5866 __func__, rfilt, ether_sprintf(sc->sc_curbssid), sc->sc_curaid); 5867 ath_hal_setrxfilter(ah, rfilt); 5868 5869 /* XXX is this to restore keycache on resume? */ 5870 if (vap->iv_opmode != IEEE80211_M_STA && 5871 (vap->iv_flags & IEEE80211_F_PRIVACY)) { 5872 for (i = 0; i < IEEE80211_WEP_NKID; i++) 5873 if (ath_hal_keyisvalid(ah, i)) 5874 ath_hal_keysetmac(ah, i, ni->ni_bssid); 5875 } 5876 5877 /* 5878 * Invoke the parent method to do net80211 work. 5879 */ 5880 error = avp->av_newstate(vap, nstate, arg); 5881 if (error != 0) 5882 goto bad; 5883 5884 /* 5885 * See above: ensure av_newstate() doesn't drop the lock 5886 * on us. 5887 */ 5888 IEEE80211_LOCK_ASSERT(ic); 5889 5890 if (nstate == IEEE80211_S_RUN) { 5891 /* NB: collect bss node again, it may have changed */ 5892 ieee80211_free_node(ni); 5893 ni = ieee80211_ref_node(vap->iv_bss); 5894 5895 DPRINTF(sc, ATH_DEBUG_STATE, 5896 "%s(RUN): iv_flags 0x%08x bintvl %d bssid %s " 5897 "capinfo 0x%04x chan %d\n", __func__, 5898 vap->iv_flags, ni->ni_intval, ether_sprintf(ni->ni_bssid), 5899 ni->ni_capinfo, ieee80211_chan2ieee(ic, ic->ic_curchan)); 5900 5901 switch (vap->iv_opmode) { 5902 #ifdef IEEE80211_SUPPORT_TDMA 5903 case IEEE80211_M_AHDEMO: 5904 if ((vap->iv_caps & IEEE80211_C_TDMA) == 0) 5905 break; 5906 /* fall thru... */ 5907 #endif 5908 case IEEE80211_M_HOSTAP: 5909 case IEEE80211_M_IBSS: 5910 case IEEE80211_M_MBSS: 5911 /* 5912 * Allocate and setup the beacon frame. 5913 * 5914 * Stop any previous beacon DMA. This may be 5915 * necessary, for example, when an ibss merge 5916 * causes reconfiguration; there will be a state 5917 * transition from RUN->RUN that means we may 5918 * be called with beacon transmission active. 5919 */ 5920 ath_hal_stoptxdma(ah, sc->sc_bhalq); 5921 5922 error = ath_beacon_alloc(sc, ni); 5923 if (error != 0) 5924 goto bad; 5925 /* 5926 * If joining an adhoc network defer beacon timer 5927 * configuration to the next beacon frame so we 5928 * have a current TSF to use. Otherwise we're 5929 * starting an ibss/bss so there's no need to delay; 5930 * if this is the first vap moving to RUN state, then 5931 * beacon state needs to be [re]configured. 5932 */ 5933 if (vap->iv_opmode == IEEE80211_M_IBSS && 5934 ni->ni_tstamp.tsf != 0) { 5935 sc->sc_syncbeacon = 1; 5936 } else if (!sc->sc_beacons) { 5937 #ifdef IEEE80211_SUPPORT_TDMA 5938 if (vap->iv_caps & IEEE80211_C_TDMA) 5939 ath_tdma_config(sc, vap); 5940 else 5941 #endif 5942 ath_beacon_config(sc, vap); 5943 sc->sc_beacons = 1; 5944 } 5945 break; 5946 case IEEE80211_M_STA: 5947 /* 5948 * Defer beacon timer configuration to the next 5949 * beacon frame so we have a current TSF to use 5950 * (any TSF collected when scanning is likely old). 5951 * However if it's due to a CSA -> RUN transition, 5952 * force a beacon update so we pick up a lack of 5953 * beacons from an AP in CAC and thus force a 5954 * scan. 5955 * 5956 * And, there's also corner cases here where 5957 * after a scan, the AP may have disappeared. 5958 * In that case, we may not receive an actual 5959 * beacon to update the beacon timer and thus we 5960 * won't get notified of the missing beacons. 5961 */ 5962 if (ostate != IEEE80211_S_RUN && 5963 ostate != IEEE80211_S_SLEEP) { 5964 DPRINTF(sc, ATH_DEBUG_BEACON, 5965 "%s: STA; syncbeacon=1\n", __func__); 5966 sc->sc_syncbeacon = 1; 5967 5968 if (csa_run_transition) 5969 ath_beacon_config(sc, vap); 5970 5971 /* 5972 * PR: kern/175227 5973 * 5974 * Reconfigure beacons during reset; as otherwise 5975 * we won't get the beacon timers reprogrammed 5976 * after a reset and thus we won't pick up a 5977 * beacon miss interrupt. 5978 * 5979 * Hopefully we'll see a beacon before the BMISS 5980 * timer fires (too often), leading to a STA 5981 * disassociation. 5982 */ 5983 sc->sc_beacons = 1; 5984 } 5985 break; 5986 case IEEE80211_M_MONITOR: 5987 /* 5988 * Monitor mode vaps have only INIT->RUN and RUN->RUN 5989 * transitions so we must re-enable interrupts here to 5990 * handle the case of a single monitor mode vap. 5991 */ 5992 ath_hal_intrset(ah, sc->sc_imask); 5993 break; 5994 case IEEE80211_M_WDS: 5995 break; 5996 default: 5997 break; 5998 } 5999 /* 6000 * Let the hal process statistics collected during a 6001 * scan so it can provide calibrated noise floor data. 6002 */ 6003 ath_hal_process_noisefloor(ah); 6004 /* 6005 * Reset rssi stats; maybe not the best place... 6006 */ 6007 sc->sc_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER; 6008 sc->sc_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER; 6009 sc->sc_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER; 6010 6011 /* 6012 * Force awake for RUN mode. 6013 */ 6014 ATH_LOCK(sc); 6015 ath_power_setselfgen(sc, HAL_PM_AWAKE); 6016 ath_power_setpower(sc, HAL_PM_AWAKE); 6017 6018 /* 6019 * Finally, start any timers and the task q thread 6020 * (in case we didn't go through SCAN state). 6021 */ 6022 if (ath_longcalinterval != 0) { 6023 /* start periodic recalibration timer */ 6024 callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc); 6025 } else { 6026 DPRINTF(sc, ATH_DEBUG_CALIBRATE, 6027 "%s: calibration disabled\n", __func__); 6028 } 6029 ATH_UNLOCK(sc); 6030 6031 taskqueue_unblock(sc->sc_tq); 6032 } else if (nstate == IEEE80211_S_INIT) { 6033 /* 6034 * If there are no vaps left in RUN state then 6035 * shutdown host/driver operation: 6036 * o disable interrupts 6037 * o disable the task queue thread 6038 * o mark beacon processing as stopped 6039 */ 6040 if (!ath_isanyrunningvaps(vap)) { 6041 sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS); 6042 /* disable interrupts */ 6043 ath_hal_intrset(ah, sc->sc_imask &~ HAL_INT_GLOBAL); 6044 taskqueue_block(sc->sc_tq); 6045 sc->sc_beacons = 0; 6046 } 6047 #ifdef IEEE80211_SUPPORT_TDMA 6048 ath_hal_setcca(ah, AH_TRUE); 6049 #endif 6050 } else if (nstate == IEEE80211_S_SLEEP) { 6051 /* We're going to sleep, so transition appropriately */ 6052 /* For now, only do this if we're a single STA vap */ 6053 if (sc->sc_nvaps == 1 && 6054 vap->iv_opmode == IEEE80211_M_STA) { 6055 DPRINTF(sc, ATH_DEBUG_BEACON, "%s: syncbeacon=%d\n", __func__, sc->sc_syncbeacon); 6056 ATH_LOCK(sc); 6057 /* 6058 * Always at least set the self-generated 6059 * frame config to set PWRMGT=1. 6060 */ 6061 ath_power_setselfgen(sc, HAL_PM_NETWORK_SLEEP); 6062 6063 /* 6064 * If we're not syncing beacons, transition 6065 * to NETWORK_SLEEP. 6066 * 6067 * We stay awake if syncbeacon > 0 in case 6068 * we need to listen for some beacons otherwise 6069 * our beacon timer config may be wrong. 6070 */ 6071 if (sc->sc_syncbeacon == 0) { 6072 ath_power_setpower(sc, HAL_PM_NETWORK_SLEEP); 6073 } 6074 ATH_UNLOCK(sc); 6075 } 6076 } 6077 bad: 6078 ieee80211_free_node(ni); 6079 6080 /* 6081 * Restore the power state - either to what it was, or 6082 * to network_sleep if it's alright. 6083 */ 6084 ATH_LOCK(sc); 6085 ath_power_restore_power_state(sc); 6086 ATH_UNLOCK(sc); 6087 return error; 6088 } 6089 6090 /* 6091 * Allocate a key cache slot to the station so we can 6092 * setup a mapping from key index to node. The key cache 6093 * slot is needed for managing antenna state and for 6094 * compression when stations do not use crypto. We do 6095 * it uniliaterally here; if crypto is employed this slot 6096 * will be reassigned. 6097 */ 6098 static void 6099 ath_setup_stationkey(struct ieee80211_node *ni) 6100 { 6101 struct ieee80211vap *vap = ni->ni_vap; 6102 struct ath_softc *sc = vap->iv_ic->ic_softc; 6103 ieee80211_keyix keyix, rxkeyix; 6104 6105 /* XXX should take a locked ref to vap->iv_bss */ 6106 if (!ath_key_alloc(vap, &ni->ni_ucastkey, &keyix, &rxkeyix)) { 6107 /* 6108 * Key cache is full; we'll fall back to doing 6109 * the more expensive lookup in software. Note 6110 * this also means no h/w compression. 6111 */ 6112 /* XXX msg+statistic */ 6113 } else { 6114 /* XXX locking? */ 6115 ni->ni_ucastkey.wk_keyix = keyix; 6116 ni->ni_ucastkey.wk_rxkeyix = rxkeyix; 6117 /* NB: must mark device key to get called back on delete */ 6118 ni->ni_ucastkey.wk_flags |= IEEE80211_KEY_DEVKEY; 6119 IEEE80211_ADDR_COPY(ni->ni_ucastkey.wk_macaddr, ni->ni_macaddr); 6120 /* NB: this will create a pass-thru key entry */ 6121 ath_keyset(sc, vap, &ni->ni_ucastkey, vap->iv_bss); 6122 } 6123 } 6124 6125 /* 6126 * Setup driver-specific state for a newly associated node. 6127 * Note that we're called also on a re-associate, the isnew 6128 * param tells us if this is the first time or not. 6129 */ 6130 static void 6131 ath_newassoc(struct ieee80211_node *ni, int isnew) 6132 { 6133 struct ath_node *an = ATH_NODE(ni); 6134 struct ieee80211vap *vap = ni->ni_vap; 6135 struct ath_softc *sc = vap->iv_ic->ic_softc; 6136 const struct ieee80211_txparam *tp = ni->ni_txparms; 6137 6138 an->an_mcastrix = ath_tx_findrix(sc, tp->mcastrate); 6139 an->an_mgmtrix = ath_tx_findrix(sc, tp->mgmtrate); 6140 6141 DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: reassoc; isnew=%d, is_powersave=%d\n", 6142 __func__, 6143 ni->ni_macaddr, 6144 ":", 6145 isnew, 6146 an->an_is_powersave); 6147 6148 ATH_NODE_LOCK(an); 6149 ath_rate_newassoc(sc, an, isnew); 6150 ATH_NODE_UNLOCK(an); 6151 6152 if (isnew && 6153 (vap->iv_flags & IEEE80211_F_PRIVACY) == 0 && sc->sc_hasclrkey && 6154 ni->ni_ucastkey.wk_keyix == IEEE80211_KEYIX_NONE) 6155 ath_setup_stationkey(ni); 6156 6157 /* 6158 * If we're reassociating, make sure that any paused queues 6159 * get unpaused. 6160 * 6161 * Now, we may hvae frames in the hardware queue for this node. 6162 * So if we are reassociating and there are frames in the queue, 6163 * we need to go through the cleanup path to ensure that they're 6164 * marked as non-aggregate. 6165 */ 6166 if (! isnew) { 6167 DPRINTF(sc, ATH_DEBUG_NODE, 6168 "%s: %6D: reassoc; is_powersave=%d\n", 6169 __func__, 6170 ni->ni_macaddr, 6171 ":", 6172 an->an_is_powersave); 6173 6174 /* XXX for now, we can't hold the lock across assoc */ 6175 ath_tx_node_reassoc(sc, an); 6176 6177 /* XXX for now, we can't hold the lock across wakeup */ 6178 if (an->an_is_powersave) 6179 ath_tx_node_wakeup(sc, an); 6180 } 6181 } 6182 6183 static int 6184 ath_setregdomain(struct ieee80211com *ic, struct ieee80211_regdomain *reg, 6185 int nchans, struct ieee80211_channel chans[]) 6186 { 6187 struct ath_softc *sc = ic->ic_softc; 6188 struct ath_hal *ah = sc->sc_ah; 6189 HAL_STATUS status; 6190 6191 DPRINTF(sc, ATH_DEBUG_REGDOMAIN, 6192 "%s: rd %u cc %u location %c%s\n", 6193 __func__, reg->regdomain, reg->country, reg->location, 6194 reg->ecm ? " ecm" : ""); 6195 6196 status = ath_hal_set_channels(ah, chans, nchans, 6197 reg->country, reg->regdomain); 6198 if (status != HAL_OK) { 6199 DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: failed, status %u\n", 6200 __func__, status); 6201 return EINVAL; /* XXX */ 6202 } 6203 6204 return 0; 6205 } 6206 6207 static void 6208 ath_getradiocaps(struct ieee80211com *ic, 6209 int maxchans, int *nchans, struct ieee80211_channel chans[]) 6210 { 6211 struct ath_softc *sc = ic->ic_softc; 6212 struct ath_hal *ah = sc->sc_ah; 6213 6214 DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: use rd %u cc %d\n", 6215 __func__, SKU_DEBUG, CTRY_DEFAULT); 6216 6217 /* XXX check return */ 6218 (void) ath_hal_getchannels(ah, chans, maxchans, nchans, 6219 HAL_MODE_ALL, CTRY_DEFAULT, SKU_DEBUG, AH_TRUE); 6220 6221 } 6222 6223 static int 6224 ath_getchannels(struct ath_softc *sc) 6225 { 6226 struct ieee80211com *ic = &sc->sc_ic; 6227 struct ath_hal *ah = sc->sc_ah; 6228 HAL_STATUS status; 6229 6230 /* 6231 * Collect channel set based on EEPROM contents. 6232 */ 6233 status = ath_hal_init_channels(ah, ic->ic_channels, IEEE80211_CHAN_MAX, 6234 &ic->ic_nchans, HAL_MODE_ALL, CTRY_DEFAULT, SKU_NONE, AH_TRUE); 6235 if (status != HAL_OK) { 6236 device_printf(sc->sc_dev, 6237 "%s: unable to collect channel list from hal, status %d\n", 6238 __func__, status); 6239 return EINVAL; 6240 } 6241 (void) ath_hal_getregdomain(ah, &sc->sc_eerd); 6242 ath_hal_getcountrycode(ah, &sc->sc_eecc); /* NB: cannot fail */ 6243 /* XXX map Atheros sku's to net80211 SKU's */ 6244 /* XXX net80211 types too small */ 6245 ic->ic_regdomain.regdomain = (uint16_t) sc->sc_eerd; 6246 ic->ic_regdomain.country = (uint16_t) sc->sc_eecc; 6247 ic->ic_regdomain.isocc[0] = ' '; /* XXX don't know */ 6248 ic->ic_regdomain.isocc[1] = ' '; 6249 6250 ic->ic_regdomain.ecm = 1; 6251 ic->ic_regdomain.location = 'I'; 6252 6253 DPRINTF(sc, ATH_DEBUG_REGDOMAIN, 6254 "%s: eeprom rd %u cc %u (mapped rd %u cc %u) location %c%s\n", 6255 __func__, sc->sc_eerd, sc->sc_eecc, 6256 ic->ic_regdomain.regdomain, ic->ic_regdomain.country, 6257 ic->ic_regdomain.location, ic->ic_regdomain.ecm ? " ecm" : ""); 6258 return 0; 6259 } 6260 6261 static int 6262 ath_rate_setup(struct ath_softc *sc, u_int mode) 6263 { 6264 struct ath_hal *ah = sc->sc_ah; 6265 const HAL_RATE_TABLE *rt; 6266 6267 switch (mode) { 6268 case IEEE80211_MODE_11A: 6269 rt = ath_hal_getratetable(ah, HAL_MODE_11A); 6270 break; 6271 case IEEE80211_MODE_HALF: 6272 rt = ath_hal_getratetable(ah, HAL_MODE_11A_HALF_RATE); 6273 break; 6274 case IEEE80211_MODE_QUARTER: 6275 rt = ath_hal_getratetable(ah, HAL_MODE_11A_QUARTER_RATE); 6276 break; 6277 case IEEE80211_MODE_11B: 6278 rt = ath_hal_getratetable(ah, HAL_MODE_11B); 6279 break; 6280 case IEEE80211_MODE_11G: 6281 rt = ath_hal_getratetable(ah, HAL_MODE_11G); 6282 break; 6283 case IEEE80211_MODE_TURBO_A: 6284 rt = ath_hal_getratetable(ah, HAL_MODE_108A); 6285 break; 6286 case IEEE80211_MODE_TURBO_G: 6287 rt = ath_hal_getratetable(ah, HAL_MODE_108G); 6288 break; 6289 case IEEE80211_MODE_STURBO_A: 6290 rt = ath_hal_getratetable(ah, HAL_MODE_TURBO); 6291 break; 6292 case IEEE80211_MODE_11NA: 6293 rt = ath_hal_getratetable(ah, HAL_MODE_11NA_HT20); 6294 break; 6295 case IEEE80211_MODE_11NG: 6296 rt = ath_hal_getratetable(ah, HAL_MODE_11NG_HT20); 6297 break; 6298 default: 6299 DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid mode %u\n", 6300 __func__, mode); 6301 return 0; 6302 } 6303 sc->sc_rates[mode] = rt; 6304 return (rt != NULL); 6305 } 6306 6307 static void 6308 ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode) 6309 { 6310 /* NB: on/off times from the Atheros NDIS driver, w/ permission */ 6311 static const struct { 6312 u_int rate; /* tx/rx 802.11 rate */ 6313 u_int16_t timeOn; /* LED on time (ms) */ 6314 u_int16_t timeOff; /* LED off time (ms) */ 6315 } blinkrates[] = { 6316 { 108, 40, 10 }, 6317 { 96, 44, 11 }, 6318 { 72, 50, 13 }, 6319 { 48, 57, 14 }, 6320 { 36, 67, 16 }, 6321 { 24, 80, 20 }, 6322 { 22, 100, 25 }, 6323 { 18, 133, 34 }, 6324 { 12, 160, 40 }, 6325 { 10, 200, 50 }, 6326 { 6, 240, 58 }, 6327 { 4, 267, 66 }, 6328 { 2, 400, 100 }, 6329 { 0, 500, 130 }, 6330 /* XXX half/quarter rates */ 6331 }; 6332 const HAL_RATE_TABLE *rt; 6333 int i, j; 6334 6335 memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap)); 6336 rt = sc->sc_rates[mode]; 6337 KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode)); 6338 for (i = 0; i < rt->rateCount; i++) { 6339 uint8_t ieeerate = rt->info[i].dot11Rate & IEEE80211_RATE_VAL; 6340 if (rt->info[i].phy != IEEE80211_T_HT) 6341 sc->sc_rixmap[ieeerate] = i; 6342 else 6343 sc->sc_rixmap[ieeerate | IEEE80211_RATE_MCS] = i; 6344 } 6345 memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap)); 6346 for (i = 0; i < nitems(sc->sc_hwmap); i++) { 6347 if (i >= rt->rateCount) { 6348 sc->sc_hwmap[i].ledon = (500 * hz) / 1000; 6349 sc->sc_hwmap[i].ledoff = (130 * hz) / 1000; 6350 continue; 6351 } 6352 sc->sc_hwmap[i].ieeerate = 6353 rt->info[i].dot11Rate & IEEE80211_RATE_VAL; 6354 if (rt->info[i].phy == IEEE80211_T_HT) 6355 sc->sc_hwmap[i].ieeerate |= IEEE80211_RATE_MCS; 6356 sc->sc_hwmap[i].txflags = IEEE80211_RADIOTAP_F_DATAPAD; 6357 if (rt->info[i].shortPreamble || 6358 rt->info[i].phy == IEEE80211_T_OFDM) 6359 sc->sc_hwmap[i].txflags |= IEEE80211_RADIOTAP_F_SHORTPRE; 6360 sc->sc_hwmap[i].rxflags = sc->sc_hwmap[i].txflags; 6361 for (j = 0; j < nitems(blinkrates)-1; j++) 6362 if (blinkrates[j].rate == sc->sc_hwmap[i].ieeerate) 6363 break; 6364 /* NB: this uses the last entry if the rate isn't found */ 6365 /* XXX beware of overlow */ 6366 sc->sc_hwmap[i].ledon = (blinkrates[j].timeOn * hz) / 1000; 6367 sc->sc_hwmap[i].ledoff = (blinkrates[j].timeOff * hz) / 1000; 6368 } 6369 sc->sc_currates = rt; 6370 sc->sc_curmode = mode; 6371 /* 6372 * All protection frames are transmited at 2Mb/s for 6373 * 11g, otherwise at 1Mb/s. 6374 */ 6375 if (mode == IEEE80211_MODE_11G) 6376 sc->sc_protrix = ath_tx_findrix(sc, 2*2); 6377 else 6378 sc->sc_protrix = ath_tx_findrix(sc, 2*1); 6379 /* NB: caller is responsible for resetting rate control state */ 6380 } 6381 6382 static void 6383 ath_watchdog(void *arg) 6384 { 6385 struct ath_softc *sc = arg; 6386 struct ieee80211com *ic = &sc->sc_ic; 6387 int do_reset = 0; 6388 6389 ATH_LOCK_ASSERT(sc); 6390 6391 if (sc->sc_wd_timer != 0 && --sc->sc_wd_timer == 0) { 6392 uint32_t hangs; 6393 6394 ath_power_set_power_state(sc, HAL_PM_AWAKE); 6395 6396 if (ath_hal_gethangstate(sc->sc_ah, 0xffff, &hangs) && 6397 hangs != 0) { 6398 device_printf(sc->sc_dev, "%s hang detected (0x%x)\n", 6399 hangs & 0xff ? "bb" : "mac", hangs); 6400 } else 6401 device_printf(sc->sc_dev, "device timeout\n"); 6402 do_reset = 1; 6403 counter_u64_add(ic->ic_oerrors, 1); 6404 sc->sc_stats.ast_watchdog++; 6405 6406 ath_power_restore_power_state(sc); 6407 } 6408 6409 /* 6410 * We can't hold the lock across the ath_reset() call. 6411 * 6412 * And since this routine can't hold a lock and sleep, 6413 * do the reset deferred. 6414 */ 6415 if (do_reset) { 6416 taskqueue_enqueue(sc->sc_tq, &sc->sc_resettask); 6417 } 6418 6419 callout_schedule(&sc->sc_wd_ch, hz); 6420 } 6421 6422 /* 6423 * Fetch the rate control statistics for the given node. 6424 */ 6425 static int 6426 ath_ioctl_ratestats(struct ath_softc *sc, struct ath_rateioctl *rs) 6427 { 6428 struct ath_node *an; 6429 struct ieee80211com *ic = &sc->sc_ic; 6430 struct ieee80211_node *ni; 6431 int error = 0; 6432 6433 /* Perform a lookup on the given node */ 6434 ni = ieee80211_find_node(&ic->ic_sta, rs->is_u.macaddr); 6435 if (ni == NULL) { 6436 error = EINVAL; 6437 goto bad; 6438 } 6439 6440 /* Lock the ath_node */ 6441 an = ATH_NODE(ni); 6442 ATH_NODE_LOCK(an); 6443 6444 /* Fetch the rate control stats for this node */ 6445 error = ath_rate_fetch_node_stats(sc, an, rs); 6446 6447 /* No matter what happens here, just drop through */ 6448 6449 /* Unlock the ath_node */ 6450 ATH_NODE_UNLOCK(an); 6451 6452 /* Unref the node */ 6453 ieee80211_node_decref(ni); 6454 6455 bad: 6456 return (error); 6457 } 6458 6459 #ifdef ATH_DIAGAPI 6460 /* 6461 * Diagnostic interface to the HAL. This is used by various 6462 * tools to do things like retrieve register contents for 6463 * debugging. The mechanism is intentionally opaque so that 6464 * it can change frequently w/o concern for compatiblity. 6465 */ 6466 static int 6467 ath_ioctl_diag(struct ath_softc *sc, struct ath_diag *ad) 6468 { 6469 struct ath_hal *ah = sc->sc_ah; 6470 u_int id = ad->ad_id & ATH_DIAG_ID; 6471 void *indata = NULL; 6472 void *outdata = NULL; 6473 u_int32_t insize = ad->ad_in_size; 6474 u_int32_t outsize = ad->ad_out_size; 6475 int error = 0; 6476 6477 if (ad->ad_id & ATH_DIAG_IN) { 6478 /* 6479 * Copy in data. 6480 */ 6481 indata = malloc(insize, M_TEMP, M_NOWAIT); 6482 if (indata == NULL) { 6483 error = ENOMEM; 6484 goto bad; 6485 } 6486 error = copyin(ad->ad_in_data, indata, insize); 6487 if (error) 6488 goto bad; 6489 } 6490 if (ad->ad_id & ATH_DIAG_DYN) { 6491 /* 6492 * Allocate a buffer for the results (otherwise the HAL 6493 * returns a pointer to a buffer where we can read the 6494 * results). Note that we depend on the HAL leaving this 6495 * pointer for us to use below in reclaiming the buffer; 6496 * may want to be more defensive. 6497 */ 6498 outdata = malloc(outsize, M_TEMP, M_NOWAIT); 6499 if (outdata == NULL) { 6500 error = ENOMEM; 6501 goto bad; 6502 } 6503 } 6504 6505 6506 ATH_LOCK(sc); 6507 if (id != HAL_DIAG_REGS) 6508 ath_power_set_power_state(sc, HAL_PM_AWAKE); 6509 ATH_UNLOCK(sc); 6510 6511 if (ath_hal_getdiagstate(ah, id, indata, insize, &outdata, &outsize)) { 6512 if (outsize < ad->ad_out_size) 6513 ad->ad_out_size = outsize; 6514 if (outdata != NULL) 6515 error = copyout(outdata, ad->ad_out_data, 6516 ad->ad_out_size); 6517 } else { 6518 error = EINVAL; 6519 } 6520 6521 ATH_LOCK(sc); 6522 if (id != HAL_DIAG_REGS) 6523 ath_power_restore_power_state(sc); 6524 ATH_UNLOCK(sc); 6525 6526 bad: 6527 if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL) 6528 free(indata, M_TEMP); 6529 if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL) 6530 free(outdata, M_TEMP); 6531 return error; 6532 } 6533 #endif /* ATH_DIAGAPI */ 6534 6535 static void 6536 ath_parent(struct ieee80211com *ic) 6537 { 6538 struct ath_softc *sc = ic->ic_softc; 6539 int error = EDOOFUS; 6540 6541 ATH_LOCK(sc); 6542 if (ic->ic_nrunning > 0) { 6543 /* 6544 * To avoid rescanning another access point, 6545 * do not call ath_init() here. Instead, 6546 * only reflect promisc mode settings. 6547 */ 6548 if (sc->sc_running) { 6549 ath_power_set_power_state(sc, HAL_PM_AWAKE); 6550 ath_mode_init(sc); 6551 ath_power_restore_power_state(sc); 6552 } else if (!sc->sc_invalid) { 6553 /* 6554 * Beware of being called during attach/detach 6555 * to reset promiscuous mode. In that case we 6556 * will still be marked UP but not RUNNING. 6557 * However trying to re-init the interface 6558 * is the wrong thing to do as we've already 6559 * torn down much of our state. There's 6560 * probably a better way to deal with this. 6561 */ 6562 error = ath_init(sc); 6563 } 6564 } else { 6565 ath_stop(sc); 6566 if (!sc->sc_invalid) 6567 ath_power_setpower(sc, HAL_PM_FULL_SLEEP); 6568 } 6569 ATH_UNLOCK(sc); 6570 6571 if (error == 0) { 6572 #ifdef ATH_TX99_DIAG 6573 if (sc->sc_tx99 != NULL) 6574 sc->sc_tx99->start(sc->sc_tx99); 6575 else 6576 #endif 6577 ieee80211_start_all(ic); 6578 } 6579 } 6580 6581 static int 6582 ath_ioctl(struct ieee80211com *ic, u_long cmd, void *data) 6583 { 6584 struct ifreq *ifr = data; 6585 struct ath_softc *sc = ic->ic_softc; 6586 6587 switch (cmd) { 6588 case SIOCGATHSTATS: { 6589 struct ieee80211vap *vap; 6590 struct ifnet *ifp; 6591 const HAL_RATE_TABLE *rt; 6592 6593 /* NB: embed these numbers to get a consistent view */ 6594 sc->sc_stats.ast_tx_packets = 0; 6595 sc->sc_stats.ast_rx_packets = 0; 6596 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) { 6597 ifp = vap->iv_ifp; 6598 sc->sc_stats.ast_tx_packets += ifp->if_get_counter(ifp, 6599 IFCOUNTER_OPACKETS); 6600 sc->sc_stats.ast_rx_packets += ifp->if_get_counter(ifp, 6601 IFCOUNTER_IPACKETS); 6602 } 6603 sc->sc_stats.ast_tx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgtxrssi); 6604 sc->sc_stats.ast_rx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgrssi); 6605 #ifdef IEEE80211_SUPPORT_TDMA 6606 sc->sc_stats.ast_tdma_tsfadjp = TDMA_AVG(sc->sc_avgtsfdeltap); 6607 sc->sc_stats.ast_tdma_tsfadjm = TDMA_AVG(sc->sc_avgtsfdeltam); 6608 #endif 6609 rt = sc->sc_currates; 6610 sc->sc_stats.ast_tx_rate = 6611 rt->info[sc->sc_txrix].dot11Rate &~ IEEE80211_RATE_BASIC; 6612 if (rt->info[sc->sc_txrix].phy & IEEE80211_T_HT) 6613 sc->sc_stats.ast_tx_rate |= IEEE80211_RATE_MCS; 6614 return copyout(&sc->sc_stats, 6615 ifr->ifr_data, sizeof (sc->sc_stats)); 6616 } 6617 case SIOCGATHAGSTATS: 6618 return copyout(&sc->sc_aggr_stats, 6619 ifr->ifr_data, sizeof (sc->sc_aggr_stats)); 6620 case SIOCZATHSTATS: { 6621 int error; 6622 6623 error = priv_check(curthread, PRIV_DRIVER); 6624 if (error == 0) { 6625 memset(&sc->sc_stats, 0, sizeof(sc->sc_stats)); 6626 memset(&sc->sc_aggr_stats, 0, 6627 sizeof(sc->sc_aggr_stats)); 6628 memset(&sc->sc_intr_stats, 0, 6629 sizeof(sc->sc_intr_stats)); 6630 } 6631 return (error); 6632 } 6633 #ifdef ATH_DIAGAPI 6634 case SIOCGATHDIAG: 6635 return (ath_ioctl_diag(sc, data)); 6636 case SIOCGATHPHYERR: 6637 return (ath_ioctl_phyerr(sc, data)); 6638 #endif 6639 case SIOCGATHSPECTRAL: 6640 return (ath_ioctl_spectral(sc, data)); 6641 case SIOCGATHNODERATESTATS: 6642 return (ath_ioctl_ratestats(sc, data)); 6643 default: 6644 return (ENOTTY); 6645 } 6646 } 6647 6648 /* 6649 * Announce various information on device/driver attach. 6650 */ 6651 static void 6652 ath_announce(struct ath_softc *sc) 6653 { 6654 struct ath_hal *ah = sc->sc_ah; 6655 6656 device_printf(sc->sc_dev, "AR%s mac %d.%d RF%s phy %d.%d\n", 6657 ath_hal_mac_name(ah), ah->ah_macVersion, ah->ah_macRev, 6658 ath_hal_rf_name(ah), ah->ah_phyRev >> 4, ah->ah_phyRev & 0xf); 6659 device_printf(sc->sc_dev, "2GHz radio: 0x%.4x; 5GHz radio: 0x%.4x\n", 6660 ah->ah_analog2GhzRev, ah->ah_analog5GhzRev); 6661 if (bootverbose) { 6662 int i; 6663 for (i = 0; i <= WME_AC_VO; i++) { 6664 struct ath_txq *txq = sc->sc_ac2q[i]; 6665 device_printf(sc->sc_dev, 6666 "Use hw queue %u for %s traffic\n", 6667 txq->axq_qnum, ieee80211_wme_acnames[i]); 6668 } 6669 device_printf(sc->sc_dev, "Use hw queue %u for CAB traffic\n", 6670 sc->sc_cabq->axq_qnum); 6671 device_printf(sc->sc_dev, "Use hw queue %u for beacons\n", 6672 sc->sc_bhalq); 6673 } 6674 if (ath_rxbuf != ATH_RXBUF) 6675 device_printf(sc->sc_dev, "using %u rx buffers\n", ath_rxbuf); 6676 if (ath_txbuf != ATH_TXBUF) 6677 device_printf(sc->sc_dev, "using %u tx buffers\n", ath_txbuf); 6678 if (sc->sc_mcastkey && bootverbose) 6679 device_printf(sc->sc_dev, "using multicast key search\n"); 6680 } 6681 6682 static void 6683 ath_dfs_tasklet(void *p, int npending) 6684 { 6685 struct ath_softc *sc = (struct ath_softc *) p; 6686 struct ieee80211com *ic = &sc->sc_ic; 6687 6688 /* 6689 * If previous processing has found a radar event, 6690 * signal this to the net80211 layer to begin DFS 6691 * processing. 6692 */ 6693 if (ath_dfs_process_radar_event(sc, sc->sc_curchan)) { 6694 /* DFS event found, initiate channel change */ 6695 /* 6696 * XXX doesn't currently tell us whether the event 6697 * XXX was found in the primary or extension 6698 * XXX channel! 6699 */ 6700 IEEE80211_LOCK(ic); 6701 ieee80211_dfs_notify_radar(ic, sc->sc_curchan); 6702 IEEE80211_UNLOCK(ic); 6703 } 6704 } 6705 6706 /* 6707 * Enable/disable power save. This must be called with 6708 * no TX driver locks currently held, so it should only 6709 * be called from the RX path (which doesn't hold any 6710 * TX driver locks.) 6711 */ 6712 static void 6713 ath_node_powersave(struct ieee80211_node *ni, int enable) 6714 { 6715 #ifdef ATH_SW_PSQ 6716 struct ath_node *an = ATH_NODE(ni); 6717 struct ieee80211com *ic = ni->ni_ic; 6718 struct ath_softc *sc = ic->ic_softc; 6719 struct ath_vap *avp = ATH_VAP(ni->ni_vap); 6720 6721 /* XXX and no TXQ locks should be held here */ 6722 6723 DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE, "%s: %6D: enable=%d\n", 6724 __func__, 6725 ni->ni_macaddr, 6726 ":", 6727 !! enable); 6728 6729 /* Suspend or resume software queue handling */ 6730 if (enable) 6731 ath_tx_node_sleep(sc, an); 6732 else 6733 ath_tx_node_wakeup(sc, an); 6734 6735 /* Update net80211 state */ 6736 avp->av_node_ps(ni, enable); 6737 #else 6738 struct ath_vap *avp = ATH_VAP(ni->ni_vap); 6739 6740 /* Update net80211 state */ 6741 avp->av_node_ps(ni, enable); 6742 #endif/* ATH_SW_PSQ */ 6743 } 6744 6745 /* 6746 * Notification from net80211 that the powersave queue state has 6747 * changed. 6748 * 6749 * Since the software queue also may have some frames: 6750 * 6751 * + if the node software queue has frames and the TID state 6752 * is 0, we set the TIM; 6753 * + if the node and the stack are both empty, we clear the TIM bit. 6754 * + If the stack tries to set the bit, always set it. 6755 * + If the stack tries to clear the bit, only clear it if the 6756 * software queue in question is also cleared. 6757 * 6758 * TODO: this is called during node teardown; so let's ensure this 6759 * is all correctly handled and that the TIM bit is cleared. 6760 * It may be that the node flush is called _AFTER_ the net80211 6761 * stack clears the TIM. 6762 * 6763 * Here is the racy part. Since it's possible >1 concurrent, 6764 * overlapping TXes will appear complete with a TX completion in 6765 * another thread, it's possible that the concurrent TIM calls will 6766 * clash. We can't hold the node lock here because setting the 6767 * TIM grabs the net80211 comlock and this may cause a LOR. 6768 * The solution is either to totally serialise _everything_ at 6769 * this point (ie, all TX, completion and any reset/flush go into 6770 * one taskqueue) or a new "ath TIM lock" needs to be created that 6771 * just wraps the driver state change and this call to avp->av_set_tim(). 6772 * 6773 * The same race exists in the net80211 power save queue handling 6774 * as well. Since multiple transmitting threads may queue frames 6775 * into the driver, as well as ps-poll and the driver transmitting 6776 * frames (and thus clearing the psq), it's quite possible that 6777 * a packet entering the PSQ and a ps-poll being handled will 6778 * race, causing the TIM to be cleared and not re-set. 6779 */ 6780 static int 6781 ath_node_set_tim(struct ieee80211_node *ni, int enable) 6782 { 6783 #ifdef ATH_SW_PSQ 6784 struct ieee80211com *ic = ni->ni_ic; 6785 struct ath_softc *sc = ic->ic_softc; 6786 struct ath_node *an = ATH_NODE(ni); 6787 struct ath_vap *avp = ATH_VAP(ni->ni_vap); 6788 int changed = 0; 6789 6790 ATH_TX_LOCK(sc); 6791 an->an_stack_psq = enable; 6792 6793 /* 6794 * This will get called for all operating modes, 6795 * even if avp->av_set_tim is unset. 6796 * It's currently set for hostap/ibss modes; but 6797 * the same infrastructure is used for both STA 6798 * and AP/IBSS node power save. 6799 */ 6800 if (avp->av_set_tim == NULL) { 6801 ATH_TX_UNLOCK(sc); 6802 return (0); 6803 } 6804 6805 /* 6806 * If setting the bit, always set it here. 6807 * If clearing the bit, only clear it if the 6808 * software queue is also empty. 6809 * 6810 * If the node has left power save, just clear the TIM 6811 * bit regardless of the state of the power save queue. 6812 * 6813 * XXX TODO: although atomics are used, it's quite possible 6814 * that a race will occur between this and setting/clearing 6815 * in another thread. TX completion will occur always in 6816 * one thread, however setting/clearing the TIM bit can come 6817 * from a variety of different process contexts! 6818 */ 6819 if (enable && an->an_tim_set == 1) { 6820 DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE, 6821 "%s: %6D: enable=%d, tim_set=1, ignoring\n", 6822 __func__, 6823 ni->ni_macaddr, 6824 ":", 6825 enable); 6826 ATH_TX_UNLOCK(sc); 6827 } else if (enable) { 6828 DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE, 6829 "%s: %6D: enable=%d, enabling TIM\n", 6830 __func__, 6831 ni->ni_macaddr, 6832 ":", 6833 enable); 6834 an->an_tim_set = 1; 6835 ATH_TX_UNLOCK(sc); 6836 changed = avp->av_set_tim(ni, enable); 6837 } else if (an->an_swq_depth == 0) { 6838 /* disable */ 6839 DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE, 6840 "%s: %6D: enable=%d, an_swq_depth == 0, disabling\n", 6841 __func__, 6842 ni->ni_macaddr, 6843 ":", 6844 enable); 6845 an->an_tim_set = 0; 6846 ATH_TX_UNLOCK(sc); 6847 changed = avp->av_set_tim(ni, enable); 6848 } else if (! an->an_is_powersave) { 6849 /* 6850 * disable regardless; the node isn't in powersave now 6851 */ 6852 DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE, 6853 "%s: %6D: enable=%d, an_pwrsave=0, disabling\n", 6854 __func__, 6855 ni->ni_macaddr, 6856 ":", 6857 enable); 6858 an->an_tim_set = 0; 6859 ATH_TX_UNLOCK(sc); 6860 changed = avp->av_set_tim(ni, enable); 6861 } else { 6862 /* 6863 * psq disable, node is currently in powersave, node 6864 * software queue isn't empty, so don't clear the TIM bit 6865 * for now. 6866 */ 6867 ATH_TX_UNLOCK(sc); 6868 DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE, 6869 "%s: %6D: enable=%d, an_swq_depth > 0, ignoring\n", 6870 __func__, 6871 ni->ni_macaddr, 6872 ":", 6873 enable); 6874 changed = 0; 6875 } 6876 6877 return (changed); 6878 #else 6879 struct ath_vap *avp = ATH_VAP(ni->ni_vap); 6880 6881 /* 6882 * Some operating modes don't set av_set_tim(), so don't 6883 * update it here. 6884 */ 6885 if (avp->av_set_tim == NULL) 6886 return (0); 6887 6888 return (avp->av_set_tim(ni, enable)); 6889 #endif /* ATH_SW_PSQ */ 6890 } 6891 6892 /* 6893 * Set or update the TIM from the software queue. 6894 * 6895 * Check the software queue depth before attempting to do lock 6896 * anything; that avoids trying to obtain the lock. Then, 6897 * re-check afterwards to ensure nothing has changed in the 6898 * meantime. 6899 * 6900 * set: This is designed to be called from the TX path, after 6901 * a frame has been queued; to see if the swq > 0. 6902 * 6903 * clear: This is designed to be called from the buffer completion point 6904 * (right now it's ath_tx_default_comp()) where the state of 6905 * a software queue has changed. 6906 * 6907 * It makes sense to place it at buffer free / completion rather 6908 * than after each software queue operation, as there's no real 6909 * point in churning the TIM bit as the last frames in the software 6910 * queue are transmitted. If they fail and we retry them, we'd 6911 * just be setting the TIM bit again anyway. 6912 */ 6913 void 6914 ath_tx_update_tim(struct ath_softc *sc, struct ieee80211_node *ni, 6915 int enable) 6916 { 6917 #ifdef ATH_SW_PSQ 6918 struct ath_node *an; 6919 struct ath_vap *avp; 6920 6921 /* Don't do this for broadcast/etc frames */ 6922 if (ni == NULL) 6923 return; 6924 6925 an = ATH_NODE(ni); 6926 avp = ATH_VAP(ni->ni_vap); 6927 6928 /* 6929 * And for operating modes without the TIM handler set, let's 6930 * just skip those. 6931 */ 6932 if (avp->av_set_tim == NULL) 6933 return; 6934 6935 ATH_TX_LOCK_ASSERT(sc); 6936 6937 if (enable) { 6938 if (an->an_is_powersave && 6939 an->an_tim_set == 0 && 6940 an->an_swq_depth != 0) { 6941 DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE, 6942 "%s: %6D: swq_depth>0, tim_set=0, set!\n", 6943 __func__, 6944 ni->ni_macaddr, 6945 ":"); 6946 an->an_tim_set = 1; 6947 (void) avp->av_set_tim(ni, 1); 6948 } 6949 } else { 6950 /* 6951 * Don't bother grabbing the lock unless the queue is empty. 6952 */ 6953 if (an->an_swq_depth != 0) 6954 return; 6955 6956 if (an->an_is_powersave && 6957 an->an_stack_psq == 0 && 6958 an->an_tim_set == 1 && 6959 an->an_swq_depth == 0) { 6960 DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE, 6961 "%s: %6D: swq_depth=0, tim_set=1, psq_set=0," 6962 " clear!\n", 6963 __func__, 6964 ni->ni_macaddr, 6965 ":"); 6966 an->an_tim_set = 0; 6967 (void) avp->av_set_tim(ni, 0); 6968 } 6969 } 6970 #else 6971 return; 6972 #endif /* ATH_SW_PSQ */ 6973 } 6974 6975 /* 6976 * Received a ps-poll frame from net80211. 6977 * 6978 * Here we get a chance to serve out a software-queued frame ourselves 6979 * before we punt it to net80211 to transmit us one itself - either 6980 * because there's traffic in the net80211 psq, or a NULL frame to 6981 * indicate there's nothing else. 6982 */ 6983 static void 6984 ath_node_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m) 6985 { 6986 #ifdef ATH_SW_PSQ 6987 struct ath_node *an; 6988 struct ath_vap *avp; 6989 struct ieee80211com *ic = ni->ni_ic; 6990 struct ath_softc *sc = ic->ic_softc; 6991 int tid; 6992 6993 /* Just paranoia */ 6994 if (ni == NULL) 6995 return; 6996 6997 /* 6998 * Unassociated (temporary node) station. 6999 */ 7000 if (ni->ni_associd == 0) 7001 return; 7002 7003 /* 7004 * We do have an active node, so let's begin looking into it. 7005 */ 7006 an = ATH_NODE(ni); 7007 avp = ATH_VAP(ni->ni_vap); 7008 7009 /* 7010 * For now, we just call the original ps-poll method. 7011 * Once we're ready to flip this on: 7012 * 7013 * + Set leak to 1, as no matter what we're going to have 7014 * to send a frame; 7015 * + Check the software queue and if there's something in it, 7016 * schedule the highest TID thas has traffic from this node. 7017 * Then make sure we schedule the software scheduler to 7018 * run so it picks up said frame. 7019 * 7020 * That way whatever happens, we'll at least send _a_ frame 7021 * to the given node. 7022 * 7023 * Again, yes, it's crappy QoS if the node has multiple 7024 * TIDs worth of traffic - but let's get it working first 7025 * before we optimise it. 7026 * 7027 * Also yes, there's definitely latency here - we're not 7028 * direct dispatching to the hardware in this path (and 7029 * we're likely being called from the packet receive path, 7030 * so going back into TX may be a little hairy!) but again 7031 * I'd like to get this working first before optimising 7032 * turn-around time. 7033 */ 7034 7035 ATH_TX_LOCK(sc); 7036 7037 /* 7038 * Legacy - we're called and the node isn't asleep. 7039 * Immediately punt. 7040 */ 7041 if (! an->an_is_powersave) { 7042 DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE, 7043 "%s: %6D: not in powersave?\n", 7044 __func__, 7045 ni->ni_macaddr, 7046 ":"); 7047 ATH_TX_UNLOCK(sc); 7048 avp->av_recv_pspoll(ni, m); 7049 return; 7050 } 7051 7052 /* 7053 * We're in powersave. 7054 * 7055 * Leak a frame. 7056 */ 7057 an->an_leak_count = 1; 7058 7059 /* 7060 * Now, if there's no frames in the node, just punt to 7061 * recv_pspoll. 7062 * 7063 * Don't bother checking if the TIM bit is set, we really 7064 * only care if there are any frames here! 7065 */ 7066 if (an->an_swq_depth == 0) { 7067 ATH_TX_UNLOCK(sc); 7068 DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE, 7069 "%s: %6D: SWQ empty; punting to net80211\n", 7070 __func__, 7071 ni->ni_macaddr, 7072 ":"); 7073 avp->av_recv_pspoll(ni, m); 7074 return; 7075 } 7076 7077 /* 7078 * Ok, let's schedule the highest TID that has traffic 7079 * and then schedule something. 7080 */ 7081 for (tid = IEEE80211_TID_SIZE - 1; tid >= 0; tid--) { 7082 struct ath_tid *atid = &an->an_tid[tid]; 7083 /* 7084 * No frames? Skip. 7085 */ 7086 if (atid->axq_depth == 0) 7087 continue; 7088 ath_tx_tid_sched(sc, atid); 7089 /* 7090 * XXX we could do a direct call to the TXQ 7091 * scheduler code here to optimise latency 7092 * at the expense of a REALLY deep callstack. 7093 */ 7094 ATH_TX_UNLOCK(sc); 7095 taskqueue_enqueue(sc->sc_tq, &sc->sc_txqtask); 7096 DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE, 7097 "%s: %6D: leaking frame to TID %d\n", 7098 __func__, 7099 ni->ni_macaddr, 7100 ":", 7101 tid); 7102 return; 7103 } 7104 7105 ATH_TX_UNLOCK(sc); 7106 7107 /* 7108 * XXX nothing in the TIDs at this point? Eek. 7109 */ 7110 DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE, 7111 "%s: %6D: TIDs empty, but ath_node showed traffic?!\n", 7112 __func__, 7113 ni->ni_macaddr, 7114 ":"); 7115 avp->av_recv_pspoll(ni, m); 7116 #else 7117 avp->av_recv_pspoll(ni, m); 7118 #endif /* ATH_SW_PSQ */ 7119 } 7120 7121 MODULE_VERSION(if_ath, 1); 7122 MODULE_DEPEND(if_ath, wlan, 1, 1, 1); /* 802.11 media layer */ 7123 #if defined(IEEE80211_ALQ) || defined(AH_DEBUG_ALQ) || defined(ATH_DEBUG_ALQ) 7124 MODULE_DEPEND(if_ath, alq, 1, 1, 1); 7125 #endif 7126