xref: /freebsd/sys/dev/ath/if_ath.c (revision 548a605d0dd69900862afa70b0ea4338600f08ce)
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>
11448237774SAdrian Chadd #include <dev/ath/if_athdfs.h>
1155bc8125aSAdrian Chadd 
11686e07743SSam Leffler #ifdef ATH_TX99_DIAG
11786e07743SSam Leffler #include <dev/ath/ath_tx99/ath_tx99.h>
11886e07743SSam Leffler #endif
11986e07743SSam Leffler 
120b032f27cSSam Leffler /*
121b032f27cSSam Leffler  * ATH_BCBUF determines the number of vap's that can transmit
122b032f27cSSam Leffler  * beacons and also (currently) the number of vap's that can
123b032f27cSSam Leffler  * have unique mac addresses/bssid.  When staggering beacons
124b032f27cSSam Leffler  * 4 is probably a good max as otherwise the beacons become
125b032f27cSSam Leffler  * very closely spaced and there is limited time for cab q traffic
126b032f27cSSam Leffler  * to go out.  You can burst beacons instead but that is not good
127b032f27cSSam Leffler  * for stations in power save and at some point you really want
128b032f27cSSam Leffler  * another radio (and channel).
129b032f27cSSam Leffler  *
130b032f27cSSam Leffler  * The limit on the number of mac addresses is tied to our use of
131b032f27cSSam Leffler  * the U/L bit and tracking addresses in a byte; it would be
132b032f27cSSam Leffler  * worthwhile to allow more for applications like proxy sta.
133b032f27cSSam Leffler  */
134b032f27cSSam Leffler CTASSERT(ATH_BCBUF <= 8);
135b032f27cSSam Leffler 
136b032f27cSSam Leffler static struct ieee80211vap *ath_vap_create(struct ieee80211com *,
137fcd9500fSBernhard Schmidt 		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
138fcd9500fSBernhard Schmidt 		    const uint8_t [IEEE80211_ADDR_LEN],
139fcd9500fSBernhard Schmidt 		    const uint8_t [IEEE80211_ADDR_LEN]);
140b032f27cSSam Leffler static void	ath_vap_delete(struct ieee80211vap *);
1415591b213SSam Leffler static void	ath_init(void *);
142c42a7b7eSSam Leffler static void	ath_stop_locked(struct ifnet *);
1435591b213SSam Leffler static void	ath_stop(struct ifnet *);
144b032f27cSSam Leffler static int	ath_reset_vap(struct ieee80211vap *, u_long);
1458e739394SAdrian Chadd static void	ath_start_queue(struct ifnet *ifp);
1465591b213SSam Leffler static int	ath_media_change(struct ifnet *);
1472e986da5SSam Leffler static void	ath_watchdog(void *);
1485591b213SSam Leffler static int	ath_ioctl(struct ifnet *, u_long, caddr_t);
1495591b213SSam Leffler static void	ath_fatal_proc(void *, int);
150b032f27cSSam Leffler static void	ath_bmiss_vap(struct ieee80211vap *);
1515591b213SSam Leffler static void	ath_bmiss_proc(void *, int);
152b032f27cSSam Leffler static void	ath_key_update_begin(struct ieee80211vap *);
153b032f27cSSam Leffler static void	ath_key_update_end(struct ieee80211vap *);
154b032f27cSSam Leffler static void	ath_update_mcast(struct ifnet *);
155b032f27cSSam Leffler static void	ath_update_promisc(struct ifnet *);
156c42a7b7eSSam Leffler static void	ath_updateslot(struct ifnet *);
157c42a7b7eSSam Leffler static void	ath_bstuck_proc(void *, int);
158d52f7132SAdrian Chadd static void	ath_reset_proc(void *, int);
1595591b213SSam Leffler static int	ath_desc_alloc(struct ath_softc *);
1605591b213SSam Leffler static void	ath_desc_free(struct ath_softc *);
16138c208f8SSam Leffler static struct ieee80211_node *ath_node_alloc(struct ieee80211vap *,
16238c208f8SSam Leffler 			const uint8_t [IEEE80211_ADDR_LEN]);
1634afa805eSAdrian Chadd static void	ath_node_cleanup(struct ieee80211_node *);
164c42a7b7eSSam Leffler static void	ath_node_free(struct ieee80211_node *);
16568e8e04eSSam Leffler static void	ath_node_getsignal(const struct ieee80211_node *,
16668e8e04eSSam Leffler 			int8_t *, int8_t *);
167622b3fd2SSam Leffler static void	ath_txq_init(struct ath_softc *sc, struct ath_txq *, int);
168c42a7b7eSSam Leffler static struct ath_txq *ath_txq_setup(struct ath_softc*, int qtype, int subtype);
169c42a7b7eSSam Leffler static int	ath_tx_setup(struct ath_softc *, int, int);
170c42a7b7eSSam Leffler static void	ath_tx_cleanupq(struct ath_softc *, struct ath_txq *);
171c42a7b7eSSam Leffler static void	ath_tx_cleanup(struct ath_softc *);
172788e6aa9SAdrian Chadd static int	ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq,
173788e6aa9SAdrian Chadd 		    int dosched);
174c42a7b7eSSam Leffler static void	ath_tx_proc_q0(void *, int);
175c42a7b7eSSam Leffler static void	ath_tx_proc_q0123(void *, int);
1765591b213SSam Leffler static void	ath_tx_proc(void *, int);
17703e9308fSAdrian Chadd static void	ath_txq_sched_tasklet(void *, int);
1785591b213SSam Leffler static int	ath_chan_set(struct ath_softc *, struct ieee80211_channel *);
179c42a7b7eSSam Leffler static void	ath_chan_change(struct ath_softc *, struct ieee80211_channel *);
18068e8e04eSSam Leffler static void	ath_scan_start(struct ieee80211com *);
18168e8e04eSSam Leffler static void	ath_scan_end(struct ieee80211com *);
18268e8e04eSSam Leffler static void	ath_set_channel(struct ieee80211com *);
183fdd72b4aSAdrian Chadd #ifdef	ATH_ENABLE_11N
184e7200579SAdrian Chadd static void	ath_update_chw(struct ieee80211com *);
185fdd72b4aSAdrian Chadd #endif	/* ATH_ENABLE_11N */
1865591b213SSam Leffler static void	ath_calibrate(void *);
187b032f27cSSam Leffler static int	ath_newstate(struct ieee80211vap *, enum ieee80211_state, int);
188e8fd88a3SSam Leffler static void	ath_setup_stationkey(struct ieee80211_node *);
189e9962332SSam Leffler static void	ath_newassoc(struct ieee80211_node *, int);
190b032f27cSSam Leffler static int	ath_setregdomain(struct ieee80211com *,
191b032f27cSSam Leffler 		    struct ieee80211_regdomain *, int,
192b032f27cSSam Leffler 		    struct ieee80211_channel []);
1935fe9f044SSam Leffler static void	ath_getradiocaps(struct ieee80211com *, int, int *,
194b032f27cSSam Leffler 		    struct ieee80211_channel []);
195b032f27cSSam Leffler static int	ath_getchannels(struct ath_softc *);
1965591b213SSam Leffler 
197c42a7b7eSSam Leffler static int	ath_rate_setup(struct ath_softc *, u_int mode);
1985591b213SSam Leffler static void	ath_setcurmode(struct ath_softc *, enum ieee80211_phymode);
199c42a7b7eSSam Leffler 
200c42a7b7eSSam Leffler static void	ath_announce(struct ath_softc *);
2015591b213SSam Leffler 
20248237774SAdrian Chadd static void	ath_dfs_tasklet(void *, int);
2030eb81626SAdrian Chadd static void	ath_node_powersave(struct ieee80211_node *, int);
204*548a605dSAdrian Chadd static int	ath_node_set_tim(struct ieee80211_node *, int);
20548237774SAdrian Chadd 
206584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
207a35dae8dSAdrian Chadd #include <dev/ath/if_ath_tdma.h>
208a35dae8dSAdrian Chadd #endif
20910ad9a77SSam Leffler 
2105591b213SSam Leffler SYSCTL_DECL(_hw_ath);
2115591b213SSam Leffler 
2125591b213SSam Leffler /* XXX validate sysctl values */
2132dc7fcc4SSam Leffler static	int ath_longcalinterval = 30;		/* long cals every 30 secs */
2142dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, longcal, CTLFLAG_RW, &ath_longcalinterval,
2152dc7fcc4SSam Leffler 	    0, "long chip calibration interval (secs)");
2162dc7fcc4SSam Leffler static	int ath_shortcalinterval = 100;		/* short cals every 100 ms */
2172dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, shortcal, CTLFLAG_RW, &ath_shortcalinterval,
2182dc7fcc4SSam Leffler 	    0, "short chip calibration interval (msecs)");
2192dc7fcc4SSam Leffler static	int ath_resetcalinterval = 20*60;	/* reset cal state 20 mins */
2202dc7fcc4SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, resetcal, CTLFLAG_RW, &ath_resetcalinterval,
2212dc7fcc4SSam Leffler 	    0, "reset chip calibration results (secs)");
222a108ab63SAdrian Chadd static	int ath_anicalinterval = 100;		/* ANI calibration - 100 msec */
223a108ab63SAdrian Chadd SYSCTL_INT(_hw_ath, OID_AUTO, anical, CTLFLAG_RW, &ath_anicalinterval,
224a108ab63SAdrian Chadd 	    0, "ANI calibration (msecs)");
2255591b213SSam Leffler 
2263d184db2SAdrian Chadd int ath_rxbuf = ATH_RXBUF;		/* # rx buffers to allocate */
227aaa70f2fSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, rxbuf, CTLFLAG_RW, &ath_rxbuf,
228e2d787faSSam Leffler 	    0, "rx buffers allocated");
229e2d787faSSam Leffler TUNABLE_INT("hw.ath.rxbuf", &ath_rxbuf);
2303d184db2SAdrian Chadd int ath_txbuf = ATH_TXBUF;		/* # tx buffers to allocate */
231aaa70f2fSSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, txbuf, CTLFLAG_RW, &ath_txbuf,
232e2d787faSSam Leffler 	    0, "tx buffers allocated");
233e2d787faSSam Leffler TUNABLE_INT("hw.ath.txbuf", &ath_txbuf);
2343d184db2SAdrian Chadd int ath_txbuf_mgmt = ATH_MGMT_TXBUF;	/* # mgmt tx buffers to allocate */
235af33d486SAdrian Chadd SYSCTL_INT(_hw_ath, OID_AUTO, txbuf_mgmt, CTLFLAG_RW, &ath_txbuf_mgmt,
236af33d486SAdrian Chadd 	    0, "tx (mgmt) buffers allocated");
237af33d486SAdrian Chadd TUNABLE_INT("hw.ath.txbuf_mgmt", &ath_txbuf_mgmt);
238e2d787faSSam Leffler 
239a35dae8dSAdrian Chadd int ath_bstuck_threshold = 4;		/* max missed beacons */
240a32ac9d3SSam Leffler SYSCTL_INT(_hw_ath, OID_AUTO, bstuck, CTLFLAG_RW, &ath_bstuck_threshold,
241a32ac9d3SSam Leffler 	    0, "max missed beacon xmits before chip reset");
242a32ac9d3SSam Leffler 
2436b349e5aSAdrian Chadd MALLOC_DEFINE(M_ATHDEV, "athdev", "ath driver dma buffers");
244c42a7b7eSSam Leffler 
245f8418db5SAdrian Chadd void
246f8418db5SAdrian Chadd ath_legacy_attach_comp_func(struct ath_softc *sc)
247f8418db5SAdrian Chadd {
248f8418db5SAdrian Chadd 
249f8418db5SAdrian Chadd 	/*
250f8418db5SAdrian Chadd 	 * Special case certain configurations.  Note the
251f8418db5SAdrian Chadd 	 * CAB queue is handled by these specially so don't
252f8418db5SAdrian Chadd 	 * include them when checking the txq setup mask.
253f8418db5SAdrian Chadd 	 */
254f8418db5SAdrian Chadd 	switch (sc->sc_txqsetup &~ (1<<sc->sc_cabq->axq_qnum)) {
255f8418db5SAdrian Chadd 	case 0x01:
256f8418db5SAdrian Chadd 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0, sc);
257f8418db5SAdrian Chadd 		break;
258f8418db5SAdrian Chadd 	case 0x0f:
259f8418db5SAdrian Chadd 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0123, sc);
260f8418db5SAdrian Chadd 		break;
261f8418db5SAdrian Chadd 	default:
262f8418db5SAdrian Chadd 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc, sc);
263f8418db5SAdrian Chadd 		break;
264f8418db5SAdrian Chadd 	}
265f8418db5SAdrian Chadd }
266f8418db5SAdrian Chadd 
26767397d39SAdrian Chadd #define	HAL_MODE_HT20 (HAL_MODE_11NG_HT20 | HAL_MODE_11NA_HT20)
26867397d39SAdrian Chadd #define	HAL_MODE_HT40 \
26967397d39SAdrian Chadd 	(HAL_MODE_11NG_HT40PLUS | HAL_MODE_11NG_HT40MINUS | \
27067397d39SAdrian Chadd 	HAL_MODE_11NA_HT40PLUS | HAL_MODE_11NA_HT40MINUS)
2715591b213SSam Leffler int
2725591b213SSam Leffler ath_attach(u_int16_t devid, struct ath_softc *sc)
2735591b213SSam Leffler {
274fc74a9f9SBrooks Davis 	struct ifnet *ifp;
275b032f27cSSam Leffler 	struct ieee80211com *ic;
276fc74a9f9SBrooks Davis 	struct ath_hal *ah = NULL;
2775591b213SSam Leffler 	HAL_STATUS status;
278c42a7b7eSSam Leffler 	int error = 0, i;
279411373ebSSam Leffler 	u_int wmodes;
28029aca940SSam Leffler 	uint8_t macaddr[IEEE80211_ADDR_LEN];
281a865860dSAdrian Chadd 	int rx_chainmask, tx_chainmask;
2825591b213SSam Leffler 
283c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: devid 0x%x\n", __func__, devid);
2845591b213SSam Leffler 
285a93c5097SAdrian Chadd 	CURVNET_SET(vnet0);
286b032f27cSSam Leffler 	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
287fc74a9f9SBrooks Davis 	if (ifp == NULL) {
288fc74a9f9SBrooks Davis 		device_printf(sc->sc_dev, "can not if_alloc()\n");
289fc74a9f9SBrooks Davis 		error = ENOSPC;
290fc74a9f9SBrooks Davis 		goto bad;
291fc74a9f9SBrooks Davis 	}
292b032f27cSSam Leffler 	ic = ifp->if_l2com;
293fc74a9f9SBrooks Davis 
2945591b213SSam Leffler 	/* set these up early for if_printf use */
2959bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(sc->sc_dev),
2969bf40edeSBrooks Davis 		device_get_unit(sc->sc_dev));
297a93c5097SAdrian Chadd 	CURVNET_RESTORE();
2985591b213SSam Leffler 
2997e97436bSAdrian Chadd 	ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh,
3007e97436bSAdrian Chadd 	    sc->sc_eepromdata, &status);
3015591b213SSam Leffler 	if (ah == NULL) {
3025591b213SSam Leffler 		if_printf(ifp, "unable to attach hardware; HAL status %u\n",
3035591b213SSam Leffler 			status);
3045591b213SSam Leffler 		error = ENXIO;
3055591b213SSam Leffler 		goto bad;
3065591b213SSam Leffler 	}
3075591b213SSam Leffler 	sc->sc_ah = ah;
308b58b3803SSam Leffler 	sc->sc_invalid = 0;	/* ready to go, enable interrupt handling */
3093297be13SSam Leffler #ifdef	ATH_DEBUG
3103297be13SSam Leffler 	sc->sc_debug = ath_debug;
3113297be13SSam Leffler #endif
3125591b213SSam Leffler 
3135591b213SSam Leffler 	/*
314f8cc9b09SAdrian Chadd 	 * Setup the DMA/EDMA functions based on the current
315f8cc9b09SAdrian Chadd 	 * hardware support.
316f8cc9b09SAdrian Chadd 	 *
317f8cc9b09SAdrian Chadd 	 * This is required before the descriptors are allocated.
318f8cc9b09SAdrian Chadd 	 */
3193d184db2SAdrian Chadd 	if (ath_hal_hasedma(sc->sc_ah)) {
3203d184db2SAdrian Chadd 		sc->sc_isedma = 1;
321f8cc9b09SAdrian Chadd 		ath_recv_setup_edma(sc);
3223fdfc330SAdrian Chadd 		ath_xmit_setup_edma(sc);
3233fdfc330SAdrian Chadd 	} else {
324f8cc9b09SAdrian Chadd 		ath_recv_setup_legacy(sc);
3253fdfc330SAdrian Chadd 		ath_xmit_setup_legacy(sc);
3263fdfc330SAdrian Chadd 	}
327f8cc9b09SAdrian Chadd 
328f8cc9b09SAdrian Chadd 	/*
329c42a7b7eSSam Leffler 	 * Check if the MAC has multi-rate retry support.
330c42a7b7eSSam Leffler 	 * We do this by trying to setup a fake extended
331c42a7b7eSSam Leffler 	 * descriptor.  MAC's that don't have support will
332c42a7b7eSSam Leffler 	 * return false w/o doing anything.  MAC's that do
333c42a7b7eSSam Leffler 	 * support it will return true w/o doing anything.
334c42a7b7eSSam Leffler 	 */
335c42a7b7eSSam Leffler 	sc->sc_mrretry = ath_hal_setupxtxdesc(ah, NULL, 0,0, 0,0, 0,0);
336c42a7b7eSSam Leffler 
337c42a7b7eSSam Leffler 	/*
338c42a7b7eSSam Leffler 	 * Check if the device has hardware counters for PHY
339c42a7b7eSSam Leffler 	 * errors.  If so we need to enable the MIB interrupt
340c42a7b7eSSam Leffler 	 * so we can act on stat triggers.
341c42a7b7eSSam Leffler 	 */
342c42a7b7eSSam Leffler 	if (ath_hal_hwphycounters(ah))
343c42a7b7eSSam Leffler 		sc->sc_needmib = 1;
344c42a7b7eSSam Leffler 
345c42a7b7eSSam Leffler 	/*
346c42a7b7eSSam Leffler 	 * Get the hardware key cache size.
347c42a7b7eSSam Leffler 	 */
348c42a7b7eSSam Leffler 	sc->sc_keymax = ath_hal_keycachesize(ah);
349e8fd88a3SSam Leffler 	if (sc->sc_keymax > ATH_KEYMAX) {
350e8fd88a3SSam Leffler 		if_printf(ifp, "Warning, using only %u of %u key cache slots\n",
351e8fd88a3SSam Leffler 			ATH_KEYMAX, sc->sc_keymax);
352e8fd88a3SSam Leffler 		sc->sc_keymax = ATH_KEYMAX;
353c42a7b7eSSam Leffler 	}
354c42a7b7eSSam Leffler 	/*
355c42a7b7eSSam Leffler 	 * Reset the key cache since some parts do not
356c42a7b7eSSam Leffler 	 * reset the contents on initial power up.
357c42a7b7eSSam Leffler 	 */
358c42a7b7eSSam Leffler 	for (i = 0; i < sc->sc_keymax; i++)
359c42a7b7eSSam Leffler 		ath_hal_keyreset(ah, i);
360c42a7b7eSSam Leffler 
361c42a7b7eSSam Leffler 	/*
362b032f27cSSam Leffler 	 * Collect the default channel list.
3635591b213SSam Leffler 	 */
364b032f27cSSam Leffler 	error = ath_getchannels(sc);
3655591b213SSam Leffler 	if (error != 0)
3665591b213SSam Leffler 		goto bad;
3675591b213SSam Leffler 
3685591b213SSam Leffler 	/*
3695591b213SSam Leffler 	 * Setup rate tables for all potential media types.
3705591b213SSam Leffler 	 */
3715591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11A);
3725591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11B);
3735591b213SSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11G);
374c42a7b7eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_A);
375c42a7b7eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_G);
37668e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_STURBO_A);
37768e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11NA);
37868e8e04eSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_11NG);
379724c193aSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_HALF);
380724c193aSSam Leffler 	ath_rate_setup(sc, IEEE80211_MODE_QUARTER);
381aaa70f2fSSam Leffler 
382c42a7b7eSSam Leffler 	/* NB: setup here so ath_rate_update is happy */
383c42a7b7eSSam Leffler 	ath_setcurmode(sc, IEEE80211_MODE_11A);
3845591b213SSam Leffler 
385c42a7b7eSSam Leffler 	/*
3863fdfc330SAdrian Chadd 	 * Allocate TX descriptors and populate the lists.
387c42a7b7eSSam Leffler 	 */
3885591b213SSam Leffler 	error = ath_desc_alloc(sc);
3895591b213SSam Leffler 	if (error != 0) {
3903fdfc330SAdrian Chadd 		if_printf(ifp, "failed to allocate TX descriptors: %d\n",
3913fdfc330SAdrian Chadd 		    error);
3923fdfc330SAdrian Chadd 		goto bad;
3933fdfc330SAdrian Chadd 	}
3943fdfc330SAdrian Chadd 	error = ath_txdma_setup(sc);
3953fdfc330SAdrian Chadd 	if (error != 0) {
3963fdfc330SAdrian Chadd 		if_printf(ifp, "failed to allocate TX descriptors: %d\n",
3973fdfc330SAdrian Chadd 		    error);
3985591b213SSam Leffler 		goto bad;
3995591b213SSam Leffler 	}
4003d184db2SAdrian Chadd 
4013fdfc330SAdrian Chadd 	/*
4023fdfc330SAdrian Chadd 	 * Allocate RX descriptors and populate the lists.
4033fdfc330SAdrian Chadd 	 */
4043d184db2SAdrian Chadd 	error = ath_rxdma_setup(sc);
4053d184db2SAdrian Chadd 	if (error != 0) {
4063d184db2SAdrian Chadd 		if_printf(ifp, "failed to allocate RX descriptors: %d\n",
4073d184db2SAdrian Chadd 		    error);
4083d184db2SAdrian Chadd 		goto bad;
4093d184db2SAdrian Chadd 	}
4103d184db2SAdrian Chadd 
4112e986da5SSam Leffler 	callout_init_mtx(&sc->sc_cal_ch, &sc->sc_mtx, 0);
4122e986da5SSam Leffler 	callout_init_mtx(&sc->sc_wd_ch, &sc->sc_mtx, 0);
4135591b213SSam Leffler 
414f0b2a0beSSam Leffler 	ATH_TXBUF_LOCK_INIT(sc);
4155591b213SSam Leffler 
4160bbf5441SSam Leffler 	sc->sc_tq = taskqueue_create("ath_taskq", M_NOWAIT,
4170bbf5441SSam Leffler 		taskqueue_thread_enqueue, &sc->sc_tq);
4180bbf5441SSam Leffler 	taskqueue_start_threads(&sc->sc_tq, 1, PI_NET,
4190bbf5441SSam Leffler 		"%s taskq", ifp->if_xname);
4200bbf5441SSam Leffler 
421f8cc9b09SAdrian Chadd 	TASK_INIT(&sc->sc_rxtask, 0, sc->sc_rx.recv_tasklet, sc);
4225591b213SSam Leffler 	TASK_INIT(&sc->sc_bmisstask, 0, ath_bmiss_proc, sc);
423c42a7b7eSSam Leffler 	TASK_INIT(&sc->sc_bstucktask,0, ath_bstuck_proc, sc);
424d52f7132SAdrian Chadd 	TASK_INIT(&sc->sc_resettask,0, ath_reset_proc, sc);
42503e9308fSAdrian Chadd 	TASK_INIT(&sc->sc_txqtask,0, ath_txq_sched_tasklet, sc);
426f846cf42SAdrian Chadd 	TASK_INIT(&sc->sc_fataltask,0, ath_fatal_proc, sc);
4278e739394SAdrian Chadd 	TASK_INIT(&sc->sc_txsndtask, 0, ath_start_task, sc);
4285591b213SSam Leffler 
4295591b213SSam Leffler 	/*
430c42a7b7eSSam Leffler 	 * Allocate hardware transmit queues: one queue for
431c42a7b7eSSam Leffler 	 * beacon frames and one data queue for each QoS
4324fa8d4efSDaniel Eischen 	 * priority.  Note that the hal handles resetting
433c42a7b7eSSam Leffler 	 * these queues at the needed time.
434c42a7b7eSSam Leffler 	 *
435c42a7b7eSSam Leffler 	 * XXX PS-Poll
4365591b213SSam Leffler 	 */
437e1252ce1SAdrian Chadd 	sc->sc_bhalq = ath_beaconq_setup(sc);
4385591b213SSam Leffler 	if (sc->sc_bhalq == (u_int) -1) {
4395591b213SSam Leffler 		if_printf(ifp, "unable to setup a beacon xmit queue!\n");
440c42a7b7eSSam Leffler 		error = EIO;
441b28b4653SSam Leffler 		goto bad2;
4425591b213SSam Leffler 	}
443c42a7b7eSSam Leffler 	sc->sc_cabq = ath_txq_setup(sc, HAL_TX_QUEUE_CAB, 0);
444c42a7b7eSSam Leffler 	if (sc->sc_cabq == NULL) {
445c42a7b7eSSam Leffler 		if_printf(ifp, "unable to setup CAB xmit queue!\n");
446c42a7b7eSSam Leffler 		error = EIO;
447c42a7b7eSSam Leffler 		goto bad2;
448c42a7b7eSSam Leffler 	}
449c42a7b7eSSam Leffler 	/* NB: insure BK queue is the lowest priority h/w queue */
450c42a7b7eSSam Leffler 	if (!ath_tx_setup(sc, WME_AC_BK, HAL_WME_AC_BK)) {
451c42a7b7eSSam Leffler 		if_printf(ifp, "unable to setup xmit queue for %s traffic!\n",
452c42a7b7eSSam Leffler 			ieee80211_wme_acnames[WME_AC_BK]);
453c42a7b7eSSam Leffler 		error = EIO;
454c42a7b7eSSam Leffler 		goto bad2;
455c42a7b7eSSam Leffler 	}
456c42a7b7eSSam Leffler 	if (!ath_tx_setup(sc, WME_AC_BE, HAL_WME_AC_BE) ||
457c42a7b7eSSam Leffler 	    !ath_tx_setup(sc, WME_AC_VI, HAL_WME_AC_VI) ||
458c42a7b7eSSam Leffler 	    !ath_tx_setup(sc, WME_AC_VO, HAL_WME_AC_VO)) {
459c42a7b7eSSam Leffler 		/*
460c42a7b7eSSam Leffler 		 * Not enough hardware tx queues to properly do WME;
461c42a7b7eSSam Leffler 		 * just punt and assign them all to the same h/w queue.
462c42a7b7eSSam Leffler 		 * We could do a better job of this if, for example,
463c42a7b7eSSam Leffler 		 * we allocate queues when we switch from station to
464c42a7b7eSSam Leffler 		 * AP mode.
465c42a7b7eSSam Leffler 		 */
466c42a7b7eSSam Leffler 		if (sc->sc_ac2q[WME_AC_VI] != NULL)
467c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_VI]);
468c42a7b7eSSam Leffler 		if (sc->sc_ac2q[WME_AC_BE] != NULL)
469c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_BE]);
470c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_BE] = sc->sc_ac2q[WME_AC_BK];
471c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_VI] = sc->sc_ac2q[WME_AC_BK];
472c42a7b7eSSam Leffler 		sc->sc_ac2q[WME_AC_VO] = sc->sc_ac2q[WME_AC_BK];
473c42a7b7eSSam Leffler 	}
474c42a7b7eSSam Leffler 
475c42a7b7eSSam Leffler 	/*
476f8418db5SAdrian Chadd 	 * Attach the TX completion function.
477f8418db5SAdrian Chadd 	 *
478f8418db5SAdrian Chadd 	 * The non-EDMA chips may have some special case optimisations;
479f8418db5SAdrian Chadd 	 * this method gives everyone a chance to attach cleanly.
480c42a7b7eSSam Leffler 	 */
481f8418db5SAdrian Chadd 	sc->sc_tx.xmit_attach_comp_func(sc);
482c42a7b7eSSam Leffler 
483c42a7b7eSSam Leffler 	/*
484c42a7b7eSSam Leffler 	 * Setup rate control.  Some rate control modules
485c42a7b7eSSam Leffler 	 * call back to change the anntena state so expose
486c42a7b7eSSam Leffler 	 * the necessary entry points.
487c42a7b7eSSam Leffler 	 * XXX maybe belongs in struct ath_ratectrl?
488c42a7b7eSSam Leffler 	 */
489c42a7b7eSSam Leffler 	sc->sc_setdefantenna = ath_setdefantenna;
490c42a7b7eSSam Leffler 	sc->sc_rc = ath_rate_attach(sc);
491c42a7b7eSSam Leffler 	if (sc->sc_rc == NULL) {
492c42a7b7eSSam Leffler 		error = EIO;
493c42a7b7eSSam Leffler 		goto bad2;
494c42a7b7eSSam Leffler 	}
495c42a7b7eSSam Leffler 
49648237774SAdrian Chadd 	/* Attach DFS module */
49748237774SAdrian Chadd 	if (! ath_dfs_attach(sc)) {
4987e97436bSAdrian Chadd 		device_printf(sc->sc_dev,
4997e97436bSAdrian Chadd 		    "%s: unable to attach DFS\n", __func__);
50048237774SAdrian Chadd 		error = EIO;
50148237774SAdrian Chadd 		goto bad2;
50248237774SAdrian Chadd 	}
50348237774SAdrian Chadd 
50448237774SAdrian Chadd 	/* Start DFS processing tasklet */
50548237774SAdrian Chadd 	TASK_INIT(&sc->sc_dfstask, 0, ath_dfs_tasklet, sc);
50648237774SAdrian Chadd 
5073440495aSAdrian Chadd 	/* Configure LED state */
5083e50ec2cSSam Leffler 	sc->sc_blinking = 0;
509c42a7b7eSSam Leffler 	sc->sc_ledstate = 1;
5103e50ec2cSSam Leffler 	sc->sc_ledon = 0;			/* low true */
5113e50ec2cSSam Leffler 	sc->sc_ledidle = (2700*hz)/1000;	/* 2.7sec */
5123e50ec2cSSam Leffler 	callout_init(&sc->sc_ledtimer, CALLOUT_MPSAFE);
5133440495aSAdrian Chadd 
5143440495aSAdrian Chadd 	/*
5153440495aSAdrian Chadd 	 * Don't setup hardware-based blinking.
5163440495aSAdrian Chadd 	 *
5173440495aSAdrian Chadd 	 * Although some NICs may have this configured in the
5183440495aSAdrian Chadd 	 * default reset register values, the user may wish
5193440495aSAdrian Chadd 	 * to alter which pins have which function.
5203440495aSAdrian Chadd 	 *
5213440495aSAdrian Chadd 	 * The reference driver attaches the MAC network LED to GPIO1 and
5223440495aSAdrian Chadd 	 * the MAC power LED to GPIO2.  However, the DWA-552 cardbus
5233440495aSAdrian Chadd 	 * NIC has these reversed.
5243440495aSAdrian Chadd 	 */
5253440495aSAdrian Chadd 	sc->sc_hardled = (1 == 0);
5263440495aSAdrian Chadd 	sc->sc_led_net_pin = -1;
5273440495aSAdrian Chadd 	sc->sc_led_pwr_pin = -1;
528c42a7b7eSSam Leffler 	/*
529c42a7b7eSSam Leffler 	 * Auto-enable soft led processing for IBM cards and for
530c42a7b7eSSam Leffler 	 * 5211 minipci cards.  Users can also manually enable/disable
531c42a7b7eSSam Leffler 	 * support with a sysctl.
532c42a7b7eSSam Leffler 	 */
533c42a7b7eSSam Leffler 	sc->sc_softled = (devid == AR5212_DEVID_IBM || devid == AR5211_DEVID);
5346558ffd9SAdrian Chadd 	ath_led_config(sc);
535a497cd88SAdrian Chadd 	ath_hal_setledstate(ah, HAL_LED_INIT);
5365591b213SSam Leffler 
5375591b213SSam Leffler 	ifp->if_softc = sc;
5385591b213SSam Leffler 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
5398e739394SAdrian Chadd 	ifp->if_start = ath_start_queue;
5405591b213SSam Leffler 	ifp->if_ioctl = ath_ioctl;
5415591b213SSam Leffler 	ifp->if_init = ath_init;
542e50d35e6SMaxim Sobolev 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
543e50d35e6SMaxim Sobolev 	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
544154b8df2SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
5455591b213SSam Leffler 
546c42a7b7eSSam Leffler 	ic->ic_ifp = ifp;
5475591b213SSam Leffler 	/* XXX not right but it's not used anywhere important */
5485591b213SSam Leffler 	ic->ic_phytype = IEEE80211_T_OFDM;
5495591b213SSam Leffler 	ic->ic_opmode = IEEE80211_M_STA;
550c42a7b7eSSam Leffler 	ic->ic_caps =
551c43feedeSSam Leffler 		  IEEE80211_C_STA		/* station mode */
552c43feedeSSam Leffler 		| IEEE80211_C_IBSS		/* ibss, nee adhoc, mode */
553fe32c3efSSam Leffler 		| IEEE80211_C_HOSTAP		/* hostap mode */
554fe32c3efSSam Leffler 		| IEEE80211_C_MONITOR		/* monitor mode */
5557a04dc27SSam Leffler 		| IEEE80211_C_AHDEMO		/* adhoc demo mode */
556b032f27cSSam Leffler 		| IEEE80211_C_WDS		/* 4-address traffic works */
55759aa14a9SRui Paulo 		| IEEE80211_C_MBSS		/* mesh point link mode */
558fe32c3efSSam Leffler 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
559c42a7b7eSSam Leffler 		| IEEE80211_C_SHSLOT		/* short slot time supported */
560c42a7b7eSSam Leffler 		| IEEE80211_C_WPA		/* capable of WPA1+WPA2 */
5613b324f57SAdrian Chadd #ifndef	ATH_ENABLE_11N
56268e8e04eSSam Leffler 		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
5633b324f57SAdrian Chadd #endif
56468e8e04eSSam Leffler 		| IEEE80211_C_TXFRAG		/* handle tx frags */
56510dc8de4SAdrian Chadd #ifdef	ATH_ENABLE_DFS
5667e97436bSAdrian Chadd 		| IEEE80211_C_DFS		/* Enable radar detection */
56710dc8de4SAdrian Chadd #endif
56801e7e035SSam Leffler 		;
569c42a7b7eSSam Leffler 	/*
570c42a7b7eSSam Leffler 	 * Query the hal to figure out h/w crypto support.
571c42a7b7eSSam Leffler 	 */
572c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_WEP))
573b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP;
574c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_OCB))
575b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_OCB;
576c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_CCM))
577b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_CCM;
578c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_CKIP))
579b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_CKIP;
580c42a7b7eSSam Leffler 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_TKIP)) {
581b032f27cSSam Leffler 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIP;
582c42a7b7eSSam Leffler 		/*
583c42a7b7eSSam Leffler 		 * Check if h/w does the MIC and/or whether the
584c42a7b7eSSam Leffler 		 * separate key cache entries are required to
585c42a7b7eSSam Leffler 		 * handle both tx+rx MIC keys.
586c42a7b7eSSam Leffler 		 */
587c42a7b7eSSam Leffler 		if (ath_hal_ciphersupported(ah, HAL_CIPHER_MIC))
588b032f27cSSam Leffler 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
5895901d2d3SSam Leffler 		/*
5905901d2d3SSam Leffler 		 * If the h/w supports storing tx+rx MIC keys
5915901d2d3SSam Leffler 		 * in one cache slot automatically enable use.
5925901d2d3SSam Leffler 		 */
5935901d2d3SSam Leffler 		if (ath_hal_hastkipsplit(ah) ||
5945901d2d3SSam Leffler 		    !ath_hal_settkipsplit(ah, AH_FALSE))
595c42a7b7eSSam Leffler 			sc->sc_splitmic = 1;
596b032f27cSSam Leffler 		/*
597b032f27cSSam Leffler 		 * If the h/w can do TKIP MIC together with WME then
598b032f27cSSam Leffler 		 * we use it; otherwise we force the MIC to be done
599b032f27cSSam Leffler 		 * in software by the net80211 layer.
600b032f27cSSam Leffler 		 */
601b032f27cSSam Leffler 		if (ath_hal_haswmetkipmic(ah))
602b032f27cSSam Leffler 			sc->sc_wmetkipmic = 1;
603c42a7b7eSSam Leffler 	}
604e8fd88a3SSam Leffler 	sc->sc_hasclrkey = ath_hal_ciphersupported(ah, HAL_CIPHER_CLR);
6059ac01d39SRui Paulo 	/*
6061ac5dac2SRui Paulo 	 * Check for multicast key search support.
6079ac01d39SRui Paulo 	 */
6089ac01d39SRui Paulo 	if (ath_hal_hasmcastkeysearch(sc->sc_ah) &&
6099ac01d39SRui Paulo 	    !ath_hal_getmcastkeysearch(sc->sc_ah)) {
6109ac01d39SRui Paulo 		ath_hal_setmcastkeysearch(sc->sc_ah, 1);
6119ac01d39SRui Paulo 	}
612e8fd88a3SSam Leffler 	sc->sc_mcastkey = ath_hal_getmcastkeysearch(ah);
613c42a7b7eSSam Leffler 	/*
6145901d2d3SSam Leffler 	 * Mark key cache slots associated with global keys
6155901d2d3SSam Leffler 	 * as in use.  If we knew TKIP was not to be used we
6165901d2d3SSam Leffler 	 * could leave the +32, +64, and +32+64 slots free.
6175901d2d3SSam Leffler 	 */
6185901d2d3SSam Leffler 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
6195901d2d3SSam Leffler 		setbit(sc->sc_keymap, i);
6205901d2d3SSam Leffler 		setbit(sc->sc_keymap, i+64);
6215901d2d3SSam Leffler 		if (sc->sc_splitmic) {
6225901d2d3SSam Leffler 			setbit(sc->sc_keymap, i+32);
6235901d2d3SSam Leffler 			setbit(sc->sc_keymap, i+32+64);
6245901d2d3SSam Leffler 		}
6255901d2d3SSam Leffler 	}
6265901d2d3SSam Leffler 	/*
627c42a7b7eSSam Leffler 	 * TPC support can be done either with a global cap or
628c42a7b7eSSam Leffler 	 * per-packet support.  The latter is not available on
629c42a7b7eSSam Leffler 	 * all parts.  We're a bit pedantic here as all parts
630c42a7b7eSSam Leffler 	 * support a global cap.
631c42a7b7eSSam Leffler 	 */
632c59005e9SSam Leffler 	if (ath_hal_hastpc(ah) || ath_hal_hastxpowlimit(ah))
633c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TXPMGT;
634c42a7b7eSSam Leffler 
635c42a7b7eSSam Leffler 	/*
636c42a7b7eSSam Leffler 	 * Mark WME capability only if we have sufficient
637c42a7b7eSSam Leffler 	 * hardware queues to do proper priority scheduling.
638c42a7b7eSSam Leffler 	 */
639c42a7b7eSSam Leffler 	if (sc->sc_ac2q[WME_AC_BE] != sc->sc_ac2q[WME_AC_BK])
640c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_WME;
641c42a7b7eSSam Leffler 	/*
642e8fd88a3SSam Leffler 	 * Check for misc other capabilities.
643c42a7b7eSSam Leffler 	 */
644c42a7b7eSSam Leffler 	if (ath_hal_hasbursting(ah))
645c42a7b7eSSam Leffler 		ic->ic_caps |= IEEE80211_C_BURST;
646b032f27cSSam Leffler 	sc->sc_hasbmask = ath_hal_hasbssidmask(ah);
64759aa14a9SRui Paulo 	sc->sc_hasbmatch = ath_hal_hasbssidmatch(ah);
648b032f27cSSam Leffler 	sc->sc_hastsfadd = ath_hal_hastsfadjust(ah);
6498a2a6beeSAdrian Chadd 	sc->sc_rxslink = ath_hal_self_linked_final_rxdesc(ah);
650fc4de9b7SAdrian Chadd 	sc->sc_rxtsf32 = ath_hal_has_long_rxdesc_tsf(ah);
65168e8e04eSSam Leffler 	if (ath_hal_hasfastframes(ah))
65268e8e04eSSam Leffler 		ic->ic_caps |= IEEE80211_C_FF;
65359efa8b5SSam Leffler 	wmodes = ath_hal_getwirelessmodes(ah);
654411373ebSSam Leffler 	if (wmodes & (HAL_MODE_108G|HAL_MODE_TURBO))
65568e8e04eSSam Leffler 		ic->ic_caps |= IEEE80211_C_TURBOP;
656584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
65710ad9a77SSam Leffler 	if (ath_hal_macversion(ah) > 0x78) {
65810ad9a77SSam Leffler 		ic->ic_caps |= IEEE80211_C_TDMA; /* capable of TDMA */
65910ad9a77SSam Leffler 		ic->ic_tdma_update = ath_tdma_update;
66010ad9a77SSam Leffler 	}
66110ad9a77SSam Leffler #endif
66267397d39SAdrian Chadd 
66367397d39SAdrian Chadd 	/*
6649c85ff91SAdrian Chadd 	 * TODO: enforce that at least this many frames are available
6659c85ff91SAdrian Chadd 	 * in the txbuf list before allowing data frames (raw or
6669c85ff91SAdrian Chadd 	 * otherwise) to be transmitted.
6679c85ff91SAdrian Chadd 	 */
6689c85ff91SAdrian Chadd 	sc->sc_txq_data_minfree = 10;
6699c85ff91SAdrian Chadd 	/*
6709c85ff91SAdrian Chadd 	 * Leave this as default to maintain legacy behaviour.
6719c85ff91SAdrian Chadd 	 * Shortening the cabq/mcastq may end up causing some
6729c85ff91SAdrian Chadd 	 * undesirable behaviour.
6739c85ff91SAdrian Chadd 	 */
6749c85ff91SAdrian Chadd 	sc->sc_txq_mcastq_maxdepth = ath_txbuf;
6759c85ff91SAdrian Chadd 
6769c85ff91SAdrian Chadd 	/*
677a865860dSAdrian Chadd 	 * Allow the TX and RX chainmasks to be overridden by
678a865860dSAdrian Chadd 	 * environment variables and/or device.hints.
679a865860dSAdrian Chadd 	 *
680a865860dSAdrian Chadd 	 * This must be done early - before the hardware is
681a865860dSAdrian Chadd 	 * calibrated or before the 802.11n stream calculation
682a865860dSAdrian Chadd 	 * is done.
683a865860dSAdrian Chadd 	 */
684a865860dSAdrian Chadd 	if (resource_int_value(device_get_name(sc->sc_dev),
685a865860dSAdrian Chadd 	    device_get_unit(sc->sc_dev), "rx_chainmask",
686a865860dSAdrian Chadd 	    &rx_chainmask) == 0) {
687a865860dSAdrian Chadd 		device_printf(sc->sc_dev, "Setting RX chainmask to 0x%x\n",
688a865860dSAdrian Chadd 		    rx_chainmask);
689a865860dSAdrian Chadd 		(void) ath_hal_setrxchainmask(sc->sc_ah, rx_chainmask);
690a865860dSAdrian Chadd 	}
691a865860dSAdrian Chadd 	if (resource_int_value(device_get_name(sc->sc_dev),
692a865860dSAdrian Chadd 	    device_get_unit(sc->sc_dev), "tx_chainmask",
693a865860dSAdrian Chadd 	    &tx_chainmask) == 0) {
694a865860dSAdrian Chadd 		device_printf(sc->sc_dev, "Setting TX chainmask to 0x%x\n",
695a865860dSAdrian Chadd 		    tx_chainmask);
696dc8552d5SAdrian Chadd 		(void) ath_hal_settxchainmask(sc->sc_ah, tx_chainmask);
697a865860dSAdrian Chadd 	}
698a865860dSAdrian Chadd 
699af017101SAdrian Chadd 	/*
700af017101SAdrian Chadd 	 * Disable MRR with protected frames by default.
701af017101SAdrian Chadd 	 * Only 802.11n series NICs can handle this.
702af017101SAdrian Chadd 	 */
703af017101SAdrian Chadd 	sc->sc_mrrprot = 0;	/* XXX should be a capability */
704af017101SAdrian Chadd 
7058fd67f92SAdrian Chadd #ifdef	ATH_ENABLE_11N
70667397d39SAdrian Chadd 	/*
70767397d39SAdrian Chadd 	 * Query HT capabilities
70867397d39SAdrian Chadd 	 */
70967397d39SAdrian Chadd 	if (ath_hal_getcapability(ah, HAL_CAP_HT, 0, NULL) == HAL_OK &&
71067397d39SAdrian Chadd 	    (wmodes & (HAL_MODE_HT20 | HAL_MODE_HT40))) {
71167397d39SAdrian Chadd 		int rxs, txs;
71267397d39SAdrian Chadd 
71367397d39SAdrian Chadd 		device_printf(sc->sc_dev, "[HT] enabling HT modes\n");
714af017101SAdrian Chadd 
715af017101SAdrian Chadd 		sc->sc_mrrprot = 1;	/* XXX should be a capability */
716af017101SAdrian Chadd 
71767397d39SAdrian Chadd 		ic->ic_htcaps = IEEE80211_HTC_HT	/* HT operation */
71867397d39SAdrian Chadd 			    | IEEE80211_HTC_AMPDU	/* A-MPDU tx/rx */
71967397d39SAdrian Chadd 			    | IEEE80211_HTC_AMSDU	/* A-MSDU tx/rx */
7207e97436bSAdrian Chadd 			    | IEEE80211_HTCAP_MAXAMSDU_3839
7217e97436bSAdrian Chadd 			    				/* max A-MSDU length */
72267397d39SAdrian Chadd 			    | IEEE80211_HTCAP_SMPS_OFF;	/* SM power save off */
72367397d39SAdrian Chadd 			;
72467397d39SAdrian Chadd 
72576355edbSAdrian Chadd 		/*
72676355edbSAdrian Chadd 		 * Enable short-GI for HT20 only if the hardware
72776355edbSAdrian Chadd 		 * advertises support.
72876355edbSAdrian Chadd 		 * Notably, anything earlier than the AR9287 doesn't.
72976355edbSAdrian Chadd 		 */
73076355edbSAdrian Chadd 		if ((ath_hal_getcapability(ah,
73176355edbSAdrian Chadd 		    HAL_CAP_HT20_SGI, 0, NULL) == HAL_OK) &&
73276355edbSAdrian Chadd 		    (wmodes & HAL_MODE_HT20)) {
73376355edbSAdrian Chadd 			device_printf(sc->sc_dev,
73476355edbSAdrian Chadd 			    "[HT] enabling short-GI in 20MHz mode\n");
73576355edbSAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_SHORTGI20;
73676355edbSAdrian Chadd 		}
73776355edbSAdrian Chadd 
73867397d39SAdrian Chadd 		if (wmodes & HAL_MODE_HT40)
73967397d39SAdrian Chadd 			ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40
74067397d39SAdrian Chadd 			    |  IEEE80211_HTCAP_SHORTGI40;
74167397d39SAdrian Chadd 
74267397d39SAdrian Chadd 		/*
7437e97436bSAdrian Chadd 		 * TX/RX streams need to be taken into account when
7447e97436bSAdrian Chadd 		 * negotiating which MCS rates it'll receive and
74567397d39SAdrian Chadd 		 * what MCS rates are available for TX.
74667397d39SAdrian Chadd 		 */
74754517070SAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 0, &txs);
74854517070SAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 1, &rxs);
74967397d39SAdrian Chadd 
75067397d39SAdrian Chadd 		ath_hal_getrxchainmask(ah, &sc->sc_rxchainmask);
75167397d39SAdrian Chadd 		ath_hal_gettxchainmask(ah, &sc->sc_txchainmask);
75267397d39SAdrian Chadd 
75367397d39SAdrian Chadd 		ic->ic_txstream = txs;
75467397d39SAdrian Chadd 		ic->ic_rxstream = rxs;
75567397d39SAdrian Chadd 
756ce656facSAdrian Chadd 		(void) ath_hal_getcapability(ah, HAL_CAP_RTS_AGGR_LIMIT, 1,
757ce656facSAdrian Chadd 		    &sc->sc_rts_aggr_limit);
758ce656facSAdrian Chadd 		if (sc->sc_rts_aggr_limit != (64 * 1024))
759ce656facSAdrian Chadd 			device_printf(sc->sc_dev,
760ce656facSAdrian Chadd 			    "[HT] RTS aggregates limited to %d KiB\n",
761ce656facSAdrian Chadd 			    sc->sc_rts_aggr_limit / 1024);
762ce656facSAdrian Chadd 
7637e97436bSAdrian Chadd 		device_printf(sc->sc_dev,
7647e97436bSAdrian Chadd 		    "[HT] %d RX streams; %d TX streams\n", rxs, txs);
76567397d39SAdrian Chadd 	}
76667397d39SAdrian Chadd #endif
76767397d39SAdrian Chadd 
768c42a7b7eSSam Leffler 	/*
769f8aa9fd5SAdrian Chadd 	 * Initial aggregation settings.
770f8aa9fd5SAdrian Chadd 	 */
771f8aa9fd5SAdrian Chadd 	sc->sc_hwq_limit = ATH_AGGR_MIN_QDEPTH;
772f8aa9fd5SAdrian Chadd 	sc->sc_tid_hwq_lo = ATH_AGGR_SCHED_LOW;
773f8aa9fd5SAdrian Chadd 	sc->sc_tid_hwq_hi = ATH_AGGR_SCHED_HIGH;
774f8aa9fd5SAdrian Chadd 
775f8aa9fd5SAdrian Chadd 	/*
776ddbe3036SAdrian Chadd 	 * Check if the hardware requires PCI register serialisation.
777ddbe3036SAdrian Chadd 	 * Some of the Owl based MACs require this.
778ddbe3036SAdrian Chadd 	 */
779ddbe3036SAdrian Chadd 	if (mp_ncpus > 1 &&
780ddbe3036SAdrian Chadd 	    ath_hal_getcapability(ah, HAL_CAP_SERIALISE_WAR,
781ddbe3036SAdrian Chadd 	     0, NULL) == HAL_OK) {
782ddbe3036SAdrian Chadd 		sc->sc_ah->ah_config.ah_serialise_reg_war = 1;
7837e97436bSAdrian Chadd 		device_printf(sc->sc_dev,
7847e97436bSAdrian Chadd 		    "Enabling register serialisation\n");
785ddbe3036SAdrian Chadd 	}
786ddbe3036SAdrian Chadd 
787ddbe3036SAdrian Chadd 	/*
788c42a7b7eSSam Leffler 	 * Indicate we need the 802.11 header padded to a
789c42a7b7eSSam Leffler 	 * 32-bit boundary for 4-address and QoS frames.
790c42a7b7eSSam Leffler 	 */
791c42a7b7eSSam Leffler 	ic->ic_flags |= IEEE80211_F_DATAPAD;
792c42a7b7eSSam Leffler 
793c42a7b7eSSam Leffler 	/*
794c42a7b7eSSam Leffler 	 * Query the hal about antenna support.
795c42a7b7eSSam Leffler 	 */
796c42a7b7eSSam Leffler 	sc->sc_defant = ath_hal_getdefantenna(ah);
797c42a7b7eSSam Leffler 
798c42a7b7eSSam Leffler 	/*
799c42a7b7eSSam Leffler 	 * Not all chips have the VEOL support we want to
800c42a7b7eSSam Leffler 	 * use with IBSS beacons; check here for it.
801c42a7b7eSSam Leffler 	 */
802c42a7b7eSSam Leffler 	sc->sc_hasveol = ath_hal_hasveol(ah);
8035591b213SSam Leffler 
8045591b213SSam Leffler 	/* get mac address from hardware */
80529aca940SSam Leffler 	ath_hal_getmac(ah, macaddr);
806b032f27cSSam Leffler 	if (sc->sc_hasbmask)
807b032f27cSSam Leffler 		ath_hal_getbssidmask(ah, sc->sc_hwbssidmask);
8085591b213SSam Leffler 
809b032f27cSSam Leffler 	/* NB: used to size node table key mapping array */
810b032f27cSSam Leffler 	ic->ic_max_keyix = sc->sc_keymax;
8115591b213SSam Leffler 	/* call MI attach routine. */
81229aca940SSam Leffler 	ieee80211_ifattach(ic, macaddr);
813b032f27cSSam Leffler 	ic->ic_setregdomain = ath_setregdomain;
814b032f27cSSam Leffler 	ic->ic_getradiocaps = ath_getradiocaps;
815b032f27cSSam Leffler 	sc->sc_opmode = HAL_M_STA;
816b032f27cSSam Leffler 
8175591b213SSam Leffler 	/* override default methods */
818b032f27cSSam Leffler 	ic->ic_newassoc = ath_newassoc;
819b032f27cSSam Leffler 	ic->ic_updateslot = ath_updateslot;
820b032f27cSSam Leffler 	ic->ic_wme.wme_update = ath_wme_update;
821b032f27cSSam Leffler 	ic->ic_vap_create = ath_vap_create;
822b032f27cSSam Leffler 	ic->ic_vap_delete = ath_vap_delete;
823b032f27cSSam Leffler 	ic->ic_raw_xmit = ath_raw_xmit;
824b032f27cSSam Leffler 	ic->ic_update_mcast = ath_update_mcast;
825b032f27cSSam Leffler 	ic->ic_update_promisc = ath_update_promisc;
8265591b213SSam Leffler 	ic->ic_node_alloc = ath_node_alloc;
8271e774079SSam Leffler 	sc->sc_node_free = ic->ic_node_free;
8285591b213SSam Leffler 	ic->ic_node_free = ath_node_free;
8294afa805eSAdrian Chadd 	sc->sc_node_cleanup = ic->ic_node_cleanup;
8304afa805eSAdrian Chadd 	ic->ic_node_cleanup = ath_node_cleanup;
83168e8e04eSSam Leffler 	ic->ic_node_getsignal = ath_node_getsignal;
83268e8e04eSSam Leffler 	ic->ic_scan_start = ath_scan_start;
83368e8e04eSSam Leffler 	ic->ic_scan_end = ath_scan_end;
83468e8e04eSSam Leffler 	ic->ic_set_channel = ath_set_channel;
835fdd72b4aSAdrian Chadd #ifdef	ATH_ENABLE_11N
836eb6f0de0SAdrian Chadd 	/* 802.11n specific - but just override anyway */
837eb6f0de0SAdrian Chadd 	sc->sc_addba_request = ic->ic_addba_request;
838eb6f0de0SAdrian Chadd 	sc->sc_addba_response = ic->ic_addba_response;
839eb6f0de0SAdrian Chadd 	sc->sc_addba_stop = ic->ic_addba_stop;
840eb6f0de0SAdrian Chadd 	sc->sc_bar_response = ic->ic_bar_response;
841eb6f0de0SAdrian Chadd 	sc->sc_addba_response_timeout = ic->ic_addba_response_timeout;
842eb6f0de0SAdrian Chadd 
843eb6f0de0SAdrian Chadd 	ic->ic_addba_request = ath_addba_request;
844eb6f0de0SAdrian Chadd 	ic->ic_addba_response = ath_addba_response;
845eb6f0de0SAdrian Chadd 	ic->ic_addba_response_timeout = ath_addba_response_timeout;
846eb6f0de0SAdrian Chadd 	ic->ic_addba_stop = ath_addba_stop;
847eb6f0de0SAdrian Chadd 	ic->ic_bar_response = ath_bar_response;
848eb6f0de0SAdrian Chadd 
849fdd72b4aSAdrian Chadd 	ic->ic_update_chw = ath_update_chw;
850fdd72b4aSAdrian Chadd #endif	/* ATH_ENABLE_11N */
851fdd72b4aSAdrian Chadd 
852e1b5ab97SAdrian Chadd #ifdef	ATH_ENABLE_RADIOTAP_VENDOR_EXT
853e1b5ab97SAdrian Chadd 	/*
854e1b5ab97SAdrian Chadd 	 * There's one vendor bitmap entry in the RX radiotap
855e1b5ab97SAdrian Chadd 	 * header; make sure that's taken into account.
856e1b5ab97SAdrian Chadd 	 */
857e1b5ab97SAdrian Chadd 	ieee80211_radiotap_attachv(ic,
858e1b5ab97SAdrian Chadd 	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th), 0,
859e1b5ab97SAdrian Chadd 		ATH_TX_RADIOTAP_PRESENT,
860e1b5ab97SAdrian Chadd 	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th), 1,
861e1b5ab97SAdrian Chadd 		ATH_RX_RADIOTAP_PRESENT);
862e1b5ab97SAdrian Chadd #else
863e1b5ab97SAdrian Chadd 	/*
864e1b5ab97SAdrian Chadd 	 * No vendor bitmap/extensions are present.
865e1b5ab97SAdrian Chadd 	 */
8665463c4a4SSam Leffler 	ieee80211_radiotap_attach(ic,
8675463c4a4SSam Leffler 	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
8685463c4a4SSam Leffler 		ATH_TX_RADIOTAP_PRESENT,
8695463c4a4SSam Leffler 	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
8705463c4a4SSam Leffler 		ATH_RX_RADIOTAP_PRESENT);
871e1b5ab97SAdrian Chadd #endif	/* ATH_ENABLE_RADIOTAP_VENDOR_EXT */
8725463c4a4SSam Leffler 
8734866e6c2SSam Leffler 	/*
8744866e6c2SSam Leffler 	 * Setup dynamic sysctl's now that country code and
8754866e6c2SSam Leffler 	 * regdomain are available from the hal.
8764866e6c2SSam Leffler 	 */
8774866e6c2SSam Leffler 	ath_sysctlattach(sc);
878e8dabfbeSAdrian Chadd 	ath_sysctl_stats_attach(sc);
87937931a35SAdrian Chadd 	ath_sysctl_hal_attach(sc);
88073454c73SSam Leffler 
881c42a7b7eSSam Leffler 	if (bootverbose)
882c42a7b7eSSam Leffler 		ieee80211_announce(ic);
883c42a7b7eSSam Leffler 	ath_announce(sc);
8845591b213SSam Leffler 	return 0;
885b28b4653SSam Leffler bad2:
886c42a7b7eSSam Leffler 	ath_tx_cleanup(sc);
887b28b4653SSam Leffler 	ath_desc_free(sc);
8883fdfc330SAdrian Chadd 	ath_txdma_teardown(sc);
8893d184db2SAdrian Chadd 	ath_rxdma_teardown(sc);
8905591b213SSam Leffler bad:
8915591b213SSam Leffler 	if (ah)
8925591b213SSam Leffler 		ath_hal_detach(ah);
893a93c5097SAdrian Chadd 	if (ifp != NULL) {
894a93c5097SAdrian Chadd 		CURVNET_SET(ifp->if_vnet);
895fc74a9f9SBrooks Davis 		if_free(ifp);
896a93c5097SAdrian Chadd 		CURVNET_RESTORE();
897a93c5097SAdrian Chadd 	}
8985591b213SSam Leffler 	sc->sc_invalid = 1;
8995591b213SSam Leffler 	return error;
9005591b213SSam Leffler }
9015591b213SSam Leffler 
9025591b213SSam Leffler int
9035591b213SSam Leffler ath_detach(struct ath_softc *sc)
9045591b213SSam Leffler {
905fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
9065591b213SSam Leffler 
907c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
908c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
9095591b213SSam Leffler 
910c42a7b7eSSam Leffler 	/*
911c42a7b7eSSam Leffler 	 * NB: the order of these is important:
91271b85077SSam Leffler 	 * o stop the chip so no more interrupts will fire
913c42a7b7eSSam Leffler 	 * o call the 802.11 layer before detaching the hal to
914c42a7b7eSSam Leffler 	 *   insure callbacks into the driver to delete global
915c42a7b7eSSam Leffler 	 *   key cache entries can be handled
91671b85077SSam Leffler 	 * o free the taskqueue which drains any pending tasks
917c42a7b7eSSam Leffler 	 * o reclaim the tx queue data structures after calling
918c42a7b7eSSam Leffler 	 *   the 802.11 layer as we'll get called back to reclaim
919c42a7b7eSSam Leffler 	 *   node state and potentially want to use them
920c42a7b7eSSam Leffler 	 * o to cleanup the tx queues the hal is called, so detach
921c42a7b7eSSam Leffler 	 *   it last
922c42a7b7eSSam Leffler 	 * Other than that, it's straightforward...
923c42a7b7eSSam Leffler 	 */
92471b85077SSam Leffler 	ath_stop(ifp);
925b032f27cSSam Leffler 	ieee80211_ifdetach(ifp->if_l2com);
92671b85077SSam Leffler 	taskqueue_free(sc->sc_tq);
92786e07743SSam Leffler #ifdef ATH_TX99_DIAG
92886e07743SSam Leffler 	if (sc->sc_tx99 != NULL)
92986e07743SSam Leffler 		sc->sc_tx99->detach(sc->sc_tx99);
93086e07743SSam Leffler #endif
931c42a7b7eSSam Leffler 	ath_rate_detach(sc->sc_rc);
93248237774SAdrian Chadd 
93348237774SAdrian Chadd 	ath_dfs_detach(sc);
9345591b213SSam Leffler 	ath_desc_free(sc);
9354bf404eaSAdrian Chadd 	ath_txdma_teardown(sc);
9363d184db2SAdrian Chadd 	ath_rxdma_teardown(sc);
937c42a7b7eSSam Leffler 	ath_tx_cleanup(sc);
93871b85077SSam Leffler 	ath_hal_detach(sc->sc_ah);	/* NB: sets chip in full sleep */
939a93c5097SAdrian Chadd 
940a93c5097SAdrian Chadd 	CURVNET_SET(ifp->if_vnet);
941c4c6f08fSRuslan Ermilov 	if_free(ifp);
942a93c5097SAdrian Chadd 	CURVNET_RESTORE();
943f0b2a0beSSam Leffler 
9445591b213SSam Leffler 	return 0;
9455591b213SSam Leffler }
9465591b213SSam Leffler 
947b032f27cSSam Leffler /*
948b032f27cSSam Leffler  * MAC address handling for multiple BSS on the same radio.
949b032f27cSSam Leffler  * The first vap uses the MAC address from the EEPROM.  For
950b032f27cSSam Leffler  * subsequent vap's we set the U/L bit (bit 1) in the MAC
951b032f27cSSam Leffler  * address and use the next six bits as an index.
952b032f27cSSam Leffler  */
953b032f27cSSam Leffler static void
954b032f27cSSam Leffler assign_address(struct ath_softc *sc, uint8_t mac[IEEE80211_ADDR_LEN], int clone)
955b032f27cSSam Leffler {
956b032f27cSSam Leffler 	int i;
957b032f27cSSam Leffler 
958b032f27cSSam Leffler 	if (clone && sc->sc_hasbmask) {
959b032f27cSSam Leffler 		/* NB: we only do this if h/w supports multiple bssid */
960b032f27cSSam Leffler 		for (i = 0; i < 8; i++)
961b032f27cSSam Leffler 			if ((sc->sc_bssidmask & (1<<i)) == 0)
962b032f27cSSam Leffler 				break;
963b032f27cSSam Leffler 		if (i != 0)
964b032f27cSSam Leffler 			mac[0] |= (i << 2)|0x2;
965b032f27cSSam Leffler 	} else
966b032f27cSSam Leffler 		i = 0;
967b032f27cSSam Leffler 	sc->sc_bssidmask |= 1<<i;
968b032f27cSSam Leffler 	sc->sc_hwbssidmask[0] &= ~mac[0];
969b032f27cSSam Leffler 	if (i == 0)
970b032f27cSSam Leffler 		sc->sc_nbssid0++;
971b032f27cSSam Leffler }
972b032f27cSSam Leffler 
973b032f27cSSam Leffler static void
974b032f27cSSam Leffler reclaim_address(struct ath_softc *sc, const uint8_t mac[IEEE80211_ADDR_LEN])
975b032f27cSSam Leffler {
976b032f27cSSam Leffler 	int i = mac[0] >> 2;
977b032f27cSSam Leffler 	uint8_t mask;
978b032f27cSSam Leffler 
979b032f27cSSam Leffler 	if (i != 0 || --sc->sc_nbssid0 == 0) {
980b032f27cSSam Leffler 		sc->sc_bssidmask &= ~(1<<i);
981b032f27cSSam Leffler 		/* recalculate bssid mask from remaining addresses */
982b032f27cSSam Leffler 		mask = 0xff;
983b032f27cSSam Leffler 		for (i = 1; i < 8; i++)
984b032f27cSSam Leffler 			if (sc->sc_bssidmask & (1<<i))
985b032f27cSSam Leffler 				mask &= ~((i<<2)|0x2);
986b032f27cSSam Leffler 		sc->sc_hwbssidmask[0] |= mask;
987b032f27cSSam Leffler 	}
988b032f27cSSam Leffler }
989b032f27cSSam Leffler 
990b032f27cSSam Leffler /*
991b032f27cSSam Leffler  * Assign a beacon xmit slot.  We try to space out
992b032f27cSSam Leffler  * assignments so when beacons are staggered the
993b032f27cSSam Leffler  * traffic coming out of the cab q has maximal time
994b032f27cSSam Leffler  * to go out before the next beacon is scheduled.
995b032f27cSSam Leffler  */
996b032f27cSSam Leffler static int
997b032f27cSSam Leffler assign_bslot(struct ath_softc *sc)
998b032f27cSSam Leffler {
999b032f27cSSam Leffler 	u_int slot, free;
1000b032f27cSSam Leffler 
1001b032f27cSSam Leffler 	free = 0;
1002b032f27cSSam Leffler 	for (slot = 0; slot < ATH_BCBUF; slot++)
1003b032f27cSSam Leffler 		if (sc->sc_bslot[slot] == NULL) {
1004b032f27cSSam Leffler 			if (sc->sc_bslot[(slot+1)%ATH_BCBUF] == NULL &&
1005b032f27cSSam Leffler 			    sc->sc_bslot[(slot-1)%ATH_BCBUF] == NULL)
1006b032f27cSSam Leffler 				return slot;
1007b032f27cSSam Leffler 			free = slot;
1008b032f27cSSam Leffler 			/* NB: keep looking for a double slot */
1009b032f27cSSam Leffler 		}
1010b032f27cSSam Leffler 	return free;
1011b032f27cSSam Leffler }
1012b032f27cSSam Leffler 
1013b032f27cSSam Leffler static struct ieee80211vap *
1014fcd9500fSBernhard Schmidt ath_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
1015fcd9500fSBernhard Schmidt     enum ieee80211_opmode opmode, int flags,
1016b032f27cSSam Leffler     const uint8_t bssid[IEEE80211_ADDR_LEN],
1017b032f27cSSam Leffler     const uint8_t mac0[IEEE80211_ADDR_LEN])
1018b032f27cSSam Leffler {
1019b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
1020b032f27cSSam Leffler 	struct ath_vap *avp;
1021b032f27cSSam Leffler 	struct ieee80211vap *vap;
1022b032f27cSSam Leffler 	uint8_t mac[IEEE80211_ADDR_LEN];
1023fcd9500fSBernhard Schmidt 	int needbeacon, error;
1024fcd9500fSBernhard Schmidt 	enum ieee80211_opmode ic_opmode;
1025b032f27cSSam Leffler 
1026b032f27cSSam Leffler 	avp = (struct ath_vap *) malloc(sizeof(struct ath_vap),
1027b032f27cSSam Leffler 	    M_80211_VAP, M_WAITOK | M_ZERO);
1028b032f27cSSam Leffler 	needbeacon = 0;
1029b032f27cSSam Leffler 	IEEE80211_ADDR_COPY(mac, mac0);
1030b032f27cSSam Leffler 
1031b032f27cSSam Leffler 	ATH_LOCK(sc);
1032a8962181SSam Leffler 	ic_opmode = opmode;		/* default to opmode of new vap */
1033b032f27cSSam Leffler 	switch (opmode) {
1034b032f27cSSam Leffler 	case IEEE80211_M_STA:
1035a8962181SSam Leffler 		if (sc->sc_nstavaps != 0) {	/* XXX only 1 for now */
1036b032f27cSSam Leffler 			device_printf(sc->sc_dev, "only 1 sta vap supported\n");
1037b032f27cSSam Leffler 			goto bad;
1038b032f27cSSam Leffler 		}
1039b032f27cSSam Leffler 		if (sc->sc_nvaps) {
1040b032f27cSSam Leffler 			/*
1041a8962181SSam Leffler 			 * With multiple vaps we must fall back
1042a8962181SSam Leffler 			 * to s/w beacon miss handling.
1043b032f27cSSam Leffler 			 */
1044b032f27cSSam Leffler 			flags |= IEEE80211_CLONE_NOBEACONS;
1045b032f27cSSam Leffler 		}
1046a8962181SSam Leffler 		if (flags & IEEE80211_CLONE_NOBEACONS) {
1047a8962181SSam Leffler 			/*
1048a8962181SSam Leffler 			 * Station mode w/o beacons are implemented w/ AP mode.
1049a8962181SSam Leffler 			 */
1050b032f27cSSam Leffler 			ic_opmode = IEEE80211_M_HOSTAP;
1051a8962181SSam Leffler 		}
1052b032f27cSSam Leffler 		break;
1053b032f27cSSam Leffler 	case IEEE80211_M_IBSS:
1054b032f27cSSam Leffler 		if (sc->sc_nvaps != 0) {	/* XXX only 1 for now */
1055b032f27cSSam Leffler 			device_printf(sc->sc_dev,
1056b032f27cSSam Leffler 			    "only 1 ibss vap supported\n");
1057b032f27cSSam Leffler 			goto bad;
1058b032f27cSSam Leffler 		}
1059b032f27cSSam Leffler 		needbeacon = 1;
1060b032f27cSSam Leffler 		break;
1061b032f27cSSam Leffler 	case IEEE80211_M_AHDEMO:
1062584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
106310ad9a77SSam Leffler 		if (flags & IEEE80211_CLONE_TDMA) {
1064a8962181SSam Leffler 			if (sc->sc_nvaps != 0) {
1065a8962181SSam Leffler 				device_printf(sc->sc_dev,
1066a8962181SSam Leffler 				    "only 1 tdma vap supported\n");
1067a8962181SSam Leffler 				goto bad;
1068a8962181SSam Leffler 			}
106910ad9a77SSam Leffler 			needbeacon = 1;
107010ad9a77SSam Leffler 			flags |= IEEE80211_CLONE_NOBEACONS;
107110ad9a77SSam Leffler 		}
1072b032f27cSSam Leffler 		/* fall thru... */
107310ad9a77SSam Leffler #endif
1074b032f27cSSam Leffler 	case IEEE80211_M_MONITOR:
1075b032f27cSSam Leffler 		if (sc->sc_nvaps != 0 && ic->ic_opmode != opmode) {
1076a8962181SSam Leffler 			/*
1077a8962181SSam Leffler 			 * Adopt existing mode.  Adding a monitor or ahdemo
1078a8962181SSam Leffler 			 * vap to an existing configuration is of dubious
1079a8962181SSam Leffler 			 * value but should be ok.
1080a8962181SSam Leffler 			 */
1081b032f27cSSam Leffler 			/* XXX not right for monitor mode */
1082b032f27cSSam Leffler 			ic_opmode = ic->ic_opmode;
1083a8962181SSam Leffler 		}
1084b032f27cSSam Leffler 		break;
1085b032f27cSSam Leffler 	case IEEE80211_M_HOSTAP:
108659aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
1087b032f27cSSam Leffler 		needbeacon = 1;
1088a8962181SSam Leffler 		break;
1089b032f27cSSam Leffler 	case IEEE80211_M_WDS:
1090a8962181SSam Leffler 		if (sc->sc_nvaps != 0 && ic->ic_opmode == IEEE80211_M_STA) {
1091b032f27cSSam Leffler 			device_printf(sc->sc_dev,
1092b032f27cSSam Leffler 			    "wds not supported in sta mode\n");
1093b032f27cSSam Leffler 			goto bad;
1094b032f27cSSam Leffler 		}
1095b032f27cSSam Leffler 		/*
1096b032f27cSSam Leffler 		 * Silently remove any request for a unique
1097b032f27cSSam Leffler 		 * bssid; WDS vap's always share the local
1098b032f27cSSam Leffler 		 * mac address.
1099b032f27cSSam Leffler 		 */
1100b032f27cSSam Leffler 		flags &= ~IEEE80211_CLONE_BSSID;
1101a8962181SSam Leffler 		if (sc->sc_nvaps == 0)
1102b032f27cSSam Leffler 			ic_opmode = IEEE80211_M_HOSTAP;
1103a8962181SSam Leffler 		else
1104a8962181SSam Leffler 			ic_opmode = ic->ic_opmode;
11057d261891SRui Paulo 		break;
1106b032f27cSSam Leffler 	default:
1107b032f27cSSam Leffler 		device_printf(sc->sc_dev, "unknown opmode %d\n", opmode);
1108b032f27cSSam Leffler 		goto bad;
1109b032f27cSSam Leffler 	}
1110b032f27cSSam Leffler 	/*
1111b032f27cSSam Leffler 	 * Check that a beacon buffer is available; the code below assumes it.
1112b032f27cSSam Leffler 	 */
11136b349e5aSAdrian Chadd 	if (needbeacon & TAILQ_EMPTY(&sc->sc_bbuf)) {
1114b032f27cSSam Leffler 		device_printf(sc->sc_dev, "no beacon buffer available\n");
1115b032f27cSSam Leffler 		goto bad;
1116b032f27cSSam Leffler 	}
1117b032f27cSSam Leffler 
1118b032f27cSSam Leffler 	/* STA, AHDEMO? */
111959aa14a9SRui Paulo 	if (opmode == IEEE80211_M_HOSTAP || opmode == IEEE80211_M_MBSS) {
1120b032f27cSSam Leffler 		assign_address(sc, mac, flags & IEEE80211_CLONE_BSSID);
1121b032f27cSSam Leffler 		ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
1122b032f27cSSam Leffler 	}
1123b032f27cSSam Leffler 
1124b032f27cSSam Leffler 	vap = &avp->av_vap;
1125b032f27cSSam Leffler 	/* XXX can't hold mutex across if_alloc */
1126b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1127b032f27cSSam Leffler 	error = ieee80211_vap_setup(ic, vap, name, unit, opmode, flags,
1128b032f27cSSam Leffler 	    bssid, mac);
1129b032f27cSSam Leffler 	ATH_LOCK(sc);
1130b032f27cSSam Leffler 	if (error != 0) {
1131b032f27cSSam Leffler 		device_printf(sc->sc_dev, "%s: error %d creating vap\n",
1132b032f27cSSam Leffler 		    __func__, error);
1133b032f27cSSam Leffler 		goto bad2;
1134b032f27cSSam Leffler 	}
1135b032f27cSSam Leffler 
1136b032f27cSSam Leffler 	/* h/w crypto support */
1137b032f27cSSam Leffler 	vap->iv_key_alloc = ath_key_alloc;
1138b032f27cSSam Leffler 	vap->iv_key_delete = ath_key_delete;
1139b032f27cSSam Leffler 	vap->iv_key_set = ath_key_set;
1140b032f27cSSam Leffler 	vap->iv_key_update_begin = ath_key_update_begin;
1141b032f27cSSam Leffler 	vap->iv_key_update_end = ath_key_update_end;
1142b032f27cSSam Leffler 
1143b032f27cSSam Leffler 	/* override various methods */
1144b032f27cSSam Leffler 	avp->av_recv_mgmt = vap->iv_recv_mgmt;
1145b032f27cSSam Leffler 	vap->iv_recv_mgmt = ath_recv_mgmt;
1146b032f27cSSam Leffler 	vap->iv_reset = ath_reset_vap;
1147b032f27cSSam Leffler 	vap->iv_update_beacon = ath_beacon_update;
1148b032f27cSSam Leffler 	avp->av_newstate = vap->iv_newstate;
1149b032f27cSSam Leffler 	vap->iv_newstate = ath_newstate;
1150b032f27cSSam Leffler 	avp->av_bmiss = vap->iv_bmiss;
1151b032f27cSSam Leffler 	vap->iv_bmiss = ath_bmiss_vap;
1152b032f27cSSam Leffler 
11530eb81626SAdrian Chadd 	avp->av_node_ps = vap->iv_node_ps;
11540eb81626SAdrian Chadd 	vap->iv_node_ps = ath_node_powersave;
11550eb81626SAdrian Chadd 
1156*548a605dSAdrian Chadd 	avp->av_set_tim = vap->iv_set_tim;
1157*548a605dSAdrian Chadd 	vap->iv_set_tim = ath_node_set_tim;
1158*548a605dSAdrian Chadd 
11599be25f4aSAdrian Chadd 	/* Set default parameters */
11609be25f4aSAdrian Chadd 
11619be25f4aSAdrian Chadd 	/*
11629be25f4aSAdrian Chadd 	 * Anything earlier than some AR9300 series MACs don't
11639be25f4aSAdrian Chadd 	 * support a smaller MPDU density.
11649be25f4aSAdrian Chadd 	 */
11659be25f4aSAdrian Chadd 	vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_8;
11669be25f4aSAdrian Chadd 	/*
11679be25f4aSAdrian Chadd 	 * All NICs can handle the maximum size, however
11689be25f4aSAdrian Chadd 	 * AR5416 based MACs can only TX aggregates w/ RTS
11699be25f4aSAdrian Chadd 	 * protection when the total aggregate size is <= 8k.
11709be25f4aSAdrian Chadd 	 * However, for now that's enforced by the TX path.
11719be25f4aSAdrian Chadd 	 */
11729be25f4aSAdrian Chadd 	vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_64K;
11739be25f4aSAdrian Chadd 
1174b032f27cSSam Leffler 	avp->av_bslot = -1;
1175b032f27cSSam Leffler 	if (needbeacon) {
1176b032f27cSSam Leffler 		/*
1177b032f27cSSam Leffler 		 * Allocate beacon state and setup the q for buffered
1178b032f27cSSam Leffler 		 * multicast frames.  We know a beacon buffer is
1179b032f27cSSam Leffler 		 * available because we checked above.
1180b032f27cSSam Leffler 		 */
11816b349e5aSAdrian Chadd 		avp->av_bcbuf = TAILQ_FIRST(&sc->sc_bbuf);
11826b349e5aSAdrian Chadd 		TAILQ_REMOVE(&sc->sc_bbuf, avp->av_bcbuf, bf_list);
1183b032f27cSSam Leffler 		if (opmode != IEEE80211_M_IBSS || !sc->sc_hasveol) {
1184b032f27cSSam Leffler 			/*
1185b032f27cSSam Leffler 			 * Assign the vap to a beacon xmit slot.  As above
1186b032f27cSSam Leffler 			 * this cannot fail to find a free one.
1187b032f27cSSam Leffler 			 */
1188b032f27cSSam Leffler 			avp->av_bslot = assign_bslot(sc);
1189b032f27cSSam Leffler 			KASSERT(sc->sc_bslot[avp->av_bslot] == NULL,
1190b032f27cSSam Leffler 			    ("beacon slot %u not empty", avp->av_bslot));
1191b032f27cSSam Leffler 			sc->sc_bslot[avp->av_bslot] = vap;
1192b032f27cSSam Leffler 			sc->sc_nbcnvaps++;
1193b032f27cSSam Leffler 		}
1194b032f27cSSam Leffler 		if (sc->sc_hastsfadd && sc->sc_nbcnvaps > 0) {
1195b032f27cSSam Leffler 			/*
1196b032f27cSSam Leffler 			 * Multple vaps are to transmit beacons and we
1197b032f27cSSam Leffler 			 * have h/w support for TSF adjusting; enable
1198b032f27cSSam Leffler 			 * use of staggered beacons.
1199b032f27cSSam Leffler 			 */
1200b032f27cSSam Leffler 			sc->sc_stagbeacons = 1;
1201b032f27cSSam Leffler 		}
1202b032f27cSSam Leffler 		ath_txq_init(sc, &avp->av_mcastq, ATH_TXQ_SWQ);
1203b032f27cSSam Leffler 	}
1204b032f27cSSam Leffler 
1205b032f27cSSam Leffler 	ic->ic_opmode = ic_opmode;
1206b032f27cSSam Leffler 	if (opmode != IEEE80211_M_WDS) {
1207b032f27cSSam Leffler 		sc->sc_nvaps++;
1208b032f27cSSam Leffler 		if (opmode == IEEE80211_M_STA)
1209b032f27cSSam Leffler 			sc->sc_nstavaps++;
1210fe0dd789SSam Leffler 		if (opmode == IEEE80211_M_MBSS)
1211fe0dd789SSam Leffler 			sc->sc_nmeshvaps++;
1212b032f27cSSam Leffler 	}
1213b032f27cSSam Leffler 	switch (ic_opmode) {
1214b032f27cSSam Leffler 	case IEEE80211_M_IBSS:
1215b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_IBSS;
1216b032f27cSSam Leffler 		break;
1217b032f27cSSam Leffler 	case IEEE80211_M_STA:
1218b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_STA;
1219b032f27cSSam Leffler 		break;
1220b032f27cSSam Leffler 	case IEEE80211_M_AHDEMO:
1221584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
122210ad9a77SSam Leffler 		if (vap->iv_caps & IEEE80211_C_TDMA) {
122310ad9a77SSam Leffler 			sc->sc_tdma = 1;
122410ad9a77SSam Leffler 			/* NB: disable tsf adjust */
122510ad9a77SSam Leffler 			sc->sc_stagbeacons = 0;
122610ad9a77SSam Leffler 		}
122710ad9a77SSam Leffler 		/*
122810ad9a77SSam Leffler 		 * NB: adhoc demo mode is a pseudo mode; to the hal it's
122910ad9a77SSam Leffler 		 * just ap mode.
123010ad9a77SSam Leffler 		 */
123110ad9a77SSam Leffler 		/* fall thru... */
123210ad9a77SSam Leffler #endif
1233b032f27cSSam Leffler 	case IEEE80211_M_HOSTAP:
123459aa14a9SRui Paulo 	case IEEE80211_M_MBSS:
1235b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_HOSTAP;
1236b032f27cSSam Leffler 		break;
1237b032f27cSSam Leffler 	case IEEE80211_M_MONITOR:
1238b032f27cSSam Leffler 		sc->sc_opmode = HAL_M_MONITOR;
1239b032f27cSSam Leffler 		break;
1240b032f27cSSam Leffler 	default:
1241b032f27cSSam Leffler 		/* XXX should not happen */
1242b032f27cSSam Leffler 		break;
1243b032f27cSSam Leffler 	}
1244b032f27cSSam Leffler 	if (sc->sc_hastsfadd) {
1245b032f27cSSam Leffler 		/*
1246b032f27cSSam Leffler 		 * Configure whether or not TSF adjust should be done.
1247b032f27cSSam Leffler 		 */
1248b032f27cSSam Leffler 		ath_hal_settsfadjust(sc->sc_ah, sc->sc_stagbeacons);
1249b032f27cSSam Leffler 	}
125010ad9a77SSam Leffler 	if (flags & IEEE80211_CLONE_NOBEACONS) {
125110ad9a77SSam Leffler 		/*
125210ad9a77SSam Leffler 		 * Enable s/w beacon miss handling.
125310ad9a77SSam Leffler 		 */
125410ad9a77SSam Leffler 		sc->sc_swbmiss = 1;
125510ad9a77SSam Leffler 	}
1256b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1257b032f27cSSam Leffler 
1258b032f27cSSam Leffler 	/* complete setup */
1259b032f27cSSam Leffler 	ieee80211_vap_attach(vap, ath_media_change, ieee80211_media_status);
1260b032f27cSSam Leffler 	return vap;
1261b032f27cSSam Leffler bad2:
1262b032f27cSSam Leffler 	reclaim_address(sc, mac);
1263b032f27cSSam Leffler 	ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
1264b032f27cSSam Leffler bad:
1265b032f27cSSam Leffler 	free(avp, M_80211_VAP);
1266b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1267b032f27cSSam Leffler 	return NULL;
1268b032f27cSSam Leffler }
1269b032f27cSSam Leffler 
1270b032f27cSSam Leffler static void
1271b032f27cSSam Leffler ath_vap_delete(struct ieee80211vap *vap)
1272b032f27cSSam Leffler {
1273b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
1274b032f27cSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
1275b032f27cSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
1276b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1277b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
1278b032f27cSSam Leffler 
1279f52d3452SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: called\n", __func__);
1280b032f27cSSam Leffler 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1281b032f27cSSam Leffler 		/*
1282b032f27cSSam Leffler 		 * Quiesce the hardware while we remove the vap.  In
1283b032f27cSSam Leffler 		 * particular we need to reclaim all references to
1284b032f27cSSam Leffler 		 * the vap state by any frames pending on the tx queues.
1285b032f27cSSam Leffler 		 */
1286b032f27cSSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
1287517526efSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_DEFAULT);		/* stop hw xmit side */
1288517526efSAdrian Chadd 		/* XXX Do all frames from all vaps/nodes need draining here? */
12899a842e8bSAdrian Chadd 		ath_stoprecv(sc, 1);		/* stop recv side */
1290b032f27cSSam Leffler 	}
1291b032f27cSSam Leffler 
1292b032f27cSSam Leffler 	ieee80211_vap_detach(vap);
129316d4de92SAdrian Chadd 
129416d4de92SAdrian Chadd 	/*
129516d4de92SAdrian Chadd 	 * XXX Danger Will Robinson! Danger!
129616d4de92SAdrian Chadd 	 *
129716d4de92SAdrian Chadd 	 * Because ieee80211_vap_detach() can queue a frame (the station
129816d4de92SAdrian Chadd 	 * diassociate message?) after we've drained the TXQ and
129916d4de92SAdrian Chadd 	 * flushed the software TXQ, we will end up with a frame queued
130016d4de92SAdrian Chadd 	 * to a node whose vap is about to be freed.
130116d4de92SAdrian Chadd 	 *
130216d4de92SAdrian Chadd 	 * To work around this, flush the hardware/software again.
130316d4de92SAdrian Chadd 	 * This may be racy - the ath task may be running and the packet
130416d4de92SAdrian Chadd 	 * may be being scheduled between sw->hw txq. Tsk.
130516d4de92SAdrian Chadd 	 *
130616d4de92SAdrian Chadd 	 * TODO: figure out why a new node gets allocated somewhere around
130716d4de92SAdrian Chadd 	 * here (after the ath_tx_swq() call; and after an ath_stop_locked()
130816d4de92SAdrian Chadd 	 * call!)
130916d4de92SAdrian Chadd 	 */
131016d4de92SAdrian Chadd 
131116d4de92SAdrian Chadd 	ath_draintxq(sc, ATH_RESET_DEFAULT);
131216d4de92SAdrian Chadd 
1313b032f27cSSam Leffler 	ATH_LOCK(sc);
1314b032f27cSSam Leffler 	/*
1315b032f27cSSam Leffler 	 * Reclaim beacon state.  Note this must be done before
1316b032f27cSSam Leffler 	 * the vap instance is reclaimed as we may have a reference
1317b032f27cSSam Leffler 	 * to it in the buffer for the beacon frame.
1318b032f27cSSam Leffler 	 */
1319b032f27cSSam Leffler 	if (avp->av_bcbuf != NULL) {
1320b032f27cSSam Leffler 		if (avp->av_bslot != -1) {
1321b032f27cSSam Leffler 			sc->sc_bslot[avp->av_bslot] = NULL;
1322b032f27cSSam Leffler 			sc->sc_nbcnvaps--;
1323b032f27cSSam Leffler 		}
1324b032f27cSSam Leffler 		ath_beacon_return(sc, avp->av_bcbuf);
1325b032f27cSSam Leffler 		avp->av_bcbuf = NULL;
1326b032f27cSSam Leffler 		if (sc->sc_nbcnvaps == 0) {
1327b032f27cSSam Leffler 			sc->sc_stagbeacons = 0;
1328b032f27cSSam Leffler 			if (sc->sc_hastsfadd)
1329b032f27cSSam Leffler 				ath_hal_settsfadjust(sc->sc_ah, 0);
1330b032f27cSSam Leffler 		}
1331b032f27cSSam Leffler 		/*
1332b032f27cSSam Leffler 		 * Reclaim any pending mcast frames for the vap.
1333b032f27cSSam Leffler 		 */
1334b032f27cSSam Leffler 		ath_tx_draintxq(sc, &avp->av_mcastq);
1335b032f27cSSam Leffler 		ATH_TXQ_LOCK_DESTROY(&avp->av_mcastq);
1336b032f27cSSam Leffler 	}
1337b032f27cSSam Leffler 	/*
1338b032f27cSSam Leffler 	 * Update bookkeeping.
1339b032f27cSSam Leffler 	 */
1340b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_STA) {
1341b032f27cSSam Leffler 		sc->sc_nstavaps--;
1342b032f27cSSam Leffler 		if (sc->sc_nstavaps == 0 && sc->sc_swbmiss)
1343b032f27cSSam Leffler 			sc->sc_swbmiss = 0;
134459aa14a9SRui Paulo 	} else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
134559aa14a9SRui Paulo 	    vap->iv_opmode == IEEE80211_M_MBSS) {
1346b032f27cSSam Leffler 		reclaim_address(sc, vap->iv_myaddr);
1347b032f27cSSam Leffler 		ath_hal_setbssidmask(ah, sc->sc_hwbssidmask);
1348fe0dd789SSam Leffler 		if (vap->iv_opmode == IEEE80211_M_MBSS)
1349fe0dd789SSam Leffler 			sc->sc_nmeshvaps--;
1350b032f27cSSam Leffler 	}
1351b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_WDS)
1352b032f27cSSam Leffler 		sc->sc_nvaps--;
1353584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
135410ad9a77SSam Leffler 	/* TDMA operation ceases when the last vap is destroyed */
135510ad9a77SSam Leffler 	if (sc->sc_tdma && sc->sc_nvaps == 0) {
135610ad9a77SSam Leffler 		sc->sc_tdma = 0;
135710ad9a77SSam Leffler 		sc->sc_swbmiss = 0;
135810ad9a77SSam Leffler 	}
135910ad9a77SSam Leffler #endif
1360b032f27cSSam Leffler 	free(avp, M_80211_VAP);
1361b032f27cSSam Leffler 
1362b032f27cSSam Leffler 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1363b032f27cSSam Leffler 		/*
1364b032f27cSSam Leffler 		 * Restart rx+tx machines if still running (RUNNING will
1365b032f27cSSam Leffler 		 * be reset if we just destroyed the last vap).
1366b032f27cSSam Leffler 		 */
1367b032f27cSSam Leffler 		if (ath_startrecv(sc) != 0)
1368b032f27cSSam Leffler 			if_printf(ifp, "%s: unable to restart recv logic\n",
1369b032f27cSSam Leffler 			    __func__);
1370c89b957aSSam Leffler 		if (sc->sc_beacons) {		/* restart beacons */
1371c89b957aSSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
1372c89b957aSSam Leffler 			if (sc->sc_tdma)
1373c89b957aSSam Leffler 				ath_tdma_config(sc, NULL);
1374c89b957aSSam Leffler 			else
1375c89b957aSSam Leffler #endif
1376b032f27cSSam Leffler 				ath_beacon_config(sc, NULL);
1377c89b957aSSam Leffler 		}
1378b032f27cSSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
1379b032f27cSSam Leffler 	}
138016d4de92SAdrian Chadd 	ATH_UNLOCK(sc);
1381b032f27cSSam Leffler }
1382b032f27cSSam Leffler 
13835591b213SSam Leffler void
13845591b213SSam Leffler ath_suspend(struct ath_softc *sc)
13855591b213SSam Leffler {
1386fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
1387d3ac945bSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
13885591b213SSam Leffler 
1389c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1390c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
13915591b213SSam Leffler 
1392d3ac945bSSam Leffler 	sc->sc_resume_up = (ifp->if_flags & IFF_UP) != 0;
1393d1328898SAdrian Chadd 
1394d3ac945bSSam Leffler 	ieee80211_suspend_all(ic);
1395d3ac945bSSam Leffler 	/*
1396d3ac945bSSam Leffler 	 * NB: don't worry about putting the chip in low power
1397d3ac945bSSam Leffler 	 * mode; pci will power off our socket on suspend and
1398f29b8b7fSWarner Losh 	 * CardBus detaches the device.
1399d3ac945bSSam Leffler 	 */
1400d73df6d5SAdrian Chadd 
1401ae2a0aa4SAdrian Chadd 	/*
1402ae2a0aa4SAdrian Chadd 	 * XXX ensure none of the taskqueues are running
1403ae2a0aa4SAdrian Chadd 	 * XXX ensure sc_invalid is 1
1404ae2a0aa4SAdrian Chadd 	 * XXX ensure the calibration callout is disabled
1405ae2a0aa4SAdrian Chadd 	 */
1406ae2a0aa4SAdrian Chadd 
1407ae2a0aa4SAdrian Chadd 	/* Disable the PCIe PHY, complete with workarounds */
1408ae2a0aa4SAdrian Chadd 	ath_hal_enablepcie(sc->sc_ah, 1, 1);
1409d3ac945bSSam Leffler }
1410d3ac945bSSam Leffler 
1411d3ac945bSSam Leffler /*
1412d3ac945bSSam Leffler  * Reset the key cache since some parts do not reset the
1413d3ac945bSSam Leffler  * contents on resume.  First we clear all entries, then
1414d3ac945bSSam Leffler  * re-load keys that the 802.11 layer assumes are setup
1415d3ac945bSSam Leffler  * in h/w.
1416d3ac945bSSam Leffler  */
1417d3ac945bSSam Leffler static void
1418d3ac945bSSam Leffler ath_reset_keycache(struct ath_softc *sc)
1419d3ac945bSSam Leffler {
1420d3ac945bSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
1421d3ac945bSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
1422d3ac945bSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1423d3ac945bSSam Leffler 	int i;
1424d3ac945bSSam Leffler 
1425d3ac945bSSam Leffler 	for (i = 0; i < sc->sc_keymax; i++)
1426d3ac945bSSam Leffler 		ath_hal_keyreset(ah, i);
1427d3ac945bSSam Leffler 	ieee80211_crypto_reload_keys(ic);
14285591b213SSam Leffler }
14295591b213SSam Leffler 
14305591b213SSam Leffler void
14315591b213SSam Leffler ath_resume(struct ath_softc *sc)
14325591b213SSam Leffler {
1433fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
1434d3ac945bSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
1435d3ac945bSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
1436d3ac945bSSam Leffler 	HAL_STATUS status;
14375591b213SSam Leffler 
1438c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1439c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
14405591b213SSam Leffler 
1441d73df6d5SAdrian Chadd 	/* Re-enable PCIe, re-enable the PCIe bus */
1442ae2a0aa4SAdrian Chadd 	ath_hal_enablepcie(ah, 0, 0);
1443d73df6d5SAdrian Chadd 
1444d3ac945bSSam Leffler 	/*
1445d3ac945bSSam Leffler 	 * Must reset the chip before we reload the
1446d3ac945bSSam Leffler 	 * keycache as we were powered down on suspend.
1447d3ac945bSSam Leffler 	 */
1448054d7b69SSam Leffler 	ath_hal_reset(ah, sc->sc_opmode,
1449054d7b69SSam Leffler 	    sc->sc_curchan != NULL ? sc->sc_curchan : ic->ic_curchan,
1450054d7b69SSam Leffler 	    AH_FALSE, &status);
1451d3ac945bSSam Leffler 	ath_reset_keycache(sc);
14527e5eb44dSAdrian Chadd 
14537e5eb44dSAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
14547e5eb44dSAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
14557e5eb44dSAdrian Chadd 
1456a497cd88SAdrian Chadd 	/* Restore the LED configuration */
1457a497cd88SAdrian Chadd 	ath_led_config(sc);
1458a497cd88SAdrian Chadd 	ath_hal_setledstate(ah, HAL_LED_INIT);
1459a497cd88SAdrian Chadd 
1460d1328898SAdrian Chadd 	if (sc->sc_resume_up)
1461021a0db5SAdrian Chadd 		ieee80211_resume_all(ic);
14622fd9aabbSAdrian Chadd 
14632fd9aabbSAdrian Chadd 	/* XXX beacons ? */
14646b59f5e3SSam Leffler }
14655591b213SSam Leffler 
14665591b213SSam Leffler void
14675591b213SSam Leffler ath_shutdown(struct ath_softc *sc)
14685591b213SSam Leffler {
1469fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
14705591b213SSam Leffler 
1471c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1472c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
14735591b213SSam Leffler 
14745591b213SSam Leffler 	ath_stop(ifp);
1475d3ac945bSSam Leffler 	/* NB: no point powering down chip as we're about to reboot */
14765591b213SSam Leffler }
14775591b213SSam Leffler 
1478c42a7b7eSSam Leffler /*
1479c42a7b7eSSam Leffler  * Interrupt handler.  Most of the actual processing is deferred.
1480c42a7b7eSSam Leffler  */
14815591b213SSam Leffler void
14825591b213SSam Leffler ath_intr(void *arg)
14835591b213SSam Leffler {
14845591b213SSam Leffler 	struct ath_softc *sc = arg;
1485fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
14865591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
14876f5fe81eSAdrian Chadd 	HAL_INT status = 0;
14888f939e79SAdrian Chadd 	uint32_t txqs;
14895591b213SSam Leffler 
1490ef27340cSAdrian Chadd 	/*
1491ef27340cSAdrian Chadd 	 * If we're inside a reset path, just print a warning and
1492ef27340cSAdrian Chadd 	 * clear the ISR. The reset routine will finish it for us.
1493ef27340cSAdrian Chadd 	 */
1494ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
1495ef27340cSAdrian Chadd 	if (sc->sc_inreset_cnt) {
1496ef27340cSAdrian Chadd 		HAL_INT status;
1497ef27340cSAdrian Chadd 		ath_hal_getisr(ah, &status);	/* clear ISR */
1498ef27340cSAdrian Chadd 		ath_hal_intrset(ah, 0);		/* disable further intr's */
1499ef27340cSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_ANY,
1500ef27340cSAdrian Chadd 		    "%s: in reset, ignoring: status=0x%x\n",
1501ef27340cSAdrian Chadd 		    __func__, status);
1502ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
1503ef27340cSAdrian Chadd 		return;
1504ef27340cSAdrian Chadd 	}
1505ef27340cSAdrian Chadd 
15065591b213SSam Leffler 	if (sc->sc_invalid) {
15075591b213SSam Leffler 		/*
1508b58b3803SSam Leffler 		 * The hardware is not ready/present, don't touch anything.
1509b58b3803SSam Leffler 		 * Note this can happen early on if the IRQ is shared.
15105591b213SSam Leffler 		 */
1511c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid; ignored\n", __func__);
1512ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
15135591b213SSam Leffler 		return;
15145591b213SSam Leffler 	}
1515ef27340cSAdrian Chadd 	if (!ath_hal_intrpend(ah)) {		/* shared irq, not for us */
1516ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
1517fdd758d4SSam Leffler 		return;
1518ef27340cSAdrian Chadd 	}
1519ef27340cSAdrian Chadd 
152068e8e04eSSam Leffler 	if ((ifp->if_flags & IFF_UP) == 0 ||
152168e8e04eSSam Leffler 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
152268e8e04eSSam Leffler 		HAL_INT status;
152368e8e04eSSam Leffler 
1524c42a7b7eSSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
1525c42a7b7eSSam Leffler 			__func__, ifp->if_flags);
15265591b213SSam Leffler 		ath_hal_getisr(ah, &status);	/* clear ISR */
15275591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable further intr's */
1528ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
15295591b213SSam Leffler 		return;
15305591b213SSam Leffler 	}
1531ef27340cSAdrian Chadd 
1532c42a7b7eSSam Leffler 	/*
1533c42a7b7eSSam Leffler 	 * Figure out the reason(s) for the interrupt.  Note
1534c42a7b7eSSam Leffler 	 * that the hal returns a pseudo-ISR that may include
1535c42a7b7eSSam Leffler 	 * bits we haven't explicitly enabled so we mask the
1536c42a7b7eSSam Leffler 	 * value to insure we only process bits we requested.
1537c42a7b7eSSam Leffler 	 */
15385591b213SSam Leffler 	ath_hal_getisr(ah, &status);		/* NB: clears ISR too */
1539c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_INTR, "%s: status 0x%x\n", __func__, status);
154003682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_INTERRUPTS, 1, "ath_intr: mask=0x%.8x", status);
154131fdf3d6SAdrian Chadd #ifdef	ATH_KTR_INTR_DEBUG
154203682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_INTERRUPTS, 5,
1543f52d3452SAdrian Chadd 	    "ath_intr: ISR=0x%.8x, ISR_S0=0x%.8x, ISR_S1=0x%.8x, ISR_S2=0x%.8x, ISR_S5=0x%.8x",
1544f52d3452SAdrian Chadd 	    ah->ah_intrstate[0],
1545f52d3452SAdrian Chadd 	    ah->ah_intrstate[1],
1546f52d3452SAdrian Chadd 	    ah->ah_intrstate[2],
1547f52d3452SAdrian Chadd 	    ah->ah_intrstate[3],
1548f52d3452SAdrian Chadd 	    ah->ah_intrstate[6]);
154931fdf3d6SAdrian Chadd #endif
15509467e3f3SAdrian Chadd 
15519467e3f3SAdrian Chadd 	/* Squirrel away SYNC interrupt debugging */
15529467e3f3SAdrian Chadd 	if (ah->ah_syncstate != 0) {
15539467e3f3SAdrian Chadd 		int i;
15549467e3f3SAdrian Chadd 		for (i = 0; i < 32; i++)
15559467e3f3SAdrian Chadd 			if (ah->ah_syncstate & (i << i))
15569467e3f3SAdrian Chadd 				sc->sc_intr_stats.sync_intr[i]++;
15579467e3f3SAdrian Chadd 	}
15589467e3f3SAdrian Chadd 
1559ecddff40SSam Leffler 	status &= sc->sc_imask;			/* discard unasked for bits */
15606f5fe81eSAdrian Chadd 
15616f5fe81eSAdrian Chadd 	/* Short-circuit un-handled interrupts */
1562ef27340cSAdrian Chadd 	if (status == 0x0) {
1563ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
15646f5fe81eSAdrian Chadd 		return;
1565ef27340cSAdrian Chadd 	}
15666f5fe81eSAdrian Chadd 
1567ef27340cSAdrian Chadd 	/*
1568ef27340cSAdrian Chadd 	 * Take a note that we're inside the interrupt handler, so
1569ef27340cSAdrian Chadd 	 * the reset routines know to wait.
1570ef27340cSAdrian Chadd 	 */
1571ef27340cSAdrian Chadd 	sc->sc_intr_cnt++;
1572ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
1573ef27340cSAdrian Chadd 
1574ef27340cSAdrian Chadd 	/*
1575ef27340cSAdrian Chadd 	 * Handle the interrupt. We won't run concurrent with the reset
1576ef27340cSAdrian Chadd 	 * or channel change routines as they'll wait for sc_intr_cnt
1577ef27340cSAdrian Chadd 	 * to be 0 before continuing.
1578ef27340cSAdrian Chadd 	 */
15795591b213SSam Leffler 	if (status & HAL_INT_FATAL) {
15805591b213SSam Leffler 		sc->sc_stats.ast_hardware++;
15815591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
1582f846cf42SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_fataltask);
15835591b213SSam Leffler 	} else {
1584c42a7b7eSSam Leffler 		if (status & HAL_INT_SWBA) {
1585c42a7b7eSSam Leffler 			/*
1586c42a7b7eSSam Leffler 			 * Software beacon alert--time to send a beacon.
1587c42a7b7eSSam Leffler 			 * Handle beacon transmission directly; deferring
1588c42a7b7eSSam Leffler 			 * this is too slow to meet timing constraints
1589c42a7b7eSSam Leffler 			 * under load.
1590c42a7b7eSSam Leffler 			 */
1591584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
159210ad9a77SSam Leffler 			if (sc->sc_tdma) {
159310ad9a77SSam Leffler 				if (sc->sc_tdmaswba == 0) {
159410ad9a77SSam Leffler 					struct ieee80211com *ic = ifp->if_l2com;
159510ad9a77SSam Leffler 					struct ieee80211vap *vap =
159610ad9a77SSam Leffler 					    TAILQ_FIRST(&ic->ic_vaps);
159710ad9a77SSam Leffler 					ath_tdma_beacon_send(sc, vap);
159810ad9a77SSam Leffler 					sc->sc_tdmaswba =
159910ad9a77SSam Leffler 					    vap->iv_tdma->tdma_bintval;
160010ad9a77SSam Leffler 				} else
160110ad9a77SSam Leffler 					sc->sc_tdmaswba--;
160210ad9a77SSam Leffler 			} else
160310ad9a77SSam Leffler #endif
1604339ccfb3SSam Leffler 			{
1605c42a7b7eSSam Leffler 				ath_beacon_proc(sc, 0);
1606339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
1607339ccfb3SSam Leffler 				/*
1608339ccfb3SSam Leffler 				 * Schedule the rx taskq in case there's no
1609339ccfb3SSam Leffler 				 * traffic so any frames held on the staging
1610339ccfb3SSam Leffler 				 * queue are aged and potentially flushed.
1611339ccfb3SSam Leffler 				 */
1612339ccfb3SSam Leffler 				taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
1613339ccfb3SSam Leffler #endif
1614339ccfb3SSam Leffler 			}
1615c42a7b7eSSam Leffler 		}
16165591b213SSam Leffler 		if (status & HAL_INT_RXEOL) {
16178f939e79SAdrian Chadd 			int imask;
161803682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_ERROR, 0, "ath_intr: RXEOL");
1619ef27340cSAdrian Chadd 			ATH_PCU_LOCK(sc);
16205591b213SSam Leffler 			/*
16215591b213SSam Leffler 			 * NB: the hardware should re-read the link when
16225591b213SSam Leffler 			 *     RXE bit is written, but it doesn't work at
16235591b213SSam Leffler 			 *     least on older hardware revs.
16245591b213SSam Leffler 			 */
16255591b213SSam Leffler 			sc->sc_stats.ast_rxeol++;
162673f895fcSAdrian Chadd 			/*
162773f895fcSAdrian Chadd 			 * Disable RXEOL/RXORN - prevent an interrupt
162873f895fcSAdrian Chadd 			 * storm until the PCU logic can be reset.
16291fdadc0fSAdrian Chadd 			 * In case the interface is reset some other
16301fdadc0fSAdrian Chadd 			 * way before "sc_kickpcu" is called, don't
16311fdadc0fSAdrian Chadd 			 * modify sc_imask - that way if it is reset
16321fdadc0fSAdrian Chadd 			 * by a call to ath_reset() somehow, the
16331fdadc0fSAdrian Chadd 			 * interrupt mask will be correctly reprogrammed.
163473f895fcSAdrian Chadd 			 */
16358f939e79SAdrian Chadd 			imask = sc->sc_imask;
16361fdadc0fSAdrian Chadd 			imask &= ~(HAL_INT_RXEOL | HAL_INT_RXORN);
16371fdadc0fSAdrian Chadd 			ath_hal_intrset(ah, imask);
16381fdadc0fSAdrian Chadd 			/*
16398f939e79SAdrian Chadd 			 * Only blank sc_rxlink if we've not yet kicked
16408f939e79SAdrian Chadd 			 * the PCU.
16418f939e79SAdrian Chadd 			 *
16428f939e79SAdrian Chadd 			 * This isn't entirely correct - the correct solution
16438f939e79SAdrian Chadd 			 * would be to have a PCU lock and engage that for
16448f939e79SAdrian Chadd 			 * the duration of the PCU fiddling; which would include
16458f939e79SAdrian Chadd 			 * running the RX process. Otherwise we could end up
16468f939e79SAdrian Chadd 			 * messing up the RX descriptor chain and making the
16478f939e79SAdrian Chadd 			 * RX desc list much shorter.
16488f939e79SAdrian Chadd 			 */
16498f939e79SAdrian Chadd 			if (! sc->sc_kickpcu)
16508f939e79SAdrian Chadd 				sc->sc_rxlink = NULL;
16518f939e79SAdrian Chadd 			sc->sc_kickpcu = 1;
16528f939e79SAdrian Chadd 			/*
16531fdadc0fSAdrian Chadd 			 * Enqueue an RX proc, to handled whatever
16541fdadc0fSAdrian Chadd 			 * is in the RX queue.
16551fdadc0fSAdrian Chadd 			 * This will then kick the PCU.
16561fdadc0fSAdrian Chadd 			 */
16571fdadc0fSAdrian Chadd 			taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
1658ef27340cSAdrian Chadd 			ATH_PCU_UNLOCK(sc);
16595591b213SSam Leffler 		}
16605591b213SSam Leffler 		if (status & HAL_INT_TXURN) {
16615591b213SSam Leffler 			sc->sc_stats.ast_txurn++;
16625591b213SSam Leffler 			/* bump tx trigger level */
16635591b213SSam Leffler 			ath_hal_updatetxtriglevel(ah, AH_TRUE);
16645591b213SSam Leffler 		}
1665bcbb08ceSAdrian Chadd 		/*
1666bcbb08ceSAdrian Chadd 		 * Handle both the legacy and RX EDMA interrupt bits.
1667bcbb08ceSAdrian Chadd 		 * Note that HAL_INT_RXLP is also HAL_INT_RXDESC.
1668bcbb08ceSAdrian Chadd 		 */
1669bcbb08ceSAdrian Chadd 		if (status & (HAL_INT_RX | HAL_INT_RXHP | HAL_INT_RXLP)) {
16708f939e79SAdrian Chadd 			sc->sc_stats.ast_rx_intr++;
16710bbf5441SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
16728f939e79SAdrian Chadd 		}
16738f939e79SAdrian Chadd 		if (status & HAL_INT_TX) {
16748f939e79SAdrian Chadd 			sc->sc_stats.ast_tx_intr++;
16758f939e79SAdrian Chadd 			/*
16768f939e79SAdrian Chadd 			 * Grab all the currently set bits in the HAL txq bitmap
16778f939e79SAdrian Chadd 			 * and blank them. This is the only place we should be
16788f939e79SAdrian Chadd 			 * doing this.
16798f939e79SAdrian Chadd 			 */
1680bad98824SAdrian Chadd 			if (! sc->sc_isedma) {
1681ef27340cSAdrian Chadd 				ATH_PCU_LOCK(sc);
16828f939e79SAdrian Chadd 				txqs = 0xffffffff;
16838f939e79SAdrian Chadd 				ath_hal_gettxintrtxqs(sc->sc_ah, &txqs);
168403682514SAdrian Chadd 				ATH_KTR(sc, ATH_KTR_INTERRUPTS, 3,
168503682514SAdrian Chadd 				    "ath_intr: TX; txqs=0x%08x, txq_active was 0x%08x, now 0x%08x",
168603682514SAdrian Chadd 				    txqs,
168703682514SAdrian Chadd 				    sc->sc_txq_active,
168803682514SAdrian Chadd 				    sc->sc_txq_active | txqs);
16898f939e79SAdrian Chadd 				sc->sc_txq_active |= txqs;
1690ef27340cSAdrian Chadd 				ATH_PCU_UNLOCK(sc);
16918f939e79SAdrian Chadd 			}
1692bad98824SAdrian Chadd 			taskqueue_enqueue(sc->sc_tq, &sc->sc_txtask);
1693bad98824SAdrian Chadd 		}
16945591b213SSam Leffler 		if (status & HAL_INT_BMISS) {
16955591b213SSam Leffler 			sc->sc_stats.ast_bmiss++;
16960bbf5441SSam Leffler 			taskqueue_enqueue(sc->sc_tq, &sc->sc_bmisstask);
16975591b213SSam Leffler 		}
16986ad02dbaSAdrian Chadd 		if (status & HAL_INT_GTT)
16996ad02dbaSAdrian Chadd 			sc->sc_stats.ast_tx_timeout++;
17005594f5c0SAdrian Chadd 		if (status & HAL_INT_CST)
17015594f5c0SAdrian Chadd 			sc->sc_stats.ast_tx_cst++;
1702c42a7b7eSSam Leffler 		if (status & HAL_INT_MIB) {
1703c42a7b7eSSam Leffler 			sc->sc_stats.ast_mib++;
1704ef27340cSAdrian Chadd 			ATH_PCU_LOCK(sc);
1705c42a7b7eSSam Leffler 			/*
1706c42a7b7eSSam Leffler 			 * Disable interrupts until we service the MIB
1707c42a7b7eSSam Leffler 			 * interrupt; otherwise it will continue to fire.
1708c42a7b7eSSam Leffler 			 */
1709c42a7b7eSSam Leffler 			ath_hal_intrset(ah, 0);
1710c42a7b7eSSam Leffler 			/*
1711c42a7b7eSSam Leffler 			 * Let the hal handle the event.  We assume it will
1712c42a7b7eSSam Leffler 			 * clear whatever condition caused the interrupt.
1713c42a7b7eSSam Leffler 			 */
1714ffa2cab6SSam Leffler 			ath_hal_mibevent(ah, &sc->sc_halstats);
17158f939e79SAdrian Chadd 			/*
17168f939e79SAdrian Chadd 			 * Don't reset the interrupt if we've just
17178f939e79SAdrian Chadd 			 * kicked the PCU, or we may get a nested
17188f939e79SAdrian Chadd 			 * RXEOL before the rxproc has had a chance
17198f939e79SAdrian Chadd 			 * to run.
17208f939e79SAdrian Chadd 			 */
17218f939e79SAdrian Chadd 			if (sc->sc_kickpcu == 0)
1722c42a7b7eSSam Leffler 				ath_hal_intrset(ah, sc->sc_imask);
1723ef27340cSAdrian Chadd 			ATH_PCU_UNLOCK(sc);
1724c42a7b7eSSam Leffler 		}
17259c4fc1e8SSam Leffler 		if (status & HAL_INT_RXORN) {
17269c4fc1e8SSam Leffler 			/* NB: hal marks HAL_INT_FATAL when RXORN is fatal */
172703682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_ERROR, 0, "ath_intr: RXORN");
17289c4fc1e8SSam Leffler 			sc->sc_stats.ast_rxorn++;
17299c4fc1e8SSam Leffler 		}
17305591b213SSam Leffler 	}
1731ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
1732ef27340cSAdrian Chadd 	sc->sc_intr_cnt--;
1733ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
17345591b213SSam Leffler }
17355591b213SSam Leffler 
17365591b213SSam Leffler static void
17375591b213SSam Leffler ath_fatal_proc(void *arg, int pending)
17385591b213SSam Leffler {
17395591b213SSam Leffler 	struct ath_softc *sc = arg;
1740fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
174116c8acaaSSam Leffler 	u_int32_t *state;
174216c8acaaSSam Leffler 	u_int32_t len;
174368e8e04eSSam Leffler 	void *sp;
17445591b213SSam Leffler 
1745c42a7b7eSSam Leffler 	if_printf(ifp, "hardware error; resetting\n");
174616c8acaaSSam Leffler 	/*
174716c8acaaSSam Leffler 	 * Fatal errors are unrecoverable.  Typically these
174816c8acaaSSam Leffler 	 * are caused by DMA errors.  Collect h/w state from
174916c8acaaSSam Leffler 	 * the hal so we can diagnose what's going on.
175016c8acaaSSam Leffler 	 */
175168e8e04eSSam Leffler 	if (ath_hal_getfatalstate(sc->sc_ah, &sp, &len)) {
175216c8acaaSSam Leffler 		KASSERT(len >= 6*sizeof(u_int32_t), ("len %u bytes", len));
175368e8e04eSSam Leffler 		state = sp;
175416c8acaaSSam Leffler 		if_printf(ifp, "0x%08x 0x%08x 0x%08x, 0x%08x 0x%08x 0x%08x\n",
175516c8acaaSSam Leffler 		    state[0], state[1] , state[2], state[3],
175616c8acaaSSam Leffler 		    state[4], state[5]);
175716c8acaaSSam Leffler 	}
1758517526efSAdrian Chadd 	ath_reset(ifp, ATH_RESET_NOLOSS);
17595591b213SSam Leffler }
17605591b213SSam Leffler 
17615591b213SSam Leffler static void
1762b032f27cSSam Leffler ath_bmiss_vap(struct ieee80211vap *vap)
17635591b213SSam Leffler {
176459fbb257SSam Leffler 	/*
176559fbb257SSam Leffler 	 * Workaround phantom bmiss interrupts by sanity-checking
176659fbb257SSam Leffler 	 * the time of our last rx'd frame.  If it is within the
176759fbb257SSam Leffler 	 * beacon miss interval then ignore the interrupt.  If it's
176859fbb257SSam Leffler 	 * truly a bmiss we'll get another interrupt soon and that'll
176959fbb257SSam Leffler 	 * be dispatched up for processing.  Note this applies only
177059fbb257SSam Leffler 	 * for h/w beacon miss events.
177159fbb257SSam Leffler 	 */
177259fbb257SSam Leffler 	if ((vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) == 0) {
1773a7ace843SSam Leffler 		struct ifnet *ifp = vap->iv_ic->ic_ifp;
1774a7ace843SSam Leffler 		struct ath_softc *sc = ifp->if_softc;
1775d7736e13SSam Leffler 		u_int64_t lastrx = sc->sc_lastrx;
1776d7736e13SSam Leffler 		u_int64_t tsf = ath_hal_gettsf64(sc->sc_ah);
177780767531SAdrian Chadd 		/* XXX should take a locked ref to iv_bss */
1778d7736e13SSam Leffler 		u_int bmisstimeout =
1779b032f27cSSam Leffler 			vap->iv_bmissthreshold * vap->iv_bss->ni_intval * 1024;
1780d7736e13SSam Leffler 
1781d7736e13SSam Leffler 		DPRINTF(sc, ATH_DEBUG_BEACON,
1782d7736e13SSam Leffler 		    "%s: tsf %llu lastrx %lld (%llu) bmiss %u\n",
1783d7736e13SSam Leffler 		    __func__, (unsigned long long) tsf,
1784d7736e13SSam Leffler 		    (unsigned long long)(tsf - lastrx),
1785d7736e13SSam Leffler 		    (unsigned long long) lastrx, bmisstimeout);
178659fbb257SSam Leffler 
178759fbb257SSam Leffler 		if (tsf - lastrx <= bmisstimeout) {
1788d7736e13SSam Leffler 			sc->sc_stats.ast_bmiss_phantom++;
178959fbb257SSam Leffler 			return;
179059fbb257SSam Leffler 		}
179159fbb257SSam Leffler 	}
179259fbb257SSam Leffler 	ATH_VAP(vap)->av_bmiss(vap);
1793e585d188SSam Leffler }
1794b032f27cSSam Leffler 
1795459bc4f0SSam Leffler static int
1796459bc4f0SSam Leffler ath_hal_gethangstate(struct ath_hal *ah, uint32_t mask, uint32_t *hangs)
1797459bc4f0SSam Leffler {
1798459bc4f0SSam Leffler 	uint32_t rsize;
1799459bc4f0SSam Leffler 	void *sp;
1800459bc4f0SSam Leffler 
180125c96056SAdrian Chadd 	if (!ath_hal_getdiagstate(ah, HAL_DIAG_CHECK_HANGS, &mask, sizeof(mask), &sp, &rsize))
1802459bc4f0SSam Leffler 		return 0;
1803459bc4f0SSam Leffler 	KASSERT(rsize == sizeof(uint32_t), ("resultsize %u", rsize));
1804459bc4f0SSam Leffler 	*hangs = *(uint32_t *)sp;
1805459bc4f0SSam Leffler 	return 1;
1806459bc4f0SSam Leffler }
1807459bc4f0SSam Leffler 
1808b032f27cSSam Leffler static void
1809b032f27cSSam Leffler ath_bmiss_proc(void *arg, int pending)
1810b032f27cSSam Leffler {
1811b032f27cSSam Leffler 	struct ath_softc *sc = arg;
1812b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
1813459bc4f0SSam Leffler 	uint32_t hangs;
1814b032f27cSSam Leffler 
1815b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: pending %u\n", __func__, pending);
1816459bc4f0SSam Leffler 
1817459bc4f0SSam Leffler 	if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0) {
18184fa8d4efSDaniel Eischen 		if_printf(ifp, "bb hang detected (0x%x), resetting\n", hangs);
1819517526efSAdrian Chadd 		ath_reset(ifp, ATH_RESET_NOLOSS);
1820459bc4f0SSam Leffler 	} else
1821b032f27cSSam Leffler 		ieee80211_beacon_miss(ifp->if_l2com);
18225591b213SSam Leffler }
18235591b213SSam Leffler 
1824724c193aSSam Leffler /*
1825b032f27cSSam Leffler  * Handle TKIP MIC setup to deal hardware that doesn't do MIC
1826b032f27cSSam Leffler  * calcs together with WME.  If necessary disable the crypto
1827b032f27cSSam Leffler  * hardware and mark the 802.11 state so keys will be setup
1828b032f27cSSam Leffler  * with the MIC work done in software.
1829b032f27cSSam Leffler  */
1830b032f27cSSam Leffler static void
1831b032f27cSSam Leffler ath_settkipmic(struct ath_softc *sc)
1832b032f27cSSam Leffler {
1833b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
1834b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
1835b032f27cSSam Leffler 
1836b032f27cSSam Leffler 	if ((ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIP) && !sc->sc_wmetkipmic) {
1837b032f27cSSam Leffler 		if (ic->ic_flags & IEEE80211_F_WME) {
1838b032f27cSSam Leffler 			ath_hal_settkipmic(sc->sc_ah, AH_FALSE);
1839b032f27cSSam Leffler 			ic->ic_cryptocaps &= ~IEEE80211_CRYPTO_TKIPMIC;
1840b032f27cSSam Leffler 		} else {
1841b032f27cSSam Leffler 			ath_hal_settkipmic(sc->sc_ah, AH_TRUE);
1842b032f27cSSam Leffler 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
1843b032f27cSSam Leffler 		}
1844b032f27cSSam Leffler 	}
1845b032f27cSSam Leffler }
1846b032f27cSSam Leffler 
18475591b213SSam Leffler static void
18485591b213SSam Leffler ath_init(void *arg)
18495591b213SSam Leffler {
18505591b213SSam Leffler 	struct ath_softc *sc = (struct ath_softc *) arg;
1851fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
1852b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
18535591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
18545591b213SSam Leffler 	HAL_STATUS status;
18555591b213SSam Leffler 
1856c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
1857c42a7b7eSSam Leffler 		__func__, ifp->if_flags);
18585591b213SSam Leffler 
1859f0b2a0beSSam Leffler 	ATH_LOCK(sc);
18605591b213SSam Leffler 	/*
18615591b213SSam Leffler 	 * Stop anything previously setup.  This is safe
18625591b213SSam Leffler 	 * whether this is the first time through or not.
18635591b213SSam Leffler 	 */
1864c42a7b7eSSam Leffler 	ath_stop_locked(ifp);
18655591b213SSam Leffler 
18665591b213SSam Leffler 	/*
18675591b213SSam Leffler 	 * The basic interface to setting the hardware in a good
18685591b213SSam Leffler 	 * state is ``reset''.  On return the hardware is known to
18695591b213SSam Leffler 	 * be powered up and with interrupts disabled.  This must
18705591b213SSam Leffler 	 * be followed by initialization of the appropriate bits
18715591b213SSam Leffler 	 * and then setup of the interrupt mask.
18725591b213SSam Leffler 	 */
1873b032f27cSSam Leffler 	ath_settkipmic(sc);
187459efa8b5SSam Leffler 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_FALSE, &status)) {
18755591b213SSam Leffler 		if_printf(ifp, "unable to reset hardware; hal status %u\n",
18765591b213SSam Leffler 			status);
1877b032f27cSSam Leffler 		ATH_UNLOCK(sc);
1878b032f27cSSam Leffler 		return;
18795591b213SSam Leffler 	}
1880b032f27cSSam Leffler 	ath_chan_change(sc, ic->ic_curchan);
18815591b213SSam Leffler 
188248237774SAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
188348237774SAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
188448237774SAdrian Chadd 
18855591b213SSam Leffler 	/*
1886c59005e9SSam Leffler 	 * Likewise this is set during reset so update
1887c59005e9SSam Leffler 	 * state cached in the driver.
1888c59005e9SSam Leffler 	 */
1889c59005e9SSam Leffler 	sc->sc_diversity = ath_hal_getdiversity(ah);
18902dc7fcc4SSam Leffler 	sc->sc_lastlongcal = 0;
18912dc7fcc4SSam Leffler 	sc->sc_resetcal = 1;
18922dc7fcc4SSam Leffler 	sc->sc_lastcalreset = 0;
1893a108ab63SAdrian Chadd 	sc->sc_lastani = 0;
1894a108ab63SAdrian Chadd 	sc->sc_lastshortcal = 0;
1895a108ab63SAdrian Chadd 	sc->sc_doresetcal = AH_FALSE;
18962fd9aabbSAdrian Chadd 	/*
18972fd9aabbSAdrian Chadd 	 * Beacon timers were cleared here; give ath_newstate()
18982fd9aabbSAdrian Chadd 	 * a hint that the beacon timers should be poked when
18992fd9aabbSAdrian Chadd 	 * things transition to the RUN state.
19002fd9aabbSAdrian Chadd 	 */
19012fd9aabbSAdrian Chadd 	sc->sc_beacons = 0;
1902c42a7b7eSSam Leffler 
1903c42a7b7eSSam Leffler 	/*
19045591b213SSam Leffler 	 * Setup the hardware after reset: the key cache
19055591b213SSam Leffler 	 * is filled as needed and the receive engine is
19065591b213SSam Leffler 	 * set going.  Frame transmit is handled entirely
19075591b213SSam Leffler 	 * in the frame output path; there's nothing to do
19085591b213SSam Leffler 	 * here except setup the interrupt mask.
19095591b213SSam Leffler 	 */
19105591b213SSam Leffler 	if (ath_startrecv(sc) != 0) {
19115591b213SSam Leffler 		if_printf(ifp, "unable to start recv logic\n");
1912b032f27cSSam Leffler 		ATH_UNLOCK(sc);
1913b032f27cSSam Leffler 		return;
19145591b213SSam Leffler 	}
19155591b213SSam Leffler 
19165591b213SSam Leffler 	/*
19175591b213SSam Leffler 	 * Enable interrupts.
19185591b213SSam Leffler 	 */
19195591b213SSam Leffler 	sc->sc_imask = HAL_INT_RX | HAL_INT_TX
19205591b213SSam Leffler 		  | HAL_INT_RXEOL | HAL_INT_RXORN
19215591b213SSam Leffler 		  | HAL_INT_FATAL | HAL_INT_GLOBAL;
1922bcbb08ceSAdrian Chadd 
1923bcbb08ceSAdrian Chadd 	/*
1924bcbb08ceSAdrian Chadd 	 * Enable RX EDMA bits.  Note these overlap with
1925bcbb08ceSAdrian Chadd 	 * HAL_INT_RX and HAL_INT_RXDESC respectively.
1926bcbb08ceSAdrian Chadd 	 */
1927bcbb08ceSAdrian Chadd 	if (sc->sc_isedma)
1928bcbb08ceSAdrian Chadd 		sc->sc_imask |= (HAL_INT_RXHP | HAL_INT_RXLP);
1929bcbb08ceSAdrian Chadd 
1930c42a7b7eSSam Leffler 	/*
1931c42a7b7eSSam Leffler 	 * Enable MIB interrupts when there are hardware phy counters.
1932c42a7b7eSSam Leffler 	 * Note we only do this (at the moment) for station mode.
1933c42a7b7eSSam Leffler 	 */
1934c42a7b7eSSam Leffler 	if (sc->sc_needmib && ic->ic_opmode == IEEE80211_M_STA)
1935c42a7b7eSSam Leffler 		sc->sc_imask |= HAL_INT_MIB;
19365591b213SSam Leffler 
19375594f5c0SAdrian Chadd 	/* Enable global TX timeout and carrier sense timeout if available */
19386ad02dbaSAdrian Chadd 	if (ath_hal_gtxto_supported(ah))
19393788ebedSAdrian Chadd 		sc->sc_imask |= HAL_INT_GTT;
1940d0a0ebc6SAdrian Chadd 
1941d0a0ebc6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: imask=0x%x\n",
1942d0a0ebc6SAdrian Chadd 		__func__, sc->sc_imask);
19436ad02dbaSAdrian Chadd 
194413f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
19452e986da5SSam Leffler 	callout_reset(&sc->sc_wd_ch, hz, ath_watchdog, sc);
1946b032f27cSSam Leffler 	ath_hal_intrset(ah, sc->sc_imask);
19475591b213SSam Leffler 
1948b032f27cSSam Leffler 	ATH_UNLOCK(sc);
1949b032f27cSSam Leffler 
195086e07743SSam Leffler #ifdef ATH_TX99_DIAG
195186e07743SSam Leffler 	if (sc->sc_tx99 != NULL)
195286e07743SSam Leffler 		sc->sc_tx99->start(sc->sc_tx99);
195386e07743SSam Leffler 	else
195486e07743SSam Leffler #endif
1955b032f27cSSam Leffler 	ieee80211_start_all(ic);		/* start all vap's */
19565591b213SSam Leffler }
19575591b213SSam Leffler 
19585591b213SSam Leffler static void
1959c42a7b7eSSam Leffler ath_stop_locked(struct ifnet *ifp)
19605591b213SSam Leffler {
19615591b213SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
19625591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
19635591b213SSam Leffler 
1964c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid %u if_flags 0x%x\n",
1965c42a7b7eSSam Leffler 		__func__, sc->sc_invalid, ifp->if_flags);
19665591b213SSam Leffler 
1967c42a7b7eSSam Leffler 	ATH_LOCK_ASSERT(sc);
196813f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
19695591b213SSam Leffler 		/*
19705591b213SSam Leffler 		 * Shutdown the hardware and driver:
1971c42a7b7eSSam Leffler 		 *    reset 802.11 state machine
19725591b213SSam Leffler 		 *    turn off timers
1973c42a7b7eSSam Leffler 		 *    disable interrupts
1974c42a7b7eSSam Leffler 		 *    turn off the radio
19755591b213SSam Leffler 		 *    clear transmit machinery
19765591b213SSam Leffler 		 *    clear receive machinery
19775591b213SSam Leffler 		 *    drain and release tx queues
19785591b213SSam Leffler 		 *    reclaim beacon resources
19795591b213SSam Leffler 		 *    power down hardware
19805591b213SSam Leffler 		 *
19815591b213SSam Leffler 		 * Note that some of this work is not possible if the
19825591b213SSam Leffler 		 * hardware is gone (invalid).
19835591b213SSam Leffler 		 */
198486e07743SSam Leffler #ifdef ATH_TX99_DIAG
198586e07743SSam Leffler 		if (sc->sc_tx99 != NULL)
198686e07743SSam Leffler 			sc->sc_tx99->stop(sc->sc_tx99);
198786e07743SSam Leffler #endif
19882e986da5SSam Leffler 		callout_stop(&sc->sc_wd_ch);
19892e986da5SSam Leffler 		sc->sc_wd_timer = 0;
199013f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1991c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
19923e50ec2cSSam Leffler 			if (sc->sc_softled) {
19933e50ec2cSSam Leffler 				callout_stop(&sc->sc_ledtimer);
19943e50ec2cSSam Leffler 				ath_hal_gpioset(ah, sc->sc_ledpin,
19953e50ec2cSSam Leffler 					!sc->sc_ledon);
19963e50ec2cSSam Leffler 				sc->sc_blinking = 0;
19973e50ec2cSSam Leffler 			}
19985591b213SSam Leffler 			ath_hal_intrset(ah, 0);
1999c42a7b7eSSam Leffler 		}
2000517526efSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_DEFAULT);
2001c42a7b7eSSam Leffler 		if (!sc->sc_invalid) {
20029a842e8bSAdrian Chadd 			ath_stoprecv(sc, 1);
2003c42a7b7eSSam Leffler 			ath_hal_phydisable(ah);
2004c42a7b7eSSam Leffler 		} else
20055591b213SSam Leffler 			sc->sc_rxlink = NULL;
2006b032f27cSSam Leffler 		ath_beacon_free(sc);	/* XXX not needed */
2007c42a7b7eSSam Leffler 	}
2008c42a7b7eSSam Leffler }
2009c42a7b7eSSam Leffler 
2010ef27340cSAdrian Chadd #define	MAX_TXRX_ITERATIONS	1000
2011ef27340cSAdrian Chadd static void
201221008bf1SAdrian Chadd ath_txrx_stop_locked(struct ath_softc *sc)
2013ef27340cSAdrian Chadd {
2014ef27340cSAdrian Chadd 	int i = MAX_TXRX_ITERATIONS;
2015ef27340cSAdrian Chadd 
2016ef27340cSAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
201721008bf1SAdrian Chadd 	ATH_PCU_LOCK_ASSERT(sc);
201821008bf1SAdrian Chadd 
2019ef27340cSAdrian Chadd 	/*
2020ef27340cSAdrian Chadd 	 * Sleep until all the pending operations have completed.
2021ef27340cSAdrian Chadd 	 *
2022ef27340cSAdrian Chadd 	 * The caller must ensure that reset has been incremented
2023ef27340cSAdrian Chadd 	 * or the pending operations may continue being queued.
2024ef27340cSAdrian Chadd 	 */
2025ef27340cSAdrian Chadd 	while (sc->sc_rxproc_cnt || sc->sc_txproc_cnt ||
2026ef27340cSAdrian Chadd 	    sc->sc_txstart_cnt || sc->sc_intr_cnt) {
2027ef27340cSAdrian Chadd 		if (i <= 0)
2028ef27340cSAdrian Chadd 			break;
2029a2d8240dSAdrian Chadd 		msleep(sc, &sc->sc_pcu_mtx, 0, "ath_txrx_stop", 1);
2030ef27340cSAdrian Chadd 		i--;
2031ef27340cSAdrian Chadd 	}
2032ef27340cSAdrian Chadd 
2033ef27340cSAdrian Chadd 	if (i <= 0)
2034ef27340cSAdrian Chadd 		device_printf(sc->sc_dev,
2035ef27340cSAdrian Chadd 		    "%s: didn't finish after %d iterations\n",
2036ef27340cSAdrian Chadd 		    __func__, MAX_TXRX_ITERATIONS);
2037ef27340cSAdrian Chadd }
2038ef27340cSAdrian Chadd #undef	MAX_TXRX_ITERATIONS
2039ef27340cSAdrian Chadd 
2040e78719adSAdrian Chadd #if 0
2041ef27340cSAdrian Chadd static void
204221008bf1SAdrian Chadd ath_txrx_stop(struct ath_softc *sc)
204321008bf1SAdrian Chadd {
204421008bf1SAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
204521008bf1SAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
204621008bf1SAdrian Chadd 
204721008bf1SAdrian Chadd 	ATH_PCU_LOCK(sc);
204821008bf1SAdrian Chadd 	ath_txrx_stop_locked(sc);
204921008bf1SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
205021008bf1SAdrian Chadd }
2051e78719adSAdrian Chadd #endif
205221008bf1SAdrian Chadd 
205321008bf1SAdrian Chadd static void
2054ef27340cSAdrian Chadd ath_txrx_start(struct ath_softc *sc)
2055ef27340cSAdrian Chadd {
2056ef27340cSAdrian Chadd 
2057ef27340cSAdrian Chadd 	taskqueue_unblock(sc->sc_tq);
2058ef27340cSAdrian Chadd }
2059ef27340cSAdrian Chadd 
2060ee321975SAdrian Chadd /*
2061ee321975SAdrian Chadd  * Grab the reset lock, and wait around until noone else
2062ee321975SAdrian Chadd  * is trying to do anything with it.
2063ee321975SAdrian Chadd  *
2064ee321975SAdrian Chadd  * This is totally horrible but we can't hold this lock for
2065ee321975SAdrian Chadd  * long enough to do TX/RX or we end up with net80211/ip stack
2066ee321975SAdrian Chadd  * LORs and eventual deadlock.
2067ee321975SAdrian Chadd  *
2068ee321975SAdrian Chadd  * "dowait" signals whether to spin, waiting for the reset
2069ee321975SAdrian Chadd  * lock count to reach 0. This should (for now) only be used
2070ee321975SAdrian Chadd  * during the reset path, as the rest of the code may not
2071ee321975SAdrian Chadd  * be locking-reentrant enough to behave correctly.
2072ee321975SAdrian Chadd  *
2073ee321975SAdrian Chadd  * Another, cleaner way should be found to serialise all of
2074ee321975SAdrian Chadd  * these operations.
2075ee321975SAdrian Chadd  */
2076ee321975SAdrian Chadd #define	MAX_RESET_ITERATIONS	10
2077ee321975SAdrian Chadd static int
2078ee321975SAdrian Chadd ath_reset_grablock(struct ath_softc *sc, int dowait)
2079ee321975SAdrian Chadd {
2080ee321975SAdrian Chadd 	int w = 0;
2081ee321975SAdrian Chadd 	int i = MAX_RESET_ITERATIONS;
2082ee321975SAdrian Chadd 
2083ee321975SAdrian Chadd 	ATH_PCU_LOCK_ASSERT(sc);
2084ee321975SAdrian Chadd 	do {
2085ee321975SAdrian Chadd 		if (sc->sc_inreset_cnt == 0) {
2086ee321975SAdrian Chadd 			w = 1;
2087ee321975SAdrian Chadd 			break;
2088ee321975SAdrian Chadd 		}
2089ee321975SAdrian Chadd 		if (dowait == 0) {
2090ee321975SAdrian Chadd 			w = 0;
2091ee321975SAdrian Chadd 			break;
2092ee321975SAdrian Chadd 		}
2093ee321975SAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2094ee321975SAdrian Chadd 		pause("ath_reset_grablock", 1);
2095ee321975SAdrian Chadd 		i--;
2096ee321975SAdrian Chadd 		ATH_PCU_LOCK(sc);
2097ee321975SAdrian Chadd 	} while (i > 0);
2098ee321975SAdrian Chadd 
2099ee321975SAdrian Chadd 	/*
2100ee321975SAdrian Chadd 	 * We always increment the refcounter, regardless
2101ee321975SAdrian Chadd 	 * of whether we succeeded to get it in an exclusive
2102ee321975SAdrian Chadd 	 * way.
2103ee321975SAdrian Chadd 	 */
2104ee321975SAdrian Chadd 	sc->sc_inreset_cnt++;
2105ee321975SAdrian Chadd 
2106ee321975SAdrian Chadd 	if (i <= 0)
2107ee321975SAdrian Chadd 		device_printf(sc->sc_dev,
2108ee321975SAdrian Chadd 		    "%s: didn't finish after %d iterations\n",
2109ee321975SAdrian Chadd 		    __func__, MAX_RESET_ITERATIONS);
2110ee321975SAdrian Chadd 
2111ee321975SAdrian Chadd 	if (w == 0)
2112ee321975SAdrian Chadd 		device_printf(sc->sc_dev,
2113ee321975SAdrian Chadd 		    "%s: warning, recursive reset path!\n",
2114ee321975SAdrian Chadd 		    __func__);
2115ee321975SAdrian Chadd 
2116ee321975SAdrian Chadd 	return w;
2117ee321975SAdrian Chadd }
2118ee321975SAdrian Chadd #undef MAX_RESET_ITERATIONS
2119ee321975SAdrian Chadd 
2120ee321975SAdrian Chadd /*
2121ee321975SAdrian Chadd  * XXX TODO: write ath_reset_releaselock
2122ee321975SAdrian Chadd  */
2123ee321975SAdrian Chadd 
2124c42a7b7eSSam Leffler static void
2125c42a7b7eSSam Leffler ath_stop(struct ifnet *ifp)
2126c42a7b7eSSam Leffler {
2127c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2128c42a7b7eSSam Leffler 
2129c42a7b7eSSam Leffler 	ATH_LOCK(sc);
2130c42a7b7eSSam Leffler 	ath_stop_locked(ifp);
2131f0b2a0beSSam Leffler 	ATH_UNLOCK(sc);
21325591b213SSam Leffler }
21335591b213SSam Leffler 
21345591b213SSam Leffler /*
21355591b213SSam Leffler  * Reset the hardware w/o losing operational state.  This is
21365591b213SSam Leffler  * basically a more efficient way of doing ath_stop, ath_init,
21375591b213SSam Leffler  * followed by state transitions to the current 802.11
2138c42a7b7eSSam Leffler  * operational state.  Used to recover from various errors and
2139c42a7b7eSSam Leffler  * to reset or reload hardware state.
21405591b213SSam Leffler  */
21416079fdbeSAdrian Chadd int
2142517526efSAdrian Chadd ath_reset(struct ifnet *ifp, ATH_RESET_TYPE reset_type)
21435591b213SSam Leffler {
2144c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2145b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
21465591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
21475591b213SSam Leffler 	HAL_STATUS status;
2148ef27340cSAdrian Chadd 	int i;
21495591b213SSam Leffler 
2150f52d3452SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: called\n", __func__);
215116d4de92SAdrian Chadd 
2152ee321975SAdrian Chadd 	/* Ensure ATH_LOCK isn't held; ath_rx_proc can't be locked */
2153ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
2154ef27340cSAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
2155ef27340cSAdrian Chadd 
2156d52f7132SAdrian Chadd 	/* Try to (stop any further TX/RX from occuring */
2157d52f7132SAdrian Chadd 	taskqueue_block(sc->sc_tq);
2158d52f7132SAdrian Chadd 
2159ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2160e78719adSAdrian Chadd 	ath_hal_intrset(ah, 0);		/* disable interrupts */
2161e78719adSAdrian Chadd 	ath_txrx_stop_locked(sc);	/* Ensure TX/RX is stopped */
2162ee321975SAdrian Chadd 	if (ath_reset_grablock(sc, 1) == 0) {
2163ee321975SAdrian Chadd 		device_printf(sc->sc_dev, "%s: concurrent reset! Danger!\n",
2164ef27340cSAdrian Chadd 		    __func__);
2165ef27340cSAdrian Chadd 	}
2166ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2167ef27340cSAdrian Chadd 
2168f52d3452SAdrian Chadd 	/*
21699a842e8bSAdrian Chadd 	 * Should now wait for pending TX/RX to complete
21709a842e8bSAdrian Chadd 	 * and block future ones from occuring. This needs to be
21719a842e8bSAdrian Chadd 	 * done before the TX queue is drained.
2172f52d3452SAdrian Chadd 	 */
2173ef27340cSAdrian Chadd 	ath_draintxq(sc, reset_type);	/* stop xmit side */
2174ef27340cSAdrian Chadd 
2175ef27340cSAdrian Chadd 	/*
2176ef27340cSAdrian Chadd 	 * Regardless of whether we're doing a no-loss flush or
2177ef27340cSAdrian Chadd 	 * not, stop the PCU and handle what's in the RX queue.
2178ef27340cSAdrian Chadd 	 * That way frames aren't dropped which shouldn't be.
2179ef27340cSAdrian Chadd 	 */
21809a842e8bSAdrian Chadd 	ath_stoprecv(sc, (reset_type != ATH_RESET_NOLOSS));
2181f8cc9b09SAdrian Chadd 	ath_rx_flush(sc);
2182ef27340cSAdrian Chadd 
2183b032f27cSSam Leffler 	ath_settkipmic(sc);		/* configure TKIP MIC handling */
21845591b213SSam Leffler 	/* NB: indicate channel change so we do a full reset */
218559efa8b5SSam Leffler 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_TRUE, &status))
21865591b213SSam Leffler 		if_printf(ifp, "%s: unable to reset hardware; hal status %u\n",
21875591b213SSam Leffler 			__func__, status);
2188c59005e9SSam Leffler 	sc->sc_diversity = ath_hal_getdiversity(ah);
218948237774SAdrian Chadd 
219048237774SAdrian Chadd 	/* Let DFS at it in case it's a DFS channel */
219148237774SAdrian Chadd 	ath_dfs_radar_enable(sc, ic->ic_curchan);
219248237774SAdrian Chadd 
219368e8e04eSSam Leffler 	if (ath_startrecv(sc) != 0)	/* restart recv */
219468e8e04eSSam Leffler 		if_printf(ifp, "%s: unable to start recv logic\n", __func__);
2195c42a7b7eSSam Leffler 	/*
2196c42a7b7eSSam Leffler 	 * We may be doing a reset in response to an ioctl
2197c42a7b7eSSam Leffler 	 * that changes the channel so update any state that
2198c42a7b7eSSam Leffler 	 * might change as a result.
2199c42a7b7eSSam Leffler 	 */
2200724c193aSSam Leffler 	ath_chan_change(sc, ic->ic_curchan);
2201c89b957aSSam Leffler 	if (sc->sc_beacons) {		/* restart beacons */
2202584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
220310ad9a77SSam Leffler 		if (sc->sc_tdma)
220410ad9a77SSam Leffler 			ath_tdma_config(sc, NULL);
220510ad9a77SSam Leffler 		else
220610ad9a77SSam Leffler #endif
2207c89b957aSSam Leffler 			ath_beacon_config(sc, NULL);
220810ad9a77SSam Leffler 	}
2209c42a7b7eSSam Leffler 
2210ef27340cSAdrian Chadd 	/*
2211ef27340cSAdrian Chadd 	 * Release the reset lock and re-enable interrupts here.
2212ef27340cSAdrian Chadd 	 * If an interrupt was being processed in ath_intr(),
2213ef27340cSAdrian Chadd 	 * it would disable interrupts at this point. So we have
2214ef27340cSAdrian Chadd 	 * to atomically enable interrupts and decrement the
2215ef27340cSAdrian Chadd 	 * reset counter - this way ath_intr() doesn't end up
2216ef27340cSAdrian Chadd 	 * disabling interrupts without a corresponding enable
2217ef27340cSAdrian Chadd 	 * in the rest or channel change path.
2218ef27340cSAdrian Chadd 	 */
2219ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2220ef27340cSAdrian Chadd 	sc->sc_inreset_cnt--;
2221ef27340cSAdrian Chadd 	/* XXX only do this if sc_inreset_cnt == 0? */
2222ef27340cSAdrian Chadd 	ath_hal_intrset(ah, sc->sc_imask);
2223ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2224ef27340cSAdrian Chadd 
2225ef27340cSAdrian Chadd 	/*
2226ef27340cSAdrian Chadd 	 * TX and RX can be started here. If it were started with
2227ef27340cSAdrian Chadd 	 * sc_inreset_cnt > 0, the TX and RX path would abort.
2228ef27340cSAdrian Chadd 	 * Thus if this is a nested call through the reset or
2229ef27340cSAdrian Chadd 	 * channel change code, TX completion will occur but
2230ef27340cSAdrian Chadd 	 * RX completion and ath_start / ath_tx_start will not
2231ef27340cSAdrian Chadd 	 * run.
2232ef27340cSAdrian Chadd 	 */
2233ef27340cSAdrian Chadd 
2234ef27340cSAdrian Chadd 	/* Restart TX/RX as needed */
2235ef27340cSAdrian Chadd 	ath_txrx_start(sc);
2236ef27340cSAdrian Chadd 
2237ef27340cSAdrian Chadd 	/* XXX Restart TX completion and pending TX */
2238ef27340cSAdrian Chadd 	if (reset_type == ATH_RESET_NOLOSS) {
2239ef27340cSAdrian Chadd 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
2240ef27340cSAdrian Chadd 			if (ATH_TXQ_SETUP(sc, i)) {
2241ef27340cSAdrian Chadd 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
2242ef27340cSAdrian Chadd 				ath_txq_restart_dma(sc, &sc->sc_txq[i]);
2243ef27340cSAdrian Chadd 				ath_txq_sched(sc, &sc->sc_txq[i]);
2244ef27340cSAdrian Chadd 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
2245ef27340cSAdrian Chadd 			}
2246ef27340cSAdrian Chadd 		}
2247ef27340cSAdrian Chadd 	}
2248ef27340cSAdrian Chadd 
2249ef27340cSAdrian Chadd 	/*
2250ef27340cSAdrian Chadd 	 * This may have been set during an ath_start() call which
2251ef27340cSAdrian Chadd 	 * set this once it detected a concurrent TX was going on.
2252ef27340cSAdrian Chadd 	 * So, clear it.
2253ef27340cSAdrian Chadd 	 */
2254e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
2255ef27340cSAdrian Chadd 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2256e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
2257ef27340cSAdrian Chadd 
2258ef27340cSAdrian Chadd 	/* Handle any frames in the TX queue */
2259ef27340cSAdrian Chadd 	/*
2260ef27340cSAdrian Chadd 	 * XXX should this be done by the caller, rather than
2261ef27340cSAdrian Chadd 	 * ath_reset() ?
2262ef27340cSAdrian Chadd 	 */
22638e739394SAdrian Chadd 	ath_tx_kick(sc);		/* restart xmit */
2264c42a7b7eSSam Leffler 	return 0;
22655591b213SSam Leffler }
22665591b213SSam Leffler 
226768e8e04eSSam Leffler static int
2268b032f27cSSam Leffler ath_reset_vap(struct ieee80211vap *vap, u_long cmd)
2269b032f27cSSam Leffler {
22704b54a231SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
22714b54a231SSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
22724b54a231SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
22734b54a231SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
22744b54a231SSam Leffler 
22754b54a231SSam Leffler 	switch (cmd) {
22764b54a231SSam Leffler 	case IEEE80211_IOC_TXPOWER:
22774b54a231SSam Leffler 		/*
22784b54a231SSam Leffler 		 * If per-packet TPC is enabled, then we have nothing
22794b54a231SSam Leffler 		 * to do; otherwise we need to force the global limit.
22804b54a231SSam Leffler 		 * All this can happen directly; no need to reset.
22814b54a231SSam Leffler 		 */
22824b54a231SSam Leffler 		if (!ath_hal_gettpc(ah))
22834b54a231SSam Leffler 			ath_hal_settxpowlimit(ah, ic->ic_txpowlimit);
22844b54a231SSam Leffler 		return 0;
22854b54a231SSam Leffler 	}
2286517526efSAdrian Chadd 	/* XXX? Full or NOLOSS? */
2287517526efSAdrian Chadd 	return ath_reset(ifp, ATH_RESET_FULL);
2288b032f27cSSam Leffler }
2289b032f27cSSam Leffler 
2290b8e788a5SAdrian Chadd struct ath_buf *
2291af33d486SAdrian Chadd _ath_getbuf_locked(struct ath_softc *sc, ath_buf_type_t btype)
229210ad9a77SSam Leffler {
229310ad9a77SSam Leffler 	struct ath_buf *bf;
229410ad9a77SSam Leffler 
229510ad9a77SSam Leffler 	ATH_TXBUF_LOCK_ASSERT(sc);
229610ad9a77SSam Leffler 
2297af33d486SAdrian Chadd 	if (btype == ATH_BUFTYPE_MGMT)
2298af33d486SAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_txbuf_mgmt);
2299af33d486SAdrian Chadd 	else
23006b349e5aSAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_txbuf);
2301af33d486SAdrian Chadd 
2302e346b073SAdrian Chadd 	if (bf == NULL) {
2303e346b073SAdrian Chadd 		sc->sc_stats.ast_tx_getnobuf++;
2304e346b073SAdrian Chadd 	} else {
2305e346b073SAdrian Chadd 		if (bf->bf_flags & ATH_BUF_BUSY) {
2306e346b073SAdrian Chadd 			sc->sc_stats.ast_tx_getbusybuf++;
2307e346b073SAdrian Chadd 			bf = NULL;
2308e346b073SAdrian Chadd 		}
2309e346b073SAdrian Chadd 	}
2310e346b073SAdrian Chadd 
2311af33d486SAdrian Chadd 	if (bf != NULL && (bf->bf_flags & ATH_BUF_BUSY) == 0) {
2312af33d486SAdrian Chadd 		if (btype == ATH_BUFTYPE_MGMT)
2313af33d486SAdrian Chadd 			TAILQ_REMOVE(&sc->sc_txbuf_mgmt, bf, bf_list);
231423ced6c1SAdrian Chadd 		else {
2315af33d486SAdrian Chadd 			TAILQ_REMOVE(&sc->sc_txbuf, bf, bf_list);
231623ced6c1SAdrian Chadd 			sc->sc_txbuf_cnt--;
231723ced6c1SAdrian Chadd 
231823ced6c1SAdrian Chadd 			/*
231923ced6c1SAdrian Chadd 			 * This shuldn't happen; however just to be
232023ced6c1SAdrian Chadd 			 * safe print a warning and fudge the txbuf
232123ced6c1SAdrian Chadd 			 * count.
232223ced6c1SAdrian Chadd 			 */
232323ced6c1SAdrian Chadd 			if (sc->sc_txbuf_cnt < 0) {
232423ced6c1SAdrian Chadd 				device_printf(sc->sc_dev,
232523ced6c1SAdrian Chadd 				    "%s: sc_txbuf_cnt < 0?\n",
232623ced6c1SAdrian Chadd 				    __func__);
232723ced6c1SAdrian Chadd 				sc->sc_txbuf_cnt = 0;
232823ced6c1SAdrian Chadd 			}
232923ced6c1SAdrian Chadd 		}
2330af33d486SAdrian Chadd 	} else
233110ad9a77SSam Leffler 		bf = NULL;
2332e346b073SAdrian Chadd 
233310ad9a77SSam Leffler 	if (bf == NULL) {
2334af33d486SAdrian Chadd 		/* XXX should check which list, mgmt or otherwise */
233510ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: %s\n", __func__,
23366b349e5aSAdrian Chadd 		    TAILQ_FIRST(&sc->sc_txbuf) == NULL ?
233710ad9a77SSam Leffler 			"out of xmit buffers" : "xmit buffer busy");
2338e346b073SAdrian Chadd 		return NULL;
233910ad9a77SSam Leffler 	}
2340e346b073SAdrian Chadd 
2341af33d486SAdrian Chadd 	/* XXX TODO: should do this at buffer list initialisation */
2342af33d486SAdrian Chadd 	/* XXX (then, ensure the buffer has the right flag set) */
2343af33d486SAdrian Chadd 	if (btype == ATH_BUFTYPE_MGMT)
2344af33d486SAdrian Chadd 		bf->bf_flags |= ATH_BUF_MGMT;
2345af33d486SAdrian Chadd 	else
2346af33d486SAdrian Chadd 		bf->bf_flags &= (~ATH_BUF_MGMT);
2347af33d486SAdrian Chadd 
2348e346b073SAdrian Chadd 	/* Valid bf here; clear some basic fields */
2349e346b073SAdrian Chadd 	bf->bf_next = NULL;	/* XXX just to be sure */
2350e346b073SAdrian Chadd 	bf->bf_last = NULL;	/* XXX again, just to be sure */
2351e346b073SAdrian Chadd 	bf->bf_comp = NULL;	/* XXX again, just to be sure */
2352e346b073SAdrian Chadd 	bzero(&bf->bf_state, sizeof(bf->bf_state));
2353e346b073SAdrian Chadd 
235485bf9bc3SAdrian Chadd 	/*
235585bf9bc3SAdrian Chadd 	 * Track the descriptor ID only if doing EDMA
235685bf9bc3SAdrian Chadd 	 */
235785bf9bc3SAdrian Chadd 	if (sc->sc_isedma) {
235885bf9bc3SAdrian Chadd 		bf->bf_descid = sc->sc_txbuf_descid;
235985bf9bc3SAdrian Chadd 		sc->sc_txbuf_descid++;
236085bf9bc3SAdrian Chadd 	}
236185bf9bc3SAdrian Chadd 
236210ad9a77SSam Leffler 	return bf;
236310ad9a77SSam Leffler }
236410ad9a77SSam Leffler 
2365e346b073SAdrian Chadd /*
2366e346b073SAdrian Chadd  * When retrying a software frame, buffers marked ATH_BUF_BUSY
2367e346b073SAdrian Chadd  * can't be thrown back on the queue as they could still be
2368e346b073SAdrian Chadd  * in use by the hardware.
2369e346b073SAdrian Chadd  *
2370e346b073SAdrian Chadd  * This duplicates the buffer, or returns NULL.
2371e346b073SAdrian Chadd  *
2372e346b073SAdrian Chadd  * The descriptor is also copied but the link pointers and
2373e346b073SAdrian Chadd  * the DMA segments aren't copied; this frame should thus
2374e346b073SAdrian Chadd  * be again passed through the descriptor setup/chain routines
2375e346b073SAdrian Chadd  * so the link is correct.
2376e346b073SAdrian Chadd  *
2377e346b073SAdrian Chadd  * The caller must free the buffer using ath_freebuf().
2378e346b073SAdrian Chadd  *
2379e346b073SAdrian Chadd  * XXX TODO: this call shouldn't fail as it'll cause packet loss
2380e346b073SAdrian Chadd  * XXX in the TX pathway when retries are needed.
2381e346b073SAdrian Chadd  * XXX Figure out how to keep some buffers free, or factor the
2382e346b073SAdrian Chadd  * XXX number of busy buffers into the xmit path (ath_start())
2383e346b073SAdrian Chadd  * XXX so we don't over-commit.
2384e346b073SAdrian Chadd  */
2385e346b073SAdrian Chadd struct ath_buf *
2386e346b073SAdrian Chadd ath_buf_clone(struct ath_softc *sc, const struct ath_buf *bf)
2387e346b073SAdrian Chadd {
2388e346b073SAdrian Chadd 	struct ath_buf *tbf;
2389e346b073SAdrian Chadd 
2390af33d486SAdrian Chadd 	tbf = ath_getbuf(sc,
2391af33d486SAdrian Chadd 	    (bf->bf_flags & ATH_BUF_MGMT) ?
2392af33d486SAdrian Chadd 	     ATH_BUFTYPE_MGMT : ATH_BUFTYPE_NORMAL);
2393e346b073SAdrian Chadd 	if (tbf == NULL)
2394e346b073SAdrian Chadd 		return NULL;	/* XXX failure? Why? */
2395e346b073SAdrian Chadd 
2396e346b073SAdrian Chadd 	/* Copy basics */
2397e346b073SAdrian Chadd 	tbf->bf_next = NULL;
2398e346b073SAdrian Chadd 	tbf->bf_nseg = bf->bf_nseg;
2399e346b073SAdrian Chadd 	tbf->bf_flags = bf->bf_flags & ~ATH_BUF_BUSY;
2400e346b073SAdrian Chadd 	tbf->bf_status = bf->bf_status;
2401e346b073SAdrian Chadd 	tbf->bf_m = bf->bf_m;
2402e346b073SAdrian Chadd 	tbf->bf_node = bf->bf_node;
2403e346b073SAdrian Chadd 	/* will be setup by the chain/setup function */
2404e346b073SAdrian Chadd 	tbf->bf_lastds = NULL;
2405e346b073SAdrian Chadd 	/* for now, last == self */
2406e346b073SAdrian Chadd 	tbf->bf_last = tbf;
2407e346b073SAdrian Chadd 	tbf->bf_comp = bf->bf_comp;
2408e346b073SAdrian Chadd 
2409e346b073SAdrian Chadd 	/* NOTE: DMA segments will be setup by the setup/chain functions */
2410e346b073SAdrian Chadd 
2411e346b073SAdrian Chadd 	/* The caller has to re-init the descriptor + links */
2412e346b073SAdrian Chadd 
2413e346b073SAdrian Chadd 	/* Copy state */
2414e346b073SAdrian Chadd 	memcpy(&tbf->bf_state, &bf->bf_state, sizeof(bf->bf_state));
2415e346b073SAdrian Chadd 
2416e346b073SAdrian Chadd 	return tbf;
2417e346b073SAdrian Chadd }
2418e346b073SAdrian Chadd 
2419b8e788a5SAdrian Chadd struct ath_buf *
2420af33d486SAdrian Chadd ath_getbuf(struct ath_softc *sc, ath_buf_type_t btype)
242110ad9a77SSam Leffler {
242210ad9a77SSam Leffler 	struct ath_buf *bf;
242310ad9a77SSam Leffler 
242410ad9a77SSam Leffler 	ATH_TXBUF_LOCK(sc);
2425af33d486SAdrian Chadd 	bf = _ath_getbuf_locked(sc, btype);
2426af33d486SAdrian Chadd 	/*
2427af33d486SAdrian Chadd 	 * If a mgmt buffer was requested but we're out of those,
2428af33d486SAdrian Chadd 	 * try requesting a normal one.
2429af33d486SAdrian Chadd 	 */
2430af33d486SAdrian Chadd 	if (bf == NULL && btype == ATH_BUFTYPE_MGMT)
2431af33d486SAdrian Chadd 		bf = _ath_getbuf_locked(sc, ATH_BUFTYPE_NORMAL);
2432e4e7938aSAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
243310ad9a77SSam Leffler 	if (bf == NULL) {
243410ad9a77SSam Leffler 		struct ifnet *ifp = sc->sc_ifp;
243510ad9a77SSam Leffler 
243610ad9a77SSam Leffler 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: stop queue\n", __func__);
243710ad9a77SSam Leffler 		sc->sc_stats.ast_tx_qstop++;
2438e4e7938aSAdrian Chadd 		IF_LOCK(&ifp->if_snd);
243910ad9a77SSam Leffler 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2440e4e7938aSAdrian Chadd 		IF_UNLOCK(&ifp->if_snd);
244110ad9a77SSam Leffler 	}
244210ad9a77SSam Leffler 	return bf;
244310ad9a77SSam Leffler }
244410ad9a77SSam Leffler 
24458e739394SAdrian Chadd static void
24468e739394SAdrian Chadd ath_start_queue(struct ifnet *ifp)
24475591b213SSam Leffler {
24485591b213SSam Leffler 	struct ath_softc *sc = ifp->if_softc;
24495591b213SSam Leffler 
24508e739394SAdrian Chadd 	ath_tx_kick(sc);
24518e739394SAdrian Chadd }
24528e739394SAdrian Chadd 
24538e739394SAdrian Chadd void
24548e739394SAdrian Chadd ath_start_task(void *arg, int npending)
24558e739394SAdrian Chadd {
24568e739394SAdrian Chadd 	struct ath_softc *sc = (struct ath_softc *) arg;
24578e739394SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
2458ef27340cSAdrian Chadd 
2459ef27340cSAdrian Chadd 	/* XXX is it ok to hold the ATH_LOCK here? */
2460ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
2461ef27340cSAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
2462ef27340cSAdrian Chadd 		device_printf(sc->sc_dev,
2463ef27340cSAdrian Chadd 		    "%s: sc_inreset_cnt > 0; bailing\n", __func__);
2464ef27340cSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
2465e4e7938aSAdrian Chadd 		IF_LOCK(&ifp->if_snd);
246623ced6c1SAdrian Chadd 		sc->sc_stats.ast_tx_qstop++;
2467e4e7938aSAdrian Chadd 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2468e4e7938aSAdrian Chadd 		IF_UNLOCK(&ifp->if_snd);
2469ef27340cSAdrian Chadd 		return;
2470ef27340cSAdrian Chadd 	}
2471ef27340cSAdrian Chadd 	sc->sc_txstart_cnt++;
2472ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
2473ef27340cSAdrian Chadd 
24748e739394SAdrian Chadd 	ath_start(sc->sc_ifp);
24758e739394SAdrian Chadd 
24768e739394SAdrian Chadd 	ATH_PCU_LOCK(sc);
24778e739394SAdrian Chadd 	sc->sc_txstart_cnt--;
24788e739394SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
24798e739394SAdrian Chadd }
24808e739394SAdrian Chadd 
24818e739394SAdrian Chadd void
24828e739394SAdrian Chadd ath_start(struct ifnet *ifp)
24838e739394SAdrian Chadd {
24848e739394SAdrian Chadd 	struct ath_softc *sc = ifp->if_softc;
24858e739394SAdrian Chadd 	struct ieee80211_node *ni;
24868e739394SAdrian Chadd 	struct ath_buf *bf;
24878e739394SAdrian Chadd 	struct mbuf *m, *next;
24888e739394SAdrian Chadd 	ath_bufhead frags;
24898e739394SAdrian Chadd 
24908e739394SAdrian Chadd 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->sc_invalid)
24918e739394SAdrian Chadd 		return;
24928e739394SAdrian Chadd 
24935591b213SSam Leffler 	for (;;) {
249423ced6c1SAdrian Chadd 		ATH_TXBUF_LOCK(sc);
249523ced6c1SAdrian Chadd 		if (sc->sc_txbuf_cnt <= sc->sc_txq_data_minfree) {
249623ced6c1SAdrian Chadd 			/* XXX increment counter? */
249723ced6c1SAdrian Chadd 			ATH_TXBUF_UNLOCK(sc);
249823ced6c1SAdrian Chadd 			IF_LOCK(&ifp->if_snd);
249923ced6c1SAdrian Chadd 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
250023ced6c1SAdrian Chadd 			IF_UNLOCK(&ifp->if_snd);
250123ced6c1SAdrian Chadd 			break;
250223ced6c1SAdrian Chadd 		}
250323ced6c1SAdrian Chadd 		ATH_TXBUF_UNLOCK(sc);
250423ced6c1SAdrian Chadd 
25055591b213SSam Leffler 		/*
25065591b213SSam Leffler 		 * Grab a TX buffer and associated resources.
25075591b213SSam Leffler 		 */
2508af33d486SAdrian Chadd 		bf = ath_getbuf(sc, ATH_BUFTYPE_NORMAL);
250910ad9a77SSam Leffler 		if (bf == NULL)
25105591b213SSam Leffler 			break;
25112b9411e2SSam Leffler 
2512b032f27cSSam Leffler 		IFQ_DEQUEUE(&ifp->if_snd, m);
2513b032f27cSSam Leffler 		if (m == NULL) {
2514b032f27cSSam Leffler 			ATH_TXBUF_LOCK(sc);
2515e1a50456SAdrian Chadd 			ath_returnbuf_head(sc, bf);
2516b032f27cSSam Leffler 			ATH_TXBUF_UNLOCK(sc);
2517b032f27cSSam Leffler 			break;
2518b032f27cSSam Leffler 		}
2519b032f27cSSam Leffler 		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
252068e8e04eSSam Leffler 		/*
252168e8e04eSSam Leffler 		 * Check for fragmentation.  If this frame
252268e8e04eSSam Leffler 		 * has been broken up verify we have enough
252368e8e04eSSam Leffler 		 * buffers to send all the fragments so all
252468e8e04eSSam Leffler 		 * go out or none...
252568e8e04eSSam Leffler 		 */
25266b349e5aSAdrian Chadd 		TAILQ_INIT(&frags);
252768e8e04eSSam Leffler 		if ((m->m_flags & M_FRAG) &&
252868e8e04eSSam Leffler 		    !ath_txfrag_setup(sc, &frags, m, ni)) {
252968e8e04eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_XMIT,
253068e8e04eSSam Leffler 			    "%s: out of txfrag buffers\n", __func__);
253136c6be9aSSam Leffler 			sc->sc_stats.ast_tx_nofrag++;
25329cb93076SSam Leffler 			ifp->if_oerrors++;
253368e8e04eSSam Leffler 			ath_freetx(m);
253468e8e04eSSam Leffler 			goto bad;
253568e8e04eSSam Leffler 		}
2536339ccfb3SSam Leffler 		ifp->if_opackets++;
253768e8e04eSSam Leffler 	nextfrag:
253868e8e04eSSam Leffler 		/*
253968e8e04eSSam Leffler 		 * Pass the frame to the h/w for transmission.
254068e8e04eSSam Leffler 		 * Fragmented frames have each frag chained together
254168e8e04eSSam Leffler 		 * with m_nextpkt.  We know there are sufficient ath_buf's
254268e8e04eSSam Leffler 		 * to send all the frags because of work done by
254368e8e04eSSam Leffler 		 * ath_txfrag_setup.  We leave m_nextpkt set while
254468e8e04eSSam Leffler 		 * calling ath_tx_start so it can use it to extend the
254568e8e04eSSam Leffler 		 * the tx duration to cover the subsequent frag and
254668e8e04eSSam Leffler 		 * so it can reclaim all the mbufs in case of an error;
254768e8e04eSSam Leffler 		 * ath_tx_start clears m_nextpkt once it commits to
254868e8e04eSSam Leffler 		 * handing the frame to the hardware.
254968e8e04eSSam Leffler 		 */
255068e8e04eSSam Leffler 		next = m->m_nextpkt;
25515591b213SSam Leffler 		if (ath_tx_start(sc, ni, bf, m)) {
25525591b213SSam Leffler 	bad:
25535591b213SSam Leffler 			ifp->if_oerrors++;
2554c42a7b7eSSam Leffler 	reclaim:
255568e8e04eSSam Leffler 			bf->bf_m = NULL;
255668e8e04eSSam Leffler 			bf->bf_node = NULL;
2557c42a7b7eSSam Leffler 			ATH_TXBUF_LOCK(sc);
2558e1a50456SAdrian Chadd 			ath_returnbuf_head(sc, bf);
255968e8e04eSSam Leffler 			ath_txfrag_cleanup(sc, &frags, ni);
2560c42a7b7eSSam Leffler 			ATH_TXBUF_UNLOCK(sc);
2561c42a7b7eSSam Leffler 			if (ni != NULL)
2562c42a7b7eSSam Leffler 				ieee80211_free_node(ni);
25635591b213SSam Leffler 			continue;
25645591b213SSam Leffler 		}
2565*548a605dSAdrian Chadd 
2566*548a605dSAdrian Chadd 		/*
2567*548a605dSAdrian Chadd 		 * Check here if the node is in power save state.
2568*548a605dSAdrian Chadd 		 */
2569*548a605dSAdrian Chadd 		ath_tx_update_tim(sc, ni, 1);
2570*548a605dSAdrian Chadd 
257168e8e04eSSam Leffler 		if (next != NULL) {
257268e8e04eSSam Leffler 			/*
257368e8e04eSSam Leffler 			 * Beware of state changing between frags.
257468e8e04eSSam Leffler 			 * XXX check sta power-save state?
257568e8e04eSSam Leffler 			 */
2576b032f27cSSam Leffler 			if (ni->ni_vap->iv_state != IEEE80211_S_RUN) {
257768e8e04eSSam Leffler 				DPRINTF(sc, ATH_DEBUG_XMIT,
257868e8e04eSSam Leffler 				    "%s: flush fragmented packet, state %s\n",
257968e8e04eSSam Leffler 				    __func__,
2580b032f27cSSam Leffler 				    ieee80211_state_name[ni->ni_vap->iv_state]);
258168e8e04eSSam Leffler 				ath_freetx(next);
258268e8e04eSSam Leffler 				goto reclaim;
258368e8e04eSSam Leffler 			}
258468e8e04eSSam Leffler 			m = next;
25856b349e5aSAdrian Chadd 			bf = TAILQ_FIRST(&frags);
258668e8e04eSSam Leffler 			KASSERT(bf != NULL, ("no buf for txfrag"));
25876b349e5aSAdrian Chadd 			TAILQ_REMOVE(&frags, bf, bf_list);
258868e8e04eSSam Leffler 			goto nextfrag;
258968e8e04eSSam Leffler 		}
25905591b213SSam Leffler 
25912e986da5SSam Leffler 		sc->sc_wd_timer = 5;
25925591b213SSam Leffler 	}
25935591b213SSam Leffler }
25945591b213SSam Leffler 
25955591b213SSam Leffler static int
25965591b213SSam Leffler ath_media_change(struct ifnet *ifp)
25975591b213SSam Leffler {
2598b032f27cSSam Leffler 	int error = ieee80211_media_change(ifp);
2599b032f27cSSam Leffler 	/* NB: only the fixed rate can change and that doesn't need a reset */
2600b032f27cSSam Leffler 	return (error == ENETRESET ? 0 : error);
26015591b213SSam Leffler }
26025591b213SSam Leffler 
2603c42a7b7eSSam Leffler /*
2604c42a7b7eSSam Leffler  * Block/unblock tx+rx processing while a key change is done.
2605c42a7b7eSSam Leffler  * We assume the caller serializes key management operations
2606c42a7b7eSSam Leffler  * so we only need to worry about synchronization with other
2607c42a7b7eSSam Leffler  * uses that originate in the driver.
2608c42a7b7eSSam Leffler  */
2609c42a7b7eSSam Leffler static void
2610b032f27cSSam Leffler ath_key_update_begin(struct ieee80211vap *vap)
2611c42a7b7eSSam Leffler {
2612b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ic->ic_ifp;
2613c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2614c42a7b7eSSam Leffler 
2615c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
2616b032f27cSSam Leffler 	taskqueue_block(sc->sc_tq);
2617c42a7b7eSSam Leffler 	IF_LOCK(&ifp->if_snd);		/* NB: doesn't block mgmt frames */
2618c42a7b7eSSam Leffler }
2619c42a7b7eSSam Leffler 
2620c42a7b7eSSam Leffler static void
2621b032f27cSSam Leffler ath_key_update_end(struct ieee80211vap *vap)
2622c42a7b7eSSam Leffler {
2623b032f27cSSam Leffler 	struct ifnet *ifp = vap->iv_ic->ic_ifp;
2624c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2625c42a7b7eSSam Leffler 
2626c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
2627c42a7b7eSSam Leffler 	IF_UNLOCK(&ifp->if_snd);
2628b032f27cSSam Leffler 	taskqueue_unblock(sc->sc_tq);
2629c42a7b7eSSam Leffler }
26305591b213SSam Leffler 
2631b032f27cSSam Leffler static void
2632b032f27cSSam Leffler ath_update_promisc(struct ifnet *ifp)
2633b032f27cSSam Leffler {
2634b032f27cSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2635b032f27cSSam Leffler 	u_int32_t rfilt;
2636b032f27cSSam Leffler 
2637b032f27cSSam Leffler 	/* configure rx filter */
2638b032f27cSSam Leffler 	rfilt = ath_calcrxfilter(sc);
2639b032f27cSSam Leffler 	ath_hal_setrxfilter(sc->sc_ah, rfilt);
2640b032f27cSSam Leffler 
2641b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x\n", __func__, rfilt);
2642b032f27cSSam Leffler }
2643b032f27cSSam Leffler 
2644b032f27cSSam Leffler static void
2645b032f27cSSam Leffler ath_update_mcast(struct ifnet *ifp)
2646b032f27cSSam Leffler {
2647b032f27cSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2648b032f27cSSam Leffler 	u_int32_t mfilt[2];
2649b032f27cSSam Leffler 
2650b032f27cSSam Leffler 	/* calculate and install multicast filter */
2651b032f27cSSam Leffler 	if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
2652b032f27cSSam Leffler 		struct ifmultiaddr *ifma;
2653b032f27cSSam Leffler 		/*
2654b032f27cSSam Leffler 		 * Merge multicast addresses to form the hardware filter.
2655b032f27cSSam Leffler 		 */
2656b032f27cSSam Leffler 		mfilt[0] = mfilt[1] = 0;
2657eb956cd0SRobert Watson 		if_maddr_rlock(ifp);	/* XXX need some fiddling to remove? */
2658b032f27cSSam Leffler 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2659b032f27cSSam Leffler 			caddr_t dl;
2660b032f27cSSam Leffler 			u_int32_t val;
2661b032f27cSSam Leffler 			u_int8_t pos;
2662b032f27cSSam Leffler 
2663b032f27cSSam Leffler 			/* calculate XOR of eight 6bit values */
2664b032f27cSSam Leffler 			dl = LLADDR((struct sockaddr_dl *) ifma->ifma_addr);
2665b032f27cSSam Leffler 			val = LE_READ_4(dl + 0);
2666b032f27cSSam Leffler 			pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
2667b032f27cSSam Leffler 			val = LE_READ_4(dl + 3);
2668b032f27cSSam Leffler 			pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
2669b032f27cSSam Leffler 			pos &= 0x3f;
2670b032f27cSSam Leffler 			mfilt[pos / 32] |= (1 << (pos % 32));
2671b032f27cSSam Leffler 		}
2672eb956cd0SRobert Watson 		if_maddr_runlock(ifp);
2673b032f27cSSam Leffler 	} else
2674b032f27cSSam Leffler 		mfilt[0] = mfilt[1] = ~0;
2675b032f27cSSam Leffler 	ath_hal_setmcastfilter(sc->sc_ah, mfilt[0], mfilt[1]);
2676b032f27cSSam Leffler 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: MC filter %08x:%08x\n",
2677b032f27cSSam Leffler 		__func__, mfilt[0], mfilt[1]);
26784bc0e754SSam Leffler }
26794bc0e754SSam Leffler 
2680e60c4fc2SAdrian Chadd void
26815591b213SSam Leffler ath_mode_init(struct ath_softc *sc)
26825591b213SSam Leffler {
2683fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
2684b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2685b032f27cSSam Leffler 	u_int32_t rfilt;
26865591b213SSam Leffler 
26874bc0e754SSam Leffler 	/* configure rx filter */
268868e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
26894bc0e754SSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
26904bc0e754SSam Leffler 
26915591b213SSam Leffler 	/* configure operational mode */
2692c42a7b7eSSam Leffler 	ath_hal_setopmode(ah);
2693c42a7b7eSSam Leffler 
26943d184db2SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_STATE | ATH_DEBUG_MODE,
26953d184db2SAdrian Chadd 	    "%s: ah=%p, ifp=%p, if_addr=%p\n",
26963d184db2SAdrian Chadd 	    __func__,
26973d184db2SAdrian Chadd 	    ah,
26983d184db2SAdrian Chadd 	    ifp,
26993d184db2SAdrian Chadd 	    (ifp == NULL) ? NULL : ifp->if_addr);
27003d184db2SAdrian Chadd 
270129aca940SSam Leffler 	/* handle any link-level address change */
270229aca940SSam Leffler 	ath_hal_setmac(ah, IF_LLADDR(ifp));
27035591b213SSam Leffler 
27045591b213SSam Leffler 	/* calculate and install multicast filter */
2705b032f27cSSam Leffler 	ath_update_mcast(ifp);
27065591b213SSam Leffler }
27075591b213SSam Leffler 
2708c42a7b7eSSam Leffler /*
2709c42a7b7eSSam Leffler  * Set the slot time based on the current setting.
2710c42a7b7eSSam Leffler  */
2711ba5c15d9SAdrian Chadd void
2712c42a7b7eSSam Leffler ath_setslottime(struct ath_softc *sc)
2713c42a7b7eSSam Leffler {
2714b032f27cSSam Leffler 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2715c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
2716aaa70f2fSSam Leffler 	u_int usec;
2717c42a7b7eSSam Leffler 
2718aaa70f2fSSam Leffler 	if (IEEE80211_IS_CHAN_HALF(ic->ic_curchan))
2719aaa70f2fSSam Leffler 		usec = 13;
2720aaa70f2fSSam Leffler 	else if (IEEE80211_IS_CHAN_QUARTER(ic->ic_curchan))
2721aaa70f2fSSam Leffler 		usec = 21;
2722724c193aSSam Leffler 	else if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan)) {
2723724c193aSSam Leffler 		/* honor short/long slot time only in 11g */
2724724c193aSSam Leffler 		/* XXX shouldn't honor on pure g or turbo g channel */
2725724c193aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
2726aaa70f2fSSam Leffler 			usec = HAL_SLOT_TIME_9;
2727aaa70f2fSSam Leffler 		else
2728aaa70f2fSSam Leffler 			usec = HAL_SLOT_TIME_20;
2729724c193aSSam Leffler 	} else
2730724c193aSSam Leffler 		usec = HAL_SLOT_TIME_9;
2731aaa70f2fSSam Leffler 
2732aaa70f2fSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET,
2733aaa70f2fSSam Leffler 	    "%s: chan %u MHz flags 0x%x %s slot, %u usec\n",
2734aaa70f2fSSam Leffler 	    __func__, ic->ic_curchan->ic_freq, ic->ic_curchan->ic_flags,
2735aaa70f2fSSam Leffler 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", usec);
2736aaa70f2fSSam Leffler 
2737aaa70f2fSSam Leffler 	ath_hal_setslottime(ah, usec);
2738c42a7b7eSSam Leffler 	sc->sc_updateslot = OK;
2739c42a7b7eSSam Leffler }
2740c42a7b7eSSam Leffler 
2741c42a7b7eSSam Leffler /*
2742c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update the
2743c42a7b7eSSam Leffler  * slot time based on the current setting.
2744c42a7b7eSSam Leffler  */
2745c42a7b7eSSam Leffler static void
2746c42a7b7eSSam Leffler ath_updateslot(struct ifnet *ifp)
2747c42a7b7eSSam Leffler {
2748c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
2749b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
2750c42a7b7eSSam Leffler 
2751c42a7b7eSSam Leffler 	/*
2752c42a7b7eSSam Leffler 	 * When not coordinating the BSS, change the hardware
2753c42a7b7eSSam Leffler 	 * immediately.  For other operation we defer the change
2754c42a7b7eSSam Leffler 	 * until beacon updates have propagated to the stations.
2755c42a7b7eSSam Leffler 	 */
275659aa14a9SRui Paulo 	if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
275759aa14a9SRui Paulo 	    ic->ic_opmode == IEEE80211_M_MBSS)
2758c42a7b7eSSam Leffler 		sc->sc_updateslot = UPDATE;
2759c42a7b7eSSam Leffler 	else
2760c42a7b7eSSam Leffler 		ath_setslottime(sc);
2761c42a7b7eSSam Leffler }
2762c42a7b7eSSam Leffler 
2763c42a7b7eSSam Leffler /*
2764622b3fd2SSam Leffler  * Append the contents of src to dst; both queues
2765622b3fd2SSam Leffler  * are assumed to be locked.
2766622b3fd2SSam Leffler  */
2767ba5c15d9SAdrian Chadd void
2768622b3fd2SSam Leffler ath_txqmove(struct ath_txq *dst, struct ath_txq *src)
2769622b3fd2SSam Leffler {
2770e86fd7a7SAdrian Chadd 
2771e86fd7a7SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(dst);
2772e86fd7a7SAdrian Chadd 	ATH_TXQ_LOCK_ASSERT(src);
2773e86fd7a7SAdrian Chadd 
27746b349e5aSAdrian Chadd 	TAILQ_CONCAT(&dst->axq_q, &src->axq_q, bf_list);
2775622b3fd2SSam Leffler 	dst->axq_link = src->axq_link;
2776622b3fd2SSam Leffler 	src->axq_link = NULL;
2777622b3fd2SSam Leffler 	dst->axq_depth += src->axq_depth;
27786edf1dc7SAdrian Chadd 	dst->axq_aggr_depth += src->axq_aggr_depth;
2779622b3fd2SSam Leffler 	src->axq_depth = 0;
27806edf1dc7SAdrian Chadd 	src->axq_aggr_depth = 0;
2781622b3fd2SSam Leffler }
2782622b3fd2SSam Leffler 
2783622b3fd2SSam Leffler /*
2784d52f7132SAdrian Chadd  * Reset the hardware, with no loss.
2785d52f7132SAdrian Chadd  *
2786d52f7132SAdrian Chadd  * This can't be used for a general case reset.
2787d52f7132SAdrian Chadd  */
2788d52f7132SAdrian Chadd static void
2789d52f7132SAdrian Chadd ath_reset_proc(void *arg, int pending)
2790d52f7132SAdrian Chadd {
2791d52f7132SAdrian Chadd 	struct ath_softc *sc = arg;
2792d52f7132SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
2793d52f7132SAdrian Chadd 
2794d52f7132SAdrian Chadd #if 0
2795d52f7132SAdrian Chadd 	if_printf(ifp, "%s: resetting\n", __func__);
2796d52f7132SAdrian Chadd #endif
2797d52f7132SAdrian Chadd 	ath_reset(ifp, ATH_RESET_NOLOSS);
2798d52f7132SAdrian Chadd }
2799d52f7132SAdrian Chadd 
2800d52f7132SAdrian Chadd /*
2801c42a7b7eSSam Leffler  * Reset the hardware after detecting beacons have stopped.
2802c42a7b7eSSam Leffler  */
2803c42a7b7eSSam Leffler static void
2804c42a7b7eSSam Leffler ath_bstuck_proc(void *arg, int pending)
2805c42a7b7eSSam Leffler {
2806c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
2807fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
280816d4de92SAdrian Chadd 	uint32_t hangs = 0;
280916d4de92SAdrian Chadd 
281016d4de92SAdrian Chadd 	if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0)
281116d4de92SAdrian Chadd 		if_printf(ifp, "bb hang detected (0x%x)\n", hangs);
2812c42a7b7eSSam Leffler 
2813c42a7b7eSSam Leffler 	if_printf(ifp, "stuck beacon; resetting (bmiss count %u)\n",
2814c42a7b7eSSam Leffler 		sc->sc_bmisscount);
2815c2e34459SSam Leffler 	sc->sc_stats.ast_bstuck++;
281616d4de92SAdrian Chadd 	/*
281716d4de92SAdrian Chadd 	 * This assumes that there's no simultaneous channel mode change
281816d4de92SAdrian Chadd 	 * occuring.
281916d4de92SAdrian Chadd 	 */
2820517526efSAdrian Chadd 	ath_reset(ifp, ATH_RESET_NOLOSS);
2821c42a7b7eSSam Leffler }
2822c42a7b7eSSam Leffler 
28235591b213SSam Leffler static void
28245591b213SSam Leffler ath_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
28255591b213SSam Leffler {
28265591b213SSam Leffler 	bus_addr_t *paddr = (bus_addr_t*) arg;
2827d77367bfSSam Leffler 	KASSERT(error == 0, ("error %u on bus_dma callback", error));
28285591b213SSam Leffler 	*paddr = segs->ds_addr;
28295591b213SSam Leffler }
28305591b213SSam Leffler 
2831c9f78537SAdrian Chadd /*
2832c9f78537SAdrian Chadd  * Allocate the descriptors and appropriate DMA tag/setup.
2833c9f78537SAdrian Chadd  *
2834c9f78537SAdrian Chadd  * For some situations (eg EDMA TX completion), there isn't a requirement
2835c9f78537SAdrian Chadd  * for the ath_buf entries to be allocated.
2836c9f78537SAdrian Chadd  */
28373d184db2SAdrian Chadd int
2838c9f78537SAdrian Chadd ath_descdma_alloc_desc(struct ath_softc *sc,
2839c42a7b7eSSam Leffler 	struct ath_descdma *dd, ath_bufhead *head,
2840b39722d6SAdrian Chadd 	const char *name, int ds_size, int ndesc)
2841c42a7b7eSSam Leffler {
2842c42a7b7eSSam Leffler #define	DS2PHYS(_dd, _ds) \
2843c42a7b7eSSam Leffler 	((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
284445abcd6cSAdrian Chadd #define	ATH_DESC_4KB_BOUND_CHECK(_daddr, _len) \
284545abcd6cSAdrian Chadd 	((((u_int32_t)(_daddr) & 0xFFF) > (0x1000 - (_len))) ? 1 : 0)
2846fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
2847c9f78537SAdrian Chadd 	int error;
284845abcd6cSAdrian Chadd 
28491006fc0cSAdrian Chadd 	dd->dd_descsize = ds_size;
2850c42a7b7eSSam Leffler 
28513d9b1596SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET,
2852b39722d6SAdrian Chadd 	    "%s: %s DMA: %u desc, %d bytes per descriptor\n",
2853b39722d6SAdrian Chadd 	    __func__, name, ndesc, dd->dd_descsize);
2854c42a7b7eSSam Leffler 
2855c42a7b7eSSam Leffler 	dd->dd_name = name;
2856b39722d6SAdrian Chadd 	dd->dd_desc_len = dd->dd_descsize * ndesc;
285745abcd6cSAdrian Chadd 
285845abcd6cSAdrian Chadd 	/*
285945abcd6cSAdrian Chadd 	 * Merlin work-around:
286045abcd6cSAdrian Chadd 	 * Descriptors that cross the 4KB boundary can't be used.
286145abcd6cSAdrian Chadd 	 * Assume one skipped descriptor per 4KB page.
286245abcd6cSAdrian Chadd 	 */
286345abcd6cSAdrian Chadd 	if (! ath_hal_split4ktrans(sc->sc_ah)) {
2864b39722d6SAdrian Chadd 		int numpages = dd->dd_desc_len / 4096;
2865b39722d6SAdrian Chadd 		dd->dd_desc_len += ds_size * numpages;
286645abcd6cSAdrian Chadd 	}
2867c42a7b7eSSam Leffler 
2868c42a7b7eSSam Leffler 	/*
2869c42a7b7eSSam Leffler 	 * Setup DMA descriptor area.
2870c42a7b7eSSam Leffler 	 */
2871c2175ff5SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev),	/* parent */
2872c42a7b7eSSam Leffler 		       PAGE_SIZE, 0,		/* alignment, bounds */
2873c42a7b7eSSam Leffler 		       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
2874c42a7b7eSSam Leffler 		       BUS_SPACE_MAXADDR,	/* highaddr */
2875c42a7b7eSSam Leffler 		       NULL, NULL,		/* filter, filterarg */
2876c42a7b7eSSam Leffler 		       dd->dd_desc_len,		/* maxsize */
2877c42a7b7eSSam Leffler 		       1,			/* nsegments */
28786ccb8ea7SSam Leffler 		       dd->dd_desc_len,		/* maxsegsize */
2879c42a7b7eSSam Leffler 		       BUS_DMA_ALLOCNOW,	/* flags */
2880c42a7b7eSSam Leffler 		       NULL,			/* lockfunc */
2881c42a7b7eSSam Leffler 		       NULL,			/* lockarg */
2882c42a7b7eSSam Leffler 		       &dd->dd_dmat);
2883c42a7b7eSSam Leffler 	if (error != 0) {
2884c42a7b7eSSam Leffler 		if_printf(ifp, "cannot allocate %s DMA tag\n", dd->dd_name);
2885c42a7b7eSSam Leffler 		return error;
2886c42a7b7eSSam Leffler 	}
2887c42a7b7eSSam Leffler 
2888c42a7b7eSSam Leffler 	/* allocate descriptors */
2889c42a7b7eSSam Leffler 	error = bus_dmamem_alloc(dd->dd_dmat, (void**) &dd->dd_desc,
28900553a01fSSam Leffler 				 BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
28910553a01fSSam Leffler 				 &dd->dd_dmamap);
2892c42a7b7eSSam Leffler 	if (error != 0) {
2893c42a7b7eSSam Leffler 		if_printf(ifp, "unable to alloc memory for %u %s descriptors, "
2894b39722d6SAdrian Chadd 			"error %u\n", ndesc, dd->dd_name, error);
2895c42a7b7eSSam Leffler 		goto fail1;
2896c42a7b7eSSam Leffler 	}
2897c42a7b7eSSam Leffler 
2898c42a7b7eSSam Leffler 	error = bus_dmamap_load(dd->dd_dmat, dd->dd_dmamap,
2899c42a7b7eSSam Leffler 				dd->dd_desc, dd->dd_desc_len,
2900c42a7b7eSSam Leffler 				ath_load_cb, &dd->dd_desc_paddr,
2901c42a7b7eSSam Leffler 				BUS_DMA_NOWAIT);
2902c42a7b7eSSam Leffler 	if (error != 0) {
2903c42a7b7eSSam Leffler 		if_printf(ifp, "unable to map %s descriptors, error %u\n",
2904c42a7b7eSSam Leffler 			dd->dd_name, error);
2905c42a7b7eSSam Leffler 		goto fail2;
2906c42a7b7eSSam Leffler 	}
2907c42a7b7eSSam Leffler 
2908c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA map: %p (%lu) -> %p (%lu)\n",
2909c9f78537SAdrian Chadd 	    __func__, dd->dd_name, (uint8_t *) dd->dd_desc,
2910c9f78537SAdrian Chadd 	    (u_long) dd->dd_desc_len, (caddr_t) dd->dd_desc_paddr,
2911c9f78537SAdrian Chadd 	    /*XXX*/ (u_long) dd->dd_desc_len);
2912c9f78537SAdrian Chadd 
2913c9f78537SAdrian Chadd 	return (0);
2914c9f78537SAdrian Chadd 
2915c9f78537SAdrian Chadd fail2:
2916c9f78537SAdrian Chadd 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
2917c9f78537SAdrian Chadd fail1:
2918c9f78537SAdrian Chadd 	bus_dma_tag_destroy(dd->dd_dmat);
2919c9f78537SAdrian Chadd 	memset(dd, 0, sizeof(*dd));
2920c9f78537SAdrian Chadd 	return error;
2921c9f78537SAdrian Chadd #undef DS2PHYS
2922c9f78537SAdrian Chadd #undef ATH_DESC_4KB_BOUND_CHECK
2923c9f78537SAdrian Chadd }
2924c9f78537SAdrian Chadd 
2925c9f78537SAdrian Chadd int
2926c9f78537SAdrian Chadd ath_descdma_setup(struct ath_softc *sc,
2927c9f78537SAdrian Chadd 	struct ath_descdma *dd, ath_bufhead *head,
2928c9f78537SAdrian Chadd 	const char *name, int ds_size, int nbuf, int ndesc)
2929c9f78537SAdrian Chadd {
2930c9f78537SAdrian Chadd #define	DS2PHYS(_dd, _ds) \
2931c9f78537SAdrian Chadd 	((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
2932c9f78537SAdrian Chadd #define	ATH_DESC_4KB_BOUND_CHECK(_daddr, _len) \
2933c9f78537SAdrian Chadd 	((((u_int32_t)(_daddr) & 0xFFF) > (0x1000 - (_len))) ? 1 : 0)
2934c9f78537SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
2935c9f78537SAdrian Chadd 	uint8_t *ds;
2936c9f78537SAdrian Chadd 	struct ath_buf *bf;
2937c9f78537SAdrian Chadd 	int i, bsize, error;
2938c9f78537SAdrian Chadd 
2939c9f78537SAdrian Chadd 	/* Allocate descriptors */
2940c9f78537SAdrian Chadd 	error = ath_descdma_alloc_desc(sc, dd, head, name, ds_size,
2941b39722d6SAdrian Chadd 	    nbuf * ndesc);
2942c9f78537SAdrian Chadd 
2943c9f78537SAdrian Chadd 	/* Assume any errors during allocation were dealt with */
2944c9f78537SAdrian Chadd 	if (error != 0) {
2945c9f78537SAdrian Chadd 		return (error);
2946c9f78537SAdrian Chadd 	}
2947c9f78537SAdrian Chadd 
2948c9f78537SAdrian Chadd 	ds = (uint8_t *) dd->dd_desc;
2949c42a7b7eSSam Leffler 
2950ebecf802SSam Leffler 	/* allocate rx buffers */
2951c42a7b7eSSam Leffler 	bsize = sizeof(struct ath_buf) * nbuf;
2952c42a7b7eSSam Leffler 	bf = malloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO);
2953c42a7b7eSSam Leffler 	if (bf == NULL) {
2954c42a7b7eSSam Leffler 		if_printf(ifp, "malloc of %s buffers failed, size %u\n",
2955c42a7b7eSSam Leffler 			dd->dd_name, bsize);
2956c42a7b7eSSam Leffler 		goto fail3;
2957c42a7b7eSSam Leffler 	}
2958c42a7b7eSSam Leffler 	dd->dd_bufptr = bf;
2959c42a7b7eSSam Leffler 
29606b349e5aSAdrian Chadd 	TAILQ_INIT(head);
29613d9b1596SAdrian Chadd 	for (i = 0; i < nbuf; i++, bf++, ds += (ndesc * dd->dd_descsize)) {
296245abcd6cSAdrian Chadd 		bf->bf_desc = (struct ath_desc *) ds;
2963c42a7b7eSSam Leffler 		bf->bf_daddr = DS2PHYS(dd, ds);
296445abcd6cSAdrian Chadd 		if (! ath_hal_split4ktrans(sc->sc_ah)) {
296545abcd6cSAdrian Chadd 			/*
296645abcd6cSAdrian Chadd 			 * Merlin WAR: Skip descriptor addresses which
296745abcd6cSAdrian Chadd 			 * cause 4KB boundary crossing along any point
296845abcd6cSAdrian Chadd 			 * in the descriptor.
296945abcd6cSAdrian Chadd 			 */
297045abcd6cSAdrian Chadd 			 if (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr,
29717ef7f613SAdrian Chadd 			     dd->dd_descsize)) {
297245abcd6cSAdrian Chadd 				/* Start at the next page */
297345abcd6cSAdrian Chadd 				ds += 0x1000 - (bf->bf_daddr & 0xFFF);
297445abcd6cSAdrian Chadd 				bf->bf_desc = (struct ath_desc *) ds;
297545abcd6cSAdrian Chadd 				bf->bf_daddr = DS2PHYS(dd, ds);
297645abcd6cSAdrian Chadd 			}
297745abcd6cSAdrian Chadd 		}
2978c42a7b7eSSam Leffler 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
2979c42a7b7eSSam Leffler 				&bf->bf_dmamap);
2980c42a7b7eSSam Leffler 		if (error != 0) {
2981c42a7b7eSSam Leffler 			if_printf(ifp, "unable to create dmamap for %s "
2982c42a7b7eSSam Leffler 				"buffer %u, error %u\n", dd->dd_name, i, error);
2983c42a7b7eSSam Leffler 			ath_descdma_cleanup(sc, dd, head);
2984c42a7b7eSSam Leffler 			return error;
2985c42a7b7eSSam Leffler 		}
29866edf1dc7SAdrian Chadd 		bf->bf_lastds = bf->bf_desc;	/* Just an initial value */
29876b349e5aSAdrian Chadd 		TAILQ_INSERT_TAIL(head, bf, bf_list);
2988c42a7b7eSSam Leffler 	}
29897ef7f613SAdrian Chadd 
29907ef7f613SAdrian Chadd 	/*
29917ef7f613SAdrian Chadd 	 * XXX TODO: ensure that ds doesn't overflow the descriptor
29927ef7f613SAdrian Chadd 	 * allocation otherwise weird stuff will occur and crash your
29937ef7f613SAdrian Chadd 	 * machine.
29947ef7f613SAdrian Chadd 	 */
2995c42a7b7eSSam Leffler 	return 0;
2996c9f78537SAdrian Chadd 	/* XXX this should likely just call ath_descdma_cleanup() */
2997c42a7b7eSSam Leffler fail3:
2998c42a7b7eSSam Leffler 	bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
2999c42a7b7eSSam Leffler 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3000c42a7b7eSSam Leffler 	bus_dma_tag_destroy(dd->dd_dmat);
3001c42a7b7eSSam Leffler 	memset(dd, 0, sizeof(*dd));
3002c42a7b7eSSam Leffler 	return error;
3003c42a7b7eSSam Leffler #undef DS2PHYS
300445abcd6cSAdrian Chadd #undef ATH_DESC_4KB_BOUND_CHECK
3005c42a7b7eSSam Leffler }
3006c42a7b7eSSam Leffler 
300739abbd9bSAdrian Chadd /*
300839abbd9bSAdrian Chadd  * Allocate ath_buf entries but no descriptor contents.
300939abbd9bSAdrian Chadd  *
301039abbd9bSAdrian Chadd  * This is for RX EDMA where the descriptors are the header part of
301139abbd9bSAdrian Chadd  * the RX buffer.
301239abbd9bSAdrian Chadd  */
301339abbd9bSAdrian Chadd int
301439abbd9bSAdrian Chadd ath_descdma_setup_rx_edma(struct ath_softc *sc,
301539abbd9bSAdrian Chadd 	struct ath_descdma *dd, ath_bufhead *head,
301639abbd9bSAdrian Chadd 	const char *name, int nbuf, int rx_status_len)
301739abbd9bSAdrian Chadd {
301839abbd9bSAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
301939abbd9bSAdrian Chadd 	struct ath_buf *bf;
302039abbd9bSAdrian Chadd 	int i, bsize, error;
302139abbd9bSAdrian Chadd 
302239abbd9bSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA: %u buffers\n",
302339abbd9bSAdrian Chadd 	    __func__, name, nbuf);
302439abbd9bSAdrian Chadd 
302539abbd9bSAdrian Chadd 	dd->dd_name = name;
302639abbd9bSAdrian Chadd 	/*
302739abbd9bSAdrian Chadd 	 * This is (mostly) purely for show.  We're not allocating any actual
302839abbd9bSAdrian Chadd 	 * descriptors here as EDMA RX has the descriptor be part
302939abbd9bSAdrian Chadd 	 * of the RX buffer.
303039abbd9bSAdrian Chadd 	 *
303139abbd9bSAdrian Chadd 	 * However, dd_desc_len is used by ath_descdma_free() to determine
303239abbd9bSAdrian Chadd 	 * whether we have already freed this DMA mapping.
303339abbd9bSAdrian Chadd 	 */
30343d9b1596SAdrian Chadd 	dd->dd_desc_len = rx_status_len * nbuf;
30353d9b1596SAdrian Chadd 	dd->dd_descsize = rx_status_len;
303639abbd9bSAdrian Chadd 
303739abbd9bSAdrian Chadd 	/* allocate rx buffers */
303839abbd9bSAdrian Chadd 	bsize = sizeof(struct ath_buf) * nbuf;
303939abbd9bSAdrian Chadd 	bf = malloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO);
304039abbd9bSAdrian Chadd 	if (bf == NULL) {
304139abbd9bSAdrian Chadd 		if_printf(ifp, "malloc of %s buffers failed, size %u\n",
304239abbd9bSAdrian Chadd 			dd->dd_name, bsize);
3043b5b60f35SAdrian Chadd 		error = ENOMEM;
304439abbd9bSAdrian Chadd 		goto fail3;
304539abbd9bSAdrian Chadd 	}
304639abbd9bSAdrian Chadd 	dd->dd_bufptr = bf;
304739abbd9bSAdrian Chadd 
304839abbd9bSAdrian Chadd 	TAILQ_INIT(head);
304939abbd9bSAdrian Chadd 	for (i = 0; i < nbuf; i++, bf++) {
305039abbd9bSAdrian Chadd 		bf->bf_desc = NULL;
305139abbd9bSAdrian Chadd 		bf->bf_daddr = 0;
305239abbd9bSAdrian Chadd 		bf->bf_lastds = NULL;	/* Just an initial value */
305339abbd9bSAdrian Chadd 
305439abbd9bSAdrian Chadd 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
305539abbd9bSAdrian Chadd 				&bf->bf_dmamap);
305639abbd9bSAdrian Chadd 		if (error != 0) {
305739abbd9bSAdrian Chadd 			if_printf(ifp, "unable to create dmamap for %s "
305839abbd9bSAdrian Chadd 				"buffer %u, error %u\n", dd->dd_name, i, error);
305939abbd9bSAdrian Chadd 			ath_descdma_cleanup(sc, dd, head);
306039abbd9bSAdrian Chadd 			return error;
306139abbd9bSAdrian Chadd 		}
306239abbd9bSAdrian Chadd 		TAILQ_INSERT_TAIL(head, bf, bf_list);
306339abbd9bSAdrian Chadd 	}
306439abbd9bSAdrian Chadd 	return 0;
306539abbd9bSAdrian Chadd fail3:
306639abbd9bSAdrian Chadd 	memset(dd, 0, sizeof(*dd));
306739abbd9bSAdrian Chadd 	return error;
306839abbd9bSAdrian Chadd }
306939abbd9bSAdrian Chadd 
30703d184db2SAdrian Chadd void
3071c42a7b7eSSam Leffler ath_descdma_cleanup(struct ath_softc *sc,
3072c42a7b7eSSam Leffler 	struct ath_descdma *dd, ath_bufhead *head)
3073c42a7b7eSSam Leffler {
3074c42a7b7eSSam Leffler 	struct ath_buf *bf;
3075c42a7b7eSSam Leffler 	struct ieee80211_node *ni;
3076c42a7b7eSSam Leffler 
30778d467c41SAdrian Chadd 	if (dd->dd_dmamap != 0) {
3078c42a7b7eSSam Leffler 		bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
3079c42a7b7eSSam Leffler 		bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3080c42a7b7eSSam Leffler 		bus_dma_tag_destroy(dd->dd_dmat);
30818d467c41SAdrian Chadd 	}
3082c42a7b7eSSam Leffler 
30839ed9f02bSAdrian Chadd 	if (head != NULL) {
30846b349e5aSAdrian Chadd 		TAILQ_FOREACH(bf, head, bf_list) {
3085c42a7b7eSSam Leffler 			if (bf->bf_m) {
3086c42a7b7eSSam Leffler 				m_freem(bf->bf_m);
3087c42a7b7eSSam Leffler 				bf->bf_m = NULL;
3088c42a7b7eSSam Leffler 			}
3089c42a7b7eSSam Leffler 			if (bf->bf_dmamap != NULL) {
3090c42a7b7eSSam Leffler 				bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
3091c42a7b7eSSam Leffler 				bf->bf_dmamap = NULL;
3092c42a7b7eSSam Leffler 			}
3093c42a7b7eSSam Leffler 			ni = bf->bf_node;
3094c42a7b7eSSam Leffler 			bf->bf_node = NULL;
3095c42a7b7eSSam Leffler 			if (ni != NULL) {
3096c42a7b7eSSam Leffler 				/*
3097c42a7b7eSSam Leffler 				 * Reclaim node reference.
3098c42a7b7eSSam Leffler 				 */
3099c42a7b7eSSam Leffler 				ieee80211_free_node(ni);
3100c42a7b7eSSam Leffler 			}
3101c42a7b7eSSam Leffler 		}
31029ed9f02bSAdrian Chadd 	}
3103c42a7b7eSSam Leffler 
31049ed9f02bSAdrian Chadd 	if (head != NULL)
31056b349e5aSAdrian Chadd 		TAILQ_INIT(head);
31069ed9f02bSAdrian Chadd 
31079ed9f02bSAdrian Chadd 	if (dd->dd_bufptr != NULL)
3108c42a7b7eSSam Leffler 		free(dd->dd_bufptr, M_ATHDEV);
3109c42a7b7eSSam Leffler 	memset(dd, 0, sizeof(*dd));
3110c42a7b7eSSam Leffler }
3111c42a7b7eSSam Leffler 
3112c42a7b7eSSam Leffler static int
31135591b213SSam Leffler ath_desc_alloc(struct ath_softc *sc)
31145591b213SSam Leffler {
3115c42a7b7eSSam Leffler 	int error;
31165591b213SSam Leffler 
3117c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_txdma, &sc->sc_txbuf,
31181006fc0cSAdrian Chadd 		    "tx", sc->sc_tx_desclen, ath_txbuf, ATH_TXDESC);
3119c42a7b7eSSam Leffler 	if (error != 0) {
31205591b213SSam Leffler 		return error;
3121c42a7b7eSSam Leffler 	}
312223ced6c1SAdrian Chadd 	sc->sc_txbuf_cnt = ath_txbuf;
3123c42a7b7eSSam Leffler 
3124af33d486SAdrian Chadd 	error = ath_descdma_setup(sc, &sc->sc_txdma_mgmt, &sc->sc_txbuf_mgmt,
31251006fc0cSAdrian Chadd 		    "tx_mgmt", sc->sc_tx_desclen, ath_txbuf_mgmt,
31261006fc0cSAdrian Chadd 		    ATH_TXDESC);
3127af33d486SAdrian Chadd 	if (error != 0) {
3128af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3129af33d486SAdrian Chadd 		return error;
3130af33d486SAdrian Chadd 	}
3131af33d486SAdrian Chadd 
3132af33d486SAdrian Chadd 	/*
3133af33d486SAdrian Chadd 	 * XXX mark txbuf_mgmt frames with ATH_BUF_MGMT, so the
3134af33d486SAdrian Chadd 	 * flag doesn't have to be set in ath_getbuf_locked().
3135af33d486SAdrian Chadd 	 */
3136af33d486SAdrian Chadd 
3137c42a7b7eSSam Leffler 	error = ath_descdma_setup(sc, &sc->sc_bdma, &sc->sc_bbuf,
31381006fc0cSAdrian Chadd 			"beacon", sc->sc_tx_desclen, ATH_BCBUF, 1);
3139c42a7b7eSSam Leffler 	if (error != 0) {
3140af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3141af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma_mgmt,
3142af33d486SAdrian Chadd 		    &sc->sc_txbuf_mgmt);
3143c42a7b7eSSam Leffler 		return error;
3144c42a7b7eSSam Leffler 	}
31455591b213SSam Leffler 	return 0;
31465591b213SSam Leffler }
31475591b213SSam Leffler 
31485591b213SSam Leffler static void
31495591b213SSam Leffler ath_desc_free(struct ath_softc *sc)
31505591b213SSam Leffler {
31515591b213SSam Leffler 
3152c42a7b7eSSam Leffler 	if (sc->sc_bdma.dd_desc_len != 0)
3153c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_bdma, &sc->sc_bbuf);
3154c42a7b7eSSam Leffler 	if (sc->sc_txdma.dd_desc_len != 0)
3155c42a7b7eSSam Leffler 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3156af33d486SAdrian Chadd 	if (sc->sc_txdma_mgmt.dd_desc_len != 0)
3157af33d486SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_txdma_mgmt,
3158af33d486SAdrian Chadd 		    &sc->sc_txbuf_mgmt);
31595591b213SSam Leffler }
31605591b213SSam Leffler 
31615591b213SSam Leffler static struct ieee80211_node *
316238c208f8SSam Leffler ath_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
31635591b213SSam Leffler {
316438c208f8SSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
3165c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
3166c42a7b7eSSam Leffler 	const size_t space = sizeof(struct ath_node) + sc->sc_rc->arc_space;
3167c42a7b7eSSam Leffler 	struct ath_node *an;
3168c42a7b7eSSam Leffler 
3169c42a7b7eSSam Leffler 	an = malloc(space, M_80211_NODE, M_NOWAIT|M_ZERO);
3170c42a7b7eSSam Leffler 	if (an == NULL) {
3171c42a7b7eSSam Leffler 		/* XXX stat+msg */
3172de5af704SSam Leffler 		return NULL;
31735591b213SSam Leffler 	}
3174c42a7b7eSSam Leffler 	ath_rate_node_init(sc, an);
31755591b213SSam Leffler 
31763dd85b26SAdrian Chadd 	/* Setup the mutex - there's no associd yet so set the name to NULL */
31773dd85b26SAdrian Chadd 	snprintf(an->an_name, sizeof(an->an_name), "%s: node %p",
31783dd85b26SAdrian Chadd 	    device_get_nameunit(sc->sc_dev), an);
31793dd85b26SAdrian Chadd 	mtx_init(&an->an_mtx, an->an_name, NULL, MTX_DEF);
31803dd85b26SAdrian Chadd 
3181eb6f0de0SAdrian Chadd 	/* XXX setup ath_tid */
3182eb6f0de0SAdrian Chadd 	ath_tx_tid_init(sc, an);
3183eb6f0de0SAdrian Chadd 
3184c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: an %p\n", __func__, an);
3185c42a7b7eSSam Leffler 	return &an->an_node;
3186c42a7b7eSSam Leffler }
3187c42a7b7eSSam Leffler 
31885591b213SSam Leffler static void
31894afa805eSAdrian Chadd ath_node_cleanup(struct ieee80211_node *ni)
31904afa805eSAdrian Chadd {
31914afa805eSAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
31924afa805eSAdrian Chadd 	struct ath_softc *sc = ic->ic_ifp->if_softc;
31934afa805eSAdrian Chadd 
31944afa805eSAdrian Chadd 	/* Cleanup ath_tid, free unused bufs, unlink bufs in TXQ */
3195eb6f0de0SAdrian Chadd 	ath_tx_node_flush(sc, ATH_NODE(ni));
31964afa805eSAdrian Chadd 	ath_rate_node_cleanup(sc, ATH_NODE(ni));
31974afa805eSAdrian Chadd 	sc->sc_node_cleanup(ni);
31984afa805eSAdrian Chadd }
31994afa805eSAdrian Chadd 
32004afa805eSAdrian Chadd static void
3201c42a7b7eSSam Leffler ath_node_free(struct ieee80211_node *ni)
32025591b213SSam Leffler {
3203c42a7b7eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
3204c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
32051e774079SSam Leffler 
3206c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: ni %p\n", __func__, ni);
32073dd85b26SAdrian Chadd 	mtx_destroy(&ATH_NODE(ni)->an_mtx);
3208c42a7b7eSSam Leffler 	sc->sc_node_free(ni);
32095591b213SSam Leffler }
32105591b213SSam Leffler 
321168e8e04eSSam Leffler static void
321268e8e04eSSam Leffler ath_node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
321368e8e04eSSam Leffler {
321468e8e04eSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
321568e8e04eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
321668e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
321768e8e04eSSam Leffler 
3218b032f27cSSam Leffler 	*rssi = ic->ic_node_getrssi(ni);
321959efa8b5SSam Leffler 	if (ni->ni_chan != IEEE80211_CHAN_ANYC)
322059efa8b5SSam Leffler 		*noise = ath_hal_getchannoise(ah, ni->ni_chan);
322159efa8b5SSam Leffler 	else
322268e8e04eSSam Leffler 		*noise = -95;		/* nominally correct */
322368e8e04eSSam Leffler }
322468e8e04eSSam Leffler 
3225c42a7b7eSSam Leffler /*
3226c42a7b7eSSam Leffler  * Set the default antenna.
3227c42a7b7eSSam Leffler  */
3228e60c4fc2SAdrian Chadd void
3229c42a7b7eSSam Leffler ath_setdefantenna(struct ath_softc *sc, u_int antenna)
3230c42a7b7eSSam Leffler {
3231c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3232c42a7b7eSSam Leffler 
3233c42a7b7eSSam Leffler 	/* XXX block beacon interrupts */
3234c42a7b7eSSam Leffler 	ath_hal_setdefantenna(ah, antenna);
3235c42a7b7eSSam Leffler 	if (sc->sc_defant != antenna)
3236c42a7b7eSSam Leffler 		sc->sc_stats.ast_ant_defswitch++;
3237c42a7b7eSSam Leffler 	sc->sc_defant = antenna;
3238c42a7b7eSSam Leffler 	sc->sc_rxotherant = 0;
3239c42a7b7eSSam Leffler }
3240c42a7b7eSSam Leffler 
32415463c4a4SSam Leffler static void
3242622b3fd2SSam Leffler ath_txq_init(struct ath_softc *sc, struct ath_txq *txq, int qnum)
3243622b3fd2SSam Leffler {
3244622b3fd2SSam Leffler 	txq->axq_qnum = qnum;
3245339ccfb3SSam Leffler 	txq->axq_ac = 0;
3246622b3fd2SSam Leffler 	txq->axq_depth = 0;
324716d4de92SAdrian Chadd 	txq->axq_aggr_depth = 0;
3248622b3fd2SSam Leffler 	txq->axq_intrcnt = 0;
3249622b3fd2SSam Leffler 	txq->axq_link = NULL;
32506b349e5aSAdrian Chadd 	txq->axq_softc = sc;
32516b349e5aSAdrian Chadd 	TAILQ_INIT(&txq->axq_q);
32526b349e5aSAdrian Chadd 	TAILQ_INIT(&txq->axq_tidq);
3253622b3fd2SSam Leffler 	ATH_TXQ_LOCK_INIT(sc, txq);
3254622b3fd2SSam Leffler }
3255622b3fd2SSam Leffler 
32565591b213SSam Leffler /*
3257c42a7b7eSSam Leffler  * Setup a h/w transmit queue.
32585591b213SSam Leffler  */
3259c42a7b7eSSam Leffler static struct ath_txq *
3260c42a7b7eSSam Leffler ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
3261c42a7b7eSSam Leffler {
3262c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
3263c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3264c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
3265c42a7b7eSSam Leffler 	int qnum;
3266c42a7b7eSSam Leffler 
3267c42a7b7eSSam Leffler 	memset(&qi, 0, sizeof(qi));
3268c42a7b7eSSam Leffler 	qi.tqi_subtype = subtype;
3269c42a7b7eSSam Leffler 	qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
3270c42a7b7eSSam Leffler 	qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
3271c42a7b7eSSam Leffler 	qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
3272c42a7b7eSSam Leffler 	/*
3273c42a7b7eSSam Leffler 	 * Enable interrupts only for EOL and DESC conditions.
3274c42a7b7eSSam Leffler 	 * We mark tx descriptors to receive a DESC interrupt
3275c42a7b7eSSam Leffler 	 * when a tx queue gets deep; otherwise waiting for the
3276c42a7b7eSSam Leffler 	 * EOL to reap descriptors.  Note that this is done to
3277c42a7b7eSSam Leffler 	 * reduce interrupt load and this only defers reaping
3278c42a7b7eSSam Leffler 	 * descriptors, never transmitting frames.  Aside from
3279c42a7b7eSSam Leffler 	 * reducing interrupts this also permits more concurrency.
3280c42a7b7eSSam Leffler 	 * The only potential downside is if the tx queue backs
3281c42a7b7eSSam Leffler 	 * up in which case the top half of the kernel may backup
3282c42a7b7eSSam Leffler 	 * due to a lack of tx descriptors.
3283c42a7b7eSSam Leffler 	 */
3284bd5a9920SSam Leffler 	qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE | HAL_TXQ_TXDESCINT_ENABLE;
3285c42a7b7eSSam Leffler 	qnum = ath_hal_setuptxqueue(ah, qtype, &qi);
3286c42a7b7eSSam Leffler 	if (qnum == -1) {
3287c42a7b7eSSam Leffler 		/*
3288c42a7b7eSSam Leffler 		 * NB: don't print a message, this happens
3289a614e076SSam Leffler 		 * normally on parts with too few tx queues
3290c42a7b7eSSam Leffler 		 */
3291c42a7b7eSSam Leffler 		return NULL;
3292c42a7b7eSSam Leffler 	}
3293c42a7b7eSSam Leffler 	if (qnum >= N(sc->sc_txq)) {
32946891c875SPeter Wemm 		device_printf(sc->sc_dev,
32956891c875SPeter Wemm 			"hal qnum %u out of range, max %zu!\n",
3296c42a7b7eSSam Leffler 			qnum, N(sc->sc_txq));
3297c42a7b7eSSam Leffler 		ath_hal_releasetxqueue(ah, qnum);
3298c42a7b7eSSam Leffler 		return NULL;
3299c42a7b7eSSam Leffler 	}
3300c42a7b7eSSam Leffler 	if (!ATH_TXQ_SETUP(sc, qnum)) {
3301622b3fd2SSam Leffler 		ath_txq_init(sc, &sc->sc_txq[qnum], qnum);
3302c42a7b7eSSam Leffler 		sc->sc_txqsetup |= 1<<qnum;
3303c42a7b7eSSam Leffler 	}
3304c42a7b7eSSam Leffler 	return &sc->sc_txq[qnum];
3305c42a7b7eSSam Leffler #undef N
3306c42a7b7eSSam Leffler }
3307c42a7b7eSSam Leffler 
3308c42a7b7eSSam Leffler /*
3309c42a7b7eSSam Leffler  * Setup a hardware data transmit queue for the specified
3310c42a7b7eSSam Leffler  * access control.  The hal may not support all requested
3311c42a7b7eSSam Leffler  * queues in which case it will return a reference to a
3312c42a7b7eSSam Leffler  * previously setup queue.  We record the mapping from ac's
3313c42a7b7eSSam Leffler  * to h/w queues for use by ath_tx_start and also track
3314c42a7b7eSSam Leffler  * the set of h/w queues being used to optimize work in the
3315c42a7b7eSSam Leffler  * transmit interrupt handler and related routines.
3316c42a7b7eSSam Leffler  */
3317c42a7b7eSSam Leffler static int
3318c42a7b7eSSam Leffler ath_tx_setup(struct ath_softc *sc, int ac, int haltype)
3319c42a7b7eSSam Leffler {
3320c42a7b7eSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
3321c42a7b7eSSam Leffler 	struct ath_txq *txq;
3322c42a7b7eSSam Leffler 
3323c42a7b7eSSam Leffler 	if (ac >= N(sc->sc_ac2q)) {
33246891c875SPeter Wemm 		device_printf(sc->sc_dev, "AC %u out of range, max %zu!\n",
3325c42a7b7eSSam Leffler 			ac, N(sc->sc_ac2q));
3326c42a7b7eSSam Leffler 		return 0;
3327c42a7b7eSSam Leffler 	}
3328c42a7b7eSSam Leffler 	txq = ath_txq_setup(sc, HAL_TX_QUEUE_DATA, haltype);
3329c42a7b7eSSam Leffler 	if (txq != NULL) {
3330339ccfb3SSam Leffler 		txq->axq_ac = ac;
3331c42a7b7eSSam Leffler 		sc->sc_ac2q[ac] = txq;
3332c42a7b7eSSam Leffler 		return 1;
3333c42a7b7eSSam Leffler 	} else
3334c42a7b7eSSam Leffler 		return 0;
3335c42a7b7eSSam Leffler #undef N
3336c42a7b7eSSam Leffler }
3337c42a7b7eSSam Leffler 
3338c42a7b7eSSam Leffler /*
3339c42a7b7eSSam Leffler  * Update WME parameters for a transmit queue.
3340c42a7b7eSSam Leffler  */
3341c42a7b7eSSam Leffler static int
3342c42a7b7eSSam Leffler ath_txq_update(struct ath_softc *sc, int ac)
3343c42a7b7eSSam Leffler {
3344c42a7b7eSSam Leffler #define	ATH_EXPONENT_TO_VALUE(v)	((1<<v)-1)
3345c42a7b7eSSam Leffler #define	ATH_TXOP_TO_US(v)		(v<<5)
3346b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
3347b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
3348c42a7b7eSSam Leffler 	struct ath_txq *txq = sc->sc_ac2q[ac];
3349c42a7b7eSSam Leffler 	struct wmeParams *wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
3350c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
3351c42a7b7eSSam Leffler 	HAL_TXQ_INFO qi;
3352c42a7b7eSSam Leffler 
3353c42a7b7eSSam Leffler 	ath_hal_gettxqueueprops(ah, txq->axq_qnum, &qi);
3354584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
335510ad9a77SSam Leffler 	if (sc->sc_tdma) {
335610ad9a77SSam Leffler 		/*
335710ad9a77SSam Leffler 		 * AIFS is zero so there's no pre-transmit wait.  The
335810ad9a77SSam Leffler 		 * burst time defines the slot duration and is configured
335909be6601SSam Leffler 		 * through net80211.  The QCU is setup to not do post-xmit
336010ad9a77SSam Leffler 		 * back off, lockout all lower-priority QCU's, and fire
336110ad9a77SSam Leffler 		 * off the DMA beacon alert timer which is setup based
336210ad9a77SSam Leffler 		 * on the slot configuration.
336310ad9a77SSam Leffler 		 */
336410ad9a77SSam Leffler 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
336510ad9a77SSam Leffler 			      | HAL_TXQ_TXERRINT_ENABLE
336610ad9a77SSam Leffler 			      | HAL_TXQ_TXURNINT_ENABLE
336710ad9a77SSam Leffler 			      | HAL_TXQ_TXEOLINT_ENABLE
336810ad9a77SSam Leffler 			      | HAL_TXQ_DBA_GATED
336910ad9a77SSam Leffler 			      | HAL_TXQ_BACKOFF_DISABLE
337010ad9a77SSam Leffler 			      | HAL_TXQ_ARB_LOCKOUT_GLOBAL
337110ad9a77SSam Leffler 			      ;
337210ad9a77SSam Leffler 		qi.tqi_aifs = 0;
337310ad9a77SSam Leffler 		/* XXX +dbaprep? */
337410ad9a77SSam Leffler 		qi.tqi_readyTime = sc->sc_tdmaslotlen;
337510ad9a77SSam Leffler 		qi.tqi_burstTime = qi.tqi_readyTime;
337610ad9a77SSam Leffler 	} else {
337710ad9a77SSam Leffler #endif
337816d4de92SAdrian Chadd 		/*
337916d4de92SAdrian Chadd 		 * XXX shouldn't this just use the default flags
338016d4de92SAdrian Chadd 		 * used in the previous queue setup?
338116d4de92SAdrian Chadd 		 */
338210ad9a77SSam Leffler 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
338310ad9a77SSam Leffler 			      | HAL_TXQ_TXERRINT_ENABLE
338410ad9a77SSam Leffler 			      | HAL_TXQ_TXDESCINT_ENABLE
338510ad9a77SSam Leffler 			      | HAL_TXQ_TXURNINT_ENABLE
33861f25c0f7SAdrian Chadd 			      | HAL_TXQ_TXEOLINT_ENABLE
338710ad9a77SSam Leffler 			      ;
3388c42a7b7eSSam Leffler 		qi.tqi_aifs = wmep->wmep_aifsn;
3389c42a7b7eSSam Leffler 		qi.tqi_cwmin = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
3390c42a7b7eSSam Leffler 		qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
339110ad9a77SSam Leffler 		qi.tqi_readyTime = 0;
3392c42a7b7eSSam Leffler 		qi.tqi_burstTime = ATH_TXOP_TO_US(wmep->wmep_txopLimit);
3393584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
339410ad9a77SSam Leffler 	}
339510ad9a77SSam Leffler #endif
339610ad9a77SSam Leffler 
339710ad9a77SSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET,
339810ad9a77SSam Leffler 	    "%s: Q%u qflags 0x%x aifs %u cwmin %u cwmax %u burstTime %u\n",
339910ad9a77SSam Leffler 	    __func__, txq->axq_qnum, qi.tqi_qflags,
340010ad9a77SSam Leffler 	    qi.tqi_aifs, qi.tqi_cwmin, qi.tqi_cwmax, qi.tqi_burstTime);
3401c42a7b7eSSam Leffler 
3402c42a7b7eSSam Leffler 	if (!ath_hal_settxqueueprops(ah, txq->axq_qnum, &qi)) {
3403b032f27cSSam Leffler 		if_printf(ifp, "unable to update hardware queue "
3404c42a7b7eSSam Leffler 			"parameters for %s traffic!\n",
3405c42a7b7eSSam Leffler 			ieee80211_wme_acnames[ac]);
3406c42a7b7eSSam Leffler 		return 0;
3407c42a7b7eSSam Leffler 	} else {
3408c42a7b7eSSam Leffler 		ath_hal_resettxqueue(ah, txq->axq_qnum); /* push to h/w */
3409c42a7b7eSSam Leffler 		return 1;
3410c42a7b7eSSam Leffler 	}
3411c42a7b7eSSam Leffler #undef ATH_TXOP_TO_US
3412c42a7b7eSSam Leffler #undef ATH_EXPONENT_TO_VALUE
3413c42a7b7eSSam Leffler }
3414c42a7b7eSSam Leffler 
3415c42a7b7eSSam Leffler /*
3416c42a7b7eSSam Leffler  * Callback from the 802.11 layer to update WME parameters.
3417c42a7b7eSSam Leffler  */
3418a35dae8dSAdrian Chadd int
3419c42a7b7eSSam Leffler ath_wme_update(struct ieee80211com *ic)
3420c42a7b7eSSam Leffler {
3421c42a7b7eSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
3422c42a7b7eSSam Leffler 
3423c42a7b7eSSam Leffler 	return !ath_txq_update(sc, WME_AC_BE) ||
3424c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_BK) ||
3425c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VI) ||
3426c42a7b7eSSam Leffler 	    !ath_txq_update(sc, WME_AC_VO) ? EIO : 0;
3427c42a7b7eSSam Leffler }
3428c42a7b7eSSam Leffler 
3429c42a7b7eSSam Leffler /*
3430c42a7b7eSSam Leffler  * Reclaim resources for a setup queue.
3431c42a7b7eSSam Leffler  */
3432c42a7b7eSSam Leffler static void
3433c42a7b7eSSam Leffler ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
3434c42a7b7eSSam Leffler {
3435c42a7b7eSSam Leffler 
3436c42a7b7eSSam Leffler 	ath_hal_releasetxqueue(sc->sc_ah, txq->axq_qnum);
3437c42a7b7eSSam Leffler 	ATH_TXQ_LOCK_DESTROY(txq);
3438c42a7b7eSSam Leffler 	sc->sc_txqsetup &= ~(1<<txq->axq_qnum);
3439c42a7b7eSSam Leffler }
3440c42a7b7eSSam Leffler 
3441c42a7b7eSSam Leffler /*
3442c42a7b7eSSam Leffler  * Reclaim all tx queue resources.
3443c42a7b7eSSam Leffler  */
3444c42a7b7eSSam Leffler static void
3445c42a7b7eSSam Leffler ath_tx_cleanup(struct ath_softc *sc)
3446c42a7b7eSSam Leffler {
3447c42a7b7eSSam Leffler 	int i;
3448c42a7b7eSSam Leffler 
3449c42a7b7eSSam Leffler 	ATH_TXBUF_LOCK_DESTROY(sc);
3450c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
3451c42a7b7eSSam Leffler 		if (ATH_TXQ_SETUP(sc, i))
3452c42a7b7eSSam Leffler 			ath_tx_cleanupq(sc, &sc->sc_txq[i]);
3453c42a7b7eSSam Leffler }
34545591b213SSam Leffler 
345599d258fdSSam Leffler /*
3456ab06fdf2SSam Leffler  * Return h/w rate index for an IEEE rate (w/o basic rate bit)
3457ab06fdf2SSam Leffler  * using the current rates in sc_rixmap.
34588b5341deSSam Leffler  */
3459b8e788a5SAdrian Chadd int
3460ab06fdf2SSam Leffler ath_tx_findrix(const struct ath_softc *sc, uint8_t rate)
34618b5341deSSam Leffler {
3462ab06fdf2SSam Leffler 	int rix = sc->sc_rixmap[rate];
3463ab06fdf2SSam Leffler 	/* NB: return lowest rix for invalid rate */
3464ab06fdf2SSam Leffler 	return (rix == 0xff ? 0 : rix);
34658b5341deSSam Leffler }
34668b5341deSSam Leffler 
34679352fb7aSAdrian Chadd static void
34689352fb7aSAdrian Chadd ath_tx_update_stats(struct ath_softc *sc, struct ath_tx_status *ts,
34699352fb7aSAdrian Chadd     struct ath_buf *bf)
34709352fb7aSAdrian Chadd {
34719352fb7aSAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
34729352fb7aSAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
34739352fb7aSAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
34749352fb7aSAdrian Chadd 	int sr, lr, pri;
34759352fb7aSAdrian Chadd 
34769352fb7aSAdrian Chadd 	if (ts->ts_status == 0) {
34779352fb7aSAdrian Chadd 		u_int8_t txant = ts->ts_antenna;
34789352fb7aSAdrian Chadd 		sc->sc_stats.ast_ant_tx[txant]++;
34799352fb7aSAdrian Chadd 		sc->sc_ant_tx[txant]++;
34809352fb7aSAdrian Chadd 		if (ts->ts_finaltsi != 0)
34819352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_altrate++;
34829352fb7aSAdrian Chadd 		pri = M_WME_GETAC(bf->bf_m);
34839352fb7aSAdrian Chadd 		if (pri >= WME_AC_VO)
34849352fb7aSAdrian Chadd 			ic->ic_wme.wme_hipri_traffic++;
3485875a9451SAdrian Chadd 		if ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0)
34869352fb7aSAdrian Chadd 			ni->ni_inact = ni->ni_inact_reload;
34879352fb7aSAdrian Chadd 	} else {
34889352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_XRETRY)
34899352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_xretries++;
34909352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_FIFO)
34919352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_fifoerr++;
34929352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_FILT)
34939352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_filtered++;
34949352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_XTXOP)
34959352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_xtxop++;
34969352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TXERR_TIMER_EXPIRED)
34979352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_timerexpired++;
34989352fb7aSAdrian Chadd 
34999352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TX_DATA_UNDERRUN)
35009352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_data_underrun++;
35019352fb7aSAdrian Chadd 		if (ts->ts_status & HAL_TX_DELIM_UNDERRUN)
35029352fb7aSAdrian Chadd 			sc->sc_stats.ast_tx_delim_underrun++;
35039352fb7aSAdrian Chadd 
35049352fb7aSAdrian Chadd 		if (bf->bf_m->m_flags & M_FF)
35059352fb7aSAdrian Chadd 			sc->sc_stats.ast_ff_txerr++;
35069352fb7aSAdrian Chadd 	}
35079352fb7aSAdrian Chadd 	/* XXX when is this valid? */
35089352fb7aSAdrian Chadd 	if (ts->ts_status & HAL_TX_DESC_CFG_ERR)
35099352fb7aSAdrian Chadd 		sc->sc_stats.ast_tx_desccfgerr++;
35109352fb7aSAdrian Chadd 
35119352fb7aSAdrian Chadd 	sr = ts->ts_shortretry;
35129352fb7aSAdrian Chadd 	lr = ts->ts_longretry;
35139352fb7aSAdrian Chadd 	sc->sc_stats.ast_tx_shortretry += sr;
35149352fb7aSAdrian Chadd 	sc->sc_stats.ast_tx_longretry += lr;
35159352fb7aSAdrian Chadd 
35169352fb7aSAdrian Chadd }
35179352fb7aSAdrian Chadd 
35189352fb7aSAdrian Chadd /*
35199352fb7aSAdrian Chadd  * The default completion. If fail is 1, this means
35209352fb7aSAdrian Chadd  * "please don't retry the frame, and just return -1 status
35219352fb7aSAdrian Chadd  * to the net80211 stack.
35229352fb7aSAdrian Chadd  */
35239352fb7aSAdrian Chadd void
35249352fb7aSAdrian Chadd ath_tx_default_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
35259352fb7aSAdrian Chadd {
35269352fb7aSAdrian Chadd 	struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
35279352fb7aSAdrian Chadd 	int st;
35289352fb7aSAdrian Chadd 
35299352fb7aSAdrian Chadd 	if (fail == 1)
35309352fb7aSAdrian Chadd 		st = -1;
35319352fb7aSAdrian Chadd 	else
3532875a9451SAdrian Chadd 		st = ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0) ?
35339352fb7aSAdrian Chadd 		    ts->ts_status : HAL_TXERR_XRETRY;
35349352fb7aSAdrian Chadd 
35359352fb7aSAdrian Chadd 	if (bf->bf_state.bfs_dobaw)
35369352fb7aSAdrian Chadd 		device_printf(sc->sc_dev,
3537a66d5089SAdrian Chadd 		    "%s: bf %p: seqno %d: dobaw should've been cleared!\n",
3538a66d5089SAdrian Chadd 		    __func__,
3539a66d5089SAdrian Chadd 		    bf,
3540a66d5089SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
35419352fb7aSAdrian Chadd 	if (bf->bf_next != NULL)
35429352fb7aSAdrian Chadd 		device_printf(sc->sc_dev,
3543a66d5089SAdrian Chadd 		    "%s: bf %p: seqno %d: bf_next not NULL!\n",
3544a66d5089SAdrian Chadd 		    __func__,
3545a66d5089SAdrian Chadd 		    bf,
3546a66d5089SAdrian Chadd 		    SEQNO(bf->bf_state.bfs_seqno));
35479352fb7aSAdrian Chadd 
35489352fb7aSAdrian Chadd 	/*
3549*548a605dSAdrian Chadd 	 * Check if the node software queue is empty; if so
3550*548a605dSAdrian Chadd 	 * then clear the TIM.
3551*548a605dSAdrian Chadd 	 *
3552*548a605dSAdrian Chadd 	 * This needs to be done before the buffer is freed as
3553*548a605dSAdrian Chadd 	 * otherwise the node reference will have been released
3554*548a605dSAdrian Chadd 	 * and the node may not actually exist any longer.
3555*548a605dSAdrian Chadd 	 *
3556*548a605dSAdrian Chadd 	 * XXX I don't like this belonging here, but it's cleaner
3557*548a605dSAdrian Chadd 	 * to do it here right now then all the other places
3558*548a605dSAdrian Chadd 	 * where ath_tx_default_comp() is called.
3559*548a605dSAdrian Chadd 	 *
3560*548a605dSAdrian Chadd 	 * XXX TODO: during drain, ensure that the callback is
3561*548a605dSAdrian Chadd 	 * being called so we get a chance to update the TIM.
3562*548a605dSAdrian Chadd 	 */
3563*548a605dSAdrian Chadd 	if (bf->bf_node)
3564*548a605dSAdrian Chadd 		ath_tx_update_tim(sc, bf->bf_node, 0);
3565*548a605dSAdrian Chadd 
3566*548a605dSAdrian Chadd 	/*
35679352fb7aSAdrian Chadd 	 * Do any tx complete callback.  Note this must
35689352fb7aSAdrian Chadd 	 * be done before releasing the node reference.
35699352fb7aSAdrian Chadd 	 * This will free the mbuf, release the net80211
35709352fb7aSAdrian Chadd 	 * node and recycle the ath_buf.
35719352fb7aSAdrian Chadd 	 */
35729352fb7aSAdrian Chadd 	ath_tx_freebuf(sc, bf, st);
35739352fb7aSAdrian Chadd }
35749352fb7aSAdrian Chadd 
35759352fb7aSAdrian Chadd /*
3576eb6f0de0SAdrian Chadd  * Update rate control with the given completion status.
3577eb6f0de0SAdrian Chadd  */
3578eb6f0de0SAdrian Chadd void
3579eb6f0de0SAdrian Chadd ath_tx_update_ratectrl(struct ath_softc *sc, struct ieee80211_node *ni,
3580eb6f0de0SAdrian Chadd     struct ath_rc_series *rc, struct ath_tx_status *ts, int frmlen,
3581eb6f0de0SAdrian Chadd     int nframes, int nbad)
3582eb6f0de0SAdrian Chadd {
3583eb6f0de0SAdrian Chadd 	struct ath_node *an;
3584eb6f0de0SAdrian Chadd 
3585eb6f0de0SAdrian Chadd 	/* Only for unicast frames */
3586eb6f0de0SAdrian Chadd 	if (ni == NULL)
3587eb6f0de0SAdrian Chadd 		return;
3588eb6f0de0SAdrian Chadd 
3589eb6f0de0SAdrian Chadd 	an = ATH_NODE(ni);
3590*548a605dSAdrian Chadd 	ATH_NODE_UNLOCK_ASSERT(an);
3591eb6f0de0SAdrian Chadd 
3592eb6f0de0SAdrian Chadd 	if ((ts->ts_status & HAL_TXERR_FILT) == 0) {
3593eb6f0de0SAdrian Chadd 		ATH_NODE_LOCK(an);
3594eb6f0de0SAdrian Chadd 		ath_rate_tx_complete(sc, an, rc, ts, frmlen, nframes, nbad);
3595eb6f0de0SAdrian Chadd 		ATH_NODE_UNLOCK(an);
3596eb6f0de0SAdrian Chadd 	}
3597eb6f0de0SAdrian Chadd }
3598eb6f0de0SAdrian Chadd 
3599eb6f0de0SAdrian Chadd /*
36009352fb7aSAdrian Chadd  * Update the busy status of the last frame on the free list.
36019352fb7aSAdrian Chadd  * When doing TDMA, the busy flag tracks whether the hardware
36029352fb7aSAdrian Chadd  * currently points to this buffer or not, and thus gated DMA
36039352fb7aSAdrian Chadd  * may restart by re-reading the last descriptor in this
36049352fb7aSAdrian Chadd  * buffer.
36059352fb7aSAdrian Chadd  *
36069352fb7aSAdrian Chadd  * This should be called in the completion function once one
36079352fb7aSAdrian Chadd  * of the buffers has been used.
36089352fb7aSAdrian Chadd  */
36099352fb7aSAdrian Chadd static void
36109352fb7aSAdrian Chadd ath_tx_update_busy(struct ath_softc *sc)
36119352fb7aSAdrian Chadd {
36129352fb7aSAdrian Chadd 	struct ath_buf *last;
36139352fb7aSAdrian Chadd 
36149352fb7aSAdrian Chadd 	/*
36159352fb7aSAdrian Chadd 	 * Since the last frame may still be marked
36169352fb7aSAdrian Chadd 	 * as ATH_BUF_BUSY, unmark it here before
36179352fb7aSAdrian Chadd 	 * finishing the frame processing.
36189352fb7aSAdrian Chadd 	 * Since we've completed a frame (aggregate
36199352fb7aSAdrian Chadd 	 * or otherwise), the hardware has moved on
36209352fb7aSAdrian Chadd 	 * and is no longer referencing the previous
36219352fb7aSAdrian Chadd 	 * descriptor.
36229352fb7aSAdrian Chadd 	 */
36239352fb7aSAdrian Chadd 	ATH_TXBUF_LOCK_ASSERT(sc);
3624af33d486SAdrian Chadd 	last = TAILQ_LAST(&sc->sc_txbuf_mgmt, ath_bufhead_s);
3625af33d486SAdrian Chadd 	if (last != NULL)
3626af33d486SAdrian Chadd 		last->bf_flags &= ~ATH_BUF_BUSY;
36279352fb7aSAdrian Chadd 	last = TAILQ_LAST(&sc->sc_txbuf, ath_bufhead_s);
36289352fb7aSAdrian Chadd 	if (last != NULL)
36299352fb7aSAdrian Chadd 		last->bf_flags &= ~ATH_BUF_BUSY;
36309352fb7aSAdrian Chadd }
36319352fb7aSAdrian Chadd 
363268e8e04eSSam Leffler /*
3633bad98824SAdrian Chadd  * Process the completion of the given buffer.
3634bad98824SAdrian Chadd  *
3635bad98824SAdrian Chadd  * This calls the rate control update and then the buffer completion.
3636bad98824SAdrian Chadd  * This will either free the buffer or requeue it.  In any case, the
3637bad98824SAdrian Chadd  * bf pointer should be treated as invalid after this function is called.
3638bad98824SAdrian Chadd  */
3639bad98824SAdrian Chadd void
3640bad98824SAdrian Chadd ath_tx_process_buf_completion(struct ath_softc *sc, struct ath_txq *txq,
3641bad98824SAdrian Chadd     struct ath_tx_status *ts, struct ath_buf *bf)
3642bad98824SAdrian Chadd {
3643bad98824SAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
3644bad98824SAdrian Chadd 	struct ath_node *an = NULL;
3645bad98824SAdrian Chadd 
3646bad98824SAdrian Chadd 	ATH_TXQ_UNLOCK_ASSERT(txq);
3647bad98824SAdrian Chadd 
3648bad98824SAdrian Chadd 	/* If unicast frame, update general statistics */
3649bad98824SAdrian Chadd 	if (ni != NULL) {
3650bad98824SAdrian Chadd 		an = ATH_NODE(ni);
3651bad98824SAdrian Chadd 		/* update statistics */
3652bad98824SAdrian Chadd 		ath_tx_update_stats(sc, ts, bf);
3653bad98824SAdrian Chadd 	}
3654bad98824SAdrian Chadd 
3655bad98824SAdrian Chadd 	/*
3656bad98824SAdrian Chadd 	 * Call the completion handler.
3657bad98824SAdrian Chadd 	 * The completion handler is responsible for
3658bad98824SAdrian Chadd 	 * calling the rate control code.
3659bad98824SAdrian Chadd 	 *
3660bad98824SAdrian Chadd 	 * Frames with no completion handler get the
3661bad98824SAdrian Chadd 	 * rate control code called here.
3662bad98824SAdrian Chadd 	 */
3663bad98824SAdrian Chadd 	if (bf->bf_comp == NULL) {
3664bad98824SAdrian Chadd 		if ((ts->ts_status & HAL_TXERR_FILT) == 0 &&
3665bad98824SAdrian Chadd 		    (bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0) {
3666bad98824SAdrian Chadd 			/*
3667bad98824SAdrian Chadd 			 * XXX assume this isn't an aggregate
3668bad98824SAdrian Chadd 			 * frame.
3669bad98824SAdrian Chadd 			 */
3670bad98824SAdrian Chadd 			ath_tx_update_ratectrl(sc, ni,
3671bad98824SAdrian Chadd 			     bf->bf_state.bfs_rc, ts,
3672bad98824SAdrian Chadd 			    bf->bf_state.bfs_pktlen, 1,
3673bad98824SAdrian Chadd 			    (ts->ts_status == 0 ? 0 : 1));
3674bad98824SAdrian Chadd 		}
3675bad98824SAdrian Chadd 		ath_tx_default_comp(sc, bf, 0);
3676bad98824SAdrian Chadd 	} else
3677bad98824SAdrian Chadd 		bf->bf_comp(sc, bf, 0);
3678bad98824SAdrian Chadd }
3679bad98824SAdrian Chadd 
3680bad98824SAdrian Chadd 
3681bad98824SAdrian Chadd 
3682bad98824SAdrian Chadd /*
3683c42a7b7eSSam Leffler  * Process completed xmit descriptors from the specified queue.
3684eb6f0de0SAdrian Chadd  * Kick the packet scheduler if needed. This can occur from this
3685eb6f0de0SAdrian Chadd  * particular task.
3686c42a7b7eSSam Leffler  */
3687788e6aa9SAdrian Chadd static int
3688788e6aa9SAdrian Chadd ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq, int dosched)
36895591b213SSam Leffler {
36905591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
36919352fb7aSAdrian Chadd 	struct ath_buf *bf;
36926edf1dc7SAdrian Chadd 	struct ath_desc *ds;
369365f9edeeSSam Leffler 	struct ath_tx_status *ts;
36945591b213SSam Leffler 	struct ieee80211_node *ni;
369553e98d5aSAdrian Chadd #ifdef	IEEE80211_SUPPORT_SUPERG
369643faa6b2SAdrian Chadd 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
369753e98d5aSAdrian Chadd #endif	/* IEEE80211_SUPPORT_SUPERG */
36989352fb7aSAdrian Chadd 	int nacked;
36995591b213SSam Leffler 	HAL_STATUS status;
37005591b213SSam Leffler 
3701c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_TX_PROC, "%s: tx queue %u head %p link %p\n",
3702c42a7b7eSSam Leffler 		__func__, txq->axq_qnum,
3703c42a7b7eSSam Leffler 		(caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
3704c42a7b7eSSam Leffler 		txq->axq_link);
370503682514SAdrian Chadd 
370603682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 4,
370703682514SAdrian Chadd 	    "ath_tx_processq: txq=%u head %p link %p depth %p",
370803682514SAdrian Chadd 	    txq->axq_qnum,
370903682514SAdrian Chadd 	    (caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
371003682514SAdrian Chadd 	    txq->axq_link,
371103682514SAdrian Chadd 	    txq->axq_depth);
371203682514SAdrian Chadd 
3713d7736e13SSam Leffler 	nacked = 0;
37145591b213SSam Leffler 	for (;;) {
3715c42a7b7eSSam Leffler 		ATH_TXQ_LOCK(txq);
3716c42a7b7eSSam Leffler 		txq->axq_intrcnt = 0;	/* reset periodic desc intr count */
37176b349e5aSAdrian Chadd 		bf = TAILQ_FIRST(&txq->axq_q);
37185591b213SSam Leffler 		if (bf == NULL) {
3719c42a7b7eSSam Leffler 			ATH_TXQ_UNLOCK(txq);
37205591b213SSam Leffler 			break;
37215591b213SSam Leffler 		}
37226edf1dc7SAdrian Chadd 		ds = bf->bf_lastds;	/* XXX must be setup correctly! */
372365f9edeeSSam Leffler 		ts = &bf->bf_status.ds_txstat;
372403682514SAdrian Chadd 
372565f9edeeSSam Leffler 		status = ath_hal_txprocdesc(ah, ds, ts);
3726a585a9a1SSam Leffler #ifdef ATH_DEBUG
3727c42a7b7eSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_XMIT_DESC)
37286902009eSSam Leffler 			ath_printtxbuf(sc, bf, txq->axq_qnum, 0,
37296902009eSSam Leffler 			    status == HAL_OK);
373003682514SAdrian Chadd 		else if ((sc->sc_debug & ATH_DEBUG_RESET) && (dosched == 0))
3731d6b20023SAdrian Chadd 			ath_printtxbuf(sc, bf, txq->axq_qnum, 0,
3732d6b20023SAdrian Chadd 			    status == HAL_OK);
37335591b213SSam Leffler #endif
373403682514SAdrian Chadd 
37355591b213SSam Leffler 		if (status == HAL_EINPROGRESS) {
373603682514SAdrian Chadd 			ATH_KTR(sc, ATH_KTR_TXCOMP, 3,
373703682514SAdrian Chadd 			    "ath_tx_processq: txq=%u, bf=%p ds=%p, HAL_EINPROGRESS",
373803682514SAdrian Chadd 			    txq->axq_qnum, bf, ds);
3739c42a7b7eSSam Leffler 			ATH_TXQ_UNLOCK(txq);
37405591b213SSam Leffler 			break;
37415591b213SSam Leffler 		}
37426b349e5aSAdrian Chadd 		ATH_TXQ_REMOVE(txq, bf, bf_list);
3743584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
374410ad9a77SSam Leffler 		if (txq->axq_depth > 0) {
374510ad9a77SSam Leffler 			/*
374610ad9a77SSam Leffler 			 * More frames follow.  Mark the buffer busy
374710ad9a77SSam Leffler 			 * so it's not re-used while the hardware may
374810ad9a77SSam Leffler 			 * still re-read the link field in the descriptor.
37496edf1dc7SAdrian Chadd 			 *
37506edf1dc7SAdrian Chadd 			 * Use the last buffer in an aggregate as that
37516edf1dc7SAdrian Chadd 			 * is where the hardware may be - intermediate
37526edf1dc7SAdrian Chadd 			 * descriptors won't be "busy".
375310ad9a77SSam Leffler 			 */
37546edf1dc7SAdrian Chadd 			bf->bf_last->bf_flags |= ATH_BUF_BUSY;
375510ad9a77SSam Leffler 		} else
375610ad9a77SSam Leffler #else
3757ebecf802SSam Leffler 		if (txq->axq_depth == 0)
375810ad9a77SSam Leffler #endif
37591539af1eSSam Leffler 			txq->axq_link = NULL;
37606edf1dc7SAdrian Chadd 		if (bf->bf_state.bfs_aggr)
37616edf1dc7SAdrian Chadd 			txq->axq_aggr_depth--;
37625591b213SSam Leffler 
37635591b213SSam Leffler 		ni = bf->bf_node;
376403682514SAdrian Chadd 
376503682514SAdrian Chadd 		ATH_KTR(sc, ATH_KTR_TXCOMP, 5,
376603682514SAdrian Chadd 		    "ath_tx_processq: txq=%u, bf=%p, ds=%p, ni=%p, ts_status=0x%08x",
376703682514SAdrian Chadd 		    txq->axq_qnum, bf, ds, ni, ts->ts_status);
3768c42a7b7eSSam Leffler 		/*
37699352fb7aSAdrian Chadd 		 * If unicast frame was ack'd update RSSI,
377084784be1SSam Leffler 		 * including the last rx time used to
377184784be1SSam Leffler 		 * workaround phantom bmiss interrupts.
3772d7736e13SSam Leffler 		 */
37739352fb7aSAdrian Chadd 		if (ni != NULL && ts->ts_status == 0 &&
3774875a9451SAdrian Chadd 		    ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0)) {
3775d7736e13SSam Leffler 			nacked++;
377684784be1SSam Leffler 			sc->sc_stats.ast_tx_rssi = ts->ts_rssi;
377784784be1SSam Leffler 			ATH_RSSI_LPF(sc->sc_halstats.ns_avgtxrssi,
377884784be1SSam Leffler 				ts->ts_rssi);
377984784be1SSam Leffler 		}
37809352fb7aSAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
37819352fb7aSAdrian Chadd 
3782bad98824SAdrian Chadd 		/*
3783bad98824SAdrian Chadd 		 * Update statistics and call completion
3784bad98824SAdrian Chadd 		 */
3785bad98824SAdrian Chadd 		ath_tx_process_buf_completion(sc, txq, ts, bf);
3786*548a605dSAdrian Chadd 
3787*548a605dSAdrian Chadd 		/* XXX at this point, bf and ni may be totally invalid */
37885591b213SSam Leffler 	}
3789339ccfb3SSam Leffler #ifdef IEEE80211_SUPPORT_SUPERG
379068e8e04eSSam Leffler 	/*
379168e8e04eSSam Leffler 	 * Flush fast-frame staging queue when traffic slows.
379268e8e04eSSam Leffler 	 */
379368e8e04eSSam Leffler 	if (txq->axq_depth <= 1)
379404f19fd6SSam Leffler 		ieee80211_ff_flush(ic, txq->axq_ac);
3795339ccfb3SSam Leffler #endif
3796eb6f0de0SAdrian Chadd 
3797eb6f0de0SAdrian Chadd 	/* Kick the TXQ scheduler */
3798eb6f0de0SAdrian Chadd 	if (dosched) {
3799eb6f0de0SAdrian Chadd 		ATH_TXQ_LOCK(txq);
3800eb6f0de0SAdrian Chadd 		ath_txq_sched(sc, txq);
3801eb6f0de0SAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
3802eb6f0de0SAdrian Chadd 	}
3803eb6f0de0SAdrian Chadd 
380403682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
380503682514SAdrian Chadd 	    "ath_tx_processq: txq=%u: done",
380603682514SAdrian Chadd 	    txq->axq_qnum);
380703682514SAdrian Chadd 
3808d7736e13SSam Leffler 	return nacked;
3809d7736e13SSam Leffler }
3810d7736e13SSam Leffler 
38118f939e79SAdrian Chadd #define	TXQACTIVE(t, q)		( (t) & (1 << (q)))
3812c42a7b7eSSam Leffler 
3813c42a7b7eSSam Leffler /*
3814c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
3815c42a7b7eSSam Leffler  * for a single hardware transmit queue (e.g. 5210 and 5211).
3816c42a7b7eSSam Leffler  */
3817c42a7b7eSSam Leffler static void
3818c42a7b7eSSam Leffler ath_tx_proc_q0(void *arg, int npending)
3819c42a7b7eSSam Leffler {
3820c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
3821fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
38228f939e79SAdrian Chadd 	uint32_t txqs;
3823c42a7b7eSSam Leffler 
3824ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
3825ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
38268f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
38278f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
3828ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
38298f939e79SAdrian Chadd 
383003682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
383103682514SAdrian Chadd 	    "ath_tx_proc_q0: txqs=0x%08x", txqs);
383203682514SAdrian Chadd 
383396ff485dSAdrian Chadd 	if (TXQACTIVE(txqs, 0) && ath_tx_processq(sc, &sc->sc_txq[0], 1))
38348f939e79SAdrian Chadd 		/* XXX why is lastrx updated in tx code? */
3835d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
38368f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum))
383796ff485dSAdrian Chadd 		ath_tx_processq(sc, sc->sc_cabq, 1);
3838e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
383913f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3840e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
38412e986da5SSam Leffler 	sc->sc_wd_timer = 0;
38425591b213SSam Leffler 
38433e50ec2cSSam Leffler 	if (sc->sc_softled)
384446d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
38453e50ec2cSSam Leffler 
3846ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
3847ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
3848ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
3849ef27340cSAdrian Chadd 
385014d33c7eSAdrian Chadd 	ath_tx_kick(sc);
38515591b213SSam Leffler }
38525591b213SSam Leffler 
38535591b213SSam Leffler /*
3854c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt; special-cased
3855c42a7b7eSSam Leffler  * for four hardware queues, 0-3 (e.g. 5212 w/ WME support).
38565591b213SSam Leffler  */
38575591b213SSam Leffler static void
3858c42a7b7eSSam Leffler ath_tx_proc_q0123(void *arg, int npending)
3859c42a7b7eSSam Leffler {
3860c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
3861fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
3862d7736e13SSam Leffler 	int nacked;
38638f939e79SAdrian Chadd 	uint32_t txqs;
38648f939e79SAdrian Chadd 
3865ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
3866ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
38678f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
38688f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
3869ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
3870c42a7b7eSSam Leffler 
387103682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
387203682514SAdrian Chadd 	    "ath_tx_proc_q0123: txqs=0x%08x", txqs);
387303682514SAdrian Chadd 
3874c42a7b7eSSam Leffler 	/*
3875c42a7b7eSSam Leffler 	 * Process each active queue.
3876c42a7b7eSSam Leffler 	 */
3877d7736e13SSam Leffler 	nacked = 0;
38788f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 0))
387996ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[0], 1);
38808f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 1))
388196ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[1], 1);
38828f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 2))
388396ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[2], 1);
38848f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, 3))
388596ff485dSAdrian Chadd 		nacked += ath_tx_processq(sc, &sc->sc_txq[3], 1);
38868f939e79SAdrian Chadd 	if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum))
388796ff485dSAdrian Chadd 		ath_tx_processq(sc, sc->sc_cabq, 1);
3888d7736e13SSam Leffler 	if (nacked)
3889d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
3890c42a7b7eSSam Leffler 
3891e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
389213f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3893e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
38942e986da5SSam Leffler 	sc->sc_wd_timer = 0;
3895c42a7b7eSSam Leffler 
38963e50ec2cSSam Leffler 	if (sc->sc_softled)
389746d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
38983e50ec2cSSam Leffler 
3899ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
3900ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
3901ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
3902ef27340cSAdrian Chadd 
390314d33c7eSAdrian Chadd 	ath_tx_kick(sc);
3904c42a7b7eSSam Leffler }
3905c42a7b7eSSam Leffler 
3906c42a7b7eSSam Leffler /*
3907c42a7b7eSSam Leffler  * Deferred processing of transmit interrupt.
3908c42a7b7eSSam Leffler  */
3909c42a7b7eSSam Leffler static void
3910c42a7b7eSSam Leffler ath_tx_proc(void *arg, int npending)
3911c42a7b7eSSam Leffler {
3912c42a7b7eSSam Leffler 	struct ath_softc *sc = arg;
3913fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
3914d7736e13SSam Leffler 	int i, nacked;
39158f939e79SAdrian Chadd 	uint32_t txqs;
39168f939e79SAdrian Chadd 
3917ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
3918ef27340cSAdrian Chadd 	sc->sc_txproc_cnt++;
39198f939e79SAdrian Chadd 	txqs = sc->sc_txq_active;
39208f939e79SAdrian Chadd 	sc->sc_txq_active &= ~txqs;
3921ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
3922c42a7b7eSSam Leffler 
392303682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1, "ath_tx_proc: txqs=0x%08x", txqs);
392403682514SAdrian Chadd 
3925c42a7b7eSSam Leffler 	/*
3926c42a7b7eSSam Leffler 	 * Process each active queue.
3927c42a7b7eSSam Leffler 	 */
3928d7736e13SSam Leffler 	nacked = 0;
3929c42a7b7eSSam Leffler 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
39308f939e79SAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i) && TXQACTIVE(txqs, i))
393196ff485dSAdrian Chadd 			nacked += ath_tx_processq(sc, &sc->sc_txq[i], 1);
3932d7736e13SSam Leffler 	if (nacked)
3933d7736e13SSam Leffler 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
3934c42a7b7eSSam Leffler 
3935ef27340cSAdrian Chadd 	/* XXX check this inside of IF_LOCK? */
3936e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
393713f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3938e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
39392e986da5SSam Leffler 	sc->sc_wd_timer = 0;
3940c42a7b7eSSam Leffler 
39413e50ec2cSSam Leffler 	if (sc->sc_softled)
394246d4d74cSSam Leffler 		ath_led_event(sc, sc->sc_txrix);
39433e50ec2cSSam Leffler 
3944ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
3945ef27340cSAdrian Chadd 	sc->sc_txproc_cnt--;
3946ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
3947ef27340cSAdrian Chadd 
394814d33c7eSAdrian Chadd 	ath_tx_kick(sc);
3949c42a7b7eSSam Leffler }
395016d4de92SAdrian Chadd #undef	TXQACTIVE
3951c42a7b7eSSam Leffler 
39529352fb7aSAdrian Chadd /*
395303e9308fSAdrian Chadd  * Deferred processing of TXQ rescheduling.
395403e9308fSAdrian Chadd  */
395503e9308fSAdrian Chadd static void
395603e9308fSAdrian Chadd ath_txq_sched_tasklet(void *arg, int npending)
395703e9308fSAdrian Chadd {
395803e9308fSAdrian Chadd 	struct ath_softc *sc = arg;
395903e9308fSAdrian Chadd 	int i;
396003e9308fSAdrian Chadd 
396103e9308fSAdrian Chadd 	/* XXX is skipping ok? */
396203e9308fSAdrian Chadd 	ATH_PCU_LOCK(sc);
396303e9308fSAdrian Chadd #if 0
396403e9308fSAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
396503e9308fSAdrian Chadd 		device_printf(sc->sc_dev,
396603e9308fSAdrian Chadd 		    "%s: sc_inreset_cnt > 0; skipping\n", __func__);
396703e9308fSAdrian Chadd 		ATH_PCU_UNLOCK(sc);
396803e9308fSAdrian Chadd 		return;
396903e9308fSAdrian Chadd 	}
397003e9308fSAdrian Chadd #endif
397103e9308fSAdrian Chadd 	sc->sc_txproc_cnt++;
397203e9308fSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
397303e9308fSAdrian Chadd 
397403e9308fSAdrian Chadd 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
3975b5a9dfd5SAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i)) {
3976b5a9dfd5SAdrian Chadd 			ATH_TXQ_LOCK(&sc->sc_txq[i]);
397703e9308fSAdrian Chadd 			ath_txq_sched(sc, &sc->sc_txq[i]);
3978b5a9dfd5SAdrian Chadd 			ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
3979b5a9dfd5SAdrian Chadd 		}
398003e9308fSAdrian Chadd 	}
398103e9308fSAdrian Chadd 
398203e9308fSAdrian Chadd 	ATH_PCU_LOCK(sc);
398303e9308fSAdrian Chadd 	sc->sc_txproc_cnt--;
398403e9308fSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
398503e9308fSAdrian Chadd }
398603e9308fSAdrian Chadd 
3987e1a50456SAdrian Chadd void
3988e1a50456SAdrian Chadd ath_returnbuf_tail(struct ath_softc *sc, struct ath_buf *bf)
3989e1a50456SAdrian Chadd {
3990e1a50456SAdrian Chadd 
3991e1a50456SAdrian Chadd 	ATH_TXBUF_LOCK_ASSERT(sc);
3992e1a50456SAdrian Chadd 
3993af33d486SAdrian Chadd 	if (bf->bf_flags & ATH_BUF_MGMT)
3994af33d486SAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_txbuf_mgmt, bf, bf_list);
399523ced6c1SAdrian Chadd 	else {
3996e1a50456SAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
399723ced6c1SAdrian Chadd 		sc->sc_txbuf_cnt++;
399823ced6c1SAdrian Chadd 		if (sc->sc_txbuf_cnt > ath_txbuf) {
399923ced6c1SAdrian Chadd 			device_printf(sc->sc_dev,
400023ced6c1SAdrian Chadd 			    "%s: sc_txbuf_cnt > %d?\n",
400123ced6c1SAdrian Chadd 			    __func__,
400223ced6c1SAdrian Chadd 			    ath_txbuf);
400323ced6c1SAdrian Chadd 			sc->sc_txbuf_cnt = ath_txbuf;
400423ced6c1SAdrian Chadd 		}
400523ced6c1SAdrian Chadd 	}
4006e1a50456SAdrian Chadd }
4007e1a50456SAdrian Chadd 
4008e1a50456SAdrian Chadd void
4009e1a50456SAdrian Chadd ath_returnbuf_head(struct ath_softc *sc, struct ath_buf *bf)
4010e1a50456SAdrian Chadd {
4011e1a50456SAdrian Chadd 
4012e1a50456SAdrian Chadd 	ATH_TXBUF_LOCK_ASSERT(sc);
4013e1a50456SAdrian Chadd 
4014af33d486SAdrian Chadd 	if (bf->bf_flags & ATH_BUF_MGMT)
4015af33d486SAdrian Chadd 		TAILQ_INSERT_HEAD(&sc->sc_txbuf_mgmt, bf, bf_list);
401623ced6c1SAdrian Chadd 	else {
4017e1a50456SAdrian Chadd 		TAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
401823ced6c1SAdrian Chadd 		sc->sc_txbuf_cnt++;
401923ced6c1SAdrian Chadd 		if (sc->sc_txbuf_cnt > ATH_TXBUF) {
402023ced6c1SAdrian Chadd 			device_printf(sc->sc_dev,
402123ced6c1SAdrian Chadd 			    "%s: sc_txbuf_cnt > %d?\n",
402223ced6c1SAdrian Chadd 			    __func__,
402323ced6c1SAdrian Chadd 			    ATH_TXBUF);
402423ced6c1SAdrian Chadd 			sc->sc_txbuf_cnt = ATH_TXBUF;
402523ced6c1SAdrian Chadd 		}
402623ced6c1SAdrian Chadd 	}
4027e1a50456SAdrian Chadd }
4028e1a50456SAdrian Chadd 
402903e9308fSAdrian Chadd /*
40309352fb7aSAdrian Chadd  * Return a buffer to the pool and update the 'busy' flag on the
40319352fb7aSAdrian Chadd  * previous 'tail' entry.
40329352fb7aSAdrian Chadd  *
40339352fb7aSAdrian Chadd  * This _must_ only be called when the buffer is involved in a completed
40349352fb7aSAdrian Chadd  * TX. The logic is that if it was part of an active TX, the previous
40359352fb7aSAdrian Chadd  * buffer on the list is now not involved in a halted TX DMA queue, waiting
40369352fb7aSAdrian Chadd  * for restart (eg for TDMA.)
40379352fb7aSAdrian Chadd  *
40389352fb7aSAdrian Chadd  * The caller must free the mbuf and recycle the node reference.
40399352fb7aSAdrian Chadd  */
40409352fb7aSAdrian Chadd void
40419352fb7aSAdrian Chadd ath_freebuf(struct ath_softc *sc, struct ath_buf *bf)
40429352fb7aSAdrian Chadd {
40439352fb7aSAdrian Chadd 	bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
40449352fb7aSAdrian Chadd 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_POSTWRITE);
40459352fb7aSAdrian Chadd 
40469352fb7aSAdrian Chadd 	KASSERT((bf->bf_node == NULL), ("%s: bf->bf_node != NULL\n", __func__));
40479352fb7aSAdrian Chadd 	KASSERT((bf->bf_m == NULL), ("%s: bf->bf_m != NULL\n", __func__));
40489352fb7aSAdrian Chadd 
40499352fb7aSAdrian Chadd 	ATH_TXBUF_LOCK(sc);
40509352fb7aSAdrian Chadd 	ath_tx_update_busy(sc);
4051e1a50456SAdrian Chadd 	ath_returnbuf_tail(sc, bf);
40529352fb7aSAdrian Chadd 	ATH_TXBUF_UNLOCK(sc);
40539352fb7aSAdrian Chadd }
40549352fb7aSAdrian Chadd 
40559352fb7aSAdrian Chadd /*
40569352fb7aSAdrian Chadd  * This is currently used by ath_tx_draintxq() and
40579352fb7aSAdrian Chadd  * ath_tx_tid_free_pkts().
40589352fb7aSAdrian Chadd  *
40599352fb7aSAdrian Chadd  * It recycles a single ath_buf.
40609352fb7aSAdrian Chadd  */
40619352fb7aSAdrian Chadd void
40629352fb7aSAdrian Chadd ath_tx_freebuf(struct ath_softc *sc, struct ath_buf *bf, int status)
40639352fb7aSAdrian Chadd {
40649352fb7aSAdrian Chadd 	struct ieee80211_node *ni = bf->bf_node;
40659352fb7aSAdrian Chadd 	struct mbuf *m0 = bf->bf_m;
40669352fb7aSAdrian Chadd 
40679352fb7aSAdrian Chadd 	bf->bf_node = NULL;
40689352fb7aSAdrian Chadd 	bf->bf_m = NULL;
40699352fb7aSAdrian Chadd 
40709352fb7aSAdrian Chadd 	/* Free the buffer, it's not needed any longer */
40719352fb7aSAdrian Chadd 	ath_freebuf(sc, bf);
40729352fb7aSAdrian Chadd 
40739352fb7aSAdrian Chadd 	if (ni != NULL) {
40749352fb7aSAdrian Chadd 		/*
40759352fb7aSAdrian Chadd 		 * Do any callback and reclaim the node reference.
40769352fb7aSAdrian Chadd 		 */
40779352fb7aSAdrian Chadd 		if (m0->m_flags & M_TXCB)
40789352fb7aSAdrian Chadd 			ieee80211_process_callback(ni, m0, status);
40799352fb7aSAdrian Chadd 		ieee80211_free_node(ni);
40809352fb7aSAdrian Chadd 	}
40819352fb7aSAdrian Chadd 	m_freem(m0);
40829352fb7aSAdrian Chadd 
40839352fb7aSAdrian Chadd 	/*
40849352fb7aSAdrian Chadd 	 * XXX the buffer used to be freed -after-, but the DMA map was
40859352fb7aSAdrian Chadd 	 * freed where ath_freebuf() now is. I've no idea what this
40869352fb7aSAdrian Chadd 	 * will do.
40879352fb7aSAdrian Chadd 	 */
40889352fb7aSAdrian Chadd }
40899352fb7aSAdrian Chadd 
40909352fb7aSAdrian Chadd void
40911762ec94SAdrian Chadd ath_tx_draintxq(struct ath_softc *sc, struct ath_txq *txq)
40925591b213SSam Leffler {
4093a585a9a1SSam Leffler #ifdef ATH_DEBUG
40945591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4095d2f6ed15SSam Leffler #endif
40965591b213SSam Leffler 	struct ath_buf *bf;
40977a4c5ed9SSam Leffler 	u_int ix;
40985591b213SSam Leffler 
4099c42a7b7eSSam Leffler 	/*
4100c42a7b7eSSam Leffler 	 * NB: this assumes output has been stopped and
41015d61b5e8SSam Leffler 	 *     we do not need to block ath_tx_proc
4102c42a7b7eSSam Leffler 	 */
410310ad9a77SSam Leffler 	ATH_TXBUF_LOCK(sc);
41046b349e5aSAdrian Chadd 	bf = TAILQ_LAST(&sc->sc_txbuf, ath_bufhead_s);
410510ad9a77SSam Leffler 	if (bf != NULL)
410610ad9a77SSam Leffler 		bf->bf_flags &= ~ATH_BUF_BUSY;
4107af33d486SAdrian Chadd 	bf = TAILQ_LAST(&sc->sc_txbuf_mgmt, ath_bufhead_s);
4108af33d486SAdrian Chadd 	if (bf != NULL)
4109af33d486SAdrian Chadd 		bf->bf_flags &= ~ATH_BUF_BUSY;
411010ad9a77SSam Leffler 	ATH_TXBUF_UNLOCK(sc);
41119352fb7aSAdrian Chadd 
41127a4c5ed9SSam Leffler 	for (ix = 0;; ix++) {
4113c42a7b7eSSam Leffler 		ATH_TXQ_LOCK(txq);
41146b349e5aSAdrian Chadd 		bf = TAILQ_FIRST(&txq->axq_q);
41155591b213SSam Leffler 		if (bf == NULL) {
4116ebecf802SSam Leffler 			txq->axq_link = NULL;
41171762ec94SAdrian Chadd 			/*
41181762ec94SAdrian Chadd 			 * There's currently no flag that indicates
41191762ec94SAdrian Chadd 			 * a buffer is on the FIFO.  So until that
41201762ec94SAdrian Chadd 			 * occurs, just clear the FIFO counter here.
41211762ec94SAdrian Chadd 			 *
41221762ec94SAdrian Chadd 			 * Yes, this means that if something in parallel
41231762ec94SAdrian Chadd 			 * is pushing things onto this TXQ and pushing
41241762ec94SAdrian Chadd 			 * _that_ into the hardware, things will get
41251762ec94SAdrian Chadd 			 * very fruity very quickly.
41261762ec94SAdrian Chadd 			 */
41271762ec94SAdrian Chadd 			txq->axq_fifo_depth = 0;
4128c42a7b7eSSam Leffler 			ATH_TXQ_UNLOCK(txq);
41295591b213SSam Leffler 			break;
41305591b213SSam Leffler 		}
41316b349e5aSAdrian Chadd 		ATH_TXQ_REMOVE(txq, bf, bf_list);
41326edf1dc7SAdrian Chadd 		if (bf->bf_state.bfs_aggr)
41336edf1dc7SAdrian Chadd 			txq->axq_aggr_depth--;
4134a585a9a1SSam Leffler #ifdef ATH_DEBUG
41354a3ac3fcSSam Leffler 		if (sc->sc_debug & ATH_DEBUG_RESET) {
4136b032f27cSSam Leffler 			struct ieee80211com *ic = sc->sc_ifp->if_l2com;
41371762ec94SAdrian Chadd 			int status = 0;
4138b032f27cSSam Leffler 
41391762ec94SAdrian Chadd 			/*
41401762ec94SAdrian Chadd 			 * EDMA operation has a TX completion FIFO
41411762ec94SAdrian Chadd 			 * separate from the TX descriptor, so this
41421762ec94SAdrian Chadd 			 * method of checking the "completion" status
41431762ec94SAdrian Chadd 			 * is wrong.
41441762ec94SAdrian Chadd 			 */
41451762ec94SAdrian Chadd 			if (! sc->sc_isedma) {
41461762ec94SAdrian Chadd 				status = (ath_hal_txprocdesc(ah,
41471762ec94SAdrian Chadd 				    bf->bf_lastds,
414865f9edeeSSam Leffler 				    &bf->bf_status.ds_txstat) == HAL_OK);
41491762ec94SAdrian Chadd 			}
41501762ec94SAdrian Chadd 			ath_printtxbuf(sc, bf, txq->axq_qnum, ix, status);
4151e40b6ab1SSam Leffler 			ieee80211_dump_pkt(ic, mtod(bf->bf_m, const uint8_t *),
41524a3ac3fcSSam Leffler 			    bf->bf_m->m_len, 0, -1);
41534a3ac3fcSSam Leffler 		}
4154a585a9a1SSam Leffler #endif /* ATH_DEBUG */
415523428eafSSam Leffler 		/*
41569352fb7aSAdrian Chadd 		 * Since we're now doing magic in the completion
41579352fb7aSAdrian Chadd 		 * functions, we -must- call it for aggregation
41589352fb7aSAdrian Chadd 		 * destinations or BAW tracking will get upset.
415923428eafSSam Leffler 		 */
41609352fb7aSAdrian Chadd 		/*
41619352fb7aSAdrian Chadd 		 * Clear ATH_BUF_BUSY; the completion handler
41629352fb7aSAdrian Chadd 		 * will free the buffer.
41639352fb7aSAdrian Chadd 		 */
41649352fb7aSAdrian Chadd 		ATH_TXQ_UNLOCK(txq);
416510ad9a77SSam Leffler 		bf->bf_flags &= ~ATH_BUF_BUSY;
41669352fb7aSAdrian Chadd 		if (bf->bf_comp)
41679352fb7aSAdrian Chadd 			bf->bf_comp(sc, bf, 1);
41689352fb7aSAdrian Chadd 		else
41699352fb7aSAdrian Chadd 			ath_tx_default_comp(sc, bf, 1);
41705591b213SSam Leffler 	}
41719352fb7aSAdrian Chadd 
4172eb6f0de0SAdrian Chadd 	/*
4173eb6f0de0SAdrian Chadd 	 * Drain software queued frames which are on
4174eb6f0de0SAdrian Chadd 	 * active TIDs.
4175eb6f0de0SAdrian Chadd 	 */
4176eb6f0de0SAdrian Chadd 	ath_tx_txq_drain(sc, txq);
4177c42a7b7eSSam Leffler }
4178c42a7b7eSSam Leffler 
4179c42a7b7eSSam Leffler static void
4180c42a7b7eSSam Leffler ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
4181c42a7b7eSSam Leffler {
4182c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4183c42a7b7eSSam Leffler 
4184c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n",
4185c42a7b7eSSam Leffler 	    __func__, txq->axq_qnum,
41866891c875SPeter Wemm 	    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, txq->axq_qnum),
41876891c875SPeter Wemm 	    txq->axq_link);
41884a3ac3fcSSam Leffler 	(void) ath_hal_stoptxdma(ah, txq->axq_qnum);
4189c42a7b7eSSam Leffler }
4190c42a7b7eSSam Leffler 
4191bad98824SAdrian Chadd int
41922d433424SAdrian Chadd ath_stoptxdma(struct ath_softc *sc)
4193c42a7b7eSSam Leffler {
4194c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4195c42a7b7eSSam Leffler 	int i;
4196c42a7b7eSSam Leffler 
4197c42a7b7eSSam Leffler 	/* XXX return value */
41982d433424SAdrian Chadd 	if (sc->sc_invalid)
41992d433424SAdrian Chadd 		return 0;
42002d433424SAdrian Chadd 
4201c42a7b7eSSam Leffler 	if (!sc->sc_invalid) {
4202c42a7b7eSSam Leffler 		/* don't touch the hardware if marked invalid */
42034a3ac3fcSSam Leffler 		DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n",
42044a3ac3fcSSam Leffler 		    __func__, sc->sc_bhalq,
42054a3ac3fcSSam Leffler 		    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq),
42064a3ac3fcSSam Leffler 		    NULL);
4207c42a7b7eSSam Leffler 		(void) ath_hal_stoptxdma(ah, sc->sc_bhalq);
4208c42a7b7eSSam Leffler 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
4209c42a7b7eSSam Leffler 			if (ATH_TXQ_SETUP(sc, i))
4210c42a7b7eSSam Leffler 				ath_tx_stopdma(sc, &sc->sc_txq[i]);
4211c42a7b7eSSam Leffler 	}
42122d433424SAdrian Chadd 
42132d433424SAdrian Chadd 	return 1;
42142d433424SAdrian Chadd }
42152d433424SAdrian Chadd 
42162d433424SAdrian Chadd /*
42172d433424SAdrian Chadd  * Drain the transmit queues and reclaim resources.
42182d433424SAdrian Chadd  */
4219788e6aa9SAdrian Chadd void
4220788e6aa9SAdrian Chadd ath_legacy_tx_drain(struct ath_softc *sc, ATH_RESET_TYPE reset_type)
42212d433424SAdrian Chadd {
42222d433424SAdrian Chadd #ifdef	ATH_DEBUG
42232d433424SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
42242d433424SAdrian Chadd #endif
42252d433424SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
42262d433424SAdrian Chadd 	int i;
42272d433424SAdrian Chadd 
42282d433424SAdrian Chadd 	(void) ath_stoptxdma(sc);
42292d433424SAdrian Chadd 
4230ef27340cSAdrian Chadd 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
4231ef27340cSAdrian Chadd 		/*
4232ef27340cSAdrian Chadd 		 * XXX TODO: should we just handle the completed TX frames
4233ef27340cSAdrian Chadd 		 * here, whether or not the reset is a full one or not?
4234ef27340cSAdrian Chadd 		 */
4235ef27340cSAdrian Chadd 		if (ATH_TXQ_SETUP(sc, i)) {
4236ef27340cSAdrian Chadd 			if (reset_type == ATH_RESET_NOLOSS)
4237ef27340cSAdrian Chadd 				ath_tx_processq(sc, &sc->sc_txq[i], 0);
4238ef27340cSAdrian Chadd 			else
4239c42a7b7eSSam Leffler 				ath_tx_draintxq(sc, &sc->sc_txq[i]);
4240ef27340cSAdrian Chadd 		}
4241ef27340cSAdrian Chadd 	}
42424a3ac3fcSSam Leffler #ifdef ATH_DEBUG
42434a3ac3fcSSam Leffler 	if (sc->sc_debug & ATH_DEBUG_RESET) {
42446b349e5aSAdrian Chadd 		struct ath_buf *bf = TAILQ_FIRST(&sc->sc_bbuf);
42454a3ac3fcSSam Leffler 		if (bf != NULL && bf->bf_m != NULL) {
42466902009eSSam Leffler 			ath_printtxbuf(sc, bf, sc->sc_bhalq, 0,
42476edf1dc7SAdrian Chadd 				ath_hal_txprocdesc(ah, bf->bf_lastds,
424865f9edeeSSam Leffler 				    &bf->bf_status.ds_txstat) == HAL_OK);
4249e40b6ab1SSam Leffler 			ieee80211_dump_pkt(ifp->if_l2com,
4250e40b6ab1SSam Leffler 			    mtod(bf->bf_m, const uint8_t *), bf->bf_m->m_len,
4251e40b6ab1SSam Leffler 			    0, -1);
42524a3ac3fcSSam Leffler 		}
42534a3ac3fcSSam Leffler 	}
42544a3ac3fcSSam Leffler #endif /* ATH_DEBUG */
4255e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
425613f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4257e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
42582e986da5SSam Leffler 	sc->sc_wd_timer = 0;
42595591b213SSam Leffler }
42605591b213SSam Leffler 
42615591b213SSam Leffler /*
4262c42a7b7eSSam Leffler  * Update internal state after a channel change.
4263c42a7b7eSSam Leffler  */
4264c42a7b7eSSam Leffler static void
4265c42a7b7eSSam Leffler ath_chan_change(struct ath_softc *sc, struct ieee80211_channel *chan)
4266c42a7b7eSSam Leffler {
4267c42a7b7eSSam Leffler 	enum ieee80211_phymode mode;
4268c42a7b7eSSam Leffler 
4269c42a7b7eSSam Leffler 	/*
4270c42a7b7eSSam Leffler 	 * Change channels and update the h/w rate map
4271c42a7b7eSSam Leffler 	 * if we're switching; e.g. 11a to 11b/g.
4272c42a7b7eSSam Leffler 	 */
427368e8e04eSSam Leffler 	mode = ieee80211_chan2mode(chan);
4274c42a7b7eSSam Leffler 	if (mode != sc->sc_curmode)
4275c42a7b7eSSam Leffler 		ath_setcurmode(sc, mode);
427659efa8b5SSam Leffler 	sc->sc_curchan = chan;
4277c42a7b7eSSam Leffler }
4278c42a7b7eSSam Leffler 
4279c42a7b7eSSam Leffler /*
42805591b213SSam Leffler  * Set/change channels.  If the channel is really being changed,
42814fa8d4efSDaniel Eischen  * it's done by resetting the chip.  To accomplish this we must
42825591b213SSam Leffler  * first cleanup any pending DMA, then restart stuff after a la
42835591b213SSam Leffler  * ath_init.
42845591b213SSam Leffler  */
42855591b213SSam Leffler static int
42865591b213SSam Leffler ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan)
42875591b213SSam Leffler {
4288b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
4289b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
42905591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4291ef27340cSAdrian Chadd 	int ret = 0;
4292ef27340cSAdrian Chadd 
4293ef27340cSAdrian Chadd 	/* Treat this as an interface reset */
4294d52f7132SAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
4295d52f7132SAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
4296d52f7132SAdrian Chadd 
4297d52f7132SAdrian Chadd 	/* (Try to) stop TX/RX from occuring */
4298d52f7132SAdrian Chadd 	taskqueue_block(sc->sc_tq);
4299d52f7132SAdrian Chadd 
4300ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4301e78719adSAdrian Chadd 	ath_hal_intrset(ah, 0);		/* Stop new RX/TX completion */
4302e78719adSAdrian Chadd 	ath_txrx_stop_locked(sc);	/* Stop pending RX/TX completion */
4303ee321975SAdrian Chadd 	if (ath_reset_grablock(sc, 1) == 0) {
4304ee321975SAdrian Chadd 		device_printf(sc->sc_dev, "%s: concurrent reset! Danger!\n",
4305ef27340cSAdrian Chadd 		    __func__);
4306ee321975SAdrian Chadd 	}
4307ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
4308c42a7b7eSSam Leffler 
430959efa8b5SSam Leffler 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %u (%u MHz, flags 0x%x)\n",
431059efa8b5SSam Leffler 	    __func__, ieee80211_chan2ieee(ic, chan),
431159efa8b5SSam Leffler 	    chan->ic_freq, chan->ic_flags);
431259efa8b5SSam Leffler 	if (chan != sc->sc_curchan) {
4313c42a7b7eSSam Leffler 		HAL_STATUS status;
43145591b213SSam Leffler 		/*
43155591b213SSam Leffler 		 * To switch channels clear any pending DMA operations;
43165591b213SSam Leffler 		 * wait long enough for the RX fifo to drain, reset the
43175591b213SSam Leffler 		 * hardware at the new frequency, and then re-enable
43185591b213SSam Leffler 		 * the relevant bits of the h/w.
43195591b213SSam Leffler 		 */
4320ef27340cSAdrian Chadd #if 0
43215591b213SSam Leffler 		ath_hal_intrset(ah, 0);		/* disable interrupts */
4322ef27340cSAdrian Chadd #endif
43239a842e8bSAdrian Chadd 		ath_stoprecv(sc, 1);		/* turn off frame recv */
43249a842e8bSAdrian Chadd 		/*
43259a842e8bSAdrian Chadd 		 * First, handle completed TX/RX frames.
43269a842e8bSAdrian Chadd 		 */
4327f8cc9b09SAdrian Chadd 		ath_rx_flush(sc);
43289a842e8bSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_NOLOSS);
43299a842e8bSAdrian Chadd 		/*
43309a842e8bSAdrian Chadd 		 * Next, flush the non-scheduled frames.
43319a842e8bSAdrian Chadd 		 */
4332517526efSAdrian Chadd 		ath_draintxq(sc, ATH_RESET_FULL);	/* clear pending tx frames */
43339a842e8bSAdrian Chadd 
433459efa8b5SSam Leffler 		if (!ath_hal_reset(ah, sc->sc_opmode, chan, AH_TRUE, &status)) {
4335b032f27cSSam Leffler 			if_printf(ifp, "%s: unable to reset "
433679649302SGavin Atkinson 			    "channel %u (%u MHz, flags 0x%x), hal status %u\n",
433759efa8b5SSam Leffler 			    __func__, ieee80211_chan2ieee(ic, chan),
433859efa8b5SSam Leffler 			    chan->ic_freq, chan->ic_flags, status);
4339ef27340cSAdrian Chadd 			ret = EIO;
4340ef27340cSAdrian Chadd 			goto finish;
43415591b213SSam Leffler 		}
4342c59005e9SSam Leffler 		sc->sc_diversity = ath_hal_getdiversity(ah);
4343c42a7b7eSSam Leffler 
434448237774SAdrian Chadd 		/* Let DFS at it in case it's a DFS channel */
4345398bca2eSAdrian Chadd 		ath_dfs_radar_enable(sc, chan);
434648237774SAdrian Chadd 
43475591b213SSam Leffler 		/*
43485591b213SSam Leffler 		 * Re-enable rx framework.
43495591b213SSam Leffler 		 */
43505591b213SSam Leffler 		if (ath_startrecv(sc) != 0) {
4351b032f27cSSam Leffler 			if_printf(ifp, "%s: unable to restart recv logic\n",
4352b032f27cSSam Leffler 			    __func__);
4353ef27340cSAdrian Chadd 			ret = EIO;
4354ef27340cSAdrian Chadd 			goto finish;
43555591b213SSam Leffler 		}
43565591b213SSam Leffler 
43575591b213SSam Leffler 		/*
43585591b213SSam Leffler 		 * Change channels and update the h/w rate map
43595591b213SSam Leffler 		 * if we're switching; e.g. 11a to 11b/g.
43605591b213SSam Leffler 		 */
4361c42a7b7eSSam Leffler 		ath_chan_change(sc, chan);
43620a915fadSSam Leffler 
43630a915fadSSam Leffler 		/*
43642fd9aabbSAdrian Chadd 		 * Reset clears the beacon timers; reset them
43652fd9aabbSAdrian Chadd 		 * here if needed.
43662fd9aabbSAdrian Chadd 		 */
43672fd9aabbSAdrian Chadd 		if (sc->sc_beacons) {		/* restart beacons */
43682fd9aabbSAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
43692fd9aabbSAdrian Chadd 			if (sc->sc_tdma)
43702fd9aabbSAdrian Chadd 				ath_tdma_config(sc, NULL);
43712fd9aabbSAdrian Chadd 			else
43722fd9aabbSAdrian Chadd #endif
43732fd9aabbSAdrian Chadd 			ath_beacon_config(sc, NULL);
43742fd9aabbSAdrian Chadd 		}
43752fd9aabbSAdrian Chadd 
43762fd9aabbSAdrian Chadd 		/*
43770a915fadSSam Leffler 		 * Re-enable interrupts.
43780a915fadSSam Leffler 		 */
4379e78719adSAdrian Chadd #if 0
43800a915fadSSam Leffler 		ath_hal_intrset(ah, sc->sc_imask);
4381ef27340cSAdrian Chadd #endif
43825591b213SSam Leffler 	}
4383ef27340cSAdrian Chadd 
4384ef27340cSAdrian Chadd finish:
4385ef27340cSAdrian Chadd 	ATH_PCU_LOCK(sc);
4386ef27340cSAdrian Chadd 	sc->sc_inreset_cnt--;
4387ef27340cSAdrian Chadd 	/* XXX only do this if sc_inreset_cnt == 0? */
4388ef27340cSAdrian Chadd 	ath_hal_intrset(ah, sc->sc_imask);
4389ef27340cSAdrian Chadd 	ATH_PCU_UNLOCK(sc);
4390ef27340cSAdrian Chadd 
4391e4e7938aSAdrian Chadd 	IF_LOCK(&ifp->if_snd);
4392ef27340cSAdrian Chadd 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4393e4e7938aSAdrian Chadd 	IF_UNLOCK(&ifp->if_snd);
4394ef27340cSAdrian Chadd 	ath_txrx_start(sc);
4395ef27340cSAdrian Chadd 	/* XXX ath_start? */
4396ef27340cSAdrian Chadd 
4397ef27340cSAdrian Chadd 	return ret;
43985591b213SSam Leffler }
43995591b213SSam Leffler 
44005591b213SSam Leffler /*
44015591b213SSam Leffler  * Periodically recalibrate the PHY to account
44025591b213SSam Leffler  * for temperature/environment changes.
44035591b213SSam Leffler  */
44045591b213SSam Leffler static void
44055591b213SSam Leffler ath_calibrate(void *arg)
44065591b213SSam Leffler {
44075591b213SSam Leffler 	struct ath_softc *sc = arg;
44085591b213SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
44092dc7fcc4SSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
44108d91de92SSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
4411943e37a1SAdrian Chadd 	HAL_BOOL longCal, isCalDone = AH_TRUE;
4412a108ab63SAdrian Chadd 	HAL_BOOL aniCal, shortCal = AH_FALSE;
44132dc7fcc4SSam Leffler 	int nextcal;
44145591b213SSam Leffler 
44158d91de92SSam Leffler 	if (ic->ic_flags & IEEE80211_F_SCAN)	/* defer, off channel */
44168d91de92SSam Leffler 		goto restart;
44172dc7fcc4SSam Leffler 	longCal = (ticks - sc->sc_lastlongcal >= ath_longcalinterval*hz);
4418a108ab63SAdrian Chadd 	aniCal = (ticks - sc->sc_lastani >= ath_anicalinterval*hz/1000);
4419a108ab63SAdrian Chadd 	if (sc->sc_doresetcal)
4420a108ab63SAdrian Chadd 		shortCal = (ticks - sc->sc_lastshortcal >= ath_shortcalinterval*hz/1000);
4421a108ab63SAdrian Chadd 
4422a108ab63SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: shortCal=%d; longCal=%d; aniCal=%d\n", __func__, shortCal, longCal, aniCal);
4423a108ab63SAdrian Chadd 	if (aniCal) {
4424a108ab63SAdrian Chadd 		sc->sc_stats.ast_ani_cal++;
4425a108ab63SAdrian Chadd 		sc->sc_lastani = ticks;
4426a108ab63SAdrian Chadd 		ath_hal_ani_poll(ah, sc->sc_curchan);
4427a108ab63SAdrian Chadd 	}
4428a108ab63SAdrian Chadd 
44292dc7fcc4SSam Leffler 	if (longCal) {
44305591b213SSam Leffler 		sc->sc_stats.ast_per_cal++;
44318197f57eSAdrian Chadd 		sc->sc_lastlongcal = ticks;
44325591b213SSam Leffler 		if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) {
44335591b213SSam Leffler 			/*
44345591b213SSam Leffler 			 * Rfgain is out of bounds, reset the chip
44355591b213SSam Leffler 			 * to load new gain values.
44365591b213SSam Leffler 			 */
4437370572d9SSam Leffler 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
4438370572d9SSam Leffler 				"%s: rfgain change\n", __func__);
44395591b213SSam Leffler 			sc->sc_stats.ast_per_rfgain++;
4440ef27340cSAdrian Chadd 			sc->sc_resetcal = 0;
4441ef27340cSAdrian Chadd 			sc->sc_doresetcal = AH_TRUE;
4442d52f7132SAdrian Chadd 			taskqueue_enqueue(sc->sc_tq, &sc->sc_resettask);
4443d52f7132SAdrian Chadd 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
4444ef27340cSAdrian Chadd 			return;
44455591b213SSam Leffler 		}
44462dc7fcc4SSam Leffler 		/*
44472dc7fcc4SSam Leffler 		 * If this long cal is after an idle period, then
44482dc7fcc4SSam Leffler 		 * reset the data collection state so we start fresh.
44492dc7fcc4SSam Leffler 		 */
44502dc7fcc4SSam Leffler 		if (sc->sc_resetcal) {
445159efa8b5SSam Leffler 			(void) ath_hal_calreset(ah, sc->sc_curchan);
44522dc7fcc4SSam Leffler 			sc->sc_lastcalreset = ticks;
4453a108ab63SAdrian Chadd 			sc->sc_lastshortcal = ticks;
44542dc7fcc4SSam Leffler 			sc->sc_resetcal = 0;
4455a108ab63SAdrian Chadd 			sc->sc_doresetcal = AH_TRUE;
44562dc7fcc4SSam Leffler 		}
44572dc7fcc4SSam Leffler 	}
4458a108ab63SAdrian Chadd 
4459a108ab63SAdrian Chadd 	/* Only call if we're doing a short/long cal, not for ANI calibration */
4460a108ab63SAdrian Chadd 	if (shortCal || longCal) {
4461943e37a1SAdrian Chadd 		isCalDone = AH_FALSE;
446259efa8b5SSam Leffler 		if (ath_hal_calibrateN(ah, sc->sc_curchan, longCal, &isCalDone)) {
44632dc7fcc4SSam Leffler 			if (longCal) {
44642dc7fcc4SSam Leffler 				/*
44652dc7fcc4SSam Leffler 				 * Calibrate noise floor data again in case of change.
44662dc7fcc4SSam Leffler 				 */
44672dc7fcc4SSam Leffler 				ath_hal_process_noisefloor(ah);
44682dc7fcc4SSam Leffler 			}
44692dc7fcc4SSam Leffler 		} else {
4470c42a7b7eSSam Leffler 			DPRINTF(sc, ATH_DEBUG_ANY,
4471c42a7b7eSSam Leffler 				"%s: calibration of channel %u failed\n",
447259efa8b5SSam Leffler 				__func__, sc->sc_curchan->ic_freq);
44735591b213SSam Leffler 			sc->sc_stats.ast_per_calfail++;
44745591b213SSam Leffler 		}
4475a108ab63SAdrian Chadd 		if (shortCal)
4476a108ab63SAdrian Chadd 			sc->sc_lastshortcal = ticks;
4477a108ab63SAdrian Chadd 	}
44782dc7fcc4SSam Leffler 	if (!isCalDone) {
44798d91de92SSam Leffler restart:
44807b0c77ecSSam Leffler 		/*
44812dc7fcc4SSam Leffler 		 * Use a shorter interval to potentially collect multiple
44822dc7fcc4SSam Leffler 		 * data samples required to complete calibration.  Once
44832dc7fcc4SSam Leffler 		 * we're told the work is done we drop back to a longer
44842dc7fcc4SSam Leffler 		 * interval between requests.  We're more aggressive doing
44852dc7fcc4SSam Leffler 		 * work when operating as an AP to improve operation right
44862dc7fcc4SSam Leffler 		 * after startup.
44877b0c77ecSSam Leffler 		 */
4488a108ab63SAdrian Chadd 		sc->sc_lastshortcal = ticks;
4489a108ab63SAdrian Chadd 		nextcal = ath_shortcalinterval*hz/1000;
44902dc7fcc4SSam Leffler 		if (sc->sc_opmode != HAL_M_HOSTAP)
44912dc7fcc4SSam Leffler 			nextcal *= 10;
4492a108ab63SAdrian Chadd 		sc->sc_doresetcal = AH_TRUE;
44932dc7fcc4SSam Leffler 	} else {
4494a108ab63SAdrian Chadd 		/* nextcal should be the shortest time for next event */
44952dc7fcc4SSam Leffler 		nextcal = ath_longcalinterval*hz;
44962dc7fcc4SSam Leffler 		if (sc->sc_lastcalreset == 0)
44972dc7fcc4SSam Leffler 			sc->sc_lastcalreset = sc->sc_lastlongcal;
44982dc7fcc4SSam Leffler 		else if (ticks - sc->sc_lastcalreset >= ath_resetcalinterval*hz)
44992dc7fcc4SSam Leffler 			sc->sc_resetcal = 1;	/* setup reset next trip */
4500a108ab63SAdrian Chadd 		sc->sc_doresetcal = AH_FALSE;
4501bd5a9920SSam Leffler 	}
4502a108ab63SAdrian Chadd 	/* ANI calibration may occur more often than short/long/resetcal */
4503a108ab63SAdrian Chadd 	if (ath_anicalinterval > 0)
4504a108ab63SAdrian Chadd 		nextcal = MIN(nextcal, ath_anicalinterval*hz/1000);
4505bd5a9920SSam Leffler 
45062dc7fcc4SSam Leffler 	if (nextcal != 0) {
45072dc7fcc4SSam Leffler 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: next +%u (%sisCalDone)\n",
45082dc7fcc4SSam Leffler 		    __func__, nextcal, isCalDone ? "" : "!");
45092dc7fcc4SSam Leffler 		callout_reset(&sc->sc_cal_ch, nextcal, ath_calibrate, sc);
45102dc7fcc4SSam Leffler 	} else {
45112dc7fcc4SSam Leffler 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: calibration disabled\n",
45122dc7fcc4SSam Leffler 		    __func__);
45132dc7fcc4SSam Leffler 		/* NB: don't rearm timer */
45142dc7fcc4SSam Leffler 	}
45155591b213SSam Leffler }
45165591b213SSam Leffler 
451768e8e04eSSam Leffler static void
451868e8e04eSSam Leffler ath_scan_start(struct ieee80211com *ic)
451968e8e04eSSam Leffler {
452068e8e04eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
452168e8e04eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
452268e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
452368e8e04eSSam Leffler 	u_int32_t rfilt;
452468e8e04eSSam Leffler 
452568e8e04eSSam Leffler 	/* XXX calibration timer? */
452668e8e04eSSam Leffler 
4527c98cefc5SAdrian Chadd 	ATH_LOCK(sc);
452868e8e04eSSam Leffler 	sc->sc_scanning = 1;
452968e8e04eSSam Leffler 	sc->sc_syncbeacon = 0;
453068e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
4531c98cefc5SAdrian Chadd 	ATH_UNLOCK(sc);
4532c98cefc5SAdrian Chadd 
4533c98cefc5SAdrian Chadd 	ATH_PCU_LOCK(sc);
453468e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
453568e8e04eSSam Leffler 	ath_hal_setassocid(ah, ifp->if_broadcastaddr, 0);
4536c98cefc5SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
453768e8e04eSSam Leffler 
453868e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0\n",
453968e8e04eSSam Leffler 		 __func__, rfilt, ether_sprintf(ifp->if_broadcastaddr));
454068e8e04eSSam Leffler }
454168e8e04eSSam Leffler 
454268e8e04eSSam Leffler static void
454368e8e04eSSam Leffler ath_scan_end(struct ieee80211com *ic)
454468e8e04eSSam Leffler {
454568e8e04eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
454668e8e04eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
454768e8e04eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
454868e8e04eSSam Leffler 	u_int32_t rfilt;
454968e8e04eSSam Leffler 
4550c98cefc5SAdrian Chadd 	ATH_LOCK(sc);
455168e8e04eSSam Leffler 	sc->sc_scanning = 0;
455268e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
4553c98cefc5SAdrian Chadd 	ATH_UNLOCK(sc);
4554c98cefc5SAdrian Chadd 
4555c98cefc5SAdrian Chadd 	ATH_PCU_LOCK(sc);
455668e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
455768e8e04eSSam Leffler 	ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
455868e8e04eSSam Leffler 
455968e8e04eSSam Leffler 	ath_hal_process_noisefloor(ah);
4560c98cefc5SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
456168e8e04eSSam Leffler 
456268e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
456368e8e04eSSam Leffler 		 __func__, rfilt, ether_sprintf(sc->sc_curbssid),
456468e8e04eSSam Leffler 		 sc->sc_curaid);
456568e8e04eSSam Leffler }
456668e8e04eSSam Leffler 
4567fdd72b4aSAdrian Chadd #ifdef	ATH_ENABLE_11N
4568e7200579SAdrian Chadd /*
4569e7200579SAdrian Chadd  * For now, just do a channel change.
4570e7200579SAdrian Chadd  *
4571e7200579SAdrian Chadd  * Later, we'll go through the hard slog of suspending tx/rx, changing rate
4572e7200579SAdrian Chadd  * control state and resetting the hardware without dropping frames out
4573e7200579SAdrian Chadd  * of the queue.
4574e7200579SAdrian Chadd  *
4575e7200579SAdrian Chadd  * The unfortunate trouble here is making absolutely sure that the
4576e7200579SAdrian Chadd  * channel width change has propagated enough so the hardware
4577e7200579SAdrian Chadd  * absolutely isn't handed bogus frames for it's current operating
4578e7200579SAdrian Chadd  * mode. (Eg, 40MHz frames in 20MHz mode.) Since TX and RX can and
4579e7200579SAdrian Chadd  * does occur in parallel, we need to make certain we've blocked
4580e7200579SAdrian Chadd  * any further ongoing TX (and RX, that can cause raw TX)
4581e7200579SAdrian Chadd  * before we do this.
4582e7200579SAdrian Chadd  */
4583e7200579SAdrian Chadd static void
4584e7200579SAdrian Chadd ath_update_chw(struct ieee80211com *ic)
4585e7200579SAdrian Chadd {
4586e7200579SAdrian Chadd 	struct ifnet *ifp = ic->ic_ifp;
4587e7200579SAdrian Chadd 	struct ath_softc *sc = ifp->if_softc;
4588e7200579SAdrian Chadd 
4589e7200579SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: called\n", __func__);
4590e7200579SAdrian Chadd 	ath_set_channel(ic);
4591e7200579SAdrian Chadd }
4592fdd72b4aSAdrian Chadd #endif	/* ATH_ENABLE_11N */
4593e7200579SAdrian Chadd 
459468e8e04eSSam Leffler static void
459568e8e04eSSam Leffler ath_set_channel(struct ieee80211com *ic)
459668e8e04eSSam Leffler {
459768e8e04eSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
459868e8e04eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
459968e8e04eSSam Leffler 
460068e8e04eSSam Leffler 	(void) ath_chan_set(sc, ic->ic_curchan);
460168e8e04eSSam Leffler 	/*
460268e8e04eSSam Leffler 	 * If we are returning to our bss channel then mark state
460368e8e04eSSam Leffler 	 * so the next recv'd beacon's tsf will be used to sync the
460468e8e04eSSam Leffler 	 * beacon timers.  Note that since we only hear beacons in
460568e8e04eSSam Leffler 	 * sta/ibss mode this has no effect in other operating modes.
460668e8e04eSSam Leffler 	 */
4607a887b1e3SAdrian Chadd 	ATH_LOCK(sc);
460868e8e04eSSam Leffler 	if (!sc->sc_scanning && ic->ic_curchan == ic->ic_bsschan)
460968e8e04eSSam Leffler 		sc->sc_syncbeacon = 1;
4610a887b1e3SAdrian Chadd 	ATH_UNLOCK(sc);
461168e8e04eSSam Leffler }
461268e8e04eSSam Leffler 
4613b032f27cSSam Leffler /*
4614b032f27cSSam Leffler  * Walk the vap list and check if there any vap's in RUN state.
4615b032f27cSSam Leffler  */
46165591b213SSam Leffler static int
4617b032f27cSSam Leffler ath_isanyrunningvaps(struct ieee80211vap *this)
46185591b213SSam Leffler {
4619b032f27cSSam Leffler 	struct ieee80211com *ic = this->iv_ic;
4620b032f27cSSam Leffler 	struct ieee80211vap *vap;
4621b032f27cSSam Leffler 
4622b032f27cSSam Leffler 	IEEE80211_LOCK_ASSERT(ic);
4623b032f27cSSam Leffler 
4624b032f27cSSam Leffler 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
4625309a3e45SSam Leffler 		if (vap != this && vap->iv_state >= IEEE80211_S_RUN)
4626b032f27cSSam Leffler 			return 1;
4627b032f27cSSam Leffler 	}
4628b032f27cSSam Leffler 	return 0;
4629b032f27cSSam Leffler }
4630b032f27cSSam Leffler 
4631b032f27cSSam Leffler static int
4632b032f27cSSam Leffler ath_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
4633b032f27cSSam Leffler {
4634b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
4635b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
4636b032f27cSSam Leffler 	struct ath_vap *avp = ATH_VAP(vap);
463745bbf62fSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4638b032f27cSSam Leffler 	struct ieee80211_node *ni = NULL;
463968e8e04eSSam Leffler 	int i, error, stamode;
46405591b213SSam Leffler 	u_int32_t rfilt;
4641f52efb6dSAdrian Chadd 	int csa_run_transition = 0;
46425591b213SSam Leffler 	static const HAL_LED_STATE leds[] = {
46435591b213SSam Leffler 	    HAL_LED_INIT,	/* IEEE80211_S_INIT */
46445591b213SSam Leffler 	    HAL_LED_SCAN,	/* IEEE80211_S_SCAN */
46455591b213SSam Leffler 	    HAL_LED_AUTH,	/* IEEE80211_S_AUTH */
46465591b213SSam Leffler 	    HAL_LED_ASSOC, 	/* IEEE80211_S_ASSOC */
464777d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_CAC */
46485591b213SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_RUN */
464977d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_CSA */
465077d5e068SSam Leffler 	    HAL_LED_RUN, 	/* IEEE80211_S_SLEEP */
46515591b213SSam Leffler 	};
46525591b213SSam Leffler 
4653c42a7b7eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: %s -> %s\n", __func__,
4654b032f27cSSam Leffler 		ieee80211_state_name[vap->iv_state],
4655c42a7b7eSSam Leffler 		ieee80211_state_name[nstate]);
46565591b213SSam Leffler 
4657107fdf96SAdrian Chadd 	/*
4658107fdf96SAdrian Chadd 	 * net80211 _should_ have the comlock asserted at this point.
4659107fdf96SAdrian Chadd 	 * There are some comments around the calls to vap->iv_newstate
4660107fdf96SAdrian Chadd 	 * which indicate that it (newstate) may end up dropping the
4661107fdf96SAdrian Chadd 	 * lock.  This and the subsequent lock assert check after newstate
4662107fdf96SAdrian Chadd 	 * are an attempt to catch these and figure out how/why.
4663107fdf96SAdrian Chadd 	 */
4664107fdf96SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
4665107fdf96SAdrian Chadd 
4666f52efb6dSAdrian Chadd 	if (vap->iv_state == IEEE80211_S_CSA && nstate == IEEE80211_S_RUN)
4667f52efb6dSAdrian Chadd 		csa_run_transition = 1;
4668f52efb6dSAdrian Chadd 
46692e986da5SSam Leffler 	callout_drain(&sc->sc_cal_ch);
46705591b213SSam Leffler 	ath_hal_setledstate(ah, leds[nstate]);	/* set LED */
46715591b213SSam Leffler 
4672b032f27cSSam Leffler 	if (nstate == IEEE80211_S_SCAN) {
467358769f58SSam Leffler 		/*
4674b032f27cSSam Leffler 		 * Scanning: turn off beacon miss and don't beacon.
4675b032f27cSSam Leffler 		 * Mark beacon state so when we reach RUN state we'll
4676b032f27cSSam Leffler 		 * [re]setup beacons.  Unblock the task q thread so
4677b032f27cSSam Leffler 		 * deferred interrupt processing is done.
467858769f58SSam Leffler 		 */
4679b032f27cSSam Leffler 		ath_hal_intrset(ah,
4680b032f27cSSam Leffler 		    sc->sc_imask &~ (HAL_INT_SWBA | HAL_INT_BMISS));
46815591b213SSam Leffler 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
4682b032f27cSSam Leffler 		sc->sc_beacons = 0;
4683b032f27cSSam Leffler 		taskqueue_unblock(sc->sc_tq);
46845591b213SSam Leffler 	}
46855591b213SSam Leffler 
468680767531SAdrian Chadd 	ni = ieee80211_ref_node(vap->iv_bss);
468768e8e04eSSam Leffler 	rfilt = ath_calcrxfilter(sc);
4688b032f27cSSam Leffler 	stamode = (vap->iv_opmode == IEEE80211_M_STA ||
46897b916f89SSam Leffler 		   vap->iv_opmode == IEEE80211_M_AHDEMO ||
4690b032f27cSSam Leffler 		   vap->iv_opmode == IEEE80211_M_IBSS);
469168e8e04eSSam Leffler 	if (stamode && nstate == IEEE80211_S_RUN) {
469268e8e04eSSam Leffler 		sc->sc_curaid = ni->ni_associd;
469368e8e04eSSam Leffler 		IEEE80211_ADDR_COPY(sc->sc_curbssid, ni->ni_bssid);
4694b032f27cSSam Leffler 		ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
4695b032f27cSSam Leffler 	}
469668e8e04eSSam Leffler 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
4697b032f27cSSam Leffler 	   __func__, rfilt, ether_sprintf(sc->sc_curbssid), sc->sc_curaid);
469868e8e04eSSam Leffler 	ath_hal_setrxfilter(ah, rfilt);
469968e8e04eSSam Leffler 
4700b032f27cSSam Leffler 	/* XXX is this to restore keycache on resume? */
4701b032f27cSSam Leffler 	if (vap->iv_opmode != IEEE80211_M_STA &&
4702b032f27cSSam Leffler 	    (vap->iv_flags & IEEE80211_F_PRIVACY)) {
47035591b213SSam Leffler 		for (i = 0; i < IEEE80211_WEP_NKID; i++)
47045591b213SSam Leffler 			if (ath_hal_keyisvalid(ah, i))
470568e8e04eSSam Leffler 				ath_hal_keysetmac(ah, i, ni->ni_bssid);
47065591b213SSam Leffler 	}
4707b032f27cSSam Leffler 
4708b032f27cSSam Leffler 	/*
4709b032f27cSSam Leffler 	 * Invoke the parent method to do net80211 work.
4710b032f27cSSam Leffler 	 */
4711b032f27cSSam Leffler 	error = avp->av_newstate(vap, nstate, arg);
4712b032f27cSSam Leffler 	if (error != 0)
4713b032f27cSSam Leffler 		goto bad;
4714c42a7b7eSSam Leffler 
4715107fdf96SAdrian Chadd 	/*
4716107fdf96SAdrian Chadd 	 * See above: ensure av_newstate() doesn't drop the lock
4717107fdf96SAdrian Chadd 	 * on us.
4718107fdf96SAdrian Chadd 	 */
4719107fdf96SAdrian Chadd 	IEEE80211_LOCK_ASSERT(ic);
4720107fdf96SAdrian Chadd 
472168e8e04eSSam Leffler 	if (nstate == IEEE80211_S_RUN) {
4722b032f27cSSam Leffler 		/* NB: collect bss node again, it may have changed */
472380767531SAdrian Chadd 		ieee80211_free_node(ni);
472480767531SAdrian Chadd 		ni = ieee80211_ref_node(vap->iv_bss);
47255591b213SSam Leffler 
4726b032f27cSSam Leffler 		DPRINTF(sc, ATH_DEBUG_STATE,
4727b032f27cSSam Leffler 		    "%s(RUN): iv_flags 0x%08x bintvl %d bssid %s "
4728b032f27cSSam Leffler 		    "capinfo 0x%04x chan %d\n", __func__,
4729b032f27cSSam Leffler 		    vap->iv_flags, ni->ni_intval, ether_sprintf(ni->ni_bssid),
4730b032f27cSSam Leffler 		    ni->ni_capinfo, ieee80211_chan2ieee(ic, ic->ic_curchan));
4731b032f27cSSam Leffler 
4732b032f27cSSam Leffler 		switch (vap->iv_opmode) {
4733584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
473410ad9a77SSam Leffler 		case IEEE80211_M_AHDEMO:
473510ad9a77SSam Leffler 			if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
473610ad9a77SSam Leffler 				break;
473710ad9a77SSam Leffler 			/* fall thru... */
473810ad9a77SSam Leffler #endif
4739e8fd88a3SSam Leffler 		case IEEE80211_M_HOSTAP:
4740e8fd88a3SSam Leffler 		case IEEE80211_M_IBSS:
474159aa14a9SRui Paulo 		case IEEE80211_M_MBSS:
47425591b213SSam Leffler 			/*
4743e8fd88a3SSam Leffler 			 * Allocate and setup the beacon frame.
4744e8fd88a3SSam Leffler 			 *
4745f818612bSSam Leffler 			 * Stop any previous beacon DMA.  This may be
4746f818612bSSam Leffler 			 * necessary, for example, when an ibss merge
4747f818612bSSam Leffler 			 * causes reconfiguration; there will be a state
4748f818612bSSam Leffler 			 * transition from RUN->RUN that means we may
4749f818612bSSam Leffler 			 * be called with beacon transmission active.
4750f818612bSSam Leffler 			 */
4751f818612bSSam Leffler 			ath_hal_stoptxdma(ah, sc->sc_bhalq);
4752b032f27cSSam Leffler 
47535591b213SSam Leffler 			error = ath_beacon_alloc(sc, ni);
47545591b213SSam Leffler 			if (error != 0)
47555591b213SSam Leffler 				goto bad;
47567a04dc27SSam Leffler 			/*
475780d939bfSSam Leffler 			 * If joining an adhoc network defer beacon timer
475880d939bfSSam Leffler 			 * configuration to the next beacon frame so we
475980d939bfSSam Leffler 			 * have a current TSF to use.  Otherwise we're
4760b032f27cSSam Leffler 			 * starting an ibss/bss so there's no need to delay;
4761b032f27cSSam Leffler 			 * if this is the first vap moving to RUN state, then
4762b032f27cSSam Leffler 			 * beacon state needs to be [re]configured.
47637a04dc27SSam Leffler 			 */
4764b032f27cSSam Leffler 			if (vap->iv_opmode == IEEE80211_M_IBSS &&
4765b032f27cSSam Leffler 			    ni->ni_tstamp.tsf != 0) {
476680d939bfSSam Leffler 				sc->sc_syncbeacon = 1;
4767b032f27cSSam Leffler 			} else if (!sc->sc_beacons) {
4768584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
476910ad9a77SSam Leffler 				if (vap->iv_caps & IEEE80211_C_TDMA)
477010ad9a77SSam Leffler 					ath_tdma_config(sc, vap);
477110ad9a77SSam Leffler 				else
477210ad9a77SSam Leffler #endif
4773b032f27cSSam Leffler 					ath_beacon_config(sc, vap);
4774b032f27cSSam Leffler 				sc->sc_beacons = 1;
4775b032f27cSSam Leffler 			}
4776e8fd88a3SSam Leffler 			break;
4777e8fd88a3SSam Leffler 		case IEEE80211_M_STA:
4778e8fd88a3SSam Leffler 			/*
477980d939bfSSam Leffler 			 * Defer beacon timer configuration to the next
478080d939bfSSam Leffler 			 * beacon frame so we have a current TSF to use
478180d939bfSSam Leffler 			 * (any TSF collected when scanning is likely old).
4782f52efb6dSAdrian Chadd 			 * However if it's due to a CSA -> RUN transition,
4783f52efb6dSAdrian Chadd 			 * force a beacon update so we pick up a lack of
4784f52efb6dSAdrian Chadd 			 * beacons from an AP in CAC and thus force a
4785f52efb6dSAdrian Chadd 			 * scan.
47867a04dc27SSam Leffler 			 */
478780d939bfSSam Leffler 			sc->sc_syncbeacon = 1;
4788f52efb6dSAdrian Chadd 			if (csa_run_transition)
4789f52efb6dSAdrian Chadd 				ath_beacon_config(sc, vap);
4790e8fd88a3SSam Leffler 			break;
4791b032f27cSSam Leffler 		case IEEE80211_M_MONITOR:
4792b032f27cSSam Leffler 			/*
4793b032f27cSSam Leffler 			 * Monitor mode vaps have only INIT->RUN and RUN->RUN
4794b032f27cSSam Leffler 			 * transitions so we must re-enable interrupts here to
4795b032f27cSSam Leffler 			 * handle the case of a single monitor mode vap.
4796b032f27cSSam Leffler 			 */
4797b032f27cSSam Leffler 			ath_hal_intrset(ah, sc->sc_imask);
4798b032f27cSSam Leffler 			break;
4799b032f27cSSam Leffler 		case IEEE80211_M_WDS:
4800b032f27cSSam Leffler 			break;
4801e8fd88a3SSam Leffler 		default:
4802e8fd88a3SSam Leffler 			break;
48035591b213SSam Leffler 		}
48045591b213SSam Leffler 		/*
48057b0c77ecSSam Leffler 		 * Let the hal process statistics collected during a
48067b0c77ecSSam Leffler 		 * scan so it can provide calibrated noise floor data.
48077b0c77ecSSam Leffler 		 */
48087b0c77ecSSam Leffler 		ath_hal_process_noisefloor(ah);
48097b0c77ecSSam Leffler 		/*
4810ffa2cab6SSam Leffler 		 * Reset rssi stats; maybe not the best place...
4811ffa2cab6SSam Leffler 		 */
4812ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
4813ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
4814ffa2cab6SSam Leffler 		sc->sc_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
481545bbf62fSSam Leffler 		/*
4816b032f27cSSam Leffler 		 * Finally, start any timers and the task q thread
4817b032f27cSSam Leffler 		 * (in case we didn't go through SCAN state).
481845bbf62fSSam Leffler 		 */
48192dc7fcc4SSam Leffler 		if (ath_longcalinterval != 0) {
4820c42a7b7eSSam Leffler 			/* start periodic recalibration timer */
48212dc7fcc4SSam Leffler 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
48222dc7fcc4SSam Leffler 		} else {
48232dc7fcc4SSam Leffler 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
48242dc7fcc4SSam Leffler 			    "%s: calibration disabled\n", __func__);
4825c42a7b7eSSam Leffler 		}
4826b032f27cSSam Leffler 		taskqueue_unblock(sc->sc_tq);
4827b032f27cSSam Leffler 	} else if (nstate == IEEE80211_S_INIT) {
4828b032f27cSSam Leffler 		/*
4829b032f27cSSam Leffler 		 * If there are no vaps left in RUN state then
4830b032f27cSSam Leffler 		 * shutdown host/driver operation:
4831b032f27cSSam Leffler 		 * o disable interrupts
4832b032f27cSSam Leffler 		 * o disable the task queue thread
4833b032f27cSSam Leffler 		 * o mark beacon processing as stopped
4834b032f27cSSam Leffler 		 */
4835b032f27cSSam Leffler 		if (!ath_isanyrunningvaps(vap)) {
4836b032f27cSSam Leffler 			sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
4837b032f27cSSam Leffler 			/* disable interrupts  */
4838b032f27cSSam Leffler 			ath_hal_intrset(ah, sc->sc_imask &~ HAL_INT_GLOBAL);
4839b032f27cSSam Leffler 			taskqueue_block(sc->sc_tq);
4840b032f27cSSam Leffler 			sc->sc_beacons = 0;
4841b032f27cSSam Leffler 		}
4842584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
484310ad9a77SSam Leffler 		ath_hal_setcca(ah, AH_TRUE);
484410ad9a77SSam Leffler #endif
4845b032f27cSSam Leffler 	}
48465591b213SSam Leffler bad:
484780767531SAdrian Chadd 	ieee80211_free_node(ni);
48485591b213SSam Leffler 	return error;
48495591b213SSam Leffler }
48505591b213SSam Leffler 
48515591b213SSam Leffler /*
4852e8fd88a3SSam Leffler  * Allocate a key cache slot to the station so we can
4853e8fd88a3SSam Leffler  * setup a mapping from key index to node. The key cache
4854e8fd88a3SSam Leffler  * slot is needed for managing antenna state and for
4855e8fd88a3SSam Leffler  * compression when stations do not use crypto.  We do
4856e8fd88a3SSam Leffler  * it uniliaterally here; if crypto is employed this slot
4857e8fd88a3SSam Leffler  * will be reassigned.
4858e8fd88a3SSam Leffler  */
4859e8fd88a3SSam Leffler static void
4860e8fd88a3SSam Leffler ath_setup_stationkey(struct ieee80211_node *ni)
4861e8fd88a3SSam Leffler {
4862b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
4863b032f27cSSam Leffler 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
4864c1225b52SSam Leffler 	ieee80211_keyix keyix, rxkeyix;
4865e8fd88a3SSam Leffler 
486680767531SAdrian Chadd 	/* XXX should take a locked ref to vap->iv_bss */
4867b032f27cSSam Leffler 	if (!ath_key_alloc(vap, &ni->ni_ucastkey, &keyix, &rxkeyix)) {
4868e8fd88a3SSam Leffler 		/*
4869e8fd88a3SSam Leffler 		 * Key cache is full; we'll fall back to doing
4870e8fd88a3SSam Leffler 		 * the more expensive lookup in software.  Note
4871e8fd88a3SSam Leffler 		 * this also means no h/w compression.
4872e8fd88a3SSam Leffler 		 */
4873e8fd88a3SSam Leffler 		/* XXX msg+statistic */
4874e8fd88a3SSam Leffler 	} else {
4875c1225b52SSam Leffler 		/* XXX locking? */
4876e8fd88a3SSam Leffler 		ni->ni_ucastkey.wk_keyix = keyix;
4877c1225b52SSam Leffler 		ni->ni_ucastkey.wk_rxkeyix = rxkeyix;
487833052833SSam Leffler 		/* NB: must mark device key to get called back on delete */
487933052833SSam Leffler 		ni->ni_ucastkey.wk_flags |= IEEE80211_KEY_DEVKEY;
4880d3ac945bSSam Leffler 		IEEE80211_ADDR_COPY(ni->ni_ucastkey.wk_macaddr, ni->ni_macaddr);
4881e8fd88a3SSam Leffler 		/* NB: this will create a pass-thru key entry */
488255c7b877SAdrian Chadd 		ath_keyset(sc, vap, &ni->ni_ucastkey, vap->iv_bss);
4883e8fd88a3SSam Leffler 	}
4884e8fd88a3SSam Leffler }
4885e8fd88a3SSam Leffler 
4886e8fd88a3SSam Leffler /*
48875591b213SSam Leffler  * Setup driver-specific state for a newly associated node.
48885591b213SSam Leffler  * Note that we're called also on a re-associate, the isnew
48895591b213SSam Leffler  * param tells us if this is the first time or not.
48905591b213SSam Leffler  */
48915591b213SSam Leffler static void
4892e9962332SSam Leffler ath_newassoc(struct ieee80211_node *ni, int isnew)
48935591b213SSam Leffler {
4894b032f27cSSam Leffler 	struct ath_node *an = ATH_NODE(ni);
4895b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
4896b032f27cSSam Leffler 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
4897c62362cbSSam Leffler 	const struct ieee80211_txparam *tp = ni->ni_txparms;
48985591b213SSam Leffler 
4899ab06fdf2SSam Leffler 	an->an_mcastrix = ath_tx_findrix(sc, tp->mcastrate);
4900ab06fdf2SSam Leffler 	an->an_mgmtrix = ath_tx_findrix(sc, tp->mgmtrate);
4901b032f27cSSam Leffler 
4902b032f27cSSam Leffler 	ath_rate_newassoc(sc, an, isnew);
4903e8fd88a3SSam Leffler 	if (isnew &&
4904b032f27cSSam Leffler 	    (vap->iv_flags & IEEE80211_F_PRIVACY) == 0 && sc->sc_hasclrkey &&
4905b032f27cSSam Leffler 	    ni->ni_ucastkey.wk_keyix == IEEE80211_KEYIX_NONE)
4906e8fd88a3SSam Leffler 		ath_setup_stationkey(ni);
4907e8fd88a3SSam Leffler }
49085591b213SSam Leffler 
49095591b213SSam Leffler static int
491059efa8b5SSam Leffler ath_setregdomain(struct ieee80211com *ic, struct ieee80211_regdomain *reg,
4911b032f27cSSam Leffler 	int nchans, struct ieee80211_channel chans[])
4912b032f27cSSam Leffler {
4913b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
4914b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
491559efa8b5SSam Leffler 	HAL_STATUS status;
4916b032f27cSSam Leffler 
4917033022a9SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
491859efa8b5SSam Leffler 	    "%s: rd %u cc %u location %c%s\n",
491959efa8b5SSam Leffler 	    __func__, reg->regdomain, reg->country, reg->location,
492059efa8b5SSam Leffler 	    reg->ecm ? " ecm" : "");
4921033022a9SSam Leffler 
492259efa8b5SSam Leffler 	status = ath_hal_set_channels(ah, chans, nchans,
492359efa8b5SSam Leffler 	    reg->country, reg->regdomain);
492459efa8b5SSam Leffler 	if (status != HAL_OK) {
492559efa8b5SSam Leffler 		DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: failed, status %u\n",
492659efa8b5SSam Leffler 		    __func__, status);
492759efa8b5SSam Leffler 		return EINVAL;		/* XXX */
4928b032f27cSSam Leffler 	}
49298db87e40SAdrian Chadd 
4930b032f27cSSam Leffler 	return 0;
4931b032f27cSSam Leffler }
4932b032f27cSSam Leffler 
4933b032f27cSSam Leffler static void
4934b032f27cSSam Leffler ath_getradiocaps(struct ieee80211com *ic,
49355fe9f044SSam Leffler 	int maxchans, int *nchans, struct ieee80211_channel chans[])
4936b032f27cSSam Leffler {
4937b032f27cSSam Leffler 	struct ath_softc *sc = ic->ic_ifp->if_softc;
4938b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
4939b032f27cSSam Leffler 
494059efa8b5SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: use rd %u cc %d\n",
494159efa8b5SSam Leffler 	    __func__, SKU_DEBUG, CTRY_DEFAULT);
4942033022a9SSam Leffler 
494359efa8b5SSam Leffler 	/* XXX check return */
494459efa8b5SSam Leffler 	(void) ath_hal_getchannels(ah, chans, maxchans, nchans,
494559efa8b5SSam Leffler 	    HAL_MODE_ALL, CTRY_DEFAULT, SKU_DEBUG, AH_TRUE);
4946033022a9SSam Leffler 
4947b032f27cSSam Leffler }
4948b032f27cSSam Leffler 
4949b032f27cSSam Leffler static int
4950b032f27cSSam Leffler ath_getchannels(struct ath_softc *sc)
4951b032f27cSSam Leffler {
4952b032f27cSSam Leffler 	struct ifnet *ifp = sc->sc_ifp;
4953b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
4954b032f27cSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
495559efa8b5SSam Leffler 	HAL_STATUS status;
4956b032f27cSSam Leffler 
4957b032f27cSSam Leffler 	/*
495859efa8b5SSam Leffler 	 * Collect channel set based on EEPROM contents.
4959b032f27cSSam Leffler 	 */
496059efa8b5SSam Leffler 	status = ath_hal_init_channels(ah, ic->ic_channels, IEEE80211_CHAN_MAX,
496159efa8b5SSam Leffler 	    &ic->ic_nchans, HAL_MODE_ALL, CTRY_DEFAULT, SKU_NONE, AH_TRUE);
496259efa8b5SSam Leffler 	if (status != HAL_OK) {
496359efa8b5SSam Leffler 		if_printf(ifp, "%s: unable to collect channel list from hal, "
496459efa8b5SSam Leffler 		    "status %d\n", __func__, status);
496559efa8b5SSam Leffler 		return EINVAL;
496659efa8b5SSam Leffler 	}
4967ca876918SSam Leffler 	(void) ath_hal_getregdomain(ah, &sc->sc_eerd);
4968ca876918SSam Leffler 	ath_hal_getcountrycode(ah, &sc->sc_eecc);	/* NB: cannot fail */
496959efa8b5SSam Leffler 	/* XXX map Atheros sku's to net80211 SKU's */
497059efa8b5SSam Leffler 	/* XXX net80211 types too small */
497159efa8b5SSam Leffler 	ic->ic_regdomain.regdomain = (uint16_t) sc->sc_eerd;
497259efa8b5SSam Leffler 	ic->ic_regdomain.country = (uint16_t) sc->sc_eecc;
497359efa8b5SSam Leffler 	ic->ic_regdomain.isocc[0] = ' ';	/* XXX don't know */
497459efa8b5SSam Leffler 	ic->ic_regdomain.isocc[1] = ' ';
497559efa8b5SSam Leffler 
4976b032f27cSSam Leffler 	ic->ic_regdomain.ecm = 1;
4977b032f27cSSam Leffler 	ic->ic_regdomain.location = 'I';
4978033022a9SSam Leffler 
4979033022a9SSam Leffler 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
498059efa8b5SSam Leffler 	    "%s: eeprom rd %u cc %u (mapped rd %u cc %u) location %c%s\n",
4981033022a9SSam Leffler 	    __func__, sc->sc_eerd, sc->sc_eecc,
4982033022a9SSam Leffler 	    ic->ic_regdomain.regdomain, ic->ic_regdomain.country,
498359efa8b5SSam Leffler 	    ic->ic_regdomain.location, ic->ic_regdomain.ecm ? " ecm" : "");
49845591b213SSam Leffler 	return 0;
49855591b213SSam Leffler }
49865591b213SSam Leffler 
49876c4612b9SSam Leffler static int
49886c4612b9SSam Leffler ath_rate_setup(struct ath_softc *sc, u_int mode)
49896c4612b9SSam Leffler {
49906c4612b9SSam Leffler 	struct ath_hal *ah = sc->sc_ah;
49916c4612b9SSam Leffler 	const HAL_RATE_TABLE *rt;
49926c4612b9SSam Leffler 
49936c4612b9SSam Leffler 	switch (mode) {
49946c4612b9SSam Leffler 	case IEEE80211_MODE_11A:
49956c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A);
49966c4612b9SSam Leffler 		break;
4997724c193aSSam Leffler 	case IEEE80211_MODE_HALF:
4998aaa70f2fSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_HALF_RATE);
4999aaa70f2fSSam Leffler 		break;
5000724c193aSSam Leffler 	case IEEE80211_MODE_QUARTER:
5001aaa70f2fSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_QUARTER_RATE);
5002aaa70f2fSSam Leffler 		break;
50036c4612b9SSam Leffler 	case IEEE80211_MODE_11B:
50046c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11B);
50056c4612b9SSam Leffler 		break;
50066c4612b9SSam Leffler 	case IEEE80211_MODE_11G:
50076c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11G);
50086c4612b9SSam Leffler 		break;
50096c4612b9SSam Leffler 	case IEEE80211_MODE_TURBO_A:
501068e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_108A);
50116c4612b9SSam Leffler 		break;
50126c4612b9SSam Leffler 	case IEEE80211_MODE_TURBO_G:
50136c4612b9SSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_108G);
50146c4612b9SSam Leffler 		break;
501568e8e04eSSam Leffler 	case IEEE80211_MODE_STURBO_A:
501668e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_TURBO);
501768e8e04eSSam Leffler 		break;
501868e8e04eSSam Leffler 	case IEEE80211_MODE_11NA:
501968e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11NA_HT20);
502068e8e04eSSam Leffler 		break;
502168e8e04eSSam Leffler 	case IEEE80211_MODE_11NG:
502268e8e04eSSam Leffler 		rt = ath_hal_getratetable(ah, HAL_MODE_11NG_HT20);
502368e8e04eSSam Leffler 		break;
50246c4612b9SSam Leffler 	default:
50256c4612b9SSam Leffler 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid mode %u\n",
50266c4612b9SSam Leffler 			__func__, mode);
50276c4612b9SSam Leffler 		return 0;
50286c4612b9SSam Leffler 	}
50296c4612b9SSam Leffler 	sc->sc_rates[mode] = rt;
5030aaa70f2fSSam Leffler 	return (rt != NULL);
50315591b213SSam Leffler }
50325591b213SSam Leffler 
50335591b213SSam Leffler static void
50345591b213SSam Leffler ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode)
50355591b213SSam Leffler {
50363e50ec2cSSam Leffler #define	N(a)	(sizeof(a)/sizeof(a[0]))
50373e50ec2cSSam Leffler 	/* NB: on/off times from the Atheros NDIS driver, w/ permission */
50383e50ec2cSSam Leffler 	static const struct {
50393e50ec2cSSam Leffler 		u_int		rate;		/* tx/rx 802.11 rate */
50403e50ec2cSSam Leffler 		u_int16_t	timeOn;		/* LED on time (ms) */
50413e50ec2cSSam Leffler 		u_int16_t	timeOff;	/* LED off time (ms) */
50423e50ec2cSSam Leffler 	} blinkrates[] = {
50433e50ec2cSSam Leffler 		{ 108,  40,  10 },
50443e50ec2cSSam Leffler 		{  96,  44,  11 },
50453e50ec2cSSam Leffler 		{  72,  50,  13 },
50463e50ec2cSSam Leffler 		{  48,  57,  14 },
50473e50ec2cSSam Leffler 		{  36,  67,  16 },
50483e50ec2cSSam Leffler 		{  24,  80,  20 },
50493e50ec2cSSam Leffler 		{  22, 100,  25 },
50503e50ec2cSSam Leffler 		{  18, 133,  34 },
50513e50ec2cSSam Leffler 		{  12, 160,  40 },
50523e50ec2cSSam Leffler 		{  10, 200,  50 },
50533e50ec2cSSam Leffler 		{   6, 240,  58 },
50543e50ec2cSSam Leffler 		{   4, 267,  66 },
50553e50ec2cSSam Leffler 		{   2, 400, 100 },
50563e50ec2cSSam Leffler 		{   0, 500, 130 },
5057724c193aSSam Leffler 		/* XXX half/quarter rates */
50583e50ec2cSSam Leffler 	};
50595591b213SSam Leffler 	const HAL_RATE_TABLE *rt;
50603e50ec2cSSam Leffler 	int i, j;
50615591b213SSam Leffler 
50625591b213SSam Leffler 	memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap));
50635591b213SSam Leffler 	rt = sc->sc_rates[mode];
50645591b213SSam Leffler 	KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode));
5065180f268dSSam Leffler 	for (i = 0; i < rt->rateCount; i++) {
5066180f268dSSam Leffler 		uint8_t ieeerate = rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
5067180f268dSSam Leffler 		if (rt->info[i].phy != IEEE80211_T_HT)
5068180f268dSSam Leffler 			sc->sc_rixmap[ieeerate] = i;
5069180f268dSSam Leffler 		else
5070180f268dSSam Leffler 			sc->sc_rixmap[ieeerate | IEEE80211_RATE_MCS] = i;
5071180f268dSSam Leffler 	}
50721b1a8e41SSam Leffler 	memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap));
507346d4d74cSSam Leffler 	for (i = 0; i < N(sc->sc_hwmap); i++) {
507446d4d74cSSam Leffler 		if (i >= rt->rateCount) {
50753e50ec2cSSam Leffler 			sc->sc_hwmap[i].ledon = (500 * hz) / 1000;
50763e50ec2cSSam Leffler 			sc->sc_hwmap[i].ledoff = (130 * hz) / 1000;
507716b4851aSSam Leffler 			continue;
50783e50ec2cSSam Leffler 		}
50793e50ec2cSSam Leffler 		sc->sc_hwmap[i].ieeerate =
508046d4d74cSSam Leffler 			rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
508146d4d74cSSam Leffler 		if (rt->info[i].phy == IEEE80211_T_HT)
508226041a14SSam Leffler 			sc->sc_hwmap[i].ieeerate |= IEEE80211_RATE_MCS;
5083d3be6f5bSSam Leffler 		sc->sc_hwmap[i].txflags = IEEE80211_RADIOTAP_F_DATAPAD;
508446d4d74cSSam Leffler 		if (rt->info[i].shortPreamble ||
508546d4d74cSSam Leffler 		    rt->info[i].phy == IEEE80211_T_OFDM)
5086d3be6f5bSSam Leffler 			sc->sc_hwmap[i].txflags |= IEEE80211_RADIOTAP_F_SHORTPRE;
50875463c4a4SSam Leffler 		sc->sc_hwmap[i].rxflags = sc->sc_hwmap[i].txflags;
50883e50ec2cSSam Leffler 		for (j = 0; j < N(blinkrates)-1; j++)
50893e50ec2cSSam Leffler 			if (blinkrates[j].rate == sc->sc_hwmap[i].ieeerate)
50903e50ec2cSSam Leffler 				break;
50913e50ec2cSSam Leffler 		/* NB: this uses the last entry if the rate isn't found */
50923e50ec2cSSam Leffler 		/* XXX beware of overlow */
50933e50ec2cSSam Leffler 		sc->sc_hwmap[i].ledon = (blinkrates[j].timeOn * hz) / 1000;
50943e50ec2cSSam Leffler 		sc->sc_hwmap[i].ledoff = (blinkrates[j].timeOff * hz) / 1000;
5095c42a7b7eSSam Leffler 	}
50965591b213SSam Leffler 	sc->sc_currates = rt;
50975591b213SSam Leffler 	sc->sc_curmode = mode;
50985591b213SSam Leffler 	/*
5099c42a7b7eSSam Leffler 	 * All protection frames are transmited at 2Mb/s for
5100c42a7b7eSSam Leffler 	 * 11g, otherwise at 1Mb/s.
51015591b213SSam Leffler 	 */
5102913a1ba1SSam Leffler 	if (mode == IEEE80211_MODE_11G)
5103ab06fdf2SSam Leffler 		sc->sc_protrix = ath_tx_findrix(sc, 2*2);
5104913a1ba1SSam Leffler 	else
5105ab06fdf2SSam Leffler 		sc->sc_protrix = ath_tx_findrix(sc, 2*1);
51064fa8d4efSDaniel Eischen 	/* NB: caller is responsible for resetting rate control state */
51073e50ec2cSSam Leffler #undef N
51085591b213SSam Leffler }
51095591b213SSam Leffler 
5110c42a7b7eSSam Leffler static void
51112e986da5SSam Leffler ath_watchdog(void *arg)
5112c42a7b7eSSam Leffler {
51132e986da5SSam Leffler 	struct ath_softc *sc = arg;
5114ef27340cSAdrian Chadd 	int do_reset = 0;
5115c42a7b7eSSam Leffler 
51162e986da5SSam Leffler 	if (sc->sc_wd_timer != 0 && --sc->sc_wd_timer == 0) {
51172e986da5SSam Leffler 		struct ifnet *ifp = sc->sc_ifp;
5118459bc4f0SSam Leffler 		uint32_t hangs;
5119459bc4f0SSam Leffler 
5120459bc4f0SSam Leffler 		if (ath_hal_gethangstate(sc->sc_ah, 0xffff, &hangs) &&
5121459bc4f0SSam Leffler 		    hangs != 0) {
5122459bc4f0SSam Leffler 			if_printf(ifp, "%s hang detected (0x%x)\n",
5123459bc4f0SSam Leffler 			    hangs & 0xff ? "bb" : "mac", hangs);
5124459bc4f0SSam Leffler 		} else
5125c42a7b7eSSam Leffler 			if_printf(ifp, "device timeout\n");
5126ef27340cSAdrian Chadd 		do_reset = 1;
5127c42a7b7eSSam Leffler 		ifp->if_oerrors++;
5128c42a7b7eSSam Leffler 		sc->sc_stats.ast_watchdog++;
5129c42a7b7eSSam Leffler 	}
5130ef27340cSAdrian Chadd 
5131ef27340cSAdrian Chadd 	/*
5132ef27340cSAdrian Chadd 	 * We can't hold the lock across the ath_reset() call.
5133d52f7132SAdrian Chadd 	 *
5134d52f7132SAdrian Chadd 	 * And since this routine can't hold a lock and sleep,
5135d52f7132SAdrian Chadd 	 * do the reset deferred.
5136ef27340cSAdrian Chadd 	 */
5137ef27340cSAdrian Chadd 	if (do_reset) {
5138d52f7132SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_resettask);
5139ef27340cSAdrian Chadd 	}
5140ef27340cSAdrian Chadd 
51412e986da5SSam Leffler 	callout_schedule(&sc->sc_wd_ch, hz);
5142c42a7b7eSSam Leffler }
5143c42a7b7eSSam Leffler 
5144b8f2a853SAdrian Chadd /*
5145b8f2a853SAdrian Chadd  * Fetch the rate control statistics for the given node.
5146b8f2a853SAdrian Chadd  */
5147b8f2a853SAdrian Chadd static int
5148b8f2a853SAdrian Chadd ath_ioctl_ratestats(struct ath_softc *sc, struct ath_rateioctl *rs)
5149b8f2a853SAdrian Chadd {
5150b8f2a853SAdrian Chadd 	struct ath_node *an;
5151b8f2a853SAdrian Chadd 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
5152b8f2a853SAdrian Chadd 	struct ieee80211_node *ni;
5153b8f2a853SAdrian Chadd 	int error = 0;
5154b8f2a853SAdrian Chadd 
5155b8f2a853SAdrian Chadd 	/* Perform a lookup on the given node */
5156b8f2a853SAdrian Chadd 	ni = ieee80211_find_node(&ic->ic_sta, rs->is_u.macaddr);
5157b8f2a853SAdrian Chadd 	if (ni == NULL) {
5158b8f2a853SAdrian Chadd 		error = EINVAL;
5159b8f2a853SAdrian Chadd 		goto bad;
5160b8f2a853SAdrian Chadd 	}
5161b8f2a853SAdrian Chadd 
5162b8f2a853SAdrian Chadd 	/* Lock the ath_node */
5163b8f2a853SAdrian Chadd 	an = ATH_NODE(ni);
5164b8f2a853SAdrian Chadd 	ATH_NODE_LOCK(an);
5165b8f2a853SAdrian Chadd 
5166b8f2a853SAdrian Chadd 	/* Fetch the rate control stats for this node */
5167b8f2a853SAdrian Chadd 	error = ath_rate_fetch_node_stats(sc, an, rs);
5168b8f2a853SAdrian Chadd 
5169b8f2a853SAdrian Chadd 	/* No matter what happens here, just drop through */
5170b8f2a853SAdrian Chadd 
5171b8f2a853SAdrian Chadd 	/* Unlock the ath_node */
5172b8f2a853SAdrian Chadd 	ATH_NODE_UNLOCK(an);
5173b8f2a853SAdrian Chadd 
5174b8f2a853SAdrian Chadd 	/* Unref the node */
5175b8f2a853SAdrian Chadd 	ieee80211_node_decref(ni);
5176b8f2a853SAdrian Chadd 
5177b8f2a853SAdrian Chadd bad:
5178b8f2a853SAdrian Chadd 	return (error);
5179b8f2a853SAdrian Chadd }
5180b8f2a853SAdrian Chadd 
5181a585a9a1SSam Leffler #ifdef ATH_DIAGAPI
5182c42a7b7eSSam Leffler /*
5183c42a7b7eSSam Leffler  * Diagnostic interface to the HAL.  This is used by various
5184c42a7b7eSSam Leffler  * tools to do things like retrieve register contents for
5185c42a7b7eSSam Leffler  * debugging.  The mechanism is intentionally opaque so that
5186c42a7b7eSSam Leffler  * it can change frequently w/o concern for compatiblity.
5187c42a7b7eSSam Leffler  */
5188c42a7b7eSSam Leffler static int
5189c42a7b7eSSam Leffler ath_ioctl_diag(struct ath_softc *sc, struct ath_diag *ad)
5190c42a7b7eSSam Leffler {
5191c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5192c42a7b7eSSam Leffler 	u_int id = ad->ad_id & ATH_DIAG_ID;
5193c42a7b7eSSam Leffler 	void *indata = NULL;
5194c42a7b7eSSam Leffler 	void *outdata = NULL;
5195c42a7b7eSSam Leffler 	u_int32_t insize = ad->ad_in_size;
5196c42a7b7eSSam Leffler 	u_int32_t outsize = ad->ad_out_size;
5197c42a7b7eSSam Leffler 	int error = 0;
5198c42a7b7eSSam Leffler 
5199c42a7b7eSSam Leffler 	if (ad->ad_id & ATH_DIAG_IN) {
5200c42a7b7eSSam Leffler 		/*
5201c42a7b7eSSam Leffler 		 * Copy in data.
5202c42a7b7eSSam Leffler 		 */
5203c42a7b7eSSam Leffler 		indata = malloc(insize, M_TEMP, M_NOWAIT);
5204c42a7b7eSSam Leffler 		if (indata == NULL) {
5205c42a7b7eSSam Leffler 			error = ENOMEM;
5206c42a7b7eSSam Leffler 			goto bad;
5207c42a7b7eSSam Leffler 		}
5208c42a7b7eSSam Leffler 		error = copyin(ad->ad_in_data, indata, insize);
5209c42a7b7eSSam Leffler 		if (error)
5210c42a7b7eSSam Leffler 			goto bad;
5211c42a7b7eSSam Leffler 	}
5212c42a7b7eSSam Leffler 	if (ad->ad_id & ATH_DIAG_DYN) {
5213c42a7b7eSSam Leffler 		/*
5214c42a7b7eSSam Leffler 		 * Allocate a buffer for the results (otherwise the HAL
5215c42a7b7eSSam Leffler 		 * returns a pointer to a buffer where we can read the
5216c42a7b7eSSam Leffler 		 * results).  Note that we depend on the HAL leaving this
5217c42a7b7eSSam Leffler 		 * pointer for us to use below in reclaiming the buffer;
5218c42a7b7eSSam Leffler 		 * may want to be more defensive.
5219c42a7b7eSSam Leffler 		 */
5220c42a7b7eSSam Leffler 		outdata = malloc(outsize, M_TEMP, M_NOWAIT);
5221c42a7b7eSSam Leffler 		if (outdata == NULL) {
5222c42a7b7eSSam Leffler 			error = ENOMEM;
5223c42a7b7eSSam Leffler 			goto bad;
5224c42a7b7eSSam Leffler 		}
5225c42a7b7eSSam Leffler 	}
5226c42a7b7eSSam Leffler 	if (ath_hal_getdiagstate(ah, id, indata, insize, &outdata, &outsize)) {
5227c42a7b7eSSam Leffler 		if (outsize < ad->ad_out_size)
5228c42a7b7eSSam Leffler 			ad->ad_out_size = outsize;
5229c42a7b7eSSam Leffler 		if (outdata != NULL)
5230c42a7b7eSSam Leffler 			error = copyout(outdata, ad->ad_out_data,
5231c42a7b7eSSam Leffler 					ad->ad_out_size);
5232c42a7b7eSSam Leffler 	} else {
5233c42a7b7eSSam Leffler 		error = EINVAL;
5234c42a7b7eSSam Leffler 	}
5235c42a7b7eSSam Leffler bad:
5236c42a7b7eSSam Leffler 	if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
5237c42a7b7eSSam Leffler 		free(indata, M_TEMP);
5238c42a7b7eSSam Leffler 	if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
5239c42a7b7eSSam Leffler 		free(outdata, M_TEMP);
5240c42a7b7eSSam Leffler 	return error;
5241c42a7b7eSSam Leffler }
5242a585a9a1SSam Leffler #endif /* ATH_DIAGAPI */
5243c42a7b7eSSam Leffler 
5244c42a7b7eSSam Leffler static int
5245c42a7b7eSSam Leffler ath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
5246c42a7b7eSSam Leffler {
5247c42a7b7eSSam Leffler #define	IS_RUNNING(ifp) \
524813f4c340SRobert Watson 	((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
5249c42a7b7eSSam Leffler 	struct ath_softc *sc = ifp->if_softc;
5250b032f27cSSam Leffler 	struct ieee80211com *ic = ifp->if_l2com;
5251c42a7b7eSSam Leffler 	struct ifreq *ifr = (struct ifreq *)data;
525284784be1SSam Leffler 	const HAL_RATE_TABLE *rt;
5253c42a7b7eSSam Leffler 	int error = 0;
5254c42a7b7eSSam Leffler 
5255c42a7b7eSSam Leffler 	switch (cmd) {
5256c42a7b7eSSam Leffler 	case SIOCSIFFLAGS:
525731a8c1edSAndrew Thompson 		ATH_LOCK(sc);
5258c42a7b7eSSam Leffler 		if (IS_RUNNING(ifp)) {
5259c42a7b7eSSam Leffler 			/*
5260c42a7b7eSSam Leffler 			 * To avoid rescanning another access point,
5261c42a7b7eSSam Leffler 			 * do not call ath_init() here.  Instead,
5262c42a7b7eSSam Leffler 			 * only reflect promisc mode settings.
5263c42a7b7eSSam Leffler 			 */
5264c42a7b7eSSam Leffler 			ath_mode_init(sc);
5265c42a7b7eSSam Leffler 		} else if (ifp->if_flags & IFF_UP) {
5266c42a7b7eSSam Leffler 			/*
5267c42a7b7eSSam Leffler 			 * Beware of being called during attach/detach
5268c42a7b7eSSam Leffler 			 * to reset promiscuous mode.  In that case we
5269c42a7b7eSSam Leffler 			 * will still be marked UP but not RUNNING.
5270c42a7b7eSSam Leffler 			 * However trying to re-init the interface
5271c42a7b7eSSam Leffler 			 * is the wrong thing to do as we've already
5272c42a7b7eSSam Leffler 			 * torn down much of our state.  There's
5273c42a7b7eSSam Leffler 			 * probably a better way to deal with this.
5274c42a7b7eSSam Leffler 			 */
5275b032f27cSSam Leffler 			if (!sc->sc_invalid)
5276fc74a9f9SBrooks Davis 				ath_init(sc);	/* XXX lose error */
5277d3ac945bSSam Leffler 		} else {
5278c42a7b7eSSam Leffler 			ath_stop_locked(ifp);
5279d3ac945bSSam Leffler #ifdef notyet
5280d3ac945bSSam Leffler 			/* XXX must wakeup in places like ath_vap_delete */
5281d3ac945bSSam Leffler 			if (!sc->sc_invalid)
5282d3ac945bSSam Leffler 				ath_hal_setpower(sc->sc_ah, HAL_PM_FULL_SLEEP);
5283d3ac945bSSam Leffler #endif
5284d3ac945bSSam Leffler 		}
528531a8c1edSAndrew Thompson 		ATH_UNLOCK(sc);
5286c42a7b7eSSam Leffler 		break;
5287b032f27cSSam Leffler 	case SIOCGIFMEDIA:
5288b032f27cSSam Leffler 	case SIOCSIFMEDIA:
5289b032f27cSSam Leffler 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
5290b032f27cSSam Leffler 		break;
5291c42a7b7eSSam Leffler 	case SIOCGATHSTATS:
5292c42a7b7eSSam Leffler 		/* NB: embed these numbers to get a consistent view */
5293c42a7b7eSSam Leffler 		sc->sc_stats.ast_tx_packets = ifp->if_opackets;
5294c42a7b7eSSam Leffler 		sc->sc_stats.ast_rx_packets = ifp->if_ipackets;
529584784be1SSam Leffler 		sc->sc_stats.ast_tx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgtxrssi);
529684784be1SSam Leffler 		sc->sc_stats.ast_rx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgrssi);
5297584f7327SSam Leffler #ifdef IEEE80211_SUPPORT_TDMA
529810ad9a77SSam Leffler 		sc->sc_stats.ast_tdma_tsfadjp = TDMA_AVG(sc->sc_avgtsfdeltap);
529910ad9a77SSam Leffler 		sc->sc_stats.ast_tdma_tsfadjm = TDMA_AVG(sc->sc_avgtsfdeltam);
530010ad9a77SSam Leffler #endif
530184784be1SSam Leffler 		rt = sc->sc_currates;
530246d4d74cSSam Leffler 		sc->sc_stats.ast_tx_rate =
530346d4d74cSSam Leffler 		    rt->info[sc->sc_txrix].dot11Rate &~ IEEE80211_RATE_BASIC;
53046aa113fdSAdrian Chadd 		if (rt->info[sc->sc_txrix].phy & IEEE80211_T_HT)
53056aa113fdSAdrian Chadd 			sc->sc_stats.ast_tx_rate |= IEEE80211_RATE_MCS;
5306c42a7b7eSSam Leffler 		return copyout(&sc->sc_stats,
5307c42a7b7eSSam Leffler 		    ifr->ifr_data, sizeof (sc->sc_stats));
530894fe37d2SAdrian Chadd 	case SIOCGATHAGSTATS:
530994fe37d2SAdrian Chadd 		return copyout(&sc->sc_aggr_stats,
531094fe37d2SAdrian Chadd 		    ifr->ifr_data, sizeof (sc->sc_aggr_stats));
53113fc21fedSSam Leffler 	case SIOCZATHSTATS:
53123fc21fedSSam Leffler 		error = priv_check(curthread, PRIV_DRIVER);
53139467e3f3SAdrian Chadd 		if (error == 0) {
53143fc21fedSSam Leffler 			memset(&sc->sc_stats, 0, sizeof(sc->sc_stats));
531541b6b507SAdrian Chadd 			memset(&sc->sc_aggr_stats, 0,
531641b6b507SAdrian Chadd 			    sizeof(sc->sc_aggr_stats));
53179467e3f3SAdrian Chadd 			memset(&sc->sc_intr_stats, 0,
53189467e3f3SAdrian Chadd 			    sizeof(sc->sc_intr_stats));
53199467e3f3SAdrian Chadd 		}
53203fc21fedSSam Leffler 		break;
5321a585a9a1SSam Leffler #ifdef ATH_DIAGAPI
5322c42a7b7eSSam Leffler 	case SIOCGATHDIAG:
5323c42a7b7eSSam Leffler 		error = ath_ioctl_diag(sc, (struct ath_diag *) ifr);
5324c42a7b7eSSam Leffler 		break;
5325f51c84eaSAdrian Chadd 	case SIOCGATHPHYERR:
5326f51c84eaSAdrian Chadd 		error = ath_ioctl_phyerr(sc,(struct ath_diag*) ifr);
5327f51c84eaSAdrian Chadd 		break;
5328a585a9a1SSam Leffler #endif
5329b8f2a853SAdrian Chadd 	case SIOCGATHNODERATESTATS:
5330b8f2a853SAdrian Chadd 		error = ath_ioctl_ratestats(sc, (struct ath_rateioctl *) ifr);
5331b8f2a853SAdrian Chadd 		break;
533231a8c1edSAndrew Thompson 	case SIOCGIFADDR:
5333b032f27cSSam Leffler 		error = ether_ioctl(ifp, cmd, data);
5334c42a7b7eSSam Leffler 		break;
533531a8c1edSAndrew Thompson 	default:
533631a8c1edSAndrew Thompson 		error = EINVAL;
533731a8c1edSAndrew Thompson 		break;
5338c42a7b7eSSam Leffler 	}
5339c42a7b7eSSam Leffler 	return error;
5340a614e076SSam Leffler #undef IS_RUNNING
5341c42a7b7eSSam Leffler }
5342c42a7b7eSSam Leffler 
5343c42a7b7eSSam Leffler /*
5344c42a7b7eSSam Leffler  * Announce various information on device/driver attach.
5345c42a7b7eSSam Leffler  */
5346c42a7b7eSSam Leffler static void
5347c42a7b7eSSam Leffler ath_announce(struct ath_softc *sc)
5348c42a7b7eSSam Leffler {
5349fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
5350c42a7b7eSSam Leffler 	struct ath_hal *ah = sc->sc_ah;
5351c42a7b7eSSam Leffler 
5352498657cfSSam Leffler 	if_printf(ifp, "AR%s mac %d.%d RF%s phy %d.%d\n",
5353498657cfSSam Leffler 		ath_hal_mac_name(ah), ah->ah_macVersion, ah->ah_macRev,
5354498657cfSSam Leffler 		ath_hal_rf_name(ah), ah->ah_phyRev >> 4, ah->ah_phyRev & 0xf);
535546a924c4SAdrian Chadd 	if_printf(ifp, "2GHz radio: 0x%.4x; 5GHz radio: 0x%.4x\n",
535646a924c4SAdrian Chadd 		ah->ah_analog2GhzRev, ah->ah_analog5GhzRev);
5357c42a7b7eSSam Leffler 	if (bootverbose) {
5358c42a7b7eSSam Leffler 		int i;
5359c42a7b7eSSam Leffler 		for (i = 0; i <= WME_AC_VO; i++) {
5360c42a7b7eSSam Leffler 			struct ath_txq *txq = sc->sc_ac2q[i];
5361c42a7b7eSSam Leffler 			if_printf(ifp, "Use hw queue %u for %s traffic\n",
5362c42a7b7eSSam Leffler 				txq->axq_qnum, ieee80211_wme_acnames[i]);
5363c42a7b7eSSam Leffler 		}
5364c42a7b7eSSam Leffler 		if_printf(ifp, "Use hw queue %u for CAB traffic\n",
5365c42a7b7eSSam Leffler 			sc->sc_cabq->axq_qnum);
5366c42a7b7eSSam Leffler 		if_printf(ifp, "Use hw queue %u for beacons\n", sc->sc_bhalq);
5367c42a7b7eSSam Leffler 	}
5368e2d787faSSam Leffler 	if (ath_rxbuf != ATH_RXBUF)
5369e2d787faSSam Leffler 		if_printf(ifp, "using %u rx buffers\n", ath_rxbuf);
5370e2d787faSSam Leffler 	if (ath_txbuf != ATH_TXBUF)
5371e2d787faSSam Leffler 		if_printf(ifp, "using %u tx buffers\n", ath_txbuf);
53729ac01d39SRui Paulo 	if (sc->sc_mcastkey && bootverbose)
53739ac01d39SRui Paulo 		if_printf(ifp, "using multicast key search\n");
5374c42a7b7eSSam Leffler }
537510ad9a77SSam Leffler 
537648237774SAdrian Chadd static void
537748237774SAdrian Chadd ath_dfs_tasklet(void *p, int npending)
537848237774SAdrian Chadd {
537948237774SAdrian Chadd 	struct ath_softc *sc = (struct ath_softc *) p;
538048237774SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
538148237774SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
538248237774SAdrian Chadd 
538348237774SAdrian Chadd 	/*
538448237774SAdrian Chadd 	 * If previous processing has found a radar event,
538548237774SAdrian Chadd 	 * signal this to the net80211 layer to begin DFS
538648237774SAdrian Chadd 	 * processing.
538748237774SAdrian Chadd 	 */
538848237774SAdrian Chadd 	if (ath_dfs_process_radar_event(sc, sc->sc_curchan)) {
538948237774SAdrian Chadd 		/* DFS event found, initiate channel change */
539006fc4a10SAdrian Chadd 		/*
539106fc4a10SAdrian Chadd 		 * XXX doesn't currently tell us whether the event
539206fc4a10SAdrian Chadd 		 * XXX was found in the primary or extension
539306fc4a10SAdrian Chadd 		 * XXX channel!
539406fc4a10SAdrian Chadd 		 */
539506fc4a10SAdrian Chadd 		IEEE80211_LOCK(ic);
539648237774SAdrian Chadd 		ieee80211_dfs_notify_radar(ic, sc->sc_curchan);
539706fc4a10SAdrian Chadd 		IEEE80211_UNLOCK(ic);
539848237774SAdrian Chadd 	}
539948237774SAdrian Chadd }
540048237774SAdrian Chadd 
54010eb81626SAdrian Chadd /*
54020eb81626SAdrian Chadd  * Enable/disable power save.  This must be called with
54030eb81626SAdrian Chadd  * no TX driver locks currently held, so it should only
54040eb81626SAdrian Chadd  * be called from the RX path (which doesn't hold any
54050eb81626SAdrian Chadd  * TX driver locks.)
54060eb81626SAdrian Chadd  */
54070eb81626SAdrian Chadd static void
54080eb81626SAdrian Chadd ath_node_powersave(struct ieee80211_node *ni, int enable)
54090eb81626SAdrian Chadd {
54100eb81626SAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
54110eb81626SAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
54120eb81626SAdrian Chadd 	struct ath_softc *sc = ic->ic_ifp->if_softc;
54130eb81626SAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
54140eb81626SAdrian Chadd 
54150eb81626SAdrian Chadd 	ATH_NODE_UNLOCK_ASSERT(an);
54160eb81626SAdrian Chadd 	/* XXX and no TXQ locks should be held here */
54170eb81626SAdrian Chadd 
54180eb81626SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE, "%s: ni=%p, enable=%d\n",
54190eb81626SAdrian Chadd 	    __func__, ni, enable);
54200eb81626SAdrian Chadd 
54210eb81626SAdrian Chadd 	/* Suspend or resume software queue handling */
54220eb81626SAdrian Chadd 	if (enable)
54230eb81626SAdrian Chadd 		ath_tx_node_sleep(sc, an);
54240eb81626SAdrian Chadd 	else
54250eb81626SAdrian Chadd 		ath_tx_node_wakeup(sc, an);
54260eb81626SAdrian Chadd 
54270eb81626SAdrian Chadd 	/* Update net80211 state */
54280eb81626SAdrian Chadd 	avp->av_node_ps(ni, enable);
54290eb81626SAdrian Chadd }
54300eb81626SAdrian Chadd 
5431*548a605dSAdrian Chadd /*
5432*548a605dSAdrian Chadd  * Notification from net80211 that the powersave queue state has
5433*548a605dSAdrian Chadd  * changed.
5434*548a605dSAdrian Chadd  *
5435*548a605dSAdrian Chadd  * Since the software queue also may have some frames:
5436*548a605dSAdrian Chadd  *
5437*548a605dSAdrian Chadd  * + if the node software queue has frames and the TID state
5438*548a605dSAdrian Chadd  *   is 0, we set the TIM;
5439*548a605dSAdrian Chadd  * + if the node and the stack are both empty, we clear the TIM bit.
5440*548a605dSAdrian Chadd  * + If the stack tries to set the bit, always set it.
5441*548a605dSAdrian Chadd  * + If the stack tries to clear the bit, only clear it if the
5442*548a605dSAdrian Chadd  *   software queue in question is also cleared.
5443*548a605dSAdrian Chadd  *
5444*548a605dSAdrian Chadd  * TODO: this is called during node teardown; so let's ensure this
5445*548a605dSAdrian Chadd  * is all correctly handled and that the TIM bit is cleared.
5446*548a605dSAdrian Chadd  * It may be that the node flush is called _AFTER_ the net80211
5447*548a605dSAdrian Chadd  * stack clears the TIM.
5448*548a605dSAdrian Chadd  *
5449*548a605dSAdrian Chadd  * Here is the racy part.  Since it's possible >1 concurrent,
5450*548a605dSAdrian Chadd  * overlapping TXes will appear complete with a TX completion in
5451*548a605dSAdrian Chadd  * another thread, it's possible that the concurrent TIM calls will
5452*548a605dSAdrian Chadd  * clash.  We can't hold the node lock here because setting the
5453*548a605dSAdrian Chadd  * TIM grabs the net80211 comlock and this may cause a LOR.
5454*548a605dSAdrian Chadd  * The solution is either to totally serialise _everything_ at
5455*548a605dSAdrian Chadd  * this point (ie, all TX, completion and any reset/flush go into
5456*548a605dSAdrian Chadd  * one taskqueue) or a new "ath TIM lock" needs to be created that
5457*548a605dSAdrian Chadd  * just wraps the driver state change and this call to avp->av_set_tim().
5458*548a605dSAdrian Chadd  *
5459*548a605dSAdrian Chadd  * The same race exists in the net80211 power save queue handling
5460*548a605dSAdrian Chadd  * as well.  Since multiple transmitting threads may queue frames
5461*548a605dSAdrian Chadd  * into the driver, as well as ps-poll and the driver transmitting
5462*548a605dSAdrian Chadd  * frames (and thus clearing the psq), it's quite possible that
5463*548a605dSAdrian Chadd  * a packet entering the PSQ and a ps-poll being handled will
5464*548a605dSAdrian Chadd  * race, causing the TIM to be cleared and not re-set.
5465*548a605dSAdrian Chadd  */
5466*548a605dSAdrian Chadd static int
5467*548a605dSAdrian Chadd ath_node_set_tim(struct ieee80211_node *ni, int enable)
5468*548a605dSAdrian Chadd {
5469*548a605dSAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
5470*548a605dSAdrian Chadd 	struct ath_softc *sc = ic->ic_ifp->if_softc;
5471*548a605dSAdrian Chadd 	struct ath_node *an = ATH_NODE(ni);
5472*548a605dSAdrian Chadd 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
5473*548a605dSAdrian Chadd 	int changed = 0;
5474*548a605dSAdrian Chadd 
5475*548a605dSAdrian Chadd 	ATH_NODE_UNLOCK_ASSERT(an);
5476*548a605dSAdrian Chadd 
5477*548a605dSAdrian Chadd 	/*
5478*548a605dSAdrian Chadd 	 * For now, just track and then update the TIM.
5479*548a605dSAdrian Chadd 	 */
5480*548a605dSAdrian Chadd 	ATH_NODE_LOCK(an);
5481*548a605dSAdrian Chadd 	an->an_stack_psq = enable;
5482*548a605dSAdrian Chadd 
5483*548a605dSAdrian Chadd 	/*
5484*548a605dSAdrian Chadd 	 * This will get called for all operating modes,
5485*548a605dSAdrian Chadd 	 * even if avp->av_set_tim is unset.
5486*548a605dSAdrian Chadd 	 * It's currently set for hostap/ibss modes; but
5487*548a605dSAdrian Chadd 	 * the same infrastructure is used for both STA
5488*548a605dSAdrian Chadd 	 * and AP/IBSS node power save.
5489*548a605dSAdrian Chadd 	 */
5490*548a605dSAdrian Chadd 	if (avp->av_set_tim == NULL) {
5491*548a605dSAdrian Chadd 		ATH_NODE_UNLOCK(an);
5492*548a605dSAdrian Chadd 		return (0);
5493*548a605dSAdrian Chadd 	}
5494*548a605dSAdrian Chadd 
5495*548a605dSAdrian Chadd 	/*
5496*548a605dSAdrian Chadd 	 * If setting the bit, always set it here.
5497*548a605dSAdrian Chadd 	 * If clearing the bit, only clear it if the
5498*548a605dSAdrian Chadd 	 * software queue is also empty.
5499*548a605dSAdrian Chadd 	 *
5500*548a605dSAdrian Chadd 	 * If the node has left power save, just clear the TIM
5501*548a605dSAdrian Chadd 	 * bit regardless of the state of the power save queue.
5502*548a605dSAdrian Chadd 	 *
5503*548a605dSAdrian Chadd 	 * XXX TODO: although atomics are used, it's quite possible
5504*548a605dSAdrian Chadd 	 * that a race will occur between this and setting/clearing
5505*548a605dSAdrian Chadd 	 * in another thread.  TX completion will occur always in
5506*548a605dSAdrian Chadd 	 * one thread, however setting/clearing the TIM bit can come
5507*548a605dSAdrian Chadd 	 * from a variety of different process contexts!
5508*548a605dSAdrian Chadd 	 */
5509*548a605dSAdrian Chadd 	if (enable && an->an_tim_set == 1) {
5510*548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
5511*548a605dSAdrian Chadd 		    "%s: an=%p, enable=%d, tim_set=1, ignoring\n",
5512*548a605dSAdrian Chadd 		    __func__, an, enable);
5513*548a605dSAdrian Chadd 		ATH_NODE_UNLOCK(an);
5514*548a605dSAdrian Chadd 	} else if (enable) {
5515*548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
5516*548a605dSAdrian Chadd 		    "%s: an=%p, enable=%d, enabling TIM\n",
5517*548a605dSAdrian Chadd 		    __func__, an, enable);
5518*548a605dSAdrian Chadd 		an->an_tim_set = 1;
5519*548a605dSAdrian Chadd 		ATH_NODE_UNLOCK(an);
5520*548a605dSAdrian Chadd 		changed = avp->av_set_tim(ni, enable);
5521*548a605dSAdrian Chadd 	} else if (atomic_load_acq_int(&an->an_swq_depth) == 0) {
5522*548a605dSAdrian Chadd 		/* disable */
5523*548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
5524*548a605dSAdrian Chadd 		    "%s: an=%p, enable=%d, an_swq_depth == 0, disabling\n",
5525*548a605dSAdrian Chadd 		    __func__, an, enable);
5526*548a605dSAdrian Chadd 		an->an_tim_set = 0;
5527*548a605dSAdrian Chadd 		ATH_NODE_UNLOCK(an);
5528*548a605dSAdrian Chadd 		changed = avp->av_set_tim(ni, enable);
5529*548a605dSAdrian Chadd 	} else if (! an->an_is_powersave) {
5530*548a605dSAdrian Chadd 		/*
5531*548a605dSAdrian Chadd 		 * disable regardless; the node isn't in powersave now
5532*548a605dSAdrian Chadd 		 */
5533*548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
5534*548a605dSAdrian Chadd 		    "%s: an=%p, enable=%d, an_pwrsave=0, disabling\n",
5535*548a605dSAdrian Chadd 		    __func__, an, enable);
5536*548a605dSAdrian Chadd 		an->an_tim_set = 0;
5537*548a605dSAdrian Chadd 		ATH_NODE_UNLOCK(an);
5538*548a605dSAdrian Chadd 		changed = avp->av_set_tim(ni, enable);
5539*548a605dSAdrian Chadd 	} else {
5540*548a605dSAdrian Chadd 		/*
5541*548a605dSAdrian Chadd 		 * psq disable, node is currently in powersave, node
5542*548a605dSAdrian Chadd 		 * software queue isn't empty, so don't clear the TIM bit
5543*548a605dSAdrian Chadd 		 * for now.
5544*548a605dSAdrian Chadd 		 */
5545*548a605dSAdrian Chadd 		ATH_NODE_UNLOCK(an);
5546*548a605dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
5547*548a605dSAdrian Chadd 		    "%s: enable=%d, an_swq_depth > 0, ignoring\n",
5548*548a605dSAdrian Chadd 		    __func__, enable);
5549*548a605dSAdrian Chadd 		changed = 0;
5550*548a605dSAdrian Chadd 	}
5551*548a605dSAdrian Chadd 
5552*548a605dSAdrian Chadd 	return (changed);
5553*548a605dSAdrian Chadd }
5554*548a605dSAdrian Chadd 
5555*548a605dSAdrian Chadd /*
5556*548a605dSAdrian Chadd  * Set or update the TIM from the software queue.
5557*548a605dSAdrian Chadd  *
5558*548a605dSAdrian Chadd  * Check the software queue depth before attempting to do lock
5559*548a605dSAdrian Chadd  * anything; that avoids trying to obtain the lock.  Then,
5560*548a605dSAdrian Chadd  * re-check afterwards to ensure nothing has changed in the
5561*548a605dSAdrian Chadd  * meantime.
5562*548a605dSAdrian Chadd  *
5563*548a605dSAdrian Chadd  * set:   This is designed to be called from the TX path, after
5564*548a605dSAdrian Chadd  *        a frame has been queued; to see if the swq > 0.
5565*548a605dSAdrian Chadd  *
5566*548a605dSAdrian Chadd  * clear: This is designed to be called from the buffer completion point
5567*548a605dSAdrian Chadd  *        (right now it's ath_tx_default_comp()) where the state of
5568*548a605dSAdrian Chadd  *        a software queue has changed.
5569*548a605dSAdrian Chadd  *
5570*548a605dSAdrian Chadd  * It makes sense to place it at buffer free / completion rather
5571*548a605dSAdrian Chadd  * than after each software queue operation, as there's no real
5572*548a605dSAdrian Chadd  * point in churning the TIM bit as the last frames in the software
5573*548a605dSAdrian Chadd  * queue are transmitted.  If they fail and we retry them, we'd
5574*548a605dSAdrian Chadd  * just be setting the TIM bit again anyway.
5575*548a605dSAdrian Chadd  */
5576*548a605dSAdrian Chadd void
5577*548a605dSAdrian Chadd ath_tx_update_tim(struct ath_softc *sc, struct ieee80211_node *ni,
5578*548a605dSAdrian Chadd      int enable)
5579*548a605dSAdrian Chadd {
5580*548a605dSAdrian Chadd 	struct ath_node *an;
5581*548a605dSAdrian Chadd 	struct ath_vap *avp;
5582*548a605dSAdrian Chadd 
5583*548a605dSAdrian Chadd 	/* Don't do this for broadcast/etc frames */
5584*548a605dSAdrian Chadd 	if (ni == NULL)
5585*548a605dSAdrian Chadd 		return;
5586*548a605dSAdrian Chadd 
5587*548a605dSAdrian Chadd 	an = ATH_NODE(ni);
5588*548a605dSAdrian Chadd 	avp = ATH_VAP(ni->ni_vap);
5589*548a605dSAdrian Chadd 
5590*548a605dSAdrian Chadd 	/*
5591*548a605dSAdrian Chadd 	 * And for operating modes without the TIM handler set, let's
5592*548a605dSAdrian Chadd 	 * just skip those.
5593*548a605dSAdrian Chadd 	 */
5594*548a605dSAdrian Chadd 	if (avp->av_set_tim == NULL)
5595*548a605dSAdrian Chadd 		return;
5596*548a605dSAdrian Chadd 
5597*548a605dSAdrian Chadd 	ATH_NODE_UNLOCK_ASSERT(an);
5598*548a605dSAdrian Chadd 
5599*548a605dSAdrian Chadd 	if (enable) {
5600*548a605dSAdrian Chadd 		/*
5601*548a605dSAdrian Chadd 		 * Don't bother grabbing the lock unless the queue is not
5602*548a605dSAdrian Chadd 		 * empty.
5603*548a605dSAdrian Chadd 		 */
5604*548a605dSAdrian Chadd 		if (atomic_load_acq_int(&an->an_swq_depth) == 0)
5605*548a605dSAdrian Chadd 			return;
5606*548a605dSAdrian Chadd 
5607*548a605dSAdrian Chadd 		ATH_NODE_LOCK(an);
5608*548a605dSAdrian Chadd 		if (an->an_is_powersave &&
5609*548a605dSAdrian Chadd 		    an->an_tim_set == 0 &&
5610*548a605dSAdrian Chadd 		    atomic_load_acq_int(&an->an_swq_depth) != 0) {
5611*548a605dSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
5612*548a605dSAdrian Chadd 			    "%s: an=%p, swq_depth>0, tim_set=0, set!\n",
5613*548a605dSAdrian Chadd 			    __func__, an);
5614*548a605dSAdrian Chadd 			an->an_tim_set = 1;
5615*548a605dSAdrian Chadd 			ATH_NODE_UNLOCK(an);
5616*548a605dSAdrian Chadd 			(void) avp->av_set_tim(ni, 1);
5617*548a605dSAdrian Chadd 		} else {
5618*548a605dSAdrian Chadd 			ATH_NODE_UNLOCK(an);
5619*548a605dSAdrian Chadd 		}
5620*548a605dSAdrian Chadd 	} else {
5621*548a605dSAdrian Chadd 		/*
5622*548a605dSAdrian Chadd 		 * Don't bother grabbing the lock unless the queue is empty.
5623*548a605dSAdrian Chadd 		 */
5624*548a605dSAdrian Chadd 		if (atomic_load_acq_int(&an->an_swq_depth) != 0)
5625*548a605dSAdrian Chadd 			return;
5626*548a605dSAdrian Chadd 
5627*548a605dSAdrian Chadd 		ATH_NODE_LOCK(an);
5628*548a605dSAdrian Chadd 		if (an->an_is_powersave &&
5629*548a605dSAdrian Chadd 		    an->an_stack_psq == 0 &&
5630*548a605dSAdrian Chadd 		    an->an_tim_set == 1 &&
5631*548a605dSAdrian Chadd 		    atomic_load_acq_int(&an->an_swq_depth) == 0) {
5632*548a605dSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
5633*548a605dSAdrian Chadd 			    "%s: an=%p, swq_depth=0, tim_set=1, psq_set=0,"
5634*548a605dSAdrian Chadd 			    " clear!\n",
5635*548a605dSAdrian Chadd 			    __func__, an);
5636*548a605dSAdrian Chadd 			an->an_tim_set = 0;
5637*548a605dSAdrian Chadd 			ATH_NODE_UNLOCK(an);
5638*548a605dSAdrian Chadd 			(void) avp->av_set_tim(ni, 0);
5639*548a605dSAdrian Chadd 		} else {
5640*548a605dSAdrian Chadd 			ATH_NODE_UNLOCK(an);
5641*548a605dSAdrian Chadd 		}
5642*548a605dSAdrian Chadd 	}
5643*548a605dSAdrian Chadd }
56440eb81626SAdrian Chadd 
5645dba9c859SAdrian Chadd MODULE_VERSION(if_ath, 1);
5646dba9c859SAdrian Chadd MODULE_DEPEND(if_ath, wlan, 1, 1, 1);          /* 802.11 media layer */
564758816f3fSAdrian Chadd #if	defined(IEEE80211_ALQ) || defined(AH_DEBUG_ALQ)
564858816f3fSAdrian Chadd MODULE_DEPEND(if_ath, alq, 1, 1, 1);
564958816f3fSAdrian Chadd #endif
5650