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