xref: /freebsd/sys/dev/ath/if_ath.c (revision 370f81fab6e1c49ec11480f965bb2bf1a2afc220)
15591b213SSam Leffler /*-
210ad9a77SSam Leffler  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
35591b213SSam Leffler  * All rights reserved.
45591b213SSam Leffler  *
55591b213SSam Leffler  * Redistribution and use in source and binary forms, with or without
65591b213SSam Leffler  * modification, are permitted provided that the following conditions
75591b213SSam Leffler  * are met:
85591b213SSam Leffler  * 1. Redistributions of source code must retain the above copyright
95591b213SSam Leffler  *    notice, this list of conditions and the following disclaimer,
105591b213SSam Leffler  *    without modification.
115591b213SSam Leffler  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
125591b213SSam Leffler  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
135591b213SSam Leffler  *    redistribution must be conditioned upon including a substantially
145591b213SSam Leffler  *    similar Disclaimer requirement for further binary redistribution.
155591b213SSam Leffler  *
165591b213SSam Leffler  * NO WARRANTY
175591b213SSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
185591b213SSam Leffler  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
195591b213SSam Leffler  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
205591b213SSam Leffler  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
215591b213SSam Leffler  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
225591b213SSam Leffler  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
235591b213SSam Leffler  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
245591b213SSam Leffler  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
255591b213SSam Leffler  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
265591b213SSam Leffler  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
275591b213SSam Leffler  * THE POSSIBILITY OF SUCH DAMAGES.
285591b213SSam Leffler  */
295591b213SSam Leffler 
305591b213SSam Leffler #include <sys/cdefs.h>
315591b213SSam Leffler __FBSDID("$FreeBSD$");
325591b213SSam Leffler 
335591b213SSam Leffler /*
345591b213SSam Leffler  * Driver for the Atheros Wireless LAN controller.
355f3721d5SSam Leffler  *
365f3721d5SSam Leffler  * This software is derived from work of Atsushi Onoe; his contribution
375f3721d5SSam Leffler  * is greatly appreciated.
385591b213SSam Leffler  */
395591b213SSam Leffler 
405591b213SSam Leffler #include "opt_inet.h"
41a585a9a1SSam Leffler #include "opt_ath.h"
423f3087fdSAdrian Chadd /*
433f3087fdSAdrian Chadd  * This is needed for register operations which are performed
443f3087fdSAdrian Chadd  * by the driver - eg, calls to ath_hal_gettsf32().
4558816f3fSAdrian Chadd  *
4658816f3fSAdrian Chadd  * It's also required for any AH_DEBUG checks in here, eg the
4758816f3fSAdrian Chadd  * module dependencies.
483f3087fdSAdrian Chadd  */
493f3087fdSAdrian Chadd #include "opt_ah.h"
50584f7327SSam Leffler #include "opt_wlan.h"
515591b213SSam Leffler 
525591b213SSam Leffler #include <sys/param.h>
535591b213SSam Leffler #include <sys/systm.h>
545591b213SSam Leffler #include <sys/sysctl.h>
555591b213SSam Leffler #include <sys/mbuf.h>
565591b213SSam Leffler #include <sys/malloc.h>
575591b213SSam Leffler #include <sys/lock.h>
585591b213SSam Leffler #include <sys/mutex.h>
595591b213SSam Leffler #include <sys/kernel.h>
605591b213SSam Leffler #include <sys/socket.h>
615591b213SSam Leffler #include <sys/sockio.h>
625591b213SSam Leffler #include <sys/errno.h>
635591b213SSam Leffler #include <sys/callout.h>
645591b213SSam Leffler #include <sys/bus.h>
655591b213SSam Leffler #include <sys/endian.h>
660bbf5441SSam Leffler #include <sys/kthread.h>
670bbf5441SSam Leffler #include <sys/taskqueue.h>
683fc21fedSSam Leffler #include <sys/priv.h>
69dba9c859SAdrian Chadd #include <sys/module.h>
70f52d3452SAdrian Chadd #include <sys/ktr.h>
71ddbe3036SAdrian Chadd #include <sys/smp.h>	/* for mp_ncpus */
725591b213SSam Leffler 
735591b213SSam Leffler #include <machine/bus.h>
745591b213SSam Leffler 
755591b213SSam Leffler #include <net/if.h>
765591b213SSam Leffler #include <net/if_dl.h>
775591b213SSam Leffler #include <net/if_media.h>
78fc74a9f9SBrooks Davis #include <net/if_types.h>
795591b213SSam Leffler #include <net/if_arp.h>
805591b213SSam Leffler #include <net/ethernet.h>
815591b213SSam Leffler #include <net/if_llc.h>
825591b213SSam Leffler 
835591b213SSam Leffler #include <net80211/ieee80211_var.h>
8459efa8b5SSam Leffler #include <net80211/ieee80211_regdomain.h>
85339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
86339ccfb3SSam Leffler #include <net80211/ieee80211_superg.h>
87339ccfb3SSam Leffler #endif
88584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
8910ad9a77SSam Leffler #include <net80211/ieee80211_tdma.h>
9010ad9a77SSam Leffler #endif
915591b213SSam Leffler 
925591b213SSam Leffler #include <net/bpf.h>
935591b213SSam Leffler 
945591b213SSam Leffler #ifdef INET
955591b213SSam Leffler #include <netinet/in.h>
965591b213SSam Leffler #include <netinet/if_ether.h>
975591b213SSam Leffler #endif
985591b213SSam Leffler 
995591b213SSam Leffler #include <dev/ath/if_athvar.h>
10033644623SSam Leffler #include <dev/ath/ath_hal/ah_devid.h>		/* XXX for softled */
1010dbe9289SAdrian Chadd #include <dev/ath/ath_hal/ah_diagcodes.h>
1025591b213SSam Leffler 
1035bc8125aSAdrian Chadd #include <dev/ath/if_ath_debug.h>
104b8e788a5SAdrian Chadd #include <dev/ath/if_ath_misc.h>
105e60c4fc2SAdrian Chadd #include <dev/ath/if_ath_tsf.h>
106b8e788a5SAdrian Chadd #include <dev/ath/if_ath_tx.h>
1076079fdbeSAdrian Chadd #include <dev/ath/if_ath_sysctl.h>
108c65ee21dSAdrian Chadd #include <dev/ath/if_ath_led.h>
109d2d7a00aSAdrian Chadd #include <dev/ath/if_ath_keycache.h>
110e60c4fc2SAdrian Chadd #include <dev/ath/if_ath_rx.h>
111f8cc9b09SAdrian Chadd #include <dev/ath/if_ath_rx_edma.h>
1123fdfc330SAdrian Chadd #include <dev/ath/if_ath_tx_edma.h>
113a35dae8dSAdrian Chadd #include <dev/ath/if_ath_beacon.h>
1149af351f9SAdrian Chadd #include <dev/ath/if_ath_spectral.h>
11548237774SAdrian Chadd #include <dev/ath/if_athdfs.h>
1165bc8125aSAdrian Chadd 
11786e07743SSam Leffler #ifdef ATH_TX99_DIAG
11886e07743SSam Leffler #include <dev/ath/ath_tx99/ath_tx99.h>
11986e07743SSam Leffler #endif
12086e07743SSam Leffler 
12189d2e576SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
122bdbb6e5bSAdrian Chadd #include <dev/ath/if_ath_alq.h>
123bdbb6e5bSAdrian Chadd #endif
124bdbb6e5bSAdrian Chadd 
125bdbb6e5bSAdrian Chadd /*
126bdbb6e5bSAdrian Chadd  * Only enable this if you're working on PS-POLL support.
127bdbb6e5bSAdrian Chadd  */
128bdbb6e5bSAdrian Chadd #undef	ATH_SW_PSQ
129bdbb6e5bSAdrian Chadd 
130b032f27cSSam Leffler /*
131b032f27cSSam Leffler  * ATH_BCBUF determines the number of vap's that can transmit
132b032f27cSSam Leffler  * beacons and also (currently) the number of vap's that can
133b032f27cSSam Leffler  * have unique mac addresses/bssid.  When staggering beacons
134b032f27cSSam Leffler  * 4 is probably a good max as otherwise the beacons become
135b032f27cSSam Leffler  * very closely spaced and there is limited time for cab q traffic
136b032f27cSSam Leffler  * to go out.  You can burst beacons instead but that is not good
137b032f27cSSam Leffler  * for stations in power save and at some point you really want
138b032f27cSSam Leffler  * another radio (and channel).
139b032f27cSSam Leffler  *
140b032f27cSSam Leffler  * The limit on the number of mac addresses is tied to our use of
141b032f27cSSam Leffler  * the U/L bit and tracking addresses in a byte; it would be
142b032f27cSSam Leffler  * worthwhile to allow more for applications like proxy sta.
143b032f27cSSam Leffler  */
144b032f27cSSam Leffler CTASSERT(ATH_BCBUF <= 8);
145b032f27cSSam Leffler 
146b032f27cSSam Leffler static struct ieee80211vap *ath_vap_create(struct ieee80211com *,
147fcd9500fSBernhard Schmidt 		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
148fcd9500fSBernhard Schmidt 		    const uint8_t [IEEE80211_ADDR_LEN],
149fcd9500fSBernhard Schmidt 		    const uint8_t [IEEE80211_ADDR_LEN]);
150b032f27cSSam Leffler static void	ath_vap_delete(struct ieee80211vap *);
1515591b213SSam Leffler static void	ath_init(void *);
152c42a7b7eSSam Leffler static void	ath_stop_locked(struct ifnet *);
1535591b213SSam Leffler static void	ath_stop(struct ifnet *);
154b032f27cSSam Leffler static int	ath_reset_vap(struct ieee80211vap *, u_long);
1551a85141aSAdrian Chadd static void	ath_start_queue(struct ifnet *ifp);
1565591b213SSam Leffler static int	ath_media_change(struct ifnet *);
1572e986da5SSam Leffler static void	ath_watchdog(void *);
1585591b213SSam Leffler static int	ath_ioctl(struct ifnet *, u_long, caddr_t);
1595591b213SSam Leffler static void	ath_fatal_proc(void *, int);
160b032f27cSSam Leffler static void	ath_bmiss_vap(struct ieee80211vap *);
1615591b213SSam Leffler static void	ath_bmiss_proc(void *, int);
162b032f27cSSam Leffler static void	ath_key_update_begin(struct ieee80211vap *);
163b032f27cSSam Leffler static void	ath_key_update_end(struct ieee80211vap *);
164b032f27cSSam Leffler static void	ath_update_mcast(struct ifnet *);
165b032f27cSSam Leffler static void	ath_update_promisc(struct ifnet *);
166c42a7b7eSSam Leffler static void	ath_updateslot(struct ifnet *);
167c42a7b7eSSam Leffler static void	ath_bstuck_proc(void *, int);
168d52f7132SAdrian Chadd static void	ath_reset_proc(void *, int);
1695591b213SSam Leffler static int	ath_desc_alloc(struct ath_softc *);
1705591b213SSam Leffler static void	ath_desc_free(struct ath_softc *);
17138c208f8SSam Leffler static struct ieee80211_node *ath_node_alloc(struct ieee80211vap *,
17238c208f8SSam Leffler 			const uint8_t [IEEE80211_ADDR_LEN]);
1734afa805eSAdrian Chadd static void	ath_node_cleanup(struct ieee80211_node *);
174c42a7b7eSSam Leffler static void	ath_node_free(struct ieee80211_node *);
17568e8e04eSSam Leffler static void	ath_node_getsignal(const struct ieee80211_node *,
17668e8e04eSSam Leffler 			int8_t *, int8_t *);
177622b3fd2SSam Leffler static void	ath_txq_init(struct ath_softc *sc, struct ath_txq *, int);
178c42a7b7eSSam Leffler static struct ath_txq *ath_txq_setup(struct ath_softc*, int qtype, int subtype);
179c42a7b7eSSam Leffler static int	ath_tx_setup(struct ath_softc *, int, int);
180c42a7b7eSSam Leffler static void	ath_tx_cleanupq(struct ath_softc *, struct ath_txq *);
181c42a7b7eSSam Leffler static void	ath_tx_cleanup(struct ath_softc *);
182788e6aa9SAdrian Chadd static int	ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq,
183788e6aa9SAdrian Chadd 		    int dosched);
184c42a7b7eSSam Leffler static void	ath_tx_proc_q0(void *, int);
185c42a7b7eSSam Leffler static void	ath_tx_proc_q0123(void *, int);
1865591b213SSam Leffler static void	ath_tx_proc(void *, int);
18703e9308fSAdrian Chadd static void	ath_txq_sched_tasklet(void *, int);
1885591b213SSam Leffler static int	ath_chan_set(struct ath_softc *, struct ieee80211_channel *);
189c42a7b7eSSam Leffler static void	ath_chan_change(struct ath_softc *, struct ieee80211_channel *);
19068e8e04eSSam Leffler static void	ath_scan_start(struct ieee80211com *);
19168e8e04eSSam Leffler static void	ath_scan_end(struct ieee80211com *);
19268e8e04eSSam Leffler static void	ath_set_channel(struct ieee80211com *);
193fdd72b4aSAdrian Chadd #ifdef	ATH_ENABLE_11N
194e7200579SAdrian Chadd static void	ath_update_chw(struct ieee80211com *);
195fdd72b4aSAdrian Chadd #endif	/* ATH_ENABLE_11N */
1965591b213SSam Leffler static void	ath_calibrate(void *);
197b032f27cSSam Leffler static int	ath_newstate(struct ieee80211vap *, enum ieee80211_state, int);
198e8fd88a3SSam Leffler static void	ath_setup_stationkey(struct ieee80211_node *);
199e9962332SSam Leffler static void	ath_newassoc(struct ieee80211_node *, int);
200b032f27cSSam Leffler static int	ath_setregdomain(struct ieee80211com *,
201b032f27cSSam Leffler 		    struct ieee80211_regdomain *, int,
202b032f27cSSam Leffler 		    struct ieee80211_channel []);
2035fe9f044SSam Leffler static void	ath_getradiocaps(struct ieee80211com *, int, int *,
204b032f27cSSam Leffler 		    struct ieee80211_channel []);
205b032f27cSSam Leffler static int	ath_getchannels(struct ath_softc *);
2065591b213SSam Leffler 
207c42a7b7eSSam Leffler static int	ath_rate_setup(struct ath_softc *, u_int mode);
2085591b213SSam Leffler static void	ath_setcurmode(struct ath_softc *, enum ieee80211_phymode);
209c42a7b7eSSam Leffler 
210c42a7b7eSSam Leffler static void	ath_announce(struct ath_softc *);
2115591b213SSam Leffler 
21248237774SAdrian Chadd static void	ath_dfs_tasklet(void *, int);
2130eb81626SAdrian Chadd static void	ath_node_powersave(struct ieee80211_node *, int);
214548a605dSAdrian Chadd static int	ath_node_set_tim(struct ieee80211_node *, int);
21548237774SAdrian Chadd 
216584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
217a35dae8dSAdrian Chadd #include <dev/ath/if_ath_tdma.h>
218a35dae8dSAdrian Chadd #endif
21910ad9a77SSam Leffler 
2205591b213SSam Leffler SYSCTL_DECL(_hw_ath);
2215591b213SSam Leffler 
2225591b213SSam Leffler /* XXX validate sysctl values */
2232dc7fcc4SSam Leffler static	int ath_longcalinterval = 30;		/* long cals every 30 secs */
2242dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, longcal, CTLFLAG_RW, &ath_longcalinterval,
2252dc7fcc4SSam Leffler 	    0, "long chip calibration interval (secs)");
2262dc7fcc4SSam Leffler static	int ath_shortcalinterval = 100;		/* short cals every 100 ms */
2272dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, shortcal, CTLFLAG_RW, &ath_shortcalinterval,
2282dc7fcc4SSam Leffler 	    0, "short chip calibration interval (msecs)");
2292dc7fcc4SSam Leffler static	int ath_resetcalinterval = 20*60;	/* reset cal state 20 mins */
2302dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, resetcal, CTLFLAG_RW, &ath_resetcalinterval,
2312dc7fcc4SSam Leffler 	    0, "reset chip calibration results (secs)");
232a108ab63SAdrian Chadd static	int ath_anicalinterval = 100;		/* ANI calibration - 100 msec */
233a108ab63SAdrian Chadd SYSCTL_INT(_hw_ath, OID_AUTO, anical, CTLFLAG_RW, &ath_anicalinterval,
234a108ab63SAdrian Chadd 	    0, "ANI calibration (msecs)");
2355591b213SSam Leffler 
2363d184db2SAdrian Chadd int ath_rxbuf = ATH_RXBUF;		/* # rx buffers to allocate */
237aaa70f2fSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, rxbuf, CTLFLAG_RW, &ath_rxbuf,
238e2d787faSSam Leffler 	    0, "rx buffers allocated");
239e2d787faSSam Leffler TUNABLE_INT("hw.ath.rxbuf", &ath_rxbuf);
2403d184db2SAdrian Chadd int ath_txbuf = ATH_TXBUF;		/* # tx buffers to allocate */
241aaa70f2fSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, txbuf, CTLFLAG_RW, &ath_txbuf,
242e2d787faSSam Leffler 	    0, "tx buffers allocated");
243e2d787faSSam Leffler TUNABLE_INT("hw.ath.txbuf", &ath_txbuf);
2443d184db2SAdrian Chadd int ath_txbuf_mgmt = ATH_MGMT_TXBUF;	/* # mgmt tx buffers to allocate */
245af33d486SAdrian Chadd SYSCTL_INT(_hw_ath, OID_AUTO, txbuf_mgmt, CTLFLAG_RW, &ath_txbuf_mgmt,
246af33d486SAdrian Chadd 	    0, "tx (mgmt) buffers allocated");
247af33d486SAdrian Chadd TUNABLE_INT("hw.ath.txbuf_mgmt", &ath_txbuf_mgmt);
248e2d787faSSam Leffler 
249a35dae8dSAdrian Chadd int ath_bstuck_threshold = 4;		/* max missed beacons */
250a32ac9d3SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, bstuck, CTLFLAG_RW, &ath_bstuck_threshold,
251a32ac9d3SSam Leffler 	    0, "max missed beacon xmits before chip reset");
252a32ac9d3SSam Leffler 
2536b349e5aSAdrian Chadd MALLOC_DEFINE(M_ATHDEV, "athdev", "ath driver dma buffers");
254c42a7b7eSSam Leffler 
255f8418db5SAdrian Chadd void
256f8418db5SAdrian Chadd ath_legacy_attach_comp_func(struct ath_softc *sc)
257f8418db5SAdrian Chadd {
258f8418db5SAdrian Chadd 
259f8418db5SAdrian Chadd 	/*
260f8418db5SAdrian Chadd 	 * Special case certain configurations.  Note the
261f8418db5SAdrian Chadd 	 * CAB queue is handled by these specially so don't
262f8418db5SAdrian Chadd 	 * include them when checking the txq setup mask.
263f8418db5SAdrian Chadd 	 */
264f8418db5SAdrian Chadd 	switch (sc->sc_txqsetup &~ (1<<sc->sc_cabq->axq_qnum)) {
265f8418db5SAdrian Chadd 	case 0x01:
266f8418db5SAdrian Chadd 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0, sc);
267f8418db5SAdrian Chadd 		break;
268f8418db5SAdrian Chadd 	case 0x0f:
269f8418db5SAdrian Chadd 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0123, sc);
270f8418db5SAdrian Chadd 		break;
271f8418db5SAdrian Chadd 	default:
272f8418db5SAdrian Chadd 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc, sc);
273f8418db5SAdrian Chadd 		break;
274f8418db5SAdrian Chadd 	}
275f8418db5SAdrian Chadd }
276f8418db5SAdrian Chadd 
27767397d39SAdrian Chadd #define	HAL_MODE_HT20 (HAL_MODE_11NG_HT20 | HAL_MODE_11NA_HT20)
27867397d39SAdrian Chadd #define	HAL_MODE_HT40 \
27967397d39SAdrian Chadd 	(HAL_MODE_11NG_HT40PLUS | HAL_MODE_11NG_HT40MINUS | \
28067397d39SAdrian Chadd 	HAL_MODE_11NA_HT40PLUS | HAL_MODE_11NA_HT40MINUS)
2815591b213SSam Leffler int
2825591b213SSam Leffler ath_attach(u_int16_t devid, struct ath_softc *sc)
2835591b213SSam Leffler {
284fc74a9f9SBrooks Davis 	struct ifnet *ifp;
285b032f27cSSam Leffler 	struct ieee80211com *ic;
286fc74a9f9SBrooks Davis 	struct ath_hal *ah = NULL;
2875591b213SSam Leffler 	HAL_STATUS status;
288c42a7b7eSSam Leffler 	int error = 0, i;
289411373ebSSam Leffler 	u_int wmodes;
29029aca940SSam Leffler 	uint8_t macaddr[IEEE80211_ADDR_LEN];
291a865860dSAdrian Chadd 	int rx_chainmask, tx_chainmask;
2925591b213SSam Leffler 
293c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: devid 0x%x\n", __func__, devid);
2945591b213SSam Leffler 
295a93c5097SAdrian Chadd 	CURVNET_SET(vnet0);
296b032f27cSSam Leffler 	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
297fc74a9f9SBrooks Davis 	if (ifp == NULL) {
298fc74a9f9SBrooks Davis 		device_printf(sc->sc_dev, "can not if_alloc()\n");
299fc74a9f9SBrooks Davis 		error = ENOSPC;
300bb327d28SAdrian Chadd 		CURVNET_RESTORE();
301fc74a9f9SBrooks Davis 		goto bad;
302fc74a9f9SBrooks Davis 	}
303b032f27cSSam Leffler 	ic = ifp->if_l2com;
304fc74a9f9SBrooks Davis 
3055591b213SSam Leffler 	/* set these up early for if_printf use */
3069bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(sc->sc_dev),
3079bf40edeSBrooks Davis 		device_get_unit(sc->sc_dev));
308a93c5097SAdrian Chadd 	CURVNET_RESTORE();
3095591b213SSam Leffler 
3107e97436bSAdrian Chadd 	ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh,
3117e97436bSAdrian Chadd 	    sc->sc_eepromdata, &status);
3125591b213SSam Leffler 	if (ah == NULL) {
3135591b213SSam Leffler 		if_printf(ifp, "unable to attach hardware; HAL status %u\n",
3145591b213SSam Leffler 			status);
3155591b213SSam Leffler 		error = ENXIO;
3165591b213SSam Leffler 		goto bad;
3175591b213SSam Leffler 	}
3185591b213SSam Leffler 	sc->sc_ah = ah;
319b58b3803SSam Leffler 	sc->sc_invalid = 0;	/* ready to go, enable interrupt handling */
3203297be13SSam Leffler #ifdef	ATH_DEBUG
3213297be13SSam Leffler 	sc->sc_debug = ath_debug;
3223297be13SSam Leffler #endif
3235591b213SSam Leffler 
3245591b213SSam Leffler 	/*
325f8cc9b09SAdrian Chadd 	 * Setup the DMA/EDMA functions based on the current
326f8cc9b09SAdrian Chadd 	 * hardware support.
327f8cc9b09SAdrian Chadd 	 *
328f8cc9b09SAdrian Chadd 	 * This is required before the descriptors are allocated.
329f8cc9b09SAdrian Chadd 	 */
3303d184db2SAdrian Chadd 	if (ath_hal_hasedma(sc->sc_ah)) {
3313d184db2SAdrian Chadd 		sc->sc_isedma = 1;
332f8cc9b09SAdrian Chadd 		ath_recv_setup_edma(sc);
3333fdfc330SAdrian Chadd 		ath_xmit_setup_edma(sc);
3343fdfc330SAdrian Chadd 	} else {
335f8cc9b09SAdrian Chadd 		ath_recv_setup_legacy(sc);
3363fdfc330SAdrian Chadd 		ath_xmit_setup_legacy(sc);
3373fdfc330SAdrian Chadd 	}
338f8cc9b09SAdrian Chadd 
339f8cc9b09SAdrian Chadd 	/*
340c42a7b7eSSam Leffler 	 * Check if the MAC has multi-rate retry support.
341c42a7b7eSSam Leffler 	 * We do this by trying to setup a fake extended
342c42a7b7eSSam Leffler 	 * descriptor.  MAC's that don't have support will
343c42a7b7eSSam Leffler 	 * return false w/o doing anything.  MAC's that do
344c42a7b7eSSam Leffler 	 * support it will return true w/o doing anything.
345c42a7b7eSSam Leffler 	 */
346c42a7b7eSSam Leffler 	sc->sc_mrretry = ath_hal_setupxtxdesc(ah, NULL, 0,0, 0,0, 0,0);
347c42a7b7eSSam Leffler 
348c42a7b7eSSam Leffler 	/*
349c42a7b7eSSam Leffler 	 * Check if the device has hardware counters for PHY
350c42a7b7eSSam Leffler 	 * errors.  If so we need to enable the MIB interrupt
351c42a7b7eSSam Leffler 	 * so we can act on stat triggers.
352c42a7b7eSSam Leffler 	 */
353c42a7b7eSSam Leffler 	if (ath_hal_hwphycounters(ah))
354c42a7b7eSSam Leffler 		sc->sc_needmib = 1;
355c42a7b7eSSam Leffler 
356c42a7b7eSSam Leffler 	/*
357c42a7b7eSSam Leffler 	 * Get the hardware key cache size.
358c42a7b7eSSam Leffler 	 */
359c42a7b7eSSam Leffler 	sc->sc_keymax = ath_hal_keycachesize(ah);
360e8fd88a3SSam Leffler 	if (sc->sc_keymax > ATH_KEYMAX) {
361e8fd88a3SSam Leffler 		if_printf(ifp, "Warning, using only %u of %u key cache slots\n",
362e8fd88a3SSam Leffler 			ATH_KEYMAX, sc->sc_keymax);
363e8fd88a3SSam Leffler 		sc->sc_keymax = ATH_KEYMAX;
364c42a7b7eSSam Leffler 	}
365c42a7b7eSSam Leffler 	/*
366c42a7b7eSSam Leffler 	 * Reset the key cache since some parts do not
367c42a7b7eSSam Leffler 	 * reset the contents on initial power up.
368c42a7b7eSSam Leffler 	 */
369c42a7b7eSSam Leffler 	for (i = 0; i < sc->sc_keymax; i++)
370c42a7b7eSSam Leffler 		ath_hal_keyreset(ah, i);
371c42a7b7eSSam Leffler 
372c42a7b7eSSam Leffler 	/*
373b032f27cSSam Leffler 	 * Collect the default channel list.
3745591b213SSam Leffler 	 */
375b032f27cSSam Leffler 	error = ath_getchannels(sc);
3765591b213SSam Leffler 	if (error != 0)
3775591b213SSam Leffler 		goto bad;
3785591b213SSam Leffler 
3795591b213SSam Leffler 	/*
3805591b213SSam Leffler 	 * Setup rate tables for all potential media types.
3815591b213SSam Leffler 	 */
3825591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11A);
3835591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11B);
3845591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11G);
385c42a7b7eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_A);
386c42a7b7eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_G);
38768e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_STURBO_A);
38868e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11NA);
38968e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11NG);
390724c193aSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_HALF);
391724c193aSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_QUARTER);
392aaa70f2fSSam Leffler 
393c42a7b7eSSam Leffler 	/* NB: setup here so ath_rate_update is happy */
394c42a7b7eSSam Leffler 	ath_setcurmode(sc, IEEE80211_MODE_11A);
3955591b213SSam Leffler 
396c42a7b7eSSam Leffler 	/*
3973fdfc330SAdrian Chadd 	 * Allocate TX descriptors and populate the lists.
398c42a7b7eSSam Leffler 	 */
3995591b213SSam Leffler 	error = ath_desc_alloc(sc);
4005591b213SSam Leffler 	if (error != 0) {
4013fdfc330SAdrian Chadd 		if_printf(ifp, "failed to allocate TX descriptors: %d\n",
4023fdfc330SAdrian Chadd 		    error);
4033fdfc330SAdrian Chadd 		goto bad;
4043fdfc330SAdrian Chadd 	}
4053fdfc330SAdrian Chadd 	error = ath_txdma_setup(sc);
4063fdfc330SAdrian Chadd 	if (error != 0) {
4073fdfc330SAdrian Chadd 		if_printf(ifp, "failed to allocate TX descriptors: %d\n",
4083fdfc330SAdrian Chadd 		    error);
4095591b213SSam Leffler 		goto bad;
4105591b213SSam Leffler 	}
4113d184db2SAdrian Chadd 
4123fdfc330SAdrian Chadd 	/*
4133fdfc330SAdrian Chadd 	 * Allocate RX descriptors and populate the lists.
4143fdfc330SAdrian Chadd 	 */
4153d184db2SAdrian Chadd 	error = ath_rxdma_setup(sc);
4163d184db2SAdrian Chadd 	if (error != 0) {
4173d184db2SAdrian Chadd 		if_printf(ifp, "failed to allocate RX descriptors: %d\n",
4183d184db2SAdrian Chadd 		    error);
4193d184db2SAdrian Chadd 		goto bad;
4203d184db2SAdrian Chadd 	}
4213d184db2SAdrian Chadd 
4222e986da5SSam Leffler 	callout_init_mtx(&sc->sc_cal_ch, &sc->sc_mtx, 0);
4232e986da5SSam Leffler 	callout_init_mtx(&sc->sc_wd_ch, &sc->sc_mtx, 0);
4245591b213SSam Leffler 
425f0b2a0beSSam Leffler 	ATH_TXBUF_LOCK_INIT(sc);
4265591b213SSam Leffler 
4270bbf5441SSam Leffler 	sc->sc_tq = taskqueue_create("ath_taskq", M_NOWAIT,
4280bbf5441SSam Leffler 		taskqueue_thread_enqueue, &sc->sc_tq);
4290bbf5441SSam Leffler 	taskqueue_start_threads(&sc->sc_tq, 1, PI_NET,
4300bbf5441SSam Leffler 		"%s taskq", ifp->if_xname);
4310bbf5441SSam Leffler 
432f8cc9b09SAdrian Chadd 	TASK_INIT(&sc->sc_rxtask, 0, sc->sc_rx.recv_tasklet, sc);
4335591b213SSam Leffler 	TASK_INIT(&sc->sc_bmisstask, 0, ath_bmiss_proc, sc);
434c42a7b7eSSam Leffler 	TASK_INIT(&sc->sc_bstucktask,0, ath_bstuck_proc, sc);
435d52f7132SAdrian Chadd 	TASK_INIT(&sc->sc_resettask,0, ath_reset_proc, sc);
43603e9308fSAdrian Chadd 	TASK_INIT(&sc->sc_txqtask, 0, ath_txq_sched_tasklet, sc);
437f846cf42SAdrian Chadd 	TASK_INIT(&sc->sc_fataltask, 0, ath_fatal_proc, sc);
4385591b213SSam Leffler 
439c5239edbSAdrian Chadd 	/* XXX make this a higher priority taskqueue? */
440c5239edbSAdrian Chadd 	TASK_INIT(&sc->sc_txpkttask, 0, ath_start_task, sc);
441c5239edbSAdrian Chadd 
4425591b213SSam Leffler 	/*
443c42a7b7eSSam Leffler 	 * Allocate hardware transmit queues: one queue for
444c42a7b7eSSam Leffler 	 * beacon frames and one data queue for each QoS
4454fa8d4efSDaniel Eischen 	 * priority.  Note that the hal handles resetting
446c42a7b7eSSam Leffler 	 * these queues at the needed time.
447c42a7b7eSSam Leffler 	 *
448c42a7b7eSSam Leffler 	 * XXX PS-Poll
4495591b213SSam Leffler 	 */
450e1252ce1SAdrian Chadd 	sc->sc_bhalq = ath_beaconq_setup(sc);
4515591b213SSam Leffler 	if (sc->sc_bhalq == (u_int) -1) {
4525591b213SSam Leffler 		if_printf(ifp, "unable to setup a beacon xmit queue!\n");
453c42a7b7eSSam Leffler 		error = EIO;
454b28b4653SSam Leffler 		goto bad2;
4555591b213SSam Leffler 	}
456c42a7b7eSSam Leffler 	sc->sc_cabq = ath_txq_setup(sc, HAL_TX_QUEUE_CAB, 0);
457c42a7b7eSSam Leffler 	if (sc->sc_cabq == NULL) {
458c42a7b7eSSam Leffler 		if_printf(ifp, "unable to setup CAB xmit queue!\n");
459c42a7b7eSSam Leffler 		error = EIO;
460c42a7b7eSSam Leffler 		goto bad2;
461c42a7b7eSSam Leffler 	}
462c42a7b7eSSam Leffler 	/* NB: insure BK queue is the lowest priority h/w queue */
463c42a7b7eSSam Leffler 	if (!ath_tx_setup(sc, WME_AC_BK, HAL_WME_AC_BK)) {
464c42a7b7eSSam Leffler 		if_printf(ifp, "unable to setup xmit queue for %s traffic!\n",
465c42a7b7eSSam Leffler 			ieee80211_wme_acnames[WME_AC_BK]);
466c42a7b7eSSam Leffler 		error = EIO;
467c42a7b7eSSam Leffler 		goto bad2;
468c42a7b7eSSam Leffler 	}
469c42a7b7eSSam Leffler 	if (!ath_tx_setup(sc, WME_AC_BE, HAL_WME_AC_BE) ||
470c42a7b7eSSam Leffler 	    !ath_tx_setup(sc, WME_AC_VI, HAL_WME_AC_VI) ||
471c42a7b7eSSam Leffler 	    !ath_tx_setup(sc, WME_AC_VO, HAL_WME_AC_VO)) {
472c42a7b7eSSam Leffler 		/*
473c42a7b7eSSam Leffler 		 * Not enough hardware tx queues to properly do WME;
474c42a7b7eSSam Leffler 		 * just punt and assign them all to the same h/w queue.
475c42a7b7eSSam Leffler 		 * We could do a better job of this if, for example,
476c42a7b7eSSam Leffler 		 * we allocate queues when we switch from station to
477c42a7b7eSSam Leffler 		 * AP mode.
478c42a7b7eSSam Leffler 		 */
479c42a7b7eSSam Leffler 		if (sc->sc_ac2q[WME_AC_VI] != NULL)
480c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_VI]);
481c42a7b7eSSam Leffler 		if (sc->sc_ac2q[WME_AC_BE] != NULL)
482c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_BE]);
483c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_BE] = sc->sc_ac2q[WME_AC_BK];
484c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_VI] = sc->sc_ac2q[WME_AC_BK];
485c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_VO] = sc->sc_ac2q[WME_AC_BK];
486c42a7b7eSSam Leffler 	}
487c42a7b7eSSam Leffler 
488c42a7b7eSSam Leffler 	/*
489f8418db5SAdrian Chadd 	 * Attach the TX completion function.
490f8418db5SAdrian Chadd 	 *
491f8418db5SAdrian Chadd 	 * The non-EDMA chips may have some special case optimisations;
492f8418db5SAdrian Chadd 	 * this method gives everyone a chance to attach cleanly.
493c42a7b7eSSam Leffler 	 */
494f8418db5SAdrian Chadd 	sc->sc_tx.xmit_attach_comp_func(sc);
495c42a7b7eSSam Leffler 
496c42a7b7eSSam Leffler 	/*
497c42a7b7eSSam Leffler 	 * Setup rate control.  Some rate control modules
498c42a7b7eSSam Leffler 	 * call back to change the anntena state so expose
499c42a7b7eSSam Leffler 	 * the necessary entry points.
500c42a7b7eSSam Leffler 	 * XXX maybe belongs in struct ath_ratectrl?
501c42a7b7eSSam Leffler 	 */
502c42a7b7eSSam Leffler 	sc->sc_setdefantenna = ath_setdefantenna;
503c42a7b7eSSam Leffler 	sc->sc_rc = ath_rate_attach(sc);
504c42a7b7eSSam Leffler 	if (sc->sc_rc == NULL) {
505c42a7b7eSSam Leffler 		error = EIO;
506c42a7b7eSSam Leffler 		goto bad2;
507c42a7b7eSSam Leffler 	}
508c42a7b7eSSam Leffler 
50948237774SAdrian Chadd 	/* Attach DFS module */
51048237774SAdrian Chadd 	if (! ath_dfs_attach(sc)) {
5117e97436bSAdrian Chadd 		device_printf(sc->sc_dev,
5127e97436bSAdrian Chadd 		    "%s: unable to attach DFS\n", __func__);
51348237774SAdrian Chadd 		error = EIO;
51448237774SAdrian Chadd 		goto bad2;
51548237774SAdrian Chadd 	}
51648237774SAdrian Chadd 
5179af351f9SAdrian Chadd 	/* Attach spectral module */
5189af351f9SAdrian Chadd 	if (ath_spectral_attach(sc) < 0) {
5199af351f9SAdrian Chadd 		device_printf(sc->sc_dev,
5209af351f9SAdrian Chadd 		    "%s: unable to attach spectral\n", __func__);
5219af351f9SAdrian Chadd 		error = EIO;
5229af351f9SAdrian Chadd 		goto bad2;
5239af351f9SAdrian Chadd 	}
5249af351f9SAdrian Chadd 
52548237774SAdrian Chadd 	/* Start DFS processing tasklet */
52648237774SAdrian Chadd 	TASK_INIT(&sc->sc_dfstask, 0, ath_dfs_tasklet, sc);
52748237774SAdrian Chadd 
5283440495aSAdrian Chadd 	/* Configure LED state */
5293e50ec2cSSam Leffler 	sc->sc_blinking = 0;
530c42a7b7eSSam Leffler 	sc->sc_ledstate = 1;
5313e50ec2cSSam Leffler 	sc->sc_ledon = 0;			/* low true */
5323e50ec2cSSam Leffler 	sc->sc_ledidle = (2700*hz)/1000;	/* 2.7sec */
5333e50ec2cSSam Leffler 	callout_init(&sc->sc_ledtimer, CALLOUT_MPSAFE);
5343440495aSAdrian Chadd 
5353440495aSAdrian Chadd 	/*
5363440495aSAdrian Chadd 	 * Don't setup hardware-based blinking.
5373440495aSAdrian Chadd 	 *
5383440495aSAdrian Chadd 	 * Although some NICs may have this configured in the
5393440495aSAdrian Chadd 	 * default reset register values, the user may wish
5403440495aSAdrian Chadd 	 * to alter which pins have which function.
5413440495aSAdrian Chadd 	 *
5423440495aSAdrian Chadd 	 * The reference driver attaches the MAC network LED to GPIO1 and
5433440495aSAdrian Chadd 	 * the MAC power LED to GPIO2.  However, the DWA-552 cardbus
5443440495aSAdrian Chadd 	 * NIC has these reversed.
5453440495aSAdrian Chadd 	 */
5463440495aSAdrian Chadd 	sc->sc_hardled = (1 == 0);
5473440495aSAdrian Chadd 	sc->sc_led_net_pin = -1;
5483440495aSAdrian Chadd 	sc->sc_led_pwr_pin = -1;
549c42a7b7eSSam Leffler 	/*
550c42a7b7eSSam Leffler 	 * Auto-enable soft led processing for IBM cards and for
551c42a7b7eSSam Leffler 	 * 5211 minipci cards.  Users can also manually enable/disable
552c42a7b7eSSam Leffler 	 * support with a sysctl.
553c42a7b7eSSam Leffler 	 */
554c42a7b7eSSam Leffler 	sc->sc_softled = (devid == AR5212_DEVID_IBM || devid == AR5211_DEVID);
5556558ffd9SAdrian Chadd 	ath_led_config(sc);
556a497cd88SAdrian Chadd 	ath_hal_setledstate(ah, HAL_LED_INIT);
5575591b213SSam Leffler 
5585591b213SSam Leffler 	ifp->if_softc = sc;
5595591b213SSam Leffler 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
5601a85141aSAdrian Chadd 	ifp->if_start = ath_start_queue;
5615591b213SSam Leffler 	ifp->if_ioctl = ath_ioctl;
5625591b213SSam Leffler 	ifp->if_init = ath_init;
563e50d35e6SMaxim Sobolev 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
564e50d35e6SMaxim Sobolev 	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
565154b8df2SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
5665591b213SSam Leffler 
567c42a7b7eSSam Leffler 	ic->ic_ifp = ifp;
5685591b213SSam Leffler 	/* XXX not right but it's not used anywhere important */
5695591b213SSam Leffler 	ic->ic_phytype = IEEE80211_T_OFDM;
5705591b213SSam Leffler 	ic->ic_opmode = IEEE80211_M_STA;
571c42a7b7eSSam Leffler 	ic->ic_caps =
572c43feedeSSam Leffler 		  IEEE80211_C_STA		/* station mode */
573c43feedeSSam Leffler 		| IEEE80211_C_IBSS		/* ibss, nee adhoc, mode */
574fe32c3efSSam Leffler 		| IEEE80211_C_HOSTAP		/* hostap mode */
575fe32c3efSSam Leffler 		| IEEE80211_C_MONITOR		/* monitor mode */
5767a04dc27SSam Leffler 		| IEEE80211_C_AHDEMO		/* adhoc demo mode */
577b032f27cSSam Leffler 		| IEEE80211_C_WDS		/* 4-address traffic works */
57859aa14a9SRui Paulo 		| IEEE80211_C_MBSS		/* mesh point link mode */
579fe32c3efSSam Leffler 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
580c42a7b7eSSam Leffler 		| IEEE80211_C_SHSLOT		/* short slot time supported */
581c42a7b7eSSam Leffler 		| IEEE80211_C_WPA		/* capable of WPA1+WPA2 */
5823b324f57SAdrian Chadd #ifndef	ATH_ENABLE_11N
58368e8e04eSSam Leffler 		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
5843b324f57SAdrian Chadd #endif
58568e8e04eSSam Leffler 		| IEEE80211_C_TXFRAG		/* handle tx frags */
58610dc8de4SAdrian Chadd #ifdef	ATH_ENABLE_DFS
5877e97436bSAdrian Chadd 		| IEEE80211_C_DFS		/* Enable radar detection */
58810dc8de4SAdrian Chadd #endif
58901e7e035SSam Leffler 		;
590c42a7b7eSSam Leffler 	/*
591c42a7b7eSSam Leffler 	 * Query the hal to figure out h/w crypto support.
592c42a7b7eSSam Leffler 	 */
593c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_WEP))
594b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP;
595c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_OCB))
596b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_OCB;
597c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_CCM))
598b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_CCM;
599c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_CKIP))
600b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_CKIP;
601c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_TKIP)) {
602b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIP;
603c42a7b7eSSam Leffler 		/*
604c42a7b7eSSam Leffler 		 * Check if h/w does the MIC and/or whether the
605c42a7b7eSSam Leffler 		 * separate key cache entries are required to
606c42a7b7eSSam Leffler 		 * handle both tx+rx MIC keys.
607c42a7b7eSSam Leffler 		 */
608c42a7b7eSSam Leffler 		if (ath_hal_ciphersupported(ah, HAL_CIPHER_MIC))
609b032f27cSSam Leffler 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
6105901d2d3SSam Leffler 		/*
6115901d2d3SSam Leffler 		 * If the h/w supports storing tx+rx MIC keys
6125901d2d3SSam Leffler 		 * in one cache slot automatically enable use.
6135901d2d3SSam Leffler 		 */
6145901d2d3SSam Leffler 		if (ath_hal_hastkipsplit(ah) ||
6155901d2d3SSam Leffler 		    !ath_hal_settkipsplit(ah, AH_FALSE))
616c42a7b7eSSam Leffler 			sc->sc_splitmic = 1;
617b032f27cSSam Leffler 		/*
618b032f27cSSam Leffler 		 * If the h/w can do TKIP MIC together with WME then
619b032f27cSSam Leffler 		 * we use it; otherwise we force the MIC to be done
620b032f27cSSam Leffler 		 * in software by the net80211 layer.
621b032f27cSSam Leffler 		 */
622b032f27cSSam Leffler 		if (ath_hal_haswmetkipmic(ah))
623b032f27cSSam Leffler 			sc->sc_wmetkipmic = 1;
624c42a7b7eSSam Leffler 	}
625e8fd88a3SSam Leffler 	sc->sc_hasclrkey = ath_hal_ciphersupported(ah, HAL_CIPHER_CLR);
6269ac01d39SRui Paulo 	/*
6271ac5dac2SRui Paulo 	 * Check for multicast key search support.
6289ac01d39SRui Paulo 	 */
6299ac01d39SRui Paulo 	if (ath_hal_hasmcastkeysearch(sc->sc_ah) &&
6309ac01d39SRui Paulo 	    !ath_hal_getmcastkeysearch(sc->sc_ah)) {
6319ac01d39SRui Paulo 		ath_hal_setmcastkeysearch(sc->sc_ah, 1);
6329ac01d39SRui Paulo 	}
633e8fd88a3SSam Leffler 	sc->sc_mcastkey = ath_hal_getmcastkeysearch(ah);
634c42a7b7eSSam Leffler 	/*
6355901d2d3SSam Leffler 	 * Mark key cache slots associated with global keys
6365901d2d3SSam Leffler 	 * as in use.  If we knew TKIP was not to be used we
6375901d2d3SSam Leffler 	 * could leave the +32, +64, and +32+64 slots free.
6385901d2d3SSam Leffler 	 */
6395901d2d3SSam Leffler 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
6405901d2d3SSam Leffler 		setbit(sc->sc_keymap, i);
6415901d2d3SSam Leffler 		setbit(sc->sc_keymap, i+64);
6425901d2d3SSam Leffler 		if (sc->sc_splitmic) {
6435901d2d3SSam Leffler 			setbit(sc->sc_keymap, i+32);
6445901d2d3SSam Leffler 			setbit(sc->sc_keymap, i+32+64);
6455901d2d3SSam Leffler 		}
6465901d2d3SSam Leffler 	}
6475901d2d3SSam Leffler 	/*
648c42a7b7eSSam Leffler 	 * TPC support can be done either with a global cap or
649c42a7b7eSSam Leffler 	 * per-packet support.  The latter is not available on
650c42a7b7eSSam Leffler 	 * all parts.  We're a bit pedantic here as all parts
651c42a7b7eSSam Leffler 	 * support a global cap.
652c42a7b7eSSam Leffler 	 */
653c59005e9SSam Leffler 	if (ath_hal_hastpc(ah) || ath_hal_hastxpowlimit(ah))
654c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TXPMGT;
655c42a7b7eSSam Leffler 
656c42a7b7eSSam Leffler 	/*
657c42a7b7eSSam Leffler 	 * Mark WME capability only if we have sufficient
658c42a7b7eSSam Leffler 	 * hardware queues to do proper priority scheduling.
659c42a7b7eSSam Leffler 	 */
660c42a7b7eSSam Leffler 	if (sc->sc_ac2q[WME_AC_BE] != sc->sc_ac2q[WME_AC_BK])
661c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_WME;
662c42a7b7eSSam Leffler 	/*
663e8fd88a3SSam Leffler 	 * Check for misc other capabilities.
664c42a7b7eSSam Leffler 	 */
665c42a7b7eSSam Leffler 	if (ath_hal_hasbursting(ah))
666c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_BURST;
667b032f27cSSam Leffler 	sc->sc_hasbmask = ath_hal_hasbssidmask(ah);
66859aa14a9SRui Paulo 	sc->sc_hasbmatch = ath_hal_hasbssidmatch(ah);
669b032f27cSSam Leffler 	sc->sc_hastsfadd = ath_hal_hastsfadjust(ah);
6708a2a6beeSAdrian Chadd 	sc->sc_rxslink = ath_hal_self_linked_final_rxdesc(ah);
671fc4de9b7SAdrian Chadd 	sc->sc_rxtsf32 = ath_hal_has_long_rxdesc_tsf(ah);
67268e8e04eSSam Leffler 	if (ath_hal_hasfastframes(ah))
67368e8e04eSSam Leffler 		ic->ic_caps |= IEEE80211_C_FF;
67459efa8b5SSam Leffler 	wmodes = ath_hal_getwirelessmodes(ah);
675411373ebSSam Leffler 	if (wmodes & (HAL_MODE_108G|HAL_MODE_TURBO))
67668e8e04eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TURBOP;
677584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
67810ad9a77SSam Leffler 	if (ath_hal_macversion(ah) > 0x78) {
67910ad9a77SSam Leffler 		ic->ic_caps |= IEEE80211_C_TDMA; /* capable of TDMA */
68010ad9a77SSam Leffler 		ic->ic_tdma_update = ath_tdma_update;
68110ad9a77SSam Leffler 	}
68210ad9a77SSam Leffler #endif
68367397d39SAdrian Chadd 
68467397d39SAdrian Chadd 	/*
6859c85ff91SAdrian Chadd 	 * TODO: enforce that at least this many frames are available
6869c85ff91SAdrian Chadd 	 * in the txbuf list before allowing data frames (raw or
6879c85ff91SAdrian Chadd 	 * otherwise) to be transmitted.
6889c85ff91SAdrian Chadd 	 */
6899c85ff91SAdrian Chadd 	sc->sc_txq_data_minfree = 10;
6909c85ff91SAdrian Chadd 	/*
6919c85ff91SAdrian Chadd 	 * Leave this as default to maintain legacy behaviour.
6929c85ff91SAdrian Chadd 	 * Shortening the cabq/mcastq may end up causing some
6939c85ff91SAdrian Chadd 	 * undesirable behaviour.
6949c85ff91SAdrian Chadd 	 */
6959c85ff91SAdrian Chadd 	sc->sc_txq_mcastq_maxdepth = ath_txbuf;
6969c85ff91SAdrian Chadd 
6977dcb2beaSAdrian Chadd 	/*
6987dcb2beaSAdrian Chadd 	 * Default the maximum queue depth for a given node
6997dcb2beaSAdrian Chadd 	 * to 1/4'th the TX buffers, or 64, whichever
7007dcb2beaSAdrian Chadd 	 * is larger.
7017dcb2beaSAdrian Chadd 	 */
7027dcb2beaSAdrian Chadd 	sc->sc_txq_node_maxdepth = MAX(64, ath_txbuf / 4);
7037dcb2beaSAdrian Chadd 
704b837332dSAdrian Chadd 	/* Enable CABQ by default */
705b837332dSAdrian Chadd 	sc->sc_cabq_enable = 1;
706b837332dSAdrian Chadd 
7079c85ff91SAdrian Chadd 	/*
708a865860dSAdrian Chadd 	 * Allow the TX and RX chainmasks to be overridden by
709a865860dSAdrian Chadd 	 * environment variables and/or device.hints.
710a865860dSAdrian Chadd 	 *
711a865860dSAdrian Chadd 	 * This must be done early - before the hardware is
712a865860dSAdrian Chadd 	 * calibrated or before the 802.11n stream calculation
713a865860dSAdrian Chadd 	 * is done.
714a865860dSAdrian Chadd 	 */
715a865860dSAdrian Chadd 	if (resource_int_value(device_get_name(sc->sc_dev),
716a865860dSAdrian Chadd 	    device_get_unit(sc->sc_dev), "rx_chainmask",
717a865860dSAdrian Chadd 	    &rx_chainmask) == 0) {
718a865860dSAdrian Chadd 		device_printf(sc->sc_dev, "Setting RX chainmask to 0x%x\n",
719a865860dSAdrian Chadd 		    rx_chainmask);
720a865860dSAdrian Chadd 		(void) ath_hal_setrxchainmask(sc->sc_ah, rx_chainmask);
721a865860dSAdrian Chadd 	}
722a865860dSAdrian Chadd 	if (resource_int_value(device_get_name(sc->sc_dev),
723a865860dSAdrian Chadd 	    device_get_unit(sc->sc_dev), "tx_chainmask",
724a865860dSAdrian Chadd 	    &tx_chainmask) == 0) {
725a865860dSAdrian Chadd 		device_printf(sc->sc_dev, "Setting TX chainmask to 0x%x\n",
726a865860dSAdrian Chadd 		    tx_chainmask);
727dc8552d5SAdrian Chadd 		(void) ath_hal_settxchainmask(sc->sc_ah, tx_chainmask);
728a865860dSAdrian Chadd 	}
729a865860dSAdrian Chadd 
730af017101SAdrian Chadd 	/*
731ff5b5634SAdrian Chadd 	 * Query the TX/RX chainmask configuration.
732ff5b5634SAdrian Chadd 	 *
733ff5b5634SAdrian Chadd 	 * This is only relevant for 11n devices.
734ff5b5634SAdrian Chadd 	 */
735ff5b5634SAdrian Chadd 	ath_hal_getrxchainmask(ah, &sc->sc_rxchainmask);
736ff5b5634SAdrian Chadd 	ath_hal_gettxchainmask(ah, &sc->sc_txchainmask);
737ff5b5634SAdrian Chadd 
738ff5b5634SAdrian Chadd 	/*
739af017101SAdrian Chadd 	 * Disable MRR with protected frames by default.
740af017101SAdrian Chadd 	 * Only 802.11n series NICs can handle this.
741af017101SAdrian Chadd 	 */
742af017101SAdrian Chadd 	sc->sc_mrrprot = 0;	/* XXX should be a capability */
743af017101SAdrian Chadd 
7445540369bSAdrian Chadd 	/*
7455540369bSAdrian Chadd 	 * Query the enterprise mode information the HAL.
7465540369bSAdrian Chadd 	 */
7475540369bSAdrian Chadd 	if (ath_hal_getcapability(ah, HAL_CAP_ENTERPRISE_MODE, 0,
7485540369bSAdrian Chadd 	    &sc->sc_ent_cfg) == HAL_OK)
7495540369bSAdrian Chadd 		sc->sc_use_ent = 1;
7505540369bSAdrian Chadd 
7518fd67f92SAdrian Chadd #ifdef	ATH_ENABLE_11N
75267397d39SAdrian Chadd 	/*
75367397d39SAdrian Chadd 	 * Query HT capabilities
75467397d39SAdrian Chadd 	 */
75567397d39SAdrian Chadd 	if (ath_hal_getcapability(ah, HAL_CAP_HT, 0, NULL) == HAL_OK &&
75667397d39SAdrian Chadd 	    (wmodes & (HAL_MODE_HT20 | HAL_MODE_HT40))) {
7576f4fb2d8SAdrian Chadd 		uint32_t rxs, txs;
75867397d39SAdrian Chadd 
75967397d39SAdrian Chadd 		device_printf(sc->sc_dev, "[HT] enabling HT modes\n");
760af017101SAdrian Chadd 
761af017101SAdrian Chadd 		sc->sc_mrrprot = 1;	/* XXX should be a capability */
762af017101SAdrian Chadd 
76367397d39SAdrian Chadd 		ic->ic_htcaps = IEEE80211_HTC_HT	/* HT operation */
76467397d39SAdrian Chadd 			    | IEEE80211_HTC_AMPDU	/* A-MPDU tx/rx */
76567397d39SAdrian Chadd 			    | IEEE80211_HTC_AMSDU	/* A-MSDU tx/rx */
7667e97436bSAdrian Chadd 			    | IEEE80211_HTCAP_MAXAMSDU_3839
7677e97436bSAdrian Chadd 			    				/* max A-MSDU length */
76867397d39SAdrian Chadd 			    | IEEE80211_HTCAP_SMPS_OFF;	/* SM power save off */
76967397d39SAdrian Chadd 			;
77067397d39SAdrian Chadd 
77176355edbSAdrian Chadd 		/*
77276355edbSAdrian Chadd 		 * Enable short-GI for HT20 only if the hardware
77376355edbSAdrian Chadd 		 * advertises support.
77476355edbSAdrian Chadd 		 * Notably, anything earlier than the AR9287 doesn't.
77576355edbSAdrian Chadd 		 */
77676355edbSAdrian Chadd 		if ((ath_hal_getcapability(ah,
77776355edbSAdrian Chadd 		    HAL_CAP_HT20_SGI, 0, NULL) == HAL_OK) &&
77876355edbSAdrian Chadd 		    (wmodes & HAL_MODE_HT20)) {
77976355edbSAdrian Chadd 			device_printf(sc->sc_dev,
78076355edbSAdrian Chadd 			    "[HT] enabling short-GI in 20MHz mode\n");
78176355edbSAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_SHORTGI20;
78276355edbSAdrian Chadd 		}
78376355edbSAdrian Chadd 
78467397d39SAdrian Chadd 		if (wmodes & HAL_MODE_HT40)
78567397d39SAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40
78667397d39SAdrian Chadd 			    |  IEEE80211_HTCAP_SHORTGI40;
78767397d39SAdrian Chadd 
78867397d39SAdrian Chadd 		/*
7897e97436bSAdrian Chadd 		 * TX/RX streams need to be taken into account when
7907e97436bSAdrian Chadd 		 * negotiating which MCS rates it'll receive and
79167397d39SAdrian Chadd 		 * what MCS rates are available for TX.
79267397d39SAdrian Chadd 		 */
79354517070SAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 0, &txs);
79454517070SAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 1, &rxs);
79567397d39SAdrian Chadd 		ic->ic_txstream = txs;
79667397d39SAdrian Chadd 		ic->ic_rxstream = rxs;
79767397d39SAdrian Chadd 
7986606ba81SAdrian Chadd 		/*
7996606ba81SAdrian Chadd 		 * Setup TX and RX STBC based on what the HAL allows and
8006606ba81SAdrian Chadd 		 * the currently configured chainmask set.
8016606ba81SAdrian Chadd 		 * Ie - don't enable STBC TX if only one chain is enabled.
8026606ba81SAdrian Chadd 		 * STBC RX is fine on a single RX chain; it just won't
8036606ba81SAdrian Chadd 		 * provide any real benefit.
8046606ba81SAdrian Chadd 		 */
8056606ba81SAdrian Chadd 		if (ath_hal_getcapability(ah, HAL_CAP_RX_STBC, 0,
8066606ba81SAdrian Chadd 		    NULL) == HAL_OK) {
8076606ba81SAdrian Chadd 			sc->sc_rx_stbc = 1;
8086606ba81SAdrian Chadd 			device_printf(sc->sc_dev,
8096606ba81SAdrian Chadd 			    "[HT] 1 stream STBC receive enabled\n");
8106606ba81SAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_1STREAM;
8116606ba81SAdrian Chadd 		}
8126606ba81SAdrian Chadd 		if (txs > 1 && ath_hal_getcapability(ah, HAL_CAP_TX_STBC, 0,
8136606ba81SAdrian Chadd 		    NULL) == HAL_OK) {
8146606ba81SAdrian Chadd 			sc->sc_tx_stbc = 1;
8156606ba81SAdrian Chadd 			device_printf(sc->sc_dev,
8166606ba81SAdrian Chadd 			    "[HT] 1 stream STBC transmit enabled\n");
8176606ba81SAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_TXSTBC;
8186606ba81SAdrian Chadd 		}
8196606ba81SAdrian Chadd 
820ce656facSAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_RTS_AGGR_LIMIT, 1,
821ce656facSAdrian Chadd 		    &sc->sc_rts_aggr_limit);
822ce656facSAdrian Chadd 		if (sc->sc_rts_aggr_limit != (64 * 1024))
823ce656facSAdrian Chadd 			device_printf(sc->sc_dev,
824ce656facSAdrian Chadd 			    "[HT] RTS aggregates limited to %d KiB\n",
825ce656facSAdrian Chadd 			    sc->sc_rts_aggr_limit / 1024);
826ce656facSAdrian Chadd 
8277e97436bSAdrian Chadd 		device_printf(sc->sc_dev,
8287e97436bSAdrian Chadd 		    "[HT] %d RX streams; %d TX streams\n", rxs, txs);
82967397d39SAdrian Chadd 	}
83067397d39SAdrian Chadd #endif
83167397d39SAdrian Chadd 
832c42a7b7eSSam Leffler 	/*
833f8aa9fd5SAdrian Chadd 	 * Initial aggregation settings.
834f8aa9fd5SAdrian Chadd 	 */
835f8aa9fd5SAdrian Chadd 	sc->sc_hwq_limit = ATH_AGGR_MIN_QDEPTH;
836f8aa9fd5SAdrian Chadd 	sc->sc_tid_hwq_lo = ATH_AGGR_SCHED_LOW;
837f8aa9fd5SAdrian Chadd 	sc->sc_tid_hwq_hi = ATH_AGGR_SCHED_HIGH;
8384a502c33SAdrian Chadd 	sc->sc_aggr_limit = ATH_AGGR_MAXSIZE;
839a54ecf78SAdrian Chadd 	sc->sc_delim_min_pad = 0;
840f8aa9fd5SAdrian Chadd 
841f8aa9fd5SAdrian Chadd 	/*
842ddbe3036SAdrian Chadd 	 * Check if the hardware requires PCI register serialisation.
843ddbe3036SAdrian Chadd 	 * Some of the Owl based MACs require this.
844ddbe3036SAdrian Chadd 	 */
845ddbe3036SAdrian Chadd 	if (mp_ncpus > 1 &&
846ddbe3036SAdrian Chadd 	    ath_hal_getcapability(ah, HAL_CAP_SERIALISE_WAR,
847ddbe3036SAdrian Chadd 	     0, NULL) == HAL_OK) {
848ddbe3036SAdrian Chadd 		sc->sc_ah->ah_config.ah_serialise_reg_war = 1;
8497e97436bSAdrian Chadd 		device_printf(sc->sc_dev,
8507e97436bSAdrian Chadd 		    "Enabling register serialisation\n");
851ddbe3036SAdrian Chadd 	}
852ddbe3036SAdrian Chadd 
853ddbe3036SAdrian Chadd 	/*
854f0db652cSAdrian Chadd 	 * Initialise the deferred completed RX buffer list.
855f0db652cSAdrian Chadd 	 */
8565d4dedadSAdrian Chadd 	TAILQ_INIT(&sc->sc_rx_rxlist[HAL_RX_QUEUE_HP]);
8575d4dedadSAdrian Chadd 	TAILQ_INIT(&sc->sc_rx_rxlist[HAL_RX_QUEUE_LP]);
858f0db652cSAdrian Chadd 
859f0db652cSAdrian Chadd 	/*
860c42a7b7eSSam Leffler 	 * Indicate we need the 802.11 header padded to a
861c42a7b7eSSam Leffler 	 * 32-bit boundary for 4-address and QoS frames.
862c42a7b7eSSam Leffler 	 */
863c42a7b7eSSam Leffler 	ic->ic_flags |= IEEE80211_F_DATAPAD;
864c42a7b7eSSam Leffler 
865c42a7b7eSSam Leffler 	/*
866c42a7b7eSSam Leffler 	 * Query the hal about antenna support.
867c42a7b7eSSam Leffler 	 */
868c42a7b7eSSam Leffler 	sc->sc_defant = ath_hal_getdefantenna(ah);
869c42a7b7eSSam Leffler 
870c42a7b7eSSam Leffler 	/*
871c42a7b7eSSam Leffler 	 * Not all chips have the VEOL support we want to
872c42a7b7eSSam Leffler 	 * use with IBSS beacons; check here for it.
873c42a7b7eSSam Leffler 	 */
874c42a7b7eSSam Leffler 	sc->sc_hasveol = ath_hal_hasveol(ah);
8755591b213SSam Leffler 
8765591b213SSam Leffler 	/* get mac address from hardware */
87729aca940SSam Leffler 	ath_hal_getmac(ah, macaddr);
878b032f27cSSam Leffler 	if (sc->sc_hasbmask)
879b032f27cSSam Leffler 		ath_hal_getbssidmask(ah, sc->sc_hwbssidmask);
8805591b213SSam Leffler 
881b032f27cSSam Leffler 	/* NB: used to size node table key mapping array */
882b032f27cSSam Leffler 	ic->ic_max_keyix = sc->sc_keymax;
8835591b213SSam Leffler 	/* call MI attach routine. */
88429aca940SSam Leffler 	ieee80211_ifattach(ic, macaddr);
885b032f27cSSam Leffler 	ic->ic_setregdomain = ath_setregdomain;
886b032f27cSSam Leffler 	ic->ic_getradiocaps = ath_getradiocaps;
887b032f27cSSam Leffler 	sc->sc_opmode = HAL_M_STA;
888b032f27cSSam Leffler 
8895591b213SSam Leffler 	/* override default methods */
890b032f27cSSam Leffler 	ic->ic_newassoc = ath_newassoc;
891b032f27cSSam Leffler 	ic->ic_updateslot = ath_updateslot;
892b032f27cSSam Leffler 	ic->ic_wme.wme_update = ath_wme_update;
893b032f27cSSam Leffler 	ic->ic_vap_create = ath_vap_create;
894b032f27cSSam Leffler 	ic->ic_vap_delete = ath_vap_delete;
895b032f27cSSam Leffler 	ic->ic_raw_xmit = ath_raw_xmit;
896b032f27cSSam Leffler 	ic->ic_update_mcast = ath_update_mcast;
897b032f27cSSam Leffler 	ic->ic_update_promisc = ath_update_promisc;
8985591b213SSam Leffler 	ic->ic_node_alloc = ath_node_alloc;
8991e774079SSam Leffler 	sc->sc_node_free = ic->ic_node_free;
9005591b213SSam Leffler 	ic->ic_node_free = ath_node_free;
9014afa805eSAdrian Chadd 	sc->sc_node_cleanup = ic->ic_node_cleanup;
9024afa805eSAdrian Chadd 	ic->ic_node_cleanup = ath_node_cleanup;
90368e8e04eSSam Leffler 	ic->ic_node_getsignal = ath_node_getsignal;
90468e8e04eSSam Leffler 	ic->ic_scan_start = ath_scan_start;
90568e8e04eSSam Leffler 	ic->ic_scan_end = ath_scan_end;
90668e8e04eSSam Leffler 	ic->ic_set_channel = ath_set_channel;
907fdd72b4aSAdrian Chadd #ifdef	ATH_ENABLE_11N
908eb6f0de0SAdrian Chadd 	/* 802.11n specific - but just override anyway */
909eb6f0de0SAdrian Chadd 	sc->sc_addba_request = ic->ic_addba_request;
910eb6f0de0SAdrian Chadd 	sc->sc_addba_response = ic->ic_addba_response;
911eb6f0de0SAdrian Chadd 	sc->sc_addba_stop = ic->ic_addba_stop;
912eb6f0de0SAdrian Chadd 	sc->sc_bar_response = ic->ic_bar_response;
913eb6f0de0SAdrian Chadd 	sc->sc_addba_response_timeout = ic->ic_addba_response_timeout;
914eb6f0de0SAdrian Chadd 
915eb6f0de0SAdrian Chadd 	ic->ic_addba_request = ath_addba_request;
916eb6f0de0SAdrian Chadd 	ic->ic_addba_response = ath_addba_response;
917eb6f0de0SAdrian Chadd 	ic->ic_addba_response_timeout = ath_addba_response_timeout;
918eb6f0de0SAdrian Chadd 	ic->ic_addba_stop = ath_addba_stop;
919eb6f0de0SAdrian Chadd 	ic->ic_bar_response = ath_bar_response;
920eb6f0de0SAdrian Chadd 
921fdd72b4aSAdrian Chadd 	ic->ic_update_chw = ath_update_chw;
922fdd72b4aSAdrian Chadd #endif	/* ATH_ENABLE_11N */
923fdd72b4aSAdrian Chadd 
924e1b5ab97SAdrian Chadd #ifdef	ATH_ENABLE_RADIOTAP_VENDOR_EXT
925e1b5ab97SAdrian Chadd 	/*
926e1b5ab97SAdrian Chadd 	 * There's one vendor bitmap entry in the RX radiotap
927e1b5ab97SAdrian Chadd 	 * header; make sure that's taken into account.
928e1b5ab97SAdrian Chadd 	 */
929e1b5ab97SAdrian Chadd 	ieee80211_radiotap_attachv(ic,
930e1b5ab97SAdrian Chadd 	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th), 0,
931e1b5ab97SAdrian Chadd 		ATH_TX_RADIOTAP_PRESENT,
932e1b5ab97SAdrian Chadd 	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th), 1,
933e1b5ab97SAdrian Chadd 		ATH_RX_RADIOTAP_PRESENT);
934e1b5ab97SAdrian Chadd #else
935e1b5ab97SAdrian Chadd 	/*
936e1b5ab97SAdrian Chadd 	 * No vendor bitmap/extensions are present.
937e1b5ab97SAdrian Chadd 	 */
9385463c4a4SSam Leffler 	ieee80211_radiotap_attach(ic,
9395463c4a4SSam Leffler 	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
9405463c4a4SSam Leffler 		ATH_TX_RADIOTAP_PRESENT,
9415463c4a4SSam Leffler 	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
9425463c4a4SSam Leffler 		ATH_RX_RADIOTAP_PRESENT);
943e1b5ab97SAdrian Chadd #endif	/* ATH_ENABLE_RADIOTAP_VENDOR_EXT */
9445463c4a4SSam Leffler 
9454866e6c2SSam Leffler 	/*
946bdbb6e5bSAdrian Chadd 	 * Setup the ALQ logging if required
947bdbb6e5bSAdrian Chadd 	 */
94889d2e576SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
949bdbb6e5bSAdrian Chadd 	if_ath_alq_init(&sc->sc_alq, device_get_nameunit(sc->sc_dev));
950bb327d28SAdrian Chadd 	if_ath_alq_setcfg(&sc->sc_alq,
951bb327d28SAdrian Chadd 	    sc->sc_ah->ah_macVersion,
952bb327d28SAdrian Chadd 	    sc->sc_ah->ah_macRev,
953bb327d28SAdrian Chadd 	    sc->sc_ah->ah_phyRev,
954bb327d28SAdrian Chadd 	    sc->sc_ah->ah_magic);
955bdbb6e5bSAdrian Chadd #endif
956bdbb6e5bSAdrian Chadd 
957bdbb6e5bSAdrian Chadd 	/*
9584866e6c2SSam Leffler 	 * Setup dynamic sysctl's now that country code and
9594866e6c2SSam Leffler 	 * regdomain are available from the hal.
9604866e6c2SSam Leffler 	 */
9614866e6c2SSam Leffler 	ath_sysctlattach(sc);
962e8dabfbeSAdrian Chadd 	ath_sysctl_stats_attach(sc);
96337931a35SAdrian Chadd 	ath_sysctl_hal_attach(sc);
96473454c73SSam Leffler 
965c42a7b7eSSam Leffler 	if (bootverbose)
966c42a7b7eSSam Leffler 		ieee80211_announce(ic);
967c42a7b7eSSam Leffler 	ath_announce(sc);
9685591b213SSam Leffler 	return 0;
969b28b4653SSam Leffler bad2:
970c42a7b7eSSam Leffler 	ath_tx_cleanup(sc);
971b28b4653SSam Leffler 	ath_desc_free(sc);
9723fdfc330SAdrian Chadd 	ath_txdma_teardown(sc);
9733d184db2SAdrian Chadd 	ath_rxdma_teardown(sc);
9745591b213SSam Leffler bad:
9755591b213SSam Leffler 	if (ah)
9765591b213SSam Leffler 		ath_hal_detach(ah);
9778bf40208SAdrian Chadd 
9788bf40208SAdrian Chadd 	/*
9798bf40208SAdrian Chadd 	 * To work around scoping issues with CURVNET_SET/CURVNET_RESTORE..
9808bf40208SAdrian Chadd 	 */
9818bf40208SAdrian Chadd 	if (ifp != NULL && ifp->if_vnet) {
982a93c5097SAdrian Chadd 		CURVNET_SET(ifp->if_vnet);
983fc74a9f9SBrooks Davis 		if_free(ifp);
984a93c5097SAdrian Chadd 		CURVNET_RESTORE();
9858bf40208SAdrian Chadd 	} else if (ifp != NULL)
9868bf40208SAdrian Chadd 		if_free(ifp);
9875591b213SSam Leffler 	sc->sc_invalid = 1;
9885591b213SSam Leffler 	return error;
9895591b213SSam Leffler }
9905591b213SSam Leffler 
9915591b213SSam Leffler int
9925591b213SSam Leffler ath_detach(struct ath_softc *sc)
9935591b213SSam Leffler {
994fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
9955591b213SSam Leffler 
996c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
997c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
9985591b213SSam Leffler 
999c42a7b7eSSam Leffler 	/*
1000c42a7b7eSSam Leffler 	 * NB: the order of these is important:
100171b85077SSam Leffler 	 * o stop the chip so no more interrupts will fire
1002c42a7b7eSSam Leffler 	 * o call the 802.11 layer before detaching the hal to
1003c42a7b7eSSam Leffler 	 *   insure callbacks into the driver to delete global
1004c42a7b7eSSam Leffler 	 *   key cache entries can be handled
100571b85077SSam Leffler 	 * o free the taskqueue which drains any pending tasks
1006c42a7b7eSSam Leffler 	 * o reclaim the tx queue data structures after calling
1007c42a7b7eSSam Leffler 	 *   the 802.11 layer as we'll get called back to reclaim
1008c42a7b7eSSam Leffler 	 *   node state and potentially want to use them
1009c42a7b7eSSam Leffler 	 * o to cleanup the tx queues the hal is called, so detach
1010c42a7b7eSSam Leffler 	 *   it last
1011c42a7b7eSSam Leffler 	 * Other than that, it's straightforward...
1012c42a7b7eSSam Leffler 	 */
101371b85077SSam Leffler 	ath_stop(ifp);
1014b032f27cSSam Leffler 	ieee80211_ifdetach(ifp->if_l2com);
101571b85077SSam Leffler 	taskqueue_free(sc->sc_tq);
101686e07743SSam Leffler #ifdef ATH_TX99_DIAG
101786e07743SSam Leffler 	if (sc->sc_tx99 != NULL)
101886e07743SSam Leffler 		sc->sc_tx99->detach(sc->sc_tx99);
101986e07743SSam Leffler #endif
1020c42a7b7eSSam Leffler 	ath_rate_detach(sc->sc_rc);
102189d2e576SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
1022bdbb6e5bSAdrian Chadd 	if_ath_alq_tidyup(&sc->sc_alq);
1023bdbb6e5bSAdrian Chadd #endif
10249af351f9SAdrian Chadd 	ath_spectral_detach(sc);
102548237774SAdrian Chadd 	ath_dfs_detach(sc);
10265591b213SSam Leffler 	ath_desc_free(sc);
10274bf404eaSAdrian Chadd 	ath_txdma_teardown(sc);
10283d184db2SAdrian Chadd 	ath_rxdma_teardown(sc);
1029c42a7b7eSSam Leffler 	ath_tx_cleanup(sc);
103071b85077SSam Leffler 	ath_hal_detach(sc->sc_ah);	/* NB: sets chip in full sleep */
1031a93c5097SAdrian Chadd 
1032a93c5097SAdrian Chadd 	CURVNET_SET(ifp->if_vnet);
1033c4c6f08fSRuslan Ermilov 	if_free(ifp);
1034a93c5097SAdrian Chadd 	CURVNET_RESTORE();
1035f0b2a0beSSam Leffler 
10365591b213SSam Leffler 	return 0;
10375591b213SSam Leffler }
10385591b213SSam Leffler 
1039b032f27cSSam Leffler /*
1040b032f27cSSam Leffler  * MAC address handling for multiple BSS on the same radio.
1041b032f27cSSam Leffler  * The first vap uses the MAC address from the EEPROM.  For
1042b032f27cSSam Leffler  * subsequent vap's we set the U/L bit (bit 1) in the MAC
1043b032f27cSSam Leffler  * address and use the next six bits as an index.
1044b032f27cSSam Leffler  */
1045b032f27cSSam Leffler static void
1046b032f27cSSam Leffler assign_address(struct ath_softc *sc, uint8_t mac[IEEE80211_ADDR_LEN], int clone)
1047b032f27cSSam Leffler {
1048b032f27cSSam Leffler 	int i;
1049b032f27cSSam Leffler 
1050b032f27cSSam Leffler 	if (clone && sc->sc_hasbmask) {
1051b032f27cSSam Leffler 		/* NB: we only do this if h/w supports multiple bssid */
1052b032f27cSSam Leffler 		for (i = 0; i < 8; i++)
1053b032f27cSSam Leffler 			if ((sc->sc_bssidmask & (1<<i)) == 0)
1054b032f27cSSam Leffler 				break;
1055b032f27cSSam Leffler 		if (i != 0)
1056b032f27cSSam Leffler 			mac[0] |= (i << 2)|0x2;
1057b032f27cSSam Leffler 	} else
1058b032f27cSSam Leffler 		i = 0;
1059b032f27cSSam Leffler 	sc->sc_bssidmask |= 1<<i;
1060b032f27cSSam Leffler 	sc->sc_hwbssidmask[0] &= ~mac[0];
1061b032f27cSSam Leffler 	if (i == 0)
1062b032f27cSSam Leffler 		sc->sc_nbssid0++;
1063b032f27cSSam Leffler }
1064b032f27cSSam Leffler 
1065b032f27cSSam Leffler static void
1066b032f27cSSam Leffler reclaim_address(struct ath_softc *sc, const uint8_t mac[IEEE80211_ADDR_LEN])
1067b032f27cSSam Leffler {
1068b032f27cSSam Leffler 	int i = mac[0] >> 2;
1069b032f27cSSam Leffler 	uint8_t mask;
1070b032f27cSSam Leffler 
1071b032f27cSSam Leffler 	if (i != 0 || --sc->sc_nbssid0 == 0) {
1072b032f27cSSam Leffler 		sc->sc_bssidmask &= ~(1<<i);
1073b032f27cSSam Leffler 		/* recalculate bssid mask from remaining addresses */
1074b032f27cSSam Leffler 		mask = 0xff;
1075b032f27cSSam Leffler 		for (i = 1; i < 8; i++)
1076b032f27cSSam Leffler 			if (sc->sc_bssidmask & (1<<i))
1077b032f27cSSam Leffler 				mask &= ~((i<<2)|0x2);
1078b032f27cSSam Leffler 		sc->sc_hwbssidmask[0] |= mask;
1079b032f27cSSam Leffler 	}
1080b032f27cSSam Leffler }
1081b032f27cSSam Leffler 
1082b032f27cSSam Leffler /*
1083b032f27cSSam Leffler  * Assign a beacon xmit slot.  We try to space out
1084b032f27cSSam Leffler  * assignments so when beacons are staggered the
1085b032f27cSSam Leffler  * traffic coming out of the cab q has maximal time
1086b032f27cSSam Leffler  * to go out before the next beacon is scheduled.
1087b032f27cSSam Leffler  */
1088b032f27cSSam Leffler static int
1089b032f27cSSam Leffler assign_bslot(struct ath_softc *sc)
1090b032f27cSSam Leffler {
1091b032f27cSSam Leffler 	u_int slot, free;
1092b032f27cSSam Leffler 
1093b032f27cSSam Leffler 	free = 0;
1094b032f27cSSam Leffler 	for (slot = 0; slot < ATH_BCBUF; slot++)
1095b032f27cSSam Leffler 		if (sc->sc_bslot[slot] == NULL) {
1096b032f27cSSam Leffler 			if (sc->sc_bslot[(slot+1)%ATH_BCBUF] == NULL &&
1097b032f27cSSam Leffler 			    sc->sc_bslot[(slot-1)%ATH_BCBUF] == NULL)
1098b032f27cSSam Leffler 				return slot;
1099b032f27cSSam Leffler 			free = slot;
1100b032f27cSSam Leffler 			/* NB: keep looking for a double slot */
1101b032f27cSSam Leffler 		}
1102b032f27cSSam Leffler 	return free;
1103b032f27cSSam Leffler }
1104b032f27cSSam Leffler 
1105b032f27cSSam Leffler static struct ieee80211vap *
1106fcd9500fSBernhard Schmidt ath_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
1107fcd9500fSBernhard Schmidt     enum ieee80211_opmode opmode, int flags,
1108b032f27cSSam Leffler     const uint8_t bssid[IEEE80211_ADDR_LEN],
1109b032f27cSSam Leffler     const uint8_t mac0[IEEE80211_ADDR_LEN])
1110b032f27cSSam Leffler {
1111b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
1112b032f27cSSam Leffler 	struct ath_vap *avp;
1113b032f27cSSam Leffler 	struct ieee80211vap *vap;
1114b032f27cSSam Leffler 	uint8_t mac[IEEE80211_ADDR_LEN];
1115fcd9500fSBernhard Schmidt 	int needbeacon, error;
1116fcd9500fSBernhard Schmidt 	enum ieee80211_opmode ic_opmode;
1117b032f27cSSam Leffler 
1118b032f27cSSam Leffler 	avp = (struct ath_vap *) malloc(sizeof(struct ath_vap),
1119b032f27cSSam Leffler 	    M_80211_VAP, M_WAITOK | M_ZERO);
1120b032f27cSSam Leffler 	needbeacon = 0;
1121b032f27cSSam Leffler 	IEEE80211_ADDR_COPY(mac, mac0);
1122b032f27cSSam Leffler 
1123b032f27cSSam Leffler 	ATH_LOCK(sc);
1124a8962181SSam Leffler 	ic_opmode = opmode;		/* default to opmode of new vap */
1125b032f27cSSam Leffler 	switch (opmode) {
1126b032f27cSSam Leffler 	case IEEE80211_M_STA:
1127a8962181SSam Leffler 		if (sc->sc_nstavaps != 0) {	/* XXX only 1 for now */
1128b032f27cSSam Leffler 			device_printf(sc->sc_dev, "only 1 sta vap supported\n");
1129b032f27cSSam Leffler 			goto bad;
1130b032f27cSSam Leffler 		}
1131b032f27cSSam Leffler 		if (sc->sc_nvaps) {
1132b032f27cSSam Leffler 			/*
1133a8962181SSam Leffler 			 * With multiple vaps we must fall back
1134a8962181SSam Leffler 			 * to s/w beacon miss handling.
1135b032f27cSSam Leffler 			 */
1136b032f27cSSam Leffler 			flags |= IEEE80211_CLONE_NOBEACONS;
1137b032f27cSSam Leffler 		}
1138a8962181SSam Leffler 		if (flags & IEEE80211_CLONE_NOBEACONS) {
1139a8962181SSam Leffler 			/*
1140a8962181SSam Leffler 			 * Station mode w/o beacons are implemented w/ AP mode.
1141a8962181SSam Leffler 			 */
1142b032f27cSSam Leffler 			ic_opmode = IEEE80211_M_HOSTAP;
1143a8962181SSam Leffler 		}
1144b032f27cSSam Leffler 		break;
1145b032f27cSSam Leffler 	case IEEE80211_M_IBSS:
1146b032f27cSSam Leffler 		if (sc->sc_nvaps != 0) {	/* XXX only 1 for now */
1147b032f27cSSam Leffler 			device_printf(sc->sc_dev,
1148b032f27cSSam Leffler 			    "only 1 ibss vap supported\n");
1149b032f27cSSam Leffler 			goto bad;
1150b032f27cSSam Leffler 		}
1151b032f27cSSam Leffler 		needbeacon = 1;
1152b032f27cSSam Leffler 		break;
1153b032f27cSSam Leffler 	case IEEE80211_M_AHDEMO:
1154584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
115510ad9a77SSam Leffler 		if (flags & IEEE80211_CLONE_TDMA) {
1156a8962181SSam Leffler 			if (sc->sc_nvaps != 0) {
1157a8962181SSam Leffler 				device_printf(sc->sc_dev,
1158a8962181SSam Leffler 				    "only 1 tdma vap supported\n");
1159a8962181SSam Leffler 				goto bad;
1160a8962181SSam Leffler 			}
116110ad9a77SSam Leffler 			needbeacon = 1;
116210ad9a77SSam Leffler 			flags |= IEEE80211_CLONE_NOBEACONS;
116310ad9a77SSam Leffler 		}
1164b032f27cSSam Leffler 		/* fall thru... */
116510ad9a77SSam Leffler #endif
1166b032f27cSSam Leffler 	case IEEE80211_M_MONITOR:
1167b032f27cSSam Leffler 		if (sc->sc_nvaps != 0 && ic->ic_opmode != opmode) {
1168a8962181SSam Leffler 			/*
1169a8962181SSam Leffler 			 * Adopt existing mode.  Adding a monitor or ahdemo
1170a8962181SSam Leffler 			 * vap to an existing configuration is of dubious
1171a8962181SSam Leffler 			 * value but should be ok.
1172a8962181SSam Leffler 			 */
1173b032f27cSSam Leffler 			/* XXX not right for monitor mode */
1174b032f27cSSam Leffler 			ic_opmode = ic->ic_opmode;
1175a8962181SSam Leffler 		}
1176b032f27cSSam Leffler 		break;
1177b032f27cSSam Leffler 	case IEEE80211_M_HOSTAP:
117859aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
1179b032f27cSSam Leffler 		needbeacon = 1;
1180a8962181SSam Leffler 		break;
1181b032f27cSSam Leffler 	case IEEE80211_M_WDS:
1182a8962181SSam Leffler 		if (sc->sc_nvaps != 0 && ic->ic_opmode == IEEE80211_M_STA) {
1183b032f27cSSam Leffler 			device_printf(sc->sc_dev,
1184b032f27cSSam Leffler 			    "wds not supported in sta mode\n");
1185b032f27cSSam Leffler 			goto bad;
1186b032f27cSSam Leffler 		}
1187b032f27cSSam Leffler 		/*
1188b032f27cSSam Leffler 		 * Silently remove any request for a unique
1189b032f27cSSam Leffler 		 * bssid; WDS vap's always share the local
1190b032f27cSSam Leffler 		 * mac address.
1191b032f27cSSam Leffler 		 */
1192b032f27cSSam Leffler 		flags &= ~IEEE80211_CLONE_BSSID;
1193a8962181SSam Leffler 		if (sc->sc_nvaps == 0)
1194b032f27cSSam Leffler 			ic_opmode = IEEE80211_M_HOSTAP;
1195a8962181SSam Leffler 		else
1196a8962181SSam Leffler 			ic_opmode = ic->ic_opmode;
11977d261891SRui Paulo 		break;
1198b032f27cSSam Leffler 	default:
1199b032f27cSSam Leffler 		device_printf(sc->sc_dev, "unknown opmode %d\n", opmode);
1200b032f27cSSam Leffler 		goto bad;
1201b032f27cSSam Leffler 	}
1202b032f27cSSam Leffler 	/*
1203b032f27cSSam Leffler 	 * Check that a beacon buffer is available; the code below assumes it.
1204b032f27cSSam Leffler 	 */
12056b349e5aSAdrian Chadd 	if (needbeacon & TAILQ_EMPTY(&sc->sc_bbuf)) {
1206b032f27cSSam Leffler 		device_printf(sc->sc_dev, "no beacon buffer available\n");
1207b032f27cSSam Leffler 		goto bad;
1208b032f27cSSam Leffler 	}
1209b032f27cSSam Leffler 
1210b032f27cSSam Leffler 	/* STA, AHDEMO? */
121159aa14a9SRui Paulo 	if (opmode == IEEE80211_M_HOSTAP || opmode == IEEE80211_M_MBSS) {
1212b032f27cSSam Leffler 		assign_address(sc, mac, flags & IEEE80211_CLONE_BSSID);
1213b032f27cSSam Leffler 		ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
1214b032f27cSSam Leffler 	}
1215b032f27cSSam Leffler 
1216b032f27cSSam Leffler 	vap = &avp->av_vap;
1217b032f27cSSam Leffler 	/* XXX can't hold mutex across if_alloc */
1218b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1219b032f27cSSam Leffler 	error = ieee80211_vap_setup(ic, vap, name, unit, opmode, flags,
1220b032f27cSSam Leffler 	    bssid, mac);
1221b032f27cSSam Leffler 	ATH_LOCK(sc);
1222b032f27cSSam Leffler 	if (error != 0) {
1223b032f27cSSam Leffler 		device_printf(sc->sc_dev, "%s: error %d creating vap\n",
1224b032f27cSSam Leffler 		    __func__, error);
1225b032f27cSSam Leffler 		goto bad2;
1226b032f27cSSam Leffler 	}
1227b032f27cSSam Leffler 
1228b032f27cSSam Leffler 	/* h/w crypto support */
1229b032f27cSSam Leffler 	vap->iv_key_alloc = ath_key_alloc;
1230b032f27cSSam Leffler 	vap->iv_key_delete = ath_key_delete;
1231b032f27cSSam Leffler 	vap->iv_key_set = ath_key_set;
1232b032f27cSSam Leffler 	vap->iv_key_update_begin = ath_key_update_begin;
1233b032f27cSSam Leffler 	vap->iv_key_update_end = ath_key_update_end;
1234b032f27cSSam Leffler 
1235b032f27cSSam Leffler 	/* override various methods */
1236b032f27cSSam Leffler 	avp->av_recv_mgmt = vap->iv_recv_mgmt;
1237b032f27cSSam Leffler 	vap->iv_recv_mgmt = ath_recv_mgmt;
1238b032f27cSSam Leffler 	vap->iv_reset = ath_reset_vap;
1239b032f27cSSam Leffler 	vap->iv_update_beacon = ath_beacon_update;
1240b032f27cSSam Leffler 	avp->av_newstate = vap->iv_newstate;
1241b032f27cSSam Leffler 	vap->iv_newstate = ath_newstate;
1242b032f27cSSam Leffler 	avp->av_bmiss = vap->iv_bmiss;
1243b032f27cSSam Leffler 	vap->iv_bmiss = ath_bmiss_vap;
1244b032f27cSSam Leffler 
12450eb81626SAdrian Chadd 	avp->av_node_ps = vap->iv_node_ps;
12460eb81626SAdrian Chadd 	vap->iv_node_ps = ath_node_powersave;
12470eb81626SAdrian Chadd 
1248548a605dSAdrian Chadd 	avp->av_set_tim = vap->iv_set_tim;
1249548a605dSAdrian Chadd 	vap->iv_set_tim = ath_node_set_tim;
1250548a605dSAdrian Chadd 
12519be25f4aSAdrian Chadd 	/* Set default parameters */
12529be25f4aSAdrian Chadd 
12539be25f4aSAdrian Chadd 	/*
12549be25f4aSAdrian Chadd 	 * Anything earlier than some AR9300 series MACs don't
12559be25f4aSAdrian Chadd 	 * support a smaller MPDU density.
12569be25f4aSAdrian Chadd 	 */
12579be25f4aSAdrian Chadd 	vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_8;
12589be25f4aSAdrian Chadd 	/*
12599be25f4aSAdrian Chadd 	 * All NICs can handle the maximum size, however
12609be25f4aSAdrian Chadd 	 * AR5416 based MACs can only TX aggregates w/ RTS
12619be25f4aSAdrian Chadd 	 * protection when the total aggregate size is <= 8k.
12629be25f4aSAdrian Chadd 	 * However, for now that's enforced by the TX path.
12639be25f4aSAdrian Chadd 	 */
12649be25f4aSAdrian Chadd 	vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_64K;
12659be25f4aSAdrian Chadd 
1266b032f27cSSam Leffler 	avp->av_bslot = -1;
1267b032f27cSSam Leffler 	if (needbeacon) {
1268b032f27cSSam Leffler 		/*
1269b032f27cSSam Leffler 		 * Allocate beacon state and setup the q for buffered
1270b032f27cSSam Leffler 		 * multicast frames.  We know a beacon buffer is
1271b032f27cSSam Leffler 		 * available because we checked above.
1272b032f27cSSam Leffler 		 */
12736b349e5aSAdrian Chadd 		avp->av_bcbuf = TAILQ_FIRST(&sc->sc_bbuf);
12746b349e5aSAdrian Chadd 		TAILQ_REMOVE(&sc->sc_bbuf, avp->av_bcbuf, bf_list);
1275b032f27cSSam Leffler 		if (opmode != IEEE80211_M_IBSS || !sc->sc_hasveol) {
1276b032f27cSSam Leffler 			/*
1277b032f27cSSam Leffler 			 * Assign the vap to a beacon xmit slot.  As above
1278b032f27cSSam Leffler 			 * this cannot fail to find a free one.
1279b032f27cSSam Leffler 			 */
1280b032f27cSSam Leffler 			avp->av_bslot = assign_bslot(sc);
1281b032f27cSSam Leffler 			KASSERT(sc->sc_bslot[avp->av_bslot] == NULL,
1282b032f27cSSam Leffler 			    ("beacon slot %u not empty", avp->av_bslot));
1283b032f27cSSam Leffler 			sc->sc_bslot[avp->av_bslot] = vap;
1284b032f27cSSam Leffler 			sc->sc_nbcnvaps++;
1285b032f27cSSam Leffler 		}
1286b032f27cSSam Leffler 		if (sc->sc_hastsfadd && sc->sc_nbcnvaps > 0) {
1287b032f27cSSam Leffler 			/*
1288b032f27cSSam Leffler 			 * Multple vaps are to transmit beacons and we
1289b032f27cSSam Leffler 			 * have h/w support for TSF adjusting; enable
1290b032f27cSSam Leffler 			 * use of staggered beacons.
1291b032f27cSSam Leffler 			 */
1292b032f27cSSam Leffler 			sc->sc_stagbeacons = 1;
1293b032f27cSSam Leffler 		}
1294b032f27cSSam Leffler 		ath_txq_init(sc, &avp->av_mcastq, ATH_TXQ_SWQ);
1295b032f27cSSam Leffler 	}
1296b032f27cSSam Leffler 
1297b032f27cSSam Leffler 	ic->ic_opmode = ic_opmode;
1298b032f27cSSam Leffler 	if (opmode != IEEE80211_M_WDS) {
1299b032f27cSSam Leffler 		sc->sc_nvaps++;
1300b032f27cSSam Leffler 		if (opmode == IEEE80211_M_STA)
1301b032f27cSSam Leffler 			sc->sc_nstavaps++;
1302fe0dd789SSam Leffler 		if (opmode == IEEE80211_M_MBSS)
1303fe0dd789SSam Leffler 			sc->sc_nmeshvaps++;
1304b032f27cSSam Leffler 	}
1305b032f27cSSam Leffler 	switch (ic_opmode) {
1306b032f27cSSam Leffler 	case IEEE80211_M_IBSS:
1307b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_IBSS;
1308b032f27cSSam Leffler 		break;
1309b032f27cSSam Leffler 	case IEEE80211_M_STA:
1310b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_STA;
1311b032f27cSSam Leffler 		break;
1312b032f27cSSam Leffler 	case IEEE80211_M_AHDEMO:
1313584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
131410ad9a77SSam Leffler 		if (vap->iv_caps & IEEE80211_C_TDMA) {
131510ad9a77SSam Leffler 			sc->sc_tdma = 1;
131610ad9a77SSam Leffler 			/* NB: disable tsf adjust */
131710ad9a77SSam Leffler 			sc->sc_stagbeacons = 0;
131810ad9a77SSam Leffler 		}
131910ad9a77SSam Leffler 		/*
132010ad9a77SSam Leffler 		 * NB: adhoc demo mode is a pseudo mode; to the hal it's
132110ad9a77SSam Leffler 		 * just ap mode.
132210ad9a77SSam Leffler 		 */
132310ad9a77SSam Leffler 		/* fall thru... */
132410ad9a77SSam Leffler #endif
1325b032f27cSSam Leffler 	case IEEE80211_M_HOSTAP:
132659aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
1327b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_HOSTAP;
1328b032f27cSSam Leffler 		break;
1329b032f27cSSam Leffler 	case IEEE80211_M_MONITOR:
1330b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_MONITOR;
1331b032f27cSSam Leffler 		break;
1332b032f27cSSam Leffler 	default:
1333b032f27cSSam Leffler 		/* XXX should not happen */
1334b032f27cSSam Leffler 		break;
1335b032f27cSSam Leffler 	}
1336b032f27cSSam Leffler 	if (sc->sc_hastsfadd) {
1337b032f27cSSam Leffler 		/*
1338b032f27cSSam Leffler 		 * Configure whether or not TSF adjust should be done.
1339b032f27cSSam Leffler 		 */
1340b032f27cSSam Leffler 		ath_hal_settsfadjust(sc->sc_ah, sc->sc_stagbeacons);
1341b032f27cSSam Leffler 	}
134210ad9a77SSam Leffler 	if (flags & IEEE80211_CLONE_NOBEACONS) {
134310ad9a77SSam Leffler 		/*
134410ad9a77SSam Leffler 		 * Enable s/w beacon miss handling.
134510ad9a77SSam Leffler 		 */
134610ad9a77SSam Leffler 		sc->sc_swbmiss = 1;
134710ad9a77SSam Leffler 	}
1348b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1349b032f27cSSam Leffler 
1350b032f27cSSam Leffler 	/* complete setup */
1351b032f27cSSam Leffler 	ieee80211_vap_attach(vap, ath_media_change, ieee80211_media_status);
1352b032f27cSSam Leffler 	return vap;
1353b032f27cSSam Leffler bad2:
1354b032f27cSSam Leffler 	reclaim_address(sc, mac);
1355b032f27cSSam Leffler 	ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
1356b032f27cSSam Leffler bad:
1357b032f27cSSam Leffler 	free(avp, M_80211_VAP);
1358b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1359b032f27cSSam Leffler 	return NULL;
1360b032f27cSSam Leffler }
1361b032f27cSSam Leffler 
1362b032f27cSSam Leffler static void
1363b032f27cSSam Leffler ath_vap_delete(struct ieee80211vap *vap)
1364b032f27cSSam Leffler {
1365b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1366b032f27cSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
1367b032f27cSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
1368b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1369b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
1370b032f27cSSam Leffler 
1371f52d3452SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: called\n", __func__);
1372b032f27cSSam Leffler 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1373b032f27cSSam Leffler 		/*
1374b032f27cSSam Leffler 		 * Quiesce the hardware while we remove the vap.  In
1375b032f27cSSam Leffler 		 * particular we need to reclaim all references to
1376b032f27cSSam Leffler 		 * the vap state by any frames pending on the tx queues.
1377b032f27cSSam Leffler 		 */
1378b032f27cSSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
1379517526efSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_DEFAULT);		/* stop hw xmit side */
1380517526efSAdrian Chadd 		/* XXX Do all frames from all vaps/nodes need draining here? */
13819a842e8bSAdrian Chadd 		ath_stoprecv(sc, 1);		/* stop recv side */
1382b032f27cSSam Leffler 	}
1383b032f27cSSam Leffler 
1384b032f27cSSam Leffler 	ieee80211_vap_detach(vap);
138516d4de92SAdrian Chadd 
138616d4de92SAdrian Chadd 	/*
138716d4de92SAdrian Chadd 	 * XXX Danger Will Robinson! Danger!
138816d4de92SAdrian Chadd 	 *
138916d4de92SAdrian Chadd 	 * Because ieee80211_vap_detach() can queue a frame (the station
139016d4de92SAdrian Chadd 	 * diassociate message?) after we've drained the TXQ and
139116d4de92SAdrian Chadd 	 * flushed the software TXQ, we will end up with a frame queued
139216d4de92SAdrian Chadd 	 * to a node whose vap is about to be freed.
139316d4de92SAdrian Chadd 	 *
139416d4de92SAdrian Chadd 	 * To work around this, flush the hardware/software again.
139516d4de92SAdrian Chadd 	 * This may be racy - the ath task may be running and the packet
139616d4de92SAdrian Chadd 	 * may be being scheduled between sw->hw txq. Tsk.
139716d4de92SAdrian Chadd 	 *
139816d4de92SAdrian Chadd 	 * TODO: figure out why a new node gets allocated somewhere around
139916d4de92SAdrian Chadd 	 * here (after the ath_tx_swq() call; and after an ath_stop_locked()
140016d4de92SAdrian Chadd 	 * call!)
140116d4de92SAdrian Chadd 	 */
140216d4de92SAdrian Chadd 
140316d4de92SAdrian Chadd 	ath_draintxq(sc, ATH_RESET_DEFAULT);
140416d4de92SAdrian Chadd 
1405b032f27cSSam Leffler 	ATH_LOCK(sc);
1406b032f27cSSam Leffler 	/*
1407b032f27cSSam Leffler 	 * Reclaim beacon state.  Note this must be done before
1408b032f27cSSam Leffler 	 * the vap instance is reclaimed as we may have a reference
1409b032f27cSSam Leffler 	 * to it in the buffer for the beacon frame.
1410b032f27cSSam Leffler 	 */
1411b032f27cSSam Leffler 	if (avp->av_bcbuf != NULL) {
1412b032f27cSSam Leffler 		if (avp->av_bslot != -1) {
1413b032f27cSSam Leffler 			sc->sc_bslot[avp->av_bslot] = NULL;
1414b032f27cSSam Leffler 			sc->sc_nbcnvaps--;
1415b032f27cSSam Leffler 		}
1416b032f27cSSam Leffler 		ath_beacon_return(sc, avp->av_bcbuf);
1417b032f27cSSam Leffler 		avp->av_bcbuf = NULL;
1418b032f27cSSam Leffler 		if (sc->sc_nbcnvaps == 0) {
1419b032f27cSSam Leffler 			sc->sc_stagbeacons = 0;
1420b032f27cSSam Leffler 			if (sc->sc_hastsfadd)
1421b032f27cSSam Leffler 				ath_hal_settsfadjust(sc->sc_ah, 0);
1422b032f27cSSam Leffler 		}
1423b032f27cSSam Leffler 		/*
1424b032f27cSSam Leffler 		 * Reclaim any pending mcast frames for the vap.
1425b032f27cSSam Leffler 		 */
1426b032f27cSSam Leffler 		ath_tx_draintxq(sc, &avp->av_mcastq);
1427b032f27cSSam Leffler 	}
1428b032f27cSSam Leffler 	/*
1429b032f27cSSam Leffler 	 * Update bookkeeping.
1430b032f27cSSam Leffler 	 */
1431b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA) {
1432b032f27cSSam Leffler 		sc->sc_nstavaps--;
1433b032f27cSSam Leffler 		if (sc->sc_nstavaps == 0 && sc->sc_swbmiss)
1434b032f27cSSam Leffler 			sc->sc_swbmiss = 0;
143559aa14a9SRui Paulo 	} else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
143659aa14a9SRui Paulo 	    vap->iv_opmode == IEEE80211_M_MBSS) {
1437b032f27cSSam Leffler 		reclaim_address(sc, vap->iv_myaddr);
1438b032f27cSSam Leffler 		ath_hal_setbssidmask(ah, sc->sc_hwbssidmask);
1439fe0dd789SSam Leffler 		if (vap->iv_opmode == IEEE80211_M_MBSS)
1440fe0dd789SSam Leffler 			sc->sc_nmeshvaps--;
1441b032f27cSSam Leffler 	}
1442b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_WDS)
1443b032f27cSSam Leffler 		sc->sc_nvaps--;
1444584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
144510ad9a77SSam Leffler 	/* TDMA operation ceases when the last vap is destroyed */
144610ad9a77SSam Leffler 	if (sc->sc_tdma && sc->sc_nvaps == 0) {
144710ad9a77SSam Leffler 		sc->sc_tdma = 0;
144810ad9a77SSam Leffler 		sc->sc_swbmiss = 0;
144910ad9a77SSam Leffler 	}
145010ad9a77SSam Leffler #endif
1451b032f27cSSam Leffler 	free(avp, M_80211_VAP);
1452b032f27cSSam Leffler 
1453b032f27cSSam Leffler 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1454b032f27cSSam Leffler 		/*
1455b032f27cSSam Leffler 		 * Restart rx+tx machines if still running (RUNNING will
1456b032f27cSSam Leffler 		 * be reset if we just destroyed the last vap).
1457b032f27cSSam Leffler 		 */
1458b032f27cSSam Leffler 		if (ath_startrecv(sc) != 0)
1459b032f27cSSam Leffler 			if_printf(ifp, "%s: unable to restart recv logic\n",
1460b032f27cSSam Leffler 			    __func__);
1461c89b957aSSam Leffler 		if (sc->sc_beacons) {		/* restart beacons */
1462c89b957aSSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
1463c89b957aSSam Leffler 			if (sc->sc_tdma)
1464c89b957aSSam Leffler 				ath_tdma_config(sc, NULL);
1465c89b957aSSam Leffler 			else
1466c89b957aSSam Leffler #endif
1467b032f27cSSam Leffler 				ath_beacon_config(sc, NULL);
1468c89b957aSSam Leffler 		}
1469b032f27cSSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
1470b032f27cSSam Leffler 	}
147116d4de92SAdrian Chadd 	ATH_UNLOCK(sc);
1472b032f27cSSam Leffler }
1473b032f27cSSam Leffler 
14745591b213SSam Leffler void
14755591b213SSam Leffler ath_suspend(struct ath_softc *sc)
14765591b213SSam Leffler {
1477fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
1478d3ac945bSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
14795591b213SSam Leffler 
1480c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1481c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
14825591b213SSam Leffler 
1483d3ac945bSSam Leffler 	sc->sc_resume_up = (ifp->if_flags & IFF_UP) != 0;
1484d1328898SAdrian Chadd 
1485d3ac945bSSam Leffler 	ieee80211_suspend_all(ic);
1486d3ac945bSSam Leffler 	/*
1487d3ac945bSSam Leffler 	 * NB: don't worry about putting the chip in low power
1488d3ac945bSSam Leffler 	 * mode; pci will power off our socket on suspend and
1489f29b8b7fSWarner Losh 	 * CardBus detaches the device.
1490d3ac945bSSam Leffler 	 */
1491d73df6d5SAdrian Chadd 
1492ae2a0aa4SAdrian Chadd 	/*
1493ae2a0aa4SAdrian Chadd 	 * XXX ensure none of the taskqueues are running
1494ae2a0aa4SAdrian Chadd 	 * XXX ensure sc_invalid is 1
1495ae2a0aa4SAdrian Chadd 	 * XXX ensure the calibration callout is disabled
1496ae2a0aa4SAdrian Chadd 	 */
1497ae2a0aa4SAdrian Chadd 
1498ae2a0aa4SAdrian Chadd 	/* Disable the PCIe PHY, complete with workarounds */
1499ae2a0aa4SAdrian Chadd 	ath_hal_enablepcie(sc->sc_ah, 1, 1);
1500d3ac945bSSam Leffler }
1501d3ac945bSSam Leffler 
1502d3ac945bSSam Leffler /*
1503d3ac945bSSam Leffler  * Reset the key cache since some parts do not reset the
1504d3ac945bSSam Leffler  * contents on resume.  First we clear all entries, then
1505d3ac945bSSam Leffler  * re-load keys that the 802.11 layer assumes are setup
1506d3ac945bSSam Leffler  * in h/w.
1507d3ac945bSSam Leffler  */
1508d3ac945bSSam Leffler static void
1509d3ac945bSSam Leffler ath_reset_keycache(struct ath_softc *sc)
1510d3ac945bSSam Leffler {
1511d3ac945bSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
1512d3ac945bSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
1513d3ac945bSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1514d3ac945bSSam Leffler 	int i;
1515d3ac945bSSam Leffler 
1516d3ac945bSSam Leffler 	for (i = 0; i < sc->sc_keymax; i++)
1517d3ac945bSSam Leffler 		ath_hal_keyreset(ah, i);
1518d3ac945bSSam Leffler 	ieee80211_crypto_reload_keys(ic);
15195591b213SSam Leffler }
15205591b213SSam Leffler 
15216322256bSAdrian Chadd /*
15226322256bSAdrian Chadd  * Fetch the current chainmask configuration based on the current
15236322256bSAdrian Chadd  * operating channel and options.
15246322256bSAdrian Chadd  */
15256322256bSAdrian Chadd static void
15266322256bSAdrian Chadd ath_update_chainmasks(struct ath_softc *sc, struct ieee80211_channel *chan)
15276322256bSAdrian Chadd {
15286322256bSAdrian Chadd 
15296322256bSAdrian Chadd 	/*
15306322256bSAdrian Chadd 	 * Set TX chainmask to the currently configured chainmask;
15316322256bSAdrian Chadd 	 * the TX chainmask depends upon the current operating mode.
15326322256bSAdrian Chadd 	 */
15336322256bSAdrian Chadd 	sc->sc_cur_rxchainmask = sc->sc_rxchainmask;
15346322256bSAdrian Chadd 	if (IEEE80211_IS_CHAN_HT(chan)) {
15356322256bSAdrian Chadd 		sc->sc_cur_txchainmask = sc->sc_txchainmask;
15366322256bSAdrian Chadd 	} else {
15376322256bSAdrian Chadd 		sc->sc_cur_txchainmask = 1;
15386322256bSAdrian Chadd 	}
15397904f516SAdrian Chadd 
15407904f516SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET,
15417904f516SAdrian Chadd 	    "%s: TX chainmask is now 0x%x, RX is now 0x%x\n",
15427904f516SAdrian Chadd 	    __func__,
15437904f516SAdrian Chadd 	    sc->sc_cur_txchainmask,
15447904f516SAdrian Chadd 	    sc->sc_cur_rxchainmask);
15456322256bSAdrian Chadd }
15466322256bSAdrian Chadd 
15475591b213SSam Leffler void
15485591b213SSam Leffler ath_resume(struct ath_softc *sc)
15495591b213SSam Leffler {
1550fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
1551d3ac945bSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
1552d3ac945bSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1553d3ac945bSSam Leffler 	HAL_STATUS status;
15545591b213SSam Leffler 
1555c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1556c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
15575591b213SSam Leffler 
1558d73df6d5SAdrian Chadd 	/* Re-enable PCIe, re-enable the PCIe bus */
1559ae2a0aa4SAdrian Chadd 	ath_hal_enablepcie(ah, 0, 0);
1560d73df6d5SAdrian Chadd 
1561d3ac945bSSam Leffler 	/*
1562d3ac945bSSam Leffler 	 * Must reset the chip before we reload the
1563d3ac945bSSam Leffler 	 * keycache as we were powered down on suspend.
1564d3ac945bSSam Leffler 	 */
15656322256bSAdrian Chadd 	ath_update_chainmasks(sc,
15666322256bSAdrian Chadd 	    sc->sc_curchan != NULL ? sc->sc_curchan : ic->ic_curchan);
15676322256bSAdrian Chadd 	ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
15686322256bSAdrian Chadd 	    sc->sc_cur_rxchainmask);
1569054d7b69SSam Leffler 	ath_hal_reset(ah, sc->sc_opmode,
1570054d7b69SSam Leffler 	    sc->sc_curchan != NULL ? sc->sc_curchan : ic->ic_curchan,
1571054d7b69SSam Leffler 	    AH_FALSE, &status);
1572d3ac945bSSam Leffler 	ath_reset_keycache(sc);
15737e5eb44dSAdrian Chadd 
15747e5eb44dSAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
15757e5eb44dSAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
15767e5eb44dSAdrian Chadd 
15779af351f9SAdrian Chadd 	/* Let spectral at in case spectral is enabled */
15789af351f9SAdrian Chadd 	ath_spectral_enable(sc, ic->ic_curchan);
15799af351f9SAdrian Chadd 
1580a497cd88SAdrian Chadd 	/* Restore the LED configuration */
1581a497cd88SAdrian Chadd 	ath_led_config(sc);
1582a497cd88SAdrian Chadd 	ath_hal_setledstate(ah, HAL_LED_INIT);
1583a497cd88SAdrian Chadd 
1584d1328898SAdrian Chadd 	if (sc->sc_resume_up)
1585021a0db5SAdrian Chadd 		ieee80211_resume_all(ic);
15862fd9aabbSAdrian Chadd 
15872fd9aabbSAdrian Chadd 	/* XXX beacons ? */
15886b59f5e3SSam Leffler }
15895591b213SSam Leffler 
15905591b213SSam Leffler void
15915591b213SSam Leffler ath_shutdown(struct ath_softc *sc)
15925591b213SSam Leffler {
1593fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
15945591b213SSam Leffler 
1595c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1596c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
15975591b213SSam Leffler 
15985591b213SSam Leffler 	ath_stop(ifp);
1599d3ac945bSSam Leffler 	/* NB: no point powering down chip as we're about to reboot */
16005591b213SSam Leffler }
16015591b213SSam Leffler 
1602c42a7b7eSSam Leffler /*
1603c42a7b7eSSam Leffler  * Interrupt handler.  Most of the actual processing is deferred.
1604c42a7b7eSSam Leffler  */
16055591b213SSam Leffler void
16065591b213SSam Leffler ath_intr(void *arg)
16075591b213SSam Leffler {
16085591b213SSam Leffler 	struct ath_softc *sc = arg;
1609fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
16105591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
16116f5fe81eSAdrian Chadd 	HAL_INT status = 0;
16128f939e79SAdrian Chadd 	uint32_t txqs;
16135591b213SSam Leffler 
1614ef27340cSAdrian Chadd 	/*
1615ef27340cSAdrian Chadd 	 * If we're inside a reset path, just print a warning and
1616ef27340cSAdrian Chadd 	 * clear the ISR. The reset routine will finish it for us.
1617ef27340cSAdrian Chadd 	 */
1618ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
1619ef27340cSAdrian Chadd 	if (sc->sc_inreset_cnt) {
1620ef27340cSAdrian Chadd 		HAL_INT status;
1621ef27340cSAdrian Chadd 		ath_hal_getisr(ah, &status);	/* clear ISR */
1622ef27340cSAdrian Chadd 		ath_hal_intrset(ah, 0);		/* disable further intr's */
1623ef27340cSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_ANY,
1624ef27340cSAdrian Chadd 		    "%s: in reset, ignoring: status=0x%x\n",
1625ef27340cSAdrian Chadd 		    __func__, status);
1626ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
1627ef27340cSAdrian Chadd 		return;
1628ef27340cSAdrian Chadd 	}
1629ef27340cSAdrian Chadd 
16305591b213SSam Leffler 	if (sc->sc_invalid) {
16315591b213SSam Leffler 		/*
1632b58b3803SSam Leffler 		 * The hardware is not ready/present, don't touch anything.
1633b58b3803SSam Leffler 		 * Note this can happen early on if the IRQ is shared.
16345591b213SSam Leffler 		 */
1635c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid; ignored\n", __func__);
1636ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
16375591b213SSam Leffler 		return;
16385591b213SSam Leffler 	}
1639ef27340cSAdrian Chadd 	if (!ath_hal_intrpend(ah)) {		/* shared irq, not for us */
1640ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
1641fdd758d4SSam Leffler 		return;
1642ef27340cSAdrian Chadd 	}
1643ef27340cSAdrian Chadd 
164468e8e04eSSam Leffler 	if ((ifp->if_flags & IFF_UP) == 0 ||
164568e8e04eSSam Leffler 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
164668e8e04eSSam Leffler 		HAL_INT status;
164768e8e04eSSam Leffler 
1648c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
1649c42a7b7eSSam Leffler 			__func__, ifp->if_flags);
16505591b213SSam Leffler 		ath_hal_getisr(ah, &status);	/* clear ISR */
16515591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable further intr's */
1652ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
16535591b213SSam Leffler 		return;
16545591b213SSam Leffler 	}
1655ef27340cSAdrian Chadd 
1656c42a7b7eSSam Leffler 	/*
1657c42a7b7eSSam Leffler 	 * Figure out the reason(s) for the interrupt.  Note
1658c42a7b7eSSam Leffler 	 * that the hal returns a pseudo-ISR that may include
1659c42a7b7eSSam Leffler 	 * bits we haven't explicitly enabled so we mask the
1660c42a7b7eSSam Leffler 	 * value to insure we only process bits we requested.
1661c42a7b7eSSam Leffler 	 */
16625591b213SSam Leffler 	ath_hal_getisr(ah, &status);		/* NB: clears ISR too */
1663c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_INTR, "%s: status 0x%x\n", __func__, status);
166403682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_INTERRUPTS, 1, "ath_intr: mask=0x%.8x", status);
1665a26f3327SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
1666a26f3327SAdrian Chadd 	if_ath_alq_post_intr(&sc->sc_alq, status, ah->ah_intrstate,
1667a26f3327SAdrian Chadd 	    ah->ah_syncstate);
1668a26f3327SAdrian Chadd #endif	/* ATH_DEBUG_ALQ */
166931fdf3d6SAdrian Chadd #ifdef	ATH_KTR_INTR_DEBUG
167003682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_INTERRUPTS, 5,
1671f52d3452SAdrian Chadd 	    "ath_intr: ISR=0x%.8x, ISR_S0=0x%.8x, ISR_S1=0x%.8x, ISR_S2=0x%.8x, ISR_S5=0x%.8x",
1672f52d3452SAdrian Chadd 	    ah->ah_intrstate[0],
1673f52d3452SAdrian Chadd 	    ah->ah_intrstate[1],
1674f52d3452SAdrian Chadd 	    ah->ah_intrstate[2],
1675f52d3452SAdrian Chadd 	    ah->ah_intrstate[3],
1676f52d3452SAdrian Chadd 	    ah->ah_intrstate[6]);
167731fdf3d6SAdrian Chadd #endif
16789467e3f3SAdrian Chadd 
16799467e3f3SAdrian Chadd 	/* Squirrel away SYNC interrupt debugging */
16809467e3f3SAdrian Chadd 	if (ah->ah_syncstate != 0) {
16819467e3f3SAdrian Chadd 		int i;
16829467e3f3SAdrian Chadd 		for (i = 0; i < 32; i++)
16839467e3f3SAdrian Chadd 			if (ah->ah_syncstate & (i << i))
16849467e3f3SAdrian Chadd 				sc->sc_intr_stats.sync_intr[i]++;
16859467e3f3SAdrian Chadd 	}
16869467e3f3SAdrian Chadd 
1687ecddff40SSam Leffler 	status &= sc->sc_imask;			/* discard unasked for bits */
16886f5fe81eSAdrian Chadd 
16896f5fe81eSAdrian Chadd 	/* Short-circuit un-handled interrupts */
1690ef27340cSAdrian Chadd 	if (status == 0x0) {
1691ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
16926f5fe81eSAdrian Chadd 		return;
1693ef27340cSAdrian Chadd 	}
16946f5fe81eSAdrian Chadd 
1695ef27340cSAdrian Chadd 	/*
1696ef27340cSAdrian Chadd 	 * Take a note that we're inside the interrupt handler, so
1697ef27340cSAdrian Chadd 	 * the reset routines know to wait.
1698ef27340cSAdrian Chadd 	 */
1699ef27340cSAdrian Chadd 	sc->sc_intr_cnt++;
1700ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
1701ef27340cSAdrian Chadd 
1702ef27340cSAdrian Chadd 	/*
1703ef27340cSAdrian Chadd 	 * Handle the interrupt. We won't run concurrent with the reset
1704ef27340cSAdrian Chadd 	 * or channel change routines as they'll wait for sc_intr_cnt
1705ef27340cSAdrian Chadd 	 * to be 0 before continuing.
1706ef27340cSAdrian Chadd 	 */
17075591b213SSam Leffler 	if (status & HAL_INT_FATAL) {
17085591b213SSam Leffler 		sc->sc_stats.ast_hardware++;
17095591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
1710f846cf42SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_fataltask);
17115591b213SSam Leffler 	} else {
1712c42a7b7eSSam Leffler 		if (status & HAL_INT_SWBA) {
1713c42a7b7eSSam Leffler 			/*
1714c42a7b7eSSam Leffler 			 * Software beacon alert--time to send a beacon.
1715c42a7b7eSSam Leffler 			 * Handle beacon transmission directly; deferring
1716c42a7b7eSSam Leffler 			 * this is too slow to meet timing constraints
1717c42a7b7eSSam Leffler 			 * under load.
1718c42a7b7eSSam Leffler 			 */
1719584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
172010ad9a77SSam Leffler 			if (sc->sc_tdma) {
172110ad9a77SSam Leffler 				if (sc->sc_tdmaswba == 0) {
172210ad9a77SSam Leffler 					struct ieee80211com *ic = ifp->if_l2com;
172310ad9a77SSam Leffler 					struct ieee80211vap *vap =
172410ad9a77SSam Leffler 					    TAILQ_FIRST(&ic->ic_vaps);
172510ad9a77SSam Leffler 					ath_tdma_beacon_send(sc, vap);
172610ad9a77SSam Leffler 					sc->sc_tdmaswba =
172710ad9a77SSam Leffler 					    vap->iv_tdma->tdma_bintval;
172810ad9a77SSam Leffler 				} else
172910ad9a77SSam Leffler 					sc->sc_tdmaswba--;
173010ad9a77SSam Leffler 			} else
173110ad9a77SSam Leffler #endif
1732339ccfb3SSam Leffler 			{
1733c42a7b7eSSam Leffler 				ath_beacon_proc(sc, 0);
1734339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
1735339ccfb3SSam Leffler 				/*
1736339ccfb3SSam Leffler 				 * Schedule the rx taskq in case there's no
1737339ccfb3SSam Leffler 				 * traffic so any frames held on the staging
1738339ccfb3SSam Leffler 				 * queue are aged and potentially flushed.
1739339ccfb3SSam Leffler 				 */
1740f0db652cSAdrian Chadd 				sc->sc_rx.recv_sched(sc, 1);
1741339ccfb3SSam Leffler #endif
1742339ccfb3SSam Leffler 			}
1743c42a7b7eSSam Leffler 		}
17445591b213SSam Leffler 		if (status & HAL_INT_RXEOL) {
17458f939e79SAdrian Chadd 			int imask;
174603682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_ERROR, 0, "ath_intr: RXEOL");
1747ef27340cSAdrian Chadd 			ATH_PCU_LOCK(sc);
17485591b213SSam Leffler 			/*
17495591b213SSam Leffler 			 * NB: the hardware should re-read the link when
17505591b213SSam Leffler 			 *     RXE bit is written, but it doesn't work at
17515591b213SSam Leffler 			 *     least on older hardware revs.
17525591b213SSam Leffler 			 */
17535591b213SSam Leffler 			sc->sc_stats.ast_rxeol++;
175473f895fcSAdrian Chadd 			/*
175573f895fcSAdrian Chadd 			 * Disable RXEOL/RXORN - prevent an interrupt
175673f895fcSAdrian Chadd 			 * storm until the PCU logic can be reset.
17571fdadc0fSAdrian Chadd 			 * In case the interface is reset some other
17581fdadc0fSAdrian Chadd 			 * way before "sc_kickpcu" is called, don't
17591fdadc0fSAdrian Chadd 			 * modify sc_imask - that way if it is reset
17601fdadc0fSAdrian Chadd 			 * by a call to ath_reset() somehow, the
17611fdadc0fSAdrian Chadd 			 * interrupt mask will be correctly reprogrammed.
176273f895fcSAdrian Chadd 			 */
17638f939e79SAdrian Chadd 			imask = sc->sc_imask;
17641fdadc0fSAdrian Chadd 			imask &= ~(HAL_INT_RXEOL | HAL_INT_RXORN);
17651fdadc0fSAdrian Chadd 			ath_hal_intrset(ah, imask);
17661fdadc0fSAdrian Chadd 			/*
17678f939e79SAdrian Chadd 			 * Only blank sc_rxlink if we've not yet kicked
17688f939e79SAdrian Chadd 			 * the PCU.
17698f939e79SAdrian Chadd 			 *
17708f939e79SAdrian Chadd 			 * This isn't entirely correct - the correct solution
17718f939e79SAdrian Chadd 			 * would be to have a PCU lock and engage that for
17728f939e79SAdrian Chadd 			 * the duration of the PCU fiddling; which would include
17738f939e79SAdrian Chadd 			 * running the RX process. Otherwise we could end up
17748f939e79SAdrian Chadd 			 * messing up the RX descriptor chain and making the
17758f939e79SAdrian Chadd 			 * RX desc list much shorter.
17768f939e79SAdrian Chadd 			 */
17778f939e79SAdrian Chadd 			if (! sc->sc_kickpcu)
17788f939e79SAdrian Chadd 				sc->sc_rxlink = NULL;
17798f939e79SAdrian Chadd 			sc->sc_kickpcu = 1;
1780f0db652cSAdrian Chadd 			ATH_PCU_UNLOCK(sc);
17818f939e79SAdrian Chadd 			/*
17821fdadc0fSAdrian Chadd 			 * Enqueue an RX proc, to handled whatever
17831fdadc0fSAdrian Chadd 			 * is in the RX queue.
17841fdadc0fSAdrian Chadd 			 * This will then kick the PCU.
17851fdadc0fSAdrian Chadd 			 */
1786f0db652cSAdrian Chadd 			sc->sc_rx.recv_sched(sc, 1);
17875591b213SSam Leffler 		}
17885591b213SSam Leffler 		if (status & HAL_INT_TXURN) {
17895591b213SSam Leffler 			sc->sc_stats.ast_txurn++;
17905591b213SSam Leffler 			/* bump tx trigger level */
17915591b213SSam Leffler 			ath_hal_updatetxtriglevel(ah, AH_TRUE);
17925591b213SSam Leffler 		}
1793bcbb08ceSAdrian Chadd 		/*
1794bcbb08ceSAdrian Chadd 		 * Handle both the legacy and RX EDMA interrupt bits.
1795bcbb08ceSAdrian Chadd 		 * Note that HAL_INT_RXLP is also HAL_INT_RXDESC.
1796bcbb08ceSAdrian Chadd 		 */
1797bcbb08ceSAdrian Chadd 		if (status & (HAL_INT_RX | HAL_INT_RXHP | HAL_INT_RXLP)) {
17988f939e79SAdrian Chadd 			sc->sc_stats.ast_rx_intr++;
1799f0db652cSAdrian Chadd 			sc->sc_rx.recv_sched(sc, 1);
18008f939e79SAdrian Chadd 		}
18018f939e79SAdrian Chadd 		if (status & HAL_INT_TX) {
18028f939e79SAdrian Chadd 			sc->sc_stats.ast_tx_intr++;
18038f939e79SAdrian Chadd 			/*
18048f939e79SAdrian Chadd 			 * Grab all the currently set bits in the HAL txq bitmap
18058f939e79SAdrian Chadd 			 * and blank them. This is the only place we should be
18068f939e79SAdrian Chadd 			 * doing this.
18078f939e79SAdrian Chadd 			 */
1808bad98824SAdrian Chadd 			if (! sc->sc_isedma) {
1809ef27340cSAdrian Chadd 				ATH_PCU_LOCK(sc);
18108f939e79SAdrian Chadd 				txqs = 0xffffffff;
18118f939e79SAdrian Chadd 				ath_hal_gettxintrtxqs(sc->sc_ah, &txqs);
181203682514SAdrian Chadd 				ATH_KTR(sc, ATH_KTR_INTERRUPTS, 3,
181303682514SAdrian Chadd 				    "ath_intr: TX; txqs=0x%08x, txq_active was 0x%08x, now 0x%08x",
181403682514SAdrian Chadd 				    txqs,
181503682514SAdrian Chadd 				    sc->sc_txq_active,
181603682514SAdrian Chadd 				    sc->sc_txq_active | txqs);
18178f939e79SAdrian Chadd 				sc->sc_txq_active |= txqs;
1818ef27340cSAdrian Chadd 				ATH_PCU_UNLOCK(sc);
18198f939e79SAdrian Chadd 			}
1820bad98824SAdrian Chadd 			taskqueue_enqueue(sc->sc_tq, &sc->sc_txtask);
1821bad98824SAdrian Chadd 		}
18225591b213SSam Leffler 		if (status & HAL_INT_BMISS) {
18235591b213SSam Leffler 			sc->sc_stats.ast_bmiss++;
18240bbf5441SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_bmisstask);
18255591b213SSam Leffler 		}
18266ad02dbaSAdrian Chadd 		if (status & HAL_INT_GTT)
18276ad02dbaSAdrian Chadd 			sc->sc_stats.ast_tx_timeout++;
18285594f5c0SAdrian Chadd 		if (status & HAL_INT_CST)
18295594f5c0SAdrian Chadd 			sc->sc_stats.ast_tx_cst++;
1830c42a7b7eSSam Leffler 		if (status & HAL_INT_MIB) {
1831c42a7b7eSSam Leffler 			sc->sc_stats.ast_mib++;
1832ef27340cSAdrian Chadd 			ATH_PCU_LOCK(sc);
1833c42a7b7eSSam Leffler 			/*
1834c42a7b7eSSam Leffler 			 * Disable interrupts until we service the MIB
1835c42a7b7eSSam Leffler 			 * interrupt; otherwise it will continue to fire.
1836c42a7b7eSSam Leffler 			 */
1837c42a7b7eSSam Leffler 			ath_hal_intrset(ah, 0);
1838c42a7b7eSSam Leffler 			/*
1839c42a7b7eSSam Leffler 			 * Let the hal handle the event.  We assume it will
1840c42a7b7eSSam Leffler 			 * clear whatever condition caused the interrupt.
1841c42a7b7eSSam Leffler 			 */
1842ffa2cab6SSam Leffler 			ath_hal_mibevent(ah, &sc->sc_halstats);
18438f939e79SAdrian Chadd 			/*
18448f939e79SAdrian Chadd 			 * Don't reset the interrupt if we've just
18458f939e79SAdrian Chadd 			 * kicked the PCU, or we may get a nested
18468f939e79SAdrian Chadd 			 * RXEOL before the rxproc has had a chance
18478f939e79SAdrian Chadd 			 * to run.
18488f939e79SAdrian Chadd 			 */
18498f939e79SAdrian Chadd 			if (sc->sc_kickpcu == 0)
1850c42a7b7eSSam Leffler 				ath_hal_intrset(ah, sc->sc_imask);
1851ef27340cSAdrian Chadd 			ATH_PCU_UNLOCK(sc);
1852c42a7b7eSSam Leffler 		}
18539c4fc1e8SSam Leffler 		if (status & HAL_INT_RXORN) {
18549c4fc1e8SSam Leffler 			/* NB: hal marks HAL_INT_FATAL when RXORN is fatal */
185503682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_ERROR, 0, "ath_intr: RXORN");
18569c4fc1e8SSam Leffler 			sc->sc_stats.ast_rxorn++;
18579c4fc1e8SSam Leffler 		}
18585591b213SSam Leffler 	}
1859ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
1860ef27340cSAdrian Chadd 	sc->sc_intr_cnt--;
1861ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
18625591b213SSam Leffler }
18635591b213SSam Leffler 
18645591b213SSam Leffler static void
18655591b213SSam Leffler ath_fatal_proc(void *arg, int pending)
18665591b213SSam Leffler {
18675591b213SSam Leffler 	struct ath_softc *sc = arg;
1868fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
186916c8acaaSSam Leffler 	u_int32_t *state;
187016c8acaaSSam Leffler 	u_int32_t len;
187168e8e04eSSam Leffler 	void *sp;
18725591b213SSam Leffler 
1873c42a7b7eSSam Leffler 	if_printf(ifp, "hardware error; resetting\n");
187416c8acaaSSam Leffler 	/*
187516c8acaaSSam Leffler 	 * Fatal errors are unrecoverable.  Typically these
187616c8acaaSSam Leffler 	 * are caused by DMA errors.  Collect h/w state from
187716c8acaaSSam Leffler 	 * the hal so we can diagnose what's going on.
187816c8acaaSSam Leffler 	 */
187968e8e04eSSam Leffler 	if (ath_hal_getfatalstate(sc->sc_ah, &sp, &len)) {
188016c8acaaSSam Leffler 		KASSERT(len >= 6*sizeof(u_int32_t), ("len %u bytes", len));
188168e8e04eSSam Leffler 		state = sp;
188216c8acaaSSam Leffler 		if_printf(ifp, "0x%08x 0x%08x 0x%08x, 0x%08x 0x%08x 0x%08x\n",
188316c8acaaSSam Leffler 		    state[0], state[1] , state[2], state[3],
188416c8acaaSSam Leffler 		    state[4], state[5]);
188516c8acaaSSam Leffler 	}
1886517526efSAdrian Chadd 	ath_reset(ifp, ATH_RESET_NOLOSS);
18875591b213SSam Leffler }
18885591b213SSam Leffler 
18895591b213SSam Leffler static void
1890b032f27cSSam Leffler ath_bmiss_vap(struct ieee80211vap *vap)
18915591b213SSam Leffler {
189259fbb257SSam Leffler 	/*
189359fbb257SSam Leffler 	 * Workaround phantom bmiss interrupts by sanity-checking
189459fbb257SSam Leffler 	 * the time of our last rx'd frame.  If it is within the
189559fbb257SSam Leffler 	 * beacon miss interval then ignore the interrupt.  If it's
189659fbb257SSam Leffler 	 * truly a bmiss we'll get another interrupt soon and that'll
189759fbb257SSam Leffler 	 * be dispatched up for processing.  Note this applies only
189859fbb257SSam Leffler 	 * for h/w beacon miss events.
189959fbb257SSam Leffler 	 */
190059fbb257SSam Leffler 	if ((vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) == 0) {
1901a7ace843SSam Leffler 		struct ifnet *ifp = vap->iv_ic->ic_ifp;
1902a7ace843SSam Leffler 		struct ath_softc *sc = ifp->if_softc;
1903d7736e13SSam Leffler 		u_int64_t lastrx = sc->sc_lastrx;
1904d7736e13SSam Leffler 		u_int64_t tsf = ath_hal_gettsf64(sc->sc_ah);
190580767531SAdrian Chadd 		/* XXX should take a locked ref to iv_bss */
1906d7736e13SSam Leffler 		u_int bmisstimeout =
1907b032f27cSSam Leffler 			vap->iv_bmissthreshold * vap->iv_bss->ni_intval * 1024;
1908d7736e13SSam Leffler 
1909d7736e13SSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
1910d7736e13SSam Leffler 		    "%s: tsf %llu lastrx %lld (%llu) bmiss %u\n",
1911d7736e13SSam Leffler 		    __func__, (unsigned long long) tsf,
1912d7736e13SSam Leffler 		    (unsigned long long)(tsf - lastrx),
1913d7736e13SSam Leffler 		    (unsigned long long) lastrx, bmisstimeout);
191459fbb257SSam Leffler 
191559fbb257SSam Leffler 		if (tsf - lastrx <= bmisstimeout) {
1916d7736e13SSam Leffler 			sc->sc_stats.ast_bmiss_phantom++;
191759fbb257SSam Leffler 			return;
191859fbb257SSam Leffler 		}
191959fbb257SSam Leffler 	}
192059fbb257SSam Leffler 	ATH_VAP(vap)->av_bmiss(vap);
1921e585d188SSam Leffler }
1922b032f27cSSam Leffler 
1923b837332dSAdrian Chadd int
1924459bc4f0SSam Leffler ath_hal_gethangstate(struct ath_hal *ah, uint32_t mask, uint32_t *hangs)
1925459bc4f0SSam Leffler {
1926459bc4f0SSam Leffler 	uint32_t rsize;
1927459bc4f0SSam Leffler 	void *sp;
1928459bc4f0SSam Leffler 
192925c96056SAdrian Chadd 	if (!ath_hal_getdiagstate(ah, HAL_DIAG_CHECK_HANGS, &mask, sizeof(mask), &sp, &rsize))
1930459bc4f0SSam Leffler 		return 0;
1931459bc4f0SSam Leffler 	KASSERT(rsize == sizeof(uint32_t), ("resultsize %u", rsize));
1932459bc4f0SSam Leffler 	*hangs = *(uint32_t *)sp;
1933459bc4f0SSam Leffler 	return 1;
1934459bc4f0SSam Leffler }
1935459bc4f0SSam Leffler 
1936b032f27cSSam Leffler static void
1937b032f27cSSam Leffler ath_bmiss_proc(void *arg, int pending)
1938b032f27cSSam Leffler {
1939b032f27cSSam Leffler 	struct ath_softc *sc = arg;
1940b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
1941459bc4f0SSam Leffler 	uint32_t hangs;
1942b032f27cSSam Leffler 
1943b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: pending %u\n", __func__, pending);
1944459bc4f0SSam Leffler 
1945a74ebfe5SAdrian Chadd 	/*
1946a74ebfe5SAdrian Chadd 	 * Do a reset upon any becaon miss event.
1947a74ebfe5SAdrian Chadd 	 *
1948a74ebfe5SAdrian Chadd 	 * It may be a non-recognised RX clear hang which needs a reset
1949a74ebfe5SAdrian Chadd 	 * to clear.
1950a74ebfe5SAdrian Chadd 	 */
1951459bc4f0SSam Leffler 	if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0) {
1952517526efSAdrian Chadd 		ath_reset(ifp, ATH_RESET_NOLOSS);
1953a74ebfe5SAdrian Chadd 		if_printf(ifp, "bb hang detected (0x%x), resetting\n", hangs);
1954a74ebfe5SAdrian Chadd 	} else {
1955a74ebfe5SAdrian Chadd 		ath_reset(ifp, ATH_RESET_NOLOSS);
1956b032f27cSSam Leffler 		ieee80211_beacon_miss(ifp->if_l2com);
19575591b213SSam Leffler 	}
1958a74ebfe5SAdrian Chadd }
19595591b213SSam Leffler 
1960724c193aSSam Leffler /*
1961b032f27cSSam Leffler  * Handle TKIP MIC setup to deal hardware that doesn't do MIC
1962b032f27cSSam Leffler  * calcs together with WME.  If necessary disable the crypto
1963b032f27cSSam Leffler  * hardware and mark the 802.11 state so keys will be setup
1964b032f27cSSam Leffler  * with the MIC work done in software.
1965b032f27cSSam Leffler  */
1966b032f27cSSam Leffler static void
1967b032f27cSSam Leffler ath_settkipmic(struct ath_softc *sc)
1968b032f27cSSam Leffler {
1969b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
1970b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
1971b032f27cSSam Leffler 
1972b032f27cSSam Leffler 	if ((ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIP) && !sc->sc_wmetkipmic) {
1973b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_WME) {
1974b032f27cSSam Leffler 			ath_hal_settkipmic(sc->sc_ah, AH_FALSE);
1975b032f27cSSam Leffler 			ic->ic_cryptocaps &= ~IEEE80211_CRYPTO_TKIPMIC;
1976b032f27cSSam Leffler 		} else {
1977b032f27cSSam Leffler 			ath_hal_settkipmic(sc->sc_ah, AH_TRUE);
1978b032f27cSSam Leffler 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
1979b032f27cSSam Leffler 		}
1980b032f27cSSam Leffler 	}
1981b032f27cSSam Leffler }
1982b032f27cSSam Leffler 
19835591b213SSam Leffler static void
19845591b213SSam Leffler ath_init(void *arg)
19855591b213SSam Leffler {
19865591b213SSam Leffler 	struct ath_softc *sc = (struct ath_softc *) arg;
1987fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
1988b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
19895591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
19905591b213SSam Leffler 	HAL_STATUS status;
19915591b213SSam Leffler 
1992c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
1993c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
19945591b213SSam Leffler 
1995f0b2a0beSSam Leffler 	ATH_LOCK(sc);
19965591b213SSam Leffler 	/*
19975591b213SSam Leffler 	 * Stop anything previously setup.  This is safe
19985591b213SSam Leffler 	 * whether this is the first time through or not.
19995591b213SSam Leffler 	 */
2000c42a7b7eSSam Leffler 	ath_stop_locked(ifp);
20015591b213SSam Leffler 
20025591b213SSam Leffler 	/*
20035591b213SSam Leffler 	 * The basic interface to setting the hardware in a good
20045591b213SSam Leffler 	 * state is ``reset''.  On return the hardware is known to
20055591b213SSam Leffler 	 * be powered up and with interrupts disabled.  This must
20065591b213SSam Leffler 	 * be followed by initialization of the appropriate bits
20075591b213SSam Leffler 	 * and then setup of the interrupt mask.
20085591b213SSam Leffler 	 */
2009b032f27cSSam Leffler 	ath_settkipmic(sc);
20106322256bSAdrian Chadd 	ath_update_chainmasks(sc, ic->ic_curchan);
20116322256bSAdrian Chadd 	ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
20126322256bSAdrian Chadd 	    sc->sc_cur_rxchainmask);
201359efa8b5SSam Leffler 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_FALSE, &status)) {
20145591b213SSam Leffler 		if_printf(ifp, "unable to reset hardware; hal status %u\n",
20155591b213SSam Leffler 			status);
2016b032f27cSSam Leffler 		ATH_UNLOCK(sc);
2017b032f27cSSam Leffler 		return;
20185591b213SSam Leffler 	}
2019b032f27cSSam Leffler 	ath_chan_change(sc, ic->ic_curchan);
20205591b213SSam Leffler 
202148237774SAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
202248237774SAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
202348237774SAdrian Chadd 
20249af351f9SAdrian Chadd 	/* Let spectral at in case spectral is enabled */
20259af351f9SAdrian Chadd 	ath_spectral_enable(sc, ic->ic_curchan);
20269af351f9SAdrian Chadd 
20275591b213SSam Leffler 	/*
2028c59005e9SSam Leffler 	 * Likewise this is set during reset so update
2029c59005e9SSam Leffler 	 * state cached in the driver.
2030c59005e9SSam Leffler 	 */
2031c59005e9SSam Leffler 	sc->sc_diversity = ath_hal_getdiversity(ah);
20322dc7fcc4SSam Leffler 	sc->sc_lastlongcal = 0;
20332dc7fcc4SSam Leffler 	sc->sc_resetcal = 1;
20342dc7fcc4SSam Leffler 	sc->sc_lastcalreset = 0;
2035a108ab63SAdrian Chadd 	sc->sc_lastani = 0;
2036a108ab63SAdrian Chadd 	sc->sc_lastshortcal = 0;
2037a108ab63SAdrian Chadd 	sc->sc_doresetcal = AH_FALSE;
20382fd9aabbSAdrian Chadd 	/*
20392fd9aabbSAdrian Chadd 	 * Beacon timers were cleared here; give ath_newstate()
20402fd9aabbSAdrian Chadd 	 * a hint that the beacon timers should be poked when
20412fd9aabbSAdrian Chadd 	 * things transition to the RUN state.
20422fd9aabbSAdrian Chadd 	 */
20432fd9aabbSAdrian Chadd 	sc->sc_beacons = 0;
2044c42a7b7eSSam Leffler 
2045c42a7b7eSSam Leffler 	/*
20465591b213SSam Leffler 	 * Setup the hardware after reset: the key cache
20475591b213SSam Leffler 	 * is filled as needed and the receive engine is
20485591b213SSam Leffler 	 * set going.  Frame transmit is handled entirely
20495591b213SSam Leffler 	 * in the frame output path; there's nothing to do
20505591b213SSam Leffler 	 * here except setup the interrupt mask.
20515591b213SSam Leffler 	 */
20525591b213SSam Leffler 	if (ath_startrecv(sc) != 0) {
20535591b213SSam Leffler 		if_printf(ifp, "unable to start recv logic\n");
2054b032f27cSSam Leffler 		ATH_UNLOCK(sc);
2055b032f27cSSam Leffler 		return;
20565591b213SSam Leffler 	}
20575591b213SSam Leffler 
20585591b213SSam Leffler 	/*
20595591b213SSam Leffler 	 * Enable interrupts.
20605591b213SSam Leffler 	 */
20615591b213SSam Leffler 	sc->sc_imask = HAL_INT_RX | HAL_INT_TX
20625591b213SSam Leffler 		  | HAL_INT_RXEOL | HAL_INT_RXORN
206369930f87SAdrian Chadd 		  | HAL_INT_TXURN
20645591b213SSam Leffler 		  | HAL_INT_FATAL | HAL_INT_GLOBAL;
2065bcbb08ceSAdrian Chadd 
2066bcbb08ceSAdrian Chadd 	/*
2067bcbb08ceSAdrian Chadd 	 * Enable RX EDMA bits.  Note these overlap with
2068bcbb08ceSAdrian Chadd 	 * HAL_INT_RX and HAL_INT_RXDESC respectively.
2069bcbb08ceSAdrian Chadd 	 */
2070bcbb08ceSAdrian Chadd 	if (sc->sc_isedma)
2071bcbb08ceSAdrian Chadd 		sc->sc_imask |= (HAL_INT_RXHP | HAL_INT_RXLP);
2072bcbb08ceSAdrian Chadd 
2073c42a7b7eSSam Leffler 	/*
2074c42a7b7eSSam Leffler 	 * Enable MIB interrupts when there are hardware phy counters.
2075c42a7b7eSSam Leffler 	 * Note we only do this (at the moment) for station mode.
2076c42a7b7eSSam Leffler 	 */
2077c42a7b7eSSam Leffler 	if (sc->sc_needmib && ic->ic_opmode == IEEE80211_M_STA)
2078c42a7b7eSSam Leffler 		sc->sc_imask |= HAL_INT_MIB;
20795591b213SSam Leffler 
20805594f5c0SAdrian Chadd 	/* Enable global TX timeout and carrier sense timeout if available */
20816ad02dbaSAdrian Chadd 	if (ath_hal_gtxto_supported(ah))
20823788ebedSAdrian Chadd 		sc->sc_imask |= HAL_INT_GTT;
2083d0a0ebc6SAdrian Chadd 
2084d0a0ebc6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: imask=0x%x\n",
2085d0a0ebc6SAdrian Chadd 		__func__, sc->sc_imask);
20866ad02dbaSAdrian Chadd 
208713f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
20882e986da5SSam Leffler 	callout_reset(&sc->sc_wd_ch, hz, ath_watchdog, sc);
2089b032f27cSSam Leffler 	ath_hal_intrset(ah, sc->sc_imask);
20905591b213SSam Leffler 
2091b032f27cSSam Leffler 	ATH_UNLOCK(sc);
2092b032f27cSSam Leffler 
209386e07743SSam Leffler #ifdef ATH_TX99_DIAG
209486e07743SSam Leffler 	if (sc->sc_tx99 != NULL)
209586e07743SSam Leffler 		sc->sc_tx99->start(sc->sc_tx99);
209686e07743SSam Leffler 	else
209786e07743SSam Leffler #endif
2098b032f27cSSam Leffler 	ieee80211_start_all(ic);		/* start all vap's */
20995591b213SSam Leffler }
21005591b213SSam Leffler 
21015591b213SSam Leffler static void
2102c42a7b7eSSam Leffler ath_stop_locked(struct ifnet *ifp)
21035591b213SSam Leffler {
21045591b213SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
21055591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
21065591b213SSam Leffler 
2107c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid %u if_flags 0x%x\n",
2108c42a7b7eSSam Leffler 		__func__, sc->sc_invalid, ifp->if_flags);
21095591b213SSam Leffler 
2110c42a7b7eSSam Leffler 	ATH_LOCK_ASSERT(sc);
211113f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
21125591b213SSam Leffler 		/*
21135591b213SSam Leffler 		 * Shutdown the hardware and driver:
2114c42a7b7eSSam Leffler 		 *    reset 802.11 state machine
21155591b213SSam Leffler 		 *    turn off timers
2116c42a7b7eSSam Leffler 		 *    disable interrupts
2117c42a7b7eSSam Leffler 		 *    turn off the radio
21185591b213SSam Leffler 		 *    clear transmit machinery
21195591b213SSam Leffler 		 *    clear receive machinery
21205591b213SSam Leffler 		 *    drain and release tx queues
21215591b213SSam Leffler 		 *    reclaim beacon resources
21225591b213SSam Leffler 		 *    power down hardware
21235591b213SSam Leffler 		 *
21245591b213SSam Leffler 		 * Note that some of this work is not possible if the
21255591b213SSam Leffler 		 * hardware is gone (invalid).
21265591b213SSam Leffler 		 */
212786e07743SSam Leffler #ifdef ATH_TX99_DIAG
212886e07743SSam Leffler 		if (sc->sc_tx99 != NULL)
212986e07743SSam Leffler 			sc->sc_tx99->stop(sc->sc_tx99);
213086e07743SSam Leffler #endif
21312e986da5SSam Leffler 		callout_stop(&sc->sc_wd_ch);
21322e986da5SSam Leffler 		sc->sc_wd_timer = 0;
213313f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2134c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
21353e50ec2cSSam Leffler 			if (sc->sc_softled) {
21363e50ec2cSSam Leffler 				callout_stop(&sc->sc_ledtimer);
21373e50ec2cSSam Leffler 				ath_hal_gpioset(ah, sc->sc_ledpin,
21383e50ec2cSSam Leffler 					!sc->sc_ledon);
21393e50ec2cSSam Leffler 				sc->sc_blinking = 0;
21403e50ec2cSSam Leffler 			}
21415591b213SSam Leffler 			ath_hal_intrset(ah, 0);
2142c42a7b7eSSam Leffler 		}
2143517526efSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_DEFAULT);
2144c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
21459a842e8bSAdrian Chadd 			ath_stoprecv(sc, 1);
2146c42a7b7eSSam Leffler 			ath_hal_phydisable(ah);
2147c42a7b7eSSam Leffler 		} else
21485591b213SSam Leffler 			sc->sc_rxlink = NULL;
2149b032f27cSSam Leffler 		ath_beacon_free(sc);	/* XXX not needed */
2150c42a7b7eSSam Leffler 	}
2151c42a7b7eSSam Leffler }
2152c42a7b7eSSam Leffler 
2153ef27340cSAdrian Chadd #define	MAX_TXRX_ITERATIONS	1000
2154ef27340cSAdrian Chadd static void
215521008bf1SAdrian Chadd ath_txrx_stop_locked(struct ath_softc *sc)
2156ef27340cSAdrian Chadd {
2157ef27340cSAdrian Chadd 	int i = MAX_TXRX_ITERATIONS;
2158ef27340cSAdrian Chadd 
2159ef27340cSAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
216021008bf1SAdrian Chadd 	ATH_PCU_LOCK_ASSERT(sc);
216121008bf1SAdrian Chadd 
2162ef27340cSAdrian Chadd 	/*
2163ef27340cSAdrian Chadd 	 * Sleep until all the pending operations have completed.
2164ef27340cSAdrian Chadd 	 *
2165ef27340cSAdrian Chadd 	 * The caller must ensure that reset has been incremented
2166ef27340cSAdrian Chadd 	 * or the pending operations may continue being queued.
2167ef27340cSAdrian Chadd 	 */
2168ef27340cSAdrian Chadd 	while (sc->sc_rxproc_cnt || sc->sc_txproc_cnt ||
2169ef27340cSAdrian Chadd 	    sc->sc_txstart_cnt || sc->sc_intr_cnt) {
2170ef27340cSAdrian Chadd 		if (i <= 0)
2171ef27340cSAdrian Chadd 			break;
2172a2d8240dSAdrian Chadd 		msleep(sc, &sc->sc_pcu_mtx, 0, "ath_txrx_stop", 1);
2173ef27340cSAdrian Chadd 		i--;
2174ef27340cSAdrian Chadd 	}
2175ef27340cSAdrian Chadd 
2176ef27340cSAdrian Chadd 	if (i <= 0)
2177ef27340cSAdrian Chadd 		device_printf(sc->sc_dev,
2178ef27340cSAdrian Chadd 		    "%s: didn't finish after %d iterations\n",
2179ef27340cSAdrian Chadd 		    __func__, MAX_TXRX_ITERATIONS);
2180ef27340cSAdrian Chadd }
2181ef27340cSAdrian Chadd #undef	MAX_TXRX_ITERATIONS
2182ef27340cSAdrian Chadd 
2183e78719adSAdrian Chadd #if 0
2184ef27340cSAdrian Chadd static void
218521008bf1SAdrian Chadd ath_txrx_stop(struct ath_softc *sc)
218621008bf1SAdrian Chadd {
218721008bf1SAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
218821008bf1SAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
218921008bf1SAdrian Chadd 
219021008bf1SAdrian Chadd 	ATH_PCU_LOCK(sc);
219121008bf1SAdrian Chadd 	ath_txrx_stop_locked(sc);
219221008bf1SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
219321008bf1SAdrian Chadd }
2194e78719adSAdrian Chadd #endif
219521008bf1SAdrian Chadd 
219621008bf1SAdrian Chadd static void
2197ef27340cSAdrian Chadd ath_txrx_start(struct ath_softc *sc)
2198ef27340cSAdrian Chadd {
2199ef27340cSAdrian Chadd 
2200ef27340cSAdrian Chadd 	taskqueue_unblock(sc->sc_tq);
2201ef27340cSAdrian Chadd }
2202ef27340cSAdrian Chadd 
2203ee321975SAdrian Chadd /*
2204ee321975SAdrian Chadd  * Grab the reset lock, and wait around until noone else
2205ee321975SAdrian Chadd  * is trying to do anything with it.
2206ee321975SAdrian Chadd  *
2207ee321975SAdrian Chadd  * This is totally horrible but we can't hold this lock for
2208ee321975SAdrian Chadd  * long enough to do TX/RX or we end up with net80211/ip stack
2209ee321975SAdrian Chadd  * LORs and eventual deadlock.
2210ee321975SAdrian Chadd  *
2211ee321975SAdrian Chadd  * "dowait" signals whether to spin, waiting for the reset
2212ee321975SAdrian Chadd  * lock count to reach 0. This should (for now) only be used
2213ee321975SAdrian Chadd  * during the reset path, as the rest of the code may not
2214ee321975SAdrian Chadd  * be locking-reentrant enough to behave correctly.
2215ee321975SAdrian Chadd  *
2216ee321975SAdrian Chadd  * Another, cleaner way should be found to serialise all of
2217ee321975SAdrian Chadd  * these operations.
2218ee321975SAdrian Chadd  */
2219ee321975SAdrian Chadd #define	MAX_RESET_ITERATIONS	10
2220ee321975SAdrian Chadd static int
2221ee321975SAdrian Chadd ath_reset_grablock(struct ath_softc *sc, int dowait)
2222ee321975SAdrian Chadd {
2223ee321975SAdrian Chadd 	int w = 0;
2224ee321975SAdrian Chadd 	int i = MAX_RESET_ITERATIONS;
2225ee321975SAdrian Chadd 
2226ee321975SAdrian Chadd 	ATH_PCU_LOCK_ASSERT(sc);
2227ee321975SAdrian Chadd 	do {
2228ee321975SAdrian Chadd 		if (sc->sc_inreset_cnt == 0) {
2229ee321975SAdrian Chadd 			w = 1;
2230ee321975SAdrian Chadd 			break;
2231ee321975SAdrian Chadd 		}
2232ee321975SAdrian Chadd 		if (dowait == 0) {
2233ee321975SAdrian Chadd 			w = 0;
2234ee321975SAdrian Chadd 			break;
2235ee321975SAdrian Chadd 		}
2236ee321975SAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2237ee321975SAdrian Chadd 		pause("ath_reset_grablock", 1);
2238ee321975SAdrian Chadd 		i--;
2239ee321975SAdrian Chadd 		ATH_PCU_LOCK(sc);
2240ee321975SAdrian Chadd 	} while (i > 0);
2241ee321975SAdrian Chadd 
2242ee321975SAdrian Chadd 	/*
2243ee321975SAdrian Chadd 	 * We always increment the refcounter, regardless
2244ee321975SAdrian Chadd 	 * of whether we succeeded to get it in an exclusive
2245ee321975SAdrian Chadd 	 * way.
2246ee321975SAdrian Chadd 	 */
2247ee321975SAdrian Chadd 	sc->sc_inreset_cnt++;
2248ee321975SAdrian Chadd 
2249ee321975SAdrian Chadd 	if (i <= 0)
2250ee321975SAdrian Chadd 		device_printf(sc->sc_dev,
2251ee321975SAdrian Chadd 		    "%s: didn't finish after %d iterations\n",
2252ee321975SAdrian Chadd 		    __func__, MAX_RESET_ITERATIONS);
2253ee321975SAdrian Chadd 
2254ee321975SAdrian Chadd 	if (w == 0)
2255ee321975SAdrian Chadd 		device_printf(sc->sc_dev,
2256ee321975SAdrian Chadd 		    "%s: warning, recursive reset path!\n",
2257ee321975SAdrian Chadd 		    __func__);
2258ee321975SAdrian Chadd 
2259ee321975SAdrian Chadd 	return w;
2260ee321975SAdrian Chadd }
2261ee321975SAdrian Chadd #undef MAX_RESET_ITERATIONS
2262ee321975SAdrian Chadd 
2263ee321975SAdrian Chadd /*
2264ee321975SAdrian Chadd  * XXX TODO: write ath_reset_releaselock
2265ee321975SAdrian Chadd  */
2266ee321975SAdrian Chadd 
2267c42a7b7eSSam Leffler static void
2268c42a7b7eSSam Leffler ath_stop(struct ifnet *ifp)
2269c42a7b7eSSam Leffler {
2270c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2271c42a7b7eSSam Leffler 
2272c42a7b7eSSam Leffler 	ATH_LOCK(sc);
2273c42a7b7eSSam Leffler 	ath_stop_locked(ifp);
2274f0b2a0beSSam Leffler 	ATH_UNLOCK(sc);
22755591b213SSam Leffler }
22765591b213SSam Leffler 
22775591b213SSam Leffler /*
22785591b213SSam Leffler  * Reset the hardware w/o losing operational state.  This is
22795591b213SSam Leffler  * basically a more efficient way of doing ath_stop, ath_init,
22805591b213SSam Leffler  * followed by state transitions to the current 802.11
2281c42a7b7eSSam Leffler  * operational state.  Used to recover from various errors and
2282c42a7b7eSSam Leffler  * to reset or reload hardware state.
22835591b213SSam Leffler  */
22846079fdbeSAdrian Chadd int
2285517526efSAdrian Chadd ath_reset(struct ifnet *ifp, ATH_RESET_TYPE reset_type)
22865591b213SSam Leffler {
2287c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2288b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
22895591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
22905591b213SSam Leffler 	HAL_STATUS status;
2291ef27340cSAdrian Chadd 	int i;
22925591b213SSam Leffler 
2293f52d3452SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: called\n", __func__);
229416d4de92SAdrian Chadd 
2295ee321975SAdrian Chadd 	/* Ensure ATH_LOCK isn't held; ath_rx_proc can't be locked */
2296ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
2297ef27340cSAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
2298ef27340cSAdrian Chadd 
2299d52f7132SAdrian Chadd 	/* Try to (stop any further TX/RX from occuring */
2300d52f7132SAdrian Chadd 	taskqueue_block(sc->sc_tq);
2301d52f7132SAdrian Chadd 
2302ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2303e78719adSAdrian Chadd 	ath_hal_intrset(ah, 0);		/* disable interrupts */
2304e78719adSAdrian Chadd 	ath_txrx_stop_locked(sc);	/* Ensure TX/RX is stopped */
2305ee321975SAdrian Chadd 	if (ath_reset_grablock(sc, 1) == 0) {
2306ee321975SAdrian Chadd 		device_printf(sc->sc_dev, "%s: concurrent reset! Danger!\n",
2307ef27340cSAdrian Chadd 		    __func__);
2308ef27340cSAdrian Chadd 	}
2309ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2310ef27340cSAdrian Chadd 
2311f52d3452SAdrian Chadd 	/*
23129a842e8bSAdrian Chadd 	 * Should now wait for pending TX/RX to complete
23139a842e8bSAdrian Chadd 	 * and block future ones from occuring. This needs to be
23149a842e8bSAdrian Chadd 	 * done before the TX queue is drained.
2315f52d3452SAdrian Chadd 	 */
2316ef27340cSAdrian Chadd 	ath_draintxq(sc, reset_type);	/* stop xmit side */
2317ef27340cSAdrian Chadd 
2318ef27340cSAdrian Chadd 	/*
2319ef27340cSAdrian Chadd 	 * Regardless of whether we're doing a no-loss flush or
2320ef27340cSAdrian Chadd 	 * not, stop the PCU and handle what's in the RX queue.
2321ef27340cSAdrian Chadd 	 * That way frames aren't dropped which shouldn't be.
2322ef27340cSAdrian Chadd 	 */
23239a842e8bSAdrian Chadd 	ath_stoprecv(sc, (reset_type != ATH_RESET_NOLOSS));
2324f8cc9b09SAdrian Chadd 	ath_rx_flush(sc);
2325ef27340cSAdrian Chadd 
2326b032f27cSSam Leffler 	ath_settkipmic(sc);		/* configure TKIP MIC handling */
23275591b213SSam Leffler 	/* NB: indicate channel change so we do a full reset */
23286322256bSAdrian Chadd 	ath_update_chainmasks(sc, ic->ic_curchan);
23296322256bSAdrian Chadd 	ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
23306322256bSAdrian Chadd 	    sc->sc_cur_rxchainmask);
233159efa8b5SSam Leffler 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_TRUE, &status))
23325591b213SSam Leffler 		if_printf(ifp, "%s: unable to reset hardware; hal status %u\n",
23335591b213SSam Leffler 			__func__, status);
2334c59005e9SSam Leffler 	sc->sc_diversity = ath_hal_getdiversity(ah);
233548237774SAdrian Chadd 
233648237774SAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
233748237774SAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
233848237774SAdrian Chadd 
23399af351f9SAdrian Chadd 	/* Let spectral at in case spectral is enabled */
23409af351f9SAdrian Chadd 	ath_spectral_enable(sc, ic->ic_curchan);
23419af351f9SAdrian Chadd 
234268e8e04eSSam Leffler 	if (ath_startrecv(sc) != 0)	/* restart recv */
234368e8e04eSSam Leffler 		if_printf(ifp, "%s: unable to start recv logic\n", __func__);
2344c42a7b7eSSam Leffler 	/*
2345c42a7b7eSSam Leffler 	 * We may be doing a reset in response to an ioctl
2346c42a7b7eSSam Leffler 	 * that changes the channel so update any state that
2347c42a7b7eSSam Leffler 	 * might change as a result.
2348c42a7b7eSSam Leffler 	 */
2349724c193aSSam Leffler 	ath_chan_change(sc, ic->ic_curchan);
2350c89b957aSSam Leffler 	if (sc->sc_beacons) {		/* restart beacons */
2351584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
235210ad9a77SSam Leffler 		if (sc->sc_tdma)
235310ad9a77SSam Leffler 			ath_tdma_config(sc, NULL);
235410ad9a77SSam Leffler 		else
235510ad9a77SSam Leffler #endif
2356c89b957aSSam Leffler 			ath_beacon_config(sc, NULL);
235710ad9a77SSam Leffler 	}
2358c42a7b7eSSam Leffler 
2359ef27340cSAdrian Chadd 	/*
2360ef27340cSAdrian Chadd 	 * Release the reset lock and re-enable interrupts here.
2361ef27340cSAdrian Chadd 	 * If an interrupt was being processed in ath_intr(),
2362ef27340cSAdrian Chadd 	 * it would disable interrupts at this point. So we have
2363ef27340cSAdrian Chadd 	 * to atomically enable interrupts and decrement the
2364ef27340cSAdrian Chadd 	 * reset counter - this way ath_intr() doesn't end up
2365ef27340cSAdrian Chadd 	 * disabling interrupts without a corresponding enable
2366ef27340cSAdrian Chadd 	 * in the rest or channel change path.
2367ef27340cSAdrian Chadd 	 */
2368ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2369ef27340cSAdrian Chadd 	sc->sc_inreset_cnt--;
2370ef27340cSAdrian Chadd 	/* XXX only do this if sc_inreset_cnt == 0? */
2371ef27340cSAdrian Chadd 	ath_hal_intrset(ah, sc->sc_imask);
2372ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2373ef27340cSAdrian Chadd 
2374ef27340cSAdrian Chadd 	/*
2375ef27340cSAdrian Chadd 	 * TX and RX can be started here. If it were started with
2376ef27340cSAdrian Chadd 	 * sc_inreset_cnt > 0, the TX and RX path would abort.
2377ef27340cSAdrian Chadd 	 * Thus if this is a nested call through the reset or
2378ef27340cSAdrian Chadd 	 * channel change code, TX completion will occur but
2379ef27340cSAdrian Chadd 	 * RX completion and ath_start / ath_tx_start will not
2380ef27340cSAdrian Chadd 	 * run.
2381ef27340cSAdrian Chadd 	 */
2382ef27340cSAdrian Chadd 
2383ef27340cSAdrian Chadd 	/* Restart TX/RX as needed */
2384ef27340cSAdrian Chadd 	ath_txrx_start(sc);
2385ef27340cSAdrian Chadd 
2386375307d4SAdrian Chadd 	/* Restart TX completion and pending TX */
2387ef27340cSAdrian Chadd 	if (reset_type == ATH_RESET_NOLOSS) {
2388ef27340cSAdrian Chadd 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
2389ef27340cSAdrian Chadd 			if (ATH_TXQ_SETUP(sc, i)) {
2390b837332dSAdrian Chadd 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
2391ef27340cSAdrian Chadd 				ath_txq_restart_dma(sc, &sc->sc_txq[i]);
2392b837332dSAdrian Chadd 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
2393b837332dSAdrian Chadd 
2394b837332dSAdrian Chadd 				ATH_TX_LOCK(sc);
2395ef27340cSAdrian Chadd 				ath_txq_sched(sc, &sc->sc_txq[i]);
2396375307d4SAdrian Chadd 				ATH_TX_UNLOCK(sc);
2397ef27340cSAdrian Chadd 			}
2398b837332dSAdrian Chadd 		}
2399b837332dSAdrian Chadd 	}
2400ef27340cSAdrian Chadd 
2401ef27340cSAdrian Chadd 	/*
2402ef27340cSAdrian Chadd 	 * This may have been set during an ath_start() call which
2403ef27340cSAdrian Chadd 	 * set this once it detected a concurrent TX was going on.
2404ef27340cSAdrian Chadd 	 * So, clear it.
2405ef27340cSAdrian Chadd 	 */
2406e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
2407ef27340cSAdrian Chadd 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2408e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
2409ef27340cSAdrian Chadd 
2410ef27340cSAdrian Chadd 	/* Handle any frames in the TX queue */
2411ef27340cSAdrian Chadd 	/*
2412ef27340cSAdrian Chadd 	 * XXX should this be done by the caller, rather than
2413ef27340cSAdrian Chadd 	 * ath_reset() ?
2414ef27340cSAdrian Chadd 	 */
24158e739394SAdrian Chadd 	ath_tx_kick(sc);		/* restart xmit */
2416c42a7b7eSSam Leffler 	return 0;
24175591b213SSam Leffler }
24185591b213SSam Leffler 
241968e8e04eSSam Leffler static int
2420b032f27cSSam Leffler ath_reset_vap(struct ieee80211vap *vap, u_long cmd)
2421b032f27cSSam Leffler {
24224b54a231SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
24234b54a231SSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
24244b54a231SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
24254b54a231SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
24264b54a231SSam Leffler 
24274b54a231SSam Leffler 	switch (cmd) {
24284b54a231SSam Leffler 	case IEEE80211_IOC_TXPOWER:
24294b54a231SSam Leffler 		/*
24304b54a231SSam Leffler 		 * If per-packet TPC is enabled, then we have nothing
24314b54a231SSam Leffler 		 * to do; otherwise we need to force the global limit.
24324b54a231SSam Leffler 		 * All this can happen directly; no need to reset.
24334b54a231SSam Leffler 		 */
24344b54a231SSam Leffler 		if (!ath_hal_gettpc(ah))
24354b54a231SSam Leffler 			ath_hal_settxpowlimit(ah, ic->ic_txpowlimit);
24364b54a231SSam Leffler 		return 0;
24374b54a231SSam Leffler 	}
2438517526efSAdrian Chadd 	/* XXX? Full or NOLOSS? */
2439517526efSAdrian Chadd 	return ath_reset(ifp, ATH_RESET_FULL);
2440b032f27cSSam Leffler }
2441b032f27cSSam Leffler 
2442b8e788a5SAdrian Chadd struct ath_buf *
2443af33d486SAdrian Chadd _ath_getbuf_locked(struct ath_softc *sc, ath_buf_type_t btype)
244410ad9a77SSam Leffler {
244510ad9a77SSam Leffler 	struct ath_buf *bf;
244610ad9a77SSam Leffler 
244710ad9a77SSam Leffler 	ATH_TXBUF_LOCK_ASSERT(sc);
244810ad9a77SSam Leffler 
2449af33d486SAdrian Chadd 	if (btype == ATH_BUFTYPE_MGMT)
2450af33d486SAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_txbuf_mgmt);
2451af33d486SAdrian Chadd 	else
24526b349e5aSAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_txbuf);
2453af33d486SAdrian Chadd 
2454e346b073SAdrian Chadd 	if (bf == NULL) {
2455e346b073SAdrian Chadd 		sc->sc_stats.ast_tx_getnobuf++;
2456e346b073SAdrian Chadd 	} else {
2457e346b073SAdrian Chadd 		if (bf->bf_flags & ATH_BUF_BUSY) {
2458e346b073SAdrian Chadd 			sc->sc_stats.ast_tx_getbusybuf++;
2459e346b073SAdrian Chadd 			bf = NULL;
2460e346b073SAdrian Chadd 		}
2461e346b073SAdrian Chadd 	}
2462e346b073SAdrian Chadd 
2463af33d486SAdrian Chadd 	if (bf != NULL && (bf->bf_flags & ATH_BUF_BUSY) == 0) {
2464af33d486SAdrian Chadd 		if (btype == ATH_BUFTYPE_MGMT)
2465af33d486SAdrian Chadd 			TAILQ_REMOVE(&sc->sc_txbuf_mgmt, bf, bf_list);
246623ced6c1SAdrian Chadd 		else {
2467af33d486SAdrian Chadd 			TAILQ_REMOVE(&sc->sc_txbuf, bf, bf_list);
246823ced6c1SAdrian Chadd 			sc->sc_txbuf_cnt--;
246923ced6c1SAdrian Chadd 
247023ced6c1SAdrian Chadd 			/*
247123ced6c1SAdrian Chadd 			 * This shuldn't happen; however just to be
247223ced6c1SAdrian Chadd 			 * safe print a warning and fudge the txbuf
247323ced6c1SAdrian Chadd 			 * count.
247423ced6c1SAdrian Chadd 			 */
247523ced6c1SAdrian Chadd 			if (sc->sc_txbuf_cnt < 0) {
247623ced6c1SAdrian Chadd 				device_printf(sc->sc_dev,
247723ced6c1SAdrian Chadd 				    "%s: sc_txbuf_cnt < 0?\n",
247823ced6c1SAdrian Chadd 				    __func__);
247923ced6c1SAdrian Chadd 				sc->sc_txbuf_cnt = 0;
248023ced6c1SAdrian Chadd 			}
248123ced6c1SAdrian Chadd 		}
2482af33d486SAdrian Chadd 	} else
248310ad9a77SSam Leffler 		bf = NULL;
2484e346b073SAdrian Chadd 
248510ad9a77SSam Leffler 	if (bf == NULL) {
2486af33d486SAdrian Chadd 		/* XXX should check which list, mgmt or otherwise */
248710ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: %s\n", __func__,
24886b349e5aSAdrian Chadd 		    TAILQ_FIRST(&sc->sc_txbuf) == NULL ?
248910ad9a77SSam Leffler 			"out of xmit buffers" : "xmit buffer busy");
2490e346b073SAdrian Chadd 		return NULL;
249110ad9a77SSam Leffler 	}
2492e346b073SAdrian Chadd 
2493af33d486SAdrian Chadd 	/* XXX TODO: should do this at buffer list initialisation */
2494af33d486SAdrian Chadd 	/* XXX (then, ensure the buffer has the right flag set) */
24953feffbd7SAdrian Chadd 	bf->bf_flags = 0;
2496af33d486SAdrian Chadd 	if (btype == ATH_BUFTYPE_MGMT)
2497af33d486SAdrian Chadd 		bf->bf_flags |= ATH_BUF_MGMT;
2498af33d486SAdrian Chadd 	else
2499af33d486SAdrian Chadd 		bf->bf_flags &= (~ATH_BUF_MGMT);
2500af33d486SAdrian Chadd 
2501e346b073SAdrian Chadd 	/* Valid bf here; clear some basic fields */
2502e346b073SAdrian Chadd 	bf->bf_next = NULL;	/* XXX just to be sure */
2503e346b073SAdrian Chadd 	bf->bf_last = NULL;	/* XXX again, just to be sure */
2504e346b073SAdrian Chadd 	bf->bf_comp = NULL;	/* XXX again, just to be sure */
2505e346b073SAdrian Chadd 	bzero(&bf->bf_state, sizeof(bf->bf_state));
2506e346b073SAdrian Chadd 
250785bf9bc3SAdrian Chadd 	/*
250885bf9bc3SAdrian Chadd 	 * Track the descriptor ID only if doing EDMA
250985bf9bc3SAdrian Chadd 	 */
251085bf9bc3SAdrian Chadd 	if (sc->sc_isedma) {
251185bf9bc3SAdrian Chadd 		bf->bf_descid = sc->sc_txbuf_descid;
251285bf9bc3SAdrian Chadd 		sc->sc_txbuf_descid++;
251385bf9bc3SAdrian Chadd 	}
251485bf9bc3SAdrian Chadd 
251510ad9a77SSam Leffler 	return bf;
251610ad9a77SSam Leffler }
251710ad9a77SSam Leffler 
2518e346b073SAdrian Chadd /*
2519e346b073SAdrian Chadd  * When retrying a software frame, buffers marked ATH_BUF_BUSY
2520e346b073SAdrian Chadd  * can't be thrown back on the queue as they could still be
2521e346b073SAdrian Chadd  * in use by the hardware.
2522e346b073SAdrian Chadd  *
2523e346b073SAdrian Chadd  * This duplicates the buffer, or returns NULL.
2524e346b073SAdrian Chadd  *
2525e346b073SAdrian Chadd  * The descriptor is also copied but the link pointers and
2526e346b073SAdrian Chadd  * the DMA segments aren't copied; this frame should thus
2527e346b073SAdrian Chadd  * be again passed through the descriptor setup/chain routines
2528e346b073SAdrian Chadd  * so the link is correct.
2529e346b073SAdrian Chadd  *
2530e346b073SAdrian Chadd  * The caller must free the buffer using ath_freebuf().
2531e346b073SAdrian Chadd  */
2532e346b073SAdrian Chadd struct ath_buf *
25333f3a5dbdSAdrian Chadd ath_buf_clone(struct ath_softc *sc, struct ath_buf *bf)
2534e346b073SAdrian Chadd {
2535e346b073SAdrian Chadd 	struct ath_buf *tbf;
2536e346b073SAdrian Chadd 
2537af33d486SAdrian Chadd 	tbf = ath_getbuf(sc,
2538af33d486SAdrian Chadd 	    (bf->bf_flags & ATH_BUF_MGMT) ?
2539af33d486SAdrian Chadd 	     ATH_BUFTYPE_MGMT : ATH_BUFTYPE_NORMAL);
2540e346b073SAdrian Chadd 	if (tbf == NULL)
2541e346b073SAdrian Chadd 		return NULL;	/* XXX failure? Why? */
2542e346b073SAdrian Chadd 
2543e346b073SAdrian Chadd 	/* Copy basics */
2544e346b073SAdrian Chadd 	tbf->bf_next = NULL;
2545e346b073SAdrian Chadd 	tbf->bf_nseg = bf->bf_nseg;
25463feffbd7SAdrian Chadd 	tbf->bf_flags = bf->bf_flags & ATH_BUF_FLAGS_CLONE;
2547e346b073SAdrian Chadd 	tbf->bf_status = bf->bf_status;
2548e346b073SAdrian Chadd 	tbf->bf_m = bf->bf_m;
2549e346b073SAdrian Chadd 	tbf->bf_node = bf->bf_node;
2550e346b073SAdrian Chadd 	/* will be setup by the chain/setup function */
2551e346b073SAdrian Chadd 	tbf->bf_lastds = NULL;
2552e346b073SAdrian Chadd 	/* for now, last == self */
2553e346b073SAdrian Chadd 	tbf->bf_last = tbf;
2554e346b073SAdrian Chadd 	tbf->bf_comp = bf->bf_comp;
2555e346b073SAdrian Chadd 
2556e346b073SAdrian Chadd 	/* NOTE: DMA segments will be setup by the setup/chain functions */
2557e346b073SAdrian Chadd 
2558e346b073SAdrian Chadd 	/* The caller has to re-init the descriptor + links */
2559e346b073SAdrian Chadd 
25603f3a5dbdSAdrian Chadd 	/*
25613f3a5dbdSAdrian Chadd 	 * Free the DMA mapping here, before we NULL the mbuf.
25623f3a5dbdSAdrian Chadd 	 * We must only call bus_dmamap_unload() once per mbuf chain
25633f3a5dbdSAdrian Chadd 	 * or behaviour is undefined.
25643f3a5dbdSAdrian Chadd 	 */
25653f3a5dbdSAdrian Chadd 	if (bf->bf_m != NULL) {
25663f3a5dbdSAdrian Chadd 		/*
25673f3a5dbdSAdrian Chadd 		 * XXX is this POSTWRITE call required?
25683f3a5dbdSAdrian Chadd 		 */
25693f3a5dbdSAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
25703f3a5dbdSAdrian Chadd 		    BUS_DMASYNC_POSTWRITE);
25713f3a5dbdSAdrian Chadd 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
25723f3a5dbdSAdrian Chadd 	}
25733f3a5dbdSAdrian Chadd 
25743f3a5dbdSAdrian Chadd 	bf->bf_m = NULL;
25753f3a5dbdSAdrian Chadd 	bf->bf_node = NULL;
25763f3a5dbdSAdrian Chadd 
2577e346b073SAdrian Chadd 	/* Copy state */
2578e346b073SAdrian Chadd 	memcpy(&tbf->bf_state, &bf->bf_state, sizeof(bf->bf_state));
2579e346b073SAdrian Chadd 
2580e346b073SAdrian Chadd 	return tbf;
2581e346b073SAdrian Chadd }
2582e346b073SAdrian Chadd 
2583b8e788a5SAdrian Chadd struct ath_buf *
2584af33d486SAdrian Chadd ath_getbuf(struct ath_softc *sc, ath_buf_type_t btype)
258510ad9a77SSam Leffler {
258610ad9a77SSam Leffler 	struct ath_buf *bf;
258710ad9a77SSam Leffler 
258810ad9a77SSam Leffler 	ATH_TXBUF_LOCK(sc);
2589af33d486SAdrian Chadd 	bf = _ath_getbuf_locked(sc, btype);
2590af33d486SAdrian Chadd 	/*
2591af33d486SAdrian Chadd 	 * If a mgmt buffer was requested but we're out of those,
2592af33d486SAdrian Chadd 	 * try requesting a normal one.
2593af33d486SAdrian Chadd 	 */
2594af33d486SAdrian Chadd 	if (bf == NULL && btype == ATH_BUFTYPE_MGMT)
2595af33d486SAdrian Chadd 		bf = _ath_getbuf_locked(sc, ATH_BUFTYPE_NORMAL);
2596e4e7938aSAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
259710ad9a77SSam Leffler 	if (bf == NULL) {
259810ad9a77SSam Leffler 		struct ifnet *ifp = sc->sc_ifp;
259910ad9a77SSam Leffler 
260010ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: stop queue\n", __func__);
260110ad9a77SSam Leffler 		sc->sc_stats.ast_tx_qstop++;
2602e4e7938aSAdrian Chadd 		IF_LOCK(&ifp->if_snd);
260310ad9a77SSam Leffler 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2604e4e7938aSAdrian Chadd 		IF_UNLOCK(&ifp->if_snd);
260510ad9a77SSam Leffler 	}
260610ad9a77SSam Leffler 	return bf;
260710ad9a77SSam Leffler }
260810ad9a77SSam Leffler 
26098e739394SAdrian Chadd static void
26101a85141aSAdrian Chadd ath_start_queue(struct ifnet *ifp)
26115591b213SSam Leffler {
26121a85141aSAdrian Chadd 	struct ath_softc *sc = ifp->if_softc;
26135591b213SSam Leffler 
26141a85141aSAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 0, "ath_start_queue: start");
26158e739394SAdrian Chadd 	ath_tx_kick(sc);
26161a85141aSAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 0, "ath_start_queue: finished");
26178e739394SAdrian Chadd }
26188e739394SAdrian Chadd 
26198e739394SAdrian Chadd void
26208e739394SAdrian Chadd ath_start_task(void *arg, int npending)
26218e739394SAdrian Chadd {
26228e739394SAdrian Chadd 	struct ath_softc *sc = (struct ath_softc *) arg;
26238e739394SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
2624ef27340cSAdrian Chadd 
26251b5c5f5aSAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 0, "ath_start_task: start");
26261b5c5f5aSAdrian Chadd 
2627ef27340cSAdrian Chadd 	/* XXX is it ok to hold the ATH_LOCK here? */
2628ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2629ef27340cSAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
2630ef27340cSAdrian Chadd 		device_printf(sc->sc_dev,
2631ef27340cSAdrian Chadd 		    "%s: sc_inreset_cnt > 0; bailing\n", __func__);
2632ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2633e4e7938aSAdrian Chadd 		IF_LOCK(&ifp->if_snd);
263423ced6c1SAdrian Chadd 		sc->sc_stats.ast_tx_qstop++;
2635e4e7938aSAdrian Chadd 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2636e4e7938aSAdrian Chadd 		IF_UNLOCK(&ifp->if_snd);
26371b5c5f5aSAdrian Chadd 		ATH_KTR(sc, ATH_KTR_TX, 0, "ath_start_task: OACTIVE, finish");
2638ef27340cSAdrian Chadd 		return;
2639ef27340cSAdrian Chadd 	}
2640ef27340cSAdrian Chadd 	sc->sc_txstart_cnt++;
2641ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2642ef27340cSAdrian Chadd 
26431a85141aSAdrian Chadd 	ATH_TX_LOCK(sc);
26441a85141aSAdrian Chadd 	ath_start(sc->sc_ifp);
26451a85141aSAdrian Chadd 	ATH_TX_UNLOCK(sc);
26468e739394SAdrian Chadd 
26478e739394SAdrian Chadd 	ATH_PCU_LOCK(sc);
26488e739394SAdrian Chadd 	sc->sc_txstart_cnt--;
26498e739394SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
26501b5c5f5aSAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 0, "ath_start_task: finished");
26518e739394SAdrian Chadd }
26528e739394SAdrian Chadd 
26531a85141aSAdrian Chadd void
26541a85141aSAdrian Chadd ath_start(struct ifnet *ifp)
26558e739394SAdrian Chadd {
26568e739394SAdrian Chadd 	struct ath_softc *sc = ifp->if_softc;
2657c5239edbSAdrian Chadd 	struct ieee80211_node *ni;
26581a85141aSAdrian Chadd 	struct ath_buf *bf;
26591a85141aSAdrian Chadd 	struct mbuf *m, *next;
26601a85141aSAdrian Chadd 	ath_bufhead frags;
26611a85141aSAdrian Chadd 	int npkts = 0;
26628e739394SAdrian Chadd 
26631a85141aSAdrian Chadd 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->sc_invalid)
26641a85141aSAdrian Chadd 		return;
26658e739394SAdrian Chadd 
26661a85141aSAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
2667375307d4SAdrian Chadd 
26681a85141aSAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 0, "ath_start: called");
26691a85141aSAdrian Chadd 
26701a85141aSAdrian Chadd 	for (;;) {
26717dcb2beaSAdrian Chadd 		/*
26727dcb2beaSAdrian Chadd 		 * Grab the frame that we're going to try and transmit.
26737dcb2beaSAdrian Chadd 		 */
26747dcb2beaSAdrian Chadd 		IFQ_DEQUEUE(&ifp->if_snd, m);
26757dcb2beaSAdrian Chadd 		if (m == NULL)
26767dcb2beaSAdrian Chadd 			break;
26777dcb2beaSAdrian Chadd 		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
26787dcb2beaSAdrian Chadd 
26797dcb2beaSAdrian Chadd 		/*
26807dcb2beaSAdrian Chadd 		 * Enforce how deep a node queue can get.
26817dcb2beaSAdrian Chadd 		 *
26827dcb2beaSAdrian Chadd 		 * XXX it would be nicer if we kept an mbuf queue per
26837dcb2beaSAdrian Chadd 		 * node and only whacked them into ath_bufs when we
26847dcb2beaSAdrian Chadd 		 * are ready to schedule some traffic from them.
26857dcb2beaSAdrian Chadd 		 * .. that may come later.
26867dcb2beaSAdrian Chadd 		 *
26877dcb2beaSAdrian Chadd 		 * XXX we should also track the per-node hardware queue
26887dcb2beaSAdrian Chadd 		 * depth so it is easy to limit the _SUM_ of the swq and
26897dcb2beaSAdrian Chadd 		 * hwq frames.  Since we only schedule two HWQ frames
26907dcb2beaSAdrian Chadd 		 * at a time, this should be OK for now.
26917dcb2beaSAdrian Chadd 		 */
26927dcb2beaSAdrian Chadd 		if ((!(m->m_flags & M_EAPOL)) &&
26937dcb2beaSAdrian Chadd 		    (ATH_NODE(ni)->an_swq_depth > sc->sc_txq_node_maxdepth)) {
26947dcb2beaSAdrian Chadd 			sc->sc_stats.ast_tx_nodeq_overflow++;
26957dcb2beaSAdrian Chadd 			if (ni != NULL)
26967dcb2beaSAdrian Chadd 				ieee80211_free_node(ni);
26977dcb2beaSAdrian Chadd 			m_freem(m);
26987dcb2beaSAdrian Chadd 			m = NULL;
26997dcb2beaSAdrian Chadd 			continue;
27007dcb2beaSAdrian Chadd 		}
27017dcb2beaSAdrian Chadd 
27027dcb2beaSAdrian Chadd 		/*
27037dcb2beaSAdrian Chadd 		 * Check how many TX buffers are available.
27047dcb2beaSAdrian Chadd 		 *
27057dcb2beaSAdrian Chadd 		 * If this is for non-EAPOL traffic, just leave some
27067dcb2beaSAdrian Chadd 		 * space free in order for buffer cloning and raw
27077dcb2beaSAdrian Chadd 		 * frame transmission to occur.
27087dcb2beaSAdrian Chadd 		 *
27097dcb2beaSAdrian Chadd 		 * If it's for EAPOL traffic, ignore this for now.
27107dcb2beaSAdrian Chadd 		 * Management traffic will be sent via the raw transmit
27117dcb2beaSAdrian Chadd 		 * method which bypasses this check.
27127dcb2beaSAdrian Chadd 		 *
27137dcb2beaSAdrian Chadd 		 * This is needed to ensure that EAPOL frames during
27147dcb2beaSAdrian Chadd 		 * (re) keying have a chance to go out.
27157dcb2beaSAdrian Chadd 		 *
27167dcb2beaSAdrian Chadd 		 * See kern/138379 for more information.
27177dcb2beaSAdrian Chadd 		 */
27187dcb2beaSAdrian Chadd 		if ((!(m->m_flags & M_EAPOL)) &&
27197dcb2beaSAdrian Chadd 		    (sc->sc_txbuf_cnt <= sc->sc_txq_data_minfree)) {
27207dcb2beaSAdrian Chadd 			sc->sc_stats.ast_tx_nobuf++;
272123ced6c1SAdrian Chadd 			IF_LOCK(&ifp->if_snd);
27227dcb2beaSAdrian Chadd 			_IF_PREPEND(&ifp->if_snd, m);
272323ced6c1SAdrian Chadd 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
272423ced6c1SAdrian Chadd 			IF_UNLOCK(&ifp->if_snd);
27257dcb2beaSAdrian Chadd 			m = NULL;
27261a85141aSAdrian Chadd 			break;
272723ced6c1SAdrian Chadd 		}
272823ced6c1SAdrian Chadd 
27295591b213SSam Leffler 		/*
27305591b213SSam Leffler 		 * Grab a TX buffer and associated resources.
27317dcb2beaSAdrian Chadd 		 *
27327dcb2beaSAdrian Chadd 		 * If it's an EAPOL frame, allocate a MGMT ath_buf.
27337dcb2beaSAdrian Chadd 		 * That way even with temporary buffer exhaustion due to
27347dcb2beaSAdrian Chadd 		 * the data path doesn't leave us without the ability
27357dcb2beaSAdrian Chadd 		 * to transmit management frames.
27367dcb2beaSAdrian Chadd 		 *
27377dcb2beaSAdrian Chadd 		 * Otherwise allocate a normal buffer.
27385591b213SSam Leffler 		 */
27397dcb2beaSAdrian Chadd 		if (m->m_flags & M_EAPOL)
27407dcb2beaSAdrian Chadd 			bf = ath_getbuf(sc, ATH_BUFTYPE_MGMT);
27417dcb2beaSAdrian Chadd 		else
2742af33d486SAdrian Chadd 			bf = ath_getbuf(sc, ATH_BUFTYPE_NORMAL);
27431a85141aSAdrian Chadd 
27447dcb2beaSAdrian Chadd 		if (bf == NULL) {
27457dcb2beaSAdrian Chadd 			/*
27467dcb2beaSAdrian Chadd 			 * If we failed to allocate a buffer, prepend it
27477dcb2beaSAdrian Chadd 			 * and continue.
27487dcb2beaSAdrian Chadd 			 *
27497dcb2beaSAdrian Chadd 			 * We shouldn't fail normally, due to the check
27507dcb2beaSAdrian Chadd 			 * above.
27517dcb2beaSAdrian Chadd 			 */
27527dcb2beaSAdrian Chadd 			sc->sc_stats.ast_tx_nobuf++;
27537dcb2beaSAdrian Chadd 			IF_LOCK(&ifp->if_snd);
27547dcb2beaSAdrian Chadd 			_IF_PREPEND(&ifp->if_snd, m);
27557dcb2beaSAdrian Chadd 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
27567dcb2beaSAdrian Chadd 			IF_UNLOCK(&ifp->if_snd);
27577dcb2beaSAdrian Chadd 			m = NULL;
27581a85141aSAdrian Chadd 			break;
2759b032f27cSSam Leffler 		}
27607dcb2beaSAdrian Chadd 
27611a85141aSAdrian Chadd 		npkts ++;
27627dcb2beaSAdrian Chadd 
276368e8e04eSSam Leffler 		/*
276468e8e04eSSam Leffler 		 * Check for fragmentation.  If this frame
276568e8e04eSSam Leffler 		 * has been broken up verify we have enough
276668e8e04eSSam Leffler 		 * buffers to send all the fragments so all
276768e8e04eSSam Leffler 		 * go out or none...
276868e8e04eSSam Leffler 		 */
27696b349e5aSAdrian Chadd 		TAILQ_INIT(&frags);
27701a85141aSAdrian Chadd 		if ((m->m_flags & M_FRAG) &&
27711a85141aSAdrian Chadd 		    !ath_txfrag_setup(sc, &frags, m, ni)) {
277268e8e04eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_XMIT,
277368e8e04eSSam Leffler 			    "%s: out of txfrag buffers\n", __func__);
277436c6be9aSSam Leffler 			sc->sc_stats.ast_tx_nofrag++;
27759cb93076SSam Leffler 			ifp->if_oerrors++;
27761a85141aSAdrian Chadd 			ath_freetx(m);
277768e8e04eSSam Leffler 			goto bad;
277868e8e04eSSam Leffler 		}
27791a85141aSAdrian Chadd 		ifp->if_opackets++;
27801a85141aSAdrian Chadd 	nextfrag:
278168e8e04eSSam Leffler 		/*
27821a85141aSAdrian Chadd 		 * Pass the frame to the h/w for transmission.
27831a85141aSAdrian Chadd 		 * Fragmented frames have each frag chained together
27841a85141aSAdrian Chadd 		 * with m_nextpkt.  We know there are sufficient ath_buf's
27851a85141aSAdrian Chadd 		 * to send all the frags because of work done by
27861a85141aSAdrian Chadd 		 * ath_txfrag_setup.  We leave m_nextpkt set while
27871a85141aSAdrian Chadd 		 * calling ath_tx_start so it can use it to extend the
27881a85141aSAdrian Chadd 		 * the tx duration to cover the subsequent frag and
27891a85141aSAdrian Chadd 		 * so it can reclaim all the mbufs in case of an error;
27901a85141aSAdrian Chadd 		 * ath_tx_start clears m_nextpkt once it commits to
27911a85141aSAdrian Chadd 		 * handing the frame to the hardware.
279268e8e04eSSam Leffler 		 */
27931a85141aSAdrian Chadd 		next = m->m_nextpkt;
27941a85141aSAdrian Chadd 		if (ath_tx_start(sc, ni, bf, m)) {
27955591b213SSam Leffler 	bad:
27961a85141aSAdrian Chadd 			ifp->if_oerrors++;
27971a85141aSAdrian Chadd 	reclaim:
279868e8e04eSSam Leffler 			bf->bf_m = NULL;
279968e8e04eSSam Leffler 			bf->bf_node = NULL;
2800c42a7b7eSSam Leffler 			ATH_TXBUF_LOCK(sc);
2801e1a50456SAdrian Chadd 			ath_returnbuf_head(sc, bf);
280268e8e04eSSam Leffler 			ath_txfrag_cleanup(sc, &frags, ni);
2803c42a7b7eSSam Leffler 			ATH_TXBUF_UNLOCK(sc);
2804375307d4SAdrian Chadd 			/*
2805375307d4SAdrian Chadd 			 * XXX todo, free the node outside of
2806375307d4SAdrian Chadd 			 * the TX lock context!
2807375307d4SAdrian Chadd 			 */
2808c42a7b7eSSam Leffler 			if (ni != NULL)
2809c42a7b7eSSam Leffler 				ieee80211_free_node(ni);
28101a85141aSAdrian Chadd 			continue;
28111a85141aSAdrian Chadd 		}
28121a85141aSAdrian Chadd 
2813548a605dSAdrian Chadd 		/*
2814548a605dSAdrian Chadd 		 * Check here if the node is in power save state.
2815548a605dSAdrian Chadd 		 */
2816548a605dSAdrian Chadd 		ath_tx_update_tim(sc, ni, 1);
2817548a605dSAdrian Chadd 
28181a85141aSAdrian Chadd 		if (next != NULL) {
281968e8e04eSSam Leffler 			/*
28201a85141aSAdrian Chadd 			 * Beware of state changing between frags.
28211a85141aSAdrian Chadd 			 * XXX check sta power-save state?
282268e8e04eSSam Leffler 			 */
28231a85141aSAdrian Chadd 			if (ni->ni_vap->iv_state != IEEE80211_S_RUN) {
2824c5239edbSAdrian Chadd 				DPRINTF(sc, ATH_DEBUG_XMIT,
28251a85141aSAdrian Chadd 				    "%s: flush fragmented packet, state %s\n",
28261a85141aSAdrian Chadd 				    __func__,
28271a85141aSAdrian Chadd 				    ieee80211_state_name[ni->ni_vap->iv_state]);
2828a91ab3c0SAdrian Chadd 				/* XXX dmamap */
28291a85141aSAdrian Chadd 				ath_freetx(next);
28301a85141aSAdrian Chadd 				goto reclaim;
2831c5239edbSAdrian Chadd 			}
28321a85141aSAdrian Chadd 			m = next;
28331a85141aSAdrian Chadd 			bf = TAILQ_FIRST(&frags);
28341a85141aSAdrian Chadd 			KASSERT(bf != NULL, ("no buf for txfrag"));
28351a85141aSAdrian Chadd 			TAILQ_REMOVE(&frags, bf, bf_list);
28361a85141aSAdrian Chadd 			goto nextfrag;
2837c5239edbSAdrian Chadd 		}
2838c5239edbSAdrian Chadd 
28391a85141aSAdrian Chadd 		sc->sc_wd_timer = 5;
28405591b213SSam Leffler 	}
28411a85141aSAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TX, 1, "ath_start: finished; npkts=%d", npkts);
28421a85141aSAdrian Chadd }
28435591b213SSam Leffler static int
28445591b213SSam Leffler ath_media_change(struct ifnet *ifp)
28455591b213SSam Leffler {
2846b032f27cSSam Leffler 	int error = ieee80211_media_change(ifp);
2847b032f27cSSam Leffler 	/* NB: only the fixed rate can change and that doesn't need a reset */
2848b032f27cSSam Leffler 	return (error == ENETRESET ? 0 : error);
28495591b213SSam Leffler }
28505591b213SSam Leffler 
2851c42a7b7eSSam Leffler /*
2852c42a7b7eSSam Leffler  * Block/unblock tx+rx processing while a key change is done.
2853c42a7b7eSSam Leffler  * We assume the caller serializes key management operations
2854c42a7b7eSSam Leffler  * so we only need to worry about synchronization with other
2855c42a7b7eSSam Leffler  * uses that originate in the driver.
2856c42a7b7eSSam Leffler  */
2857c42a7b7eSSam Leffler static void
2858b032f27cSSam Leffler ath_key_update_begin(struct ieee80211vap *vap)
2859c42a7b7eSSam Leffler {
2860b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ic->ic_ifp;
2861c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2862c42a7b7eSSam Leffler 
2863c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
2864b032f27cSSam Leffler 	taskqueue_block(sc->sc_tq);
2865c42a7b7eSSam Leffler 	IF_LOCK(&ifp->if_snd);		/* NB: doesn't block mgmt frames */
2866c42a7b7eSSam Leffler }
2867c42a7b7eSSam Leffler 
2868c42a7b7eSSam Leffler static void
2869b032f27cSSam Leffler ath_key_update_end(struct ieee80211vap *vap)
2870c42a7b7eSSam Leffler {
2871b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ic->ic_ifp;
2872c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2873c42a7b7eSSam Leffler 
2874c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
2875c42a7b7eSSam Leffler 	IF_UNLOCK(&ifp->if_snd);
2876b032f27cSSam Leffler 	taskqueue_unblock(sc->sc_tq);
2877c42a7b7eSSam Leffler }
28785591b213SSam Leffler 
2879b032f27cSSam Leffler static void
2880b032f27cSSam Leffler ath_update_promisc(struct ifnet *ifp)
2881b032f27cSSam Leffler {
2882b032f27cSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2883b032f27cSSam Leffler 	u_int32_t rfilt;
2884b032f27cSSam Leffler 
2885b032f27cSSam Leffler 	/* configure rx filter */
2886b032f27cSSam Leffler 	rfilt = ath_calcrxfilter(sc);
2887b032f27cSSam Leffler 	ath_hal_setrxfilter(sc->sc_ah, rfilt);
2888b032f27cSSam Leffler 
2889b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x\n", __func__, rfilt);
2890b032f27cSSam Leffler }
2891b032f27cSSam Leffler 
2892b032f27cSSam Leffler static void
2893b032f27cSSam Leffler ath_update_mcast(struct ifnet *ifp)
2894b032f27cSSam Leffler {
2895b032f27cSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2896b032f27cSSam Leffler 	u_int32_t mfilt[2];
2897b032f27cSSam Leffler 
2898b032f27cSSam Leffler 	/* calculate and install multicast filter */
2899b032f27cSSam Leffler 	if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
2900b032f27cSSam Leffler 		struct ifmultiaddr *ifma;
2901b032f27cSSam Leffler 		/*
2902b032f27cSSam Leffler 		 * Merge multicast addresses to form the hardware filter.
2903b032f27cSSam Leffler 		 */
2904b032f27cSSam Leffler 		mfilt[0] = mfilt[1] = 0;
2905eb956cd0SRobert Watson 		if_maddr_rlock(ifp);	/* XXX need some fiddling to remove? */
2906b032f27cSSam Leffler 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2907b032f27cSSam Leffler 			caddr_t dl;
2908b032f27cSSam Leffler 			u_int32_t val;
2909b032f27cSSam Leffler 			u_int8_t pos;
2910b032f27cSSam Leffler 
2911b032f27cSSam Leffler 			/* calculate XOR of eight 6bit values */
2912b032f27cSSam Leffler 			dl = LLADDR((struct sockaddr_dl *) ifma->ifma_addr);
2913b032f27cSSam Leffler 			val = LE_READ_4(dl + 0);
2914b032f27cSSam Leffler 			pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
2915b032f27cSSam Leffler 			val = LE_READ_4(dl + 3);
2916b032f27cSSam Leffler 			pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
2917b032f27cSSam Leffler 			pos &= 0x3f;
2918b032f27cSSam Leffler 			mfilt[pos / 32] |= (1 << (pos % 32));
2919b032f27cSSam Leffler 		}
2920eb956cd0SRobert Watson 		if_maddr_runlock(ifp);
2921b032f27cSSam Leffler 	} else
2922b032f27cSSam Leffler 		mfilt[0] = mfilt[1] = ~0;
2923b032f27cSSam Leffler 	ath_hal_setmcastfilter(sc->sc_ah, mfilt[0], mfilt[1]);
2924b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: MC filter %08x:%08x\n",
2925b032f27cSSam Leffler 		__func__, mfilt[0], mfilt[1]);
29264bc0e754SSam Leffler }
29274bc0e754SSam Leffler 
2928e60c4fc2SAdrian Chadd void
29295591b213SSam Leffler ath_mode_init(struct ath_softc *sc)
29305591b213SSam Leffler {
2931fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
2932b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2933b032f27cSSam Leffler 	u_int32_t rfilt;
29345591b213SSam Leffler 
29354bc0e754SSam Leffler 	/* configure rx filter */
293668e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
29374bc0e754SSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
29384bc0e754SSam Leffler 
29395591b213SSam Leffler 	/* configure operational mode */
2940c42a7b7eSSam Leffler 	ath_hal_setopmode(ah);
2941c42a7b7eSSam Leffler 
29423d184db2SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_STATE | ATH_DEBUG_MODE,
29433d184db2SAdrian Chadd 	    "%s: ah=%p, ifp=%p, if_addr=%p\n",
29443d184db2SAdrian Chadd 	    __func__,
29453d184db2SAdrian Chadd 	    ah,
29463d184db2SAdrian Chadd 	    ifp,
29473d184db2SAdrian Chadd 	    (ifp == NULL) ? NULL : ifp->if_addr);
29483d184db2SAdrian Chadd 
294929aca940SSam Leffler 	/* handle any link-level address change */
295029aca940SSam Leffler 	ath_hal_setmac(ah, IF_LLADDR(ifp));
29515591b213SSam Leffler 
29525591b213SSam Leffler 	/* calculate and install multicast filter */
2953b032f27cSSam Leffler 	ath_update_mcast(ifp);
29545591b213SSam Leffler }
29555591b213SSam Leffler 
2956c42a7b7eSSam Leffler /*
2957c42a7b7eSSam Leffler  * Set the slot time based on the current setting.
2958c42a7b7eSSam Leffler  */
2959ba5c15d9SAdrian Chadd void
2960c42a7b7eSSam Leffler ath_setslottime(struct ath_softc *sc)
2961c42a7b7eSSam Leffler {
2962b032f27cSSam Leffler 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2963c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2964aaa70f2fSSam Leffler 	u_int usec;
2965c42a7b7eSSam Leffler 
2966aaa70f2fSSam Leffler 	if (IEEE80211_IS_CHAN_HALF(ic->ic_curchan))
2967aaa70f2fSSam Leffler 		usec = 13;
2968aaa70f2fSSam Leffler 	else if (IEEE80211_IS_CHAN_QUARTER(ic->ic_curchan))
2969aaa70f2fSSam Leffler 		usec = 21;
2970724c193aSSam Leffler 	else if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan)) {
2971724c193aSSam Leffler 		/* honor short/long slot time only in 11g */
2972724c193aSSam Leffler 		/* XXX shouldn't honor on pure g or turbo g channel */
2973724c193aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
2974aaa70f2fSSam Leffler 			usec = HAL_SLOT_TIME_9;
2975aaa70f2fSSam Leffler 		else
2976aaa70f2fSSam Leffler 			usec = HAL_SLOT_TIME_20;
2977724c193aSSam Leffler 	} else
2978724c193aSSam Leffler 		usec = HAL_SLOT_TIME_9;
2979aaa70f2fSSam Leffler 
2980aaa70f2fSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET,
2981aaa70f2fSSam Leffler 	    "%s: chan %u MHz flags 0x%x %s slot, %u usec\n",
2982aaa70f2fSSam Leffler 	    __func__, ic->ic_curchan->ic_freq, ic->ic_curchan->ic_flags,
2983aaa70f2fSSam Leffler 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", usec);
2984aaa70f2fSSam Leffler 
2985aaa70f2fSSam Leffler 	ath_hal_setslottime(ah, usec);
2986c42a7b7eSSam Leffler 	sc->sc_updateslot = OK;
2987c42a7b7eSSam Leffler }
2988c42a7b7eSSam Leffler 
2989c42a7b7eSSam Leffler /*
2990c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update the
2991c42a7b7eSSam Leffler  * slot time based on the current setting.
2992c42a7b7eSSam Leffler  */
2993c42a7b7eSSam Leffler static void
2994c42a7b7eSSam Leffler ath_updateslot(struct ifnet *ifp)
2995c42a7b7eSSam Leffler {
2996c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2997b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
2998c42a7b7eSSam Leffler 
2999c42a7b7eSSam Leffler 	/*
3000c42a7b7eSSam Leffler 	 * When not coordinating the BSS, change the hardware
3001c42a7b7eSSam Leffler 	 * immediately.  For other operation we defer the change
3002c42a7b7eSSam Leffler 	 * until beacon updates have propagated to the stations.
3003c42a7b7eSSam Leffler 	 */
300459aa14a9SRui Paulo 	if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
300559aa14a9SRui Paulo 	    ic->ic_opmode == IEEE80211_M_MBSS)
3006c42a7b7eSSam Leffler 		sc->sc_updateslot = UPDATE;
3007c42a7b7eSSam Leffler 	else
3008c42a7b7eSSam Leffler 		ath_setslottime(sc);
3009c42a7b7eSSam Leffler }
3010c42a7b7eSSam Leffler 
3011c42a7b7eSSam Leffler /*
3012622b3fd2SSam Leffler  * Append the contents of src to dst; both queues
3013622b3fd2SSam Leffler  * are assumed to be locked.
3014622b3fd2SSam Leffler  */
3015ba5c15d9SAdrian Chadd void
3016622b3fd2SSam Leffler ath_txqmove(struct ath_txq *dst, struct ath_txq *src)
3017622b3fd2SSam Leffler {
3018e86fd7a7SAdrian Chadd 
3019b837332dSAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(src);
3020b837332dSAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(dst);
3021b837332dSAdrian Chadd 
30226b349e5aSAdrian Chadd 	TAILQ_CONCAT(&dst->axq_q, &src->axq_q, bf_list);
3023622b3fd2SSam Leffler 	dst->axq_link = src->axq_link;
3024622b3fd2SSam Leffler 	src->axq_link = NULL;
3025622b3fd2SSam Leffler 	dst->axq_depth += src->axq_depth;
30266edf1dc7SAdrian Chadd 	dst->axq_aggr_depth += src->axq_aggr_depth;
3027622b3fd2SSam Leffler 	src->axq_depth = 0;
30286edf1dc7SAdrian Chadd 	src->axq_aggr_depth = 0;
3029622b3fd2SSam Leffler }
3030622b3fd2SSam Leffler 
3031622b3fd2SSam Leffler /*
3032d52f7132SAdrian Chadd  * Reset the hardware, with no loss.
3033d52f7132SAdrian Chadd  *
3034d52f7132SAdrian Chadd  * This can't be used for a general case reset.
3035d52f7132SAdrian Chadd  */
3036d52f7132SAdrian Chadd static void
3037d52f7132SAdrian Chadd ath_reset_proc(void *arg, int pending)
3038d52f7132SAdrian Chadd {
3039d52f7132SAdrian Chadd 	struct ath_softc *sc = arg;
3040d52f7132SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
3041d52f7132SAdrian Chadd 
3042d52f7132SAdrian Chadd #if 0
3043d52f7132SAdrian Chadd 	if_printf(ifp, "%s: resetting\n", __func__);
3044d52f7132SAdrian Chadd #endif
3045d52f7132SAdrian Chadd 	ath_reset(ifp, ATH_RESET_NOLOSS);
3046d52f7132SAdrian Chadd }
3047d52f7132SAdrian Chadd 
3048d52f7132SAdrian Chadd /*
3049c42a7b7eSSam Leffler  * Reset the hardware after detecting beacons have stopped.
3050c42a7b7eSSam Leffler  */
3051c42a7b7eSSam Leffler static void
3052c42a7b7eSSam Leffler ath_bstuck_proc(void *arg, int pending)
3053c42a7b7eSSam Leffler {
3054c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
3055fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
305616d4de92SAdrian Chadd 	uint32_t hangs = 0;
305716d4de92SAdrian Chadd 
305816d4de92SAdrian Chadd 	if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0)
305916d4de92SAdrian Chadd 		if_printf(ifp, "bb hang detected (0x%x)\n", hangs);
3060c42a7b7eSSam Leffler 
3061*370f81faSAdrian Chadd #ifdef	ATH_DEBUG_ALQ
3062*370f81faSAdrian Chadd 	if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_STUCK_BEACON))
3063*370f81faSAdrian Chadd 		if_ath_alq_post(&sc->sc_alq, ATH_ALQ_STUCK_BEACON, 0, NULL);
3064*370f81faSAdrian Chadd #endif
3065*370f81faSAdrian Chadd 
3066c42a7b7eSSam Leffler 	if_printf(ifp, "stuck beacon; resetting (bmiss count %u)\n",
3067c42a7b7eSSam Leffler 		sc->sc_bmisscount);
3068c2e34459SSam Leffler 	sc->sc_stats.ast_bstuck++;
306916d4de92SAdrian Chadd 	/*
307016d4de92SAdrian Chadd 	 * This assumes that there's no simultaneous channel mode change
307116d4de92SAdrian Chadd 	 * occuring.
307216d4de92SAdrian Chadd 	 */
3073517526efSAdrian Chadd 	ath_reset(ifp, ATH_RESET_NOLOSS);
3074c42a7b7eSSam Leffler }
3075c42a7b7eSSam Leffler 
30765591b213SSam Leffler static void
30775591b213SSam Leffler ath_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
30785591b213SSam Leffler {
30795591b213SSam Leffler 	bus_addr_t *paddr = (bus_addr_t*) arg;
3080d77367bfSSam Leffler 	KASSERT(error == 0, ("error %u on bus_dma callback", error));
30815591b213SSam Leffler 	*paddr = segs->ds_addr;
30825591b213SSam Leffler }
30835591b213SSam Leffler 
3084c9f78537SAdrian Chadd /*
3085c9f78537SAdrian Chadd  * Allocate the descriptors and appropriate DMA tag/setup.
3086c9f78537SAdrian Chadd  *
3087c9f78537SAdrian Chadd  * For some situations (eg EDMA TX completion), there isn't a requirement
3088c9f78537SAdrian Chadd  * for the ath_buf entries to be allocated.
3089c9f78537SAdrian Chadd  */
30903d184db2SAdrian Chadd int
3091c9f78537SAdrian Chadd ath_descdma_alloc_desc(struct ath_softc *sc,
3092c42a7b7eSSam Leffler 	struct ath_descdma *dd, ath_bufhead *head,
3093b39722d6SAdrian Chadd 	const char *name, int ds_size, int ndesc)
3094c42a7b7eSSam Leffler {
3095c42a7b7eSSam Leffler #define	DS2PHYS(_dd, _ds) \
3096c42a7b7eSSam Leffler 	((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
309745abcd6cSAdrian Chadd #define	ATH_DESC_4KB_BOUND_CHECK(_daddr, _len) \
309845abcd6cSAdrian Chadd 	((((u_int32_t)(_daddr) & 0xFFF) > (0x1000 - (_len))) ? 1 : 0)
3099fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
3100c9f78537SAdrian Chadd 	int error;
310145abcd6cSAdrian Chadd 
31021006fc0cSAdrian Chadd 	dd->dd_descsize = ds_size;
3103c42a7b7eSSam Leffler 
31043d9b1596SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET,
3105b39722d6SAdrian Chadd 	    "%s: %s DMA: %u desc, %d bytes per descriptor\n",
3106b39722d6SAdrian Chadd 	    __func__, name, ndesc, dd->dd_descsize);
3107c42a7b7eSSam Leffler 
3108c42a7b7eSSam Leffler 	dd->dd_name = name;
3109b39722d6SAdrian Chadd 	dd->dd_desc_len = dd->dd_descsize * ndesc;
311045abcd6cSAdrian Chadd 
311145abcd6cSAdrian Chadd 	/*
311245abcd6cSAdrian Chadd 	 * Merlin work-around:
311345abcd6cSAdrian Chadd 	 * Descriptors that cross the 4KB boundary can't be used.
311445abcd6cSAdrian Chadd 	 * Assume one skipped descriptor per 4KB page.
311545abcd6cSAdrian Chadd 	 */
311645abcd6cSAdrian Chadd 	if (! ath_hal_split4ktrans(sc->sc_ah)) {
3117b39722d6SAdrian Chadd 		int numpages = dd->dd_desc_len / 4096;
3118b39722d6SAdrian Chadd 		dd->dd_desc_len += ds_size * numpages;
311945abcd6cSAdrian Chadd 	}
3120c42a7b7eSSam Leffler 
3121c42a7b7eSSam Leffler 	/*
3122c42a7b7eSSam Leffler 	 * Setup DMA descriptor area.
3123a91ab3c0SAdrian Chadd 	 *
3124a91ab3c0SAdrian Chadd 	 * BUS_DMA_ALLOCNOW is not used; we never use bounce
3125a91ab3c0SAdrian Chadd 	 * buffers for the descriptors themselves.
3126c42a7b7eSSam Leffler 	 */
3127c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev),	/* parent */
3128c42a7b7eSSam Leffler 		       PAGE_SIZE, 0,		/* alignment, bounds */
3129c42a7b7eSSam Leffler 		       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
3130c42a7b7eSSam Leffler 		       BUS_SPACE_MAXADDR,	/* highaddr */
3131c42a7b7eSSam Leffler 		       NULL, NULL,		/* filter, filterarg */
3132c42a7b7eSSam Leffler 		       dd->dd_desc_len,		/* maxsize */
3133c42a7b7eSSam Leffler 		       1,			/* nsegments */
31346ccb8ea7SSam Leffler 		       dd->dd_desc_len,		/* maxsegsize */
3135a91ab3c0SAdrian Chadd 		       0,			/* flags */
3136c42a7b7eSSam Leffler 		       NULL,			/* lockfunc */
3137c42a7b7eSSam Leffler 		       NULL,			/* lockarg */
3138c42a7b7eSSam Leffler 		       &dd->dd_dmat);
3139c42a7b7eSSam Leffler 	if (error != 0) {
3140c42a7b7eSSam Leffler 		if_printf(ifp, "cannot allocate %s DMA tag\n", dd->dd_name);
3141c42a7b7eSSam Leffler 		return error;
3142c42a7b7eSSam Leffler 	}
3143c42a7b7eSSam Leffler 
3144c42a7b7eSSam Leffler 	/* allocate descriptors */
3145c42a7b7eSSam Leffler 	error = bus_dmamem_alloc(dd->dd_dmat, (void**) &dd->dd_desc,
31460553a01fSSam Leffler 				 BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
31470553a01fSSam Leffler 				 &dd->dd_dmamap);
3148c42a7b7eSSam Leffler 	if (error != 0) {
3149c42a7b7eSSam Leffler 		if_printf(ifp, "unable to alloc memory for %u %s descriptors, "
3150b39722d6SAdrian Chadd 			"error %u\n", ndesc, dd->dd_name, error);
3151c42a7b7eSSam Leffler 		goto fail1;
3152c42a7b7eSSam Leffler 	}
3153c42a7b7eSSam Leffler 
3154c42a7b7eSSam Leffler 	error = bus_dmamap_load(dd->dd_dmat, dd->dd_dmamap,
3155c42a7b7eSSam Leffler 				dd->dd_desc, dd->dd_desc_len,
3156c42a7b7eSSam Leffler 				ath_load_cb, &dd->dd_desc_paddr,
3157c42a7b7eSSam Leffler 				BUS_DMA_NOWAIT);
3158c42a7b7eSSam Leffler 	if (error != 0) {
3159c42a7b7eSSam Leffler 		if_printf(ifp, "unable to map %s descriptors, error %u\n",
3160c42a7b7eSSam Leffler 			dd->dd_name, error);
3161c42a7b7eSSam Leffler 		goto fail2;
3162c42a7b7eSSam Leffler 	}
3163c42a7b7eSSam Leffler 
3164c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA map: %p (%lu) -> %p (%lu)\n",
3165c9f78537SAdrian Chadd 	    __func__, dd->dd_name, (uint8_t *) dd->dd_desc,
3166c9f78537SAdrian Chadd 	    (u_long) dd->dd_desc_len, (caddr_t) dd->dd_desc_paddr,
3167c9f78537SAdrian Chadd 	    /*XXX*/ (u_long) dd->dd_desc_len);
3168c9f78537SAdrian Chadd 
3169c9f78537SAdrian Chadd 	return (0);
3170c9f78537SAdrian Chadd 
3171c9f78537SAdrian Chadd fail2:
3172c9f78537SAdrian Chadd 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3173c9f78537SAdrian Chadd fail1:
3174c9f78537SAdrian Chadd 	bus_dma_tag_destroy(dd->dd_dmat);
3175c9f78537SAdrian Chadd 	memset(dd, 0, sizeof(*dd));
3176c9f78537SAdrian Chadd 	return error;
3177c9f78537SAdrian Chadd #undef DS2PHYS
3178c9f78537SAdrian Chadd #undef ATH_DESC_4KB_BOUND_CHECK
3179c9f78537SAdrian Chadd }
3180c9f78537SAdrian Chadd 
3181c9f78537SAdrian Chadd int
3182c9f78537SAdrian Chadd ath_descdma_setup(struct ath_softc *sc,
3183c9f78537SAdrian Chadd 	struct ath_descdma *dd, ath_bufhead *head,
3184c9f78537SAdrian Chadd 	const char *name, int ds_size, int nbuf, int ndesc)
3185c9f78537SAdrian Chadd {
3186c9f78537SAdrian Chadd #define	DS2PHYS(_dd, _ds) \
3187c9f78537SAdrian Chadd 	((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
3188c9f78537SAdrian Chadd #define	ATH_DESC_4KB_BOUND_CHECK(_daddr, _len) \
3189c9f78537SAdrian Chadd 	((((u_int32_t)(_daddr) & 0xFFF) > (0x1000 - (_len))) ? 1 : 0)
3190c9f78537SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
3191c9f78537SAdrian Chadd 	uint8_t *ds;
3192c9f78537SAdrian Chadd 	struct ath_buf *bf;
3193c9f78537SAdrian Chadd 	int i, bsize, error;
3194c9f78537SAdrian Chadd 
3195c9f78537SAdrian Chadd 	/* Allocate descriptors */
3196c9f78537SAdrian Chadd 	error = ath_descdma_alloc_desc(sc, dd, head, name, ds_size,
3197b39722d6SAdrian Chadd 	    nbuf * ndesc);
3198c9f78537SAdrian Chadd 
3199c9f78537SAdrian Chadd 	/* Assume any errors during allocation were dealt with */
3200c9f78537SAdrian Chadd 	if (error != 0) {
3201c9f78537SAdrian Chadd 		return (error);
3202c9f78537SAdrian Chadd 	}
3203c9f78537SAdrian Chadd 
3204c9f78537SAdrian Chadd 	ds = (uint8_t *) dd->dd_desc;
3205c42a7b7eSSam Leffler 
3206ebecf802SSam Leffler 	/* allocate rx buffers */
3207c42a7b7eSSam Leffler 	bsize = sizeof(struct ath_buf) * nbuf;
3208c42a7b7eSSam Leffler 	bf = malloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO);
3209c42a7b7eSSam Leffler 	if (bf == NULL) {
3210c42a7b7eSSam Leffler 		if_printf(ifp, "malloc of %s buffers failed, size %u\n",
3211c42a7b7eSSam Leffler 			dd->dd_name, bsize);
3212c42a7b7eSSam Leffler 		goto fail3;
3213c42a7b7eSSam Leffler 	}
3214c42a7b7eSSam Leffler 	dd->dd_bufptr = bf;
3215c42a7b7eSSam Leffler 
32166b349e5aSAdrian Chadd 	TAILQ_INIT(head);
32173d9b1596SAdrian Chadd 	for (i = 0; i < nbuf; i++, bf++, ds += (ndesc * dd->dd_descsize)) {
321845abcd6cSAdrian Chadd 		bf->bf_desc = (struct ath_desc *) ds;
3219c42a7b7eSSam Leffler 		bf->bf_daddr = DS2PHYS(dd, ds);
322045abcd6cSAdrian Chadd 		if (! ath_hal_split4ktrans(sc->sc_ah)) {
322145abcd6cSAdrian Chadd 			/*
322245abcd6cSAdrian Chadd 			 * Merlin WAR: Skip descriptor addresses which
322345abcd6cSAdrian Chadd 			 * cause 4KB boundary crossing along any point
322445abcd6cSAdrian Chadd 			 * in the descriptor.
322545abcd6cSAdrian Chadd 			 */
322645abcd6cSAdrian Chadd 			 if (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr,
32277ef7f613SAdrian Chadd 			     dd->dd_descsize)) {
322845abcd6cSAdrian Chadd 				/* Start at the next page */
322945abcd6cSAdrian Chadd 				ds += 0x1000 - (bf->bf_daddr & 0xFFF);
323045abcd6cSAdrian Chadd 				bf->bf_desc = (struct ath_desc *) ds;
323145abcd6cSAdrian Chadd 				bf->bf_daddr = DS2PHYS(dd, ds);
323245abcd6cSAdrian Chadd 			}
323345abcd6cSAdrian Chadd 		}
3234c42a7b7eSSam Leffler 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
3235c42a7b7eSSam Leffler 				&bf->bf_dmamap);
3236c42a7b7eSSam Leffler 		if (error != 0) {
3237c42a7b7eSSam Leffler 			if_printf(ifp, "unable to create dmamap for %s "
3238c42a7b7eSSam Leffler 				"buffer %u, error %u\n", dd->dd_name, i, error);
3239c42a7b7eSSam Leffler 			ath_descdma_cleanup(sc, dd, head);
3240c42a7b7eSSam Leffler 			return error;
3241c42a7b7eSSam Leffler 		}
32426edf1dc7SAdrian Chadd 		bf->bf_lastds = bf->bf_desc;	/* Just an initial value */
32436b349e5aSAdrian Chadd 		TAILQ_INSERT_TAIL(head, bf, bf_list);
3244c42a7b7eSSam Leffler 	}
32457ef7f613SAdrian Chadd 
32467ef7f613SAdrian Chadd 	/*
32477ef7f613SAdrian Chadd 	 * XXX TODO: ensure that ds doesn't overflow the descriptor
32487ef7f613SAdrian Chadd 	 * allocation otherwise weird stuff will occur and crash your
32497ef7f613SAdrian Chadd 	 * machine.
32507ef7f613SAdrian Chadd 	 */
3251c42a7b7eSSam Leffler 	return 0;
3252c9f78537SAdrian Chadd 	/* XXX this should likely just call ath_descdma_cleanup() */
3253c42a7b7eSSam Leffler fail3:
3254c42a7b7eSSam Leffler 	bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
3255c42a7b7eSSam Leffler 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3256c42a7b7eSSam Leffler 	bus_dma_tag_destroy(dd->dd_dmat);
3257c42a7b7eSSam Leffler 	memset(dd, 0, sizeof(*dd));
3258c42a7b7eSSam Leffler 	return error;
3259c42a7b7eSSam Leffler #undef DS2PHYS
326045abcd6cSAdrian Chadd #undef ATH_DESC_4KB_BOUND_CHECK
3261c42a7b7eSSam Leffler }
3262c42a7b7eSSam Leffler 
326339abbd9bSAdrian Chadd /*
326439abbd9bSAdrian Chadd  * Allocate ath_buf entries but no descriptor contents.
326539abbd9bSAdrian Chadd  *
326639abbd9bSAdrian Chadd  * This is for RX EDMA where the descriptors are the header part of
326739abbd9bSAdrian Chadd  * the RX buffer.
326839abbd9bSAdrian Chadd  */
326939abbd9bSAdrian Chadd int
327039abbd9bSAdrian Chadd ath_descdma_setup_rx_edma(struct ath_softc *sc,
327139abbd9bSAdrian Chadd 	struct ath_descdma *dd, ath_bufhead *head,
327239abbd9bSAdrian Chadd 	const char *name, int nbuf, int rx_status_len)
327339abbd9bSAdrian Chadd {
327439abbd9bSAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
327539abbd9bSAdrian Chadd 	struct ath_buf *bf;
327639abbd9bSAdrian Chadd 	int i, bsize, error;
327739abbd9bSAdrian Chadd 
327839abbd9bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA: %u buffers\n",
327939abbd9bSAdrian Chadd 	    __func__, name, nbuf);
328039abbd9bSAdrian Chadd 
328139abbd9bSAdrian Chadd 	dd->dd_name = name;
328239abbd9bSAdrian Chadd 	/*
328339abbd9bSAdrian Chadd 	 * This is (mostly) purely for show.  We're not allocating any actual
328439abbd9bSAdrian Chadd 	 * descriptors here as EDMA RX has the descriptor be part
328539abbd9bSAdrian Chadd 	 * of the RX buffer.
328639abbd9bSAdrian Chadd 	 *
328739abbd9bSAdrian Chadd 	 * However, dd_desc_len is used by ath_descdma_free() to determine
328839abbd9bSAdrian Chadd 	 * whether we have already freed this DMA mapping.
328939abbd9bSAdrian Chadd 	 */
32903d9b1596SAdrian Chadd 	dd->dd_desc_len = rx_status_len * nbuf;
32913d9b1596SAdrian Chadd 	dd->dd_descsize = rx_status_len;
329239abbd9bSAdrian Chadd 
329339abbd9bSAdrian Chadd 	/* allocate rx buffers */
329439abbd9bSAdrian Chadd 	bsize = sizeof(struct ath_buf) * nbuf;
329539abbd9bSAdrian Chadd 	bf = malloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO);
329639abbd9bSAdrian Chadd 	if (bf == NULL) {
329739abbd9bSAdrian Chadd 		if_printf(ifp, "malloc of %s buffers failed, size %u\n",
329839abbd9bSAdrian Chadd 			dd->dd_name, bsize);
3299b5b60f35SAdrian Chadd 		error = ENOMEM;
330039abbd9bSAdrian Chadd 		goto fail3;
330139abbd9bSAdrian Chadd 	}
330239abbd9bSAdrian Chadd 	dd->dd_bufptr = bf;
330339abbd9bSAdrian Chadd 
330439abbd9bSAdrian Chadd 	TAILQ_INIT(head);
330539abbd9bSAdrian Chadd 	for (i = 0; i < nbuf; i++, bf++) {
330639abbd9bSAdrian Chadd 		bf->bf_desc = NULL;
330739abbd9bSAdrian Chadd 		bf->bf_daddr = 0;
330839abbd9bSAdrian Chadd 		bf->bf_lastds = NULL;	/* Just an initial value */
330939abbd9bSAdrian Chadd 
331039abbd9bSAdrian Chadd 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
331139abbd9bSAdrian Chadd 				&bf->bf_dmamap);
331239abbd9bSAdrian Chadd 		if (error != 0) {
331339abbd9bSAdrian Chadd 			if_printf(ifp, "unable to create dmamap for %s "
331439abbd9bSAdrian Chadd 				"buffer %u, error %u\n", dd->dd_name, i, error);
331539abbd9bSAdrian Chadd 			ath_descdma_cleanup(sc, dd, head);
331639abbd9bSAdrian Chadd 			return error;
331739abbd9bSAdrian Chadd 		}
331839abbd9bSAdrian Chadd 		TAILQ_INSERT_TAIL(head, bf, bf_list);
331939abbd9bSAdrian Chadd 	}
332039abbd9bSAdrian Chadd 	return 0;
332139abbd9bSAdrian Chadd fail3:
332239abbd9bSAdrian Chadd 	memset(dd, 0, sizeof(*dd));
332339abbd9bSAdrian Chadd 	return error;
332439abbd9bSAdrian Chadd }
332539abbd9bSAdrian Chadd 
33263d184db2SAdrian Chadd void
3327c42a7b7eSSam Leffler ath_descdma_cleanup(struct ath_softc *sc,
3328c42a7b7eSSam Leffler 	struct ath_descdma *dd, ath_bufhead *head)
3329c42a7b7eSSam Leffler {
3330c42a7b7eSSam Leffler 	struct ath_buf *bf;
3331c42a7b7eSSam Leffler 	struct ieee80211_node *ni;
3332a91ab3c0SAdrian Chadd 	int do_warning = 0;
3333c42a7b7eSSam Leffler 
33348d467c41SAdrian Chadd 	if (dd->dd_dmamap != 0) {
3335c42a7b7eSSam Leffler 		bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
3336c42a7b7eSSam Leffler 		bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3337c42a7b7eSSam Leffler 		bus_dma_tag_destroy(dd->dd_dmat);
33388d467c41SAdrian Chadd 	}
3339c42a7b7eSSam Leffler 
33409ed9f02bSAdrian Chadd 	if (head != NULL) {
33416b349e5aSAdrian Chadd 		TAILQ_FOREACH(bf, head, bf_list) {
3342c42a7b7eSSam Leffler 			if (bf->bf_m) {
3343a91ab3c0SAdrian Chadd 				/*
3344a91ab3c0SAdrian Chadd 				 * XXX warn if there's buffers here.
3345a91ab3c0SAdrian Chadd 				 * XXX it should have been freed by the
3346a91ab3c0SAdrian Chadd 				 * owner!
3347a91ab3c0SAdrian Chadd 				 */
3348a91ab3c0SAdrian Chadd 
3349a91ab3c0SAdrian Chadd 				if (do_warning == 0) {
3350a91ab3c0SAdrian Chadd 					do_warning = 1;
3351a91ab3c0SAdrian Chadd 					device_printf(sc->sc_dev,
3352a91ab3c0SAdrian Chadd 					    "%s: %s: mbuf should've been"
3353a91ab3c0SAdrian Chadd 					    " unmapped/freed!\n",
3354a91ab3c0SAdrian Chadd 					    __func__,
3355a91ab3c0SAdrian Chadd 					    dd->dd_name);
3356a91ab3c0SAdrian Chadd 				}
3357a91ab3c0SAdrian Chadd 				bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
3358a91ab3c0SAdrian Chadd 				    BUS_DMASYNC_POSTREAD);
3359a91ab3c0SAdrian Chadd 				bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
3360c42a7b7eSSam Leffler 				m_freem(bf->bf_m);
3361c42a7b7eSSam Leffler 				bf->bf_m = NULL;
3362c42a7b7eSSam Leffler 			}
3363c42a7b7eSSam Leffler 			if (bf->bf_dmamap != NULL) {
3364c42a7b7eSSam Leffler 				bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
3365c42a7b7eSSam Leffler 				bf->bf_dmamap = NULL;
3366c42a7b7eSSam Leffler 			}
3367c42a7b7eSSam Leffler 			ni = bf->bf_node;
3368c42a7b7eSSam Leffler 			bf->bf_node = NULL;
3369c42a7b7eSSam Leffler 			if (ni != NULL) {
3370c42a7b7eSSam Leffler 				/*
3371c42a7b7eSSam Leffler 				 * Reclaim node reference.
3372c42a7b7eSSam Leffler 				 */
3373c42a7b7eSSam Leffler 				ieee80211_free_node(ni);
3374c42a7b7eSSam Leffler 			}
3375c42a7b7eSSam Leffler 		}
33769ed9f02bSAdrian Chadd 	}
3377c42a7b7eSSam Leffler 
33789ed9f02bSAdrian Chadd 	if (head != NULL)
33796b349e5aSAdrian Chadd 		TAILQ_INIT(head);
33809ed9f02bSAdrian Chadd 
33819ed9f02bSAdrian Chadd 	if (dd->dd_bufptr != NULL)
3382c42a7b7eSSam Leffler 		free(dd->dd_bufptr, M_ATHDEV);
3383c42a7b7eSSam Leffler 	memset(dd, 0, sizeof(*dd));
3384c42a7b7eSSam Leffler }
3385c42a7b7eSSam Leffler 
3386c42a7b7eSSam Leffler static int
33875591b213SSam Leffler ath_desc_alloc(struct ath_softc *sc)
33885591b213SSam Leffler {
3389c42a7b7eSSam Leffler 	int error;
33905591b213SSam Leffler 
3391c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_txdma, &sc->sc_txbuf,
339209067b6eSAdrian Chadd 		    "tx", sc->sc_tx_desclen, ath_txbuf, ATH_MAX_SCATTER);
3393c42a7b7eSSam Leffler 	if (error != 0) {
33945591b213SSam Leffler 		return error;
3395c42a7b7eSSam Leffler 	}
339623ced6c1SAdrian Chadd 	sc->sc_txbuf_cnt = ath_txbuf;
3397c42a7b7eSSam Leffler 
3398af33d486SAdrian Chadd 	error = ath_descdma_setup(sc, &sc->sc_txdma_mgmt, &sc->sc_txbuf_mgmt,
33991006fc0cSAdrian Chadd 		    "tx_mgmt", sc->sc_tx_desclen, ath_txbuf_mgmt,
34001006fc0cSAdrian Chadd 		    ATH_TXDESC);
3401af33d486SAdrian Chadd 	if (error != 0) {
3402af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3403af33d486SAdrian Chadd 		return error;
3404af33d486SAdrian Chadd 	}
3405af33d486SAdrian Chadd 
3406af33d486SAdrian Chadd 	/*
3407af33d486SAdrian Chadd 	 * XXX mark txbuf_mgmt frames with ATH_BUF_MGMT, so the
3408af33d486SAdrian Chadd 	 * flag doesn't have to be set in ath_getbuf_locked().
3409af33d486SAdrian Chadd 	 */
3410af33d486SAdrian Chadd 
3411c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_bdma, &sc->sc_bbuf,
34121006fc0cSAdrian Chadd 			"beacon", sc->sc_tx_desclen, ATH_BCBUF, 1);
3413c42a7b7eSSam Leffler 	if (error != 0) {
3414af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3415af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma_mgmt,
3416af33d486SAdrian Chadd 		    &sc->sc_txbuf_mgmt);
3417c42a7b7eSSam Leffler 		return error;
3418c42a7b7eSSam Leffler 	}
34195591b213SSam Leffler 	return 0;
34205591b213SSam Leffler }
34215591b213SSam Leffler 
34225591b213SSam Leffler static void
34235591b213SSam Leffler ath_desc_free(struct ath_softc *sc)
34245591b213SSam Leffler {
34255591b213SSam Leffler 
3426c42a7b7eSSam Leffler 	if (sc->sc_bdma.dd_desc_len != 0)
3427c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_bdma, &sc->sc_bbuf);
3428c42a7b7eSSam Leffler 	if (sc->sc_txdma.dd_desc_len != 0)
3429c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3430af33d486SAdrian Chadd 	if (sc->sc_txdma_mgmt.dd_desc_len != 0)
3431af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma_mgmt,
3432af33d486SAdrian Chadd 		    &sc->sc_txbuf_mgmt);
34335591b213SSam Leffler }
34345591b213SSam Leffler 
34355591b213SSam Leffler static struct ieee80211_node *
343638c208f8SSam Leffler ath_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
34375591b213SSam Leffler {
343838c208f8SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
3439c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
3440c42a7b7eSSam Leffler 	const size_t space = sizeof(struct ath_node) + sc->sc_rc->arc_space;
3441c42a7b7eSSam Leffler 	struct ath_node *an;
3442c42a7b7eSSam Leffler 
3443c42a7b7eSSam Leffler 	an = malloc(space, M_80211_NODE, M_NOWAIT|M_ZERO);
3444c42a7b7eSSam Leffler 	if (an == NULL) {
3445c42a7b7eSSam Leffler 		/* XXX stat+msg */
3446de5af704SSam Leffler 		return NULL;
34475591b213SSam Leffler 	}
3448c42a7b7eSSam Leffler 	ath_rate_node_init(sc, an);
34495591b213SSam Leffler 
34503dd85b26SAdrian Chadd 	/* Setup the mutex - there's no associd yet so set the name to NULL */
34513dd85b26SAdrian Chadd 	snprintf(an->an_name, sizeof(an->an_name), "%s: node %p",
34523dd85b26SAdrian Chadd 	    device_get_nameunit(sc->sc_dev), an);
34533dd85b26SAdrian Chadd 	mtx_init(&an->an_mtx, an->an_name, NULL, MTX_DEF);
34543dd85b26SAdrian Chadd 
3455eb6f0de0SAdrian Chadd 	/* XXX setup ath_tid */
3456eb6f0de0SAdrian Chadd 	ath_tx_tid_init(sc, an);
3457eb6f0de0SAdrian Chadd 
34589b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__, mac, ":", an);
3459c42a7b7eSSam Leffler 	return &an->an_node;
3460c42a7b7eSSam Leffler }
3461c42a7b7eSSam Leffler 
34625591b213SSam Leffler static void
34634afa805eSAdrian Chadd ath_node_cleanup(struct ieee80211_node *ni)
34644afa805eSAdrian Chadd {
34654afa805eSAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
34664afa805eSAdrian Chadd 	struct ath_softc *sc = ic->ic_ifp->if_softc;
34674afa805eSAdrian Chadd 
34689b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__,
34699b48fb4bSAdrian Chadd 	    ni->ni_macaddr, ":", ATH_NODE(ni));
34709b48fb4bSAdrian Chadd 
34714afa805eSAdrian Chadd 	/* Cleanup ath_tid, free unused bufs, unlink bufs in TXQ */
3472eb6f0de0SAdrian Chadd 	ath_tx_node_flush(sc, ATH_NODE(ni));
34734afa805eSAdrian Chadd 	ath_rate_node_cleanup(sc, ATH_NODE(ni));
34744afa805eSAdrian Chadd 	sc->sc_node_cleanup(ni);
34754afa805eSAdrian Chadd }
34764afa805eSAdrian Chadd 
34774afa805eSAdrian Chadd static void
3478c42a7b7eSSam Leffler ath_node_free(struct ieee80211_node *ni)
34795591b213SSam Leffler {
3480c42a7b7eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
3481c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
34821e774079SSam Leffler 
34839b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__,
34849b48fb4bSAdrian Chadd 	    ni->ni_macaddr, ":", ATH_NODE(ni));
34853dd85b26SAdrian Chadd 	mtx_destroy(&ATH_NODE(ni)->an_mtx);
3486c42a7b7eSSam Leffler 	sc->sc_node_free(ni);
34875591b213SSam Leffler }
34885591b213SSam Leffler 
348968e8e04eSSam Leffler static void
349068e8e04eSSam Leffler ath_node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
349168e8e04eSSam Leffler {
349268e8e04eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
349368e8e04eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
349468e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
349568e8e04eSSam Leffler 
3496b032f27cSSam Leffler 	*rssi = ic->ic_node_getrssi(ni);
349759efa8b5SSam Leffler 	if (ni->ni_chan != IEEE80211_CHAN_ANYC)
349859efa8b5SSam Leffler 		*noise = ath_hal_getchannoise(ah, ni->ni_chan);
349959efa8b5SSam Leffler 	else
350068e8e04eSSam Leffler 		*noise = -95;		/* nominally correct */
350168e8e04eSSam Leffler }
350268e8e04eSSam Leffler 
3503c42a7b7eSSam Leffler /*
3504c42a7b7eSSam Leffler  * Set the default antenna.
3505c42a7b7eSSam Leffler  */
3506e60c4fc2SAdrian Chadd void
3507c42a7b7eSSam Leffler ath_setdefantenna(struct ath_softc *sc, u_int antenna)
3508c42a7b7eSSam Leffler {
3509c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3510c42a7b7eSSam Leffler 
3511c42a7b7eSSam Leffler 	/* XXX block beacon interrupts */
3512c42a7b7eSSam Leffler 	ath_hal_setdefantenna(ah, antenna);
3513c42a7b7eSSam Leffler 	if (sc->sc_defant != antenna)
3514c42a7b7eSSam Leffler 		sc->sc_stats.ast_ant_defswitch++;
3515c42a7b7eSSam Leffler 	sc->sc_defant = antenna;
3516c42a7b7eSSam Leffler 	sc->sc_rxotherant = 0;
3517c42a7b7eSSam Leffler }
3518c42a7b7eSSam Leffler 
35195463c4a4SSam Leffler static void
3520622b3fd2SSam Leffler ath_txq_init(struct ath_softc *sc, struct ath_txq *txq, int qnum)
3521622b3fd2SSam Leffler {
3522622b3fd2SSam Leffler 	txq->axq_qnum = qnum;
3523339ccfb3SSam Leffler 	txq->axq_ac = 0;
3524622b3fd2SSam Leffler 	txq->axq_depth = 0;
352516d4de92SAdrian Chadd 	txq->axq_aggr_depth = 0;
3526622b3fd2SSam Leffler 	txq->axq_intrcnt = 0;
3527622b3fd2SSam Leffler 	txq->axq_link = NULL;
35286b349e5aSAdrian Chadd 	txq->axq_softc = sc;
35296b349e5aSAdrian Chadd 	TAILQ_INIT(&txq->axq_q);
35306b349e5aSAdrian Chadd 	TAILQ_INIT(&txq->axq_tidq);
35313feffbd7SAdrian Chadd 	TAILQ_INIT(&txq->fifo.axq_q);
3532b837332dSAdrian Chadd 	ATH_TXQ_LOCK_INIT(sc, txq);
3533622b3fd2SSam Leffler }
3534622b3fd2SSam Leffler 
35355591b213SSam Leffler /*
3536c42a7b7eSSam Leffler  * Setup a h/w transmit queue.
35375591b213SSam Leffler  */
3538c42a7b7eSSam Leffler static struct ath_txq *
3539c42a7b7eSSam Leffler ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
3540c42a7b7eSSam Leffler {
3541c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
3542c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3543c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
3544c42a7b7eSSam Leffler 	int qnum;
3545c42a7b7eSSam Leffler 
3546c42a7b7eSSam Leffler 	memset(&qi, 0, sizeof(qi));
3547c42a7b7eSSam Leffler 	qi.tqi_subtype = subtype;
3548c42a7b7eSSam Leffler 	qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
3549c42a7b7eSSam Leffler 	qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
3550c42a7b7eSSam Leffler 	qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
3551c42a7b7eSSam Leffler 	/*
3552c42a7b7eSSam Leffler 	 * Enable interrupts only for EOL and DESC conditions.
3553c42a7b7eSSam Leffler 	 * We mark tx descriptors to receive a DESC interrupt
3554c42a7b7eSSam Leffler 	 * when a tx queue gets deep; otherwise waiting for the
3555c42a7b7eSSam Leffler 	 * EOL to reap descriptors.  Note that this is done to
3556c42a7b7eSSam Leffler 	 * reduce interrupt load and this only defers reaping
3557c42a7b7eSSam Leffler 	 * descriptors, never transmitting frames.  Aside from
3558c42a7b7eSSam Leffler 	 * reducing interrupts this also permits more concurrency.
3559c42a7b7eSSam Leffler 	 * The only potential downside is if the tx queue backs
3560c42a7b7eSSam Leffler 	 * up in which case the top half of the kernel may backup
3561c42a7b7eSSam Leffler 	 * due to a lack of tx descriptors.
3562c42a7b7eSSam Leffler 	 */
35636961e9edSAdrian Chadd 	if (sc->sc_isedma)
35646961e9edSAdrian Chadd 		qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE |
35656961e9edSAdrian Chadd 		    HAL_TXQ_TXOKINT_ENABLE;
35666961e9edSAdrian Chadd 	else
35676961e9edSAdrian Chadd 		qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE |
35686961e9edSAdrian Chadd 		    HAL_TXQ_TXDESCINT_ENABLE;
35696961e9edSAdrian Chadd 
3570c42a7b7eSSam Leffler 	qnum = ath_hal_setuptxqueue(ah, qtype, &qi);
3571c42a7b7eSSam Leffler 	if (qnum == -1) {
3572c42a7b7eSSam Leffler 		/*
3573c42a7b7eSSam Leffler 		 * NB: don't print a message, this happens
3574a614e076SSam Leffler 		 * normally on parts with too few tx queues
3575c42a7b7eSSam Leffler 		 */
3576c42a7b7eSSam Leffler 		return NULL;
3577c42a7b7eSSam Leffler 	}
3578c42a7b7eSSam Leffler 	if (qnum >= N(sc->sc_txq)) {
35796891c875SPeter Wemm 		device_printf(sc->sc_dev,
35806891c875SPeter Wemm 			"hal qnum %u out of range, max %zu!\n",
3581c42a7b7eSSam Leffler 			qnum, N(sc->sc_txq));
3582c42a7b7eSSam Leffler 		ath_hal_releasetxqueue(ah, qnum);
3583c42a7b7eSSam Leffler 		return NULL;
3584c42a7b7eSSam Leffler 	}
3585c42a7b7eSSam Leffler 	if (!ATH_TXQ_SETUP(sc, qnum)) {
3586622b3fd2SSam Leffler 		ath_txq_init(sc, &sc->sc_txq[qnum], qnum);
3587c42a7b7eSSam Leffler 		sc->sc_txqsetup |= 1<<qnum;
3588c42a7b7eSSam Leffler 	}
3589c42a7b7eSSam Leffler 	return &sc->sc_txq[qnum];
3590c42a7b7eSSam Leffler #undef N
3591c42a7b7eSSam Leffler }
3592c42a7b7eSSam Leffler 
3593c42a7b7eSSam Leffler /*
3594c42a7b7eSSam Leffler  * Setup a hardware data transmit queue for the specified
3595c42a7b7eSSam Leffler  * access control.  The hal may not support all requested
3596c42a7b7eSSam Leffler  * queues in which case it will return a reference to a
3597c42a7b7eSSam Leffler  * previously setup queue.  We record the mapping from ac's
3598c42a7b7eSSam Leffler  * to h/w queues for use by ath_tx_start and also track
3599c42a7b7eSSam Leffler  * the set of h/w queues being used to optimize work in the
3600c42a7b7eSSam Leffler  * transmit interrupt handler and related routines.
3601c42a7b7eSSam Leffler  */
3602c42a7b7eSSam Leffler static int
3603c42a7b7eSSam Leffler ath_tx_setup(struct ath_softc *sc, int ac, int haltype)
3604c42a7b7eSSam Leffler {
3605c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
3606c42a7b7eSSam Leffler 	struct ath_txq *txq;
3607c42a7b7eSSam Leffler 
3608c42a7b7eSSam Leffler 	if (ac >= N(sc->sc_ac2q)) {
36096891c875SPeter Wemm 		device_printf(sc->sc_dev, "AC %u out of range, max %zu!\n",
3610c42a7b7eSSam Leffler 			ac, N(sc->sc_ac2q));
3611c42a7b7eSSam Leffler 		return 0;
3612c42a7b7eSSam Leffler 	}
3613c42a7b7eSSam Leffler 	txq = ath_txq_setup(sc, HAL_TX_QUEUE_DATA, haltype);
3614c42a7b7eSSam Leffler 	if (txq != NULL) {
3615339ccfb3SSam Leffler 		txq->axq_ac = ac;
3616c42a7b7eSSam Leffler 		sc->sc_ac2q[ac] = txq;
3617c42a7b7eSSam Leffler 		return 1;
3618c42a7b7eSSam Leffler 	} else
3619c42a7b7eSSam Leffler 		return 0;
3620c42a7b7eSSam Leffler #undef N
3621c42a7b7eSSam Leffler }
3622c42a7b7eSSam Leffler 
3623c42a7b7eSSam Leffler /*
3624c42a7b7eSSam Leffler  * Update WME parameters for a transmit queue.
3625c42a7b7eSSam Leffler  */
3626c42a7b7eSSam Leffler static int
3627c42a7b7eSSam Leffler ath_txq_update(struct ath_softc *sc, int ac)
3628c42a7b7eSSam Leffler {
3629c42a7b7eSSam Leffler #define	ATH_EXPONENT_TO_VALUE(v)	((1<<v)-1)
3630c42a7b7eSSam Leffler #define	ATH_TXOP_TO_US(v)		(v<<5)
3631b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
3632b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
3633c42a7b7eSSam Leffler 	struct ath_txq *txq = sc->sc_ac2q[ac];
3634c42a7b7eSSam Leffler 	struct wmeParams *wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
3635c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3636c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
3637c42a7b7eSSam Leffler 
3638c42a7b7eSSam Leffler 	ath_hal_gettxqueueprops(ah, txq->axq_qnum, &qi);
3639584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
364010ad9a77SSam Leffler 	if (sc->sc_tdma) {
364110ad9a77SSam Leffler 		/*
364210ad9a77SSam Leffler 		 * AIFS is zero so there's no pre-transmit wait.  The
364310ad9a77SSam Leffler 		 * burst time defines the slot duration and is configured
364409be6601SSam Leffler 		 * through net80211.  The QCU is setup to not do post-xmit
364510ad9a77SSam Leffler 		 * back off, lockout all lower-priority QCU's, and fire
364610ad9a77SSam Leffler 		 * off the DMA beacon alert timer which is setup based
364710ad9a77SSam Leffler 		 * on the slot configuration.
364810ad9a77SSam Leffler 		 */
364910ad9a77SSam Leffler 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
365010ad9a77SSam Leffler 			      | HAL_TXQ_TXERRINT_ENABLE
365110ad9a77SSam Leffler 			      | HAL_TXQ_TXURNINT_ENABLE
365210ad9a77SSam Leffler 			      | HAL_TXQ_TXEOLINT_ENABLE
365310ad9a77SSam Leffler 			      | HAL_TXQ_DBA_GATED
365410ad9a77SSam Leffler 			      | HAL_TXQ_BACKOFF_DISABLE
365510ad9a77SSam Leffler 			      | HAL_TXQ_ARB_LOCKOUT_GLOBAL
365610ad9a77SSam Leffler 			      ;
365710ad9a77SSam Leffler 		qi.tqi_aifs = 0;
365810ad9a77SSam Leffler 		/* XXX +dbaprep? */
365910ad9a77SSam Leffler 		qi.tqi_readyTime = sc->sc_tdmaslotlen;
366010ad9a77SSam Leffler 		qi.tqi_burstTime = qi.tqi_readyTime;
366110ad9a77SSam Leffler 	} else {
366210ad9a77SSam Leffler #endif
366316d4de92SAdrian Chadd 		/*
366416d4de92SAdrian Chadd 		 * XXX shouldn't this just use the default flags
366516d4de92SAdrian Chadd 		 * used in the previous queue setup?
366616d4de92SAdrian Chadd 		 */
366710ad9a77SSam Leffler 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
366810ad9a77SSam Leffler 			      | HAL_TXQ_TXERRINT_ENABLE
366910ad9a77SSam Leffler 			      | HAL_TXQ_TXDESCINT_ENABLE
367010ad9a77SSam Leffler 			      | HAL_TXQ_TXURNINT_ENABLE
36711f25c0f7SAdrian Chadd 			      | HAL_TXQ_TXEOLINT_ENABLE
367210ad9a77SSam Leffler 			      ;
3673c42a7b7eSSam Leffler 		qi.tqi_aifs = wmep->wmep_aifsn;
3674c42a7b7eSSam Leffler 		qi.tqi_cwmin = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
3675c42a7b7eSSam Leffler 		qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
367610ad9a77SSam Leffler 		qi.tqi_readyTime = 0;
3677c42a7b7eSSam Leffler 		qi.tqi_burstTime = ATH_TXOP_TO_US(wmep->wmep_txopLimit);
3678584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
367910ad9a77SSam Leffler 	}
368010ad9a77SSam Leffler #endif
368110ad9a77SSam Leffler 
368210ad9a77SSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET,
368310ad9a77SSam Leffler 	    "%s: Q%u qflags 0x%x aifs %u cwmin %u cwmax %u burstTime %u\n",
368410ad9a77SSam Leffler 	    __func__, txq->axq_qnum, qi.tqi_qflags,
368510ad9a77SSam Leffler 	    qi.tqi_aifs, qi.tqi_cwmin, qi.tqi_cwmax, qi.tqi_burstTime);
3686c42a7b7eSSam Leffler 
3687c42a7b7eSSam Leffler 	if (!ath_hal_settxqueueprops(ah, txq->axq_qnum, &qi)) {
3688b032f27cSSam Leffler 		if_printf(ifp, "unable to update hardware queue "
3689c42a7b7eSSam Leffler 			"parameters for %s traffic!\n",
3690c42a7b7eSSam Leffler 			ieee80211_wme_acnames[ac]);
3691c42a7b7eSSam Leffler 		return 0;
3692c42a7b7eSSam Leffler 	} else {
3693c42a7b7eSSam Leffler 		ath_hal_resettxqueue(ah, txq->axq_qnum); /* push to h/w */
3694c42a7b7eSSam Leffler 		return 1;
3695c42a7b7eSSam Leffler 	}
3696c42a7b7eSSam Leffler #undef ATH_TXOP_TO_US
3697c42a7b7eSSam Leffler #undef ATH_EXPONENT_TO_VALUE
3698c42a7b7eSSam Leffler }
3699c42a7b7eSSam Leffler 
3700c42a7b7eSSam Leffler /*
3701c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update WME parameters.
3702c42a7b7eSSam Leffler  */
3703a35dae8dSAdrian Chadd int
3704c42a7b7eSSam Leffler ath_wme_update(struct ieee80211com *ic)
3705c42a7b7eSSam Leffler {
3706c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
3707c42a7b7eSSam Leffler 
3708c42a7b7eSSam Leffler 	return !ath_txq_update(sc, WME_AC_BE) ||
3709c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_BK) ||
3710c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VI) ||
3711c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VO) ? EIO : 0;
3712c42a7b7eSSam Leffler }
3713c42a7b7eSSam Leffler 
3714c42a7b7eSSam Leffler /*
3715c42a7b7eSSam Leffler  * Reclaim resources for a setup queue.
3716c42a7b7eSSam Leffler  */
3717c42a7b7eSSam Leffler static void
3718c42a7b7eSSam Leffler ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
3719c42a7b7eSSam Leffler {
3720c42a7b7eSSam Leffler 
3721c42a7b7eSSam Leffler 	ath_hal_releasetxqueue(sc->sc_ah, txq->axq_qnum);
3722c42a7b7eSSam Leffler 	sc->sc_txqsetup &= ~(1<<txq->axq_qnum);
3723b837332dSAdrian Chadd 	ATH_TXQ_LOCK_DESTROY(txq);
3724c42a7b7eSSam Leffler }
3725c42a7b7eSSam Leffler 
3726c42a7b7eSSam Leffler /*
3727c42a7b7eSSam Leffler  * Reclaim all tx queue resources.
3728c42a7b7eSSam Leffler  */
3729c42a7b7eSSam Leffler static void
3730c42a7b7eSSam Leffler ath_tx_cleanup(struct ath_softc *sc)
3731c42a7b7eSSam Leffler {
3732c42a7b7eSSam Leffler 	int i;
3733c42a7b7eSSam Leffler 
3734c42a7b7eSSam Leffler 	ATH_TXBUF_LOCK_DESTROY(sc);
3735c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
3736c42a7b7eSSam Leffler 		if (ATH_TXQ_SETUP(sc, i))
3737c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, &sc->sc_txq[i]);
3738c42a7b7eSSam Leffler }
37395591b213SSam Leffler 
374099d258fdSSam Leffler /*
3741ab06fdf2SSam Leffler  * Return h/w rate index for an IEEE rate (w/o basic rate bit)
3742ab06fdf2SSam Leffler  * using the current rates in sc_rixmap.
37438b5341deSSam Leffler  */
3744b8e788a5SAdrian Chadd int
3745ab06fdf2SSam Leffler ath_tx_findrix(const struct ath_softc *sc, uint8_t rate)
37468b5341deSSam Leffler {
3747ab06fdf2SSam Leffler 	int rix = sc->sc_rixmap[rate];
3748ab06fdf2SSam Leffler 	/* NB: return lowest rix for invalid rate */
3749ab06fdf2SSam Leffler 	return (rix == 0xff ? 0 : rix);
37508b5341deSSam Leffler }
37518b5341deSSam Leffler 
37529352fb7aSAdrian Chadd static void
37539352fb7aSAdrian Chadd ath_tx_update_stats(struct ath_softc *sc, struct ath_tx_status *ts,
37549352fb7aSAdrian Chadd     struct ath_buf *bf)
37559352fb7aSAdrian Chadd {
37569352fb7aSAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
37579352fb7aSAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
37589352fb7aSAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
37599352fb7aSAdrian Chadd 	int sr, lr, pri;
37609352fb7aSAdrian Chadd 
37619352fb7aSAdrian Chadd 	if (ts->ts_status == 0) {
37629352fb7aSAdrian Chadd 		u_int8_t txant = ts->ts_antenna;
37639352fb7aSAdrian Chadd 		sc->sc_stats.ast_ant_tx[txant]++;
37649352fb7aSAdrian Chadd 		sc->sc_ant_tx[txant]++;
37659352fb7aSAdrian Chadd 		if (ts->ts_finaltsi != 0)
37669352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_altrate++;
37679352fb7aSAdrian Chadd 		pri = M_WME_GETAC(bf->bf_m);
37689352fb7aSAdrian Chadd 		if (pri >= WME_AC_VO)
37699352fb7aSAdrian Chadd 			ic->ic_wme.wme_hipri_traffic++;
3770875a9451SAdrian Chadd 		if ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0)
37719352fb7aSAdrian Chadd 			ni->ni_inact = ni->ni_inact_reload;
37729352fb7aSAdrian Chadd 	} else {
37739352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_XRETRY)
37749352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_xretries++;
37759352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_FIFO)
37769352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_fifoerr++;
37779352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_FILT)
37789352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_filtered++;
37799352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_XTXOP)
37809352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_xtxop++;
37819352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_TIMER_EXPIRED)
37829352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_timerexpired++;
37839352fb7aSAdrian Chadd 
37849352fb7aSAdrian Chadd 		if (bf->bf_m->m_flags & M_FF)
37859352fb7aSAdrian Chadd 			sc->sc_stats.ast_ff_txerr++;
37869352fb7aSAdrian Chadd 	}
37879352fb7aSAdrian Chadd 	/* XXX when is this valid? */
3788158cb431SAdrian Chadd 	if (ts->ts_flags & HAL_TX_DESC_CFG_ERR)
37899352fb7aSAdrian Chadd 		sc->sc_stats.ast_tx_desccfgerr++;
3790158cb431SAdrian Chadd 	/*
3791158cb431SAdrian Chadd 	 * This can be valid for successful frame transmission!
3792158cb431SAdrian Chadd 	 * If there's a TX FIFO underrun during aggregate transmission,
3793158cb431SAdrian Chadd 	 * the MAC will pad the rest of the aggregate with delimiters.
3794158cb431SAdrian Chadd 	 * If a BA is returned, the frame is marked as "OK" and it's up
3795158cb431SAdrian Chadd 	 * to the TX completion code to notice which frames weren't
3796158cb431SAdrian Chadd 	 * successfully transmitted.
3797158cb431SAdrian Chadd 	 */
3798158cb431SAdrian Chadd 	if (ts->ts_flags & HAL_TX_DATA_UNDERRUN)
3799158cb431SAdrian Chadd 		sc->sc_stats.ast_tx_data_underrun++;
3800158cb431SAdrian Chadd 	if (ts->ts_flags & HAL_TX_DELIM_UNDERRUN)
3801158cb431SAdrian Chadd 		sc->sc_stats.ast_tx_delim_underrun++;
38029352fb7aSAdrian Chadd 
38039352fb7aSAdrian Chadd 	sr = ts->ts_shortretry;
38049352fb7aSAdrian Chadd 	lr = ts->ts_longretry;
38059352fb7aSAdrian Chadd 	sc->sc_stats.ast_tx_shortretry += sr;
38069352fb7aSAdrian Chadd 	sc->sc_stats.ast_tx_longretry += lr;
38079352fb7aSAdrian Chadd 
38089352fb7aSAdrian Chadd }
38099352fb7aSAdrian Chadd 
38109352fb7aSAdrian Chadd /*
38119352fb7aSAdrian Chadd  * The default completion. If fail is 1, this means
38129352fb7aSAdrian Chadd  * "please don't retry the frame, and just return -1 status
38139352fb7aSAdrian Chadd  * to the net80211 stack.
38149352fb7aSAdrian Chadd  */
38159352fb7aSAdrian Chadd void
38169352fb7aSAdrian Chadd ath_tx_default_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
38179352fb7aSAdrian Chadd {
38189352fb7aSAdrian Chadd 	struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
38199352fb7aSAdrian Chadd 	int st;
38209352fb7aSAdrian Chadd 
38219352fb7aSAdrian Chadd 	if (fail == 1)
38229352fb7aSAdrian Chadd 		st = -1;
38239352fb7aSAdrian Chadd 	else
3824875a9451SAdrian Chadd 		st = ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0) ?
38259352fb7aSAdrian Chadd 		    ts->ts_status : HAL_TXERR_XRETRY;
38269352fb7aSAdrian Chadd 
3827ce597531SAdrian Chadd #if 0
38289352fb7aSAdrian Chadd 	if (bf->bf_state.bfs_dobaw)
38299352fb7aSAdrian Chadd 		device_printf(sc->sc_dev,
3830a66d5089SAdrian Chadd 		    "%s: bf %p: seqno %d: dobaw should've been cleared!\n",
3831a66d5089SAdrian Chadd 		    __func__,
3832a66d5089SAdrian Chadd 		    bf,
3833a66d5089SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
3834ce597531SAdrian Chadd #endif
38359352fb7aSAdrian Chadd 	if (bf->bf_next != NULL)
38369352fb7aSAdrian Chadd 		device_printf(sc->sc_dev,
3837a66d5089SAdrian Chadd 		    "%s: bf %p: seqno %d: bf_next not NULL!\n",
3838a66d5089SAdrian Chadd 		    __func__,
3839a66d5089SAdrian Chadd 		    bf,
3840a66d5089SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
38419352fb7aSAdrian Chadd 
38429352fb7aSAdrian Chadd 	/*
3843548a605dSAdrian Chadd 	 * Check if the node software queue is empty; if so
3844548a605dSAdrian Chadd 	 * then clear the TIM.
3845548a605dSAdrian Chadd 	 *
3846548a605dSAdrian Chadd 	 * This needs to be done before the buffer is freed as
3847548a605dSAdrian Chadd 	 * otherwise the node reference will have been released
3848548a605dSAdrian Chadd 	 * and the node may not actually exist any longer.
3849548a605dSAdrian Chadd 	 *
3850548a605dSAdrian Chadd 	 * XXX I don't like this belonging here, but it's cleaner
3851548a605dSAdrian Chadd 	 * to do it here right now then all the other places
3852548a605dSAdrian Chadd 	 * where ath_tx_default_comp() is called.
3853548a605dSAdrian Chadd 	 *
3854548a605dSAdrian Chadd 	 * XXX TODO: during drain, ensure that the callback is
3855548a605dSAdrian Chadd 	 * being called so we get a chance to update the TIM.
3856548a605dSAdrian Chadd 	 */
38574bed2b67SAdrian Chadd 	if (bf->bf_node) {
38584bed2b67SAdrian Chadd 		ATH_TX_LOCK(sc);
3859548a605dSAdrian Chadd 		ath_tx_update_tim(sc, bf->bf_node, 0);
38604bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
38614bed2b67SAdrian Chadd 	}
3862548a605dSAdrian Chadd 
3863548a605dSAdrian Chadd 	/*
38649352fb7aSAdrian Chadd 	 * Do any tx complete callback.  Note this must
38659352fb7aSAdrian Chadd 	 * be done before releasing the node reference.
38669352fb7aSAdrian Chadd 	 * This will free the mbuf, release the net80211
38679352fb7aSAdrian Chadd 	 * node and recycle the ath_buf.
38689352fb7aSAdrian Chadd 	 */
38699352fb7aSAdrian Chadd 	ath_tx_freebuf(sc, bf, st);
38709352fb7aSAdrian Chadd }
38719352fb7aSAdrian Chadd 
38729352fb7aSAdrian Chadd /*
3873eb6f0de0SAdrian Chadd  * Update rate control with the given completion status.
3874eb6f0de0SAdrian Chadd  */
3875eb6f0de0SAdrian Chadd void
3876eb6f0de0SAdrian Chadd ath_tx_update_ratectrl(struct ath_softc *sc, struct ieee80211_node *ni,
3877eb6f0de0SAdrian Chadd     struct ath_rc_series *rc, struct ath_tx_status *ts, int frmlen,
3878eb6f0de0SAdrian Chadd     int nframes, int nbad)
3879eb6f0de0SAdrian Chadd {
3880eb6f0de0SAdrian Chadd 	struct ath_node *an;
3881eb6f0de0SAdrian Chadd 
3882eb6f0de0SAdrian Chadd 	/* Only for unicast frames */
3883eb6f0de0SAdrian Chadd 	if (ni == NULL)
3884eb6f0de0SAdrian Chadd 		return;
3885eb6f0de0SAdrian Chadd 
3886eb6f0de0SAdrian Chadd 	an = ATH_NODE(ni);
3887548a605dSAdrian Chadd 	ATH_NODE_UNLOCK_ASSERT(an);
3888eb6f0de0SAdrian Chadd 
3889eb6f0de0SAdrian Chadd 	if ((ts->ts_status & HAL_TXERR_FILT) == 0) {
3890eb6f0de0SAdrian Chadd 		ATH_NODE_LOCK(an);
3891eb6f0de0SAdrian Chadd 		ath_rate_tx_complete(sc, an, rc, ts, frmlen, nframes, nbad);
3892eb6f0de0SAdrian Chadd 		ATH_NODE_UNLOCK(an);
3893eb6f0de0SAdrian Chadd 	}
3894eb6f0de0SAdrian Chadd }
3895eb6f0de0SAdrian Chadd 
3896eb6f0de0SAdrian Chadd /*
3897bad98824SAdrian Chadd  * Process the completion of the given buffer.
3898bad98824SAdrian Chadd  *
3899bad98824SAdrian Chadd  * This calls the rate control update and then the buffer completion.
3900bad98824SAdrian Chadd  * This will either free the buffer or requeue it.  In any case, the
3901bad98824SAdrian Chadd  * bf pointer should be treated as invalid after this function is called.
3902bad98824SAdrian Chadd  */
3903bad98824SAdrian Chadd void
3904bad98824SAdrian Chadd ath_tx_process_buf_completion(struct ath_softc *sc, struct ath_txq *txq,
3905bad98824SAdrian Chadd     struct ath_tx_status *ts, struct ath_buf *bf)
3906bad98824SAdrian Chadd {
3907bad98824SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
3908bad98824SAdrian Chadd 	struct ath_node *an = NULL;
3909bad98824SAdrian Chadd 
3910375307d4SAdrian Chadd 	ATH_TX_UNLOCK_ASSERT(sc);
39115e018508SAdrian Chadd 	ATH_TXQ_UNLOCK_ASSERT(txq);
3912bad98824SAdrian Chadd 
3913bad98824SAdrian Chadd 	/* If unicast frame, update general statistics */
3914bad98824SAdrian Chadd 	if (ni != NULL) {
3915bad98824SAdrian Chadd 		an = ATH_NODE(ni);
3916bad98824SAdrian Chadd 		/* update statistics */
3917bad98824SAdrian Chadd 		ath_tx_update_stats(sc, ts, bf);
3918bad98824SAdrian Chadd 	}
3919bad98824SAdrian Chadd 
3920bad98824SAdrian Chadd 	/*
3921bad98824SAdrian Chadd 	 * Call the completion handler.
3922bad98824SAdrian Chadd 	 * The completion handler is responsible for
3923bad98824SAdrian Chadd 	 * calling the rate control code.
3924bad98824SAdrian Chadd 	 *
3925bad98824SAdrian Chadd 	 * Frames with no completion handler get the
3926bad98824SAdrian Chadd 	 * rate control code called here.
3927bad98824SAdrian Chadd 	 */
3928bad98824SAdrian Chadd 	if (bf->bf_comp == NULL) {
3929bad98824SAdrian Chadd 		if ((ts->ts_status & HAL_TXERR_FILT) == 0 &&
3930bad98824SAdrian Chadd 		    (bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0) {
3931bad98824SAdrian Chadd 			/*
3932bad98824SAdrian Chadd 			 * XXX assume this isn't an aggregate
3933bad98824SAdrian Chadd 			 * frame.
3934bad98824SAdrian Chadd 			 */
3935bad98824SAdrian Chadd 			ath_tx_update_ratectrl(sc, ni,
3936bad98824SAdrian Chadd 			     bf->bf_state.bfs_rc, ts,
3937bad98824SAdrian Chadd 			    bf->bf_state.bfs_pktlen, 1,
3938bad98824SAdrian Chadd 			    (ts->ts_status == 0 ? 0 : 1));
3939bad98824SAdrian Chadd 		}
3940bad98824SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
3941bad98824SAdrian Chadd 	} else
3942bad98824SAdrian Chadd 		bf->bf_comp(sc, bf, 0);
3943bad98824SAdrian Chadd }
3944bad98824SAdrian Chadd 
3945bad98824SAdrian Chadd 
3946bad98824SAdrian Chadd 
3947bad98824SAdrian Chadd /*
3948c42a7b7eSSam Leffler  * Process completed xmit descriptors from the specified queue.
3949eb6f0de0SAdrian Chadd  * Kick the packet scheduler if needed. This can occur from this
3950eb6f0de0SAdrian Chadd  * particular task.
3951c42a7b7eSSam Leffler  */
3952788e6aa9SAdrian Chadd static int
3953788e6aa9SAdrian Chadd ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq, int dosched)
39545591b213SSam Leffler {
39555591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
39569352fb7aSAdrian Chadd 	struct ath_buf *bf;
39576edf1dc7SAdrian Chadd 	struct ath_desc *ds;
395865f9edeeSSam Leffler 	struct ath_tx_status *ts;
39595591b213SSam Leffler 	struct ieee80211_node *ni;
396053e98d5aSAdrian Chadd #ifdef	IEEE80211_SUPPORT_SUPERG
396143faa6b2SAdrian Chadd 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
396253e98d5aSAdrian Chadd #endif	/* IEEE80211_SUPPORT_SUPERG */
39639352fb7aSAdrian Chadd 	int nacked;
39645591b213SSam Leffler 	HAL_STATUS status;
39655591b213SSam Leffler 
3966c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_TX_PROC, "%s: tx queue %u head %p link %p\n",
3967c42a7b7eSSam Leffler 		__func__, txq->axq_qnum,
3968c42a7b7eSSam Leffler 		(caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
3969c42a7b7eSSam Leffler 		txq->axq_link);
397003682514SAdrian Chadd 
397103682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 4,
397203682514SAdrian Chadd 	    "ath_tx_processq: txq=%u head %p link %p depth %p",
397303682514SAdrian Chadd 	    txq->axq_qnum,
397403682514SAdrian Chadd 	    (caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
397503682514SAdrian Chadd 	    txq->axq_link,
397603682514SAdrian Chadd 	    txq->axq_depth);
397703682514SAdrian Chadd 
3978d7736e13SSam Leffler 	nacked = 0;
39795591b213SSam Leffler 	for (;;) {
3980b837332dSAdrian Chadd 		ATH_TXQ_LOCK(txq);
3981c42a7b7eSSam Leffler 		txq->axq_intrcnt = 0;	/* reset periodic desc intr count */
39826b349e5aSAdrian Chadd 		bf = TAILQ_FIRST(&txq->axq_q);
39835591b213SSam Leffler 		if (bf == NULL) {
3984b837332dSAdrian Chadd 			ATH_TXQ_UNLOCK(txq);
39855591b213SSam Leffler 			break;
39865591b213SSam Leffler 		}
39876edf1dc7SAdrian Chadd 		ds = bf->bf_lastds;	/* XXX must be setup correctly! */
398865f9edeeSSam Leffler 		ts = &bf->bf_status.ds_txstat;
398903682514SAdrian Chadd 
399065f9edeeSSam Leffler 		status = ath_hal_txprocdesc(ah, ds, ts);
3991a585a9a1SSam Leffler #ifdef ATH_DEBUG
3992c42a7b7eSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_XMIT_DESC)
39936902009eSSam Leffler 			ath_printtxbuf(sc, bf, txq->axq_qnum, 0,
39946902009eSSam Leffler 			    status == HAL_OK);
399503682514SAdrian Chadd 		else if ((sc->sc_debug & ATH_DEBUG_RESET) && (dosched == 0))
3996d6b20023SAdrian Chadd 			ath_printtxbuf(sc, bf, txq->axq_qnum, 0,
3997d6b20023SAdrian Chadd 			    status == HAL_OK);
39985591b213SSam Leffler #endif
3999bb327d28SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
4000bb327d28SAdrian Chadd 		if (if_ath_alq_checkdebug(&sc->sc_alq,
4001bb327d28SAdrian Chadd 		    ATH_ALQ_EDMA_TXSTATUS)) {
4002bb327d28SAdrian Chadd 			if_ath_alq_post(&sc->sc_alq, ATH_ALQ_EDMA_TXSTATUS,
4003bb327d28SAdrian Chadd 			sc->sc_tx_statuslen,
4004bb327d28SAdrian Chadd 			(char *) ds);
4005bb327d28SAdrian Chadd 		}
4006bb327d28SAdrian Chadd #endif
400703682514SAdrian Chadd 
40085591b213SSam Leffler 		if (status == HAL_EINPROGRESS) {
400903682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_TXCOMP, 3,
401003682514SAdrian Chadd 			    "ath_tx_processq: txq=%u, bf=%p ds=%p, HAL_EINPROGRESS",
401103682514SAdrian Chadd 			    txq->axq_qnum, bf, ds);
4012b837332dSAdrian Chadd 			ATH_TXQ_UNLOCK(txq);
40135591b213SSam Leffler 			break;
40145591b213SSam Leffler 		}
40156b349e5aSAdrian Chadd 		ATH_TXQ_REMOVE(txq, bf, bf_list);
40165e018508SAdrian Chadd 
40175e018508SAdrian Chadd 		/*
40185e018508SAdrian Chadd 		 * Sanity check.
40195e018508SAdrian Chadd 		 */
40205e018508SAdrian Chadd 		if (txq->axq_qnum != bf->bf_state.bfs_tx_queue) {
40215e018508SAdrian Chadd 			device_printf(sc->sc_dev,
40225e018508SAdrian Chadd 			    "%s: TXQ=%d: bf=%p, bfs_tx_queue=%d\n",
40235e018508SAdrian Chadd 			    __func__,
40245e018508SAdrian Chadd 			    txq->axq_qnum,
40255e018508SAdrian Chadd 			    bf,
40265e018508SAdrian Chadd 			    bf->bf_state.bfs_tx_queue);
40275e018508SAdrian Chadd 		}
40285e018508SAdrian Chadd 		if (txq->axq_qnum != bf->bf_last->bf_state.bfs_tx_queue) {
40295e018508SAdrian Chadd 			device_printf(sc->sc_dev,
40305e018508SAdrian Chadd 			    "%s: TXQ=%d: bf_last=%p, bfs_tx_queue=%d\n",
40315e018508SAdrian Chadd 			    __func__,
40325e018508SAdrian Chadd 			    txq->axq_qnum,
40335e018508SAdrian Chadd 			    bf->bf_last,
40345e018508SAdrian Chadd 			    bf->bf_last->bf_state.bfs_tx_queue);
40355e018508SAdrian Chadd 		}
40365e018508SAdrian Chadd 
40375e018508SAdrian Chadd #if 0
4038d3731e4bSAdrian Chadd 		if (txq->axq_depth > 0) {
403910ad9a77SSam Leffler 			/*
4040d3731e4bSAdrian Chadd 			 * More frames follow.  Mark the buffer busy
4041d3731e4bSAdrian Chadd 			 * so it's not re-used while the hardware may
4042d3731e4bSAdrian Chadd 			 * still re-read the link field in the descriptor.
40436edf1dc7SAdrian Chadd 			 *
4044d3731e4bSAdrian Chadd 			 * Use the last buffer in an aggregate as that
4045d3731e4bSAdrian Chadd 			 * is where the hardware may be - intermediate
4046d3731e4bSAdrian Chadd 			 * descriptors won't be "busy".
404710ad9a77SSam Leffler 			 */
40486edf1dc7SAdrian Chadd 			bf->bf_last->bf_flags |= ATH_BUF_BUSY;
4049d3731e4bSAdrian Chadd 		} else
4050d3731e4bSAdrian Chadd 			txq->axq_link = NULL;
40515e018508SAdrian Chadd #else
40525e018508SAdrian Chadd 		bf->bf_last->bf_flags |= ATH_BUF_BUSY;
40535e018508SAdrian Chadd #endif
40546edf1dc7SAdrian Chadd 		if (bf->bf_state.bfs_aggr)
40556edf1dc7SAdrian Chadd 			txq->axq_aggr_depth--;
40565591b213SSam Leffler 
40575591b213SSam Leffler 		ni = bf->bf_node;
405803682514SAdrian Chadd 
405903682514SAdrian Chadd 		ATH_KTR(sc, ATH_KTR_TXCOMP, 5,
406003682514SAdrian Chadd 		    "ath_tx_processq: txq=%u, bf=%p, ds=%p, ni=%p, ts_status=0x%08x",
406103682514SAdrian Chadd 		    txq->axq_qnum, bf, ds, ni, ts->ts_status);
4062c42a7b7eSSam Leffler 		/*
40639352fb7aSAdrian Chadd 		 * If unicast frame was ack'd update RSSI,
406484784be1SSam Leffler 		 * including the last rx time used to
406584784be1SSam Leffler 		 * workaround phantom bmiss interrupts.
4066d7736e13SSam Leffler 		 */
40679352fb7aSAdrian Chadd 		if (ni != NULL && ts->ts_status == 0 &&
4068875a9451SAdrian Chadd 		    ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0)) {
4069d7736e13SSam Leffler 			nacked++;
407084784be1SSam Leffler 			sc->sc_stats.ast_tx_rssi = ts->ts_rssi;
407184784be1SSam Leffler 			ATH_RSSI_LPF(sc->sc_halstats.ns_avgtxrssi,
407284784be1SSam Leffler 				ts->ts_rssi);
407384784be1SSam Leffler 		}
4074b837332dSAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
40759352fb7aSAdrian Chadd 
4076bad98824SAdrian Chadd 		/*
4077bad98824SAdrian Chadd 		 * Update statistics and call completion
4078bad98824SAdrian Chadd 		 */
4079bad98824SAdrian Chadd 		ath_tx_process_buf_completion(sc, txq, ts, bf);
4080548a605dSAdrian Chadd 
4081548a605dSAdrian Chadd 		/* XXX at this point, bf and ni may be totally invalid */
40825591b213SSam Leffler 	}
4083339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
408468e8e04eSSam Leffler 	/*
408568e8e04eSSam Leffler 	 * Flush fast-frame staging queue when traffic slows.
408668e8e04eSSam Leffler 	 */
408768e8e04eSSam Leffler 	if (txq->axq_depth <= 1)
408804f19fd6SSam Leffler 		ieee80211_ff_flush(ic, txq->axq_ac);
4089339ccfb3SSam Leffler #endif
4090eb6f0de0SAdrian Chadd 
409121bca442SAdrian Chadd 	/* Kick the software TXQ scheduler */
4092eb6f0de0SAdrian Chadd 	if (dosched) {
4093a40880adSAdrian Chadd 		ATH_TX_LOCK(sc);
4094a40880adSAdrian Chadd 		ath_txq_sched(sc, txq);
4095a40880adSAdrian Chadd 		ATH_TX_UNLOCK(sc);
4096eb6f0de0SAdrian Chadd 	}
4097eb6f0de0SAdrian Chadd 
409803682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
409903682514SAdrian Chadd 	    "ath_tx_processq: txq=%u: done",
410003682514SAdrian Chadd 	    txq->axq_qnum);
410103682514SAdrian Chadd 
4102d7736e13SSam Leffler 	return nacked;
4103d7736e13SSam Leffler }
4104d7736e13SSam Leffler 
41058f939e79SAdrian Chadd #define	TXQACTIVE(t, q)		( (t) & (1 << (q)))
4106c42a7b7eSSam Leffler 
4107c42a7b7eSSam Leffler /*
4108c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
4109c42a7b7eSSam Leffler  * for a single hardware transmit queue (e.g. 5210 and 5211).
4110c42a7b7eSSam Leffler  */
4111c42a7b7eSSam Leffler static void
4112c42a7b7eSSam Leffler ath_tx_proc_q0(void *arg, int npending)
4113c42a7b7eSSam Leffler {
4114c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4115fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
41168f939e79SAdrian Chadd 	uint32_t txqs;
4117c42a7b7eSSam Leffler 
4118ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4119ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
41208f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
41218f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
4122ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
41238f939e79SAdrian Chadd 
412403682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
412503682514SAdrian Chadd 	    "ath_tx_proc_q0: txqs=0x%08x", txqs);
412603682514SAdrian Chadd 
412796ff485dSAdrian Chadd 	if (TXQACTIVE(txqs, 0) && ath_tx_processq(sc, &sc->sc_txq[0], 1))
41288f939e79SAdrian Chadd 		/* XXX why is lastrx updated in tx code? */
4129d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
41308f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum))
413196ff485dSAdrian Chadd 		ath_tx_processq(sc, sc->sc_cabq, 1);
4132e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
413313f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4134e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
41352e986da5SSam Leffler 	sc->sc_wd_timer = 0;
41365591b213SSam Leffler 
41373e50ec2cSSam Leffler 	if (sc->sc_softled)
413846d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
41393e50ec2cSSam Leffler 
4140ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4141ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
4142ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
41431a85141aSAdrian Chadd 
41441a85141aSAdrian Chadd 	ath_tx_kick(sc);
41455591b213SSam Leffler }
41465591b213SSam Leffler 
41475591b213SSam Leffler /*
4148c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
4149c42a7b7eSSam Leffler  * for four hardware queues, 0-3 (e.g. 5212 w/ WME support).
41505591b213SSam Leffler  */
41515591b213SSam Leffler static void
4152c42a7b7eSSam Leffler ath_tx_proc_q0123(void *arg, int npending)
4153c42a7b7eSSam Leffler {
4154c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4155fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
4156d7736e13SSam Leffler 	int nacked;
41578f939e79SAdrian Chadd 	uint32_t txqs;
41588f939e79SAdrian Chadd 
4159ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4160ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
41618f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
41628f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
4163ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
4164c42a7b7eSSam Leffler 
416503682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
416603682514SAdrian Chadd 	    "ath_tx_proc_q0123: txqs=0x%08x", txqs);
416703682514SAdrian Chadd 
4168c42a7b7eSSam Leffler 	/*
4169c42a7b7eSSam Leffler 	 * Process each active queue.
4170c42a7b7eSSam Leffler 	 */
4171d7736e13SSam Leffler 	nacked = 0;
41728f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 0))
417396ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[0], 1);
41748f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 1))
417596ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[1], 1);
41768f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 2))
417796ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[2], 1);
41788f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 3))
417996ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[3], 1);
41808f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum))
418196ff485dSAdrian Chadd 		ath_tx_processq(sc, sc->sc_cabq, 1);
4182d7736e13SSam Leffler 	if (nacked)
4183d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4184c42a7b7eSSam Leffler 
4185e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
418613f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4187e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
41882e986da5SSam Leffler 	sc->sc_wd_timer = 0;
4189c42a7b7eSSam Leffler 
41903e50ec2cSSam Leffler 	if (sc->sc_softled)
419146d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
41923e50ec2cSSam Leffler 
4193ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4194ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
4195ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
41961a85141aSAdrian Chadd 
41971a85141aSAdrian Chadd 	ath_tx_kick(sc);
4198c42a7b7eSSam Leffler }
4199c42a7b7eSSam Leffler 
4200c42a7b7eSSam Leffler /*
4201c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt.
4202c42a7b7eSSam Leffler  */
4203c42a7b7eSSam Leffler static void
4204c42a7b7eSSam Leffler ath_tx_proc(void *arg, int npending)
4205c42a7b7eSSam Leffler {
4206c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
4207fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
4208d7736e13SSam Leffler 	int i, nacked;
42098f939e79SAdrian Chadd 	uint32_t txqs;
42108f939e79SAdrian Chadd 
4211ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4212ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
42138f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
42148f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
4215ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
4216c42a7b7eSSam Leffler 
421703682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1, "ath_tx_proc: txqs=0x%08x", txqs);
421803682514SAdrian Chadd 
4219c42a7b7eSSam Leffler 	/*
4220c42a7b7eSSam Leffler 	 * Process each active queue.
4221c42a7b7eSSam Leffler 	 */
4222d7736e13SSam Leffler 	nacked = 0;
4223c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
42248f939e79SAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i) && TXQACTIVE(txqs, i))
422596ff485dSAdrian Chadd 			nacked += ath_tx_processq(sc, &sc->sc_txq[i], 1);
4226d7736e13SSam Leffler 	if (nacked)
4227d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4228c42a7b7eSSam Leffler 
4229ef27340cSAdrian Chadd 	/* XXX check this inside of IF_LOCK? */
4230e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
423113f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4232e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
42332e986da5SSam Leffler 	sc->sc_wd_timer = 0;
4234c42a7b7eSSam Leffler 
42353e50ec2cSSam Leffler 	if (sc->sc_softled)
423646d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
42373e50ec2cSSam Leffler 
4238ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4239ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
4240ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
42411a85141aSAdrian Chadd 
42421a85141aSAdrian Chadd 	ath_tx_kick(sc);
4243c42a7b7eSSam Leffler }
424416d4de92SAdrian Chadd #undef	TXQACTIVE
4245c42a7b7eSSam Leffler 
42469352fb7aSAdrian Chadd /*
424703e9308fSAdrian Chadd  * Deferred processing of TXQ rescheduling.
424803e9308fSAdrian Chadd  */
424903e9308fSAdrian Chadd static void
425003e9308fSAdrian Chadd ath_txq_sched_tasklet(void *arg, int npending)
425103e9308fSAdrian Chadd {
425203e9308fSAdrian Chadd 	struct ath_softc *sc = arg;
425303e9308fSAdrian Chadd 	int i;
425403e9308fSAdrian Chadd 
425503e9308fSAdrian Chadd 	/* XXX is skipping ok? */
425603e9308fSAdrian Chadd 	ATH_PCU_LOCK(sc);
425703e9308fSAdrian Chadd #if 0
425803e9308fSAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
425903e9308fSAdrian Chadd 		device_printf(sc->sc_dev,
426003e9308fSAdrian Chadd 		    "%s: sc_inreset_cnt > 0; skipping\n", __func__);
426103e9308fSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
426203e9308fSAdrian Chadd 		return;
426303e9308fSAdrian Chadd 	}
426403e9308fSAdrian Chadd #endif
426503e9308fSAdrian Chadd 	sc->sc_txproc_cnt++;
426603e9308fSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
426703e9308fSAdrian Chadd 
4268375307d4SAdrian Chadd 	ATH_TX_LOCK(sc);
426903e9308fSAdrian Chadd 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
4270b5a9dfd5SAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i)) {
427103e9308fSAdrian Chadd 			ath_txq_sched(sc, &sc->sc_txq[i]);
4272b5a9dfd5SAdrian Chadd 		}
427303e9308fSAdrian Chadd 	}
4274375307d4SAdrian Chadd 	ATH_TX_UNLOCK(sc);
427503e9308fSAdrian Chadd 
427603e9308fSAdrian Chadd 	ATH_PCU_LOCK(sc);
427703e9308fSAdrian Chadd 	sc->sc_txproc_cnt--;
427803e9308fSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
427903e9308fSAdrian Chadd }
428003e9308fSAdrian Chadd 
4281e1a50456SAdrian Chadd void
4282e1a50456SAdrian Chadd ath_returnbuf_tail(struct ath_softc *sc, struct ath_buf *bf)
4283e1a50456SAdrian Chadd {
4284e1a50456SAdrian Chadd 
4285e1a50456SAdrian Chadd 	ATH_TXBUF_LOCK_ASSERT(sc);
4286e1a50456SAdrian Chadd 
4287af33d486SAdrian Chadd 	if (bf->bf_flags & ATH_BUF_MGMT)
4288af33d486SAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_txbuf_mgmt, bf, bf_list);
428923ced6c1SAdrian Chadd 	else {
4290e1a50456SAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
429123ced6c1SAdrian Chadd 		sc->sc_txbuf_cnt++;
429223ced6c1SAdrian Chadd 		if (sc->sc_txbuf_cnt > ath_txbuf) {
429323ced6c1SAdrian Chadd 			device_printf(sc->sc_dev,
429423ced6c1SAdrian Chadd 			    "%s: sc_txbuf_cnt > %d?\n",
429523ced6c1SAdrian Chadd 			    __func__,
429623ced6c1SAdrian Chadd 			    ath_txbuf);
429723ced6c1SAdrian Chadd 			sc->sc_txbuf_cnt = ath_txbuf;
429823ced6c1SAdrian Chadd 		}
429923ced6c1SAdrian Chadd 	}
4300e1a50456SAdrian Chadd }
4301e1a50456SAdrian Chadd 
4302e1a50456SAdrian Chadd void
4303e1a50456SAdrian Chadd ath_returnbuf_head(struct ath_softc *sc, struct ath_buf *bf)
4304e1a50456SAdrian Chadd {
4305e1a50456SAdrian Chadd 
4306e1a50456SAdrian Chadd 	ATH_TXBUF_LOCK_ASSERT(sc);
4307e1a50456SAdrian Chadd 
4308af33d486SAdrian Chadd 	if (bf->bf_flags & ATH_BUF_MGMT)
4309af33d486SAdrian Chadd 		TAILQ_INSERT_HEAD(&sc->sc_txbuf_mgmt, bf, bf_list);
431023ced6c1SAdrian Chadd 	else {
4311e1a50456SAdrian Chadd 		TAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
431223ced6c1SAdrian Chadd 		sc->sc_txbuf_cnt++;
431323ced6c1SAdrian Chadd 		if (sc->sc_txbuf_cnt > ATH_TXBUF) {
431423ced6c1SAdrian Chadd 			device_printf(sc->sc_dev,
431523ced6c1SAdrian Chadd 			    "%s: sc_txbuf_cnt > %d?\n",
431623ced6c1SAdrian Chadd 			    __func__,
431723ced6c1SAdrian Chadd 			    ATH_TXBUF);
431823ced6c1SAdrian Chadd 			sc->sc_txbuf_cnt = ATH_TXBUF;
431923ced6c1SAdrian Chadd 		}
432023ced6c1SAdrian Chadd 	}
4321e1a50456SAdrian Chadd }
4322e1a50456SAdrian Chadd 
432303e9308fSAdrian Chadd /*
4324629ce218SAdrian Chadd  * Free the holding buffer if it exists
4325629ce218SAdrian Chadd  */
43263feffbd7SAdrian Chadd void
4327629ce218SAdrian Chadd ath_txq_freeholdingbuf(struct ath_softc *sc, struct ath_txq *txq)
4328629ce218SAdrian Chadd {
43295e018508SAdrian Chadd 	ATH_TXBUF_UNLOCK_ASSERT(sc);
43305e018508SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
4331629ce218SAdrian Chadd 
4332629ce218SAdrian Chadd 	if (txq->axq_holdingbf == NULL)
4333629ce218SAdrian Chadd 		return;
4334629ce218SAdrian Chadd 
4335629ce218SAdrian Chadd 	txq->axq_holdingbf->bf_flags &= ~ATH_BUF_BUSY;
43365e018508SAdrian Chadd 
43375e018508SAdrian Chadd 	ATH_TXBUF_LOCK(sc);
4338629ce218SAdrian Chadd 	ath_returnbuf_tail(sc, txq->axq_holdingbf);
43395e018508SAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
43405e018508SAdrian Chadd 
4341629ce218SAdrian Chadd 	txq->axq_holdingbf = NULL;
4342629ce218SAdrian Chadd }
4343629ce218SAdrian Chadd 
4344629ce218SAdrian Chadd /*
4345629ce218SAdrian Chadd  * Add this buffer to the holding queue, freeing the previous
4346629ce218SAdrian Chadd  * one if it exists.
4347629ce218SAdrian Chadd  */
4348629ce218SAdrian Chadd static void
4349629ce218SAdrian Chadd ath_txq_addholdingbuf(struct ath_softc *sc, struct ath_buf *bf)
4350629ce218SAdrian Chadd {
4351629ce218SAdrian Chadd 	struct ath_txq *txq;
4352629ce218SAdrian Chadd 
43535e018508SAdrian Chadd 	txq = &sc->sc_txq[bf->bf_state.bfs_tx_queue];
43545e018508SAdrian Chadd 
43555e018508SAdrian Chadd 	ATH_TXBUF_UNLOCK_ASSERT(sc);
43565e018508SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
43575f2f0e61SAdrian Chadd 
4358629ce218SAdrian Chadd 	/* XXX assert ATH_BUF_BUSY is set */
4359629ce218SAdrian Chadd 
4360629ce218SAdrian Chadd 	/* XXX assert the tx queue is under the max number */
4361629ce218SAdrian Chadd 	if (bf->bf_state.bfs_tx_queue > HAL_NUM_TX_QUEUES) {
4362629ce218SAdrian Chadd 		device_printf(sc->sc_dev, "%s: bf=%p: invalid tx queue (%d)\n",
4363629ce218SAdrian Chadd 		    __func__,
4364629ce218SAdrian Chadd 		    bf,
4365629ce218SAdrian Chadd 		    bf->bf_state.bfs_tx_queue);
4366629ce218SAdrian Chadd 		bf->bf_flags &= ~ATH_BUF_BUSY;
4367629ce218SAdrian Chadd 		ath_returnbuf_tail(sc, bf);
4368629ce218SAdrian Chadd 		return;
4369629ce218SAdrian Chadd 	}
4370629ce218SAdrian Chadd 	ath_txq_freeholdingbuf(sc, txq);
4371629ce218SAdrian Chadd 	txq->axq_holdingbf = bf;
4372629ce218SAdrian Chadd }
4373629ce218SAdrian Chadd 
4374629ce218SAdrian Chadd /*
43759352fb7aSAdrian Chadd  * Return a buffer to the pool and update the 'busy' flag on the
43769352fb7aSAdrian Chadd  * previous 'tail' entry.
43779352fb7aSAdrian Chadd  *
43789352fb7aSAdrian Chadd  * This _must_ only be called when the buffer is involved in a completed
43799352fb7aSAdrian Chadd  * TX. The logic is that if it was part of an active TX, the previous
43809352fb7aSAdrian Chadd  * buffer on the list is now not involved in a halted TX DMA queue, waiting
43819352fb7aSAdrian Chadd  * for restart (eg for TDMA.)
43829352fb7aSAdrian Chadd  *
43839352fb7aSAdrian Chadd  * The caller must free the mbuf and recycle the node reference.
43845e018508SAdrian Chadd  *
43855e018508SAdrian Chadd  * XXX This method of handling busy / holding buffers is insanely stupid.
43865e018508SAdrian Chadd  * It requires bf_state.bfs_tx_queue to be correctly assigned.  It would
43875e018508SAdrian Chadd  * be much nicer if buffers in the processq() methods would instead be
43885e018508SAdrian Chadd  * always completed there (pushed onto a txq or ath_bufhead) so we knew
43895e018508SAdrian Chadd  * exactly what hardware queue they came from in the first place.
43909352fb7aSAdrian Chadd  */
43919352fb7aSAdrian Chadd void
43929352fb7aSAdrian Chadd ath_freebuf(struct ath_softc *sc, struct ath_buf *bf)
43939352fb7aSAdrian Chadd {
43945e018508SAdrian Chadd 	struct ath_txq *txq;
43955e018508SAdrian Chadd 
43965e018508SAdrian Chadd 	txq = &sc->sc_txq[bf->bf_state.bfs_tx_queue];
43975e018508SAdrian Chadd 
43989352fb7aSAdrian Chadd 	KASSERT((bf->bf_node == NULL), ("%s: bf->bf_node != NULL\n", __func__));
43999352fb7aSAdrian Chadd 	KASSERT((bf->bf_m == NULL), ("%s: bf->bf_m != NULL\n", __func__));
44009352fb7aSAdrian Chadd 
4401629ce218SAdrian Chadd 	/*
44025e018508SAdrian Chadd 	 * If this buffer is busy, push it onto the holding queue.
4403629ce218SAdrian Chadd 	 */
4404629ce218SAdrian Chadd 	if (bf->bf_flags & ATH_BUF_BUSY) {
44055e018508SAdrian Chadd 		ATH_TXQ_LOCK(txq);
4406629ce218SAdrian Chadd 		ath_txq_addholdingbuf(sc, bf);
44075e018508SAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
4408629ce218SAdrian Chadd 		return;
4409629ce218SAdrian Chadd 	}
4410629ce218SAdrian Chadd 
4411629ce218SAdrian Chadd 	/*
4412629ce218SAdrian Chadd 	 * Not a busy buffer, so free normally
4413629ce218SAdrian Chadd 	 */
44149352fb7aSAdrian Chadd 	ATH_TXBUF_LOCK(sc);
4415e1a50456SAdrian Chadd 	ath_returnbuf_tail(sc, bf);
44169352fb7aSAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
44179352fb7aSAdrian Chadd }
44189352fb7aSAdrian Chadd 
44199352fb7aSAdrian Chadd /*
44209352fb7aSAdrian Chadd  * This is currently used by ath_tx_draintxq() and
44219352fb7aSAdrian Chadd  * ath_tx_tid_free_pkts().
44229352fb7aSAdrian Chadd  *
44239352fb7aSAdrian Chadd  * It recycles a single ath_buf.
44249352fb7aSAdrian Chadd  */
44259352fb7aSAdrian Chadd void
44269352fb7aSAdrian Chadd ath_tx_freebuf(struct ath_softc *sc, struct ath_buf *bf, int status)
44279352fb7aSAdrian Chadd {
44289352fb7aSAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
44299352fb7aSAdrian Chadd 	struct mbuf *m0 = bf->bf_m;
44309352fb7aSAdrian Chadd 
44313f3a5dbdSAdrian Chadd 	/*
44323f3a5dbdSAdrian Chadd 	 * Make sure that we only sync/unload if there's an mbuf.
44333f3a5dbdSAdrian Chadd 	 * If not (eg we cloned a buffer), the unload will have already
44343f3a5dbdSAdrian Chadd 	 * occured.
44353f3a5dbdSAdrian Chadd 	 */
44363f3a5dbdSAdrian Chadd 	if (bf->bf_m != NULL) {
44373f3a5dbdSAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
44383f3a5dbdSAdrian Chadd 		    BUS_DMASYNC_POSTWRITE);
44393f3a5dbdSAdrian Chadd 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
44403f3a5dbdSAdrian Chadd 	}
44413f3a5dbdSAdrian Chadd 
44429352fb7aSAdrian Chadd 	bf->bf_node = NULL;
44439352fb7aSAdrian Chadd 	bf->bf_m = NULL;
44449352fb7aSAdrian Chadd 
44459352fb7aSAdrian Chadd 	/* Free the buffer, it's not needed any longer */
44469352fb7aSAdrian Chadd 	ath_freebuf(sc, bf);
44479352fb7aSAdrian Chadd 
44489352fb7aSAdrian Chadd 	if (ni != NULL) {
44499352fb7aSAdrian Chadd 		/*
44509352fb7aSAdrian Chadd 		 * Do any callback and reclaim the node reference.
44519352fb7aSAdrian Chadd 		 */
44529352fb7aSAdrian Chadd 		if (m0->m_flags & M_TXCB)
44539352fb7aSAdrian Chadd 			ieee80211_process_callback(ni, m0, status);
44549352fb7aSAdrian Chadd 		ieee80211_free_node(ni);
44559352fb7aSAdrian Chadd 	}
44569352fb7aSAdrian Chadd 
44573f3a5dbdSAdrian Chadd 	/* Finally, we don't need this mbuf any longer */
44583f3a5dbdSAdrian Chadd 	m_freem(m0);
44599352fb7aSAdrian Chadd }
44609352fb7aSAdrian Chadd 
44613feffbd7SAdrian Chadd static struct ath_buf *
44623feffbd7SAdrian Chadd ath_tx_draintxq_get_one(struct ath_softc *sc, struct ath_txq *txq)
44633feffbd7SAdrian Chadd {
44643feffbd7SAdrian Chadd 	struct ath_buf *bf;
44653feffbd7SAdrian Chadd 
44663feffbd7SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(txq);
44673feffbd7SAdrian Chadd 
44683feffbd7SAdrian Chadd 	/*
44693feffbd7SAdrian Chadd 	 * Drain the FIFO queue first, then if it's
44703feffbd7SAdrian Chadd 	 * empty, move to the normal frame queue.
44713feffbd7SAdrian Chadd 	 */
44723feffbd7SAdrian Chadd 	bf = TAILQ_FIRST(&txq->fifo.axq_q);
44733feffbd7SAdrian Chadd 	if (bf != NULL) {
44743feffbd7SAdrian Chadd 		/*
44753feffbd7SAdrian Chadd 		 * Is it the last buffer in this set?
44763feffbd7SAdrian Chadd 		 * Decrement the FIFO counter.
44773feffbd7SAdrian Chadd 		 */
44783feffbd7SAdrian Chadd 		if (bf->bf_flags & ATH_BUF_FIFOEND) {
44793feffbd7SAdrian Chadd 			if (txq->axq_fifo_depth == 0) {
44803feffbd7SAdrian Chadd 				device_printf(sc->sc_dev,
44813feffbd7SAdrian Chadd 				    "%s: Q%d: fifo_depth=0, fifo.axq_depth=%d?\n",
44823feffbd7SAdrian Chadd 				    __func__,
44833feffbd7SAdrian Chadd 				    txq->axq_qnum,
44843feffbd7SAdrian Chadd 				    txq->fifo.axq_depth);
44853feffbd7SAdrian Chadd 			} else
44863feffbd7SAdrian Chadd 				txq->axq_fifo_depth--;
44873feffbd7SAdrian Chadd 		}
44883feffbd7SAdrian Chadd 		ATH_TXQ_REMOVE(&txq->fifo, bf, bf_list);
44893feffbd7SAdrian Chadd 		return (bf);
44903feffbd7SAdrian Chadd 	}
44913feffbd7SAdrian Chadd 
44923feffbd7SAdrian Chadd 	/*
44933feffbd7SAdrian Chadd 	 * Debugging!
44943feffbd7SAdrian Chadd 	 */
44953feffbd7SAdrian Chadd 	if (txq->axq_fifo_depth != 0 || txq->fifo.axq_depth != 0) {
44963feffbd7SAdrian Chadd 		device_printf(sc->sc_dev,
44973feffbd7SAdrian Chadd 		    "%s: Q%d: fifo_depth=%d, fifo.axq_depth=%d\n",
44983feffbd7SAdrian Chadd 		    __func__,
44993feffbd7SAdrian Chadd 		    txq->axq_qnum,
45003feffbd7SAdrian Chadd 		    txq->axq_fifo_depth,
45013feffbd7SAdrian Chadd 		    txq->fifo.axq_depth);
45023feffbd7SAdrian Chadd 	}
45033feffbd7SAdrian Chadd 
45043feffbd7SAdrian Chadd 	/*
45053feffbd7SAdrian Chadd 	 * Now drain the pending queue.
45063feffbd7SAdrian Chadd 	 */
45073feffbd7SAdrian Chadd 	bf = TAILQ_FIRST(&txq->axq_q);
45083feffbd7SAdrian Chadd 	if (bf == NULL) {
45093feffbd7SAdrian Chadd 		txq->axq_link = NULL;
45103feffbd7SAdrian Chadd 		return (NULL);
45113feffbd7SAdrian Chadd 	}
45123feffbd7SAdrian Chadd 	ATH_TXQ_REMOVE(txq, bf, bf_list);
45133feffbd7SAdrian Chadd 	return (bf);
45143feffbd7SAdrian Chadd }
45153feffbd7SAdrian Chadd 
45169352fb7aSAdrian Chadd void
45171762ec94SAdrian Chadd ath_tx_draintxq(struct ath_softc *sc, struct ath_txq *txq)
45185591b213SSam Leffler {
4519a585a9a1SSam Leffler #ifdef ATH_DEBUG
45205591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4521d2f6ed15SSam Leffler #endif
45225591b213SSam Leffler 	struct ath_buf *bf;
45237a4c5ed9SSam Leffler 	u_int ix;
45245591b213SSam Leffler 
4525c42a7b7eSSam Leffler 	/*
4526c42a7b7eSSam Leffler 	 * NB: this assumes output has been stopped and
45275d61b5e8SSam Leffler 	 *     we do not need to block ath_tx_proc
4528c42a7b7eSSam Leffler 	 */
45297a4c5ed9SSam Leffler 	for (ix = 0;; ix++) {
4530b837332dSAdrian Chadd 		ATH_TXQ_LOCK(txq);
45313feffbd7SAdrian Chadd 		bf = ath_tx_draintxq_get_one(sc, txq);
45325591b213SSam Leffler 		if (bf == NULL) {
4533b837332dSAdrian Chadd 			ATH_TXQ_UNLOCK(txq);
45345591b213SSam Leffler 			break;
45355591b213SSam Leffler 		}
45366edf1dc7SAdrian Chadd 		if (bf->bf_state.bfs_aggr)
45376edf1dc7SAdrian Chadd 			txq->axq_aggr_depth--;
4538a585a9a1SSam Leffler #ifdef ATH_DEBUG
45394a3ac3fcSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_RESET) {
4540b032f27cSSam Leffler 			struct ieee80211com *ic = sc->sc_ifp->if_l2com;
45411762ec94SAdrian Chadd 			int status = 0;
4542b032f27cSSam Leffler 
45431762ec94SAdrian Chadd 			/*
45441762ec94SAdrian Chadd 			 * EDMA operation has a TX completion FIFO
45451762ec94SAdrian Chadd 			 * separate from the TX descriptor, so this
45461762ec94SAdrian Chadd 			 * method of checking the "completion" status
45471762ec94SAdrian Chadd 			 * is wrong.
45481762ec94SAdrian Chadd 			 */
45491762ec94SAdrian Chadd 			if (! sc->sc_isedma) {
45501762ec94SAdrian Chadd 				status = (ath_hal_txprocdesc(ah,
45511762ec94SAdrian Chadd 				    bf->bf_lastds,
455265f9edeeSSam Leffler 				    &bf->bf_status.ds_txstat) == HAL_OK);
45531762ec94SAdrian Chadd 			}
45541762ec94SAdrian Chadd 			ath_printtxbuf(sc, bf, txq->axq_qnum, ix, status);
4555e40b6ab1SSam Leffler 			ieee80211_dump_pkt(ic, mtod(bf->bf_m, const uint8_t *),
45564a3ac3fcSSam Leffler 			    bf->bf_m->m_len, 0, -1);
45574a3ac3fcSSam Leffler 		}
4558a585a9a1SSam Leffler #endif /* ATH_DEBUG */
455923428eafSSam Leffler 		/*
45609352fb7aSAdrian Chadd 		 * Since we're now doing magic in the completion
45619352fb7aSAdrian Chadd 		 * functions, we -must- call it for aggregation
45629352fb7aSAdrian Chadd 		 * destinations or BAW tracking will get upset.
456323428eafSSam Leffler 		 */
45649352fb7aSAdrian Chadd 		/*
45659352fb7aSAdrian Chadd 		 * Clear ATH_BUF_BUSY; the completion handler
45669352fb7aSAdrian Chadd 		 * will free the buffer.
45679352fb7aSAdrian Chadd 		 */
4568b837332dSAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
456910ad9a77SSam Leffler 		bf->bf_flags &= ~ATH_BUF_BUSY;
45709352fb7aSAdrian Chadd 		if (bf->bf_comp)
45719352fb7aSAdrian Chadd 			bf->bf_comp(sc, bf, 1);
45729352fb7aSAdrian Chadd 		else
45739352fb7aSAdrian Chadd 			ath_tx_default_comp(sc, bf, 1);
45745591b213SSam Leffler 	}
45759352fb7aSAdrian Chadd 
4576eb6f0de0SAdrian Chadd 	/*
4577629ce218SAdrian Chadd 	 * Free the holding buffer if it exists
4578629ce218SAdrian Chadd 	 */
45795e018508SAdrian Chadd 	ATH_TXQ_LOCK(txq);
4580629ce218SAdrian Chadd 	ath_txq_freeholdingbuf(sc, txq);
45815e018508SAdrian Chadd 	ATH_TXQ_UNLOCK(txq);
4582629ce218SAdrian Chadd 
4583629ce218SAdrian Chadd 	/*
4584eb6f0de0SAdrian Chadd 	 * Drain software queued frames which are on
4585eb6f0de0SAdrian Chadd 	 * active TIDs.
4586eb6f0de0SAdrian Chadd 	 */
4587eb6f0de0SAdrian Chadd 	ath_tx_txq_drain(sc, txq);
4588c42a7b7eSSam Leffler }
4589c42a7b7eSSam Leffler 
4590c42a7b7eSSam Leffler static void
4591c42a7b7eSSam Leffler ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
4592c42a7b7eSSam Leffler {
4593c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4594c42a7b7eSSam Leffler 
45959d2a962bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET,
45968d060542SAdrian Chadd 	    "%s: tx queue [%u] %p, active=%d, hwpending=%d, flags 0x%08x, link %p\n",
45979d2a962bSAdrian Chadd 	    __func__,
45989d2a962bSAdrian Chadd 	    txq->axq_qnum,
45996891c875SPeter Wemm 	    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, txq->axq_qnum),
46008d060542SAdrian Chadd 	    (int) (!! ath_hal_txqenabled(ah, txq->axq_qnum)),
46018d060542SAdrian Chadd 	    (int) ath_hal_numtxpending(ah, txq->axq_qnum),
46029d2a962bSAdrian Chadd 	    txq->axq_flags,
46036891c875SPeter Wemm 	    txq->axq_link);
46044a3ac3fcSSam Leffler 	(void) ath_hal_stoptxdma(ah, txq->axq_qnum);
4605c42a7b7eSSam Leffler }
4606c42a7b7eSSam Leffler 
4607bad98824SAdrian Chadd int
46082d433424SAdrian Chadd ath_stoptxdma(struct ath_softc *sc)
4609c42a7b7eSSam Leffler {
4610c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4611c42a7b7eSSam Leffler 	int i;
4612c42a7b7eSSam Leffler 
4613c42a7b7eSSam Leffler 	/* XXX return value */
46142d433424SAdrian Chadd 	if (sc->sc_invalid)
46152d433424SAdrian Chadd 		return 0;
46162d433424SAdrian Chadd 
4617c42a7b7eSSam Leffler 	if (!sc->sc_invalid) {
4618c42a7b7eSSam Leffler 		/* don't touch the hardware if marked invalid */
46194a3ac3fcSSam Leffler 		DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n",
46204a3ac3fcSSam Leffler 		    __func__, sc->sc_bhalq,
46214a3ac3fcSSam Leffler 		    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq),
46224a3ac3fcSSam Leffler 		    NULL);
4623c42a7b7eSSam Leffler 		(void) ath_hal_stoptxdma(ah, sc->sc_bhalq);
4624c42a7b7eSSam Leffler 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
4625c42a7b7eSSam Leffler 			if (ATH_TXQ_SETUP(sc, i))
4626c42a7b7eSSam Leffler 				ath_tx_stopdma(sc, &sc->sc_txq[i]);
4627c42a7b7eSSam Leffler 	}
46282d433424SAdrian Chadd 
46292d433424SAdrian Chadd 	return 1;
46302d433424SAdrian Chadd }
46312d433424SAdrian Chadd 
463207187d11SAdrian Chadd #ifdef	ATH_DEBUG
4633ed261a61SAdrian Chadd static void
4634ed261a61SAdrian Chadd ath_tx_dump(struct ath_softc *sc, struct ath_txq *txq)
4635ed261a61SAdrian Chadd {
4636ed261a61SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
4637ed261a61SAdrian Chadd 	struct ath_buf *bf;
4638ed261a61SAdrian Chadd 	int i = 0;
4639ed261a61SAdrian Chadd 
4640ed261a61SAdrian Chadd 	if (! (sc->sc_debug & ATH_DEBUG_RESET))
4641ed261a61SAdrian Chadd 		return;
4642ed261a61SAdrian Chadd 
4643ed261a61SAdrian Chadd 	device_printf(sc->sc_dev, "%s: Q%d: begin\n",
4644ed261a61SAdrian Chadd 	    __func__, txq->axq_qnum);
4645ed261a61SAdrian Chadd 	TAILQ_FOREACH(bf, &txq->axq_q, bf_list) {
4646ed261a61SAdrian Chadd 		ath_printtxbuf(sc, bf, txq->axq_qnum, i,
4647ed261a61SAdrian Chadd 			ath_hal_txprocdesc(ah, bf->bf_lastds,
4648ed261a61SAdrian Chadd 			    &bf->bf_status.ds_txstat) == HAL_OK);
4649ed261a61SAdrian Chadd 		i++;
4650ed261a61SAdrian Chadd 	}
4651ed261a61SAdrian Chadd 	device_printf(sc->sc_dev, "%s: Q%d: end\n",
4652ed261a61SAdrian Chadd 	    __func__, txq->axq_qnum);
4653ed261a61SAdrian Chadd }
465407187d11SAdrian Chadd #endif /* ATH_DEBUG */
4655ed261a61SAdrian Chadd 
46562d433424SAdrian Chadd /*
46572d433424SAdrian Chadd  * Drain the transmit queues and reclaim resources.
46582d433424SAdrian Chadd  */
4659788e6aa9SAdrian Chadd void
4660788e6aa9SAdrian Chadd ath_legacy_tx_drain(struct ath_softc *sc, ATH_RESET_TYPE reset_type)
46612d433424SAdrian Chadd {
46622d433424SAdrian Chadd #ifdef	ATH_DEBUG
46632d433424SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
46642d433424SAdrian Chadd #endif
46652d433424SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
46662d433424SAdrian Chadd 	int i;
46672d433424SAdrian Chadd 
46682d433424SAdrian Chadd 	(void) ath_stoptxdma(sc);
46692d433424SAdrian Chadd 
4670ed261a61SAdrian Chadd 	/*
4671ed261a61SAdrian Chadd 	 * Dump the queue contents
4672ed261a61SAdrian Chadd 	 */
4673ef27340cSAdrian Chadd 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
4674ef27340cSAdrian Chadd 		/*
4675ef27340cSAdrian Chadd 		 * XXX TODO: should we just handle the completed TX frames
4676ef27340cSAdrian Chadd 		 * here, whether or not the reset is a full one or not?
4677ef27340cSAdrian Chadd 		 */
4678ef27340cSAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i)) {
467907187d11SAdrian Chadd #ifdef	ATH_DEBUG
4680ed261a61SAdrian Chadd 			if (sc->sc_debug & ATH_DEBUG_RESET)
4681ed261a61SAdrian Chadd 				ath_tx_dump(sc, &sc->sc_txq[i]);
468207187d11SAdrian Chadd #endif	/* ATH_DEBUG */
46838328d6e4SAdrian Chadd 			if (reset_type == ATH_RESET_NOLOSS) {
4684ef27340cSAdrian Chadd 				ath_tx_processq(sc, &sc->sc_txq[i], 0);
46858328d6e4SAdrian Chadd 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
46868328d6e4SAdrian Chadd 				/*
46878328d6e4SAdrian Chadd 				 * Free the holding buffer; DMA is now
46888328d6e4SAdrian Chadd 				 * stopped.
46898328d6e4SAdrian Chadd 				 */
46908328d6e4SAdrian Chadd 				ath_txq_freeholdingbuf(sc, &sc->sc_txq[i]);
46918328d6e4SAdrian Chadd 				/*
46928328d6e4SAdrian Chadd 				 * Reset the link pointer to NULL; there's
46938328d6e4SAdrian Chadd 				 * no frames to chain DMA to.
46948328d6e4SAdrian Chadd 				 */
46958328d6e4SAdrian Chadd 				sc->sc_txq[i].axq_link = NULL;
46968328d6e4SAdrian Chadd 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
46978328d6e4SAdrian Chadd 			} else
4698c42a7b7eSSam Leffler 				ath_tx_draintxq(sc, &sc->sc_txq[i]);
4699ef27340cSAdrian Chadd 		}
4700ef27340cSAdrian Chadd 	}
47014a3ac3fcSSam Leffler #ifdef ATH_DEBUG
47024a3ac3fcSSam Leffler 	if (sc->sc_debug & ATH_DEBUG_RESET) {
47036b349e5aSAdrian Chadd 		struct ath_buf *bf = TAILQ_FIRST(&sc->sc_bbuf);
47044a3ac3fcSSam Leffler 		if (bf != NULL && bf->bf_m != NULL) {
47056902009eSSam Leffler 			ath_printtxbuf(sc, bf, sc->sc_bhalq, 0,
47066edf1dc7SAdrian Chadd 				ath_hal_txprocdesc(ah, bf->bf_lastds,
470765f9edeeSSam Leffler 				    &bf->bf_status.ds_txstat) == HAL_OK);
4708e40b6ab1SSam Leffler 			ieee80211_dump_pkt(ifp->if_l2com,
4709e40b6ab1SSam Leffler 			    mtod(bf->bf_m, const uint8_t *), bf->bf_m->m_len,
4710e40b6ab1SSam Leffler 			    0, -1);
47114a3ac3fcSSam Leffler 		}
47124a3ac3fcSSam Leffler 	}
47134a3ac3fcSSam Leffler #endif /* ATH_DEBUG */
4714e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
471513f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4716e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
47172e986da5SSam Leffler 	sc->sc_wd_timer = 0;
47185591b213SSam Leffler }
47195591b213SSam Leffler 
47205591b213SSam Leffler /*
4721c42a7b7eSSam Leffler  * Update internal state after a channel change.
4722c42a7b7eSSam Leffler  */
4723c42a7b7eSSam Leffler static void
4724c42a7b7eSSam Leffler ath_chan_change(struct ath_softc *sc, struct ieee80211_channel *chan)
4725c42a7b7eSSam Leffler {
4726c42a7b7eSSam Leffler 	enum ieee80211_phymode mode;
4727c42a7b7eSSam Leffler 
4728c42a7b7eSSam Leffler 	/*
4729c42a7b7eSSam Leffler 	 * Change channels and update the h/w rate map
4730c42a7b7eSSam Leffler 	 * if we're switching; e.g. 11a to 11b/g.
4731c42a7b7eSSam Leffler 	 */
473268e8e04eSSam Leffler 	mode = ieee80211_chan2mode(chan);
4733c42a7b7eSSam Leffler 	if (mode != sc->sc_curmode)
4734c42a7b7eSSam Leffler 		ath_setcurmode(sc, mode);
473559efa8b5SSam Leffler 	sc->sc_curchan = chan;
4736c42a7b7eSSam Leffler }
4737c42a7b7eSSam Leffler 
4738c42a7b7eSSam Leffler /*
47395591b213SSam Leffler  * Set/change channels.  If the channel is really being changed,
47404fa8d4efSDaniel Eischen  * it's done by resetting the chip.  To accomplish this we must
47415591b213SSam Leffler  * first cleanup any pending DMA, then restart stuff after a la
47425591b213SSam Leffler  * ath_init.
47435591b213SSam Leffler  */
47445591b213SSam Leffler static int
47455591b213SSam Leffler ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan)
47465591b213SSam Leffler {
4747b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
4748b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
47495591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4750ef27340cSAdrian Chadd 	int ret = 0;
4751ef27340cSAdrian Chadd 
4752ef27340cSAdrian Chadd 	/* Treat this as an interface reset */
4753d52f7132SAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
4754d52f7132SAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
4755d52f7132SAdrian Chadd 
4756d52f7132SAdrian Chadd 	/* (Try to) stop TX/RX from occuring */
4757d52f7132SAdrian Chadd 	taskqueue_block(sc->sc_tq);
4758d52f7132SAdrian Chadd 
4759ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4760e78719adSAdrian Chadd 	ath_hal_intrset(ah, 0);		/* Stop new RX/TX completion */
4761e78719adSAdrian Chadd 	ath_txrx_stop_locked(sc);	/* Stop pending RX/TX completion */
4762ee321975SAdrian Chadd 	if (ath_reset_grablock(sc, 1) == 0) {
4763ee321975SAdrian Chadd 		device_printf(sc->sc_dev, "%s: concurrent reset! Danger!\n",
4764ef27340cSAdrian Chadd 		    __func__);
4765ee321975SAdrian Chadd 	}
4766ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
4767c42a7b7eSSam Leffler 
476859efa8b5SSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %u (%u MHz, flags 0x%x)\n",
476959efa8b5SSam Leffler 	    __func__, ieee80211_chan2ieee(ic, chan),
477059efa8b5SSam Leffler 	    chan->ic_freq, chan->ic_flags);
477159efa8b5SSam Leffler 	if (chan != sc->sc_curchan) {
4772c42a7b7eSSam Leffler 		HAL_STATUS status;
47735591b213SSam Leffler 		/*
47745591b213SSam Leffler 		 * To switch channels clear any pending DMA operations;
47755591b213SSam Leffler 		 * wait long enough for the RX fifo to drain, reset the
47765591b213SSam Leffler 		 * hardware at the new frequency, and then re-enable
47775591b213SSam Leffler 		 * the relevant bits of the h/w.
47785591b213SSam Leffler 		 */
4779ef27340cSAdrian Chadd #if 0
47805591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
4781ef27340cSAdrian Chadd #endif
47829a842e8bSAdrian Chadd 		ath_stoprecv(sc, 1);		/* turn off frame recv */
47839a842e8bSAdrian Chadd 		/*
47849a842e8bSAdrian Chadd 		 * First, handle completed TX/RX frames.
47859a842e8bSAdrian Chadd 		 */
4786f8cc9b09SAdrian Chadd 		ath_rx_flush(sc);
47879a842e8bSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_NOLOSS);
47889a842e8bSAdrian Chadd 		/*
47899a842e8bSAdrian Chadd 		 * Next, flush the non-scheduled frames.
47909a842e8bSAdrian Chadd 		 */
4791517526efSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_FULL);	/* clear pending tx frames */
47929a842e8bSAdrian Chadd 
47936322256bSAdrian Chadd 		ath_update_chainmasks(sc, chan);
47946322256bSAdrian Chadd 		ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
47956322256bSAdrian Chadd 		    sc->sc_cur_rxchainmask);
479659efa8b5SSam Leffler 		if (!ath_hal_reset(ah, sc->sc_opmode, chan, AH_TRUE, &status)) {
4797b032f27cSSam Leffler 			if_printf(ifp, "%s: unable to reset "
479879649302SGavin Atkinson 			    "channel %u (%u MHz, flags 0x%x), hal status %u\n",
479959efa8b5SSam Leffler 			    __func__, ieee80211_chan2ieee(ic, chan),
480059efa8b5SSam Leffler 			    chan->ic_freq, chan->ic_flags, status);
4801ef27340cSAdrian Chadd 			ret = EIO;
4802ef27340cSAdrian Chadd 			goto finish;
48035591b213SSam Leffler 		}
4804c59005e9SSam Leffler 		sc->sc_diversity = ath_hal_getdiversity(ah);
4805c42a7b7eSSam Leffler 
480648237774SAdrian Chadd 		/* Let DFS at it in case it's a DFS channel */
4807398bca2eSAdrian Chadd 		ath_dfs_radar_enable(sc, chan);
480848237774SAdrian Chadd 
48099af351f9SAdrian Chadd 		/* Let spectral at in case spectral is enabled */
48109af351f9SAdrian Chadd 		ath_spectral_enable(sc, chan);
48119af351f9SAdrian Chadd 
48125591b213SSam Leffler 		/*
48135591b213SSam Leffler 		 * Re-enable rx framework.
48145591b213SSam Leffler 		 */
48155591b213SSam Leffler 		if (ath_startrecv(sc) != 0) {
4816b032f27cSSam Leffler 			if_printf(ifp, "%s: unable to restart recv logic\n",
4817b032f27cSSam Leffler 			    __func__);
4818ef27340cSAdrian Chadd 			ret = EIO;
4819ef27340cSAdrian Chadd 			goto finish;
48205591b213SSam Leffler 		}
48215591b213SSam Leffler 
48225591b213SSam Leffler 		/*
48235591b213SSam Leffler 		 * Change channels and update the h/w rate map
48245591b213SSam Leffler 		 * if we're switching; e.g. 11a to 11b/g.
48255591b213SSam Leffler 		 */
4826c42a7b7eSSam Leffler 		ath_chan_change(sc, chan);
48270a915fadSSam Leffler 
48280a915fadSSam Leffler 		/*
48292fd9aabbSAdrian Chadd 		 * Reset clears the beacon timers; reset them
48302fd9aabbSAdrian Chadd 		 * here if needed.
48312fd9aabbSAdrian Chadd 		 */
48322fd9aabbSAdrian Chadd 		if (sc->sc_beacons) {		/* restart beacons */
48332fd9aabbSAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
48342fd9aabbSAdrian Chadd 			if (sc->sc_tdma)
48352fd9aabbSAdrian Chadd 				ath_tdma_config(sc, NULL);
48362fd9aabbSAdrian Chadd 			else
48372fd9aabbSAdrian Chadd #endif
48382fd9aabbSAdrian Chadd 			ath_beacon_config(sc, NULL);
48392fd9aabbSAdrian Chadd 		}
48402fd9aabbSAdrian Chadd 
48412fd9aabbSAdrian Chadd 		/*
48420a915fadSSam Leffler 		 * Re-enable interrupts.
48430a915fadSSam Leffler 		 */
4844e78719adSAdrian Chadd #if 0
48450a915fadSSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
4846ef27340cSAdrian Chadd #endif
48475591b213SSam Leffler 	}
4848ef27340cSAdrian Chadd 
4849ef27340cSAdrian Chadd finish:
4850ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4851ef27340cSAdrian Chadd 	sc->sc_inreset_cnt--;
4852ef27340cSAdrian Chadd 	/* XXX only do this if sc_inreset_cnt == 0? */
4853ef27340cSAdrian Chadd 	ath_hal_intrset(ah, sc->sc_imask);
4854ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
4855ef27340cSAdrian Chadd 
4856e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
4857ef27340cSAdrian Chadd 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4858e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
4859ef27340cSAdrian Chadd 	ath_txrx_start(sc);
4860ef27340cSAdrian Chadd 	/* XXX ath_start? */
4861ef27340cSAdrian Chadd 
4862ef27340cSAdrian Chadd 	return ret;
48635591b213SSam Leffler }
48645591b213SSam Leffler 
48655591b213SSam Leffler /*
48665591b213SSam Leffler  * Periodically recalibrate the PHY to account
48675591b213SSam Leffler  * for temperature/environment changes.
48685591b213SSam Leffler  */
48695591b213SSam Leffler static void
48705591b213SSam Leffler ath_calibrate(void *arg)
48715591b213SSam Leffler {
48725591b213SSam Leffler 	struct ath_softc *sc = arg;
48735591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
48742dc7fcc4SSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
48758d91de92SSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
4876943e37a1SAdrian Chadd 	HAL_BOOL longCal, isCalDone = AH_TRUE;
4877a108ab63SAdrian Chadd 	HAL_BOOL aniCal, shortCal = AH_FALSE;
48782dc7fcc4SSam Leffler 	int nextcal;
48795591b213SSam Leffler 
48808d91de92SSam Leffler 	if (ic->ic_flags & IEEE80211_F_SCAN)	/* defer, off channel */
48818d91de92SSam Leffler 		goto restart;
48822dc7fcc4SSam Leffler 	longCal = (ticks - sc->sc_lastlongcal >= ath_longcalinterval*hz);
4883a108ab63SAdrian Chadd 	aniCal = (ticks - sc->sc_lastani >= ath_anicalinterval*hz/1000);
4884a108ab63SAdrian Chadd 	if (sc->sc_doresetcal)
4885a108ab63SAdrian Chadd 		shortCal = (ticks - sc->sc_lastshortcal >= ath_shortcalinterval*hz/1000);
4886a108ab63SAdrian Chadd 
4887a108ab63SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: shortCal=%d; longCal=%d; aniCal=%d\n", __func__, shortCal, longCal, aniCal);
4888a108ab63SAdrian Chadd 	if (aniCal) {
4889a108ab63SAdrian Chadd 		sc->sc_stats.ast_ani_cal++;
4890a108ab63SAdrian Chadd 		sc->sc_lastani = ticks;
4891a108ab63SAdrian Chadd 		ath_hal_ani_poll(ah, sc->sc_curchan);
4892a108ab63SAdrian Chadd 	}
4893a108ab63SAdrian Chadd 
48942dc7fcc4SSam Leffler 	if (longCal) {
48955591b213SSam Leffler 		sc->sc_stats.ast_per_cal++;
48968197f57eSAdrian Chadd 		sc->sc_lastlongcal = ticks;
48975591b213SSam Leffler 		if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) {
48985591b213SSam Leffler 			/*
48995591b213SSam Leffler 			 * Rfgain is out of bounds, reset the chip
49005591b213SSam Leffler 			 * to load new gain values.
49015591b213SSam Leffler 			 */
4902370572d9SSam Leffler 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
4903370572d9SSam Leffler 				"%s: rfgain change\n", __func__);
49045591b213SSam Leffler 			sc->sc_stats.ast_per_rfgain++;
4905ef27340cSAdrian Chadd 			sc->sc_resetcal = 0;
4906ef27340cSAdrian Chadd 			sc->sc_doresetcal = AH_TRUE;
4907d52f7132SAdrian Chadd 			taskqueue_enqueue(sc->sc_tq, &sc->sc_resettask);
4908d52f7132SAdrian Chadd 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
4909ef27340cSAdrian Chadd 			return;
49105591b213SSam Leffler 		}
49112dc7fcc4SSam Leffler 		/*
49122dc7fcc4SSam Leffler 		 * If this long cal is after an idle period, then
49132dc7fcc4SSam Leffler 		 * reset the data collection state so we start fresh.
49142dc7fcc4SSam Leffler 		 */
49152dc7fcc4SSam Leffler 		if (sc->sc_resetcal) {
491659efa8b5SSam Leffler 			(void) ath_hal_calreset(ah, sc->sc_curchan);
49172dc7fcc4SSam Leffler 			sc->sc_lastcalreset = ticks;
4918a108ab63SAdrian Chadd 			sc->sc_lastshortcal = ticks;
49192dc7fcc4SSam Leffler 			sc->sc_resetcal = 0;
4920a108ab63SAdrian Chadd 			sc->sc_doresetcal = AH_TRUE;
49212dc7fcc4SSam Leffler 		}
49222dc7fcc4SSam Leffler 	}
4923a108ab63SAdrian Chadd 
4924a108ab63SAdrian Chadd 	/* Only call if we're doing a short/long cal, not for ANI calibration */
4925a108ab63SAdrian Chadd 	if (shortCal || longCal) {
4926943e37a1SAdrian Chadd 		isCalDone = AH_FALSE;
492759efa8b5SSam Leffler 		if (ath_hal_calibrateN(ah, sc->sc_curchan, longCal, &isCalDone)) {
49282dc7fcc4SSam Leffler 			if (longCal) {
49292dc7fcc4SSam Leffler 				/*
49302dc7fcc4SSam Leffler 				 * Calibrate noise floor data again in case of change.
49312dc7fcc4SSam Leffler 				 */
49322dc7fcc4SSam Leffler 				ath_hal_process_noisefloor(ah);
49332dc7fcc4SSam Leffler 			}
49342dc7fcc4SSam Leffler 		} else {
4935c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
4936c42a7b7eSSam Leffler 				"%s: calibration of channel %u failed\n",
493759efa8b5SSam Leffler 				__func__, sc->sc_curchan->ic_freq);
49385591b213SSam Leffler 			sc->sc_stats.ast_per_calfail++;
49395591b213SSam Leffler 		}
4940a108ab63SAdrian Chadd 		if (shortCal)
4941a108ab63SAdrian Chadd 			sc->sc_lastshortcal = ticks;
4942a108ab63SAdrian Chadd 	}
49432dc7fcc4SSam Leffler 	if (!isCalDone) {
49448d91de92SSam Leffler restart:
49457b0c77ecSSam Leffler 		/*
49462dc7fcc4SSam Leffler 		 * Use a shorter interval to potentially collect multiple
49472dc7fcc4SSam Leffler 		 * data samples required to complete calibration.  Once
49482dc7fcc4SSam Leffler 		 * we're told the work is done we drop back to a longer
49492dc7fcc4SSam Leffler 		 * interval between requests.  We're more aggressive doing
49502dc7fcc4SSam Leffler 		 * work when operating as an AP to improve operation right
49512dc7fcc4SSam Leffler 		 * after startup.
49527b0c77ecSSam Leffler 		 */
4953a108ab63SAdrian Chadd 		sc->sc_lastshortcal = ticks;
4954a108ab63SAdrian Chadd 		nextcal = ath_shortcalinterval*hz/1000;
49552dc7fcc4SSam Leffler 		if (sc->sc_opmode != HAL_M_HOSTAP)
49562dc7fcc4SSam Leffler 			nextcal *= 10;
4957a108ab63SAdrian Chadd 		sc->sc_doresetcal = AH_TRUE;
49582dc7fcc4SSam Leffler 	} else {
4959a108ab63SAdrian Chadd 		/* nextcal should be the shortest time for next event */
49602dc7fcc4SSam Leffler 		nextcal = ath_longcalinterval*hz;
49612dc7fcc4SSam Leffler 		if (sc->sc_lastcalreset == 0)
49622dc7fcc4SSam Leffler 			sc->sc_lastcalreset = sc->sc_lastlongcal;
49632dc7fcc4SSam Leffler 		else if (ticks - sc->sc_lastcalreset >= ath_resetcalinterval*hz)
49642dc7fcc4SSam Leffler 			sc->sc_resetcal = 1;	/* setup reset next trip */
4965a108ab63SAdrian Chadd 		sc->sc_doresetcal = AH_FALSE;
4966bd5a9920SSam Leffler 	}
4967a108ab63SAdrian Chadd 	/* ANI calibration may occur more often than short/long/resetcal */
4968a108ab63SAdrian Chadd 	if (ath_anicalinterval > 0)
4969a108ab63SAdrian Chadd 		nextcal = MIN(nextcal, ath_anicalinterval*hz/1000);
4970bd5a9920SSam Leffler 
49712dc7fcc4SSam Leffler 	if (nextcal != 0) {
49722dc7fcc4SSam Leffler 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: next +%u (%sisCalDone)\n",
49732dc7fcc4SSam Leffler 		    __func__, nextcal, isCalDone ? "" : "!");
49742dc7fcc4SSam Leffler 		callout_reset(&sc->sc_cal_ch, nextcal, ath_calibrate, sc);
49752dc7fcc4SSam Leffler 	} else {
49762dc7fcc4SSam Leffler 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: calibration disabled\n",
49772dc7fcc4SSam Leffler 		    __func__);
49782dc7fcc4SSam Leffler 		/* NB: don't rearm timer */
49792dc7fcc4SSam Leffler 	}
49805591b213SSam Leffler }
49815591b213SSam Leffler 
498268e8e04eSSam Leffler static void
498368e8e04eSSam Leffler ath_scan_start(struct ieee80211com *ic)
498468e8e04eSSam Leffler {
498568e8e04eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
498668e8e04eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
498768e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
498868e8e04eSSam Leffler 	u_int32_t rfilt;
498968e8e04eSSam Leffler 
499068e8e04eSSam Leffler 	/* XXX calibration timer? */
499168e8e04eSSam Leffler 
4992c98cefc5SAdrian Chadd 	ATH_LOCK(sc);
499368e8e04eSSam Leffler 	sc->sc_scanning = 1;
499468e8e04eSSam Leffler 	sc->sc_syncbeacon = 0;
499568e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
4996c98cefc5SAdrian Chadd 	ATH_UNLOCK(sc);
4997c98cefc5SAdrian Chadd 
4998c98cefc5SAdrian Chadd 	ATH_PCU_LOCK(sc);
499968e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
500068e8e04eSSam Leffler 	ath_hal_setassocid(ah, ifp->if_broadcastaddr, 0);
5001c98cefc5SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
500268e8e04eSSam Leffler 
500368e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0\n",
500468e8e04eSSam Leffler 		 __func__, rfilt, ether_sprintf(ifp->if_broadcastaddr));
500568e8e04eSSam Leffler }
500668e8e04eSSam Leffler 
500768e8e04eSSam Leffler static void
500868e8e04eSSam Leffler ath_scan_end(struct ieee80211com *ic)
500968e8e04eSSam Leffler {
501068e8e04eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
501168e8e04eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
501268e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
501368e8e04eSSam Leffler 	u_int32_t rfilt;
501468e8e04eSSam Leffler 
5015c98cefc5SAdrian Chadd 	ATH_LOCK(sc);
501668e8e04eSSam Leffler 	sc->sc_scanning = 0;
501768e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
5018c98cefc5SAdrian Chadd 	ATH_UNLOCK(sc);
5019c98cefc5SAdrian Chadd 
5020c98cefc5SAdrian Chadd 	ATH_PCU_LOCK(sc);
502168e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
502268e8e04eSSam Leffler 	ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
502368e8e04eSSam Leffler 
502468e8e04eSSam Leffler 	ath_hal_process_noisefloor(ah);
5025c98cefc5SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
502668e8e04eSSam Leffler 
502768e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
502868e8e04eSSam Leffler 		 __func__, rfilt, ether_sprintf(sc->sc_curbssid),
502968e8e04eSSam Leffler 		 sc->sc_curaid);
503068e8e04eSSam Leffler }
503168e8e04eSSam Leffler 
5032fdd72b4aSAdrian Chadd #ifdef	ATH_ENABLE_11N
5033e7200579SAdrian Chadd /*
5034e7200579SAdrian Chadd  * For now, just do a channel change.
5035e7200579SAdrian Chadd  *
5036e7200579SAdrian Chadd  * Later, we'll go through the hard slog of suspending tx/rx, changing rate
5037e7200579SAdrian Chadd  * control state and resetting the hardware without dropping frames out
5038e7200579SAdrian Chadd  * of the queue.
5039e7200579SAdrian Chadd  *
5040e7200579SAdrian Chadd  * The unfortunate trouble here is making absolutely sure that the
5041e7200579SAdrian Chadd  * channel width change has propagated enough so the hardware
5042e7200579SAdrian Chadd  * absolutely isn't handed bogus frames for it's current operating
5043e7200579SAdrian Chadd  * mode. (Eg, 40MHz frames in 20MHz mode.) Since TX and RX can and
5044e7200579SAdrian Chadd  * does occur in parallel, we need to make certain we've blocked
5045e7200579SAdrian Chadd  * any further ongoing TX (and RX, that can cause raw TX)
5046e7200579SAdrian Chadd  * before we do this.
5047e7200579SAdrian Chadd  */
5048e7200579SAdrian Chadd static void
5049e7200579SAdrian Chadd ath_update_chw(struct ieee80211com *ic)
5050e7200579SAdrian Chadd {
5051e7200579SAdrian Chadd 	struct ifnet *ifp = ic->ic_ifp;
5052e7200579SAdrian Chadd 	struct ath_softc *sc = ifp->if_softc;
5053e7200579SAdrian Chadd 
5054e7200579SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: called\n", __func__);
5055e7200579SAdrian Chadd 	ath_set_channel(ic);
5056e7200579SAdrian Chadd }
5057fdd72b4aSAdrian Chadd #endif	/* ATH_ENABLE_11N */
5058e7200579SAdrian Chadd 
505968e8e04eSSam Leffler static void
506068e8e04eSSam Leffler ath_set_channel(struct ieee80211com *ic)
506168e8e04eSSam Leffler {
506268e8e04eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
506368e8e04eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
506468e8e04eSSam Leffler 
506568e8e04eSSam Leffler 	(void) ath_chan_set(sc, ic->ic_curchan);
506668e8e04eSSam Leffler 	/*
506768e8e04eSSam Leffler 	 * If we are returning to our bss channel then mark state
506868e8e04eSSam Leffler 	 * so the next recv'd beacon's tsf will be used to sync the
506968e8e04eSSam Leffler 	 * beacon timers.  Note that since we only hear beacons in
507068e8e04eSSam Leffler 	 * sta/ibss mode this has no effect in other operating modes.
507168e8e04eSSam Leffler 	 */
5072a887b1e3SAdrian Chadd 	ATH_LOCK(sc);
507368e8e04eSSam Leffler 	if (!sc->sc_scanning && ic->ic_curchan == ic->ic_bsschan)
507468e8e04eSSam Leffler 		sc->sc_syncbeacon = 1;
5075a887b1e3SAdrian Chadd 	ATH_UNLOCK(sc);
507668e8e04eSSam Leffler }
507768e8e04eSSam Leffler 
5078b032f27cSSam Leffler /*
5079b032f27cSSam Leffler  * Walk the vap list and check if there any vap's in RUN state.
5080b032f27cSSam Leffler  */
50815591b213SSam Leffler static int
5082b032f27cSSam Leffler ath_isanyrunningvaps(struct ieee80211vap *this)
50835591b213SSam Leffler {
5084b032f27cSSam Leffler 	struct ieee80211com *ic = this->iv_ic;
5085b032f27cSSam Leffler 	struct ieee80211vap *vap;
5086b032f27cSSam Leffler 
5087b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
5088b032f27cSSam Leffler 
5089b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
5090309a3e45SSam Leffler 		if (vap != this && vap->iv_state >= IEEE80211_S_RUN)
5091b032f27cSSam Leffler 			return 1;
5092b032f27cSSam Leffler 	}
5093b032f27cSSam Leffler 	return 0;
5094b032f27cSSam Leffler }
5095b032f27cSSam Leffler 
5096b032f27cSSam Leffler static int
5097b032f27cSSam Leffler ath_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
5098b032f27cSSam Leffler {
5099b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
5100b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
5101b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
510245bbf62fSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5103b032f27cSSam Leffler 	struct ieee80211_node *ni = NULL;
510468e8e04eSSam Leffler 	int i, error, stamode;
51055591b213SSam Leffler 	u_int32_t rfilt;
5106f52efb6dSAdrian Chadd 	int csa_run_transition = 0;
5107a74ebfe5SAdrian Chadd 
51085591b213SSam Leffler 	static const HAL_LED_STATE leds[] = {
51095591b213SSam Leffler 	    HAL_LED_INIT,	/* IEEE80211_S_INIT */
51105591b213SSam Leffler 	    HAL_LED_SCAN,	/* IEEE80211_S_SCAN */
51115591b213SSam Leffler 	    HAL_LED_AUTH,	/* IEEE80211_S_AUTH */
51125591b213SSam Leffler 	    HAL_LED_ASSOC, 	/* IEEE80211_S_ASSOC */
511377d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_CAC */
51145591b213SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_RUN */
511577d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_CSA */
511677d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_SLEEP */
51175591b213SSam Leffler 	};
51185591b213SSam Leffler 
5119c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: %s -> %s\n", __func__,
5120b032f27cSSam Leffler 		ieee80211_state_name[vap->iv_state],
5121c42a7b7eSSam Leffler 		ieee80211_state_name[nstate]);
51225591b213SSam Leffler 
5123107fdf96SAdrian Chadd 	/*
5124107fdf96SAdrian Chadd 	 * net80211 _should_ have the comlock asserted at this point.
5125107fdf96SAdrian Chadd 	 * There are some comments around the calls to vap->iv_newstate
5126107fdf96SAdrian Chadd 	 * which indicate that it (newstate) may end up dropping the
5127107fdf96SAdrian Chadd 	 * lock.  This and the subsequent lock assert check after newstate
5128107fdf96SAdrian Chadd 	 * are an attempt to catch these and figure out how/why.
5129107fdf96SAdrian Chadd 	 */
5130107fdf96SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
5131107fdf96SAdrian Chadd 
5132f52efb6dSAdrian Chadd 	if (vap->iv_state == IEEE80211_S_CSA && nstate == IEEE80211_S_RUN)
5133f52efb6dSAdrian Chadd 		csa_run_transition = 1;
5134f52efb6dSAdrian Chadd 
51352e986da5SSam Leffler 	callout_drain(&sc->sc_cal_ch);
51365591b213SSam Leffler 	ath_hal_setledstate(ah, leds[nstate]);	/* set LED */
51375591b213SSam Leffler 
5138b032f27cSSam Leffler 	if (nstate == IEEE80211_S_SCAN) {
513958769f58SSam Leffler 		/*
5140b032f27cSSam Leffler 		 * Scanning: turn off beacon miss and don't beacon.
5141b032f27cSSam Leffler 		 * Mark beacon state so when we reach RUN state we'll
5142b032f27cSSam Leffler 		 * [re]setup beacons.  Unblock the task q thread so
5143b032f27cSSam Leffler 		 * deferred interrupt processing is done.
514458769f58SSam Leffler 		 */
5145b032f27cSSam Leffler 		ath_hal_intrset(ah,
5146b032f27cSSam Leffler 		    sc->sc_imask &~ (HAL_INT_SWBA | HAL_INT_BMISS));
51475591b213SSam Leffler 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
5148b032f27cSSam Leffler 		sc->sc_beacons = 0;
5149b032f27cSSam Leffler 		taskqueue_unblock(sc->sc_tq);
51505591b213SSam Leffler 	}
51515591b213SSam Leffler 
515280767531SAdrian Chadd 	ni = ieee80211_ref_node(vap->iv_bss);
515368e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
5154b032f27cSSam Leffler 	stamode = (vap->iv_opmode == IEEE80211_M_STA ||
51557b916f89SSam Leffler 		   vap->iv_opmode == IEEE80211_M_AHDEMO ||
5156b032f27cSSam Leffler 		   vap->iv_opmode == IEEE80211_M_IBSS);
515768e8e04eSSam Leffler 	if (stamode && nstate == IEEE80211_S_RUN) {
515868e8e04eSSam Leffler 		sc->sc_curaid = ni->ni_associd;
515968e8e04eSSam Leffler 		IEEE80211_ADDR_COPY(sc->sc_curbssid, ni->ni_bssid);
5160b032f27cSSam Leffler 		ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
5161b032f27cSSam Leffler 	}
516268e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
5163b032f27cSSam Leffler 	   __func__, rfilt, ether_sprintf(sc->sc_curbssid), sc->sc_curaid);
516468e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
516568e8e04eSSam Leffler 
5166b032f27cSSam Leffler 	/* XXX is this to restore keycache on resume? */
5167b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_STA &&
5168b032f27cSSam Leffler 	    (vap->iv_flags & IEEE80211_F_PRIVACY)) {
51695591b213SSam Leffler 		for (i = 0; i < IEEE80211_WEP_NKID; i++)
51705591b213SSam Leffler 			if (ath_hal_keyisvalid(ah, i))
517168e8e04eSSam Leffler 				ath_hal_keysetmac(ah, i, ni->ni_bssid);
51725591b213SSam Leffler 	}
5173b032f27cSSam Leffler 
5174b032f27cSSam Leffler 	/*
5175b032f27cSSam Leffler 	 * Invoke the parent method to do net80211 work.
5176b032f27cSSam Leffler 	 */
5177b032f27cSSam Leffler 	error = avp->av_newstate(vap, nstate, arg);
5178b032f27cSSam Leffler 	if (error != 0)
5179b032f27cSSam Leffler 		goto bad;
5180c42a7b7eSSam Leffler 
5181107fdf96SAdrian Chadd 	/*
5182107fdf96SAdrian Chadd 	 * See above: ensure av_newstate() doesn't drop the lock
5183107fdf96SAdrian Chadd 	 * on us.
5184107fdf96SAdrian Chadd 	 */
5185107fdf96SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
5186107fdf96SAdrian Chadd 
518768e8e04eSSam Leffler 	if (nstate == IEEE80211_S_RUN) {
5188b032f27cSSam Leffler 		/* NB: collect bss node again, it may have changed */
518980767531SAdrian Chadd 		ieee80211_free_node(ni);
519080767531SAdrian Chadd 		ni = ieee80211_ref_node(vap->iv_bss);
51915591b213SSam Leffler 
5192b032f27cSSam Leffler 		DPRINTF(sc, ATH_DEBUG_STATE,
5193b032f27cSSam Leffler 		    "%s(RUN): iv_flags 0x%08x bintvl %d bssid %s "
5194b032f27cSSam Leffler 		    "capinfo 0x%04x chan %d\n", __func__,
5195b032f27cSSam Leffler 		    vap->iv_flags, ni->ni_intval, ether_sprintf(ni->ni_bssid),
5196b032f27cSSam Leffler 		    ni->ni_capinfo, ieee80211_chan2ieee(ic, ic->ic_curchan));
5197b032f27cSSam Leffler 
5198b032f27cSSam Leffler 		switch (vap->iv_opmode) {
5199584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
520010ad9a77SSam Leffler 		case IEEE80211_M_AHDEMO:
520110ad9a77SSam Leffler 			if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
520210ad9a77SSam Leffler 				break;
520310ad9a77SSam Leffler 			/* fall thru... */
520410ad9a77SSam Leffler #endif
5205e8fd88a3SSam Leffler 		case IEEE80211_M_HOSTAP:
5206e8fd88a3SSam Leffler 		case IEEE80211_M_IBSS:
520759aa14a9SRui Paulo 		case IEEE80211_M_MBSS:
52085591b213SSam Leffler 			/*
5209e8fd88a3SSam Leffler 			 * Allocate and setup the beacon frame.
5210e8fd88a3SSam Leffler 			 *
5211f818612bSSam Leffler 			 * Stop any previous beacon DMA.  This may be
5212f818612bSSam Leffler 			 * necessary, for example, when an ibss merge
5213f818612bSSam Leffler 			 * causes reconfiguration; there will be a state
5214f818612bSSam Leffler 			 * transition from RUN->RUN that means we may
5215f818612bSSam Leffler 			 * be called with beacon transmission active.
5216f818612bSSam Leffler 			 */
5217f818612bSSam Leffler 			ath_hal_stoptxdma(ah, sc->sc_bhalq);
5218b032f27cSSam Leffler 
52195591b213SSam Leffler 			error = ath_beacon_alloc(sc, ni);
52205591b213SSam Leffler 			if (error != 0)
52215591b213SSam Leffler 				goto bad;
52227a04dc27SSam Leffler 			/*
522380d939bfSSam Leffler 			 * If joining an adhoc network defer beacon timer
522480d939bfSSam Leffler 			 * configuration to the next beacon frame so we
522580d939bfSSam Leffler 			 * have a current TSF to use.  Otherwise we're
5226b032f27cSSam Leffler 			 * starting an ibss/bss so there's no need to delay;
5227b032f27cSSam Leffler 			 * if this is the first vap moving to RUN state, then
5228b032f27cSSam Leffler 			 * beacon state needs to be [re]configured.
52297a04dc27SSam Leffler 			 */
5230b032f27cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_IBSS &&
5231b032f27cSSam Leffler 			    ni->ni_tstamp.tsf != 0) {
523280d939bfSSam Leffler 				sc->sc_syncbeacon = 1;
5233b032f27cSSam Leffler 			} else if (!sc->sc_beacons) {
5234584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
523510ad9a77SSam Leffler 				if (vap->iv_caps & IEEE80211_C_TDMA)
523610ad9a77SSam Leffler 					ath_tdma_config(sc, vap);
523710ad9a77SSam Leffler 				else
523810ad9a77SSam Leffler #endif
5239b032f27cSSam Leffler 					ath_beacon_config(sc, vap);
5240b032f27cSSam Leffler 				sc->sc_beacons = 1;
5241b032f27cSSam Leffler 			}
5242e8fd88a3SSam Leffler 			break;
5243e8fd88a3SSam Leffler 		case IEEE80211_M_STA:
5244e8fd88a3SSam Leffler 			/*
524580d939bfSSam Leffler 			 * Defer beacon timer configuration to the next
524680d939bfSSam Leffler 			 * beacon frame so we have a current TSF to use
524780d939bfSSam Leffler 			 * (any TSF collected when scanning is likely old).
5248f52efb6dSAdrian Chadd 			 * However if it's due to a CSA -> RUN transition,
5249f52efb6dSAdrian Chadd 			 * force a beacon update so we pick up a lack of
5250f52efb6dSAdrian Chadd 			 * beacons from an AP in CAC and thus force a
5251f52efb6dSAdrian Chadd 			 * scan.
5252a74ebfe5SAdrian Chadd 			 *
5253a74ebfe5SAdrian Chadd 			 * And, there's also corner cases here where
5254a74ebfe5SAdrian Chadd 			 * after a scan, the AP may have disappeared.
5255a74ebfe5SAdrian Chadd 			 * In that case, we may not receive an actual
5256a74ebfe5SAdrian Chadd 			 * beacon to update the beacon timer and thus we
5257a74ebfe5SAdrian Chadd 			 * won't get notified of the missing beacons.
52587a04dc27SSam Leffler 			 */
525980d939bfSSam Leffler 			sc->sc_syncbeacon = 1;
5260a74ebfe5SAdrian Chadd #if 0
5261f52efb6dSAdrian Chadd 			if (csa_run_transition)
5262a74ebfe5SAdrian Chadd #endif
5263f52efb6dSAdrian Chadd 				ath_beacon_config(sc, vap);
5264a74ebfe5SAdrian Chadd 
5265a74ebfe5SAdrian Chadd 			/*
5266a74ebfe5SAdrian Chadd 			 * PR: kern/175227
5267a74ebfe5SAdrian Chadd 			 *
5268a74ebfe5SAdrian Chadd 			 * Reconfigure beacons during reset; as otherwise
5269a74ebfe5SAdrian Chadd 			 * we won't get the beacon timers reprogrammed
5270a74ebfe5SAdrian Chadd 			 * after a reset and thus we won't pick up a
5271a74ebfe5SAdrian Chadd 			 * beacon miss interrupt.
5272a74ebfe5SAdrian Chadd 			 *
5273a74ebfe5SAdrian Chadd 			 * Hopefully we'll see a beacon before the BMISS
5274a74ebfe5SAdrian Chadd 			 * timer fires (too often), leading to a STA
5275a74ebfe5SAdrian Chadd 			 * disassociation.
5276a74ebfe5SAdrian Chadd 			 */
5277a74ebfe5SAdrian Chadd 			sc->sc_beacons = 1;
5278e8fd88a3SSam Leffler 			break;
5279b032f27cSSam Leffler 		case IEEE80211_M_MONITOR:
5280b032f27cSSam Leffler 			/*
5281b032f27cSSam Leffler 			 * Monitor mode vaps have only INIT->RUN and RUN->RUN
5282b032f27cSSam Leffler 			 * transitions so we must re-enable interrupts here to
5283b032f27cSSam Leffler 			 * handle the case of a single monitor mode vap.
5284b032f27cSSam Leffler 			 */
5285b032f27cSSam Leffler 			ath_hal_intrset(ah, sc->sc_imask);
5286b032f27cSSam Leffler 			break;
5287b032f27cSSam Leffler 		case IEEE80211_M_WDS:
5288b032f27cSSam Leffler 			break;
5289e8fd88a3SSam Leffler 		default:
5290e8fd88a3SSam Leffler 			break;
52915591b213SSam Leffler 		}
52925591b213SSam Leffler 		/*
52937b0c77ecSSam Leffler 		 * Let the hal process statistics collected during a
52947b0c77ecSSam Leffler 		 * scan so it can provide calibrated noise floor data.
52957b0c77ecSSam Leffler 		 */
52967b0c77ecSSam Leffler 		ath_hal_process_noisefloor(ah);
52977b0c77ecSSam Leffler 		/*
5298ffa2cab6SSam Leffler 		 * Reset rssi stats; maybe not the best place...
5299ffa2cab6SSam Leffler 		 */
5300ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
5301ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
5302ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
530345bbf62fSSam Leffler 		/*
5304b032f27cSSam Leffler 		 * Finally, start any timers and the task q thread
5305b032f27cSSam Leffler 		 * (in case we didn't go through SCAN state).
530645bbf62fSSam Leffler 		 */
53072dc7fcc4SSam Leffler 		if (ath_longcalinterval != 0) {
5308c42a7b7eSSam Leffler 			/* start periodic recalibration timer */
53092dc7fcc4SSam Leffler 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
53102dc7fcc4SSam Leffler 		} else {
53112dc7fcc4SSam Leffler 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
53122dc7fcc4SSam Leffler 			    "%s: calibration disabled\n", __func__);
5313c42a7b7eSSam Leffler 		}
5314b032f27cSSam Leffler 		taskqueue_unblock(sc->sc_tq);
5315b032f27cSSam Leffler 	} else if (nstate == IEEE80211_S_INIT) {
5316b032f27cSSam Leffler 		/*
5317b032f27cSSam Leffler 		 * If there are no vaps left in RUN state then
5318b032f27cSSam Leffler 		 * shutdown host/driver operation:
5319b032f27cSSam Leffler 		 * o disable interrupts
5320b032f27cSSam Leffler 		 * o disable the task queue thread
5321b032f27cSSam Leffler 		 * o mark beacon processing as stopped
5322b032f27cSSam Leffler 		 */
5323b032f27cSSam Leffler 		if (!ath_isanyrunningvaps(vap)) {
5324b032f27cSSam Leffler 			sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
5325b032f27cSSam Leffler 			/* disable interrupts  */
5326b032f27cSSam Leffler 			ath_hal_intrset(ah, sc->sc_imask &~ HAL_INT_GLOBAL);
5327b032f27cSSam Leffler 			taskqueue_block(sc->sc_tq);
5328b032f27cSSam Leffler 			sc->sc_beacons = 0;
5329b032f27cSSam Leffler 		}
5330584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
533110ad9a77SSam Leffler 		ath_hal_setcca(ah, AH_TRUE);
533210ad9a77SSam Leffler #endif
5333b032f27cSSam Leffler 	}
53345591b213SSam Leffler bad:
533580767531SAdrian Chadd 	ieee80211_free_node(ni);
53365591b213SSam Leffler 	return error;
53375591b213SSam Leffler }
53385591b213SSam Leffler 
53395591b213SSam Leffler /*
5340e8fd88a3SSam Leffler  * Allocate a key cache slot to the station so we can
5341e8fd88a3SSam Leffler  * setup a mapping from key index to node. The key cache
5342e8fd88a3SSam Leffler  * slot is needed for managing antenna state and for
5343e8fd88a3SSam Leffler  * compression when stations do not use crypto.  We do
5344e8fd88a3SSam Leffler  * it uniliaterally here; if crypto is employed this slot
5345e8fd88a3SSam Leffler  * will be reassigned.
5346e8fd88a3SSam Leffler  */
5347e8fd88a3SSam Leffler static void
5348e8fd88a3SSam Leffler ath_setup_stationkey(struct ieee80211_node *ni)
5349e8fd88a3SSam Leffler {
5350b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
5351b032f27cSSam Leffler 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
5352c1225b52SSam Leffler 	ieee80211_keyix keyix, rxkeyix;
5353e8fd88a3SSam Leffler 
535480767531SAdrian Chadd 	/* XXX should take a locked ref to vap->iv_bss */
5355b032f27cSSam Leffler 	if (!ath_key_alloc(vap, &ni->ni_ucastkey, &keyix, &rxkeyix)) {
5356e8fd88a3SSam Leffler 		/*
5357e8fd88a3SSam Leffler 		 * Key cache is full; we'll fall back to doing
5358e8fd88a3SSam Leffler 		 * the more expensive lookup in software.  Note
5359e8fd88a3SSam Leffler 		 * this also means no h/w compression.
5360e8fd88a3SSam Leffler 		 */
5361e8fd88a3SSam Leffler 		/* XXX msg+statistic */
5362e8fd88a3SSam Leffler 	} else {
5363c1225b52SSam Leffler 		/* XXX locking? */
5364e8fd88a3SSam Leffler 		ni->ni_ucastkey.wk_keyix = keyix;
5365c1225b52SSam Leffler 		ni->ni_ucastkey.wk_rxkeyix = rxkeyix;
536633052833SSam Leffler 		/* NB: must mark device key to get called back on delete */
536733052833SSam Leffler 		ni->ni_ucastkey.wk_flags |= IEEE80211_KEY_DEVKEY;
5368d3ac945bSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_ucastkey.wk_macaddr, ni->ni_macaddr);
5369e8fd88a3SSam Leffler 		/* NB: this will create a pass-thru key entry */
537055c7b877SAdrian Chadd 		ath_keyset(sc, vap, &ni->ni_ucastkey, vap->iv_bss);
5371e8fd88a3SSam Leffler 	}
5372e8fd88a3SSam Leffler }
5373e8fd88a3SSam Leffler 
5374e8fd88a3SSam Leffler /*
53755591b213SSam Leffler  * Setup driver-specific state for a newly associated node.
53765591b213SSam Leffler  * Note that we're called also on a re-associate, the isnew
53775591b213SSam Leffler  * param tells us if this is the first time or not.
53785591b213SSam Leffler  */
53795591b213SSam Leffler static void
5380e9962332SSam Leffler ath_newassoc(struct ieee80211_node *ni, int isnew)
53815591b213SSam Leffler {
5382b032f27cSSam Leffler 	struct ath_node *an = ATH_NODE(ni);
5383b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
5384b032f27cSSam Leffler 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
5385c62362cbSSam Leffler 	const struct ieee80211_txparam *tp = ni->ni_txparms;
53865591b213SSam Leffler 
5387ab06fdf2SSam Leffler 	an->an_mcastrix = ath_tx_findrix(sc, tp->mcastrate);
5388ab06fdf2SSam Leffler 	an->an_mgmtrix = ath_tx_findrix(sc, tp->mgmtrate);
5389b032f27cSSam Leffler 
5390b032f27cSSam Leffler 	ath_rate_newassoc(sc, an, isnew);
5391e8fd88a3SSam Leffler 	if (isnew &&
5392b032f27cSSam Leffler 	    (vap->iv_flags & IEEE80211_F_PRIVACY) == 0 && sc->sc_hasclrkey &&
5393b032f27cSSam Leffler 	    ni->ni_ucastkey.wk_keyix == IEEE80211_KEYIX_NONE)
5394e8fd88a3SSam Leffler 		ath_setup_stationkey(ni);
53954bed2b67SAdrian Chadd 
53964bed2b67SAdrian Chadd 	/*
53974bed2b67SAdrian Chadd 	 * If we're reassociating, make sure that any paused queues
53984bed2b67SAdrian Chadd 	 * get unpaused.
53994bed2b67SAdrian Chadd 	 *
54004bed2b67SAdrian Chadd 	 * Now, we may hvae frames in the hardware queue for this node.
54014bed2b67SAdrian Chadd 	 * So if we are reassociating and there are frames in the queue,
54024bed2b67SAdrian Chadd 	 * we need to go through the cleanup path to ensure that they're
54034bed2b67SAdrian Chadd 	 * marked as non-aggregate.
54044bed2b67SAdrian Chadd 	 */
54054bed2b67SAdrian Chadd 	if (! isnew) {
54064bed2b67SAdrian Chadd 		device_printf(sc->sc_dev,
54074bed2b67SAdrian Chadd 		    "%s: %6D: reassoc; is_powersave=%d\n",
54084bed2b67SAdrian Chadd 		    __func__,
54094bed2b67SAdrian Chadd 		    ni->ni_macaddr,
54104bed2b67SAdrian Chadd 		    ":",
54114bed2b67SAdrian Chadd 		    an->an_is_powersave);
54124bed2b67SAdrian Chadd 
54134bed2b67SAdrian Chadd 		/* XXX for now, we can't hold the lock across assoc */
54144bed2b67SAdrian Chadd 		ath_tx_node_reassoc(sc, an);
54154bed2b67SAdrian Chadd 
54164bed2b67SAdrian Chadd 		/* XXX for now, we can't hold the lock across wakeup */
54174bed2b67SAdrian Chadd 		if (an->an_is_powersave)
54184bed2b67SAdrian Chadd 			ath_tx_node_wakeup(sc, an);
54194bed2b67SAdrian Chadd 	}
5420e8fd88a3SSam Leffler }
54215591b213SSam Leffler 
54225591b213SSam Leffler static int
542359efa8b5SSam Leffler ath_setregdomain(struct ieee80211com *ic, struct ieee80211_regdomain *reg,
5424b032f27cSSam Leffler 	int nchans, struct ieee80211_channel chans[])
5425b032f27cSSam Leffler {
5426b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
5427b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
542859efa8b5SSam Leffler 	HAL_STATUS status;
5429b032f27cSSam Leffler 
5430033022a9SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
543159efa8b5SSam Leffler 	    "%s: rd %u cc %u location %c%s\n",
543259efa8b5SSam Leffler 	    __func__, reg->regdomain, reg->country, reg->location,
543359efa8b5SSam Leffler 	    reg->ecm ? " ecm" : "");
5434033022a9SSam Leffler 
543559efa8b5SSam Leffler 	status = ath_hal_set_channels(ah, chans, nchans,
543659efa8b5SSam Leffler 	    reg->country, reg->regdomain);
543759efa8b5SSam Leffler 	if (status != HAL_OK) {
543859efa8b5SSam Leffler 		DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: failed, status %u\n",
543959efa8b5SSam Leffler 		    __func__, status);
544059efa8b5SSam Leffler 		return EINVAL;		/* XXX */
5441b032f27cSSam Leffler 	}
54428db87e40SAdrian Chadd 
5443b032f27cSSam Leffler 	return 0;
5444b032f27cSSam Leffler }
5445b032f27cSSam Leffler 
5446b032f27cSSam Leffler static void
5447b032f27cSSam Leffler ath_getradiocaps(struct ieee80211com *ic,
54485fe9f044SSam Leffler 	int maxchans, int *nchans, struct ieee80211_channel chans[])
5449b032f27cSSam Leffler {
5450b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
5451b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5452b032f27cSSam Leffler 
545359efa8b5SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: use rd %u cc %d\n",
545459efa8b5SSam Leffler 	    __func__, SKU_DEBUG, CTRY_DEFAULT);
5455033022a9SSam Leffler 
545659efa8b5SSam Leffler 	/* XXX check return */
545759efa8b5SSam Leffler 	(void) ath_hal_getchannels(ah, chans, maxchans, nchans,
545859efa8b5SSam Leffler 	    HAL_MODE_ALL, CTRY_DEFAULT, SKU_DEBUG, AH_TRUE);
5459033022a9SSam Leffler 
5460b032f27cSSam Leffler }
5461b032f27cSSam Leffler 
5462b032f27cSSam Leffler static int
5463b032f27cSSam Leffler ath_getchannels(struct ath_softc *sc)
5464b032f27cSSam Leffler {
5465b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
5466b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
5467b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
546859efa8b5SSam Leffler 	HAL_STATUS status;
5469b032f27cSSam Leffler 
5470b032f27cSSam Leffler 	/*
547159efa8b5SSam Leffler 	 * Collect channel set based on EEPROM contents.
5472b032f27cSSam Leffler 	 */
547359efa8b5SSam Leffler 	status = ath_hal_init_channels(ah, ic->ic_channels, IEEE80211_CHAN_MAX,
547459efa8b5SSam Leffler 	    &ic->ic_nchans, HAL_MODE_ALL, CTRY_DEFAULT, SKU_NONE, AH_TRUE);
547559efa8b5SSam Leffler 	if (status != HAL_OK) {
547659efa8b5SSam Leffler 		if_printf(ifp, "%s: unable to collect channel list from hal, "
547759efa8b5SSam Leffler 		    "status %d\n", __func__, status);
547859efa8b5SSam Leffler 		return EINVAL;
547959efa8b5SSam Leffler 	}
5480ca876918SSam Leffler 	(void) ath_hal_getregdomain(ah, &sc->sc_eerd);
5481ca876918SSam Leffler 	ath_hal_getcountrycode(ah, &sc->sc_eecc);	/* NB: cannot fail */
548259efa8b5SSam Leffler 	/* XXX map Atheros sku's to net80211 SKU's */
548359efa8b5SSam Leffler 	/* XXX net80211 types too small */
548459efa8b5SSam Leffler 	ic->ic_regdomain.regdomain = (uint16_t) sc->sc_eerd;
548559efa8b5SSam Leffler 	ic->ic_regdomain.country = (uint16_t) sc->sc_eecc;
548659efa8b5SSam Leffler 	ic->ic_regdomain.isocc[0] = ' ';	/* XXX don't know */
548759efa8b5SSam Leffler 	ic->ic_regdomain.isocc[1] = ' ';
548859efa8b5SSam Leffler 
5489b032f27cSSam Leffler 	ic->ic_regdomain.ecm = 1;
5490b032f27cSSam Leffler 	ic->ic_regdomain.location = 'I';
5491033022a9SSam Leffler 
5492033022a9SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
549359efa8b5SSam Leffler 	    "%s: eeprom rd %u cc %u (mapped rd %u cc %u) location %c%s\n",
5494033022a9SSam Leffler 	    __func__, sc->sc_eerd, sc->sc_eecc,
5495033022a9SSam Leffler 	    ic->ic_regdomain.regdomain, ic->ic_regdomain.country,
549659efa8b5SSam Leffler 	    ic->ic_regdomain.location, ic->ic_regdomain.ecm ? " ecm" : "");
54975591b213SSam Leffler 	return 0;
54985591b213SSam Leffler }
54995591b213SSam Leffler 
55006c4612b9SSam Leffler static int
55016c4612b9SSam Leffler ath_rate_setup(struct ath_softc *sc, u_int mode)
55026c4612b9SSam Leffler {
55036c4612b9SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
55046c4612b9SSam Leffler 	const HAL_RATE_TABLE *rt;
55056c4612b9SSam Leffler 
55066c4612b9SSam Leffler 	switch (mode) {
55076c4612b9SSam Leffler 	case IEEE80211_MODE_11A:
55086c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A);
55096c4612b9SSam Leffler 		break;
5510724c193aSSam Leffler 	case IEEE80211_MODE_HALF:
5511aaa70f2fSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_HALF_RATE);
5512aaa70f2fSSam Leffler 		break;
5513724c193aSSam Leffler 	case IEEE80211_MODE_QUARTER:
5514aaa70f2fSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_QUARTER_RATE);
5515aaa70f2fSSam Leffler 		break;
55166c4612b9SSam Leffler 	case IEEE80211_MODE_11B:
55176c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11B);
55186c4612b9SSam Leffler 		break;
55196c4612b9SSam Leffler 	case IEEE80211_MODE_11G:
55206c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11G);
55216c4612b9SSam Leffler 		break;
55226c4612b9SSam Leffler 	case IEEE80211_MODE_TURBO_A:
552368e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_108A);
55246c4612b9SSam Leffler 		break;
55256c4612b9SSam Leffler 	case IEEE80211_MODE_TURBO_G:
55266c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_108G);
55276c4612b9SSam Leffler 		break;
552868e8e04eSSam Leffler 	case IEEE80211_MODE_STURBO_A:
552968e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_TURBO);
553068e8e04eSSam Leffler 		break;
553168e8e04eSSam Leffler 	case IEEE80211_MODE_11NA:
553268e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11NA_HT20);
553368e8e04eSSam Leffler 		break;
553468e8e04eSSam Leffler 	case IEEE80211_MODE_11NG:
553568e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11NG_HT20);
553668e8e04eSSam Leffler 		break;
55376c4612b9SSam Leffler 	default:
55386c4612b9SSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid mode %u\n",
55396c4612b9SSam Leffler 			__func__, mode);
55406c4612b9SSam Leffler 		return 0;
55416c4612b9SSam Leffler 	}
55426c4612b9SSam Leffler 	sc->sc_rates[mode] = rt;
5543aaa70f2fSSam Leffler 	return (rt != NULL);
55445591b213SSam Leffler }
55455591b213SSam Leffler 
55465591b213SSam Leffler static void
55475591b213SSam Leffler ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode)
55485591b213SSam Leffler {
55493e50ec2cSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
55503e50ec2cSSam Leffler 	/* NB: on/off times from the Atheros NDIS driver, w/ permission */
55513e50ec2cSSam Leffler 	static const struct {
55523e50ec2cSSam Leffler 		u_int		rate;		/* tx/rx 802.11 rate */
55533e50ec2cSSam Leffler 		u_int16_t	timeOn;		/* LED on time (ms) */
55543e50ec2cSSam Leffler 		u_int16_t	timeOff;	/* LED off time (ms) */
55553e50ec2cSSam Leffler 	} blinkrates[] = {
55563e50ec2cSSam Leffler 		{ 108,  40,  10 },
55573e50ec2cSSam Leffler 		{  96,  44,  11 },
55583e50ec2cSSam Leffler 		{  72,  50,  13 },
55593e50ec2cSSam Leffler 		{  48,  57,  14 },
55603e50ec2cSSam Leffler 		{  36,  67,  16 },
55613e50ec2cSSam Leffler 		{  24,  80,  20 },
55623e50ec2cSSam Leffler 		{  22, 100,  25 },
55633e50ec2cSSam Leffler 		{  18, 133,  34 },
55643e50ec2cSSam Leffler 		{  12, 160,  40 },
55653e50ec2cSSam Leffler 		{  10, 200,  50 },
55663e50ec2cSSam Leffler 		{   6, 240,  58 },
55673e50ec2cSSam Leffler 		{   4, 267,  66 },
55683e50ec2cSSam Leffler 		{   2, 400, 100 },
55693e50ec2cSSam Leffler 		{   0, 500, 130 },
5570724c193aSSam Leffler 		/* XXX half/quarter rates */
55713e50ec2cSSam Leffler 	};
55725591b213SSam Leffler 	const HAL_RATE_TABLE *rt;
55733e50ec2cSSam Leffler 	int i, j;
55745591b213SSam Leffler 
55755591b213SSam Leffler 	memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap));
55765591b213SSam Leffler 	rt = sc->sc_rates[mode];
55775591b213SSam Leffler 	KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode));
5578180f268dSSam Leffler 	for (i = 0; i < rt->rateCount; i++) {
5579180f268dSSam Leffler 		uint8_t ieeerate = rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
5580180f268dSSam Leffler 		if (rt->info[i].phy != IEEE80211_T_HT)
5581180f268dSSam Leffler 			sc->sc_rixmap[ieeerate] = i;
5582180f268dSSam Leffler 		else
5583180f268dSSam Leffler 			sc->sc_rixmap[ieeerate | IEEE80211_RATE_MCS] = i;
5584180f268dSSam Leffler 	}
55851b1a8e41SSam Leffler 	memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap));
558646d4d74cSSam Leffler 	for (i = 0; i < N(sc->sc_hwmap); i++) {
558746d4d74cSSam Leffler 		if (i >= rt->rateCount) {
55883e50ec2cSSam Leffler 			sc->sc_hwmap[i].ledon = (500 * hz) / 1000;
55893e50ec2cSSam Leffler 			sc->sc_hwmap[i].ledoff = (130 * hz) / 1000;
559016b4851aSSam Leffler 			continue;
55913e50ec2cSSam Leffler 		}
55923e50ec2cSSam Leffler 		sc->sc_hwmap[i].ieeerate =
559346d4d74cSSam Leffler 			rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
559446d4d74cSSam Leffler 		if (rt->info[i].phy == IEEE80211_T_HT)
559526041a14SSam Leffler 			sc->sc_hwmap[i].ieeerate |= IEEE80211_RATE_MCS;
5596d3be6f5bSSam Leffler 		sc->sc_hwmap[i].txflags = IEEE80211_RADIOTAP_F_DATAPAD;
559746d4d74cSSam Leffler 		if (rt->info[i].shortPreamble ||
559846d4d74cSSam Leffler 		    rt->info[i].phy == IEEE80211_T_OFDM)
5599d3be6f5bSSam Leffler 			sc->sc_hwmap[i].txflags |= IEEE80211_RADIOTAP_F_SHORTPRE;
56005463c4a4SSam Leffler 		sc->sc_hwmap[i].rxflags = sc->sc_hwmap[i].txflags;
56013e50ec2cSSam Leffler 		for (j = 0; j < N(blinkrates)-1; j++)
56023e50ec2cSSam Leffler 			if (blinkrates[j].rate == sc->sc_hwmap[i].ieeerate)
56033e50ec2cSSam Leffler 				break;
56043e50ec2cSSam Leffler 		/* NB: this uses the last entry if the rate isn't found */
56053e50ec2cSSam Leffler 		/* XXX beware of overlow */
56063e50ec2cSSam Leffler 		sc->sc_hwmap[i].ledon = (blinkrates[j].timeOn * hz) / 1000;
56073e50ec2cSSam Leffler 		sc->sc_hwmap[i].ledoff = (blinkrates[j].timeOff * hz) / 1000;
5608c42a7b7eSSam Leffler 	}
56095591b213SSam Leffler 	sc->sc_currates = rt;
56105591b213SSam Leffler 	sc->sc_curmode = mode;
56115591b213SSam Leffler 	/*
5612c42a7b7eSSam Leffler 	 * All protection frames are transmited at 2Mb/s for
5613c42a7b7eSSam Leffler 	 * 11g, otherwise at 1Mb/s.
56145591b213SSam Leffler 	 */
5615913a1ba1SSam Leffler 	if (mode == IEEE80211_MODE_11G)
5616ab06fdf2SSam Leffler 		sc->sc_protrix = ath_tx_findrix(sc, 2*2);
5617913a1ba1SSam Leffler 	else
5618ab06fdf2SSam Leffler 		sc->sc_protrix = ath_tx_findrix(sc, 2*1);
56194fa8d4efSDaniel Eischen 	/* NB: caller is responsible for resetting rate control state */
56203e50ec2cSSam Leffler #undef N
56215591b213SSam Leffler }
56225591b213SSam Leffler 
5623c42a7b7eSSam Leffler static void
56242e986da5SSam Leffler ath_watchdog(void *arg)
5625c42a7b7eSSam Leffler {
56262e986da5SSam Leffler 	struct ath_softc *sc = arg;
5627ef27340cSAdrian Chadd 	int do_reset = 0;
5628c42a7b7eSSam Leffler 
56292e986da5SSam Leffler 	if (sc->sc_wd_timer != 0 && --sc->sc_wd_timer == 0) {
56302e986da5SSam Leffler 		struct ifnet *ifp = sc->sc_ifp;
5631459bc4f0SSam Leffler 		uint32_t hangs;
5632459bc4f0SSam Leffler 
5633459bc4f0SSam Leffler 		if (ath_hal_gethangstate(sc->sc_ah, 0xffff, &hangs) &&
5634459bc4f0SSam Leffler 		    hangs != 0) {
5635459bc4f0SSam Leffler 			if_printf(ifp, "%s hang detected (0x%x)\n",
5636459bc4f0SSam Leffler 			    hangs & 0xff ? "bb" : "mac", hangs);
5637459bc4f0SSam Leffler 		} else
5638c42a7b7eSSam Leffler 			if_printf(ifp, "device timeout\n");
5639ef27340cSAdrian Chadd 		do_reset = 1;
5640c42a7b7eSSam Leffler 		ifp->if_oerrors++;
5641c42a7b7eSSam Leffler 		sc->sc_stats.ast_watchdog++;
5642c42a7b7eSSam Leffler 	}
5643ef27340cSAdrian Chadd 
5644ef27340cSAdrian Chadd 	/*
5645ef27340cSAdrian Chadd 	 * We can't hold the lock across the ath_reset() call.
5646d52f7132SAdrian Chadd 	 *
5647d52f7132SAdrian Chadd 	 * And since this routine can't hold a lock and sleep,
5648d52f7132SAdrian Chadd 	 * do the reset deferred.
5649ef27340cSAdrian Chadd 	 */
5650ef27340cSAdrian Chadd 	if (do_reset) {
5651d52f7132SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_resettask);
5652ef27340cSAdrian Chadd 	}
5653ef27340cSAdrian Chadd 
56542e986da5SSam Leffler 	callout_schedule(&sc->sc_wd_ch, hz);
5655c42a7b7eSSam Leffler }
5656c42a7b7eSSam Leffler 
5657b8f2a853SAdrian Chadd /*
5658b8f2a853SAdrian Chadd  * Fetch the rate control statistics for the given node.
5659b8f2a853SAdrian Chadd  */
5660b8f2a853SAdrian Chadd static int
5661b8f2a853SAdrian Chadd ath_ioctl_ratestats(struct ath_softc *sc, struct ath_rateioctl *rs)
5662b8f2a853SAdrian Chadd {
5663b8f2a853SAdrian Chadd 	struct ath_node *an;
5664b8f2a853SAdrian Chadd 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
5665b8f2a853SAdrian Chadd 	struct ieee80211_node *ni;
5666b8f2a853SAdrian Chadd 	int error = 0;
5667b8f2a853SAdrian Chadd 
5668b8f2a853SAdrian Chadd 	/* Perform a lookup on the given node */
5669b8f2a853SAdrian Chadd 	ni = ieee80211_find_node(&ic->ic_sta, rs->is_u.macaddr);
5670b8f2a853SAdrian Chadd 	if (ni == NULL) {
5671b8f2a853SAdrian Chadd 		error = EINVAL;
5672b8f2a853SAdrian Chadd 		goto bad;
5673b8f2a853SAdrian Chadd 	}
5674b8f2a853SAdrian Chadd 
5675b8f2a853SAdrian Chadd 	/* Lock the ath_node */
5676b8f2a853SAdrian Chadd 	an = ATH_NODE(ni);
5677b8f2a853SAdrian Chadd 	ATH_NODE_LOCK(an);
5678b8f2a853SAdrian Chadd 
5679b8f2a853SAdrian Chadd 	/* Fetch the rate control stats for this node */
5680b8f2a853SAdrian Chadd 	error = ath_rate_fetch_node_stats(sc, an, rs);
5681b8f2a853SAdrian Chadd 
5682b8f2a853SAdrian Chadd 	/* No matter what happens here, just drop through */
5683b8f2a853SAdrian Chadd 
5684b8f2a853SAdrian Chadd 	/* Unlock the ath_node */
5685b8f2a853SAdrian Chadd 	ATH_NODE_UNLOCK(an);
5686b8f2a853SAdrian Chadd 
5687b8f2a853SAdrian Chadd 	/* Unref the node */
5688b8f2a853SAdrian Chadd 	ieee80211_node_decref(ni);
5689b8f2a853SAdrian Chadd 
5690b8f2a853SAdrian Chadd bad:
5691b8f2a853SAdrian Chadd 	return (error);
5692b8f2a853SAdrian Chadd }
5693b8f2a853SAdrian Chadd 
5694a585a9a1SSam Leffler #ifdef ATH_DIAGAPI
5695c42a7b7eSSam Leffler /*
5696c42a7b7eSSam Leffler  * Diagnostic interface to the HAL.  This is used by various
5697c42a7b7eSSam Leffler  * tools to do things like retrieve register contents for
5698c42a7b7eSSam Leffler  * debugging.  The mechanism is intentionally opaque so that
5699c42a7b7eSSam Leffler  * it can change frequently w/o concern for compatiblity.
5700c42a7b7eSSam Leffler  */
5701c42a7b7eSSam Leffler static int
5702c42a7b7eSSam Leffler ath_ioctl_diag(struct ath_softc *sc, struct ath_diag *ad)
5703c42a7b7eSSam Leffler {
5704c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5705c42a7b7eSSam Leffler 	u_int id = ad->ad_id & ATH_DIAG_ID;
5706c42a7b7eSSam Leffler 	void *indata = NULL;
5707c42a7b7eSSam Leffler 	void *outdata = NULL;
5708c42a7b7eSSam Leffler 	u_int32_t insize = ad->ad_in_size;
5709c42a7b7eSSam Leffler 	u_int32_t outsize = ad->ad_out_size;
5710c42a7b7eSSam Leffler 	int error = 0;
5711c42a7b7eSSam Leffler 
5712c42a7b7eSSam Leffler 	if (ad->ad_id & ATH_DIAG_IN) {
5713c42a7b7eSSam Leffler 		/*
5714c42a7b7eSSam Leffler 		 * Copy in data.
5715c42a7b7eSSam Leffler 		 */
5716c42a7b7eSSam Leffler 		indata = malloc(insize, M_TEMP, M_NOWAIT);
5717c42a7b7eSSam Leffler 		if (indata == NULL) {
5718c42a7b7eSSam Leffler 			error = ENOMEM;
5719c42a7b7eSSam Leffler 			goto bad;
5720c42a7b7eSSam Leffler 		}
5721c42a7b7eSSam Leffler 		error = copyin(ad->ad_in_data, indata, insize);
5722c42a7b7eSSam Leffler 		if (error)
5723c42a7b7eSSam Leffler 			goto bad;
5724c42a7b7eSSam Leffler 	}
5725c42a7b7eSSam Leffler 	if (ad->ad_id & ATH_DIAG_DYN) {
5726c42a7b7eSSam Leffler 		/*
5727c42a7b7eSSam Leffler 		 * Allocate a buffer for the results (otherwise the HAL
5728c42a7b7eSSam Leffler 		 * returns a pointer to a buffer where we can read the
5729c42a7b7eSSam Leffler 		 * results).  Note that we depend on the HAL leaving this
5730c42a7b7eSSam Leffler 		 * pointer for us to use below in reclaiming the buffer;
5731c42a7b7eSSam Leffler 		 * may want to be more defensive.
5732c42a7b7eSSam Leffler 		 */
5733c42a7b7eSSam Leffler 		outdata = malloc(outsize, M_TEMP, M_NOWAIT);
5734c42a7b7eSSam Leffler 		if (outdata == NULL) {
5735c42a7b7eSSam Leffler 			error = ENOMEM;
5736c42a7b7eSSam Leffler 			goto bad;
5737c42a7b7eSSam Leffler 		}
5738c42a7b7eSSam Leffler 	}
5739c42a7b7eSSam Leffler 	if (ath_hal_getdiagstate(ah, id, indata, insize, &outdata, &outsize)) {
5740c42a7b7eSSam Leffler 		if (outsize < ad->ad_out_size)
5741c42a7b7eSSam Leffler 			ad->ad_out_size = outsize;
5742c42a7b7eSSam Leffler 		if (outdata != NULL)
5743c42a7b7eSSam Leffler 			error = copyout(outdata, ad->ad_out_data,
5744c42a7b7eSSam Leffler 					ad->ad_out_size);
5745c42a7b7eSSam Leffler 	} else {
5746c42a7b7eSSam Leffler 		error = EINVAL;
5747c42a7b7eSSam Leffler 	}
5748c42a7b7eSSam Leffler bad:
5749c42a7b7eSSam Leffler 	if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
5750c42a7b7eSSam Leffler 		free(indata, M_TEMP);
5751c42a7b7eSSam Leffler 	if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
5752c42a7b7eSSam Leffler 		free(outdata, M_TEMP);
5753c42a7b7eSSam Leffler 	return error;
5754c42a7b7eSSam Leffler }
5755a585a9a1SSam Leffler #endif /* ATH_DIAGAPI */
5756c42a7b7eSSam Leffler 
5757c42a7b7eSSam Leffler static int
5758c42a7b7eSSam Leffler ath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
5759c42a7b7eSSam Leffler {
5760c42a7b7eSSam Leffler #define	IS_RUNNING(ifp) \
576113f4c340SRobert Watson 	((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
5762c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
5763b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
5764c42a7b7eSSam Leffler 	struct ifreq *ifr = (struct ifreq *)data;
576584784be1SSam Leffler 	const HAL_RATE_TABLE *rt;
5766c42a7b7eSSam Leffler 	int error = 0;
5767c42a7b7eSSam Leffler 
5768c42a7b7eSSam Leffler 	switch (cmd) {
5769c42a7b7eSSam Leffler 	case SIOCSIFFLAGS:
577031a8c1edSAndrew Thompson 		ATH_LOCK(sc);
5771c42a7b7eSSam Leffler 		if (IS_RUNNING(ifp)) {
5772c42a7b7eSSam Leffler 			/*
5773c42a7b7eSSam Leffler 			 * To avoid rescanning another access point,
5774c42a7b7eSSam Leffler 			 * do not call ath_init() here.  Instead,
5775c42a7b7eSSam Leffler 			 * only reflect promisc mode settings.
5776c42a7b7eSSam Leffler 			 */
5777c42a7b7eSSam Leffler 			ath_mode_init(sc);
5778c42a7b7eSSam Leffler 		} else if (ifp->if_flags & IFF_UP) {
5779c42a7b7eSSam Leffler 			/*
5780c42a7b7eSSam Leffler 			 * Beware of being called during attach/detach
5781c42a7b7eSSam Leffler 			 * to reset promiscuous mode.  In that case we
5782c42a7b7eSSam Leffler 			 * will still be marked UP but not RUNNING.
5783c42a7b7eSSam Leffler 			 * However trying to re-init the interface
5784c42a7b7eSSam Leffler 			 * is the wrong thing to do as we've already
5785c42a7b7eSSam Leffler 			 * torn down much of our state.  There's
5786c42a7b7eSSam Leffler 			 * probably a better way to deal with this.
5787c42a7b7eSSam Leffler 			 */
5788b032f27cSSam Leffler 			if (!sc->sc_invalid)
5789fc74a9f9SBrooks Davis 				ath_init(sc);	/* XXX lose error */
5790d3ac945bSSam Leffler 		} else {
5791c42a7b7eSSam Leffler 			ath_stop_locked(ifp);
5792d3ac945bSSam Leffler #ifdef notyet
5793d3ac945bSSam Leffler 			/* XXX must wakeup in places like ath_vap_delete */
5794d3ac945bSSam Leffler 			if (!sc->sc_invalid)
5795d3ac945bSSam Leffler 				ath_hal_setpower(sc->sc_ah, HAL_PM_FULL_SLEEP);
5796d3ac945bSSam Leffler #endif
5797d3ac945bSSam Leffler 		}
579831a8c1edSAndrew Thompson 		ATH_UNLOCK(sc);
5799c42a7b7eSSam Leffler 		break;
5800b032f27cSSam Leffler 	case SIOCGIFMEDIA:
5801b032f27cSSam Leffler 	case SIOCSIFMEDIA:
5802b032f27cSSam Leffler 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
5803b032f27cSSam Leffler 		break;
5804c42a7b7eSSam Leffler 	case SIOCGATHSTATS:
5805c42a7b7eSSam Leffler 		/* NB: embed these numbers to get a consistent view */
5806c42a7b7eSSam Leffler 		sc->sc_stats.ast_tx_packets = ifp->if_opackets;
5807c42a7b7eSSam Leffler 		sc->sc_stats.ast_rx_packets = ifp->if_ipackets;
580884784be1SSam Leffler 		sc->sc_stats.ast_tx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgtxrssi);
580984784be1SSam Leffler 		sc->sc_stats.ast_rx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgrssi);
5810584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
581110ad9a77SSam Leffler 		sc->sc_stats.ast_tdma_tsfadjp = TDMA_AVG(sc->sc_avgtsfdeltap);
581210ad9a77SSam Leffler 		sc->sc_stats.ast_tdma_tsfadjm = TDMA_AVG(sc->sc_avgtsfdeltam);
581310ad9a77SSam Leffler #endif
581484784be1SSam Leffler 		rt = sc->sc_currates;
581546d4d74cSSam Leffler 		sc->sc_stats.ast_tx_rate =
581646d4d74cSSam Leffler 		    rt->info[sc->sc_txrix].dot11Rate &~ IEEE80211_RATE_BASIC;
58176aa113fdSAdrian Chadd 		if (rt->info[sc->sc_txrix].phy & IEEE80211_T_HT)
58186aa113fdSAdrian Chadd 			sc->sc_stats.ast_tx_rate |= IEEE80211_RATE_MCS;
5819c42a7b7eSSam Leffler 		return copyout(&sc->sc_stats,
5820c42a7b7eSSam Leffler 		    ifr->ifr_data, sizeof (sc->sc_stats));
582194fe37d2SAdrian Chadd 	case SIOCGATHAGSTATS:
582294fe37d2SAdrian Chadd 		return copyout(&sc->sc_aggr_stats,
582394fe37d2SAdrian Chadd 		    ifr->ifr_data, sizeof (sc->sc_aggr_stats));
58243fc21fedSSam Leffler 	case SIOCZATHSTATS:
58253fc21fedSSam Leffler 		error = priv_check(curthread, PRIV_DRIVER);
58269467e3f3SAdrian Chadd 		if (error == 0) {
58273fc21fedSSam Leffler 			memset(&sc->sc_stats, 0, sizeof(sc->sc_stats));
582841b6b507SAdrian Chadd 			memset(&sc->sc_aggr_stats, 0,
582941b6b507SAdrian Chadd 			    sizeof(sc->sc_aggr_stats));
58309467e3f3SAdrian Chadd 			memset(&sc->sc_intr_stats, 0,
58319467e3f3SAdrian Chadd 			    sizeof(sc->sc_intr_stats));
58329467e3f3SAdrian Chadd 		}
58333fc21fedSSam Leffler 		break;
5834a585a9a1SSam Leffler #ifdef ATH_DIAGAPI
5835c42a7b7eSSam Leffler 	case SIOCGATHDIAG:
5836c42a7b7eSSam Leffler 		error = ath_ioctl_diag(sc, (struct ath_diag *) ifr);
5837c42a7b7eSSam Leffler 		break;
5838f51c84eaSAdrian Chadd 	case SIOCGATHPHYERR:
5839f51c84eaSAdrian Chadd 		error = ath_ioctl_phyerr(sc,(struct ath_diag*) ifr);
5840f51c84eaSAdrian Chadd 		break;
5841a585a9a1SSam Leffler #endif
58429af351f9SAdrian Chadd 	case SIOCGATHSPECTRAL:
58439af351f9SAdrian Chadd 		error = ath_ioctl_spectral(sc,(struct ath_diag*) ifr);
58449af351f9SAdrian Chadd 		break;
5845b8f2a853SAdrian Chadd 	case SIOCGATHNODERATESTATS:
5846b8f2a853SAdrian Chadd 		error = ath_ioctl_ratestats(sc, (struct ath_rateioctl *) ifr);
5847b8f2a853SAdrian Chadd 		break;
584831a8c1edSAndrew Thompson 	case SIOCGIFADDR:
5849b032f27cSSam Leffler 		error = ether_ioctl(ifp, cmd, data);
5850c42a7b7eSSam Leffler 		break;
585131a8c1edSAndrew Thompson 	default:
585231a8c1edSAndrew Thompson 		error = EINVAL;
585331a8c1edSAndrew Thompson 		break;
5854c42a7b7eSSam Leffler 	}
5855c42a7b7eSSam Leffler 	return error;
5856a614e076SSam Leffler #undef IS_RUNNING
5857c42a7b7eSSam Leffler }
5858c42a7b7eSSam Leffler 
5859c42a7b7eSSam Leffler /*
5860c42a7b7eSSam Leffler  * Announce various information on device/driver attach.
5861c42a7b7eSSam Leffler  */
5862c42a7b7eSSam Leffler static void
5863c42a7b7eSSam Leffler ath_announce(struct ath_softc *sc)
5864c42a7b7eSSam Leffler {
5865fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
5866c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5867c42a7b7eSSam Leffler 
5868498657cfSSam Leffler 	if_printf(ifp, "AR%s mac %d.%d RF%s phy %d.%d\n",
5869498657cfSSam Leffler 		ath_hal_mac_name(ah), ah->ah_macVersion, ah->ah_macRev,
5870498657cfSSam Leffler 		ath_hal_rf_name(ah), ah->ah_phyRev >> 4, ah->ah_phyRev & 0xf);
587146a924c4SAdrian Chadd 	if_printf(ifp, "2GHz radio: 0x%.4x; 5GHz radio: 0x%.4x\n",
587246a924c4SAdrian Chadd 		ah->ah_analog2GhzRev, ah->ah_analog5GhzRev);
5873c42a7b7eSSam Leffler 	if (bootverbose) {
5874c42a7b7eSSam Leffler 		int i;
5875c42a7b7eSSam Leffler 		for (i = 0; i <= WME_AC_VO; i++) {
5876c42a7b7eSSam Leffler 			struct ath_txq *txq = sc->sc_ac2q[i];
5877c42a7b7eSSam Leffler 			if_printf(ifp, "Use hw queue %u for %s traffic\n",
5878c42a7b7eSSam Leffler 				txq->axq_qnum, ieee80211_wme_acnames[i]);
5879c42a7b7eSSam Leffler 		}
5880c42a7b7eSSam Leffler 		if_printf(ifp, "Use hw queue %u for CAB traffic\n",
5881c42a7b7eSSam Leffler 			sc->sc_cabq->axq_qnum);
5882c42a7b7eSSam Leffler 		if_printf(ifp, "Use hw queue %u for beacons\n", sc->sc_bhalq);
5883c42a7b7eSSam Leffler 	}
5884e2d787faSSam Leffler 	if (ath_rxbuf != ATH_RXBUF)
5885e2d787faSSam Leffler 		if_printf(ifp, "using %u rx buffers\n", ath_rxbuf);
5886e2d787faSSam Leffler 	if (ath_txbuf != ATH_TXBUF)
5887e2d787faSSam Leffler 		if_printf(ifp, "using %u tx buffers\n", ath_txbuf);
58889ac01d39SRui Paulo 	if (sc->sc_mcastkey && bootverbose)
58899ac01d39SRui Paulo 		if_printf(ifp, "using multicast key search\n");
5890c42a7b7eSSam Leffler }
589110ad9a77SSam Leffler 
589248237774SAdrian Chadd static void
589348237774SAdrian Chadd ath_dfs_tasklet(void *p, int npending)
589448237774SAdrian Chadd {
589548237774SAdrian Chadd 	struct ath_softc *sc = (struct ath_softc *) p;
589648237774SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
589748237774SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
589848237774SAdrian Chadd 
589948237774SAdrian Chadd 	/*
590048237774SAdrian Chadd 	 * If previous processing has found a radar event,
590148237774SAdrian Chadd 	 * signal this to the net80211 layer to begin DFS
590248237774SAdrian Chadd 	 * processing.
590348237774SAdrian Chadd 	 */
590448237774SAdrian Chadd 	if (ath_dfs_process_radar_event(sc, sc->sc_curchan)) {
590548237774SAdrian Chadd 		/* DFS event found, initiate channel change */
590606fc4a10SAdrian Chadd 		/*
590706fc4a10SAdrian Chadd 		 * XXX doesn't currently tell us whether the event
590806fc4a10SAdrian Chadd 		 * XXX was found in the primary or extension
590906fc4a10SAdrian Chadd 		 * XXX channel!
591006fc4a10SAdrian Chadd 		 */
591106fc4a10SAdrian Chadd 		IEEE80211_LOCK(ic);
591248237774SAdrian Chadd 		ieee80211_dfs_notify_radar(ic, sc->sc_curchan);
591306fc4a10SAdrian Chadd 		IEEE80211_UNLOCK(ic);
591448237774SAdrian Chadd 	}
591548237774SAdrian Chadd }
591648237774SAdrian Chadd 
59170eb81626SAdrian Chadd /*
59180eb81626SAdrian Chadd  * Enable/disable power save.  This must be called with
59190eb81626SAdrian Chadd  * no TX driver locks currently held, so it should only
59200eb81626SAdrian Chadd  * be called from the RX path (which doesn't hold any
59210eb81626SAdrian Chadd  * TX driver locks.)
59220eb81626SAdrian Chadd  */
59230eb81626SAdrian Chadd static void
59240eb81626SAdrian Chadd ath_node_powersave(struct ieee80211_node *ni, int enable)
59250eb81626SAdrian Chadd {
5926bdbb6e5bSAdrian Chadd #ifdef	ATH_SW_PSQ
59270eb81626SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
59280eb81626SAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
59290eb81626SAdrian Chadd 	struct ath_softc *sc = ic->ic_ifp->if_softc;
59300eb81626SAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
59310eb81626SAdrian Chadd 
59320eb81626SAdrian Chadd 	/* XXX and no TXQ locks should be held here */
59330eb81626SAdrian Chadd 
59349b48fb4bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE, "%s: %6D: enable=%d\n",
59359b48fb4bSAdrian Chadd 	    __func__,
59369b48fb4bSAdrian Chadd 	    ni->ni_macaddr,
59379b48fb4bSAdrian Chadd 	    ":",
59389b48fb4bSAdrian Chadd 	    !! enable);
59390eb81626SAdrian Chadd 
59400eb81626SAdrian Chadd 	/* Suspend or resume software queue handling */
59410eb81626SAdrian Chadd 	if (enable)
59420eb81626SAdrian Chadd 		ath_tx_node_sleep(sc, an);
59430eb81626SAdrian Chadd 	else
59440eb81626SAdrian Chadd 		ath_tx_node_wakeup(sc, an);
59450eb81626SAdrian Chadd 
59460eb81626SAdrian Chadd 	/* Update net80211 state */
59470eb81626SAdrian Chadd 	avp->av_node_ps(ni, enable);
5948bdbb6e5bSAdrian Chadd #else
5949bdbb6e5bSAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
5950bdbb6e5bSAdrian Chadd 
5951bdbb6e5bSAdrian Chadd 	/* Update net80211 state */
5952bdbb6e5bSAdrian Chadd 	avp->av_node_ps(ni, enable);
5953bdbb6e5bSAdrian Chadd #endif/* ATH_SW_PSQ */
59540eb81626SAdrian Chadd }
59550eb81626SAdrian Chadd 
5956548a605dSAdrian Chadd /*
5957548a605dSAdrian Chadd  * Notification from net80211 that the powersave queue state has
5958548a605dSAdrian Chadd  * changed.
5959548a605dSAdrian Chadd  *
5960548a605dSAdrian Chadd  * Since the software queue also may have some frames:
5961548a605dSAdrian Chadd  *
5962548a605dSAdrian Chadd  * + if the node software queue has frames and the TID state
5963548a605dSAdrian Chadd  *   is 0, we set the TIM;
5964548a605dSAdrian Chadd  * + if the node and the stack are both empty, we clear the TIM bit.
5965548a605dSAdrian Chadd  * + If the stack tries to set the bit, always set it.
5966548a605dSAdrian Chadd  * + If the stack tries to clear the bit, only clear it if the
5967548a605dSAdrian Chadd  *   software queue in question is also cleared.
5968548a605dSAdrian Chadd  *
5969548a605dSAdrian Chadd  * TODO: this is called during node teardown; so let's ensure this
5970548a605dSAdrian Chadd  * is all correctly handled and that the TIM bit is cleared.
5971548a605dSAdrian Chadd  * It may be that the node flush is called _AFTER_ the net80211
5972548a605dSAdrian Chadd  * stack clears the TIM.
5973548a605dSAdrian Chadd  *
5974548a605dSAdrian Chadd  * Here is the racy part.  Since it's possible >1 concurrent,
5975548a605dSAdrian Chadd  * overlapping TXes will appear complete with a TX completion in
5976548a605dSAdrian Chadd  * another thread, it's possible that the concurrent TIM calls will
5977548a605dSAdrian Chadd  * clash.  We can't hold the node lock here because setting the
5978548a605dSAdrian Chadd  * TIM grabs the net80211 comlock and this may cause a LOR.
5979548a605dSAdrian Chadd  * The solution is either to totally serialise _everything_ at
5980548a605dSAdrian Chadd  * this point (ie, all TX, completion and any reset/flush go into
5981548a605dSAdrian Chadd  * one taskqueue) or a new "ath TIM lock" needs to be created that
5982548a605dSAdrian Chadd  * just wraps the driver state change and this call to avp->av_set_tim().
5983548a605dSAdrian Chadd  *
5984548a605dSAdrian Chadd  * The same race exists in the net80211 power save queue handling
5985548a605dSAdrian Chadd  * as well.  Since multiple transmitting threads may queue frames
5986548a605dSAdrian Chadd  * into the driver, as well as ps-poll and the driver transmitting
5987548a605dSAdrian Chadd  * frames (and thus clearing the psq), it's quite possible that
5988548a605dSAdrian Chadd  * a packet entering the PSQ and a ps-poll being handled will
5989548a605dSAdrian Chadd  * race, causing the TIM to be cleared and not re-set.
5990548a605dSAdrian Chadd  */
5991548a605dSAdrian Chadd static int
5992548a605dSAdrian Chadd ath_node_set_tim(struct ieee80211_node *ni, int enable)
5993548a605dSAdrian Chadd {
5994bdbb6e5bSAdrian Chadd #ifdef	ATH_SW_PSQ
5995548a605dSAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
5996548a605dSAdrian Chadd 	struct ath_softc *sc = ic->ic_ifp->if_softc;
5997548a605dSAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
5998548a605dSAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
5999548a605dSAdrian Chadd 	int changed = 0;
6000548a605dSAdrian Chadd 
60014bed2b67SAdrian Chadd 	ATH_TX_LOCK(sc);
6002548a605dSAdrian Chadd 	an->an_stack_psq = enable;
6003548a605dSAdrian Chadd 
6004548a605dSAdrian Chadd 	/*
6005548a605dSAdrian Chadd 	 * This will get called for all operating modes,
6006548a605dSAdrian Chadd 	 * even if avp->av_set_tim is unset.
6007548a605dSAdrian Chadd 	 * It's currently set for hostap/ibss modes; but
6008548a605dSAdrian Chadd 	 * the same infrastructure is used for both STA
6009548a605dSAdrian Chadd 	 * and AP/IBSS node power save.
6010548a605dSAdrian Chadd 	 */
6011548a605dSAdrian Chadd 	if (avp->av_set_tim == NULL) {
60124bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6013548a605dSAdrian Chadd 		return (0);
6014548a605dSAdrian Chadd 	}
6015548a605dSAdrian Chadd 
6016548a605dSAdrian Chadd 	/*
6017548a605dSAdrian Chadd 	 * If setting the bit, always set it here.
6018548a605dSAdrian Chadd 	 * If clearing the bit, only clear it if the
6019548a605dSAdrian Chadd 	 * software queue is also empty.
6020548a605dSAdrian Chadd 	 *
6021548a605dSAdrian Chadd 	 * If the node has left power save, just clear the TIM
6022548a605dSAdrian Chadd 	 * bit regardless of the state of the power save queue.
6023548a605dSAdrian Chadd 	 *
6024548a605dSAdrian Chadd 	 * XXX TODO: although atomics are used, it's quite possible
6025548a605dSAdrian Chadd 	 * that a race will occur between this and setting/clearing
6026548a605dSAdrian Chadd 	 * in another thread.  TX completion will occur always in
6027548a605dSAdrian Chadd 	 * one thread, however setting/clearing the TIM bit can come
6028548a605dSAdrian Chadd 	 * from a variety of different process contexts!
6029548a605dSAdrian Chadd 	 */
6030548a605dSAdrian Chadd 	if (enable && an->an_tim_set == 1) {
6031548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
60329b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, tim_set=1, ignoring\n",
60339b48fb4bSAdrian Chadd 		    __func__,
60349b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
60359b48fb4bSAdrian Chadd 		    ":",
60369b48fb4bSAdrian Chadd 		    enable);
60374bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6038548a605dSAdrian Chadd 	} else if (enable) {
6039548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
60409b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, enabling TIM\n",
60419b48fb4bSAdrian Chadd 		    __func__,
60429b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
60439b48fb4bSAdrian Chadd 		    ":",
60449b48fb4bSAdrian Chadd 		    enable);
6045548a605dSAdrian Chadd 		an->an_tim_set = 1;
60464bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6047548a605dSAdrian Chadd 		changed = avp->av_set_tim(ni, enable);
6048ba83edd4SAdrian Chadd 	} else if (an->an_swq_depth == 0) {
6049548a605dSAdrian Chadd 		/* disable */
6050548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
60519b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, an_swq_depth == 0, disabling\n",
60529b48fb4bSAdrian Chadd 		    __func__,
60539b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
60549b48fb4bSAdrian Chadd 		    ":",
60559b48fb4bSAdrian Chadd 		    enable);
6056548a605dSAdrian Chadd 		an->an_tim_set = 0;
60574bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6058548a605dSAdrian Chadd 		changed = avp->av_set_tim(ni, enable);
6059548a605dSAdrian Chadd 	} else if (! an->an_is_powersave) {
6060548a605dSAdrian Chadd 		/*
6061548a605dSAdrian Chadd 		 * disable regardless; the node isn't in powersave now
6062548a605dSAdrian Chadd 		 */
6063548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
60649b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, an_pwrsave=0, disabling\n",
60659b48fb4bSAdrian Chadd 		    __func__,
60669b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
60679b48fb4bSAdrian Chadd 		    ":",
60689b48fb4bSAdrian Chadd 		    enable);
6069548a605dSAdrian Chadd 		an->an_tim_set = 0;
60704bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6071548a605dSAdrian Chadd 		changed = avp->av_set_tim(ni, enable);
6072548a605dSAdrian Chadd 	} else {
6073548a605dSAdrian Chadd 		/*
6074548a605dSAdrian Chadd 		 * psq disable, node is currently in powersave, node
6075548a605dSAdrian Chadd 		 * software queue isn't empty, so don't clear the TIM bit
6076548a605dSAdrian Chadd 		 * for now.
6077548a605dSAdrian Chadd 		 */
60784bed2b67SAdrian Chadd 		ATH_TX_UNLOCK(sc);
6079548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
60809b48fb4bSAdrian Chadd 		    "%s: %6D: enable=%d, an_swq_depth > 0, ignoring\n",
60819b48fb4bSAdrian Chadd 		    __func__,
60829b48fb4bSAdrian Chadd 		    ni->ni_macaddr,
60839b48fb4bSAdrian Chadd 		    ":",
60849b48fb4bSAdrian Chadd 		    enable);
6085548a605dSAdrian Chadd 		changed = 0;
6086548a605dSAdrian Chadd 	}
6087548a605dSAdrian Chadd 
6088548a605dSAdrian Chadd 	return (changed);
6089bdbb6e5bSAdrian Chadd #else
6090bdbb6e5bSAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
6091bdbb6e5bSAdrian Chadd 
609260328038SAdrian Chadd 	/*
6093661c81c3SBaptiste Daroussin 	 * Some operating modes don't set av_set_tim(), so don't
609460328038SAdrian Chadd 	 * update it here.
609560328038SAdrian Chadd 	 */
609660328038SAdrian Chadd 	if (avp->av_set_tim == NULL)
609760328038SAdrian Chadd 		return (0);
609860328038SAdrian Chadd 
6099bdbb6e5bSAdrian Chadd 	return (avp->av_set_tim(ni, enable));
6100bdbb6e5bSAdrian Chadd #endif /* ATH_SW_PSQ */
6101548a605dSAdrian Chadd }
6102548a605dSAdrian Chadd 
6103548a605dSAdrian Chadd /*
6104548a605dSAdrian Chadd  * Set or update the TIM from the software queue.
6105548a605dSAdrian Chadd  *
6106548a605dSAdrian Chadd  * Check the software queue depth before attempting to do lock
6107548a605dSAdrian Chadd  * anything; that avoids trying to obtain the lock.  Then,
6108548a605dSAdrian Chadd  * re-check afterwards to ensure nothing has changed in the
6109548a605dSAdrian Chadd  * meantime.
6110548a605dSAdrian Chadd  *
6111548a605dSAdrian Chadd  * set:   This is designed to be called from the TX path, after
6112548a605dSAdrian Chadd  *        a frame has been queued; to see if the swq > 0.
6113548a605dSAdrian Chadd  *
6114548a605dSAdrian Chadd  * clear: This is designed to be called from the buffer completion point
6115548a605dSAdrian Chadd  *        (right now it's ath_tx_default_comp()) where the state of
6116548a605dSAdrian Chadd  *        a software queue has changed.
6117548a605dSAdrian Chadd  *
6118548a605dSAdrian Chadd  * It makes sense to place it at buffer free / completion rather
6119548a605dSAdrian Chadd  * than after each software queue operation, as there's no real
6120548a605dSAdrian Chadd  * point in churning the TIM bit as the last frames in the software
6121548a605dSAdrian Chadd  * queue are transmitted.  If they fail and we retry them, we'd
6122548a605dSAdrian Chadd  * just be setting the TIM bit again anyway.
6123548a605dSAdrian Chadd  */
6124548a605dSAdrian Chadd void
6125548a605dSAdrian Chadd ath_tx_update_tim(struct ath_softc *sc, struct ieee80211_node *ni,
6126548a605dSAdrian Chadd      int enable)
6127548a605dSAdrian Chadd {
6128bdbb6e5bSAdrian Chadd #ifdef	ATH_SW_PSQ
6129548a605dSAdrian Chadd 	struct ath_node *an;
6130548a605dSAdrian Chadd 	struct ath_vap *avp;
6131548a605dSAdrian Chadd 
6132548a605dSAdrian Chadd 	/* Don't do this for broadcast/etc frames */
6133548a605dSAdrian Chadd 	if (ni == NULL)
6134548a605dSAdrian Chadd 		return;
6135548a605dSAdrian Chadd 
6136548a605dSAdrian Chadd 	an = ATH_NODE(ni);
6137548a605dSAdrian Chadd 	avp = ATH_VAP(ni->ni_vap);
6138548a605dSAdrian Chadd 
6139548a605dSAdrian Chadd 	/*
6140548a605dSAdrian Chadd 	 * And for operating modes without the TIM handler set, let's
6141548a605dSAdrian Chadd 	 * just skip those.
6142548a605dSAdrian Chadd 	 */
6143548a605dSAdrian Chadd 	if (avp->av_set_tim == NULL)
6144548a605dSAdrian Chadd 		return;
6145548a605dSAdrian Chadd 
61464bed2b67SAdrian Chadd 	ATH_TX_LOCK_ASSERT(sc);
6147548a605dSAdrian Chadd 
6148548a605dSAdrian Chadd 	if (enable) {
6149548a605dSAdrian Chadd 		if (an->an_is_powersave &&
6150548a605dSAdrian Chadd 		    an->an_tim_set == 0 &&
6151ba83edd4SAdrian Chadd 		    an->an_swq_depth != 0) {
6152548a605dSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
61539b48fb4bSAdrian Chadd 			    "%s: %6D: swq_depth>0, tim_set=0, set!\n",
61549b48fb4bSAdrian Chadd 			    __func__,
61559b48fb4bSAdrian Chadd 			    ni->ni_macaddr,
61569b48fb4bSAdrian Chadd 			    ":");
6157548a605dSAdrian Chadd 			an->an_tim_set = 1;
6158548a605dSAdrian Chadd 			(void) avp->av_set_tim(ni, 1);
6159548a605dSAdrian Chadd 		}
6160548a605dSAdrian Chadd 	} else {
6161548a605dSAdrian Chadd 		/*
6162548a605dSAdrian Chadd 		 * Don't bother grabbing the lock unless the queue is empty.
6163548a605dSAdrian Chadd 		 */
6164ba83edd4SAdrian Chadd 		if (&an->an_swq_depth != 0)
6165548a605dSAdrian Chadd 			return;
6166548a605dSAdrian Chadd 
6167548a605dSAdrian Chadd 		if (an->an_is_powersave &&
6168548a605dSAdrian Chadd 		    an->an_stack_psq == 0 &&
6169548a605dSAdrian Chadd 		    an->an_tim_set == 1 &&
6170ba83edd4SAdrian Chadd 		    an->an_swq_depth == 0) {
6171548a605dSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
6172548a605dSAdrian Chadd 			    "%s: an=%p, swq_depth=0, tim_set=1, psq_set=0,"
6173548a605dSAdrian Chadd 			    " clear!\n",
6174548a605dSAdrian Chadd 			    __func__, an);
6175548a605dSAdrian Chadd 			an->an_tim_set = 0;
6176548a605dSAdrian Chadd 			(void) avp->av_set_tim(ni, 0);
6177548a605dSAdrian Chadd 		}
6178548a605dSAdrian Chadd 	}
6179bdbb6e5bSAdrian Chadd #else
6180bdbb6e5bSAdrian Chadd 	return;
6181bdbb6e5bSAdrian Chadd #endif	/* ATH_SW_PSQ */
6182548a605dSAdrian Chadd }
61830eb81626SAdrian Chadd 
6184dba9c859SAdrian Chadd MODULE_VERSION(if_ath, 1);
6185dba9c859SAdrian Chadd MODULE_DEPEND(if_ath, wlan, 1, 1, 1);          /* 802.11 media layer */
618658816f3fSAdrian Chadd #if	defined(IEEE80211_ALQ) || defined(AH_DEBUG_ALQ)
618758816f3fSAdrian Chadd MODULE_DEPEND(if_ath, alq, 1, 1, 1);
618858816f3fSAdrian Chadd #endif
6189