1 /* $OpenBSD: athn.c,v 1.104 2019/05/17 01:05:20 kevlo Exp $ */ 2 3 /*- 4 * Copyright (c) 2009 Damien Bergamini <damien.bergamini@free.fr> 5 * Copyright (c) 2008-2010 Atheros Communications Inc. 6 * 7 * Permission to use, copy, modify, and/or distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 /* 21 * Driver for Atheros 802.11a/g/n chipsets. 22 */ 23 24 #include "athn_usb.h" 25 #include "bpfilter.h" 26 27 #include <sys/param.h> 28 #include <sys/sockio.h> 29 #include <sys/mbuf.h> 30 #include <sys/kernel.h> 31 #include <sys/socket.h> 32 #include <sys/systm.h> 33 #include <sys/malloc.h> 34 #include <sys/queue.h> 35 #include <sys/timeout.h> 36 #include <sys/conf.h> 37 #include <sys/device.h> 38 #include <sys/stdint.h> /* uintptr_t */ 39 #include <sys/endian.h> 40 41 #include <machine/bus.h> 42 #include <machine/intr.h> 43 44 #if NBPFILTER > 0 45 #include <net/bpf.h> 46 #endif 47 #include <net/if.h> 48 #include <net/if_dl.h> 49 #include <net/if_media.h> 50 51 #include <netinet/in.h> 52 #include <netinet/if_ether.h> 53 54 #include <net80211/ieee80211_var.h> 55 #include <net80211/ieee80211_amrr.h> 56 #include <net80211/ieee80211_mira.h> 57 #include <net80211/ieee80211_radiotap.h> 58 59 #include <dev/ic/athnreg.h> 60 #include <dev/ic/athnvar.h> 61 62 #ifdef ATHN_DEBUG 63 int athn_debug = 0; 64 #endif 65 66 void athn_radiotap_attach(struct athn_softc *); 67 void athn_get_chanlist(struct athn_softc *); 68 const char * athn_get_mac_name(struct athn_softc *); 69 const char * athn_get_rf_name(struct athn_softc *); 70 void athn_led_init(struct athn_softc *); 71 void athn_set_led(struct athn_softc *, int); 72 void athn_btcoex_init(struct athn_softc *); 73 void athn_btcoex_enable(struct athn_softc *); 74 void athn_btcoex_disable(struct athn_softc *); 75 void athn_set_rxfilter(struct athn_softc *, uint32_t); 76 void athn_get_chipid(struct athn_softc *); 77 int athn_reset_power_on(struct athn_softc *); 78 int athn_reset(struct athn_softc *, int); 79 void athn_init_pll(struct athn_softc *, 80 const struct ieee80211_channel *); 81 int athn_set_power_awake(struct athn_softc *); 82 void athn_set_power_sleep(struct athn_softc *); 83 void athn_write_serdes(struct athn_softc *, 84 const struct athn_serdes *); 85 void athn_config_pcie(struct athn_softc *); 86 void athn_config_nonpcie(struct athn_softc *); 87 int athn_set_chan(struct athn_softc *, struct ieee80211_channel *, 88 struct ieee80211_channel *); 89 int athn_switch_chan(struct athn_softc *, 90 struct ieee80211_channel *, struct ieee80211_channel *); 91 void athn_get_delta_slope(uint32_t, uint32_t *, uint32_t *); 92 void athn_reset_key(struct athn_softc *, int); 93 int athn_set_key(struct ieee80211com *, struct ieee80211_node *, 94 struct ieee80211_key *); 95 void athn_delete_key(struct ieee80211com *, struct ieee80211_node *, 96 struct ieee80211_key *); 97 void athn_iter_calib(void *, struct ieee80211_node *); 98 int athn_cap_noisefloor(struct athn_softc *, int); 99 int athn_nf_hist_mid(int *, int); 100 void athn_filter_noisefloor(struct athn_softc *); 101 void athn_start_noisefloor_calib(struct athn_softc *, int); 102 void athn_calib_to(void *); 103 int athn_init_calib(struct athn_softc *, 104 struct ieee80211_channel *, struct ieee80211_channel *); 105 uint8_t athn_chan2fbin(struct ieee80211_channel *); 106 int athn_interpolate(int, int, int, int, int); 107 void athn_get_pier_ival(uint8_t, const uint8_t *, int, int *, 108 int *); 109 void athn_init_dma(struct athn_softc *); 110 void athn_rx_start(struct athn_softc *); 111 void athn_inc_tx_trigger_level(struct athn_softc *); 112 int athn_stop_rx_dma(struct athn_softc *); 113 int athn_rx_abort(struct athn_softc *); 114 void athn_tx_reclaim(struct athn_softc *, int); 115 int athn_tx_pending(struct athn_softc *, int); 116 void athn_stop_tx_dma(struct athn_softc *, int); 117 int athn_txtime(struct athn_softc *, int, int, u_int); 118 void athn_set_sta_timers(struct athn_softc *); 119 void athn_set_hostap_timers(struct athn_softc *); 120 void athn_set_opmode(struct athn_softc *); 121 void athn_set_bss(struct athn_softc *, struct ieee80211_node *); 122 void athn_enable_interrupts(struct athn_softc *); 123 void athn_disable_interrupts(struct athn_softc *); 124 void athn_init_qos(struct athn_softc *); 125 int athn_hw_reset(struct athn_softc *, struct ieee80211_channel *, 126 struct ieee80211_channel *, int); 127 struct ieee80211_node *athn_node_alloc(struct ieee80211com *); 128 void athn_newassoc(struct ieee80211com *, struct ieee80211_node *, 129 int); 130 void athn_node_leave(struct ieee80211com *, struct ieee80211_node *); 131 int athn_media_change(struct ifnet *); 132 void athn_next_scan(void *); 133 void athn_iter_mira_delete(void *, struct ieee80211_node *); 134 void athn_delete_mira_nodes(struct athn_softc *); 135 int athn_newstate(struct ieee80211com *, enum ieee80211_state, 136 int); 137 void athn_updateedca(struct ieee80211com *); 138 int athn_clock_rate(struct athn_softc *); 139 int athn_chan_sifs(struct ieee80211_channel *); 140 void athn_setsifs(struct athn_softc *); 141 int athn_acktimeout(struct ieee80211_channel *, int); 142 void athn_setacktimeout(struct athn_softc *, 143 struct ieee80211_channel *, int); 144 void athn_setctstimeout(struct athn_softc *, 145 struct ieee80211_channel *, int); 146 void athn_setclockrate(struct athn_softc *); 147 void athn_updateslot(struct ieee80211com *); 148 void athn_start(struct ifnet *); 149 void athn_watchdog(struct ifnet *); 150 void athn_set_multi(struct athn_softc *); 151 int athn_ioctl(struct ifnet *, u_long, caddr_t); 152 int athn_init(struct ifnet *); 153 void athn_stop(struct ifnet *, int); 154 void athn_init_tx_queues(struct athn_softc *); 155 int32_t athn_ani_get_rssi(struct athn_softc *); 156 void athn_ani_ofdm_err_trigger(struct athn_softc *); 157 void athn_ani_cck_err_trigger(struct athn_softc *); 158 void athn_ani_lower_immunity(struct athn_softc *); 159 void athn_ani_restart(struct athn_softc *); 160 void athn_ani_monitor(struct athn_softc *); 161 162 /* Extern functions. */ 163 int ar5416_attach(struct athn_softc *); 164 int ar9280_attach(struct athn_softc *); 165 int ar9285_attach(struct athn_softc *); 166 int ar9287_attach(struct athn_softc *); 167 int ar9380_attach(struct athn_softc *); 168 int ar5416_init_calib(struct athn_softc *, 169 struct ieee80211_channel *, struct ieee80211_channel *); 170 int ar9285_init_calib(struct athn_softc *, 171 struct ieee80211_channel *, struct ieee80211_channel *); 172 int ar9003_init_calib(struct athn_softc *); 173 void ar9285_pa_calib(struct athn_softc *); 174 void ar9271_pa_calib(struct athn_softc *); 175 void ar9287_1_3_enable_async_fifo(struct athn_softc *); 176 void ar9287_1_3_setup_async_fifo(struct athn_softc *); 177 void ar9003_reset_txsring(struct athn_softc *); 178 179 struct cfdriver athn_cd = { 180 NULL, "athn", DV_IFNET 181 }; 182 183 int 184 athn_attach(struct athn_softc *sc) 185 { 186 struct ieee80211com *ic = &sc->sc_ic; 187 struct ifnet *ifp = &ic->ic_if; 188 int error; 189 190 /* Read hardware revision. */ 191 athn_get_chipid(sc); 192 193 if ((error = athn_reset_power_on(sc)) != 0) { 194 printf("%s: could not reset chip\n", sc->sc_dev.dv_xname); 195 return (error); 196 } 197 198 if ((error = athn_set_power_awake(sc)) != 0) { 199 printf("%s: could not wakeup chip\n", sc->sc_dev.dv_xname); 200 return (error); 201 } 202 203 if (AR_SREV_5416(sc) || AR_SREV_9160(sc)) 204 error = ar5416_attach(sc); 205 else if (AR_SREV_9280(sc)) 206 error = ar9280_attach(sc); 207 else if (AR_SREV_9285(sc)) 208 error = ar9285_attach(sc); 209 #if NATHN_USB > 0 210 else if (AR_SREV_9271(sc)) 211 error = ar9285_attach(sc); 212 #endif 213 else if (AR_SREV_9287(sc)) 214 error = ar9287_attach(sc); 215 else if (AR_SREV_9380(sc) || AR_SREV_9485(sc)) 216 error = ar9380_attach(sc); 217 else 218 error = ENOTSUP; 219 if (error != 0) { 220 printf("%s: could not attach chip\n", sc->sc_dev.dv_xname); 221 return (error); 222 } 223 224 /* We can put the chip in sleep state now. */ 225 athn_set_power_sleep(sc); 226 227 if (!(sc->flags & ATHN_FLAG_USB)) { 228 error = sc->ops.dma_alloc(sc); 229 if (error != 0) { 230 printf("%s: could not allocate DMA resources\n", 231 sc->sc_dev.dv_xname); 232 return (error); 233 } 234 /* Steal one Tx buffer for beacons. */ 235 sc->bcnbuf = SIMPLEQ_FIRST(&sc->txbufs); 236 SIMPLEQ_REMOVE_HEAD(&sc->txbufs, bf_list); 237 } 238 239 if (sc->flags & ATHN_FLAG_RFSILENT) { 240 DPRINTF(("found RF switch connected to GPIO pin %d\n", 241 sc->rfsilent_pin)); 242 } 243 DPRINTF(("%d key cache entries\n", sc->kc_entries)); 244 /* 245 * In HostAP mode, the number of STAs that we can handle is 246 * limited by the number of entries in the HW key cache. 247 * TKIP keys consume 2 entries in the cache. 248 */ 249 ic->ic_max_nnodes = (sc->kc_entries / 2) - IEEE80211_WEP_NKID; 250 if (ic->ic_max_nnodes > IEEE80211_CACHE_SIZE) 251 ic->ic_max_nnodes = IEEE80211_CACHE_SIZE; 252 253 DPRINTF(("using %s loop power control\n", 254 (sc->flags & ATHN_FLAG_OLPC) ? "open" : "closed")); 255 256 DPRINTF(("txchainmask=0x%x rxchainmask=0x%x\n", 257 sc->txchainmask, sc->rxchainmask)); 258 /* Count the number of bits set (in lowest 3 bits). */ 259 sc->ntxchains = 260 ((sc->txchainmask >> 2) & 1) + 261 ((sc->txchainmask >> 1) & 1) + 262 ((sc->txchainmask >> 0) & 1); 263 sc->nrxchains = 264 ((sc->rxchainmask >> 2) & 1) + 265 ((sc->rxchainmask >> 1) & 1) + 266 ((sc->rxchainmask >> 0) & 1); 267 268 if (AR_SINGLE_CHIP(sc)) { 269 printf("%s: %s rev %d (%dT%dR), ROM rev %d, address %s\n", 270 sc->sc_dev.dv_xname, athn_get_mac_name(sc), sc->mac_rev, 271 sc->ntxchains, sc->nrxchains, sc->eep_rev, 272 ether_sprintf(ic->ic_myaddr)); 273 } else { 274 printf("%s: MAC %s rev %d, RF %s (%dT%dR), ROM rev %d, " 275 "address %s\n", 276 sc->sc_dev.dv_xname, athn_get_mac_name(sc), sc->mac_rev, 277 athn_get_rf_name(sc), sc->ntxchains, sc->nrxchains, 278 sc->eep_rev, ether_sprintf(ic->ic_myaddr)); 279 } 280 281 timeout_set(&sc->scan_to, athn_next_scan, sc); 282 timeout_set(&sc->calib_to, athn_calib_to, sc); 283 284 sc->amrr.amrr_min_success_threshold = 1; 285 sc->amrr.amrr_max_success_threshold = 15; 286 287 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 288 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */ 289 ic->ic_state = IEEE80211_S_INIT; 290 291 /* Set device capabilities. */ 292 ic->ic_caps = 293 IEEE80211_C_WEP | /* WEP. */ 294 IEEE80211_C_RSN | /* WPA/RSN. */ 295 #ifndef IEEE80211_STA_ONLY 296 IEEE80211_C_HOSTAP | /* Host AP mode supported. */ 297 IEEE80211_C_APPMGT | /* Host AP power saving supported. */ 298 #endif 299 IEEE80211_C_MONITOR | /* Monitor mode supported. */ 300 IEEE80211_C_SHSLOT | /* Short slot time supported. */ 301 IEEE80211_C_SHPREAMBLE | /* Short preamble supported. */ 302 IEEE80211_C_PMGT; /* Power saving supported. */ 303 304 if (sc->flags & ATHN_FLAG_11N) { 305 int i, ntxstreams, nrxstreams; 306 307 /* Set HT capabilities. */ 308 ic->ic_htcaps = (IEEE80211_HTCAP_SMPS_DIS << 309 IEEE80211_HTCAP_SMPS_SHIFT); 310 #ifdef notyet 311 ic->ic_htcaps |= IEEE80211_HTCAP_CBW20_40 | 312 IEEE80211_HTCAP_SGI40 | 313 IEEE80211_HTCAP_DSSSCCK40; 314 #endif 315 ic->ic_htxcaps = 0; 316 #ifdef notyet 317 if (AR_SREV_9271(sc) || AR_SREV_9287_10_OR_LATER(sc)) 318 ic->ic_htcaps |= IEEE80211_HTCAP_SGI20; 319 if (AR_SREV_9380_10_OR_LATER(sc)) 320 ic->ic_htcaps |= IEEE80211_HTCAP_LDPC; 321 if (AR_SREV_9280_10_OR_LATER(sc)) { 322 ic->ic_htcaps |= IEEE80211_HTCAP_TXSTBC; 323 ic->ic_htcaps |= 1 << IEEE80211_HTCAP_RXSTBC_SHIFT; 324 } 325 #endif 326 ntxstreams = sc->ntxchains; 327 nrxstreams = sc->nrxchains; 328 if (!AR_SREV_9380_10_OR_LATER(sc)) { 329 ntxstreams = MIN(ntxstreams, 2); 330 nrxstreams = MIN(nrxstreams, 2); 331 } 332 /* Set supported HT rates. */ 333 for (i = 0; i < nrxstreams; i++) 334 ic->ic_sup_mcs[i] = 0xff; 335 ic->ic_tx_mcs_set |= IEEE80211_TX_MCS_SET_DEFINED; 336 if (ntxstreams != nrxstreams) { 337 ic->ic_tx_mcs_set |= IEEE80211_TX_RX_MCS_NOT_EQUAL; 338 ic->ic_tx_mcs_set |= (ntxstreams - 1) << 2; 339 } 340 } 341 342 /* Set supported rates. */ 343 if (sc->flags & ATHN_FLAG_11G) { 344 ic->ic_sup_rates[IEEE80211_MODE_11B] = 345 ieee80211_std_rateset_11b; 346 ic->ic_sup_rates[IEEE80211_MODE_11G] = 347 ieee80211_std_rateset_11g; 348 } 349 if (sc->flags & ATHN_FLAG_11A) { 350 ic->ic_sup_rates[IEEE80211_MODE_11A] = 351 ieee80211_std_rateset_11a; 352 } 353 354 /* Get the list of authorized/supported channels. */ 355 athn_get_chanlist(sc); 356 357 /* IBSS channel undefined for now. */ 358 ic->ic_ibss_chan = &ic->ic_channels[0]; 359 360 ifp->if_softc = sc; 361 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 362 ifp->if_ioctl = athn_ioctl; 363 ifp->if_start = athn_start; 364 ifp->if_watchdog = athn_watchdog; 365 memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ); 366 367 if_attach(ifp); 368 ieee80211_ifattach(ifp); 369 ic->ic_node_alloc = athn_node_alloc; 370 #ifndef IEEE80211_STA_ONLY 371 ic->ic_node_leave = athn_node_leave; 372 #endif 373 ic->ic_newassoc = athn_newassoc; 374 ic->ic_updateslot = athn_updateslot; 375 ic->ic_updateedca = athn_updateedca; 376 #ifdef notyet 377 ic->ic_set_key = athn_set_key; 378 ic->ic_delete_key = athn_delete_key; 379 #endif 380 381 /* Override 802.11 state transition machine. */ 382 sc->sc_newstate = ic->ic_newstate; 383 ic->ic_newstate = athn_newstate; 384 ieee80211_media_init(ifp, athn_media_change, ieee80211_media_status); 385 386 #if NBPFILTER > 0 387 athn_radiotap_attach(sc); 388 #endif 389 390 return (0); 391 } 392 393 void 394 athn_detach(struct athn_softc *sc) 395 { 396 struct ifnet *ifp = &sc->sc_ic.ic_if; 397 struct ieee80211com *ic = &sc->sc_ic; 398 int qid; 399 400 timeout_del(&sc->scan_to); 401 timeout_del(&sc->calib_to); 402 if (ic->ic_flags & IEEE80211_F_HTON) 403 athn_delete_mira_nodes(sc); 404 405 if (!(sc->flags & ATHN_FLAG_USB)) { 406 for (qid = 0; qid < ATHN_QID_COUNT; qid++) 407 athn_tx_reclaim(sc, qid); 408 409 /* Free Tx/Rx DMA resources. */ 410 sc->ops.dma_free(sc); 411 } 412 /* Free ROM copy. */ 413 if (sc->eep != NULL) 414 free(sc->eep, M_DEVBUF, 0); 415 416 ieee80211_ifdetach(ifp); 417 if_detach(ifp); 418 } 419 420 #if NBPFILTER > 0 421 /* 422 * Attach the interface to 802.11 radiotap. 423 */ 424 void 425 athn_radiotap_attach(struct athn_softc *sc) 426 { 427 bpfattach(&sc->sc_drvbpf, &sc->sc_ic.ic_if, DLT_IEEE802_11_RADIO, 428 sizeof(struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN); 429 430 sc->sc_rxtap_len = sizeof(sc->sc_rxtapu); 431 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len); 432 sc->sc_rxtap.wr_ihdr.it_present = htole32(ATHN_RX_RADIOTAP_PRESENT); 433 434 sc->sc_txtap_len = sizeof(sc->sc_txtapu); 435 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len); 436 sc->sc_txtap.wt_ihdr.it_present = htole32(ATHN_TX_RADIOTAP_PRESENT); 437 } 438 #endif 439 440 void 441 athn_get_chanlist(struct athn_softc *sc) 442 { 443 struct ieee80211com *ic = &sc->sc_ic; 444 uint8_t chan; 445 int i; 446 447 if (sc->flags & ATHN_FLAG_11G) { 448 for (i = 1; i <= 14; i++) { 449 chan = i; 450 ic->ic_channels[chan].ic_freq = 451 ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ); 452 ic->ic_channels[chan].ic_flags = 453 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM | 454 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ; 455 if (sc->flags & ATHN_FLAG_11N) 456 ic->ic_channels[chan].ic_flags |= 457 IEEE80211_CHAN_HT; 458 } 459 } 460 if (sc->flags & ATHN_FLAG_11A) { 461 for (i = 0; i < nitems(athn_5ghz_chans); i++) { 462 chan = athn_5ghz_chans[i]; 463 ic->ic_channels[chan].ic_freq = 464 ieee80211_ieee2mhz(chan, IEEE80211_CHAN_5GHZ); 465 ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_A; 466 if (sc->flags & ATHN_FLAG_11N) 467 ic->ic_channels[chan].ic_flags |= 468 IEEE80211_CHAN_HT; 469 } 470 } 471 } 472 473 void 474 athn_rx_start(struct athn_softc *sc) 475 { 476 struct ieee80211com *ic = &sc->sc_ic; 477 uint32_t rfilt; 478 479 /* Setup Rx DMA descriptors. */ 480 sc->ops.rx_enable(sc); 481 482 /* Set Rx filter. */ 483 rfilt = AR_RX_FILTER_UCAST | AR_RX_FILTER_BCAST | AR_RX_FILTER_MCAST; 484 /* Want Compressed Block Ack Requests. */ 485 rfilt |= AR_RX_FILTER_COMPR_BAR; 486 rfilt |= AR_RX_FILTER_BEACON; 487 if (ic->ic_opmode != IEEE80211_M_STA) { 488 rfilt |= AR_RX_FILTER_PROBEREQ; 489 if (ic->ic_opmode == IEEE80211_M_MONITOR) 490 rfilt |= AR_RX_FILTER_PROM; 491 #ifndef IEEE80211_STA_ONLY 492 if (AR_SREV_9280_10_OR_LATER(sc) && 493 ic->ic_opmode == IEEE80211_M_HOSTAP) 494 rfilt |= AR_RX_FILTER_PSPOLL; 495 #endif 496 } 497 athn_set_rxfilter(sc, rfilt); 498 499 /* Set BSSID mask. */ 500 AR_WRITE(sc, AR_BSSMSKL, 0xffffffff); 501 AR_WRITE(sc, AR_BSSMSKU, 0xffff); 502 503 athn_set_opmode(sc); 504 505 /* Set multicast filter. */ 506 AR_WRITE(sc, AR_MCAST_FIL0, 0xffffffff); 507 AR_WRITE(sc, AR_MCAST_FIL1, 0xffffffff); 508 509 AR_WRITE(sc, AR_FILT_OFDM, 0); 510 AR_WRITE(sc, AR_FILT_CCK, 0); 511 AR_WRITE(sc, AR_MIBC, 0); 512 AR_WRITE(sc, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING); 513 AR_WRITE(sc, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING); 514 515 /* XXX ANI. */ 516 AR_WRITE(sc, AR_PHY_ERR_1, 0); 517 AR_WRITE(sc, AR_PHY_ERR_2, 0); 518 519 /* Disable HW crypto for now. */ 520 AR_SETBITS(sc, AR_DIAG_SW, AR_DIAG_ENCRYPT_DIS | AR_DIAG_DECRYPT_DIS); 521 522 /* Start PCU Rx. */ 523 AR_CLRBITS(sc, AR_DIAG_SW, AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT); 524 AR_WRITE_BARRIER(sc); 525 } 526 527 void 528 athn_set_rxfilter(struct athn_softc *sc, uint32_t rfilt) 529 { 530 AR_WRITE(sc, AR_RX_FILTER, rfilt); 531 532 #ifdef notyet 533 reg = AR_READ(sc, AR_PHY_ERR); 534 reg &= (AR_PHY_ERR_RADAR | AR_PHY_ERR_OFDM_TIMING | 535 AR_PHY_ERR_CCK_TIMING); 536 AR_WRITE(sc, AR_PHY_ERR, reg); 537 if (reg != 0) 538 AR_SETBITS(sc, AR_RXCFG, AR_RXCFG_ZLFDMA); 539 else 540 AR_CLRBITS(sc, AR_RXCFG, AR_RXCFG_ZLFDMA); 541 #else 542 AR_WRITE(sc, AR_PHY_ERR, 0); 543 AR_CLRBITS(sc, AR_RXCFG, AR_RXCFG_ZLFDMA); 544 #endif 545 AR_WRITE_BARRIER(sc); 546 } 547 548 int 549 athn_intr(void *xsc) 550 { 551 struct athn_softc *sc = xsc; 552 struct ifnet *ifp = &sc->sc_ic.ic_if; 553 554 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != 555 (IFF_UP | IFF_RUNNING)) 556 return (0); 557 558 return (sc->ops.intr(sc)); 559 } 560 561 void 562 athn_get_chipid(struct athn_softc *sc) 563 { 564 uint32_t reg; 565 566 reg = AR_READ(sc, AR_SREV); 567 if (MS(reg, AR_SREV_ID) == 0xff) { 568 sc->mac_ver = MS(reg, AR_SREV_VERSION2); 569 sc->mac_rev = MS(reg, AR_SREV_REVISION2); 570 if (!(reg & AR_SREV_TYPE2_HOST_MODE)) 571 sc->flags |= ATHN_FLAG_PCIE; 572 } else { 573 sc->mac_ver = MS(reg, AR_SREV_VERSION); 574 sc->mac_rev = MS(reg, AR_SREV_REVISION); 575 if (sc->mac_ver == AR_SREV_VERSION_5416_PCIE) 576 sc->flags |= ATHN_FLAG_PCIE; 577 } 578 } 579 580 const char * 581 athn_get_mac_name(struct athn_softc *sc) 582 { 583 switch (sc->mac_ver) { 584 case AR_SREV_VERSION_5416_PCI: 585 return ("AR5416"); 586 case AR_SREV_VERSION_5416_PCIE: 587 return ("AR5418"); 588 case AR_SREV_VERSION_9160: 589 return ("AR9160"); 590 case AR_SREV_VERSION_9280: 591 return ("AR9280"); 592 case AR_SREV_VERSION_9285: 593 return ("AR9285"); 594 case AR_SREV_VERSION_9271: 595 return ("AR9271"); 596 case AR_SREV_VERSION_9287: 597 return ("AR9287"); 598 case AR_SREV_VERSION_9380: 599 return ("AR9380"); 600 case AR_SREV_VERSION_9485: 601 return ("AR9485"); 602 } 603 return ("unknown"); 604 } 605 606 /* 607 * Return RF chip name (not for single-chip solutions). 608 */ 609 const char * 610 athn_get_rf_name(struct athn_softc *sc) 611 { 612 KASSERT(!AR_SINGLE_CHIP(sc)); 613 614 switch (sc->rf_rev) { 615 case AR_RAD5133_SREV_MAJOR: /* Dual-band 3T3R. */ 616 return ("AR5133"); 617 case AR_RAD2133_SREV_MAJOR: /* Single-band 3T3R. */ 618 return ("AR2133"); 619 case AR_RAD5122_SREV_MAJOR: /* Dual-band 2T2R. */ 620 return ("AR5122"); 621 case AR_RAD2122_SREV_MAJOR: /* Single-band 2T2R. */ 622 return ("AR2122"); 623 } 624 return ("unknown"); 625 } 626 627 int 628 athn_reset_power_on(struct athn_softc *sc) 629 { 630 int ntries; 631 632 /* Set force wake. */ 633 AR_WRITE(sc, AR_RTC_FORCE_WAKE, 634 AR_RTC_FORCE_WAKE_EN | AR_RTC_FORCE_WAKE_ON_INT); 635 636 if (!AR_SREV_9380_10_OR_LATER(sc)) { 637 /* Make sure no DMA is active by doing an AHB reset. */ 638 AR_WRITE(sc, AR_RC, AR_RC_AHB); 639 } 640 /* RTC reset and clear. */ 641 AR_WRITE(sc, AR_RTC_RESET, 0); 642 AR_WRITE_BARRIER(sc); 643 DELAY(2); 644 if (!AR_SREV_9380_10_OR_LATER(sc)) 645 AR_WRITE(sc, AR_RC, 0); 646 AR_WRITE(sc, AR_RTC_RESET, 1); 647 648 /* Poll until RTC is ON. */ 649 for (ntries = 0; ntries < 1000; ntries++) { 650 if ((AR_READ(sc, AR_RTC_STATUS) & AR_RTC_STATUS_M) == 651 AR_RTC_STATUS_ON) 652 break; 653 DELAY(10); 654 } 655 if (ntries == 1000) { 656 DPRINTF(("RTC not waking up\n")); 657 return (ETIMEDOUT); 658 } 659 return (athn_reset(sc, 0)); 660 } 661 662 int 663 athn_reset(struct athn_softc *sc, int cold) 664 { 665 int ntries; 666 667 /* Set force wake. */ 668 AR_WRITE(sc, AR_RTC_FORCE_WAKE, 669 AR_RTC_FORCE_WAKE_EN | AR_RTC_FORCE_WAKE_ON_INT); 670 671 if (AR_READ(sc, AR_INTR_SYNC_CAUSE) & 672 (AR_INTR_SYNC_LOCAL_TIMEOUT | AR_INTR_SYNC_RADM_CPL_TIMEOUT)) { 673 AR_WRITE(sc, AR_INTR_SYNC_ENABLE, 0); 674 AR_WRITE(sc, AR_RC, AR_RC_HOSTIF | 675 (!AR_SREV_9380_10_OR_LATER(sc) ? AR_RC_AHB : 0)); 676 } else if (!AR_SREV_9380_10_OR_LATER(sc)) 677 AR_WRITE(sc, AR_RC, AR_RC_AHB); 678 679 AR_WRITE(sc, AR_RTC_RC, AR_RTC_RC_MAC_WARM | 680 (cold ? AR_RTC_RC_MAC_COLD : 0)); 681 AR_WRITE_BARRIER(sc); 682 DELAY(50); 683 AR_WRITE(sc, AR_RTC_RC, 0); 684 for (ntries = 0; ntries < 1000; ntries++) { 685 if (!(AR_READ(sc, AR_RTC_RC) & 686 (AR_RTC_RC_MAC_WARM | AR_RTC_RC_MAC_COLD))) 687 break; 688 DELAY(10); 689 } 690 if (ntries == 1000) { 691 DPRINTF(("RTC stuck in MAC reset\n")); 692 return (ETIMEDOUT); 693 } 694 AR_WRITE(sc, AR_RC, 0); 695 AR_WRITE_BARRIER(sc); 696 return (0); 697 } 698 699 int 700 athn_set_power_awake(struct athn_softc *sc) 701 { 702 int ntries, error; 703 704 /* Do a Power-On-Reset if shutdown. */ 705 if ((AR_READ(sc, AR_RTC_STATUS) & AR_RTC_STATUS_M) == 706 AR_RTC_STATUS_SHUTDOWN) { 707 if ((error = athn_reset_power_on(sc)) != 0) 708 return (error); 709 if (!AR_SREV_9380_10_OR_LATER(sc)) 710 athn_init_pll(sc, NULL); 711 } 712 AR_SETBITS(sc, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN); 713 AR_WRITE_BARRIER(sc); 714 DELAY(50); /* Give chip the chance to awake. */ 715 716 /* Poll until RTC is ON. */ 717 for (ntries = 0; ntries < 4000; ntries++) { 718 if ((AR_READ(sc, AR_RTC_STATUS) & AR_RTC_STATUS_M) == 719 AR_RTC_STATUS_ON) 720 break; 721 DELAY(50); 722 AR_SETBITS(sc, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN); 723 } 724 if (ntries == 4000) { 725 DPRINTF(("RTC not waking up\n")); 726 return (ETIMEDOUT); 727 } 728 729 AR_CLRBITS(sc, AR_STA_ID1, AR_STA_ID1_PWR_SAV); 730 AR_WRITE_BARRIER(sc); 731 return (0); 732 } 733 734 void 735 athn_set_power_sleep(struct athn_softc *sc) 736 { 737 AR_SETBITS(sc, AR_STA_ID1, AR_STA_ID1_PWR_SAV); 738 /* Allow the MAC to go to sleep. */ 739 AR_CLRBITS(sc, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN); 740 if (!AR_SREV_9380_10_OR_LATER(sc)) 741 AR_WRITE(sc, AR_RC, AR_RC_AHB | AR_RC_HOSTIF); 742 /* 743 * NB: Clearing RTC_RESET_EN when setting the chip to sleep mode 744 * results in high power consumption on AR5416 chipsets. 745 */ 746 if (!AR_SREV_5416(sc) && !AR_SREV_9271(sc)) 747 AR_CLRBITS(sc, AR_RTC_RESET, AR_RTC_RESET_EN); 748 AR_WRITE_BARRIER(sc); 749 } 750 751 void 752 athn_init_pll(struct athn_softc *sc, const struct ieee80211_channel *c) 753 { 754 uint32_t pll; 755 756 if (AR_SREV_9380_10_OR_LATER(sc)) { 757 if (AR_SREV_9485(sc)) 758 AR_WRITE(sc, AR_RTC_PLL_CONTROL2, 0x886666); 759 pll = SM(AR_RTC_9160_PLL_REFDIV, 0x5); 760 pll |= SM(AR_RTC_9160_PLL_DIV, 0x2c); 761 } else if (AR_SREV_9280_10_OR_LATER(sc)) { 762 pll = SM(AR_RTC_9160_PLL_REFDIV, 0x05); 763 if (c != NULL && IEEE80211_IS_CHAN_5GHZ(c)) { 764 if (sc->flags & ATHN_FLAG_FAST_PLL_CLOCK) 765 pll = 0x142c; 766 else if (AR_SREV_9280_20(sc)) 767 pll = 0x2850; 768 else 769 pll |= SM(AR_RTC_9160_PLL_DIV, 0x28); 770 } else 771 pll |= SM(AR_RTC_9160_PLL_DIV, 0x2c); 772 } else if (AR_SREV_9160_10_OR_LATER(sc)) { 773 pll = SM(AR_RTC_9160_PLL_REFDIV, 0x05); 774 if (c != NULL && IEEE80211_IS_CHAN_5GHZ(c)) 775 pll |= SM(AR_RTC_9160_PLL_DIV, 0x50); 776 else 777 pll |= SM(AR_RTC_9160_PLL_DIV, 0x58); 778 } else { 779 pll = AR_RTC_PLL_REFDIV_5 | AR_RTC_PLL_DIV2; 780 if (c != NULL && IEEE80211_IS_CHAN_5GHZ(c)) 781 pll |= SM(AR_RTC_PLL_DIV, 0x0a); 782 else 783 pll |= SM(AR_RTC_PLL_DIV, 0x0b); 784 } 785 DPRINTFN(5, ("AR_RTC_PLL_CONTROL=0x%08x\n", pll)); 786 AR_WRITE(sc, AR_RTC_PLL_CONTROL, pll); 787 if (AR_SREV_9271(sc)) { 788 /* Switch core clock to 117MHz. */ 789 AR_WRITE_BARRIER(sc); 790 DELAY(500); 791 AR_WRITE(sc, AR9271_CLOCK_CONTROL, 0x304); 792 } 793 AR_WRITE_BARRIER(sc); 794 DELAY(100); 795 AR_WRITE(sc, AR_RTC_SLEEP_CLK, AR_RTC_FORCE_DERIVED_CLK); 796 AR_WRITE_BARRIER(sc); 797 } 798 799 void 800 athn_write_serdes(struct athn_softc *sc, const struct athn_serdes *serdes) 801 { 802 int i; 803 804 /* Write sequence to Serializer/Deserializer. */ 805 for (i = 0; i < serdes->nvals; i++) 806 AR_WRITE(sc, serdes->regs[i], serdes->vals[i]); 807 AR_WRITE_BARRIER(sc); 808 } 809 810 void 811 athn_config_pcie(struct athn_softc *sc) 812 { 813 /* Disable PLL when in L0s as well as receiver clock when in L1. */ 814 athn_write_serdes(sc, sc->serdes); 815 816 DELAY(1000); 817 /* Allow forcing of PCIe core into L1 state. */ 818 AR_SETBITS(sc, AR_PCIE_PM_CTRL, AR_PCIE_PM_CTRL_ENA); 819 820 #ifndef ATHN_PCIE_WAEN 821 AR_WRITE(sc, AR_WA, sc->workaround); 822 #else 823 AR_WRITE(sc, AR_WA, ATHN_PCIE_WAEN); 824 #endif 825 AR_WRITE_BARRIER(sc); 826 } 827 828 /* 829 * Serializer/Deserializer programming for non-PCIe devices. 830 */ 831 static const uint32_t ar_nonpcie_serdes_regs[] = { 832 AR_PCIE_SERDES, 833 AR_PCIE_SERDES, 834 AR_PCIE_SERDES, 835 AR_PCIE_SERDES, 836 AR_PCIE_SERDES, 837 AR_PCIE_SERDES, 838 AR_PCIE_SERDES, 839 AR_PCIE_SERDES, 840 AR_PCIE_SERDES, 841 AR_PCIE_SERDES2, 842 }; 843 844 static const uint32_t ar_nonpcie_serdes_vals[] = { 845 0x9248fc00, 846 0x24924924, 847 0x28000029, 848 0x57160824, 849 0x25980579, 850 0x00000000, 851 0x1aaabe40, 852 0xbe105554, 853 0x000e1007, 854 0x00000000 855 }; 856 857 static const struct athn_serdes ar_nonpcie_serdes = { 858 nitems(ar_nonpcie_serdes_vals), 859 ar_nonpcie_serdes_regs, 860 ar_nonpcie_serdes_vals 861 }; 862 863 void 864 athn_config_nonpcie(struct athn_softc *sc) 865 { 866 athn_write_serdes(sc, &ar_nonpcie_serdes); 867 } 868 869 int 870 athn_set_chan(struct athn_softc *sc, struct ieee80211_channel *c, 871 struct ieee80211_channel *extc) 872 { 873 struct athn_ops *ops = &sc->ops; 874 int error, qid; 875 876 /* Check that Tx is stopped, otherwise RF Bus grant will not work. */ 877 for (qid = 0; qid < ATHN_QID_COUNT; qid++) 878 if (athn_tx_pending(sc, qid)) 879 return (EBUSY); 880 881 /* Request RF Bus grant. */ 882 if ((error = ops->rf_bus_request(sc)) != 0) 883 return (error); 884 885 ops->set_phy(sc, c, extc); 886 887 /* Change the synthesizer. */ 888 if ((error = ops->set_synth(sc, c, extc)) != 0) 889 return (error); 890 891 sc->curchan = c; 892 sc->curchanext = extc; 893 894 /* Set transmit power values for new channel. */ 895 ops->set_txpower(sc, c, extc); 896 897 /* Release the RF Bus grant. */ 898 ops->rf_bus_release(sc); 899 900 /* Write delta slope coeffs for modes where OFDM may be used. */ 901 if (sc->sc_ic.ic_curmode != IEEE80211_MODE_11B) 902 ops->set_delta_slope(sc, c, extc); 903 904 ops->spur_mitigate(sc, c, extc); 905 906 return (0); 907 } 908 909 int 910 athn_switch_chan(struct athn_softc *sc, struct ieee80211_channel *c, 911 struct ieee80211_channel *extc) 912 { 913 int error, qid; 914 915 /* Disable interrupts. */ 916 athn_disable_interrupts(sc); 917 918 /* Stop all Tx queues. */ 919 for (qid = 0; qid < ATHN_QID_COUNT; qid++) 920 athn_stop_tx_dma(sc, qid); 921 for (qid = 0; qid < ATHN_QID_COUNT; qid++) 922 athn_tx_reclaim(sc, qid); 923 924 /* Stop Rx. */ 925 AR_SETBITS(sc, AR_DIAG_SW, AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT); 926 AR_WRITE(sc, AR_MIBC, AR_MIBC_FMC); 927 AR_WRITE(sc, AR_MIBC, AR_MIBC_CMC); 928 AR_WRITE(sc, AR_FILT_OFDM, 0); 929 AR_WRITE(sc, AR_FILT_CCK, 0); 930 athn_set_rxfilter(sc, 0); 931 error = athn_stop_rx_dma(sc); 932 if (error != 0) 933 goto reset; 934 935 #ifdef notyet 936 /* AR9280 needs a full reset. */ 937 if (AR_SREV_9280(sc)) 938 #endif 939 goto reset; 940 941 /* If band or bandwidth changes, we need to do a full reset. */ 942 if (c->ic_flags != sc->curchan->ic_flags || 943 ((extc != NULL) ^ (sc->curchanext != NULL))) { 944 DPRINTFN(2, ("channel band switch\n")); 945 goto reset; 946 } 947 error = athn_set_power_awake(sc); 948 if (error != 0) 949 goto reset; 950 951 error = athn_set_chan(sc, c, extc); 952 if (error != 0) { 953 reset: /* Error found, try a full reset. */ 954 DPRINTFN(3, ("needs a full reset\n")); 955 error = athn_hw_reset(sc, c, extc, 0); 956 if (error != 0) /* Hopeless case. */ 957 return (error); 958 } 959 athn_rx_start(sc); 960 961 /* Re-enable interrupts. */ 962 athn_enable_interrupts(sc); 963 return (0); 964 } 965 966 void 967 athn_get_delta_slope(uint32_t coeff, uint32_t *exponent, uint32_t *mantissa) 968 { 969 #define COEFF_SCALE_SHIFT 24 970 uint32_t exp, man; 971 972 /* exponent = 14 - floor(log2(coeff)) */ 973 for (exp = 31; exp > 0; exp--) 974 if (coeff & (1 << exp)) 975 break; 976 exp = 14 - (exp - COEFF_SCALE_SHIFT); 977 978 /* mantissa = floor(coeff * 2^exponent + 0.5) */ 979 man = coeff + (1 << (COEFF_SCALE_SHIFT - exp - 1)); 980 981 *mantissa = man >> (COEFF_SCALE_SHIFT - exp); 982 *exponent = exp - 16; 983 #undef COEFF_SCALE_SHIFT 984 } 985 986 void 987 athn_reset_key(struct athn_softc *sc, int entry) 988 { 989 /* 990 * NB: Key cache registers access special memory area that requires 991 * two 32-bit writes to actually update the values in the internal 992 * memory. Consequently, writes must be grouped by pair. 993 */ 994 AR_WRITE(sc, AR_KEYTABLE_KEY0(entry), 0); 995 AR_WRITE(sc, AR_KEYTABLE_KEY1(entry), 0); 996 997 AR_WRITE(sc, AR_KEYTABLE_KEY2(entry), 0); 998 AR_WRITE(sc, AR_KEYTABLE_KEY3(entry), 0); 999 1000 AR_WRITE(sc, AR_KEYTABLE_KEY4(entry), 0); 1001 AR_WRITE(sc, AR_KEYTABLE_TYPE(entry), AR_KEYTABLE_TYPE_CLR); 1002 1003 AR_WRITE(sc, AR_KEYTABLE_MAC0(entry), 0); 1004 AR_WRITE(sc, AR_KEYTABLE_MAC1(entry), 0); 1005 1006 AR_WRITE_BARRIER(sc); 1007 } 1008 1009 int 1010 athn_set_key(struct ieee80211com *ic, struct ieee80211_node *ni, 1011 struct ieee80211_key *k) 1012 { 1013 struct athn_softc *sc = ic->ic_softc; 1014 const uint8_t *txmic, *rxmic, *key, *addr; 1015 uintptr_t entry, micentry; 1016 uint32_t type, lo, hi; 1017 1018 switch (k->k_cipher) { 1019 case IEEE80211_CIPHER_WEP40: 1020 type = AR_KEYTABLE_TYPE_40; 1021 break; 1022 case IEEE80211_CIPHER_WEP104: 1023 type = AR_KEYTABLE_TYPE_104; 1024 break; 1025 case IEEE80211_CIPHER_TKIP: 1026 type = AR_KEYTABLE_TYPE_TKIP; 1027 break; 1028 case IEEE80211_CIPHER_CCMP: 1029 type = AR_KEYTABLE_TYPE_CCM; 1030 break; 1031 default: 1032 /* Fallback to software crypto for other ciphers. */ 1033 return (ieee80211_set_key(ic, ni, k)); 1034 } 1035 1036 if (!(k->k_flags & IEEE80211_KEY_GROUP)) 1037 entry = IEEE80211_WEP_NKID + IEEE80211_AID(ni->ni_associd); 1038 else 1039 entry = k->k_id; 1040 k->k_priv = (void *)entry; 1041 1042 /* NB: See note about key cache registers access above. */ 1043 key = k->k_key; 1044 if (type == AR_KEYTABLE_TYPE_TKIP) { 1045 #ifndef IEEE80211_STA_ONLY 1046 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 1047 txmic = &key[16]; 1048 rxmic = &key[24]; 1049 } else 1050 #endif 1051 { 1052 rxmic = &key[16]; 1053 txmic = &key[24]; 1054 } 1055 /* Tx+Rx MIC key is at entry + 64. */ 1056 micentry = entry + 64; 1057 AR_WRITE(sc, AR_KEYTABLE_KEY0(micentry), LE_READ_4(&rxmic[0])); 1058 AR_WRITE(sc, AR_KEYTABLE_KEY1(micentry), LE_READ_2(&txmic[2])); 1059 1060 AR_WRITE(sc, AR_KEYTABLE_KEY2(micentry), LE_READ_4(&rxmic[4])); 1061 AR_WRITE(sc, AR_KEYTABLE_KEY3(micentry), LE_READ_2(&txmic[0])); 1062 1063 AR_WRITE(sc, AR_KEYTABLE_KEY4(micentry), LE_READ_4(&txmic[4])); 1064 AR_WRITE(sc, AR_KEYTABLE_TYPE(micentry), AR_KEYTABLE_TYPE_CLR); 1065 } 1066 AR_WRITE(sc, AR_KEYTABLE_KEY0(entry), LE_READ_4(&key[ 0])); 1067 AR_WRITE(sc, AR_KEYTABLE_KEY1(entry), LE_READ_2(&key[ 4])); 1068 1069 AR_WRITE(sc, AR_KEYTABLE_KEY2(entry), LE_READ_4(&key[ 6])); 1070 AR_WRITE(sc, AR_KEYTABLE_KEY3(entry), LE_READ_2(&key[10])); 1071 1072 AR_WRITE(sc, AR_KEYTABLE_KEY4(entry), LE_READ_4(&key[12])); 1073 AR_WRITE(sc, AR_KEYTABLE_TYPE(entry), type); 1074 1075 if (!(k->k_flags & IEEE80211_KEY_GROUP)) { 1076 addr = ni->ni_macaddr; 1077 lo = LE_READ_4(&addr[0]); 1078 hi = LE_READ_2(&addr[4]); 1079 lo = lo >> 1 | hi << 31; 1080 hi = hi >> 1; 1081 } else 1082 lo = hi = 0; 1083 AR_WRITE(sc, AR_KEYTABLE_MAC0(entry), lo); 1084 AR_WRITE(sc, AR_KEYTABLE_MAC1(entry), hi | AR_KEYTABLE_VALID); 1085 AR_WRITE_BARRIER(sc); 1086 return (0); 1087 } 1088 1089 void 1090 athn_delete_key(struct ieee80211com *ic, struct ieee80211_node *ni, 1091 struct ieee80211_key *k) 1092 { 1093 struct athn_softc *sc = ic->ic_softc; 1094 uintptr_t entry; 1095 1096 switch (k->k_cipher) { 1097 case IEEE80211_CIPHER_WEP40: 1098 case IEEE80211_CIPHER_WEP104: 1099 case IEEE80211_CIPHER_CCMP: 1100 entry = (uintptr_t)k->k_priv; 1101 athn_reset_key(sc, entry); 1102 break; 1103 case IEEE80211_CIPHER_TKIP: 1104 entry = (uintptr_t)k->k_priv; 1105 athn_reset_key(sc, entry); 1106 athn_reset_key(sc, entry + 64); 1107 break; 1108 default: 1109 /* Fallback to software crypto for other ciphers. */ 1110 ieee80211_delete_key(ic, ni, k); 1111 } 1112 } 1113 1114 void 1115 athn_led_init(struct athn_softc *sc) 1116 { 1117 struct athn_ops *ops = &sc->ops; 1118 1119 ops->gpio_config_output(sc, sc->led_pin, AR_GPIO_OUTPUT_MUX_AS_OUTPUT); 1120 /* LED off, active low. */ 1121 athn_set_led(sc, 0); 1122 } 1123 1124 void 1125 athn_set_led(struct athn_softc *sc, int on) 1126 { 1127 struct athn_ops *ops = &sc->ops; 1128 1129 sc->led_state = on; 1130 ops->gpio_write(sc, sc->led_pin, !sc->led_state); 1131 } 1132 1133 #ifdef ATHN_BT_COEXISTENCE 1134 void 1135 athn_btcoex_init(struct athn_softc *sc) 1136 { 1137 struct athn_ops *ops = &sc->ops; 1138 uint32_t reg; 1139 1140 if (sc->flags & ATHN_FLAG_BTCOEX2WIRE) { 1141 /* Connect bt_active to baseband. */ 1142 AR_CLRBITS(sc, sc->gpio_input_en_off, 1143 AR_GPIO_INPUT_EN_VAL_BT_PRIORITY_DEF | 1144 AR_GPIO_INPUT_EN_VAL_BT_FREQUENCY_DEF); 1145 AR_SETBITS(sc, sc->gpio_input_en_off, 1146 AR_GPIO_INPUT_EN_VAL_BT_ACTIVE_BB); 1147 1148 reg = AR_READ(sc, AR_GPIO_INPUT_MUX1); 1149 reg = RW(reg, AR_GPIO_INPUT_MUX1_BT_ACTIVE, 1150 AR_GPIO_BTACTIVE_PIN); 1151 AR_WRITE(sc, AR_GPIO_INPUT_MUX1, reg); 1152 AR_WRITE_BARRIER(sc); 1153 1154 ops->gpio_config_input(sc, AR_GPIO_BTACTIVE_PIN); 1155 } else { /* 3-wire. */ 1156 AR_SETBITS(sc, sc->gpio_input_en_off, 1157 AR_GPIO_INPUT_EN_VAL_BT_PRIORITY_BB | 1158 AR_GPIO_INPUT_EN_VAL_BT_ACTIVE_BB); 1159 1160 reg = AR_READ(sc, AR_GPIO_INPUT_MUX1); 1161 reg = RW(reg, AR_GPIO_INPUT_MUX1_BT_ACTIVE, 1162 AR_GPIO_BTACTIVE_PIN); 1163 reg = RW(reg, AR_GPIO_INPUT_MUX1_BT_PRIORITY, 1164 AR_GPIO_BTPRIORITY_PIN); 1165 AR_WRITE(sc, AR_GPIO_INPUT_MUX1, reg); 1166 AR_WRITE_BARRIER(sc); 1167 1168 ops->gpio_config_input(sc, AR_GPIO_BTACTIVE_PIN); 1169 ops->gpio_config_input(sc, AR_GPIO_BTPRIORITY_PIN); 1170 } 1171 } 1172 1173 void 1174 athn_btcoex_enable(struct athn_softc *sc) 1175 { 1176 struct athn_ops *ops = &sc->ops; 1177 uint32_t reg; 1178 1179 if (sc->flags & ATHN_FLAG_BTCOEX3WIRE) { 1180 AR_WRITE(sc, AR_BT_COEX_MODE, 1181 SM(AR_BT_MODE, AR_BT_MODE_SLOTTED) | 1182 SM(AR_BT_PRIORITY_TIME, 2) | 1183 SM(AR_BT_FIRST_SLOT_TIME, 5) | 1184 SM(AR_BT_QCU_THRESH, ATHN_QID_AC_BE) | 1185 AR_BT_TXSTATE_EXTEND | AR_BT_TX_FRAME_EXTEND | 1186 AR_BT_QUIET | AR_BT_RX_CLEAR_POLARITY); 1187 AR_WRITE(sc, AR_BT_COEX_WEIGHT, 1188 SM(AR_BTCOEX_BT_WGHT, AR_STOMP_LOW_BT_WGHT) | 1189 SM(AR_BTCOEX_WL_WGHT, AR_STOMP_LOW_WL_WGHT)); 1190 AR_WRITE(sc, AR_BT_COEX_MODE2, 1191 SM(AR_BT_BCN_MISS_THRESH, 50) | 1192 AR_BT_HOLD_RX_CLEAR | AR_BT_DISABLE_BT_ANT); 1193 1194 AR_SETBITS(sc, AR_QUIET1, AR_QUIET1_QUIET_ACK_CTS_ENABLE); 1195 AR_CLRBITS(sc, AR_PCU_MISC, AR_PCU_BT_ANT_PREVENT_RX); 1196 AR_WRITE_BARRIER(sc); 1197 1198 ops->gpio_config_output(sc, AR_GPIO_WLANACTIVE_PIN, 1199 AR_GPIO_OUTPUT_MUX_AS_RX_CLEAR_EXTERNAL); 1200 1201 } else { /* 2-wire. */ 1202 ops->gpio_config_output(sc, AR_GPIO_WLANACTIVE_PIN, 1203 AR_GPIO_OUTPUT_MUX_AS_TX_FRAME); 1204 } 1205 reg = AR_READ(sc, AR_GPIO_PDPU); 1206 reg &= ~(0x3 << (AR_GPIO_WLANACTIVE_PIN * 2)); 1207 reg |= 0x2 << (AR_GPIO_WLANACTIVE_PIN * 2); 1208 AR_WRITE(sc, AR_GPIO_PDPU, reg); 1209 AR_WRITE_BARRIER(sc); 1210 1211 /* Disable PCIe Active State Power Management (ASPM). */ 1212 if (sc->sc_disable_aspm != NULL) 1213 sc->sc_disable_aspm(sc); 1214 1215 /* XXX Start periodic timer. */ 1216 } 1217 1218 void 1219 athn_btcoex_disable(struct athn_softc *sc) 1220 { 1221 struct athn_ops *ops = &sc->ops; 1222 1223 ops->gpio_write(sc, AR_GPIO_WLANACTIVE_PIN, 0); 1224 1225 ops->gpio_config_output(sc, AR_GPIO_WLANACTIVE_PIN, 1226 AR_GPIO_OUTPUT_MUX_AS_OUTPUT); 1227 1228 if (sc->flags & ATHN_FLAG_BTCOEX3WIRE) { 1229 AR_WRITE(sc, AR_BT_COEX_MODE, 1230 SM(AR_BT_MODE, AR_BT_MODE_DISABLED) | AR_BT_QUIET); 1231 AR_WRITE(sc, AR_BT_COEX_WEIGHT, 0); 1232 AR_WRITE(sc, AR_BT_COEX_MODE2, 0); 1233 /* XXX Stop periodic timer. */ 1234 } 1235 AR_WRITE_BARRIER(sc); 1236 /* XXX Restore ASPM setting? */ 1237 } 1238 #endif 1239 1240 void 1241 athn_iter_calib(void *arg, struct ieee80211_node *ni) 1242 { 1243 struct athn_softc *sc = arg; 1244 struct athn_node *an = (struct athn_node *)ni; 1245 1246 if ((ni->ni_flags & IEEE80211_NODE_HT) == 0) 1247 ieee80211_amrr_choose(&sc->amrr, ni, &an->amn); 1248 } 1249 1250 int 1251 athn_cap_noisefloor(struct athn_softc *sc, int nf) 1252 { 1253 int16_t min, max; 1254 1255 if (nf == 0 || nf == -1) /* invalid measurement */ 1256 return AR_DEFAULT_NOISE_FLOOR; 1257 1258 if (IEEE80211_IS_CHAN_2GHZ(sc->sc_ic.ic_bss->ni_chan)) { 1259 min = sc->cca_min_2g; 1260 max = sc->cca_max_2g; 1261 } else { 1262 min = sc->cca_min_5g; 1263 max = sc->cca_max_5g; 1264 } 1265 1266 if (nf < min) 1267 return min; 1268 if (nf > max) 1269 return max; 1270 1271 return nf; 1272 } 1273 1274 int 1275 athn_nf_hist_mid(int *nf_vals, int nvalid) 1276 { 1277 int nf_sorted[ATHN_NF_CAL_HIST_MAX]; 1278 int i, j, nf; 1279 1280 if (nvalid <= 1) 1281 return nf_vals[0]; 1282 1283 for (i = 0; i < nvalid; i++) 1284 nf_sorted[i] = nf_vals[i]; 1285 1286 for (i = 0; i < nvalid; i++) { 1287 for (j = 1; j < nvalid - i; j++) { 1288 if (nf_sorted[j] > nf_sorted[j - 1]) { 1289 nf = nf_sorted[j]; 1290 nf_sorted[j] = nf_sorted[j - 1]; 1291 nf_sorted[j - 1] = nf; 1292 } 1293 } 1294 } 1295 1296 return nf_sorted[nvalid / 2]; 1297 } 1298 1299 void 1300 athn_filter_noisefloor(struct athn_softc *sc) 1301 { 1302 int nf_vals[ATHN_NF_CAL_HIST_MAX]; 1303 int nf_ext_vals[ATHN_NF_CAL_HIST_MAX]; 1304 int i, cur, n; 1305 1306 for (i = 0; i < sc->nrxchains; i++) { 1307 if (sc->nf_hist_cur > 0) 1308 cur = sc->nf_hist_cur - 1; 1309 else 1310 cur = ATHN_NF_CAL_HIST_MAX - 1; 1311 for (n = 0; n < sc->nf_hist_nvalid; n++) { 1312 nf_vals[n] = sc->nf_hist[cur].nf[i]; 1313 nf_ext_vals[n] = sc->nf_hist[cur].nf_ext[i]; 1314 if (++cur >= ATHN_NF_CAL_HIST_MAX) 1315 cur = 0; 1316 } 1317 sc->nf_priv[i] = athn_cap_noisefloor(sc, 1318 athn_nf_hist_mid(nf_vals, sc->nf_hist_nvalid)); 1319 sc->nf_ext_priv[i] = athn_cap_noisefloor(sc, 1320 athn_nf_hist_mid(nf_ext_vals, sc->nf_hist_nvalid)); 1321 } 1322 } 1323 1324 void 1325 athn_start_noisefloor_calib(struct athn_softc *sc, int reset_history) 1326 { 1327 extern int ticks; 1328 1329 if (reset_history) 1330 sc->nf_hist_nvalid = 0; 1331 1332 sc->nf_calib_pending = 1; 1333 sc->nf_calib_ticks = ticks; 1334 1335 sc->ops.noisefloor_calib(sc); 1336 } 1337 1338 void 1339 athn_calib_to(void *arg) 1340 { 1341 extern int ticks; 1342 struct athn_softc *sc = arg; 1343 struct athn_ops *ops = &sc->ops; 1344 struct ieee80211com *ic = &sc->sc_ic; 1345 int s; 1346 1347 s = splnet(); 1348 1349 /* Do periodic (every 4 minutes) PA calibration. */ 1350 if (AR_SREV_9285_11_OR_LATER(sc) && 1351 !AR_SREV_9380_10_OR_LATER(sc) && 1352 (ticks - (sc->pa_calib_ticks + 240 * hz)) >= 0) { 1353 sc->pa_calib_ticks = ticks; 1354 if (AR_SREV_9271(sc)) 1355 ar9271_pa_calib(sc); 1356 else 1357 ar9285_pa_calib(sc); 1358 } 1359 1360 /* Do periodic (every 4 minutes) NF calibration. */ 1361 if (sc->nf_calib_pending && ops->get_noisefloor(sc)) { 1362 if (sc->nf_hist_nvalid < ATHN_NF_CAL_HIST_MAX) 1363 sc->nf_hist_nvalid++; 1364 athn_filter_noisefloor(sc); 1365 ops->apply_noisefloor(sc); 1366 sc->nf_calib_pending = 0; 1367 } 1368 if (ticks - (sc->nf_calib_ticks + 240 * hz) >= 0) 1369 athn_start_noisefloor_calib(sc, 0); 1370 1371 /* Do periodic (every 30 seconds) temperature compensation. */ 1372 if ((sc->flags & ATHN_FLAG_OLPC) && 1373 ticks >= sc->olpc_ticks + 30 * hz) { 1374 sc->olpc_ticks = ticks; 1375 ops->olpc_temp_compensation(sc); 1376 } 1377 1378 #ifdef notyet 1379 /* XXX ANI. */ 1380 athn_ani_monitor(sc); 1381 #endif 1382 1383 /* Do periodic (every 30 seconds) ADC/IQ calibration. */ 1384 if (sc->cur_calib_mask != 0) { 1385 ops->next_calib(sc); 1386 sc->iqcal_ticks = ticks; 1387 } else if (sc->sup_calib_mask != 0 && 1388 ticks >= sc->iqcal_ticks + 30 * hz) { 1389 memset(&sc->calib, 0, sizeof(sc->calib)); 1390 sc->cur_calib_mask = sc->sup_calib_mask; 1391 ops->do_calib(sc); 1392 sc->iqcal_ticks = ticks; 1393 } 1394 1395 if (ic->ic_fixed_rate == -1) { 1396 if (ic->ic_opmode == IEEE80211_M_STA) 1397 athn_iter_calib(sc, ic->ic_bss); 1398 else 1399 ieee80211_iterate_nodes(ic, athn_iter_calib, sc); 1400 } 1401 timeout_add_msec(&sc->calib_to, 500); 1402 splx(s); 1403 } 1404 1405 int 1406 athn_init_calib(struct athn_softc *sc, struct ieee80211_channel *c, 1407 struct ieee80211_channel *extc) 1408 { 1409 struct athn_ops *ops = &sc->ops; 1410 int error; 1411 1412 if (AR_SREV_9380_10_OR_LATER(sc)) 1413 error = ar9003_init_calib(sc); 1414 else if (AR_SREV_9285_10_OR_LATER(sc)) 1415 error = ar9285_init_calib(sc, c, extc); 1416 else 1417 error = ar5416_init_calib(sc, c, extc); 1418 if (error != 0) 1419 return (error); 1420 1421 if (!AR_SREV_9380_10_OR_LATER(sc)) { 1422 /* Do PA calibration. */ 1423 if (AR_SREV_9285_11_OR_LATER(sc)) { 1424 extern int ticks; 1425 sc->pa_calib_ticks = ticks; 1426 if (AR_SREV_9271(sc)) 1427 ar9271_pa_calib(sc); 1428 else 1429 ar9285_pa_calib(sc); 1430 } 1431 } 1432 1433 /* Do noisefloor calibration. */ 1434 ops->init_noisefloor_calib(sc); 1435 1436 if (AR_SREV_9160_10_OR_LATER(sc)) { 1437 /* Support IQ calibration. */ 1438 sc->sup_calib_mask = ATHN_CAL_IQ; 1439 if (AR_SREV_9380_10_OR_LATER(sc)) { 1440 /* Support temperature compensation calibration. */ 1441 sc->sup_calib_mask |= ATHN_CAL_TEMP; 1442 } else if (IEEE80211_IS_CHAN_5GHZ(c) || extc != NULL) { 1443 /* 1444 * ADC gain calibration causes uplink throughput 1445 * drops in HT40 mode on AR9287. 1446 */ 1447 if (!AR_SREV_9287(sc)) { 1448 /* Support ADC gain calibration. */ 1449 sc->sup_calib_mask |= ATHN_CAL_ADC_GAIN; 1450 } 1451 /* Support ADC DC offset calibration. */ 1452 sc->sup_calib_mask |= ATHN_CAL_ADC_DC; 1453 } 1454 } 1455 return (0); 1456 } 1457 1458 /* 1459 * Adaptive noise immunity. 1460 */ 1461 int32_t 1462 athn_ani_get_rssi(struct athn_softc *sc) 1463 { 1464 return (0); /* XXX */ 1465 } 1466 1467 void 1468 athn_ani_ofdm_err_trigger(struct athn_softc *sc) 1469 { 1470 struct athn_ani *ani = &sc->ani; 1471 struct athn_ops *ops = &sc->ops; 1472 int32_t rssi; 1473 1474 /* First, raise noise immunity level, up to max. */ 1475 if (ani->noise_immunity_level < 4) { 1476 ani->noise_immunity_level++; 1477 ops->set_noise_immunity_level(sc, ani->noise_immunity_level); 1478 return; 1479 } 1480 1481 /* Then, raise our spur immunity level, up to max. */ 1482 if (ani->spur_immunity_level < 7) { 1483 ani->spur_immunity_level++; 1484 ops->set_spur_immunity_level(sc, ani->spur_immunity_level); 1485 return; 1486 } 1487 1488 #ifndef IEEE80211_STA_ONLY 1489 if (sc->sc_ic.ic_opmode == IEEE80211_M_HOSTAP) { 1490 if (ani->firstep_level < 2) { 1491 ani->firstep_level++; 1492 ops->set_firstep_level(sc, ani->firstep_level); 1493 } 1494 return; 1495 } 1496 #endif 1497 rssi = athn_ani_get_rssi(sc); 1498 if (rssi > ATHN_ANI_RSSI_THR_HIGH) { 1499 /* 1500 * Beacon RSSI is high, turn off OFDM weak signal detection 1501 * or raise first step level as last resort. 1502 */ 1503 if (ani->ofdm_weak_signal) { 1504 ani->ofdm_weak_signal = 0; 1505 ops->disable_ofdm_weak_signal(sc); 1506 ani->spur_immunity_level = 0; 1507 ops->set_spur_immunity_level(sc, 0); 1508 } else if (ani->firstep_level < 2) { 1509 ani->firstep_level++; 1510 ops->set_firstep_level(sc, ani->firstep_level); 1511 } 1512 } else if (rssi > ATHN_ANI_RSSI_THR_LOW) { 1513 /* 1514 * Beacon RSSI is in mid range, we need OFDM weak signal 1515 * detection but we can raise first step level. 1516 */ 1517 if (!ani->ofdm_weak_signal) { 1518 ani->ofdm_weak_signal = 1; 1519 ops->enable_ofdm_weak_signal(sc); 1520 } 1521 if (ani->firstep_level < 2) { 1522 ani->firstep_level++; 1523 ops->set_firstep_level(sc, ani->firstep_level); 1524 } 1525 } else if (IEEE80211_IS_CHAN_2GHZ(sc->sc_ic.ic_bss->ni_chan)) { 1526 /* 1527 * Beacon RSSI is low, if in b/g mode, turn off OFDM weak 1528 * signal detection and zero first step level to maximize 1529 * CCK sensitivity. 1530 */ 1531 if (ani->ofdm_weak_signal) { 1532 ani->ofdm_weak_signal = 0; 1533 ops->disable_ofdm_weak_signal(sc); 1534 } 1535 if (ani->firstep_level > 0) { 1536 ani->firstep_level = 0; 1537 ops->set_firstep_level(sc, 0); 1538 } 1539 } 1540 } 1541 1542 void 1543 athn_ani_cck_err_trigger(struct athn_softc *sc) 1544 { 1545 struct athn_ani *ani = &sc->ani; 1546 struct athn_ops *ops = &sc->ops; 1547 int32_t rssi; 1548 1549 /* Raise noise immunity level, up to max. */ 1550 if (ani->noise_immunity_level < 4) { 1551 ani->noise_immunity_level++; 1552 ops->set_noise_immunity_level(sc, ani->noise_immunity_level); 1553 return; 1554 } 1555 1556 #ifndef IEEE80211_STA_ONLY 1557 if (sc->sc_ic.ic_opmode == IEEE80211_M_HOSTAP) { 1558 if (ani->firstep_level < 2) { 1559 ani->firstep_level++; 1560 ops->set_firstep_level(sc, ani->firstep_level); 1561 } 1562 return; 1563 } 1564 #endif 1565 rssi = athn_ani_get_rssi(sc); 1566 if (rssi > ATHN_ANI_RSSI_THR_LOW) { 1567 /* 1568 * Beacon RSSI is in mid or high range, raise first step 1569 * level. 1570 */ 1571 if (ani->firstep_level < 2) { 1572 ani->firstep_level++; 1573 ops->set_firstep_level(sc, ani->firstep_level); 1574 } 1575 } else if (IEEE80211_IS_CHAN_2GHZ(sc->sc_ic.ic_bss->ni_chan)) { 1576 /* 1577 * Beacon RSSI is low, zero first step level to maximize 1578 * CCK sensitivity. 1579 */ 1580 if (ani->firstep_level > 0) { 1581 ani->firstep_level = 0; 1582 ops->set_firstep_level(sc, 0); 1583 } 1584 } 1585 } 1586 1587 void 1588 athn_ani_lower_immunity(struct athn_softc *sc) 1589 { 1590 struct athn_ani *ani = &sc->ani; 1591 struct athn_ops *ops = &sc->ops; 1592 int32_t rssi; 1593 1594 #ifndef IEEE80211_STA_ONLY 1595 if (sc->sc_ic.ic_opmode == IEEE80211_M_HOSTAP) { 1596 if (ani->firstep_level > 0) { 1597 ani->firstep_level--; 1598 ops->set_firstep_level(sc, ani->firstep_level); 1599 } 1600 return; 1601 } 1602 #endif 1603 rssi = athn_ani_get_rssi(sc); 1604 if (rssi > ATHN_ANI_RSSI_THR_HIGH) { 1605 /* 1606 * Beacon RSSI is high, leave OFDM weak signal detection 1607 * off or it may oscillate. 1608 */ 1609 } else if (rssi > ATHN_ANI_RSSI_THR_LOW) { 1610 /* 1611 * Beacon RSSI is in mid range, turn on OFDM weak signal 1612 * detection or lower first step level. 1613 */ 1614 if (!ani->ofdm_weak_signal) { 1615 ani->ofdm_weak_signal = 1; 1616 ops->enable_ofdm_weak_signal(sc); 1617 return; 1618 } 1619 if (ani->firstep_level > 0) { 1620 ani->firstep_level--; 1621 ops->set_firstep_level(sc, ani->firstep_level); 1622 return; 1623 } 1624 } else { 1625 /* Beacon RSSI is low, lower first step level. */ 1626 if (ani->firstep_level > 0) { 1627 ani->firstep_level--; 1628 ops->set_firstep_level(sc, ani->firstep_level); 1629 return; 1630 } 1631 } 1632 /* 1633 * Lower spur immunity level down to zero, or if all else fails, 1634 * lower noise immunity level down to zero. 1635 */ 1636 if (ani->spur_immunity_level > 0) { 1637 ani->spur_immunity_level--; 1638 ops->set_spur_immunity_level(sc, ani->spur_immunity_level); 1639 } else if (ani->noise_immunity_level > 0) { 1640 ani->noise_immunity_level--; 1641 ops->set_noise_immunity_level(sc, ani->noise_immunity_level); 1642 } 1643 } 1644 1645 void 1646 athn_ani_restart(struct athn_softc *sc) 1647 { 1648 struct athn_ani *ani = &sc->ani; 1649 1650 AR_WRITE(sc, AR_PHY_ERR_1, 0); 1651 AR_WRITE(sc, AR_PHY_ERR_2, 0); 1652 AR_WRITE(sc, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING); 1653 AR_WRITE(sc, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING); 1654 AR_WRITE_BARRIER(sc); 1655 1656 ani->listen_time = 0; 1657 ani->ofdm_phy_err_count = 0; 1658 ani->cck_phy_err_count = 0; 1659 } 1660 1661 void 1662 athn_ani_monitor(struct athn_softc *sc) 1663 { 1664 struct athn_ani *ani = &sc->ani; 1665 uint32_t cyccnt, txfcnt, rxfcnt, phy1, phy2; 1666 int32_t cycdelta, txfdelta, rxfdelta; 1667 int32_t listen_time; 1668 1669 txfcnt = AR_READ(sc, AR_TFCNT); /* Tx frame count. */ 1670 rxfcnt = AR_READ(sc, AR_RFCNT); /* Rx frame count. */ 1671 cyccnt = AR_READ(sc, AR_CCCNT); /* Cycle count. */ 1672 1673 if (ani->cyccnt != 0 && ani->cyccnt <= cyccnt) { 1674 cycdelta = cyccnt - ani->cyccnt; 1675 txfdelta = txfcnt - ani->txfcnt; 1676 rxfdelta = rxfcnt - ani->rxfcnt; 1677 1678 listen_time = (cycdelta - txfdelta - rxfdelta) / 1679 (athn_clock_rate(sc) * 1000); 1680 } else 1681 listen_time = 0; 1682 1683 ani->cyccnt = cyccnt; 1684 ani->txfcnt = txfcnt; 1685 ani->rxfcnt = rxfcnt; 1686 1687 if (listen_time < 0) { 1688 athn_ani_restart(sc); 1689 return; 1690 } 1691 ani->listen_time += listen_time; 1692 1693 phy1 = AR_READ(sc, AR_PHY_ERR_1); 1694 phy2 = AR_READ(sc, AR_PHY_ERR_2); 1695 1696 if (phy1 < ani->ofdm_phy_err_base) { 1697 AR_WRITE(sc, AR_PHY_ERR_1, ani->ofdm_phy_err_base); 1698 AR_WRITE(sc, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING); 1699 } 1700 if (phy2 < ani->cck_phy_err_base) { 1701 AR_WRITE(sc, AR_PHY_ERR_2, ani->cck_phy_err_base); 1702 AR_WRITE(sc, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING); 1703 } 1704 if (phy1 < ani->ofdm_phy_err_base || phy2 < ani->cck_phy_err_base) { 1705 AR_WRITE_BARRIER(sc); 1706 return; 1707 } 1708 ani->ofdm_phy_err_count = phy1 - ani->ofdm_phy_err_base; 1709 ani->cck_phy_err_count = phy2 - ani->cck_phy_err_base; 1710 1711 if (ani->listen_time > 5 * ATHN_ANI_PERIOD) { 1712 /* Check to see if we need to lower immunity. */ 1713 if (ani->ofdm_phy_err_count <= 1714 ani->listen_time * ani->ofdm_trig_low / 1000 && 1715 ani->cck_phy_err_count <= 1716 ani->listen_time * ani->cck_trig_low / 1000) 1717 athn_ani_lower_immunity(sc); 1718 athn_ani_restart(sc); 1719 1720 } else if (ani->listen_time > ATHN_ANI_PERIOD) { 1721 /* Check to see if we need to raise immunity. */ 1722 if (ani->ofdm_phy_err_count > 1723 ani->listen_time * ani->ofdm_trig_high / 1000) { 1724 athn_ani_ofdm_err_trigger(sc); 1725 athn_ani_restart(sc); 1726 } else if (ani->cck_phy_err_count > 1727 ani->listen_time * ani->cck_trig_high / 1000) { 1728 athn_ani_cck_err_trigger(sc); 1729 athn_ani_restart(sc); 1730 } 1731 } 1732 } 1733 1734 uint8_t 1735 athn_chan2fbin(struct ieee80211_channel *c) 1736 { 1737 if (IEEE80211_IS_CHAN_2GHZ(c)) 1738 return (c->ic_freq - 2300); 1739 else 1740 return ((c->ic_freq - 4800) / 5); 1741 } 1742 1743 int 1744 athn_interpolate(int x, int x1, int y1, int x2, int y2) 1745 { 1746 if (x1 == x2) /* Prevents division by zero. */ 1747 return (y1); 1748 /* Linear interpolation. */ 1749 return (y1 + ((x - x1) * (y2 - y1)) / (x2 - x1)); 1750 } 1751 1752 void 1753 athn_get_pier_ival(uint8_t fbin, const uint8_t *pierfreq, int npiers, 1754 int *lo, int *hi) 1755 { 1756 int i; 1757 1758 for (i = 0; i < npiers; i++) 1759 if (pierfreq[i] == AR_BCHAN_UNUSED || 1760 pierfreq[i] > fbin) 1761 break; 1762 *hi = i; 1763 *lo = *hi - 1; 1764 if (*lo == -1) 1765 *lo = *hi; 1766 else if (*hi == npiers || pierfreq[*hi] == AR_BCHAN_UNUSED) 1767 *hi = *lo; 1768 } 1769 1770 void 1771 athn_init_dma(struct athn_softc *sc) 1772 { 1773 uint32_t reg; 1774 1775 if (!AR_SREV_9380_10_OR_LATER(sc)) { 1776 /* Set AHB not to do cacheline prefetches. */ 1777 AR_SETBITS(sc, AR_AHB_MODE, AR_AHB_PREFETCH_RD_EN); 1778 } 1779 reg = AR_READ(sc, AR_TXCFG); 1780 /* Let MAC DMA reads be in 128-byte chunks. */ 1781 reg = RW(reg, AR_TXCFG_DMASZ, AR_DMASZ_128B); 1782 1783 /* Set initial Tx trigger level. */ 1784 if (AR_SREV_9285(sc) || AR_SREV_9271(sc)) 1785 reg = RW(reg, AR_TXCFG_FTRIG, AR_TXCFG_FTRIG_256B); 1786 else if (!AR_SREV_9380_10_OR_LATER(sc)) 1787 reg = RW(reg, AR_TXCFG_FTRIG, AR_TXCFG_FTRIG_512B); 1788 AR_WRITE(sc, AR_TXCFG, reg); 1789 1790 /* Let MAC DMA writes be in 128-byte chunks. */ 1791 reg = AR_READ(sc, AR_RXCFG); 1792 reg = RW(reg, AR_RXCFG_DMASZ, AR_DMASZ_128B); 1793 AR_WRITE(sc, AR_RXCFG, reg); 1794 1795 /* Setup Rx FIFO threshold to hold off Tx activities. */ 1796 AR_WRITE(sc, AR_RXFIFO_CFG, 512); 1797 1798 /* Reduce the number of entries in PCU TXBUF to avoid wrap around. */ 1799 if (AR_SREV_9285(sc)) { 1800 AR_WRITE(sc, AR_PCU_TXBUF_CTRL, 1801 AR9285_PCU_TXBUF_CTRL_USABLE_SIZE); 1802 } else if (!AR_SREV_9271(sc)) { 1803 AR_WRITE(sc, AR_PCU_TXBUF_CTRL, 1804 AR_PCU_TXBUF_CTRL_USABLE_SIZE); 1805 } 1806 AR_WRITE_BARRIER(sc); 1807 1808 /* Reset Tx status ring. */ 1809 if (AR_SREV_9380_10_OR_LATER(sc)) 1810 ar9003_reset_txsring(sc); 1811 } 1812 1813 void 1814 athn_inc_tx_trigger_level(struct athn_softc *sc) 1815 { 1816 uint32_t reg, ftrig; 1817 1818 reg = AR_READ(sc, AR_TXCFG); 1819 ftrig = MS(reg, AR_TXCFG_FTRIG); 1820 /* 1821 * NB: The AR9285 and all single-stream parts have an issue that 1822 * limits the size of the PCU Tx FIFO to 2KB instead of 4KB. 1823 */ 1824 if (ftrig == ((AR_SREV_9285(sc) || AR_SREV_9271(sc)) ? 0x1f : 0x3f)) 1825 return; /* Already at max. */ 1826 reg = RW(reg, AR_TXCFG_FTRIG, ftrig + 1); 1827 AR_WRITE(sc, AR_TXCFG, reg); 1828 AR_WRITE_BARRIER(sc); 1829 } 1830 1831 int 1832 athn_stop_rx_dma(struct athn_softc *sc) 1833 { 1834 int ntries; 1835 1836 AR_WRITE(sc, AR_CR, AR_CR_RXD); 1837 /* Wait for Rx enable bit to go low. */ 1838 for (ntries = 0; ntries < 100; ntries++) { 1839 if (!(AR_READ(sc, AR_CR) & AR_CR_RXE)) 1840 return (0); 1841 DELAY(100); 1842 } 1843 DPRINTF(("Rx DMA failed to stop\n")); 1844 return (ETIMEDOUT); 1845 } 1846 1847 int 1848 athn_rx_abort(struct athn_softc *sc) 1849 { 1850 int ntries; 1851 1852 AR_SETBITS(sc, AR_DIAG_SW, AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT); 1853 for (ntries = 0; ntries < 1000; ntries++) { 1854 if (MS(AR_READ(sc, AR_OBS_BUS_1), AR_OBS_BUS_1_RX_STATE) == 0) 1855 return (0); 1856 DELAY(10); 1857 } 1858 DPRINTF(("Rx failed to go idle in 10ms\n")); 1859 AR_CLRBITS(sc, AR_DIAG_SW, AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT); 1860 AR_WRITE_BARRIER(sc); 1861 return (ETIMEDOUT); 1862 } 1863 1864 void 1865 athn_tx_reclaim(struct athn_softc *sc, int qid) 1866 { 1867 struct athn_txq *txq = &sc->txq[qid]; 1868 struct athn_tx_buf *bf; 1869 1870 /* Reclaim all buffers queued in the specified Tx queue. */ 1871 /* NB: Tx DMA must be stopped. */ 1872 while ((bf = SIMPLEQ_FIRST(&txq->head)) != NULL) { 1873 SIMPLEQ_REMOVE_HEAD(&txq->head, bf_list); 1874 1875 bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0, 1876 bf->bf_map->dm_mapsize, BUS_DMASYNC_POSTWRITE); 1877 bus_dmamap_unload(sc->sc_dmat, bf->bf_map); 1878 m_freem(bf->bf_m); 1879 bf->bf_m = NULL; 1880 bf->bf_ni = NULL; /* Nodes already freed! */ 1881 1882 /* Link Tx buffer back to global free list. */ 1883 SIMPLEQ_INSERT_TAIL(&sc->txbufs, bf, bf_list); 1884 } 1885 } 1886 1887 int 1888 athn_tx_pending(struct athn_softc *sc, int qid) 1889 { 1890 return (MS(AR_READ(sc, AR_QSTS(qid)), AR_Q_STS_PEND_FR_CNT) != 0 || 1891 (AR_READ(sc, AR_Q_TXE) & (1 << qid)) != 0); 1892 } 1893 1894 void 1895 athn_stop_tx_dma(struct athn_softc *sc, int qid) 1896 { 1897 uint32_t tsflo; 1898 int ntries, i; 1899 1900 AR_WRITE(sc, AR_Q_TXD, 1 << qid); 1901 for (ntries = 0; ntries < 40; ntries++) { 1902 if (!athn_tx_pending(sc, qid)) 1903 break; 1904 DELAY(100); 1905 } 1906 if (ntries == 40) { 1907 for (i = 0; i < 2; i++) { 1908 tsflo = AR_READ(sc, AR_TSF_L32) / 1024; 1909 AR_WRITE(sc, AR_QUIET2, 1910 SM(AR_QUIET2_QUIET_DUR, 10)); 1911 AR_WRITE(sc, AR_QUIET_PERIOD, 100); 1912 AR_WRITE(sc, AR_NEXT_QUIET_TIMER, tsflo); 1913 AR_SETBITS(sc, AR_TIMER_MODE, AR_QUIET_TIMER_EN); 1914 if (AR_READ(sc, AR_TSF_L32) / 1024 == tsflo) 1915 break; 1916 } 1917 AR_SETBITS(sc, AR_DIAG_SW, AR_DIAG_FORCE_CH_IDLE_HIGH); 1918 AR_WRITE_BARRIER(sc); 1919 DELAY(200); 1920 AR_CLRBITS(sc, AR_TIMER_MODE, AR_QUIET_TIMER_EN); 1921 AR_WRITE_BARRIER(sc); 1922 1923 for (ntries = 0; ntries < 40; ntries++) { 1924 if (!athn_tx_pending(sc, qid)) 1925 break; 1926 DELAY(100); 1927 } 1928 1929 AR_CLRBITS(sc, AR_DIAG_SW, AR_DIAG_FORCE_CH_IDLE_HIGH); 1930 } 1931 AR_WRITE(sc, AR_Q_TXD, 0); 1932 AR_WRITE_BARRIER(sc); 1933 } 1934 1935 int 1936 athn_txtime(struct athn_softc *sc, int len, int ridx, u_int flags) 1937 { 1938 struct ieee80211com *ic = &sc->sc_ic; 1939 #define divround(a, b) (((a) + (b) - 1) / (b)) 1940 int txtime; 1941 1942 if (athn_rates[ridx].hwrate & 0x80) { /* MCS */ 1943 /* Assumes a 20MHz channel, HT-mixed frame format, no STBC. */ 1944 txtime = 8 + 8 + 4 + 4 + 4 * 4 + 8 /* HT PLCP */ 1945 + 4 * ((8 * len + 16 + 6) / (athn_rates[ridx].rate * 2)); 1946 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_bss->ni_chan)) 1947 txtime += 6; /* aSignalExtension */ 1948 } else if (athn_rates[ridx].phy == IEEE80211_T_OFDM) { 1949 txtime = divround(8 + 4 * len + 3, athn_rates[ridx].rate); 1950 /* SIFS is 10us for 11g but Signal Extension adds 6us. */ 1951 txtime = 16 + 4 + 4 * txtime + 16; 1952 } else { 1953 txtime = divround(16 * len, athn_rates[ridx].rate); 1954 if (ridx != ATHN_RIDX_CCK1 && (flags & IEEE80211_F_SHPREAMBLE)) 1955 txtime += 72 + 24; 1956 else 1957 txtime += 144 + 48; 1958 txtime += 10; /* 10us SIFS. */ 1959 } 1960 return (txtime); 1961 #undef divround 1962 } 1963 1964 void 1965 athn_init_tx_queues(struct athn_softc *sc) 1966 { 1967 int qid; 1968 1969 for (qid = 0; qid < ATHN_QID_COUNT; qid++) { 1970 SIMPLEQ_INIT(&sc->txq[qid].head); 1971 sc->txq[qid].lastds = NULL; 1972 sc->txq[qid].wait = NULL; 1973 sc->txq[qid].queued = 0; 1974 1975 AR_WRITE(sc, AR_DRETRY_LIMIT(qid), 1976 SM(AR_D_RETRY_LIMIT_STA_SH, 32) | 1977 SM(AR_D_RETRY_LIMIT_STA_LG, 32) | 1978 SM(AR_D_RETRY_LIMIT_FR_SH, 10)); 1979 AR_WRITE(sc, AR_QMISC(qid), 1980 AR_Q_MISC_DCU_EARLY_TERM_REQ); 1981 AR_WRITE(sc, AR_DMISC(qid), 1982 SM(AR_D_MISC_BKOFF_THRESH, 2) | 1983 AR_D_MISC_CW_BKOFF_EN | AR_D_MISC_FRAG_WAIT_EN); 1984 } 1985 1986 /* Init beacon queue. */ 1987 AR_SETBITS(sc, AR_QMISC(ATHN_QID_BEACON), 1988 AR_Q_MISC_FSP_DBA_GATED | AR_Q_MISC_BEACON_USE | 1989 AR_Q_MISC_CBR_INCR_DIS1); 1990 AR_SETBITS(sc, AR_DMISC(ATHN_QID_BEACON), 1991 SM(AR_D_MISC_ARB_LOCKOUT_CNTRL, 1992 AR_D_MISC_ARB_LOCKOUT_CNTRL_GLOBAL) | 1993 AR_D_MISC_BEACON_USE | 1994 AR_D_MISC_POST_FR_BKOFF_DIS); 1995 AR_WRITE(sc, AR_DLCL_IFS(ATHN_QID_BEACON), 1996 SM(AR_D_LCL_IFS_CWMIN, 0) | 1997 SM(AR_D_LCL_IFS_CWMAX, 0) | 1998 SM(AR_D_LCL_IFS_AIFS, 1)); 1999 2000 /* Init CAB (Content After Beacon) queue. */ 2001 AR_SETBITS(sc, AR_QMISC(ATHN_QID_CAB), 2002 AR_Q_MISC_FSP_DBA_GATED | AR_Q_MISC_CBR_INCR_DIS1 | 2003 AR_Q_MISC_CBR_INCR_DIS0); 2004 AR_SETBITS(sc, AR_DMISC(ATHN_QID_CAB), 2005 SM(AR_D_MISC_ARB_LOCKOUT_CNTRL, 2006 AR_D_MISC_ARB_LOCKOUT_CNTRL_GLOBAL)); 2007 2008 /* Init PS-Poll queue. */ 2009 AR_SETBITS(sc, AR_QMISC(ATHN_QID_PSPOLL), 2010 AR_Q_MISC_CBR_INCR_DIS1); 2011 2012 /* Init UAPSD queue. */ 2013 AR_SETBITS(sc, AR_DMISC(ATHN_QID_UAPSD), 2014 AR_D_MISC_POST_FR_BKOFF_DIS); 2015 2016 if (AR_SREV_9380_10_OR_LATER(sc)) { 2017 /* Enable MAC descriptor CRC check. */ 2018 AR_WRITE(sc, AR_Q_DESC_CRCCHK, AR_Q_DESC_CRCCHK_EN); 2019 } 2020 /* Enable DESC interrupts for all Tx queues. */ 2021 AR_WRITE(sc, AR_IMR_S0, 0x00ff0000); 2022 /* Enable EOL interrupts for all Tx queues except UAPSD. */ 2023 AR_WRITE(sc, AR_IMR_S1, 0x00df0000); 2024 AR_WRITE_BARRIER(sc); 2025 } 2026 2027 void 2028 athn_set_sta_timers(struct athn_softc *sc) 2029 { 2030 struct ieee80211com *ic = &sc->sc_ic; 2031 uint32_t tsfhi, tsflo, tsftu, reg; 2032 uint32_t intval, next_tbtt, next_dtim; 2033 int dtim_period, dtim_count, rem_dtim_count; 2034 2035 tsfhi = AR_READ(sc, AR_TSF_U32); 2036 tsflo = AR_READ(sc, AR_TSF_L32); 2037 tsftu = AR_TSF_TO_TU(tsfhi, tsflo) + AR_FUDGE; 2038 2039 /* Beacon interval in TU. */ 2040 intval = ic->ic_bss->ni_intval; 2041 2042 next_tbtt = roundup(tsftu, intval); 2043 #ifdef notyet 2044 dtim_period = ic->ic_dtim_period; 2045 if (dtim_period <= 0) 2046 #endif 2047 dtim_period = 1; /* Assume all TIMs are DTIMs. */ 2048 2049 #ifdef notyet 2050 dtim_count = ic->ic_dtim_count; 2051 if (dtim_count >= dtim_period) /* Should not happen. */ 2052 #endif 2053 dtim_count = 0; /* Assume last TIM was a DTIM. */ 2054 2055 /* Compute number of remaining TIMs until next DTIM. */ 2056 rem_dtim_count = 0; /* XXX */ 2057 next_dtim = next_tbtt + rem_dtim_count * intval; 2058 2059 AR_WRITE(sc, AR_NEXT_TBTT_TIMER, next_tbtt * IEEE80211_DUR_TU); 2060 AR_WRITE(sc, AR_BEACON_PERIOD, intval * IEEE80211_DUR_TU); 2061 AR_WRITE(sc, AR_DMA_BEACON_PERIOD, intval * IEEE80211_DUR_TU); 2062 2063 /* 2064 * Set the number of consecutive beacons to miss before raising 2065 * a BMISS interrupt to 10. 2066 */ 2067 reg = AR_READ(sc, AR_RSSI_THR); 2068 reg = RW(reg, AR_RSSI_THR_BM_THR, 10); 2069 AR_WRITE(sc, AR_RSSI_THR, reg); 2070 2071 AR_WRITE(sc, AR_NEXT_DTIM, 2072 (next_dtim - AR_SLEEP_SLOP) * IEEE80211_DUR_TU); 2073 AR_WRITE(sc, AR_NEXT_TIM, 2074 (next_tbtt - AR_SLEEP_SLOP) * IEEE80211_DUR_TU); 2075 2076 /* CAB timeout is in 1/8 TU. */ 2077 AR_WRITE(sc, AR_SLEEP1, 2078 SM(AR_SLEEP1_CAB_TIMEOUT, AR_CAB_TIMEOUT_VAL * 8) | 2079 AR_SLEEP1_ASSUME_DTIM); 2080 AR_WRITE(sc, AR_SLEEP2, 2081 SM(AR_SLEEP2_BEACON_TIMEOUT, AR_MIN_BEACON_TIMEOUT_VAL)); 2082 2083 AR_WRITE(sc, AR_TIM_PERIOD, intval * IEEE80211_DUR_TU); 2084 AR_WRITE(sc, AR_DTIM_PERIOD, dtim_period * intval * IEEE80211_DUR_TU); 2085 2086 AR_SETBITS(sc, AR_TIMER_MODE, 2087 AR_TBTT_TIMER_EN | AR_TIM_TIMER_EN | AR_DTIM_TIMER_EN); 2088 2089 /* Set TSF out-of-range threshold (fixed at 16k us). */ 2090 AR_WRITE(sc, AR_TSFOOR_THRESHOLD, 0x4240); 2091 2092 AR_WRITE_BARRIER(sc); 2093 } 2094 2095 #ifndef IEEE80211_STA_ONLY 2096 void 2097 athn_set_hostap_timers(struct athn_softc *sc) 2098 { 2099 struct ieee80211com *ic = &sc->sc_ic; 2100 uint32_t intval, next_tbtt; 2101 2102 /* Beacon interval in TU. */ 2103 intval = ic->ic_bss->ni_intval; 2104 next_tbtt = intval; 2105 2106 AR_WRITE(sc, AR_NEXT_TBTT_TIMER, next_tbtt * IEEE80211_DUR_TU); 2107 AR_WRITE(sc, AR_NEXT_DMA_BEACON_ALERT, 2108 (next_tbtt - AR_BEACON_DMA_DELAY) * IEEE80211_DUR_TU); 2109 AR_WRITE(sc, AR_NEXT_CFP, 2110 (next_tbtt - AR_SWBA_DELAY) * IEEE80211_DUR_TU); 2111 2112 AR_WRITE(sc, AR_BEACON_PERIOD, intval * IEEE80211_DUR_TU); 2113 AR_WRITE(sc, AR_DMA_BEACON_PERIOD, intval * IEEE80211_DUR_TU); 2114 AR_WRITE(sc, AR_SWBA_PERIOD, intval * IEEE80211_DUR_TU); 2115 AR_WRITE(sc, AR_NDP_PERIOD, intval * IEEE80211_DUR_TU); 2116 2117 AR_WRITE(sc, AR_TIMER_MODE, 2118 AR_TBTT_TIMER_EN | AR_DBA_TIMER_EN | AR_SWBA_TIMER_EN); 2119 2120 AR_WRITE_BARRIER(sc); 2121 } 2122 #endif 2123 2124 void 2125 athn_set_opmode(struct athn_softc *sc) 2126 { 2127 uint32_t reg; 2128 2129 switch (sc->sc_ic.ic_opmode) { 2130 #ifndef IEEE80211_STA_ONLY 2131 case IEEE80211_M_HOSTAP: 2132 reg = AR_READ(sc, AR_STA_ID1); 2133 reg &= ~AR_STA_ID1_ADHOC; 2134 reg |= AR_STA_ID1_STA_AP | AR_STA_ID1_KSRCH_MODE; 2135 AR_WRITE(sc, AR_STA_ID1, reg); 2136 2137 AR_CLRBITS(sc, AR_CFG, AR_CFG_AP_ADHOC_INDICATION); 2138 break; 2139 case IEEE80211_M_IBSS: 2140 case IEEE80211_M_AHDEMO: 2141 reg = AR_READ(sc, AR_STA_ID1); 2142 reg &= ~AR_STA_ID1_STA_AP; 2143 reg |= AR_STA_ID1_ADHOC | AR_STA_ID1_KSRCH_MODE; 2144 AR_WRITE(sc, AR_STA_ID1, reg); 2145 2146 AR_SETBITS(sc, AR_CFG, AR_CFG_AP_ADHOC_INDICATION); 2147 break; 2148 #endif 2149 default: 2150 reg = AR_READ(sc, AR_STA_ID1); 2151 reg &= ~(AR_STA_ID1_ADHOC | AR_STA_ID1_STA_AP); 2152 reg |= AR_STA_ID1_KSRCH_MODE; 2153 AR_WRITE(sc, AR_STA_ID1, reg); 2154 break; 2155 } 2156 AR_WRITE_BARRIER(sc); 2157 } 2158 2159 void 2160 athn_set_bss(struct athn_softc *sc, struct ieee80211_node *ni) 2161 { 2162 const uint8_t *bssid = ni->ni_bssid; 2163 2164 AR_WRITE(sc, AR_BSS_ID0, LE_READ_4(&bssid[0])); 2165 AR_WRITE(sc, AR_BSS_ID1, LE_READ_2(&bssid[4]) | 2166 SM(AR_BSS_ID1_AID, IEEE80211_AID(ni->ni_associd))); 2167 AR_WRITE_BARRIER(sc); 2168 } 2169 2170 void 2171 athn_enable_interrupts(struct athn_softc *sc) 2172 { 2173 uint32_t mask2; 2174 2175 athn_disable_interrupts(sc); /* XXX */ 2176 2177 AR_WRITE(sc, AR_IMR, sc->imask); 2178 2179 mask2 = AR_READ(sc, AR_IMR_S2); 2180 mask2 &= ~(AR_IMR_S2_TIM | AR_IMR_S2_DTIM | AR_IMR_S2_DTIMSYNC | 2181 AR_IMR_S2_CABEND | AR_IMR_S2_CABTO | AR_IMR_S2_TSFOOR); 2182 mask2 |= AR_IMR_S2_GTT | AR_IMR_S2_CST; 2183 AR_WRITE(sc, AR_IMR_S2, mask2); 2184 2185 AR_CLRBITS(sc, AR_IMR_S5, AR_IMR_S5_TIM_TIMER); 2186 2187 AR_WRITE(sc, AR_IER, AR_IER_ENABLE); 2188 2189 AR_WRITE(sc, AR_INTR_ASYNC_ENABLE, AR_INTR_MAC_IRQ); 2190 AR_WRITE(sc, AR_INTR_ASYNC_MASK, AR_INTR_MAC_IRQ); 2191 2192 AR_WRITE(sc, AR_INTR_SYNC_ENABLE, sc->isync); 2193 AR_WRITE(sc, AR_INTR_SYNC_MASK, sc->isync); 2194 AR_WRITE_BARRIER(sc); 2195 } 2196 2197 void 2198 athn_disable_interrupts(struct athn_softc *sc) 2199 { 2200 AR_WRITE(sc, AR_IER, 0); 2201 (void)AR_READ(sc, AR_IER); 2202 2203 AR_WRITE(sc, AR_INTR_ASYNC_ENABLE, 0); 2204 (void)AR_READ(sc, AR_INTR_ASYNC_ENABLE); 2205 2206 AR_WRITE(sc, AR_INTR_SYNC_ENABLE, 0); 2207 (void)AR_READ(sc, AR_INTR_SYNC_ENABLE); 2208 2209 AR_WRITE(sc, AR_IMR, 0); 2210 2211 AR_CLRBITS(sc, AR_IMR_S2, AR_IMR_S2_TIM | AR_IMR_S2_DTIM | 2212 AR_IMR_S2_DTIMSYNC | AR_IMR_S2_CABEND | AR_IMR_S2_CABTO | 2213 AR_IMR_S2_TSFOOR | AR_IMR_S2_GTT | AR_IMR_S2_CST); 2214 2215 AR_CLRBITS(sc, AR_IMR_S5, AR_IMR_S5_TIM_TIMER); 2216 AR_WRITE_BARRIER(sc); 2217 } 2218 2219 void 2220 athn_init_qos(struct athn_softc *sc) 2221 { 2222 /* Initialize QoS settings. */ 2223 AR_WRITE(sc, AR_MIC_QOS_CONTROL, 0x100aa); 2224 AR_WRITE(sc, AR_MIC_QOS_SELECT, 0x3210); 2225 AR_WRITE(sc, AR_QOS_NO_ACK, 2226 SM(AR_QOS_NO_ACK_TWO_BIT, 2) | 2227 SM(AR_QOS_NO_ACK_BIT_OFF, 5) | 2228 SM(AR_QOS_NO_ACK_BYTE_OFF, 0)); 2229 AR_WRITE(sc, AR_TXOP_X, AR_TXOP_X_VAL); 2230 /* Initialize TXOP for all TIDs. */ 2231 AR_WRITE(sc, AR_TXOP_0_3, 0xffffffff); 2232 AR_WRITE(sc, AR_TXOP_4_7, 0xffffffff); 2233 AR_WRITE(sc, AR_TXOP_8_11, 0xffffffff); 2234 AR_WRITE(sc, AR_TXOP_12_15, 0xffffffff); 2235 AR_WRITE_BARRIER(sc); 2236 } 2237 2238 int 2239 athn_hw_reset(struct athn_softc *sc, struct ieee80211_channel *c, 2240 struct ieee80211_channel *extc, int init) 2241 { 2242 struct ieee80211com *ic = &sc->sc_ic; 2243 struct athn_ops *ops = &sc->ops; 2244 uint32_t reg, def_ant, sta_id1, cfg_led, tsflo, tsfhi; 2245 int i, error; 2246 2247 /* XXX not if already awake */ 2248 if ((error = athn_set_power_awake(sc)) != 0) { 2249 printf("%s: could not wakeup chip\n", sc->sc_dev.dv_xname); 2250 return (error); 2251 } 2252 2253 /* Preserve the antenna on a channel switch. */ 2254 if ((def_ant = AR_READ(sc, AR_DEF_ANTENNA)) == 0) 2255 def_ant = 1; 2256 /* Preserve other registers. */ 2257 sta_id1 = AR_READ(sc, AR_STA_ID1) & AR_STA_ID1_BASE_RATE_11B; 2258 cfg_led = AR_READ(sc, AR_CFG_LED) & (AR_CFG_LED_ASSOC_CTL_M | 2259 AR_CFG_LED_MODE_SEL_M | AR_CFG_LED_BLINK_THRESH_SEL_M | 2260 AR_CFG_LED_BLINK_SLOW); 2261 2262 /* Mark PHY as inactive. */ 2263 ops->disable_phy(sc); 2264 2265 if (init && AR_SREV_9271(sc)) { 2266 AR_WRITE(sc, AR9271_RESET_POWER_DOWN_CONTROL, 2267 AR9271_RADIO_RF_RST); 2268 DELAY(50); 2269 } 2270 if (AR_SREV_9280(sc) && (sc->flags & ATHN_FLAG_OLPC)) { 2271 /* Save TSF before it gets cleared. */ 2272 tsfhi = AR_READ(sc, AR_TSF_U32); 2273 tsflo = AR_READ(sc, AR_TSF_L32); 2274 2275 /* NB: RTC reset clears TSF. */ 2276 error = athn_reset_power_on(sc); 2277 } else 2278 error = athn_reset(sc, 0); 2279 if (error != 0) { 2280 printf("%s: could not reset chip (error=%d)\n", 2281 sc->sc_dev.dv_xname, error); 2282 return (error); 2283 } 2284 2285 /* XXX not if already awake */ 2286 if ((error = athn_set_power_awake(sc)) != 0) { 2287 printf("%s: could not wakeup chip\n", sc->sc_dev.dv_xname); 2288 return (error); 2289 } 2290 2291 athn_init_pll(sc, c); 2292 ops->set_rf_mode(sc, c); 2293 2294 if (sc->flags & ATHN_FLAG_RFSILENT) { 2295 /* Check that the radio is not disabled by hardware switch. */ 2296 reg = ops->gpio_read(sc, sc->rfsilent_pin); 2297 if (sc->flags & ATHN_FLAG_RFSILENT_REVERSED) 2298 reg = !reg; 2299 if (!reg) { 2300 printf("%s: radio is disabled by hardware switch\n", 2301 sc->sc_dev.dv_xname); 2302 return (EPERM); 2303 } 2304 } 2305 if (init && AR_SREV_9271(sc)) { 2306 AR_WRITE(sc, AR9271_RESET_POWER_DOWN_CONTROL, 2307 AR9271_GATE_MAC_CTL); 2308 DELAY(50); 2309 } 2310 if (AR_SREV_9280(sc) && (sc->flags & ATHN_FLAG_OLPC)) { 2311 /* Restore TSF if it got cleared. */ 2312 AR_WRITE(sc, AR_TSF_L32, tsflo); 2313 AR_WRITE(sc, AR_TSF_U32, tsfhi); 2314 } 2315 2316 if (AR_SREV_9280_10_OR_LATER(sc)) 2317 AR_SETBITS(sc, sc->gpio_input_en_off, AR_GPIO_JTAG_DISABLE); 2318 2319 if (AR_SREV_9287_13_OR_LATER(sc) && !AR_SREV_9380_10_OR_LATER(sc)) 2320 ar9287_1_3_enable_async_fifo(sc); 2321 2322 /* Write init values to hardware. */ 2323 ops->hw_init(sc, c, extc); 2324 2325 /* 2326 * Only >=AR9280 2.0 parts are capable of encrypting unicast 2327 * management frames using CCMP. 2328 */ 2329 if (AR_SREV_9280_20_OR_LATER(sc)) { 2330 reg = AR_READ(sc, AR_AES_MUTE_MASK1); 2331 /* Do not mask the subtype field in management frames. */ 2332 reg = RW(reg, AR_AES_MUTE_MASK1_FC0_MGMT, 0xff); 2333 reg = RW(reg, AR_AES_MUTE_MASK1_FC1_MGMT, 2334 ~(IEEE80211_FC1_RETRY | IEEE80211_FC1_PWR_MGT | 2335 IEEE80211_FC1_MORE_DATA)); 2336 AR_WRITE(sc, AR_AES_MUTE_MASK1, reg); 2337 } else if (AR_SREV_9160_10_OR_LATER(sc)) { 2338 /* Disable hardware crypto for management frames. */ 2339 AR_CLRBITS(sc, AR_PCU_MISC_MODE2, 2340 AR_PCU_MISC_MODE2_MGMT_CRYPTO_ENABLE); 2341 AR_SETBITS(sc, AR_PCU_MISC_MODE2, 2342 AR_PCU_MISC_MODE2_NO_CRYPTO_FOR_NON_DATA_PKT); 2343 } 2344 2345 if (ic->ic_curmode != IEEE80211_MODE_11B) 2346 ops->set_delta_slope(sc, c, extc); 2347 2348 ops->spur_mitigate(sc, c, extc); 2349 ops->init_from_rom(sc, c, extc); 2350 2351 /* XXX */ 2352 AR_WRITE(sc, AR_STA_ID0, LE_READ_4(&ic->ic_myaddr[0])); 2353 AR_WRITE(sc, AR_STA_ID1, LE_READ_2(&ic->ic_myaddr[4]) | 2354 sta_id1 | AR_STA_ID1_RTS_USE_DEF | AR_STA_ID1_CRPT_MIC_ENABLE); 2355 2356 athn_set_opmode(sc); 2357 2358 AR_WRITE(sc, AR_BSSMSKL, 0xffffffff); 2359 AR_WRITE(sc, AR_BSSMSKU, 0xffff); 2360 2361 /* Restore previous antenna. */ 2362 AR_WRITE(sc, AR_DEF_ANTENNA, def_ant); 2363 2364 AR_WRITE(sc, AR_BSS_ID0, 0); 2365 AR_WRITE(sc, AR_BSS_ID1, 0); 2366 2367 AR_WRITE(sc, AR_ISR, 0xffffffff); 2368 2369 AR_WRITE(sc, AR_RSSI_THR, SM(AR_RSSI_THR_BM_THR, 7)); 2370 2371 if ((error = ops->set_synth(sc, c, extc)) != 0) { 2372 printf("%s: could not set channel\n", sc->sc_dev.dv_xname); 2373 return (error); 2374 } 2375 sc->curchan = c; 2376 sc->curchanext = extc; 2377 2378 for (i = 0; i < AR_NUM_DCU; i++) 2379 AR_WRITE(sc, AR_DQCUMASK(i), 1 << i); 2380 2381 athn_init_tx_queues(sc); 2382 2383 /* Initialize interrupt mask. */ 2384 sc->imask = 2385 AR_IMR_TXDESC | AR_IMR_TXEOL | 2386 AR_IMR_RXERR | AR_IMR_RXEOL | AR_IMR_RXORN | 2387 AR_IMR_RXMINTR | AR_IMR_RXINTM | 2388 AR_IMR_GENTMR | AR_IMR_BCNMISC; 2389 if (AR_SREV_9380_10_OR_LATER(sc)) 2390 sc->imask |= AR_IMR_RXERR | AR_IMR_HP_RXOK; 2391 #ifndef IEEE80211_STA_ONLY 2392 if (0 && ic->ic_opmode == IEEE80211_M_HOSTAP) 2393 sc->imask |= AR_IMR_MIB; 2394 #endif 2395 AR_WRITE(sc, AR_IMR, sc->imask); 2396 AR_SETBITS(sc, AR_IMR_S2, AR_IMR_S2_GTT); 2397 AR_WRITE(sc, AR_INTR_SYNC_CAUSE, 0xffffffff); 2398 sc->isync = AR_INTR_SYNC_DEFAULT; 2399 if (sc->flags & ATHN_FLAG_RFSILENT) 2400 sc->isync |= AR_INTR_SYNC_GPIO_PIN(sc->rfsilent_pin); 2401 AR_WRITE(sc, AR_INTR_SYNC_ENABLE, sc->isync); 2402 AR_WRITE(sc, AR_INTR_SYNC_MASK, 0); 2403 if (AR_SREV_9380_10_OR_LATER(sc)) { 2404 AR_WRITE(sc, AR_INTR_PRIO_ASYNC_ENABLE, 0); 2405 AR_WRITE(sc, AR_INTR_PRIO_ASYNC_MASK, 0); 2406 AR_WRITE(sc, AR_INTR_PRIO_SYNC_ENABLE, 0); 2407 AR_WRITE(sc, AR_INTR_PRIO_SYNC_MASK, 0); 2408 } 2409 2410 athn_init_qos(sc); 2411 2412 AR_SETBITS(sc, AR_PCU_MISC, AR_PCU_MIC_NEW_LOC_ENA); 2413 2414 athn_setsifs(sc); 2415 athn_updateslot(ic); 2416 athn_setclockrate(sc); 2417 if (AR_SREV_9287_13_OR_LATER(sc) && !AR_SREV_9380_10_OR_LATER(sc)) 2418 ar9287_1_3_setup_async_fifo(sc); 2419 2420 /* Disable sequence number generation in hardware. */ 2421 AR_SETBITS(sc, AR_STA_ID1, AR_STA_ID1_PRESERVE_SEQNUM); 2422 2423 athn_init_dma(sc); 2424 2425 /* Program observation bus to see MAC interrupts. */ 2426 AR_WRITE(sc, sc->obs_off, 8); 2427 2428 /* Setup Rx interrupt mitigation. */ 2429 AR_WRITE(sc, AR_RIMT, SM(AR_RIMT_FIRST, 2000) | SM(AR_RIMT_LAST, 500)); 2430 2431 ops->init_baseband(sc); 2432 2433 if ((error = athn_init_calib(sc, c, extc)) != 0) { 2434 printf("%s: could not initialize calibration\n", 2435 sc->sc_dev.dv_xname); 2436 return (error); 2437 } 2438 2439 ops->set_rxchains(sc); 2440 2441 AR_WRITE(sc, AR_CFG_LED, cfg_led | AR_CFG_SCLK_32KHZ); 2442 2443 if (sc->flags & ATHN_FLAG_USB) { 2444 if (AR_SREV_9271(sc)) 2445 AR_WRITE(sc, AR_CFG, AR_CFG_SWRB | AR_CFG_SWTB); 2446 else 2447 AR_WRITE(sc, AR_CFG, AR_CFG_SWTD | AR_CFG_SWRD); 2448 } 2449 #if BYTE_ORDER == BIG_ENDIAN 2450 else { 2451 /* Default is LE, turn on swapping for BE. */ 2452 AR_WRITE(sc, AR_CFG, AR_CFG_SWTD | AR_CFG_SWRD); 2453 } 2454 #endif 2455 AR_WRITE_BARRIER(sc); 2456 2457 return (0); 2458 } 2459 2460 struct ieee80211_node * 2461 athn_node_alloc(struct ieee80211com *ic) 2462 { 2463 struct athn_node *an; 2464 2465 an = malloc(sizeof(struct athn_node), M_DEVBUF, M_NOWAIT | M_ZERO); 2466 if (an && (ic->ic_flags & IEEE80211_F_HTON)) 2467 ieee80211_mira_node_init(&an->mn); 2468 return (struct ieee80211_node *)an; 2469 } 2470 2471 void 2472 athn_newassoc(struct ieee80211com *ic, struct ieee80211_node *ni, int isnew) 2473 { 2474 struct athn_softc *sc = ic->ic_softc; 2475 struct athn_node *an = (void *)ni; 2476 struct ieee80211_rateset *rs = &ni->ni_rates; 2477 uint8_t rate; 2478 int ridx, i, j; 2479 2480 if ((ni->ni_flags & IEEE80211_NODE_HT) == 0) 2481 ieee80211_amrr_node_init(&sc->amrr, &an->amn); 2482 else if (ic->ic_opmode == IEEE80211_M_STA) 2483 ieee80211_mira_node_init(&an->mn); 2484 2485 /* Start at lowest available bit-rate, AMRR will raise. */ 2486 ni->ni_txrate = 0; 2487 2488 for (i = 0; i < rs->rs_nrates; i++) { 2489 rate = rs->rs_rates[i] & IEEE80211_RATE_VAL; 2490 2491 /* Map 802.11 rate to HW rate index. */ 2492 for (ridx = 0; ridx <= ATHN_RIDX_MAX; ridx++) 2493 if (athn_rates[ridx].rate == rate) 2494 break; 2495 an->ridx[i] = ridx; 2496 DPRINTFN(2, ("rate %d index %d\n", rate, ridx)); 2497 2498 /* Compute fallback rate for retries. */ 2499 an->fallback[i] = i; 2500 for (j = i - 1; j >= 0; j--) { 2501 if (athn_rates[an->ridx[j]].phy == 2502 athn_rates[an->ridx[i]].phy) { 2503 an->fallback[i] = j; 2504 break; 2505 } 2506 } 2507 DPRINTFN(2, ("%d fallbacks to %d\n", i, an->fallback[i])); 2508 } 2509 2510 /* In 11n mode, start at lowest available bit-rate, MiRA will raise. */ 2511 ni->ni_txmcs = 0; 2512 2513 for (i = 0; i <= ATHN_MCS_MAX; i++) { 2514 /* Map MCS index to HW rate index. */ 2515 ridx = ATHN_NUM_LEGACY_RATES + i; 2516 an->ridx[ridx] = ATHN_RIDX_MCS0 + i; 2517 2518 DPRINTFN(2, ("mcs %d index %d ", i, ridx)); 2519 /* Compute fallback rate for retries. */ 2520 if (i == 0 || i == 8) { 2521 /* MCS 0 and 8 fall back to the lowest legacy rate. */ 2522 if (IEEE80211_IS_CHAN_5GHZ(ni->ni_chan)) 2523 an->fallback[ridx] = ATHN_RIDX_OFDM6; 2524 else 2525 an->fallback[ridx] = ATHN_RIDX_CCK1; 2526 } else { 2527 /* Other MCS fall back to next supported lower MCS. */ 2528 an->fallback[ridx] = ATHN_NUM_LEGACY_RATES + i; 2529 for (j = i - 1; j >= 0; j--) { 2530 if (!isset(ni->ni_rxmcs, j)) 2531 continue; 2532 an->fallback[ridx] = ATHN_NUM_LEGACY_RATES + j; 2533 break; 2534 } 2535 } 2536 DPRINTFN(2, (" fallback to %d\n", an->fallback[ridx])); 2537 } 2538 } 2539 2540 #ifndef IEEE80211_STA_ONLY 2541 void 2542 athn_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni) 2543 { 2544 struct athn_node *an = (void *)ni; 2545 if (ic->ic_flags & IEEE80211_F_HTON) 2546 ieee80211_mira_cancel_timeouts(&an->mn); 2547 } 2548 #endif 2549 2550 int 2551 athn_media_change(struct ifnet *ifp) 2552 { 2553 struct athn_softc *sc = ifp->if_softc; 2554 struct ieee80211com *ic = &sc->sc_ic; 2555 uint8_t rate, ridx; 2556 int error; 2557 2558 error = ieee80211_media_change(ifp); 2559 if (error != ENETRESET) 2560 return (error); 2561 2562 if (ic->ic_fixed_rate != -1) { 2563 rate = ic->ic_sup_rates[ic->ic_curmode]. 2564 rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL; 2565 /* Map 802.11 rate to HW rate index. */ 2566 for (ridx = 0; ridx <= ATHN_RIDX_MAX; ridx++) 2567 if (athn_rates[ridx].rate == rate) 2568 break; 2569 sc->fixed_ridx = ridx; 2570 } 2571 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 2572 (IFF_UP | IFF_RUNNING)) { 2573 athn_stop(ifp, 0); 2574 error = athn_init(ifp); 2575 } 2576 return (error); 2577 } 2578 2579 void 2580 athn_next_scan(void *arg) 2581 { 2582 struct athn_softc *sc = arg; 2583 struct ieee80211com *ic = &sc->sc_ic; 2584 int s; 2585 2586 s = splnet(); 2587 if (ic->ic_state == IEEE80211_S_SCAN) 2588 ieee80211_next_scan(&ic->ic_if); 2589 splx(s); 2590 } 2591 2592 void 2593 athn_iter_mira_delete(void *arg, struct ieee80211_node *ni) 2594 { 2595 struct athn_node *an = (struct athn_node *)ni; 2596 ieee80211_mira_cancel_timeouts(&an->mn); 2597 } 2598 2599 /* Delete pending timeouts managed by MiRA. */ 2600 void 2601 athn_delete_mira_nodes(struct athn_softc *sc) 2602 { 2603 struct ieee80211com *ic = &sc->sc_ic; 2604 2605 if (ic->ic_opmode == IEEE80211_M_STA) { 2606 struct athn_node *an = (struct athn_node *)ic->ic_bss; 2607 ieee80211_mira_cancel_timeouts(&an->mn); 2608 } else 2609 ieee80211_iterate_nodes(ic, athn_iter_mira_delete, sc); 2610 } 2611 2612 int 2613 athn_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 2614 { 2615 struct ifnet *ifp = &ic->ic_if; 2616 struct athn_softc *sc = ifp->if_softc; 2617 uint32_t reg; 2618 int error; 2619 2620 timeout_del(&sc->calib_to); 2621 2622 if ((ic->ic_flags & IEEE80211_F_HTON) && 2623 ic->ic_state == IEEE80211_S_RUN && nstate != IEEE80211_S_RUN) 2624 athn_delete_mira_nodes(sc); 2625 2626 switch (nstate) { 2627 case IEEE80211_S_INIT: 2628 athn_set_led(sc, 0); 2629 break; 2630 case IEEE80211_S_SCAN: 2631 /* Make the LED blink while scanning. */ 2632 athn_set_led(sc, !sc->led_state); 2633 error = athn_switch_chan(sc, ic->ic_bss->ni_chan, NULL); 2634 if (error != 0) 2635 return (error); 2636 timeout_add_msec(&sc->scan_to, 200); 2637 break; 2638 case IEEE80211_S_AUTH: 2639 athn_set_led(sc, 0); 2640 error = athn_switch_chan(sc, ic->ic_bss->ni_chan, NULL); 2641 if (error != 0) 2642 return (error); 2643 break; 2644 case IEEE80211_S_ASSOC: 2645 break; 2646 case IEEE80211_S_RUN: 2647 athn_set_led(sc, 1); 2648 2649 if (ic->ic_opmode == IEEE80211_M_MONITOR) 2650 break; 2651 2652 /* Fake a join to initialize the Tx rate. */ 2653 athn_newassoc(ic, ic->ic_bss, 1); 2654 2655 athn_set_bss(sc, ic->ic_bss); 2656 athn_disable_interrupts(sc); 2657 #ifndef IEEE80211_STA_ONLY 2658 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 2659 athn_set_hostap_timers(sc); 2660 /* Enable software beacon alert interrupts. */ 2661 sc->imask |= AR_IMR_SWBA; 2662 } else 2663 #endif 2664 { 2665 athn_set_sta_timers(sc); 2666 /* Enable beacon miss interrupts. */ 2667 sc->imask |= AR_IMR_BMISS; 2668 2669 /* Stop receiving beacons from other BSS. */ 2670 reg = AR_READ(sc, AR_RX_FILTER); 2671 reg = (reg & ~AR_RX_FILTER_BEACON) | 2672 AR_RX_FILTER_MYBEACON; 2673 AR_WRITE(sc, AR_RX_FILTER, reg); 2674 AR_WRITE_BARRIER(sc); 2675 } 2676 athn_enable_interrupts(sc); 2677 2678 if (sc->sup_calib_mask != 0) { 2679 memset(&sc->calib, 0, sizeof(sc->calib)); 2680 sc->cur_calib_mask = sc->sup_calib_mask; 2681 sc->ops.do_calib(sc); 2682 } 2683 /* XXX Start ANI. */ 2684 2685 athn_start_noisefloor_calib(sc, 1); 2686 timeout_add_msec(&sc->calib_to, 500); 2687 break; 2688 } 2689 2690 return (sc->sc_newstate(ic, nstate, arg)); 2691 } 2692 2693 void 2694 athn_updateedca(struct ieee80211com *ic) 2695 { 2696 #define ATHN_EXP2(x) ((1 << (x)) - 1) /* CWmin = 2^ECWmin - 1 */ 2697 struct athn_softc *sc = ic->ic_softc; 2698 const struct ieee80211_edca_ac_params *ac; 2699 int aci, qid; 2700 2701 for (aci = 0; aci < EDCA_NUM_AC; aci++) { 2702 ac = &ic->ic_edca_ac[aci]; 2703 qid = athn_ac2qid[aci]; 2704 2705 AR_WRITE(sc, AR_DLCL_IFS(qid), 2706 SM(AR_D_LCL_IFS_CWMIN, ATHN_EXP2(ac->ac_ecwmin)) | 2707 SM(AR_D_LCL_IFS_CWMAX, ATHN_EXP2(ac->ac_ecwmax)) | 2708 SM(AR_D_LCL_IFS_AIFS, ac->ac_aifsn)); 2709 if (ac->ac_txoplimit != 0) { 2710 AR_WRITE(sc, AR_DCHNTIME(qid), 2711 SM(AR_D_CHNTIME_DUR, 2712 IEEE80211_TXOP_TO_US(ac->ac_txoplimit)) | 2713 AR_D_CHNTIME_EN); 2714 } else 2715 AR_WRITE(sc, AR_DCHNTIME(qid), 0); 2716 } 2717 AR_WRITE_BARRIER(sc); 2718 #undef ATHN_EXP2 2719 } 2720 2721 int 2722 athn_clock_rate(struct athn_softc *sc) 2723 { 2724 struct ieee80211com *ic = &sc->sc_ic; 2725 int clockrate; /* MHz. */ 2726 2727 /* 2728 * AR9287 v1.3+ MAC runs at 117MHz (instead of 88/44MHz) when 2729 * ASYNC FIFO is enabled. 2730 */ 2731 if (AR_SREV_9287_13_OR_LATER(sc) && !AR_SREV_9380_10_OR_LATER(sc)) 2732 clockrate = 117; 2733 else if (ic->ic_bss->ni_chan != IEEE80211_CHAN_ANYC && 2734 IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan)) { 2735 if (sc->flags & ATHN_FLAG_FAST_PLL_CLOCK) 2736 clockrate = AR_CLOCK_RATE_FAST_5GHZ_OFDM; 2737 else 2738 clockrate = AR_CLOCK_RATE_5GHZ_OFDM; 2739 } else if (ic->ic_curmode == IEEE80211_MODE_11B) { 2740 clockrate = AR_CLOCK_RATE_CCK; 2741 } else 2742 clockrate = AR_CLOCK_RATE_2GHZ_OFDM; 2743 if (sc->curchanext != NULL) 2744 clockrate *= 2; 2745 2746 return (clockrate); 2747 } 2748 2749 int 2750 athn_chan_sifs(struct ieee80211_channel *c) 2751 { 2752 return IEEE80211_IS_CHAN_2GHZ(c) ? IEEE80211_DUR_DS_SIFS : 16; 2753 } 2754 2755 void 2756 athn_setsifs(struct athn_softc *sc) 2757 { 2758 int sifs = athn_chan_sifs(sc->sc_ic.ic_bss->ni_chan); 2759 AR_WRITE(sc, AR_D_GBL_IFS_SIFS, (sifs - 2) * athn_clock_rate(sc)); 2760 AR_WRITE_BARRIER(sc); 2761 } 2762 2763 int 2764 athn_acktimeout(struct ieee80211_channel *c, int slot) 2765 { 2766 int sifs = athn_chan_sifs(c); 2767 int ackto = sifs + slot; 2768 2769 /* Workaround for early ACK timeouts. */ 2770 if (IEEE80211_IS_CHAN_2GHZ(c)) 2771 ackto += 64 - sifs - slot; 2772 2773 return ackto; 2774 } 2775 2776 void 2777 athn_setacktimeout(struct athn_softc *sc, struct ieee80211_channel *c, int slot) 2778 { 2779 int ackto = athn_acktimeout(c, slot); 2780 uint32_t reg = AR_READ(sc, AR_TIME_OUT); 2781 reg = RW(reg, AR_TIME_OUT_ACK, ackto * athn_clock_rate(sc)); 2782 AR_WRITE(sc, AR_TIME_OUT, reg); 2783 AR_WRITE_BARRIER(sc); 2784 } 2785 2786 void 2787 athn_setctstimeout(struct athn_softc *sc, struct ieee80211_channel *c, int slot) 2788 { 2789 int ctsto = athn_acktimeout(c, slot); 2790 int sifs = athn_chan_sifs(c); 2791 uint32_t reg = AR_READ(sc, AR_TIME_OUT); 2792 2793 /* Workaround for early CTS timeouts. */ 2794 if (IEEE80211_IS_CHAN_2GHZ(c)) 2795 ctsto += 48 - sifs - slot; 2796 2797 reg = RW(reg, AR_TIME_OUT_CTS, ctsto * athn_clock_rate(sc)); 2798 AR_WRITE(sc, AR_TIME_OUT, reg); 2799 AR_WRITE_BARRIER(sc); 2800 } 2801 2802 void 2803 athn_setclockrate(struct athn_softc *sc) 2804 { 2805 int clockrate = athn_clock_rate(sc); 2806 uint32_t reg = AR_READ(sc, AR_USEC); 2807 reg = RW(reg, AR_USEC_USEC, clockrate - 1); 2808 AR_WRITE(sc, AR_USEC, reg); 2809 AR_WRITE_BARRIER(sc); 2810 } 2811 2812 void 2813 athn_updateslot(struct ieee80211com *ic) 2814 { 2815 struct athn_softc *sc = ic->ic_softc; 2816 int slot; 2817 2818 slot = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 2819 IEEE80211_DUR_DS_SHSLOT : IEEE80211_DUR_DS_SLOT; 2820 AR_WRITE(sc, AR_D_GBL_IFS_SLOT, slot * athn_clock_rate(sc)); 2821 AR_WRITE_BARRIER(sc); 2822 2823 athn_setacktimeout(sc, ic->ic_bss->ni_chan, slot); 2824 athn_setctstimeout(sc, ic->ic_bss->ni_chan, slot); 2825 } 2826 2827 void 2828 athn_start(struct ifnet *ifp) 2829 { 2830 struct athn_softc *sc = ifp->if_softc; 2831 struct ieee80211com *ic = &sc->sc_ic; 2832 struct ieee80211_node *ni; 2833 struct mbuf *m; 2834 2835 if (!(ifp->if_flags & IFF_RUNNING) || ifq_is_oactive(&ifp->if_snd)) 2836 return; 2837 2838 for (;;) { 2839 if (SIMPLEQ_EMPTY(&sc->txbufs)) { 2840 ifq_set_oactive(&ifp->if_snd); 2841 break; 2842 } 2843 /* Send pending management frames first. */ 2844 m = mq_dequeue(&ic->ic_mgtq); 2845 if (m != NULL) { 2846 ni = m->m_pkthdr.ph_cookie; 2847 goto sendit; 2848 } 2849 if (ic->ic_state != IEEE80211_S_RUN) 2850 break; 2851 2852 m = mq_dequeue(&ic->ic_pwrsaveq); 2853 if (m != NULL) { 2854 ni = m->m_pkthdr.ph_cookie; 2855 goto sendit; 2856 } 2857 if (ic->ic_state != IEEE80211_S_RUN) 2858 break; 2859 2860 /* Encapsulate and send data frames. */ 2861 IFQ_DEQUEUE(&ifp->if_snd, m); 2862 if (m == NULL) 2863 break; 2864 #if NBPFILTER > 0 2865 if (ifp->if_bpf != NULL) 2866 bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT); 2867 #endif 2868 if ((m = ieee80211_encap(ifp, m, &ni)) == NULL) 2869 continue; 2870 sendit: 2871 #if NBPFILTER > 0 2872 if (ic->ic_rawbpf != NULL) 2873 bpf_mtap(ic->ic_rawbpf, m, BPF_DIRECTION_OUT); 2874 #endif 2875 if (sc->ops.tx(sc, m, ni, 0) != 0) { 2876 ieee80211_release_node(ic, ni); 2877 ifp->if_oerrors++; 2878 continue; 2879 } 2880 2881 sc->sc_tx_timer = 5; 2882 ifp->if_timer = 1; 2883 } 2884 } 2885 2886 void 2887 athn_watchdog(struct ifnet *ifp) 2888 { 2889 struct athn_softc *sc = ifp->if_softc; 2890 2891 ifp->if_timer = 0; 2892 2893 if (sc->sc_tx_timer > 0) { 2894 if (--sc->sc_tx_timer == 0) { 2895 printf("%s: device timeout\n", sc->sc_dev.dv_xname); 2896 athn_stop(ifp, 1); 2897 (void)athn_init(ifp); 2898 ifp->if_oerrors++; 2899 return; 2900 } 2901 ifp->if_timer = 1; 2902 } 2903 2904 ieee80211_watchdog(ifp); 2905 } 2906 2907 void 2908 athn_set_multi(struct athn_softc *sc) 2909 { 2910 struct arpcom *ac = &sc->sc_ic.ic_ac; 2911 struct ifnet *ifp = &ac->ac_if; 2912 struct ether_multi *enm; 2913 struct ether_multistep step; 2914 const uint8_t *addr; 2915 uint32_t val, lo, hi; 2916 uint8_t bit; 2917 2918 if (ac->ac_multirangecnt > 0) 2919 ifp->if_flags |= IFF_ALLMULTI; 2920 2921 if ((ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) != 0) { 2922 lo = hi = 0xffffffff; 2923 goto done; 2924 } 2925 lo = hi = 0; 2926 ETHER_FIRST_MULTI(step, ac, enm); 2927 while (enm != NULL) { 2928 addr = enm->enm_addrlo; 2929 /* Calculate the XOR value of all eight 6-bit words. */ 2930 val = addr[0] | addr[1] << 8 | addr[2] << 16; 2931 bit = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val; 2932 val = addr[3] | addr[4] << 8 | addr[5] << 16; 2933 bit ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val; 2934 bit &= 0x3f; 2935 if (bit < 32) 2936 lo |= 1 << bit; 2937 else 2938 hi |= 1 << (bit - 32); 2939 ETHER_NEXT_MULTI(step, enm); 2940 } 2941 done: 2942 AR_WRITE(sc, AR_MCAST_FIL0, lo); 2943 AR_WRITE(sc, AR_MCAST_FIL1, hi); 2944 AR_WRITE_BARRIER(sc); 2945 } 2946 2947 int 2948 athn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 2949 { 2950 struct athn_softc *sc = ifp->if_softc; 2951 struct ieee80211com *ic = &sc->sc_ic; 2952 struct ifreq *ifr; 2953 int s, error = 0; 2954 2955 s = splnet(); 2956 2957 switch (cmd) { 2958 case SIOCSIFADDR: 2959 ifp->if_flags |= IFF_UP; 2960 /* FALLTHROUGH */ 2961 case SIOCSIFFLAGS: 2962 if (ifp->if_flags & IFF_UP) { 2963 if ((ifp->if_flags & IFF_RUNNING) && 2964 ((ifp->if_flags ^ sc->sc_if_flags) & 2965 (IFF_ALLMULTI | IFF_PROMISC)) != 0) { 2966 athn_set_multi(sc); 2967 } else if (!(ifp->if_flags & IFF_RUNNING)) 2968 error = athn_init(ifp); 2969 } else { 2970 if (ifp->if_flags & IFF_RUNNING) 2971 athn_stop(ifp, 1); 2972 } 2973 sc->sc_if_flags = ifp->if_flags; 2974 break; 2975 2976 case SIOCADDMULTI: 2977 case SIOCDELMULTI: 2978 ifr = (struct ifreq *)data; 2979 error = (cmd == SIOCADDMULTI) ? 2980 ether_addmulti(ifr, &ic->ic_ac) : 2981 ether_delmulti(ifr, &ic->ic_ac); 2982 if (error == ENETRESET) { 2983 athn_set_multi(sc); 2984 error = 0; 2985 } 2986 break; 2987 2988 case SIOCS80211CHANNEL: 2989 error = ieee80211_ioctl(ifp, cmd, data); 2990 if (error == ENETRESET && 2991 ic->ic_opmode == IEEE80211_M_MONITOR) { 2992 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 2993 (IFF_UP | IFF_RUNNING)) 2994 athn_switch_chan(sc, ic->ic_ibss_chan, NULL); 2995 error = 0; 2996 } 2997 break; 2998 2999 default: 3000 error = ieee80211_ioctl(ifp, cmd, data); 3001 } 3002 3003 if (error == ENETRESET) { 3004 error = 0; 3005 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 3006 (IFF_UP | IFF_RUNNING)) { 3007 athn_stop(ifp, 0); 3008 error = athn_init(ifp); 3009 } 3010 } 3011 3012 splx(s); 3013 return (error); 3014 } 3015 3016 int 3017 athn_init(struct ifnet *ifp) 3018 { 3019 struct athn_softc *sc = ifp->if_softc; 3020 struct athn_ops *ops = &sc->ops; 3021 struct ieee80211com *ic = &sc->sc_ic; 3022 struct ieee80211_channel *c, *extc; 3023 int i, error; 3024 3025 c = ic->ic_bss->ni_chan = ic->ic_ibss_chan; 3026 extc = NULL; 3027 3028 /* In case a new MAC address has been configured. */ 3029 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl)); 3030 3031 /* For CardBus, power on the socket. */ 3032 if (sc->sc_enable != NULL) { 3033 if ((error = sc->sc_enable(sc)) != 0) { 3034 printf("%s: could not enable device\n", 3035 sc->sc_dev.dv_xname); 3036 goto fail; 3037 } 3038 if ((error = athn_reset_power_on(sc)) != 0) { 3039 printf("%s: could not power on device\n", 3040 sc->sc_dev.dv_xname); 3041 goto fail; 3042 } 3043 } 3044 if (!(sc->flags & ATHN_FLAG_PCIE)) 3045 athn_config_nonpcie(sc); 3046 else 3047 athn_config_pcie(sc); 3048 3049 /* Reset HW key cache entries. */ 3050 for (i = 0; i < sc->kc_entries; i++) 3051 athn_reset_key(sc, i); 3052 3053 ops->enable_antenna_diversity(sc); 3054 3055 #ifdef ATHN_BT_COEXISTENCE 3056 /* Configure bluetooth coexistence for combo chips. */ 3057 if (sc->flags & ATHN_FLAG_BTCOEX) 3058 athn_btcoex_init(sc); 3059 #endif 3060 3061 /* Configure LED. */ 3062 athn_led_init(sc); 3063 3064 /* Configure hardware radio switch. */ 3065 if (sc->flags & ATHN_FLAG_RFSILENT) 3066 ops->rfsilent_init(sc); 3067 3068 if ((error = athn_hw_reset(sc, c, extc, 1)) != 0) { 3069 printf("%s: unable to reset hardware; reset status %d\n", 3070 sc->sc_dev.dv_xname, error); 3071 goto fail; 3072 } 3073 3074 /* Enable Rx. */ 3075 athn_rx_start(sc); 3076 3077 /* Enable interrupts. */ 3078 athn_enable_interrupts(sc); 3079 3080 #ifdef ATHN_BT_COEXISTENCE 3081 /* Enable bluetooth coexistence for combo chips. */ 3082 if (sc->flags & ATHN_FLAG_BTCOEX) 3083 athn_btcoex_enable(sc); 3084 #endif 3085 3086 ifq_clr_oactive(&ifp->if_snd); 3087 ifp->if_flags |= IFF_RUNNING; 3088 3089 #ifdef notyet 3090 if (ic->ic_flags & IEEE80211_F_WEPON) { 3091 /* Configure WEP keys. */ 3092 for (i = 0; i < IEEE80211_WEP_NKID; i++) 3093 athn_set_key(ic, NULL, &ic->ic_nw_keys[i]); 3094 } 3095 #endif 3096 if (ic->ic_opmode == IEEE80211_M_MONITOR) 3097 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 3098 else 3099 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 3100 3101 return (0); 3102 fail: 3103 athn_stop(ifp, 1); 3104 return (error); 3105 } 3106 3107 void 3108 athn_stop(struct ifnet *ifp, int disable) 3109 { 3110 struct athn_softc *sc = ifp->if_softc; 3111 struct ieee80211com *ic = &sc->sc_ic; 3112 int qid; 3113 3114 ifp->if_timer = sc->sc_tx_timer = 0; 3115 ifp->if_flags &= ~IFF_RUNNING; 3116 ifq_clr_oactive(&ifp->if_snd); 3117 3118 timeout_del(&sc->scan_to); 3119 3120 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 3121 3122 #ifdef ATHN_BT_COEXISTENCE 3123 /* Disable bluetooth coexistence for combo chips. */ 3124 if (sc->flags & ATHN_FLAG_BTCOEX) 3125 athn_btcoex_disable(sc); 3126 #endif 3127 3128 /* Disable interrupts. */ 3129 athn_disable_interrupts(sc); 3130 /* Acknowledge interrupts (avoids interrupt storms). */ 3131 AR_WRITE(sc, AR_INTR_SYNC_CAUSE, 0xffffffff); 3132 AR_WRITE(sc, AR_INTR_SYNC_MASK, 0); 3133 3134 for (qid = 0; qid < ATHN_QID_COUNT; qid++) 3135 athn_stop_tx_dma(sc, qid); 3136 /* XXX call athn_hw_reset if Tx still pending? */ 3137 for (qid = 0; qid < ATHN_QID_COUNT; qid++) 3138 athn_tx_reclaim(sc, qid); 3139 3140 /* Stop Rx. */ 3141 AR_SETBITS(sc, AR_DIAG_SW, AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT); 3142 AR_WRITE(sc, AR_MIBC, AR_MIBC_FMC); 3143 AR_WRITE(sc, AR_MIBC, AR_MIBC_CMC); 3144 AR_WRITE(sc, AR_FILT_OFDM, 0); 3145 AR_WRITE(sc, AR_FILT_CCK, 0); 3146 AR_WRITE_BARRIER(sc); 3147 athn_set_rxfilter(sc, 0); 3148 athn_stop_rx_dma(sc); 3149 3150 athn_reset(sc, 0); 3151 athn_init_pll(sc, NULL); 3152 athn_set_power_awake(sc); 3153 athn_reset(sc, 1); 3154 athn_init_pll(sc, NULL); 3155 3156 athn_set_power_sleep(sc); 3157 3158 /* For CardBus, power down the socket. */ 3159 if (disable && sc->sc_disable != NULL) 3160 sc->sc_disable(sc); 3161 } 3162 3163 void 3164 athn_suspend(struct athn_softc *sc) 3165 { 3166 struct ifnet *ifp = &sc->sc_ic.ic_if; 3167 3168 if (ifp->if_flags & IFF_RUNNING) 3169 athn_stop(ifp, 1); 3170 } 3171 3172 void 3173 athn_wakeup(struct athn_softc *sc) 3174 { 3175 struct ifnet *ifp = &sc->sc_ic.ic_if; 3176 3177 if (ifp->if_flags & IFF_UP) 3178 athn_init(ifp); 3179 } 3180