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